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/string.h>
16 #include <asm/asm-eva.h>
17 #include <asm/extable.h>
21 #define __UA_LIMIT 0x80000000UL
23 #define __UA_ADDR ".word"
25 #define __UA_ADDU "addu"
29 #endif /* CONFIG_32BIT */
33 extern u64 __ua_limit;
35 #define __UA_LIMIT __ua_limit
37 #define __UA_ADDR ".dword"
39 #define __UA_ADDU "daddu"
43 #endif /* CONFIG_64BIT */
46 * Is a address valid? This does a straightforward calculation rather
50 * - "addr" doesn't have any high-bits set
51 * - AND "size" doesn't have any high-bits set
52 * - AND "addr+size" doesn't have any high-bits set
53 * - OR we are in kernel mode.
55 * __ua_size() is a trick to avoid runtime checking of positive constant
56 * sizes; for those we already know at compile time that the size is ok.
58 #define __ua_size(size) \
59 ((__builtin_constant_p(size) && (signed long) (size) > 0) ? 0 : (size))
62 * access_ok: - Checks if a user space pointer is valid
63 * @addr: User space pointer to start of block to check
64 * @size: Size of block to check
66 * Context: User context only. This function may sleep if pagefaults are
69 * Checks if a pointer to a block of memory in user space is valid.
71 * Returns true (nonzero) if the memory block may be valid, false (zero)
72 * if it is definitely invalid.
74 * Note that, depending on architecture, this function probably just
75 * checks that the pointer is in the user space range - after calling
76 * this function, memory access functions may still return -EFAULT.
79 static inline int __access_ok(const void __user *p, unsigned long size)
81 unsigned long addr = (unsigned long)p;
82 unsigned long end = addr + size - !!size;
84 return (__UA_LIMIT & (addr | end | __ua_size(size))) == 0;
87 #define access_ok(addr, size) \
88 likely(__access_ok((addr), (size)))
91 * put_user: - Write a simple value into user space.
92 * @x: Value to copy to user space.
93 * @ptr: Destination address, in user space.
95 * Context: User context only. This function may sleep if pagefaults are
98 * This macro copies a single simple value from kernel space to user
99 * space. It supports simple types like char and int, but not larger
100 * data types like structures or arrays.
102 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
103 * to the result of dereferencing @ptr.
105 * Returns zero on success, or -EFAULT on error.
107 #define put_user(x, ptr) \
109 __typeof__(*(ptr)) __user *__p = (ptr); \
112 access_ok(__p, sizeof(*__p)) ? __put_user((x), __p) : -EFAULT; \
116 * get_user: - Get a simple variable from user space.
117 * @x: Variable to store result.
118 * @ptr: Source address, in user space.
120 * Context: User context only. This function may sleep if pagefaults are
123 * This macro copies a single simple variable from user space to kernel
124 * space. It supports simple types like char and int, but not larger
125 * data types like structures or arrays.
127 * @ptr must have pointer-to-simple-variable type, and the result of
128 * dereferencing @ptr must be assignable to @x without a cast.
130 * Returns zero on success, or -EFAULT on error.
131 * On error, the variable @x is set to zero.
133 #define get_user(x, ptr) \
135 const __typeof__(*(ptr)) __user *__p = (ptr); \
138 access_ok(__p, sizeof(*__p)) ? __get_user((x), __p) : \
139 ((x) = 0, -EFAULT); \
143 * __put_user: - Write a simple value into user space, with less checking.
144 * @x: Value to copy to user space.
145 * @ptr: Destination address, in user space.
147 * Context: User context only. This function may sleep if pagefaults are
150 * This macro copies a single simple value from kernel space to user
151 * space. It supports simple types like char and int, but not larger
152 * data types like structures or arrays.
154 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
155 * to the result of dereferencing @ptr.
157 * Caller must check the pointer with access_ok() before calling this
160 * Returns zero on success, or -EFAULT on error.
162 #define __put_user(x, ptr) \
164 __typeof__(*(ptr)) __user *__pu_ptr = (ptr); \
165 __typeof__(*(ptr)) __pu_val = (x); \
168 __chk_user_ptr(__pu_ptr); \
169 switch (sizeof(*__pu_ptr)) { \
171 __put_data_asm(user_sb, __pu_ptr); \
174 __put_data_asm(user_sh, __pu_ptr); \
177 __put_data_asm(user_sw, __pu_ptr); \
180 __PUT_DW(user_sd, __pu_ptr); \
190 * __get_user: - Get a simple variable from user space, with less checking.
191 * @x: Variable to store result.
192 * @ptr: Source address, in user space.
194 * Context: User context only. This function may sleep if pagefaults are
197 * This macro copies a single simple variable from user space to kernel
198 * space. It supports simple types like char and int, but not larger
199 * data types like structures or arrays.
201 * @ptr must have pointer-to-simple-variable type, and the result of
202 * dereferencing @ptr must be assignable to @x without a cast.
204 * Caller must check the pointer with access_ok() before calling this
207 * Returns zero on success, or -EFAULT on error.
208 * On error, the variable @x is set to zero.
210 #define __get_user(x, ptr) \
212 const __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
215 __chk_user_ptr(__gu_ptr); \
216 switch (sizeof(*__gu_ptr)) { \
218 __get_data_asm((x), user_lb, __gu_ptr); \
221 __get_data_asm((x), user_lh, __gu_ptr); \
224 __get_data_asm((x), user_lw, __gu_ptr); \
227 __GET_DW((x), user_ld, __gu_ptr); \
236 struct __large_struct { unsigned long buf[100]; };
237 #define __m(x) (*(struct __large_struct __user *)(x))
240 #define __GET_DW(val, insn, ptr) __get_data_asm_ll32(val, insn, ptr)
243 #define __GET_DW(val, insn, ptr) __get_data_asm(val, insn, ptr)
246 #define __get_data_asm(val, insn, addr) \
250 __asm__ __volatile__( \
251 "1: "insn("%1", "%3")" \n" \
254 " .section .fixup,\"ax\" \n" \
259 " .section __ex_table,\"a\" \n" \
260 " "__UA_ADDR "\t1b, 3b \n" \
262 : "=r" (__gu_err), "=r" (__gu_tmp) \
263 : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \
265 (val) = (__typeof__(*(addr))) __gu_tmp; \
269 * Get a long long 64 using 32 bit registers.
271 #define __get_data_asm_ll32(val, insn, addr) \
274 unsigned long long l; \
275 __typeof__(*(addr)) t; \
278 __asm__ __volatile__( \
279 "1: " insn("%1", "(%3)")" \n" \
280 "2: " insn("%D1", "4(%3)")" \n" \
283 " .section .fixup,\"ax\" \n" \
289 " .section __ex_table,\"a\" \n" \
290 " " __UA_ADDR " 1b, 4b \n" \
291 " " __UA_ADDR " 2b, 4b \n" \
293 : "=r" (__gu_err), "=&r" (__gu_tmp.l) \
294 : "0" (0), "r" (addr), "i" (-EFAULT)); \
296 (val) = __gu_tmp.t; \
299 #define HAVE_GET_KERNEL_NOFAULT
301 #define __get_kernel_nofault(dst, src, type, err_label) \
305 switch (sizeof(type)) { \
307 __get_data_asm(*(type *)(dst), kernel_lb, \
308 (__force type *)(src)); \
311 __get_data_asm(*(type *)(dst), kernel_lh, \
312 (__force type *)(src)); \
315 __get_data_asm(*(type *)(dst), kernel_lw, \
316 (__force type *)(src)); \
319 __GET_DW(*(type *)(dst), kernel_ld, \
320 (__force type *)(src)); \
326 if (unlikely(__gu_err)) \
331 * Yuck. We need two variants, one for 64bit operation and one
332 * for 32 bit mode and old iron.
335 #define __PUT_DW(insn, ptr) __put_data_asm_ll32(insn, ptr)
338 #define __PUT_DW(insn, ptr) __put_data_asm(insn, ptr)
341 #define __put_data_asm(insn, ptr) \
343 __asm__ __volatile__( \
344 "1: "insn("%z2", "%3")" # __put_data_asm \n" \
347 " .section .fixup,\"ax\" \n" \
351 " .section __ex_table,\"a\" \n" \
352 " " __UA_ADDR " 1b, 3b \n" \
355 : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)), \
359 #define __put_data_asm_ll32(insn, ptr) \
361 __asm__ __volatile__( \
362 "1: "insn("%2", "(%3)")" # __put_data_asm_ll32 \n" \
363 "2: "insn("%D2", "4(%3)")" \n" \
366 " .section .fixup,\"ax\" \n" \
370 " .section __ex_table,\"a\" \n" \
371 " " __UA_ADDR " 1b, 4b \n" \
372 " " __UA_ADDR " 2b, 4b \n" \
375 : "0" (0), "r" (__pu_val), "r" (ptr), \
379 #define __put_kernel_nofault(dst, src, type, err_label) \
384 __pu_val = *(__force type *)(src); \
385 switch (sizeof(type)) { \
387 __put_data_asm(kernel_sb, (type *)(dst)); \
390 __put_data_asm(kernel_sh, (type *)(dst)); \
393 __put_data_asm(kernel_sw, (type *)(dst)) \
396 __PUT_DW(kernel_sd, (type *)(dst)); \
402 if (unlikely(__pu_err)) \
408 * We're generating jump to subroutines which will be outside the range of
412 #define __MODULE_JAL(destination) \
414 __UA_LA "\t$1, " #destination "\n\t" \
418 #define __MODULE_JAL(destination) \
419 "jal\t" #destination "\n\t"
422 #if defined(CONFIG_CPU_DADDI_WORKAROUNDS) || (defined(CONFIG_EVA) && \
423 defined(CONFIG_CPU_HAS_PREFETCH))
424 #define DADDI_SCRATCH "$3"
426 #define DADDI_SCRATCH "$0"
429 extern size_t __raw_copy_from_user(void *__to, const void *__from, size_t __n);
430 extern size_t __raw_copy_to_user(void *__to, const void *__from, size_t __n);
432 static inline unsigned long
433 raw_copy_from_user(void *to, const void __user *from, unsigned long n)
435 register void *__cu_to_r __asm__("$4");
436 register const void __user *__cu_from_r __asm__("$5");
437 register long __cu_len_r __asm__("$6");
443 __asm__ __volatile__(
444 ".set\tnoreorder\n\t"
445 __MODULE_JAL(__raw_copy_from_user)
447 __UA_ADDU "\t$1, %1, %2\n\t"
450 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r)
452 : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31",
453 DADDI_SCRATCH, "memory");
458 static inline unsigned long
459 raw_copy_to_user(void __user *to, const void *from, unsigned long n)
461 register void __user *__cu_to_r __asm__("$4");
462 register const void *__cu_from_r __asm__("$5");
463 register long __cu_len_r __asm__("$6");
466 __cu_from_r = (from);
469 __asm__ __volatile__(
470 __MODULE_JAL(__raw_copy_to_user)
471 : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r)
473 : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31",
474 DADDI_SCRATCH, "memory");
479 #define INLINE_COPY_FROM_USER
480 #define INLINE_COPY_TO_USER
482 extern __kernel_size_t __bzero(void __user *addr, __kernel_size_t size);
485 * __clear_user: - Zero a block of memory in user space, with less checking.
486 * @to: Destination address, in user space.
487 * @n: Number of bytes to zero.
489 * Zero a block of memory in user space. Caller must check
490 * the specified block with access_ok() before calling this function.
492 * Returns number of bytes that could not be cleared.
493 * On success, this will be zero.
495 static inline __kernel_size_t
496 __clear_user(void __user *addr, __kernel_size_t size)
500 #ifdef CONFIG_CPU_MICROMIPS
501 /* micromips memset / bzero also clobbers t7 & t8 */
502 #define bzero_clobbers "$4", "$5", "$6", __UA_t0, __UA_t1, "$15", "$24", "$31"
504 #define bzero_clobbers "$4", "$5", "$6", __UA_t0, __UA_t1, "$31"
505 #endif /* CONFIG_CPU_MICROMIPS */
508 __asm__ __volatile__(
512 __MODULE_JAL(__bzero)
515 : "r" (addr), "r" (size)
521 #define clear_user(addr,n) \
523 void __user * __cl_addr = (addr); \
524 unsigned long __cl_size = (n); \
525 if (__cl_size && access_ok(__cl_addr, __cl_size)) \
526 __cl_size = __clear_user(__cl_addr, __cl_size); \
530 extern long __strncpy_from_user_asm(char *__to, const char __user *__from, long __len);
533 * strncpy_from_user: - Copy a NUL terminated string from userspace.
534 * @dst: Destination address, in kernel space. This buffer must be at
535 * least @count bytes long.
536 * @src: Source address, in user space.
537 * @count: Maximum number of bytes to copy, including the trailing NUL.
539 * Copies a NUL-terminated string from userspace to kernel space.
541 * On success, returns the length of the string (not including the trailing
544 * If access to userspace fails, returns -EFAULT (some data may have been
547 * If @count is smaller than the length of the string, copies @count bytes
548 * and returns @count.
551 strncpy_from_user(char *__to, const char __user *__from, long __len)
555 if (!access_ok(__from, __len))
559 __asm__ __volatile__(
563 __MODULE_JAL(__strncpy_from_user_asm)
566 : "r" (__to), "r" (__from), "r" (__len)
567 : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
572 extern long __strnlen_user_asm(const char __user *s, long n);
575 * strnlen_user: - Get the size of a string in user space.
576 * @str: The string to measure.
578 * Context: User context only. This function may sleep if pagefaults are
581 * Get the size of a NUL-terminated string in user space.
583 * Returns the size of the string INCLUDING the terminating NUL.
584 * On exception, returns 0.
585 * If the string is too long, returns a value greater than @n.
587 static inline long strnlen_user(const char __user *s, long n)
591 if (!access_ok(s, 1))
595 __asm__ __volatile__(
598 __MODULE_JAL(__strnlen_user_asm)
602 : "$2", "$4", "$5", __UA_t0, "$31");
607 #endif /* _ASM_UACCESS_H */