1 #ifndef _ASM_X86_UACCESS_H
2 #define _ASM_X86_UACCESS_H
4 * User space memory access functions
6 #include <linux/errno.h>
7 #include <linux/compiler.h>
8 #include <linux/thread_info.h>
9 #include <linux/string.h>
10 #include <linux/preempt.h>
16 #define VERIFY_WRITE 1
19 * The fs value determines whether argument validity checking should be
20 * performed or not. If get_fs() == USER_DS, checking is performed, with
21 * get_fs() == KERNEL_DS, checking is bypassed.
23 * For historical reasons, these macros are grossly misnamed.
26 #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
28 #define KERNEL_DS MAKE_MM_SEG(-1UL)
29 #define USER_DS MAKE_MM_SEG(TASK_SIZE_MAX)
31 #define get_ds() (KERNEL_DS)
32 #define get_fs() (current_thread_info()->addr_limit)
33 #define set_fs(x) (current_thread_info()->addr_limit = (x))
35 #define segment_eq(a, b) ((a).seg == (b).seg)
37 #define user_addr_max() (current_thread_info()->addr_limit.seg)
38 #define __addr_ok(addr) \
39 ((unsigned long __force)(addr) < user_addr_max())
42 * Test whether a block of memory is a valid user space address.
43 * Returns 0 if the range is valid, nonzero otherwise.
45 static inline bool __chk_range_not_ok(unsigned long addr, unsigned long size, unsigned long limit)
48 * If we have used "sizeof()" for the size,
49 * we know it won't overflow the limit (but
50 * it might overflow the 'addr', so it's
51 * important to subtract the size from the
52 * limit, not add it to the address).
54 if (__builtin_constant_p(size))
55 return unlikely(addr > limit - size);
57 /* Arbitrary sizes? Be careful about overflow */
59 if (unlikely(addr < size))
61 return unlikely(addr > limit);
64 #define __range_not_ok(addr, size, limit) \
66 __chk_user_ptr(addr); \
67 __chk_range_not_ok((unsigned long __force)(addr), size, limit); \
70 #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
71 # define WARN_ON_IN_IRQ() WARN_ON_ONCE(!in_task())
73 # define WARN_ON_IN_IRQ()
77 * access_ok: - Checks if a user space pointer is valid
78 * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
79 * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
80 * to write to a block, it is always safe to read from it.
81 * @addr: User space pointer to start of block to check
82 * @size: Size of block to check
84 * Context: User context only. This function may sleep if pagefaults are
87 * Checks if a pointer to a block of memory in user space is valid.
89 * Returns true (nonzero) if the memory block may be valid, false (zero)
90 * if it is definitely invalid.
92 * Note that, depending on architecture, this function probably just
93 * checks that the pointer is in the user space range - after calling
94 * this function, memory access functions may still return -EFAULT.
96 #define access_ok(type, addr, size) \
99 likely(!__range_not_ok(addr, size, user_addr_max())); \
103 * The exception table consists of pairs of addresses relative to the
104 * exception table enty itself: the first is the address of an
105 * instruction that is allowed to fault, and the second is the address
106 * at which the program should continue. No registers are modified,
107 * so it is entirely up to the continuation code to figure out what to
110 * All the routines below use bits of fixup code that are out of line
111 * with the main instruction path. This means when everything is well,
112 * we don't even have to jump over them. Further, they do not intrude
113 * on our cache or tlb entries.
116 struct exception_table_entry {
119 /* This is not the generic standard exception_table_entry format */
120 #define ARCH_HAS_SORT_EXTABLE
121 #define ARCH_HAS_SEARCH_EXTABLE
123 extern int fixup_exception(struct pt_regs *regs);
124 extern int early_fixup_exception(unsigned long *ip);
127 * These are the main single-value transfer routines. They automatically
128 * use the right size if we just have the right pointer type.
130 * This gets kind of ugly. We want to return _two_ values in "get_user()"
131 * and yet we don't want to do any pointers, because that is too much
132 * of a performance impact. Thus we have a few rather ugly macros here,
133 * and hide all the ugliness from the user.
135 * The "__xxx" versions of the user access functions are versions that
136 * do not verify the address space, that must have been done previously
137 * with a separate "access_ok()" call (this is used when we do multiple
138 * accesses to the same area of user memory).
141 extern int __get_user_1(void);
142 extern int __get_user_2(void);
143 extern int __get_user_4(void);
144 extern int __get_user_8(void);
145 extern int __get_user_bad(void);
147 #define __uaccess_begin() stac()
148 #define __uaccess_end() clac()
149 #define __uaccess_begin_nospec() \
156 * This is a type: either unsigned long, if the argument fits into
157 * that type, or otherwise unsigned long long.
159 #define __inttype(x) \
160 __typeof__(__builtin_choose_expr(sizeof(x) > sizeof(0UL), 0ULL, 0UL))
163 * get_user: - Get a simple variable from user space.
164 * @x: Variable to store result.
165 * @ptr: Source address, in user space.
167 * Context: User context only. This function may sleep if pagefaults are
170 * This macro copies a single simple variable from user space to kernel
171 * space. It supports simple types like char and int, but not larger
172 * data types like structures or arrays.
174 * @ptr must have pointer-to-simple-variable type, and the result of
175 * dereferencing @ptr must be assignable to @x without a cast.
177 * Returns zero on success, or -EFAULT on error.
178 * On error, the variable @x is set to zero.
181 * Careful: we have to cast the result to the type of the pointer
184 * The use of _ASM_DX as the register specifier is a bit of a
185 * simplification, as gcc only cares about it as the starting point
186 * and not size: for a 64-bit value it will use %ecx:%edx on 32 bits
187 * (%ecx being the next register in gcc's x86 register sequence), and
190 * Clang/LLVM cares about the size of the register, but still wants
191 * the base register for something that ends up being a pair.
193 #define get_user(x, ptr) \
196 register __inttype(*(ptr)) __val_gu asm("%"_ASM_DX); \
197 __chk_user_ptr(ptr); \
199 asm volatile("call __get_user_%P3" \
200 : "=a" (__ret_gu), "=r" (__val_gu) \
201 : "0" (ptr), "i" (sizeof(*(ptr)))); \
202 (x) = (__force __typeof__(*(ptr))) __val_gu; \
203 __builtin_expect(__ret_gu, 0); \
206 #define __put_user_x(size, x, ptr, __ret_pu) \
207 asm volatile("call __put_user_" #size : "=a" (__ret_pu) \
208 : "0" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx")
213 #define __put_user_asm_u64(x, addr, err, errret) \
215 "1: movl %%eax,0(%2)\n" \
216 "2: movl %%edx,4(%2)\n" \
218 ".section .fixup,\"ax\"\n" \
222 _ASM_EXTABLE(1b, 4b) \
223 _ASM_EXTABLE(2b, 4b) \
225 : "A" (x), "r" (addr), "i" (errret), "0" (err))
227 #define __put_user_asm_ex_u64(x, addr) \
229 "1: movl %%eax,0(%1)\n" \
230 "2: movl %%edx,4(%1)\n" \
232 _ASM_EXTABLE_EX(1b, 2b) \
233 _ASM_EXTABLE_EX(2b, 3b) \
234 : : "A" (x), "r" (addr))
236 #define __put_user_x8(x, ptr, __ret_pu) \
237 asm volatile("call __put_user_8" : "=a" (__ret_pu) \
238 : "A" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx")
240 #define __put_user_asm_u64(x, ptr, retval, errret) \
241 __put_user_asm(x, ptr, retval, "q", "", "er", errret)
242 #define __put_user_asm_ex_u64(x, addr) \
243 __put_user_asm_ex(x, addr, "q", "", "er")
244 #define __put_user_x8(x, ptr, __ret_pu) __put_user_x(8, x, ptr, __ret_pu)
247 extern void __put_user_bad(void);
250 * Strange magic calling convention: pointer in %ecx,
251 * value in %eax(:%edx), return value in %eax. clobbers %rbx
253 extern void __put_user_1(void);
254 extern void __put_user_2(void);
255 extern void __put_user_4(void);
256 extern void __put_user_8(void);
259 * put_user: - Write a simple value into user space.
260 * @x: Value to copy to user space.
261 * @ptr: Destination address, in user space.
263 * Context: User context only. This function may sleep if pagefaults are
266 * This macro copies a single simple value from kernel space to user
267 * space. It supports simple types like char and int, but not larger
268 * data types like structures or arrays.
270 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
271 * to the result of dereferencing @ptr.
273 * Returns zero on success, or -EFAULT on error.
275 #define put_user(x, ptr) \
278 __typeof__(*(ptr)) __pu_val; \
279 __chk_user_ptr(ptr); \
282 switch (sizeof(*(ptr))) { \
284 __put_user_x(1, __pu_val, ptr, __ret_pu); \
287 __put_user_x(2, __pu_val, ptr, __ret_pu); \
290 __put_user_x(4, __pu_val, ptr, __ret_pu); \
293 __put_user_x8(__pu_val, ptr, __ret_pu); \
296 __put_user_x(X, __pu_val, ptr, __ret_pu); \
299 __builtin_expect(__ret_pu, 0); \
302 #define __put_user_size(x, ptr, size, retval, errret) \
305 __chk_user_ptr(ptr); \
308 __put_user_asm(x, ptr, retval, "b", "b", "iq", errret); \
311 __put_user_asm(x, ptr, retval, "w", "w", "ir", errret); \
314 __put_user_asm(x, ptr, retval, "l", "k", "ir", errret); \
317 __put_user_asm_u64(x, ptr, retval, errret); \
325 * This doesn't do __uaccess_begin/end - the exception handling
326 * around it must do that.
328 #define __put_user_size_ex(x, ptr, size) \
330 __chk_user_ptr(ptr); \
333 __put_user_asm_ex(x, ptr, "b", "b", "iq"); \
336 __put_user_asm_ex(x, ptr, "w", "w", "ir"); \
339 __put_user_asm_ex(x, ptr, "l", "k", "ir"); \
342 __put_user_asm_ex_u64((__typeof__(*ptr))(x), ptr); \
350 #define __get_user_asm_u64(x, ptr, retval, errret) (x) = __get_user_bad()
351 #define __get_user_asm_ex_u64(x, ptr) (x) = __get_user_bad()
353 #define __get_user_asm_u64(x, ptr, retval, errret) \
354 __get_user_asm(x, ptr, retval, "q", "", "=r", errret)
355 #define __get_user_asm_ex_u64(x, ptr) \
356 __get_user_asm_ex(x, ptr, "q", "", "=&r")
359 #define __get_user_size(x, ptr, size, retval, errret) \
362 __chk_user_ptr(ptr); \
365 __get_user_asm(x, ptr, retval, "b", "b", "=q", errret); \
368 __get_user_asm(x, ptr, retval, "w", "w", "=r", errret); \
371 __get_user_asm(x, ptr, retval, "l", "k", "=r", errret); \
374 __get_user_asm_u64(x, ptr, retval, errret); \
377 (x) = __get_user_bad(); \
381 #define __get_user_asm(x, addr, err, itype, rtype, ltype, errret) \
383 "1: mov"itype" %2,%"rtype"1\n" \
385 ".section .fixup,\"ax\"\n" \
387 " xor"itype" %"rtype"1,%"rtype"1\n" \
390 _ASM_EXTABLE(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 _ASM_EXTABLE_EX(1b, 2b) \
423 : ltype(x) : "m" (__m(addr)), "0" (0))
425 #define __put_user_nocheck(x, ptr, size) \
428 __typeof__(*(ptr)) __pu_val; \
431 __put_user_size(__pu_val, (ptr), (size), __pu_err, -EFAULT);\
433 __builtin_expect(__pu_err, 0); \
436 #define __get_user_nocheck(x, ptr, size) \
439 unsigned long __gu_val; \
440 __uaccess_begin_nospec(); \
441 __get_user_size(__gu_val, (ptr), (size), __gu_err, -EFAULT); \
443 (x) = (__force __typeof__(*(ptr)))__gu_val; \
444 __builtin_expect(__gu_err, 0); \
447 /* FIXME: this hack is definitely wrong -AK */
448 struct __large_struct { unsigned long buf[100]; };
449 #define __m(x) (*(struct __large_struct __user *)(x))
452 * Tell gcc we read from memory instead of writing: this is because
453 * we do not write to any memory gcc knows about, so there are no
456 #define __put_user_asm(x, addr, err, itype, rtype, ltype, errret) \
458 "1: mov"itype" %"rtype"1,%2\n" \
460 ".section .fixup,\"ax\"\n" \
464 _ASM_EXTABLE(1b, 3b) \
466 : ltype(x), "m" (__m(addr)), "i" (errret), "0" (err))
468 #define __put_user_asm_ex(x, addr, itype, rtype, ltype) \
469 asm volatile("1: mov"itype" %"rtype"0,%1\n" \
471 _ASM_EXTABLE_EX(1b, 2b) \
472 : : ltype(x), "m" (__m(addr)))
475 * uaccess_try and catch
477 #define uaccess_try do { \
478 current_thread_info()->uaccess_err = 0; \
482 #define uaccess_try_nospec do { \
483 current_thread_info()->uaccess_err = 0; \
484 __uaccess_begin_nospec(); \
486 #define uaccess_catch(err) \
488 (err) |= (current_thread_info()->uaccess_err ? -EFAULT : 0); \
492 * __get_user: - Get a simple variable from user space, with less checking.
493 * @x: Variable to store result.
494 * @ptr: Source address, in user space.
496 * Context: User context only. This function may sleep if pagefaults are
499 * This macro copies a single simple variable from user space to kernel
500 * space. It supports simple types like char and int, but not larger
501 * data types like structures or arrays.
503 * @ptr must have pointer-to-simple-variable type, and the result of
504 * dereferencing @ptr must be assignable to @x without a cast.
506 * Caller must check the pointer with access_ok() before calling this
509 * Returns zero on success, or -EFAULT on error.
510 * On error, the variable @x is set to zero.
513 #define __get_user(x, ptr) \
514 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
517 * __put_user: - Write a simple value into user space, with less checking.
518 * @x: Value to copy to user space.
519 * @ptr: Destination address, in user space.
521 * Context: User context only. This function may sleep if pagefaults are
524 * This macro copies a single simple value from kernel space to user
525 * space. It supports simple types like char and int, but not larger
526 * data types like structures or arrays.
528 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
529 * to the result of dereferencing @ptr.
531 * Caller must check the pointer with access_ok() before calling this
534 * Returns zero on success, or -EFAULT on error.
537 #define __put_user(x, ptr) \
538 __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
540 #define __get_user_unaligned __get_user
541 #define __put_user_unaligned __put_user
544 * {get|put}_user_try and catch
548 * } get_user_catch(err)
550 #define get_user_try uaccess_try_nospec
551 #define get_user_catch(err) uaccess_catch(err)
553 #define get_user_ex(x, ptr) do { \
554 unsigned long __gue_val; \
555 __get_user_size_ex((__gue_val), (ptr), (sizeof(*(ptr)))); \
556 (x) = (__force __typeof__(*(ptr)))__gue_val; \
559 #define put_user_try uaccess_try
560 #define put_user_catch(err) uaccess_catch(err)
562 #define put_user_ex(x, ptr) \
563 __put_user_size_ex((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
566 copy_from_user_nmi(void *to, const void __user *from, unsigned long n);
567 extern __must_check long
568 strncpy_from_user(char *dst, const char __user *src, long count);
570 extern __must_check long strlen_user(const char __user *str);
571 extern __must_check long strnlen_user(const char __user *str, long n);
573 unsigned long __must_check clear_user(void __user *mem, unsigned long len);
574 unsigned long __must_check __clear_user(void __user *mem, unsigned long len);
576 extern void __cmpxchg_wrong_size(void)
577 __compiletime_error("Bad argument size for cmpxchg");
579 #define __user_atomic_cmpxchg_inatomic(uval, ptr, old, new, size) \
582 __typeof__(ptr) __uval = (uval); \
583 __typeof__(*(ptr)) __old = (old); \
584 __typeof__(*(ptr)) __new = (new); \
585 __uaccess_begin_nospec(); \
590 "1:\t" LOCK_PREFIX "cmpxchgb %4, %2\n" \
592 "\t.section .fixup, \"ax\"\n" \
596 _ASM_EXTABLE(1b, 3b) \
597 : "+r" (__ret), "=a" (__old), "+m" (*(ptr)) \
598 : "i" (-EFAULT), "q" (__new), "1" (__old) \
606 "1:\t" LOCK_PREFIX "cmpxchgw %4, %2\n" \
608 "\t.section .fixup, \"ax\"\n" \
612 _ASM_EXTABLE(1b, 3b) \
613 : "+r" (__ret), "=a" (__old), "+m" (*(ptr)) \
614 : "i" (-EFAULT), "r" (__new), "1" (__old) \
622 "1:\t" LOCK_PREFIX "cmpxchgl %4, %2\n" \
624 "\t.section .fixup, \"ax\"\n" \
628 _ASM_EXTABLE(1b, 3b) \
629 : "+r" (__ret), "=a" (__old), "+m" (*(ptr)) \
630 : "i" (-EFAULT), "r" (__new), "1" (__old) \
637 if (!IS_ENABLED(CONFIG_X86_64)) \
638 __cmpxchg_wrong_size(); \
641 "1:\t" LOCK_PREFIX "cmpxchgq %4, %2\n" \
643 "\t.section .fixup, \"ax\"\n" \
647 _ASM_EXTABLE(1b, 3b) \
648 : "+r" (__ret), "=a" (__old), "+m" (*(ptr)) \
649 : "i" (-EFAULT), "r" (__new), "1" (__old) \
655 __cmpxchg_wrong_size(); \
662 #define user_atomic_cmpxchg_inatomic(uval, ptr, old, new) \
664 access_ok(VERIFY_WRITE, (ptr), sizeof(*(ptr))) ? \
665 __user_atomic_cmpxchg_inatomic((uval), (ptr), \
666 (old), (new), sizeof(*(ptr))) : \
671 * movsl can be slow when source and dest are not both 8-byte aligned
673 #ifdef CONFIG_X86_INTEL_USERCOPY
674 extern struct movsl_mask {
676 } ____cacheline_aligned_in_smp movsl_mask;
679 #define ARCH_HAS_NOCACHE_UACCESS 1
682 # include <asm/uaccess_32.h>
684 # include <asm/uaccess_64.h>
687 unsigned long __must_check _copy_from_user(void *to, const void __user *from,
689 unsigned long __must_check _copy_to_user(void __user *to, const void *from,
692 #ifdef CONFIG_DEBUG_STRICT_USER_COPY_CHECKS
693 # define copy_user_diag __compiletime_error
695 # define copy_user_diag __compiletime_warning
698 extern void copy_user_diag("copy_from_user() buffer size is too small")
699 copy_from_user_overflow(void);
700 extern void copy_user_diag("copy_to_user() buffer size is too small")
701 copy_to_user_overflow(void) __asm__("copy_from_user_overflow");
703 #undef copy_user_diag
705 #ifdef CONFIG_DEBUG_STRICT_USER_COPY_CHECKS
708 __compiletime_warning("copy_from_user() buffer size is not provably correct")
709 __copy_from_user_overflow(void) __asm__("copy_from_user_overflow");
710 #define __copy_from_user_overflow(size, count) __copy_from_user_overflow()
713 __compiletime_warning("copy_to_user() buffer size is not provably correct")
714 __copy_to_user_overflow(void) __asm__("copy_from_user_overflow");
715 #define __copy_to_user_overflow(size, count) __copy_to_user_overflow()
720 __copy_from_user_overflow(int size, unsigned long count)
722 WARN(1, "Buffer overflow detected (%d < %lu)!\n", size, count);
725 #define __copy_to_user_overflow __copy_from_user_overflow
729 static inline unsigned long __must_check
730 copy_from_user(void *to, const void __user *from, unsigned long n)
732 int sz = __compiletime_object_size(to);
737 * While we would like to have the compiler do the checking for us
738 * even in the non-constant size case, any false positives there are
739 * a problem (especially when DEBUG_STRICT_USER_COPY_CHECKS, but even
740 * without - the [hopefully] dangerous looking nature of the warning
741 * would make people go look at the respecitive call sites over and
742 * over again just to find that there's no problem).
744 * And there are cases where it's just not realistic for the compiler
745 * to prove the count to be in range. For example when multiple call
746 * sites of a helper function - perhaps in different source files -
747 * all doing proper range checking, yet the helper function not doing
750 * Therefore limit the compile time checking to the constant size
751 * case, and do only runtime checking for non-constant sizes.
754 if (likely(sz < 0 || sz >= n))
755 n = _copy_from_user(to, from, n);
756 else if(__builtin_constant_p(n))
757 copy_from_user_overflow();
759 __copy_from_user_overflow(sz, n);
764 static inline unsigned long __must_check
765 copy_to_user(void __user *to, const void *from, unsigned long n)
767 int sz = __compiletime_object_size(from);
771 /* See the comment in copy_from_user() above. */
772 if (likely(sz < 0 || sz >= n))
773 n = _copy_to_user(to, from, n);
774 else if(__builtin_constant_p(n))
775 copy_to_user_overflow();
777 __copy_to_user_overflow(sz, n);
782 #undef __copy_from_user_overflow
783 #undef __copy_to_user_overflow
785 #endif /* _ASM_X86_UACCESS_H */