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)
37 * Test whether a block of memory is a valid user space address.
38 * Returns 0 if the range is valid, nonzero otherwise.
40 * This is equivalent to the following test:
41 * (u33)addr + (u33)size >= (u33)current->addr_limit.seg (u65 for x86_64)
43 * This needs 33-bit (65-bit for x86_64) arithmetic. We have a carry...
46 #define __range_not_ok(addr, size) \
48 unsigned long flag, roksum; \
49 __chk_user_ptr(addr); \
50 asm("add %3,%1 ; sbb %0,%0 ; cmp %1,%4 ; sbb $0,%0" \
51 : "=&r" (flag), "=r" (roksum) \
52 : "1" (addr), "g" ((long)(size)), \
53 "rm" (current_thread_info()->addr_limit.seg)); \
58 * access_ok: - Checks if a user space pointer is valid
59 * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
60 * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
61 * to write to a block, it is always safe to read from it.
62 * @addr: User space pointer to start of block to check
63 * @size: Size of block to check
65 * Context: User context only. This function may sleep.
67 * Checks if a pointer to a block of memory in user space is valid.
69 * Returns true (nonzero) if the memory block may be valid, false (zero)
70 * if it is definitely invalid.
72 * Note that, depending on architecture, this function probably just
73 * checks that the pointer is in the user space range - after calling
74 * this function, memory access functions may still return -EFAULT.
76 #define access_ok(type, addr, size) (likely(__range_not_ok(addr, size) == 0))
79 * The exception table consists of pairs of addresses: the first is the
80 * address of an instruction that is allowed to fault, and the second is
81 * the address at which the program should continue. No registers are
82 * modified, so it is entirely up to the continuation code to figure out
85 * All the routines below use bits of fixup code that are out of line
86 * with the main instruction path. This means when everything is well,
87 * we don't even have to jump over them. Further, they do not intrude
88 * on our cache or tlb entries.
91 struct exception_table_entry {
92 unsigned long insn, fixup;
95 extern int fixup_exception(struct pt_regs *regs);
98 * These are the main single-value transfer routines. They automatically
99 * use the right size if we just have the right pointer type.
101 * This gets kind of ugly. We want to return _two_ values in "get_user()"
102 * and yet we don't want to do any pointers, because that is too much
103 * of a performance impact. Thus we have a few rather ugly macros here,
104 * and hide all the ugliness from the user.
106 * The "__xxx" versions of the user access functions are versions that
107 * do not verify the address space, that must have been done previously
108 * with a separate "access_ok()" call (this is used when we do multiple
109 * accesses to the same area of user memory).
112 extern int __get_user_1(void);
113 extern int __get_user_2(void);
114 extern int __get_user_4(void);
115 extern int __get_user_8(void);
116 extern int __get_user_bad(void);
118 #define __get_user_x(size, ret, x, ptr) \
119 asm volatile("call __get_user_" #size \
120 : "=a" (ret),"=d" (x) \
123 /* Careful: we have to cast the result to the type of the pointer
124 * for sign reasons */
127 * get_user: - Get a simple variable from user space.
128 * @x: Variable to store result.
129 * @ptr: Source address, in user space.
131 * Context: User context only. This function may sleep.
133 * This macro copies a single simple variable from user space to kernel
134 * space. It supports simple types like char and int, but not larger
135 * data types like structures or arrays.
137 * @ptr must have pointer-to-simple-variable type, and the result of
138 * dereferencing @ptr must be assignable to @x without a cast.
140 * Returns zero on success, or -EFAULT on error.
141 * On error, the variable @x is set to zero.
144 #define __get_user_8(__ret_gu, __val_gu, ptr) \
145 __get_user_x(X, __ret_gu, __val_gu, ptr)
147 #define __get_user_8(__ret_gu, __val_gu, ptr) \
148 __get_user_x(8, __ret_gu, __val_gu, ptr)
151 #define get_user(x, ptr) \
154 unsigned long __val_gu; \
155 __chk_user_ptr(ptr); \
156 switch (sizeof(*(ptr))) { \
158 __get_user_x(1, __ret_gu, __val_gu, ptr); \
161 __get_user_x(2, __ret_gu, __val_gu, ptr); \
164 __get_user_x(4, __ret_gu, __val_gu, ptr); \
167 __get_user_8(__ret_gu, __val_gu, ptr); \
170 __get_user_x(X, __ret_gu, __val_gu, ptr); \
173 (x) = (__typeof__(*(ptr)))__val_gu; \
179 # include "uaccess_32.h"
181 # include "uaccess_64.h"