]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/score/include/asm/uaccess.h
uaccess: move VERIFY_{READ,WRITE} definitions to linux/uaccess.h
[karo-tx-linux.git] / arch / score / include / asm / uaccess.h
1 #ifndef __SCORE_UACCESS_H
2 #define __SCORE_UACCESS_H
3
4 #include <linux/kernel.h>
5 #include <linux/errno.h>
6 #include <linux/thread_info.h>
7 #include <asm/extable.h>
8
9 #define get_ds()                (KERNEL_DS)
10 #define get_fs()                (current_thread_info()->addr_limit)
11 #define segment_eq(a, b)        ((a).seg == (b).seg)
12
13 /*
14  * Is a address valid? This does a straighforward calculation rather
15  * than tests.
16  *
17  * Address valid if:
18  *  - "addr" doesn't have any high-bits set
19  *  - AND "size" doesn't have any high-bits set
20  *  - AND "addr+size" doesn't have any high-bits set
21  *  - OR we are in kernel mode.
22  *
23  * __ua_size() is a trick to avoid runtime checking of positive constant
24  * sizes; for those we already know at compile time that the size is ok.
25  */
26 #define __ua_size(size)                                                 \
27         ((__builtin_constant_p(size) && (signed long) (size) > 0) ? 0 : (size))
28
29 /*
30  * access_ok: - Checks if a user space pointer is valid
31  * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE.  Note that
32  *        %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
33  *        to write to a block, it is always safe to read from it.
34  * @addr: User space pointer to start of block to check
35  * @size: Size of block to check
36  *
37  * Context: User context only. This function may sleep if pagefaults are
38  *          enabled.
39  *
40  * Checks if a pointer to a block of memory in user space is valid.
41  *
42  * Returns true (nonzero) if the memory block may be valid, false (zero)
43  * if it is definitely invalid.
44  *
45  * Note that, depending on architecture, this function probably just
46  * checks that the pointer is in the user space range - after calling
47  * this function, memory access functions may still return -EFAULT.
48  */
49
50 #define __access_ok(addr, size)                                 \
51         (((long)((get_fs().seg) &                               \
52                  ((addr) | ((addr) + (size)) |                  \
53                   __ua_size(size)))) == 0)
54
55 #define access_ok(type, addr, size)                             \
56         likely(__access_ok((unsigned long)(addr), (size)))
57
58 /*
59  * put_user: - Write a simple value into user space.
60  * @x:   Value to copy to user space.
61  * @ptr: Destination address, in user space.
62  *
63  * Context: User context only. This function may sleep if pagefaults are
64  *          enabled.
65  *
66  * This macro copies a single simple value from kernel space to user
67  * space.  It supports simple types like char and int, but not larger
68  * data types like structures or arrays.
69  *
70  * @ptr must have pointer-to-simple-variable type, and @x must be assignable
71  * to the result of dereferencing @ptr.
72  *
73  * Returns zero on success, or -EFAULT on error.
74  */
75 #define put_user(x, ptr) __put_user_check((x), (ptr), sizeof(*(ptr)))
76
77 /*
78  * get_user: - Get a simple variable from user space.
79  * @x:   Variable to store result.
80  * @ptr: Source address, in user space.
81  *
82  * Context: User context only. This function may sleep if pagefaults are
83  *          enabled.
84  *
85  * This macro copies a single simple variable from user space to kernel
86  * space.  It supports simple types like char and int, but not larger
87  * data types like structures or arrays.
88  *
89  * @ptr must have pointer-to-simple-variable type, and the result of
90  * dereferencing @ptr must be assignable to @x without a cast.
91  *
92  * Returns zero on success, or -EFAULT on error.
93  * On error, the variable @x is set to zero.
94  */
95 #define get_user(x, ptr) __get_user_check((x), (ptr), sizeof(*(ptr)))
96
97 /*
98  * __put_user: - Write a simple value into user space, with less checking.
99  * @x:   Value to copy to user space.
100  * @ptr: Destination address, in user space.
101  *
102  * Context: User context only. This function may sleep if pagefaults are
103  *          enabled.
104  *
105  * This macro copies a single simple value from kernel space to user
106  * space.  It supports simple types like char and int, but not larger
107  * data types like structures or arrays.
108  *
109  * @ptr must have pointer-to-simple-variable type, and @x must be assignable
110  * to the result of dereferencing @ptr.
111  *
112  * Caller must check the pointer with access_ok() before calling this
113  * function.
114  *
115  * Returns zero on success, or -EFAULT on error.
116  */
117 #define __put_user(x, ptr) __put_user_nocheck((x), (ptr), sizeof(*(ptr)))
118
119 /*
120  * __get_user: - Get a simple variable from user space, with less checking.
121  * @x:   Variable to store result.
122  * @ptr: Source address, in user space.
123  *
124  * Context: User context only. This function may sleep if pagefaults are
125  *          enabled.
126  *
127  * This macro copies a single simple variable from user space to kernel
128  * space.  It supports simple types like char and int, but not larger
129  * data types like structures or arrays.
130  *
131  * @ptr must have pointer-to-simple-variable type, and the result of
132  * dereferencing @ptr must be assignable to @x without a cast.
133  *
134  * Caller must check the pointer with access_ok() before calling this
135  * function.
136  *
137  * Returns zero on success, or -EFAULT on error.
138  * On error, the variable @x is set to zero.
139  */
140 #define __get_user(x, ptr) __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
141
142 struct __large_struct { unsigned long buf[100]; };
143 #define __m(x) (*(struct __large_struct __user *)(x))
144
145 /*
146  * Yuck.  We need two variants, one for 64bit operation and one
147  * for 32 bit mode and old iron.
148  */
149 extern void __get_user_unknown(void);
150
151 #define __get_user_common(val, size, ptr)                               \
152 do {                                                                    \
153         switch (size) {                                                 \
154         case 1:                                                         \
155                 __get_user_asm(val, "lb", ptr);                         \
156                 break;                                                  \
157         case 2:                                                         \
158                 __get_user_asm(val, "lh", ptr);                         \
159                  break;                                                 \
160         case 4:                                                         \
161                 __get_user_asm(val, "lw", ptr);                         \
162                  break;                                                 \
163         case 8:                                                         \
164                 if (__copy_from_user((void *)&val, ptr, 8) == 0)        \
165                         __gu_err = 0;                                   \
166                 else                                                    \
167                         __gu_err = -EFAULT;                             \
168                 break;                                                  \
169         default:                                                        \
170                 __get_user_unknown();                                   \
171                 break;                                                  \
172         }                                                               \
173 } while (0)
174
175 #define __get_user_nocheck(x, ptr, size)                                \
176 ({                                                                      \
177         long __gu_err = 0;                                              \
178         __get_user_common((x), size, ptr);                              \
179         __gu_err;                                                       \
180 })
181
182 #define __get_user_check(x, ptr, size)                                  \
183 ({                                                                      \
184         long __gu_err = -EFAULT;                                        \
185         const __typeof__(*(ptr)) __user *__gu_ptr = (ptr);              \
186                                                                         \
187         if (likely(access_ok(VERIFY_READ, __gu_ptr, size)))             \
188                 __get_user_common((x), size, __gu_ptr);                 \
189         else                                                            \
190                 (x) = 0;                                                \
191                                                                         \
192         __gu_err;                                                       \
193 })
194
195 #define __get_user_asm(val, insn, addr)                                 \
196 {                                                                       \
197         long __gu_tmp;                                                  \
198                                                                         \
199         __asm__ __volatile__(                                           \
200                 "1:" insn " %1, %3\n"                                   \
201                 "2:\n"                                                  \
202                 ".section .fixup,\"ax\"\n"                              \
203                 "3:li   %0, %4\n"                                       \
204                 "li     %1, 0\n"                                        \
205                 "j      2b\n"                                           \
206                 ".previous\n"                                           \
207                 ".section __ex_table,\"a\"\n"                           \
208                 ".word  1b, 3b\n"                                       \
209                 ".previous\n"                                           \
210                 : "=r" (__gu_err), "=r" (__gu_tmp)                      \
211                 : "0" (0), "o" (__m(addr)), "i" (-EFAULT));             \
212                                                                         \
213                 (val) = (__typeof__(*(addr))) __gu_tmp;                 \
214 }
215
216 /*
217  * Yuck.  We need two variants, one for 64bit operation and one
218  * for 32 bit mode and old iron.
219  */
220 #define __put_user_nocheck(val, ptr, size)                              \
221 ({                                                                      \
222         __typeof__(*(ptr)) __pu_val;                                    \
223         long __pu_err = 0;                                              \
224                                                                         \
225         __pu_val = (val);                                               \
226         switch (size) {                                                 \
227         case 1:                                                         \
228                 __put_user_asm("sb", ptr);                              \
229                 break;                                                  \
230         case 2:                                                         \
231                 __put_user_asm("sh", ptr);                              \
232                 break;                                                  \
233         case 4:                                                         \
234                 __put_user_asm("sw", ptr);                              \
235                 break;                                                  \
236         case 8:                                                         \
237                 if ((__copy_to_user((void *)ptr, &__pu_val, 8)) == 0)   \
238                         __pu_err = 0;                                   \
239                 else                                                    \
240                         __pu_err = -EFAULT;                             \
241                 break;                                                  \
242         default:                                                        \
243                  __put_user_unknown();                                  \
244                  break;                                                 \
245         }                                                               \
246         __pu_err;                                                       \
247 })
248
249
250 #define __put_user_check(val, ptr, size)                                \
251 ({                                                                      \
252         __typeof__(*(ptr)) __user *__pu_addr = (ptr);                   \
253         __typeof__(*(ptr)) __pu_val = (val);                            \
254         long __pu_err = -EFAULT;                                        \
255                                                                         \
256         if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) {         \
257                 switch (size) {                                         \
258                 case 1:                                                 \
259                         __put_user_asm("sb", __pu_addr);                \
260                         break;                                          \
261                 case 2:                                                 \
262                         __put_user_asm("sh", __pu_addr);                \
263                         break;                                          \
264                 case 4:                                                 \
265                         __put_user_asm("sw", __pu_addr);                \
266                         break;                                          \
267                 case 8:                                                 \
268                         if ((__copy_to_user((void *)__pu_addr, &__pu_val, 8)) == 0)\
269                                 __pu_err = 0;                           \
270                         else                                            \
271                                 __pu_err = -EFAULT;                     \
272                         break;                                          \
273                 default:                                                \
274                         __put_user_unknown();                           \
275                         break;                                          \
276                 }                                                       \
277         }                                                               \
278         __pu_err;                                                       \
279 })
280
281 #define __put_user_asm(insn, ptr)                                       \
282         __asm__ __volatile__(                                           \
283                 "1:" insn " %2, %3\n"                                   \
284                 "2:\n"                                                  \
285                 ".section .fixup,\"ax\"\n"                              \
286                 "3:li %0, %4\n"                                         \
287                 "j 2b\n"                                                \
288                 ".previous\n"                                           \
289                 ".section __ex_table,\"a\"\n"                           \
290                 ".word 1b, 3b\n"                                        \
291                 ".previous\n"                                           \
292                 : "=r" (__pu_err)                                       \
293                 : "0" (0), "r" (__pu_val), "o" (__m(ptr)),              \
294                   "i" (-EFAULT));
295
296 extern void __put_user_unknown(void);
297 extern int __copy_tofrom_user(void *to, const void *from, unsigned long len);
298
299 static inline unsigned long
300 copy_from_user(void *to, const void *from, unsigned long len)
301 {
302         unsigned long res = len;
303
304         if (likely(access_ok(VERIFY_READ, from, len)))
305                 res = __copy_tofrom_user(to, from, len);
306
307         if (unlikely(res))
308                 memset(to + (len - res), 0, res);
309
310         return res;
311 }
312
313 static inline unsigned long
314 copy_to_user(void *to, const void *from, unsigned long len)
315 {
316         if (likely(access_ok(VERIFY_WRITE, to, len)))
317                 len = __copy_tofrom_user(to, from, len);
318
319         return len;
320 }
321
322 static inline unsigned long
323 __copy_from_user(void *to, const void *from, unsigned long len)
324 {
325         unsigned long left = __copy_tofrom_user(to, from, len);
326         if (unlikely(left))
327                 memset(to + (len - left), 0, left);
328         return left;
329 }
330
331 #define __copy_to_user(to, from, len)           \
332                 __copy_tofrom_user((to), (from), (len))
333
334 static inline unsigned long
335 __copy_to_user_inatomic(void *to, const void *from, unsigned long len)
336 {
337         return __copy_to_user(to, from, len);
338 }
339
340 static inline unsigned long
341 __copy_from_user_inatomic(void *to, const void *from, unsigned long len)
342 {
343         return __copy_tofrom_user(to, from, len);
344 }
345
346 #define __copy_in_user(to, from, len)   __copy_tofrom_user(to, from, len)
347
348 static inline unsigned long
349 copy_in_user(void *to, const void *from, unsigned long len)
350 {
351         if (access_ok(VERIFY_READ, from, len) &&
352                       access_ok(VERFITY_WRITE, to, len))
353                 return __copy_tofrom_user(to, from, len);
354 }
355
356 /*
357  * __clear_user: - Zero a block of memory in user space, with less checking.
358  * @to:   Destination address, in user space.
359  * @n:    Number of bytes to zero.
360  *
361  * Zero a block of memory in user space.  Caller must check
362  * the specified block with access_ok() before calling this function.
363  *
364  * Returns number of bytes that could not be cleared.
365  * On success, this will be zero.
366  */
367 extern unsigned long __clear_user(void __user *src, unsigned long size);
368
369 static inline unsigned long clear_user(char *src, unsigned long size)
370 {
371         if (access_ok(VERIFY_WRITE, src, size))
372                 return __clear_user(src, size);
373
374         return -EFAULT;
375 }
376 /*
377  * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
378  * @dst:   Destination address, in kernel space.  This buffer must be at
379  *         least @count bytes long.
380  * @src:   Source address, in user space.
381  * @count: Maximum number of bytes to copy, including the trailing NUL.
382  *
383  * Copies a NUL-terminated string from userspace to kernel space.
384  * Caller must check the specified block with access_ok() before calling
385  * this function.
386  *
387  * On success, returns the length of the string (not including the trailing
388  * NUL).
389  *
390  * If access to userspace fails, returns -EFAULT (some data may have been
391  * copied).
392  *
393  * If @count is smaller than the length of the string, copies @count bytes
394  * and returns @count.
395  */
396 extern int __strncpy_from_user(char *dst, const char *src, long len);
397
398 static inline int strncpy_from_user(char *dst, const char *src, long len)
399 {
400         if (access_ok(VERIFY_READ, src, 1))
401                 return __strncpy_from_user(dst, src, len);
402
403         return -EFAULT;
404 }
405
406 extern int __strlen_user(const char *src);
407 static inline long strlen_user(const char __user *src)
408 {
409         return __strlen_user(src);
410 }
411
412 extern int __strnlen_user(const char *str, long len);
413 static inline long strnlen_user(const char __user *str, long len)
414 {
415         if (!access_ok(VERIFY_READ, str, 0))
416                 return 0;
417         else            
418                 return __strnlen_user(str, len);
419 }
420
421 #endif /* __SCORE_UACCESS_H */
422