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