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