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
9 * Copyright (C) 2014, Imagination Technologies Ltd.
11 #ifndef _ASM_UACCESS_H
12 #define _ASM_UACCESS_H
14 #include <linux/kernel.h>
15 #include <linux/errno.h>
16 #include <linux/thread_info.h>
17 #include <asm/asm-eva.h>
20 * The fs value determines whether argument validity checking should be
21 * performed or not. If get_fs() == USER_DS, checking is performed, with
22 * get_fs() == KERNEL_DS, checking is bypassed.
24 * For historical reasons, these macros are grossly misnamed.
28 #ifdef CONFIG_KVM_GUEST
29 #define __UA_LIMIT 0x40000000UL
31 #define __UA_LIMIT 0x80000000UL
34 #define __UA_ADDR ".word"
36 #define __UA_ADDU "addu"
40 #endif /* CONFIG_32BIT */
44 extern u64 __ua_limit;
46 #define __UA_LIMIT __ua_limit
48 #define __UA_ADDR ".dword"
50 #define __UA_ADDU "daddu"
54 #endif /* CONFIG_64BIT */
57 * USER_DS is a bitmask that has the bits set that may not be set in a valid
58 * userspace address. Note that we limit 32-bit userspace to 0x7fff8000 but
59 * the arithmetic we're doing only works if the limit is a power of two, so
60 * we use 0x80000000 here on 32-bit kernels. If a process passes an invalid
61 * address in this range it's the process's problem, not ours :-)
64 #ifdef CONFIG_KVM_GUEST
65 #define KERNEL_DS ((mm_segment_t) { 0x80000000UL })
66 #define USER_DS ((mm_segment_t) { 0xC0000000UL })
68 #define KERNEL_DS ((mm_segment_t) { 0UL })
69 #define USER_DS ((mm_segment_t) { __UA_LIMIT })
73 #define VERIFY_WRITE 1
75 #define get_ds() (KERNEL_DS)
76 #define get_fs() (current_thread_info()->addr_limit)
77 #define set_fs(x) (current_thread_info()->addr_limit = (x))
79 #define segment_eq(a, b) ((a).seg == (b).seg)
83 * Is a address valid? This does a straighforward calculation rather
87 * - "addr" doesn't have any high-bits set
88 * - AND "size" doesn't have any high-bits set
89 * - AND "addr+size" doesn't have any high-bits set
90 * - OR we are in kernel mode.
92 * __ua_size() is a trick to avoid runtime checking of positive constant
93 * sizes; for those we already know at compile time that the size is ok.
95 #define __ua_size(size) \
96 ((__builtin_constant_p(size) && (signed long) (size) > 0) ? 0 : (size))
99 * access_ok: - Checks if a user space pointer is valid
100 * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
101 * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
102 * to write to a block, it is always safe to read from it.
103 * @addr: User space pointer to start of block to check
104 * @size: Size of block to check
106 * Context: User context only. This function may sleep.
108 * Checks if a pointer to a block of memory in user space is valid.
110 * Returns true (nonzero) if the memory block may be valid, false (zero)
111 * if it is definitely invalid.
113 * Note that, depending on architecture, this function probably just
114 * checks that the pointer is in the user space range - after calling
115 * this function, memory access functions may still return -EFAULT.
118 #define __access_mask get_fs().seg
120 #define __access_ok(addr, size, mask) \
122 unsigned long __addr = (unsigned long) (addr); \
123 unsigned long __size = size; \
124 unsigned long __mask = mask; \
125 unsigned long __ok; \
127 __chk_user_ptr(addr); \
128 __ok = (signed long)(__mask & (__addr | (__addr + __size) | \
129 __ua_size(__size))); \
133 #define access_ok(type, addr, size) \
134 likely(__access_ok((addr), (size), __access_mask))
137 * put_user: - Write a simple value into user space.
138 * @x: Value to copy to user space.
139 * @ptr: Destination address, in user space.
141 * Context: User context only. This function may sleep.
143 * This macro copies a single simple value from kernel space to user
144 * space. It supports simple types like char and int, but not larger
145 * data types like structures or arrays.
147 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
148 * to the result of dereferencing @ptr.
150 * Returns zero on success, or -EFAULT on error.
152 #define put_user(x,ptr) \
153 __put_user_check((x), (ptr), sizeof(*(ptr)))
156 * get_user: - Get a simple variable from user space.
157 * @x: Variable to store result.
158 * @ptr: Source address, in user space.
160 * Context: User context only. This function may sleep.
162 * This macro copies a single simple variable from user space to kernel
163 * space. It supports simple types like char and int, but not larger
164 * data types like structures or arrays.
166 * @ptr must have pointer-to-simple-variable type, and the result of
167 * dereferencing @ptr must be assignable to @x without a cast.
169 * Returns zero on success, or -EFAULT on error.
170 * On error, the variable @x is set to zero.
172 #define get_user(x,ptr) \
173 __get_user_check((x), (ptr), sizeof(*(ptr)))
176 * __put_user: - Write a simple value into user space, with less checking.
177 * @x: Value to copy to user space.
178 * @ptr: Destination address, in user space.
180 * Context: User context only. This function may sleep.
182 * This macro copies a single simple value from kernel space to user
183 * space. It supports simple types like char and int, but not larger
184 * data types like structures or arrays.
186 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
187 * to the result of dereferencing @ptr.
189 * Caller must check the pointer with access_ok() before calling this
192 * Returns zero on success, or -EFAULT on error.
194 #define __put_user(x,ptr) \
195 __put_user_nocheck((x), (ptr), sizeof(*(ptr)))
198 * __get_user: - Get a simple variable from user space, with less checking.
199 * @x: Variable to store result.
200 * @ptr: Source address, in user space.
202 * Context: User context only. This function may sleep.
204 * This macro copies a single simple variable from user space to kernel
205 * space. It supports simple types like char and int, but not larger
206 * data types like structures or arrays.
208 * @ptr must have pointer-to-simple-variable type, and the result of
209 * dereferencing @ptr must be assignable to @x without a cast.
211 * Caller must check the pointer with access_ok() before calling this
214 * Returns zero on success, or -EFAULT on error.
215 * On error, the variable @x is set to zero.
217 #define __get_user(x,ptr) \
218 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
220 struct __large_struct { unsigned long buf[100]; };
221 #define __m(x) (*(struct __large_struct __user *)(x))
224 * Yuck. We need two variants, one for 64bit operation and one
225 * for 32 bit mode and old iron.
228 #define __get_kernel_common(val, size, ptr) __get_user_common(val, size, ptr)
231 * Kernel specific functions for EVA. We need to use normal load instructions
232 * to read data from kernel when operating in EVA mode. We use these macros to
233 * avoid redefining __get_user_asm for EVA.
240 #define _loadd _loadw
242 #define _loadd(reg, addr) "ld " reg ", " addr
244 #define _loadw(reg, addr) "lw " reg ", " addr
245 #define _loadh(reg, addr) "lh " reg ", " addr
246 #define _loadb(reg, addr) "lb " reg ", " addr
248 #define __get_kernel_common(val, size, ptr) \
251 case 1: __get_data_asm(val, _loadb, ptr); break; \
252 case 2: __get_data_asm(val, _loadh, ptr); break; \
253 case 4: __get_data_asm(val, _loadw, ptr); break; \
254 case 8: __GET_DW(val, _loadd, ptr); break; \
255 default: __get_user_unknown(); break; \
261 #define __GET_DW(val, insn, ptr) __get_data_asm_ll32(val, insn, ptr)
264 #define __GET_DW(val, insn, ptr) __get_data_asm(val, insn, ptr)
267 extern void __get_user_unknown(void);
269 #define __get_user_common(val, size, ptr) \
272 case 1: __get_data_asm(val, user_lb, ptr); break; \
273 case 2: __get_data_asm(val, user_lh, ptr); break; \
274 case 4: __get_data_asm(val, user_lw, ptr); break; \
275 case 8: __GET_DW(val, user_ld, ptr); break; \
276 default: __get_user_unknown(); break; \
280 #define __get_user_nocheck(x, ptr, size) \
284 if (segment_eq(get_fs(), get_ds())) { \
285 __get_kernel_common((x), size, ptr); \
287 __chk_user_ptr(ptr); \
288 __get_user_common((x), size, ptr); \
293 #define __get_user_check(x, ptr, size) \
295 int __gu_err = -EFAULT; \
296 const __typeof__(*(ptr)) __user * __gu_ptr = (ptr); \
299 if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) { \
300 if (segment_eq(get_fs(), get_ds())) \
301 __get_kernel_common((x), size, __gu_ptr); \
303 __get_user_common((x), size, __gu_ptr); \
310 #define __get_data_asm(val, insn, addr) \
314 __asm__ __volatile__( \
315 "1: "insn("%1", "%3")" \n" \
318 " .section .fixup,\"ax\" \n" \
323 " .section __ex_table,\"a\" \n" \
324 " "__UA_ADDR "\t1b, 3b \n" \
326 : "=r" (__gu_err), "=r" (__gu_tmp) \
327 : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \
329 (val) = (__typeof__(*(addr))) __gu_tmp; \
333 * Get a long long 64 using 32 bit registers.
335 #define __get_data_asm_ll32(val, insn, addr) \
338 unsigned long long l; \
339 __typeof__(*(addr)) t; \
342 __asm__ __volatile__( \
343 "1: " insn("%1", "(%3)")" \n" \
344 "2: " insn("%D1", "4(%3)")" \n" \
347 " .section .fixup,\"ax\" \n" \
353 " .section __ex_table,\"a\" \n" \
354 " " __UA_ADDR " 1b, 4b \n" \
355 " " __UA_ADDR " 2b, 4b \n" \
357 : "=r" (__gu_err), "=&r" (__gu_tmp.l) \
358 : "0" (0), "r" (addr), "i" (-EFAULT)); \
360 (val) = __gu_tmp.t; \
364 #define __put_kernel_common(ptr, size) __put_user_common(ptr, size)
367 * Kernel specific functions for EVA. We need to use normal load instructions
368 * to read data from kernel when operating in EVA mode. We use these macros to
369 * avoid redefining __get_data_asm for EVA.
376 #define _stored _storew
378 #define _stored(reg, addr) "ld " reg ", " addr
381 #define _storew(reg, addr) "sw " reg ", " addr
382 #define _storeh(reg, addr) "sh " reg ", " addr
383 #define _storeb(reg, addr) "sb " reg ", " addr
385 #define __put_kernel_common(ptr, size) \
388 case 1: __put_data_asm(_storeb, ptr); break; \
389 case 2: __put_data_asm(_storeh, ptr); break; \
390 case 4: __put_data_asm(_storew, ptr); break; \
391 case 8: __PUT_DW(_stored, ptr); break; \
392 default: __put_user_unknown(); break; \
398 * Yuck. We need two variants, one for 64bit operation and one
399 * for 32 bit mode and old iron.
402 #define __PUT_DW(insn, ptr) __put_data_asm_ll32(insn, ptr)
405 #define __PUT_DW(insn, ptr) __put_data_asm(insn, ptr)
408 #define __put_user_common(ptr, size) \
411 case 1: __put_data_asm(user_sb, ptr); break; \
412 case 2: __put_data_asm(user_sh, ptr); break; \
413 case 4: __put_data_asm(user_sw, ptr); break; \
414 case 8: __PUT_DW(user_sd, ptr); break; \
415 default: __put_user_unknown(); break; \
419 #define __put_user_nocheck(x, ptr, size) \
421 __typeof__(*(ptr)) __pu_val; \
425 if (segment_eq(get_fs(), get_ds())) { \
426 __put_kernel_common(ptr, size); \
428 __chk_user_ptr(ptr); \
429 __put_user_common(ptr, size); \
434 #define __put_user_check(x, ptr, size) \
436 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
437 __typeof__(*(ptr)) __pu_val = (x); \
438 int __pu_err = -EFAULT; \
441 if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) { \
442 if (segment_eq(get_fs(), get_ds())) \
443 __put_kernel_common(__pu_addr, size); \
445 __put_user_common(__pu_addr, size); \
451 #define __put_data_asm(insn, ptr) \
453 __asm__ __volatile__( \
454 "1: "insn("%z2", "%3")" # __put_data_asm \n" \
457 " .section .fixup,\"ax\" \n" \
461 " .section __ex_table,\"a\" \n" \
462 " " __UA_ADDR " 1b, 3b \n" \
465 : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)), \
469 #define __put_data_asm_ll32(insn, ptr) \
471 __asm__ __volatile__( \
472 "1: "insn("%2", "(%3)")" # __put_data_asm_ll32 \n" \
473 "2: "insn("%D2", "4(%3)")" \n" \
476 " .section .fixup,\"ax\" \n" \
480 " .section __ex_table,\"a\" \n" \
481 " " __UA_ADDR " 1b, 4b \n" \
482 " " __UA_ADDR " 2b, 4b \n" \
485 : "0" (0), "r" (__pu_val), "r" (ptr), \
489 extern void __put_user_unknown(void);
492 * ul{b,h,w} are macros and there are no equivalent macros for EVA.
493 * EVA unaligned access is handled in the ADE exception handler.
497 * put_user_unaligned: - Write a simple value into user space.
498 * @x: Value to copy to user space.
499 * @ptr: Destination address, in user space.
501 * Context: User context only. This function may sleep.
503 * This macro copies a single simple value from kernel space to user
504 * space. It supports simple types like char and int, but not larger
505 * data types like structures or arrays.
507 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
508 * to the result of dereferencing @ptr.
510 * Returns zero on success, or -EFAULT on error.
512 #define put_user_unaligned(x,ptr) \
513 __put_user_unaligned_check((x),(ptr),sizeof(*(ptr)))
516 * get_user_unaligned: - Get a simple variable from user space.
517 * @x: Variable to store result.
518 * @ptr: Source address, in user space.
520 * Context: User context only. This function may sleep.
522 * This macro copies a single simple variable from user space to kernel
523 * space. It supports simple types like char and int, but not larger
524 * data types like structures or arrays.
526 * @ptr must have pointer-to-simple-variable type, and the result of
527 * dereferencing @ptr must be assignable to @x without a cast.
529 * Returns zero on success, or -EFAULT on error.
530 * On error, the variable @x is set to zero.
532 #define get_user_unaligned(x,ptr) \
533 __get_user_unaligned_check((x),(ptr),sizeof(*(ptr)))
536 * __put_user_unaligned: - Write a simple value into user space, with less checking.
537 * @x: Value to copy to user space.
538 * @ptr: Destination address, in user space.
540 * Context: User context only. This function may sleep.
542 * This macro copies a single simple value from kernel space to user
543 * space. It supports simple types like char and int, but not larger
544 * data types like structures or arrays.
546 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
547 * to the result of dereferencing @ptr.
549 * Caller must check the pointer with access_ok() before calling this
552 * Returns zero on success, or -EFAULT on error.
554 #define __put_user_unaligned(x,ptr) \
555 __put_user_unaligned_nocheck((x),(ptr),sizeof(*(ptr)))
558 * __get_user_unaligned: - Get a simple variable from user space, with less checking.
559 * @x: Variable to store result.
560 * @ptr: Source address, in user space.
562 * Context: User context only. This function may sleep.
564 * This macro copies a single simple variable from user space to kernel
565 * space. It supports simple types like char and int, but not larger
566 * data types like structures or arrays.
568 * @ptr must have pointer-to-simple-variable type, and the result of
569 * dereferencing @ptr must be assignable to @x without a cast.
571 * Caller must check the pointer with access_ok() before calling this
574 * Returns zero on success, or -EFAULT on error.
575 * On error, the variable @x is set to zero.
577 #define __get_user_unaligned(x,ptr) \
578 __get_user__unalignednocheck((x),(ptr),sizeof(*(ptr)))
581 * Yuck. We need two variants, one for 64bit operation and one
582 * for 32 bit mode and old iron.
585 #define __GET_USER_UNALIGNED_DW(val, ptr) \
586 __get_user_unaligned_asm_ll32(val, ptr)
589 #define __GET_USER_UNALIGNED_DW(val, ptr) \
590 __get_user_unaligned_asm(val, "uld", ptr)
593 extern void __get_user_unaligned_unknown(void);
595 #define __get_user_unaligned_common(val, size, ptr) \
598 case 1: __get_data_asm(val, "lb", ptr); break; \
599 case 2: __get_user_unaligned_asm(val, "ulh", ptr); break; \
600 case 4: __get_user_unaligned_asm(val, "ulw", ptr); break; \
601 case 8: __GET_USER_UNALIGNED_DW(val, ptr); break; \
602 default: __get_user_unaligned_unknown(); break; \
606 #define __get_user_unaligned_nocheck(x,ptr,size) \
610 __get_user_unaligned_common((x), size, ptr); \
614 #define __get_user_unaligned_check(x,ptr,size) \
616 int __gu_err = -EFAULT; \
617 const __typeof__(*(ptr)) __user * __gu_ptr = (ptr); \
619 if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) \
620 __get_user_unaligned_common((x), size, __gu_ptr); \
625 #define __get_data_unaligned_asm(val, insn, addr) \
629 __asm__ __volatile__( \
630 "1: " insn " %1, %3 \n" \
633 " .section .fixup,\"ax\" \n" \
638 " .section __ex_table,\"a\" \n" \
639 " "__UA_ADDR "\t1b, 3b \n" \
640 " "__UA_ADDR "\t1b + 4, 3b \n" \
642 : "=r" (__gu_err), "=r" (__gu_tmp) \
643 : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \
645 (val) = (__typeof__(*(addr))) __gu_tmp; \
649 * Get a long long 64 using 32 bit registers.
651 #define __get_user_unaligned_asm_ll32(val, addr) \
653 unsigned long long __gu_tmp; \
655 __asm__ __volatile__( \
656 "1: ulw %1, (%3) \n" \
657 "2: ulw %D1, 4(%3) \n" \
661 " .section .fixup,\"ax\" \n" \
667 " .section __ex_table,\"a\" \n" \
668 " " __UA_ADDR " 1b, 4b \n" \
669 " " __UA_ADDR " 1b + 4, 4b \n" \
670 " " __UA_ADDR " 2b, 4b \n" \
671 " " __UA_ADDR " 2b + 4, 4b \n" \
673 : "=r" (__gu_err), "=&r" (__gu_tmp) \
674 : "0" (0), "r" (addr), "i" (-EFAULT)); \
675 (val) = (__typeof__(*(addr))) __gu_tmp; \
679 * Yuck. We need two variants, one for 64bit operation and one
680 * for 32 bit mode and old iron.
683 #define __PUT_USER_UNALIGNED_DW(ptr) __put_user_unaligned_asm_ll32(ptr)
686 #define __PUT_USER_UNALIGNED_DW(ptr) __put_user_unaligned_asm("usd", ptr)
689 #define __put_user_unaligned_common(ptr, size) \
692 case 1: __put_data_asm("sb", ptr); break; \
693 case 2: __put_user_unaligned_asm("ush", ptr); break; \
694 case 4: __put_user_unaligned_asm("usw", ptr); break; \
695 case 8: __PUT_USER_UNALIGNED_DW(ptr); break; \
696 default: __put_user_unaligned_unknown(); break; \
699 #define __put_user_unaligned_nocheck(x,ptr,size) \
701 __typeof__(*(ptr)) __pu_val; \
705 __put_user_unaligned_common(ptr, size); \
709 #define __put_user_unaligned_check(x,ptr,size) \
711 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
712 __typeof__(*(ptr)) __pu_val = (x); \
713 int __pu_err = -EFAULT; \
715 if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) \
716 __put_user_unaligned_common(__pu_addr, size); \
721 #define __put_user_unaligned_asm(insn, ptr) \
723 __asm__ __volatile__( \
724 "1: " insn " %z2, %3 # __put_user_unaligned_asm\n" \
727 " .section .fixup,\"ax\" \n" \
731 " .section __ex_table,\"a\" \n" \
732 " " __UA_ADDR " 1b, 3b \n" \
735 : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)), \
739 #define __put_user_unaligned_asm_ll32(ptr) \
741 __asm__ __volatile__( \
742 "1: sw %2, (%3) # __put_user_unaligned_asm_ll32 \n" \
743 "2: sw %D2, 4(%3) \n" \
746 " .section .fixup,\"ax\" \n" \
750 " .section __ex_table,\"a\" \n" \
751 " " __UA_ADDR " 1b, 4b \n" \
752 " " __UA_ADDR " 1b + 4, 4b \n" \
753 " " __UA_ADDR " 2b, 4b \n" \
754 " " __UA_ADDR " 2b + 4, 4b \n" \
757 : "0" (0), "r" (__pu_val), "r" (ptr), \
761 extern void __put_user_unaligned_unknown(void);
765 * We're generating jump to subroutines which will be outside the range of
769 #define __MODULE_JAL(destination) \
771 __UA_LA "\t$1, " #destination "\n\t" \
775 #define __MODULE_JAL(destination) \
776 "jal\t" #destination "\n\t"
779 #if defined(CONFIG_CPU_DADDI_WORKAROUNDS) || (defined(CONFIG_EVA) && \
780 defined(CONFIG_CPU_HAS_PREFETCH))
781 #define DADDI_SCRATCH "$3"
783 #define DADDI_SCRATCH "$0"
786 extern size_t __copy_user(void *__to, const void *__from, size_t __n);
789 #define __invoke_copy_to_user(to, from, n) \
791 register void __user *__cu_to_r __asm__("$4"); \
792 register const void *__cu_from_r __asm__("$5"); \
793 register long __cu_len_r __asm__("$6"); \
796 __cu_from_r = (from); \
798 __asm__ __volatile__( \
799 __MODULE_JAL(__copy_user) \
800 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
802 : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
803 DADDI_SCRATCH, "memory"); \
807 #define __invoke_copy_to_kernel(to, from, n) \
808 __invoke_copy_to_user(to, from, n)
813 * __copy_to_user: - Copy a block of data into user space, with less checking.
814 * @to: Destination address, in user space.
815 * @from: Source address, in kernel space.
816 * @n: Number of bytes to copy.
818 * Context: User context only. This function may sleep.
820 * Copy data from kernel space to user space. Caller must check
821 * the specified block with access_ok() before calling this function.
823 * Returns number of bytes that could not be copied.
824 * On success, this will be zero.
826 #define __copy_to_user(to, from, n) \
828 void __user *__cu_to; \
829 const void *__cu_from; \
833 __cu_from = (from); \
836 if (segment_eq(get_fs(), get_ds())) \
837 __cu_len = __invoke_copy_to_kernel(__cu_to, __cu_from, \
840 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, \
845 extern size_t __copy_user_inatomic(void *__to, const void *__from, size_t __n);
847 #define __copy_to_user_inatomic(to, from, n) \
849 void __user *__cu_to; \
850 const void *__cu_from; \
854 __cu_from = (from); \
856 if (segment_eq(get_fs(), get_ds())) \
857 __cu_len = __invoke_copy_to_kernel(__cu_to, __cu_from, \
860 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, \
865 #define __copy_from_user_inatomic(to, from, n) \
868 const void __user *__cu_from; \
872 __cu_from = (from); \
874 if (segment_eq(get_fs(), get_ds())) \
875 __cu_len = __invoke_copy_from_kernel_inatomic(__cu_to, \
879 __cu_len = __invoke_copy_from_user_inatomic(__cu_to, \
886 * copy_to_user: - Copy a block of data into user space.
887 * @to: Destination address, in user space.
888 * @from: Source address, in kernel space.
889 * @n: Number of bytes to copy.
891 * Context: User context only. This function may sleep.
893 * Copy data from kernel space to user space.
895 * Returns number of bytes that could not be copied.
896 * On success, this will be zero.
898 #define copy_to_user(to, from, n) \
900 void __user *__cu_to; \
901 const void *__cu_from; \
905 __cu_from = (from); \
907 if (segment_eq(get_fs(), get_ds())) { \
908 __cu_len = __invoke_copy_to_kernel(__cu_to, \
912 if (access_ok(VERIFY_WRITE, __cu_to, __cu_len)) { \
914 __cu_len = __invoke_copy_to_user(__cu_to, \
924 #define __invoke_copy_from_user(to, from, n) \
926 register void *__cu_to_r __asm__("$4"); \
927 register const void __user *__cu_from_r __asm__("$5"); \
928 register long __cu_len_r __asm__("$6"); \
931 __cu_from_r = (from); \
933 __asm__ __volatile__( \
934 ".set\tnoreorder\n\t" \
935 __MODULE_JAL(__copy_user) \
937 __UA_ADDU "\t$1, %1, %2\n\t" \
940 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
942 : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
943 DADDI_SCRATCH, "memory"); \
947 #define __invoke_copy_from_kernel(to, from, n) \
948 __invoke_copy_from_user(to, from, n)
950 /* For userland <-> userland operations */
951 #define ___invoke_copy_in_user(to, from, n) \
952 __invoke_copy_from_user(to, from, n)
954 /* For kernel <-> kernel operations */
955 #define ___invoke_copy_in_kernel(to, from, n) \
956 __invoke_copy_from_user(to, from, n)
958 #define __invoke_copy_from_user_inatomic(to, from, n) \
960 register void *__cu_to_r __asm__("$4"); \
961 register const void __user *__cu_from_r __asm__("$5"); \
962 register long __cu_len_r __asm__("$6"); \
965 __cu_from_r = (from); \
967 __asm__ __volatile__( \
968 ".set\tnoreorder\n\t" \
969 __MODULE_JAL(__copy_user_inatomic) \
971 __UA_ADDU "\t$1, %1, %2\n\t" \
974 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
976 : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
977 DADDI_SCRATCH, "memory"); \
981 #define __invoke_copy_from_kernel_inatomic(to, from, n) \
982 __invoke_copy_from_user_inatomic(to, from, n) \
986 /* EVA specific functions */
988 extern size_t __copy_user_inatomic_eva(void *__to, const void *__from,
990 extern size_t __copy_from_user_eva(void *__to, const void *__from,
992 extern size_t __copy_to_user_eva(void *__to, const void *__from,
994 extern size_t __copy_in_user_eva(void *__to, const void *__from, size_t __n);
996 #define __invoke_copy_from_user_eva_generic(to, from, n, func_ptr) \
998 register void *__cu_to_r __asm__("$4"); \
999 register const void __user *__cu_from_r __asm__("$5"); \
1000 register long __cu_len_r __asm__("$6"); \
1003 __cu_from_r = (from); \
1005 __asm__ __volatile__( \
1006 ".set\tnoreorder\n\t" \
1007 __MODULE_JAL(func_ptr) \
1009 __UA_ADDU "\t$1, %1, %2\n\t" \
1012 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
1014 : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
1015 DADDI_SCRATCH, "memory"); \
1019 #define __invoke_copy_to_user_eva_generic(to, from, n, func_ptr) \
1021 register void *__cu_to_r __asm__("$4"); \
1022 register const void __user *__cu_from_r __asm__("$5"); \
1023 register long __cu_len_r __asm__("$6"); \
1026 __cu_from_r = (from); \
1028 __asm__ __volatile__( \
1029 __MODULE_JAL(func_ptr) \
1030 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
1032 : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
1033 DADDI_SCRATCH, "memory"); \
1038 * Source or destination address is in userland. We need to go through
1041 #define __invoke_copy_from_user(to, from, n) \
1042 __invoke_copy_from_user_eva_generic(to, from, n, __copy_from_user_eva)
1044 #define __invoke_copy_from_user_inatomic(to, from, n) \
1045 __invoke_copy_from_user_eva_generic(to, from, n, \
1046 __copy_user_inatomic_eva)
1048 #define __invoke_copy_to_user(to, from, n) \
1049 __invoke_copy_to_user_eva_generic(to, from, n, __copy_to_user_eva)
1051 #define ___invoke_copy_in_user(to, from, n) \
1052 __invoke_copy_from_user_eva_generic(to, from, n, __copy_in_user_eva)
1055 * Source or destination address in the kernel. We are not going through
1058 #define __invoke_copy_from_kernel(to, from, n) \
1059 __invoke_copy_from_user_eva_generic(to, from, n, __copy_user)
1061 #define __invoke_copy_from_kernel_inatomic(to, from, n) \
1062 __invoke_copy_from_user_eva_generic(to, from, n, __copy_user_inatomic)
1064 #define __invoke_copy_to_kernel(to, from, n) \
1065 __invoke_copy_to_user_eva_generic(to, from, n, __copy_user)
1067 #define ___invoke_copy_in_kernel(to, from, n) \
1068 __invoke_copy_from_user_eva_generic(to, from, n, __copy_user)
1070 #endif /* CONFIG_EVA */
1073 * __copy_from_user: - Copy a block of data from user space, with less checking.
1074 * @to: Destination address, in kernel space.
1075 * @from: Source address, in user space.
1076 * @n: Number of bytes to copy.
1078 * Context: User context only. This function may sleep.
1080 * Copy data from user space to kernel space. Caller must check
1081 * the specified block with access_ok() before calling this function.
1083 * Returns number of bytes that could not be copied.
1084 * On success, this will be zero.
1086 * If some data could not be copied, this function will pad the copied
1087 * data to the requested size using zero bytes.
1089 #define __copy_from_user(to, from, n) \
1092 const void __user *__cu_from; \
1096 __cu_from = (from); \
1099 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
1105 * copy_from_user: - Copy a block of data from user space.
1106 * @to: Destination address, in kernel space.
1107 * @from: Source address, in user space.
1108 * @n: Number of bytes to copy.
1110 * Context: User context only. This function may sleep.
1112 * Copy data from user space to kernel space.
1114 * Returns number of bytes that could not be copied.
1115 * On success, this will be zero.
1117 * If some data could not be copied, this function will pad the copied
1118 * data to the requested size using zero bytes.
1120 #define copy_from_user(to, from, n) \
1123 const void __user *__cu_from; \
1127 __cu_from = (from); \
1129 if (segment_eq(get_fs(), get_ds())) { \
1130 __cu_len = __invoke_copy_from_kernel(__cu_to, \
1134 if (access_ok(VERIFY_READ, __cu_from, __cu_len)) { \
1136 __cu_len = __invoke_copy_from_user(__cu_to, \
1144 #define __copy_in_user(to, from, n) \
1146 void __user *__cu_to; \
1147 const void __user *__cu_from; \
1151 __cu_from = (from); \
1153 if (segment_eq(get_fs(), get_ds())) { \
1154 __cu_len = ___invoke_copy_in_kernel(__cu_to, __cu_from, \
1158 __cu_len = ___invoke_copy_in_user(__cu_to, __cu_from, \
1164 #define copy_in_user(to, from, n) \
1166 void __user *__cu_to; \
1167 const void __user *__cu_from; \
1171 __cu_from = (from); \
1173 if (segment_eq(get_fs(), get_ds())) { \
1174 __cu_len = ___invoke_copy_in_kernel(__cu_to,__cu_from, \
1177 if (likely(access_ok(VERIFY_READ, __cu_from, __cu_len) &&\
1178 access_ok(VERIFY_WRITE, __cu_to, __cu_len))) {\
1180 __cu_len = ___invoke_copy_in_user(__cu_to, \
1189 * __clear_user: - Zero a block of memory in user space, with less checking.
1190 * @to: Destination address, in user space.
1191 * @n: Number of bytes to zero.
1193 * Zero a block of memory in user space. Caller must check
1194 * the specified block with access_ok() before calling this function.
1196 * Returns number of bytes that could not be cleared.
1197 * On success, this will be zero.
1199 static inline __kernel_size_t
1200 __clear_user(void __user *addr, __kernel_size_t size)
1202 __kernel_size_t res;
1205 __asm__ __volatile__(
1209 __MODULE_JAL(__bzero)
1212 : "r" (addr), "r" (size)
1213 : "$4", "$5", "$6", __UA_t0, __UA_t1, "$31");
1218 #define clear_user(addr,n) \
1220 void __user * __cl_addr = (addr); \
1221 unsigned long __cl_size = (n); \
1222 if (__cl_size && access_ok(VERIFY_WRITE, \
1223 __cl_addr, __cl_size)) \
1224 __cl_size = __clear_user(__cl_addr, __cl_size); \
1229 * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
1230 * @dst: Destination address, in kernel space. This buffer must be at
1231 * least @count bytes long.
1232 * @src: Source address, in user space.
1233 * @count: Maximum number of bytes to copy, including the trailing NUL.
1235 * Copies a NUL-terminated string from userspace to kernel space.
1236 * Caller must check the specified block with access_ok() before calling
1239 * On success, returns the length of the string (not including the trailing
1242 * If access to userspace fails, returns -EFAULT (some data may have been
1245 * If @count is smaller than the length of the string, copies @count bytes
1246 * and returns @count.
1249 __strncpy_from_user(char *__to, const char __user *__from, long __len)
1253 if (segment_eq(get_fs(), get_ds())) {
1254 __asm__ __volatile__(
1258 __MODULE_JAL(__strncpy_from_kernel_nocheck_asm)
1261 : "r" (__to), "r" (__from), "r" (__len)
1262 : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
1265 __asm__ __volatile__(
1269 __MODULE_JAL(__strncpy_from_user_nocheck_asm)
1272 : "r" (__to), "r" (__from), "r" (__len)
1273 : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
1280 * strncpy_from_user: - Copy a NUL terminated string from userspace.
1281 * @dst: Destination address, in kernel space. This buffer must be at
1282 * least @count bytes long.
1283 * @src: Source address, in user space.
1284 * @count: Maximum number of bytes to copy, including the trailing NUL.
1286 * Copies a NUL-terminated string from userspace to kernel space.
1288 * On success, returns the length of the string (not including the trailing
1291 * If access to userspace fails, returns -EFAULT (some data may have been
1294 * If @count is smaller than the length of the string, copies @count bytes
1295 * and returns @count.
1298 strncpy_from_user(char *__to, const char __user *__from, long __len)
1302 if (segment_eq(get_fs(), get_ds())) {
1303 __asm__ __volatile__(
1307 __MODULE_JAL(__strncpy_from_kernel_asm)
1310 : "r" (__to), "r" (__from), "r" (__len)
1311 : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
1314 __asm__ __volatile__(
1318 __MODULE_JAL(__strncpy_from_user_asm)
1321 : "r" (__to), "r" (__from), "r" (__len)
1322 : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
1329 * strlen_user: - Get the size of a string in user space.
1330 * @str: The string to measure.
1332 * Context: User context only. This function may sleep.
1334 * Get the size of a NUL-terminated string in user space.
1336 * Returns the size of the string INCLUDING the terminating NUL.
1337 * On exception, returns 0.
1339 * If there is a limit on the length of a valid string, you may wish to
1340 * consider using strnlen_user() instead.
1342 static inline long strlen_user(const char __user *s)
1346 if (segment_eq(get_fs(), get_ds())) {
1347 __asm__ __volatile__(
1349 __MODULE_JAL(__strlen_kernel_asm)
1353 : "$2", "$4", __UA_t0, "$31");
1356 __asm__ __volatile__(
1358 __MODULE_JAL(__strlen_kernel_asm)
1362 : "$2", "$4", __UA_t0, "$31");
1368 /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
1369 static inline long __strnlen_user(const char __user *s, long n)
1373 if (segment_eq(get_fs(), get_ds())) {
1374 __asm__ __volatile__(
1377 __MODULE_JAL(__strnlen_kernel_nocheck_asm)
1381 : "$2", "$4", "$5", __UA_t0, "$31");
1384 __asm__ __volatile__(
1387 __MODULE_JAL(__strnlen_user_nocheck_asm)
1391 : "$2", "$4", "$5", __UA_t0, "$31");
1398 * strnlen_user: - Get the size of a string in user space.
1399 * @str: The string to measure.
1401 * Context: User context only. This function may sleep.
1403 * Get the size of a NUL-terminated string in user space.
1405 * Returns the size of the string INCLUDING the terminating NUL.
1406 * On exception, returns 0.
1407 * If the string is too long, returns a value greater than @n.
1409 static inline long strnlen_user(const char __user *s, long n)
1414 if (segment_eq(get_fs(), get_ds())) {
1415 __asm__ __volatile__(
1418 __MODULE_JAL(__strnlen_kernel_asm)
1422 : "$2", "$4", "$5", __UA_t0, "$31");
1424 __asm__ __volatile__(
1427 __MODULE_JAL(__strnlen_user_asm)
1431 : "$2", "$4", "$5", __UA_t0, "$31");
1437 struct exception_table_entry
1440 unsigned long nextinsn;
1443 extern int fixup_exception(struct pt_regs *regs);
1445 #endif /* _ASM_UACCESS_H */