xref: /netbsd-src/sys/kern/init_main.c (revision 69b6d498973bb4d7230c2d3c12bd9a032738ec8e)
1 /*	$NetBSD: init_main.c,v 1.250 2005/07/16 22:47:18 christos 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.250 2005/07/16 22:47:18 christos 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 #include "opt_rootfs_magiclinks.h"
88 #include "opt_verified_exec.h"
89 
90 #include "opencrypto.h"
91 #include "rnd.h"
92 
93 #include <sys/param.h>
94 #include <sys/acct.h>
95 #include <sys/filedesc.h>
96 #include <sys/file.h>
97 #include <sys/errno.h>
98 #include <sys/callout.h>
99 #include <sys/kernel.h>
100 #include <sys/kcont.h>
101 #include <sys/mount.h>
102 #include <sys/proc.h>
103 #include <sys/kthread.h>
104 #include <sys/resourcevar.h>
105 #include <sys/signalvar.h>
106 #include <sys/systm.h>
107 #include <sys/vnode.h>
108 #include <sys/tty.h>
109 #include <sys/conf.h>
110 #include <sys/disklabel.h>
111 #include <sys/buf.h>
112 #include <sys/device.h>
113 #include <sys/exec.h>
114 #include <sys/socketvar.h>
115 #include <sys/protosw.h>
116 #include <sys/reboot.h>
117 #include <sys/user.h>
118 #include <sys/sysctl.h>
119 #include <sys/event.h>
120 #include <sys/mbuf.h>
121 #ifdef FAST_IPSEC
122 #include <netipsec/ipsec.h>
123 #endif
124 #ifdef SYSVSHM
125 #include <sys/shm.h>
126 #endif
127 #ifdef SYSVSEM
128 #include <sys/sem.h>
129 #endif
130 #ifdef SYSVMSG
131 #include <sys/msg.h>
132 #endif
133 #ifdef P1003_1B_SEMAPHORE
134 #include <sys/ksem.h>
135 #endif
136 #ifdef SYSTRACE
137 #include <sys/systrace.h>
138 #endif
139 #include <sys/domain.h>
140 #include <sys/namei.h>
141 #if NOPENCRYPTO > 0
142 #include <opencrypto/cryptodev.h>	/* XXX really the framework */
143 #endif
144 #if NRND > 0
145 #include <sys/rnd.h>
146 #endif
147 #ifndef PIPE_SOCKETPAIR
148 #include <sys/pipe.h>
149 #endif
150 #ifdef LKM
151 #include <sys/lkm.h>
152 #endif
153 #ifdef VERIFIED_EXEC
154 #include <sys/verified_exec.h>
155 #endif
156 
157 #include <sys/syscall.h>
158 #include <sys/sa.h>
159 #include <sys/syscallargs.h>
160 
161 #include <ufs/ufs/quota.h>
162 
163 #include <miscfs/genfs/genfs.h>
164 #include <miscfs/syncfs/syncfs.h>
165 
166 #include <machine/cpu.h>
167 
168 #include <uvm/uvm.h>
169 
170 #include <dev/cons.h>
171 
172 #include <net/if.h>
173 #include <net/raw_cb.h>
174 
175 /* Components of the first process -- never freed. */
176 struct	session session0;
177 struct	pgrp pgrp0;
178 struct	proc proc0;
179 struct	lwp lwp0;
180 struct	pcred cred0;
181 struct	filedesc0 filedesc0;
182 struct	cwdinfo cwdi0;
183 struct	plimit limit0;
184 struct	pstats pstat0;
185 struct	vmspace vmspace0;
186 struct	sigacts sigacts0;
187 #ifndef curlwp
188 struct	lwp *curlwp = &lwp0;
189 #endif
190 struct	proc *initproc;
191 
192 int	nofile = NOFILE;
193 int	maxuprc = MAXUPRC;
194 int	cmask = CMASK;
195 extern	struct user *proc0paddr;
196 
197 struct	vnode *rootvp, *swapdev_vp;
198 int	boothowto;
199 int	cold = 1;			/* still working on startup */
200 struct	timeval boottime;
201 time_t	rootfstime;			/* recorded root fs time, if known */
202 
203 __volatile int start_init_exec;		/* semaphore for start_init() */
204 
205 static void check_console(struct proc *p);
206 static void start_init(void *);
207 void main(void);
208 
209 extern const struct emul emul_netbsd;	/* defined in kern_exec.c */
210 
211 /*
212  * System startup; initialize the world, create process 0, mount root
213  * filesystem, and fork to create init and pagedaemon.  Most of the
214  * hard work is done in the lower-level initialization routines including
215  * startup(), which does memory initialization and autoconfiguration.
216  */
217 void
218 main(void)
219 {
220 	struct lwp *l;
221 	struct proc *p;
222 	struct pdevinit *pdev;
223 	int s, error;
224 	u_int i;
225 	rlim_t lim;
226 	extern struct pdevinit pdevinit[];
227 	extern void schedcpu(void *);
228 #if defined(NFSSERVER) || defined(NFS)
229 	extern void nfs_init(void);
230 #endif
231 #ifdef NVNODE_IMPLICIT
232 	int usevnodes;
233 #endif
234 
235 	/*
236 	 * Initialize the current LWP pointer (curlwp) before
237 	 * any possible traps/probes to simplify trap processing.
238 	 */
239 	l = &lwp0;
240 	curlwp = l;
241 	l->l_cpu = curcpu();
242 	l->l_proc = &proc0;
243 	l->l_lid = 1;
244 
245 	/*
246 	 * Attempt to find console and initialize
247 	 * in case of early panic or other messages.
248 	 */
249 	consinit();
250 
251 	KERNEL_LOCK_INIT();
252 
253 	uvm_init();
254 
255 	/* Do machine-dependent initialization. */
256 	cpu_startup();
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 	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 
432 #ifdef VERIFIED_EXEC
433 	  /*
434 	   * Initialise the fingerprint operations vectors before
435 	   * fingerprints can be loaded.
436 	   */
437 	veriexec_init_fp_ops();
438 #endif
439 
440 	/* Attach pseudo-devices. */
441 	for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++)
442 		(*pdev->pdev_attach)(pdev->pdev_count);
443 
444 #ifdef	FAST_IPSEC
445 	/* Attach network crypto subsystem */
446 	ipsec_attach();
447 #endif
448 
449 	/*
450 	 * Initialize protocols.  Block reception of incoming packets
451 	 * until everything is ready.
452 	 */
453 	s = splnet();
454 	ifinit();
455 	domaininit();
456 	if_attachdomain();
457 	splx(s);
458 
459 #ifdef GPROF
460 	/* Initialize kernel profiling. */
461 	kmstartup();
462 #endif
463 
464 	/* Initialize system accouting. */
465 	acct_init();
466 
467 #ifdef SYSTRACE
468 	systrace_init();
469 #endif
470 	/*
471 	 * Initialize signal-related data structures, and signal state
472 	 * for proc0.
473 	 */
474 	signal_init();
475 	p->p_sigacts = &sigacts0;
476 	siginit(p);
477 
478 	/* Kick off timeout driven events by calling first time. */
479 	schedcpu(NULL);
480 
481 	/*
482 	 * Create process 1 (init(8)).  We do this now, as Unix has
483 	 * historically had init be process 1, and changing this would
484 	 * probably upset a lot of people.
485 	 *
486 	 * Note that process 1 won't immediately exec init(8), but will
487 	 * wait for us to inform it that the root file system has been
488 	 * mounted.
489 	 */
490 	if (fork1(l, 0, SIGCHLD, NULL, 0, start_init, NULL, NULL, &initproc))
491 		panic("fork init");
492 
493 	/*
494 	 * Create any kernel threads who's creation was deferred because
495 	 * initproc had not yet been created.
496 	 */
497 	kthread_run_deferred_queue();
498 
499 	/*
500 	 * Now that device driver threads have been created, wait for
501 	 * them to finish any deferred autoconfiguration.  Note we don't
502 	 * need to lock this semaphore, since we haven't booted any
503 	 * secondary processors, yet.
504 	 */
505 	while (config_pending)
506 		(void) tsleep(&config_pending, PWAIT, "cfpend", 0);
507 
508 	/*
509 	 * Finalize configuration now that all real devices have been
510 	 * found.  This needs to be done before the root device is
511 	 * selected, since finalization may create the root device.
512 	 */
513 	config_finalize();
514 
515 	/*
516 	 * Now that autoconfiguration has completed, we can determine
517 	 * the root and dump devices.
518 	 */
519 	cpu_rootconf();
520 	cpu_dumpconf();
521 
522 	/* Mount the root file system. */
523 	do {
524 		domountroothook();
525 		if ((error = vfs_mountroot())) {
526 			printf("cannot mount root, error = %d\n", error);
527 			boothowto |= RB_ASKNAME;
528 			setroot(root_device,
529 			    (rootdev != NODEV) ? DISKPART(rootdev) : 0);
530 		}
531 	} while (error != 0);
532 	mountroothook_destroy();
533 
534 	/*
535 	 * Initialise the time-of-day clock, passing the time recorded
536 	 * in the root filesystem (if any) for use by systems that
537 	 * don't have a non-volatile time-of-day device.
538 	 */
539 	inittodr(rootfstime);
540 
541 	CIRCLEQ_FIRST(&mountlist)->mnt_flag |= MNT_ROOTFS;
542 #ifdef ROOTFS_MAGICLINKS
543 	CIRCLEQ_FIRST(&mountlist)->mnt_flag |= MNT_MAGICLINKS;
544 #endif
545 	CIRCLEQ_FIRST(&mountlist)->mnt_op->vfs_refcount++;
546 
547 	/*
548 	 * Get the vnode for '/'.  Set filedesc0.fd_fd.fd_cdir to
549 	 * reference it.
550 	 */
551 	error = VFS_ROOT(CIRCLEQ_FIRST(&mountlist), &rootvnode);
552 	if (error)
553 		panic("cannot find root vnode, error=%d", error);
554 	cwdi0.cwdi_cdir = rootvnode;
555 	VREF(cwdi0.cwdi_cdir);
556 	VOP_UNLOCK(rootvnode, 0);
557 	cwdi0.cwdi_rdir = NULL;
558 
559 	/*
560 	 * Now that root is mounted, we can fixup initproc's CWD
561 	 * info.  All other processes are kthreads, which merely
562 	 * share proc0's CWD info.
563 	 */
564 	initproc->p_cwdi->cwdi_cdir = rootvnode;
565 	VREF(initproc->p_cwdi->cwdi_cdir);
566 	initproc->p_cwdi->cwdi_rdir = NULL;
567 
568 	/*
569 	 * Now can look at time, having had a chance to verify the time
570 	 * from the file system.  Reset p->p_rtime as it may have been
571 	 * munched in mi_switch() after the time got set.
572 	 */
573 	proclist_lock_read();
574 	s = splsched();
575 	LIST_FOREACH(p, &allproc, p_list) {
576 		KASSERT((p->p_flag & P_MARKER) == 0);
577 		p->p_stats->p_start = mono_time = boottime = time;
578 		LIST_FOREACH(l, &p->p_lwps, l_sibling) {
579 			if (l->l_cpu != NULL)
580 				l->l_cpu->ci_schedstate.spc_runtime = time;
581 		}
582 		p->p_rtime.tv_sec = p->p_rtime.tv_usec = 0;
583 	}
584 	splx(s);
585 	proclist_unlock_read();
586 
587 	/* Create the pageout daemon kernel thread. */
588 	uvm_swap_init();
589 	if (kthread_create1(uvm_pageout, NULL, NULL, "pagedaemon"))
590 		panic("fork pagedaemon");
591 
592 	/* Create the filesystem syncer kernel thread. */
593 	if (kthread_create1(sched_sync, NULL, NULL, "ioflush"))
594 		panic("fork syncer");
595 
596 	/* Create the aiodone daemon kernel thread. */
597 	if (kthread_create1(uvm_aiodone_daemon, NULL, &uvm.aiodoned_proc,
598 	    "aiodoned"))
599 		panic("fork aiodoned");
600 
601 #if defined(MULTIPROCESSOR)
602 	/* Boot the secondary processors. */
603 	cpu_boot_secondary_processors();
604 #endif
605 
606 	/* Initialize exec structures */
607 	exec_init(1);
608 
609 	/*
610 	 * Okay, now we can let init(8) exec!  It's off to userland!
611 	 */
612 	start_init_exec = 1;
613 	wakeup(&start_init_exec);
614 
615 	/* The scheduler is an infinite loop. */
616 	uvm_scheduler();
617 	/* NOTREACHED */
618 }
619 
620 void
621 setrootfstime(time_t t)
622 {
623 
624 	rootfstime = t;
625 }
626 
627 static void
628 check_console(struct proc *p)
629 {
630 	struct nameidata nd;
631 	int error;
632 
633 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/console", p);
634 	error = namei(&nd);
635 	if (error == 0)
636 		vrele(nd.ni_vp);
637 	else if (error == ENOENT)
638 		printf("warning: no /dev/console\n");
639 	else
640 		printf("warning: lookup /dev/console: error %d\n", error);
641 }
642 
643 /*
644  * List of paths to try when searching for "init".
645  */
646 static const char *initpaths[] = {
647 	"/sbin/init",
648 	"/sbin/oinit",
649 	"/sbin/init.bak",
650 	NULL,
651 };
652 
653 /*
654  * Start the initial user process; try exec'ing each pathname in "initpaths".
655  * The program is invoked with one argument containing the boot flags.
656  */
657 static void
658 start_init(void *arg)
659 {
660 	struct lwp *l = arg;
661 	struct proc *p = l->l_proc;
662 	vaddr_t addr;
663 	struct sys_execve_args /* {
664 		syscallarg(const char *) path;
665 		syscallarg(char * const *) argp;
666 		syscallarg(char * const *) envp;
667 	} */ args;
668 	int options, i, error;
669 	register_t retval[2];
670 	char flags[4], *flagsp;
671 	const char *path, *slash;
672 	char *ucp, **uap, *arg0, *arg1 = NULL;
673 	char ipath[129];
674 	int ipx, len;
675 
676 	/*
677 	 * Now in process 1.
678 	 */
679 	strncpy(p->p_comm, "init", MAXCOMLEN);
680 
681 	/*
682 	 * Wait for main() to tell us that it's safe to exec.
683 	 */
684 	while (start_init_exec == 0)
685 		(void) tsleep(&start_init_exec, PWAIT, "initexec", 0);
686 
687 	/*
688 	 * This is not the right way to do this.  We really should
689 	 * hand-craft a descriptor onto /dev/console to hand to init,
690 	 * but that's a _lot_ more work, and the benefit from this easy
691 	 * hack makes up for the "good is the enemy of the best" effect.
692 	 */
693 	check_console(p);
694 
695 	/*
696 	 * Need just enough stack to hold the faked-up "execve()" arguments.
697 	 */
698 	addr = (vaddr_t)STACK_ALLOC(USRSTACK, PAGE_SIZE);
699 	if (uvm_map(&p->p_vmspace->vm_map, &addr, PAGE_SIZE,
700                     NULL, UVM_UNKNOWN_OFFSET, 0,
701                     UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY,
702 		    UVM_ADV_NORMAL,
703                     UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW)) != 0)
704 		panic("init: couldn't allocate argument space");
705 	p->p_vmspace->vm_maxsaddr = (caddr_t)STACK_MAX(addr, PAGE_SIZE);
706 
707 	ipx = 0;
708 	while (1) {
709 		if (boothowto & RB_ASKNAME) {
710 			printf("init path");
711 			if (initpaths[ipx])
712 				printf(" (default %s)", initpaths[ipx]);
713 			printf(": ");
714 			len = cngetsn(ipath, sizeof(ipath)-1);
715 			if (len == 0) {
716 				if (initpaths[ipx])
717 					path = initpaths[ipx++];
718 				else
719 					continue;
720 			} else {
721 				ipath[len] = '\0';
722 				path = ipath;
723 			}
724 		} else {
725 			if ((path = initpaths[ipx++]) == NULL)
726 				break;
727 		}
728 
729 		ucp = (char *)USRSTACK;
730 
731 		/*
732 		 * Construct the boot flag argument.
733 		 */
734 		flagsp = flags;
735 		*flagsp++ = '-';
736 		options = 0;
737 
738 		if (boothowto & RB_SINGLE) {
739 			*flagsp++ = 's';
740 			options = 1;
741 		}
742 #ifdef notyet
743 		if (boothowto & RB_FASTBOOT) {
744 			*flagsp++ = 'f';
745 			options = 1;
746 		}
747 #endif
748 
749 		/*
750 		 * Move out the flags (arg 1), if necessary.
751 		 */
752 		if (options != 0) {
753 			*flagsp++ = '\0';
754 			i = flagsp - flags;
755 #ifdef DEBUG
756 			printf("init: copying out flags `%s' %d\n", flags, i);
757 #endif
758 			arg1 = STACK_ALLOC(ucp, i);
759 			ucp = STACK_MAX(arg1, i);
760 			(void)copyout((caddr_t)flags, arg1, i);
761 		}
762 
763 		/*
764 		 * Move out the file name (also arg 0).
765 		 */
766 		i = strlen(path) + 1;
767 #ifdef DEBUG
768 		printf("init: copying out path `%s' %d\n", path, i);
769 #else
770 		if (boothowto & RB_ASKNAME || path != initpaths[0])
771 			printf("init: trying %s\n", path);
772 #endif
773 		arg0 = STACK_ALLOC(ucp, i);
774 		ucp = STACK_MAX(arg0, i);
775 		(void)copyout(path, arg0, i);
776 
777 		/*
778 		 * Move out the arg pointers.
779 		 */
780 		ucp = (caddr_t)STACK_ALIGN(ucp, ALIGNBYTES);
781 		uap = (char **)STACK_ALLOC(ucp, sizeof(char *) * 3);
782 		SCARG(&args, path) = arg0;
783 		SCARG(&args, argp) = uap;
784 		SCARG(&args, envp) = NULL;
785 		slash = strrchr(path, '/');
786 		if (slash)
787 			(void)suword((caddr_t)uap++,
788 			    (long)arg0 + (slash + 1 - path));
789 		else
790 			(void)suword((caddr_t)uap++, (long)arg0);
791 		if (options != 0)
792 			(void)suword((caddr_t)uap++, (long)arg1);
793 		(void)suword((caddr_t)uap++, 0);	/* terminator */
794 
795 		/*
796 		 * Now try to exec the program.  If can't for any reason
797 		 * other than it doesn't exist, complain.
798 		 */
799 		error = sys_execve(LIST_FIRST(&p->p_lwps), &args, retval);
800 		if (error == 0 || error == EJUSTRETURN) {
801 			KERNEL_PROC_UNLOCK(l);
802 			return;
803 		}
804 		printf("exec %s: error %d\n", path, error);
805 	}
806 	printf("init: not found\n");
807 	panic("no init");
808 }
809