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