1*15799Smckusick /* init_main.c 6.2 84/01/03 */ 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; 7126Sbill u.u_cmask = CMASK; 727864Sroot for (i = 1; i < NGROUPS; i++) 7311809Ssam u.u_groups[i] = NOGROUP; 748027Sroot for (i = 0; i < sizeof(u.u_rlimit)/sizeof(u.u_rlimit[0]); i++) 758027Sroot u.u_rlimit[i].rlim_cur = u.u_rlimit[i].rlim_max = 768027Sroot RLIM_INFINITY; 778027Sroot u.u_rlimit[RLIMIT_STACK].rlim_cur = 512*1024; 788153Sroot u.u_rlimit[RLIMIT_STACK].rlim_max = ctob(MAXDSIZ); 798153Sroot u.u_rlimit[RLIMIT_DATA].rlim_max = 808153Sroot u.u_rlimit[RLIMIT_DATA].rlim_cur = ctob(MAXDSIZ); 818027Sroot p->p_maxrss = RLIM_INFINITY/NBPG; 827486Skre #ifdef QUOTA 837486Skre qtinit(); 847486Skre p->p_quota = u.u_quota = getquota(0, 0, Q_NDQ); 857486Skre #endif 869025Sroot startrtclock(); 8712823Ssam #include "kg.h" 8812823Ssam #if NKG > 0 8911356Smckusick startkgclock(); 9011356Smckusick #endif 9126Sbill 9226Sbill /* 934821Swnj * Initialize tables, protocols, and set up well-known inodes. 9426Sbill */ 954821Swnj mbinit(); 966121Swnj cinit(); /* needed by dmc-11 driver */ 974821Swnj #ifdef INET 985855Swnj #if NLOOP > 0 995855Swnj loattach(); /* XXX */ 1005855Swnj #endif 1019156Ssam /* 1029156Ssam * Block reception of incoming packets 1039156Ssam * until protocols have been initialized. 1049156Ssam */ 1059156Ssam s = splimp(); 1065227Swnj ifinit(); 1074821Swnj #endif 1088969Sroot domaininit(); 1099156Ssam #ifdef INET 1109156Ssam splx(s); 1119156Ssam #endif 11226Sbill ihinit(); 11392Sbill bhinit(); 11426Sbill binit(); 11526Sbill bswinit(); 116*15799Smckusick nchinit(); 1177419Sroot #ifdef GPROF 1187419Sroot kmstartup(); 1197419Sroot #endif 1207189Sroot 1217813Sroot fs = mountfs(rootdev, 0, (struct inode *)0); 1227189Sroot if (fs == 0) 1237189Sroot panic("iinit"); 1247189Sroot bcopy("/", fs->fs_fsmnt, 2); 1258096Sroot 1269025Sroot inittodr(fs->fs_time); 1278027Sroot boottime = time; 1287189Sroot 1298096Sroot /* kick off timeout driven events by calling first time */ 1308096Sroot roundrobin(); 1318096Sroot schedcpu(); 1328096Sroot schedpaging(); 1338096Sroot 1348096Sroot /* set up the root file system */ 1357189Sroot rootdir = iget(rootdev, fs, (ino_t)ROOTINO); 1367189Sroot iunlock(rootdir); 1377189Sroot u.u_cdir = iget(rootdev, fs, (ino_t)ROOTINO); 1387189Sroot iunlock(u.u_cdir); 1398096Sroot u.u_rdir = NULL; 1407189Sroot 14126Sbill u.u_dmap = zdmap; 14226Sbill u.u_smap = zdmap; 14326Sbill 14426Sbill /* 1453591Swnj * Set the scan rate and other parameters of the paging subsystem. 1463591Swnj */ 1473591Swnj setupclock(); 1483591Swnj 1493591Swnj /* 15026Sbill * make page-out daemon (process 2) 1512753Swnj * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page 15226Sbill * table so that it can map dirty pages into 15326Sbill * its address space during asychronous pushes. 15426Sbill */ 15526Sbill mpid = 1; 1562753Swnj proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES)); 15726Sbill proc[1].p_stat = SZOMB; /* force it to be in proc slot 2 */ 15826Sbill if (newproc(0)) { 15926Sbill proc[2].p_flag |= SLOAD|SSYS; 1602753Swnj proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX; 16126Sbill pageout(); 1629025Sroot /*NOTREACHED*/ 16326Sbill } 16426Sbill 16526Sbill /* 16626Sbill * make init process and 16726Sbill * enter scheduling loop 16826Sbill */ 16926Sbill 17026Sbill mpid = 0; 17126Sbill proc[1].p_stat = 0; 17226Sbill proc[0].p_szpt = CLSIZE; 17326Sbill if (newproc(0)) { 1748441Sroot expand(clrnd((int)btoc(szicode)), 0); 1751787Sbill (void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap); 176135Sbill (void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode); 17726Sbill /* 17826Sbill * Return goes to loc. 0 of user init 17926Sbill * code just copied out. 18026Sbill */ 18126Sbill return; 18226Sbill } 18326Sbill proc[0].p_szpt = 1; 18426Sbill sched(); 18526Sbill } 18626Sbill 18726Sbill /* 1887001Smckusick * Initialize hash links for buffers. 1897001Smckusick */ 1907001Smckusick bhinit() 1917001Smckusick { 1927001Smckusick register int i; 1937001Smckusick register struct bufhd *bp; 1947001Smckusick 1957001Smckusick for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++) 1967001Smckusick bp->b_forw = bp->b_back = (struct buf *)bp; 1977001Smckusick } 1987001Smckusick 1997001Smckusick /* 20026Sbill * Initialize the buffer I/O system by freeing 20126Sbill * all buffers and setting all device buffer lists to empty. 20226Sbill */ 20326Sbill binit() 20426Sbill { 2058554Sroot register struct buf *bp, *dp; 20626Sbill register int i; 207306Sbill struct swdevt *swp; 20810336Smckusick int base, residual; 20926Sbill 2102322Swnj for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) { 2112322Swnj dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp; 2122322Swnj dp->b_flags = B_HEAD; 2132322Swnj } 21410336Smckusick base = bufpages / nbuf; 21510336Smckusick residual = bufpages % nbuf; 2166570Smckusic for (i = 0; i < nbuf; i++) { 21726Sbill bp = &buf[i]; 21826Sbill bp->b_dev = NODEV; 2199156Ssam bp->b_bcount = 0; 2206570Smckusic bp->b_un.b_addr = buffers + i * MAXBSIZE; 22110336Smckusick if (i < residual) 22210336Smckusick bp->b_bufsize = (base + 1) * CLBYTES; 22310336Smckusick else 22410336Smckusick bp->b_bufsize = base * CLBYTES; 2259750Ssam binshash(bp, &bfreelist[BQ_AGE]); 2262322Swnj bp->b_flags = B_BUSY|B_INVAL; 22726Sbill brelse(bp); 22826Sbill } 229306Sbill /* 230306Sbill * Count swap devices, and adjust total swap space available. 231306Sbill * Some of this space will not be available until a vswapon() 232306Sbill * system is issued, usually when the system goes multi-user. 233306Sbill */ 234306Sbill nswdev = 0; 23512489Ssam nswap = 0; 23612489Ssam for (swp = swdevt; swp->sw_dev; swp++) { 237306Sbill nswdev++; 23812489Ssam if (swp->sw_nblks > nswap) 23912489Ssam nswap = swp->sw_nblks; 24012489Ssam } 241306Sbill if (nswdev == 0) 242306Sbill panic("binit"); 2434132Secc if (nswdev > 1) 24412489Ssam nswap = ((nswap + dmmax - 1) / dmmax) * dmmax; 245306Sbill nswap *= nswdev; 246306Sbill maxpgio *= nswdev; 247306Sbill swfree(0); 24826Sbill } 24926Sbill 25026Sbill /* 25126Sbill * Initialize linked list of free swap 25226Sbill * headers. These do not actually point 25326Sbill * to buffers, but rather to pages that 25426Sbill * are being swapped in and out. 25526Sbill */ 25626Sbill bswinit() 25726Sbill { 25826Sbill register int i; 2592769Swnj register struct buf *sp = swbuf; 26026Sbill 2612753Swnj bswlist.av_forw = sp; 2622769Swnj for (i=0; i<nswbuf-1; i++, sp++) 2632753Swnj sp->av_forw = sp+1; 2642753Swnj sp->av_forw = NULL; 26526Sbill } 2662746Swnj 2672746Swnj /* 2682746Swnj * Initialize clist by freeing all character blocks, then count 2692746Swnj * number of character devices. (Once-only routine) 2702746Swnj */ 2712746Swnj cinit() 2722746Swnj { 2732746Swnj register int ccp; 2742746Swnj register struct cblock *cp; 2752746Swnj 2762746Swnj ccp = (int)cfree; 2772746Swnj ccp = (ccp+CROUND) & ~CROUND; 2782746Swnj for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) { 2792746Swnj cp->c_next = cfreelist; 2802746Swnj cfreelist = cp; 2812746Swnj cfreecount += CBSIZE; 2822746Swnj } 2832746Swnj } 284