| Commit | Line | Data |
|---|---|---|
| 8a9d8995 MD |
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. | |
| 8a9d8995 MD |
33 | */ |
| 34 | ||
| 35 | #include <sys/types.h> | |
| 36 | #include <sys/systm.h> | |
| 5c5185ae | 37 | #include <cpu/lwbuf.h> |
| 8a9d8995 | 38 | #include <vm/vm_page.h> |
| 6092278a | 39 | #include <vm/vm_extern.h> |
| 8a9d8995 MD |
40 | #include <assert.h> |
| 41 | ||
| 6092278a MD |
42 | #include <sys/stat.h> |
| 43 | #include <sys/mman.h> | |
| 44 | ||
| 684a93c4 MD |
45 | #include <sys/mplock2.h> |
| 46 | ||
| 8a9d8995 | 47 | /* |
| e4a473f1 MD |
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 | ||
| 6f7b98e0 MD |
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) { | |
| 0171c06b MD |
69 | if (lencopied) |
| 70 | *lencopied = i + 1; | |
| 6f7b98e0 MD |
71 | return(0); |
| 72 | } | |
| 73 | } | |
| 74 | return (ENAMETOOLONG); | |
| 75 | } | |
| 76 | ||
| e4a473f1 | 77 | /* |
| 8a9d8995 MD |
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 | { | |
| 6092278a MD |
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); | |
| 8a9d8995 MD |
115 | } |
| 116 | ||
| 117 | /* | |
| 118 | * Copy a binary buffer from user space to kernel space. | |
| 119 | * | |
| 7e522155 MD |
120 | * NOTE: on a real system copyin/copyout are MP safe, but the current |
| 121 | * implementation on a vkernel is not so we get the mp lock. | |
| 122 | * | |
| 8a9d8995 MD |
123 | * Returns 0 on success, EFAULT on failure. |
| 124 | */ | |
| 125 | int | |
| 6092278a | 126 | copyin(const void *udaddr, void *kaddr, size_t len) |
| 8a9d8995 | 127 | { |
| 6092278a | 128 | struct vmspace *vm = curproc->p_vmspace; |
| 5c5185ae | 129 | struct lwbuf *lwb; |
| 6092278a MD |
130 | vm_page_t m; |
| 131 | int error; | |
| 132 | size_t n; | |
| 133 | ||
| 134 | error = 0; | |
| 7e522155 | 135 | get_mplock(); |
| 6092278a MD |
136 | while (len) { |
| 137 | m = vm_fault_page(&vm->vm_map, trunc_page((vm_offset_t)udaddr), | |
| 0035dca9 MD |
138 | VM_PROT_READ, |
| 139 | VM_FAULT_NORMAL, &error); | |
| 6092278a MD |
140 | if (error) |
| 141 | break; | |
| 142 | n = PAGE_SIZE - ((vm_offset_t)udaddr & PAGE_MASK); | |
| 143 | if (n > len) | |
| 144 | n = len; | |
| 5c5185ae SG |
145 | lwb = lwbuf_alloc(m); |
| 146 | bcopy((char *)lwbuf_kva(lwb)+((vm_offset_t)udaddr & PAGE_MASK), | |
| 6092278a MD |
147 | kaddr, n); |
| 148 | len -= n; | |
| 149 | udaddr = (const char *)udaddr + n; | |
| 150 | kaddr = (char *)kaddr + n; | |
| 5c5185ae | 151 | lwbuf_free(lwb); |
| 573fb415 | 152 | vm_page_unhold(m); |
| 6092278a | 153 | } |
| 7e522155 | 154 | rel_mplock(); |
| 6092278a | 155 | return (error); |
| 8a9d8995 MD |
156 | } |
| 157 | ||
| 158 | /* | |
| 159 | * Copy a binary buffer from kernel space to user space. | |
| 160 | * | |
| 161 | * Returns 0 on success, EFAULT on failure. | |
| 162 | */ | |
| 163 | int | |
| 6092278a | 164 | copyout(const void *kaddr, void *udaddr, size_t len) |
| 8a9d8995 | 165 | { |
| 6092278a | 166 | struct vmspace *vm = curproc->p_vmspace; |
| 5c5185ae | 167 | struct lwbuf *lwb; |
| 6092278a MD |
168 | vm_page_t m; |
| 169 | int error; | |
| 170 | size_t n; | |
| 171 | ||
| 172 | error = 0; | |
| 7e522155 | 173 | get_mplock(); |
| 6092278a MD |
174 | while (len) { |
| 175 | m = vm_fault_page(&vm->vm_map, trunc_page((vm_offset_t)udaddr), | |
| 0035dca9 MD |
176 | VM_PROT_READ|VM_PROT_WRITE, |
| 177 | VM_FAULT_NORMAL, &error); | |
| 6092278a MD |
178 | if (error) |
| 179 | break; | |
| 180 | n = PAGE_SIZE - ((vm_offset_t)udaddr & PAGE_MASK); | |
| 181 | if (n > len) | |
| 182 | n = len; | |
| 5c5185ae SG |
183 | lwb = lwbuf_alloc(m); |
| 184 | bcopy(kaddr, (char *)lwbuf_kva(lwb) + | |
| 6092278a MD |
185 | ((vm_offset_t)udaddr & PAGE_MASK), n); |
| 186 | len -= n; | |
| 187 | udaddr = (char *)udaddr + n; | |
| 188 | kaddr = (const char *)kaddr + n; | |
| 17cde63e | 189 | vm_page_dirty(m); |
| 5c5185ae | 190 | lwbuf_free(lwb); |
| 573fb415 | 191 | vm_page_unhold(m); |
| 6092278a | 192 | } |
| 7e522155 | 193 | rel_mplock(); |
| 6092278a | 194 | return (error); |
| 8a9d8995 MD |
195 | } |
| 196 | ||
| 197 | /* | |
| 198 | * Fetch the byte at the specified user address. Returns -1 on failure. | |
| 199 | */ | |
| 200 | int | |
| 201 | fubyte(const void *base) | |
| 202 | { | |
| 6092278a | 203 | unsigned char c; |
| 6092278a | 204 | |
| c0a27981 | 205 | if (copyin(base, &c, 1) == 0) |
| 6092278a MD |
206 | return((int)c); |
| 207 | return(-1); | |
| 8a9d8995 MD |
208 | } |
| 209 | ||
| 210 | /* | |
| 211 | * Store a byte at the specified user address. Returns -1 on failure. | |
| 212 | */ | |
| 213 | int | |
| 214 | subyte (void *base, int byte) | |
| 215 | { | |
| 6092278a | 216 | unsigned char c = byte; |
| 6092278a | 217 | |
| c0a27981 | 218 | if (copyout(&c, base, 1) == 0) |
| 6092278a MD |
219 | return(0); |
| 220 | return(-1); | |
| 8a9d8995 MD |
221 | } |
| 222 | ||
| 223 | /* | |
| 224 | * Fetch a word (integer, 32 bits) from user space | |
| 225 | */ | |
| 226 | long | |
| 6092278a | 227 | fuword(const void *base) |
| 8a9d8995 | 228 | { |
| 6092278a | 229 | long v; |
| 6092278a | 230 | |
| c0a27981 | 231 | if (copyin(base, &v, sizeof(v)) == 0) |
| 6092278a MD |
232 | return((long)v); |
| 233 | return(-1); | |
| 8a9d8995 MD |
234 | } |
| 235 | ||
| 236 | /* | |
| 237 | * Store a word (integer, 32 bits) to user space | |
| 238 | */ | |
| 239 | int | |
| 6092278a | 240 | suword(void *base, long word) |
| 8a9d8995 | 241 | { |
| c0a27981 | 242 | if (copyout(&word, base, sizeof(word)) == 0) |
| 6092278a MD |
243 | return(0); |
| 244 | return(-1); | |
| 8a9d8995 MD |
245 | } |
| 246 | ||
| 247 | /* | |
| 248 | * Fetch an short word (16 bits) from user space | |
| 249 | */ | |
| 250 | int | |
| 6092278a | 251 | fusword(void *base) |
| 8a9d8995 | 252 | { |
| 6092278a | 253 | unsigned short sword; |
| 6092278a | 254 | |
| c0a27981 | 255 | if (copyin(base, &sword, sizeof(sword)) == 0) |
| 6092278a MD |
256 | return((int)sword); |
| 257 | return(-1); | |
| 8a9d8995 MD |
258 | } |
| 259 | ||
| 260 | /* | |
| 261 | * Store a short word (16 bits) to user space | |
| 262 | */ | |
| 263 | int | |
| 264 | susword (void *base, int word) | |
| 265 | { | |
| 6092278a | 266 | unsigned short sword = word; |
| 6092278a | 267 | |
| c0a27981 | 268 | if (copyout(&sword, base, sizeof(sword)) == 0) |
| 6092278a MD |
269 | return(0); |
| 270 | return(-1); | |
| 8a9d8995 | 271 | } |