1 /* $OpenBSD: init_main.c,v 1.73 2001/07/27 09:55:07 niklas 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/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 <vm/vm.h> 88 89 #include <uvm/uvm.h> 90 91 #include <net/if.h> 92 #include <net/raw_cb.h> 93 94 #if defined(CRYPTO) 95 #include <crypto/cryptodev.h> 96 #include <crypto/cryptosoft.h> 97 #endif 98 99 #if defined(NFSSERVER) || defined(NFSCLIENT) 100 extern void nfs_init __P((void)); 101 #endif 102 103 char copyright[] = 104 "Copyright (c) 1982, 1986, 1989, 1991, 1993\n" 105 "\tThe Regents of the University of California. All rights reserved.\n" 106 "Copyright (c) 1995-2001 OpenBSD. All rights reserved. http://www.OpenBSD.org\n"; 107 108 /* Components of the first process -- never freed. */ 109 struct session session0; 110 struct pgrp pgrp0; 111 struct proc proc0; 112 struct pcred cred0; 113 struct filedesc0 filedesc0; 114 struct plimit limit0; 115 struct vmspace vmspace0; 116 struct sigacts sigacts0; 117 #ifndef curproc 118 struct proc *curproc; 119 #endif 120 struct proc *initproc; 121 122 int cmask = CMASK; 123 extern struct user *proc0paddr; 124 125 void (*md_diskconf) __P((void)) = NULL; 126 struct vnode *rootvp, *swapdev_vp; 127 int boothowto; 128 struct timeval boottime; 129 struct timeval runtime; 130 131 /* XXX return int so gcc -Werror won't complain */ 132 int main __P((void *)); 133 void check_console __P((struct proc *)); 134 void start_init __P((void *)); 135 void start_pagedaemon __P((void *)); 136 void start_update __P((void *)); 137 void start_reaper __P((void *)); 138 void start_crypto __P((void *)); 139 140 extern char sigcode[], esigcode[]; 141 #ifdef SYSCALL_DEBUG 142 extern char *syscallnames[]; 143 #endif 144 145 struct emul emul_native = { 146 "native", 147 NULL, 148 sendsig, 149 SYS_syscall, 150 SYS_MAXSYSCALL, 151 sysent, 152 #ifdef SYSCALL_DEBUG 153 syscallnames, 154 #else 155 NULL, 156 #endif 157 0, 158 copyargs, 159 setregs, 160 NULL, 161 sigcode, 162 esigcode, 163 }; 164 165 /* 166 * System startup; initialize the world, create process 0, mount root 167 * filesystem, and fork to create init and pagedaemon. Most of the 168 * hard work is done in the lower-level initialization routines including 169 * startup(), which does memory initialization and autoconfiguration. 170 */ 171 /* XXX return int, so gcc -Werror won't complain */ 172 int 173 main(framep) 174 void *framep; /* XXX should go away */ 175 { 176 register struct proc *p; 177 register struct pdevinit *pdev; 178 struct timeval rtv; 179 register int i; 180 int s; 181 register_t rval[2]; 182 extern struct pdevinit pdevinit[]; 183 extern void scheduler_start __P((void)); 184 extern void disk_init __P((void)); 185 extern void endtsleep __P((void *)); 186 extern void realitexpire __P((void *)); 187 188 /* 189 * Initialize the current process pointer (curproc) before 190 * any possible traps/probes to simplify trap processing. 191 */ 192 p = &proc0; 193 curproc = p; 194 195 /* 196 * Attempt to find console and initialize 197 * in case of early panic or other messages. 198 */ 199 config_init(); /* init autoconfiguration data structures */ 200 consinit(); 201 printf(copyright); 202 printf("\n"); 203 204 uvm_init(); 205 disk_init(); /* must come before autoconfiguration */ 206 tty_init(); /* initialise tty's */ 207 cpu_startup(); 208 209 /* 210 * Initialize mbuf's. Do this now because we might attempt to 211 * allocate mbufs or mbuf clusters during autoconfiguration. 212 */ 213 mbinit(); 214 215 cpu_configure(); 216 217 /* Initialize sysctls (must be done before any processes run) */ 218 sysctl_init(); 219 220 /* 221 * Initialize process and pgrp structures. 222 */ 223 procinit(); 224 225 /* 226 * Initialize filedescriptors. 227 */ 228 filedesc_init(); 229 230 /* 231 * Initialize pipes. 232 */ 233 pipe_init(); 234 235 /* 236 * Create process 0 (the swapper). 237 */ 238 LIST_INSERT_HEAD(&allproc, p, p_list); 239 p->p_pgrp = &pgrp0; 240 LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash); 241 LIST_INIT(&pgrp0.pg_members); 242 LIST_INSERT_HEAD(&pgrp0.pg_members, p, p_pglist); 243 244 pgrp0.pg_session = &session0; 245 session0.s_count = 1; 246 session0.s_leader = p; 247 248 p->p_flag = P_INMEM | P_SYSTEM | P_NOCLDWAIT; 249 p->p_stat = SRUN; 250 p->p_nice = NZERO; 251 p->p_emul = &emul_native; 252 bcopy("swapper", p->p_comm, sizeof ("swapper")); 253 254 /* Init timeouts. */ 255 timeout_set(&p->p_sleep_to, endtsleep, p); 256 timeout_set(&p->p_realit_to, realitexpire, p); 257 258 /* Create credentials. */ 259 cred0.p_refcnt = 1; 260 p->p_cred = &cred0; 261 p->p_ucred = crget(); 262 p->p_ucred->cr_ngroups = 1; /* group 0 */ 263 264 /* Create the file descriptor table. */ 265 p->p_fd = &filedesc0.fd_fd; 266 filedesc0.fd_fd.fd_refcnt = 1; 267 filedesc0.fd_fd.fd_cmask = cmask; 268 filedesc0.fd_fd.fd_ofiles = filedesc0.fd_dfiles; 269 filedesc0.fd_fd.fd_ofileflags = filedesc0.fd_dfileflags; 270 filedesc0.fd_fd.fd_nfiles = NDFILE; 271 filedesc0.fd_fd.fd_himap = filedesc0.fd_dhimap; 272 filedesc0.fd_fd.fd_lomap = filedesc0.fd_dlomap; 273 274 /* Create the limits structures. */ 275 p->p_limit = &limit0; 276 for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++) 277 limit0.pl_rlimit[i].rlim_cur = 278 limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY; 279 limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur = NOFILE; 280 limit0.pl_rlimit[RLIMIT_NOFILE].rlim_max = MIN(NOFILE_MAX, 281 (maxfiles - NOFILE > NOFILE) ? maxfiles - NOFILE : NOFILE); 282 limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur = MAXUPRC; 283 i = ptoa(uvmexp.free); 284 limit0.pl_rlimit[RLIMIT_RSS].rlim_max = i; 285 limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = i; 286 limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = i / 3; 287 limit0.p_refcnt = 1; 288 289 /* Allocate a prototype map so we have something to fork. */ 290 uvmspace_init(&vmspace0, pmap_kernel(), round_page(VM_MIN_ADDRESS), 291 trunc_page(VM_MAX_ADDRESS), TRUE); 292 p->p_vmspace = &vmspace0; 293 294 p->p_addr = proc0paddr; /* XXX */ 295 296 /* 297 * We continue to place resource usage info in the 298 * user struct so they're pageable. 299 */ 300 p->p_stats = &p->p_addr->u_stats; 301 302 /* 303 * Charge root for one process. 304 */ 305 (void)chgproccnt(0, 1); 306 307 rqinit(); 308 309 /* Configure virtual memory system, set vm rlimits. */ 310 uvm_init_limits(p); 311 312 /* Initialize the file systems. */ 313 #if defined(NFSSERVER) || defined(NFSCLIENT) 314 nfs_init(); /* initialize server/shared data */ 315 #endif 316 vfsinit(); 317 318 /* Start real time and statistics clocks. */ 319 initclocks(); 320 321 #ifdef SYSVSHM 322 /* Initialize System V style shared memory. */ 323 shminit(); 324 #endif 325 326 #ifdef SYSVSEM 327 /* Initialize System V style semaphores. */ 328 seminit(); 329 #endif 330 331 #ifdef SYSVMSG 332 /* Initialize System V style message queues. */ 333 msginit(); 334 #endif 335 336 /* Attach pseudo-devices. */ 337 randomattach(); 338 for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++) 339 if (pdev->pdev_count > 0) 340 (*pdev->pdev_attach)(pdev->pdev_count); 341 342 #ifdef CRYPTO 343 swcr_init(); 344 #endif /* CRYPTO */ 345 346 /* 347 * Initialize protocols. Block reception of incoming packets 348 * until everything is ready. 349 */ 350 s = splimp(); 351 ifinit(); 352 domaininit(); 353 splx(s); 354 355 #ifdef GPROF 356 /* Initialize kernel profiling. */ 357 kmstartup(); 358 #endif 359 360 /* Start the scheduler */ 361 scheduler_start(); 362 363 dostartuphooks(); 364 365 /* Configure root/swap devices */ 366 if (md_diskconf) 367 (*md_diskconf)(); 368 369 /* Mount the root file system. */ 370 if (vfs_mountroot()) 371 panic("cannot mount root"); 372 CIRCLEQ_FIRST(&mountlist)->mnt_flag |= MNT_ROOTFS; 373 374 /* Get the vnode for '/'. Set filedesc0.fd_fd.fd_cdir to reference it. */ 375 if (VFS_ROOT(mountlist.cqh_first, &rootvnode)) 376 panic("cannot find root vnode"); 377 filedesc0.fd_fd.fd_cdir = rootvnode; 378 VREF(filedesc0.fd_fd.fd_cdir); 379 VOP_UNLOCK(rootvnode, 0, p); 380 filedesc0.fd_fd.fd_rdir = NULL; 381 382 uvm_swap_init(); 383 384 /* 385 * Now can look at time, having had a chance to verify the time 386 * from the file system. Reset p->p_rtime as it may have been 387 * munched in mi_switch() after the time got set. 388 */ 389 p->p_stats->p_start = runtime = mono_time = boottime = time; 390 p->p_rtime.tv_sec = p->p_rtime.tv_usec = 0; 391 392 /* Initialize signal state for process 0. */ 393 signal_init(); 394 p->p_sigacts = &sigacts0; 395 siginit(p); 396 397 /* Create process 1 (init(8)). */ 398 if (fork1(p, SIGCHLD, FORK_FORK, NULL, 0, rval)) 399 panic("fork init"); 400 cpu_set_kpc(pfind(rval[0]), start_init, pfind(rval[0])); 401 402 /* Create process 2, the pageout daemon kernel thread. */ 403 if (kthread_create(start_pagedaemon, NULL, NULL, "pagedaemon")) 404 panic("fork pagedaemon"); 405 406 /* Create process 3, the reaper daemon kernel thread. */ 407 if (kthread_create(start_reaper, NULL, NULL, "reaper")) 408 panic("fork reaper"); 409 410 /* Create process 4, the update daemon kernel thread. */ 411 if (kthread_create(start_update, NULL, NULL, "update")) { 412 #ifdef DIAGNOSTIC 413 panic("fork update"); 414 #endif 415 } 416 417 #ifdef CRYPTO 418 /* Create process 5, the crypto kernel thread. */ 419 if (kthread_create(start_crypto, NULL, NULL, "crypto")) 420 panic("crypto thread"); 421 #endif /* CRYPTO */ 422 423 /* Create any other deferred kernel threads. */ 424 kthread_run_deferred_queue(); 425 426 microtime(&rtv); 427 srandom((u_long)(rtv.tv_sec ^ rtv.tv_usec)); 428 429 randompid = 1; 430 /* The scheduler is an infinite loop. */ 431 uvm_scheduler(); 432 /* NOTREACHED */ 433 } 434 435 /* 436 * List of paths to try when searching for "init". 437 */ 438 static char *initpaths[] = { 439 "/sbin/init", 440 "/sbin/oinit", 441 "/sbin/init.bak", 442 NULL, 443 }; 444 445 void 446 check_console(p) 447 struct proc *p; 448 { 449 struct nameidata nd; 450 int error; 451 452 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/console", p); 453 error = namei(&nd); 454 if (error) { 455 if (error == ENOENT) 456 printf("warning: /dev/console does not exist\n"); 457 else 458 printf("warning: /dev/console error %d\n", error); 459 } else 460 vrele(nd.ni_vp); 461 } 462 463 /* 464 * Start the initial user process; try exec'ing each pathname in "initpaths". 465 * The program is invoked with one argument containing the boot flags. 466 */ 467 void 468 start_init(arg) 469 void *arg; 470 { 471 struct proc *p = arg; 472 vaddr_t addr; 473 struct sys_execve_args /* { 474 syscallarg(char *) path; 475 syscallarg(char **) argp; 476 syscallarg(char **) envp; 477 } */ args; 478 int options, i, error; 479 register_t retval[2]; 480 char flags[4], *flagsp; 481 char **pathp, *path, *ucp, **uap, *arg0, *arg1 = NULL; 482 483 /* 484 * Now in process 1. 485 */ 486 initproc = p; 487 488 check_console(p); 489 490 /* 491 * Need just enough stack to hold the faked-up "execve()" arguments. 492 */ 493 #ifdef MACHINE_STACK_GROWS_UP 494 addr = USRSTACK; 495 #else 496 addr = USRSTACK - PAGE_SIZE; 497 #endif 498 if (uvm_map(&p->p_vmspace->vm_map, &addr, PAGE_SIZE, 499 NULL, UVM_UNKNOWN_OFFSET, 500 UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY, 501 UVM_ADV_NORMAL, UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW)) 502 != KERN_SUCCESS) 503 panic("init: couldn't allocate argument space"); 504 #ifdef MACHINE_STACK_GROWS_UP 505 p->p_vmspace->vm_maxsaddr = (caddr_t)addr + PAGE_SIZE; 506 #else 507 p->p_vmspace->vm_maxsaddr = (caddr_t)addr; 508 #endif 509 510 for (pathp = &initpaths[0]; (path = *pathp) != NULL; pathp++) { 511 #ifdef MACHINE_STACK_GROWS_UP 512 ucp = (char *)addr; 513 #else 514 ucp = (char *)(addr + PAGE_SIZE); 515 #endif 516 /* 517 * Construct the boot flag argument. 518 */ 519 flagsp = flags; 520 *flagsp++ = '-'; 521 options = 0; 522 523 if (boothowto & RB_SINGLE) { 524 *flagsp++ = 's'; 525 options = 1; 526 } 527 #ifdef notyet 528 if (boothowto & RB_FASTBOOT) { 529 *flagsp++ = 'f'; 530 options = 1; 531 } 532 #endif 533 534 /* 535 * Move out the flags (arg 1), if necessary. 536 */ 537 if (options != 0) { 538 *flagsp++ = '\0'; 539 i = flagsp - flags; 540 #ifdef DEBUG 541 printf("init: copying out flags `%s' %d\n", flags, i); 542 #endif 543 #ifdef MACHINE_STACK_GROWS_UP 544 arg1 = ucp; 545 (void)copyout((caddr_t)flags, (caddr_t)ucp, i); 546 ucp += i; 547 #else 548 (void)copyout((caddr_t)flags, (caddr_t)(ucp -= i), i); 549 arg1 = ucp; 550 #endif 551 } 552 553 /* 554 * Move out the file name (also arg 0). 555 */ 556 i = strlen(path) + 1; 557 #ifdef DEBUG 558 printf("init: copying out path `%s' %d\n", path, i); 559 #endif 560 #ifdef MACHINE_STACK_GROWS_UP 561 arg0 = ucp; 562 (void)copyout((caddr_t)path, (caddr_t)ucp, i); 563 ucp += i; 564 ucp = (caddr_t)ALIGN((u_long)ucp); 565 uap = (char **)ucp + 3; 566 #else 567 (void)copyout((caddr_t)path, (caddr_t)(ucp -= i), i); 568 arg0 = ucp; 569 uap = (char **)((u_long)ucp & ~ALIGNBYTES); 570 #endif 571 572 /* 573 * Move out the arg pointers. 574 */ 575 (void)suword((caddr_t)--uap, 0); /* terminator */ 576 if (options != 0) 577 (void)suword((caddr_t)--uap, (long)arg1); 578 (void)suword((caddr_t)--uap, (long)arg0); 579 580 /* 581 * Point at the arguments. 582 */ 583 SCARG(&args, path) = arg0; 584 SCARG(&args, argp) = uap; 585 SCARG(&args, envp) = NULL; 586 587 /* 588 * Now try to exec the program. If can't for any reason 589 * other than it doesn't exist, complain. 590 */ 591 if ((error = sys_execve(p, &args, retval)) == 0) 592 return; 593 if (error != ENOENT) 594 printf("exec %s: error %d\n", path, error); 595 } 596 printf("init: not found\n"); 597 panic("no init"); 598 } 599 600 void 601 start_pagedaemon(arg) 602 void *arg; 603 { 604 uvm_pageout(); 605 /* NOTREACHED */ 606 } 607 608 void 609 start_update(arg) 610 void *arg; 611 { 612 sched_sync(curproc); 613 /* NOTREACHED */ 614 } 615 616 void 617 start_reaper(arg) 618 void *arg; 619 { 620 reaper(); 621 /* NOTREACHED */ 622 } 623 624 #ifdef CRYPTO 625 void 626 start_crypto(arg) 627 void *arg; 628 { 629 crypto_thread(); 630 /* NOTREACHED */ 631 } 632 #endif /* CRYPTO */ 633