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.20 (Berkeley) 11/02/89 7 */ 8 9 #include "param.h" 10 #include "systm.h" 11 #include "user.h" 12 #include "kernel.h" 13 #include "mount.h" 14 #include "map.h" 15 #include "proc.h" 16 #include "vnode.h" 17 #include "seg.h" 18 #include "conf.h" 19 #include "buf.h" 20 #include "vm.h" 21 #include "cmap.h" 22 #include "text.h" 23 #include "clist.h" 24 #include "malloc.h" 25 #include "protosw.h" 26 #include "reboot.h" 27 #include "../ufs/quota.h" 28 29 #include "machine/pte.h" 30 #include "machine/reg.h" 31 #include "machine/cpu.h" 32 33 int cmask = CMASK; 34 extern int (*mountroot)(); 35 /* 36 * Initialization code. 37 * Called from cold start routine as 38 * soon as a stack and segmentation 39 * have been established. 40 * Functions: 41 * clear and free user core 42 * turn on clock 43 * hand craft 0th process 44 * call all initialization routines 45 * fork - process 0 to schedule 46 * - process 1 execute bootstrap 47 * - process 2 to page out 48 */ 49 main(firstaddr) 50 int firstaddr; 51 { 52 register int i; 53 register struct proc *p; 54 register struct pgrp *pg; 55 int s; 56 57 rqinit(); 58 #include "loop.h" 59 startup(firstaddr); 60 61 /* 62 * set up system process 0 (swapper) 63 */ 64 p = &proc[0]; 65 p->p_p0br = u.u_pcb.pcb_p0br; 66 p->p_szpt = 1; 67 p->p_addr = uaddr(p); 68 p->p_stat = SRUN; 69 p->p_flag |= SLOAD|SSYS; 70 p->p_nice = NZERO; 71 setredzone(p->p_addr, (caddr_t)&u); 72 u.u_procp = p; 73 MALLOC(pgrphash[0], struct pgrp *, sizeof (struct pgrp), 74 M_PGRP, M_NOWAIT); 75 if ((pg = pgrphash[0]) == NULL) 76 panic("no space to craft zero'th process group"); 77 pg->pg_id = 0; 78 pg->pg_hforw = 0; 79 pg->pg_mem = p; 80 pg->pg_jobc = 0; 81 p->p_pgrp = pg; 82 p->p_pgrpnxt = 0; 83 MALLOC(pg->pg_session, struct session *, sizeof (struct session), 84 M_SESSION, M_NOWAIT); 85 if (pg->pg_session == NULL) 86 panic("no space to craft zero'th session"); 87 pg->pg_session->s_count = 1; 88 pg->pg_session->s_leader = 0; 89 #ifdef KTRACE 90 p->p_tracep = NULL; 91 p->p_traceflag = 0; 92 #endif 93 /* 94 * These assume that the u. area is always mapped 95 * to the same virtual address. Otherwise must be 96 * handled when copying the u. area in newproc(). 97 */ 98 ndinit(&u.u_nd); 99 u.u_ap = u.u_arg; 100 101 u.u_cmask = cmask; 102 u.u_lastfile = -1; 103 for (i = 0; i < sizeof(u.u_rlimit)/sizeof(u.u_rlimit[0]); i++) 104 u.u_rlimit[i].rlim_cur = u.u_rlimit[i].rlim_max = 105 RLIM_INFINITY; 106 /* 107 * configure virtual memory system, 108 * set vm rlimits 109 */ 110 vminit(); 111 112 /* 113 * Initialize the file systems. 114 * 115 * Get vnodes for swapdev, argdev, and rootdev. 116 */ 117 vfsinit(); 118 if (bdevvp(swapdev, &swapdev_vp) || 119 bdevvp(argdev, &argdev_vp) || 120 bdevvp(rootdev, &rootvp)) 121 panic("can't setup bdevvp's"); 122 123 /* 124 * Setup credentials 125 */ 126 u.u_cred = crget(); 127 u.u_ngroups = 1; 128 129 #if defined(QUOTA) 130 qtinit(); 131 p->p_quota = u.u_quota = getquota(0, 0, Q_NDQ); 132 #endif 133 startrtclock(); 134 #if defined(vax) 135 #include "kg.h" 136 #if NKG > 0 137 startkgclock(); 138 #endif 139 #endif 140 141 /* 142 * Initialize tables, protocols, and set up well-known inodes. 143 */ 144 mbinit(); 145 cinit(); 146 #include "sl.h" 147 #if NSL > 0 148 slattach(); /* XXX */ 149 #endif 150 #if NLOOP > 0 151 loattach(); /* XXX */ 152 #endif 153 /* 154 * Block reception of incoming packets 155 * until protocols have been initialized. 156 */ 157 s = splimp(); 158 ifinit(); 159 domaininit(); 160 splx(s); 161 pqinit(); 162 xinit(); 163 swapinit(); 164 #ifdef GPROF 165 kmstartup(); 166 #endif 167 168 /* kick off timeout driven events by calling first time */ 169 roundrobin(); 170 schedcpu(); 171 schedpaging(); 172 173 /* set up the root file system */ 174 if ((*mountroot)()) 175 panic("cannot mount root"); 176 /* 177 * Get vnode for '/'. 178 * Setup rootdir and u.u_cdir to point to it. 179 */ 180 if (VFS_ROOT(rootfs, &rootdir)) 181 panic("cannot find root vnode"); 182 u.u_cdir = rootdir; 183 VREF(u.u_cdir); 184 VOP_UNLOCK(rootdir); 185 u.u_rdir = NULL; 186 boottime = time; 187 188 u.u_dmap = zdmap; 189 u.u_smap = zdmap; 190 191 enablertclock(); /* enable realtime clock interrupts */ 192 /* 193 * make init process 194 */ 195 196 siginit(&proc[0]); 197 proc[0].p_szpt = CLSIZE; 198 if (newproc(0)) { 199 expand(clrnd((int)btoc(szicode)), 0); 200 (void) swpexpand(u.u_dsize, (size_t)0, &u.u_dmap, &u.u_smap); 201 (void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode); 202 /* 203 * Return goes to loc. 0 of user init 204 * code just copied out. 205 */ 206 return; 207 } 208 /* 209 * make page-out daemon (process 2) 210 * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page 211 * table so that it can map dirty pages into 212 * its address space during asychronous pushes. 213 */ 214 proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES)); 215 if (newproc(0)) { 216 proc[2].p_flag |= SLOAD|SSYS; 217 proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX; 218 pageout(); 219 /*NOTREACHED*/ 220 } 221 222 /* 223 * enter scheduling loop 224 */ 225 proc[0].p_szpt = 1; 226 sched(); 227 } 228 229 /* 230 * Initialize hash links for buffers. 231 */ 232 bhinit() 233 { 234 register int i; 235 register struct bufhd *bp; 236 237 for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++) 238 bp->b_forw = bp->b_back = (struct buf *)bp; 239 } 240 241 /* 242 * Initialize the buffer I/O system by freeing 243 * all buffers and setting all device buffer lists to empty. 244 */ 245 binit() 246 { 247 register struct buf *bp, *dp; 248 register int i; 249 int base, residual; 250 251 for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) { 252 dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp; 253 dp->b_flags = B_HEAD; 254 } 255 base = bufpages / nbuf; 256 residual = bufpages % nbuf; 257 for (i = 0; i < nbuf; i++) { 258 bp = &buf[i]; 259 bp->b_dev = NODEV; 260 bp->b_bcount = 0; 261 bp->b_rcred = NOCRED; 262 bp->b_wcred = NOCRED; 263 bp->b_dirtyoff = 0; 264 bp->b_dirtyend = 0; 265 bp->b_un.b_addr = buffers + i * MAXBSIZE; 266 if (i < residual) 267 bp->b_bufsize = (base + 1) * CLBYTES; 268 else 269 bp->b_bufsize = base * CLBYTES; 270 binshash(bp, &bfreelist[BQ_AGE]); 271 bp->b_flags = B_BUSY|B_INVAL; 272 brelse(bp); 273 } 274 } 275 276 /* 277 * Set up swap devices. 278 * Initialize linked list of free swap 279 * headers. These do not actually point 280 * to buffers, but rather to pages that 281 * are being swapped in and out. 282 */ 283 swapinit() 284 { 285 register int i; 286 register struct buf *sp = swbuf; 287 struct swdevt *swp; 288 int error; 289 290 /* 291 * Count swap devices, and adjust total swap space available. 292 * Some of this space will not be available until a swapon() 293 * system is issued, usually when the system goes multi-user. 294 */ 295 nswdev = 0; 296 nswap = 0; 297 for (swp = swdevt; swp->sw_dev; swp++) { 298 nswdev++; 299 if (swp->sw_nblks > nswap) 300 nswap = swp->sw_nblks; 301 } 302 if (nswdev == 0) 303 panic("swapinit"); 304 if (nswdev > 1) 305 nswap = ((nswap + dmmax - 1) / dmmax) * dmmax; 306 nswap *= nswdev; 307 /* 308 * If there are multiple swap areas, 309 * allow more paging operations per second. 310 */ 311 if (nswdev > 1) 312 maxpgio = (maxpgio * (2 * nswdev - 1)) / 2; 313 if (bdevvp(swdevt[0].sw_dev, &swdevt[0].sw_vp)) 314 panic("swapvp"); 315 if (error = swfree(0)) { 316 printf("swfree errno %d\n", error); /* XXX */ 317 panic("swapinit swfree 0"); 318 } 319 320 /* 321 * Now set up swap buffer headers. 322 */ 323 bswlist.av_forw = sp; 324 for (i=0; i<nswbuf-1; i++, sp++) 325 sp->av_forw = sp+1; 326 sp->av_forw = NULL; 327 } 328 329 /* 330 * Initialize clist by freeing all character blocks, then count 331 * number of character devices. (Once-only routine) 332 */ 333 cinit() 334 { 335 register int ccp; 336 register struct cblock *cp; 337 338 ccp = (int)cfree; 339 ccp = (ccp+CROUND) & ~CROUND; 340 for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) { 341 cp->c_next = cfreelist; 342 cfreelist = cp; 343 cfreecount += CBSIZE; 344 } 345 } 346