1 #ifndef _ASM_X86_UACCESS_H
2 #define _ASM_X86_UACCESS_H
4 * User space memory access functions
6 #include <linux/compiler.h>
7 #include <linux/kasan-checks.h>
8 #include <linux/string.h>
12 #include <asm/extable.h>
15 * The fs value determines whether argument validity checking should be
16 * performed or not. If get_fs() == USER_DS, checking is performed, with
17 * get_fs() == KERNEL_DS, checking is bypassed.
19 * For historical reasons, these macros are grossly misnamed.
22 #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
24 #define KERNEL_DS MAKE_MM_SEG(-1UL)
25 #define USER_DS MAKE_MM_SEG(TASK_SIZE_MAX)
27 #define get_ds() (KERNEL_DS)
28 #define get_fs() (current->thread.addr_limit)
29 #define set_fs(x) (current->thread.addr_limit = (x))
31 #define segment_eq(a, b) ((a).seg == (b).seg)
33 #define user_addr_max() (current->thread.addr_limit.seg)
34 #define __addr_ok(addr) \
35 ((unsigned long __force)(addr) < user_addr_max())
38 * Test whether a block of memory is a valid user space address.
39 * Returns 0 if the range is valid, nonzero otherwise.
41 static inline bool __chk_range_not_ok(unsigned long addr
, unsigned long size
, unsigned long limit
)
44 * If we have used "sizeof()" for the size,
45 * we know it won't overflow the limit (but
46 * it might overflow the 'addr', so it's
47 * important to subtract the size from the
48 * limit, not add it to the address).
50 if (__builtin_constant_p(size
))
51 return unlikely(addr
> limit
- size
);
53 /* Arbitrary sizes? Be careful about overflow */
55 if (unlikely(addr
< size
))
57 return unlikely(addr
> limit
);
60 #define __range_not_ok(addr, size, limit) \
62 __chk_user_ptr(addr); \
63 __chk_range_not_ok((unsigned long __force)(addr), size, limit); \
66 #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
67 # define WARN_ON_IN_IRQ() WARN_ON_ONCE(!in_task())
69 # define WARN_ON_IN_IRQ()
73 * access_ok: - Checks if a user space pointer is valid
74 * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
75 * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
76 * to write to a block, it is always safe to read from it.
77 * @addr: User space pointer to start of block to check
78 * @size: Size of block to check
80 * Context: User context only. This function may sleep if pagefaults are
83 * Checks if a pointer to a block of memory in user space is valid.
85 * Returns true (nonzero) if the memory block may be valid, false (zero)
86 * if it is definitely invalid.
88 * Note that, depending on architecture, this function probably just
89 * checks that the pointer is in the user space range - after calling
90 * this function, memory access functions may still return -EFAULT.
92 #define access_ok(type, addr, size) \
95 likely(!__range_not_ok(addr, size, user_addr_max())); \
99 * These are the main single-value transfer routines. They automatically
100 * use the right size if we just have the right pointer type.
102 * This gets kind of ugly. We want to return _two_ values in "get_user()"
103 * and yet we don't want to do any pointers, because that is too much
104 * of a performance impact. Thus we have a few rather ugly macros here,
105 * and hide all the ugliness from the user.
107 * The "__xxx" versions of the user access functions are versions that
108 * do not verify the address space, that must have been done previously
109 * with a separate "access_ok()" call (this is used when we do multiple
110 * accesses to the same area of user memory).
113 extern int __get_user_1(void);
114 extern int __get_user_2(void);
115 extern int __get_user_4(void);
116 extern int __get_user_8(void);
117 extern int __get_user_bad(void);
119 #define __uaccess_begin() stac()
120 #define __uaccess_end() clac()
123 * This is a type: either unsigned long, if the argument fits into
124 * that type, or otherwise unsigned long long.
126 #define __inttype(x) \
127 __typeof__(__builtin_choose_expr(sizeof(x) > sizeof(0UL), 0ULL, 0UL))
130 * get_user: - Get a simple variable from user space.
131 * @x: Variable to store result.
132 * @ptr: Source address, in user space.
134 * Context: User context only. This function may sleep if pagefaults are
137 * This macro copies a single simple variable from user space to kernel
138 * space. It supports simple types like char and int, but not larger
139 * data types like structures or arrays.
141 * @ptr must have pointer-to-simple-variable type, and the result of
142 * dereferencing @ptr must be assignable to @x without a cast.
144 * Returns zero on success, or -EFAULT on error.
145 * On error, the variable @x is set to zero.
148 * Careful: we have to cast the result to the type of the pointer
151 * The use of _ASM_DX as the register specifier is a bit of a
152 * simplification, as gcc only cares about it as the starting point
153 * and not size: for a 64-bit value it will use %ecx:%edx on 32 bits
154 * (%ecx being the next register in gcc's x86 register sequence), and
157 * Clang/LLVM cares about the size of the register, but still wants
158 * the base register for something that ends up being a pair.
160 #define get_user(x, ptr) \
163 register __inttype(*(ptr)) __val_gu asm("%"_ASM_DX); \
164 register void *__sp asm(_ASM_SP); \
165 __chk_user_ptr(ptr); \
167 asm volatile("call __get_user_%P4" \
168 : "=a" (__ret_gu), "=r" (__val_gu), "+r" (__sp) \
169 : "0" (ptr), "i" (sizeof(*(ptr)))); \
170 (x) = (__force __typeof__(*(ptr))) __val_gu; \
171 __builtin_expect(__ret_gu, 0); \
174 #define __put_user_x(size, x, ptr, __ret_pu) \
175 asm volatile("call __put_user_" #size : "=a" (__ret_pu) \
176 : "0" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx")
181 #define __put_user_asm_u64(x, addr, err, errret) \
183 "1: movl %%eax,0(%2)\n" \
184 "2: movl %%edx,4(%2)\n" \
186 ".section .fixup,\"ax\"\n" \
190 _ASM_EXTABLE(1b, 4b) \
191 _ASM_EXTABLE(2b, 4b) \
193 : "A" (x), "r" (addr), "i" (errret), "0" (err))
195 #define __put_user_asm_ex_u64(x, addr) \
197 "1: movl %%eax,0(%1)\n" \
198 "2: movl %%edx,4(%1)\n" \
200 _ASM_EXTABLE_EX(1b, 2b) \
201 _ASM_EXTABLE_EX(2b, 3b) \
202 : : "A" (x), "r" (addr))
204 #define __put_user_x8(x, ptr, __ret_pu) \
205 asm volatile("call __put_user_8" : "=a" (__ret_pu) \
206 : "A" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx")
208 #define __put_user_asm_u64(x, ptr, retval, errret) \
209 __put_user_asm(x, ptr, retval, "q", "", "er", errret)
210 #define __put_user_asm_ex_u64(x, addr) \
211 __put_user_asm_ex(x, addr, "q", "", "er")
212 #define __put_user_x8(x, ptr, __ret_pu) __put_user_x(8, x, ptr, __ret_pu)
215 extern void __put_user_bad(void);
218 * Strange magic calling convention: pointer in %ecx,
219 * value in %eax(:%edx), return value in %eax. clobbers %rbx
221 extern void __put_user_1(void);
222 extern void __put_user_2(void);
223 extern void __put_user_4(void);
224 extern void __put_user_8(void);
227 * put_user: - Write a simple value into user space.
228 * @x: Value to copy to user space.
229 * @ptr: Destination address, in user space.
231 * Context: User context only. This function may sleep if pagefaults are
234 * This macro copies a single simple value from kernel space to user
235 * space. It supports simple types like char and int, but not larger
236 * data types like structures or arrays.
238 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
239 * to the result of dereferencing @ptr.
241 * Returns zero on success, or -EFAULT on error.
243 #define put_user(x, ptr) \
246 __typeof__(*(ptr)) __pu_val; \
247 __chk_user_ptr(ptr); \
250 switch (sizeof(*(ptr))) { \
252 __put_user_x(1, __pu_val, ptr, __ret_pu); \
255 __put_user_x(2, __pu_val, ptr, __ret_pu); \
258 __put_user_x(4, __pu_val, ptr, __ret_pu); \
261 __put_user_x8(__pu_val, ptr, __ret_pu); \
264 __put_user_x(X, __pu_val, ptr, __ret_pu); \
267 __builtin_expect(__ret_pu, 0); \
270 #define __put_user_size(x, ptr, size, retval, errret) \
273 __chk_user_ptr(ptr); \
276 __put_user_asm(x, ptr, retval, "b", "b", "iq", errret); \
279 __put_user_asm(x, ptr, retval, "w", "w", "ir", errret); \
282 __put_user_asm(x, ptr, retval, "l", "k", "ir", errret); \
285 __put_user_asm_u64((__typeof__(*ptr))(x), ptr, retval, \
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(1b, 4b) \
333 _ASM_EXTABLE(2b, 4b) \
334 : "=r" (retval), "=&A"(x) \
335 : "m" (__m(__ptr)), "m" __m(((u32 *)(__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(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(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) \
433 __put_user_size((x), (ptr), (size), __pu_err, -EFAULT); \
435 __builtin_expect(__pu_err, 0); \
438 #define __get_user_nocheck(x, ptr, size) \
441 __inttype(*(ptr)) __gu_val; \
443 __get_user_size(__gu_val, (ptr), (size), __gu_err, -EFAULT); \
445 (x) = (__force __typeof__(*(ptr)))__gu_val; \
446 __builtin_expect(__gu_err, 0); \
449 /* FIXME: this hack is definitely wrong -AK */
450 struct __large_struct
{ unsigned long buf
[100]; };
451 #define __m(x) (*(struct __large_struct __user *)(x))
454 * Tell gcc we read from memory instead of writing: this is because
455 * we do not write to any memory gcc knows about, so there are no
458 #define __put_user_asm(x, addr, err, itype, rtype, ltype, errret) \
460 "1: mov"itype" %"rtype"1,%2\n" \
462 ".section .fixup,\"ax\"\n" \
466 _ASM_EXTABLE(1b, 3b) \
468 : ltype(x), "m" (__m(addr)), "i" (errret), "0" (err))
470 #define __put_user_asm_ex(x, addr, itype, rtype, ltype) \
471 asm volatile("1: mov"itype" %"rtype"0,%1\n" \
473 _ASM_EXTABLE_EX(1b, 2b) \
474 : : ltype(x), "m" (__m(addr)))
477 * uaccess_try and catch
479 #define uaccess_try do { \
480 current->thread.uaccess_err = 0; \
484 #define uaccess_catch(err) \
486 (err) |= (current->thread.uaccess_err ? -EFAULT : 0); \
490 * __get_user: - Get a simple variable from user space, with less checking.
491 * @x: Variable to store result.
492 * @ptr: Source address, in user space.
494 * Context: User context only. This function may sleep if pagefaults are
497 * This macro copies a single simple variable from user space to kernel
498 * space. It supports simple types like char and int, but not larger
499 * data types like structures or arrays.
501 * @ptr must have pointer-to-simple-variable type, and the result of
502 * dereferencing @ptr must be assignable to @x without a cast.
504 * Caller must check the pointer with access_ok() before calling this
507 * Returns zero on success, or -EFAULT on error.
508 * On error, the variable @x is set to zero.
511 #define __get_user(x, ptr) \
512 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
515 * __put_user: - Write a simple value into user space, with less checking.
516 * @x: Value to copy to user space.
517 * @ptr: Destination address, in user space.
519 * Context: User context only. This function may sleep if pagefaults are
522 * This macro copies a single simple value from kernel space to user
523 * space. It supports simple types like char and int, but not larger
524 * data types like structures or arrays.
526 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
527 * to the result of dereferencing @ptr.
529 * Caller must check the pointer with access_ok() before calling this
532 * Returns zero on success, or -EFAULT on error.
535 #define __put_user(x, ptr) \
536 __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
539 * {get|put}_user_try and catch
543 * } get_user_catch(err)
545 #define get_user_try uaccess_try
546 #define get_user_catch(err) uaccess_catch(err)
548 #define get_user_ex(x, ptr) do { \
549 unsigned long __gue_val; \
550 __get_user_size_ex((__gue_val), (ptr), (sizeof(*(ptr)))); \
551 (x) = (__force __typeof__(*(ptr)))__gue_val; \
554 #define put_user_try uaccess_try
555 #define put_user_catch(err) uaccess_catch(err)
557 #define put_user_ex(x, ptr) \
558 __put_user_size_ex((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
561 copy_from_user_nmi(void *to
, const void __user
*from
, unsigned long n
);
562 extern __must_check
long
563 strncpy_from_user(char *dst
, const char __user
*src
, long count
);
565 extern __must_check
long strnlen_user(const char __user
*str
, long n
);
567 unsigned long __must_check
clear_user(void __user
*mem
, unsigned long len
);
568 unsigned long __must_check
__clear_user(void __user
*mem
, unsigned long len
);
570 extern void __cmpxchg_wrong_size(void)
571 __compiletime_error("Bad argument size for cmpxchg");
573 #define __user_atomic_cmpxchg_inatomic(uval, ptr, old, new, size) \
576 __typeof__(ptr) __uval = (uval); \
577 __typeof__(*(ptr)) __old = (old); \
578 __typeof__(*(ptr)) __new = (new); \
584 "1:\t" LOCK_PREFIX "cmpxchgb %4, %2\n" \
586 "\t.section .fixup, \"ax\"\n" \
590 _ASM_EXTABLE(1b, 3b) \
591 : "+r" (__ret), "=a" (__old), "+m" (*(ptr)) \
592 : "i" (-EFAULT), "q" (__new), "1" (__old) \
600 "1:\t" LOCK_PREFIX "cmpxchgw %4, %2\n" \
602 "\t.section .fixup, \"ax\"\n" \
606 _ASM_EXTABLE(1b, 3b) \
607 : "+r" (__ret), "=a" (__old), "+m" (*(ptr)) \
608 : "i" (-EFAULT), "r" (__new), "1" (__old) \
616 "1:\t" LOCK_PREFIX "cmpxchgl %4, %2\n" \
618 "\t.section .fixup, \"ax\"\n" \
622 _ASM_EXTABLE(1b, 3b) \
623 : "+r" (__ret), "=a" (__old), "+m" (*(ptr)) \
624 : "i" (-EFAULT), "r" (__new), "1" (__old) \
631 if (!IS_ENABLED(CONFIG_X86_64)) \
632 __cmpxchg_wrong_size(); \
635 "1:\t" LOCK_PREFIX "cmpxchgq %4, %2\n" \
637 "\t.section .fixup, \"ax\"\n" \
641 _ASM_EXTABLE(1b, 3b) \
642 : "+r" (__ret), "=a" (__old), "+m" (*(ptr)) \
643 : "i" (-EFAULT), "r" (__new), "1" (__old) \
649 __cmpxchg_wrong_size(); \
656 #define user_atomic_cmpxchg_inatomic(uval, ptr, old, new) \
658 access_ok(VERIFY_WRITE, (ptr), sizeof(*(ptr))) ? \
659 __user_atomic_cmpxchg_inatomic((uval), (ptr), \
660 (old), (new), sizeof(*(ptr))) : \
665 * movsl can be slow when source and dest are not both 8-byte aligned
667 #ifdef CONFIG_X86_INTEL_USERCOPY
668 extern struct movsl_mask
{
670 } ____cacheline_aligned_in_smp movsl_mask
;
673 #define ARCH_HAS_NOCACHE_UACCESS 1
676 # include <asm/uaccess_32.h>
678 # include <asm/uaccess_64.h>
682 * We rely on the nested NMI work to allow atomic faults from the NMI path; the
683 * nested NMI paths are careful to preserve CR2.
685 * Caller must use pagefault_enable/disable, or run in interrupt context,
686 * and also do a uaccess_ok() check
688 #define __copy_from_user_nmi __copy_from_user_inatomic
691 * The "unsafe" user accesses aren't really "unsafe", but the naming
692 * is a big fat warning: you have to not only do the access_ok()
693 * checking before using them, but you have to surround them with the
694 * user_access_begin/end() pair.
696 #define user_access_begin() __uaccess_begin()
697 #define user_access_end() __uaccess_end()
699 #define unsafe_put_user(x, ptr, err_label) \
702 __typeof__(*(ptr)) __pu_val = (x); \
703 __put_user_size(__pu_val, (ptr), sizeof(*(ptr)), __pu_err, -EFAULT); \
704 if (unlikely(__pu_err)) goto err_label; \
707 #define unsafe_get_user(x, ptr, err_label) \
710 __inttype(*(ptr)) __gu_val; \
711 __get_user_size(__gu_val, (ptr), sizeof(*(ptr)), __gu_err, -EFAULT); \
712 (x) = (__force __typeof__(*(ptr)))__gu_val; \
713 if (unlikely(__gu_err)) goto err_label; \
716 #endif /* _ASM_X86_UACCESS_H */