1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_UACCESS_H
3 #define _ASM_X86_UACCESS_H
5 * User space memory access functions
7 #include <linux/compiler.h>
8 #include <linux/kasan-checks.h>
9 #include <linux/string.h>
13 #include <asm/extable.h>
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.
20 * For historical reasons, these macros are grossly misnamed.
23 #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
25 #define KERNEL_DS MAKE_MM_SEG(-1UL)
26 #define USER_DS MAKE_MM_SEG(TASK_SIZE_MAX)
28 #define get_fs() (current->thread.addr_limit)
29 static inline void set_fs(mm_segment_t fs)
31 current->thread.addr_limit = fs;
32 /* On user-mode return, check fs is correct */
33 set_thread_flag(TIF_FSCHECK);
36 #define segment_eq(a, b) ((a).seg == (b).seg)
38 #define user_addr_max() (current->thread.addr_limit.seg)
39 #define __addr_ok(addr) \
40 ((unsigned long __force)(addr) < user_addr_max())
43 * Test whether a block of memory is a valid user space address.
44 * Returns 0 if the range is valid, nonzero otherwise.
46 static inline bool __chk_range_not_ok(unsigned long addr, unsigned long size, unsigned long limit)
49 * If we have used "sizeof()" for the size,
50 * we know it won't overflow the limit (but
51 * it might overflow the 'addr', so it's
52 * important to subtract the size from the
53 * limit, not add it to the address).
55 if (__builtin_constant_p(size))
56 return unlikely(addr > limit - size);
58 /* Arbitrary sizes? Be careful about overflow */
60 if (unlikely(addr < size))
62 return unlikely(addr > limit);
65 #define __range_not_ok(addr, size, limit) \
67 __chk_user_ptr(addr); \
68 __chk_range_not_ok((unsigned long __force)(addr), size, limit); \
71 #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
72 # define WARN_ON_IN_IRQ() WARN_ON_ONCE(!in_task())
74 # define WARN_ON_IN_IRQ()
78 * access_ok - Checks if a user space pointer is valid
79 * @addr: User space pointer to start of block to check
80 * @size: Size of block to check
82 * Context: User context only. This function may sleep if pagefaults are
85 * Checks if a pointer to a block of memory in user space is valid.
87 * Note that, depending on architecture, this function probably just
88 * checks that the pointer is in the user space range - after calling
89 * this function, memory access functions may still return -EFAULT.
91 * Return: true (nonzero) if the memory block may be valid, false (zero)
92 * if it is definitely invalid.
94 #define access_ok(addr, size) \
97 likely(!__range_not_ok(addr, size, user_addr_max())); \
101 * These are the main single-value transfer routines. They automatically
102 * use the right size if we just have the right pointer type.
104 * This gets kind of ugly. We want to return _two_ values in "get_user()"
105 * and yet we don't want to do any pointers, because that is too much
106 * of a performance impact. Thus we have a few rather ugly macros here,
107 * and hide all the ugliness from the user.
109 * The "__xxx" versions of the user access functions are versions that
110 * do not verify the address space, that must have been done previously
111 * with a separate "access_ok()" call (this is used when we do multiple
112 * accesses to the same area of user memory).
115 extern int __get_user_1(void);
116 extern int __get_user_2(void);
117 extern int __get_user_4(void);
118 extern int __get_user_8(void);
119 extern int __get_user_bad(void);
121 #define __uaccess_begin() stac()
122 #define __uaccess_end() clac()
123 #define __uaccess_begin_nospec() \
130 * This is a type: either unsigned long, if the argument fits into
131 * that type, or otherwise unsigned long long.
133 #define __inttype(x) \
134 __typeof__(__builtin_choose_expr(sizeof(x) > sizeof(0UL), 0ULL, 0UL))
137 * get_user - Get a simple variable from user space.
138 * @x: Variable to store result.
139 * @ptr: Source address, in user space.
141 * Context: User context only. This function may sleep if pagefaults are
144 * This macro copies a single simple variable from user space to kernel
145 * space. It supports simple types like char and int, but not larger
146 * data types like structures or arrays.
148 * @ptr must have pointer-to-simple-variable type, and the result of
149 * dereferencing @ptr must be assignable to @x without a cast.
151 * Return: zero on success, or -EFAULT on error.
152 * On error, the variable @x is set to zero.
155 * Careful: we have to cast the result to the type of the pointer
158 * The use of _ASM_DX as the register specifier is a bit of a
159 * simplification, as gcc only cares about it as the starting point
160 * and not size: for a 64-bit value it will use %ecx:%edx on 32 bits
161 * (%ecx being the next register in gcc's x86 register sequence), and
164 * Clang/LLVM cares about the size of the register, but still wants
165 * the base register for something that ends up being a pair.
167 #define get_user(x, ptr) \
170 register __inttype(*(ptr)) __val_gu asm("%"_ASM_DX); \
171 __chk_user_ptr(ptr); \
173 asm volatile("call __get_user_%P4" \
174 : "=a" (__ret_gu), "=r" (__val_gu), \
175 ASM_CALL_CONSTRAINT \
176 : "0" (ptr), "i" (sizeof(*(ptr)))); \
177 (x) = (__force __typeof__(*(ptr))) __val_gu; \
178 __builtin_expect(__ret_gu, 0); \
181 #define __put_user_x(size, x, ptr, __ret_pu) \
182 asm volatile("call __put_user_" #size : "=a" (__ret_pu) \
183 : "0" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx")
188 #define __put_user_goto_u64(x, addr, label) \
189 asm_volatile_goto("\n" \
190 "1: movl %%eax,0(%1)\n" \
191 "2: movl %%edx,4(%1)\n" \
192 _ASM_EXTABLE_UA(1b, %l2) \
193 _ASM_EXTABLE_UA(2b, %l2) \
194 : : "A" (x), "r" (addr) \
197 #define __put_user_asm_ex_u64(x, addr) \
199 "1: movl %%eax,0(%1)\n" \
200 "2: movl %%edx,4(%1)\n" \
202 _ASM_EXTABLE_EX(1b, 2b) \
203 _ASM_EXTABLE_EX(2b, 3b) \
204 : : "A" (x), "r" (addr))
206 #define __put_user_x8(x, ptr, __ret_pu) \
207 asm volatile("call __put_user_8" : "=a" (__ret_pu) \
208 : "A" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx")
210 #define __put_user_goto_u64(x, ptr, label) \
211 __put_user_goto(x, ptr, "q", "", "er", label)
212 #define __put_user_asm_ex_u64(x, addr) \
213 __put_user_asm_ex(x, addr, "q", "", "er")
214 #define __put_user_x8(x, ptr, __ret_pu) __put_user_x(8, x, ptr, __ret_pu)
217 extern void __put_user_bad(void);
220 * Strange magic calling convention: pointer in %ecx,
221 * value in %eax(:%edx), return value in %eax. clobbers %rbx
223 extern void __put_user_1(void);
224 extern void __put_user_2(void);
225 extern void __put_user_4(void);
226 extern void __put_user_8(void);
229 * put_user - Write a simple value into user space.
230 * @x: Value to copy to user space.
231 * @ptr: Destination address, in user space.
233 * Context: User context only. This function may sleep if pagefaults are
236 * This macro copies a single simple value from kernel space to user
237 * space. It supports simple types like char and int, but not larger
238 * data types like structures or arrays.
240 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
241 * to the result of dereferencing @ptr.
243 * Return: zero on success, or -EFAULT on error.
245 #define put_user(x, ptr) \
248 __typeof__(*(ptr)) __pu_val; \
249 __chk_user_ptr(ptr); \
252 switch (sizeof(*(ptr))) { \
254 __put_user_x(1, __pu_val, ptr, __ret_pu); \
257 __put_user_x(2, __pu_val, ptr, __ret_pu); \
260 __put_user_x(4, __pu_val, ptr, __ret_pu); \
263 __put_user_x8(__pu_val, ptr, __ret_pu); \
266 __put_user_x(X, __pu_val, ptr, __ret_pu); \
269 __builtin_expect(__ret_pu, 0); \
272 #define __put_user_size(x, ptr, size, label) \
274 __chk_user_ptr(ptr); \
277 __put_user_goto(x, ptr, "b", "b", "iq", label); \
280 __put_user_goto(x, ptr, "w", "w", "ir", label); \
283 __put_user_goto(x, ptr, "l", "k", "ir", label); \
286 __put_user_goto_u64(x, ptr, label); \
294 * This doesn't do __uaccess_begin/end - the exception handling
295 * around it must do that.
297 #define __put_user_size_ex(x, ptr, size) \
299 __chk_user_ptr(ptr); \
302 __put_user_asm_ex(x, ptr, "b", "b", "iq"); \
305 __put_user_asm_ex(x, ptr, "w", "w", "ir"); \
308 __put_user_asm_ex(x, ptr, "l", "k", "ir"); \
311 __put_user_asm_ex_u64((__typeof__(*ptr))(x), ptr); \
319 #define __get_user_asm_u64(x, ptr, retval, errret) \
321 __typeof__(ptr) __ptr = (ptr); \
323 "1: movl %2,%%eax\n" \
324 "2: movl %3,%%edx\n" \
326 ".section .fixup,\"ax\"\n" \
328 " xorl %%eax,%%eax\n" \
329 " xorl %%edx,%%edx\n" \
332 _ASM_EXTABLE_UA(1b, 4b) \
333 _ASM_EXTABLE_UA(2b, 4b) \
334 : "=r" (retval), "=&A"(x) \
335 : "m" (__m(__ptr)), "m" __m(((u32 __user *)(__ptr)) + 1), \
336 "i" (errret), "0" (retval)); \
339 #define __get_user_asm_ex_u64(x, ptr) (x) = __get_user_bad()
341 #define __get_user_asm_u64(x, ptr, retval, errret) \
342 __get_user_asm(x, ptr, retval, "q", "", "=r", errret)
343 #define __get_user_asm_ex_u64(x, ptr) \
344 __get_user_asm_ex(x, ptr, "q", "", "=r")
347 #define __get_user_size(x, ptr, size, retval, errret) \
350 __chk_user_ptr(ptr); \
353 __get_user_asm(x, ptr, retval, "b", "b", "=q", errret); \
356 __get_user_asm(x, ptr, retval, "w", "w", "=r", errret); \
359 __get_user_asm(x, ptr, retval, "l", "k", "=r", errret); \
362 __get_user_asm_u64(x, ptr, retval, errret); \
365 (x) = __get_user_bad(); \
369 #define __get_user_asm(x, addr, err, itype, rtype, ltype, errret) \
371 "1: mov"itype" %2,%"rtype"1\n" \
373 ".section .fixup,\"ax\"\n" \
375 " xor"itype" %"rtype"1,%"rtype"1\n" \
378 _ASM_EXTABLE_UA(1b, 3b) \
379 : "=r" (err), ltype(x) \
380 : "m" (__m(addr)), "i" (errret), "0" (err))
382 #define __get_user_asm_nozero(x, addr, err, itype, rtype, ltype, errret) \
384 "1: mov"itype" %2,%"rtype"1\n" \
386 ".section .fixup,\"ax\"\n" \
390 _ASM_EXTABLE_UA(1b, 3b) \
391 : "=r" (err), ltype(x) \
392 : "m" (__m(addr)), "i" (errret), "0" (err))
395 * This doesn't do __uaccess_begin/end - the exception handling
396 * around it must do that.
398 #define __get_user_size_ex(x, ptr, size) \
400 __chk_user_ptr(ptr); \
403 __get_user_asm_ex(x, ptr, "b", "b", "=q"); \
406 __get_user_asm_ex(x, ptr, "w", "w", "=r"); \
409 __get_user_asm_ex(x, ptr, "l", "k", "=r"); \
412 __get_user_asm_ex_u64(x, ptr); \
415 (x) = __get_user_bad(); \
419 #define __get_user_asm_ex(x, addr, itype, rtype, ltype) \
420 asm volatile("1: mov"itype" %1,%"rtype"0\n" \
422 ".section .fixup,\"ax\"\n" \
423 "3:xor"itype" %"rtype"0,%"rtype"0\n" \
426 _ASM_EXTABLE_EX(1b, 3b) \
427 : ltype(x) : "m" (__m(addr)))
429 #define __put_user_nocheck(x, ptr, size) \
431 __label__ __pu_label; \
432 int __pu_err = -EFAULT; \
433 __typeof__(*(ptr)) __pu_val; \
436 __put_user_size(__pu_val, (ptr), (size), __pu_label); \
440 __builtin_expect(__pu_err, 0); \
443 #define __get_user_nocheck(x, ptr, size) \
446 __inttype(*(ptr)) __gu_val; \
447 __uaccess_begin_nospec(); \
448 __get_user_size(__gu_val, (ptr), (size), __gu_err, -EFAULT); \
450 (x) = (__force __typeof__(*(ptr)))__gu_val; \
451 __builtin_expect(__gu_err, 0); \
454 /* FIXME: this hack is definitely wrong -AK */
455 struct __large_struct { unsigned long buf[100]; };
456 #define __m(x) (*(struct __large_struct __user *)(x))
459 * Tell gcc we read from memory instead of writing: this is because
460 * we do not write to any memory gcc knows about, so there are no
463 #define __put_user_goto(x, addr, itype, rtype, ltype, label) \
464 asm_volatile_goto("\n" \
465 "1: mov"itype" %"rtype"0,%1\n" \
466 _ASM_EXTABLE_UA(1b, %l2) \
467 : : ltype(x), "m" (__m(addr)) \
470 #define __put_user_failed(x, addr, itype, rtype, ltype, errret) \
471 ({ __label__ __puflab; \
472 int __pufret = errret; \
473 __put_user_goto(x,addr,itype,rtype,ltype,__puflab); \
475 __puflab: __pufret; })
477 #define __put_user_asm(x, addr, retval, itype, rtype, ltype, errret) do { \
478 retval = __put_user_failed(x, addr, itype, rtype, ltype, errret); \
481 #define __put_user_asm_ex(x, addr, itype, rtype, ltype) \
482 asm volatile("1: mov"itype" %"rtype"0,%1\n" \
484 _ASM_EXTABLE_EX(1b, 2b) \
485 : : ltype(x), "m" (__m(addr)))
488 * uaccess_try and catch
490 #define uaccess_try do { \
491 current->thread.uaccess_err = 0; \
495 #define uaccess_try_nospec do { \
496 current->thread.uaccess_err = 0; \
497 __uaccess_begin_nospec(); \
499 #define uaccess_catch(err) \
501 (err) |= (current->thread.uaccess_err ? -EFAULT : 0); \
505 * __get_user - Get a simple variable from user space, with less checking.
506 * @x: Variable to store result.
507 * @ptr: Source address, in user space.
509 * Context: User context only. This function may sleep if pagefaults are
512 * This macro copies a single simple variable from user space to kernel
513 * space. It supports simple types like char and int, but not larger
514 * data types like structures or arrays.
516 * @ptr must have pointer-to-simple-variable type, and the result of
517 * dereferencing @ptr must be assignable to @x without a cast.
519 * Caller must check the pointer with access_ok() before calling this
522 * Return: zero on success, or -EFAULT on error.
523 * On error, the variable @x is set to zero.
526 #define __get_user(x, ptr) \
527 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
530 * __put_user - Write a simple value into user space, with less checking.
531 * @x: Value to copy to user space.
532 * @ptr: Destination address, in user space.
534 * Context: User context only. This function may sleep if pagefaults are
537 * This macro copies a single simple value from kernel space to user
538 * space. It supports simple types like char and int, but not larger
539 * data types like structures or arrays.
541 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
542 * to the result of dereferencing @ptr.
544 * Caller must check the pointer with access_ok() before calling this
547 * Return: zero on success, or -EFAULT on error.
550 #define __put_user(x, ptr) \
551 __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
554 * {get|put}_user_try and catch
558 * } get_user_catch(err)
560 #define get_user_try uaccess_try_nospec
561 #define get_user_catch(err) uaccess_catch(err)
563 #define get_user_ex(x, ptr) do { \
564 unsigned long __gue_val; \
565 __get_user_size_ex((__gue_val), (ptr), (sizeof(*(ptr)))); \
566 (x) = (__force __typeof__(*(ptr)))__gue_val; \
569 #define put_user_try uaccess_try
570 #define put_user_catch(err) uaccess_catch(err)
572 #define put_user_ex(x, ptr) \
573 __put_user_size_ex((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
576 copy_from_user_nmi(void *to, const void __user *from, unsigned long n);
577 extern __must_check long
578 strncpy_from_user(char *dst, const char __user *src, long count);
580 extern __must_check long strnlen_user(const char __user *str, long n);
582 unsigned long __must_check clear_user(void __user *mem, unsigned long len);
583 unsigned long __must_check __clear_user(void __user *mem, unsigned long len);
585 extern void __cmpxchg_wrong_size(void)
586 __compiletime_error("Bad argument size for cmpxchg");
588 #define __user_atomic_cmpxchg_inatomic(uval, ptr, old, new, size) \
591 __typeof__(ptr) __uval = (uval); \
592 __typeof__(*(ptr)) __old = (old); \
593 __typeof__(*(ptr)) __new = (new); \
594 __uaccess_begin_nospec(); \
599 "1:\t" LOCK_PREFIX "cmpxchgb %4, %2\n" \
601 "\t.section .fixup, \"ax\"\n" \
605 _ASM_EXTABLE_UA(1b, 3b) \
606 : "+r" (__ret), "=a" (__old), "+m" (*(ptr)) \
607 : "i" (-EFAULT), "q" (__new), "1" (__old) \
615 "1:\t" LOCK_PREFIX "cmpxchgw %4, %2\n" \
617 "\t.section .fixup, \"ax\"\n" \
621 _ASM_EXTABLE_UA(1b, 3b) \
622 : "+r" (__ret), "=a" (__old), "+m" (*(ptr)) \
623 : "i" (-EFAULT), "r" (__new), "1" (__old) \
631 "1:\t" LOCK_PREFIX "cmpxchgl %4, %2\n" \
633 "\t.section .fixup, \"ax\"\n" \
637 _ASM_EXTABLE_UA(1b, 3b) \
638 : "+r" (__ret), "=a" (__old), "+m" (*(ptr)) \
639 : "i" (-EFAULT), "r" (__new), "1" (__old) \
646 if (!IS_ENABLED(CONFIG_X86_64)) \
647 __cmpxchg_wrong_size(); \
650 "1:\t" LOCK_PREFIX "cmpxchgq %4, %2\n" \
652 "\t.section .fixup, \"ax\"\n" \
656 _ASM_EXTABLE_UA(1b, 3b) \
657 : "+r" (__ret), "=a" (__old), "+m" (*(ptr)) \
658 : "i" (-EFAULT), "r" (__new), "1" (__old) \
664 __cmpxchg_wrong_size(); \
671 #define user_atomic_cmpxchg_inatomic(uval, ptr, old, new) \
673 access_ok((ptr), sizeof(*(ptr))) ? \
674 __user_atomic_cmpxchg_inatomic((uval), (ptr), \
675 (old), (new), sizeof(*(ptr))) : \
680 * movsl can be slow when source and dest are not both 8-byte aligned
682 #ifdef CONFIG_X86_INTEL_USERCOPY
683 extern struct movsl_mask {
685 } ____cacheline_aligned_in_smp movsl_mask;
688 #define ARCH_HAS_NOCACHE_UACCESS 1
691 # include <asm/uaccess_32.h>
693 # include <asm/uaccess_64.h>
697 * We rely on the nested NMI work to allow atomic faults from the NMI path; the
698 * nested NMI paths are careful to preserve CR2.
700 * Caller must use pagefault_enable/disable, or run in interrupt context,
701 * and also do a uaccess_ok() check
703 #define __copy_from_user_nmi __copy_from_user_inatomic
706 * The "unsafe" user accesses aren't really "unsafe", but the naming
707 * is a big fat warning: you have to not only do the access_ok()
708 * checking before using them, but you have to surround them with the
709 * user_access_begin/end() pair.
711 static __must_check inline bool user_access_begin(const void __user *ptr, size_t len)
713 if (unlikely(!access_ok(ptr,len)))
715 __uaccess_begin_nospec();
718 #define user_access_begin(a,b) user_access_begin(a,b)
719 #define user_access_end() __uaccess_end()
721 #define unsafe_put_user(x, ptr, label) \
722 __put_user_size((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)), label)
724 #define unsafe_get_user(x, ptr, err_label) \
727 __inttype(*(ptr)) __gu_val; \
728 __get_user_size(__gu_val, (ptr), sizeof(*(ptr)), __gu_err, -EFAULT); \
729 (x) = (__force __typeof__(*(ptr)))__gu_val; \
730 if (unlikely(__gu_err)) goto err_label; \
733 #endif /* _ASM_X86_UACCESS_H */