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/string.h>
16 #include <asm/asm-eva.h>
17 #include <asm/extable.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 })
72 #define get_ds() (KERNEL_DS)
73 #define get_fs() (current_thread_info()->addr_limit)
74 #define set_fs(x) (current_thread_info()->addr_limit = (x))
76 #define segment_eq(a, b) ((a).seg == (b).seg)
79 * eva_kernel_access() - determine whether kernel memory access on an EVA system
81 * Determines whether memory accesses should be performed to kernel memory
82 * on a system using Extended Virtual Addressing (EVA).
84 * Return: true if a kernel memory access on an EVA system, else false.
86 static inline bool eva_kernel_access(void)
88 if (!IS_ENABLED(CONFIG_EVA))
91 return uaccess_kernel();
95 * Is a address valid? This does a straightforward calculation rather
99 * - "addr" doesn't have any high-bits set
100 * - AND "size" doesn't have any high-bits set
101 * - AND "addr+size" doesn't have any high-bits set
102 * - OR we are in kernel mode.
104 * __ua_size() is a trick to avoid runtime checking of positive constant
105 * sizes; for those we already know at compile time that the size is ok.
107 #define __ua_size(size) \
108 ((__builtin_constant_p(size) && (signed long) (size) > 0) ? 0 : (size))
111 * access_ok: - Checks if a user space pointer is valid
112 * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
113 * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
114 * to write to a block, it is always safe to read from it.
115 * @addr: User space pointer to start of block to check
116 * @size: Size of block to check
118 * Context: User context only. This function may sleep if pagefaults are
121 * Checks if a pointer to a block of memory in user space is valid.
123 * Returns true (nonzero) if the memory block may be valid, false (zero)
124 * if it is definitely invalid.
126 * Note that, depending on architecture, this function probably just
127 * checks that the pointer is in the user space range - after calling
128 * this function, memory access functions may still return -EFAULT.
131 static inline int __access_ok(const void __user *p, unsigned long size)
133 unsigned long addr = (unsigned long)p;
134 return (get_fs().seg & (addr | (addr + size) | __ua_size(size))) == 0;
137 #define access_ok(type, addr, size) \
138 likely(__access_ok((addr), (size)))
141 * put_user: - Write a simple value into user space.
142 * @x: Value to copy to user space.
143 * @ptr: Destination address, in user space.
145 * Context: User context only. This function may sleep if pagefaults are
148 * This macro copies a single simple value from kernel space to user
149 * space. It supports simple types like char and int, but not larger
150 * data types like structures or arrays.
152 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
153 * to the result of dereferencing @ptr.
155 * Returns zero on success, or -EFAULT on error.
157 #define put_user(x,ptr) \
158 __put_user_check((x), (ptr), sizeof(*(ptr)))
161 * get_user: - Get a simple variable from user space.
162 * @x: Variable to store result.
163 * @ptr: Source address, in user space.
165 * Context: User context only. This function may sleep if pagefaults are
168 * This macro copies a single simple variable from user space to kernel
169 * space. It supports simple types like char and int, but not larger
170 * data types like structures or arrays.
172 * @ptr must have pointer-to-simple-variable type, and the result of
173 * dereferencing @ptr must be assignable to @x without a cast.
175 * Returns zero on success, or -EFAULT on error.
176 * On error, the variable @x is set to zero.
178 #define get_user(x,ptr) \
179 __get_user_check((x), (ptr), sizeof(*(ptr)))
182 * __put_user: - Write a simple value into user space, with less checking.
183 * @x: Value to copy to user space.
184 * @ptr: Destination address, in user space.
186 * Context: User context only. This function may sleep if pagefaults are
189 * This macro copies a single simple value from kernel space to user
190 * space. It supports simple types like char and int, but not larger
191 * data types like structures or arrays.
193 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
194 * to the result of dereferencing @ptr.
196 * Caller must check the pointer with access_ok() before calling this
199 * Returns zero on success, or -EFAULT on error.
201 #define __put_user(x,ptr) \
202 __put_user_nocheck((x), (ptr), sizeof(*(ptr)))
205 * __get_user: - Get a simple variable from user space, with less checking.
206 * @x: Variable to store result.
207 * @ptr: Source address, in user space.
209 * Context: User context only. This function may sleep if pagefaults are
212 * This macro copies a single simple variable from user space to kernel
213 * space. It supports simple types like char and int, but not larger
214 * data types like structures or arrays.
216 * @ptr must have pointer-to-simple-variable type, and the result of
217 * dereferencing @ptr must be assignable to @x without a cast.
219 * Caller must check the pointer with access_ok() before calling this
222 * Returns zero on success, or -EFAULT on error.
223 * On error, the variable @x is set to zero.
225 #define __get_user(x,ptr) \
226 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
228 struct __large_struct { unsigned long buf[100]; };
229 #define __m(x) (*(struct __large_struct __user *)(x))
232 * Yuck. We need two variants, one for 64bit operation and one
233 * for 32 bit mode and old iron.
236 #define __get_kernel_common(val, size, ptr) __get_user_common(val, size, ptr)
239 * Kernel specific functions for EVA. We need to use normal load instructions
240 * to read data from kernel when operating in EVA mode. We use these macros to
241 * avoid redefining __get_user_asm for EVA.
248 #define _loadd _loadw
250 #define _loadd(reg, addr) "ld " reg ", " addr
252 #define _loadw(reg, addr) "lw " reg ", " addr
253 #define _loadh(reg, addr) "lh " reg ", " addr
254 #define _loadb(reg, addr) "lb " reg ", " addr
256 #define __get_kernel_common(val, size, ptr) \
259 case 1: __get_data_asm(val, _loadb, ptr); break; \
260 case 2: __get_data_asm(val, _loadh, ptr); break; \
261 case 4: __get_data_asm(val, _loadw, ptr); break; \
262 case 8: __GET_DW(val, _loadd, ptr); break; \
263 default: __get_user_unknown(); break; \
269 #define __GET_DW(val, insn, ptr) __get_data_asm_ll32(val, insn, ptr)
272 #define __GET_DW(val, insn, ptr) __get_data_asm(val, insn, ptr)
275 extern void __get_user_unknown(void);
277 #define __get_user_common(val, size, ptr) \
280 case 1: __get_data_asm(val, user_lb, ptr); break; \
281 case 2: __get_data_asm(val, user_lh, ptr); break; \
282 case 4: __get_data_asm(val, user_lw, ptr); break; \
283 case 8: __GET_DW(val, user_ld, ptr); break; \
284 default: __get_user_unknown(); break; \
288 #define __get_user_nocheck(x, ptr, size) \
292 if (eva_kernel_access()) { \
293 __get_kernel_common((x), size, ptr); \
295 __chk_user_ptr(ptr); \
296 __get_user_common((x), size, ptr); \
301 #define __get_user_check(x, ptr, size) \
303 int __gu_err = -EFAULT; \
304 const __typeof__(*(ptr)) __user * __gu_ptr = (ptr); \
307 if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) { \
308 if (eva_kernel_access()) \
309 __get_kernel_common((x), size, __gu_ptr); \
311 __get_user_common((x), size, __gu_ptr); \
318 #define __get_data_asm(val, insn, addr) \
322 __asm__ __volatile__( \
323 "1: "insn("%1", "%3")" \n" \
326 " .section .fixup,\"ax\" \n" \
331 " .section __ex_table,\"a\" \n" \
332 " "__UA_ADDR "\t1b, 3b \n" \
334 : "=r" (__gu_err), "=r" (__gu_tmp) \
335 : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \
337 (val) = (__typeof__(*(addr))) __gu_tmp; \
341 * Get a long long 64 using 32 bit registers.
343 #define __get_data_asm_ll32(val, insn, addr) \
346 unsigned long long l; \
347 __typeof__(*(addr)) t; \
350 __asm__ __volatile__( \
351 "1: " insn("%1", "(%3)")" \n" \
352 "2: " insn("%D1", "4(%3)")" \n" \
355 " .section .fixup,\"ax\" \n" \
361 " .section __ex_table,\"a\" \n" \
362 " " __UA_ADDR " 1b, 4b \n" \
363 " " __UA_ADDR " 2b, 4b \n" \
365 : "=r" (__gu_err), "=&r" (__gu_tmp.l) \
366 : "0" (0), "r" (addr), "i" (-EFAULT)); \
368 (val) = __gu_tmp.t; \
372 #define __put_kernel_common(ptr, size) __put_user_common(ptr, size)
375 * Kernel specific functions for EVA. We need to use normal load instructions
376 * to read data from kernel when operating in EVA mode. We use these macros to
377 * avoid redefining __get_data_asm for EVA.
384 #define _stored _storew
386 #define _stored(reg, addr) "ld " reg ", " addr
389 #define _storew(reg, addr) "sw " reg ", " addr
390 #define _storeh(reg, addr) "sh " reg ", " addr
391 #define _storeb(reg, addr) "sb " reg ", " addr
393 #define __put_kernel_common(ptr, size) \
396 case 1: __put_data_asm(_storeb, ptr); break; \
397 case 2: __put_data_asm(_storeh, ptr); break; \
398 case 4: __put_data_asm(_storew, ptr); break; \
399 case 8: __PUT_DW(_stored, ptr); break; \
400 default: __put_user_unknown(); break; \
406 * Yuck. We need two variants, one for 64bit operation and one
407 * for 32 bit mode and old iron.
410 #define __PUT_DW(insn, ptr) __put_data_asm_ll32(insn, ptr)
413 #define __PUT_DW(insn, ptr) __put_data_asm(insn, ptr)
416 #define __put_user_common(ptr, size) \
419 case 1: __put_data_asm(user_sb, ptr); break; \
420 case 2: __put_data_asm(user_sh, ptr); break; \
421 case 4: __put_data_asm(user_sw, ptr); break; \
422 case 8: __PUT_DW(user_sd, ptr); break; \
423 default: __put_user_unknown(); break; \
427 #define __put_user_nocheck(x, ptr, size) \
429 __typeof__(*(ptr)) __pu_val; \
433 if (eva_kernel_access()) { \
434 __put_kernel_common(ptr, size); \
436 __chk_user_ptr(ptr); \
437 __put_user_common(ptr, size); \
442 #define __put_user_check(x, ptr, size) \
444 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
445 __typeof__(*(ptr)) __pu_val = (x); \
446 int __pu_err = -EFAULT; \
449 if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) { \
450 if (eva_kernel_access()) \
451 __put_kernel_common(__pu_addr, size); \
453 __put_user_common(__pu_addr, size); \
459 #define __put_data_asm(insn, ptr) \
461 __asm__ __volatile__( \
462 "1: "insn("%z2", "%3")" # __put_data_asm \n" \
465 " .section .fixup,\"ax\" \n" \
469 " .section __ex_table,\"a\" \n" \
470 " " __UA_ADDR " 1b, 3b \n" \
473 : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)), \
477 #define __put_data_asm_ll32(insn, ptr) \
479 __asm__ __volatile__( \
480 "1: "insn("%2", "(%3)")" # __put_data_asm_ll32 \n" \
481 "2: "insn("%D2", "4(%3)")" \n" \
484 " .section .fixup,\"ax\" \n" \
488 " .section __ex_table,\"a\" \n" \
489 " " __UA_ADDR " 1b, 4b \n" \
490 " " __UA_ADDR " 2b, 4b \n" \
493 : "0" (0), "r" (__pu_val), "r" (ptr), \
497 extern void __put_user_unknown(void);
500 * ul{b,h,w} are macros and there are no equivalent macros for EVA.
501 * EVA unaligned access is handled in the ADE exception handler.
505 * put_user_unaligned: - Write a simple value into user space.
506 * @x: Value to copy to user space.
507 * @ptr: Destination address, in user space.
509 * Context: User context only. This function may sleep if pagefaults are
512 * This macro copies a single simple value from kernel space to user
513 * space. It supports simple types like char and int, but not larger
514 * data types like structures or arrays.
516 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
517 * to the result of dereferencing @ptr.
519 * Returns zero on success, or -EFAULT on error.
521 #define put_user_unaligned(x,ptr) \
522 __put_user_unaligned_check((x),(ptr),sizeof(*(ptr)))
525 * get_user_unaligned: - Get a simple variable from user space.
526 * @x: Variable to store result.
527 * @ptr: Source address, in user space.
529 * Context: User context only. This function may sleep if pagefaults are
532 * This macro copies a single simple variable from user space to kernel
533 * space. It supports simple types like char and int, but not larger
534 * data types like structures or arrays.
536 * @ptr must have pointer-to-simple-variable type, and the result of
537 * dereferencing @ptr must be assignable to @x without a cast.
539 * Returns zero on success, or -EFAULT on error.
540 * On error, the variable @x is set to zero.
542 #define get_user_unaligned(x,ptr) \
543 __get_user_unaligned_check((x),(ptr),sizeof(*(ptr)))
546 * __put_user_unaligned: - Write a simple value into user space, with less checking.
547 * @x: Value to copy to user space.
548 * @ptr: Destination address, in user space.
550 * Context: User context only. This function may sleep if pagefaults are
553 * This macro copies a single simple value from kernel space to user
554 * space. It supports simple types like char and int, but not larger
555 * data types like structures or arrays.
557 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
558 * to the result of dereferencing @ptr.
560 * Caller must check the pointer with access_ok() before calling this
563 * Returns zero on success, or -EFAULT on error.
565 #define __put_user_unaligned(x,ptr) \
566 __put_user_unaligned_nocheck((x),(ptr),sizeof(*(ptr)))
569 * __get_user_unaligned: - Get a simple variable from user space, with less checking.
570 * @x: Variable to store result.
571 * @ptr: Source address, in user space.
573 * Context: User context only. This function may sleep if pagefaults are
576 * This macro copies a single simple variable from user space to kernel
577 * space. It supports simple types like char and int, but not larger
578 * data types like structures or arrays.
580 * @ptr must have pointer-to-simple-variable type, and the result of
581 * dereferencing @ptr must be assignable to @x without a cast.
583 * Caller must check the pointer with access_ok() before calling this
586 * Returns zero on success, or -EFAULT on error.
587 * On error, the variable @x is set to zero.
589 #define __get_user_unaligned(x,ptr) \
590 __get_user_unaligned_nocheck((x),(ptr),sizeof(*(ptr)))
593 * Yuck. We need two variants, one for 64bit operation and one
594 * for 32 bit mode and old iron.
597 #define __GET_USER_UNALIGNED_DW(val, ptr) \
598 __get_user_unaligned_asm_ll32(val, ptr)
601 #define __GET_USER_UNALIGNED_DW(val, ptr) \
602 __get_user_unaligned_asm(val, "uld", ptr)
605 extern void __get_user_unaligned_unknown(void);
607 #define __get_user_unaligned_common(val, size, ptr) \
610 case 1: __get_data_asm(val, "lb", ptr); break; \
611 case 2: __get_data_unaligned_asm(val, "ulh", ptr); break; \
612 case 4: __get_data_unaligned_asm(val, "ulw", ptr); break; \
613 case 8: __GET_USER_UNALIGNED_DW(val, ptr); break; \
614 default: __get_user_unaligned_unknown(); break; \
618 #define __get_user_unaligned_nocheck(x,ptr,size) \
622 __get_user_unaligned_common((x), size, ptr); \
626 #define __get_user_unaligned_check(x,ptr,size) \
628 int __gu_err = -EFAULT; \
629 const __typeof__(*(ptr)) __user * __gu_ptr = (ptr); \
631 if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) \
632 __get_user_unaligned_common((x), size, __gu_ptr); \
637 #define __get_data_unaligned_asm(val, insn, addr) \
641 __asm__ __volatile__( \
642 "1: " insn " %1, %3 \n" \
645 " .section .fixup,\"ax\" \n" \
650 " .section __ex_table,\"a\" \n" \
651 " "__UA_ADDR "\t1b, 3b \n" \
652 " "__UA_ADDR "\t1b + 4, 3b \n" \
654 : "=r" (__gu_err), "=r" (__gu_tmp) \
655 : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \
657 (val) = (__typeof__(*(addr))) __gu_tmp; \
661 * Get a long long 64 using 32 bit registers.
663 #define __get_user_unaligned_asm_ll32(val, addr) \
665 unsigned long long __gu_tmp; \
667 __asm__ __volatile__( \
668 "1: ulw %1, (%3) \n" \
669 "2: ulw %D1, 4(%3) \n" \
673 " .section .fixup,\"ax\" \n" \
679 " .section __ex_table,\"a\" \n" \
680 " " __UA_ADDR " 1b, 4b \n" \
681 " " __UA_ADDR " 1b + 4, 4b \n" \
682 " " __UA_ADDR " 2b, 4b \n" \
683 " " __UA_ADDR " 2b + 4, 4b \n" \
685 : "=r" (__gu_err), "=&r" (__gu_tmp) \
686 : "0" (0), "r" (addr), "i" (-EFAULT)); \
687 (val) = (__typeof__(*(addr))) __gu_tmp; \
691 * Yuck. We need two variants, one for 64bit operation and one
692 * for 32 bit mode and old iron.
695 #define __PUT_USER_UNALIGNED_DW(ptr) __put_user_unaligned_asm_ll32(ptr)
698 #define __PUT_USER_UNALIGNED_DW(ptr) __put_user_unaligned_asm("usd", ptr)
701 #define __put_user_unaligned_common(ptr, size) \
704 case 1: __put_data_asm("sb", ptr); break; \
705 case 2: __put_user_unaligned_asm("ush", ptr); break; \
706 case 4: __put_user_unaligned_asm("usw", ptr); break; \
707 case 8: __PUT_USER_UNALIGNED_DW(ptr); break; \
708 default: __put_user_unaligned_unknown(); break; \
711 #define __put_user_unaligned_nocheck(x,ptr,size) \
713 __typeof__(*(ptr)) __pu_val; \
717 __put_user_unaligned_common(ptr, size); \
721 #define __put_user_unaligned_check(x,ptr,size) \
723 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
724 __typeof__(*(ptr)) __pu_val = (x); \
725 int __pu_err = -EFAULT; \
727 if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) \
728 __put_user_unaligned_common(__pu_addr, size); \
733 #define __put_user_unaligned_asm(insn, ptr) \
735 __asm__ __volatile__( \
736 "1: " insn " %z2, %3 # __put_user_unaligned_asm\n" \
739 " .section .fixup,\"ax\" \n" \
743 " .section __ex_table,\"a\" \n" \
744 " " __UA_ADDR " 1b, 3b \n" \
747 : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)), \
751 #define __put_user_unaligned_asm_ll32(ptr) \
753 __asm__ __volatile__( \
754 "1: sw %2, (%3) # __put_user_unaligned_asm_ll32 \n" \
755 "2: sw %D2, 4(%3) \n" \
758 " .section .fixup,\"ax\" \n" \
762 " .section __ex_table,\"a\" \n" \
763 " " __UA_ADDR " 1b, 4b \n" \
764 " " __UA_ADDR " 1b + 4, 4b \n" \
765 " " __UA_ADDR " 2b, 4b \n" \
766 " " __UA_ADDR " 2b + 4, 4b \n" \
769 : "0" (0), "r" (__pu_val), "r" (ptr), \
773 extern void __put_user_unaligned_unknown(void);
777 * We're generating jump to subroutines which will be outside the range of
781 #define __MODULE_JAL(destination) \
783 __UA_LA "\t$1, " #destination "\n\t" \
787 #define __MODULE_JAL(destination) \
788 "jal\t" #destination "\n\t"
791 #if defined(CONFIG_CPU_DADDI_WORKAROUNDS) || (defined(CONFIG_EVA) && \
792 defined(CONFIG_CPU_HAS_PREFETCH))
793 #define DADDI_SCRATCH "$3"
795 #define DADDI_SCRATCH "$0"
798 extern size_t __copy_user(void *__to, const void *__from, size_t __n);
800 #define __invoke_copy_from(func, 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" \
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", "$14", "$15", "$24", "$31", \
819 DADDI_SCRATCH, "memory"); \
823 #define __invoke_copy_to(func, to, from, n) \
825 register void __user *__cu_to_r __asm__("$4"); \
826 register const void *__cu_from_r __asm__("$5"); \
827 register long __cu_len_r __asm__("$6"); \
830 __cu_from_r = (from); \
832 __asm__ __volatile__( \
834 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
836 : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
837 DADDI_SCRATCH, "memory"); \
841 #define __invoke_copy_from_kernel(to, from, n) \
842 __invoke_copy_from(__copy_user, to, from, n)
844 #define __invoke_copy_from_kernel_inatomic(to, from, n) \
845 __invoke_copy_from(__copy_user_inatomic, to, from, n)
847 #define __invoke_copy_to_kernel(to, from, n) \
848 __invoke_copy_to(__copy_user, to, from, n)
850 #define ___invoke_copy_in_kernel(to, from, n) \
851 __invoke_copy_from(__copy_user, to, from, n)
854 #define __invoke_copy_from_user(to, from, n) \
855 __invoke_copy_from(__copy_user, to, from, n)
857 #define __invoke_copy_from_user_inatomic(to, from, n) \
858 __invoke_copy_from(__copy_user_inatomic, to, from, n)
860 #define __invoke_copy_to_user(to, from, n) \
861 __invoke_copy_to(__copy_user, to, from, n)
863 #define ___invoke_copy_in_user(to, from, n) \
864 __invoke_copy_from(__copy_user, to, from, n)
868 /* EVA specific functions */
870 extern size_t __copy_user_inatomic_eva(void *__to, const void *__from,
872 extern size_t __copy_from_user_eva(void *__to, const void *__from,
874 extern size_t __copy_to_user_eva(void *__to, const void *__from,
876 extern size_t __copy_in_user_eva(void *__to, const void *__from, size_t __n);
879 * Source or destination address is in userland. We need to go through
882 #define __invoke_copy_from_user(to, from, n) \
883 __invoke_copy_from(__copy_from_user_eva, to, from, n)
885 #define __invoke_copy_from_user_inatomic(to, from, n) \
886 __invoke_copy_from(__copy_user_inatomic_eva, to, from, n)
888 #define __invoke_copy_to_user(to, from, n) \
889 __invoke_copy_to(__copy_to_user_eva, to, from, n)
891 #define ___invoke_copy_in_user(to, from, n) \
892 __invoke_copy_from(__copy_in_user_eva, to, from, n)
894 #endif /* CONFIG_EVA */
897 * __copy_to_user: - Copy a block of data into user space, with less checking.
898 * @to: Destination address, in user space.
899 * @from: Source address, in kernel space.
900 * @n: Number of bytes to copy.
902 * Context: User context only. This function may sleep if pagefaults are
905 * Copy data from kernel space to user space. Caller must check
906 * the specified block with access_ok() before calling this function.
908 * Returns number of bytes that could not be copied.
909 * On success, this will be zero.
911 #define __copy_to_user(to, from, n) \
913 void __user *__cu_to; \
914 const void *__cu_from; \
918 __cu_from = (from); \
921 check_object_size(__cu_from, __cu_len, true); \
924 if (eva_kernel_access()) \
925 __cu_len = __invoke_copy_to_kernel(__cu_to, __cu_from, \
928 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, \
933 extern size_t __copy_user_inatomic(void *__to, const void *__from, size_t __n);
935 #define __copy_to_user_inatomic(to, from, n) \
937 void __user *__cu_to; \
938 const void *__cu_from; \
942 __cu_from = (from); \
945 check_object_size(__cu_from, __cu_len, true); \
947 if (eva_kernel_access()) \
948 __cu_len = __invoke_copy_to_kernel(__cu_to, __cu_from, \
951 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, \
956 #define __copy_from_user_inatomic(to, from, n) \
959 const void __user *__cu_from; \
963 __cu_from = (from); \
966 check_object_size(__cu_to, __cu_len, false); \
968 if (eva_kernel_access()) \
969 __cu_len = __invoke_copy_from_kernel_inatomic(__cu_to, \
973 __cu_len = __invoke_copy_from_user_inatomic(__cu_to, \
980 * copy_to_user: - Copy a block of data into user space.
981 * @to: Destination address, in user space.
982 * @from: Source address, in kernel space.
983 * @n: Number of bytes to copy.
985 * Context: User context only. This function may sleep if pagefaults are
988 * Copy data from kernel space to user space.
990 * Returns number of bytes that could not be copied.
991 * On success, this will be zero.
993 #define copy_to_user(to, from, n) \
995 void __user *__cu_to; \
996 const void *__cu_from; \
1000 __cu_from = (from); \
1003 check_object_size(__cu_from, __cu_len, true); \
1005 if (eva_kernel_access()) { \
1006 __cu_len = __invoke_copy_to_kernel(__cu_to, \
1010 if (access_ok(VERIFY_WRITE, __cu_to, __cu_len)) { \
1012 __cu_len = __invoke_copy_to_user(__cu_to, \
1021 * __copy_from_user: - Copy a block of data from user space, with less checking.
1022 * @to: Destination address, in kernel space.
1023 * @from: Source address, in user space.
1024 * @n: Number of bytes to copy.
1026 * Context: User context only. This function may sleep if pagefaults are
1029 * Copy data from user space to kernel space. Caller must check
1030 * the specified block with access_ok() before calling this function.
1032 * Returns number of bytes that could not be copied.
1033 * On success, this will be zero.
1035 * If some data could not be copied, this function will pad the copied
1036 * data to the requested size using zero bytes.
1038 #define __copy_from_user(to, from, n) \
1041 const void __user *__cu_from; \
1045 __cu_from = (from); \
1048 check_object_size(__cu_to, __cu_len, false); \
1050 if (eva_kernel_access()) { \
1051 __cu_len = __invoke_copy_from_kernel(__cu_to, \
1056 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
1063 * copy_from_user: - Copy a block of data from user space.
1064 * @to: Destination address, in kernel space.
1065 * @from: Source address, in user space.
1066 * @n: Number of bytes to copy.
1068 * Context: User context only. This function may sleep if pagefaults are
1071 * Copy data from user space to kernel space.
1073 * Returns number of bytes that could not be copied.
1074 * On success, this will be zero.
1076 * If some data could not be copied, this function will pad the copied
1077 * data to the requested size using zero bytes.
1079 #define copy_from_user(to, from, n) \
1082 const void __user *__cu_from; \
1083 long __cu_len, __cu_res; \
1086 __cu_from = (from); \
1087 __cu_res = __cu_len = (n); \
1089 check_object_size(__cu_to, __cu_len, false); \
1091 if (eva_kernel_access()) { \
1092 __cu_res = __invoke_copy_from_kernel(__cu_to, \
1096 if (access_ok(VERIFY_READ, __cu_from, __cu_len)) { \
1098 __cu_res = __invoke_copy_from_user(__cu_to, \
1103 if (unlikely(__cu_res)) \
1104 memset(__cu_to + __cu_len - __cu_res, 0, __cu_res); \
1108 #define __copy_in_user(to, from, n) \
1110 void __user *__cu_to; \
1111 const void __user *__cu_from; \
1115 __cu_from = (from); \
1117 if (eva_kernel_access()) { \
1118 __cu_len = ___invoke_copy_in_kernel(__cu_to, __cu_from, \
1122 __cu_len = ___invoke_copy_in_user(__cu_to, __cu_from, \
1128 #define copy_in_user(to, from, n) \
1130 void __user *__cu_to; \
1131 const void __user *__cu_from; \
1135 __cu_from = (from); \
1137 if (eva_kernel_access()) { \
1138 __cu_len = ___invoke_copy_in_kernel(__cu_to,__cu_from, \
1141 if (likely(access_ok(VERIFY_READ, __cu_from, __cu_len) &&\
1142 access_ok(VERIFY_WRITE, __cu_to, __cu_len))) {\
1144 __cu_len = ___invoke_copy_in_user(__cu_to, \
1152 extern __kernel_size_t __bzero_kernel(void __user *addr, __kernel_size_t size);
1153 extern __kernel_size_t __bzero(void __user *addr, __kernel_size_t size);
1156 * __clear_user: - Zero a block of memory in user space, with less checking.
1157 * @to: Destination address, in user space.
1158 * @n: Number of bytes to zero.
1160 * Zero a block of memory in user space. Caller must check
1161 * the specified block with access_ok() before calling this function.
1163 * Returns number of bytes that could not be cleared.
1164 * On success, this will be zero.
1166 static inline __kernel_size_t
1167 __clear_user(void __user *addr, __kernel_size_t size)
1169 __kernel_size_t res;
1171 if (eva_kernel_access()) {
1172 __asm__ __volatile__(
1176 __MODULE_JAL(__bzero_kernel)
1179 : "r" (addr), "r" (size)
1180 : "$4", "$5", "$6", __UA_t0, __UA_t1, "$31");
1183 __asm__ __volatile__(
1187 __MODULE_JAL(__bzero)
1190 : "r" (addr), "r" (size)
1191 : "$4", "$5", "$6", __UA_t0, __UA_t1, "$31");
1197 #define clear_user(addr,n) \
1199 void __user * __cl_addr = (addr); \
1200 unsigned long __cl_size = (n); \
1201 if (__cl_size && access_ok(VERIFY_WRITE, \
1202 __cl_addr, __cl_size)) \
1203 __cl_size = __clear_user(__cl_addr, __cl_size); \
1207 extern long __strncpy_from_kernel_nocheck_asm(char *__to, const char __user *__from, long __len);
1208 extern long __strncpy_from_user_nocheck_asm(char *__to, const char __user *__from, long __len);
1211 * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
1212 * @dst: Destination address, in kernel space. This buffer must be at
1213 * least @count bytes long.
1214 * @src: Source address, in user space.
1215 * @count: Maximum number of bytes to copy, including the trailing NUL.
1217 * Copies a NUL-terminated string from userspace to kernel space.
1218 * Caller must check the specified block with access_ok() before calling
1221 * On success, returns the length of the string (not including the trailing
1224 * If access to userspace fails, returns -EFAULT (some data may have been
1227 * If @count is smaller than the length of the string, copies @count bytes
1228 * and returns @count.
1231 __strncpy_from_user(char *__to, const char __user *__from, long __len)
1235 if (eva_kernel_access()) {
1236 __asm__ __volatile__(
1240 __MODULE_JAL(__strncpy_from_kernel_nocheck_asm)
1243 : "r" (__to), "r" (__from), "r" (__len)
1244 : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
1247 __asm__ __volatile__(
1251 __MODULE_JAL(__strncpy_from_user_nocheck_asm)
1254 : "r" (__to), "r" (__from), "r" (__len)
1255 : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
1261 extern long __strncpy_from_kernel_asm(char *__to, const char __user *__from, long __len);
1262 extern long __strncpy_from_user_asm(char *__to, const char __user *__from, long __len);
1265 * strncpy_from_user: - Copy a NUL terminated string from userspace.
1266 * @dst: Destination address, in kernel space. This buffer must be at
1267 * least @count bytes long.
1268 * @src: Source address, in user space.
1269 * @count: Maximum number of bytes to copy, including the trailing NUL.
1271 * Copies a NUL-terminated string from userspace to kernel space.
1273 * On success, returns the length of the string (not including the trailing
1276 * If access to userspace fails, returns -EFAULT (some data may have been
1279 * If @count is smaller than the length of the string, copies @count bytes
1280 * and returns @count.
1283 strncpy_from_user(char *__to, const char __user *__from, long __len)
1287 if (eva_kernel_access()) {
1288 __asm__ __volatile__(
1292 __MODULE_JAL(__strncpy_from_kernel_asm)
1295 : "r" (__to), "r" (__from), "r" (__len)
1296 : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
1299 __asm__ __volatile__(
1303 __MODULE_JAL(__strncpy_from_user_asm)
1306 : "r" (__to), "r" (__from), "r" (__len)
1307 : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
1313 extern long __strlen_kernel_asm(const char __user *s);
1314 extern long __strlen_user_asm(const char __user *s);
1317 * strlen_user: - Get the size of a string in user space.
1318 * @str: The string to measure.
1320 * Context: User context only. This function may sleep if pagefaults are
1323 * Get the size of a NUL-terminated string in user space.
1325 * Returns the size of the string INCLUDING the terminating NUL.
1326 * On exception, returns 0.
1328 * If there is a limit on the length of a valid string, you may wish to
1329 * consider using strnlen_user() instead.
1331 static inline long strlen_user(const char __user *s)
1335 if (eva_kernel_access()) {
1336 __asm__ __volatile__(
1338 __MODULE_JAL(__strlen_kernel_asm)
1342 : "$2", "$4", __UA_t0, "$31");
1345 __asm__ __volatile__(
1347 __MODULE_JAL(__strlen_user_asm)
1351 : "$2", "$4", __UA_t0, "$31");
1357 extern long __strnlen_kernel_nocheck_asm(const char __user *s, long n);
1358 extern long __strnlen_user_nocheck_asm(const char __user *s, long n);
1360 /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
1361 static inline long __strnlen_user(const char __user *s, long n)
1365 if (eva_kernel_access()) {
1366 __asm__ __volatile__(
1369 __MODULE_JAL(__strnlen_kernel_nocheck_asm)
1373 : "$2", "$4", "$5", __UA_t0, "$31");
1376 __asm__ __volatile__(
1379 __MODULE_JAL(__strnlen_user_nocheck_asm)
1383 : "$2", "$4", "$5", __UA_t0, "$31");
1389 extern long __strnlen_kernel_asm(const char __user *s, long n);
1390 extern long __strnlen_user_asm(const char __user *s, long n);
1393 * strnlen_user: - Get the size of a string in user space.
1394 * @str: The string to measure.
1396 * Context: User context only. This function may sleep if pagefaults are
1399 * Get the size of a NUL-terminated string in user space.
1401 * Returns the size of the string INCLUDING the terminating NUL.
1402 * On exception, returns 0.
1403 * If the string is too long, returns a value greater than @n.
1405 static inline long strnlen_user(const char __user *s, long n)
1410 if (eva_kernel_access()) {
1411 __asm__ __volatile__(
1414 __MODULE_JAL(__strnlen_kernel_asm)
1418 : "$2", "$4", "$5", __UA_t0, "$31");
1420 __asm__ __volatile__(
1423 __MODULE_JAL(__strnlen_user_asm)
1427 : "$2", "$4", "$5", __UA_t0, "$31");
1433 #endif /* _ASM_UACCESS_H */