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