x86: merge put_user.
[linux-2.6] / include / asm-x86 / uaccess_32.h
1 #ifndef __i386_UACCESS_H
2 #define __i386_UACCESS_H
3
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>
13
14 /*
15  * movsl can be slow when source and dest are not both 8-byte aligned
16  */
17 #ifdef CONFIG_X86_INTEL_USERCOPY
18 extern struct movsl_mask {
19         int mask;
20 } ____cacheline_aligned_in_smp movsl_mask;
21 #endif
22
23 /**
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.
27  *
28  * Context: User context only.  This function may sleep.
29  *
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.
33  *
34  * @ptr must have pointer-to-simple-variable type, and the result of
35  * dereferencing @ptr must be assignable to @x without a cast.
36  *
37  * Caller must check the pointer with access_ok() before calling this
38  * function.
39  *
40  * Returns zero on success, or -EFAULT on error.
41  * On error, the variable @x is set to zero.
42  */
43 #define __get_user(x, ptr)                              \
44         __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
45
46
47 /**
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.
51  *
52  * Context: User context only.  This function may sleep.
53  *
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.
57  *
58  * @ptr must have pointer-to-simple-variable type, and @x must be assignable
59  * to the result of dereferencing @ptr.
60  *
61  * Caller must check the pointer with access_ok() before calling this
62  * function.
63  *
64  * Returns zero on success, or -EFAULT on error.
65  */
66 #define __put_user(x, ptr)                                              \
67         __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
68
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);
79
80 /**
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.
85  *
86  * Context: User context only.
87  *
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.
92  *
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.
97  */
98
99 static __always_inline unsigned long __must_check
100 __copy_to_user_inatomic(void __user *to, const void *from, unsigned long n)
101 {
102         if (__builtin_constant_p(n)) {
103                 unsigned long ret;
104
105                 switch (n) {
106                 case 1:
107                         __put_user_size(*(u8 *)from, (u8 __user *)to,
108                                         1, ret, 1);
109                         return ret;
110                 case 2:
111                         __put_user_size(*(u16 *)from, (u16 __user *)to,
112                                         2, ret, 2);
113                         return ret;
114                 case 4:
115                         __put_user_size(*(u32 *)from, (u32 __user *)to,
116                                         4, ret, 4);
117                         return ret;
118                 }
119         }
120         return __copy_to_user_ll(to, from, n);
121 }
122
123 /**
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.
128  *
129  * Context: User context only.  This function may sleep.
130  *
131  * Copy data from kernel space to user space.  Caller must check
132  * the specified block with access_ok() before calling this function.
133  *
134  * Returns number of bytes that could not be copied.
135  * On success, this will be zero.
136  */
137 static __always_inline unsigned long __must_check
138 __copy_to_user(void __user *to, const void *from, unsigned long n)
139 {
140        might_sleep();
141        return __copy_to_user_inatomic(to, from, n);
142 }
143
144 static __always_inline unsigned long
145 __copy_from_user_inatomic(void *to, const void __user *from, unsigned long n)
146 {
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.
151          */
152         if (__builtin_constant_p(n)) {
153                 unsigned long ret;
154
155                 switch (n) {
156                 case 1:
157                         __get_user_size(*(u8 *)to, from, 1, ret, 1);
158                         return ret;
159                 case 2:
160                         __get_user_size(*(u16 *)to, from, 2, ret, 2);
161                         return ret;
162                 case 4:
163                         __get_user_size(*(u32 *)to, from, 4, ret, 4);
164                         return ret;
165                 }
166         }
167         return __copy_from_user_ll_nozero(to, from, n);
168 }
169
170 /**
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.
175  *
176  * Context: User context only.  This function may sleep.
177  *
178  * Copy data from user space to kernel space.  Caller must check
179  * the specified block with access_ok() before calling this function.
180  *
181  * Returns number of bytes that could not be copied.
182  * On success, this will be zero.
183  *
184  * If some data could not be copied, this function will pad the copied
185  * data to the requested size using zero bytes.
186  *
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.
191  */
192 static __always_inline unsigned long
193 __copy_from_user(void *to, const void __user *from, unsigned long n)
194 {
195         might_sleep();
196         if (__builtin_constant_p(n)) {
197                 unsigned long ret;
198
199                 switch (n) {
200                 case 1:
201                         __get_user_size(*(u8 *)to, from, 1, ret, 1);
202                         return ret;
203                 case 2:
204                         __get_user_size(*(u16 *)to, from, 2, ret, 2);
205                         return ret;
206                 case 4:
207                         __get_user_size(*(u32 *)to, from, 4, ret, 4);
208                         return ret;
209                 }
210         }
211         return __copy_from_user_ll(to, from, n);
212 }
213
214 #define ARCH_HAS_NOCACHE_UACCESS
215
216 static __always_inline unsigned long __copy_from_user_nocache(void *to,
217                                 const void __user *from, unsigned long n)
218 {
219         might_sleep();
220         if (__builtin_constant_p(n)) {
221                 unsigned long ret;
222
223                 switch (n) {
224                 case 1:
225                         __get_user_size(*(u8 *)to, from, 1, ret, 1);
226                         return ret;
227                 case 2:
228                         __get_user_size(*(u16 *)to, from, 2, ret, 2);
229                         return ret;
230                 case 4:
231                         __get_user_size(*(u32 *)to, from, 4, ret, 4);
232                         return ret;
233                 }
234         }
235         return __copy_from_user_ll_nocache(to, from, n);
236 }
237
238 static __always_inline unsigned long
239 __copy_from_user_inatomic_nocache(void *to, const void __user *from,
240                                   unsigned long n)
241 {
242        return __copy_from_user_ll_nocache_nozero(to, from, n);
243 }
244
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,
249                                           unsigned long n);
250 long __must_check strncpy_from_user(char *dst, const char __user *src,
251                                     long count);
252 long __must_check __strncpy_from_user(char *dst,
253                                       const char __user *src, long count);
254
255 /**
256  * strlen_user: - Get the size of a string in user space.
257  * @str: The string to measure.
258  *
259  * Context: User context only.  This function may sleep.
260  *
261  * Get the size of a NUL-terminated string in user space.
262  *
263  * Returns the size of the string INCLUDING the terminating NUL.
264  * On exception, returns 0.
265  *
266  * If there is a limit on the length of a valid string, you may wish to
267  * consider using strnlen_user() instead.
268  */
269 #define strlen_user(str) strnlen_user(str, LONG_MAX)
270
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);
274
275 #endif /* __i386_UACCESS_H */