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