]> CyberLeo.Net >> Repos - FreeBSD/releng/8.2.git/blob - sys/powerpc/aim/copyinout.c
Copy stable/8 to releng/8.2 in preparation for FreeBSD-8.2 release.
[FreeBSD/releng/8.2.git] / sys / powerpc / aim / copyinout.c
1 /*-
2  * Copyright (C) 2002 Benno Rice
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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.
24 */
25 /*-
26  * Copyright (C) 1993 Wolfgang Solfrank.
27  * Copyright (C) 1993 TooLs GmbH.
28  * All rights reserved.
29  *
30  * Redistribution and use in source and binary forms, with or without
31  * modification, are permitted provided that the following conditions
32  * are met:
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.
43  *
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.
54  */
55
56 #include <sys/cdefs.h>
57 __FBSDID("$FreeBSD$");
58
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/proc.h>
62
63 #include <vm/vm.h>
64 #include <vm/pmap.h>
65 #include <vm/vm_map.h>
66
67 #include <machine/pcb.h>
68 #include <machine/sr.h>
69
70 int     setfault(faultbuf);     /* defined in locore.S */
71
72 /*
73  * Makes sure that the right segment of userspace is mapped in.
74  */
75 static __inline void
76 set_user_sr(register_t vsid)
77 {
78
79         /* Mark segment no-execute */
80         vsid |= SR_N;
81
82         isync();
83         __asm __volatile ("mtsr %0,%1" :: "n"(USER_SR), "r"(vsid));
84         isync();
85 }
86
87 int
88 copyout(const void *kaddr, void *udaddr, size_t len)
89 {
90         struct          thread *td;
91         pmap_t          pm;
92         faultbuf        env;
93         const char      *kp;
94         char            *up, *p;
95         size_t          l;
96
97         td = PCPU_GET(curthread);
98         pm = &td->td_proc->p_vmspace->vm_pmap;
99
100         if (setfault(env)) {
101                 td->td_pcb->pcb_onfault = NULL;
102                 return (EFAULT);
103         }
104
105         kp = kaddr;
106         up = udaddr;
107
108         while (len > 0) {
109                 p = (char *)USER_ADDR + ((u_int)up & ~SEGMENT_MASK);
110
111                 l = ((char *)USER_ADDR + SEGMENT_LENGTH) - p;
112                 if (l > len)
113                         l = len;
114
115                 set_user_sr(pm->pm_sr[(u_int)up >> ADDR_SR_SHFT]);
116
117                 bcopy(kp, p, l);
118
119                 up += l;
120                 kp += l;
121                 len -= l;
122         }
123
124         td->td_pcb->pcb_onfault = NULL;
125         return (0);
126 }
127
128 int
129 copyin(const void *udaddr, void *kaddr, size_t len)
130 {
131         struct          thread *td;
132         pmap_t          pm;
133         faultbuf        env;
134         const char      *up;
135         char            *kp, *p;
136         size_t          l;
137
138         td = PCPU_GET(curthread);
139         pm = &td->td_proc->p_vmspace->vm_pmap;
140
141         if (setfault(env)) {
142                 td->td_pcb->pcb_onfault = NULL;
143                 return (EFAULT);
144         }
145
146         kp = kaddr;
147         up = udaddr;
148
149         while (len > 0) {
150                 p = (char *)USER_ADDR + ((u_int)up & ~SEGMENT_MASK);
151
152                 l = ((char *)USER_ADDR + SEGMENT_LENGTH) - p;
153                 if (l > len)
154                         l = len;
155
156                 set_user_sr(pm->pm_sr[(u_int)up >> ADDR_SR_SHFT]);
157
158                 bcopy(p, kp, l);
159
160                 up += l;
161                 kp += l;
162                 len -= l;
163         }
164
165         td->td_pcb->pcb_onfault = NULL;
166         return (0);
167 }
168
169 int
170 copyinstr(const void *udaddr, void *kaddr, size_t len, size_t *done)
171 {
172         struct          thread *td;
173         pmap_t          pm;
174         faultbuf        env;
175         const char      *up;
176         char            *kp;
177         size_t          l;
178         int             rv, c;
179
180         td = PCPU_GET(curthread);
181         pm = &td->td_proc->p_vmspace->vm_pmap;
182
183         if (setfault(env)) {
184                 td->td_pcb->pcb_onfault = NULL;
185                 return (EFAULT);
186         }
187
188         kp = kaddr;
189         up = udaddr;
190
191         rv = ENAMETOOLONG;
192
193         for (l = 0; len-- > 0; l++) {
194                 if ((c = fubyte(up++)) < 0) {
195                         rv = EFAULT;
196                         break;
197                 }
198
199                 if (!(*kp++ = c)) {
200                         l++;
201                         rv = 0;
202                         break;
203                 }
204         }
205
206         if (done != NULL) {
207                 *done = l;
208         }
209
210         td->td_pcb->pcb_onfault = NULL;
211         return (rv);
212 }
213
214 int
215 subyte(void *addr, int byte)
216 {
217         struct          thread *td;
218         pmap_t          pm;
219         faultbuf        env;
220         char            *p;
221
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));
225
226         if (setfault(env)) {
227                 td->td_pcb->pcb_onfault = NULL;
228                 return (-1);
229         }
230
231         set_user_sr(pm->pm_sr[(u_int)addr >> ADDR_SR_SHFT]);
232
233         *p = (char)byte;
234
235         td->td_pcb->pcb_onfault = NULL;
236         return (0);
237 }
238
239 int
240 suword(void *addr, long word)
241 {
242         struct          thread *td;
243         pmap_t          pm;
244         faultbuf        env;
245         long            *p;
246
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));
250
251         if (setfault(env)) {
252                 td->td_pcb->pcb_onfault = NULL;
253                 return (-1);
254         }
255
256         set_user_sr(pm->pm_sr[(u_int)addr >> ADDR_SR_SHFT]);
257
258         *p = word;
259
260         td->td_pcb->pcb_onfault = NULL;
261         return (0);
262 }
263
264 int
265 suword32(void *addr, int32_t word)
266 {
267         return (suword(addr, (long)word));
268 }
269
270
271 int
272 fubyte(const void *addr)
273 {
274         struct          thread *td;
275         pmap_t          pm;
276         faultbuf        env;
277         u_char          *p;
278         int             val;
279
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));
283
284         if (setfault(env)) {
285                 td->td_pcb->pcb_onfault = NULL;
286                 return (-1);
287         }
288
289         set_user_sr(pm->pm_sr[(u_int)addr >> ADDR_SR_SHFT]);
290
291         val = *p;
292
293         td->td_pcb->pcb_onfault = NULL;
294         return (val);
295 }
296
297 long
298 fuword(const void *addr)
299 {
300         struct          thread *td;
301         pmap_t          pm;
302         faultbuf        env;
303         long            *p, val;
304
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));
308
309         if (setfault(env)) {
310                 td->td_pcb->pcb_onfault = NULL;
311                 return (-1);
312         }
313
314         set_user_sr(pm->pm_sr[(u_int)addr >> ADDR_SR_SHFT]);
315
316         val = *p;
317
318         td->td_pcb->pcb_onfault = NULL;
319         return (val);
320 }
321
322 int32_t
323 fuword32(const void *addr)
324 {
325         return ((int32_t)fuword(addr));
326 }
327
328 uint32_t
329 casuword32(volatile uint32_t *base, uint32_t oldval, uint32_t newval)
330 {
331         return (casuword((volatile u_long *)base, oldval, newval));
332 }
333
334 u_long
335 casuword(volatile u_long *addr, u_long old, u_long new)
336 {
337         struct thread *td;
338         pmap_t pm;
339         faultbuf env;
340         u_long *p, val;
341
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));
345
346         set_user_sr(pm->pm_sr[(u_int)addr >> ADDR_SR_SHFT]);
347
348         if (setfault(env)) {
349                 td->td_pcb->pcb_onfault = NULL;
350                 return (-1);
351         }
352
353         __asm __volatile (
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 */
360                 "2:\n\t"
361                 "stwcx. %0, 0, %2\n\t"          /* clear reservation (74xx) */
362                 "3:\n\t"
363                 : "=&r" (val), "=m" (*p)
364                 : "r" (p), "r" (old), "r" (new), "m" (*p)
365                 : "cc", "memory");
366
367         td->td_pcb->pcb_onfault = NULL;
368
369         return (val);
370 }