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