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