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