1*18278Smckusick /* init_main.c 6.8 85/03/08 */ 226Sbill 39750Ssam #include "../machine/pte.h" 49750Ssam 517087Sbloom #include "param.h" 617087Sbloom #include "systm.h" 717087Sbloom #include "dir.h" 817087Sbloom #include "user.h" 917087Sbloom #include "kernel.h" 1017087Sbloom #include "fs.h" 1117087Sbloom #include "mount.h" 1217087Sbloom #include "map.h" 1317087Sbloom #include "proc.h" 1417087Sbloom #include "inode.h" 1517087Sbloom #include "seg.h" 1617087Sbloom #include "conf.h" 1717087Sbloom #include "buf.h" 1817087Sbloom #include "vm.h" 1917087Sbloom #include "cmap.h" 2017087Sbloom #include "text.h" 2117087Sbloom #include "clist.h" 224821Swnj #ifdef INET 2317087Sbloom #include "protosw.h" 244821Swnj #endif 2517087Sbloom #include "quota.h" 2610610Ssam #include "../machine/reg.h" 2710610Ssam #include "../machine/cpu.h" 2826Sbill 2916807Skarels 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 /* 73*18278Smckusick * These assume that the u. area is always mapped 74*18278Smckusick * to the same virtual 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; 78*18278Smckusick u.u_ap = u.u_arg; 7916690Smckusick #endif 8016690Smckusick u.u_nd.ni_iovcnt = 1; 8116807Skarels u.u_cmask = cmask; 827864Sroot for (i = 1; i < NGROUPS; i++) 8311809Ssam u.u_groups[i] = NOGROUP; 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; 87*18278Smckusick /* 88*18278Smckusick * Virtual memory limits get set in vminit(). 89*18278Smckusick */ 90*18278Smckusick vminit(); 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 /* 15516807Skarels * make init process 15616807Skarels */ 15716807Skarels 15816807Skarels proc[0].p_szpt = CLSIZE; 15916807Skarels if (newproc(0)) { 16016807Skarels expand(clrnd((int)btoc(szicode)), 0); 16116807Skarels (void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap); 16216807Skarels (void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode); 16316807Skarels /* 16416807Skarels * Return goes to loc. 0 of user init 16516807Skarels * code just copied out. 16616807Skarels */ 16716807Skarels return; 16816807Skarels } 16916807Skarels /* 17026Sbill * make page-out daemon (process 2) 1712753Swnj * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page 17226Sbill * table so that it can map dirty pages into 17326Sbill * its address space during asychronous pushes. 17426Sbill */ 1752753Swnj proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES)); 17626Sbill if (newproc(0)) { 17726Sbill proc[2].p_flag |= SLOAD|SSYS; 1782753Swnj proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX; 17926Sbill pageout(); 1809025Sroot /*NOTREACHED*/ 18126Sbill } 18226Sbill 18326Sbill /* 18426Sbill * enter scheduling loop 18526Sbill */ 18626Sbill proc[0].p_szpt = 1; 18726Sbill sched(); 18826Sbill } 18926Sbill 19026Sbill /* 1917001Smckusick * Initialize hash links for buffers. 1927001Smckusick */ 1937001Smckusick bhinit() 1947001Smckusick { 1957001Smckusick register int i; 1967001Smckusick register struct bufhd *bp; 1977001Smckusick 1987001Smckusick for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++) 1997001Smckusick bp->b_forw = bp->b_back = (struct buf *)bp; 2007001Smckusick } 2017001Smckusick 2027001Smckusick /* 20326Sbill * Initialize the buffer I/O system by freeing 20426Sbill * all buffers and setting all device buffer lists to empty. 20526Sbill */ 20626Sbill binit() 20726Sbill { 2088554Sroot register struct buf *bp, *dp; 20926Sbill register int i; 210306Sbill struct swdevt *swp; 21110336Smckusick int base, residual; 21226Sbill 2132322Swnj for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) { 2142322Swnj dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp; 2152322Swnj dp->b_flags = B_HEAD; 2162322Swnj } 21710336Smckusick base = bufpages / nbuf; 21810336Smckusick residual = bufpages % nbuf; 2196570Smckusic for (i = 0; i < nbuf; i++) { 22026Sbill bp = &buf[i]; 22126Sbill bp->b_dev = NODEV; 2229156Ssam bp->b_bcount = 0; 2236570Smckusic bp->b_un.b_addr = buffers + i * MAXBSIZE; 22410336Smckusick if (i < residual) 22510336Smckusick bp->b_bufsize = (base + 1) * CLBYTES; 22610336Smckusick else 22710336Smckusick bp->b_bufsize = base * CLBYTES; 2289750Ssam binshash(bp, &bfreelist[BQ_AGE]); 2292322Swnj bp->b_flags = B_BUSY|B_INVAL; 23026Sbill brelse(bp); 23126Sbill } 232306Sbill /* 233306Sbill * Count swap devices, and adjust total swap space available. 234306Sbill * Some of this space will not be available until a vswapon() 235306Sbill * system is issued, usually when the system goes multi-user. 236306Sbill */ 237306Sbill nswdev = 0; 23812489Ssam nswap = 0; 23912489Ssam for (swp = swdevt; swp->sw_dev; swp++) { 240306Sbill nswdev++; 24112489Ssam if (swp->sw_nblks > nswap) 24212489Ssam nswap = swp->sw_nblks; 24312489Ssam } 244306Sbill if (nswdev == 0) 245306Sbill panic("binit"); 2464132Secc if (nswdev > 1) 24712489Ssam nswap = ((nswap + dmmax - 1) / dmmax) * dmmax; 248306Sbill nswap *= nswdev; 249306Sbill maxpgio *= nswdev; 250306Sbill swfree(0); 25126Sbill } 25226Sbill 25326Sbill /* 25426Sbill * Initialize linked list of free swap 25526Sbill * headers. These do not actually point 25626Sbill * to buffers, but rather to pages that 25726Sbill * are being swapped in and out. 25826Sbill */ 25926Sbill bswinit() 26026Sbill { 26126Sbill register int i; 2622769Swnj register struct buf *sp = swbuf; 26326Sbill 2642753Swnj bswlist.av_forw = sp; 2652769Swnj for (i=0; i<nswbuf-1; i++, sp++) 2662753Swnj sp->av_forw = sp+1; 2672753Swnj sp->av_forw = NULL; 26826Sbill } 2692746Swnj 2702746Swnj /* 2712746Swnj * Initialize clist by freeing all character blocks, then count 2722746Swnj * number of character devices. (Once-only routine) 2732746Swnj */ 2742746Swnj cinit() 2752746Swnj { 2762746Swnj register int ccp; 2772746Swnj register struct cblock *cp; 2782746Swnj 2792746Swnj ccp = (int)cfree; 2802746Swnj ccp = (ccp+CROUND) & ~CROUND; 2812746Swnj for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) { 2822746Swnj cp->c_next = cfreelist; 2832746Swnj cfreelist = cp; 2842746Swnj cfreecount += CBSIZE; 2852746Swnj } 2862746Swnj } 287