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