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