1 /* 2 * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved. 3 * 4 * This code is derived from software contributed to The DragonFly Project 5 * by Matthew Dillon <dillon@backplane.com> 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * 3. Neither the name of The DragonFly Project nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific, prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * $DragonFly: src/sys/kern/kern_xio.c,v 1.10 2005/06/06 15:02:28 dillon Exp $ 35 */ 36 /* 37 * Kernel XIO interface. An initialized XIO is basically a collection of 38 * appropriately held vm_page_t's. XIO buffers are vmspace agnostic and 39 * can represent userspace or kernelspace buffers, and can be passed to 40 * foreign threads outside of the originating vmspace. XIO buffers are 41 * not mapped into KVM and thus can be manipulated and passed around with 42 * very low overheads. 43 * 44 * The intent is for XIO to be used in the I/O path, VFS, CAPS, and other 45 * places that need to pass (possibly userspace) data between threads. 46 * 47 * TODO: check for busy page when modifying, check writeable. 48 */ 49 50 #include <sys/param.h> 51 #include <sys/systm.h> 52 #include <sys/malloc.h> 53 #include <sys/proc.h> 54 #include <sys/vmmeter.h> 55 #include <sys/vnode.h> 56 #include <sys/xio.h> 57 #include <sys/sfbuf.h> 58 59 #include <vm/vm.h> 60 #include <vm/vm_param.h> 61 #include <sys/lock.h> 62 #include <vm/vm_kern.h> 63 #include <vm/pmap.h> 64 #include <vm/vm_map.h> 65 #include <vm/vm_object.h> 66 #include <vm/vm_page.h> 67 #include <vm/vm_pageout.h> 68 #include <vm/vm_pager.h> 69 #include <vm/vm_extern.h> 70 #include <vm/vm_page2.h> 71 72 /* 73 * Just do basic initialization of an empty XIO 74 */ 75 void 76 xio_init(xio_t xio) 77 { 78 xio->xio_flags = 0; 79 xio->xio_bytes = 0; 80 xio->xio_error = 0; 81 xio->xio_offset = 0; 82 xio->xio_npages = 0; 83 xio->xio_pages = xio->xio_internal_pages; 84 } 85 86 /* 87 * Initialize an XIO given a userspace buffer. 0 is returned on success, 88 * an error code on failure. The actual number of bytes that could be 89 * accomodated in the XIO will be stored in xio_bytes and the page offset 90 * will be stored in xio_offset. 91 */ 92 int 93 xio_init_ubuf(xio_t xio, void *ubase, size_t ubytes, int flags) 94 { 95 vm_offset_t addr; 96 vm_paddr_t paddr; 97 vm_page_t m; 98 int i; 99 int n; 100 int vmprot; 101 102 addr = trunc_page((vm_offset_t)ubase); 103 xio->xio_flags = flags; 104 xio->xio_bytes = 0; 105 xio->xio_error = 0; 106 if (ubytes == 0) { 107 xio->xio_offset = 0; 108 xio->xio_npages = 0; 109 } else { 110 vmprot = (flags & XIOF_WRITE) ? VM_PROT_WRITE : VM_PROT_READ; 111 xio->xio_offset = (vm_offset_t)ubase & PAGE_MASK; 112 xio->xio_pages = xio->xio_internal_pages; 113 if ((n = PAGE_SIZE - xio->xio_offset) > ubytes) 114 n = ubytes; 115 for (i = 0; n && i < XIO_INTERNAL_PAGES; ++i) { 116 if (vm_fault_quick((caddr_t)addr, vmprot) < 0) 117 break; 118 if ((paddr = pmap_kextract(addr)) == 0) 119 break; 120 crit_enter(); 121 m = PHYS_TO_VM_PAGE(paddr); 122 vm_page_hold(m); 123 crit_exit(); 124 xio->xio_pages[i] = m; 125 ubytes -= n; 126 xio->xio_bytes += n; 127 if ((n = ubytes) > PAGE_SIZE) 128 n = PAGE_SIZE; 129 addr += PAGE_SIZE; 130 } 131 xio->xio_npages = i; 132 133 /* 134 * If a failure occured clean out what we loaded and return EFAULT. 135 * Return 0 on success. 136 */ 137 if (i < XIO_INTERNAL_PAGES && n) { 138 xio_release(xio); 139 xio->xio_error = EFAULT; 140 } 141 } 142 return(xio->xio_error); 143 } 144 145 /* 146 * Initialize an XIO given a kernelspace buffer. 0 is returned on success, 147 * an error code on failure. The actual number of bytes that could be 148 * accomodated in the XIO will be stored in xio_bytes and the page offset 149 * will be stored in xio_offset. 150 */ 151 int 152 xio_init_kbuf(xio_t xio, void *kbase, size_t kbytes) 153 { 154 vm_offset_t addr; 155 vm_paddr_t paddr; 156 vm_page_t m; 157 int i; 158 int n; 159 160 addr = trunc_page((vm_offset_t)kbase); 161 xio->xio_flags = 0; 162 xio->xio_offset = (vm_offset_t)kbase & PAGE_MASK; 163 xio->xio_bytes = 0; 164 xio->xio_pages = xio->xio_internal_pages; 165 xio->xio_error = 0; 166 if ((n = PAGE_SIZE - xio->xio_offset) > kbytes) 167 n = kbytes; 168 for (i = 0; n && i < XIO_INTERNAL_PAGES; ++i) { 169 if ((paddr = pmap_kextract(addr)) == 0) 170 break; 171 crit_enter(); 172 m = PHYS_TO_VM_PAGE(paddr); 173 vm_page_hold(m); 174 crit_exit(); 175 xio->xio_pages[i] = m; 176 kbytes -= n; 177 xio->xio_bytes += n; 178 if ((n = kbytes) > PAGE_SIZE) 179 n = PAGE_SIZE; 180 addr += PAGE_SIZE; 181 } 182 xio->xio_npages = i; 183 184 /* 185 * If a failure occured clean out what we loaded and return EFAULT. 186 * Return 0 on success. 187 */ 188 if (i < XIO_INTERNAL_PAGES && n) { 189 xio_release(xio); 190 xio->xio_error = EFAULT; 191 } 192 return(xio->xio_error); 193 } 194 195 /* 196 * Cleanup an XIO so it can be destroyed. The pages associated with the 197 * XIO are released. 198 */ 199 void 200 xio_release(xio_t xio) 201 { 202 int i; 203 vm_page_t m; 204 205 crit_enter(); 206 for (i = 0; i < xio->xio_npages; ++i) { 207 m = xio->xio_pages[i]; 208 vm_page_unhold(m); 209 } 210 crit_exit(); 211 xio->xio_offset = 0; 212 xio->xio_npages = 0; 213 xio->xio_bytes = 0; 214 xio->xio_error = ENOBUFS; 215 } 216 217 /* 218 * Copy data between an XIO and a UIO. If the UIO represents userspace it 219 * must be relative to the current context. 220 * 221 * uoffset is the abstracted starting offset in the XIO, not the actual 222 * offset, and usually starts at 0. 223 * 224 * The XIO is not modified. The UIO is updated to reflect the copy. 225 * 226 * UIO_READ xio -> uio 227 * UIO_WRITE uio -> xio 228 */ 229 int 230 xio_uio_copy(xio_t xio, int uoffset, struct uio *uio, int *sizep) 231 { 232 int error; 233 int bytes; 234 235 bytes = xio->xio_bytes - uoffset; 236 if (bytes > uio->uio_resid) 237 bytes = uio->uio_resid; 238 KKASSERT(bytes >= 0); 239 error = uiomove_fromphys(xio->xio_pages, xio->xio_offset + uoffset, 240 bytes, uio); 241 if (error == 0) 242 *sizep = bytes; 243 else 244 *sizep = 0; 245 return(error); 246 } 247 248 /* 249 * Copy the specified number of bytes from the xio to a userland 250 * buffer. Return an error code or 0 on success. 251 * 252 * uoffset is the abstracted starting offset in the XIO, not the actual 253 * offset, and usually starts at 0. 254 * 255 * The XIO is not modified. 256 */ 257 int 258 xio_copy_xtou(xio_t xio, int uoffset, void *uptr, int bytes) 259 { 260 int i; 261 int n; 262 int error; 263 int offset; 264 vm_page_t m; 265 struct sf_buf *sf; 266 267 if (bytes > xio->xio_bytes) 268 return(EFAULT); 269 270 offset = (xio->xio_offset + uoffset) & PAGE_MASK; 271 if ((n = PAGE_SIZE - offset) > bytes) 272 n = bytes; 273 274 error = 0; 275 for (i = (xio->xio_offset + uoffset) >> PAGE_SHIFT; 276 i < xio->xio_npages; 277 ++i 278 ) { 279 m = xio->xio_pages[i]; 280 sf = sf_buf_alloc(m, SFB_CPUPRIVATE); 281 error = copyout((char *)sf_buf_kva(sf) + offset, uptr, n); 282 sf_buf_free(sf); 283 if (error) 284 break; 285 bytes -= n; 286 uptr = (char *)uptr + n; 287 if (bytes == 0) 288 break; 289 if ((n = bytes) > PAGE_SIZE) 290 n = PAGE_SIZE; 291 offset = 0; 292 } 293 return(error); 294 } 295 296 /* 297 * Copy the specified number of bytes from the xio to a kernel 298 * buffer. Return an error code or 0 on success. 299 * 300 * uoffset is the abstracted starting offset in the XIO, not the actual 301 * offset, and usually starts at 0. 302 * 303 * The XIO is not modified. 304 */ 305 int 306 xio_copy_xtok(xio_t xio, int uoffset, void *kptr, int bytes) 307 { 308 int i; 309 int n; 310 int error; 311 int offset; 312 vm_page_t m; 313 struct sf_buf *sf; 314 315 if (bytes + uoffset > xio->xio_bytes) 316 return(EFAULT); 317 318 offset = (xio->xio_offset + uoffset) & PAGE_MASK; 319 if ((n = PAGE_SIZE - offset) > bytes) 320 n = bytes; 321 322 error = 0; 323 for (i = (xio->xio_offset + uoffset) >> PAGE_SHIFT; 324 i < xio->xio_npages; 325 ++i 326 ) { 327 m = xio->xio_pages[i]; 328 sf = sf_buf_alloc(m, SFB_CPUPRIVATE); 329 bcopy((char *)sf_buf_kva(sf) + offset, kptr, n); 330 sf_buf_free(sf); 331 bytes -= n; 332 kptr = (char *)kptr + n; 333 if (bytes == 0) 334 break; 335 if ((n = bytes) > PAGE_SIZE) 336 n = PAGE_SIZE; 337 offset = 0; 338 } 339 return(error); 340 } 341 342