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