123461Smckusick /* 229132Smckusick * Copyright (c) 1982, 1986 Regents of the University of California. 323461Smckusick * All rights reserved. The Berkeley software License Agreement 423461Smckusick * specifies the terms and conditions for redistribution. 523461Smckusick * 6*37521Smckusick * @(#)kern_physio.c 7.5 (Berkeley) 04/25/89 723461Smckusick */ 88Sbill 917108Sbloom #include "param.h" 1017108Sbloom #include "systm.h" 1117108Sbloom #include "dir.h" 1217108Sbloom #include "user.h" 1317108Sbloom #include "buf.h" 1417108Sbloom #include "conf.h" 1517108Sbloom #include "proc.h" 1617108Sbloom #include "seg.h" 1717108Sbloom #include "vm.h" 1817108Sbloom #include "trace.h" 1917108Sbloom #include "map.h" 2017108Sbloom #include "uio.h" 218Sbill 22*37521Smckusick #include "machine/pte.h" 23*37521Smckusick 2491Sbill /* 258Sbill * Swap IO headers - 268Sbill * They contain the necessary information for the swap I/O. 278Sbill * At any given time, a swap header can be in three 288Sbill * different lists. When free it is in the free list, 298Sbill * when allocated and the I/O queued, it is on the swap 308Sbill * device list, and finally, if the operation was a dirty 318Sbill * page push, when the I/O completes, it is inserted 328Sbill * in a list of cleaned pages to be processed by the pageout daemon. 338Sbill */ 342771Swnj struct buf *swbuf; 358Sbill 368Sbill /* 378Sbill * swap I/O - 388Sbill * 398Sbill * If the flag indicates a dirty page push initiated 408Sbill * by the pageout daemon, we map the page into the i th 418Sbill * virtual page of process 2 (the daemon itself) where i is 428Sbill * the index of the swap header that has been allocated. 438Sbill * We simply initialize the header and queue the I/O but 448Sbill * do not wait for completion. When the I/O completes, 4530750Skarels * biodone() will link the header to a list of cleaned 468Sbill * pages to be processed by the pageout daemon. 478Sbill */ 488Sbill swap(p, dblkno, addr, nbytes, rdflg, flag, dev, pfcent) 498Sbill struct proc *p; 508Sbill swblk_t dblkno; 518Sbill caddr_t addr; 528674S int nbytes, rdflg, flag; 538Sbill dev_t dev; 548674S u_int pfcent; 558Sbill { 568Sbill register struct buf *bp; 5734215Sbostic register struct pte *dpte, *vpte; 588962Sroot register u_int c; 5934215Sbostic int p2dp, s, error = 0; 6034215Sbostic struct buf *getswbuf(); 6134215Sbostic int swdone(); 628Sbill 6334215Sbostic bp = getswbuf(PSWP+1); 648Sbill bp->b_flags = B_BUSY | B_PHYS | rdflg | flag; 658Sbill if ((bp->b_flags & (B_DIRTY|B_PGIN)) == 0) 668Sbill if (rdflg == B_READ) 678Sbill sum.v_pswpin += btoc(nbytes); 688Sbill else 698Sbill sum.v_pswpout += btoc(nbytes); 708Sbill bp->b_proc = p; 718Sbill if (flag & B_DIRTY) { 728Sbill p2dp = ((bp - swbuf) * CLSIZE) * KLMAX; 738Sbill dpte = dptopte(&proc[2], p2dp); 748Sbill vpte = vtopte(p, btop(addr)); 758Sbill for (c = 0; c < nbytes; c += NBPG) { 768Sbill if (vpte->pg_pfnum == 0 || vpte->pg_fod) 778Sbill panic("swap bad pte"); 788Sbill *dpte++ = *vpte++; 798Sbill } 8012491Ssam bp->b_un.b_addr = (caddr_t)ctob(dptov(&proc[2], p2dp)); 8112491Ssam bp->b_flags |= B_CALL; 8212491Ssam bp->b_iodone = swdone; 8312491Ssam bp->b_pfcent = pfcent; 848Sbill } else 858Sbill bp->b_un.b_addr = addr; 868Sbill while (nbytes > 0) { 878962Sroot bp->b_bcount = nbytes; 888962Sroot minphys(bp); 898962Sroot c = bp->b_bcount; 908Sbill bp->b_blkno = dblkno; 918Sbill bp->b_dev = dev; 924033Swnj #ifdef TRACE 934033Swnj trace(TR_SWAPIO, dev, bp->b_blkno); 944033Swnj #endif 9534215Sbostic (*bdevsw[major(dev)].d_strategy)(bp); 9634215Sbostic /* pageout daemon doesn't wait for pushed pages */ 978Sbill if (flag & B_DIRTY) { 988Sbill if (c < nbytes) 998Sbill panic("big push"); 10030750Skarels return (0); 10134215Sbostic } else { 10234215Sbostic s = splbio(); 10334215Sbostic while ((bp->b_flags & B_DONE) == 0) 10434215Sbostic sleep((caddr_t)bp, PSWP); 10534215Sbostic splx(s); 1068Sbill } 1078Sbill bp->b_un.b_addr += c; 1088Sbill bp->b_flags &= ~B_DONE; 1098Sbill if (bp->b_flags & B_ERROR) { 1108Sbill if ((flag & (B_UAREA|B_PAGET)) || rdflg == B_WRITE) 1118Sbill panic("hard IO err in swap"); 11218319Smckusick swkill(p, "swap: read error from swap device"); 11326004Smckusick error = EIO; 1148Sbill } 1158Sbill nbytes -= c; 11612647Ssam dblkno += btodb(c); 1178Sbill } 1188Sbill bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS|B_PAGET|B_UAREA|B_DIRTY); 11934215Sbostic freeswbuf(bp); 12026004Smckusick return (error); 1218Sbill } 1228Sbill 1238Sbill /* 12412491Ssam * Put a buffer on the clean list after I/O is done. 12512491Ssam * Called from biodone. 12612491Ssam */ 12712491Ssam swdone(bp) 12812491Ssam register struct buf *bp; 12912491Ssam { 13012491Ssam register int s; 13112491Ssam 13212491Ssam if (bp->b_flags & B_ERROR) 13312491Ssam panic("IO err in push"); 13426315Skarels s = splbio(); 13512491Ssam bp->av_forw = bclnlist; 13612491Ssam cnt.v_pgout++; 13712491Ssam cnt.v_pgpgout += bp->b_bcount / NBPG; 13812491Ssam bclnlist = bp; 13912491Ssam if (bswlist.b_flags & B_WANTED) 14012491Ssam wakeup((caddr_t)&proc[2]); 14112491Ssam splx(s); 14212491Ssam } 14312491Ssam 14412491Ssam /* 1458Sbill * If rout == 0 then killed on swap error, else 1468Sbill * rout is the name of the routine where we ran out of 1478Sbill * swap space. 1488Sbill */ 1498Sbill swkill(p, rout) 1508Sbill struct proc *p; 1518Sbill char *rout; 1528Sbill { 1538Sbill 15424448Sbloom printf("pid %d: %s\n", p->p_pid, rout); 15524448Sbloom uprintf("sorry, pid %d was killed in %s\n", p->p_pid, rout); 1568Sbill /* 1578Sbill * To be sure no looping (e.g. in vmsched trying to 1588Sbill * swap out) mark process locked in core (as though 1598Sbill * done by user) after killing it so noone will try 1608Sbill * to swap it out. 1618Sbill */ 162165Sbill psignal(p, SIGKILL); 1638Sbill p->p_flag |= SULOCK; 1648Sbill } 1658Sbill 1668Sbill /* 1678Sbill * Raw I/O. The arguments are 1688Sbill * The strategy routine for the device 16934215Sbostic * A buffer, which will either be a special buffer header owned 17034215Sbostic * exclusively by the device for this purpose, or NULL, 17134215Sbostic * indicating that we should use a swap buffer 1728Sbill * The device number 1738Sbill * Read/write flag 1748Sbill * Essentially all the work is computing physical addresses and 1758Sbill * validating them. 1768Sbill * If the user has the proper access privilidges, the process is 1778Sbill * marked 'delayed unlock' and the pages involved in the I/O are 1788Sbill * faulted and locked. After the completion of the I/O, the above pages 1798Sbill * are unlocked. 1808Sbill */ 1817724Swnj physio(strat, bp, dev, rw, mincnt, uio) 1827724Swnj int (*strat)(); 1837724Swnj register struct buf *bp; 1847724Swnj dev_t dev; 1857724Swnj int rw; 18634215Sbostic u_int (*mincnt)(); 1877724Swnj struct uio *uio; 1888Sbill { 18917313Skarels register struct iovec *iov; 1908Sbill register int c; 1918Sbill char *a; 19234215Sbostic int s, allocbuf = 0, error = 0; 19334215Sbostic struct buf *getswbuf(); 1948Sbill 19534215Sbostic if (bp == NULL) { 19634215Sbostic allocbuf = 1; 19734215Sbostic bp = getswbuf(PRIBIO+1); 19834215Sbostic } 19934215Sbostic for (; uio->uio_iovcnt; uio->uio_iov++, uio->uio_iovcnt--) { 20030750Skarels iov = uio->uio_iov; 20134215Sbostic if (!useracc(iov->iov_base, (u_int)iov->iov_len, 20234215Sbostic rw == B_READ ? B_WRITE : B_READ)) { 20334215Sbostic error = EFAULT; 20434215Sbostic break; 20530750Skarels } 20634215Sbostic if (!allocbuf) { /* only if sharing caller's buffer */ 20734215Sbostic s = splbio(); 20834215Sbostic while (bp->b_flags&B_BUSY) { 20934215Sbostic bp->b_flags |= B_WANTED; 21034215Sbostic sleep((caddr_t)bp, PRIBIO+1); 21134215Sbostic } 21234215Sbostic splx(s); 21334215Sbostic } 21430750Skarels bp->b_error = 0; 21530750Skarels bp->b_proc = u.u_procp; 21630750Skarels bp->b_un.b_addr = iov->iov_base; 21730750Skarels while (iov->iov_len > 0) { 21834215Sbostic bp->b_flags = B_BUSY | B_PHYS | B_RAW | rw; 21930750Skarels bp->b_dev = dev; 22030750Skarels bp->b_blkno = btodb(uio->uio_offset); 22130750Skarels bp->b_bcount = iov->iov_len; 22230750Skarels (*mincnt)(bp); 22330750Skarels c = bp->b_bcount; 22430750Skarels u.u_procp->p_flag |= SPHYSIO; 22530750Skarels vslock(a = bp->b_un.b_addr, c); 22634215Sbostic (*strat)(bp); 22734215Sbostic s = splbio(); 22834215Sbostic while ((bp->b_flags & B_DONE) == 0) 22934215Sbostic sleep((caddr_t)bp, PRIBIO); 23030750Skarels vsunlock(a, c, rw); 23130750Skarels u.u_procp->p_flag &= ~SPHYSIO; 23234215Sbostic if (bp->b_flags&B_WANTED) /* rare */ 23330750Skarels wakeup((caddr_t)bp); 23430750Skarels splx(s); 23530750Skarels c -= bp->b_resid; 23630750Skarels bp->b_un.b_addr += c; 23730750Skarels iov->iov_len -= c; 23830750Skarels uio->uio_resid -= c; 23930750Skarels uio->uio_offset += c; 24030750Skarels /* temp kludge for tape drives */ 24130750Skarels if (bp->b_resid || (bp->b_flags&B_ERROR)) 24230750Skarels break; 24330750Skarels } 24434215Sbostic bp->b_flags &= ~(B_BUSY | B_WANTED | B_PHYS | B_RAW); 24530750Skarels error = geterror(bp); 2469766Ssam /* temp kludge for tape drives */ 24730750Skarels if (bp->b_resid || error) 24834215Sbostic break; 2498Sbill } 25034215Sbostic if (allocbuf) 25134215Sbostic freeswbuf(bp); 25234215Sbostic return (error); 2538Sbill } 2548Sbill 25534215Sbostic u_int 2568Sbill minphys(bp) 2577724Swnj struct buf *bp; 2588Sbill { 25910400Ssam if (bp->b_bcount > MAXPHYS) 26010400Ssam bp->b_bcount = MAXPHYS; 2618Sbill } 26234215Sbostic 26334215Sbostic static 26434215Sbostic struct buf * 26534215Sbostic getswbuf(prio) 26634215Sbostic int prio; 26734215Sbostic { 26834215Sbostic int s; 26934215Sbostic struct buf *bp; 27034215Sbostic 27134215Sbostic s = splbio(); 27234215Sbostic while (bswlist.av_forw == NULL) { 27334215Sbostic bswlist.b_flags |= B_WANTED; 27434215Sbostic sleep((caddr_t)&bswlist, prio); 27534215Sbostic } 27634215Sbostic bp = bswlist.av_forw; 27734215Sbostic bswlist.av_forw = bp->av_forw; 27834215Sbostic splx(s); 27934215Sbostic return (bp); 28034215Sbostic } 28134215Sbostic 28234215Sbostic static 28334215Sbostic freeswbuf(bp) 28434215Sbostic struct buf *bp; 28534215Sbostic { 28634215Sbostic int s; 28734215Sbostic 28834215Sbostic s = splbio(); 28934215Sbostic bp->av_forw = bswlist.av_forw; 29034215Sbostic bswlist.av_forw = bp; 29134215Sbostic if (bswlist.b_flags & B_WANTED) { 29234215Sbostic bswlist.b_flags &= ~B_WANTED; 29334215Sbostic wakeup((caddr_t)&bswlist); 29434215Sbostic wakeup((caddr_t)&proc[2]); 29534215Sbostic } 29634215Sbostic splx(s); 29734215Sbostic } 29834215Sbostic 29934215Sbostic rawread(dev, uio) 30034215Sbostic dev_t dev; 30134215Sbostic struct uio *uio; 30234215Sbostic { 30334215Sbostic return (physio(cdevsw[major(dev)].d_strategy, (struct buf *)NULL, 30434215Sbostic dev, B_READ, minphys, uio)); 30534215Sbostic } 30634215Sbostic 30734215Sbostic rawwrite(dev, uio) 30834215Sbostic dev_t dev; 30934215Sbostic struct uio *uio; 31034215Sbostic { 31134215Sbostic return (physio(cdevsw[major(dev)].d_strategy, (struct buf *)NULL, 31234215Sbostic dev, B_WRITE, minphys, uio)); 31334215Sbostic } 314