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 casu64(volatile uint64_t *p
, uint64_t oldval
, uint64_t newval
)
48 struct vmspace
*vm
= curproc
->p_vmspace
;
51 volatile uint64_t *dest
;
56 /* XXX No idea how to handle this case in a simple way, just abort */
57 if (PAGE_SIZE
- ((vm_offset_t
)p
& PAGE_MASK
) < sizeof(uint64_t))
60 m
= vm_fault_page(&vm
->vm_map
, trunc_page((vm_offset_t
)p
),
61 VM_PROT_READ
|VM_PROT_WRITE
,
67 kva
= PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m
));
68 dest
= (uint64_t *)(kva
+ ((vm_offset_t
)p
& PAGE_MASK
));
70 __asm
__volatile(MPLOCKED
"cmpxchgq %2,%1; " \
71 : "+a" (res
), "=m" (*dest
) \
72 : "r" (newval
), "m" (*dest
) \
84 casu32(volatile u_int
*p
, u_int oldval
, u_int newval
)
86 struct vmspace
*vm
= curproc
->p_vmspace
;
94 /* XXX No idea how to handle this case in a simple way, just abort */
95 if (PAGE_SIZE
- ((vm_offset_t
)p
& PAGE_MASK
) < sizeof(u_int
))
98 m
= vm_fault_page(&vm
->vm_map
, trunc_page((vm_offset_t
)p
),
99 VM_PROT_READ
|VM_PROT_WRITE
,
105 kva
= PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m
));
106 dest
= (u_int
*)(kva
+ ((vm_offset_t
)p
& PAGE_MASK
));
108 __asm
__volatile(MPLOCKED
"cmpxchgl %2,%1; " \
109 : "+a" (res
), "=m" (*dest
) \
110 : "r" (newval
), "m" (*dest
) \
122 swapu64(volatile uint64_t *p
, uint64_t val
)
124 struct vmspace
*vm
= curproc
->p_vmspace
;
131 /* XXX No idea how to handle this case in a simple way, just abort */
132 if (PAGE_SIZE
- ((vm_offset_t
)p
& PAGE_MASK
) < sizeof(uint64_t))
135 m
= vm_fault_page(&vm
->vm_map
, trunc_page((vm_offset_t
)p
),
136 VM_PROT_READ
|VM_PROT_WRITE
,
142 kva
= PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m
));
143 res
= atomic_swap_long((uint64_t *)(kva
+ ((vm_offset_t
)p
& PAGE_MASK
)),
154 swapu32(volatile uint32_t *p
, uint32_t val
)
156 struct vmspace
*vm
= curproc
->p_vmspace
;
163 /* XXX No idea how to handle this case in a simple way, just abort */
164 if (PAGE_SIZE
- ((vm_offset_t
)p
& PAGE_MASK
) < sizeof(uint64_t))
167 m
= vm_fault_page(&vm
->vm_map
, trunc_page((vm_offset_t
)p
),
168 VM_PROT_READ
|VM_PROT_WRITE
,
174 kva
= PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m
));
175 res
= atomic_swap_int((u_int
*)(kva
+ ((vm_offset_t
)p
& PAGE_MASK
)),
186 copystr(const void *kfaddr
, void *kdaddr
, size_t len
, size_t *lencopied
)
190 for (i
= 0; i
< len
; ++i
) {
191 if ((((char *)kdaddr
)[i
] = ((const char *)kfaddr
)[i
]) == 0) {
197 return (ENAMETOOLONG
);
201 * Copies a NUL-terminated string from user space to kernel space.
202 * The number of bytes copied, including the terminator, is returned in
205 * Returns 0 on success, EFAULT or ENAMETOOLONG on failure.
208 copyinstr(const void *udaddr
, void *kaddr
, size_t len
, size_t *res
)
212 const char *uptr
= udaddr
;
218 n
= PAGE_SIZE
- ((vm_offset_t
)uptr
& PAGE_MASK
);
223 if ((error
= copyin(uptr
, kptr
, n
)) != 0)
237 return(ENAMETOOLONG
);
241 * Copy a binary buffer from user space to kernel space.
243 * Returns 0 on success, EFAULT on failure.
246 copyin(const void *udaddr
, void *kaddr
, size_t len
)
248 struct vmspace
*vm
= curproc
->p_vmspace
;
250 struct lwbuf lwb_cache
;
257 m
= vm_fault_page(&vm
->vm_map
, trunc_page((vm_offset_t
)udaddr
),
263 n
= PAGE_SIZE
- ((vm_offset_t
)udaddr
& PAGE_MASK
);
266 lwb
= lwbuf_alloc(m
, &lwb_cache
);
267 bcopy((char *)lwbuf_kva(lwb
)+((vm_offset_t
)udaddr
& PAGE_MASK
),
270 udaddr
= (const char *)udaddr
+ n
;
271 kaddr
= (char *)kaddr
+ n
;
281 * Copy a binary buffer from kernel space to user space.
283 * Returns 0 on success, EFAULT on failure.
286 copyout(const void *kaddr
, void *udaddr
, size_t len
)
288 struct vmspace
*vm
= curproc
->p_vmspace
;
290 struct lwbuf lwb_cache
;
298 m
= vm_fault_page(&vm
->vm_map
, trunc_page((vm_offset_t
)udaddr
),
299 VM_PROT_READ
|VM_PROT_WRITE
,
304 n
= PAGE_SIZE
- ((vm_offset_t
)udaddr
& PAGE_MASK
);
307 lwb
= lwbuf_alloc(m
, &lwb_cache
);
308 bcopy(kaddr
, (char *)lwbuf_kva(lwb
) +
309 ((vm_offset_t
)udaddr
& PAGE_MASK
), n
);
311 udaddr
= (char *)udaddr
+ n
;
312 kaddr
= (const char *)kaddr
+ n
;
325 * Fetch the byte at the specified user address. Returns -1 on failure.
328 fubyte(const uint8_t *base
)
332 if (copyin(base
, &c
, 1) == 0)
338 * Store a byte at the specified user address. Returns -1 on failure.
341 subyte(uint8_t *base
, uint8_t byte
)
345 if (copyout(&c
, base
, 1) == 0)
351 * Fetch a word (integer, 32 bits) from user space
354 fuword32(const uint32_t *base
)
358 if (copyin(base
, &v
, sizeof(v
)) == 0)
364 * Fetch a word (integer, 32 bits) from user space
367 fuword64(const uint64_t *base
)
371 if (copyin(base
, &v
, sizeof(v
)) == 0)
377 * Store a word (integer, 32 bits) to user space
380 suword64(uint64_t *base
, uint64_t word
)
382 if (copyout(&word
, base
, sizeof(word
)) == 0)
388 suword32(uint32_t *base
, int word
)
390 if (copyout(&word
, base
, sizeof(word
)) == 0)