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