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