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