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