xref: /dragonfly/sys/kern/kern_xio.c (revision e8364298)
1 /*
2  * Copyright (c) 2004 Matthew Dillon <dillon@backplane.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  * $DragonFly: src/sys/kern/kern_xio.c,v 1.6 2004/06/05 19:57:35 dillon Exp $
27  */
28 /*
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
34  * very low overheads.
35  *
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.
38  *
39  * TODO: check for busy page when modifying, check writeable.
40  */
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/malloc.h>
45 #include <sys/proc.h>
46 #include <sys/vmmeter.h>
47 #include <sys/vnode.h>
48 #include <sys/xio.h>
49 #include <sys/sfbuf.h>
50 
51 #include <vm/vm.h>
52 #include <vm/vm_param.h>
53 #include <sys/lock.h>
54 #include <vm/vm_kern.h>
55 #include <vm/pmap.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>
63 
64 /*
65  * Just do basic initialization of an empty XIO
66  */
67 void
68 xio_init(xio_t xio)
69 {
70     xio->xio_flags = 0;
71     xio->xio_bytes = 0;
72     xio->xio_error = 0;
73     xio->xio_offset = 0;
74     xio->xio_npages = 0;
75     xio->xio_pages = xio->xio_internal_pages;
76 }
77 
78 /*
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.
82  *
83  * Note that you cannot legally accessed a previously cached linmap with
84  * a newly initialized xio until after calling xio_linmap().
85  */
86 int
87 xio_init_ubuf(xio_t xio, void *ubase, size_t ubytes, int flags)
88 {
89     vm_offset_t addr;
90     vm_paddr_t paddr;
91     vm_page_t m;
92     int i;
93     int n;
94     int s;
95     int vmprot;
96 
97     addr = trunc_page((vm_offset_t)ubase);
98     xio->xio_flags = flags;
99     xio->xio_bytes = 0;
100     xio->xio_error = 0;
101     if (ubytes == 0) {
102 	xio->xio_offset = 0;
103 	xio->xio_npages = 0;
104     } else {
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)
109 	    n = ubytes;
110 	for (i = 0; n && i < XIO_INTERNAL_PAGES; ++i) {
111 	    if (vm_fault_quick((caddr_t)addr, vmprot) < 0)
112 		break;
113 	    if ((paddr = pmap_kextract(addr)) == 0)
114 		break;
115 	    s = splvm();
116 	    m = PHYS_TO_VM_PAGE(paddr);
117 	    vm_page_hold(m);
118 	    splx(s);
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.
144  *
145  * vmprot is usually either VM_PROT_READ or VM_PROT_WRITE.
146  *
147  * Note that you cannot legally accessed a previously cached linmap with
148  * a newly initialized xio until after calling xio_linmap().
149  */
150 int
151 xio_init_kbuf(xio_t xio, void *kbase, size_t kbytes)
152 {
153     vm_offset_t addr;
154     vm_paddr_t paddr;
155     vm_page_t m;
156     int i;
157     int n;
158     int s;
159 
160     addr = trunc_page((vm_offset_t)kbase);
161     xio->xio_flags = 0;
162     xio->xio_offset = (vm_offset_t)kbase & PAGE_MASK;
163     xio->xio_bytes = 0;
164     xio->xio_pages = xio->xio_internal_pages;
165     xio->xio_error = 0;
166     if ((n = PAGE_SIZE - xio->xio_offset) > kbytes)
167 	n = kbytes;
168     for (i = 0; n && i < XIO_INTERNAL_PAGES; ++i) {
169 	if ((paddr = pmap_kextract(addr)) == 0)
170 	    break;
171 	s = splvm();
172 	m = PHYS_TO_VM_PAGE(paddr);
173 	vm_page_hold(m);
174 	splx(s);
175 	xio->xio_pages[i] = m;
176 	kbytes -= n;
177 	xio->xio_bytes += n;
178 	if ((n = kbytes) > PAGE_SIZE)
179 	    n = PAGE_SIZE;
180 	addr += PAGE_SIZE;
181     }
182     xio->xio_npages = i;
183 
184     /*
185      * If a failure occured clean out what we loaded and return EFAULT.
186      * Return 0 on success.
187      */
188     if (i < XIO_INTERNAL_PAGES && n) {
189 	xio_release(xio);
190 	xio->xio_error = EFAULT;
191     }
192     return(xio->xio_error);
193 }
194 
195 /*
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.
200  *
201  * Note that you cannot legally accessed the linmap on a released XIO.
202  */
203 void
204 xio_release(xio_t xio)
205 {
206     int i;
207     int s;
208     vm_page_t m;
209 
210     s = splvm();
211     for (i = 0; i < xio->xio_npages; ++i) {
212 	m = xio->xio_pages[i];
213 	vm_page_unhold(m);
214     }
215     splx(s);
216     if (xio->xio_flags & XIOF_LINMAP) {
217 	xio->xio_flags &= ~XIOF_LINMAP;
218 	/* XXX */
219     }
220     xio->xio_offset = 0;
221     xio->xio_npages = 0;
222     xio->xio_bytes = 0;
223     xio->xio_error = ENOBUFS;
224 }
225 
226 /*
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.
230  *
231  * UIO_READ	xio -> uio
232  * UIO_WRITE	uio -> xio
233  */
234 int
235 xio_uio_copy(xio_t xio, struct uio *uio, int *sizep)
236 {
237     int error;
238     int bytes;
239 
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);
243     if (error == 0) {
244 	xio->xio_bytes -= bytes;
245 	xio->xio_offset += bytes;
246 	*sizep = bytes;
247     } else {
248 	*sizep = 0;
249     }
250     return(error);
251 }
252 
253 /*
254  * Copy the specified number of bytes from the xio to a userland
255  * buffer.  Return an error code or 0 on success.
256  *
257  * The XIO is modified, but the XIO's pages are not released when exhausted.
258  */
259 int
260 xio_copy_xtou(xio_t xio, void *uptr, int bytes)
261 {
262     int i;
263     int n;
264     int error;
265     int offset;
266     vm_page_t m;
267     struct sf_buf *sf;
268 
269     if (bytes > xio->xio_bytes)
270 	return(EFAULT);
271 
272     offset = xio->xio_offset & PAGE_MASK;
273     if ((n = PAGE_SIZE - offset) > bytes)
274 	n = bytes;
275 
276     error = 0;
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);
281 	sf_buf_free(sf);
282 	if (error)
283 	    break;
284 	bytes -= n;
285 	xio->xio_bytes -= n;
286 	xio->xio_offset += n;
287 	uptr = (char *)uptr + n;
288 	if (bytes == 0)
289 	    break;
290 	if ((n = bytes) > PAGE_SIZE)
291 	    n = PAGE_SIZE;
292 	offset = 0;
293     }
294     return(error);
295 }
296 
297 /*
298  * Copy the specified number of bytes from the xio to a kernel
299  * buffer.  Return an error code or 0 on success.
300  *
301  * The XIO is modified, but the XIO's pages are not released when exhausted.
302  */
303 int
304 xio_copy_xtok(xio_t xio, void *kptr, int bytes)
305 {
306     int i;
307     int n;
308     int error;
309     int offset;
310     vm_page_t m;
311     struct sf_buf *sf;
312 
313     if (bytes > xio->xio_bytes)
314 	return(EFAULT);
315 
316     offset = xio->xio_offset & PAGE_MASK;
317     if ((n = PAGE_SIZE - offset) > bytes)
318 	n = bytes;
319 
320     error = 0;
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);
325 	sf_buf_free(sf);
326 	bytes -= n;
327 	xio->xio_bytes -= n;
328 	xio->xio_offset += n;
329 	kptr = (char *)kptr + n;
330 	if (bytes == 0)
331 	    break;
332 	if ((n = bytes) > PAGE_SIZE)
333 	    n = PAGE_SIZE;
334 	offset = 0;
335     }
336     return(error);
337 }
338 
339