1*8096Sroot /* init_main.c 4.37 82/09/06 */ 226Sbill 326Sbill #include "../h/param.h" 426Sbill #include "../h/systm.h" 526Sbill #include "../h/dir.h" 626Sbill #include "../h/user.h" 78027Sroot #include "../h/kernel.h" 86570Smckusic #include "../h/fs.h" 926Sbill #include "../h/mount.h" 1026Sbill #include "../h/map.h" 1126Sbill #include "../h/proc.h" 1226Sbill #include "../h/inode.h" 1326Sbill #include "../h/seg.h" 1426Sbill #include "../h/conf.h" 1526Sbill #include "../h/buf.h" 1626Sbill #include "../h/mtpr.h" 1726Sbill #include "../h/pte.h" 1826Sbill #include "../h/vm.h" 1926Sbill #include "../h/cmap.h" 20348Sbill #include "../h/text.h" 212769Swnj #include "../h/clist.h" 224821Swnj #ifdef INET 234821Swnj #include "../h/protosw.h" 244821Swnj #endif 257486Skre #include "../h/quota.h" 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; 677864Sroot for (i = 1; i < NGROUPS; i++) 687864Sroot u.u_groups[i] = -1; 698027Sroot for (i = 0; i < sizeof(u.u_rlimit)/sizeof(u.u_rlimit[0]); i++) 708027Sroot u.u_rlimit[i].rlim_cur = u.u_rlimit[i].rlim_max = 718027Sroot RLIM_INFINITY; 728027Sroot u.u_rlimit[RLIMIT_STACK].rlim_cur = 512*1024; 738027Sroot u.u_rlimit[RLIMIT_DATA].rlim_cur = ctob(MAXDSIZ); 748027Sroot p->p_maxrss = RLIM_INFINITY/NBPG; 757486Skre #ifdef QUOTA 767486Skre qtinit(); 777486Skre p->p_quota = u.u_quota = getquota(0, 0, Q_NDQ); 787486Skre #endif 798027Sroot clockstart(); 8026Sbill 8126Sbill /* 824821Swnj * Initialize tables, protocols, and set up well-known inodes. 8326Sbill */ 844821Swnj mbinit(); 856121Swnj cinit(); /* needed by dmc-11 driver */ 864821Swnj #ifdef INET 875855Swnj #if NLOOP > 0 885855Swnj loattach(); /* XXX */ 895855Swnj #endif 905227Swnj ifinit(); 916342Ssam pfinit(); /* must follow interfaces */ 924821Swnj #endif 9326Sbill ihinit(); 9492Sbill bhinit(); 9526Sbill binit(); 9626Sbill bswinit(); 977419Sroot #ifdef GPROF 987419Sroot kmstartup(); 997419Sroot #endif 1007189Sroot 1017813Sroot fs = mountfs(rootdev, 0, (struct inode *)0); 1027189Sroot if (fs == 0) 1037189Sroot panic("iinit"); 1047189Sroot bcopy("/", fs->fs_fsmnt, 2); 105*8096Sroot 106*8096Sroot /* initialize wall clock */ 1078027Sroot clockinit(fs->fs_time); 1088027Sroot boottime = time; 1097189Sroot 110*8096Sroot /* kick off timeout driven events by calling first time */ 111*8096Sroot roundrobin(); 112*8096Sroot schedcpu(); 113*8096Sroot schedpaging(); 114*8096Sroot 115*8096Sroot /* set up the root file system */ 1167189Sroot rootdir = iget(rootdev, fs, (ino_t)ROOTINO); 1177189Sroot iunlock(rootdir); 1187189Sroot u.u_cdir = iget(rootdev, fs, (ino_t)ROOTINO); 1197189Sroot iunlock(u.u_cdir); 120*8096Sroot u.u_rdir = NULL; 1217189Sroot 12226Sbill u.u_dmap = zdmap; 12326Sbill u.u_smap = zdmap; 12426Sbill 12526Sbill /* 1263591Swnj * Set the scan rate and other parameters of the paging subsystem. 1273591Swnj */ 1283591Swnj setupclock(); 1293591Swnj 1303591Swnj /* 13126Sbill * make page-out daemon (process 2) 1322753Swnj * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page 13326Sbill * table so that it can map dirty pages into 13426Sbill * its address space during asychronous pushes. 13526Sbill */ 13626Sbill mpid = 1; 1372753Swnj proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES)); 13826Sbill proc[1].p_stat = SZOMB; /* force it to be in proc slot 2 */ 13926Sbill if (newproc(0)) { 14026Sbill proc[2].p_flag |= SLOAD|SSYS; 1412753Swnj proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX; 14226Sbill pageout(); 14326Sbill } 14426Sbill 14526Sbill /* 14626Sbill * make init process and 14726Sbill * enter scheduling loop 14826Sbill */ 14926Sbill 15026Sbill mpid = 0; 15126Sbill proc[1].p_stat = 0; 15226Sbill proc[0].p_szpt = CLSIZE; 15326Sbill if (newproc(0)) { 15426Sbill expand(clrnd((int)btoc(szicode)), P0BR); 1551787Sbill (void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap); 156135Sbill (void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode); 15726Sbill /* 15826Sbill * Return goes to loc. 0 of user init 15926Sbill * code just copied out. 16026Sbill */ 16126Sbill return; 16226Sbill } 1637486Skre #ifdef MUSH 1647486Skre /* 1657486Skre * start MUSH daemon 1667486Skre * pid == 3 1677486Skre */ 1687486Skre if (newproc(0)) { 1697486Skre expand(clrnd((int)btoc(szmcode)), P0BR); 1707486Skre (void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap); 1717486Skre (void) copyout((caddr_t)mcode, (caddr_t)0, (unsigned)szmcode); 1727486Skre /* 1737486Skre * Return goes to loc. 0 of user mush 1747486Skre * code just copied out. 1757486Skre */ 1767486Skre return; 1777486Skre } 1787486Skre #endif 17926Sbill proc[0].p_szpt = 1; 18026Sbill sched(); 18126Sbill } 18226Sbill 18326Sbill /* 1847001Smckusick * Initialize hash links for buffers. 1857001Smckusick */ 1867001Smckusick bhinit() 1877001Smckusick { 1887001Smckusick register int i; 1897001Smckusick register struct bufhd *bp; 1907001Smckusick 1917001Smckusick for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++) 1927001Smckusick bp->b_forw = bp->b_back = (struct buf *)bp; 1937001Smckusick } 1947001Smckusick 1957001Smckusick /* 19626Sbill * Initialize the buffer I/O system by freeing 19726Sbill * all buffers and setting all device buffer lists to empty. 19826Sbill */ 19926Sbill binit() 20026Sbill { 20126Sbill register struct buf *bp; 20226Sbill register struct buf *dp; 20326Sbill register int i; 20426Sbill struct bdevsw *bdp; 205306Sbill struct swdevt *swp; 20626Sbill 2072322Swnj for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) { 2082322Swnj dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp; 2092322Swnj dp->b_flags = B_HEAD; 2102322Swnj } 2112322Swnj dp--; /* dp = &bfreelist[BQUEUES-1]; */ 2126570Smckusic for (i = 0; i < nbuf; i++) { 21326Sbill bp = &buf[i]; 21426Sbill bp->b_dev = NODEV; 2156570Smckusic bp->b_un.b_addr = buffers + i * MAXBSIZE; 2166570Smckusic bp->b_bcount = MAXBSIZE; 2172322Swnj bp->b_back = dp; 2182322Swnj bp->b_forw = dp->b_forw; 2192322Swnj dp->b_forw->b_back = bp; 2202322Swnj dp->b_forw = bp; 2212322Swnj bp->b_flags = B_BUSY|B_INVAL; 22226Sbill brelse(bp); 22326Sbill } 2242435Skre for (bdp = bdevsw; bdp->d_open; bdp++) 22526Sbill nblkdev++; 226306Sbill /* 227306Sbill * Count swap devices, and adjust total swap space available. 228306Sbill * Some of this space will not be available until a vswapon() 229306Sbill * system is issued, usually when the system goes multi-user. 230306Sbill */ 231306Sbill nswdev = 0; 232306Sbill for (swp = swdevt; swp->sw_dev; swp++) 233306Sbill nswdev++; 234306Sbill if (nswdev == 0) 235306Sbill panic("binit"); 2364132Secc if (nswdev > 1) 2374132Secc nswap = (nswap/DMMAX)*DMMAX; 238306Sbill nswap *= nswdev; 239306Sbill maxpgio *= nswdev; 240306Sbill swfree(0); 24126Sbill } 24226Sbill 24326Sbill /* 24426Sbill * Initialize linked list of free swap 24526Sbill * headers. These do not actually point 24626Sbill * to buffers, but rather to pages that 24726Sbill * are being swapped in and out. 24826Sbill */ 24926Sbill bswinit() 25026Sbill { 25126Sbill register int i; 2522769Swnj register struct buf *sp = swbuf; 25326Sbill 2542753Swnj bswlist.av_forw = sp; 2552769Swnj for (i=0; i<nswbuf-1; i++, sp++) 2562753Swnj sp->av_forw = sp+1; 2572753Swnj sp->av_forw = NULL; 25826Sbill } 2592746Swnj 2602746Swnj /* 2612746Swnj * Initialize clist by freeing all character blocks, then count 2622746Swnj * number of character devices. (Once-only routine) 2632746Swnj */ 2642746Swnj cinit() 2652746Swnj { 2662746Swnj register int ccp; 2672746Swnj register struct cblock *cp; 2682746Swnj register struct cdevsw *cdp; 2692746Swnj 2702746Swnj ccp = (int)cfree; 2712746Swnj ccp = (ccp+CROUND) & ~CROUND; 2722746Swnj for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) { 2732746Swnj cp->c_next = cfreelist; 2742746Swnj cfreelist = cp; 2752746Swnj cfreecount += CBSIZE; 2762746Swnj } 2772746Swnj ccp = 0; 2782746Swnj for(cdp = cdevsw; cdp->d_open; cdp++) 2792746Swnj ccp++; 2802746Swnj nchrdev = ccp; 2812746Swnj } 282