Merge tag 'gfs2-4.8.fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/gfs2...
[cascardo/linux.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 struct __large_struct { unsigned long buf[100]; };
114 #define __m(x) (*(struct __large_struct *)(x))
115
116 #define __get_user_nocheck(x, ptr, size)                                \
117 ({                                                                      \
118         unsigned long __gu_addr;                                        \
119         int __gu_err;                                                   \
120         __gu_addr = (unsigned long) (ptr);                              \
121         switch (size) {                                                 \
122         case 1: {                                                       \
123                 unsigned char __gu_val;                                 \
124                 __get_user_asm("bu");                                   \
125                 (x) = *(__force __typeof__(*(ptr))*) &__gu_val;         \
126                 break;                                                  \
127         }                                                               \
128         case 2: {                                                       \
129                 unsigned short __gu_val;                                \
130                 __get_user_asm("hu");                                   \
131                 (x) = *(__force __typeof__(*(ptr))*) &__gu_val;         \
132                 break;                                                  \
133         }                                                               \
134         case 4: {                                                       \
135                 unsigned int __gu_val;                                  \
136                 __get_user_asm("");                                     \
137                 (x) = *(__force __typeof__(*(ptr))*) &__gu_val;         \
138                 break;                                                  \
139         }                                                               \
140         default:                                                        \
141                 __get_user_unknown();                                   \
142                 break;                                                  \
143         }                                                               \
144         __gu_err;                                                       \
145 })
146
147 #define __get_user_check(x, ptr, size)                                  \
148 ({                                                                      \
149         const __typeof__(*(ptr))* __guc_ptr = (ptr);                    \
150         int _e;                                                         \
151         if (likely(__access_ok((unsigned long) __guc_ptr, (size))))     \
152                 _e = __get_user_nocheck((x), __guc_ptr, (size));        \
153         else {                                                          \
154                 _e = -EFAULT;                                           \
155                 (x) = (__typeof__(x))0;                                 \
156         }                                                               \
157         _e;                                                             \
158 })
159
160 #define __get_user_asm(INSN)                                    \
161 ({                                                              \
162         asm volatile(                                   \
163                 "1:\n"                                          \
164                 "       mov"INSN"       %2,%1\n"                \
165                 "       mov             0,%0\n"                 \
166                 "2:\n"                                          \
167                 "       .section        .fixup,\"ax\"\n"        \
168                 "3:\n\t"                                        \
169                 "       mov             0,%1\n"                 \
170                 "       mov             %3,%0\n"                \
171                 "       jmp             2b\n"                   \
172                 "       .previous\n"                            \
173                 "       .section        __ex_table,\"a\"\n"     \
174                 "       .balign         4\n"                    \
175                 "       .long           1b, 3b\n"               \
176                 "       .previous"                              \
177                 : "=&r" (__gu_err), "=&r" (__gu_val)            \
178                 : "m" (__m(__gu_addr)), "i" (-EFAULT));         \
179 })
180
181 extern int __get_user_unknown(void);
182
183 #define __put_user_nocheck(x, ptr, size)                        \
184 ({                                                              \
185         union {                                                 \
186                 __typeof__(*(ptr)) val;                         \
187                 u32 bits[2];                                    \
188         } __pu_val;                                             \
189         unsigned long __pu_addr;                                \
190         int __pu_err;                                           \
191         __pu_val.val = (x);                                     \
192         __pu_addr = (unsigned long) (ptr);                      \
193         switch (size) {                                         \
194         case 1:  __put_user_asm("bu"); break;                   \
195         case 2:  __put_user_asm("hu"); break;                   \
196         case 4:  __put_user_asm(""  ); break;                   \
197         case 8:  __put_user_asm8();    break;                   \
198         default: __pu_err = __put_user_unknown(); break;        \
199         }                                                       \
200         __pu_err;                                               \
201 })
202
203 #define __put_user_check(x, ptr, size)                                  \
204 ({                                                                      \
205         union {                                                         \
206                 __typeof__(*(ptr)) val;                                 \
207                 u32 bits[2];                                            \
208         } __pu_val;                                                     \
209         unsigned long __pu_addr;                                        \
210         int __pu_err;                                                   \
211         __pu_val.val = (x);                                             \
212         __pu_addr = (unsigned long) (ptr);                              \
213         if (likely(__access_ok(__pu_addr, size))) {                     \
214                 switch (size) {                                         \
215                 case 1:  __put_user_asm("bu"); break;                   \
216                 case 2:  __put_user_asm("hu"); break;                   \
217                 case 4:  __put_user_asm(""  ); break;                   \
218                 case 8:  __put_user_asm8();    break;                   \
219                 default: __pu_err = __put_user_unknown(); break;        \
220                 }                                                       \
221         }                                                               \
222         else {                                                          \
223                 __pu_err = -EFAULT;                                     \
224         }                                                               \
225         __pu_err;                                                       \
226 })
227
228 #define __put_user_asm(INSN)                                    \
229 ({                                                              \
230         asm volatile(                                           \
231                 "1:\n"                                          \
232                 "       mov"INSN"       %1,%2\n"                \
233                 "       mov             0,%0\n"                 \
234                 "2:\n"                                          \
235                 "       .section        .fixup,\"ax\"\n"        \
236                 "3:\n"                                          \
237                 "       mov             %3,%0\n"                \
238                 "       jmp             2b\n"                   \
239                 "       .previous\n"                            \
240                 "       .section        __ex_table,\"a\"\n"     \
241                 "       .balign         4\n"                    \
242                 "       .long           1b, 3b\n"               \
243                 "       .previous"                              \
244                 : "=&r" (__pu_err)                              \
245                 : "r" (__pu_val.val), "m" (__m(__pu_addr)),     \
246                   "i" (-EFAULT)                                 \
247                 );                                              \
248 })
249
250 #define __put_user_asm8()                                               \
251 ({                                                                      \
252         asm volatile(                                                   \
253                 "1:     mov             %1,%3           \n"             \
254                 "2:     mov             %2,%4           \n"             \
255                 "       mov             0,%0            \n"             \
256                 "3:                                     \n"             \
257                 "       .section        .fixup,\"ax\"   \n"             \
258                 "4:                                     \n"             \
259                 "       mov             %5,%0           \n"             \
260                 "       jmp             3b              \n"             \
261                 "       .previous                       \n"             \
262                 "       .section        __ex_table,\"a\"\n"             \
263                 "       .balign         4               \n"             \
264                 "       .long           1b, 4b          \n"             \
265                 "       .long           2b, 4b          \n"             \
266                 "       .previous                       \n"             \
267                 : "=&r" (__pu_err)                                      \
268                 : "r" (__pu_val.bits[0]), "r" (__pu_val.bits[1]),       \
269                   "m" (__m(__pu_addr)), "m" (__m(__pu_addr+4)),         \
270                   "i" (-EFAULT)                                         \
271                 );                                                      \
272 })
273
274 extern int __put_user_unknown(void);
275
276
277 /*
278  * Copy To/From Userspace
279  */
280 /* Generic arbitrary sized copy.  */
281 #define __copy_user(to, from, size)                                     \
282 do {                                                                    \
283         if (size) {                                                     \
284                 void *__to = to;                                        \
285                 const void *__from = from;                              \
286                 int w;                                                  \
287                 asm volatile(                                           \
288                         "0:     movbu   (%0),%3;\n"                     \
289                         "1:     movbu   %3,(%1);\n"                     \
290                         "       inc     %0;\n"                          \
291                         "       inc     %1;\n"                          \
292                         "       add     -1,%2;\n"                       \
293                         "       bne     0b;\n"                          \
294                         "2:\n"                                          \
295                         "       .section .fixup,\"ax\"\n"               \
296                         "3:     jmp     2b\n"                           \
297                         "       .previous\n"                            \
298                         "       .section __ex_table,\"a\"\n"            \
299                         "       .balign 4\n"                            \
300                         "       .long   0b,3b\n"                        \
301                         "       .long   1b,3b\n"                        \
302                         "       .previous\n"                            \
303                         : "=a"(__from), "=a"(__to), "=r"(size), "=&r"(w)\
304                         : "0"(__from), "1"(__to), "2"(size)             \
305                         : "cc", "memory");                              \
306         }                                                               \
307 } while (0)
308
309 #define __copy_user_zeroing(to, from, size)                             \
310 do {                                                                    \
311         if (size) {                                                     \
312                 void *__to = to;                                        \
313                 const void *__from = from;                              \
314                 int w;                                                  \
315                 asm volatile(                                           \
316                         "0:     movbu   (%0),%3;\n"                     \
317                         "1:     movbu   %3,(%1);\n"                     \
318                         "       inc     %0;\n"                          \
319                         "       inc     %1;\n"                          \
320                         "       add     -1,%2;\n"                       \
321                         "       bne     0b;\n"                          \
322                         "2:\n"                                          \
323                         "       .section .fixup,\"ax\"\n"               \
324                         "3:\n"                                          \
325                         "       mov     %2,%0\n"                        \
326                         "       clr     %3\n"                           \
327                         "4:     movbu   %3,(%1);\n"                     \
328                         "       inc     %1;\n"                          \
329                         "       add     -1,%2;\n"                       \
330                         "       bne     4b;\n"                          \
331                         "       mov     %0,%2\n"                        \
332                         "       jmp     2b\n"                           \
333                         "       .previous\n"                            \
334                         "       .section __ex_table,\"a\"\n"            \
335                         "       .balign 4\n"                            \
336                         "       .long   0b,3b\n"                        \
337                         "       .long   1b,3b\n"                        \
338                         "       .previous\n"                            \
339                         : "=a"(__from), "=a"(__to), "=r"(size), "=&r"(w)\
340                         : "0"(__from), "1"(__to), "2"(size)             \
341                         : "cc", "memory");                              \
342         }                                                               \
343 } while (0)
344
345 /* We let the __ versions of copy_from/to_user inline, because they're often
346  * used in fast paths and have only a small space overhead.
347  */
348 static inline
349 unsigned long __generic_copy_from_user_nocheck(void *to, const void *from,
350                                                unsigned long n)
351 {
352         __copy_user_zeroing(to, from, n);
353         return n;
354 }
355
356 static inline
357 unsigned long __generic_copy_to_user_nocheck(void *to, const void *from,
358                                              unsigned long n)
359 {
360         __copy_user(to, from, n);
361         return n;
362 }
363
364
365 #if 0
366 #error "don't use - these macros don't increment to & from pointers"
367 /* Optimize just a little bit when we know the size of the move. */
368 #define __constant_copy_user(to, from, size)    \
369 do {                                            \
370         asm volatile(                           \
371                 "       mov %0,a0;\n"           \
372                 "0:     movbu (%1),d3;\n"       \
373                 "1:     movbu d3,(%2);\n"       \
374                 "       add -1,a0;\n"           \
375                 "       bne 0b;\n"              \
376                 "2:;"                           \
377                 ".section .fixup,\"ax\"\n"      \
378                 "3:     jmp 2b\n"               \
379                 ".previous\n"                   \
380                 ".section __ex_table,\"a\"\n"   \
381                 "       .balign 4\n"            \
382                 "       .long 0b,3b\n"          \
383                 "       .long 1b,3b\n"          \
384                 ".previous"                     \
385                 :                               \
386                 : "d"(size), "d"(to), "d"(from) \
387                 : "d3", "a0");                  \
388 } while (0)
389
390 /* Optimize just a little bit when we know the size of the move. */
391 #define __constant_copy_user_zeroing(to, from, size)    \
392 do {                                                    \
393         asm volatile(                                   \
394                 "       mov %0,a0;\n"                   \
395                 "0:     movbu (%1),d3;\n"               \
396                 "1:     movbu d3,(%2);\n"               \
397                 "       add -1,a0;\n"                   \
398                 "       bne 0b;\n"                      \
399                 "2:;"                                   \
400                 ".section .fixup,\"ax\"\n"              \
401                 "3:     jmp 2b\n"                       \
402                 ".previous\n"                           \
403                 ".section __ex_table,\"a\"\n"           \
404                 "       .balign 4\n"                    \
405                 "       .long 0b,3b\n"                  \
406                 "       .long 1b,3b\n"                  \
407                 ".previous"                             \
408                 :                                       \
409                 : "d"(size), "d"(to), "d"(from)         \
410                 : "d3", "a0");                          \
411 } while (0)
412
413 static inline
414 unsigned long __constant_copy_to_user(void *to, const void *from,
415                                       unsigned long n)
416 {
417         if (access_ok(VERIFY_WRITE, to, n))
418                 __constant_copy_user(to, from, n);
419         return n;
420 }
421
422 static inline
423 unsigned long __constant_copy_from_user(void *to, const void *from,
424                                         unsigned long n)
425 {
426         if (access_ok(VERIFY_READ, from, n))
427                 __constant_copy_user_zeroing(to, from, n);
428         return n;
429 }
430
431 static inline
432 unsigned long __constant_copy_to_user_nocheck(void *to, const void *from,
433                                               unsigned long n)
434 {
435         __constant_copy_user(to, from, n);
436         return n;
437 }
438
439 static inline
440 unsigned long __constant_copy_from_user_nocheck(void *to, const void *from,
441                                                 unsigned long n)
442 {
443         __constant_copy_user_zeroing(to, from, n);
444         return n;
445 }
446 #endif
447
448 extern unsigned long __generic_copy_to_user(void __user *, const void *,
449                                             unsigned long);
450 extern unsigned long __generic_copy_from_user(void *, const void __user *,
451                                               unsigned long);
452
453 #define __copy_to_user_inatomic(to, from, n) \
454         __generic_copy_to_user_nocheck((to), (from), (n))
455 #define __copy_from_user_inatomic(to, from, n) \
456         __generic_copy_from_user_nocheck((to), (from), (n))
457
458 #define __copy_to_user(to, from, n)                     \
459 ({                                                      \
460         might_fault();                                  \
461         __copy_to_user_inatomic((to), (from), (n));     \
462 })
463
464 #define __copy_from_user(to, from, n)                   \
465 ({                                                      \
466         might_fault();                                  \
467         __copy_from_user_inatomic((to), (from), (n));   \
468 })
469
470
471 #define copy_to_user(to, from, n)   __generic_copy_to_user((to), (from), (n))
472 #define copy_from_user(to, from, n) __generic_copy_from_user((to), (from), (n))
473
474 extern long strncpy_from_user(char *dst, const char __user *src, long count);
475 extern long __strncpy_from_user(char *dst, const char __user *src, long count);
476 extern long strnlen_user(const char __user *str, long n);
477 #define strlen_user(str) strnlen_user(str, ~0UL >> 1)
478 extern unsigned long clear_user(void __user *mem, unsigned long len);
479 extern unsigned long __clear_user(void __user *mem, unsigned long len);
480
481 #endif /* _ASM_UACCESS_H */