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*39880Smckusick * @(#)kern_physio.c 7.12 (Berkeley) 01/04/90 723461Smckusick */ 88Sbill 917108Sbloom #include "param.h" 1017108Sbloom #include "systm.h" 1117108Sbloom #include "user.h" 1217108Sbloom #include "buf.h" 1317108Sbloom #include "conf.h" 1417108Sbloom #include "proc.h" 1517108Sbloom #include "seg.h" 1617108Sbloom #include "vm.h" 1717108Sbloom #include "trace.h" 1817108Sbloom #include "map.h" 1937729Smckusick #include "vnode.h" 208Sbill 2137521Smckusick #include "machine/pte.h" 2237521Smckusick 2391Sbill /* 248Sbill * Swap IO headers - 258Sbill * They contain the necessary information for the swap I/O. 268Sbill * At any given time, a swap header can be in three 278Sbill * different lists. When free it is in the free list, 288Sbill * when allocated and the I/O queued, it is on the swap 298Sbill * device list, and finally, if the operation was a dirty 308Sbill * page push, when the I/O completes, it is inserted 318Sbill * in a list of cleaned pages to be processed by the pageout daemon. 328Sbill */ 332771Swnj struct buf *swbuf; 348Sbill 358Sbill /* 368Sbill * swap I/O - 378Sbill * 388Sbill * If the flag indicates a dirty page push initiated 398Sbill * by the pageout daemon, we map the page into the i th 408Sbill * virtual page of process 2 (the daemon itself) where i is 418Sbill * the index of the swap header that has been allocated. 428Sbill * We simply initialize the header and queue the I/O but 438Sbill * do not wait for completion. When the I/O completes, 4430750Skarels * biodone() will link the header to a list of cleaned 458Sbill * pages to be processed by the pageout daemon. 468Sbill */ 4737729Smckusick swap(p, dblkno, addr, nbytes, rdflg, flag, vp, pfcent) 488Sbill struct proc *p; 498Sbill swblk_t dblkno; 508Sbill caddr_t addr; 518674S int nbytes, rdflg, flag; 5237729Smckusick struct vnode *vp; 538674S u_int pfcent; 548Sbill { 558Sbill register struct buf *bp; 5634215Sbostic register struct pte *dpte, *vpte; 578962Sroot register u_int c; 5834215Sbostic int p2dp, s, error = 0; 5934215Sbostic struct buf *getswbuf(); 6034215Sbostic int swdone(); 618Sbill 6234215Sbostic bp = getswbuf(PSWP+1); 638Sbill bp->b_flags = B_BUSY | B_PHYS | rdflg | flag; 648Sbill if ((bp->b_flags & (B_DIRTY|B_PGIN)) == 0) 658Sbill if (rdflg == B_READ) 668Sbill sum.v_pswpin += btoc(nbytes); 678Sbill else 688Sbill sum.v_pswpout += btoc(nbytes); 698Sbill bp->b_proc = p; 708Sbill if (flag & B_DIRTY) { 718Sbill p2dp = ((bp - swbuf) * CLSIZE) * KLMAX; 728Sbill dpte = dptopte(&proc[2], p2dp); 738Sbill vpte = vtopte(p, btop(addr)); 748Sbill for (c = 0; c < nbytes; c += NBPG) { 758Sbill if (vpte->pg_pfnum == 0 || vpte->pg_fod) 768Sbill panic("swap bad pte"); 778Sbill *dpte++ = *vpte++; 788Sbill } 7912491Ssam bp->b_un.b_addr = (caddr_t)ctob(dptov(&proc[2], p2dp)); 8012491Ssam bp->b_flags |= B_CALL; 8112491Ssam bp->b_iodone = swdone; 8212491Ssam bp->b_pfcent = pfcent; 838Sbill } else 848Sbill bp->b_un.b_addr = addr; 858Sbill while (nbytes > 0) { 8637729Smckusick bp->b_blkno = dblkno; 8739148Smckusick if (bp->b_vp) 8839148Smckusick brelvp(bp); 8939807Smckusick VHOLD(vp); 9039148Smckusick bp->b_vp = vp; 9139148Smckusick bp->b_dev = vp->v_rdev; 928962Sroot bp->b_bcount = nbytes; 93*39880Smckusick if ((bp->b_flags & B_READ) == 0) 94*39880Smckusick vp->v_numoutput++; 958962Sroot minphys(bp); 968962Sroot c = bp->b_bcount; 974033Swnj #ifdef TRACE 9837729Smckusick trace(TR_SWAPIO, vp, bp->b_blkno); 994033Swnj #endif 10037729Smckusick VOP_STRATEGY(bp); 10134215Sbostic /* pageout daemon doesn't wait for pushed pages */ 1028Sbill if (flag & B_DIRTY) { 1038Sbill if (c < nbytes) 1048Sbill panic("big push"); 10530750Skarels return (0); 10634215Sbostic } else { 10734215Sbostic s = splbio(); 10834215Sbostic while ((bp->b_flags & B_DONE) == 0) 10934215Sbostic sleep((caddr_t)bp, PSWP); 11034215Sbostic splx(s); 1118Sbill } 1128Sbill bp->b_un.b_addr += c; 1138Sbill bp->b_flags &= ~B_DONE; 1148Sbill if (bp->b_flags & B_ERROR) { 1158Sbill if ((flag & (B_UAREA|B_PAGET)) || rdflg == B_WRITE) 1168Sbill panic("hard IO err in swap"); 11718319Smckusick swkill(p, "swap: read error from swap device"); 11826004Smckusick error = EIO; 1198Sbill } 1208Sbill nbytes -= c; 12112647Ssam dblkno += btodb(c); 1228Sbill } 1238Sbill bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS|B_PAGET|B_UAREA|B_DIRTY); 12434215Sbostic freeswbuf(bp); 12526004Smckusick return (error); 1268Sbill } 1278Sbill 1288Sbill /* 12912491Ssam * Put a buffer on the clean list after I/O is done. 13012491Ssam * Called from biodone. 13112491Ssam */ 13212491Ssam swdone(bp) 13312491Ssam register struct buf *bp; 13412491Ssam { 13512491Ssam register int s; 13612491Ssam 13712491Ssam if (bp->b_flags & B_ERROR) 13812491Ssam panic("IO err in push"); 13926315Skarels s = splbio(); 14012491Ssam bp->av_forw = bclnlist; 14112491Ssam cnt.v_pgout++; 14212491Ssam cnt.v_pgpgout += bp->b_bcount / NBPG; 14312491Ssam bclnlist = bp; 14412491Ssam if (bswlist.b_flags & B_WANTED) 14512491Ssam wakeup((caddr_t)&proc[2]); 14612491Ssam splx(s); 14712491Ssam } 14812491Ssam 14912491Ssam /* 1508Sbill * If rout == 0 then killed on swap error, else 1518Sbill * rout is the name of the routine where we ran out of 1528Sbill * swap space. 1538Sbill */ 1548Sbill swkill(p, rout) 1558Sbill struct proc *p; 1568Sbill char *rout; 1578Sbill { 1588Sbill 15924448Sbloom printf("pid %d: %s\n", p->p_pid, rout); 16024448Sbloom uprintf("sorry, pid %d was killed in %s\n", p->p_pid, rout); 1618Sbill /* 1628Sbill * To be sure no looping (e.g. in vmsched trying to 1638Sbill * swap out) mark process locked in core (as though 1648Sbill * done by user) after killing it so noone will try 1658Sbill * to swap it out. 1668Sbill */ 167165Sbill psignal(p, SIGKILL); 1688Sbill p->p_flag |= SULOCK; 1698Sbill } 1708Sbill 1718Sbill /* 1728Sbill * Raw I/O. The arguments are 1738Sbill * The strategy routine for the device 17434215Sbostic * A buffer, which will either be a special buffer header owned 17534215Sbostic * exclusively by the device for this purpose, or NULL, 17634215Sbostic * indicating that we should use a swap buffer 1778Sbill * The device number 1788Sbill * Read/write flag 1798Sbill * Essentially all the work is computing physical addresses and 1808Sbill * validating them. 1818Sbill * If the user has the proper access privilidges, the process is 1828Sbill * marked 'delayed unlock' and the pages involved in the I/O are 1838Sbill * faulted and locked. After the completion of the I/O, the above pages 1848Sbill * are unlocked. 1858Sbill */ 1867724Swnj physio(strat, bp, dev, rw, mincnt, uio) 1877724Swnj int (*strat)(); 1887724Swnj register struct buf *bp; 1897724Swnj dev_t dev; 1907724Swnj int rw; 19134215Sbostic u_int (*mincnt)(); 1927724Swnj struct uio *uio; 1938Sbill { 19417313Skarels register struct iovec *iov; 19538794Skarels register int requested, done; 1968Sbill char *a; 19734215Sbostic int s, allocbuf = 0, error = 0; 19834215Sbostic struct buf *getswbuf(); 1998Sbill 20034215Sbostic if (bp == NULL) { 20134215Sbostic allocbuf = 1; 20234215Sbostic bp = getswbuf(PRIBIO+1); 20334215Sbostic } 20434215Sbostic for (; uio->uio_iovcnt; uio->uio_iov++, uio->uio_iovcnt--) { 20530750Skarels iov = uio->uio_iov; 20634215Sbostic if (!useracc(iov->iov_base, (u_int)iov->iov_len, 20734215Sbostic rw == B_READ ? B_WRITE : B_READ)) { 20834215Sbostic error = EFAULT; 20934215Sbostic break; 21030750Skarels } 21134215Sbostic if (!allocbuf) { /* only if sharing caller's buffer */ 21234215Sbostic s = splbio(); 21334215Sbostic while (bp->b_flags&B_BUSY) { 21434215Sbostic bp->b_flags |= B_WANTED; 21534215Sbostic sleep((caddr_t)bp, PRIBIO+1); 21634215Sbostic } 21734215Sbostic splx(s); 21834215Sbostic } 21930750Skarels bp->b_error = 0; 22030750Skarels bp->b_proc = u.u_procp; 22130750Skarels bp->b_un.b_addr = iov->iov_base; 22230750Skarels while (iov->iov_len > 0) { 22334215Sbostic bp->b_flags = B_BUSY | B_PHYS | B_RAW | rw; 22430750Skarels bp->b_dev = dev; 22530750Skarels bp->b_blkno = btodb(uio->uio_offset); 22630750Skarels bp->b_bcount = iov->iov_len; 22730750Skarels (*mincnt)(bp); 22838794Skarels requested = bp->b_bcount; 22930750Skarels u.u_procp->p_flag |= SPHYSIO; 23038794Skarels vslock(a = bp->b_un.b_addr, requested); 23134215Sbostic (*strat)(bp); 23234215Sbostic s = splbio(); 23334215Sbostic while ((bp->b_flags & B_DONE) == 0) 23434215Sbostic sleep((caddr_t)bp, PRIBIO); 23538794Skarels vsunlock(a, requested, rw); 23630750Skarels u.u_procp->p_flag &= ~SPHYSIO; 23734215Sbostic if (bp->b_flags&B_WANTED) /* rare */ 23830750Skarels wakeup((caddr_t)bp); 23930750Skarels splx(s); 24038794Skarels done = bp->b_bcount - bp->b_resid; 24138794Skarels bp->b_un.b_addr += done; 24238794Skarels iov->iov_len -= done; 24338794Skarels uio->uio_resid -= done; 24438794Skarels uio->uio_offset += done; 24538794Skarels /* temp kludge for disk drives */ 24638794Skarels if (done < requested || bp->b_flags & B_ERROR) 24730750Skarels break; 24830750Skarels } 24934215Sbostic bp->b_flags &= ~(B_BUSY | B_WANTED | B_PHYS | B_RAW); 25037729Smckusick error = biowait(bp); 25138794Skarels /* temp kludge for disk drives */ 25238794Skarels if (done < requested || bp->b_flags & B_ERROR) 25334215Sbostic break; 2548Sbill } 25534215Sbostic if (allocbuf) 25634215Sbostic freeswbuf(bp); 25734215Sbostic return (error); 2588Sbill } 2598Sbill 26034215Sbostic u_int 2618Sbill minphys(bp) 2627724Swnj struct buf *bp; 2638Sbill { 26410400Ssam if (bp->b_bcount > MAXPHYS) 26510400Ssam bp->b_bcount = MAXPHYS; 2668Sbill } 26734215Sbostic 26834215Sbostic static 26934215Sbostic struct buf * 27034215Sbostic getswbuf(prio) 27134215Sbostic int prio; 27234215Sbostic { 27334215Sbostic int s; 27434215Sbostic struct buf *bp; 27534215Sbostic 27634215Sbostic s = splbio(); 27734215Sbostic while (bswlist.av_forw == NULL) { 27834215Sbostic bswlist.b_flags |= B_WANTED; 27934215Sbostic sleep((caddr_t)&bswlist, prio); 28034215Sbostic } 28134215Sbostic bp = bswlist.av_forw; 28234215Sbostic bswlist.av_forw = bp->av_forw; 28334215Sbostic splx(s); 28434215Sbostic return (bp); 28534215Sbostic } 28634215Sbostic 28734215Sbostic static 28834215Sbostic freeswbuf(bp) 28934215Sbostic struct buf *bp; 29034215Sbostic { 29134215Sbostic int s; 29234215Sbostic 29334215Sbostic s = splbio(); 29434215Sbostic bp->av_forw = bswlist.av_forw; 29534215Sbostic bswlist.av_forw = bp; 29639148Smckusick if (bp->b_vp) 29739148Smckusick brelvp(bp); 29834215Sbostic if (bswlist.b_flags & B_WANTED) { 29934215Sbostic bswlist.b_flags &= ~B_WANTED; 30034215Sbostic wakeup((caddr_t)&bswlist); 30134215Sbostic wakeup((caddr_t)&proc[2]); 30234215Sbostic } 30334215Sbostic splx(s); 30434215Sbostic } 30534215Sbostic 30634215Sbostic rawread(dev, uio) 30734215Sbostic dev_t dev; 30834215Sbostic struct uio *uio; 30934215Sbostic { 31034215Sbostic return (physio(cdevsw[major(dev)].d_strategy, (struct buf *)NULL, 31134215Sbostic dev, B_READ, minphys, uio)); 31234215Sbostic } 31334215Sbostic 31434215Sbostic rawwrite(dev, uio) 31534215Sbostic dev_t dev; 31634215Sbostic struct uio *uio; 31734215Sbostic { 31834215Sbostic return (physio(cdevsw[major(dev)].d_strategy, (struct buf *)NULL, 31934215Sbostic dev, B_WRITE, minphys, uio)); 32034215Sbostic } 321