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