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