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