1 /* $NetBSD: init_main.c,v 1.372 2008/11/12 12:36:16 ad Exp $ */ 2 3 /*- 4 * Copyright (c) 2008 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.372 2008/11/12 12:36:16 ad Exp $"); 101 102 #include "opt_ddb.h" 103 #include "opt_ipsec.h" 104 #include "opt_ntp.h" 105 #include "opt_pipe.h" 106 #include "opt_posix.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_wapbl.h" 113 114 #include "rnd.h" 115 #include "sysmon_envsys.h" 116 #include "sysmon_power.h" 117 #include "sysmon_taskq.h" 118 #include "sysmon_wdog.h" 119 #include "veriexec.h" 120 121 #include <sys/param.h> 122 #include <sys/acct.h> 123 #include <sys/filedesc.h> 124 #include <sys/file.h> 125 #include <sys/errno.h> 126 #include <sys/callout.h> 127 #include <sys/cpu.h> 128 #include <sys/kernel.h> 129 #include <sys/mount.h> 130 #include <sys/proc.h> 131 #include <sys/kthread.h> 132 #include <sys/resourcevar.h> 133 #include <sys/signalvar.h> 134 #include <sys/systm.h> 135 #include <sys/vnode.h> 136 #include <sys/fstrans.h> 137 #include <sys/tty.h> 138 #include <sys/conf.h> 139 #include <sys/disklabel.h> 140 #include <sys/buf.h> 141 #include <sys/device.h> 142 #include <sys/exec.h> 143 #include <sys/socketvar.h> 144 #include <sys/protosw.h> 145 #include <sys/percpu.h> 146 #include <sys/pset.h> 147 #include <sys/sysctl.h> 148 #include <sys/reboot.h> 149 #include <sys/user.h> 150 #include <sys/sysctl.h> 151 #include <sys/event.h> 152 #include <sys/mbuf.h> 153 #include <sys/sched.h> 154 #include <sys/sleepq.h> 155 #include <sys/iostat.h> 156 #include <sys/vmem.h> 157 #include <sys/uuid.h> 158 #include <sys/extent.h> 159 #include <sys/disk.h> 160 #include <sys/mqueue.h> 161 #include <sys/msgbuf.h> 162 #include <sys/module.h> 163 #include <sys/event.h> 164 #include <sys/lockf.h> 165 #include <sys/once.h> 166 #include <sys/ksyms.h> 167 #include <sys/uidinfo.h> 168 #ifdef FAST_IPSEC 169 #include <netipsec/ipsec.h> 170 #endif 171 #ifdef SYSVSHM 172 #include <sys/shm.h> 173 #endif 174 #ifdef SYSVSEM 175 #include <sys/sem.h> 176 #endif 177 #ifdef SYSVMSG 178 #include <sys/msg.h> 179 #endif 180 #ifdef P1003_1B_SEMAPHORE 181 #include <sys/ksem.h> 182 #endif 183 #include <sys/domain.h> 184 #include <sys/namei.h> 185 #if NRND > 0 186 #include <sys/rnd.h> 187 #endif 188 #include <sys/pipe.h> 189 #if NVERIEXEC > 0 190 #include <sys/verified_exec.h> 191 #endif /* NVERIEXEC > 0 */ 192 #ifdef KTRACE 193 #include <sys/ktrace.h> 194 #endif 195 #include <sys/kauth.h> 196 #ifdef WAPBL 197 #include <sys/wapbl.h> 198 #endif 199 #include <net80211/ieee80211_netbsd.h> 200 201 #include <sys/syscall.h> 202 #include <sys/syscallargs.h> 203 204 #if defined(PAX_MPROTECT) || defined(PAX_SEGVGUARD) || defined(PAX_ASLR) 205 #include <sys/pax.h> 206 #endif /* PAX_MPROTECT || PAX_SEGVGUARD || PAX_ASLR */ 207 208 #include <ufs/ufs/quota.h> 209 210 #include <miscfs/genfs/genfs.h> 211 #include <miscfs/syncfs/syncfs.h> 212 213 #include <sys/cpu.h> 214 215 #include <uvm/uvm.h> 216 217 #if NSYSMON_TASKQ > 0 218 #include <dev/sysmon/sysmon_taskq.h> 219 #endif 220 221 #include <dev/cons.h> 222 223 #if NSYSMON_ENVSYS > 0 || NSYSMON_POWER > 0 || NSYSMON_WDOG > 0 224 #include <dev/sysmon/sysmonvar.h> 225 #endif 226 227 #include <net/if.h> 228 #include <net/raw_cb.h> 229 230 #include <secmodel/secmodel.h> 231 232 extern struct proc proc0; 233 extern struct lwp lwp0; 234 extern struct cwdinfo cwdi0; 235 extern time_t rootfstime; 236 237 #ifndef curlwp 238 struct lwp *curlwp = &lwp0; 239 #endif 240 struct proc *initproc; 241 242 struct vnode *rootvp, *swapdev_vp; 243 int boothowto; 244 int cold = 1; /* still working on startup */ 245 struct timeval boottime; /* time at system startup - will only follow settime deltas */ 246 247 int start_init_exec; /* semaphore for start_init() */ 248 249 static void check_console(struct lwp *l); 250 static void start_init(void *); 251 void main(void); 252 void ssp_init(void); 253 254 #if defined(__SSP__) || defined(__SSP_ALL__) 255 long __stack_chk_guard[8] = {0, 0, 0, 0, 0, 0, 0, 0}; 256 void __stack_chk_fail(void); 257 258 void 259 __stack_chk_fail(void) 260 { 261 panic("stack overflow detected; terminated"); 262 } 263 264 void 265 ssp_init(void) 266 { 267 int s; 268 269 #ifdef DIAGNOSTIC 270 printf("Initializing SSP:"); 271 #endif 272 /* 273 * We initialize ssp here carefully: 274 * 1. after we got some entropy 275 * 2. without calling a function 276 */ 277 size_t i; 278 long guard[__arraycount(__stack_chk_guard)]; 279 280 arc4randbytes(guard, sizeof(guard)); 281 s = splhigh(); 282 for (i = 0; i < __arraycount(guard); i++) 283 __stack_chk_guard[i] = guard[i]; 284 splx(s); 285 #ifdef DIAGNOSTIC 286 for (i = 0; i < __arraycount(guard); i++) 287 printf("%lx ", guard[i]); 288 printf("\n"); 289 #endif 290 } 291 #else 292 void 293 ssp_init(void) 294 { 295 296 } 297 #endif 298 299 void __secmodel_none(void); 300 __weak_alias(secmodel_start,__secmodel_none); 301 void 302 __secmodel_none(void) 303 { 304 return; 305 } 306 307 /* 308 * System startup; initialize the world, create process 0, mount root 309 * filesystem, and fork to create init and pagedaemon. Most of the 310 * hard work is done in the lower-level initialization routines including 311 * startup(), which does memory initialization and autoconfiguration. 312 */ 313 void 314 main(void) 315 { 316 struct timeval time; 317 struct lwp *l; 318 struct proc *p; 319 int s, error; 320 #ifdef NVNODE_IMPLICIT 321 int usevnodes; 322 #endif 323 CPU_INFO_ITERATOR cii; 324 struct cpu_info *ci; 325 326 l = &lwp0; 327 #ifndef LWP0_CPU_INFO 328 l->l_cpu = curcpu(); 329 #endif 330 331 /* 332 * Attempt to find console and initialize 333 * in case of early panic or other messages. 334 */ 335 consinit(); 336 337 kernel_lock_init(); 338 once_init(); 339 340 uvm_init(); 341 342 percpu_init(); 343 344 /* Initialize lock caches. */ 345 mutex_obj_init(); 346 347 /* Initialize the extent manager. */ 348 extent_init(); 349 350 /* Do machine-dependent initialization. */ 351 cpu_startup(); 352 353 /* Initialize callouts, part 1. */ 354 callout_startup(); 355 356 /* 357 * Initialize the kernel authorization subsystem and start the 358 * default security model, if any. We need to do this early 359 * enough so that subsystems relying on any of the aforementioned 360 * can work properly. Since the security model may dictate the 361 * credential inheritance policy, it is needed at least before 362 * any process is created, specifically proc0. 363 */ 364 kauth_init(); 365 secmodel_start(); 366 367 /* Initialize the buffer cache */ 368 bufinit(); 369 370 /* Initialize sockets. */ 371 soinit(); 372 373 /* 374 * The following things must be done before autoconfiguration. 375 */ 376 evcnt_init(); /* initialize event counters */ 377 #if NRND > 0 378 rnd_init(); /* initialize random number generator */ 379 #endif 380 381 /* Initialize process and pgrp structures. */ 382 procinit(); 383 lwpinit(); 384 385 /* Initialize signal-related data structures. */ 386 signal_init(); 387 388 /* Initialize resource management. */ 389 resource_init(); 390 391 /* Create process 0 (the swapper). */ 392 proc0_init(); 393 394 /* Initialize the UID hash table. */ 395 uid_init(); 396 397 /* Charge root for one process. */ 398 (void)chgproccnt(0, 1); 399 400 /* Initialize timekeeping. */ 401 time_init(); 402 403 /* Initialize the run queues, turnstiles and sleep queues. */ 404 mutex_init(&cpu_lock, MUTEX_DEFAULT, IPL_NONE); 405 sched_rqinit(); 406 turnstile_init(); 407 sleeptab_init(&sleeptab); 408 409 /* Initialize processor-sets */ 410 psets_init(); 411 412 /* MI initialization of the boot cpu */ 413 error = mi_cpu_attach(curcpu()); 414 KASSERT(error == 0); 415 416 /* Initialize timekeeping, part 2. */ 417 time_init2(); 418 419 /* 420 * Initialize mbuf's. Do this now because we might attempt to 421 * allocate mbufs or mbuf clusters during autoconfiguration. 422 */ 423 mbinit(); 424 425 /* Initialize the sysctl subsystem. */ 426 sysctl_init(); 427 428 /* Initialize I/O statistics. */ 429 iostat_init(); 430 431 /* Initialize the log device. */ 432 loginit(); 433 434 /* Start module system. */ 435 module_init(); 436 437 /* Initialize the file systems. */ 438 #ifdef NVNODE_IMPLICIT 439 /* 440 * If maximum number of vnodes in namei vnode cache is not explicitly 441 * defined in kernel config, adjust the number such as we use roughly 442 * 10% of memory for vnodes and associated data structures in the 443 * assumed worst case. Do not provide fewer than NVNODE vnodes. 444 */ 445 usevnodes = 446 calc_cache_size(kernel_map, 10, VNODE_VA_MAXPCT) / VNODE_COST; 447 if (usevnodes > desiredvnodes) 448 desiredvnodes = usevnodes; 449 #endif 450 vfsinit(); 451 lf_init(); 452 453 /* Initialize fstrans. */ 454 fstrans_init(); 455 456 /* Initialize the file descriptor system. */ 457 fd_sys_init(); 458 459 /* Initialize kqueue. */ 460 kqueue_init(); 461 462 /* Initialize asynchronous I/O. */ 463 aio_sysinit(); 464 465 /* Initialize message queues. */ 466 mqueue_sysinit(); 467 468 /* Initialize the system monitor subsystems. */ 469 #if NSYSMON_TASKQ > 0 470 sysmon_task_queue_preinit(); 471 #endif 472 473 #if NSYSMON_ENVSYS > 0 474 sysmon_envsys_init(); 475 #endif 476 477 #if NSYSMON_POWER > 0 478 sysmon_power_init(); 479 #endif 480 481 #if NSYSMON_WDOG > 0 482 sysmon_wdog_init(); 483 #endif 484 485 inittimecounter(); 486 ntp_init(); 487 488 /* Initialize the device switch tables. */ 489 devsw_init(); 490 491 /* Initialize tty subsystem. */ 492 tty_init(); 493 ttyldisc_init(); 494 495 /* Initialize the buffer cache, part 2. */ 496 bufinit2(); 497 498 /* Initialize the disk wedge subsystem. */ 499 dkwedge_init(); 500 501 /* Initialize interfaces. */ 502 ifinit1(); 503 504 /* Configure the system hardware. This will enable interrupts. */ 505 configure(); 506 507 ubc_init(); /* must be after autoconfig */ 508 509 #ifdef SYSVSHM 510 /* Initialize System V style shared memory. */ 511 shminit(); 512 #endif 513 514 #ifdef SYSVSEM 515 /* Initialize System V style semaphores. */ 516 seminit(); 517 #endif 518 519 #ifdef SYSVMSG 520 /* Initialize System V style message queues. */ 521 msginit(); 522 #endif 523 524 #ifdef P1003_1B_SEMAPHORE 525 /* Initialize posix semaphores */ 526 ksem_init(); 527 #endif 528 529 #if NVERIEXEC > 0 530 /* 531 * Initialise the Veriexec subsystem. 532 */ 533 veriexec_init(); 534 #endif /* NVERIEXEC > 0 */ 535 536 #if defined(PAX_MPROTECT) || defined(PAX_SEGVGUARD) || defined(PAX_ASLR) 537 pax_init(); 538 #endif /* PAX_MPROTECT || PAX_SEGVGUARD || PAX_ASLR */ 539 540 #ifdef FAST_IPSEC 541 /* Attach network crypto subsystem */ 542 ipsec_attach(); 543 #endif 544 545 /* 546 * Initialize protocols. Block reception of incoming packets 547 * until everything is ready. 548 */ 549 s = splnet(); 550 ifinit(); 551 domaininit(); 552 if_attachdomain(); 553 splx(s); 554 555 #ifdef GPROF 556 /* Initialize kernel profiling. */ 557 kmstartup(); 558 #endif 559 560 /* Initialize system accounting. */ 561 acct_init(); 562 563 #ifndef PIPE_SOCKETPAIR 564 /* Initialize pipes. */ 565 pipe_init(); 566 #endif 567 568 #ifdef KTRACE 569 /* Initialize ktrace. */ 570 ktrinit(); 571 #endif 572 573 /* Initialize the UUID system calls. */ 574 uuid_init(); 575 576 #ifdef WAPBL 577 /* Initialize write-ahead physical block logging. */ 578 wapbl_init(); 579 #endif 580 581 /* 582 * Create process 1 (init(8)). We do this now, as Unix has 583 * historically had init be process 1, and changing this would 584 * probably upset a lot of people. 585 * 586 * Note that process 1 won't immediately exec init(8), but will 587 * wait for us to inform it that the root file system has been 588 * mounted. 589 */ 590 if (fork1(l, 0, SIGCHLD, NULL, 0, start_init, NULL, NULL, &initproc)) 591 panic("fork init"); 592 593 /* 594 * Load any remaining builtin modules, and hand back temporary 595 * storage to the VM system. 596 */ 597 module_init_class(MODULE_CLASS_ANY); 598 599 /* 600 * Finalize configuration now that all real devices have been 601 * found. This needs to be done before the root device is 602 * selected, since finalization may create the root device. 603 */ 604 config_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 CIRCLEQ_FIRST(&mountlist)->mnt_flag |= MNT_ROOTFS; 633 CIRCLEQ_FIRST(&mountlist)->mnt_op->vfs_refcount++; 634 635 /* 636 * Get the vnode for '/'. Set filedesc0.fd_fd.fd_cdir to 637 * reference it. 638 */ 639 error = VFS_ROOT(CIRCLEQ_FIRST(&mountlist), &rootvnode); 640 if (error) 641 panic("cannot find root vnode, error=%d", error); 642 cwdi0.cwdi_cdir = rootvnode; 643 VREF(cwdi0.cwdi_cdir); 644 VOP_UNLOCK(rootvnode, 0); 645 cwdi0.cwdi_rdir = NULL; 646 647 /* 648 * Now that root is mounted, we can fixup initproc's CWD 649 * info. All other processes are kthreads, which merely 650 * share proc0's CWD info. 651 */ 652 initproc->p_cwdi->cwdi_cdir = rootvnode; 653 VREF(initproc->p_cwdi->cwdi_cdir); 654 initproc->p_cwdi->cwdi_rdir = NULL; 655 656 /* 657 * Now can look at time, having had a chance to verify the time 658 * from the file system. Reset l->l_rtime as it may have been 659 * munched in mi_switch() after the time got set. 660 */ 661 getmicrotime(&time); 662 boottime = time; 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 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 static void 717 check_console(struct lwp *l) 718 { 719 struct nameidata nd; 720 int error; 721 722 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/console"); 723 error = namei(&nd); 724 if (error == 0) 725 vrele(nd.ni_vp); 726 else if (error == ENOENT) 727 printf("warning: no /dev/console\n"); 728 else 729 printf("warning: lookup /dev/console: error %d\n", error); 730 } 731 732 /* 733 * List of paths to try when searching for "init". 734 */ 735 static const char * const initpaths[] = { 736 "/sbin/init", 737 "/sbin/oinit", 738 "/sbin/init.bak", 739 NULL, 740 }; 741 742 /* 743 * Start the initial user process; try exec'ing each pathname in "initpaths". 744 * The program is invoked with one argument containing the boot flags. 745 */ 746 static void 747 start_init(void *arg) 748 { 749 struct lwp *l = arg; 750 struct proc *p = l->l_proc; 751 vaddr_t addr; 752 struct sys_execve_args /* { 753 syscallarg(const char *) path; 754 syscallarg(char * const *) argp; 755 syscallarg(char * const *) envp; 756 } */ args; 757 int options, i, error; 758 register_t retval[2]; 759 char flags[4], *flagsp; 760 const char *path, *slash; 761 char *ucp, **uap, *arg0, *arg1 = NULL; 762 char ipath[129]; 763 int ipx, len; 764 765 /* 766 * Now in process 1. 767 */ 768 strncpy(p->p_comm, "init", MAXCOMLEN); 769 770 /* 771 * Wait for main() to tell us that it's safe to exec. 772 */ 773 mutex_enter(proc_lock); 774 while (start_init_exec == 0) 775 cv_wait(&lbolt, proc_lock); 776 mutex_exit(proc_lock); 777 778 /* 779 * This is not the right way to do this. We really should 780 * hand-craft a descriptor onto /dev/console to hand to init, 781 * but that's a _lot_ more work, and the benefit from this easy 782 * hack makes up for the "good is the enemy of the best" effect. 783 */ 784 check_console(l); 785 786 /* 787 * Need just enough stack to hold the faked-up "execve()" arguments. 788 */ 789 addr = (vaddr_t)STACK_ALLOC(USRSTACK, PAGE_SIZE); 790 if (uvm_map(&p->p_vmspace->vm_map, &addr, PAGE_SIZE, 791 NULL, UVM_UNKNOWN_OFFSET, 0, 792 UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY, 793 UVM_ADV_NORMAL, 794 UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW)) != 0) 795 panic("init: couldn't allocate argument space"); 796 p->p_vmspace->vm_maxsaddr = (void *)STACK_MAX(addr, PAGE_SIZE); 797 798 ipx = 0; 799 while (1) { 800 if (boothowto & RB_ASKNAME) { 801 printf("init path"); 802 if (initpaths[ipx]) 803 printf(" (default %s)", initpaths[ipx]); 804 printf(": "); 805 len = cngetsn(ipath, sizeof(ipath)-1); 806 if (len == 4 && strcmp(ipath, "halt") == 0) { 807 cpu_reboot(RB_HALT, NULL); 808 } else if (len == 6 && strcmp(ipath, "reboot") == 0) { 809 cpu_reboot(0, NULL); 810 #if defined(DDB) 811 } else if (len == 3 && strcmp(ipath, "ddb") == 0) { 812 console_debugger(); 813 continue; 814 #endif 815 } else if (len > 0 && ipath[0] == '/') { 816 ipath[len] = '\0'; 817 path = ipath; 818 } else if (len == 0 && initpaths[ipx] != NULL) { 819 path = initpaths[ipx++]; 820 } else { 821 printf("use absolute path, "); 822 #if defined(DDB) 823 printf("\"ddb\", "); 824 #endif 825 printf("\"halt\", or \"reboot\"\n"); 826 continue; 827 } 828 } else { 829 if ((path = initpaths[ipx++]) == NULL) { 830 ipx = 0; 831 boothowto |= RB_ASKNAME; 832 continue; 833 } 834 } 835 836 ucp = (char *)USRSTACK; 837 838 /* 839 * Construct the boot flag argument. 840 */ 841 flagsp = flags; 842 *flagsp++ = '-'; 843 options = 0; 844 845 if (boothowto & RB_SINGLE) { 846 *flagsp++ = 's'; 847 options = 1; 848 } 849 #ifdef notyet 850 if (boothowto & RB_FASTBOOT) { 851 *flagsp++ = 'f'; 852 options = 1; 853 } 854 #endif 855 856 /* 857 * Move out the flags (arg 1), if necessary. 858 */ 859 if (options != 0) { 860 *flagsp++ = '\0'; 861 i = flagsp - flags; 862 #ifdef DEBUG 863 printf("init: copying out flags `%s' %d\n", flags, i); 864 #endif 865 arg1 = STACK_ALLOC(ucp, i); 866 ucp = STACK_MAX(arg1, i); 867 (void)copyout((void *)flags, arg1, i); 868 } 869 870 /* 871 * Move out the file name (also arg 0). 872 */ 873 i = strlen(path) + 1; 874 #ifdef DEBUG 875 printf("init: copying out path `%s' %d\n", path, i); 876 #else 877 if (boothowto & RB_ASKNAME || path != initpaths[0]) 878 printf("init: trying %s\n", path); 879 #endif 880 arg0 = STACK_ALLOC(ucp, i); 881 ucp = STACK_MAX(arg0, i); 882 (void)copyout(path, arg0, i); 883 884 /* 885 * Move out the arg pointers. 886 */ 887 ucp = (void *)STACK_ALIGN(ucp, ALIGNBYTES); 888 uap = (char **)STACK_ALLOC(ucp, sizeof(char *) * 3); 889 SCARG(&args, path) = arg0; 890 SCARG(&args, argp) = uap; 891 SCARG(&args, envp) = NULL; 892 slash = strrchr(path, '/'); 893 if (slash) 894 (void)suword((void *)uap++, 895 (long)arg0 + (slash + 1 - path)); 896 else 897 (void)suword((void *)uap++, (long)arg0); 898 if (options != 0) 899 (void)suword((void *)uap++, (long)arg1); 900 (void)suword((void *)uap++, 0); /* terminator */ 901 902 /* 903 * Now try to exec the program. If can't for any reason 904 * other than it doesn't exist, complain. 905 */ 906 error = sys_execve(l, &args, retval); 907 if (error == 0 || error == EJUSTRETURN) { 908 KERNEL_UNLOCK_LAST(l); 909 return; 910 } 911 printf("exec %s: error %d\n", path, error); 912 } 913 printf("init: not found\n"); 914 panic("no init"); 915 } 916 917 /* 918 * calculate cache size from physmem and vm_map size. 919 */ 920 vaddr_t 921 calc_cache_size(struct vm_map *map, int pct, int va_pct) 922 { 923 paddr_t t; 924 925 /* XXX should consider competing cache if any */ 926 /* XXX should consider submaps */ 927 t = (uintmax_t)physmem * pct / 100 * PAGE_SIZE; 928 if (map != NULL) { 929 vsize_t vsize; 930 931 vsize = vm_map_max(map) - vm_map_min(map); 932 vsize = (uintmax_t)vsize * va_pct / 100; 933 if (t > vsize) { 934 t = vsize; 935 } 936 } 937 return t; 938 } 939