1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __ASM_GENERIC_UACCESS_H
3 #define __ASM_GENERIC_UACCESS_H
6 * User space memory access functions, these should work
7 * on any machine that has kernel and user data in the same
8 * address space, e.g. all NOMMU machines.
10 #include <linux/string.h>
12 #ifdef CONFIG_UACCESS_MEMCPY
13 #include <asm/unaligned.h>
15 static inline int __get_user_fn(size_t size, const void __user *from, void *to)
17 BUILD_BUG_ON(!__builtin_constant_p(size));
21 *(u8 *)to = get_unaligned((u8 __force *)from);
24 *(u16 *)to = get_unaligned((u16 __force *)from);
27 *(u32 *)to = get_unaligned((u32 __force *)from);
30 *(u64 *)to = get_unaligned((u64 __force *)from);
38 #define __get_user_fn(sz, u, k) __get_user_fn(sz, u, k)
40 static inline int __put_user_fn(size_t size, void __user *to, void *from)
42 BUILD_BUG_ON(!__builtin_constant_p(size));
46 put_unaligned(*(u8 *)from, (u8 __force *)to);
49 put_unaligned(*(u16 *)from, (u16 __force *)to);
52 put_unaligned(*(u32 *)from, (u32 __force *)to);
55 put_unaligned(*(u64 *)from, (u64 __force *)to);
62 #define __put_user_fn(sz, u, k) __put_user_fn(sz, u, k)
64 #define __get_kernel_nofault(dst, src, type, err_label) \
66 *((type *)dst) = get_unaligned((type *)(src)); \
67 if (0) /* make sure the label looks used to the compiler */ \
71 #define __put_kernel_nofault(dst, src, type, err_label) \
73 put_unaligned(*((type *)src), (type *)(dst)); \
74 if (0) /* make sure the label looks used to the compiler */ \
78 #define HAVE_GET_KERNEL_NOFAULT 1
80 static inline __must_check unsigned long
81 raw_copy_from_user(void *to, const void __user * from, unsigned long n)
83 memcpy(to, (const void __force *)from, n);
87 static inline __must_check unsigned long
88 raw_copy_to_user(void __user *to, const void *from, unsigned long n)
90 memcpy((void __force *)to, from, n);
93 #define INLINE_COPY_FROM_USER
94 #define INLINE_COPY_TO_USER
95 #endif /* CONFIG_UACCESS_MEMCPY */
98 #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
101 #define KERNEL_DS MAKE_MM_SEG(~0UL)
105 #define USER_DS MAKE_MM_SEG(TASK_SIZE - 1)
109 #define get_fs() (current_thread_info()->addr_limit)
111 static inline void set_fs(mm_segment_t fs)
113 current_thread_info()->addr_limit = fs;
117 #ifndef uaccess_kernel
118 #define uaccess_kernel() (get_fs().seg == KERNEL_DS.seg)
120 #endif /* CONFIG_SET_FS */
122 #define access_ok(addr, size) __access_ok((unsigned long)(addr),(size))
125 * The architecture should really override this if possible, at least
126 * doing a check on the get_fs()
129 static inline int __access_ok(unsigned long addr, unsigned long size)
136 * These are the main single-value transfer routines. They automatically
137 * use the right size if we just have the right pointer type.
138 * This version just falls back to copy_{from,to}_user, which should
139 * provide a fast-path for small values.
141 #define __put_user(x, ptr) \
143 __typeof__(*(ptr)) __x = (x); \
144 int __pu_err = -EFAULT; \
145 __chk_user_ptr(ptr); \
146 switch (sizeof (*(ptr))) { \
151 __pu_err = __put_user_fn(sizeof (*(ptr)), \
161 #define put_user(x, ptr) \
163 void __user *__p = (ptr); \
165 access_ok(__p, sizeof(*ptr)) ? \
166 __put_user((x), ((__typeof__(*(ptr)) __user *)__p)) : \
170 #ifndef __put_user_fn
172 static inline int __put_user_fn(size_t size, void __user *ptr, void *x)
174 return unlikely(raw_copy_to_user(ptr, x, size)) ? -EFAULT : 0;
177 #define __put_user_fn(sz, u, k) __put_user_fn(sz, u, k)
181 extern int __put_user_bad(void) __attribute__((noreturn));
183 #define __get_user(x, ptr) \
185 int __gu_err = -EFAULT; \
186 __chk_user_ptr(ptr); \
187 switch (sizeof(*(ptr))) { \
189 unsigned char __x = 0; \
190 __gu_err = __get_user_fn(sizeof (*(ptr)), \
192 (x) = *(__force __typeof__(*(ptr)) *) &__x; \
196 unsigned short __x = 0; \
197 __gu_err = __get_user_fn(sizeof (*(ptr)), \
199 (x) = *(__force __typeof__(*(ptr)) *) &__x; \
203 unsigned int __x = 0; \
204 __gu_err = __get_user_fn(sizeof (*(ptr)), \
206 (x) = *(__force __typeof__(*(ptr)) *) &__x; \
210 unsigned long long __x = 0; \
211 __gu_err = __get_user_fn(sizeof (*(ptr)), \
213 (x) = *(__force __typeof__(*(ptr)) *) &__x; \
223 #define get_user(x, ptr) \
225 const void __user *__p = (ptr); \
227 access_ok(__p, sizeof(*ptr)) ? \
228 __get_user((x), (__typeof__(*(ptr)) __user *)__p) :\
229 ((x) = (__typeof__(*(ptr)))0,-EFAULT); \
232 #ifndef __get_user_fn
233 static inline int __get_user_fn(size_t size, const void __user *ptr, void *x)
235 return unlikely(raw_copy_from_user(x, ptr, size)) ? -EFAULT : 0;
238 #define __get_user_fn(sz, u, k) __get_user_fn(sz, u, k)
242 extern int __get_user_bad(void) __attribute__((noreturn));
245 * Copy a null terminated string from userspace.
247 #ifndef __strncpy_from_user
249 __strncpy_from_user(char *dst, const char __user *src, long count)
252 strncpy(dst, (const char __force *)src, count);
253 for (tmp = dst; *tmp && count > 0; tmp++, count--)
260 strncpy_from_user(char *dst, const char __user *src, long count)
262 if (!access_ok(src, 1))
264 return __strncpy_from_user(dst, src, count);
268 * Return the size of a string (including the ending 0)
270 * Return 0 on exception, a value greater than N if too long
272 #ifndef __strnlen_user
273 #define __strnlen_user(s, n) (strnlen((s), (n)) + 1)
277 * Unlike strnlen, strnlen_user includes the nul terminator in
278 * its returned count. Callers should check for a returned value
279 * greater than N as an indication the string is too long.
281 static inline long strnlen_user(const char __user *src, long n)
283 if (!access_ok(src, 1))
285 return __strnlen_user(src, n);
292 static inline __must_check unsigned long
293 __clear_user(void __user *to, unsigned long n)
295 memset((void __force *)to, 0, n);
300 static inline __must_check unsigned long
301 clear_user(void __user *to, unsigned long n)
304 if (!access_ok(to, n))
307 return __clear_user(to, n);
310 #include <asm/extable.h>
312 #endif /* __ASM_GENERIC_UACCESS_H */