1*7419Sroot /* init_main.c 4.32 82/07/15 */ 226Sbill 326Sbill #include "../h/param.h" 426Sbill #include "../h/systm.h" 526Sbill #include "../h/dir.h" 626Sbill #include "../h/user.h" 76570Smckusic #include "../h/fs.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; 487189Sroot struct fs *fs; 4926Sbill 5026Sbill rqinit(); 515227Swnj #include "loop.h" 5226Sbill startup(firstaddr); 5326Sbill 5426Sbill /* 5526Sbill * set up system process 0 (swapper) 5626Sbill */ 572451Swnj p = &proc[0]; 582451Swnj p->p_p0br = (struct pte *)mfpr(P0BR); 592451Swnj p->p_szpt = 1; 602451Swnj p->p_addr = uaddr(p); 612451Swnj p->p_stat = SRUN; 622451Swnj p->p_flag |= SLOAD|SSYS; 632451Swnj p->p_nice = NZERO; 642451Swnj setredzone(p->p_addr, (caddr_t)&u); 652451Swnj u.u_procp = p; 6626Sbill u.u_cmask = CMASK; 67361Sbill for (i = 1; i < sizeof(u.u_limit)/sizeof(u.u_limit[0]); i++) 68870Sbill switch (i) { 69870Sbill 70870Sbill case LIM_STACK: 71870Sbill u.u_limit[i] = 512*1024; 72870Sbill continue; 73870Sbill case LIM_DATA: 74870Sbill u.u_limit[i] = ctob(MAXDSIZ); 75870Sbill continue; 76870Sbill default: 77870Sbill u.u_limit[i] = INFINITY; 78870Sbill continue; 79870Sbill } 803513Sroot p->p_maxrss = INFINITY/NBPG; 8126Sbill clkstart(); 8226Sbill 8326Sbill /* 844821Swnj * Initialize tables, protocols, and set up well-known inodes. 8526Sbill */ 864821Swnj mbinit(); 876121Swnj cinit(); /* needed by dmc-11 driver */ 884821Swnj #ifdef INET 895855Swnj #if NLOOP > 0 905855Swnj loattach(); /* XXX */ 915855Swnj #endif 925227Swnj ifinit(); 936342Ssam pfinit(); /* must follow interfaces */ 944821Swnj #endif 9526Sbill ihinit(); 9692Sbill bhinit(); 9726Sbill binit(); 9826Sbill bswinit(); 99*7419Sroot #ifdef GPROF 100*7419Sroot kmstartup(); 101*7419Sroot #endif 1027189Sroot 1037189Sroot fs = mountfs(rootdev, 0, 0); 1047189Sroot if (fs == 0) 1057189Sroot panic("iinit"); 1067189Sroot bcopy("/", fs->fs_fsmnt, 2); 1077189Sroot clkinit(fs->fs_time); 1087189Sroot bootime = time; 1097189Sroot 1107189Sroot rootdir = iget(rootdev, fs, (ino_t)ROOTINO); 1117189Sroot iunlock(rootdir); 1127189Sroot u.u_cdir = iget(rootdev, fs, (ino_t)ROOTINO); 1137189Sroot iunlock(u.u_cdir); 1147189Sroot 11526Sbill u.u_rdir = NULL; 11626Sbill u.u_dmap = zdmap; 11726Sbill u.u_smap = zdmap; 11826Sbill 11926Sbill /* 1203591Swnj * Set the scan rate and other parameters of the paging subsystem. 1213591Swnj */ 1223591Swnj setupclock(); 1233591Swnj 1243591Swnj /* 12526Sbill * make page-out daemon (process 2) 1262753Swnj * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page 12726Sbill * table so that it can map dirty pages into 12826Sbill * its address space during asychronous pushes. 12926Sbill */ 13026Sbill mpid = 1; 1312753Swnj proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES)); 13226Sbill proc[1].p_stat = SZOMB; /* force it to be in proc slot 2 */ 13326Sbill if (newproc(0)) { 13426Sbill proc[2].p_flag |= SLOAD|SSYS; 1352753Swnj proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX; 13626Sbill pageout(); 13726Sbill } 13826Sbill 13926Sbill /* 14026Sbill * make init process and 14126Sbill * enter scheduling loop 14226Sbill */ 14326Sbill 14426Sbill mpid = 0; 14526Sbill proc[1].p_stat = 0; 14626Sbill proc[0].p_szpt = CLSIZE; 14726Sbill if (newproc(0)) { 14826Sbill expand(clrnd((int)btoc(szicode)), P0BR); 1491787Sbill (void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap); 150135Sbill (void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode); 15126Sbill /* 15226Sbill * Return goes to loc. 0 of user init 15326Sbill * code just copied out. 15426Sbill */ 15526Sbill return; 15626Sbill } 15726Sbill proc[0].p_szpt = 1; 15826Sbill sched(); 15926Sbill } 16026Sbill 16126Sbill /* 1627001Smckusick * Initialize hash links for buffers. 1637001Smckusick */ 1647001Smckusick bhinit() 1657001Smckusick { 1667001Smckusick register int i; 1677001Smckusick register struct bufhd *bp; 1687001Smckusick 1697001Smckusick for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++) 1707001Smckusick bp->b_forw = bp->b_back = (struct buf *)bp; 1717001Smckusick } 1727001Smckusick 1737001Smckusick /* 17426Sbill * Initialize the buffer I/O system by freeing 17526Sbill * all buffers and setting all device buffer lists to empty. 17626Sbill */ 17726Sbill binit() 17826Sbill { 17926Sbill register struct buf *bp; 18026Sbill register struct buf *dp; 18126Sbill register int i; 18226Sbill struct bdevsw *bdp; 183306Sbill struct swdevt *swp; 18426Sbill 1852322Swnj for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) { 1862322Swnj dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp; 1872322Swnj dp->b_flags = B_HEAD; 1882322Swnj } 1892322Swnj dp--; /* dp = &bfreelist[BQUEUES-1]; */ 1906570Smckusic for (i = 0; i < nbuf; i++) { 19126Sbill bp = &buf[i]; 19226Sbill bp->b_dev = NODEV; 1936570Smckusic bp->b_un.b_addr = buffers + i * MAXBSIZE; 1946570Smckusic bp->b_bcount = MAXBSIZE; 1952322Swnj bp->b_back = dp; 1962322Swnj bp->b_forw = dp->b_forw; 1972322Swnj dp->b_forw->b_back = bp; 1982322Swnj dp->b_forw = bp; 1992322Swnj bp->b_flags = B_BUSY|B_INVAL; 20026Sbill brelse(bp); 20126Sbill } 2022435Skre for (bdp = bdevsw; bdp->d_open; bdp++) 20326Sbill nblkdev++; 204306Sbill /* 205306Sbill * Count swap devices, and adjust total swap space available. 206306Sbill * Some of this space will not be available until a vswapon() 207306Sbill * system is issued, usually when the system goes multi-user. 208306Sbill */ 209306Sbill nswdev = 0; 210306Sbill for (swp = swdevt; swp->sw_dev; swp++) 211306Sbill nswdev++; 212306Sbill if (nswdev == 0) 213306Sbill panic("binit"); 2144132Secc if (nswdev > 1) 2154132Secc nswap = (nswap/DMMAX)*DMMAX; 216306Sbill nswap *= nswdev; 217306Sbill maxpgio *= nswdev; 218306Sbill swfree(0); 21926Sbill } 22026Sbill 22126Sbill /* 22226Sbill * Initialize linked list of free swap 22326Sbill * headers. These do not actually point 22426Sbill * to buffers, but rather to pages that 22526Sbill * are being swapped in and out. 22626Sbill */ 22726Sbill bswinit() 22826Sbill { 22926Sbill register int i; 2302769Swnj register struct buf *sp = swbuf; 23126Sbill 2322753Swnj bswlist.av_forw = sp; 2332769Swnj for (i=0; i<nswbuf-1; i++, sp++) 2342753Swnj sp->av_forw = sp+1; 2352753Swnj sp->av_forw = NULL; 23626Sbill } 2372746Swnj 2382746Swnj /* 2392746Swnj * Initialize clist by freeing all character blocks, then count 2402746Swnj * number of character devices. (Once-only routine) 2412746Swnj */ 2422746Swnj cinit() 2432746Swnj { 2442746Swnj register int ccp; 2452746Swnj register struct cblock *cp; 2462746Swnj register struct cdevsw *cdp; 2472746Swnj 2482746Swnj ccp = (int)cfree; 2492746Swnj ccp = (ccp+CROUND) & ~CROUND; 2502746Swnj for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) { 2512746Swnj cp->c_next = cfreelist; 2522746Swnj cfreelist = cp; 2532746Swnj cfreecount += CBSIZE; 2542746Swnj } 2552746Swnj ccp = 0; 2562746Swnj for(cdp = cdevsw; cdp->d_open; cdp++) 2572746Swnj ccp++; 2582746Swnj nchrdev = ccp; 2592746Swnj } 260