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