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