1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __SCORE_UACCESS_H
3 #define __SCORE_UACCESS_H
5 #include <linux/kernel.h>
6 #include <asm/extable.h>
8 #define get_ds() (KERNEL_DS)
9 #define get_fs() (current_thread_info()->addr_limit)
10 #define segment_eq(a, b) ((a).seg == (b).seg)
13 * Is a address valid? This does a straighforward calculation rather
17 * - "addr" doesn't have any high-bits set
18 * - AND "size" doesn't have any high-bits set
19 * - AND "addr+size" doesn't have any high-bits set
20 * - OR we are in kernel mode.
22 * __ua_size() is a trick to avoid runtime checking of positive constant
23 * sizes; for those we already know at compile time that the size is ok.
25 #define __ua_size(size) \
26 ((__builtin_constant_p(size) && (signed long) (size) > 0) ? 0 : (size))
29 * access_ok: - Checks if a user space pointer is valid
30 * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
31 * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
32 * to write to a block, it is always safe to read from it.
33 * @addr: User space pointer to start of block to check
34 * @size: Size of block to check
36 * Context: User context only. This function may sleep if pagefaults are
39 * Checks if a pointer to a block of memory in user space is valid.
41 * Returns true (nonzero) if the memory block may be valid, false (zero)
42 * if it is definitely invalid.
44 * Note that, depending on architecture, this function probably just
45 * checks that the pointer is in the user space range - after calling
46 * this function, memory access functions may still return -EFAULT.
49 #define __access_ok(addr, size) \
50 (((long)((get_fs().seg) & \
51 ((addr) | ((addr) + (size)) | \
52 __ua_size(size)))) == 0)
54 #define access_ok(type, addr, size) \
55 likely(__access_ok((unsigned long)(addr), (size)))
58 * put_user: - Write a simple value into user space.
59 * @x: Value to copy to user space.
60 * @ptr: Destination address, in user space.
62 * Context: User context only. This function may sleep if pagefaults are
65 * This macro copies a single simple value from kernel space to user
66 * space. It supports simple types like char and int, but not larger
67 * data types like structures or arrays.
69 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
70 * to the result of dereferencing @ptr.
72 * Returns zero on success, or -EFAULT on error.
74 #define put_user(x, ptr) __put_user_check((x), (ptr), sizeof(*(ptr)))
77 * get_user: - Get a simple variable from user space.
78 * @x: Variable to store result.
79 * @ptr: Source address, in user space.
81 * Context: User context only. This function may sleep if pagefaults are
84 * This macro copies a single simple variable from user space to kernel
85 * space. It supports simple types like char and int, but not larger
86 * data types like structures or arrays.
88 * @ptr must have pointer-to-simple-variable type, and the result of
89 * dereferencing @ptr must be assignable to @x without a cast.
91 * Returns zero on success, or -EFAULT on error.
92 * On error, the variable @x is set to zero.
94 #define get_user(x, ptr) __get_user_check((x), (ptr), sizeof(*(ptr)))
97 * __put_user: - Write a simple value into user space, with less checking.
98 * @x: Value to copy to user space.
99 * @ptr: Destination address, in user space.
101 * Context: User context only. This function may sleep if pagefaults are
104 * This macro copies a single simple value from kernel space to user
105 * space. It supports simple types like char and int, but not larger
106 * data types like structures or arrays.
108 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
109 * to the result of dereferencing @ptr.
111 * Caller must check the pointer with access_ok() before calling this
114 * Returns zero on success, or -EFAULT on error.
116 #define __put_user(x, ptr) __put_user_nocheck((x), (ptr), sizeof(*(ptr)))
119 * __get_user: - Get a simple variable from user space, with less checking.
120 * @x: Variable to store result.
121 * @ptr: Source address, in user space.
123 * Context: User context only. This function may sleep if pagefaults are
126 * This macro copies a single simple variable from user space to kernel
127 * space. It supports simple types like char and int, but not larger
128 * data types like structures or arrays.
130 * @ptr must have pointer-to-simple-variable type, and the result of
131 * dereferencing @ptr must be assignable to @x without a cast.
133 * Caller must check the pointer with access_ok() before calling this
136 * Returns zero on success, or -EFAULT on error.
137 * On error, the variable @x is set to zero.
139 #define __get_user(x, ptr) __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
141 struct __large_struct
{ unsigned long buf
[100]; };
142 #define __m(x) (*(struct __large_struct __user *)(x))
145 * Yuck. We need two variants, one for 64bit operation and one
146 * for 32 bit mode and old iron.
148 extern void __get_user_unknown(void);
150 #define __get_user_common(val, size, ptr) \
154 __get_user_asm(val, "lb", ptr); \
157 __get_user_asm(val, "lh", ptr); \
160 __get_user_asm(val, "lw", ptr); \
163 if (__copy_from_user((void *)&val, ptr, 8) == 0) \
166 __gu_err = -EFAULT; \
169 __get_user_unknown(); \
174 #define __get_user_nocheck(x, ptr, size) \
177 __get_user_common((x), size, ptr); \
181 #define __get_user_check(x, ptr, size) \
183 long __gu_err = -EFAULT; \
184 const __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
186 if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) \
187 __get_user_common((x), size, __gu_ptr); \
194 #define __get_user_asm(val, insn, addr) \
198 __asm__ __volatile__( \
199 "1:" insn " %1, %3\n" \
201 ".section .fixup,\"ax\"\n" \
206 ".section __ex_table,\"a\"\n" \
209 : "=r" (__gu_err), "=r" (__gu_tmp) \
210 : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \
212 (val) = (__typeof__(*(addr))) __gu_tmp; \
216 * Yuck. We need two variants, one for 64bit operation and one
217 * for 32 bit mode and old iron.
219 #define __put_user_nocheck(val, ptr, size) \
221 __typeof__(*(ptr)) __pu_val; \
227 __put_user_asm("sb", ptr); \
230 __put_user_asm("sh", ptr); \
233 __put_user_asm("sw", ptr); \
236 if ((__copy_to_user((void *)ptr, &__pu_val, 8)) == 0) \
239 __pu_err = -EFAULT; \
242 __put_user_unknown(); \
249 #define __put_user_check(val, ptr, size) \
251 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
252 __typeof__(*(ptr)) __pu_val = (val); \
253 long __pu_err = -EFAULT; \
255 if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) { \
258 __put_user_asm("sb", __pu_addr); \
261 __put_user_asm("sh", __pu_addr); \
264 __put_user_asm("sw", __pu_addr); \
267 if ((__copy_to_user((void *)__pu_addr, &__pu_val, 8)) == 0)\
270 __pu_err = -EFAULT; \
273 __put_user_unknown(); \
280 #define __put_user_asm(insn, ptr) \
281 __asm__ __volatile__( \
282 "1:" insn " %2, %3\n" \
284 ".section .fixup,\"ax\"\n" \
288 ".section __ex_table,\"a\"\n" \
292 : "0" (0), "r" (__pu_val), "o" (__m(ptr)), \
295 extern void __put_user_unknown(void);
296 extern int __copy_tofrom_user(void *to
, const void *from
, unsigned long len
);
298 static inline unsigned long
299 raw_copy_from_user(void *to
, const void __user
*from
, unsigned long len
)
301 return __copy_tofrom_user(to
, (__force
const void *)from
, len
);
304 static inline unsigned long
305 raw_copy_to_user(void __user
*to
, const void *from
, unsigned long len
)
307 return __copy_tofrom_user((__force
void *)to
, from
, len
);
310 #define INLINE_COPY_FROM_USER
311 #define INLINE_COPY_TO_USER
314 * __clear_user: - Zero a block of memory in user space, with less checking.
315 * @to: Destination address, in user space.
316 * @n: Number of bytes to zero.
318 * Zero a block of memory in user space. Caller must check
319 * the specified block with access_ok() before calling this function.
321 * Returns number of bytes that could not be cleared.
322 * On success, this will be zero.
324 extern unsigned long __clear_user(void __user
*src
, unsigned long size
);
326 static inline unsigned long clear_user(char *src
, unsigned long size
)
328 if (access_ok(VERIFY_WRITE
, src
, size
))
329 return __clear_user(src
, size
);
334 * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
335 * @dst: Destination address, in kernel space. This buffer must be at
336 * least @count bytes long.
337 * @src: Source address, in user space.
338 * @count: Maximum number of bytes to copy, including the trailing NUL.
340 * Copies a NUL-terminated string from userspace to kernel space.
341 * Caller must check the specified block with access_ok() before calling
344 * On success, returns the length of the string (not including the trailing
347 * If access to userspace fails, returns -EFAULT (some data may have been
350 * If @count is smaller than the length of the string, copies @count bytes
351 * and returns @count.
353 extern int __strncpy_from_user(char *dst
, const char *src
, long len
);
355 static inline int strncpy_from_user(char *dst
, const char *src
, long len
)
357 if (access_ok(VERIFY_READ
, src
, 1))
358 return __strncpy_from_user(dst
, src
, len
);
363 extern int __strnlen_user(const char *str
, long len
);
364 static inline long strnlen_user(const char __user
*str
, long len
)
366 if (!access_ok(VERIFY_READ
, str
, 0))
369 return __strnlen_user(str
, len
);
372 #endif /* __SCORE_UACCESS_H */