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