Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | #ifndef __i386_UACCESS_H |
2 | #define __i386_UACCESS_H | |
3 | ||
4 | /* | |
5 | * User space memory access functions | |
6 | */ | |
7 | #include <linux/config.h> | |
8 | #include <linux/errno.h> | |
9 | #include <linux/thread_info.h> | |
10 | #include <linux/prefetch.h> | |
11 | #include <linux/string.h> | |
12 | #include <asm/page.h> | |
13 | ||
14 | #define VERIFY_READ 0 | |
15 | #define VERIFY_WRITE 1 | |
16 | ||
17 | /* | |
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. | |
21 | * | |
22 | * For historical reasons, these macros are grossly misnamed. | |
23 | */ | |
24 | ||
25 | #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) }) | |
26 | ||
27 | ||
28 | #define KERNEL_DS MAKE_MM_SEG(0xFFFFFFFFUL) | |
29 | #define USER_DS MAKE_MM_SEG(PAGE_OFFSET) | |
30 | ||
31 | #define get_ds() (KERNEL_DS) | |
32 | #define get_fs() (current_thread_info()->addr_limit) | |
33 | #define set_fs(x) (current_thread_info()->addr_limit = (x)) | |
34 | ||
35 | #define segment_eq(a,b) ((a).seg == (b).seg) | |
36 | ||
37 | /* | |
38 | * movsl can be slow when source and dest are not both 8-byte aligned | |
39 | */ | |
40 | #ifdef CONFIG_X86_INTEL_USERCOPY | |
41 | extern struct movsl_mask { | |
42 | int mask; | |
43 | } ____cacheline_aligned_in_smp movsl_mask; | |
44 | #endif | |
45 | ||
46 | #define __addr_ok(addr) ((unsigned long __force)(addr) < (current_thread_info()->addr_limit.seg)) | |
47 | ||
48 | /* | |
49 | * Test whether a block of memory is a valid user space address. | |
50 | * Returns 0 if the range is valid, nonzero otherwise. | |
51 | * | |
52 | * This is equivalent to the following test: | |
53 | * (u33)addr + (u33)size >= (u33)current->addr_limit.seg | |
54 | * | |
55 | * This needs 33-bit arithmetic. We have a carry... | |
56 | */ | |
57 | #define __range_ok(addr,size) ({ \ | |
58 | unsigned long flag,sum; \ | |
59 | __chk_user_ptr(addr); \ | |
60 | asm("addl %3,%1 ; sbbl %0,%0; cmpl %1,%4; sbbl $0,%0" \ | |
61 | :"=&r" (flag), "=r" (sum) \ | |
62 | :"1" (addr),"g" ((int)(size)),"g" (current_thread_info()->addr_limit.seg)); \ | |
63 | flag; }) | |
64 | ||
65 | /** | |
66 | * access_ok: - Checks if a user space pointer is valid | |
67 | * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that | |
68 | * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe | |
69 | * to write to a block, it is always safe to read from it. | |
70 | * @addr: User space pointer to start of block to check | |
71 | * @size: Size of block to check | |
72 | * | |
73 | * Context: User context only. This function may sleep. | |
74 | * | |
75 | * Checks if a pointer to a block of memory in user space is valid. | |
76 | * | |
77 | * Returns true (nonzero) if the memory block may be valid, false (zero) | |
78 | * if it is definitely invalid. | |
79 | * | |
80 | * Note that, depending on architecture, this function probably just | |
81 | * checks that the pointer is in the user space range - after calling | |
82 | * this function, memory access functions may still return -EFAULT. | |
83 | */ | |
84 | #define access_ok(type,addr,size) (likely(__range_ok(addr,size) == 0)) | |
85 | ||
86 | /** | |
87 | * verify_area: - Obsolete/deprecated and will go away soon, | |
88 | * use access_ok() instead. | |
89 | * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE | |
90 | * @addr: User space pointer to start of block to check | |
91 | * @size: Size of block to check | |
92 | * | |
93 | * Context: User context only. This function may sleep. | |
94 | * | |
95 | * This function has been replaced by access_ok(). | |
96 | * | |
97 | * Checks if a pointer to a block of memory in user space is valid. | |
98 | * | |
99 | * Returns zero if the memory block may be valid, -EFAULT | |
100 | * if it is definitely invalid. | |
101 | * | |
102 | * See access_ok() for more details. | |
103 | */ | |
104 | static inline int __deprecated verify_area(int type, const void __user * addr, unsigned long size) | |
105 | { | |
106 | return access_ok(type,addr,size) ? 0 : -EFAULT; | |
107 | } | |
108 | ||
109 | ||
110 | /* | |
111 | * The exception table consists of pairs of addresses: the first is the | |
112 | * address of an instruction that is allowed to fault, and the second is | |
113 | * the address at which the program should continue. No registers are | |
114 | * modified, so it is entirely up to the continuation code to figure out | |
115 | * what to do. | |
116 | * | |
117 | * All the routines below use bits of fixup code that are out of line | |
118 | * with the main instruction path. This means when everything is well, | |
119 | * we don't even have to jump over them. Further, they do not intrude | |
120 | * on our cache or tlb entries. | |
121 | */ | |
122 | ||
123 | struct exception_table_entry | |
124 | { | |
125 | unsigned long insn, fixup; | |
126 | }; | |
127 | ||
128 | extern int fixup_exception(struct pt_regs *regs); | |
129 | ||
130 | /* | |
131 | * These are the main single-value transfer routines. They automatically | |
132 | * use the right size if we just have the right pointer type. | |
133 | * | |
134 | * This gets kind of ugly. We want to return _two_ values in "get_user()" | |
135 | * and yet we don't want to do any pointers, because that is too much | |
136 | * of a performance impact. Thus we have a few rather ugly macros here, | |
137 | * and hide all the ugliness from the user. | |
138 | * | |
139 | * The "__xxx" versions of the user access functions are versions that | |
140 | * do not verify the address space, that must have been done previously | |
141 | * with a separate "access_ok()" call (this is used when we do multiple | |
142 | * accesses to the same area of user memory). | |
143 | */ | |
144 | ||
145 | extern void __get_user_1(void); | |
146 | extern void __get_user_2(void); | |
147 | extern void __get_user_4(void); | |
148 | ||
149 | #define __get_user_x(size,ret,x,ptr) \ | |
150 | __asm__ __volatile__("call __get_user_" #size \ | |
151 | :"=a" (ret),"=d" (x) \ | |
152 | :"0" (ptr)) | |
153 | ||
154 | ||
155 | /* Careful: we have to cast the result to the type of the pointer for sign reasons */ | |
156 | /** | |
157 | * get_user: - Get a simple variable from user space. | |
158 | * @x: Variable to store result. | |
159 | * @ptr: Source address, in user space. | |
160 | * | |
161 | * Context: User context only. This function may sleep. | |
162 | * | |
163 | * This macro copies a single simple variable from user space to kernel | |
164 | * space. It supports simple types like char and int, but not larger | |
165 | * data types like structures or arrays. | |
166 | * | |
167 | * @ptr must have pointer-to-simple-variable type, and the result of | |
168 | * dereferencing @ptr must be assignable to @x without a cast. | |
169 | * | |
170 | * Returns zero on success, or -EFAULT on error. | |
171 | * On error, the variable @x is set to zero. | |
172 | */ | |
173 | #define get_user(x,ptr) \ | |
174 | ({ int __ret_gu; \ | |
175 | unsigned long __val_gu; \ | |
176 | __chk_user_ptr(ptr); \ | |
177 | switch(sizeof (*(ptr))) { \ | |
178 | case 1: __get_user_x(1,__ret_gu,__val_gu,ptr); break; \ | |
179 | case 2: __get_user_x(2,__ret_gu,__val_gu,ptr); break; \ | |
180 | case 4: __get_user_x(4,__ret_gu,__val_gu,ptr); break; \ | |
181 | default: __get_user_x(X,__ret_gu,__val_gu,ptr); break; \ | |
182 | } \ | |
183 | (x) = (__typeof__(*(ptr)))__val_gu; \ | |
184 | __ret_gu; \ | |
185 | }) | |
186 | ||
187 | extern void __put_user_bad(void); | |
188 | ||
189 | /* | |
190 | * Strange magic calling convention: pointer in %ecx, | |
191 | * value in %eax(:%edx), return value in %eax, no clobbers. | |
192 | */ | |
193 | extern void __put_user_1(void); | |
194 | extern void __put_user_2(void); | |
195 | extern void __put_user_4(void); | |
196 | extern void __put_user_8(void); | |
197 | ||
198 | #define __put_user_1(x, ptr) __asm__ __volatile__("call __put_user_1":"=a" (__ret_pu):"0" ((typeof(*(ptr)))(x)), "c" (ptr)) | |
199 | #define __put_user_2(x, ptr) __asm__ __volatile__("call __put_user_2":"=a" (__ret_pu):"0" ((typeof(*(ptr)))(x)), "c" (ptr)) | |
200 | #define __put_user_4(x, ptr) __asm__ __volatile__("call __put_user_4":"=a" (__ret_pu):"0" ((typeof(*(ptr)))(x)), "c" (ptr)) | |
201 | #define __put_user_8(x, ptr) __asm__ __volatile__("call __put_user_8":"=a" (__ret_pu):"A" ((typeof(*(ptr)))(x)), "c" (ptr)) | |
202 | #define __put_user_X(x, ptr) __asm__ __volatile__("call __put_user_X":"=a" (__ret_pu):"c" (ptr)) | |
203 | ||
204 | /** | |
205 | * put_user: - Write a simple value into user space. | |
206 | * @x: Value to copy to user space. | |
207 | * @ptr: Destination address, in user space. | |
208 | * | |
209 | * Context: User context only. This function may sleep. | |
210 | * | |
211 | * This macro copies a single simple value from kernel space to user | |
212 | * space. It supports simple types like char and int, but not larger | |
213 | * data types like structures or arrays. | |
214 | * | |
215 | * @ptr must have pointer-to-simple-variable type, and @x must be assignable | |
216 | * to the result of dereferencing @ptr. | |
217 | * | |
218 | * Returns zero on success, or -EFAULT on error. | |
219 | */ | |
220 | #ifdef CONFIG_X86_WP_WORKS_OK | |
221 | ||
222 | #define put_user(x,ptr) \ | |
223 | ({ int __ret_pu; \ | |
224 | __chk_user_ptr(ptr); \ | |
225 | switch(sizeof(*(ptr))) { \ | |
226 | case 1: __put_user_1(x, ptr); break; \ | |
227 | case 2: __put_user_2(x, ptr); break; \ | |
228 | case 4: __put_user_4(x, ptr); break; \ | |
229 | case 8: __put_user_8(x, ptr); break; \ | |
230 | default:__put_user_X(x, ptr); break; \ | |
231 | } \ | |
232 | __ret_pu; \ | |
233 | }) | |
234 | ||
235 | #else | |
236 | #define put_user(x,ptr) \ | |
237 | ({ \ | |
238 | int __ret_pu; \ | |
239 | __typeof__(*(ptr)) __pus_tmp = x; \ | |
240 | __ret_pu=0; \ | |
241 | if(unlikely(__copy_to_user_ll(ptr, &__pus_tmp, \ | |
242 | sizeof(*(ptr))) != 0)) \ | |
243 | __ret_pu=-EFAULT; \ | |
244 | __ret_pu; \ | |
245 | }) | |
246 | ||
247 | ||
248 | #endif | |
249 | ||
250 | /** | |
251 | * __get_user: - Get a simple variable from user space, with less checking. | |
252 | * @x: Variable to store result. | |
253 | * @ptr: Source address, in user space. | |
254 | * | |
255 | * Context: User context only. This function may sleep. | |
256 | * | |
257 | * This macro copies a single simple variable from user space to kernel | |
258 | * space. It supports simple types like char and int, but not larger | |
259 | * data types like structures or arrays. | |
260 | * | |
261 | * @ptr must have pointer-to-simple-variable type, and the result of | |
262 | * dereferencing @ptr must be assignable to @x without a cast. | |
263 | * | |
264 | * Caller must check the pointer with access_ok() before calling this | |
265 | * function. | |
266 | * | |
267 | * Returns zero on success, or -EFAULT on error. | |
268 | * On error, the variable @x is set to zero. | |
269 | */ | |
270 | #define __get_user(x,ptr) \ | |
271 | __get_user_nocheck((x),(ptr),sizeof(*(ptr))) | |
272 | ||
273 | ||
274 | /** | |
275 | * __put_user: - Write a simple value into user space, with less checking. | |
276 | * @x: Value to copy to user space. | |
277 | * @ptr: Destination address, in user space. | |
278 | * | |
279 | * Context: User context only. This function may sleep. | |
280 | * | |
281 | * This macro copies a single simple value from kernel space to user | |
282 | * space. It supports simple types like char and int, but not larger | |
283 | * data types like structures or arrays. | |
284 | * | |
285 | * @ptr must have pointer-to-simple-variable type, and @x must be assignable | |
286 | * to the result of dereferencing @ptr. | |
287 | * | |
288 | * Caller must check the pointer with access_ok() before calling this | |
289 | * function. | |
290 | * | |
291 | * Returns zero on success, or -EFAULT on error. | |
292 | */ | |
293 | #define __put_user(x,ptr) \ | |
294 | __put_user_nocheck((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr))) | |
295 | ||
296 | #define __put_user_nocheck(x,ptr,size) \ | |
297 | ({ \ | |
298 | long __pu_err; \ | |
299 | __put_user_size((x),(ptr),(size),__pu_err,-EFAULT); \ | |
300 | __pu_err; \ | |
301 | }) | |
302 | ||
303 | ||
304 | #define __put_user_u64(x, addr, err) \ | |
305 | __asm__ __volatile__( \ | |
306 | "1: movl %%eax,0(%2)\n" \ | |
307 | "2: movl %%edx,4(%2)\n" \ | |
308 | "3:\n" \ | |
309 | ".section .fixup,\"ax\"\n" \ | |
310 | "4: movl %3,%0\n" \ | |
311 | " jmp 3b\n" \ | |
312 | ".previous\n" \ | |
313 | ".section __ex_table,\"a\"\n" \ | |
314 | " .align 4\n" \ | |
315 | " .long 1b,4b\n" \ | |
316 | " .long 2b,4b\n" \ | |
317 | ".previous" \ | |
318 | : "=r"(err) \ | |
319 | : "A" (x), "r" (addr), "i"(-EFAULT), "0"(err)) | |
320 | ||
321 | #ifdef CONFIG_X86_WP_WORKS_OK | |
322 | ||
323 | #define __put_user_size(x,ptr,size,retval,errret) \ | |
324 | do { \ | |
325 | retval = 0; \ | |
326 | __chk_user_ptr(ptr); \ | |
327 | switch (size) { \ | |
328 | case 1: __put_user_asm(x,ptr,retval,"b","b","iq",errret);break; \ | |
329 | case 2: __put_user_asm(x,ptr,retval,"w","w","ir",errret);break; \ | |
330 | case 4: __put_user_asm(x,ptr,retval,"l","","ir",errret); break; \ | |
331 | case 8: __put_user_u64((__typeof__(*ptr))(x),ptr,retval); break;\ | |
332 | default: __put_user_bad(); \ | |
333 | } \ | |
334 | } while (0) | |
335 | ||
336 | #else | |
337 | ||
338 | #define __put_user_size(x,ptr,size,retval,errret) \ | |
339 | do { \ | |
340 | __typeof__(*(ptr)) __pus_tmp = x; \ | |
341 | retval = 0; \ | |
342 | \ | |
343 | if(unlikely(__copy_to_user_ll(ptr, &__pus_tmp, size) != 0)) \ | |
344 | retval = errret; \ | |
345 | } while (0) | |
346 | ||
347 | #endif | |
348 | struct __large_struct { unsigned long buf[100]; }; | |
349 | #define __m(x) (*(struct __large_struct __user *)(x)) | |
350 | ||
351 | /* | |
352 | * Tell gcc we read from memory instead of writing: this is because | |
353 | * we do not write to any memory gcc knows about, so there are no | |
354 | * aliasing issues. | |
355 | */ | |
356 | #define __put_user_asm(x, addr, err, itype, rtype, ltype, errret) \ | |
357 | __asm__ __volatile__( \ | |
358 | "1: mov"itype" %"rtype"1,%2\n" \ | |
359 | "2:\n" \ | |
360 | ".section .fixup,\"ax\"\n" \ | |
361 | "3: movl %3,%0\n" \ | |
362 | " jmp 2b\n" \ | |
363 | ".previous\n" \ | |
364 | ".section __ex_table,\"a\"\n" \ | |
365 | " .align 4\n" \ | |
366 | " .long 1b,3b\n" \ | |
367 | ".previous" \ | |
368 | : "=r"(err) \ | |
369 | : ltype (x), "m"(__m(addr)), "i"(errret), "0"(err)) | |
370 | ||
371 | ||
372 | #define __get_user_nocheck(x,ptr,size) \ | |
373 | ({ \ | |
374 | long __gu_err; \ | |
375 | unsigned long __gu_val; \ | |
376 | __get_user_size(__gu_val,(ptr),(size),__gu_err,-EFAULT);\ | |
377 | (x) = (__typeof__(*(ptr)))__gu_val; \ | |
378 | __gu_err; \ | |
379 | }) | |
380 | ||
381 | extern long __get_user_bad(void); | |
382 | ||
383 | #define __get_user_size(x,ptr,size,retval,errret) \ | |
384 | do { \ | |
385 | retval = 0; \ | |
386 | __chk_user_ptr(ptr); \ | |
387 | switch (size) { \ | |
388 | case 1: __get_user_asm(x,ptr,retval,"b","b","=q",errret);break; \ | |
389 | case 2: __get_user_asm(x,ptr,retval,"w","w","=r",errret);break; \ | |
390 | case 4: __get_user_asm(x,ptr,retval,"l","","=r",errret);break; \ | |
391 | default: (x) = __get_user_bad(); \ | |
392 | } \ | |
393 | } while (0) | |
394 | ||
395 | #define __get_user_asm(x, addr, err, itype, rtype, ltype, errret) \ | |
396 | __asm__ __volatile__( \ | |
397 | "1: mov"itype" %2,%"rtype"1\n" \ | |
398 | "2:\n" \ | |
399 | ".section .fixup,\"ax\"\n" \ | |
400 | "3: movl %3,%0\n" \ | |
401 | " xor"itype" %"rtype"1,%"rtype"1\n" \ | |
402 | " jmp 2b\n" \ | |
403 | ".previous\n" \ | |
404 | ".section __ex_table,\"a\"\n" \ | |
405 | " .align 4\n" \ | |
406 | " .long 1b,3b\n" \ | |
407 | ".previous" \ | |
408 | : "=r"(err), ltype (x) \ | |
409 | : "m"(__m(addr)), "i"(errret), "0"(err)) | |
410 | ||
411 | ||
412 | unsigned long __must_check __copy_to_user_ll(void __user *to, | |
413 | const void *from, unsigned long n); | |
414 | unsigned long __must_check __copy_from_user_ll(void *to, | |
415 | const void __user *from, unsigned long n); | |
416 | ||
417 | /* | |
418 | * Here we special-case 1, 2 and 4-byte copy_*_user invocations. On a fault | |
419 | * we return the initial request size (1, 2 or 4), as copy_*_user should do. | |
420 | * If a store crosses a page boundary and gets a fault, the x86 will not write | |
421 | * anything, so this is accurate. | |
422 | */ | |
423 | ||
424 | /** | |
425 | * __copy_to_user: - Copy a block of data into user space, with less checking. | |
426 | * @to: Destination address, in user space. | |
427 | * @from: Source address, in kernel space. | |
428 | * @n: Number of bytes to copy. | |
429 | * | |
430 | * Context: User context only. This function may sleep. | |
431 | * | |
432 | * Copy data from kernel space to user space. Caller must check | |
433 | * the specified block with access_ok() before calling this function. | |
434 | * | |
435 | * Returns number of bytes that could not be copied. | |
436 | * On success, this will be zero. | |
437 | */ | |
438 | static inline unsigned long __must_check | |
439 | __copy_to_user_inatomic(void __user *to, const void *from, unsigned long n) | |
440 | { | |
441 | if (__builtin_constant_p(n)) { | |
442 | unsigned long ret; | |
443 | ||
444 | switch (n) { | |
445 | case 1: | |
446 | __put_user_size(*(u8 *)from, (u8 __user *)to, 1, ret, 1); | |
447 | return ret; | |
448 | case 2: | |
449 | __put_user_size(*(u16 *)from, (u16 __user *)to, 2, ret, 2); | |
450 | return ret; | |
451 | case 4: | |
452 | __put_user_size(*(u32 *)from, (u32 __user *)to, 4, ret, 4); | |
453 | return ret; | |
454 | } | |
455 | } | |
456 | return __copy_to_user_ll(to, from, n); | |
457 | } | |
458 | ||
459 | static inline unsigned long __must_check | |
460 | __copy_to_user(void __user *to, const void *from, unsigned long n) | |
461 | { | |
462 | might_sleep(); | |
463 | return __copy_to_user_inatomic(to, from, n); | |
464 | } | |
465 | ||
466 | /** | |
467 | * __copy_from_user: - Copy a block of data from user space, with less checking. | |
468 | * @to: Destination address, in kernel space. | |
469 | * @from: Source address, in user space. | |
470 | * @n: Number of bytes to copy. | |
471 | * | |
472 | * Context: User context only. This function may sleep. | |
473 | * | |
474 | * Copy data from user space to kernel space. Caller must check | |
475 | * the specified block with access_ok() before calling this function. | |
476 | * | |
477 | * Returns number of bytes that could not be copied. | |
478 | * On success, this will be zero. | |
479 | * | |
480 | * If some data could not be copied, this function will pad the copied | |
481 | * data to the requested size using zero bytes. | |
482 | */ | |
483 | static inline unsigned long | |
484 | __copy_from_user_inatomic(void *to, const void __user *from, unsigned long n) | |
485 | { | |
486 | if (__builtin_constant_p(n)) { | |
487 | unsigned long ret; | |
488 | ||
489 | switch (n) { | |
490 | case 1: | |
491 | __get_user_size(*(u8 *)to, from, 1, ret, 1); | |
492 | return ret; | |
493 | case 2: | |
494 | __get_user_size(*(u16 *)to, from, 2, ret, 2); | |
495 | return ret; | |
496 | case 4: | |
497 | __get_user_size(*(u32 *)to, from, 4, ret, 4); | |
498 | return ret; | |
499 | } | |
500 | } | |
501 | return __copy_from_user_ll(to, from, n); | |
502 | } | |
503 | ||
504 | static inline unsigned long | |
505 | __copy_from_user(void *to, const void __user *from, unsigned long n) | |
506 | { | |
507 | might_sleep(); | |
508 | return __copy_from_user_inatomic(to, from, n); | |
509 | } | |
510 | unsigned long __must_check copy_to_user(void __user *to, | |
511 | const void *from, unsigned long n); | |
512 | unsigned long __must_check copy_from_user(void *to, | |
513 | const void __user *from, unsigned long n); | |
514 | long __must_check strncpy_from_user(char *dst, const char __user *src, | |
515 | long count); | |
516 | long __must_check __strncpy_from_user(char *dst, | |
517 | const char __user *src, long count); | |
518 | ||
519 | /** | |
520 | * strlen_user: - Get the size of a string in user space. | |
521 | * @str: The string to measure. | |
522 | * | |
523 | * Context: User context only. This function may sleep. | |
524 | * | |
525 | * Get the size of a NUL-terminated string in user space. | |
526 | * | |
527 | * Returns the size of the string INCLUDING the terminating NUL. | |
528 | * On exception, returns 0. | |
529 | * | |
530 | * If there is a limit on the length of a valid string, you may wish to | |
531 | * consider using strnlen_user() instead. | |
532 | */ | |
533 | #define strlen_user(str) strnlen_user(str, ~0UL >> 1) | |
534 | ||
535 | long strnlen_user(const char __user *str, long n); | |
536 | unsigned long __must_check clear_user(void __user *mem, unsigned long len); | |
537 | unsigned long __must_check __clear_user(void __user *mem, unsigned long len); | |
538 | ||
539 | #endif /* __i386_UACCESS_H */ |