uml: 64-bit tlb fixes
[pv_ops_mirror.git] / include / asm-x86 / uaccess_32.h
blobfcc570ec4feed9107f4910da8e6a4939df3d2824
1 #ifndef __i386_UACCESS_H
2 #define __i386_UACCESS_H
4 /*
5 * User space memory access functions
6 */
7 #include <linux/errno.h>
8 #include <linux/thread_info.h>
9 #include <linux/prefetch.h>
10 #include <linux/string.h>
11 #include <asm/asm.h>
12 #include <asm/page.h>
14 #define VERIFY_READ 0
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) })
28 #define KERNEL_DS MAKE_MM_SEG(0xFFFFFFFFUL)
29 #define USER_DS MAKE_MM_SEG(PAGE_OFFSET)
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))
35 #define segment_eq(a,b) ((a).seg == (b).seg)
38 * movsl can be slow when source and dest are not both 8-byte aligned
40 #ifdef CONFIG_X86_INTEL_USERCOPY
41 extern struct movsl_mask {
42 int mask;
43 } ____cacheline_aligned_in_smp movsl_mask;
44 #endif
46 #define __addr_ok(addr) ((unsigned long __force)(addr) < (current_thread_info()->addr_limit.seg))
49 * Test whether a block of memory is a valid user space address.
50 * Returns 0 if the range is valid, nonzero otherwise.
52 * This is equivalent to the following test:
53 * (u33)addr + (u33)size >= (u33)current->addr_limit.seg
55 * This needs 33-bit arithmetic. We have a carry...
57 #define __range_ok(addr,size) ({ \
58 unsigned long flag,roksum; \
59 __chk_user_ptr(addr); \
60 asm("addl %3,%1 ; sbbl %0,%0; cmpl %1,%4; sbbl $0,%0" \
61 :"=&r" (flag), "=r" (roksum) \
62 :"1" (addr),"g" ((int)(size)),"rm" (current_thread_info()->addr_limit.seg)); \
63 flag; })
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
73 * Context: User context only. This function may sleep.
75 * Checks if a pointer to a block of memory in user space is valid.
77 * Returns true (nonzero) if the memory block may be valid, false (zero)
78 * if it is definitely invalid.
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.
84 #define access_ok(type,addr,size) (likely(__range_ok(addr,size) == 0))
87 * The exception table consists of pairs of addresses: the first is the
88 * address of an instruction that is allowed to fault, and the second is
89 * the address at which the program should continue. No registers are
90 * modified, so it is entirely up to the continuation code to figure out
91 * what to do.
93 * All the routines below use bits of fixup code that are out of line
94 * with the main instruction path. This means when everything is well,
95 * we don't even have to jump over them. Further, they do not intrude
96 * on our cache or tlb entries.
99 struct exception_table_entry
101 unsigned long insn, fixup;
104 extern int fixup_exception(struct pt_regs *regs);
107 * These are the main single-value transfer routines. They automatically
108 * use the right size if we just have the right pointer type.
110 * This gets kind of ugly. We want to return _two_ values in "get_user()"
111 * and yet we don't want to do any pointers, because that is too much
112 * of a performance impact. Thus we have a few rather ugly macros here,
113 * and hide all the ugliness from the user.
115 * The "__xxx" versions of the user access functions are versions that
116 * do not verify the address space, that must have been done previously
117 * with a separate "access_ok()" call (this is used when we do multiple
118 * accesses to the same area of user memory).
121 extern void __get_user_1(void);
122 extern void __get_user_2(void);
123 extern void __get_user_4(void);
125 #define __get_user_x(size,ret,x,ptr) \
126 __asm__ __volatile__("call __get_user_" #size \
127 :"=a" (ret),"=d" (x) \
128 :"0" (ptr))
131 /* Careful: we have to cast the result to the type of the pointer for sign reasons */
133 * get_user: - Get a simple variable from user space.
134 * @x: Variable to store result.
135 * @ptr: Source address, in user space.
137 * Context: User context only. This function may sleep.
139 * This macro copies a single simple variable from user space to kernel
140 * space. It supports simple types like char and int, but not larger
141 * data types like structures or arrays.
143 * @ptr must have pointer-to-simple-variable type, and the result of
144 * dereferencing @ptr must be assignable to @x without a cast.
146 * Returns zero on success, or -EFAULT on error.
147 * On error, the variable @x is set to zero.
149 #define get_user(x,ptr) \
150 ({ int __ret_gu; \
151 unsigned long __val_gu; \
152 __chk_user_ptr(ptr); \
153 switch(sizeof (*(ptr))) { \
154 case 1: __get_user_x(1,__ret_gu,__val_gu,ptr); break; \
155 case 2: __get_user_x(2,__ret_gu,__val_gu,ptr); break; \
156 case 4: __get_user_x(4,__ret_gu,__val_gu,ptr); break; \
157 default: __get_user_x(X,__ret_gu,__val_gu,ptr); break; \
159 (x) = (__typeof__(*(ptr)))__val_gu; \
160 __ret_gu; \
163 extern void __put_user_bad(void);
166 * Strange magic calling convention: pointer in %ecx,
167 * value in %eax(:%edx), return value in %eax, no clobbers.
169 extern void __put_user_1(void);
170 extern void __put_user_2(void);
171 extern void __put_user_4(void);
172 extern void __put_user_8(void);
174 #define __put_user_1(x, ptr) __asm__ __volatile__("call __put_user_1":"=a" (__ret_pu):"0" ((typeof(*(ptr)))(x)), "c" (ptr))
175 #define __put_user_2(x, ptr) __asm__ __volatile__("call __put_user_2":"=a" (__ret_pu):"0" ((typeof(*(ptr)))(x)), "c" (ptr))
176 #define __put_user_4(x, ptr) __asm__ __volatile__("call __put_user_4":"=a" (__ret_pu):"0" ((typeof(*(ptr)))(x)), "c" (ptr))
177 #define __put_user_8(x, ptr) __asm__ __volatile__("call __put_user_8":"=a" (__ret_pu):"A" ((typeof(*(ptr)))(x)), "c" (ptr))
178 #define __put_user_X(x, ptr) __asm__ __volatile__("call __put_user_X":"=a" (__ret_pu):"c" (ptr))
181 * put_user: - Write a simple value into user space.
182 * @x: Value to copy to user space.
183 * @ptr: Destination address, in user space.
185 * Context: User context only. This function may sleep.
187 * This macro copies a single simple value from kernel space to user
188 * space. It supports simple types like char and int, but not larger
189 * data types like structures or arrays.
191 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
192 * to the result of dereferencing @ptr.
194 * Returns zero on success, or -EFAULT on error.
196 #ifdef CONFIG_X86_WP_WORKS_OK
198 #define put_user(x,ptr) \
199 ({ int __ret_pu; \
200 __typeof__(*(ptr)) __pu_val; \
201 __chk_user_ptr(ptr); \
202 __pu_val = x; \
203 switch(sizeof(*(ptr))) { \
204 case 1: __put_user_1(__pu_val, ptr); break; \
205 case 2: __put_user_2(__pu_val, ptr); break; \
206 case 4: __put_user_4(__pu_val, ptr); break; \
207 case 8: __put_user_8(__pu_val, ptr); break; \
208 default:__put_user_X(__pu_val, ptr); break; \
210 __ret_pu; \
213 #else
214 #define put_user(x,ptr) \
215 ({ \
216 int __ret_pu; \
217 __typeof__(*(ptr)) __pus_tmp = x; \
218 __ret_pu=0; \
219 if(unlikely(__copy_to_user_ll(ptr, &__pus_tmp, \
220 sizeof(*(ptr))) != 0)) \
221 __ret_pu=-EFAULT; \
222 __ret_pu; \
226 #endif
229 * __get_user: - Get a simple variable from user space, with less checking.
230 * @x: Variable to store result.
231 * @ptr: Source address, in user space.
233 * Context: User context only. This function may sleep.
235 * This macro copies a single simple variable from user space to kernel
236 * space. It supports simple types like char and int, but not larger
237 * data types like structures or arrays.
239 * @ptr must have pointer-to-simple-variable type, and the result of
240 * dereferencing @ptr must be assignable to @x without a cast.
242 * Caller must check the pointer with access_ok() before calling this
243 * function.
245 * Returns zero on success, or -EFAULT on error.
246 * On error, the variable @x is set to zero.
248 #define __get_user(x,ptr) \
249 __get_user_nocheck((x),(ptr),sizeof(*(ptr)))
253 * __put_user: - Write a simple value into user space, with less checking.
254 * @x: Value to copy to user space.
255 * @ptr: Destination address, in user space.
257 * Context: User context only. This function may sleep.
259 * This macro copies a single simple value from kernel space to user
260 * space. It supports simple types like char and int, but not larger
261 * data types like structures or arrays.
263 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
264 * to the result of dereferencing @ptr.
266 * Caller must check the pointer with access_ok() before calling this
267 * function.
269 * Returns zero on success, or -EFAULT on error.
271 #define __put_user(x,ptr) \
272 __put_user_nocheck((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
274 #define __put_user_nocheck(x,ptr,size) \
275 ({ \
276 long __pu_err; \
277 __put_user_size((x),(ptr),(size),__pu_err,-EFAULT); \
278 __pu_err; \
282 #define __put_user_u64(x, addr, err) \
283 __asm__ __volatile__( \
284 "1: movl %%eax,0(%2)\n" \
285 "2: movl %%edx,4(%2)\n" \
286 "3:\n" \
287 ".section .fixup,\"ax\"\n" \
288 "4: movl %3,%0\n" \
289 " jmp 3b\n" \
290 ".previous\n" \
291 _ASM_EXTABLE(1b,4b) \
292 _ASM_EXTABLE(2b,4b) \
293 : "=r"(err) \
294 : "A" (x), "r" (addr), "i"(-EFAULT), "0"(err))
296 #ifdef CONFIG_X86_WP_WORKS_OK
298 #define __put_user_size(x,ptr,size,retval,errret) \
299 do { \
300 retval = 0; \
301 __chk_user_ptr(ptr); \
302 switch (size) { \
303 case 1: __put_user_asm(x,ptr,retval,"b","b","iq",errret);break; \
304 case 2: __put_user_asm(x,ptr,retval,"w","w","ir",errret);break; \
305 case 4: __put_user_asm(x,ptr,retval,"l","","ir",errret); break; \
306 case 8: __put_user_u64((__typeof__(*ptr))(x),ptr,retval); break;\
307 default: __put_user_bad(); \
309 } while (0)
311 #else
313 #define __put_user_size(x,ptr,size,retval,errret) \
314 do { \
315 __typeof__(*(ptr)) __pus_tmp = x; \
316 retval = 0; \
318 if(unlikely(__copy_to_user_ll(ptr, &__pus_tmp, size) != 0)) \
319 retval = errret; \
320 } while (0)
322 #endif
323 struct __large_struct { unsigned long buf[100]; };
324 #define __m(x) (*(struct __large_struct __user *)(x))
327 * Tell gcc we read from memory instead of writing: this is because
328 * we do not write to any memory gcc knows about, so there are no
329 * aliasing issues.
331 #define __put_user_asm(x, addr, err, itype, rtype, ltype, errret) \
332 __asm__ __volatile__( \
333 "1: mov"itype" %"rtype"1,%2\n" \
334 "2:\n" \
335 ".section .fixup,\"ax\"\n" \
336 "3: movl %3,%0\n" \
337 " jmp 2b\n" \
338 ".previous\n" \
339 _ASM_EXTABLE(1b,3b) \
340 : "=r"(err) \
341 : ltype (x), "m"(__m(addr)), "i"(errret), "0"(err))
344 #define __get_user_nocheck(x,ptr,size) \
345 ({ \
346 long __gu_err; \
347 unsigned long __gu_val; \
348 __get_user_size(__gu_val,(ptr),(size),__gu_err,-EFAULT);\
349 (x) = (__typeof__(*(ptr)))__gu_val; \
350 __gu_err; \
353 extern long __get_user_bad(void);
355 #define __get_user_size(x,ptr,size,retval,errret) \
356 do { \
357 retval = 0; \
358 __chk_user_ptr(ptr); \
359 switch (size) { \
360 case 1: __get_user_asm(x,ptr,retval,"b","b","=q",errret);break; \
361 case 2: __get_user_asm(x,ptr,retval,"w","w","=r",errret);break; \
362 case 4: __get_user_asm(x,ptr,retval,"l","","=r",errret);break; \
363 default: (x) = __get_user_bad(); \
365 } while (0)
367 #define __get_user_asm(x, addr, err, itype, rtype, ltype, errret) \
368 __asm__ __volatile__( \
369 "1: mov"itype" %2,%"rtype"1\n" \
370 "2:\n" \
371 ".section .fixup,\"ax\"\n" \
372 "3: movl %3,%0\n" \
373 " xor"itype" %"rtype"1,%"rtype"1\n" \
374 " jmp 2b\n" \
375 ".previous\n" \
376 _ASM_EXTABLE(1b,3b) \
377 : "=r"(err), ltype (x) \
378 : "m"(__m(addr)), "i"(errret), "0"(err))
381 unsigned long __must_check __copy_to_user_ll(void __user *to,
382 const void *from, unsigned long n);
383 unsigned long __must_check __copy_from_user_ll(void *to,
384 const void __user *from, unsigned long n);
385 unsigned long __must_check __copy_from_user_ll_nozero(void *to,
386 const void __user *from, unsigned long n);
387 unsigned long __must_check __copy_from_user_ll_nocache(void *to,
388 const void __user *from, unsigned long n);
389 unsigned long __must_check __copy_from_user_ll_nocache_nozero(void *to,
390 const void __user *from, unsigned long n);
393 * __copy_to_user_inatomic: - Copy a block of data into user space, with less checking.
394 * @to: Destination address, in user space.
395 * @from: Source address, in kernel space.
396 * @n: Number of bytes to copy.
398 * Context: User context only.
400 * Copy data from kernel space to user space. Caller must check
401 * the specified block with access_ok() before calling this function.
402 * The caller should also make sure he pins the user space address
403 * so that the we don't result in page fault and sleep.
405 * Here we special-case 1, 2 and 4-byte copy_*_user invocations. On a fault
406 * we return the initial request size (1, 2 or 4), as copy_*_user should do.
407 * If a store crosses a page boundary and gets a fault, the x86 will not write
408 * anything, so this is accurate.
411 static __always_inline unsigned long __must_check
412 __copy_to_user_inatomic(void __user *to, const void *from, unsigned long n)
414 if (__builtin_constant_p(n)) {
415 unsigned long ret;
417 switch (n) {
418 case 1:
419 __put_user_size(*(u8 *)from, (u8 __user *)to, 1, ret, 1);
420 return ret;
421 case 2:
422 __put_user_size(*(u16 *)from, (u16 __user *)to, 2, ret, 2);
423 return ret;
424 case 4:
425 __put_user_size(*(u32 *)from, (u32 __user *)to, 4, ret, 4);
426 return ret;
429 return __copy_to_user_ll(to, from, n);
433 * __copy_to_user: - Copy a block of data into user space, with less checking.
434 * @to: Destination address, in user space.
435 * @from: Source address, in kernel space.
436 * @n: Number of bytes to copy.
438 * Context: User context only. This function may sleep.
440 * Copy data from kernel space to user space. Caller must check
441 * the specified block with access_ok() before calling this function.
443 * Returns number of bytes that could not be copied.
444 * On success, this will be zero.
446 static __always_inline unsigned long __must_check
447 __copy_to_user(void __user *to, const void *from, unsigned long n)
449 might_sleep();
450 return __copy_to_user_inatomic(to, from, n);
453 static __always_inline unsigned long
454 __copy_from_user_inatomic(void *to, const void __user *from, unsigned long n)
456 /* Avoid zeroing the tail if the copy fails..
457 * If 'n' is constant and 1, 2, or 4, we do still zero on a failure,
458 * but as the zeroing behaviour is only significant when n is not
459 * constant, that shouldn't be a problem.
461 if (__builtin_constant_p(n)) {
462 unsigned long ret;
464 switch (n) {
465 case 1:
466 __get_user_size(*(u8 *)to, from, 1, ret, 1);
467 return ret;
468 case 2:
469 __get_user_size(*(u16 *)to, from, 2, ret, 2);
470 return ret;
471 case 4:
472 __get_user_size(*(u32 *)to, from, 4, ret, 4);
473 return ret;
476 return __copy_from_user_ll_nozero(to, from, n);
480 * __copy_from_user: - Copy a block of data from user space, with less checking.
481 * @to: Destination address, in kernel space.
482 * @from: Source address, in user space.
483 * @n: Number of bytes to copy.
485 * Context: User context only. This function may sleep.
487 * Copy data from user space to kernel space. Caller must check
488 * the specified block with access_ok() before calling this function.
490 * Returns number of bytes that could not be copied.
491 * On success, this will be zero.
493 * If some data could not be copied, this function will pad the copied
494 * data to the requested size using zero bytes.
496 * An alternate version - __copy_from_user_inatomic() - may be called from
497 * atomic context and will fail rather than sleep. In this case the
498 * uncopied bytes will *NOT* be padded with zeros. See fs/filemap.h
499 * for explanation of why this is needed.
501 static __always_inline unsigned long
502 __copy_from_user(void *to, const void __user *from, unsigned long n)
504 might_sleep();
505 if (__builtin_constant_p(n)) {
506 unsigned long ret;
508 switch (n) {
509 case 1:
510 __get_user_size(*(u8 *)to, from, 1, ret, 1);
511 return ret;
512 case 2:
513 __get_user_size(*(u16 *)to, from, 2, ret, 2);
514 return ret;
515 case 4:
516 __get_user_size(*(u32 *)to, from, 4, ret, 4);
517 return ret;
520 return __copy_from_user_ll(to, from, n);
523 #define ARCH_HAS_NOCACHE_UACCESS
525 static __always_inline unsigned long __copy_from_user_nocache(void *to,
526 const void __user *from, unsigned long n)
528 might_sleep();
529 if (__builtin_constant_p(n)) {
530 unsigned long ret;
532 switch (n) {
533 case 1:
534 __get_user_size(*(u8 *)to, from, 1, ret, 1);
535 return ret;
536 case 2:
537 __get_user_size(*(u16 *)to, from, 2, ret, 2);
538 return ret;
539 case 4:
540 __get_user_size(*(u32 *)to, from, 4, ret, 4);
541 return ret;
544 return __copy_from_user_ll_nocache(to, from, n);
547 static __always_inline unsigned long
548 __copy_from_user_inatomic_nocache(void *to, const void __user *from, unsigned long n)
550 return __copy_from_user_ll_nocache_nozero(to, from, n);
553 unsigned long __must_check copy_to_user(void __user *to,
554 const void *from, unsigned long n);
555 unsigned long __must_check copy_from_user(void *to,
556 const void __user *from, unsigned long n);
557 long __must_check strncpy_from_user(char *dst, const char __user *src,
558 long count);
559 long __must_check __strncpy_from_user(char *dst,
560 const char __user *src, long count);
563 * strlen_user: - Get the size of a string in user space.
564 * @str: The string to measure.
566 * Context: User context only. This function may sleep.
568 * Get the size of a NUL-terminated string in user space.
570 * Returns the size of the string INCLUDING the terminating NUL.
571 * On exception, returns 0.
573 * If there is a limit on the length of a valid string, you may wish to
574 * consider using strnlen_user() instead.
576 #define strlen_user(str) strnlen_user(str, LONG_MAX)
578 long strnlen_user(const char __user *str, long n);
579 unsigned long __must_check clear_user(void __user *mem, unsigned long len);
580 unsigned long __must_check __clear_user(void __user *mem, unsigned long len);
582 #endif /* __i386_UACCESS_H */