4111edb3188e2531e04167ab41def2aa6422ec87
[platform/kernel/linux-rpi.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  * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE.  Note that
81  *        %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
82  *        to write to a block, it is always safe to read from it.
83  * @addr: User space pointer to start of block to check
84  * @size: Size of block to check
85  *
86  * Context: User context only. This function may sleep if pagefaults are
87  *          enabled.
88  *
89  * Checks if a pointer to a block of memory in user space is valid.
90  *
91  * Returns true (nonzero) if the memory block may be valid, false (zero)
92  * if it is definitely invalid.
93  *
94  * Note that, depending on architecture, this function probably just
95  * checks that the pointer is in the user space range - after calling
96  * this function, memory access functions may still return -EFAULT.
97  */
98 #define access_ok(type, addr, size)                                     \
99 ({                                                                      \
100         WARN_ON_IN_IRQ();                                               \
101         likely(!__range_not_ok(addr, size, user_addr_max()));           \
102 })
103
104 /*
105  * These are the main single-value transfer routines.  They automatically
106  * use the right size if we just have the right pointer type.
107  *
108  * This gets kind of ugly. We want to return _two_ values in "get_user()"
109  * and yet we don't want to do any pointers, because that is too much
110  * of a performance impact. Thus we have a few rather ugly macros here,
111  * and hide all the ugliness from the user.
112  *
113  * The "__xxx" versions of the user access functions are versions that
114  * do not verify the address space, that must have been done previously
115  * with a separate "access_ok()" call (this is used when we do multiple
116  * accesses to the same area of user memory).
117  */
118
119 extern int __get_user_1(void);
120 extern int __get_user_2(void);
121 extern int __get_user_4(void);
122 extern int __get_user_8(void);
123 extern int __get_user_bad(void);
124
125 #define __uaccess_begin() stac()
126 #define __uaccess_end()   clac()
127 #define __uaccess_begin_nospec()        \
128 ({                                      \
129         stac();                         \
130         barrier_nospec();               \
131 })
132
133 /*
134  * This is a type: either unsigned long, if the argument fits into
135  * that type, or otherwise unsigned long long.
136  */
137 #define __inttype(x) \
138 __typeof__(__builtin_choose_expr(sizeof(x) > sizeof(0UL), 0ULL, 0UL))
139
140 /**
141  * get_user: - Get a simple variable from user space.
142  * @x:   Variable to store result.
143  * @ptr: Source address, in user space.
144  *
145  * Context: User context only. This function may sleep if pagefaults are
146  *          enabled.
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.
157  */
158 /*
159  * Careful: we have to cast the result to the type of the pointer
160  * for sign reasons.
161  *
162  * The use of _ASM_DX as the register specifier is a bit of a
163  * 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.
167  *
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.
170  */
171 #define get_user(x, ptr)                                                \
172 ({                                                                      \
173         int __ret_gu;                                                   \
174         register __inttype(*(ptr)) __val_gu asm("%"_ASM_DX);            \
175         __chk_user_ptr(ptr);                                            \
176         might_fault();                                                  \
177         asm volatile("call __get_user_%P4"                              \
178                      : "=a" (__ret_gu), "=r" (__val_gu),                \
179                         ASM_CALL_CONSTRAINT                             \
180                      : "0" (ptr), "i" (sizeof(*(ptr))));                \
181         (x) = (__force __typeof__(*(ptr))) __val_gu;                    \
182         __builtin_expect(__ret_gu, 0);                                  \
183 })
184
185 #define __put_user_x(size, x, ptr, __ret_pu)                    \
186         asm volatile("call __put_user_" #size : "=a" (__ret_pu) \
187                      : "0" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx")
188
189
190
191 #ifdef CONFIG_X86_32
192 #define __put_user_asm_u64(x, addr, err, errret)                        \
193         asm volatile("\n"                                               \
194                      "1:        movl %%eax,0(%2)\n"                     \
195                      "2:        movl %%edx,4(%2)\n"                     \
196                      "3:"                                               \
197                      ".section .fixup,\"ax\"\n"                         \
198                      "4:        movl %3,%0\n"                           \
199                      "  jmp 3b\n"                                       \
200                      ".previous\n"                                      \
201                      _ASM_EXTABLE(1b, 4b)                               \
202                      _ASM_EXTABLE(2b, 4b)                               \
203                      : "=r" (err)                                       \
204                      : "A" (x), "r" (addr), "i" (errret), "0" (err))
205
206 #define __put_user_asm_ex_u64(x, addr)                                  \
207         asm volatile("\n"                                               \
208                      "1:        movl %%eax,0(%1)\n"                     \
209                      "2:        movl %%edx,4(%1)\n"                     \
210                      "3:"                                               \
211                      _ASM_EXTABLE_EX(1b, 2b)                            \
212                      _ASM_EXTABLE_EX(2b, 3b)                            \
213                      : : "A" (x), "r" (addr))
214
215 #define __put_user_x8(x, ptr, __ret_pu)                         \
216         asm volatile("call __put_user_8" : "=a" (__ret_pu)      \
217                      : "A" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx")
218 #else
219 #define __put_user_asm_u64(x, ptr, retval, errret) \
220         __put_user_asm(x, ptr, retval, "q", "", "er", errret)
221 #define __put_user_asm_ex_u64(x, addr)  \
222         __put_user_asm_ex(x, addr, "q", "", "er")
223 #define __put_user_x8(x, ptr, __ret_pu) __put_user_x(8, x, ptr, __ret_pu)
224 #endif
225
226 extern void __put_user_bad(void);
227
228 /*
229  * Strange magic calling convention: pointer in %ecx,
230  * value in %eax(:%edx), return value in %eax. clobbers %rbx
231  */
232 extern void __put_user_1(void);
233 extern void __put_user_2(void);
234 extern void __put_user_4(void);
235 extern void __put_user_8(void);
236
237 /**
238  * put_user: - Write a simple value into user space.
239  * @x:   Value to copy to user space.
240  * @ptr: Destination address, in user space.
241  *
242  * Context: User context only. This function may sleep if pagefaults are
243  *          enabled.
244  *
245  * This macro copies a single simple value from kernel space to user
246  * space.  It supports simple types like char and int, but not larger
247  * data types like structures or arrays.
248  *
249  * @ptr must have pointer-to-simple-variable type, and @x must be assignable
250  * to the result of dereferencing @ptr.
251  *
252  * Returns zero on success, or -EFAULT on error.
253  */
254 #define put_user(x, ptr)                                        \
255 ({                                                              \
256         int __ret_pu;                                           \
257         __typeof__(*(ptr)) __pu_val;                            \
258         __chk_user_ptr(ptr);                                    \
259         might_fault();                                          \
260         __pu_val = x;                                           \
261         switch (sizeof(*(ptr))) {                               \
262         case 1:                                                 \
263                 __put_user_x(1, __pu_val, ptr, __ret_pu);       \
264                 break;                                          \
265         case 2:                                                 \
266                 __put_user_x(2, __pu_val, ptr, __ret_pu);       \
267                 break;                                          \
268         case 4:                                                 \
269                 __put_user_x(4, __pu_val, ptr, __ret_pu);       \
270                 break;                                          \
271         case 8:                                                 \
272                 __put_user_x8(__pu_val, ptr, __ret_pu);         \
273                 break;                                          \
274         default:                                                \
275                 __put_user_x(X, __pu_val, ptr, __ret_pu);       \
276                 break;                                          \
277         }                                                       \
278         __builtin_expect(__ret_pu, 0);                          \
279 })
280
281 #define __put_user_size(x, ptr, size, retval, errret)                   \
282 do {                                                                    \
283         retval = 0;                                                     \
284         __chk_user_ptr(ptr);                                            \
285         switch (size) {                                                 \
286         case 1:                                                         \
287                 __put_user_asm(x, ptr, retval, "b", "b", "iq", errret); \
288                 break;                                                  \
289         case 2:                                                         \
290                 __put_user_asm(x, ptr, retval, "w", "w", "ir", errret); \
291                 break;                                                  \
292         case 4:                                                         \
293                 __put_user_asm(x, ptr, retval, "l", "k", "ir", errret); \
294                 break;                                                  \
295         case 8:                                                         \
296                 __put_user_asm_u64(x, ptr, retval, errret);             \
297                 break;                                                  \
298         default:                                                        \
299                 __put_user_bad();                                       \
300         }                                                               \
301 } while (0)
302
303 /*
304  * This doesn't do __uaccess_begin/end - the exception handling
305  * around it must do that.
306  */
307 #define __put_user_size_ex(x, ptr, size)                                \
308 do {                                                                    \
309         __chk_user_ptr(ptr);                                            \
310         switch (size) {                                                 \
311         case 1:                                                         \
312                 __put_user_asm_ex(x, ptr, "b", "b", "iq");              \
313                 break;                                                  \
314         case 2:                                                         \
315                 __put_user_asm_ex(x, ptr, "w", "w", "ir");              \
316                 break;                                                  \
317         case 4:                                                         \
318                 __put_user_asm_ex(x, ptr, "l", "k", "ir");              \
319                 break;                                                  \
320         case 8:                                                         \
321                 __put_user_asm_ex_u64((__typeof__(*ptr))(x), ptr);      \
322                 break;                                                  \
323         default:                                                        \
324                 __put_user_bad();                                       \
325         }                                                               \
326 } while (0)
327
328 #ifdef CONFIG_X86_32
329 #define __get_user_asm_u64(x, ptr, retval, errret)                      \
330 ({                                                                      \
331         __typeof__(ptr) __ptr = (ptr);                                  \
332         asm volatile("\n"                                       \
333                      "1:        movl %2,%%eax\n"                        \
334                      "2:        movl %3,%%edx\n"                        \
335                      "3:\n"                             \
336                      ".section .fixup,\"ax\"\n"                         \
337                      "4:        mov %4,%0\n"                            \
338                      "  xorl %%eax,%%eax\n"                             \
339                      "  xorl %%edx,%%edx\n"                             \
340                      "  jmp 3b\n"                                       \
341                      ".previous\n"                                      \
342                      _ASM_EXTABLE(1b, 4b)                               \
343                      _ASM_EXTABLE(2b, 4b)                               \
344                      : "=r" (retval), "=&A"(x)                          \
345                      : "m" (__m(__ptr)), "m" __m(((u32 __user *)(__ptr)) + 1),  \
346                        "i" (errret), "0" (retval));                     \
347 })
348
349 #define __get_user_asm_ex_u64(x, ptr)                   (x) = __get_user_bad()
350 #else
351 #define __get_user_asm_u64(x, ptr, retval, errret) \
352          __get_user_asm(x, ptr, retval, "q", "", "=r", errret)
353 #define __get_user_asm_ex_u64(x, ptr) \
354          __get_user_asm_ex(x, ptr, "q", "", "=r")
355 #endif
356
357 #define __get_user_size(x, ptr, size, retval, errret)                   \
358 do {                                                                    \
359         retval = 0;                                                     \
360         __chk_user_ptr(ptr);                                            \
361         switch (size) {                                                 \
362         case 1:                                                         \
363                 __get_user_asm(x, ptr, retval, "b", "b", "=q", errret); \
364                 break;                                                  \
365         case 2:                                                         \
366                 __get_user_asm(x, ptr, retval, "w", "w", "=r", errret); \
367                 break;                                                  \
368         case 4:                                                         \
369                 __get_user_asm(x, ptr, retval, "l", "k", "=r", errret); \
370                 break;                                                  \
371         case 8:                                                         \
372                 __get_user_asm_u64(x, ptr, retval, errret);             \
373                 break;                                                  \
374         default:                                                        \
375                 (x) = __get_user_bad();                                 \
376         }                                                               \
377 } while (0)
378
379 #define __get_user_asm(x, addr, err, itype, rtype, ltype, errret)       \
380         asm volatile("\n"                                               \
381                      "1:        mov"itype" %2,%"rtype"1\n"              \
382                      "2:\n"                                             \
383                      ".section .fixup,\"ax\"\n"                         \
384                      "3:        mov %3,%0\n"                            \
385                      "  xor"itype" %"rtype"1,%"rtype"1\n"               \
386                      "  jmp 2b\n"                                       \
387                      ".previous\n"                                      \
388                      _ASM_EXTABLE(1b, 3b)                               \
389                      : "=r" (err), ltype(x)                             \
390                      : "m" (__m(addr)), "i" (errret), "0" (err))
391
392 #define __get_user_asm_nozero(x, addr, err, itype, rtype, ltype, errret)        \
393         asm volatile("\n"                                               \
394                      "1:        mov"itype" %2,%"rtype"1\n"              \
395                      "2:\n"                                             \
396                      ".section .fixup,\"ax\"\n"                         \
397                      "3:        mov %3,%0\n"                            \
398                      "  jmp 2b\n"                                       \
399                      ".previous\n"                                      \
400                      _ASM_EXTABLE(1b, 3b)                               \
401                      : "=r" (err), ltype(x)                             \
402                      : "m" (__m(addr)), "i" (errret), "0" (err))
403
404 /*
405  * This doesn't do __uaccess_begin/end - the exception handling
406  * around it must do that.
407  */
408 #define __get_user_size_ex(x, ptr, size)                                \
409 do {                                                                    \
410         __chk_user_ptr(ptr);                                            \
411         switch (size) {                                                 \
412         case 1:                                                         \
413                 __get_user_asm_ex(x, ptr, "b", "b", "=q");              \
414                 break;                                                  \
415         case 2:                                                         \
416                 __get_user_asm_ex(x, ptr, "w", "w", "=r");              \
417                 break;                                                  \
418         case 4:                                                         \
419                 __get_user_asm_ex(x, ptr, "l", "k", "=r");              \
420                 break;                                                  \
421         case 8:                                                         \
422                 __get_user_asm_ex_u64(x, ptr);                          \
423                 break;                                                  \
424         default:                                                        \
425                 (x) = __get_user_bad();                                 \
426         }                                                               \
427 } while (0)
428
429 #define __get_user_asm_ex(x, addr, itype, rtype, ltype)                 \
430         asm volatile("1:        mov"itype" %1,%"rtype"0\n"              \
431                      "2:\n"                                             \
432                      ".section .fixup,\"ax\"\n"                         \
433                      "3:xor"itype" %"rtype"0,%"rtype"0\n"               \
434                      "  jmp 2b\n"                                       \
435                      ".previous\n"                                      \
436                      _ASM_EXTABLE_EX(1b, 3b)                            \
437                      : ltype(x) : "m" (__m(addr)))
438
439 #define __put_user_nocheck(x, ptr, size)                        \
440 ({                                                              \
441         int __pu_err;                                           \
442         __typeof__(*(ptr)) __pu_val;                            \
443         __pu_val = x;                                           \
444         __uaccess_begin();                                      \
445         __put_user_size(__pu_val, (ptr), (size), __pu_err, -EFAULT);\
446         __uaccess_end();                                        \
447         __builtin_expect(__pu_err, 0);                          \
448 })
449
450 #define __get_user_nocheck(x, ptr, size)                                \
451 ({                                                                      \
452         int __gu_err;                                                   \
453         __inttype(*(ptr)) __gu_val;                                     \
454         __uaccess_begin_nospec();                                       \
455         __get_user_size(__gu_val, (ptr), (size), __gu_err, -EFAULT);    \
456         __uaccess_end();                                                \
457         (x) = (__force __typeof__(*(ptr)))__gu_val;                     \
458         __builtin_expect(__gu_err, 0);                                  \
459 })
460
461 /* FIXME: this hack is definitely wrong -AK */
462 struct __large_struct { unsigned long buf[100]; };
463 #define __m(x) (*(struct __large_struct __user *)(x))
464
465 /*
466  * Tell gcc we read from memory instead of writing: this is because
467  * we do not write to any memory gcc knows about, so there are no
468  * aliasing issues.
469  */
470 #define __put_user_asm(x, addr, err, itype, rtype, ltype, errret)       \
471         asm volatile("\n"                                               \
472                      "1:        mov"itype" %"rtype"1,%2\n"              \
473                      "2:\n"                                             \
474                      ".section .fixup,\"ax\"\n"                         \
475                      "3:        mov %3,%0\n"                            \
476                      "  jmp 2b\n"                                       \
477                      ".previous\n"                                      \
478                      _ASM_EXTABLE(1b, 3b)                               \
479                      : "=r"(err)                                        \
480                      : ltype(x), "m" (__m(addr)), "i" (errret), "0" (err))
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(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(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(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(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(VERIFY_WRITE, (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 #define user_access_begin()     __uaccess_begin()
713 #define user_access_end()       __uaccess_end()
714
715 #define unsafe_put_user(x, ptr, err_label)                                      \
716 do {                                                                            \
717         int __pu_err;                                                           \
718         __typeof__(*(ptr)) __pu_val = (x);                                      \
719         __put_user_size(__pu_val, (ptr), sizeof(*(ptr)), __pu_err, -EFAULT);    \
720         if (unlikely(__pu_err)) goto err_label;                                 \
721 } while (0)
722
723 #define unsafe_get_user(x, ptr, err_label)                                      \
724 do {                                                                            \
725         int __gu_err;                                                           \
726         __inttype(*(ptr)) __gu_val;                                             \
727         __get_user_size(__gu_val, (ptr), sizeof(*(ptr)), __gu_err, -EFAULT);    \
728         (x) = (__force __typeof__(*(ptr)))__gu_val;                             \
729         if (unlikely(__gu_err)) goto err_label;                                 \
730 } while (0)
731
732 #endif /* _ASM_X86_UACCESS_H */
733