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