1 #ifndef _ARCH_POWERPC_UACCESS_H
2 #define _ARCH_POWERPC_UACCESS_H
7 #include <linux/sched.h>
8 #include <linux/errno.h>
9 #include <asm/processor.h>
13 #define VERIFY_WRITE 1
16 * The fs value determines whether argument validity checking should be
17 * performed or not. If get_fs() == USER_DS, checking is performed, with
18 * get_fs() == KERNEL_DS, checking is bypassed.
20 * For historical reasons, these macros are grossly misnamed.
22 * The fs/ds values are now the highest legal address in the "segment".
23 * This simplifies the checking in the routines below.
26 #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
28 #define KERNEL_DS MAKE_MM_SEG(~0UL)
30 /* We use TASK_SIZE_USER64 as TASK_SIZE is not constant */
31 #define USER_DS MAKE_MM_SEG(TASK_SIZE_USER64 - 1)
33 #define USER_DS MAKE_MM_SEG(TASK_SIZE - 1)
36 #define get_ds() (KERNEL_DS)
37 #define get_fs() (current->thread.fs)
38 #define set_fs(val) (current->thread.fs = (val))
40 #define segment_eq(a, b) ((a).seg == (b).seg)
44 * This check is sufficient because there is a large enough
45 * gap between user addresses and the kernel addresses
47 #define __access_ok(addr, size, segment) \
48 (((addr) <= (segment).seg) && ((size) <= (segment).seg))
52 #define __access_ok(addr, size, segment) \
53 (((addr) <= (segment).seg) && \
54 (((size) == 0) || (((size) - 1) <= ((segment).seg - (addr)))))
58 #define access_ok(type, addr, size) \
59 (__chk_user_ptr(addr), \
60 __access_ok((__force unsigned long)(addr), (size), get_fs()))
63 * The exception table consists of pairs of addresses: the first is the
64 * address of an instruction that is allowed to fault, and the second is
65 * the address at which the program should continue. No registers are
66 * modified, so it is entirely up to the continuation code to figure out
69 * All the routines below use bits of fixup code that are out of line
70 * with the main instruction path. This means when everything is well,
71 * we don't even have to jump over them. Further, they do not intrude
72 * on our cache or tlb entries.
75 struct exception_table_entry {
81 * These are the main single-value transfer routines. They automatically
82 * use the right size if we just have the right pointer type.
84 * This gets kind of ugly. We want to return _two_ values in "get_user()"
85 * and yet we don't want to do any pointers, because that is too much
86 * of a performance impact. Thus we have a few rather ugly macros here,
87 * and hide all the ugliness from the user.
89 * The "__xxx" versions of the user access functions are versions that
90 * do not verify the address space, that must have been done previously
91 * with a separate "access_ok()" call (this is used when we do multiple
92 * accesses to the same area of user memory).
94 * As we use the same address space for kernel and user data on the
95 * PowerPC, we can just do these as direct assignments. (Of course, the
96 * exception handling means that it's no longer "just"...)
98 * The "user64" versions of the user access functions are versions that
99 * allow access of 64-bit data. The "get_user" functions do not
100 * properly handle 64-bit data because the value gets down cast to a long.
101 * The "put_user" functions already handle 64-bit data properly but we add
102 * "user64" versions for completeness
104 #define get_user(x, ptr) \
105 __get_user_check((x), (ptr), sizeof(*(ptr)))
106 #define put_user(x, ptr) \
107 __put_user_check((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
109 #define __get_user(x, ptr) \
110 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
111 #define __put_user(x, ptr) \
112 __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
113 #ifndef __powerpc64__
114 #define __get_user64(x, ptr) \
115 __get_user64_nocheck((x), (ptr), sizeof(*(ptr)))
116 #define __put_user64(x, ptr) __put_user(x, ptr)
119 #define __get_user_unaligned __get_user
120 #define __put_user_unaligned __put_user
122 extern long __put_user_bad(void);
125 * We don't tell gcc that we are accessing memory, but this is OK
126 * because we do not write to any memory gcc knows about, so there
127 * are no aliasing issues.
129 #define __put_user_asm(x, addr, err, op) \
130 __asm__ __volatile__( \
131 "1: " op " %1,0(%2) # put_user\n" \
133 ".section .fixup,\"ax\"\n" \
137 ".section __ex_table,\"a\"\n" \
142 : "r" (x), "b" (addr), "i" (-EFAULT), "0" (err),\
143 "i"(sizeof(unsigned long)))
146 #define __put_user_asm2(x, ptr, retval) \
147 __put_user_asm(x, ptr, retval, "std")
148 #else /* __powerpc64__ */
149 #define __put_user_asm2(x, addr, err) \
150 __asm__ __volatile__( \
151 "1: stw %1,0(%2)\n" \
152 "2: stw %1+1,4(%2)\n" \
154 ".section .fixup,\"ax\"\n" \
158 ".section __ex_table,\"a\"\n" \
164 : "r" (x), "b" (addr), "i" (-EFAULT), "0" (err),\
165 "i"(sizeof(unsigned long)))
166 #endif /* __powerpc64__ */
168 #define __put_user_size(x, ptr, size, retval) \
172 case 1: __put_user_asm(x, ptr, retval, "stb"); break; \
173 case 2: __put_user_asm(x, ptr, retval, "sth"); break; \
174 case 4: __put_user_asm(x, ptr, retval, "stw"); break; \
175 case 8: __put_user_asm2(x, ptr, retval); break; \
176 default: __put_user_bad(); \
180 #define __put_user_nocheck(x, ptr, size) \
183 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
184 if (!is_kernel_addr((unsigned long)__pu_addr)) \
186 __chk_user_ptr(ptr); \
187 __put_user_size((x), __pu_addr, (size), __pu_err); \
191 #define __put_user_check(x, ptr, size) \
193 long __pu_err = -EFAULT; \
194 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
196 if (access_ok(VERIFY_WRITE, __pu_addr, size)) \
197 __put_user_size((x), __pu_addr, (size), __pu_err); \
201 extern long __get_user_bad(void);
203 #define __get_user_asm(x, addr, err, op) \
204 __asm__ __volatile__( \
205 "1: "op" %1,0(%2) # get_user\n" \
207 ".section .fixup,\"ax\"\n" \
212 ".section __ex_table,\"a\"\n" \
216 : "=r" (err), "=r" (x) \
217 : "b" (addr), "i" (-EFAULT), "0" (err), \
218 "i"(sizeof(unsigned long)))
221 #define __get_user_asm2(x, addr, err) \
222 __get_user_asm(x, addr, err, "ld")
223 #else /* __powerpc64__ */
224 #define __get_user_asm2(x, addr, err) \
225 __asm__ __volatile__( \
226 "1: lwz %1,0(%2)\n" \
227 "2: lwz %1+1,4(%2)\n" \
229 ".section .fixup,\"ax\"\n" \
235 ".section __ex_table,\"a\"\n" \
240 : "=r" (err), "=&r" (x) \
241 : "b" (addr), "i" (-EFAULT), "0" (err), \
242 "i"(sizeof(unsigned long)))
243 #endif /* __powerpc64__ */
245 #define __get_user_size(x, ptr, size, retval) \
248 __chk_user_ptr(ptr); \
249 if (size > sizeof(x)) \
250 (x) = __get_user_bad(); \
252 case 1: __get_user_asm(x, ptr, retval, "lbz"); break; \
253 case 2: __get_user_asm(x, ptr, retval, "lhz"); break; \
254 case 4: __get_user_asm(x, ptr, retval, "lwz"); break; \
255 case 8: __get_user_asm2(x, ptr, retval); break; \
256 default: (x) = __get_user_bad(); \
260 #define __get_user_nocheck(x, ptr, size) \
263 unsigned long __gu_val; \
264 const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
265 __chk_user_ptr(ptr); \
266 if (!is_kernel_addr((unsigned long)__gu_addr)) \
268 __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
269 (x) = (__typeof__(*(ptr)))__gu_val; \
273 #ifndef __powerpc64__
274 #define __get_user64_nocheck(x, ptr, size) \
277 long long __gu_val; \
278 const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
279 __chk_user_ptr(ptr); \
280 if (!is_kernel_addr((unsigned long)__gu_addr)) \
282 __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
283 (x) = (__typeof__(*(ptr)))__gu_val; \
286 #endif /* __powerpc64__ */
288 #define __get_user_check(x, ptr, size) \
290 long __gu_err = -EFAULT; \
291 unsigned long __gu_val = 0; \
292 const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
294 if (access_ok(VERIFY_READ, __gu_addr, (size))) \
295 __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
296 (x) = (__typeof__(*(ptr)))__gu_val; \
300 /* more complex routines */
302 extern unsigned long __copy_tofrom_user(void __user *to,
303 const void __user *from, unsigned long size);
305 #ifndef __powerpc64__
307 extern inline unsigned long copy_from_user(void *to,
308 const void __user *from, unsigned long n)
312 if (access_ok(VERIFY_READ, from, n))
313 return __copy_tofrom_user((__force void __user *)to, from, n);
314 if ((unsigned long)from < TASK_SIZE) {
315 over = (unsigned long)from + n - TASK_SIZE;
316 return __copy_tofrom_user((__force void __user *)to, from,
322 extern inline unsigned long copy_to_user(void __user *to,
323 const void *from, unsigned long n)
327 if (access_ok(VERIFY_WRITE, to, n))
328 return __copy_tofrom_user(to, (__force void __user *)from, n);
329 if ((unsigned long)to < TASK_SIZE) {
330 over = (unsigned long)to + n - TASK_SIZE;
331 return __copy_tofrom_user(to, (__force void __user *)from,
337 #else /* __powerpc64__ */
339 #define __copy_in_user(to, from, size) \
340 __copy_tofrom_user((to), (from), (size))
342 extern unsigned long copy_from_user(void *to, const void __user *from,
344 extern unsigned long copy_to_user(void __user *to, const void *from,
346 extern unsigned long copy_in_user(void __user *to, const void __user *from,
349 #endif /* __powerpc64__ */
351 static inline unsigned long __copy_from_user_inatomic(void *to,
352 const void __user *from, unsigned long n)
354 if (__builtin_constant_p(n) && (n <= 8)) {
359 __get_user_size(*(u8 *)to, from, 1, ret);
362 __get_user_size(*(u16 *)to, from, 2, ret);
365 __get_user_size(*(u32 *)to, from, 4, ret);
368 __get_user_size(*(u64 *)to, from, 8, ret);
374 return __copy_tofrom_user((__force void __user *)to, from, n);
377 static inline unsigned long __copy_to_user_inatomic(void __user *to,
378 const void *from, unsigned long n)
380 if (__builtin_constant_p(n) && (n <= 8)) {
385 __put_user_size(*(u8 *)from, (u8 __user *)to, 1, ret);
388 __put_user_size(*(u16 *)from, (u16 __user *)to, 2, ret);
391 __put_user_size(*(u32 *)from, (u32 __user *)to, 4, ret);
394 __put_user_size(*(u64 *)from, (u64 __user *)to, 8, ret);
400 return __copy_tofrom_user(to, (__force const void __user *)from, n);
403 static inline unsigned long __copy_from_user(void *to,
404 const void __user *from, unsigned long size)
407 return __copy_from_user_inatomic(to, from, size);
410 static inline unsigned long __copy_to_user(void __user *to,
411 const void *from, unsigned long size)
414 return __copy_to_user_inatomic(to, from, size);
417 extern unsigned long __clear_user(void __user *addr, unsigned long size);
419 static inline unsigned long clear_user(void __user *addr, unsigned long size)
422 if (likely(access_ok(VERIFY_WRITE, addr, size)))
423 return __clear_user(addr, size);
424 if ((unsigned long)addr < TASK_SIZE) {
425 unsigned long over = (unsigned long)addr + size - TASK_SIZE;
426 return __clear_user(addr, size - over) + over;
431 extern int __strncpy_from_user(char *dst, const char __user *src, long count);
433 static inline long strncpy_from_user(char *dst, const char __user *src,
437 if (likely(access_ok(VERIFY_READ, src, 1)))
438 return __strncpy_from_user(dst, src, count);
443 * Return the size of a string (including the ending 0)
447 extern int __strnlen_user(const char __user *str, long len, unsigned long top);
450 * Returns the length of the string at str (including the null byte),
451 * or 0 if we hit a page we can't access,
452 * or something > len if we didn't find a null byte.
454 * The `top' parameter to __strnlen_user is to make sure that
455 * we can never overflow from the user area into kernel space.
457 static inline int strnlen_user(const char __user *str, long len)
459 unsigned long top = current->thread.fs.seg;
461 if ((unsigned long)str > top)
463 return __strnlen_user(str, len, top);
466 #define strlen_user(str) strnlen_user((str), 0x7ffffffe)
468 #endif /* __ASSEMBLY__ */
469 #endif /* __KERNEL__ */
471 #endif /* _ARCH_POWERPC_UACCESS_H */