1 /* $NetBSD: init_main.c,v 1.270 2006/07/01 05:41:10 kardel Exp $ */ 2 3 /* 4 * Copyright (c) 1982, 1986, 1989, 1991, 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * (c) UNIX System Laboratories, Inc. 7 * All or some portions of this file are derived from material licensed 8 * to the University of California by American Telephone and Telegraph 9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 10 * the permission of UNIX System Laboratories, Inc. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)init_main.c 8.16 (Berkeley) 5/14/95 37 */ 38 39 /* 40 * Copyright (c) 1995 Christopher G. Demetriou. All rights reserved. 41 * 42 * Redistribution and use in source and binary forms, with or without 43 * modification, are permitted provided that the following conditions 44 * are met: 45 * 1. Redistributions of source code must retain the above copyright 46 * notice, this list of conditions and the following disclaimer. 47 * 2. Redistributions in binary form must reproduce the above copyright 48 * notice, this list of conditions and the following disclaimer in the 49 * documentation and/or other materials provided with the distribution. 50 * 3. All advertising materials mentioning features or use of this software 51 * must display the following acknowledgement: 52 * This product includes software developed by the University of 53 * California, Berkeley and its contributors. 54 * 4. Neither the name of the University nor the names of its contributors 55 * may be used to endorse or promote products derived from this software 56 * without specific prior written permission. 57 * 58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 68 * SUCH DAMAGE. 69 * 70 * @(#)init_main.c 8.16 (Berkeley) 5/14/95 71 */ 72 73 #include <sys/cdefs.h> 74 __KERNEL_RCSID(0, "$NetBSD: init_main.c,v 1.270 2006/07/01 05:41:10 kardel Exp $"); 75 76 #include "opt_ipsec.h" 77 #include "opt_kcont.h" 78 #include "opt_multiprocessor.h" 79 #include "opt_ntp.h" 80 #include "opt_pipe.h" 81 #include "opt_posix.h" 82 #include "opt_syscall_debug.h" 83 #include "opt_sysv.h" 84 #include "opt_verified_exec.h" 85 86 #include "rnd.h" 87 88 #include <sys/param.h> 89 #include <sys/acct.h> 90 #include <sys/filedesc.h> 91 #include <sys/file.h> 92 #include <sys/errno.h> 93 #include <sys/callout.h> 94 #include <sys/kernel.h> 95 #include <sys/kcont.h> 96 #include <sys/kmem.h> 97 #include <sys/mount.h> 98 #include <sys/proc.h> 99 #include <sys/kthread.h> 100 #include <sys/resourcevar.h> 101 #include <sys/signalvar.h> 102 #include <sys/systm.h> 103 #include <sys/vnode.h> 104 #include <sys/tty.h> 105 #include <sys/conf.h> 106 #include <sys/disklabel.h> 107 #include <sys/buf.h> 108 #include <sys/device.h> 109 #include <sys/exec.h> 110 #include <sys/socketvar.h> 111 #include <sys/protosw.h> 112 #include <sys/reboot.h> 113 #include <sys/user.h> 114 #include <sys/sysctl.h> 115 #include <sys/event.h> 116 #include <sys/mbuf.h> 117 #ifdef FAST_IPSEC 118 #include <netipsec/ipsec.h> 119 #endif 120 #ifdef SYSVSHM 121 #include <sys/shm.h> 122 #endif 123 #ifdef SYSVSEM 124 #include <sys/sem.h> 125 #endif 126 #ifdef SYSVMSG 127 #include <sys/msg.h> 128 #endif 129 #ifdef P1003_1B_SEMAPHORE 130 #include <sys/ksem.h> 131 #endif 132 #include <sys/domain.h> 133 #include <sys/namei.h> 134 #if NRND > 0 135 #include <sys/rnd.h> 136 #endif 137 #ifndef PIPE_SOCKETPAIR 138 #include <sys/pipe.h> 139 #endif 140 #ifdef LKM 141 #include <sys/lkm.h> 142 #endif 143 #ifdef VERIFIED_EXEC 144 #include <sys/verified_exec.h> 145 #endif 146 #include <sys/kauth.h> 147 #include <net80211/ieee80211_netbsd.h> 148 149 #include <sys/syscall.h> 150 #include <sys/sa.h> 151 #include <sys/syscallargs.h> 152 153 #include <ufs/ufs/quota.h> 154 155 #include <miscfs/genfs/genfs.h> 156 #include <miscfs/syncfs/syncfs.h> 157 158 #include <machine/cpu.h> 159 160 #include <uvm/uvm.h> 161 162 #include <dev/cons.h> 163 164 #include <net/if.h> 165 #include <net/raw_cb.h> 166 167 extern struct proc proc0; 168 extern struct lwp lwp0; 169 extern struct cwdinfo cwdi0; 170 171 #ifndef curlwp 172 struct lwp *curlwp = &lwp0; 173 #endif 174 struct proc *initproc; 175 176 struct vnode *rootvp, *swapdev_vp; 177 int boothowto; 178 int cold = 1; /* still working on startup */ 179 #ifndef __HAVE_TIMECOUNTER 180 struct timeval boottime; 181 #endif 182 time_t rootfstime; /* recorded root fs time, if known */ 183 184 volatile int start_init_exec; /* semaphore for start_init() */ 185 186 static void check_console(struct lwp *l); 187 static void start_init(void *); 188 void main(void); 189 190 /* 191 * System startup; initialize the world, create process 0, mount root 192 * filesystem, and fork to create init and pagedaemon. Most of the 193 * hard work is done in the lower-level initialization routines including 194 * startup(), which does memory initialization and autoconfiguration. 195 */ 196 void 197 main(void) 198 { 199 #ifdef __HAVE_TIMECOUNTER 200 struct timeval time; 201 #endif 202 struct lwp *l; 203 struct proc *p; 204 struct pdevinit *pdev; 205 int s, error; 206 extern struct pdevinit pdevinit[]; 207 extern void schedcpu(void *); 208 #ifdef NVNODE_IMPLICIT 209 int usevnodes; 210 #endif 211 212 /* 213 * Initialize the current LWP pointer (curlwp) before 214 * any possible traps/probes to simplify trap processing. 215 */ 216 l = &lwp0; 217 curlwp = l; 218 l->l_cpu = curcpu(); 219 l->l_proc = &proc0; 220 l->l_lid = 1; 221 222 /* 223 * Attempt to find console and initialize 224 * in case of early panic or other messages. 225 */ 226 consinit(); 227 228 KERNEL_LOCK_INIT(); 229 230 uvm_init(); 231 232 kmem_init(); 233 234 /* Do machine-dependent initialization. */ 235 cpu_startup(); 236 237 /* Initialize callouts. */ 238 callout_startup(); 239 240 /* Initialize the buffer cache */ 241 bufinit(); 242 243 /* 244 * Initialize mbuf's. Do this now because we might attempt to 245 * allocate mbufs or mbuf clusters during autoconfiguration. 246 */ 247 mbinit(); 248 249 /* Initialize sockets. */ 250 soinit(); 251 252 #ifdef KCONT 253 /* Initialize kcont. */ 254 kcont_init(); 255 #endif 256 257 /* 258 * The following things must be done before autoconfiguration. 259 */ 260 evcnt_init(); /* initialize event counters */ 261 #if NRND > 0 262 rnd_init(); /* initialize RNG */ 263 #endif 264 /* Initialize the sysctl subsystem. */ 265 sysctl_init(); 266 267 /* Initialize process and pgrp structures. */ 268 procinit(); 269 270 /* Initialize signal-related data structures. */ 271 signal_init(); 272 273 /* Create process 0 (the swapper). */ 274 proc0_init(); 275 276 /* 277 * Charge root for one process. 278 */ 279 (void)chgproccnt(0, 1); 280 281 rqinit(); 282 283 /* Initialize the file systems. */ 284 #ifdef NVNODE_IMPLICIT 285 /* 286 * If maximum number of vnodes in namei vnode cache is not explicitly 287 * defined in kernel config, adjust the number such as we use roughly 288 * 1.0% of memory for vnode cache (but not less than NVNODE vnodes). 289 */ 290 usevnodes = (ptoa((unsigned)physmem) / 100) / sizeof(struct vnode); 291 if (usevnodes > desiredvnodes) 292 desiredvnodes = usevnodes; 293 #endif 294 vfsinit(); 295 296 297 #ifdef __HAVE_TIMECOUNTER 298 inittimecounter(); 299 ntp_init(); 300 #endif /* __HAVE_TIMECOUNTER */ 301 302 /* Configure the system hardware. This will enable interrupts. */ 303 configure(); 304 305 ubc_init(); /* must be after autoconfig */ 306 307 /* Lock the kernel on behalf of proc0. */ 308 KERNEL_PROC_LOCK(l); 309 310 #ifdef SYSVSHM 311 /* Initialize System V style shared memory. */ 312 shminit(); 313 #endif 314 315 #ifdef SYSVSEM 316 /* Initialize System V style semaphores. */ 317 seminit(); 318 #endif 319 320 #ifdef SYSVMSG 321 /* Initialize System V style message queues. */ 322 msginit(); 323 #endif 324 325 #ifdef P1003_1B_SEMAPHORE 326 /* Initialize posix semaphores */ 327 ksem_init(); 328 #endif 329 330 /* Initialize kauth. */ 331 kauth_init(); 332 333 #ifdef VERIFIED_EXEC 334 /* 335 * Initialise the fingerprint operations vectors before 336 * fingerprints can be loaded. 337 */ 338 veriexec_init_fp_ops(); 339 #endif 340 341 /* Attach pseudo-devices. */ 342 for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++) 343 (*pdev->pdev_attach)(pdev->pdev_count); 344 345 #ifdef FAST_IPSEC 346 /* Attach network crypto subsystem */ 347 ipsec_attach(); 348 #endif 349 350 /* 351 * Initialize protocols. Block reception of incoming packets 352 * until everything is ready. 353 */ 354 s = splnet(); 355 ifinit(); 356 domaininit(); 357 if_attachdomain(); 358 splx(s); 359 360 #ifdef GPROF 361 /* Initialize kernel profiling. */ 362 kmstartup(); 363 #endif 364 365 /* Initialize system accouting. */ 366 acct_init(); 367 368 /* Kick off timeout driven events by calling first time. */ 369 schedcpu(NULL); 370 371 /* 372 * Create process 1 (init(8)). We do this now, as Unix has 373 * historically had init be process 1, and changing this would 374 * probably upset a lot of people. 375 * 376 * Note that process 1 won't immediately exec init(8), but will 377 * wait for us to inform it that the root file system has been 378 * mounted. 379 */ 380 if (fork1(l, 0, SIGCHLD, NULL, 0, start_init, NULL, NULL, &initproc)) 381 panic("fork init"); 382 383 /* 384 * Create any kernel threads who's creation was deferred because 385 * initproc had not yet been created. 386 */ 387 kthread_run_deferred_queue(); 388 389 /* 390 * Now that device driver threads have been created, wait for 391 * them to finish any deferred autoconfiguration. Note we don't 392 * need to lock this semaphore, since we haven't booted any 393 * secondary processors, yet. 394 */ 395 while (config_pending) 396 (void) tsleep(&config_pending, PWAIT, "cfpend", 0); 397 398 /* 399 * Finalize configuration now that all real devices have been 400 * found. This needs to be done before the root device is 401 * selected, since finalization may create the root device. 402 */ 403 config_finalize(); 404 405 /* 406 * Now that autoconfiguration has completed, we can determine 407 * the root and dump devices. 408 */ 409 cpu_rootconf(); 410 cpu_dumpconf(); 411 412 /* Mount the root file system. */ 413 do { 414 domountroothook(); 415 if ((error = vfs_mountroot())) { 416 printf("cannot mount root, error = %d\n", error); 417 boothowto |= RB_ASKNAME; 418 setroot(root_device, 419 (rootdev != NODEV) ? DISKPART(rootdev) : 0); 420 } 421 } while (error != 0); 422 mountroothook_destroy(); 423 424 /* 425 * Initialise the time-of-day clock, passing the time recorded 426 * in the root filesystem (if any) for use by systems that 427 * don't have a non-volatile time-of-day device. 428 */ 429 inittodr(rootfstime); 430 431 CIRCLEQ_FIRST(&mountlist)->mnt_flag |= MNT_ROOTFS; 432 CIRCLEQ_FIRST(&mountlist)->mnt_op->vfs_refcount++; 433 434 /* 435 * Get the vnode for '/'. Set filedesc0.fd_fd.fd_cdir to 436 * reference it. 437 */ 438 error = VFS_ROOT(CIRCLEQ_FIRST(&mountlist), &rootvnode); 439 if (error) 440 panic("cannot find root vnode, error=%d", error); 441 cwdi0.cwdi_cdir = rootvnode; 442 VREF(cwdi0.cwdi_cdir); 443 VOP_UNLOCK(rootvnode, 0); 444 cwdi0.cwdi_rdir = NULL; 445 446 /* 447 * Now that root is mounted, we can fixup initproc's CWD 448 * info. All other processes are kthreads, which merely 449 * share proc0's CWD info. 450 */ 451 initproc->p_cwdi->cwdi_cdir = rootvnode; 452 VREF(initproc->p_cwdi->cwdi_cdir); 453 initproc->p_cwdi->cwdi_rdir = NULL; 454 455 /* 456 * Now can look at time, having had a chance to verify the time 457 * from the file system. Reset p->p_rtime as it may have been 458 * munched in mi_switch() after the time got set. 459 */ 460 proclist_lock_read(); 461 s = splsched(); 462 #ifdef __HAVE_TIMECOUNTER 463 getmicrotime(&time); 464 #else 465 mono_time = time; 466 #endif 467 boottime = time; 468 LIST_FOREACH(p, &allproc, p_list) { 469 KASSERT((p->p_flag & P_MARKER) == 0); 470 p->p_stats->p_start = time; 471 LIST_FOREACH(l, &p->p_lwps, l_sibling) { 472 if (l->l_cpu != NULL) 473 l->l_cpu->ci_schedstate.spc_runtime = time; 474 } 475 p->p_rtime.tv_sec = p->p_rtime.tv_usec = 0; 476 } 477 splx(s); 478 proclist_unlock_read(); 479 480 /* Create the pageout daemon kernel thread. */ 481 uvm_swap_init(); 482 if (kthread_create1(uvm_pageout, NULL, NULL, "pagedaemon")) 483 panic("fork pagedaemon"); 484 485 /* Create the filesystem syncer kernel thread. */ 486 if (kthread_create1(sched_sync, NULL, NULL, "ioflush")) 487 panic("fork syncer"); 488 489 /* Create the aiodone daemon kernel thread. */ 490 if (kthread_create1(uvm_aiodone_daemon, NULL, &uvm.aiodoned_proc, 491 "aiodoned")) 492 panic("fork aiodoned"); 493 494 #if defined(MULTIPROCESSOR) 495 /* Boot the secondary processors. */ 496 cpu_boot_secondary_processors(); 497 #endif 498 499 /* Initialize exec structures */ 500 exec_init(1); 501 502 /* 503 * Okay, now we can let init(8) exec! It's off to userland! 504 */ 505 start_init_exec = 1; 506 wakeup(&start_init_exec); 507 508 /* The scheduler is an infinite loop. */ 509 uvm_scheduler(); 510 /* NOTREACHED */ 511 } 512 513 void 514 setrootfstime(time_t t) 515 { 516 rootfstime = t; 517 } 518 519 static void 520 check_console(struct lwp *l) 521 { 522 struct nameidata nd; 523 int error; 524 525 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/console", l); 526 error = namei(&nd); 527 if (error == 0) 528 vrele(nd.ni_vp); 529 else if (error == ENOENT) 530 printf("warning: no /dev/console\n"); 531 else 532 printf("warning: lookup /dev/console: error %d\n", error); 533 } 534 535 /* 536 * List of paths to try when searching for "init". 537 */ 538 static const char *initpaths[] = { 539 "/sbin/init", 540 "/sbin/oinit", 541 "/sbin/init.bak", 542 NULL, 543 }; 544 545 /* 546 * Start the initial user process; try exec'ing each pathname in "initpaths". 547 * The program is invoked with one argument containing the boot flags. 548 */ 549 static void 550 start_init(void *arg) 551 { 552 struct lwp *l = arg; 553 struct proc *p = l->l_proc; 554 vaddr_t addr; 555 struct sys_execve_args /* { 556 syscallarg(const char *) path; 557 syscallarg(char * const *) argp; 558 syscallarg(char * const *) envp; 559 } */ args; 560 int options, i, error; 561 register_t retval[2]; 562 char flags[4], *flagsp; 563 const char *path, *slash; 564 char *ucp, **uap, *arg0, *arg1 = NULL; 565 char ipath[129]; 566 int ipx, len; 567 568 /* 569 * Now in process 1. 570 */ 571 strncpy(p->p_comm, "init", MAXCOMLEN); 572 573 /* 574 * Wait for main() to tell us that it's safe to exec. 575 */ 576 while (start_init_exec == 0) 577 (void) tsleep(&start_init_exec, PWAIT, "initexec", 0); 578 579 /* 580 * This is not the right way to do this. We really should 581 * hand-craft a descriptor onto /dev/console to hand to init, 582 * but that's a _lot_ more work, and the benefit from this easy 583 * hack makes up for the "good is the enemy of the best" effect. 584 */ 585 check_console(l); 586 587 /* 588 * Need just enough stack to hold the faked-up "execve()" arguments. 589 */ 590 addr = (vaddr_t)STACK_ALLOC(USRSTACK, PAGE_SIZE); 591 if (uvm_map(&p->p_vmspace->vm_map, &addr, PAGE_SIZE, 592 NULL, UVM_UNKNOWN_OFFSET, 0, 593 UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY, 594 UVM_ADV_NORMAL, 595 UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW)) != 0) 596 panic("init: couldn't allocate argument space"); 597 p->p_vmspace->vm_maxsaddr = (caddr_t)STACK_MAX(addr, PAGE_SIZE); 598 599 ipx = 0; 600 while (1) { 601 if (boothowto & RB_ASKNAME) { 602 printf("init path"); 603 if (initpaths[ipx]) 604 printf(" (default %s)", initpaths[ipx]); 605 printf(": "); 606 len = cngetsn(ipath, sizeof(ipath)-1); 607 if (len == 0) { 608 if (initpaths[ipx]) 609 path = initpaths[ipx++]; 610 else 611 continue; 612 } else { 613 ipath[len] = '\0'; 614 path = ipath; 615 } 616 } else { 617 if ((path = initpaths[ipx++]) == NULL) 618 break; 619 } 620 621 ucp = (char *)USRSTACK; 622 623 /* 624 * Construct the boot flag argument. 625 */ 626 flagsp = flags; 627 *flagsp++ = '-'; 628 options = 0; 629 630 if (boothowto & RB_SINGLE) { 631 *flagsp++ = 's'; 632 options = 1; 633 } 634 #ifdef notyet 635 if (boothowto & RB_FASTBOOT) { 636 *flagsp++ = 'f'; 637 options = 1; 638 } 639 #endif 640 641 /* 642 * Move out the flags (arg 1), if necessary. 643 */ 644 if (options != 0) { 645 *flagsp++ = '\0'; 646 i = flagsp - flags; 647 #ifdef DEBUG 648 printf("init: copying out flags `%s' %d\n", flags, i); 649 #endif 650 arg1 = STACK_ALLOC(ucp, i); 651 ucp = STACK_MAX(arg1, i); 652 (void)copyout((caddr_t)flags, arg1, i); 653 } 654 655 /* 656 * Move out the file name (also arg 0). 657 */ 658 i = strlen(path) + 1; 659 #ifdef DEBUG 660 printf("init: copying out path `%s' %d\n", path, i); 661 #else 662 if (boothowto & RB_ASKNAME || path != initpaths[0]) 663 printf("init: trying %s\n", path); 664 #endif 665 arg0 = STACK_ALLOC(ucp, i); 666 ucp = STACK_MAX(arg0, i); 667 (void)copyout(path, arg0, i); 668 669 /* 670 * Move out the arg pointers. 671 */ 672 ucp = (caddr_t)STACK_ALIGN(ucp, ALIGNBYTES); 673 uap = (char **)STACK_ALLOC(ucp, sizeof(char *) * 3); 674 SCARG(&args, path) = arg0; 675 SCARG(&args, argp) = uap; 676 SCARG(&args, envp) = NULL; 677 slash = strrchr(path, '/'); 678 if (slash) 679 (void)suword((caddr_t)uap++, 680 (long)arg0 + (slash + 1 - path)); 681 else 682 (void)suword((caddr_t)uap++, (long)arg0); 683 if (options != 0) 684 (void)suword((caddr_t)uap++, (long)arg1); 685 (void)suword((caddr_t)uap++, 0); /* terminator */ 686 687 /* 688 * Now try to exec the program. If can't for any reason 689 * other than it doesn't exist, complain. 690 */ 691 error = sys_execve(l, &args, retval); 692 if (error == 0 || error == EJUSTRETURN) { 693 KERNEL_PROC_UNLOCK(l); 694 return; 695 } 696 printf("exec %s: error %d\n", path, error); 697 } 698 printf("init: not found\n"); 699 panic("no init"); 700 } 701