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); \
249 if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) \
250 __get_user_common((x), size, __gu_ptr); \
255 #define __get_user_asm(val, insn, addr) \
259 __asm__ __volatile__( \
260 "1: " insn " %1, %3 \n" \
262 " .section .fixup,\"ax\" \n" \
266 " .section __ex_table,\"a\" \n" \
267 " "__UA_ADDR "\t1b, 3b \n" \
269 : "=r" (__gu_err), "=r" (__gu_tmp) \
270 : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \
272 (val) = (__typeof__(*(addr))) __gu_tmp; \
276 * Get a long long 64 using 32 bit registers.
278 #define __get_user_asm_ll32(val, addr) \
281 unsigned long long l; \
282 __typeof__(*(addr)) t; \
285 __asm__ __volatile__( \
286 "1: lw %1, (%3) \n" \
287 "2: lw %D1, 4(%3) \n" \
288 "3: .section .fixup,\"ax\" \n" \
294 " .section __ex_table,\"a\" \n" \
295 " " __UA_ADDR " 1b, 4b \n" \
296 " " __UA_ADDR " 2b, 4b \n" \
298 : "=r" (__gu_err), "=&r" (__gu_tmp.l) \
299 : "0" (0), "r" (addr), "i" (-EFAULT)); \
301 (val) = __gu_tmp.t; \
305 * Yuck. We need two variants, one for 64bit operation and one
306 * for 32 bit mode and old iron.
309 #define __PUT_USER_DW(ptr) __put_user_asm_ll32(ptr)
312 #define __PUT_USER_DW(ptr) __put_user_asm("sd", ptr)
315 #define __put_user_nocheck(x, ptr, size) \
317 __typeof__(*(ptr)) __pu_val; \
320 __chk_user_ptr(ptr); \
323 case 1: __put_user_asm("sb", ptr); break; \
324 case 2: __put_user_asm("sh", ptr); break; \
325 case 4: __put_user_asm("sw", ptr); break; \
326 case 8: __PUT_USER_DW(ptr); break; \
327 default: __put_user_unknown(); break; \
332 #define __put_user_check(x, ptr, size) \
334 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
335 __typeof__(*(ptr)) __pu_val = (x); \
336 int __pu_err = -EFAULT; \
339 if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) { \
341 case 1: __put_user_asm("sb", __pu_addr); break; \
342 case 2: __put_user_asm("sh", __pu_addr); break; \
343 case 4: __put_user_asm("sw", __pu_addr); break; \
344 case 8: __PUT_USER_DW(__pu_addr); break; \
345 default: __put_user_unknown(); break; \
351 #define __put_user_asm(insn, ptr) \
353 __asm__ __volatile__( \
354 "1: " insn " %z2, %3 # __put_user_asm\n" \
356 " .section .fixup,\"ax\" \n" \
360 " .section __ex_table,\"a\" \n" \
361 " " __UA_ADDR " 1b, 3b \n" \
364 : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)), \
368 #define __put_user_asm_ll32(ptr) \
370 __asm__ __volatile__( \
371 "1: sw %2, (%3) # __put_user_asm_ll32 \n" \
372 "2: sw %D2, 4(%3) \n" \
374 " .section .fixup,\"ax\" \n" \
378 " .section __ex_table,\"a\" \n" \
379 " " __UA_ADDR " 1b, 4b \n" \
380 " " __UA_ADDR " 2b, 4b \n" \
383 : "0" (0), "r" (__pu_val), "r" (ptr), \
387 extern void __put_user_unknown(void);
390 * put_user_unaligned: - Write a simple value into user space.
391 * @x: Value to copy to user space.
392 * @ptr: Destination address, in user space.
394 * Context: User context only. This function may sleep.
396 * This macro copies a single simple value from kernel space to user
397 * space. It supports simple types like char and int, but not larger
398 * data types like structures or arrays.
400 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
401 * to the result of dereferencing @ptr.
403 * Returns zero on success, or -EFAULT on error.
405 #define put_user_unaligned(x,ptr) \
406 __put_user_unaligned_check((x),(ptr),sizeof(*(ptr)))
409 * get_user_unaligned: - Get a simple variable from user space.
410 * @x: Variable to store result.
411 * @ptr: Source address, in user space.
413 * Context: User context only. This function may sleep.
415 * This macro copies a single simple variable from user space to kernel
416 * space. It supports simple types like char and int, but not larger
417 * data types like structures or arrays.
419 * @ptr must have pointer-to-simple-variable type, and the result of
420 * dereferencing @ptr must be assignable to @x without a cast.
422 * Returns zero on success, or -EFAULT on error.
423 * On error, the variable @x is set to zero.
425 #define get_user_unaligned(x,ptr) \
426 __get_user_unaligned_check((x),(ptr),sizeof(*(ptr)))
429 * __put_user_unaligned: - Write a simple value into user space, with less checking.
430 * @x: Value to copy to user space.
431 * @ptr: Destination address, in user space.
433 * Context: User context only. This function may sleep.
435 * This macro copies a single simple value from kernel space to user
436 * space. It supports simple types like char and int, but not larger
437 * data types like structures or arrays.
439 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
440 * to the result of dereferencing @ptr.
442 * Caller must check the pointer with access_ok() before calling this
445 * Returns zero on success, or -EFAULT on error.
447 #define __put_user_unaligned(x,ptr) \
448 __put_user_unaligned_nocheck((x),(ptr),sizeof(*(ptr)))
451 * __get_user_unaligned: - Get a simple variable from user space, with less checking.
452 * @x: Variable to store result.
453 * @ptr: Source address, in user space.
455 * Context: User context only. This function may sleep.
457 * This macro copies a single simple variable from user space to kernel
458 * space. It supports simple types like char and int, but not larger
459 * data types like structures or arrays.
461 * @ptr must have pointer-to-simple-variable type, and the result of
462 * dereferencing @ptr must be assignable to @x without a cast.
464 * Caller must check the pointer with access_ok() before calling this
467 * Returns zero on success, or -EFAULT on error.
468 * On error, the variable @x is set to zero.
470 #define __get_user_unaligned(x,ptr) \
471 __get_user__unalignednocheck((x),(ptr),sizeof(*(ptr)))
474 * Yuck. We need two variants, one for 64bit operation and one
475 * for 32 bit mode and old iron.
478 #define __GET_USER_UNALIGNED_DW(val, ptr) \
479 __get_user_unaligned_asm_ll32(val, ptr)
482 #define __GET_USER_UNALIGNED_DW(val, ptr) \
483 __get_user_unaligned_asm(val, "uld", ptr)
486 extern void __get_user_unaligned_unknown(void);
488 #define __get_user_unaligned_common(val, size, ptr) \
491 case 1: __get_user_asm(val, "lb", ptr); break; \
492 case 2: __get_user_unaligned_asm(val, "ulh", ptr); break; \
493 case 4: __get_user_unaligned_asm(val, "ulw", ptr); break; \
494 case 8: __GET_USER_UNALIGNED_DW(val, ptr); break; \
495 default: __get_user_unaligned_unknown(); break; \
499 #define __get_user_unaligned_nocheck(x,ptr,size) \
503 __get_user_unaligned_common((x), size, ptr); \
507 #define __get_user_unaligned_check(x,ptr,size) \
509 int __gu_err = -EFAULT; \
510 const __typeof__(*(ptr)) __user * __gu_ptr = (ptr); \
512 if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) \
513 __get_user_unaligned_common((x), size, __gu_ptr); \
518 #define __get_user_unaligned_asm(val, insn, addr) \
522 __asm__ __volatile__( \
523 "1: " insn " %1, %3 \n" \
525 " .section .fixup,\"ax\" \n" \
529 " .section __ex_table,\"a\" \n" \
530 " "__UA_ADDR "\t1b, 3b \n" \
531 " "__UA_ADDR "\t1b + 4, 3b \n" \
533 : "=r" (__gu_err), "=r" (__gu_tmp) \
534 : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \
536 (val) = (__typeof__(*(addr))) __gu_tmp; \
540 * Get a long long 64 using 32 bit registers.
542 #define __get_user_unaligned_asm_ll32(val, addr) \
544 unsigned long long __gu_tmp; \
546 __asm__ __volatile__( \
547 "1: ulw %1, (%3) \n" \
548 "2: ulw %D1, 4(%3) \n" \
550 "3: .section .fixup,\"ax\" \n" \
556 " .section __ex_table,\"a\" \n" \
557 " " __UA_ADDR " 1b, 4b \n" \
558 " " __UA_ADDR " 1b + 4, 4b \n" \
559 " " __UA_ADDR " 2b, 4b \n" \
560 " " __UA_ADDR " 2b + 4, 4b \n" \
562 : "=r" (__gu_err), "=&r" (__gu_tmp) \
563 : "0" (0), "r" (addr), "i" (-EFAULT)); \
564 (val) = (__typeof__(*(addr))) __gu_tmp; \
568 * Yuck. We need two variants, one for 64bit operation and one
569 * for 32 bit mode and old iron.
572 #define __PUT_USER_UNALIGNED_DW(ptr) __put_user_unaligned_asm_ll32(ptr)
575 #define __PUT_USER_UNALIGNED_DW(ptr) __put_user_unaligned_asm("usd", ptr)
578 #define __put_user_unaligned_nocheck(x,ptr,size) \
580 __typeof__(*(ptr)) __pu_val; \
585 case 1: __put_user_asm("sb", ptr); break; \
586 case 2: __put_user_unaligned_asm("ush", ptr); break; \
587 case 4: __put_user_unaligned_asm("usw", ptr); break; \
588 case 8: __PUT_USER_UNALIGNED_DW(ptr); break; \
589 default: __put_user_unaligned_unknown(); break; \
594 #define __put_user_unaligned_check(x,ptr,size) \
596 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
597 __typeof__(*(ptr)) __pu_val = (x); \
598 int __pu_err = -EFAULT; \
600 if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) { \
602 case 1: __put_user_asm("sb", __pu_addr); break; \
603 case 2: __put_user_unaligned_asm("ush", __pu_addr); break; \
604 case 4: __put_user_unaligned_asm("usw", __pu_addr); break; \
605 case 8: __PUT_USER_UNALGINED_DW(__pu_addr); break; \
606 default: __put_user_unaligned_unknown(); break; \
612 #define __put_user_unaligned_asm(insn, ptr) \
614 __asm__ __volatile__( \
615 "1: " insn " %z2, %3 # __put_user_unaligned_asm\n" \
617 " .section .fixup,\"ax\" \n" \
621 " .section __ex_table,\"a\" \n" \
622 " " __UA_ADDR " 1b, 3b \n" \
625 : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)), \
629 #define __put_user_unaligned_asm_ll32(ptr) \
631 __asm__ __volatile__( \
632 "1: sw %2, (%3) # __put_user_unaligned_asm_ll32 \n" \
633 "2: sw %D2, 4(%3) \n" \
635 " .section .fixup,\"ax\" \n" \
639 " .section __ex_table,\"a\" \n" \
640 " " __UA_ADDR " 1b, 4b \n" \
641 " " __UA_ADDR " 1b + 4, 4b \n" \
642 " " __UA_ADDR " 2b, 4b \n" \
643 " " __UA_ADDR " 2b + 4, 4b \n" \
646 : "0" (0), "r" (__pu_val), "r" (ptr), \
650 extern void __put_user_unaligned_unknown(void);
653 * We're generating jump to subroutines which will be outside the range of
657 #define __MODULE_JAL(destination) \
659 __UA_LA "\t$1, " #destination "\n\t" \
663 #define __MODULE_JAL(destination) \
664 "jal\t" #destination "\n\t"
667 #ifndef CONFIG_CPU_DADDI_WORKAROUNDS
668 #define DADDI_SCRATCH "$0"
670 #define DADDI_SCRATCH "$3"
673 extern size_t __copy_user(void *__to
, const void *__from
, size_t __n
);
675 #define __invoke_copy_to_user(to, from, n) \
677 register void __user *__cu_to_r __asm__("$4"); \
678 register const void *__cu_from_r __asm__("$5"); \
679 register long __cu_len_r __asm__("$6"); \
682 __cu_from_r = (from); \
684 __asm__ __volatile__( \
685 __MODULE_JAL(__copy_user) \
686 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
688 : "$8", "$9", "$10", "$11", "$12", "$15", "$24", "$31", \
689 DADDI_SCRATCH, "memory"); \
694 * __copy_to_user: - Copy a block of data into user space, with less checking.
695 * @to: Destination address, in user space.
696 * @from: Source address, in kernel space.
697 * @n: Number of bytes to copy.
699 * Context: User context only. This function may sleep.
701 * Copy data from kernel space to user space. Caller must check
702 * the specified block with access_ok() before calling this function.
704 * Returns number of bytes that could not be copied.
705 * On success, this will be zero.
707 #define __copy_to_user(to, from, n) \
709 void __user *__cu_to; \
710 const void *__cu_from; \
714 __cu_from = (from); \
717 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, __cu_len); \
721 extern size_t __copy_user_inatomic(void *__to
, const void *__from
, size_t __n
);
723 #define __copy_to_user_inatomic(to, from, n) \
725 void __user *__cu_to; \
726 const void *__cu_from; \
730 __cu_from = (from); \
732 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, __cu_len); \
736 #define __copy_from_user_inatomic(to, from, n) \
739 const void __user *__cu_from; \
743 __cu_from = (from); \
745 __cu_len = __invoke_copy_from_user_inatomic(__cu_to, __cu_from, \
751 * copy_to_user: - Copy a block of data into user space.
752 * @to: Destination address, in user space.
753 * @from: Source address, in kernel space.
754 * @n: Number of bytes to copy.
756 * Context: User context only. This function may sleep.
758 * Copy data from kernel space to user space.
760 * Returns number of bytes that could not be copied.
761 * On success, this will be zero.
763 #define copy_to_user(to, from, n) \
765 void __user *__cu_to; \
766 const void *__cu_from; \
770 __cu_from = (from); \
772 if (access_ok(VERIFY_WRITE, __cu_to, __cu_len)) { \
774 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, \
780 #define __invoke_copy_from_user(to, from, n) \
782 register void *__cu_to_r __asm__("$4"); \
783 register const void __user *__cu_from_r __asm__("$5"); \
784 register long __cu_len_r __asm__("$6"); \
787 __cu_from_r = (from); \
789 __asm__ __volatile__( \
790 ".set\tnoreorder\n\t" \
791 __MODULE_JAL(__copy_user) \
793 __UA_ADDU "\t$1, %1, %2\n\t" \
796 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
798 : "$8", "$9", "$10", "$11", "$12", "$15", "$24", "$31", \
799 DADDI_SCRATCH, "memory"); \
803 #define __invoke_copy_from_user_inatomic(to, from, n) \
805 register void *__cu_to_r __asm__("$4"); \
806 register const void __user *__cu_from_r __asm__("$5"); \
807 register long __cu_len_r __asm__("$6"); \
810 __cu_from_r = (from); \
812 __asm__ __volatile__( \
813 ".set\tnoreorder\n\t" \
814 __MODULE_JAL(__copy_user_inatomic) \
816 __UA_ADDU "\t$1, %1, %2\n\t" \
819 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
821 : "$8", "$9", "$10", "$11", "$12", "$15", "$24", "$31", \
822 DADDI_SCRATCH, "memory"); \
827 * __copy_from_user: - Copy a block of data from user space, with less checking.
828 * @to: Destination address, in kernel space.
829 * @from: Source address, in user space.
830 * @n: Number of bytes to copy.
832 * Context: User context only. This function may sleep.
834 * Copy data from user space to kernel space. Caller must check
835 * the specified block with access_ok() before calling this function.
837 * Returns number of bytes that could not be copied.
838 * On success, this will be zero.
840 * If some data could not be copied, this function will pad the copied
841 * data to the requested size using zero bytes.
843 #define __copy_from_user(to, from, n) \
846 const void __user *__cu_from; \
850 __cu_from = (from); \
853 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
859 * copy_from_user: - Copy a block of data from user space.
860 * @to: Destination address, in kernel space.
861 * @from: Source address, in user space.
862 * @n: Number of bytes to copy.
864 * Context: User context only. This function may sleep.
866 * Copy data from user space to kernel space.
868 * Returns number of bytes that could not be copied.
869 * On success, this will be zero.
871 * If some data could not be copied, this function will pad the copied
872 * data to the requested size using zero bytes.
874 #define copy_from_user(to, from, n) \
877 const void __user *__cu_from; \
881 __cu_from = (from); \
883 if (access_ok(VERIFY_READ, __cu_from, __cu_len)) { \
885 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
891 #define __copy_in_user(to, from, n) \
893 void __user *__cu_to; \
894 const void __user *__cu_from; \
898 __cu_from = (from); \
901 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
906 #define copy_in_user(to, from, n) \
908 void __user *__cu_to; \
909 const void __user *__cu_from; \
913 __cu_from = (from); \
915 if (likely(access_ok(VERIFY_READ, __cu_from, __cu_len) && \
916 access_ok(VERIFY_WRITE, __cu_to, __cu_len))) { \
918 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
925 * __clear_user: - Zero a block of memory in user space, with less checking.
926 * @to: Destination address, in user space.
927 * @n: Number of bytes to zero.
929 * Zero a block of memory in user space. Caller must check
930 * the specified block with access_ok() before calling this function.
932 * Returns number of bytes that could not be cleared.
933 * On success, this will be zero.
935 static inline __kernel_size_t
936 __clear_user(void __user
*addr
, __kernel_size_t size
)
941 __asm__
__volatile__(
945 __MODULE_JAL(__bzero
)
948 : "r" (addr
), "r" (size
)
949 : "$4", "$5", "$6", __UA_t0
, __UA_t1
, "$31");
954 #define clear_user(addr,n) \
956 void __user * __cl_addr = (addr); \
957 unsigned long __cl_size = (n); \
958 if (__cl_size && access_ok(VERIFY_WRITE, \
959 __cl_addr, __cl_size)) \
960 __cl_size = __clear_user(__cl_addr, __cl_size); \
965 * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
966 * @dst: Destination address, in kernel space. This buffer must be at
967 * least @count bytes long.
968 * @src: Source address, in user space.
969 * @count: Maximum number of bytes to copy, including the trailing NUL.
971 * Copies a NUL-terminated string from userspace to kernel space.
972 * Caller must check the specified block with access_ok() before calling
975 * On success, returns the length of the string (not including the trailing
978 * If access to userspace fails, returns -EFAULT (some data may have been
981 * If @count is smaller than the length of the string, copies @count bytes
982 * and returns @count.
985 __strncpy_from_user(char *__to
, const char __user
*__from
, long __len
)
990 __asm__
__volatile__(
994 __MODULE_JAL(__strncpy_from_user_nocheck_asm
)
997 : "r" (__to
), "r" (__from
), "r" (__len
)
998 : "$2", "$3", "$4", "$5", "$6", __UA_t0
, "$31", "memory");
1004 * strncpy_from_user: - Copy a NUL terminated string from userspace.
1005 * @dst: Destination address, in kernel space. This buffer must be at
1006 * least @count bytes long.
1007 * @src: Source address, in user space.
1008 * @count: Maximum number of bytes to copy, including the trailing NUL.
1010 * Copies a NUL-terminated string from userspace to kernel space.
1012 * On success, returns the length of the string (not including the trailing
1015 * If access to userspace fails, returns -EFAULT (some data may have been
1018 * If @count is smaller than the length of the string, copies @count bytes
1019 * and returns @count.
1022 strncpy_from_user(char *__to
, const char __user
*__from
, long __len
)
1027 __asm__
__volatile__(
1031 __MODULE_JAL(__strncpy_from_user_asm
)
1034 : "r" (__to
), "r" (__from
), "r" (__len
)
1035 : "$2", "$3", "$4", "$5", "$6", __UA_t0
, "$31", "memory");
1040 /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
1041 static inline long __strlen_user(const char __user
*s
)
1046 __asm__
__volatile__(
1048 __MODULE_JAL(__strlen_user_nocheck_asm
)
1052 : "$2", "$4", __UA_t0
, "$31");
1058 * strlen_user: - Get the size of a string in user space.
1059 * @str: The string to measure.
1061 * Context: User context only. This function may sleep.
1063 * Get the size of a NUL-terminated string in user space.
1065 * Returns the size of the string INCLUDING the terminating NUL.
1066 * On exception, returns 0.
1068 * If there is a limit on the length of a valid string, you may wish to
1069 * consider using strnlen_user() instead.
1071 static inline long strlen_user(const char __user
*s
)
1076 __asm__
__volatile__(
1078 __MODULE_JAL(__strlen_user_asm
)
1082 : "$2", "$4", __UA_t0
, "$31");
1087 /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
1088 static inline long __strnlen_user(const char __user
*s
, long n
)
1093 __asm__
__volatile__(
1096 __MODULE_JAL(__strnlen_user_nocheck_asm
)
1100 : "$2", "$4", "$5", __UA_t0
, "$31");
1106 * strlen_user: - Get the size of a string in user space.
1107 * @str: The string to measure.
1109 * Context: User context only. This function may sleep.
1111 * Get the size of a NUL-terminated string in user space.
1113 * Returns the size of the string INCLUDING the terminating NUL.
1114 * On exception, returns 0.
1116 * If there is a limit on the length of a valid string, you may wish to
1117 * consider using strnlen_user() instead.
1119 static inline long strnlen_user(const char __user
*s
, long n
)
1124 __asm__
__volatile__(
1127 __MODULE_JAL(__strnlen_user_asm
)
1131 : "$2", "$4", "$5", __UA_t0
, "$31");
1136 struct exception_table_entry
1139 unsigned long nextinsn
;
1142 extern int fixup_exception(struct pt_regs
*regs
);
1144 #endif /* _ASM_UACCESS_H */