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