xref: /netbsd-src/sys/kern/init_main.c (revision 453f6b99a313f2f372963fe81f55bf6f811e3f55)
1 /*	$NetBSD: init_main.c,v 1.217 2003/01/20 20:02:56 christos 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.217 2003/01/20 20:02:56 christos 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 	LIST_INIT(&p->p_lwps);
265 	LIST_INSERT_HEAD(&p->p_lwps, l, l_sibling);
266 	p->p_nlwps = 1;
267 
268 	s = proclist_lock_write();
269 	LIST_INSERT_HEAD(&allproc, p, p_list);
270 	LIST_INSERT_HEAD(PIDHASH(p->p_pid), p, p_hash);
271 	LIST_INSERT_HEAD(&alllwp, l, l_list);
272 	proclist_unlock_write(s);
273 
274 	p->p_pgrp = &pgrp0;
275 	LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash);
276 	LIST_INIT(&pgrp0.pg_members);
277 	LIST_INSERT_HEAD(&pgrp0.pg_members, p, p_pglist);
278 
279 	pgrp0.pg_session = &session0;
280 	session0.s_count = 1;
281 	session0.s_sid = p->p_pid;
282 	session0.s_leader = p;
283 
284 	/*
285 	 * Set P_NOCLDWAIT so that kernel threads are reparented to
286 	 * init(8) when they exit.  init(8) can easily wait them out
287 	 * for us.
288 	 */
289 	p->p_flag = P_SYSTEM | P_NOCLDWAIT;
290 	p->p_stat = SACTIVE;
291 	p->p_nice = NZERO;
292 	p->p_emul = &emul_netbsd;
293 #ifdef __HAVE_SYSCALL_INTERN
294 	(*p->p_emul->e_syscall_intern)(p);
295 #endif
296 	strncpy(p->p_comm, "swapper", MAXCOMLEN);
297 
298 	l->l_flag = L_INMEM;
299 	l->l_stat = LSONPROC;
300 	p->p_nrlwps = 1;
301 
302 	callout_init(&l->l_tsleep_ch);
303 
304 	/* Create credentials. */
305 	cred0.p_refcnt = 1;
306 	p->p_cred = &cred0;
307 	p->p_ucred = crget();
308 	p->p_ucred->cr_ngroups = 1;	/* group 0 */
309 
310 	/* Create the file descriptor table. */
311 	finit();
312 	p->p_fd = &filedesc0.fd_fd;
313 	fdinit1(&filedesc0);
314 
315 	/* Create the CWD info. */
316 	p->p_cwdi = &cwdi0;
317 	cwdi0.cwdi_cmask = cmask;
318 	cwdi0.cwdi_refcnt = 1;
319 
320 	/* Create the limits structures. */
321 	p->p_limit = &limit0;
322 	for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++)
323 		limit0.pl_rlimit[i].rlim_cur =
324 		    limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY;
325 
326 	limit0.pl_rlimit[RLIMIT_NOFILE].rlim_max = maxfiles;
327 	limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur =
328 	    maxfiles < nofile ? maxfiles : nofile;
329 
330 	limit0.pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc;
331 	limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur =
332 	    maxproc < maxuprc ? maxproc : maxuprc;
333 
334 	lim = ptoa(uvmexp.free);
335 	limit0.pl_rlimit[RLIMIT_RSS].rlim_max = lim;
336 	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = lim;
337 	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = lim / 3;
338 	limit0.pl_corename = defcorename;
339 	limit0.p_refcnt = 1;
340 
341 	/*
342 	 * Initialize proc0's vmspace, which uses the kernel pmap.
343 	 * All kernel processes (which never have user space mappings)
344 	 * share proc0's vmspace, and thus, the kernel pmap.
345 	 */
346 	uvmspace_init(&vmspace0, pmap_kernel(), round_page(VM_MIN_ADDRESS),
347 	    trunc_page(VM_MAX_ADDRESS));
348 	p->p_vmspace = &vmspace0;
349 
350 	l->l_addr = proc0paddr;				/* XXX */
351 
352 	p->p_stats = &pstat0;
353 
354 	/*
355 	 * Charge root for one process.
356 	 */
357 	(void)chgproccnt(0, 1);
358 
359 	rqinit();
360 
361 	/* Configure virtual memory system, set vm rlimits. */
362 	uvm_init_limits(p);
363 
364 	/* Initialize the file systems. */
365 #if defined(NFSSERVER) || defined(NFS)
366 	nfs_init();			/* initialize server/shared data */
367 #endif
368 #ifdef NVNODE_IMPLICIT
369 	/*
370 	 * If maximum number of vnodes in namei vnode cache is not explicitly
371 	 * defined in kernel config, adjust the number such as we use roughly
372 	 * 0.5% of memory for vnode cache (but not less than NVNODE vnodes).
373 	 */
374 	usevnodes = (ptoa((unsigned)physmem) / 200) / sizeof(struct vnode);
375 	if (usevnodes > desiredvnodes)
376 		desiredvnodes = usevnodes;
377 #endif
378 	vfsinit();
379 
380 	/* Configure the system hardware.  This will enable interrupts. */
381 	configure();
382 
383 	ubc_init();		/* must be after autoconfig */
384 
385 	/* Lock the kernel on behalf of proc0. */
386 	KERNEL_PROC_LOCK(l);
387 
388 #ifdef SYSVSHM
389 	/* Initialize System V style shared memory. */
390 	shminit();
391 #endif
392 
393 #ifdef SYSVSEM
394 	/* Initialize System V style semaphores. */
395 	seminit();
396 #endif
397 
398 #ifdef SYSVMSG
399 	/* Initialize System V style message queues. */
400 	msginit();
401 #endif
402 
403 
404 #ifdef P1003_1B_SEMAPHORE
405 	/* Initialize posix semaphores */
406 	ksem_init();
407 #endif
408 	/* Attach pseudo-devices. */
409 	for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++)
410 		(*pdev->pdev_attach)(pdev->pdev_count);
411 
412 	/*
413 	 * Initialize protocols.  Block reception of incoming packets
414 	 * until everything is ready.
415 	 */
416 	s = splnet();
417 	ifinit();
418 	domaininit();
419 	if_attachdomain();
420 	splx(s);
421 
422 #ifdef GPROF
423 	/* Initialize kernel profiling. */
424 	kmstartup();
425 #endif
426 
427 	/* Initialize system accouting. */
428 	acct_init();
429 
430 #ifdef SYSTRACE
431 	systrace_init();
432 #endif
433 	/*
434 	 * Initialize signal-related data structures, and signal state
435 	 * for proc0.
436 	 */
437 	signal_init();
438 	p->p_sigacts = &sigacts0;
439 	siginit(p);
440 
441 	/* Kick off timeout driven events by calling first time. */
442 	schedcpu(NULL);
443 
444 	/*
445 	 * Create process 1 (init(8)).  We do this now, as Unix has
446 	 * historically had init be process 1, and changing this would
447 	 * probably upset a lot of people.
448 	 *
449 	 * Note that process 1 won't immediately exec init(8), but will
450 	 * wait for us to inform it that the root file system has been
451 	 * mounted.
452 	 */
453 	if (fork1(l, 0, SIGCHLD, NULL, 0, start_init, NULL, NULL, &initproc))
454 		panic("fork init");
455 
456 	/*
457 	 * Create any kernel threads who's creation was deferred because
458 	 * initproc had not yet been created.
459 	 */
460 	kthread_run_deferred_queue();
461 
462 	/*
463 	 * Now that device driver threads have been created, wait for
464 	 * them to finish any deferred autoconfiguration.  Note we don't
465 	 * need to lock this semaphore, since we haven't booted any
466 	 * secondary processors, yet.
467 	 */
468 	while (config_pending)
469 		(void) tsleep((void *)&config_pending, PWAIT, "cfpend", 0);
470 
471 	/*
472 	 * Finalize configuration now that all real devices have been
473 	 * found.  This needs to be done before the root device is
474 	 * selected, since finalization may create the root device.
475 	 */
476 	config_finalize();
477 
478 	/*
479 	 * Now that autoconfiguration has completed, we can determine
480 	 * the root and dump devices.
481 	 */
482 	cpu_rootconf();
483 	cpu_dumpconf();
484 
485 	/* Mount the root file system. */
486 	do {
487 		domountroothook();
488 		if ((error = vfs_mountroot())) {
489 			printf("cannot mount root, error = %d\n", error);
490 			boothowto |= RB_ASKNAME;
491 			setroot(root_device,
492 			    (rootdev != NODEV) ? DISKPART(rootdev) : 0);
493 		}
494 	} while (error != 0);
495 	mountroothook_destroy();
496 
497 	CIRCLEQ_FIRST(&mountlist)->mnt_flag |= MNT_ROOTFS;
498 	CIRCLEQ_FIRST(&mountlist)->mnt_op->vfs_refcount++;
499 
500 	/*
501 	 * Get the vnode for '/'.  Set filedesc0.fd_fd.fd_cdir to
502 	 * reference it.
503 	 */
504 	if (VFS_ROOT(CIRCLEQ_FIRST(&mountlist), &rootvnode))
505 		panic("cannot find root vnode");
506 	cwdi0.cwdi_cdir = rootvnode;
507 	VREF(cwdi0.cwdi_cdir);
508 	VOP_UNLOCK(rootvnode, 0);
509 	cwdi0.cwdi_rdir = NULL;
510 
511 	/*
512 	 * Now that root is mounted, we can fixup initproc's CWD
513 	 * info.  All other processes are kthreads, which merely
514 	 * share proc0's CWD info.
515 	 */
516 	initproc->p_cwdi->cwdi_cdir = rootvnode;
517 	VREF(initproc->p_cwdi->cwdi_cdir);
518 	initproc->p_cwdi->cwdi_rdir = NULL;
519 
520 	/*
521 	 * Now can look at time, having had a chance to verify the time
522 	 * from the file system.  Reset p->p_rtime as it may have been
523 	 * munched in mi_switch() after the time got set.
524 	 */
525 	proclist_lock_read();
526 	s = splsched();
527 	for (p = LIST_FIRST(&allproc); p != NULL;
528 	     p = LIST_NEXT(p, p_list)) {
529 		p->p_stats->p_start = mono_time = boottime = time;
530 		for (l = LIST_FIRST(&p->p_lwps); l != NULL;
531 		     l = LIST_NEXT(l, l_sibling))
532 			if (l->l_cpu != NULL)
533 				l->l_cpu->ci_schedstate.spc_runtime = time;
534 		p->p_rtime.tv_sec = p->p_rtime.tv_usec = 0;
535 	}
536 	splx(s);
537 	proclist_unlock_read();
538 
539 	/* Create the pageout daemon kernel thread. */
540 	uvm_swap_init();
541 	if (kthread_create1(uvm_pageout, NULL, NULL, "pagedaemon"))
542 		panic("fork pagedaemon");
543 
544 	/* Create the process reaper kernel thread. */
545 	if (kthread_create1(reaper, NULL, NULL, "reaper"))
546 		panic("fork reaper");
547 
548 	/* Create the filesystem syncer kernel thread. */
549 	if (kthread_create1(sched_sync, NULL, NULL, "ioflush"))
550 		panic("fork syncer");
551 
552 	/* Create the aiodone daemon kernel thread. */
553 	if (kthread_create1(uvm_aiodone_daemon, NULL, &uvm.aiodoned_proc,
554 	    "aiodoned"))
555 		panic("fork aiodoned");
556 
557 #if defined(MULTIPROCESSOR)
558 	/* Boot the secondary processors. */
559 	cpu_boot_secondary_processors();
560 #endif
561 
562 	/* Initialize exec structures */
563 	exec_init(1);
564 
565 #ifndef PIPE_SOCKETPAIR
566 	/* Initialize pipe structures */
567 	pipe_init();
568 #endif
569 
570 	/*
571 	 * Okay, now we can let init(8) exec!  It's off to userland!
572 	 */
573 	start_init_exec = 1;
574 	wakeup((void *)&start_init_exec);
575 
576 	/* The scheduler is an infinite loop. */
577 	uvm_scheduler();
578 	/* NOTREACHED */
579 }
580 
581 static void
582 check_console(struct proc *p)
583 {
584 	struct nameidata nd;
585 	int error;
586 
587 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/console", p);
588 	error = namei(&nd);
589 	if (error == 0)
590 		vrele(nd.ni_vp);
591 	else if (error == ENOENT)
592 		printf("warning: no /dev/console\n");
593 	else
594 		printf("warning: lookup /dev/console: error %d\n", error);
595 }
596 
597 /*
598  * List of paths to try when searching for "init".
599  */
600 static const char *initpaths[] = {
601 	"/sbin/init",
602 	"/sbin/oinit",
603 	"/sbin/init.bak",
604 	NULL,
605 };
606 
607 /*
608  * Start the initial user process; try exec'ing each pathname in "initpaths".
609  * The program is invoked with one argument containing the boot flags.
610  */
611 static void
612 start_init(void *arg)
613 {
614 	struct lwp *l = arg;
615 	struct proc *p = l->l_proc;
616 	vaddr_t addr;
617 	struct sys_execve_args /* {
618 		syscallarg(const char *) path;
619 		syscallarg(char * const *) argp;
620 		syscallarg(char * const *) envp;
621 	} */ args;
622 	int options, i, error;
623 	register_t retval[2];
624 	char flags[4], *flagsp;
625 	const char *path, *slash;
626 	char *ucp, **uap, *arg0, *arg1 = NULL;
627 	char ipath[129];
628 	int ipx, len;
629 
630 	/*
631 	 * Now in process 1.
632 	 */
633 	strncpy(p->p_comm, "init", MAXCOMLEN);
634 
635 	/*
636 	 * Wait for main() to tell us that it's safe to exec.
637 	 */
638 	while (start_init_exec == 0)
639 		(void) tsleep((void *)&start_init_exec, PWAIT, "initexec", 0);
640 
641 	/*
642 	 * This is not the right way to do this.  We really should
643 	 * hand-craft a descriptor onto /dev/console to hand to init,
644 	 * but that's a _lot_ more work, and the benefit from this easy
645 	 * hack makes up for the "good is the enemy of the best" effect.
646 	 */
647 	check_console(p);
648 
649 	/*
650 	 * Need just enough stack to hold the faked-up "execve()" arguments.
651 	 */
652 	addr = (vaddr_t)STACK_ALLOC(USRSTACK, PAGE_SIZE);
653 	if (uvm_map(&p->p_vmspace->vm_map, &addr, PAGE_SIZE,
654                     NULL, UVM_UNKNOWN_OFFSET, 0,
655                     UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY,
656 		    UVM_ADV_NORMAL,
657                     UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW)) != 0)
658 		panic("init: couldn't allocate argument space");
659 	p->p_vmspace->vm_maxsaddr = (caddr_t)STACK_MAX(addr, PAGE_SIZE);
660 
661 	ipx = 0;
662 	while (1) {
663 		if (boothowto & RB_ASKNAME) {
664 			printf("init path");
665 			if (initpaths[ipx])
666 				printf(" (default %s)", initpaths[ipx]);
667 			printf(": ");
668 			len = cngetsn(ipath, sizeof(ipath)-1);
669 			if (len == 0) {
670 				if (initpaths[ipx])
671 					path = initpaths[ipx++];
672 				else
673 					continue;
674 			} else {
675 				ipath[len] = '\0';
676 				path = ipath;
677 			}
678 		} else {
679 			if ((path = initpaths[ipx++]) == NULL)
680 				break;
681 		}
682 
683 		ucp = (char *)USRSTACK;
684 
685 		/*
686 		 * Construct the boot flag argument.
687 		 */
688 		flagsp = flags;
689 		*flagsp++ = '-';
690 		options = 0;
691 
692 		if (boothowto & RB_SINGLE) {
693 			*flagsp++ = 's';
694 			options = 1;
695 		}
696 #ifdef notyet
697 		if (boothowto & RB_FASTBOOT) {
698 			*flagsp++ = 'f';
699 			options = 1;
700 		}
701 #endif
702 
703 		/*
704 		 * Move out the flags (arg 1), if necessary.
705 		 */
706 		if (options != 0) {
707 			*flagsp++ = '\0';
708 			i = flagsp - flags;
709 #ifdef DEBUG
710 			printf("init: copying out flags `%s' %d\n", flags, i);
711 #endif
712 			arg1 = STACK_ALLOC(ucp, i);
713 			ucp = STACK_MAX(arg1, i);
714 			(void)copyout((caddr_t)flags, arg1, i);
715 		}
716 
717 		/*
718 		 * Move out the file name (also arg 0).
719 		 */
720 		i = strlen(path) + 1;
721 #ifdef DEBUG
722 		printf("init: copying out path `%s' %d\n", path, i);
723 #else
724 		if (boothowto & RB_ASKNAME || path != initpaths[0])
725 			printf("init: trying %s\n", path);
726 #endif
727 		arg0 = STACK_ALLOC(ucp, i);
728 		ucp = STACK_MAX(arg0, i);
729 		(void)copyout((caddr_t)path, arg0, i);
730 
731 		/*
732 		 * Move out the arg pointers.
733 		 */
734 		ucp = (caddr_t)STACK_ALIGN(ucp, ALIGNBYTES);
735 		uap = (char **)STACK_ALLOC(ucp, sizeof(char *) * 3);
736 		SCARG(&args, path) = arg0;
737 		SCARG(&args, argp) = uap;
738 		SCARG(&args, envp) = NULL;
739 		slash = strrchr(path, '/');
740 		if (slash)
741 			(void)suword((caddr_t)uap++,
742 			    (long)arg0 + (slash + 1 - path));
743 		else
744 			(void)suword((caddr_t)uap++, (long)arg0);
745 		if (options != 0)
746 			(void)suword((caddr_t)uap++, (long)arg1);
747 		(void)suword((caddr_t)uap++, 0);	/* terminator */
748 
749 		/*
750 		 * Now try to exec the program.  If can't for any reason
751 		 * other than it doesn't exist, complain.
752 		 */
753 		error = sys_execve(LIST_FIRST(&p->p_lwps), &args, retval);
754 		if (error == 0 || error == EJUSTRETURN) {
755 			KERNEL_PROC_UNLOCK(l);
756 			return;
757 		}
758 		printf("exec %s: error %d\n", path, error);
759 	}
760 	printf("init: not found\n");
761 	panic("no init");
762 }
763