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