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