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*25455Skarels * @(#)init_main.c 6.13 (Berkeley) 11/09/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" 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]; 648441Sroot p->p_p0br = u.u_pcb.pcb_p0br; 652451Swnj p->p_szpt = 1; 662451Swnj p->p_addr = uaddr(p); 672451Swnj p->p_stat = SRUN; 682451Swnj p->p_flag |= SLOAD|SSYS; 692451Swnj p->p_nice = NZERO; 702451Swnj setredzone(p->p_addr, (caddr_t)&u); 712451Swnj u.u_procp = p; 7216690Smckusick #ifdef vax 7316690Smckusick /* 7418278Smckusick * These assume that the u. area is always mapped 7518278Smckusick * to the same virtual address. Otherwise must be 7616690Smckusick * handled when copying the u. area in newproc(). 7716690Smckusick */ 7816690Smckusick u.u_nd.ni_iov = &u.u_nd.ni_iovec; 7918278Smckusick u.u_ap = u.u_arg; 8016690Smckusick #endif 8116690Smckusick u.u_nd.ni_iovcnt = 1; 8216807Skarels u.u_cmask = cmask; 8321103Skarels u.u_lastfile = -1; 847864Sroot for (i = 1; i < NGROUPS; i++) 8511809Ssam u.u_groups[i] = NOGROUP; 868027Sroot for (i = 0; i < sizeof(u.u_rlimit)/sizeof(u.u_rlimit[0]); i++) 878027Sroot u.u_rlimit[i].rlim_cur = u.u_rlimit[i].rlim_max = 888027Sroot RLIM_INFINITY; 8918278Smckusick /* 9023534Skarels * configure virtual memory system, 9123534Skarels * set vm rlimits 9218278Smckusick */ 9318278Smckusick vminit(); 9423534Skarels 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(); 1105855Swnj #if NLOOP > 0 1115855Swnj loattach(); /* XXX */ 1125855Swnj #endif 1139156Ssam /* 1149156Ssam * Block reception of incoming packets 1159156Ssam * until protocols have been initialized. 1169156Ssam */ 1179156Ssam s = splimp(); 1185227Swnj ifinit(); 1198969Sroot domaininit(); 1209156Ssam splx(s); 12116526Skarels pqinit(); 122*25455Skarels xinit(); 12326Sbill ihinit(); 12492Sbill bhinit(); 12526Sbill binit(); 12626Sbill bswinit(); 12715799Smckusick nchinit(); 1287419Sroot #ifdef GPROF 1297419Sroot kmstartup(); 1307419Sroot #endif 1317189Sroot 1327813Sroot fs = mountfs(rootdev, 0, (struct inode *)0); 1337189Sroot if (fs == 0) 1347189Sroot panic("iinit"); 1357189Sroot bcopy("/", fs->fs_fsmnt, 2); 1368096Sroot 1379025Sroot inittodr(fs->fs_time); 1388027Sroot boottime = time; 1397189Sroot 1408096Sroot /* kick off timeout driven events by calling first time */ 1418096Sroot roundrobin(); 1428096Sroot schedcpu(); 1438096Sroot schedpaging(); 1448096Sroot 1458096Sroot /* set up the root file system */ 1467189Sroot rootdir = iget(rootdev, fs, (ino_t)ROOTINO); 1477189Sroot iunlock(rootdir); 1487189Sroot u.u_cdir = iget(rootdev, fs, (ino_t)ROOTINO); 1497189Sroot iunlock(u.u_cdir); 1508096Sroot u.u_rdir = NULL; 1517189Sroot 15226Sbill u.u_dmap = zdmap; 15326Sbill u.u_smap = zdmap; 15426Sbill 15526Sbill /* 15616807Skarels * make init process 15716807Skarels */ 15816807Skarels 15916807Skarels proc[0].p_szpt = CLSIZE; 16016807Skarels if (newproc(0)) { 16116807Skarels expand(clrnd((int)btoc(szicode)), 0); 16216807Skarels (void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap); 16316807Skarels (void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode); 16416807Skarels /* 16516807Skarels * Return goes to loc. 0 of user init 16616807Skarels * code just copied out. 16716807Skarels */ 16816807Skarels return; 16916807Skarels } 17016807Skarels /* 17126Sbill * make page-out daemon (process 2) 1722753Swnj * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page 17326Sbill * table so that it can map dirty pages into 17426Sbill * its address space during asychronous pushes. 17526Sbill */ 1762753Swnj proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES)); 17726Sbill if (newproc(0)) { 17826Sbill proc[2].p_flag |= SLOAD|SSYS; 1792753Swnj proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX; 18026Sbill pageout(); 1819025Sroot /*NOTREACHED*/ 18226Sbill } 18326Sbill 18426Sbill /* 18526Sbill * enter scheduling loop 18626Sbill */ 18726Sbill proc[0].p_szpt = 1; 18826Sbill sched(); 18926Sbill } 19026Sbill 19126Sbill /* 1927001Smckusick * Initialize hash links for buffers. 1937001Smckusick */ 1947001Smckusick bhinit() 1957001Smckusick { 1967001Smckusick register int i; 1977001Smckusick register struct bufhd *bp; 1987001Smckusick 1997001Smckusick for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++) 2007001Smckusick bp->b_forw = bp->b_back = (struct buf *)bp; 2017001Smckusick } 2027001Smckusick 2037001Smckusick /* 20426Sbill * Initialize the buffer I/O system by freeing 20526Sbill * all buffers and setting all device buffer lists to empty. 20626Sbill */ 20726Sbill binit() 20826Sbill { 2098554Sroot register struct buf *bp, *dp; 21026Sbill register int i; 211306Sbill struct swdevt *swp; 21210336Smckusick int base, residual; 21326Sbill 2142322Swnj for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) { 2152322Swnj dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp; 2162322Swnj dp->b_flags = B_HEAD; 2172322Swnj } 21810336Smckusick base = bufpages / nbuf; 21910336Smckusick residual = bufpages % nbuf; 2206570Smckusic for (i = 0; i < nbuf; i++) { 22126Sbill bp = &buf[i]; 22226Sbill bp->b_dev = NODEV; 2239156Ssam bp->b_bcount = 0; 2246570Smckusic bp->b_un.b_addr = buffers + i * MAXBSIZE; 22510336Smckusick if (i < residual) 22610336Smckusick bp->b_bufsize = (base + 1) * CLBYTES; 22710336Smckusick else 22810336Smckusick bp->b_bufsize = base * CLBYTES; 2299750Ssam binshash(bp, &bfreelist[BQ_AGE]); 2302322Swnj bp->b_flags = B_BUSY|B_INVAL; 23126Sbill brelse(bp); 23226Sbill } 233306Sbill /* 234306Sbill * Count swap devices, and adjust total swap space available. 235306Sbill * Some of this space will not be available until a vswapon() 236306Sbill * system is issued, usually when the system goes multi-user. 237306Sbill */ 238306Sbill nswdev = 0; 23912489Ssam nswap = 0; 24012489Ssam for (swp = swdevt; swp->sw_dev; swp++) { 241306Sbill nswdev++; 24212489Ssam if (swp->sw_nblks > nswap) 24312489Ssam nswap = swp->sw_nblks; 24412489Ssam } 245306Sbill if (nswdev == 0) 246306Sbill panic("binit"); 2474132Secc if (nswdev > 1) 24812489Ssam nswap = ((nswap + dmmax - 1) / dmmax) * dmmax; 249306Sbill nswap *= nswdev; 25023534Skarels /* 25123534Skarels * If there are multiple swap areas, 25223534Skarels * allow more paging operations per second. 25323534Skarels */ 25423534Skarels if (nswdev > 1) 25523534Skarels maxpgio = (maxpgio * (2 * nswdev - 1)) / 2; 256306Sbill swfree(0); 25726Sbill } 25826Sbill 25926Sbill /* 26026Sbill * Initialize linked list of free swap 26126Sbill * headers. These do not actually point 26226Sbill * to buffers, but rather to pages that 26326Sbill * are being swapped in and out. 26426Sbill */ 26526Sbill bswinit() 26626Sbill { 26726Sbill register int i; 2682769Swnj register struct buf *sp = swbuf; 26926Sbill 2702753Swnj bswlist.av_forw = sp; 2712769Swnj for (i=0; i<nswbuf-1; i++, sp++) 2722753Swnj sp->av_forw = sp+1; 2732753Swnj sp->av_forw = NULL; 27426Sbill } 2752746Swnj 2762746Swnj /* 2772746Swnj * Initialize clist by freeing all character blocks, then count 2782746Swnj * number of character devices. (Once-only routine) 2792746Swnj */ 2802746Swnj cinit() 2812746Swnj { 2822746Swnj register int ccp; 2832746Swnj register struct cblock *cp; 2842746Swnj 2852746Swnj ccp = (int)cfree; 2862746Swnj ccp = (ccp+CROUND) & ~CROUND; 2872746Swnj for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) { 2882746Swnj cp->c_next = cfreelist; 2892746Swnj cfreelist = cp; 2902746Swnj cfreecount += CBSIZE; 2912746Swnj } 2922746Swnj } 293