1 /* $NetBSD: init_main.c,v 1.459 2014/08/14 16:27:55 riastradh 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.459 2014/08/14 16:27:55 riastradh 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 /* Enable deferred processing of RNG samples */ 532 rnd_init_softint(); 533 534 #ifdef RND_PRINTF 535 /* Enable periodic injection of console output into entropy pool */ 536 kprintf_init_callout(); 537 #endif 538 539 #ifdef SYSVSHM 540 /* Initialize System V style shared memory. */ 541 shminit(); 542 #endif 543 544 vmem_rehash_start(); /* must be before exec_init */ 545 546 /* Initialize exec structures */ 547 exec_init(1); /* seminit calls exithook_establish() */ 548 549 #ifdef SYSVSEM 550 /* Initialize System V style semaphores. */ 551 seminit(); 552 #endif 553 554 #ifdef SYSVMSG 555 /* Initialize System V style message queues. */ 556 msginit(); 557 #endif 558 559 #if NVERIEXEC > 0 560 /* 561 * Initialise the Veriexec subsystem. 562 */ 563 veriexec_init(); 564 #endif /* NVERIEXEC > 0 */ 565 566 #if defined(PAX_MPROTECT) || defined(PAX_SEGVGUARD) || defined(PAX_ASLR) 567 pax_init(); 568 #endif /* PAX_MPROTECT || PAX_SEGVGUARD || PAX_ASLR */ 569 570 #ifdef IPSEC 571 /* Attach network crypto subsystem */ 572 ipsec_attach(); 573 #endif 574 575 /* 576 * Initialize protocols. Block reception of incoming packets 577 * until everything is ready. 578 */ 579 s = splnet(); 580 ifinit(); 581 domaininit(true); 582 if_attachdomain(); 583 splx(s); 584 585 #ifdef GPROF 586 /* Initialize kernel profiling. */ 587 kmstartup(); 588 #endif 589 590 /* Initialize system accounting. */ 591 acct_init(); 592 593 #ifndef PIPE_SOCKETPAIR 594 /* Initialize pipes. */ 595 pipe_init(); 596 #endif 597 598 #ifdef KTRACE 599 /* Initialize ktrace. */ 600 ktrinit(); 601 #endif 602 603 #ifdef PTRACE 604 /* Initialize ptrace. */ 605 ptrace_init(); 606 #endif /* PTRACE */ 607 608 /* Initialize the UUID system calls. */ 609 uuid_init(); 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 /* Get the threads going and into any sleeps before continuing. */ 811 yield(); 812 } 813 814 static void 815 configure3(void) 816 { 817 818 /* 819 * Create threads to call back and finish configuration for 820 * devices that want the mounted root file system. 821 */ 822 config_create_mountrootthreads(); 823 824 /* Get the threads going and into any sleeps before continuing. */ 825 yield(); 826 } 827 828 static void 829 rootconf_handle_wedges(void) 830 { 831 struct partinfo dpart; 832 struct partition *p; 833 struct vnode *vp; 834 daddr_t startblk; 835 uint64_t nblks; 836 device_t dev; 837 int error; 838 839 if (booted_nblks) { 840 /* 841 * bootloader passed geometry 842 */ 843 dev = booted_device; 844 startblk = booted_startblk; 845 nblks = booted_nblks; 846 847 /* 848 * keep booted_device and booted_partition 849 * in case the kernel doesn't identify a wedge 850 */ 851 } else { 852 /* 853 * bootloader passed partition number 854 * 855 * We cannot ask the partition device directly when it is 856 * covered by a wedge. Instead we look up the geometry in 857 * the disklabel. 858 */ 859 vp = opendisk(booted_device); 860 861 if (vp == NULL) 862 return; 863 864 error = VOP_IOCTL(vp, DIOCGPART, &dpart, FREAD, NOCRED); 865 VOP_CLOSE(vp, FREAD, NOCRED); 866 vput(vp); 867 if (error) 868 return; 869 870 KASSERT(booted_partition >= 0 871 && booted_partition < MAXPARTITIONS); 872 873 p = &dpart.disklab->d_partitions[booted_partition]; 874 875 dev = booted_device; 876 startblk = p->p_offset; 877 nblks = p->p_size; 878 } 879 880 dev = dkwedge_find_partition(dev, startblk, nblks); 881 if (dev != NULL) { 882 booted_device = dev; 883 booted_partition = 0; 884 } 885 } 886 887 void 888 rootconf(void) 889 { 890 if (booted_device != NULL) 891 rootconf_handle_wedges(); 892 893 setroot(booted_device, booted_partition); 894 } 895 896 static void 897 check_console(struct lwp *l) 898 { 899 struct vnode *vp; 900 int error; 901 902 error = namei_simple_kernel("/dev/console", 903 NSM_FOLLOW_NOEMULROOT, &vp); 904 if (error == 0) 905 vrele(vp); 906 else if (error == ENOENT) 907 printf("warning: no /dev/console\n"); 908 else 909 printf("warning: lookup /dev/console: error %d\n", error); 910 } 911 912 /* 913 * List of paths to try when searching for "init". 914 */ 915 static const char * const initpaths[] = { 916 "/sbin/init", 917 "/sbin/oinit", 918 "/sbin/init.bak", 919 "/rescue/init", 920 NULL, 921 }; 922 923 /* 924 * Start the initial user process; try exec'ing each pathname in "initpaths". 925 * The program is invoked with one argument containing the boot flags. 926 */ 927 static void 928 start_init(void *arg) 929 { 930 struct lwp *l = arg; 931 struct proc *p = l->l_proc; 932 vaddr_t addr; 933 struct sys_execve_args /* { 934 syscallarg(const char *) path; 935 syscallarg(char * const *) argp; 936 syscallarg(char * const *) envp; 937 } */ args; 938 int options, i, error; 939 register_t retval[2]; 940 char flags[4], *flagsp; 941 const char *path, *slash; 942 char *ucp, **uap, *arg0, *arg1 = NULL; 943 char ipath[129]; 944 int ipx, len; 945 946 /* 947 * Now in process 1. 948 */ 949 strncpy(p->p_comm, "init", MAXCOMLEN); 950 951 /* 952 * Wait for main() to tell us that it's safe to exec. 953 */ 954 mutex_enter(proc_lock); 955 while (start_init_exec == 0) 956 cv_wait(&lbolt, proc_lock); 957 mutex_exit(proc_lock); 958 959 /* 960 * This is not the right way to do this. We really should 961 * hand-craft a descriptor onto /dev/console to hand to init, 962 * but that's a _lot_ more work, and the benefit from this easy 963 * hack makes up for the "good is the enemy of the best" effect. 964 */ 965 check_console(l); 966 967 /* 968 * Need just enough stack to hold the faked-up "execve()" arguments. 969 */ 970 addr = (vaddr_t)STACK_ALLOC(USRSTACK, PAGE_SIZE); 971 if (uvm_map(&p->p_vmspace->vm_map, &addr, PAGE_SIZE, 972 NULL, UVM_UNKNOWN_OFFSET, 0, 973 UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY, 974 UVM_ADV_NORMAL, 975 UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW)) != 0) 976 panic("init: couldn't allocate argument space"); 977 p->p_vmspace->vm_maxsaddr = (void *)STACK_MAX(addr, PAGE_SIZE); 978 979 ipx = 0; 980 while (1) { 981 if (boothowto & RB_ASKNAME) { 982 printf("init path"); 983 if (initpaths[ipx]) 984 printf(" (default %s)", initpaths[ipx]); 985 printf(": "); 986 len = cngetsn(ipath, sizeof(ipath)-1); 987 if (len == 4 && strcmp(ipath, "halt") == 0) { 988 cpu_reboot(RB_HALT, NULL); 989 } else if (len == 6 && strcmp(ipath, "reboot") == 0) { 990 cpu_reboot(0, NULL); 991 #if defined(DDB) 992 } else if (len == 3 && strcmp(ipath, "ddb") == 0) { 993 console_debugger(); 994 continue; 995 #endif 996 } else if (len > 0 && ipath[0] == '/') { 997 ipath[len] = '\0'; 998 path = ipath; 999 } else if (len == 0 && initpaths[ipx] != NULL) { 1000 path = initpaths[ipx++]; 1001 } else { 1002 printf("use absolute path, "); 1003 #if defined(DDB) 1004 printf("\"ddb\", "); 1005 #endif 1006 printf("\"halt\", or \"reboot\"\n"); 1007 continue; 1008 } 1009 } else { 1010 if ((path = initpaths[ipx++]) == NULL) { 1011 ipx = 0; 1012 boothowto |= RB_ASKNAME; 1013 continue; 1014 } 1015 } 1016 1017 ucp = (char *)USRSTACK; 1018 1019 /* 1020 * Construct the boot flag argument. 1021 */ 1022 flagsp = flags; 1023 *flagsp++ = '-'; 1024 options = 0; 1025 1026 if (boothowto & RB_SINGLE) { 1027 *flagsp++ = 's'; 1028 options = 1; 1029 } 1030 #ifdef notyet 1031 if (boothowto & RB_FASTBOOT) { 1032 *flagsp++ = 'f'; 1033 options = 1; 1034 } 1035 #endif 1036 1037 /* 1038 * Move out the flags (arg 1), if necessary. 1039 */ 1040 if (options != 0) { 1041 *flagsp++ = '\0'; 1042 i = flagsp - flags; 1043 #ifdef DEBUG 1044 aprint_normal("init: copying out flags `%s' %d\n", flags, i); 1045 #endif 1046 arg1 = STACK_ALLOC(ucp, i); 1047 ucp = STACK_MAX(arg1, i); 1048 (void)copyout((void *)flags, arg1, i); 1049 } 1050 1051 /* 1052 * Move out the file name (also arg 0). 1053 */ 1054 i = strlen(path) + 1; 1055 #ifdef DEBUG 1056 aprint_normal("init: copying out path `%s' %d\n", path, i); 1057 #else 1058 if (boothowto & RB_ASKNAME || path != initpaths[0]) 1059 printf("init: trying %s\n", path); 1060 #endif 1061 arg0 = STACK_ALLOC(ucp, i); 1062 ucp = STACK_MAX(arg0, i); 1063 (void)copyout(path, arg0, i); 1064 1065 /* 1066 * Move out the arg pointers. 1067 */ 1068 ucp = (void *)STACK_ALIGN(ucp, STACK_ALIGNBYTES); 1069 uap = (char **)STACK_ALLOC(ucp, sizeof(char *) * 3); 1070 SCARG(&args, path) = arg0; 1071 SCARG(&args, argp) = uap; 1072 SCARG(&args, envp) = NULL; 1073 slash = strrchr(path, '/'); 1074 if (slash) 1075 (void)suword((void *)uap++, 1076 (long)arg0 + (slash + 1 - path)); 1077 else 1078 (void)suword((void *)uap++, (long)arg0); 1079 if (options != 0) 1080 (void)suword((void *)uap++, (long)arg1); 1081 (void)suword((void *)uap++, 0); /* terminator */ 1082 1083 /* 1084 * Now try to exec the program. If can't for any reason 1085 * other than it doesn't exist, complain. 1086 */ 1087 error = sys_execve(l, &args, retval); 1088 if (error == 0 || error == EJUSTRETURN) { 1089 KERNEL_UNLOCK_LAST(l); 1090 return; 1091 } 1092 printf("exec %s: error %d\n", path, error); 1093 } 1094 printf("init: not found\n"); 1095 panic("no init"); 1096 } 1097 1098 /* 1099 * calculate cache size (in bytes) from physmem and vsize. 1100 */ 1101 vaddr_t 1102 calc_cache_size(vsize_t vsize, int pct, int va_pct) 1103 { 1104 paddr_t t; 1105 1106 /* XXX should consider competing cache if any */ 1107 /* XXX should consider submaps */ 1108 t = (uintmax_t)physmem * pct / 100 * PAGE_SIZE; 1109 if (vsize != 0) { 1110 vsize = (uintmax_t)vsize * va_pct / 100; 1111 if (t > vsize) { 1112 t = vsize; 1113 } 1114 } 1115 return t; 1116 } 1117 1118 /* 1119 * Print the system start up banner. 1120 * 1121 * - Print a limited banner if AB_SILENT. 1122 * - Always send normal banner to the log. 1123 */ 1124 #define MEM_PBUFSIZE sizeof("99999 MB") 1125 1126 void 1127 banner(void) 1128 { 1129 static char notice[] = " Notice: this software is " 1130 "protected by copyright"; 1131 char pbuf[81]; 1132 void (*pr)(const char *, ...) __printflike(1, 2); 1133 int i; 1134 1135 if ((boothowto & AB_SILENT) != 0) { 1136 snprintf(pbuf, sizeof(pbuf), "%s %s (%s)", 1137 ostype, osrelease, kernel_ident); 1138 printf_nolog("%s", pbuf); 1139 for (i = 80 - strlen(pbuf) - sizeof(notice); i > 0; i--) 1140 printf(" "); 1141 printf_nolog("%s\n", notice); 1142 pr = aprint_normal; 1143 } else { 1144 pr = printf; 1145 } 1146 1147 memset(pbuf, 0, sizeof(pbuf)); 1148 (*pr)("%s%s", copyright, version); 1149 format_bytes(pbuf, MEM_PBUFSIZE, ctob((uint64_t)physmem)); 1150 (*pr)("total memory = %s\n", pbuf); 1151 format_bytes(pbuf, MEM_PBUFSIZE, ctob((uint64_t)uvmexp.free)); 1152 (*pr)("avail memory = %s\n", pbuf); 1153 } 1154