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*37522Smckusick * @(#)init_main.c 7.8 (Berkeley) 04/25/89 723364Smckusick */ 826Sbill 917087Sbloom #include "param.h" 1017087Sbloom #include "systm.h" 1117087Sbloom #include "dir.h" 1217087Sbloom #include "user.h" 1317087Sbloom #include "kernel.h" 1417087Sbloom #include "fs.h" 1517087Sbloom #include "mount.h" 1617087Sbloom #include "map.h" 1717087Sbloom #include "proc.h" 1817087Sbloom #include "inode.h" 1917087Sbloom #include "seg.h" 2017087Sbloom #include "conf.h" 2117087Sbloom #include "buf.h" 2217087Sbloom #include "vm.h" 2317087Sbloom #include "cmap.h" 2417087Sbloom #include "text.h" 2517087Sbloom #include "clist.h" 2635810Smarc #include "malloc.h" 2717087Sbloom #include "protosw.h" 2817087Sbloom #include "quota.h" 2930570Skarels #include "reboot.h" 3026Sbill 31*37522Smckusick #include "machine/pte.h" 32*37522Smckusick #include "machine/reg.h" 33*37522Smckusick #include "machine/cpu.h" 34*37522Smckusick 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; 5535810Smarc register struct pgrp *pg; 567189Sroot struct fs *fs; 579156Ssam int s; 5826Sbill 5926Sbill rqinit(); 605227Swnj #include "loop.h" 6126Sbill startup(firstaddr); 6226Sbill 6326Sbill /* 6426Sbill * set up system process 0 (swapper) 6526Sbill */ 662451Swnj p = &proc[0]; 678441Sroot p->p_p0br = u.u_pcb.pcb_p0br; 682451Swnj p->p_szpt = 1; 692451Swnj p->p_addr = uaddr(p); 702451Swnj p->p_stat = SRUN; 712451Swnj p->p_flag |= SLOAD|SSYS; 722451Swnj p->p_nice = NZERO; 732451Swnj setredzone(p->p_addr, (caddr_t)&u); 742451Swnj u.u_procp = p; 7535810Smarc MALLOC(pgrphash[0], struct pgrp *, sizeof (struct pgrp), 7635810Smarc M_PGRP, M_NOWAIT); 7735810Smarc if ((pg = pgrphash[0]) == NULL) 7835810Smarc panic("no space to craft zero'th process group"); 7935810Smarc pg->pg_id = 0; 8035810Smarc pg->pg_hforw = 0; 8135810Smarc pg->pg_mem = p; 8235810Smarc pg->pg_jobc = 0; 8335810Smarc p->p_pgrp = pg; 8435810Smarc p->p_pgrpnxt = 0; 8535810Smarc MALLOC(pg->pg_session, struct session *, sizeof (struct session), 8635810Smarc M_SESSION, M_NOWAIT); 8735810Smarc if (pg->pg_session == NULL) 8835810Smarc panic("no space to craft zero'th session"); 8935810Smarc pg->pg_session->s_count = 1; 9035810Smarc pg->pg_session->s_leader = 0; 9116690Smckusick /* 9218278Smckusick * These assume that the u. area is always mapped 9318278Smckusick * to the same virtual address. Otherwise must be 9416690Smckusick * handled when copying the u. area in newproc(). 9516690Smckusick */ 9616690Smckusick u.u_nd.ni_iov = &u.u_nd.ni_iovec; 9718278Smckusick u.u_ap = u.u_arg; 9816690Smckusick u.u_nd.ni_iovcnt = 1; 9929946Skarels 10016807Skarels u.u_cmask = cmask; 10121103Skarels u.u_lastfile = -1; 1027864Sroot for (i = 1; i < NGROUPS; i++) 10311809Ssam u.u_groups[i] = NOGROUP; 1048027Sroot for (i = 0; i < sizeof(u.u_rlimit)/sizeof(u.u_rlimit[0]); i++) 1058027Sroot u.u_rlimit[i].rlim_cur = u.u_rlimit[i].rlim_max = 1068027Sroot RLIM_INFINITY; 10718278Smckusick /* 10823534Skarels * configure virtual memory system, 10923534Skarels * set vm rlimits 11018278Smckusick */ 11118278Smckusick vminit(); 11223534Skarels 11316526Skarels #if defined(QUOTA) 1147486Skre qtinit(); 1157486Skre p->p_quota = u.u_quota = getquota(0, 0, Q_NDQ); 1167486Skre #endif 11730256Ssam startrtclock(); 11829946Skarels #if defined(vax) 11912823Ssam #include "kg.h" 12012823Ssam #if NKG > 0 12111356Smckusick startkgclock(); 12211356Smckusick #endif 12329946Skarels #endif 12426Sbill 12526Sbill /* 1264821Swnj * Initialize tables, protocols, and set up well-known inodes. 12726Sbill */ 1284821Swnj mbinit(); 12921103Skarels cinit(); 13026137Skarels #include "sl.h" 13126137Skarels #if NSL > 0 13226137Skarels slattach(); /* XXX */ 13326137Skarels #endif 1345855Swnj #if NLOOP > 0 1355855Swnj loattach(); /* XXX */ 1365855Swnj #endif 1379156Ssam /* 1389156Ssam * Block reception of incoming packets 1399156Ssam * until protocols have been initialized. 1409156Ssam */ 1419156Ssam s = splimp(); 1425227Swnj ifinit(); 1438969Sroot domaininit(); 1449156Ssam splx(s); 14516526Skarels pqinit(); 14625455Skarels xinit(); 14726Sbill ihinit(); 14830531Skarels swapinit(); 14915799Smckusick nchinit(); 1507419Sroot #ifdef GPROF 1517419Sroot kmstartup(); 1527419Sroot #endif 1537189Sroot 15430570Skarels fs = mountfs(rootdev, boothowto & RB_RDONLY, (struct inode *)0); 1557189Sroot if (fs == 0) 1567189Sroot panic("iinit"); 1577189Sroot bcopy("/", fs->fs_fsmnt, 2); 1588096Sroot 1599025Sroot inittodr(fs->fs_time); 1608027Sroot boottime = time; 1617189Sroot 1628096Sroot /* kick off timeout driven events by calling first time */ 1638096Sroot roundrobin(); 1648096Sroot schedcpu(); 1658096Sroot schedpaging(); 1668096Sroot 1678096Sroot /* set up the root file system */ 1687189Sroot rootdir = iget(rootdev, fs, (ino_t)ROOTINO); 1697189Sroot iunlock(rootdir); 1707189Sroot u.u_cdir = iget(rootdev, fs, (ino_t)ROOTINO); 1717189Sroot iunlock(u.u_cdir); 1728096Sroot u.u_rdir = NULL; 1737189Sroot 17426Sbill u.u_dmap = zdmap; 17526Sbill u.u_smap = zdmap; 17626Sbill 17730256Ssam enablertclock(); /* enable realtime clock interrupts */ 17826Sbill /* 17916807Skarels * make init process 18016807Skarels */ 18116807Skarels 18216807Skarels proc[0].p_szpt = CLSIZE; 18316807Skarels if (newproc(0)) { 18416807Skarels expand(clrnd((int)btoc(szicode)), 0); 18526352Skarels (void) swpexpand(u.u_dsize, (size_t)0, &u.u_dmap, &u.u_smap); 18616807Skarels (void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode); 18716807Skarels /* 18816807Skarels * Return goes to loc. 0 of user init 18916807Skarels * code just copied out. 19016807Skarels */ 19116807Skarels return; 19216807Skarels } 19316807Skarels /* 19426Sbill * make page-out daemon (process 2) 1952753Swnj * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page 19626Sbill * table so that it can map dirty pages into 19726Sbill * its address space during asychronous pushes. 19826Sbill */ 1992753Swnj proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES)); 20026Sbill if (newproc(0)) { 20126Sbill proc[2].p_flag |= SLOAD|SSYS; 2022753Swnj proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX; 20326Sbill pageout(); 2049025Sroot /*NOTREACHED*/ 20526Sbill } 20626Sbill 20726Sbill /* 20826Sbill * enter scheduling loop 20926Sbill */ 21026Sbill proc[0].p_szpt = 1; 21126Sbill sched(); 21226Sbill } 21326Sbill 21426Sbill /* 2157001Smckusick * Initialize hash links for buffers. 2167001Smckusick */ 2177001Smckusick bhinit() 2187001Smckusick { 2197001Smckusick register int i; 2207001Smckusick register struct bufhd *bp; 2217001Smckusick 2227001Smckusick for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++) 2237001Smckusick bp->b_forw = bp->b_back = (struct buf *)bp; 2247001Smckusick } 2257001Smckusick 2267001Smckusick /* 22726Sbill * Initialize the buffer I/O system by freeing 22826Sbill * all buffers and setting all device buffer lists to empty. 22926Sbill */ 23026Sbill binit() 23126Sbill { 2328554Sroot register struct buf *bp, *dp; 23326Sbill register int i; 23410336Smckusick int base, residual; 23526Sbill 2362322Swnj for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) { 2372322Swnj dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp; 2382322Swnj dp->b_flags = B_HEAD; 2392322Swnj } 24010336Smckusick base = bufpages / nbuf; 24110336Smckusick residual = bufpages % nbuf; 2426570Smckusic for (i = 0; i < nbuf; i++) { 24326Sbill bp = &buf[i]; 24426Sbill bp->b_dev = NODEV; 2459156Ssam bp->b_bcount = 0; 2466570Smckusic bp->b_un.b_addr = buffers + i * MAXBSIZE; 24710336Smckusick if (i < residual) 24810336Smckusick bp->b_bufsize = (base + 1) * CLBYTES; 24910336Smckusick else 25010336Smckusick bp->b_bufsize = base * CLBYTES; 2519750Ssam binshash(bp, &bfreelist[BQ_AGE]); 2522322Swnj bp->b_flags = B_BUSY|B_INVAL; 25326Sbill brelse(bp); 25426Sbill } 25530531Skarels } 25630531Skarels 25730531Skarels /* 25830531Skarels * Set up swap devices. 25930531Skarels * Initialize linked list of free swap 26030531Skarels * headers. These do not actually point 26130531Skarels * to buffers, but rather to pages that 26230531Skarels * are being swapped in and out. 26330531Skarels */ 26430531Skarels swapinit() 26530531Skarels { 26630531Skarels register int i; 26730531Skarels register struct buf *sp = swbuf; 26830531Skarels struct swdevt *swp; 26934492Skarels int error; 27030531Skarels 271306Sbill /* 272306Sbill * Count swap devices, and adjust total swap space available. 27330531Skarels * Some of this space will not be available until a swapon() 274306Sbill * system is issued, usually when the system goes multi-user. 275306Sbill */ 276306Sbill nswdev = 0; 27712489Ssam nswap = 0; 27812489Ssam for (swp = swdevt; swp->sw_dev; swp++) { 279306Sbill nswdev++; 28012489Ssam if (swp->sw_nblks > nswap) 28112489Ssam nswap = swp->sw_nblks; 28212489Ssam } 283306Sbill if (nswdev == 0) 28430531Skarels panic("swapinit"); 2854132Secc if (nswdev > 1) 28612489Ssam nswap = ((nswap + dmmax - 1) / dmmax) * dmmax; 287306Sbill nswap *= nswdev; 28823534Skarels /* 28923534Skarels * If there are multiple swap areas, 29023534Skarels * allow more paging operations per second. 29123534Skarels */ 29223534Skarels if (nswdev > 1) 29323534Skarels maxpgio = (maxpgio * (2 * nswdev - 1)) / 2; 29434492Skarels if (error = swfree(0)) { 29534492Skarels printf("swfree errno %d\n", error); /* XXX */ 29634492Skarels panic("swapinit swfree 0"); 29734492Skarels } 29826Sbill 29930531Skarels /* 30030531Skarels * Now set up swap buffer headers. 30130531Skarels */ 3022753Swnj bswlist.av_forw = sp; 3032769Swnj for (i=0; i<nswbuf-1; i++, sp++) 3042753Swnj sp->av_forw = sp+1; 3052753Swnj sp->av_forw = NULL; 30626Sbill } 3072746Swnj 3082746Swnj /* 3092746Swnj * Initialize clist by freeing all character blocks, then count 3102746Swnj * number of character devices. (Once-only routine) 3112746Swnj */ 3122746Swnj cinit() 3132746Swnj { 3142746Swnj register int ccp; 3152746Swnj register struct cblock *cp; 3162746Swnj 3172746Swnj ccp = (int)cfree; 3182746Swnj ccp = (ccp+CROUND) & ~CROUND; 3192746Swnj for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) { 3202746Swnj cp->c_next = cfreelist; 3212746Swnj cfreelist = cp; 3222746Swnj cfreecount += CBSIZE; 3232746Swnj } 3242746Swnj } 325