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