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