123364Smckusick /* 229084Smckusick * Copyright (c) 1982, 1986 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*30531Skarels * @(#)init_main.c 7.4 (Berkeley) 02/19/87 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" 2817087Sbloom #include "protosw.h" 2917087Sbloom #include "quota.h" 3010610Ssam #include "../machine/reg.h" 3110610Ssam #include "../machine/cpu.h" 3226Sbill 3316807Skarels int cmask = CMASK; 3426Sbill /* 3526Sbill * Initialization code. 3626Sbill * Called from cold start routine as 3726Sbill * soon as a stack and segmentation 3826Sbill * have been established. 3926Sbill * Functions: 4026Sbill * clear and free user core 4126Sbill * turn on clock 4226Sbill * hand craft 0th process 4326Sbill * call all initialization routines 4426Sbill * fork - process 0 to schedule 4521103Skarels * - process 1 execute bootstrap 4626Sbill * - process 2 to page out 4726Sbill */ 4826Sbill main(firstaddr) 498969Sroot int firstaddr; 5026Sbill { 51361Sbill register int i; 522451Swnj register struct proc *p; 537189Sroot struct fs *fs; 549156Ssam int s; 5526Sbill 5626Sbill rqinit(); 575227Swnj #include "loop.h" 5826Sbill startup(firstaddr); 5926Sbill 6026Sbill /* 6126Sbill * set up system process 0 (swapper) 6226Sbill */ 632451Swnj p = &proc[0]; 6429946Skarels #if defined(tahoe) 6529946Skarels #ifndef lint 6629946Skarels #define initkey(which, p, index) \ 6729946Skarels which/**/_cache[index] = 1, which/**/_cnt[index] = 1; \ 6829946Skarels p->p_/**/which = index; 6929946Skarels initkey(ckey, p, MAXCKEY); 7029946Skarels initkey(dkey, p, MAXDKEY); 7129946Skarels #endif 7229946Skarels #endif 738441Sroot p->p_p0br = u.u_pcb.pcb_p0br; 742451Swnj p->p_szpt = 1; 752451Swnj p->p_addr = uaddr(p); 762451Swnj p->p_stat = SRUN; 772451Swnj p->p_flag |= SLOAD|SSYS; 782451Swnj p->p_nice = NZERO; 792451Swnj setredzone(p->p_addr, (caddr_t)&u); 802451Swnj u.u_procp = p; 8116690Smckusick /* 8218278Smckusick * These assume that the u. area is always mapped 8318278Smckusick * to the same virtual address. Otherwise must be 8416690Smckusick * handled when copying the u. area in newproc(). 8516690Smckusick */ 8616690Smckusick u.u_nd.ni_iov = &u.u_nd.ni_iovec; 8718278Smckusick u.u_ap = u.u_arg; 8816690Smckusick u.u_nd.ni_iovcnt = 1; 8929946Skarels 9016807Skarels u.u_cmask = cmask; 9121103Skarels u.u_lastfile = -1; 927864Sroot for (i = 1; i < NGROUPS; i++) 9311809Ssam u.u_groups[i] = NOGROUP; 948027Sroot for (i = 0; i < sizeof(u.u_rlimit)/sizeof(u.u_rlimit[0]); i++) 958027Sroot u.u_rlimit[i].rlim_cur = u.u_rlimit[i].rlim_max = 968027Sroot RLIM_INFINITY; 9718278Smckusick /* 9823534Skarels * configure virtual memory system, 9923534Skarels * set vm rlimits 10018278Smckusick */ 10118278Smckusick vminit(); 10223534Skarels 10316526Skarels #if defined(QUOTA) 1047486Skre qtinit(); 1057486Skre p->p_quota = u.u_quota = getquota(0, 0, Q_NDQ); 1067486Skre #endif 10730256Ssam startrtclock(); 10829946Skarels #if defined(vax) 10912823Ssam #include "kg.h" 11012823Ssam #if NKG > 0 11111356Smckusick startkgclock(); 11211356Smckusick #endif 11329946Skarels #endif 11426Sbill 11526Sbill /* 1164821Swnj * Initialize tables, protocols, and set up well-known inodes. 11726Sbill */ 1184821Swnj mbinit(); 11921103Skarels cinit(); 12026137Skarels #include "sl.h" 12126137Skarels #if NSL > 0 12226137Skarels slattach(); /* XXX */ 12326137Skarels #endif 1245855Swnj #if NLOOP > 0 1255855Swnj loattach(); /* XXX */ 1265855Swnj #endif 1279156Ssam /* 1289156Ssam * Block reception of incoming packets 1299156Ssam * until protocols have been initialized. 1309156Ssam */ 1319156Ssam s = splimp(); 1325227Swnj ifinit(); 1338969Sroot domaininit(); 1349156Ssam splx(s); 13516526Skarels pqinit(); 13625455Skarels xinit(); 13726Sbill ihinit(); 138*30531Skarels swapinit(); 13915799Smckusick nchinit(); 1407419Sroot #ifdef GPROF 1417419Sroot kmstartup(); 1427419Sroot #endif 1437189Sroot 1447813Sroot fs = mountfs(rootdev, 0, (struct inode *)0); 1457189Sroot if (fs == 0) 1467189Sroot panic("iinit"); 1477189Sroot bcopy("/", fs->fs_fsmnt, 2); 1488096Sroot 1499025Sroot inittodr(fs->fs_time); 1508027Sroot boottime = time; 1517189Sroot 1528096Sroot /* kick off timeout driven events by calling first time */ 1538096Sroot roundrobin(); 1548096Sroot schedcpu(); 1558096Sroot schedpaging(); 1568096Sroot 1578096Sroot /* set up the root file system */ 1587189Sroot rootdir = iget(rootdev, fs, (ino_t)ROOTINO); 1597189Sroot iunlock(rootdir); 1607189Sroot u.u_cdir = iget(rootdev, fs, (ino_t)ROOTINO); 1617189Sroot iunlock(u.u_cdir); 1628096Sroot u.u_rdir = NULL; 1637189Sroot 16426Sbill u.u_dmap = zdmap; 16526Sbill u.u_smap = zdmap; 16626Sbill 16730256Ssam enablertclock(); /* enable realtime clock interrupts */ 16829946Skarels #if defined(tahoe) 16929946Skarels clk_enable = 1; /* enable clock interrupt */ 17029946Skarels #endif 17126Sbill /* 17216807Skarels * make init process 17316807Skarels */ 17416807Skarels 17516807Skarels proc[0].p_szpt = CLSIZE; 17616807Skarels if (newproc(0)) { 17716807Skarels expand(clrnd((int)btoc(szicode)), 0); 17826352Skarels (void) swpexpand(u.u_dsize, (size_t)0, &u.u_dmap, &u.u_smap); 17916807Skarels (void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode); 18016807Skarels /* 18116807Skarels * Return goes to loc. 0 of user init 18216807Skarels * code just copied out. 18316807Skarels */ 18416807Skarels return; 18516807Skarels } 18616807Skarels /* 18726Sbill * make page-out daemon (process 2) 1882753Swnj * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page 18926Sbill * table so that it can map dirty pages into 19026Sbill * its address space during asychronous pushes. 19126Sbill */ 1922753Swnj proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES)); 19326Sbill if (newproc(0)) { 19426Sbill proc[2].p_flag |= SLOAD|SSYS; 1952753Swnj proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX; 19626Sbill pageout(); 1979025Sroot /*NOTREACHED*/ 19826Sbill } 19926Sbill 20026Sbill /* 20126Sbill * enter scheduling loop 20226Sbill */ 20326Sbill proc[0].p_szpt = 1; 20426Sbill sched(); 20526Sbill } 20626Sbill 20726Sbill /* 2087001Smckusick * Initialize hash links for buffers. 2097001Smckusick */ 2107001Smckusick bhinit() 2117001Smckusick { 2127001Smckusick register int i; 2137001Smckusick register struct bufhd *bp; 2147001Smckusick 2157001Smckusick for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++) 2167001Smckusick bp->b_forw = bp->b_back = (struct buf *)bp; 2177001Smckusick } 2187001Smckusick 2197001Smckusick /* 22026Sbill * Initialize the buffer I/O system by freeing 22126Sbill * all buffers and setting all device buffer lists to empty. 22226Sbill */ 22326Sbill binit() 22426Sbill { 2258554Sroot register struct buf *bp, *dp; 22626Sbill register int i; 22710336Smckusick int base, residual; 22826Sbill 2292322Swnj for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) { 2302322Swnj dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp; 2312322Swnj dp->b_flags = B_HEAD; 2322322Swnj } 23310336Smckusick base = bufpages / nbuf; 23410336Smckusick residual = bufpages % nbuf; 2356570Smckusic for (i = 0; i < nbuf; i++) { 23626Sbill bp = &buf[i]; 23726Sbill bp->b_dev = NODEV; 2389156Ssam bp->b_bcount = 0; 2396570Smckusic bp->b_un.b_addr = buffers + i * MAXBSIZE; 24010336Smckusick if (i < residual) 24110336Smckusick bp->b_bufsize = (base + 1) * CLBYTES; 24210336Smckusick else 24310336Smckusick bp->b_bufsize = base * CLBYTES; 2449750Ssam binshash(bp, &bfreelist[BQ_AGE]); 2452322Swnj bp->b_flags = B_BUSY|B_INVAL; 24626Sbill brelse(bp); 24726Sbill } 248*30531Skarels } 249*30531Skarels 250*30531Skarels /* 251*30531Skarels * Set up swap devices. 252*30531Skarels * Initialize linked list of free swap 253*30531Skarels * headers. These do not actually point 254*30531Skarels * to buffers, but rather to pages that 255*30531Skarels * are being swapped in and out. 256*30531Skarels */ 257*30531Skarels swapinit() 258*30531Skarels { 259*30531Skarels register int i; 260*30531Skarels register struct buf *sp = swbuf; 261*30531Skarels struct swdevt *swp; 262*30531Skarels 263306Sbill /* 264306Sbill * Count swap devices, and adjust total swap space available. 265*30531Skarels * Some of this space will not be available until a swapon() 266306Sbill * system is issued, usually when the system goes multi-user. 267306Sbill */ 268306Sbill nswdev = 0; 26912489Ssam nswap = 0; 27012489Ssam for (swp = swdevt; swp->sw_dev; swp++) { 271306Sbill nswdev++; 27212489Ssam if (swp->sw_nblks > nswap) 27312489Ssam nswap = swp->sw_nblks; 27412489Ssam } 275306Sbill if (nswdev == 0) 276*30531Skarels panic("swapinit"); 2774132Secc if (nswdev > 1) 27812489Ssam nswap = ((nswap + dmmax - 1) / dmmax) * dmmax; 279306Sbill nswap *= nswdev; 28023534Skarels /* 28123534Skarels * If there are multiple swap areas, 28223534Skarels * allow more paging operations per second. 28323534Skarels */ 28423534Skarels if (nswdev > 1) 28523534Skarels maxpgio = (maxpgio * (2 * nswdev - 1)) / 2; 286306Sbill swfree(0); 28726Sbill 288*30531Skarels /* 289*30531Skarels * Now set up swap buffer headers. 290*30531Skarels */ 2912753Swnj bswlist.av_forw = sp; 2922769Swnj for (i=0; i<nswbuf-1; i++, sp++) 2932753Swnj sp->av_forw = sp+1; 2942753Swnj sp->av_forw = NULL; 29526Sbill } 2962746Swnj 2972746Swnj /* 2982746Swnj * Initialize clist by freeing all character blocks, then count 2992746Swnj * number of character devices. (Once-only routine) 3002746Swnj */ 3012746Swnj cinit() 3022746Swnj { 3032746Swnj register int ccp; 3042746Swnj register struct cblock *cp; 3052746Swnj 3062746Swnj ccp = (int)cfree; 3072746Swnj ccp = (ccp+CROUND) & ~CROUND; 3082746Swnj for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) { 3092746Swnj cp->c_next = cfreelist; 3102746Swnj cfreelist = cp; 3112746Swnj cfreecount += CBSIZE; 3122746Swnj } 3132746Swnj } 314