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 * movsl can be slow when source and dest are not both 8-byte aligned
17 #ifdef CONFIG_X86_INTEL_USERCOPY
18 extern struct movsl_mask {
20 } ____cacheline_aligned_in_smp movsl_mask;
24 * __get_user: - Get a simple variable from user space, with less checking.
25 * @x: Variable to store result.
26 * @ptr: Source address, in user space.
28 * Context: User context only. This function may sleep.
30 * This macro copies a single simple variable from user space to kernel
31 * space. It supports simple types like char and int, but not larger
32 * data types like structures or arrays.
34 * @ptr must have pointer-to-simple-variable type, and the result of
35 * dereferencing @ptr must be assignable to @x without a cast.
37 * Caller must check the pointer with access_ok() before calling this
40 * Returns zero on success, or -EFAULT on error.
41 * On error, the variable @x is set to zero.
43 #define __get_user(x, ptr) \
44 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
48 * __put_user: - Write a simple value into user space, with less checking.
49 * @x: Value to copy to user space.
50 * @ptr: Destination address, in user space.
52 * Context: User context only. This function may sleep.
54 * This macro copies a single simple value from kernel space to user
55 * space. It supports simple types like char and int, but not larger
56 * data types like structures or arrays.
58 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
59 * to the result of dereferencing @ptr.
61 * Caller must check the pointer with access_ok() before calling this
64 * Returns zero on success, or -EFAULT on error.
66 #define __put_user(x, ptr) \
67 __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
69 unsigned long __must_check __copy_to_user_ll
70 (void __user *to, const void *from, unsigned long n);
71 unsigned long __must_check __copy_from_user_ll
72 (void *to, const void __user *from, unsigned long n);
73 unsigned long __must_check __copy_from_user_ll_nozero
74 (void *to, const void __user *from, unsigned long n);
75 unsigned long __must_check __copy_from_user_ll_nocache
76 (void *to, const void __user *from, unsigned long n);
77 unsigned long __must_check __copy_from_user_ll_nocache_nozero
78 (void *to, const void __user *from, unsigned long n);
81 * __copy_to_user_inatomic: - Copy a block of data into user space, with less checking.
82 * @to: Destination address, in user space.
83 * @from: Source address, in kernel space.
84 * @n: Number of bytes to copy.
86 * Context: User context only.
88 * Copy data from kernel space to user space. Caller must check
89 * the specified block with access_ok() before calling this function.
90 * The caller should also make sure he pins the user space address
91 * so that the we don't result in page fault and sleep.
93 * Here we special-case 1, 2 and 4-byte copy_*_user invocations. On a fault
94 * we return the initial request size (1, 2 or 4), as copy_*_user should do.
95 * If a store crosses a page boundary and gets a fault, the x86 will not write
96 * anything, so this is accurate.
99 static __always_inline unsigned long __must_check
100 __copy_to_user_inatomic(void __user *to, const void *from, unsigned long n)
102 if (__builtin_constant_p(n)) {
107 __put_user_size(*(u8 *)from, (u8 __user *)to,
111 __put_user_size(*(u16 *)from, (u16 __user *)to,
115 __put_user_size(*(u32 *)from, (u32 __user *)to,
120 return __copy_to_user_ll(to, from, n);
124 * __copy_to_user: - Copy a block of data into user space, with less checking.
125 * @to: Destination address, in user space.
126 * @from: Source address, in kernel space.
127 * @n: Number of bytes to copy.
129 * Context: User context only. This function may sleep.
131 * Copy data from kernel space to user space. Caller must check
132 * the specified block with access_ok() before calling this function.
134 * Returns number of bytes that could not be copied.
135 * On success, this will be zero.
137 static __always_inline unsigned long __must_check
138 __copy_to_user(void __user *to, const void *from, unsigned long n)
141 return __copy_to_user_inatomic(to, from, n);
144 static __always_inline unsigned long
145 __copy_from_user_inatomic(void *to, const void __user *from, unsigned long n)
147 /* Avoid zeroing the tail if the copy fails..
148 * If 'n' is constant and 1, 2, or 4, we do still zero on a failure,
149 * but as the zeroing behaviour is only significant when n is not
150 * constant, that shouldn't be a problem.
152 if (__builtin_constant_p(n)) {
157 __get_user_size(*(u8 *)to, from, 1, ret, 1);
160 __get_user_size(*(u16 *)to, from, 2, ret, 2);
163 __get_user_size(*(u32 *)to, from, 4, ret, 4);
167 return __copy_from_user_ll_nozero(to, from, n);
171 * __copy_from_user: - Copy a block of data from user space, with less checking.
172 * @to: Destination address, in kernel space.
173 * @from: Source address, in user space.
174 * @n: Number of bytes to copy.
176 * Context: User context only. This function may sleep.
178 * Copy data from user space to kernel space. Caller must check
179 * the specified block with access_ok() before calling this function.
181 * Returns number of bytes that could not be copied.
182 * On success, this will be zero.
184 * If some data could not be copied, this function will pad the copied
185 * data to the requested size using zero bytes.
187 * An alternate version - __copy_from_user_inatomic() - may be called from
188 * atomic context and will fail rather than sleep. In this case the
189 * uncopied bytes will *NOT* be padded with zeros. See fs/filemap.h
190 * for explanation of why this is needed.
192 static __always_inline unsigned long
193 __copy_from_user(void *to, const void __user *from, unsigned long n)
196 if (__builtin_constant_p(n)) {
201 __get_user_size(*(u8 *)to, from, 1, ret, 1);
204 __get_user_size(*(u16 *)to, from, 2, ret, 2);
207 __get_user_size(*(u32 *)to, from, 4, ret, 4);
211 return __copy_from_user_ll(to, from, n);
214 #define ARCH_HAS_NOCACHE_UACCESS
216 static __always_inline unsigned long __copy_from_user_nocache(void *to,
217 const void __user *from, unsigned long n)
220 if (__builtin_constant_p(n)) {
225 __get_user_size(*(u8 *)to, from, 1, ret, 1);
228 __get_user_size(*(u16 *)to, from, 2, ret, 2);
231 __get_user_size(*(u32 *)to, from, 4, ret, 4);
235 return __copy_from_user_ll_nocache(to, from, n);
238 static __always_inline unsigned long
239 __copy_from_user_inatomic_nocache(void *to, const void __user *from,
242 return __copy_from_user_ll_nocache_nozero(to, from, n);
245 unsigned long __must_check copy_to_user(void __user *to,
246 const void *from, unsigned long n);
247 unsigned long __must_check copy_from_user(void *to,
248 const void __user *from,
250 long __must_check strncpy_from_user(char *dst, const char __user *src,
252 long __must_check __strncpy_from_user(char *dst,
253 const char __user *src, long count);
256 * strlen_user: - Get the size of a string in user space.
257 * @str: The string to measure.
259 * Context: User context only. This function may sleep.
261 * Get the size of a NUL-terminated string in user space.
263 * Returns the size of the string INCLUDING the terminating NUL.
264 * On exception, returns 0.
266 * If there is a limit on the length of a valid string, you may wish to
267 * consider using strnlen_user() instead.
269 #define strlen_user(str) strnlen_user(str, LONG_MAX)
271 long strnlen_user(const char __user *str, long n);
272 unsigned long __must_check clear_user(void __user *mem, unsigned long len);
273 unsigned long __must_check __clear_user(void __user *mem, unsigned long len);
275 #endif /* __i386_UACCESS_H */