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