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