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.6 (Berkeley) 12/10/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 /* Create process 0 (the swapper). */ 102 p = &proc0; 103 curproc = p; 104 105 allproc = (volatile struct proc *)p; 106 p->p_prev = (struct proc **)&allproc; 107 p->p_pgrp = &pgrp0; 108 pgrphash[0] = &pgrp0; 109 pgrp0.pg_mem = p; 110 pgrp0.pg_session = &session0; 111 session0.s_count = 1; 112 session0.s_leader = p; 113 114 p->p_flag = P_INMEM | P_SYSTEM; 115 p->p_stat = SRUN; 116 p->p_nice = NZERO; 117 bcopy("swapper", p->p_comm, sizeof ("swapper")); 118 119 /* Create credentials. */ 120 cred0.p_refcnt = 1; 121 p->p_cred = &cred0; 122 p->p_ucred = crget(); 123 p->p_ucred->cr_ngroups = 1; /* group 0 */ 124 125 /* Create the file descriptor table. */ 126 fdp = &filedesc0; 127 p->p_fd = &fdp->fd_fd; 128 fdp->fd_fd.fd_refcnt = 1; 129 fdp->fd_fd.fd_cmask = cmask; 130 fdp->fd_fd.fd_ofiles = fdp->fd_dfiles; 131 fdp->fd_fd.fd_ofileflags = fdp->fd_dfileflags; 132 fdp->fd_fd.fd_nfiles = NDFILE; 133 134 /* Create the limits structures. */ 135 p->p_limit = &limit0; 136 for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++) 137 limit0.pl_rlimit[i].rlim_cur = 138 limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY; 139 limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur = NOFILE; 140 limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur = MAXUPRC; 141 i = ptoa(cnt.v_free_count); 142 limit0.pl_rlimit[RLIMIT_RSS].rlim_max = i; 143 limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = i; 144 limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = i / 3; 145 limit0.p_refcnt = 1; 146 147 /* Allocate a prototype map so we have something to fork. */ 148 p->p_vmspace = &vmspace0; 149 vmspace0.vm_refcnt = 1; 150 pmap_pinit(&vmspace0.vm_pmap); 151 vm_map_init(&p->p_vmspace->vm_map, round_page(VM_MIN_ADDRESS), 152 trunc_page(VM_MAX_ADDRESS), TRUE); 153 vmspace0.vm_map.pmap = &vmspace0.vm_pmap; 154 p->p_addr = proc0paddr; /* XXX */ 155 156 /* 157 * We continue to place resource usage info and signal 158 * actions in the user struct so they're pageable. 159 */ 160 p->p_stats = &p->p_addr->u_stats; 161 p->p_sigacts = &p->p_addr->u_sigacts; 162 163 /* 164 * Initialize per uid information structure and charge 165 * root for one process. 166 */ 167 usrinfoinit(); 168 (void)chgproccnt(0, 1); 169 170 rqinit(); 171 172 /* Configure virtual memory system, set vm rlimits. */ 173 vm_init_limits(p); 174 175 /* Initialize the file systems. */ 176 vfsinit(); 177 178 /* Start real time and statistics clocks. */ 179 initclocks(); 180 181 /* Initialize mbuf's. */ 182 mbinit(); 183 184 /* Initialize clists. */ 185 clist_init(); 186 187 #ifdef SYSVSHM 188 /* Initialize System V style shared memory. */ 189 shminit(); 190 #endif 191 192 /* Attach pseudo-devices. */ 193 for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++) 194 (*pdev->pdev_attach)(pdev->pdev_count); 195 196 /* 197 * Initialize protocols. Block reception of incoming packets 198 * until everything is ready. 199 */ 200 s = splimp(); 201 ifinit(); 202 domaininit(); 203 splx(s); 204 205 #ifdef GPROF 206 /* Initialize kernel profiling. */ 207 kmstartup(); 208 #endif 209 210 /* Kick off timeout driven events by calling first time. */ 211 roundrobin(NULL); 212 schedcpu(NULL); 213 214 /* Mount the root file system. */ 215 if ((*mountroot)()) 216 panic("cannot mount root"); 217 218 /* Get the vnode for '/'. Set fdp->fd_fd.fd_cdir to reference it. */ 219 if (VFS_ROOT(rootfs, &rootvnode)) 220 panic("cannot find root vnode"); 221 fdp->fd_fd.fd_cdir = rootvnode; 222 VREF(fdp->fd_fd.fd_cdir); 223 VOP_UNLOCK(rootvnode); 224 fdp->fd_fd.fd_rdir = NULL; 225 swapinit(); 226 227 /* 228 * Now can look at time, having had a chance to verify the time 229 * from the file system. Reset p->p_rtime as it may have been 230 * munched in mi_switch() after the time got set. 231 */ 232 p->p_stats->p_start = runtime = mono_time = boottime = time; 233 p->p_rtime.tv_sec = p->p_rtime.tv_usec = 0; 234 235 /* Initialize signal state for process 0. */ 236 siginit(p); 237 238 /* Create process 1 (init(8)). */ 239 if (fork(p, NULL, rval)) 240 panic("fork init"); 241 if (rval[1]) { 242 start_init(curproc, framep); 243 return; 244 } 245 246 /* Create process 2 (the pageout daemon). */ 247 if (fork(p, NULL, rval)) 248 panic("fork pager"); 249 if (rval[1]) { 250 /* 251 * Now in process 2. 252 */ 253 p = curproc; 254 pageproc = p; 255 p->p_flag |= P_INMEM | P_SYSTEM; /* XXX */ 256 bcopy("pagedaemon", curproc->p_comm, sizeof ("pagedaemon")); 257 vm_pageout(); 258 /* NOTREACHED */ 259 } 260 261 /* The scheduler is an infinite loop. */ 262 scheduler(); 263 /* NOTREACHED */ 264 } 265 266 /* 267 * List of paths to try when searching for "init". 268 */ 269 static char *initpaths[] = { 270 "/sbin/init", 271 "/sbin/oinit", 272 "/sbin/init.bak", 273 NULL, 274 }; 275 276 /* 277 * Start the initial user process; try exec'ing each pathname in "initpaths". 278 * The program is invoked with one argument containing the boot flags. 279 */ 280 static void 281 start_init(p, framep) 282 struct proc *p; 283 void *framep; 284 { 285 vm_offset_t addr; 286 struct execve_args args; 287 int options, i, retval[2], error; 288 char **pathp, *path, *ucp, **uap, *arg0, *arg1; 289 290 initproc = p; 291 292 /* 293 * We need to set the system call frame as if we were entered through 294 * a syscall() so that when we call execve() below, it will be able 295 * to set the entry point (see setregs) when it tries to exec. The 296 * startup code in "locore.s" has allocated space for the frame and 297 * passed a pointer to that space as main's argument. 298 */ 299 cpu_set_init_frame(p, framep); 300 301 /* 302 * Need just enough stack to hold the faked-up "execve()" arguments. 303 */ 304 addr = trunc_page(VM_MAX_ADDRESS - PAGE_SIZE); 305 if (vm_allocate(&p->p_vmspace->vm_map, &addr, PAGE_SIZE, FALSE) != 0) 306 panic("init: couldn't allocate argument space"); 307 p->p_vmspace->vm_maxsaddr = (caddr_t)addr; 308 309 for (pathp = &initpaths[0]; (path = *pathp) != NULL; pathp++) { 310 /* 311 * Move out the boot flag argument. 312 */ 313 options = 0; 314 ucp = (char *)USRSTACK; 315 (void)subyte(--ucp, 0); /* trailing zero */ 316 if (boothowto & RB_SINGLE) { 317 (void)subyte(--ucp, 's'); 318 options = 1; 319 } 320 #ifdef notyet 321 if (boothowto & RB_FASTBOOT) { 322 (void)subyte(--ucp, 'f'); 323 options = 1; 324 } 325 #endif 326 if (options == 0) 327 (void)subyte(--ucp, '-'); 328 (void)subyte(--ucp, '-'); /* leading hyphen */ 329 arg1 = ucp; 330 331 /* 332 * Move out the file name (also arg 0). 333 */ 334 for (i = strlen(path) + 1; i >= 0; i--) 335 (void)subyte(--ucp, path[i]); 336 arg0 = ucp; 337 338 /* 339 * Move out the arg pointers. 340 */ 341 uap = (char **)((int)ucp & ~(NBPW-1)); 342 (void)suword((caddr_t)--uap, 0); /* terminator */ 343 (void)suword((caddr_t)--uap, (int)arg1); 344 (void)suword((caddr_t)--uap, (int)arg0); 345 346 /* 347 * Point at the arguments. 348 */ 349 args.fname = arg0; 350 args.argp = uap; 351 args.envp = NULL; 352 353 /* 354 * Now try to exec the program. If can't for any reason 355 * other than it doesn't exist, complain. 356 */ 357 if ((error = execve(p, &args, &retval)) == 0) 358 return; 359 if (error != ENOENT) 360 printf("exec %s: error %d\n", path, error); 361 } 362 printf("init: not found\n"); 363 panic("no init"); 364 } 365