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;
23 extern void __put_user_bad(void);
26 * Strange magic calling convention: pointer in %ecx,
27 * value in %eax(:%edx), return value in %eax, no clobbers.
29 extern void __put_user_1(void);
30 extern void __put_user_2(void);
31 extern void __put_user_4(void);
32 extern void __put_user_8(void);
34 #define __put_user_x(size, x, ptr, __ret_pu) \
35 asm volatile("call __put_user_" #size : "=a" (__ret_pu) \
36 :"0" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx")
38 #define __put_user_8(x, ptr, __ret_pu) \
39 asm volatile("call __put_user_8" : "=a" (__ret_pu) \
40 : "A" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx")
44 * put_user: - Write a simple value into user space.
45 * @x: Value to copy to user space.
46 * @ptr: Destination address, in user space.
48 * Context: User context only. This function may sleep.
50 * This macro copies a single simple value from kernel space to user
51 * space. It supports simple types like char and int, but not larger
52 * data types like structures or arrays.
54 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
55 * to the result of dereferencing @ptr.
57 * Returns zero on success, or -EFAULT on error.
59 #ifdef CONFIG_X86_WP_WORKS_OK
61 #define put_user(x, ptr) \
64 __typeof__(*(ptr)) __pu_val; \
65 __chk_user_ptr(ptr); \
67 switch (sizeof(*(ptr))) { \
69 __put_user_x(1, __pu_val, ptr, __ret_pu); \
72 __put_user_x(2, __pu_val, ptr, __ret_pu); \
75 __put_user_x(4, __pu_val, ptr, __ret_pu); \
78 __put_user_8(__pu_val, ptr, __ret_pu); \
81 __put_user_x(X, __pu_val, ptr, __ret_pu); \
88 #define put_user(x, ptr) \
91 __typeof__(*(ptr))__pus_tmp = x; \
93 if (unlikely(__copy_to_user_ll(ptr, &__pus_tmp, \
94 sizeof(*(ptr))) != 0)) \
103 * __get_user: - Get a simple variable from user space, with less checking.
104 * @x: Variable to store result.
105 * @ptr: Source address, in user space.
107 * Context: User context only. This function may sleep.
109 * This macro copies a single simple variable from user space to kernel
110 * space. It supports simple types like char and int, but not larger
111 * data types like structures or arrays.
113 * @ptr must have pointer-to-simple-variable type, and the result of
114 * dereferencing @ptr must be assignable to @x without a cast.
116 * Caller must check the pointer with access_ok() before calling this
119 * Returns zero on success, or -EFAULT on error.
120 * On error, the variable @x is set to zero.
122 #define __get_user(x, ptr) \
123 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
127 * __put_user: - Write a simple value into user space, with less checking.
128 * @x: Value to copy to user space.
129 * @ptr: Destination address, in user space.
131 * Context: User context only. This function may sleep.
133 * This macro copies a single simple value from kernel space to user
134 * space. It supports simple types like char and int, but not larger
135 * data types like structures or arrays.
137 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
138 * to the result of dereferencing @ptr.
140 * Caller must check the pointer with access_ok() before calling this
143 * Returns zero on success, or -EFAULT on error.
145 #define __put_user(x, ptr) \
146 __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
148 unsigned long __must_check __copy_to_user_ll
149 (void __user *to, const void *from, unsigned long n);
150 unsigned long __must_check __copy_from_user_ll
151 (void *to, const void __user *from, unsigned long n);
152 unsigned long __must_check __copy_from_user_ll_nozero
153 (void *to, const void __user *from, unsigned long n);
154 unsigned long __must_check __copy_from_user_ll_nocache
155 (void *to, const void __user *from, unsigned long n);
156 unsigned long __must_check __copy_from_user_ll_nocache_nozero
157 (void *to, const void __user *from, unsigned long n);
160 * __copy_to_user_inatomic: - Copy a block of data into user space, with less checking.
161 * @to: Destination address, in user space.
162 * @from: Source address, in kernel space.
163 * @n: Number of bytes to copy.
165 * Context: User context only.
167 * Copy data from kernel space to user space. Caller must check
168 * the specified block with access_ok() before calling this function.
169 * The caller should also make sure he pins the user space address
170 * so that the we don't result in page fault and sleep.
172 * Here we special-case 1, 2 and 4-byte copy_*_user invocations. On a fault
173 * we return the initial request size (1, 2 or 4), as copy_*_user should do.
174 * If a store crosses a page boundary and gets a fault, the x86 will not write
175 * anything, so this is accurate.
178 static __always_inline unsigned long __must_check
179 __copy_to_user_inatomic(void __user *to, const void *from, unsigned long n)
181 if (__builtin_constant_p(n)) {
186 __put_user_size(*(u8 *)from, (u8 __user *)to,
190 __put_user_size(*(u16 *)from, (u16 __user *)to,
194 __put_user_size(*(u32 *)from, (u32 __user *)to,
199 return __copy_to_user_ll(to, from, n);
203 * __copy_to_user: - Copy a block of data into user space, with less checking.
204 * @to: Destination address, in user space.
205 * @from: Source address, in kernel space.
206 * @n: Number of bytes to copy.
208 * Context: User context only. This function may sleep.
210 * Copy data from kernel space to user space. Caller must check
211 * the specified block with access_ok() before calling this function.
213 * Returns number of bytes that could not be copied.
214 * On success, this will be zero.
216 static __always_inline unsigned long __must_check
217 __copy_to_user(void __user *to, const void *from, unsigned long n)
220 return __copy_to_user_inatomic(to, from, n);
223 static __always_inline unsigned long
224 __copy_from_user_inatomic(void *to, const void __user *from, unsigned long n)
226 /* Avoid zeroing the tail if the copy fails..
227 * If 'n' is constant and 1, 2, or 4, we do still zero on a failure,
228 * but as the zeroing behaviour is only significant when n is not
229 * constant, that shouldn't be a problem.
231 if (__builtin_constant_p(n)) {
236 __get_user_size(*(u8 *)to, from, 1, ret, 1);
239 __get_user_size(*(u16 *)to, from, 2, ret, 2);
242 __get_user_size(*(u32 *)to, from, 4, ret, 4);
246 return __copy_from_user_ll_nozero(to, from, n);
250 * __copy_from_user: - Copy a block of data from user space, with less checking.
251 * @to: Destination address, in kernel space.
252 * @from: Source address, in user space.
253 * @n: Number of bytes to copy.
255 * Context: User context only. This function may sleep.
257 * Copy data from user space to kernel space. Caller must check
258 * the specified block with access_ok() before calling this function.
260 * Returns number of bytes that could not be copied.
261 * On success, this will be zero.
263 * If some data could not be copied, this function will pad the copied
264 * data to the requested size using zero bytes.
266 * An alternate version - __copy_from_user_inatomic() - may be called from
267 * atomic context and will fail rather than sleep. In this case the
268 * uncopied bytes will *NOT* be padded with zeros. See fs/filemap.h
269 * for explanation of why this is needed.
271 static __always_inline unsigned long
272 __copy_from_user(void *to, const void __user *from, unsigned long n)
275 if (__builtin_constant_p(n)) {
280 __get_user_size(*(u8 *)to, from, 1, ret, 1);
283 __get_user_size(*(u16 *)to, from, 2, ret, 2);
286 __get_user_size(*(u32 *)to, from, 4, ret, 4);
290 return __copy_from_user_ll(to, from, n);
293 #define ARCH_HAS_NOCACHE_UACCESS
295 static __always_inline unsigned long __copy_from_user_nocache(void *to,
296 const void __user *from, unsigned long n)
299 if (__builtin_constant_p(n)) {
304 __get_user_size(*(u8 *)to, from, 1, ret, 1);
307 __get_user_size(*(u16 *)to, from, 2, ret, 2);
310 __get_user_size(*(u32 *)to, from, 4, ret, 4);
314 return __copy_from_user_ll_nocache(to, from, n);
317 static __always_inline unsigned long
318 __copy_from_user_inatomic_nocache(void *to, const void __user *from,
321 return __copy_from_user_ll_nocache_nozero(to, from, n);
324 unsigned long __must_check copy_to_user(void __user *to,
325 const void *from, unsigned long n);
326 unsigned long __must_check copy_from_user(void *to,
327 const void __user *from,
329 long __must_check strncpy_from_user(char *dst, const char __user *src,
331 long __must_check __strncpy_from_user(char *dst,
332 const char __user *src, long count);
335 * strlen_user: - Get the size of a string in user space.
336 * @str: The string to measure.
338 * Context: User context only. This function may sleep.
340 * Get the size of a NUL-terminated string in user space.
342 * Returns the size of the string INCLUDING the terminating NUL.
343 * On exception, returns 0.
345 * If there is a limit on the length of a valid string, you may wish to
346 * consider using strnlen_user() instead.
348 #define strlen_user(str) strnlen_user(str, LONG_MAX)
350 long strnlen_user(const char __user *str, long n);
351 unsigned long __must_check clear_user(void __user *mem, unsigned long len);
352 unsigned long __must_check __clear_user(void __user *mem, unsigned long len);
354 #endif /* __i386_UACCESS_H */