Home | History | Annotate | Line # | Download | only in kern
init_main.c revision 1.216
      1 /*	$NetBSD: init_main.c,v 1.216 2003/01/18 10:06:23 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995 Christopher G. Demetriou.  All rights reserved.
      5  * Copyright (c) 1982, 1986, 1989, 1991, 1992, 1993
      6  *	The Regents of the University of California.  All rights reserved.
      7  * (c) UNIX System Laboratories, Inc.
      8  * All or some portions of this file are derived from material licensed
      9  * to the University of California by American Telephone and Telegraph
     10  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     11  * the permission of UNIX System Laboratories, Inc.
     12  *
     13  * Redistribution and use in source and binary forms, with or without
     14  * modification, are permitted provided that the following conditions
     15  * are met:
     16  * 1. Redistributions of source code must retain the above copyright
     17  *    notice, this list of conditions and the following disclaimer.
     18  * 2. Redistributions in binary form must reproduce the above copyright
     19  *    notice, this list of conditions and the following disclaimer in the
     20  *    documentation and/or other materials provided with the distribution.
     21  * 3. All advertising materials mentioning features or use of this software
     22  *    must display the following acknowledgement:
     23  *	This product includes software developed by the University of
     24  *	California, Berkeley and its contributors.
     25  * 4. Neither the name of the University nor the names of its contributors
     26  *    may be used to endorse or promote products derived from this software
     27  *    without specific prior written permission.
     28  *
     29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     39  * SUCH DAMAGE.
     40  *
     41  *	@(#)init_main.c	8.16 (Berkeley) 5/14/95
     42  */
     43 
     44 #include <sys/cdefs.h>
     45 __KERNEL_RCSID(0, "$NetBSD: init_main.c,v 1.216 2003/01/18 10:06:23 thorpej Exp $");
     46 
     47 #include "fs_nfs.h"
     48 #include "opt_nfsserver.h"
     49 #include "opt_sysv.h"
     50 #include "opt_maxuprc.h"
     51 #include "opt_multiprocessor.h"
     52 #include "opt_pipe.h"
     53 #include "opt_syscall_debug.h"
     54 #include "opt_systrace.h"
     55 
     56 #include "rnd.h"
     57 
     58 #include <sys/param.h>
     59 #include <sys/acct.h>
     60 #include <sys/filedesc.h>
     61 #include <sys/file.h>
     62 #include <sys/errno.h>
     63 #include <sys/callout.h>
     64 #include <sys/kernel.h>
     65 #include <sys/mount.h>
     66 #include <sys/proc.h>
     67 #include <sys/kthread.h>
     68 #include <sys/resourcevar.h>
     69 #include <sys/signalvar.h>
     70 #include <sys/systm.h>
     71 #include <sys/vnode.h>
     72 #include <sys/tty.h>
     73 #include <sys/conf.h>
     74 #include <sys/disklabel.h>
     75 #include <sys/buf.h>
     76 #include <sys/device.h>
     77 #include <sys/disk.h>
     78 #include <sys/exec.h>
     79 #include <sys/socketvar.h>
     80 #include <sys/protosw.h>
     81 #include <sys/reboot.h>
     82 #include <sys/user.h>
     83 #include <sys/sysctl.h>
     84 #include <sys/event.h>
     85 #ifdef SYSVSHM
     86 #include <sys/shm.h>
     87 #endif
     88 #ifdef SYSVSEM
     89 #include <sys/sem.h>
     90 #endif
     91 #ifdef SYSVMSG
     92 #include <sys/msg.h>
     93 #endif
     94 #ifdef SYSTRACE
     95 #include <sys/systrace.h>
     96 #endif
     97 #include <sys/domain.h>
     98 #include <sys/mbuf.h>
     99 #include <sys/namei.h>
    100 #if NRND > 0
    101 #include <sys/rnd.h>
    102 #endif
    103 #ifndef PIPE_SOCKETPAIR
    104 #include <sys/pipe.h>
    105 #endif
    106 #ifdef LKM
    107 #include <sys/lkm.h>
    108 #endif
    109 
    110 #include <sys/syscall.h>
    111 #include <sys/sa.h>
    112 #include <sys/syscallargs.h>
    113 
    114 #include <ufs/ufs/quota.h>
    115 
    116 #include <miscfs/genfs/genfs.h>
    117 #include <miscfs/syncfs/syncfs.h>
    118 
    119 #include <machine/cpu.h>
    120 
    121 #include <uvm/uvm.h>
    122 
    123 #include <dev/cons.h>
    124 
    125 #include <net/if.h>
    126 #include <net/raw_cb.h>
    127 
    128 const char copyright[] =
    129 "Copyright (c) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003\n"
    130 "    The NetBSD Foundation, Inc.  All rights reserved.\n"
    131 "Copyright (c) 1982, 1986, 1989, 1991, 1993\n"
    132 "    The Regents of the University of California.  All rights reserved.\n"
    133 "\n";
    134 
    135 /* Components of the first process -- never freed. */
    136 struct	session session0;
    137 struct	pgrp pgrp0;
    138 struct	proc proc0;
    139 struct	lwp lwp0;
    140 struct	pcred cred0;
    141 struct	filedesc0 filedesc0;
    142 struct	cwdinfo cwdi0;
    143 struct	plimit limit0;
    144 struct	pstats pstat0;
    145 struct	vmspace vmspace0;
    146 struct	sigacts sigacts0;
    147 #ifndef curlwp
    148 struct	lwp *curlwp = &lwp0;
    149 #endif
    150 struct	proc *initproc;
    151 
    152 int	nofile = NOFILE;
    153 int	maxuprc = MAXUPRC;
    154 int	cmask = CMASK;
    155 extern	struct user *proc0paddr;
    156 
    157 struct	vnode *rootvp, *swapdev_vp;
    158 int	boothowto;
    159 int	cold = 1;			/* still working on startup */
    160 struct	timeval boottime;
    161 
    162 __volatile int start_init_exec;		/* semaphore for start_init() */
    163 
    164 static void check_console(struct proc *p);
    165 static void start_init(void *);
    166 void main(void);
    167 
    168 extern const struct emul emul_netbsd;	/* defined in kern_exec.c */
    169 
    170 /*
    171  * System startup; initialize the world, create process 0, mount root
    172  * filesystem, and fork to create init and pagedaemon.  Most of the
    173  * hard work is done in the lower-level initialization routines including
    174  * startup(), which does memory initialization and autoconfiguration.
    175  */
    176 void
    177 main(void)
    178 {
    179 	struct lwp *l;
    180 	struct proc *p;
    181 	struct pdevinit *pdev;
    182 	int s, error;
    183 	u_int i;
    184 	rlim_t lim;
    185 	extern struct pdevinit pdevinit[];
    186 	extern void schedcpu(void *);
    187 #if defined(NFSSERVER) || defined(NFS)
    188 	extern void nfs_init(void);
    189 #endif
    190 #ifdef NVNODE_IMPLICIT
    191 	int usevnodes;
    192 #endif
    193 
    194 	/*
    195 	 * Initialize the current LWP pointer (curlwp) before
    196 	 * any possible traps/probes to simplify trap processing.
    197 	 */
    198 	simple_lock_init(&proc0.p_raslock);
    199 	l = &lwp0;
    200 	curlwp = l;
    201 	l->l_cpu = curcpu();
    202 	l->l_proc = &proc0;
    203 	l->l_lid = 1;
    204 	/*
    205 	 * Attempt to find console and initialize
    206 	 * in case of early panic or other messages.
    207 	 */
    208 	consinit();
    209 	printf("%s", copyright);
    210 
    211 	KERNEL_LOCK_INIT();
    212 
    213 	uvm_init();
    214 
    215 	/* Do machine-dependent initialization. */
    216 	cpu_startup();
    217 
    218 	/* Initialize callouts. */
    219 	callout_startup();
    220 
    221 	/*
    222 	 * Initialize mbuf's.  Do this now because we might attempt to
    223 	 * allocate mbufs or mbuf clusters during autoconfiguration.
    224 	 */
    225 	mbinit();
    226 
    227 	/* Initialize kqueues. */
    228 	kqueue_init();
    229 
    230 	/* Initialize sockets. */
    231 	soinit();
    232 
    233 	/*
    234 	 * The following things must be done before autoconfiguration.
    235 	 */
    236 	evcnt_init();		/* initialize event counters */
    237 	disk_init();		/* initialize disk list */
    238 	tty_init();		/* initialize tty list */
    239 #if NRND > 0
    240 	rnd_init();		/* initialize RNG */
    241 #endif
    242 
    243 	/* Initialize the sysctl subsystem. */
    244 	sysctl_init();
    245 
    246 	/*
    247 	 * Initialize process and pgrp structures.
    248 	 */
    249 	procinit();
    250 
    251 #ifdef LKM
    252 	/* Initialize the LKM system. */
    253 	lkm_init();
    254 #endif
    255 
    256 	/*
    257 	 * Create process 0 (the swapper).
    258 	 */
    259 	p = &proc0;
    260 	LIST_INIT(&p->p_lwps);
    261 	LIST_INSERT_HEAD(&p->p_lwps, l, l_sibling);
    262 	p->p_nlwps = 1;
    263 
    264 	s = proclist_lock_write();
    265 	LIST_INSERT_HEAD(&allproc, p, p_list);
    266 	LIST_INSERT_HEAD(PIDHASH(p->p_pid), p, p_hash);
    267 	LIST_INSERT_HEAD(&alllwp, l, l_list);
    268 	proclist_unlock_write(s);
    269 
    270 	p->p_pgrp = &pgrp0;
    271 	LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash);
    272 	LIST_INIT(&pgrp0.pg_members);
    273 	LIST_INSERT_HEAD(&pgrp0.pg_members, p, p_pglist);
    274 
    275 	pgrp0.pg_session = &session0;
    276 	session0.s_count = 1;
    277 	session0.s_sid = p->p_pid;
    278 	session0.s_leader = p;
    279 
    280 	/*
    281 	 * Set P_NOCLDWAIT so that kernel threads are reparented to
    282 	 * init(8) when they exit.  init(8) can easily wait them out
    283 	 * for us.
    284 	 */
    285 	p->p_flag = P_SYSTEM | P_NOCLDWAIT;
    286 	p->p_stat = SACTIVE;
    287 	p->p_nice = NZERO;
    288 	p->p_emul = &emul_netbsd;
    289 #ifdef __HAVE_SYSCALL_INTERN
    290 	(*p->p_emul->e_syscall_intern)(p);
    291 #endif
    292 	strncpy(p->p_comm, "swapper", MAXCOMLEN);
    293 
    294 	l->l_flag = L_INMEM;
    295 	l->l_stat = LSONPROC;
    296 	p->p_nrlwps = 1;
    297 
    298 	callout_init(&l->l_tsleep_ch);
    299 
    300 	/* Create credentials. */
    301 	cred0.p_refcnt = 1;
    302 	p->p_cred = &cred0;
    303 	p->p_ucred = crget();
    304 	p->p_ucred->cr_ngroups = 1;	/* group 0 */
    305 
    306 	/* Create the file descriptor table. */
    307 	finit();
    308 	p->p_fd = &filedesc0.fd_fd;
    309 	fdinit1(&filedesc0);
    310 
    311 	/* Create the CWD info. */
    312 	p->p_cwdi = &cwdi0;
    313 	cwdi0.cwdi_cmask = cmask;
    314 	cwdi0.cwdi_refcnt = 1;
    315 
    316 	/* Create the limits structures. */
    317 	p->p_limit = &limit0;
    318 	for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++)
    319 		limit0.pl_rlimit[i].rlim_cur =
    320 		    limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY;
    321 
    322 	limit0.pl_rlimit[RLIMIT_NOFILE].rlim_max = maxfiles;
    323 	limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur =
    324 	    maxfiles < nofile ? maxfiles : nofile;
    325 
    326 	limit0.pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc;
    327 	limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur =
    328 	    maxproc < maxuprc ? maxproc : maxuprc;
    329 
    330 	lim = ptoa(uvmexp.free);
    331 	limit0.pl_rlimit[RLIMIT_RSS].rlim_max = lim;
    332 	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = lim;
    333 	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = lim / 3;
    334 	limit0.pl_corename = defcorename;
    335 	limit0.p_refcnt = 1;
    336 
    337 	/*
    338 	 * Initialize proc0's vmspace, which uses the kernel pmap.
    339 	 * All kernel processes (which never have user space mappings)
    340 	 * share proc0's vmspace, and thus, the kernel pmap.
    341 	 */
    342 	uvmspace_init(&vmspace0, pmap_kernel(), round_page(VM_MIN_ADDRESS),
    343 	    trunc_page(VM_MAX_ADDRESS));
    344 	p->p_vmspace = &vmspace0;
    345 
    346 	l->l_addr = proc0paddr;				/* XXX */
    347 
    348 	p->p_stats = &pstat0;
    349 
    350 	/*
    351 	 * Charge root for one process.
    352 	 */
    353 	(void)chgproccnt(0, 1);
    354 
    355 	rqinit();
    356 
    357 	/* Configure virtual memory system, set vm rlimits. */
    358 	uvm_init_limits(p);
    359 
    360 	/* Initialize the file systems. */
    361 #if defined(NFSSERVER) || defined(NFS)
    362 	nfs_init();			/* initialize server/shared data */
    363 #endif
    364 #ifdef NVNODE_IMPLICIT
    365 	/*
    366 	 * If maximum number of vnodes in namei vnode cache is not explicitly
    367 	 * defined in kernel config, adjust the number such as we use roughly
    368 	 * 0.5% of memory for vnode cache (but not less than NVNODE vnodes).
    369 	 */
    370 	usevnodes = (ptoa((unsigned)physmem) / 200) / sizeof(struct vnode);
    371 	if (usevnodes > desiredvnodes)
    372 		desiredvnodes = usevnodes;
    373 #endif
    374 	vfsinit();
    375 
    376 	/* Configure the system hardware.  This will enable interrupts. */
    377 	configure();
    378 
    379 	ubc_init();		/* must be after autoconfig */
    380 
    381 	/* Lock the kernel on behalf of proc0. */
    382 	KERNEL_PROC_LOCK(l);
    383 
    384 #ifdef SYSVSHM
    385 	/* Initialize System V style shared memory. */
    386 	shminit();
    387 #endif
    388 
    389 #ifdef SYSVSEM
    390 	/* Initialize System V style semaphores. */
    391 	seminit();
    392 #endif
    393 
    394 #ifdef SYSVMSG
    395 	/* Initialize System V style message queues. */
    396 	msginit();
    397 #endif
    398 
    399 	/* Attach pseudo-devices. */
    400 	for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++)
    401 		(*pdev->pdev_attach)(pdev->pdev_count);
    402 
    403 	/*
    404 	 * Initialize protocols.  Block reception of incoming packets
    405 	 * until everything is ready.
    406 	 */
    407 	s = splnet();
    408 	ifinit();
    409 	domaininit();
    410 	if_attachdomain();
    411 	splx(s);
    412 
    413 #ifdef GPROF
    414 	/* Initialize kernel profiling. */
    415 	kmstartup();
    416 #endif
    417 
    418 	/* Initialize system accouting. */
    419 	acct_init();
    420 
    421 #ifdef SYSTRACE
    422 	systrace_init();
    423 #endif
    424 	/*
    425 	 * Initialize signal-related data structures, and signal state
    426 	 * for proc0.
    427 	 */
    428 	signal_init();
    429 	p->p_sigacts = &sigacts0;
    430 	siginit(p);
    431 
    432 	/* Kick off timeout driven events by calling first time. */
    433 	schedcpu(NULL);
    434 
    435 	/*
    436 	 * Create process 1 (init(8)).  We do this now, as Unix has
    437 	 * historically had init be process 1, and changing this would
    438 	 * probably upset a lot of people.
    439 	 *
    440 	 * Note that process 1 won't immediately exec init(8), but will
    441 	 * wait for us to inform it that the root file system has been
    442 	 * mounted.
    443 	 */
    444 	if (fork1(l, 0, SIGCHLD, NULL, 0, start_init, NULL, NULL, &initproc))
    445 		panic("fork init");
    446 
    447 	/*
    448 	 * Create any kernel threads who's creation was deferred because
    449 	 * initproc had not yet been created.
    450 	 */
    451 	kthread_run_deferred_queue();
    452 
    453 	/*
    454 	 * Now that device driver threads have been created, wait for
    455 	 * them to finish any deferred autoconfiguration.  Note we don't
    456 	 * need to lock this semaphore, since we haven't booted any
    457 	 * secondary processors, yet.
    458 	 */
    459 	while (config_pending)
    460 		(void) tsleep((void *)&config_pending, PWAIT, "cfpend", 0);
    461 
    462 	/*
    463 	 * Finalize configuration now that all real devices have been
    464 	 * found.  This needs to be done before the root device is
    465 	 * selected, since finalization may create the root device.
    466 	 */
    467 	config_finalize();
    468 
    469 	/*
    470 	 * Now that autoconfiguration has completed, we can determine
    471 	 * the root and dump devices.
    472 	 */
    473 	cpu_rootconf();
    474 	cpu_dumpconf();
    475 
    476 	/* Mount the root file system. */
    477 	do {
    478 		domountroothook();
    479 		if ((error = vfs_mountroot())) {
    480 			printf("cannot mount root, error = %d\n", error);
    481 			boothowto |= RB_ASKNAME;
    482 			setroot(root_device,
    483 			    (rootdev != NODEV) ? DISKPART(rootdev) : 0);
    484 		}
    485 	} while (error != 0);
    486 	mountroothook_destroy();
    487 
    488 	CIRCLEQ_FIRST(&mountlist)->mnt_flag |= MNT_ROOTFS;
    489 	CIRCLEQ_FIRST(&mountlist)->mnt_op->vfs_refcount++;
    490 
    491 	/*
    492 	 * Get the vnode for '/'.  Set filedesc0.fd_fd.fd_cdir to
    493 	 * reference it.
    494 	 */
    495 	if (VFS_ROOT(CIRCLEQ_FIRST(&mountlist), &rootvnode))
    496 		panic("cannot find root vnode");
    497 	cwdi0.cwdi_cdir = rootvnode;
    498 	VREF(cwdi0.cwdi_cdir);
    499 	VOP_UNLOCK(rootvnode, 0);
    500 	cwdi0.cwdi_rdir = NULL;
    501 
    502 	/*
    503 	 * Now that root is mounted, we can fixup initproc's CWD
    504 	 * info.  All other processes are kthreads, which merely
    505 	 * share proc0's CWD info.
    506 	 */
    507 	initproc->p_cwdi->cwdi_cdir = rootvnode;
    508 	VREF(initproc->p_cwdi->cwdi_cdir);
    509 	initproc->p_cwdi->cwdi_rdir = NULL;
    510 
    511 	/*
    512 	 * Now can look at time, having had a chance to verify the time
    513 	 * from the file system.  Reset p->p_rtime as it may have been
    514 	 * munched in mi_switch() after the time got set.
    515 	 */
    516 	proclist_lock_read();
    517 	s = splsched();
    518 	for (p = LIST_FIRST(&allproc); p != NULL;
    519 	     p = LIST_NEXT(p, p_list)) {
    520 		p->p_stats->p_start = mono_time = boottime = time;
    521 		for (l = LIST_FIRST(&p->p_lwps); l != NULL;
    522 		     l = LIST_NEXT(l, l_sibling))
    523 			if (l->l_cpu != NULL)
    524 				l->l_cpu->ci_schedstate.spc_runtime = time;
    525 		p->p_rtime.tv_sec = p->p_rtime.tv_usec = 0;
    526 	}
    527 	splx(s);
    528 	proclist_unlock_read();
    529 
    530 	/* Create the pageout daemon kernel thread. */
    531 	uvm_swap_init();
    532 	if (kthread_create1(uvm_pageout, NULL, NULL, "pagedaemon"))
    533 		panic("fork pagedaemon");
    534 
    535 	/* Create the process reaper kernel thread. */
    536 	if (kthread_create1(reaper, NULL, NULL, "reaper"))
    537 		panic("fork reaper");
    538 
    539 	/* Create the filesystem syncer kernel thread. */
    540 	if (kthread_create1(sched_sync, NULL, NULL, "ioflush"))
    541 		panic("fork syncer");
    542 
    543 	/* Create the aiodone daemon kernel thread. */
    544 	if (kthread_create1(uvm_aiodone_daemon, NULL, &uvm.aiodoned_proc,
    545 	    "aiodoned"))
    546 		panic("fork aiodoned");
    547 
    548 #if defined(MULTIPROCESSOR)
    549 	/* Boot the secondary processors. */
    550 	cpu_boot_secondary_processors();
    551 #endif
    552 
    553 	/* Initialize exec structures */
    554 	exec_init(1);
    555 
    556 #ifndef PIPE_SOCKETPAIR
    557 	/* Initialize pipe structures */
    558 	pipe_init();
    559 #endif
    560 
    561 	/*
    562 	 * Okay, now we can let init(8) exec!  It's off to userland!
    563 	 */
    564 	start_init_exec = 1;
    565 	wakeup((void *)&start_init_exec);
    566 
    567 	/* The scheduler is an infinite loop. */
    568 	uvm_scheduler();
    569 	/* NOTREACHED */
    570 }
    571 
    572 static void
    573 check_console(struct proc *p)
    574 {
    575 	struct nameidata nd;
    576 	int error;
    577 
    578 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/console", p);
    579 	error = namei(&nd);
    580 	if (error == 0)
    581 		vrele(nd.ni_vp);
    582 	else if (error == ENOENT)
    583 		printf("warning: no /dev/console\n");
    584 	else
    585 		printf("warning: lookup /dev/console: error %d\n", error);
    586 }
    587 
    588 /*
    589  * List of paths to try when searching for "init".
    590  */
    591 static const char *initpaths[] = {
    592 	"/sbin/init",
    593 	"/sbin/oinit",
    594 	"/sbin/init.bak",
    595 	NULL,
    596 };
    597 
    598 /*
    599  * Start the initial user process; try exec'ing each pathname in "initpaths".
    600  * The program is invoked with one argument containing the boot flags.
    601  */
    602 static void
    603 start_init(void *arg)
    604 {
    605 	struct lwp *l = arg;
    606 	struct proc *p = l->l_proc;
    607 	vaddr_t addr;
    608 	struct sys_execve_args /* {
    609 		syscallarg(const char *) path;
    610 		syscallarg(char * const *) argp;
    611 		syscallarg(char * const *) envp;
    612 	} */ args;
    613 	int options, i, error;
    614 	register_t retval[2];
    615 	char flags[4], *flagsp;
    616 	const char *path, *slash;
    617 	char *ucp, **uap, *arg0, *arg1 = NULL;
    618 	char ipath[129];
    619 	int ipx, len;
    620 
    621 	/*
    622 	 * Now in process 1.
    623 	 */
    624 	strncpy(p->p_comm, "init", MAXCOMLEN);
    625 
    626 	/*
    627 	 * Wait for main() to tell us that it's safe to exec.
    628 	 */
    629 	while (start_init_exec == 0)
    630 		(void) tsleep((void *)&start_init_exec, PWAIT, "initexec", 0);
    631 
    632 	/*
    633 	 * This is not the right way to do this.  We really should
    634 	 * hand-craft a descriptor onto /dev/console to hand to init,
    635 	 * but that's a _lot_ more work, and the benefit from this easy
    636 	 * hack makes up for the "good is the enemy of the best" effect.
    637 	 */
    638 	check_console(p);
    639 
    640 	/*
    641 	 * Need just enough stack to hold the faked-up "execve()" arguments.
    642 	 */
    643 	addr = (vaddr_t)STACK_ALLOC(USRSTACK, PAGE_SIZE);
    644 	if (uvm_map(&p->p_vmspace->vm_map, &addr, PAGE_SIZE,
    645                     NULL, UVM_UNKNOWN_OFFSET, 0,
    646                     UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY,
    647 		    UVM_ADV_NORMAL,
    648                     UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW)) != 0)
    649 		panic("init: couldn't allocate argument space");
    650 	p->p_vmspace->vm_maxsaddr = (caddr_t)STACK_MAX(addr, PAGE_SIZE);
    651 
    652 	ipx = 0;
    653 	while (1) {
    654 		if (boothowto & RB_ASKNAME) {
    655 			printf("init path");
    656 			if (initpaths[ipx])
    657 				printf(" (default %s)", initpaths[ipx]);
    658 			printf(": ");
    659 			len = cngetsn(ipath, sizeof(ipath)-1);
    660 			if (len == 0) {
    661 				if (initpaths[ipx])
    662 					path = initpaths[ipx++];
    663 				else
    664 					continue;
    665 			} else {
    666 				ipath[len] = '\0';
    667 				path = ipath;
    668 			}
    669 		} else {
    670 			if ((path = initpaths[ipx++]) == NULL)
    671 				break;
    672 		}
    673 
    674 		ucp = (char *)USRSTACK;
    675 
    676 		/*
    677 		 * Construct the boot flag argument.
    678 		 */
    679 		flagsp = flags;
    680 		*flagsp++ = '-';
    681 		options = 0;
    682 
    683 		if (boothowto & RB_SINGLE) {
    684 			*flagsp++ = 's';
    685 			options = 1;
    686 		}
    687 #ifdef notyet
    688 		if (boothowto & RB_FASTBOOT) {
    689 			*flagsp++ = 'f';
    690 			options = 1;
    691 		}
    692 #endif
    693 
    694 		/*
    695 		 * Move out the flags (arg 1), if necessary.
    696 		 */
    697 		if (options != 0) {
    698 			*flagsp++ = '\0';
    699 			i = flagsp - flags;
    700 #ifdef DEBUG
    701 			printf("init: copying out flags `%s' %d\n", flags, i);
    702 #endif
    703 			arg1 = STACK_ALLOC(ucp, i);
    704 			ucp = STACK_MAX(arg1, i);
    705 			(void)copyout((caddr_t)flags, arg1, i);
    706 		}
    707 
    708 		/*
    709 		 * Move out the file name (also arg 0).
    710 		 */
    711 		i = strlen(path) + 1;
    712 #ifdef DEBUG
    713 		printf("init: copying out path `%s' %d\n", path, i);
    714 #else
    715 		if (boothowto & RB_ASKNAME || path != initpaths[0])
    716 			printf("init: trying %s\n", path);
    717 #endif
    718 		arg0 = STACK_ALLOC(ucp, i);
    719 		ucp = STACK_MAX(arg0, i);
    720 		(void)copyout((caddr_t)path, arg0, i);
    721 
    722 		/*
    723 		 * Move out the arg pointers.
    724 		 */
    725 		ucp = (caddr_t)STACK_ALIGN(ucp, ALIGNBYTES);
    726 		uap = (char **)STACK_ALLOC(ucp, sizeof(char *) * 3);
    727 		SCARG(&args, path) = arg0;
    728 		SCARG(&args, argp) = uap;
    729 		SCARG(&args, envp) = NULL;
    730 		slash = strrchr(path, '/');
    731 		if (slash)
    732 			(void)suword((caddr_t)uap++,
    733 			    (long)arg0 + (slash + 1 - path));
    734 		else
    735 			(void)suword((caddr_t)uap++, (long)arg0);
    736 		if (options != 0)
    737 			(void)suword((caddr_t)uap++, (long)arg1);
    738 		(void)suword((caddr_t)uap++, 0);	/* terminator */
    739 
    740 		/*
    741 		 * Now try to exec the program.  If can't for any reason
    742 		 * other than it doesn't exist, complain.
    743 		 */
    744 		error = sys_execve(LIST_FIRST(&p->p_lwps), &args, retval);
    745 		if (error == 0 || error == EJUSTRETURN) {
    746 			KERNEL_PROC_UNLOCK(l);
    747 			return;
    748 		}
    749 		printf("exec %s: error %d\n", path, error);
    750 	}
    751 	printf("init: not found\n");
    752 	panic("no init");
    753 }
    754