1 /* $OpenBSD: init_main.c,v 1.90 2002/01/01 20:41:56 deraadt Exp $ */ 2 /* $NetBSD: init_main.c,v 1.84.4.1 1996/06/02 09:08:06 mrg Exp $ */ 3 4 /* 5 * Copyright (c) 1995 Christopher G. Demetriou. All rights reserved. 6 * Copyright (c) 1982, 1986, 1989, 1991, 1992, 1993 7 * The Regents of the University of California. All rights reserved. 8 * (c) UNIX System Laboratories, Inc. 9 * All or some portions of this file are derived from material licensed 10 * to the University of California by American Telephone and Telegraph 11 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 12 * the permission of UNIX System Laboratories, Inc. 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions 16 * are met: 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in the 21 * documentation and/or other materials provided with the distribution. 22 * 3. All advertising materials mentioning features or use of this software 23 * must display the following acknowledgement: 24 * This product includes software developed by the University of 25 * California, Berkeley and its contributors. 26 * 4. Neither the name of the University nor the names of its contributors 27 * may be used to endorse or promote products derived from this software 28 * without specific prior written permission. 29 * 30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 40 * SUCH DAMAGE. 41 * 42 * @(#)init_main.c 8.9 (Berkeley) 1/21/94 43 */ 44 45 #include <sys/param.h> 46 #include <sys/filedesc.h> 47 #include <sys/file.h> 48 #include <sys/errno.h> 49 #include <sys/exec.h> 50 #include <sys/kernel.h> 51 #include <sys/kthread.h> 52 #include <sys/mount.h> 53 #include <sys/map.h> 54 #include <sys/proc.h> 55 #include <sys/resourcevar.h> 56 #include <sys/signalvar.h> 57 #include <sys/systm.h> 58 #include <sys/namei.h> 59 #include <sys/vnode.h> 60 #include <sys/tty.h> 61 #include <sys/conf.h> 62 #include <sys/buf.h> 63 #include <sys/device.h> 64 #include <sys/socketvar.h> 65 #include <sys/protosw.h> 66 #include <sys/reboot.h> 67 #include <sys/user.h> 68 #ifdef SYSVSHM 69 #include <sys/shm.h> 70 #endif 71 #ifdef SYSVSEM 72 #include <sys/sem.h> 73 #endif 74 #ifdef SYSVMSG 75 #include <sys/msg.h> 76 #endif 77 #include <sys/domain.h> 78 #include <sys/mbuf.h> 79 #include <sys/pipe.h> 80 81 #include <sys/syscall.h> 82 #include <sys/syscallargs.h> 83 84 #include <ufs/ufs/quota.h> 85 86 #include <machine/cpu.h> 87 88 #include <uvm/uvm.h> 89 90 #include <net/if.h> 91 #include <net/raw_cb.h> 92 93 #if defined(CRYPTO) 94 #include <crypto/cryptodev.h> 95 #include <crypto/cryptosoft.h> 96 #endif 97 98 #if defined(NFSSERVER) || defined(NFSCLIENT) 99 extern void nfs_init __P((void)); 100 #endif 101 102 char copyright[] = 103 "Copyright (c) 1982, 1986, 1989, 1991, 1993\n" 104 "\tThe Regents of the University of California. All rights reserved.\n" 105 "Copyright (c) 1995-2002 OpenBSD. All rights reserved. http://www.OpenBSD.org\n"; 106 107 /* Components of the first process -- never freed. */ 108 struct session session0; 109 struct pgrp pgrp0; 110 struct proc proc0; 111 struct pcred cred0; 112 struct filedesc0 filedesc0; 113 struct plimit limit0; 114 struct vmspace vmspace0; 115 struct sigacts sigacts0; 116 #ifndef curproc 117 struct proc *curproc; 118 #endif 119 struct proc *initproc; 120 121 int cmask = CMASK; 122 extern struct user *proc0paddr; 123 124 void (*md_diskconf) __P((void)) = NULL; 125 struct vnode *rootvp, *swapdev_vp; 126 int boothowto; 127 struct timeval boottime; 128 struct timeval runtime; 129 130 /* XXX return int so gcc -Werror won't complain */ 131 int main __P((void *)); 132 void check_console __P((struct proc *)); 133 void start_init __P((void *)); 134 void start_cleaner __P((void *)); 135 void start_update __P((void *)); 136 void start_reaper __P((void *)); 137 void start_crypto __P((void *)); 138 139 extern char sigcode[], esigcode[]; 140 #ifdef SYSCALL_DEBUG 141 extern char *syscallnames[]; 142 #endif 143 144 struct emul emul_native = { 145 "native", 146 NULL, 147 sendsig, 148 SYS_syscall, 149 SYS_MAXSYSCALL, 150 sysent, 151 #ifdef SYSCALL_DEBUG 152 syscallnames, 153 #else 154 NULL, 155 #endif 156 0, 157 copyargs, 158 setregs, 159 NULL, 160 sigcode, 161 esigcode, 162 }; 163 164 /* 165 * System startup; initialize the world, create process 0, mount root 166 * filesystem, and fork to create init and pagedaemon. Most of the 167 * hard work is done in the lower-level initialization routines including 168 * startup(), which does memory initialization and autoconfiguration. 169 */ 170 /* XXX return int, so gcc -Werror won't complain */ 171 int 172 main(framep) 173 void *framep; /* XXX should go away */ 174 { 175 register struct proc *p; 176 register struct pdevinit *pdev; 177 struct timeval rtv; 178 register int i; 179 int s; 180 register_t rval[2]; 181 extern struct pdevinit pdevinit[]; 182 extern void scheduler_start __P((void)); 183 extern void disk_init __P((void)); 184 extern void endtsleep __P((void *)); 185 extern void realitexpire __P((void *)); 186 187 /* 188 * Initialize the current process pointer (curproc) before 189 * any possible traps/probes to simplify trap processing. 190 */ 191 p = &proc0; 192 curproc = p; 193 194 /* 195 * Attempt to find console and initialize 196 * in case of early panic or other messages. 197 */ 198 config_init(); /* init autoconfiguration data structures */ 199 consinit(); 200 printf(copyright); 201 printf("\n"); 202 203 uvm_init(); 204 disk_init(); /* must come before autoconfiguration */ 205 tty_init(); /* initialise tty's */ 206 cpu_startup(); 207 208 /* 209 * Initialize mbuf's. Do this now because we might attempt to 210 * allocate mbufs or mbuf clusters during autoconfiguration. 211 */ 212 mbinit(); 213 214 /* Initalize sockets. */ 215 soinit(); 216 217 /* 218 * Initialize timeouts. 219 */ 220 timeout_startup(); 221 222 cpu_configure(); 223 224 /* Initialize sysctls (must be done before any processes run) */ 225 sysctl_init(); 226 227 /* 228 * Initialize process and pgrp structures. 229 */ 230 procinit(); 231 232 /* 233 * Initialize filedescriptors. 234 */ 235 filedesc_init(); 236 237 /* 238 * Initialize pipes. 239 */ 240 pipe_init(); 241 242 /* 243 * Create process 0 (the swapper). 244 */ 245 LIST_INSERT_HEAD(&allproc, p, p_list); 246 p->p_pgrp = &pgrp0; 247 LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash); 248 LIST_INIT(&pgrp0.pg_members); 249 LIST_INSERT_HEAD(&pgrp0.pg_members, p, p_pglist); 250 251 pgrp0.pg_session = &session0; 252 session0.s_count = 1; 253 session0.s_leader = p; 254 255 p->p_flag = P_INMEM | P_SYSTEM | P_NOCLDWAIT; 256 p->p_stat = SRUN; 257 p->p_nice = NZERO; 258 p->p_emul = &emul_native; 259 bcopy("swapper", p->p_comm, sizeof ("swapper")); 260 261 /* Init timeouts. */ 262 timeout_set(&p->p_sleep_to, endtsleep, p); 263 timeout_set(&p->p_realit_to, realitexpire, p); 264 265 /* Create credentials. */ 266 cred0.p_refcnt = 1; 267 p->p_cred = &cred0; 268 p->p_ucred = crget(); 269 p->p_ucred->cr_ngroups = 1; /* group 0 */ 270 271 /* Create the file descriptor table. */ 272 p->p_fd = &filedesc0.fd_fd; 273 filedesc0.fd_fd.fd_refcnt = 1; 274 filedesc0.fd_fd.fd_cmask = cmask; 275 filedesc0.fd_fd.fd_ofiles = filedesc0.fd_dfiles; 276 filedesc0.fd_fd.fd_ofileflags = filedesc0.fd_dfileflags; 277 filedesc0.fd_fd.fd_nfiles = NDFILE; 278 filedesc0.fd_fd.fd_himap = filedesc0.fd_dhimap; 279 filedesc0.fd_fd.fd_lomap = filedesc0.fd_dlomap; 280 281 /* Create the limits structures. */ 282 p->p_limit = &limit0; 283 for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++) 284 limit0.pl_rlimit[i].rlim_cur = 285 limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY; 286 limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur = NOFILE; 287 limit0.pl_rlimit[RLIMIT_NOFILE].rlim_max = MIN(NOFILE_MAX, 288 (maxfiles - NOFILE > NOFILE) ? maxfiles - NOFILE : NOFILE); 289 limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur = MAXUPRC; 290 i = ptoa(uvmexp.free); 291 limit0.pl_rlimit[RLIMIT_RSS].rlim_max = i; 292 limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = i; 293 limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = i / 3; 294 limit0.p_refcnt = 1; 295 296 /* Allocate a prototype map so we have something to fork. */ 297 uvmspace_init(&vmspace0, pmap_kernel(), round_page(VM_MIN_ADDRESS), 298 trunc_page(VM_MAX_ADDRESS), TRUE); 299 p->p_vmspace = &vmspace0; 300 301 p->p_addr = proc0paddr; /* XXX */ 302 303 /* 304 * We continue to place resource usage info in the 305 * user struct so they're pageable. 306 */ 307 p->p_stats = &p->p_addr->u_stats; 308 309 /* 310 * Charge root for one process. 311 */ 312 (void)chgproccnt(0, 1); 313 314 rqinit(); 315 316 /* Configure virtual memory system, set vm rlimits. */ 317 uvm_init_limits(p); 318 319 /* Initialize the file systems. */ 320 #if defined(NFSSERVER) || defined(NFSCLIENT) 321 nfs_init(); /* initialize server/shared data */ 322 #endif 323 vfsinit(); 324 325 /* Start real time and statistics clocks. */ 326 initclocks(); 327 328 #ifdef SYSVSHM 329 /* Initialize System V style shared memory. */ 330 shminit(); 331 #endif 332 333 #ifdef SYSVSEM 334 /* Initialize System V style semaphores. */ 335 seminit(); 336 #endif 337 338 #ifdef SYSVMSG 339 /* Initialize System V style message queues. */ 340 msginit(); 341 #endif 342 343 /* Attach pseudo-devices. */ 344 randomattach(); 345 for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++) 346 if (pdev->pdev_count > 0) 347 (*pdev->pdev_attach)(pdev->pdev_count); 348 349 #ifdef CRYPTO 350 swcr_init(); 351 #endif /* CRYPTO */ 352 353 /* 354 * Initialize protocols. Block reception of incoming packets 355 * until everything is ready. 356 */ 357 s = splimp(); 358 ifinit(); 359 domaininit(); 360 splx(s); 361 362 #ifdef GPROF 363 /* Initialize kernel profiling. */ 364 kmstartup(); 365 #endif 366 367 /* Start the scheduler */ 368 scheduler_start(); 369 370 /* Initialize signal state for process 0. */ 371 signal_init(); 372 p->p_sigacts = &sigacts0; 373 siginit(p); 374 375 dostartuphooks(); 376 377 /* Configure root/swap devices */ 378 if (md_diskconf) 379 (*md_diskconf)(); 380 381 /* Mount the root file system. */ 382 if (vfs_mountroot()) 383 panic("cannot mount root"); 384 CIRCLEQ_FIRST(&mountlist)->mnt_flag |= MNT_ROOTFS; 385 386 /* Get the vnode for '/'. Set filedesc0.fd_fd.fd_cdir to reference it. */ 387 if (VFS_ROOT(mountlist.cqh_first, &rootvnode)) 388 panic("cannot find root vnode"); 389 filedesc0.fd_fd.fd_cdir = rootvnode; 390 VREF(filedesc0.fd_fd.fd_cdir); 391 VOP_UNLOCK(rootvnode, 0, p); 392 filedesc0.fd_fd.fd_rdir = NULL; 393 394 uvm_swap_init(); 395 396 /* 397 * Now can look at time, having had a chance to verify the time 398 * from the file system. Reset p->p_rtime as it may have been 399 * munched in mi_switch() after the time got set. 400 */ 401 p->p_stats->p_start = runtime = mono_time = boottime = time; 402 p->p_rtime.tv_sec = p->p_rtime.tv_usec = 0; 403 404 /* Create process 1 (init(8)). */ 405 if (fork1(p, SIGCHLD, FORK_FORK, NULL, 0, start_init, NULL, rval)) 406 panic("fork init"); 407 408 /* Create process 2, the pageout daemon kernel thread. */ 409 if (kthread_create(uvm_pageout, NULL, NULL, "pagedaemon")) 410 panic("fork pagedaemon"); 411 412 /* Create process 3, the reaper daemon kernel thread. */ 413 if (kthread_create(start_reaper, NULL, NULL, "reaper")) 414 panic("fork reaper"); 415 416 /* Create process 4, the cleaner daemon kernel thread. */ 417 if (kthread_create(start_cleaner, NULL, NULL, "cleaner")) 418 panic("fork cleaner"); 419 420 /* Create process 5, the update daemon kernel thread. */ 421 if (kthread_create(start_update, NULL, NULL, "update")) 422 panic("fork update"); 423 424 /* Create process 6, the aiodone daemon kernel thread. */ 425 if (kthread_create(uvm_aiodone_daemon, NULL, NULL, "aiodoned")) 426 panic("fork aiodoned"); 427 428 #ifdef CRYPTO 429 /* Create process 7, the crypto kernel thread. */ 430 if (kthread_create(start_crypto, NULL, NULL, "crypto")) 431 panic("crypto thread"); 432 #endif /* CRYPTO */ 433 434 /* Create any other deferred kernel threads. */ 435 kthread_run_deferred_queue(); 436 437 microtime(&rtv); 438 srandom((u_long)(rtv.tv_sec ^ rtv.tv_usec)); 439 440 randompid = 1; 441 /* The scheduler is an infinite loop. */ 442 uvm_scheduler(); 443 /* NOTREACHED */ 444 } 445 446 /* 447 * List of paths to try when searching for "init". 448 */ 449 static char *initpaths[] = { 450 "/sbin/init", 451 "/sbin/oinit", 452 "/sbin/init.bak", 453 NULL, 454 }; 455 456 void 457 check_console(p) 458 struct proc *p; 459 { 460 struct nameidata nd; 461 int error; 462 463 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/console", p); 464 error = namei(&nd); 465 if (error) { 466 if (error == ENOENT) 467 printf("warning: /dev/console does not exist\n"); 468 else 469 printf("warning: /dev/console error %d\n", error); 470 } else 471 vrele(nd.ni_vp); 472 } 473 474 /* 475 * Start the initial user process; try exec'ing each pathname in "initpaths". 476 * The program is invoked with one argument containing the boot flags. 477 */ 478 void 479 start_init(arg) 480 void *arg; 481 { 482 struct proc *p = arg; 483 vaddr_t addr; 484 struct sys_execve_args /* { 485 syscallarg(char *) path; 486 syscallarg(char **) argp; 487 syscallarg(char **) envp; 488 } */ args; 489 int options, i, error; 490 register_t retval[2]; 491 char flags[4], *flagsp; 492 char **pathp, *path, *ucp, **uap, *arg0, *arg1 = NULL; 493 494 initproc = p; 495 496 /* 497 * Now in process 1. 498 */ 499 check_console(p); 500 501 /* 502 * Need just enough stack to hold the faked-up "execve()" arguments. 503 */ 504 #ifdef MACHINE_STACK_GROWS_UP 505 addr = USRSTACK; 506 #else 507 addr = USRSTACK - PAGE_SIZE; 508 #endif 509 if (uvm_map(&p->p_vmspace->vm_map, &addr, PAGE_SIZE, 510 NULL, UVM_UNKNOWN_OFFSET, 0, 511 UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY, 512 UVM_ADV_NORMAL, UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW))) 513 panic("init: couldn't allocate argument space"); 514 #ifdef MACHINE_STACK_GROWS_UP 515 p->p_vmspace->vm_maxsaddr = (caddr_t)addr + PAGE_SIZE; 516 #else 517 p->p_vmspace->vm_maxsaddr = (caddr_t)addr; 518 #endif 519 520 for (pathp = &initpaths[0]; (path = *pathp) != NULL; pathp++) { 521 #ifdef MACHINE_STACK_GROWS_UP 522 ucp = (char *)addr; 523 #else 524 ucp = (char *)(addr + PAGE_SIZE); 525 #endif 526 /* 527 * Construct the boot flag argument. 528 */ 529 flagsp = flags; 530 *flagsp++ = '-'; 531 options = 0; 532 533 if (boothowto & RB_SINGLE) { 534 *flagsp++ = 's'; 535 options = 1; 536 } 537 #ifdef notyet 538 if (boothowto & RB_FASTBOOT) { 539 *flagsp++ = 'f'; 540 options = 1; 541 } 542 #endif 543 544 /* 545 * Move out the flags (arg 1), if necessary. 546 */ 547 if (options != 0) { 548 *flagsp++ = '\0'; 549 i = flagsp - flags; 550 #ifdef DEBUG 551 printf("init: copying out flags `%s' %d\n", flags, i); 552 #endif 553 #ifdef MACHINE_STACK_GROWS_UP 554 arg1 = ucp; 555 (void)copyout((caddr_t)flags, (caddr_t)ucp, i); 556 ucp += i; 557 #else 558 (void)copyout((caddr_t)flags, (caddr_t)(ucp -= i), i); 559 arg1 = ucp; 560 #endif 561 } 562 563 /* 564 * Move out the file name (also arg 0). 565 */ 566 i = strlen(path) + 1; 567 #ifdef DEBUG 568 printf("init: copying out path `%s' %d\n", path, i); 569 #endif 570 #ifdef MACHINE_STACK_GROWS_UP 571 arg0 = ucp; 572 (void)copyout((caddr_t)path, (caddr_t)ucp, i); 573 ucp += i; 574 ucp = (caddr_t)ALIGN((u_long)ucp); 575 uap = (char **)ucp + 3; 576 #else 577 (void)copyout((caddr_t)path, (caddr_t)(ucp -= i), i); 578 arg0 = ucp; 579 uap = (char **)((u_long)ucp & ~ALIGNBYTES); 580 #endif 581 582 /* 583 * Move out the arg pointers. 584 */ 585 (void)suword((caddr_t)--uap, 0); /* terminator */ 586 if (options != 0) 587 (void)suword((caddr_t)--uap, (long)arg1); 588 (void)suword((caddr_t)--uap, (long)arg0); 589 590 /* 591 * Point at the arguments. 592 */ 593 SCARG(&args, path) = arg0; 594 SCARG(&args, argp) = uap; 595 SCARG(&args, envp) = NULL; 596 597 /* 598 * Now try to exec the program. If can't for any reason 599 * other than it doesn't exist, complain. 600 */ 601 if ((error = sys_execve(p, &args, retval)) == 0) 602 return; 603 if (error != ENOENT) 604 printf("exec %s: error %d\n", path, error); 605 } 606 printf("init: not found\n"); 607 panic("no init"); 608 } 609 610 void 611 start_update(arg) 612 void *arg; 613 { 614 sched_sync(curproc); 615 /* NOTREACHED */ 616 } 617 618 void 619 start_cleaner(arg) 620 void *arg; 621 { 622 buf_daemon(curproc); 623 /* NOTREACHED */ 624 } 625 626 void 627 start_reaper(arg) 628 void *arg; 629 { 630 reaper(); 631 /* NOTREACHED */ 632 } 633 634 #ifdef CRYPTO 635 void 636 start_crypto(arg) 637 void *arg; 638 { 639 crypto_thread(); 640 /* NOTREACHED */ 641 } 642 #endif /* CRYPTO */ 643