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