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/asm-compat.h>
10 #include <asm/processor.h>
14 #define VERIFY_WRITE 1
17 * The fs value determines whether argument validity checking should be
18 * performed or not. If get_fs() == USER_DS, checking is performed, with
19 * get_fs() == KERNEL_DS, checking is bypassed.
21 * For historical reasons, these macros are grossly misnamed.
23 * The fs/ds values are now the highest legal address in the "segment".
24 * This simplifies the checking in the routines below.
27 #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
29 #define KERNEL_DS MAKE_MM_SEG(~0UL)
31 /* We use TASK_SIZE_USER64 as TASK_SIZE is not constant */
32 #define USER_DS MAKE_MM_SEG(TASK_SIZE_USER64 - 1)
34 #define USER_DS MAKE_MM_SEG(TASK_SIZE - 1)
37 #define get_ds() (KERNEL_DS)
38 #define get_fs() (current->thread.fs)
39 #define set_fs(val) (current->thread.fs = (val))
41 #define segment_eq(a, b) ((a).seg == (b).seg)
45 * This check is sufficient because there is a large enough
46 * gap between user addresses and the kernel addresses
48 #define __access_ok(addr, size, segment) \
49 (((addr) <= (segment).seg) && ((size) <= (segment).seg))
53 #define __access_ok(addr, size, segment) \
54 (((addr) <= (segment).seg) && \
55 (((size) == 0) || (((size) - 1) <= ((segment).seg - (addr)))))
59 #define access_ok(type, addr, size) \
60 (__chk_user_ptr(addr), \
61 __access_ok((__force unsigned long)(addr), (size), get_fs()))
64 * The exception table consists of pairs of addresses: the first is the
65 * address of an instruction that is allowed to fault, and the second is
66 * the address at which the program should continue. No registers are
67 * modified, so it is entirely up to the continuation code to figure out
70 * All the routines below use bits of fixup code that are out of line
71 * with the main instruction path. This means when everything is well,
72 * we don't even have to jump over them. Further, they do not intrude
73 * on our cache or tlb entries.
76 struct exception_table_entry
{
82 * These are the main single-value transfer routines. They automatically
83 * use the right size if we just have the right pointer type.
85 * This gets kind of ugly. We want to return _two_ values in "get_user()"
86 * and yet we don't want to do any pointers, because that is too much
87 * of a performance impact. Thus we have a few rather ugly macros here,
88 * and hide all the ugliness from the user.
90 * The "__xxx" versions of the user access functions are versions that
91 * do not verify the address space, that must have been done previously
92 * with a separate "access_ok()" call (this is used when we do multiple
93 * accesses to the same area of user memory).
95 * As we use the same address space for kernel and user data on the
96 * PowerPC, we can just do these as direct assignments. (Of course, the
97 * exception handling means that it's no longer "just"...)
99 * The "user64" versions of the user access functions are versions that
100 * allow access of 64-bit data. The "get_user" functions do not
101 * properly handle 64-bit data because the value gets down cast to a long.
102 * The "put_user" functions already handle 64-bit data properly but we add
103 * "user64" versions for completeness
105 #define get_user(x, ptr) \
106 __get_user_check((x), (ptr), sizeof(*(ptr)))
107 #define put_user(x, ptr) \
108 __put_user_check((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
110 #define __get_user(x, ptr) \
111 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
112 #define __put_user(x, ptr) \
113 __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
115 #ifndef __powerpc64__
116 #define __get_user64(x, ptr) \
117 __get_user64_nocheck((x), (ptr), sizeof(*(ptr)))
118 #define __put_user64(x, ptr) __put_user(x, ptr)
121 #define __get_user_inatomic(x, ptr) \
122 __get_user_nosleep((x), (ptr), sizeof(*(ptr)))
123 #define __put_user_inatomic(x, ptr) \
124 __put_user_nosleep((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
126 #define __get_user_unaligned __get_user
127 #define __put_user_unaligned __put_user
129 extern long __put_user_bad(void);
132 * We don't tell gcc that we are accessing memory, but this is OK
133 * because we do not write to any memory gcc knows about, so there
134 * are no aliasing issues.
136 #define __put_user_asm(x, addr, err, op) \
137 __asm__ __volatile__( \
138 "1: " op " %1,0(%2) # put_user\n" \
140 ".section .fixup,\"ax\"\n" \
144 ".section __ex_table,\"a\"\n" \
145 PPC_LONG_ALIGN "\n" \
149 : "r" (x), "b" (addr), "i" (-EFAULT), "0" (err))
152 #define __put_user_asm2(x, ptr, retval) \
153 __put_user_asm(x, ptr, retval, "std")
154 #else /* __powerpc64__ */
155 #define __put_user_asm2(x, addr, err) \
156 __asm__ __volatile__( \
157 "1: stw %1,0(%2)\n" \
158 "2: stw %1+1,4(%2)\n" \
160 ".section .fixup,\"ax\"\n" \
164 ".section __ex_table,\"a\"\n" \
165 PPC_LONG_ALIGN "\n" \
170 : "r" (x), "b" (addr), "i" (-EFAULT), "0" (err))
171 #endif /* __powerpc64__ */
173 #define __put_user_size(x, ptr, size, retval) \
177 case 1: __put_user_asm(x, ptr, retval, "stb"); break; \
178 case 2: __put_user_asm(x, ptr, retval, "sth"); break; \
179 case 4: __put_user_asm(x, ptr, retval, "stw"); break; \
180 case 8: __put_user_asm2(x, ptr, retval); break; \
181 default: __put_user_bad(); \
185 #define __put_user_nocheck(x, ptr, size) \
188 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
189 if (!is_kernel_addr((unsigned long)__pu_addr)) \
191 __chk_user_ptr(ptr); \
192 __put_user_size((x), __pu_addr, (size), __pu_err); \
196 #define __put_user_check(x, ptr, size) \
198 long __pu_err = -EFAULT; \
199 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
201 if (access_ok(VERIFY_WRITE, __pu_addr, size)) \
202 __put_user_size((x), __pu_addr, (size), __pu_err); \
206 #define __put_user_nosleep(x, ptr, size) \
209 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
210 __chk_user_ptr(ptr); \
211 __put_user_size((x), __pu_addr, (size), __pu_err); \
216 extern long __get_user_bad(void);
218 #define __get_user_asm(x, addr, err, op) \
219 __asm__ __volatile__( \
220 "1: "op" %1,0(%2) # get_user\n" \
222 ".section .fixup,\"ax\"\n" \
227 ".section __ex_table,\"a\"\n" \
228 PPC_LONG_ALIGN "\n" \
231 : "=r" (err), "=r" (x) \
232 : "b" (addr), "i" (-EFAULT), "0" (err))
235 #define __get_user_asm2(x, addr, err) \
236 __get_user_asm(x, addr, err, "ld")
237 #else /* __powerpc64__ */
238 #define __get_user_asm2(x, addr, err) \
239 __asm__ __volatile__( \
240 "1: lwz %1,0(%2)\n" \
241 "2: lwz %1+1,4(%2)\n" \
243 ".section .fixup,\"ax\"\n" \
249 ".section __ex_table,\"a\"\n" \
250 PPC_LONG_ALIGN "\n" \
254 : "=r" (err), "=&r" (x) \
255 : "b" (addr), "i" (-EFAULT), "0" (err))
256 #endif /* __powerpc64__ */
258 #define __get_user_size(x, ptr, size, retval) \
261 __chk_user_ptr(ptr); \
262 if (size > sizeof(x)) \
263 (x) = __get_user_bad(); \
265 case 1: __get_user_asm(x, ptr, retval, "lbz"); break; \
266 case 2: __get_user_asm(x, ptr, retval, "lhz"); break; \
267 case 4: __get_user_asm(x, ptr, retval, "lwz"); break; \
268 case 8: __get_user_asm2(x, ptr, retval); break; \
269 default: (x) = __get_user_bad(); \
273 #define __get_user_nocheck(x, ptr, size) \
276 unsigned long __gu_val; \
277 const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
278 __chk_user_ptr(ptr); \
279 if (!is_kernel_addr((unsigned long)__gu_addr)) \
281 __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
282 (x) = (__typeof__(*(ptr)))__gu_val; \
286 #ifndef __powerpc64__
287 #define __get_user64_nocheck(x, ptr, size) \
290 long long __gu_val; \
291 const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
292 __chk_user_ptr(ptr); \
293 if (!is_kernel_addr((unsigned long)__gu_addr)) \
295 __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
296 (x) = (__typeof__(*(ptr)))__gu_val; \
299 #endif /* __powerpc64__ */
301 #define __get_user_check(x, ptr, size) \
303 long __gu_err = -EFAULT; \
304 unsigned long __gu_val = 0; \
305 const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
307 if (access_ok(VERIFY_READ, __gu_addr, (size))) \
308 __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
309 (x) = (__typeof__(*(ptr)))__gu_val; \
313 #define __get_user_nosleep(x, ptr, size) \
316 unsigned long __gu_val; \
317 const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
318 __chk_user_ptr(ptr); \
319 __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
320 (x) = (__typeof__(*(ptr)))__gu_val; \
325 /* more complex routines */
327 extern unsigned long __copy_tofrom_user(void __user
*to
,
328 const void __user
*from
, unsigned long size
);
330 #ifndef __powerpc64__
332 static inline unsigned long copy_from_user(void *to
,
333 const void __user
*from
, unsigned long n
)
337 if (access_ok(VERIFY_READ
, from
, n
))
338 return __copy_tofrom_user((__force
void __user
*)to
, from
, n
);
339 if ((unsigned long)from
< TASK_SIZE
) {
340 over
= (unsigned long)from
+ n
- TASK_SIZE
;
341 return __copy_tofrom_user((__force
void __user
*)to
, from
,
347 static inline unsigned long copy_to_user(void __user
*to
,
348 const void *from
, unsigned long n
)
352 if (access_ok(VERIFY_WRITE
, to
, n
))
353 return __copy_tofrom_user(to
, (__force
void __user
*)from
, n
);
354 if ((unsigned long)to
< TASK_SIZE
) {
355 over
= (unsigned long)to
+ n
- TASK_SIZE
;
356 return __copy_tofrom_user(to
, (__force
void __user
*)from
,
362 #else /* __powerpc64__ */
364 #define __copy_in_user(to, from, size) \
365 __copy_tofrom_user((to), (from), (size))
367 extern unsigned long copy_from_user(void *to
, const void __user
*from
,
369 extern unsigned long copy_to_user(void __user
*to
, const void *from
,
371 extern unsigned long copy_in_user(void __user
*to
, const void __user
*from
,
374 #endif /* __powerpc64__ */
376 static inline unsigned long __copy_from_user_inatomic(void *to
,
377 const void __user
*from
, unsigned long n
)
379 if (__builtin_constant_p(n
) && (n
<= 8)) {
380 unsigned long ret
= 1;
384 __get_user_size(*(u8
*)to
, from
, 1, ret
);
387 __get_user_size(*(u16
*)to
, from
, 2, ret
);
390 __get_user_size(*(u32
*)to
, from
, 4, ret
);
393 __get_user_size(*(u64
*)to
, from
, 8, ret
);
399 return __copy_tofrom_user((__force
void __user
*)to
, from
, n
);
402 static inline unsigned long __copy_to_user_inatomic(void __user
*to
,
403 const void *from
, unsigned long n
)
405 if (__builtin_constant_p(n
) && (n
<= 8)) {
406 unsigned long ret
= 1;
410 __put_user_size(*(u8
*)from
, (u8 __user
*)to
, 1, ret
);
413 __put_user_size(*(u16
*)from
, (u16 __user
*)to
, 2, ret
);
416 __put_user_size(*(u32
*)from
, (u32 __user
*)to
, 4, ret
);
419 __put_user_size(*(u64
*)from
, (u64 __user
*)to
, 8, ret
);
425 return __copy_tofrom_user(to
, (__force
const void __user
*)from
, n
);
428 static inline unsigned long __copy_from_user(void *to
,
429 const void __user
*from
, unsigned long size
)
432 return __copy_from_user_inatomic(to
, from
, size
);
435 static inline unsigned long __copy_to_user(void __user
*to
,
436 const void *from
, unsigned long size
)
439 return __copy_to_user_inatomic(to
, from
, size
);
442 extern unsigned long __clear_user(void __user
*addr
, unsigned long size
);
444 static inline unsigned long clear_user(void __user
*addr
, unsigned long size
)
447 if (likely(access_ok(VERIFY_WRITE
, addr
, size
)))
448 return __clear_user(addr
, size
);
449 if ((unsigned long)addr
< TASK_SIZE
) {
450 unsigned long over
= (unsigned long)addr
+ size
- TASK_SIZE
;
451 return __clear_user(addr
, size
- over
) + over
;
456 extern int __strncpy_from_user(char *dst
, const char __user
*src
, long count
);
458 static inline long strncpy_from_user(char *dst
, const char __user
*src
,
462 if (likely(access_ok(VERIFY_READ
, src
, 1)))
463 return __strncpy_from_user(dst
, src
, count
);
468 * Return the size of a string (including the ending 0)
472 extern int __strnlen_user(const char __user
*str
, long len
, unsigned long top
);
475 * Returns the length of the string at str (including the null byte),
476 * or 0 if we hit a page we can't access,
477 * or something > len if we didn't find a null byte.
479 * The `top' parameter to __strnlen_user is to make sure that
480 * we can never overflow from the user area into kernel space.
482 static inline int strnlen_user(const char __user
*str
, long len
)
484 unsigned long top
= current
->thread
.fs
.seg
;
486 if ((unsigned long)str
> top
)
488 return __strnlen_user(str
, len
, top
);
491 #define strlen_user(str) strnlen_user((str), 0x7ffffffe)
493 #endif /* __ASSEMBLY__ */
494 #endif /* __KERNEL__ */
496 #endif /* _ARCH_POWERPC_UACCESS_H */