mn10300: failing __get_user() and get_user() should zero
[pandora-kernel.git] / arch / mn10300 / include / asm / uaccess.h
1 /* MN10300 userspace access functions
2  *
3  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public Licence
8  * as published by the Free Software Foundation; either version
9  * 2 of the Licence, or (at your option) any later version.
10  */
11 #ifndef _ASM_UACCESS_H
12 #define _ASM_UACCESS_H
13
14 /*
15  * User space memory access functions
16  */
17 #include <linux/thread_info.h>
18 #include <linux/kernel.h>
19 #include <asm/page.h>
20 #include <asm/errno.h>
21
22 #define VERIFY_READ 0
23 #define VERIFY_WRITE 1
24
25 /*
26  * The fs value determines whether argument validity checking should be
27  * performed or not.  If get_fs() == USER_DS, checking is performed, with
28  * get_fs() == KERNEL_DS, checking is bypassed.
29  *
30  * For historical reasons, these macros are grossly misnamed.
31  */
32 #define MAKE_MM_SEG(s)  ((mm_segment_t) { (s) })
33
34 #define KERNEL_XDS      MAKE_MM_SEG(0xBFFFFFFF)
35 #define KERNEL_DS       MAKE_MM_SEG(0x9FFFFFFF)
36 #define USER_DS         MAKE_MM_SEG(TASK_SIZE)
37
38 #define get_ds()        (KERNEL_DS)
39 #define get_fs()        (current_thread_info()->addr_limit)
40 #define set_fs(x)       (current_thread_info()->addr_limit = (x))
41 #define __kernel_ds_p() (current_thread_info()->addr_limit.seg == 0x9FFFFFFF)
42
43 #define segment_eq(a, b) ((a).seg == (b).seg)
44
45 #define __addr_ok(addr) \
46         ((unsigned long)(addr) < (current_thread_info()->addr_limit.seg))
47
48 /*
49  * check that a range of addresses falls within the current address limit
50  */
51 static inline int ___range_ok(unsigned long addr, unsigned int size)
52 {
53         int flag = 1, tmp;
54
55         asm("   add     %3,%1   \n"     /* set C-flag if addr + size > 4Gb */
56             "   bcs     0f      \n"
57             "   cmp     %4,%1   \n"     /* jump if addr+size>limit (error) */
58             "   bhi     0f      \n"
59             "   clr     %0      \n"     /* mark okay */
60             "0:                 \n"
61             : "=r"(flag), "=&r"(tmp)
62             : "1"(addr), "ir"(size),
63               "r"(current_thread_info()->addr_limit.seg), "0"(flag)
64             : "cc"
65             );
66
67         return flag;
68 }
69
70 #define __range_ok(addr, size) ___range_ok((unsigned long)(addr), (u32)(size))
71
72 #define access_ok(type, addr, size) (__range_ok((addr), (size)) == 0)
73 #define __access_ok(addr, size)     (__range_ok((addr), (size)) == 0)
74
75 static inline int verify_area(int type, const void *addr, unsigned long size)
76 {
77         return access_ok(type, addr, size) ? 0 : -EFAULT;
78 }
79
80
81 /*
82  * The exception table consists of pairs of addresses: the first is the
83  * address of an instruction that is allowed to fault, and the second is
84  * the address at which the program should continue.  No registers are
85  * modified, so it is entirely up to the continuation code to figure out
86  * what to do.
87  *
88  * All the routines below use bits of fixup code that are out of line
89  * with the main instruction path.  This means when everything is well,
90  * we don't even have to jump over them.  Further, they do not intrude
91  * on our cache or tlb entries.
92  */
93
94 struct exception_table_entry
95 {
96         unsigned long insn, fixup;
97 };
98
99 /* Returns 0 if exception not found and fixup otherwise.  */
100 extern int fixup_exception(struct pt_regs *regs);
101
102 #define put_user(x, ptr) __put_user_check((x), (ptr), sizeof(*(ptr)))
103 #define get_user(x, ptr) __get_user_check((x), (ptr), sizeof(*(ptr)))
104
105 /*
106  * The "__xxx" versions do not do address space checking, useful when
107  * doing multiple accesses to the same area (the user has to do the
108  * checks by hand with "access_ok()")
109  */
110 #define __put_user(x, ptr) __put_user_nocheck((x), (ptr), sizeof(*(ptr)))
111 #define __get_user(x, ptr) __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
112
113 /*
114  * The "xxx_ret" versions return constant specified in third argument, if
115  * something bad happens. These macros can be optimized for the
116  * case of just returning from the function xxx_ret is used.
117  */
118
119 #define put_user_ret(x, ptr, ret) \
120         ({ if (put_user((x), (ptr)))    return (ret); })
121 #define get_user_ret(x, ptr, ret) \
122         ({ if (get_user((x), (ptr)))    return (ret); })
123 #define __put_user_ret(x, ptr, ret) \
124         ({ if (__put_user((x), (ptr)))  return (ret); })
125 #define __get_user_ret(x, ptr, ret) \
126         ({ if (__get_user((x), (ptr)))  return (ret); })
127
128 struct __large_struct { unsigned long buf[100]; };
129 #define __m(x) (*(struct __large_struct *)(x))
130
131 #define __get_user_nocheck(x, ptr, size)                                \
132 ({                                                                      \
133         unsigned long __gu_addr;                                        \
134         int __gu_err;                                                   \
135         __gu_addr = (unsigned long) (ptr);                              \
136         switch (size) {                                                 \
137         case 1: {                                                       \
138                 unsigned char __gu_val;                                 \
139                 __get_user_asm("bu");                                   \
140                 (x) = *(__force __typeof__(*(ptr))*) &__gu_val;         \
141                 break;                                                  \
142         }                                                               \
143         case 2: {                                                       \
144                 unsigned short __gu_val;                                \
145                 __get_user_asm("hu");                                   \
146                 (x) = *(__force __typeof__(*(ptr))*) &__gu_val;         \
147                 break;                                                  \
148         }                                                               \
149         case 4: {                                                       \
150                 unsigned int __gu_val;                                  \
151                 __get_user_asm("");                                     \
152                 (x) = *(__force __typeof__(*(ptr))*) &__gu_val;         \
153                 break;                                                  \
154         }                                                               \
155         default:                                                        \
156                 __get_user_unknown();                                   \
157                 break;                                                  \
158         }                                                               \
159         __gu_err;                                                       \
160 })
161
162 #define __get_user_check(x, ptr, size)                                  \
163 ({                                                                      \
164         const __typeof__(ptr) __guc_ptr = (ptr);                        \
165         int _e;                                                         \
166         if (likely(__access_ok((unsigned long) __guc_ptr, (size))))     \
167                 _e = __get_user_nocheck((x), __guc_ptr, (size));        \
168         else {                                                          \
169                 _e = -EFAULT;                                           \
170                 (x) = (__typeof__(x))0;                                 \
171         }                                                               \
172         _e;                                                             \
173 })
174
175 #define __get_user_asm(INSN)                                    \
176 ({                                                              \
177         asm volatile(                                   \
178                 "1:\n"                                          \
179                 "       mov"INSN"       %2,%1\n"                \
180                 "       mov             0,%0\n"                 \
181                 "2:\n"                                          \
182                 "       .section        .fixup,\"ax\"\n"        \
183                 "3:\n\t"                                        \
184                 "       mov             0,%1\n"                 \
185                 "       mov             %3,%0\n"                \
186                 "       jmp             2b\n"                   \
187                 "       .previous\n"                            \
188                 "       .section        __ex_table,\"a\"\n"     \
189                 "       .balign         4\n"                    \
190                 "       .long           1b, 3b\n"               \
191                 "       .previous"                              \
192                 : "=&r" (__gu_err), "=&r" (__gu_val)            \
193                 : "m" (__m(__gu_addr)), "i" (-EFAULT));         \
194 })
195
196 extern int __get_user_unknown(void);
197
198 #define __put_user_nocheck(x, ptr, size)                        \
199 ({                                                              \
200         union {                                                 \
201                 __typeof__(*(ptr)) val;                         \
202                 u32 bits[2];                                    \
203         } __pu_val;                                             \
204         unsigned long __pu_addr;                                \
205         int __pu_err;                                           \
206         __pu_val.val = (x);                                     \
207         __pu_addr = (unsigned long) (ptr);                      \
208         switch (size) {                                         \
209         case 1:  __put_user_asm("bu"); break;                   \
210         case 2:  __put_user_asm("hu"); break;                   \
211         case 4:  __put_user_asm(""  ); break;                   \
212         case 8:  __put_user_asm8();    break;                   \
213         default: __pu_err = __put_user_unknown(); break;        \
214         }                                                       \
215         __pu_err;                                               \
216 })
217
218 #define __put_user_check(x, ptr, size)                                  \
219 ({                                                                      \
220         union {                                                         \
221                 __typeof__(*(ptr)) val;                                 \
222                 u32 bits[2];                                            \
223         } __pu_val;                                                     \
224         unsigned long __pu_addr;                                        \
225         int __pu_err;                                                   \
226         __pu_val.val = (x);                                             \
227         __pu_addr = (unsigned long) (ptr);                              \
228         if (likely(__access_ok(__pu_addr, size))) {                     \
229                 switch (size) {                                         \
230                 case 1:  __put_user_asm("bu"); break;                   \
231                 case 2:  __put_user_asm("hu"); break;                   \
232                 case 4:  __put_user_asm(""  ); break;                   \
233                 case 8:  __put_user_asm8();    break;                   \
234                 default: __pu_err = __put_user_unknown(); break;        \
235                 }                                                       \
236         }                                                               \
237         else {                                                          \
238                 __pu_err = -EFAULT;                                     \
239         }                                                               \
240         __pu_err;                                                       \
241 })
242
243 #define __put_user_asm(INSN)                                    \
244 ({                                                              \
245         asm volatile(                                           \
246                 "1:\n"                                          \
247                 "       mov"INSN"       %1,%2\n"                \
248                 "       mov             0,%0\n"                 \
249                 "2:\n"                                          \
250                 "       .section        .fixup,\"ax\"\n"        \
251                 "3:\n"                                          \
252                 "       mov             %3,%0\n"                \
253                 "       jmp             2b\n"                   \
254                 "       .previous\n"                            \
255                 "       .section        __ex_table,\"a\"\n"     \
256                 "       .balign         4\n"                    \
257                 "       .long           1b, 3b\n"               \
258                 "       .previous"                              \
259                 : "=&r" (__pu_err)                              \
260                 : "r" (__pu_val.val), "m" (__m(__pu_addr)),     \
261                   "i" (-EFAULT)                                 \
262                 );                                              \
263 })
264
265 #define __put_user_asm8()                                               \
266 ({                                                                      \
267         asm volatile(                                                   \
268                 "1:     mov             %1,%3           \n"             \
269                 "2:     mov             %2,%4           \n"             \
270                 "       mov             0,%0            \n"             \
271                 "3:                                     \n"             \
272                 "       .section        .fixup,\"ax\"   \n"             \
273                 "4:                                     \n"             \
274                 "       mov             %5,%0           \n"             \
275                 "       jmp             3b              \n"             \
276                 "       .previous                       \n"             \
277                 "       .section        __ex_table,\"a\"\n"             \
278                 "       .balign         4               \n"             \
279                 "       .long           1b, 4b          \n"             \
280                 "       .long           2b, 4b          \n"             \
281                 "       .previous                       \n"             \
282                 : "=&r" (__pu_err)                                      \
283                 : "r" (__pu_val.bits[0]), "r" (__pu_val.bits[1]),       \
284                   "m" (__m(__pu_addr)), "m" (__m(__pu_addr+4)),         \
285                   "i" (-EFAULT)                                         \
286                 );                                                      \
287 })
288
289 extern int __put_user_unknown(void);
290
291
292 /*
293  * Copy To/From Userspace
294  */
295 /* Generic arbitrary sized copy.  */
296 #define __copy_user(to, from, size)                                     \
297 do {                                                                    \
298         if (size) {                                                     \
299                 void *__to = to;                                        \
300                 const void *__from = from;                              \
301                 int w;                                                  \
302                 asm volatile(                                           \
303                         "0:     movbu   (%0),%3;\n"                     \
304                         "1:     movbu   %3,(%1);\n"                     \
305                         "       inc     %0;\n"                          \
306                         "       inc     %1;\n"                          \
307                         "       add     -1,%2;\n"                       \
308                         "       bne     0b;\n"                          \
309                         "2:\n"                                          \
310                         "       .section .fixup,\"ax\"\n"               \
311                         "3:     jmp     2b\n"                           \
312                         "       .previous\n"                            \
313                         "       .section __ex_table,\"a\"\n"            \
314                         "       .balign 4\n"                            \
315                         "       .long   0b,3b\n"                        \
316                         "       .long   1b,3b\n"                        \
317                         "       .previous\n"                            \
318                         : "=a"(__from), "=a"(__to), "=r"(size), "=&r"(w)\
319                         : "0"(__from), "1"(__to), "2"(size)             \
320                         : "cc", "memory");                              \
321         }                                                               \
322 } while (0)
323
324 #define __copy_user_zeroing(to, from, size)                             \
325 do {                                                                    \
326         if (size) {                                                     \
327                 void *__to = to;                                        \
328                 const void *__from = from;                              \
329                 int w;                                                  \
330                 asm volatile(                                           \
331                         "0:     movbu   (%0),%3;\n"                     \
332                         "1:     movbu   %3,(%1);\n"                     \
333                         "       inc     %0;\n"                          \
334                         "       inc     %1;\n"                          \
335                         "       add     -1,%2;\n"                       \
336                         "       bne     0b;\n"                          \
337                         "2:\n"                                          \
338                         "       .section .fixup,\"ax\"\n"               \
339                         "3:\n"                                          \
340                         "       mov     %2,%0\n"                        \
341                         "       clr     %3\n"                           \
342                         "4:     movbu   %3,(%1);\n"                     \
343                         "       inc     %1;\n"                          \
344                         "       add     -1,%2;\n"                       \
345                         "       bne     4b;\n"                          \
346                         "       mov     %0,%2\n"                        \
347                         "       jmp     2b\n"                           \
348                         "       .previous\n"                            \
349                         "       .section __ex_table,\"a\"\n"            \
350                         "       .balign 4\n"                            \
351                         "       .long   0b,3b\n"                        \
352                         "       .long   1b,3b\n"                        \
353                         "       .previous\n"                            \
354                         : "=a"(__from), "=a"(__to), "=r"(size), "=&r"(w)\
355                         : "0"(__from), "1"(__to), "2"(size)             \
356                         : "cc", "memory");                              \
357         }                                                               \
358 } while (0)
359
360 /* We let the __ versions of copy_from/to_user inline, because they're often
361  * used in fast paths and have only a small space overhead.
362  */
363 static inline
364 unsigned long __generic_copy_from_user_nocheck(void *to, const void *from,
365                                                unsigned long n)
366 {
367         __copy_user_zeroing(to, from, n);
368         return n;
369 }
370
371 static inline
372 unsigned long __generic_copy_to_user_nocheck(void *to, const void *from,
373                                              unsigned long n)
374 {
375         __copy_user(to, from, n);
376         return n;
377 }
378
379
380 #if 0
381 #error "don't use - these macros don't increment to & from pointers"
382 /* Optimize just a little bit when we know the size of the move. */
383 #define __constant_copy_user(to, from, size)    \
384 do {                                            \
385         asm volatile(                           \
386                 "       mov %0,a0;\n"           \
387                 "0:     movbu (%1),d3;\n"       \
388                 "1:     movbu d3,(%2);\n"       \
389                 "       add -1,a0;\n"           \
390                 "       bne 0b;\n"              \
391                 "2:;"                           \
392                 ".section .fixup,\"ax\"\n"      \
393                 "3:     jmp 2b\n"               \
394                 ".previous\n"                   \
395                 ".section __ex_table,\"a\"\n"   \
396                 "       .balign 4\n"            \
397                 "       .long 0b,3b\n"          \
398                 "       .long 1b,3b\n"          \
399                 ".previous"                     \
400                 :                               \
401                 : "d"(size), "d"(to), "d"(from) \
402                 : "d3", "a0");                  \
403 } while (0)
404
405 /* Optimize just a little bit when we know the size of the move. */
406 #define __constant_copy_user_zeroing(to, from, size)    \
407 do {                                                    \
408         asm volatile(                                   \
409                 "       mov %0,a0;\n"                   \
410                 "0:     movbu (%1),d3;\n"               \
411                 "1:     movbu d3,(%2);\n"               \
412                 "       add -1,a0;\n"                   \
413                 "       bne 0b;\n"                      \
414                 "2:;"                                   \
415                 ".section .fixup,\"ax\"\n"              \
416                 "3:     jmp 2b\n"                       \
417                 ".previous\n"                           \
418                 ".section __ex_table,\"a\"\n"           \
419                 "       .balign 4\n"                    \
420                 "       .long 0b,3b\n"                  \
421                 "       .long 1b,3b\n"                  \
422                 ".previous"                             \
423                 :                                       \
424                 : "d"(size), "d"(to), "d"(from)         \
425                 : "d3", "a0");                          \
426 } while (0)
427
428 static inline
429 unsigned long __constant_copy_to_user(void *to, const void *from,
430                                       unsigned long n)
431 {
432         if (access_ok(VERIFY_WRITE, to, n))
433                 __constant_copy_user(to, from, n);
434         return n;
435 }
436
437 static inline
438 unsigned long __constant_copy_from_user(void *to, const void *from,
439                                         unsigned long n)
440 {
441         if (access_ok(VERIFY_READ, from, n))
442                 __constant_copy_user_zeroing(to, from, n);
443         return n;
444 }
445
446 static inline
447 unsigned long __constant_copy_to_user_nocheck(void *to, const void *from,
448                                               unsigned long n)
449 {
450         __constant_copy_user(to, from, n);
451         return n;
452 }
453
454 static inline
455 unsigned long __constant_copy_from_user_nocheck(void *to, const void *from,
456                                                 unsigned long n)
457 {
458         __constant_copy_user_zeroing(to, from, n);
459         return n;
460 }
461 #endif
462
463 extern unsigned long __generic_copy_to_user(void __user *, const void *,
464                                             unsigned long);
465 extern unsigned long __generic_copy_from_user(void *, const void __user *,
466                                               unsigned long);
467
468 #define __copy_to_user_inatomic(to, from, n) \
469         __generic_copy_to_user_nocheck((to), (from), (n))
470 #define __copy_from_user_inatomic(to, from, n) \
471         __generic_copy_from_user_nocheck((to), (from), (n))
472
473 #define __copy_to_user(to, from, n)                     \
474 ({                                                      \
475         might_sleep();                                  \
476         __copy_to_user_inatomic((to), (from), (n));     \
477 })
478
479 #define __copy_from_user(to, from, n)                   \
480 ({                                                      \
481         might_sleep();                                  \
482         __copy_from_user_inatomic((to), (from), (n));   \
483 })
484
485
486 #define copy_to_user(to, from, n)   __generic_copy_to_user((to), (from), (n))
487 #define copy_from_user(to, from, n) __generic_copy_from_user((to), (from), (n))
488
489 extern long strncpy_from_user(char *dst, const char __user *src, long count);
490 extern long __strncpy_from_user(char *dst, const char __user *src, long count);
491 extern long strnlen_user(const char __user *str, long n);
492 #define strlen_user(str) strnlen_user(str, ~0UL >> 1)
493 extern unsigned long clear_user(void __user *mem, unsigned long len);
494 extern unsigned long __clear_user(void __user *mem, unsigned long len);
495
496 #endif /* _ASM_UACCESS_H */