MIPS: Make uaccess.h slightly more sparse friendly.
[firefly-linux-kernel-4.4.55.git] / arch / mips / include / asm / uaccess.h
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (C) 1996, 1997, 1998, 1999, 2000, 03, 04 by Ralf Baechle
7  * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
8  * Copyright (C) 2007  Maciej W. Rozycki
9  */
10 #ifndef _ASM_UACCESS_H
11 #define _ASM_UACCESS_H
12
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/thread_info.h>
16
17 /*
18  * The fs value determines whether argument validity checking should be
19  * performed or not.  If get_fs() == USER_DS, checking is performed, with
20  * get_fs() == KERNEL_DS, checking is bypassed.
21  *
22  * For historical reasons, these macros are grossly misnamed.
23  */
24 #ifdef CONFIG_32BIT
25
26 #define __UA_LIMIT      0x80000000UL
27
28 #define __UA_ADDR       ".word"
29 #define __UA_LA         "la"
30 #define __UA_ADDU       "addu"
31 #define __UA_t0         "$8"
32 #define __UA_t1         "$9"
33
34 #endif /* CONFIG_32BIT */
35
36 #ifdef CONFIG_64BIT
37
38 #define __UA_LIMIT      (- TASK_SIZE)
39
40 #define __UA_ADDR       ".dword"
41 #define __UA_LA         "dla"
42 #define __UA_ADDU       "daddu"
43 #define __UA_t0         "$12"
44 #define __UA_t1         "$13"
45
46 #endif /* CONFIG_64BIT */
47
48 /*
49  * USER_DS is a bitmask that has the bits set that may not be set in a valid
50  * userspace address.  Note that we limit 32-bit userspace to 0x7fff8000 but
51  * the arithmetic we're doing only works if the limit is a power of two, so
52  * we use 0x80000000 here on 32-bit kernels.  If a process passes an invalid
53  * address in this range it's the process's problem, not ours :-)
54  */
55
56 #define KERNEL_DS       ((mm_segment_t) { 0UL })
57 #define USER_DS         ((mm_segment_t) { __UA_LIMIT })
58
59 #define VERIFY_READ    0
60 #define VERIFY_WRITE   1
61
62 #define get_ds()        (KERNEL_DS)
63 #define get_fs()        (current_thread_info()->addr_limit)
64 #define set_fs(x)       (current_thread_info()->addr_limit = (x))
65
66 #define segment_eq(a, b)        ((a).seg == (b).seg)
67
68
69 /*
70  * Is a address valid? This does a straighforward calculation rather
71  * than tests.
72  *
73  * Address valid if:
74  *  - "addr" doesn't have any high-bits set
75  *  - AND "size" doesn't have any high-bits set
76  *  - AND "addr+size" doesn't have any high-bits set
77  *  - OR we are in kernel mode.
78  *
79  * __ua_size() is a trick to avoid runtime checking of positive constant
80  * sizes; for those we already know at compile time that the size is ok.
81  */
82 #define __ua_size(size)                                                 \
83         ((__builtin_constant_p(size) && (signed long) (size) > 0) ? 0 : (size))
84
85 /*
86  * access_ok: - Checks if a user space pointer is valid
87  * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE.  Note that
88  *        %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
89  *        to write to a block, it is always safe to read from it.
90  * @addr: User space pointer to start of block to check
91  * @size: Size of block to check
92  *
93  * Context: User context only.  This function may sleep.
94  *
95  * Checks if a pointer to a block of memory in user space is valid.
96  *
97  * Returns true (nonzero) if the memory block may be valid, false (zero)
98  * if it is definitely invalid.
99  *
100  * Note that, depending on architecture, this function probably just
101  * checks that the pointer is in the user space range - after calling
102  * this function, memory access functions may still return -EFAULT.
103  */
104
105 #define __access_mask get_fs().seg
106
107 #define __access_ok(addr, size, mask)                                   \
108 ({                                                                      \
109         unsigned long __addr = (unsigned long) (addr);                  \
110         unsigned long __size = size;                                    \
111         unsigned long __mask = mask;                                    \
112         unsigned long __ok;                                             \
113                                                                         \
114         __chk_user_ptr(addr);                                           \
115         __ok = (signed long)(__mask & (__addr | (__addr + __size) |     \
116                 __ua_size(__size)));                                    \
117         __ok == 0;                                                      \
118 })
119
120 #define access_ok(type, addr, size)                                     \
121         likely(__access_ok((addr), (size), __access_mask))
122
123 /*
124  * put_user: - Write a simple value into user space.
125  * @x:   Value to copy to user space.
126  * @ptr: Destination address, in user space.
127  *
128  * Context: User context only.  This function may sleep.
129  *
130  * This macro copies a single simple value from kernel space to user
131  * space.  It supports simple types like char and int, but not larger
132  * data types like structures or arrays.
133  *
134  * @ptr must have pointer-to-simple-variable type, and @x must be assignable
135  * to the result of dereferencing @ptr.
136  *
137  * Returns zero on success, or -EFAULT on error.
138  */
139 #define put_user(x,ptr) \
140         __put_user_check((x), (ptr), sizeof(*(ptr)))
141
142 /*
143  * get_user: - Get a simple variable from user space.
144  * @x:   Variable to store result.
145  * @ptr: Source address, in user space.
146  *
147  * Context: User context only.  This function may sleep.
148  *
149  * This macro copies a single simple variable from user space to kernel
150  * space.  It supports simple types like char and int, but not larger
151  * data types like structures or arrays.
152  *
153  * @ptr must have pointer-to-simple-variable type, and the result of
154  * dereferencing @ptr must be assignable to @x without a cast.
155  *
156  * Returns zero on success, or -EFAULT on error.
157  * On error, the variable @x is set to zero.
158  */
159 #define get_user(x,ptr) \
160         __get_user_check((x), (ptr), sizeof(*(ptr)))
161
162 /*
163  * __put_user: - Write a simple value into user space, with less checking.
164  * @x:   Value to copy to user space.
165  * @ptr: Destination address, in user space.
166  *
167  * Context: User context only.  This function may sleep.
168  *
169  * This macro copies a single simple value from kernel space to user
170  * space.  It supports simple types like char and int, but not larger
171  * data types like structures or arrays.
172  *
173  * @ptr must have pointer-to-simple-variable type, and @x must be assignable
174  * to the result of dereferencing @ptr.
175  *
176  * Caller must check the pointer with access_ok() before calling this
177  * function.
178  *
179  * Returns zero on success, or -EFAULT on error.
180  */
181 #define __put_user(x,ptr) \
182         __put_user_nocheck((x), (ptr), sizeof(*(ptr)))
183
184 /*
185  * __get_user: - Get a simple variable from user space, with less checking.
186  * @x:   Variable to store result.
187  * @ptr: Source address, in user space.
188  *
189  * Context: User context only.  This function may sleep.
190  *
191  * This macro copies a single simple variable from user space to kernel
192  * space.  It supports simple types like char and int, but not larger
193  * data types like structures or arrays.
194  *
195  * @ptr must have pointer-to-simple-variable type, and the result of
196  * dereferencing @ptr must be assignable to @x without a cast.
197  *
198  * Caller must check the pointer with access_ok() before calling this
199  * function.
200  *
201  * Returns zero on success, or -EFAULT on error.
202  * On error, the variable @x is set to zero.
203  */
204 #define __get_user(x,ptr) \
205         __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
206
207 struct __large_struct { unsigned long buf[100]; };
208 #define __m(x) (*(struct __large_struct __user *)(x))
209
210 /*
211  * Yuck.  We need two variants, one for 64bit operation and one
212  * for 32 bit mode and old iron.
213  */
214 #ifdef CONFIG_32BIT
215 #define __GET_USER_DW(val, ptr) __get_user_asm_ll32(val, ptr)
216 #endif
217 #ifdef CONFIG_64BIT
218 #define __GET_USER_DW(val, ptr) __get_user_asm(val, "ld", ptr)
219 #endif
220
221 extern void __get_user_unknown(void);
222
223 #define __get_user_common(val, size, ptr)                               \
224 do {                                                                    \
225         switch (size) {                                                 \
226         case 1: __get_user_asm(val, "lb", ptr); break;                  \
227         case 2: __get_user_asm(val, "lh", ptr); break;                  \
228         case 4: __get_user_asm(val, "lw", ptr); break;                  \
229         case 8: __GET_USER_DW(val, ptr); break;                         \
230         default: __get_user_unknown(); break;                           \
231         }                                                               \
232 } while (0)
233
234 #define __get_user_nocheck(x, ptr, size)                                \
235 ({                                                                      \
236         int __gu_err;                                                   \
237                                                                         \
238         __chk_user_ptr(ptr);                                            \
239         __get_user_common((x), size, ptr);                              \
240         __gu_err;                                                       \
241 })
242
243 #define __get_user_check(x, ptr, size)                                  \
244 ({                                                                      \
245         int __gu_err = -EFAULT;                                         \
246         const __typeof__(*(ptr)) __user * __gu_ptr = (ptr);             \
247                                                                         \
248         if (likely(access_ok(VERIFY_READ,  __gu_ptr, size)))            \
249                 __get_user_common((x), size, __gu_ptr);                 \
250                                                                         \
251         __gu_err;                                                       \
252 })
253
254 #define __get_user_asm(val, insn, addr)                                 \
255 {                                                                       \
256         long __gu_tmp;                                                  \
257                                                                         \
258         __asm__ __volatile__(                                           \
259         "1:     " insn "        %1, %3                          \n"     \
260         "2:                                                     \n"     \
261         "       .section .fixup,\"ax\"                          \n"     \
262         "3:     li      %0, %4                                  \n"     \
263         "       j       2b                                      \n"     \
264         "       .previous                                       \n"     \
265         "       .section __ex_table,\"a\"                       \n"     \
266         "       "__UA_ADDR "\t1b, 3b                            \n"     \
267         "       .previous                                       \n"     \
268         : "=r" (__gu_err), "=r" (__gu_tmp)                              \
269         : "0" (0), "o" (__m(addr)), "i" (-EFAULT));                     \
270                                                                         \
271         (val) = (__typeof__(*(addr))) __gu_tmp;                         \
272 }
273
274 /*
275  * Get a long long 64 using 32 bit registers.
276  */
277 #define __get_user_asm_ll32(val, addr)                                  \
278 {                                                                       \
279         union {                                                         \
280                 unsigned long long      l;                              \
281                 __typeof__(*(addr))     t;                              \
282         } __gu_tmp;                                                     \
283                                                                         \
284         __asm__ __volatile__(                                           \
285         "1:     lw      %1, (%3)                                \n"     \
286         "2:     lw      %D1, 4(%3)                              \n"     \
287         "3:     .section        .fixup,\"ax\"                   \n"     \
288         "4:     li      %0, %4                                  \n"     \
289         "       move    %1, $0                                  \n"     \
290         "       move    %D1, $0                                 \n"     \
291         "       j       3b                                      \n"     \
292         "       .previous                                       \n"     \
293         "       .section        __ex_table,\"a\"                \n"     \
294         "       " __UA_ADDR "   1b, 4b                          \n"     \
295         "       " __UA_ADDR "   2b, 4b                          \n"     \
296         "       .previous                                       \n"     \
297         : "=r" (__gu_err), "=&r" (__gu_tmp.l)                           \
298         : "0" (0), "r" (addr), "i" (-EFAULT));                          \
299                                                                         \
300         (val) = __gu_tmp.t;                                             \
301 }
302
303 /*
304  * Yuck.  We need two variants, one for 64bit operation and one
305  * for 32 bit mode and old iron.
306  */
307 #ifdef CONFIG_32BIT
308 #define __PUT_USER_DW(ptr) __put_user_asm_ll32(ptr)
309 #endif
310 #ifdef CONFIG_64BIT
311 #define __PUT_USER_DW(ptr) __put_user_asm("sd", ptr)
312 #endif
313
314 #define __put_user_nocheck(x, ptr, size)                                \
315 ({                                                                      \
316         __typeof__(*(ptr)) __pu_val;                                    \
317         int __pu_err = 0;                                               \
318                                                                         \
319         __chk_user_ptr(ptr);                                            \
320         __pu_val = (x);                                                 \
321         switch (size) {                                                 \
322         case 1: __put_user_asm("sb", ptr); break;                       \
323         case 2: __put_user_asm("sh", ptr); break;                       \
324         case 4: __put_user_asm("sw", ptr); break;                       \
325         case 8: __PUT_USER_DW(ptr); break;                              \
326         default: __put_user_unknown(); break;                           \
327         }                                                               \
328         __pu_err;                                                       \
329 })
330
331 #define __put_user_check(x, ptr, size)                                  \
332 ({                                                                      \
333         __typeof__(*(ptr)) __user *__pu_addr = (ptr);                   \
334         __typeof__(*(ptr)) __pu_val = (x);                              \
335         int __pu_err = -EFAULT;                                         \
336                                                                         \
337         if (likely(access_ok(VERIFY_WRITE,  __pu_addr, size))) {        \
338                 switch (size) {                                         \
339                 case 1: __put_user_asm("sb", __pu_addr); break;         \
340                 case 2: __put_user_asm("sh", __pu_addr); break;         \
341                 case 4: __put_user_asm("sw", __pu_addr); break;         \
342                 case 8: __PUT_USER_DW(__pu_addr); break;                \
343                 default: __put_user_unknown(); break;                   \
344                 }                                                       \
345         }                                                               \
346         __pu_err;                                                       \
347 })
348
349 #define __put_user_asm(insn, ptr)                                       \
350 {                                                                       \
351         __asm__ __volatile__(                                           \
352         "1:     " insn "        %z2, %3         # __put_user_asm\n"     \
353         "2:                                                     \n"     \
354         "       .section        .fixup,\"ax\"                   \n"     \
355         "3:     li      %0, %4                                  \n"     \
356         "       j       2b                                      \n"     \
357         "       .previous                                       \n"     \
358         "       .section        __ex_table,\"a\"                \n"     \
359         "       " __UA_ADDR "   1b, 3b                          \n"     \
360         "       .previous                                       \n"     \
361         : "=r" (__pu_err)                                               \
362         : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)),                     \
363           "i" (-EFAULT));                                               \
364 }
365
366 #define __put_user_asm_ll32(ptr)                                        \
367 {                                                                       \
368         __asm__ __volatile__(                                           \
369         "1:     sw      %2, (%3)        # __put_user_asm_ll32   \n"     \
370         "2:     sw      %D2, 4(%3)                              \n"     \
371         "3:                                                     \n"     \
372         "       .section        .fixup,\"ax\"                   \n"     \
373         "4:     li      %0, %4                                  \n"     \
374         "       j       3b                                      \n"     \
375         "       .previous                                       \n"     \
376         "       .section        __ex_table,\"a\"                \n"     \
377         "       " __UA_ADDR "   1b, 4b                          \n"     \
378         "       " __UA_ADDR "   2b, 4b                          \n"     \
379         "       .previous"                                              \
380         : "=r" (__pu_err)                                               \
381         : "0" (0), "r" (__pu_val), "r" (ptr),                           \
382           "i" (-EFAULT));                                               \
383 }
384
385 extern void __put_user_unknown(void);
386
387 /*
388  * put_user_unaligned: - Write a simple value into user space.
389  * @x:   Value to copy to user space.
390  * @ptr: Destination address, in user space.
391  *
392  * Context: User context only.  This function may sleep.
393  *
394  * This macro copies a single simple value from kernel space to user
395  * space.  It supports simple types like char and int, but not larger
396  * data types like structures or arrays.
397  *
398  * @ptr must have pointer-to-simple-variable type, and @x must be assignable
399  * to the result of dereferencing @ptr.
400  *
401  * Returns zero on success, or -EFAULT on error.
402  */
403 #define put_user_unaligned(x,ptr)       \
404         __put_user_unaligned_check((x),(ptr),sizeof(*(ptr)))
405
406 /*
407  * get_user_unaligned: - Get a simple variable from user space.
408  * @x:   Variable to store result.
409  * @ptr: Source address, in user space.
410  *
411  * Context: User context only.  This function may sleep.
412  *
413  * This macro copies a single simple variable from user space to kernel
414  * space.  It supports simple types like char and int, but not larger
415  * data types like structures or arrays.
416  *
417  * @ptr must have pointer-to-simple-variable type, and the result of
418  * dereferencing @ptr must be assignable to @x without a cast.
419  *
420  * Returns zero on success, or -EFAULT on error.
421  * On error, the variable @x is set to zero.
422  */
423 #define get_user_unaligned(x,ptr) \
424         __get_user_unaligned_check((x),(ptr),sizeof(*(ptr)))
425
426 /*
427  * __put_user_unaligned: - Write a simple value into user space, with less checking.
428  * @x:   Value to copy to user space.
429  * @ptr: Destination address, in user space.
430  *
431  * Context: User context only.  This function may sleep.
432  *
433  * This macro copies a single simple value from kernel space to user
434  * space.  It supports simple types like char and int, but not larger
435  * data types like structures or arrays.
436  *
437  * @ptr must have pointer-to-simple-variable type, and @x must be assignable
438  * to the result of dereferencing @ptr.
439  *
440  * Caller must check the pointer with access_ok() before calling this
441  * function.
442  *
443  * Returns zero on success, or -EFAULT on error.
444  */
445 #define __put_user_unaligned(x,ptr) \
446         __put_user_unaligned_nocheck((x),(ptr),sizeof(*(ptr)))
447
448 /*
449  * __get_user_unaligned: - Get a simple variable from user space, with less checking.
450  * @x:   Variable to store result.
451  * @ptr: Source address, in user space.
452  *
453  * Context: User context only.  This function may sleep.
454  *
455  * This macro copies a single simple variable from user space to kernel
456  * space.  It supports simple types like char and int, but not larger
457  * data types like structures or arrays.
458  *
459  * @ptr must have pointer-to-simple-variable type, and the result of
460  * dereferencing @ptr must be assignable to @x without a cast.
461  *
462  * Caller must check the pointer with access_ok() before calling this
463  * function.
464  *
465  * Returns zero on success, or -EFAULT on error.
466  * On error, the variable @x is set to zero.
467  */
468 #define __get_user_unaligned(x,ptr) \
469         __get_user__unalignednocheck((x),(ptr),sizeof(*(ptr)))
470
471 /*
472  * Yuck.  We need two variants, one for 64bit operation and one
473  * for 32 bit mode and old iron.
474  */
475 #ifdef CONFIG_32BIT
476 #define __GET_USER_UNALIGNED_DW(val, ptr)                               \
477         __get_user_unaligned_asm_ll32(val, ptr)
478 #endif
479 #ifdef CONFIG_64BIT
480 #define __GET_USER_UNALIGNED_DW(val, ptr)                               \
481         __get_user_unaligned_asm(val, "uld", ptr)
482 #endif
483
484 extern void __get_user_unaligned_unknown(void);
485
486 #define __get_user_unaligned_common(val, size, ptr)                     \
487 do {                                                                    \
488         switch (size) {                                                 \
489         case 1: __get_user_asm(val, "lb", ptr); break;                  \
490         case 2: __get_user_unaligned_asm(val, "ulh", ptr); break;       \
491         case 4: __get_user_unaligned_asm(val, "ulw", ptr); break;       \
492         case 8: __GET_USER_UNALIGNED_DW(val, ptr); break;               \
493         default: __get_user_unaligned_unknown(); break;                 \
494         }                                                               \
495 } while (0)
496
497 #define __get_user_unaligned_nocheck(x,ptr,size)                        \
498 ({                                                                      \
499         int __gu_err;                                                   \
500                                                                         \
501         __get_user_unaligned_common((x), size, ptr);                    \
502         __gu_err;                                                       \
503 })
504
505 #define __get_user_unaligned_check(x,ptr,size)                          \
506 ({                                                                      \
507         int __gu_err = -EFAULT;                                         \
508         const __typeof__(*(ptr)) __user * __gu_ptr = (ptr);             \
509                                                                         \
510         if (likely(access_ok(VERIFY_READ,  __gu_ptr, size)))            \
511                 __get_user_unaligned_common((x), size, __gu_ptr);       \
512                                                                         \
513         __gu_err;                                                       \
514 })
515
516 #define __get_user_unaligned_asm(val, insn, addr)                       \
517 {                                                                       \
518         long __gu_tmp;                                                  \
519                                                                         \
520         __asm__ __volatile__(                                           \
521         "1:     " insn "        %1, %3                          \n"     \
522         "2:                                                     \n"     \
523         "       .section .fixup,\"ax\"                          \n"     \
524         "3:     li      %0, %4                                  \n"     \
525         "       j       2b                                      \n"     \
526         "       .previous                                       \n"     \
527         "       .section __ex_table,\"a\"                       \n"     \
528         "       "__UA_ADDR "\t1b, 3b                            \n"     \
529         "       "__UA_ADDR "\t1b + 4, 3b                        \n"     \
530         "       .previous                                       \n"     \
531         : "=r" (__gu_err), "=r" (__gu_tmp)                              \
532         : "0" (0), "o" (__m(addr)), "i" (-EFAULT));                     \
533                                                                         \
534         (val) = (__typeof__(*(addr))) __gu_tmp;                         \
535 }
536
537 /*
538  * Get a long long 64 using 32 bit registers.
539  */
540 #define __get_user_unaligned_asm_ll32(val, addr)                        \
541 {                                                                       \
542         unsigned long long __gu_tmp;                                    \
543                                                                         \
544         __asm__ __volatile__(                                           \
545         "1:     ulw     %1, (%3)                                \n"     \
546         "2:     ulw     %D1, 4(%3)                              \n"     \
547         "       move    %0, $0                                  \n"     \
548         "3:     .section        .fixup,\"ax\"                   \n"     \
549         "4:     li      %0, %4                                  \n"     \
550         "       move    %1, $0                                  \n"     \
551         "       move    %D1, $0                                 \n"     \
552         "       j       3b                                      \n"     \
553         "       .previous                                       \n"     \
554         "       .section        __ex_table,\"a\"                \n"     \
555         "       " __UA_ADDR "   1b, 4b                          \n"     \
556         "       " __UA_ADDR "   1b + 4, 4b                      \n"     \
557         "       " __UA_ADDR "   2b, 4b                          \n"     \
558         "       " __UA_ADDR "   2b + 4, 4b                      \n"     \
559         "       .previous                                       \n"     \
560         : "=r" (__gu_err), "=&r" (__gu_tmp)                             \
561         : "0" (0), "r" (addr), "i" (-EFAULT));                          \
562         (val) = (__typeof__(*(addr))) __gu_tmp;                         \
563 }
564
565 /*
566  * Yuck.  We need two variants, one for 64bit operation and one
567  * for 32 bit mode and old iron.
568  */
569 #ifdef CONFIG_32BIT
570 #define __PUT_USER_UNALIGNED_DW(ptr) __put_user_unaligned_asm_ll32(ptr)
571 #endif
572 #ifdef CONFIG_64BIT
573 #define __PUT_USER_UNALIGNED_DW(ptr) __put_user_unaligned_asm("usd", ptr)
574 #endif
575
576 #define __put_user_unaligned_nocheck(x,ptr,size)                        \
577 ({                                                                      \
578         __typeof__(*(ptr)) __pu_val;                                    \
579         int __pu_err = 0;                                               \
580                                                                         \
581         __pu_val = (x);                                                 \
582         switch (size) {                                                 \
583         case 1: __put_user_asm("sb", ptr); break;                       \
584         case 2: __put_user_unaligned_asm("ush", ptr); break;            \
585         case 4: __put_user_unaligned_asm("usw", ptr); break;            \
586         case 8: __PUT_USER_UNALIGNED_DW(ptr); break;                    \
587         default: __put_user_unaligned_unknown(); break;                 \
588         }                                                               \
589         __pu_err;                                                       \
590 })
591
592 #define __put_user_unaligned_check(x,ptr,size)                          \
593 ({                                                                      \
594         __typeof__(*(ptr)) __user *__pu_addr = (ptr);                   \
595         __typeof__(*(ptr)) __pu_val = (x);                              \
596         int __pu_err = -EFAULT;                                         \
597                                                                         \
598         if (likely(access_ok(VERIFY_WRITE,  __pu_addr, size))) {        \
599                 switch (size) {                                         \
600                 case 1: __put_user_asm("sb", __pu_addr); break;         \
601                 case 2: __put_user_unaligned_asm("ush", __pu_addr); break; \
602                 case 4: __put_user_unaligned_asm("usw", __pu_addr); break; \
603                 case 8: __PUT_USER_UNALGINED_DW(__pu_addr); break;      \
604                 default: __put_user_unaligned_unknown(); break;         \
605                 }                                                       \
606         }                                                               \
607         __pu_err;                                                       \
608 })
609
610 #define __put_user_unaligned_asm(insn, ptr)                             \
611 {                                                                       \
612         __asm__ __volatile__(                                           \
613         "1:     " insn "        %z2, %3         # __put_user_unaligned_asm\n" \
614         "2:                                                     \n"     \
615         "       .section        .fixup,\"ax\"                   \n"     \
616         "3:     li      %0, %4                                  \n"     \
617         "       j       2b                                      \n"     \
618         "       .previous                                       \n"     \
619         "       .section        __ex_table,\"a\"                \n"     \
620         "       " __UA_ADDR "   1b, 3b                          \n"     \
621         "       .previous                                       \n"     \
622         : "=r" (__pu_err)                                               \
623         : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)),                     \
624           "i" (-EFAULT));                                               \
625 }
626
627 #define __put_user_unaligned_asm_ll32(ptr)                              \
628 {                                                                       \
629         __asm__ __volatile__(                                           \
630         "1:     sw      %2, (%3)        # __put_user_unaligned_asm_ll32 \n" \
631         "2:     sw      %D2, 4(%3)                              \n"     \
632         "3:                                                     \n"     \
633         "       .section        .fixup,\"ax\"                   \n"     \
634         "4:     li      %0, %4                                  \n"     \
635         "       j       3b                                      \n"     \
636         "       .previous                                       \n"     \
637         "       .section        __ex_table,\"a\"                \n"     \
638         "       " __UA_ADDR "   1b, 4b                          \n"     \
639         "       " __UA_ADDR "   1b + 4, 4b                      \n"     \
640         "       " __UA_ADDR "   2b, 4b                          \n"     \
641         "       " __UA_ADDR "   2b + 4, 4b                      \n"     \
642         "       .previous"                                              \
643         : "=r" (__pu_err)                                               \
644         : "0" (0), "r" (__pu_val), "r" (ptr),                           \
645           "i" (-EFAULT));                                               \
646 }
647
648 extern void __put_user_unaligned_unknown(void);
649
650 /*
651  * We're generating jump to subroutines which will be outside the range of
652  * jump instructions
653  */
654 #ifdef MODULE
655 #define __MODULE_JAL(destination)                                       \
656         ".set\tnoat\n\t"                                                \
657         __UA_LA "\t$1, " #destination "\n\t"                            \
658         "jalr\t$1\n\t"                                                  \
659         ".set\tat\n\t"
660 #else
661 #define __MODULE_JAL(destination)                                       \
662         "jal\t" #destination "\n\t"
663 #endif
664
665 #ifndef CONFIG_CPU_DADDI_WORKAROUNDS
666 #define DADDI_SCRATCH "$0"
667 #else
668 #define DADDI_SCRATCH "$3"
669 #endif
670
671 extern size_t __copy_user(void *__to, const void *__from, size_t __n);
672
673 #define __invoke_copy_to_user(to, from, n)                              \
674 ({                                                                      \
675         register void __user *__cu_to_r __asm__("$4");                  \
676         register const void *__cu_from_r __asm__("$5");                 \
677         register long __cu_len_r __asm__("$6");                         \
678                                                                         \
679         __cu_to_r = (to);                                               \
680         __cu_from_r = (from);                                           \
681         __cu_len_r = (n);                                               \
682         __asm__ __volatile__(                                           \
683         __MODULE_JAL(__copy_user)                                       \
684         : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r)       \
685         :                                                               \
686         : "$8", "$9", "$10", "$11", "$12", "$15", "$24", "$31",         \
687           DADDI_SCRATCH, "memory");                                     \
688         __cu_len_r;                                                     \
689 })
690
691 /*
692  * __copy_to_user: - Copy a block of data into user space, with less checking.
693  * @to:   Destination address, in user space.
694  * @from: Source address, in kernel space.
695  * @n:    Number of bytes to copy.
696  *
697  * Context: User context only.  This function may sleep.
698  *
699  * Copy data from kernel space to user space.  Caller must check
700  * the specified block with access_ok() before calling this function.
701  *
702  * Returns number of bytes that could not be copied.
703  * On success, this will be zero.
704  */
705 #define __copy_to_user(to, from, n)                                     \
706 ({                                                                      \
707         void __user *__cu_to;                                           \
708         const void *__cu_from;                                          \
709         long __cu_len;                                                  \
710                                                                         \
711         might_sleep();                                                  \
712         __cu_to = (to);                                                 \
713         __cu_from = (from);                                             \
714         __cu_len = (n);                                                 \
715         __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, __cu_len); \
716         __cu_len;                                                       \
717 })
718
719 extern size_t __copy_user_inatomic(void *__to, const void *__from, size_t __n);
720
721 #define __copy_to_user_inatomic(to, from, n)                            \
722 ({                                                                      \
723         void __user *__cu_to;                                           \
724         const void *__cu_from;                                          \
725         long __cu_len;                                                  \
726                                                                         \
727         __cu_to = (to);                                                 \
728         __cu_from = (from);                                             \
729         __cu_len = (n);                                                 \
730         __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, __cu_len); \
731         __cu_len;                                                       \
732 })
733
734 #define __copy_from_user_inatomic(to, from, n)                          \
735 ({                                                                      \
736         void *__cu_to;                                                  \
737         const void __user *__cu_from;                                   \
738         long __cu_len;                                                  \
739                                                                         \
740         __cu_to = (to);                                                 \
741         __cu_from = (from);                                             \
742         __cu_len = (n);                                                 \
743         __cu_len = __invoke_copy_from_user_inatomic(__cu_to, __cu_from, \
744                                                     __cu_len);          \
745         __cu_len;                                                       \
746 })
747
748 /*
749  * copy_to_user: - Copy a block of data into user space.
750  * @to:   Destination address, in user space.
751  * @from: Source address, in kernel space.
752  * @n:    Number of bytes to copy.
753  *
754  * Context: User context only.  This function may sleep.
755  *
756  * Copy data from kernel space to user space.
757  *
758  * Returns number of bytes that could not be copied.
759  * On success, this will be zero.
760  */
761 #define copy_to_user(to, from, n)                                       \
762 ({                                                                      \
763         void __user *__cu_to;                                           \
764         const void *__cu_from;                                          \
765         long __cu_len;                                                  \
766                                                                         \
767         might_sleep();                                                  \
768         __cu_to = (to);                                                 \
769         __cu_from = (from);                                             \
770         __cu_len = (n);                                                 \
771         if (access_ok(VERIFY_WRITE, __cu_to, __cu_len))                 \
772                 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from,    \
773                                                  __cu_len);             \
774         __cu_len;                                                       \
775 })
776
777 #define __invoke_copy_from_user(to, from, n)                            \
778 ({                                                                      \
779         register void *__cu_to_r __asm__("$4");                         \
780         register const void __user *__cu_from_r __asm__("$5");          \
781         register long __cu_len_r __asm__("$6");                         \
782                                                                         \
783         __cu_to_r = (to);                                               \
784         __cu_from_r = (from);                                           \
785         __cu_len_r = (n);                                               \
786         __asm__ __volatile__(                                           \
787         ".set\tnoreorder\n\t"                                           \
788         __MODULE_JAL(__copy_user)                                       \
789         ".set\tnoat\n\t"                                                \
790         __UA_ADDU "\t$1, %1, %2\n\t"                                    \
791         ".set\tat\n\t"                                                  \
792         ".set\treorder"                                                 \
793         : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r)       \
794         :                                                               \
795         : "$8", "$9", "$10", "$11", "$12", "$15", "$24", "$31",         \
796           DADDI_SCRATCH, "memory");                                     \
797         __cu_len_r;                                                     \
798 })
799
800 #define __invoke_copy_from_user_inatomic(to, from, n)                   \
801 ({                                                                      \
802         register void *__cu_to_r __asm__("$4");                         \
803         register const void __user *__cu_from_r __asm__("$5");          \
804         register long __cu_len_r __asm__("$6");                         \
805                                                                         \
806         __cu_to_r = (to);                                               \
807         __cu_from_r = (from);                                           \
808         __cu_len_r = (n);                                               \
809         __asm__ __volatile__(                                           \
810         ".set\tnoreorder\n\t"                                           \
811         __MODULE_JAL(__copy_user_inatomic)                              \
812         ".set\tnoat\n\t"                                                \
813         __UA_ADDU "\t$1, %1, %2\n\t"                                    \
814         ".set\tat\n\t"                                                  \
815         ".set\treorder"                                                 \
816         : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r)       \
817         :                                                               \
818         : "$8", "$9", "$10", "$11", "$12", "$15", "$24", "$31",         \
819           DADDI_SCRATCH, "memory");                                     \
820         __cu_len_r;                                                     \
821 })
822
823 /*
824  * __copy_from_user: - Copy a block of data from user space, with less checking.
825  * @to:   Destination address, in kernel space.
826  * @from: Source address, in user space.
827  * @n:    Number of bytes to copy.
828  *
829  * Context: User context only.  This function may sleep.
830  *
831  * Copy data from user space to kernel space.  Caller must check
832  * the specified block with access_ok() before calling this function.
833  *
834  * Returns number of bytes that could not be copied.
835  * On success, this will be zero.
836  *
837  * If some data could not be copied, this function will pad the copied
838  * data to the requested size using zero bytes.
839  */
840 #define __copy_from_user(to, from, n)                                   \
841 ({                                                                      \
842         void *__cu_to;                                                  \
843         const void __user *__cu_from;                                   \
844         long __cu_len;                                                  \
845                                                                         \
846         might_sleep();                                                  \
847         __cu_to = (to);                                                 \
848         __cu_from = (from);                                             \
849         __cu_len = (n);                                                 \
850         __cu_len = __invoke_copy_from_user(__cu_to, __cu_from,          \
851                                            __cu_len);                   \
852         __cu_len;                                                       \
853 })
854
855 /*
856  * copy_from_user: - Copy a block of data from user space.
857  * @to:   Destination address, in kernel space.
858  * @from: Source address, in user space.
859  * @n:    Number of bytes to copy.
860  *
861  * Context: User context only.  This function may sleep.
862  *
863  * Copy data from user space to kernel space.
864  *
865  * Returns number of bytes that could not be copied.
866  * On success, this will be zero.
867  *
868  * If some data could not be copied, this function will pad the copied
869  * data to the requested size using zero bytes.
870  */
871 #define copy_from_user(to, from, n)                                     \
872 ({                                                                      \
873         void *__cu_to;                                                  \
874         const void __user *__cu_from;                                   \
875         long __cu_len;                                                  \
876                                                                         \
877         might_sleep();                                                  \
878         __cu_to = (to);                                                 \
879         __cu_from = (from);                                             \
880         __cu_len = (n);                                                 \
881         if (access_ok(VERIFY_READ, __cu_from, __cu_len))                \
882                 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from,  \
883                                                    __cu_len);           \
884         __cu_len;                                                       \
885 })
886
887 #define __copy_in_user(to, from, n)                                     \
888 ({                                                                      \
889         void __user *__cu_to;                                           \
890         const void __user *__cu_from;                                   \
891         long __cu_len;                                                  \
892                                                                         \
893         might_sleep();                                                  \
894         __cu_to = (to);                                                 \
895         __cu_from = (from);                                             \
896         __cu_len = (n);                                                 \
897         __cu_len = __invoke_copy_from_user(__cu_to, __cu_from,          \
898                                            __cu_len);                   \
899         __cu_len;                                                       \
900 })
901
902 #define copy_in_user(to, from, n)                                       \
903 ({                                                                      \
904         void __user *__cu_to;                                           \
905         const void __user *__cu_from;                                   \
906         long __cu_len;                                                  \
907                                                                         \
908         might_sleep();                                                  \
909         __cu_to = (to);                                                 \
910         __cu_from = (from);                                             \
911         __cu_len = (n);                                                 \
912         if (likely(access_ok(VERIFY_READ, __cu_from, __cu_len) &&       \
913                    access_ok(VERIFY_WRITE, __cu_to, __cu_len)))         \
914                 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from,  \
915                                                    __cu_len);           \
916         __cu_len;                                                       \
917 })
918
919 /*
920  * __clear_user: - Zero a block of memory in user space, with less checking.
921  * @to:   Destination address, in user space.
922  * @n:    Number of bytes to zero.
923  *
924  * Zero a block of memory in user space.  Caller must check
925  * the specified block with access_ok() before calling this function.
926  *
927  * Returns number of bytes that could not be cleared.
928  * On success, this will be zero.
929  */
930 static inline __kernel_size_t
931 __clear_user(void __user *addr, __kernel_size_t size)
932 {
933         __kernel_size_t res;
934
935         might_sleep();
936         __asm__ __volatile__(
937                 "move\t$4, %1\n\t"
938                 "move\t$5, $0\n\t"
939                 "move\t$6, %2\n\t"
940                 __MODULE_JAL(__bzero)
941                 "move\t%0, $6"
942                 : "=r" (res)
943                 : "r" (addr), "r" (size)
944                 : "$4", "$5", "$6", __UA_t0, __UA_t1, "$31");
945
946         return res;
947 }
948
949 #define clear_user(addr,n)                                              \
950 ({                                                                      \
951         void __user * __cl_addr = (addr);                               \
952         unsigned long __cl_size = (n);                                  \
953         if (__cl_size && access_ok(VERIFY_WRITE,                        \
954                 ((unsigned long)(__cl_addr)), __cl_size))               \
955                 __cl_size = __clear_user(__cl_addr, __cl_size);         \
956         __cl_size;                                                      \
957 })
958
959 /*
960  * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
961  * @dst:   Destination address, in kernel space.  This buffer must be at
962  *         least @count bytes long.
963  * @src:   Source address, in user space.
964  * @count: Maximum number of bytes to copy, including the trailing NUL.
965  *
966  * Copies a NUL-terminated string from userspace to kernel space.
967  * Caller must check the specified block with access_ok() before calling
968  * this function.
969  *
970  * On success, returns the length of the string (not including the trailing
971  * NUL).
972  *
973  * If access to userspace fails, returns -EFAULT (some data may have been
974  * copied).
975  *
976  * If @count is smaller than the length of the string, copies @count bytes
977  * and returns @count.
978  */
979 static inline long
980 __strncpy_from_user(char *__to, const char __user *__from, long __len)
981 {
982         long res;
983
984         might_sleep();
985         __asm__ __volatile__(
986                 "move\t$4, %1\n\t"
987                 "move\t$5, %2\n\t"
988                 "move\t$6, %3\n\t"
989                 __MODULE_JAL(__strncpy_from_user_nocheck_asm)
990                 "move\t%0, $2"
991                 : "=r" (res)
992                 : "r" (__to), "r" (__from), "r" (__len)
993                 : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
994
995         return res;
996 }
997
998 /*
999  * strncpy_from_user: - Copy a NUL terminated string from userspace.
1000  * @dst:   Destination address, in kernel space.  This buffer must be at
1001  *         least @count bytes long.
1002  * @src:   Source address, in user space.
1003  * @count: Maximum number of bytes to copy, including the trailing NUL.
1004  *
1005  * Copies a NUL-terminated string from userspace to kernel space.
1006  *
1007  * On success, returns the length of the string (not including the trailing
1008  * NUL).
1009  *
1010  * If access to userspace fails, returns -EFAULT (some data may have been
1011  * copied).
1012  *
1013  * If @count is smaller than the length of the string, copies @count bytes
1014  * and returns @count.
1015  */
1016 static inline long
1017 strncpy_from_user(char *__to, const char __user *__from, long __len)
1018 {
1019         long res;
1020
1021         might_sleep();
1022         __asm__ __volatile__(
1023                 "move\t$4, %1\n\t"
1024                 "move\t$5, %2\n\t"
1025                 "move\t$6, %3\n\t"
1026                 __MODULE_JAL(__strncpy_from_user_asm)
1027                 "move\t%0, $2"
1028                 : "=r" (res)
1029                 : "r" (__to), "r" (__from), "r" (__len)
1030                 : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
1031
1032         return res;
1033 }
1034
1035 /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
1036 static inline long __strlen_user(const char __user *s)
1037 {
1038         long res;
1039
1040         might_sleep();
1041         __asm__ __volatile__(
1042                 "move\t$4, %1\n\t"
1043                 __MODULE_JAL(__strlen_user_nocheck_asm)
1044                 "move\t%0, $2"
1045                 : "=r" (res)
1046                 : "r" (s)
1047                 : "$2", "$4", __UA_t0, "$31");
1048
1049         return res;
1050 }
1051
1052 /*
1053  * strlen_user: - Get the size of a string in user space.
1054  * @str: The string to measure.
1055  *
1056  * Context: User context only.  This function may sleep.
1057  *
1058  * Get the size of a NUL-terminated string in user space.
1059  *
1060  * Returns the size of the string INCLUDING the terminating NUL.
1061  * On exception, returns 0.
1062  *
1063  * If there is a limit on the length of a valid string, you may wish to
1064  * consider using strnlen_user() instead.
1065  */
1066 static inline long strlen_user(const char __user *s)
1067 {
1068         long res;
1069
1070         might_sleep();
1071         __asm__ __volatile__(
1072                 "move\t$4, %1\n\t"
1073                 __MODULE_JAL(__strlen_user_asm)
1074                 "move\t%0, $2"
1075                 : "=r" (res)
1076                 : "r" (s)
1077                 : "$2", "$4", __UA_t0, "$31");
1078
1079         return res;
1080 }
1081
1082 /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
1083 static inline long __strnlen_user(const char __user *s, long n)
1084 {
1085         long res;
1086
1087         might_sleep();
1088         __asm__ __volatile__(
1089                 "move\t$4, %1\n\t"
1090                 "move\t$5, %2\n\t"
1091                 __MODULE_JAL(__strnlen_user_nocheck_asm)
1092                 "move\t%0, $2"
1093                 : "=r" (res)
1094                 : "r" (s), "r" (n)
1095                 : "$2", "$4", "$5", __UA_t0, "$31");
1096
1097         return res;
1098 }
1099
1100 /*
1101  * strlen_user: - Get the size of a string in user space.
1102  * @str: The string to measure.
1103  *
1104  * Context: User context only.  This function may sleep.
1105  *
1106  * Get the size of a NUL-terminated string in user space.
1107  *
1108  * Returns the size of the string INCLUDING the terminating NUL.
1109  * On exception, returns 0.
1110  *
1111  * If there is a limit on the length of a valid string, you may wish to
1112  * consider using strnlen_user() instead.
1113  */
1114 static inline long strnlen_user(const char __user *s, long n)
1115 {
1116         long res;
1117
1118         might_sleep();
1119         __asm__ __volatile__(
1120                 "move\t$4, %1\n\t"
1121                 "move\t$5, %2\n\t"
1122                 __MODULE_JAL(__strnlen_user_asm)
1123                 "move\t%0, $2"
1124                 : "=r" (res)
1125                 : "r" (s), "r" (n)
1126                 : "$2", "$4", "$5", __UA_t0, "$31");
1127
1128         return res;
1129 }
1130
1131 struct exception_table_entry
1132 {
1133         unsigned long insn;
1134         unsigned long nextinsn;
1135 };
1136
1137 extern int fixup_exception(struct pt_regs *regs);
1138
1139 #endif /* _ASM_UACCESS_H */