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 #ifdef CONFIG_KVM_GUEST
27 #define __UA_LIMIT 0x40000000UL
29 #define __UA_LIMIT 0x80000000UL
32 #define __UA_ADDR ".word"
34 #define __UA_ADDU "addu"
38 #endif /* CONFIG_32BIT */
42 extern u64 __ua_limit;
44 #define __UA_LIMIT __ua_limit
46 #define __UA_ADDR ".dword"
48 #define __UA_ADDU "daddu"
52 #endif /* CONFIG_64BIT */
55 * USER_DS is a bitmask that has the bits set that may not be set in a valid
56 * userspace address. Note that we limit 32-bit userspace to 0x7fff8000 but
57 * the arithmetic we're doing only works if the limit is a power of two, so
58 * we use 0x80000000 here on 32-bit kernels. If a process passes an invalid
59 * address in this range it's the process's problem, not ours :-)
62 #ifdef CONFIG_KVM_GUEST
63 #define KERNEL_DS ((mm_segment_t) { 0x80000000UL })
64 #define USER_DS ((mm_segment_t) { 0xC0000000UL })
66 #define KERNEL_DS ((mm_segment_t) { 0UL })
67 #define USER_DS ((mm_segment_t) { __UA_LIMIT })
71 #define VERIFY_WRITE 1
73 #define get_ds() (KERNEL_DS)
74 #define get_fs() (current_thread_info()->addr_limit)
75 #define set_fs(x) (current_thread_info()->addr_limit = (x))
77 #define segment_eq(a, b) ((a).seg == (b).seg)
81 * Is a address valid? This does a straighforward calculation rather
85 * - "addr" doesn't have any high-bits set
86 * - AND "size" doesn't have any high-bits set
87 * - AND "addr+size" doesn't have any high-bits set
88 * - OR we are in kernel mode.
90 * __ua_size() is a trick to avoid runtime checking of positive constant
91 * sizes; for those we already know at compile time that the size is ok.
93 #define __ua_size(size) \
94 ((__builtin_constant_p(size) && (signed long) (size) > 0) ? 0 : (size))
97 * access_ok: - Checks if a user space pointer is valid
98 * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
99 * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
100 * to write to a block, it is always safe to read from it.
101 * @addr: User space pointer to start of block to check
102 * @size: Size of block to check
104 * Context: User context only. This function may sleep.
106 * Checks if a pointer to a block of memory in user space is valid.
108 * Returns true (nonzero) if the memory block may be valid, false (zero)
109 * if it is definitely invalid.
111 * Note that, depending on architecture, this function probably just
112 * checks that the pointer is in the user space range - after calling
113 * this function, memory access functions may still return -EFAULT.
116 #define __access_mask get_fs().seg
118 #define __access_ok(addr, size, mask) \
120 unsigned long __addr = (unsigned long) (addr); \
121 unsigned long __size = size; \
122 unsigned long __mask = mask; \
123 unsigned long __ok; \
125 __chk_user_ptr(addr); \
126 __ok = (signed long)(__mask & (__addr | (__addr + __size) | \
127 __ua_size(__size))); \
131 #define access_ok(type, addr, size) \
132 likely(__access_ok((addr), (size), __access_mask))
135 * put_user: - Write a simple value into user space.
136 * @x: Value to copy to user space.
137 * @ptr: Destination address, in user space.
139 * Context: User context only. This function may sleep.
141 * This macro copies a single simple value from kernel space to user
142 * space. It supports simple types like char and int, but not larger
143 * data types like structures or arrays.
145 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
146 * to the result of dereferencing @ptr.
148 * Returns zero on success, or -EFAULT on error.
150 #define put_user(x,ptr) \
151 __put_user_check((x), (ptr), sizeof(*(ptr)))
154 * get_user: - Get a simple variable from user space.
155 * @x: Variable to store result.
156 * @ptr: Source address, in user space.
158 * Context: User context only. This function may sleep.
160 * This macro copies a single simple variable from user space to kernel
161 * space. It supports simple types like char and int, but not larger
162 * data types like structures or arrays.
164 * @ptr must have pointer-to-simple-variable type, and the result of
165 * dereferencing @ptr must be assignable to @x without a cast.
167 * Returns zero on success, or -EFAULT on error.
168 * On error, the variable @x is set to zero.
170 #define get_user(x,ptr) \
171 __get_user_check((x), (ptr), sizeof(*(ptr)))
174 * __put_user: - Write a simple value into user space, with less checking.
175 * @x: Value to copy to user space.
176 * @ptr: Destination address, in user space.
178 * Context: User context only. This function may sleep.
180 * This macro copies a single simple value from kernel space to user
181 * space. It supports simple types like char and int, but not larger
182 * data types like structures or arrays.
184 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
185 * to the result of dereferencing @ptr.
187 * Caller must check the pointer with access_ok() before calling this
190 * Returns zero on success, or -EFAULT on error.
192 #define __put_user(x,ptr) \
193 __put_user_nocheck((x), (ptr), sizeof(*(ptr)))
196 * __get_user: - Get a simple variable from user space, with less checking.
197 * @x: Variable to store result.
198 * @ptr: Source address, in user space.
200 * Context: User context only. This function may sleep.
202 * This macro copies a single simple variable from user space to kernel
203 * space. It supports simple types like char and int, but not larger
204 * data types like structures or arrays.
206 * @ptr must have pointer-to-simple-variable type, and the result of
207 * dereferencing @ptr must be assignable to @x without a cast.
209 * Caller must check the pointer with access_ok() before calling this
212 * Returns zero on success, or -EFAULT on error.
213 * On error, the variable @x is set to zero.
215 #define __get_user(x,ptr) \
216 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
218 struct __large_struct { unsigned long buf[100]; };
219 #define __m(x) (*(struct __large_struct __user *)(x))
222 * Yuck. We need two variants, one for 64bit operation and one
223 * for 32 bit mode and old iron.
226 #define __GET_USER_DW(val, ptr) __get_user_asm_ll32(val, ptr)
229 #define __GET_USER_DW(val, ptr) __get_user_asm(val, "ld", ptr)
232 extern void __get_user_unknown(void);
234 #define __get_user_common(val, size, ptr) \
237 case 1: __get_user_asm(val, "lb", ptr); break; \
238 case 2: __get_user_asm(val, "lh", ptr); break; \
239 case 4: __get_user_asm(val, "lw", ptr); break; \
240 case 8: __GET_USER_DW(val, ptr); break; \
241 default: __get_user_unknown(); break; \
245 #define __get_user_nocheck(x, ptr, size) \
249 __chk_user_ptr(ptr); \
250 __get_user_common((x), size, ptr); \
254 #define __get_user_check(x, ptr, size) \
256 int __gu_err = -EFAULT; \
257 const __typeof__(*(ptr)) __user * __gu_ptr = (ptr); \
260 if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) \
261 __get_user_common((x), size, __gu_ptr); \
266 #define __get_user_asm(val, insn, addr) \
270 __asm__ __volatile__( \
271 "1: " insn " %1, %3 \n" \
274 " .section .fixup,\"ax\" \n" \
278 " .section __ex_table,\"a\" \n" \
279 " "__UA_ADDR "\t1b, 3b \n" \
281 : "=r" (__gu_err), "=r" (__gu_tmp) \
282 : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \
284 (val) = (__typeof__(*(addr))) __gu_tmp; \
288 * Get a long long 64 using 32 bit registers.
290 #define __get_user_asm_ll32(val, addr) \
293 unsigned long long l; \
294 __typeof__(*(addr)) t; \
297 __asm__ __volatile__( \
298 "1: lw %1, (%3) \n" \
299 "2: lw %D1, 4(%3) \n" \
302 " .section .fixup,\"ax\" \n" \
308 " .section __ex_table,\"a\" \n" \
309 " " __UA_ADDR " 1b, 4b \n" \
310 " " __UA_ADDR " 2b, 4b \n" \
312 : "=r" (__gu_err), "=&r" (__gu_tmp.l) \
313 : "0" (0), "r" (addr), "i" (-EFAULT)); \
315 (val) = __gu_tmp.t; \
319 * Yuck. We need two variants, one for 64bit operation and one
320 * for 32 bit mode and old iron.
323 #define __PUT_USER_DW(ptr) __put_user_asm_ll32(ptr)
326 #define __PUT_USER_DW(ptr) __put_user_asm("sd", ptr)
329 #define __put_user_nocheck(x, ptr, size) \
331 __typeof__(*(ptr)) __pu_val; \
334 __chk_user_ptr(ptr); \
337 case 1: __put_user_asm("sb", ptr); break; \
338 case 2: __put_user_asm("sh", ptr); break; \
339 case 4: __put_user_asm("sw", ptr); break; \
340 case 8: __PUT_USER_DW(ptr); break; \
341 default: __put_user_unknown(); break; \
346 #define __put_user_check(x, ptr, size) \
348 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
349 __typeof__(*(ptr)) __pu_val = (x); \
350 int __pu_err = -EFAULT; \
353 if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) { \
355 case 1: __put_user_asm("sb", __pu_addr); break; \
356 case 2: __put_user_asm("sh", __pu_addr); break; \
357 case 4: __put_user_asm("sw", __pu_addr); break; \
358 case 8: __PUT_USER_DW(__pu_addr); break; \
359 default: __put_user_unknown(); break; \
365 #define __put_user_asm(insn, ptr) \
367 __asm__ __volatile__( \
368 "1: " insn " %z2, %3 # __put_user_asm\n" \
371 " .section .fixup,\"ax\" \n" \
375 " .section __ex_table,\"a\" \n" \
376 " " __UA_ADDR " 1b, 3b \n" \
379 : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)), \
383 #define __put_user_asm_ll32(ptr) \
385 __asm__ __volatile__( \
386 "1: sw %2, (%3) # __put_user_asm_ll32 \n" \
387 "2: sw %D2, 4(%3) \n" \
390 " .section .fixup,\"ax\" \n" \
394 " .section __ex_table,\"a\" \n" \
395 " " __UA_ADDR " 1b, 4b \n" \
396 " " __UA_ADDR " 2b, 4b \n" \
399 : "0" (0), "r" (__pu_val), "r" (ptr), \
403 extern void __put_user_unknown(void);
406 * put_user_unaligned: - Write a simple value into user space.
407 * @x: Value to copy to user space.
408 * @ptr: Destination address, in user space.
410 * Context: User context only. This function may sleep.
412 * This macro copies a single simple value from kernel space to user
413 * space. It supports simple types like char and int, but not larger
414 * data types like structures or arrays.
416 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
417 * to the result of dereferencing @ptr.
419 * Returns zero on success, or -EFAULT on error.
421 #define put_user_unaligned(x,ptr) \
422 __put_user_unaligned_check((x),(ptr),sizeof(*(ptr)))
425 * get_user_unaligned: - Get a simple variable from user space.
426 * @x: Variable to store result.
427 * @ptr: Source address, in user space.
429 * Context: User context only. This function may sleep.
431 * This macro copies a single simple variable from user space to kernel
432 * space. It supports simple types like char and int, but not larger
433 * data types like structures or arrays.
435 * @ptr must have pointer-to-simple-variable type, and the result of
436 * dereferencing @ptr must be assignable to @x without a cast.
438 * Returns zero on success, or -EFAULT on error.
439 * On error, the variable @x is set to zero.
441 #define get_user_unaligned(x,ptr) \
442 __get_user_unaligned_check((x),(ptr),sizeof(*(ptr)))
445 * __put_user_unaligned: - Write a simple value into user space, with less checking.
446 * @x: Value to copy to user space.
447 * @ptr: Destination address, in user space.
449 * Context: User context only. This function may sleep.
451 * This macro copies a single simple value from kernel space to user
452 * space. It supports simple types like char and int, but not larger
453 * data types like structures or arrays.
455 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
456 * to the result of dereferencing @ptr.
458 * Caller must check the pointer with access_ok() before calling this
461 * Returns zero on success, or -EFAULT on error.
463 #define __put_user_unaligned(x,ptr) \
464 __put_user_unaligned_nocheck((x),(ptr),sizeof(*(ptr)))
467 * __get_user_unaligned: - Get a simple variable from user space, with less checking.
468 * @x: Variable to store result.
469 * @ptr: Source address, in user space.
471 * Context: User context only. This function may sleep.
473 * This macro copies a single simple variable from user space to kernel
474 * space. It supports simple types like char and int, but not larger
475 * data types like structures or arrays.
477 * @ptr must have pointer-to-simple-variable type, and the result of
478 * dereferencing @ptr must be assignable to @x without a cast.
480 * Caller must check the pointer with access_ok() before calling this
483 * Returns zero on success, or -EFAULT on error.
484 * On error, the variable @x is set to zero.
486 #define __get_user_unaligned(x,ptr) \
487 __get_user__unalignednocheck((x),(ptr),sizeof(*(ptr)))
490 * Yuck. We need two variants, one for 64bit operation and one
491 * for 32 bit mode and old iron.
494 #define __GET_USER_UNALIGNED_DW(val, ptr) \
495 __get_user_unaligned_asm_ll32(val, ptr)
498 #define __GET_USER_UNALIGNED_DW(val, ptr) \
499 __get_user_unaligned_asm(val, "uld", ptr)
502 extern void __get_user_unaligned_unknown(void);
504 #define __get_user_unaligned_common(val, size, ptr) \
507 case 1: __get_user_asm(val, "lb", ptr); break; \
508 case 2: __get_user_unaligned_asm(val, "ulh", ptr); break; \
509 case 4: __get_user_unaligned_asm(val, "ulw", ptr); break; \
510 case 8: __GET_USER_UNALIGNED_DW(val, ptr); break; \
511 default: __get_user_unaligned_unknown(); break; \
515 #define __get_user_unaligned_nocheck(x,ptr,size) \
519 __get_user_unaligned_common((x), size, ptr); \
523 #define __get_user_unaligned_check(x,ptr,size) \
525 int __gu_err = -EFAULT; \
526 const __typeof__(*(ptr)) __user * __gu_ptr = (ptr); \
528 if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) \
529 __get_user_unaligned_common((x), size, __gu_ptr); \
534 #define __get_user_unaligned_asm(val, insn, addr) \
538 __asm__ __volatile__( \
539 "1: " insn " %1, %3 \n" \
542 " .section .fixup,\"ax\" \n" \
546 " .section __ex_table,\"a\" \n" \
547 " "__UA_ADDR "\t1b, 3b \n" \
548 " "__UA_ADDR "\t1b + 4, 3b \n" \
550 : "=r" (__gu_err), "=r" (__gu_tmp) \
551 : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \
553 (val) = (__typeof__(*(addr))) __gu_tmp; \
557 * Get a long long 64 using 32 bit registers.
559 #define __get_user_unaligned_asm_ll32(val, addr) \
561 unsigned long long __gu_tmp; \
563 __asm__ __volatile__( \
564 "1: ulw %1, (%3) \n" \
565 "2: ulw %D1, 4(%3) \n" \
569 " .section .fixup,\"ax\" \n" \
575 " .section __ex_table,\"a\" \n" \
576 " " __UA_ADDR " 1b, 4b \n" \
577 " " __UA_ADDR " 1b + 4, 4b \n" \
578 " " __UA_ADDR " 2b, 4b \n" \
579 " " __UA_ADDR " 2b + 4, 4b \n" \
581 : "=r" (__gu_err), "=&r" (__gu_tmp) \
582 : "0" (0), "r" (addr), "i" (-EFAULT)); \
583 (val) = (__typeof__(*(addr))) __gu_tmp; \
587 * Yuck. We need two variants, one for 64bit operation and one
588 * for 32 bit mode and old iron.
591 #define __PUT_USER_UNALIGNED_DW(ptr) __put_user_unaligned_asm_ll32(ptr)
594 #define __PUT_USER_UNALIGNED_DW(ptr) __put_user_unaligned_asm("usd", ptr)
597 #define __put_user_unaligned_nocheck(x,ptr,size) \
599 __typeof__(*(ptr)) __pu_val; \
604 case 1: __put_user_asm("sb", ptr); break; \
605 case 2: __put_user_unaligned_asm("ush", ptr); break; \
606 case 4: __put_user_unaligned_asm("usw", ptr); break; \
607 case 8: __PUT_USER_UNALIGNED_DW(ptr); break; \
608 default: __put_user_unaligned_unknown(); break; \
613 #define __put_user_unaligned_check(x,ptr,size) \
615 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
616 __typeof__(*(ptr)) __pu_val = (x); \
617 int __pu_err = -EFAULT; \
619 if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) { \
621 case 1: __put_user_asm("sb", __pu_addr); break; \
622 case 2: __put_user_unaligned_asm("ush", __pu_addr); break; \
623 case 4: __put_user_unaligned_asm("usw", __pu_addr); break; \
624 case 8: __PUT_USER_UNALGINED_DW(__pu_addr); break; \
625 default: __put_user_unaligned_unknown(); break; \
631 #define __put_user_unaligned_asm(insn, ptr) \
633 __asm__ __volatile__( \
634 "1: " insn " %z2, %3 # __put_user_unaligned_asm\n" \
637 " .section .fixup,\"ax\" \n" \
641 " .section __ex_table,\"a\" \n" \
642 " " __UA_ADDR " 1b, 3b \n" \
645 : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)), \
649 #define __put_user_unaligned_asm_ll32(ptr) \
651 __asm__ __volatile__( \
652 "1: sw %2, (%3) # __put_user_unaligned_asm_ll32 \n" \
653 "2: sw %D2, 4(%3) \n" \
656 " .section .fixup,\"ax\" \n" \
660 " .section __ex_table,\"a\" \n" \
661 " " __UA_ADDR " 1b, 4b \n" \
662 " " __UA_ADDR " 1b + 4, 4b \n" \
663 " " __UA_ADDR " 2b, 4b \n" \
664 " " __UA_ADDR " 2b + 4, 4b \n" \
667 : "0" (0), "r" (__pu_val), "r" (ptr), \
671 extern void __put_user_unaligned_unknown(void);
674 * We're generating jump to subroutines which will be outside the range of
678 #define __MODULE_JAL(destination) \
680 __UA_LA "\t$1, " #destination "\n\t" \
684 #define __MODULE_JAL(destination) \
685 "jal\t" #destination "\n\t"
688 #ifndef CONFIG_CPU_DADDI_WORKAROUNDS
689 #define DADDI_SCRATCH "$0"
691 #define DADDI_SCRATCH "$3"
694 extern size_t __copy_user(void *__to, const void *__from, size_t __n);
696 #define __invoke_copy_to_user(to, from, n) \
698 register void __user *__cu_to_r __asm__("$4"); \
699 register const void *__cu_from_r __asm__("$5"); \
700 register long __cu_len_r __asm__("$6"); \
703 __cu_from_r = (from); \
705 __asm__ __volatile__( \
706 __MODULE_JAL(__copy_user) \
707 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
709 : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
710 DADDI_SCRATCH, "memory"); \
715 * __copy_to_user: - Copy a block of data into user space, with less checking.
716 * @to: Destination address, in user space.
717 * @from: Source address, in kernel space.
718 * @n: Number of bytes to copy.
720 * Context: User context only. This function may sleep.
722 * Copy data from kernel space to user space. Caller must check
723 * the specified block with access_ok() before calling this function.
725 * Returns number of bytes that could not be copied.
726 * On success, this will be zero.
728 #define __copy_to_user(to, from, n) \
730 void __user *__cu_to; \
731 const void *__cu_from; \
735 __cu_from = (from); \
738 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, __cu_len); \
742 extern size_t __copy_user_inatomic(void *__to, const void *__from, size_t __n);
744 #define __copy_to_user_inatomic(to, from, n) \
746 void __user *__cu_to; \
747 const void *__cu_from; \
751 __cu_from = (from); \
753 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, __cu_len); \
757 #define __copy_from_user_inatomic(to, from, n) \
760 const void __user *__cu_from; \
764 __cu_from = (from); \
766 __cu_len = __invoke_copy_from_user_inatomic(__cu_to, __cu_from, \
772 * copy_to_user: - Copy a block of data into user space.
773 * @to: Destination address, in user space.
774 * @from: Source address, in kernel space.
775 * @n: Number of bytes to copy.
777 * Context: User context only. This function may sleep.
779 * Copy data from kernel space to user space.
781 * Returns number of bytes that could not be copied.
782 * On success, this will be zero.
784 #define copy_to_user(to, from, n) \
786 void __user *__cu_to; \
787 const void *__cu_from; \
791 __cu_from = (from); \
793 if (access_ok(VERIFY_WRITE, __cu_to, __cu_len)) { \
795 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, \
801 #define __invoke_copy_from_user(to, from, n) \
803 register void *__cu_to_r __asm__("$4"); \
804 register const void __user *__cu_from_r __asm__("$5"); \
805 register long __cu_len_r __asm__("$6"); \
808 __cu_from_r = (from); \
810 __asm__ __volatile__( \
811 ".set\tnoreorder\n\t" \
812 __MODULE_JAL(__copy_user) \
814 __UA_ADDU "\t$1, %1, %2\n\t" \
817 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
819 : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
820 DADDI_SCRATCH, "memory"); \
824 #define __invoke_copy_from_user_inatomic(to, from, n) \
826 register void *__cu_to_r __asm__("$4"); \
827 register const void __user *__cu_from_r __asm__("$5"); \
828 register long __cu_len_r __asm__("$6"); \
831 __cu_from_r = (from); \
833 __asm__ __volatile__( \
834 ".set\tnoreorder\n\t" \
835 __MODULE_JAL(__copy_user_inatomic) \
837 __UA_ADDU "\t$1, %1, %2\n\t" \
840 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
842 : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
843 DADDI_SCRATCH, "memory"); \
848 * __copy_from_user: - Copy a block of data from user space, with less checking.
849 * @to: Destination address, in kernel space.
850 * @from: Source address, in user space.
851 * @n: Number of bytes to copy.
853 * Context: User context only. This function may sleep.
855 * Copy data from user space to kernel space. Caller must check
856 * the specified block with access_ok() before calling this function.
858 * Returns number of bytes that could not be copied.
859 * On success, this will be zero.
861 * If some data could not be copied, this function will pad the copied
862 * data to the requested size using zero bytes.
864 #define __copy_from_user(to, from, n) \
867 const void __user *__cu_from; \
871 __cu_from = (from); \
874 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
880 * copy_from_user: - Copy a block of data from user space.
881 * @to: Destination address, in kernel space.
882 * @from: Source address, in user space.
883 * @n: Number of bytes to copy.
885 * Context: User context only. This function may sleep.
887 * Copy data from user space to kernel space.
889 * Returns number of bytes that could not be copied.
890 * On success, this will be zero.
892 * If some data could not be copied, this function will pad the copied
893 * data to the requested size using zero bytes.
895 #define copy_from_user(to, from, n) \
898 const void __user *__cu_from; \
902 __cu_from = (from); \
904 if (access_ok(VERIFY_READ, __cu_from, __cu_len)) { \
906 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
912 #define __copy_in_user(to, from, n) \
914 void __user *__cu_to; \
915 const void __user *__cu_from; \
919 __cu_from = (from); \
922 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
927 #define copy_in_user(to, from, n) \
929 void __user *__cu_to; \
930 const void __user *__cu_from; \
934 __cu_from = (from); \
936 if (likely(access_ok(VERIFY_READ, __cu_from, __cu_len) && \
937 access_ok(VERIFY_WRITE, __cu_to, __cu_len))) { \
939 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
946 * __clear_user: - Zero a block of memory in user space, with less checking.
947 * @to: Destination address, in user space.
948 * @n: Number of bytes to zero.
950 * Zero a block of memory in user space. Caller must check
951 * the specified block with access_ok() before calling this function.
953 * Returns number of bytes that could not be cleared.
954 * On success, this will be zero.
956 static inline __kernel_size_t
957 __clear_user(void __user *addr, __kernel_size_t size)
962 __asm__ __volatile__(
966 __MODULE_JAL(__bzero)
969 : "r" (addr), "r" (size)
970 : "$4", "$5", "$6", __UA_t0, __UA_t1, "$31");
975 #define clear_user(addr,n) \
977 void __user * __cl_addr = (addr); \
978 unsigned long __cl_size = (n); \
979 if (__cl_size && access_ok(VERIFY_WRITE, \
980 __cl_addr, __cl_size)) \
981 __cl_size = __clear_user(__cl_addr, __cl_size); \
986 * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
987 * @dst: Destination address, in kernel space. This buffer must be at
988 * least @count bytes long.
989 * @src: Source address, in user space.
990 * @count: Maximum number of bytes to copy, including the trailing NUL.
992 * Copies a NUL-terminated string from userspace to kernel space.
993 * Caller must check the specified block with access_ok() before calling
996 * On success, returns the length of the string (not including the trailing
999 * If access to userspace fails, returns -EFAULT (some data may have been
1002 * If @count is smaller than the length of the string, copies @count bytes
1003 * and returns @count.
1006 __strncpy_from_user(char *__to, const char __user *__from, long __len)
1011 __asm__ __volatile__(
1015 __MODULE_JAL(__strncpy_from_user_nocheck_asm)
1018 : "r" (__to), "r" (__from), "r" (__len)
1019 : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
1025 * strncpy_from_user: - Copy a NUL terminated string from userspace.
1026 * @dst: Destination address, in kernel space. This buffer must be at
1027 * least @count bytes long.
1028 * @src: Source address, in user space.
1029 * @count: Maximum number of bytes to copy, including the trailing NUL.
1031 * Copies a NUL-terminated string from userspace to kernel space.
1033 * On success, returns the length of the string (not including the trailing
1036 * If access to userspace fails, returns -EFAULT (some data may have been
1039 * If @count is smaller than the length of the string, copies @count bytes
1040 * and returns @count.
1043 strncpy_from_user(char *__to, const char __user *__from, long __len)
1048 __asm__ __volatile__(
1052 __MODULE_JAL(__strncpy_from_user_asm)
1055 : "r" (__to), "r" (__from), "r" (__len)
1056 : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
1061 /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
1062 static inline long __strlen_user(const char __user *s)
1067 __asm__ __volatile__(
1069 __MODULE_JAL(__strlen_user_nocheck_asm)
1073 : "$2", "$4", __UA_t0, "$31");
1079 * strlen_user: - Get the size of a string in user space.
1080 * @str: The string to measure.
1082 * Context: User context only. This function may sleep.
1084 * Get the size of a NUL-terminated string in user space.
1086 * Returns the size of the string INCLUDING the terminating NUL.
1087 * On exception, returns 0.
1089 * If there is a limit on the length of a valid string, you may wish to
1090 * consider using strnlen_user() instead.
1092 static inline long strlen_user(const char __user *s)
1097 __asm__ __volatile__(
1099 __MODULE_JAL(__strlen_user_asm)
1103 : "$2", "$4", __UA_t0, "$31");
1108 /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
1109 static inline long __strnlen_user(const char __user *s, long n)
1114 __asm__ __volatile__(
1117 __MODULE_JAL(__strnlen_user_nocheck_asm)
1121 : "$2", "$4", "$5", __UA_t0, "$31");
1127 * strlen_user: - Get the size of a string in user space.
1128 * @str: The string to measure.
1130 * Context: User context only. This function may sleep.
1132 * Get the size of a NUL-terminated string in user space.
1134 * Returns the size of the string INCLUDING the terminating NUL.
1135 * On exception, returns 0.
1137 * If there is a limit on the length of a valid string, you may wish to
1138 * consider using strnlen_user() instead.
1140 static inline long strnlen_user(const char __user *s, long n)
1145 __asm__ __volatile__(
1148 __MODULE_JAL(__strnlen_user_asm)
1152 : "$2", "$4", "$5", __UA_t0, "$31");
1157 struct exception_table_entry
1160 unsigned long nextinsn;
1163 extern int fixup_exception(struct pt_regs *regs);
1165 #endif /* _ASM_UACCESS_H */