1*727Sbill /* init_main.c 3.11 08/27/80 */ 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" 20348Sbill #include "../h/text.h" 21366Sbill #include "../h/limit.h" 2226Sbill 2326Sbill /* 2426Sbill * Initialization code. 2526Sbill * Called from cold start routine as 2626Sbill * soon as a stack and segmentation 2726Sbill * have been established. 2826Sbill * Functions: 2926Sbill * clear and free user core 3026Sbill * turn on clock 3126Sbill * hand craft 0th process 3226Sbill * call all initialization routines 3326Sbill * fork - process 0 to schedule 3426Sbill * - process 2 to page out 3526Sbill * - process 1 execute bootstrap 3626Sbill * 3726Sbill * loop at loc 13 (0xd) in user mode -- /etc/init 3826Sbill * cannot be executed. 3926Sbill */ 4026Sbill main(firstaddr) 4126Sbill { 42361Sbill register int i; 4326Sbill 4426Sbill cpusid = mfpr(SID); /* get system identification */ 4526Sbill #ifdef FASTVAX 4626Sbill rqinit(); 4726Sbill #endif 4826Sbill startup(firstaddr); 4926Sbill if (lotsfree == 0) 5026Sbill lotsfree = LOTSFREE; 5126Sbill 5226Sbill /* 5326Sbill * set up system process 0 (swapper) 5426Sbill */ 5526Sbill 5626Sbill proc[0].p_p0br = (struct pte *)mfpr(P0BR); 5726Sbill proc[0].p_szpt = 1; 5826Sbill proc[0].p_addr = uaddr(&proc[0]); 5926Sbill proc[0].p_stat = SRUN; 6026Sbill proc[0].p_flag |= SLOAD|SSYS; 6126Sbill proc[0].p_nice = NZERO; 6226Sbill u.u_procp = &proc[0]; 6326Sbill u.u_cmask = CMASK; 64361Sbill for (i = 1; i < sizeof(u.u_limit)/sizeof(u.u_limit[0]); i++) 65361Sbill u.u_limit[i] = INFINITY; 6626Sbill clkstart(); 6726Sbill 6826Sbill /* 6926Sbill * Initialize devices and 7026Sbill * set up 'known' i-nodes 7126Sbill */ 7226Sbill 7326Sbill ihinit(); 7492Sbill bhinit(); 7526Sbill cinit(); 7626Sbill binit(); 7726Sbill bswinit(); 7826Sbill iinit(); 7926Sbill rootdir = iget(rootdev, (ino_t)ROOTINO); 8026Sbill rootdir->i_flag &= ~ILOCK; 8126Sbill u.u_cdir = iget(rootdev, (ino_t)ROOTINO); 8226Sbill u.u_cdir->i_flag &= ~ILOCK; 8326Sbill u.u_rdir = NULL; 8426Sbill u.u_dmap = zdmap; 8526Sbill u.u_smap = zdmap; 8626Sbill 8726Sbill /* 8826Sbill * make page-out daemon (process 2) 8926Sbill * the daemon has ctopt(NSWBUF*CLSIZE*KLMAX) pages of page 9026Sbill * table so that it can map dirty pages into 9126Sbill * its address space during asychronous pushes. 9226Sbill */ 9326Sbill 9426Sbill mpid = 1; 9526Sbill proc[0].p_szpt = clrnd(ctopt(NSWBUF*CLSIZE*KLMAX + UPAGES)); 9626Sbill proc[1].p_stat = SZOMB; /* force it to be in proc slot 2 */ 9726Sbill if (newproc(0)) { 9826Sbill proc[2].p_flag |= SLOAD|SSYS; 9926Sbill proc[2].p_dsize = u.u_dsize = NSWBUF*CLSIZE*KLMAX; 10026Sbill pageout(); 10126Sbill } 10226Sbill 10326Sbill /* 10426Sbill * make init process and 10526Sbill * enter scheduling loop 10626Sbill */ 10726Sbill 10826Sbill mpid = 0; 10926Sbill proc[1].p_stat = 0; 11026Sbill proc[0].p_szpt = CLSIZE; 11126Sbill if (newproc(0)) { 11226Sbill expand(clrnd((int)btoc(szicode)), P0BR); 113348Sbill swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap); 114135Sbill (void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode); 11526Sbill /* 11626Sbill * Return goes to loc. 0 of user init 11726Sbill * code just copied out. 11826Sbill */ 11926Sbill return; 12026Sbill } 12126Sbill proc[0].p_szpt = 1; 12226Sbill sched(); 12326Sbill } 12426Sbill 12526Sbill /* 12626Sbill * iinit is called once (from main) 12726Sbill * very early in initialization. 12826Sbill * It reads the root's super block 12926Sbill * and initializes the current date 13026Sbill * from the last modified date. 13126Sbill * 13226Sbill * panic: iinit -- cannot read the super 13326Sbill * block. Usually because of an IO error. 13426Sbill */ 13526Sbill iinit() 13626Sbill { 13726Sbill register struct buf *cp, *bp; 13826Sbill register struct filsys *fp; 13926Sbill register unsigned i, j; 14026Sbill 14126Sbill (*bdevsw[major(rootdev)].d_open)(rootdev, 1); 14226Sbill bp = bread(rootdev, SUPERB); 14326Sbill cp = geteblk(); 14426Sbill if(u.u_error) 14526Sbill panic("iinit"); 14626Sbill bcopy(bp->b_un.b_addr, cp->b_un.b_addr, sizeof(struct filsys)); 14726Sbill brelse(bp); 14826Sbill mount[0].m_bufp = cp; 14926Sbill mount[0].m_dev = rootdev; 15026Sbill fp = cp->b_un.b_filsys; 15126Sbill fp->s_flock = 0; 15226Sbill fp->s_ilock = 0; 15326Sbill fp->s_ronly = 0; 15426Sbill fp->s_lasti = 1; 15526Sbill fp->s_nbehind = 0; 15626Sbill /* on boot, read VAX TODR register (GMT 10 ms. 15726Sbill * clicks into current year) and set software time 15826Sbill * in 'int time' (GMT seconds since year YRREF) 15926Sbill */ 16026Sbill for (i = 0 , j = YRREF ; j < YRCURR ; j++) 16126Sbill i += (SECYR + (j%4?0:SECDAY)) ; 16226Sbill time = udiv(mfpr(TODR),100) + i ; 16326Sbill bootime = time; 16426Sbill } 16526Sbill 16626Sbill /* 16726Sbill * This is the set of buffers proper, whose heads 16826Sbill * were declared in buf.h. There can exist buffer 16926Sbill * headers not pointing here that are used purely 17026Sbill * as arguments to the I/O routines to describe 17126Sbill * I/O to be done-- e.g. swap headers swbuf[] for 17226Sbill * swapping. 173*727Sbill * 174*727Sbill * These are actually allocated kernel map slots and space is 175*727Sbill * allocated in locore.s for them. 17626Sbill */ 177108Sbill char buffers[NBUF][BSIZE]; 17826Sbill 17926Sbill /* 18026Sbill * Initialize the buffer I/O system by freeing 18126Sbill * all buffers and setting all device buffer lists to empty. 18226Sbill */ 18326Sbill binit() 18426Sbill { 18526Sbill register struct buf *bp; 18626Sbill register struct buf *dp; 18726Sbill register int i; 18826Sbill struct bdevsw *bdp; 189306Sbill struct swdevt *swp; 19026Sbill 19126Sbill bfreelist.b_forw = bfreelist.b_back = 19226Sbill bfreelist.av_forw = bfreelist.av_back = &bfreelist; 19326Sbill for (i=0; i<NBUF; i++) { 19426Sbill bp = &buf[i]; 19526Sbill bp->b_dev = NODEV; 19626Sbill bp->b_un.b_addr = buffers[i]; 19726Sbill bp->b_back = &bfreelist; 19826Sbill bp->b_forw = bfreelist.b_forw; 19926Sbill bfreelist.b_forw->b_back = bp; 20026Sbill bfreelist.b_forw = bp; 20126Sbill bp->b_flags = B_BUSY; 20226Sbill brelse(bp); 20326Sbill } 20426Sbill for (bdp = bdevsw; bdp->d_open; bdp++) { 20526Sbill dp = bdp->d_tab; 20626Sbill if(dp) { 20726Sbill dp->b_forw = dp; 20826Sbill dp->b_back = dp; 20926Sbill } 21026Sbill nblkdev++; 21126Sbill } 212306Sbill /* 213306Sbill * Count swap devices, and adjust total swap space available. 214306Sbill * Some of this space will not be available until a vswapon() 215306Sbill * system is issued, usually when the system goes multi-user. 216306Sbill */ 217306Sbill nswdev = 0; 218306Sbill for (swp = swdevt; swp->sw_dev; swp++) 219306Sbill nswdev++; 220306Sbill if (nswdev == 0) 221306Sbill panic("binit"); 222306Sbill nswap *= nswdev; 223306Sbill maxpgio *= nswdev; 224306Sbill swfree(0); 22526Sbill } 22626Sbill 22726Sbill /* 22826Sbill * Initialize linked list of free swap 22926Sbill * headers. These do not actually point 23026Sbill * to buffers, but rather to pages that 23126Sbill * are being swapped in and out. 23226Sbill */ 23326Sbill bswinit() 23426Sbill { 23526Sbill register int i; 23626Sbill 23726Sbill bswlist.av_forw = &swbuf[0]; 23826Sbill for (i=0; i<NSWBUF-1; i++) 23926Sbill swbuf[i].av_forw = &swbuf[i+1]; 24026Sbill swbuf[NSWBUF-1].av_forw = NULL; 24126Sbill } 242