Merge branch 'vendor/XZ' into HEAD
[dragonfly.git] / sys / platform / vkernel / platform / copyio.c
1 /*
2  * Copyright (c) 2006 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
35 #include <sys/types.h>
36 #include <sys/systm.h>
37 #include <cpu/lwbuf.h>
38 #include <vm/vm_page.h>
39 #include <vm/vm_extern.h>
40 #include <assert.h>
41
42 #include <sys/stat.h>
43 #include <sys/mman.h>
44
45 /*
46  * A bcopy that works dring low level boot, before FP is working
47  */
48 void
49 ovbcopy(const void *src, void *dst, size_t len)
50 {
51         bcopy(src, dst, len);
52 }
53
54 void
55 bcopyi(const void *src, void *dst, size_t len)
56 {
57         bcopy(src, dst, len);
58 }
59
60 int
61 copystr(const void *kfaddr, void *kdaddr, size_t len, size_t *lencopied)
62 {
63         size_t i;
64
65         for (i = 0; i < len; ++i) {
66                 if ((((char *)kdaddr)[i] = ((const char *)kfaddr)[i]) == 0) {
67                         if (lencopied)
68                                 *lencopied = i + 1;
69                         return(0);
70                 }
71         }
72         return (ENAMETOOLONG);
73 }
74
75 /*
76  * Copies a NUL-terminated string from user space to kernel space.
77  * The number of bytes copied, including the terminator, is returned in
78  * (*res).
79  *
80  * Returns 0 on success, EFAULT or ENAMETOOLONG on failure.
81  */
82 int
83 copyinstr(const void *udaddr, void *kaddr, size_t len, size_t *res)
84 {
85         int error;
86         size_t n;
87         const char *uptr = udaddr;
88         char *kptr = kaddr;
89
90         if (res)
91                 *res = 0;
92         while (len) {
93                 n = PAGE_SIZE - ((vm_offset_t)uptr & PAGE_MASK);
94                 if (n > 32)
95                         n = 32;
96                 if (n > len)
97                         n = len;
98                 if ((error = copyin(uptr, kptr, n)) != 0)
99                         return(error);
100                 while (n) {
101                         if (res)
102                                 ++*res;
103                         if (*kptr == 0)
104                                 return(0);
105                         ++kptr;
106                         ++uptr;
107                         --n;
108                         --len;
109                 }
110
111         }
112         return(ENAMETOOLONG);
113 }
114
115 /*
116  * Copy a binary buffer from user space to kernel space.
117  *
118  * NOTE: on a real system copyin/copyout are MP safe, but the current
119  * implementation on a vkernel is not so we get the mp lock.
120  *
121  * Returns 0 on success, EFAULT on failure.
122  */
123 int
124 copyin(const void *udaddr, void *kaddr, size_t len)
125 {
126         struct vmspace *vm = curproc->p_vmspace;
127         struct lwbuf *lwb;
128         vm_page_t m;
129         int error;
130         size_t n;
131
132         error = 0;
133         while (len) {
134                 m = vm_fault_page(&vm->vm_map, trunc_page((vm_offset_t)udaddr),
135                                   VM_PROT_READ,
136                                   VM_FAULT_NORMAL, &error);
137                 if (error)
138                         break;
139                 n = PAGE_SIZE - ((vm_offset_t)udaddr & PAGE_MASK);
140                 if (n > len)
141                         n = len;
142                 lwb = lwbuf_alloc(m);
143                 bcopy((char *)lwbuf_kva(lwb)+((vm_offset_t)udaddr & PAGE_MASK),
144                       kaddr, n);
145                 len -= n;
146                 udaddr = (const char *)udaddr + n;
147                 kaddr = (char *)kaddr + n;
148                 lwbuf_free(lwb);
149                 vm_page_unhold(m);
150         }
151         return (error);
152 }
153
154 /*
155  * Copy a binary buffer from kernel space to user space.
156  *
157  * Returns 0 on success, EFAULT on failure.
158  */
159 int
160 copyout(const void *kaddr, void *udaddr, size_t len)
161 {
162         struct vmspace *vm = curproc->p_vmspace;
163         struct lwbuf *lwb;
164         vm_page_t m;
165         int error;
166         size_t n;
167
168         error = 0;
169         while (len) {
170                 m = vm_fault_page(&vm->vm_map, trunc_page((vm_offset_t)udaddr),
171                                   VM_PROT_READ|VM_PROT_WRITE, 
172                                   VM_FAULT_NORMAL, &error);
173                 if (error)
174                         break;
175                 n = PAGE_SIZE - ((vm_offset_t)udaddr & PAGE_MASK);
176                 if (n > len)
177                         n = len;
178                 lwb = lwbuf_alloc(m);
179                 bcopy(kaddr, (char *)lwbuf_kva(lwb) +
180                              ((vm_offset_t)udaddr & PAGE_MASK), n);
181                 len -= n;
182                 udaddr = (char *)udaddr + n;
183                 kaddr = (const char *)kaddr + n;
184                 vm_page_dirty(m);
185                 lwbuf_free(lwb);
186                 vm_page_unhold(m);
187         }
188         return (error);
189 }
190  
191 /*
192  * Fetch the byte at the specified user address.  Returns -1 on failure.
193  */
194 int
195 fubyte(const void *base)
196 {
197         unsigned char c;
198
199         if (copyin(base, &c, 1) == 0)
200                 return((int)c);
201         return(-1);
202 }
203
204 /*
205  * Store a byte at the specified user address.  Returns -1 on failure.
206  */
207 int
208 subyte (void *base, int byte)
209 {
210         unsigned char c = byte;
211
212         if (copyout(&c, base, 1) == 0)
213                 return(0);
214         return(-1);
215 }
216
217 /*
218  * Fetch a word (integer, 32 bits) from user space
219  */
220 long
221 fuword(const void *base)
222 {
223         long v;
224
225         if (copyin(base, &v, sizeof(v)) == 0)
226                 return((long)v);
227         return(-1);
228 }
229
230 /*
231  * Store a word (integer, 32 bits) to user space
232  */
233 int
234 suword(void *base, long word)
235 {
236         if (copyout(&word, base, sizeof(word)) == 0)
237                 return(0);
238         return(-1);
239 }
240
241 /*
242  * Fetch an short word (16 bits) from user space
243  */
244 int
245 fusword(void *base)
246 {
247         unsigned short sword;
248
249         if (copyin(base, &sword, sizeof(sword)) == 0)
250                 return((int)sword);
251         return(-1);
252 }
253
254 /*
255  * Store a short word (16 bits) to user space
256  */
257 int
258 susword (void *base, int word)
259 {
260         unsigned short sword = word;
261
262         if (copyout(&sword, base, sizeof(sword)) == 0)
263                 return(0);
264         return(-1);
265 }