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