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