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