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