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