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