2 * Copyright (C) 2002 Benno Rice
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * Copyright (C) 1993 Wolfgang Solfrank.
27 * Copyright (C) 1993 TooLs GmbH.
28 * All rights reserved.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by TooLs GmbH.
41 * 4. The name of TooLs GmbH may not be used to endorse or promote products
42 * derived from this software without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
45 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
46 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
47 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
49 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
50 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
51 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
52 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
53 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56 #include <sys/cdefs.h>
57 __FBSDID("$FreeBSD$");
59 #include <sys/param.h>
61 #include <sys/mutex.h>
62 #include <sys/systm.h>
67 #include <vm/vm_map.h>
69 #include <machine/pcb.h>
70 #include <machine/sr.h>
71 #include <machine/slb.h>
72 #include <machine/vmparam.h>
74 int setfault(faultbuf); /* defined in locore.S */
78 * Makes sure that the right segment of userspace is mapped in.
83 set_user_sr(pmap_t pm, const void *addr)
88 /* Try lockless look-up first */
89 slb = user_va_to_slb_entry(pm, (vm_offset_t)addr);
92 /* If it isn't there, we need to pre-fault the VSID */
94 slbv = va_to_vsid(pm, (vm_offset_t)addr) << SLBV_VSID_SHIFT;
100 /* Mark segment no-execute */
103 /* If we have already set this VSID, we can just return */
104 if (curthread->td_pcb->pcb_cpu.aim.usr_vsid == slbv)
107 __asm __volatile("isync");
108 curthread->td_pcb->pcb_cpu.aim.usr_segm =
109 (uintptr_t)addr >> ADDR_SR_SHFT;
110 curthread->td_pcb->pcb_cpu.aim.usr_vsid = slbv;
111 __asm __volatile ("slbie %0; slbmte %1, %2; isync" ::
112 "r"(USER_ADDR), "r"(slbv), "r"(USER_SLB_SLBE));
116 set_user_sr(pmap_t pm, const void *addr)
120 vsid = va_to_vsid(pm, (vm_offset_t)addr);
122 /* Mark segment no-execute */
125 /* If we have already set this VSID, we can just return */
126 if (curthread->td_pcb->pcb_cpu.aim.usr_vsid == vsid)
129 __asm __volatile("isync");
130 curthread->td_pcb->pcb_cpu.aim.usr_segm =
131 (uintptr_t)addr >> ADDR_SR_SHFT;
132 curthread->td_pcb->pcb_cpu.aim.usr_vsid = vsid;
133 __asm __volatile("mtsr %0,%1; isync" :: "n"(USER_SR), "r"(vsid));
138 map_user_ptr(pmap_t pm, const void *uaddr, void **kaddr, size_t ulen,
143 *kaddr = (char *)USER_ADDR + ((uintptr_t)uaddr & ~SEGMENT_MASK);
145 l = ((char *)USER_ADDR + SEGMENT_LENGTH) - (char *)(*kaddr);
153 set_user_sr(pm, uaddr);
157 #else /* Book-E uses a combined kernel/user mapping */
159 map_user_ptr(pmap_t pm, const void *uaddr, void **kaddr, size_t ulen,
163 if ((uintptr_t)uaddr + ulen > VM_MAXUSER_ADDRESS + PAGE_SIZE)
166 *kaddr = (void *)(uintptr_t)uaddr;
175 copyout(const void *kaddr, void *udaddr, size_t len)
185 pm = &td->td_proc->p_vmspace->vm_pmap;
188 td->td_pcb->pcb_onfault = NULL;
196 if (map_user_ptr(pm, udaddr, (void **)&p, len, &l)) {
197 td->td_pcb->pcb_onfault = NULL;
208 td->td_pcb->pcb_onfault = NULL;
213 copyin(const void *udaddr, void *kaddr, size_t len)
223 pm = &td->td_proc->p_vmspace->vm_pmap;
226 td->td_pcb->pcb_onfault = NULL;
234 if (map_user_ptr(pm, udaddr, (void **)&p, len, &l)) {
235 td->td_pcb->pcb_onfault = NULL;
246 td->td_pcb->pcb_onfault = NULL;
251 copyinstr(const void *udaddr, void *kaddr, size_t len, size_t *done)
263 for (l = 0; len-- > 0; l++) {
264 if ((c = fubyte(up++)) < 0) {
284 subyte(void *addr, int byte)
292 pm = &td->td_proc->p_vmspace->vm_pmap;
295 td->td_pcb->pcb_onfault = NULL;
299 if (map_user_ptr(pm, addr, (void **)&p, sizeof(*p), NULL)) {
300 td->td_pcb->pcb_onfault = NULL;
306 td->td_pcb->pcb_onfault = NULL;
312 suword32(void *addr, int word)
320 pm = &td->td_proc->p_vmspace->vm_pmap;
323 td->td_pcb->pcb_onfault = NULL;
327 if (map_user_ptr(pm, addr, (void **)&p, sizeof(*p), NULL)) {
328 td->td_pcb->pcb_onfault = NULL;
334 td->td_pcb->pcb_onfault = NULL;
340 suword(void *addr, long word)
348 pm = &td->td_proc->p_vmspace->vm_pmap;
351 td->td_pcb->pcb_onfault = NULL;
355 if (map_user_ptr(pm, addr, (void **)&p, sizeof(*p), NULL)) {
356 td->td_pcb->pcb_onfault = NULL;
362 td->td_pcb->pcb_onfault = NULL;
368 suword64(void *addr, int64_t word)
370 return (suword(addr, (long)word));
374 suword32(void *addr, int32_t word)
376 return (suword(addr, (long)word));
381 fubyte(const void *addr)
390 pm = &td->td_proc->p_vmspace->vm_pmap;
393 td->td_pcb->pcb_onfault = NULL;
397 if (map_user_ptr(pm, addr, (void **)&p, sizeof(*p), NULL)) {
398 td->td_pcb->pcb_onfault = NULL;
404 td->td_pcb->pcb_onfault = NULL;
410 fuword32(const void *addr)
418 pm = &td->td_proc->p_vmspace->vm_pmap;
421 td->td_pcb->pcb_onfault = NULL;
425 if (map_user_ptr(pm, addr, (void **)&p, sizeof(*p), NULL)) {
426 td->td_pcb->pcb_onfault = NULL;
432 td->td_pcb->pcb_onfault = NULL;
438 fuword(const void *addr)
446 pm = &td->td_proc->p_vmspace->vm_pmap;
449 td->td_pcb->pcb_onfault = NULL;
453 if (map_user_ptr(pm, addr, (void **)&p, sizeof(*p), NULL)) {
454 td->td_pcb->pcb_onfault = NULL;
460 td->td_pcb->pcb_onfault = NULL;
464 #ifndef __powerpc64__
466 fuword32(const void *addr)
468 return ((int32_t)fuword(addr));
473 casuword32(volatile uint32_t *addr, uint32_t old, uint32_t new)
481 pm = &td->td_proc->p_vmspace->vm_pmap;
484 td->td_pcb->pcb_onfault = NULL;
488 if (map_user_ptr(pm, (void *)(uintptr_t)addr, (void **)&p, sizeof(*p),
490 td->td_pcb->pcb_onfault = NULL;
495 "1:\tlwarx %0, 0, %2\n\t" /* load old value */
496 "cmplw %3, %0\n\t" /* compare */
497 "bne 2f\n\t" /* exit if not equal */
498 "stwcx. %4, 0, %2\n\t" /* attempt to store */
499 "bne- 1b\n\t" /* spin if failed */
500 "b 3f\n\t" /* we've succeeded */
502 "stwcx. %0, 0, %2\n\t" /* clear reservation (74xx) */
504 : "=&r" (val), "=m" (*p)
505 : "r" (p), "r" (old), "r" (new), "m" (*p)
508 td->td_pcb->pcb_onfault = NULL;
513 #ifndef __powerpc64__
515 casuword(volatile u_long *addr, u_long old, u_long new)
517 return (casuword32((volatile uint32_t *)addr, old, new));
521 casuword(volatile u_long *addr, u_long old, u_long new)
529 pm = &td->td_proc->p_vmspace->vm_pmap;
532 td->td_pcb->pcb_onfault = NULL;
536 if (map_user_ptr(pm, (void *)(uintptr_t)addr, (void **)&p, sizeof(*p),
538 td->td_pcb->pcb_onfault = NULL;
543 "1:\tldarx %0, 0, %2\n\t" /* load old value */
544 "cmpld %3, %0\n\t" /* compare */
545 "bne 2f\n\t" /* exit if not equal */
546 "stdcx. %4, 0, %2\n\t" /* attempt to store */
547 "bne- 1b\n\t" /* spin if failed */
548 "b 3f\n\t" /* we've succeeded */
550 "stdcx. %0, 0, %2\n\t" /* clear reservation (74xx) */
552 : "=&r" (val), "=m" (*p)
553 : "r" (p), "r" (old), "r" (new), "m" (*p)
556 td->td_pcb->pcb_onfault = NULL;