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