1
2
3
4
5
6
7
8#ifndef __ASM_AVR32_UACCESS_H
9#define __ASM_AVR32_UACCESS_H
10
11#include <linux/errno.h>
12#include <linux/sched.h>
13
14#define VERIFY_READ 0
15#define VERIFY_WRITE 1
16
17typedef struct {
18 unsigned int is_user_space;
19} mm_segment_t;
20
21
22
23
24
25
26
27
28#define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
29#define segment_eq(a, b) ((a).is_user_space == (b).is_user_space)
30
31#define USER_ADDR_LIMIT 0x80000000
32
33#define KERNEL_DS MAKE_MM_SEG(0)
34#define USER_DS MAKE_MM_SEG(1)
35
36#define get_ds() (KERNEL_DS)
37
38static inline mm_segment_t get_fs(void)
39{
40 return MAKE_MM_SEG(test_thread_flag(TIF_USERSPACE));
41}
42
43static inline void set_fs(mm_segment_t s)
44{
45 if (s.is_user_space)
46 set_thread_flag(TIF_USERSPACE);
47 else
48 clear_thread_flag(TIF_USERSPACE);
49}
50
51
52
53
54
55
56
57
58
59
60
61
62
63#define __range_ok(addr, size) \
64 (test_thread_flag(TIF_USERSPACE) \
65 && (((unsigned long)(addr) >= 0x80000000) \
66 || ((unsigned long)(size) > 0x80000000) \
67 || (((unsigned long)(addr) + (unsigned long)(size)) > 0x80000000)))
68
69#define access_ok(type, addr, size) (likely(__range_ok(addr, size) == 0))
70
71
72extern __kernel_size_t __copy_user(void *to, const void *from,
73 __kernel_size_t n);
74
75extern __kernel_size_t copy_to_user(void __user *to, const void *from,
76 __kernel_size_t n);
77extern __kernel_size_t ___copy_from_user(void *to, const void __user *from,
78 __kernel_size_t n);
79
80static inline __kernel_size_t __copy_to_user(void __user *to, const void *from,
81 __kernel_size_t n)
82{
83 return __copy_user((void __force *)to, from, n);
84}
85static inline __kernel_size_t __copy_from_user(void *to,
86 const void __user *from,
87 __kernel_size_t n)
88{
89 return __copy_user(to, (const void __force *)from, n);
90}
91static inline __kernel_size_t copy_from_user(void *to,
92 const void __user *from,
93 __kernel_size_t n)
94{
95 size_t res = ___copy_from_user(to, from, n);
96 if (unlikely(res))
97 memset(to + (n - res), 0, res);
98 return res;
99}
100
101#define __copy_to_user_inatomic __copy_to_user
102#define __copy_from_user_inatomic __copy_from_user
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121#define put_user(x, ptr) \
122 __put_user_check((x), (ptr), sizeof(*(ptr)))
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142#define get_user(x, ptr) \
143 __get_user_check((x), (ptr), sizeof(*(ptr)))
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165#define __put_user(x, ptr) \
166 __put_user_nocheck((x), (ptr), sizeof(*(ptr)))
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189#define __get_user(x, ptr) \
190 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
191
192extern int __get_user_bad(void);
193extern int __put_user_bad(void);
194
195#define __get_user_nocheck(x, ptr, size) \
196({ \
197 unsigned long __gu_val = 0; \
198 int __gu_err = 0; \
199 \
200 switch (size) { \
201 case 1: __get_user_asm("ub", __gu_val, ptr, __gu_err); break; \
202 case 2: __get_user_asm("uh", __gu_val, ptr, __gu_err); break; \
203 case 4: __get_user_asm("w", __gu_val, ptr, __gu_err); break; \
204 default: __gu_err = __get_user_bad(); break; \
205 } \
206 \
207 x = (__force typeof(*(ptr)))__gu_val; \
208 __gu_err; \
209})
210
211#define __get_user_check(x, ptr, size) \
212({ \
213 unsigned long __gu_val = 0; \
214 const typeof(*(ptr)) __user * __gu_addr = (ptr); \
215 int __gu_err = 0; \
216 \
217 if (access_ok(VERIFY_READ, __gu_addr, size)) { \
218 switch (size) { \
219 case 1: \
220 __get_user_asm("ub", __gu_val, __gu_addr, \
221 __gu_err); \
222 break; \
223 case 2: \
224 __get_user_asm("uh", __gu_val, __gu_addr, \
225 __gu_err); \
226 break; \
227 case 4: \
228 __get_user_asm("w", __gu_val, __gu_addr, \
229 __gu_err); \
230 break; \
231 default: \
232 __gu_err = __get_user_bad(); \
233 break; \
234 } \
235 } else { \
236 __gu_err = -EFAULT; \
237 } \
238 x = (__force typeof(*(ptr)))__gu_val; \
239 __gu_err; \
240})
241
242#define __get_user_asm(suffix, __gu_val, ptr, __gu_err) \
243 asm volatile( \
244 "1: ld." suffix " %1, %3 \n" \
245 "2: \n" \
246 " .subsection 1 \n" \
247 "3: mov %0, %4 \n" \
248 " rjmp 2b \n" \
249 " .subsection 0 \n" \
250 " .section __ex_table, \"a\" \n" \
251 " .long 1b, 3b \n" \
252 " .previous \n" \
253 : "=r"(__gu_err), "=r"(__gu_val) \
254 : "0"(__gu_err), "m"(*(ptr)), "i"(-EFAULT))
255
256#define __put_user_nocheck(x, ptr, size) \
257({ \
258 typeof(*(ptr)) __pu_val; \
259 int __pu_err = 0; \
260 \
261 __pu_val = (x); \
262 switch (size) { \
263 case 1: __put_user_asm("b", ptr, __pu_val, __pu_err); break; \
264 case 2: __put_user_asm("h", ptr, __pu_val, __pu_err); break; \
265 case 4: __put_user_asm("w", ptr, __pu_val, __pu_err); break; \
266 case 8: __put_user_asm("d", ptr, __pu_val, __pu_err); break; \
267 default: __pu_err = __put_user_bad(); break; \
268 } \
269 __pu_err; \
270})
271
272#define __put_user_check(x, ptr, size) \
273({ \
274 typeof(*(ptr)) __pu_val; \
275 typeof(*(ptr)) __user *__pu_addr = (ptr); \
276 int __pu_err = 0; \
277 \
278 __pu_val = (x); \
279 if (access_ok(VERIFY_WRITE, __pu_addr, size)) { \
280 switch (size) { \
281 case 1: \
282 __put_user_asm("b", __pu_addr, __pu_val, \
283 __pu_err); \
284 break; \
285 case 2: \
286 __put_user_asm("h", __pu_addr, __pu_val, \
287 __pu_err); \
288 break; \
289 case 4: \
290 __put_user_asm("w", __pu_addr, __pu_val, \
291 __pu_err); \
292 break; \
293 case 8: \
294 __put_user_asm("d", __pu_addr, __pu_val, \
295 __pu_err); \
296 break; \
297 default: \
298 __pu_err = __put_user_bad(); \
299 break; \
300 } \
301 } else { \
302 __pu_err = -EFAULT; \
303 } \
304 __pu_err; \
305})
306
307#define __put_user_asm(suffix, ptr, __pu_val, __gu_err) \
308 asm volatile( \
309 "1: st." suffix " %1, %3 \n" \
310 "2: \n" \
311 " .subsection 1 \n" \
312 "3: mov %0, %4 \n" \
313 " rjmp 2b \n" \
314 " .subsection 0 \n" \
315 " .section __ex_table, \"a\" \n" \
316 " .long 1b, 3b \n" \
317 " .previous \n" \
318 : "=r"(__gu_err), "=m"(*(ptr)) \
319 : "0"(__gu_err), "r"(__pu_val), "i"(-EFAULT))
320
321extern __kernel_size_t clear_user(void __user *addr, __kernel_size_t size);
322extern __kernel_size_t __clear_user(void __user *addr, __kernel_size_t size);
323
324extern long strncpy_from_user(char *dst, const char __user *src, long count);
325extern long __strncpy_from_user(char *dst, const char __user *src, long count);
326
327extern long strnlen_user(const char __user *__s, long __n);
328extern long __strnlen_user(const char __user *__s, long __n);
329
330#define strlen_user(s) strnlen_user(s, ~0UL >> 1)
331
332struct exception_table_entry
333{
334 unsigned long insn, fixup;
335};
336
337#endif
338