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 * @addr: User space pointer to start of block to check
81 * @size: Size of block to check
83 * Context: User context only. This function may sleep if pagefaults are
86 * Checks if a pointer to a block of memory in user space is valid.
88 * Returns true (nonzero) if the memory block may be valid, false (zero)
89 * if it is definitely invalid.
91 * Note that, depending on architecture, this function probably just
92 * checks that the pointer is in the user space range - after calling
93 * this function, memory access functions may still return -EFAULT.
95 #define access_ok(addr, size) \
98 likely(!__range_not_ok(addr, size, user_addr_max())); \
102 * These are the main single-value transfer routines. They automatically
103 * use the right size if we just have the right pointer type.
105 * This gets kind of ugly. We want to return _two_ values in "get_user()"
106 * and yet we don't want to do any pointers, because that is too much
107 * of a performance impact. Thus we have a few rather ugly macros here,
108 * and hide all the ugliness from the user.
110 * The "__xxx" versions of the user access functions are versions that
111 * do not verify the address space, that must have been done previously
112 * with a separate "access_ok()" call (this is used when we do multiple
113 * accesses to the same area of user memory).
116 extern int __get_user_1(void);
117 extern int __get_user_2(void);
118 extern int __get_user_4(void);
119 extern int __get_user_8(void);
120 extern int __get_user_bad(void);
122 #define __uaccess_begin() stac()
123 #define __uaccess_end() clac()
124 #define __uaccess_begin_nospec() \
131 * This is a type: either unsigned long, if the argument fits into
132 * that type, or otherwise unsigned long long.
134 #define __inttype(x) \
135 __typeof__(__builtin_choose_expr(sizeof(x) > sizeof(0UL), 0ULL, 0UL))
138 * get_user: - Get a simple variable from user space.
139 * @x: Variable to store result.
140 * @ptr: Source address, in user space.
142 * Context: User context only. This function may sleep if pagefaults are
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.
149 * @ptr must have pointer-to-simple-variable type, and the result of
150 * dereferencing @ptr must be assignable to @x without a cast.
152 * Returns zero on success, or -EFAULT on error.
153 * On error, the variable @x is set to zero.
156 * Careful: we have to cast the result to the type of the pointer
159 * The use of _ASM_DX as the register specifier is a bit of a
160 * simplification, as gcc only cares about it as the starting point
161 * and not size: for a 64-bit value it will use %ecx:%edx on 32 bits
162 * (%ecx being the next register in gcc's x86 register sequence), and
165 * Clang/LLVM cares about the size of the register, but still wants
166 * the base register for something that ends up being a pair.
168 #define get_user(x, ptr) \
171 register __inttype(*(ptr)) __val_gu asm("%"_ASM_DX); \
172 __chk_user_ptr(ptr); \
174 asm volatile("call __get_user_%P4" \
175 : "=a" (__ret_gu), "=r" (__val_gu), \
176 ASM_CALL_CONSTRAINT \
177 : "0" (ptr), "i" (sizeof(*(ptr)))); \
178 (x) = (__force __typeof__(*(ptr))) __val_gu; \
179 __builtin_expect(__ret_gu, 0); \
182 #define __put_user_x(size, x, ptr, __ret_pu) \
183 asm volatile("call __put_user_" #size : "=a" (__ret_pu) \
184 : "0" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx")
189 #define __put_user_asm_u64(x, addr, err, errret) \
191 "1: movl %%eax,0(%2)\n" \
192 "2: movl %%edx,4(%2)\n" \
194 ".section .fixup,\"ax\"\n" \
198 _ASM_EXTABLE_UA(1b, 4b) \
199 _ASM_EXTABLE_UA(2b, 4b) \
201 : "A" (x), "r" (addr), "i" (errret), "0" (err))
203 #define __put_user_asm_ex_u64(x, addr) \
205 "1: movl %%eax,0(%1)\n" \
206 "2: movl %%edx,4(%1)\n" \
208 _ASM_EXTABLE_EX(1b, 2b) \
209 _ASM_EXTABLE_EX(2b, 3b) \
210 : : "A" (x), "r" (addr))
212 #define __put_user_x8(x, ptr, __ret_pu) \
213 asm volatile("call __put_user_8" : "=a" (__ret_pu) \
214 : "A" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx")
216 #define __put_user_asm_u64(x, ptr, retval, errret) \
217 __put_user_asm(x, ptr, retval, "q", "", "er", errret)
218 #define __put_user_asm_ex_u64(x, addr) \
219 __put_user_asm_ex(x, addr, "q", "", "er")
220 #define __put_user_x8(x, ptr, __ret_pu) __put_user_x(8, x, ptr, __ret_pu)
223 extern void __put_user_bad(void);
226 * Strange magic calling convention: pointer in %ecx,
227 * value in %eax(:%edx), return value in %eax. clobbers %rbx
229 extern void __put_user_1(void);
230 extern void __put_user_2(void);
231 extern void __put_user_4(void);
232 extern void __put_user_8(void);
235 * put_user: - Write a simple value into user space.
236 * @x: Value to copy to user space.
237 * @ptr: Destination address, in user space.
239 * Context: User context only. This function may sleep if pagefaults are
242 * This macro copies a single simple value from kernel space to user
243 * space. It supports simple types like char and int, but not larger
244 * data types like structures or arrays.
246 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
247 * to the result of dereferencing @ptr.
249 * Returns zero on success, or -EFAULT on error.
251 #define put_user(x, ptr) \
254 __typeof__(*(ptr)) __pu_val; \
255 __chk_user_ptr(ptr); \
258 switch (sizeof(*(ptr))) { \
260 __put_user_x(1, __pu_val, ptr, __ret_pu); \
263 __put_user_x(2, __pu_val, ptr, __ret_pu); \
266 __put_user_x(4, __pu_val, ptr, __ret_pu); \
269 __put_user_x8(__pu_val, ptr, __ret_pu); \
272 __put_user_x(X, __pu_val, ptr, __ret_pu); \
275 __builtin_expect(__ret_pu, 0); \
278 #define __put_user_size(x, ptr, size, retval, errret) \
281 __chk_user_ptr(ptr); \
284 __put_user_asm(x, ptr, retval, "b", "b", "iq", errret); \
287 __put_user_asm(x, ptr, retval, "w", "w", "ir", errret); \
290 __put_user_asm(x, ptr, retval, "l", "k", "ir", errret); \
293 __put_user_asm_u64((__typeof__(*ptr))(x), ptr, retval, \
302 * This doesn't do __uaccess_begin/end - the exception handling
303 * around it must do that.
305 #define __put_user_size_ex(x, ptr, size) \
307 __chk_user_ptr(ptr); \
310 __put_user_asm_ex(x, ptr, "b", "b", "iq"); \
313 __put_user_asm_ex(x, ptr, "w", "w", "ir"); \
316 __put_user_asm_ex(x, ptr, "l", "k", "ir"); \
319 __put_user_asm_ex_u64((__typeof__(*ptr))(x), ptr); \
327 #define __get_user_asm_u64(x, ptr, retval, errret) \
329 __typeof__(ptr) __ptr = (ptr); \
331 "1: movl %2,%%eax\n" \
332 "2: movl %3,%%edx\n" \
334 ".section .fixup,\"ax\"\n" \
336 " xorl %%eax,%%eax\n" \
337 " xorl %%edx,%%edx\n" \
340 _ASM_EXTABLE_UA(1b, 4b) \
341 _ASM_EXTABLE_UA(2b, 4b) \
342 : "=r" (retval), "=&A"(x) \
343 : "m" (__m(__ptr)), "m" __m(((u32 __user *)(__ptr)) + 1), \
344 "i" (errret), "0" (retval)); \
347 #define __get_user_asm_ex_u64(x, ptr) (x) = __get_user_bad()
349 #define __get_user_asm_u64(x, ptr, retval, errret) \
350 __get_user_asm(x, ptr, retval, "q", "", "=r", errret)
351 #define __get_user_asm_ex_u64(x, ptr) \
352 __get_user_asm_ex(x, ptr, "q", "", "=r")
355 #define __get_user_size(x, ptr, size, retval, errret) \
358 __chk_user_ptr(ptr); \
361 __get_user_asm(x, ptr, retval, "b", "b", "=q", errret); \
364 __get_user_asm(x, ptr, retval, "w", "w", "=r", errret); \
367 __get_user_asm(x, ptr, retval, "l", "k", "=r", errret); \
370 __get_user_asm_u64(x, ptr, retval, errret); \
373 (x) = __get_user_bad(); \
377 #define __get_user_asm(x, addr, err, itype, rtype, ltype, errret) \
379 "1: mov"itype" %2,%"rtype"1\n" \
381 ".section .fixup,\"ax\"\n" \
383 " xor"itype" %"rtype"1,%"rtype"1\n" \
386 _ASM_EXTABLE_UA(1b, 3b) \
387 : "=r" (err), ltype(x) \
388 : "m" (__m(addr)), "i" (errret), "0" (err))
390 #define __get_user_asm_nozero(x, addr, err, itype, rtype, ltype, errret) \
392 "1: mov"itype" %2,%"rtype"1\n" \
394 ".section .fixup,\"ax\"\n" \
398 _ASM_EXTABLE_UA(1b, 3b) \
399 : "=r" (err), ltype(x) \
400 : "m" (__m(addr)), "i" (errret), "0" (err))
403 * This doesn't do __uaccess_begin/end - the exception handling
404 * around it must do that.
406 #define __get_user_size_ex(x, ptr, size) \
408 __chk_user_ptr(ptr); \
411 __get_user_asm_ex(x, ptr, "b", "b", "=q"); \
414 __get_user_asm_ex(x, ptr, "w", "w", "=r"); \
417 __get_user_asm_ex(x, ptr, "l", "k", "=r"); \
420 __get_user_asm_ex_u64(x, ptr); \
423 (x) = __get_user_bad(); \
427 #define __get_user_asm_ex(x, addr, itype, rtype, ltype) \
428 asm volatile("1: mov"itype" %1,%"rtype"0\n" \
430 ".section .fixup,\"ax\"\n" \
431 "3:xor"itype" %"rtype"0,%"rtype"0\n" \
434 _ASM_EXTABLE_EX(1b, 3b) \
435 : ltype(x) : "m" (__m(addr)))
437 #define __put_user_nocheck(x, ptr, size) \
441 __put_user_size((x), (ptr), (size), __pu_err, -EFAULT); \
443 __builtin_expect(__pu_err, 0); \
446 #define __get_user_nocheck(x, ptr, size) \
449 __inttype(*(ptr)) __gu_val; \
450 __uaccess_begin_nospec(); \
451 __get_user_size(__gu_val, (ptr), (size), __gu_err, -EFAULT); \
453 (x) = (__force __typeof__(*(ptr)))__gu_val; \
454 __builtin_expect(__gu_err, 0); \
457 /* FIXME: this hack is definitely wrong -AK */
458 struct __large_struct { unsigned long buf[100]; };
459 #define __m(x) (*(struct __large_struct __user *)(x))
462 * Tell gcc we read from memory instead of writing: this is because
463 * we do not write to any memory gcc knows about, so there are no
466 #define __put_user_asm(x, addr, err, itype, rtype, ltype, errret) \
468 "1: mov"itype" %"rtype"1,%2\n" \
470 ".section .fixup,\"ax\"\n" \
474 _ASM_EXTABLE_UA(1b, 3b) \
476 : ltype(x), "m" (__m(addr)), "i" (errret), "0" (err))
478 #define __put_user_asm_ex(x, addr, itype, rtype, ltype) \
479 asm volatile("1: mov"itype" %"rtype"0,%1\n" \
481 _ASM_EXTABLE_EX(1b, 2b) \
482 : : ltype(x), "m" (__m(addr)))
485 * uaccess_try and catch
487 #define uaccess_try do { \
488 current->thread.uaccess_err = 0; \
492 #define uaccess_try_nospec do { \
493 current->thread.uaccess_err = 0; \
494 __uaccess_begin_nospec(); \
496 #define uaccess_catch(err) \
498 (err) |= (current->thread.uaccess_err ? -EFAULT : 0); \
502 * __get_user: - Get a simple variable from user space, with less checking.
503 * @x: Variable to store result.
504 * @ptr: Source address, in user space.
506 * Context: User context only. This function may sleep if pagefaults are
509 * This macro copies a single simple variable from user space to kernel
510 * space. It supports simple types like char and int, but not larger
511 * data types like structures or arrays.
513 * @ptr must have pointer-to-simple-variable type, and the result of
514 * dereferencing @ptr must be assignable to @x without a cast.
516 * Caller must check the pointer with access_ok() before calling this
519 * Returns zero on success, or -EFAULT on error.
520 * On error, the variable @x is set to zero.
523 #define __get_user(x, ptr) \
524 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
527 * __put_user: - Write a simple value into user space, with less checking.
528 * @x: Value to copy to user space.
529 * @ptr: Destination address, in user space.
531 * Context: User context only. This function may sleep if pagefaults are
534 * This macro copies a single simple value from kernel space to user
535 * space. It supports simple types like char and int, but not larger
536 * data types like structures or arrays.
538 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
539 * to the result of dereferencing @ptr.
541 * Caller must check the pointer with access_ok() before calling this
544 * Returns zero on success, or -EFAULT on error.
547 #define __put_user(x, ptr) \
548 __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
551 * {get|put}_user_try and catch
555 * } get_user_catch(err)
557 #define get_user_try uaccess_try_nospec
558 #define get_user_catch(err) uaccess_catch(err)
560 #define get_user_ex(x, ptr) do { \
561 unsigned long __gue_val; \
562 __get_user_size_ex((__gue_val), (ptr), (sizeof(*(ptr)))); \
563 (x) = (__force __typeof__(*(ptr)))__gue_val; \
566 #define put_user_try uaccess_try
567 #define put_user_catch(err) uaccess_catch(err)
569 #define put_user_ex(x, ptr) \
570 __put_user_size_ex((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
573 copy_from_user_nmi(void *to, const void __user *from, unsigned long n);
574 extern __must_check long
575 strncpy_from_user(char *dst, const char __user *src, long count);
577 extern __must_check long strnlen_user(const char __user *str, long n);
579 unsigned long __must_check clear_user(void __user *mem, unsigned long len);
580 unsigned long __must_check __clear_user(void __user *mem, unsigned long len);
582 extern void __cmpxchg_wrong_size(void)
583 __compiletime_error("Bad argument size for cmpxchg");
585 #define __user_atomic_cmpxchg_inatomic(uval, ptr, old, new, size) \
588 __typeof__(ptr) __uval = (uval); \
589 __typeof__(*(ptr)) __old = (old); \
590 __typeof__(*(ptr)) __new = (new); \
591 __uaccess_begin_nospec(); \
596 "1:\t" LOCK_PREFIX "cmpxchgb %4, %2\n" \
598 "\t.section .fixup, \"ax\"\n" \
602 _ASM_EXTABLE_UA(1b, 3b) \
603 : "+r" (__ret), "=a" (__old), "+m" (*(ptr)) \
604 : "i" (-EFAULT), "q" (__new), "1" (__old) \
612 "1:\t" LOCK_PREFIX "cmpxchgw %4, %2\n" \
614 "\t.section .fixup, \"ax\"\n" \
618 _ASM_EXTABLE_UA(1b, 3b) \
619 : "+r" (__ret), "=a" (__old), "+m" (*(ptr)) \
620 : "i" (-EFAULT), "r" (__new), "1" (__old) \
628 "1:\t" LOCK_PREFIX "cmpxchgl %4, %2\n" \
630 "\t.section .fixup, \"ax\"\n" \
634 _ASM_EXTABLE_UA(1b, 3b) \
635 : "+r" (__ret), "=a" (__old), "+m" (*(ptr)) \
636 : "i" (-EFAULT), "r" (__new), "1" (__old) \
643 if (!IS_ENABLED(CONFIG_X86_64)) \
644 __cmpxchg_wrong_size(); \
647 "1:\t" LOCK_PREFIX "cmpxchgq %4, %2\n" \
649 "\t.section .fixup, \"ax\"\n" \
653 _ASM_EXTABLE_UA(1b, 3b) \
654 : "+r" (__ret), "=a" (__old), "+m" (*(ptr)) \
655 : "i" (-EFAULT), "r" (__new), "1" (__old) \
661 __cmpxchg_wrong_size(); \
668 #define user_atomic_cmpxchg_inatomic(uval, ptr, old, new) \
670 access_ok((ptr), sizeof(*(ptr))) ? \
671 __user_atomic_cmpxchg_inatomic((uval), (ptr), \
672 (old), (new), sizeof(*(ptr))) : \
677 * movsl can be slow when source and dest are not both 8-byte aligned
679 #ifdef CONFIG_X86_INTEL_USERCOPY
680 extern struct movsl_mask {
682 } ____cacheline_aligned_in_smp movsl_mask;
685 #define ARCH_HAS_NOCACHE_UACCESS 1
688 # include <asm/uaccess_32.h>
690 # include <asm/uaccess_64.h>
694 * We rely on the nested NMI work to allow atomic faults from the NMI path; the
695 * nested NMI paths are careful to preserve CR2.
697 * Caller must use pagefault_enable/disable, or run in interrupt context,
698 * and also do a uaccess_ok() check
700 #define __copy_from_user_nmi __copy_from_user_inatomic
703 * The "unsafe" user accesses aren't really "unsafe", but the naming
704 * is a big fat warning: you have to not only do the access_ok()
705 * checking before using them, but you have to surround them with the
706 * user_access_begin/end() pair.
708 static __must_check inline bool user_access_begin(const void __user *ptr, size_t len)
710 if (unlikely(!access_ok(ptr,len)))
715 #define user_access_begin(a,b) user_access_begin(a,b)
716 #define user_access_end() __uaccess_end()
718 #define unsafe_put_user(x, ptr, err_label) \
721 __typeof__(*(ptr)) __pu_val = (x); \
722 __put_user_size(__pu_val, (ptr), sizeof(*(ptr)), __pu_err, -EFAULT); \
723 if (unlikely(__pu_err)) goto err_label; \
726 #define unsafe_get_user(x, ptr, err_label) \
729 __inttype(*(ptr)) __gu_val; \
730 __get_user_size(__gu_val, (ptr), sizeof(*(ptr)), __gu_err, -EFAULT); \
731 (x) = (__force __typeof__(*(ptr)))__gu_val; \
732 if (unlikely(__gu_err)) goto err_label; \
735 #endif /* _ASM_X86_UACCESS_H */