123364Smckusick /* 223364Smckusick * Copyright (c) 1982 Regents of the University of California. 323364Smckusick * All rights reserved. The Berkeley software License Agreement 423364Smckusick * specifies the terms and conditions for redistribution. 523364Smckusick * 6*23534Skarels * @(#)init_main.c 6.11 (Berkeley) 06/18/85 723364Smckusick */ 826Sbill 99750Ssam #include "../machine/pte.h" 109750Ssam 1117087Sbloom #include "param.h" 1217087Sbloom #include "systm.h" 1317087Sbloom #include "dir.h" 1417087Sbloom #include "user.h" 1517087Sbloom #include "kernel.h" 1617087Sbloom #include "fs.h" 1717087Sbloom #include "mount.h" 1817087Sbloom #include "map.h" 1917087Sbloom #include "proc.h" 2017087Sbloom #include "inode.h" 2117087Sbloom #include "seg.h" 2217087Sbloom #include "conf.h" 2317087Sbloom #include "buf.h" 2417087Sbloom #include "vm.h" 2517087Sbloom #include "cmap.h" 2617087Sbloom #include "text.h" 2717087Sbloom #include "clist.h" 284821Swnj #ifdef INET 2917087Sbloom #include "protosw.h" 304821Swnj #endif 3117087Sbloom #include "quota.h" 3210610Ssam #include "../machine/reg.h" 3310610Ssam #include "../machine/cpu.h" 3426Sbill 3516807Skarels int cmask = CMASK; 3626Sbill /* 3726Sbill * Initialization code. 3826Sbill * Called from cold start routine as 3926Sbill * soon as a stack and segmentation 4026Sbill * have been established. 4126Sbill * Functions: 4226Sbill * clear and free user core 4326Sbill * turn on clock 4426Sbill * hand craft 0th process 4526Sbill * call all initialization routines 4626Sbill * fork - process 0 to schedule 4721103Skarels * - process 1 execute bootstrap 4826Sbill * - process 2 to page out 4926Sbill */ 5026Sbill main(firstaddr) 518969Sroot int firstaddr; 5226Sbill { 53361Sbill register int i; 542451Swnj register struct proc *p; 557189Sroot struct fs *fs; 569156Ssam int s; 5726Sbill 5826Sbill rqinit(); 595227Swnj #include "loop.h" 6026Sbill startup(firstaddr); 6126Sbill 6226Sbill /* 6326Sbill * set up system process 0 (swapper) 6426Sbill */ 652451Swnj p = &proc[0]; 668441Sroot p->p_p0br = u.u_pcb.pcb_p0br; 672451Swnj p->p_szpt = 1; 682451Swnj p->p_addr = uaddr(p); 692451Swnj p->p_stat = SRUN; 702451Swnj p->p_flag |= SLOAD|SSYS; 712451Swnj p->p_nice = NZERO; 722451Swnj setredzone(p->p_addr, (caddr_t)&u); 732451Swnj u.u_procp = p; 7416690Smckusick #ifdef vax 7516690Smckusick /* 7618278Smckusick * These assume that the u. area is always mapped 7718278Smckusick * to the same virtual address. Otherwise must be 7816690Smckusick * handled when copying the u. area in newproc(). 7916690Smckusick */ 8016690Smckusick u.u_nd.ni_iov = &u.u_nd.ni_iovec; 8118278Smckusick u.u_ap = u.u_arg; 8216690Smckusick #endif 8316690Smckusick u.u_nd.ni_iovcnt = 1; 8416807Skarels u.u_cmask = cmask; 8521103Skarels u.u_lastfile = -1; 867864Sroot for (i = 1; i < NGROUPS; i++) 8711809Ssam u.u_groups[i] = NOGROUP; 888027Sroot for (i = 0; i < sizeof(u.u_rlimit)/sizeof(u.u_rlimit[0]); i++) 898027Sroot u.u_rlimit[i].rlim_cur = u.u_rlimit[i].rlim_max = 908027Sroot RLIM_INFINITY; 9118278Smckusick /* 92*23534Skarels * configure virtual memory system, 93*23534Skarels * set vm rlimits 9418278Smckusick */ 9518278Smckusick vminit(); 96*23534Skarels 9716526Skarels #if defined(QUOTA) 987486Skre qtinit(); 997486Skre p->p_quota = u.u_quota = getquota(0, 0, Q_NDQ); 1007486Skre #endif 1019025Sroot startrtclock(); 10212823Ssam #include "kg.h" 10312823Ssam #if NKG > 0 10411356Smckusick startkgclock(); 10511356Smckusick #endif 10626Sbill 10726Sbill /* 1084821Swnj * Initialize tables, protocols, and set up well-known inodes. 10926Sbill */ 1104821Swnj mbinit(); 11121103Skarels cinit(); 1124821Swnj #ifdef INET 1135855Swnj #if NLOOP > 0 1145855Swnj loattach(); /* XXX */ 1155855Swnj #endif 1169156Ssam /* 1179156Ssam * Block reception of incoming packets 1189156Ssam * until protocols have been initialized. 1199156Ssam */ 1209156Ssam s = splimp(); 1215227Swnj ifinit(); 1224821Swnj #endif 1238969Sroot domaininit(); 1249156Ssam #ifdef INET 1259156Ssam splx(s); 1269156Ssam #endif 12716526Skarels pqinit(); 12826Sbill ihinit(); 12992Sbill bhinit(); 13026Sbill binit(); 13126Sbill bswinit(); 13215799Smckusick nchinit(); 1337419Sroot #ifdef GPROF 1347419Sroot kmstartup(); 1357419Sroot #endif 1367189Sroot 1377813Sroot fs = mountfs(rootdev, 0, (struct inode *)0); 1387189Sroot if (fs == 0) 1397189Sroot panic("iinit"); 1407189Sroot bcopy("/", fs->fs_fsmnt, 2); 1418096Sroot 1429025Sroot inittodr(fs->fs_time); 1438027Sroot boottime = time; 1447189Sroot 1458096Sroot /* kick off timeout driven events by calling first time */ 1468096Sroot roundrobin(); 1478096Sroot schedcpu(); 1488096Sroot schedpaging(); 1498096Sroot 1508096Sroot /* set up the root file system */ 1517189Sroot rootdir = iget(rootdev, fs, (ino_t)ROOTINO); 1527189Sroot iunlock(rootdir); 1537189Sroot u.u_cdir = iget(rootdev, fs, (ino_t)ROOTINO); 1547189Sroot iunlock(u.u_cdir); 1558096Sroot u.u_rdir = NULL; 1567189Sroot 15726Sbill u.u_dmap = zdmap; 15826Sbill u.u_smap = zdmap; 15926Sbill 16026Sbill /* 16116807Skarels * make init process 16216807Skarels */ 16316807Skarels 16416807Skarels proc[0].p_szpt = CLSIZE; 16516807Skarels if (newproc(0)) { 16616807Skarels expand(clrnd((int)btoc(szicode)), 0); 16716807Skarels (void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap); 16816807Skarels (void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode); 16916807Skarels /* 17016807Skarels * Return goes to loc. 0 of user init 17116807Skarels * code just copied out. 17216807Skarels */ 17316807Skarels return; 17416807Skarels } 17516807Skarels /* 17626Sbill * make page-out daemon (process 2) 1772753Swnj * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page 17826Sbill * table so that it can map dirty pages into 17926Sbill * its address space during asychronous pushes. 18026Sbill */ 1812753Swnj proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES)); 18226Sbill if (newproc(0)) { 18326Sbill proc[2].p_flag |= SLOAD|SSYS; 1842753Swnj proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX; 18526Sbill pageout(); 1869025Sroot /*NOTREACHED*/ 18726Sbill } 18826Sbill 18926Sbill /* 19026Sbill * enter scheduling loop 19126Sbill */ 19226Sbill proc[0].p_szpt = 1; 19326Sbill sched(); 19426Sbill } 19526Sbill 19626Sbill /* 1977001Smckusick * Initialize hash links for buffers. 1987001Smckusick */ 1997001Smckusick bhinit() 2007001Smckusick { 2017001Smckusick register int i; 2027001Smckusick register struct bufhd *bp; 2037001Smckusick 2047001Smckusick for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++) 2057001Smckusick bp->b_forw = bp->b_back = (struct buf *)bp; 2067001Smckusick } 2077001Smckusick 2087001Smckusick /* 20926Sbill * Initialize the buffer I/O system by freeing 21026Sbill * all buffers and setting all device buffer lists to empty. 21126Sbill */ 21226Sbill binit() 21326Sbill { 2148554Sroot register struct buf *bp, *dp; 21526Sbill register int i; 216306Sbill struct swdevt *swp; 21710336Smckusick int base, residual; 21826Sbill 2192322Swnj for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) { 2202322Swnj dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp; 2212322Swnj dp->b_flags = B_HEAD; 2222322Swnj } 22310336Smckusick base = bufpages / nbuf; 22410336Smckusick residual = bufpages % nbuf; 2256570Smckusic for (i = 0; i < nbuf; i++) { 22626Sbill bp = &buf[i]; 22726Sbill bp->b_dev = NODEV; 2289156Ssam bp->b_bcount = 0; 2296570Smckusic bp->b_un.b_addr = buffers + i * MAXBSIZE; 23010336Smckusick if (i < residual) 23110336Smckusick bp->b_bufsize = (base + 1) * CLBYTES; 23210336Smckusick else 23310336Smckusick bp->b_bufsize = base * CLBYTES; 2349750Ssam binshash(bp, &bfreelist[BQ_AGE]); 2352322Swnj bp->b_flags = B_BUSY|B_INVAL; 23626Sbill brelse(bp); 23726Sbill } 238306Sbill /* 239306Sbill * Count swap devices, and adjust total swap space available. 240306Sbill * Some of this space will not be available until a vswapon() 241306Sbill * system is issued, usually when the system goes multi-user. 242306Sbill */ 243306Sbill nswdev = 0; 24412489Ssam nswap = 0; 24512489Ssam for (swp = swdevt; swp->sw_dev; swp++) { 246306Sbill nswdev++; 24712489Ssam if (swp->sw_nblks > nswap) 24812489Ssam nswap = swp->sw_nblks; 24912489Ssam } 250306Sbill if (nswdev == 0) 251306Sbill panic("binit"); 2524132Secc if (nswdev > 1) 25312489Ssam nswap = ((nswap + dmmax - 1) / dmmax) * dmmax; 254306Sbill nswap *= nswdev; 255*23534Skarels /* 256*23534Skarels * If there are multiple swap areas, 257*23534Skarels * allow more paging operations per second. 258*23534Skarels */ 259*23534Skarels if (nswdev > 1) 260*23534Skarels maxpgio = (maxpgio * (2 * nswdev - 1)) / 2; 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