xref: /netbsd-src/sys/kern/init_main.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: init_main.c,v 1.423 2010/08/21 13:17:31 pgoyette 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.423 2010/08/21 13:17:31 pgoyette Exp $");
101 
102 #include "opt_ddb.h"
103 #include "opt_ipsec.h"
104 #include "opt_modular.h"
105 #include "opt_ntp.h"
106 #include "opt_pipe.h"
107 #include "opt_sa.h"
108 #include "opt_syscall_debug.h"
109 #include "opt_sysv.h"
110 #include "opt_fileassoc.h"
111 #include "opt_ktrace.h"
112 #include "opt_pax.h"
113 #include "opt_compat_netbsd.h"
114 #include "opt_wapbl.h"
115 #include "opt_ptrace.h"
116 
117 #include "drvctl.h"
118 #include "ksyms.h"
119 #include "rnd.h"
120 #include "sysmon_envsys.h"
121 #include "sysmon_power.h"
122 #include "sysmon_taskq.h"
123 #include "sysmon_wdog.h"
124 #include "veriexec.h"
125 
126 #include <sys/param.h>
127 #include <sys/acct.h>
128 #include <sys/filedesc.h>
129 #include <sys/file.h>
130 #include <sys/errno.h>
131 #include <sys/callout.h>
132 #include <sys/cpu.h>
133 #include <sys/spldebug.h>
134 #include <sys/kernel.h>
135 #include <sys/mount.h>
136 #include <sys/proc.h>
137 #include <sys/kthread.h>
138 #include <sys/resourcevar.h>
139 #include <sys/signalvar.h>
140 #include <sys/systm.h>
141 #include <sys/vnode.h>
142 #include <sys/fstrans.h>
143 #include <sys/tty.h>
144 #include <sys/conf.h>
145 #include <sys/disklabel.h>
146 #include <sys/buf.h>
147 #include <sys/device.h>
148 #include <sys/exec.h>
149 #include <sys/socketvar.h>
150 #include <sys/protosw.h>
151 #include <sys/percpu.h>
152 #include <sys/pset.h>
153 #include <sys/sysctl.h>
154 #include <sys/reboot.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 KERN_SA
198 #include <sys/savar.h>
199 #endif
200 #include <net80211/ieee80211_netbsd.h>
201 #ifdef PTRACE
202 #include <sys/ptrace.h>
203 #endif /* PTRACE */
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 #include <miscfs/specfs/specdev.h>
217 
218 #include <sys/cpu.h>
219 
220 #include <uvm/uvm.h>
221 
222 #if NSYSMON_TASKQ > 0
223 #include <dev/sysmon/sysmon_taskq.h>
224 #endif
225 
226 #include <dev/cons.h>
227 
228 #if NSYSMON_ENVSYS > 0 || NSYSMON_POWER > 0 || NSYSMON_WDOG > 0
229 #include <dev/sysmon/sysmonvar.h>
230 #endif
231 
232 #include <net/bpf.h>
233 #include <net/if.h>
234 #include <net/raw_cb.h>
235 
236 #include <prop/proplib.h>
237 
238 #ifdef COMPAT_50
239 #include <compat/sys/time.h>
240 struct timeval50 boottime50;
241 #endif
242 
243 #ifdef _KERNEL_OPT
244 #include "opt_userconf.h"
245 #endif
246 #ifdef USERCONF
247 #include <sys/userconf.h>
248 #endif
249 
250 extern struct lwp lwp0;
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 static void configure3(void);
270 void main(void);
271 
272 /*
273  * System startup; initialize the world, create process 0, mount root
274  * filesystem, and fork to create init and pagedaemon.  Most of the
275  * hard work is done in the lower-level initialization routines including
276  * startup(), which does memory initialization and autoconfiguration.
277  */
278 void
279 main(void)
280 {
281 	struct timespec time;
282 	struct lwp *l;
283 	struct proc *p;
284 	int s, error;
285 #ifdef NVNODE_IMPLICIT
286 	int usevnodes;
287 #endif
288 	CPU_INFO_ITERATOR cii;
289 	struct cpu_info *ci;
290 
291 	l = &lwp0;
292 #ifndef LWP0_CPU_INFO
293 	l->l_cpu = curcpu();
294 #endif
295 	l->l_pflag |= LP_RUNNING;
296 
297 	/*
298 	 * Attempt to find console and initialize
299 	 * in case of early panic or other messages.
300 	 */
301 	consinit();
302 
303 	kernel_lock_init();
304 	once_init();
305 	mutex_init(&cpu_lock, MUTEX_DEFAULT, IPL_NONE);
306 	kernconfig_lock_init();
307 
308 	/* Initialize the device switch tables. */
309 	devsw_init();
310 
311 	uvm_init();
312 
313 	prop_kern_init();
314 
315 #if ((NKSYMS > 0) || (NDDB > 0) || (NMODULAR > 0))
316 	ksyms_init();
317 #endif
318 	kprintf_init();
319 
320 	percpu_init();
321 
322 	/* Initialize lock caches. */
323 	mutex_obj_init();
324 	rw_obj_init();
325 
326 	/* Initialize the extent manager. */
327 	extent_init();
328 
329 	/* Do machine-dependent initialization. */
330 	cpu_startup();
331 
332 	/* Initialize the sysctl subsystem. */
333 	sysctl_init();
334 
335 	/* Initialize callouts, part 1. */
336 	callout_startup();
337 
338 	/* Initialize the kernel authorization subsystem. */
339 	kauth_init();
340 
341 	spec_init();
342 
343 	/*
344 	 * Set BPF op vector.  Can't do this in bpf attach, since
345 	 * network drivers attach before bpf.
346 	 */
347 	bpf_setops();
348 
349 	/* Start module system. */
350 	module_init();
351 
352 	/*
353 	 * Initialize the kernel authorization subsystem and start the
354 	 * default security model, if any. We need to do this early
355 	 * enough so that subsystems relying on any of the aforementioned
356 	 * can work properly. Since the security model may dictate the
357 	 * credential inheritance policy, it is needed at least before
358 	 * any process is created, specifically proc0.
359 	 */
360 	module_init_class(MODULE_CLASS_SECMODEL);
361 
362 	/* Initialize the buffer cache */
363 	bufinit();
364 
365 	/* Initialize sockets. */
366 	soinit();
367 
368 	/*
369 	 * The following things must be done before autoconfiguration.
370 	 */
371 	evcnt_init();		/* initialize event counters */
372 #if NRND > 0
373 	rnd_init();		/* initialize random number generator */
374 #endif
375 
376 	/* Initialize process and pgrp structures. */
377 #ifdef KERN_SA
378 	sa_init();
379 #endif
380 	procinit();
381 	lwpinit();
382 
383 	/* Initialize signal-related data structures. */
384 	signal_init();
385 
386 	/* Initialize resource management. */
387 	resource_init();
388 
389 	/* Create process 0. */
390 	proc0_init();
391 	lwp0_init();
392 
393 	/* Disable preemption during boot. */
394 	kpreempt_disable();
395 
396 	/* Initialize the UID hash table. */
397 	uid_init();
398 
399 	/* Charge root for one process. */
400 	(void)chgproccnt(0, 1);
401 
402 	/* Initialize timekeeping. */
403 	time_init();
404 
405 	/* Initialize the run queues, turnstiles and sleep queues. */
406 	sched_rqinit();
407 	turnstile_init();
408 	sleeptab_init(&sleeptab);
409 
410 	sched_init();
411 
412 	/* Initialize processor-sets */
413 	psets_init();
414 
415 	/* MI initialization of the boot cpu */
416 	error = mi_cpu_attach(curcpu());
417 	KASSERT(error == 0);
418 
419 	/* Initialize timekeeping, part 2. */
420 	time_init2();
421 
422 	/*
423 	 * Initialize mbuf's.  Do this now because we might attempt to
424 	 * allocate mbufs or mbuf clusters during autoconfiguration.
425 	 */
426 	mbinit();
427 
428 	/* Initialize I/O statistics. */
429 	iostat_init();
430 
431 	/* Initialize the log device. */
432 	loginit();
433 
434 	/* Second part of module system initialization. */
435 	module_start_unload_thread();
436 
437 	/* Initialize the file systems. */
438 #ifdef NVNODE_IMPLICIT
439 	/*
440 	 * If maximum number of vnodes in namei vnode cache is not explicitly
441 	 * defined in kernel config, adjust the number such as we use roughly
442 	 * 10% of memory for vnodes and associated data structures in the
443 	 * assumed worst case.  Do not provide fewer than NVNODE vnodes.
444 	 */
445 	usevnodes =
446 	    calc_cache_size(kernel_map, 10, VNODE_VA_MAXPCT) / VNODE_COST;
447 	if (usevnodes > desiredvnodes)
448 		desiredvnodes = usevnodes;
449 #endif
450 	vfsinit();
451 	lf_init();
452 
453 	/* Initialize fstrans. */
454 	fstrans_init();
455 
456 	/* Initialize the file descriptor system. */
457 	fd_sys_init();
458 
459 	/* Initialize cwd structures */
460 	cwd_sys_init();
461 
462 	/* Initialize kqueue. */
463 	kqueue_init();
464 
465 	/* Initialize the system monitor subsystems. */
466 #if NSYSMON_TASKQ > 0
467 	sysmon_task_queue_preinit();
468 #endif
469 
470 #if NSYSMON_ENVSYS > 0
471 	sysmon_envsys_init();
472 #endif
473 
474 #if NSYSMON_POWER > 0
475 	sysmon_power_init();
476 #endif
477 
478 #if NSYSMON_WDOG > 0
479 	sysmon_wdog_init();
480 #endif
481 
482 	inittimecounter();
483 	ntp_init();
484 
485 	/* Initialize tty subsystem. */
486 	tty_init();
487 	ttyldisc_init();
488 
489 	/* Initialize the buffer cache, part 2. */
490 	bufinit2();
491 
492 	/* Initialize the disk wedge subsystem. */
493 	dkwedge_init();
494 
495 	/* Initialize interfaces. */
496 	ifinit1();
497 
498 	spldebug_start();
499 
500 	/* Configure the system hardware.  This will enable interrupts. */
501 	configure();
502 
503 	ssp_init();
504 
505 	configure2();
506 	/* Now timer is working.  Enable preemption. */
507 	kpreempt_enable();
508 
509 	ubc_init();		/* must be after autoconfig */
510 
511 #ifdef SYSVSHM
512 	/* Initialize System V style shared memory. */
513 	shminit();
514 #endif
515 
516 	vmem_rehash_start();	/* must be before exec_init */
517 
518 	/* Initialize exec structures */
519 	exec_init(1);		/* seminit calls exithook_establish() */
520 
521 #ifdef SYSVSEM
522 	/* Initialize System V style semaphores. */
523 	seminit();
524 #endif
525 
526 #ifdef SYSVMSG
527 	/* Initialize System V style message queues. */
528 	msginit();
529 #endif
530 
531 #if NVERIEXEC > 0
532 	/*
533 	 * Initialise the Veriexec subsystem.
534 	 */
535 	veriexec_init();
536 #endif /* NVERIEXEC > 0 */
537 
538 #if defined(PAX_MPROTECT) || defined(PAX_SEGVGUARD) || defined(PAX_ASLR)
539 	pax_init();
540 #endif /* PAX_MPROTECT || PAX_SEGVGUARD || PAX_ASLR */
541 
542 #ifdef	FAST_IPSEC
543 	/* Attach network crypto subsystem */
544 	ipsec_attach();
545 #endif
546 
547 	/*
548 	 * Initialize protocols.  Block reception of incoming packets
549 	 * until everything is ready.
550 	 */
551 	s = splnet();
552 	ifinit();
553 	domaininit(true);
554 	if_attachdomain();
555 	splx(s);
556 
557 #ifdef GPROF
558 	/* Initialize kernel profiling. */
559 	kmstartup();
560 #endif
561 
562 	/* Initialize system accounting. */
563 	acct_init();
564 
565 #ifndef PIPE_SOCKETPAIR
566 	/* Initialize pipes. */
567 	pipe_init();
568 #endif
569 
570 #ifdef KTRACE
571 	/* Initialize ktrace. */
572 	ktrinit();
573 #endif
574 
575 #ifdef PTRACE
576 	/* Initialize ptrace. */
577 	ptrace_init();
578 #endif /* PTRACE */
579 
580 	/* Initialize the UUID system calls. */
581 	uuid_init();
582 
583 	machdep_init();
584 
585 	/*
586 	 * Create process 1 (init(8)).  We do this now, as Unix has
587 	 * historically had init be process 1, and changing this would
588 	 * probably upset a lot of people.
589 	 *
590 	 * Note that process 1 won't immediately exec init(8), but will
591 	 * wait for us to inform it that the root file system has been
592 	 * mounted.
593 	 */
594 	if (fork1(l, 0, SIGCHLD, NULL, 0, start_init, NULL, NULL, &initproc))
595 		panic("fork init");
596 
597 	/*
598 	 * Load any remaining builtin modules, and hand back temporary
599 	 * storage to the VM system.  Then require force when loading any
600 	 * remaining un-init'ed built-in modules to avoid later surprises.
601 	 */
602 	module_init_class(MODULE_CLASS_ANY);
603 	module_builtin_require_force();
604 
605 	/*
606 	 * Finalize configuration now that all real devices have been
607 	 * found.  This needs to be done before the root device is
608 	 * selected, since finalization may create the root device.
609 	 */
610 	config_finalize();
611 
612 	sysctl_finalize();
613 
614 	/*
615 	 * Now that autoconfiguration has completed, we can determine
616 	 * the root and dump devices.
617 	 */
618 	cpu_rootconf();
619 	cpu_dumpconf();
620 
621 	/* Mount the root file system. */
622 	do {
623 		domountroothook(root_device);
624 		if ((error = vfs_mountroot())) {
625 			printf("cannot mount root, error = %d\n", error);
626 			boothowto |= RB_ASKNAME;
627 			setroot(root_device,
628 			    (rootdev != NODEV) ? DISKPART(rootdev) : 0);
629 		}
630 	} while (error != 0);
631 	mountroothook_destroy();
632 
633 	configure3();
634 
635 	/*
636 	 * Initialise the time-of-day clock, passing the time recorded
637 	 * in the root filesystem (if any) for use by systems that
638 	 * don't have a non-volatile time-of-day device.
639 	 */
640 	inittodr(rootfstime);
641 
642 	/*
643 	 * Now can look at time, having had a chance to verify the time
644 	 * from the file system.  Reset l->l_rtime as it may have been
645 	 * munched in mi_switch() after the time got set.
646 	 */
647 	getnanotime(&time);
648 	boottime = time;
649 #ifdef COMPAT_50
650 	{
651 		struct timeval tv;
652 		TIMESPEC_TO_TIMEVAL(&tv, &time);
653 		timeval_to_timeval50(&tv, &boottime50);
654 	}
655 #endif
656 	mutex_enter(proc_lock);
657 	LIST_FOREACH(p, &allproc, p_list) {
658 		KASSERT((p->p_flag & PK_MARKER) == 0);
659 		mutex_enter(p->p_lock);
660 		TIMESPEC_TO_TIMEVAL(&p->p_stats->p_start, &time);
661 		LIST_FOREACH(l, &p->p_lwps, l_sibling) {
662 			lwp_lock(l);
663 			memset(&l->l_rtime, 0, sizeof(l->l_rtime));
664 			lwp_unlock(l);
665 		}
666 		mutex_exit(p->p_lock);
667 	}
668 	mutex_exit(proc_lock);
669 	binuptime(&curlwp->l_stime);
670 
671 	for (CPU_INFO_FOREACH(cii, ci)) {
672 		ci->ci_schedstate.spc_lastmod = time_second;
673 	}
674 
675 	/* Create the pageout daemon kernel thread. */
676 	uvm_swap_init();
677 	if (kthread_create(PRI_PGDAEMON, KTHREAD_MPSAFE, NULL, uvm_pageout,
678 	    NULL, NULL, "pgdaemon"))
679 		panic("fork pagedaemon");
680 
681 	/* Create the filesystem syncer kernel thread. */
682 	if (kthread_create(PRI_IOFLUSH, KTHREAD_MPSAFE, NULL, sched_sync,
683 	    NULL, NULL, "ioflush"))
684 		panic("fork syncer");
685 
686 	/* Create the aiodone daemon kernel thread. */
687 	if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
688 	    uvm_aiodone_worker, NULL, PRI_VM, IPL_NONE, WQ_MPSAFE))
689 		panic("fork aiodoned");
690 
691 	/*
692 	 * Okay, now we can let init(8) exec!  It's off to userland!
693 	 */
694 	mutex_enter(proc_lock);
695 	start_init_exec = 1;
696 	cv_broadcast(&lbolt);
697 	mutex_exit(proc_lock);
698 
699 	/* The scheduler is an infinite loop. */
700 	uvm_scheduler();
701 	/* NOTREACHED */
702 }
703 
704 /*
705  * Configure the system's hardware.
706  */
707 static void
708 configure(void)
709 {
710 
711 	/* Initialize autoconf data structures. */
712 	config_init_mi();
713 	/*
714 	 * XXX
715 	 * callout_setfunc() requires mutex(9) so it can't be in config_init()
716 	 * on amiga and atari which use config_init() and autoconf(9) fucntions
717 	 * to initialize console devices.
718 	 */
719 	config_twiddle_init();
720 
721 	pmf_init();
722 #if NDRVCTL > 0
723 	drvctl_init();
724 #endif
725 
726 #ifdef USERCONF
727 	if (boothowto & RB_USERCONF)
728 		user_config();
729 #endif
730 
731 	if ((boothowto & (AB_SILENT|AB_VERBOSE)) == AB_SILENT) {
732 		printf_nolog("Detecting hardware...");
733 	}
734 
735 	/*
736 	 * Do the machine-dependent portion of autoconfiguration.  This
737 	 * sets the configuration machinery here in motion by "finding"
738 	 * the root bus.  When this function returns, we expect interrupts
739 	 * to be enabled.
740 	 */
741 	cpu_configure();
742 }
743 
744 static void
745 configure2(void)
746 {
747 	CPU_INFO_ITERATOR cii;
748 	struct cpu_info *ci;
749 	int s;
750 
751 	/*
752 	 * Now that we've found all the hardware, start the real time
753 	 * and statistics clocks.
754 	 */
755 	initclocks();
756 
757 	cold = 0;	/* clocks are running, we're warm now! */
758 	s = splsched();
759 	curcpu()->ci_schedstate.spc_flags |= SPCF_RUNNING;
760 	splx(s);
761 
762 	/* Boot the secondary processors. */
763 	for (CPU_INFO_FOREACH(cii, ci)) {
764 		uvm_cpu_attach(ci);
765 	}
766 	mp_online = true;
767 #if defined(MULTIPROCESSOR)
768 	cpu_boot_secondary_processors();
769 #endif
770 
771 	/* Setup the runqueues and scheduler. */
772 	runq_init();
773 	synch_init();
774 
775 	/*
776 	 * Bus scans can make it appear as if the system has paused, so
777 	 * twiddle constantly while config_interrupts() jobs are running.
778 	 */
779 	config_twiddle_fn(NULL);
780 
781 	/*
782 	 * Create threads to call back and finish configuration for
783 	 * devices that want interrupts enabled.
784 	 */
785 	config_create_interruptthreads();
786 
787 	/* Get the threads going and into any sleeps before continuing. */
788 	yield();
789 }
790 
791 static void
792 configure3(void)
793 {
794 
795 	/*
796 	 * Create threads to call back and finish configuration for
797 	 * devices that want the mounted root file system.
798 	 */
799 	config_create_mountrootthreads();
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 			aprint_normal("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 		aprint_normal("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