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