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/prefetch.h>
10 #include <linux/string.h>
15 #define VERIFY_WRITE 1
18 * The fs value determines whether argument validity checking should be
19 * performed or not. If get_fs() == USER_DS, checking is performed, with
20 * get_fs() == KERNEL_DS, checking is bypassed.
22 * For historical reasons, these macros are grossly misnamed.
25 #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
27 #define KERNEL_DS MAKE_MM_SEG(-1UL)
28 #define USER_DS MAKE_MM_SEG(PAGE_OFFSET)
30 #define get_ds() (KERNEL_DS)
31 #define get_fs() (current_thread_info()->addr_limit)
32 #define set_fs(x) (current_thread_info()->addr_limit = (x))
34 #define segment_eq(a, b) ((a).seg == (b).seg)
36 #define __addr_ok(addr) \
37 ((unsigned long __force)(addr) < \
38 (current_thread_info()->addr_limit.seg))
41 * Test whether a block of memory is a valid user space address.
42 * Returns 0 if the range is valid, nonzero otherwise.
44 * This is equivalent to the following test:
45 * (u33)addr + (u33)size >= (u33)current->addr_limit.seg (u65 for x86_64)
47 * This needs 33-bit (65-bit for x86_64) arithmetic. We have a carry...
50 #define __range_not_ok(addr, size) \
52 unsigned long flag, roksum; \
53 __chk_user_ptr(addr); \
54 asm("add %3,%1 ; sbb %0,%0 ; cmp %1,%4 ; sbb $0,%0" \
55 : "=&r" (flag), "=r" (roksum) \
56 : "1" (addr), "g" ((long)(size)), \
57 "rm" (current_thread_info()->addr_limit.seg)); \
62 * access_ok: - Checks if a user space pointer is valid
63 * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
64 * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
65 * to write to a block, it is always safe to read from it.
66 * @addr: User space pointer to start of block to check
67 * @size: Size of block to check
69 * Context: User context only. This function may sleep.
71 * Checks if a pointer to a block of memory in user space is valid.
73 * Returns true (nonzero) if the memory block may be valid, false (zero)
74 * if it is definitely invalid.
76 * Note that, depending on architecture, this function probably just
77 * checks that the pointer is in the user space range - after calling
78 * this function, memory access functions may still return -EFAULT.
80 #define access_ok(type, addr, size) (likely(__range_not_ok(addr, size) == 0))
83 * The exception table consists of pairs of addresses: the first is the
84 * address of an instruction that is allowed to fault, and the second is
85 * the address at which the program should continue. No registers are
86 * modified, so it is entirely up to the continuation code to figure out
89 * All the routines below use bits of fixup code that are out of line
90 * with the main instruction path. This means when everything is well,
91 * we don't even have to jump over them. Further, they do not intrude
92 * on our cache or tlb entries.
95 struct exception_table_entry {
96 unsigned long insn, fixup;
99 extern int fixup_exception(struct pt_regs *regs);
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 __get_user_x(size, ret, x, ptr) \
123 asm volatile("call __get_user_" #size \
124 : "=a" (ret),"=d" (x) \
127 /* Careful: we have to cast the result to the type of the pointer
128 * for sign reasons */
131 * get_user: - Get a simple variable from user space.
132 * @x: Variable to store result.
133 * @ptr: Source address, in user space.
135 * Context: User context only. This function may sleep.
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 #define __get_user_8(__ret_gu, __val_gu, ptr) \
149 __get_user_x(X, __ret_gu, __val_gu, ptr)
151 #define __get_user_8(__ret_gu, __val_gu, ptr) \
152 __get_user_x(8, __ret_gu, __val_gu, ptr)
155 #define get_user(x, ptr) \
158 unsigned long __val_gu; \
159 __chk_user_ptr(ptr); \
161 switch (sizeof(*(ptr))) { \
163 __get_user_x(1, __ret_gu, __val_gu, ptr); \
166 __get_user_x(2, __ret_gu, __val_gu, ptr); \
169 __get_user_x(4, __ret_gu, __val_gu, ptr); \
172 __get_user_8(__ret_gu, __val_gu, ptr); \
175 __get_user_x(X, __ret_gu, __val_gu, ptr); \
178 (x) = (__typeof__(*(ptr)))__val_gu; \
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_u64(x, addr, err) \
190 asm volatile("1: movl %%eax,0(%2)\n" \
191 "2: movl %%edx,4(%2)\n" \
193 ".section .fixup,\"ax\"\n" \
197 _ASM_EXTABLE(1b, 4b) \
198 _ASM_EXTABLE(2b, 4b) \
200 : "A" (x), "r" (addr), "i" (-EFAULT), "0" (err))
202 #define __put_user_x8(x, ptr, __ret_pu) \
203 asm volatile("call __put_user_8" : "=a" (__ret_pu) \
204 : "A" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx")
206 #define __put_user_u64(x, ptr, retval) \
207 __put_user_asm(x, ptr, retval, "q", "", "Zr", -EFAULT)
208 #define __put_user_x8(x, ptr, __ret_pu) __put_user_x(8, x, ptr, __ret_pu)
211 extern void __put_user_bad(void);
214 * Strange magic calling convention: pointer in %ecx,
215 * value in %eax(:%edx), return value in %eax. clobbers %rbx
217 extern void __put_user_1(void);
218 extern void __put_user_2(void);
219 extern void __put_user_4(void);
220 extern void __put_user_8(void);
222 #ifdef CONFIG_X86_WP_WORKS_OK
225 * put_user: - Write a simple value into user space.
226 * @x: Value to copy to user space.
227 * @ptr: Destination address, in user space.
229 * Context: User context only. This function may sleep.
231 * This macro copies a single simple value from kernel space to user
232 * space. It supports simple types like char and int, but not larger
233 * data types like structures or arrays.
235 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
236 * to the result of dereferencing @ptr.
238 * Returns zero on success, or -EFAULT on error.
240 #define put_user(x, ptr) \
243 __typeof__(*(ptr)) __pu_val; \
244 __chk_user_ptr(ptr); \
247 switch (sizeof(*(ptr))) { \
249 __put_user_x(1, __pu_val, ptr, __ret_pu); \
252 __put_user_x(2, __pu_val, ptr, __ret_pu); \
255 __put_user_x(4, __pu_val, ptr, __ret_pu); \
258 __put_user_x8(__pu_val, ptr, __ret_pu); \
261 __put_user_x(X, __pu_val, ptr, __ret_pu); \
267 #define __put_user_size(x, ptr, size, retval, errret) \
270 __chk_user_ptr(ptr); \
273 __put_user_asm(x, ptr, retval, "b", "b", "iq", errret); \
276 __put_user_asm(x, ptr, retval, "w", "w", "ir", errret); \
279 __put_user_asm(x, ptr, retval, "l", "k", "ir", errret);\
282 __put_user_u64((__typeof__(*ptr))(x), ptr, retval); \
291 #define __put_user_size(x, ptr, size, retval, errret) \
293 __typeof__(*(ptr))__pus_tmp = x; \
296 if (unlikely(__copy_to_user_ll(ptr, &__pus_tmp, size) != 0)) \
300 #define put_user(x, ptr) \
303 __typeof__(*(ptr))__pus_tmp = x; \
305 if (unlikely(__copy_to_user_ll(ptr, &__pus_tmp, \
306 sizeof(*(ptr))) != 0)) \
307 __ret_pu = -EFAULT; \
313 #define __get_user_asm_u64(x, ptr, retval, errret) (x) = __get_user_bad()
315 #define __get_user_asm_u64(x, ptr, retval, errret) \
316 __get_user_asm(x, ptr, retval, "q", "", "=r", errret)
319 #define __get_user_size(x, ptr, size, retval, errret) \
322 __chk_user_ptr(ptr); \
325 __get_user_asm(x, ptr, retval, "b", "b", "=q", errret); \
328 __get_user_asm(x, ptr, retval, "w", "w", "=r", errret); \
331 __get_user_asm(x, ptr, retval, "l", "k", "=r", errret); \
334 __get_user_asm_u64(x, ptr, retval, errret); \
337 (x) = __get_user_bad(); \
341 #define __get_user_asm(x, addr, err, itype, rtype, ltype, errret) \
342 asm volatile("1: mov"itype" %2,%"rtype"1\n" \
344 ".section .fixup,\"ax\"\n" \
346 " xor"itype" %"rtype"1,%"rtype"1\n" \
349 _ASM_EXTABLE(1b, 3b) \
350 : "=r" (err), ltype(x) \
351 : "m" (__m(addr)), "i" (errret), "0" (err))
353 #define __put_user_nocheck(x, ptr, size) \
356 __put_user_size((x), (ptr), (size), __pu_err, -EFAULT); \
360 #define __get_user_nocheck(x, ptr, size) \
363 unsigned long __gu_val; \
364 __get_user_size(__gu_val, (ptr), (size), __gu_err, -EFAULT); \
365 (x) = (__force __typeof__(*(ptr)))__gu_val; \
369 /* FIXME: this hack is definitely wrong -AK */
370 struct __large_struct { unsigned long buf[100]; };
371 #define __m(x) (*(struct __large_struct __user *)(x))
374 * Tell gcc we read from memory instead of writing: this is because
375 * we do not write to any memory gcc knows about, so there are no
378 #define __put_user_asm(x, addr, err, itype, rtype, ltype, errret) \
379 asm volatile("1: mov"itype" %"rtype"1,%2\n" \
381 ".section .fixup,\"ax\"\n" \
385 _ASM_EXTABLE(1b, 3b) \
387 : ltype(x), "m" (__m(addr)), "i" (errret), "0" (err))
389 * __get_user: - Get a simple variable from user space, with less checking.
390 * @x: Variable to store result.
391 * @ptr: Source address, in user space.
393 * Context: User context only. This function may sleep.
395 * This macro copies a single simple variable from user space to kernel
396 * space. It supports simple types like char and int, but not larger
397 * data types like structures or arrays.
399 * @ptr must have pointer-to-simple-variable type, and the result of
400 * dereferencing @ptr must be assignable to @x without a cast.
402 * Caller must check the pointer with access_ok() before calling this
405 * Returns zero on success, or -EFAULT on error.
406 * On error, the variable @x is set to zero.
409 #define __get_user(x, ptr) \
410 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
412 * __put_user: - Write a simple value into user space, with less checking.
413 * @x: Value to copy to user space.
414 * @ptr: Destination address, in user space.
416 * Context: User context only. This function may sleep.
418 * This macro copies a single simple value from kernel space to user
419 * space. It supports simple types like char and int, but not larger
420 * data types like structures or arrays.
422 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
423 * to the result of dereferencing @ptr.
425 * Caller must check the pointer with access_ok() before calling this
428 * Returns zero on success, or -EFAULT on error.
431 #define __put_user(x, ptr) \
432 __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
434 #define __get_user_unaligned __get_user
435 #define __put_user_unaligned __put_user
438 * movsl can be slow when source and dest are not both 8-byte aligned
440 #ifdef CONFIG_X86_INTEL_USERCOPY
441 extern struct movsl_mask {
443 } ____cacheline_aligned_in_smp movsl_mask;
446 #define ARCH_HAS_NOCACHE_UACCESS 1
449 # include "uaccess_32.h"
451 # define ARCH_HAS_SEARCH_EXTABLE
452 # include "uaccess_64.h"
455 #endif /* _ASM_X86_UACCESS_H */