1 /* 2 * Copyright (c) 1982, 1986 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 7.4 (Berkeley) 05/06/88 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 * biodone() 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 struct pte *dpte, *vpte; 58 register u_int c; 59 int p2dp, s, error = 0; 60 struct buf *getswbuf(); 61 int swdone(); 62 63 bp = getswbuf(PSWP+1); 64 bp->b_flags = B_BUSY | B_PHYS | rdflg | flag; 65 if ((bp->b_flags & (B_DIRTY|B_PGIN)) == 0) 66 if (rdflg == B_READ) 67 sum.v_pswpin += btoc(nbytes); 68 else 69 sum.v_pswpout += btoc(nbytes); 70 bp->b_proc = p; 71 if (flag & B_DIRTY) { 72 p2dp = ((bp - swbuf) * CLSIZE) * KLMAX; 73 dpte = dptopte(&proc[2], p2dp); 74 vpte = vtopte(p, btop(addr)); 75 for (c = 0; c < nbytes; c += NBPG) { 76 if (vpte->pg_pfnum == 0 || vpte->pg_fod) 77 panic("swap bad pte"); 78 *dpte++ = *vpte++; 79 } 80 bp->b_un.b_addr = (caddr_t)ctob(dptov(&proc[2], p2dp)); 81 bp->b_flags |= B_CALL; 82 bp->b_iodone = swdone; 83 bp->b_pfcent = pfcent; 84 } else 85 bp->b_un.b_addr = addr; 86 while (nbytes > 0) { 87 bp->b_bcount = nbytes; 88 minphys(bp); 89 c = bp->b_bcount; 90 bp->b_blkno = dblkno; 91 bp->b_dev = dev; 92 #ifdef TRACE 93 trace(TR_SWAPIO, dev, bp->b_blkno); 94 #endif 95 (*bdevsw[major(dev)].d_strategy)(bp); 96 /* pageout daemon doesn't wait for pushed pages */ 97 if (flag & B_DIRTY) { 98 if (c < nbytes) 99 panic("big push"); 100 return (0); 101 } else { 102 s = splbio(); 103 while ((bp->b_flags & B_DONE) == 0) 104 sleep((caddr_t)bp, PSWP); 105 splx(s); 106 } 107 bp->b_un.b_addr += c; 108 bp->b_flags &= ~B_DONE; 109 if (bp->b_flags & B_ERROR) { 110 if ((flag & (B_UAREA|B_PAGET)) || rdflg == B_WRITE) 111 panic("hard IO err in swap"); 112 swkill(p, "swap: read error from swap device"); 113 error = EIO; 114 } 115 nbytes -= c; 116 dblkno += btodb(c); 117 } 118 bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS|B_PAGET|B_UAREA|B_DIRTY); 119 freeswbuf(bp); 120 return (error); 121 } 122 123 /* 124 * Put a buffer on the clean list after I/O is done. 125 * Called from biodone. 126 */ 127 swdone(bp) 128 register struct buf *bp; 129 { 130 register int s; 131 132 if (bp->b_flags & B_ERROR) 133 panic("IO err in push"); 134 s = splbio(); 135 bp->av_forw = bclnlist; 136 cnt.v_pgout++; 137 cnt.v_pgpgout += bp->b_bcount / NBPG; 138 bclnlist = bp; 139 if (bswlist.b_flags & B_WANTED) 140 wakeup((caddr_t)&proc[2]); 141 splx(s); 142 } 143 144 /* 145 * If rout == 0 then killed on swap error, else 146 * rout is the name of the routine where we ran out of 147 * swap space. 148 */ 149 swkill(p, rout) 150 struct proc *p; 151 char *rout; 152 { 153 154 printf("pid %d: %s\n", p->p_pid, rout); 155 uprintf("sorry, pid %d was killed in %s\n", p->p_pid, rout); 156 /* 157 * To be sure no looping (e.g. in vmsched trying to 158 * swap out) mark process locked in core (as though 159 * done by user) after killing it so noone will try 160 * to swap it out. 161 */ 162 psignal(p, SIGKILL); 163 p->p_flag |= SULOCK; 164 } 165 166 /* 167 * Raw I/O. The arguments are 168 * The strategy routine for the device 169 * A buffer, which will either be a special buffer header owned 170 * exclusively by the device for this purpose, or NULL, 171 * indicating that we should use a swap buffer 172 * The device number 173 * Read/write flag 174 * Essentially all the work is computing physical addresses and 175 * validating them. 176 * If the user has the proper access privilidges, the process is 177 * marked 'delayed unlock' and the pages involved in the I/O are 178 * faulted and locked. After the completion of the I/O, the above pages 179 * are unlocked. 180 */ 181 physio(strat, bp, dev, rw, mincnt, uio) 182 int (*strat)(); 183 register struct buf *bp; 184 dev_t dev; 185 int rw; 186 u_int (*mincnt)(); 187 struct uio *uio; 188 { 189 register struct iovec *iov; 190 register int c; 191 char *a; 192 int s, allocbuf = 0, error = 0; 193 struct buf *getswbuf(); 194 195 if (bp == NULL) { 196 allocbuf = 1; 197 bp = getswbuf(PRIBIO+1); 198 } 199 for (; uio->uio_iovcnt; uio->uio_iov++, uio->uio_iovcnt--) { 200 iov = uio->uio_iov; 201 if (!useracc(iov->iov_base, (u_int)iov->iov_len, 202 rw == B_READ ? B_WRITE : B_READ)) { 203 error = EFAULT; 204 break; 205 } 206 if (!allocbuf) { /* only if sharing caller's buffer */ 207 s = splbio(); 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 } 214 bp->b_error = 0; 215 bp->b_proc = u.u_procp; 216 bp->b_un.b_addr = iov->iov_base; 217 while (iov->iov_len > 0) { 218 bp->b_flags = B_BUSY | B_PHYS | B_RAW | rw; 219 bp->b_dev = dev; 220 bp->b_blkno = btodb(uio->uio_offset); 221 bp->b_bcount = iov->iov_len; 222 (*mincnt)(bp); 223 c = bp->b_bcount; 224 u.u_procp->p_flag |= SPHYSIO; 225 vslock(a = bp->b_un.b_addr, c); 226 (*strat)(bp); 227 s = splbio(); 228 while ((bp->b_flags & B_DONE) == 0) 229 sleep((caddr_t)bp, PRIBIO); 230 vsunlock(a, c, rw); 231 u.u_procp->p_flag &= ~SPHYSIO; 232 if (bp->b_flags&B_WANTED) /* rare */ 233 wakeup((caddr_t)bp); 234 splx(s); 235 c -= bp->b_resid; 236 bp->b_un.b_addr += c; 237 iov->iov_len -= c; 238 uio->uio_resid -= c; 239 uio->uio_offset += c; 240 /* temp kludge for tape drives */ 241 if (bp->b_resid || (bp->b_flags&B_ERROR)) 242 break; 243 } 244 bp->b_flags &= ~(B_BUSY | B_WANTED | B_PHYS | B_RAW); 245 error = geterror(bp); 246 /* temp kludge for tape drives */ 247 if (bp->b_resid || error) 248 break; 249 } 250 if (allocbuf) 251 freeswbuf(bp); 252 return (error); 253 } 254 255 u_int 256 minphys(bp) 257 struct buf *bp; 258 { 259 if (bp->b_bcount > MAXPHYS) 260 bp->b_bcount = MAXPHYS; 261 } 262 263 static 264 struct buf * 265 getswbuf(prio) 266 int prio; 267 { 268 int s; 269 struct buf *bp; 270 271 s = splbio(); 272 while (bswlist.av_forw == NULL) { 273 bswlist.b_flags |= B_WANTED; 274 sleep((caddr_t)&bswlist, prio); 275 } 276 bp = bswlist.av_forw; 277 bswlist.av_forw = bp->av_forw; 278 splx(s); 279 return (bp); 280 } 281 282 static 283 freeswbuf(bp) 284 struct buf *bp; 285 { 286 int s; 287 288 s = splbio(); 289 bp->av_forw = bswlist.av_forw; 290 bswlist.av_forw = bp; 291 if (bswlist.b_flags & B_WANTED) { 292 bswlist.b_flags &= ~B_WANTED; 293 wakeup((caddr_t)&bswlist); 294 wakeup((caddr_t)&proc[2]); 295 } 296 splx(s); 297 } 298 299 rawread(dev, uio) 300 dev_t dev; 301 struct uio *uio; 302 { 303 return (physio(cdevsw[major(dev)].d_strategy, (struct buf *)NULL, 304 dev, B_READ, minphys, uio)); 305 } 306 307 rawwrite(dev, uio) 308 dev_t dev; 309 struct uio *uio; 310 { 311 return (physio(cdevsw[major(dev)].d_strategy, (struct buf *)NULL, 312 dev, B_WRITE, minphys, uio)); 313 } 314