avr32: fix copy_from_user()
[pandora-kernel.git] / arch / avr32 / include / asm / uaccess.h
1 /*
2  * Copyright (C) 2004-2006 Atmel Corporation
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  */
8 #ifndef __ASM_AVR32_UACCESS_H
9 #define __ASM_AVR32_UACCESS_H
10
11 #include <linux/errno.h>
12 #include <linux/sched.h>
13
14 #define VERIFY_READ     0
15 #define VERIFY_WRITE    1
16
17 typedef struct {
18         unsigned int is_user_space;
19 } mm_segment_t;
20
21 /*
22  * The fs value determines whether argument validity checking should be
23  * performed or not.  If get_fs() == USER_DS, checking is performed, with
24  * get_fs() == KERNEL_DS, checking is bypassed.
25  *
26  * For historical reasons (Data Segment Register?), these macros are misnamed.
27  */
28 #define MAKE_MM_SEG(s)  ((mm_segment_t) { (s) })
29 #define segment_eq(a,b) ((a).is_user_space == (b).is_user_space)
30
31 #define USER_ADDR_LIMIT 0x80000000
32
33 #define KERNEL_DS       MAKE_MM_SEG(0)
34 #define USER_DS         MAKE_MM_SEG(1)
35
36 #define get_ds()        (KERNEL_DS)
37
38 static inline mm_segment_t get_fs(void)
39 {
40         return MAKE_MM_SEG(test_thread_flag(TIF_USERSPACE));
41 }
42
43 static inline void set_fs(mm_segment_t s)
44 {
45         if (s.is_user_space)
46                 set_thread_flag(TIF_USERSPACE);
47         else
48                 clear_thread_flag(TIF_USERSPACE);
49 }
50
51 /*
52  * Test whether a block of memory is a valid user space address.
53  * Returns 0 if the range is valid, nonzero otherwise.
54  *
55  * We do the following checks:
56  *   1. Is the access from kernel space?
57  *   2. Does (addr + size) set the carry bit?
58  *   3. Is (addr + size) a negative number (i.e. >= 0x80000000)?
59  *
60  * If yes on the first check, access is granted.
61  * If no on any of the others, access is denied.
62  */
63 #define __range_ok(addr, size)                                          \
64         (test_thread_flag(TIF_USERSPACE)                                \
65          && (((unsigned long)(addr) >= 0x80000000)                      \
66              || ((unsigned long)(size) > 0x80000000)                    \
67              || (((unsigned long)(addr) + (unsigned long)(size)) > 0x80000000)))
68
69 #define access_ok(type, addr, size) (likely(__range_ok(addr, size) == 0))
70
71 /* Generic arbitrary sized copy. Return the number of bytes NOT copied */
72 extern __kernel_size_t __copy_user(void *to, const void *from,
73                                    __kernel_size_t n);
74
75 extern __kernel_size_t copy_to_user(void __user *to, const void *from,
76                                     __kernel_size_t n);
77 extern __kernel_size_t ___copy_from_user(void *to, const void __user *from,
78                                       __kernel_size_t n);
79
80 static inline __kernel_size_t __copy_to_user(void __user *to, const void *from,
81                                              __kernel_size_t n)
82 {
83         return __copy_user((void __force *)to, from, n);
84 }
85 static inline __kernel_size_t __copy_from_user(void *to,
86                                                const void __user *from,
87                                                __kernel_size_t n)
88 {
89         return __copy_user(to, (const void __force *)from, n);
90 }
91 static inline __kernel_size_t copy_from_user(void *to,
92                                                const void __user *from,
93                                                __kernel_size_t n)
94 {
95         size_t res = ___copy_from_user(to, from, n);
96         if (unlikely(res))
97                 memset(to + (n - res), 0, res);
98         return res;
99 }
100
101 #define __copy_to_user_inatomic __copy_to_user
102 #define __copy_from_user_inatomic __copy_from_user
103
104 /*
105  * put_user: - Write a simple value into user space.
106  * @x:   Value to copy to user space.
107  * @ptr: Destination address, in user space.
108  *
109  * Context: User context only.  This function may sleep.
110  *
111  * This macro copies a single simple value from kernel space to user
112  * space.  It supports simple types like char and int, but not larger
113  * data types like structures or arrays.
114  *
115  * @ptr must have pointer-to-simple-variable type, and @x must be assignable
116  * to the result of dereferencing @ptr.
117  *
118  * Returns zero on success, or -EFAULT on error.
119  */
120 #define put_user(x,ptr) \
121         __put_user_check((x),(ptr),sizeof(*(ptr)))
122
123 /*
124  * get_user: - Get a simple variable from user space.
125  * @x:   Variable to store result.
126  * @ptr: Source address, in user space.
127  *
128  * Context: User context only.  This function may sleep.
129  *
130  * This macro copies a single simple variable from user space to kernel
131  * space.  It supports simple types like char and int, but not larger
132  * data types like structures or arrays.
133  *
134  * @ptr must have pointer-to-simple-variable type, and the result of
135  * dereferencing @ptr must be assignable to @x without a cast.
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) \
141         __get_user_check((x),(ptr),sizeof(*(ptr)))
142
143 /*
144  * __put_user: - Write a simple value into user space, with less checking.
145  * @x:   Value to copy to user space.
146  * @ptr: Destination address, in user space.
147  *
148  * Context: User context only.  This function may sleep.
149  *
150  * This macro copies a single simple value from kernel space to user
151  * space.  It supports simple types like char and int, but not larger
152  * data types like structures or arrays.
153  *
154  * @ptr must have pointer-to-simple-variable type, and @x must be assignable
155  * to the result of dereferencing @ptr.
156  *
157  * Caller must check the pointer with access_ok() before calling this
158  * function.
159  *
160  * Returns zero on success, or -EFAULT on error.
161  */
162 #define __put_user(x,ptr) \
163         __put_user_nocheck((x),(ptr),sizeof(*(ptr)))
164
165 /*
166  * __get_user: - Get a simple variable from user space, with less checking.
167  * @x:   Variable to store result.
168  * @ptr: Source address, in user space.
169  *
170  * Context: User context only.  This function may sleep.
171  *
172  * This macro copies a single simple variable from user space to kernel
173  * space.  It supports simple types like char and int, but not larger
174  * data types like structures or arrays.
175  *
176  * @ptr must have pointer-to-simple-variable type, and the result of
177  * dereferencing @ptr must be assignable to @x without a cast.
178  *
179  * Caller must check the pointer with access_ok() before calling this
180  * function.
181  *
182  * Returns zero on success, or -EFAULT on error.
183  * On error, the variable @x is set to zero.
184  */
185 #define __get_user(x,ptr) \
186         __get_user_nocheck((x),(ptr),sizeof(*(ptr)))
187
188 extern int __get_user_bad(void);
189 extern int __put_user_bad(void);
190
191 #define __get_user_nocheck(x, ptr, size)                                \
192 ({                                                                      \
193         unsigned long __gu_val = 0;                                     \
194         int __gu_err = 0;                                               \
195                                                                         \
196         switch (size) {                                                 \
197         case 1: __get_user_asm("ub", __gu_val, ptr, __gu_err); break;   \
198         case 2: __get_user_asm("uh", __gu_val, ptr, __gu_err); break;   \
199         case 4: __get_user_asm("w", __gu_val, ptr, __gu_err); break;    \
200         default: __gu_err = __get_user_bad(); break;                    \
201         }                                                               \
202                                                                         \
203         x = (typeof(*(ptr)))__gu_val;                                   \
204         __gu_err;                                                       \
205 })
206
207 #define __get_user_check(x, ptr, size)                                  \
208 ({                                                                      \
209         unsigned long __gu_val = 0;                                     \
210         const typeof(*(ptr)) __user * __gu_addr = (ptr);                \
211         int __gu_err = 0;                                               \
212                                                                         \
213         if (access_ok(VERIFY_READ, __gu_addr, size)) {                  \
214                 switch (size) {                                         \
215                 case 1:                                                 \
216                         __get_user_asm("ub", __gu_val, __gu_addr,       \
217                                        __gu_err);                       \
218                         break;                                          \
219                 case 2:                                                 \
220                         __get_user_asm("uh", __gu_val, __gu_addr,       \
221                                        __gu_err);                       \
222                         break;                                          \
223                 case 4:                                                 \
224                         __get_user_asm("w", __gu_val, __gu_addr,        \
225                                        __gu_err);                       \
226                         break;                                          \
227                 default:                                                \
228                         __gu_err = __get_user_bad();                    \
229                         break;                                          \
230                 }                                                       \
231         } else {                                                        \
232                 __gu_err = -EFAULT;                                     \
233         }                                                               \
234         x = (typeof(*(ptr)))__gu_val;                                   \
235         __gu_err;                                                       \
236 })
237
238 #define __get_user_asm(suffix, __gu_val, ptr, __gu_err)                 \
239         asm volatile(                                                   \
240                 "1:     ld." suffix "   %1, %3                  \n"     \
241                 "2:                                             \n"     \
242                 "       .subsection 1                           \n"     \
243                 "3:     mov     %0, %4                          \n"     \
244                 "       rjmp    2b                              \n"     \
245                 "       .subsection 0                           \n"     \
246                 "       .section __ex_table, \"a\"              \n"     \
247                 "       .long   1b, 3b                          \n"     \
248                 "       .previous                               \n"     \
249                 : "=r"(__gu_err), "=r"(__gu_val)                        \
250                 : "0"(__gu_err), "m"(*(ptr)), "i"(-EFAULT))
251
252 #define __put_user_nocheck(x, ptr, size)                                \
253 ({                                                                      \
254         typeof(*(ptr)) __pu_val;                                        \
255         int __pu_err = 0;                                               \
256                                                                         \
257         __pu_val = (x);                                                 \
258         switch (size) {                                                 \
259         case 1: __put_user_asm("b", ptr, __pu_val, __pu_err); break;    \
260         case 2: __put_user_asm("h", ptr, __pu_val, __pu_err); break;    \
261         case 4: __put_user_asm("w", ptr, __pu_val, __pu_err); break;    \
262         case 8: __put_user_asm("d", ptr, __pu_val, __pu_err); break;    \
263         default: __pu_err = __put_user_bad(); break;                    \
264         }                                                               \
265         __pu_err;                                                       \
266 })
267
268 #define __put_user_check(x, ptr, size)                                  \
269 ({                                                                      \
270         typeof(*(ptr)) __pu_val;                                        \
271         typeof(*(ptr)) __user *__pu_addr = (ptr);                       \
272         int __pu_err = 0;                                               \
273                                                                         \
274         __pu_val = (x);                                                 \
275         if (access_ok(VERIFY_WRITE, __pu_addr, size)) {                 \
276                 switch (size) {                                         \
277                 case 1:                                                 \
278                         __put_user_asm("b", __pu_addr, __pu_val,        \
279                                        __pu_err);                       \
280                         break;                                          \
281                 case 2:                                                 \
282                         __put_user_asm("h", __pu_addr, __pu_val,        \
283                                        __pu_err);                       \
284                         break;                                          \
285                 case 4:                                                 \
286                         __put_user_asm("w", __pu_addr, __pu_val,        \
287                                        __pu_err);                       \
288                         break;                                          \
289                 case 8:                                                 \
290                         __put_user_asm("d", __pu_addr, __pu_val,                \
291                                        __pu_err);                       \
292                         break;                                          \
293                 default:                                                \
294                         __pu_err = __put_user_bad();                    \
295                         break;                                          \
296                 }                                                       \
297         } else {                                                        \
298                 __pu_err = -EFAULT;                                     \
299         }                                                               \
300         __pu_err;                                                       \
301 })
302
303 #define __put_user_asm(suffix, ptr, __pu_val, __gu_err)                 \
304         asm volatile(                                                   \
305                 "1:     st." suffix "   %1, %3                  \n"     \
306                 "2:                                             \n"     \
307                 "       .subsection 1                           \n"     \
308                 "3:     mov     %0, %4                          \n"     \
309                 "       rjmp    2b                              \n"     \
310                 "       .subsection 0                           \n"     \
311                 "       .section __ex_table, \"a\"              \n"     \
312                 "       .long   1b, 3b                          \n"     \
313                 "       .previous                               \n"     \
314                 : "=r"(__gu_err), "=m"(*(ptr))                          \
315                 : "0"(__gu_err), "r"(__pu_val), "i"(-EFAULT))
316
317 extern __kernel_size_t clear_user(void __user *addr, __kernel_size_t size);
318 extern __kernel_size_t __clear_user(void __user *addr, __kernel_size_t size);
319
320 extern long strncpy_from_user(char *dst, const char __user *src, long count);
321 extern long __strncpy_from_user(char *dst, const char __user *src, long count);
322
323 extern long strnlen_user(const char __user *__s, long __n);
324 extern long __strnlen_user(const char __user *__s, long __n);
325
326 #define strlen_user(s) strnlen_user(s, ~0UL >> 1)
327
328 struct exception_table_entry
329 {
330         unsigned long insn, fixup;
331 };
332
333 #endif /* __ASM_AVR32_UACCESS_H */