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.7 2004/07/16 05:51:10 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. 90 * 91 * Note that you cannot legally accessed a previously cached linmap with 92 * a newly initialized xio until after calling xio_linmap(). 93 */ 94 int 95 xio_init_ubuf(xio_t xio, void *ubase, size_t ubytes, int flags) 96 { 97 vm_offset_t addr; 98 vm_paddr_t paddr; 99 vm_page_t m; 100 int i; 101 int n; 102 int s; 103 int vmprot; 104 105 addr = trunc_page((vm_offset_t)ubase); 106 xio->xio_flags = flags; 107 xio->xio_bytes = 0; 108 xio->xio_error = 0; 109 if (ubytes == 0) { 110 xio->xio_offset = 0; 111 xio->xio_npages = 0; 112 } else { 113 vmprot = (flags & XIOF_WRITE) ? VM_PROT_WRITE : VM_PROT_READ; 114 xio->xio_offset = (vm_offset_t)ubase & PAGE_MASK; 115 xio->xio_pages = xio->xio_internal_pages; 116 if ((n = PAGE_SIZE - xio->xio_offset) > ubytes) 117 n = ubytes; 118 for (i = 0; n && i < XIO_INTERNAL_PAGES; ++i) { 119 if (vm_fault_quick((caddr_t)addr, vmprot) < 0) 120 break; 121 if ((paddr = pmap_kextract(addr)) == 0) 122 break; 123 s = splvm(); 124 m = PHYS_TO_VM_PAGE(paddr); 125 vm_page_hold(m); 126 splx(s); 127 xio->xio_pages[i] = m; 128 ubytes -= n; 129 xio->xio_bytes += n; 130 if ((n = ubytes) > PAGE_SIZE) 131 n = PAGE_SIZE; 132 addr += PAGE_SIZE; 133 } 134 xio->xio_npages = i; 135 136 /* 137 * If a failure occured clean out what we loaded and return EFAULT. 138 * Return 0 on success. 139 */ 140 if (i < XIO_INTERNAL_PAGES && n) { 141 xio_release(xio); 142 xio->xio_error = EFAULT; 143 } 144 } 145 return(xio->xio_error); 146 } 147 148 /* 149 * Initialize an XIO given a kernelspace buffer. 0 is returned on success, 150 * an error code on failure. The actual number of bytes that could be 151 * accomodated in the XIO will be stored in xio_bytes. 152 * 153 * vmprot is usually either VM_PROT_READ or VM_PROT_WRITE. 154 * 155 * Note that you cannot legally accessed a previously cached linmap with 156 * a newly initialized xio until after calling xio_linmap(). 157 */ 158 int 159 xio_init_kbuf(xio_t xio, void *kbase, size_t kbytes) 160 { 161 vm_offset_t addr; 162 vm_paddr_t paddr; 163 vm_page_t m; 164 int i; 165 int n; 166 int s; 167 168 addr = trunc_page((vm_offset_t)kbase); 169 xio->xio_flags = 0; 170 xio->xio_offset = (vm_offset_t)kbase & PAGE_MASK; 171 xio->xio_bytes = 0; 172 xio->xio_pages = xio->xio_internal_pages; 173 xio->xio_error = 0; 174 if ((n = PAGE_SIZE - xio->xio_offset) > kbytes) 175 n = kbytes; 176 for (i = 0; n && i < XIO_INTERNAL_PAGES; ++i) { 177 if ((paddr = pmap_kextract(addr)) == 0) 178 break; 179 s = splvm(); 180 m = PHYS_TO_VM_PAGE(paddr); 181 vm_page_hold(m); 182 splx(s); 183 xio->xio_pages[i] = m; 184 kbytes -= n; 185 xio->xio_bytes += n; 186 if ((n = kbytes) > PAGE_SIZE) 187 n = PAGE_SIZE; 188 addr += PAGE_SIZE; 189 } 190 xio->xio_npages = i; 191 192 /* 193 * If a failure occured clean out what we loaded and return EFAULT. 194 * Return 0 on success. 195 */ 196 if (i < XIO_INTERNAL_PAGES && n) { 197 xio_release(xio); 198 xio->xio_error = EFAULT; 199 } 200 return(xio->xio_error); 201 } 202 203 /* 204 * Cleanup an XIO so it can be destroyed. The pages associated with the 205 * XIO are released. If a linear mapping buffer is active, it will be 206 * unlocked but the mappings will be left intact for optimal reconstitution 207 * in a later xio_linmap() call. 208 * 209 * Note that you cannot legally accessed the linmap on a released XIO. 210 */ 211 void 212 xio_release(xio_t xio) 213 { 214 int i; 215 int s; 216 vm_page_t m; 217 218 s = splvm(); 219 for (i = 0; i < xio->xio_npages; ++i) { 220 m = xio->xio_pages[i]; 221 vm_page_unhold(m); 222 } 223 splx(s); 224 if (xio->xio_flags & XIOF_LINMAP) { 225 xio->xio_flags &= ~XIOF_LINMAP; 226 /* XXX */ 227 } 228 xio->xio_offset = 0; 229 xio->xio_npages = 0; 230 xio->xio_bytes = 0; 231 xio->xio_error = ENOBUFS; 232 } 233 234 /* 235 * Copy data between an XIO and a UIO. If the UIO represents userspace it 236 * must be relative to the current context. Both the UIO and the XIO are 237 * modified, but the XIO's pages are not released when exhausted. 238 * 239 * UIO_READ xio -> uio 240 * UIO_WRITE uio -> xio 241 */ 242 int 243 xio_uio_copy(xio_t xio, struct uio *uio, int *sizep) 244 { 245 int error; 246 int bytes; 247 248 if ((bytes = xio->xio_bytes) > uio->uio_resid) 249 bytes = uio->uio_resid; 250 error = uiomove_fromphys(xio->xio_pages, xio->xio_offset, bytes, uio); 251 if (error == 0) { 252 xio->xio_bytes -= bytes; 253 xio->xio_offset += bytes; 254 *sizep = bytes; 255 } else { 256 *sizep = 0; 257 } 258 return(error); 259 } 260 261 /* 262 * Copy the specified number of bytes from the xio to a userland 263 * buffer. Return an error code or 0 on success. 264 * 265 * The XIO is modified, but the XIO's pages are not released when exhausted. 266 */ 267 int 268 xio_copy_xtou(xio_t xio, void *uptr, int bytes) 269 { 270 int i; 271 int n; 272 int error; 273 int offset; 274 vm_page_t m; 275 struct sf_buf *sf; 276 277 if (bytes > xio->xio_bytes) 278 return(EFAULT); 279 280 offset = xio->xio_offset & PAGE_MASK; 281 if ((n = PAGE_SIZE - offset) > bytes) 282 n = bytes; 283 284 error = 0; 285 for (i = xio->xio_offset >> PAGE_SHIFT; i < xio->xio_npages; ++i) { 286 m = xio->xio_pages[i]; 287 sf = sf_buf_alloc(m, SFBA_QUICK); 288 error = copyout((char *)sf_buf_kva(sf) + offset, uptr, n); 289 sf_buf_free(sf); 290 if (error) 291 break; 292 bytes -= n; 293 xio->xio_bytes -= n; 294 xio->xio_offset += n; 295 uptr = (char *)uptr + n; 296 if (bytes == 0) 297 break; 298 if ((n = bytes) > PAGE_SIZE) 299 n = PAGE_SIZE; 300 offset = 0; 301 } 302 return(error); 303 } 304 305 /* 306 * Copy the specified number of bytes from the xio to a kernel 307 * buffer. Return an error code or 0 on success. 308 * 309 * The XIO is modified, but the XIO's pages are not released when exhausted. 310 */ 311 int 312 xio_copy_xtok(xio_t xio, void *kptr, int bytes) 313 { 314 int i; 315 int n; 316 int error; 317 int offset; 318 vm_page_t m; 319 struct sf_buf *sf; 320 321 if (bytes > xio->xio_bytes) 322 return(EFAULT); 323 324 offset = xio->xio_offset & PAGE_MASK; 325 if ((n = PAGE_SIZE - offset) > bytes) 326 n = bytes; 327 328 error = 0; 329 for (i = xio->xio_offset >> PAGE_SHIFT; i < xio->xio_npages; ++i) { 330 m = xio->xio_pages[i]; 331 sf = sf_buf_alloc(m, SFBA_QUICK); 332 bcopy((char *)sf_buf_kva(sf) + offset, kptr, n); 333 sf_buf_free(sf); 334 bytes -= n; 335 xio->xio_bytes -= n; 336 xio->xio_offset += n; 337 kptr = (char *)kptr + n; 338 if (bytes == 0) 339 break; 340 if ((n = bytes) > PAGE_SIZE) 341 n = PAGE_SIZE; 342 offset = 0; 343 } 344 return(error); 345 } 346 347