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