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