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()
129 * This is a type: either unsigned long, if the argument fits into
130 * that type, or otherwise unsigned long long.
132 #define __inttype(x) \
133 __typeof__(__builtin_choose_expr(sizeof(x) > sizeof(0UL), 0ULL, 0UL))
136 * get_user: - Get a simple variable from user space.
137 * @x: Variable to store result.
138 * @ptr: Source address, in user space.
140 * Context: User context only. This function may sleep if pagefaults are
143 * This macro copies a single simple variable from user space to kernel
144 * space. It supports simple types like char and int, but not larger
145 * data types like structures or arrays.
147 * @ptr must have pointer-to-simple-variable type, and the result of
148 * dereferencing @ptr must be assignable to @x without a cast.
150 * Returns zero on success, or -EFAULT on error.
151 * On error, the variable @x is set to zero.
154 * Careful: we have to cast the result to the type of the pointer
157 * The use of _ASM_DX as the register specifier is a bit of a
158 * simplification, as gcc only cares about it as the starting point
159 * and not size: for a 64-bit value it will use %ecx:%edx on 32 bits
160 * (%ecx being the next register in gcc's x86 register sequence), and
163 * Clang/LLVM cares about the size of the register, but still wants
164 * the base register for something that ends up being a pair.
166 #define get_user(x, ptr) \
169 register __inttype(*(ptr)) __val_gu asm("%"_ASM_DX); \
170 __chk_user_ptr(ptr); \
172 asm volatile("call __get_user_%P4" \
173 : "=a" (__ret_gu), "=r" (__val_gu), \
174 ASM_CALL_CONSTRAINT \
175 : "0" (ptr), "i" (sizeof(*(ptr)))); \
176 (x) = (__force __typeof__(*(ptr))) __val_gu; \
177 __builtin_expect(__ret_gu, 0); \
180 #define __put_user_x(size, x, ptr, __ret_pu) \
181 asm volatile("call __put_user_" #size : "=a" (__ret_pu) \
182 : "0" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx")
187 #define __put_user_asm_u64(x, addr, err, errret) \
189 "1: movl %%eax,0(%2)\n" \
190 "2: movl %%edx,4(%2)\n" \
192 ".section .fixup,\"ax\"\n" \
196 _ASM_EXTABLE(1b, 4b) \
197 _ASM_EXTABLE(2b, 4b) \
199 : "A" (x), "r" (addr), "i" (errret), "0" (err))
201 #define __put_user_asm_ex_u64(x, addr) \
203 "1: movl %%eax,0(%1)\n" \
204 "2: movl %%edx,4(%1)\n" \
206 _ASM_EXTABLE_EX(1b, 2b) \
207 _ASM_EXTABLE_EX(2b, 3b) \
208 : : "A" (x), "r" (addr))
210 #define __put_user_x8(x, ptr, __ret_pu) \
211 asm volatile("call __put_user_8" : "=a" (__ret_pu) \
212 : "A" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx")
214 #define __put_user_asm_u64(x, ptr, retval, errret) \
215 __put_user_asm(x, ptr, retval, "q", "", "er", errret)
216 #define __put_user_asm_ex_u64(x, addr) \
217 __put_user_asm_ex(x, addr, "q", "", "er")
218 #define __put_user_x8(x, ptr, __ret_pu) __put_user_x(8, x, ptr, __ret_pu)
221 extern void __put_user_bad(void);
224 * Strange magic calling convention: pointer in %ecx,
225 * value in %eax(:%edx), return value in %eax. clobbers %rbx
227 extern void __put_user_1(void);
228 extern void __put_user_2(void);
229 extern void __put_user_4(void);
230 extern void __put_user_8(void);
233 * put_user: - Write a simple value into user space.
234 * @x: Value to copy to user space.
235 * @ptr: Destination address, in user space.
237 * Context: User context only. This function may sleep if pagefaults are
240 * This macro copies a single simple value from kernel space to user
241 * space. It supports simple types like char and int, but not larger
242 * data types like structures or arrays.
244 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
245 * to the result of dereferencing @ptr.
247 * Returns zero on success, or -EFAULT on error.
249 #define put_user(x, ptr) \
252 __typeof__(*(ptr)) __pu_val; \
253 __chk_user_ptr(ptr); \
256 switch (sizeof(*(ptr))) { \
258 __put_user_x(1, __pu_val, ptr, __ret_pu); \
261 __put_user_x(2, __pu_val, ptr, __ret_pu); \
264 __put_user_x(4, __pu_val, ptr, __ret_pu); \
267 __put_user_x8(__pu_val, ptr, __ret_pu); \
270 __put_user_x(X, __pu_val, ptr, __ret_pu); \
273 __builtin_expect(__ret_pu, 0); \
276 #define __put_user_size(x, ptr, size, retval, errret) \
279 __chk_user_ptr(ptr); \
282 __put_user_asm(x, ptr, retval, "b", "b", "iq", errret); \
285 __put_user_asm(x, ptr, retval, "w", "w", "ir", errret); \
288 __put_user_asm(x, ptr, retval, "l", "k", "ir", errret); \
291 __put_user_asm_u64((__typeof__(*ptr))(x), ptr, retval, \
300 * This doesn't do __uaccess_begin/end - the exception handling
301 * around it must do that.
303 #define __put_user_size_ex(x, ptr, size) \
305 __chk_user_ptr(ptr); \
308 __put_user_asm_ex(x, ptr, "b", "b", "iq"); \
311 __put_user_asm_ex(x, ptr, "w", "w", "ir"); \
314 __put_user_asm_ex(x, ptr, "l", "k", "ir"); \
317 __put_user_asm_ex_u64((__typeof__(*ptr))(x), ptr); \
325 #define __get_user_asm_u64(x, ptr, retval, errret) \
327 __typeof__(ptr) __ptr = (ptr); \
329 "1: movl %2,%%eax\n" \
330 "2: movl %3,%%edx\n" \
332 ".section .fixup,\"ax\"\n" \
334 " xorl %%eax,%%eax\n" \
335 " xorl %%edx,%%edx\n" \
338 _ASM_EXTABLE(1b, 4b) \
339 _ASM_EXTABLE(2b, 4b) \
340 : "=r" (retval), "=&A"(x) \
341 : "m" (__m(__ptr)), "m" __m(((u32 __user *)(__ptr)) + 1), \
342 "i" (errret), "0" (retval)); \
345 #define __get_user_asm_ex_u64(x, ptr) (x) = __get_user_bad()
347 #define __get_user_asm_u64(x, ptr, retval, errret) \
348 __get_user_asm(x, ptr, retval, "q", "", "=r", errret)
349 #define __get_user_asm_ex_u64(x, ptr) \
350 __get_user_asm_ex(x, ptr, "q", "", "=r")
353 #define __get_user_size(x, ptr, size, retval, errret) \
356 __chk_user_ptr(ptr); \
359 __get_user_asm(x, ptr, retval, "b", "b", "=q", errret); \
362 __get_user_asm(x, ptr, retval, "w", "w", "=r", errret); \
365 __get_user_asm(x, ptr, retval, "l", "k", "=r", errret); \
368 __get_user_asm_u64(x, ptr, retval, errret); \
371 (x) = __get_user_bad(); \
375 #define __get_user_asm(x, addr, err, itype, rtype, ltype, errret) \
377 "1: mov"itype" %2,%"rtype"1\n" \
379 ".section .fixup,\"ax\"\n" \
381 " xor"itype" %"rtype"1,%"rtype"1\n" \
384 _ASM_EXTABLE(1b, 3b) \
385 : "=r" (err), ltype(x) \
386 : "m" (__m(addr)), "i" (errret), "0" (err))
388 #define __get_user_asm_nozero(x, addr, err, itype, rtype, ltype, errret) \
390 "1: mov"itype" %2,%"rtype"1\n" \
392 ".section .fixup,\"ax\"\n" \
396 _ASM_EXTABLE(1b, 3b) \
397 : "=r" (err), ltype(x) \
398 : "m" (__m(addr)), "i" (errret), "0" (err))
401 * This doesn't do __uaccess_begin/end - the exception handling
402 * around it must do that.
404 #define __get_user_size_ex(x, ptr, size) \
406 __chk_user_ptr(ptr); \
409 __get_user_asm_ex(x, ptr, "b", "b", "=q"); \
412 __get_user_asm_ex(x, ptr, "w", "w", "=r"); \
415 __get_user_asm_ex(x, ptr, "l", "k", "=r"); \
418 __get_user_asm_ex_u64(x, ptr); \
421 (x) = __get_user_bad(); \
425 #define __get_user_asm_ex(x, addr, itype, rtype, ltype) \
426 asm volatile("1: mov"itype" %1,%"rtype"0\n" \
428 ".section .fixup,\"ax\"\n" \
429 "3:xor"itype" %"rtype"0,%"rtype"0\n" \
432 _ASM_EXTABLE_EX(1b, 3b) \
433 : ltype(x) : "m" (__m(addr)))
435 #define __put_user_nocheck(x, ptr, size) \
439 __put_user_size((x), (ptr), (size), __pu_err, -EFAULT); \
441 __builtin_expect(__pu_err, 0); \
444 #define __get_user_nocheck(x, ptr, size) \
447 __inttype(*(ptr)) __gu_val; \
449 __get_user_size(__gu_val, (ptr), (size), __gu_err, -EFAULT); \
451 (x) = (__force __typeof__(*(ptr)))__gu_val; \
452 __builtin_expect(__gu_err, 0); \
455 /* FIXME: this hack is definitely wrong -AK */
456 struct __large_struct { unsigned long buf[100]; };
457 #define __m(x) (*(struct __large_struct __user *)(x))
460 * Tell gcc we read from memory instead of writing: this is because
461 * we do not write to any memory gcc knows about, so there are no
464 #define __put_user_asm(x, addr, err, itype, rtype, ltype, errret) \
466 "1: mov"itype" %"rtype"1,%2\n" \
468 ".section .fixup,\"ax\"\n" \
472 _ASM_EXTABLE(1b, 3b) \
474 : ltype(x), "m" (__m(addr)), "i" (errret), "0" (err))
476 #define __put_user_asm_ex(x, addr, itype, rtype, ltype) \
477 asm volatile("1: mov"itype" %"rtype"0,%1\n" \
479 _ASM_EXTABLE_EX(1b, 2b) \
480 : : ltype(x), "m" (__m(addr)))
483 * uaccess_try and catch
485 #define uaccess_try do { \
486 current->thread.uaccess_err = 0; \
490 #define uaccess_catch(err) \
492 (err) |= (current->thread.uaccess_err ? -EFAULT : 0); \
496 * __get_user: - Get a simple variable from user space, with less checking.
497 * @x: Variable to store result.
498 * @ptr: Source address, in user space.
500 * Context: User context only. This function may sleep if pagefaults are
503 * This macro copies a single simple variable from user space to kernel
504 * space. It supports simple types like char and int, but not larger
505 * data types like structures or arrays.
507 * @ptr must have pointer-to-simple-variable type, and the result of
508 * dereferencing @ptr must be assignable to @x without a cast.
510 * Caller must check the pointer with access_ok() before calling this
513 * Returns zero on success, or -EFAULT on error.
514 * On error, the variable @x is set to zero.
517 #define __get_user(x, ptr) \
518 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
521 * __put_user: - Write a simple value into user space, with less checking.
522 * @x: Value to copy to user space.
523 * @ptr: Destination address, in user space.
525 * Context: User context only. This function may sleep if pagefaults are
528 * This macro copies a single simple value from kernel space to user
529 * space. It supports simple types like char and int, but not larger
530 * data types like structures or arrays.
532 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
533 * to the result of dereferencing @ptr.
535 * Caller must check the pointer with access_ok() before calling this
538 * Returns zero on success, or -EFAULT on error.
541 #define __put_user(x, ptr) \
542 __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
545 * {get|put}_user_try and catch
549 * } get_user_catch(err)
551 #define get_user_try uaccess_try
552 #define get_user_catch(err) uaccess_catch(err)
554 #define get_user_ex(x, ptr) do { \
555 unsigned long __gue_val; \
556 __get_user_size_ex((__gue_val), (ptr), (sizeof(*(ptr)))); \
557 (x) = (__force __typeof__(*(ptr)))__gue_val; \
560 #define put_user_try uaccess_try
561 #define put_user_catch(err) uaccess_catch(err)
563 #define put_user_ex(x, ptr) \
564 __put_user_size_ex((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
567 copy_from_user_nmi(void *to, const void __user *from, unsigned long n);
568 extern __must_check long
569 strncpy_from_user(char *dst, const char __user *src, long count);
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); \
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>
688 * We rely on the nested NMI work to allow atomic faults from the NMI path; the
689 * nested NMI paths are careful to preserve CR2.
691 * Caller must use pagefault_enable/disable, or run in interrupt context,
692 * and also do a uaccess_ok() check
694 #define __copy_from_user_nmi __copy_from_user_inatomic
697 * The "unsafe" user accesses aren't really "unsafe", but the naming
698 * is a big fat warning: you have to not only do the access_ok()
699 * checking before using them, but you have to surround them with the
700 * user_access_begin/end() pair.
702 #define user_access_begin() __uaccess_begin()
703 #define user_access_end() __uaccess_end()
705 #define unsafe_put_user(x, ptr, err_label) \
708 __typeof__(*(ptr)) __pu_val = (x); \
709 __put_user_size(__pu_val, (ptr), sizeof(*(ptr)), __pu_err, -EFAULT); \
710 if (unlikely(__pu_err)) goto err_label; \
713 #define unsafe_get_user(x, ptr, err_label) \
716 __inttype(*(ptr)) __gu_val; \
717 __get_user_size(__gu_val, (ptr), sizeof(*(ptr)), __gu_err, -EFAULT); \
718 (x) = (__force __typeof__(*(ptr)))__gu_val; \
719 if (unlikely(__gu_err)) goto err_label; \
722 #endif /* _ASM_X86_UACCESS_H */