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