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