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