1 /* init_main.c 4.46 83/01/23 */ 2 3 #include "../machine/pte.h" 4 5 #include "../h/param.h" 6 #include "../h/systm.h" 7 #include "../h/dir.h" 8 #include "../h/user.h" 9 #include "../h/kernel.h" 10 #include "../h/fs.h" 11 #include "../h/mount.h" 12 #include "../h/map.h" 13 #include "../h/proc.h" 14 #include "../h/inode.h" 15 #include "../h/seg.h" 16 #include "../h/conf.h" 17 #include "../h/buf.h" 18 #include "../h/vm.h" 19 #include "../h/cmap.h" 20 #include "../h/text.h" 21 #include "../h/clist.h" 22 #ifdef INET 23 #include "../h/protosw.h" 24 #endif 25 #include "../h/quota.h" 26 #include "../machine/reg.h" 27 #include "../machine/cpu.h" 28 29 extern struct user u; /* have to declare it somewhere! */ 30 /* 31 * Initialization code. 32 * Called from cold start routine as 33 * soon as a stack and segmentation 34 * have been established. 35 * Functions: 36 * clear and free user core 37 * turn on clock 38 * hand craft 0th process 39 * call all initialization routines 40 * fork - process 0 to schedule 41 * - process 2 to page out 42 * - process 1 execute bootstrap 43 * 44 * loop at loc 13 (0xd) in user mode -- /etc/init 45 * cannot be executed. 46 */ 47 #ifdef vax 48 main(firstaddr) 49 int firstaddr; 50 #endif 51 #ifdef sun 52 main(regs) 53 struct regs regs; 54 #endif 55 { 56 register int i; 57 register struct proc *p; 58 struct fs *fs; 59 int s; 60 61 rqinit(); 62 #include "loop.h" 63 #ifdef vax 64 startup(firstaddr); 65 #endif 66 #ifdef sun 67 startup(); 68 #endif 69 70 /* 71 * set up system process 0 (swapper) 72 */ 73 p = &proc[0]; 74 p->p_p0br = u.u_pcb.pcb_p0br; 75 p->p_szpt = 1; 76 p->p_addr = uaddr(p); 77 p->p_stat = SRUN; 78 p->p_flag |= SLOAD|SSYS; 79 p->p_nice = NZERO; 80 setredzone(p->p_addr, (caddr_t)&u); 81 u.u_procp = p; 82 #ifdef sun 83 u.u_ar0 = ®s.r_r0; 84 #endif 85 u.u_cmask = CMASK; 86 for (i = 1; i < NGROUPS; i++) 87 u.u_groups[i] = -1; 88 for (i = 0; i < sizeof(u.u_rlimit)/sizeof(u.u_rlimit[0]); i++) 89 u.u_rlimit[i].rlim_cur = u.u_rlimit[i].rlim_max = 90 RLIM_INFINITY; 91 u.u_rlimit[RLIMIT_STACK].rlim_cur = 512*1024; 92 u.u_rlimit[RLIMIT_STACK].rlim_max = ctob(MAXDSIZ); 93 u.u_rlimit[RLIMIT_DATA].rlim_max = 94 u.u_rlimit[RLIMIT_DATA].rlim_cur = ctob(MAXDSIZ); 95 p->p_maxrss = RLIM_INFINITY/NBPG; 96 #ifdef QUOTA 97 qtinit(); 98 p->p_quota = u.u_quota = getquota(0, 0, Q_NDQ); 99 #endif 100 startrtclock(); 101 102 /* 103 * Initialize tables, protocols, and set up well-known inodes. 104 */ 105 mbinit(); 106 cinit(); /* needed by dmc-11 driver */ 107 #ifdef INET 108 #if NLOOP > 0 109 loattach(); /* XXX */ 110 #endif 111 /* 112 * Block reception of incoming packets 113 * until protocols have been initialized. 114 */ 115 s = splimp(); 116 ifinit(); 117 #endif 118 domaininit(); 119 #ifdef INET 120 splx(s); 121 #endif 122 ihinit(); 123 bhinit(); 124 binit(); 125 bswinit(); 126 #ifdef GPROF 127 kmstartup(); 128 #endif 129 130 fs = mountfs(rootdev, 0, (struct inode *)0); 131 if (fs == 0) 132 panic("iinit"); 133 bcopy("/", fs->fs_fsmnt, 2); 134 135 inittodr(fs->fs_time); 136 boottime = time; 137 138 /* kick off timeout driven events by calling first time */ 139 roundrobin(); 140 schedcpu(); 141 schedpaging(); 142 143 /* set up the root file system */ 144 rootdir = iget(rootdev, fs, (ino_t)ROOTINO); 145 iunlock(rootdir); 146 u.u_cdir = iget(rootdev, fs, (ino_t)ROOTINO); 147 iunlock(u.u_cdir); 148 u.u_rdir = NULL; 149 150 u.u_dmap = zdmap; 151 u.u_smap = zdmap; 152 153 /* 154 * Set the scan rate and other parameters of the paging subsystem. 155 */ 156 setupclock(); 157 158 /* 159 * make page-out daemon (process 2) 160 * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page 161 * table so that it can map dirty pages into 162 * its address space during asychronous pushes. 163 */ 164 mpid = 1; 165 proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES)); 166 proc[1].p_stat = SZOMB; /* force it to be in proc slot 2 */ 167 if (newproc(0)) { 168 proc[2].p_flag |= SLOAD|SSYS; 169 proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX; 170 #ifdef NOPAGING 171 for (;;) 172 sleep((caddr_t)&u, PSLEP); 173 /*NOTREACHED*/ 174 #else 175 pageout(); 176 /*NOTREACHED*/ 177 #endif 178 } 179 180 /* 181 * make init process and 182 * enter scheduling loop 183 */ 184 185 mpid = 0; 186 proc[1].p_stat = 0; 187 proc[0].p_szpt = CLSIZE; 188 if (newproc(0)) { 189 #ifdef vax 190 expand(clrnd((int)btoc(szicode)), 0); 191 (void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap); 192 (void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode); 193 #endif 194 #ifdef sun 195 icode(); 196 usetup(); 197 regs.r_context = u.u_pcb.pcb_ctx->ctx_context; 198 #endif 199 /* 200 * Return goes to loc. 0 of user init 201 * code just copied out. 202 */ 203 return; 204 } 205 #ifdef MUSH 206 /* 207 * start MUSH daemon 208 * pid == 3 209 */ 210 if (newproc(0)) { 211 #ifdef vax 212 expand(clrnd((int)btoc(szmcode)), 0); 213 (void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap); 214 (void) copyout((caddr_t)mcode, (caddr_t)0, (unsigned)szmcode); 215 /* 216 * Return goes to loc. 0 of user mush 217 * code just copied out. 218 */ 219 return; 220 #endif 221 } 222 #endif 223 proc[0].p_szpt = 1; 224 sched(); 225 } 226 227 /* 228 * Initialize hash links for buffers. 229 */ 230 bhinit() 231 { 232 register int i; 233 register struct bufhd *bp; 234 235 for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++) 236 bp->b_forw = bp->b_back = (struct buf *)bp; 237 } 238 239 /* 240 * Initialize the buffer I/O system by freeing 241 * all buffers and setting all device buffer lists to empty. 242 */ 243 binit() 244 { 245 register struct buf *bp, *dp; 246 register int i; 247 struct swdevt *swp; 248 int base, residual; 249 250 for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) { 251 dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp; 252 dp->b_flags = B_HEAD; 253 } 254 base = bufpages / nbuf; 255 residual = bufpages % nbuf; 256 for (i = 0; i < nbuf; i++) { 257 bp = &buf[i]; 258 bp->b_dev = NODEV; 259 bp->b_bcount = 0; 260 #ifndef sun 261 bp->b_un.b_addr = buffers + i * MAXBSIZE; 262 if (i < residual) 263 bp->b_bufsize = (base + 1) * CLBYTES; 264 else 265 bp->b_bufsize = base * CLBYTES; 266 binshash(bp, &bfreelist[BQ_AGE]); 267 #else 268 bp->b_un.b_addr = (char *)0; 269 bp->b_bufsize = 0; 270 binshash(bp, &bfreelist[BQ_EMPTY]); 271 #endif 272 bp->b_flags = B_BUSY|B_INVAL; 273 brelse(bp); 274 } 275 /* 276 * Count swap devices, and adjust total swap space available. 277 * Some of this space will not be available until a vswapon() 278 * system is issued, usually when the system goes multi-user. 279 */ 280 nswdev = 0; 281 for (swp = swdevt; swp->sw_dev; swp++) 282 nswdev++; 283 if (nswdev == 0) 284 panic("binit"); 285 if (nswdev > 1) 286 nswap = (nswap/DMMAX)*DMMAX; 287 nswap *= nswdev; 288 maxpgio *= nswdev; 289 swfree(0); 290 } 291 292 /* 293 * Initialize linked list of free swap 294 * headers. These do not actually point 295 * to buffers, but rather to pages that 296 * are being swapped in and out. 297 */ 298 bswinit() 299 { 300 register int i; 301 register struct buf *sp = swbuf; 302 303 bswlist.av_forw = sp; 304 for (i=0; i<nswbuf-1; i++, sp++) 305 sp->av_forw = sp+1; 306 sp->av_forw = NULL; 307 } 308 309 /* 310 * Initialize clist by freeing all character blocks, then count 311 * number of character devices. (Once-only routine) 312 */ 313 cinit() 314 { 315 register int ccp; 316 register struct cblock *cp; 317 318 ccp = (int)cfree; 319 ccp = (ccp+CROUND) & ~CROUND; 320 for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) { 321 cp->c_next = cfreelist; 322 cfreelist = cp; 323 cfreecount += CBSIZE; 324 } 325 } 326