1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ARCH_POWERPC_UACCESS_H
3 #define _ARCH_POWERPC_UACCESS_H
5 #include <asm/ppc_asm.h>
6 #include <asm/processor.h>
8 #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_fs() (current->thread.addr_limit)
34 static inline void set_fs(mm_segment_t fs
)
36 current
->thread
.addr_limit
= fs
;
37 /* On user-mode return check addr_limit (fs) is correct */
38 set_thread_flag(TIF_FSCHECK
);
41 #define segment_eq(a, b) ((a).seg == (b).seg)
43 #define user_addr_max() (get_fs().seg)
47 * This check is sufficient because there is a large enough
48 * gap between user addresses and the kernel addresses
50 #define __access_ok(addr, size, segment) \
51 (((addr) <= (segment).seg) && ((size) <= (segment).seg))
55 static inline int __access_ok(unsigned long addr
, unsigned long size
,
60 return (size
== 0 || size
- 1 <= seg
.seg
- addr
);
65 #define access_ok(addr, size) \
66 (__chk_user_ptr(addr), \
67 __access_ok((__force unsigned long)(addr), (size), get_fs()))
70 * These are the main single-value transfer routines. They automatically
71 * use the right size if we just have the right pointer type.
73 * This gets kind of ugly. We want to return _two_ values in "get_user()"
74 * and yet we don't want to do any pointers, because that is too much
75 * of a performance impact. Thus we have a few rather ugly macros here,
76 * and hide all the ugliness from the user.
78 * The "__xxx" versions of the user access functions are versions that
79 * do not verify the address space, that must have been done previously
80 * with a separate "access_ok()" call (this is used when we do multiple
81 * accesses to the same area of user memory).
83 * As we use the same address space for kernel and user data on the
84 * PowerPC, we can just do these as direct assignments. (Of course, the
85 * exception handling means that it's no longer "just"...)
88 #define get_user(x, ptr) \
89 __get_user_check((x), (ptr), sizeof(*(ptr)))
90 #define put_user(x, ptr) \
91 __put_user_check((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
93 #define __get_user(x, ptr) \
94 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
95 #define __put_user(x, ptr) \
96 __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
98 #define __get_user_inatomic(x, ptr) \
99 __get_user_nosleep((x), (ptr), sizeof(*(ptr)))
100 #define __put_user_inatomic(x, ptr) \
101 __put_user_nosleep((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
103 extern long __put_user_bad(void);
106 * We don't tell gcc that we are accessing memory, but this is OK
107 * because we do not write to any memory gcc knows about, so there
108 * are no aliasing issues.
110 #define __put_user_asm(x, addr, err, op) \
111 __asm__ __volatile__( \
112 "1: " op " %1,0(%2) # put_user\n" \
114 ".section .fixup,\"ax\"\n" \
120 : "r" (x), "b" (addr), "i" (-EFAULT), "0" (err))
123 #define __put_user_asm2(x, ptr, retval) \
124 __put_user_asm(x, ptr, retval, "std")
125 #else /* __powerpc64__ */
126 #define __put_user_asm2(x, addr, err) \
127 __asm__ __volatile__( \
128 "1: stw %1,0(%2)\n" \
129 "2: stw %1+1,4(%2)\n" \
131 ".section .fixup,\"ax\"\n" \
138 : "r" (x), "b" (addr), "i" (-EFAULT), "0" (err))
139 #endif /* __powerpc64__ */
141 #define __put_user_size(x, ptr, size, retval) \
144 allow_write_to_user(ptr, size); \
146 case 1: __put_user_asm(x, ptr, retval, "stb"); break; \
147 case 2: __put_user_asm(x, ptr, retval, "sth"); break; \
148 case 4: __put_user_asm(x, ptr, retval, "stw"); break; \
149 case 8: __put_user_asm2(x, ptr, retval); break; \
150 default: __put_user_bad(); \
152 prevent_write_to_user(ptr, size); \
155 #define __put_user_nocheck(x, ptr, size) \
158 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
159 if (!is_kernel_addr((unsigned long)__pu_addr)) \
161 __chk_user_ptr(ptr); \
162 __put_user_size((x), __pu_addr, (size), __pu_err); \
166 #define __put_user_check(x, ptr, size) \
168 long __pu_err = -EFAULT; \
169 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
171 if (access_ok(__pu_addr, size)) \
172 __put_user_size((x), __pu_addr, (size), __pu_err); \
176 #define __put_user_nosleep(x, ptr, size) \
179 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
180 __chk_user_ptr(ptr); \
181 __put_user_size((x), __pu_addr, (size), __pu_err); \
186 extern long __get_user_bad(void);
189 * This does an atomic 128 byte aligned load from userspace.
190 * Upto caller to do enable_kernel_vmx() before calling!
192 #define __get_user_atomic_128_aligned(kaddr, uaddr, err) \
193 __asm__ __volatile__( \
194 "1: lvx 0,0,%1 # get user\n" \
195 " stvx 0,0,%2 # put kernel\n" \
197 ".section .fixup,\"ax\"\n" \
203 : "b" (uaddr), "b" (kaddr), "i" (-EFAULT), "0" (err))
205 #define __get_user_asm(x, addr, err, op) \
206 __asm__ __volatile__( \
207 "1: "op" %1,0(%2) # get_user\n" \
209 ".section .fixup,\"ax\"\n" \
215 : "=r" (err), "=r" (x) \
216 : "b" (addr), "i" (-EFAULT), "0" (err))
219 #define __get_user_asm2(x, addr, err) \
220 __get_user_asm(x, addr, err, "ld")
221 #else /* __powerpc64__ */
222 #define __get_user_asm2(x, addr, err) \
223 __asm__ __volatile__( \
224 "1: lwz %1,0(%2)\n" \
225 "2: lwz %1+1,4(%2)\n" \
227 ".section .fixup,\"ax\"\n" \
235 : "=r" (err), "=&r" (x) \
236 : "b" (addr), "i" (-EFAULT), "0" (err))
237 #endif /* __powerpc64__ */
239 #define __get_user_size(x, ptr, size, retval) \
242 __chk_user_ptr(ptr); \
243 if (size > sizeof(x)) \
244 (x) = __get_user_bad(); \
245 allow_read_from_user(ptr, size); \
247 case 1: __get_user_asm(x, ptr, retval, "lbz"); break; \
248 case 2: __get_user_asm(x, ptr, retval, "lhz"); break; \
249 case 4: __get_user_asm(x, ptr, retval, "lwz"); break; \
250 case 8: __get_user_asm2(x, ptr, retval); break; \
251 default: (x) = __get_user_bad(); \
253 prevent_read_from_user(ptr, size); \
257 * This is a type: either unsigned long, if the argument fits into
258 * that type, or otherwise unsigned long long.
260 #define __long_type(x) \
261 __typeof__(__builtin_choose_expr(sizeof(x) > sizeof(0UL), 0ULL, 0UL))
263 #define __get_user_nocheck(x, ptr, size) \
266 __long_type(*(ptr)) __gu_val; \
267 __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
268 __chk_user_ptr(ptr); \
269 if (!is_kernel_addr((unsigned long)__gu_addr)) \
272 __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
273 (x) = (__typeof__(*(ptr)))__gu_val; \
277 #define __get_user_check(x, ptr, size) \
279 long __gu_err = -EFAULT; \
280 __long_type(*(ptr)) __gu_val = 0; \
281 __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
283 if (access_ok(__gu_addr, (size))) { \
285 __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
287 (x) = (__force __typeof__(*(ptr)))__gu_val; \
291 #define __get_user_nosleep(x, ptr, size) \
294 __long_type(*(ptr)) __gu_val; \
295 __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
296 __chk_user_ptr(ptr); \
298 __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
299 (x) = (__force __typeof__(*(ptr)))__gu_val; \
304 /* more complex routines */
306 extern unsigned long __copy_tofrom_user(void __user
*to
,
307 const void __user
*from
, unsigned long size
);
310 static inline unsigned long
311 raw_copy_in_user(void __user
*to
, const void __user
*from
, unsigned long n
)
316 allow_user_access(to
, from
, n
);
317 ret
= __copy_tofrom_user(to
, from
, n
);
318 prevent_user_access(to
, from
, n
);
321 #endif /* __powerpc64__ */
323 static inline unsigned long raw_copy_from_user(void *to
,
324 const void __user
*from
, unsigned long n
)
327 if (__builtin_constant_p(n
) && (n
<= 8)) {
333 __get_user_size(*(u8
*)to
, from
, 1, ret
);
337 __get_user_size(*(u16
*)to
, from
, 2, ret
);
341 __get_user_size(*(u32
*)to
, from
, 4, ret
);
345 __get_user_size(*(u64
*)to
, from
, 8, ret
);
353 allow_read_from_user(from
, n
);
354 ret
= __copy_tofrom_user((__force
void __user
*)to
, from
, n
);
355 prevent_read_from_user(from
, n
);
359 static inline unsigned long raw_copy_to_user(void __user
*to
,
360 const void *from
, unsigned long n
)
363 if (__builtin_constant_p(n
) && (n
<= 8)) {
368 __put_user_size(*(u8
*)from
, (u8 __user
*)to
, 1, ret
);
371 __put_user_size(*(u16
*)from
, (u16 __user
*)to
, 2, ret
);
374 __put_user_size(*(u32
*)from
, (u32 __user
*)to
, 4, ret
);
377 __put_user_size(*(u64
*)from
, (u64 __user
*)to
, 8, ret
);
384 allow_write_to_user(to
, n
);
385 ret
= __copy_tofrom_user(to
, (__force
const void __user
*)from
, n
);
386 prevent_write_to_user(to
, n
);
390 static __always_inline
unsigned long __must_check
391 copy_to_user_mcsafe(void __user
*to
, const void *from
, unsigned long n
)
393 if (likely(check_copy_size(from
, n
, true))) {
394 if (access_ok(to
, n
)) {
395 allow_write_to_user(to
, n
);
396 n
= memcpy_mcsafe((void *)to
, from
, n
);
397 prevent_write_to_user(to
, n
);
404 unsigned long __arch_clear_user(void __user
*addr
, unsigned long size
);
406 static inline unsigned long clear_user(void __user
*addr
, unsigned long size
)
408 unsigned long ret
= size
;
410 if (likely(access_ok(addr
, size
))) {
411 allow_write_to_user(addr
, size
);
412 ret
= __arch_clear_user(addr
, size
);
413 prevent_write_to_user(addr
, size
);
418 static inline unsigned long __clear_user(void __user
*addr
, unsigned long size
)
420 return clear_user(addr
, size
);
423 extern long strncpy_from_user(char *dst
, const char __user
*src
, long count
);
424 extern __must_check
long strnlen_user(const char __user
*str
, long n
);
426 extern long __copy_from_user_flushcache(void *dst
, const void __user
*src
,
428 extern void memcpy_page_flushcache(char *to
, struct page
*page
, size_t offset
,
431 #endif /* _ARCH_POWERPC_UACCESS_H */