blob: 2177c7551ff7774e0760fd7105a132fe47e16f56 [file] [log] [blame]
H. Peter Anvin1965aae2008-10-22 22:26:29 -07001#ifndef _ASM_X86_UACCESS_H
2#define _ASM_X86_UACCESS_H
Glauber Costaca233862008-06-13 14:39:25 -03003/*
4 * User space memory access functions
5 */
6#include <linux/errno.h>
7#include <linux/compiler.h>
Andrey Ryabinin1771c6e2016-05-20 16:59:31 -07008#include <linux/kasan-checks.h>
Glauber Costaca233862008-06-13 14:39:25 -03009#include <linux/thread_info.h>
Glauber Costaca233862008-06-13 14:39:25 -030010#include <linux/string.h>
11#include <asm/asm.h>
12#include <asm/page.h>
H. Peter Anvin63bcff22012-09-21 12:43:12 -070013#include <asm/smap.h>
Al Viro45caf472016-09-05 11:32:44 -040014#include <asm/extable.h>
Glauber Costaca233862008-06-13 14:39:25 -030015
16#define VERIFY_READ 0
17#define VERIFY_WRITE 1
18
19/*
20 * The fs value determines whether argument validity checking should be
21 * performed or not. If get_fs() == USER_DS, checking is performed, with
22 * get_fs() == KERNEL_DS, checking is bypassed.
23 *
24 * For historical reasons, these macros are grossly misnamed.
25 */
26
27#define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
28
29#define KERNEL_DS MAKE_MM_SEG(-1UL)
Linus Torvalds9063c612009-06-20 15:40:00 -070030#define USER_DS MAKE_MM_SEG(TASK_SIZE_MAX)
Glauber Costaca233862008-06-13 14:39:25 -030031
32#define get_ds() (KERNEL_DS)
Andy Lutomirski13d4ea02016-07-14 13:22:57 -070033#define get_fs() (current->thread.addr_limit)
34#define set_fs(x) (current->thread.addr_limit = (x))
Glauber Costaca233862008-06-13 14:39:25 -030035
36#define segment_eq(a, b) ((a).seg == (b).seg)
37
Andy Lutomirski13d4ea02016-07-14 13:22:57 -070038#define user_addr_max() (current->thread.addr_limit.seg)
Arun Sharmabc6ca7b2012-04-20 15:41:35 -070039#define __addr_ok(addr) \
40 ((unsigned long __force)(addr) < user_addr_max())
Glauber Costa002ca162008-06-25 11:08:51 -030041
Glauber Costaca233862008-06-13 14:39:25 -030042/*
43 * Test whether a block of memory is a valid user space address.
44 * Returns 0 if the range is valid, nonzero otherwise.
Glauber Costaca233862008-06-13 14:39:25 -030045 */
H. Peter Anvina7405762013-12-27 16:52:47 -080046static inline bool __chk_range_not_ok(unsigned long addr, unsigned long size, unsigned long limit)
Linus Torvaldsc5fe5d82013-12-27 15:30:58 -080047{
48 /*
49 * If we have used "sizeof()" for the size,
50 * we know it won't overflow the limit (but
51 * it might overflow the 'addr', so it's
52 * important to subtract the size from the
53 * limit, not add it to the address).
54 */
55 if (__builtin_constant_p(size))
Andy Lutomirski7e0f51c2015-10-05 17:47:50 -070056 return unlikely(addr > limit - size);
Linus Torvaldsc5fe5d82013-12-27 15:30:58 -080057
58 /* Arbitrary sizes? Be careful about overflow */
59 addr += size;
Andy Lutomirski7e0f51c2015-10-05 17:47:50 -070060 if (unlikely(addr < size))
H. Peter Anvina7405762013-12-27 16:52:47 -080061 return true;
Andy Lutomirski7e0f51c2015-10-05 17:47:50 -070062 return unlikely(addr > limit);
Linus Torvaldsc5fe5d82013-12-27 15:30:58 -080063}
Glauber Costaca233862008-06-13 14:39:25 -030064
Arun Sharmabc6ca7b2012-04-20 15:41:35 -070065#define __range_not_ok(addr, size, limit) \
Glauber Costaca233862008-06-13 14:39:25 -030066({ \
Glauber Costaca233862008-06-13 14:39:25 -030067 __chk_user_ptr(addr); \
Linus Torvaldsc5fe5d82013-12-27 15:30:58 -080068 __chk_range_not_ok((unsigned long __force)(addr), size, limit); \
Glauber Costaca233862008-06-13 14:39:25 -030069})
70
Peter Zijlstra2715f682016-11-22 10:57:15 +010071#ifdef CONFIG_DEBUG_ATOMIC_SLEEP
72# define WARN_ON_IN_IRQ() WARN_ON_ONCE(!in_task())
73#else
74# define WARN_ON_IN_IRQ()
75#endif
76
Glauber Costaca233862008-06-13 14:39:25 -030077/**
78 * access_ok: - Checks if a user space pointer is valid
79 * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
80 * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
81 * to write to a block, it is always safe to read from it.
82 * @addr: User space pointer to start of block to check
83 * @size: Size of block to check
84 *
David Hildenbrandb3c395e2015-05-11 17:52:08 +020085 * Context: User context only. This function may sleep if pagefaults are
86 * enabled.
Glauber Costaca233862008-06-13 14:39:25 -030087 *
88 * Checks if a pointer to a block of memory in user space is valid.
89 *
90 * Returns true (nonzero) if the memory block may be valid, false (zero)
91 * if it is definitely invalid.
92 *
93 * Note that, depending on architecture, this function probably just
94 * checks that the pointer is in the user space range - after calling
95 * this function, memory access functions may still return -EFAULT.
96 */
Peter Zijlstra2715f682016-11-22 10:57:15 +010097#define access_ok(type, addr, size) \
98({ \
99 WARN_ON_IN_IRQ(); \
100 likely(!__range_not_ok(addr, size, user_addr_max())); \
101})
Glauber Costaca233862008-06-13 14:39:25 -0300102
103/*
Glauber Costaca233862008-06-13 14:39:25 -0300104 * These are the main single-value transfer routines. They automatically
105 * use the right size if we just have the right pointer type.
106 *
107 * This gets kind of ugly. We want to return _two_ values in "get_user()"
108 * and yet we don't want to do any pointers, because that is too much
109 * of a performance impact. Thus we have a few rather ugly macros here,
110 * and hide all the ugliness from the user.
111 *
112 * The "__xxx" versions of the user access functions are versions that
113 * do not verify the address space, that must have been done previously
114 * with a separate "access_ok()" call (this is used when we do multiple
115 * accesses to the same area of user memory).
116 */
117
118extern int __get_user_1(void);
119extern int __get_user_2(void);
120extern int __get_user_4(void);
121extern int __get_user_8(void);
122extern int __get_user_bad(void);
123
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800124#define __uaccess_begin() stac()
125#define __uaccess_end() clac()
Dan Williamse06d7bf2018-01-29 17:02:39 -0800126#define __uaccess_begin_nospec() \
127({ \
128 stac(); \
129 barrier_nospec(); \
130})
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800131
H. Peter Anvin3578baa2013-02-12 11:47:31 -0800132/*
133 * This is a type: either unsigned long, if the argument fits into
134 * that type, or otherwise unsigned long long.
135 */
136#define __inttype(x) \
137__typeof__(__builtin_choose_expr(sizeof(x) > sizeof(0UL), 0ULL, 0UL))
Glauber Costa865e5b72008-06-25 11:05:11 -0300138
139/**
140 * get_user: - Get a simple variable from user space.
141 * @x: Variable to store result.
142 * @ptr: Source address, in user space.
143 *
David Hildenbrandb3c395e2015-05-11 17:52:08 +0200144 * Context: User context only. This function may sleep if pagefaults are
145 * enabled.
Glauber Costa865e5b72008-06-25 11:05:11 -0300146 *
147 * This macro copies a single simple variable from user space to kernel
148 * space. It supports simple types like char and int, but not larger
149 * data types like structures or arrays.
150 *
151 * @ptr must have pointer-to-simple-variable type, and the result of
152 * dereferencing @ptr must be assignable to @x without a cast.
153 *
154 * Returns zero on success, or -EFAULT on error.
155 * On error, the variable @x is set to zero.
H. Peter Anvinff52c3b2013-02-12 15:37:02 -0800156 */
157/*
H. Peter Anvin3578baa2013-02-12 11:47:31 -0800158 * Careful: we have to cast the result to the type of the pointer
159 * for sign reasons.
H. Peter Anvinff52c3b2013-02-12 15:37:02 -0800160 *
H. Peter Anvinf69fa9a2013-08-29 13:34:50 -0700161 * The use of _ASM_DX as the register specifier is a bit of a
H. Peter Anvinff52c3b2013-02-12 15:37:02 -0800162 * simplification, as gcc only cares about it as the starting point
163 * and not size: for a 64-bit value it will use %ecx:%edx on 32 bits
164 * (%ecx being the next register in gcc's x86 register sequence), and
165 * %rdx on 64 bits.
H. Peter Anvinf69fa9a2013-08-29 13:34:50 -0700166 *
167 * Clang/LLVM cares about the size of the register, but still wants
168 * the base register for something that ends up being a pair.
Glauber Costa865e5b72008-06-25 11:05:11 -0300169 */
Glauber Costa865e5b72008-06-25 11:05:11 -0300170#define get_user(x, ptr) \
171({ \
172 int __ret_gu; \
Jan-Simon Möllerbdfc0172013-08-29 21:13:05 +0200173 register __inttype(*(ptr)) __val_gu asm("%"_ASM_DX); \
Chris J Argesf05058c2016-01-21 16:49:25 -0600174 register void *__sp asm(_ASM_SP); \
Glauber Costa865e5b72008-06-25 11:05:11 -0300175 __chk_user_ptr(ptr); \
Ingo Molnard1a76182008-10-28 16:54:49 +0100176 might_fault(); \
Chris J Argesf05058c2016-01-21 16:49:25 -0600177 asm volatile("call __get_user_%P4" \
178 : "=a" (__ret_gu), "=r" (__val_gu), "+r" (__sp) \
H. Peter Anvin3578baa2013-02-12 11:47:31 -0800179 : "0" (ptr), "i" (sizeof(*(ptr)))); \
Michael S. Tsirkine182c572014-12-12 01:56:04 +0200180 (x) = (__force __typeof__(*(ptr))) __val_gu; \
Andy Lutomirskia76cf662015-10-05 17:47:49 -0700181 __builtin_expect(__ret_gu, 0); \
Glauber Costa865e5b72008-06-25 11:05:11 -0300182})
183
Glauber Costae30a44f2008-06-25 13:17:43 -0300184#define __put_user_x(size, x, ptr, __ret_pu) \
185 asm volatile("call __put_user_" #size : "=a" (__ret_pu) \
Hiroshi Shimamoto4d5d7832009-01-19 16:34:26 -0800186 : "0" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx")
Glauber Costae30a44f2008-06-25 13:17:43 -0300187
188
189
Glauber Costadc70ddf2008-06-25 11:48:29 -0300190#ifdef CONFIG_X86_32
Hiroshi Shimamoto18114f62009-01-30 18:16:46 -0800191#define __put_user_asm_u64(x, addr, err, errret) \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800192 asm volatile("\n" \
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700193 "1: movl %%eax,0(%2)\n" \
Glauber Costadc70ddf2008-06-25 11:48:29 -0300194 "2: movl %%edx,4(%2)\n" \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800195 "3:" \
Glauber Costadc70ddf2008-06-25 11:48:29 -0300196 ".section .fixup,\"ax\"\n" \
197 "4: movl %3,%0\n" \
198 " jmp 3b\n" \
199 ".previous\n" \
200 _ASM_EXTABLE(1b, 4b) \
201 _ASM_EXTABLE(2b, 4b) \
202 : "=r" (err) \
Hiroshi Shimamoto18114f62009-01-30 18:16:46 -0800203 : "A" (x), "r" (addr), "i" (errret), "0" (err))
Glauber Costae30a44f2008-06-25 13:17:43 -0300204
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800205#define __put_user_asm_ex_u64(x, addr) \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800206 asm volatile("\n" \
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700207 "1: movl %%eax,0(%1)\n" \
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800208 "2: movl %%edx,4(%1)\n" \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800209 "3:" \
H. Peter Anvin535c0c32012-04-20 16:57:35 -0700210 _ASM_EXTABLE_EX(1b, 2b) \
211 _ASM_EXTABLE_EX(2b, 3b) \
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800212 : : "A" (x), "r" (addr))
213
Glauber Costae30a44f2008-06-25 13:17:43 -0300214#define __put_user_x8(x, ptr, __ret_pu) \
215 asm volatile("call __put_user_8" : "=a" (__ret_pu) \
216 : "A" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx")
Glauber Costadc70ddf2008-06-25 11:48:29 -0300217#else
Hiroshi Shimamoto18114f62009-01-30 18:16:46 -0800218#define __put_user_asm_u64(x, ptr, retval, errret) \
H. Peter Anvinebe119c2009-07-20 23:27:39 -0700219 __put_user_asm(x, ptr, retval, "q", "", "er", errret)
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800220#define __put_user_asm_ex_u64(x, addr) \
H. Peter Anvinebe119c2009-07-20 23:27:39 -0700221 __put_user_asm_ex(x, addr, "q", "", "er")
Glauber Costae30a44f2008-06-25 13:17:43 -0300222#define __put_user_x8(x, ptr, __ret_pu) __put_user_x(8, x, ptr, __ret_pu)
Glauber Costadc70ddf2008-06-25 11:48:29 -0300223#endif
224
Glauber Costae30a44f2008-06-25 13:17:43 -0300225extern void __put_user_bad(void);
226
227/*
228 * Strange magic calling convention: pointer in %ecx,
229 * value in %eax(:%edx), return value in %eax. clobbers %rbx
230 */
231extern void __put_user_1(void);
232extern void __put_user_2(void);
233extern void __put_user_4(void);
234extern void __put_user_8(void);
235
Glauber Costae30a44f2008-06-25 13:17:43 -0300236/**
237 * put_user: - Write a simple value into user space.
238 * @x: Value to copy to user space.
239 * @ptr: Destination address, in user space.
240 *
David Hildenbrandb3c395e2015-05-11 17:52:08 +0200241 * Context: User context only. This function may sleep if pagefaults are
242 * enabled.
Glauber Costae30a44f2008-06-25 13:17:43 -0300243 *
244 * This macro copies a single simple value from kernel space to user
245 * space. It supports simple types like char and int, but not larger
246 * data types like structures or arrays.
247 *
248 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
249 * to the result of dereferencing @ptr.
250 *
251 * Returns zero on success, or -EFAULT on error.
252 */
253#define put_user(x, ptr) \
254({ \
255 int __ret_pu; \
256 __typeof__(*(ptr)) __pu_val; \
257 __chk_user_ptr(ptr); \
Ingo Molnard1a76182008-10-28 16:54:49 +0100258 might_fault(); \
Glauber Costae30a44f2008-06-25 13:17:43 -0300259 __pu_val = x; \
260 switch (sizeof(*(ptr))) { \
261 case 1: \
262 __put_user_x(1, __pu_val, ptr, __ret_pu); \
263 break; \
264 case 2: \
265 __put_user_x(2, __pu_val, ptr, __ret_pu); \
266 break; \
267 case 4: \
268 __put_user_x(4, __pu_val, ptr, __ret_pu); \
269 break; \
270 case 8: \
271 __put_user_x8(__pu_val, ptr, __ret_pu); \
272 break; \
273 default: \
274 __put_user_x(X, __pu_val, ptr, __ret_pu); \
275 break; \
276 } \
Andy Lutomirskia76cf662015-10-05 17:47:49 -0700277 __builtin_expect(__ret_pu, 0); \
Glauber Costae30a44f2008-06-25 13:17:43 -0300278})
279
Glauber Costadc70ddf2008-06-25 11:48:29 -0300280#define __put_user_size(x, ptr, size, retval, errret) \
281do { \
282 retval = 0; \
283 __chk_user_ptr(ptr); \
284 switch (size) { \
285 case 1: \
286 __put_user_asm(x, ptr, retval, "b", "b", "iq", errret); \
287 break; \
288 case 2: \
289 __put_user_asm(x, ptr, retval, "w", "w", "ir", errret); \
290 break; \
291 case 4: \
Hiroshi Shimamoto4d5d7832009-01-19 16:34:26 -0800292 __put_user_asm(x, ptr, retval, "l", "k", "ir", errret); \
Glauber Costadc70ddf2008-06-25 11:48:29 -0300293 break; \
294 case 8: \
Andy Lutomirskidd15ae32019-02-22 17:17:04 -0800295 __put_user_asm_u64(x, ptr, retval, errret); \
Glauber Costadc70ddf2008-06-25 11:48:29 -0300296 break; \
297 default: \
298 __put_user_bad(); \
299 } \
300} while (0)
301
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800302/*
303 * This doesn't do __uaccess_begin/end - the exception handling
304 * around it must do that.
305 */
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800306#define __put_user_size_ex(x, ptr, size) \
307do { \
308 __chk_user_ptr(ptr); \
309 switch (size) { \
310 case 1: \
311 __put_user_asm_ex(x, ptr, "b", "b", "iq"); \
312 break; \
313 case 2: \
314 __put_user_asm_ex(x, ptr, "w", "w", "ir"); \
315 break; \
316 case 4: \
317 __put_user_asm_ex(x, ptr, "l", "k", "ir"); \
318 break; \
319 case 8: \
320 __put_user_asm_ex_u64((__typeof__(*ptr))(x), ptr); \
321 break; \
322 default: \
323 __put_user_bad(); \
324 } \
325} while (0)
326
Glauber Costa3f168222008-06-25 12:48:47 -0300327#ifdef CONFIG_X86_32
Benjamin LaHaiseb2f68032016-03-09 15:05:56 -0500328#define __get_user_asm_u64(x, ptr, retval, errret) \
329({ \
330 __typeof__(ptr) __ptr = (ptr); \
Linus Torvaldsae382ca2017-05-21 18:26:54 -0700331 asm volatile("\n" \
Benjamin LaHaiseb2f68032016-03-09 15:05:56 -0500332 "1: movl %2,%%eax\n" \
333 "2: movl %3,%%edx\n" \
Linus Torvaldsae382ca2017-05-21 18:26:54 -0700334 "3:\n" \
Benjamin LaHaiseb2f68032016-03-09 15:05:56 -0500335 ".section .fixup,\"ax\"\n" \
336 "4: mov %4,%0\n" \
337 " xorl %%eax,%%eax\n" \
338 " xorl %%edx,%%edx\n" \
339 " jmp 3b\n" \
340 ".previous\n" \
341 _ASM_EXTABLE(1b, 4b) \
342 _ASM_EXTABLE(2b, 4b) \
Linus Torvaldsae382ca2017-05-21 18:26:54 -0700343 : "=r" (retval), "=&A"(x) \
Benjamin LaHaiseb2f68032016-03-09 15:05:56 -0500344 : "m" (__m(__ptr)), "m" __m(((u32 *)(__ptr)) + 1), \
345 "i" (errret), "0" (retval)); \
346})
347
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800348#define __get_user_asm_ex_u64(x, ptr) (x) = __get_user_bad()
Glauber Costa3f168222008-06-25 12:48:47 -0300349#else
350#define __get_user_asm_u64(x, ptr, retval, errret) \
351 __get_user_asm(x, ptr, retval, "q", "", "=r", errret)
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800352#define __get_user_asm_ex_u64(x, ptr) \
353 __get_user_asm_ex(x, ptr, "q", "", "=r")
Glauber Costa3f168222008-06-25 12:48:47 -0300354#endif
355
356#define __get_user_size(x, ptr, size, retval, errret) \
357do { \
358 retval = 0; \
359 __chk_user_ptr(ptr); \
360 switch (size) { \
361 case 1: \
362 __get_user_asm(x, ptr, retval, "b", "b", "=q", errret); \
363 break; \
364 case 2: \
365 __get_user_asm(x, ptr, retval, "w", "w", "=r", errret); \
366 break; \
367 case 4: \
368 __get_user_asm(x, ptr, retval, "l", "k", "=r", errret); \
369 break; \
370 case 8: \
371 __get_user_asm_u64(x, ptr, retval, errret); \
372 break; \
373 default: \
374 (x) = __get_user_bad(); \
375 } \
376} while (0)
377
378#define __get_user_asm(x, addr, err, itype, rtype, ltype, errret) \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800379 asm volatile("\n" \
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700380 "1: mov"itype" %2,%"rtype"1\n" \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800381 "2:\n" \
Glauber Costa3f168222008-06-25 12:48:47 -0300382 ".section .fixup,\"ax\"\n" \
383 "3: mov %3,%0\n" \
384 " xor"itype" %"rtype"1,%"rtype"1\n" \
385 " jmp 2b\n" \
386 ".previous\n" \
387 _ASM_EXTABLE(1b, 3b) \
388 : "=r" (err), ltype(x) \
389 : "m" (__m(addr)), "i" (errret), "0" (err))
390
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800391/*
392 * This doesn't do __uaccess_begin/end - the exception handling
393 * around it must do that.
394 */
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800395#define __get_user_size_ex(x, ptr, size) \
396do { \
397 __chk_user_ptr(ptr); \
398 switch (size) { \
399 case 1: \
400 __get_user_asm_ex(x, ptr, "b", "b", "=q"); \
401 break; \
402 case 2: \
403 __get_user_asm_ex(x, ptr, "w", "w", "=r"); \
404 break; \
405 case 4: \
406 __get_user_asm_ex(x, ptr, "l", "k", "=r"); \
407 break; \
408 case 8: \
409 __get_user_asm_ex_u64(x, ptr); \
410 break; \
411 default: \
412 (x) = __get_user_bad(); \
413 } \
414} while (0)
415
416#define __get_user_asm_ex(x, addr, itype, rtype, ltype) \
H. Peter Anvin5e883532012-09-21 12:43:15 -0700417 asm volatile("1: mov"itype" %1,%"rtype"0\n" \
418 "2:\n" \
Al Viro1c109fa2016-09-15 02:35:29 +0100419 ".section .fixup,\"ax\"\n" \
420 "3:xor"itype" %"rtype"0,%"rtype"0\n" \
421 " jmp 2b\n" \
422 ".previous\n" \
423 _ASM_EXTABLE_EX(1b, 3b) \
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800424 : ltype(x) : "m" (__m(addr)))
425
Glauber Costadc70ddf2008-06-25 11:48:29 -0300426#define __put_user_nocheck(x, ptr, size) \
427({ \
Hiroshi Shimamoto16855f82008-12-08 19:18:38 -0800428 int __pu_err; \
Andy Lutomirskidd15ae32019-02-22 17:17:04 -0800429 __typeof__(*(ptr)) __pu_val; \
430 __pu_val = x; \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800431 __uaccess_begin(); \
Andy Lutomirskidd15ae32019-02-22 17:17:04 -0800432 __put_user_size(__pu_val, (ptr), (size), __pu_err, -EFAULT);\
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800433 __uaccess_end(); \
Andy Lutomirskia76cf662015-10-05 17:47:49 -0700434 __builtin_expect(__pu_err, 0); \
Glauber Costadc70ddf2008-06-25 11:48:29 -0300435})
436
Glauber Costa3f168222008-06-25 12:48:47 -0300437#define __get_user_nocheck(x, ptr, size) \
438({ \
Hiroshi Shimamoto16855f82008-12-08 19:18:38 -0800439 int __gu_err; \
Benjamin LaHaiseb2f68032016-03-09 15:05:56 -0500440 __inttype(*(ptr)) __gu_val; \
Dan Williams065eae42018-01-29 17:02:49 -0800441 __uaccess_begin_nospec(); \
Glauber Costa3f168222008-06-25 12:48:47 -0300442 __get_user_size(__gu_val, (ptr), (size), __gu_err, -EFAULT); \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800443 __uaccess_end(); \
Glauber Costa3f168222008-06-25 12:48:47 -0300444 (x) = (__force __typeof__(*(ptr)))__gu_val; \
Andy Lutomirskia76cf662015-10-05 17:47:49 -0700445 __builtin_expect(__gu_err, 0); \
Glauber Costa3f168222008-06-25 12:48:47 -0300446})
Glauber Costadc70ddf2008-06-25 11:48:29 -0300447
448/* FIXME: this hack is definitely wrong -AK */
449struct __large_struct { unsigned long buf[100]; };
450#define __m(x) (*(struct __large_struct __user *)(x))
451
452/*
453 * Tell gcc we read from memory instead of writing: this is because
454 * we do not write to any memory gcc knows about, so there are no
455 * aliasing issues.
456 */
457#define __put_user_asm(x, addr, err, itype, rtype, ltype, errret) \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800458 asm volatile("\n" \
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700459 "1: mov"itype" %"rtype"1,%2\n" \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800460 "2:\n" \
Glauber Costadc70ddf2008-06-25 11:48:29 -0300461 ".section .fixup,\"ax\"\n" \
462 "3: mov %3,%0\n" \
463 " jmp 2b\n" \
464 ".previous\n" \
465 _ASM_EXTABLE(1b, 3b) \
466 : "=r"(err) \
467 : ltype(x), "m" (__m(addr)), "i" (errret), "0" (err))
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800468
469#define __put_user_asm_ex(x, addr, itype, rtype, ltype) \
H. Peter Anvin5e883532012-09-21 12:43:15 -0700470 asm volatile("1: mov"itype" %"rtype"0,%1\n" \
471 "2:\n" \
H. Peter Anvin535c0c32012-04-20 16:57:35 -0700472 _ASM_EXTABLE_EX(1b, 2b) \
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800473 : : ltype(x), "m" (__m(addr)))
474
475/*
476 * uaccess_try and catch
477 */
478#define uaccess_try do { \
Andy Lutomirskidfa9a942016-07-14 13:22:56 -0700479 current->thread.uaccess_err = 0; \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800480 __uaccess_begin(); \
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800481 barrier();
482
Dan Williamse06d7bf2018-01-29 17:02:39 -0800483#define uaccess_try_nospec do { \
484 current->thread.uaccess_err = 0; \
485 __uaccess_begin_nospec(); \
486
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800487#define uaccess_catch(err) \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800488 __uaccess_end(); \
Andy Lutomirskidfa9a942016-07-14 13:22:56 -0700489 (err) |= (current->thread.uaccess_err ? -EFAULT : 0); \
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800490} while (0)
491
Glauber Costa8cb834e2008-06-25 14:43:30 -0300492/**
493 * __get_user: - Get a simple variable from user space, with less checking.
494 * @x: Variable to store result.
495 * @ptr: Source address, in user space.
496 *
David Hildenbrandb3c395e2015-05-11 17:52:08 +0200497 * Context: User context only. This function may sleep if pagefaults are
498 * enabled.
Glauber Costa8cb834e2008-06-25 14:43:30 -0300499 *
500 * This macro copies a single simple variable from user space to kernel
501 * space. It supports simple types like char and int, but not larger
502 * data types like structures or arrays.
503 *
504 * @ptr must have pointer-to-simple-variable type, and the result of
505 * dereferencing @ptr must be assignable to @x without a cast.
506 *
507 * Caller must check the pointer with access_ok() before calling this
508 * function.
509 *
510 * Returns zero on success, or -EFAULT on error.
511 * On error, the variable @x is set to zero.
512 */
Glauber Costadc70ddf2008-06-25 11:48:29 -0300513
Glauber Costa8cb834e2008-06-25 14:43:30 -0300514#define __get_user(x, ptr) \
515 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800516
Glauber Costa8cb834e2008-06-25 14:43:30 -0300517/**
518 * __put_user: - Write a simple value into user space, with less checking.
519 * @x: Value to copy to user space.
520 * @ptr: Destination address, in user space.
521 *
David Hildenbrandb3c395e2015-05-11 17:52:08 +0200522 * Context: User context only. This function may sleep if pagefaults are
523 * enabled.
Glauber Costa8cb834e2008-06-25 14:43:30 -0300524 *
525 * This macro copies a single simple value from kernel space to user
526 * space. It supports simple types like char and int, but not larger
527 * data types like structures or arrays.
528 *
529 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
530 * to the result of dereferencing @ptr.
531 *
532 * Caller must check the pointer with access_ok() before calling this
533 * function.
534 *
535 * Returns zero on success, or -EFAULT on error.
536 */
537
538#define __put_user(x, ptr) \
539 __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
540
541#define __get_user_unaligned __get_user
542#define __put_user_unaligned __put_user
Glauber Costa865e5b72008-06-25 11:05:11 -0300543
Glauber Costa8bc7de0c2008-06-25 14:53:41 -0300544/*
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800545 * {get|put}_user_try and catch
546 *
547 * get_user_try {
548 * get_user_ex(...);
549 * } get_user_catch(err)
550 */
Dan Williams065eae42018-01-29 17:02:49 -0800551#define get_user_try uaccess_try_nospec
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800552#define get_user_catch(err) uaccess_catch(err)
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800553
554#define get_user_ex(x, ptr) do { \
555 unsigned long __gue_val; \
556 __get_user_size_ex((__gue_val), (ptr), (sizeof(*(ptr)))); \
557 (x) = (__force __typeof__(*(ptr)))__gue_val; \
558} while (0)
559
Hiroshi Shimamoto019a1362009-01-29 11:49:18 -0800560#define put_user_try uaccess_try
561#define put_user_catch(err) uaccess_catch(err)
562
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800563#define put_user_ex(x, ptr) \
564 __put_user_size_ex((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
565
Robert Richter1ac2e6c2011-06-07 11:49:55 +0200566extern unsigned long
567copy_from_user_nmi(void *to, const void __user *from, unsigned long n);
Linus Torvalds92ae03f2012-04-06 14:32:32 -0700568extern __must_check long
569strncpy_from_user(char *dst, const char __user *src, long count);
Robert Richter1ac2e6c2011-06-07 11:49:55 +0200570
Linus Torvalds5723aa92012-05-26 11:09:53 -0700571extern __must_check long strlen_user(const char __user *str);
572extern __must_check long strnlen_user(const char __user *str, long n);
573
H. Peter Anvina0528582012-09-21 12:43:11 -0700574unsigned long __must_check clear_user(void __user *mem, unsigned long len);
575unsigned long __must_check __clear_user(void __user *mem, unsigned long len);
576
Qiaowei Renf09174c2013-12-14 14:25:02 +0800577extern void __cmpxchg_wrong_size(void)
578 __compiletime_error("Bad argument size for cmpxchg");
579
580#define __user_atomic_cmpxchg_inatomic(uval, ptr, old, new, size) \
581({ \
582 int __ret = 0; \
583 __typeof__(ptr) __uval = (uval); \
584 __typeof__(*(ptr)) __old = (old); \
585 __typeof__(*(ptr)) __new = (new); \
Dan Williams065eae42018-01-29 17:02:49 -0800586 __uaccess_begin_nospec(); \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800587 switch (size) { \
588 case 1: \
589 { \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800590 asm volatile("\n" \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800591 "1:\t" LOCK_PREFIX "cmpxchgb %4, %2\n" \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800592 "2:\n" \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800593 "\t.section .fixup, \"ax\"\n" \
594 "3:\tmov %3, %0\n" \
595 "\tjmp 2b\n" \
596 "\t.previous\n" \
597 _ASM_EXTABLE(1b, 3b) \
598 : "+r" (__ret), "=a" (__old), "+m" (*(ptr)) \
599 : "i" (-EFAULT), "q" (__new), "1" (__old) \
600 : "memory" \
601 ); \
602 break; \
603 } \
604 case 2: \
605 { \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800606 asm volatile("\n" \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800607 "1:\t" LOCK_PREFIX "cmpxchgw %4, %2\n" \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800608 "2:\n" \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800609 "\t.section .fixup, \"ax\"\n" \
610 "3:\tmov %3, %0\n" \
611 "\tjmp 2b\n" \
612 "\t.previous\n" \
613 _ASM_EXTABLE(1b, 3b) \
614 : "+r" (__ret), "=a" (__old), "+m" (*(ptr)) \
615 : "i" (-EFAULT), "r" (__new), "1" (__old) \
616 : "memory" \
617 ); \
618 break; \
619 } \
620 case 4: \
621 { \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800622 asm volatile("\n" \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800623 "1:\t" LOCK_PREFIX "cmpxchgl %4, %2\n" \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800624 "2:\n" \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800625 "\t.section .fixup, \"ax\"\n" \
626 "3:\tmov %3, %0\n" \
627 "\tjmp 2b\n" \
628 "\t.previous\n" \
629 _ASM_EXTABLE(1b, 3b) \
630 : "+r" (__ret), "=a" (__old), "+m" (*(ptr)) \
631 : "i" (-EFAULT), "r" (__new), "1" (__old) \
632 : "memory" \
633 ); \
634 break; \
635 } \
636 case 8: \
637 { \
638 if (!IS_ENABLED(CONFIG_X86_64)) \
639 __cmpxchg_wrong_size(); \
640 \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800641 asm volatile("\n" \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800642 "1:\t" LOCK_PREFIX "cmpxchgq %4, %2\n" \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800643 "2:\n" \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800644 "\t.section .fixup, \"ax\"\n" \
645 "3:\tmov %3, %0\n" \
646 "\tjmp 2b\n" \
647 "\t.previous\n" \
648 _ASM_EXTABLE(1b, 3b) \
649 : "+r" (__ret), "=a" (__old), "+m" (*(ptr)) \
650 : "i" (-EFAULT), "r" (__new), "1" (__old) \
651 : "memory" \
652 ); \
653 break; \
654 } \
655 default: \
656 __cmpxchg_wrong_size(); \
657 } \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800658 __uaccess_end(); \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800659 *__uval = __old; \
660 __ret; \
661})
662
663#define user_atomic_cmpxchg_inatomic(uval, ptr, old, new) \
664({ \
665 access_ok(VERIFY_WRITE, (ptr), sizeof(*(ptr))) ? \
666 __user_atomic_cmpxchg_inatomic((uval), (ptr), \
667 (old), (new), sizeof(*(ptr))) : \
668 -EFAULT; \
669})
670
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800671/*
Glauber Costa8bc7de0c2008-06-25 14:53:41 -0300672 * movsl can be slow when source and dest are not both 8-byte aligned
673 */
674#ifdef CONFIG_X86_INTEL_USERCOPY
675extern struct movsl_mask {
676 int mask;
677} ____cacheline_aligned_in_smp movsl_mask;
678#endif
679
Glauber Costa22cac162008-06-25 14:56:53 -0300680#define ARCH_HAS_NOCACHE_UACCESS 1
681
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200682#ifdef CONFIG_X86_32
David Howellsa1ce3922012-10-02 18:01:25 +0100683# include <asm/uaccess_32.h>
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200684#else
David Howellsa1ce3922012-10-02 18:01:25 +0100685# include <asm/uaccess_64.h>
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200686#endif
Glauber Costaca233862008-06-13 14:39:25 -0300687
Jan Beulich3df7b412013-10-21 09:43:57 +0100688unsigned long __must_check _copy_from_user(void *to, const void __user *from,
689 unsigned n);
Jan Beulich7a3d9b02013-10-21 09:44:37 +0100690unsigned long __must_check _copy_to_user(void __user *to, const void *from,
691 unsigned n);
Jan Beulich3df7b412013-10-21 09:43:57 +0100692
Josh Poimboeuf0d025d22016-08-30 08:04:16 -0500693extern void __compiletime_error("usercopy buffer size is too small")
694__bad_copy_user(void);
Jan Beulich3df7b412013-10-21 09:43:57 +0100695
Josh Poimboeuf0d025d22016-08-30 08:04:16 -0500696static inline void copy_user_overflow(int size, unsigned long count)
Jan Beulich3df7b412013-10-21 09:43:57 +0100697{
698 WARN(1, "Buffer overflow detected (%d < %lu)!\n", size, count);
699}
700
Kees Cooke6971002016-09-06 11:56:01 -0700701static __always_inline unsigned long __must_check
Jan Beulich3df7b412013-10-21 09:43:57 +0100702copy_from_user(void *to, const void __user *from, unsigned long n)
703{
704 int sz = __compiletime_object_size(to);
705
706 might_fault();
707
Andrey Ryabinin1771c6e2016-05-20 16:59:31 -0700708 kasan_check_write(to, n);
709
Kees Cook5b710f32016-06-23 15:04:01 -0700710 if (likely(sz < 0 || sz >= n)) {
711 check_object_size(to, n, false);
Jan Beulich3df7b412013-10-21 09:43:57 +0100712 n = _copy_from_user(to, from, n);
Josh Poimboeuf0d025d22016-08-30 08:04:16 -0500713 } else if (!__builtin_constant_p(n))
714 copy_user_overflow(sz, n);
Jan Beulich3df7b412013-10-21 09:43:57 +0100715 else
Josh Poimboeuf0d025d22016-08-30 08:04:16 -0500716 __bad_copy_user();
Jan Beulich3df7b412013-10-21 09:43:57 +0100717
718 return n;
719}
720
Kees Cooke6971002016-09-06 11:56:01 -0700721static __always_inline unsigned long __must_check
Jan Beulich7a3d9b02013-10-21 09:44:37 +0100722copy_to_user(void __user *to, const void *from, unsigned long n)
723{
724 int sz = __compiletime_object_size(from);
725
Andrey Ryabinin1771c6e2016-05-20 16:59:31 -0700726 kasan_check_read(from, n);
727
Jan Beulich7a3d9b02013-10-21 09:44:37 +0100728 might_fault();
729
Kees Cook5b710f32016-06-23 15:04:01 -0700730 if (likely(sz < 0 || sz >= n)) {
731 check_object_size(from, n, true);
Jan Beulich7a3d9b02013-10-21 09:44:37 +0100732 n = _copy_to_user(to, from, n);
Josh Poimboeuf0d025d22016-08-30 08:04:16 -0500733 } else if (!__builtin_constant_p(n))
734 copy_user_overflow(sz, n);
Jan Beulich7a3d9b02013-10-21 09:44:37 +0100735 else
Josh Poimboeuf0d025d22016-08-30 08:04:16 -0500736 __bad_copy_user();
Jan Beulich7a3d9b02013-10-21 09:44:37 +0100737
738 return n;
739}
740
Andi Kleen10013eb2015-10-22 15:07:20 -0700741/*
742 * We rely on the nested NMI work to allow atomic faults from the NMI path; the
743 * nested NMI paths are careful to preserve CR2.
744 *
745 * Caller must use pagefault_enable/disable, or run in interrupt context,
746 * and also do a uaccess_ok() check
747 */
748#define __copy_from_user_nmi __copy_from_user_inatomic
749
Linus Torvalds404a4742016-01-21 13:02:41 -0800750/*
Linus Torvalds5b24a7a2015-12-17 09:57:27 -0800751 * The "unsafe" user accesses aren't really "unsafe", but the naming
752 * is a big fat warning: you have to not only do the access_ok()
753 * checking before using them, but you have to surround them with the
754 * user_access_begin/end() pair.
755 */
756#define user_access_begin() __uaccess_begin()
757#define user_access_end() __uaccess_end()
758
Linus Torvalds1bd44032016-08-08 13:02:01 -0700759#define unsafe_put_user(x, ptr, err_label) \
760do { \
Linus Torvalds5b24a7a2015-12-17 09:57:27 -0800761 int __pu_err; \
762 __put_user_size((x), (ptr), sizeof(*(ptr)), __pu_err, -EFAULT); \
Linus Torvalds1bd44032016-08-08 13:02:01 -0700763 if (unlikely(__pu_err)) goto err_label; \
764} while (0)
Linus Torvalds5b24a7a2015-12-17 09:57:27 -0800765
Linus Torvalds1bd44032016-08-08 13:02:01 -0700766#define unsafe_get_user(x, ptr, err_label) \
767do { \
Linus Torvalds5b24a7a2015-12-17 09:57:27 -0800768 int __gu_err; \
769 unsigned long __gu_val; \
770 __get_user_size(__gu_val, (ptr), sizeof(*(ptr)), __gu_err, -EFAULT); \
771 (x) = (__force __typeof__(*(ptr)))__gu_val; \
Linus Torvalds1bd44032016-08-08 13:02:01 -0700772 if (unlikely(__gu_err)) goto err_label; \
773} while (0)
Linus Torvalds5b24a7a2015-12-17 09:57:27 -0800774
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700775#endif /* _ASM_X86_UACCESS_H */
Nick Piggin8174c432008-07-25 19:45:24 -0700776