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