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*40652Smckusick * @(#)kern_physio.c 7.13 (Berkeley) 03/27/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" 20*40652Smckusick #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 10137729Smckusick VOP_STRATEGY(bp); 10234215Sbostic /* pageout daemon doesn't wait for pushed pages */ 1038Sbill if (flag & B_DIRTY) { 1048Sbill if (c < nbytes) 1058Sbill panic("big push"); 10630750Skarels return (0); 10734215Sbostic } else { 10834215Sbostic s = splbio(); 10934215Sbostic while ((bp->b_flags & B_DONE) == 0) 11034215Sbostic sleep((caddr_t)bp, PSWP); 11134215Sbostic splx(s); 1128Sbill } 1138Sbill bp->b_un.b_addr += c; 1148Sbill bp->b_flags &= ~B_DONE; 1158Sbill if (bp->b_flags & B_ERROR) { 1168Sbill if ((flag & (B_UAREA|B_PAGET)) || rdflg == B_WRITE) 1178Sbill panic("hard IO err in swap"); 11818319Smckusick swkill(p, "swap: read error from swap device"); 11926004Smckusick error = EIO; 1208Sbill } 1218Sbill nbytes -= c; 12212647Ssam dblkno += btodb(c); 1238Sbill } 1248Sbill bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS|B_PAGET|B_UAREA|B_DIRTY); 12534215Sbostic freeswbuf(bp); 12626004Smckusick return (error); 1278Sbill } 1288Sbill 1298Sbill /* 13012491Ssam * Put a buffer on the clean list after I/O is done. 13112491Ssam * Called from biodone. 13212491Ssam */ 13312491Ssam swdone(bp) 13412491Ssam register struct buf *bp; 13512491Ssam { 13612491Ssam register int s; 13712491Ssam 13812491Ssam if (bp->b_flags & B_ERROR) 13912491Ssam panic("IO err in push"); 14026315Skarels s = splbio(); 14112491Ssam bp->av_forw = bclnlist; 14212491Ssam cnt.v_pgout++; 14312491Ssam cnt.v_pgpgout += bp->b_bcount / NBPG; 14412491Ssam bclnlist = bp; 14512491Ssam if (bswlist.b_flags & B_WANTED) 14612491Ssam wakeup((caddr_t)&proc[2]); 14712491Ssam splx(s); 14812491Ssam } 14912491Ssam 15012491Ssam /* 1518Sbill * If rout == 0 then killed on swap error, else 1528Sbill * rout is the name of the routine where we ran out of 1538Sbill * swap space. 1548Sbill */ 1558Sbill swkill(p, rout) 1568Sbill struct proc *p; 1578Sbill char *rout; 1588Sbill { 1598Sbill 16024448Sbloom printf("pid %d: %s\n", p->p_pid, rout); 16124448Sbloom uprintf("sorry, pid %d was killed in %s\n", p->p_pid, rout); 1628Sbill /* 1638Sbill * To be sure no looping (e.g. in vmsched trying to 1648Sbill * swap out) mark process locked in core (as though 1658Sbill * done by user) after killing it so noone will try 1668Sbill * to swap it out. 1678Sbill */ 168165Sbill psignal(p, SIGKILL); 1698Sbill p->p_flag |= SULOCK; 1708Sbill } 1718Sbill 1728Sbill /* 1738Sbill * Raw I/O. The arguments are 1748Sbill * The strategy routine for the device 17534215Sbostic * A buffer, which will either be a special buffer header owned 17634215Sbostic * exclusively by the device for this purpose, or NULL, 17734215Sbostic * indicating that we should use a swap buffer 1788Sbill * The device number 1798Sbill * Read/write flag 1808Sbill * Essentially all the work is computing physical addresses and 1818Sbill * validating them. 1828Sbill * If the user has the proper access privilidges, the process is 1838Sbill * marked 'delayed unlock' and the pages involved in the I/O are 1848Sbill * faulted and locked. After the completion of the I/O, the above pages 1858Sbill * are unlocked. 1868Sbill */ 1877724Swnj physio(strat, bp, dev, rw, mincnt, uio) 1887724Swnj int (*strat)(); 1897724Swnj register struct buf *bp; 1907724Swnj dev_t dev; 1917724Swnj int rw; 19234215Sbostic u_int (*mincnt)(); 1937724Swnj struct uio *uio; 1948Sbill { 19517313Skarels register struct iovec *iov; 19638794Skarels register int requested, done; 1978Sbill char *a; 19834215Sbostic int s, allocbuf = 0, error = 0; 19934215Sbostic struct buf *getswbuf(); 2008Sbill 20134215Sbostic if (bp == NULL) { 20234215Sbostic allocbuf = 1; 20334215Sbostic bp = getswbuf(PRIBIO+1); 20434215Sbostic } 20534215Sbostic for (; uio->uio_iovcnt; uio->uio_iov++, uio->uio_iovcnt--) { 20630750Skarels iov = uio->uio_iov; 20734215Sbostic if (!useracc(iov->iov_base, (u_int)iov->iov_len, 20834215Sbostic rw == B_READ ? B_WRITE : B_READ)) { 20934215Sbostic error = EFAULT; 21034215Sbostic break; 21130750Skarels } 21234215Sbostic if (!allocbuf) { /* only if sharing caller's buffer */ 21334215Sbostic s = splbio(); 21434215Sbostic while (bp->b_flags&B_BUSY) { 21534215Sbostic bp->b_flags |= B_WANTED; 21634215Sbostic sleep((caddr_t)bp, PRIBIO+1); 21734215Sbostic } 21834215Sbostic splx(s); 21934215Sbostic } 22030750Skarels bp->b_error = 0; 22130750Skarels bp->b_proc = u.u_procp; 22230750Skarels bp->b_un.b_addr = iov->iov_base; 22330750Skarels while (iov->iov_len > 0) { 22434215Sbostic bp->b_flags = B_BUSY | B_PHYS | B_RAW | rw; 22530750Skarels bp->b_dev = dev; 22630750Skarels bp->b_blkno = btodb(uio->uio_offset); 22730750Skarels bp->b_bcount = iov->iov_len; 22830750Skarels (*mincnt)(bp); 22938794Skarels requested = bp->b_bcount; 23030750Skarels u.u_procp->p_flag |= SPHYSIO; 23138794Skarels vslock(a = bp->b_un.b_addr, requested); 23234215Sbostic (*strat)(bp); 23334215Sbostic s = splbio(); 23434215Sbostic while ((bp->b_flags & B_DONE) == 0) 23534215Sbostic sleep((caddr_t)bp, PRIBIO); 23638794Skarels vsunlock(a, requested, rw); 23730750Skarels u.u_procp->p_flag &= ~SPHYSIO; 23834215Sbostic if (bp->b_flags&B_WANTED) /* rare */ 23930750Skarels wakeup((caddr_t)bp); 24030750Skarels splx(s); 24138794Skarels done = bp->b_bcount - bp->b_resid; 24238794Skarels bp->b_un.b_addr += done; 24338794Skarels iov->iov_len -= done; 24438794Skarels uio->uio_resid -= done; 24538794Skarels uio->uio_offset += done; 24638794Skarels /* temp kludge for disk drives */ 24738794Skarels if (done < requested || bp->b_flags & B_ERROR) 24830750Skarels break; 24930750Skarels } 25034215Sbostic bp->b_flags &= ~(B_BUSY | B_WANTED | B_PHYS | B_RAW); 25137729Smckusick error = biowait(bp); 25238794Skarels /* temp kludge for disk drives */ 25338794Skarels if (done < requested || bp->b_flags & B_ERROR) 25434215Sbostic break; 2558Sbill } 25634215Sbostic if (allocbuf) 25734215Sbostic freeswbuf(bp); 25834215Sbostic return (error); 2598Sbill } 2608Sbill 26134215Sbostic u_int 2628Sbill minphys(bp) 2637724Swnj struct buf *bp; 2648Sbill { 26510400Ssam if (bp->b_bcount > MAXPHYS) 26610400Ssam bp->b_bcount = MAXPHYS; 2678Sbill } 26834215Sbostic 26934215Sbostic static 27034215Sbostic struct buf * 27134215Sbostic getswbuf(prio) 27234215Sbostic int prio; 27334215Sbostic { 27434215Sbostic int s; 27534215Sbostic struct buf *bp; 27634215Sbostic 27734215Sbostic s = splbio(); 27834215Sbostic while (bswlist.av_forw == NULL) { 27934215Sbostic bswlist.b_flags |= B_WANTED; 28034215Sbostic sleep((caddr_t)&bswlist, prio); 28134215Sbostic } 28234215Sbostic bp = bswlist.av_forw; 28334215Sbostic bswlist.av_forw = bp->av_forw; 28434215Sbostic splx(s); 28534215Sbostic return (bp); 28634215Sbostic } 28734215Sbostic 28834215Sbostic static 28934215Sbostic freeswbuf(bp) 29034215Sbostic struct buf *bp; 29134215Sbostic { 29234215Sbostic int s; 29334215Sbostic 29434215Sbostic s = splbio(); 29534215Sbostic bp->av_forw = bswlist.av_forw; 29634215Sbostic bswlist.av_forw = bp; 29739148Smckusick if (bp->b_vp) 29839148Smckusick brelvp(bp); 29934215Sbostic if (bswlist.b_flags & B_WANTED) { 30034215Sbostic bswlist.b_flags &= ~B_WANTED; 30134215Sbostic wakeup((caddr_t)&bswlist); 30234215Sbostic wakeup((caddr_t)&proc[2]); 30334215Sbostic } 30434215Sbostic splx(s); 30534215Sbostic } 30634215Sbostic 30734215Sbostic rawread(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_READ, minphys, uio)); 31334215Sbostic } 31434215Sbostic 31534215Sbostic rawwrite(dev, uio) 31634215Sbostic dev_t dev; 31734215Sbostic struct uio *uio; 31834215Sbostic { 31934215Sbostic return (physio(cdevsw[major(dev)].d_strategy, (struct buf *)NULL, 32034215Sbostic dev, B_WRITE, minphys, uio)); 32134215Sbostic } 322