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