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