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 <linux/string.h>
18 #include <asm/asm-eva.h>
21 * The fs value determines whether argument validity checking should be
22 * performed or not. If get_fs() == USER_DS, checking is performed, with
23 * get_fs() == KERNEL_DS, checking is bypassed.
25 * For historical reasons, these macros are grossly misnamed.
29 #ifdef CONFIG_KVM_GUEST
30 #define __UA_LIMIT 0x40000000UL
32 #define __UA_LIMIT 0x80000000UL
35 #define __UA_ADDR ".word"
37 #define __UA_ADDU "addu"
41 #endif /* CONFIG_32BIT */
45 extern u64 __ua_limit
;
47 #define __UA_LIMIT __ua_limit
49 #define __UA_ADDR ".dword"
51 #define __UA_ADDU "daddu"
55 #endif /* CONFIG_64BIT */
58 * USER_DS is a bitmask that has the bits set that may not be set in a valid
59 * userspace address. Note that we limit 32-bit userspace to 0x7fff8000 but
60 * the arithmetic we're doing only works if the limit is a power of two, so
61 * we use 0x80000000 here on 32-bit kernels. If a process passes an invalid
62 * address in this range it's the process's problem, not ours :-)
65 #ifdef CONFIG_KVM_GUEST
66 #define KERNEL_DS ((mm_segment_t) { 0x80000000UL })
67 #define USER_DS ((mm_segment_t) { 0xC0000000UL })
69 #define KERNEL_DS ((mm_segment_t) { 0UL })
70 #define USER_DS ((mm_segment_t) { __UA_LIMIT })
74 #define VERIFY_WRITE 1
76 #define get_ds() (KERNEL_DS)
77 #define get_fs() (current_thread_info()->addr_limit)
78 #define set_fs(x) (current_thread_info()->addr_limit = (x))
80 #define segment_eq(a, b) ((a).seg == (b).seg)
83 * eva_kernel_access() - determine whether kernel memory access on an EVA system
85 * Determines whether memory accesses should be performed to kernel memory
86 * on a system using Extended Virtual Addressing (EVA).
88 * Return: true if a kernel memory access on an EVA system, else false.
90 static inline bool eva_kernel_access(void)
92 if (!IS_ENABLED(CONFIG_EVA
))
95 return segment_eq(get_fs(), get_ds());
99 * Is a address valid? This does a straightforward calculation rather
103 * - "addr" doesn't have any high-bits set
104 * - AND "size" doesn't have any high-bits set
105 * - AND "addr+size" doesn't have any high-bits set
106 * - OR we are in kernel mode.
108 * __ua_size() is a trick to avoid runtime checking of positive constant
109 * sizes; for those we already know at compile time that the size is ok.
111 #define __ua_size(size) \
112 ((__builtin_constant_p(size) && (signed long) (size) > 0) ? 0 : (size))
115 * access_ok: - Checks if a user space pointer is valid
116 * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
117 * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
118 * to write to a block, it is always safe to read from it.
119 * @addr: User space pointer to start of block to check
120 * @size: Size of block to check
122 * Context: User context only. This function may sleep if pagefaults are
125 * Checks if a pointer to a block of memory in user space is valid.
127 * Returns true (nonzero) if the memory block may be valid, false (zero)
128 * if it is definitely invalid.
130 * Note that, depending on architecture, this function probably just
131 * checks that the pointer is in the user space range - after calling
132 * this function, memory access functions may still return -EFAULT.
135 #define __access_mask get_fs().seg
137 #define __access_ok(addr, size, mask) \
139 unsigned long __addr = (unsigned long) (addr); \
140 unsigned long __size = size; \
141 unsigned long __mask = mask; \
142 unsigned long __ok; \
144 __chk_user_ptr(addr); \
145 __ok = (signed long)(__mask & (__addr | (__addr + __size) | \
146 __ua_size(__size))); \
150 #define access_ok(type, addr, size) \
151 likely(__access_ok((addr), (size), __access_mask))
154 * put_user: - Write a simple value into user space.
155 * @x: Value to copy to user space.
156 * @ptr: Destination address, in user space.
158 * Context: User context only. This function may sleep if pagefaults are
161 * This macro copies a single simple value from kernel space to user
162 * space. It supports simple types like char and int, but not larger
163 * data types like structures or arrays.
165 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
166 * to the result of dereferencing @ptr.
168 * Returns zero on success, or -EFAULT on error.
170 #define put_user(x,ptr) \
171 __put_user_check((x), (ptr), sizeof(*(ptr)))
174 * get_user: - Get a simple variable from user space.
175 * @x: Variable to store result.
176 * @ptr: Source address, in user space.
178 * Context: User context only. This function may sleep if pagefaults are
181 * This macro copies a single simple variable from user space to kernel
182 * space. It supports simple types like char and int, but not larger
183 * data types like structures or arrays.
185 * @ptr must have pointer-to-simple-variable type, and the result of
186 * dereferencing @ptr must be assignable to @x without a cast.
188 * Returns zero on success, or -EFAULT on error.
189 * On error, the variable @x is set to zero.
191 #define get_user(x,ptr) \
192 __get_user_check((x), (ptr), sizeof(*(ptr)))
195 * __put_user: - Write a simple value into user space, with less checking.
196 * @x: Value to copy to user space.
197 * @ptr: Destination address, in user space.
199 * Context: User context only. This function may sleep if pagefaults are
202 * This macro copies a single simple value from kernel space to user
203 * space. It supports simple types like char and int, but not larger
204 * data types like structures or arrays.
206 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
207 * to the result of dereferencing @ptr.
209 * Caller must check the pointer with access_ok() before calling this
212 * Returns zero on success, or -EFAULT on error.
214 #define __put_user(x,ptr) \
215 __put_user_nocheck((x), (ptr), sizeof(*(ptr)))
218 * __get_user: - Get a simple variable from user space, with less checking.
219 * @x: Variable to store result.
220 * @ptr: Source address, in user space.
222 * Context: User context only. This function may sleep if pagefaults are
225 * This macro copies a single simple variable from user space to kernel
226 * space. It supports simple types like char and int, but not larger
227 * data types like structures or arrays.
229 * @ptr must have pointer-to-simple-variable type, and the result of
230 * dereferencing @ptr must be assignable to @x without a cast.
232 * Caller must check the pointer with access_ok() before calling this
235 * Returns zero on success, or -EFAULT on error.
236 * On error, the variable @x is set to zero.
238 #define __get_user(x,ptr) \
239 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
241 struct __large_struct
{ unsigned long buf
[100]; };
242 #define __m(x) (*(struct __large_struct __user *)(x))
245 * Yuck. We need two variants, one for 64bit operation and one
246 * for 32 bit mode and old iron.
249 #define __get_kernel_common(val, size, ptr) __get_user_common(val, size, ptr)
252 * Kernel specific functions for EVA. We need to use normal load instructions
253 * to read data from kernel when operating in EVA mode. We use these macros to
254 * avoid redefining __get_user_asm for EVA.
261 #define _loadd _loadw
263 #define _loadd(reg, addr) "ld " reg ", " addr
265 #define _loadw(reg, addr) "lw " reg ", " addr
266 #define _loadh(reg, addr) "lh " reg ", " addr
267 #define _loadb(reg, addr) "lb " reg ", " addr
269 #define __get_kernel_common(val, size, ptr) \
272 case 1: __get_data_asm(val, _loadb, ptr); break; \
273 case 2: __get_data_asm(val, _loadh, ptr); break; \
274 case 4: __get_data_asm(val, _loadw, ptr); break; \
275 case 8: __GET_DW(val, _loadd, ptr); break; \
276 default: __get_user_unknown(); break; \
282 #define __GET_DW(val, insn, ptr) __get_data_asm_ll32(val, insn, ptr)
285 #define __GET_DW(val, insn, ptr) __get_data_asm(val, insn, ptr)
288 extern void __get_user_unknown(void);
290 #define __get_user_common(val, size, ptr) \
293 case 1: __get_data_asm(val, user_lb, ptr); break; \
294 case 2: __get_data_asm(val, user_lh, ptr); break; \
295 case 4: __get_data_asm(val, user_lw, ptr); break; \
296 case 8: __GET_DW(val, user_ld, ptr); break; \
297 default: __get_user_unknown(); break; \
301 #define __get_user_nocheck(x, ptr, size) \
305 if (eva_kernel_access()) { \
306 __get_kernel_common((x), size, ptr); \
308 __chk_user_ptr(ptr); \
309 __get_user_common((x), size, ptr); \
314 #define __get_user_check(x, ptr, size) \
316 int __gu_err = -EFAULT; \
317 const __typeof__(*(ptr)) __user * __gu_ptr = (ptr); \
320 if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) { \
321 if (eva_kernel_access()) \
322 __get_kernel_common((x), size, __gu_ptr); \
324 __get_user_common((x), size, __gu_ptr); \
331 #define __get_data_asm(val, insn, addr) \
335 __asm__ __volatile__( \
336 "1: "insn("%1", "%3")" \n" \
339 " .section .fixup,\"ax\" \n" \
344 " .section __ex_table,\"a\" \n" \
345 " "__UA_ADDR "\t1b, 3b \n" \
347 : "=r" (__gu_err), "=r" (__gu_tmp) \
348 : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \
350 (val) = (__typeof__(*(addr))) __gu_tmp; \
354 * Get a long long 64 using 32 bit registers.
356 #define __get_data_asm_ll32(val, insn, addr) \
359 unsigned long long l; \
360 __typeof__(*(addr)) t; \
363 __asm__ __volatile__( \
364 "1: " insn("%1", "(%3)")" \n" \
365 "2: " insn("%D1", "4(%3)")" \n" \
368 " .section .fixup,\"ax\" \n" \
374 " .section __ex_table,\"a\" \n" \
375 " " __UA_ADDR " 1b, 4b \n" \
376 " " __UA_ADDR " 2b, 4b \n" \
378 : "=r" (__gu_err), "=&r" (__gu_tmp.l) \
379 : "0" (0), "r" (addr), "i" (-EFAULT)); \
381 (val) = __gu_tmp.t; \
385 #define __put_kernel_common(ptr, size) __put_user_common(ptr, size)
388 * Kernel specific functions for EVA. We need to use normal load instructions
389 * to read data from kernel when operating in EVA mode. We use these macros to
390 * avoid redefining __get_data_asm for EVA.
397 #define _stored _storew
399 #define _stored(reg, addr) "ld " reg ", " addr
402 #define _storew(reg, addr) "sw " reg ", " addr
403 #define _storeh(reg, addr) "sh " reg ", " addr
404 #define _storeb(reg, addr) "sb " reg ", " addr
406 #define __put_kernel_common(ptr, size) \
409 case 1: __put_data_asm(_storeb, ptr); break; \
410 case 2: __put_data_asm(_storeh, ptr); break; \
411 case 4: __put_data_asm(_storew, ptr); break; \
412 case 8: __PUT_DW(_stored, ptr); break; \
413 default: __put_user_unknown(); break; \
419 * Yuck. We need two variants, one for 64bit operation and one
420 * for 32 bit mode and old iron.
423 #define __PUT_DW(insn, ptr) __put_data_asm_ll32(insn, ptr)
426 #define __PUT_DW(insn, ptr) __put_data_asm(insn, ptr)
429 #define __put_user_common(ptr, size) \
432 case 1: __put_data_asm(user_sb, ptr); break; \
433 case 2: __put_data_asm(user_sh, ptr); break; \
434 case 4: __put_data_asm(user_sw, ptr); break; \
435 case 8: __PUT_DW(user_sd, ptr); break; \
436 default: __put_user_unknown(); break; \
440 #define __put_user_nocheck(x, ptr, size) \
442 __typeof__(*(ptr)) __pu_val; \
446 if (eva_kernel_access()) { \
447 __put_kernel_common(ptr, size); \
449 __chk_user_ptr(ptr); \
450 __put_user_common(ptr, size); \
455 #define __put_user_check(x, ptr, size) \
457 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
458 __typeof__(*(ptr)) __pu_val = (x); \
459 int __pu_err = -EFAULT; \
462 if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) { \
463 if (eva_kernel_access()) \
464 __put_kernel_common(__pu_addr, size); \
466 __put_user_common(__pu_addr, size); \
472 #define __put_data_asm(insn, ptr) \
474 __asm__ __volatile__( \
475 "1: "insn("%z2", "%3")" # __put_data_asm \n" \
478 " .section .fixup,\"ax\" \n" \
482 " .section __ex_table,\"a\" \n" \
483 " " __UA_ADDR " 1b, 3b \n" \
486 : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)), \
490 #define __put_data_asm_ll32(insn, ptr) \
492 __asm__ __volatile__( \
493 "1: "insn("%2", "(%3)")" # __put_data_asm_ll32 \n" \
494 "2: "insn("%D2", "4(%3)")" \n" \
497 " .section .fixup,\"ax\" \n" \
501 " .section __ex_table,\"a\" \n" \
502 " " __UA_ADDR " 1b, 4b \n" \
503 " " __UA_ADDR " 2b, 4b \n" \
506 : "0" (0), "r" (__pu_val), "r" (ptr), \
510 extern void __put_user_unknown(void);
513 * ul{b,h,w} are macros and there are no equivalent macros for EVA.
514 * EVA unaligned access is handled in the ADE exception handler.
518 * put_user_unaligned: - Write a simple value into user space.
519 * @x: Value to copy to user space.
520 * @ptr: Destination address, in user space.
522 * Context: User context only. This function may sleep if pagefaults are
525 * This macro copies a single simple value from kernel space to user
526 * space. It supports simple types like char and int, but not larger
527 * data types like structures or arrays.
529 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
530 * to the result of dereferencing @ptr.
532 * Returns zero on success, or -EFAULT on error.
534 #define put_user_unaligned(x,ptr) \
535 __put_user_unaligned_check((x),(ptr),sizeof(*(ptr)))
538 * get_user_unaligned: - Get a simple variable from user space.
539 * @x: Variable to store result.
540 * @ptr: Source address, in user space.
542 * Context: User context only. This function may sleep if pagefaults are
545 * This macro copies a single simple variable from user space to kernel
546 * space. It supports simple types like char and int, but not larger
547 * data types like structures or arrays.
549 * @ptr must have pointer-to-simple-variable type, and the result of
550 * dereferencing @ptr must be assignable to @x without a cast.
552 * Returns zero on success, or -EFAULT on error.
553 * On error, the variable @x is set to zero.
555 #define get_user_unaligned(x,ptr) \
556 __get_user_unaligned_check((x),(ptr),sizeof(*(ptr)))
559 * __put_user_unaligned: - Write a simple value into user space, with less checking.
560 * @x: Value to copy to user space.
561 * @ptr: Destination address, in user space.
563 * Context: User context only. This function may sleep if pagefaults are
566 * This macro copies a single simple value from kernel space to user
567 * space. It supports simple types like char and int, but not larger
568 * data types like structures or arrays.
570 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
571 * to the result of dereferencing @ptr.
573 * Caller must check the pointer with access_ok() before calling this
576 * Returns zero on success, or -EFAULT on error.
578 #define __put_user_unaligned(x,ptr) \
579 __put_user_unaligned_nocheck((x),(ptr),sizeof(*(ptr)))
582 * __get_user_unaligned: - Get a simple variable from user space, with less checking.
583 * @x: Variable to store result.
584 * @ptr: Source address, in user space.
586 * Context: User context only. This function may sleep if pagefaults are
589 * This macro copies a single simple variable from user space to kernel
590 * space. It supports simple types like char and int, but not larger
591 * data types like structures or arrays.
593 * @ptr must have pointer-to-simple-variable type, and the result of
594 * dereferencing @ptr must be assignable to @x without a cast.
596 * Caller must check the pointer with access_ok() before calling this
599 * Returns zero on success, or -EFAULT on error.
600 * On error, the variable @x is set to zero.
602 #define __get_user_unaligned(x,ptr) \
603 __get_user_unaligned_nocheck((x),(ptr),sizeof(*(ptr)))
606 * Yuck. We need two variants, one for 64bit operation and one
607 * for 32 bit mode and old iron.
610 #define __GET_USER_UNALIGNED_DW(val, ptr) \
611 __get_user_unaligned_asm_ll32(val, ptr)
614 #define __GET_USER_UNALIGNED_DW(val, ptr) \
615 __get_user_unaligned_asm(val, "uld", ptr)
618 extern void __get_user_unaligned_unknown(void);
620 #define __get_user_unaligned_common(val, size, ptr) \
623 case 1: __get_data_asm(val, "lb", ptr); break; \
624 case 2: __get_data_unaligned_asm(val, "ulh", ptr); break; \
625 case 4: __get_data_unaligned_asm(val, "ulw", ptr); break; \
626 case 8: __GET_USER_UNALIGNED_DW(val, ptr); break; \
627 default: __get_user_unaligned_unknown(); break; \
631 #define __get_user_unaligned_nocheck(x,ptr,size) \
635 __get_user_unaligned_common((x), size, ptr); \
639 #define __get_user_unaligned_check(x,ptr,size) \
641 int __gu_err = -EFAULT; \
642 const __typeof__(*(ptr)) __user * __gu_ptr = (ptr); \
644 if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) \
645 __get_user_unaligned_common((x), size, __gu_ptr); \
650 #define __get_data_unaligned_asm(val, insn, addr) \
654 __asm__ __volatile__( \
655 "1: " insn " %1, %3 \n" \
658 " .section .fixup,\"ax\" \n" \
663 " .section __ex_table,\"a\" \n" \
664 " "__UA_ADDR "\t1b, 3b \n" \
665 " "__UA_ADDR "\t1b + 4, 3b \n" \
667 : "=r" (__gu_err), "=r" (__gu_tmp) \
668 : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \
670 (val) = (__typeof__(*(addr))) __gu_tmp; \
674 * Get a long long 64 using 32 bit registers.
676 #define __get_user_unaligned_asm_ll32(val, addr) \
678 unsigned long long __gu_tmp; \
680 __asm__ __volatile__( \
681 "1: ulw %1, (%3) \n" \
682 "2: ulw %D1, 4(%3) \n" \
686 " .section .fixup,\"ax\" \n" \
692 " .section __ex_table,\"a\" \n" \
693 " " __UA_ADDR " 1b, 4b \n" \
694 " " __UA_ADDR " 1b + 4, 4b \n" \
695 " " __UA_ADDR " 2b, 4b \n" \
696 " " __UA_ADDR " 2b + 4, 4b \n" \
698 : "=r" (__gu_err), "=&r" (__gu_tmp) \
699 : "0" (0), "r" (addr), "i" (-EFAULT)); \
700 (val) = (__typeof__(*(addr))) __gu_tmp; \
704 * Yuck. We need two variants, one for 64bit operation and one
705 * for 32 bit mode and old iron.
708 #define __PUT_USER_UNALIGNED_DW(ptr) __put_user_unaligned_asm_ll32(ptr)
711 #define __PUT_USER_UNALIGNED_DW(ptr) __put_user_unaligned_asm("usd", ptr)
714 #define __put_user_unaligned_common(ptr, size) \
717 case 1: __put_data_asm("sb", ptr); break; \
718 case 2: __put_user_unaligned_asm("ush", ptr); break; \
719 case 4: __put_user_unaligned_asm("usw", ptr); break; \
720 case 8: __PUT_USER_UNALIGNED_DW(ptr); break; \
721 default: __put_user_unaligned_unknown(); break; \
724 #define __put_user_unaligned_nocheck(x,ptr,size) \
726 __typeof__(*(ptr)) __pu_val; \
730 __put_user_unaligned_common(ptr, size); \
734 #define __put_user_unaligned_check(x,ptr,size) \
736 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
737 __typeof__(*(ptr)) __pu_val = (x); \
738 int __pu_err = -EFAULT; \
740 if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) \
741 __put_user_unaligned_common(__pu_addr, size); \
746 #define __put_user_unaligned_asm(insn, ptr) \
748 __asm__ __volatile__( \
749 "1: " insn " %z2, %3 # __put_user_unaligned_asm\n" \
752 " .section .fixup,\"ax\" \n" \
756 " .section __ex_table,\"a\" \n" \
757 " " __UA_ADDR " 1b, 3b \n" \
760 : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)), \
764 #define __put_user_unaligned_asm_ll32(ptr) \
766 __asm__ __volatile__( \
767 "1: sw %2, (%3) # __put_user_unaligned_asm_ll32 \n" \
768 "2: sw %D2, 4(%3) \n" \
771 " .section .fixup,\"ax\" \n" \
775 " .section __ex_table,\"a\" \n" \
776 " " __UA_ADDR " 1b, 4b \n" \
777 " " __UA_ADDR " 1b + 4, 4b \n" \
778 " " __UA_ADDR " 2b, 4b \n" \
779 " " __UA_ADDR " 2b + 4, 4b \n" \
782 : "0" (0), "r" (__pu_val), "r" (ptr), \
786 extern void __put_user_unaligned_unknown(void);
790 * We're generating jump to subroutines which will be outside the range of
794 #define __MODULE_JAL(destination) \
796 __UA_LA "\t$1, " #destination "\n\t" \
800 #define __MODULE_JAL(destination) \
801 "jal\t" #destination "\n\t"
804 #if defined(CONFIG_CPU_DADDI_WORKAROUNDS) || (defined(CONFIG_EVA) && \
805 defined(CONFIG_CPU_HAS_PREFETCH))
806 #define DADDI_SCRATCH "$3"
808 #define DADDI_SCRATCH "$0"
811 extern size_t __copy_user(void *__to
, const void *__from
, size_t __n
);
814 #define __invoke_copy_to_user(to, from, n) \
816 register void __user *__cu_to_r __asm__("$4"); \
817 register const void *__cu_from_r __asm__("$5"); \
818 register long __cu_len_r __asm__("$6"); \
821 __cu_from_r = (from); \
823 __asm__ __volatile__( \
824 __MODULE_JAL(__copy_user) \
825 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
827 : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
828 DADDI_SCRATCH, "memory"); \
832 #define __invoke_copy_to_kernel(to, from, n) \
833 __invoke_copy_to_user(to, from, n)
838 * __copy_to_user: - Copy a block of data into user space, with less checking.
839 * @to: Destination address, in user space.
840 * @from: Source address, in kernel space.
841 * @n: Number of bytes to copy.
843 * Context: User context only. This function may sleep if pagefaults are
846 * Copy data from kernel space to user 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 #define __copy_to_user(to, from, n) \
854 void __user *__cu_to; \
855 const void *__cu_from; \
859 __cu_from = (from); \
862 if (eva_kernel_access()) \
863 __cu_len = __invoke_copy_to_kernel(__cu_to, __cu_from, \
866 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, \
871 extern size_t __copy_user_inatomic(void *__to
, const void *__from
, size_t __n
);
873 #define __copy_to_user_inatomic(to, from, n) \
875 void __user *__cu_to; \
876 const void *__cu_from; \
880 __cu_from = (from); \
882 if (eva_kernel_access()) \
883 __cu_len = __invoke_copy_to_kernel(__cu_to, __cu_from, \
886 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, \
891 #define __copy_from_user_inatomic(to, from, n) \
894 const void __user *__cu_from; \
898 __cu_from = (from); \
900 if (eva_kernel_access()) \
901 __cu_len = __invoke_copy_from_kernel_inatomic(__cu_to, \
905 __cu_len = __invoke_copy_from_user_inatomic(__cu_to, \
912 * copy_to_user: - Copy a block of data into user space.
913 * @to: Destination address, in user space.
914 * @from: Source address, in kernel space.
915 * @n: Number of bytes to copy.
917 * Context: User context only. This function may sleep if pagefaults are
920 * Copy data from kernel space to user space.
922 * Returns number of bytes that could not be copied.
923 * On success, this will be zero.
925 #define copy_to_user(to, from, n) \
927 void __user *__cu_to; \
928 const void *__cu_from; \
932 __cu_from = (from); \
934 if (eva_kernel_access()) { \
935 __cu_len = __invoke_copy_to_kernel(__cu_to, \
939 if (access_ok(VERIFY_WRITE, __cu_to, __cu_len)) { \
941 __cu_len = __invoke_copy_to_user(__cu_to, \
951 #define __invoke_copy_from_user(to, from, n) \
953 register void *__cu_to_r __asm__("$4"); \
954 register const void __user *__cu_from_r __asm__("$5"); \
955 register long __cu_len_r __asm__("$6"); \
958 __cu_from_r = (from); \
960 __asm__ __volatile__( \
961 ".set\tnoreorder\n\t" \
962 __MODULE_JAL(__copy_user) \
964 __UA_ADDU "\t$1, %1, %2\n\t" \
967 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
969 : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
970 DADDI_SCRATCH, "memory"); \
974 #define __invoke_copy_from_kernel(to, from, n) \
975 __invoke_copy_from_user(to, from, n)
977 /* For userland <-> userland operations */
978 #define ___invoke_copy_in_user(to, from, n) \
979 __invoke_copy_from_user(to, from, n)
981 /* For kernel <-> kernel operations */
982 #define ___invoke_copy_in_kernel(to, from, n) \
983 __invoke_copy_from_user(to, from, n)
985 #define __invoke_copy_from_user_inatomic(to, from, n) \
987 register void *__cu_to_r __asm__("$4"); \
988 register const void __user *__cu_from_r __asm__("$5"); \
989 register long __cu_len_r __asm__("$6"); \
992 __cu_from_r = (from); \
994 __asm__ __volatile__( \
995 ".set\tnoreorder\n\t" \
996 __MODULE_JAL(__copy_user_inatomic) \
998 __UA_ADDU "\t$1, %1, %2\n\t" \
1001 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
1003 : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
1004 DADDI_SCRATCH, "memory"); \
1008 #define __invoke_copy_from_kernel_inatomic(to, from, n) \
1009 __invoke_copy_from_user_inatomic(to, from, n) \
1013 /* EVA specific functions */
1015 extern size_t __copy_user_inatomic_eva(void *__to
, const void *__from
,
1017 extern size_t __copy_from_user_eva(void *__to
, const void *__from
,
1019 extern size_t __copy_to_user_eva(void *__to
, const void *__from
,
1021 extern size_t __copy_in_user_eva(void *__to
, const void *__from
, size_t __n
);
1023 #define __invoke_copy_from_user_eva_generic(to, from, n, func_ptr) \
1025 register void *__cu_to_r __asm__("$4"); \
1026 register const void __user *__cu_from_r __asm__("$5"); \
1027 register long __cu_len_r __asm__("$6"); \
1030 __cu_from_r = (from); \
1032 __asm__ __volatile__( \
1033 ".set\tnoreorder\n\t" \
1034 __MODULE_JAL(func_ptr) \
1036 __UA_ADDU "\t$1, %1, %2\n\t" \
1039 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
1041 : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
1042 DADDI_SCRATCH, "memory"); \
1046 #define __invoke_copy_to_user_eva_generic(to, from, n, func_ptr) \
1048 register void *__cu_to_r __asm__("$4"); \
1049 register const void __user *__cu_from_r __asm__("$5"); \
1050 register long __cu_len_r __asm__("$6"); \
1053 __cu_from_r = (from); \
1055 __asm__ __volatile__( \
1056 __MODULE_JAL(func_ptr) \
1057 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
1059 : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
1060 DADDI_SCRATCH, "memory"); \
1065 * Source or destination address is in userland. We need to go through
1068 #define __invoke_copy_from_user(to, from, n) \
1069 __invoke_copy_from_user_eva_generic(to, from, n, __copy_from_user_eva)
1071 #define __invoke_copy_from_user_inatomic(to, from, n) \
1072 __invoke_copy_from_user_eva_generic(to, from, n, \
1073 __copy_user_inatomic_eva)
1075 #define __invoke_copy_to_user(to, from, n) \
1076 __invoke_copy_to_user_eva_generic(to, from, n, __copy_to_user_eva)
1078 #define ___invoke_copy_in_user(to, from, n) \
1079 __invoke_copy_from_user_eva_generic(to, from, n, __copy_in_user_eva)
1082 * Source or destination address in the kernel. We are not going through
1085 #define __invoke_copy_from_kernel(to, from, n) \
1086 __invoke_copy_from_user_eva_generic(to, from, n, __copy_user)
1088 #define __invoke_copy_from_kernel_inatomic(to, from, n) \
1089 __invoke_copy_from_user_eva_generic(to, from, n, __copy_user_inatomic)
1091 #define __invoke_copy_to_kernel(to, from, n) \
1092 __invoke_copy_to_user_eva_generic(to, from, n, __copy_user)
1094 #define ___invoke_copy_in_kernel(to, from, n) \
1095 __invoke_copy_from_user_eva_generic(to, from, n, __copy_user)
1097 #endif /* CONFIG_EVA */
1100 * __copy_from_user: - Copy a block of data from user space, with less checking.
1101 * @to: Destination address, in kernel space.
1102 * @from: Source address, in user space.
1103 * @n: Number of bytes to copy.
1105 * Context: User context only. This function may sleep if pagefaults are
1108 * Copy data from user space to kernel space. Caller must check
1109 * the specified block with access_ok() before calling this function.
1111 * Returns number of bytes that could not be copied.
1112 * On success, this will be zero.
1114 * If some data could not be copied, this function will pad the copied
1115 * data to the requested size using zero bytes.
1117 #define __copy_from_user(to, from, n) \
1120 const void __user *__cu_from; \
1124 __cu_from = (from); \
1126 if (eva_kernel_access()) { \
1127 __cu_len = __invoke_copy_from_kernel(__cu_to, \
1132 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
1139 * copy_from_user: - Copy a block of data from user space.
1140 * @to: Destination address, in kernel space.
1141 * @from: Source address, in user space.
1142 * @n: Number of bytes to copy.
1144 * Context: User context only. This function may sleep if pagefaults are
1147 * Copy data from user space to kernel space.
1149 * Returns number of bytes that could not be copied.
1150 * On success, this will be zero.
1152 * If some data could not be copied, this function will pad the copied
1153 * data to the requested size using zero bytes.
1155 #define copy_from_user(to, from, n) \
1158 const void __user *__cu_from; \
1162 __cu_from = (from); \
1164 if (eva_kernel_access()) { \
1165 __cu_len = __invoke_copy_from_kernel(__cu_to, \
1169 if (access_ok(VERIFY_READ, __cu_from, __cu_len)) { \
1171 __cu_len = __invoke_copy_from_user(__cu_to, \
1175 memset(__cu_to, 0, __cu_len); \
1181 #define __copy_in_user(to, from, n) \
1183 void __user *__cu_to; \
1184 const void __user *__cu_from; \
1188 __cu_from = (from); \
1190 if (eva_kernel_access()) { \
1191 __cu_len = ___invoke_copy_in_kernel(__cu_to, __cu_from, \
1195 __cu_len = ___invoke_copy_in_user(__cu_to, __cu_from, \
1201 #define copy_in_user(to, from, n) \
1203 void __user *__cu_to; \
1204 const void __user *__cu_from; \
1208 __cu_from = (from); \
1210 if (eva_kernel_access()) { \
1211 __cu_len = ___invoke_copy_in_kernel(__cu_to,__cu_from, \
1214 if (likely(access_ok(VERIFY_READ, __cu_from, __cu_len) &&\
1215 access_ok(VERIFY_WRITE, __cu_to, __cu_len))) {\
1217 __cu_len = ___invoke_copy_in_user(__cu_to, \
1226 * __clear_user: - Zero a block of memory in user space, with less checking.
1227 * @to: Destination address, in user space.
1228 * @n: Number of bytes to zero.
1230 * Zero a block of memory in user space. Caller must check
1231 * the specified block with access_ok() before calling this function.
1233 * Returns number of bytes that could not be cleared.
1234 * On success, this will be zero.
1236 static inline __kernel_size_t
1237 __clear_user(void __user
*addr
, __kernel_size_t size
)
1239 __kernel_size_t res
;
1241 if (eva_kernel_access()) {
1242 __asm__
__volatile__(
1246 __MODULE_JAL(__bzero_kernel
)
1249 : "r" (addr
), "r" (size
)
1250 : "$4", "$5", "$6", __UA_t0
, __UA_t1
, "$31");
1253 __asm__
__volatile__(
1257 __MODULE_JAL(__bzero
)
1260 : "r" (addr
), "r" (size
)
1261 : "$4", "$5", "$6", __UA_t0
, __UA_t1
, "$31");
1267 #define clear_user(addr,n) \
1269 void __user * __cl_addr = (addr); \
1270 unsigned long __cl_size = (n); \
1271 if (__cl_size && access_ok(VERIFY_WRITE, \
1272 __cl_addr, __cl_size)) \
1273 __cl_size = __clear_user(__cl_addr, __cl_size); \
1278 * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
1279 * @dst: Destination address, in kernel space. This buffer must be at
1280 * least @count bytes long.
1281 * @src: Source address, in user space.
1282 * @count: Maximum number of bytes to copy, including the trailing NUL.
1284 * Copies a NUL-terminated string from userspace to kernel space.
1285 * Caller must check the specified block with access_ok() before calling
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 (eva_kernel_access()) {
1303 __asm__
__volatile__(
1307 __MODULE_JAL(__strncpy_from_kernel_nocheck_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_nocheck_asm
)
1321 : "r" (__to
), "r" (__from
), "r" (__len
)
1322 : "$2", "$3", "$4", "$5", "$6", __UA_t0
, "$31", "memory");
1329 * strncpy_from_user: - Copy a NUL terminated string from userspace.
1330 * @dst: Destination address, in kernel space. This buffer must be at
1331 * least @count bytes long.
1332 * @src: Source address, in user space.
1333 * @count: Maximum number of bytes to copy, including the trailing NUL.
1335 * Copies a NUL-terminated string from userspace to kernel space.
1337 * On success, returns the length of the string (not including the trailing
1340 * If access to userspace fails, returns -EFAULT (some data may have been
1343 * If @count is smaller than the length of the string, copies @count bytes
1344 * and returns @count.
1347 strncpy_from_user(char *__to
, const char __user
*__from
, long __len
)
1351 if (eva_kernel_access()) {
1352 __asm__
__volatile__(
1356 __MODULE_JAL(__strncpy_from_kernel_asm
)
1359 : "r" (__to
), "r" (__from
), "r" (__len
)
1360 : "$2", "$3", "$4", "$5", "$6", __UA_t0
, "$31", "memory");
1363 __asm__
__volatile__(
1367 __MODULE_JAL(__strncpy_from_user_asm
)
1370 : "r" (__to
), "r" (__from
), "r" (__len
)
1371 : "$2", "$3", "$4", "$5", "$6", __UA_t0
, "$31", "memory");
1378 * strlen_user: - Get the size of a string in user space.
1379 * @str: The string to measure.
1381 * Context: User context only. This function may sleep if pagefaults are
1384 * Get the size of a NUL-terminated string in user space.
1386 * Returns the size of the string INCLUDING the terminating NUL.
1387 * On exception, returns 0.
1389 * If there is a limit on the length of a valid string, you may wish to
1390 * consider using strnlen_user() instead.
1392 static inline long strlen_user(const char __user
*s
)
1396 if (eva_kernel_access()) {
1397 __asm__
__volatile__(
1399 __MODULE_JAL(__strlen_kernel_asm
)
1403 : "$2", "$4", __UA_t0
, "$31");
1406 __asm__
__volatile__(
1408 __MODULE_JAL(__strlen_user_asm
)
1412 : "$2", "$4", __UA_t0
, "$31");
1418 /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
1419 static inline long __strnlen_user(const char __user
*s
, long n
)
1423 if (eva_kernel_access()) {
1424 __asm__
__volatile__(
1427 __MODULE_JAL(__strnlen_kernel_nocheck_asm
)
1431 : "$2", "$4", "$5", __UA_t0
, "$31");
1434 __asm__
__volatile__(
1437 __MODULE_JAL(__strnlen_user_nocheck_asm
)
1441 : "$2", "$4", "$5", __UA_t0
, "$31");
1448 * strnlen_user: - Get the size of a string in user space.
1449 * @str: The string to measure.
1451 * Context: User context only. This function may sleep if pagefaults are
1454 * Get the size of a NUL-terminated string in user space.
1456 * Returns the size of the string INCLUDING the terminating NUL.
1457 * On exception, returns 0.
1458 * If the string is too long, returns a value greater than @n.
1460 static inline long strnlen_user(const char __user
*s
, long n
)
1465 if (eva_kernel_access()) {
1466 __asm__
__volatile__(
1469 __MODULE_JAL(__strnlen_kernel_asm
)
1473 : "$2", "$4", "$5", __UA_t0
, "$31");
1475 __asm__
__volatile__(
1478 __MODULE_JAL(__strnlen_user_asm
)
1482 : "$2", "$4", "$5", __UA_t0
, "$31");
1488 struct exception_table_entry
1491 unsigned long nextinsn
;
1494 extern int fixup_exception(struct pt_regs
*regs
);
1496 #endif /* _ASM_UACCESS_H */