]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - include/asm-x86/uaccess_32.h
x86: merge __put_user_asm and its user.
[linux-2.6-omap-h63xx.git] / 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 extern void __put_user_bad(void);
24
25 /*
26  * Strange magic calling convention: pointer in %ecx,
27  * value in %eax(:%edx), return value in %eax, no clobbers.
28  */
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);
33
34 #define __put_user_x(size, x, ptr)                              \
35         asm volatile("call __put_user_" #size : "=a" (__ret_pu) \
36                      :"0" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx")
37
38 #define __put_user_8(x, ptr)                                    \
39         asm volatile("call __put_user_8" : "=a" (__ret_pu)      \
40                      : "A" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx")
41
42
43 /**
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.
47  *
48  * Context: User context only.  This function may sleep.
49  *
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.
53  *
54  * @ptr must have pointer-to-simple-variable type, and @x must be assignable
55  * to the result of dereferencing @ptr.
56  *
57  * Returns zero on success, or -EFAULT on error.
58  */
59 #ifdef CONFIG_X86_WP_WORKS_OK
60
61 #define put_user(x, ptr)                                        \
62 ({                                                              \
63         int __ret_pu;                                           \
64         __typeof__(*(ptr)) __pu_val;                            \
65         __chk_user_ptr(ptr);                                    \
66         __pu_val = x;                                           \
67         switch (sizeof(*(ptr))) {                               \
68         case 1:                                                 \
69                 __put_user_x(1, __pu_val, ptr);                 \
70                 break;                                          \
71         case 2:                                                 \
72                 __put_user_x(2, __pu_val, ptr);                 \
73                 break;                                          \
74         case 4:                                                 \
75                 __put_user_x(4, __pu_val, ptr);                 \
76                 break;                                          \
77         case 8:                                                 \
78                 __put_user_8(__pu_val, ptr);                    \
79                 break;                                          \
80         default:                                                \
81                 __put_user_x(X, __pu_val, ptr);                 \
82                 break;                                          \
83         }                                                       \
84         __ret_pu;                                               \
85 })
86
87 #else
88 #define put_user(x, ptr)                                        \
89 ({                                                              \
90         int __ret_pu;                                           \
91         __typeof__(*(ptr))__pus_tmp = x;                        \
92         __ret_pu = 0;                                           \
93         if (unlikely(__copy_to_user_ll(ptr, &__pus_tmp,         \
94                                        sizeof(*(ptr))) != 0))   \
95                 __ret_pu = -EFAULT;                             \
96         __ret_pu;                                               \
97 })
98
99
100 #endif
101
102 /**
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.
106  *
107  * Context: User context only.  This function may sleep.
108  *
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.
112  *
113  * @ptr must have pointer-to-simple-variable type, and the result of
114  * dereferencing @ptr must be assignable to @x without a cast.
115  *
116  * Caller must check the pointer with access_ok() before calling this
117  * function.
118  *
119  * Returns zero on success, or -EFAULT on error.
120  * On error, the variable @x is set to zero.
121  */
122 #define __get_user(x, ptr)                              \
123         __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
124
125
126 /**
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.
130  *
131  * Context: User context only.  This function may sleep.
132  *
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.
136  *
137  * @ptr must have pointer-to-simple-variable type, and @x must be assignable
138  * to the result of dereferencing @ptr.
139  *
140  * Caller must check the pointer with access_ok() before calling this
141  * function.
142  *
143  * Returns zero on success, or -EFAULT on error.
144  */
145 #define __put_user(x, ptr)                                              \
146         __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
147
148 #define __get_user_nocheck(x, ptr, size)                                \
149 ({                                                                      \
150         long __gu_err;                                                  \
151         unsigned long __gu_val;                                         \
152         __get_user_size(__gu_val, (ptr), (size), __gu_err, -EFAULT);    \
153         (x) = (__typeof__(*(ptr)))__gu_val;                             \
154         __gu_err;                                                       \
155 })
156
157 #define __get_user_size(x, ptr, size, retval, errret)                   \
158 do {                                                                    \
159         retval = 0;                                                     \
160         __chk_user_ptr(ptr);                                            \
161         switch (size) {                                                 \
162         case 1:                                                         \
163                 __get_user_asm(x, ptr, retval, "b", "b", "=q", errret); \
164                 break;                                                  \
165         case 2:                                                         \
166                 __get_user_asm(x, ptr, retval, "w", "w", "=r", errret); \
167                 break;                                                  \
168         case 4:                                                         \
169                 __get_user_asm(x, ptr, retval, "l", "", "=r", errret);  \
170                 break;                                                  \
171         default:                                                        \
172                 (x) = __get_user_bad();                                 \
173         }                                                               \
174 } while (0)
175
176 #define __get_user_asm(x, addr, err, itype, rtype, ltype, errret)       \
177         asm volatile("1:        mov"itype" %2,%"rtype"1\n"              \
178                      "2:\n"                                             \
179                      ".section .fixup,\"ax\"\n"                         \
180                      "3:        movl %3,%0\n"                           \
181                      "  xor"itype" %"rtype"1,%"rtype"1\n"               \
182                      "  jmp 2b\n"                                       \
183                      ".previous\n"                                      \
184                      _ASM_EXTABLE(1b, 3b)                               \
185                      : "=r" (err), ltype (x)                            \
186                      : "m" (__m(addr)), "i" (errret), "0" (err))
187
188
189 unsigned long __must_check __copy_to_user_ll
190                 (void __user *to, const void *from, unsigned long n);
191 unsigned long __must_check __copy_from_user_ll
192                 (void *to, const void __user *from, unsigned long n);
193 unsigned long __must_check __copy_from_user_ll_nozero
194                 (void *to, const void __user *from, unsigned long n);
195 unsigned long __must_check __copy_from_user_ll_nocache
196                 (void *to, const void __user *from, unsigned long n);
197 unsigned long __must_check __copy_from_user_ll_nocache_nozero
198                 (void *to, const void __user *from, unsigned long n);
199
200 /**
201  * __copy_to_user_inatomic: - Copy a block of data into user space, with less checking.
202  * @to:   Destination address, in user space.
203  * @from: Source address, in kernel space.
204  * @n:    Number of bytes to copy.
205  *
206  * Context: User context only.
207  *
208  * Copy data from kernel space to user space.  Caller must check
209  * the specified block with access_ok() before calling this function.
210  * The caller should also make sure he pins the user space address
211  * so that the we don't result in page fault and sleep.
212  *
213  * Here we special-case 1, 2 and 4-byte copy_*_user invocations.  On a fault
214  * we return the initial request size (1, 2 or 4), as copy_*_user should do.
215  * If a store crosses a page boundary and gets a fault, the x86 will not write
216  * anything, so this is accurate.
217  */
218
219 static __always_inline unsigned long __must_check
220 __copy_to_user_inatomic(void __user *to, const void *from, unsigned long n)
221 {
222         if (__builtin_constant_p(n)) {
223                 unsigned long ret;
224
225                 switch (n) {
226                 case 1:
227                         __put_user_size(*(u8 *)from, (u8 __user *)to,
228                                         1, ret, 1);
229                         return ret;
230                 case 2:
231                         __put_user_size(*(u16 *)from, (u16 __user *)to,
232                                         2, ret, 2);
233                         return ret;
234                 case 4:
235                         __put_user_size(*(u32 *)from, (u32 __user *)to,
236                                         4, ret, 4);
237                         return ret;
238                 }
239         }
240         return __copy_to_user_ll(to, from, n);
241 }
242
243 /**
244  * __copy_to_user: - Copy a block of data into user space, with less checking.
245  * @to:   Destination address, in user space.
246  * @from: Source address, in kernel space.
247  * @n:    Number of bytes to copy.
248  *
249  * Context: User context only.  This function may sleep.
250  *
251  * Copy data from kernel space to user space.  Caller must check
252  * the specified block with access_ok() before calling this function.
253  *
254  * Returns number of bytes that could not be copied.
255  * On success, this will be zero.
256  */
257 static __always_inline unsigned long __must_check
258 __copy_to_user(void __user *to, const void *from, unsigned long n)
259 {
260        might_sleep();
261        return __copy_to_user_inatomic(to, from, n);
262 }
263
264 static __always_inline unsigned long
265 __copy_from_user_inatomic(void *to, const void __user *from, unsigned long n)
266 {
267         /* Avoid zeroing the tail if the copy fails..
268          * If 'n' is constant and 1, 2, or 4, we do still zero on a failure,
269          * but as the zeroing behaviour is only significant when n is not
270          * constant, that shouldn't be a problem.
271          */
272         if (__builtin_constant_p(n)) {
273                 unsigned long ret;
274
275                 switch (n) {
276                 case 1:
277                         __get_user_size(*(u8 *)to, from, 1, ret, 1);
278                         return ret;
279                 case 2:
280                         __get_user_size(*(u16 *)to, from, 2, ret, 2);
281                         return ret;
282                 case 4:
283                         __get_user_size(*(u32 *)to, from, 4, ret, 4);
284                         return ret;
285                 }
286         }
287         return __copy_from_user_ll_nozero(to, from, n);
288 }
289
290 /**
291  * __copy_from_user: - Copy a block of data from user space, with less checking.
292  * @to:   Destination address, in kernel space.
293  * @from: Source address, in user space.
294  * @n:    Number of bytes to copy.
295  *
296  * Context: User context only.  This function may sleep.
297  *
298  * Copy data from user space to kernel space.  Caller must check
299  * the specified block with access_ok() before calling this function.
300  *
301  * Returns number of bytes that could not be copied.
302  * On success, this will be zero.
303  *
304  * If some data could not be copied, this function will pad the copied
305  * data to the requested size using zero bytes.
306  *
307  * An alternate version - __copy_from_user_inatomic() - may be called from
308  * atomic context and will fail rather than sleep.  In this case the
309  * uncopied bytes will *NOT* be padded with zeros.  See fs/filemap.h
310  * for explanation of why this is needed.
311  */
312 static __always_inline unsigned long
313 __copy_from_user(void *to, const void __user *from, unsigned long n)
314 {
315         might_sleep();
316         if (__builtin_constant_p(n)) {
317                 unsigned long ret;
318
319                 switch (n) {
320                 case 1:
321                         __get_user_size(*(u8 *)to, from, 1, ret, 1);
322                         return ret;
323                 case 2:
324                         __get_user_size(*(u16 *)to, from, 2, ret, 2);
325                         return ret;
326                 case 4:
327                         __get_user_size(*(u32 *)to, from, 4, ret, 4);
328                         return ret;
329                 }
330         }
331         return __copy_from_user_ll(to, from, n);
332 }
333
334 #define ARCH_HAS_NOCACHE_UACCESS
335
336 static __always_inline unsigned long __copy_from_user_nocache(void *to,
337                                 const void __user *from, unsigned long n)
338 {
339         might_sleep();
340         if (__builtin_constant_p(n)) {
341                 unsigned long ret;
342
343                 switch (n) {
344                 case 1:
345                         __get_user_size(*(u8 *)to, from, 1, ret, 1);
346                         return ret;
347                 case 2:
348                         __get_user_size(*(u16 *)to, from, 2, ret, 2);
349                         return ret;
350                 case 4:
351                         __get_user_size(*(u32 *)to, from, 4, ret, 4);
352                         return ret;
353                 }
354         }
355         return __copy_from_user_ll_nocache(to, from, n);
356 }
357
358 static __always_inline unsigned long
359 __copy_from_user_inatomic_nocache(void *to, const void __user *from,
360                                   unsigned long n)
361 {
362        return __copy_from_user_ll_nocache_nozero(to, from, n);
363 }
364
365 unsigned long __must_check copy_to_user(void __user *to,
366                                         const void *from, unsigned long n);
367 unsigned long __must_check copy_from_user(void *to,
368                                           const void __user *from,
369                                           unsigned long n);
370 long __must_check strncpy_from_user(char *dst, const char __user *src,
371                                     long count);
372 long __must_check __strncpy_from_user(char *dst,
373                                       const char __user *src, long count);
374
375 /**
376  * strlen_user: - Get the size of a string in user space.
377  * @str: The string to measure.
378  *
379  * Context: User context only.  This function may sleep.
380  *
381  * Get the size of a NUL-terminated string in user space.
382  *
383  * Returns the size of the string INCLUDING the terminating NUL.
384  * On exception, returns 0.
385  *
386  * If there is a limit on the length of a valid string, you may wish to
387  * consider using strnlen_user() instead.
388  */
389 #define strlen_user(str) strnlen_user(str, LONG_MAX)
390
391 long strnlen_user(const char __user *str, long n);
392 unsigned long __must_check clear_user(void __user *mem, unsigned long len);
393 unsigned long __must_check __clear_user(void __user *mem, unsigned long len);
394
395 #endif /* __i386_UACCESS_H */