1*9025Sroot /* init_main.c 4.42 82/11/02 */ 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 * 418969Sroot * loop at loc something in user mode -- /etc/init 4226Sbill * cannot be executed. 4326Sbill */ 448969Sroot #ifdef vax 4526Sbill main(firstaddr) 468969Sroot int firstaddr; 478969Sroot #endif 488969Sroot #ifdef sun 498969Sroot main(regs) 508969Sroot struct regs regs; 518969Sroot #endif 5226Sbill { 53361Sbill register int i; 542451Swnj register struct proc *p; 557189Sroot struct fs *fs; 5626Sbill 5726Sbill rqinit(); 585227Swnj #include "loop.h" 598969Sroot #ifdef vax 6026Sbill startup(firstaddr); 618969Sroot #endif 628969Sroot #ifdef sun 638969Sroot startup(); 648969Sroot #endif 6526Sbill 6626Sbill /* 6726Sbill * set up system process 0 (swapper) 6826Sbill */ 692451Swnj p = &proc[0]; 708441Sroot p->p_p0br = u.u_pcb.pcb_p0br; 712451Swnj p->p_szpt = 1; 722451Swnj p->p_addr = uaddr(p); 732451Swnj p->p_stat = SRUN; 742451Swnj p->p_flag |= SLOAD|SSYS; 752451Swnj p->p_nice = NZERO; 762451Swnj setredzone(p->p_addr, (caddr_t)&u); 772451Swnj u.u_procp = p; 788969Sroot #ifdef sun 798969Sroot u.u_ar0 = ®s.r0; 808969Sroot #endif 8126Sbill u.u_cmask = CMASK; 827864Sroot for (i = 1; i < NGROUPS; i++) 837864Sroot u.u_groups[i] = -1; 848027Sroot for (i = 0; i < sizeof(u.u_rlimit)/sizeof(u.u_rlimit[0]); i++) 858027Sroot u.u_rlimit[i].rlim_cur = u.u_rlimit[i].rlim_max = 868027Sroot RLIM_INFINITY; 878027Sroot u.u_rlimit[RLIMIT_STACK].rlim_cur = 512*1024; 888153Sroot u.u_rlimit[RLIMIT_STACK].rlim_max = ctob(MAXDSIZ); 898153Sroot u.u_rlimit[RLIMIT_DATA].rlim_max = 908153Sroot u.u_rlimit[RLIMIT_DATA].rlim_cur = ctob(MAXDSIZ); 918027Sroot p->p_maxrss = RLIM_INFINITY/NBPG; 927486Skre #ifdef QUOTA 937486Skre qtinit(); 947486Skre p->p_quota = u.u_quota = getquota(0, 0, Q_NDQ); 957486Skre #endif 96*9025Sroot startrtclock(); 9726Sbill 9826Sbill /* 994821Swnj * Initialize tables, protocols, and set up well-known inodes. 10026Sbill */ 1014821Swnj mbinit(); 1026121Swnj cinit(); /* needed by dmc-11 driver */ 1034821Swnj #ifdef INET 1045855Swnj #if NLOOP > 0 1055855Swnj loattach(); /* XXX */ 1065855Swnj #endif 1075227Swnj ifinit(); 1084821Swnj #endif 1098969Sroot domaininit(); 11026Sbill ihinit(); 11192Sbill bhinit(); 11226Sbill binit(); 11326Sbill bswinit(); 1147419Sroot #ifdef GPROF 1157419Sroot kmstartup(); 1167419Sroot #endif 1177189Sroot 1187813Sroot fs = mountfs(rootdev, 0, (struct inode *)0); 1197189Sroot if (fs == 0) 1207189Sroot panic("iinit"); 1217189Sroot bcopy("/", fs->fs_fsmnt, 2); 1228096Sroot 123*9025Sroot inittodr(fs->fs_time); 1248027Sroot boottime = time; 1257189Sroot 1268096Sroot /* kick off timeout driven events by calling first time */ 1278096Sroot roundrobin(); 1288096Sroot schedcpu(); 1298096Sroot schedpaging(); 1308096Sroot 1318096Sroot /* set up the root file system */ 1327189Sroot rootdir = iget(rootdev, fs, (ino_t)ROOTINO); 1337189Sroot iunlock(rootdir); 1347189Sroot u.u_cdir = iget(rootdev, fs, (ino_t)ROOTINO); 1357189Sroot iunlock(u.u_cdir); 1368096Sroot u.u_rdir = NULL; 1377189Sroot 13826Sbill u.u_dmap = zdmap; 13926Sbill u.u_smap = zdmap; 14026Sbill 14126Sbill /* 1423591Swnj * Set the scan rate and other parameters of the paging subsystem. 1433591Swnj */ 1443591Swnj setupclock(); 1453591Swnj 1463591Swnj /* 14726Sbill * make page-out daemon (process 2) 1482753Swnj * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page 14926Sbill * table so that it can map dirty pages into 15026Sbill * its address space during asychronous pushes. 15126Sbill */ 15226Sbill mpid = 1; 1532753Swnj proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES)); 15426Sbill proc[1].p_stat = SZOMB; /* force it to be in proc slot 2 */ 15526Sbill if (newproc(0)) { 15626Sbill proc[2].p_flag |= SLOAD|SSYS; 1572753Swnj proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX; 1588969Sroot #ifdef NOPAGING 1598969Sroot for (;;) 1608969Sroot sleep((caddr_t)&u, PSLEP); 1618969Sroot #else 16226Sbill pageout(); 163*9025Sroot /*NOTREACHED*/ 1648969Sroot #endif 16526Sbill } 16626Sbill 16726Sbill /* 16826Sbill * make init process and 16926Sbill * enter scheduling loop 17026Sbill */ 17126Sbill 17226Sbill mpid = 0; 17326Sbill proc[1].p_stat = 0; 17426Sbill proc[0].p_szpt = CLSIZE; 17526Sbill if (newproc(0)) { 1768969Sroot #ifdef vax 1778441Sroot expand(clrnd((int)btoc(szicode)), 0); 1781787Sbill (void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap); 179135Sbill (void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode); 1808969Sroot #endif 1818969Sroot #ifdef sun 1828969Sroot icode(); 1838969Sroot usetup(); 1848969Sroot regs.r_context = u.u_pcb.pcb_ctx->ctx_context; 1858969Sroot #endif 18626Sbill /* 18726Sbill * Return goes to loc. 0 of user init 18826Sbill * code just copied out. 18926Sbill */ 19026Sbill return; 19126Sbill } 1927486Skre #ifdef MUSH 1937486Skre /* 1947486Skre * start MUSH daemon 1957486Skre * pid == 3 1967486Skre */ 1977486Skre if (newproc(0)) { 1988441Sroot expand(clrnd((int)btoc(szmcode)), 0); 1997486Skre (void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap); 2007486Skre (void) copyout((caddr_t)mcode, (caddr_t)0, (unsigned)szmcode); 2017486Skre /* 2027486Skre * Return goes to loc. 0 of user mush 2037486Skre * code just copied out. 2047486Skre */ 2057486Skre return; 2067486Skre } 2077486Skre #endif 20826Sbill proc[0].p_szpt = 1; 20926Sbill sched(); 21026Sbill } 21126Sbill 21226Sbill /* 2137001Smckusick * Initialize hash links for buffers. 2147001Smckusick */ 2157001Smckusick bhinit() 2167001Smckusick { 2177001Smckusick register int i; 2187001Smckusick register struct bufhd *bp; 2197001Smckusick 2207001Smckusick for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++) 2217001Smckusick bp->b_forw = bp->b_back = (struct buf *)bp; 2227001Smckusick } 2237001Smckusick 2247001Smckusick /* 22526Sbill * Initialize the buffer I/O system by freeing 22626Sbill * all buffers and setting all device buffer lists to empty. 22726Sbill */ 22826Sbill binit() 22926Sbill { 2308554Sroot register struct buf *bp, *dp; 23126Sbill register int i; 232306Sbill struct swdevt *swp; 23326Sbill 2342322Swnj for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) { 2352322Swnj dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp; 2362322Swnj dp->b_flags = B_HEAD; 2372322Swnj } 2382322Swnj dp--; /* dp = &bfreelist[BQUEUES-1]; */ 2396570Smckusic for (i = 0; i < nbuf; i++) { 24026Sbill bp = &buf[i]; 24126Sbill bp->b_dev = NODEV; 2426570Smckusic bp->b_un.b_addr = buffers + i * MAXBSIZE; 2436570Smckusic bp->b_bcount = MAXBSIZE; 2442322Swnj bp->b_back = dp; 2452322Swnj bp->b_forw = dp->b_forw; 2462322Swnj dp->b_forw->b_back = bp; 2472322Swnj dp->b_forw = bp; 2482322Swnj bp->b_flags = B_BUSY|B_INVAL; 24926Sbill brelse(bp); 25026Sbill } 251306Sbill /* 252306Sbill * Count swap devices, and adjust total swap space available. 253306Sbill * Some of this space will not be available until a vswapon() 254306Sbill * system is issued, usually when the system goes multi-user. 255306Sbill */ 256306Sbill nswdev = 0; 257306Sbill for (swp = swdevt; swp->sw_dev; swp++) 258306Sbill nswdev++; 259306Sbill if (nswdev == 0) 260306Sbill panic("binit"); 2614132Secc if (nswdev > 1) 2624132Secc nswap = (nswap/DMMAX)*DMMAX; 263306Sbill nswap *= nswdev; 264306Sbill maxpgio *= nswdev; 265306Sbill swfree(0); 26626Sbill } 26726Sbill 26826Sbill /* 26926Sbill * Initialize linked list of free swap 27026Sbill * headers. These do not actually point 27126Sbill * to buffers, but rather to pages that 27226Sbill * are being swapped in and out. 27326Sbill */ 27426Sbill bswinit() 27526Sbill { 27626Sbill register int i; 2772769Swnj register struct buf *sp = swbuf; 27826Sbill 2792753Swnj bswlist.av_forw = sp; 2802769Swnj for (i=0; i<nswbuf-1; i++, sp++) 2812753Swnj sp->av_forw = sp+1; 2822753Swnj sp->av_forw = NULL; 28326Sbill } 2842746Swnj 2852746Swnj /* 2862746Swnj * Initialize clist by freeing all character blocks, then count 2872746Swnj * number of character devices. (Once-only routine) 2882746Swnj */ 2892746Swnj cinit() 2902746Swnj { 2912746Swnj register int ccp; 2922746Swnj register struct cblock *cp; 2932746Swnj 2942746Swnj ccp = (int)cfree; 2952746Swnj ccp = (ccp+CROUND) & ~CROUND; 2962746Swnj for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) { 2972746Swnj cp->c_next = cfreelist; 2982746Swnj cfreelist = cp; 2992746Swnj cfreecount += CBSIZE; 3002746Swnj } 3012746Swnj } 302