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