1*10336Smckusick /* init_main.c 4.45 83/01/16 */ 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" 2626Sbill 279750Ssam #ifdef sun 289750Ssam #include "../sun/reg.h" 299750Ssam #include "../sun/cpu.h" 309750Ssam #endif 319750Ssam 328441Sroot extern struct user u; /* have to declare it somewhere! */ 3326Sbill /* 3426Sbill * Initialization code. 3526Sbill * Called from cold start routine as 3626Sbill * soon as a stack and segmentation 3726Sbill * have been established. 3826Sbill * Functions: 3926Sbill * clear and free user core 4026Sbill * turn on clock 4126Sbill * hand craft 0th process 4226Sbill * call all initialization routines 4326Sbill * fork - process 0 to schedule 4426Sbill * - process 2 to page out 4526Sbill * - process 1 execute bootstrap 4626Sbill * 479156Ssam * loop at loc 13 (0xd) in user mode -- /etc/init 4826Sbill * cannot be executed. 4926Sbill */ 508969Sroot #ifdef vax 5126Sbill main(firstaddr) 528969Sroot int firstaddr; 538969Sroot #endif 548969Sroot #ifdef sun 558969Sroot main(regs) 568969Sroot struct regs regs; 578969Sroot #endif 5826Sbill { 59361Sbill register int i; 602451Swnj register struct proc *p; 617189Sroot struct fs *fs; 629156Ssam int s; 6326Sbill 6426Sbill rqinit(); 655227Swnj #include "loop.h" 668969Sroot #ifdef vax 6726Sbill startup(firstaddr); 688969Sroot #endif 698969Sroot #ifdef sun 708969Sroot startup(); 718969Sroot #endif 7226Sbill 7326Sbill /* 7426Sbill * set up system process 0 (swapper) 7526Sbill */ 762451Swnj p = &proc[0]; 778441Sroot p->p_p0br = u.u_pcb.pcb_p0br; 782451Swnj p->p_szpt = 1; 792451Swnj p->p_addr = uaddr(p); 802451Swnj p->p_stat = SRUN; 812451Swnj p->p_flag |= SLOAD|SSYS; 822451Swnj p->p_nice = NZERO; 832451Swnj setredzone(p->p_addr, (caddr_t)&u); 842451Swnj u.u_procp = p; 858969Sroot #ifdef sun 869750Ssam u.u_ar0 = ®s.r_r0; 878969Sroot #endif 8826Sbill u.u_cmask = CMASK; 897864Sroot for (i = 1; i < NGROUPS; i++) 907864Sroot u.u_groups[i] = -1; 918027Sroot for (i = 0; i < sizeof(u.u_rlimit)/sizeof(u.u_rlimit[0]); i++) 928027Sroot u.u_rlimit[i].rlim_cur = u.u_rlimit[i].rlim_max = 938027Sroot RLIM_INFINITY; 948027Sroot u.u_rlimit[RLIMIT_STACK].rlim_cur = 512*1024; 958153Sroot u.u_rlimit[RLIMIT_STACK].rlim_max = ctob(MAXDSIZ); 968153Sroot u.u_rlimit[RLIMIT_DATA].rlim_max = 978153Sroot u.u_rlimit[RLIMIT_DATA].rlim_cur = ctob(MAXDSIZ); 988027Sroot p->p_maxrss = RLIM_INFINITY/NBPG; 997486Skre #ifdef QUOTA 1007486Skre qtinit(); 1017486Skre p->p_quota = u.u_quota = getquota(0, 0, Q_NDQ); 1027486Skre #endif 1039025Sroot startrtclock(); 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; 1738969Sroot #ifdef NOPAGING 1748969Sroot for (;;) 1758969Sroot sleep((caddr_t)&u, PSLEP); 1769750Ssam /*NOTREACHED*/ 1778969Sroot #else 17826Sbill pageout(); 1799025Sroot /*NOTREACHED*/ 1808969Sroot #endif 18126Sbill } 18226Sbill 18326Sbill /* 18426Sbill * make init process and 18526Sbill * enter scheduling loop 18626Sbill */ 18726Sbill 18826Sbill mpid = 0; 18926Sbill proc[1].p_stat = 0; 19026Sbill proc[0].p_szpt = CLSIZE; 19126Sbill if (newproc(0)) { 1928969Sroot #ifdef vax 1938441Sroot expand(clrnd((int)btoc(szicode)), 0); 1941787Sbill (void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap); 195135Sbill (void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode); 1968969Sroot #endif 1978969Sroot #ifdef sun 1988969Sroot icode(); 1998969Sroot usetup(); 2008969Sroot regs.r_context = u.u_pcb.pcb_ctx->ctx_context; 2018969Sroot #endif 20226Sbill /* 20326Sbill * Return goes to loc. 0 of user init 20426Sbill * code just copied out. 20526Sbill */ 20626Sbill return; 20726Sbill } 2087486Skre #ifdef MUSH 2097486Skre /* 2107486Skre * start MUSH daemon 2117486Skre * pid == 3 2127486Skre */ 2137486Skre if (newproc(0)) { 2149750Ssam #ifdef vax 2158441Sroot expand(clrnd((int)btoc(szmcode)), 0); 2167486Skre (void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap); 2177486Skre (void) copyout((caddr_t)mcode, (caddr_t)0, (unsigned)szmcode); 2187486Skre /* 2197486Skre * Return goes to loc. 0 of user mush 2207486Skre * code just copied out. 2217486Skre */ 2227486Skre return; 2239750Ssam #endif 2247486Skre } 2257486Skre #endif 22626Sbill proc[0].p_szpt = 1; 22726Sbill sched(); 22826Sbill } 22926Sbill 23026Sbill /* 2317001Smckusick * Initialize hash links for buffers. 2327001Smckusick */ 2337001Smckusick bhinit() 2347001Smckusick { 2357001Smckusick register int i; 2367001Smckusick register struct bufhd *bp; 2377001Smckusick 2387001Smckusick for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++) 2397001Smckusick bp->b_forw = bp->b_back = (struct buf *)bp; 2407001Smckusick } 2417001Smckusick 2427001Smckusick /* 24326Sbill * Initialize the buffer I/O system by freeing 24426Sbill * all buffers and setting all device buffer lists to empty. 24526Sbill */ 24626Sbill binit() 24726Sbill { 2488554Sroot register struct buf *bp, *dp; 24926Sbill register int i; 250306Sbill struct swdevt *swp; 251*10336Smckusick int base, residual; 25226Sbill 2532322Swnj for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) { 2542322Swnj dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp; 2552322Swnj dp->b_flags = B_HEAD; 2562322Swnj } 257*10336Smckusick base = bufpages / nbuf; 258*10336Smckusick residual = bufpages % nbuf; 2596570Smckusic for (i = 0; i < nbuf; i++) { 26026Sbill bp = &buf[i]; 26126Sbill bp->b_dev = NODEV; 2629156Ssam bp->b_bcount = 0; 2639750Ssam #ifndef sun 2646570Smckusic bp->b_un.b_addr = buffers + i * MAXBSIZE; 265*10336Smckusick if (i < residual) 266*10336Smckusick bp->b_bufsize = (base + 1) * CLBYTES; 267*10336Smckusick else 268*10336Smckusick bp->b_bufsize = base * CLBYTES; 2699750Ssam binshash(bp, &bfreelist[BQ_AGE]); 2709750Ssam #else 2719750Ssam bp->b_un.b_addr = (char *)0; 2729750Ssam bp->b_bufsize = 0; 2739750Ssam binshash(bp, &bfreelist[BQ_EMPTY]); 2749750Ssam #endif 2752322Swnj bp->b_flags = B_BUSY|B_INVAL; 27626Sbill brelse(bp); 27726Sbill } 278306Sbill /* 279306Sbill * Count swap devices, and adjust total swap space available. 280306Sbill * Some of this space will not be available until a vswapon() 281306Sbill * system is issued, usually when the system goes multi-user. 282306Sbill */ 283306Sbill nswdev = 0; 284306Sbill for (swp = swdevt; swp->sw_dev; swp++) 285306Sbill nswdev++; 286306Sbill if (nswdev == 0) 287306Sbill panic("binit"); 2884132Secc if (nswdev > 1) 2894132Secc nswap = (nswap/DMMAX)*DMMAX; 290306Sbill nswap *= nswdev; 291306Sbill maxpgio *= nswdev; 292306Sbill swfree(0); 29326Sbill } 29426Sbill 29526Sbill /* 29626Sbill * Initialize linked list of free swap 29726Sbill * headers. These do not actually point 29826Sbill * to buffers, but rather to pages that 29926Sbill * are being swapped in and out. 30026Sbill */ 30126Sbill bswinit() 30226Sbill { 30326Sbill register int i; 3042769Swnj register struct buf *sp = swbuf; 30526Sbill 3062753Swnj bswlist.av_forw = sp; 3072769Swnj for (i=0; i<nswbuf-1; i++, sp++) 3082753Swnj sp->av_forw = sp+1; 3092753Swnj sp->av_forw = NULL; 31026Sbill } 3112746Swnj 3122746Swnj /* 3132746Swnj * Initialize clist by freeing all character blocks, then count 3142746Swnj * number of character devices. (Once-only routine) 3152746Swnj */ 3162746Swnj cinit() 3172746Swnj { 3182746Swnj register int ccp; 3192746Swnj register struct cblock *cp; 3202746Swnj 3212746Swnj ccp = (int)cfree; 3222746Swnj ccp = (ccp+CROUND) & ~CROUND; 3232746Swnj for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) { 3242746Swnj cp->c_next = cfreelist; 3252746Swnj cfreelist = cp; 3262746Swnj cfreecount += CBSIZE; 3272746Swnj } 3282746Swnj } 329