1 /* $NetBSD: init_main.c,v 1.108 1997/10/19 02:00:19 mycroft Exp $ */ 2 3 /* 4 * Copyright (c) 1995 Christopher G. Demetriou. All rights reserved. 5 * Copyright (c) 1982, 1986, 1989, 1991, 1992, 1993 6 * The Regents of the University of California. All rights reserved. 7 * (c) UNIX System Laboratories, Inc. 8 * All or some portions of this file are derived from material licensed 9 * to the University of California by American Telephone and Telegraph 10 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 11 * the permission of UNIX System Laboratories, Inc. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgement: 23 * This product includes software developed by the University of 24 * California, Berkeley and its contributors. 25 * 4. Neither the name of the University nor the names of its contributors 26 * may be used to endorse or promote products derived from this software 27 * without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39 * SUCH DAMAGE. 40 * 41 * @(#)init_main.c 8.9 (Berkeley) 1/21/94 42 */ 43 44 #include "rnd.h" 45 46 #include <sys/param.h> 47 #include <sys/filedesc.h> 48 #include <sys/errno.h> 49 #include <sys/exec.h> 50 #include <sys/kernel.h> 51 #include <sys/mount.h> 52 #include <sys/map.h> 53 #include <sys/proc.h> 54 #include <sys/resourcevar.h> 55 #include <sys/signalvar.h> 56 #include <sys/systm.h> 57 #include <sys/vnode.h> 58 #include <sys/tty.h> 59 #include <sys/conf.h> 60 #include <sys/disklabel.h> 61 #include <sys/buf.h> 62 #ifdef REAL_CLISTS 63 #include <sys/clist.h> 64 #endif 65 #include <sys/device.h> 66 #include <sys/protosw.h> 67 #include <sys/reboot.h> 68 #include <sys/user.h> 69 #ifdef SYSVSHM 70 #include <sys/shm.h> 71 #endif 72 #ifdef SYSVSEM 73 #include <sys/sem.h> 74 #endif 75 #ifdef SYSVMSG 76 #include <sys/msg.h> 77 #endif 78 #include <sys/domain.h> 79 #include <sys/mbuf.h> 80 #include <sys/namei.h> 81 #if NRND > 0 82 #include <sys/rnd.h> 83 #endif 84 85 #include <sys/syscall.h> 86 #include <sys/syscallargs.h> 87 88 #include <ufs/ufs/quota.h> 89 90 #include <machine/cpu.h> 91 92 #include <vm/vm.h> 93 #include <vm/vm_pageout.h> 94 95 #include <net/if.h> 96 #include <net/raw_cb.h> 97 98 char copyright[] = "\ 99 Copyright (c) 1996, 1997 The NetBSD Foundation, Inc. All rights reserved. 100 Copyright (c) 1982, 1986, 1989, 1991, 1993 101 The Regents of the University of California. All rights reserved. 102 103 "; 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 filedesc0 filedesc0; 111 struct plimit limit0; 112 struct vmspace vmspace0; 113 struct proc *curproc = &proc0; 114 struct proc *initproc; 115 116 int cmask = CMASK; 117 extern struct user *proc0paddr; 118 119 struct vnode *rootvp, *swapdev_vp; 120 int boothowto; 121 struct timeval boottime; 122 struct timeval runtime; 123 124 static void check_console __P((struct proc *p)); 125 static void start_init __P((struct proc *)); 126 static void start_pagedaemon __P((struct proc *)); 127 void main __P((void *)); 128 129 #ifdef cpu_set_init_frame 130 void *initframep; /* XXX should go away */ 131 #endif 132 133 extern char sigcode[], esigcode[]; 134 #ifdef SYSCALL_DEBUG 135 extern char *syscallnames[]; 136 #endif 137 138 struct emul emul_netbsd = { 139 "netbsd", 140 NULL, 141 sendsig, 142 SYS_syscall, 143 SYS_MAXSYSCALL, 144 sysent, 145 #ifdef SYSCALL_DEBUG 146 syscallnames, 147 #else 148 NULL, 149 #endif 150 0, 151 copyargs, 152 setregs, 153 sigcode, 154 esigcode, 155 }; 156 157 /* 158 * System startup; initialize the world, create process 0, mount root 159 * filesystem, and fork to create init and pagedaemon. Most of the 160 * hard work is done in the lower-level initialization routines including 161 * startup(), which does memory initialization and autoconfiguration. 162 */ 163 void 164 main(framep) 165 void *framep; /* XXX should go away */ 166 { 167 register struct proc *p; 168 register struct pdevinit *pdev; 169 register int i; 170 int s, error; 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 #if defined(NFSSERVER) || defined(NFS) 177 extern void nfs_init __P((void)); 178 #endif 179 180 /* 181 * Initialize the current process pointer (curproc) before 182 * any possible traps/probes to simplify trap processing. 183 */ 184 p = &proc0; 185 curproc = p; 186 /* 187 * Attempt to find console and initialize 188 * in case of early panic or other messages. 189 */ 190 consinit(); 191 printf(copyright); 192 193 vm_mem_init(); 194 kmeminit(); 195 disk_init(); /* must come before autoconfiguration */ 196 tty_init(); /* initialise tty list */ 197 #if NRND > 0 198 rnd_init(); 199 #endif 200 config_init(); /* init autoconfiguration data structures */ 201 cpu_startup(); 202 203 /* 204 * Initialize process and pgrp structures. 205 */ 206 procinit(); 207 208 /* 209 * Create process 0 (the swapper). 210 */ 211 LIST_INSERT_HEAD(&allproc, p, p_list); 212 p->p_pgrp = &pgrp0; 213 LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash); 214 LIST_INIT(&pgrp0.pg_members); 215 LIST_INSERT_HEAD(&pgrp0.pg_members, p, p_pglist); 216 217 pgrp0.pg_session = &session0; 218 session0.s_count = 1; 219 session0.s_leader = p; 220 221 p->p_flag = P_INMEM | P_SYSTEM; 222 p->p_stat = SRUN; 223 p->p_nice = NZERO; 224 p->p_emul = &emul_netbsd; 225 bcopy("swapper", p->p_comm, sizeof ("swapper")); 226 227 /* Create credentials. */ 228 cred0.p_refcnt = 1; 229 p->p_cred = &cred0; 230 p->p_ucred = crget(); 231 p->p_ucred->cr_ngroups = 1; /* group 0 */ 232 233 /* Create the file descriptor table. */ 234 p->p_fd = &filedesc0.fd_fd; 235 filedesc0.fd_fd.fd_refcnt = 1; 236 filedesc0.fd_fd.fd_cmask = cmask; 237 filedesc0.fd_fd.fd_ofiles = filedesc0.fd_dfiles; 238 filedesc0.fd_fd.fd_ofileflags = filedesc0.fd_dfileflags; 239 filedesc0.fd_fd.fd_nfiles = NDFILE; 240 241 /* Create the limits structures. */ 242 p->p_limit = &limit0; 243 for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++) 244 limit0.pl_rlimit[i].rlim_cur = 245 limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY; 246 limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur = NOFILE; 247 limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur = MAXUPRC; 248 i = ptoa(cnt.v_free_count); 249 limit0.pl_rlimit[RLIMIT_RSS].rlim_max = i; 250 limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = i; 251 limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = i / 3; 252 limit0.p_refcnt = 1; 253 254 /* Allocate a prototype map so we have something to fork. */ 255 p->p_vmspace = vmspace_alloc(round_page(VM_MIN_ADDRESS), 256 trunc_page(VM_MAX_ADDRESS), TRUE); 257 258 p->p_addr = proc0paddr; /* XXX */ 259 260 /* 261 * We continue to place resource usage info and signal 262 * actions in the user struct so they're pageable. 263 */ 264 p->p_stats = &p->p_addr->u_stats; 265 p->p_sigacts = &p->p_addr->u_sigacts; 266 267 /* 268 * Charge root for one process. 269 */ 270 (void)chgproccnt(0, 1); 271 272 rqinit(); 273 274 /* Configure virtual memory system, set vm rlimits. */ 275 vm_init_limits(p); 276 277 /* Initialize the file systems. */ 278 #if defined(NFSSERVER) || defined(NFS) 279 nfs_init(); /* initialize server/shared data */ 280 #endif 281 vfsinit(); 282 283 /* Start real time and statistics clocks. */ 284 initclocks(); 285 286 /* Initialize mbuf's. */ 287 mbinit(); 288 289 #ifdef REAL_CLISTS 290 /* Initialize clists. */ 291 clist_init(); 292 #endif 293 294 #ifdef SYSVSHM 295 /* Initialize System V style shared memory. */ 296 shminit(); 297 #endif 298 299 #ifdef SYSVSEM 300 /* Initialize System V style semaphores. */ 301 seminit(); 302 #endif 303 304 #ifdef SYSVMSG 305 /* Initialize System V style message queues. */ 306 msginit(); 307 #endif 308 309 /* Attach pseudo-devices. */ 310 for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++) 311 (*pdev->pdev_attach)(pdev->pdev_count); 312 313 /* 314 * Initialize protocols. Block reception of incoming packets 315 * until everything is ready. 316 */ 317 s = splimp(); 318 ifinit(); 319 domaininit(); 320 splx(s); 321 322 #ifdef GPROF 323 /* Initialize kernel profiling. */ 324 kmstartup(); 325 #endif 326 327 /* Kick off timeout driven events by calling first time. */ 328 roundrobin(NULL); 329 schedcpu(NULL); 330 331 /* Determine the root and dump devices. */ 332 cpu_rootconf(); 333 cpu_dumpconf(); 334 335 /* Mount the root file system. */ 336 do { 337 domountroothook(); 338 if ((error = vfs_mountroot())) { 339 printf("cannot mount root, error = %d\n", error); 340 boothowto |= RB_ASKNAME; 341 setroot(root_device, 342 (rootdev != NODEV) ? DISKPART(rootdev) : 0, NULL); 343 } 344 } while (error != 0); 345 mountroothook_destroy(); 346 347 mountlist.cqh_first->mnt_flag |= MNT_ROOTFS; 348 mountlist.cqh_first->mnt_op->vfs_refcount++; 349 350 /* Get the vnode for '/'. Set filedesc0.fd_fd.fd_cdir to reference it. */ 351 if (VFS_ROOT(mountlist.cqh_first, &rootvnode)) 352 panic("cannot find root vnode"); 353 filedesc0.fd_fd.fd_cdir = rootvnode; 354 VREF(filedesc0.fd_fd.fd_cdir); 355 VOP_UNLOCK(rootvnode); 356 filedesc0.fd_fd.fd_rdir = NULL; 357 swapinit(); 358 359 /* 360 * Now can look at time, having had a chance to verify the time 361 * from the file system. Reset p->p_rtime as it may have been 362 * munched in mi_switch() after the time got set. 363 */ 364 p->p_stats->p_start = runtime = mono_time = boottime = time; 365 p->p_rtime.tv_sec = p->p_rtime.tv_usec = 0; 366 367 /* Initialize signal state for process 0. */ 368 siginit(p); 369 370 /* Create process 1 (init(8)). */ 371 if (sys_fork(p, NULL, rval)) 372 panic("fork init"); 373 #ifdef cpu_set_init_frame /* XXX should go away */ 374 if (rval[1]) { 375 /* 376 * Now in process 1. 377 */ 378 initframep = framep; 379 start_init(curproc); 380 return; 381 } 382 #else 383 cpu_set_kpc(pfind(1), start_init); 384 #endif 385 386 /* Create process 2 (the pageout daemon). */ 387 if (sys_fork(p, NULL, rval)) 388 panic("fork pager"); 389 #ifdef cpu_set_init_frame /* XXX should go away */ 390 if (rval[1]) { 391 /* 392 * Now in process 2. 393 */ 394 start_pagedaemon(curproc); 395 } 396 #else 397 cpu_set_kpc(pfind(2), start_pagedaemon); 398 #endif 399 400 /* The scheduler is an infinite loop. */ 401 scheduler(); 402 /* NOTREACHED */ 403 } 404 405 static void 406 check_console(p) 407 struct proc *p; 408 { 409 struct nameidata nd; 410 int error; 411 412 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/console", p); 413 error = namei(&nd); 414 if (error == 0) 415 vrele(nd.ni_vp); 416 else if (error == ENOENT) 417 printf("warning: no /dev/console\n"); 418 else 419 printf("warning: lookup /dev/console: error %d\n", error); 420 } 421 422 /* 423 * List of paths to try when searching for "init". 424 */ 425 static char *initpaths[] = { 426 "/sbin/init", 427 "/sbin/oinit", 428 "/sbin/init.bak", 429 NULL, 430 }; 431 432 /* 433 * Start the initial user process; try exec'ing each pathname in "initpaths". 434 * The program is invoked with one argument containing the boot flags. 435 */ 436 static void 437 start_init(p) 438 struct proc *p; 439 { 440 vm_offset_t addr; 441 struct sys_execve_args /* { 442 syscallarg(const char *) path; 443 syscallarg(char * const *) argp; 444 syscallarg(char * const *) envp; 445 } */ args; 446 int options, i, error; 447 register_t retval[2]; 448 char flags[4], *flagsp; 449 char **pathp, *path, *ucp, **uap, *arg0, *arg1 = NULL; 450 451 /* 452 * Now in process 1. 453 */ 454 initproc = p; 455 456 #ifdef cpu_set_init_frame /* XXX should go away */ 457 /* 458 * We need to set the system call frame as if we were entered through 459 * a syscall() so that when we call sys_execve() below, it will be able 460 * to set the entry point (see setregs) when it tries to exec. The 461 * startup code in "locore.s" has allocated space for the frame and 462 * passed a pointer to that space as main's argument. 463 */ 464 cpu_set_init_frame(p, initframep); 465 #endif 466 467 /* 468 * This is not the right way to do this. We really should 469 * hand-craft a descriptor onto /dev/console to hand to init, 470 * but that's a _lot_ more work, and the benefit from this easy 471 * hack makes up for the "good is the enemy of the best" effect. 472 */ 473 check_console(p); 474 475 /* 476 * Need just enough stack to hold the faked-up "execve()" arguments. 477 */ 478 addr = USRSTACK - PAGE_SIZE; 479 if (vm_allocate(&p->p_vmspace->vm_map, &addr, (vm_size_t)PAGE_SIZE, 480 FALSE) != 0) 481 panic("init: couldn't allocate argument space"); 482 p->p_vmspace->vm_maxsaddr = (caddr_t)addr; 483 484 for (pathp = &initpaths[0]; (path = *pathp) != NULL; pathp++) { 485 ucp = (char *)(addr + PAGE_SIZE); 486 487 /* 488 * Construct the boot flag argument. 489 */ 490 flagsp = flags; 491 *flagsp++ = '-'; 492 options = 0; 493 494 if (boothowto & RB_SINGLE) { 495 *flagsp++ = 's'; 496 options = 1; 497 } 498 #ifdef notyet 499 if (boothowto & RB_FASTBOOT) { 500 *flagsp++ = 'f'; 501 options = 1; 502 } 503 #endif 504 505 /* 506 * Move out the flags (arg 1), if necessary. 507 */ 508 if (options != 0) { 509 *flagsp++ = '\0'; 510 i = flagsp - flags; 511 #ifdef DEBUG 512 printf("init: copying out flags `%s' %d\n", flags, i); 513 #endif 514 (void)copyout((caddr_t)flags, (caddr_t)(ucp -= i), i); 515 arg1 = ucp; 516 } 517 518 /* 519 * Move out the file name (also arg 0). 520 */ 521 i = strlen(path) + 1; 522 #ifdef DEBUG 523 printf("init: copying out path `%s' %d\n", path, i); 524 #endif 525 (void)copyout((caddr_t)path, (caddr_t)(ucp -= i), i); 526 arg0 = ucp; 527 528 /* 529 * Move out the arg pointers. 530 */ 531 uap = (char **)((long)ucp & ~ALIGNBYTES); 532 (void)suword((caddr_t)--uap, 0); /* terminator */ 533 if (options != 0) 534 (void)suword((caddr_t)--uap, (long)arg1); 535 (void)suword((caddr_t)--uap, (long)arg0); 536 537 /* 538 * Point at the arguments. 539 */ 540 SCARG(&args, path) = arg0; 541 SCARG(&args, argp) = uap; 542 SCARG(&args, envp) = NULL; 543 544 /* 545 * Now try to exec the program. If can't for any reason 546 * other than it doesn't exist, complain. 547 */ 548 error = sys_execve(p, &args, retval); 549 if (error == 0 || error == EJUSTRETURN) 550 return; 551 if (error != ENOENT) 552 printf("exec %s: error %d\n", path, error); 553 } 554 printf("init: not found\n"); 555 panic("no init"); 556 } 557 558 static void 559 start_pagedaemon(p) 560 struct proc *p; 561 { 562 563 /* 564 * Now in process 2. 565 */ 566 p->p_flag |= P_INMEM | P_SYSTEM; /* XXX */ 567 bcopy("pagedaemon", curproc->p_comm, sizeof ("pagedaemon")); 568 vm_pageout(); 569 /* NOTREACHED */ 570 } 571