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.
10 #define _ASM_UACCESS_H
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
14 #include <linux/thread_info.h>
15 #include <asm-generic/uaccess.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) \
108 (((signed long)((mask) & ((addr) | ((addr) + (size)) | __ua_size(size)))) == 0)
110 #define access_ok(type, addr, size) \
111 likely(__access_ok((unsigned long)(addr), (size),__access_mask))
114 * put_user: - Write a simple value into user space.
115 * @x: Value to copy to user space.
116 * @ptr: Destination address, in user space.
118 * Context: User context only. This function may sleep.
120 * This macro copies a single simple value from kernel space to user
121 * space. It supports simple types like char and int, but not larger
122 * data types like structures or arrays.
124 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
125 * to the result of dereferencing @ptr.
127 * Returns zero on success, or -EFAULT on error.
129 #define put_user(x,ptr) \
130 __put_user_check((x),(ptr),sizeof(*(ptr)))
133 * get_user: - Get a simple variable from user space.
134 * @x: Variable to store result.
135 * @ptr: Source address, in user space.
137 * Context: User context only. This function may sleep.
139 * This macro copies a single simple variable from user space to kernel
140 * space. It supports simple types like char and int, but not larger
141 * data types like structures or arrays.
143 * @ptr must have pointer-to-simple-variable type, and the result of
144 * dereferencing @ptr must be assignable to @x without a cast.
146 * Returns zero on success, or -EFAULT on error.
147 * On error, the variable @x is set to zero.
149 #define get_user(x,ptr) \
150 __get_user_check((x),(ptr),sizeof(*(ptr)))
153 * __put_user: - Write a simple value into user space, with less checking.
154 * @x: Value to copy to user space.
155 * @ptr: Destination address, in user space.
157 * Context: User context only. This function may sleep.
159 * This macro copies a single simple value from kernel space to user
160 * space. It supports simple types like char and int, but not larger
161 * data types like structures or arrays.
163 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
164 * to the result of dereferencing @ptr.
166 * Caller must check the pointer with access_ok() before calling this
169 * Returns zero on success, or -EFAULT on error.
171 #define __put_user(x,ptr) \
172 __put_user_nocheck((x),(ptr),sizeof(*(ptr)))
175 * __get_user: - Get a simple variable from user space, with less checking.
176 * @x: Variable to store result.
177 * @ptr: Source address, in user space.
179 * Context: User context only. This function may sleep.
181 * This macro copies a single simple variable from user space to kernel
182 * space. It supports simple types like char and int, but not larger
183 * data types like structures or arrays.
185 * @ptr must have pointer-to-simple-variable type, and the result of
186 * dereferencing @ptr must be assignable to @x without a cast.
188 * Caller must check the pointer with access_ok() before calling this
191 * Returns zero on success, or -EFAULT on error.
192 * On error, the variable @x is set to zero.
194 #define __get_user(x,ptr) \
195 __get_user_nocheck((x),(ptr),sizeof(*(ptr)))
197 struct __large_struct { unsigned long buf[100]; };
198 #define __m(x) (*(struct __large_struct __user *)(x))
201 * Yuck. We need two variants, one for 64bit operation and one
202 * for 32 bit mode and old iron.
205 #define __GET_USER_DW(val, ptr) __get_user_asm_ll32(val, ptr)
208 #define __GET_USER_DW(val, ptr) __get_user_asm(val, "ld", ptr)
211 extern void __get_user_unknown(void);
213 #define __get_user_common(val, size, ptr) \
216 case 1: __get_user_asm(val, "lb", ptr); break; \
217 case 2: __get_user_asm(val, "lh", ptr); break; \
218 case 4: __get_user_asm(val, "lw", ptr); break; \
219 case 8: __GET_USER_DW(val, ptr); break; \
220 default: __get_user_unknown(); break; \
224 #define __get_user_nocheck(x,ptr,size) \
228 __get_user_common((x), size, ptr); \
232 #define __get_user_check(x,ptr,size) \
234 long __gu_err = -EFAULT; \
235 const __typeof__(*(ptr)) __user * __gu_ptr = (ptr); \
237 if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) \
238 __get_user_common((x), size, __gu_ptr); \
243 #define __get_user_asm(val, insn, addr) \
247 __asm__ __volatile__( \
248 "1: " insn " %1, %3 \n" \
250 " .section .fixup,\"ax\" \n" \
254 " .section __ex_table,\"a\" \n" \
255 " "__UA_ADDR "\t1b, 3b \n" \
257 : "=r" (__gu_err), "=r" (__gu_tmp) \
258 : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \
260 (val) = (__typeof__(*(addr))) __gu_tmp; \
264 * Get a long long 64 using 32 bit registers.
266 #define __get_user_asm_ll32(val, addr) \
269 unsigned long long l; \
270 __typeof__(*(addr)) t; \
273 __asm__ __volatile__( \
274 "1: lw %1, (%3) \n" \
275 "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 extern size_t __copy_user(void *__to, const void *__from, size_t __n);
393 #define __invoke_copy_to_user(to,from,n) \
395 register void __user *__cu_to_r __asm__ ("$4"); \
396 register const void *__cu_from_r __asm__ ("$5"); \
397 register long __cu_len_r __asm__ ("$6"); \
400 __cu_from_r = (from); \
402 __asm__ __volatile__( \
403 __MODULE_JAL(__copy_user) \
404 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
406 : "$8", "$9", "$10", "$11", "$12", "$15", "$24", "$31", \
412 * __copy_to_user: - Copy a block of data into user space, with less checking.
413 * @to: Destination address, in user space.
414 * @from: Source address, in kernel space.
415 * @n: Number of bytes to copy.
417 * Context: User context only. This function may sleep.
419 * Copy data from kernel space to user space. Caller must check
420 * the specified block with access_ok() before calling this function.
422 * Returns number of bytes that could not be copied.
423 * On success, this will be zero.
425 #define __copy_to_user(to,from,n) \
427 void __user *__cu_to; \
428 const void *__cu_from; \
433 __cu_from = (from); \
435 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, __cu_len); \
439 #define __copy_to_user_inatomic __copy_to_user
440 #define __copy_from_user_inatomic __copy_from_user
443 * copy_to_user: - Copy a block of data into user space.
444 * @to: Destination address, in user space.
445 * @from: Source address, in kernel space.
446 * @n: Number of bytes to copy.
448 * Context: User context only. This function may sleep.
450 * Copy data from kernel space to user space.
452 * Returns number of bytes that could not be copied.
453 * On success, this will be zero.
455 #define copy_to_user(to,from,n) \
457 void __user *__cu_to; \
458 const void *__cu_from; \
463 __cu_from = (from); \
465 if (access_ok(VERIFY_WRITE, __cu_to, __cu_len)) \
466 __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, \
471 #define __invoke_copy_from_user(to,from,n) \
473 register void *__cu_to_r __asm__ ("$4"); \
474 register const void __user *__cu_from_r __asm__ ("$5"); \
475 register long __cu_len_r __asm__ ("$6"); \
478 __cu_from_r = (from); \
480 __asm__ __volatile__( \
481 ".set\tnoreorder\n\t" \
482 __MODULE_JAL(__copy_user) \
484 __UA_ADDU "\t$1, %1, %2\n\t" \
487 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
489 : "$8", "$9", "$10", "$11", "$12", "$15", "$24", "$31", \
495 * __copy_from_user: - Copy a block of data from user space, with less checking.
496 * @to: Destination address, in kernel space.
497 * @from: Source address, in user space.
498 * @n: Number of bytes to copy.
500 * Context: User context only. This function may sleep.
502 * Copy data from user space to kernel space. Caller must check
503 * the specified block with access_ok() before calling this function.
505 * Returns number of bytes that could not be copied.
506 * On success, this will be zero.
508 * If some data could not be copied, this function will pad the copied
509 * data to the requested size using zero bytes.
511 #define __copy_from_user(to,from,n) \
514 const void __user *__cu_from; \
519 __cu_from = (from); \
521 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
527 * copy_from_user: - Copy a block of data from user space.
528 * @to: Destination address, in kernel space.
529 * @from: Source address, in user space.
530 * @n: Number of bytes to copy.
532 * Context: User context only. This function may sleep.
534 * Copy data from user space to kernel space.
536 * Returns number of bytes that could not be copied.
537 * On success, this will be zero.
539 * If some data could not be copied, this function will pad the copied
540 * data to the requested size using zero bytes.
542 #define copy_from_user(to,from,n) \
545 const void __user *__cu_from; \
550 __cu_from = (from); \
552 if (access_ok(VERIFY_READ, __cu_from, __cu_len)) \
553 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
558 #define __copy_in_user(to, from, n) __copy_from_user(to, from, n)
560 #define copy_in_user(to,from,n) \
562 void __user *__cu_to; \
563 const void __user *__cu_from; \
568 __cu_from = (from); \
570 if (likely(access_ok(VERIFY_READ, __cu_from, __cu_len) && \
571 access_ok(VERIFY_WRITE, __cu_to, __cu_len))) \
572 __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
578 * __clear_user: - Zero a block of memory in user space, with less checking.
579 * @to: Destination address, in user space.
580 * @n: Number of bytes to zero.
582 * Zero a block of memory in user space. Caller must check
583 * the specified block with access_ok() before calling this function.
585 * Returns number of bytes that could not be cleared.
586 * On success, this will be zero.
588 static inline __kernel_size_t
589 __clear_user(void __user *addr, __kernel_size_t size)
594 __asm__ __volatile__(
598 __MODULE_JAL(__bzero)
601 : "r" (addr), "r" (size)
602 : "$4", "$5", "$6", __UA_t0, __UA_t1, "$31");
607 #define clear_user(addr,n) \
609 void __user * __cl_addr = (addr); \
610 unsigned long __cl_size = (n); \
611 if (__cl_size && access_ok(VERIFY_WRITE, \
612 ((unsigned long)(__cl_addr)), __cl_size)) \
613 __cl_size = __clear_user(__cl_addr, __cl_size); \
618 * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
619 * @dst: Destination address, in kernel space. This buffer must be at
620 * least @count bytes long.
621 * @src: Source address, in user space.
622 * @count: Maximum number of bytes to copy, including the trailing NUL.
624 * Copies a NUL-terminated string from userspace to kernel space.
625 * Caller must check the specified block with access_ok() before calling
628 * On success, returns the length of the string (not including the trailing
631 * If access to userspace fails, returns -EFAULT (some data may have been
634 * If @count is smaller than the length of the string, copies @count bytes
635 * and returns @count.
638 __strncpy_from_user(char *__to, const char __user *__from, long __len)
643 __asm__ __volatile__(
647 __MODULE_JAL(__strncpy_from_user_nocheck_asm)
650 : "r" (__to), "r" (__from), "r" (__len)
651 : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
657 * strncpy_from_user: - Copy a NUL terminated string from userspace.
658 * @dst: Destination address, in kernel space. This buffer must be at
659 * least @count bytes long.
660 * @src: Source address, in user space.
661 * @count: Maximum number of bytes to copy, including the trailing NUL.
663 * Copies a NUL-terminated string from userspace to kernel space.
665 * On success, returns the length of the string (not including the trailing
668 * If access to userspace fails, returns -EFAULT (some data may have been
671 * If @count is smaller than the length of the string, copies @count bytes
672 * and returns @count.
675 strncpy_from_user(char *__to, const char __user *__from, long __len)
680 __asm__ __volatile__(
684 __MODULE_JAL(__strncpy_from_user_asm)
687 : "r" (__to), "r" (__from), "r" (__len)
688 : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
693 /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
694 static inline long __strlen_user(const char __user *s)
699 __asm__ __volatile__(
701 __MODULE_JAL(__strlen_user_nocheck_asm)
705 : "$2", "$4", __UA_t0, "$31");
711 * strlen_user: - Get the size of a string in user space.
712 * @str: The string to measure.
714 * Context: User context only. This function may sleep.
716 * Get the size of a NUL-terminated string in user space.
718 * Returns the size of the string INCLUDING the terminating NUL.
719 * On exception, returns 0.
721 * If there is a limit on the length of a valid string, you may wish to
722 * consider using strnlen_user() instead.
724 static inline long strlen_user(const char __user *s)
729 __asm__ __volatile__(
731 __MODULE_JAL(__strlen_user_asm)
735 : "$2", "$4", __UA_t0, "$31");
740 /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
741 static inline long __strnlen_user(const char __user *s, long n)
746 __asm__ __volatile__(
749 __MODULE_JAL(__strnlen_user_nocheck_asm)
753 : "$2", "$4", "$5", __UA_t0, "$31");
759 * strlen_user: - Get the size of a string in user space.
760 * @str: The string to measure.
762 * Context: User context only. This function may sleep.
764 * Get the size of a NUL-terminated string in user space.
766 * Returns the size of the string INCLUDING the terminating NUL.
767 * On exception, returns 0.
769 * If there is a limit on the length of a valid string, you may wish to
770 * consider using strnlen_user() instead.
772 static inline long strnlen_user(const char __user *s, long n)
777 __asm__ __volatile__(
780 __MODULE_JAL(__strnlen_user_asm)
784 : "$2", "$4", "$5", __UA_t0, "$31");
789 struct exception_table_entry
792 unsigned long nextinsn;
795 extern int fixup_exception(struct pt_regs *regs);
797 #endif /* _ASM_UACCESS_H */