]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/metag/include/asm/uaccess.h
uaccess: move VERIFY_{READ,WRITE} definitions to linux/uaccess.h
[karo-tx-linux.git] / arch / metag / include / asm / uaccess.h
1 #ifndef __METAG_UACCESS_H
2 #define __METAG_UACCESS_H
3
4 /*
5  * User space memory access functions
6  */
7 #include <linux/sched.h>
8
9 /*
10  * The fs value determines whether argument validity checking should be
11  * performed or not.  If get_fs() == USER_DS, checking is performed, with
12  * get_fs() == KERNEL_DS, checking is bypassed.
13  *
14  * For historical reasons, these macros are grossly misnamed.
15  */
16
17 #define MAKE_MM_SEG(s)  ((mm_segment_t) { (s) })
18
19 #define KERNEL_DS       MAKE_MM_SEG(0xFFFFFFFF)
20 #define USER_DS         MAKE_MM_SEG(PAGE_OFFSET)
21
22 #define get_ds()        (KERNEL_DS)
23 #define get_fs()        (current_thread_info()->addr_limit)
24 #define set_fs(x)       (current_thread_info()->addr_limit = (x))
25
26 #define segment_eq(a, b)        ((a).seg == (b).seg)
27
28 #define __kernel_ok (segment_eq(get_fs(), KERNEL_DS))
29 /*
30  * Explicitly allow NULL pointers here. Parts of the kernel such
31  * as readv/writev use access_ok to validate pointers, but want
32  * to allow NULL pointers for various reasons. NULL pointers are
33  * safe to allow through because the first page is not mappable on
34  * Meta.
35  *
36  * We also wish to avoid letting user code access the system area
37  * and the kernel half of the address space.
38  */
39 #define __user_bad(addr, size) (((addr) > 0 && (addr) < META_MEMORY_BASE) || \
40                                 ((addr) > PAGE_OFFSET &&                \
41                                  (addr) < LINCORE_BASE))
42
43 static inline int __access_ok(unsigned long addr, unsigned long size)
44 {
45         return __kernel_ok || !__user_bad(addr, size);
46 }
47
48 #define access_ok(type, addr, size) __access_ok((unsigned long)(addr),  \
49                                                 (unsigned long)(size))
50
51 static inline int verify_area(int type, const void *addr, unsigned long size)
52 {
53         return access_ok(type, addr, size) ? 0 : -EFAULT;
54 }
55
56 /*
57  * The exception table consists of pairs of addresses: the first is the
58  * address of an instruction that is allowed to fault, and the second is
59  * the address at which the program should continue.  No registers are
60  * modified, so it is entirely up to the continuation code to figure out
61  * what to do.
62  *
63  * All the routines below use bits of fixup code that are out of line
64  * with the main instruction path.  This means when everything is well,
65  * we don't even have to jump over them.  Further, they do not intrude
66  * on our cache or tlb entries.
67  */
68 struct exception_table_entry {
69         unsigned long insn, fixup;
70 };
71
72 extern int fixup_exception(struct pt_regs *regs);
73
74 /*
75  * These are the main single-value transfer routines.  They automatically
76  * use the right size if we just have the right pointer type.
77  */
78
79 #define put_user(x, ptr) \
80         __put_user_check((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
81 #define __put_user(x, ptr) \
82         __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
83
84 extern void __put_user_bad(void);
85
86 #define __put_user_nocheck(x, ptr, size)                \
87 ({                                                      \
88         long __pu_err;                                  \
89         __put_user_size((x), (ptr), (size), __pu_err);  \
90         __pu_err;                                       \
91 })
92
93 #define __put_user_check(x, ptr, size)                          \
94 ({                                                              \
95         long __pu_err = -EFAULT;                                \
96         __typeof__(*(ptr)) __user *__pu_addr = (ptr);           \
97         if (access_ok(VERIFY_WRITE, __pu_addr, size))           \
98                 __put_user_size((x), __pu_addr, (size), __pu_err);      \
99         __pu_err;                                               \
100 })
101
102 extern long __put_user_asm_b(unsigned int x, void __user *addr);
103 extern long __put_user_asm_w(unsigned int x, void __user *addr);
104 extern long __put_user_asm_d(unsigned int x, void __user *addr);
105 extern long __put_user_asm_l(unsigned long long x, void __user *addr);
106
107 #define __put_user_size(x, ptr, size, retval)                           \
108 do {                                                                    \
109         retval = 0;                                                     \
110         switch (size) {                                                 \
111         case 1:                                                         \
112                 retval = __put_user_asm_b((__force unsigned int)x, ptr);\
113                 break;                                                  \
114         case 2:                                                         \
115                 retval = __put_user_asm_w((__force unsigned int)x, ptr);\
116                 break;                                                  \
117         case 4:                                                         \
118                 retval = __put_user_asm_d((__force unsigned int)x, ptr);\
119                 break;                                                  \
120         case 8:                                                         \
121                 retval = __put_user_asm_l((__force unsigned long long)x,\
122                                           ptr);                         \
123                 break;                                                  \
124         default:                                                        \
125                 __put_user_bad();                                       \
126         }                                                               \
127 } while (0)
128
129 #define get_user(x, ptr) \
130         __get_user_check((x), (ptr), sizeof(*(ptr)))
131 #define __get_user(x, ptr) \
132         __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
133
134 extern long __get_user_bad(void);
135
136 #define __get_user_nocheck(x, ptr, size)                        \
137 ({                                                              \
138         long __gu_err, __gu_val;                                \
139         __get_user_size(__gu_val, (ptr), (size), __gu_err);     \
140         (x) = (__force __typeof__(*(ptr)))__gu_val;             \
141         __gu_err;                                               \
142 })
143
144 #define __get_user_check(x, ptr, size)                                  \
145 ({                                                                      \
146         long __gu_err = -EFAULT, __gu_val = 0;                          \
147         const __typeof__(*(ptr)) __user *__gu_addr = (ptr);             \
148         if (access_ok(VERIFY_READ, __gu_addr, size))                    \
149                 __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
150         (x) = (__force __typeof__(*(ptr)))__gu_val;                     \
151         __gu_err;                                                       \
152 })
153
154 extern unsigned char __get_user_asm_b(const void __user *addr, long *err);
155 extern unsigned short __get_user_asm_w(const void __user *addr, long *err);
156 extern unsigned int __get_user_asm_d(const void __user *addr, long *err);
157
158 #define __get_user_size(x, ptr, size, retval)                   \
159 do {                                                            \
160         retval = 0;                                             \
161         switch (size) {                                         \
162         case 1:                                                 \
163                 x = __get_user_asm_b(ptr, &retval); break;      \
164         case 2:                                                 \
165                 x = __get_user_asm_w(ptr, &retval); break;      \
166         case 4:                                                 \
167                 x = __get_user_asm_d(ptr, &retval); break;      \
168         default:                                                \
169                 (x) = __get_user_bad();                         \
170         }                                                       \
171 } while (0)
172
173 /*
174  * Copy a null terminated string from userspace.
175  *
176  * Must return:
177  * -EFAULT              for an exception
178  * count                if we hit the buffer limit
179  * bytes copied         if we hit a null byte
180  * (without the null byte)
181  */
182
183 extern long __must_check __strncpy_from_user(char *dst, const char __user *src,
184                                              long count);
185
186 #define strncpy_from_user(dst, src, count) __strncpy_from_user(dst, src, count)
187
188 /*
189  * Return the size of a string (including the ending 0)
190  *
191  * Return 0 on exception, a value greater than N if too long
192  */
193 extern long __must_check strnlen_user(const char __user *src, long count);
194
195 #define strlen_user(str) strnlen_user(str, 32767)
196
197 extern unsigned long __must_check __copy_user_zeroing(void *to,
198                                                       const void __user *from,
199                                                       unsigned long n);
200
201 static inline unsigned long
202 copy_from_user(void *to, const void __user *from, unsigned long n)
203 {
204         if (likely(access_ok(VERIFY_READ, from, n)))
205                 return __copy_user_zeroing(to, from, n);
206         memset(to, 0, n);
207         return n;
208 }
209
210 #define __copy_from_user(to, from, n) __copy_user_zeroing(to, from, n)
211 #define __copy_from_user_inatomic __copy_from_user
212
213 extern unsigned long __must_check __copy_user(void __user *to,
214                                               const void *from,
215                                               unsigned long n);
216
217 static inline unsigned long copy_to_user(void __user *to, const void *from,
218                                          unsigned long n)
219 {
220         if (access_ok(VERIFY_WRITE, to, n))
221                 return __copy_user(to, from, n);
222         return n;
223 }
224
225 #define __copy_to_user(to, from, n) __copy_user(to, from, n)
226 #define __copy_to_user_inatomic __copy_to_user
227
228 /*
229  * Zero Userspace
230  */
231
232 extern unsigned long __must_check __do_clear_user(void __user *to,
233                                                   unsigned long n);
234
235 static inline unsigned long clear_user(void __user *to, unsigned long n)
236 {
237         if (access_ok(VERIFY_WRITE, to, n))
238                 return __do_clear_user(to, n);
239         return n;
240 }
241
242 #define __clear_user(to, n)            __do_clear_user(to, n)
243
244 #endif /* _METAG_UACCESS_H */