2 * Copyright (c) 2006 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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
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>
46 * A bcopy that works dring low level boot, before FP is working
49 ovbcopy(const void *src, void *dst, size_t len)
55 bcopyi(const void *src, void *dst, size_t len)
61 copystr(const void *kfaddr, void *kdaddr, size_t len, size_t *lencopied)
65 for (i = 0; i < len; ++i) {
66 if ((((char *)kdaddr)[i] = ((const char *)kfaddr)[i]) == 0) {
72 return (ENAMETOOLONG);
76 * Copies a NUL-terminated string from user space to kernel space.
77 * The number of bytes copied, including the terminator, is returned in
80 * Returns 0 on success, EFAULT or ENAMETOOLONG on failure.
83 copyinstr(const void *udaddr, void *kaddr, size_t len, size_t *res)
87 const char *uptr = udaddr;
93 n = PAGE_SIZE - ((vm_offset_t)uptr & PAGE_MASK);
98 if ((error = copyin(uptr, kptr, n)) != 0)
112 return(ENAMETOOLONG);
116 * Copy a binary buffer from user space to kernel space.
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.
121 * Returns 0 on success, EFAULT on failure.
124 copyin(const void *udaddr, void *kaddr, size_t len)
126 struct vmspace *vm = curproc->p_vmspace;
128 struct lwbuf lwb_cache;
135 m = vm_fault_page(&vm->vm_map, trunc_page((vm_offset_t)udaddr),
137 VM_FAULT_NORMAL, &error);
140 n = PAGE_SIZE - ((vm_offset_t)udaddr & PAGE_MASK);
143 lwb = lwbuf_alloc(m, &lwb_cache);
144 bcopy((char *)lwbuf_kva(lwb)+((vm_offset_t)udaddr & PAGE_MASK),
147 udaddr = (const char *)udaddr + n;
148 kaddr = (char *)kaddr + n;
158 * Copy a binary buffer from kernel space to user space.
160 * Returns 0 on success, EFAULT on failure.
163 copyout(const void *kaddr, void *udaddr, size_t len)
165 struct vmspace *vm = curproc->p_vmspace;
167 struct lwbuf lwb_cache;
174 m = vm_fault_page(&vm->vm_map, trunc_page((vm_offset_t)udaddr),
175 VM_PROT_READ|VM_PROT_WRITE,
176 VM_FAULT_NORMAL, &error);
179 n = PAGE_SIZE - ((vm_offset_t)udaddr & PAGE_MASK);
182 lwb = lwbuf_alloc(m, &lwb_cache);
183 bcopy(kaddr, (char *)lwbuf_kva(lwb) +
184 ((vm_offset_t)udaddr & PAGE_MASK), n);
186 udaddr = (char *)udaddr + n;
187 kaddr = (const char *)kaddr + n;
198 * Fetch the byte at the specified user address. Returns -1 on failure.
201 fubyte(const void *base)
205 if (copyin(base, &c, 1) == 0)
211 * Store a byte at the specified user address. Returns -1 on failure.
214 subyte (void *base, int byte)
216 unsigned char c = byte;
218 if (copyout(&c, base, 1) == 0)
224 * Fetch a word (integer, 32 bits) from user space
227 fuword(const void *base)
231 if (copyin(base, &v, sizeof(v)) == 0)
237 * Store a word (integer, 32 bits) to user space
240 suword(void *base, long word)
242 if (copyout(&word, base, sizeof(word)) == 0)
248 suword32(void *base, int word)
250 if (copyout(&word, base, sizeof(word)) == 0)
256 * Fetch an short word (16 bits) from user space
261 unsigned short sword;
263 if (copyin(base, &sword, sizeof(sword)) == 0)
269 * Store a short word (16 bits) to user space
272 susword (void *base, int word)
274 unsigned short sword = word;
276 if (copyout(&sword, base, sizeof(sword)) == 0)