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_ds() (KERNEL_DS)
29 #define get_fs() (current->thread.addr_limit)
30 static inline void set_fs(mm_segment_t fs)
32 current->thread.addr_limit = fs;
33 /* On user-mode return, check fs is correct */
34 set_thread_flag(TIF_FSCHECK);
37 #define segment_eq(a, b) ((a).seg == (b).seg)
39 #define user_addr_max() (current->thread.addr_limit.seg)
40 #define __addr_ok(addr) \
41 ((unsigned long __force)(addr) < user_addr_max())
44 * Test whether a block of memory is a valid user space address.
45 * Returns 0 if the range is valid, nonzero otherwise.
47 static inline bool __chk_range_not_ok(unsigned long addr, unsigned long size, unsigned long limit)
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).
56 if (__builtin_constant_p(size))
57 return unlikely(addr > limit - size);
59 /* Arbitrary sizes? Be careful about overflow */
61 if (unlikely(addr < size))
63 return unlikely(addr > limit);
66 #define __range_not_ok(addr, size, limit) \
68 __chk_user_ptr(addr); \
69 __chk_range_not_ok((unsigned long __force)(addr), size, limit); \
72 #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
73 # define WARN_ON_IN_IRQ() WARN_ON_ONCE(!in_task())
75 # define WARN_ON_IN_IRQ()
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
86 * Context: User context only. This function may sleep if pagefaults are
89 * Checks if a pointer to a block of memory in user space is valid.
91 * Returns true (nonzero) if the memory block may be valid, false (zero)
92 * if it is definitely invalid.
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.
98 #define access_ok(type, addr, size) \
101 likely(!__range_not_ok(addr, size, user_addr_max())); \
105 * These are the main single-value transfer routines. They automatically
106 * use the right size if we just have the right pointer type.
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.
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).
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);
125 #define __uaccess_begin() stac()
126 #define __uaccess_end() clac()
127 #define __uaccess_begin_nospec() \
134 * This is a type: either unsigned long, if the argument fits into
135 * that type, or otherwise unsigned long long.
137 #define __inttype(x) \
138 __typeof__(__builtin_choose_expr(sizeof(x) > sizeof(0UL), 0ULL, 0UL))
141 * get_user: - Get a simple variable from user space.
142 * @x: Variable to store result.
143 * @ptr: Source address, in user space.
145 * Context: User context only. This function may sleep if pagefaults are
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.
152 * @ptr must have pointer-to-simple-variable type, and the result of
153 * dereferencing @ptr must be assignable to @x without a cast.
155 * Returns zero on success, or -EFAULT on error.
156 * On error, the variable @x is set to zero.
159 * Careful: we have to cast the result to the type of the pointer
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
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.
171 #define get_user(x, ptr) \
174 register __inttype(*(ptr)) __val_gu asm("%"_ASM_DX); \
175 __chk_user_ptr(ptr); \
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); \
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")
192 #define __put_user_asm_u64(x, addr, err, errret) \
194 "1: movl %%eax,0(%2)\n" \
195 "2: movl %%edx,4(%2)\n" \
197 ".section .fixup,\"ax\"\n" \
201 _ASM_EXTABLE(1b, 4b) \
202 _ASM_EXTABLE(2b, 4b) \
204 : "A" (x), "r" (addr), "i" (errret), "0" (err))
206 #define __put_user_asm_ex_u64(x, addr) \
208 "1: movl %%eax,0(%1)\n" \
209 "2: movl %%edx,4(%1)\n" \
211 _ASM_EXTABLE_EX(1b, 2b) \
212 _ASM_EXTABLE_EX(2b, 3b) \
213 : : "A" (x), "r" (addr))
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")
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)
226 extern void __put_user_bad(void);
229 * Strange magic calling convention: pointer in %ecx,
230 * value in %eax(:%edx), return value in %eax. clobbers %rbx
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);
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.
242 * Context: User context only. This function may sleep if pagefaults are
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.
249 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
250 * to the result of dereferencing @ptr.
252 * Returns zero on success, or -EFAULT on error.
254 #define put_user(x, ptr) \
257 __typeof__(*(ptr)) __pu_val; \
258 __chk_user_ptr(ptr); \
261 switch (sizeof(*(ptr))) { \
263 __put_user_x(1, __pu_val, ptr, __ret_pu); \
266 __put_user_x(2, __pu_val, ptr, __ret_pu); \
269 __put_user_x(4, __pu_val, ptr, __ret_pu); \
272 __put_user_x8(__pu_val, ptr, __ret_pu); \
275 __put_user_x(X, __pu_val, ptr, __ret_pu); \
278 __builtin_expect(__ret_pu, 0); \
281 #define __put_user_size(x, ptr, size, retval, errret) \
284 __chk_user_ptr(ptr); \
287 __put_user_asm(x, ptr, retval, "b", "b", "iq", errret); \
290 __put_user_asm(x, ptr, retval, "w", "w", "ir", errret); \
293 __put_user_asm(x, ptr, retval, "l", "k", "ir", errret); \
296 __put_user_asm_u64(x, ptr, retval, errret); \
304 * This doesn't do __uaccess_begin/end - the exception handling
305 * around it must do that.
307 #define __put_user_size_ex(x, ptr, size) \
309 __chk_user_ptr(ptr); \
312 __put_user_asm_ex(x, ptr, "b", "b", "iq"); \
315 __put_user_asm_ex(x, ptr, "w", "w", "ir"); \
318 __put_user_asm_ex(x, ptr, "l", "k", "ir"); \
321 __put_user_asm_ex_u64((__typeof__(*ptr))(x), ptr); \
329 #define __get_user_asm_u64(x, ptr, retval, errret) \
331 __typeof__(ptr) __ptr = (ptr); \
333 "1: movl %2,%%eax\n" \
334 "2: movl %3,%%edx\n" \
336 ".section .fixup,\"ax\"\n" \
338 " xorl %%eax,%%eax\n" \
339 " xorl %%edx,%%edx\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)); \
349 #define __get_user_asm_ex_u64(x, ptr) (x) = __get_user_bad()
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")
357 #define __get_user_size(x, ptr, size, retval, errret) \
360 __chk_user_ptr(ptr); \
363 __get_user_asm(x, ptr, retval, "b", "b", "=q", errret); \
366 __get_user_asm(x, ptr, retval, "w", "w", "=r", errret); \
369 __get_user_asm(x, ptr, retval, "l", "k", "=r", errret); \
372 __get_user_asm_u64(x, ptr, retval, errret); \
375 (x) = __get_user_bad(); \
379 #define __get_user_asm(x, addr, err, itype, rtype, ltype, errret) \
381 "1: mov"itype" %2,%"rtype"1\n" \
383 ".section .fixup,\"ax\"\n" \
385 " xor"itype" %"rtype"1,%"rtype"1\n" \
388 _ASM_EXTABLE(1b, 3b) \
389 : "=r" (err), ltype(x) \
390 : "m" (__m(addr)), "i" (errret), "0" (err))
392 #define __get_user_asm_nozero(x, addr, err, itype, rtype, ltype, errret) \
394 "1: mov"itype" %2,%"rtype"1\n" \
396 ".section .fixup,\"ax\"\n" \
400 _ASM_EXTABLE(1b, 3b) \
401 : "=r" (err), ltype(x) \
402 : "m" (__m(addr)), "i" (errret), "0" (err))
405 * This doesn't do __uaccess_begin/end - the exception handling
406 * around it must do that.
408 #define __get_user_size_ex(x, ptr, size) \
410 __chk_user_ptr(ptr); \
413 __get_user_asm_ex(x, ptr, "b", "b", "=q"); \
416 __get_user_asm_ex(x, ptr, "w", "w", "=r"); \
419 __get_user_asm_ex(x, ptr, "l", "k", "=r"); \
422 __get_user_asm_ex_u64(x, ptr); \
425 (x) = __get_user_bad(); \
429 #define __get_user_asm_ex(x, addr, itype, rtype, ltype) \
430 asm volatile("1: mov"itype" %1,%"rtype"0\n" \
432 ".section .fixup,\"ax\"\n" \
433 "3:xor"itype" %"rtype"0,%"rtype"0\n" \
436 _ASM_EXTABLE_EX(1b, 3b) \
437 : ltype(x) : "m" (__m(addr)))
439 #define __put_user_nocheck(x, ptr, size) \
442 __typeof__(*(ptr)) __pu_val; \
445 __put_user_size(__pu_val, (ptr), (size), __pu_err, -EFAULT);\
447 __builtin_expect(__pu_err, 0); \
450 #define __get_user_nocheck(x, ptr, size) \
453 __inttype(*(ptr)) __gu_val; \
454 __typeof__(ptr) __gu_ptr = (ptr); \
455 __typeof__(size) __gu_size = (size); \
456 __uaccess_begin_nospec(); \
457 __get_user_size(__gu_val, __gu_ptr, __gu_size, __gu_err, -EFAULT); \
459 (x) = (__force __typeof__(*(ptr)))__gu_val; \
460 __builtin_expect(__gu_err, 0); \
463 /* FIXME: this hack is definitely wrong -AK */
464 struct __large_struct { unsigned long buf[100]; };
465 #define __m(x) (*(struct __large_struct __user *)(x))
468 * Tell gcc we read from memory instead of writing: this is because
469 * we do not write to any memory gcc knows about, so there are no
472 #define __put_user_asm(x, addr, err, itype, rtype, ltype, errret) \
474 "1: mov"itype" %"rtype"1,%2\n" \
476 ".section .fixup,\"ax\"\n" \
480 _ASM_EXTABLE(1b, 3b) \
482 : ltype(x), "m" (__m(addr)), "i" (errret), "0" (err))
484 #define __put_user_asm_ex(x, addr, itype, rtype, ltype) \
485 asm volatile("1: mov"itype" %"rtype"0,%1\n" \
487 _ASM_EXTABLE_EX(1b, 2b) \
488 : : ltype(x), "m" (__m(addr)))
491 * uaccess_try and catch
493 #define uaccess_try do { \
494 current->thread.uaccess_err = 0; \
498 #define uaccess_try_nospec do { \
499 current->thread.uaccess_err = 0; \
500 __uaccess_begin_nospec(); \
502 #define uaccess_catch(err) \
504 (err) |= (current->thread.uaccess_err ? -EFAULT : 0); \
508 * __get_user: - Get a simple variable from user space, with less checking.
509 * @x: Variable to store result.
510 * @ptr: Source address, in user space.
512 * Context: User context only. This function may sleep if pagefaults are
515 * This macro copies a single simple variable from user space to kernel
516 * space. It supports simple types like char and int, but not larger
517 * data types like structures or arrays.
519 * @ptr must have pointer-to-simple-variable type, and the result of
520 * dereferencing @ptr must be assignable to @x without a cast.
522 * Caller must check the pointer with access_ok() before calling this
525 * Returns zero on success, or -EFAULT on error.
526 * On error, the variable @x is set to zero.
529 #define __get_user(x, ptr) \
530 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
533 * __put_user: - Write a simple value into user space, with less checking.
534 * @x: Value to copy to user space.
535 * @ptr: Destination address, in user space.
537 * Context: User context only. This function may sleep if pagefaults are
540 * This macro copies a single simple value from kernel space to user
541 * space. It supports simple types like char and int, but not larger
542 * data types like structures or arrays.
544 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
545 * to the result of dereferencing @ptr.
547 * Caller must check the pointer with access_ok() before calling this
550 * Returns zero on success, or -EFAULT on error.
553 #define __put_user(x, ptr) \
554 __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
557 * {get|put}_user_try and catch
561 * } get_user_catch(err)
563 #define get_user_try uaccess_try_nospec
564 #define get_user_catch(err) uaccess_catch(err)
566 #define get_user_ex(x, ptr) do { \
567 unsigned long __gue_val; \
568 __get_user_size_ex((__gue_val), (ptr), (sizeof(*(ptr)))); \
569 (x) = (__force __typeof__(*(ptr)))__gue_val; \
572 #define put_user_try uaccess_try
573 #define put_user_catch(err) uaccess_catch(err)
575 #define put_user_ex(x, ptr) \
576 __put_user_size_ex((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
579 copy_from_user_nmi(void *to, const void __user *from, unsigned long n);
580 extern __must_check long
581 strncpy_from_user(char *dst, const char __user *src, long count);
583 extern __must_check long strnlen_user(const char __user *str, long n);
585 unsigned long __must_check clear_user(void __user *mem, unsigned long len);
586 unsigned long __must_check __clear_user(void __user *mem, unsigned long len);
588 extern void __cmpxchg_wrong_size(void)
589 __compiletime_error("Bad argument size for cmpxchg");
591 #define __user_atomic_cmpxchg_inatomic(uval, ptr, old, new, size) \
594 __typeof__(ptr) __uval = (uval); \
595 __typeof__(*(ptr)) __old = (old); \
596 __typeof__(*(ptr)) __new = (new); \
597 __uaccess_begin_nospec(); \
602 "1:\t" LOCK_PREFIX "cmpxchgb %4, %2\n" \
604 "\t.section .fixup, \"ax\"\n" \
608 _ASM_EXTABLE(1b, 3b) \
609 : "+r" (__ret), "=a" (__old), "+m" (*(ptr)) \
610 : "i" (-EFAULT), "q" (__new), "1" (__old) \
618 "1:\t" LOCK_PREFIX "cmpxchgw %4, %2\n" \
620 "\t.section .fixup, \"ax\"\n" \
624 _ASM_EXTABLE(1b, 3b) \
625 : "+r" (__ret), "=a" (__old), "+m" (*(ptr)) \
626 : "i" (-EFAULT), "r" (__new), "1" (__old) \
634 "1:\t" LOCK_PREFIX "cmpxchgl %4, %2\n" \
636 "\t.section .fixup, \"ax\"\n" \
640 _ASM_EXTABLE(1b, 3b) \
641 : "+r" (__ret), "=a" (__old), "+m" (*(ptr)) \
642 : "i" (-EFAULT), "r" (__new), "1" (__old) \
649 if (!IS_ENABLED(CONFIG_X86_64)) \
650 __cmpxchg_wrong_size(); \
653 "1:\t" LOCK_PREFIX "cmpxchgq %4, %2\n" \
655 "\t.section .fixup, \"ax\"\n" \
659 _ASM_EXTABLE(1b, 3b) \
660 : "+r" (__ret), "=a" (__old), "+m" (*(ptr)) \
661 : "i" (-EFAULT), "r" (__new), "1" (__old) \
667 __cmpxchg_wrong_size(); \
674 #define user_atomic_cmpxchg_inatomic(uval, ptr, old, new) \
676 access_ok(VERIFY_WRITE, (ptr), sizeof(*(ptr))) ? \
677 __user_atomic_cmpxchg_inatomic((uval), (ptr), \
678 (old), (new), sizeof(*(ptr))) : \
683 * movsl can be slow when source and dest are not both 8-byte aligned
685 #ifdef CONFIG_X86_INTEL_USERCOPY
686 extern struct movsl_mask {
688 } ____cacheline_aligned_in_smp movsl_mask;
691 #define ARCH_HAS_NOCACHE_UACCESS 1
694 # include <asm/uaccess_32.h>
696 # include <asm/uaccess_64.h>
700 * We rely on the nested NMI work to allow atomic faults from the NMI path; the
701 * nested NMI paths are careful to preserve CR2.
703 * Caller must use pagefault_enable/disable, or run in interrupt context,
704 * and also do a uaccess_ok() check
706 #define __copy_from_user_nmi __copy_from_user_inatomic
709 * The "unsafe" user accesses aren't really "unsafe", but the naming
710 * is a big fat warning: you have to not only do the access_ok()
711 * checking before using them, but you have to surround them with the
712 * user_access_begin/end() pair.
714 static __must_check inline bool user_access_begin(int type,
715 const void __user *ptr,
718 if (unlikely(!access_ok(type, ptr, len)))
720 __uaccess_begin_nospec();
724 #define user_access_begin(a, b, c) user_access_begin(a, b, c)
725 #define user_access_end() __uaccess_end()
727 #define unsafe_put_user(x, ptr, err_label) \
730 __typeof__(*(ptr)) __pu_val = (x); \
731 __put_user_size(__pu_val, (ptr), sizeof(*(ptr)), __pu_err, -EFAULT); \
732 if (unlikely(__pu_err)) goto err_label; \
735 #define unsafe_get_user(x, ptr, err_label) \
738 __inttype(*(ptr)) __gu_val; \
739 __get_user_size(__gu_val, (ptr), sizeof(*(ptr)), __gu_err, -EFAULT); \
740 (x) = (__force __typeof__(*(ptr)))__gu_val; \
741 if (unlikely(__gu_err)) goto err_label; \
744 #endif /* _ASM_X86_UACCESS_H */