1*16807Skarels /* init_main.c 6.5 84/08/01 */ 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 29*16807Skarels int cmask = CMASK; 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 */ 4726Sbill main(firstaddr) 488969Sroot int firstaddr; 4926Sbill { 50361Sbill register int i; 512451Swnj register struct proc *p; 527189Sroot struct fs *fs; 539156Ssam int s; 5426Sbill 5526Sbill rqinit(); 565227Swnj #include "loop.h" 5726Sbill startup(firstaddr); 5826Sbill 5926Sbill /* 6026Sbill * set up system process 0 (swapper) 6126Sbill */ 622451Swnj p = &proc[0]; 638441Sroot p->p_p0br = u.u_pcb.pcb_p0br; 642451Swnj p->p_szpt = 1; 652451Swnj p->p_addr = uaddr(p); 662451Swnj p->p_stat = SRUN; 672451Swnj p->p_flag |= SLOAD|SSYS; 682451Swnj p->p_nice = NZERO; 692451Swnj setredzone(p->p_addr, (caddr_t)&u); 702451Swnj u.u_procp = p; 7116690Smckusick #ifdef vax 7216690Smckusick /* 7316690Smckusick * This assumes that the u. area is always mapped 7416690Smckusick * to the same physical address. Otherwise must be 7516690Smckusick * handled when copying the u. area in newproc(). 7616690Smckusick */ 7716690Smckusick u.u_nd.ni_iov = &u.u_nd.ni_iovec; 7816690Smckusick #endif 7916690Smckusick u.u_nd.ni_iovcnt = 1; 80*16807Skarels u.u_cmask = cmask; 817864Sroot for (i = 1; i < NGROUPS; i++) 8211809Ssam u.u_groups[i] = NOGROUP; 838027Sroot for (i = 0; i < sizeof(u.u_rlimit)/sizeof(u.u_rlimit[0]); i++) 848027Sroot u.u_rlimit[i].rlim_cur = u.u_rlimit[i].rlim_max = 858027Sroot RLIM_INFINITY; 868027Sroot u.u_rlimit[RLIMIT_STACK].rlim_cur = 512*1024; 8716526Skarels u.u_rlimit[RLIMIT_STACK].rlim_max = ctob(MAXSSIZ); 888153Sroot u.u_rlimit[RLIMIT_DATA].rlim_max = 898153Sroot u.u_rlimit[RLIMIT_DATA].rlim_cur = ctob(MAXDSIZ); 908027Sroot p->p_maxrss = RLIM_INFINITY/NBPG; 9116526Skarels #if defined(QUOTA) 927486Skre qtinit(); 937486Skre p->p_quota = u.u_quota = getquota(0, 0, Q_NDQ); 947486Skre #endif 959025Sroot startrtclock(); 9612823Ssam #include "kg.h" 9712823Ssam #if NKG > 0 9811356Smckusick startkgclock(); 9911356Smckusick #endif 10026Sbill 10126Sbill /* 1024821Swnj * Initialize tables, protocols, and set up well-known inodes. 10326Sbill */ 1044821Swnj mbinit(); 1056121Swnj cinit(); /* needed by dmc-11 driver */ 1064821Swnj #ifdef INET 1075855Swnj #if NLOOP > 0 1085855Swnj loattach(); /* XXX */ 1095855Swnj #endif 1109156Ssam /* 1119156Ssam * Block reception of incoming packets 1129156Ssam * until protocols have been initialized. 1139156Ssam */ 1149156Ssam s = splimp(); 1155227Swnj ifinit(); 1164821Swnj #endif 1178969Sroot domaininit(); 1189156Ssam #ifdef INET 1199156Ssam splx(s); 1209156Ssam #endif 12116526Skarels pqinit(); 12226Sbill ihinit(); 12392Sbill bhinit(); 12426Sbill binit(); 12526Sbill bswinit(); 12615799Smckusick nchinit(); 1277419Sroot #ifdef GPROF 1287419Sroot kmstartup(); 1297419Sroot #endif 1307189Sroot 1317813Sroot fs = mountfs(rootdev, 0, (struct inode *)0); 1327189Sroot if (fs == 0) 1337189Sroot panic("iinit"); 1347189Sroot bcopy("/", fs->fs_fsmnt, 2); 1358096Sroot 1369025Sroot inittodr(fs->fs_time); 1378027Sroot boottime = time; 1387189Sroot 1398096Sroot /* kick off timeout driven events by calling first time */ 1408096Sroot roundrobin(); 1418096Sroot schedcpu(); 1428096Sroot schedpaging(); 1438096Sroot 1448096Sroot /* set up the root file system */ 1457189Sroot rootdir = iget(rootdev, fs, (ino_t)ROOTINO); 1467189Sroot iunlock(rootdir); 1477189Sroot u.u_cdir = iget(rootdev, fs, (ino_t)ROOTINO); 1487189Sroot iunlock(u.u_cdir); 1498096Sroot u.u_rdir = NULL; 1507189Sroot 15126Sbill u.u_dmap = zdmap; 15226Sbill u.u_smap = zdmap; 15326Sbill 15426Sbill /* 1553591Swnj * Set the scan rate and other parameters of the paging subsystem. 1563591Swnj */ 1573591Swnj setupclock(); 1583591Swnj 1593591Swnj /* 160*16807Skarels * make init process 161*16807Skarels */ 162*16807Skarels 163*16807Skarels proc[0].p_szpt = CLSIZE; 164*16807Skarels if (newproc(0)) { 165*16807Skarels expand(clrnd((int)btoc(szicode)), 0); 166*16807Skarels (void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap); 167*16807Skarels (void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode); 168*16807Skarels /* 169*16807Skarels * Return goes to loc. 0 of user init 170*16807Skarels * code just copied out. 171*16807Skarels */ 172*16807Skarels return; 173*16807Skarels } 174*16807Skarels /* 17526Sbill * make page-out daemon (process 2) 1762753Swnj * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page 17726Sbill * table so that it can map dirty pages into 17826Sbill * its address space during asychronous pushes. 17926Sbill */ 1802753Swnj proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES)); 18126Sbill if (newproc(0)) { 18226Sbill proc[2].p_flag |= SLOAD|SSYS; 1832753Swnj proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX; 18426Sbill pageout(); 1859025Sroot /*NOTREACHED*/ 18626Sbill } 18726Sbill 18826Sbill /* 18926Sbill * enter scheduling loop 19026Sbill */ 19126Sbill proc[0].p_szpt = 1; 19226Sbill sched(); 19326Sbill } 19426Sbill 19526Sbill /* 1967001Smckusick * Initialize hash links for buffers. 1977001Smckusick */ 1987001Smckusick bhinit() 1997001Smckusick { 2007001Smckusick register int i; 2017001Smckusick register struct bufhd *bp; 2027001Smckusick 2037001Smckusick for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++) 2047001Smckusick bp->b_forw = bp->b_back = (struct buf *)bp; 2057001Smckusick } 2067001Smckusick 2077001Smckusick /* 20826Sbill * Initialize the buffer I/O system by freeing 20926Sbill * all buffers and setting all device buffer lists to empty. 21026Sbill */ 21126Sbill binit() 21226Sbill { 2138554Sroot register struct buf *bp, *dp; 21426Sbill register int i; 215306Sbill struct swdevt *swp; 21610336Smckusick int base, residual; 21726Sbill 2182322Swnj for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) { 2192322Swnj dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp; 2202322Swnj dp->b_flags = B_HEAD; 2212322Swnj } 22210336Smckusick base = bufpages / nbuf; 22310336Smckusick residual = bufpages % nbuf; 2246570Smckusic for (i = 0; i < nbuf; i++) { 22526Sbill bp = &buf[i]; 22626Sbill bp->b_dev = NODEV; 2279156Ssam bp->b_bcount = 0; 2286570Smckusic bp->b_un.b_addr = buffers + i * MAXBSIZE; 22910336Smckusick if (i < residual) 23010336Smckusick bp->b_bufsize = (base + 1) * CLBYTES; 23110336Smckusick else 23210336Smckusick bp->b_bufsize = base * CLBYTES; 2339750Ssam binshash(bp, &bfreelist[BQ_AGE]); 2342322Swnj bp->b_flags = B_BUSY|B_INVAL; 23526Sbill brelse(bp); 23626Sbill } 237306Sbill /* 238306Sbill * Count swap devices, and adjust total swap space available. 239306Sbill * Some of this space will not be available until a vswapon() 240306Sbill * system is issued, usually when the system goes multi-user. 241306Sbill */ 242306Sbill nswdev = 0; 24312489Ssam nswap = 0; 24412489Ssam for (swp = swdevt; swp->sw_dev; swp++) { 245306Sbill nswdev++; 24612489Ssam if (swp->sw_nblks > nswap) 24712489Ssam nswap = swp->sw_nblks; 24812489Ssam } 249306Sbill if (nswdev == 0) 250306Sbill panic("binit"); 2514132Secc if (nswdev > 1) 25212489Ssam nswap = ((nswap + dmmax - 1) / dmmax) * dmmax; 253306Sbill nswap *= nswdev; 254306Sbill maxpgio *= nswdev; 255306Sbill swfree(0); 25626Sbill } 25726Sbill 25826Sbill /* 25926Sbill * Initialize linked list of free swap 26026Sbill * headers. These do not actually point 26126Sbill * to buffers, but rather to pages that 26226Sbill * are being swapped in and out. 26326Sbill */ 26426Sbill bswinit() 26526Sbill { 26626Sbill register int i; 2672769Swnj register struct buf *sp = swbuf; 26826Sbill 2692753Swnj bswlist.av_forw = sp; 2702769Swnj for (i=0; i<nswbuf-1; i++, sp++) 2712753Swnj sp->av_forw = sp+1; 2722753Swnj sp->av_forw = NULL; 27326Sbill } 2742746Swnj 2752746Swnj /* 2762746Swnj * Initialize clist by freeing all character blocks, then count 2772746Swnj * number of character devices. (Once-only routine) 2782746Swnj */ 2792746Swnj cinit() 2802746Swnj { 2812746Swnj register int ccp; 2822746Swnj register struct cblock *cp; 2832746Swnj 2842746Swnj ccp = (int)cfree; 2852746Swnj ccp = (ccp+CROUND) & ~CROUND; 2862746Swnj for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) { 2872746Swnj cp->c_next = cfreelist; 2882746Swnj cfreelist = cp; 2892746Swnj cfreecount += CBSIZE; 2902746Swnj } 2912746Swnj } 292