1 /* init_main.c 4.12 81/03/09 */ 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 clkstart(); 78 79 /* 80 * Initialize devices and 81 * set up 'known' i-nodes 82 */ 83 84 ihinit(); 85 bhinit(); 86 cinit(); 87 binit(); 88 bswinit(); 89 iinit(); 90 rootdir = iget(rootdev, (ino_t)ROOTINO); 91 rootdir->i_flag &= ~ILOCK; 92 u.u_cdir = iget(rootdev, (ino_t)ROOTINO); 93 u.u_cdir->i_flag &= ~ILOCK; 94 u.u_rdir = NULL; 95 u.u_dmap = zdmap; 96 u.u_smap = zdmap; 97 98 /* 99 * make page-out daemon (process 2) 100 * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page 101 * table so that it can map dirty pages into 102 * its address space during asychronous pushes. 103 */ 104 105 mpid = 1; 106 proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES)); 107 proc[1].p_stat = SZOMB; /* force it to be in proc slot 2 */ 108 if (newproc(0)) { 109 proc[2].p_flag |= SLOAD|SSYS; 110 proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX; 111 pageout(); 112 } 113 114 /* 115 * make init process and 116 * enter scheduling loop 117 */ 118 119 mpid = 0; 120 proc[1].p_stat = 0; 121 proc[0].p_szpt = CLSIZE; 122 if (newproc(0)) { 123 expand(clrnd((int)btoc(szicode)), P0BR); 124 (void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap); 125 (void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode); 126 /* 127 * Return goes to loc. 0 of user init 128 * code just copied out. 129 */ 130 return; 131 } 132 proc[0].p_szpt = 1; 133 sched(); 134 } 135 136 /* 137 * iinit is called once (from main) 138 * very early in initialization. 139 * It reads the root's super block 140 * and initializes the current date 141 * from the last modified date. 142 * 143 * panic: iinit -- cannot read the super 144 * block. Usually because of an IO error. 145 */ 146 iinit() 147 { 148 register struct buf *bp; 149 register struct filsys *fp; 150 register int i; 151 152 (*bdevsw[major(rootdev)].d_open)(rootdev, 1); 153 bp = bread(rootdev, SUPERB); 154 if(u.u_error) 155 panic("iinit"); 156 bp->b_flags |= B_LOCKED; /* block can never be re-used */ 157 brelse(bp); 158 mount[0].m_dev = rootdev; 159 mount[0].m_bufp = bp; 160 fp = bp->b_un.b_filsys; 161 fp->s_flock = 0; 162 fp->s_ilock = 0; 163 fp->s_ronly = 0; 164 fp->s_lasti = 1; 165 fp->s_nbehind = 0; 166 fp->s_fsmnt[0] = '/'; 167 for (i = 1; i < sizeof(fp->s_fsmnt); i++) 168 fp->s_fsmnt[i] = 0; 169 clkinit(fp->s_time); 170 bootime = time; 171 } 172 173 /* 174 * Initialize the buffer I/O system by freeing 175 * all buffers and setting all device buffer lists to empty. 176 */ 177 binit() 178 { 179 register struct buf *bp; 180 register struct buf *dp; 181 register int i; 182 struct bdevsw *bdp; 183 struct swdevt *swp; 184 185 for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) { 186 dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp; 187 dp->b_flags = B_HEAD; 188 } 189 dp--; /* dp = &bfreelist[BQUEUES-1]; */ 190 for (i=0; i<nbuf; i++) { 191 bp = &buf[i]; 192 bp->b_dev = NODEV; 193 bp->b_un.b_addr = buffers + i * BSIZE; 194 bp->b_back = dp; 195 bp->b_forw = dp->b_forw; 196 dp->b_forw->b_back = bp; 197 dp->b_forw = bp; 198 bp->b_flags = B_BUSY|B_INVAL; 199 brelse(bp); 200 } 201 for (bdp = bdevsw; bdp->d_open; bdp++) 202 nblkdev++; 203 /* 204 * Count swap devices, and adjust total swap space available. 205 * Some of this space will not be available until a vswapon() 206 * system is issued, usually when the system goes multi-user. 207 */ 208 nswdev = 0; 209 for (swp = swdevt; swp->sw_dev; swp++) 210 nswdev++; 211 if (nswdev == 0) 212 panic("binit"); 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