1 /* 2 * Copyright (c) 1982 Regents of the University of California. 3 * All rights reserved. The Berkeley software License Agreement 4 * specifies the terms and conditions for redistribution. 5 * 6 * @(#)kern_physio.c 6.7 (Berkeley) 08/28/85 7 */ 8 9 #include "../machine/pte.h" 10 11 #include "param.h" 12 #include "systm.h" 13 #include "dir.h" 14 #include "user.h" 15 #include "buf.h" 16 #include "conf.h" 17 #include "proc.h" 18 #include "seg.h" 19 #include "vm.h" 20 #include "trace.h" 21 #include "map.h" 22 #include "uio.h" 23 24 /* 25 * Swap IO headers - 26 * They contain the necessary information for the swap I/O. 27 * At any given time, a swap header can be in three 28 * different lists. When free it is in the free list, 29 * when allocated and the I/O queued, it is on the swap 30 * device list, and finally, if the operation was a dirty 31 * page push, when the I/O completes, it is inserted 32 * in a list of cleaned pages to be processed by the pageout daemon. 33 */ 34 struct buf *swbuf; 35 36 /* 37 * swap I/O - 38 * 39 * If the flag indicates a dirty page push initiated 40 * by the pageout daemon, we map the page into the i th 41 * virtual page of process 2 (the daemon itself) where i is 42 * the index of the swap header that has been allocated. 43 * We simply initialize the header and queue the I/O but 44 * do not wait for completion. When the I/O completes, 45 * iodone() will link the header to a list of cleaned 46 * pages to be processed by the pageout daemon. 47 */ 48 swap(p, dblkno, addr, nbytes, rdflg, flag, dev, pfcent) 49 struct proc *p; 50 swblk_t dblkno; 51 caddr_t addr; 52 int nbytes, rdflg, flag; 53 dev_t dev; 54 u_int pfcent; 55 { 56 register struct buf *bp; 57 register u_int c; 58 int p2dp; 59 register struct pte *dpte, *vpte; 60 int s; 61 extern swdone(); 62 63 s = spl6(); 64 while (bswlist.av_forw == NULL) { 65 bswlist.b_flags |= B_WANTED; 66 sleep((caddr_t)&bswlist, PSWP+1); 67 } 68 bp = bswlist.av_forw; 69 bswlist.av_forw = bp->av_forw; 70 splx(s); 71 72 bp->b_flags = B_BUSY | B_PHYS | rdflg | flag; 73 if ((bp->b_flags & (B_DIRTY|B_PGIN)) == 0) 74 if (rdflg == B_READ) 75 sum.v_pswpin += btoc(nbytes); 76 else 77 sum.v_pswpout += btoc(nbytes); 78 bp->b_proc = p; 79 if (flag & B_DIRTY) { 80 p2dp = ((bp - swbuf) * CLSIZE) * KLMAX; 81 dpte = dptopte(&proc[2], p2dp); 82 vpte = vtopte(p, btop(addr)); 83 for (c = 0; c < nbytes; c += NBPG) { 84 if (vpte->pg_pfnum == 0 || vpte->pg_fod) 85 panic("swap bad pte"); 86 *dpte++ = *vpte++; 87 } 88 bp->b_un.b_addr = (caddr_t)ctob(dptov(&proc[2], p2dp)); 89 bp->b_flags |= B_CALL; 90 bp->b_iodone = swdone; 91 bp->b_pfcent = pfcent; 92 } else 93 bp->b_un.b_addr = addr; 94 while (nbytes > 0) { 95 bp->b_bcount = nbytes; 96 minphys(bp); 97 c = bp->b_bcount; 98 bp->b_blkno = dblkno; 99 bp->b_dev = dev; 100 #ifdef TRACE 101 trace(TR_SWAPIO, dev, bp->b_blkno); 102 #endif 103 physstrat(bp, bdevsw[major(dev)].d_strategy, PSWP); 104 if (flag & B_DIRTY) { 105 if (c < nbytes) 106 panic("big push"); 107 return; 108 } 109 bp->b_un.b_addr += c; 110 bp->b_flags &= ~B_DONE; 111 if (bp->b_flags & B_ERROR) { 112 if ((flag & (B_UAREA|B_PAGET)) || rdflg == B_WRITE) 113 panic("hard IO err in swap"); 114 swkill(p, "swap: read error from swap device"); 115 } 116 nbytes -= c; 117 dblkno += btodb(c); 118 } 119 s = spl6(); 120 bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS|B_PAGET|B_UAREA|B_DIRTY); 121 bp->av_forw = bswlist.av_forw; 122 bswlist.av_forw = bp; 123 if (bswlist.b_flags & B_WANTED) { 124 bswlist.b_flags &= ~B_WANTED; 125 wakeup((caddr_t)&bswlist); 126 wakeup((caddr_t)&proc[2]); 127 } 128 splx(s); 129 } 130 131 /* 132 * Put a buffer on the clean list after I/O is done. 133 * Called from biodone. 134 */ 135 swdone(bp) 136 register struct buf *bp; 137 { 138 register int s; 139 140 if (bp->b_flags & B_ERROR) 141 panic("IO err in push"); 142 s = spl6(); 143 bp->av_forw = bclnlist; 144 cnt.v_pgout++; 145 cnt.v_pgpgout += bp->b_bcount / NBPG; 146 bclnlist = bp; 147 if (bswlist.b_flags & B_WANTED) 148 wakeup((caddr_t)&proc[2]); 149 splx(s); 150 } 151 152 /* 153 * If rout == 0 then killed on swap error, else 154 * rout is the name of the routine where we ran out of 155 * swap space. 156 */ 157 swkill(p, rout) 158 struct proc *p; 159 char *rout; 160 { 161 162 printf("pid %d: %s\n", p->p_pid, rout); 163 uprintf("sorry, pid %d was killed in %s\n", p->p_pid, rout); 164 /* 165 * To be sure no looping (e.g. in vmsched trying to 166 * swap out) mark process locked in core (as though 167 * done by user) after killing it so noone will try 168 * to swap it out. 169 */ 170 psignal(p, SIGKILL); 171 p->p_flag |= SULOCK; 172 } 173 174 /* 175 * Raw I/O. The arguments are 176 * The strategy routine for the device 177 * A buffer, which will always be a special buffer 178 * header owned exclusively by the device for this purpose 179 * The device number 180 * Read/write flag 181 * Essentially all the work is computing physical addresses and 182 * validating them. 183 * If the user has the proper access privilidges, the process is 184 * marked 'delayed unlock' and the pages involved in the I/O are 185 * faulted and locked. After the completion of the I/O, the above pages 186 * are unlocked. 187 */ 188 physio(strat, bp, dev, rw, mincnt, uio) 189 int (*strat)(); 190 register struct buf *bp; 191 dev_t dev; 192 int rw; 193 unsigned (*mincnt)(); 194 struct uio *uio; 195 { 196 register struct iovec *iov; 197 register int c; 198 char *a; 199 int s, error = 0; 200 201 nextiov: 202 if (uio->uio_iovcnt == 0) 203 return (0); 204 iov = uio->uio_iov; 205 if (useracc(iov->iov_base,(u_int)iov->iov_len,rw==B_READ?B_WRITE:B_READ) == NULL) 206 return (EFAULT); 207 s = spl6(); 208 while (bp->b_flags&B_BUSY) { 209 bp->b_flags |= B_WANTED; 210 sleep((caddr_t)bp, PRIBIO+1); 211 } 212 splx(s); 213 bp->b_error = 0; 214 bp->b_proc = u.u_procp; 215 bp->b_un.b_addr = iov->iov_base; 216 while (iov->iov_len > 0) { 217 bp->b_flags = B_BUSY | B_PHYS | rw; 218 bp->b_dev = dev; 219 bp->b_blkno = btodb(uio->uio_offset); 220 bp->b_bcount = iov->iov_len; 221 (*mincnt)(bp); 222 c = bp->b_bcount; 223 u.u_procp->p_flag |= SPHYSIO; 224 vslock(a = bp->b_un.b_addr, c); 225 physstrat(bp, strat, PRIBIO); 226 (void) spl6(); 227 vsunlock(a, c, rw); 228 u.u_procp->p_flag &= ~SPHYSIO; 229 if (bp->b_flags&B_WANTED) 230 wakeup((caddr_t)bp); 231 splx(s); 232 c -= bp->b_resid; 233 bp->b_un.b_addr += c; 234 iov->iov_len -= c; 235 uio->uio_resid -= c; 236 uio->uio_offset += c; 237 /* temp kludge for tape drives */ 238 if (bp->b_resid || (bp->b_flags&B_ERROR)) 239 break; 240 } 241 bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS); 242 error = geterror(bp); 243 /* temp kludge for tape drives */ 244 if (bp->b_resid || error) 245 return (error); 246 uio->uio_iov++; 247 uio->uio_iovcnt--; 248 goto nextiov; 249 } 250 251 #define MAXPHYS (63 * 1024) 252 253 unsigned 254 minphys(bp) 255 struct buf *bp; 256 { 257 258 if (bp->b_bcount > MAXPHYS) 259 bp->b_bcount = MAXPHYS; 260 } 261