1 /* kern_physio.c 6.4 84/10/31 */ 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, (char *)0); 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 char *mesg; 156 157 printf("pid %d: ", p->p_pid); 158 if (rout) 159 printf(mesg = "killed due to no swap space\n"); 160 else 161 printf(mesg = "killed on swap error\n"); 162 uprintf("sorry, pid %d was %s", p->p_pid, mesg); 163 /* 164 * To be sure no looping (e.g. in vmsched trying to 165 * swap out) mark process locked in core (as though 166 * done by user) after killing it so noone will try 167 * to swap it out. 168 */ 169 psignal(p, SIGKILL); 170 p->p_flag |= SULOCK; 171 } 172 173 /* 174 * Raw I/O. The arguments are 175 * The strategy routine for the device 176 * A buffer, which will always be a special buffer 177 * header owned exclusively by the device for this purpose 178 * The device number 179 * Read/write flag 180 * Essentially all the work is computing physical addresses and 181 * validating them. 182 * If the user has the proper access privilidges, the process is 183 * marked 'delayed unlock' and the pages involved in the I/O are 184 * faulted and locked. After the completion of the I/O, the above pages 185 * are unlocked. 186 */ 187 physio(strat, bp, dev, rw, mincnt, uio) 188 int (*strat)(); 189 register struct buf *bp; 190 dev_t dev; 191 int rw; 192 unsigned (*mincnt)(); 193 struct uio *uio; 194 { 195 register struct iovec *iov; 196 register int c; 197 char *a; 198 int s, error = 0; 199 200 nextiov: 201 if (uio->uio_iovcnt == 0) 202 return (0); 203 iov = uio->uio_iov; 204 if (useracc(iov->iov_base,(u_int)iov->iov_len,rw==B_READ?B_WRITE:B_READ) == NULL) 205 return (EFAULT); 206 s = spl6(); 207 while (bp->b_flags&B_BUSY) { 208 bp->b_flags |= B_WANTED; 209 sleep((caddr_t)bp, PRIBIO+1); 210 } 211 splx(s); 212 bp->b_error = 0; 213 bp->b_proc = u.u_procp; 214 bp->b_un.b_addr = iov->iov_base; 215 while (iov->iov_len > 0) { 216 bp->b_flags = B_BUSY | B_PHYS | rw; 217 bp->b_dev = dev; 218 bp->b_blkno = btodb(uio->uio_offset); 219 bp->b_bcount = iov->iov_len; 220 (*mincnt)(bp); 221 c = bp->b_bcount; 222 u.u_procp->p_flag |= SPHYSIO; 223 vslock(a = bp->b_un.b_addr, c); 224 physstrat(bp, strat, PRIBIO); 225 (void) spl6(); 226 vsunlock(a, c, rw); 227 u.u_procp->p_flag &= ~SPHYSIO; 228 if (bp->b_flags&B_WANTED) 229 wakeup((caddr_t)bp); 230 splx(s); 231 c -= bp->b_resid; 232 bp->b_un.b_addr += c; 233 iov->iov_len -= c; 234 uio->uio_resid -= c; 235 uio->uio_offset += c; 236 /* temp kludge for tape drives */ 237 if (bp->b_resid || (bp->b_flags&B_ERROR)) 238 break; 239 } 240 bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS); 241 error = geterror(bp); 242 /* temp kludge for tape drives */ 243 if (bp->b_resid || error) 244 return (error); 245 uio->uio_iov++; 246 uio->uio_iovcnt--; 247 goto nextiov; 248 } 249 250 #define MAXPHYS (63 * 1024) 251 252 unsigned 253 minphys(bp) 254 struct buf *bp; 255 { 256 257 if (bp->b_bcount > MAXPHYS) 258 bp->b_bcount = MAXPHYS; 259 } 260