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