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