1
2
3
4
5
6
7
8
9
10
11#ifndef _ASM_MICROBLAZE_UACCESS_H
12#define _ASM_MICROBLAZE_UACCESS_H
13
14#ifdef __KERNEL__
15#ifndef __ASSEMBLY__
16
17#include <linux/kernel.h>
18#include <linux/errno.h>
19#include <linux/sched.h>
20#include <linux/mm.h>
21
22#include <asm/mmu.h>
23#include <asm/page.h>
24#include <asm/pgtable.h>
25#include <linux/string.h>
26
27#define VERIFY_READ 0
28#define VERIFY_WRITE 1
29
30
31
32
33
34
35
36
37
38
39
40
41
42# define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
43
44# ifndef CONFIG_MMU
45# define KERNEL_DS MAKE_MM_SEG(0)
46# define USER_DS KERNEL_DS
47# else
48# define KERNEL_DS MAKE_MM_SEG(0xFFFFFFFF)
49# define USER_DS MAKE_MM_SEG(TASK_SIZE - 1)
50# endif
51
52# define get_ds() (KERNEL_DS)
53# define get_fs() (current_thread_info()->addr_limit)
54# define set_fs(val) (current_thread_info()->addr_limit = (val))
55
56# define segment_eq(a, b) ((a).seg == (b).seg)
57
58
59
60
61
62
63
64
65
66
67
68
69
70struct exception_table_entry {
71 unsigned long insn, fixup;
72};
73
74
75extern unsigned long search_exception_table(unsigned long);
76
77#ifndef CONFIG_MMU
78
79
80static inline int ___range_ok(unsigned long addr, unsigned long size)
81{
82 return ((addr < memory_start) ||
83 ((addr + size - 1) > (memory_start + memory_size - 1)));
84}
85
86#define __range_ok(addr, size) \
87 ___range_ok((unsigned long)(addr), (unsigned long)(size))
88
89#define access_ok(type, addr, size) (__range_ok((addr), (size)) == 0)
90
91#else
92
93
94
95
96
97#define access_ok(type, addr, size) \
98 (get_fs().seg >= (((unsigned long)(addr)) | \
99 (size) | ((unsigned long)(addr) + (size))))
100
101
102
103
104#endif
105
106#ifdef CONFIG_MMU
107# define __FIXUP_SECTION ".section .fixup,\"ax\"\n"
108# define __EX_TABLE_SECTION ".section __ex_table,\"a\"\n"
109#else
110# define __FIXUP_SECTION ".section .discard,\"ax\"\n"
111# define __EX_TABLE_SECTION ".section .discard,\"a\"\n"
112#endif
113
114extern unsigned long __copy_tofrom_user(void __user *to,
115 const void __user *from, unsigned long size);
116
117
118static inline unsigned long __must_check __clear_user(void __user *to,
119 unsigned long n)
120{
121
122 __asm__ __volatile__ ( \
123 "1: sb r0, %1, r0;" \
124 " addik %0, %0, -1;" \
125 " bneid %0, 1b;" \
126 " addik %1, %1, 1;" \
127 "2: " \
128 __EX_TABLE_SECTION \
129 ".word 1b,2b;" \
130 ".previous;" \
131 : "=r"(n), "=r"(to) \
132 : "0"(n), "1"(to)
133 );
134 return n;
135}
136
137static inline unsigned long __must_check clear_user(void __user *to,
138 unsigned long n)
139{
140 might_sleep();
141 if (unlikely(!access_ok(VERIFY_WRITE, to, n)))
142 return n;
143
144 return __clear_user(to, n);
145}
146
147
148extern long __user_bad(void);
149
150#define __get_user_asm(insn, __gu_ptr, __gu_val, __gu_err) \
151({ \
152 __asm__ __volatile__ ( \
153 "1:" insn " %1, %2, r0;" \
154 " addk %0, r0, r0;" \
155 "2: " \
156 __FIXUP_SECTION \
157 "3: brid 2b;" \
158 " addik %0, r0, %3;" \
159 ".previous;" \
160 __EX_TABLE_SECTION \
161 ".word 1b,3b;" \
162 ".previous;" \
163 : "=&r"(__gu_err), "=r"(__gu_val) \
164 : "r"(__gu_ptr), "i"(-EFAULT) \
165 ); \
166})
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185#define get_user(x, ptr) \
186 __get_user_check((x), (ptr), sizeof(*(ptr)))
187
188#define __get_user_check(x, ptr, size) \
189({ \
190 unsigned long __gu_val = 0; \
191 const typeof(*(ptr)) __user *__gu_addr = (ptr); \
192 int __gu_err = 0; \
193 \
194 if (access_ok(VERIFY_READ, __gu_addr, size)) { \
195 switch (size) { \
196 case 1: \
197 __get_user_asm("lbu", __gu_addr, __gu_val, \
198 __gu_err); \
199 break; \
200 case 2: \
201 __get_user_asm("lhu", __gu_addr, __gu_val, \
202 __gu_err); \
203 break; \
204 case 4: \
205 __get_user_asm("lw", __gu_addr, __gu_val, \
206 __gu_err); \
207 break; \
208 default: \
209 __gu_err = __user_bad(); \
210 break; \
211 } \
212 } else { \
213 __gu_err = -EFAULT; \
214 } \
215 x = (typeof(*(ptr)))__gu_val; \
216 __gu_err; \
217})
218
219#define __get_user(x, ptr) \
220({ \
221 unsigned long __gu_val; \
222 \
223 long __gu_err; \
224 switch (sizeof(*(ptr))) { \
225 case 1: \
226 __get_user_asm("lbu", (ptr), __gu_val, __gu_err); \
227 break; \
228 case 2: \
229 __get_user_asm("lhu", (ptr), __gu_val, __gu_err); \
230 break; \
231 case 4: \
232 __get_user_asm("lw", (ptr), __gu_val, __gu_err); \
233 break; \
234 default: \
235 __gu_err = __user_bad();\
236 } \
237 x = (__typeof__(*(ptr))) __gu_val; \
238 __gu_err; \
239})
240
241
242#define __put_user_asm(insn, __gu_ptr, __gu_val, __gu_err) \
243({ \
244 __asm__ __volatile__ ( \
245 "1:" insn " %1, %2, r0;" \
246 " addk %0, r0, r0;" \
247 "2: " \
248 __FIXUP_SECTION \
249 "3: brid 2b;" \
250 " addik %0, r0, %3;" \
251 ".previous;" \
252 __EX_TABLE_SECTION \
253 ".word 1b,3b;" \
254 ".previous;" \
255 : "=&r"(__gu_err) \
256 : "r"(__gu_val), "r"(__gu_ptr), "i"(-EFAULT) \
257 ); \
258})
259
260#define __put_user_asm_8(__gu_ptr, __gu_val, __gu_err) \
261({ \
262 __asm__ __volatile__ (" lwi %0, %1, 0;" \
263 "1: swi %0, %2, 0;" \
264 " lwi %0, %1, 4;" \
265 "2: swi %0, %2, 4;" \
266 " addk %0, r0, r0;" \
267 "3: " \
268 __FIXUP_SECTION \
269 "4: brid 3b;" \
270 " addik %0, r0, %3;" \
271 ".previous;" \
272 __EX_TABLE_SECTION \
273 ".word 1b,4b,2b,4b;" \
274 ".previous;" \
275 : "=&r"(__gu_err) \
276 : "r"(&__gu_val), "r"(__gu_ptr), "i"(-EFAULT) \
277 ); \
278})
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296#define put_user(x, ptr) \
297 __put_user_check((x), (ptr), sizeof(*(ptr)))
298
299#define __put_user_check(x, ptr, size) \
300({ \
301 typeof(*(ptr)) __pu_val; \
302 typeof(*(ptr)) __user *__pu_addr = (ptr); \
303 int __pu_err = 0; \
304 \
305 __pu_val = (x); \
306 if (access_ok(VERIFY_WRITE, __pu_addr, size)) { \
307 switch (size) { \
308 case 1: \
309 __put_user_asm("sb", __pu_addr, __pu_val, \
310 __pu_err); \
311 break; \
312 case 2: \
313 __put_user_asm("sh", __pu_addr, __pu_val, \
314 __pu_err); \
315 break; \
316 case 4: \
317 __put_user_asm("sw", __pu_addr, __pu_val, \
318 __pu_err); \
319 break; \
320 case 8: \
321 __put_user_asm_8(__pu_addr, __pu_val, __pu_err);\
322 break; \
323 default: \
324 __pu_err = __user_bad(); \
325 break; \
326 } \
327 } else { \
328 __pu_err = -EFAULT; \
329 } \
330 __pu_err; \
331})
332
333#define __put_user(x, ptr) \
334({ \
335 __typeof__(*(ptr)) volatile __gu_val = (x); \
336 long __gu_err = 0; \
337 switch (sizeof(__gu_val)) { \
338 case 1: \
339 __put_user_asm("sb", (ptr), __gu_val, __gu_err); \
340 break; \
341 case 2: \
342 __put_user_asm("sh", (ptr), __gu_val, __gu_err); \
343 break; \
344 case 4: \
345 __put_user_asm("sw", (ptr), __gu_val, __gu_err); \
346 break; \
347 case 8: \
348 __put_user_asm_8((ptr), __gu_val, __gu_err); \
349 break; \
350 default: \
351 __gu_err = __user_bad(); \
352 } \
353 __gu_err; \
354})
355
356
357
358#define __copy_from_user(to, from, n) \
359 __copy_tofrom_user((__force void __user *)(to), \
360 (void __user *)(from), (n))
361#define __copy_from_user_inatomic(to, from, n) \
362 __copy_from_user((to), (from), (n))
363
364static inline long copy_from_user(void *to,
365 const void __user *from, unsigned long n)
366{
367 might_sleep();
368 if (access_ok(VERIFY_READ, from, n))
369 return __copy_from_user(to, from, n);
370 return n;
371}
372
373#define __copy_to_user(to, from, n) \
374 __copy_tofrom_user((void __user *)(to), \
375 (__force const void __user *)(from), (n))
376#define __copy_to_user_inatomic(to, from, n) __copy_to_user((to), (from), (n))
377
378static inline long copy_to_user(void __user *to,
379 const void *from, unsigned long n)
380{
381 might_sleep();
382 if (access_ok(VERIFY_WRITE, to, n))
383 return __copy_to_user(to, from, n);
384 return n;
385}
386
387
388
389
390extern int __strncpy_user(char *to, const char __user *from, int len);
391
392#define __strncpy_from_user __strncpy_user
393
394static inline long
395strncpy_from_user(char *dst, const char __user *src, long count)
396{
397 if (!access_ok(VERIFY_READ, src, 1))
398 return -EFAULT;
399 return __strncpy_from_user(dst, src, count);
400}
401
402
403
404
405
406
407extern int __strnlen_user(const char __user *sstr, int len);
408
409static inline long strnlen_user(const char __user *src, long n)
410{
411 if (!access_ok(VERIFY_READ, src, 1))
412 return 0;
413 return __strnlen_user(src, n);
414}
415
416#endif
417#endif
418
419#endif
420