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