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