xref: /netbsd-src/sys/kern/init_main.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: init_main.c,v 1.331 2007/12/08 19:29:46 pooka 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.331 2007/12/08 19:29:46 pooka Exp $");
75 
76 #include "opt_ipsec.h"
77 #include "opt_ntp.h"
78 #include "opt_pipe.h"
79 #include "opt_posix.h"
80 #include "opt_syscall_debug.h"
81 #include "opt_sysv.h"
82 #include "opt_systrace.h"
83 #include "opt_fileassoc.h"
84 #include "opt_ktrace.h"
85 #include "opt_pax.h"
86 
87 #include "rnd.h"
88 #include "sysmon_envsys.h"
89 #include "sysmon_power.h"
90 #include "sysmon_taskq.h"
91 #include "sysmon_wdog.h"
92 #include "veriexec.h"
93 
94 #include <sys/param.h>
95 #include <sys/acct.h>
96 #include <sys/filedesc.h>
97 #include <sys/file.h>
98 #include <sys/errno.h>
99 #include <sys/callout.h>
100 #include <sys/cpu.h>
101 #include <sys/kernel.h>
102 #include <sys/kmem.h>
103 #include <sys/mount.h>
104 #include <sys/proc.h>
105 #include <sys/kthread.h>
106 #include <sys/resourcevar.h>
107 #include <sys/signalvar.h>
108 #include <sys/systm.h>
109 #include <sys/vnode.h>
110 #include <sys/fstrans.h>
111 #include <sys/tty.h>
112 #include <sys/conf.h>
113 #include <sys/disklabel.h>
114 #include <sys/buf.h>
115 #include <sys/device.h>
116 #include <sys/exec.h>
117 #include <sys/socketvar.h>
118 #include <sys/protosw.h>
119 #include <sys/reboot.h>
120 #include <sys/user.h>
121 #include <sys/sysctl.h>
122 #include <sys/event.h>
123 #include <sys/mbuf.h>
124 #include <sys/sched.h>
125 #include <sys/sleepq.h>
126 #include <sys/iostat.h>
127 #include <sys/vmem.h>
128 #include <sys/uuid.h>
129 #include <sys/extent.h>
130 #include <sys/disk.h>
131 #include <sys/mqueue.h>
132 #include <sys/msgbuf.h>
133 #ifdef FAST_IPSEC
134 #include <netipsec/ipsec.h>
135 #endif
136 #ifdef SYSVSHM
137 #include <sys/shm.h>
138 #endif
139 #ifdef SYSVSEM
140 #include <sys/sem.h>
141 #endif
142 #ifdef SYSVMSG
143 #include <sys/msg.h>
144 #endif
145 #ifdef SYSTRACE
146 #include <sys/systrace.h>
147 #endif
148 #ifdef P1003_1B_SEMAPHORE
149 #include <sys/ksem.h>
150 #endif
151 #include <sys/domain.h>
152 #include <sys/namei.h>
153 #if NRND > 0
154 #include <sys/rnd.h>
155 #endif
156 #include <sys/pipe.h>
157 #ifdef LKM
158 #include <sys/lkm.h>
159 #endif
160 #if NVERIEXEC > 0
161 #include <sys/verified_exec.h>
162 #endif /* NVERIEXEC > 0 */
163 #ifdef KTRACE
164 #include <sys/ktrace.h>
165 #endif
166 #include <sys/kauth.h>
167 #include <net80211/ieee80211_netbsd.h>
168 
169 #include <sys/syscall.h>
170 #include <sys/syscallargs.h>
171 
172 #if defined(PAX_MPROTECT) || defined(PAX_SEGVGUARD)
173 #include <sys/pax.h>
174 #endif /* PAX_MPROTECT || PAX_SEGVGUARD */
175 #include <ufs/ufs/quota.h>
176 
177 #include <miscfs/genfs/genfs.h>
178 #include <miscfs/syncfs/syncfs.h>
179 
180 #include <sys/cpu.h>
181 
182 #include <uvm/uvm.h>
183 
184 #if NSYSMON_TASKQ > 0
185 #include <dev/sysmon/sysmon_taskq.h>
186 #endif
187 
188 #include <dev/cons.h>
189 
190 #if NSYSMON_ENVSYS > 0 || NSYSMON_POWER > 0 || NSYSMON_WDOG > 0
191 #include <dev/sysmon/sysmonvar.h>
192 #endif
193 
194 #include <net/if.h>
195 #include <net/raw_cb.h>
196 
197 #include <secmodel/secmodel.h>
198 
199 extern struct proc proc0;
200 extern struct lwp lwp0;
201 extern struct cwdinfo cwdi0;
202 extern time_t rootfstime;
203 
204 #ifndef curlwp
205 struct	lwp *curlwp = &lwp0;
206 #endif
207 struct	proc *initproc;
208 
209 struct	vnode *rootvp, *swapdev_vp;
210 int	boothowto;
211 int	cold = 1;			/* still working on startup */
212 struct timeval boottime;	        /* time at system startup - will only follow settime deltas */
213 
214 volatile int start_init_exec;		/* semaphore for start_init() */
215 
216 static void check_console(struct lwp *l);
217 static void start_init(void *);
218 void main(void);
219 
220 #if defined(__SSP__) || defined(__SSP_ALL__)
221 long __stack_chk_guard[8] = {0, 0, 0, 0, 0, 0, 0, 0};
222 void __stack_chk_fail(void);
223 
224 void
225 __stack_chk_fail(void)
226 {
227 	panic("stack overflow detected; terminated");
228 }
229 #endif
230 
231 void __secmodel_none(void);
232 __weak_alias(secmodel_start,__secmodel_none);
233 void
234 __secmodel_none(void)
235 {
236 	return;
237 }
238 
239 /*
240  * System startup; initialize the world, create process 0, mount root
241  * filesystem, and fork to create init and pagedaemon.  Most of the
242  * hard work is done in the lower-level initialization routines including
243  * startup(), which does memory initialization and autoconfiguration.
244  */
245 void
246 main(void)
247 {
248 #ifdef __HAVE_TIMECOUNTER
249 	struct timeval time;
250 #endif
251 	struct lwp *l;
252 	struct proc *p;
253 	struct pdevinit *pdev;
254 	int s, error;
255 	extern struct pdevinit pdevinit[];
256 #ifdef NVNODE_IMPLICIT
257 	int usevnodes;
258 #endif
259 	CPU_INFO_ITERATOR cii;
260 	struct cpu_info *ci;
261 
262 	l = &lwp0;
263 #ifndef LWP0_CPU_INFO
264 	l->l_cpu = curcpu();
265 #endif
266 
267 	/*
268 	 * XXX This is a temporary check to be removed before
269 	 * NetBSD 5.0 is released.
270 	 */
271 #if !defined(__i386__ ) && !defined(__x86_64__)
272 	if (curlwp != l) {
273 		printf("NOTICE: curlwp should be set before main()\n");
274 		DELAY(250000);
275 		curlwp = l;
276 	}
277 #endif
278 
279 	/*
280 	 * Attempt to find console and initialize
281 	 * in case of early panic or other messages.
282 	 */
283 	consinit();
284 
285 	kernel_lock_init();
286 
287 	uvm_init();
288 
289 	kmem_init();
290 
291 	/* Initialize the extent manager. */
292 	extent_init();
293 
294 	/* Do machine-dependent initialization. */
295 	cpu_startup();
296 
297 	/* Initialize callouts, part 1. */
298 	callout_startup();
299 
300 	/*
301 	 * Initialize the kernel authorization subsystem and start the
302 	 * default security model, if any. We need to do this early
303 	 * enough so that subsystems relying on any of the aforementioned
304 	 * can work properly. Since the security model may dictate the
305 	 * credential inheritance policy, it is needed at least before
306 	 * any process is created, specifically proc0.
307 	 */
308 	kauth_init();
309 	secmodel_start();
310 
311 	/* Initialize the buffer cache */
312 	bufinit();
313 
314 	/*
315 	 * Initialize mbuf's.  Do this now because we might attempt to
316 	 * allocate mbufs or mbuf clusters during autoconfiguration.
317 	 */
318 	mbinit();
319 
320 	/* Initialize sockets. */
321 	soinit();
322 
323 	/*
324 	 * The following things must be done before autoconfiguration.
325 	 */
326 	evcnt_init();		/* initialize event counters */
327 #if NRND > 0
328 	rnd_init();		/* initialize RNG */
329 #endif
330 
331 	/* Initialize process and pgrp structures. */
332 	procinit();
333 	lwpinit();
334 
335 	/* Initialize signal-related data structures. */
336 	signal_init();
337 
338 	/* Create process 0 (the swapper). */
339 	proc0_init();
340 
341 	/* Initialize the UID hash table. */
342 	uid_init();
343 
344 	/* Charge root for one process. */
345 	(void)chgproccnt(0, 1);
346 
347 	/* Initialize timekeeping. */
348 	time_init();
349 
350 	/* Initialize the run queues, turnstiles and sleep queues. */
351 	mutex_init(&cpu_lock, MUTEX_DEFAULT, IPL_NONE);
352 	sched_rqinit();
353 	turnstile_init();
354 	sleeptab_init(&sleeptab);
355 
356 	/* MI initialization of the boot cpu */
357 	error = mi_cpu_attach(curcpu());
358 	KASSERT(error == 0);
359 
360 	/* Initialize the sysctl subsystem. */
361 	sysctl_init();
362 
363 	/* Initialize I/O statistics. */
364 	iostat_init();
365 
366 	/* Initialize the log device. */
367 	loginit();
368 
369 	/* Initialize the file systems. */
370 #ifdef NVNODE_IMPLICIT
371 	/*
372 	 * If maximum number of vnodes in namei vnode cache is not explicitly
373 	 * defined in kernel config, adjust the number such as we use roughly
374 	 * 1.0% of memory for vnode cache (but not less than NVNODE vnodes).
375 	 */
376 	usevnodes = (ptoa((unsigned)physmem) / 100) / sizeof(struct vnode);
377 	if (usevnodes > desiredvnodes)
378 		desiredvnodes = usevnodes;
379 #endif
380 	vfsinit();
381 
382 	/* Initialize fstrans. */
383 	fstrans_init();
384 
385 	/* Initialize the file descriptor system. */
386 	filedesc_init();
387 
388 	/* Initialize the select()/poll() system calls. */
389 	selsysinit();
390 
391 	/* Initialize asynchronous I/O. */
392 	aio_sysinit();
393 
394 	/* Initialize message queues. */
395 	mqueue_sysinit();
396 
397 	/* Initialize the system monitor subsystems. */
398 #if NSYSMON_TASKQ > 0
399 	sysmon_task_queue_preinit();
400 #endif
401 
402 #if NSYSMON_ENVSYS > 0
403 	sysmon_envsys_init();
404 #endif
405 
406 #if NSYSMON_POWER > 0
407 	sysmon_power_init();
408 #endif
409 
410 #if NSYSMON_WDOG > 0
411 	sysmon_wdog_init();
412 #endif
413 
414 #ifdef __HAVE_TIMECOUNTER
415 	inittimecounter();
416 	ntp_init();
417 #endif /* __HAVE_TIMECOUNTER */
418 
419 	/* Initialize the device switch tables. */
420 	devsw_init();
421 
422 	/* Initialize tty subsystem. */
423 	tty_init();
424 	ttyldisc_init();
425 
426 	/* Initialize the disk wedge subsystem. */
427 	dkwedge_init();
428 
429 	/* Configure the system hardware.  This will enable interrupts. */
430 	configure();
431 
432 #if defined(__SSP__) || defined(__SSP_ALL__)
433 	{
434 #ifdef DIAGNOSTIC
435 		printf("Initializing SSP:");
436 #endif
437 		/*
438 		 * We initialize ssp here carefully:
439 		 *	1. after we got some entropy
440 		 *	2. without calling a function
441 		 */
442 		size_t i;
443 		long guard[__arraycount(__stack_chk_guard)];
444 
445 		arc4randbytes(guard, sizeof(guard));
446 		for (i = 0; i < __arraycount(guard); i++)
447 			__stack_chk_guard[i] = guard[i];
448 #ifdef DIAGNOSTIC
449 		for (i = 0; i < __arraycount(guard); i++)
450 			printf("%lx ", guard[i]);
451 		printf("\n");
452 #endif
453 	}
454 #endif
455 	ubc_init();		/* must be after autoconfig */
456 
457 	/* Lock the kernel on behalf of proc0. */
458 	KERNEL_LOCK(1, l);
459 
460 #ifdef SYSTRACE
461 	systrace_init();
462 #endif
463 
464 #ifdef SYSVSHM
465 	/* Initialize System V style shared memory. */
466 	shminit();
467 #endif
468 
469 #ifdef SYSVSEM
470 	/* Initialize System V style semaphores. */
471 	seminit();
472 #endif
473 
474 #ifdef SYSVMSG
475 	/* Initialize System V style message queues. */
476 	msginit();
477 #endif
478 
479 #ifdef P1003_1B_SEMAPHORE
480 	/* Initialize posix semaphores */
481 	ksem_init();
482 #endif
483 
484 #if NVERIEXEC > 0
485 	/*
486 	 * Initialise the Veriexec subsystem.
487 	 */
488 	veriexec_init();
489 #endif /* NVERIEXEC > 0 */
490 
491 #if defined(PAX_MPROTECT) || defined(PAX_SEGVGUARD)
492 	pax_init();
493 #endif /* PAX_MPROTECT || PAX_SEGVGUARD */
494 
495 	/* Attach pseudo-devices. */
496 	for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++)
497 		(*pdev->pdev_attach)(pdev->pdev_count);
498 
499 #ifdef	FAST_IPSEC
500 	/* Attach network crypto subsystem */
501 	ipsec_attach();
502 #endif
503 
504 	/*
505 	 * Initialize protocols.  Block reception of incoming packets
506 	 * until everything is ready.
507 	 */
508 	s = splnet();
509 	ifinit();
510 	domaininit();
511 	if_attachdomain();
512 	splx(s);
513 
514 #ifdef GPROF
515 	/* Initialize kernel profiling. */
516 	kmstartup();
517 #endif
518 
519 	/* Initialize system accouting. */
520 	acct_init();
521 
522 #ifndef PIPE_SOCKETPAIR
523 	/* Initialize pipes. */
524 	pipe_init();
525 #endif
526 
527 	/* Setup the scheduler */
528 	sched_init();
529 
530 #ifdef KTRACE
531 	/* Initialize ktrace. */
532 	ktrinit();
533 #endif
534 
535 	/* Initialize the UUID system calls. */
536 	uuid_init();
537 
538 	/*
539 	 * Create process 1 (init(8)).  We do this now, as Unix has
540 	 * historically had init be process 1, and changing this would
541 	 * probably upset a lot of people.
542 	 *
543 	 * Note that process 1 won't immediately exec init(8), but will
544 	 * wait for us to inform it that the root file system has been
545 	 * mounted.
546 	 */
547 	if (fork1(l, 0, SIGCHLD, NULL, 0, start_init, NULL, NULL, &initproc))
548 		panic("fork init");
549 
550 	/*
551 	 * Now that device driver threads have been created, wait for
552 	 * them to finish any deferred autoconfiguration.
553 	 */
554 	while (config_pending)
555 		(void) tsleep(&config_pending, PWAIT, "cfpend", hz);
556 
557 	/*
558 	 * Finalize configuration now that all real devices have been
559 	 * found.  This needs to be done before the root device is
560 	 * selected, since finalization may create the root device.
561 	 */
562 	config_finalize();
563 
564 	/*
565 	 * Now that autoconfiguration has completed, we can determine
566 	 * the root and dump devices.
567 	 */
568 	cpu_rootconf();
569 	cpu_dumpconf();
570 
571 	/* Mount the root file system. */
572 	do {
573 		domountroothook();
574 		if ((error = vfs_mountroot())) {
575 			printf("cannot mount root, error = %d\n", error);
576 			boothowto |= RB_ASKNAME;
577 			setroot(root_device,
578 			    (rootdev != NODEV) ? DISKPART(rootdev) : 0);
579 		}
580 	} while (error != 0);
581 	mountroothook_destroy();
582 
583 	/*
584 	 * Initialise the time-of-day clock, passing the time recorded
585 	 * in the root filesystem (if any) for use by systems that
586 	 * don't have a non-volatile time-of-day device.
587 	 */
588 	inittodr(rootfstime);
589 
590 	CIRCLEQ_FIRST(&mountlist)->mnt_flag |= MNT_ROOTFS;
591 	CIRCLEQ_FIRST(&mountlist)->mnt_op->vfs_refcount++;
592 
593 	/*
594 	 * Get the vnode for '/'.  Set filedesc0.fd_fd.fd_cdir to
595 	 * reference it.
596 	 */
597 	error = VFS_ROOT(CIRCLEQ_FIRST(&mountlist), &rootvnode);
598 	if (error)
599 		panic("cannot find root vnode, error=%d", error);
600 	cwdi0.cwdi_cdir = rootvnode;
601 	VREF(cwdi0.cwdi_cdir);
602 	VOP_UNLOCK(rootvnode, 0);
603 	cwdi0.cwdi_rdir = NULL;
604 
605 	/*
606 	 * Now that root is mounted, we can fixup initproc's CWD
607 	 * info.  All other processes are kthreads, which merely
608 	 * share proc0's CWD info.
609 	 */
610 	initproc->p_cwdi->cwdi_cdir = rootvnode;
611 	VREF(initproc->p_cwdi->cwdi_cdir);
612 	initproc->p_cwdi->cwdi_rdir = NULL;
613 
614 	/*
615 	 * Now can look at time, having had a chance to verify the time
616 	 * from the file system.  Reset l->l_rtime as it may have been
617 	 * munched in mi_switch() after the time got set.
618 	 */
619 #ifdef __HAVE_TIMECOUNTER
620 	getmicrotime(&time);
621 #else
622 	mono_time = time;
623 #endif
624 	boottime = time;
625 	mutex_enter(&proclist_lock);
626 	LIST_FOREACH(p, &allproc, p_list) {
627 		KASSERT((p->p_flag & PK_MARKER) == 0);
628 		mutex_enter(&p->p_smutex);
629 		p->p_stats->p_start = time;
630 		LIST_FOREACH(l, &p->p_lwps, l_sibling) {
631 			lwp_lock(l);
632 			l->l_rtime.tv_sec = l->l_rtime.tv_usec = 0;
633 			lwp_unlock(l);
634 		}
635 		mutex_exit(&p->p_smutex);
636 	}
637 	mutex_exit(&proclist_lock);
638 	curlwp->l_stime = time;
639 
640 	for (CPU_INFO_FOREACH(cii, ci)) {
641 		ci->ci_schedstate.spc_lastmod = time_second;
642 	}
643 
644 	/* Create the pageout daemon kernel thread. */
645 	uvm_swap_init();
646 	if (kthread_create(PRI_PGDAEMON, 0, NULL, uvm_pageout,
647 	    NULL, NULL, "pgdaemon"))
648 		panic("fork pagedaemon");
649 
650 	/* Create the filesystem syncer kernel thread. */
651 	if (kthread_create(PRI_IOFLUSH, 0, NULL, sched_sync, NULL, NULL, "ioflush"))
652 		panic("fork syncer");
653 
654 	/* Create the aiodone daemon kernel thread. */
655 	if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
656 	    uvm_aiodone_worker, NULL, PRI_VM, IPL_BIO, 0))
657 		panic("fork aiodoned");
658 
659 	vmem_rehash_start();
660 
661 	/* Initialize exec structures */
662 	exec_init(1);
663 
664 	/*
665 	 * Okay, now we can let init(8) exec!  It's off to userland!
666 	 */
667 	start_init_exec = 1;
668 	wakeup(&start_init_exec);
669 
670 	/* The scheduler is an infinite loop. */
671 	uvm_scheduler();
672 	/* NOTREACHED */
673 }
674 
675 static void
676 check_console(struct lwp *l)
677 {
678 	struct nameidata nd;
679 	int error;
680 
681 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/console");
682 	error = namei(&nd);
683 	if (error == 0)
684 		vrele(nd.ni_vp);
685 	else if (error == ENOENT)
686 		printf("warning: no /dev/console\n");
687 	else
688 		printf("warning: lookup /dev/console: error %d\n", error);
689 }
690 
691 /*
692  * List of paths to try when searching for "init".
693  */
694 static const char * const initpaths[] = {
695 	"/sbin/init",
696 	"/sbin/oinit",
697 	"/sbin/init.bak",
698 	NULL,
699 };
700 
701 /*
702  * Start the initial user process; try exec'ing each pathname in "initpaths".
703  * The program is invoked with one argument containing the boot flags.
704  */
705 static void
706 start_init(void *arg)
707 {
708 	struct lwp *l = arg;
709 	struct proc *p = l->l_proc;
710 	vaddr_t addr;
711 	struct sys_execve_args /* {
712 		syscallarg(const char *) path;
713 		syscallarg(char * const *) argp;
714 		syscallarg(char * const *) envp;
715 	} */ args;
716 	int options, i, error;
717 	register_t retval[2];
718 	char flags[4], *flagsp;
719 	const char *path, *slash;
720 	char *ucp, **uap, *arg0, *arg1 = NULL;
721 	char ipath[129];
722 	int ipx, len;
723 
724 	/*
725 	 * Now in process 1.
726 	 */
727 	strncpy(p->p_comm, "init", MAXCOMLEN);
728 
729 	/*
730 	 * Wait for main() to tell us that it's safe to exec.
731 	 */
732 	while (start_init_exec == 0)
733 		(void) tsleep(&start_init_exec, PWAIT, "initexec", 0);
734 
735 	/*
736 	 * This is not the right way to do this.  We really should
737 	 * hand-craft a descriptor onto /dev/console to hand to init,
738 	 * but that's a _lot_ more work, and the benefit from this easy
739 	 * hack makes up for the "good is the enemy of the best" effect.
740 	 */
741 	check_console(l);
742 
743 	/*
744 	 * Need just enough stack to hold the faked-up "execve()" arguments.
745 	 */
746 	addr = (vaddr_t)STACK_ALLOC(USRSTACK, PAGE_SIZE);
747 	if (uvm_map(&p->p_vmspace->vm_map, &addr, PAGE_SIZE,
748                     NULL, UVM_UNKNOWN_OFFSET, 0,
749                     UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY,
750 		    UVM_ADV_NORMAL,
751                     UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW)) != 0)
752 		panic("init: couldn't allocate argument space");
753 	p->p_vmspace->vm_maxsaddr = (void *)STACK_MAX(addr, PAGE_SIZE);
754 
755 	ipx = 0;
756 	while (1) {
757 		if (boothowto & RB_ASKNAME) {
758 			printf("init path");
759 			if (initpaths[ipx])
760 				printf(" (default %s)", initpaths[ipx]);
761 			printf(": ");
762 			len = cngetsn(ipath, sizeof(ipath)-1);
763 			if (len == 0) {
764 				if (initpaths[ipx])
765 					path = initpaths[ipx++];
766 				else
767 					continue;
768 			} else {
769 				ipath[len] = '\0';
770 				path = ipath;
771 			}
772 		} else {
773 			if ((path = initpaths[ipx++]) == NULL)
774 				break;
775 		}
776 
777 		ucp = (char *)USRSTACK;
778 
779 		/*
780 		 * Construct the boot flag argument.
781 		 */
782 		flagsp = flags;
783 		*flagsp++ = '-';
784 		options = 0;
785 
786 		if (boothowto & RB_SINGLE) {
787 			*flagsp++ = 's';
788 			options = 1;
789 		}
790 #ifdef notyet
791 		if (boothowto & RB_FASTBOOT) {
792 			*flagsp++ = 'f';
793 			options = 1;
794 		}
795 #endif
796 
797 		/*
798 		 * Move out the flags (arg 1), if necessary.
799 		 */
800 		if (options != 0) {
801 			*flagsp++ = '\0';
802 			i = flagsp - flags;
803 #ifdef DEBUG
804 			printf("init: copying out flags `%s' %d\n", flags, i);
805 #endif
806 			arg1 = STACK_ALLOC(ucp, i);
807 			ucp = STACK_MAX(arg1, i);
808 			(void)copyout((void *)flags, arg1, i);
809 		}
810 
811 		/*
812 		 * Move out the file name (also arg 0).
813 		 */
814 		i = strlen(path) + 1;
815 #ifdef DEBUG
816 		printf("init: copying out path `%s' %d\n", path, i);
817 #else
818 		if (boothowto & RB_ASKNAME || path != initpaths[0])
819 			printf("init: trying %s\n", path);
820 #endif
821 		arg0 = STACK_ALLOC(ucp, i);
822 		ucp = STACK_MAX(arg0, i);
823 		(void)copyout(path, arg0, i);
824 
825 		/*
826 		 * Move out the arg pointers.
827 		 */
828 		ucp = (void *)STACK_ALIGN(ucp, ALIGNBYTES);
829 		uap = (char **)STACK_ALLOC(ucp, sizeof(char *) * 3);
830 		SCARG(&args, path) = arg0;
831 		SCARG(&args, argp) = uap;
832 		SCARG(&args, envp) = NULL;
833 		slash = strrchr(path, '/');
834 		if (slash)
835 			(void)suword((void *)uap++,
836 			    (long)arg0 + (slash + 1 - path));
837 		else
838 			(void)suword((void *)uap++, (long)arg0);
839 		if (options != 0)
840 			(void)suword((void *)uap++, (long)arg1);
841 		(void)suword((void *)uap++, 0);	/* terminator */
842 
843 		/*
844 		 * Now try to exec the program.  If can't for any reason
845 		 * other than it doesn't exist, complain.
846 		 */
847 		error = sys_execve(l, &args, retval);
848 		if (error == 0 || error == EJUSTRETURN) {
849 			KERNEL_UNLOCK_LAST(l);
850 			return;
851 		}
852 		printf("exec %s: error %d\n", path, error);
853 	}
854 	printf("init: not found\n");
855 	panic("no init");
856 }
857