1 /* $NetBSD: init_main.c,v 1.531 2020/09/08 16:00:35 riastradh Exp $ */ 2 3 /*- 4 * Copyright (c) 2008, 2009, 2019 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.531 2020/09/08 16:00:35 riastradh 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_wapbl.h" 116 #include "opt_ptrace.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/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 203 #include <sys/syscall.h> 204 #include <sys/syscallargs.h> 205 206 #include <sys/pax.h> 207 208 #include <dev/clock_subr.h> 209 210 #include <secmodel/secmodel.h> 211 212 #include <ufs/ufs/quota.h> 213 214 #include <miscfs/genfs/genfs.h> 215 #include <miscfs/specfs/specdev.h> 216 217 #include <sys/cpu.h> 218 219 #include <uvm/uvm.h> /* extern struct uvm uvm */ 220 221 #include <dev/cons.h> 222 #include <dev/splash/splash.h> 223 224 #include <net/bpf.h> 225 #include <net/if.h> 226 #include <net/pfil.h> 227 #include <net/raw_cb.h> 228 #include <net/if_llatbl.h> 229 230 #include <prop/proplib.h> 231 232 #include <sys/userconf.h> 233 234 extern struct lwp lwp0; 235 extern time_t rootfstime; 236 237 #ifndef curlwp 238 struct lwp *curlwp = &lwp0; 239 #endif 240 struct proc *initproc; 241 242 struct vnode *rootvp, *swapdev_vp; 243 int boothowto; 244 int cold __read_mostly = 1; /* still working on startup */ 245 int shutting_down __read_mostly; /* system is shutting down */ 246 247 int start_init_exec; /* semaphore for start_init() */ 248 249 static void check_console(struct lwp *l); 250 static void start_init(void *); 251 static void configure(void); 252 static void configure2(void); 253 static void configure3(void); 254 void main(void); 255 256 /* 257 * System startup; initialize the world, create process 0, mount root 258 * filesystem, and fork to create init and pagedaemon. Most of the 259 * hard work is done in the lower-level initialization routines including 260 * startup(), which does memory initialization and autoconfiguration. 261 */ 262 void 263 main(void) 264 { 265 struct timespec time; 266 struct lwp *l; 267 struct proc *p; 268 int s, error; 269 #ifdef NVNODE_IMPLICIT 270 int usevnodes; 271 #endif 272 CPU_INFO_ITERATOR cii; 273 struct cpu_info *ci; 274 275 #ifdef DIAGNOSTIC 276 /* 277 * Verify that CPU_INFO_FOREACH() knows about the boot CPU 278 * and only the boot CPU at this point. 279 */ 280 int cpucount = 0; 281 for (CPU_INFO_FOREACH(cii, ci)) { 282 KASSERT(ci == curcpu()); 283 cpucount++; 284 } 285 KASSERT(cpucount == 1); 286 #endif 287 288 l = &lwp0; 289 #ifndef LWP0_CPU_INFO 290 l->l_cpu = curcpu(); 291 #endif 292 l->l_pflag |= LP_RUNNING; 293 294 /* 295 * Attempt to find console and initialize 296 * in case of early panic or other messages. 297 */ 298 consinit(); 299 #ifdef CNMAGIC 300 cn_set_magic(CNMAGIC); 301 #endif 302 303 kernel_lock_init(); 304 once_init(); 305 todr_init(); 306 307 mi_cpu_init(); 308 kernconfig_lock_init(); 309 kthread_sysinit(); 310 311 /* Initialize the device switch tables. */ 312 devsw_init(); 313 314 /* Initialize event counters. */ 315 evcnt_init(); 316 317 uvm_init(); 318 ubchist_init(); 319 kcpuset_sysinit(); 320 321 prop_kern_init(); 322 323 #if ((NKSYMS > 0) || (NDDB > 0) || (NMODULAR > 0)) 324 ksyms_init(); 325 #endif 326 kprintf_init(); 327 328 percpu_init(); 329 330 /* Initialize lock caches. */ 331 mutex_obj_init(); 332 rw_obj_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(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 signal-related data structures. */ 415 signal_init(); 416 417 /* Initialize resource management. */ 418 resource_init(); 419 420 /* Create process 0. */ 421 proc0_init(); 422 lwp0_init(); 423 424 /* Disable preemption during boot. */ 425 kpreempt_disable(); 426 427 /* Initialize the threadpool system. */ 428 threadpools_init(); 429 430 /* Initialize the UID hash table. */ 431 uid_init(); 432 433 /* Charge root for one process. */ 434 (void)chgproccnt(0, 1); 435 436 /* Initialize timekeeping. */ 437 time_init(); 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, part 2. */ 457 time_init2(); 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 490 491 /* Initialize fstrans. */ 492 fstrans_init(); 493 494 vfsinit(); 495 lf_init(); 496 497 /* Initialize the file descriptor system. */ 498 fd_sys_init(); 499 500 /* Initialize cwd structures */ 501 cwd_sys_init(); 502 503 /* Initialize kqueue. */ 504 kqueue_init(); 505 506 inittimecounter(); 507 ntp_init(); 508 509 /* Initialize tty subsystem. */ 510 tty_init(); 511 ttyldisc_init(); 512 513 /* Initialize the buffer cache, part 2. */ 514 bufinit2(); 515 516 /* Initialize the disk wedge subsystem. */ 517 dkwedge_init(); 518 519 /* Initialize pfil */ 520 pfil_init(); 521 522 /* Initialize interfaces. */ 523 ifinit1(); 524 525 spldebug_start(); 526 527 /* Initialize sockets thread(s) */ 528 soinit1(); 529 530 /* 531 * Initialize the bufq strategy sub-system and any built-in 532 * strategy modules - they may be needed by some devices during 533 * auto-configuration 534 */ 535 bufq_init(); 536 module_init_class(MODULE_CLASS_BUFQ); 537 538 /* Configure the system hardware. This will enable interrupts. */ 539 configure(); 540 541 /* Once all CPUs are detected, initialize the per-CPU cprng_fast. */ 542 cprng_fast_init(); 543 544 ssp_init(); 545 546 ubc_init(); /* must be after autoconfig */ 547 548 mm_init(); 549 550 configure2(); 551 552 /* Initialize the rest of ipi(9) after CPUs have been detected. */ 553 ipi_percpu_init(); 554 555 futex_sys_init(); 556 557 /* Now timer is working. Enable preemption. */ 558 kpreempt_enable(); 559 560 /* Get the threads going and into any sleeps before continuing. */ 561 yield(); 562 563 /* Enable deferred processing of RNG samples */ 564 rnd_init_softint(); 565 566 vmem_rehash_start(); /* must be before exec_init */ 567 568 /* Initialize exec structures */ 569 exec_init(1); /* seminit calls exithook_establish() */ 570 571 #if NVERIEXEC > 0 572 /* 573 * Initialise the Veriexec subsystem. 574 */ 575 veriexec_init(); 576 #endif /* NVERIEXEC > 0 */ 577 578 pax_init(); 579 580 #ifdef IPSEC 581 /* Attach network crypto subsystem */ 582 ipsec_attach(); 583 #endif 584 585 /* 586 * Initialize protocols. Block reception of incoming packets 587 * until everything is ready. 588 */ 589 s = splnet(); 590 ifinit(); 591 #if defined(INET) || defined(INET6) 592 lltableinit(); 593 #endif 594 domaininit(true); 595 ifinit_post(); 596 if_attachdomain(); 597 splx(s); 598 599 #ifdef GPROF 600 /* Initialize kernel profiling. */ 601 kmstartup(); 602 #endif 603 604 /* Initialize system accounting. */ 605 acct_init(); 606 607 #ifndef PIPE_SOCKETPAIR 608 /* Initialize pipes. */ 609 pipe_init(); 610 #endif 611 612 #ifdef KTRACE 613 /* Initialize ktrace. */ 614 ktrinit(); 615 #endif 616 617 machdep_init(); 618 619 procinit_sysctl(); 620 621 scdebug_init(); 622 623 /* 624 * Create process 1 (init(8)). We do this now, as Unix has 625 * historically had init be process 1, and changing this would 626 * probably upset a lot of people. 627 * 628 * Note that process 1 won't immediately exec init(8), but will 629 * wait for us to inform it that the root file system has been 630 * mounted. 631 */ 632 if (fork1(l, 0, SIGCHLD, NULL, 0, start_init, NULL, NULL)) 633 panic("fork init"); 634 635 /* 636 * The initproc variable cannot be initialized in start_init as there 637 * is a race between vfs_mountroot and start_init. 638 */ 639 mutex_enter(&proc_lock); 640 initproc = proc_find_raw(1); 641 mutex_exit(&proc_lock); 642 643 /* 644 * Load any remaining builtin modules, and hand back temporary 645 * storage to the VM system. Then require force when loading any 646 * remaining un-init'ed built-in modules to avoid later surprises. 647 */ 648 module_init_class(MODULE_CLASS_ANY); 649 module_builtin_require_force(); 650 651 /* 652 * Finalize configuration now that all real devices have been 653 * found. This needs to be done before the root device is 654 * selected, since finalization may create the root device. 655 */ 656 config_finalize(); 657 658 sysctl_finalize(); 659 660 /* 661 * Now that autoconfiguration has completed, we can determine 662 * the root and dump devices. 663 */ 664 cpu_rootconf(); 665 cpu_dumpconf(); 666 667 /* Mount the root file system. */ 668 do { 669 domountroothook(root_device); 670 if ((error = vfs_mountroot())) { 671 printf("cannot mount root, error = %d\n", error); 672 boothowto |= RB_ASKNAME; 673 setroot(root_device, 674 (rootdev != NODEV) ? DISKPART(rootdev) : 0); 675 } 676 } while (error != 0); 677 mountroothook_destroy(); 678 679 configure3(); 680 681 /* 682 * Initialise the time-of-day clock, passing the time recorded 683 * in the root filesystem (if any) for use by systems that 684 * don't have a non-volatile time-of-day device. 685 */ 686 inittodr(rootfstime); 687 688 /* 689 * Now can look at time, having had a chance to verify the time 690 * from the file system. Reset l->l_rtime as it may have been 691 * munched in mi_switch() after the time got set. 692 */ 693 getnanotime(&time); 694 695 mutex_enter(&proc_lock); 696 LIST_FOREACH(p, &allproc, p_list) { 697 KASSERT((p->p_flag & PK_MARKER) == 0); 698 mutex_enter(p->p_lock); 699 TIMESPEC_TO_TIMEVAL(&p->p_stats->p_start, &time); 700 LIST_FOREACH(l, &p->p_lwps, l_sibling) { 701 lwp_lock(l); 702 memset(&l->l_rtime, 0, sizeof(l->l_rtime)); 703 lwp_unlock(l); 704 } 705 mutex_exit(p->p_lock); 706 } 707 mutex_exit(&proc_lock); 708 binuptime(&curlwp->l_stime); 709 710 for (CPU_INFO_FOREACH(cii, ci)) { 711 ci->ci_schedstate.spc_lastmod = time_second; 712 } 713 714 /* Create the pageout daemon kernel thread. */ 715 uvm_swap_init(); 716 if (kthread_create(PRI_PGDAEMON, KTHREAD_MPSAFE, NULL, uvm_pageout, 717 NULL, NULL, "pgdaemon")) 718 panic("fork pagedaemon"); 719 720 /* Create the filesystem syncer kernel thread. */ 721 if (kthread_create(PRI_IOFLUSH, KTHREAD_MPSAFE, NULL, sched_sync, 722 NULL, NULL, "ioflush")) 723 panic("fork syncer"); 724 725 /* Wait for final configure threads to complete. */ 726 config_finalize_mountroot(); 727 728 /* 729 * Okay, now we can let init(8) exec! It's off to userland! 730 */ 731 mutex_enter(&proc_lock); 732 start_init_exec = 1; 733 cv_broadcast(&lbolt); 734 mutex_exit(&proc_lock); 735 736 /* The scheduler is an infinite loop. */ 737 uvm_scheduler(); 738 /* NOTREACHED */ 739 } 740 741 /* 742 * Configure the system's hardware. 743 */ 744 static void 745 configure(void) 746 { 747 748 /* 749 * XXX 750 * callout_setfunc() requires mutex(9) so it can't be in config_init() 751 * on amiga and atari which use config_init() and autoconf(9) fucntions 752 * to initialize console devices. 753 */ 754 config_twiddle_init(); 755 756 pmf_init(); 757 758 /* Initialize driver modules */ 759 module_init_class(MODULE_CLASS_DRIVER); 760 761 userconf_init(); 762 if (boothowto & RB_USERCONF) 763 userconf_prompt(); 764 765 if ((boothowto & (AB_SILENT|AB_VERBOSE)) == AB_SILENT) { 766 printf_nolog("Detecting hardware..."); 767 } 768 769 /* 770 * Do the machine-dependent portion of autoconfiguration. This 771 * sets the configuration machinery here in motion by "finding" 772 * the root bus. When this function returns, we expect interrupts 773 * to be enabled. 774 */ 775 cpu_configure(); 776 } 777 778 static void 779 configure2(void) 780 { 781 CPU_INFO_ITERATOR cii; 782 struct cpu_info *ci; 783 int s; 784 785 /* Fix up CPU topology info, which has all been collected by now. */ 786 cpu_topology_init(); 787 788 /* 789 * Now that we've found all the hardware, start the real time 790 * and statistics clocks. 791 */ 792 initclocks(); 793 794 cold = 0; /* clocks are running, we're warm now! */ 795 s = splsched(); 796 curcpu()->ci_schedstate.spc_flags |= SPCF_RUNNING; 797 splx(s); 798 799 /* Setup the runqueues and scheduler. */ 800 runq_init(); 801 synch_init(); 802 803 /* Boot the secondary processors. */ 804 for (CPU_INFO_FOREACH(cii, ci)) { 805 uvm_cpu_attach(ci); 806 } 807 808 /* Decide how to partition free memory. */ 809 uvm_page_rebucket(); 810 811 mp_online = true; 812 #if defined(MULTIPROCESSOR) 813 cpu_boot_secondary_processors(); 814 #endif 815 816 /* 817 * Bus scans can make it appear as if the system has paused, so 818 * twiddle constantly while config_interrupts() jobs are running. 819 */ 820 config_twiddle_fn(NULL); 821 822 /* 823 * Create threads to call back and finish configuration for 824 * devices that want interrupts enabled. 825 */ 826 config_create_interruptthreads(); 827 } 828 829 static void 830 configure3(void) 831 { 832 833 /* 834 * Create threads to call back and finish configuration for 835 * devices that want the mounted root file system. 836 */ 837 config_create_mountrootthreads(); 838 839 /* Get the threads going and into any sleeps before continuing. */ 840 yield(); 841 } 842 843 static void 844 rootconf_handle_wedges(void) 845 { 846 struct disklabel label; 847 struct partition *p; 848 struct vnode *vp; 849 daddr_t startblk; 850 uint64_t nblks; 851 device_t dev; 852 int error; 853 854 if (booted_nblks) { 855 /* 856 * bootloader passed geometry 857 */ 858 dev = booted_device; 859 startblk = booted_startblk; 860 nblks = booted_nblks; 861 862 /* 863 * keep booted_device and booted_partition 864 * in case the kernel doesn't identify a wedge 865 */ 866 } else { 867 /* 868 * bootloader passed partition number 869 * 870 * We cannot ask the partition device directly when it is 871 * covered by a wedge. Instead we look up the geometry in 872 * the disklabel. 873 */ 874 vp = opendisk(booted_device); 875 876 if (vp == NULL) 877 return; 878 879 error = VOP_IOCTL(vp, DIOCGDINFO, &label, FREAD, NOCRED); 880 VOP_CLOSE(vp, FREAD, NOCRED); 881 vput(vp); 882 if (error) 883 return; 884 885 KASSERT(booted_partition >= 0 886 && booted_partition < MAXPARTITIONS); 887 888 p = &label.d_partitions[booted_partition]; 889 890 dev = booted_device; 891 startblk = p->p_offset; 892 nblks = p->p_size; 893 } 894 895 dev = dkwedge_find_partition(dev, startblk, nblks); 896 if (dev != NULL) { 897 booted_device = dev; 898 booted_partition = 0; 899 } 900 } 901 902 void 903 rootconf(void) 904 { 905 if (booted_device != NULL) 906 rootconf_handle_wedges(); 907 908 setroot(booted_device, booted_partition); 909 } 910 911 static void 912 check_console(struct lwp *l) 913 { 914 struct vnode *vp; 915 int error; 916 917 error = namei_simple_kernel("/dev/console", 918 NSM_FOLLOW_NOEMULROOT, &vp); 919 if (error == 0) { 920 vrele(vp); 921 } else if (error == ENOENT) { 922 if (boothowto & (AB_VERBOSE|AB_DEBUG)) 923 printf("warning: no /dev/console\n"); 924 } else { 925 printf("warning: lookup /dev/console: error %d\n", error); 926 } 927 } 928 929 /* 930 * List of paths to try when searching for "init". 931 */ 932 static const char * const initpaths[] = { 933 "/sbin/init", 934 "/sbin/oinit", 935 "/sbin/init.bak", 936 "/rescue/init", 937 NULL, 938 }; 939 940 /* 941 * Start the initial user process; try exec'ing each pathname in "initpaths". 942 * The program is invoked with one argument containing the boot flags. 943 */ 944 static void 945 start_init(void *arg) 946 { 947 struct lwp *l = arg; 948 struct proc *p = l->l_proc; 949 vaddr_t addr; 950 struct sys_execve_args /* { 951 syscallarg(const char *) path; 952 syscallarg(char * const *) argp; 953 syscallarg(char * const *) envp; 954 } */ args; 955 int options, i, error; 956 register_t retval[2]; 957 char flags[4], *flagsp; 958 const char *path, *slash; 959 char *ucp, **uap, *arg0, *arg1, *argv[3]; 960 char ipath[129]; 961 int ipx, len; 962 963 /* 964 * Now in process 1. 965 */ 966 strncpy(p->p_comm, "init", MAXCOMLEN); 967 968 /* 969 * Wait for main() to tell us that it's safe to exec. 970 */ 971 mutex_enter(&proc_lock); 972 while (start_init_exec == 0) 973 cv_wait(&lbolt, &proc_lock); 974 mutex_exit(&proc_lock); 975 976 /* 977 * This is not the right way to do this. We really should 978 * hand-craft a descriptor onto /dev/console to hand to init, 979 * but that's a _lot_ more work, and the benefit from this easy 980 * hack makes up for the "good is the enemy of the best" effect. 981 */ 982 check_console(l); 983 984 /* 985 * Need just enough stack to hold the faked-up "execve()" arguments. 986 */ 987 addr = (vaddr_t)STACK_ALLOC(USRSTACK, PAGE_SIZE); 988 if (uvm_map(&p->p_vmspace->vm_map, &addr, PAGE_SIZE, 989 NULL, UVM_UNKNOWN_OFFSET, 0, 990 UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_RW, UVM_INH_COPY, 991 UVM_ADV_NORMAL, 992 UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW)) != 0) 993 panic("init: couldn't allocate argument space"); 994 p->p_vmspace->vm_maxsaddr = (void *)STACK_MAX(addr, PAGE_SIZE); 995 996 ipx = 0; 997 while (1) { 998 if (boothowto & RB_ASKNAME) { 999 printf("init path"); 1000 if (initpaths[ipx]) 1001 printf(" (default %s)", initpaths[ipx]); 1002 printf(": "); 1003 len = cngetsn(ipath, sizeof(ipath)-1); 1004 if (len == 4 && strcmp(ipath, "halt") == 0) { 1005 kern_reboot(RB_HALT, NULL); 1006 } else if (len == 6 && strcmp(ipath, "reboot") == 0) { 1007 kern_reboot(0, NULL); 1008 #if defined(DDB) 1009 } else if (len == 3 && strcmp(ipath, "ddb") == 0) { 1010 console_debugger(); 1011 continue; 1012 #endif 1013 } else if (len > 0 && ipath[0] == '/') { 1014 ipath[len] = '\0'; 1015 path = ipath; 1016 } else if (len == 0 && initpaths[ipx] != NULL) { 1017 path = initpaths[ipx++]; 1018 } else { 1019 printf("use absolute path, "); 1020 #if defined(DDB) 1021 printf("\"ddb\", "); 1022 #endif 1023 printf("\"halt\", or \"reboot\"\n"); 1024 continue; 1025 } 1026 } else { 1027 if ((path = initpaths[ipx++]) == NULL) { 1028 ipx = 0; 1029 boothowto |= RB_ASKNAME; 1030 continue; 1031 } 1032 } 1033 1034 ucp = (char *)USRSTACK; 1035 1036 /* 1037 * Construct the boot flag argument. 1038 */ 1039 flagsp = flags; 1040 *flagsp++ = '-'; 1041 options = 0; 1042 1043 if (boothowto & RB_SINGLE) { 1044 *flagsp++ = 's'; 1045 options = 1; 1046 } 1047 #ifdef notyet 1048 if (boothowto & RB_FASTBOOT) { 1049 *flagsp++ = 'f'; 1050 options = 1; 1051 } 1052 #endif 1053 1054 /* 1055 * Move out the flags (arg 1), if necessary. 1056 */ 1057 if (options != 0) { 1058 *flagsp++ = '\0'; 1059 i = flagsp - flags; 1060 #ifdef DEBUG 1061 aprint_normal("init: copying out flags `%s' %d\n", flags, i); 1062 #endif 1063 arg1 = STACK_ALLOC(ucp, i); 1064 ucp = STACK_MAX(arg1, i); 1065 if ((error = copyout((void *)flags, arg1, i)) != 0) 1066 goto copyerr; 1067 } else 1068 arg1 = NULL; 1069 1070 /* 1071 * Move out the file name (also arg 0). 1072 */ 1073 i = strlen(path) + 1; 1074 #ifdef DEBUG 1075 aprint_normal("init: copying out path `%s' %d\n", path, i); 1076 #else 1077 if (boothowto & RB_ASKNAME || path != initpaths[0]) 1078 printf("init: trying %s\n", path); 1079 #endif 1080 arg0 = STACK_ALLOC(ucp, i); 1081 ucp = STACK_MAX(arg0, i); 1082 if ((error = copyout(path, arg0, i)) != 0) 1083 goto copyerr; 1084 1085 /* 1086 * Move out the arg pointers. 1087 */ 1088 ucp = (void *)STACK_ALIGN(ucp, STACK_ALIGNBYTES); 1089 uap = (char **)STACK_ALLOC(ucp, sizeof(argv)); 1090 SCARG(&args, path) = arg0; 1091 SCARG(&args, argp) = uap; 1092 SCARG(&args, envp) = NULL; 1093 slash = strrchr(path, '/'); 1094 1095 argv[0] = slash ? arg0 + (slash + 1 - path) : arg0; 1096 argv[1] = arg1; 1097 argv[2] = NULL; 1098 if ((error = copyout(argv, uap, sizeof(argv))) != 0) 1099 goto copyerr; 1100 1101 /* 1102 * Now try to exec the program. If it can't for any reason 1103 * other than it doesn't exist, complain. 1104 */ 1105 error = sys_execve(l, &args, retval); 1106 if (error == 0 || error == EJUSTRETURN) { 1107 KERNEL_UNLOCK_LAST(l); 1108 return; 1109 } 1110 printf("exec %s: error %d\n", path, error); 1111 } 1112 printf("init: not found\n"); 1113 panic("no init"); 1114 copyerr: 1115 panic("copyout %d", error); 1116 } 1117 1118 /* 1119 * calculate cache size (in bytes) from physmem and vsize. 1120 */ 1121 vaddr_t 1122 calc_cache_size(vsize_t vsize, int pct, int va_pct) 1123 { 1124 paddr_t t; 1125 1126 /* XXX should consider competing cache if any */ 1127 /* XXX should consider submaps */ 1128 t = (uintmax_t)physmem * pct / 100 * PAGE_SIZE; 1129 if (vsize != 0) { 1130 vsize = (uintmax_t)vsize * va_pct / 100; 1131 if (t > vsize) { 1132 t = vsize; 1133 } 1134 } 1135 return t; 1136 } 1137 1138 /* 1139 * Print the system start up banner. 1140 * 1141 * - Print a limited banner if AB_SILENT. 1142 * - Always send normal banner to the log. 1143 */ 1144 #define MEM_PBUFSIZE sizeof("99999 MB") 1145 1146 void 1147 banner(void) 1148 { 1149 static char notice[] = " Notice: this software is " 1150 "protected by copyright"; 1151 char pbuf[81]; 1152 void (*pr)(const char *, ...) __printflike(1, 2); 1153 int i; 1154 1155 if ((boothowto & AB_SILENT) != 0) { 1156 snprintf(pbuf, sizeof(pbuf), "%s %s (%s)", 1157 ostype, osrelease, kernel_ident); 1158 printf_nolog("%s", pbuf); 1159 for (i = 80 - strlen(pbuf) - sizeof(notice); i > 0; i--) 1160 printf(" "); 1161 printf_nolog("%s\n", notice); 1162 pr = aprint_normal; 1163 } else { 1164 pr = printf; 1165 } 1166 1167 memset(pbuf, 0, sizeof(pbuf)); 1168 (*pr)("%s%s", copyright, version); 1169 format_bytes(pbuf, MEM_PBUFSIZE, ctob((uint64_t)physmem)); 1170 (*pr)("total memory = %s\n", pbuf); 1171 format_bytes(pbuf, MEM_PBUFSIZE, ctob((uint64_t)uvm_availmem(false))); 1172 (*pr)("avail memory = %s\n", pbuf); 1173 } 1174