init_main.c revision 1.317 1 /* $NetBSD: init_main.c,v 1.317 2007/09/25 22:33:59 ad Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1989, 1991, 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)init_main.c 8.16 (Berkeley) 5/14/95
37 */
38
39 /*
40 * Copyright (c) 1995 Christopher G. Demetriou. All rights reserved.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the University of
53 * California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 *
70 * @(#)init_main.c 8.16 (Berkeley) 5/14/95
71 */
72
73 #include <sys/cdefs.h>
74 __KERNEL_RCSID(0, "$NetBSD: init_main.c,v 1.317 2007/09/25 22:33:59 ad Exp $");
75
76 #include "opt_ipsec.h"
77 #include "opt_multiprocessor.h"
78 #include "opt_ntp.h"
79 #include "opt_pipe.h"
80 #include "opt_posix.h"
81 #include "opt_syscall_debug.h"
82 #include "opt_sysv.h"
83 #include "opt_systrace.h"
84 #include "opt_fileassoc.h"
85 #include "opt_ktrace.h"
86 #include "opt_pax.h"
87
88 #include "rnd.h"
89 #include "sysmon_envsys.h"
90 #include "sysmon_power.h"
91 #include "sysmon_taskq.h"
92 #include "sysmon_wdog.h"
93 #include "veriexec.h"
94
95 #include <sys/param.h>
96 #include <sys/acct.h>
97 #include <sys/filedesc.h>
98 #include <sys/file.h>
99 #include <sys/errno.h>
100 #include <sys/callout.h>
101 #include <sys/cpu.h>
102 #include <sys/kernel.h>
103 #include <sys/kmem.h>
104 #include <sys/mount.h>
105 #include <sys/proc.h>
106 #include <sys/kthread.h>
107 #include <sys/resourcevar.h>
108 #include <sys/signalvar.h>
109 #include <sys/systm.h>
110 #include <sys/vnode.h>
111 #include <sys/fstrans.h>
112 #include <sys/tty.h>
113 #include <sys/conf.h>
114 #include <sys/disklabel.h>
115 #include <sys/buf.h>
116 #include <sys/device.h>
117 #include <sys/exec.h>
118 #include <sys/socketvar.h>
119 #include <sys/protosw.h>
120 #include <sys/reboot.h>
121 #include <sys/user.h>
122 #include <sys/sysctl.h>
123 #include <sys/event.h>
124 #include <sys/mbuf.h>
125 #include <sys/sched.h>
126 #include <sys/sleepq.h>
127 #include <sys/iostat.h>
128 #include <sys/vmem.h>
129 #include <sys/uuid.h>
130 #include <sys/extent.h>
131 #include <sys/disk.h>
132 #include <sys/mqueue.h>
133 #ifdef FAST_IPSEC
134 #include <netipsec/ipsec.h>
135 #endif
136 #ifdef SYSVSHM
137 #include <sys/shm.h>
138 #endif
139 #ifdef SYSVSEM
140 #include <sys/sem.h>
141 #endif
142 #ifdef SYSVMSG
143 #include <sys/msg.h>
144 #endif
145 #ifdef SYSTRACE
146 #include <sys/systrace.h>
147 #endif
148 #ifdef P1003_1B_SEMAPHORE
149 #include <sys/ksem.h>
150 #endif
151 #include <sys/domain.h>
152 #include <sys/namei.h>
153 #if NRND > 0
154 #include <sys/rnd.h>
155 #endif
156 #include <sys/pipe.h>
157 #ifdef LKM
158 #include <sys/lkm.h>
159 #endif
160 #if NVERIEXEC > 0
161 #include <sys/verified_exec.h>
162 #endif /* NVERIEXEC > 0 */
163 #ifdef KTRACE
164 #include <sys/ktrace.h>
165 #endif
166 #include <sys/debug.h>
167 #include <sys/kauth.h>
168 #include <net80211/ieee80211_netbsd.h>
169
170 #include <sys/syscall.h>
171 #include <sys/syscallargs.h>
172
173 #if defined(PAX_MPROTECT) || defined(PAX_SEGVGUARD)
174 #include <sys/pax.h>
175 #endif /* PAX_MPROTECT || PAX_SEGVGUARD */
176 #include <ufs/ufs/quota.h>
177
178 #ifdef DDB
179 #include <machine/db_machdep.h>
180 #include <ddb/db_command.h>
181 #endif
182
183 #include <miscfs/genfs/genfs.h>
184 #include <miscfs/syncfs/syncfs.h>
185
186 #include <machine/cpu.h>
187
188 #include <uvm/uvm.h>
189
190 #if NSYSMON_TASKQ > 0
191 #include <dev/sysmon/sysmon_taskq.h>
192 #endif
193
194 #include <dev/cons.h>
195
196 #if NSYSMON_ENVSYS > 0 || NSYSMON_POWER > 0 || NSYSMON_WDOG > 0
197 #include <dev/sysmon/sysmonvar.h>
198 #endif
199
200 #include <net/if.h>
201 #include <net/raw_cb.h>
202
203 #include <secmodel/secmodel.h>
204
205 extern struct proc proc0;
206 extern struct lwp lwp0;
207 extern struct cwdinfo cwdi0;
208 extern time_t rootfstime;
209
210 #ifndef curlwp
211 struct lwp *curlwp = &lwp0;
212 #endif
213 struct proc *initproc;
214
215 struct vnode *rootvp, *swapdev_vp;
216 int boothowto;
217 int cold = 1; /* still working on startup */
218 struct timeval boottime; /* time at system startup - will only follow settime deltas */
219 int ncpu = 0; /* number of CPUs configured, assume 1 */
220
221 volatile int start_init_exec; /* semaphore for start_init() */
222
223 static void check_console(struct lwp *l);
224 static void start_init(void *);
225 void main(void);
226
227 #if defined(__SSP__) || defined(__SSP_ALL__)
228 long __stack_chk_guard[8] = {0, 0, 0, 0, 0, 0, 0, 0};
229 void __stack_chk_fail(void);
230
231 void
232 __stack_chk_fail(void)
233 {
234 panic("stack overflow detected; terminated");
235 }
236 #endif
237
238 void __secmodel_none(void);
239 __weak_alias(secmodel_start,__secmodel_none);
240 void
241 __secmodel_none(void)
242 {
243 return;
244 }
245
246 /*
247 * System startup; initialize the world, create process 0, mount root
248 * filesystem, and fork to create init and pagedaemon. Most of the
249 * hard work is done in the lower-level initialization routines including
250 * startup(), which does memory initialization and autoconfiguration.
251 */
252 void
253 main(void)
254 {
255 #ifdef __HAVE_TIMECOUNTER
256 struct timeval time;
257 #endif
258 struct lwp *l;
259 struct proc *p;
260 struct pdevinit *pdev;
261 int s, error;
262 extern struct pdevinit pdevinit[];
263 #ifdef NVNODE_IMPLICIT
264 int usevnodes;
265 #endif
266 CPU_INFO_ITERATOR cii;
267 struct cpu_info *ci;
268
269 l = &lwp0;
270 l->l_cpu = curcpu();
271 l->l_proc = &proc0;
272 l->l_lid = 1;
273
274 /*
275 * XXX This is a temporary check to be removed before
276 * NetBSD 5.0 is released.
277 */
278 #if !defined(__i386__ ) && !defined(__x86_64__)
279 if (curlwp != l) {
280 printf("NOTICE: curlwp should be set before main()\n");
281 DELAY(250000);
282 curlwp = l;
283 }
284 #endif
285
286 /*
287 * Attempt to find console and initialize
288 * in case of early panic or other messages.
289 */
290 consinit();
291
292 KERNEL_LOCK_INIT();
293
294 uvm_init();
295
296 #ifdef DEBUG
297 debug_init();
298 #endif
299
300 kmem_init();
301
302 /* Initialize the extent manager. */
303 extent_init();
304
305 /* Do machine-dependent initialization. */
306 cpu_startup();
307
308 /* Initialize callouts, part 1. */
309 callout_startup();
310
311 /*
312 * Initialize the kernel authorization subsystem and start the
313 * default security model, if any. We need to do this early
314 * enough so that subsystems relying on any of the aforementioned
315 * can work properly. Since the security model may dictate the
316 * credential inheritance policy, it is needed at least before
317 * any process is created, specifically proc0.
318 */
319 kauth_init();
320 secmodel_start();
321
322 /* Initialize the buffer cache */
323 bufinit();
324
325 /*
326 * Initialize mbuf's. Do this now because we might attempt to
327 * allocate mbufs or mbuf clusters during autoconfiguration.
328 */
329 mbinit();
330
331 /* Initialize sockets. */
332 soinit();
333
334 /*
335 * The following things must be done before autoconfiguration.
336 */
337 evcnt_init(); /* initialize event counters */
338 #if NRND > 0
339 rnd_init(); /* initialize RNG */
340 #endif
341
342 /* Initialize process and pgrp structures. */
343 procinit();
344 lwpinit();
345
346 /* Initialize signal-related data structures. */
347 signal_init();
348
349 /* Create process 0 (the swapper). */
350 proc0_init();
351
352 /* Initialize the UID hash table. */
353 uid_init();
354
355 /* Charge root for one process. */
356 (void)chgproccnt(0, 1);
357
358 /* Initialize the run queues, turnstiles and sleep queues. */
359 mutex_init(&cpu_lock, MUTEX_DEFAULT, IPL_NONE);
360 sched_rqinit();
361 turnstile_init();
362 sleeptab_init(&sleeptab);
363
364 /* MI initialization of the boot cpu */
365 error = mi_cpu_attach(curcpu());
366 KASSERT(error == 0);
367
368 /* Initialize the sysctl subsystem. */
369 sysctl_init();
370
371 /* Initialize I/O statistics. */
372 iostat_init();
373
374 /* Initialize the file systems. */
375 #ifdef NVNODE_IMPLICIT
376 /*
377 * If maximum number of vnodes in namei vnode cache is not explicitly
378 * defined in kernel config, adjust the number such as we use roughly
379 * 1.0% of memory for vnode cache (but not less than NVNODE vnodes).
380 */
381 usevnodes = (ptoa((unsigned)physmem) / 100) / sizeof(struct vnode);
382 if (usevnodes > desiredvnodes)
383 desiredvnodes = usevnodes;
384 #endif
385 vfsinit();
386
387 /* Initialize fstrans. */
388 fstrans_init();
389
390 /* Initialize the select()/poll() system calls. */
391 selsysinit();
392
393 /* Initialize asynchronous I/O. */
394 aio_sysinit();
395
396 /* Initialize message queues. */
397 mqueue_sysinit();
398
399 /* Initialize the system monitor subsystems. */
400 #if NSYSMON_TASKQ > 0
401 sysmon_task_queue_preinit();
402 #endif
403
404 #if NSYSMON_ENVSYS > 0
405 sysmon_envsys_init();
406 #endif
407
408 #if NSYSMON_POWER > 0
409 sysmon_power_init();
410 #endif
411
412 #if NSYSMON_WDOG > 0
413 sysmon_wdog_init();
414 #endif
415
416 #ifdef __HAVE_TIMECOUNTER
417 inittimecounter();
418 ntp_init();
419 #endif /* __HAVE_TIMECOUNTER */
420
421 #ifdef DDB
422 db_init_commands();
423 #endif
424
425 /* Initialize the device switch tables. */
426 devsw_init();
427
428 /* Initialize the disk wedge subsystem. */
429 dkwedge_init();
430
431 /* Configure the system hardware. This will enable interrupts. */
432 configure();
433
434 #if defined(__SSP__) || defined(__SSP_ALL__)
435 {
436 #ifdef DIAGNOSTIC
437 printf("Initializing SSP:");
438 #endif
439 /*
440 * We initialize ssp here carefully:
441 * 1. after we got some entropy
442 * 2. without calling a function
443 */
444 size_t i;
445 long guard[__arraycount(__stack_chk_guard)];
446
447 arc4randbytes(guard, sizeof(guard));
448 for (i = 0; i < __arraycount(guard); i++)
449 __stack_chk_guard[i] = guard[i];
450 #ifdef DIAGNOSTIC
451 for (i = 0; i < __arraycount(guard); i++)
452 printf("%lx ", guard[i]);
453 printf("\n");
454 #endif
455 }
456 #endif
457 ubc_init(); /* must be after autoconfig */
458
459 /* Lock the kernel on behalf of proc0. */
460 KERNEL_LOCK(1, l);
461
462 #ifdef SYSTRACE
463 systrace_init();
464 #endif
465
466 #ifdef SYSVSHM
467 /* Initialize System V style shared memory. */
468 shminit();
469 #endif
470
471 #ifdef SYSVSEM
472 /* Initialize System V style semaphores. */
473 seminit();
474 #endif
475
476 #ifdef SYSVMSG
477 /* Initialize System V style message queues. */
478 msginit();
479 #endif
480
481 #ifdef P1003_1B_SEMAPHORE
482 /* Initialize posix semaphores */
483 ksem_init();
484 #endif
485
486 #if NVERIEXEC > 0
487 /*
488 * Initialise the Veriexec subsystem.
489 */
490 veriexec_init();
491 #endif /* NVERIEXEC > 0 */
492
493 #if defined(PAX_MPROTECT) || defined(PAX_SEGVGUARD)
494 pax_init();
495 #endif /* PAX_MPROTECT || PAX_SEGVGUARD */
496
497 /* Attach pseudo-devices. */
498 for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++)
499 (*pdev->pdev_attach)(pdev->pdev_count);
500
501 #ifdef FAST_IPSEC
502 /* Attach network crypto subsystem */
503 ipsec_attach();
504 #endif
505
506 /*
507 * Initialize protocols. Block reception of incoming packets
508 * until everything is ready.
509 */
510 s = splnet();
511 ifinit();
512 domaininit();
513 if_attachdomain();
514 splx(s);
515
516 #ifdef GPROF
517 /* Initialize kernel profiling. */
518 kmstartup();
519 #endif
520
521 /* Initialize system accouting. */
522 acct_init();
523
524 #ifndef PIPE_SOCKETPAIR
525 /* Initialize pipes. */
526 pipe_init();
527 #endif
528
529 /* Setup the scheduler */
530 sched_init();
531
532 #ifdef KTRACE
533 /* Initialize ktrace. */
534 ktrinit();
535 #endif
536
537 /* Initialize the UUID system calls. */
538 uuid_init();
539
540 /*
541 * Create process 1 (init(8)). We do this now, as Unix has
542 * historically had init be process 1, and changing this would
543 * probably upset a lot of people.
544 *
545 * Note that process 1 won't immediately exec init(8), but will
546 * wait for us to inform it that the root file system has been
547 * mounted.
548 */
549 if (fork1(l, 0, SIGCHLD, NULL, 0, start_init, NULL, NULL, &initproc))
550 panic("fork init");
551
552 /*
553 * Now that device driver threads have been created, wait for
554 * them to finish any deferred autoconfiguration. Note we don't
555 * need to lock this semaphore, since we haven't booted any
556 * secondary processors, yet.
557 */
558 while (config_pending)
559 (void) tsleep(&config_pending, PWAIT, "cfpend", 0);
560
561 /*
562 * Finalize configuration now that all real devices have been
563 * found. This needs to be done before the root device is
564 * selected, since finalization may create the root device.
565 */
566 config_finalize();
567
568 /*
569 * Now that autoconfiguration has completed, we can determine
570 * the root and dump devices.
571 */
572 cpu_rootconf();
573 cpu_dumpconf();
574
575 /* Mount the root file system. */
576 do {
577 domountroothook();
578 if ((error = vfs_mountroot())) {
579 printf("cannot mount root, error = %d\n", error);
580 boothowto |= RB_ASKNAME;
581 setroot(root_device,
582 (rootdev != NODEV) ? DISKPART(rootdev) : 0);
583 }
584 } while (error != 0);
585 mountroothook_destroy();
586
587 /*
588 * Initialise the time-of-day clock, passing the time recorded
589 * in the root filesystem (if any) for use by systems that
590 * don't have a non-volatile time-of-day device.
591 */
592 inittodr(rootfstime);
593
594 CIRCLEQ_FIRST(&mountlist)->mnt_flag |= MNT_ROOTFS;
595 CIRCLEQ_FIRST(&mountlist)->mnt_op->vfs_refcount++;
596
597 /*
598 * Get the vnode for '/'. Set filedesc0.fd_fd.fd_cdir to
599 * reference it.
600 */
601 error = VFS_ROOT(CIRCLEQ_FIRST(&mountlist), &rootvnode);
602 if (error)
603 panic("cannot find root vnode, error=%d", error);
604 cwdi0.cwdi_cdir = rootvnode;
605 VREF(cwdi0.cwdi_cdir);
606 VOP_UNLOCK(rootvnode, 0);
607 cwdi0.cwdi_rdir = NULL;
608
609 /*
610 * Now that root is mounted, we can fixup initproc's CWD
611 * info. All other processes are kthreads, which merely
612 * share proc0's CWD info.
613 */
614 initproc->p_cwdi->cwdi_cdir = rootvnode;
615 VREF(initproc->p_cwdi->cwdi_cdir);
616 initproc->p_cwdi->cwdi_rdir = NULL;
617
618 /*
619 * Now can look at time, having had a chance to verify the time
620 * from the file system. Reset l->l_rtime as it may have been
621 * munched in mi_switch() after the time got set.
622 */
623 #ifdef __HAVE_TIMECOUNTER
624 getmicrotime(&time);
625 #else
626 mono_time = time;
627 #endif
628 boottime = time;
629 mutex_enter(&proclist_lock);
630 LIST_FOREACH(p, &allproc, p_list) {
631 KASSERT((p->p_flag & PK_MARKER) == 0);
632 mutex_enter(&p->p_smutex);
633 p->p_stats->p_start = time;
634 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
635 lwp_lock(l);
636 l->l_cpu->ci_schedstate.spc_runtime = time;
637 l->l_rtime.tv_sec = l->l_rtime.tv_usec = 0;
638 lwp_unlock(l);
639 }
640 mutex_exit(&p->p_smutex);
641 }
642 mutex_exit(&proclist_lock);
643
644 for (CPU_INFO_FOREACH(cii, ci)) {
645 ci->ci_schedstate.spc_lastmod = time_second;
646 }
647
648 /* Create the pageout daemon kernel thread. */
649 uvm_swap_init();
650 if (kthread_create(PVM, 0, NULL, uvm_pageout,
651 NULL, NULL, "pgdaemon"))
652 panic("fork pagedaemon");
653
654 /* Create the filesystem syncer kernel thread. */
655 if (kthread_create(PINOD, 0, NULL, sched_sync, NULL, NULL, "ioflush"))
656 panic("fork syncer");
657
658 /* Create the aiodone daemon kernel thread. */
659 if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
660 uvm_aiodone_worker, NULL, PVM, IPL_BIO, 0))
661 panic("fork aiodoned");
662
663 vmem_rehash_start();
664
665 #if defined(MULTIPROCESSOR)
666 /* Boot the secondary processors. */
667 cpu_boot_secondary_processors();
668 #endif
669
670 /* Initialize exec structures */
671 exec_init(1);
672
673 /*
674 * Okay, now we can let init(8) exec! It's off to userland!
675 */
676 start_init_exec = 1;
677 wakeup(&start_init_exec);
678
679 /* The scheduler is an infinite loop. */
680 uvm_scheduler();
681 /* NOTREACHED */
682 }
683
684 static void
685 check_console(struct lwp *l)
686 {
687 struct nameidata nd;
688 int error;
689
690 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/console", l);
691 error = namei(&nd);
692 if (error == 0)
693 vrele(nd.ni_vp);
694 else if (error == ENOENT)
695 printf("warning: no /dev/console\n");
696 else
697 printf("warning: lookup /dev/console: error %d\n", error);
698 }
699
700 /*
701 * List of paths to try when searching for "init".
702 */
703 static const char *initpaths[] = {
704 "/sbin/init",
705 "/sbin/oinit",
706 "/sbin/init.bak",
707 NULL,
708 };
709
710 /*
711 * Start the initial user process; try exec'ing each pathname in "initpaths".
712 * The program is invoked with one argument containing the boot flags.
713 */
714 static void
715 start_init(void *arg)
716 {
717 struct lwp *l = arg;
718 struct proc *p = l->l_proc;
719 vaddr_t addr;
720 struct sys_execve_args /* {
721 syscallarg(const char *) path;
722 syscallarg(char * const *) argp;
723 syscallarg(char * const *) envp;
724 } */ args;
725 int options, i, error;
726 register_t retval[2];
727 char flags[4], *flagsp;
728 const char *path, *slash;
729 char *ucp, **uap, *arg0, *arg1 = NULL;
730 char ipath[129];
731 int ipx, len;
732
733 /*
734 * Now in process 1.
735 */
736 strncpy(p->p_comm, "init", MAXCOMLEN);
737
738 /*
739 * Wait for main() to tell us that it's safe to exec.
740 */
741 while (start_init_exec == 0)
742 (void) tsleep(&start_init_exec, PWAIT, "initexec", 0);
743
744 /*
745 * This is not the right way to do this. We really should
746 * hand-craft a descriptor onto /dev/console to hand to init,
747 * but that's a _lot_ more work, and the benefit from this easy
748 * hack makes up for the "good is the enemy of the best" effect.
749 */
750 check_console(l);
751
752 /*
753 * Need just enough stack to hold the faked-up "execve()" arguments.
754 */
755 addr = (vaddr_t)STACK_ALLOC(USRSTACK, PAGE_SIZE);
756 if (uvm_map(&p->p_vmspace->vm_map, &addr, PAGE_SIZE,
757 NULL, UVM_UNKNOWN_OFFSET, 0,
758 UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY,
759 UVM_ADV_NORMAL,
760 UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW)) != 0)
761 panic("init: couldn't allocate argument space");
762 p->p_vmspace->vm_maxsaddr = (void *)STACK_MAX(addr, PAGE_SIZE);
763
764 ipx = 0;
765 while (1) {
766 if (boothowto & RB_ASKNAME) {
767 printf("init path");
768 if (initpaths[ipx])
769 printf(" (default %s)", initpaths[ipx]);
770 printf(": ");
771 len = cngetsn(ipath, sizeof(ipath)-1);
772 if (len == 0) {
773 if (initpaths[ipx])
774 path = initpaths[ipx++];
775 else
776 continue;
777 } else {
778 ipath[len] = '\0';
779 path = ipath;
780 }
781 } else {
782 if ((path = initpaths[ipx++]) == NULL)
783 break;
784 }
785
786 ucp = (char *)USRSTACK;
787
788 /*
789 * Construct the boot flag argument.
790 */
791 flagsp = flags;
792 *flagsp++ = '-';
793 options = 0;
794
795 if (boothowto & RB_SINGLE) {
796 *flagsp++ = 's';
797 options = 1;
798 }
799 #ifdef notyet
800 if (boothowto & RB_FASTBOOT) {
801 *flagsp++ = 'f';
802 options = 1;
803 }
804 #endif
805
806 /*
807 * Move out the flags (arg 1), if necessary.
808 */
809 if (options != 0) {
810 *flagsp++ = '\0';
811 i = flagsp - flags;
812 #ifdef DEBUG
813 printf("init: copying out flags `%s' %d\n", flags, i);
814 #endif
815 arg1 = STACK_ALLOC(ucp, i);
816 ucp = STACK_MAX(arg1, i);
817 (void)copyout((void *)flags, arg1, i);
818 }
819
820 /*
821 * Move out the file name (also arg 0).
822 */
823 i = strlen(path) + 1;
824 #ifdef DEBUG
825 printf("init: copying out path `%s' %d\n", path, i);
826 #else
827 if (boothowto & RB_ASKNAME || path != initpaths[0])
828 printf("init: trying %s\n", path);
829 #endif
830 arg0 = STACK_ALLOC(ucp, i);
831 ucp = STACK_MAX(arg0, i);
832 (void)copyout(path, arg0, i);
833
834 /*
835 * Move out the arg pointers.
836 */
837 ucp = (void *)STACK_ALIGN(ucp, ALIGNBYTES);
838 uap = (char **)STACK_ALLOC(ucp, sizeof(char *) * 3);
839 SCARG(&args, path) = arg0;
840 SCARG(&args, argp) = uap;
841 SCARG(&args, envp) = NULL;
842 slash = strrchr(path, '/');
843 if (slash)
844 (void)suword((void *)uap++,
845 (long)arg0 + (slash + 1 - path));
846 else
847 (void)suword((void *)uap++, (long)arg0);
848 if (options != 0)
849 (void)suword((void *)uap++, (long)arg1);
850 (void)suword((void *)uap++, 0); /* terminator */
851
852 /*
853 * Now try to exec the program. If can't for any reason
854 * other than it doesn't exist, complain.
855 */
856 error = sys_execve(l, &args, retval);
857 if (error == 0 || error == EJUSTRETURN) {
858 KERNEL_UNLOCK_LAST(l);
859 return;
860 }
861 printf("exec %s: error %d\n", path, error);
862 }
863 printf("init: not found\n");
864 panic("no init");
865 }
866