1 #ifndef _ARCH_POWERPC_UACCESS_H
2 #define _ARCH_POWERPC_UACCESS_H
4 #include <asm/asm-compat.h>
5 #include <asm/ppc_asm.h>
6 #include <asm/processor.h>
8 #include <asm/extable.h>
11 * The fs value determines whether argument validity checking should be
12 * performed or not. If get_fs() == USER_DS, checking is performed, with
13 * get_fs() == KERNEL_DS, checking is bypassed.
15 * For historical reasons, these macros are grossly misnamed.
17 * The fs/ds values are now the highest legal address in the "segment".
18 * This simplifies the checking in the routines below.
21 #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
23 #define KERNEL_DS MAKE_MM_SEG(~0UL)
25 /* We use TASK_SIZE_USER64 as TASK_SIZE is not constant */
26 #define USER_DS MAKE_MM_SEG(TASK_SIZE_USER64 - 1)
28 #define USER_DS MAKE_MM_SEG(TASK_SIZE - 1)
31 #define get_ds() (KERNEL_DS)
32 #define get_fs() (current->thread.fs)
33 #define set_fs(val) (current->thread.fs = (val))
35 #define segment_eq(a, b) ((a).seg == (b).seg)
37 #define user_addr_max() (get_fs().seg)
41 * This check is sufficient because there is a large enough
42 * gap between user addresses and the kernel addresses
44 #define __access_ok(addr, size, segment) \
45 (((addr) <= (segment).seg) && ((size) <= (segment).seg))
49 #define __access_ok(addr, size, segment) \
50 (((addr) <= (segment).seg) && \
51 (((size) == 0) || (((size) - 1) <= ((segment).seg - (addr)))))
55 #define access_ok(type, addr, size) \
56 (__chk_user_ptr(addr), \
57 __access_ok((__force unsigned long)(addr), (size), get_fs()))
60 * These are the main single-value transfer routines. They automatically
61 * use the right size if we just have the right pointer type.
63 * This gets kind of ugly. We want to return _two_ values in "get_user()"
64 * and yet we don't want to do any pointers, because that is too much
65 * of a performance impact. Thus we have a few rather ugly macros here,
66 * and hide all the ugliness from the user.
68 * The "__xxx" versions of the user access functions are versions that
69 * do not verify the address space, that must have been done previously
70 * with a separate "access_ok()" call (this is used when we do multiple
71 * accesses to the same area of user memory).
73 * As we use the same address space for kernel and user data on the
74 * PowerPC, we can just do these as direct assignments. (Of course, the
75 * exception handling means that it's no longer "just"...)
78 #define get_user(x, ptr) \
79 __get_user_check((x), (ptr), sizeof(*(ptr)))
80 #define put_user(x, ptr) \
81 __put_user_check((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
83 #define __get_user(x, ptr) \
84 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
85 #define __put_user(x, ptr) \
86 __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
88 #define __get_user_inatomic(x, ptr) \
89 __get_user_nosleep((x), (ptr), sizeof(*(ptr)))
90 #define __put_user_inatomic(x, ptr) \
91 __put_user_nosleep((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
93 extern long __put_user_bad(void);
96 * We don't tell gcc that we are accessing memory, but this is OK
97 * because we do not write to any memory gcc knows about, so there
98 * are no aliasing issues.
100 #define __put_user_asm(x, addr, err, op) \
101 __asm__ __volatile__( \
102 "1: " op " %1,0(%2) # put_user\n" \
104 ".section .fixup,\"ax\"\n" \
110 : "r" (x), "b" (addr), "i" (-EFAULT), "0" (err))
113 #define __put_user_asm2(x, ptr, retval) \
114 __put_user_asm(x, ptr, retval, "std")
115 #else /* __powerpc64__ */
116 #define __put_user_asm2(x, addr, err) \
117 __asm__ __volatile__( \
118 "1: stw %1,0(%2)\n" \
119 "2: stw %1+1,4(%2)\n" \
121 ".section .fixup,\"ax\"\n" \
128 : "r" (x), "b" (addr), "i" (-EFAULT), "0" (err))
129 #endif /* __powerpc64__ */
131 #define __put_user_size(x, ptr, size, retval) \
135 case 1: __put_user_asm(x, ptr, retval, "stb"); break; \
136 case 2: __put_user_asm(x, ptr, retval, "sth"); break; \
137 case 4: __put_user_asm(x, ptr, retval, "stw"); break; \
138 case 8: __put_user_asm2(x, ptr, retval); break; \
139 default: __put_user_bad(); \
143 #define __put_user_nocheck(x, ptr, size) \
146 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
147 if (!is_kernel_addr((unsigned long)__pu_addr)) \
149 __chk_user_ptr(ptr); \
150 __put_user_size((x), __pu_addr, (size), __pu_err); \
154 #define __put_user_check(x, ptr, size) \
156 long __pu_err = -EFAULT; \
157 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
159 if (access_ok(VERIFY_WRITE, __pu_addr, size)) \
160 __put_user_size((x), __pu_addr, (size), __pu_err); \
164 #define __put_user_nosleep(x, ptr, size) \
167 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
168 __chk_user_ptr(ptr); \
169 __put_user_size((x), __pu_addr, (size), __pu_err); \
174 extern long __get_user_bad(void);
176 #define __get_user_asm(x, addr, err, op) \
177 __asm__ __volatile__( \
178 "1: "op" %1,0(%2) # get_user\n" \
180 ".section .fixup,\"ax\"\n" \
186 : "=r" (err), "=r" (x) \
187 : "b" (addr), "i" (-EFAULT), "0" (err))
190 #define __get_user_asm2(x, addr, err) \
191 __get_user_asm(x, addr, err, "ld")
192 #else /* __powerpc64__ */
193 #define __get_user_asm2(x, addr, err) \
194 __asm__ __volatile__( \
195 "1: lwz %1,0(%2)\n" \
196 "2: lwz %1+1,4(%2)\n" \
198 ".section .fixup,\"ax\"\n" \
206 : "=r" (err), "=&r" (x) \
207 : "b" (addr), "i" (-EFAULT), "0" (err))
208 #endif /* __powerpc64__ */
210 #define __get_user_size(x, ptr, size, retval) \
213 __chk_user_ptr(ptr); \
214 if (size > sizeof(x)) \
215 (x) = __get_user_bad(); \
217 case 1: __get_user_asm(x, ptr, retval, "lbz"); break; \
218 case 2: __get_user_asm(x, ptr, retval, "lhz"); break; \
219 case 4: __get_user_asm(x, ptr, retval, "lwz"); break; \
220 case 8: __get_user_asm2(x, ptr, retval); break; \
221 default: (x) = __get_user_bad(); \
225 #define __get_user_nocheck(x, ptr, size) \
228 unsigned long __gu_val; \
229 const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
230 __chk_user_ptr(ptr); \
231 if (!is_kernel_addr((unsigned long)__gu_addr)) \
233 __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
234 (x) = (__typeof__(*(ptr)))__gu_val; \
238 #define __get_user_check(x, ptr, size) \
240 long __gu_err = -EFAULT; \
241 unsigned long __gu_val = 0; \
242 const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
244 if (access_ok(VERIFY_READ, __gu_addr, (size))) \
245 __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
246 (x) = (__force __typeof__(*(ptr)))__gu_val; \
250 #define __get_user_nosleep(x, ptr, size) \
253 unsigned long __gu_val; \
254 const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
255 __chk_user_ptr(ptr); \
256 __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
257 (x) = (__force __typeof__(*(ptr)))__gu_val; \
262 /* more complex routines */
264 extern unsigned long __copy_tofrom_user(void __user
*to
,
265 const void __user
*from
, unsigned long size
);
268 static inline unsigned long
269 raw_copy_in_user(void __user
*to
, const void __user
*from
, unsigned long n
)
271 return __copy_tofrom_user(to
, from
, n
);
273 #endif /* __powerpc64__ */
275 static inline unsigned long raw_copy_from_user(void *to
,
276 const void __user
*from
, unsigned long n
)
278 if (__builtin_constant_p(n
) && (n
<= 8)) {
279 unsigned long ret
= 1;
283 __get_user_size(*(u8
*)to
, from
, 1, ret
);
286 __get_user_size(*(u16
*)to
, from
, 2, ret
);
289 __get_user_size(*(u32
*)to
, from
, 4, ret
);
292 __get_user_size(*(u64
*)to
, from
, 8, ret
);
299 return __copy_tofrom_user((__force
void __user
*)to
, from
, n
);
302 static inline unsigned long raw_copy_to_user(void __user
*to
,
303 const void *from
, unsigned long n
)
305 if (__builtin_constant_p(n
) && (n
<= 8)) {
306 unsigned long ret
= 1;
310 __put_user_size(*(u8
*)from
, (u8 __user
*)to
, 1, ret
);
313 __put_user_size(*(u16
*)from
, (u16 __user
*)to
, 2, ret
);
316 __put_user_size(*(u32
*)from
, (u32 __user
*)to
, 4, ret
);
319 __put_user_size(*(u64
*)from
, (u64 __user
*)to
, 8, ret
);
326 return __copy_tofrom_user(to
, (__force
const void __user
*)from
, n
);
329 extern unsigned long __clear_user(void __user
*addr
, unsigned long size
);
331 static inline unsigned long clear_user(void __user
*addr
, unsigned long size
)
334 if (likely(access_ok(VERIFY_WRITE
, addr
, size
)))
335 return __clear_user(addr
, size
);
339 extern long strncpy_from_user(char *dst
, const char __user
*src
, long count
);
340 extern __must_check
long strnlen_user(const char __user
*str
, long n
);
342 #endif /* _ARCH_POWERPC_UACCESS_H */