Home | History | Annotate | Line # | Download | only in kern
init_main.c revision 1.228
      1 /*	$NetBSD: init_main.c,v 1.228 2003/12/30 12:33:23 pk 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.228 2003/12/30 12:33:23 pk 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 	/* 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 kqueues. */
    271 	kqueue_init();
    272 
    273 	/* Initialize sockets. */
    274 	soinit();
    275 
    276 	/*
    277 	 * The following things must be done before autoconfiguration.
    278 	 */
    279 	evcnt_init();		/* initialize event counters */
    280 	disk_init();		/* initialize disk list */
    281 	tty_init();		/* initialize tty list */
    282 #if NRND > 0
    283 	rnd_init();		/* initialize RNG */
    284 #endif
    285 #if NOPENCRYPTO > 0
    286 	/* Initialize crypto subsystem before configuring crypto hardware. */
    287 	(void)crypto_init();
    288 #endif
    289 	/* Initialize the sysctl subsystem. */
    290 	sysctl_init();
    291 
    292 	/*
    293 	 * Initialize process and pgrp structures.
    294 	 */
    295 	procinit();
    296 
    297 #ifdef LKM
    298 	/* Initialize the LKM system. */
    299 	lkm_init();
    300 #endif
    301 
    302 	/*
    303 	 * Create process 0 (the swapper).
    304 	 */
    305 	p = &proc0;
    306 	proc0_insert(p, l, &pgrp0, &session0);
    307 
    308 	/*
    309 	 * Set P_NOCLDWAIT so that kernel threads are reparented to
    310 	 * init(8) when they exit.  init(8) can easily wait them out
    311 	 * for us.
    312 	 */
    313 	p->p_flag = P_SYSTEM | P_NOCLDWAIT;
    314 	p->p_stat = SACTIVE;
    315 	p->p_nice = NZERO;
    316 	p->p_emul = &emul_netbsd;
    317 #ifdef __HAVE_SYSCALL_INTERN
    318 	(*p->p_emul->e_syscall_intern)(p);
    319 #endif
    320 	strncpy(p->p_comm, "swapper", MAXCOMLEN);
    321 
    322 	l->l_flag = L_INMEM;
    323 	l->l_stat = LSONPROC;
    324 	p->p_nrlwps = 1;
    325 
    326 	callout_init(&l->l_tsleep_ch);
    327 
    328 	/* Create credentials. */
    329 	cred0.p_refcnt = 1;
    330 	p->p_cred = &cred0;
    331 	p->p_ucred = crget();
    332 	p->p_ucred->cr_ngroups = 1;	/* group 0 */
    333 
    334 	/* Create the file descriptor table. */
    335 	finit();
    336 	p->p_fd = &filedesc0.fd_fd;
    337 	fdinit1(&filedesc0);
    338 
    339 	/* Create the CWD info. */
    340 	p->p_cwdi = &cwdi0;
    341 	cwdi0.cwdi_cmask = cmask;
    342 	cwdi0.cwdi_refcnt = 1;
    343 
    344 	/* Create the limits structures. */
    345 	p->p_limit = &limit0;
    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 	 * 0.5% of memory for vnode cache (but not less than NVNODE vnodes).
    397 	 */
    398 	usevnodes = (ptoa((unsigned)physmem) / 200) / 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 
    428 #ifdef P1003_1B_SEMAPHORE
    429 	/* Initialize posix semaphores */
    430 	ksem_init();
    431 #endif
    432 	/* Attach pseudo-devices. */
    433 	for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++)
    434 		(*pdev->pdev_attach)(pdev->pdev_count);
    435 
    436 #ifdef	FAST_IPSEC
    437 	/* Attach network crypto subsystem */
    438 	ipsec_attach();
    439 #endif
    440 
    441 	/*
    442 	 * Initialize protocols.  Block reception of incoming packets
    443 	 * until everything is ready.
    444 	 */
    445 	s = splnet();
    446 	ifinit();
    447 	domaininit();
    448 	if_attachdomain();
    449 	splx(s);
    450 
    451 #ifdef GPROF
    452 	/* Initialize kernel profiling. */
    453 	kmstartup();
    454 #endif
    455 
    456 	/* Initialize system accouting. */
    457 	acct_init();
    458 
    459 #ifdef SYSTRACE
    460 	systrace_init();
    461 #endif
    462 	/*
    463 	 * Initialize signal-related data structures, and signal state
    464 	 * for proc0.
    465 	 */
    466 	signal_init();
    467 	p->p_sigacts = &sigacts0;
    468 	siginit(p);
    469 
    470 	/* Kick off timeout driven events by calling first time. */
    471 	schedcpu(NULL);
    472 
    473 	/*
    474 	 * Create process 1 (init(8)).  We do this now, as Unix has
    475 	 * historically had init be process 1, and changing this would
    476 	 * probably upset a lot of people.
    477 	 *
    478 	 * Note that process 1 won't immediately exec init(8), but will
    479 	 * wait for us to inform it that the root file system has been
    480 	 * mounted.
    481 	 */
    482 	if (fork1(l, 0, SIGCHLD, NULL, 0, start_init, NULL, NULL, &initproc))
    483 		panic("fork init");
    484 
    485 	/*
    486 	 * Create any kernel threads who's creation was deferred because
    487 	 * initproc had not yet been created.
    488 	 */
    489 	kthread_run_deferred_queue();
    490 
    491 	/*
    492 	 * Now that device driver threads have been created, wait for
    493 	 * them to finish any deferred autoconfiguration.  Note we don't
    494 	 * need to lock this semaphore, since we haven't booted any
    495 	 * secondary processors, yet.
    496 	 */
    497 	while (config_pending)
    498 		(void) tsleep((void *)&config_pending, PWAIT, "cfpend", 0);
    499 
    500 	/*
    501 	 * Finalize configuration now that all real devices have been
    502 	 * found.  This needs to be done before the root device is
    503 	 * selected, since finalization may create the root device.
    504 	 */
    505 	config_finalize();
    506 
    507 	/*
    508 	 * Now that autoconfiguration has completed, we can determine
    509 	 * the root and dump devices.
    510 	 */
    511 	cpu_rootconf();
    512 	cpu_dumpconf();
    513 
    514 	/* Mount the root file system. */
    515 	do {
    516 		domountroothook();
    517 		if ((error = vfs_mountroot())) {
    518 			printf("cannot mount root, error = %d\n", error);
    519 			boothowto |= RB_ASKNAME;
    520 			setroot(root_device,
    521 			    (rootdev != NODEV) ? DISKPART(rootdev) : 0);
    522 		}
    523 	} while (error != 0);
    524 	mountroothook_destroy();
    525 
    526 	CIRCLEQ_FIRST(&mountlist)->mnt_flag |= MNT_ROOTFS;
    527 	CIRCLEQ_FIRST(&mountlist)->mnt_op->vfs_refcount++;
    528 
    529 	/*
    530 	 * Get the vnode for '/'.  Set filedesc0.fd_fd.fd_cdir to
    531 	 * reference it.
    532 	 */
    533 	if (VFS_ROOT(CIRCLEQ_FIRST(&mountlist), &rootvnode))
    534 		panic("cannot find root vnode");
    535 	cwdi0.cwdi_cdir = rootvnode;
    536 	VREF(cwdi0.cwdi_cdir);
    537 	VOP_UNLOCK(rootvnode, 0);
    538 	cwdi0.cwdi_rdir = NULL;
    539 
    540 	/*
    541 	 * Now that root is mounted, we can fixup initproc's CWD
    542 	 * info.  All other processes are kthreads, which merely
    543 	 * share proc0's CWD info.
    544 	 */
    545 	initproc->p_cwdi->cwdi_cdir = rootvnode;
    546 	VREF(initproc->p_cwdi->cwdi_cdir);
    547 	initproc->p_cwdi->cwdi_rdir = NULL;
    548 
    549 	/*
    550 	 * Now can look at time, having had a chance to verify the time
    551 	 * from the file system.  Reset p->p_rtime as it may have been
    552 	 * munched in mi_switch() after the time got set.
    553 	 */
    554 	proclist_lock_read();
    555 	s = splsched();
    556 	LIST_FOREACH(p, &allproc, p_list) {
    557 		p->p_stats->p_start = mono_time = boottime = time;
    558 		LIST_FOREACH(l, &p->p_lwps, l_sibling) {
    559 			if (l->l_cpu != NULL)
    560 				l->l_cpu->ci_schedstate.spc_runtime = time;
    561 		}
    562 		p->p_rtime.tv_sec = p->p_rtime.tv_usec = 0;
    563 	}
    564 	splx(s);
    565 	proclist_unlock_read();
    566 
    567 	/* Create the pageout daemon kernel thread. */
    568 	uvm_swap_init();
    569 	if (kthread_create1(uvm_pageout, NULL, NULL, "pagedaemon"))
    570 		panic("fork pagedaemon");
    571 
    572 	/* Create the process reaper kernel thread. */
    573 	if (kthread_create1(reaper, NULL, NULL, "reaper"))
    574 		panic("fork reaper");
    575 
    576 	/* Create the filesystem syncer kernel thread. */
    577 	if (kthread_create1(sched_sync, NULL, NULL, "ioflush"))
    578 		panic("fork syncer");
    579 
    580 	/* Create the aiodone daemon kernel thread. */
    581 	if (kthread_create1(uvm_aiodone_daemon, NULL, &uvm.aiodoned_proc,
    582 	    "aiodoned"))
    583 		panic("fork aiodoned");
    584 
    585 #if defined(MULTIPROCESSOR)
    586 	/* Boot the secondary processors. */
    587 	cpu_boot_secondary_processors();
    588 #endif
    589 
    590 	/* Initialize exec structures */
    591 	exec_init(1);
    592 
    593 #ifndef PIPE_SOCKETPAIR
    594 	/* Initialize pipe structures */
    595 	pipe_init();
    596 #endif
    597 
    598 	/*
    599 	 * Okay, now we can let init(8) exec!  It's off to userland!
    600 	 */
    601 	start_init_exec = 1;
    602 	wakeup((void *)&start_init_exec);
    603 
    604 	/* The scheduler is an infinite loop. */
    605 	uvm_scheduler();
    606 	/* NOTREACHED */
    607 }
    608 
    609 static void
    610 check_console(struct proc *p)
    611 {
    612 	struct nameidata nd;
    613 	int error;
    614 
    615 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/console", p);
    616 	error = namei(&nd);
    617 	if (error == 0)
    618 		vrele(nd.ni_vp);
    619 	else if (error == ENOENT)
    620 		printf("warning: no /dev/console\n");
    621 	else
    622 		printf("warning: lookup /dev/console: error %d\n", error);
    623 }
    624 
    625 /*
    626  * List of paths to try when searching for "init".
    627  */
    628 static const char *initpaths[] = {
    629 	"/sbin/init",
    630 	"/sbin/oinit",
    631 	"/sbin/init.bak",
    632 	NULL,
    633 };
    634 
    635 /*
    636  * Start the initial user process; try exec'ing each pathname in "initpaths".
    637  * The program is invoked with one argument containing the boot flags.
    638  */
    639 static void
    640 start_init(void *arg)
    641 {
    642 	struct lwp *l = arg;
    643 	struct proc *p = l->l_proc;
    644 	vaddr_t addr;
    645 	struct sys_execve_args /* {
    646 		syscallarg(const char *) path;
    647 		syscallarg(char * const *) argp;
    648 		syscallarg(char * const *) envp;
    649 	} */ args;
    650 	int options, i, error;
    651 	register_t retval[2];
    652 	char flags[4], *flagsp;
    653 	const char *path, *slash;
    654 	char *ucp, **uap, *arg0, *arg1 = NULL;
    655 	char ipath[129];
    656 	int ipx, len;
    657 
    658 	/*
    659 	 * Now in process 1.
    660 	 */
    661 	strncpy(p->p_comm, "init", MAXCOMLEN);
    662 
    663 	/*
    664 	 * Wait for main() to tell us that it's safe to exec.
    665 	 */
    666 	while (start_init_exec == 0)
    667 		(void) tsleep((void *)&start_init_exec, PWAIT, "initexec", 0);
    668 
    669 	/*
    670 	 * This is not the right way to do this.  We really should
    671 	 * hand-craft a descriptor onto /dev/console to hand to init,
    672 	 * but that's a _lot_ more work, and the benefit from this easy
    673 	 * hack makes up for the "good is the enemy of the best" effect.
    674 	 */
    675 	check_console(p);
    676 
    677 	/*
    678 	 * Need just enough stack to hold the faked-up "execve()" arguments.
    679 	 */
    680 	addr = (vaddr_t)STACK_ALLOC(USRSTACK, PAGE_SIZE);
    681 	if (uvm_map(&p->p_vmspace->vm_map, &addr, PAGE_SIZE,
    682                     NULL, UVM_UNKNOWN_OFFSET, 0,
    683                     UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY,
    684 		    UVM_ADV_NORMAL,
    685                     UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW)) != 0)
    686 		panic("init: couldn't allocate argument space");
    687 	p->p_vmspace->vm_maxsaddr = (caddr_t)STACK_MAX(addr, PAGE_SIZE);
    688 
    689 	ipx = 0;
    690 	while (1) {
    691 		if (boothowto & RB_ASKNAME) {
    692 			printf("init path");
    693 			if (initpaths[ipx])
    694 				printf(" (default %s)", initpaths[ipx]);
    695 			printf(": ");
    696 			len = cngetsn(ipath, sizeof(ipath)-1);
    697 			if (len == 0) {
    698 				if (initpaths[ipx])
    699 					path = initpaths[ipx++];
    700 				else
    701 					continue;
    702 			} else {
    703 				ipath[len] = '\0';
    704 				path = ipath;
    705 			}
    706 		} else {
    707 			if ((path = initpaths[ipx++]) == NULL)
    708 				break;
    709 		}
    710 
    711 		ucp = (char *)USRSTACK;
    712 
    713 		/*
    714 		 * Construct the boot flag argument.
    715 		 */
    716 		flagsp = flags;
    717 		*flagsp++ = '-';
    718 		options = 0;
    719 
    720 		if (boothowto & RB_SINGLE) {
    721 			*flagsp++ = 's';
    722 			options = 1;
    723 		}
    724 #ifdef notyet
    725 		if (boothowto & RB_FASTBOOT) {
    726 			*flagsp++ = 'f';
    727 			options = 1;
    728 		}
    729 #endif
    730 
    731 		/*
    732 		 * Move out the flags (arg 1), if necessary.
    733 		 */
    734 		if (options != 0) {
    735 			*flagsp++ = '\0';
    736 			i = flagsp - flags;
    737 #ifdef DEBUG
    738 			printf("init: copying out flags `%s' %d\n", flags, i);
    739 #endif
    740 			arg1 = STACK_ALLOC(ucp, i);
    741 			ucp = STACK_MAX(arg1, i);
    742 			(void)copyout((caddr_t)flags, arg1, i);
    743 		}
    744 
    745 		/*
    746 		 * Move out the file name (also arg 0).
    747 		 */
    748 		i = strlen(path) + 1;
    749 #ifdef DEBUG
    750 		printf("init: copying out path `%s' %d\n", path, i);
    751 #else
    752 		if (boothowto & RB_ASKNAME || path != initpaths[0])
    753 			printf("init: trying %s\n", path);
    754 #endif
    755 		arg0 = STACK_ALLOC(ucp, i);
    756 		ucp = STACK_MAX(arg0, i);
    757 		(void)copyout((caddr_t)path, arg0, i);
    758 
    759 		/*
    760 		 * Move out the arg pointers.
    761 		 */
    762 		ucp = (caddr_t)STACK_ALIGN(ucp, ALIGNBYTES);
    763 		uap = (char **)STACK_ALLOC(ucp, sizeof(char *) * 3);
    764 		SCARG(&args, path) = arg0;
    765 		SCARG(&args, argp) = uap;
    766 		SCARG(&args, envp) = NULL;
    767 		slash = strrchr(path, '/');
    768 		if (slash)
    769 			(void)suword((caddr_t)uap++,
    770 			    (long)arg0 + (slash + 1 - path));
    771 		else
    772 			(void)suword((caddr_t)uap++, (long)arg0);
    773 		if (options != 0)
    774 			(void)suword((caddr_t)uap++, (long)arg1);
    775 		(void)suword((caddr_t)uap++, 0);	/* terminator */
    776 
    777 		/*
    778 		 * Now try to exec the program.  If can't for any reason
    779 		 * other than it doesn't exist, complain.
    780 		 */
    781 		error = sys_execve(LIST_FIRST(&p->p_lwps), &args, retval);
    782 		if (error == 0 || error == EJUSTRETURN) {
    783 			KERNEL_PROC_UNLOCK(l);
    784 			return;
    785 		}
    786 		printf("exec %s: error %d\n", path, error);
    787 	}
    788 	printf("init: not found\n");
    789 	panic("no init");
    790 }
    791