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);
784 #define __invoke_copy_to_user(to, from, n) \
786 register void __user *__cu_to_r __asm__("$4"); \
787 register const void *__cu_from_r __asm__("$5"); \
788 register long __cu_len_r __asm__("$6"); \
791 __cu_from_r = (from); \
793 __asm__ __volatile__( \
794 __MODULE_JAL(__copy_user) \
795 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
797 : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
798 DADDI_SCRATCH, "memory"); \
803 * __copy_to_user: - Copy a block of data into user space, with less checking.
804 * @to: Destination address, in user space.
805 * @from: Source address, in kernel space.
806 * @n: Number of bytes to copy.
808 * Context: User context only. This function may sleep.
810 * Copy data from kernel space to user space. Caller must check
811 * the specified block with access_ok() before calling this function.
813 * Returns number of bytes that could not be copied.
814 * On success, this will be zero.
816 #define __copy_to_user(to, from, n) \
818 void __user *__cu_to; \
819 const void *__cu_from; \
823 __cu_from = (from); \
826 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, __cu_len); \
830 extern size_t __copy_user_inatomic(void *__to, const void *__from, size_t __n);
832 #define __copy_to_user_inatomic(to, from, n) \
834 void __user *__cu_to; \
835 const void *__cu_from; \
839 __cu_from = (from); \
841 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, __cu_len); \
845 #define __copy_from_user_inatomic(to, from, n) \
848 const void __user *__cu_from; \
852 __cu_from = (from); \
854 __cu_len = __invoke_copy_from_user_inatomic(__cu_to, __cu_from, \
860 * copy_to_user: - Copy a block of data into user space.
861 * @to: Destination address, in user space.
862 * @from: Source address, in kernel space.
863 * @n: Number of bytes to copy.
865 * Context: User context only. This function may sleep.
867 * Copy data from kernel space to user space.
869 * Returns number of bytes that could not be copied.
870 * On success, this will be zero.
872 #define copy_to_user(to, from, n) \
874 void __user *__cu_to; \
875 const void *__cu_from; \
879 __cu_from = (from); \
881 if (access_ok(VERIFY_WRITE, __cu_to, __cu_len)) { \
883 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, \
889 #define __invoke_copy_from_user(to, from, n) \
891 register void *__cu_to_r __asm__("$4"); \
892 register const void __user *__cu_from_r __asm__("$5"); \
893 register long __cu_len_r __asm__("$6"); \
896 __cu_from_r = (from); \
898 __asm__ __volatile__( \
899 ".set\tnoreorder\n\t" \
900 __MODULE_JAL(__copy_user) \
902 __UA_ADDU "\t$1, %1, %2\n\t" \
905 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
907 : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
908 DADDI_SCRATCH, "memory"); \
912 #define __invoke_copy_from_user_inatomic(to, from, n) \
914 register void *__cu_to_r __asm__("$4"); \
915 register const void __user *__cu_from_r __asm__("$5"); \
916 register long __cu_len_r __asm__("$6"); \
919 __cu_from_r = (from); \
921 __asm__ __volatile__( \
922 ".set\tnoreorder\n\t" \
923 __MODULE_JAL(__copy_user_inatomic) \
925 __UA_ADDU "\t$1, %1, %2\n\t" \
928 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
930 : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
931 DADDI_SCRATCH, "memory"); \
936 * __copy_from_user: - Copy a block of data from user space, with less checking.
937 * @to: Destination address, in kernel space.
938 * @from: Source address, in user space.
939 * @n: Number of bytes to copy.
941 * Context: User context only. This function may sleep.
943 * Copy data from user space to kernel space. Caller must check
944 * the specified block with access_ok() before calling this function.
946 * Returns number of bytes that could not be copied.
947 * On success, this will be zero.
949 * If some data could not be copied, this function will pad the copied
950 * data to the requested size using zero bytes.
952 #define __copy_from_user(to, from, n) \
955 const void __user *__cu_from; \
959 __cu_from = (from); \
962 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
968 * copy_from_user: - Copy a block of data from user space.
969 * @to: Destination address, in kernel space.
970 * @from: Source address, in user space.
971 * @n: Number of bytes to copy.
973 * Context: User context only. This function may sleep.
975 * Copy data from user space to kernel space.
977 * Returns number of bytes that could not be copied.
978 * On success, this will be zero.
980 * If some data could not be copied, this function will pad the copied
981 * data to the requested size using zero bytes.
983 #define copy_from_user(to, from, n) \
986 const void __user *__cu_from; \
990 __cu_from = (from); \
992 if (access_ok(VERIFY_READ, __cu_from, __cu_len)) { \
994 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
1000 #define __copy_in_user(to, from, n) \
1002 void __user *__cu_to; \
1003 const void __user *__cu_from; \
1007 __cu_from = (from); \
1010 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
1015 #define copy_in_user(to, from, n) \
1017 void __user *__cu_to; \
1018 const void __user *__cu_from; \
1022 __cu_from = (from); \
1024 if (likely(access_ok(VERIFY_READ, __cu_from, __cu_len) && \
1025 access_ok(VERIFY_WRITE, __cu_to, __cu_len))) { \
1027 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
1034 * __clear_user: - Zero a block of memory in user space, with less checking.
1035 * @to: Destination address, in user space.
1036 * @n: Number of bytes to zero.
1038 * Zero a block of memory in user space. Caller must check
1039 * the specified block with access_ok() before calling this function.
1041 * Returns number of bytes that could not be cleared.
1042 * On success, this will be zero.
1044 static inline __kernel_size_t
1045 __clear_user(void __user *addr, __kernel_size_t size)
1047 __kernel_size_t res;
1050 __asm__ __volatile__(
1054 __MODULE_JAL(__bzero)
1057 : "r" (addr), "r" (size)
1058 : "$4", "$5", "$6", __UA_t0, __UA_t1, "$31");
1063 #define clear_user(addr,n) \
1065 void __user * __cl_addr = (addr); \
1066 unsigned long __cl_size = (n); \
1067 if (__cl_size && access_ok(VERIFY_WRITE, \
1068 __cl_addr, __cl_size)) \
1069 __cl_size = __clear_user(__cl_addr, __cl_size); \
1074 * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
1075 * @dst: Destination address, in kernel space. This buffer must be at
1076 * least @count bytes long.
1077 * @src: Source address, in user space.
1078 * @count: Maximum number of bytes to copy, including the trailing NUL.
1080 * Copies a NUL-terminated string from userspace to kernel space.
1081 * Caller must check the specified block with access_ok() before calling
1084 * On success, returns the length of the string (not including the trailing
1087 * If access to userspace fails, returns -EFAULT (some data may have been
1090 * If @count is smaller than the length of the string, copies @count bytes
1091 * and returns @count.
1094 __strncpy_from_user(char *__to, const char __user *__from, long __len)
1099 __asm__ __volatile__(
1103 __MODULE_JAL(__strncpy_from_user_nocheck_asm)
1106 : "r" (__to), "r" (__from), "r" (__len)
1107 : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
1113 * strncpy_from_user: - Copy a NUL terminated string from userspace.
1114 * @dst: Destination address, in kernel space. This buffer must be at
1115 * least @count bytes long.
1116 * @src: Source address, in user space.
1117 * @count: Maximum number of bytes to copy, including the trailing NUL.
1119 * Copies a NUL-terminated string from userspace to kernel space.
1121 * On success, returns the length of the string (not including the trailing
1124 * If access to userspace fails, returns -EFAULT (some data may have been
1127 * If @count is smaller than the length of the string, copies @count bytes
1128 * and returns @count.
1131 strncpy_from_user(char *__to, const char __user *__from, long __len)
1136 __asm__ __volatile__(
1140 __MODULE_JAL(__strncpy_from_user_asm)
1143 : "r" (__to), "r" (__from), "r" (__len)
1144 : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
1149 /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
1150 static inline long __strlen_user(const char __user *s)
1155 __asm__ __volatile__(
1157 __MODULE_JAL(__strlen_user_nocheck_asm)
1161 : "$2", "$4", __UA_t0, "$31");
1167 * strlen_user: - Get the size of a string in user space.
1168 * @str: The string to measure.
1170 * Context: User context only. This function may sleep.
1172 * Get the size of a NUL-terminated string in user space.
1174 * Returns the size of the string INCLUDING the terminating NUL.
1175 * On exception, returns 0.
1177 * If there is a limit on the length of a valid string, you may wish to
1178 * consider using strnlen_user() instead.
1180 static inline long strlen_user(const char __user *s)
1185 __asm__ __volatile__(
1187 __MODULE_JAL(__strlen_user_asm)
1191 : "$2", "$4", __UA_t0, "$31");
1196 /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
1197 static inline long __strnlen_user(const char __user *s, long n)
1202 __asm__ __volatile__(
1205 __MODULE_JAL(__strnlen_user_nocheck_asm)
1209 : "$2", "$4", "$5", __UA_t0, "$31");
1215 * strlen_user: - Get the size of a string in user space.
1216 * @str: The string to measure.
1218 * Context: User context only. This function may sleep.
1220 * Get the size of a NUL-terminated string in user space.
1222 * Returns the size of the string INCLUDING the terminating NUL.
1223 * On exception, returns 0.
1225 * If there is a limit on the length of a valid string, you may wish to
1226 * consider using strnlen_user() instead.
1228 static inline long strnlen_user(const char __user *s, long n)
1233 __asm__ __volatile__(
1236 __MODULE_JAL(__strnlen_user_asm)
1240 : "$2", "$4", "$5", __UA_t0, "$31");
1245 struct exception_table_entry
1248 unsigned long nextinsn;
1251 extern int fixup_exception(struct pt_regs *regs);
1253 #endif /* _ASM_UACCESS_H */