blob: 307698688fa1cfde037e9d9f96426269b7cd61e9 [file] [log] [blame]
H. Peter Anvin1965aae2008-10-22 22:26:29 -07001#ifndef _ASM_X86_UACCESS_64_H
2#define _ASM_X86_UACCESS_64_H
Linus Torvalds1da177e2005-04-16 15:20:36 -07003
4/*
5 * User space memory access functions
6 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/compiler.h>
8#include <linux/errno.h>
Nick Piggin16dbc6c2008-10-02 14:50:12 -07009#include <linux/lockdep.h>
Jan Beulich1b1d9252009-12-18 16:12:56 +000010#include <asm/alternative.h>
Borislav Petkovcd4d09e2016-01-26 22:12:04 +010011#include <asm/cpufeatures.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <asm/page.h>
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014/*
15 * Copy To/From Userspace
16 */
17
18/* Handles exceptions in both to and from, but doesn't do access_ok */
Andi Kleen95912002006-09-26 10:52:39 +020019__must_check unsigned long
Fenghua Yu954e4822012-05-24 18:19:45 -070020copy_user_enhanced_fast_string(void *to, const void *from, unsigned len);
21__must_check unsigned long
Jan Beulich1b1d9252009-12-18 16:12:56 +000022copy_user_generic_string(void *to, const void *from, unsigned len);
23__must_check unsigned long
24copy_user_generic_unrolled(void *to, const void *from, unsigned len);
25
26static __always_inline __must_check unsigned long
27copy_user_generic(void *to, const void *from, unsigned len)
28{
29 unsigned ret;
30
Fenghua Yu954e4822012-05-24 18:19:45 -070031 /*
32 * If CPU has ERMS feature, use copy_user_enhanced_fast_string.
33 * Otherwise, if CPU has rep_good feature, use copy_user_generic_string.
34 * Otherwise, use copy_user_generic_unrolled.
35 */
36 alternative_call_2(copy_user_generic_unrolled,
Jan Beulich1b1d9252009-12-18 16:12:56 +000037 copy_user_generic_string,
38 X86_FEATURE_REP_GOOD,
Fenghua Yu954e4822012-05-24 18:19:45 -070039 copy_user_enhanced_fast_string,
40 X86_FEATURE_ERMS,
Jan Beulich1b1d9252009-12-18 16:12:56 +000041 ASM_OUTPUT2("=a" (ret), "=D" (to), "=S" (from),
42 "=d" (len)),
43 "1" (to), "2" (from), "3" (len)
44 : "memory", "rcx", "r8", "r9", "r10", "r11");
45 return ret;
46}
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Andi Kleen95912002006-09-26 10:52:39 +020048__must_check unsigned long
Andi Kleen95912002006-09-26 10:52:39 +020049copy_in_user(void __user *to, const void __user *from, unsigned len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Frederic Weisbecker3c93ca02009-11-16 15:42:18 +010051static __always_inline __must_check
Andi Kleenff47ab4f2013-08-16 14:17:19 -070052int __copy_from_user_nocheck(void *dst, const void __user *src, unsigned size)
Joe Perchesb8963132008-03-23 01:03:49 -070053{
Andi Kleen383d0792006-09-26 10:52:40 +020054 int ret = 0;
Ingo Molnard1a76182008-10-28 16:54:49 +010055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 if (!__builtin_constant_p(size))
Joe Perchesb8963132008-03-23 01:03:49 -070057 return copy_user_generic(dst, (__force void *)src, size);
58 switch (size) {
Linus Torvalds11f1a4b2015-12-17 09:45:09 -080059 case 1:
60 __uaccess_begin();
61 __get_user_asm(*(u8 *)dst, (u8 __user *)src,
Joe Perchesb8963132008-03-23 01:03:49 -070062 ret, "b", "b", "=q", 1);
Linus Torvalds11f1a4b2015-12-17 09:45:09 -080063 __uaccess_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 return ret;
Linus Torvalds11f1a4b2015-12-17 09:45:09 -080065 case 2:
66 __uaccess_begin();
67 __get_user_asm(*(u16 *)dst, (u16 __user *)src,
Joe Perchesb8963132008-03-23 01:03:49 -070068 ret, "w", "w", "=r", 2);
Linus Torvalds11f1a4b2015-12-17 09:45:09 -080069 __uaccess_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 return ret;
Linus Torvalds11f1a4b2015-12-17 09:45:09 -080071 case 4:
72 __uaccess_begin();
73 __get_user_asm(*(u32 *)dst, (u32 __user *)src,
Joe Perchesb8963132008-03-23 01:03:49 -070074 ret, "l", "k", "=r", 4);
Linus Torvalds11f1a4b2015-12-17 09:45:09 -080075 __uaccess_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 return ret;
Linus Torvalds11f1a4b2015-12-17 09:45:09 -080077 case 8:
78 __uaccess_begin();
79 __get_user_asm(*(u64 *)dst, (u64 __user *)src,
Joe Perchesb8963132008-03-23 01:03:49 -070080 ret, "q", "", "=r", 8);
Linus Torvalds11f1a4b2015-12-17 09:45:09 -080081 __uaccess_end();
Joe Perchesb8963132008-03-23 01:03:49 -070082 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 case 10:
Linus Torvalds11f1a4b2015-12-17 09:45:09 -080084 __uaccess_begin();
Joe Perchesb8963132008-03-23 01:03:49 -070085 __get_user_asm(*(u64 *)dst, (u64 __user *)src,
Hiroshi Shimamoto20a4a232008-11-13 18:06:04 -080086 ret, "q", "", "=r", 10);
Linus Torvalds11f1a4b2015-12-17 09:45:09 -080087 if (likely(!ret))
88 __get_user_asm(*(u16 *)(8 + (char *)dst),
89 (u16 __user *)(8 + (char __user *)src),
90 ret, "w", "w", "=r", 2);
91 __uaccess_end();
Joe Perchesb8963132008-03-23 01:03:49 -070092 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 case 16:
Linus Torvalds11f1a4b2015-12-17 09:45:09 -080094 __uaccess_begin();
Joe Perchesb8963132008-03-23 01:03:49 -070095 __get_user_asm(*(u64 *)dst, (u64 __user *)src,
96 ret, "q", "", "=r", 16);
Linus Torvalds11f1a4b2015-12-17 09:45:09 -080097 if (likely(!ret))
98 __get_user_asm(*(u64 *)(8 + (char *)dst),
99 (u64 __user *)(8 + (char __user *)src),
100 ret, "q", "", "=r", 8);
101 __uaccess_end();
Joe Perchesb8963132008-03-23 01:03:49 -0700102 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 default:
Joe Perchesb8963132008-03-23 01:03:49 -0700104 return copy_user_generic(dst, (__force void *)src, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
Joe Perchesb8963132008-03-23 01:03:49 -0700106}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Andi Kleen95912002006-09-26 10:52:39 +0200108static __always_inline __must_check
Andi Kleenff47ab4f2013-08-16 14:17:19 -0700109int __copy_from_user(void *dst, const void __user *src, unsigned size)
110{
111 might_fault();
112 return __copy_from_user_nocheck(dst, src, size);
113}
114
115static __always_inline __must_check
116int __copy_to_user_nocheck(void __user *dst, const void *src, unsigned size)
Joe Perchesb8963132008-03-23 01:03:49 -0700117{
Andi Kleen383d0792006-09-26 10:52:40 +0200118 int ret = 0;
Ingo Molnard1a76182008-10-28 16:54:49 +0100119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 if (!__builtin_constant_p(size))
Joe Perchesb8963132008-03-23 01:03:49 -0700121 return copy_user_generic((__force void *)dst, src, size);
122 switch (size) {
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800123 case 1:
124 __uaccess_begin();
125 __put_user_asm(*(u8 *)src, (u8 __user *)dst,
Joe Perchesb8963132008-03-23 01:03:49 -0700126 ret, "b", "b", "iq", 1);
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800127 __uaccess_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 return ret;
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800129 case 2:
130 __uaccess_begin();
131 __put_user_asm(*(u16 *)src, (u16 __user *)dst,
Joe Perchesb8963132008-03-23 01:03:49 -0700132 ret, "w", "w", "ir", 2);
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800133 __uaccess_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return ret;
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800135 case 4:
136 __uaccess_begin();
137 __put_user_asm(*(u32 *)src, (u32 __user *)dst,
Joe Perchesb8963132008-03-23 01:03:49 -0700138 ret, "l", "k", "ir", 4);
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800139 __uaccess_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 return ret;
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800141 case 8:
142 __uaccess_begin();
143 __put_user_asm(*(u64 *)src, (u64 __user *)dst,
Uros Bizjak155b7352009-07-19 18:06:35 +0200144 ret, "q", "", "er", 8);
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800145 __uaccess_end();
Joe Perchesb8963132008-03-23 01:03:49 -0700146 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 case 10:
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800148 __uaccess_begin();
Joe Perchesb8963132008-03-23 01:03:49 -0700149 __put_user_asm(*(u64 *)src, (u64 __user *)dst,
Uros Bizjak155b7352009-07-19 18:06:35 +0200150 ret, "q", "", "er", 10);
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800151 if (likely(!ret)) {
152 asm("":::"memory");
153 __put_user_asm(4[(u16 *)src], 4 + (u16 __user *)dst,
154 ret, "w", "w", "ir", 2);
155 }
156 __uaccess_end();
Joe Perchesb8963132008-03-23 01:03:49 -0700157 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 case 16:
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800159 __uaccess_begin();
Joe Perchesb8963132008-03-23 01:03:49 -0700160 __put_user_asm(*(u64 *)src, (u64 __user *)dst,
Uros Bizjak155b7352009-07-19 18:06:35 +0200161 ret, "q", "", "er", 16);
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800162 if (likely(!ret)) {
163 asm("":::"memory");
164 __put_user_asm(1[(u64 *)src], 1 + (u64 __user *)dst,
165 ret, "q", "", "er", 8);
166 }
167 __uaccess_end();
Joe Perchesb8963132008-03-23 01:03:49 -0700168 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 default:
Joe Perchesb8963132008-03-23 01:03:49 -0700170 return copy_user_generic((__force void *)dst, src, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
Joe Perchesb8963132008-03-23 01:03:49 -0700172}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Andi Kleen95912002006-09-26 10:52:39 +0200174static __always_inline __must_check
Andi Kleenff47ab4f2013-08-16 14:17:19 -0700175int __copy_to_user(void __user *dst, const void *src, unsigned size)
176{
177 might_fault();
178 return __copy_to_user_nocheck(dst, src, size);
179}
180
181static __always_inline __must_check
Andi Kleen95912002006-09-26 10:52:39 +0200182int __copy_in_user(void __user *dst, const void __user *src, unsigned size)
Joe Perchesb8963132008-03-23 01:03:49 -0700183{
Andi Kleen383d0792006-09-26 10:52:40 +0200184 int ret = 0;
Ingo Molnard1a76182008-10-28 16:54:49 +0100185
186 might_fault();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if (!__builtin_constant_p(size))
Joe Perchesb8963132008-03-23 01:03:49 -0700188 return copy_user_generic((__force void *)dst,
189 (__force void *)src, size);
190 switch (size) {
191 case 1: {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 u8 tmp;
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800193 __uaccess_begin();
Joe Perchesb8963132008-03-23 01:03:49 -0700194 __get_user_asm(tmp, (u8 __user *)src,
195 ret, "b", "b", "=q", 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 if (likely(!ret))
Joe Perchesb8963132008-03-23 01:03:49 -0700197 __put_user_asm(tmp, (u8 __user *)dst,
198 ret, "b", "b", "iq", 1);
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800199 __uaccess_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return ret;
201 }
Joe Perchesb8963132008-03-23 01:03:49 -0700202 case 2: {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 u16 tmp;
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800204 __uaccess_begin();
Joe Perchesb8963132008-03-23 01:03:49 -0700205 __get_user_asm(tmp, (u16 __user *)src,
206 ret, "w", "w", "=r", 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if (likely(!ret))
Joe Perchesb8963132008-03-23 01:03:49 -0700208 __put_user_asm(tmp, (u16 __user *)dst,
209 ret, "w", "w", "ir", 2);
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800210 __uaccess_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return ret;
212 }
213
Joe Perchesb8963132008-03-23 01:03:49 -0700214 case 4: {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 u32 tmp;
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800216 __uaccess_begin();
Joe Perchesb8963132008-03-23 01:03:49 -0700217 __get_user_asm(tmp, (u32 __user *)src,
218 ret, "l", "k", "=r", 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 if (likely(!ret))
Joe Perchesb8963132008-03-23 01:03:49 -0700220 __put_user_asm(tmp, (u32 __user *)dst,
221 ret, "l", "k", "ir", 4);
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800222 __uaccess_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return ret;
224 }
Joe Perchesb8963132008-03-23 01:03:49 -0700225 case 8: {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 u64 tmp;
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800227 __uaccess_begin();
Joe Perchesb8963132008-03-23 01:03:49 -0700228 __get_user_asm(tmp, (u64 __user *)src,
229 ret, "q", "", "=r", 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 if (likely(!ret))
Joe Perchesb8963132008-03-23 01:03:49 -0700231 __put_user_asm(tmp, (u64 __user *)dst,
Uros Bizjak155b7352009-07-19 18:06:35 +0200232 ret, "q", "", "er", 8);
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800233 __uaccess_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return ret;
235 }
236 default:
Joe Perchesb8963132008-03-23 01:03:49 -0700237 return copy_user_generic((__force void *)dst,
238 (__force void *)src, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
Joe Perchesb8963132008-03-23 01:03:49 -0700240}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Jan Beulich14722482009-11-13 11:56:24 +0000242static __must_check __always_inline int
243__copy_from_user_inatomic(void *dst, const void __user *src, unsigned size)
244{
Steven Rostedtdf90ca92014-01-03 16:45:00 -0500245 return __copy_from_user_nocheck(dst, src, size);
Jan Beulich14722482009-11-13 11:56:24 +0000246}
Andi Kleenb8858082006-09-30 01:47:55 +0200247
248static __must_check __always_inline int
249__copy_to_user_inatomic(void __user *dst, const void *src, unsigned size)
250{
Steven Rostedtdf90ca92014-01-03 16:45:00 -0500251 return __copy_to_user_nocheck(dst, src, size);
Andi Kleenb8858082006-09-30 01:47:55 +0200252}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Joe Perchesb8963132008-03-23 01:03:49 -0700254extern long __copy_user_nocache(void *dst, const void __user *src,
255 unsigned size, int zerorest);
Andi Kleen0812a572007-02-13 13:26:19 +0100256
Ingo Molnarf1800532009-03-02 11:00:57 +0100257static inline int
258__copy_from_user_nocache(void *dst, const void __user *src, unsigned size)
Andi Kleen0812a572007-02-13 13:26:19 +0100259{
Michael S. Tsirkin016be2e2013-05-26 17:31:55 +0300260 might_fault();
Ingo Molnarf1800532009-03-02 11:00:57 +0100261 return __copy_user_nocache(dst, src, size, 1);
Andi Kleen0812a572007-02-13 13:26:19 +0100262}
263
Ingo Molnarf1800532009-03-02 11:00:57 +0100264static inline int
265__copy_from_user_inatomic_nocache(void *dst, const void __user *src,
266 unsigned size)
Andi Kleen0812a572007-02-13 13:26:19 +0100267{
Ingo Molnarf1800532009-03-02 11:00:57 +0100268 return __copy_user_nocache(dst, src, size, 0);
Andi Kleen0812a572007-02-13 13:26:19 +0100269}
270
Vitaly Mayatskikh11295852008-07-02 15:48:21 +0200271unsigned long
Linus Torvaldscae2a172015-04-06 10:26:17 -0700272copy_user_handle_tail(char *to, char *from, unsigned len);
Vitaly Mayatskikh11295852008-07-02 15:48:21 +0200273
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700274#endif /* _ASM_X86_UACCESS_64_H */