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