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