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