microblaze: uaccess: Fix put_user for noMMU
[linux-2.6.git] / arch / microblaze / include / asm / uaccess.h
1 /*
2  * Copyright (C) 2008-2009 Michal Simek <monstr@monstr.eu>
3  * Copyright (C) 2008-2009 PetaLogix
4  * Copyright (C) 2006 Atmark Techno, Inc.
5  *
6  * This file is subject to the terms and conditions of the GNU General Public
7  * License. See the file "COPYING" in the main directory of this archive
8  * for more details.
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> /* RLIMIT_FSIZE */
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  * On Microblaze the fs value is actually the top of the corresponding
32  * address space.
33  *
34  * The fs value determines whether argument validity checking should be
35  * performed or not. If get_fs() == USER_DS, checking is performed, with
36  * get_fs() == KERNEL_DS, checking is bypassed.
37  *
38  * For historical reasons, these macros are grossly misnamed.
39  *
40  * For non-MMU arch like Microblaze, KERNEL_DS and USER_DS is equal.
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  * The exception table consists of pairs of addresses: the first is the
60  * address of an instruction that is allowed to fault, and the second is
61  * the address at which the program should continue. No registers are
62  * modified, so it is entirely up to the continuation code to figure out
63  * what to do.
64  *
65  * All the routines below use bits of fixup code that are out of line
66  * with the main instruction path. This means when everything is well,
67  * we don't even have to jump over them. Further, they do not intrude
68  * on our cache or tlb entries.
69  */
70 struct exception_table_entry {
71         unsigned long insn, fixup;
72 };
73
74 /* Returns 0 if exception not found and fixup otherwise.  */
75 extern unsigned long search_exception_table(unsigned long);
76
77 #ifndef CONFIG_MMU
78
79 /* Check against bounds of physical memory */
80 static inline int ___range_ok(unsigned long addr, unsigned long size)
81 {
82         return ((addr < memory_start) ||
83                 ((addr + size) > memory_end));
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  * Address is valid if:
95  *  - "addr", "addr + size" and "size" are all below the limit
96  */
97 #define access_ok(type, addr, size) \
98         (get_fs().seg > (((unsigned long)(addr)) | \
99                 (size) | ((unsigned long)(addr) + (size))))
100
101 /* || printk("access_ok failed for %s at 0x%08lx (size %d), seg 0x%08x\n",
102  type?"WRITE":"READ",addr,size,get_fs().seg)) */
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
114 extern unsigned long __copy_tofrom_user(void __user *to,
115                 const void __user *from, unsigned long size);
116
117 /* Return: number of not copied bytes, i.e. 0 if OK or non-zero if fail. */
118 static inline unsigned long __must_check __clear_user(void __user *to,
119                                                         unsigned long n)
120 {
121         /* normal memset with two words to __ex_table */
122         __asm__ __volatile__ (                          \
123                         "1:     sb      r0, %2, r0;"    \
124                         "       addik   %0, %0, -1;"    \
125                         "       bneid   %0, 1b;"        \
126                         "       addik   %2, %2, 1;"     \
127                         "2:                     "       \
128                         __EX_TABLE_SECTION              \
129                         ".word  1b,2b;"                 \
130                         ".previous;"                    \
131                 : "=r"(n)                               \
132                 : "0"(n), "r"(to)
133         );
134         return n;
135 }
136
137 static 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 #ifndef CONFIG_MMU
148
149 extern long __user_bad(void);
150
151 #define __get_user_asm(insn, __gu_ptr, __gu_val, __gu_err)      \
152 ({                                                              \
153         __asm__ __volatile__ (                                  \
154                         "1:"    insn    " %1, %2, r0;"          \
155                         "       addk    %0, r0, r0;"            \
156                         "2:                     "               \
157                         __FIXUP_SECTION                         \
158                         "3:     brid    2b;"                    \
159                         "       addik   %0, r0, %3;"            \
160                         ".previous;"                            \
161                         __EX_TABLE_SECTION                      \
162                         ".word  1b,3b;"                         \
163                         ".previous;"                            \
164                 : "=&r"(__gu_err), "=r"(__gu_val)               \
165                 : "r"(__gu_ptr), "i"(-EFAULT)                   \
166         );                                                      \
167 })
168
169 /**
170  * get_user: - Get a simple variable from user space.
171  * @x:   Variable to store result.
172  * @ptr: Source address, in user space.
173  *
174  * Context: User context only.  This function may sleep.
175  *
176  * This macro copies a single simple variable from user space to kernel
177  * space.  It supports simple types like char and int, but not larger
178  * data types like structures or arrays.
179  *
180  * @ptr must have pointer-to-simple-variable type, and the result of
181  * dereferencing @ptr must be assignable to @x without a cast.
182  *
183  * Returns zero on success, or -EFAULT on error.
184  * On error, the variable @x is set to zero.
185  */
186
187 #define __get_user(x, ptr)                                              \
188 ({                                                                      \
189         unsigned long __gu_val;                                         \
190         /*unsigned long __gu_ptr = (unsigned long)(ptr);*/              \
191         long __gu_err;                                                  \
192         switch (sizeof(*(ptr))) {                                       \
193         case 1:                                                         \
194                 __get_user_asm("lbu", (ptr), __gu_val, __gu_err);       \
195                 break;                                                  \
196         case 2:                                                         \
197                 __get_user_asm("lhu", (ptr), __gu_val, __gu_err);       \
198                 break;                                                  \
199         case 4:                                                         \
200                 __get_user_asm("lw", (ptr), __gu_val, __gu_err);        \
201                 break;                                                  \
202         default:                                                        \
203                 /* __gu_val = 0; __gu_err = -EINVAL;*/ __gu_err = __user_bad();\
204         }                                                               \
205         x = (__typeof__(*(ptr))) __gu_val;                              \
206         __gu_err;                                                       \
207 })
208
209
210 #define get_user(x, ptr)                                                \
211 ({                                                                      \
212         access_ok(VERIFY_READ, (ptr), sizeof(*(ptr)))                   \
213                 ? __get_user((x), (ptr)) : -EFAULT;                     \
214 })
215
216 #define __put_user_asm(insn, __gu_ptr, __gu_val, __gu_err)      \
217 ({                                                              \
218         __asm__ __volatile__ (                                  \
219                         "1:"    insn    " %1, %2, r0;"          \
220                         "       addk    %0, r0, r0;"            \
221                         "2:                     "               \
222                         __FIXUP_SECTION                         \
223                         "3:     brid    2b;"                    \
224                         "       addik   %0, r0, %3;"            \
225                         ".previous;"                            \
226                         __EX_TABLE_SECTION                      \
227                         ".word  1b,3b;"                         \
228                         ".previous;"                            \
229                 : "=&r"(__gu_err)                               \
230                 : "r"(__gu_val), "r"(__gu_ptr), "i"(-EFAULT)    \
231         );                                                      \
232 })
233
234 #define __put_user_asm_8(__gu_ptr, __gu_val, __gu_err)          \
235 ({                                                              \
236         __asm__ __volatile__ (" lwi     %0, %1, 0;"             \
237                         "1:     swi     %0, %2, 0;"             \
238                         "       lwi     %0, %1, 4;"             \
239                         "2:     swi     %0, %2, 4;"             \
240                         "       addk    %0, r0, r0;"            \
241                         "3:                     "               \
242                         __FIXUP_SECTION                         \
243                         "4:     brid    3b;"                    \
244                         "       addik   %0, r0, %3;"            \
245                         ".previous;"                            \
246                         __EX_TABLE_SECTION                      \
247                         ".word  1b,4b,2b,4b;"                   \
248                         ".previous;"                            \
249                 : "=&r"(__gu_err)                               \
250                 : "r"(&__gu_val), "r"(__gu_ptr), "i"(-EFAULT)   \
251                 );                                              \
252 })
253
254 #define __put_user(x, ptr)                                              \
255 ({                                                                      \
256         __typeof__(*(ptr)) volatile __gu_val = (x);                     \
257         long __gu_err = 0;                                              \
258         switch (sizeof(__gu_val)) {                                     \
259         case 1:                                                         \
260                 __put_user_asm("sb", (ptr), __gu_val, __gu_err);        \
261                 break;                                                  \
262         case 2:                                                         \
263                 __put_user_asm("sh", (ptr), __gu_val, __gu_err);        \
264                 break;                                                  \
265         case 4:                                                         \
266                 __put_user_asm("sw", (ptr), __gu_val, __gu_err);        \
267                 break;                                                  \
268         case 8:                                                         \
269                 __put_user_asm_8((ptr), __gu_val, __gu_err);            \
270                 break;                                                  \
271         default:                                                        \
272                 /*__gu_err = -EINVAL;*/ __gu_err = __user_bad();        \
273         }                                                               \
274         __gu_err;                                                       \
275 })
276
277 #define put_user(x, ptr)        __put_user((x), (ptr))
278
279 #define copy_to_user(to, from, n)       (memcpy((to), (from), (n)), 0)
280 #define copy_from_user(to, from, n)     (memcpy((to), (from), (n)), 0)
281
282 #define __copy_to_user(to, from, n)     (copy_to_user((to), (from), (n)))
283 #define __copy_from_user(to, from, n)   (copy_from_user((to), (from), (n)))
284 #define __copy_to_user_inatomic(to, from, n) \
285                         (__copy_to_user((to), (from), (n)))
286 #define __copy_from_user_inatomic(to, from, n) \
287                         (__copy_from_user((to), (from), (n)))
288
289 extern long strncpy_from_user(char *dst, const char *src, long count);
290 extern long strnlen_user(const char *src, long count);
291
292 #else /* CONFIG_MMU */
293
294 /* put_user and get_user macros */
295
296 extern long __user_bad(void);
297
298 #define __get_user_asm(insn, __gu_ptr, __gu_val, __gu_err)      \
299 ({                                                              \
300         __asm__ __volatile__ (                                  \
301                         "1:"    insn    " %1, %2, r0;"          \
302                         "       addk    %0, r0, r0;"            \
303                         "2:                     "               \
304                         __FIXUP_SECTION                         \
305                         "3:     brid    2b;     "               \
306                         "       addik   %0, r0, %3;"            \
307                         ".previous;"                            \
308                         __EX_TABLE_SECTION                      \
309                         ".word  1b,3b;"                         \
310                         ".previous;"                            \
311                 : "=&r"(__gu_err), "=r"(__gu_val)               \
312                 : "r"(__gu_ptr), "i"(-EFAULT)                   \
313         );                                                      \
314 })
315
316 #define __get_user(x, ptr)                                              \
317 ({                                                                      \
318         unsigned long __gu_val;                                         \
319         /*unsigned long __gu_ptr = (unsigned long)(ptr);*/              \
320         long __gu_err;                                                  \
321         switch (sizeof(*(ptr))) {                                       \
322         case 1:                                                         \
323                 __get_user_asm("lbu", (ptr), __gu_val, __gu_err);       \
324                 break;                                                  \
325         case 2:                                                         \
326                 __get_user_asm("lhu", (ptr), __gu_val, __gu_err);       \
327                 break;                                                  \
328         case 4:                                                         \
329                 __get_user_asm("lw", (ptr), __gu_val, __gu_err);        \
330                 break;                                                  \
331         default:                                                        \
332                 /* __gu_val = 0; __gu_err = -EINVAL;*/ __gu_err = __user_bad();\
333         }                                                               \
334         x = (__typeof__(*(ptr))) __gu_val;                              \
335         __gu_err;                                                       \
336 })
337
338 /**
339  * get_user: - Get a simple variable from user space.
340  * @x:   Variable to store result.
341  * @ptr: Source address, in user space.
342  *
343  * Context: User context only.  This function may sleep.
344  *
345  * This macro copies a single simple variable from user space to kernel
346  * space.  It supports simple types like char and int, but not larger
347  * data types like structures or arrays.
348  *
349  * @ptr must have pointer-to-simple-variable type, and the result of
350  * dereferencing @ptr must be assignable to @x without a cast.
351  *
352  * Returns zero on success, or -EFAULT on error.
353  * On error, the variable @x is set to zero.
354  */
355
356 #define get_user(x, ptr)                                                \
357 ({                                                                      \
358         access_ok(VERIFY_READ, (ptr), sizeof(*(ptr)))                   \
359                 ? __get_user((x), (ptr)) : -EFAULT;                     \
360 })
361
362 /**
363  * put_user: - Write a simple value into user space.
364  * @x:   Value to copy to user space.
365  * @ptr: Destination address, in user space.
366  *
367  * Context: User context only.  This function may sleep.
368  *
369  * This macro copies a single simple value from kernel space to user
370  * space.  It supports simple types like char and int, but not larger
371  * data types like structures or arrays.
372  *
373  * @ptr must have pointer-to-simple-variable type, and @x must be assignable
374  * to the result of dereferencing @ptr.
375  *
376  * Returns zero on success, or -EFAULT on error.
377  */
378
379 #define __put_user_asm(insn, __gu_ptr, __gu_val, __gu_err)      \
380 ({                                                              \
381         __asm__ __volatile__ (                                  \
382                         "1:"    insn    " %1, %2, r0;"          \
383                         "       addk    %0, r0, r0;"            \
384                         "2:                     "               \
385                         __FIXUP_SECTION                         \
386                         "3:     brid    2b;"                    \
387                         "       addik   %0, r0, %3;"            \
388                         ".previous;"                            \
389                         __EX_TABLE_SECTION                      \
390                         ".word  1b,3b;"                         \
391                         ".previous;"                            \
392                 : "=&r"(__gu_err)                               \
393                 : "r"(__gu_val), "r"(__gu_ptr), "i"(-EFAULT)    \
394         );                                                      \
395 })
396
397 #define __put_user_asm_8(__gu_ptr, __gu_val, __gu_err)          \
398 ({                                                              \
399         __asm__ __volatile__ (" lwi     %0, %1, 0;"             \
400                         "1:     swi     %0, %2, 0;"             \
401                         "       lwi     %0, %1, 4;"             \
402                         "2:     swi     %0, %2, 4;"             \
403                         "       addk    %0, r0, r0;"            \
404                         "3:                             "       \
405                         __FIXUP_SECTION                         \
406                         "4:     brid    3b;"                    \
407                         "       addik   %0, r0, %3;"            \
408                         ".previous;"                            \
409                         __EX_TABLE_SECTION                      \
410                         ".word  1b,4b,2b,4b;"                   \
411                         ".previous;"                            \
412                 : "=&r"(__gu_err)                               \
413                 : "r"(&__gu_val), "r"(__gu_ptr), "i"(-EFAULT)   \
414                 );                                              \
415 })
416
417 #define __put_user(x, ptr)                                              \
418 ({                                                                      \
419         __typeof__(*(ptr)) volatile __gu_val = (x);                     \
420         long __gu_err = 0;                                              \
421         switch (sizeof(__gu_val)) {                                     \
422         case 1:                                                         \
423                 __put_user_asm("sb", (ptr), __gu_val, __gu_err);        \
424                 break;                                                  \
425         case 2:                                                         \
426                 __put_user_asm("sh", (ptr), __gu_val, __gu_err);        \
427                 break;                                                  \
428         case 4:                                                         \
429                 __put_user_asm("sw", (ptr), __gu_val, __gu_err);        \
430                 break;                                                  \
431         case 8:                                                         \
432                 __put_user_asm_8((ptr), __gu_val, __gu_err);            \
433                 break;                                                  \
434         default:                                                        \
435                 /*__gu_err = -EINVAL;*/ __gu_err = __user_bad();        \
436         }                                                               \
437         __gu_err;                                                       \
438 })
439
440 #define put_user(x, ptr)                                                \
441 ({                                                                      \
442         access_ok(VERIFY_WRITE, (ptr), sizeof(*(ptr)))                  \
443                 ? __put_user((x), (ptr)) : -EFAULT;                     \
444 })
445
446 #define __copy_from_user(to, from, n)   \
447         __copy_tofrom_user((__force void __user *)(to), \
448                                 (void __user *)(from), (n))
449 #define __copy_from_user_inatomic(to, from, n) \
450                 copy_from_user((to), (from), (n))
451
452 static inline long copy_from_user(void *to,
453                 const void __user *from, unsigned long n)
454 {
455         might_sleep();
456         if (access_ok(VERIFY_READ, from, n))
457                 return __copy_from_user(to, from, n);
458         else
459                 return n;
460 }
461
462 #define __copy_to_user(to, from, n)     \
463                 __copy_tofrom_user((void __user *)(to), \
464                         (__force const void __user *)(from), (n))
465 #define __copy_to_user_inatomic(to, from, n)    copy_to_user((to), (from), (n))
466
467 static inline long copy_to_user(void __user *to,
468                 const void *from, unsigned long n)
469 {
470         might_sleep();
471         if (access_ok(VERIFY_WRITE, to, n))
472                 return __copy_to_user(to, from, n);
473         else
474                 return n;
475 }
476
477 extern int __strncpy_user(char *to, const char __user *from, int len);
478
479 #define __strncpy_from_user     __strncpy_user
480
481 static inline long
482 strncpy_from_user(char *dst, const char __user *src, long count)
483 {
484         if (!access_ok(VERIFY_READ, src, 1))
485                 return -EFAULT;
486         return __strncpy_from_user(dst, src, count);
487 }
488
489 extern int __strnlen_user(const char __user *sstr, int len);
490
491 #define strnlen_user(str, len)  \
492                 (access_ok(VERIFY_READ, str, 1) ? __strnlen_user(str, len) : 0)
493
494 #endif /* CONFIG_MMU */
495
496 #endif  /* __ASSEMBLY__ */
497 #endif /* __KERNEL__ */
498
499 #endif /* _ASM_MICROBLAZE_UACCESS_H */