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*37729Smckusick * @(#)kern_physio.c 7.6 (Berkeley) 05/09/89 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" 19*37729Smckusick #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 */ 47*37729Smckusick 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; 52*37729Smckusick 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) { 86*37729Smckusick bp->b_blkno = dblkno; 87*37729Smckusick bp->b_dev = vp->v_rdev; 88*37729Smckusick if (bp->b_vp) 89*37729Smckusick brelvp(bp); 90*37729Smckusick vp->v_count++; 91*37729Smckusick bp->b_vp = vp; 928962Sroot bp->b_bcount = nbytes; 938962Sroot minphys(bp); 948962Sroot c = bp->b_bcount; 954033Swnj #ifdef TRACE 96*37729Smckusick trace(TR_SWAPIO, vp, bp->b_blkno); 974033Swnj #endif 98*37729Smckusick VOP_STRATEGY(bp); 9934215Sbostic /* pageout daemon doesn't wait for pushed pages */ 1008Sbill if (flag & B_DIRTY) { 1018Sbill if (c < nbytes) 1028Sbill panic("big push"); 10330750Skarels return (0); 10434215Sbostic } else { 10534215Sbostic s = splbio(); 10634215Sbostic while ((bp->b_flags & B_DONE) == 0) 10734215Sbostic sleep((caddr_t)bp, PSWP); 10834215Sbostic splx(s); 1098Sbill } 1108Sbill bp->b_un.b_addr += c; 1118Sbill bp->b_flags &= ~B_DONE; 1128Sbill if (bp->b_flags & B_ERROR) { 1138Sbill if ((flag & (B_UAREA|B_PAGET)) || rdflg == B_WRITE) 1148Sbill panic("hard IO err in swap"); 11518319Smckusick swkill(p, "swap: read error from swap device"); 11626004Smckusick error = EIO; 1178Sbill } 1188Sbill nbytes -= c; 11912647Ssam dblkno += btodb(c); 1208Sbill } 1218Sbill bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS|B_PAGET|B_UAREA|B_DIRTY); 12234215Sbostic freeswbuf(bp); 12326004Smckusick return (error); 1248Sbill } 1258Sbill 1268Sbill /* 12712491Ssam * Put a buffer on the clean list after I/O is done. 12812491Ssam * Called from biodone. 12912491Ssam */ 13012491Ssam swdone(bp) 13112491Ssam register struct buf *bp; 13212491Ssam { 13312491Ssam register int s; 13412491Ssam 13512491Ssam if (bp->b_flags & B_ERROR) 13612491Ssam panic("IO err in push"); 13726315Skarels s = splbio(); 13812491Ssam bp->av_forw = bclnlist; 13912491Ssam cnt.v_pgout++; 14012491Ssam cnt.v_pgpgout += bp->b_bcount / NBPG; 14112491Ssam bclnlist = bp; 14212491Ssam if (bswlist.b_flags & B_WANTED) 14312491Ssam wakeup((caddr_t)&proc[2]); 14412491Ssam splx(s); 14512491Ssam } 14612491Ssam 14712491Ssam /* 1488Sbill * If rout == 0 then killed on swap error, else 1498Sbill * rout is the name of the routine where we ran out of 1508Sbill * swap space. 1518Sbill */ 1528Sbill swkill(p, rout) 1538Sbill struct proc *p; 1548Sbill char *rout; 1558Sbill { 1568Sbill 15724448Sbloom printf("pid %d: %s\n", p->p_pid, rout); 15824448Sbloom uprintf("sorry, pid %d was killed in %s\n", p->p_pid, rout); 1598Sbill /* 1608Sbill * To be sure no looping (e.g. in vmsched trying to 1618Sbill * swap out) mark process locked in core (as though 1628Sbill * done by user) after killing it so noone will try 1638Sbill * to swap it out. 1648Sbill */ 165165Sbill psignal(p, SIGKILL); 1668Sbill p->p_flag |= SULOCK; 1678Sbill } 1688Sbill 1698Sbill /* 1708Sbill * Raw I/O. The arguments are 1718Sbill * The strategy routine for the device 17234215Sbostic * A buffer, which will either be a special buffer header owned 17334215Sbostic * exclusively by the device for this purpose, or NULL, 17434215Sbostic * indicating that we should use a swap buffer 1758Sbill * The device number 1768Sbill * Read/write flag 1778Sbill * Essentially all the work is computing physical addresses and 1788Sbill * validating them. 1798Sbill * If the user has the proper access privilidges, the process is 1808Sbill * marked 'delayed unlock' and the pages involved in the I/O are 1818Sbill * faulted and locked. After the completion of the I/O, the above pages 1828Sbill * are unlocked. 1838Sbill */ 1847724Swnj physio(strat, bp, dev, rw, mincnt, uio) 1857724Swnj int (*strat)(); 1867724Swnj register struct buf *bp; 1877724Swnj dev_t dev; 1887724Swnj int rw; 18934215Sbostic u_int (*mincnt)(); 1907724Swnj struct uio *uio; 1918Sbill { 19217313Skarels register struct iovec *iov; 1938Sbill register int c; 1948Sbill char *a; 19534215Sbostic int s, allocbuf = 0, error = 0; 19634215Sbostic struct buf *getswbuf(); 1978Sbill 19834215Sbostic if (bp == NULL) { 19934215Sbostic allocbuf = 1; 20034215Sbostic bp = getswbuf(PRIBIO+1); 20134215Sbostic } 20234215Sbostic for (; uio->uio_iovcnt; uio->uio_iov++, uio->uio_iovcnt--) { 20330750Skarels iov = uio->uio_iov; 20434215Sbostic if (!useracc(iov->iov_base, (u_int)iov->iov_len, 20534215Sbostic rw == B_READ ? B_WRITE : B_READ)) { 20634215Sbostic error = EFAULT; 20734215Sbostic break; 20830750Skarels } 20934215Sbostic if (!allocbuf) { /* only if sharing caller's buffer */ 21034215Sbostic s = splbio(); 21134215Sbostic while (bp->b_flags&B_BUSY) { 21234215Sbostic bp->b_flags |= B_WANTED; 21334215Sbostic sleep((caddr_t)bp, PRIBIO+1); 21434215Sbostic } 21534215Sbostic splx(s); 21634215Sbostic } 21730750Skarels bp->b_error = 0; 21830750Skarels bp->b_proc = u.u_procp; 21930750Skarels bp->b_un.b_addr = iov->iov_base; 22030750Skarels while (iov->iov_len > 0) { 22134215Sbostic bp->b_flags = B_BUSY | B_PHYS | B_RAW | rw; 22230750Skarels bp->b_dev = dev; 22330750Skarels bp->b_blkno = btodb(uio->uio_offset); 22430750Skarels bp->b_bcount = iov->iov_len; 22530750Skarels (*mincnt)(bp); 22630750Skarels c = bp->b_bcount; 22730750Skarels u.u_procp->p_flag |= SPHYSIO; 22830750Skarels vslock(a = bp->b_un.b_addr, c); 22934215Sbostic (*strat)(bp); 23034215Sbostic s = splbio(); 23134215Sbostic while ((bp->b_flags & B_DONE) == 0) 23234215Sbostic sleep((caddr_t)bp, PRIBIO); 23330750Skarels vsunlock(a, c, rw); 23430750Skarels u.u_procp->p_flag &= ~SPHYSIO; 23534215Sbostic if (bp->b_flags&B_WANTED) /* rare */ 23630750Skarels wakeup((caddr_t)bp); 23730750Skarels splx(s); 23830750Skarels c -= bp->b_resid; 23930750Skarels bp->b_un.b_addr += c; 24030750Skarels iov->iov_len -= c; 24130750Skarels uio->uio_resid -= c; 24230750Skarels uio->uio_offset += c; 24330750Skarels /* temp kludge for tape drives */ 24430750Skarels if (bp->b_resid || (bp->b_flags&B_ERROR)) 24530750Skarels break; 24630750Skarels } 24734215Sbostic bp->b_flags &= ~(B_BUSY | B_WANTED | B_PHYS | B_RAW); 248*37729Smckusick error = biowait(bp); 2499766Ssam /* temp kludge for tape drives */ 25030750Skarels if (bp->b_resid || error) 25134215Sbostic break; 2528Sbill } 25334215Sbostic if (allocbuf) 25434215Sbostic freeswbuf(bp); 25534215Sbostic return (error); 2568Sbill } 2578Sbill 25834215Sbostic u_int 2598Sbill minphys(bp) 2607724Swnj struct buf *bp; 2618Sbill { 26210400Ssam if (bp->b_bcount > MAXPHYS) 26310400Ssam bp->b_bcount = MAXPHYS; 2648Sbill } 26534215Sbostic 26634215Sbostic static 26734215Sbostic struct buf * 26834215Sbostic getswbuf(prio) 26934215Sbostic int prio; 27034215Sbostic { 27134215Sbostic int s; 27234215Sbostic struct buf *bp; 27334215Sbostic 27434215Sbostic s = splbio(); 27534215Sbostic while (bswlist.av_forw == NULL) { 27634215Sbostic bswlist.b_flags |= B_WANTED; 27734215Sbostic sleep((caddr_t)&bswlist, prio); 27834215Sbostic } 27934215Sbostic bp = bswlist.av_forw; 28034215Sbostic bswlist.av_forw = bp->av_forw; 28134215Sbostic splx(s); 28234215Sbostic return (bp); 28334215Sbostic } 28434215Sbostic 28534215Sbostic static 28634215Sbostic freeswbuf(bp) 28734215Sbostic struct buf *bp; 28834215Sbostic { 28934215Sbostic int s; 29034215Sbostic 29134215Sbostic s = splbio(); 29234215Sbostic bp->av_forw = bswlist.av_forw; 29334215Sbostic bswlist.av_forw = bp; 29434215Sbostic if (bswlist.b_flags & B_WANTED) { 29534215Sbostic bswlist.b_flags &= ~B_WANTED; 29634215Sbostic wakeup((caddr_t)&bswlist); 29734215Sbostic wakeup((caddr_t)&proc[2]); 29834215Sbostic } 29934215Sbostic splx(s); 30034215Sbostic } 30134215Sbostic 30234215Sbostic rawread(dev, uio) 30334215Sbostic dev_t dev; 30434215Sbostic struct uio *uio; 30534215Sbostic { 30634215Sbostic return (physio(cdevsw[major(dev)].d_strategy, (struct buf *)NULL, 30734215Sbostic dev, B_READ, minphys, uio)); 30834215Sbostic } 30934215Sbostic 31034215Sbostic rawwrite(dev, uio) 31134215Sbostic dev_t dev; 31234215Sbostic struct uio *uio; 31334215Sbostic { 31434215Sbostic return (physio(cdevsw[major(dev)].d_strategy, (struct buf *)NULL, 31534215Sbostic dev, B_WRITE, minphys, uio)); 31634215Sbostic } 317