1*2746Swnj /* init_main.c 4.8 02/26/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; 432451Swnj register struct proc *p; 4426Sbill 4526Sbill rqinit(); 4626Sbill startup(firstaddr); 4726Sbill if (lotsfree == 0) 4826Sbill lotsfree = LOTSFREE; 4926Sbill 5026Sbill /* 5126Sbill * set up system process 0 (swapper) 5226Sbill */ 532451Swnj p = &proc[0]; 542451Swnj p->p_p0br = (struct pte *)mfpr(P0BR); 552451Swnj p->p_szpt = 1; 562451Swnj p->p_addr = uaddr(p); 572451Swnj p->p_stat = SRUN; 582451Swnj p->p_flag |= SLOAD|SSYS; 592451Swnj p->p_nice = NZERO; 602451Swnj setredzone(p->p_addr, (caddr_t)&u); 612451Swnj u.u_procp = p; 6226Sbill u.u_cmask = CMASK; 63361Sbill for (i = 1; i < sizeof(u.u_limit)/sizeof(u.u_limit[0]); i++) 64870Sbill switch (i) { 65870Sbill 66870Sbill case LIM_STACK: 67870Sbill u.u_limit[i] = 512*1024; 68870Sbill continue; 69870Sbill case LIM_DATA: 70870Sbill u.u_limit[i] = ctob(MAXDSIZ); 71870Sbill continue; 72870Sbill default: 73870Sbill u.u_limit[i] = INFINITY; 74870Sbill continue; 75870Sbill } 7626Sbill clkstart(); 7726Sbill 7826Sbill /* 7926Sbill * Initialize devices and 8026Sbill * set up 'known' i-nodes 8126Sbill */ 8226Sbill 8326Sbill ihinit(); 8492Sbill bhinit(); 8526Sbill cinit(); 8626Sbill binit(); 8726Sbill bswinit(); 8826Sbill iinit(); 8926Sbill rootdir = iget(rootdev, (ino_t)ROOTINO); 9026Sbill rootdir->i_flag &= ~ILOCK; 9126Sbill u.u_cdir = iget(rootdev, (ino_t)ROOTINO); 9226Sbill u.u_cdir->i_flag &= ~ILOCK; 9326Sbill u.u_rdir = NULL; 9426Sbill u.u_dmap = zdmap; 9526Sbill u.u_smap = zdmap; 9626Sbill 9726Sbill /* 9826Sbill * make page-out daemon (process 2) 9926Sbill * the daemon has ctopt(NSWBUF*CLSIZE*KLMAX) pages of page 10026Sbill * table so that it can map dirty pages into 10126Sbill * its address space during asychronous pushes. 10226Sbill */ 10326Sbill 10426Sbill mpid = 1; 10526Sbill proc[0].p_szpt = clrnd(ctopt(NSWBUF*CLSIZE*KLMAX + UPAGES)); 10626Sbill proc[1].p_stat = SZOMB; /* force it to be in proc slot 2 */ 10726Sbill if (newproc(0)) { 10826Sbill proc[2].p_flag |= SLOAD|SSYS; 10926Sbill proc[2].p_dsize = u.u_dsize = NSWBUF*CLSIZE*KLMAX; 11026Sbill pageout(); 11126Sbill } 11226Sbill 11326Sbill /* 11426Sbill * make init process and 11526Sbill * enter scheduling loop 11626Sbill */ 11726Sbill 11826Sbill mpid = 0; 11926Sbill proc[1].p_stat = 0; 12026Sbill proc[0].p_szpt = CLSIZE; 12126Sbill if (newproc(0)) { 12226Sbill expand(clrnd((int)btoc(szicode)), P0BR); 1231787Sbill (void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap); 124135Sbill (void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode); 12526Sbill /* 12626Sbill * Return goes to loc. 0 of user init 12726Sbill * code just copied out. 12826Sbill */ 12926Sbill return; 13026Sbill } 13126Sbill proc[0].p_szpt = 1; 13226Sbill sched(); 13326Sbill } 13426Sbill 13526Sbill /* 13626Sbill * iinit is called once (from main) 13726Sbill * very early in initialization. 13826Sbill * It reads the root's super block 13926Sbill * and initializes the current date 14026Sbill * from the last modified date. 14126Sbill * 14226Sbill * panic: iinit -- cannot read the super 14326Sbill * block. Usually because of an IO error. 14426Sbill */ 14526Sbill iinit() 14626Sbill { 1472435Skre register struct buf *bp; 14826Sbill register struct filsys *fp; 14926Sbill 15026Sbill (*bdevsw[major(rootdev)].d_open)(rootdev, 1); 15126Sbill bp = bread(rootdev, SUPERB); 15226Sbill if(u.u_error) 15326Sbill panic("iinit"); 1542322Swnj bp->b_flags |= B_LOCKED; /* block can never be re-used */ 15526Sbill brelse(bp); 15626Sbill mount[0].m_dev = rootdev; 1572322Swnj mount[0].m_bufp = bp; 1582322Swnj fp = bp->b_un.b_filsys; 15926Sbill fp->s_flock = 0; 16026Sbill fp->s_ilock = 0; 16126Sbill fp->s_ronly = 0; 16226Sbill fp->s_lasti = 1; 16326Sbill fp->s_nbehind = 0; 164870Sbill clkinit(fp->s_time); 16526Sbill bootime = time; 16626Sbill } 16726Sbill 16826Sbill /* 16926Sbill * This is the set of buffers proper, whose heads 17026Sbill * were declared in buf.h. There can exist buffer 17126Sbill * headers not pointing here that are used purely 17226Sbill * as arguments to the I/O routines to describe 17326Sbill * I/O to be done-- e.g. swap headers swbuf[] for 17426Sbill * swapping. 175727Sbill * 176727Sbill * These are actually allocated kernel map slots and space is 177727Sbill * allocated in locore.s for them. 17826Sbill */ 179108Sbill char buffers[NBUF][BSIZE]; 18026Sbill 18126Sbill /* 18226Sbill * Initialize the buffer I/O system by freeing 18326Sbill * all buffers and setting all device buffer lists to empty. 18426Sbill */ 18526Sbill binit() 18626Sbill { 18726Sbill register struct buf *bp; 18826Sbill register struct buf *dp; 18926Sbill register int i; 19026Sbill struct bdevsw *bdp; 191306Sbill struct swdevt *swp; 19226Sbill 1932322Swnj for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) { 1942322Swnj dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp; 1952322Swnj dp->b_flags = B_HEAD; 1962322Swnj } 1972322Swnj dp--; /* dp = &bfreelist[BQUEUES-1]; */ 19826Sbill for (i=0; i<NBUF; i++) { 19926Sbill bp = &buf[i]; 20026Sbill bp->b_dev = NODEV; 20126Sbill bp->b_un.b_addr = buffers[i]; 2022322Swnj bp->b_back = dp; 2032322Swnj bp->b_forw = dp->b_forw; 2042322Swnj dp->b_forw->b_back = bp; 2052322Swnj dp->b_forw = bp; 2062322Swnj bp->b_flags = B_BUSY|B_INVAL; 20726Sbill brelse(bp); 20826Sbill } 2092435Skre for (bdp = bdevsw; bdp->d_open; bdp++) 21026Sbill nblkdev++; 211306Sbill /* 212306Sbill * Count swap devices, and adjust total swap space available. 213306Sbill * Some of this space will not be available until a vswapon() 214306Sbill * system is issued, usually when the system goes multi-user. 215306Sbill */ 216306Sbill nswdev = 0; 217306Sbill for (swp = swdevt; swp->sw_dev; swp++) 218306Sbill nswdev++; 219306Sbill if (nswdev == 0) 220306Sbill panic("binit"); 221306Sbill nswap *= nswdev; 222306Sbill maxpgio *= nswdev; 223306Sbill swfree(0); 22426Sbill } 22526Sbill 22626Sbill /* 22726Sbill * Initialize linked list of free swap 22826Sbill * headers. These do not actually point 22926Sbill * to buffers, but rather to pages that 23026Sbill * are being swapped in and out. 23126Sbill */ 23226Sbill bswinit() 23326Sbill { 23426Sbill register int i; 23526Sbill 23626Sbill bswlist.av_forw = &swbuf[0]; 23726Sbill for (i=0; i<NSWBUF-1; i++) 23826Sbill swbuf[i].av_forw = &swbuf[i+1]; 23926Sbill swbuf[NSWBUF-1].av_forw = NULL; 24026Sbill } 241*2746Swnj 242*2746Swnj /* 243*2746Swnj * Initialize clist by freeing all character blocks, then count 244*2746Swnj * number of character devices. (Once-only routine) 245*2746Swnj */ 246*2746Swnj cinit() 247*2746Swnj { 248*2746Swnj register int ccp; 249*2746Swnj register struct cblock *cp; 250*2746Swnj register struct cdevsw *cdp; 251*2746Swnj 252*2746Swnj ccp = (int)cfree; 253*2746Swnj ccp = (ccp+CROUND) & ~CROUND; 254*2746Swnj for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) { 255*2746Swnj cp->c_next = cfreelist; 256*2746Swnj cfreelist = cp; 257*2746Swnj cfreecount += CBSIZE; 258*2746Swnj } 259*2746Swnj ccp = 0; 260*2746Swnj for(cdp = cdevsw; cdp->d_open; cdp++) 261*2746Swnj ccp++; 262*2746Swnj nchrdev = ccp; 263*2746Swnj } 264