1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_UACCESS_64_H
3 #define _ASM_X86_UACCESS_64_H
6 * User space memory access functions
8 #include <linux/compiler.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 static __always_inline __must_check unsigned long
50 copy_to_user_mcsafe(void *to, const void *from, unsigned len)
56 * Note, __memcpy_mcsafe() is explicitly used since it can
57 * handle exceptions / faults. memcpy_mcsafe() may fall back to
58 * memcpy() which lacks this handling.
60 ret = __memcpy_mcsafe(to, from, len);
65 static __always_inline __must_check unsigned long
66 raw_copy_from_user(void *dst, const void __user *src, unsigned long size)
70 if (!__builtin_constant_p(size))
71 return copy_user_generic(dst, (__force void *)src, size);
74 __uaccess_begin_nospec();
75 __get_user_asm_nozero(*(u8 *)dst, (u8 __user *)src,
76 ret, "b", "b", "=q", 1);
80 __uaccess_begin_nospec();
81 __get_user_asm_nozero(*(u16 *)dst, (u16 __user *)src,
82 ret, "w", "w", "=r", 2);
86 __uaccess_begin_nospec();
87 __get_user_asm_nozero(*(u32 *)dst, (u32 __user *)src,
88 ret, "l", "k", "=r", 4);
92 __uaccess_begin_nospec();
93 __get_user_asm_nozero(*(u64 *)dst, (u64 __user *)src,
94 ret, "q", "", "=r", 8);
98 __uaccess_begin_nospec();
99 __get_user_asm_nozero(*(u64 *)dst, (u64 __user *)src,
100 ret, "q", "", "=r", 10);
102 __get_user_asm_nozero(*(u16 *)(8 + (char *)dst),
103 (u16 __user *)(8 + (char __user *)src),
104 ret, "w", "w", "=r", 2);
108 __uaccess_begin_nospec();
109 __get_user_asm_nozero(*(u64 *)dst, (u64 __user *)src,
110 ret, "q", "", "=r", 16);
112 __get_user_asm_nozero(*(u64 *)(8 + (char *)dst),
113 (u64 __user *)(8 + (char __user *)src),
114 ret, "q", "", "=r", 8);
118 return copy_user_generic(dst, (__force void *)src, size);
122 static __always_inline __must_check unsigned long
123 raw_copy_to_user(void __user *dst, const void *src, unsigned long size)
127 if (!__builtin_constant_p(size))
128 return copy_user_generic((__force void *)dst, src, size);
132 __put_user_asm(*(u8 *)src, (u8 __user *)dst,
133 ret, "b", "b", "iq", 1);
138 __put_user_asm(*(u16 *)src, (u16 __user *)dst,
139 ret, "w", "w", "ir", 2);
144 __put_user_asm(*(u32 *)src, (u32 __user *)dst,
145 ret, "l", "k", "ir", 4);
150 __put_user_asm(*(u64 *)src, (u64 __user *)dst,
151 ret, "q", "", "er", 8);
156 __put_user_asm(*(u64 *)src, (u64 __user *)dst,
157 ret, "q", "", "er", 10);
160 __put_user_asm(4[(u16 *)src], 4 + (u16 __user *)dst,
161 ret, "w", "w", "ir", 2);
167 __put_user_asm(*(u64 *)src, (u64 __user *)dst,
168 ret, "q", "", "er", 16);
171 __put_user_asm(1[(u64 *)src], 1 + (u64 __user *)dst,
172 ret, "q", "", "er", 8);
177 return copy_user_generic((__force void *)dst, src, size);
181 static __always_inline __must_check
182 unsigned long raw_copy_in_user(void __user *dst, const void __user *src, unsigned long size)
184 return copy_user_generic((__force void *)dst,
185 (__force void *)src, size);
188 extern long __copy_user_nocache(void *dst, const void __user *src,
189 unsigned size, int zerorest);
191 extern long __copy_user_flushcache(void *dst, const void __user *src, unsigned size);
192 extern void memcpy_page_flushcache(char *to, struct page *page, size_t offset,
196 __copy_from_user_inatomic_nocache(void *dst, const void __user *src,
199 kasan_check_write(dst, size);
200 return __copy_user_nocache(dst, src, size, 0);
204 __copy_from_user_flushcache(void *dst, const void __user *src, unsigned size)
206 kasan_check_write(dst, size);
207 return __copy_user_flushcache(dst, src, size);
211 mcsafe_handle_tail(char *to, char *from, unsigned len);
213 #endif /* _ASM_X86_UACCESS_64_H */