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