blob: 84ecf1df2ac66d26f7c725d6b29e737349007e21 [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 */
Linus Torvaldsc5fe5d82013-12-27 15:30:58 -080044static inline int __chk_range_not_ok(unsigned long addr, unsigned long size, unsigned long limit)
45{
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))
54 return addr > limit - size;
55
56 /* Arbitrary sizes? Be careful about overflow */
57 addr += size;
58 return (addr < size) || (addr > limit);
59}
Glauber Costaca233862008-06-13 14:39:25 -030060
Arun Sharmabc6ca7b2012-04-20 15:41:35 -070061#define __range_not_ok(addr, size, limit) \
Glauber Costaca233862008-06-13 14:39:25 -030062({ \
Glauber Costaca233862008-06-13 14:39:25 -030063 __chk_user_ptr(addr); \
Linus Torvaldsc5fe5d82013-12-27 15:30:58 -080064 __chk_range_not_ok((unsigned long __force)(addr), size, limit); \
Glauber Costaca233862008-06-13 14:39:25 -030065})
66
67/**
68 * access_ok: - Checks if a user space pointer is valid
69 * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
70 * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
71 * to write to a block, it is always safe to read from it.
72 * @addr: User space pointer to start of block to check
73 * @size: Size of block to check
74 *
75 * Context: User context only. This function may sleep.
76 *
77 * Checks if a pointer to a block of memory in user space is valid.
78 *
79 * Returns true (nonzero) if the memory block may be valid, false (zero)
80 * if it is definitely invalid.
81 *
82 * Note that, depending on architecture, this function probably just
83 * checks that the pointer is in the user space range - after calling
84 * this function, memory access functions may still return -EFAULT.
85 */
Arun Sharmabc6ca7b2012-04-20 15:41:35 -070086#define access_ok(type, addr, size) \
87 (likely(__range_not_ok(addr, size, user_addr_max()) == 0))
Glauber Costaca233862008-06-13 14:39:25 -030088
89/*
H. Peter Anvin70627652012-04-20 17:12:48 -070090 * The exception table consists of pairs of addresses relative to the
91 * exception table enty itself: the first is the address of an
92 * instruction that is allowed to fault, and the second is the address
93 * at which the program should continue. No registers are modified,
94 * so it is entirely up to the continuation code to figure out what to
95 * do.
Glauber Costaca233862008-06-13 14:39:25 -030096 *
97 * All the routines below use bits of fixup code that are out of line
98 * with the main instruction path. This means when everything is well,
99 * we don't even have to jump over them. Further, they do not intrude
100 * on our cache or tlb entries.
101 */
102
103struct exception_table_entry {
H. Peter Anvin70627652012-04-20 17:12:48 -0700104 int insn, fixup;
Glauber Costaca233862008-06-13 14:39:25 -0300105};
H. Peter Anvin70627652012-04-20 17:12:48 -0700106/* This is not the generic standard exception_table_entry format */
107#define ARCH_HAS_SORT_EXTABLE
108#define ARCH_HAS_SEARCH_EXTABLE
Glauber Costaca233862008-06-13 14:39:25 -0300109
110extern int fixup_exception(struct pt_regs *regs);
H. Peter Anvin70627652012-04-20 17:12:48 -0700111extern int early_fixup_exception(unsigned long *ip);
Glauber Costaca233862008-06-13 14:39:25 -0300112
113/*
114 * These are the main single-value transfer routines. They automatically
115 * use the right size if we just have the right pointer type.
116 *
117 * This gets kind of ugly. We want to return _two_ values in "get_user()"
118 * and yet we don't want to do any pointers, because that is too much
119 * of a performance impact. Thus we have a few rather ugly macros here,
120 * and hide all the ugliness from the user.
121 *
122 * The "__xxx" versions of the user access functions are versions that
123 * do not verify the address space, that must have been done previously
124 * with a separate "access_ok()" call (this is used when we do multiple
125 * accesses to the same area of user memory).
126 */
127
128extern int __get_user_1(void);
129extern int __get_user_2(void);
130extern int __get_user_4(void);
131extern int __get_user_8(void);
132extern int __get_user_bad(void);
133
H. Peter Anvin3578baa2013-02-12 11:47:31 -0800134/*
135 * This is a type: either unsigned long, if the argument fits into
136 * that type, or otherwise unsigned long long.
137 */
138#define __inttype(x) \
139__typeof__(__builtin_choose_expr(sizeof(x) > sizeof(0UL), 0ULL, 0UL))
Glauber Costa865e5b72008-06-25 11:05:11 -0300140
141/**
142 * get_user: - Get a simple variable from user space.
143 * @x: Variable to store result.
144 * @ptr: Source address, in user space.
145 *
146 * Context: User context only. This function may sleep.
147 *
148 * This macro copies a single simple variable from user space to kernel
149 * space. It supports simple types like char and int, but not larger
150 * data types like structures or arrays.
151 *
152 * @ptr must have pointer-to-simple-variable type, and the result of
153 * dereferencing @ptr must be assignable to @x without a cast.
154 *
155 * Returns zero on success, or -EFAULT on error.
156 * On error, the variable @x is set to zero.
H. Peter Anvinff52c3b2013-02-12 15:37:02 -0800157 */
158/*
H. Peter Anvin3578baa2013-02-12 11:47:31 -0800159 * Careful: we have to cast the result to the type of the pointer
160 * for sign reasons.
H. Peter Anvinff52c3b2013-02-12 15:37:02 -0800161 *
H. Peter Anvinf69fa9a2013-08-29 13:34:50 -0700162 * The use of _ASM_DX as the register specifier is a bit of a
H. Peter Anvinff52c3b2013-02-12 15:37:02 -0800163 * simplification, as gcc only cares about it as the starting point
164 * and not size: for a 64-bit value it will use %ecx:%edx on 32 bits
165 * (%ecx being the next register in gcc's x86 register sequence), and
166 * %rdx on 64 bits.
H. Peter Anvinf69fa9a2013-08-29 13:34:50 -0700167 *
168 * Clang/LLVM cares about the size of the register, but still wants
169 * the base register for something that ends up being a pair.
Glauber Costa865e5b72008-06-25 11:05:11 -0300170 */
Glauber Costa865e5b72008-06-25 11:05:11 -0300171#define get_user(x, ptr) \
172({ \
173 int __ret_gu; \
Jan-Simon Möllerbdfc0172013-08-29 21:13:05 +0200174 register __inttype(*(ptr)) __val_gu asm("%"_ASM_DX); \
Glauber Costa865e5b72008-06-25 11:05:11 -0300175 __chk_user_ptr(ptr); \
Ingo Molnard1a76182008-10-28 16:54:49 +0100176 might_fault(); \
H. Peter Anvin3578baa2013-02-12 11:47:31 -0800177 asm volatile("call __get_user_%P3" \
178 : "=a" (__ret_gu), "=r" (__val_gu) \
179 : "0" (ptr), "i" (sizeof(*(ptr)))); \
180 (x) = (__typeof__(*(ptr))) __val_gu; \
Glauber Costa865e5b72008-06-25 11:05:11 -0300181 __ret_gu; \
182})
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) \
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700192 asm volatile(ASM_STAC "\n" \
193 "1: movl %%eax,0(%2)\n" \
Glauber Costadc70ddf2008-06-25 11:48:29 -0300194 "2: movl %%edx,4(%2)\n" \
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700195 "3: " ASM_CLAC "\n" \
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) \
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700206 asm volatile(ASM_STAC "\n" \
207 "1: movl %%eax,0(%1)\n" \
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800208 "2: movl %%edx,4(%1)\n" \
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700209 "3: " ASM_CLAC "\n" \
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 *
241 * Context: User context only. This function may sleep.
242 *
243 * This macro copies a single simple value from kernel space to user
244 * space. It supports simple types like char and int, but not larger
245 * data types like structures or arrays.
246 *
247 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
248 * to the result of dereferencing @ptr.
249 *
250 * Returns zero on success, or -EFAULT on error.
251 */
252#define put_user(x, ptr) \
253({ \
254 int __ret_pu; \
255 __typeof__(*(ptr)) __pu_val; \
256 __chk_user_ptr(ptr); \
Ingo Molnard1a76182008-10-28 16:54:49 +0100257 might_fault(); \
Glauber Costae30a44f2008-06-25 13:17:43 -0300258 __pu_val = x; \
259 switch (sizeof(*(ptr))) { \
260 case 1: \
261 __put_user_x(1, __pu_val, ptr, __ret_pu); \
262 break; \
263 case 2: \
264 __put_user_x(2, __pu_val, ptr, __ret_pu); \
265 break; \
266 case 4: \
267 __put_user_x(4, __pu_val, ptr, __ret_pu); \
268 break; \
269 case 8: \
270 __put_user_x8(__pu_val, ptr, __ret_pu); \
271 break; \
272 default: \
273 __put_user_x(X, __pu_val, ptr, __ret_pu); \
274 break; \
275 } \
276 __ret_pu; \
277})
278
Glauber Costadc70ddf2008-06-25 11:48:29 -0300279#define __put_user_size(x, ptr, size, retval, errret) \
280do { \
281 retval = 0; \
282 __chk_user_ptr(ptr); \
283 switch (size) { \
284 case 1: \
285 __put_user_asm(x, ptr, retval, "b", "b", "iq", errret); \
286 break; \
287 case 2: \
288 __put_user_asm(x, ptr, retval, "w", "w", "ir", errret); \
289 break; \
290 case 4: \
Hiroshi Shimamoto4d5d7832009-01-19 16:34:26 -0800291 __put_user_asm(x, ptr, retval, "l", "k", "ir", errret); \
Glauber Costadc70ddf2008-06-25 11:48:29 -0300292 break; \
293 case 8: \
Hiroshi Shimamoto18114f62009-01-30 18:16:46 -0800294 __put_user_asm_u64((__typeof__(*ptr))(x), ptr, retval, \
295 errret); \
Glauber Costadc70ddf2008-06-25 11:48:29 -0300296 break; \
297 default: \
298 __put_user_bad(); \
299 } \
300} while (0)
301
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800302#define __put_user_size_ex(x, ptr, size) \
303do { \
304 __chk_user_ptr(ptr); \
305 switch (size) { \
306 case 1: \
307 __put_user_asm_ex(x, ptr, "b", "b", "iq"); \
308 break; \
309 case 2: \
310 __put_user_asm_ex(x, ptr, "w", "w", "ir"); \
311 break; \
312 case 4: \
313 __put_user_asm_ex(x, ptr, "l", "k", "ir"); \
314 break; \
315 case 8: \
316 __put_user_asm_ex_u64((__typeof__(*ptr))(x), ptr); \
317 break; \
318 default: \
319 __put_user_bad(); \
320 } \
321} while (0)
322
Glauber Costa3f168222008-06-25 12:48:47 -0300323#ifdef CONFIG_X86_32
324#define __get_user_asm_u64(x, ptr, retval, errret) (x) = __get_user_bad()
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800325#define __get_user_asm_ex_u64(x, ptr) (x) = __get_user_bad()
Glauber Costa3f168222008-06-25 12:48:47 -0300326#else
327#define __get_user_asm_u64(x, ptr, retval, errret) \
328 __get_user_asm(x, ptr, retval, "q", "", "=r", errret)
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800329#define __get_user_asm_ex_u64(x, ptr) \
330 __get_user_asm_ex(x, ptr, "q", "", "=r")
Glauber Costa3f168222008-06-25 12:48:47 -0300331#endif
332
333#define __get_user_size(x, ptr, size, retval, errret) \
334do { \
335 retval = 0; \
336 __chk_user_ptr(ptr); \
337 switch (size) { \
338 case 1: \
339 __get_user_asm(x, ptr, retval, "b", "b", "=q", errret); \
340 break; \
341 case 2: \
342 __get_user_asm(x, ptr, retval, "w", "w", "=r", errret); \
343 break; \
344 case 4: \
345 __get_user_asm(x, ptr, retval, "l", "k", "=r", errret); \
346 break; \
347 case 8: \
348 __get_user_asm_u64(x, ptr, retval, errret); \
349 break; \
350 default: \
351 (x) = __get_user_bad(); \
352 } \
353} while (0)
354
355#define __get_user_asm(x, addr, err, itype, rtype, ltype, errret) \
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700356 asm volatile(ASM_STAC "\n" \
357 "1: mov"itype" %2,%"rtype"1\n" \
358 "2: " ASM_CLAC "\n" \
Glauber Costa3f168222008-06-25 12:48:47 -0300359 ".section .fixup,\"ax\"\n" \
360 "3: mov %3,%0\n" \
361 " xor"itype" %"rtype"1,%"rtype"1\n" \
362 " jmp 2b\n" \
363 ".previous\n" \
364 _ASM_EXTABLE(1b, 3b) \
365 : "=r" (err), ltype(x) \
366 : "m" (__m(addr)), "i" (errret), "0" (err))
367
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800368#define __get_user_size_ex(x, ptr, size) \
369do { \
370 __chk_user_ptr(ptr); \
371 switch (size) { \
372 case 1: \
373 __get_user_asm_ex(x, ptr, "b", "b", "=q"); \
374 break; \
375 case 2: \
376 __get_user_asm_ex(x, ptr, "w", "w", "=r"); \
377 break; \
378 case 4: \
379 __get_user_asm_ex(x, ptr, "l", "k", "=r"); \
380 break; \
381 case 8: \
382 __get_user_asm_ex_u64(x, ptr); \
383 break; \
384 default: \
385 (x) = __get_user_bad(); \
386 } \
387} while (0)
388
389#define __get_user_asm_ex(x, addr, itype, rtype, ltype) \
H. Peter Anvin5e883532012-09-21 12:43:15 -0700390 asm volatile("1: mov"itype" %1,%"rtype"0\n" \
391 "2:\n" \
H. Peter Anvin535c0c32012-04-20 16:57:35 -0700392 _ASM_EXTABLE_EX(1b, 2b) \
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800393 : ltype(x) : "m" (__m(addr)))
394
Glauber Costadc70ddf2008-06-25 11:48:29 -0300395#define __put_user_nocheck(x, ptr, size) \
396({ \
Hiroshi Shimamoto16855f82008-12-08 19:18:38 -0800397 int __pu_err; \
Glauber Costadc70ddf2008-06-25 11:48:29 -0300398 __put_user_size((x), (ptr), (size), __pu_err, -EFAULT); \
399 __pu_err; \
400})
401
Glauber Costa3f168222008-06-25 12:48:47 -0300402#define __get_user_nocheck(x, ptr, size) \
403({ \
Hiroshi Shimamoto16855f82008-12-08 19:18:38 -0800404 int __gu_err; \
Glauber Costa3f168222008-06-25 12:48:47 -0300405 unsigned long __gu_val; \
406 __get_user_size(__gu_val, (ptr), (size), __gu_err, -EFAULT); \
407 (x) = (__force __typeof__(*(ptr)))__gu_val; \
408 __gu_err; \
409})
Glauber Costadc70ddf2008-06-25 11:48:29 -0300410
411/* FIXME: this hack is definitely wrong -AK */
412struct __large_struct { unsigned long buf[100]; };
413#define __m(x) (*(struct __large_struct __user *)(x))
414
415/*
416 * Tell gcc we read from memory instead of writing: this is because
417 * we do not write to any memory gcc knows about, so there are no
418 * aliasing issues.
419 */
420#define __put_user_asm(x, addr, err, itype, rtype, ltype, errret) \
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700421 asm volatile(ASM_STAC "\n" \
422 "1: mov"itype" %"rtype"1,%2\n" \
423 "2: " ASM_CLAC "\n" \
Glauber Costadc70ddf2008-06-25 11:48:29 -0300424 ".section .fixup,\"ax\"\n" \
425 "3: mov %3,%0\n" \
426 " jmp 2b\n" \
427 ".previous\n" \
428 _ASM_EXTABLE(1b, 3b) \
429 : "=r"(err) \
430 : ltype(x), "m" (__m(addr)), "i" (errret), "0" (err))
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800431
432#define __put_user_asm_ex(x, addr, itype, rtype, ltype) \
H. Peter Anvin5e883532012-09-21 12:43:15 -0700433 asm volatile("1: mov"itype" %"rtype"0,%1\n" \
434 "2:\n" \
H. Peter Anvin535c0c32012-04-20 16:57:35 -0700435 _ASM_EXTABLE_EX(1b, 2b) \
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800436 : : ltype(x), "m" (__m(addr)))
437
438/*
439 * uaccess_try and catch
440 */
441#define uaccess_try do { \
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800442 current_thread_info()->uaccess_err = 0; \
H. Peter Anvin5e883532012-09-21 12:43:15 -0700443 stac(); \
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800444 barrier();
445
446#define uaccess_catch(err) \
H. Peter Anvin5e883532012-09-21 12:43:15 -0700447 clac(); \
Andy Lutomirski4fc34902011-11-07 16:33:40 -0800448 (err) |= (current_thread_info()->uaccess_err ? -EFAULT : 0); \
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800449} while (0)
450
Glauber Costa8cb834e2008-06-25 14:43:30 -0300451/**
452 * __get_user: - Get a simple variable from user space, with less checking.
453 * @x: Variable to store result.
454 * @ptr: Source address, in user space.
455 *
456 * Context: User context only. This function may sleep.
457 *
458 * This macro copies a single simple variable from user space to kernel
459 * space. It supports simple types like char and int, but not larger
460 * data types like structures or arrays.
461 *
462 * @ptr must have pointer-to-simple-variable type, and the result of
463 * dereferencing @ptr must be assignable to @x without a cast.
464 *
465 * Caller must check the pointer with access_ok() before calling this
466 * function.
467 *
468 * Returns zero on success, or -EFAULT on error.
469 * On error, the variable @x is set to zero.
470 */
Glauber Costadc70ddf2008-06-25 11:48:29 -0300471
Glauber Costa8cb834e2008-06-25 14:43:30 -0300472#define __get_user(x, ptr) \
473 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800474
Glauber Costa8cb834e2008-06-25 14:43:30 -0300475/**
476 * __put_user: - Write a simple value into user space, with less checking.
477 * @x: Value to copy to user space.
478 * @ptr: Destination address, in user space.
479 *
480 * Context: User context only. This function may sleep.
481 *
482 * This macro copies a single simple value from kernel space to user
483 * space. It supports simple types like char and int, but not larger
484 * data types like structures or arrays.
485 *
486 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
487 * to the result of dereferencing @ptr.
488 *
489 * Caller must check the pointer with access_ok() before calling this
490 * function.
491 *
492 * Returns zero on success, or -EFAULT on error.
493 */
494
495#define __put_user(x, ptr) \
496 __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
497
498#define __get_user_unaligned __get_user
499#define __put_user_unaligned __put_user
Glauber Costa865e5b72008-06-25 11:05:11 -0300500
Glauber Costa8bc7de0c2008-06-25 14:53:41 -0300501/*
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800502 * {get|put}_user_try and catch
503 *
504 * get_user_try {
505 * get_user_ex(...);
506 * } get_user_catch(err)
507 */
508#define get_user_try uaccess_try
509#define get_user_catch(err) uaccess_catch(err)
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800510
511#define get_user_ex(x, ptr) do { \
512 unsigned long __gue_val; \
513 __get_user_size_ex((__gue_val), (ptr), (sizeof(*(ptr)))); \
514 (x) = (__force __typeof__(*(ptr)))__gue_val; \
515} while (0)
516
Hiroshi Shimamoto019a1362009-01-29 11:49:18 -0800517#define put_user_try uaccess_try
518#define put_user_catch(err) uaccess_catch(err)
519
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800520#define put_user_ex(x, ptr) \
521 __put_user_size_ex((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
522
Robert Richter1ac2e6c2011-06-07 11:49:55 +0200523extern unsigned long
524copy_from_user_nmi(void *to, const void __user *from, unsigned long n);
Linus Torvalds92ae03f2012-04-06 14:32:32 -0700525extern __must_check long
526strncpy_from_user(char *dst, const char __user *src, long count);
Robert Richter1ac2e6c2011-06-07 11:49:55 +0200527
Linus Torvalds5723aa92012-05-26 11:09:53 -0700528extern __must_check long strlen_user(const char __user *str);
529extern __must_check long strnlen_user(const char __user *str, long n);
530
H. Peter Anvina0528582012-09-21 12:43:11 -0700531unsigned long __must_check clear_user(void __user *mem, unsigned long len);
532unsigned long __must_check __clear_user(void __user *mem, unsigned long len);
533
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800534/*
Glauber Costa8bc7de0c2008-06-25 14:53:41 -0300535 * movsl can be slow when source and dest are not both 8-byte aligned
536 */
537#ifdef CONFIG_X86_INTEL_USERCOPY
538extern struct movsl_mask {
539 int mask;
540} ____cacheline_aligned_in_smp movsl_mask;
541#endif
542
Glauber Costa22cac162008-06-25 14:56:53 -0300543#define ARCH_HAS_NOCACHE_UACCESS 1
544
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200545#ifdef CONFIG_X86_32
David Howellsa1ce3922012-10-02 18:01:25 +0100546# include <asm/uaccess_32.h>
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200547#else
David Howellsa1ce3922012-10-02 18:01:25 +0100548# include <asm/uaccess_64.h>
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200549#endif
Glauber Costaca233862008-06-13 14:39:25 -0300550
Jan Beulich3df7b412013-10-21 09:43:57 +0100551unsigned long __must_check _copy_from_user(void *to, const void __user *from,
552 unsigned n);
Jan Beulich7a3d9b02013-10-21 09:44:37 +0100553unsigned long __must_check _copy_to_user(void __user *to, const void *from,
554 unsigned n);
Jan Beulich3df7b412013-10-21 09:43:57 +0100555
556#ifdef CONFIG_DEBUG_STRICT_USER_COPY_CHECKS
557# define copy_user_diag __compiletime_error
558#else
559# define copy_user_diag __compiletime_warning
560#endif
561
562extern void copy_user_diag("copy_from_user() buffer size is too small")
563copy_from_user_overflow(void);
Jan Beulich7a3d9b02013-10-21 09:44:37 +0100564extern void copy_user_diag("copy_to_user() buffer size is too small")
565copy_to_user_overflow(void) __asm__("copy_from_user_overflow");
Jan Beulich3df7b412013-10-21 09:43:57 +0100566
567#undef copy_user_diag
568
569#ifdef CONFIG_DEBUG_STRICT_USER_COPY_CHECKS
570
571extern void
572__compiletime_warning("copy_from_user() buffer size is not provably correct")
573__copy_from_user_overflow(void) __asm__("copy_from_user_overflow");
574#define __copy_from_user_overflow(size, count) __copy_from_user_overflow()
575
Jan Beulich7a3d9b02013-10-21 09:44:37 +0100576extern void
577__compiletime_warning("copy_to_user() buffer size is not provably correct")
578__copy_to_user_overflow(void) __asm__("copy_from_user_overflow");
579#define __copy_to_user_overflow(size, count) __copy_to_user_overflow()
580
Jan Beulich3df7b412013-10-21 09:43:57 +0100581#else
582
583static inline void
584__copy_from_user_overflow(int size, unsigned long count)
585{
586 WARN(1, "Buffer overflow detected (%d < %lu)!\n", size, count);
587}
588
Jan Beulich7a3d9b02013-10-21 09:44:37 +0100589#define __copy_to_user_overflow __copy_from_user_overflow
590
Jan Beulich3df7b412013-10-21 09:43:57 +0100591#endif
592
593static inline unsigned long __must_check
594copy_from_user(void *to, const void __user *from, unsigned long n)
595{
596 int sz = __compiletime_object_size(to);
597
598 might_fault();
599
600 /*
601 * While we would like to have the compiler do the checking for us
602 * even in the non-constant size case, any false positives there are
603 * a problem (especially when DEBUG_STRICT_USER_COPY_CHECKS, but even
604 * without - the [hopefully] dangerous looking nature of the warning
605 * would make people go look at the respecitive call sites over and
606 * over again just to find that there's no problem).
607 *
608 * And there are cases where it's just not realistic for the compiler
609 * to prove the count to be in range. For example when multiple call
610 * sites of a helper function - perhaps in different source files -
611 * all doing proper range checking, yet the helper function not doing
612 * so again.
613 *
614 * Therefore limit the compile time checking to the constant size
615 * case, and do only runtime checking for non-constant sizes.
616 */
617
618 if (likely(sz < 0 || sz >= n))
619 n = _copy_from_user(to, from, n);
620 else if(__builtin_constant_p(n))
621 copy_from_user_overflow();
622 else
623 __copy_from_user_overflow(sz, n);
624
625 return n;
626}
627
Jan Beulich7a3d9b02013-10-21 09:44:37 +0100628static inline unsigned long __must_check
629copy_to_user(void __user *to, const void *from, unsigned long n)
630{
631 int sz = __compiletime_object_size(from);
632
633 might_fault();
634
635 /* See the comment in copy_from_user() above. */
636 if (likely(sz < 0 || sz >= n))
637 n = _copy_to_user(to, from, n);
638 else if(__builtin_constant_p(n))
639 copy_to_user_overflow();
640 else
641 __copy_to_user_overflow(sz, n);
642
643 return n;
644}
645
Jan Beulich3df7b412013-10-21 09:43:57 +0100646#undef __copy_from_user_overflow
Jan Beulich7a3d9b02013-10-21 09:44:37 +0100647#undef __copy_to_user_overflow
Jan Beulich3df7b412013-10-21 09:43:57 +0100648
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700649#endif /* _ASM_X86_UACCESS_H */
Nick Piggin8174c432008-07-25 19:45:24 -0700650