]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - include/asm-x86/uaccess_32.h
x86: move __get_user and __put_user into uaccess.h.
[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 unsigned long __must_check __copy_to_user_ll
24                 (void __user *to, const void *from, unsigned long n);
25 unsigned long __must_check __copy_from_user_ll
26                 (void *to, const void __user *from, unsigned long n);
27 unsigned long __must_check __copy_from_user_ll_nozero
28                 (void *to, const void __user *from, unsigned long n);
29 unsigned long __must_check __copy_from_user_ll_nocache
30                 (void *to, const void __user *from, unsigned long n);
31 unsigned long __must_check __copy_from_user_ll_nocache_nozero
32                 (void *to, const void __user *from, unsigned long n);
33
34 /**
35  * __copy_to_user_inatomic: - Copy a block of data into user space, with less checking.
36  * @to:   Destination address, in user space.
37  * @from: Source address, in kernel space.
38  * @n:    Number of bytes to copy.
39  *
40  * Context: User context only.
41  *
42  * Copy data from kernel space to user space.  Caller must check
43  * the specified block with access_ok() before calling this function.
44  * The caller should also make sure he pins the user space address
45  * so that the we don't result in page fault and sleep.
46  *
47  * Here we special-case 1, 2 and 4-byte copy_*_user invocations.  On a fault
48  * we return the initial request size (1, 2 or 4), as copy_*_user should do.
49  * If a store crosses a page boundary and gets a fault, the x86 will not write
50  * anything, so this is accurate.
51  */
52
53 static __always_inline unsigned long __must_check
54 __copy_to_user_inatomic(void __user *to, const void *from, unsigned long n)
55 {
56         if (__builtin_constant_p(n)) {
57                 unsigned long ret;
58
59                 switch (n) {
60                 case 1:
61                         __put_user_size(*(u8 *)from, (u8 __user *)to,
62                                         1, ret, 1);
63                         return ret;
64                 case 2:
65                         __put_user_size(*(u16 *)from, (u16 __user *)to,
66                                         2, ret, 2);
67                         return ret;
68                 case 4:
69                         __put_user_size(*(u32 *)from, (u32 __user *)to,
70                                         4, ret, 4);
71                         return ret;
72                 }
73         }
74         return __copy_to_user_ll(to, from, n);
75 }
76
77 /**
78  * __copy_to_user: - Copy a block of data into user space, with less checking.
79  * @to:   Destination address, in user space.
80  * @from: Source address, in kernel space.
81  * @n:    Number of bytes to copy.
82  *
83  * Context: User context only.  This function may sleep.
84  *
85  * Copy data from kernel space to user space.  Caller must check
86  * the specified block with access_ok() before calling this function.
87  *
88  * Returns number of bytes that could not be copied.
89  * On success, this will be zero.
90  */
91 static __always_inline unsigned long __must_check
92 __copy_to_user(void __user *to, const void *from, unsigned long n)
93 {
94        might_sleep();
95        return __copy_to_user_inatomic(to, from, n);
96 }
97
98 static __always_inline unsigned long
99 __copy_from_user_inatomic(void *to, const void __user *from, unsigned long n)
100 {
101         /* Avoid zeroing the tail if the copy fails..
102          * If 'n' is constant and 1, 2, or 4, we do still zero on a failure,
103          * but as the zeroing behaviour is only significant when n is not
104          * constant, that shouldn't be a problem.
105          */
106         if (__builtin_constant_p(n)) {
107                 unsigned long ret;
108
109                 switch (n) {
110                 case 1:
111                         __get_user_size(*(u8 *)to, from, 1, ret, 1);
112                         return ret;
113                 case 2:
114                         __get_user_size(*(u16 *)to, from, 2, ret, 2);
115                         return ret;
116                 case 4:
117                         __get_user_size(*(u32 *)to, from, 4, ret, 4);
118                         return ret;
119                 }
120         }
121         return __copy_from_user_ll_nozero(to, from, n);
122 }
123
124 /**
125  * __copy_from_user: - Copy a block of data from user space, with less checking.
126  * @to:   Destination address, in kernel space.
127  * @from: Source address, in user space.
128  * @n:    Number of bytes to copy.
129  *
130  * Context: User context only.  This function may sleep.
131  *
132  * Copy data from user space to kernel space.  Caller must check
133  * the specified block with access_ok() before calling this function.
134  *
135  * Returns number of bytes that could not be copied.
136  * On success, this will be zero.
137  *
138  * If some data could not be copied, this function will pad the copied
139  * data to the requested size using zero bytes.
140  *
141  * An alternate version - __copy_from_user_inatomic() - may be called from
142  * atomic context and will fail rather than sleep.  In this case the
143  * uncopied bytes will *NOT* be padded with zeros.  See fs/filemap.h
144  * for explanation of why this is needed.
145  */
146 static __always_inline unsigned long
147 __copy_from_user(void *to, const void __user *from, unsigned long n)
148 {
149         might_sleep();
150         if (__builtin_constant_p(n)) {
151                 unsigned long ret;
152
153                 switch (n) {
154                 case 1:
155                         __get_user_size(*(u8 *)to, from, 1, ret, 1);
156                         return ret;
157                 case 2:
158                         __get_user_size(*(u16 *)to, from, 2, ret, 2);
159                         return ret;
160                 case 4:
161                         __get_user_size(*(u32 *)to, from, 4, ret, 4);
162                         return ret;
163                 }
164         }
165         return __copy_from_user_ll(to, from, n);
166 }
167
168 #define ARCH_HAS_NOCACHE_UACCESS
169
170 static __always_inline unsigned long __copy_from_user_nocache(void *to,
171                                 const void __user *from, unsigned long n)
172 {
173         might_sleep();
174         if (__builtin_constant_p(n)) {
175                 unsigned long ret;
176
177                 switch (n) {
178                 case 1:
179                         __get_user_size(*(u8 *)to, from, 1, ret, 1);
180                         return ret;
181                 case 2:
182                         __get_user_size(*(u16 *)to, from, 2, ret, 2);
183                         return ret;
184                 case 4:
185                         __get_user_size(*(u32 *)to, from, 4, ret, 4);
186                         return ret;
187                 }
188         }
189         return __copy_from_user_ll_nocache(to, from, n);
190 }
191
192 static __always_inline unsigned long
193 __copy_from_user_inatomic_nocache(void *to, const void __user *from,
194                                   unsigned long n)
195 {
196        return __copy_from_user_ll_nocache_nozero(to, from, n);
197 }
198
199 unsigned long __must_check copy_to_user(void __user *to,
200                                         const void *from, unsigned long n);
201 unsigned long __must_check copy_from_user(void *to,
202                                           const void __user *from,
203                                           unsigned long n);
204 long __must_check strncpy_from_user(char *dst, const char __user *src,
205                                     long count);
206 long __must_check __strncpy_from_user(char *dst,
207                                       const char __user *src, long count);
208
209 /**
210  * strlen_user: - Get the size of a string in user space.
211  * @str: The string to measure.
212  *
213  * Context: User context only.  This function may sleep.
214  *
215  * Get the size of a NUL-terminated string in user space.
216  *
217  * Returns the size of the string INCLUDING the terminating NUL.
218  * On exception, returns 0.
219  *
220  * If there is a limit on the length of a valid string, you may wish to
221  * consider using strnlen_user() instead.
222  */
223 #define strlen_user(str) strnlen_user(str, LONG_MAX)
224
225 long strnlen_user(const char __user *str, long n);
226 unsigned long __must_check clear_user(void __user *mem, unsigned long len);
227 unsigned long __must_check __clear_user(void __user *mem, unsigned long len);
228
229 #endif /* __i386_UACCESS_H */