1*8969Sroot /* init_main.c 4.41 82/10/31 */ 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 * 41*8969Sroot * loop at loc something in user mode -- /etc/init 4226Sbill * cannot be executed. 4326Sbill */ 44*8969Sroot #ifdef vax 4526Sbill main(firstaddr) 46*8969Sroot int firstaddr; 47*8969Sroot #endif 48*8969Sroot #ifdef sun 49*8969Sroot main(regs) 50*8969Sroot struct regs regs; 51*8969Sroot #endif 5226Sbill { 53361Sbill register int i; 542451Swnj register struct proc *p; 557189Sroot struct fs *fs; 5626Sbill 5726Sbill rqinit(); 585227Swnj #include "loop.h" 59*8969Sroot #ifdef vax 6026Sbill startup(firstaddr); 61*8969Sroot #endif 62*8969Sroot #ifdef sun 63*8969Sroot startup(); 64*8969Sroot #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; 78*8969Sroot #ifdef sun 79*8969Sroot u.u_ar0 = ®s.r0; 80*8969Sroot #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 968027Sroot clockstart(); 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 109*8969Sroot 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 1238096Sroot /* initialize wall clock */ 1248027Sroot clockinit(fs->fs_time); 1258027Sroot boottime = time; 1267189Sroot 1278096Sroot /* kick off timeout driven events by calling first time */ 1288096Sroot roundrobin(); 1298096Sroot schedcpu(); 1308096Sroot schedpaging(); 1318096Sroot 1328096Sroot /* set up the root file system */ 1337189Sroot rootdir = iget(rootdev, fs, (ino_t)ROOTINO); 1347189Sroot iunlock(rootdir); 1357189Sroot u.u_cdir = iget(rootdev, fs, (ino_t)ROOTINO); 1367189Sroot iunlock(u.u_cdir); 1378096Sroot u.u_rdir = NULL; 1387189Sroot 13926Sbill u.u_dmap = zdmap; 14026Sbill u.u_smap = zdmap; 14126Sbill 14226Sbill /* 1433591Swnj * Set the scan rate and other parameters of the paging subsystem. 1443591Swnj */ 1453591Swnj setupclock(); 1463591Swnj 1473591Swnj /* 14826Sbill * make page-out daemon (process 2) 1492753Swnj * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page 15026Sbill * table so that it can map dirty pages into 15126Sbill * its address space during asychronous pushes. 15226Sbill */ 15326Sbill mpid = 1; 1542753Swnj proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES)); 15526Sbill proc[1].p_stat = SZOMB; /* force it to be in proc slot 2 */ 15626Sbill if (newproc(0)) { 15726Sbill proc[2].p_flag |= SLOAD|SSYS; 1582753Swnj proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX; 159*8969Sroot #ifdef NOPAGING 160*8969Sroot for (;;) 161*8969Sroot sleep((caddr_t)&u, PSLEP); 162*8969Sroot #else 16326Sbill pageout(); 164*8969Sroot #endif 165*8969Sroot /*NOTREACHED*/ 16626Sbill } 16726Sbill 16826Sbill /* 16926Sbill * make init process and 17026Sbill * enter scheduling loop 17126Sbill */ 17226Sbill 17326Sbill mpid = 0; 17426Sbill proc[1].p_stat = 0; 17526Sbill proc[0].p_szpt = CLSIZE; 17626Sbill if (newproc(0)) { 177*8969Sroot #ifdef vax 1788441Sroot expand(clrnd((int)btoc(szicode)), 0); 1791787Sbill (void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap); 180135Sbill (void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode); 181*8969Sroot #endif 182*8969Sroot #ifdef sun 183*8969Sroot icode(); 184*8969Sroot usetup(); 185*8969Sroot regs.r_context = u.u_pcb.pcb_ctx->ctx_context; 186*8969Sroot #endif 18726Sbill /* 18826Sbill * Return goes to loc. 0 of user init 18926Sbill * code just copied out. 19026Sbill */ 19126Sbill return; 19226Sbill } 1937486Skre #ifdef MUSH 1947486Skre /* 1957486Skre * start MUSH daemon 1967486Skre * pid == 3 1977486Skre */ 1987486Skre if (newproc(0)) { 1998441Sroot expand(clrnd((int)btoc(szmcode)), 0); 2007486Skre (void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap); 2017486Skre (void) copyout((caddr_t)mcode, (caddr_t)0, (unsigned)szmcode); 2027486Skre /* 2037486Skre * Return goes to loc. 0 of user mush 2047486Skre * code just copied out. 2057486Skre */ 2067486Skre return; 2077486Skre } 2087486Skre #endif 20926Sbill proc[0].p_szpt = 1; 21026Sbill sched(); 21126Sbill } 21226Sbill 21326Sbill /* 2147001Smckusick * Initialize hash links for buffers. 2157001Smckusick */ 2167001Smckusick bhinit() 2177001Smckusick { 2187001Smckusick register int i; 2197001Smckusick register struct bufhd *bp; 2207001Smckusick 2217001Smckusick for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++) 2227001Smckusick bp->b_forw = bp->b_back = (struct buf *)bp; 2237001Smckusick } 2247001Smckusick 2257001Smckusick /* 22626Sbill * Initialize the buffer I/O system by freeing 22726Sbill * all buffers and setting all device buffer lists to empty. 22826Sbill */ 22926Sbill binit() 23026Sbill { 2318554Sroot register struct buf *bp, *dp; 23226Sbill register int i; 233306Sbill struct swdevt *swp; 23426Sbill 2352322Swnj for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) { 2362322Swnj dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp; 2372322Swnj dp->b_flags = B_HEAD; 2382322Swnj } 2392322Swnj dp--; /* dp = &bfreelist[BQUEUES-1]; */ 2406570Smckusic for (i = 0; i < nbuf; i++) { 24126Sbill bp = &buf[i]; 24226Sbill bp->b_dev = NODEV; 2436570Smckusic bp->b_un.b_addr = buffers + i * MAXBSIZE; 2446570Smckusic bp->b_bcount = MAXBSIZE; 2452322Swnj bp->b_back = dp; 2462322Swnj bp->b_forw = dp->b_forw; 2472322Swnj dp->b_forw->b_back = bp; 2482322Swnj dp->b_forw = bp; 2492322Swnj bp->b_flags = B_BUSY|B_INVAL; 25026Sbill brelse(bp); 25126Sbill } 252306Sbill /* 253306Sbill * Count swap devices, and adjust total swap space available. 254306Sbill * Some of this space will not be available until a vswapon() 255306Sbill * system is issued, usually when the system goes multi-user. 256306Sbill */ 257306Sbill nswdev = 0; 258306Sbill for (swp = swdevt; swp->sw_dev; swp++) 259306Sbill nswdev++; 260306Sbill if (nswdev == 0) 261306Sbill panic("binit"); 2624132Secc if (nswdev > 1) 2634132Secc nswap = (nswap/DMMAX)*DMMAX; 264306Sbill nswap *= nswdev; 265306Sbill maxpgio *= nswdev; 266306Sbill swfree(0); 26726Sbill } 26826Sbill 26926Sbill /* 27026Sbill * Initialize linked list of free swap 27126Sbill * headers. These do not actually point 27226Sbill * to buffers, but rather to pages that 27326Sbill * are being swapped in and out. 27426Sbill */ 27526Sbill bswinit() 27626Sbill { 27726Sbill register int i; 2782769Swnj register struct buf *sp = swbuf; 27926Sbill 2802753Swnj bswlist.av_forw = sp; 2812769Swnj for (i=0; i<nswbuf-1; i++, sp++) 2822753Swnj sp->av_forw = sp+1; 2832753Swnj sp->av_forw = NULL; 28426Sbill } 2852746Swnj 2862746Swnj /* 2872746Swnj * Initialize clist by freeing all character blocks, then count 2882746Swnj * number of character devices. (Once-only routine) 2892746Swnj */ 2902746Swnj cinit() 2912746Swnj { 2922746Swnj register int ccp; 2932746Swnj register struct cblock *cp; 2942746Swnj 2952746Swnj ccp = (int)cfree; 2962746Swnj ccp = (ccp+CROUND) & ~CROUND; 2972746Swnj for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) { 2982746Swnj cp->c_next = cfreelist; 2992746Swnj cfreelist = cp; 3002746Swnj cfreecount += CBSIZE; 3012746Swnj } 3022746Swnj } 303