Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / arch / tile / include / asm / uaccess.h
1 /*
2  * Copyright 2010 Tilera Corporation. All Rights Reserved.
3  *
4  *   This program is free software; you can redistribute it and/or
5  *   modify it under the terms of the GNU General Public License
6  *   as published by the Free Software Foundation, version 2.
7  *
8  *   This program is distributed in the hope that it will be useful, but
9  *   WITHOUT ANY WARRANTY; without even the implied warranty of
10  *   MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11  *   NON INFRINGEMENT.  See the GNU General Public License for
12  *   more details.
13  */
14
15 #ifndef _ASM_TILE_UACCESS_H
16 #define _ASM_TILE_UACCESS_H
17
18 /*
19  * User space memory access functions
20  */
21 #include <linux/sched.h>
22 #include <linux/mm.h>
23 #include <asm-generic/uaccess-unaligned.h>
24 #include <asm/processor.h>
25 #include <asm/page.h>
26
27 #define VERIFY_READ     0
28 #define VERIFY_WRITE    1
29
30 /*
31  * The fs value determines whether argument validity checking should be
32  * performed or not.  If get_fs() == USER_DS, checking is performed, with
33  * get_fs() == KERNEL_DS, checking is bypassed.
34  *
35  * For historical reasons, these macros are grossly misnamed.
36  */
37 #define MAKE_MM_SEG(a)  ((mm_segment_t) { (a) })
38
39 #define KERNEL_DS       MAKE_MM_SEG(-1UL)
40 #define USER_DS         MAKE_MM_SEG(PAGE_OFFSET)
41
42 #define get_ds()        (KERNEL_DS)
43 #define get_fs()        (current_thread_info()->addr_limit)
44 #define set_fs(x)       (current_thread_info()->addr_limit = (x))
45
46 #define segment_eq(a, b) ((a).seg == (b).seg)
47
48 #ifndef __tilegx__
49 /*
50  * We could allow mapping all 16 MB at 0xfc000000, but we set up a
51  * special hack in arch_setup_additional_pages() to auto-create a mapping
52  * for the first 16 KB, and it would seem strange to have different
53  * user-accessible semantics for memory at 0xfc000000 and above 0xfc004000.
54  */
55 static inline int is_arch_mappable_range(unsigned long addr,
56                                          unsigned long size)
57 {
58         return (addr >= MEM_USER_INTRPT &&
59                 addr < (MEM_USER_INTRPT + INTRPT_SIZE) &&
60                 size <= (MEM_USER_INTRPT + INTRPT_SIZE) - addr);
61 }
62 #define is_arch_mappable_range is_arch_mappable_range
63 #else
64 #define is_arch_mappable_range(addr, size) 0
65 #endif
66
67 /*
68  * Test whether a block of memory is a valid user space address.
69  * Returns 0 if the range is valid, nonzero otherwise.
70  */
71 int __range_ok(unsigned long addr, unsigned long size);
72
73 /**
74  * access_ok: - Checks if a user space pointer is valid
75  * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE.  Note that
76  *        %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
77  *        to write to a block, it is always safe to read from it.
78  * @addr: User space pointer to start of block to check
79  * @size: Size of block to check
80  *
81  * Context: User context only. This function may sleep if pagefaults are
82  *          enabled.
83  *
84  * Checks if a pointer to a block of memory in user space is valid.
85  *
86  * Returns true (nonzero) if the memory block may be valid, false (zero)
87  * if it is definitely invalid.
88  *
89  * Note that, depending on architecture, this function probably just
90  * checks that the pointer is in the user space range - after calling
91  * this function, memory access functions may still return -EFAULT.
92  */
93 #define access_ok(type, addr, size) ({ \
94         __chk_user_ptr(addr); \
95         likely(__range_ok((unsigned long)(addr), (size)) == 0); \
96 })
97
98 /*
99  * The exception table consists of pairs of addresses: the first is the
100  * address of an instruction that is allowed to fault, and the second is
101  * the address at which the program should continue.  No registers are
102  * modified, so it is entirely up to the continuation code to figure out
103  * what to do.
104  *
105  * All the routines below use bits of fixup code that are out of line
106  * with the main instruction path.  This means when everything is well,
107  * we don't even have to jump over them.  Further, they do not intrude
108  * on our cache or tlb entries.
109  */
110
111 struct exception_table_entry {
112         unsigned long insn, fixup;
113 };
114
115 extern int fixup_exception(struct pt_regs *regs);
116
117 /*
118  * This is a type: either unsigned long, if the argument fits into
119  * that type, or otherwise unsigned long long.
120  */
121 #define __inttype(x) \
122         __typeof__(__builtin_choose_expr(sizeof(x) > sizeof(0UL), 0ULL, 0UL))
123
124 /*
125  * Support macros for __get_user().
126  * Note that __get_user() and __put_user() assume proper alignment.
127  */
128
129 #ifdef __LP64__
130 #define _ASM_PTR        ".quad"
131 #define _ASM_ALIGN      ".align 8"
132 #else
133 #define _ASM_PTR        ".long"
134 #define _ASM_ALIGN      ".align 4"
135 #endif
136
137 #define __get_user_asm(OP, x, ptr, ret)                                 \
138         asm volatile("1: {" #OP " %1, %2; movei %0, 0 }\n"              \
139                      ".pushsection .fixup,\"ax\"\n"                     \
140                      "0: { movei %1, 0; movei %0, %3 }\n"               \
141                      "j 9f\n"                                           \
142                      ".section __ex_table,\"a\"\n"                      \
143                      _ASM_ALIGN "\n"                                    \
144                      _ASM_PTR " 1b, 0b\n"                               \
145                      ".popsection\n"                                    \
146                      "9:"                                               \
147                      : "=r" (ret), "=r" (x)                             \
148                      : "r" (ptr), "i" (-EFAULT))
149
150 #ifdef __tilegx__
151 #define __get_user_1(x, ptr, ret) __get_user_asm(ld1u, x, ptr, ret)
152 #define __get_user_2(x, ptr, ret) __get_user_asm(ld2u, x, ptr, ret)
153 #define __get_user_4(x, ptr, ret) __get_user_asm(ld4s, x, ptr, ret)
154 #define __get_user_8(x, ptr, ret) __get_user_asm(ld, x, ptr, ret)
155 #else
156 #define __get_user_1(x, ptr, ret) __get_user_asm(lb_u, x, ptr, ret)
157 #define __get_user_2(x, ptr, ret) __get_user_asm(lh_u, x, ptr, ret)
158 #define __get_user_4(x, ptr, ret) __get_user_asm(lw, x, ptr, ret)
159 #ifdef __LITTLE_ENDIAN
160 #define __lo32(a, b) a
161 #define __hi32(a, b) b
162 #else
163 #define __lo32(a, b) b
164 #define __hi32(a, b) a
165 #endif
166 #define __get_user_8(x, ptr, ret)                                       \
167         ({                                                              \
168                 unsigned int __a, __b;                                  \
169                 asm volatile("1: { lw %1, %3; addi %2, %3, 4 }\n"       \
170                              "2: { lw %2, %2; movei %0, 0 }\n"          \
171                              ".pushsection .fixup,\"ax\"\n"             \
172                              "0: { movei %1, 0; movei %2, 0 }\n"        \
173                              "{ movei %0, %4; j 9f }\n"                 \
174                              ".section __ex_table,\"a\"\n"              \
175                              ".align 4\n"                               \
176                              ".word 1b, 0b\n"                           \
177                              ".word 2b, 0b\n"                           \
178                              ".popsection\n"                            \
179                              "9:"                                       \
180                              : "=r" (ret), "=r" (__a), "=&r" (__b)      \
181                              : "r" (ptr), "i" (-EFAULT));               \
182                 (x) = (__force __typeof(x))(__inttype(x))               \
183                         (((u64)__hi32(__a, __b) << 32) |                \
184                          __lo32(__a, __b));                             \
185         })
186 #endif
187
188 extern int __get_user_bad(void)
189   __attribute__((warning("sizeof __get_user argument not 1, 2, 4 or 8")));
190
191 /**
192  * __get_user: - Get a simple variable from user space, with less checking.
193  * @x:   Variable to store result.
194  * @ptr: Source address, in user space.
195  *
196  * Context: User context only. This function may sleep if pagefaults are
197  *          enabled.
198  *
199  * This macro copies a single simple variable from user space to kernel
200  * space.  It supports simple types like char and int, but not larger
201  * data types like structures or arrays.
202  *
203  * @ptr must have pointer-to-simple-variable type, and the result of
204  * dereferencing @ptr must be assignable to @x without a cast.
205  *
206  * Returns zero on success, or -EFAULT on error.
207  * On error, the variable @x is set to zero.
208  *
209  * Caller must check the pointer with access_ok() before calling this
210  * function.
211  */
212 #define __get_user(x, ptr)                                              \
213         ({                                                              \
214                 int __ret;                                              \
215                 typeof(x) _x;                                           \
216                 __chk_user_ptr(ptr);                                    \
217                 switch (sizeof(*(ptr))) {                               \
218                 case 1: __get_user_1(_x, ptr, __ret); break;            \
219                 case 2: __get_user_2(_x, ptr, __ret); break;            \
220                 case 4: __get_user_4(_x, ptr, __ret); break;            \
221                 case 8: __get_user_8(_x, ptr, __ret); break;            \
222                 default: __ret = __get_user_bad(); break;               \
223                 }                                                       \
224                 (x) = (typeof(*(ptr))) _x;                              \
225                 __ret;                                                  \
226         })
227
228 /* Support macros for __put_user(). */
229
230 #define __put_user_asm(OP, x, ptr, ret)                 \
231         asm volatile("1: {" #OP " %1, %2; movei %0, 0 }\n"              \
232                      ".pushsection .fixup,\"ax\"\n"                     \
233                      "0: { movei %0, %3; j 9f }\n"                      \
234                      ".section __ex_table,\"a\"\n"                      \
235                      _ASM_ALIGN "\n"                                    \
236                      _ASM_PTR " 1b, 0b\n"                               \
237                      ".popsection\n"                                    \
238                      "9:"                                               \
239                      : "=r" (ret)                                       \
240                      : "r" (ptr), "r" (x), "i" (-EFAULT))
241
242 #ifdef __tilegx__
243 #define __put_user_1(x, ptr, ret) __put_user_asm(st1, x, ptr, ret)
244 #define __put_user_2(x, ptr, ret) __put_user_asm(st2, x, ptr, ret)
245 #define __put_user_4(x, ptr, ret) __put_user_asm(st4, x, ptr, ret)
246 #define __put_user_8(x, ptr, ret) __put_user_asm(st, x, ptr, ret)
247 #else
248 #define __put_user_1(x, ptr, ret) __put_user_asm(sb, x, ptr, ret)
249 #define __put_user_2(x, ptr, ret) __put_user_asm(sh, x, ptr, ret)
250 #define __put_user_4(x, ptr, ret) __put_user_asm(sw, x, ptr, ret)
251 #define __put_user_8(x, ptr, ret)                                       \
252         ({                                                              \
253                 u64 __x = (__force __inttype(x))(x);                    \
254                 int __lo = (int) __x, __hi = (int) (__x >> 32);         \
255                 asm volatile("1: { sw %1, %2; addi %0, %1, 4 }\n"       \
256                              "2: { sw %0, %3; movei %0, 0 }\n"          \
257                              ".pushsection .fixup,\"ax\"\n"             \
258                              "0: { movei %0, %4; j 9f }\n"              \
259                              ".section __ex_table,\"a\"\n"              \
260                              ".align 4\n"                               \
261                              ".word 1b, 0b\n"                           \
262                              ".word 2b, 0b\n"                           \
263                              ".popsection\n"                            \
264                              "9:"                                       \
265                              : "=&r" (ret)                              \
266                              : "r" (ptr), "r" (__lo32(__lo, __hi)),     \
267                              "r" (__hi32(__lo, __hi)), "i" (-EFAULT));  \
268         })
269 #endif
270
271 extern int __put_user_bad(void)
272   __attribute__((warning("sizeof __put_user argument not 1, 2, 4 or 8")));
273
274 /**
275  * __put_user: - Write a simple value into user space, with less checking.
276  * @x:   Value to copy to user space.
277  * @ptr: Destination address, in user space.
278  *
279  * Context: User context only. This function may sleep if pagefaults are
280  *          enabled.
281  *
282  * This macro copies a single simple value from kernel space to user
283  * space.  It supports simple types like char and int, but not larger
284  * data types like structures or arrays.
285  *
286  * @ptr must have pointer-to-simple-variable type, and @x must be assignable
287  * to the result of dereferencing @ptr.
288  *
289  * Caller must check the pointer with access_ok() before calling this
290  * function.
291  *
292  * Returns zero on success, or -EFAULT on error.
293  */
294 #define __put_user(x, ptr)                                              \
295 ({                                                                      \
296         int __ret;                                                      \
297         typeof(*(ptr)) _x = (x);                                        \
298         __chk_user_ptr(ptr);                                            \
299         switch (sizeof(*(ptr))) {                                       \
300         case 1: __put_user_1(_x, ptr, __ret); break;                    \
301         case 2: __put_user_2(_x, ptr, __ret); break;                    \
302         case 4: __put_user_4(_x, ptr, __ret); break;                    \
303         case 8: __put_user_8(_x, ptr, __ret); break;                    \
304         default: __ret = __put_user_bad(); break;                       \
305         }                                                               \
306         __ret;                                                          \
307 })
308
309 /*
310  * The versions of get_user and put_user without initial underscores
311  * check the address of their arguments to make sure they are not
312  * in kernel space.
313  */
314 #define put_user(x, ptr)                                                \
315 ({                                                                      \
316         __typeof__(*(ptr)) __user *__Pu_addr = (ptr);                   \
317         access_ok(VERIFY_WRITE, (__Pu_addr), sizeof(*(__Pu_addr))) ?    \
318                 __put_user((x), (__Pu_addr)) :                          \
319                 -EFAULT;                                                \
320 })
321
322 #define get_user(x, ptr)                                                \
323 ({                                                                      \
324         __typeof__(*(ptr)) const __user *__Gu_addr = (ptr);             \
325         access_ok(VERIFY_READ, (__Gu_addr), sizeof(*(__Gu_addr))) ?     \
326                 __get_user((x), (__Gu_addr)) :                          \
327                 ((x) = 0, -EFAULT);                                     \
328 })
329
330 /**
331  * __copy_to_user() - copy data into user space, with less checking.
332  * @to:   Destination address, in user space.
333  * @from: Source address, in kernel space.
334  * @n:    Number of bytes to copy.
335  *
336  * Context: User context only. This function may sleep if pagefaults are
337  *          enabled.
338  *
339  * Copy data from kernel space to user space.  Caller must check
340  * the specified block with access_ok() before calling this function.
341  *
342  * Returns number of bytes that could not be copied.
343  * On success, this will be zero.
344  *
345  * An alternate version - __copy_to_user_inatomic() - is designed
346  * to be called from atomic context, typically bracketed by calls
347  * to pagefault_disable() and pagefault_enable().
348  */
349 extern unsigned long __must_check __copy_to_user_inatomic(
350         void __user *to, const void *from, unsigned long n);
351
352 static inline unsigned long __must_check
353 __copy_to_user(void __user *to, const void *from, unsigned long n)
354 {
355         might_fault();
356         return __copy_to_user_inatomic(to, from, n);
357 }
358
359 static inline unsigned long __must_check
360 copy_to_user(void __user *to, const void *from, unsigned long n)
361 {
362         if (access_ok(VERIFY_WRITE, to, n))
363                 n = __copy_to_user(to, from, n);
364         return n;
365 }
366
367 /**
368  * __copy_from_user() - copy data from user space, with less checking.
369  * @to:   Destination address, in kernel space.
370  * @from: Source address, in user space.
371  * @n:    Number of bytes to copy.
372  *
373  * Context: User context only. This function may sleep if pagefaults are
374  *          enabled.
375  *
376  * Copy data from user space to kernel space.  Caller must check
377  * the specified block with access_ok() before calling this function.
378  *
379  * Returns number of bytes that could not be copied.
380  * On success, this will be zero.
381  *
382  * If some data could not be copied, this function will pad the copied
383  * data to the requested size using zero bytes.
384  *
385  * An alternate version - __copy_from_user_inatomic() - is designed
386  * to be called from atomic context, typically bracketed by calls
387  * to pagefault_disable() and pagefault_enable().  This version
388  * does *NOT* pad with zeros.
389  */
390 extern unsigned long __must_check __copy_from_user_inatomic(
391         void *to, const void __user *from, unsigned long n);
392 extern unsigned long __must_check __copy_from_user_zeroing(
393         void *to, const void __user *from, unsigned long n);
394
395 static inline unsigned long __must_check
396 __copy_from_user(void *to, const void __user *from, unsigned long n)
397 {
398        might_fault();
399        return __copy_from_user_zeroing(to, from, n);
400 }
401
402 static inline unsigned long __must_check
403 _copy_from_user(void *to, const void __user *from, unsigned long n)
404 {
405         if (access_ok(VERIFY_READ, from, n))
406                 n = __copy_from_user(to, from, n);
407         else
408                 memset(to, 0, n);
409         return n;
410 }
411
412 #ifdef CONFIG_DEBUG_STRICT_USER_COPY_CHECKS
413 /*
414  * There are still unprovable places in the generic code as of 2.6.34, so this
415  * option is not really compatible with -Werror, which is more useful in
416  * general.
417  */
418 extern void copy_from_user_overflow(void)
419         __compiletime_warning("copy_from_user() size is not provably correct");
420
421 static inline unsigned long __must_check copy_from_user(void *to,
422                                           const void __user *from,
423                                           unsigned long n)
424 {
425         int sz = __compiletime_object_size(to);
426
427         if (likely(sz == -1 || sz >= n))
428                 n = _copy_from_user(to, from, n);
429         else
430                 copy_from_user_overflow();
431
432         return n;
433 }
434 #else
435 #define copy_from_user _copy_from_user
436 #endif
437
438 #ifdef __tilegx__
439 /**
440  * __copy_in_user() - copy data within user space, with less checking.
441  * @to:   Destination address, in user space.
442  * @from: Source address, in user space.
443  * @n:    Number of bytes to copy.
444  *
445  * Context: User context only. This function may sleep if pagefaults are
446  *          enabled.
447  *
448  * Copy data from user space to user space.  Caller must check
449  * the specified blocks with access_ok() before calling this function.
450  *
451  * Returns number of bytes that could not be copied.
452  * On success, this will be zero.
453  */
454 extern unsigned long __copy_in_user_inatomic(
455         void __user *to, const void __user *from, unsigned long n);
456
457 static inline unsigned long __must_check
458 __copy_in_user(void __user *to, const void __user *from, unsigned long n)
459 {
460         might_fault();
461         return __copy_in_user_inatomic(to, from, n);
462 }
463
464 static inline unsigned long __must_check
465 copy_in_user(void __user *to, const void __user *from, unsigned long n)
466 {
467         if (access_ok(VERIFY_WRITE, to, n) && access_ok(VERIFY_READ, from, n))
468                 n = __copy_in_user(to, from, n);
469         return n;
470 }
471 #endif
472
473
474 /**
475  * strlen_user: - Get the size of a string in user space.
476  * @str: The string to measure.
477  *
478  * Context: User context only.  This function may sleep.
479  *
480  * Get the size of a NUL-terminated string in user space.
481  *
482  * Returns the size of the string INCLUDING the terminating NUL.
483  * On exception, returns 0.
484  *
485  * If there is a limit on the length of a valid string, you may wish to
486  * consider using strnlen_user() instead.
487  */
488 extern long strnlen_user_asm(const char __user *str, long n);
489 static inline long __must_check strnlen_user(const char __user *str, long n)
490 {
491         might_fault();
492         return strnlen_user_asm(str, n);
493 }
494 #define strlen_user(str) strnlen_user(str, LONG_MAX)
495
496 /**
497  * strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
498  * @dst:   Destination address, in kernel space.  This buffer must be at
499  *         least @count bytes long.
500  * @src:   Source address, in user space.
501  * @count: Maximum number of bytes to copy, including the trailing NUL.
502  *
503  * Copies a NUL-terminated string from userspace to kernel space.
504  * Caller must check the specified block with access_ok() before calling
505  * this function.
506  *
507  * On success, returns the length of the string (not including the trailing
508  * NUL).
509  *
510  * If access to userspace fails, returns -EFAULT (some data may have been
511  * copied).
512  *
513  * If @count is smaller than the length of the string, copies @count bytes
514  * and returns @count.
515  */
516 extern long strncpy_from_user_asm(char *dst, const char __user *src, long);
517 static inline long __must_check __strncpy_from_user(
518         char *dst, const char __user *src, long count)
519 {
520         might_fault();
521         return strncpy_from_user_asm(dst, src, count);
522 }
523 static inline long __must_check strncpy_from_user(
524         char *dst, const char __user *src, long count)
525 {
526         if (access_ok(VERIFY_READ, src, 1))
527                 return __strncpy_from_user(dst, src, count);
528         return -EFAULT;
529 }
530
531 /**
532  * clear_user: - Zero a block of memory in user space.
533  * @mem:   Destination address, in user space.
534  * @len:   Number of bytes to zero.
535  *
536  * Zero a block of memory in user space.
537  *
538  * Returns number of bytes that could not be cleared.
539  * On success, this will be zero.
540  */
541 extern unsigned long clear_user_asm(void __user *mem, unsigned long len);
542 static inline unsigned long __must_check __clear_user(
543         void __user *mem, unsigned long len)
544 {
545         might_fault();
546         return clear_user_asm(mem, len);
547 }
548 static inline unsigned long __must_check clear_user(
549         void __user *mem, unsigned long len)
550 {
551         if (access_ok(VERIFY_WRITE, mem, len))
552                 return __clear_user(mem, len);
553         return len;
554 }
555
556 /**
557  * flush_user: - Flush a block of memory in user space from cache.
558  * @mem:   Destination address, in user space.
559  * @len:   Number of bytes to flush.
560  *
561  * Returns number of bytes that could not be flushed.
562  * On success, this will be zero.
563  */
564 extern unsigned long flush_user_asm(void __user *mem, unsigned long len);
565 static inline unsigned long __must_check __flush_user(
566         void __user *mem, unsigned long len)
567 {
568         int retval;
569
570         might_fault();
571         retval = flush_user_asm(mem, len);
572         mb_incoherent();
573         return retval;
574 }
575
576 static inline unsigned long __must_check flush_user(
577         void __user *mem, unsigned long len)
578 {
579         if (access_ok(VERIFY_WRITE, mem, len))
580                 return __flush_user(mem, len);
581         return len;
582 }
583
584 /**
585  * finv_user: - Flush-inval a block of memory in user space from cache.
586  * @mem:   Destination address, in user space.
587  * @len:   Number of bytes to invalidate.
588  *
589  * Returns number of bytes that could not be flush-invalidated.
590  * On success, this will be zero.
591  */
592 extern unsigned long finv_user_asm(void __user *mem, unsigned long len);
593 static inline unsigned long __must_check __finv_user(
594         void __user *mem, unsigned long len)
595 {
596         int retval;
597
598         might_fault();
599         retval = finv_user_asm(mem, len);
600         mb_incoherent();
601         return retval;
602 }
603 static inline unsigned long __must_check finv_user(
604         void __user *mem, unsigned long len)
605 {
606         if (access_ok(VERIFY_WRITE, mem, len))
607                 return __finv_user(mem, len);
608         return len;
609 }
610
611 #endif /* _ASM_TILE_UACCESS_H */