Home | History | Annotate | Line # | Download | only in kern
init_main.c revision 1.441
      1 /*	$NetBSD: init_main.c,v 1.441 2012/02/02 19:43:07 tls Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2008, 2009 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.441 2012/02/02 19:43:07 tls 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_sa.h"
    108 #include "opt_syscall_debug.h"
    109 #include "opt_sysv.h"
    110 #include "opt_fileassoc.h"
    111 #include "opt_ktrace.h"
    112 #include "opt_pax.h"
    113 #include "opt_compat_netbsd.h"
    114 #include "opt_wapbl.h"
    115 #include "opt_ptrace.h"
    116 
    117 #include "drvctl.h"
    118 #include "ksyms.h"
    119 
    120 #include "sysmon_envsys.h"
    121 #include "sysmon_power.h"
    122 #include "sysmon_taskq.h"
    123 #include "sysmon_wdog.h"
    124 #include "veriexec.h"
    125 
    126 #include <sys/param.h>
    127 #include <sys/acct.h>
    128 #include <sys/filedesc.h>
    129 #include <sys/file.h>
    130 #include <sys/errno.h>
    131 #include <sys/callout.h>
    132 #include <sys/cpu.h>
    133 #include <sys/cpufreq.h>
    134 #include <sys/spldebug.h>
    135 #include <sys/kernel.h>
    136 #include <sys/mount.h>
    137 #include <sys/proc.h>
    138 #include <sys/kthread.h>
    139 #include <sys/resourcevar.h>
    140 #include <sys/signalvar.h>
    141 #include <sys/systm.h>
    142 #include <sys/vnode.h>
    143 #include <sys/fstrans.h>
    144 #include <sys/tty.h>
    145 #include <sys/conf.h>
    146 #include <sys/disklabel.h>
    147 #include <sys/buf.h>
    148 #include <sys/device.h>
    149 #include <sys/exec.h>
    150 #include <sys/socketvar.h>
    151 #include <sys/protosw.h>
    152 #include <sys/percpu.h>
    153 #include <sys/pserialize.h>
    154 #include <sys/pset.h>
    155 #include <sys/sysctl.h>
    156 #include <sys/reboot.h>
    157 #include <sys/event.h>
    158 #include <sys/mbuf.h>
    159 #include <sys/sched.h>
    160 #include <sys/sleepq.h>
    161 #include <sys/iostat.h>
    162 #include <sys/vmem.h>
    163 #include <sys/uuid.h>
    164 #include <sys/extent.h>
    165 #include <sys/disk.h>
    166 #include <sys/msgbuf.h>
    167 #include <sys/module.h>
    168 #include <sys/event.h>
    169 #include <sys/lockf.h>
    170 #include <sys/once.h>
    171 #include <sys/kcpuset.h>
    172 #include <sys/ksyms.h>
    173 #include <sys/uidinfo.h>
    174 #include <sys/kprintf.h>
    175 #ifdef FAST_IPSEC
    176 #include <netipsec/ipsec.h>
    177 #endif
    178 #ifdef SYSVSHM
    179 #include <sys/shm.h>
    180 #endif
    181 #ifdef SYSVSEM
    182 #include <sys/sem.h>
    183 #endif
    184 #ifdef SYSVMSG
    185 #include <sys/msg.h>
    186 #endif
    187 #include <sys/domain.h>
    188 #include <sys/namei.h>
    189 #include <sys/rnd.h>
    190 #include <sys/pipe.h>
    191 #if NVERIEXEC > 0
    192 #include <sys/verified_exec.h>
    193 #endif /* NVERIEXEC > 0 */
    194 #ifdef KTRACE
    195 #include <sys/ktrace.h>
    196 #endif
    197 #include <sys/kauth.h>
    198 #ifdef KERN_SA
    199 #include <sys/savar.h>
    200 #endif
    201 #include <net80211/ieee80211_netbsd.h>
    202 #ifdef PTRACE
    203 #include <sys/ptrace.h>
    204 #endif /* PTRACE */
    205 #include <sys/cprng.h>
    206 
    207 #include <sys/syscall.h>
    208 #include <sys/syscallargs.h>
    209 
    210 #if defined(PAX_MPROTECT) || defined(PAX_SEGVGUARD) || defined(PAX_ASLR)
    211 #include <sys/pax.h>
    212 #endif /* PAX_MPROTECT || PAX_SEGVGUARD || PAX_ASLR */
    213 
    214 #include <secmodel/secmodel.h>
    215 
    216 #include <ufs/ufs/quota.h>
    217 
    218 #include <miscfs/genfs/genfs.h>
    219 #include <miscfs/syncfs/syncfs.h>
    220 #include <miscfs/specfs/specdev.h>
    221 
    222 #include <sys/cpu.h>
    223 
    224 #include <uvm/uvm.h>	/* extern struct uvm uvm */
    225 
    226 #if NSYSMON_TASKQ > 0
    227 #include <dev/sysmon/sysmon_taskq.h>
    228 #endif
    229 
    230 #include <dev/cons.h>
    231 
    232 #if NSYSMON_ENVSYS > 0 || NSYSMON_POWER > 0 || NSYSMON_WDOG > 0
    233 #include <dev/sysmon/sysmonvar.h>
    234 #endif
    235 
    236 #include <net/bpf.h>
    237 #include <net/if.h>
    238 #include <net/raw_cb.h>
    239 
    240 #include <prop/proplib.h>
    241 
    242 #ifdef COMPAT_50
    243 #include <compat/sys/time.h>
    244 struct timeval50 boottime50;
    245 #endif
    246 
    247 #include <sys/userconf.h>
    248 
    249 extern struct lwp lwp0;
    250 extern time_t rootfstime;
    251 
    252 #ifndef curlwp
    253 struct	lwp *curlwp = &lwp0;
    254 #endif
    255 struct	proc *initproc;
    256 
    257 struct	vnode *rootvp, *swapdev_vp;
    258 int	boothowto;
    259 int	cold = 1;			/* still working on startup */
    260 struct timespec boottime;	        /* time at system startup - will only follow settime deltas */
    261 
    262 int	start_init_exec;		/* semaphore for start_init() */
    263 
    264 cprng_strong_t	*kern_cprng;
    265 
    266 static void check_console(struct lwp *l);
    267 static void start_init(void *);
    268 static void configure(void);
    269 static void configure2(void);
    270 static void configure3(void);
    271 void main(void);
    272 
    273 /*
    274  * System startup; initialize the world, create process 0, mount root
    275  * filesystem, and fork to create init and pagedaemon.  Most of the
    276  * hard work is done in the lower-level initialization routines including
    277  * startup(), which does memory initialization and autoconfiguration.
    278  */
    279 void
    280 main(void)
    281 {
    282 	struct timespec time;
    283 	struct lwp *l;
    284 	struct proc *p;
    285 	int s, error;
    286 #ifdef NVNODE_IMPLICIT
    287 	int usevnodes;
    288 #endif
    289 	CPU_INFO_ITERATOR cii;
    290 	struct cpu_info *ci;
    291 
    292 	l = &lwp0;
    293 #ifndef LWP0_CPU_INFO
    294 	l->l_cpu = curcpu();
    295 #endif
    296 	l->l_pflag |= LP_RUNNING;
    297 
    298 	/*
    299 	 * Attempt to find console and initialize
    300 	 * in case of early panic or other messages.
    301 	 */
    302 	consinit();
    303 
    304 	kernel_lock_init();
    305 	once_init();
    306 
    307 	mi_cpu_init();
    308 	kernconfig_lock_init();
    309 	kthread_sysinit();
    310 
    311 	/* Initialize the device switch tables. */
    312 	devsw_init();
    313 
    314 	/* Initialize event counters. */
    315 	evcnt_init();
    316 
    317 	uvm_init();
    318 	kcpuset_sysinit();
    319 
    320 	prop_kern_init();
    321 
    322 #if ((NKSYMS > 0) || (NDDB > 0) || (NMODULAR > 0))
    323 	ksyms_init();
    324 #endif
    325 	kprintf_init();
    326 
    327 	percpu_init();
    328 
    329 	/* Initialize lock caches. */
    330 	mutex_obj_init();
    331 	rw_obj_init();
    332 
    333 	/* Passive serialization. */
    334 	pserialize_init();
    335 
    336 	/* Initialize the extent manager. */
    337 	extent_init();
    338 
    339 	/* Do machine-dependent initialization. */
    340 	cpu_startup();
    341 
    342 	/* Initialize the sysctl subsystem. */
    343 	sysctl_init();
    344 
    345 	/* Initialize callouts, part 1. */
    346 	callout_startup();
    347 
    348 	/* Initialize the kernel authorization subsystem. */
    349 	kauth_init();
    350 
    351 	secmodel_init();
    352 
    353 	spec_init();
    354 
    355 	/*
    356 	 * Set BPF op vector.  Can't do this in bpf attach, since
    357 	 * network drivers attach before bpf.
    358 	 */
    359 	bpf_setops();
    360 
    361 	/* Start module system. */
    362 	module_init();
    363 
    364 	/*
    365 	 * Initialize the kernel authorization subsystem and start the
    366 	 * default security model, if any. We need to do this early
    367 	 * enough so that subsystems relying on any of the aforementioned
    368 	 * can work properly. Since the security model may dictate the
    369 	 * credential inheritance policy, it is needed at least before
    370 	 * any process is created, specifically proc0.
    371 	 */
    372 	module_init_class(MODULE_CLASS_SECMODEL);
    373 
    374 	/* Initialize the buffer cache */
    375 	bufinit();
    376 
    377 	/* Initialize sockets. */
    378 	soinit();
    379 
    380 	/*
    381 	 * The following things must be done before autoconfiguration.
    382 	 */
    383 	rnd_init();		/* initialize entropy pool */
    384 
    385 	cprng_init();		/* initialize cryptographic PRNG */
    386 
    387 	/* Initialize process and pgrp structures. */
    388 #ifdef KERN_SA
    389 	sa_init();
    390 #endif
    391 	procinit();
    392 	lwpinit();
    393 
    394 	/* Initialize signal-related data structures. */
    395 	signal_init();
    396 
    397 	/* Initialize resource management. */
    398 	resource_init();
    399 
    400 	/* Create process 0. */
    401 	proc0_init();
    402 	lwp0_init();
    403 
    404 	/* Disable preemption during boot. */
    405 	kpreempt_disable();
    406 
    407 	/* Initialize the UID hash table. */
    408 	uid_init();
    409 
    410 	/* Charge root for one process. */
    411 	(void)chgproccnt(0, 1);
    412 
    413 	/* Initialize timekeeping. */
    414 	time_init();
    415 
    416 	/* Initialize the run queues, turnstiles and sleep queues. */
    417 	sched_rqinit();
    418 	turnstile_init();
    419 	sleeptab_init(&sleeptab);
    420 
    421 	sched_init();
    422 
    423 	/* Initialize processor-sets */
    424 	psets_init();
    425 
    426 	/* Initialize cpufreq(9) */
    427 	cpufreq_init();
    428 
    429 	/* MI initialization of the boot cpu */
    430 	error = mi_cpu_attach(curcpu());
    431 	KASSERT(error == 0);
    432 
    433 	/* Initialize timekeeping, part 2. */
    434 	time_init2();
    435 
    436 	/*
    437 	 * Initialize mbuf's.  Do this now because we might attempt to
    438 	 * allocate mbufs or mbuf clusters during autoconfiguration.
    439 	 */
    440 	mbinit();
    441 
    442 	/* Initialize I/O statistics. */
    443 	iostat_init();
    444 
    445 	/* Initialize the log device. */
    446 	loginit();
    447 
    448 	/* Second part of module system initialization. */
    449 	module_start_unload_thread();
    450 
    451 	/* Initialize the file systems. */
    452 #ifdef NVNODE_IMPLICIT
    453 	/*
    454 	 * If maximum number of vnodes in namei vnode cache is not explicitly
    455 	 * defined in kernel config, adjust the number such as we use roughly
    456 	 * 10% of memory for vnodes and associated data structures in the
    457 	 * assumed worst case.  Do not provide fewer than NVNODE vnodes.
    458 	 */
    459 	usevnodes =
    460 	    calc_cache_size(kernel_map, 10, VNODE_VA_MAXPCT) / VNODE_COST;
    461 	if (usevnodes > desiredvnodes)
    462 		desiredvnodes = usevnodes;
    463 #endif
    464 	vfsinit();
    465 	lf_init();
    466 
    467 	/* Initialize fstrans. */
    468 	fstrans_init();
    469 
    470 	/* Initialize the file descriptor system. */
    471 	fd_sys_init();
    472 
    473 	/* Initialize cwd structures */
    474 	cwd_sys_init();
    475 
    476 	/* Initialize kqueue. */
    477 	kqueue_init();
    478 
    479 	/* Initialize the system monitor subsystems. */
    480 #if NSYSMON_TASKQ > 0
    481 	sysmon_task_queue_preinit();
    482 #endif
    483 
    484 #if NSYSMON_ENVSYS > 0
    485 	sysmon_envsys_init();
    486 #endif
    487 
    488 #if NSYSMON_POWER > 0
    489 	sysmon_power_init();
    490 #endif
    491 
    492 #if NSYSMON_WDOG > 0
    493 	sysmon_wdog_init();
    494 #endif
    495 
    496 	inittimecounter();
    497 	ntp_init();
    498 
    499 	/* Initialize tty subsystem. */
    500 	tty_init();
    501 	ttyldisc_init();
    502 
    503 	/* Initialize the buffer cache, part 2. */
    504 	bufinit2();
    505 
    506 	/* Initialize the disk wedge subsystem. */
    507 	dkwedge_init();
    508 
    509 	/* Initialize the kernel strong PRNG. */
    510 	kern_cprng = cprng_strong_create("kernel", IPL_VM,
    511 					 CPRNG_INIT_ANY|CPRNG_REKEY_ANY);
    512 
    513 	/* Initialize interfaces. */
    514 	ifinit1();
    515 
    516 	spldebug_start();
    517 
    518 	/* Initialize sockets thread(s) */
    519 	soinit1();
    520 
    521 	/* Configure the system hardware.  This will enable interrupts. */
    522 	configure();
    523 
    524 	ssp_init();
    525 
    526 	ubc_init();		/* must be after autoconfig */
    527 
    528 	mm_init();
    529 
    530 	configure2();
    531 	/* Now timer is working.  Enable preemption. */
    532 	kpreempt_enable();
    533 
    534 #ifdef SYSVSHM
    535 	/* Initialize System V style shared memory. */
    536 	shminit();
    537 #endif
    538 
    539 	vmem_rehash_start();	/* must be before exec_init */
    540 
    541 	/* Initialize exec structures */
    542 	exec_init(1);		/* seminit calls exithook_establish() */
    543 
    544 #ifdef SYSVSEM
    545 	/* Initialize System V style semaphores. */
    546 	seminit();
    547 #endif
    548 
    549 #ifdef SYSVMSG
    550 	/* Initialize System V style message queues. */
    551 	msginit();
    552 #endif
    553 
    554 #if NVERIEXEC > 0
    555 	/*
    556 	 * Initialise the Veriexec subsystem.
    557 	 */
    558 	veriexec_init();
    559 #endif /* NVERIEXEC > 0 */
    560 
    561 #if defined(PAX_MPROTECT) || defined(PAX_SEGVGUARD) || defined(PAX_ASLR)
    562 	pax_init();
    563 #endif /* PAX_MPROTECT || PAX_SEGVGUARD || PAX_ASLR */
    564 
    565 #ifdef	FAST_IPSEC
    566 	/* Attach network crypto subsystem */
    567 	ipsec_attach();
    568 #endif
    569 
    570 	/*
    571 	 * Initialize protocols.  Block reception of incoming packets
    572 	 * until everything is ready.
    573 	 */
    574 	s = splnet();
    575 	ifinit();
    576 	domaininit(true);
    577 	if_attachdomain();
    578 	splx(s);
    579 
    580 #ifdef GPROF
    581 	/* Initialize kernel profiling. */
    582 	kmstartup();
    583 #endif
    584 
    585 	/* Initialize system accounting. */
    586 	acct_init();
    587 
    588 #ifndef PIPE_SOCKETPAIR
    589 	/* Initialize pipes. */
    590 	pipe_init();
    591 #endif
    592 
    593 #ifdef KTRACE
    594 	/* Initialize ktrace. */
    595 	ktrinit();
    596 #endif
    597 
    598 #ifdef PTRACE
    599 	/* Initialize ptrace. */
    600 	ptrace_init();
    601 #endif /* PTRACE */
    602 
    603 	/* Initialize the UUID system calls. */
    604 	uuid_init();
    605 
    606 	machdep_init();
    607 
    608 	procinit_sysctl();
    609 
    610 	/*
    611 	 * Create process 1 (init(8)).  We do this now, as Unix has
    612 	 * historically had init be process 1, and changing this would
    613 	 * probably upset a lot of people.
    614 	 *
    615 	 * Note that process 1 won't immediately exec init(8), but will
    616 	 * wait for us to inform it that the root file system has been
    617 	 * mounted.
    618 	 */
    619 	if (fork1(l, 0, SIGCHLD, NULL, 0, start_init, NULL, NULL, &initproc))
    620 		panic("fork init");
    621 
    622 	/*
    623 	 * Load any remaining builtin modules, and hand back temporary
    624 	 * storage to the VM system.  Then require force when loading any
    625 	 * remaining un-init'ed built-in modules to avoid later surprises.
    626 	 */
    627 	module_init_class(MODULE_CLASS_ANY);
    628 	module_builtin_require_force();
    629 
    630 	/*
    631 	 * Finalize configuration now that all real devices have been
    632 	 * found.  This needs to be done before the root device is
    633 	 * selected, since finalization may create the root device.
    634 	 */
    635 	config_finalize();
    636 
    637 	sysctl_finalize();
    638 
    639 	/*
    640 	 * Now that autoconfiguration has completed, we can determine
    641 	 * the root and dump devices.
    642 	 */
    643 	cpu_rootconf();
    644 	cpu_dumpconf();
    645 
    646 	/* Mount the root file system. */
    647 	do {
    648 		domountroothook(root_device);
    649 		if ((error = vfs_mountroot())) {
    650 			printf("cannot mount root, error = %d\n", error);
    651 			boothowto |= RB_ASKNAME;
    652 			setroot(root_device,
    653 			    (rootdev != NODEV) ? DISKPART(rootdev) : 0);
    654 		}
    655 	} while (error != 0);
    656 	mountroothook_destroy();
    657 
    658 	configure3();
    659 
    660 	/*
    661 	 * Initialise the time-of-day clock, passing the time recorded
    662 	 * in the root filesystem (if any) for use by systems that
    663 	 * don't have a non-volatile time-of-day device.
    664 	 */
    665 	inittodr(rootfstime);
    666 
    667 	/*
    668 	 * Now can look at time, having had a chance to verify the time
    669 	 * from the file system.  Reset l->l_rtime as it may have been
    670 	 * munched in mi_switch() after the time got set.
    671 	 */
    672 	getnanotime(&time);
    673 	boottime = time;
    674 #ifdef COMPAT_50
    675 	{
    676 		struct timeval tv;
    677 		TIMESPEC_TO_TIMEVAL(&tv, &time);
    678 		timeval_to_timeval50(&tv, &boottime50);
    679 	}
    680 #endif
    681 	mutex_enter(proc_lock);
    682 	LIST_FOREACH(p, &allproc, p_list) {
    683 		KASSERT((p->p_flag & PK_MARKER) == 0);
    684 		mutex_enter(p->p_lock);
    685 		TIMESPEC_TO_TIMEVAL(&p->p_stats->p_start, &time);
    686 		LIST_FOREACH(l, &p->p_lwps, l_sibling) {
    687 			lwp_lock(l);
    688 			memset(&l->l_rtime, 0, sizeof(l->l_rtime));
    689 			lwp_unlock(l);
    690 		}
    691 		mutex_exit(p->p_lock);
    692 	}
    693 	mutex_exit(proc_lock);
    694 	binuptime(&curlwp->l_stime);
    695 
    696 	for (CPU_INFO_FOREACH(cii, ci)) {
    697 		ci->ci_schedstate.spc_lastmod = time_second;
    698 	}
    699 
    700 	/* Create the pageout daemon kernel thread. */
    701 	uvm_swap_init();
    702 	if (kthread_create(PRI_PGDAEMON, KTHREAD_MPSAFE, NULL, uvm_pageout,
    703 	    NULL, NULL, "pgdaemon"))
    704 		panic("fork pagedaemon");
    705 
    706 	/* Create the filesystem syncer kernel thread. */
    707 	if (kthread_create(PRI_IOFLUSH, KTHREAD_MPSAFE, NULL, sched_sync,
    708 	    NULL, NULL, "ioflush"))
    709 		panic("fork syncer");
    710 
    711 	/* Create the aiodone daemon kernel thread. */
    712 	if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
    713 	    uvm_aiodone_worker, NULL, PRI_VM, IPL_NONE, WQ_MPSAFE))
    714 		panic("fork aiodoned");
    715 
    716 	/*
    717 	 * Okay, now we can let init(8) exec!  It's off to userland!
    718 	 */
    719 	mutex_enter(proc_lock);
    720 	start_init_exec = 1;
    721 	cv_broadcast(&lbolt);
    722 	mutex_exit(proc_lock);
    723 
    724 	/* The scheduler is an infinite loop. */
    725 	uvm_scheduler();
    726 	/* NOTREACHED */
    727 }
    728 
    729 /*
    730  * Configure the system's hardware.
    731  */
    732 static void
    733 configure(void)
    734 {
    735 
    736 	/* Initialize autoconf data structures. */
    737 	config_init_mi();
    738 	/*
    739 	 * XXX
    740 	 * callout_setfunc() requires mutex(9) so it can't be in config_init()
    741 	 * on amiga and atari which use config_init() and autoconf(9) fucntions
    742 	 * to initialize console devices.
    743 	 */
    744 	config_twiddle_init();
    745 
    746 	pmf_init();
    747 #if NDRVCTL > 0
    748 	drvctl_init();
    749 #endif
    750 
    751 	userconf_init();
    752 	if (boothowto & RB_USERCONF)
    753 		userconf_prompt();
    754 
    755 	if ((boothowto & (AB_SILENT|AB_VERBOSE)) == AB_SILENT) {
    756 		printf_nolog("Detecting hardware...");
    757 	}
    758 
    759 	/*
    760 	 * Do the machine-dependent portion of autoconfiguration.  This
    761 	 * sets the configuration machinery here in motion by "finding"
    762 	 * the root bus.  When this function returns, we expect interrupts
    763 	 * to be enabled.
    764 	 */
    765 	cpu_configure();
    766 }
    767 
    768 static void
    769 configure2(void)
    770 {
    771 	CPU_INFO_ITERATOR cii;
    772 	struct cpu_info *ci;
    773 	int s;
    774 
    775 	/*
    776 	 * Now that we've found all the hardware, start the real time
    777 	 * and statistics clocks.
    778 	 */
    779 	initclocks();
    780 
    781 	cold = 0;	/* clocks are running, we're warm now! */
    782 	s = splsched();
    783 	curcpu()->ci_schedstate.spc_flags |= SPCF_RUNNING;
    784 	splx(s);
    785 
    786 	/* Boot the secondary processors. */
    787 	for (CPU_INFO_FOREACH(cii, ci)) {
    788 		uvm_cpu_attach(ci);
    789 	}
    790 	mp_online = true;
    791 #if defined(MULTIPROCESSOR)
    792 	cpu_boot_secondary_processors();
    793 #endif
    794 
    795 	/* Setup the runqueues and scheduler. */
    796 	runq_init();
    797 	synch_init();
    798 
    799 	/*
    800 	 * Bus scans can make it appear as if the system has paused, so
    801 	 * twiddle constantly while config_interrupts() jobs are running.
    802 	 */
    803 	config_twiddle_fn(NULL);
    804 
    805 	/*
    806 	 * Create threads to call back and finish configuration for
    807 	 * devices that want interrupts enabled.
    808 	 */
    809 	config_create_interruptthreads();
    810 
    811 	/* Get the threads going and into any sleeps before continuing. */
    812 	yield();
    813 }
    814 
    815 static void
    816 configure3(void)
    817 {
    818 
    819 	/*
    820 	 * Create threads to call back and finish configuration for
    821 	 * devices that want the mounted root file system.
    822 	 */
    823 	config_create_mountrootthreads();
    824 
    825 	/* Get the threads going and into any sleeps before continuing. */
    826 	yield();
    827 }
    828 
    829 static void
    830 check_console(struct lwp *l)
    831 {
    832 	struct vnode *vp;
    833 	int error;
    834 
    835 	error = namei_simple_kernel("/dev/console",
    836 				NSM_FOLLOW_NOEMULROOT, &vp);
    837 	if (error == 0)
    838 		vrele(vp);
    839 	else if (error == ENOENT)
    840 		printf("warning: no /dev/console\n");
    841 	else
    842 		printf("warning: lookup /dev/console: error %d\n", error);
    843 }
    844 
    845 /*
    846  * List of paths to try when searching for "init".
    847  */
    848 static const char * const initpaths[] = {
    849 	"/sbin/init",
    850 	"/sbin/oinit",
    851 	"/sbin/init.bak",
    852 	NULL,
    853 };
    854 
    855 /*
    856  * Start the initial user process; try exec'ing each pathname in "initpaths".
    857  * The program is invoked with one argument containing the boot flags.
    858  */
    859 static void
    860 start_init(void *arg)
    861 {
    862 	struct lwp *l = arg;
    863 	struct proc *p = l->l_proc;
    864 	vaddr_t addr;
    865 	struct sys_execve_args /* {
    866 		syscallarg(const char *) path;
    867 		syscallarg(char * const *) argp;
    868 		syscallarg(char * const *) envp;
    869 	} */ args;
    870 	int options, i, error;
    871 	register_t retval[2];
    872 	char flags[4], *flagsp;
    873 	const char *path, *slash;
    874 	char *ucp, **uap, *arg0, *arg1 = NULL;
    875 	char ipath[129];
    876 	int ipx, len;
    877 
    878 	/*
    879 	 * Now in process 1.
    880 	 */
    881 	strncpy(p->p_comm, "init", MAXCOMLEN);
    882 
    883 	/*
    884 	 * Wait for main() to tell us that it's safe to exec.
    885 	 */
    886 	mutex_enter(proc_lock);
    887 	while (start_init_exec == 0)
    888 		cv_wait(&lbolt, proc_lock);
    889 	mutex_exit(proc_lock);
    890 
    891 	/*
    892 	 * This is not the right way to do this.  We really should
    893 	 * hand-craft a descriptor onto /dev/console to hand to init,
    894 	 * but that's a _lot_ more work, and the benefit from this easy
    895 	 * hack makes up for the "good is the enemy of the best" effect.
    896 	 */
    897 	check_console(l);
    898 
    899 	/*
    900 	 * Need just enough stack to hold the faked-up "execve()" arguments.
    901 	 */
    902 	addr = (vaddr_t)STACK_ALLOC(USRSTACK, PAGE_SIZE);
    903 	if (uvm_map(&p->p_vmspace->vm_map, &addr, PAGE_SIZE,
    904                     NULL, UVM_UNKNOWN_OFFSET, 0,
    905                     UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY,
    906 		    UVM_ADV_NORMAL,
    907                     UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW)) != 0)
    908 		panic("init: couldn't allocate argument space");
    909 	p->p_vmspace->vm_maxsaddr = (void *)STACK_MAX(addr, PAGE_SIZE);
    910 
    911 	ipx = 0;
    912 	while (1) {
    913 		if (boothowto & RB_ASKNAME) {
    914 			printf("init path");
    915 			if (initpaths[ipx])
    916 				printf(" (default %s)", initpaths[ipx]);
    917 			printf(": ");
    918 			len = cngetsn(ipath, sizeof(ipath)-1);
    919 			if (len == 4 && strcmp(ipath, "halt") == 0) {
    920 				cpu_reboot(RB_HALT, NULL);
    921 			} else if (len == 6 && strcmp(ipath, "reboot") == 0) {
    922 				cpu_reboot(0, NULL);
    923 #if defined(DDB)
    924 			} else if (len == 3 && strcmp(ipath, "ddb") == 0) {
    925 				console_debugger();
    926 				continue;
    927 #endif
    928 			} else if (len > 0 && ipath[0] == '/') {
    929 				ipath[len] = '\0';
    930 				path = ipath;
    931 			} else if (len == 0 && initpaths[ipx] != NULL) {
    932 				path = initpaths[ipx++];
    933 			} else {
    934 				printf("use absolute path, ");
    935 #if defined(DDB)
    936 				printf("\"ddb\", ");
    937 #endif
    938 				printf("\"halt\", or \"reboot\"\n");
    939 				continue;
    940 			}
    941 		} else {
    942 			if ((path = initpaths[ipx++]) == NULL) {
    943 				ipx = 0;
    944 				boothowto |= RB_ASKNAME;
    945 				continue;
    946 			}
    947 		}
    948 
    949 		ucp = (char *)USRSTACK;
    950 
    951 		/*
    952 		 * Construct the boot flag argument.
    953 		 */
    954 		flagsp = flags;
    955 		*flagsp++ = '-';
    956 		options = 0;
    957 
    958 		if (boothowto & RB_SINGLE) {
    959 			*flagsp++ = 's';
    960 			options = 1;
    961 		}
    962 #ifdef notyet
    963 		if (boothowto & RB_FASTBOOT) {
    964 			*flagsp++ = 'f';
    965 			options = 1;
    966 		}
    967 #endif
    968 
    969 		/*
    970 		 * Move out the flags (arg 1), if necessary.
    971 		 */
    972 		if (options != 0) {
    973 			*flagsp++ = '\0';
    974 			i = flagsp - flags;
    975 #ifdef DEBUG
    976 			aprint_normal("init: copying out flags `%s' %d\n", flags, i);
    977 #endif
    978 			arg1 = STACK_ALLOC(ucp, i);
    979 			ucp = STACK_MAX(arg1, i);
    980 			(void)copyout((void *)flags, arg1, i);
    981 		}
    982 
    983 		/*
    984 		 * Move out the file name (also arg 0).
    985 		 */
    986 		i = strlen(path) + 1;
    987 #ifdef DEBUG
    988 		aprint_normal("init: copying out path `%s' %d\n", path, i);
    989 #else
    990 		if (boothowto & RB_ASKNAME || path != initpaths[0])
    991 			printf("init: trying %s\n", path);
    992 #endif
    993 		arg0 = STACK_ALLOC(ucp, i);
    994 		ucp = STACK_MAX(arg0, i);
    995 		(void)copyout(path, arg0, i);
    996 
    997 		/*
    998 		 * Move out the arg pointers.
    999 		 */
   1000 		ucp = (void *)STACK_ALIGN(ucp, STACK_ALIGNBYTES);
   1001 		uap = (char **)STACK_ALLOC(ucp, sizeof(char *) * 3);
   1002 		SCARG(&args, path) = arg0;
   1003 		SCARG(&args, argp) = uap;
   1004 		SCARG(&args, envp) = NULL;
   1005 		slash = strrchr(path, '/');
   1006 		if (slash)
   1007 			(void)suword((void *)uap++,
   1008 			    (long)arg0 + (slash + 1 - path));
   1009 		else
   1010 			(void)suword((void *)uap++, (long)arg0);
   1011 		if (options != 0)
   1012 			(void)suword((void *)uap++, (long)arg1);
   1013 		(void)suword((void *)uap++, 0);	/* terminator */
   1014 
   1015 		/*
   1016 		 * Now try to exec the program.  If can't for any reason
   1017 		 * other than it doesn't exist, complain.
   1018 		 */
   1019 		error = sys_execve(l, &args, retval);
   1020 		if (error == 0 || error == EJUSTRETURN) {
   1021 			KERNEL_UNLOCK_LAST(l);
   1022 			return;
   1023 		}
   1024 		printf("exec %s: error %d\n", path, error);
   1025 	}
   1026 	printf("init: not found\n");
   1027 	panic("no init");
   1028 }
   1029 
   1030 /*
   1031  * calculate cache size (in bytes) from physmem and vm_map size.
   1032  */
   1033 vaddr_t
   1034 calc_cache_size(struct vm_map *map, int pct, int va_pct)
   1035 {
   1036 	paddr_t t;
   1037 
   1038 	/* XXX should consider competing cache if any */
   1039 	/* XXX should consider submaps */
   1040 	t = (uintmax_t)physmem * pct / 100 * PAGE_SIZE;
   1041 	if (map != NULL) {
   1042 		vsize_t vsize;
   1043 
   1044 		vsize = vm_map_max(map) - vm_map_min(map);
   1045 		vsize = (uintmax_t)vsize * va_pct / 100;
   1046 		if (t > vsize) {
   1047 			t = vsize;
   1048 		}
   1049 	}
   1050 	return t;
   1051 }
   1052 
   1053 /*
   1054  * Print the system start up banner.
   1055  *
   1056  * - Print a limited banner if AB_SILENT.
   1057  * - Always send normal banner to the log.
   1058  */
   1059 #define MEM_PBUFSIZE	sizeof("99999 MB")
   1060 
   1061 void
   1062 banner(void)
   1063 {
   1064 	static char notice[] = " Notice: this software is "
   1065 	    "protected by copyright";
   1066 	char pbuf[81];
   1067 	void (*pr)(const char *, ...);
   1068 	int i;
   1069 
   1070 	if ((boothowto & AB_SILENT) != 0) {
   1071 		snprintf(pbuf, sizeof(pbuf), "%s %s (%s)",
   1072 		    ostype, osrelease, kernel_ident);
   1073 		printf_nolog("%s", pbuf);
   1074 		for (i = 80 - strlen(pbuf) - sizeof(notice); i > 0; i--)
   1075 			printf(" ");
   1076 		printf_nolog("%s\n", notice);
   1077 		pr = aprint_normal;
   1078 	} else {
   1079 		pr = printf;
   1080 	}
   1081 
   1082 	memset(pbuf, 0, sizeof(pbuf));
   1083 	(*pr)("%s%s", copyright, version);
   1084 	format_bytes(pbuf, MEM_PBUFSIZE, ctob((uint64_t)physmem));
   1085 	(*pr)("total memory = %s\n", pbuf);
   1086 	format_bytes(pbuf, MEM_PBUFSIZE, ctob((uint64_t)uvmexp.free));
   1087 	(*pr)("avail memory = %s\n", pbuf);
   1088 }
   1089