1 #ifndef __i386_UACCESS_H
2 #define __i386_UACCESS_H
5 * User space memory access functions
7 #include <linux/errno.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) })
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 {
43 } ____cacheline_aligned_in_smp movsl_mask;
46 #define __addr_ok(addr) \
47 ((unsigned long __force)(addr) < \
48 (current_thread_info()->addr_limit.seg))
51 * Test whether a block of memory is a valid user space address.
52 * Returns 0 if the range is valid, nonzero otherwise.
54 * This is equivalent to the following test:
55 * (u33)addr + (u33)size >= (u33)current->addr_limit.seg
57 * This needs 33-bit arithmetic. We have a carry...
59 #define __range_ok(addr, size) \
61 unsigned long flag, roksum; \
62 __chk_user_ptr(addr); \
63 asm("addl %3,%1 ; sbbl %0,%0; cmpl %1,%4; sbbl $0,%0" \
64 :"=&r" (flag), "=r" (roksum) \
65 :"1" (addr), "g" ((int)(size)), \
66 "rm" (current_thread_info()->addr_limit.seg)); \
71 * access_ok: - Checks if a user space pointer is valid
72 * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
73 * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
74 * to write to a block, it is always safe to read from it.
75 * @addr: User space pointer to start of block to check
76 * @size: Size of block to check
78 * Context: User context only. This function may sleep.
80 * Checks if a pointer to a block of memory in user space is valid.
82 * Returns true (nonzero) if the memory block may be valid, false (zero)
83 * if it is definitely invalid.
85 * Note that, depending on architecture, this function probably just
86 * checks that the pointer is in the user space range - after calling
87 * this function, memory access functions may still return -EFAULT.
89 #define access_ok(type, addr, size) (likely(__range_ok(addr, size) == 0))
92 * The exception table consists of pairs of addresses: the first is the
93 * address of an instruction that is allowed to fault, and the second is
94 * the address at which the program should continue. No registers are
95 * modified, so it is entirely up to the continuation code to figure out
98 * All the routines below use bits of fixup code that are out of line
99 * with the main instruction path. This means when everything is well,
100 * we don't even have to jump over them. Further, they do not intrude
101 * on our cache or tlb entries.
104 struct exception_table_entry {
105 unsigned long insn, fixup;
108 extern int fixup_exception(struct pt_regs *regs);
111 * These are the main single-value transfer routines. They automatically
112 * use the right size if we just have the right pointer type.
114 * This gets kind of ugly. We want to return _two_ values in "get_user()"
115 * and yet we don't want to do any pointers, because that is too much
116 * of a performance impact. Thus we have a few rather ugly macros here,
117 * and hide all the ugliness from the user.
119 * The "__xxx" versions of the user access functions are versions that
120 * do not verify the address space, that must have been done previously
121 * with a separate "access_ok()" call (this is used when we do multiple
122 * accesses to the same area of user memory).
125 extern void __get_user_1(void);
126 extern void __get_user_2(void);
127 extern void __get_user_4(void);
129 #define __get_user_x(size, ret, x, ptr) \
130 asm volatile("call __get_user_" #size \
131 :"=a" (ret),"=d" (x) \
135 /* Careful: we have to cast the result to the type of the pointer
136 * for sign reasons */
139 * get_user: - Get a simple variable from user space.
140 * @x: Variable to store result.
141 * @ptr: Source address, in user space.
143 * Context: User context only. This function may sleep.
145 * This macro copies a single simple variable from user space to kernel
146 * space. It supports simple types like char and int, but not larger
147 * data types like structures or arrays.
149 * @ptr must have pointer-to-simple-variable type, and the result of
150 * dereferencing @ptr must be assignable to @x without a cast.
152 * Returns zero on success, or -EFAULT on error.
153 * On error, the variable @x is set to zero.
155 #define get_user(x, ptr) \
158 unsigned long __val_gu; \
159 __chk_user_ptr(ptr); \
160 switch (sizeof(*(ptr))) { \
162 __get_user_x(1, __ret_gu, __val_gu, ptr); \
165 __get_user_x(2, __ret_gu, __val_gu, ptr); \
168 __get_user_x(4, __ret_gu, __val_gu, ptr); \
171 __get_user_x(X, __ret_gu, __val_gu, ptr); \
174 (x) = (__typeof__(*(ptr)))__val_gu; \
178 extern void __put_user_bad(void);
181 * Strange magic calling convention: pointer in %ecx,
182 * value in %eax(:%edx), return value in %eax, no clobbers.
184 extern void __put_user_1(void);
185 extern void __put_user_2(void);
186 extern void __put_user_4(void);
187 extern void __put_user_8(void);
189 #define __put_user_1(x, ptr) \
190 asm volatile("call __put_user_1" : "=a" (__ret_pu) \
191 : "0" ((typeof(*(ptr)))(x)), "c" (ptr))
193 #define __put_user_2(x, ptr) \
194 asm volatile("call __put_user_2" : "=a" (__ret_pu) \
195 : "0" ((typeof(*(ptr)))(x)), "c" (ptr))
197 #define __put_user_4(x, ptr) \
198 asm volatile("call __put_user_4" : "=a" (__ret_pu) \
199 : "0" ((typeof(*(ptr)))(x)), "c" (ptr))
201 #define __put_user_8(x, ptr) \
202 asm volatile("call __put_user_8" : "=a" (__ret_pu) \
203 : "A" ((typeof(*(ptr)))(x)), "c" (ptr))
205 #define __put_user_X(x, ptr) \
206 asm volatile("call __put_user_X" : "=a" (__ret_pu) \
210 * put_user: - Write a simple value into user space.
211 * @x: Value to copy to user space.
212 * @ptr: Destination address, in user space.
214 * Context: User context only. This function may sleep.
216 * This macro copies a single simple value from kernel space to user
217 * space. It supports simple types like char and int, but not larger
218 * data types like structures or arrays.
220 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
221 * to the result of dereferencing @ptr.
223 * Returns zero on success, or -EFAULT on error.
225 #ifdef CONFIG_X86_WP_WORKS_OK
227 #define put_user(x, ptr) \
230 __typeof__(*(ptr)) __pu_val; \
231 __chk_user_ptr(ptr); \
233 switch (sizeof(*(ptr))) { \
235 __put_user_1(__pu_val, ptr); \
238 __put_user_2(__pu_val, ptr); \
241 __put_user_4(__pu_val, ptr); \
244 __put_user_8(__pu_val, ptr); \
247 __put_user_X(__pu_val, ptr); \
254 #define put_user(x, ptr) \
257 __typeof__(*(ptr))__pus_tmp = x; \
259 if (unlikely(__copy_to_user_ll(ptr, &__pus_tmp, \
260 sizeof(*(ptr))) != 0)) \
261 __ret_pu = -EFAULT; \
269 * __get_user: - Get a simple variable from user space, with less checking.
270 * @x: Variable to store result.
271 * @ptr: Source address, in user space.
273 * Context: User context only. This function may sleep.
275 * This macro copies a single simple variable from user space to kernel
276 * space. It supports simple types like char and int, but not larger
277 * data types like structures or arrays.
279 * @ptr must have pointer-to-simple-variable type, and the result of
280 * dereferencing @ptr must be assignable to @x without a cast.
282 * Caller must check the pointer with access_ok() before calling this
285 * Returns zero on success, or -EFAULT on error.
286 * On error, the variable @x is set to zero.
288 #define __get_user(x, ptr) \
289 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
293 * __put_user: - Write a simple value into user space, with less checking.
294 * @x: Value to copy to user space.
295 * @ptr: Destination address, in user space.
297 * Context: User context only. This function may sleep.
299 * This macro copies a single simple value from kernel space to user
300 * space. It supports simple types like char and int, but not larger
301 * data types like structures or arrays.
303 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
304 * to the result of dereferencing @ptr.
306 * Caller must check the pointer with access_ok() before calling this
309 * Returns zero on success, or -EFAULT on error.
311 #define __put_user(x, ptr) \
312 __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
314 #define __put_user_nocheck(x, ptr, size) \
317 __put_user_size((x), (ptr), (size), __pu_err, -EFAULT); \
322 #define __put_user_u64(x, addr, err) \
323 asm volatile("1: movl %%eax,0(%2)\n" \
324 "2: movl %%edx,4(%2)\n" \
326 ".section .fixup,\"ax\"\n" \
330 _ASM_EXTABLE(1b, 4b) \
331 _ASM_EXTABLE(2b, 4b) \
333 : "A" (x), "r" (addr), "i" (-EFAULT), "0" (err))
335 #ifdef CONFIG_X86_WP_WORKS_OK
337 #define __put_user_size(x, ptr, size, retval, errret) \
340 __chk_user_ptr(ptr); \
343 __put_user_asm(x, ptr, retval, "b", "b", "iq", errret); \
346 __put_user_asm(x, ptr, retval, "w", "w", "ir", errret); \
349 __put_user_asm(x, ptr, retval, "l", "", "ir", errret); \
352 __put_user_u64((__typeof__(*ptr))(x), ptr, retval); \
361 #define __put_user_size(x, ptr, size, retval, errret) \
363 __typeof__(*(ptr))__pus_tmp = x; \
366 if (unlikely(__copy_to_user_ll(ptr, &__pus_tmp, size) != 0)) \
371 struct __large_struct { unsigned long buf[100]; };
372 #define __m(x) (*(struct __large_struct __user *)(x))
375 * Tell gcc we read from memory instead of writing: this is because
376 * we do not write to any memory gcc knows about, so there are no
379 #define __put_user_asm(x, addr, err, itype, rtype, ltype, errret) \
380 asm volatile("1: mov"itype" %"rtype"1,%2\n" \
382 ".section .fixup,\"ax\"\n" \
386 _ASM_EXTABLE(1b, 3b) \
388 : ltype (x), "m" (__m(addr)), "i" (errret), "0" (err))
391 #define __get_user_nocheck(x, ptr, size) \
394 unsigned long __gu_val; \
395 __get_user_size(__gu_val, (ptr), (size), __gu_err, -EFAULT); \
396 (x) = (__typeof__(*(ptr)))__gu_val; \
400 extern long __get_user_bad(void);
402 #define __get_user_size(x, ptr, size, retval, errret) \
405 __chk_user_ptr(ptr); \
408 __get_user_asm(x, ptr, retval, "b", "b", "=q", errret); \
411 __get_user_asm(x, ptr, retval, "w", "w", "=r", errret); \
414 __get_user_asm(x, ptr, retval, "l", "", "=r", errret); \
417 (x) = __get_user_bad(); \
421 #define __get_user_asm(x, addr, err, itype, rtype, ltype, errret) \
422 asm volatile("1: mov"itype" %2,%"rtype"1\n" \
424 ".section .fixup,\"ax\"\n" \
426 " xor"itype" %"rtype"1,%"rtype"1\n" \
429 _ASM_EXTABLE(1b, 3b) \
430 : "=r" (err), ltype (x) \
431 : "m" (__m(addr)), "i" (errret), "0" (err))
434 unsigned long __must_check __copy_to_user_ll
435 (void __user *to, const void *from, unsigned long n);
436 unsigned long __must_check __copy_from_user_ll
437 (void *to, const void __user *from, unsigned long n);
438 unsigned long __must_check __copy_from_user_ll_nozero
439 (void *to, const void __user *from, unsigned long n);
440 unsigned long __must_check __copy_from_user_ll_nocache
441 (void *to, const void __user *from, unsigned long n);
442 unsigned long __must_check __copy_from_user_ll_nocache_nozero
443 (void *to, const void __user *from, unsigned long n);
446 * __copy_to_user_inatomic: - Copy a block of data into user space, with less checking.
447 * @to: Destination address, in user space.
448 * @from: Source address, in kernel space.
449 * @n: Number of bytes to copy.
451 * Context: User context only.
453 * Copy data from kernel space to user space. Caller must check
454 * the specified block with access_ok() before calling this function.
455 * The caller should also make sure he pins the user space address
456 * so that the we don't result in page fault and sleep.
458 * Here we special-case 1, 2 and 4-byte copy_*_user invocations. On a fault
459 * we return the initial request size (1, 2 or 4), as copy_*_user should do.
460 * If a store crosses a page boundary and gets a fault, the x86 will not write
461 * anything, so this is accurate.
464 static __always_inline unsigned long __must_check
465 __copy_to_user_inatomic(void __user *to, const void *from, unsigned long n)
467 if (__builtin_constant_p(n)) {
472 __put_user_size(*(u8 *)from, (u8 __user *)to,
476 __put_user_size(*(u16 *)from, (u16 __user *)to,
480 __put_user_size(*(u32 *)from, (u32 __user *)to,
485 return __copy_to_user_ll(to, from, n);
489 * __copy_to_user: - Copy a block of data into user space, with less checking.
490 * @to: Destination address, in user space.
491 * @from: Source address, in kernel space.
492 * @n: Number of bytes to copy.
494 * Context: User context only. This function may sleep.
496 * Copy data from kernel space to user space. Caller must check
497 * the specified block with access_ok() before calling this function.
499 * Returns number of bytes that could not be copied.
500 * On success, this will be zero.
502 static __always_inline unsigned long __must_check
503 __copy_to_user(void __user *to, const void *from, unsigned long n)
506 return __copy_to_user_inatomic(to, from, n);
509 static __always_inline unsigned long
510 __copy_from_user_inatomic(void *to, const void __user *from, unsigned long n)
512 /* Avoid zeroing the tail if the copy fails..
513 * If 'n' is constant and 1, 2, or 4, we do still zero on a failure,
514 * but as the zeroing behaviour is only significant when n is not
515 * constant, that shouldn't be a problem.
517 if (__builtin_constant_p(n)) {
522 __get_user_size(*(u8 *)to, from, 1, ret, 1);
525 __get_user_size(*(u16 *)to, from, 2, ret, 2);
528 __get_user_size(*(u32 *)to, from, 4, ret, 4);
532 return __copy_from_user_ll_nozero(to, from, n);
536 * __copy_from_user: - Copy a block of data from user space, with less checking.
537 * @to: Destination address, in kernel space.
538 * @from: Source address, in user space.
539 * @n: Number of bytes to copy.
541 * Context: User context only. This function may sleep.
543 * Copy data from user space to kernel space. Caller must check
544 * the specified block with access_ok() before calling this function.
546 * Returns number of bytes that could not be copied.
547 * On success, this will be zero.
549 * If some data could not be copied, this function will pad the copied
550 * data to the requested size using zero bytes.
552 * An alternate version - __copy_from_user_inatomic() - may be called from
553 * atomic context and will fail rather than sleep. In this case the
554 * uncopied bytes will *NOT* be padded with zeros. See fs/filemap.h
555 * for explanation of why this is needed.
557 static __always_inline unsigned long
558 __copy_from_user(void *to, const void __user *from, unsigned long n)
561 if (__builtin_constant_p(n)) {
566 __get_user_size(*(u8 *)to, from, 1, ret, 1);
569 __get_user_size(*(u16 *)to, from, 2, ret, 2);
572 __get_user_size(*(u32 *)to, from, 4, ret, 4);
576 return __copy_from_user_ll(to, from, n);
579 #define ARCH_HAS_NOCACHE_UACCESS
581 static __always_inline unsigned long __copy_from_user_nocache(void *to,
582 const void __user *from, unsigned long n)
585 if (__builtin_constant_p(n)) {
590 __get_user_size(*(u8 *)to, from, 1, ret, 1);
593 __get_user_size(*(u16 *)to, from, 2, ret, 2);
596 __get_user_size(*(u32 *)to, from, 4, ret, 4);
600 return __copy_from_user_ll_nocache(to, from, n);
603 static __always_inline unsigned long
604 __copy_from_user_inatomic_nocache(void *to, const void __user *from,
607 return __copy_from_user_ll_nocache_nozero(to, from, n);
610 unsigned long __must_check copy_to_user(void __user *to,
611 const void *from, unsigned long n);
612 unsigned long __must_check copy_from_user(void *to,
613 const void __user *from,
615 long __must_check strncpy_from_user(char *dst, const char __user *src,
617 long __must_check __strncpy_from_user(char *dst,
618 const char __user *src, long count);
621 * strlen_user: - Get the size of a string in user space.
622 * @str: The string to measure.
624 * Context: User context only. This function may sleep.
626 * Get the size of a NUL-terminated string in user space.
628 * Returns the size of the string INCLUDING the terminating NUL.
629 * On exception, returns 0.
631 * If there is a limit on the length of a valid string, you may wish to
632 * consider using strnlen_user() instead.
634 #define strlen_user(str) strnlen_user(str, LONG_MAX)
636 long strnlen_user(const char __user *str, long n);
637 unsigned long __must_check clear_user(void __user *mem, unsigned long len);
638 unsigned long __must_check __clear_user(void __user *mem, unsigned long len);
640 #endif /* __i386_UACCESS_H */