1 /* $OpenBSD: init_main.c,v 1.92 2002/01/30 20:29:44 nordin 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/proc.h> 54 #include <sys/resourcevar.h> 55 #include <sys/signalvar.h> 56 #include <sys/systm.h> 57 #include <sys/namei.h> 58 #include <sys/vnode.h> 59 #include <sys/tty.h> 60 #include <sys/conf.h> 61 #include <sys/buf.h> 62 #include <sys/device.h> 63 #include <sys/socketvar.h> 64 #include <sys/protosw.h> 65 #include <sys/reboot.h> 66 #include <sys/user.h> 67 #ifdef SYSVSHM 68 #include <sys/shm.h> 69 #endif 70 #ifdef SYSVSEM 71 #include <sys/sem.h> 72 #endif 73 #ifdef SYSVMSG 74 #include <sys/msg.h> 75 #endif 76 #include <sys/domain.h> 77 #include <sys/mbuf.h> 78 #include <sys/pipe.h> 79 80 #include <sys/syscall.h> 81 #include <sys/syscallargs.h> 82 83 #include <ufs/ufs/quota.h> 84 85 #include <machine/cpu.h> 86 87 #include <uvm/uvm.h> 88 89 #include <net/if.h> 90 #include <net/raw_cb.h> 91 92 #if defined(CRYPTO) 93 #include <crypto/cryptodev.h> 94 #include <crypto/cryptosoft.h> 95 #endif 96 97 #if defined(NFSSERVER) || defined(NFSCLIENT) 98 extern void nfs_init __P((void)); 99 #endif 100 101 char copyright[] = 102 "Copyright (c) 1982, 1986, 1989, 1991, 1993\n" 103 "\tThe Regents of the University of California. All rights reserved.\n" 104 "Copyright (c) 1995-2002 OpenBSD. All rights reserved. http://www.OpenBSD.org\n"; 105 106 /* Components of the first process -- never freed. */ 107 struct session session0; 108 struct pgrp pgrp0; 109 struct proc proc0; 110 struct pcred cred0; 111 struct filedesc0 filedesc0; 112 struct plimit limit0; 113 struct vmspace vmspace0; 114 struct sigacts sigacts0; 115 #ifndef curproc 116 struct proc *curproc; 117 #endif 118 struct proc *initproc; 119 120 int cmask = CMASK; 121 extern struct user *proc0paddr; 122 123 void (*md_diskconf) __P((void)) = NULL; 124 struct vnode *rootvp, *swapdev_vp; 125 int boothowto; 126 struct timeval boottime; 127 struct timeval runtime; 128 129 /* XXX return int so gcc -Werror won't complain */ 130 int main __P((void *)); 131 void check_console __P((struct proc *)); 132 void start_init __P((void *)); 133 void start_cleaner __P((void *)); 134 void start_update __P((void *)); 135 void start_reaper __P((void *)); 136 void start_crypto __P((void *)); 137 138 extern char sigcode[], esigcode[]; 139 #ifdef SYSCALL_DEBUG 140 extern char *syscallnames[]; 141 #endif 142 143 struct emul emul_native = { 144 "native", 145 NULL, 146 sendsig, 147 SYS_syscall, 148 SYS_MAXSYSCALL, 149 sysent, 150 #ifdef SYSCALL_DEBUG 151 syscallnames, 152 #else 153 NULL, 154 #endif 155 0, 156 copyargs, 157 setregs, 158 NULL, 159 sigcode, 160 esigcode, 161 }; 162 163 /* 164 * System startup; initialize the world, create process 0, mount root 165 * filesystem, and fork to create init and pagedaemon. Most of the 166 * hard work is done in the lower-level initialization routines including 167 * startup(), which does memory initialization and autoconfiguration. 168 */ 169 /* XXX return int, so gcc -Werror won't complain */ 170 int 171 main(framep) 172 void *framep; /* XXX should go away */ 173 { 174 register struct proc *p; 175 register struct pdevinit *pdev; 176 struct timeval rtv; 177 register int i; 178 int s; 179 register_t rval[2]; 180 extern struct pdevinit pdevinit[]; 181 extern void scheduler_start __P((void)); 182 extern void disk_init __P((void)); 183 extern void endtsleep __P((void *)); 184 extern void realitexpire __P((void *)); 185 186 /* 187 * Initialize the current process pointer (curproc) before 188 * any possible traps/probes to simplify trap processing. 189 */ 190 p = &proc0; 191 curproc = p; 192 193 /* 194 * Attempt to find console and initialize 195 * in case of early panic or other messages. 196 */ 197 config_init(); /* init autoconfiguration data structures */ 198 consinit(); 199 printf(copyright); 200 printf("\n"); 201 202 uvm_init(); 203 disk_init(); /* must come before autoconfiguration */ 204 tty_init(); /* initialise tty's */ 205 cpu_startup(); 206 207 /* 208 * Initialize mbuf's. Do this now because we might attempt to 209 * allocate mbufs or mbuf clusters during autoconfiguration. 210 */ 211 mbinit(); 212 213 /* Initalize sockets. */ 214 soinit(); 215 216 /* 217 * Initialize timeouts. 218 */ 219 timeout_startup(); 220 221 cpu_configure(); 222 223 /* Initialize sysctls (must be done before any processes run) */ 224 sysctl_init(); 225 226 /* 227 * Initialize process and pgrp structures. 228 */ 229 procinit(); 230 231 /* 232 * Initialize filedescriptors. 233 */ 234 filedesc_init(); 235 236 /* 237 * Initialize pipes. 238 */ 239 pipe_init(); 240 241 /* 242 * Create process 0 (the swapper). 243 */ 244 LIST_INSERT_HEAD(&allproc, p, p_list); 245 p->p_pgrp = &pgrp0; 246 LIST_INSERT_HEAD(PIDHASH(0), p, p_hash); 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