1 | |
---|
2 | #ifndef __X86_UACCESS_H__ |
---|
3 | #define __X86_UACCESS_H__ |
---|
4 | |
---|
5 | #include <xen/config.h> |
---|
6 | #include <xen/compiler.h> |
---|
7 | #include <xen/errno.h> |
---|
8 | #include <xen/prefetch.h> |
---|
9 | #include <asm/page.h> |
---|
10 | |
---|
11 | #ifdef __x86_64__ |
---|
12 | #include <asm/x86_64/uaccess.h> |
---|
13 | #else |
---|
14 | #include <asm/x86_32/uaccess.h> |
---|
15 | #endif |
---|
16 | |
---|
17 | unsigned long copy_to_user(void *to, const void *from, unsigned len); |
---|
18 | unsigned long copy_from_user(void *to, const void *from, unsigned len); |
---|
19 | /* Handles exceptions in both to and from, but doesn't do access_ok */ |
---|
20 | unsigned long __copy_to_user_ll(void *to, const void *from, unsigned n); |
---|
21 | unsigned long __copy_from_user_ll(void *to, const void *from, unsigned n); |
---|
22 | |
---|
23 | extern long __get_user_bad(void); |
---|
24 | extern void __put_user_bad(void); |
---|
25 | |
---|
26 | /** |
---|
27 | * get_user: - Get a simple variable from user space. |
---|
28 | * @x: Variable to store result. |
---|
29 | * @ptr: Source address, in user space. |
---|
30 | * |
---|
31 | * Context: User context only. This function may sleep. |
---|
32 | * |
---|
33 | * This macro copies a single simple variable from user space to kernel |
---|
34 | * space. It supports simple types like char and int, but not larger |
---|
35 | * data types like structures or arrays. |
---|
36 | * |
---|
37 | * @ptr must have pointer-to-simple-variable type, and the result of |
---|
38 | * dereferencing @ptr must be assignable to @x without a cast. |
---|
39 | * |
---|
40 | * Returns zero on success, or -EFAULT on error. |
---|
41 | * On error, the variable @x is set to zero. |
---|
42 | */ |
---|
43 | #define get_user(x,ptr) \ |
---|
44 | __get_user_check((x),(ptr),sizeof(*(ptr))) |
---|
45 | |
---|
46 | /** |
---|
47 | * put_user: - Write a simple value into user space. |
---|
48 | * @x: Value to copy to user space. |
---|
49 | * @ptr: Destination address, in user space. |
---|
50 | * |
---|
51 | * Context: User context only. This function may sleep. |
---|
52 | * |
---|
53 | * This macro copies a single simple value from kernel space to user |
---|
54 | * space. It supports simple types like char and int, but not larger |
---|
55 | * data types like structures or arrays. |
---|
56 | * |
---|
57 | * @ptr must have pointer-to-simple-variable type, and @x must be assignable |
---|
58 | * to the result of dereferencing @ptr. |
---|
59 | * |
---|
60 | * Returns zero on success, or -EFAULT on error. |
---|
61 | */ |
---|
62 | #define put_user(x,ptr) \ |
---|
63 | __put_user_check((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr))) |
---|
64 | |
---|
65 | /** |
---|
66 | * __get_user: - Get a simple variable from user space, with less checking. |
---|
67 | * @x: Variable to store result. |
---|
68 | * @ptr: Source address, in user space. |
---|
69 | * |
---|
70 | * Context: User context only. This function may sleep. |
---|
71 | * |
---|
72 | * This macro copies a single simple variable from user space to kernel |
---|
73 | * space. It supports simple types like char and int, but not larger |
---|
74 | * data types like structures or arrays. |
---|
75 | * |
---|
76 | * @ptr must have pointer-to-simple-variable type, and the result of |
---|
77 | * dereferencing @ptr must be assignable to @x without a cast. |
---|
78 | * |
---|
79 | * Caller must check the pointer with access_ok() before calling this |
---|
80 | * function. |
---|
81 | * |
---|
82 | * Returns zero on success, or -EFAULT on error. |
---|
83 | * On error, the variable @x is set to zero. |
---|
84 | */ |
---|
85 | #define __get_user(x,ptr) \ |
---|
86 | __get_user_nocheck((x),(ptr),sizeof(*(ptr))) |
---|
87 | |
---|
88 | /** |
---|
89 | * __put_user: - Write a simple value into user space, with less checking. |
---|
90 | * @x: Value to copy to user space. |
---|
91 | * @ptr: Destination address, in user space. |
---|
92 | * |
---|
93 | * Context: User context only. This function may sleep. |
---|
94 | * |
---|
95 | * This macro copies a single simple value from kernel space to user |
---|
96 | * space. It supports simple types like char and int, but not larger |
---|
97 | * data types like structures or arrays. |
---|
98 | * |
---|
99 | * @ptr must have pointer-to-simple-variable type, and @x must be assignable |
---|
100 | * to the result of dereferencing @ptr. |
---|
101 | * |
---|
102 | * Caller must check the pointer with access_ok() before calling this |
---|
103 | * function. |
---|
104 | * |
---|
105 | * Returns zero on success, or -EFAULT on error. |
---|
106 | */ |
---|
107 | #define __put_user(x,ptr) \ |
---|
108 | __put_user_nocheck((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr))) |
---|
109 | |
---|
110 | #define __put_user_nocheck(x,ptr,size) \ |
---|
111 | ({ \ |
---|
112 | long __pu_err; \ |
---|
113 | __put_user_size((x),(ptr),(size),__pu_err,-EFAULT); \ |
---|
114 | __pu_err; \ |
---|
115 | }) |
---|
116 | |
---|
117 | #define __put_user_check(x,ptr,size) \ |
---|
118 | ({ \ |
---|
119 | long __pu_err = -EFAULT; \ |
---|
120 | __typeof__(*(ptr)) __user *__pu_addr = (ptr); \ |
---|
121 | if (__addr_ok(__pu_addr)) \ |
---|
122 | __put_user_size((x),__pu_addr,(size),__pu_err,-EFAULT); \ |
---|
123 | __pu_err; \ |
---|
124 | }) |
---|
125 | |
---|
126 | #define __get_user_nocheck(x,ptr,size) \ |
---|
127 | ({ \ |
---|
128 | long __gu_err; \ |
---|
129 | __get_user_size((x),(ptr),(size),__gu_err,-EFAULT); \ |
---|
130 | __gu_err; \ |
---|
131 | }) |
---|
132 | |
---|
133 | #define __get_user_check(x,ptr,size) \ |
---|
134 | ({ \ |
---|
135 | long __gu_err; \ |
---|
136 | __typeof__(*(ptr)) __user *__gu_addr = (ptr); \ |
---|
137 | __get_user_size((x),__gu_addr,(size),__gu_err,-EFAULT); \ |
---|
138 | if (!__addr_ok(__gu_addr)) __gu_err = -EFAULT; \ |
---|
139 | __gu_err; \ |
---|
140 | }) |
---|
141 | |
---|
142 | struct __large_struct { unsigned long buf[100]; }; |
---|
143 | #define __m(x) (*(const struct __large_struct *)(x)) |
---|
144 | |
---|
145 | /* |
---|
146 | * Tell gcc we read from memory instead of writing: this is because |
---|
147 | * we do not write to any memory gcc knows about, so there are no |
---|
148 | * aliasing issues. |
---|
149 | */ |
---|
150 | #define __put_user_asm(x, addr, err, itype, rtype, ltype, errret) \ |
---|
151 | __asm__ __volatile__( \ |
---|
152 | "1: mov"itype" %"rtype"1,%2\n" \ |
---|
153 | "2:\n" \ |
---|
154 | ".section .fixup,\"ax\"\n" \ |
---|
155 | "3: mov %3,%0\n" \ |
---|
156 | " jmp 2b\n" \ |
---|
157 | ".previous\n" \ |
---|
158 | ".section __ex_table,\"a\"\n" \ |
---|
159 | " "__FIXUP_ALIGN"\n" \ |
---|
160 | " "__FIXUP_WORD" 1b,3b\n" \ |
---|
161 | ".previous" \ |
---|
162 | : "=r"(err) \ |
---|
163 | : ltype (x), "m"(__m(addr)), "i"(errret), "0"(err)) |
---|
164 | |
---|
165 | #define __get_user_asm(x, addr, err, itype, rtype, ltype, errret) \ |
---|
166 | __asm__ __volatile__( \ |
---|
167 | "1: mov"itype" %2,%"rtype"1\n" \ |
---|
168 | "2:\n" \ |
---|
169 | ".section .fixup,\"ax\"\n" \ |
---|
170 | "3: mov %3,%0\n" \ |
---|
171 | " xor"itype" %"rtype"1,%"rtype"1\n" \ |
---|
172 | " jmp 2b\n" \ |
---|
173 | ".previous\n" \ |
---|
174 | ".section __ex_table,\"a\"\n" \ |
---|
175 | " "__FIXUP_ALIGN"\n" \ |
---|
176 | " "__FIXUP_WORD" 1b,3b\n" \ |
---|
177 | ".previous" \ |
---|
178 | : "=r"(err), ltype (x) \ |
---|
179 | : "m"(__m(addr)), "i"(errret), "0"(err)) |
---|
180 | |
---|
181 | /** |
---|
182 | * __copy_to_user: - Copy a block of data into user space, with less checking |
---|
183 | * @to: Destination address, in user space. |
---|
184 | * @from: Source address, in kernel space. |
---|
185 | * @n: Number of bytes to copy. |
---|
186 | * |
---|
187 | * Context: User context only. This function may sleep. |
---|
188 | * |
---|
189 | * Copy data from kernel space to user space. Caller must check |
---|
190 | * the specified block with access_ok() before calling this function. |
---|
191 | * |
---|
192 | * Returns number of bytes that could not be copied. |
---|
193 | * On success, this will be zero. |
---|
194 | */ |
---|
195 | static always_inline unsigned long |
---|
196 | __copy_to_user(void __user *to, const void *from, unsigned long n) |
---|
197 | { |
---|
198 | if (__builtin_constant_p(n)) { |
---|
199 | unsigned long ret; |
---|
200 | |
---|
201 | switch (n) { |
---|
202 | case 1: |
---|
203 | __put_user_size(*(const u8 *)from, (u8 __user *)to, 1, ret, 1); |
---|
204 | return ret; |
---|
205 | case 2: |
---|
206 | __put_user_size(*(const u16 *)from, (u16 __user *)to, 2, ret, 2); |
---|
207 | return ret; |
---|
208 | case 4: |
---|
209 | __put_user_size(*(const u32 *)from, (u32 __user *)to, 4, ret, 4); |
---|
210 | return ret; |
---|
211 | case 8: |
---|
212 | __put_user_size(*(const u64 *)from, (u64 __user *)to, 8, ret, 8); |
---|
213 | return ret; |
---|
214 | } |
---|
215 | } |
---|
216 | return __copy_to_user_ll(to, from, n); |
---|
217 | } |
---|
218 | |
---|
219 | /** |
---|
220 | * __copy_from_user: - Copy a block of data from user space, with less checking |
---|
221 | * @to: Destination address, in kernel space. |
---|
222 | * @from: Source address, in user space. |
---|
223 | * @n: Number of bytes to copy. |
---|
224 | * |
---|
225 | * Context: User context only. This function may sleep. |
---|
226 | * |
---|
227 | * Copy data from user space to kernel space. Caller must check |
---|
228 | * the specified block with access_ok() before calling this function. |
---|
229 | * |
---|
230 | * Returns number of bytes that could not be copied. |
---|
231 | * On success, this will be zero. |
---|
232 | * |
---|
233 | * If some data could not be copied, this function will pad the copied |
---|
234 | * data to the requested size using zero bytes. |
---|
235 | */ |
---|
236 | static always_inline unsigned long |
---|
237 | __copy_from_user(void *to, const void __user *from, unsigned long n) |
---|
238 | { |
---|
239 | if (__builtin_constant_p(n)) { |
---|
240 | unsigned long ret; |
---|
241 | |
---|
242 | switch (n) { |
---|
243 | case 1: |
---|
244 | __get_user_size(*(u8 *)to, from, 1, ret, 1); |
---|
245 | return ret; |
---|
246 | case 2: |
---|
247 | __get_user_size(*(u16 *)to, from, 2, ret, 2); |
---|
248 | return ret; |
---|
249 | case 4: |
---|
250 | __get_user_size(*(u32 *)to, from, 4, ret, 4); |
---|
251 | return ret; |
---|
252 | case 8: |
---|
253 | __get_user_size(*(u64*)to, from, 8, ret, 8); |
---|
254 | return ret; |
---|
255 | } |
---|
256 | } |
---|
257 | return __copy_from_user_ll(to, from, n); |
---|
258 | } |
---|
259 | |
---|
260 | /* |
---|
261 | * The exception table consists of pairs of addresses: the first is the |
---|
262 | * address of an instruction that is allowed to fault, and the second is |
---|
263 | * the address at which the program should continue. No registers are |
---|
264 | * modified, so it is entirely up to the continuation code to figure out |
---|
265 | * what to do. |
---|
266 | * |
---|
267 | * All the routines below use bits of fixup code that are out of line |
---|
268 | * with the main instruction path. This means when everything is well, |
---|
269 | * we don't even have to jump over them. Further, they do not intrude |
---|
270 | * on our cache or tlb entries. |
---|
271 | */ |
---|
272 | |
---|
273 | struct exception_table_entry |
---|
274 | { |
---|
275 | unsigned long insn, fixup; |
---|
276 | }; |
---|
277 | |
---|
278 | extern unsigned long search_exception_table(unsigned long); |
---|
279 | extern void sort_exception_tables(void); |
---|
280 | |
---|
281 | #endif /* __X86_UACCESS_H__ */ |
---|