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