Home | History | Annotate | Line # | Download | only in kern
init_main.c revision 1.359
      1 /*	$NetBSD: init_main.c,v 1.359 2008/06/16 11:26:28 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 /*
     30  * Copyright (c) 1982, 1986, 1989, 1991, 1992, 1993
     31  *	The Regents of the University of California.  All rights reserved.
     32  * (c) UNIX System Laboratories, Inc.
     33  * All or some portions of this file are derived from material licensed
     34  * to the University of California by American Telephone and Telegraph
     35  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     36  * the permission of UNIX System Laboratories, Inc.
     37  *
     38  * Redistribution and use in source and binary forms, with or without
     39  * modification, are permitted provided that the following conditions
     40  * are met:
     41  * 1. Redistributions of source code must retain the above copyright
     42  *    notice, this list of conditions and the following disclaimer.
     43  * 2. Redistributions in binary form must reproduce the above copyright
     44  *    notice, this list of conditions and the following disclaimer in the
     45  *    documentation and/or other materials provided with the distribution.
     46  * 3. Neither the name of the University nor the names of its contributors
     47  *    may be used to endorse or promote products derived from this software
     48  *    without specific prior written permission.
     49  *
     50  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     51  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     52  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     53  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     54  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     55  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     56  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     57  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     58  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     59  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     60  * SUCH DAMAGE.
     61  *
     62  *	@(#)init_main.c	8.16 (Berkeley) 5/14/95
     63  */
     64 
     65 /*
     66  * Copyright (c) 1995 Christopher G. Demetriou.  All rights reserved.
     67  *
     68  * Redistribution and use in source and binary forms, with or without
     69  * modification, are permitted provided that the following conditions
     70  * are met:
     71  * 1. Redistributions of source code must retain the above copyright
     72  *    notice, this list of conditions and the following disclaimer.
     73  * 2. Redistributions in binary form must reproduce the above copyright
     74  *    notice, this list of conditions and the following disclaimer in the
     75  *    documentation and/or other materials provided with the distribution.
     76  * 3. All advertising materials mentioning features or use of this software
     77  *    must display the following acknowledgement:
     78  *	This product includes software developed by the University of
     79  *	California, Berkeley and its contributors.
     80  * 4. Neither the name of the University nor the names of its contributors
     81  *    may be used to endorse or promote products derived from this software
     82  *    without specific prior written permission.
     83  *
     84  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     85  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     86  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     87  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     88  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     89  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     90  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     91  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     92  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     93  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     94  * SUCH DAMAGE.
     95  *
     96  *	@(#)init_main.c	8.16 (Berkeley) 5/14/95
     97  */
     98 
     99 #include <sys/cdefs.h>
    100 __KERNEL_RCSID(0, "$NetBSD: init_main.c,v 1.359 2008/06/16 11:26:28 ad Exp $");
    101 
    102 #include "opt_ipsec.h"
    103 #include "opt_ntp.h"
    104 #include "opt_pipe.h"
    105 #include "opt_posix.h"
    106 #include "opt_syscall_debug.h"
    107 #include "opt_sysv.h"
    108 #include "opt_fileassoc.h"
    109 #include "opt_ktrace.h"
    110 #include "opt_pax.h"
    111 
    112 #include "rnd.h"
    113 #include "sysmon_envsys.h"
    114 #include "sysmon_power.h"
    115 #include "sysmon_taskq.h"
    116 #include "sysmon_wdog.h"
    117 #include "veriexec.h"
    118 
    119 #include <sys/param.h>
    120 #include <sys/acct.h>
    121 #include <sys/filedesc.h>
    122 #include <sys/file.h>
    123 #include <sys/errno.h>
    124 #include <sys/callout.h>
    125 #include <sys/cpu.h>
    126 #include <sys/kernel.h>
    127 #include <sys/kmem.h>
    128 #include <sys/mount.h>
    129 #include <sys/proc.h>
    130 #include <sys/kthread.h>
    131 #include <sys/resourcevar.h>
    132 #include <sys/signalvar.h>
    133 #include <sys/systm.h>
    134 #include <sys/vnode.h>
    135 #include <sys/fstrans.h>
    136 #include <sys/tty.h>
    137 #include <sys/conf.h>
    138 #include <sys/disklabel.h>
    139 #include <sys/buf.h>
    140 #include <sys/device.h>
    141 #include <sys/exec.h>
    142 #include <sys/socketvar.h>
    143 #include <sys/protosw.h>
    144 #include <sys/percpu.h>
    145 #include <sys/pset.h>
    146 #include <sys/sysctl.h>
    147 #include <sys/reboot.h>
    148 #include <sys/user.h>
    149 #include <sys/sysctl.h>
    150 #include <sys/event.h>
    151 #include <sys/mbuf.h>
    152 #include <sys/sched.h>
    153 #include <sys/sleepq.h>
    154 #include <sys/iostat.h>
    155 #include <sys/vmem.h>
    156 #include <sys/uuid.h>
    157 #include <sys/extent.h>
    158 #include <sys/disk.h>
    159 #include <sys/mqueue.h>
    160 #include <sys/msgbuf.h>
    161 #include <sys/module.h>
    162 #include <sys/event.h>
    163 #include <sys/lockf.h>
    164 #ifdef FAST_IPSEC
    165 #include <netipsec/ipsec.h>
    166 #endif
    167 #ifdef SYSVSHM
    168 #include <sys/shm.h>
    169 #endif
    170 #ifdef SYSVSEM
    171 #include <sys/sem.h>
    172 #endif
    173 #ifdef SYSVMSG
    174 #include <sys/msg.h>
    175 #endif
    176 #ifdef P1003_1B_SEMAPHORE
    177 #include <sys/ksem.h>
    178 #endif
    179 #include <sys/domain.h>
    180 #include <sys/namei.h>
    181 #if NRND > 0
    182 #include <sys/rnd.h>
    183 #endif
    184 #include <sys/pipe.h>
    185 #ifdef LKM
    186 #include <sys/lkm.h>
    187 #endif
    188 #if NVERIEXEC > 0
    189 #include <sys/verified_exec.h>
    190 #endif /* NVERIEXEC > 0 */
    191 #ifdef KTRACE
    192 #include <sys/ktrace.h>
    193 #endif
    194 #include <sys/kauth.h>
    195 #include <net80211/ieee80211_netbsd.h>
    196 
    197 #include <sys/syscall.h>
    198 #include <sys/syscallargs.h>
    199 
    200 #if defined(PAX_MPROTECT) || defined(PAX_SEGVGUARD) || defined(PAX_ASLR)
    201 #include <sys/pax.h>
    202 #endif /* PAX_MPROTECT || PAX_SEGVGUARD || PAX_ASLR */
    203 
    204 #include <ufs/ufs/quota.h>
    205 
    206 #include <miscfs/genfs/genfs.h>
    207 #include <miscfs/syncfs/syncfs.h>
    208 
    209 #include <sys/cpu.h>
    210 
    211 #include <uvm/uvm.h>
    212 
    213 #if NSYSMON_TASKQ > 0
    214 #include <dev/sysmon/sysmon_taskq.h>
    215 #endif
    216 
    217 #include <dev/cons.h>
    218 
    219 #if NSYSMON_ENVSYS > 0 || NSYSMON_POWER > 0 || NSYSMON_WDOG > 0
    220 #include <dev/sysmon/sysmonvar.h>
    221 #endif
    222 
    223 #include <net/if.h>
    224 #include <net/raw_cb.h>
    225 
    226 #include <secmodel/secmodel.h>
    227 
    228 extern struct proc proc0;
    229 extern struct lwp lwp0;
    230 extern struct cwdinfo cwdi0;
    231 extern time_t rootfstime;
    232 
    233 #ifndef curlwp
    234 struct	lwp *curlwp = &lwp0;
    235 #endif
    236 struct	proc *initproc;
    237 
    238 struct	vnode *rootvp, *swapdev_vp;
    239 int	boothowto;
    240 int	cold = 1;			/* still working on startup */
    241 struct timeval boottime;	        /* time at system startup - will only follow settime deltas */
    242 
    243 int	start_init_exec;		/* semaphore for start_init() */
    244 
    245 static void check_console(struct lwp *l);
    246 static void start_init(void *);
    247 void main(void);
    248 void ssp_init(void);
    249 
    250 #if defined(__SSP__) || defined(__SSP_ALL__)
    251 long __stack_chk_guard[8] = {0, 0, 0, 0, 0, 0, 0, 0};
    252 void __stack_chk_fail(void);
    253 
    254 void
    255 __stack_chk_fail(void)
    256 {
    257 	panic("stack overflow detected; terminated");
    258 }
    259 
    260 void
    261 ssp_init(void)
    262 {
    263 	int s;
    264 
    265 #ifdef DIAGNOSTIC
    266 	printf("Initializing SSP:");
    267 #endif
    268 	/*
    269 	 * We initialize ssp here carefully:
    270 	 *	1. after we got some entropy
    271 	 *	2. without calling a function
    272 	 */
    273 	size_t i;
    274 	long guard[__arraycount(__stack_chk_guard)];
    275 
    276 	arc4randbytes(guard, sizeof(guard));
    277 	s = splhigh();
    278 	for (i = 0; i < __arraycount(guard); i++)
    279 		__stack_chk_guard[i] = guard[i];
    280 	splx(s);
    281 #ifdef DIAGNOSTIC
    282 	for (i = 0; i < __arraycount(guard); i++)
    283 		printf("%lx ", guard[i]);
    284 	printf("\n");
    285 #endif
    286 }
    287 #else
    288 void
    289 ssp_init(void)
    290 {
    291 
    292 }
    293 #endif
    294 
    295 void __secmodel_none(void);
    296 __weak_alias(secmodel_start,__secmodel_none);
    297 void
    298 __secmodel_none(void)
    299 {
    300 	return;
    301 }
    302 
    303 /*
    304  * System startup; initialize the world, create process 0, mount root
    305  * filesystem, and fork to create init and pagedaemon.  Most of the
    306  * hard work is done in the lower-level initialization routines including
    307  * startup(), which does memory initialization and autoconfiguration.
    308  */
    309 void
    310 main(void)
    311 {
    312 	struct timeval time;
    313 	struct lwp *l;
    314 	struct proc *p;
    315 	int s, error;
    316 #ifdef NVNODE_IMPLICIT
    317 	int usevnodes;
    318 #endif
    319 	CPU_INFO_ITERATOR cii;
    320 	struct cpu_info *ci;
    321 
    322 	l = &lwp0;
    323 #ifndef LWP0_CPU_INFO
    324 	l->l_cpu = curcpu();
    325 #endif
    326 
    327 	/*
    328 	 * Attempt to find console and initialize
    329 	 * in case of early panic or other messages.
    330 	 */
    331 	consinit();
    332 
    333 	kernel_lock_init();
    334 
    335 	uvm_init();
    336 
    337 	kmem_init();
    338 
    339 	percpu_init();
    340 
    341 	/* Initialize lock caches. */
    342 	mutex_obj_init();
    343 
    344 	/* Initialize the extent manager. */
    345 	extent_init();
    346 
    347 	/* Do machine-dependent initialization. */
    348 	cpu_startup();
    349 
    350 	/* Initialize callouts, part 1. */
    351 	callout_startup();
    352 
    353 	/*
    354 	 * Initialize the kernel authorization subsystem and start the
    355 	 * default security model, if any. We need to do this early
    356 	 * enough so that subsystems relying on any of the aforementioned
    357 	 * can work properly. Since the security model may dictate the
    358 	 * credential inheritance policy, it is needed at least before
    359 	 * any process is created, specifically proc0.
    360 	 */
    361 	kauth_init();
    362 	secmodel_start();
    363 
    364 	/* Initialize the buffer cache */
    365 	bufinit();
    366 
    367 	/* Initialize sockets. */
    368 	soinit();
    369 
    370 	/*
    371 	 * The following things must be done before autoconfiguration.
    372 	 */
    373 	evcnt_init();		/* initialize event counters */
    374 #if NRND > 0
    375 	rnd_init();		/* initialize RNG */
    376 #endif
    377 
    378 	/* Initialize process and pgrp structures. */
    379 	procinit();
    380 	lwpinit();
    381 
    382 	/* Initialize signal-related data structures. */
    383 	signal_init();
    384 
    385 	/* Initialize resource management. */
    386 	resource_init();
    387 
    388 	/* Create process 0 (the swapper). */
    389 	proc0_init();
    390 
    391 	/* Initialize the UID hash table. */
    392 	uid_init();
    393 
    394 	/* Charge root for one process. */
    395 	(void)chgproccnt(0, 1);
    396 
    397 	/* Initialize timekeeping. */
    398 	time_init();
    399 
    400 	/* Initialize the run queues, turnstiles and sleep queues. */
    401 	mutex_init(&cpu_lock, MUTEX_DEFAULT, IPL_NONE);
    402 	sched_rqinit();
    403 	turnstile_init();
    404 	sleeptab_init(&sleeptab);
    405 
    406 	/* Initialize processor-sets */
    407 	psets_init();
    408 
    409 	/* MI initialization of the boot cpu */
    410 	error = mi_cpu_attach(curcpu());
    411 	KASSERT(error == 0);
    412 
    413 	/* Initialize timekeeping, part 2. */
    414 	time_init2();
    415 
    416 	/*
    417 	 * Initialize mbuf's.  Do this now because we might attempt to
    418 	 * allocate mbufs or mbuf clusters during autoconfiguration.
    419 	 */
    420 	mbinit();
    421 
    422 	/* Initialize the sysctl subsystem. */
    423 	sysctl_init();
    424 
    425 	/* Initialize I/O statistics. */
    426 	iostat_init();
    427 
    428 	/* Initialize the log device. */
    429 	loginit();
    430 
    431 	/* Start module system. */
    432 	module_init();
    433 
    434 	/* Initialize the file systems. */
    435 #ifdef NVNODE_IMPLICIT
    436 	/*
    437 	 * If maximum number of vnodes in namei vnode cache is not explicitly
    438 	 * defined in kernel config, adjust the number such as we use roughly
    439 	 * 10% of memory for vnodes and associated data structures in the
    440 	 * assumed worst case.  Do not provide fewer than NVNODE vnodes.
    441 	 */
    442 	usevnodes =
    443 	    calc_cache_size(kernel_map, 10, VNODE_VA_MAXPCT) / VNODE_COST;
    444 	if (usevnodes > desiredvnodes)
    445 		desiredvnodes = usevnodes;
    446 #endif
    447 	vfsinit();
    448 	lf_init();
    449 
    450 	/* Initialize fstrans. */
    451 	fstrans_init();
    452 
    453 	/* Initialize the file descriptor system. */
    454 	fd_sys_init();
    455 
    456 	/* Initialize kqueue. */
    457 	kqueue_init();
    458 
    459 	/* Initialize asynchronous I/O. */
    460 	aio_sysinit();
    461 
    462 	/* Initialize message queues. */
    463 	mqueue_sysinit();
    464 
    465 	/* Initialize the system monitor subsystems. */
    466 #if NSYSMON_TASKQ > 0
    467 	sysmon_task_queue_preinit();
    468 #endif
    469 
    470 #if NSYSMON_ENVSYS > 0
    471 	sysmon_envsys_init();
    472 #endif
    473 
    474 #if NSYSMON_POWER > 0
    475 	sysmon_power_init();
    476 #endif
    477 
    478 #if NSYSMON_WDOG > 0
    479 	sysmon_wdog_init();
    480 #endif
    481 
    482 	inittimecounter();
    483 	ntp_init();
    484 
    485 	/* Initialize the device switch tables. */
    486 	devsw_init();
    487 
    488 	/* Initialize tty subsystem. */
    489 	tty_init();
    490 	ttyldisc_init();
    491 
    492 	/* Initialize the buffer cache, part 2. */
    493 	bufinit2();
    494 
    495 	/* Initialize the disk wedge subsystem. */
    496 	dkwedge_init();
    497 
    498 	/* Configure the system hardware.  This will enable interrupts. */
    499 	configure();
    500 
    501 	ubc_init();		/* must be after autoconfig */
    502 
    503 #ifdef SYSVSHM
    504 	/* Initialize System V style shared memory. */
    505 	shminit();
    506 #endif
    507 
    508 #ifdef SYSVSEM
    509 	/* Initialize System V style semaphores. */
    510 	seminit();
    511 #endif
    512 
    513 #ifdef SYSVMSG
    514 	/* Initialize System V style message queues. */
    515 	msginit();
    516 #endif
    517 
    518 #ifdef P1003_1B_SEMAPHORE
    519 	/* Initialize posix semaphores */
    520 	ksem_init();
    521 #endif
    522 
    523 #if NVERIEXEC > 0
    524 	/*
    525 	 * Initialise the Veriexec subsystem.
    526 	 */
    527 	veriexec_init();
    528 #endif /* NVERIEXEC > 0 */
    529 
    530 #if defined(PAX_MPROTECT) || defined(PAX_SEGVGUARD) || defined(PAX_ASLR)
    531 	pax_init();
    532 #endif /* PAX_MPROTECT || PAX_SEGVGUARD || PAX_ASLR */
    533 
    534 #ifdef	FAST_IPSEC
    535 	/* Attach network crypto subsystem */
    536 	ipsec_attach();
    537 #endif
    538 
    539 	/*
    540 	 * Initialize protocols.  Block reception of incoming packets
    541 	 * until everything is ready.
    542 	 */
    543 	s = splnet();
    544 	ifinit();
    545 	domaininit();
    546 	if_attachdomain();
    547 	splx(s);
    548 
    549 #ifdef GPROF
    550 	/* Initialize kernel profiling. */
    551 	kmstartup();
    552 #endif
    553 
    554 	/* Initialize system accounting. */
    555 	acct_init();
    556 
    557 #ifndef PIPE_SOCKETPAIR
    558 	/* Initialize pipes. */
    559 	pipe_init();
    560 #endif
    561 
    562 #ifdef KTRACE
    563 	/* Initialize ktrace. */
    564 	ktrinit();
    565 #endif
    566 
    567 	/* Initialize the UUID system calls. */
    568 	uuid_init();
    569 
    570 	/*
    571 	 * Create process 1 (init(8)).  We do this now, as Unix has
    572 	 * historically had init be process 1, and changing this would
    573 	 * probably upset a lot of people.
    574 	 *
    575 	 * Note that process 1 won't immediately exec init(8), but will
    576 	 * wait for us to inform it that the root file system has been
    577 	 * mounted.
    578 	 */
    579 	if (fork1(l, 0, SIGCHLD, NULL, 0, start_init, NULL, NULL, &initproc))
    580 		panic("fork init");
    581 
    582 	/*
    583 	 * Load any remaining builtin modules, and hand back temporary
    584 	 * storage to the VM system.
    585 	 */
    586 	module_init_class(MODULE_CLASS_ANY);
    587 
    588 	/*
    589 	 * Finalize configuration now that all real devices have been
    590 	 * found.  This needs to be done before the root device is
    591 	 * selected, since finalization may create the root device.
    592 	 */
    593 	config_finalize();
    594 
    595 	/*
    596 	 * Now that autoconfiguration has completed, we can determine
    597 	 * the root and dump devices.
    598 	 */
    599 	cpu_rootconf();
    600 	cpu_dumpconf();
    601 
    602 	/* Mount the root file system. */
    603 	do {
    604 		domountroothook();
    605 		if ((error = vfs_mountroot())) {
    606 			printf("cannot mount root, error = %d\n", error);
    607 			boothowto |= RB_ASKNAME;
    608 			setroot(root_device,
    609 			    (rootdev != NODEV) ? DISKPART(rootdev) : 0);
    610 		}
    611 	} while (error != 0);
    612 	mountroothook_destroy();
    613 
    614 	/*
    615 	 * Initialise the time-of-day clock, passing the time recorded
    616 	 * in the root filesystem (if any) for use by systems that
    617 	 * don't have a non-volatile time-of-day device.
    618 	 */
    619 	inittodr(rootfstime);
    620 
    621 	CIRCLEQ_FIRST(&mountlist)->mnt_flag |= MNT_ROOTFS;
    622 	CIRCLEQ_FIRST(&mountlist)->mnt_op->vfs_refcount++;
    623 
    624 	/*
    625 	 * Get the vnode for '/'.  Set filedesc0.fd_fd.fd_cdir to
    626 	 * reference it.
    627 	 */
    628 	error = VFS_ROOT(CIRCLEQ_FIRST(&mountlist), &rootvnode);
    629 	if (error)
    630 		panic("cannot find root vnode, error=%d", error);
    631 	cwdi0.cwdi_cdir = rootvnode;
    632 	VREF(cwdi0.cwdi_cdir);
    633 	VOP_UNLOCK(rootvnode, 0);
    634 	cwdi0.cwdi_rdir = NULL;
    635 
    636 	/*
    637 	 * Now that root is mounted, we can fixup initproc's CWD
    638 	 * info.  All other processes are kthreads, which merely
    639 	 * share proc0's CWD info.
    640 	 */
    641 	initproc->p_cwdi->cwdi_cdir = rootvnode;
    642 	VREF(initproc->p_cwdi->cwdi_cdir);
    643 	initproc->p_cwdi->cwdi_rdir = NULL;
    644 
    645 	/*
    646 	 * Now can look at time, having had a chance to verify the time
    647 	 * from the file system.  Reset l->l_rtime as it may have been
    648 	 * munched in mi_switch() after the time got set.
    649 	 */
    650 	getmicrotime(&time);
    651 	boottime = time;
    652 	mutex_enter(proc_lock);
    653 	LIST_FOREACH(p, &allproc, p_list) {
    654 		KASSERT((p->p_flag & PK_MARKER) == 0);
    655 		mutex_enter(p->p_lock);
    656 		p->p_stats->p_start = time;
    657 		LIST_FOREACH(l, &p->p_lwps, l_sibling) {
    658 			lwp_lock(l);
    659 			memset(&l->l_rtime, 0, sizeof(l->l_rtime));
    660 			lwp_unlock(l);
    661 		}
    662 		mutex_exit(p->p_lock);
    663 	}
    664 	mutex_exit(proc_lock);
    665 	binuptime(&curlwp->l_stime);
    666 
    667 	for (CPU_INFO_FOREACH(cii, ci)) {
    668 		ci->ci_schedstate.spc_lastmod = time_second;
    669 	}
    670 
    671 	/* Create the pageout daemon kernel thread. */
    672 	uvm_swap_init();
    673 	if (kthread_create(PRI_PGDAEMON, KTHREAD_MPSAFE, NULL, uvm_pageout,
    674 	    NULL, NULL, "pgdaemon"))
    675 		panic("fork pagedaemon");
    676 
    677 	/* Create the filesystem syncer kernel thread. */
    678 	if (kthread_create(PRI_IOFLUSH, KTHREAD_MPSAFE, NULL, sched_sync,
    679 	    NULL, NULL, "ioflush"))
    680 		panic("fork syncer");
    681 
    682 	/* Create the aiodone daemon kernel thread. */
    683 	if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
    684 	    uvm_aiodone_worker, NULL, PRI_VM, IPL_NONE, WQ_MPSAFE))
    685 		panic("fork aiodoned");
    686 
    687 	vmem_rehash_start();
    688 
    689 	/* Initialize exec structures */
    690 	exec_init(1);
    691 
    692 	/*
    693 	 * Okay, now we can let init(8) exec!  It's off to userland!
    694 	 */
    695 	mutex_enter(proc_lock);
    696 	start_init_exec = 1;
    697 	cv_broadcast(&lbolt);
    698 	mutex_exit(proc_lock);
    699 
    700 	/* The scheduler is an infinite loop. */
    701 	uvm_scheduler();
    702 	/* NOTREACHED */
    703 }
    704 
    705 static void
    706 check_console(struct lwp *l)
    707 {
    708 	struct nameidata nd;
    709 	int error;
    710 
    711 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/console");
    712 	error = namei(&nd);
    713 	if (error == 0)
    714 		vrele(nd.ni_vp);
    715 	else if (error == ENOENT)
    716 		printf("warning: no /dev/console\n");
    717 	else
    718 		printf("warning: lookup /dev/console: error %d\n", error);
    719 }
    720 
    721 /*
    722  * List of paths to try when searching for "init".
    723  */
    724 static const char * const initpaths[] = {
    725 	"/sbin/init",
    726 	"/sbin/oinit",
    727 	"/sbin/init.bak",
    728 	NULL,
    729 };
    730 
    731 /*
    732  * Start the initial user process; try exec'ing each pathname in "initpaths".
    733  * The program is invoked with one argument containing the boot flags.
    734  */
    735 static void
    736 start_init(void *arg)
    737 {
    738 	struct lwp *l = arg;
    739 	struct proc *p = l->l_proc;
    740 	vaddr_t addr;
    741 	struct sys_execve_args /* {
    742 		syscallarg(const char *) path;
    743 		syscallarg(char * const *) argp;
    744 		syscallarg(char * const *) envp;
    745 	} */ args;
    746 	int options, i, error;
    747 	register_t retval[2];
    748 	char flags[4], *flagsp;
    749 	const char *path, *slash;
    750 	char *ucp, **uap, *arg0, *arg1 = NULL;
    751 	char ipath[129];
    752 	int ipx, len;
    753 
    754 	/*
    755 	 * Now in process 1.
    756 	 */
    757 	strncpy(p->p_comm, "init", MAXCOMLEN);
    758 
    759 	/*
    760 	 * Wait for main() to tell us that it's safe to exec.
    761 	 */
    762 	mutex_enter(proc_lock);
    763 	while (start_init_exec == 0)
    764 		cv_wait(&lbolt, proc_lock);
    765 	mutex_exit(proc_lock);
    766 
    767 	/*
    768 	 * This is not the right way to do this.  We really should
    769 	 * hand-craft a descriptor onto /dev/console to hand to init,
    770 	 * but that's a _lot_ more work, and the benefit from this easy
    771 	 * hack makes up for the "good is the enemy of the best" effect.
    772 	 */
    773 	check_console(l);
    774 
    775 	/*
    776 	 * Need just enough stack to hold the faked-up "execve()" arguments.
    777 	 */
    778 	addr = (vaddr_t)STACK_ALLOC(USRSTACK, PAGE_SIZE);
    779 	if (uvm_map(&p->p_vmspace->vm_map, &addr, PAGE_SIZE,
    780                     NULL, UVM_UNKNOWN_OFFSET, 0,
    781                     UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY,
    782 		    UVM_ADV_NORMAL,
    783                     UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW)) != 0)
    784 		panic("init: couldn't allocate argument space");
    785 	p->p_vmspace->vm_maxsaddr = (void *)STACK_MAX(addr, PAGE_SIZE);
    786 
    787 	ipx = 0;
    788 	while (1) {
    789 		if (boothowto & RB_ASKNAME) {
    790 			printf("init path");
    791 			if (initpaths[ipx])
    792 				printf(" (default %s)", initpaths[ipx]);
    793 			printf(": ");
    794 			len = cngetsn(ipath, sizeof(ipath)-1);
    795 			if (len == 0) {
    796 				if (initpaths[ipx])
    797 					path = initpaths[ipx++];
    798 				else
    799 					continue;
    800 			} else {
    801 				ipath[len] = '\0';
    802 				path = ipath;
    803 			}
    804 		} else {
    805 			if ((path = initpaths[ipx++]) == NULL)
    806 				break;
    807 		}
    808 
    809 		ucp = (char *)USRSTACK;
    810 
    811 		/*
    812 		 * Construct the boot flag argument.
    813 		 */
    814 		flagsp = flags;
    815 		*flagsp++ = '-';
    816 		options = 0;
    817 
    818 		if (boothowto & RB_SINGLE) {
    819 			*flagsp++ = 's';
    820 			options = 1;
    821 		}
    822 #ifdef notyet
    823 		if (boothowto & RB_FASTBOOT) {
    824 			*flagsp++ = 'f';
    825 			options = 1;
    826 		}
    827 #endif
    828 
    829 		/*
    830 		 * Move out the flags (arg 1), if necessary.
    831 		 */
    832 		if (options != 0) {
    833 			*flagsp++ = '\0';
    834 			i = flagsp - flags;
    835 #ifdef DEBUG
    836 			printf("init: copying out flags `%s' %d\n", flags, i);
    837 #endif
    838 			arg1 = STACK_ALLOC(ucp, i);
    839 			ucp = STACK_MAX(arg1, i);
    840 			(void)copyout((void *)flags, arg1, i);
    841 		}
    842 
    843 		/*
    844 		 * Move out the file name (also arg 0).
    845 		 */
    846 		i = strlen(path) + 1;
    847 #ifdef DEBUG
    848 		printf("init: copying out path `%s' %d\n", path, i);
    849 #else
    850 		if (boothowto & RB_ASKNAME || path != initpaths[0])
    851 			printf("init: trying %s\n", path);
    852 #endif
    853 		arg0 = STACK_ALLOC(ucp, i);
    854 		ucp = STACK_MAX(arg0, i);
    855 		(void)copyout(path, arg0, i);
    856 
    857 		/*
    858 		 * Move out the arg pointers.
    859 		 */
    860 		ucp = (void *)STACK_ALIGN(ucp, ALIGNBYTES);
    861 		uap = (char **)STACK_ALLOC(ucp, sizeof(char *) * 3);
    862 		SCARG(&args, path) = arg0;
    863 		SCARG(&args, argp) = uap;
    864 		SCARG(&args, envp) = NULL;
    865 		slash = strrchr(path, '/');
    866 		if (slash)
    867 			(void)suword((void *)uap++,
    868 			    (long)arg0 + (slash + 1 - path));
    869 		else
    870 			(void)suword((void *)uap++, (long)arg0);
    871 		if (options != 0)
    872 			(void)suword((void *)uap++, (long)arg1);
    873 		(void)suword((void *)uap++, 0);	/* terminator */
    874 
    875 		/*
    876 		 * Now try to exec the program.  If can't for any reason
    877 		 * other than it doesn't exist, complain.
    878 		 */
    879 		error = sys_execve(l, &args, retval);
    880 		if (error == 0 || error == EJUSTRETURN) {
    881 			KERNEL_UNLOCK_LAST(l);
    882 			return;
    883 		}
    884 		printf("exec %s: error %d\n", path, error);
    885 	}
    886 	printf("init: not found\n");
    887 	panic("no init");
    888 }
    889 
    890 /*
    891  * calculate cache size from physmem and vm_map size.
    892  */
    893 vaddr_t
    894 calc_cache_size(struct vm_map *map, int pct, int va_pct)
    895 {
    896 	paddr_t t;
    897 
    898 	/* XXX should consider competing cache if any */
    899 	/* XXX should consider submaps */
    900 	t = (uintmax_t)physmem * pct / 100 * PAGE_SIZE;
    901 	if (map != NULL) {
    902 		vsize_t vsize;
    903 
    904 		vsize = vm_map_max(map) - vm_map_min(map);
    905 		vsize = (uintmax_t)vsize * va_pct / 100;
    906 		if (t > vsize) {
    907 			t = vsize;
    908 		}
    909 	}
    910 	return t;
    911 }
    912