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