1 /** $MirOS: src/sys/kern/init_main.c,v 1.34 2014/01/11 18:16:16 tg Exp $ */
2 /* $OpenBSD: init_main.c,v 1.120 2004/11/23 19:08:55 miod Exp $ */
3 /* $NetBSD: init_main.c,v 1.84.4.1 1996/06/02 09:08:06 mrg Exp $ */
4 /* $OpenBSD: kern_xxx.c,v 1.9 2003/08/15 20:32:18 tedu Exp $ */
5 /* $NetBSD: kern_xxx.c,v 1.32 1996/04/22 01:38:41 christos Exp $ */
6
7 /*
8 * Copyright (c) 1990-2002, 2003, 2004, 2005, 2006, 2011, 2014
9 * Thorsten "mirabilos" Glaser <tg@mirbsd.org>
10 * Copyright (c) 1995 Christopher G. Demetriou. All rights reserved.
11 * Copyright (c) 1982, 1986, 1989, 1991, 1992, 1993
12 * The Regents of the University of California. All rights reserved.
13 * (c) UNIX System Laboratories, Inc.
14 * All or some portions of this file are derived from material licensed
15 * to the University of California by American Telephone and Telegraph
16 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
17 * the permission of UNIX System Laboratories, Inc.
18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
27 * 3. Neither the name of the University nor the names of its contributors
28 * may be used to endorse or promote products derived from this software
29 * without specific prior written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 * SUCH DAMAGE.
42 *
43 * @(#)init_main.c 8.9 (Berkeley) 1/21/94
44 */
45
46 #include <sys/param.h>
47 #include <sys/filedesc.h>
48 #include <sys/file.h>
49 #include <sys/errno.h>
50 #include <sys/exec.h>
51 #include <sys/kernel.h>
52 #include <sys/kthread.h>
53 #include <sys/mount.h>
54 #include <sys/proc.h>
55 #include <sys/resourcevar.h>
56 #include <sys/signalvar.h>
57 #include <sys/systm.h>
58 #include <sys/namei.h>
59 #include <sys/vnode.h>
60 #include <sys/tty.h>
61 #include <sys/conf.h>
62 #include <sys/buf.h>
63 #include <sys/device.h>
64 #include <sys/socketvar.h>
65 #include <sys/lockf.h>
66 #include <sys/protosw.h>
67 #include <sys/reboot.h>
68 #include <sys/user.h>
69 #ifdef SYSVSHM
70 #include <sys/shm.h>
71 #endif
72 #ifdef SYSVSEM
73 #include <sys/sem.h>
74 #endif
75 #ifdef SYSVMSG
76 #include <sys/msg.h>
77 #endif
78 #include <sys/domain.h>
79 #include <sys/mbuf.h>
80 #include <sys/pipe.h>
81
82 #include <sys/syscall.h>
83 #include <sys/syscallargs.h>
84
85 #include <dev/rndvar.h>
86
87 #include <ufs/ufs/quota.h>
88
89 #include <machine/cpu.h>
90
91 #include <uvm/uvm.h>
92
93 #include <net/if.h>
94 #include <net/raw_cb.h>
95
96 #if defined(CRYPTO)
97 #include <crypto/cryptodev.h>
98 #include <crypto/cryptosoft.h>
99 #endif
100 #include <crypto/randimpl.h>
101
102 #if defined(NFSSERVER) || defined(NFSCLIENT)
103 extern void nfs_init(void);
104 #endif
105
106 /* used by kernfs */
107 const char copyright[] =
108 "Copyright (c) 1982, 1986, 1989, 1991, 1993\n"
109 "\tThe Regents of the University of California. All rights reserved.\n"
110 "Copyright (c) 1995-2004 OpenBSD. All rights reserved. http://www.OpenBSD.org\n"
111 "Copyright (c) 2002-2014 The MirOS Project and contributors. http://mirbsd.de\n";
112
113 /* Components of the first process -- never freed. */
114 struct session session0;
115 struct pgrp pgrp0;
116 struct proc proc0;
117 struct pcred cred0;
118 struct plimit limit0;
119 struct vmspace vmspace0;
120 struct sigacts sigacts0;
121 #ifndef curproc
122 struct proc *curproc;
123 #endif
124 struct proc *initproc;
125
126 int cmask = CMASK;
127 extern struct user *proc0paddr;
128 extern volatile int ticks; /* XXX should move to sys/x*.h */
129
130 void (*md_diskconf)(void) = NULL;
131 struct vnode *rootvp, *swapdev_vp;
132 int boothowto;
133 struct timeval boottime;
134 struct timeval runtime;
135 __volatile int start_init_exec; /* semaphore for start_init() */
136
137 /* XXX return int so gcc -Werror won't complain */
138 int main(void *);
139 void check_console(struct proc *);
140 void start_init(void *);
141 void start_cleaner(void *);
142 void start_update(void *);
143 void start_reaper(void *);
144 void start_crypto(void *);
145 void init_exec(void);
146 void init_ssp(void);
147 void kqueue_init(void);
148
149 extern char sigcode[], esigcode[];
150 #ifdef SYSCALL_DEBUG
151 extern char *syscallnames[];
152 #endif
153
154 struct emul emul_native = {
155 "native",
156 NULL,
157 sendsig,
158 SYS_syscall,
159 SYS_MAXSYSCALL,
160 sysent,
161 #ifdef SYSCALL_DEBUG
162 syscallnames,
163 #else
164 NULL,
165 #endif
166 0,
167 copyargs,
168 setregs,
169 NULL,
170 sigcode,
171 esigcode,
172 EMUL_ENABLED | EMUL_NATIVE,
173 };
174
175
176 /*
177 * System startup; initialise the world, create process 0, mount root
178 * filesystem, and fork to create init and pagedaemon. Most of the
179 * hard work is done in the lower-level initialization routines including
180 * startup(), which does memory initialization and autoconfiguration.
181 */
182 int
main(void * framep)183 main(/* XXX should go away */ void *framep)
184 {
185 struct proc *p;
186 struct pdevinit *pdev;
187 quad_t lim;
188 int s, i;
189 extern struct pdevinit pdevinit[];
190 extern void scheduler_start(void);
191 extern void disk_init(void);
192 extern void endtsleep(void *);
193 extern void realitexpire(void *);
194
195 /*
196 * Initialise the current process pointer (curproc) before
197 * any possible traps/probes to simplify trap processing.
198 */
199 curproc = p = &proc0;
200
201 /*
202 * Initialise timeouts.
203 */
204 timeout_startup();
205
206 /*
207 * Attempt to find console and initialise
208 * in case of early panic or other messages.
209 */
210 config_init(); /* init autoconfiguration data structures */
211 consinit();
212 printf("%s\n", copyright);
213 rnd_lopool_addv(ticks);
214
215 uvm_init();
216 disk_init(); /* must come before autoconfiguration */
217 tty_init(); /* initialise ttys */
218 cpu_startup();
219
220 /*
221 * Initialise mbufs. Do this now because we might attempt to
222 * allocate mbufs or mbuf clusters during autoconfiguration.
223 */
224 mbinit();
225
226 /* Initalise sockets. */
227 soinit();
228
229 /* Initialise sysctls (must be done before any processes run) */
230 sysctl_init();
231
232 /*
233 * Initialise process and pgrp structures.
234 */
235 procinit();
236
237 /* Initialise file locking. */
238 lf_init();
239
240 /*
241 * Initialise filedescriptors.
242 */
243 filedesc_init();
244
245 /*
246 * Initialise pipes.
247 */
248 pipe_init();
249
250 /*
251 * Initialise kqueues.
252 */
253 kqueue_init();
254
255 /*
256 * Create process 0 (the swapper).
257 */
258 LIST_INSERT_HEAD(&allproc, p, p_list);
259 p->p_pgrp = &pgrp0;
260 LIST_INSERT_HEAD(PIDHASH(0), p, p_hash);
261 LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash);
262 LIST_INIT(&pgrp0.pg_members);
263 LIST_INSERT_HEAD(&pgrp0.pg_members, p, p_pglist);
264
265 pgrp0.pg_session = &session0;
266 session0.s_count = 1;
267 session0.s_leader = p;
268
269 p->p_flag = P_INMEM | P_SYSTEM | P_NOCLDWAIT;
270 p->p_stat = SRUN;
271 p->p_nice = NZERO;
272 p->p_emul = &emul_native;
273 bcopy("swapper", p->p_comm, sizeof ("swapper"));
274
275 /* Init timeouts. */
276 timeout_set(&p->p_sleep_to, endtsleep, p);
277 timeout_set(&p->p_realit_to, realitexpire, p);
278
279 /* Create credentials. */
280 cred0.p_refcnt = 1;
281 p->p_cred = &cred0;
282 p->p_ucred = crget();
283 p->p_ucred->cr_ngroups = 1; /* group 0 */
284
285 /* Initialise signal state for process 0. */
286 signal_init();
287 p->p_sigacts = &sigacts0;
288 siginit(p);
289
290 /* Create the file descriptor table. */
291 p->p_fd = fdinit(NULL);
292
293 /* Create the limits structures. */
294 p->p_limit = &limit0;
295 for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++)
296 limit0.pl_rlimit[i].rlim_cur =
297 limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY;
298 limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur = NOFILE;
299 limit0.pl_rlimit[RLIMIT_NOFILE].rlim_max = MIN(NOFILE_MAX,
300 (maxfiles - NOFILE > NOFILE) ? maxfiles - NOFILE : NOFILE);
301 limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur = MAXUPRC;
302 lim = ptoa(uvmexp.free);
303 limit0.pl_rlimit[RLIMIT_RSS].rlim_max = lim;
304 limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = lim;
305 limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = lim / 3;
306 limit0.p_refcnt = 1;
307
308 /* Allocate a prototype map so we have something to fork. */
309 uvmspace_init(&vmspace0, pmap_kernel(), round_page(VM_MIN_ADDRESS),
310 trunc_page(VM_MAX_ADDRESS), TRUE);
311 p->p_vmspace = &vmspace0;
312
313 p->p_addr = proc0paddr; /* XXX */
314
315 /*
316 * We continue to place resource usage info in the
317 * user struct so they're pageable.
318 */
319 p->p_stats = &p->p_addr->u_stats;
320
321 /*
322 * Charge root for one process.
323 */
324 (void)chgproccnt(0, 1);
325
326 /* Initialise run queues */
327 rqinit();
328
329 /* Configure the devices */
330 cpu_configure();
331
332 /* Configure virtual memory system, set vm rlimits. */
333 uvm_init_limits(p);
334
335 /* Initialise the file systems. */
336 #if defined(NFSSERVER) || defined(NFSCLIENT)
337 nfs_init(); /* initialise server/shared data */
338 #endif
339 vfsinit();
340
341 /* Start real time and statistics clocks. */
342 initclocks();
343
344 #ifdef SYSVSHM
345 /* Initialise System V style shared memory. */
346 shminit();
347 #endif
348
349 #ifdef SYSVSEM
350 /* Initialise System V style semaphores. */
351 seminit();
352 #endif
353
354 #ifdef SYSVMSG
355 /* Initialise System V style message queues. */
356 msginit();
357 #endif
358
359 #ifdef __sparc__
360 /* insert a little delay here to let things settle down */
361 delay(10000);
362 #endif
363 /* Attach pseudo-devices. */
364 randomattach();
365 for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++)
366 if (pdev->pdev_count > 0)
367 (*pdev->pdev_attach)(pdev->pdev_count);
368 rnd_lopool_addv(ticks);
369
370 #ifdef CRYPTO
371 swcr_init();
372 #endif /* CRYPTO */
373
374 /*
375 * Initialise protocols. Block reception of incoming packets
376 * until everything is ready.
377 */
378 s = splimp();
379 ifinit();
380 domaininit();
381 if_attachdomain();
382 splx(s);
383
384 /* initialise stack protector */
385 init_ssp();
386
387 /* init exec and emul */
388 init_exec();
389
390 /* Start the scheduler */
391 scheduler_start();
392
393 /*
394 * Create process 1 (init(8)). We do this now, as Unix has
395 * historically had init be process 1, and changing this would
396 * probably upset a lot of people.
397 *
398 * Note that process 1 won't immediately exec init(8), but will
399 * wait for us to inform it that the root file system has been
400 * mounted.
401 */
402 if (fork1(p, SIGCHLD, FORK_FORK, NULL, 0, start_init, NULL, NULL,
403 &initproc))
404 panic("fork init");
405
406 /*
407 * Create any kernel threads who's creation was deferred because
408 * initproc had not yet been created.
409 */
410 kthread_run_deferred_queue();
411
412 /*
413 * Now that device driver threads have been created, wait for
414 * them to finish any deferred autoconfiguration. Note we don't
415 * need to lock this semaphore, since we haven't booted any
416 * secondary processors, yet.
417 */
418 while (config_pending)
419 (void) tsleep((void *)&config_pending, PWAIT, "cfpend", 0);
420
421 dostartuphooks();
422
423 /* Configure root/swap devices */
424 if (md_diskconf)
425 (*md_diskconf)();
426
427 /* Mount the root file system. */
428 if (vfs_mountroot())
429 panic("cannot mount root");
430 CIRCLEQ_FIRST(&mountlist)->mnt_flag |= MNT_ROOTFS;
431
432 /* Get the vnode for '/'. Set p->p_fd->fd_cdir to reference it. */
433 if (VFS_ROOT(mountlist.cqh_first, &rootvnode))
434 panic("cannot find root vnode");
435 p->p_fd->fd_cdir = rootvnode;
436 VREF(p->p_fd->fd_cdir);
437 VOP_UNLOCK(rootvnode, 0, p);
438 p->p_fd->fd_rdir = NULL;
439
440 uvm_swap_init();
441
442 /*
443 * Now that root is mounted, we can fixup initproc's CWD
444 * info. All other processes are kthreads, which merely
445 * share proc0's CWD info.
446 */
447 initproc->p_fd->fd_cdir = rootvnode;
448 VREF(initproc->p_fd->fd_cdir);
449 initproc->p_fd->fd_rdir = NULL;
450
451 /*
452 * Now can look at time, having had a chance to verify the time
453 * from the file system. Reset p->p_rtime as it may have been
454 * munched in mi_switch() after the time got set.
455 */
456 runtime = mono_time = boottime = time;
457 LIST_FOREACH(p, &allproc, p_list) {
458 p->p_stats->p_start = boottime;
459 p->p_rtime.tv_sec = p->p_rtime.tv_usec = 0;
460 }
461
462 /* Create the pageout daemon kernel thread. */
463 if (kthread_create(uvm_pageout, NULL, NULL, "pagedaemon"))
464 panic("fork pagedaemon");
465
466 /* Create the reaper daemon kernel thread. */
467 if (kthread_create(start_reaper, NULL, NULL, "reaper"))
468 panic("fork reaper");
469
470 /* Create the cleaner daemon kernel thread. */
471 if (kthread_create(start_cleaner, NULL, NULL, "cleaner"))
472 panic("fork cleaner");
473
474 /* Create the update daemon kernel thread. */
475 if (kthread_create(start_update, NULL, NULL, "update"))
476 panic("fork update");
477
478 /* Create the aiodone daemon kernel thread. */
479 if (kthread_create(uvm_aiodone_daemon, NULL, NULL, "aiodoned"))
480 panic("fork aiodoned");
481
482 #ifdef CRYPTO
483 /* Create the crypto kernel thread. */
484 if (kthread_create(start_crypto, NULL, NULL, "crypto"))
485 panic("crypto thread");
486 #endif /* CRYPTO */
487
488 #if defined(I586_CPU) || defined(I686_CPU)
489 /* this adds the TSC too, if pentium_mhz!=0 */
490 rnd_lopool_add(&pentium_mhz, sizeof(pentium_mhz));
491 #endif
492
493 randompid = 1;
494
495 /*
496 * Okay, now we can let init(8) exec! It's off to userland!
497 */
498 start_init_exec = 1;
499 wakeup((void *)&start_init_exec);
500
501 /* The scheduler is an infinite loop. */
502 uvm_scheduler();
503 /* NOTREACHED */
504 }
505
506 /*
507 * List of paths to try when searching for "init".
508 */
509 static char *initpaths[] = {
510 "/sbin/init",
511 "/sbin/oinit",
512 "/sbin/init.bak",
513 NULL,
514 };
515
516 void
check_console(struct proc * p)517 check_console(struct proc *p)
518 {
519 struct nameidata nd;
520 int error;
521
522 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/console", p);
523 error = namei(&nd);
524 if (error) {
525 if (error == ENOENT)
526 printf("warning: /dev/console does not exist\n");
527 else
528 printf("warning: /dev/console error %d\n", error);
529 } else
530 vrele(nd.ni_vp);
531 }
532
533 /*
534 * Start the initial user process; try exec'ing each pathname in "initpaths".
535 * The program is invoked with one argument containing the boot flags.
536 */
537 void
start_init(void * arg)538 start_init(void *arg)
539 {
540 struct proc *p = arg;
541 vaddr_t addr;
542 struct sys_execve_args /* {
543 syscallarg(const char *) path;
544 syscallarg(char *const *) argp;
545 syscallarg(char *const *) envp;
546 } */ args;
547 int options, error;
548 long i;
549 register_t retval[2];
550 char flags[4], *flagsp;
551 char **pathp, *path, *ucp, **uap, *arg0, *arg1 = NULL;
552
553 /*
554 * Now in process 1.
555 */
556
557 /*
558 * Wait for main() to tell us that it's safe to exec.
559 */
560 while (start_init_exec == 0)
561 (void) tsleep((void *)&start_init_exec, PWAIT, "initexec", 0);
562
563 check_console(p);
564
565 /*
566 * Need just enough stack to hold the faked-up "execve()" arguments.
567 */
568 #ifdef MACHINE_STACK_GROWS_UP
569 addr = USRSTACK;
570 #else
571 addr = USRSTACK - PAGE_SIZE;
572 #endif
573 if (uvm_map(&p->p_vmspace->vm_map, &addr, PAGE_SIZE,
574 NULL, UVM_UNKNOWN_OFFSET, 0,
575 UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_ALL, UVM_INH_COPY,
576 UVM_ADV_NORMAL, UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW)))
577 panic("init: couldn't allocate argument space");
578 p->p_vmspace->vm_maxsaddr = (caddr_t)addr;
579
580 for (pathp = &initpaths[0]; (path = *pathp) != NULL; pathp++) {
581 #ifdef MACHINE_STACK_GROWS_UP
582 ucp = (char *)addr;
583 #else
584 ucp = (char *)(addr + PAGE_SIZE);
585 #endif
586 /*
587 * Construct the boot flag argument.
588 */
589 flagsp = flags;
590 *flagsp++ = '-';
591 options = 0;
592
593 if (boothowto & RB_SINGLE) {
594 *flagsp++ = 's';
595 options = 1;
596 }
597 #ifdef notyet
598 if (boothowto & RB_FASTBOOT) {
599 *flagsp++ = 'f';
600 options = 1;
601 }
602 #endif
603
604 /*
605 * Move out the flags (arg 1), if necessary.
606 */
607 if (options != 0) {
608 *flagsp++ = '\0';
609 i = flagsp - flags;
610 #ifdef DEBUG
611 printf("init: copying out flags `%s' %d\n", flags, i);
612 #endif
613 #ifdef MACHINE_STACK_GROWS_UP
614 arg1 = ucp;
615 (void)copyout((caddr_t)flags, (caddr_t)ucp, i);
616 ucp += i;
617 #else
618 (void)copyout((caddr_t)flags, (caddr_t)(ucp -= i), i);
619 arg1 = ucp;
620 #endif
621 }
622
623 /*
624 * Move out the file name (also arg 0).
625 */
626 i = strlen(path) + 1;
627 #ifdef DEBUG
628 printf("init: copying out path `%s' %d\n", path, i);
629 #endif
630 #ifdef MACHINE_STACK_GROWS_UP
631 arg0 = ucp;
632 (void)copyout((caddr_t)path, (caddr_t)ucp, i);
633 ucp += i;
634 ucp = (caddr_t)ALIGN((u_long)ucp);
635 uap = (char **)ucp + 3;
636 #else
637 (void)copyout((caddr_t)path, (caddr_t)(ucp -= i), i);
638 arg0 = ucp;
639 uap = (char **)((u_long)ucp & ~ALIGNBYTES);
640 #endif
641
642 /*
643 * Move out the arg pointers.
644 */
645 i = 0;
646 copyout(&i, (caddr_t)--uap, sizeof(register_t)); /* terminator */
647 if (options != 0)
648 copyout(&arg1, (caddr_t)--uap, sizeof(register_t));
649 copyout(&arg0, (caddr_t)--uap, sizeof(register_t));
650
651 /*
652 * Point at the arguments.
653 */
654 SCARG(&args, path) = arg0;
655 SCARG(&args, argp) = uap;
656 SCARG(&args, envp) = NULL;
657
658 /*
659 * Now try to exec the program. If can't for any reason
660 * other than it doesn't exist, complain.
661 */
662 if ((error = sys_execve(p, &args, retval)) == 0)
663 return;
664 if (error != ENOENT)
665 printf("exec %s: error %d\n", path, error);
666 }
667 printf("init: not found\n");
668 panic("no init");
669 }
670
671 void
start_update(void * arg)672 start_update(void *arg)
673 {
674 sched_sync(curproc);
675 /* NOTREACHED */
676 }
677
678 void
start_cleaner(void * arg)679 start_cleaner(void *arg)
680 {
681 buf_daemon(curproc);
682 /* NOTREACHED */
683 }
684
685 void
start_reaper(void * arg)686 start_reaper(void *arg)
687 {
688 reaper();
689 /* NOTREACHED */
690 }
691
692 #ifdef CRYPTO
693 void
start_crypto(void * arg)694 start_crypto(void *arg)
695 {
696 crypto_thread();
697 /* NOTREACHED */
698 }
699 #endif /* CRYPTO */
700
701 /* ARGSUSED */
702 int
sys_reboot(struct proc * p,void * v,register_t * retval)703 sys_reboot(struct proc *p, void *v, register_t *retval)
704 {
705 struct sys_reboot_args /* {
706 syscallarg(int) opt;
707 } */ *uap = v;
708 int error;
709
710 if ((error = suser(p, 0)) != 0)
711 return (error);
712 rnd_flush();
713 boot(SCARG(uap, opt));
714 return (0);
715 }
716
717 #ifdef SYSCALL_DEBUG
718 #define SCDEBUG_CALLS 0x0001 /* show calls */
719 #define SCDEBUG_RETURNS 0x0002 /* show returns */
720 #define SCDEBUG_ALL 0x0004 /* even syscalls that are implemented */
721 #define SCDEBUG_SHOWARGS 0x0008 /* show arguments to calls */
722
723 int scdebug = SCDEBUG_CALLS|SCDEBUG_RETURNS|SCDEBUG_SHOWARGS;
724
725 void
scdebug_call(struct proc * p,register_t code,register_t args[])726 scdebug_call(struct proc *p, register_t code, register_t args[])
727 {
728 struct sysent *sy;
729 struct emul *em;
730 int i;
731
732 if (!(scdebug & SCDEBUG_CALLS))
733 return;
734
735 em = p->p_emul;
736 sy = &em->e_sysent[code];
737 if (!(scdebug & SCDEBUG_ALL || code < 0 || code >= em->e_nsysent ||
738 sy->sy_call == sys_nosys))
739 return;
740
741 printf("proc %d (%s): %s num ", p->p_pid, p->p_comm, em->e_name);
742 if (code < 0 || code >= em->e_nsysent)
743 printf("OUT OF RANGE (%d)", code);
744 else {
745 printf("%d call: %s", code, em->e_syscallnames[code]);
746 if (scdebug & SCDEBUG_SHOWARGS) {
747 printf("(");
748 for (i = 0; i < sy->sy_argsize / sizeof(register_t);
749 i++)
750 printf("%s0x%lx", i == 0 ? "" : ", ",
751 (long)args[i]);
752 printf(")");
753 }
754 }
755 printf("\n");
756 }
757
758 void
scdebug_ret(p,code,error,retval)759 scdebug_ret(p, code, error, retval)
760 struct proc *p;
761 register_t code;
762 int error;
763 register_t retval[];
764 {
765 struct sysent *sy;
766 struct emul *em;
767
768 if (!(scdebug & SCDEBUG_RETURNS))
769 return;
770
771 em = p->p_emul;
772 sy = &em->e_sysent[code];
773 if (!(scdebug & SCDEBUG_ALL || code < 0 || code >= em->e_nsysent ||
774 sy->sy_call == sys_nosys))
775 return;
776
777 printf("proc %d (%s): %s num ", p->p_pid, p->p_comm, em->e_name);
778 if (code < 0 || code >= em->e_nsysent)
779 printf("OUT OF RANGE (%d)", code);
780 else
781 printf("%d ret: err = %d, rv = 0x%lx,0x%lx", code,
782 error, (long)retval[0], (long)retval[1]);
783 printf("\n");
784 }
785 #endif /* SYSCALL_DEBUG */
786