This commit was manufactured by cvs2svn to create tag
[linux-2.6.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_MIPS32
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_MIPS32 */
36
37 #ifdef CONFIG_MIPS64
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_MIPS64 */
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  * verify_area: - Obsolete, use access_ok()
116  * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE
117  * @addr: User space pointer to start of block to check
118  * @size: Size of block to check
119  *
120  * Context: User context only.  This function may sleep.
121  *
122  * This function has been replaced by access_ok().
123  *
124  * Checks if a pointer to a block of memory in user space is valid.
125  *
126  * Returns zero if the memory block may be valid, -EFAULT
127  * if it is definitely invalid.
128  *
129  * See access_ok() for more details.
130  */
131 static inline int verify_area(int type, const void * addr, unsigned long size)
132 {
133         return access_ok(type, addr, size) ? 0 : -EFAULT;
134 }
135
136 /*
137  * put_user: - Write a simple value into user space.
138  * @x:   Value to copy to user space.
139  * @ptr: Destination address, in user space.
140  *
141  * Context: User context only.  This function may sleep.
142  *
143  * This macro copies a single simple value from kernel space to user
144  * space.  It supports simple types like char and int, but not larger
145  * data types like structures or arrays.
146  *
147  * @ptr must have pointer-to-simple-variable type, and @x must be assignable
148  * to the result of dereferencing @ptr.
149  *
150  * Returns zero on success, or -EFAULT on error.
151  */
152 #define put_user(x,ptr) \
153         __put_user_check((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
154
155 /*
156  * get_user: - Get a simple variable from user space.
157  * @x:   Variable to store result.
158  * @ptr: Source address, in user space.
159  *
160  * Context: User context only.  This function may sleep.
161  *
162  * This macro copies a single simple variable from user space to kernel
163  * space.  It supports simple types like char and int, but not larger
164  * data types like structures or arrays.
165  *
166  * @ptr must have pointer-to-simple-variable type, and the result of
167  * dereferencing @ptr must be assignable to @x without a cast.
168  *
169  * Returns zero on success, or -EFAULT on error.
170  * On error, the variable @x is set to zero.
171  */
172 #define get_user(x,ptr) \
173         __get_user_check((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
174
175 /*
176  * __put_user: - Write a simple value into user space, with less checking.
177  * @x:   Value to copy to user space.
178  * @ptr: Destination address, in user space.
179  *
180  * Context: User context only.  This function may sleep.
181  *
182  * This macro copies a single simple value from kernel space to user
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 @x must be assignable
187  * to the result of dereferencing @ptr.
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  */
194 #define __put_user(x,ptr) \
195         __put_user_nocheck((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
196
197 /*
198  * __get_user: - Get a simple variable from user space, with less checking.
199  * @x:   Variable to store result.
200  * @ptr: Source address, in user space.
201  *
202  * Context: User context only.  This function may sleep.
203  *
204  * This macro copies a single simple variable from user space to kernel
205  * space.  It supports simple types like char and int, but not larger
206  * data types like structures or arrays.
207  *
208  * @ptr must have pointer-to-simple-variable type, and the result of
209  * dereferencing @ptr must be assignable to @x without a cast.
210  *
211  * Caller must check the pointer with access_ok() before calling this
212  * function.
213  *
214  * Returns zero on success, or -EFAULT on error.
215  * On error, the variable @x is set to zero.
216  */
217 #define __get_user(x,ptr) \
218         __get_user_nocheck((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
219
220 struct __large_struct { unsigned long buf[100]; };
221 #define __m(x) (*(struct __large_struct *)(x))
222
223 /*
224  * Yuck.  We need two variants, one for 64bit operation and one
225  * for 32 bit mode and old iron.
226  */
227 #ifdef __mips64
228 #define __GET_USER_DW(__gu_err) __get_user_asm("ld", __gu_err)
229 #else
230 #define __GET_USER_DW(__gu_err) __get_user_asm_ll32(__gu_err)
231 #endif
232
233 #define __get_user_nocheck(x,ptr,size)                                  \
234 ({                                                                      \
235         long __gu_err = 0;                                              \
236         __typeof(*(ptr)) __gu_val = 0;                                  \
237         long __gu_addr;                                                 \
238         might_sleep();                                                  \
239         __gu_addr = (long) (ptr);                                       \
240         switch (size) {                                                 \
241         case 1: __get_user_asm("lb", __gu_err); break;                  \
242         case 2: __get_user_asm("lh", __gu_err); break;                  \
243         case 4: __get_user_asm("lw", __gu_err); break;                  \
244         case 8: __GET_USER_DW(__gu_err); break;                         \
245         default: __get_user_unknown(); break;                           \
246         }                                                               \
247          x = (__typeof__(*(ptr))) __gu_val;                             \
248         __gu_err;                                                       \
249 })
250
251 #define __get_user_check(x,ptr,size)                                    \
252 ({                                                                      \
253         __typeof__(*(ptr)) __gu_val = 0;                                \
254         long __gu_addr = (long) (ptr);                                  \
255         long __gu_err;                                                  \
256                                                                         \
257         might_sleep();                                                  \
258         __gu_err = verify_area(VERIFY_READ, (void *) __gu_addr, size);  \
259                                                                         \
260         if (likely(!__gu_err)) {                                        \
261                 switch (size) {                                         \
262                 case 1: __get_user_asm("lb", __gu_err); break;          \
263                 case 2: __get_user_asm("lh", __gu_err); break;          \
264                 case 4: __get_user_asm("lw", __gu_err); break;          \
265                 case 8: __GET_USER_DW(__gu_err); break;                 \
266                 default: __get_user_unknown(); break;                   \
267                 }                                                       \
268         }                                                               \
269         x = (__typeof__(*(ptr))) __gu_val;                              \
270          __gu_err;                                                      \
271 })
272
273 #define __get_user_asm(insn,__gu_err)                                   \
274 ({                                                                      \
275         __asm__ __volatile__(                                           \
276         "1:     " insn "        %1, %3                          \n"     \
277         "2:                                                     \n"     \
278         "       .section .fixup,\"ax\"                          \n"     \
279         "3:     li      %0, %4                                  \n"     \
280         "       j       2b                                      \n"     \
281         "       .previous                                       \n"     \
282         "       .section __ex_table,\"a\"                       \n"     \
283         "       "__UA_ADDR "\t1b, 3b                            \n"     \
284         "       .previous                                       \n"     \
285         : "=r" (__gu_err), "=r" (__gu_val)                              \
286         : "0" (__gu_err), "o" (__m(__gu_addr)), "i" (-EFAULT));         \
287 })
288
289 /*
290  * Get a long long 64 using 32 bit registers.
291  */
292 #define __get_user_asm_ll32(__gu_err)                                   \
293 ({                                                                      \
294         __asm__ __volatile__(                                           \
295         "1:     lw      %1, %3                                  \n"     \
296         "2:     lw      %D1, %4                                 \n"     \
297         "       move    %0, $0                                  \n"     \
298         "3:     .section        .fixup,\"ax\"                   \n"     \
299         "4:     li      %0, %5                                  \n"     \
300         "       move    %1, $0                                  \n"     \
301         "       move    %D1, $0                                 \n"     \
302         "       j       3b                                      \n"     \
303         "       .previous                                       \n"     \
304         "       .section        __ex_table,\"a\"                \n"     \
305         "       " __UA_ADDR "   1b, 4b                          \n"     \
306         "       " __UA_ADDR "   2b, 4b                          \n"     \
307         "       .previous                                       \n"     \
308         : "=r" (__gu_err), "=&r" (__gu_val)                             \
309         : "0" (__gu_err), "o" (__m(__gu_addr)),                         \
310           "o" (__m(__gu_addr + 4)), "i" (-EFAULT));                     \
311 })
312
313 extern void __get_user_unknown(void);
314
315 /*
316  * Yuck.  We need two variants, one for 64bit operation and one
317  * for 32 bit mode and old iron.
318  */
319 #ifdef __mips64
320 #define __PUT_USER_DW(__pu_val) __put_user_asm("sd", __pu_val)
321 #else
322 #define __PUT_USER_DW(__pu_val) __put_user_asm_ll32(__pu_val)
323 #endif
324
325 #define __put_user_nocheck(x,ptr,size)                                  \
326 ({                                                                      \
327         long __pu_err = 0;                                              \
328         __typeof__(*(ptr)) __pu_val;                                    \
329         long __pu_addr;                                                 \
330         might_sleep();                                                  \
331         __pu_val = (x);                                                 \
332         __pu_addr = (long) (ptr);                                       \
333         switch (size) {                                                 \
334         case 1: __put_user_asm("sb", __pu_val); break;                  \
335         case 2: __put_user_asm("sh", __pu_val); break;                  \
336         case 4: __put_user_asm("sw", __pu_val); break;                  \
337         case 8: __PUT_USER_DW(__pu_val); break;                         \
338         default: __put_user_unknown(); break;                           \
339         }                                                               \
340         __pu_err;                                                       \
341 })
342
343 #define __put_user_check(x,ptr,size)                                    \
344 ({                                                                      \
345         __typeof__(*(ptr)) __pu_val = (x);                              \
346         long __pu_addr = (long) (ptr);                                  \
347         long __pu_err;                                                  \
348                                                                         \
349         might_sleep();                                                  \
350         __pu_err = verify_area(VERIFY_WRITE, (void *) __pu_addr, size); \
351                                                                         \
352         if (likely(!__pu_err)) {                                        \
353                 switch (size) {                                         \
354                 case 1: __put_user_asm("sb", __pu_val); break;          \
355                 case 2: __put_user_asm("sh", __pu_val); break;          \
356                 case 4: __put_user_asm("sw", __pu_val); break;          \
357                 case 8: __PUT_USER_DW(__pu_val); break;                 \
358                 default: __put_user_unknown(); break;                   \
359                 }                                                       \
360         }                                                               \
361         __pu_err;                                                       \
362 })
363
364 #define __put_user_asm(insn, __pu_val)                                  \
365 ({                                                                      \
366         __asm__ __volatile__(                                           \
367         "1:     " insn "        %z2, %3         # __put_user_asm\n"     \
368         "2:                                                     \n"     \
369         "       .section        .fixup,\"ax\"                   \n"     \
370         "3:     li      %0, %4                                  \n"     \
371         "       j       2b                                      \n"     \
372         "       .previous                                       \n"     \
373         "       .section        __ex_table,\"a\"                \n"     \
374         "       " __UA_ADDR "   1b, 3b                          \n"     \
375         "       .previous                                       \n"     \
376         : "=r" (__pu_err)                                               \
377         : "0" (__pu_err), "Jr" (__pu_val), "o" (__m(__pu_addr)),        \
378           "i" (-EFAULT));                                               \
379 })
380
381 #define __put_user_asm_ll32(__pu_val)                                   \
382 ({                                                                      \
383         __asm__ __volatile__(                                           \
384         "1:     sw      %2, %3          # __put_user_asm_ll32   \n"     \
385         "2:     sw      %D2, %4                                 \n"     \
386         "3:                                                     \n"     \
387         "       .section        .fixup,\"ax\"                   \n"     \
388         "4:     li      %0, %5                                  \n"     \
389         "       j       3b                                      \n"     \
390         "       .previous                                       \n"     \
391         "       .section        __ex_table,\"a\"                \n"     \
392         "       " __UA_ADDR "   1b, 4b                          \n"     \
393         "       " __UA_ADDR "   2b, 4b                          \n"     \
394         "       .previous"                                              \
395         : "=r" (__pu_err)                                               \
396         : "0" (__pu_err), "r" (__pu_val), "o" (__m(__pu_addr)),         \
397           "o" (__m(__pu_addr + 4)), "i" (-EFAULT));                     \
398 })
399
400 extern void __put_user_unknown(void);
401
402 /*
403  * We're generating jump to subroutines which will be outside the range of
404  * jump instructions
405  */
406 #ifdef MODULE
407 #define __MODULE_JAL(destination)                                       \
408         ".set\tnoat\n\t"                                                \
409         __UA_LA "\t$1, " #destination "\n\t"                            \
410         "jalr\t$1\n\t"                                                  \
411         ".set\tat\n\t"
412 #else
413 #define __MODULE_JAL(destination)                                       \
414         "jal\t" #destination "\n\t"
415 #endif
416
417 extern size_t __copy_user(void *__to, const void *__from, size_t __n);
418
419 #define __invoke_copy_to_user(to,from,n)                                \
420 ({                                                                      \
421         register void *__cu_to_r __asm__ ("$4");                        \
422         register const void *__cu_from_r __asm__ ("$5");                \
423         register long __cu_len_r __asm__ ("$6");                        \
424                                                                         \
425         __cu_to_r = (to);                                               \
426         __cu_from_r = (from);                                           \
427         __cu_len_r = (n);                                               \
428         __asm__ __volatile__(                                           \
429         __MODULE_JAL(__copy_user)                                       \
430         : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r)       \
431         :                                                               \
432         : "$8", "$9", "$10", "$11", "$12", "$15", "$24", "$31",         \
433           "memory");                                                    \
434         __cu_len_r;                                                     \
435 })
436
437 /*
438  * __copy_to_user: - Copy a block of data into user space, with less checking.
439  * @to:   Destination address, in user space.
440  * @from: Source address, in kernel space.
441  * @n:    Number of bytes to copy.
442  *
443  * Context: User context only.  This function may sleep.
444  *
445  * Copy data from kernel space to user space.  Caller must check
446  * the specified block with access_ok() before calling this function.
447  *
448  * Returns number of bytes that could not be copied.
449  * On success, this will be zero.
450  */
451 #define __copy_to_user(to,from,n)                                       \
452 ({                                                                      \
453         void *__cu_to;                                                  \
454         const void *__cu_from;                                          \
455         long __cu_len;                                                  \
456                                                                         \
457         might_sleep();                                                  \
458         __cu_to = (to);                                                 \
459         __cu_from = (from);                                             \
460         __cu_len = (n);                                                 \
461         __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, __cu_len); \
462         __cu_len;                                                       \
463 })
464
465 #define __copy_to_user_inatomic __copy_to_user
466 #define __copy_from_user_inatomic __copy_from_user
467
468 /*
469  * copy_to_user: - Copy a block of data into user space.
470  * @to:   Destination address, in user space.
471  * @from: Source address, in kernel space.
472  * @n:    Number of bytes to copy.
473  *
474  * Context: User context only.  This function may sleep.
475  *
476  * Copy data from kernel space to user space.
477  *
478  * Returns number of bytes that could not be copied.
479  * On success, this will be zero.
480  */
481 #define copy_to_user(to,from,n)                                         \
482 ({                                                                      \
483         void *__cu_to;                                                  \
484         const void *__cu_from;                                          \
485         long __cu_len;                                                  \
486                                                                         \
487         might_sleep();                                                  \
488         __cu_to = (to);                                                 \
489         __cu_from = (from);                                             \
490         __cu_len = (n);                                                 \
491         if (access_ok(VERIFY_WRITE, __cu_to, __cu_len))                 \
492                 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from,    \
493                                                  __cu_len);             \
494         __cu_len;                                                       \
495 })
496
497 #define __invoke_copy_from_user(to,from,n)                              \
498 ({                                                                      \
499         register void *__cu_to_r __asm__ ("$4");                        \
500         register const void *__cu_from_r __asm__ ("$5");                \
501         register long __cu_len_r __asm__ ("$6");                        \
502                                                                         \
503         __cu_to_r = (to);                                               \
504         __cu_from_r = (from);                                           \
505         __cu_len_r = (n);                                               \
506         __asm__ __volatile__(                                           \
507         ".set\tnoreorder\n\t"                                           \
508         __MODULE_JAL(__copy_user)                                       \
509         ".set\tnoat\n\t"                                                \
510         __UA_ADDU "\t$1, %1, %2\n\t"                                    \
511         ".set\tat\n\t"                                                  \
512         ".set\treorder"                                                 \
513         : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r)       \
514         :                                                               \
515         : "$8", "$9", "$10", "$11", "$12", "$15", "$24", "$31",         \
516           "memory");                                                    \
517         __cu_len_r;                                                     \
518 })
519
520 /*
521  * __copy_from_user: - Copy a block of data from user space, with less checking. * @to:   Destination address, in kernel space.
522  * @from: Source address, in user space.
523  * @n:    Number of bytes to copy.
524  *
525  * Context: User context only.  This function may sleep.
526  *
527  * Copy data from user space to kernel space.  Caller must check
528  * the specified block with access_ok() before calling this function.
529  *
530  * Returns number of bytes that could not be copied.
531  * On success, this will be zero.
532  *
533  * If some data could not be copied, this function will pad the copied
534  * data to the requested size using zero bytes.
535  */
536 #define __copy_from_user(to,from,n)                                     \
537 ({                                                                      \
538         void *__cu_to;                                                  \
539         const void *__cu_from;                                          \
540         long __cu_len;                                                  \
541                                                                         \
542         might_sleep();                                                  \
543         __cu_to = (to);                                                 \
544         __cu_from = (from);                                             \
545         __cu_len = (n);                                                 \
546         __cu_len = __invoke_copy_from_user(__cu_to, __cu_from,          \
547                                            __cu_len);                   \
548         __cu_len;                                                       \
549 })
550
551 /*
552  * copy_from_user: - Copy a block of data from user space.
553  * @to:   Destination address, in kernel space.
554  * @from: Source address, in user space.
555  * @n:    Number of bytes to copy.
556  *
557  * Context: User context only.  This function may sleep.
558  *
559  * Copy data from user space to kernel space.
560  *
561  * Returns number of bytes that could not be copied.
562  * On success, this will be zero.
563  *
564  * If some data could not be copied, this function will pad the copied
565  * data to the requested size using zero bytes.
566  */
567 #define copy_from_user(to,from,n)                                       \
568 ({                                                                      \
569         void *__cu_to;                                                  \
570         const void *__cu_from;                                          \
571         long __cu_len;                                                  \
572                                                                         \
573         might_sleep();                                                  \
574         __cu_to = (to);                                                 \
575         __cu_from = (from);                                             \
576         __cu_len = (n);                                                 \
577         if (access_ok(VERIFY_READ, __cu_from, __cu_len))                \
578                 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from,  \
579                                                    __cu_len);           \
580         __cu_len;                                                       \
581 })
582
583 #define __copy_in_user(to, from, n)     __copy_from_user(to, from, n)
584
585 #define copy_in_user(to,from,n)                                         \
586 ({                                                                      \
587         void *__cu_to;                                                  \
588         const void *__cu_from;                                          \
589         long __cu_len;                                                  \
590                                                                         \
591         might_sleep();                                                  \
592         __cu_to = (to);                                                 \
593         __cu_from = (from);                                             \
594         __cu_len = (n);                                                 \
595         if (likely(access_ok(VERIFY_READ, __cu_from, __cu_len) &&       \
596                    access_ok(VERIFY_WRITE, __cu_to, __cu_len)))         \
597                 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from,  \
598                                                    __cu_len);           \
599         __cu_len;                                                       \
600 })
601
602 /*
603  * __clear_user: - Zero a block of memory in user space, with less checking.
604  * @to:   Destination address, in user space.
605  * @n:    Number of bytes to zero.
606  *
607  * Zero a block of memory in user space.  Caller must check
608  * the specified block with access_ok() before calling this function.
609  *
610  * Returns number of bytes that could not be cleared.
611  * On success, this will be zero.
612  */
613 static inline __kernel_size_t
614 __clear_user(void *addr, __kernel_size_t size)
615 {
616         __kernel_size_t res;
617
618         might_sleep();
619         __asm__ __volatile__(
620                 "move\t$4, %1\n\t"
621                 "move\t$5, $0\n\t"
622                 "move\t$6, %2\n\t"
623                 __MODULE_JAL(__bzero)
624                 "move\t%0, $6"
625                 : "=r" (res)
626                 : "r" (addr), "r" (size)
627                 : "$4", "$5", "$6", __UA_t0, __UA_t1, "$31");
628
629         return res;
630 }
631
632 #define clear_user(addr,n)                                              \
633 ({                                                                      \
634         void * __cl_addr = (addr);                                      \
635         unsigned long __cl_size = (n);                                  \
636         if (__cl_size && access_ok(VERIFY_WRITE,                        \
637                 ((unsigned long)(__cl_addr)), __cl_size))               \
638                 __cl_size = __clear_user(__cl_addr, __cl_size);         \
639         __cl_size;                                                      \
640 })
641
642 /*
643  * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
644  * @dst:   Destination address, in kernel space.  This buffer must be at
645  *         least @count bytes long.
646  * @src:   Source address, in user space.
647  * @count: Maximum number of bytes to copy, including the trailing NUL.
648  *
649  * Copies a NUL-terminated string from userspace to kernel space.
650  * Caller must check the specified block with access_ok() before calling
651  * this function.
652  *
653  * On success, returns the length of the string (not including the trailing
654  * NUL).
655  *
656  * If access to userspace fails, returns -EFAULT (some data may have been
657  * copied).
658  *
659  * If @count is smaller than the length of the string, copies @count bytes
660  * and returns @count.
661  */
662 static inline long
663 __strncpy_from_user(char *__to, const char *__from, long __len)
664 {
665         long res;
666
667         might_sleep();
668         __asm__ __volatile__(
669                 "move\t$4, %1\n\t"
670                 "move\t$5, %2\n\t"
671                 "move\t$6, %3\n\t"
672                 __MODULE_JAL(__strncpy_from_user_nocheck_asm)
673                 "move\t%0, $2"
674                 : "=r" (res)
675                 : "r" (__to), "r" (__from), "r" (__len)
676                 : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
677
678         return res;
679 }
680
681 /*
682  * strncpy_from_user: - Copy a NUL terminated string from userspace.
683  * @dst:   Destination address, in kernel space.  This buffer must be at
684  *         least @count bytes long.
685  * @src:   Source address, in user space.
686  * @count: Maximum number of bytes to copy, including the trailing NUL.
687  *
688  * Copies a NUL-terminated string from userspace to kernel space.
689  *
690  * On success, returns the length of the string (not including the trailing
691  * NUL).
692  *
693  * If access to userspace fails, returns -EFAULT (some data may have been
694  * copied).
695  *
696  * If @count is smaller than the length of the string, copies @count bytes
697  * and returns @count.
698  */
699 static inline long
700 strncpy_from_user(char *__to, const char *__from, long __len)
701 {
702         long res;
703
704         might_sleep();
705         __asm__ __volatile__(
706                 "move\t$4, %1\n\t"
707                 "move\t$5, %2\n\t"
708                 "move\t$6, %3\n\t"
709                 __MODULE_JAL(__strncpy_from_user_asm)
710                 "move\t%0, $2"
711                 : "=r" (res)
712                 : "r" (__to), "r" (__from), "r" (__len)
713                 : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
714
715         return res;
716 }
717
718 /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
719 static inline long __strlen_user(const char *s)
720 {
721         long res;
722
723         might_sleep();
724         __asm__ __volatile__(
725                 "move\t$4, %1\n\t"
726                 __MODULE_JAL(__strlen_user_nocheck_asm)
727                 "move\t%0, $2"
728                 : "=r" (res)
729                 : "r" (s)
730                 : "$2", "$4", __UA_t0, "$31");
731
732         return res;
733 }
734
735 /*
736  * strlen_user: - Get the size of a string in user space.
737  * @str: The string to measure.
738  *
739  * Context: User context only.  This function may sleep.
740  *
741  * Get the size of a NUL-terminated string in user space.
742  *
743  * Returns the size of the string INCLUDING the terminating NUL.
744  * On exception, returns 0.
745  *
746  * If there is a limit on the length of a valid string, you may wish to
747  * consider using strnlen_user() instead.
748  */
749 static inline long strlen_user(const char *s)
750 {
751         long res;
752
753         might_sleep();
754         __asm__ __volatile__(
755                 "move\t$4, %1\n\t"
756                 __MODULE_JAL(__strlen_user_asm)
757                 "move\t%0, $2"
758                 : "=r" (res)
759                 : "r" (s)
760                 : "$2", "$4", __UA_t0, "$31");
761
762         return res;
763 }
764
765 /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
766 static inline long __strnlen_user(const char *s, long n)
767 {
768         long res;
769
770         might_sleep();
771         __asm__ __volatile__(
772                 "move\t$4, %1\n\t"
773                 "move\t$5, %2\n\t"
774                 __MODULE_JAL(__strnlen_user_nocheck_asm)
775                 "move\t%0, $2"
776                 : "=r" (res)
777                 : "r" (s), "r" (n)
778                 : "$2", "$4", "$5", __UA_t0, "$31");
779
780         return res;
781 }
782
783 /*
784  * strlen_user: - Get the size of a string in user space.
785  * @str: The string to measure.
786  *
787  * Context: User context only.  This function may sleep.
788  *
789  * Get the size of a NUL-terminated string in user space.
790  *
791  * Returns the size of the string INCLUDING the terminating NUL.
792  * On exception, returns 0.
793  *
794  * If there is a limit on the length of a valid string, you may wish to
795  * consider using strnlen_user() instead.
796  */
797 static inline long strnlen_user(const char *s, long n)
798 {
799         long res;
800
801         might_sleep();
802         __asm__ __volatile__(
803                 "move\t$4, %1\n\t"
804                 "move\t$5, %2\n\t"
805                 __MODULE_JAL(__strnlen_user_asm)
806                 "move\t%0, $2"
807                 : "=r" (res)
808                 : "r" (s), "r" (n)
809                 : "$2", "$4", "$5", __UA_t0, "$31");
810
811         return res;
812 }
813
814 struct exception_table_entry
815 {
816         unsigned long insn;
817         unsigned long nextinsn;
818 };
819
820 extern int fixup_exception(struct pt_regs *regs);
821
822 #endif /* _ASM_UACCESS_H */