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