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