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>
60 #include <sys/systm.h>
65 #include <vm/vm_map.h>
67 #include <machine/pcb.h>
68 #include <machine/sr.h>
70 int setfault(faultbuf); /* defined in locore.S */
73 * Makes sure that the right segment of userspace is mapped in.
76 set_user_sr(register_t vsid)
79 /* Mark segment no-execute */
83 __asm __volatile ("mtsr %0,%1" :: "n"(USER_SR), "r"(vsid));
88 copyout(const void *kaddr, void *udaddr, size_t len)
97 td = PCPU_GET(curthread);
98 pm = &td->td_proc->p_vmspace->vm_pmap;
101 td->td_pcb->pcb_onfault = NULL;
109 p = (char *)USER_ADDR + ((u_int)up & ~SEGMENT_MASK);
111 l = ((char *)USER_ADDR + SEGMENT_LENGTH) - p;
115 set_user_sr(pm->pm_sr[(u_int)up >> ADDR_SR_SHFT]);
124 td->td_pcb->pcb_onfault = NULL;
129 copyin(const void *udaddr, void *kaddr, size_t len)
138 td = PCPU_GET(curthread);
139 pm = &td->td_proc->p_vmspace->vm_pmap;
142 td->td_pcb->pcb_onfault = NULL;
150 p = (char *)USER_ADDR + ((u_int)up & ~SEGMENT_MASK);
152 l = ((char *)USER_ADDR + SEGMENT_LENGTH) - p;
156 set_user_sr(pm->pm_sr[(u_int)up >> ADDR_SR_SHFT]);
165 td->td_pcb->pcb_onfault = NULL;
170 copyinstr(const void *udaddr, void *kaddr, size_t len, size_t *done)
180 td = PCPU_GET(curthread);
181 pm = &td->td_proc->p_vmspace->vm_pmap;
184 td->td_pcb->pcb_onfault = NULL;
193 for (l = 0; len-- > 0; l++) {
194 if ((c = fubyte(up++)) < 0) {
210 td->td_pcb->pcb_onfault = NULL;
215 subyte(void *addr, int byte)
222 td = PCPU_GET(curthread);
223 pm = &td->td_proc->p_vmspace->vm_pmap;
224 p = (char *)((u_int)USER_ADDR + ((u_int)addr & ~SEGMENT_MASK));
227 td->td_pcb->pcb_onfault = NULL;
231 set_user_sr(pm->pm_sr[(u_int)addr >> ADDR_SR_SHFT]);
235 td->td_pcb->pcb_onfault = NULL;
240 suword(void *addr, long word)
247 td = PCPU_GET(curthread);
248 pm = &td->td_proc->p_vmspace->vm_pmap;
249 p = (long *)((u_int)USER_ADDR + ((u_int)addr & ~SEGMENT_MASK));
252 td->td_pcb->pcb_onfault = NULL;
256 set_user_sr(pm->pm_sr[(u_int)addr >> ADDR_SR_SHFT]);
260 td->td_pcb->pcb_onfault = NULL;
265 suword32(void *addr, int32_t word)
267 return (suword(addr, (long)word));
272 fubyte(const void *addr)
280 td = PCPU_GET(curthread);
281 pm = &td->td_proc->p_vmspace->vm_pmap;
282 p = (u_char *)((u_int)USER_ADDR + ((u_int)addr & ~SEGMENT_MASK));
285 td->td_pcb->pcb_onfault = NULL;
289 set_user_sr(pm->pm_sr[(u_int)addr >> ADDR_SR_SHFT]);
293 td->td_pcb->pcb_onfault = NULL;
298 fuword(const void *addr)
305 td = PCPU_GET(curthread);
306 pm = &td->td_proc->p_vmspace->vm_pmap;
307 p = (long *)((u_int)USER_ADDR + ((u_int)addr & ~SEGMENT_MASK));
310 td->td_pcb->pcb_onfault = NULL;
314 set_user_sr(pm->pm_sr[(u_int)addr >> ADDR_SR_SHFT]);
318 td->td_pcb->pcb_onfault = NULL;
323 fuword32(const void *addr)
325 return ((int32_t)fuword(addr));
329 casuword32(volatile uint32_t *base, uint32_t oldval, uint32_t newval)
331 return (casuword((volatile u_long *)base, oldval, newval));
335 casuword(volatile u_long *addr, u_long old, u_long new)
342 td = PCPU_GET(curthread);
343 pm = &td->td_proc->p_vmspace->vm_pmap;
344 p = (u_long *)((u_int)USER_ADDR + ((u_int)addr & ~SEGMENT_MASK));
346 set_user_sr(pm->pm_sr[(u_int)addr >> ADDR_SR_SHFT]);
349 td->td_pcb->pcb_onfault = NULL;
354 "1:\tlwarx %0, 0, %2\n\t" /* load old value */
355 "cmplw %3, %0\n\t" /* compare */
356 "bne 2f\n\t" /* exit if not equal */
357 "stwcx. %4, 0, %2\n\t" /* attempt to store */
358 "bne- 1b\n\t" /* spin if failed */
359 "b 3f\n\t" /* we've succeeded */
361 "stwcx. %0, 0, %2\n\t" /* clear reservation (74xx) */
363 : "=&r" (val), "=m" (*p)
364 : "r" (p), "r" (old), "r" (new), "m" (*p)
367 td->td_pcb->pcb_onfault = NULL;