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