1*21103Skarels /* init_main.c 6.9 85/05/27 */ 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 41*21103Skarels * - process 1 execute bootstrap 4226Sbill * - process 2 to page out 4326Sbill */ 4426Sbill main(firstaddr) 458969Sroot int firstaddr; 4626Sbill { 47361Sbill register int i; 482451Swnj register struct proc *p; 497189Sroot struct fs *fs; 509156Ssam int s; 5126Sbill 5226Sbill rqinit(); 535227Swnj #include "loop.h" 5426Sbill startup(firstaddr); 5526Sbill 5626Sbill /* 5726Sbill * set up system process 0 (swapper) 5826Sbill */ 592451Swnj p = &proc[0]; 608441Sroot p->p_p0br = u.u_pcb.pcb_p0br; 612451Swnj p->p_szpt = 1; 622451Swnj p->p_addr = uaddr(p); 632451Swnj p->p_stat = SRUN; 642451Swnj p->p_flag |= SLOAD|SSYS; 652451Swnj p->p_nice = NZERO; 662451Swnj setredzone(p->p_addr, (caddr_t)&u); 672451Swnj u.u_procp = p; 6816690Smckusick #ifdef vax 6916690Smckusick /* 7018278Smckusick * These assume that the u. area is always mapped 7118278Smckusick * to the same virtual address. Otherwise must be 7216690Smckusick * handled when copying the u. area in newproc(). 7316690Smckusick */ 7416690Smckusick u.u_nd.ni_iov = &u.u_nd.ni_iovec; 7518278Smckusick u.u_ap = u.u_arg; 7616690Smckusick #endif 7716690Smckusick u.u_nd.ni_iovcnt = 1; 7816807Skarels u.u_cmask = cmask; 79*21103Skarels u.u_lastfile = -1; 807864Sroot for (i = 1; i < NGROUPS; i++) 8111809Ssam u.u_groups[i] = NOGROUP; 828027Sroot for (i = 0; i < sizeof(u.u_rlimit)/sizeof(u.u_rlimit[0]); i++) 838027Sroot u.u_rlimit[i].rlim_cur = u.u_rlimit[i].rlim_max = 848027Sroot RLIM_INFINITY; 8518278Smckusick /* 8618278Smckusick * Virtual memory limits get set in vminit(). 8718278Smckusick */ 8818278Smckusick vminit(); 8916526Skarels #if defined(QUOTA) 907486Skre qtinit(); 917486Skre p->p_quota = u.u_quota = getquota(0, 0, Q_NDQ); 927486Skre #endif 939025Sroot startrtclock(); 9412823Ssam #include "kg.h" 9512823Ssam #if NKG > 0 9611356Smckusick startkgclock(); 9711356Smckusick #endif 9826Sbill 9926Sbill /* 1004821Swnj * Initialize tables, protocols, and set up well-known inodes. 10126Sbill */ 1024821Swnj mbinit(); 103*21103Skarels cinit(); 1044821Swnj #ifdef INET 1055855Swnj #if NLOOP > 0 1065855Swnj loattach(); /* XXX */ 1075855Swnj #endif 1089156Ssam /* 1099156Ssam * Block reception of incoming packets 1109156Ssam * until protocols have been initialized. 1119156Ssam */ 1129156Ssam s = splimp(); 1135227Swnj ifinit(); 1144821Swnj #endif 1158969Sroot domaininit(); 1169156Ssam #ifdef INET 1179156Ssam splx(s); 1189156Ssam #endif 11916526Skarels pqinit(); 12026Sbill ihinit(); 12192Sbill bhinit(); 12226Sbill binit(); 12326Sbill bswinit(); 12415799Smckusick nchinit(); 1257419Sroot #ifdef GPROF 1267419Sroot kmstartup(); 1277419Sroot #endif 1287189Sroot 1297813Sroot fs = mountfs(rootdev, 0, (struct inode *)0); 1307189Sroot if (fs == 0) 1317189Sroot panic("iinit"); 1327189Sroot bcopy("/", fs->fs_fsmnt, 2); 1338096Sroot 1349025Sroot inittodr(fs->fs_time); 1358027Sroot boottime = time; 1367189Sroot 1378096Sroot /* kick off timeout driven events by calling first time */ 1388096Sroot roundrobin(); 1398096Sroot schedcpu(); 1408096Sroot schedpaging(); 1418096Sroot 1428096Sroot /* set up the root file system */ 1437189Sroot rootdir = iget(rootdev, fs, (ino_t)ROOTINO); 1447189Sroot iunlock(rootdir); 1457189Sroot u.u_cdir = iget(rootdev, fs, (ino_t)ROOTINO); 1467189Sroot iunlock(u.u_cdir); 1478096Sroot u.u_rdir = NULL; 1487189Sroot 14926Sbill u.u_dmap = zdmap; 15026Sbill u.u_smap = zdmap; 15126Sbill 15226Sbill /* 15316807Skarels * make init process 15416807Skarels */ 15516807Skarels 15616807Skarels proc[0].p_szpt = CLSIZE; 15716807Skarels if (newproc(0)) { 15816807Skarels expand(clrnd((int)btoc(szicode)), 0); 15916807Skarels (void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap); 16016807Skarels (void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode); 16116807Skarels /* 16216807Skarels * Return goes to loc. 0 of user init 16316807Skarels * code just copied out. 16416807Skarels */ 16516807Skarels return; 16616807Skarels } 16716807Skarels /* 16826Sbill * make page-out daemon (process 2) 1692753Swnj * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page 17026Sbill * table so that it can map dirty pages into 17126Sbill * its address space during asychronous pushes. 17226Sbill */ 1732753Swnj proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES)); 17426Sbill if (newproc(0)) { 17526Sbill proc[2].p_flag |= SLOAD|SSYS; 1762753Swnj proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX; 17726Sbill pageout(); 1789025Sroot /*NOTREACHED*/ 17926Sbill } 18026Sbill 18126Sbill /* 18226Sbill * enter scheduling loop 18326Sbill */ 18426Sbill proc[0].p_szpt = 1; 18526Sbill sched(); 18626Sbill } 18726Sbill 18826Sbill /* 1897001Smckusick * Initialize hash links for buffers. 1907001Smckusick */ 1917001Smckusick bhinit() 1927001Smckusick { 1937001Smckusick register int i; 1947001Smckusick register struct bufhd *bp; 1957001Smckusick 1967001Smckusick for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++) 1977001Smckusick bp->b_forw = bp->b_back = (struct buf *)bp; 1987001Smckusick } 1997001Smckusick 2007001Smckusick /* 20126Sbill * Initialize the buffer I/O system by freeing 20226Sbill * all buffers and setting all device buffer lists to empty. 20326Sbill */ 20426Sbill binit() 20526Sbill { 2068554Sroot register struct buf *bp, *dp; 20726Sbill register int i; 208306Sbill struct swdevt *swp; 20910336Smckusick int base, residual; 21026Sbill 2112322Swnj for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) { 2122322Swnj dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp; 2132322Swnj dp->b_flags = B_HEAD; 2142322Swnj } 21510336Smckusick base = bufpages / nbuf; 21610336Smckusick residual = bufpages % nbuf; 2176570Smckusic for (i = 0; i < nbuf; i++) { 21826Sbill bp = &buf[i]; 21926Sbill bp->b_dev = NODEV; 2209156Ssam bp->b_bcount = 0; 2216570Smckusic bp->b_un.b_addr = buffers + i * MAXBSIZE; 22210336Smckusick if (i < residual) 22310336Smckusick bp->b_bufsize = (base + 1) * CLBYTES; 22410336Smckusick else 22510336Smckusick bp->b_bufsize = base * CLBYTES; 2269750Ssam binshash(bp, &bfreelist[BQ_AGE]); 2272322Swnj bp->b_flags = B_BUSY|B_INVAL; 22826Sbill brelse(bp); 22926Sbill } 230306Sbill /* 231306Sbill * Count swap devices, and adjust total swap space available. 232306Sbill * Some of this space will not be available until a vswapon() 233306Sbill * system is issued, usually when the system goes multi-user. 234306Sbill */ 235306Sbill nswdev = 0; 23612489Ssam nswap = 0; 23712489Ssam for (swp = swdevt; swp->sw_dev; swp++) { 238306Sbill nswdev++; 23912489Ssam if (swp->sw_nblks > nswap) 24012489Ssam nswap = swp->sw_nblks; 24112489Ssam } 242306Sbill if (nswdev == 0) 243306Sbill panic("binit"); 2444132Secc if (nswdev > 1) 24512489Ssam nswap = ((nswap + dmmax - 1) / dmmax) * dmmax; 246306Sbill nswap *= nswdev; 247306Sbill maxpgio *= nswdev; 248306Sbill swfree(0); 24926Sbill } 25026Sbill 25126Sbill /* 25226Sbill * Initialize linked list of free swap 25326Sbill * headers. These do not actually point 25426Sbill * to buffers, but rather to pages that 25526Sbill * are being swapped in and out. 25626Sbill */ 25726Sbill bswinit() 25826Sbill { 25926Sbill register int i; 2602769Swnj register struct buf *sp = swbuf; 26126Sbill 2622753Swnj bswlist.av_forw = sp; 2632769Swnj for (i=0; i<nswbuf-1; i++, sp++) 2642753Swnj sp->av_forw = sp+1; 2652753Swnj sp->av_forw = NULL; 26626Sbill } 2672746Swnj 2682746Swnj /* 2692746Swnj * Initialize clist by freeing all character blocks, then count 2702746Swnj * number of character devices. (Once-only routine) 2712746Swnj */ 2722746Swnj cinit() 2732746Swnj { 2742746Swnj register int ccp; 2752746Swnj register struct cblock *cp; 2762746Swnj 2772746Swnj ccp = (int)cfree; 2782746Swnj ccp = (ccp+CROUND) & ~CROUND; 2792746Swnj for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) { 2802746Swnj cp->c_next = cfreelist; 2812746Swnj cfreelist = cp; 2822746Swnj cfreecount += CBSIZE; 2832746Swnj } 2842746Swnj } 285