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