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