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