1*16690Smckusick /* init_main.c 6.4 84/07/08 */ 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 */ 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; 71*16690Smckusick #ifdef vax 72*16690Smckusick /* 73*16690Smckusick * This assumes that the u. area is always mapped 74*16690Smckusick * to the same physical address. Otherwise must be 75*16690Smckusick * handled when copying the u. area in newproc(). 76*16690Smckusick */ 77*16690Smckusick u.u_nd.ni_iov = &u.u_nd.ni_iovec; 78*16690Smckusick #endif 79*16690Smckusick u.u_nd.ni_iovcnt = 1; 8026Sbill 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 /* 16026Sbill * make page-out daemon (process 2) 1612753Swnj * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page 16226Sbill * table so that it can map dirty pages into 16326Sbill * its address space during asychronous pushes. 16426Sbill */ 16526Sbill mpid = 1; 1662753Swnj proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES)); 16726Sbill proc[1].p_stat = SZOMB; /* force it to be in proc slot 2 */ 16816526Skarels p = freeproc; 16916526Skarels freeproc = p->p_nxt; 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; 18516526Skarels p->p_nxt = freeproc; 18616526Skarels freeproc = p; 18726Sbill if (newproc(0)) { 1888441Sroot expand(clrnd((int)btoc(szicode)), 0); 1891787Sbill (void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap); 190135Sbill (void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode); 19126Sbill /* 19226Sbill * Return goes to loc. 0 of user init 19326Sbill * code just copied out. 19426Sbill */ 19526Sbill return; 19626Sbill } 19726Sbill proc[0].p_szpt = 1; 19826Sbill sched(); 19926Sbill } 20026Sbill 20126Sbill /* 2027001Smckusick * Initialize hash links for buffers. 2037001Smckusick */ 2047001Smckusick bhinit() 2057001Smckusick { 2067001Smckusick register int i; 2077001Smckusick register struct bufhd *bp; 2087001Smckusick 2097001Smckusick for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++) 2107001Smckusick bp->b_forw = bp->b_back = (struct buf *)bp; 2117001Smckusick } 2127001Smckusick 2137001Smckusick /* 21426Sbill * Initialize the buffer I/O system by freeing 21526Sbill * all buffers and setting all device buffer lists to empty. 21626Sbill */ 21726Sbill binit() 21826Sbill { 2198554Sroot register struct buf *bp, *dp; 22026Sbill register int i; 221306Sbill struct swdevt *swp; 22210336Smckusick int base, residual; 22326Sbill 2242322Swnj for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) { 2252322Swnj dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp; 2262322Swnj dp->b_flags = B_HEAD; 2272322Swnj } 22810336Smckusick base = bufpages / nbuf; 22910336Smckusick residual = bufpages % nbuf; 2306570Smckusic for (i = 0; i < nbuf; i++) { 23126Sbill bp = &buf[i]; 23226Sbill bp->b_dev = NODEV; 2339156Ssam bp->b_bcount = 0; 2346570Smckusic bp->b_un.b_addr = buffers + i * MAXBSIZE; 23510336Smckusick if (i < residual) 23610336Smckusick bp->b_bufsize = (base + 1) * CLBYTES; 23710336Smckusick else 23810336Smckusick bp->b_bufsize = base * CLBYTES; 2399750Ssam binshash(bp, &bfreelist[BQ_AGE]); 2402322Swnj bp->b_flags = B_BUSY|B_INVAL; 24126Sbill brelse(bp); 24226Sbill } 243306Sbill /* 244306Sbill * Count swap devices, and adjust total swap space available. 245306Sbill * Some of this space will not be available until a vswapon() 246306Sbill * system is issued, usually when the system goes multi-user. 247306Sbill */ 248306Sbill nswdev = 0; 24912489Ssam nswap = 0; 25012489Ssam for (swp = swdevt; swp->sw_dev; swp++) { 251306Sbill nswdev++; 25212489Ssam if (swp->sw_nblks > nswap) 25312489Ssam nswap = swp->sw_nblks; 25412489Ssam } 255306Sbill if (nswdev == 0) 256306Sbill panic("binit"); 2574132Secc if (nswdev > 1) 25812489Ssam nswap = ((nswap + dmmax - 1) / dmmax) * dmmax; 259306Sbill nswap *= nswdev; 260306Sbill maxpgio *= nswdev; 261306Sbill swfree(0); 26226Sbill } 26326Sbill 26426Sbill /* 26526Sbill * Initialize linked list of free swap 26626Sbill * headers. These do not actually point 26726Sbill * to buffers, but rather to pages that 26826Sbill * are being swapped in and out. 26926Sbill */ 27026Sbill bswinit() 27126Sbill { 27226Sbill register int i; 2732769Swnj register struct buf *sp = swbuf; 27426Sbill 2752753Swnj bswlist.av_forw = sp; 2762769Swnj for (i=0; i<nswbuf-1; i++, sp++) 2772753Swnj sp->av_forw = sp+1; 2782753Swnj sp->av_forw = NULL; 27926Sbill } 2802746Swnj 2812746Swnj /* 2822746Swnj * Initialize clist by freeing all character blocks, then count 2832746Swnj * number of character devices. (Once-only routine) 2842746Swnj */ 2852746Swnj cinit() 2862746Swnj { 2872746Swnj register int ccp; 2882746Swnj register struct cblock *cp; 2892746Swnj 2902746Swnj ccp = (int)cfree; 2912746Swnj ccp = (ccp+CROUND) & ~CROUND; 2922746Swnj for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) { 2932746Swnj cp->c_next = cfreelist; 2942746Swnj cfreelist = cp; 2952746Swnj cfreecount += CBSIZE; 2962746Swnj } 2972746Swnj } 298