1 /* 2 * Copyright (c) 1982, 1986, 1989 Regents of the University of California. 3 * All rights reserved. The Berkeley software License Agreement 4 * specifies the terms and conditions for redistribution. 5 * 6 * @(#)init_main.c 7.32 (Berkeley) 01/10/91 7 */ 8 9 #include "param.h" 10 #include "systm.h" 11 #include "user.h" 12 #include "filedesc.h" 13 #include "kernel.h" 14 #include "mount.h" 15 #include "map.h" 16 #include "proc.h" 17 #include "vnode.h" 18 #include "seg.h" 19 #include "conf.h" 20 #include "buf.h" 21 #include "clist.h" 22 #include "malloc.h" 23 #include "protosw.h" 24 #include "reboot.h" 25 #include "../ufs/quota.h" 26 27 #include "machine/reg.h" 28 #include "machine/cpu.h" 29 30 #include "../vm/vm_param.h" 31 #include "../vm/vm_map.h" 32 33 int cmask = CMASK; 34 extern caddr_t proc0paddr; 35 extern int (*mountroot)(); 36 #if defined(i386) 37 extern char initflags[]; 38 #endif 39 40 /* 41 * Initialization code. 42 * Called from cold start routine as 43 * soon as a stack and segmentation 44 * have been established. 45 * Functions: 46 * clear and free user core 47 * turn on clock 48 * hand craft 0th process 49 * call all initialization routines 50 * fork - process 0 to schedule 51 * - process 1 execute bootstrap 52 * - process 2 to page out 53 */ 54 main(firstaddr) 55 int firstaddr; 56 { 57 register int i; 58 register struct proc *p; 59 register struct pgrp *pg; 60 register struct filedesc *fdp; 61 int s; 62 63 rqinit(); 64 #if defined(i386) 65 /* 66 * set boot flags 67 */ 68 if (boothowto&RB_SINGLE) 69 bcopy("-s", initflags, 3); 70 else 71 if (boothowto&RB_ASKNAME) 72 bcopy("-a", initflags, 3); 73 else 74 bcopy("-", initflags, 2); 75 #endif 76 #if defined(hp300) && defined(DEBUG) 77 /* 78 * Assumes mapping is really on 79 */ 80 find_devs(); 81 cninit(); 82 #endif 83 vm_mem_init(); 84 kmeminit(); 85 startup(firstaddr); 86 87 /* 88 * set up system process 0 (swapper) 89 */ 90 p = &proc[0]; 91 bcopy("swapper", p->p_comm, sizeof ("swapper")); 92 p->p_stat = SRUN; 93 p->p_flag |= SLOAD|SSYS; 94 p->p_nice = NZERO; 95 /* 96 * Allocate a prototype map so we have something to fork 97 */ 98 p->p_map = vm_map_create(pmap_create(0), 99 round_page(VM_MIN_ADDRESS), 100 trunc_page(VM_MAX_ADDRESS), TRUE); 101 p->p_addr = proc0paddr; 102 u.u_procp = p; 103 MALLOC(pgrphash[0], struct pgrp *, sizeof (struct pgrp), 104 M_PGRP, M_NOWAIT); 105 if ((pg = pgrphash[0]) == NULL) 106 panic("no space to craft zero'th process group"); 107 pg->pg_id = 0; 108 pg->pg_hforw = 0; 109 pg->pg_mem = p; 110 pg->pg_jobc = 0; 111 p->p_pgrp = pg; 112 p->p_pgrpnxt = 0; 113 MALLOC(pg->pg_session, struct session *, sizeof (struct session), 114 M_SESSION, M_NOWAIT); 115 if (pg->pg_session == NULL) 116 panic("no space to craft zero'th session"); 117 pg->pg_session->s_count = 1; 118 pg->pg_session->s_leader = NULL; 119 pg->pg_session->s_ttyvp = NULL; 120 pg->pg_session->s_ttyp = NULL; 121 #ifdef KTRACE 122 p->p_tracep = NULL; 123 p->p_traceflag = 0; 124 #endif 125 /* 126 * These assume that the u. area is always mapped 127 * to the same virtual address. Otherwise must be 128 * handled when copying the u. area in newproc(). 129 */ 130 ndinit(&u.u_nd); 131 132 /* 133 * Create the file descriptor table for process 0. 134 */ 135 fdp = (struct filedesc *)malloc(sizeof(*fdp), M_FILE, M_WAITOK); 136 bzero((char *)fdp, sizeof(struct filedesc)); 137 p->p_fd = fdp; 138 fdp->fd_refcnt = 1; 139 fdp->fd_cmask = cmask; 140 fdp->fd_lastfile = -1; 141 fdp->fd_maxfiles = NDFILE; 142 for (i = 0; i < sizeof(u.u_rlimit)/sizeof(u.u_rlimit[0]); i++) 143 u.u_rlimit[i].rlim_cur = u.u_rlimit[i].rlim_max = 144 RLIM_INFINITY; 145 /* 146 * configure virtual memory system, 147 * set vm rlimits 148 */ 149 vminit(); 150 151 /* 152 * Initialize the file systems. 153 * 154 * Get vnodes for swapdev, argdev, and rootdev. 155 */ 156 vfsinit(); 157 if (bdevvp(swapdev, &swapdev_vp) || 158 bdevvp(argdev, &argdev_vp) || 159 bdevvp(rootdev, &rootvp)) 160 panic("can't setup bdevvp's"); 161 162 /* 163 * Setup credentials 164 */ 165 u.u_cred = crget(); 166 u.u_cred->cr_ngroups = 1; 167 168 startrtclock(); 169 #if defined(vax) 170 #include "kg.h" 171 #if NKG > 0 172 startkgclock(); 173 #endif 174 #endif 175 176 /* 177 * Initialize tables, protocols, and set up well-known inodes. 178 */ 179 mbinit(); 180 cinit(); 181 #ifdef SYSVSHM 182 shminit(); 183 #endif 184 #include "sl.h" 185 #if NSL > 0 186 slattach(); /* XXX */ 187 #endif 188 #include "loop.h" 189 #if NLOOP > 0 190 loattach(); /* XXX */ 191 #endif 192 /* 193 * Block reception of incoming packets 194 * until protocols have been initialized. 195 */ 196 s = splimp(); 197 ifinit(); 198 domaininit(); 199 splx(s); 200 pqinit(); 201 swapinit(); 202 #ifdef GPROF 203 kmstartup(); 204 #endif 205 206 /* kick off timeout driven events by calling first time */ 207 roundrobin(); 208 schedcpu(); 209 enablertclock(); /* enable realtime clock interrupts */ 210 211 /* set up the root file system */ 212 if ((*mountroot)()) 213 panic("cannot mount root"); 214 /* 215 * Get vnode for '/'. 216 * Setup rootdir and fdp->fd_cdir to point to it. 217 */ 218 if (VFS_ROOT(rootfs, &rootdir)) 219 panic("cannot find root vnode"); 220 fdp->fd_cdir = rootdir; 221 VREF(fdp->fd_cdir); 222 VOP_UNLOCK(rootdir); 223 fdp->fd_rdir = NULL; 224 boottime = u.u_start = time; 225 226 /* 227 * make init process 228 */ 229 230 siginit(&proc[0]); 231 if (newproc(0)) { 232 vm_offset_t addr = 0; 233 234 (void) vm_allocate(u.u_procp->p_map, 235 &addr, round_page(szicode), FALSE); 236 if (addr != 0) 237 panic("init: couldn't allocate at zero"); 238 239 /* need just enough stack to exec from */ 240 addr = trunc_page(VM_MAX_ADDRESS - PAGE_SIZE); 241 (void) vm_allocate(u.u_procp->p_map, &addr, PAGE_SIZE, FALSE); 242 u.u_maxsaddr = (caddr_t)addr; 243 (void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode); 244 /* 245 * Return goes to loc. 0 of user init 246 * code just copied out. 247 */ 248 return; 249 } 250 /* 251 * Start up pageout daemon (process 2). 252 */ 253 if (newproc(0)) { 254 proc[2].p_flag |= SLOAD|SSYS; 255 bcopy("pagedaemon", proc[2].p_comm, sizeof ("pagedaemon")); 256 vm_pageout(); 257 /*NOTREACHED*/ 258 } 259 260 /* 261 * enter scheduling loop 262 */ 263 sched(); 264 } 265 266 /* 267 * Initialize hash links for buffers. 268 */ 269 bhinit() 270 { 271 register int i; 272 register struct bufhd *bp; 273 274 for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++) 275 bp->b_forw = bp->b_back = (struct buf *)bp; 276 } 277 278 /* 279 * Initialize the buffer I/O system by freeing 280 * all buffers and setting all device buffer lists to empty. 281 */ 282 binit() 283 { 284 register struct buf *bp, *dp; 285 register int i; 286 int base, residual; 287 288 for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) { 289 dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp; 290 dp->b_flags = B_HEAD; 291 } 292 base = bufpages / nbuf; 293 residual = bufpages % nbuf; 294 for (i = 0; i < nbuf; i++) { 295 bp = &buf[i]; 296 bp->b_dev = NODEV; 297 bp->b_bcount = 0; 298 bp->b_rcred = NOCRED; 299 bp->b_wcred = NOCRED; 300 bp->b_dirtyoff = 0; 301 bp->b_dirtyend = 0; 302 bp->b_un.b_addr = buffers + i * MAXBSIZE; 303 if (i < residual) 304 bp->b_bufsize = (base + 1) * CLBYTES; 305 else 306 bp->b_bufsize = base * CLBYTES; 307 binshash(bp, &bfreelist[BQ_AGE]); 308 bp->b_flags = B_BUSY|B_INVAL; 309 brelse(bp); 310 } 311 } 312 313 /* 314 * Set up swap devices. 315 * Initialize linked list of free swap 316 * headers. These do not actually point 317 * to buffers, but rather to pages that 318 * are being swapped in and out. 319 */ 320 swapinit() 321 { 322 register int i; 323 register struct buf *sp = swbuf; 324 struct swdevt *swp; 325 int error; 326 327 /* 328 * Count swap devices, and adjust total swap space available. 329 * Some of this space will not be available until a swapon() 330 * system is issued, usually when the system goes multi-user. 331 */ 332 nswdev = 0; 333 nswap = 0; 334 for (swp = swdevt; swp->sw_dev; swp++) { 335 nswdev++; 336 if (swp->sw_nblks > nswap) 337 nswap = swp->sw_nblks; 338 } 339 if (nswdev == 0) 340 panic("swapinit"); 341 if (nswdev > 1) 342 nswap = ((nswap + dmmax - 1) / dmmax) * dmmax; 343 nswap *= nswdev; 344 if (bdevvp(swdevt[0].sw_dev, &swdevt[0].sw_vp)) 345 panic("swapvp"); 346 if (error = swfree(0)) { 347 printf("swfree errno %d\n", error); /* XXX */ 348 panic("swapinit swfree 0"); 349 } 350 351 /* 352 * Now set up swap buffer headers. 353 */ 354 bswlist.av_forw = sp; 355 for (i=0; i<nswbuf-1; i++, sp++) 356 sp->av_forw = sp+1; 357 sp->av_forw = NULL; 358 } 359 360 /* 361 * Initialize clist by freeing all character blocks, then count 362 * number of character devices. (Once-only routine) 363 */ 364 cinit() 365 { 366 register int ccp; 367 register struct cblock *cp; 368 369 ccp = (int)cfree; 370 ccp = (ccp+CROUND) & ~CROUND; 371 for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) { 372 cp->c_next = cfreelist; 373 cfreelist = cp; 374 cfreecount += CBSIZE; 375 } 376 } 377