1*135Sbill /* init_main.c 3.4 10/14/12 */ 226Sbill 326Sbill #include "../h/param.h" 426Sbill #include "../h/systm.h" 526Sbill #include "../h/dir.h" 626Sbill #include "../h/user.h" 726Sbill #include "../h/filsys.h" 826Sbill #include "../h/mount.h" 926Sbill #include "../h/map.h" 1026Sbill #include "../h/proc.h" 1126Sbill #include "../h/inode.h" 1226Sbill #include "../h/seg.h" 1326Sbill #include "../h/conf.h" 1426Sbill #include "../h/buf.h" 1526Sbill #include "../h/mtpr.h" 1626Sbill #include "../h/pte.h" 1726Sbill #include "../h/clock.h" 1826Sbill #include "../h/vm.h" 1926Sbill #include "../h/cmap.h" 2026Sbill 2126Sbill /* 2226Sbill * Initialization code. 2326Sbill * Called from cold start routine as 2426Sbill * soon as a stack and segmentation 2526Sbill * have been established. 2626Sbill * Functions: 2726Sbill * clear and free user core 2826Sbill * turn on clock 2926Sbill * hand craft 0th process 3026Sbill * call all initialization routines 3126Sbill * fork - process 0 to schedule 3226Sbill * - process 2 to page out 3326Sbill * - process 1 execute bootstrap 3426Sbill * 3526Sbill * loop at loc 13 (0xd) in user mode -- /etc/init 3626Sbill * cannot be executed. 3726Sbill */ 3826Sbill main(firstaddr) 3926Sbill { 4026Sbill 4126Sbill cpusid = mfpr(SID); /* get system identification */ 4226Sbill #ifdef FASTVAX 4326Sbill rqinit(); 4426Sbill #endif 4526Sbill startup(firstaddr); 4626Sbill if (lotsfree == 0) 4726Sbill lotsfree = LOTSFREE; 4826Sbill 4926Sbill /* 5026Sbill * set up system process 0 (swapper) 5126Sbill */ 5226Sbill 5326Sbill proc[0].p_p0br = (struct pte *)mfpr(P0BR); 5426Sbill proc[0].p_szpt = 1; 5526Sbill proc[0].p_addr = uaddr(&proc[0]); 5626Sbill proc[0].p_stat = SRUN; 5726Sbill proc[0].p_flag |= SLOAD|SSYS; 5826Sbill proc[0].p_nice = NZERO; 5926Sbill u.u_procp = &proc[0]; 6026Sbill u.u_cmask = CMASK; 6126Sbill clkstart(); 6226Sbill 6326Sbill /* 6426Sbill * Initialize devices and 6526Sbill * set up 'known' i-nodes 6626Sbill */ 6726Sbill 6826Sbill ihinit(); 6992Sbill bhinit(); 7026Sbill cinit(); 7126Sbill binit(); 7226Sbill bswinit(); 7326Sbill iinit(); 7426Sbill rootdir = iget(rootdev, (ino_t)ROOTINO); 7526Sbill rootdir->i_flag &= ~ILOCK; 7626Sbill u.u_cdir = iget(rootdev, (ino_t)ROOTINO); 7726Sbill u.u_cdir->i_flag &= ~ILOCK; 7826Sbill u.u_rdir = NULL; 7926Sbill u.u_dmap = zdmap; 8026Sbill u.u_smap = zdmap; 8126Sbill 8226Sbill /* 8326Sbill * make page-out daemon (process 2) 8426Sbill * the daemon has ctopt(NSWBUF*CLSIZE*KLMAX) pages of page 8526Sbill * table so that it can map dirty pages into 8626Sbill * its address space during asychronous pushes. 8726Sbill */ 8826Sbill 8926Sbill mpid = 1; 9026Sbill proc[0].p_szpt = clrnd(ctopt(NSWBUF*CLSIZE*KLMAX + UPAGES)); 9126Sbill proc[1].p_stat = SZOMB; /* force it to be in proc slot 2 */ 9226Sbill if (newproc(0)) { 9326Sbill proc[2].p_flag |= SLOAD|SSYS; 9426Sbill proc[2].p_dsize = u.u_dsize = NSWBUF*CLSIZE*KLMAX; 9526Sbill pageout(); 9626Sbill } 9726Sbill 9826Sbill /* 9926Sbill * make init process and 10026Sbill * enter scheduling loop 10126Sbill */ 10226Sbill 10326Sbill mpid = 0; 10426Sbill proc[1].p_stat = 0; 10526Sbill proc[0].p_szpt = CLSIZE; 10626Sbill if (newproc(0)) { 10726Sbill expand(clrnd((int)btoc(szicode)), P0BR); 108*135Sbill (void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode); 10926Sbill /* 11026Sbill * Return goes to loc. 0 of user init 11126Sbill * code just copied out. 11226Sbill */ 11326Sbill return; 11426Sbill } 11526Sbill proc[0].p_szpt = 1; 11626Sbill sched(); 11726Sbill } 11826Sbill 11926Sbill /* 12026Sbill * iinit is called once (from main) 12126Sbill * very early in initialization. 12226Sbill * It reads the root's super block 12326Sbill * and initializes the current date 12426Sbill * from the last modified date. 12526Sbill * 12626Sbill * panic: iinit -- cannot read the super 12726Sbill * block. Usually because of an IO error. 12826Sbill */ 12926Sbill iinit() 13026Sbill { 13126Sbill register struct buf *cp, *bp; 13226Sbill register struct filsys *fp; 13326Sbill register unsigned i, j; 13426Sbill 13526Sbill (*bdevsw[major(rootdev)].d_open)(rootdev, 1); 13626Sbill bp = bread(rootdev, SUPERB); 13726Sbill cp = geteblk(); 13826Sbill if(u.u_error) 13926Sbill panic("iinit"); 14026Sbill bcopy(bp->b_un.b_addr, cp->b_un.b_addr, sizeof(struct filsys)); 14126Sbill brelse(bp); 14226Sbill mount[0].m_bufp = cp; 14326Sbill mount[0].m_dev = rootdev; 14426Sbill fp = cp->b_un.b_filsys; 14526Sbill fp->s_flock = 0; 14626Sbill fp->s_ilock = 0; 14726Sbill fp->s_ronly = 0; 14826Sbill fp->s_lasti = 1; 14926Sbill fp->s_nbehind = 0; 15026Sbill /* on boot, read VAX TODR register (GMT 10 ms. 15126Sbill * clicks into current year) and set software time 15226Sbill * in 'int time' (GMT seconds since year YRREF) 15326Sbill */ 15426Sbill for (i = 0 , j = YRREF ; j < YRCURR ; j++) 15526Sbill i += (SECYR + (j%4?0:SECDAY)) ; 15626Sbill time = udiv(mfpr(TODR),100) + i ; 15726Sbill bootime = time; 15826Sbill } 15926Sbill 16026Sbill /* 16126Sbill * This is the set of buffers proper, whose heads 16226Sbill * were declared in buf.h. There can exist buffer 16326Sbill * headers not pointing here that are used purely 16426Sbill * as arguments to the I/O routines to describe 16526Sbill * I/O to be done-- e.g. swap headers swbuf[] for 16626Sbill * swapping. 16726Sbill */ 168108Sbill char buffers[NBUF][BSIZE]; 16926Sbill 17026Sbill /* 17126Sbill * Initialize the buffer I/O system by freeing 17226Sbill * all buffers and setting all device buffer lists to empty. 17326Sbill * 17426Sbill * SHOULD USE MEMALL HERE!!! 17526Sbill */ 17626Sbill binit() 17726Sbill { 17826Sbill register struct buf *bp; 17926Sbill register struct buf *dp; 18026Sbill register int i; 18126Sbill struct bdevsw *bdp; 18226Sbill 18326Sbill bfreelist.b_forw = bfreelist.b_back = 18426Sbill bfreelist.av_forw = bfreelist.av_back = &bfreelist; 18526Sbill for (i=0; i<NBUF; i++) { 18626Sbill bp = &buf[i]; 18726Sbill bp->b_dev = NODEV; 18826Sbill bp->b_un.b_addr = buffers[i]; 18926Sbill bp->b_back = &bfreelist; 19026Sbill bp->b_forw = bfreelist.b_forw; 19126Sbill bfreelist.b_forw->b_back = bp; 19226Sbill bfreelist.b_forw = bp; 19326Sbill bp->b_flags = B_BUSY; 19426Sbill brelse(bp); 19526Sbill } 19626Sbill for (bdp = bdevsw; bdp->d_open; bdp++) { 19726Sbill dp = bdp->d_tab; 19826Sbill if(dp) { 19926Sbill dp->b_forw = dp; 20026Sbill dp->b_back = dp; 20126Sbill } 20226Sbill nblkdev++; 20326Sbill } 20426Sbill } 20526Sbill 20626Sbill /* 20726Sbill * Initialize linked list of free swap 20826Sbill * headers. These do not actually point 20926Sbill * to buffers, but rather to pages that 21026Sbill * are being swapped in and out. 21126Sbill */ 21226Sbill bswinit() 21326Sbill { 21426Sbill register int i; 21526Sbill 21626Sbill bswlist.av_forw = &swbuf[0]; 21726Sbill for (i=0; i<NSWBUF-1; i++) 21826Sbill swbuf[i].av_forw = &swbuf[i+1]; 21926Sbill swbuf[NSWBUF-1].av_forw = NULL; 22026Sbill } 221