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