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