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