1*2451Swnj /* init_main.c 4.6 02/16/81 */ 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" 21878Sbill #include "../h/vlimit.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; 43*2451Swnj register struct proc *p; 4426Sbill 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 */ 55*2451Swnj p = &proc[0]; 56*2451Swnj p->p_p0br = (struct pte *)mfpr(P0BR); 57*2451Swnj p->p_szpt = 1; 58*2451Swnj p->p_addr = uaddr(p); 59*2451Swnj p->p_stat = SRUN; 60*2451Swnj p->p_flag |= SLOAD|SSYS; 61*2451Swnj p->p_nice = NZERO; 62*2451Swnj setredzone(p->p_addr, (caddr_t)&u); 63*2451Swnj u.u_procp = p; 6426Sbill u.u_cmask = CMASK; 65361Sbill for (i = 1; i < sizeof(u.u_limit)/sizeof(u.u_limit[0]); i++) 66870Sbill switch (i) { 67870Sbill 68870Sbill case LIM_STACK: 69870Sbill u.u_limit[i] = 512*1024; 70870Sbill continue; 71870Sbill case LIM_DATA: 72870Sbill u.u_limit[i] = ctob(MAXDSIZ); 73870Sbill continue; 74870Sbill default: 75870Sbill u.u_limit[i] = INFINITY; 76870Sbill continue; 77870Sbill } 7826Sbill clkstart(); 7926Sbill 8026Sbill /* 8126Sbill * Initialize devices and 8226Sbill * set up 'known' i-nodes 8326Sbill */ 8426Sbill 8526Sbill ihinit(); 8692Sbill bhinit(); 8726Sbill cinit(); 8826Sbill binit(); 8926Sbill bswinit(); 9026Sbill iinit(); 9126Sbill rootdir = iget(rootdev, (ino_t)ROOTINO); 9226Sbill rootdir->i_flag &= ~ILOCK; 9326Sbill u.u_cdir = iget(rootdev, (ino_t)ROOTINO); 9426Sbill u.u_cdir->i_flag &= ~ILOCK; 9526Sbill u.u_rdir = NULL; 9626Sbill u.u_dmap = zdmap; 9726Sbill u.u_smap = zdmap; 9826Sbill 9926Sbill /* 10026Sbill * make page-out daemon (process 2) 10126Sbill * the daemon has ctopt(NSWBUF*CLSIZE*KLMAX) pages of page 10226Sbill * table so that it can map dirty pages into 10326Sbill * its address space during asychronous pushes. 10426Sbill */ 10526Sbill 10626Sbill mpid = 1; 10726Sbill proc[0].p_szpt = clrnd(ctopt(NSWBUF*CLSIZE*KLMAX + UPAGES)); 10826Sbill proc[1].p_stat = SZOMB; /* force it to be in proc slot 2 */ 10926Sbill if (newproc(0)) { 11026Sbill proc[2].p_flag |= SLOAD|SSYS; 11126Sbill proc[2].p_dsize = u.u_dsize = NSWBUF*CLSIZE*KLMAX; 11226Sbill pageout(); 11326Sbill } 11426Sbill 11526Sbill /* 11626Sbill * make init process and 11726Sbill * enter scheduling loop 11826Sbill */ 11926Sbill 12026Sbill mpid = 0; 12126Sbill proc[1].p_stat = 0; 12226Sbill proc[0].p_szpt = CLSIZE; 12326Sbill if (newproc(0)) { 12426Sbill expand(clrnd((int)btoc(szicode)), P0BR); 1251787Sbill (void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap); 126135Sbill (void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode); 12726Sbill /* 12826Sbill * Return goes to loc. 0 of user init 12926Sbill * code just copied out. 13026Sbill */ 13126Sbill return; 13226Sbill } 13326Sbill proc[0].p_szpt = 1; 13426Sbill sched(); 13526Sbill } 13626Sbill 13726Sbill /* 13826Sbill * iinit is called once (from main) 13926Sbill * very early in initialization. 14026Sbill * It reads the root's super block 14126Sbill * and initializes the current date 14226Sbill * from the last modified date. 14326Sbill * 14426Sbill * panic: iinit -- cannot read the super 14526Sbill * block. Usually because of an IO error. 14626Sbill */ 14726Sbill iinit() 14826Sbill { 1492435Skre register struct buf *bp; 15026Sbill register struct filsys *fp; 15126Sbill 15226Sbill (*bdevsw[major(rootdev)].d_open)(rootdev, 1); 15326Sbill bp = bread(rootdev, SUPERB); 15426Sbill if(u.u_error) 15526Sbill panic("iinit"); 1562322Swnj bp->b_flags |= B_LOCKED; /* block can never be re-used */ 15726Sbill brelse(bp); 15826Sbill mount[0].m_dev = rootdev; 1592322Swnj mount[0].m_bufp = bp; 1602322Swnj fp = bp->b_un.b_filsys; 16126Sbill fp->s_flock = 0; 16226Sbill fp->s_ilock = 0; 16326Sbill fp->s_ronly = 0; 16426Sbill fp->s_lasti = 1; 16526Sbill fp->s_nbehind = 0; 166870Sbill clkinit(fp->s_time); 16726Sbill bootime = time; 16826Sbill } 16926Sbill 17026Sbill /* 17126Sbill * This is the set of buffers proper, whose heads 17226Sbill * were declared in buf.h. There can exist buffer 17326Sbill * headers not pointing here that are used purely 17426Sbill * as arguments to the I/O routines to describe 17526Sbill * I/O to be done-- e.g. swap headers swbuf[] for 17626Sbill * swapping. 177727Sbill * 178727Sbill * These are actually allocated kernel map slots and space is 179727Sbill * allocated in locore.s for them. 18026Sbill */ 181108Sbill char buffers[NBUF][BSIZE]; 18226Sbill 18326Sbill /* 18426Sbill * Initialize the buffer I/O system by freeing 18526Sbill * all buffers and setting all device buffer lists to empty. 18626Sbill */ 18726Sbill binit() 18826Sbill { 18926Sbill register struct buf *bp; 19026Sbill register struct buf *dp; 19126Sbill register int i; 19226Sbill struct bdevsw *bdp; 193306Sbill struct swdevt *swp; 19426Sbill 1952322Swnj for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) { 1962322Swnj dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp; 1972322Swnj dp->b_flags = B_HEAD; 1982322Swnj } 1992322Swnj dp--; /* dp = &bfreelist[BQUEUES-1]; */ 20026Sbill for (i=0; i<NBUF; i++) { 20126Sbill bp = &buf[i]; 20226Sbill bp->b_dev = NODEV; 20326Sbill bp->b_un.b_addr = buffers[i]; 2042322Swnj bp->b_back = dp; 2052322Swnj bp->b_forw = dp->b_forw; 2062322Swnj dp->b_forw->b_back = bp; 2072322Swnj dp->b_forw = bp; 2082322Swnj bp->b_flags = B_BUSY|B_INVAL; 20926Sbill brelse(bp); 21026Sbill } 2112435Skre for (bdp = bdevsw; bdp->d_open; bdp++) 21226Sbill nblkdev++; 213306Sbill /* 214306Sbill * Count swap devices, and adjust total swap space available. 215306Sbill * Some of this space will not be available until a vswapon() 216306Sbill * system is issued, usually when the system goes multi-user. 217306Sbill */ 218306Sbill nswdev = 0; 219306Sbill for (swp = swdevt; swp->sw_dev; swp++) 220306Sbill nswdev++; 221306Sbill if (nswdev == 0) 222306Sbill panic("binit"); 223306Sbill nswap *= nswdev; 224306Sbill maxpgio *= nswdev; 225306Sbill swfree(0); 22626Sbill } 22726Sbill 22826Sbill /* 22926Sbill * Initialize linked list of free swap 23026Sbill * headers. These do not actually point 23126Sbill * to buffers, but rather to pages that 23226Sbill * are being swapped in and out. 23326Sbill */ 23426Sbill bswinit() 23526Sbill { 23626Sbill register int i; 23726Sbill 23826Sbill bswlist.av_forw = &swbuf[0]; 23926Sbill for (i=0; i<NSWBUF-1; i++) 24026Sbill swbuf[i].av_forw = &swbuf[i+1]; 24126Sbill swbuf[NSWBUF-1].av_forw = NULL; 24226Sbill } 243