blob: 9bbb3b2d0372ccf0814ad5c927b85fe536212838 [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>
8#include <linux/thread_info.h>
Glauber Costaca233862008-06-13 14:39:25 -03009#include <linux/string.h>
10#include <asm/asm.h>
11#include <asm/page.h>
H. Peter Anvin63bcff22012-09-21 12:43:12 -070012#include <asm/smap.h>
Glauber Costaca233862008-06-13 14:39:25 -030013
14#define VERIFY_READ 0
15#define VERIFY_WRITE 1
16
17/*
18 * The fs value determines whether argument validity checking should be
19 * performed or not. If get_fs() == USER_DS, checking is performed, with
20 * get_fs() == KERNEL_DS, checking is bypassed.
21 *
22 * For historical reasons, these macros are grossly misnamed.
23 */
24
25#define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
26
27#define KERNEL_DS MAKE_MM_SEG(-1UL)
Linus Torvalds9063c612009-06-20 15:40:00 -070028#define USER_DS MAKE_MM_SEG(TASK_SIZE_MAX)
Glauber Costaca233862008-06-13 14:39:25 -030029
30#define get_ds() (KERNEL_DS)
31#define get_fs() (current_thread_info()->addr_limit)
32#define set_fs(x) (current_thread_info()->addr_limit = (x))
33
34#define segment_eq(a, b) ((a).seg == (b).seg)
35
Linus Torvalds4ae73f22012-05-26 10:14:39 -070036#define user_addr_max() (current_thread_info()->addr_limit.seg)
Arun Sharmabc6ca7b2012-04-20 15:41:35 -070037#define __addr_ok(addr) \
38 ((unsigned long __force)(addr) < user_addr_max())
Glauber Costa002ca162008-06-25 11:08:51 -030039
Glauber Costaca233862008-06-13 14:39:25 -030040/*
41 * Test whether a block of memory is a valid user space address.
42 * Returns 0 if the range is valid, nonzero otherwise.
Glauber Costaca233862008-06-13 14:39:25 -030043 */
H. Peter Anvina7405762013-12-27 16:52:47 -080044static inline bool __chk_range_not_ok(unsigned long addr, unsigned long size, unsigned long limit)
Linus Torvaldsc5fe5d82013-12-27 15:30:58 -080045{
46 /*
47 * If we have used "sizeof()" for the size,
48 * we know it won't overflow the limit (but
49 * it might overflow the 'addr', so it's
50 * important to subtract the size from the
51 * limit, not add it to the address).
52 */
53 if (__builtin_constant_p(size))
Andy Lutomirski7e0f51c2015-10-05 17:47:50 -070054 return unlikely(addr > limit - size);
Linus Torvaldsc5fe5d82013-12-27 15:30:58 -080055
56 /* Arbitrary sizes? Be careful about overflow */
57 addr += size;
Andy Lutomirski7e0f51c2015-10-05 17:47:50 -070058 if (unlikely(addr < size))
H. Peter Anvina7405762013-12-27 16:52:47 -080059 return true;
Andy Lutomirski7e0f51c2015-10-05 17:47:50 -070060 return unlikely(addr > limit);
Linus Torvaldsc5fe5d82013-12-27 15:30:58 -080061}
Glauber Costaca233862008-06-13 14:39:25 -030062
Arun Sharmabc6ca7b2012-04-20 15:41:35 -070063#define __range_not_ok(addr, size, limit) \
Glauber Costaca233862008-06-13 14:39:25 -030064({ \
Glauber Costaca233862008-06-13 14:39:25 -030065 __chk_user_ptr(addr); \
Linus Torvaldsc5fe5d82013-12-27 15:30:58 -080066 __chk_range_not_ok((unsigned long __force)(addr), size, limit); \
Glauber Costaca233862008-06-13 14:39:25 -030067})
68
69/**
70 * access_ok: - Checks if a user space pointer is valid
71 * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
72 * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
73 * to write to a block, it is always safe to read from it.
74 * @addr: User space pointer to start of block to check
75 * @size: Size of block to check
76 *
David Hildenbrandb3c395ef2015-05-11 17:52:08 +020077 * Context: User context only. This function may sleep if pagefaults are
78 * enabled.
Glauber Costaca233862008-06-13 14:39:25 -030079 *
80 * Checks if a pointer to a block of memory in user space is valid.
81 *
82 * Returns true (nonzero) if the memory block may be valid, false (zero)
83 * if it is definitely invalid.
84 *
85 * Note that, depending on architecture, this function probably just
86 * checks that the pointer is in the user space range - after calling
87 * this function, memory access functions may still return -EFAULT.
88 */
Arun Sharmabc6ca7b2012-04-20 15:41:35 -070089#define access_ok(type, addr, size) \
H. Peter Anvina7405762013-12-27 16:52:47 -080090 likely(!__range_not_ok(addr, size, user_addr_max()))
Glauber Costaca233862008-06-13 14:39:25 -030091
92/*
H. Peter Anvin70627652012-04-20 17:12:48 -070093 * The exception table consists of pairs of addresses relative to the
94 * exception table enty itself: the first is the address of an
95 * instruction that is allowed to fault, and the second is the address
96 * at which the program should continue. No registers are modified,
97 * so it is entirely up to the continuation code to figure out what to
98 * do.
Glauber Costaca233862008-06-13 14:39:25 -030099 *
100 * All the routines below use bits of fixup code that are out of line
101 * with the main instruction path. This means when everything is well,
102 * we don't even have to jump over them. Further, they do not intrude
103 * on our cache or tlb entries.
104 */
105
106struct exception_table_entry {
H. Peter Anvin70627652012-04-20 17:12:48 -0700107 int insn, fixup;
Glauber Costaca233862008-06-13 14:39:25 -0300108};
H. Peter Anvin70627652012-04-20 17:12:48 -0700109/* This is not the generic standard exception_table_entry format */
110#define ARCH_HAS_SORT_EXTABLE
111#define ARCH_HAS_SEARCH_EXTABLE
Glauber Costaca233862008-06-13 14:39:25 -0300112
113extern int fixup_exception(struct pt_regs *regs);
H. Peter Anvin70627652012-04-20 17:12:48 -0700114extern int early_fixup_exception(unsigned long *ip);
Glauber Costaca233862008-06-13 14:39:25 -0300115
116/*
117 * These are the main single-value transfer routines. They automatically
118 * use the right size if we just have the right pointer type.
119 *
120 * This gets kind of ugly. We want to return _two_ values in "get_user()"
121 * and yet we don't want to do any pointers, because that is too much
122 * of a performance impact. Thus we have a few rather ugly macros here,
123 * and hide all the ugliness from the user.
124 *
125 * The "__xxx" versions of the user access functions are versions that
126 * do not verify the address space, that must have been done previously
127 * with a separate "access_ok()" call (this is used when we do multiple
128 * accesses to the same area of user memory).
129 */
130
131extern int __get_user_1(void);
132extern int __get_user_2(void);
133extern int __get_user_4(void);
134extern int __get_user_8(void);
135extern int __get_user_bad(void);
136
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800137#define __uaccess_begin() stac()
138#define __uaccess_end() clac()
139
H. Peter Anvin3578baa2013-02-12 11:47:31 -0800140/*
141 * This is a type: either unsigned long, if the argument fits into
142 * that type, or otherwise unsigned long long.
143 */
144#define __inttype(x) \
145__typeof__(__builtin_choose_expr(sizeof(x) > sizeof(0UL), 0ULL, 0UL))
Glauber Costa865e5b72008-06-25 11:05:11 -0300146
147/**
148 * get_user: - Get a simple variable from user space.
149 * @x: Variable to store result.
150 * @ptr: Source address, in user space.
151 *
David Hildenbrandb3c395ef2015-05-11 17:52:08 +0200152 * Context: User context only. This function may sleep if pagefaults are
153 * enabled.
Glauber Costa865e5b72008-06-25 11:05:11 -0300154 *
155 * This macro copies a single simple variable from user space to kernel
156 * space. It supports simple types like char and int, but not larger
157 * data types like structures or arrays.
158 *
159 * @ptr must have pointer-to-simple-variable type, and the result of
160 * dereferencing @ptr must be assignable to @x without a cast.
161 *
162 * Returns zero on success, or -EFAULT on error.
163 * On error, the variable @x is set to zero.
H. Peter Anvinff52c3b2013-02-12 15:37:02 -0800164 */
165/*
H. Peter Anvin3578baa2013-02-12 11:47:31 -0800166 * Careful: we have to cast the result to the type of the pointer
167 * for sign reasons.
H. Peter Anvinff52c3b2013-02-12 15:37:02 -0800168 *
H. Peter Anvinf69fa9a2013-08-29 13:34:50 -0700169 * The use of _ASM_DX as the register specifier is a bit of a
H. Peter Anvinff52c3b2013-02-12 15:37:02 -0800170 * simplification, as gcc only cares about it as the starting point
171 * and not size: for a 64-bit value it will use %ecx:%edx on 32 bits
172 * (%ecx being the next register in gcc's x86 register sequence), and
173 * %rdx on 64 bits.
H. Peter Anvinf69fa9a2013-08-29 13:34:50 -0700174 *
175 * Clang/LLVM cares about the size of the register, but still wants
176 * the base register for something that ends up being a pair.
Glauber Costa865e5b72008-06-25 11:05:11 -0300177 */
Glauber Costa865e5b72008-06-25 11:05:11 -0300178#define get_user(x, ptr) \
179({ \
180 int __ret_gu; \
Jan-Simon Möllerbdfc0172013-08-29 21:13:05 +0200181 register __inttype(*(ptr)) __val_gu asm("%"_ASM_DX); \
Chris J Argesf05058c2016-01-21 16:49:25 -0600182 register void *__sp asm(_ASM_SP); \
Glauber Costa865e5b72008-06-25 11:05:11 -0300183 __chk_user_ptr(ptr); \
Ingo Molnard1a76182008-10-28 16:54:49 +0100184 might_fault(); \
Chris J Argesf05058c2016-01-21 16:49:25 -0600185 asm volatile("call __get_user_%P4" \
186 : "=a" (__ret_gu), "=r" (__val_gu), "+r" (__sp) \
H. Peter Anvin3578baa2013-02-12 11:47:31 -0800187 : "0" (ptr), "i" (sizeof(*(ptr)))); \
Michael S. Tsirkine182c572014-12-12 01:56:04 +0200188 (x) = (__force __typeof__(*(ptr))) __val_gu; \
Andy Lutomirskia76cf662015-10-05 17:47:49 -0700189 __builtin_expect(__ret_gu, 0); \
Glauber Costa865e5b72008-06-25 11:05:11 -0300190})
191
Glauber Costae30a44f2008-06-25 13:17:43 -0300192#define __put_user_x(size, x, ptr, __ret_pu) \
193 asm volatile("call __put_user_" #size : "=a" (__ret_pu) \
Hiroshi Shimamoto4d5d7832009-01-19 16:34:26 -0800194 : "0" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx")
Glauber Costae30a44f2008-06-25 13:17:43 -0300195
196
197
Glauber Costadc70ddf2008-06-25 11:48:29 -0300198#ifdef CONFIG_X86_32
Hiroshi Shimamoto18114f62009-01-30 18:16:46 -0800199#define __put_user_asm_u64(x, addr, err, errret) \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800200 asm volatile("\n" \
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700201 "1: movl %%eax,0(%2)\n" \
Glauber Costadc70ddf2008-06-25 11:48:29 -0300202 "2: movl %%edx,4(%2)\n" \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800203 "3:" \
Glauber Costadc70ddf2008-06-25 11:48:29 -0300204 ".section .fixup,\"ax\"\n" \
205 "4: movl %3,%0\n" \
206 " jmp 3b\n" \
207 ".previous\n" \
208 _ASM_EXTABLE(1b, 4b) \
209 _ASM_EXTABLE(2b, 4b) \
210 : "=r" (err) \
Hiroshi Shimamoto18114f62009-01-30 18:16:46 -0800211 : "A" (x), "r" (addr), "i" (errret), "0" (err))
Glauber Costae30a44f2008-06-25 13:17:43 -0300212
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800213#define __put_user_asm_ex_u64(x, addr) \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800214 asm volatile("\n" \
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700215 "1: movl %%eax,0(%1)\n" \
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800216 "2: movl %%edx,4(%1)\n" \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800217 "3:" \
H. Peter Anvin535c0c32012-04-20 16:57:35 -0700218 _ASM_EXTABLE_EX(1b, 2b) \
219 _ASM_EXTABLE_EX(2b, 3b) \
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800220 : : "A" (x), "r" (addr))
221
Glauber Costae30a44f2008-06-25 13:17:43 -0300222#define __put_user_x8(x, ptr, __ret_pu) \
223 asm volatile("call __put_user_8" : "=a" (__ret_pu) \
224 : "A" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx")
Glauber Costadc70ddf2008-06-25 11:48:29 -0300225#else
Hiroshi Shimamoto18114f62009-01-30 18:16:46 -0800226#define __put_user_asm_u64(x, ptr, retval, errret) \
H. Peter Anvinebe119c2009-07-20 23:27:39 -0700227 __put_user_asm(x, ptr, retval, "q", "", "er", errret)
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800228#define __put_user_asm_ex_u64(x, addr) \
H. Peter Anvinebe119c2009-07-20 23:27:39 -0700229 __put_user_asm_ex(x, addr, "q", "", "er")
Glauber Costae30a44f2008-06-25 13:17:43 -0300230#define __put_user_x8(x, ptr, __ret_pu) __put_user_x(8, x, ptr, __ret_pu)
Glauber Costadc70ddf2008-06-25 11:48:29 -0300231#endif
232
Glauber Costae30a44f2008-06-25 13:17:43 -0300233extern void __put_user_bad(void);
234
235/*
236 * Strange magic calling convention: pointer in %ecx,
237 * value in %eax(:%edx), return value in %eax. clobbers %rbx
238 */
239extern void __put_user_1(void);
240extern void __put_user_2(void);
241extern void __put_user_4(void);
242extern void __put_user_8(void);
243
Glauber Costae30a44f2008-06-25 13:17:43 -0300244/**
245 * put_user: - Write a simple value into user space.
246 * @x: Value to copy to user space.
247 * @ptr: Destination address, in user space.
248 *
David Hildenbrandb3c395ef2015-05-11 17:52:08 +0200249 * Context: User context only. This function may sleep if pagefaults are
250 * enabled.
Glauber Costae30a44f2008-06-25 13:17:43 -0300251 *
252 * This macro copies a single simple value from kernel space to user
253 * space. It supports simple types like char and int, but not larger
254 * data types like structures or arrays.
255 *
256 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
257 * to the result of dereferencing @ptr.
258 *
259 * Returns zero on success, or -EFAULT on error.
260 */
261#define put_user(x, ptr) \
262({ \
263 int __ret_pu; \
264 __typeof__(*(ptr)) __pu_val; \
265 __chk_user_ptr(ptr); \
Ingo Molnard1a76182008-10-28 16:54:49 +0100266 might_fault(); \
Glauber Costae30a44f2008-06-25 13:17:43 -0300267 __pu_val = x; \
268 switch (sizeof(*(ptr))) { \
269 case 1: \
270 __put_user_x(1, __pu_val, ptr, __ret_pu); \
271 break; \
272 case 2: \
273 __put_user_x(2, __pu_val, ptr, __ret_pu); \
274 break; \
275 case 4: \
276 __put_user_x(4, __pu_val, ptr, __ret_pu); \
277 break; \
278 case 8: \
279 __put_user_x8(__pu_val, ptr, __ret_pu); \
280 break; \
281 default: \
282 __put_user_x(X, __pu_val, ptr, __ret_pu); \
283 break; \
284 } \
Andy Lutomirskia76cf662015-10-05 17:47:49 -0700285 __builtin_expect(__ret_pu, 0); \
Glauber Costae30a44f2008-06-25 13:17:43 -0300286})
287
Glauber Costadc70ddf2008-06-25 11:48:29 -0300288#define __put_user_size(x, ptr, size, retval, errret) \
289do { \
290 retval = 0; \
291 __chk_user_ptr(ptr); \
292 switch (size) { \
293 case 1: \
294 __put_user_asm(x, ptr, retval, "b", "b", "iq", errret); \
295 break; \
296 case 2: \
297 __put_user_asm(x, ptr, retval, "w", "w", "ir", errret); \
298 break; \
299 case 4: \
Hiroshi Shimamoto4d5d7832009-01-19 16:34:26 -0800300 __put_user_asm(x, ptr, retval, "l", "k", "ir", errret); \
Glauber Costadc70ddf2008-06-25 11:48:29 -0300301 break; \
302 case 8: \
Hiroshi Shimamoto18114f62009-01-30 18:16:46 -0800303 __put_user_asm_u64((__typeof__(*ptr))(x), ptr, retval, \
304 errret); \
Glauber Costadc70ddf2008-06-25 11:48:29 -0300305 break; \
306 default: \
307 __put_user_bad(); \
308 } \
309} while (0)
310
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800311/*
312 * This doesn't do __uaccess_begin/end - the exception handling
313 * around it must do that.
314 */
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800315#define __put_user_size_ex(x, ptr, size) \
316do { \
317 __chk_user_ptr(ptr); \
318 switch (size) { \
319 case 1: \
320 __put_user_asm_ex(x, ptr, "b", "b", "iq"); \
321 break; \
322 case 2: \
323 __put_user_asm_ex(x, ptr, "w", "w", "ir"); \
324 break; \
325 case 4: \
326 __put_user_asm_ex(x, ptr, "l", "k", "ir"); \
327 break; \
328 case 8: \
329 __put_user_asm_ex_u64((__typeof__(*ptr))(x), ptr); \
330 break; \
331 default: \
332 __put_user_bad(); \
333 } \
334} while (0)
335
Glauber Costa3f168222008-06-25 12:48:47 -0300336#ifdef CONFIG_X86_32
337#define __get_user_asm_u64(x, ptr, retval, errret) (x) = __get_user_bad()
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800338#define __get_user_asm_ex_u64(x, ptr) (x) = __get_user_bad()
Glauber Costa3f168222008-06-25 12:48:47 -0300339#else
340#define __get_user_asm_u64(x, ptr, retval, errret) \
341 __get_user_asm(x, ptr, retval, "q", "", "=r", errret)
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800342#define __get_user_asm_ex_u64(x, ptr) \
343 __get_user_asm_ex(x, ptr, "q", "", "=r")
Glauber Costa3f168222008-06-25 12:48:47 -0300344#endif
345
346#define __get_user_size(x, ptr, size, retval, errret) \
347do { \
348 retval = 0; \
349 __chk_user_ptr(ptr); \
350 switch (size) { \
351 case 1: \
352 __get_user_asm(x, ptr, retval, "b", "b", "=q", errret); \
353 break; \
354 case 2: \
355 __get_user_asm(x, ptr, retval, "w", "w", "=r", errret); \
356 break; \
357 case 4: \
358 __get_user_asm(x, ptr, retval, "l", "k", "=r", errret); \
359 break; \
360 case 8: \
361 __get_user_asm_u64(x, ptr, retval, errret); \
362 break; \
363 default: \
364 (x) = __get_user_bad(); \
365 } \
366} while (0)
367
368#define __get_user_asm(x, addr, err, itype, rtype, ltype, errret) \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800369 asm volatile("\n" \
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700370 "1: mov"itype" %2,%"rtype"1\n" \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800371 "2:\n" \
Glauber Costa3f168222008-06-25 12:48:47 -0300372 ".section .fixup,\"ax\"\n" \
373 "3: mov %3,%0\n" \
374 " xor"itype" %"rtype"1,%"rtype"1\n" \
375 " jmp 2b\n" \
376 ".previous\n" \
377 _ASM_EXTABLE(1b, 3b) \
378 : "=r" (err), ltype(x) \
379 : "m" (__m(addr)), "i" (errret), "0" (err))
380
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800381/*
382 * This doesn't do __uaccess_begin/end - the exception handling
383 * around it must do that.
384 */
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800385#define __get_user_size_ex(x, ptr, size) \
386do { \
387 __chk_user_ptr(ptr); \
388 switch (size) { \
389 case 1: \
390 __get_user_asm_ex(x, ptr, "b", "b", "=q"); \
391 break; \
392 case 2: \
393 __get_user_asm_ex(x, ptr, "w", "w", "=r"); \
394 break; \
395 case 4: \
396 __get_user_asm_ex(x, ptr, "l", "k", "=r"); \
397 break; \
398 case 8: \
399 __get_user_asm_ex_u64(x, ptr); \
400 break; \
401 default: \
402 (x) = __get_user_bad(); \
403 } \
404} while (0)
405
406#define __get_user_asm_ex(x, addr, itype, rtype, ltype) \
H. Peter Anvin5e883532012-09-21 12:43:15 -0700407 asm volatile("1: mov"itype" %1,%"rtype"0\n" \
408 "2:\n" \
H. Peter Anvin535c0c32012-04-20 16:57:35 -0700409 _ASM_EXTABLE_EX(1b, 2b) \
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800410 : ltype(x) : "m" (__m(addr)))
411
Glauber Costadc70ddf2008-06-25 11:48:29 -0300412#define __put_user_nocheck(x, ptr, size) \
413({ \
Hiroshi Shimamoto16855f82008-12-08 19:18:38 -0800414 int __pu_err; \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800415 __uaccess_begin(); \
Glauber Costadc70ddf2008-06-25 11:48:29 -0300416 __put_user_size((x), (ptr), (size), __pu_err, -EFAULT); \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800417 __uaccess_end(); \
Andy Lutomirskia76cf662015-10-05 17:47:49 -0700418 __builtin_expect(__pu_err, 0); \
Glauber Costadc70ddf2008-06-25 11:48:29 -0300419})
420
Glauber Costa3f168222008-06-25 12:48:47 -0300421#define __get_user_nocheck(x, ptr, size) \
422({ \
Hiroshi Shimamoto16855f82008-12-08 19:18:38 -0800423 int __gu_err; \
Glauber Costa3f168222008-06-25 12:48:47 -0300424 unsigned long __gu_val; \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800425 __uaccess_begin(); \
Glauber Costa3f168222008-06-25 12:48:47 -0300426 __get_user_size(__gu_val, (ptr), (size), __gu_err, -EFAULT); \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800427 __uaccess_end(); \
Glauber Costa3f168222008-06-25 12:48:47 -0300428 (x) = (__force __typeof__(*(ptr)))__gu_val; \
Andy Lutomirskia76cf662015-10-05 17:47:49 -0700429 __builtin_expect(__gu_err, 0); \
Glauber Costa3f168222008-06-25 12:48:47 -0300430})
Glauber Costadc70ddf2008-06-25 11:48:29 -0300431
432/* FIXME: this hack is definitely wrong -AK */
433struct __large_struct { unsigned long buf[100]; };
434#define __m(x) (*(struct __large_struct __user *)(x))
435
436/*
437 * Tell gcc we read from memory instead of writing: this is because
438 * we do not write to any memory gcc knows about, so there are no
439 * aliasing issues.
440 */
441#define __put_user_asm(x, addr, err, itype, rtype, ltype, errret) \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800442 asm volatile("\n" \
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700443 "1: mov"itype" %"rtype"1,%2\n" \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800444 "2:\n" \
Glauber Costadc70ddf2008-06-25 11:48:29 -0300445 ".section .fixup,\"ax\"\n" \
446 "3: mov %3,%0\n" \
447 " jmp 2b\n" \
448 ".previous\n" \
449 _ASM_EXTABLE(1b, 3b) \
450 : "=r"(err) \
451 : ltype(x), "m" (__m(addr)), "i" (errret), "0" (err))
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800452
453#define __put_user_asm_ex(x, addr, itype, rtype, ltype) \
H. Peter Anvin5e883532012-09-21 12:43:15 -0700454 asm volatile("1: mov"itype" %"rtype"0,%1\n" \
455 "2:\n" \
H. Peter Anvin535c0c32012-04-20 16:57:35 -0700456 _ASM_EXTABLE_EX(1b, 2b) \
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800457 : : ltype(x), "m" (__m(addr)))
458
459/*
460 * uaccess_try and catch
461 */
462#define uaccess_try do { \
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800463 current_thread_info()->uaccess_err = 0; \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800464 __uaccess_begin(); \
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800465 barrier();
466
467#define uaccess_catch(err) \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800468 __uaccess_end(); \
Andy Lutomirski4fc34902011-11-07 16:33:40 -0800469 (err) |= (current_thread_info()->uaccess_err ? -EFAULT : 0); \
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800470} while (0)
471
Glauber Costa8cb834e2008-06-25 14:43:30 -0300472/**
473 * __get_user: - Get a simple variable from user space, with less checking.
474 * @x: Variable to store result.
475 * @ptr: Source address, in user space.
476 *
David Hildenbrandb3c395ef2015-05-11 17:52:08 +0200477 * Context: User context only. This function may sleep if pagefaults are
478 * enabled.
Glauber Costa8cb834e2008-06-25 14:43:30 -0300479 *
480 * This macro copies a single simple variable from user space to kernel
481 * space. It supports simple types like char and int, but not larger
482 * data types like structures or arrays.
483 *
484 * @ptr must have pointer-to-simple-variable type, and the result of
485 * dereferencing @ptr must be assignable to @x without a cast.
486 *
487 * Caller must check the pointer with access_ok() before calling this
488 * function.
489 *
490 * Returns zero on success, or -EFAULT on error.
491 * On error, the variable @x is set to zero.
492 */
Glauber Costadc70ddf2008-06-25 11:48:29 -0300493
Glauber Costa8cb834e2008-06-25 14:43:30 -0300494#define __get_user(x, ptr) \
495 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800496
Glauber Costa8cb834e2008-06-25 14:43:30 -0300497/**
498 * __put_user: - Write a simple value into user space, with less checking.
499 * @x: Value to copy to user space.
500 * @ptr: Destination address, in user space.
501 *
David Hildenbrandb3c395ef2015-05-11 17:52:08 +0200502 * Context: User context only. This function may sleep if pagefaults are
503 * enabled.
Glauber Costa8cb834e2008-06-25 14:43:30 -0300504 *
505 * This macro copies a single simple value from kernel space to user
506 * space. It supports simple types like char and int, but not larger
507 * data types like structures or arrays.
508 *
509 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
510 * to the result of dereferencing @ptr.
511 *
512 * Caller must check the pointer with access_ok() before calling this
513 * function.
514 *
515 * Returns zero on success, or -EFAULT on error.
516 */
517
518#define __put_user(x, ptr) \
519 __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
520
521#define __get_user_unaligned __get_user
522#define __put_user_unaligned __put_user
Glauber Costa865e5b72008-06-25 11:05:11 -0300523
Glauber Costa8bc7de0c2008-06-25 14:53:41 -0300524/*
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800525 * {get|put}_user_try and catch
526 *
527 * get_user_try {
528 * get_user_ex(...);
529 * } get_user_catch(err)
530 */
531#define get_user_try uaccess_try
532#define get_user_catch(err) uaccess_catch(err)
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800533
534#define get_user_ex(x, ptr) do { \
535 unsigned long __gue_val; \
536 __get_user_size_ex((__gue_val), (ptr), (sizeof(*(ptr)))); \
537 (x) = (__force __typeof__(*(ptr)))__gue_val; \
538} while (0)
539
Hiroshi Shimamoto019a1362009-01-29 11:49:18 -0800540#define put_user_try uaccess_try
541#define put_user_catch(err) uaccess_catch(err)
542
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800543#define put_user_ex(x, ptr) \
544 __put_user_size_ex((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
545
Robert Richter1ac2e6c2011-06-07 11:49:55 +0200546extern unsigned long
547copy_from_user_nmi(void *to, const void __user *from, unsigned long n);
Linus Torvalds92ae03f2012-04-06 14:32:32 -0700548extern __must_check long
549strncpy_from_user(char *dst, const char __user *src, long count);
Robert Richter1ac2e6c2011-06-07 11:49:55 +0200550
Linus Torvalds5723aa92012-05-26 11:09:53 -0700551extern __must_check long strlen_user(const char __user *str);
552extern __must_check long strnlen_user(const char __user *str, long n);
553
H. Peter Anvina0528582012-09-21 12:43:11 -0700554unsigned long __must_check clear_user(void __user *mem, unsigned long len);
555unsigned long __must_check __clear_user(void __user *mem, unsigned long len);
556
Qiaowei Renf09174c2013-12-14 14:25:02 +0800557extern void __cmpxchg_wrong_size(void)
558 __compiletime_error("Bad argument size for cmpxchg");
559
560#define __user_atomic_cmpxchg_inatomic(uval, ptr, old, new, size) \
561({ \
562 int __ret = 0; \
563 __typeof__(ptr) __uval = (uval); \
564 __typeof__(*(ptr)) __old = (old); \
565 __typeof__(*(ptr)) __new = (new); \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800566 __uaccess_begin(); \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800567 switch (size) { \
568 case 1: \
569 { \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800570 asm volatile("\n" \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800571 "1:\t" LOCK_PREFIX "cmpxchgb %4, %2\n" \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800572 "2:\n" \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800573 "\t.section .fixup, \"ax\"\n" \
574 "3:\tmov %3, %0\n" \
575 "\tjmp 2b\n" \
576 "\t.previous\n" \
577 _ASM_EXTABLE(1b, 3b) \
578 : "+r" (__ret), "=a" (__old), "+m" (*(ptr)) \
579 : "i" (-EFAULT), "q" (__new), "1" (__old) \
580 : "memory" \
581 ); \
582 break; \
583 } \
584 case 2: \
585 { \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800586 asm volatile("\n" \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800587 "1:\t" LOCK_PREFIX "cmpxchgw %4, %2\n" \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800588 "2:\n" \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800589 "\t.section .fixup, \"ax\"\n" \
590 "3:\tmov %3, %0\n" \
591 "\tjmp 2b\n" \
592 "\t.previous\n" \
593 _ASM_EXTABLE(1b, 3b) \
594 : "+r" (__ret), "=a" (__old), "+m" (*(ptr)) \
595 : "i" (-EFAULT), "r" (__new), "1" (__old) \
596 : "memory" \
597 ); \
598 break; \
599 } \
600 case 4: \
601 { \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800602 asm volatile("\n" \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800603 "1:\t" LOCK_PREFIX "cmpxchgl %4, %2\n" \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800604 "2:\n" \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800605 "\t.section .fixup, \"ax\"\n" \
606 "3:\tmov %3, %0\n" \
607 "\tjmp 2b\n" \
608 "\t.previous\n" \
609 _ASM_EXTABLE(1b, 3b) \
610 : "+r" (__ret), "=a" (__old), "+m" (*(ptr)) \
611 : "i" (-EFAULT), "r" (__new), "1" (__old) \
612 : "memory" \
613 ); \
614 break; \
615 } \
616 case 8: \
617 { \
618 if (!IS_ENABLED(CONFIG_X86_64)) \
619 __cmpxchg_wrong_size(); \
620 \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800621 asm volatile("\n" \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800622 "1:\t" LOCK_PREFIX "cmpxchgq %4, %2\n" \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800623 "2:\n" \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800624 "\t.section .fixup, \"ax\"\n" \
625 "3:\tmov %3, %0\n" \
626 "\tjmp 2b\n" \
627 "\t.previous\n" \
628 _ASM_EXTABLE(1b, 3b) \
629 : "+r" (__ret), "=a" (__old), "+m" (*(ptr)) \
630 : "i" (-EFAULT), "r" (__new), "1" (__old) \
631 : "memory" \
632 ); \
633 break; \
634 } \
635 default: \
636 __cmpxchg_wrong_size(); \
637 } \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800638 __uaccess_end(); \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800639 *__uval = __old; \
640 __ret; \
641})
642
643#define user_atomic_cmpxchg_inatomic(uval, ptr, old, new) \
644({ \
645 access_ok(VERIFY_WRITE, (ptr), sizeof(*(ptr))) ? \
646 __user_atomic_cmpxchg_inatomic((uval), (ptr), \
647 (old), (new), sizeof(*(ptr))) : \
648 -EFAULT; \
649})
650
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800651/*
Glauber Costa8bc7de0c2008-06-25 14:53:41 -0300652 * movsl can be slow when source and dest are not both 8-byte aligned
653 */
654#ifdef CONFIG_X86_INTEL_USERCOPY
655extern struct movsl_mask {
656 int mask;
657} ____cacheline_aligned_in_smp movsl_mask;
658#endif
659
Glauber Costa22cac162008-06-25 14:56:53 -0300660#define ARCH_HAS_NOCACHE_UACCESS 1
661
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200662#ifdef CONFIG_X86_32
David Howellsa1ce3922012-10-02 18:01:25 +0100663# include <asm/uaccess_32.h>
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200664#else
David Howellsa1ce3922012-10-02 18:01:25 +0100665# include <asm/uaccess_64.h>
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200666#endif
Glauber Costaca233862008-06-13 14:39:25 -0300667
Jan Beulich3df7b412013-10-21 09:43:57 +0100668unsigned long __must_check _copy_from_user(void *to, const void __user *from,
669 unsigned n);
Jan Beulich7a3d9b02013-10-21 09:44:37 +0100670unsigned long __must_check _copy_to_user(void __user *to, const void *from,
671 unsigned n);
Jan Beulich3df7b412013-10-21 09:43:57 +0100672
673#ifdef CONFIG_DEBUG_STRICT_USER_COPY_CHECKS
674# define copy_user_diag __compiletime_error
675#else
676# define copy_user_diag __compiletime_warning
677#endif
678
679extern void copy_user_diag("copy_from_user() buffer size is too small")
680copy_from_user_overflow(void);
Jan Beulich7a3d9b02013-10-21 09:44:37 +0100681extern void copy_user_diag("copy_to_user() buffer size is too small")
682copy_to_user_overflow(void) __asm__("copy_from_user_overflow");
Jan Beulich3df7b412013-10-21 09:43:57 +0100683
684#undef copy_user_diag
685
686#ifdef CONFIG_DEBUG_STRICT_USER_COPY_CHECKS
687
688extern void
689__compiletime_warning("copy_from_user() buffer size is not provably correct")
690__copy_from_user_overflow(void) __asm__("copy_from_user_overflow");
691#define __copy_from_user_overflow(size, count) __copy_from_user_overflow()
692
Jan Beulich7a3d9b02013-10-21 09:44:37 +0100693extern void
694__compiletime_warning("copy_to_user() buffer size is not provably correct")
695__copy_to_user_overflow(void) __asm__("copy_from_user_overflow");
696#define __copy_to_user_overflow(size, count) __copy_to_user_overflow()
697
Jan Beulich3df7b412013-10-21 09:43:57 +0100698#else
699
700static inline void
701__copy_from_user_overflow(int size, unsigned long count)
702{
703 WARN(1, "Buffer overflow detected (%d < %lu)!\n", size, count);
704}
705
Jan Beulich7a3d9b02013-10-21 09:44:37 +0100706#define __copy_to_user_overflow __copy_from_user_overflow
707
Jan Beulich3df7b412013-10-21 09:43:57 +0100708#endif
709
710static inline unsigned long __must_check
711copy_from_user(void *to, const void __user *from, unsigned long n)
712{
713 int sz = __compiletime_object_size(to);
714
715 might_fault();
716
717 /*
718 * While we would like to have the compiler do the checking for us
719 * even in the non-constant size case, any false positives there are
720 * a problem (especially when DEBUG_STRICT_USER_COPY_CHECKS, but even
721 * without - the [hopefully] dangerous looking nature of the warning
722 * would make people go look at the respecitive call sites over and
723 * over again just to find that there's no problem).
724 *
725 * And there are cases where it's just not realistic for the compiler
726 * to prove the count to be in range. For example when multiple call
727 * sites of a helper function - perhaps in different source files -
728 * all doing proper range checking, yet the helper function not doing
729 * so again.
730 *
731 * Therefore limit the compile time checking to the constant size
732 * case, and do only runtime checking for non-constant sizes.
733 */
734
735 if (likely(sz < 0 || sz >= n))
736 n = _copy_from_user(to, from, n);
737 else if(__builtin_constant_p(n))
738 copy_from_user_overflow();
739 else
740 __copy_from_user_overflow(sz, n);
741
742 return n;
743}
744
Jan Beulich7a3d9b02013-10-21 09:44:37 +0100745static inline unsigned long __must_check
746copy_to_user(void __user *to, const void *from, unsigned long n)
747{
748 int sz = __compiletime_object_size(from);
749
750 might_fault();
751
752 /* See the comment in copy_from_user() above. */
753 if (likely(sz < 0 || sz >= n))
754 n = _copy_to_user(to, from, n);
755 else if(__builtin_constant_p(n))
756 copy_to_user_overflow();
757 else
758 __copy_to_user_overflow(sz, n);
759
760 return n;
761}
762
Jan Beulich3df7b412013-10-21 09:43:57 +0100763#undef __copy_from_user_overflow
Jan Beulich7a3d9b02013-10-21 09:44:37 +0100764#undef __copy_to_user_overflow
Jan Beulich3df7b412013-10-21 09:43:57 +0100765
Andi Kleen10013eb2015-10-22 15:07:20 -0700766/*
767 * We rely on the nested NMI work to allow atomic faults from the NMI path; the
768 * nested NMI paths are careful to preserve CR2.
769 *
770 * Caller must use pagefault_enable/disable, or run in interrupt context,
771 * and also do a uaccess_ok() check
772 */
773#define __copy_from_user_nmi __copy_from_user_inatomic
774
Linus Torvalds404a4742016-01-21 13:02:41 -0800775/*
Linus Torvalds5b24a7a2015-12-17 09:57:27 -0800776 * The "unsafe" user accesses aren't really "unsafe", but the naming
777 * is a big fat warning: you have to not only do the access_ok()
778 * checking before using them, but you have to surround them with the
779 * user_access_begin/end() pair.
780 */
781#define user_access_begin() __uaccess_begin()
782#define user_access_end() __uaccess_end()
783
784#define unsafe_put_user(x, ptr) \
785({ \
786 int __pu_err; \
787 __put_user_size((x), (ptr), sizeof(*(ptr)), __pu_err, -EFAULT); \
788 __builtin_expect(__pu_err, 0); \
789})
790
791#define unsafe_get_user(x, ptr) \
792({ \
793 int __gu_err; \
794 unsigned long __gu_val; \
795 __get_user_size(__gu_val, (ptr), sizeof(*(ptr)), __gu_err, -EFAULT); \
796 (x) = (__force __typeof__(*(ptr)))__gu_val; \
797 __builtin_expect(__gu_err, 0); \
798})
799
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700800#endif /* _ASM_X86_UACCESS_H */
Nick Piggin8174c432008-07-25 19:45:24 -0700801