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.4 2004/04/03 08:20:10 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 * Initialize an XIO given a userspace buffer. 0 is returned on success,
66 * an error code on failure. The actual number of bytes that could be
67 * accomodated in the XIO will be stored in xio_bytes.
70 xio_init_ubuf(xio_t xio, void *ubase, size_t ubytes, int flags)
79 addr = trunc_page((vm_offset_t)ubase);
80 xio->xio_flags = flags;
87 vmprot = (flags & XIOF_WRITE) ? VM_PROT_WRITE : VM_PROT_READ;
88 xio->xio_offset = (vm_offset_t)ubase & PAGE_MASK;
89 xio->xio_pages = xio->xio_internal_pages;
90 if ((n = PAGE_SIZE - xio->xio_offset) > ubytes)
92 for (i = 0; n && i < XIO_INTERNAL_PAGES; ++i) {
93 if (vm_fault_quick((caddr_t)addr, vmprot) < 0)
95 if ((paddr = pmap_kextract(addr)) == 0)
97 m = PHYS_TO_VM_PAGE(paddr);
99 xio->xio_pages[i] = m;
102 if ((n = ubytes) > PAGE_SIZE)
109 * If a failure occured clean out what we loaded and return EFAULT.
110 * Return 0 on success.
112 if (i < XIO_INTERNAL_PAGES && n) {
114 xio->xio_error = EFAULT;
117 return(xio->xio_error);
121 * Initialize an XIO given a kernelspace buffer. 0 is returned on success,
122 * an error code on failure. The actual number of bytes that could be
123 * accomodated in the XIO will be stored in xio_bytes.
125 * vmprot is usually either VM_PROT_READ or VM_PROT_WRITE.
128 xio_init_kbuf(xio_t xio, void *kbase, size_t kbytes)
136 addr = trunc_page((vm_offset_t)kbase);
138 xio->xio_offset = (vm_offset_t)kbase & PAGE_MASK;
140 xio->xio_pages = xio->xio_internal_pages;
142 if ((n = PAGE_SIZE - xio->xio_offset) > kbytes)
144 for (i = 0; n && i < XIO_INTERNAL_PAGES; ++i) {
145 if ((paddr = pmap_kextract(addr)) == 0)
147 m = PHYS_TO_VM_PAGE(paddr);
149 xio->xio_pages[i] = m;
152 if ((n = kbytes) > PAGE_SIZE)
159 * If a failure occured clean out what we loaded and return EFAULT.
160 * Return 0 on success.
162 if (i < XIO_INTERNAL_PAGES && n) {
164 xio->xio_error = EFAULT;
166 return(xio->xio_error);
170 xio_release(xio_t xio)
175 for (i = 0; i < xio->xio_npages; ++i) {
176 m = xio->xio_pages[i];
182 xio->xio_error = ENOBUFS;
186 * Copy data between an XIO and a UIO. If the UIO represents userspace it
187 * must be relative to the current context. Both the UIO and the XIO are
188 * modified, but the XIO's pages are not released when exhausted.
190 * UIO_READ xio -> uio
191 * UIO_WRITE uio -> xio
194 xio_uio_copy(xio_t xio, struct uio *uio, int *sizep)
199 if ((bytes = xio->xio_bytes) > uio->uio_resid)
200 bytes = uio->uio_resid;
201 error = uiomove_fromphys(xio->xio_pages, xio->xio_offset, bytes, uio);
203 xio->xio_bytes -= bytes;
204 xio->xio_offset += bytes;
213 * Copy the specified number of bytes from the xio to a userland
214 * buffer. Return an error code or 0 on success.
216 * The XIO is modified, but the XIO's pages are not released when exhausted.
219 xio_copy_xtou(xio_t xio, void *uptr, int bytes)
228 if (bytes > xio->xio_bytes)
231 offset = xio->xio_offset & PAGE_MASK;
232 if ((n = PAGE_SIZE - offset) > bytes)
236 for (i = xio->xio_offset >> PAGE_SHIFT; i < xio->xio_npages; ++i) {
237 m = xio->xio_pages[i];
238 sf = sf_buf_alloc(m, SFBA_QUICK);
239 error = copyout((char *)sf_buf_kva(sf) + offset, uptr, n);
245 xio->xio_offset += n;
246 uptr = (char *)uptr + n;
249 if ((n = bytes) > PAGE_SIZE)
257 * Copy the specified number of bytes from the xio to a kernel
258 * buffer. Return an error code or 0 on success.
260 * The XIO is modified, but the XIO's pages are not released when exhausted.
263 xio_copy_xtok(xio_t xio, void *kptr, int bytes)
272 if (bytes > xio->xio_bytes)
275 offset = xio->xio_offset & PAGE_MASK;
276 if ((n = PAGE_SIZE - offset) > bytes)
280 for (i = xio->xio_offset >> PAGE_SHIFT; i < xio->xio_npages; ++i) {
281 m = xio->xio_pages[i];
282 sf = sf_buf_alloc(m, SFBA_QUICK);
283 bcopy((char *)sf_buf_kva(sf) + offset, kptr, n);
287 xio->xio_offset += n;
288 kptr = (char *)kptr + n;
291 if ((n = bytes) > PAGE_SIZE)