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*42001Smckusick * @(#)kern_physio.c 7.14 (Berkeley) 05/15/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" 2040652Smckusick #include "specdev.h" 218Sbill 2237521Smckusick #include "machine/pte.h" 2337521Smckusick 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 */ 4837729Smckusick swap(p, dblkno, addr, nbytes, rdflg, flag, vp, pfcent) 498Sbill struct proc *p; 508Sbill swblk_t dblkno; 518Sbill caddr_t addr; 528674S int nbytes, rdflg, flag; 5337729Smckusick struct vnode *vp; 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) { 8737729Smckusick bp->b_blkno = dblkno; 8839148Smckusick if (bp->b_vp) 8939148Smckusick brelvp(bp); 9039807Smckusick VHOLD(vp); 9139148Smckusick bp->b_vp = vp; 9239148Smckusick bp->b_dev = vp->v_rdev; 938962Sroot bp->b_bcount = nbytes; 9439880Smckusick if ((bp->b_flags & B_READ) == 0) 9539880Smckusick vp->v_numoutput++; 968962Sroot minphys(bp); 978962Sroot c = bp->b_bcount; 984033Swnj #ifdef TRACE 9937729Smckusick trace(TR_SWAPIO, vp, bp->b_blkno); 1004033Swnj #endif 101*42001Smckusick #if defined(hp300) 102*42001Smckusick vmapbuf(bp); 103*42001Smckusick #endif 10437729Smckusick VOP_STRATEGY(bp); 10534215Sbostic /* pageout daemon doesn't wait for pushed pages */ 1068Sbill if (flag & B_DIRTY) { 1078Sbill if (c < nbytes) 1088Sbill panic("big push"); 10930750Skarels return (0); 11034215Sbostic } else { 11134215Sbostic s = splbio(); 11234215Sbostic while ((bp->b_flags & B_DONE) == 0) 11334215Sbostic sleep((caddr_t)bp, PSWP); 11434215Sbostic splx(s); 1158Sbill } 116*42001Smckusick #if defined(hp300) 117*42001Smckusick vunmapbuf(bp); 118*42001Smckusick #endif 1198Sbill bp->b_un.b_addr += c; 1208Sbill bp->b_flags &= ~B_DONE; 1218Sbill if (bp->b_flags & B_ERROR) { 1228Sbill if ((flag & (B_UAREA|B_PAGET)) || rdflg == B_WRITE) 1238Sbill panic("hard IO err in swap"); 12418319Smckusick swkill(p, "swap: read error from swap device"); 12526004Smckusick error = EIO; 1268Sbill } 1278Sbill nbytes -= c; 12812647Ssam dblkno += btodb(c); 1298Sbill } 1308Sbill bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS|B_PAGET|B_UAREA|B_DIRTY); 13134215Sbostic freeswbuf(bp); 13226004Smckusick return (error); 1338Sbill } 1348Sbill 1358Sbill /* 13612491Ssam * Put a buffer on the clean list after I/O is done. 13712491Ssam * Called from biodone. 13812491Ssam */ 13912491Ssam swdone(bp) 14012491Ssam register struct buf *bp; 14112491Ssam { 14212491Ssam register int s; 14312491Ssam 14412491Ssam if (bp->b_flags & B_ERROR) 14512491Ssam panic("IO err in push"); 14626315Skarels s = splbio(); 14712491Ssam bp->av_forw = bclnlist; 14812491Ssam cnt.v_pgout++; 14912491Ssam cnt.v_pgpgout += bp->b_bcount / NBPG; 15012491Ssam bclnlist = bp; 15112491Ssam if (bswlist.b_flags & B_WANTED) 15212491Ssam wakeup((caddr_t)&proc[2]); 153*42001Smckusick #if defined(hp300) 154*42001Smckusick vunmapbuf(bp); 155*42001Smckusick #endif 15612491Ssam splx(s); 15712491Ssam } 15812491Ssam 15912491Ssam /* 1608Sbill * If rout == 0 then killed on swap error, else 1618Sbill * rout is the name of the routine where we ran out of 1628Sbill * swap space. 1638Sbill */ 1648Sbill swkill(p, rout) 1658Sbill struct proc *p; 1668Sbill char *rout; 1678Sbill { 1688Sbill 16924448Sbloom printf("pid %d: %s\n", p->p_pid, rout); 17024448Sbloom uprintf("sorry, pid %d was killed in %s\n", p->p_pid, rout); 1718Sbill /* 1728Sbill * To be sure no looping (e.g. in vmsched trying to 1738Sbill * swap out) mark process locked in core (as though 1748Sbill * done by user) after killing it so noone will try 1758Sbill * to swap it out. 1768Sbill */ 177165Sbill psignal(p, SIGKILL); 1788Sbill p->p_flag |= SULOCK; 1798Sbill } 1808Sbill 1818Sbill /* 1828Sbill * Raw I/O. The arguments are 1838Sbill * The strategy routine for the device 18434215Sbostic * A buffer, which will either be a special buffer header owned 18534215Sbostic * exclusively by the device for this purpose, or NULL, 18634215Sbostic * indicating that we should use a swap buffer 1878Sbill * The device number 1888Sbill * Read/write flag 1898Sbill * Essentially all the work is computing physical addresses and 1908Sbill * validating them. 1918Sbill * If the user has the proper access privilidges, the process is 1928Sbill * marked 'delayed unlock' and the pages involved in the I/O are 1938Sbill * faulted and locked. After the completion of the I/O, the above pages 1948Sbill * are unlocked. 1958Sbill */ 1967724Swnj physio(strat, bp, dev, rw, mincnt, uio) 1977724Swnj int (*strat)(); 1987724Swnj register struct buf *bp; 1997724Swnj dev_t dev; 2007724Swnj int rw; 20134215Sbostic u_int (*mincnt)(); 2027724Swnj struct uio *uio; 2038Sbill { 20417313Skarels register struct iovec *iov; 20538794Skarels register int requested, done; 2068Sbill char *a; 20734215Sbostic int s, allocbuf = 0, error = 0; 20834215Sbostic struct buf *getswbuf(); 2098Sbill 21034215Sbostic if (bp == NULL) { 21134215Sbostic allocbuf = 1; 21234215Sbostic bp = getswbuf(PRIBIO+1); 21334215Sbostic } 21434215Sbostic for (; uio->uio_iovcnt; uio->uio_iov++, uio->uio_iovcnt--) { 21530750Skarels iov = uio->uio_iov; 21634215Sbostic if (!useracc(iov->iov_base, (u_int)iov->iov_len, 21734215Sbostic rw == B_READ ? B_WRITE : B_READ)) { 21834215Sbostic error = EFAULT; 21934215Sbostic break; 22030750Skarels } 22134215Sbostic if (!allocbuf) { /* only if sharing caller's buffer */ 22234215Sbostic s = splbio(); 22334215Sbostic while (bp->b_flags&B_BUSY) { 22434215Sbostic bp->b_flags |= B_WANTED; 22534215Sbostic sleep((caddr_t)bp, PRIBIO+1); 22634215Sbostic } 22734215Sbostic splx(s); 22834215Sbostic } 22930750Skarels bp->b_error = 0; 23030750Skarels bp->b_proc = u.u_procp; 231*42001Smckusick #ifdef HPUXCOMPAT 232*42001Smckusick if (ISHPMMADDR(iov->iov_base)) 233*42001Smckusick bp->b_un.b_addr = (caddr_t)HPMMBASEADDR(iov->iov_base); 234*42001Smckusick else 235*42001Smckusick #endif 23630750Skarels bp->b_un.b_addr = iov->iov_base; 23730750Skarels while (iov->iov_len > 0) { 23834215Sbostic bp->b_flags = B_BUSY | B_PHYS | B_RAW | rw; 23930750Skarels bp->b_dev = dev; 24030750Skarels bp->b_blkno = btodb(uio->uio_offset); 24130750Skarels bp->b_bcount = iov->iov_len; 24230750Skarels (*mincnt)(bp); 24338794Skarels requested = bp->b_bcount; 24430750Skarels u.u_procp->p_flag |= SPHYSIO; 24538794Skarels vslock(a = bp->b_un.b_addr, requested); 246*42001Smckusick #if defined(hp300) 247*42001Smckusick vmapbuf(bp); 248*42001Smckusick #endif 24934215Sbostic (*strat)(bp); 25034215Sbostic s = splbio(); 25134215Sbostic while ((bp->b_flags & B_DONE) == 0) 25234215Sbostic sleep((caddr_t)bp, PRIBIO); 253*42001Smckusick #if defined(hp300) 254*42001Smckusick vunmapbuf(bp); 255*42001Smckusick #endif 25638794Skarels vsunlock(a, requested, rw); 25730750Skarels u.u_procp->p_flag &= ~SPHYSIO; 25834215Sbostic if (bp->b_flags&B_WANTED) /* rare */ 25930750Skarels wakeup((caddr_t)bp); 26030750Skarels splx(s); 26138794Skarels done = bp->b_bcount - bp->b_resid; 26238794Skarels bp->b_un.b_addr += done; 26338794Skarels iov->iov_len -= done; 26438794Skarels uio->uio_resid -= done; 26538794Skarels uio->uio_offset += done; 26638794Skarels /* temp kludge for disk drives */ 26738794Skarels if (done < requested || bp->b_flags & B_ERROR) 26830750Skarels break; 26930750Skarels } 27034215Sbostic bp->b_flags &= ~(B_BUSY | B_WANTED | B_PHYS | B_RAW); 27137729Smckusick error = biowait(bp); 27238794Skarels /* temp kludge for disk drives */ 27338794Skarels if (done < requested || bp->b_flags & B_ERROR) 27434215Sbostic break; 2758Sbill } 276*42001Smckusick #if defined(hp300) 277*42001Smckusick DCIU(); 278*42001Smckusick #endif 27934215Sbostic if (allocbuf) 28034215Sbostic freeswbuf(bp); 28134215Sbostic return (error); 2828Sbill } 2838Sbill 28434215Sbostic u_int 2858Sbill minphys(bp) 2867724Swnj struct buf *bp; 2878Sbill { 28810400Ssam if (bp->b_bcount > MAXPHYS) 28910400Ssam bp->b_bcount = MAXPHYS; 2908Sbill } 29134215Sbostic 29234215Sbostic static 29334215Sbostic struct buf * 29434215Sbostic getswbuf(prio) 29534215Sbostic int prio; 29634215Sbostic { 29734215Sbostic int s; 29834215Sbostic struct buf *bp; 29934215Sbostic 30034215Sbostic s = splbio(); 30134215Sbostic while (bswlist.av_forw == NULL) { 30234215Sbostic bswlist.b_flags |= B_WANTED; 30334215Sbostic sleep((caddr_t)&bswlist, prio); 30434215Sbostic } 30534215Sbostic bp = bswlist.av_forw; 30634215Sbostic bswlist.av_forw = bp->av_forw; 30734215Sbostic splx(s); 30834215Sbostic return (bp); 30934215Sbostic } 31034215Sbostic 31134215Sbostic static 31234215Sbostic freeswbuf(bp) 31334215Sbostic struct buf *bp; 31434215Sbostic { 31534215Sbostic int s; 31634215Sbostic 31734215Sbostic s = splbio(); 31834215Sbostic bp->av_forw = bswlist.av_forw; 31934215Sbostic bswlist.av_forw = bp; 32039148Smckusick if (bp->b_vp) 32139148Smckusick brelvp(bp); 32234215Sbostic if (bswlist.b_flags & B_WANTED) { 32334215Sbostic bswlist.b_flags &= ~B_WANTED; 32434215Sbostic wakeup((caddr_t)&bswlist); 32534215Sbostic wakeup((caddr_t)&proc[2]); 32634215Sbostic } 32734215Sbostic splx(s); 32834215Sbostic } 32934215Sbostic 33034215Sbostic rawread(dev, uio) 33134215Sbostic dev_t dev; 33234215Sbostic struct uio *uio; 33334215Sbostic { 33434215Sbostic return (physio(cdevsw[major(dev)].d_strategy, (struct buf *)NULL, 33534215Sbostic dev, B_READ, minphys, uio)); 33634215Sbostic } 33734215Sbostic 33834215Sbostic rawwrite(dev, uio) 33934215Sbostic dev_t dev; 34034215Sbostic struct uio *uio; 34134215Sbostic { 34234215Sbostic return (physio(cdevsw[major(dev)].d_strategy, (struct buf *)NULL, 34334215Sbostic dev, B_WRITE, minphys, uio)); 34434215Sbostic } 345