GNU Linux-libre 4.4.284-gnu1
[releases.git] / arch / x86 / include / asm / uaccess.h
1 #ifndef _ASM_X86_UACCESS_H
2 #define _ASM_X86_UACCESS_H
3 /*
4  * User space memory access functions
5  */
6 #include <linux/errno.h>
7 #include <linux/compiler.h>
8 #include <linux/thread_info.h>
9 #include <linux/string.h>
10 #include <linux/preempt.h>
11 #include <asm/asm.h>
12 #include <asm/page.h>
13 #include <asm/smap.h>
14
15 #define VERIFY_READ 0
16 #define VERIFY_WRITE 1
17
18 /*
19  * The fs value determines whether argument validity checking should be
20  * performed or not.  If get_fs() == USER_DS, checking is performed, with
21  * get_fs() == KERNEL_DS, checking is bypassed.
22  *
23  * For historical reasons, these macros are grossly misnamed.
24  */
25
26 #define MAKE_MM_SEG(s)  ((mm_segment_t) { (s) })
27
28 #define KERNEL_DS       MAKE_MM_SEG(-1UL)
29 #define USER_DS         MAKE_MM_SEG(TASK_SIZE_MAX)
30
31 #define get_ds()        (KERNEL_DS)
32 #define get_fs()        (current_thread_info()->addr_limit)
33 #define set_fs(x)       (current_thread_info()->addr_limit = (x))
34
35 #define segment_eq(a, b)        ((a).seg == (b).seg)
36
37 #define user_addr_max() (current_thread_info()->addr_limit.seg)
38 #define __addr_ok(addr)         \
39         ((unsigned long __force)(addr) < user_addr_max())
40
41 /*
42  * Test whether a block of memory is a valid user space address.
43  * Returns 0 if the range is valid, nonzero otherwise.
44  */
45 static inline bool __chk_range_not_ok(unsigned long addr, unsigned long size, unsigned long limit)
46 {
47         /*
48          * If we have used "sizeof()" for the size,
49          * we know it won't overflow the limit (but
50          * it might overflow the 'addr', so it's
51          * important to subtract the size from the
52          * limit, not add it to the address).
53          */
54         if (__builtin_constant_p(size))
55                 return unlikely(addr > limit - size);
56
57         /* Arbitrary sizes? Be careful about overflow */
58         addr += size;
59         if (unlikely(addr < size))
60                 return true;
61         return unlikely(addr > limit);
62 }
63
64 #define __range_not_ok(addr, size, limit)                               \
65 ({                                                                      \
66         __chk_user_ptr(addr);                                           \
67         __chk_range_not_ok((unsigned long __force)(addr), size, limit); \
68 })
69
70 #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
71 # define WARN_ON_IN_IRQ()       WARN_ON_ONCE(!in_task())
72 #else
73 # define WARN_ON_IN_IRQ()
74 #endif
75
76 /**
77  * access_ok: - Checks if a user space pointer is valid
78  * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE.  Note that
79  *        %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
80  *        to write to a block, it is always safe to read from it.
81  * @addr: User space pointer to start of block to check
82  * @size: Size of block to check
83  *
84  * Context: User context only. This function may sleep if pagefaults are
85  *          enabled.
86  *
87  * Checks if a pointer to a block of memory in user space is valid.
88  *
89  * Returns true (nonzero) if the memory block may be valid, false (zero)
90  * if it is definitely invalid.
91  *
92  * Note that, depending on architecture, this function probably just
93  * checks that the pointer is in the user space range - after calling
94  * this function, memory access functions may still return -EFAULT.
95  */
96 #define access_ok(type, addr, size)                                     \
97 ({                                                                      \
98         WARN_ON_IN_IRQ();                                               \
99         likely(!__range_not_ok(addr, size, user_addr_max()));           \
100 })
101
102 /*
103  * The exception table consists of pairs of addresses relative to the
104  * exception table enty itself: the first is the address of an
105  * instruction that is allowed to fault, and the second is the address
106  * at which the program should continue.  No registers are modified,
107  * so it is entirely up to the continuation code to figure out what to
108  * do.
109  *
110  * All the routines below use bits of fixup code that are out of line
111  * with the main instruction path.  This means when everything is well,
112  * we don't even have to jump over them.  Further, they do not intrude
113  * on our cache or tlb entries.
114  */
115
116 struct exception_table_entry {
117         int insn, fixup;
118 };
119 /* This is not the generic standard exception_table_entry format */
120 #define ARCH_HAS_SORT_EXTABLE
121 #define ARCH_HAS_SEARCH_EXTABLE
122
123 extern int fixup_exception(struct pt_regs *regs);
124 extern int early_fixup_exception(unsigned long *ip);
125
126 /*
127  * These are the main single-value transfer routines.  They automatically
128  * use the right size if we just have the right pointer type.
129  *
130  * This gets kind of ugly. We want to return _two_ values in "get_user()"
131  * and yet we don't want to do any pointers, because that is too much
132  * of a performance impact. Thus we have a few rather ugly macros here,
133  * and hide all the ugliness from the user.
134  *
135  * The "__xxx" versions of the user access functions are versions that
136  * do not verify the address space, that must have been done previously
137  * with a separate "access_ok()" call (this is used when we do multiple
138  * accesses to the same area of user memory).
139  */
140
141 extern int __get_user_1(void);
142 extern int __get_user_2(void);
143 extern int __get_user_4(void);
144 extern int __get_user_8(void);
145 extern int __get_user_bad(void);
146
147 #define __uaccess_begin() stac()
148 #define __uaccess_end()   clac()
149 #define __uaccess_begin_nospec()        \
150 ({                                      \
151         stac();                         \
152         barrier_nospec();               \
153 })
154
155 /*
156  * This is a type: either unsigned long, if the argument fits into
157  * that type, or otherwise unsigned long long.
158  */
159 #define __inttype(x) \
160 __typeof__(__builtin_choose_expr(sizeof(x) > sizeof(0UL), 0ULL, 0UL))
161
162 /**
163  * get_user: - Get a simple variable from user space.
164  * @x:   Variable to store result.
165  * @ptr: Source address, in user space.
166  *
167  * Context: User context only. This function may sleep if pagefaults are
168  *          enabled.
169  *
170  * This macro copies a single simple variable from user space to kernel
171  * space.  It supports simple types like char and int, but not larger
172  * data types like structures or arrays.
173  *
174  * @ptr must have pointer-to-simple-variable type, and the result of
175  * dereferencing @ptr must be assignable to @x without a cast.
176  *
177  * Returns zero on success, or -EFAULT on error.
178  * On error, the variable @x is set to zero.
179  */
180 /*
181  * Careful: we have to cast the result to the type of the pointer
182  * for sign reasons.
183  *
184  * The use of _ASM_DX as the register specifier is a bit of a
185  * simplification, as gcc only cares about it as the starting point
186  * and not size: for a 64-bit value it will use %ecx:%edx on 32 bits
187  * (%ecx being the next register in gcc's x86 register sequence), and
188  * %rdx on 64 bits.
189  *
190  * Clang/LLVM cares about the size of the register, but still wants
191  * the base register for something that ends up being a pair.
192  */
193 #define get_user(x, ptr)                                                \
194 ({                                                                      \
195         int __ret_gu;                                                   \
196         register __inttype(*(ptr)) __val_gu asm("%"_ASM_DX);            \
197         __chk_user_ptr(ptr);                                            \
198         might_fault();                                                  \
199         asm volatile("call __get_user_%P3"                              \
200                      : "=a" (__ret_gu), "=r" (__val_gu)                 \
201                      : "0" (ptr), "i" (sizeof(*(ptr))));                \
202         (x) = (__force __typeof__(*(ptr))) __val_gu;                    \
203         __builtin_expect(__ret_gu, 0);                                  \
204 })
205
206 #define __put_user_x(size, x, ptr, __ret_pu)                    \
207         asm volatile("call __put_user_" #size : "=a" (__ret_pu) \
208                      : "0" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx")
209
210
211
212 #ifdef CONFIG_X86_32
213 #define __put_user_asm_u64(x, addr, err, errret)                        \
214         asm volatile("\n"                                               \
215                      "1:        movl %%eax,0(%2)\n"                     \
216                      "2:        movl %%edx,4(%2)\n"                     \
217                      "3:"                                               \
218                      ".section .fixup,\"ax\"\n"                         \
219                      "4:        movl %3,%0\n"                           \
220                      "  jmp 3b\n"                                       \
221                      ".previous\n"                                      \
222                      _ASM_EXTABLE(1b, 4b)                               \
223                      _ASM_EXTABLE(2b, 4b)                               \
224                      : "=r" (err)                                       \
225                      : "A" (x), "r" (addr), "i" (errret), "0" (err))
226
227 #define __put_user_asm_ex_u64(x, addr)                                  \
228         asm volatile("\n"                                               \
229                      "1:        movl %%eax,0(%1)\n"                     \
230                      "2:        movl %%edx,4(%1)\n"                     \
231                      "3:"                                               \
232                      _ASM_EXTABLE_EX(1b, 2b)                            \
233                      _ASM_EXTABLE_EX(2b, 3b)                            \
234                      : : "A" (x), "r" (addr))
235
236 #define __put_user_x8(x, ptr, __ret_pu)                         \
237         asm volatile("call __put_user_8" : "=a" (__ret_pu)      \
238                      : "A" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx")
239 #else
240 #define __put_user_asm_u64(x, ptr, retval, errret) \
241         __put_user_asm(x, ptr, retval, "q", "", "er", errret)
242 #define __put_user_asm_ex_u64(x, addr)  \
243         __put_user_asm_ex(x, addr, "q", "", "er")
244 #define __put_user_x8(x, ptr, __ret_pu) __put_user_x(8, x, ptr, __ret_pu)
245 #endif
246
247 extern void __put_user_bad(void);
248
249 /*
250  * Strange magic calling convention: pointer in %ecx,
251  * value in %eax(:%edx), return value in %eax. clobbers %rbx
252  */
253 extern void __put_user_1(void);
254 extern void __put_user_2(void);
255 extern void __put_user_4(void);
256 extern void __put_user_8(void);
257
258 /**
259  * put_user: - Write a simple value into user space.
260  * @x:   Value to copy to user space.
261  * @ptr: Destination address, in user space.
262  *
263  * Context: User context only. This function may sleep if pagefaults are
264  *          enabled.
265  *
266  * This macro copies a single simple value from kernel space to user
267  * space.  It supports simple types like char and int, but not larger
268  * data types like structures or arrays.
269  *
270  * @ptr must have pointer-to-simple-variable type, and @x must be assignable
271  * to the result of dereferencing @ptr.
272  *
273  * Returns zero on success, or -EFAULT on error.
274  */
275 #define put_user(x, ptr)                                        \
276 ({                                                              \
277         int __ret_pu;                                           \
278         __typeof__(*(ptr)) __pu_val;                            \
279         __chk_user_ptr(ptr);                                    \
280         might_fault();                                          \
281         __pu_val = x;                                           \
282         switch (sizeof(*(ptr))) {                               \
283         case 1:                                                 \
284                 __put_user_x(1, __pu_val, ptr, __ret_pu);       \
285                 break;                                          \
286         case 2:                                                 \
287                 __put_user_x(2, __pu_val, ptr, __ret_pu);       \
288                 break;                                          \
289         case 4:                                                 \
290                 __put_user_x(4, __pu_val, ptr, __ret_pu);       \
291                 break;                                          \
292         case 8:                                                 \
293                 __put_user_x8(__pu_val, ptr, __ret_pu);         \
294                 break;                                          \
295         default:                                                \
296                 __put_user_x(X, __pu_val, ptr, __ret_pu);       \
297                 break;                                          \
298         }                                                       \
299         __builtin_expect(__ret_pu, 0);                          \
300 })
301
302 #define __put_user_size(x, ptr, size, retval, errret)                   \
303 do {                                                                    \
304         retval = 0;                                                     \
305         __chk_user_ptr(ptr);                                            \
306         switch (size) {                                                 \
307         case 1:                                                         \
308                 __put_user_asm(x, ptr, retval, "b", "b", "iq", errret); \
309                 break;                                                  \
310         case 2:                                                         \
311                 __put_user_asm(x, ptr, retval, "w", "w", "ir", errret); \
312                 break;                                                  \
313         case 4:                                                         \
314                 __put_user_asm(x, ptr, retval, "l", "k", "ir", errret); \
315                 break;                                                  \
316         case 8:                                                         \
317                 __put_user_asm_u64(x, ptr, retval, errret);             \
318                 break;                                                  \
319         default:                                                        \
320                 __put_user_bad();                                       \
321         }                                                               \
322 } while (0)
323
324 /*
325  * This doesn't do __uaccess_begin/end - the exception handling
326  * around it must do that.
327  */
328 #define __put_user_size_ex(x, ptr, size)                                \
329 do {                                                                    \
330         __chk_user_ptr(ptr);                                            \
331         switch (size) {                                                 \
332         case 1:                                                         \
333                 __put_user_asm_ex(x, ptr, "b", "b", "iq");              \
334                 break;                                                  \
335         case 2:                                                         \
336                 __put_user_asm_ex(x, ptr, "w", "w", "ir");              \
337                 break;                                                  \
338         case 4:                                                         \
339                 __put_user_asm_ex(x, ptr, "l", "k", "ir");              \
340                 break;                                                  \
341         case 8:                                                         \
342                 __put_user_asm_ex_u64((__typeof__(*ptr))(x), ptr);      \
343                 break;                                                  \
344         default:                                                        \
345                 __put_user_bad();                                       \
346         }                                                               \
347 } while (0)
348
349 #ifdef CONFIG_X86_32
350 #define __get_user_asm_u64(x, ptr, retval, errret)      (x) = __get_user_bad()
351 #define __get_user_asm_ex_u64(x, ptr)                   (x) = __get_user_bad()
352 #else
353 #define __get_user_asm_u64(x, ptr, retval, errret) \
354          __get_user_asm(x, ptr, retval, "q", "", "=r", errret)
355 #define __get_user_asm_ex_u64(x, ptr) \
356          __get_user_asm_ex(x, ptr, "q", "", "=&r")
357 #endif
358
359 #define __get_user_size(x, ptr, size, retval, errret)                   \
360 do {                                                                    \
361         retval = 0;                                                     \
362         __chk_user_ptr(ptr);                                            \
363         switch (size) {                                                 \
364         case 1:                                                         \
365                 __get_user_asm(x, ptr, retval, "b", "b", "=q", errret); \
366                 break;                                                  \
367         case 2:                                                         \
368                 __get_user_asm(x, ptr, retval, "w", "w", "=r", errret); \
369                 break;                                                  \
370         case 4:                                                         \
371                 __get_user_asm(x, ptr, retval, "l", "k", "=r", errret); \
372                 break;                                                  \
373         case 8:                                                         \
374                 __get_user_asm_u64(x, ptr, retval, errret);             \
375                 break;                                                  \
376         default:                                                        \
377                 (x) = __get_user_bad();                                 \
378         }                                                               \
379 } while (0)
380
381 #define __get_user_asm(x, addr, err, itype, rtype, ltype, errret)       \
382         asm volatile("\n"                                               \
383                      "1:        mov"itype" %2,%"rtype"1\n"              \
384                      "2:\n"                                             \
385                      ".section .fixup,\"ax\"\n"                         \
386                      "3:        mov %3,%0\n"                            \
387                      "  xor"itype" %"rtype"1,%"rtype"1\n"               \
388                      "  jmp 2b\n"                                       \
389                      ".previous\n"                                      \
390                      _ASM_EXTABLE(1b, 3b)                               \
391                      : "=r" (err), ltype(x)                             \
392                      : "m" (__m(addr)), "i" (errret), "0" (err))
393
394 /*
395  * This doesn't do __uaccess_begin/end - the exception handling
396  * around it must do that.
397  */
398 #define __get_user_size_ex(x, ptr, size)                                \
399 do {                                                                    \
400         __chk_user_ptr(ptr);                                            \
401         switch (size) {                                                 \
402         case 1:                                                         \
403                 __get_user_asm_ex(x, ptr, "b", "b", "=&q");             \
404                 break;                                                  \
405         case 2:                                                         \
406                 __get_user_asm_ex(x, ptr, "w", "w", "=&r");             \
407                 break;                                                  \
408         case 4:                                                         \
409                 __get_user_asm_ex(x, ptr, "l", "k", "=&r");             \
410                 break;                                                  \
411         case 8:                                                         \
412                 __get_user_asm_ex_u64(x, ptr);                          \
413                 break;                                                  \
414         default:                                                        \
415                 (x) = __get_user_bad();                                 \
416         }                                                               \
417 } while (0)
418
419 #define __get_user_asm_ex(x, addr, itype, rtype, ltype)                 \
420         asm volatile("1:        mov"itype" %1,%"rtype"0\n"              \
421                      "2:\n"                                             \
422                      _ASM_EXTABLE_EX(1b, 2b)                            \
423                      : ltype(x) : "m" (__m(addr)), "0" (0))
424
425 #define __put_user_nocheck(x, ptr, size)                        \
426 ({                                                              \
427         int __pu_err;                                           \
428         __typeof__(*(ptr)) __pu_val;                            \
429         __pu_val = x;                                           \
430         __uaccess_begin();                                      \
431         __put_user_size(__pu_val, (ptr), (size), __pu_err, -EFAULT);\
432         __uaccess_end();                                        \
433         __builtin_expect(__pu_err, 0);                          \
434 })
435
436 #define __get_user_nocheck(x, ptr, size)                                \
437 ({                                                                      \
438         int __gu_err;                                                   \
439         unsigned long __gu_val;                                         \
440         __uaccess_begin_nospec();                                       \
441         __get_user_size(__gu_val, (ptr), (size), __gu_err, -EFAULT);    \
442         __uaccess_end();                                                \
443         (x) = (__force __typeof__(*(ptr)))__gu_val;                     \
444         __builtin_expect(__gu_err, 0);                                  \
445 })
446
447 /* FIXME: this hack is definitely wrong -AK */
448 struct __large_struct { unsigned long buf[100]; };
449 #define __m(x) (*(struct __large_struct __user *)(x))
450
451 /*
452  * Tell gcc we read from memory instead of writing: this is because
453  * we do not write to any memory gcc knows about, so there are no
454  * aliasing issues.
455  */
456 #define __put_user_asm(x, addr, err, itype, rtype, ltype, errret)       \
457         asm volatile("\n"                                               \
458                      "1:        mov"itype" %"rtype"1,%2\n"              \
459                      "2:\n"                                             \
460                      ".section .fixup,\"ax\"\n"                         \
461                      "3:        mov %3,%0\n"                            \
462                      "  jmp 2b\n"                                       \
463                      ".previous\n"                                      \
464                      _ASM_EXTABLE(1b, 3b)                               \
465                      : "=r"(err)                                        \
466                      : ltype(x), "m" (__m(addr)), "i" (errret), "0" (err))
467
468 #define __put_user_asm_ex(x, addr, itype, rtype, ltype)                 \
469         asm volatile("1:        mov"itype" %"rtype"0,%1\n"              \
470                      "2:\n"                                             \
471                      _ASM_EXTABLE_EX(1b, 2b)                            \
472                      : : ltype(x), "m" (__m(addr)))
473
474 /*
475  * uaccess_try and catch
476  */
477 #define uaccess_try     do {                                            \
478         current_thread_info()->uaccess_err = 0;                         \
479         __uaccess_begin();                                              \
480         barrier();
481
482 #define uaccess_try_nospec do {                                         \
483         current_thread_info()->uaccess_err = 0;                         \
484         __uaccess_begin_nospec();                                       \
485
486 #define uaccess_catch(err)                                              \
487         __uaccess_end();                                                \
488         (err) |= (current_thread_info()->uaccess_err ? -EFAULT : 0);    \
489 } while (0)
490
491 /**
492  * __get_user: - Get a simple variable from user space, with less checking.
493  * @x:   Variable to store result.
494  * @ptr: Source address, in user space.
495  *
496  * Context: User context only. This function may sleep if pagefaults are
497  *          enabled.
498  *
499  * This macro copies a single simple variable from user space to kernel
500  * space.  It supports simple types like char and int, but not larger
501  * data types like structures or arrays.
502  *
503  * @ptr must have pointer-to-simple-variable type, and the result of
504  * dereferencing @ptr must be assignable to @x without a cast.
505  *
506  * Caller must check the pointer with access_ok() before calling this
507  * function.
508  *
509  * Returns zero on success, or -EFAULT on error.
510  * On error, the variable @x is set to zero.
511  */
512
513 #define __get_user(x, ptr)                                              \
514         __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
515
516 /**
517  * __put_user: - Write a simple value into user space, with less checking.
518  * @x:   Value to copy to user space.
519  * @ptr: Destination address, in user space.
520  *
521  * Context: User context only. This function may sleep if pagefaults are
522  *          enabled.
523  *
524  * This macro copies a single simple value from kernel space to user
525  * space.  It supports simple types like char and int, but not larger
526  * data types like structures or arrays.
527  *
528  * @ptr must have pointer-to-simple-variable type, and @x must be assignable
529  * to the result of dereferencing @ptr.
530  *
531  * Caller must check the pointer with access_ok() before calling this
532  * function.
533  *
534  * Returns zero on success, or -EFAULT on error.
535  */
536
537 #define __put_user(x, ptr)                                              \
538         __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
539
540 #define __get_user_unaligned __get_user
541 #define __put_user_unaligned __put_user
542
543 /*
544  * {get|put}_user_try and catch
545  *
546  * get_user_try {
547  *      get_user_ex(...);
548  * } get_user_catch(err)
549  */
550 #define get_user_try            uaccess_try_nospec
551 #define get_user_catch(err)     uaccess_catch(err)
552
553 #define get_user_ex(x, ptr)     do {                                    \
554         unsigned long __gue_val;                                        \
555         __get_user_size_ex((__gue_val), (ptr), (sizeof(*(ptr))));       \
556         (x) = (__force __typeof__(*(ptr)))__gue_val;                    \
557 } while (0)
558
559 #define put_user_try            uaccess_try
560 #define put_user_catch(err)     uaccess_catch(err)
561
562 #define put_user_ex(x, ptr)                                             \
563         __put_user_size_ex((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
564
565 extern unsigned long
566 copy_from_user_nmi(void *to, const void __user *from, unsigned long n);
567 extern __must_check long
568 strncpy_from_user(char *dst, const char __user *src, long count);
569
570 extern __must_check long strlen_user(const char __user *str);
571 extern __must_check long strnlen_user(const char __user *str, long n);
572
573 unsigned long __must_check clear_user(void __user *mem, unsigned long len);
574 unsigned long __must_check __clear_user(void __user *mem, unsigned long len);
575
576 extern void __cmpxchg_wrong_size(void)
577         __compiletime_error("Bad argument size for cmpxchg");
578
579 #define __user_atomic_cmpxchg_inatomic(uval, ptr, old, new, size)       \
580 ({                                                                      \
581         int __ret = 0;                                                  \
582         __typeof__(ptr) __uval = (uval);                                \
583         __typeof__(*(ptr)) __old = (old);                               \
584         __typeof__(*(ptr)) __new = (new);                               \
585         __uaccess_begin_nospec();                                       \
586         switch (size) {                                                 \
587         case 1:                                                         \
588         {                                                               \
589                 asm volatile("\n"                                       \
590                         "1:\t" LOCK_PREFIX "cmpxchgb %4, %2\n"          \
591                         "2:\n"                                          \
592                         "\t.section .fixup, \"ax\"\n"                   \
593                         "3:\tmov     %3, %0\n"                          \
594                         "\tjmp     2b\n"                                \
595                         "\t.previous\n"                                 \
596                         _ASM_EXTABLE(1b, 3b)                            \
597                         : "+r" (__ret), "=a" (__old), "+m" (*(ptr))     \
598                         : "i" (-EFAULT), "q" (__new), "1" (__old)       \
599                         : "memory"                                      \
600                 );                                                      \
601                 break;                                                  \
602         }                                                               \
603         case 2:                                                         \
604         {                                                               \
605                 asm volatile("\n"                                       \
606                         "1:\t" LOCK_PREFIX "cmpxchgw %4, %2\n"          \
607                         "2:\n"                                          \
608                         "\t.section .fixup, \"ax\"\n"                   \
609                         "3:\tmov     %3, %0\n"                          \
610                         "\tjmp     2b\n"                                \
611                         "\t.previous\n"                                 \
612                         _ASM_EXTABLE(1b, 3b)                            \
613                         : "+r" (__ret), "=a" (__old), "+m" (*(ptr))     \
614                         : "i" (-EFAULT), "r" (__new), "1" (__old)       \
615                         : "memory"                                      \
616                 );                                                      \
617                 break;                                                  \
618         }                                                               \
619         case 4:                                                         \
620         {                                                               \
621                 asm volatile("\n"                                       \
622                         "1:\t" LOCK_PREFIX "cmpxchgl %4, %2\n"          \
623                         "2:\n"                                          \
624                         "\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         case 8:                                                         \
636         {                                                               \
637                 if (!IS_ENABLED(CONFIG_X86_64))                         \
638                         __cmpxchg_wrong_size();                         \
639                                                                         \
640                 asm volatile("\n"                                       \
641                         "1:\t" LOCK_PREFIX "cmpxchgq %4, %2\n"          \
642                         "2:\n"                                          \
643                         "\t.section .fixup, \"ax\"\n"                   \
644                         "3:\tmov     %3, %0\n"                          \
645                         "\tjmp     2b\n"                                \
646                         "\t.previous\n"                                 \
647                         _ASM_EXTABLE(1b, 3b)                            \
648                         : "+r" (__ret), "=a" (__old), "+m" (*(ptr))     \
649                         : "i" (-EFAULT), "r" (__new), "1" (__old)       \
650                         : "memory"                                      \
651                 );                                                      \
652                 break;                                                  \
653         }                                                               \
654         default:                                                        \
655                 __cmpxchg_wrong_size();                                 \
656         }                                                               \
657         __uaccess_end();                                                \
658         *__uval = __old;                                                \
659         __ret;                                                          \
660 })
661
662 #define user_atomic_cmpxchg_inatomic(uval, ptr, old, new)               \
663 ({                                                                      \
664         access_ok(VERIFY_WRITE, (ptr), sizeof(*(ptr))) ?                \
665                 __user_atomic_cmpxchg_inatomic((uval), (ptr),           \
666                                 (old), (new), sizeof(*(ptr))) :         \
667                 -EFAULT;                                                \
668 })
669
670 /*
671  * movsl can be slow when source and dest are not both 8-byte aligned
672  */
673 #ifdef CONFIG_X86_INTEL_USERCOPY
674 extern struct movsl_mask {
675         int mask;
676 } ____cacheline_aligned_in_smp movsl_mask;
677 #endif
678
679 #define ARCH_HAS_NOCACHE_UACCESS 1
680
681 #ifdef CONFIG_X86_32
682 # include <asm/uaccess_32.h>
683 #else
684 # include <asm/uaccess_64.h>
685 #endif
686
687 unsigned long __must_check _copy_from_user(void *to, const void __user *from,
688                                            unsigned n);
689 unsigned long __must_check _copy_to_user(void __user *to, const void *from,
690                                          unsigned n);
691
692 #ifdef CONFIG_DEBUG_STRICT_USER_COPY_CHECKS
693 # define copy_user_diag __compiletime_error
694 #else
695 # define copy_user_diag __compiletime_warning
696 #endif
697
698 extern void copy_user_diag("copy_from_user() buffer size is too small")
699 copy_from_user_overflow(void);
700 extern void copy_user_diag("copy_to_user() buffer size is too small")
701 copy_to_user_overflow(void) __asm__("copy_from_user_overflow");
702
703 #undef copy_user_diag
704
705 #ifdef CONFIG_DEBUG_STRICT_USER_COPY_CHECKS
706
707 extern void
708 __compiletime_warning("copy_from_user() buffer size is not provably correct")
709 __copy_from_user_overflow(void) __asm__("copy_from_user_overflow");
710 #define __copy_from_user_overflow(size, count) __copy_from_user_overflow()
711
712 extern void
713 __compiletime_warning("copy_to_user() buffer size is not provably correct")
714 __copy_to_user_overflow(void) __asm__("copy_from_user_overflow");
715 #define __copy_to_user_overflow(size, count) __copy_to_user_overflow()
716
717 #else
718
719 static inline void
720 __copy_from_user_overflow(int size, unsigned long count)
721 {
722         WARN(1, "Buffer overflow detected (%d < %lu)!\n", size, count);
723 }
724
725 #define __copy_to_user_overflow __copy_from_user_overflow
726
727 #endif
728
729 static inline unsigned long __must_check
730 copy_from_user(void *to, const void __user *from, unsigned long n)
731 {
732         int sz = __compiletime_object_size(to);
733
734         might_fault();
735
736         /*
737          * While we would like to have the compiler do the checking for us
738          * even in the non-constant size case, any false positives there are
739          * a problem (especially when DEBUG_STRICT_USER_COPY_CHECKS, but even
740          * without - the [hopefully] dangerous looking nature of the warning
741          * would make people go look at the respecitive call sites over and
742          * over again just to find that there's no problem).
743          *
744          * And there are cases where it's just not realistic for the compiler
745          * to prove the count to be in range. For example when multiple call
746          * sites of a helper function - perhaps in different source files -
747          * all doing proper range checking, yet the helper function not doing
748          * so again.
749          *
750          * Therefore limit the compile time checking to the constant size
751          * case, and do only runtime checking for non-constant sizes.
752          */
753
754         if (likely(sz < 0 || sz >= n))
755                 n = _copy_from_user(to, from, n);
756         else if(__builtin_constant_p(n))
757                 copy_from_user_overflow();
758         else
759                 __copy_from_user_overflow(sz, n);
760
761         return n;
762 }
763
764 static inline unsigned long __must_check
765 copy_to_user(void __user *to, const void *from, unsigned long n)
766 {
767         int sz = __compiletime_object_size(from);
768
769         might_fault();
770
771         /* See the comment in copy_from_user() above. */
772         if (likely(sz < 0 || sz >= n))
773                 n = _copy_to_user(to, from, n);
774         else if(__builtin_constant_p(n))
775                 copy_to_user_overflow();
776         else
777                 __copy_to_user_overflow(sz, n);
778
779         return n;
780 }
781
782 #undef __copy_from_user_overflow
783 #undef __copy_to_user_overflow
784
785 #endif /* _ASM_X86_UACCESS_H */
786