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); \
309 #define __get_data_asm(val, insn, addr) \
313 __asm__ __volatile__( \
314 "1: "insn("%1", "%3")" \n" \
317 " .section .fixup,\"ax\" \n" \
321 " .section __ex_table,\"a\" \n" \
322 " "__UA_ADDR "\t1b, 3b \n" \
324 : "=r" (__gu_err), "=r" (__gu_tmp) \
325 : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \
327 (val) = (__typeof__(*(addr))) __gu_tmp; \
331 * Get a long long 64 using 32 bit registers.
333 #define __get_data_asm_ll32(val, insn, addr) \
336 unsigned long long l; \
337 __typeof__(*(addr)) t; \
340 __asm__ __volatile__( \
341 "1: " insn("%1", "(%3)")" \n" \
342 "2: " insn("%D1", "4(%3)")" \n" \
345 " .section .fixup,\"ax\" \n" \
351 " .section __ex_table,\"a\" \n" \
352 " " __UA_ADDR " 1b, 4b \n" \
353 " " __UA_ADDR " 2b, 4b \n" \
355 : "=r" (__gu_err), "=&r" (__gu_tmp.l) \
356 : "0" (0), "r" (addr), "i" (-EFAULT)); \
358 (val) = __gu_tmp.t; \
362 #define __put_kernel_common(ptr, size) __put_user_common(ptr, size)
365 * Kernel specific functions for EVA. We need to use normal load instructions
366 * to read data from kernel when operating in EVA mode. We use these macros to
367 * avoid redefining __get_data_asm for EVA.
374 #define _stored _storew
376 #define _stored(reg, addr) "ld " reg ", " addr
379 #define _storew(reg, addr) "sw " reg ", " addr
380 #define _storeh(reg, addr) "sh " reg ", " addr
381 #define _storeb(reg, addr) "sb " reg ", " addr
383 #define __put_kernel_common(ptr, size) \
386 case 1: __put_data_asm(_storeb, ptr); break; \
387 case 2: __put_data_asm(_storeh, ptr); break; \
388 case 4: __put_data_asm(_storew, ptr); break; \
389 case 8: __PUT_DW(_stored, ptr); break; \
390 default: __put_user_unknown(); break; \
396 * Yuck. We need two variants, one for 64bit operation and one
397 * for 32 bit mode and old iron.
400 #define __PUT_DW(insn, ptr) __put_data_asm_ll32(insn, ptr)
403 #define __PUT_DW(insn, ptr) __put_data_asm(insn, ptr)
406 #define __put_user_common(ptr, size) \
409 case 1: __put_data_asm(user_sb, ptr); break; \
410 case 2: __put_data_asm(user_sh, ptr); break; \
411 case 4: __put_data_asm(user_sw, ptr); break; \
412 case 8: __PUT_DW(user_sd, ptr); break; \
413 default: __put_user_unknown(); break; \
417 #define __put_user_nocheck(x, ptr, size) \
419 __typeof__(*(ptr)) __pu_val; \
423 if (segment_eq(get_fs(), get_ds())) { \
424 __put_kernel_common(ptr, size); \
426 __chk_user_ptr(ptr); \
427 __put_user_common(ptr, size); \
432 #define __put_user_check(x, ptr, size) \
434 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
435 __typeof__(*(ptr)) __pu_val = (x); \
436 int __pu_err = -EFAULT; \
439 if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) { \
440 if (segment_eq(get_fs(), get_ds())) \
441 __put_kernel_common(__pu_addr, size); \
443 __put_user_common(__pu_addr, size); \
449 #define __put_data_asm(insn, ptr) \
451 __asm__ __volatile__( \
452 "1: "insn("%z2", "%3")" # __put_data_asm \n" \
455 " .section .fixup,\"ax\" \n" \
459 " .section __ex_table,\"a\" \n" \
460 " " __UA_ADDR " 1b, 3b \n" \
463 : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)), \
467 #define __put_data_asm_ll32(insn, ptr) \
469 __asm__ __volatile__( \
470 "1: "insn("%2", "(%3)")" # __put_data_asm_ll32 \n" \
471 "2: "insn("%D2", "4(%3)")" \n" \
474 " .section .fixup,\"ax\" \n" \
478 " .section __ex_table,\"a\" \n" \
479 " " __UA_ADDR " 1b, 4b \n" \
480 " " __UA_ADDR " 2b, 4b \n" \
483 : "0" (0), "r" (__pu_val), "r" (ptr), \
487 extern void __put_user_unknown(void);
490 * ul{b,h,w} are macros and there are no equivalent macros for EVA.
491 * EVA unaligned access is handled in the ADE exception handler.
495 * put_user_unaligned: - Write a simple value into user space.
496 * @x: Value to copy to user space.
497 * @ptr: Destination address, in user space.
499 * Context: User context only. This function may sleep.
501 * This macro copies a single simple value from kernel space to user
502 * space. It supports simple types like char and int, but not larger
503 * data types like structures or arrays.
505 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
506 * to the result of dereferencing @ptr.
508 * Returns zero on success, or -EFAULT on error.
510 #define put_user_unaligned(x,ptr) \
511 __put_user_unaligned_check((x),(ptr),sizeof(*(ptr)))
514 * get_user_unaligned: - Get a simple variable from user space.
515 * @x: Variable to store result.
516 * @ptr: Source address, in user space.
518 * Context: User context only. This function may sleep.
520 * This macro copies a single simple variable from user space to kernel
521 * space. It supports simple types like char and int, but not larger
522 * data types like structures or arrays.
524 * @ptr must have pointer-to-simple-variable type, and the result of
525 * dereferencing @ptr must be assignable to @x without a cast.
527 * Returns zero on success, or -EFAULT on error.
528 * On error, the variable @x is set to zero.
530 #define get_user_unaligned(x,ptr) \
531 __get_user_unaligned_check((x),(ptr),sizeof(*(ptr)))
534 * __put_user_unaligned: - Write a simple value into user space, with less checking.
535 * @x: Value to copy to user space.
536 * @ptr: Destination address, in user space.
538 * Context: User context only. This function may sleep.
540 * This macro copies a single simple value from kernel space to user
541 * space. It supports simple types like char and int, but not larger
542 * data types like structures or arrays.
544 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
545 * to the result of dereferencing @ptr.
547 * Caller must check the pointer with access_ok() before calling this
550 * Returns zero on success, or -EFAULT on error.
552 #define __put_user_unaligned(x,ptr) \
553 __put_user_unaligned_nocheck((x),(ptr),sizeof(*(ptr)))
556 * __get_user_unaligned: - Get a simple variable from user space, with less checking.
557 * @x: Variable to store result.
558 * @ptr: Source address, in user space.
560 * Context: User context only. This function may sleep.
562 * This macro copies a single simple variable from user space to kernel
563 * space. It supports simple types like char and int, but not larger
564 * data types like structures or arrays.
566 * @ptr must have pointer-to-simple-variable type, and the result of
567 * dereferencing @ptr must be assignable to @x without a cast.
569 * Caller must check the pointer with access_ok() before calling this
572 * Returns zero on success, or -EFAULT on error.
573 * On error, the variable @x is set to zero.
575 #define __get_user_unaligned(x,ptr) \
576 __get_user__unalignednocheck((x),(ptr),sizeof(*(ptr)))
579 * Yuck. We need two variants, one for 64bit operation and one
580 * for 32 bit mode and old iron.
583 #define __GET_USER_UNALIGNED_DW(val, ptr) \
584 __get_user_unaligned_asm_ll32(val, ptr)
587 #define __GET_USER_UNALIGNED_DW(val, ptr) \
588 __get_user_unaligned_asm(val, "uld", ptr)
591 extern void __get_user_unaligned_unknown(void);
593 #define __get_user_unaligned_common(val, size, ptr) \
596 case 1: __get_data_asm(val, "lb", ptr); break; \
597 case 2: __get_user_unaligned_asm(val, "ulh", ptr); break; \
598 case 4: __get_user_unaligned_asm(val, "ulw", ptr); break; \
599 case 8: __GET_USER_UNALIGNED_DW(val, ptr); break; \
600 default: __get_user_unaligned_unknown(); break; \
604 #define __get_user_unaligned_nocheck(x,ptr,size) \
608 __get_user_unaligned_common((x), size, ptr); \
612 #define __get_user_unaligned_check(x,ptr,size) \
614 int __gu_err = -EFAULT; \
615 const __typeof__(*(ptr)) __user * __gu_ptr = (ptr); \
617 if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) \
618 __get_user_unaligned_common((x), size, __gu_ptr); \
623 #define __get_data_unaligned_asm(val, insn, addr) \
627 __asm__ __volatile__( \
628 "1: " insn " %1, %3 \n" \
631 " .section .fixup,\"ax\" \n" \
635 " .section __ex_table,\"a\" \n" \
636 " "__UA_ADDR "\t1b, 3b \n" \
637 " "__UA_ADDR "\t1b + 4, 3b \n" \
639 : "=r" (__gu_err), "=r" (__gu_tmp) \
640 : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \
642 (val) = (__typeof__(*(addr))) __gu_tmp; \
646 * Get a long long 64 using 32 bit registers.
648 #define __get_user_unaligned_asm_ll32(val, addr) \
650 unsigned long long __gu_tmp; \
652 __asm__ __volatile__( \
653 "1: ulw %1, (%3) \n" \
654 "2: ulw %D1, 4(%3) \n" \
658 " .section .fixup,\"ax\" \n" \
664 " .section __ex_table,\"a\" \n" \
665 " " __UA_ADDR " 1b, 4b \n" \
666 " " __UA_ADDR " 1b + 4, 4b \n" \
667 " " __UA_ADDR " 2b, 4b \n" \
668 " " __UA_ADDR " 2b + 4, 4b \n" \
670 : "=r" (__gu_err), "=&r" (__gu_tmp) \
671 : "0" (0), "r" (addr), "i" (-EFAULT)); \
672 (val) = (__typeof__(*(addr))) __gu_tmp; \
676 * Yuck. We need two variants, one for 64bit operation and one
677 * for 32 bit mode and old iron.
680 #define __PUT_USER_UNALIGNED_DW(ptr) __put_user_unaligned_asm_ll32(ptr)
683 #define __PUT_USER_UNALIGNED_DW(ptr) __put_user_unaligned_asm("usd", ptr)
686 #define __put_user_unaligned_common(ptr, size) \
689 case 1: __put_data_asm("sb", ptr); break; \
690 case 2: __put_user_unaligned_asm("ush", ptr); break; \
691 case 4: __put_user_unaligned_asm("usw", ptr); break; \
692 case 8: __PUT_USER_UNALIGNED_DW(ptr); break; \
693 default: __put_user_unaligned_unknown(); break; \
696 #define __put_user_unaligned_nocheck(x,ptr,size) \
698 __typeof__(*(ptr)) __pu_val; \
702 __put_user_unaligned_common(ptr, size); \
706 #define __put_user_unaligned_check(x,ptr,size) \
708 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
709 __typeof__(*(ptr)) __pu_val = (x); \
710 int __pu_err = -EFAULT; \
712 if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) \
713 __put_user_unaligned_common(__pu_addr, size); \
718 #define __put_user_unaligned_asm(insn, ptr) \
720 __asm__ __volatile__( \
721 "1: " insn " %z2, %3 # __put_user_unaligned_asm\n" \
724 " .section .fixup,\"ax\" \n" \
728 " .section __ex_table,\"a\" \n" \
729 " " __UA_ADDR " 1b, 3b \n" \
732 : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)), \
736 #define __put_user_unaligned_asm_ll32(ptr) \
738 __asm__ __volatile__( \
739 "1: sw %2, (%3) # __put_user_unaligned_asm_ll32 \n" \
740 "2: sw %D2, 4(%3) \n" \
743 " .section .fixup,\"ax\" \n" \
747 " .section __ex_table,\"a\" \n" \
748 " " __UA_ADDR " 1b, 4b \n" \
749 " " __UA_ADDR " 1b + 4, 4b \n" \
750 " " __UA_ADDR " 2b, 4b \n" \
751 " " __UA_ADDR " 2b + 4, 4b \n" \
754 : "0" (0), "r" (__pu_val), "r" (ptr), \
758 extern void __put_user_unaligned_unknown(void);
762 * We're generating jump to subroutines which will be outside the range of
766 #define __MODULE_JAL(destination) \
768 __UA_LA "\t$1, " #destination "\n\t" \
772 #define __MODULE_JAL(destination) \
773 "jal\t" #destination "\n\t"
776 #ifndef CONFIG_CPU_DADDI_WORKAROUNDS
777 #define DADDI_SCRATCH "$0"
779 #define DADDI_SCRATCH "$3"
782 extern size_t __copy_user(void *__to
, const void *__from
, size_t __n
);
785 #define __invoke_copy_to_user(to, from, n) \
787 register void __user *__cu_to_r __asm__("$4"); \
788 register const void *__cu_from_r __asm__("$5"); \
789 register long __cu_len_r __asm__("$6"); \
792 __cu_from_r = (from); \
794 __asm__ __volatile__( \
795 __MODULE_JAL(__copy_user) \
796 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
798 : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
799 DADDI_SCRATCH, "memory"); \
803 #define __invoke_copy_to_kernel(to, from, n) \
804 __invoke_copy_to_user(to, from, n)
809 * __copy_to_user: - Copy a block of data into user space, with less checking.
810 * @to: Destination address, in user space.
811 * @from: Source address, in kernel space.
812 * @n: Number of bytes to copy.
814 * Context: User context only. This function may sleep.
816 * Copy data from kernel space to user space. Caller must check
817 * the specified block with access_ok() before calling this function.
819 * Returns number of bytes that could not be copied.
820 * On success, this will be zero.
822 #define __copy_to_user(to, from, n) \
824 void __user *__cu_to; \
825 const void *__cu_from; \
829 __cu_from = (from); \
832 if (segment_eq(get_fs(), get_ds())) \
833 __cu_len = __invoke_copy_to_kernel(__cu_to, __cu_from, \
836 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, \
841 extern size_t __copy_user_inatomic(void *__to
, const void *__from
, size_t __n
);
843 #define __copy_to_user_inatomic(to, from, n) \
845 void __user *__cu_to; \
846 const void *__cu_from; \
850 __cu_from = (from); \
852 if (segment_eq(get_fs(), get_ds())) \
853 __cu_len = __invoke_copy_to_kernel(__cu_to, __cu_from, \
856 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, \
861 #define __copy_from_user_inatomic(to, from, n) \
864 const void __user *__cu_from; \
868 __cu_from = (from); \
870 if (segment_eq(get_fs(), get_ds())) \
871 __cu_len = __invoke_copy_from_kernel_inatomic(__cu_to, \
875 __cu_len = __invoke_copy_from_user_inatomic(__cu_to, \
882 * copy_to_user: - Copy a block of data into user space.
883 * @to: Destination address, in user space.
884 * @from: Source address, in kernel space.
885 * @n: Number of bytes to copy.
887 * Context: User context only. This function may sleep.
889 * Copy data from kernel space to user space.
891 * Returns number of bytes that could not be copied.
892 * On success, this will be zero.
894 #define copy_to_user(to, from, n) \
896 void __user *__cu_to; \
897 const void *__cu_from; \
901 __cu_from = (from); \
903 if (segment_eq(get_fs(), get_ds())) { \
904 __cu_len = __invoke_copy_to_kernel(__cu_to, \
908 if (access_ok(VERIFY_WRITE, __cu_to, __cu_len)) { \
910 __cu_len = __invoke_copy_to_user(__cu_to, \
920 #define __invoke_copy_from_user(to, from, n) \
922 register void *__cu_to_r __asm__("$4"); \
923 register const void __user *__cu_from_r __asm__("$5"); \
924 register long __cu_len_r __asm__("$6"); \
927 __cu_from_r = (from); \
929 __asm__ __volatile__( \
930 ".set\tnoreorder\n\t" \
931 __MODULE_JAL(__copy_user) \
933 __UA_ADDU "\t$1, %1, %2\n\t" \
936 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
938 : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
939 DADDI_SCRATCH, "memory"); \
943 #define __invoke_copy_from_kernel(to, from, n) \
944 __invoke_copy_from_user(to, from, n)
946 /* For userland <-> userland operations */
947 #define ___invoke_copy_in_user(to, from, n) \
948 __invoke_copy_from_user(to, from, n)
950 /* For kernel <-> kernel operations */
951 #define ___invoke_copy_in_kernel(to, from, n) \
952 __invoke_copy_from_user(to, from, n)
954 #define __invoke_copy_from_user_inatomic(to, from, n) \
956 register void *__cu_to_r __asm__("$4"); \
957 register const void __user *__cu_from_r __asm__("$5"); \
958 register long __cu_len_r __asm__("$6"); \
961 __cu_from_r = (from); \
963 __asm__ __volatile__( \
964 ".set\tnoreorder\n\t" \
965 __MODULE_JAL(__copy_user_inatomic) \
967 __UA_ADDU "\t$1, %1, %2\n\t" \
970 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
972 : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
973 DADDI_SCRATCH, "memory"); \
977 #define __invoke_copy_from_kernel_inatomic(to, from, n) \
978 __invoke_copy_from_user_inatomic(to, from, n) \
982 /* EVA specific functions */
984 extern size_t __copy_user_inatomic_eva(void *__to
, const void *__from
,
986 extern size_t __copy_from_user_eva(void *__to
, const void *__from
,
988 extern size_t __copy_to_user_eva(void *__to
, const void *__from
,
990 extern size_t __copy_in_user_eva(void *__to
, const void *__from
, size_t __n
);
992 #define __invoke_copy_from_user_eva_generic(to, from, n, func_ptr) \
994 register void *__cu_to_r __asm__("$4"); \
995 register const void __user *__cu_from_r __asm__("$5"); \
996 register long __cu_len_r __asm__("$6"); \
999 __cu_from_r = (from); \
1001 __asm__ __volatile__( \
1002 ".set\tnoreorder\n\t" \
1003 __MODULE_JAL(func_ptr) \
1005 __UA_ADDU "\t$1, %1, %2\n\t" \
1008 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
1010 : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
1011 DADDI_SCRATCH, "memory"); \
1015 #define __invoke_copy_to_user_eva_generic(to, from, n, func_ptr) \
1017 register void *__cu_to_r __asm__("$4"); \
1018 register const void __user *__cu_from_r __asm__("$5"); \
1019 register long __cu_len_r __asm__("$6"); \
1022 __cu_from_r = (from); \
1024 __asm__ __volatile__( \
1025 __MODULE_JAL(func_ptr) \
1026 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
1028 : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
1029 DADDI_SCRATCH, "memory"); \
1034 * Source or destination address is in userland. We need to go through
1037 #define __invoke_copy_from_user(to, from, n) \
1038 __invoke_copy_from_user_eva_generic(to, from, n, __copy_from_user_eva)
1040 #define __invoke_copy_from_user_inatomic(to, from, n) \
1041 __invoke_copy_from_user_eva_generic(to, from, n, \
1042 __copy_user_inatomic_eva)
1044 #define __invoke_copy_to_user(to, from, n) \
1045 __invoke_copy_to_user_eva_generic(to, from, n, __copy_to_user_eva)
1047 #define ___invoke_copy_in_user(to, from, n) \
1048 __invoke_copy_from_user_eva_generic(to, from, n, __copy_in_user_eva)
1051 * Source or destination address in the kernel. We are not going through
1054 #define __invoke_copy_from_kernel(to, from, n) \
1055 __invoke_copy_from_user_eva_generic(to, from, n, __copy_user)
1057 #define __invoke_copy_from_kernel_inatomic(to, from, n) \
1058 __invoke_copy_from_user_eva_generic(to, from, n, __copy_user_inatomic)
1060 #define __invoke_copy_to_kernel(to, from, n) \
1061 __invoke_copy_to_user_eva_generic(to, from, n, __copy_user)
1063 #define ___invoke_copy_in_kernel(to, from, n) \
1064 __invoke_copy_from_user_eva_generic(to, from, n, __copy_user)
1066 #endif /* CONFIG_EVA */
1069 * __copy_from_user: - Copy a block of data from user space, with less checking.
1070 * @to: Destination address, in kernel space.
1071 * @from: Source address, in user space.
1072 * @n: Number of bytes to copy.
1074 * Context: User context only. This function may sleep.
1076 * Copy data from user space to kernel space. Caller must check
1077 * the specified block with access_ok() before calling this function.
1079 * Returns number of bytes that could not be copied.
1080 * On success, this will be zero.
1082 * If some data could not be copied, this function will pad the copied
1083 * data to the requested size using zero bytes.
1085 #define __copy_from_user(to, from, n) \
1088 const void __user *__cu_from; \
1092 __cu_from = (from); \
1095 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
1101 * copy_from_user: - Copy a block of data from user space.
1102 * @to: Destination address, in kernel space.
1103 * @from: Source address, in user space.
1104 * @n: Number of bytes to copy.
1106 * Context: User context only. This function may sleep.
1108 * Copy data from user space to kernel space.
1110 * Returns number of bytes that could not be copied.
1111 * On success, this will be zero.
1113 * If some data could not be copied, this function will pad the copied
1114 * data to the requested size using zero bytes.
1116 #define copy_from_user(to, from, n) \
1119 const void __user *__cu_from; \
1123 __cu_from = (from); \
1125 if (segment_eq(get_fs(), get_ds())) { \
1126 __cu_len = __invoke_copy_from_kernel(__cu_to, \
1130 if (access_ok(VERIFY_READ, __cu_from, __cu_len)) { \
1132 __cu_len = __invoke_copy_from_user(__cu_to, \
1140 #define __copy_in_user(to, from, n) \
1142 void __user *__cu_to; \
1143 const void __user *__cu_from; \
1147 __cu_from = (from); \
1149 if (segment_eq(get_fs(), get_ds())) { \
1150 __cu_len = ___invoke_copy_in_kernel(__cu_to, __cu_from, \
1154 __cu_len = ___invoke_copy_in_user(__cu_to, __cu_from, \
1160 #define copy_in_user(to, from, n) \
1162 void __user *__cu_to; \
1163 const void __user *__cu_from; \
1167 __cu_from = (from); \
1169 if (segment_eq(get_fs(), get_ds())) { \
1170 __cu_len = ___invoke_copy_in_kernel(__cu_to,__cu_from, \
1173 if (likely(access_ok(VERIFY_READ, __cu_from, __cu_len) &&\
1174 access_ok(VERIFY_WRITE, __cu_to, __cu_len))) {\
1176 __cu_len = ___invoke_copy_in_user(__cu_to, \
1185 * __clear_user: - Zero a block of memory in user space, with less checking.
1186 * @to: Destination address, in user space.
1187 * @n: Number of bytes to zero.
1189 * Zero a block of memory in user space. Caller must check
1190 * the specified block with access_ok() before calling this function.
1192 * Returns number of bytes that could not be cleared.
1193 * On success, this will be zero.
1195 static inline __kernel_size_t
1196 __clear_user(void __user
*addr
, __kernel_size_t size
)
1198 __kernel_size_t res
;
1201 __asm__
__volatile__(
1205 __MODULE_JAL(__bzero
)
1208 : "r" (addr
), "r" (size
)
1209 : "$4", "$5", "$6", __UA_t0
, __UA_t1
, "$31");
1214 #define clear_user(addr,n) \
1216 void __user * __cl_addr = (addr); \
1217 unsigned long __cl_size = (n); \
1218 if (__cl_size && access_ok(VERIFY_WRITE, \
1219 __cl_addr, __cl_size)) \
1220 __cl_size = __clear_user(__cl_addr, __cl_size); \
1225 * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
1226 * @dst: Destination address, in kernel space. This buffer must be at
1227 * least @count bytes long.
1228 * @src: Source address, in user space.
1229 * @count: Maximum number of bytes to copy, including the trailing NUL.
1231 * Copies a NUL-terminated string from userspace to kernel space.
1232 * Caller must check the specified block with access_ok() before calling
1235 * On success, returns the length of the string (not including the trailing
1238 * If access to userspace fails, returns -EFAULT (some data may have been
1241 * If @count is smaller than the length of the string, copies @count bytes
1242 * and returns @count.
1245 __strncpy_from_user(char *__to
, const char __user
*__from
, long __len
)
1249 if (segment_eq(get_fs(), get_ds())) {
1250 __asm__
__volatile__(
1254 __MODULE_JAL(__strncpy_from_kernel_nocheck_asm
)
1257 : "r" (__to
), "r" (__from
), "r" (__len
)
1258 : "$2", "$3", "$4", "$5", "$6", __UA_t0
, "$31", "memory");
1261 __asm__
__volatile__(
1265 __MODULE_JAL(__strncpy_from_user_nocheck_asm
)
1268 : "r" (__to
), "r" (__from
), "r" (__len
)
1269 : "$2", "$3", "$4", "$5", "$6", __UA_t0
, "$31", "memory");
1276 * strncpy_from_user: - Copy a NUL terminated string from userspace.
1277 * @dst: Destination address, in kernel space. This buffer must be at
1278 * least @count bytes long.
1279 * @src: Source address, in user space.
1280 * @count: Maximum number of bytes to copy, including the trailing NUL.
1282 * Copies a NUL-terminated string from userspace to kernel space.
1284 * On success, returns the length of the string (not including the trailing
1287 * If access to userspace fails, returns -EFAULT (some data may have been
1290 * If @count is smaller than the length of the string, copies @count bytes
1291 * and returns @count.
1294 strncpy_from_user(char *__to
, const char __user
*__from
, long __len
)
1298 if (segment_eq(get_fs(), get_ds())) {
1299 __asm__
__volatile__(
1303 __MODULE_JAL(__strncpy_from_kernel_asm
)
1306 : "r" (__to
), "r" (__from
), "r" (__len
)
1307 : "$2", "$3", "$4", "$5", "$6", __UA_t0
, "$31", "memory");
1310 __asm__
__volatile__(
1314 __MODULE_JAL(__strncpy_from_user_asm
)
1317 : "r" (__to
), "r" (__from
), "r" (__len
)
1318 : "$2", "$3", "$4", "$5", "$6", __UA_t0
, "$31", "memory");
1324 /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
1325 static inline long __strlen_user(const char __user
*s
)
1329 if (segment_eq(get_fs(), get_ds())) {
1330 __asm__
__volatile__(
1332 __MODULE_JAL(__strlen_kernel_nocheck_asm
)
1336 : "$2", "$4", __UA_t0
, "$31");
1339 __asm__
__volatile__(
1341 __MODULE_JAL(__strlen_user_nocheck_asm
)
1345 : "$2", "$4", __UA_t0
, "$31");
1352 * strlen_user: - Get the size of a string in user space.
1353 * @str: The string to measure.
1355 * Context: User context only. This function may sleep.
1357 * Get the size of a NUL-terminated string in user space.
1359 * Returns the size of the string INCLUDING the terminating NUL.
1360 * On exception, returns 0.
1362 * If there is a limit on the length of a valid string, you may wish to
1363 * consider using strnlen_user() instead.
1365 static inline long strlen_user(const char __user
*s
)
1369 if (segment_eq(get_fs(), get_ds())) {
1370 __asm__
__volatile__(
1372 __MODULE_JAL(__strlen_kernel_asm
)
1376 : "$2", "$4", __UA_t0
, "$31");
1379 __asm__
__volatile__(
1381 __MODULE_JAL(__strlen_kernel_asm
)
1385 : "$2", "$4", __UA_t0
, "$31");
1391 /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
1392 static inline long __strnlen_user(const char __user
*s
, long n
)
1396 if (segment_eq(get_fs(), get_ds())) {
1397 __asm__
__volatile__(
1400 __MODULE_JAL(__strnlen_kernel_nocheck_asm
)
1404 : "$2", "$4", "$5", __UA_t0
, "$31");
1407 __asm__
__volatile__(
1410 __MODULE_JAL(__strnlen_user_nocheck_asm
)
1414 : "$2", "$4", "$5", __UA_t0
, "$31");
1421 * strlen_user: - Get the size of a string in user space.
1422 * @str: The string to measure.
1424 * Context: User context only. This function may sleep.
1426 * Get the size of a NUL-terminated string in user space.
1428 * Returns the size of the string INCLUDING the terminating NUL.
1429 * On exception, returns 0.
1431 * If there is a limit on the length of a valid string, you may wish to
1432 * consider using strnlen_user() instead.
1434 static inline long strnlen_user(const char __user
*s
, long n
)
1439 if (segment_eq(get_fs(), get_ds())) {
1440 __asm__
__volatile__(
1443 __MODULE_JAL(__strnlen_kernel_asm
)
1447 : "$2", "$4", "$5", __UA_t0
, "$31");
1449 __asm__
__volatile__(
1452 __MODULE_JAL(__strnlen_user_asm
)
1456 : "$2", "$4", "$5", __UA_t0
, "$31");
1462 struct exception_table_entry
1465 unsigned long nextinsn
;
1468 extern int fixup_exception(struct pt_regs
*regs
);
1470 #endif /* _ASM_UACCESS_H */