Merge branch 'work.set_fs' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[linux-2.6-microblaze.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  * Test whether a block of memory is a valid user space address.
17  * Returns 0 if the range is valid, nonzero otherwise.
18  */
19 static inline bool __chk_range_not_ok(unsigned long addr, unsigned long size, unsigned long limit)
20 {
21         /*
22          * If we have used "sizeof()" for the size,
23          * we know it won't overflow the limit (but
24          * it might overflow the 'addr', so it's
25          * important to subtract the size from the
26          * limit, not add it to the address).
27          */
28         if (__builtin_constant_p(size))
29                 return unlikely(addr > limit - size);
30
31         /* Arbitrary sizes? Be careful about overflow */
32         addr += size;
33         if (unlikely(addr < size))
34                 return true;
35         return unlikely(addr > limit);
36 }
37
38 #define __range_not_ok(addr, size, limit)                               \
39 ({                                                                      \
40         __chk_user_ptr(addr);                                           \
41         __chk_range_not_ok((unsigned long __force)(addr), size, limit); \
42 })
43
44 #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
45 static inline bool pagefault_disabled(void);
46 # define WARN_ON_IN_IRQ()       \
47         WARN_ON_ONCE(!in_task() && !pagefault_disabled())
48 #else
49 # define WARN_ON_IN_IRQ()
50 #endif
51
52 /**
53  * access_ok - Checks if a user space pointer is valid
54  * @addr: User space pointer to start of block to check
55  * @size: Size of block to check
56  *
57  * Context: User context only. This function may sleep if pagefaults are
58  *          enabled.
59  *
60  * Checks if a pointer to a block of memory in user space is valid.
61  *
62  * Note that, depending on architecture, this function probably just
63  * checks that the pointer is in the user space range - after calling
64  * this function, memory access functions may still return -EFAULT.
65  *
66  * Return: true (nonzero) if the memory block may be valid, false (zero)
67  * if it is definitely invalid.
68  */
69 #define access_ok(addr, size)                                   \
70 ({                                                                      \
71         WARN_ON_IN_IRQ();                                               \
72         likely(!__range_not_ok(addr, size, TASK_SIZE_MAX));             \
73 })
74
75 extern int __get_user_1(void);
76 extern int __get_user_2(void);
77 extern int __get_user_4(void);
78 extern int __get_user_8(void);
79 extern int __get_user_nocheck_1(void);
80 extern int __get_user_nocheck_2(void);
81 extern int __get_user_nocheck_4(void);
82 extern int __get_user_nocheck_8(void);
83 extern int __get_user_bad(void);
84
85 #define __uaccess_begin() stac()
86 #define __uaccess_end()   clac()
87 #define __uaccess_begin_nospec()        \
88 ({                                      \
89         stac();                         \
90         barrier_nospec();               \
91 })
92
93 /*
94  * This is the smallest unsigned integer type that can fit a value
95  * (up to 'long long')
96  */
97 #define __inttype(x) __typeof__(                \
98         __typefits(x,char,                      \
99           __typefits(x,short,                   \
100             __typefits(x,int,                   \
101               __typefits(x,long,0ULL)))))
102
103 #define __typefits(x,type,not) \
104         __builtin_choose_expr(sizeof(x)<=sizeof(type),(unsigned type)0,not)
105
106 /*
107  * This is used for both get_user() and __get_user() to expand to
108  * the proper special function call that has odd calling conventions
109  * due to returning both a value and an error, and that depends on
110  * the size of the pointer passed in.
111  *
112  * Careful: we have to cast the result to the type of the pointer
113  * for sign reasons.
114  *
115  * The use of _ASM_DX as the register specifier is a bit of a
116  * simplification, as gcc only cares about it as the starting point
117  * and not size: for a 64-bit value it will use %ecx:%edx on 32 bits
118  * (%ecx being the next register in gcc's x86 register sequence), and
119  * %rdx on 64 bits.
120  *
121  * Clang/LLVM cares about the size of the register, but still wants
122  * the base register for something that ends up being a pair.
123  */
124 #define do_get_user_call(fn,x,ptr)                                      \
125 ({                                                                      \
126         int __ret_gu;                                                   \
127         register __inttype(*(ptr)) __val_gu asm("%"_ASM_DX);            \
128         __chk_user_ptr(ptr);                                            \
129         asm volatile("call __" #fn "_%P4"                               \
130                      : "=a" (__ret_gu), "=r" (__val_gu),                \
131                         ASM_CALL_CONSTRAINT                             \
132                      : "0" (ptr), "i" (sizeof(*(ptr))));                \
133         (x) = (__force __typeof__(*(ptr))) __val_gu;                    \
134         __builtin_expect(__ret_gu, 0);                                  \
135 })
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  * Return: zero on success, or -EFAULT on error.
153  * On error, the variable @x is set to zero.
154  */
155 #define get_user(x,ptr) ({ might_fault(); do_get_user_call(get_user,x,ptr); })
156
157 /**
158  * __get_user - Get a simple variable from user space, with less checking.
159  * @x:   Variable to store result.
160  * @ptr: Source address, in user space.
161  *
162  * Context: User context only. This function may sleep if pagefaults are
163  *          enabled.
164  *
165  * This macro copies a single simple variable from user space to kernel
166  * space.  It supports simple types like char and int, but not larger
167  * data types like structures or arrays.
168  *
169  * @ptr must have pointer-to-simple-variable type, and the result of
170  * dereferencing @ptr must be assignable to @x without a cast.
171  *
172  * Caller must check the pointer with access_ok() before calling this
173  * function.
174  *
175  * Return: zero on success, or -EFAULT on error.
176  * On error, the variable @x is set to zero.
177  */
178 #define __get_user(x,ptr) do_get_user_call(get_user_nocheck,x,ptr)
179
180
181 #ifdef CONFIG_X86_32
182 #define __put_user_goto_u64(x, addr, label)                     \
183         asm_volatile_goto("\n"                                  \
184                      "1:        movl %%eax,0(%1)\n"             \
185                      "2:        movl %%edx,4(%1)\n"             \
186                      _ASM_EXTABLE_UA(1b, %l2)                   \
187                      _ASM_EXTABLE_UA(2b, %l2)                   \
188                      : : "A" (x), "r" (addr)                    \
189                      : : label)
190
191 #else
192 #define __put_user_goto_u64(x, ptr, label) \
193         __put_user_goto(x, ptr, "q", "er", label)
194 #endif
195
196 extern void __put_user_bad(void);
197
198 /*
199  * Strange magic calling convention: pointer in %ecx,
200  * value in %eax(:%edx), return value in %ecx. clobbers %rbx
201  */
202 extern void __put_user_1(void);
203 extern void __put_user_2(void);
204 extern void __put_user_4(void);
205 extern void __put_user_8(void);
206 extern void __put_user_nocheck_1(void);
207 extern void __put_user_nocheck_2(void);
208 extern void __put_user_nocheck_4(void);
209 extern void __put_user_nocheck_8(void);
210
211 #define do_put_user_call(fn,x,ptr)                                      \
212 ({                                                                      \
213         int __ret_pu;                                                   \
214         register __typeof__(*(ptr)) __val_pu asm("%"_ASM_AX);           \
215         __chk_user_ptr(ptr);                                            \
216         __val_pu = (x);                                                 \
217         asm volatile("call __" #fn "_%P[size]"                          \
218                      : "=c" (__ret_pu),                                 \
219                         ASM_CALL_CONSTRAINT                             \
220                      : "0" (ptr),                                       \
221                        "r" (__val_pu),                                  \
222                        [size] "i" (sizeof(*(ptr)))                      \
223                      :"ebx");                                           \
224         __builtin_expect(__ret_pu, 0);                                  \
225 })
226
227 /**
228  * put_user - Write a simple value into user space.
229  * @x:   Value to copy to user space.
230  * @ptr: Destination address, in user space.
231  *
232  * Context: User context only. This function may sleep if pagefaults are
233  *          enabled.
234  *
235  * This macro copies a single simple value from kernel space to user
236  * space.  It supports simple types like char and int, but not larger
237  * data types like structures or arrays.
238  *
239  * @ptr must have pointer-to-simple-variable type, and @x must be assignable
240  * to the result of dereferencing @ptr.
241  *
242  * Return: zero on success, or -EFAULT on error.
243  */
244 #define put_user(x, ptr) ({ might_fault(); do_put_user_call(put_user,x,ptr); })
245
246 /**
247  * __put_user - Write a simple value into user space, with less checking.
248  * @x:   Value to copy to user space.
249  * @ptr: Destination address, in user space.
250  *
251  * Context: User context only. This function may sleep if pagefaults are
252  *          enabled.
253  *
254  * This macro copies a single simple value from kernel space to user
255  * space.  It supports simple types like char and int, but not larger
256  * data types like structures or arrays.
257  *
258  * @ptr must have pointer-to-simple-variable type, and @x must be assignable
259  * to the result of dereferencing @ptr.
260  *
261  * Caller must check the pointer with access_ok() before calling this
262  * function.
263  *
264  * Return: zero on success, or -EFAULT on error.
265  */
266 #define __put_user(x, ptr) do_put_user_call(put_user_nocheck,x,ptr)
267
268 #define __put_user_size(x, ptr, size, label)                            \
269 do {                                                                    \
270         __chk_user_ptr(ptr);                                            \
271         switch (size) {                                                 \
272         case 1:                                                         \
273                 __put_user_goto(x, ptr, "b", "iq", label);              \
274                 break;                                                  \
275         case 2:                                                         \
276                 __put_user_goto(x, ptr, "w", "ir", label);              \
277                 break;                                                  \
278         case 4:                                                         \
279                 __put_user_goto(x, ptr, "l", "ir", label);              \
280                 break;                                                  \
281         case 8:                                                         \
282                 __put_user_goto_u64(x, ptr, label);                     \
283                 break;                                                  \
284         default:                                                        \
285                 __put_user_bad();                                       \
286         }                                                               \
287 } while (0)
288
289 #ifdef CONFIG_CC_HAS_ASM_GOTO_OUTPUT
290
291 #ifdef CONFIG_X86_32
292 #define __get_user_asm_u64(x, ptr, label) do {                          \
293         unsigned int __gu_low, __gu_high;                               \
294         const unsigned int __user *__gu_ptr;                            \
295         __gu_ptr = (const void __user *)(ptr);                          \
296         __get_user_asm(__gu_low, ptr, "l", "=r", label);                \
297         __get_user_asm(__gu_high, ptr+1, "l", "=r", label);             \
298         (x) = ((unsigned long long)__gu_high << 32) | __gu_low;         \
299 } while (0)
300 #else
301 #define __get_user_asm_u64(x, ptr, label)                               \
302         __get_user_asm(x, ptr, "q", "=r", label)
303 #endif
304
305 #define __get_user_size(x, ptr, size, label)                            \
306 do {                                                                    \
307         __chk_user_ptr(ptr);                                            \
308         switch (size) {                                                 \
309         unsigned char x_u8__;                                           \
310         case 1:                                                         \
311                 __get_user_asm(x_u8__, ptr, "b", "=q", label);          \
312                 (x) = x_u8__;                                           \
313                 break;                                                  \
314         case 2:                                                         \
315                 __get_user_asm(x, ptr, "w", "=r", label);               \
316                 break;                                                  \
317         case 4:                                                         \
318                 __get_user_asm(x, ptr, "l", "=r", label);               \
319                 break;                                                  \
320         case 8:                                                         \
321                 __get_user_asm_u64(x, ptr, label);                      \
322                 break;                                                  \
323         default:                                                        \
324                 (x) = __get_user_bad();                                 \
325         }                                                               \
326 } while (0)
327
328 #define __get_user_asm(x, addr, itype, ltype, label)                    \
329         asm_volatile_goto("\n"                                          \
330                      "1:        mov"itype" %[umem],%[output]\n"         \
331                      _ASM_EXTABLE_UA(1b, %l2)                           \
332                      : [output] ltype(x)                                \
333                      : [umem] "m" (__m(addr))                           \
334                      : : label)
335
336 #else // !CONFIG_CC_HAS_ASM_GOTO_OUTPUT
337
338 #ifdef CONFIG_X86_32
339 #define __get_user_asm_u64(x, ptr, retval)                              \
340 ({                                                                      \
341         __typeof__(ptr) __ptr = (ptr);                                  \
342         asm volatile("\n"                                               \
343                      "1:        movl %[lowbits],%%eax\n"                \
344                      "2:        movl %[highbits],%%edx\n"               \
345                      "3:\n"                                             \
346                      ".section .fixup,\"ax\"\n"                         \
347                      "4:        mov %[efault],%[errout]\n"              \
348                      "  xorl %%eax,%%eax\n"                             \
349                      "  xorl %%edx,%%edx\n"                             \
350                      "  jmp 3b\n"                                       \
351                      ".previous\n"                                      \
352                      _ASM_EXTABLE_UA(1b, 4b)                            \
353                      _ASM_EXTABLE_UA(2b, 4b)                            \
354                      : [errout] "=r" (retval),                          \
355                        [output] "=&A"(x)                                \
356                      : [lowbits] "m" (__m(__ptr)),                      \
357                        [highbits] "m" __m(((u32 __user *)(__ptr)) + 1), \
358                        [efault] "i" (-EFAULT), "0" (retval));           \
359 })
360
361 #else
362 #define __get_user_asm_u64(x, ptr, retval) \
363          __get_user_asm(x, ptr, retval, "q", "=r")
364 #endif
365
366 #define __get_user_size(x, ptr, size, retval)                           \
367 do {                                                                    \
368         unsigned char x_u8__;                                           \
369                                                                         \
370         retval = 0;                                                     \
371         __chk_user_ptr(ptr);                                            \
372         switch (size) {                                                 \
373         case 1:                                                         \
374                 __get_user_asm(x_u8__, ptr, retval, "b", "=q");         \
375                 (x) = x_u8__;                                           \
376                 break;                                                  \
377         case 2:                                                         \
378                 __get_user_asm(x, ptr, retval, "w", "=r");              \
379                 break;                                                  \
380         case 4:                                                         \
381                 __get_user_asm(x, ptr, retval, "l", "=r");              \
382                 break;                                                  \
383         case 8:                                                         \
384                 __get_user_asm_u64(x, ptr, retval);                     \
385                 break;                                                  \
386         default:                                                        \
387                 (x) = __get_user_bad();                                 \
388         }                                                               \
389 } while (0)
390
391 #define __get_user_asm(x, addr, err, itype, ltype)                      \
392         asm volatile("\n"                                               \
393                      "1:        mov"itype" %[umem],%[output]\n"         \
394                      "2:\n"                                             \
395                      ".section .fixup,\"ax\"\n"                         \
396                      "3:        mov %[efault],%[errout]\n"              \
397                      "  xorl %k[output],%k[output]\n"                   \
398                      "  jmp 2b\n"                                       \
399                      ".previous\n"                                      \
400                      _ASM_EXTABLE_UA(1b, 3b)                            \
401                      : [errout] "=r" (err),                             \
402                        [output] ltype(x)                                \
403                      : [umem] "m" (__m(addr)),                          \
404                        [efault] "i" (-EFAULT), "0" (err))
405
406 #endif // CONFIG_CC_ASM_GOTO_OUTPUT
407
408 /* FIXME: this hack is definitely wrong -AK */
409 struct __large_struct { unsigned long buf[100]; };
410 #define __m(x) (*(struct __large_struct __user *)(x))
411
412 /*
413  * Tell gcc we read from memory instead of writing: this is because
414  * we do not write to any memory gcc knows about, so there are no
415  * aliasing issues.
416  */
417 #define __put_user_goto(x, addr, itype, ltype, label)                   \
418         asm_volatile_goto("\n"                                          \
419                 "1:     mov"itype" %0,%1\n"                             \
420                 _ASM_EXTABLE_UA(1b, %l2)                                \
421                 : : ltype(x), "m" (__m(addr))                           \
422                 : : label)
423
424 extern unsigned long
425 copy_from_user_nmi(void *to, const void __user *from, unsigned long n);
426 extern __must_check long
427 strncpy_from_user(char *dst, const char __user *src, long count);
428
429 extern __must_check long strnlen_user(const char __user *str, long n);
430
431 unsigned long __must_check clear_user(void __user *mem, unsigned long len);
432 unsigned long __must_check __clear_user(void __user *mem, unsigned long len);
433
434 #ifdef CONFIG_ARCH_HAS_COPY_MC
435 unsigned long __must_check
436 copy_mc_to_kernel(void *to, const void *from, unsigned len);
437 #define copy_mc_to_kernel copy_mc_to_kernel
438
439 unsigned long __must_check
440 copy_mc_to_user(void *to, const void *from, unsigned len);
441 #endif
442
443 /*
444  * movsl can be slow when source and dest are not both 8-byte aligned
445  */
446 #ifdef CONFIG_X86_INTEL_USERCOPY
447 extern struct movsl_mask {
448         int mask;
449 } ____cacheline_aligned_in_smp movsl_mask;
450 #endif
451
452 #define ARCH_HAS_NOCACHE_UACCESS 1
453
454 #ifdef CONFIG_X86_32
455 # include <asm/uaccess_32.h>
456 #else
457 # include <asm/uaccess_64.h>
458 #endif
459
460 /*
461  * The "unsafe" user accesses aren't really "unsafe", but the naming
462  * is a big fat warning: you have to not only do the access_ok()
463  * checking before using them, but you have to surround them with the
464  * user_access_begin/end() pair.
465  */
466 static __must_check __always_inline bool user_access_begin(const void __user *ptr, size_t len)
467 {
468         if (unlikely(!access_ok(ptr,len)))
469                 return 0;
470         __uaccess_begin_nospec();
471         return 1;
472 }
473 #define user_access_begin(a,b)  user_access_begin(a,b)
474 #define user_access_end()       __uaccess_end()
475
476 #define user_access_save()      smap_save()
477 #define user_access_restore(x)  smap_restore(x)
478
479 #define unsafe_put_user(x, ptr, label)  \
480         __put_user_size((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)), label)
481
482 #ifdef CONFIG_CC_HAS_ASM_GOTO_OUTPUT
483 #define unsafe_get_user(x, ptr, err_label)                                      \
484 do {                                                                            \
485         __inttype(*(ptr)) __gu_val;                                             \
486         __get_user_size(__gu_val, (ptr), sizeof(*(ptr)), err_label);            \
487         (x) = (__force __typeof__(*(ptr)))__gu_val;                             \
488 } while (0)
489 #else // !CONFIG_CC_HAS_ASM_GOTO_OUTPUT
490 #define unsafe_get_user(x, ptr, err_label)                                      \
491 do {                                                                            \
492         int __gu_err;                                                           \
493         __inttype(*(ptr)) __gu_val;                                             \
494         __get_user_size(__gu_val, (ptr), sizeof(*(ptr)), __gu_err);             \
495         (x) = (__force __typeof__(*(ptr)))__gu_val;                             \
496         if (unlikely(__gu_err)) goto err_label;                                 \
497 } while (0)
498 #endif // CONFIG_CC_HAS_ASM_GOTO_OUTPUT
499
500 /*
501  * We want the unsafe accessors to always be inlined and use
502  * the error labels - thus the macro games.
503  */
504 #define unsafe_copy_loop(dst, src, len, type, label)                            \
505         while (len >= sizeof(type)) {                                           \
506                 unsafe_put_user(*(type *)(src),(type __user *)(dst),label);     \
507                 dst += sizeof(type);                                            \
508                 src += sizeof(type);                                            \
509                 len -= sizeof(type);                                            \
510         }
511
512 #define unsafe_copy_to_user(_dst,_src,_len,label)                       \
513 do {                                                                    \
514         char __user *__ucu_dst = (_dst);                                \
515         const char *__ucu_src = (_src);                                 \
516         size_t __ucu_len = (_len);                                      \
517         unsafe_copy_loop(__ucu_dst, __ucu_src, __ucu_len, u64, label);  \
518         unsafe_copy_loop(__ucu_dst, __ucu_src, __ucu_len, u32, label);  \
519         unsafe_copy_loop(__ucu_dst, __ucu_src, __ucu_len, u16, label);  \
520         unsafe_copy_loop(__ucu_dst, __ucu_src, __ucu_len, u8, label);   \
521 } while (0)
522
523 #define HAVE_GET_KERNEL_NOFAULT
524
525 #ifdef CONFIG_CC_HAS_ASM_GOTO_OUTPUT
526 #define __get_kernel_nofault(dst, src, type, err_label)                 \
527         __get_user_size(*((type *)(dst)), (__force type __user *)(src), \
528                         sizeof(type), err_label)
529 #else // !CONFIG_CC_HAS_ASM_GOTO_OUTPUT
530 #define __get_kernel_nofault(dst, src, type, err_label)                 \
531 do {                                                                    \
532         int __kr_err;                                                   \
533                                                                         \
534         __get_user_size(*((type *)(dst)), (__force type __user *)(src), \
535                         sizeof(type), __kr_err);                        \
536         if (unlikely(__kr_err))                                         \
537                 goto err_label;                                         \
538 } while (0)
539 #endif // CONFIG_CC_HAS_ASM_GOTO_OUTPUT
540
541 #define __put_kernel_nofault(dst, src, type, err_label)                 \
542         __put_user_size(*((type *)(src)), (__force type __user *)(dst), \
543                         sizeof(type), err_label)
544
545 #endif /* _ASM_X86_UACCESS_H */
546