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*30750Skarels * @(#)kern_physio.c 7.2.1.1 (Berkeley) 04/02/87 723461Smckusick */ 88Sbill 99766Ssam #include "../machine/pte.h" 109766Ssam 1117108Sbloom #include "param.h" 1217108Sbloom #include "systm.h" 1317108Sbloom #include "dir.h" 1417108Sbloom #include "user.h" 1517108Sbloom #include "buf.h" 1617108Sbloom #include "conf.h" 1717108Sbloom #include "proc.h" 1817108Sbloom #include "seg.h" 1917108Sbloom #include "vm.h" 2017108Sbloom #include "trace.h" 2117108Sbloom #include "map.h" 2217108Sbloom #include "uio.h" 23*30750Skarels #ifdef SECSIZE 24*30750Skarels #include "file.h" 25*30750Skarels #include "ioctl.h" 26*30750Skarels #include "disklabel.h" 27*30750Skarels #endif SECSIZE 288Sbill 2991Sbill /* 308Sbill * Swap IO headers - 318Sbill * They contain the necessary information for the swap I/O. 328Sbill * At any given time, a swap header can be in three 338Sbill * different lists. When free it is in the free list, 348Sbill * when allocated and the I/O queued, it is on the swap 358Sbill * device list, and finally, if the operation was a dirty 368Sbill * page push, when the I/O completes, it is inserted 378Sbill * in a list of cleaned pages to be processed by the pageout daemon. 388Sbill */ 392771Swnj struct buf *swbuf; 408Sbill 418Sbill /* 428Sbill * swap I/O - 438Sbill * 448Sbill * If the flag indicates a dirty page push initiated 458Sbill * by the pageout daemon, we map the page into the i th 468Sbill * virtual page of process 2 (the daemon itself) where i is 478Sbill * the index of the swap header that has been allocated. 488Sbill * We simply initialize the header and queue the I/O but 498Sbill * do not wait for completion. When the I/O completes, 50*30750Skarels * biodone() will link the header to a list of cleaned 518Sbill * pages to be processed by the pageout daemon. 528Sbill */ 538Sbill swap(p, dblkno, addr, nbytes, rdflg, flag, dev, pfcent) 548Sbill struct proc *p; 558Sbill swblk_t dblkno; 568Sbill caddr_t addr; 578674S int nbytes, rdflg, flag; 588Sbill dev_t dev; 598674S u_int pfcent; 608Sbill { 618Sbill register struct buf *bp; 628962Sroot register u_int c; 638Sbill int p2dp; 648Sbill register struct pte *dpte, *vpte; 655431Sroot int s; 6612491Ssam extern swdone(); 6726004Smckusick int error = 0; 688Sbill 6926315Skarels s = splbio(); 708Sbill while (bswlist.av_forw == NULL) { 718Sbill bswlist.b_flags |= B_WANTED; 728Sbill sleep((caddr_t)&bswlist, PSWP+1); 738Sbill } 748Sbill bp = bswlist.av_forw; 758Sbill bswlist.av_forw = bp->av_forw; 765431Sroot splx(s); 778Sbill 788Sbill bp->b_flags = B_BUSY | B_PHYS | rdflg | flag; 79*30750Skarels #ifdef SECSIZE 80*30750Skarels bp->b_blksize = DEV_BSIZE; 81*30750Skarels #endif SECSIZE 828Sbill if ((bp->b_flags & (B_DIRTY|B_PGIN)) == 0) 838Sbill if (rdflg == B_READ) 848Sbill sum.v_pswpin += btoc(nbytes); 858Sbill else 868Sbill sum.v_pswpout += btoc(nbytes); 878Sbill bp->b_proc = p; 888Sbill if (flag & B_DIRTY) { 898Sbill p2dp = ((bp - swbuf) * CLSIZE) * KLMAX; 908Sbill dpte = dptopte(&proc[2], p2dp); 918Sbill vpte = vtopte(p, btop(addr)); 928Sbill for (c = 0; c < nbytes; c += NBPG) { 938Sbill if (vpte->pg_pfnum == 0 || vpte->pg_fod) 948Sbill panic("swap bad pte"); 958Sbill *dpte++ = *vpte++; 968Sbill } 9712491Ssam bp->b_un.b_addr = (caddr_t)ctob(dptov(&proc[2], p2dp)); 9812491Ssam bp->b_flags |= B_CALL; 9912491Ssam bp->b_iodone = swdone; 10012491Ssam bp->b_pfcent = pfcent; 1018Sbill } else 1028Sbill bp->b_un.b_addr = addr; 1038Sbill while (nbytes > 0) { 1048962Sroot bp->b_bcount = nbytes; 1058962Sroot minphys(bp); 1068962Sroot c = bp->b_bcount; 1078Sbill bp->b_blkno = dblkno; 1088Sbill bp->b_dev = dev; 1094033Swnj #ifdef TRACE 1104033Swnj trace(TR_SWAPIO, dev, bp->b_blkno); 1114033Swnj #endif 1129011Sroot physstrat(bp, bdevsw[major(dev)].d_strategy, PSWP); 1138Sbill if (flag & B_DIRTY) { 1148Sbill if (c < nbytes) 1158Sbill panic("big push"); 116*30750Skarels return (0); 1178Sbill } 1188Sbill bp->b_un.b_addr += c; 1198Sbill bp->b_flags &= ~B_DONE; 1208Sbill if (bp->b_flags & B_ERROR) { 1218Sbill if ((flag & (B_UAREA|B_PAGET)) || rdflg == B_WRITE) 1228Sbill panic("hard IO err in swap"); 12318319Smckusick swkill(p, "swap: read error from swap device"); 12426004Smckusick error = EIO; 1258Sbill } 1268Sbill nbytes -= c; 127*30750Skarels #ifdef SECSIZE 128*30750Skarels if (flag & B_PGIN && nbytes > 0) 129*30750Skarels panic("big pgin"); 130*30750Skarels #endif SECSIZE 13112647Ssam dblkno += btodb(c); 1328Sbill } 13326315Skarels s = splbio(); 1348Sbill bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS|B_PAGET|B_UAREA|B_DIRTY); 1358Sbill bp->av_forw = bswlist.av_forw; 1368Sbill bswlist.av_forw = bp; 1378Sbill if (bswlist.b_flags & B_WANTED) { 1388Sbill bswlist.b_flags &= ~B_WANTED; 1398Sbill wakeup((caddr_t)&bswlist); 1408Sbill wakeup((caddr_t)&proc[2]); 1418Sbill } 1425431Sroot splx(s); 14326004Smckusick return (error); 1448Sbill } 1458Sbill 1468Sbill /* 14712491Ssam * Put a buffer on the clean list after I/O is done. 14812491Ssam * Called from biodone. 14912491Ssam */ 15012491Ssam swdone(bp) 15112491Ssam register struct buf *bp; 15212491Ssam { 15312491Ssam register int s; 15412491Ssam 15512491Ssam if (bp->b_flags & B_ERROR) 15612491Ssam panic("IO err in push"); 15726315Skarels s = splbio(); 15812491Ssam bp->av_forw = bclnlist; 15912491Ssam cnt.v_pgout++; 16012491Ssam cnt.v_pgpgout += bp->b_bcount / NBPG; 16112491Ssam bclnlist = bp; 16212491Ssam if (bswlist.b_flags & B_WANTED) 16312491Ssam wakeup((caddr_t)&proc[2]); 16412491Ssam splx(s); 16512491Ssam } 16612491Ssam 16712491Ssam /* 1688Sbill * If rout == 0 then killed on swap error, else 1698Sbill * rout is the name of the routine where we ran out of 1708Sbill * swap space. 1718Sbill */ 1728Sbill swkill(p, rout) 1738Sbill struct proc *p; 1748Sbill char *rout; 1758Sbill { 1768Sbill 17724448Sbloom printf("pid %d: %s\n", p->p_pid, rout); 17824448Sbloom uprintf("sorry, pid %d was killed in %s\n", p->p_pid, rout); 1798Sbill /* 1808Sbill * To be sure no looping (e.g. in vmsched trying to 1818Sbill * swap out) mark process locked in core (as though 1828Sbill * done by user) after killing it so noone will try 1838Sbill * to swap it out. 1848Sbill */ 185165Sbill psignal(p, SIGKILL); 1868Sbill p->p_flag |= SULOCK; 1878Sbill } 1888Sbill 1898Sbill /* 1908Sbill * Raw I/O. The arguments are 1918Sbill * The strategy routine for the device 1928Sbill * A buffer, which will always be a special buffer 1938Sbill * header owned exclusively by the device for this purpose 1948Sbill * The device number 1958Sbill * Read/write flag 1968Sbill * Essentially all the work is computing physical addresses and 1978Sbill * validating them. 1988Sbill * If the user has the proper access privilidges, the process is 1998Sbill * marked 'delayed unlock' and the pages involved in the I/O are 2008Sbill * faulted and locked. After the completion of the I/O, the above pages 2018Sbill * are unlocked. 2028Sbill */ 2037724Swnj physio(strat, bp, dev, rw, mincnt, uio) 2047724Swnj int (*strat)(); 2057724Swnj register struct buf *bp; 2067724Swnj dev_t dev; 2077724Swnj int rw; 2087724Swnj unsigned (*mincnt)(); 2097724Swnj struct uio *uio; 2108Sbill { 21117313Skarels register struct iovec *iov; 2128Sbill register int c; 2138Sbill char *a; 2147724Swnj int s, error = 0; 215*30750Skarels #ifdef SECSIZE 216*30750Skarels int bsize; 217*30750Skarels struct partinfo dpart; 218*30750Skarels #endif SECSIZE 2198Sbill 220*30750Skarels #ifdef SECSIZE 221*30750Skarels if ((unsigned)major(dev) < nchrdev && 222*30750Skarels (*cdevsw[major(dev)].d_ioctl)(dev, DIOCGPART, (caddr_t)&dpart, 223*30750Skarels FREAD) == 0) 224*30750Skarels bsize = dpart.disklab->d_secsize; 225*30750Skarels else 226*30750Skarels bsize = DEV_BSIZE; 227*30750Skarels #endif SECSIZE 228*30750Skarels for (;;) { 229*30750Skarels if (uio->uio_iovcnt == 0) 230*30750Skarels return (0); 231*30750Skarels iov = uio->uio_iov; 232*30750Skarels if (useracc(iov->iov_base, (u_int)iov->iov_len, 233*30750Skarels rw==B_READ? B_WRITE : B_READ) == NULL) 234*30750Skarels return (EFAULT); 235*30750Skarels s = splbio(); 236*30750Skarels while (bp->b_flags&B_BUSY) { 237*30750Skarels bp->b_flags |= B_WANTED; 238*30750Skarels sleep((caddr_t)bp, PRIBIO+1); 239*30750Skarels } 2405431Sroot splx(s); 241*30750Skarels bp->b_error = 0; 242*30750Skarels bp->b_proc = u.u_procp; 243*30750Skarels #ifdef SECSIZE 244*30750Skarels bp->b_blksize = bsize; 245*30750Skarels #endif SECSIZE 246*30750Skarels bp->b_un.b_addr = iov->iov_base; 247*30750Skarels while (iov->iov_len > 0) { 248*30750Skarels bp->b_flags = B_BUSY | B_PHYS | rw; 249*30750Skarels bp->b_dev = dev; 250*30750Skarels #ifdef SECSIZE 251*30750Skarels bp->b_blkno = uio->uio_offset / bsize; 252*30750Skarels #else SECSIZE 253*30750Skarels bp->b_blkno = btodb(uio->uio_offset); 254*30750Skarels #endif SECSIZE 255*30750Skarels bp->b_bcount = iov->iov_len; 256*30750Skarels (*mincnt)(bp); 257*30750Skarels c = bp->b_bcount; 258*30750Skarels u.u_procp->p_flag |= SPHYSIO; 259*30750Skarels vslock(a = bp->b_un.b_addr, c); 260*30750Skarels physstrat(bp, strat, PRIBIO); 261*30750Skarels (void) splbio(); 262*30750Skarels vsunlock(a, c, rw); 263*30750Skarels u.u_procp->p_flag &= ~SPHYSIO; 264*30750Skarels if (bp->b_flags&B_WANTED) 265*30750Skarels wakeup((caddr_t)bp); 266*30750Skarels splx(s); 267*30750Skarels c -= bp->b_resid; 268*30750Skarels bp->b_un.b_addr += c; 269*30750Skarels iov->iov_len -= c; 270*30750Skarels uio->uio_resid -= c; 271*30750Skarels uio->uio_offset += c; 272*30750Skarels /* temp kludge for tape drives */ 273*30750Skarels if (bp->b_resid || (bp->b_flags&B_ERROR)) 274*30750Skarels break; 275*30750Skarels } 276*30750Skarels bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS); 277*30750Skarels error = geterror(bp); 2789766Ssam /* temp kludge for tape drives */ 279*30750Skarels if (bp->b_resid || error) 280*30750Skarels return (error); 281*30750Skarels uio->uio_iov++; 282*30750Skarels uio->uio_iovcnt--; 2838Sbill } 2848Sbill } 2858Sbill 2868Sbill unsigned 2878Sbill minphys(bp) 2887724Swnj struct buf *bp; 2898Sbill { 2908Sbill 29110400Ssam if (bp->b_bcount > MAXPHYS) 29210400Ssam bp->b_bcount = MAXPHYS; 2938Sbill } 294