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