1*12489Ssam /* init_main.c 4.50 83/05/18 */ 226Sbill 39750Ssam #include "../machine/pte.h" 49750Ssam 526Sbill #include "../h/param.h" 626Sbill #include "../h/systm.h" 726Sbill #include "../h/dir.h" 826Sbill #include "../h/user.h" 98027Sroot #include "../h/kernel.h" 106570Smckusic #include "../h/fs.h" 1126Sbill #include "../h/mount.h" 1226Sbill #include "../h/map.h" 1326Sbill #include "../h/proc.h" 1426Sbill #include "../h/inode.h" 1526Sbill #include "../h/seg.h" 1626Sbill #include "../h/conf.h" 1726Sbill #include "../h/buf.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" 2610610Ssam #include "../machine/reg.h" 2710610Ssam #include "../machine/cpu.h" 2826Sbill 298441Sroot extern struct user u; /* have to declare it somewhere! */ 3026Sbill /* 3126Sbill * Initialization code. 3226Sbill * Called from cold start routine as 3326Sbill * soon as a stack and segmentation 3426Sbill * have been established. 3526Sbill * Functions: 3626Sbill * clear and free user core 3726Sbill * turn on clock 3826Sbill * hand craft 0th process 3926Sbill * call all initialization routines 4026Sbill * fork - process 0 to schedule 4126Sbill * - process 2 to page out 4226Sbill * - process 1 execute bootstrap 4326Sbill * 449156Ssam * loop at loc 13 (0xd) in user mode -- /etc/init 4526Sbill * cannot be executed. 4626Sbill */ 478969Sroot #ifdef vax 4826Sbill main(firstaddr) 498969Sroot int firstaddr; 508969Sroot #endif 518969Sroot #ifdef sun 528969Sroot main(regs) 538969Sroot struct regs regs; 548969Sroot #endif 5526Sbill { 56361Sbill register int i; 572451Swnj register struct proc *p; 587189Sroot struct fs *fs; 599156Ssam int s; 6026Sbill 6126Sbill rqinit(); 625227Swnj #include "loop.h" 638969Sroot #ifdef vax 6426Sbill startup(firstaddr); 658969Sroot #endif 668969Sroot #ifdef sun 678969Sroot startup(); 688969Sroot #endif 6926Sbill 7026Sbill /* 7126Sbill * set up system process 0 (swapper) 7226Sbill */ 732451Swnj p = &proc[0]; 748441Sroot p->p_p0br = u.u_pcb.pcb_p0br; 752451Swnj p->p_szpt = 1; 762451Swnj p->p_addr = uaddr(p); 772451Swnj p->p_stat = SRUN; 782451Swnj p->p_flag |= SLOAD|SSYS; 792451Swnj p->p_nice = NZERO; 802451Swnj setredzone(p->p_addr, (caddr_t)&u); 812451Swnj u.u_procp = p; 828969Sroot #ifdef sun 839750Ssam u.u_ar0 = ®s.r_r0; 848969Sroot #endif 8526Sbill u.u_cmask = CMASK; 867864Sroot for (i = 1; i < NGROUPS; i++) 8711809Ssam u.u_groups[i] = NOGROUP; 888027Sroot for (i = 0; i < sizeof(u.u_rlimit)/sizeof(u.u_rlimit[0]); i++) 898027Sroot u.u_rlimit[i].rlim_cur = u.u_rlimit[i].rlim_max = 908027Sroot RLIM_INFINITY; 918027Sroot u.u_rlimit[RLIMIT_STACK].rlim_cur = 512*1024; 928153Sroot u.u_rlimit[RLIMIT_STACK].rlim_max = ctob(MAXDSIZ); 938153Sroot u.u_rlimit[RLIMIT_DATA].rlim_max = 948153Sroot u.u_rlimit[RLIMIT_DATA].rlim_cur = ctob(MAXDSIZ); 958027Sroot p->p_maxrss = RLIM_INFINITY/NBPG; 967486Skre #ifdef QUOTA 977486Skre qtinit(); 987486Skre p->p_quota = u.u_quota = getquota(0, 0, Q_NDQ); 997486Skre #endif 1009025Sroot startrtclock(); 10111356Smckusick #ifdef KGCLOCK 10211356Smckusick startkgclock(); 10311356Smckusick #endif 10426Sbill 10526Sbill /* 1064821Swnj * Initialize tables, protocols, and set up well-known inodes. 10726Sbill */ 1084821Swnj mbinit(); 1096121Swnj cinit(); /* needed by dmc-11 driver */ 1104821Swnj #ifdef INET 1115855Swnj #if NLOOP > 0 1125855Swnj loattach(); /* XXX */ 1135855Swnj #endif 1149156Ssam /* 1159156Ssam * Block reception of incoming packets 1169156Ssam * until protocols have been initialized. 1179156Ssam */ 1189156Ssam s = splimp(); 1195227Swnj ifinit(); 1204821Swnj #endif 1218969Sroot domaininit(); 1229156Ssam #ifdef INET 1239156Ssam splx(s); 1249156Ssam #endif 12526Sbill ihinit(); 12692Sbill bhinit(); 12726Sbill binit(); 12826Sbill bswinit(); 1297419Sroot #ifdef GPROF 1307419Sroot kmstartup(); 1317419Sroot #endif 1327189Sroot 1337813Sroot fs = mountfs(rootdev, 0, (struct inode *)0); 1347189Sroot if (fs == 0) 1357189Sroot panic("iinit"); 1367189Sroot bcopy("/", fs->fs_fsmnt, 2); 1378096Sroot 1389025Sroot inittodr(fs->fs_time); 1398027Sroot boottime = time; 1407189Sroot 1418096Sroot /* kick off timeout driven events by calling first time */ 1428096Sroot roundrobin(); 1438096Sroot schedcpu(); 1448096Sroot schedpaging(); 1458096Sroot 1468096Sroot /* set up the root file system */ 1477189Sroot rootdir = iget(rootdev, fs, (ino_t)ROOTINO); 1487189Sroot iunlock(rootdir); 1497189Sroot u.u_cdir = iget(rootdev, fs, (ino_t)ROOTINO); 1507189Sroot iunlock(u.u_cdir); 1518096Sroot u.u_rdir = NULL; 1527189Sroot 15326Sbill u.u_dmap = zdmap; 15426Sbill u.u_smap = zdmap; 15526Sbill 15626Sbill /* 1573591Swnj * Set the scan rate and other parameters of the paging subsystem. 1583591Swnj */ 1593591Swnj setupclock(); 1603591Swnj 1613591Swnj /* 16226Sbill * make page-out daemon (process 2) 1632753Swnj * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page 16426Sbill * table so that it can map dirty pages into 16526Sbill * its address space during asychronous pushes. 16626Sbill */ 16726Sbill mpid = 1; 1682753Swnj proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES)); 16926Sbill proc[1].p_stat = SZOMB; /* force it to be in proc slot 2 */ 17026Sbill if (newproc(0)) { 17126Sbill proc[2].p_flag |= SLOAD|SSYS; 1722753Swnj proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX; 17326Sbill pageout(); 1749025Sroot /*NOTREACHED*/ 17526Sbill } 17626Sbill 17726Sbill /* 17826Sbill * make init process and 17926Sbill * enter scheduling loop 18026Sbill */ 18126Sbill 18226Sbill mpid = 0; 18326Sbill proc[1].p_stat = 0; 18426Sbill proc[0].p_szpt = CLSIZE; 18526Sbill if (newproc(0)) { 1868969Sroot #ifdef vax 1878441Sroot expand(clrnd((int)btoc(szicode)), 0); 1881787Sbill (void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap); 189135Sbill (void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode); 1908969Sroot #endif 1918969Sroot #ifdef sun 1928969Sroot icode(); 1938969Sroot usetup(); 194*12489Ssam regs.r_context = u.u_procp->p_ctx->ctx_context; 1958969Sroot #endif 19626Sbill /* 19726Sbill * Return goes to loc. 0 of user init 19826Sbill * code just copied out. 19926Sbill */ 20026Sbill return; 20126Sbill } 2027486Skre #ifdef MUSH 2037486Skre /* 2047486Skre * start MUSH daemon 2057486Skre * pid == 3 2067486Skre */ 2077486Skre if (newproc(0)) { 2089750Ssam #ifdef vax 2098441Sroot expand(clrnd((int)btoc(szmcode)), 0); 2107486Skre (void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap); 2117486Skre (void) copyout((caddr_t)mcode, (caddr_t)0, (unsigned)szmcode); 2127486Skre /* 2137486Skre * Return goes to loc. 0 of user mush 2147486Skre * code just copied out. 2157486Skre */ 2167486Skre return; 2179750Ssam #endif 2187486Skre } 2197486Skre #endif 22026Sbill proc[0].p_szpt = 1; 22126Sbill sched(); 22226Sbill } 22326Sbill 22426Sbill /* 2257001Smckusick * Initialize hash links for buffers. 2267001Smckusick */ 2277001Smckusick bhinit() 2287001Smckusick { 2297001Smckusick register int i; 2307001Smckusick register struct bufhd *bp; 2317001Smckusick 2327001Smckusick for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++) 2337001Smckusick bp->b_forw = bp->b_back = (struct buf *)bp; 2347001Smckusick } 2357001Smckusick 2367001Smckusick /* 23726Sbill * Initialize the buffer I/O system by freeing 23826Sbill * all buffers and setting all device buffer lists to empty. 23926Sbill */ 24026Sbill binit() 24126Sbill { 2428554Sroot register struct buf *bp, *dp; 24326Sbill register int i; 244306Sbill struct swdevt *swp; 24510336Smckusick int base, residual; 24626Sbill 2472322Swnj for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) { 2482322Swnj dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp; 2492322Swnj dp->b_flags = B_HEAD; 2502322Swnj } 25110336Smckusick base = bufpages / nbuf; 25210336Smckusick residual = bufpages % nbuf; 2536570Smckusic for (i = 0; i < nbuf; i++) { 25426Sbill bp = &buf[i]; 25526Sbill bp->b_dev = NODEV; 2569156Ssam bp->b_bcount = 0; 2579750Ssam #ifndef sun 2586570Smckusic bp->b_un.b_addr = buffers + i * MAXBSIZE; 25910336Smckusick if (i < residual) 26010336Smckusick bp->b_bufsize = (base + 1) * CLBYTES; 26110336Smckusick else 26210336Smckusick bp->b_bufsize = base * CLBYTES; 2639750Ssam binshash(bp, &bfreelist[BQ_AGE]); 2649750Ssam #else 2659750Ssam bp->b_un.b_addr = (char *)0; 2669750Ssam bp->b_bufsize = 0; 2679750Ssam binshash(bp, &bfreelist[BQ_EMPTY]); 2689750Ssam #endif 2692322Swnj bp->b_flags = B_BUSY|B_INVAL; 27026Sbill brelse(bp); 27126Sbill } 272306Sbill /* 273306Sbill * Count swap devices, and adjust total swap space available. 274306Sbill * Some of this space will not be available until a vswapon() 275306Sbill * system is issued, usually when the system goes multi-user. 276306Sbill */ 277306Sbill nswdev = 0; 278*12489Ssam nswap = 0; 279*12489Ssam for (swp = swdevt; swp->sw_dev; swp++) { 280306Sbill nswdev++; 281*12489Ssam if (swp->sw_nblks > nswap) 282*12489Ssam nswap = swp->sw_nblks; 283*12489Ssam } 284306Sbill if (nswdev == 0) 285306Sbill panic("binit"); 2864132Secc if (nswdev > 1) 287*12489Ssam nswap = ((nswap + dmmax - 1) / dmmax) * dmmax; 288306Sbill nswap *= nswdev; 289306Sbill maxpgio *= nswdev; 290306Sbill swfree(0); 29126Sbill } 29226Sbill 29326Sbill /* 29426Sbill * Initialize linked list of free swap 29526Sbill * headers. These do not actually point 29626Sbill * to buffers, but rather to pages that 29726Sbill * are being swapped in and out. 29826Sbill */ 29926Sbill bswinit() 30026Sbill { 30126Sbill register int i; 3022769Swnj register struct buf *sp = swbuf; 30326Sbill 3042753Swnj bswlist.av_forw = sp; 3052769Swnj for (i=0; i<nswbuf-1; i++, sp++) 3062753Swnj sp->av_forw = sp+1; 3072753Swnj sp->av_forw = NULL; 30826Sbill } 3092746Swnj 3102746Swnj /* 3112746Swnj * Initialize clist by freeing all character blocks, then count 3122746Swnj * number of character devices. (Once-only routine) 3132746Swnj */ 3142746Swnj cinit() 3152746Swnj { 3162746Swnj register int ccp; 3172746Swnj register struct cblock *cp; 3182746Swnj 3192746Swnj ccp = (int)cfree; 3202746Swnj ccp = (ccp+CROUND) & ~CROUND; 3212746Swnj for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) { 3222746Swnj cp->c_next = cfreelist; 3232746Swnj cfreelist = cp; 3242746Swnj cfreecount += CBSIZE; 3252746Swnj } 3262746Swnj } 327