Home | History | Annotate | Line # | Download | only in kern
init_main.c revision 1.222
      1 /*	$NetBSD: init_main.c,v 1.222 2003/07/30 18:45:32 jonathan Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995 Christopher G. Demetriou.  All rights reserved.
      5  * Copyright (c) 1982, 1986, 1989, 1991, 1992, 1993
      6  *	The Regents of the University of California.  All rights reserved.
      7  * (c) UNIX System Laboratories, Inc.
      8  * All or some portions of this file are derived from material licensed
      9  * to the University of California by American Telephone and Telegraph
     10  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     11  * the permission of UNIX System Laboratories, Inc.
     12  *
     13  * Redistribution and use in source and binary forms, with or without
     14  * modification, are permitted provided that the following conditions
     15  * are met:
     16  * 1. Redistributions of source code must retain the above copyright
     17  *    notice, this list of conditions and the following disclaimer.
     18  * 2. Redistributions in binary form must reproduce the above copyright
     19  *    notice, this list of conditions and the following disclaimer in the
     20  *    documentation and/or other materials provided with the distribution.
     21  * 3. All advertising materials mentioning features or use of this software
     22  *    must display the following acknowledgement:
     23  *	This product includes software developed by the University of
     24  *	California, Berkeley and its contributors.
     25  * 4. Neither the name of the University nor the names of its contributors
     26  *    may be used to endorse or promote products derived from this software
     27  *    without specific prior written permission.
     28  *
     29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     39  * SUCH DAMAGE.
     40  *
     41  *	@(#)init_main.c	8.16 (Berkeley) 5/14/95
     42  */
     43 
     44 #include <sys/cdefs.h>
     45 __KERNEL_RCSID(0, "$NetBSD: init_main.c,v 1.222 2003/07/30 18:45:32 jonathan Exp $");
     46 
     47 #include "fs_nfs.h"
     48 #include "opt_nfsserver.h"
     49 #include "opt_sysv.h"
     50 #include "opt_maxuprc.h"
     51 #include "opt_multiprocessor.h"
     52 #include "opt_pipe.h"
     53 #include "opt_syscall_debug.h"
     54 #include "opt_systrace.h"
     55 #include "opt_posix.h"
     56 
     57 #include "opencrypto.h"
     58 #include "rnd.h"
     59 
     60 #include <sys/param.h>
     61 #include <sys/acct.h>
     62 #include <sys/filedesc.h>
     63 #include <sys/file.h>
     64 #include <sys/errno.h>
     65 #include <sys/callout.h>
     66 #include <sys/kernel.h>
     67 #include <sys/mount.h>
     68 #include <sys/proc.h>
     69 #include <sys/kthread.h>
     70 #include <sys/resourcevar.h>
     71 #include <sys/signalvar.h>
     72 #include <sys/systm.h>
     73 #include <sys/vnode.h>
     74 #include <sys/tty.h>
     75 #include <sys/conf.h>
     76 #include <sys/disklabel.h>
     77 #include <sys/buf.h>
     78 #include <sys/device.h>
     79 #include <sys/disk.h>
     80 #include <sys/exec.h>
     81 #include <sys/socketvar.h>
     82 #include <sys/protosw.h>
     83 #include <sys/reboot.h>
     84 #include <sys/user.h>
     85 #include <sys/sysctl.h>
     86 #include <sys/event.h>
     87 #ifdef SYSVSHM
     88 #include <sys/shm.h>
     89 #endif
     90 #ifdef SYSVSEM
     91 #include <sys/sem.h>
     92 #endif
     93 #ifdef SYSVMSG
     94 #include <sys/msg.h>
     95 #endif
     96 #ifdef P1003_1B_SEMAPHORE
     97 #include <sys/ksem.h>
     98 #endif
     99 #ifdef SYSTRACE
    100 #include <sys/systrace.h>
    101 #endif
    102 #include <sys/domain.h>
    103 #include <sys/mbuf.h>
    104 #include <sys/namei.h>
    105 #if NOPENCRYPTO > 0
    106 #include <opencrypto/cryptodev.h>	/* XXX really  the framework */
    107 #endif
    108 #if NRND > 0
    109 #include <sys/rnd.h>
    110 #endif
    111 #ifndef PIPE_SOCKETPAIR
    112 #include <sys/pipe.h>
    113 #endif
    114 #ifdef LKM
    115 #include <sys/lkm.h>
    116 #endif
    117 
    118 #include <sys/syscall.h>
    119 #include <sys/sa.h>
    120 #include <sys/syscallargs.h>
    121 
    122 #include <ufs/ufs/quota.h>
    123 
    124 #include <miscfs/genfs/genfs.h>
    125 #include <miscfs/syncfs/syncfs.h>
    126 
    127 #include <machine/cpu.h>
    128 
    129 #include <uvm/uvm.h>
    130 
    131 #include <dev/cons.h>
    132 
    133 #include <net/if.h>
    134 #include <net/raw_cb.h>
    135 
    136 const char copyright[] =
    137 "Copyright (c) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003\n"
    138 "    The NetBSD Foundation, Inc.  All rights reserved.\n"
    139 "Copyright (c) 1982, 1986, 1989, 1991, 1993\n"
    140 "    The Regents of the University of California.  All rights reserved.\n"
    141 "\n";
    142 
    143 /* Components of the first process -- never freed. */
    144 struct	session session0;
    145 struct	pgrp pgrp0;
    146 struct	proc proc0;
    147 struct	lwp lwp0;
    148 struct	pcred cred0;
    149 struct	filedesc0 filedesc0;
    150 struct	cwdinfo cwdi0;
    151 struct	plimit limit0;
    152 struct	pstats pstat0;
    153 struct	vmspace vmspace0;
    154 struct	sigacts sigacts0;
    155 #ifndef curlwp
    156 struct	lwp *curlwp = &lwp0;
    157 #endif
    158 struct	proc *initproc;
    159 
    160 int	nofile = NOFILE;
    161 int	maxuprc = MAXUPRC;
    162 int	cmask = CMASK;
    163 extern	struct user *proc0paddr;
    164 
    165 struct	vnode *rootvp, *swapdev_vp;
    166 int	boothowto;
    167 int	cold = 1;			/* still working on startup */
    168 struct	timeval boottime;
    169 
    170 __volatile int start_init_exec;		/* semaphore for start_init() */
    171 
    172 static void check_console(struct proc *p);
    173 static void start_init(void *);
    174 void main(void);
    175 
    176 extern const struct emul emul_netbsd;	/* defined in kern_exec.c */
    177 
    178 /*
    179  * System startup; initialize the world, create process 0, mount root
    180  * filesystem, and fork to create init and pagedaemon.  Most of the
    181  * hard work is done in the lower-level initialization routines including
    182  * startup(), which does memory initialization and autoconfiguration.
    183  */
    184 void
    185 main(void)
    186 {
    187 	struct lwp *l;
    188 	struct proc *p;
    189 	struct pdevinit *pdev;
    190 	int s, error;
    191 	u_int i;
    192 	rlim_t lim;
    193 	extern struct pdevinit pdevinit[];
    194 	extern void schedcpu(void *);
    195 #if defined(NFSSERVER) || defined(NFS)
    196 	extern void nfs_init(void);
    197 #endif
    198 #ifdef NVNODE_IMPLICIT
    199 	int usevnodes;
    200 #endif
    201 
    202 	/*
    203 	 * Initialize the current LWP pointer (curlwp) before
    204 	 * any possible traps/probes to simplify trap processing.
    205 	 */
    206 	simple_lock_init(&proc0.p_raslock);
    207 	l = &lwp0;
    208 	curlwp = l;
    209 	l->l_cpu = curcpu();
    210 	l->l_proc = &proc0;
    211 	l->l_lid = 1;
    212 	/*
    213 	 * Attempt to find console and initialize
    214 	 * in case of early panic or other messages.
    215 	 */
    216 	consinit();
    217 	printf("%s", copyright);
    218 
    219 	KERNEL_LOCK_INIT();
    220 
    221 	uvm_init();
    222 
    223 	/* Do machine-dependent initialization. */
    224 	cpu_startup();
    225 
    226 	/* Initialize callouts. */
    227 	callout_startup();
    228 
    229 	/*
    230 	 * Initialize mbuf's.  Do this now because we might attempt to
    231 	 * allocate mbufs or mbuf clusters during autoconfiguration.
    232 	 */
    233 	mbinit();
    234 
    235 	/* Initialize kqueues. */
    236 	kqueue_init();
    237 
    238 	/* Initialize sockets. */
    239 	soinit();
    240 
    241 	/*
    242 	 * The following things must be done before autoconfiguration.
    243 	 */
    244 	evcnt_init();		/* initialize event counters */
    245 	disk_init();		/* initialize disk list */
    246 	tty_init();		/* initialize tty list */
    247 #if NRND > 0
    248 	rnd_init();		/* initialize RNG */
    249 #endif
    250 #if NOPENCRYPTO > 0
    251 	/* Initialize crypto subsystem before configuring crypto hardware. */
    252 	(void)crypto_init();
    253 #endif
    254 	/* Initialize the sysctl subsystem. */
    255 	sysctl_init();
    256 
    257 	/*
    258 	 * Initialize process and pgrp structures.
    259 	 */
    260 	procinit();
    261 
    262 #ifdef LKM
    263 	/* Initialize the LKM system. */
    264 	lkm_init();
    265 #endif
    266 
    267 	/*
    268 	 * Create process 0 (the swapper).
    269 	 */
    270 	p = &proc0;
    271 	proc0_insert(p, l, &pgrp0, &session0);
    272 
    273 	/*
    274 	 * Set P_NOCLDWAIT so that kernel threads are reparented to
    275 	 * init(8) when they exit.  init(8) can easily wait them out
    276 	 * for us.
    277 	 */
    278 	p->p_flag = P_SYSTEM | P_NOCLDWAIT;
    279 	p->p_stat = SACTIVE;
    280 	p->p_nice = NZERO;
    281 	p->p_emul = &emul_netbsd;
    282 #ifdef __HAVE_SYSCALL_INTERN
    283 	(*p->p_emul->e_syscall_intern)(p);
    284 #endif
    285 	strncpy(p->p_comm, "swapper", MAXCOMLEN);
    286 
    287 	l->l_flag = L_INMEM;
    288 	l->l_stat = LSONPROC;
    289 	p->p_nrlwps = 1;
    290 
    291 	callout_init(&l->l_tsleep_ch);
    292 
    293 	/* Create credentials. */
    294 	cred0.p_refcnt = 1;
    295 	p->p_cred = &cred0;
    296 	p->p_ucred = crget();
    297 	p->p_ucred->cr_ngroups = 1;	/* group 0 */
    298 
    299 	/* Create the file descriptor table. */
    300 	finit();
    301 	p->p_fd = &filedesc0.fd_fd;
    302 	fdinit1(&filedesc0);
    303 
    304 	/* Create the CWD info. */
    305 	p->p_cwdi = &cwdi0;
    306 	cwdi0.cwdi_cmask = cmask;
    307 	cwdi0.cwdi_refcnt = 1;
    308 
    309 	/* Create the limits structures. */
    310 	p->p_limit = &limit0;
    311 	for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++)
    312 		limit0.pl_rlimit[i].rlim_cur =
    313 		    limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY;
    314 
    315 	limit0.pl_rlimit[RLIMIT_NOFILE].rlim_max = maxfiles;
    316 	limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur =
    317 	    maxfiles < nofile ? maxfiles : nofile;
    318 
    319 	limit0.pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc;
    320 	limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur =
    321 	    maxproc < maxuprc ? maxproc : maxuprc;
    322 
    323 	lim = ptoa(uvmexp.free);
    324 	limit0.pl_rlimit[RLIMIT_RSS].rlim_max = lim;
    325 	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = lim;
    326 	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = lim / 3;
    327 	limit0.pl_corename = defcorename;
    328 	limit0.p_refcnt = 1;
    329 
    330 	/*
    331 	 * Initialize proc0's vmspace, which uses the kernel pmap.
    332 	 * All kernel processes (which never have user space mappings)
    333 	 * share proc0's vmspace, and thus, the kernel pmap.
    334 	 */
    335 	uvmspace_init(&vmspace0, pmap_kernel(), round_page(VM_MIN_ADDRESS),
    336 	    trunc_page(VM_MAX_ADDRESS));
    337 	p->p_vmspace = &vmspace0;
    338 
    339 	l->l_addr = proc0paddr;				/* XXX */
    340 
    341 	p->p_stats = &pstat0;
    342 
    343 	/*
    344 	 * Charge root for one process.
    345 	 */
    346 	(void)chgproccnt(0, 1);
    347 
    348 	rqinit();
    349 
    350 	/* Configure virtual memory system, set vm rlimits. */
    351 	uvm_init_limits(p);
    352 
    353 	/* Initialize the file systems. */
    354 #if defined(NFSSERVER) || defined(NFS)
    355 	nfs_init();			/* initialize server/shared data */
    356 #endif
    357 #ifdef NVNODE_IMPLICIT
    358 	/*
    359 	 * If maximum number of vnodes in namei vnode cache is not explicitly
    360 	 * defined in kernel config, adjust the number such as we use roughly
    361 	 * 0.5% of memory for vnode cache (but not less than NVNODE vnodes).
    362 	 */
    363 	usevnodes = (ptoa((unsigned)physmem) / 200) / sizeof(struct vnode);
    364 	if (usevnodes > desiredvnodes)
    365 		desiredvnodes = usevnodes;
    366 #endif
    367 	vfsinit();
    368 
    369 	/* Configure the system hardware.  This will enable interrupts. */
    370 	configure();
    371 
    372 	ubc_init();		/* must be after autoconfig */
    373 
    374 	/* Lock the kernel on behalf of proc0. */
    375 	KERNEL_PROC_LOCK(l);
    376 
    377 #ifdef SYSVSHM
    378 	/* Initialize System V style shared memory. */
    379 	shminit();
    380 #endif
    381 
    382 #ifdef SYSVSEM
    383 	/* Initialize System V style semaphores. */
    384 	seminit();
    385 #endif
    386 
    387 #ifdef SYSVMSG
    388 	/* Initialize System V style message queues. */
    389 	msginit();
    390 #endif
    391 
    392 
    393 #ifdef P1003_1B_SEMAPHORE
    394 	/* Initialize posix semaphores */
    395 	ksem_init();
    396 #endif
    397 	/* Attach pseudo-devices. */
    398 	for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++)
    399 		(*pdev->pdev_attach)(pdev->pdev_count);
    400 
    401 	/*
    402 	 * Initialize protocols.  Block reception of incoming packets
    403 	 * until everything is ready.
    404 	 */
    405 	s = splnet();
    406 	ifinit();
    407 	domaininit();
    408 	if_attachdomain();
    409 	splx(s);
    410 
    411 #ifdef GPROF
    412 	/* Initialize kernel profiling. */
    413 	kmstartup();
    414 #endif
    415 
    416 	/* Initialize system accouting. */
    417 	acct_init();
    418 
    419 #ifdef SYSTRACE
    420 	systrace_init();
    421 #endif
    422 	/*
    423 	 * Initialize signal-related data structures, and signal state
    424 	 * for proc0.
    425 	 */
    426 	signal_init();
    427 	p->p_sigacts = &sigacts0;
    428 	siginit(p);
    429 
    430 	/* Kick off timeout driven events by calling first time. */
    431 	schedcpu(NULL);
    432 
    433 	/*
    434 	 * Create process 1 (init(8)).  We do this now, as Unix has
    435 	 * historically had init be process 1, and changing this would
    436 	 * probably upset a lot of people.
    437 	 *
    438 	 * Note that process 1 won't immediately exec init(8), but will
    439 	 * wait for us to inform it that the root file system has been
    440 	 * mounted.
    441 	 */
    442 	if (fork1(l, 0, SIGCHLD, NULL, 0, start_init, NULL, NULL, &initproc))
    443 		panic("fork init");
    444 
    445 	/*
    446 	 * Create any kernel threads who's creation was deferred because
    447 	 * initproc had not yet been created.
    448 	 */
    449 	kthread_run_deferred_queue();
    450 
    451 	/*
    452 	 * Now that device driver threads have been created, wait for
    453 	 * them to finish any deferred autoconfiguration.  Note we don't
    454 	 * need to lock this semaphore, since we haven't booted any
    455 	 * secondary processors, yet.
    456 	 */
    457 	while (config_pending)
    458 		(void) tsleep((void *)&config_pending, PWAIT, "cfpend", 0);
    459 
    460 	/*
    461 	 * Finalize configuration now that all real devices have been
    462 	 * found.  This needs to be done before the root device is
    463 	 * selected, since finalization may create the root device.
    464 	 */
    465 	config_finalize();
    466 
    467 	/*
    468 	 * Now that autoconfiguration has completed, we can determine
    469 	 * the root and dump devices.
    470 	 */
    471 	cpu_rootconf();
    472 	cpu_dumpconf();
    473 
    474 	/* Mount the root file system. */
    475 	do {
    476 		domountroothook();
    477 		if ((error = vfs_mountroot())) {
    478 			printf("cannot mount root, error = %d\n", error);
    479 			boothowto |= RB_ASKNAME;
    480 			setroot(root_device,
    481 			    (rootdev != NODEV) ? DISKPART(rootdev) : 0);
    482 		}
    483 	} while (error != 0);
    484 	mountroothook_destroy();
    485 
    486 	CIRCLEQ_FIRST(&mountlist)->mnt_flag |= MNT_ROOTFS;
    487 	CIRCLEQ_FIRST(&mountlist)->mnt_op->vfs_refcount++;
    488 
    489 	/*
    490 	 * Get the vnode for '/'.  Set filedesc0.fd_fd.fd_cdir to
    491 	 * reference it.
    492 	 */
    493 	if (VFS_ROOT(CIRCLEQ_FIRST(&mountlist), &rootvnode))
    494 		panic("cannot find root vnode");
    495 	cwdi0.cwdi_cdir = rootvnode;
    496 	VREF(cwdi0.cwdi_cdir);
    497 	VOP_UNLOCK(rootvnode, 0);
    498 	cwdi0.cwdi_rdir = NULL;
    499 
    500 	/*
    501 	 * Now that root is mounted, we can fixup initproc's CWD
    502 	 * info.  All other processes are kthreads, which merely
    503 	 * share proc0's CWD info.
    504 	 */
    505 	initproc->p_cwdi->cwdi_cdir = rootvnode;
    506 	VREF(initproc->p_cwdi->cwdi_cdir);
    507 	initproc->p_cwdi->cwdi_rdir = NULL;
    508 
    509 	/*
    510 	 * Now can look at time, having had a chance to verify the time
    511 	 * from the file system.  Reset p->p_rtime as it may have been
    512 	 * munched in mi_switch() after the time got set.
    513 	 */
    514 	proclist_lock_read();
    515 	s = splsched();
    516 	for (p = LIST_FIRST(&allproc); p != NULL;
    517 	     p = LIST_NEXT(p, p_list)) {
    518 		p->p_stats->p_start = mono_time = boottime = time;
    519 		for (l = LIST_FIRST(&p->p_lwps); l != NULL;
    520 		     l = LIST_NEXT(l, l_sibling))
    521 			if (l->l_cpu != NULL)
    522 				l->l_cpu->ci_schedstate.spc_runtime = time;
    523 		p->p_rtime.tv_sec = p->p_rtime.tv_usec = 0;
    524 	}
    525 	splx(s);
    526 	proclist_unlock_read();
    527 
    528 	/* Create the pageout daemon kernel thread. */
    529 	uvm_swap_init();
    530 	if (kthread_create1(uvm_pageout, NULL, NULL, "pagedaemon"))
    531 		panic("fork pagedaemon");
    532 
    533 	/* Create the process reaper kernel thread. */
    534 	if (kthread_create1(reaper, NULL, NULL, "reaper"))
    535 		panic("fork reaper");
    536 
    537 	/* Create the filesystem syncer kernel thread. */
    538 	if (kthread_create1(sched_sync, NULL, NULL, "ioflush"))
    539 		panic("fork syncer");
    540 
    541 	/* Create the aiodone daemon kernel thread. */
    542 	if (kthread_create1(uvm_aiodone_daemon, NULL, &uvm.aiodoned_proc,
    543 	    "aiodoned"))
    544 		panic("fork aiodoned");
    545 
    546 #if defined(MULTIPROCESSOR)
    547 	/* Boot the secondary processors. */
    548 	cpu_boot_secondary_processors();
    549 #endif
    550 
    551 	/* Initialize exec structures */
    552 	exec_init(1);
    553 
    554 #ifndef PIPE_SOCKETPAIR
    555 	/* Initialize pipe structures */
    556 	pipe_init();
    557 #endif
    558 
    559 	/*
    560 	 * Okay, now we can let init(8) exec!  It's off to userland!
    561 	 */
    562 	start_init_exec = 1;
    563 	wakeup((void *)&start_init_exec);
    564 
    565 	/* The scheduler is an infinite loop. */
    566 	uvm_scheduler();
    567 	/* NOTREACHED */
    568 }
    569 
    570 static void
    571 check_console(struct proc *p)
    572 {
    573 	struct nameidata nd;
    574 	int error;
    575 
    576 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/console", p);
    577 	error = namei(&nd);
    578 	if (error == 0)
    579 		vrele(nd.ni_vp);
    580 	else if (error == ENOENT)
    581 		printf("warning: no /dev/console\n");
    582 	else
    583 		printf("warning: lookup /dev/console: error %d\n", error);
    584 }
    585 
    586 /*
    587  * List of paths to try when searching for "init".
    588  */
    589 static const char *initpaths[] = {
    590 	"/sbin/init",
    591 	"/sbin/oinit",
    592 	"/sbin/init.bak",
    593 	NULL,
    594 };
    595 
    596 /*
    597  * Start the initial user process; try exec'ing each pathname in "initpaths".
    598  * The program is invoked with one argument containing the boot flags.
    599  */
    600 static void
    601 start_init(void *arg)
    602 {
    603 	struct lwp *l = arg;
    604 	struct proc *p = l->l_proc;
    605 	vaddr_t addr;
    606 	struct sys_execve_args /* {
    607 		syscallarg(const char *) path;
    608 		syscallarg(char * const *) argp;
    609 		syscallarg(char * const *) envp;
    610 	} */ args;
    611 	int options, i, error;
    612 	register_t retval[2];
    613 	char flags[4], *flagsp;
    614 	const char *path, *slash;
    615 	char *ucp, **uap, *arg0, *arg1 = NULL;
    616 	char ipath[129];
    617 	int ipx, len;
    618 
    619 	/*
    620 	 * Now in process 1.
    621 	 */
    622 	strncpy(p->p_comm, "init", MAXCOMLEN);
    623 
    624 	/*
    625 	 * Wait for main() to tell us that it's safe to exec.
    626 	 */
    627 	while (start_init_exec == 0)
    628 		(void) tsleep((void *)&start_init_exec, PWAIT, "initexec", 0);
    629 
    630 	/*
    631 	 * This is not the right way to do this.  We really should
    632 	 * hand-craft a descriptor onto /dev/console to hand to init,
    633 	 * but that's a _lot_ more work, and the benefit from this easy
    634 	 * hack makes up for the "good is the enemy of the best" effect.
    635 	 */
    636 	check_console(p);
    637 
    638 	/*
    639 	 * Need just enough stack to hold the faked-up "execve()" arguments.
    640 	 */
    641 	addr = (vaddr_t)STACK_ALLOC(USRSTACK, PAGE_SIZE);
    642 	if (uvm_map(&p->p_vmspace->vm_map, &addr, PAGE_SIZE,
    643                     NULL, UVM_UNKNOWN_OFFSET, 0,
    644                     UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY,
    645 		    UVM_ADV_NORMAL,
    646                     UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW)) != 0)
    647 		panic("init: couldn't allocate argument space");
    648 	p->p_vmspace->vm_maxsaddr = (caddr_t)STACK_MAX(addr, PAGE_SIZE);
    649 
    650 	ipx = 0;
    651 	while (1) {
    652 		if (boothowto & RB_ASKNAME) {
    653 			printf("init path");
    654 			if (initpaths[ipx])
    655 				printf(" (default %s)", initpaths[ipx]);
    656 			printf(": ");
    657 			len = cngetsn(ipath, sizeof(ipath)-1);
    658 			if (len == 0) {
    659 				if (initpaths[ipx])
    660 					path = initpaths[ipx++];
    661 				else
    662 					continue;
    663 			} else {
    664 				ipath[len] = '\0';
    665 				path = ipath;
    666 			}
    667 		} else {
    668 			if ((path = initpaths[ipx++]) == NULL)
    669 				break;
    670 		}
    671 
    672 		ucp = (char *)USRSTACK;
    673 
    674 		/*
    675 		 * Construct the boot flag argument.
    676 		 */
    677 		flagsp = flags;
    678 		*flagsp++ = '-';
    679 		options = 0;
    680 
    681 		if (boothowto & RB_SINGLE) {
    682 			*flagsp++ = 's';
    683 			options = 1;
    684 		}
    685 #ifdef notyet
    686 		if (boothowto & RB_FASTBOOT) {
    687 			*flagsp++ = 'f';
    688 			options = 1;
    689 		}
    690 #endif
    691 
    692 		/*
    693 		 * Move out the flags (arg 1), if necessary.
    694 		 */
    695 		if (options != 0) {
    696 			*flagsp++ = '\0';
    697 			i = flagsp - flags;
    698 #ifdef DEBUG
    699 			printf("init: copying out flags `%s' %d\n", flags, i);
    700 #endif
    701 			arg1 = STACK_ALLOC(ucp, i);
    702 			ucp = STACK_MAX(arg1, i);
    703 			(void)copyout((caddr_t)flags, arg1, i);
    704 		}
    705 
    706 		/*
    707 		 * Move out the file name (also arg 0).
    708 		 */
    709 		i = strlen(path) + 1;
    710 #ifdef DEBUG
    711 		printf("init: copying out path `%s' %d\n", path, i);
    712 #else
    713 		if (boothowto & RB_ASKNAME || path != initpaths[0])
    714 			printf("init: trying %s\n", path);
    715 #endif
    716 		arg0 = STACK_ALLOC(ucp, i);
    717 		ucp = STACK_MAX(arg0, i);
    718 		(void)copyout((caddr_t)path, arg0, i);
    719 
    720 		/*
    721 		 * Move out the arg pointers.
    722 		 */
    723 		ucp = (caddr_t)STACK_ALIGN(ucp, ALIGNBYTES);
    724 		uap = (char **)STACK_ALLOC(ucp, sizeof(char *) * 3);
    725 		SCARG(&args, path) = arg0;
    726 		SCARG(&args, argp) = uap;
    727 		SCARG(&args, envp) = NULL;
    728 		slash = strrchr(path, '/');
    729 		if (slash)
    730 			(void)suword((caddr_t)uap++,
    731 			    (long)arg0 + (slash + 1 - path));
    732 		else
    733 			(void)suword((caddr_t)uap++, (long)arg0);
    734 		if (options != 0)
    735 			(void)suword((caddr_t)uap++, (long)arg1);
    736 		(void)suword((caddr_t)uap++, 0);	/* terminator */
    737 
    738 		/*
    739 		 * Now try to exec the program.  If can't for any reason
    740 		 * other than it doesn't exist, complain.
    741 		 */
    742 		error = sys_execve(LIST_FIRST(&p->p_lwps), &args, retval);
    743 		if (error == 0 || error == EJUSTRETURN) {
    744 			KERNEL_PROC_UNLOCK(l);
    745 			return;
    746 		}
    747 		printf("exec %s: error %d\n", path, error);
    748 	}
    749 	printf("init: not found\n");
    750 	panic("no init");
    751 }
    752