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