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