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