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