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