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
6 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 03, 04 by Ralf Baechle
7 * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
8 * Copyright (C) 2007 Maciej W. Rozycki
10 #ifndef _ASM_UACCESS_H
11 #define _ASM_UACCESS_H
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/thread_info.h>
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.
22 * For historical reasons, these macros are grossly misnamed.
26 #define __UA_LIMIT 0x80000000UL
28 #define __UA_ADDR ".word"
30 #define __UA_ADDU "addu"
34 #endif /* CONFIG_32BIT */
38 #define __UA_LIMIT (- TASK_SIZE)
40 #define __UA_ADDR ".dword"
42 #define __UA_ADDU "daddu"
46 #endif /* CONFIG_64BIT */
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 :-)
56 #define KERNEL_DS ((mm_segment_t) { 0UL })
57 #define USER_DS ((mm_segment_t) { __UA_LIMIT })
60 #define VERIFY_WRITE 1
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))
66 #define segment_eq(a, b) ((a).seg == (b).seg)
70 * Is a address valid? This does a straighforward calculation rather
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.
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.
82 #define __ua_size(size) \
83 ((__builtin_constant_p(size) && (signed long) (size) > 0) ? 0 : (size))
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
93 * Context: User context only. This function may sleep.
95 * Checks if a pointer to a block of memory in user space is valid.
97 * Returns true (nonzero) if the memory block may be valid, false (zero)
98 * if it is definitely invalid.
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.
105 #define __access_mask get_fs().seg
107 #define __access_ok(addr, size, mask) \
108 (((signed long)((mask) & ((addr) | ((addr) + (size)) | __ua_size(size)))) == 0)
110 #define access_ok(type, addr, size) \
111 likely(__access_ok((unsigned long)(addr), (size), __access_mask))
114 * put_user: - Write a simple value into user space.
115 * @x: Value to copy to user space.
116 * @ptr: Destination address, in user space.
118 * Context: User context only. This function may sleep.
120 * This macro copies a single simple value from kernel space to user
121 * space. It supports simple types like char and int, but not larger
122 * data types like structures or arrays.
124 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
125 * to the result of dereferencing @ptr.
127 * Returns zero on success, or -EFAULT on error.
129 #define put_user(x,ptr) \
130 __put_user_check((x), (ptr), sizeof(*(ptr)))
133 * get_user: - Get a simple variable from user space.
134 * @x: Variable to store result.
135 * @ptr: Source address, in user space.
137 * Context: User context only. This function may sleep.
139 * This macro copies a single simple variable from user space to kernel
140 * space. It supports simple types like char and int, but not larger
141 * data types like structures or arrays.
143 * @ptr must have pointer-to-simple-variable type, and the result of
144 * dereferencing @ptr must be assignable to @x without a cast.
146 * Returns zero on success, or -EFAULT on error.
147 * On error, the variable @x is set to zero.
149 #define get_user(x,ptr) \
150 __get_user_check((x), (ptr), sizeof(*(ptr)))
153 * __put_user: - Write a simple value into user space, with less checking.
154 * @x: Value to copy to user space.
155 * @ptr: Destination address, in user space.
157 * Context: User context only. This function may sleep.
159 * This macro copies a single simple value from kernel space to user
160 * space. It supports simple types like char and int, but not larger
161 * data types like structures or arrays.
163 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
164 * to the result of dereferencing @ptr.
166 * Caller must check the pointer with access_ok() before calling this
169 * Returns zero on success, or -EFAULT on error.
171 #define __put_user(x,ptr) \
172 __put_user_nocheck((x), (ptr), sizeof(*(ptr)))
175 * __get_user: - Get a simple variable from user space, with less checking.
176 * @x: Variable to store result.
177 * @ptr: Source address, in user space.
179 * Context: User context only. This function may sleep.
181 * This macro copies a single simple variable from user space to kernel
182 * space. It supports simple types like char and int, but not larger
183 * data types like structures or arrays.
185 * @ptr must have pointer-to-simple-variable type, and the result of
186 * dereferencing @ptr must be assignable to @x without a cast.
188 * Caller must check the pointer with access_ok() before calling this
191 * Returns zero on success, or -EFAULT on error.
192 * On error, the variable @x is set to zero.
194 #define __get_user(x,ptr) \
195 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
197 struct __large_struct { unsigned long buf[100]; };
198 #define __m(x) (*(struct __large_struct __user *)(x))
201 * Yuck. We need two variants, one for 64bit operation and one
202 * for 32 bit mode and old iron.
205 #define __GET_USER_DW(val, ptr) __get_user_asm_ll32(val, ptr)
208 #define __GET_USER_DW(val, ptr) __get_user_asm(val, "ld", ptr)
211 extern void __get_user_unknown(void);
213 #define __get_user_common(val, size, ptr) \
216 case 1: __get_user_asm(val, "lb", ptr); break; \
217 case 2: __get_user_asm(val, "lh", ptr); break; \
218 case 4: __get_user_asm(val, "lw", ptr); break; \
219 case 8: __GET_USER_DW(val, ptr); break; \
220 default: __get_user_unknown(); break; \
224 #define __get_user_nocheck(x, ptr, size) \
228 __get_user_common((x), size, ptr); \
232 #define __get_user_check(x, ptr, size) \
234 int __gu_err = -EFAULT; \
235 const __typeof__(*(ptr)) __user * __gu_ptr = (ptr); \
237 if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) \
238 __get_user_common((x), size, __gu_ptr); \
243 #define __get_user_asm(val, insn, addr) \
247 __asm__ __volatile__( \
248 "1: " insn " %1, %3 \n" \
250 " .section .fixup,\"ax\" \n" \
254 " .section __ex_table,\"a\" \n" \
255 " "__UA_ADDR "\t1b, 3b \n" \
257 : "=r" (__gu_err), "=r" (__gu_tmp) \
258 : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \
260 (val) = (__typeof__(*(addr))) __gu_tmp; \
264 * Get a long long 64 using 32 bit registers.
266 #define __get_user_asm_ll32(val, addr) \
269 unsigned long long l; \
270 __typeof__(*(addr)) t; \
273 __asm__ __volatile__( \
274 "1: lw %1, (%3) \n" \
275 "2: lw %D1, 4(%3) \n" \
276 "3: .section .fixup,\"ax\" \n" \
282 " .section __ex_table,\"a\" \n" \
283 " " __UA_ADDR " 1b, 4b \n" \
284 " " __UA_ADDR " 2b, 4b \n" \
286 : "=r" (__gu_err), "=&r" (__gu_tmp.l) \
287 : "0" (0), "r" (addr), "i" (-EFAULT)); \
289 (val) = __gu_tmp.t; \
293 * Yuck. We need two variants, one for 64bit operation and one
294 * for 32 bit mode and old iron.
297 #define __PUT_USER_DW(ptr) __put_user_asm_ll32(ptr)
300 #define __PUT_USER_DW(ptr) __put_user_asm("sd", ptr)
303 #define __put_user_nocheck(x, ptr, size) \
305 __typeof__(*(ptr)) __pu_val; \
310 case 1: __put_user_asm("sb", ptr); break; \
311 case 2: __put_user_asm("sh", ptr); break; \
312 case 4: __put_user_asm("sw", ptr); break; \
313 case 8: __PUT_USER_DW(ptr); break; \
314 default: __put_user_unknown(); break; \
319 #define __put_user_check(x, ptr, size) \
321 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
322 __typeof__(*(ptr)) __pu_val = (x); \
323 int __pu_err = -EFAULT; \
325 if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) { \
327 case 1: __put_user_asm("sb", __pu_addr); break; \
328 case 2: __put_user_asm("sh", __pu_addr); break; \
329 case 4: __put_user_asm("sw", __pu_addr); break; \
330 case 8: __PUT_USER_DW(__pu_addr); break; \
331 default: __put_user_unknown(); break; \
337 #define __put_user_asm(insn, ptr) \
339 __asm__ __volatile__( \
340 "1: " insn " %z2, %3 # __put_user_asm\n" \
342 " .section .fixup,\"ax\" \n" \
346 " .section __ex_table,\"a\" \n" \
347 " " __UA_ADDR " 1b, 3b \n" \
350 : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)), \
354 #define __put_user_asm_ll32(ptr) \
356 __asm__ __volatile__( \
357 "1: sw %2, (%3) # __put_user_asm_ll32 \n" \
358 "2: sw %D2, 4(%3) \n" \
360 " .section .fixup,\"ax\" \n" \
364 " .section __ex_table,\"a\" \n" \
365 " " __UA_ADDR " 1b, 4b \n" \
366 " " __UA_ADDR " 2b, 4b \n" \
369 : "0" (0), "r" (__pu_val), "r" (ptr), \
373 extern void __put_user_unknown(void);
376 * put_user_unaligned: - Write a simple value into user space.
377 * @x: Value to copy to user space.
378 * @ptr: Destination address, in user space.
380 * Context: User context only. This function may sleep.
382 * This macro copies a single simple value from kernel space to user
383 * space. It supports simple types like char and int, but not larger
384 * data types like structures or arrays.
386 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
387 * to the result of dereferencing @ptr.
389 * Returns zero on success, or -EFAULT on error.
391 #define put_user_unaligned(x,ptr) \
392 __put_user_unaligned_check((x),(ptr),sizeof(*(ptr)))
395 * get_user_unaligned: - Get a simple variable from user space.
396 * @x: Variable to store result.
397 * @ptr: Source address, in user space.
399 * Context: User context only. This function may sleep.
401 * This macro copies a single simple variable from user space to kernel
402 * space. It supports simple types like char and int, but not larger
403 * data types like structures or arrays.
405 * @ptr must have pointer-to-simple-variable type, and the result of
406 * dereferencing @ptr must be assignable to @x without a cast.
408 * Returns zero on success, or -EFAULT on error.
409 * On error, the variable @x is set to zero.
411 #define get_user_unaligned(x,ptr) \
412 __get_user_unaligned_check((x),(ptr),sizeof(*(ptr)))
415 * __put_user_unaligned: - Write a simple value into user space, with less checking.
416 * @x: Value to copy to user space.
417 * @ptr: Destination address, in user space.
419 * Context: User context only. This function may sleep.
421 * This macro copies a single simple value from kernel space to user
422 * space. It supports simple types like char and int, but not larger
423 * data types like structures or arrays.
425 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
426 * to the result of dereferencing @ptr.
428 * Caller must check the pointer with access_ok() before calling this
431 * Returns zero on success, or -EFAULT on error.
433 #define __put_user_unaligned(x,ptr) \
434 __put_user_unaligned_nocheck((x),(ptr),sizeof(*(ptr)))
437 * __get_user_unaligned: - Get a simple variable from user space, with less checking.
438 * @x: Variable to store result.
439 * @ptr: Source address, in user space.
441 * Context: User context only. This function may sleep.
443 * This macro copies a single simple variable from user space to kernel
444 * space. It supports simple types like char and int, but not larger
445 * data types like structures or arrays.
447 * @ptr must have pointer-to-simple-variable type, and the result of
448 * dereferencing @ptr must be assignable to @x without a cast.
450 * Caller must check the pointer with access_ok() before calling this
453 * Returns zero on success, or -EFAULT on error.
454 * On error, the variable @x is set to zero.
456 #define __get_user_unaligned(x,ptr) \
457 __get_user__unalignednocheck((x),(ptr),sizeof(*(ptr)))
460 * Yuck. We need two variants, one for 64bit operation and one
461 * for 32 bit mode and old iron.
464 #define __GET_USER_UNALIGNED_DW(val, ptr) \
465 __get_user_unaligned_asm_ll32(val, ptr)
468 #define __GET_USER_UNALIGNED_DW(val, ptr) \
469 __get_user_unaligned_asm(val, "uld", ptr)
472 extern void __get_user_unaligned_unknown(void);
474 #define __get_user_unaligned_common(val, size, ptr) \
477 case 1: __get_user_asm(val, "lb", ptr); break; \
478 case 2: __get_user_unaligned_asm(val, "ulh", ptr); break; \
479 case 4: __get_user_unaligned_asm(val, "ulw", ptr); break; \
480 case 8: __GET_USER_UNALIGNED_DW(val, ptr); break; \
481 default: __get_user_unaligned_unknown(); break; \
485 #define __get_user_unaligned_nocheck(x,ptr,size) \
489 __get_user_unaligned_common((x), size, ptr); \
493 #define __get_user_unaligned_check(x,ptr,size) \
495 int __gu_err = -EFAULT; \
496 const __typeof__(*(ptr)) __user * __gu_ptr = (ptr); \
498 if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) \
499 __get_user_unaligned_common((x), size, __gu_ptr); \
504 #define __get_user_unaligned_asm(val, insn, addr) \
508 __asm__ __volatile__( \
509 "1: " insn " %1, %3 \n" \
511 " .section .fixup,\"ax\" \n" \
515 " .section __ex_table,\"a\" \n" \
516 " "__UA_ADDR "\t1b, 3b \n" \
517 " "__UA_ADDR "\t1b + 4, 3b \n" \
519 : "=r" (__gu_err), "=r" (__gu_tmp) \
520 : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \
522 (val) = (__typeof__(*(addr))) __gu_tmp; \
526 * Get a long long 64 using 32 bit registers.
528 #define __get_user_unaligned_asm_ll32(val, addr) \
530 unsigned long long __gu_tmp; \
532 __asm__ __volatile__( \
533 "1: ulw %1, (%3) \n" \
534 "2: ulw %D1, 4(%3) \n" \
536 "3: .section .fixup,\"ax\" \n" \
542 " .section __ex_table,\"a\" \n" \
543 " " __UA_ADDR " 1b, 4b \n" \
544 " " __UA_ADDR " 1b + 4, 4b \n" \
545 " " __UA_ADDR " 2b, 4b \n" \
546 " " __UA_ADDR " 2b + 4, 4b \n" \
548 : "=r" (__gu_err), "=&r" (__gu_tmp) \
549 : "0" (0), "r" (addr), "i" (-EFAULT)); \
550 (val) = (__typeof__(*(addr))) __gu_tmp; \
554 * Yuck. We need two variants, one for 64bit operation and one
555 * for 32 bit mode and old iron.
558 #define __PUT_USER_UNALIGNED_DW(ptr) __put_user_unaligned_asm_ll32(ptr)
561 #define __PUT_USER_UNALIGNED_DW(ptr) __put_user_unaligned_asm("usd", ptr)
564 #define __put_user_unaligned_nocheck(x,ptr,size) \
566 __typeof__(*(ptr)) __pu_val; \
571 case 1: __put_user_asm("sb", ptr); break; \
572 case 2: __put_user_unaligned_asm("ush", ptr); break; \
573 case 4: __put_user_unaligned_asm("usw", ptr); break; \
574 case 8: __PUT_USER_UNALIGNED_DW(ptr); break; \
575 default: __put_user_unaligned_unknown(); break; \
580 #define __put_user_unaligned_check(x,ptr,size) \
582 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
583 __typeof__(*(ptr)) __pu_val = (x); \
584 int __pu_err = -EFAULT; \
586 if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) { \
588 case 1: __put_user_asm("sb", __pu_addr); break; \
589 case 2: __put_user_unaligned_asm("ush", __pu_addr); break; \
590 case 4: __put_user_unaligned_asm("usw", __pu_addr); break; \
591 case 8: __PUT_USER_UNALGINED_DW(__pu_addr); break; \
592 default: __put_user_unaligned_unknown(); break; \
598 #define __put_user_unaligned_asm(insn, ptr) \
600 __asm__ __volatile__( \
601 "1: " insn " %z2, %3 # __put_user_unaligned_asm\n" \
603 " .section .fixup,\"ax\" \n" \
607 " .section __ex_table,\"a\" \n" \
608 " " __UA_ADDR " 1b, 3b \n" \
611 : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)), \
615 #define __put_user_unaligned_asm_ll32(ptr) \
617 __asm__ __volatile__( \
618 "1: sw %2, (%3) # __put_user_unaligned_asm_ll32 \n" \
619 "2: sw %D2, 4(%3) \n" \
621 " .section .fixup,\"ax\" \n" \
625 " .section __ex_table,\"a\" \n" \
626 " " __UA_ADDR " 1b, 4b \n" \
627 " " __UA_ADDR " 1b + 4, 4b \n" \
628 " " __UA_ADDR " 2b, 4b \n" \
629 " " __UA_ADDR " 2b + 4, 4b \n" \
632 : "0" (0), "r" (__pu_val), "r" (ptr), \
636 extern void __put_user_unaligned_unknown(void);
639 * We're generating jump to subroutines which will be outside the range of
643 #define __MODULE_JAL(destination) \
645 __UA_LA "\t$1, " #destination "\n\t" \
649 #define __MODULE_JAL(destination) \
650 "jal\t" #destination "\n\t"
653 #ifndef CONFIG_CPU_DADDI_WORKAROUNDS
654 #define DADDI_SCRATCH "$0"
656 #define DADDI_SCRATCH "$3"
659 extern size_t __copy_user(void *__to, const void *__from, size_t __n);
661 #define __invoke_copy_to_user(to, from, n) \
663 register void __user *__cu_to_r __asm__("$4"); \
664 register const void *__cu_from_r __asm__("$5"); \
665 register long __cu_len_r __asm__("$6"); \
668 __cu_from_r = (from); \
670 __asm__ __volatile__( \
671 __MODULE_JAL(__copy_user) \
672 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
674 : "$8", "$9", "$10", "$11", "$12", "$15", "$24", "$31", \
675 DADDI_SCRATCH, "memory"); \
680 * __copy_to_user: - Copy a block of data into user space, with less checking.
681 * @to: Destination address, in user space.
682 * @from: Source address, in kernel space.
683 * @n: Number of bytes to copy.
685 * Context: User context only. This function may sleep.
687 * Copy data from kernel space to user space. Caller must check
688 * the specified block with access_ok() before calling this function.
690 * Returns number of bytes that could not be copied.
691 * On success, this will be zero.
693 #define __copy_to_user(to, from, n) \
695 void __user *__cu_to; \
696 const void *__cu_from; \
701 __cu_from = (from); \
703 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, __cu_len); \
707 extern size_t __copy_user_inatomic(void *__to, const void *__from, size_t __n);
709 #define __copy_to_user_inatomic(to, from, n) \
711 void __user *__cu_to; \
712 const void *__cu_from; \
716 __cu_from = (from); \
718 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, __cu_len); \
722 #define __copy_from_user_inatomic(to, from, n) \
725 const void __user *__cu_from; \
729 __cu_from = (from); \
731 __cu_len = __invoke_copy_from_user_inatomic(__cu_to, __cu_from, \
737 * copy_to_user: - Copy a block of data into user space.
738 * @to: Destination address, in user space.
739 * @from: Source address, in kernel space.
740 * @n: Number of bytes to copy.
742 * Context: User context only. This function may sleep.
744 * Copy data from kernel space to user space.
746 * Returns number of bytes that could not be copied.
747 * On success, this will be zero.
749 #define copy_to_user(to, from, n) \
751 void __user *__cu_to; \
752 const void *__cu_from; \
757 __cu_from = (from); \
759 if (access_ok(VERIFY_WRITE, __cu_to, __cu_len)) \
760 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, \
765 #define __invoke_copy_from_user(to, from, n) \
767 register void *__cu_to_r __asm__("$4"); \
768 register const void __user *__cu_from_r __asm__("$5"); \
769 register long __cu_len_r __asm__("$6"); \
772 __cu_from_r = (from); \
774 __asm__ __volatile__( \
775 ".set\tnoreorder\n\t" \
776 __MODULE_JAL(__copy_user) \
778 __UA_ADDU "\t$1, %1, %2\n\t" \
781 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
783 : "$8", "$9", "$10", "$11", "$12", "$15", "$24", "$31", \
784 DADDI_SCRATCH, "memory"); \
788 #define __invoke_copy_from_user_inatomic(to, from, n) \
790 register void *__cu_to_r __asm__("$4"); \
791 register const void __user *__cu_from_r __asm__("$5"); \
792 register long __cu_len_r __asm__("$6"); \
795 __cu_from_r = (from); \
797 __asm__ __volatile__( \
798 ".set\tnoreorder\n\t" \
799 __MODULE_JAL(__copy_user_inatomic) \
801 __UA_ADDU "\t$1, %1, %2\n\t" \
804 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
806 : "$8", "$9", "$10", "$11", "$12", "$15", "$24", "$31", \
807 DADDI_SCRATCH, "memory"); \
812 * __copy_from_user: - Copy a block of data from user space, with less checking.
813 * @to: Destination address, in kernel space.
814 * @from: Source address, in user space.
815 * @n: Number of bytes to copy.
817 * Context: User context only. This function may sleep.
819 * Copy data from user space to kernel space. Caller must check
820 * the specified block with access_ok() before calling this function.
822 * Returns number of bytes that could not be copied.
823 * On success, this will be zero.
825 * If some data could not be copied, this function will pad the copied
826 * data to the requested size using zero bytes.
828 #define __copy_from_user(to, from, n) \
831 const void __user *__cu_from; \
836 __cu_from = (from); \
838 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
844 * copy_from_user: - Copy a block of data from user space.
845 * @to: Destination address, in kernel space.
846 * @from: Source address, in user space.
847 * @n: Number of bytes to copy.
849 * Context: User context only. This function may sleep.
851 * Copy data from user space to kernel space.
853 * Returns number of bytes that could not be copied.
854 * On success, this will be zero.
856 * If some data could not be copied, this function will pad the copied
857 * data to the requested size using zero bytes.
859 #define copy_from_user(to, from, n) \
862 const void __user *__cu_from; \
867 __cu_from = (from); \
869 if (access_ok(VERIFY_READ, __cu_from, __cu_len)) \
870 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
875 #define __copy_in_user(to, from, n) __copy_from_user(to, from, n)
877 #define copy_in_user(to, from, n) \
879 void __user *__cu_to; \
880 const void __user *__cu_from; \
885 __cu_from = (from); \
887 if (likely(access_ok(VERIFY_READ, __cu_from, __cu_len) && \
888 access_ok(VERIFY_WRITE, __cu_to, __cu_len))) \
889 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
895 * __clear_user: - Zero a block of memory in user space, with less checking.
896 * @to: Destination address, in user space.
897 * @n: Number of bytes to zero.
899 * Zero a block of memory in user space. Caller must check
900 * the specified block with access_ok() before calling this function.
902 * Returns number of bytes that could not be cleared.
903 * On success, this will be zero.
905 static inline __kernel_size_t
906 __clear_user(void __user *addr, __kernel_size_t size)
911 __asm__ __volatile__(
915 __MODULE_JAL(__bzero)
918 : "r" (addr), "r" (size)
919 : "$4", "$5", "$6", __UA_t0, __UA_t1, "$31");
924 #define clear_user(addr,n) \
926 void __user * __cl_addr = (addr); \
927 unsigned long __cl_size = (n); \
928 if (__cl_size && access_ok(VERIFY_WRITE, \
929 ((unsigned long)(__cl_addr)), __cl_size)) \
930 __cl_size = __clear_user(__cl_addr, __cl_size); \
935 * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
936 * @dst: Destination address, in kernel space. This buffer must be at
937 * least @count bytes long.
938 * @src: Source address, in user space.
939 * @count: Maximum number of bytes to copy, including the trailing NUL.
941 * Copies a NUL-terminated string from userspace to kernel space.
942 * Caller must check the specified block with access_ok() before calling
945 * On success, returns the length of the string (not including the trailing
948 * If access to userspace fails, returns -EFAULT (some data may have been
951 * If @count is smaller than the length of the string, copies @count bytes
952 * and returns @count.
955 __strncpy_from_user(char *__to, const char __user *__from, long __len)
960 __asm__ __volatile__(
964 __MODULE_JAL(__strncpy_from_user_nocheck_asm)
967 : "r" (__to), "r" (__from), "r" (__len)
968 : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
974 * strncpy_from_user: - Copy a NUL terminated string from userspace.
975 * @dst: Destination address, in kernel space. This buffer must be at
976 * least @count bytes long.
977 * @src: Source address, in user space.
978 * @count: Maximum number of bytes to copy, including the trailing NUL.
980 * Copies a NUL-terminated string from userspace to kernel space.
982 * On success, returns the length of the string (not including the trailing
985 * If access to userspace fails, returns -EFAULT (some data may have been
988 * If @count is smaller than the length of the string, copies @count bytes
989 * and returns @count.
992 strncpy_from_user(char *__to, const char __user *__from, long __len)
997 __asm__ __volatile__(
1001 __MODULE_JAL(__strncpy_from_user_asm)
1004 : "r" (__to), "r" (__from), "r" (__len)
1005 : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
1010 /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
1011 static inline long __strlen_user(const char __user *s)
1016 __asm__ __volatile__(
1018 __MODULE_JAL(__strlen_user_nocheck_asm)
1022 : "$2", "$4", __UA_t0, "$31");
1028 * strlen_user: - Get the size of a string in user space.
1029 * @str: The string to measure.
1031 * Context: User context only. This function may sleep.
1033 * Get the size of a NUL-terminated string in user space.
1035 * Returns the size of the string INCLUDING the terminating NUL.
1036 * On exception, returns 0.
1038 * If there is a limit on the length of a valid string, you may wish to
1039 * consider using strnlen_user() instead.
1041 static inline long strlen_user(const char __user *s)
1046 __asm__ __volatile__(
1048 __MODULE_JAL(__strlen_user_asm)
1052 : "$2", "$4", __UA_t0, "$31");
1057 /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
1058 static inline long __strnlen_user(const char __user *s, long n)
1063 __asm__ __volatile__(
1066 __MODULE_JAL(__strnlen_user_nocheck_asm)
1070 : "$2", "$4", "$5", __UA_t0, "$31");
1076 * strlen_user: - Get the size of a string in user space.
1077 * @str: The string to measure.
1079 * Context: User context only. This function may sleep.
1081 * Get the size of a NUL-terminated string in user space.
1083 * Returns the size of the string INCLUDING the terminating NUL.
1084 * On exception, returns 0.
1086 * If there is a limit on the length of a valid string, you may wish to
1087 * consider using strnlen_user() instead.
1089 static inline long strnlen_user(const char __user *s, long n)
1094 __asm__ __volatile__(
1097 __MODULE_JAL(__strnlen_user_asm)
1101 : "$2", "$4", "$5", __UA_t0, "$31");
1106 struct exception_table_entry
1109 unsigned long nextinsn;
1112 extern int fixup_exception(struct pt_regs *regs);
1114 #endif /* _ASM_UACCESS_H */