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