1 /* 2 * Copyright (c) 1982, 1986, 1989, 1991, 1992, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)init_main.c 8.9 (Berkeley) 1/21/94 39 * $Id: init_main.c,v 1.16 1994/11/06 05:01:58 davidg Exp $ 40 */ 41 42 #include <sys/param.h> 43 #include <sys/filedesc.h> 44 #include <sys/errno.h> 45 #include <sys/exec.h> 46 #include <sys/kernel.h> 47 #include <sys/mount.h> 48 #include <sys/proc.h> 49 #include <sys/resourcevar.h> 50 #include <sys/signalvar.h> 51 #include <sys/systm.h> 52 #include <sys/vnode.h> 53 #include <sys/sysent.h> 54 #include <sys/conf.h> 55 #include <sys/buf.h> 56 #include <sys/clist.h> 57 #include <sys/device.h> 58 #include <sys/protosw.h> 59 #include <sys/reboot.h> 60 #include <sys/user.h> 61 62 #include <ufs/ufs/quota.h> 63 64 #include <machine/cpu.h> 65 66 #include <vm/vm.h> 67 68 #ifdef HPFPLIB 69 char copyright[] = 70 "Copyright (c) 1982, 1986, 1989, 1991, 1993\n\tThe Regents of the University of California.\nCopyright (c) 1992 Hewlett-Packard Company\nCopyright (c) 1992 Motorola Inc.\nAll rights reserved.\n\n"; 71 #else 72 char copyright[] = 73 "Copyright (c) 1982, 1986, 1989, 1991, 1993\n\tThe Regents of the University of California. All rights reserved.\n\n"; 74 #endif 75 76 /* Components of the first process -- never freed. */ 77 struct session session0; 78 struct pgrp pgrp0; 79 struct proc proc0; 80 struct pcred cred0; 81 struct filedesc0 filedesc0; 82 struct plimit limit0; 83 struct vmspace vmspace0; 84 struct proc *curproc = &proc0; 85 struct proc *initproc, *pageproc, *updateproc, *vmproc; 86 87 int cmask = CMASK; 88 extern struct user *proc0paddr; 89 90 struct vnode *rootvp, *swapdev_vp; 91 int boothowto; 92 struct timeval boottime; 93 struct timeval runtime; 94 95 static void start_init __P((struct proc *p, void *framep)); 96 97 #if __GNUC__ >= 2 98 void __main() {} 99 #endif 100 101 /* 102 * This table is filled in by the linker with functions that need to be 103 * called to initialize various pseudo-devices and whatnot. 104 */ 105 106 static void dummyinit() {} 107 TEXT_SET(pseudo_set, dummyinit); 108 109 typedef void (*pseudo_func_t)(void); 110 extern const struct linker_set pseudo_set; 111 static const pseudo_func_t *pseudos = 112 (const pseudo_func_t *)&pseudo_set.ls_items[0]; 113 114 /* 115 * System startup; initialize the world, create process 0, mount root 116 * filesystem, and fork to create init and pagedaemon. Most of the 117 * hard work is done in the lower-level initialization routines including 118 * startup(), which does memory initialization and autoconfiguration. 119 */ 120 void 121 main(framep) 122 void *framep; 123 { 124 register struct proc *p; 125 register struct filedesc0 *fdp; 126 register int i; 127 int s, rval[2]; 128 extern int (*mountroot) __P((void)); 129 extern void roundrobin __P((void *)); 130 extern void schedcpu __P((void *)); 131 extern struct sysentvec aout_sysvec; 132 133 /* 134 * Initialize the current process pointer (curproc) before 135 * any possible traps/probes to simplify trap processing. 136 */ 137 p = &proc0; 138 curproc = p; 139 printf(copyright); 140 141 vm_mem_init(); 142 kmeminit(); 143 cpu_startup(); 144 145 /* 146 * Create process 0 (the swapper). 147 */ 148 allproc = (volatile struct proc *)p; 149 p->p_prev = (struct proc **)&allproc; 150 p->p_pgrp = &pgrp0; 151 pgrphash[0] = &pgrp0; 152 pgrp0.pg_mem = p; 153 pgrp0.pg_session = &session0; 154 session0.s_count = 1; 155 session0.s_leader = p; 156 157 p->p_sysent = &aout_sysvec; 158 159 p->p_flag = P_INMEM | P_SYSTEM; 160 p->p_stat = SRUN; 161 p->p_nice = NZERO; 162 p->p_rtprio.type = RTP_PRIO_NORMAL; 163 p->p_rtprio.prio = 0; 164 165 bcopy("swapper", p->p_comm, sizeof ("swapper")); 166 167 /* Create credentials. */ 168 cred0.p_refcnt = 1; 169 p->p_cred = &cred0; 170 p->p_ucred = crget(); 171 p->p_ucred->cr_ngroups = 1; /* group 0 */ 172 173 /* Create the file descriptor table. */ 174 fdp = &filedesc0; 175 p->p_fd = &fdp->fd_fd; 176 fdp->fd_fd.fd_refcnt = 1; 177 fdp->fd_fd.fd_cmask = cmask; 178 fdp->fd_fd.fd_ofiles = fdp->fd_dfiles; 179 fdp->fd_fd.fd_ofileflags = fdp->fd_dfileflags; 180 fdp->fd_fd.fd_nfiles = NDFILE; 181 182 /* Create the limits structures. */ 183 p->p_limit = &limit0; 184 for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++) 185 limit0.pl_rlimit[i].rlim_cur = 186 limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY; 187 limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur = NOFILE; 188 limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur = MAXUPRC; 189 i = ptoa(cnt.v_free_count); 190 limit0.pl_rlimit[RLIMIT_RSS].rlim_max = i; 191 limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = i; 192 limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = i / 3; 193 limit0.p_refcnt = 1; 194 195 /* Allocate a prototype map so we have something to fork. */ 196 p->p_vmspace = &vmspace0; 197 vmspace0.vm_refcnt = 1; 198 pmap_pinit(&vmspace0.vm_pmap); 199 vm_map_init(&vmspace0.vm_map, round_page(VM_MIN_ADDRESS), 200 trunc_page(VM_MAX_ADDRESS), TRUE); 201 vmspace0.vm_map.pmap = &vmspace0.vm_pmap; 202 p->p_addr = proc0paddr; /* XXX */ 203 204 /* 205 * We continue to place resource usage info and signal 206 * actions in the user struct so they're pageable. 207 */ 208 p->p_stats = &p->p_addr->u_stats; 209 p->p_sigacts = &p->p_addr->u_sigacts; 210 211 /* 212 * Initialize per uid information structure and charge 213 * root for one process. 214 */ 215 usrinfoinit(); 216 (void)chgproccnt(0, 1); 217 218 rqinit(); 219 220 /* Configure virtual memory system, set vm rlimits. */ 221 vm_init_limits(p); 222 223 /* Initialize the file systems. */ 224 vfsinit(); 225 226 /* Start real time and statistics clocks. */ 227 initclocks(); 228 229 /* Initialize mbuf's. */ 230 mbinit(); 231 232 /* Initialize clists. */ 233 clist_init(); 234 235 #ifdef SYSVSHM 236 /* Initialize System V style shared memory. */ 237 shminit(); 238 #endif 239 240 #ifdef SYSVSEM 241 /* Initialize System V style semaphores. */ 242 seminit(); 243 #endif 244 245 #ifdef SYSVMSG 246 /* Initialize System V style message queues. */ 247 msginit(); 248 #endif 249 250 /* 251 * Attach pseudo-devices. 252 */ 253 while(*pseudos) { 254 (**pseudos++)(); 255 } 256 257 /* 258 * Initialize protocols. Block reception of incoming packets 259 * until everything is ready. 260 */ 261 s = splimp(); 262 ifinit(); 263 domaininit(); 264 splx(s); 265 266 #ifdef GPROF 267 /* Initialize kernel profiling. */ 268 kmstartup(); 269 #endif 270 271 /* Kick off timeout driven events by calling first time. */ 272 roundrobin(NULL); 273 schedcpu(NULL); 274 275 /* Mount the root file system. */ 276 if ((*mountroot)()) 277 panic("cannot mount root"); 278 279 /* Get the vnode for '/'. Set fdp->fd_fd.fd_cdir to reference it. */ 280 if (VFS_ROOT(mountlist.tqh_first, &rootvnode)) 281 panic("cannot find root vnode"); 282 fdp->fd_fd.fd_cdir = rootvnode; 283 VREF(fdp->fd_fd.fd_cdir); 284 VOP_UNLOCK(rootvnode); 285 fdp->fd_fd.fd_rdir = NULL; 286 swapinit(); 287 288 /* 289 * Now can look at time, having had a chance to verify the time 290 * from the file system. Reset p->p_rtime as it may have been 291 * munched in mi_switch() after the time got set. 292 */ 293 p->p_stats->p_start = runtime = mono_time = boottime = time; 294 p->p_rtime.tv_sec = p->p_rtime.tv_usec = 0; 295 296 /* Initialize signal state for process 0. */ 297 siginit(p); 298 299 /* Create process 1 (init(8)). */ 300 if (fork(p, NULL, rval)) 301 panic("fork init"); 302 if (rval[1]) { 303 start_init(curproc, framep); 304 return; 305 } 306 307 /* Create process 2 (the pageout daemon). */ 308 if (fork(p, NULL, rval)) 309 panic("fork pager"); 310 if (rval[1]) { 311 /* 312 * Now in process 2. 313 */ 314 p = curproc; 315 pageproc = p; 316 p->p_flag |= P_INMEM | P_SYSTEM; /* XXX */ 317 bcopy("pagedaemon", curproc->p_comm, sizeof ("pagedaemon")); 318 vm_pageout(); 319 /* NOTREACHED */ 320 } 321 322 /* 323 * Start high level vm daemon (process 3). 324 */ 325 if (fork(p, (void *) NULL, rval)) 326 panic("failed fork vm daemon"); 327 if (rval[1]) { 328 p = curproc; 329 vmproc = p; 330 p->p_flag |= P_INMEM | P_SYSTEM; 331 bcopy("vmdaemon", p->p_comm, sizeof("vmdaemon")); 332 vm_daemon(); 333 /*NOTREACHED*/ 334 } 335 336 /* 337 * Start update daemon (process 4). 338 */ 339 if (fork(p, (void *) NULL, rval)) 340 panic("failed fork update daemon"); 341 if (rval[1]) { 342 p = curproc; 343 updateproc = p; 344 p->p_flag |= P_INMEM | P_SYSTEM; 345 bcopy("update", p->p_comm, sizeof("update")); 346 vfs_update(); 347 /*NOTREACHED*/ 348 } 349 350 /* The scheduler is an infinite loop. */ 351 scheduler(); 352 /* NOTREACHED */ 353 } 354 355 /* 356 * List of paths to try when searching for "init". 357 */ 358 static char *initpaths[] = { 359 "/sbin/init", 360 "/sbin/oinit", 361 "/sbin/init.bak", 362 "/stand/sysinstall", 363 NULL, 364 }; 365 366 /* 367 * Start the initial user process; try exec'ing each pathname in "initpaths". 368 * The program is invoked with one argument containing the boot flags. 369 */ 370 static void 371 start_init(p, framep) 372 struct proc *p; 373 void *framep; 374 { 375 vm_offset_t addr; 376 struct execve_args args; 377 int options, i, retval[2], error; 378 char **pathp, *path, *ucp, **uap, *arg0, *arg1; 379 380 initproc = p; 381 382 /* 383 * We need to set the system call frame as if we were entered through 384 * a syscall() so that when we call execve() below, it will be able 385 * to set the entry point (see setregs) when it tries to exec. The 386 * startup code in "locore.s" has allocated space for the frame and 387 * passed a pointer to that space as main's argument. 388 */ 389 cpu_set_init_frame(p, framep); 390 391 /* 392 * Need just enough stack to hold the faked-up "execve()" arguments. 393 */ 394 addr = trunc_page(VM_MAXUSER_ADDRESS - PAGE_SIZE); 395 if (vm_allocate(&p->p_vmspace->vm_map, &addr, PAGE_SIZE, FALSE) != 0) 396 panic("init: couldn't allocate argument space"); 397 p->p_vmspace->vm_maxsaddr = (caddr_t)addr; 398 p->p_vmspace->vm_ssize = 1; 399 400 for (pathp = &initpaths[0]; (path = *pathp) != NULL; pathp++) { 401 /* 402 * Move out the boot flag argument. 403 */ 404 options = 0; 405 ucp = (char *)USRSTACK; 406 (void)subyte(--ucp, 0); /* trailing zero */ 407 if (boothowto & RB_SINGLE) { 408 (void)subyte(--ucp, 's'); 409 options = 1; 410 } 411 #ifdef notyet 412 if (boothowto & RB_FASTBOOT) { 413 (void)subyte(--ucp, 'f'); 414 options = 1; 415 } 416 #endif 417 if (options == 0) 418 (void)subyte(--ucp, '-'); 419 (void)subyte(--ucp, '-'); /* leading hyphen */ 420 arg1 = ucp; 421 422 /* 423 * Move out the file name (also arg 0). 424 */ 425 for (i = strlen(path) + 1; i >= 0; i--) 426 (void)subyte(--ucp, path[i]); 427 arg0 = ucp; 428 429 /* 430 * Move out the arg pointers. 431 */ 432 uap = (char **)((int)ucp & ~(NBPW-1)); 433 (void)suword((caddr_t)--uap, 0); /* terminator */ 434 (void)suword((caddr_t)--uap, (int)arg1); 435 (void)suword((caddr_t)--uap, (int)arg0); 436 437 /* 438 * Point at the arguments. 439 */ 440 args.fname = arg0; 441 args.argv = uap; 442 args.envv = 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