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