1*23364Smckusick /* 2*23364Smckusick * Copyright (c) 1982 Regents of the University of California. 3*23364Smckusick * All rights reserved. The Berkeley software License Agreement 4*23364Smckusick * specifies the terms and conditions for redistribution. 5*23364Smckusick * 6*23364Smckusick * @(#)init_main.c 6.10 (Berkeley) 06/08/85 7*23364Smckusick */ 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 /* 9218278Smckusick * Virtual memory limits get set in vminit(). 9318278Smckusick */ 9418278Smckusick vminit(); 9516526Skarels #if defined(QUOTA) 967486Skre qtinit(); 977486Skre p->p_quota = u.u_quota = getquota(0, 0, Q_NDQ); 987486Skre #endif 999025Sroot startrtclock(); 10012823Ssam #include "kg.h" 10112823Ssam #if NKG > 0 10211356Smckusick startkgclock(); 10311356Smckusick #endif 10426Sbill 10526Sbill /* 1064821Swnj * Initialize tables, protocols, and set up well-known inodes. 10726Sbill */ 1084821Swnj mbinit(); 10921103Skarels cinit(); 1104821Swnj #ifdef INET 1115855Swnj #if NLOOP > 0 1125855Swnj loattach(); /* XXX */ 1135855Swnj #endif 1149156Ssam /* 1159156Ssam * Block reception of incoming packets 1169156Ssam * until protocols have been initialized. 1179156Ssam */ 1189156Ssam s = splimp(); 1195227Swnj ifinit(); 1204821Swnj #endif 1218969Sroot domaininit(); 1229156Ssam #ifdef INET 1239156Ssam splx(s); 1249156Ssam #endif 12516526Skarels pqinit(); 12626Sbill ihinit(); 12792Sbill bhinit(); 12826Sbill binit(); 12926Sbill bswinit(); 13015799Smckusick nchinit(); 1317419Sroot #ifdef GPROF 1327419Sroot kmstartup(); 1337419Sroot #endif 1347189Sroot 1357813Sroot fs = mountfs(rootdev, 0, (struct inode *)0); 1367189Sroot if (fs == 0) 1377189Sroot panic("iinit"); 1387189Sroot bcopy("/", fs->fs_fsmnt, 2); 1398096Sroot 1409025Sroot inittodr(fs->fs_time); 1418027Sroot boottime = time; 1427189Sroot 1438096Sroot /* kick off timeout driven events by calling first time */ 1448096Sroot roundrobin(); 1458096Sroot schedcpu(); 1468096Sroot schedpaging(); 1478096Sroot 1488096Sroot /* set up the root file system */ 1497189Sroot rootdir = iget(rootdev, fs, (ino_t)ROOTINO); 1507189Sroot iunlock(rootdir); 1517189Sroot u.u_cdir = iget(rootdev, fs, (ino_t)ROOTINO); 1527189Sroot iunlock(u.u_cdir); 1538096Sroot u.u_rdir = NULL; 1547189Sroot 15526Sbill u.u_dmap = zdmap; 15626Sbill u.u_smap = zdmap; 15726Sbill 15826Sbill /* 15916807Skarels * make init process 16016807Skarels */ 16116807Skarels 16216807Skarels proc[0].p_szpt = CLSIZE; 16316807Skarels if (newproc(0)) { 16416807Skarels expand(clrnd((int)btoc(szicode)), 0); 16516807Skarels (void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap); 16616807Skarels (void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode); 16716807Skarels /* 16816807Skarels * Return goes to loc. 0 of user init 16916807Skarels * code just copied out. 17016807Skarels */ 17116807Skarels return; 17216807Skarels } 17316807Skarels /* 17426Sbill * make page-out daemon (process 2) 1752753Swnj * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page 17626Sbill * table so that it can map dirty pages into 17726Sbill * its address space during asychronous pushes. 17826Sbill */ 1792753Swnj proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES)); 18026Sbill if (newproc(0)) { 18126Sbill proc[2].p_flag |= SLOAD|SSYS; 1822753Swnj proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX; 18326Sbill pageout(); 1849025Sroot /*NOTREACHED*/ 18526Sbill } 18626Sbill 18726Sbill /* 18826Sbill * enter scheduling loop 18926Sbill */ 19026Sbill proc[0].p_szpt = 1; 19126Sbill sched(); 19226Sbill } 19326Sbill 19426Sbill /* 1957001Smckusick * Initialize hash links for buffers. 1967001Smckusick */ 1977001Smckusick bhinit() 1987001Smckusick { 1997001Smckusick register int i; 2007001Smckusick register struct bufhd *bp; 2017001Smckusick 2027001Smckusick for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++) 2037001Smckusick bp->b_forw = bp->b_back = (struct buf *)bp; 2047001Smckusick } 2057001Smckusick 2067001Smckusick /* 20726Sbill * Initialize the buffer I/O system by freeing 20826Sbill * all buffers and setting all device buffer lists to empty. 20926Sbill */ 21026Sbill binit() 21126Sbill { 2128554Sroot register struct buf *bp, *dp; 21326Sbill register int i; 214306Sbill struct swdevt *swp; 21510336Smckusick int base, residual; 21626Sbill 2172322Swnj for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) { 2182322Swnj dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp; 2192322Swnj dp->b_flags = B_HEAD; 2202322Swnj } 22110336Smckusick base = bufpages / nbuf; 22210336Smckusick residual = bufpages % nbuf; 2236570Smckusic for (i = 0; i < nbuf; i++) { 22426Sbill bp = &buf[i]; 22526Sbill bp->b_dev = NODEV; 2269156Ssam bp->b_bcount = 0; 2276570Smckusic bp->b_un.b_addr = buffers + i * MAXBSIZE; 22810336Smckusick if (i < residual) 22910336Smckusick bp->b_bufsize = (base + 1) * CLBYTES; 23010336Smckusick else 23110336Smckusick bp->b_bufsize = base * CLBYTES; 2329750Ssam binshash(bp, &bfreelist[BQ_AGE]); 2332322Swnj bp->b_flags = B_BUSY|B_INVAL; 23426Sbill brelse(bp); 23526Sbill } 236306Sbill /* 237306Sbill * Count swap devices, and adjust total swap space available. 238306Sbill * Some of this space will not be available until a vswapon() 239306Sbill * system is issued, usually when the system goes multi-user. 240306Sbill */ 241306Sbill nswdev = 0; 24212489Ssam nswap = 0; 24312489Ssam for (swp = swdevt; swp->sw_dev; swp++) { 244306Sbill nswdev++; 24512489Ssam if (swp->sw_nblks > nswap) 24612489Ssam nswap = swp->sw_nblks; 24712489Ssam } 248306Sbill if (nswdev == 0) 249306Sbill panic("binit"); 2504132Secc if (nswdev > 1) 25112489Ssam nswap = ((nswap + dmmax - 1) / dmmax) * dmmax; 252306Sbill nswap *= nswdev; 253306Sbill maxpgio *= nswdev; 254306Sbill swfree(0); 25526Sbill } 25626Sbill 25726Sbill /* 25826Sbill * Initialize linked list of free swap 25926Sbill * headers. These do not actually point 26026Sbill * to buffers, but rather to pages that 26126Sbill * are being swapped in and out. 26226Sbill */ 26326Sbill bswinit() 26426Sbill { 26526Sbill register int i; 2662769Swnj register struct buf *sp = swbuf; 26726Sbill 2682753Swnj bswlist.av_forw = sp; 2692769Swnj for (i=0; i<nswbuf-1; i++, sp++) 2702753Swnj sp->av_forw = sp+1; 2712753Swnj sp->av_forw = NULL; 27226Sbill } 2732746Swnj 2742746Swnj /* 2752746Swnj * Initialize clist by freeing all character blocks, then count 2762746Swnj * number of character devices. (Once-only routine) 2772746Swnj */ 2782746Swnj cinit() 2792746Swnj { 2802746Swnj register int ccp; 2812746Swnj register struct cblock *cp; 2822746Swnj 2832746Swnj ccp = (int)cfree; 2842746Swnj ccp = (ccp+CROUND) & ~CROUND; 2852746Swnj for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) { 2862746Swnj cp->c_next = cfreelist; 2872746Swnj cfreelist = cp; 2882746Swnj cfreecount += CBSIZE; 2892746Swnj } 2902746Swnj } 291