2 * Based on arch/arm/include/asm/uaccess.h
4 * Copyright (C) 2012 ARM Ltd.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #ifndef __ASM_UACCESS_H
19 #define __ASM_UACCESS_H
22 * User space memory access functions
24 #include <linux/string.h>
25 #include <linux/thread_info.h>
27 #include <asm/ptrace.h>
28 #include <asm/errno.h>
29 #include <asm/memory.h>
30 #include <asm/compiler.h>
33 #define VERIFY_WRITE 1
36 * The exception table consists of pairs of addresses: the first is the
37 * address of an instruction that is allowed to fault, and the second is
38 * the address at which the program should continue. No registers are
39 * modified, so it is entirely up to the continuation code to figure out
42 * All the routines below use bits of fixup code that are out of line
43 * with the main instruction path. This means when everything is well,
44 * we don't even have to jump over them. Further, they do not intrude
45 * on our cache or tlb entries.
48 struct exception_table_entry
50 unsigned long insn, fixup;
53 extern int fixup_exception(struct pt_regs *regs);
55 #define KERNEL_DS (-1UL)
56 #define get_ds() (KERNEL_DS)
58 #define USER_DS TASK_SIZE_64
59 #define get_fs() (current_thread_info()->addr_limit)
61 static inline void set_fs(mm_segment_t fs)
63 current_thread_info()->addr_limit = fs;
66 #define segment_eq(a,b) ((a) == (b))
69 * Return 1 if addr < current->addr_limit, 0 otherwise.
71 #define __addr_ok(addr) \
74 asm("cmp %1, %0; cset %0, lo" \
76 : "r" (addr), "0" (current_thread_info()->addr_limit) \
82 * Test whether a block of memory is a valid user space address.
83 * Returns 1 if the range is valid, 0 otherwise.
85 * This is equivalent to the following test:
86 * (u65)addr + (u65)size <= current->addr_limit
88 * This needs 65-bit arithmetic.
90 #define __range_ok(addr, size) \
92 unsigned long flag, roksum; \
93 __chk_user_ptr(addr); \
94 asm("adds %1, %1, %3; ccmp %1, %4, #2, cc; cset %0, ls" \
95 : "=&r" (flag), "=&r" (roksum) \
96 : "1" (addr), "Ir" (size), \
97 "r" (current_thread_info()->addr_limit) \
102 #define access_ok(type, addr, size) __range_ok(addr, size)
103 #define user_addr_max get_fs
106 * The "__xxx" versions of the user access functions do not verify the address
107 * space - it must have been done previously with a separate "access_ok()"
110 * The "__xxx_error" versions set the third argument to -EFAULT if an error
111 * occurs, and leave it unchanged on success.
113 #define __get_user_asm(instr, reg, x, addr, err) \
115 "1: " instr " " reg "1, [%2]\n" \
117 " .section .fixup, \"ax\"\n" \
123 " .section __ex_table,\"a\"\n" \
127 : "+r" (err), "=&r" (x) \
128 : "r" (addr), "i" (-EFAULT))
130 #define __get_user_err(x, ptr, err) \
132 unsigned long __gu_val; \
133 __chk_user_ptr(ptr); \
134 switch (sizeof(*(ptr))) { \
136 __get_user_asm("ldrb", "%w", __gu_val, (ptr), (err)); \
139 __get_user_asm("ldrh", "%w", __gu_val, (ptr), (err)); \
142 __get_user_asm("ldr", "%w", __gu_val, (ptr), (err)); \
145 __get_user_asm("ldr", "%", __gu_val, (ptr), (err)); \
150 (x) = (__typeof__(*(ptr)))__gu_val; \
153 #define __get_user(x, ptr) \
156 __get_user_err((x), (ptr), __gu_err); \
160 #define __get_user_error(x, ptr, err) \
162 __get_user_err((x), (ptr), (err)); \
166 #define __get_user_unaligned __get_user
168 #define get_user(x, ptr) \
170 __typeof__(*(ptr)) __user *__p = (ptr); \
172 access_ok(VERIFY_READ, __p, sizeof(*__p)) ? \
173 __get_user((x), __p) : \
174 ((x) = 0, -EFAULT); \
177 #define __put_user_asm(instr, reg, x, addr, err) \
179 "1: " instr " " reg "1, [%2]\n" \
181 " .section .fixup,\"ax\"\n" \
186 " .section __ex_table,\"a\"\n" \
191 : "r" (x), "r" (addr), "i" (-EFAULT))
193 #define __put_user_err(x, ptr, err) \
195 __typeof__(*(ptr)) __pu_val = (x); \
196 __chk_user_ptr(ptr); \
197 switch (sizeof(*(ptr))) { \
199 __put_user_asm("strb", "%w", __pu_val, (ptr), (err)); \
202 __put_user_asm("strh", "%w", __pu_val, (ptr), (err)); \
205 __put_user_asm("str", "%w", __pu_val, (ptr), (err)); \
208 __put_user_asm("str", "%", __pu_val, (ptr), (err)); \
215 #define __put_user(x, ptr) \
218 __put_user_err((x), (ptr), __pu_err); \
222 #define __put_user_error(x, ptr, err) \
224 __put_user_err((x), (ptr), (err)); \
228 #define __put_user_unaligned __put_user
230 #define put_user(x, ptr) \
232 __typeof__(*(ptr)) __user *__p = (ptr); \
234 access_ok(VERIFY_WRITE, __p, sizeof(*__p)) ? \
235 __put_user((x), __p) : \
239 extern unsigned long __must_check __copy_from_user(void *to, const void __user *from, unsigned long n);
240 extern unsigned long __must_check __copy_to_user(void __user *to, const void *from, unsigned long n);
241 extern unsigned long __must_check __copy_in_user(void __user *to, const void __user *from, unsigned long n);
242 extern unsigned long __must_check __clear_user(void __user *addr, unsigned long n);
244 static inline unsigned long __must_check copy_from_user(void *to, const void __user *from, unsigned long n)
246 if (access_ok(VERIFY_READ, from, n))
247 n = __copy_from_user(to, from, n);
248 else /* security hole - plug it */
253 static inline unsigned long __must_check copy_to_user(void __user *to, const void *from, unsigned long n)
255 if (access_ok(VERIFY_WRITE, to, n))
256 n = __copy_to_user(to, from, n);
260 static inline unsigned long __must_check copy_in_user(void __user *to, const void __user *from, unsigned long n)
262 if (access_ok(VERIFY_READ, from, n) && access_ok(VERIFY_WRITE, to, n))
263 n = __copy_in_user(to, from, n);
267 #define __copy_to_user_inatomic __copy_to_user
268 #define __copy_from_user_inatomic __copy_from_user
270 static inline unsigned long __must_check clear_user(void __user *to, unsigned long n)
272 if (access_ok(VERIFY_WRITE, to, n))
273 n = __clear_user(to, n);
277 extern long strncpy_from_user(char *dest, const char __user *src, long count);
279 extern __must_check long strlen_user(const char __user *str);
280 extern __must_check long strnlen_user(const char __user *str, long n);
282 #endif /* __ASM_UACCESS_H */