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>
73 int setfault(faultbuf); /* defined in locore.S */
76 * Makes sure that the right segment of userspace is mapped in.
81 set_user_sr(pmap_t pm, const void *addr)
86 /* Try lockless look-up first */
87 slb = user_va_to_slb_entry(pm, (vm_offset_t)addr);
90 /* If it isn't there, we need to pre-fault the VSID */
92 slbv = va_to_vsid(pm, (vm_offset_t)addr) << SLBV_VSID_SHIFT;
98 /* Mark segment no-execute */
101 /* If we have already set this VSID, we can just return */
102 if (curthread->td_pcb->pcb_cpu.aim.usr_vsid == slbv)
105 __asm __volatile("isync");
106 curthread->td_pcb->pcb_cpu.aim.usr_segm =
107 (uintptr_t)addr >> ADDR_SR_SHFT;
108 curthread->td_pcb->pcb_cpu.aim.usr_vsid = slbv;
109 __asm __volatile ("slbie %0; slbmte %1, %2; isync" ::
110 "r"(USER_ADDR), "r"(slbv), "r"(USER_SLB_SLBE));
114 set_user_sr(pmap_t pm, const void *addr)
118 vsid = va_to_vsid(pm, (vm_offset_t)addr);
120 /* Mark segment no-execute */
123 /* If we have already set this VSID, we can just return */
124 if (curthread->td_pcb->pcb_cpu.aim.usr_vsid == vsid)
127 __asm __volatile("isync");
128 curthread->td_pcb->pcb_cpu.aim.usr_segm =
129 (uintptr_t)addr >> ADDR_SR_SHFT;
130 curthread->td_pcb->pcb_cpu.aim.usr_vsid = vsid;
131 __asm __volatile("mtsr %0,%1; isync" :: "n"(USER_SR), "r"(vsid));
136 copyout(const void *kaddr, void *udaddr, size_t len)
146 pm = &td->td_proc->p_vmspace->vm_pmap;
149 td->td_pcb->pcb_onfault = NULL;
157 p = (char *)USER_ADDR + ((uintptr_t)up & ~SEGMENT_MASK);
159 l = ((char *)USER_ADDR + SEGMENT_LENGTH) - p;
172 td->td_pcb->pcb_onfault = NULL;
177 copyin(const void *udaddr, void *kaddr, size_t len)
187 pm = &td->td_proc->p_vmspace->vm_pmap;
190 td->td_pcb->pcb_onfault = NULL;
198 p = (char *)USER_ADDR + ((uintptr_t)up & ~SEGMENT_MASK);
200 l = ((char *)USER_ADDR + SEGMENT_LENGTH) - p;
213 td->td_pcb->pcb_onfault = NULL;
218 copyinstr(const void *udaddr, void *kaddr, size_t len, size_t *done)
229 pm = &td->td_proc->p_vmspace->vm_pmap;
232 td->td_pcb->pcb_onfault = NULL;
241 for (l = 0; len-- > 0; l++) {
242 if ((c = fubyte(up++)) < 0) {
258 td->td_pcb->pcb_onfault = NULL;
263 subyte(void *addr, int byte)
271 pm = &td->td_proc->p_vmspace->vm_pmap;
272 p = (char *)(USER_ADDR + ((uintptr_t)addr & ~SEGMENT_MASK));
275 td->td_pcb->pcb_onfault = NULL;
279 set_user_sr(pm,addr);
283 td->td_pcb->pcb_onfault = NULL;
289 suword32(void *addr, int word)
297 pm = &td->td_proc->p_vmspace->vm_pmap;
298 p = (int *)(USER_ADDR + ((uintptr_t)addr & ~SEGMENT_MASK));
301 td->td_pcb->pcb_onfault = NULL;
305 set_user_sr(pm,addr);
309 td->td_pcb->pcb_onfault = NULL;
315 suword(void *addr, long word)
323 pm = &td->td_proc->p_vmspace->vm_pmap;
324 p = (long *)(USER_ADDR + ((uintptr_t)addr & ~SEGMENT_MASK));
327 td->td_pcb->pcb_onfault = NULL;
331 set_user_sr(pm,addr);
335 td->td_pcb->pcb_onfault = NULL;
341 suword64(void *addr, int64_t word)
343 return (suword(addr, (long)word));
347 suword32(void *addr, int32_t word)
349 return (suword(addr, (long)word));
354 fubyte(const void *addr)
363 pm = &td->td_proc->p_vmspace->vm_pmap;
364 p = (u_char *)(USER_ADDR + ((uintptr_t)addr & ~SEGMENT_MASK));
367 td->td_pcb->pcb_onfault = NULL;
371 set_user_sr(pm,addr);
375 td->td_pcb->pcb_onfault = NULL;
381 fuword32(const void *addr)
389 pm = &td->td_proc->p_vmspace->vm_pmap;
390 p = (int32_t *)(USER_ADDR + ((uintptr_t)addr & ~SEGMENT_MASK));
393 td->td_pcb->pcb_onfault = NULL;
397 set_user_sr(pm,addr);
401 td->td_pcb->pcb_onfault = NULL;
407 fuword(const void *addr)
415 pm = &td->td_proc->p_vmspace->vm_pmap;
416 p = (long *)(USER_ADDR + ((uintptr_t)addr & ~SEGMENT_MASK));
419 td->td_pcb->pcb_onfault = NULL;
423 set_user_sr(pm,addr);
427 td->td_pcb->pcb_onfault = NULL;
431 #ifndef __powerpc64__
433 fuword32(const void *addr)
435 return ((int32_t)fuword(addr));
440 casuword32(volatile uint32_t *addr, uint32_t old, uint32_t new)
448 pm = &td->td_proc->p_vmspace->vm_pmap;
449 p = (uint32_t *)(USER_ADDR + ((uintptr_t)addr & ~SEGMENT_MASK));
451 set_user_sr(pm,(const void *)(vm_offset_t)addr);
454 td->td_pcb->pcb_onfault = NULL;
459 "1:\tlwarx %0, 0, %2\n\t" /* load old value */
460 "cmplw %3, %0\n\t" /* compare */
461 "bne 2f\n\t" /* exit if not equal */
462 "stwcx. %4, 0, %2\n\t" /* attempt to store */
463 "bne- 1b\n\t" /* spin if failed */
464 "b 3f\n\t" /* we've succeeded */
466 "stwcx. %0, 0, %2\n\t" /* clear reservation (74xx) */
468 : "=&r" (val), "=m" (*p)
469 : "r" (p), "r" (old), "r" (new), "m" (*p)
472 td->td_pcb->pcb_onfault = NULL;
477 #ifndef __powerpc64__
479 casuword(volatile u_long *addr, u_long old, u_long new)
481 return (casuword32((volatile uint32_t *)addr, old, new));
485 casuword(volatile u_long *addr, u_long old, u_long new)
493 pm = &td->td_proc->p_vmspace->vm_pmap;
494 p = (u_long *)(USER_ADDR + ((uintptr_t)addr & ~SEGMENT_MASK));
496 set_user_sr(pm,(const void *)(vm_offset_t)addr);
499 td->td_pcb->pcb_onfault = NULL;
504 "1:\tldarx %0, 0, %2\n\t" /* load old value */
505 "cmpld %3, %0\n\t" /* compare */
506 "bne 2f\n\t" /* exit if not equal */
507 "stdcx. %4, 0, %2\n\t" /* attempt to store */
508 "bne- 1b\n\t" /* spin if failed */
509 "b 3f\n\t" /* we've succeeded */
511 "stdcx. %0, 0, %2\n\t" /* clear reservation (74xx) */
513 : "=&r" (val), "=m" (*p)
514 : "r" (p), "r" (old), "r" (new), "m" (*p)
517 td->td_pcb->pcb_onfault = NULL;