More -Wwrite-strings cleanup and make sure you can actually play it.
[dragonfly.git] / sys / kern / kern_xio.c
blob572f4ac2b060b7e73671c3ede53a6e20bcee9247
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:
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.
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.
34 * $DragonFly: src/sys/kern/kern_xio.c,v 1.9 2005/03/02 18:42:08 hmp 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
42 * very low overheads.
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>
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>
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>
73 * Just do basic initialization of an empty XIO
75 void
76 xio_init(xio_t xio)
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;
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.
92 int
93 xio_init_ubuf(xio_t xio, void *ubase, size_t ubytes, int flags)
95 vm_offset_t addr;
96 vm_paddr_t paddr;
97 vm_page_t m;
98 int i;
99 int n;
100 int s;
101 int vmprot;
103 addr = trunc_page((vm_offset_t)ubase);
104 xio->xio_flags = flags;
105 xio->xio_bytes = 0;
106 xio->xio_error = 0;
107 if (ubytes == 0) {
108 xio->xio_offset = 0;
109 xio->xio_npages = 0;
110 } else {
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)
115 n = ubytes;
116 for (i = 0; n && i < XIO_INTERNAL_PAGES; ++i) {
117 if (vm_fault_quick((caddr_t)addr, vmprot) < 0)
118 break;
119 if ((paddr = pmap_kextract(addr)) == 0)
120 break;
121 s = splvm();
122 m = PHYS_TO_VM_PAGE(paddr);
123 vm_page_hold(m);
124 splx(s);
125 xio->xio_pages[i] = m;
126 ubytes -= n;
127 xio->xio_bytes += n;
128 if ((n = ubytes) > PAGE_SIZE)
129 n = PAGE_SIZE;
130 addr += PAGE_SIZE;
132 xio->xio_npages = i;
135 * If a failure occured clean out what we loaded and return EFAULT.
136 * Return 0 on success.
138 if (i < XIO_INTERNAL_PAGES && n) {
139 xio_release(xio);
140 xio->xio_error = EFAULT;
143 return(xio->xio_error);
147 * Initialize an XIO given a kernelspace buffer. 0 is returned on success,
148 * an error code on failure. The actual number of bytes that could be
149 * accomodated in the XIO will be stored in xio_bytes and the page offset
150 * will be stored in xio_offset.
153 xio_init_kbuf(xio_t xio, void *kbase, size_t kbytes)
155 vm_offset_t addr;
156 vm_paddr_t paddr;
157 vm_page_t m;
158 int i;
159 int n;
160 int s;
162 addr = trunc_page((vm_offset_t)kbase);
163 xio->xio_flags = 0;
164 xio->xio_offset = (vm_offset_t)kbase & PAGE_MASK;
165 xio->xio_bytes = 0;
166 xio->xio_pages = xio->xio_internal_pages;
167 xio->xio_error = 0;
168 if ((n = PAGE_SIZE - xio->xio_offset) > kbytes)
169 n = kbytes;
170 for (i = 0; n && i < XIO_INTERNAL_PAGES; ++i) {
171 if ((paddr = pmap_kextract(addr)) == 0)
172 break;
173 s = splvm();
174 m = PHYS_TO_VM_PAGE(paddr);
175 vm_page_hold(m);
176 splx(s);
177 xio->xio_pages[i] = m;
178 kbytes -= n;
179 xio->xio_bytes += n;
180 if ((n = kbytes) > PAGE_SIZE)
181 n = PAGE_SIZE;
182 addr += PAGE_SIZE;
184 xio->xio_npages = i;
187 * If a failure occured clean out what we loaded and return EFAULT.
188 * Return 0 on success.
190 if (i < XIO_INTERNAL_PAGES && n) {
191 xio_release(xio);
192 xio->xio_error = EFAULT;
194 return(xio->xio_error);
198 * Cleanup an XIO so it can be destroyed. The pages associated with the
199 * XIO are released.
201 void
202 xio_release(xio_t xio)
204 int i;
205 int s;
206 vm_page_t m;
208 s = splvm();
209 for (i = 0; i < xio->xio_npages; ++i) {
210 m = xio->xio_pages[i];
211 vm_page_unhold(m);
213 splx(s);
214 xio->xio_offset = 0;
215 xio->xio_npages = 0;
216 xio->xio_bytes = 0;
217 xio->xio_error = ENOBUFS;
221 * Copy data between an XIO and a UIO. If the UIO represents userspace it
222 * must be relative to the current context.
224 * uoffset is the abstracted starting offset in the XIO, not the actual
225 * offset, and usually starts at 0.
227 * The XIO is not modified. The UIO is updated to reflect the copy.
229 * UIO_READ xio -> uio
230 * UIO_WRITE uio -> xio
233 xio_uio_copy(xio_t xio, int uoffset, struct uio *uio, int *sizep)
235 int error;
236 int bytes;
238 bytes = xio->xio_bytes - uoffset;
239 if (bytes > uio->uio_resid)
240 bytes = uio->uio_resid;
241 KKASSERT(bytes >= 0);
242 error = uiomove_fromphys(xio->xio_pages, xio->xio_offset + uoffset,
243 bytes, uio);
244 if (error == 0)
245 *sizep = bytes;
246 else
247 *sizep = 0;
248 return(error);
252 * Copy the specified number of bytes from the xio to a userland
253 * buffer. Return an error code or 0 on success.
255 * uoffset is the abstracted starting offset in the XIO, not the actual
256 * offset, and usually starts at 0.
258 * The XIO is not modified.
261 xio_copy_xtou(xio_t xio, int uoffset, void *uptr, int bytes)
263 int i;
264 int n;
265 int error;
266 int offset;
267 vm_page_t m;
268 struct sf_buf *sf;
270 if (bytes > xio->xio_bytes)
271 return(EFAULT);
273 offset = (xio->xio_offset + uoffset) & PAGE_MASK;
274 if ((n = PAGE_SIZE - offset) > bytes)
275 n = bytes;
277 error = 0;
278 for (i = (xio->xio_offset + uoffset) >> PAGE_SHIFT;
279 i < xio->xio_npages;
282 m = xio->xio_pages[i];
283 sf = sf_buf_alloc(m, SFB_CPUPRIVATE);
284 error = copyout((char *)sf_buf_kva(sf) + offset, uptr, n);
285 sf_buf_free(sf);
286 if (error)
287 break;
288 bytes -= n;
289 uptr = (char *)uptr + n;
290 if (bytes == 0)
291 break;
292 if ((n = bytes) > PAGE_SIZE)
293 n = PAGE_SIZE;
294 offset = 0;
296 return(error);
300 * Copy the specified number of bytes from the xio to a kernel
301 * buffer. Return an error code or 0 on success.
303 * uoffset is the abstracted starting offset in the XIO, not the actual
304 * offset, and usually starts at 0.
306 * The XIO is not modified.
309 xio_copy_xtok(xio_t xio, int uoffset, void *kptr, int bytes)
311 int i;
312 int n;
313 int error;
314 int offset;
315 vm_page_t m;
316 struct sf_buf *sf;
318 if (bytes + uoffset > xio->xio_bytes)
319 return(EFAULT);
321 offset = (xio->xio_offset + uoffset) & PAGE_MASK;
322 if ((n = PAGE_SIZE - offset) > bytes)
323 n = bytes;
325 error = 0;
326 for (i = (xio->xio_offset + uoffset) >> PAGE_SHIFT;
327 i < xio->xio_npages;
330 m = xio->xio_pages[i];
331 sf = sf_buf_alloc(m, SFB_CPUPRIVATE);
332 bcopy((char *)sf_buf_kva(sf) + offset, kptr, n);
333 sf_buf_free(sf);
334 bytes -= n;
335 kptr = (char *)kptr + n;
336 if (bytes == 0)
337 break;
338 if ((n = bytes) > PAGE_SIZE)
339 n = PAGE_SIZE;
340 offset = 0;
342 return(error);