2 * Copyright (c) 2004 Matthew Dillon <dillon@backplane.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $DragonFly: src/sys/kern/kern_xio.c,v 1.6 2004/06/05 19:57:35 dillon Exp $
29 * Kernel XIO interface. An initialized XIO is basically a collection of
30 * appropriately held vm_page_t's. XIO buffers are vmspace agnostic and
31 * can represent userspace or kernelspace buffers, and can be passed to
32 * foreign threads outside of the originating vmspace. XIO buffers are
33 * not mapped into KVM and thus can be manipulated and passed around with
36 * The intent is for XIO to be used in the I/O path, VFS, CAPS, and other
37 * places that need to pass (possibly userspace) data between threads.
39 * TODO: check for busy page when modifying, check writeable.
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/malloc.h>
46 #include <sys/vmmeter.h>
47 #include <sys/vnode.h>
49 #include <sys/sfbuf.h>
52 #include <vm/vm_param.h>
54 #include <vm/vm_kern.h>
56 #include <vm/vm_map.h>
57 #include <vm/vm_object.h>
58 #include <vm/vm_page.h>
59 #include <vm/vm_pageout.h>
60 #include <vm/vm_pager.h>
61 #include <vm/vm_extern.h>
62 #include <vm/vm_page2.h>
65 * Just do basic initialization of an empty XIO
75 xio->xio_pages = xio->xio_internal_pages;
79 * Initialize an XIO given a userspace buffer. 0 is returned on success,
80 * an error code on failure. The actual number of bytes that could be
81 * accomodated in the XIO will be stored in xio_bytes.
83 * Note that you cannot legally accessed a previously cached linmap with
84 * a newly initialized xio until after calling xio_linmap().
87 xio_init_ubuf(xio_t xio, void *ubase, size_t ubytes, int flags)
97 addr = trunc_page((vm_offset_t)ubase);
98 xio->xio_flags = flags;
105 vmprot = (flags & XIOF_WRITE) ? VM_PROT_WRITE : VM_PROT_READ;
106 xio->xio_offset = (vm_offset_t)ubase & PAGE_MASK;
107 xio->xio_pages = xio->xio_internal_pages;
108 if ((n = PAGE_SIZE - xio->xio_offset) > ubytes)
110 for (i = 0; n && i < XIO_INTERNAL_PAGES; ++i) {
111 if (vm_fault_quick((caddr_t)addr, vmprot) < 0)
113 if ((paddr = pmap_kextract(addr)) == 0)
116 m = PHYS_TO_VM_PAGE(paddr);
119 xio->xio_pages[i] = m;
122 if ((n = ubytes) > PAGE_SIZE)
129 * If a failure occured clean out what we loaded and return EFAULT.
130 * Return 0 on success.
132 if (i < XIO_INTERNAL_PAGES && n) {
134 xio->xio_error = EFAULT;
137 return(xio->xio_error);
141 * Initialize an XIO given a kernelspace buffer. 0 is returned on success,
142 * an error code on failure. The actual number of bytes that could be
143 * accomodated in the XIO will be stored in xio_bytes.
145 * vmprot is usually either VM_PROT_READ or VM_PROT_WRITE.
147 * Note that you cannot legally accessed a previously cached linmap with
148 * a newly initialized xio until after calling xio_linmap().
151 xio_init_kbuf(xio_t xio, void *kbase, size_t kbytes)
160 addr = trunc_page((vm_offset_t)kbase);
162 xio->xio_offset = (vm_offset_t)kbase & PAGE_MASK;
164 xio->xio_pages = xio->xio_internal_pages;
166 if ((n = PAGE_SIZE - xio->xio_offset) > kbytes)
168 for (i = 0; n && i < XIO_INTERNAL_PAGES; ++i) {
169 if ((paddr = pmap_kextract(addr)) == 0)
172 m = PHYS_TO_VM_PAGE(paddr);
175 xio->xio_pages[i] = m;
178 if ((n = kbytes) > PAGE_SIZE)
185 * If a failure occured clean out what we loaded and return EFAULT.
186 * Return 0 on success.
188 if (i < XIO_INTERNAL_PAGES && n) {
190 xio->xio_error = EFAULT;
192 return(xio->xio_error);
196 * Cleanup an XIO so it can be destroyed. The pages associated with the
197 * XIO are released. If a linear mapping buffer is active, it will be
198 * unlocked but the mappings will be left intact for optimal reconstitution
199 * in a later xio_linmap() call.
201 * Note that you cannot legally accessed the linmap on a released XIO.
204 xio_release(xio_t xio)
211 for (i = 0; i < xio->xio_npages; ++i) {
212 m = xio->xio_pages[i];
216 if (xio->xio_flags & XIOF_LINMAP) {
217 xio->xio_flags &= ~XIOF_LINMAP;
223 xio->xio_error = ENOBUFS;
227 * Copy data between an XIO and a UIO. If the UIO represents userspace it
228 * must be relative to the current context. Both the UIO and the XIO are
229 * modified, but the XIO's pages are not released when exhausted.
231 * UIO_READ xio -> uio
232 * UIO_WRITE uio -> xio
235 xio_uio_copy(xio_t xio, struct uio *uio, int *sizep)
240 if ((bytes = xio->xio_bytes) > uio->uio_resid)
241 bytes = uio->uio_resid;
242 error = uiomove_fromphys(xio->xio_pages, xio->xio_offset, bytes, uio);
244 xio->xio_bytes -= bytes;
245 xio->xio_offset += bytes;
254 * Copy the specified number of bytes from the xio to a userland
255 * buffer. Return an error code or 0 on success.
257 * The XIO is modified, but the XIO's pages are not released when exhausted.
260 xio_copy_xtou(xio_t xio, void *uptr, int bytes)
269 if (bytes > xio->xio_bytes)
272 offset = xio->xio_offset & PAGE_MASK;
273 if ((n = PAGE_SIZE - offset) > bytes)
277 for (i = xio->xio_offset >> PAGE_SHIFT; i < xio->xio_npages; ++i) {
278 m = xio->xio_pages[i];
279 sf = sf_buf_alloc(m, SFBA_QUICK);
280 error = copyout((char *)sf_buf_kva(sf) + offset, uptr, n);
286 xio->xio_offset += n;
287 uptr = (char *)uptr + n;
290 if ((n = bytes) > PAGE_SIZE)
298 * Copy the specified number of bytes from the xio to a kernel
299 * buffer. Return an error code or 0 on success.
301 * The XIO is modified, but the XIO's pages are not released when exhausted.
304 xio_copy_xtok(xio_t xio, void *kptr, int bytes)
313 if (bytes > xio->xio_bytes)
316 offset = xio->xio_offset & PAGE_MASK;
317 if ((n = PAGE_SIZE - offset) > bytes)
321 for (i = xio->xio_offset >> PAGE_SHIFT; i < xio->xio_npages; ++i) {
322 m = xio->xio_pages[i];
323 sf = sf_buf_alloc(m, SFBA_QUICK);
324 bcopy((char *)sf_buf_kva(sf) + offset, kptr, n);
328 xio->xio_offset += n;
329 kptr = (char *)kptr + n;
332 if ((n = bytes) > PAGE_SIZE)