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