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