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