vserver 1.9.3
[linux-2.6.git] / include / asm-ppc / uaccess.h
1 #ifdef __KERNEL__
2 #ifndef _PPC_UACCESS_H
3 #define _PPC_UACCESS_H
4
5 #ifndef __ASSEMBLY__
6 #include <linux/sched.h>
7 #include <linux/errno.h>
8 #include <asm/processor.h>
9
10 #define VERIFY_READ     0
11 #define VERIFY_WRITE    1
12
13 /*
14  * The fs value determines whether argument validity checking should be
15  * performed or not.  If get_fs() == USER_DS, checking is performed, with
16  * get_fs() == KERNEL_DS, checking is bypassed.
17  *
18  * For historical reasons, these macros are grossly misnamed.
19  *
20  * The fs/ds values are now the highest legal address in the "segment".
21  * This simplifies the checking in the routines below.
22  */
23
24 #define KERNEL_DS       ((mm_segment_t) { ~0UL })
25 #define USER_DS         ((mm_segment_t) { TASK_SIZE - 1 })
26
27 #define get_ds()        (KERNEL_DS)
28 #define get_fs()        (current->thread.fs)
29 #define set_fs(val)     (current->thread.fs = (val))
30
31 #define segment_eq(a,b) ((a).seg == (b).seg)
32
33 #define __access_ok(addr,size)                                              \
34         ((addr) <= current->thread.fs.seg                                   \
35          && ((size) == 0 || (size) - 1 <= current->thread.fs.seg - (addr)))
36
37 #define access_ok(type, addr, size) \
38         (__chk_user_ptr(addr),__access_ok((unsigned long)(addr),(size)))
39
40 extern inline int verify_area(int type, const void __user * addr, unsigned long size)
41 {
42         return access_ok(type, addr, size) ? 0 : -EFAULT;
43 }
44
45
46 /*
47  * The exception table consists of pairs of addresses: the first is the
48  * address of an instruction that is allowed to fault, and the second is
49  * the address at which the program should continue.  No registers are
50  * modified, so it is entirely up to the continuation code to figure out
51  * what to do.
52  *
53  * All the routines below use bits of fixup code that are out of line
54  * with the main instruction path.  This means when everything is well,
55  * we don't even have to jump over them.  Further, they do not intrude
56  * on our cache or tlb entries.
57  */
58
59 struct exception_table_entry
60 {
61         unsigned long insn, fixup;
62 };
63
64 /*
65  * These are the main single-value transfer routines.  They automatically
66  * use the right size if we just have the right pointer type.
67  *
68  * This gets kind of ugly. We want to return _two_ values in "get_user()"
69  * and yet we don't want to do any pointers, because that is too much
70  * of a performance impact. Thus we have a few rather ugly macros here,
71  * and hide all the ugliness from the user.
72  *
73  * The "__xxx" versions of the user access functions are versions that
74  * do not verify the address space, that must have been done previously
75  * with a separate "access_ok()" call (this is used when we do multiple
76  * accesses to the same area of user memory).
77  *
78  * As we use the same address space for kernel and user data on the
79  * PowerPC, we can just do these as direct assignments.  (Of course, the
80  * exception handling means that it's no longer "just"...)
81  *
82  * The "user64" versions of the user access functions are versions that 
83  * allow access of 64-bit data. The "get_user" functions do not 
84  * properly handle 64-bit data because the value gets down cast to a long. 
85  * The "put_user" functions already handle 64-bit data properly but we add 
86  * "user64" versions for completeness
87  */
88 #define get_user(x,ptr) \
89   __get_user_check((x),(ptr),sizeof(*(ptr)))
90 #define get_user64(x,ptr) \
91   __get_user64_check((x),(ptr),sizeof(*(ptr)))
92 #define put_user(x,ptr) \
93   __put_user_check((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
94 #define put_user64(x,ptr) put_user(x,ptr)
95
96 #define __get_user(x,ptr) \
97   __get_user_nocheck((x),(ptr),sizeof(*(ptr)))
98 #define __get_user64(x,ptr) \
99   __get_user64_nocheck((x),(ptr),sizeof(*(ptr)))
100 #define __put_user(x,ptr) \
101   __put_user_nocheck((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
102 #define __put_user64(x,ptr) __put_user(x,ptr)
103
104 extern long __put_user_bad(void);
105
106 #define __put_user_nocheck(x,ptr,size)                  \
107 ({                                                      \
108         long __pu_err;                                  \
109         __chk_user_ptr(ptr);                            \
110         __put_user_size((x),(ptr),(size),__pu_err);     \
111         __pu_err;                                       \
112 })
113
114 #define __put_user_check(x,ptr,size)                            \
115 ({                                                              \
116         long __pu_err = -EFAULT;                                \
117         __typeof__(*(ptr)) __user *__pu_addr = (ptr);           \
118         if (access_ok(VERIFY_WRITE,__pu_addr,size))             \
119                 __put_user_size((x),__pu_addr,(size),__pu_err); \
120         __pu_err;                                               \
121 })
122
123 #define __put_user_size(x,ptr,size,retval)              \
124 do {                                                    \
125         retval = 0;                                     \
126         switch (size) {                                 \
127         case 1:                                         \
128                 __put_user_asm(x, ptr, retval, "stb");  \
129                 break;                                  \
130         case 2:                                         \
131                 __put_user_asm(x, ptr, retval, "sth");  \
132                 break;                                  \
133         case 4:                                         \
134                 __put_user_asm(x, ptr, retval, "stw");  \
135                 break;                                  \
136         case 8:                                         \
137                 __put_user_asm2(x, ptr, retval);        \
138                 break;                                  \
139         default:                                        \
140                 __put_user_bad();                       \
141         }                                               \
142 } while (0)
143
144 /*
145  * We don't tell gcc that we are accessing memory, but this is OK
146  * because we do not write to any memory gcc knows about, so there
147  * are no aliasing issues.
148  */
149 #define __put_user_asm(x, addr, err, op)                        \
150         __asm__ __volatile__(                                   \
151                 "1:     "op" %1,0(%2)\n"                        \
152                 "2:\n"                                          \
153                 ".section .fixup,\"ax\"\n"                      \
154                 "3:     li %0,%3\n"                             \
155                 "       b 2b\n"                                 \
156                 ".previous\n"                                   \
157                 ".section __ex_table,\"a\"\n"                   \
158                 "       .align 2\n"                             \
159                 "       .long 1b,3b\n"                          \
160                 ".previous"                                     \
161                 : "=r" (err)                                    \
162                 : "r" (x), "b" (addr), "i" (-EFAULT), "0" (err))
163
164 #define __put_user_asm2(x, addr, err)                           \
165         __asm__ __volatile__(                                   \
166                 "1:     stw %1,0(%2)\n"                         \
167                 "2:     stw %1+1,4(%2)\n"                       \
168                 "3:\n"                                          \
169                 ".section .fixup,\"ax\"\n"                      \
170                 "4:     li %0,%3\n"                             \
171                 "       b 3b\n"                                 \
172                 ".previous\n"                                   \
173                 ".section __ex_table,\"a\"\n"                   \
174                 "       .align 2\n"                             \
175                 "       .long 1b,4b\n"                          \
176                 "       .long 2b,4b\n"                          \
177                 ".previous"                                     \
178                 : "=r" (err)                                    \
179                 : "r" (x), "b" (addr), "i" (-EFAULT), "0" (err))
180
181 #define __get_user_nocheck(x, ptr, size)                        \
182 ({                                                              \
183         long __gu_err, __gu_val;                                \
184         __chk_user_ptr(ptr);                                    \
185         __get_user_size(__gu_val, (ptr), (size), __gu_err);     \
186         (x) = (__typeof__(*(ptr)))__gu_val;                     \
187         __gu_err;                                               \
188 })
189
190 #define __get_user64_nocheck(x, ptr, size)                      \
191 ({                                                              \
192         long __gu_err;                                          \
193         long long __gu_val;                                     \
194         __chk_user_ptr(ptr);                                    \
195         __get_user_size64(__gu_val, (ptr), (size), __gu_err);   \
196         (x) = (__typeof__(*(ptr)))__gu_val;                     \
197         __gu_err;                                               \
198 })
199
200 #define __get_user_check(x, ptr, size)                                  \
201 ({                                                                      \
202         long __gu_err = -EFAULT, __gu_val = 0;                          \
203         const __typeof__(*(ptr)) __user *__gu_addr = (ptr);             \
204         if (access_ok(VERIFY_READ, __gu_addr, (size)))                  \
205                 __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
206         (x) = (__typeof__(*(ptr)))__gu_val;                             \
207         __gu_err;                                                       \
208 })
209
210 #define __get_user64_check(x, ptr, size)                                  \
211 ({                                                                        \
212         long __gu_err = -EFAULT;                                          \
213         long long __gu_val = 0;                                           \
214         const __typeof__(*(ptr)) __user *__gu_addr = (ptr);               \
215         if (access_ok(VERIFY_READ, __gu_addr, (size)))                    \
216                 __get_user_size64(__gu_val, __gu_addr, (size), __gu_err); \
217         (x) = (__typeof__(*(ptr)))__gu_val;                               \
218         __gu_err;                                                         \
219 })
220
221 extern long __get_user_bad(void);
222
223 #define __get_user_size(x, ptr, size, retval)                   \
224 do {                                                            \
225         retval = 0;                                             \
226         switch (size) {                                         \
227         case 1:                                                 \
228                 __get_user_asm(x, ptr, retval, "lbz");          \
229                 break;                                          \
230         case 2:                                                 \
231                 __get_user_asm(x, ptr, retval, "lhz");          \
232                 break;                                          \
233         case 4:                                                 \
234                 __get_user_asm(x, ptr, retval, "lwz");          \
235                 break;                                          \
236         default:                                                \
237                 x = __get_user_bad();                           \
238         }                                                       \
239 } while (0)
240
241 #define __get_user_size64(x, ptr, size, retval)                 \
242 do {                                                            \
243         retval = 0;                                             \
244         switch (size) {                                         \
245         case 1:                                                 \
246                 __get_user_asm(x, ptr, retval, "lbz");          \
247                 break;                                          \
248         case 2:                                                 \
249                 __get_user_asm(x, ptr, retval, "lhz");          \
250                 break;                                          \
251         case 4:                                                 \
252                 __get_user_asm(x, ptr, retval, "lwz");          \
253                 break;                                          \
254         case 8:                                                 \
255                 __get_user_asm2(x, ptr, retval);                \
256                 break;                                          \
257         default:                                                \
258                 x = __get_user_bad();                           \
259         }                                                       \
260 } while (0)
261
262 #define __get_user_asm(x, addr, err, op)                \
263         __asm__ __volatile__(                           \
264                 "1:     "op" %1,0(%2)\n"                \
265                 "2:\n"                                  \
266                 ".section .fixup,\"ax\"\n"              \
267                 "3:     li %0,%3\n"                     \
268                 "       li %1,0\n"                      \
269                 "       b 2b\n"                         \
270                 ".previous\n"                           \
271                 ".section __ex_table,\"a\"\n"           \
272                 "       .align 2\n"                     \
273                 "       .long 1b,3b\n"                  \
274                 ".previous"                             \
275                 : "=r"(err), "=r"(x)                    \
276                 : "b"(addr), "i"(-EFAULT), "0"(err))
277
278 #define __get_user_asm2(x, addr, err)                   \
279         __asm__ __volatile__(                           \
280                 "1:     lwz %1,0(%2)\n"                 \
281                 "2:     lwz %1+1,4(%2)\n"               \
282                 "3:\n"                                  \
283                 ".section .fixup,\"ax\"\n"              \
284                 "4:     li %0,%3\n"                     \
285                 "       li %1,0\n"                      \
286                 "       li %1+1,0\n"                    \
287                 "       b 3b\n"                         \
288                 ".previous\n"                           \
289                 ".section __ex_table,\"a\"\n"           \
290                 "       .align 2\n"                     \
291                 "       .long 1b,4b\n"                  \
292                 "       .long 2b,4b\n"                  \
293                 ".previous"                             \
294                 : "=r"(err), "=&r"(x)                   \
295                 : "b"(addr), "i"(-EFAULT), "0"(err))
296
297 /* more complex routines */
298
299 extern int __copy_tofrom_user(void __user *to, const void __user *from,
300                               unsigned long size);
301
302 extern inline unsigned long
303 copy_from_user(void *to, const void __user *from, unsigned long n)
304 {
305         unsigned long over;
306
307         if (access_ok(VERIFY_READ, from, n))
308                 return __copy_tofrom_user((void __user *)to, from, n);
309         if ((unsigned long)from < TASK_SIZE) {
310                 over = (unsigned long)from + n - TASK_SIZE;
311                 return __copy_tofrom_user((void __user *)to, from, n - over) + over;
312         }
313         return n;
314 }
315
316 extern inline unsigned long
317 copy_to_user(void __user *to, const void *from, unsigned long n)
318 {
319         unsigned long over;
320
321         if (access_ok(VERIFY_WRITE, to, n))
322                 return __copy_tofrom_user(to, (void __user *) from, n);
323         if ((unsigned long)to < TASK_SIZE) {
324                 over = (unsigned long)to + n - TASK_SIZE;
325                 return __copy_tofrom_user(to, (void __user *) from, n - over) + over;
326         }
327         return n;
328 }
329
330 #define __copy_from_user(to, from, size) \
331         __copy_tofrom_user((void __user *)(to), (from), (size))
332 #define __copy_to_user(to, from, size) \
333         __copy_tofrom_user((to), (void __user *)(from), (size))
334 #define __copy_to_user_inatomic __copy_to_user
335 #define __copy_from_user_inatomic __copy_from_user
336
337 extern unsigned long __clear_user(void __user *addr, unsigned long size);
338
339 extern inline unsigned long
340 clear_user(void __user *addr, unsigned long size)
341 {
342         if (access_ok(VERIFY_WRITE, addr, size))
343                 return __clear_user(addr, size);
344         if ((unsigned long)addr < TASK_SIZE) {
345                 unsigned long over = (unsigned long)addr + size - TASK_SIZE;
346                 return __clear_user(addr, size - over) + over;
347         }
348         return size;
349 }
350
351 extern int __strncpy_from_user(char *dst, const char __user *src, long count);
352
353 extern inline long
354 strncpy_from_user(char *dst, const char __user *src, long count)
355 {
356         if (access_ok(VERIFY_READ, src, 1))
357                 return __strncpy_from_user(dst, src, count);
358         return -EFAULT;
359 }
360
361 /*
362  * Return the size of a string (including the ending 0)
363  *
364  * Return 0 for error
365  */
366
367 extern int __strnlen_user(const char __user *str, long len, unsigned long top);
368
369 /*
370  * Returns the length of the string at str (including the null byte),
371  * or 0 if we hit a page we can't access,
372  * or something > len if we didn't find a null byte.
373  *
374  * The `top' parameter to __strnlen_user is to make sure that
375  * we can never overflow from the user area into kernel space.
376  */
377 extern __inline__ int strnlen_user(const char __user *str, long len)
378 {
379         unsigned long top = current->thread.fs.seg;
380
381         if ((unsigned long)str > top)
382                 return 0;
383         return __strnlen_user(str, len, top);
384 }
385
386 #define strlen_user(str)        strnlen_user((str), 0x7ffffffe)
387
388 #endif  /* __ASSEMBLY__ */
389
390 #endif  /* _PPC_UACCESS_H */
391 #endif /* __KERNEL__ */