1 /* 2 * Copyright (c) 1994 John S. Dyson 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice immediately at the beginning of the file, without modification, 10 * this list of conditions, and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. Absolutely no warranty of function or purpose is made by the author 15 * John S. Dyson. 16 * 4. Modifications may be freely made to this file if the above conditions 17 * are met. 18 * 19 * $Id: kern_physio.c,v 1.8 1994/09/25 19:33:40 phk Exp $ 20 */ 21 22 #include <sys/param.h> 23 #include <sys/systm.h> 24 #include <sys/buf.h> 25 #include <sys/conf.h> 26 #include <sys/proc.h> 27 #include <vm/vm.h> 28 29 static void physwakeup(); 30 31 int 32 physio(strategy, bp, dev, rw, minp, uio) 33 int (*strategy)(); 34 struct buf *bp; 35 dev_t dev; 36 int rw; 37 u_int (*minp)(); 38 struct uio *uio; 39 { 40 int i; 41 int bufflags = rw?B_READ:0; 42 int error; 43 int spl; 44 caddr_t sa; 45 int bp_alloc = (bp == 0); 46 struct buf *bpa; 47 48 /* 49 * keep the process from being swapped 50 */ 51 curproc->p_flag |= P_PHYSIO; 52 53 /* create and build a buffer header for a transfer */ 54 bpa = (struct buf *)getpbuf(); 55 if (!bp_alloc) { 56 spl = splbio(); 57 while (bp->b_flags & B_BUSY) { 58 bp->b_flags |= B_WANTED; 59 tsleep((caddr_t)bp, PRIBIO, "physbw", 0); 60 } 61 bp->b_flags |= B_BUSY; 62 splx(spl); 63 } else { 64 bp = bpa; 65 } 66 67 /* 68 * get a copy of the kva from the physical buffer 69 */ 70 sa = bpa->b_data; 71 bp->b_proc = curproc; 72 bp->b_dev = dev; 73 error = bp->b_error = 0; 74 75 for(i=0;i<uio->uio_iovcnt;i++) { 76 while( uio->uio_iov[i].iov_len) { 77 78 bp->b_bcount = uio->uio_iov[i].iov_len; 79 bp->b_bcount = minp( bp); 80 if( minp != minphys) 81 bp->b_bcount = minphys( bp); 82 bp->b_bufsize = bp->b_bcount; 83 bp->b_flags = B_BUSY | B_PHYS | B_CALL | bufflags; 84 bp->b_iodone = physwakeup; 85 bp->b_data = uio->uio_iov[i].iov_base; 86 /* 87 * pass in the kva from the physical buffer 88 * for the temporary kernel mapping. 89 */ 90 bp->b_saveaddr = sa; 91 bp->b_blkno = btodb(uio->uio_offset); 92 93 94 if (rw && !useracc(bp->b_data, bp->b_bufsize, B_WRITE)) { 95 error = EFAULT; 96 goto doerror; 97 } 98 if (!rw && !useracc(bp->b_data, bp->b_bufsize, B_READ)) { 99 error = EFAULT; 100 goto doerror; 101 } 102 103 vmapbuf(bp); 104 105 /* perform transfer */ 106 (*strategy)(bp); 107 108 spl = splbio(); 109 while ((bp->b_flags & B_DONE) == 0) 110 tsleep((caddr_t)bp, PRIBIO, "physstr", 0); 111 splx(spl); 112 113 vunmapbuf(bp); 114 115 /* 116 * update the uio data 117 */ 118 { 119 int iolen = bp->b_bcount - bp->b_resid; 120 121 if (iolen == 0 && !(bp->b_flags & B_ERROR)) 122 goto doerror; /* EOF */ 123 uio->uio_iov[i].iov_len -= iolen; 124 uio->uio_iov[i].iov_base += iolen; 125 uio->uio_resid -= iolen; 126 uio->uio_offset += iolen; 127 } 128 129 /* 130 * check for an error 131 */ 132 if( bp->b_flags & B_ERROR) { 133 error = bp->b_error; 134 goto doerror; 135 } 136 } 137 } 138 139 140 doerror: 141 relpbuf(bpa); 142 if (!bp_alloc) { 143 bp->b_flags &= ~(B_BUSY|B_PHYS); 144 if( bp->b_flags & B_WANTED) { 145 bp->b_flags &= ~B_WANTED; 146 wakeup((caddr_t)bp); 147 } 148 } 149 /* 150 * allow the process to be swapped 151 */ 152 curproc->p_flag &= ~P_PHYSIO; 153 154 return (error); 155 } 156 157 u_int 158 minphys(struct buf *bp) 159 { 160 161 if( bp->b_bcount > MAXPHYS) { 162 bp->b_bcount = MAXPHYS; 163 } 164 return bp->b_bcount; 165 } 166 167 int 168 rawread(dev_t dev, struct uio *uio) 169 { 170 return (physio(cdevsw[major(dev)].d_strategy, (struct buf *)NULL, 171 dev, 1, minphys, uio)); 172 } 173 174 int 175 rawwrite(dev_t dev, struct uio *uio) 176 { 177 return (physio(cdevsw[major(dev)].d_strategy, (struct buf *)NULL, 178 dev, 0, minphys, uio)); 179 } 180 181 static void 182 physwakeup(bp) 183 struct buf *bp; 184 { 185 wakeup((caddr_t) bp); 186 bp->b_flags &= ~B_CALL; 187 } 188