1*6342Ssam /* init_main.c 4.28 82/03/28 */ 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" 222769Swnj #include "../h/clist.h" 234821Swnj #ifdef INET 244821Swnj #include "../h/protosw.h" 254821Swnj #endif 2626Sbill 2726Sbill /* 2826Sbill * Initialization code. 2926Sbill * Called from cold start routine as 3026Sbill * soon as a stack and segmentation 3126Sbill * have been established. 3226Sbill * Functions: 3326Sbill * clear and free user core 3426Sbill * turn on clock 3526Sbill * hand craft 0th process 3626Sbill * call all initialization routines 3726Sbill * fork - process 0 to schedule 3826Sbill * - process 2 to page out 3926Sbill * - process 1 execute bootstrap 4026Sbill * 4126Sbill * loop at loc 13 (0xd) in user mode -- /etc/init 4226Sbill * cannot be executed. 4326Sbill */ 4426Sbill main(firstaddr) 4526Sbill { 46361Sbill register int i; 472451Swnj register struct proc *p; 4826Sbill 4926Sbill rqinit(); 505227Swnj #include "loop.h" 5126Sbill startup(firstaddr); 5226Sbill 5326Sbill /* 5426Sbill * set up system process 0 (swapper) 5526Sbill */ 562451Swnj p = &proc[0]; 572451Swnj p->p_p0br = (struct pte *)mfpr(P0BR); 582451Swnj p->p_szpt = 1; 592451Swnj p->p_addr = uaddr(p); 602451Swnj p->p_stat = SRUN; 612451Swnj p->p_flag |= SLOAD|SSYS; 622451Swnj p->p_nice = NZERO; 632451Swnj setredzone(p->p_addr, (caddr_t)&u); 642451Swnj u.u_procp = p; 6526Sbill u.u_cmask = CMASK; 66361Sbill for (i = 1; i < sizeof(u.u_limit)/sizeof(u.u_limit[0]); i++) 67870Sbill switch (i) { 68870Sbill 69870Sbill case LIM_STACK: 70870Sbill u.u_limit[i] = 512*1024; 71870Sbill continue; 72870Sbill case LIM_DATA: 73870Sbill u.u_limit[i] = ctob(MAXDSIZ); 74870Sbill continue; 75870Sbill default: 76870Sbill u.u_limit[i] = INFINITY; 77870Sbill continue; 78870Sbill } 793513Sroot p->p_maxrss = INFINITY/NBPG; 8026Sbill clkstart(); 8126Sbill 8226Sbill /* 834821Swnj * Initialize tables, protocols, and set up well-known inodes. 8426Sbill */ 854821Swnj mbinit(); 866121Swnj cinit(); /* needed by dmc-11 driver */ 874821Swnj #ifdef INET 885855Swnj #if NLOOP > 0 895855Swnj loattach(); /* XXX */ 905855Swnj #endif 915227Swnj ifinit(); 92*6342Ssam pfinit(); /* must follow interfaces */ 934821Swnj #endif 9426Sbill ihinit(); 9592Sbill bhinit(); 9626Sbill binit(); 9726Sbill bswinit(); 9826Sbill iinit(); 9926Sbill rootdir = iget(rootdev, (ino_t)ROOTINO); 10026Sbill rootdir->i_flag &= ~ILOCK; 10126Sbill u.u_cdir = iget(rootdev, (ino_t)ROOTINO); 10226Sbill u.u_cdir->i_flag &= ~ILOCK; 10326Sbill u.u_rdir = NULL; 10426Sbill u.u_dmap = zdmap; 10526Sbill u.u_smap = zdmap; 10626Sbill 10726Sbill /* 1083591Swnj * Set the scan rate and other parameters of the paging subsystem. 1093591Swnj */ 1103591Swnj setupclock(); 1113591Swnj 1123591Swnj /* 11326Sbill * make page-out daemon (process 2) 1142753Swnj * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page 11526Sbill * table so that it can map dirty pages into 11626Sbill * its address space during asychronous pushes. 11726Sbill */ 11826Sbill mpid = 1; 1192753Swnj proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES)); 12026Sbill proc[1].p_stat = SZOMB; /* force it to be in proc slot 2 */ 12126Sbill if (newproc(0)) { 12226Sbill proc[2].p_flag |= SLOAD|SSYS; 1232753Swnj proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX; 12426Sbill pageout(); 12526Sbill } 12626Sbill 12726Sbill /* 12826Sbill * make init process and 12926Sbill * enter scheduling loop 13026Sbill */ 13126Sbill 13226Sbill mpid = 0; 13326Sbill proc[1].p_stat = 0; 13426Sbill proc[0].p_szpt = CLSIZE; 13526Sbill if (newproc(0)) { 13626Sbill expand(clrnd((int)btoc(szicode)), P0BR); 1371787Sbill (void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap); 138135Sbill (void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode); 13926Sbill /* 14026Sbill * Return goes to loc. 0 of user init 14126Sbill * code just copied out. 14226Sbill */ 14326Sbill return; 14426Sbill } 14526Sbill proc[0].p_szpt = 1; 14626Sbill sched(); 14726Sbill } 14826Sbill 14926Sbill /* 15026Sbill * iinit is called once (from main) 15126Sbill * very early in initialization. 15226Sbill * It reads the root's super block 15326Sbill * and initializes the current date 15426Sbill * from the last modified date. 15526Sbill * 15626Sbill * panic: iinit -- cannot read the super 15726Sbill * block. Usually because of an IO error. 15826Sbill */ 15926Sbill iinit() 16026Sbill { 1612435Skre register struct buf *bp; 16226Sbill register struct filsys *fp; 1632875Swnj register int i; 16426Sbill 16526Sbill (*bdevsw[major(rootdev)].d_open)(rootdev, 1); 16626Sbill bp = bread(rootdev, SUPERB); 16726Sbill if(u.u_error) 16826Sbill panic("iinit"); 1692322Swnj bp->b_flags |= B_LOCKED; /* block can never be re-used */ 17026Sbill brelse(bp); 17126Sbill mount[0].m_dev = rootdev; 1722322Swnj mount[0].m_bufp = bp; 1732322Swnj fp = bp->b_un.b_filsys; 17426Sbill fp->s_flock = 0; 17526Sbill fp->s_ilock = 0; 17626Sbill fp->s_ronly = 0; 17726Sbill fp->s_lasti = 1; 17826Sbill fp->s_nbehind = 0; 1792875Swnj fp->s_fsmnt[0] = '/'; 1802875Swnj for (i = 1; i < sizeof(fp->s_fsmnt); i++) 1812875Swnj fp->s_fsmnt[i] = 0; 182870Sbill clkinit(fp->s_time); 18326Sbill bootime = time; 18426Sbill } 18526Sbill 18626Sbill /* 18726Sbill * Initialize the buffer I/O system by freeing 18826Sbill * all buffers and setting all device buffer lists to empty. 18926Sbill */ 19026Sbill binit() 19126Sbill { 19226Sbill register struct buf *bp; 19326Sbill register struct buf *dp; 19426Sbill register int i; 19526Sbill struct bdevsw *bdp; 196306Sbill struct swdevt *swp; 19726Sbill 1982322Swnj for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) { 1992322Swnj dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp; 2002322Swnj dp->b_flags = B_HEAD; 2012322Swnj } 2022322Swnj dp--; /* dp = &bfreelist[BQUEUES-1]; */ 2032769Swnj for (i=0; i<nbuf; i++) { 20426Sbill bp = &buf[i]; 20526Sbill bp->b_dev = NODEV; 2062769Swnj bp->b_un.b_addr = buffers + i * BSIZE; 2072322Swnj bp->b_back = dp; 2082322Swnj bp->b_forw = dp->b_forw; 2092322Swnj dp->b_forw->b_back = bp; 2102322Swnj dp->b_forw = bp; 2112322Swnj bp->b_flags = B_BUSY|B_INVAL; 21226Sbill brelse(bp); 21326Sbill } 2142435Skre for (bdp = bdevsw; bdp->d_open; bdp++) 21526Sbill nblkdev++; 216306Sbill /* 217306Sbill * Count swap devices, and adjust total swap space available. 218306Sbill * Some of this space will not be available until a vswapon() 219306Sbill * system is issued, usually when the system goes multi-user. 220306Sbill */ 221306Sbill nswdev = 0; 222306Sbill for (swp = swdevt; swp->sw_dev; swp++) 223306Sbill nswdev++; 224306Sbill if (nswdev == 0) 225306Sbill panic("binit"); 2264132Secc if (nswdev > 1) 2274132Secc nswap = (nswap/DMMAX)*DMMAX; 228306Sbill nswap *= nswdev; 229306Sbill maxpgio *= nswdev; 230306Sbill swfree(0); 23126Sbill } 23226Sbill 23326Sbill /* 23426Sbill * Initialize linked list of free swap 23526Sbill * headers. These do not actually point 23626Sbill * to buffers, but rather to pages that 23726Sbill * are being swapped in and out. 23826Sbill */ 23926Sbill bswinit() 24026Sbill { 24126Sbill register int i; 2422769Swnj register struct buf *sp = swbuf; 24326Sbill 2442753Swnj bswlist.av_forw = sp; 2452769Swnj for (i=0; i<nswbuf-1; i++, sp++) 2462753Swnj sp->av_forw = sp+1; 2472753Swnj sp->av_forw = NULL; 24826Sbill } 2492746Swnj 2502746Swnj /* 2512746Swnj * Initialize clist by freeing all character blocks, then count 2522746Swnj * number of character devices. (Once-only routine) 2532746Swnj */ 2542746Swnj cinit() 2552746Swnj { 2562746Swnj register int ccp; 2572746Swnj register struct cblock *cp; 2582746Swnj register struct cdevsw *cdp; 2592746Swnj 2602746Swnj ccp = (int)cfree; 2612746Swnj ccp = (ccp+CROUND) & ~CROUND; 2622746Swnj for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) { 2632746Swnj cp->c_next = cfreelist; 2642746Swnj cfreelist = cp; 2652746Swnj cfreecount += CBSIZE; 2662746Swnj } 2672746Swnj ccp = 0; 2682746Swnj for(cdp = cdevsw; cdp->d_open; cdp++) 2692746Swnj ccp++; 2702746Swnj nchrdev = ccp; 2712746Swnj } 272