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 extern u64 __ua_limit;
40 #define __UA_LIMIT __ua_limit
42 #define __UA_ADDR ".dword"
44 #define __UA_ADDU "daddu"
48 #endif /* CONFIG_64BIT */
51 * USER_DS is a bitmask that has the bits set that may not be set in a valid
52 * userspace address. Note that we limit 32-bit userspace to 0x7fff8000 but
53 * the arithmetic we're doing only works if the limit is a power of two, so
54 * we use 0x80000000 here on 32-bit kernels. If a process passes an invalid
55 * address in this range it's the process's problem, not ours :-)
58 #define KERNEL_DS ((mm_segment_t) { 0UL })
59 #define USER_DS ((mm_segment_t) { __UA_LIMIT })
62 #define VERIFY_WRITE 1
64 #define get_ds() (KERNEL_DS)
65 #define get_fs() (current_thread_info()->addr_limit)
66 #define set_fs(x) (current_thread_info()->addr_limit = (x))
68 #define segment_eq(a, b) ((a).seg == (b).seg)
72 * Is a address valid? This does a straighforward calculation rather
76 * - "addr" doesn't have any high-bits set
77 * - AND "size" doesn't have any high-bits set
78 * - AND "addr+size" doesn't have any high-bits set
79 * - OR we are in kernel mode.
81 * __ua_size() is a trick to avoid runtime checking of positive constant
82 * sizes; for those we already know at compile time that the size is ok.
84 #define __ua_size(size) \
85 ((__builtin_constant_p(size) && (signed long) (size) > 0) ? 0 : (size))
88 * access_ok: - Checks if a user space pointer is valid
89 * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
90 * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
91 * to write to a block, it is always safe to read from it.
92 * @addr: User space pointer to start of block to check
93 * @size: Size of block to check
95 * Context: User context only. This function may sleep.
97 * Checks if a pointer to a block of memory in user space is valid.
99 * Returns true (nonzero) if the memory block may be valid, false (zero)
100 * if it is definitely invalid.
102 * Note that, depending on architecture, this function probably just
103 * checks that the pointer is in the user space range - after calling
104 * this function, memory access functions may still return -EFAULT.
107 #define __access_mask get_fs().seg
109 #define __access_ok(addr, size, mask) \
111 unsigned long __addr = (unsigned long) (addr); \
112 unsigned long __size = size; \
113 unsigned long __mask = mask; \
114 unsigned long __ok; \
116 __chk_user_ptr(addr); \
117 __ok = (signed long)(__mask & (__addr | (__addr + __size) | \
118 __ua_size(__size))); \
122 #define access_ok(type, addr, size) \
123 likely(__access_ok((addr), (size), __access_mask))
126 * put_user: - Write a simple value into user space.
127 * @x: Value to copy to user space.
128 * @ptr: Destination address, in user space.
130 * Context: User context only. This function may sleep.
132 * This macro copies a single simple value from kernel space to user
133 * space. It supports simple types like char and int, but not larger
134 * data types like structures or arrays.
136 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
137 * to the result of dereferencing @ptr.
139 * Returns zero on success, or -EFAULT on error.
141 #define put_user(x,ptr) \
142 __put_user_check((x), (ptr), sizeof(*(ptr)))
145 * get_user: - Get a simple variable from user space.
146 * @x: Variable to store result.
147 * @ptr: Source address, in user space.
149 * Context: User context only. This function may sleep.
151 * This macro copies a single simple variable from user space to kernel
152 * space. It supports simple types like char and int, but not larger
153 * data types like structures or arrays.
155 * @ptr must have pointer-to-simple-variable type, and the result of
156 * dereferencing @ptr must be assignable to @x without a cast.
158 * Returns zero on success, or -EFAULT on error.
159 * On error, the variable @x is set to zero.
161 #define get_user(x,ptr) \
162 __get_user_check((x), (ptr), sizeof(*(ptr)))
165 * __put_user: - Write a simple value into user space, with less checking.
166 * @x: Value to copy to user space.
167 * @ptr: Destination address, in user space.
169 * Context: User context only. This function may sleep.
171 * This macro copies a single simple value from kernel space to user
172 * space. It supports simple types like char and int, but not larger
173 * data types like structures or arrays.
175 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
176 * to the result of dereferencing @ptr.
178 * Caller must check the pointer with access_ok() before calling this
181 * Returns zero on success, or -EFAULT on error.
183 #define __put_user(x,ptr) \
184 __put_user_nocheck((x), (ptr), sizeof(*(ptr)))
187 * __get_user: - Get a simple variable from user space, with less checking.
188 * @x: Variable to store result.
189 * @ptr: Source address, in user space.
191 * Context: User context only. This function may sleep.
193 * This macro copies a single simple variable from user space to kernel
194 * space. It supports simple types like char and int, but not larger
195 * data types like structures or arrays.
197 * @ptr must have pointer-to-simple-variable type, and the result of
198 * dereferencing @ptr must be assignable to @x without a cast.
200 * Caller must check the pointer with access_ok() before calling this
203 * Returns zero on success, or -EFAULT on error.
204 * On error, the variable @x is set to zero.
206 #define __get_user(x,ptr) \
207 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
209 struct __large_struct { unsigned long buf[100]; };
210 #define __m(x) (*(struct __large_struct __user *)(x))
213 * Yuck. We need two variants, one for 64bit operation and one
214 * for 32 bit mode and old iron.
217 #define __GET_USER_DW(val, ptr) __get_user_asm_ll32(val, ptr)
220 #define __GET_USER_DW(val, ptr) __get_user_asm(val, "ld", ptr)
223 extern void __get_user_unknown(void);
225 #define __get_user_common(val, size, ptr) \
228 case 1: __get_user_asm(val, "lb", ptr); break; \
229 case 2: __get_user_asm(val, "lh", ptr); break; \
230 case 4: __get_user_asm(val, "lw", ptr); break; \
231 case 8: __GET_USER_DW(val, ptr); break; \
232 default: __get_user_unknown(); break; \
236 #define __get_user_nocheck(x, ptr, size) \
240 __chk_user_ptr(ptr); \
241 __get_user_common((x), size, ptr); \
245 #define __get_user_check(x, ptr, size) \
247 int __gu_err = -EFAULT; \
248 const __typeof__(*(ptr)) __user * __gu_ptr = (ptr); \
251 if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) \
252 __get_user_common((x), size, __gu_ptr); \
257 #define __get_user_asm(val, insn, addr) \
261 __asm__ __volatile__( \
262 "1: " insn " %1, %3 \n" \
265 " .section .fixup,\"ax\" \n" \
269 " .section __ex_table,\"a\" \n" \
270 " "__UA_ADDR "\t1b, 3b \n" \
272 : "=r" (__gu_err), "=r" (__gu_tmp) \
273 : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \
275 (val) = (__typeof__(*(addr))) __gu_tmp; \
279 * Get a long long 64 using 32 bit registers.
281 #define __get_user_asm_ll32(val, addr) \
284 unsigned long long l; \
285 __typeof__(*(addr)) t; \
288 __asm__ __volatile__( \
289 "1: lw %1, (%3) \n" \
290 "2: lw %D1, 4(%3) \n" \
293 " .section .fixup,\"ax\" \n" \
299 " .section __ex_table,\"a\" \n" \
300 " " __UA_ADDR " 1b, 4b \n" \
301 " " __UA_ADDR " 2b, 4b \n" \
303 : "=r" (__gu_err), "=&r" (__gu_tmp.l) \
304 : "0" (0), "r" (addr), "i" (-EFAULT)); \
306 (val) = __gu_tmp.t; \
310 * Yuck. We need two variants, one for 64bit operation and one
311 * for 32 bit mode and old iron.
314 #define __PUT_USER_DW(ptr) __put_user_asm_ll32(ptr)
317 #define __PUT_USER_DW(ptr) __put_user_asm("sd", ptr)
320 #define __put_user_nocheck(x, ptr, size) \
322 __typeof__(*(ptr)) __pu_val; \
325 __chk_user_ptr(ptr); \
328 case 1: __put_user_asm("sb", ptr); break; \
329 case 2: __put_user_asm("sh", ptr); break; \
330 case 4: __put_user_asm("sw", ptr); break; \
331 case 8: __PUT_USER_DW(ptr); break; \
332 default: __put_user_unknown(); break; \
337 #define __put_user_check(x, ptr, size) \
339 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
340 __typeof__(*(ptr)) __pu_val = (x); \
341 int __pu_err = -EFAULT; \
344 if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) { \
346 case 1: __put_user_asm("sb", __pu_addr); break; \
347 case 2: __put_user_asm("sh", __pu_addr); break; \
348 case 4: __put_user_asm("sw", __pu_addr); break; \
349 case 8: __PUT_USER_DW(__pu_addr); break; \
350 default: __put_user_unknown(); break; \
356 #define __put_user_asm(insn, ptr) \
358 __asm__ __volatile__( \
359 "1: " insn " %z2, %3 # __put_user_asm\n" \
362 " .section .fixup,\"ax\" \n" \
366 " .section __ex_table,\"a\" \n" \
367 " " __UA_ADDR " 1b, 3b \n" \
370 : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)), \
374 #define __put_user_asm_ll32(ptr) \
376 __asm__ __volatile__( \
377 "1: sw %2, (%3) # __put_user_asm_ll32 \n" \
378 "2: sw %D2, 4(%3) \n" \
381 " .section .fixup,\"ax\" \n" \
385 " .section __ex_table,\"a\" \n" \
386 " " __UA_ADDR " 1b, 4b \n" \
387 " " __UA_ADDR " 2b, 4b \n" \
390 : "0" (0), "r" (__pu_val), "r" (ptr), \
394 extern void __put_user_unknown(void);
397 * put_user_unaligned: - Write a simple value into user space.
398 * @x: Value to copy to user space.
399 * @ptr: Destination address, in user space.
401 * Context: User context only. This function may sleep.
403 * This macro copies a single simple value from kernel space to user
404 * space. It supports simple types like char and int, but not larger
405 * data types like structures or arrays.
407 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
408 * to the result of dereferencing @ptr.
410 * Returns zero on success, or -EFAULT on error.
412 #define put_user_unaligned(x,ptr) \
413 __put_user_unaligned_check((x),(ptr),sizeof(*(ptr)))
416 * get_user_unaligned: - Get a simple variable from user space.
417 * @x: Variable to store result.
418 * @ptr: Source address, in user space.
420 * Context: User context only. This function may sleep.
422 * This macro copies a single simple variable from user space to kernel
423 * space. It supports simple types like char and int, but not larger
424 * data types like structures or arrays.
426 * @ptr must have pointer-to-simple-variable type, and the result of
427 * dereferencing @ptr must be assignable to @x without a cast.
429 * Returns zero on success, or -EFAULT on error.
430 * On error, the variable @x is set to zero.
432 #define get_user_unaligned(x,ptr) \
433 __get_user_unaligned_check((x),(ptr),sizeof(*(ptr)))
436 * __put_user_unaligned: - Write a simple value into user space, with less checking.
437 * @x: Value to copy to user space.
438 * @ptr: Destination address, in user space.
440 * Context: User context only. This function may sleep.
442 * This macro copies a single simple value from kernel space to user
443 * space. It supports simple types like char and int, but not larger
444 * data types like structures or arrays.
446 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
447 * to the result of dereferencing @ptr.
449 * Caller must check the pointer with access_ok() before calling this
452 * Returns zero on success, or -EFAULT on error.
454 #define __put_user_unaligned(x,ptr) \
455 __put_user_unaligned_nocheck((x),(ptr),sizeof(*(ptr)))
458 * __get_user_unaligned: - Get a simple variable from user space, with less checking.
459 * @x: Variable to store result.
460 * @ptr: Source address, in user space.
462 * Context: User context only. This function may sleep.
464 * This macro copies a single simple variable from user space to kernel
465 * space. It supports simple types like char and int, but not larger
466 * data types like structures or arrays.
468 * @ptr must have pointer-to-simple-variable type, and the result of
469 * dereferencing @ptr must be assignable to @x without a cast.
471 * Caller must check the pointer with access_ok() before calling this
474 * Returns zero on success, or -EFAULT on error.
475 * On error, the variable @x is set to zero.
477 #define __get_user_unaligned(x,ptr) \
478 __get_user__unalignednocheck((x),(ptr),sizeof(*(ptr)))
481 * Yuck. We need two variants, one for 64bit operation and one
482 * for 32 bit mode and old iron.
485 #define __GET_USER_UNALIGNED_DW(val, ptr) \
486 __get_user_unaligned_asm_ll32(val, ptr)
489 #define __GET_USER_UNALIGNED_DW(val, ptr) \
490 __get_user_unaligned_asm(val, "uld", ptr)
493 extern void __get_user_unaligned_unknown(void);
495 #define __get_user_unaligned_common(val, size, ptr) \
498 case 1: __get_user_asm(val, "lb", ptr); break; \
499 case 2: __get_user_unaligned_asm(val, "ulh", ptr); break; \
500 case 4: __get_user_unaligned_asm(val, "ulw", ptr); break; \
501 case 8: __GET_USER_UNALIGNED_DW(val, ptr); break; \
502 default: __get_user_unaligned_unknown(); break; \
506 #define __get_user_unaligned_nocheck(x,ptr,size) \
510 __get_user_unaligned_common((x), size, ptr); \
514 #define __get_user_unaligned_check(x,ptr,size) \
516 int __gu_err = -EFAULT; \
517 const __typeof__(*(ptr)) __user * __gu_ptr = (ptr); \
519 if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) \
520 __get_user_unaligned_common((x), size, __gu_ptr); \
525 #define __get_user_unaligned_asm(val, insn, addr) \
529 __asm__ __volatile__( \
530 "1: " insn " %1, %3 \n" \
533 " .section .fixup,\"ax\" \n" \
537 " .section __ex_table,\"a\" \n" \
538 " "__UA_ADDR "\t1b, 3b \n" \
539 " "__UA_ADDR "\t1b + 4, 3b \n" \
541 : "=r" (__gu_err), "=r" (__gu_tmp) \
542 : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \
544 (val) = (__typeof__(*(addr))) __gu_tmp; \
548 * Get a long long 64 using 32 bit registers.
550 #define __get_user_unaligned_asm_ll32(val, addr) \
552 unsigned long long __gu_tmp; \
554 __asm__ __volatile__( \
555 "1: ulw %1, (%3) \n" \
556 "2: ulw %D1, 4(%3) \n" \
560 " .section .fixup,\"ax\" \n" \
566 " .section __ex_table,\"a\" \n" \
567 " " __UA_ADDR " 1b, 4b \n" \
568 " " __UA_ADDR " 1b + 4, 4b \n" \
569 " " __UA_ADDR " 2b, 4b \n" \
570 " " __UA_ADDR " 2b + 4, 4b \n" \
572 : "=r" (__gu_err), "=&r" (__gu_tmp) \
573 : "0" (0), "r" (addr), "i" (-EFAULT)); \
574 (val) = (__typeof__(*(addr))) __gu_tmp; \
578 * Yuck. We need two variants, one for 64bit operation and one
579 * for 32 bit mode and old iron.
582 #define __PUT_USER_UNALIGNED_DW(ptr) __put_user_unaligned_asm_ll32(ptr)
585 #define __PUT_USER_UNALIGNED_DW(ptr) __put_user_unaligned_asm("usd", ptr)
588 #define __put_user_unaligned_nocheck(x,ptr,size) \
590 __typeof__(*(ptr)) __pu_val; \
595 case 1: __put_user_asm("sb", ptr); break; \
596 case 2: __put_user_unaligned_asm("ush", ptr); break; \
597 case 4: __put_user_unaligned_asm("usw", ptr); break; \
598 case 8: __PUT_USER_UNALIGNED_DW(ptr); break; \
599 default: __put_user_unaligned_unknown(); break; \
604 #define __put_user_unaligned_check(x,ptr,size) \
606 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
607 __typeof__(*(ptr)) __pu_val = (x); \
608 int __pu_err = -EFAULT; \
610 if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) { \
612 case 1: __put_user_asm("sb", __pu_addr); break; \
613 case 2: __put_user_unaligned_asm("ush", __pu_addr); break; \
614 case 4: __put_user_unaligned_asm("usw", __pu_addr); break; \
615 case 8: __PUT_USER_UNALGINED_DW(__pu_addr); break; \
616 default: __put_user_unaligned_unknown(); break; \
622 #define __put_user_unaligned_asm(insn, ptr) \
624 __asm__ __volatile__( \
625 "1: " insn " %z2, %3 # __put_user_unaligned_asm\n" \
628 " .section .fixup,\"ax\" \n" \
632 " .section __ex_table,\"a\" \n" \
633 " " __UA_ADDR " 1b, 3b \n" \
636 : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)), \
640 #define __put_user_unaligned_asm_ll32(ptr) \
642 __asm__ __volatile__( \
643 "1: sw %2, (%3) # __put_user_unaligned_asm_ll32 \n" \
644 "2: sw %D2, 4(%3) \n" \
647 " .section .fixup,\"ax\" \n" \
651 " .section __ex_table,\"a\" \n" \
652 " " __UA_ADDR " 1b, 4b \n" \
653 " " __UA_ADDR " 1b + 4, 4b \n" \
654 " " __UA_ADDR " 2b, 4b \n" \
655 " " __UA_ADDR " 2b + 4, 4b \n" \
658 : "0" (0), "r" (__pu_val), "r" (ptr), \
662 extern void __put_user_unaligned_unknown(void);
665 * We're generating jump to subroutines which will be outside the range of
669 #define __MODULE_JAL(destination) \
671 __UA_LA "\t$1, " #destination "\n\t" \
675 #define __MODULE_JAL(destination) \
676 "jal\t" #destination "\n\t"
679 #ifndef CONFIG_CPU_DADDI_WORKAROUNDS
680 #define DADDI_SCRATCH "$0"
682 #define DADDI_SCRATCH "$3"
685 extern size_t __copy_user(void *__to, const void *__from, size_t __n);
687 #define __invoke_copy_to_user(to, from, n) \
689 register void __user *__cu_to_r __asm__("$4"); \
690 register const void *__cu_from_r __asm__("$5"); \
691 register long __cu_len_r __asm__("$6"); \
694 __cu_from_r = (from); \
696 __asm__ __volatile__( \
697 __MODULE_JAL(__copy_user) \
698 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
700 : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
701 DADDI_SCRATCH, "memory"); \
706 * __copy_to_user: - Copy a block of data into user space, with less checking.
707 * @to: Destination address, in user space.
708 * @from: Source address, in kernel space.
709 * @n: Number of bytes to copy.
711 * Context: User context only. This function may sleep.
713 * Copy data from kernel space to user space. Caller must check
714 * the specified block with access_ok() before calling this function.
716 * Returns number of bytes that could not be copied.
717 * On success, this will be zero.
719 #define __copy_to_user(to, from, n) \
721 void __user *__cu_to; \
722 const void *__cu_from; \
726 __cu_from = (from); \
729 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, __cu_len); \
733 extern size_t __copy_user_inatomic(void *__to, const void *__from, size_t __n);
735 #define __copy_to_user_inatomic(to, from, n) \
737 void __user *__cu_to; \
738 const void *__cu_from; \
742 __cu_from = (from); \
744 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, __cu_len); \
748 #define __copy_from_user_inatomic(to, from, n) \
751 const void __user *__cu_from; \
755 __cu_from = (from); \
757 __cu_len = __invoke_copy_from_user_inatomic(__cu_to, __cu_from, \
763 * copy_to_user: - Copy a block of data into user space.
764 * @to: Destination address, in user space.
765 * @from: Source address, in kernel space.
766 * @n: Number of bytes to copy.
768 * Context: User context only. This function may sleep.
770 * Copy data from kernel space to user space.
772 * Returns number of bytes that could not be copied.
773 * On success, this will be zero.
775 #define copy_to_user(to, from, n) \
777 void __user *__cu_to; \
778 const void *__cu_from; \
782 __cu_from = (from); \
784 if (access_ok(VERIFY_WRITE, __cu_to, __cu_len)) { \
786 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, \
792 #define __invoke_copy_from_user(to, from, n) \
794 register void *__cu_to_r __asm__("$4"); \
795 register const void __user *__cu_from_r __asm__("$5"); \
796 register long __cu_len_r __asm__("$6"); \
799 __cu_from_r = (from); \
801 __asm__ __volatile__( \
802 ".set\tnoreorder\n\t" \
803 __MODULE_JAL(__copy_user) \
805 __UA_ADDU "\t$1, %1, %2\n\t" \
808 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
810 : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
811 DADDI_SCRATCH, "memory"); \
815 #define __invoke_copy_from_user_inatomic(to, from, n) \
817 register void *__cu_to_r __asm__("$4"); \
818 register const void __user *__cu_from_r __asm__("$5"); \
819 register long __cu_len_r __asm__("$6"); \
822 __cu_from_r = (from); \
824 __asm__ __volatile__( \
825 ".set\tnoreorder\n\t" \
826 __MODULE_JAL(__copy_user_inatomic) \
828 __UA_ADDU "\t$1, %1, %2\n\t" \
831 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
833 : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
834 DADDI_SCRATCH, "memory"); \
839 * __copy_from_user: - Copy a block of data from user space, with less checking.
840 * @to: Destination address, in kernel space.
841 * @from: Source address, in user space.
842 * @n: Number of bytes to copy.
844 * Context: User context only. This function may sleep.
846 * Copy data from user space to kernel space. Caller must check
847 * the specified block with access_ok() before calling this function.
849 * Returns number of bytes that could not be copied.
850 * On success, this will be zero.
852 * If some data could not be copied, this function will pad the copied
853 * data to the requested size using zero bytes.
855 #define __copy_from_user(to, from, n) \
858 const void __user *__cu_from; \
862 __cu_from = (from); \
865 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
871 * copy_from_user: - Copy a block of data from user space.
872 * @to: Destination address, in kernel space.
873 * @from: Source address, in user space.
874 * @n: Number of bytes to copy.
876 * Context: User context only. This function may sleep.
878 * Copy data from user space to kernel space.
880 * Returns number of bytes that could not be copied.
881 * On success, this will be zero.
883 * If some data could not be copied, this function will pad the copied
884 * data to the requested size using zero bytes.
886 #define copy_from_user(to, from, n) \
889 const void __user *__cu_from; \
893 __cu_from = (from); \
895 if (access_ok(VERIFY_READ, __cu_from, __cu_len)) { \
897 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
903 #define __copy_in_user(to, from, n) \
905 void __user *__cu_to; \
906 const void __user *__cu_from; \
910 __cu_from = (from); \
913 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
918 #define copy_in_user(to, from, n) \
920 void __user *__cu_to; \
921 const void __user *__cu_from; \
925 __cu_from = (from); \
927 if (likely(access_ok(VERIFY_READ, __cu_from, __cu_len) && \
928 access_ok(VERIFY_WRITE, __cu_to, __cu_len))) { \
930 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
937 * __clear_user: - Zero a block of memory in user space, with less checking.
938 * @to: Destination address, in user space.
939 * @n: Number of bytes to zero.
941 * Zero a block of memory in user space. Caller must check
942 * the specified block with access_ok() before calling this function.
944 * Returns number of bytes that could not be cleared.
945 * On success, this will be zero.
947 static inline __kernel_size_t
948 __clear_user(void __user *addr, __kernel_size_t size)
953 __asm__ __volatile__(
957 __MODULE_JAL(__bzero)
960 : "r" (addr), "r" (size)
961 : "$4", "$5", "$6", __UA_t0, __UA_t1, "$31");
966 #define clear_user(addr,n) \
968 void __user * __cl_addr = (addr); \
969 unsigned long __cl_size = (n); \
970 if (__cl_size && access_ok(VERIFY_WRITE, \
971 __cl_addr, __cl_size)) \
972 __cl_size = __clear_user(__cl_addr, __cl_size); \
977 * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
978 * @dst: Destination address, in kernel space. This buffer must be at
979 * least @count bytes long.
980 * @src: Source address, in user space.
981 * @count: Maximum number of bytes to copy, including the trailing NUL.
983 * Copies a NUL-terminated string from userspace to kernel space.
984 * Caller must check the specified block with access_ok() before calling
987 * On success, returns the length of the string (not including the trailing
990 * If access to userspace fails, returns -EFAULT (some data may have been
993 * If @count is smaller than the length of the string, copies @count bytes
994 * and returns @count.
997 __strncpy_from_user(char *__to, const char __user *__from, long __len)
1002 __asm__ __volatile__(
1006 __MODULE_JAL(__strncpy_from_user_nocheck_asm)
1009 : "r" (__to), "r" (__from), "r" (__len)
1010 : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
1016 * strncpy_from_user: - Copy a NUL terminated string from userspace.
1017 * @dst: Destination address, in kernel space. This buffer must be at
1018 * least @count bytes long.
1019 * @src: Source address, in user space.
1020 * @count: Maximum number of bytes to copy, including the trailing NUL.
1022 * Copies a NUL-terminated string from userspace to kernel space.
1024 * On success, returns the length of the string (not including the trailing
1027 * If access to userspace fails, returns -EFAULT (some data may have been
1030 * If @count is smaller than the length of the string, copies @count bytes
1031 * and returns @count.
1034 strncpy_from_user(char *__to, const char __user *__from, long __len)
1039 __asm__ __volatile__(
1043 __MODULE_JAL(__strncpy_from_user_asm)
1046 : "r" (__to), "r" (__from), "r" (__len)
1047 : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
1052 /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
1053 static inline long __strlen_user(const char __user *s)
1058 __asm__ __volatile__(
1060 __MODULE_JAL(__strlen_user_nocheck_asm)
1064 : "$2", "$4", __UA_t0, "$31");
1070 * strlen_user: - Get the size of a string in user space.
1071 * @str: The string to measure.
1073 * Context: User context only. This function may sleep.
1075 * Get the size of a NUL-terminated string in user space.
1077 * Returns the size of the string INCLUDING the terminating NUL.
1078 * On exception, returns 0.
1080 * If there is a limit on the length of a valid string, you may wish to
1081 * consider using strnlen_user() instead.
1083 static inline long strlen_user(const char __user *s)
1088 __asm__ __volatile__(
1090 __MODULE_JAL(__strlen_user_asm)
1094 : "$2", "$4", __UA_t0, "$31");
1099 /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
1100 static inline long __strnlen_user(const char __user *s, long n)
1105 __asm__ __volatile__(
1108 __MODULE_JAL(__strnlen_user_nocheck_asm)
1112 : "$2", "$4", "$5", __UA_t0, "$31");
1118 * strlen_user: - Get the size of a string in user space.
1119 * @str: The string to measure.
1121 * Context: User context only. This function may sleep.
1123 * Get the size of a NUL-terminated string in user space.
1125 * Returns the size of the string INCLUDING the terminating NUL.
1126 * On exception, returns 0.
1128 * If there is a limit on the length of a valid string, you may wish to
1129 * consider using strnlen_user() instead.
1131 static inline long strnlen_user(const char __user *s, long n)
1136 __asm__ __volatile__(
1139 __MODULE_JAL(__strnlen_user_asm)
1143 : "$2", "$4", "$5", __UA_t0, "$31");
1148 struct exception_table_entry
1151 unsigned long nextinsn;
1154 extern int fixup_exception(struct pt_regs *regs);
1156 #endif /* _ASM_UACCESS_H */