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