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