1*8554Sroot /* init_main.c 4.40 82/10/17 */ 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/pte.h" 1726Sbill #include "../h/vm.h" 1826Sbill #include "../h/cmap.h" 19348Sbill #include "../h/text.h" 202769Swnj #include "../h/clist.h" 214821Swnj #ifdef INET 224821Swnj #include "../h/protosw.h" 234821Swnj #endif 247486Skre #include "../h/quota.h" 2526Sbill 268441Sroot extern struct user u; /* have to declare it somewhere! */ 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]; 588441Sroot p->p_p0br = u.u_pcb.pcb_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; 738153Sroot u.u_rlimit[RLIMIT_STACK].rlim_max = ctob(MAXDSIZ); 748153Sroot u.u_rlimit[RLIMIT_DATA].rlim_max = 758153Sroot u.u_rlimit[RLIMIT_DATA].rlim_cur = ctob(MAXDSIZ); 768027Sroot p->p_maxrss = RLIM_INFINITY/NBPG; 777486Skre #ifdef QUOTA 787486Skre qtinit(); 797486Skre p->p_quota = u.u_quota = getquota(0, 0, Q_NDQ); 807486Skre #endif 818027Sroot clockstart(); 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(); 997419Sroot #ifdef GPROF 1007419Sroot kmstartup(); 1017419Sroot #endif 1027189Sroot 1037813Sroot fs = mountfs(rootdev, 0, (struct inode *)0); 1047189Sroot if (fs == 0) 1057189Sroot panic("iinit"); 1067189Sroot bcopy("/", fs->fs_fsmnt, 2); 1078096Sroot 1088096Sroot /* initialize wall clock */ 1098027Sroot clockinit(fs->fs_time); 1108027Sroot boottime = time; 1117189Sroot 1128096Sroot /* kick off timeout driven events by calling first time */ 1138096Sroot roundrobin(); 1148096Sroot schedcpu(); 1158096Sroot schedpaging(); 1168096Sroot 1178096Sroot /* set up the root file system */ 1187189Sroot rootdir = iget(rootdev, fs, (ino_t)ROOTINO); 1197189Sroot iunlock(rootdir); 1207189Sroot u.u_cdir = iget(rootdev, fs, (ino_t)ROOTINO); 1217189Sroot iunlock(u.u_cdir); 1228096Sroot u.u_rdir = NULL; 1237189Sroot 12426Sbill u.u_dmap = zdmap; 12526Sbill u.u_smap = zdmap; 12626Sbill 12726Sbill /* 1283591Swnj * Set the scan rate and other parameters of the paging subsystem. 1293591Swnj */ 1303591Swnj setupclock(); 1313591Swnj 1323591Swnj /* 13326Sbill * make page-out daemon (process 2) 1342753Swnj * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page 13526Sbill * table so that it can map dirty pages into 13626Sbill * its address space during asychronous pushes. 13726Sbill */ 13826Sbill mpid = 1; 1392753Swnj proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES)); 14026Sbill proc[1].p_stat = SZOMB; /* force it to be in proc slot 2 */ 14126Sbill if (newproc(0)) { 14226Sbill proc[2].p_flag |= SLOAD|SSYS; 1432753Swnj proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX; 14426Sbill pageout(); 14526Sbill } 14626Sbill 14726Sbill /* 14826Sbill * make init process and 14926Sbill * enter scheduling loop 15026Sbill */ 15126Sbill 15226Sbill mpid = 0; 15326Sbill proc[1].p_stat = 0; 15426Sbill proc[0].p_szpt = CLSIZE; 15526Sbill if (newproc(0)) { 1568441Sroot expand(clrnd((int)btoc(szicode)), 0); 1571787Sbill (void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap); 158135Sbill (void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode); 15926Sbill /* 16026Sbill * Return goes to loc. 0 of user init 16126Sbill * code just copied out. 16226Sbill */ 16326Sbill return; 16426Sbill } 1657486Skre #ifdef MUSH 1667486Skre /* 1677486Skre * start MUSH daemon 1687486Skre * pid == 3 1697486Skre */ 1707486Skre if (newproc(0)) { 1718441Sroot expand(clrnd((int)btoc(szmcode)), 0); 1727486Skre (void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap); 1737486Skre (void) copyout((caddr_t)mcode, (caddr_t)0, (unsigned)szmcode); 1747486Skre /* 1757486Skre * Return goes to loc. 0 of user mush 1767486Skre * code just copied out. 1777486Skre */ 1787486Skre return; 1797486Skre } 1807486Skre #endif 18126Sbill proc[0].p_szpt = 1; 18226Sbill sched(); 18326Sbill } 18426Sbill 18526Sbill /* 1867001Smckusick * Initialize hash links for buffers. 1877001Smckusick */ 1887001Smckusick bhinit() 1897001Smckusick { 1907001Smckusick register int i; 1917001Smckusick register struct bufhd *bp; 1927001Smckusick 1937001Smckusick for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++) 1947001Smckusick bp->b_forw = bp->b_back = (struct buf *)bp; 1957001Smckusick } 1967001Smckusick 1977001Smckusick /* 19826Sbill * Initialize the buffer I/O system by freeing 19926Sbill * all buffers and setting all device buffer lists to empty. 20026Sbill */ 20126Sbill binit() 20226Sbill { 203*8554Sroot register struct buf *bp, *dp; 20426Sbill register int i; 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 } 224306Sbill /* 225306Sbill * Count swap devices, and adjust total swap space available. 226306Sbill * Some of this space will not be available until a vswapon() 227306Sbill * system is issued, usually when the system goes multi-user. 228306Sbill */ 229306Sbill nswdev = 0; 230306Sbill for (swp = swdevt; swp->sw_dev; swp++) 231306Sbill nswdev++; 232306Sbill if (nswdev == 0) 233306Sbill panic("binit"); 2344132Secc if (nswdev > 1) 2354132Secc nswap = (nswap/DMMAX)*DMMAX; 236306Sbill nswap *= nswdev; 237306Sbill maxpgio *= nswdev; 238306Sbill swfree(0); 23926Sbill } 24026Sbill 24126Sbill /* 24226Sbill * Initialize linked list of free swap 24326Sbill * headers. These do not actually point 24426Sbill * to buffers, but rather to pages that 24526Sbill * are being swapped in and out. 24626Sbill */ 24726Sbill bswinit() 24826Sbill { 24926Sbill register int i; 2502769Swnj register struct buf *sp = swbuf; 25126Sbill 2522753Swnj bswlist.av_forw = sp; 2532769Swnj for (i=0; i<nswbuf-1; i++, sp++) 2542753Swnj sp->av_forw = sp+1; 2552753Swnj sp->av_forw = NULL; 25626Sbill } 2572746Swnj 2582746Swnj /* 2592746Swnj * Initialize clist by freeing all character blocks, then count 2602746Swnj * number of character devices. (Once-only routine) 2612746Swnj */ 2622746Swnj cinit() 2632746Swnj { 2642746Swnj register int ccp; 2652746Swnj register struct cblock *cp; 2662746Swnj 2672746Swnj ccp = (int)cfree; 2682746Swnj ccp = (ccp+CROUND) & ~CROUND; 2692746Swnj for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) { 2702746Swnj cp->c_next = cfreelist; 2712746Swnj cfreelist = cp; 2722746Swnj cfreecount += CBSIZE; 2732746Swnj } 2742746Swnj } 275