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