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>
16 #include <asm-generic/uaccess.h>
19 * The fs value determines whether argument validity checking should be
20 * performed or not. If get_fs() == USER_DS, checking is performed, with
21 * get_fs() == KERNEL_DS, checking is bypassed.
23 * For historical reasons, these macros are grossly misnamed.
27 #define __UA_LIMIT 0x80000000UL
29 #define __UA_ADDR ".word"
31 #define __UA_ADDU "addu"
35 #endif /* CONFIG_32BIT */
39 #define __UA_LIMIT (- TASK_SIZE)
41 #define __UA_ADDR ".dword"
43 #define __UA_ADDU "daddu"
47 #endif /* CONFIG_64BIT */
50 * USER_DS is a bitmask that has the bits set that may not be set in a valid
51 * userspace address. Note that we limit 32-bit userspace to 0x7fff8000 but
52 * the arithmetic we're doing only works if the limit is a power of two, so
53 * we use 0x80000000 here on 32-bit kernels. If a process passes an invalid
54 * address in this range it's the process's problem, not ours :-)
57 #define KERNEL_DS ((mm_segment_t) { 0UL })
58 #define USER_DS ((mm_segment_t) { __UA_LIMIT })
61 #define VERIFY_WRITE 1
63 #define get_ds() (KERNEL_DS)
64 #define get_fs() (current_thread_info()->addr_limit)
65 #define set_fs(x) (current_thread_info()->addr_limit = (x))
67 #define segment_eq(a, b) ((a).seg == (b).seg)
71 * Is a address valid? This does a straighforward calculation rather
75 * - "addr" doesn't have any high-bits set
76 * - AND "size" doesn't have any high-bits set
77 * - AND "addr+size" doesn't have any high-bits set
78 * - OR we are in kernel mode.
80 * __ua_size() is a trick to avoid runtime checking of positive constant
81 * sizes; for those we already know at compile time that the size is ok.
83 #define __ua_size(size) \
84 ((__builtin_constant_p(size) && (signed long) (size) > 0) ? 0 : (size))
87 * access_ok: - Checks if a user space pointer is valid
88 * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
89 * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
90 * to write to a block, it is always safe to read from it.
91 * @addr: User space pointer to start of block to check
92 * @size: Size of block to check
94 * Context: User context only. This function may sleep.
96 * Checks if a pointer to a block of memory in user space is valid.
98 * Returns true (nonzero) if the memory block may be valid, false (zero)
99 * if it is definitely invalid.
101 * Note that, depending on architecture, this function probably just
102 * checks that the pointer is in the user space range - after calling
103 * this function, memory access functions may still return -EFAULT.
106 #define __access_mask get_fs().seg
108 #define __access_ok(addr, size, mask) \
109 (((signed long)((mask) & ((addr) | ((addr) + (size)) | __ua_size(size)))) == 0)
111 #define access_ok(type, addr, size) \
112 likely(__access_ok((unsigned long)(addr), (size), __access_mask))
115 * put_user: - Write a simple value into user space.
116 * @x: Value to copy to user space.
117 * @ptr: Destination address, in user space.
119 * Context: User context only. This function may sleep.
121 * This macro copies a single simple value from kernel space to user
122 * space. It supports simple types like char and int, but not larger
123 * data types like structures or arrays.
125 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
126 * to the result of dereferencing @ptr.
128 * Returns zero on success, or -EFAULT on error.
130 #define put_user(x,ptr) \
131 __put_user_check((x), (ptr), sizeof(*(ptr)))
134 * get_user: - Get a simple variable from user space.
135 * @x: Variable to store result.
136 * @ptr: Source address, in user space.
138 * Context: User context only. This function may sleep.
140 * This macro copies a single simple variable from user space to kernel
141 * space. It supports simple types like char and int, but not larger
142 * data types like structures or arrays.
144 * @ptr must have pointer-to-simple-variable type, and the result of
145 * dereferencing @ptr must be assignable to @x without a cast.
147 * Returns zero on success, or -EFAULT on error.
148 * On error, the variable @x is set to zero.
150 #define get_user(x,ptr) \
151 __get_user_check((x), (ptr), sizeof(*(ptr)))
154 * __put_user: - Write a simple value into user space, with less checking.
155 * @x: Value to copy to user space.
156 * @ptr: Destination address, in user space.
158 * Context: User context only. This function may sleep.
160 * This macro copies a single simple value from kernel space to user
161 * space. It supports simple types like char and int, but not larger
162 * data types like structures or arrays.
164 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
165 * to the result of dereferencing @ptr.
167 * Caller must check the pointer with access_ok() before calling this
170 * Returns zero on success, or -EFAULT on error.
172 #define __put_user(x,ptr) \
173 __put_user_nocheck((x), (ptr), sizeof(*(ptr)))
176 * __get_user: - Get a simple variable from user space, with less checking.
177 * @x: Variable to store result.
178 * @ptr: Source address, in user space.
180 * Context: User context only. This function may sleep.
182 * This macro copies a single simple variable from user space to kernel
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 the result of
187 * dereferencing @ptr must be assignable to @x without a cast.
189 * Caller must check the pointer with access_ok() before calling this
192 * Returns zero on success, or -EFAULT on error.
193 * On error, the variable @x is set to zero.
195 #define __get_user(x,ptr) \
196 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
198 struct __large_struct { unsigned long buf[100]; };
199 #define __m(x) (*(struct __large_struct __user *)(x))
202 * Yuck. We need two variants, one for 64bit operation and one
203 * for 32 bit mode and old iron.
206 #define __GET_USER_DW(val, ptr) __get_user_asm_ll32(val, ptr)
209 #define __GET_USER_DW(val, ptr) __get_user_asm(val, "ld", ptr)
212 extern void __get_user_unknown(void);
214 #define __get_user_common(val, size, ptr) \
217 case 1: __get_user_asm(val, "lb", ptr); break; \
218 case 2: __get_user_asm(val, "lh", ptr); break; \
219 case 4: __get_user_asm(val, "lw", ptr); break; \
220 case 8: __GET_USER_DW(val, ptr); break; \
221 default: __get_user_unknown(); break; \
225 #define __get_user_nocheck(x, ptr, size) \
229 __get_user_common((x), size, ptr); \
233 #define __get_user_check(x, ptr, size) \
235 long __gu_err = -EFAULT; \
236 const __typeof__(*(ptr)) __user * __gu_ptr = (ptr); \
238 if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) \
239 __get_user_common((x), size, __gu_ptr); \
244 #define __get_user_asm(val, insn, addr) \
248 __asm__ __volatile__( \
249 "1: " insn " %1, %3 \n" \
251 " .section .fixup,\"ax\" \n" \
255 " .section __ex_table,\"a\" \n" \
256 " "__UA_ADDR "\t1b, 3b \n" \
258 : "=r" (__gu_err), "=r" (__gu_tmp) \
259 : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \
261 (val) = (__typeof__(*(addr))) __gu_tmp; \
265 * Get a long long 64 using 32 bit registers.
267 #define __get_user_asm_ll32(val, addr) \
270 unsigned long long l; \
271 __typeof__(*(addr)) t; \
274 __asm__ __volatile__( \
275 "1: lw %1, (%3) \n" \
276 "2: lw %D1, 4(%3) \n" \
277 "3: .section .fixup,\"ax\" \n" \
283 " .section __ex_table,\"a\" \n" \
284 " " __UA_ADDR " 1b, 4b \n" \
285 " " __UA_ADDR " 2b, 4b \n" \
287 : "=r" (__gu_err), "=&r" (__gu_tmp.l) \
288 : "0" (0), "r" (addr), "i" (-EFAULT)); \
290 (val) = __gu_tmp.t; \
294 * Yuck. We need two variants, one for 64bit operation and one
295 * for 32 bit mode and old iron.
298 #define __PUT_USER_DW(ptr) __put_user_asm_ll32(ptr)
301 #define __PUT_USER_DW(ptr) __put_user_asm("sd", ptr)
304 #define __put_user_nocheck(x, ptr, size) \
306 __typeof__(*(ptr)) __pu_val; \
311 case 1: __put_user_asm("sb", ptr); break; \
312 case 2: __put_user_asm("sh", ptr); break; \
313 case 4: __put_user_asm("sw", ptr); break; \
314 case 8: __PUT_USER_DW(ptr); break; \
315 default: __put_user_unknown(); break; \
320 #define __put_user_check(x, ptr, size) \
322 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
323 __typeof__(*(ptr)) __pu_val = (x); \
324 long __pu_err = -EFAULT; \
326 if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) { \
328 case 1: __put_user_asm("sb", __pu_addr); break; \
329 case 2: __put_user_asm("sh", __pu_addr); break; \
330 case 4: __put_user_asm("sw", __pu_addr); break; \
331 case 8: __PUT_USER_DW(__pu_addr); break; \
332 default: __put_user_unknown(); break; \
338 #define __put_user_asm(insn, ptr) \
340 __asm__ __volatile__( \
341 "1: " insn " %z2, %3 # __put_user_asm\n" \
343 " .section .fixup,\"ax\" \n" \
347 " .section __ex_table,\"a\" \n" \
348 " " __UA_ADDR " 1b, 3b \n" \
351 : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)), \
355 #define __put_user_asm_ll32(ptr) \
357 __asm__ __volatile__( \
358 "1: sw %2, (%3) # __put_user_asm_ll32 \n" \
359 "2: sw %D2, 4(%3) \n" \
361 " .section .fixup,\"ax\" \n" \
365 " .section __ex_table,\"a\" \n" \
366 " " __UA_ADDR " 1b, 4b \n" \
367 " " __UA_ADDR " 2b, 4b \n" \
370 : "0" (0), "r" (__pu_val), "r" (ptr), \
374 extern void __put_user_unknown(void);
377 * We're generating jump to subroutines which will be outside the range of
381 #define __MODULE_JAL(destination) \
383 __UA_LA "\t$1, " #destination "\n\t" \
387 #define __MODULE_JAL(destination) \
388 "jal\t" #destination "\n\t"
391 #ifndef CONFIG_CPU_DADDI_WORKAROUNDS
392 #define DADDI_SCRATCH "$0"
394 #define DADDI_SCRATCH "$3"
397 extern size_t __copy_user(void *__to, const void *__from, size_t __n);
399 #define __invoke_copy_to_user(to, from, n) \
401 register void __user *__cu_to_r __asm__("$4"); \
402 register const void *__cu_from_r __asm__("$5"); \
403 register long __cu_len_r __asm__("$6"); \
406 __cu_from_r = (from); \
408 __asm__ __volatile__( \
409 __MODULE_JAL(__copy_user) \
410 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
412 : "$8", "$9", "$10", "$11", "$12", "$15", "$24", "$31", \
413 DADDI_SCRATCH, "memory"); \
418 * __copy_to_user: - Copy a block of data into user space, with less checking.
419 * @to: Destination address, in user space.
420 * @from: Source address, in kernel space.
421 * @n: Number of bytes to copy.
423 * Context: User context only. This function may sleep.
425 * Copy data from kernel space to user space. Caller must check
426 * the specified block with access_ok() before calling this function.
428 * Returns number of bytes that could not be copied.
429 * On success, this will be zero.
431 #define __copy_to_user(to, from, n) \
433 void __user *__cu_to; \
434 const void *__cu_from; \
439 __cu_from = (from); \
441 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, __cu_len); \
445 extern size_t __copy_user_inatomic(void *__to, const void *__from, size_t __n);
447 #define __copy_to_user_inatomic(to, from, n) \
449 void __user *__cu_to; \
450 const void *__cu_from; \
454 __cu_from = (from); \
456 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, __cu_len); \
460 #define __copy_from_user_inatomic(to, from, n) \
463 const void __user *__cu_from; \
467 __cu_from = (from); \
469 __cu_len = __invoke_copy_from_user_inatomic(__cu_to, __cu_from, \
475 * copy_to_user: - Copy a block of data into user space.
476 * @to: Destination address, in user space.
477 * @from: Source address, in kernel space.
478 * @n: Number of bytes to copy.
480 * Context: User context only. This function may sleep.
482 * Copy data from kernel space to user space.
484 * Returns number of bytes that could not be copied.
485 * On success, this will be zero.
487 #define copy_to_user(to, from, n) \
489 void __user *__cu_to; \
490 const void *__cu_from; \
495 __cu_from = (from); \
497 if (access_ok(VERIFY_WRITE, __cu_to, __cu_len)) \
498 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, \
503 #define __invoke_copy_from_user(to, from, n) \
505 register void *__cu_to_r __asm__("$4"); \
506 register const void __user *__cu_from_r __asm__("$5"); \
507 register long __cu_len_r __asm__("$6"); \
510 __cu_from_r = (from); \
512 __asm__ __volatile__( \
513 ".set\tnoreorder\n\t" \
514 __MODULE_JAL(__copy_user) \
516 __UA_ADDU "\t$1, %1, %2\n\t" \
519 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
521 : "$8", "$9", "$10", "$11", "$12", "$15", "$24", "$31", \
522 DADDI_SCRATCH, "memory"); \
526 #define __invoke_copy_from_user_inatomic(to, from, n) \
528 register void *__cu_to_r __asm__("$4"); \
529 register const void __user *__cu_from_r __asm__("$5"); \
530 register long __cu_len_r __asm__("$6"); \
533 __cu_from_r = (from); \
535 __asm__ __volatile__( \
536 ".set\tnoreorder\n\t" \
537 __MODULE_JAL(__copy_user_inatomic) \
539 __UA_ADDU "\t$1, %1, %2\n\t" \
542 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
544 : "$8", "$9", "$10", "$11", "$12", "$15", "$24", "$31", \
545 DADDI_SCRATCH, "memory"); \
550 * __copy_from_user: - Copy a block of data from user space, with less checking.
551 * @to: Destination address, in kernel space.
552 * @from: Source address, in user space.
553 * @n: Number of bytes to copy.
555 * Context: User context only. This function may sleep.
557 * Copy data from user space to kernel space. Caller must check
558 * the specified block with access_ok() before calling this function.
560 * Returns number of bytes that could not be copied.
561 * On success, this will be zero.
563 * If some data could not be copied, this function will pad the copied
564 * data to the requested size using zero bytes.
566 #define __copy_from_user(to, from, n) \
569 const void __user *__cu_from; \
574 __cu_from = (from); \
576 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
582 * copy_from_user: - Copy a block of data from user space.
583 * @to: Destination address, in kernel space.
584 * @from: Source address, in user space.
585 * @n: Number of bytes to copy.
587 * Context: User context only. This function may sleep.
589 * Copy data from user space to kernel space.
591 * Returns number of bytes that could not be copied.
592 * On success, this will be zero.
594 * If some data could not be copied, this function will pad the copied
595 * data to the requested size using zero bytes.
597 #define copy_from_user(to, from, n) \
600 const void __user *__cu_from; \
605 __cu_from = (from); \
607 if (access_ok(VERIFY_READ, __cu_from, __cu_len)) \
608 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
613 #define __copy_in_user(to, from, n) __copy_from_user(to, from, n)
615 #define copy_in_user(to, from, n) \
617 void __user *__cu_to; \
618 const void __user *__cu_from; \
623 __cu_from = (from); \
625 if (likely(access_ok(VERIFY_READ, __cu_from, __cu_len) && \
626 access_ok(VERIFY_WRITE, __cu_to, __cu_len))) \
627 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
633 * __clear_user: - Zero a block of memory in user space, with less checking.
634 * @to: Destination address, in user space.
635 * @n: Number of bytes to zero.
637 * Zero a block of memory in user space. Caller must check
638 * the specified block with access_ok() before calling this function.
640 * Returns number of bytes that could not be cleared.
641 * On success, this will be zero.
643 static inline __kernel_size_t
644 __clear_user(void __user *addr, __kernel_size_t size)
649 __asm__ __volatile__(
653 __MODULE_JAL(__bzero)
656 : "r" (addr), "r" (size)
657 : "$4", "$5", "$6", __UA_t0, __UA_t1, "$31");
662 #define clear_user(addr,n) \
664 void __user * __cl_addr = (addr); \
665 unsigned long __cl_size = (n); \
666 if (__cl_size && access_ok(VERIFY_WRITE, \
667 ((unsigned long)(__cl_addr)), __cl_size)) \
668 __cl_size = __clear_user(__cl_addr, __cl_size); \
673 * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
674 * @dst: Destination address, in kernel space. This buffer must be at
675 * least @count bytes long.
676 * @src: Source address, in user space.
677 * @count: Maximum number of bytes to copy, including the trailing NUL.
679 * Copies a NUL-terminated string from userspace to kernel space.
680 * Caller must check the specified block with access_ok() before calling
683 * On success, returns the length of the string (not including the trailing
686 * If access to userspace fails, returns -EFAULT (some data may have been
689 * If @count is smaller than the length of the string, copies @count bytes
690 * and returns @count.
693 __strncpy_from_user(char *__to, const char __user *__from, long __len)
698 __asm__ __volatile__(
702 __MODULE_JAL(__strncpy_from_user_nocheck_asm)
705 : "r" (__to), "r" (__from), "r" (__len)
706 : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
712 * strncpy_from_user: - Copy a NUL terminated string from userspace.
713 * @dst: Destination address, in kernel space. This buffer must be at
714 * least @count bytes long.
715 * @src: Source address, in user space.
716 * @count: Maximum number of bytes to copy, including the trailing NUL.
718 * Copies a NUL-terminated string from userspace to kernel space.
720 * On success, returns the length of the string (not including the trailing
723 * If access to userspace fails, returns -EFAULT (some data may have been
726 * If @count is smaller than the length of the string, copies @count bytes
727 * and returns @count.
730 strncpy_from_user(char *__to, const char __user *__from, long __len)
735 __asm__ __volatile__(
739 __MODULE_JAL(__strncpy_from_user_asm)
742 : "r" (__to), "r" (__from), "r" (__len)
743 : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
748 /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
749 static inline long __strlen_user(const char __user *s)
754 __asm__ __volatile__(
756 __MODULE_JAL(__strlen_user_nocheck_asm)
760 : "$2", "$4", __UA_t0, "$31");
766 * strlen_user: - Get the size of a string in user space.
767 * @str: The string to measure.
769 * Context: User context only. This function may sleep.
771 * Get the size of a NUL-terminated string in user space.
773 * Returns the size of the string INCLUDING the terminating NUL.
774 * On exception, returns 0.
776 * If there is a limit on the length of a valid string, you may wish to
777 * consider using strnlen_user() instead.
779 static inline long strlen_user(const char __user *s)
784 __asm__ __volatile__(
786 __MODULE_JAL(__strlen_user_asm)
790 : "$2", "$4", __UA_t0, "$31");
795 /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
796 static inline long __strnlen_user(const char __user *s, long n)
801 __asm__ __volatile__(
804 __MODULE_JAL(__strnlen_user_nocheck_asm)
808 : "$2", "$4", "$5", __UA_t0, "$31");
814 * strlen_user: - Get the size of a string in user space.
815 * @str: The string to measure.
817 * Context: User context only. This function may sleep.
819 * Get the size of a NUL-terminated string in user space.
821 * Returns the size of the string INCLUDING the terminating NUL.
822 * On exception, returns 0.
824 * If there is a limit on the length of a valid string, you may wish to
825 * consider using strnlen_user() instead.
827 static inline long strnlen_user(const char __user *s, long n)
832 __asm__ __volatile__(
835 __MODULE_JAL(__strnlen_user_asm)
839 : "$2", "$4", "$5", __UA_t0, "$31");
844 struct exception_table_entry
847 unsigned long nextinsn;
850 extern int fixup_exception(struct pt_regs *regs);
852 #endif /* _ASM_UACCESS_H */