1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ARCH_POWERPC_UACCESS_H
3 #define _ARCH_POWERPC_UACCESS_H
5 #include <asm/asm-compat.h>
6 #include <asm/ppc_asm.h>
7 #include <asm/processor.h>
9 #include <asm/extable.h>
12 * The fs value determines whether argument validity checking should be
13 * performed or not. If get_fs() == USER_DS, checking is performed, with
14 * get_fs() == KERNEL_DS, checking is bypassed.
16 * For historical reasons, these macros are grossly misnamed.
18 * The fs/ds values are now the highest legal address in the "segment".
19 * This simplifies the checking in the routines below.
22 #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
24 #define KERNEL_DS MAKE_MM_SEG(~0UL)
26 /* We use TASK_SIZE_USER64 as TASK_SIZE is not constant */
27 #define USER_DS MAKE_MM_SEG(TASK_SIZE_USER64 - 1)
29 #define USER_DS MAKE_MM_SEG(TASK_SIZE - 1)
32 #define get_ds() (KERNEL_DS)
33 #define get_fs() (current->thread.fs)
34 #define set_fs(val) (current->thread.fs = (val))
36 #define segment_eq(a, b) ((a).seg == (b).seg)
38 #define user_addr_max() (get_fs().seg)
42 * This check is sufficient because there is a large enough
43 * gap between user addresses and the kernel addresses
45 #define __access_ok(addr, size, segment) \
46 (((addr) <= (segment).seg) && ((size) <= (segment).seg))
50 #define __access_ok(addr, size, segment) \
51 (((addr) <= (segment).seg) && \
52 (((size) == 0) || (((size) - 1) <= ((segment).seg - (addr)))))
56 #define access_ok(type, addr, size) \
57 (__chk_user_ptr(addr), \
58 __access_ok((__force unsigned long)(addr), (size), get_fs()))
61 * These are the main single-value transfer routines. They automatically
62 * use the right size if we just have the right pointer type.
64 * This gets kind of ugly. We want to return _two_ values in "get_user()"
65 * and yet we don't want to do any pointers, because that is too much
66 * of a performance impact. Thus we have a few rather ugly macros here,
67 * and hide all the ugliness from the user.
69 * The "__xxx" versions of the user access functions are versions that
70 * do not verify the address space, that must have been done previously
71 * with a separate "access_ok()" call (this is used when we do multiple
72 * accesses to the same area of user memory).
74 * As we use the same address space for kernel and user data on the
75 * PowerPC, we can just do these as direct assignments. (Of course, the
76 * exception handling means that it's no longer "just"...)
79 #define get_user(x, ptr) \
80 __get_user_check((x), (ptr), sizeof(*(ptr)))
81 #define put_user(x, ptr) \
82 __put_user_check((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
84 #define __get_user(x, ptr) \
85 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
86 #define __put_user(x, ptr) \
87 __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
89 #define __get_user_inatomic(x, ptr) \
90 __get_user_nosleep((x), (ptr), sizeof(*(ptr)))
91 #define __put_user_inatomic(x, ptr) \
92 __put_user_nosleep((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
94 extern long __put_user_bad(void);
97 * We don't tell gcc that we are accessing memory, but this is OK
98 * because we do not write to any memory gcc knows about, so there
99 * are no aliasing issues.
101 #define __put_user_asm(x, addr, err, op) \
102 __asm__ __volatile__( \
103 "1: " op " %1,0(%2) # put_user\n" \
105 ".section .fixup,\"ax\"\n" \
111 : "r" (x), "b" (addr), "i" (-EFAULT), "0" (err))
114 #define __put_user_asm2(x, ptr, retval) \
115 __put_user_asm(x, ptr, retval, "std")
116 #else /* __powerpc64__ */
117 #define __put_user_asm2(x, addr, err) \
118 __asm__ __volatile__( \
119 "1: stw %1,0(%2)\n" \
120 "2: stw %1+1,4(%2)\n" \
122 ".section .fixup,\"ax\"\n" \
129 : "r" (x), "b" (addr), "i" (-EFAULT), "0" (err))
130 #endif /* __powerpc64__ */
132 #define __put_user_size(x, ptr, size, retval) \
136 case 1: __put_user_asm(x, ptr, retval, "stb"); break; \
137 case 2: __put_user_asm(x, ptr, retval, "sth"); break; \
138 case 4: __put_user_asm(x, ptr, retval, "stw"); break; \
139 case 8: __put_user_asm2(x, ptr, retval); break; \
140 default: __put_user_bad(); \
144 #define __put_user_nocheck(x, ptr, size) \
147 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
148 if (!is_kernel_addr((unsigned long)__pu_addr)) \
150 __chk_user_ptr(ptr); \
151 __put_user_size((x), __pu_addr, (size), __pu_err); \
155 #define __put_user_check(x, ptr, size) \
157 long __pu_err = -EFAULT; \
158 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
160 if (access_ok(VERIFY_WRITE, __pu_addr, size)) \
161 __put_user_size((x), __pu_addr, (size), __pu_err); \
165 #define __put_user_nosleep(x, ptr, size) \
168 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
169 __chk_user_ptr(ptr); \
170 __put_user_size((x), __pu_addr, (size), __pu_err); \
175 extern long __get_user_bad(void);
178 * This does an atomic 128 byte aligned load from userspace.
179 * Upto caller to do enable_kernel_vmx() before calling!
181 #define __get_user_atomic_128_aligned(kaddr, uaddr, err) \
182 __asm__ __volatile__( \
183 "1: lvx 0,0,%1 # get user\n" \
184 " stvx 0,0,%2 # put kernel\n" \
186 ".section .fixup,\"ax\"\n" \
192 : "b" (uaddr), "b" (kaddr), "i" (-EFAULT), "0" (err))
194 #define __get_user_asm(x, addr, err, op) \
195 __asm__ __volatile__( \
196 "1: "op" %1,0(%2) # get_user\n" \
198 ".section .fixup,\"ax\"\n" \
204 : "=r" (err), "=r" (x) \
205 : "b" (addr), "i" (-EFAULT), "0" (err))
208 #define __get_user_asm2(x, addr, err) \
209 __get_user_asm(x, addr, err, "ld")
210 #else /* __powerpc64__ */
211 #define __get_user_asm2(x, addr, err) \
212 __asm__ __volatile__( \
213 "1: lwz %1,0(%2)\n" \
214 "2: lwz %1+1,4(%2)\n" \
216 ".section .fixup,\"ax\"\n" \
224 : "=r" (err), "=&r" (x) \
225 : "b" (addr), "i" (-EFAULT), "0" (err))
226 #endif /* __powerpc64__ */
228 #define __get_user_size(x, ptr, size, retval) \
231 __chk_user_ptr(ptr); \
232 if (size > sizeof(x)) \
233 (x) = __get_user_bad(); \
235 case 1: __get_user_asm(x, ptr, retval, "lbz"); break; \
236 case 2: __get_user_asm(x, ptr, retval, "lhz"); break; \
237 case 4: __get_user_asm(x, ptr, retval, "lwz"); break; \
238 case 8: __get_user_asm2(x, ptr, retval); break; \
239 default: (x) = __get_user_bad(); \
243 #define __get_user_nocheck(x, ptr, size) \
246 unsigned long __gu_val; \
247 const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
248 __chk_user_ptr(ptr); \
249 if (!is_kernel_addr((unsigned long)__gu_addr)) \
251 __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
252 (x) = (__typeof__(*(ptr)))__gu_val; \
256 #define __get_user_check(x, ptr, size) \
258 long __gu_err = -EFAULT; \
259 unsigned long __gu_val = 0; \
260 const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
262 if (access_ok(VERIFY_READ, __gu_addr, (size))) \
263 __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
264 (x) = (__force __typeof__(*(ptr)))__gu_val; \
268 #define __get_user_nosleep(x, ptr, size) \
271 unsigned long __gu_val; \
272 const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
273 __chk_user_ptr(ptr); \
274 __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
275 (x) = (__force __typeof__(*(ptr)))__gu_val; \
280 /* more complex routines */
282 extern unsigned long __copy_tofrom_user(void __user
*to
,
283 const void __user
*from
, unsigned long size
);
286 static inline unsigned long
287 raw_copy_in_user(void __user
*to
, const void __user
*from
, unsigned long n
)
289 return __copy_tofrom_user(to
, from
, n
);
291 #endif /* __powerpc64__ */
293 static inline unsigned long raw_copy_from_user(void *to
,
294 const void __user
*from
, unsigned long n
)
296 if (__builtin_constant_p(n
) && (n
<= 8)) {
297 unsigned long ret
= 1;
301 __get_user_size(*(u8
*)to
, from
, 1, ret
);
304 __get_user_size(*(u16
*)to
, from
, 2, ret
);
307 __get_user_size(*(u32
*)to
, from
, 4, ret
);
310 __get_user_size(*(u64
*)to
, from
, 8, ret
);
317 return __copy_tofrom_user((__force
void __user
*)to
, from
, n
);
320 static inline unsigned long raw_copy_to_user(void __user
*to
,
321 const void *from
, unsigned long n
)
323 if (__builtin_constant_p(n
) && (n
<= 8)) {
324 unsigned long ret
= 1;
328 __put_user_size(*(u8
*)from
, (u8 __user
*)to
, 1, ret
);
331 __put_user_size(*(u16
*)from
, (u16 __user
*)to
, 2, ret
);
334 __put_user_size(*(u32
*)from
, (u32 __user
*)to
, 4, ret
);
337 __put_user_size(*(u64
*)from
, (u64 __user
*)to
, 8, ret
);
344 return __copy_tofrom_user(to
, (__force
const void __user
*)from
, n
);
347 extern unsigned long __clear_user(void __user
*addr
, unsigned long size
);
349 static inline unsigned long clear_user(void __user
*addr
, unsigned long size
)
352 if (likely(access_ok(VERIFY_WRITE
, addr
, size
)))
353 return __clear_user(addr
, size
);
357 extern long strncpy_from_user(char *dst
, const char __user
*src
, long count
);
358 extern __must_check
long strnlen_user(const char __user
*str
, long n
);
360 extern long __copy_from_user_flushcache(void *dst
, const void __user
*src
,
362 extern void memcpy_page_flushcache(char *to
, struct page
*page
, size_t offset
,
365 #endif /* _ARCH_POWERPC_UACCESS_H */