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 #include <asm/segment.h>
14 #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
17 #define KERNEL_DS MAKE_MM_SEG(~0UL)
21 #define USER_DS MAKE_MM_SEG(TASK_SIZE - 1)
25 #define get_fs() (current_thread_info()->addr_limit)
27 static inline void set_fs(mm_segment_t fs
)
29 current_thread_info()->addr_limit
= fs
;
34 #define segment_eq(a, b) ((a).seg == (b).seg)
37 #define access_ok(addr, size) __access_ok((unsigned long)(addr),(size))
40 * The architecture should really override this if possible, at least
41 * doing a check on the get_fs()
44 static inline int __access_ok(unsigned long addr
, unsigned long size
)
51 * These are the main single-value transfer routines. They automatically
52 * use the right size if we just have the right pointer type.
53 * This version just falls back to copy_{from,to}_user, which should
54 * provide a fast-path for small values.
56 #define __put_user(x, ptr) \
58 __typeof__(*(ptr)) __x = (x); \
59 int __pu_err = -EFAULT; \
60 __chk_user_ptr(ptr); \
61 switch (sizeof (*(ptr))) { \
66 __pu_err = __put_user_fn(sizeof (*(ptr)), \
76 #define put_user(x, ptr) \
78 void __user *__p = (ptr); \
80 access_ok(__p, sizeof(*ptr)) ? \
81 __put_user((x), ((__typeof__(*(ptr)) __user *)__p)) : \
87 static inline int __put_user_fn(size_t size
, void __user
*ptr
, void *x
)
89 return unlikely(raw_copy_to_user(ptr
, x
, size
)) ? -EFAULT
: 0;
92 #define __put_user_fn(sz, u, k) __put_user_fn(sz, u, k)
96 extern int __put_user_bad(void) __attribute__((noreturn
));
98 #define __get_user(x, ptr) \
100 int __gu_err = -EFAULT; \
101 __chk_user_ptr(ptr); \
102 switch (sizeof(*(ptr))) { \
104 unsigned char __x = 0; \
105 __gu_err = __get_user_fn(sizeof (*(ptr)), \
107 (x) = *(__force __typeof__(*(ptr)) *) &__x; \
111 unsigned short __x = 0; \
112 __gu_err = __get_user_fn(sizeof (*(ptr)), \
114 (x) = *(__force __typeof__(*(ptr)) *) &__x; \
118 unsigned int __x = 0; \
119 __gu_err = __get_user_fn(sizeof (*(ptr)), \
121 (x) = *(__force __typeof__(*(ptr)) *) &__x; \
125 unsigned long long __x = 0; \
126 __gu_err = __get_user_fn(sizeof (*(ptr)), \
128 (x) = *(__force __typeof__(*(ptr)) *) &__x; \
138 #define get_user(x, ptr) \
140 const void __user *__p = (ptr); \
142 access_ok(__p, sizeof(*ptr)) ? \
143 __get_user((x), (__typeof__(*(ptr)) __user *)__p) :\
144 ((x) = (__typeof__(*(ptr)))0,-EFAULT); \
147 #ifndef __get_user_fn
148 static inline int __get_user_fn(size_t size
, const void __user
*ptr
, void *x
)
150 return unlikely(raw_copy_from_user(x
, ptr
, size
)) ? -EFAULT
: 0;
153 #define __get_user_fn(sz, u, k) __get_user_fn(sz, u, k)
157 extern int __get_user_bad(void) __attribute__((noreturn
));
160 * Copy a null terminated string from userspace.
162 #ifndef __strncpy_from_user
164 __strncpy_from_user(char *dst
, const char __user
*src
, long count
)
167 strncpy(dst
, (const char __force
*)src
, count
);
168 for (tmp
= dst
; *tmp
&& count
> 0; tmp
++, count
--)
175 strncpy_from_user(char *dst
, const char __user
*src
, long count
)
177 if (!access_ok(src
, 1))
179 return __strncpy_from_user(dst
, src
, count
);
183 * Return the size of a string (including the ending 0)
185 * Return 0 on exception, a value greater than N if too long
187 #ifndef __strnlen_user
188 #define __strnlen_user(s, n) (strnlen((s), (n)) + 1)
192 * Unlike strnlen, strnlen_user includes the nul terminator in
193 * its returned count. Callers should check for a returned value
194 * greater than N as an indication the string is too long.
196 static inline long strnlen_user(const char __user
*src
, long n
)
198 if (!access_ok(src
, 1))
200 return __strnlen_user(src
, n
);
207 static inline __must_check
unsigned long
208 __clear_user(void __user
*to
, unsigned long n
)
210 memset((void __force
*)to
, 0, n
);
215 static inline __must_check
unsigned long
216 clear_user(void __user
*to
, unsigned long n
)
219 if (!access_ok(to
, n
))
222 return __clear_user(to
, n
);
225 #include <asm/extable.h>
227 #endif /* __ASM_GENERIC_UACCESS_H */