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