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