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