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) \
109 unsigned long __addr = (unsigned long) (addr); \
110 unsigned long __size = size; \
111 unsigned long __mask = mask; \
112 unsigned long __ok; \
114 __chk_user_ptr(addr); \
115 __ok = (signed long)(__mask & (__addr | (__addr + __size) | \
116 __ua_size(__size))); \
120 #define access_ok(type, addr, size) \
121 likely(__access_ok((addr), (size), __access_mask))
124 * put_user: - Write a simple value into user space.
125 * @x: Value to copy to user space.
126 * @ptr: Destination address, in user space.
128 * Context: User context only. This function may sleep.
130 * This macro copies a single simple value from kernel space to user
131 * space. It supports simple types like char and int, but not larger
132 * data types like structures or arrays.
134 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
135 * to the result of dereferencing @ptr.
137 * Returns zero on success, or -EFAULT on error.
139 #define put_user(x,ptr) \
140 __put_user_check((x), (ptr), sizeof(*(ptr)))
143 * get_user: - Get a simple variable from user space.
144 * @x: Variable to store result.
145 * @ptr: Source address, in user space.
147 * Context: User context only. This function may sleep.
149 * This macro copies a single simple variable from user space to kernel
150 * space. It supports simple types like char and int, but not larger
151 * data types like structures or arrays.
153 * @ptr must have pointer-to-simple-variable type, and the result of
154 * dereferencing @ptr must be assignable to @x without a cast.
156 * Returns zero on success, or -EFAULT on error.
157 * On error, the variable @x is set to zero.
159 #define get_user(x,ptr) \
160 __get_user_check((x), (ptr), sizeof(*(ptr)))
163 * __put_user: - Write a simple value into user space, with less checking.
164 * @x: Value to copy to user space.
165 * @ptr: Destination address, in user space.
167 * Context: User context only. This function may sleep.
169 * This macro copies a single simple value from kernel space to user
170 * space. It supports simple types like char and int, but not larger
171 * data types like structures or arrays.
173 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
174 * to the result of dereferencing @ptr.
176 * Caller must check the pointer with access_ok() before calling this
179 * Returns zero on success, or -EFAULT on error.
181 #define __put_user(x,ptr) \
182 __put_user_nocheck((x), (ptr), sizeof(*(ptr)))
185 * __get_user: - Get a simple variable from user space, with less checking.
186 * @x: Variable to store result.
187 * @ptr: Source address, in user space.
189 * Context: User context only. This function may sleep.
191 * This macro copies a single simple variable from user space to kernel
192 * space. It supports simple types like char and int, but not larger
193 * data types like structures or arrays.
195 * @ptr must have pointer-to-simple-variable type, and the result of
196 * dereferencing @ptr must be assignable to @x without a cast.
198 * Caller must check the pointer with access_ok() before calling this
201 * Returns zero on success, or -EFAULT on error.
202 * On error, the variable @x is set to zero.
204 #define __get_user(x,ptr) \
205 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
207 struct __large_struct { unsigned long buf[100]; };
208 #define __m(x) (*(struct __large_struct __user *)(x))
211 * Yuck. We need two variants, one for 64bit operation and one
212 * for 32 bit mode and old iron.
215 #define __GET_USER_DW(val, ptr) __get_user_asm_ll32(val, ptr)
218 #define __GET_USER_DW(val, ptr) __get_user_asm(val, "ld", ptr)
221 extern void __get_user_unknown(void);
223 #define __get_user_common(val, size, ptr) \
226 case 1: __get_user_asm(val, "lb", ptr); break; \
227 case 2: __get_user_asm(val, "lh", ptr); break; \
228 case 4: __get_user_asm(val, "lw", ptr); break; \
229 case 8: __GET_USER_DW(val, ptr); break; \
230 default: __get_user_unknown(); break; \
234 #define __get_user_nocheck(x, ptr, size) \
238 __chk_user_ptr(ptr); \
239 __get_user_common((x), size, ptr); \
243 #define __get_user_check(x, ptr, size) \
245 int __gu_err = -EFAULT; \
246 const __typeof__(*(ptr)) __user * __gu_ptr = (ptr); \
248 if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) \
249 __get_user_common((x), size, __gu_ptr); \
254 #define __get_user_asm(val, insn, addr) \
258 __asm__ __volatile__( \
259 "1: " insn " %1, %3 \n" \
261 " .section .fixup,\"ax\" \n" \
265 " .section __ex_table,\"a\" \n" \
266 " "__UA_ADDR "\t1b, 3b \n" \
268 : "=r" (__gu_err), "=r" (__gu_tmp) \
269 : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \
271 (val) = (__typeof__(*(addr))) __gu_tmp; \
275 * Get a long long 64 using 32 bit registers.
277 #define __get_user_asm_ll32(val, addr) \
280 unsigned long long l; \
281 __typeof__(*(addr)) t; \
284 __asm__ __volatile__( \
285 "1: lw %1, (%3) \n" \
286 "2: lw %D1, 4(%3) \n" \
287 "3: .section .fixup,\"ax\" \n" \
293 " .section __ex_table,\"a\" \n" \
294 " " __UA_ADDR " 1b, 4b \n" \
295 " " __UA_ADDR " 2b, 4b \n" \
297 : "=r" (__gu_err), "=&r" (__gu_tmp.l) \
298 : "0" (0), "r" (addr), "i" (-EFAULT)); \
300 (val) = __gu_tmp.t; \
304 * Yuck. We need two variants, one for 64bit operation and one
305 * for 32 bit mode and old iron.
308 #define __PUT_USER_DW(ptr) __put_user_asm_ll32(ptr)
311 #define __PUT_USER_DW(ptr) __put_user_asm("sd", ptr)
314 #define __put_user_nocheck(x, ptr, size) \
316 __typeof__(*(ptr)) __pu_val; \
319 __chk_user_ptr(ptr); \
322 case 1: __put_user_asm("sb", ptr); break; \
323 case 2: __put_user_asm("sh", ptr); break; \
324 case 4: __put_user_asm("sw", ptr); break; \
325 case 8: __PUT_USER_DW(ptr); break; \
326 default: __put_user_unknown(); break; \
331 #define __put_user_check(x, ptr, size) \
333 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
334 __typeof__(*(ptr)) __pu_val = (x); \
335 int __pu_err = -EFAULT; \
337 if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) { \
339 case 1: __put_user_asm("sb", __pu_addr); break; \
340 case 2: __put_user_asm("sh", __pu_addr); break; \
341 case 4: __put_user_asm("sw", __pu_addr); break; \
342 case 8: __PUT_USER_DW(__pu_addr); break; \
343 default: __put_user_unknown(); break; \
349 #define __put_user_asm(insn, ptr) \
351 __asm__ __volatile__( \
352 "1: " insn " %z2, %3 # __put_user_asm\n" \
354 " .section .fixup,\"ax\" \n" \
358 " .section __ex_table,\"a\" \n" \
359 " " __UA_ADDR " 1b, 3b \n" \
362 : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)), \
366 #define __put_user_asm_ll32(ptr) \
368 __asm__ __volatile__( \
369 "1: sw %2, (%3) # __put_user_asm_ll32 \n" \
370 "2: sw %D2, 4(%3) \n" \
372 " .section .fixup,\"ax\" \n" \
376 " .section __ex_table,\"a\" \n" \
377 " " __UA_ADDR " 1b, 4b \n" \
378 " " __UA_ADDR " 2b, 4b \n" \
381 : "0" (0), "r" (__pu_val), "r" (ptr), \
385 extern void __put_user_unknown(void);
388 * put_user_unaligned: - Write a simple value into user space.
389 * @x: Value to copy to user space.
390 * @ptr: Destination address, in user space.
392 * Context: User context only. This function may sleep.
394 * This macro copies a single simple value from kernel space to user
395 * space. It supports simple types like char and int, but not larger
396 * data types like structures or arrays.
398 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
399 * to the result of dereferencing @ptr.
401 * Returns zero on success, or -EFAULT on error.
403 #define put_user_unaligned(x,ptr) \
404 __put_user_unaligned_check((x),(ptr),sizeof(*(ptr)))
407 * get_user_unaligned: - Get a simple variable from user space.
408 * @x: Variable to store result.
409 * @ptr: Source address, in user space.
411 * Context: User context only. This function may sleep.
413 * This macro copies a single simple variable from user space to kernel
414 * space. It supports simple types like char and int, but not larger
415 * data types like structures or arrays.
417 * @ptr must have pointer-to-simple-variable type, and the result of
418 * dereferencing @ptr must be assignable to @x without a cast.
420 * Returns zero on success, or -EFAULT on error.
421 * On error, the variable @x is set to zero.
423 #define get_user_unaligned(x,ptr) \
424 __get_user_unaligned_check((x),(ptr),sizeof(*(ptr)))
427 * __put_user_unaligned: - Write a simple value into user space, with less checking.
428 * @x: Value to copy to user space.
429 * @ptr: Destination address, in user space.
431 * Context: User context only. This function may sleep.
433 * This macro copies a single simple value from kernel space to user
434 * space. It supports simple types like char and int, but not larger
435 * data types like structures or arrays.
437 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
438 * to the result of dereferencing @ptr.
440 * Caller must check the pointer with access_ok() before calling this
443 * Returns zero on success, or -EFAULT on error.
445 #define __put_user_unaligned(x,ptr) \
446 __put_user_unaligned_nocheck((x),(ptr),sizeof(*(ptr)))
449 * __get_user_unaligned: - Get a simple variable from user space, with less checking.
450 * @x: Variable to store result.
451 * @ptr: Source address, in user space.
453 * Context: User context only. This function may sleep.
455 * This macro copies a single simple variable from user space to kernel
456 * space. It supports simple types like char and int, but not larger
457 * data types like structures or arrays.
459 * @ptr must have pointer-to-simple-variable type, and the result of
460 * dereferencing @ptr must be assignable to @x without a cast.
462 * Caller must check the pointer with access_ok() before calling this
465 * Returns zero on success, or -EFAULT on error.
466 * On error, the variable @x is set to zero.
468 #define __get_user_unaligned(x,ptr) \
469 __get_user__unalignednocheck((x),(ptr),sizeof(*(ptr)))
472 * Yuck. We need two variants, one for 64bit operation and one
473 * for 32 bit mode and old iron.
476 #define __GET_USER_UNALIGNED_DW(val, ptr) \
477 __get_user_unaligned_asm_ll32(val, ptr)
480 #define __GET_USER_UNALIGNED_DW(val, ptr) \
481 __get_user_unaligned_asm(val, "uld", ptr)
484 extern void __get_user_unaligned_unknown(void);
486 #define __get_user_unaligned_common(val, size, ptr) \
489 case 1: __get_user_asm(val, "lb", ptr); break; \
490 case 2: __get_user_unaligned_asm(val, "ulh", ptr); break; \
491 case 4: __get_user_unaligned_asm(val, "ulw", ptr); break; \
492 case 8: __GET_USER_UNALIGNED_DW(val, ptr); break; \
493 default: __get_user_unaligned_unknown(); break; \
497 #define __get_user_unaligned_nocheck(x,ptr,size) \
501 __get_user_unaligned_common((x), size, ptr); \
505 #define __get_user_unaligned_check(x,ptr,size) \
507 int __gu_err = -EFAULT; \
508 const __typeof__(*(ptr)) __user * __gu_ptr = (ptr); \
510 if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) \
511 __get_user_unaligned_common((x), size, __gu_ptr); \
516 #define __get_user_unaligned_asm(val, insn, addr) \
520 __asm__ __volatile__( \
521 "1: " insn " %1, %3 \n" \
523 " .section .fixup,\"ax\" \n" \
527 " .section __ex_table,\"a\" \n" \
528 " "__UA_ADDR "\t1b, 3b \n" \
529 " "__UA_ADDR "\t1b + 4, 3b \n" \
531 : "=r" (__gu_err), "=r" (__gu_tmp) \
532 : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \
534 (val) = (__typeof__(*(addr))) __gu_tmp; \
538 * Get a long long 64 using 32 bit registers.
540 #define __get_user_unaligned_asm_ll32(val, addr) \
542 unsigned long long __gu_tmp; \
544 __asm__ __volatile__( \
545 "1: ulw %1, (%3) \n" \
546 "2: ulw %D1, 4(%3) \n" \
548 "3: .section .fixup,\"ax\" \n" \
554 " .section __ex_table,\"a\" \n" \
555 " " __UA_ADDR " 1b, 4b \n" \
556 " " __UA_ADDR " 1b + 4, 4b \n" \
557 " " __UA_ADDR " 2b, 4b \n" \
558 " " __UA_ADDR " 2b + 4, 4b \n" \
560 : "=r" (__gu_err), "=&r" (__gu_tmp) \
561 : "0" (0), "r" (addr), "i" (-EFAULT)); \
562 (val) = (__typeof__(*(addr))) __gu_tmp; \
566 * Yuck. We need two variants, one for 64bit operation and one
567 * for 32 bit mode and old iron.
570 #define __PUT_USER_UNALIGNED_DW(ptr) __put_user_unaligned_asm_ll32(ptr)
573 #define __PUT_USER_UNALIGNED_DW(ptr) __put_user_unaligned_asm("usd", ptr)
576 #define __put_user_unaligned_nocheck(x,ptr,size) \
578 __typeof__(*(ptr)) __pu_val; \
583 case 1: __put_user_asm("sb", ptr); break; \
584 case 2: __put_user_unaligned_asm("ush", ptr); break; \
585 case 4: __put_user_unaligned_asm("usw", ptr); break; \
586 case 8: __PUT_USER_UNALIGNED_DW(ptr); break; \
587 default: __put_user_unaligned_unknown(); break; \
592 #define __put_user_unaligned_check(x,ptr,size) \
594 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
595 __typeof__(*(ptr)) __pu_val = (x); \
596 int __pu_err = -EFAULT; \
598 if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) { \
600 case 1: __put_user_asm("sb", __pu_addr); break; \
601 case 2: __put_user_unaligned_asm("ush", __pu_addr); break; \
602 case 4: __put_user_unaligned_asm("usw", __pu_addr); break; \
603 case 8: __PUT_USER_UNALGINED_DW(__pu_addr); break; \
604 default: __put_user_unaligned_unknown(); break; \
610 #define __put_user_unaligned_asm(insn, ptr) \
612 __asm__ __volatile__( \
613 "1: " insn " %z2, %3 # __put_user_unaligned_asm\n" \
615 " .section .fixup,\"ax\" \n" \
619 " .section __ex_table,\"a\" \n" \
620 " " __UA_ADDR " 1b, 3b \n" \
623 : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)), \
627 #define __put_user_unaligned_asm_ll32(ptr) \
629 __asm__ __volatile__( \
630 "1: sw %2, (%3) # __put_user_unaligned_asm_ll32 \n" \
631 "2: sw %D2, 4(%3) \n" \
633 " .section .fixup,\"ax\" \n" \
637 " .section __ex_table,\"a\" \n" \
638 " " __UA_ADDR " 1b, 4b \n" \
639 " " __UA_ADDR " 1b + 4, 4b \n" \
640 " " __UA_ADDR " 2b, 4b \n" \
641 " " __UA_ADDR " 2b + 4, 4b \n" \
644 : "0" (0), "r" (__pu_val), "r" (ptr), \
648 extern void __put_user_unaligned_unknown(void);
651 * We're generating jump to subroutines which will be outside the range of
655 #define __MODULE_JAL(destination) \
657 __UA_LA "\t$1, " #destination "\n\t" \
661 #define __MODULE_JAL(destination) \
662 "jal\t" #destination "\n\t"
665 #ifndef CONFIG_CPU_DADDI_WORKAROUNDS
666 #define DADDI_SCRATCH "$0"
668 #define DADDI_SCRATCH "$3"
671 extern size_t __copy_user(void *__to, const void *__from, size_t __n);
673 #define __invoke_copy_to_user(to, from, n) \
675 register void __user *__cu_to_r __asm__("$4"); \
676 register const void *__cu_from_r __asm__("$5"); \
677 register long __cu_len_r __asm__("$6"); \
680 __cu_from_r = (from); \
682 __asm__ __volatile__( \
683 __MODULE_JAL(__copy_user) \
684 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
686 : "$8", "$9", "$10", "$11", "$12", "$15", "$24", "$31", \
687 DADDI_SCRATCH, "memory"); \
692 * __copy_to_user: - Copy a block of data into user space, with less checking.
693 * @to: Destination address, in user space.
694 * @from: Source address, in kernel space.
695 * @n: Number of bytes to copy.
697 * Context: User context only. This function may sleep.
699 * Copy data from kernel space to user space. Caller must check
700 * the specified block with access_ok() before calling this function.
702 * Returns number of bytes that could not be copied.
703 * On success, this will be zero.
705 #define __copy_to_user(to, from, n) \
707 void __user *__cu_to; \
708 const void *__cu_from; \
713 __cu_from = (from); \
715 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, __cu_len); \
719 extern size_t __copy_user_inatomic(void *__to, const void *__from, size_t __n);
721 #define __copy_to_user_inatomic(to, from, n) \
723 void __user *__cu_to; \
724 const void *__cu_from; \
728 __cu_from = (from); \
730 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, __cu_len); \
734 #define __copy_from_user_inatomic(to, from, n) \
737 const void __user *__cu_from; \
741 __cu_from = (from); \
743 __cu_len = __invoke_copy_from_user_inatomic(__cu_to, __cu_from, \
749 * copy_to_user: - Copy a block of data into user space.
750 * @to: Destination address, in user space.
751 * @from: Source address, in kernel space.
752 * @n: Number of bytes to copy.
754 * Context: User context only. This function may sleep.
756 * Copy data from kernel space to user space.
758 * Returns number of bytes that could not be copied.
759 * On success, this will be zero.
761 #define copy_to_user(to, from, n) \
763 void __user *__cu_to; \
764 const void *__cu_from; \
769 __cu_from = (from); \
771 if (access_ok(VERIFY_WRITE, __cu_to, __cu_len)) \
772 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, \
777 #define __invoke_copy_from_user(to, from, n) \
779 register void *__cu_to_r __asm__("$4"); \
780 register const void __user *__cu_from_r __asm__("$5"); \
781 register long __cu_len_r __asm__("$6"); \
784 __cu_from_r = (from); \
786 __asm__ __volatile__( \
787 ".set\tnoreorder\n\t" \
788 __MODULE_JAL(__copy_user) \
790 __UA_ADDU "\t$1, %1, %2\n\t" \
793 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
795 : "$8", "$9", "$10", "$11", "$12", "$15", "$24", "$31", \
796 DADDI_SCRATCH, "memory"); \
800 #define __invoke_copy_from_user_inatomic(to, from, n) \
802 register void *__cu_to_r __asm__("$4"); \
803 register const void __user *__cu_from_r __asm__("$5"); \
804 register long __cu_len_r __asm__("$6"); \
807 __cu_from_r = (from); \
809 __asm__ __volatile__( \
810 ".set\tnoreorder\n\t" \
811 __MODULE_JAL(__copy_user_inatomic) \
813 __UA_ADDU "\t$1, %1, %2\n\t" \
816 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
818 : "$8", "$9", "$10", "$11", "$12", "$15", "$24", "$31", \
819 DADDI_SCRATCH, "memory"); \
824 * __copy_from_user: - Copy a block of data from user space, with less checking.
825 * @to: Destination address, in kernel space.
826 * @from: Source address, in user space.
827 * @n: Number of bytes to copy.
829 * Context: User context only. This function may sleep.
831 * Copy data from user space to kernel space. Caller must check
832 * the specified block with access_ok() before calling this function.
834 * Returns number of bytes that could not be copied.
835 * On success, this will be zero.
837 * If some data could not be copied, this function will pad the copied
838 * data to the requested size using zero bytes.
840 #define __copy_from_user(to, from, n) \
843 const void __user *__cu_from; \
848 __cu_from = (from); \
850 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
856 * copy_from_user: - Copy a block of data from user space.
857 * @to: Destination address, in kernel space.
858 * @from: Source address, in user space.
859 * @n: Number of bytes to copy.
861 * Context: User context only. This function may sleep.
863 * Copy data from user space to kernel space.
865 * Returns number of bytes that could not be copied.
866 * On success, this will be zero.
868 * If some data could not be copied, this function will pad the copied
869 * data to the requested size using zero bytes.
871 #define copy_from_user(to, from, n) \
874 const void __user *__cu_from; \
879 __cu_from = (from); \
881 if (access_ok(VERIFY_READ, __cu_from, __cu_len)) \
882 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
887 #define __copy_in_user(to, from, n) \
889 void __user *__cu_to; \
890 const void __user *__cu_from; \
895 __cu_from = (from); \
897 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
902 #define copy_in_user(to, from, n) \
904 void __user *__cu_to; \
905 const void __user *__cu_from; \
910 __cu_from = (from); \
912 if (likely(access_ok(VERIFY_READ, __cu_from, __cu_len) && \
913 access_ok(VERIFY_WRITE, __cu_to, __cu_len))) \
914 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
920 * __clear_user: - Zero a block of memory in user space, with less checking.
921 * @to: Destination address, in user space.
922 * @n: Number of bytes to zero.
924 * Zero a block of memory in user space. Caller must check
925 * the specified block with access_ok() before calling this function.
927 * Returns number of bytes that could not be cleared.
928 * On success, this will be zero.
930 static inline __kernel_size_t
931 __clear_user(void __user *addr, __kernel_size_t size)
936 __asm__ __volatile__(
940 __MODULE_JAL(__bzero)
943 : "r" (addr), "r" (size)
944 : "$4", "$5", "$6", __UA_t0, __UA_t1, "$31");
949 #define clear_user(addr,n) \
951 void __user * __cl_addr = (addr); \
952 unsigned long __cl_size = (n); \
953 if (__cl_size && access_ok(VERIFY_WRITE, \
954 ((unsigned long)(__cl_addr)), __cl_size)) \
955 __cl_size = __clear_user(__cl_addr, __cl_size); \
960 * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
961 * @dst: Destination address, in kernel space. This buffer must be at
962 * least @count bytes long.
963 * @src: Source address, in user space.
964 * @count: Maximum number of bytes to copy, including the trailing NUL.
966 * Copies a NUL-terminated string from userspace to kernel space.
967 * Caller must check the specified block with access_ok() before calling
970 * On success, returns the length of the string (not including the trailing
973 * If access to userspace fails, returns -EFAULT (some data may have been
976 * If @count is smaller than the length of the string, copies @count bytes
977 * and returns @count.
980 __strncpy_from_user(char *__to, const char __user *__from, long __len)
985 __asm__ __volatile__(
989 __MODULE_JAL(__strncpy_from_user_nocheck_asm)
992 : "r" (__to), "r" (__from), "r" (__len)
993 : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
999 * strncpy_from_user: - Copy a NUL terminated string from userspace.
1000 * @dst: Destination address, in kernel space. This buffer must be at
1001 * least @count bytes long.
1002 * @src: Source address, in user space.
1003 * @count: Maximum number of bytes to copy, including the trailing NUL.
1005 * Copies a NUL-terminated string from userspace to kernel space.
1007 * On success, returns the length of the string (not including the trailing
1010 * If access to userspace fails, returns -EFAULT (some data may have been
1013 * If @count is smaller than the length of the string, copies @count bytes
1014 * and returns @count.
1017 strncpy_from_user(char *__to, const char __user *__from, long __len)
1022 __asm__ __volatile__(
1026 __MODULE_JAL(__strncpy_from_user_asm)
1029 : "r" (__to), "r" (__from), "r" (__len)
1030 : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
1035 /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
1036 static inline long __strlen_user(const char __user *s)
1041 __asm__ __volatile__(
1043 __MODULE_JAL(__strlen_user_nocheck_asm)
1047 : "$2", "$4", __UA_t0, "$31");
1053 * strlen_user: - Get the size of a string in user space.
1054 * @str: The string to measure.
1056 * Context: User context only. This function may sleep.
1058 * Get the size of a NUL-terminated string in user space.
1060 * Returns the size of the string INCLUDING the terminating NUL.
1061 * On exception, returns 0.
1063 * If there is a limit on the length of a valid string, you may wish to
1064 * consider using strnlen_user() instead.
1066 static inline long strlen_user(const char __user *s)
1071 __asm__ __volatile__(
1073 __MODULE_JAL(__strlen_user_asm)
1077 : "$2", "$4", __UA_t0, "$31");
1082 /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
1083 static inline long __strnlen_user(const char __user *s, long n)
1088 __asm__ __volatile__(
1091 __MODULE_JAL(__strnlen_user_nocheck_asm)
1095 : "$2", "$4", "$5", __UA_t0, "$31");
1101 * strlen_user: - Get the size of a string in user space.
1102 * @str: The string to measure.
1104 * Context: User context only. This function may sleep.
1106 * Get the size of a NUL-terminated string in user space.
1108 * Returns the size of the string INCLUDING the terminating NUL.
1109 * On exception, returns 0.
1111 * If there is a limit on the length of a valid string, you may wish to
1112 * consider using strnlen_user() instead.
1114 static inline long strnlen_user(const char __user *s, long n)
1119 __asm__ __volatile__(
1122 __MODULE_JAL(__strnlen_user_asm)
1126 : "$2", "$4", "$5", __UA_t0, "$31");
1131 struct exception_table_entry
1134 unsigned long nextinsn;
1137 extern int fixup_exception(struct pt_regs *regs);
1139 #endif /* _ASM_UACCESS_H */