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