1 /* init_main.c 4.31 82/06/14 */ 2 3 #include "../h/param.h" 4 #include "../h/systm.h" 5 #include "../h/dir.h" 6 #include "../h/user.h" 7 #include "../h/fs.h" 8 #include "../h/mount.h" 9 #include "../h/map.h" 10 #include "../h/proc.h" 11 #include "../h/inode.h" 12 #include "../h/seg.h" 13 #include "../h/conf.h" 14 #include "../h/buf.h" 15 #include "../h/mtpr.h" 16 #include "../h/pte.h" 17 #include "../h/clock.h" 18 #include "../h/vm.h" 19 #include "../h/cmap.h" 20 #include "../h/text.h" 21 #include "../h/vlimit.h" 22 #include "../h/clist.h" 23 #ifdef INET 24 #include "../h/protosw.h" 25 #endif 26 27 /* 28 * Initialization code. 29 * Called from cold start routine as 30 * soon as a stack and segmentation 31 * have been established. 32 * Functions: 33 * clear and free user core 34 * turn on clock 35 * hand craft 0th process 36 * call all initialization routines 37 * fork - process 0 to schedule 38 * - process 2 to page out 39 * - process 1 execute bootstrap 40 * 41 * loop at loc 13 (0xd) in user mode -- /etc/init 42 * cannot be executed. 43 */ 44 main(firstaddr) 45 { 46 register int i; 47 register struct proc *p; 48 struct fs *fs; 49 50 rqinit(); 51 #include "loop.h" 52 startup(firstaddr); 53 54 /* 55 * set up system process 0 (swapper) 56 */ 57 p = &proc[0]; 58 p->p_p0br = (struct pte *)mfpr(P0BR); 59 p->p_szpt = 1; 60 p->p_addr = uaddr(p); 61 p->p_stat = SRUN; 62 p->p_flag |= SLOAD|SSYS; 63 p->p_nice = NZERO; 64 setredzone(p->p_addr, (caddr_t)&u); 65 u.u_procp = p; 66 u.u_cmask = CMASK; 67 for (i = 1; i < sizeof(u.u_limit)/sizeof(u.u_limit[0]); i++) 68 switch (i) { 69 70 case LIM_STACK: 71 u.u_limit[i] = 512*1024; 72 continue; 73 case LIM_DATA: 74 u.u_limit[i] = ctob(MAXDSIZ); 75 continue; 76 default: 77 u.u_limit[i] = INFINITY; 78 continue; 79 } 80 p->p_maxrss = INFINITY/NBPG; 81 clkstart(); 82 83 /* 84 * Initialize tables, protocols, and set up well-known inodes. 85 */ 86 mbinit(); 87 cinit(); /* needed by dmc-11 driver */ 88 #ifdef INET 89 #if NLOOP > 0 90 loattach(); /* XXX */ 91 #endif 92 ifinit(); 93 pfinit(); /* must follow interfaces */ 94 #endif 95 ihinit(); 96 bhinit(); 97 binit(); 98 bswinit(); 99 100 fs = mountfs(rootdev, 0, 0); 101 if (fs == 0) 102 panic("iinit"); 103 bcopy("/", fs->fs_fsmnt, 2); 104 clkinit(fs->fs_time); 105 bootime = time; 106 107 rootdir = iget(rootdev, fs, (ino_t)ROOTINO); 108 iunlock(rootdir); 109 u.u_cdir = iget(rootdev, fs, (ino_t)ROOTINO); 110 iunlock(u.u_cdir); 111 112 u.u_rdir = NULL; 113 u.u_dmap = zdmap; 114 u.u_smap = zdmap; 115 116 /* 117 * Set the scan rate and other parameters of the paging subsystem. 118 */ 119 setupclock(); 120 121 /* 122 * make page-out daemon (process 2) 123 * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page 124 * table so that it can map dirty pages into 125 * its address space during asychronous pushes. 126 */ 127 mpid = 1; 128 proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES)); 129 proc[1].p_stat = SZOMB; /* force it to be in proc slot 2 */ 130 if (newproc(0)) { 131 proc[2].p_flag |= SLOAD|SSYS; 132 proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX; 133 pageout(); 134 } 135 136 /* 137 * make init process and 138 * enter scheduling loop 139 */ 140 141 mpid = 0; 142 proc[1].p_stat = 0; 143 proc[0].p_szpt = CLSIZE; 144 if (newproc(0)) { 145 expand(clrnd((int)btoc(szicode)), P0BR); 146 (void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap); 147 (void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode); 148 /* 149 * Return goes to loc. 0 of user init 150 * code just copied out. 151 */ 152 return; 153 } 154 proc[0].p_szpt = 1; 155 sched(); 156 } 157 158 /* 159 * Initialize hash links for buffers. 160 */ 161 bhinit() 162 { 163 register int i; 164 register struct bufhd *bp; 165 166 for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++) 167 bp->b_forw = bp->b_back = (struct buf *)bp; 168 } 169 170 /* 171 * Initialize the buffer I/O system by freeing 172 * all buffers and setting all device buffer lists to empty. 173 */ 174 binit() 175 { 176 register struct buf *bp; 177 register struct buf *dp; 178 register int i; 179 struct bdevsw *bdp; 180 struct swdevt *swp; 181 182 for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) { 183 dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp; 184 dp->b_flags = B_HEAD; 185 } 186 dp--; /* dp = &bfreelist[BQUEUES-1]; */ 187 for (i = 0; i < nbuf; i++) { 188 bp = &buf[i]; 189 bp->b_dev = NODEV; 190 bp->b_un.b_addr = buffers + i * MAXBSIZE; 191 bp->b_bcount = MAXBSIZE; 192 bp->b_back = dp; 193 bp->b_forw = dp->b_forw; 194 dp->b_forw->b_back = bp; 195 dp->b_forw = bp; 196 bp->b_flags = B_BUSY|B_INVAL; 197 brelse(bp); 198 } 199 for (bdp = bdevsw; bdp->d_open; bdp++) 200 nblkdev++; 201 /* 202 * Count swap devices, and adjust total swap space available. 203 * Some of this space will not be available until a vswapon() 204 * system is issued, usually when the system goes multi-user. 205 */ 206 nswdev = 0; 207 for (swp = swdevt; swp->sw_dev; swp++) 208 nswdev++; 209 if (nswdev == 0) 210 panic("binit"); 211 if (nswdev > 1) 212 nswap = (nswap/DMMAX)*DMMAX; 213 nswap *= nswdev; 214 maxpgio *= nswdev; 215 swfree(0); 216 } 217 218 /* 219 * Initialize linked list of free swap 220 * headers. These do not actually point 221 * to buffers, but rather to pages that 222 * are being swapped in and out. 223 */ 224 bswinit() 225 { 226 register int i; 227 register struct buf *sp = swbuf; 228 229 bswlist.av_forw = sp; 230 for (i=0; i<nswbuf-1; i++, sp++) 231 sp->av_forw = sp+1; 232 sp->av_forw = NULL; 233 } 234 235 /* 236 * Initialize clist by freeing all character blocks, then count 237 * number of character devices. (Once-only routine) 238 */ 239 cinit() 240 { 241 register int ccp; 242 register struct cblock *cp; 243 register struct cdevsw *cdp; 244 245 ccp = (int)cfree; 246 ccp = (ccp+CROUND) & ~CROUND; 247 for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) { 248 cp->c_next = cfreelist; 249 cfreelist = cp; 250 cfreecount += CBSIZE; 251 } 252 ccp = 0; 253 for(cdp = cdevsw; cdp->d_open; cdp++) 254 ccp++; 255 nchrdev = ccp; 256 } 257