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
10 #ifndef _ASM_UACCESS_H
11 #define _ASM_UACCESS_H
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/thread_info.h>
18 * The fs value determines whether argument validity checking should be
19 * performed or not. If get_fs() == USER_DS, checking is performed, with
20 * get_fs() == KERNEL_DS, checking is bypassed.
22 * For historical reasons, these macros are grossly misnamed.
26 #define __UA_LIMIT 0x80000000UL
28 #define __UA_ADDR ".word"
30 #define __UA_ADDU "addu"
34 #endif /* CONFIG_32BIT */
38 #define __UA_LIMIT (- TASK_SIZE)
40 #define __UA_ADDR ".dword"
42 #define __UA_ADDU "daddu"
46 #endif /* CONFIG_64BIT */
49 * USER_DS is a bitmask that has the bits set that may not be set in a valid
50 * userspace address. Note that we limit 32-bit userspace to 0x7fff8000 but
51 * the arithmetic we're doing only works if the limit is a power of two, so
52 * we use 0x80000000 here on 32-bit kernels. If a process passes an invalid
53 * address in this range it's the process's problem, not ours :-)
56 #define KERNEL_DS ((mm_segment_t) { 0UL })
57 #define USER_DS ((mm_segment_t) { __UA_LIMIT })
60 #define VERIFY_WRITE 1
62 #define get_ds() (KERNEL_DS)
63 #define get_fs() (current_thread_info()->addr_limit)
64 #define set_fs(x) (current_thread_info()->addr_limit = (x))
66 #define segment_eq(a, b) ((a).seg == (b).seg)
70 * Is a address valid? This does a straighforward calculation rather
74 * - "addr" doesn't have any high-bits set
75 * - AND "size" doesn't have any high-bits set
76 * - AND "addr+size" doesn't have any high-bits set
77 * - OR we are in kernel mode.
79 * __ua_size() is a trick to avoid runtime checking of positive constant
80 * sizes; for those we already know at compile time that the size is ok.
82 #define __ua_size(size) \
83 ((__builtin_constant_p(size) && (signed long) (size) > 0) ? 0 : (size))
86 * access_ok: - Checks if a user space pointer is valid
87 * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
88 * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
89 * to write to a block, it is always safe to read from it.
90 * @addr: User space pointer to start of block to check
91 * @size: Size of block to check
93 * Context: User context only. This function may sleep.
95 * Checks if a pointer to a block of memory in user space is valid.
97 * Returns true (nonzero) if the memory block may be valid, false (zero)
98 * if it is definitely invalid.
100 * Note that, depending on architecture, this function probably just
101 * checks that the pointer is in the user space range - after calling
102 * this function, memory access functions may still return -EFAULT.
105 #define __access_mask get_fs().seg
107 #define __access_ok(addr, size, mask) \
109 const volatile void __user *__up = addr; \
110 unsigned long __addr = (unsigned long) __up; \
111 unsigned long __size = size; \
112 unsigned long __mask = mask; \
113 unsigned long __ok; \
115 __ok = (signed long)(__mask & (__addr | (__addr + __size) | \
116 __ua_size(__size))); \
120 #define access_ok(type, addr, size) \
121 likely(__access_ok((addr), (size), __access_mask))
124 * put_user: - Write a simple value into user space.
125 * @x: Value to copy to user space.
126 * @ptr: Destination address, in user space.
128 * Context: User context only. This function may sleep.
130 * This macro copies a single simple value from kernel space to user
131 * space. It supports simple types like char and int, but not larger
132 * data types like structures or arrays.
134 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
135 * to the result of dereferencing @ptr.
137 * Returns zero on success, or -EFAULT on error.
139 #define put_user(x,ptr) \
140 __put_user_check((x), (ptr), sizeof(*(ptr)))
143 * get_user: - Get a simple variable from user space.
144 * @x: Variable to store result.
145 * @ptr: Source address, in user space.
147 * Context: User context only. This function may sleep.
149 * This macro copies a single simple variable from user space to kernel
150 * space. It supports simple types like char and int, but not larger
151 * data types like structures or arrays.
153 * @ptr must have pointer-to-simple-variable type, and the result of
154 * dereferencing @ptr must be assignable to @x without a cast.
156 * Returns zero on success, or -EFAULT on error.
157 * On error, the variable @x is set to zero.
159 #define get_user(x,ptr) \
160 __get_user_check((x), (ptr), sizeof(*(ptr)))
163 * __put_user: - Write a simple value into user space, with less checking.
164 * @x: Value to copy to user space.
165 * @ptr: Destination address, in user space.
167 * Context: User context only. This function may sleep.
169 * This macro copies a single simple value from kernel space to user
170 * space. It supports simple types like char and int, but not larger
171 * data types like structures or arrays.
173 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
174 * to the result of dereferencing @ptr.
176 * Caller must check the pointer with access_ok() before calling this
179 * Returns zero on success, or -EFAULT on error.
181 #define __put_user(x,ptr) \
182 __put_user_nocheck((x), (ptr), sizeof(*(ptr)))
185 * __get_user: - Get a simple variable from user space, with less checking.
186 * @x: Variable to store result.
187 * @ptr: Source address, in user space.
189 * Context: User context only. This function may sleep.
191 * This macro copies a single simple variable from user space to kernel
192 * space. It supports simple types like char and int, but not larger
193 * data types like structures or arrays.
195 * @ptr must have pointer-to-simple-variable type, and the result of
196 * dereferencing @ptr must be assignable to @x without a cast.
198 * Caller must check the pointer with access_ok() before calling this
201 * Returns zero on success, or -EFAULT on error.
202 * On error, the variable @x is set to zero.
204 #define __get_user(x,ptr) \
205 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
207 struct __large_struct
{ unsigned long buf
[100]; };
208 #define __m(x) (*(struct __large_struct __user *)(x))
211 * Yuck. We need two variants, one for 64bit operation and one
212 * for 32 bit mode and old iron.
215 #define __GET_USER_DW(val, ptr) __get_user_asm_ll32(val, ptr)
218 #define __GET_USER_DW(val, ptr) __get_user_asm(val, "ld", ptr)
221 extern void __get_user_unknown(void);
223 #define __get_user_common(val, size, ptr) \
226 case 1: __get_user_asm(val, "lb", ptr); break; \
227 case 2: __get_user_asm(val, "lh", ptr); break; \
228 case 4: __get_user_asm(val, "lw", ptr); break; \
229 case 8: __GET_USER_DW(val, ptr); break; \
230 default: __get_user_unknown(); break; \
234 #define __get_user_nocheck(x, ptr, size) \
238 __get_user_common((x), size, ptr); \
242 #define __get_user_check(x, ptr, size) \
244 int __gu_err = -EFAULT; \
245 const __typeof__(*(ptr)) __user * __gu_ptr = (ptr); \
247 if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) \
248 __get_user_common((x), size, __gu_ptr); \
253 #define __get_user_asm(val, insn, addr) \
257 __asm__ __volatile__( \
258 "1: " insn " %1, %3 \n" \
260 " .section .fixup,\"ax\" \n" \
264 " .section __ex_table,\"a\" \n" \
265 " "__UA_ADDR "\t1b, 3b \n" \
267 : "=r" (__gu_err), "=r" (__gu_tmp) \
268 : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \
270 (val) = (__typeof__(*(addr))) __gu_tmp; \
274 * Get a long long 64 using 32 bit registers.
276 #define __get_user_asm_ll32(val, addr) \
279 unsigned long long l; \
280 __typeof__(*(addr)) t; \
283 __asm__ __volatile__( \
284 "1: lw %1, (%3) \n" \
285 "2: lw %D1, 4(%3) \n" \
286 "3: .section .fixup,\"ax\" \n" \
292 " .section __ex_table,\"a\" \n" \
293 " " __UA_ADDR " 1b, 4b \n" \
294 " " __UA_ADDR " 2b, 4b \n" \
296 : "=r" (__gu_err), "=&r" (__gu_tmp.l) \
297 : "0" (0), "r" (addr), "i" (-EFAULT)); \
299 (val) = __gu_tmp.t; \
303 * Yuck. We need two variants, one for 64bit operation and one
304 * for 32 bit mode and old iron.
307 #define __PUT_USER_DW(ptr) __put_user_asm_ll32(ptr)
310 #define __PUT_USER_DW(ptr) __put_user_asm("sd", ptr)
313 #define __put_user_nocheck(x, ptr, size) \
315 __typeof__(*(ptr)) __pu_val; \
320 case 1: __put_user_asm("sb", ptr); break; \
321 case 2: __put_user_asm("sh", ptr); break; \
322 case 4: __put_user_asm("sw", ptr); break; \
323 case 8: __PUT_USER_DW(ptr); break; \
324 default: __put_user_unknown(); break; \
329 #define __put_user_check(x, ptr, size) \
331 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
332 __typeof__(*(ptr)) __pu_val = (x); \
333 int __pu_err = -EFAULT; \
335 if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) { \
337 case 1: __put_user_asm("sb", __pu_addr); break; \
338 case 2: __put_user_asm("sh", __pu_addr); break; \
339 case 4: __put_user_asm("sw", __pu_addr); break; \
340 case 8: __PUT_USER_DW(__pu_addr); break; \
341 default: __put_user_unknown(); break; \
347 #define __put_user_asm(insn, ptr) \
349 __asm__ __volatile__( \
350 "1: " insn " %z2, %3 # __put_user_asm\n" \
352 " .section .fixup,\"ax\" \n" \
356 " .section __ex_table,\"a\" \n" \
357 " " __UA_ADDR " 1b, 3b \n" \
360 : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)), \
364 #define __put_user_asm_ll32(ptr) \
366 __asm__ __volatile__( \
367 "1: sw %2, (%3) # __put_user_asm_ll32 \n" \
368 "2: sw %D2, 4(%3) \n" \
370 " .section .fixup,\"ax\" \n" \
374 " .section __ex_table,\"a\" \n" \
375 " " __UA_ADDR " 1b, 4b \n" \
376 " " __UA_ADDR " 2b, 4b \n" \
379 : "0" (0), "r" (__pu_val), "r" (ptr), \
383 extern void __put_user_unknown(void);
386 * put_user_unaligned: - Write a simple value into user space.
387 * @x: Value to copy to user space.
388 * @ptr: Destination address, in user space.
390 * Context: User context only. This function may sleep.
392 * This macro copies a single simple value from kernel space to user
393 * space. It supports simple types like char and int, but not larger
394 * data types like structures or arrays.
396 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
397 * to the result of dereferencing @ptr.
399 * Returns zero on success, or -EFAULT on error.
401 #define put_user_unaligned(x,ptr) \
402 __put_user_unaligned_check((x),(ptr),sizeof(*(ptr)))
405 * get_user_unaligned: - Get a simple variable from user space.
406 * @x: Variable to store result.
407 * @ptr: Source address, in user space.
409 * Context: User context only. This function may sleep.
411 * This macro copies a single simple variable from user space to kernel
412 * space. It supports simple types like char and int, but not larger
413 * data types like structures or arrays.
415 * @ptr must have pointer-to-simple-variable type, and the result of
416 * dereferencing @ptr must be assignable to @x without a cast.
418 * Returns zero on success, or -EFAULT on error.
419 * On error, the variable @x is set to zero.
421 #define get_user_unaligned(x,ptr) \
422 __get_user_unaligned_check((x),(ptr),sizeof(*(ptr)))
425 * __put_user_unaligned: - Write a simple value into user space, with less checking.
426 * @x: Value to copy to user space.
427 * @ptr: Destination address, in user space.
429 * Context: User context only. This function may sleep.
431 * This macro copies a single simple value from kernel space to user
432 * space. It supports simple types like char and int, but not larger
433 * data types like structures or arrays.
435 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
436 * to the result of dereferencing @ptr.
438 * Caller must check the pointer with access_ok() before calling this
441 * Returns zero on success, or -EFAULT on error.
443 #define __put_user_unaligned(x,ptr) \
444 __put_user_unaligned_nocheck((x),(ptr),sizeof(*(ptr)))
447 * __get_user_unaligned: - Get a simple variable from user space, with less checking.
448 * @x: Variable to store result.
449 * @ptr: Source address, in user space.
451 * Context: User context only. This function may sleep.
453 * This macro copies a single simple variable from user space to kernel
454 * space. It supports simple types like char and int, but not larger
455 * data types like structures or arrays.
457 * @ptr must have pointer-to-simple-variable type, and the result of
458 * dereferencing @ptr must be assignable to @x without a cast.
460 * Caller must check the pointer with access_ok() before calling this
463 * Returns zero on success, or -EFAULT on error.
464 * On error, the variable @x is set to zero.
466 #define __get_user_unaligned(x,ptr) \
467 __get_user__unalignednocheck((x),(ptr),sizeof(*(ptr)))
470 * Yuck. We need two variants, one for 64bit operation and one
471 * for 32 bit mode and old iron.
474 #define __GET_USER_UNALIGNED_DW(val, ptr) \
475 __get_user_unaligned_asm_ll32(val, ptr)
478 #define __GET_USER_UNALIGNED_DW(val, ptr) \
479 __get_user_unaligned_asm(val, "uld", ptr)
482 extern void __get_user_unaligned_unknown(void);
484 #define __get_user_unaligned_common(val, size, ptr) \
487 case 1: __get_user_asm(val, "lb", ptr); break; \
488 case 2: __get_user_unaligned_asm(val, "ulh", ptr); break; \
489 case 4: __get_user_unaligned_asm(val, "ulw", ptr); break; \
490 case 8: __GET_USER_UNALIGNED_DW(val, ptr); break; \
491 default: __get_user_unaligned_unknown(); break; \
495 #define __get_user_unaligned_nocheck(x,ptr,size) \
499 __get_user_unaligned_common((x), size, ptr); \
503 #define __get_user_unaligned_check(x,ptr,size) \
505 int __gu_err = -EFAULT; \
506 const __typeof__(*(ptr)) __user * __gu_ptr = (ptr); \
508 if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) \
509 __get_user_unaligned_common((x), size, __gu_ptr); \
514 #define __get_user_unaligned_asm(val, insn, addr) \
518 __asm__ __volatile__( \
519 "1: " insn " %1, %3 \n" \
521 " .section .fixup,\"ax\" \n" \
525 " .section __ex_table,\"a\" \n" \
526 " "__UA_ADDR "\t1b, 3b \n" \
527 " "__UA_ADDR "\t1b + 4, 3b \n" \
529 : "=r" (__gu_err), "=r" (__gu_tmp) \
530 : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \
532 (val) = (__typeof__(*(addr))) __gu_tmp; \
536 * Get a long long 64 using 32 bit registers.
538 #define __get_user_unaligned_asm_ll32(val, addr) \
540 unsigned long long __gu_tmp; \
542 __asm__ __volatile__( \
543 "1: ulw %1, (%3) \n" \
544 "2: ulw %D1, 4(%3) \n" \
546 "3: .section .fixup,\"ax\" \n" \
552 " .section __ex_table,\"a\" \n" \
553 " " __UA_ADDR " 1b, 4b \n" \
554 " " __UA_ADDR " 1b + 4, 4b \n" \
555 " " __UA_ADDR " 2b, 4b \n" \
556 " " __UA_ADDR " 2b + 4, 4b \n" \
558 : "=r" (__gu_err), "=&r" (__gu_tmp) \
559 : "0" (0), "r" (addr), "i" (-EFAULT)); \
560 (val) = (__typeof__(*(addr))) __gu_tmp; \
564 * Yuck. We need two variants, one for 64bit operation and one
565 * for 32 bit mode and old iron.
568 #define __PUT_USER_UNALIGNED_DW(ptr) __put_user_unaligned_asm_ll32(ptr)
571 #define __PUT_USER_UNALIGNED_DW(ptr) __put_user_unaligned_asm("usd", ptr)
574 #define __put_user_unaligned_nocheck(x,ptr,size) \
576 __typeof__(*(ptr)) __pu_val; \
581 case 1: __put_user_asm("sb", ptr); break; \
582 case 2: __put_user_unaligned_asm("ush", ptr); break; \
583 case 4: __put_user_unaligned_asm("usw", ptr); break; \
584 case 8: __PUT_USER_UNALIGNED_DW(ptr); break; \
585 default: __put_user_unaligned_unknown(); break; \
590 #define __put_user_unaligned_check(x,ptr,size) \
592 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
593 __typeof__(*(ptr)) __pu_val = (x); \
594 int __pu_err = -EFAULT; \
596 if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) { \
598 case 1: __put_user_asm("sb", __pu_addr); break; \
599 case 2: __put_user_unaligned_asm("ush", __pu_addr); break; \
600 case 4: __put_user_unaligned_asm("usw", __pu_addr); break; \
601 case 8: __PUT_USER_UNALGINED_DW(__pu_addr); break; \
602 default: __put_user_unaligned_unknown(); break; \
608 #define __put_user_unaligned_asm(insn, ptr) \
610 __asm__ __volatile__( \
611 "1: " insn " %z2, %3 # __put_user_unaligned_asm\n" \
613 " .section .fixup,\"ax\" \n" \
617 " .section __ex_table,\"a\" \n" \
618 " " __UA_ADDR " 1b, 3b \n" \
621 : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)), \
625 #define __put_user_unaligned_asm_ll32(ptr) \
627 __asm__ __volatile__( \
628 "1: sw %2, (%3) # __put_user_unaligned_asm_ll32 \n" \
629 "2: sw %D2, 4(%3) \n" \
631 " .section .fixup,\"ax\" \n" \
635 " .section __ex_table,\"a\" \n" \
636 " " __UA_ADDR " 1b, 4b \n" \
637 " " __UA_ADDR " 1b + 4, 4b \n" \
638 " " __UA_ADDR " 2b, 4b \n" \
639 " " __UA_ADDR " 2b + 4, 4b \n" \
642 : "0" (0), "r" (__pu_val), "r" (ptr), \
646 extern void __put_user_unaligned_unknown(void);
649 * We're generating jump to subroutines which will be outside the range of
653 #define __MODULE_JAL(destination) \
655 __UA_LA "\t$1, " #destination "\n\t" \
659 #define __MODULE_JAL(destination) \
660 "jal\t" #destination "\n\t"
663 #ifndef CONFIG_CPU_DADDI_WORKAROUNDS
664 #define DADDI_SCRATCH "$0"
666 #define DADDI_SCRATCH "$3"
669 extern size_t __copy_user(void *__to
, const void *__from
, size_t __n
);
671 #define __invoke_copy_to_user(to, from, n) \
673 register void __user *__cu_to_r __asm__("$4"); \
674 register const void *__cu_from_r __asm__("$5"); \
675 register long __cu_len_r __asm__("$6"); \
678 __cu_from_r = (from); \
680 __asm__ __volatile__( \
681 __MODULE_JAL(__copy_user) \
682 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
684 : "$8", "$9", "$10", "$11", "$12", "$15", "$24", "$31", \
685 DADDI_SCRATCH, "memory"); \
690 * __copy_to_user: - Copy a block of data into user space, with less checking.
691 * @to: Destination address, in user space.
692 * @from: Source address, in kernel space.
693 * @n: Number of bytes to copy.
695 * Context: User context only. This function may sleep.
697 * Copy data from kernel space to user space. Caller must check
698 * the specified block with access_ok() before calling this function.
700 * Returns number of bytes that could not be copied.
701 * On success, this will be zero.
703 #define __copy_to_user(to, from, n) \
705 void __user *__cu_to; \
706 const void *__cu_from; \
711 __cu_from = (from); \
713 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, __cu_len); \
717 extern size_t __copy_user_inatomic(void *__to
, const void *__from
, size_t __n
);
719 #define __copy_to_user_inatomic(to, from, n) \
721 void __user *__cu_to; \
722 const void *__cu_from; \
726 __cu_from = (from); \
728 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, __cu_len); \
732 #define __copy_from_user_inatomic(to, from, n) \
735 const void __user *__cu_from; \
739 __cu_from = (from); \
741 __cu_len = __invoke_copy_from_user_inatomic(__cu_to, __cu_from, \
747 * copy_to_user: - Copy a block of data into user space.
748 * @to: Destination address, in user space.
749 * @from: Source address, in kernel space.
750 * @n: Number of bytes to copy.
752 * Context: User context only. This function may sleep.
754 * Copy data from kernel space to user space.
756 * Returns number of bytes that could not be copied.
757 * On success, this will be zero.
759 #define copy_to_user(to, from, n) \
761 void __user *__cu_to; \
762 const void *__cu_from; \
767 __cu_from = (from); \
769 if (access_ok(VERIFY_WRITE, __cu_to, __cu_len)) \
770 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, \
775 #define __invoke_copy_from_user(to, from, n) \
777 register void *__cu_to_r __asm__("$4"); \
778 register const void __user *__cu_from_r __asm__("$5"); \
779 register long __cu_len_r __asm__("$6"); \
782 __cu_from_r = (from); \
784 __asm__ __volatile__( \
785 ".set\tnoreorder\n\t" \
786 __MODULE_JAL(__copy_user) \
788 __UA_ADDU "\t$1, %1, %2\n\t" \
791 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
793 : "$8", "$9", "$10", "$11", "$12", "$15", "$24", "$31", \
794 DADDI_SCRATCH, "memory"); \
798 #define __invoke_copy_from_user_inatomic(to, from, n) \
800 register void *__cu_to_r __asm__("$4"); \
801 register const void __user *__cu_from_r __asm__("$5"); \
802 register long __cu_len_r __asm__("$6"); \
805 __cu_from_r = (from); \
807 __asm__ __volatile__( \
808 ".set\tnoreorder\n\t" \
809 __MODULE_JAL(__copy_user_inatomic) \
811 __UA_ADDU "\t$1, %1, %2\n\t" \
814 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
816 : "$8", "$9", "$10", "$11", "$12", "$15", "$24", "$31", \
817 DADDI_SCRATCH, "memory"); \
822 * __copy_from_user: - Copy a block of data from user space, with less checking.
823 * @to: Destination address, in kernel space.
824 * @from: Source address, in user space.
825 * @n: Number of bytes to copy.
827 * Context: User context only. This function may sleep.
829 * Copy data from user space to kernel space. Caller must check
830 * the specified block with access_ok() before calling this function.
832 * Returns number of bytes that could not be copied.
833 * On success, this will be zero.
835 * If some data could not be copied, this function will pad the copied
836 * data to the requested size using zero bytes.
838 #define __copy_from_user(to, from, n) \
841 const void __user *__cu_from; \
846 __cu_from = (from); \
848 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
854 * copy_from_user: - Copy a block of data from user space.
855 * @to: Destination address, in kernel space.
856 * @from: Source address, in user space.
857 * @n: Number of bytes to copy.
859 * Context: User context only. This function may sleep.
861 * Copy data from user space to kernel space.
863 * Returns number of bytes that could not be copied.
864 * On success, this will be zero.
866 * If some data could not be copied, this function will pad the copied
867 * data to the requested size using zero bytes.
869 #define copy_from_user(to, from, n) \
872 const void __user *__cu_from; \
877 __cu_from = (from); \
879 if (access_ok(VERIFY_READ, __cu_from, __cu_len)) \
880 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
885 #define __copy_in_user(to, from, n) __copy_from_user(to, from, n)
887 #define copy_in_user(to, from, n) \
889 void __user *__cu_to; \
890 const void __user *__cu_from; \
895 __cu_from = (from); \
897 if (likely(access_ok(VERIFY_READ, __cu_from, __cu_len) && \
898 access_ok(VERIFY_WRITE, __cu_to, __cu_len))) \
899 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
905 * __clear_user: - Zero a block of memory in user space, with less checking.
906 * @to: Destination address, in user space.
907 * @n: Number of bytes to zero.
909 * Zero a block of memory in user space. Caller must check
910 * the specified block with access_ok() before calling this function.
912 * Returns number of bytes that could not be cleared.
913 * On success, this will be zero.
915 static inline __kernel_size_t
916 __clear_user(void __user
*addr
, __kernel_size_t size
)
921 __asm__
__volatile__(
925 __MODULE_JAL(__bzero
)
928 : "r" (addr
), "r" (size
)
929 : "$4", "$5", "$6", __UA_t0
, __UA_t1
, "$31");
934 #define clear_user(addr,n) \
936 void __user * __cl_addr = (addr); \
937 unsigned long __cl_size = (n); \
938 if (__cl_size && access_ok(VERIFY_WRITE, \
939 ((unsigned long)(__cl_addr)), __cl_size)) \
940 __cl_size = __clear_user(__cl_addr, __cl_size); \
945 * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
946 * @dst: Destination address, in kernel space. This buffer must be at
947 * least @count bytes long.
948 * @src: Source address, in user space.
949 * @count: Maximum number of bytes to copy, including the trailing NUL.
951 * Copies a NUL-terminated string from userspace to kernel space.
952 * Caller must check the specified block with access_ok() before calling
955 * On success, returns the length of the string (not including the trailing
958 * If access to userspace fails, returns -EFAULT (some data may have been
961 * If @count is smaller than the length of the string, copies @count bytes
962 * and returns @count.
965 __strncpy_from_user(char *__to
, const char __user
*__from
, long __len
)
970 __asm__
__volatile__(
974 __MODULE_JAL(__strncpy_from_user_nocheck_asm
)
977 : "r" (__to
), "r" (__from
), "r" (__len
)
978 : "$2", "$3", "$4", "$5", "$6", __UA_t0
, "$31", "memory");
984 * strncpy_from_user: - Copy a NUL terminated string from userspace.
985 * @dst: Destination address, in kernel space. This buffer must be at
986 * least @count bytes long.
987 * @src: Source address, in user space.
988 * @count: Maximum number of bytes to copy, including the trailing NUL.
990 * Copies a NUL-terminated string from userspace to kernel space.
992 * On success, returns the length of the string (not including the trailing
995 * If access to userspace fails, returns -EFAULT (some data may have been
998 * If @count is smaller than the length of the string, copies @count bytes
999 * and returns @count.
1002 strncpy_from_user(char *__to
, const char __user
*__from
, long __len
)
1007 __asm__
__volatile__(
1011 __MODULE_JAL(__strncpy_from_user_asm
)
1014 : "r" (__to
), "r" (__from
), "r" (__len
)
1015 : "$2", "$3", "$4", "$5", "$6", __UA_t0
, "$31", "memory");
1020 /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
1021 static inline long __strlen_user(const char __user
*s
)
1026 __asm__
__volatile__(
1028 __MODULE_JAL(__strlen_user_nocheck_asm
)
1032 : "$2", "$4", __UA_t0
, "$31");
1038 * strlen_user: - Get the size of a string in user space.
1039 * @str: The string to measure.
1041 * Context: User context only. This function may sleep.
1043 * Get the size of a NUL-terminated string in user space.
1045 * Returns the size of the string INCLUDING the terminating NUL.
1046 * On exception, returns 0.
1048 * If there is a limit on the length of a valid string, you may wish to
1049 * consider using strnlen_user() instead.
1051 static inline long strlen_user(const char __user
*s
)
1056 __asm__
__volatile__(
1058 __MODULE_JAL(__strlen_user_asm
)
1062 : "$2", "$4", __UA_t0
, "$31");
1067 /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
1068 static inline long __strnlen_user(const char __user
*s
, long n
)
1073 __asm__
__volatile__(
1076 __MODULE_JAL(__strnlen_user_nocheck_asm
)
1080 : "$2", "$4", "$5", __UA_t0
, "$31");
1086 * strlen_user: - Get the size of a string in user space.
1087 * @str: The string to measure.
1089 * Context: User context only. This function may sleep.
1091 * Get the size of a NUL-terminated string in user space.
1093 * Returns the size of the string INCLUDING the terminating NUL.
1094 * On exception, returns 0.
1096 * If there is a limit on the length of a valid string, you may wish to
1097 * consider using strnlen_user() instead.
1099 static inline long strnlen_user(const char __user
*s
, long n
)
1104 __asm__
__volatile__(
1107 __MODULE_JAL(__strnlen_user_asm
)
1111 : "$2", "$4", "$5", __UA_t0
, "$31");
1116 struct exception_table_entry
1119 unsigned long nextinsn
;
1122 extern int fixup_exception(struct pt_regs
*regs
);
1124 #endif /* _ASM_UACCESS_H */