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