x86/uaccess: Use __uaccess_begin_nospec() and uaccess_try_nospec
[pandora-kernel.git] / arch / x86 / include / asm / uaccess_32.h
1 #ifndef _ASM_X86_UACCESS_32_H
2 #define _ASM_X86_UACCESS_32_H
3
4 /*
5  * User space memory access functions
6  */
7 #include <linux/errno.h>
8 #include <linux/thread_info.h>
9 #include <linux/string.h>
10 #include <asm/asm.h>
11 #include <asm/page.h>
12
13 unsigned long __must_check __copy_to_user_ll
14                 (void __user *to, const void *from, unsigned long n);
15 unsigned long __must_check __copy_from_user_ll
16                 (void *to, const void __user *from, unsigned long n);
17 unsigned long __must_check __copy_from_user_ll_nozero
18                 (void *to, const void __user *from, unsigned long n);
19 unsigned long __must_check __copy_from_user_ll_nocache
20                 (void *to, const void __user *from, unsigned long n);
21 unsigned long __must_check __copy_from_user_ll_nocache_nozero
22                 (void *to, const void __user *from, unsigned long n);
23
24 /**
25  * __copy_to_user_inatomic: - Copy a block of data into user space, with less checking.
26  * @to:   Destination address, in user space.
27  * @from: Source address, in kernel space.
28  * @n:    Number of bytes to copy.
29  *
30  * Context: User context only.
31  *
32  * Copy data from kernel space to user space.  Caller must check
33  * the specified block with access_ok() before calling this function.
34  * The caller should also make sure he pins the user space address
35  * so that we don't result in page fault and sleep.
36  *
37  * Here we special-case 1, 2 and 4-byte copy_*_user invocations.  On a fault
38  * we return the initial request size (1, 2 or 4), as copy_*_user should do.
39  * If a store crosses a page boundary and gets a fault, the x86 will not write
40  * anything, so this is accurate.
41  */
42
43 static __always_inline unsigned long __must_check
44 __copy_to_user_inatomic(void __user *to, const void *from, unsigned long n)
45 {
46         if (__builtin_constant_p(n)) {
47                 unsigned long ret;
48
49                 switch (n) {
50                 case 1:
51                         barrier_nospec();
52                         __put_user_size(*(u8 *)from, (u8 __user *)to,
53                                         1, ret, 1);
54                         return ret;
55                 case 2:
56                         barrier_nospec();
57                         __put_user_size(*(u16 *)from, (u16 __user *)to,
58                                         2, ret, 2);
59                         return ret;
60                 case 4:
61                         barrier_nospec();
62                         __put_user_size(*(u32 *)from, (u32 __user *)to,
63                                         4, ret, 4);
64                         return ret;
65                 }
66         }
67         return __copy_to_user_ll(to, from, n);
68 }
69
70 /**
71  * __copy_to_user: - Copy a block of data into user space, with less checking.
72  * @to:   Destination address, in user space.
73  * @from: Source address, in kernel space.
74  * @n:    Number of bytes to copy.
75  *
76  * Context: User context only.  This function may sleep.
77  *
78  * Copy data from kernel space to user space.  Caller must check
79  * the specified block with access_ok() before calling this function.
80  *
81  * Returns number of bytes that could not be copied.
82  * On success, this will be zero.
83  */
84 static __always_inline unsigned long __must_check
85 __copy_to_user(void __user *to, const void *from, unsigned long n)
86 {
87         might_fault();
88         return __copy_to_user_inatomic(to, from, n);
89 }
90
91 static __always_inline unsigned long
92 __copy_from_user_inatomic(void *to, const void __user *from, unsigned long n)
93 {
94         /* Avoid zeroing the tail if the copy fails..
95          * If 'n' is constant and 1, 2, or 4, we do still zero on a failure,
96          * but as the zeroing behaviour is only significant when n is not
97          * constant, that shouldn't be a problem.
98          */
99         if (__builtin_constant_p(n)) {
100                 unsigned long ret;
101
102                 switch (n) {
103                 case 1:
104                         barrier_nospec();
105                         __get_user_size(*(u8 *)to, from, 1, ret, 1);
106                         return ret;
107                 case 2:
108                         barrier_nospec();
109                         __get_user_size(*(u16 *)to, from, 2, ret, 2);
110                         return ret;
111                 case 4:
112                         barrier_nospec();
113                         __get_user_size(*(u32 *)to, from, 4, ret, 4);
114                         return ret;
115                 }
116         }
117         return __copy_from_user_ll_nozero(to, from, n);
118 }
119
120 /**
121  * __copy_from_user: - Copy a block of data from user space, with less checking.
122  * @to:   Destination address, in kernel space.
123  * @from: Source address, in user space.
124  * @n:    Number of bytes to copy.
125  *
126  * Context: User context only.  This function may sleep.
127  *
128  * Copy data from user space to kernel space.  Caller must check
129  * the specified block with access_ok() before calling this function.
130  *
131  * Returns number of bytes that could not be copied.
132  * On success, this will be zero.
133  *
134  * If some data could not be copied, this function will pad the copied
135  * data to the requested size using zero bytes.
136  *
137  * An alternate version - __copy_from_user_inatomic() - may be called from
138  * atomic context and will fail rather than sleep.  In this case the
139  * uncopied bytes will *NOT* be padded with zeros.  See fs/filemap.h
140  * for explanation of why this is needed.
141  */
142 static __always_inline unsigned long
143 __copy_from_user(void *to, const void __user *from, unsigned long n)
144 {
145         might_fault();
146         if (__builtin_constant_p(n)) {
147                 unsigned long ret;
148
149                 switch (n) {
150                 case 1:
151                         barrier_nospec();
152                         __get_user_size(*(u8 *)to, from, 1, ret, 1);
153                         return ret;
154                 case 2:
155                         barrier_nospec();
156                         __get_user_size(*(u16 *)to, from, 2, ret, 2);
157                         return ret;
158                 case 4:
159                         barrier_nospec();
160                         __get_user_size(*(u32 *)to, from, 4, ret, 4);
161                         return ret;
162                 }
163         }
164         return __copy_from_user_ll(to, from, n);
165 }
166
167 static __always_inline unsigned long __copy_from_user_nocache(void *to,
168                                 const void __user *from, unsigned long n)
169 {
170         might_fault();
171         if (__builtin_constant_p(n)) {
172                 unsigned long ret;
173
174                 switch (n) {
175                 case 1:
176                         barrier_nospec();
177                         __get_user_size(*(u8 *)to, from, 1, ret, 1);
178                         return ret;
179                 case 2:
180                         barrier_nospec();
181                         __get_user_size(*(u16 *)to, from, 2, ret, 2);
182                         return ret;
183                 case 4:
184                         barrier_nospec();
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
205
206 extern void copy_from_user_overflow(void)
207 #ifdef CONFIG_DEBUG_STRICT_USER_COPY_CHECKS
208         __compiletime_error("copy_from_user() buffer size is not provably correct")
209 #else
210         __compiletime_warning("copy_from_user() buffer size is not provably correct")
211 #endif
212 ;
213
214 static inline unsigned long __must_check copy_from_user(void *to,
215                                           const void __user *from,
216                                           unsigned long n)
217 {
218         int sz = __compiletime_object_size(to);
219
220         if (likely(sz == -1 || sz >= n))
221                 n = _copy_from_user(to, from, n);
222         else
223                 copy_from_user_overflow();
224
225         return n;
226 }
227
228 long __must_check strncpy_from_user(char *dst, const char __user *src,
229                                     long count);
230 long __must_check __strncpy_from_user(char *dst,
231                                       const char __user *src, long count);
232
233 /**
234  * strlen_user: - Get the size of a string in user space.
235  * @str: The string to measure.
236  *
237  * Context: User context only.  This function may sleep.
238  *
239  * Get the size of a NUL-terminated string in user space.
240  *
241  * Returns the size of the string INCLUDING the terminating NUL.
242  * On exception, returns 0.
243  *
244  * If there is a limit on the length of a valid string, you may wish to
245  * consider using strnlen_user() instead.
246  */
247 #define strlen_user(str) strnlen_user(str, LONG_MAX)
248
249 long strnlen_user(const char __user *str, long n);
250 unsigned long __must_check clear_user(void __user *mem, unsigned long len);
251 unsigned long __must_check __clear_user(void __user *mem, unsigned long len);
252
253 #endif /* _ASM_X86_UACCESS_32_H */