2 * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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
34 * $DragonFly: src/sys/kern/kern_xio.c,v 1.16 2008/05/09 07:24:45 dillon Exp $
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
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.
47 * TODO: check for busy page when modifying, check writeable.
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/malloc.h>
54 #include <sys/vmmeter.h>
55 #include <sys/vnode.h>
57 #include <sys/sfbuf.h>
60 #include <vm/vm_param.h>
62 #include <vm/vm_kern.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>
73 * Just do basic initialization of an empty XIO
83 xio
->xio_pages
= xio
->xio_internal_pages
;
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.
93 xio_init_ubuf(xio_t xio
, void *ubase
, size_t ubytes
, int flags
)
103 addr
= trunc_page((vm_offset_t
)ubase
);
104 xio
->xio_flags
= flags
;
111 vmprot
= (flags
& XIOF_WRITE
) ? VM_PROT_WRITE
: VM_PROT_READ
;
112 xio
->xio_offset
= (vm_offset_t
)ubase
& PAGE_MASK
;
113 xio
->xio_pages
= xio
->xio_internal_pages
;
114 if ((n
= PAGE_SIZE
- xio
->xio_offset
) > ubytes
)
117 for (i
= 0; n
&& i
< XIO_INTERNAL_PAGES
; ++i
) {
118 m
= vm_fault_page_quick(addr
, vmprot
, &error
);
121 xio
->xio_pages
[i
] = m
;
124 if ((n
= ubytes
) > PAGE_SIZE
)
129 * Check linearity, used by syslink to memory map DMA buffers.
131 if (flags
& XIOF_VMLINEAR
) {
135 if (m
->object
!= m0
->object
|| m
->pindex
!= m0
->pindex
+ i
) {
144 * If a failure occured clean out what we loaded and return EFAULT.
145 * Return 0 on success. Do not dirty the pages.
147 if (i
< XIO_INTERNAL_PAGES
&& n
) {
148 xio
->xio_flags
&= ~XIOF_WRITE
;
150 xio
->xio_error
= EFAULT
;
153 return(xio
->xio_error
);
157 * Initialize an XIO given a kernelspace buffer. 0 is returned on success,
158 * an error code on failure. The actual number of bytes that could be
159 * accomodated in the XIO will be stored in xio_bytes and the page offset
160 * will be stored in xio_offset.
163 xio_init_kbuf(xio_t xio
, void *kbase
, size_t kbytes
)
171 addr
= trunc_page((vm_offset_t
)kbase
);
173 xio
->xio_offset
= (vm_offset_t
)kbase
& PAGE_MASK
;
175 xio
->xio_pages
= xio
->xio_internal_pages
;
177 if ((n
= PAGE_SIZE
- xio
->xio_offset
) > kbytes
)
179 for (i
= 0; n
&& i
< XIO_INTERNAL_PAGES
; ++i
) {
180 if ((paddr
= pmap_kextract(addr
)) == 0)
183 m
= PHYS_TO_VM_PAGE(paddr
);
186 xio
->xio_pages
[i
] = m
;
189 if ((n
= kbytes
) > PAGE_SIZE
)
196 * If a failure occured clean out what we loaded and return EFAULT.
197 * Return 0 on success.
199 if (i
< XIO_INTERNAL_PAGES
&& n
) {
201 xio
->xio_error
= EFAULT
;
203 return(xio
->xio_error
);
207 * Initialize an XIO given an array of vm_page pointers. The caller is
208 * responsible for any modified state changes for the pages.
211 xio_init_pages(xio_t xio
, struct vm_page
**mbase
, int npages
, int xflags
)
215 KKASSERT(npages
<= XIO_INTERNAL_PAGES
);
217 xio
->xio_flags
= xflags
;
220 xio
->xio_pages
= xio
->xio_internal_pages
;
221 xio
->xio_npages
= npages
;
224 for (i
= 0; i
< npages
; ++i
) {
225 vm_page_hold(mbase
[i
]);
226 xio
->xio_pages
[i
] = mbase
[i
];
233 * Cleanup an XIO so it can be destroyed. The pages associated with the
237 xio_release(xio_t xio
)
243 for (i
= 0; i
< xio
->xio_npages
; ++i
) {
244 m
= xio
->xio_pages
[i
];
245 if (xio
->xio_flags
& XIOF_WRITE
)
253 xio
->xio_error
= ENOBUFS
;
257 * Copy data between an XIO and a UIO. If the UIO represents userspace it
258 * must be relative to the current context.
260 * uoffset is the abstracted starting offset in the XIO, not the actual
261 * offset, and usually starts at 0.
263 * The XIO is not modified. The UIO is updated to reflect the copy.
265 * UIO_READ xio -> uio
266 * UIO_WRITE uio -> xio
269 xio_uio_copy(xio_t xio
, int uoffset
, struct uio
*uio
, int *sizep
)
274 bytes
= xio
->xio_bytes
- uoffset
;
275 if (bytes
> uio
->uio_resid
)
276 bytes
= uio
->uio_resid
;
277 KKASSERT(bytes
>= 0);
278 error
= uiomove_fromphys(xio
->xio_pages
, xio
->xio_offset
+ uoffset
,
288 * Copy the specified number of bytes from the xio to a userland
289 * buffer. Return an error code or 0 on success.
291 * uoffset is the abstracted starting offset in the XIO, not the actual
292 * offset, and usually starts at 0.
294 * The XIO is not modified.
297 xio_copy_xtou(xio_t xio
, int uoffset
, void *uptr
, int bytes
)
306 if (uoffset
+ bytes
> xio
->xio_bytes
)
309 offset
= (xio
->xio_offset
+ uoffset
) & PAGE_MASK
;
310 if ((n
= PAGE_SIZE
- offset
) > bytes
)
314 for (i
= (xio
->xio_offset
+ uoffset
) >> PAGE_SHIFT
;
318 m
= xio
->xio_pages
[i
];
319 sf
= sf_buf_alloc(m
, SFB_CPUPRIVATE
);
320 error
= copyout((char *)sf_buf_kva(sf
) + offset
, uptr
, n
);
325 uptr
= (char *)uptr
+ n
;
328 if ((n
= bytes
) > PAGE_SIZE
)
336 * Copy the specified number of bytes from the xio to a kernel
337 * buffer. Return an error code or 0 on success.
339 * uoffset is the abstracted starting offset in the XIO, not the actual
340 * offset, and usually starts at 0.
342 * The XIO is not modified.
345 xio_copy_xtok(xio_t xio
, int uoffset
, void *kptr
, int bytes
)
354 if (bytes
+ uoffset
> xio
->xio_bytes
)
357 offset
= (xio
->xio_offset
+ uoffset
) & PAGE_MASK
;
358 if ((n
= PAGE_SIZE
- offset
) > bytes
)
362 for (i
= (xio
->xio_offset
+ uoffset
) >> PAGE_SHIFT
;
366 m
= xio
->xio_pages
[i
];
367 sf
= sf_buf_alloc(m
, SFB_CPUPRIVATE
);
368 bcopy((char *)sf_buf_kva(sf
) + offset
, kptr
, n
);
371 kptr
= (char *)kptr
+ n
;
374 if ((n
= bytes
) > PAGE_SIZE
)
382 * Copy the specified number of bytes from userland to the xio.
383 * Return an error code or 0 on success.
385 * uoffset is the abstracted starting offset in the XIO, not the actual
386 * offset, and usually starts at 0.
388 * Data in pages backing the XIO will be modified.
391 xio_copy_utox(xio_t xio
, int uoffset
, const void *uptr
, int bytes
)
400 if (uoffset
+ bytes
> xio
->xio_bytes
)
403 offset
= (xio
->xio_offset
+ uoffset
) & PAGE_MASK
;
404 if ((n
= PAGE_SIZE
- offset
) > bytes
)
408 for (i
= (xio
->xio_offset
+ uoffset
) >> PAGE_SHIFT
;
412 m
= xio
->xio_pages
[i
];
413 sf
= sf_buf_alloc(m
, SFB_CPUPRIVATE
);
414 error
= copyin(uptr
, (char *)sf_buf_kva(sf
) + offset
, n
);
419 uptr
= (const char *)uptr
+ n
;
422 if ((n
= bytes
) > PAGE_SIZE
)
430 * Copy the specified number of bytes from the kernel to the xio.
431 * Return an error code or 0 on success.
433 * uoffset is the abstracted starting offset in the XIO, not the actual
434 * offset, and usually starts at 0.
436 * Data in pages backing the XIO will be modified.
439 xio_copy_ktox(xio_t xio
, int uoffset
, const void *kptr
, int bytes
)
448 if (uoffset
+ bytes
> xio
->xio_bytes
)
451 offset
= (xio
->xio_offset
+ uoffset
) & PAGE_MASK
;
452 if ((n
= PAGE_SIZE
- offset
) > bytes
)
456 for (i
= (xio
->xio_offset
+ uoffset
) >> PAGE_SHIFT
;
460 m
= xio
->xio_pages
[i
];
461 sf
= sf_buf_alloc(m
, SFB_CPUPRIVATE
);
462 bcopy(kptr
, (char *)sf_buf_kva(sf
) + offset
, n
);
465 kptr
= (const char *)kptr
+ n
;
468 if ((n
= bytes
) > PAGE_SIZE
)