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