1 #ifndef _ASM_X86_UACCESS_64_H
2 #define _ASM_X86_UACCESS_64_H
5 * User space memory access functions
7 #include <linux/compiler.h>
8 #include <linux/errno.h>
9 #include <linux/lockdep.h>
10 #include <linux/kasan-checks.h>
11 #include <asm/alternative.h>
12 #include <asm/cpufeatures.h>
16 * Copy To/From Userspace
19 /* Handles exceptions in both to and from, but doesn't do access_ok */
20 __must_check unsigned long
21 copy_user_enhanced_fast_string(void *to, const void *from, unsigned len);
22 __must_check unsigned long
23 copy_user_generic_string(void *to, const void *from, unsigned len);
24 __must_check unsigned long
25 copy_user_generic_unrolled(void *to, const void *from, unsigned len);
27 static __always_inline __must_check unsigned long
28 copy_user_generic(void *to, const void *from, unsigned len)
33 * If CPU has ERMS feature, use copy_user_enhanced_fast_string.
34 * Otherwise, if CPU has rep_good feature, use copy_user_generic_string.
35 * Otherwise, use copy_user_generic_unrolled.
37 alternative_call_2(copy_user_generic_unrolled,
38 copy_user_generic_string,
40 copy_user_enhanced_fast_string,
42 ASM_OUTPUT2("=a" (ret), "=D" (to), "=S" (from),
44 "1" (to), "2" (from), "3" (len)
45 : "memory", "rcx", "r8", "r9", "r10", "r11");
49 __must_check unsigned long
50 copy_in_user(void __user *to, const void __user *from, unsigned len);
52 static __always_inline __must_check
53 int __copy_from_user_nocheck(void *dst, const void __user *src, unsigned size)
57 if (!__builtin_constant_p(size))
58 return copy_user_generic(dst, (__force void *)src, size);
62 __get_user_asm(*(u8 *)dst, (u8 __user *)src,
63 ret, "b", "b", "=q", 1);
68 __get_user_asm(*(u16 *)dst, (u16 __user *)src,
69 ret, "w", "w", "=r", 2);
74 __get_user_asm(*(u32 *)dst, (u32 __user *)src,
75 ret, "l", "k", "=r", 4);
80 __get_user_asm(*(u64 *)dst, (u64 __user *)src,
81 ret, "q", "", "=r", 8);
86 __get_user_asm(*(u64 *)dst, (u64 __user *)src,
87 ret, "q", "", "=r", 10);
89 __get_user_asm(*(u16 *)(8 + (char *)dst),
90 (u16 __user *)(8 + (char __user *)src),
91 ret, "w", "w", "=r", 2);
96 __get_user_asm(*(u64 *)dst, (u64 __user *)src,
97 ret, "q", "", "=r", 16);
99 __get_user_asm(*(u64 *)(8 + (char *)dst),
100 (u64 __user *)(8 + (char __user *)src),
101 ret, "q", "", "=r", 8);
105 return copy_user_generic(dst, (__force void *)src, size);
109 static __always_inline __must_check
110 int __copy_from_user(void *dst, const void __user *src, unsigned size)
113 kasan_check_write(dst, size);
114 return __copy_from_user_nocheck(dst, src, size);
117 static __always_inline __must_check
118 int __copy_to_user_nocheck(void __user *dst, const void *src, unsigned size)
122 if (!__builtin_constant_p(size))
123 return copy_user_generic((__force void *)dst, src, size);
127 __put_user_asm(*(u8 *)src, (u8 __user *)dst,
128 ret, "b", "b", "iq", 1);
133 __put_user_asm(*(u16 *)src, (u16 __user *)dst,
134 ret, "w", "w", "ir", 2);
139 __put_user_asm(*(u32 *)src, (u32 __user *)dst,
140 ret, "l", "k", "ir", 4);
145 __put_user_asm(*(u64 *)src, (u64 __user *)dst,
146 ret, "q", "", "er", 8);
151 __put_user_asm(*(u64 *)src, (u64 __user *)dst,
152 ret, "q", "", "er", 10);
155 __put_user_asm(4[(u16 *)src], 4 + (u16 __user *)dst,
156 ret, "w", "w", "ir", 2);
162 __put_user_asm(*(u64 *)src, (u64 __user *)dst,
163 ret, "q", "", "er", 16);
166 __put_user_asm(1[(u64 *)src], 1 + (u64 __user *)dst,
167 ret, "q", "", "er", 8);
172 return copy_user_generic((__force void *)dst, src, size);
176 static __always_inline __must_check
177 int __copy_to_user(void __user *dst, const void *src, unsigned size)
180 kasan_check_read(src, size);
181 return __copy_to_user_nocheck(dst, src, size);
184 static __always_inline __must_check
185 int __copy_in_user(void __user *dst, const void __user *src, unsigned size)
190 if (!__builtin_constant_p(size))
191 return copy_user_generic((__force void *)dst,
192 (__force void *)src, size);
197 __get_user_asm(tmp, (u8 __user *)src,
198 ret, "b", "b", "=q", 1);
200 __put_user_asm(tmp, (u8 __user *)dst,
201 ret, "b", "b", "iq", 1);
208 __get_user_asm(tmp, (u16 __user *)src,
209 ret, "w", "w", "=r", 2);
211 __put_user_asm(tmp, (u16 __user *)dst,
212 ret, "w", "w", "ir", 2);
220 __get_user_asm(tmp, (u32 __user *)src,
221 ret, "l", "k", "=r", 4);
223 __put_user_asm(tmp, (u32 __user *)dst,
224 ret, "l", "k", "ir", 4);
231 __get_user_asm(tmp, (u64 __user *)src,
232 ret, "q", "", "=r", 8);
234 __put_user_asm(tmp, (u64 __user *)dst,
235 ret, "q", "", "er", 8);
240 return copy_user_generic((__force void *)dst,
241 (__force void *)src, size);
245 static __must_check __always_inline int
246 __copy_from_user_inatomic(void *dst, const void __user *src, unsigned size)
248 kasan_check_write(dst, size);
249 return __copy_from_user_nocheck(dst, src, size);
252 static __must_check __always_inline int
253 __copy_to_user_inatomic(void __user *dst, const void *src, unsigned size)
255 kasan_check_read(src, size);
256 return __copy_to_user_nocheck(dst, src, size);
259 extern long __copy_user_nocache(void *dst, const void __user *src,
260 unsigned size, int zerorest);
263 __copy_from_user_nocache(void *dst, const void __user *src, unsigned size)
266 kasan_check_write(dst, size);
267 return __copy_user_nocache(dst, src, size, 1);
271 __copy_from_user_inatomic_nocache(void *dst, const void __user *src,
274 kasan_check_write(dst, size);
275 return __copy_user_nocache(dst, src, size, 0);
279 copy_user_handle_tail(char *to, char *from, unsigned len);
281 #endif /* _ASM_X86_UACCESS_64_H */