Merge git://git.skbuff.net/gitroot/yoshfuji/linux-2.6.14+advapi-fix/
[pandora-kernel.git] / include / asm-mips / 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  */
9 #ifndef _ASM_UACCESS_H
10 #define _ASM_UACCESS_H
11
12 #include <linux/config.h>
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/thread_info.h>
16 #include <asm-generic/uaccess.h>
17
18 /*
19  * The fs value determines whether argument validity checking should be
20  * performed or not.  If get_fs() == USER_DS, checking is performed, with
21  * get_fs() == KERNEL_DS, checking is bypassed.
22  *
23  * For historical reasons, these macros are grossly misnamed.
24  */
25 #ifdef CONFIG_32BIT
26
27 #define __UA_LIMIT      0x80000000UL
28
29 #define __UA_ADDR       ".word"
30 #define __UA_LA         "la"
31 #define __UA_ADDU       "addu"
32 #define __UA_t0         "$8"
33 #define __UA_t1         "$9"
34
35 #endif /* CONFIG_32BIT */
36
37 #ifdef CONFIG_64BIT
38
39 #define __UA_LIMIT      (- TASK_SIZE)
40
41 #define __UA_ADDR       ".dword"
42 #define __UA_LA         "dla"
43 #define __UA_ADDU       "daddu"
44 #define __UA_t0         "$12"
45 #define __UA_t1         "$13"
46
47 #endif /* CONFIG_64BIT */
48
49 /*
50  * USER_DS is a bitmask that has the bits set that may not be set in a valid
51  * userspace address.  Note that we limit 32-bit userspace to 0x7fff8000 but
52  * the arithmetic we're doing only works if the limit is a power of two, so
53  * we use 0x80000000 here on 32-bit kernels.  If a process passes an invalid
54  * address in this range it's the process's problem, not ours :-)
55  */
56
57 #define KERNEL_DS       ((mm_segment_t) { 0UL })
58 #define USER_DS         ((mm_segment_t) { __UA_LIMIT })
59
60 #define VERIFY_READ    0
61 #define VERIFY_WRITE   1
62
63 #define get_ds()        (KERNEL_DS)
64 #define get_fs()        (current_thread_info()->addr_limit)
65 #define set_fs(x)       (current_thread_info()->addr_limit = (x))
66
67 #define segment_eq(a,b) ((a).seg == (b).seg)
68
69
70 /*
71  * Is a address valid? This does a straighforward calculation rather
72  * than tests.
73  *
74  * Address valid if:
75  *  - "addr" doesn't have any high-bits set
76  *  - AND "size" doesn't have any high-bits set
77  *  - AND "addr+size" doesn't have any high-bits set
78  *  - OR we are in kernel mode.
79  *
80  * __ua_size() is a trick to avoid runtime checking of positive constant
81  * sizes; for those we already know at compile time that the size is ok.
82  */
83 #define __ua_size(size)                                                 \
84         ((__builtin_constant_p(size) && (signed long) (size) > 0) ? 0 : (size))
85
86 /*
87  * access_ok: - Checks if a user space pointer is valid
88  * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE.  Note that
89  *        %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
90  *        to write to a block, it is always safe to read from it.
91  * @addr: User space pointer to start of block to check
92  * @size: Size of block to check
93  *
94  * Context: User context only.  This function may sleep.
95  *
96  * Checks if a pointer to a block of memory in user space is valid.
97  *
98  * Returns true (nonzero) if the memory block may be valid, false (zero)
99  * if it is definitely invalid.
100  *
101  * Note that, depending on architecture, this function probably just
102  * checks that the pointer is in the user space range - after calling
103  * this function, memory access functions may still return -EFAULT.
104  */
105
106 #define __access_mask get_fs().seg
107
108 #define __access_ok(addr, size, mask)                                   \
109         (((signed long)((mask) & ((addr) | ((addr) + (size)) | __ua_size(size)))) == 0)
110
111 #define access_ok(type, addr, size)                                     \
112         likely(__access_ok((unsigned long)(addr), (size),__access_mask))
113
114 /*
115  * put_user: - Write a simple value into user space.
116  * @x:   Value to copy to user space.
117  * @ptr: Destination address, in user space.
118  *
119  * Context: User context only.  This function may sleep.
120  *
121  * This macro copies a single simple value from kernel space to user
122  * space.  It supports simple types like char and int, but not larger
123  * data types like structures or arrays.
124  *
125  * @ptr must have pointer-to-simple-variable type, and @x must be assignable
126  * to the result of dereferencing @ptr.
127  *
128  * Returns zero on success, or -EFAULT on error.
129  */
130 #define put_user(x,ptr) \
131         __put_user_check((x),(ptr),sizeof(*(ptr)))
132
133 /*
134  * get_user: - Get a simple variable from user space.
135  * @x:   Variable to store result.
136  * @ptr: Source address, in user space.
137  *
138  * Context: User context only.  This function may sleep.
139  *
140  * This macro copies a single simple variable from user space to kernel
141  * space.  It supports simple types like char and int, but not larger
142  * data types like structures or arrays.
143  *
144  * @ptr must have pointer-to-simple-variable type, and the result of
145  * dereferencing @ptr must be assignable to @x without a cast.
146  *
147  * Returns zero on success, or -EFAULT on error.
148  * On error, the variable @x is set to zero.
149  */
150 #define get_user(x,ptr) \
151         __get_user_check((x),(ptr),sizeof(*(ptr)))
152
153 /*
154  * __put_user: - Write a simple value into user space, with less checking.
155  * @x:   Value to copy to user space.
156  * @ptr: Destination address, in user space.
157  *
158  * Context: User context only.  This function may sleep.
159  *
160  * This macro copies a single simple value from kernel space to user
161  * space.  It supports simple types like char and int, but not larger
162  * data types like structures or arrays.
163  *
164  * @ptr must have pointer-to-simple-variable type, and @x must be assignable
165  * to the result of dereferencing @ptr.
166  *
167  * Caller must check the pointer with access_ok() before calling this
168  * function.
169  *
170  * Returns zero on success, or -EFAULT on error.
171  */
172 #define __put_user(x,ptr) \
173         __put_user_nocheck((x),(ptr),sizeof(*(ptr)))
174
175 /*
176  * __get_user: - Get a simple variable from user space, with less checking.
177  * @x:   Variable to store result.
178  * @ptr: Source address, in user space.
179  *
180  * Context: User context only.  This function may sleep.
181  *
182  * This macro copies a single simple variable from user space to kernel
183  * space.  It supports simple types like char and int, but not larger
184  * data types like structures or arrays.
185  *
186  * @ptr must have pointer-to-simple-variable type, and the result of
187  * dereferencing @ptr must be assignable to @x without a cast.
188  *
189  * Caller must check the pointer with access_ok() before calling this
190  * function.
191  *
192  * Returns zero on success, or -EFAULT on error.
193  * On error, the variable @x is set to zero.
194  */
195 #define __get_user(x,ptr) \
196         __get_user_nocheck((x),(ptr),sizeof(*(ptr)))
197
198 struct __large_struct { unsigned long buf[100]; };
199 #define __m(x) (*(struct __large_struct __user *)(x))
200
201 /*
202  * Yuck.  We need two variants, one for 64bit operation and one
203  * for 32 bit mode and old iron.
204  */
205 #ifdef __mips64
206 #define __GET_USER_DW(ptr) __get_user_asm("ld", ptr)
207 #else
208 #define __GET_USER_DW(ptr) __get_user_asm_ll32(ptr)
209 #endif
210
211 #define __get_user_nocheck(x,ptr,size)                                  \
212 ({                                                                      \
213         __typeof(*(ptr)) __gu_val =  (__typeof(*(ptr))) 0;              \
214         long __gu_err = 0;                                              \
215                                                                         \
216         switch (size) {                                                 \
217         case 1: __get_user_asm("lb", ptr); break;                       \
218         case 2: __get_user_asm("lh", ptr); break;                       \
219         case 4: __get_user_asm("lw", ptr); break;                       \
220         case 8: __GET_USER_DW(ptr); break;                              \
221         default: __get_user_unknown(); break;                           \
222         }                                                               \
223         (x) = (__typeof__(*(ptr))) __gu_val;                            \
224         __gu_err;                                                       \
225 })
226
227 #define __get_user_check(x,ptr,size)                                    \
228 ({                                                                      \
229         const __typeof__(*(ptr)) __user * __gu_addr = (ptr);            \
230         __typeof__(*(ptr)) __gu_val = 0;                                \
231         long __gu_err = -EFAULT;                                        \
232                                                                         \
233         if (likely(access_ok(VERIFY_READ,  __gu_addr, size))) {         \
234                 switch (size) {                                         \
235                 case 1: __get_user_asm("lb", __gu_addr); break;         \
236                 case 2: __get_user_asm("lh", __gu_addr); break;         \
237                 case 4: __get_user_asm("lw", __gu_addr); break;         \
238                 case 8: __GET_USER_DW(__gu_addr); break;                \
239                 default: __get_user_unknown(); break;                   \
240                 }                                                       \
241         }                                                               \
242         (x) = (__typeof__(*(ptr))) __gu_val;                            \
243         __gu_err;                                                       \
244 })
245
246 #define __get_user_asm(insn, addr)                                      \
247 {                                                                       \
248         __asm__ __volatile__(                                           \
249         "1:     " insn "        %1, %3                          \n"     \
250         "2:                                                     \n"     \
251         "       .section .fixup,\"ax\"                          \n"     \
252         "3:     li      %0, %4                                  \n"     \
253         "       j       2b                                      \n"     \
254         "       .previous                                       \n"     \
255         "       .section __ex_table,\"a\"                       \n"     \
256         "       "__UA_ADDR "\t1b, 3b                            \n"     \
257         "       .previous                                       \n"     \
258         : "=r" (__gu_err), "=r" (__gu_val)                              \
259         : "0" (0), "o" (__m(addr)), "i" (-EFAULT));                     \
260 }
261
262 /*
263  * Get a long long 64 using 32 bit registers.
264  */
265 #define __get_user_asm_ll32(addr)                                       \
266 {                                                                       \
267         __asm__ __volatile__(                                           \
268         "1:     lw      %1, (%3)                                \n"     \
269         "2:     lw      %D1, 4(%3)                              \n"     \
270         "       move    %0, $0                                  \n"     \
271         "3:     .section        .fixup,\"ax\"                   \n"     \
272         "4:     li      %0, %4                                  \n"     \
273         "       move    %1, $0                                  \n"     \
274         "       move    %D1, $0                                 \n"     \
275         "       j       3b                                      \n"     \
276         "       .previous                                       \n"     \
277         "       .section        __ex_table,\"a\"                \n"     \
278         "       " __UA_ADDR "   1b, 4b                          \n"     \
279         "       " __UA_ADDR "   2b, 4b                          \n"     \
280         "       .previous                                       \n"     \
281         : "=r" (__gu_err), "=&r" (__gu_val)                             \
282         : "0" (0), "r" (addr), "i" (-EFAULT));                          \
283 }
284
285 extern void __get_user_unknown(void);
286
287 /*
288  * Yuck.  We need two variants, one for 64bit operation and one
289  * for 32 bit mode and old iron.
290  */
291 #ifdef __mips64
292 #define __PUT_USER_DW(ptr) __put_user_asm("sd", ptr)
293 #else
294 #define __PUT_USER_DW(ptr) __put_user_asm_ll32(ptr)
295 #endif
296
297 #define __put_user_nocheck(x,ptr,size)                                  \
298 ({                                                                      \
299         __typeof__(*(ptr)) __pu_val;                                    \
300         long __pu_err = 0;                                              \
301                                                                         \
302         __pu_val = (x);                                                 \
303         switch (size) {                                                 \
304         case 1: __put_user_asm("sb", ptr); break;                       \
305         case 2: __put_user_asm("sh", ptr); break;                       \
306         case 4: __put_user_asm("sw", ptr); break;                       \
307         case 8: __PUT_USER_DW(ptr); break;                              \
308         default: __put_user_unknown(); break;                           \
309         }                                                               \
310         __pu_err;                                                       \
311 })
312
313 #define __put_user_check(x,ptr,size)                                    \
314 ({                                                                      \
315         __typeof__(*(ptr)) __user *__pu_addr = (ptr);                   \
316         __typeof__(*(ptr)) __pu_val = (x);                              \
317         long __pu_err = -EFAULT;                                        \
318                                                                         \
319         if (likely(access_ok(VERIFY_WRITE,  __pu_addr, size))) {        \
320                 switch (size) {                                         \
321                 case 1: __put_user_asm("sb", __pu_addr); break;         \
322                 case 2: __put_user_asm("sh", __pu_addr); break;         \
323                 case 4: __put_user_asm("sw", __pu_addr); break;         \
324                 case 8: __PUT_USER_DW(__pu_addr); break;                \
325                 default: __put_user_unknown(); break;                   \
326                 }                                                       \
327         }                                                               \
328         __pu_err;                                                       \
329 })
330
331 #define __put_user_asm(insn, ptr)                                       \
332 {                                                                       \
333         __asm__ __volatile__(                                           \
334         "1:     " insn "        %z2, %3         # __put_user_asm\n"     \
335         "2:                                                     \n"     \
336         "       .section        .fixup,\"ax\"                   \n"     \
337         "3:     li      %0, %4                                  \n"     \
338         "       j       2b                                      \n"     \
339         "       .previous                                       \n"     \
340         "       .section        __ex_table,\"a\"                \n"     \
341         "       " __UA_ADDR "   1b, 3b                          \n"     \
342         "       .previous                                       \n"     \
343         : "=r" (__pu_err)                                               \
344         : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)),                     \
345           "i" (-EFAULT));                                               \
346 }
347
348 #define __put_user_asm_ll32(ptr)                                        \
349 {                                                                       \
350         __asm__ __volatile__(                                           \
351         "1:     sw      %2, (%3)        # __put_user_asm_ll32   \n"     \
352         "2:     sw      %D2, 4(%3)                              \n"     \
353         "3:                                                     \n"     \
354         "       .section        .fixup,\"ax\"                   \n"     \
355         "4:     li      %0, %4                                  \n"     \
356         "       j       3b                                      \n"     \
357         "       .previous                                       \n"     \
358         "       .section        __ex_table,\"a\"                \n"     \
359         "       " __UA_ADDR "   1b, 4b                          \n"     \
360         "       " __UA_ADDR "   2b, 4b                          \n"     \
361         "       .previous"                                              \
362         : "=r" (__pu_err)                                               \
363         : "0" (0), "r" (__pu_val), "r" (ptr),                           \
364           "i" (-EFAULT));                                               \
365 }
366
367 extern void __put_user_unknown(void);
368
369 /*
370  * We're generating jump to subroutines which will be outside the range of
371  * jump instructions
372  */
373 #ifdef MODULE
374 #define __MODULE_JAL(destination)                                       \
375         ".set\tnoat\n\t"                                                \
376         __UA_LA "\t$1, " #destination "\n\t"                            \
377         "jalr\t$1\n\t"                                                  \
378         ".set\tat\n\t"
379 #else
380 #define __MODULE_JAL(destination)                                       \
381         "jal\t" #destination "\n\t"
382 #endif
383
384 extern size_t __copy_user(void *__to, const void *__from, size_t __n);
385
386 #define __invoke_copy_to_user(to,from,n)                                \
387 ({                                                                      \
388         register void __user *__cu_to_r __asm__ ("$4");                 \
389         register const void *__cu_from_r __asm__ ("$5");                \
390         register long __cu_len_r __asm__ ("$6");                        \
391                                                                         \
392         __cu_to_r = (to);                                               \
393         __cu_from_r = (from);                                           \
394         __cu_len_r = (n);                                               \
395         __asm__ __volatile__(                                           \
396         __MODULE_JAL(__copy_user)                                       \
397         : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r)       \
398         :                                                               \
399         : "$8", "$9", "$10", "$11", "$12", "$15", "$24", "$31",         \
400           "memory");                                                    \
401         __cu_len_r;                                                     \
402 })
403
404 /*
405  * __copy_to_user: - Copy a block of data into user space, with less checking.
406  * @to:   Destination address, in user space.
407  * @from: Source address, in kernel space.
408  * @n:    Number of bytes to copy.
409  *
410  * Context: User context only.  This function may sleep.
411  *
412  * Copy data from kernel space to user space.  Caller must check
413  * the specified block with access_ok() before calling this function.
414  *
415  * Returns number of bytes that could not be copied.
416  * On success, this will be zero.
417  */
418 #define __copy_to_user(to,from,n)                                       \
419 ({                                                                      \
420         void __user *__cu_to;                                           \
421         const void *__cu_from;                                          \
422         long __cu_len;                                                  \
423                                                                         \
424         might_sleep();                                                  \
425         __cu_to = (to);                                                 \
426         __cu_from = (from);                                             \
427         __cu_len = (n);                                                 \
428         __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, __cu_len); \
429         __cu_len;                                                       \
430 })
431
432 #define __copy_to_user_inatomic __copy_to_user
433 #define __copy_from_user_inatomic __copy_from_user
434
435 /*
436  * copy_to_user: - Copy a block of data into user space.
437  * @to:   Destination address, in user space.
438  * @from: Source address, in kernel space.
439  * @n:    Number of bytes to copy.
440  *
441  * Context: User context only.  This function may sleep.
442  *
443  * Copy data from kernel space to user space.
444  *
445  * Returns number of bytes that could not be copied.
446  * On success, this will be zero.
447  */
448 #define copy_to_user(to,from,n)                                         \
449 ({                                                                      \
450         void __user *__cu_to;                                           \
451         const void *__cu_from;                                          \
452         long __cu_len;                                                  \
453                                                                         \
454         might_sleep();                                                  \
455         __cu_to = (to);                                                 \
456         __cu_from = (from);                                             \
457         __cu_len = (n);                                                 \
458         if (access_ok(VERIFY_WRITE, __cu_to, __cu_len))                 \
459                 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from,    \
460                                                  __cu_len);             \
461         __cu_len;                                                       \
462 })
463
464 #define __invoke_copy_from_user(to,from,n)                              \
465 ({                                                                      \
466         register void *__cu_to_r __asm__ ("$4");                        \
467         register const void __user *__cu_from_r __asm__ ("$5");         \
468         register long __cu_len_r __asm__ ("$6");                        \
469                                                                         \
470         __cu_to_r = (to);                                               \
471         __cu_from_r = (from);                                           \
472         __cu_len_r = (n);                                               \
473         __asm__ __volatile__(                                           \
474         ".set\tnoreorder\n\t"                                           \
475         __MODULE_JAL(__copy_user)                                       \
476         ".set\tnoat\n\t"                                                \
477         __UA_ADDU "\t$1, %1, %2\n\t"                                    \
478         ".set\tat\n\t"                                                  \
479         ".set\treorder"                                                 \
480         : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r)       \
481         :                                                               \
482         : "$8", "$9", "$10", "$11", "$12", "$15", "$24", "$31",         \
483           "memory");                                                    \
484         __cu_len_r;                                                     \
485 })
486
487 /*
488  * __copy_from_user: - Copy a block of data from user space, with less checking. * @to:   Destination address, in kernel space.
489  * @from: Source address, in user space.
490  * @n:    Number of bytes to copy.
491  *
492  * Context: User context only.  This function may sleep.
493  *
494  * Copy data from user space to kernel space.  Caller must check
495  * the specified block with access_ok() before calling this function.
496  *
497  * Returns number of bytes that could not be copied.
498  * On success, this will be zero.
499  *
500  * If some data could not be copied, this function will pad the copied
501  * data to the requested size using zero bytes.
502  */
503 #define __copy_from_user(to,from,n)                                     \
504 ({                                                                      \
505         void *__cu_to;                                                  \
506         const void __user *__cu_from;                                   \
507         long __cu_len;                                                  \
508                                                                         \
509         might_sleep();                                                  \
510         __cu_to = (to);                                                 \
511         __cu_from = (from);                                             \
512         __cu_len = (n);                                                 \
513         __cu_len = __invoke_copy_from_user(__cu_to, __cu_from,          \
514                                            __cu_len);                   \
515         __cu_len;                                                       \
516 })
517
518 /*
519  * copy_from_user: - Copy a block of data from user space.
520  * @to:   Destination address, in kernel space.
521  * @from: Source address, in user space.
522  * @n:    Number of bytes to copy.
523  *
524  * Context: User context only.  This function may sleep.
525  *
526  * Copy data from user space to kernel space.
527  *
528  * Returns number of bytes that could not be copied.
529  * On success, this will be zero.
530  *
531  * If some data could not be copied, this function will pad the copied
532  * data to the requested size using zero bytes.
533  */
534 #define copy_from_user(to,from,n)                                       \
535 ({                                                                      \
536         void *__cu_to;                                                  \
537         const void __user *__cu_from;                                   \
538         long __cu_len;                                                  \
539                                                                         \
540         might_sleep();                                                  \
541         __cu_to = (to);                                                 \
542         __cu_from = (from);                                             \
543         __cu_len = (n);                                                 \
544         if (access_ok(VERIFY_READ, __cu_from, __cu_len))                \
545                 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from,  \
546                                                    __cu_len);           \
547         __cu_len;                                                       \
548 })
549
550 #define __copy_in_user(to, from, n)     __copy_from_user(to, from, n)
551
552 #define copy_in_user(to,from,n)                                         \
553 ({                                                                      \
554         void __user *__cu_to;                                           \
555         const void __user *__cu_from;                                   \
556         long __cu_len;                                                  \
557                                                                         \
558         might_sleep();                                                  \
559         __cu_to = (to);                                                 \
560         __cu_from = (from);                                             \
561         __cu_len = (n);                                                 \
562         if (likely(access_ok(VERIFY_READ, __cu_from, __cu_len) &&       \
563                    access_ok(VERIFY_WRITE, __cu_to, __cu_len)))         \
564                 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from,  \
565                                                    __cu_len);           \
566         __cu_len;                                                       \
567 })
568
569 /*
570  * __clear_user: - Zero a block of memory in user space, with less checking.
571  * @to:   Destination address, in user space.
572  * @n:    Number of bytes to zero.
573  *
574  * Zero a block of memory in user space.  Caller must check
575  * the specified block with access_ok() before calling this function.
576  *
577  * Returns number of bytes that could not be cleared.
578  * On success, this will be zero.
579  */
580 static inline __kernel_size_t
581 __clear_user(void __user *addr, __kernel_size_t size)
582 {
583         __kernel_size_t res;
584
585         might_sleep();
586         __asm__ __volatile__(
587                 "move\t$4, %1\n\t"
588                 "move\t$5, $0\n\t"
589                 "move\t$6, %2\n\t"
590                 __MODULE_JAL(__bzero)
591                 "move\t%0, $6"
592                 : "=r" (res)
593                 : "r" (addr), "r" (size)
594                 : "$4", "$5", "$6", __UA_t0, __UA_t1, "$31");
595
596         return res;
597 }
598
599 #define clear_user(addr,n)                                              \
600 ({                                                                      \
601         void __user * __cl_addr = (addr);                               \
602         unsigned long __cl_size = (n);                                  \
603         if (__cl_size && access_ok(VERIFY_WRITE,                        \
604                 ((unsigned long)(__cl_addr)), __cl_size))               \
605                 __cl_size = __clear_user(__cl_addr, __cl_size);         \
606         __cl_size;                                                      \
607 })
608
609 /*
610  * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
611  * @dst:   Destination address, in kernel space.  This buffer must be at
612  *         least @count bytes long.
613  * @src:   Source address, in user space.
614  * @count: Maximum number of bytes to copy, including the trailing NUL.
615  *
616  * Copies a NUL-terminated string from userspace to kernel space.
617  * Caller must check the specified block with access_ok() before calling
618  * this function.
619  *
620  * On success, returns the length of the string (not including the trailing
621  * NUL).
622  *
623  * If access to userspace fails, returns -EFAULT (some data may have been
624  * copied).
625  *
626  * If @count is smaller than the length of the string, copies @count bytes
627  * and returns @count.
628  */
629 static inline long
630 __strncpy_from_user(char *__to, const char __user *__from, long __len)
631 {
632         long res;
633
634         might_sleep();
635         __asm__ __volatile__(
636                 "move\t$4, %1\n\t"
637                 "move\t$5, %2\n\t"
638                 "move\t$6, %3\n\t"
639                 __MODULE_JAL(__strncpy_from_user_nocheck_asm)
640                 "move\t%0, $2"
641                 : "=r" (res)
642                 : "r" (__to), "r" (__from), "r" (__len)
643                 : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
644
645         return res;
646 }
647
648 /*
649  * strncpy_from_user: - Copy a NUL terminated string from userspace.
650  * @dst:   Destination address, in kernel space.  This buffer must be at
651  *         least @count bytes long.
652  * @src:   Source address, in user space.
653  * @count: Maximum number of bytes to copy, including the trailing NUL.
654  *
655  * Copies a NUL-terminated string from userspace to kernel space.
656  *
657  * On success, returns the length of the string (not including the trailing
658  * NUL).
659  *
660  * If access to userspace fails, returns -EFAULT (some data may have been
661  * copied).
662  *
663  * If @count is smaller than the length of the string, copies @count bytes
664  * and returns @count.
665  */
666 static inline long
667 strncpy_from_user(char *__to, const char __user *__from, long __len)
668 {
669         long res;
670
671         might_sleep();
672         __asm__ __volatile__(
673                 "move\t$4, %1\n\t"
674                 "move\t$5, %2\n\t"
675                 "move\t$6, %3\n\t"
676                 __MODULE_JAL(__strncpy_from_user_asm)
677                 "move\t%0, $2"
678                 : "=r" (res)
679                 : "r" (__to), "r" (__from), "r" (__len)
680                 : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
681
682         return res;
683 }
684
685 /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
686 static inline long __strlen_user(const char __user *s)
687 {
688         long res;
689
690         might_sleep();
691         __asm__ __volatile__(
692                 "move\t$4, %1\n\t"
693                 __MODULE_JAL(__strlen_user_nocheck_asm)
694                 "move\t%0, $2"
695                 : "=r" (res)
696                 : "r" (s)
697                 : "$2", "$4", __UA_t0, "$31");
698
699         return res;
700 }
701
702 /*
703  * strlen_user: - Get the size of a string in user space.
704  * @str: The string to measure.
705  *
706  * Context: User context only.  This function may sleep.
707  *
708  * Get the size of a NUL-terminated string in user space.
709  *
710  * Returns the size of the string INCLUDING the terminating NUL.
711  * On exception, returns 0.
712  *
713  * If there is a limit on the length of a valid string, you may wish to
714  * consider using strnlen_user() instead.
715  */
716 static inline long strlen_user(const char __user *s)
717 {
718         long res;
719
720         might_sleep();
721         __asm__ __volatile__(
722                 "move\t$4, %1\n\t"
723                 __MODULE_JAL(__strlen_user_asm)
724                 "move\t%0, $2"
725                 : "=r" (res)
726                 : "r" (s)
727                 : "$2", "$4", __UA_t0, "$31");
728
729         return res;
730 }
731
732 /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
733 static inline long __strnlen_user(const char __user *s, long n)
734 {
735         long res;
736
737         might_sleep();
738         __asm__ __volatile__(
739                 "move\t$4, %1\n\t"
740                 "move\t$5, %2\n\t"
741                 __MODULE_JAL(__strnlen_user_nocheck_asm)
742                 "move\t%0, $2"
743                 : "=r" (res)
744                 : "r" (s), "r" (n)
745                 : "$2", "$4", "$5", __UA_t0, "$31");
746
747         return res;
748 }
749
750 /*
751  * strlen_user: - Get the size of a string in user space.
752  * @str: The string to measure.
753  *
754  * Context: User context only.  This function may sleep.
755  *
756  * Get the size of a NUL-terminated string in user space.
757  *
758  * Returns the size of the string INCLUDING the terminating NUL.
759  * On exception, returns 0.
760  *
761  * If there is a limit on the length of a valid string, you may wish to
762  * consider using strnlen_user() instead.
763  */
764 static inline long strnlen_user(const char __user *s, long n)
765 {
766         long res;
767
768         might_sleep();
769         __asm__ __volatile__(
770                 "move\t$4, %1\n\t"
771                 "move\t$5, %2\n\t"
772                 __MODULE_JAL(__strnlen_user_asm)
773                 "move\t%0, $2"
774                 : "=r" (res)
775                 : "r" (s), "r" (n)
776                 : "$2", "$4", "$5", __UA_t0, "$31");
777
778         return res;
779 }
780
781 struct exception_table_entry
782 {
783         unsigned long insn;
784         unsigned long nextinsn;
785 };
786
787 extern int fixup_exception(struct pt_regs *regs);
788
789 #endif /* _ASM_UACCESS_H */