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/alternative.h>
28 #include <asm/cpufeature.h>
29 #include <asm/ptrace.h>
30 #include <asm/sysreg.h>
31 #include <asm/errno.h>
32 #include <asm/memory.h>
33 #include <asm/compiler.h>
36 #define VERIFY_WRITE 1
39 * The exception table consists of pairs of relative offsets: the first
40 * is the relative offset to an instruction that is allowed to fault,
41 * and the second is the relative offset at which the program should
42 * continue. No registers are modified, so it is entirely up to the
43 * continuation code to figure out what to do.
45 * All the routines below use bits of fixup code that are out of line
46 * with the main instruction path. This means when everything is well,
47 * we don't even have to jump over them. Further, they do not intrude
48 * on our cache or tlb entries.
51 struct exception_table_entry
56 #define ARCH_HAS_RELATIVE_EXTABLE
58 extern int fixup_exception(struct pt_regs
*regs
);
60 #define KERNEL_DS (-1UL)
61 #define get_ds() (KERNEL_DS)
63 #define USER_DS TASK_SIZE_64
64 #define get_fs() (current_thread_info()->addr_limit)
66 static inline void set_fs(mm_segment_t fs
)
68 current_thread_info()->addr_limit
= fs
;
71 * Enable/disable UAO so that copy_to_user() etc can access
72 * kernel memory with the unprivileged instructions.
74 if (IS_ENABLED(CONFIG_ARM64_UAO
) && fs
== KERNEL_DS
)
75 asm(ALTERNATIVE("nop", SET_PSTATE_UAO(1), ARM64_HAS_UAO
));
77 asm(ALTERNATIVE("nop", SET_PSTATE_UAO(0), ARM64_HAS_UAO
,
81 #define segment_eq(a, b) ((a) == (b))
84 * Test whether a block of memory is a valid user space address.
85 * Returns 1 if the range is valid, 0 otherwise.
87 * This is equivalent to the following test:
88 * (u65)addr + (u65)size <= current->addr_limit
90 * This needs 65-bit arithmetic.
92 #define __range_ok(addr, size) \
94 unsigned long flag, roksum; \
95 __chk_user_ptr(addr); \
96 asm("adds %1, %1, %3; ccmp %1, %4, #2, cc; cset %0, ls" \
97 : "=&r" (flag), "=&r" (roksum) \
98 : "1" (addr), "Ir" (size), \
99 "r" (current_thread_info()->addr_limit) \
104 #define access_ok(type, addr, size) __range_ok(addr, size)
105 #define user_addr_max get_fs
107 #define _ASM_EXTABLE(from, to) \
108 " .pushsection __ex_table, \"a\"\n" \
110 " .long (" #from " - .), (" #to " - .)\n" \
114 * The "__xxx" versions of the user access functions do not verify the address
115 * space - it must have been done previously with a separate "access_ok()"
118 * The "__xxx_error" versions set the third argument to -EFAULT if an error
119 * occurs, and leave it unchanged on success.
121 #define __get_user_asm(instr, alt_instr, reg, x, addr, err, feature) \
123 "1:"ALTERNATIVE(instr " " reg "1, [%2]\n", \
124 alt_instr " " reg "1, [%2]\n", feature) \
126 " .section .fixup, \"ax\"\n" \
132 _ASM_EXTABLE(1b, 3b) \
133 : "+r" (err), "=&r" (x) \
134 : "r" (addr), "i" (-EFAULT))
136 #define __get_user_err(x, ptr, err) \
138 unsigned long __gu_val; \
139 __chk_user_ptr(ptr); \
140 asm(ALTERNATIVE("nop", SET_PSTATE_PAN(0), ARM64_ALT_PAN_NOT_UAO,\
141 CONFIG_ARM64_PAN)); \
142 switch (sizeof(*(ptr))) { \
144 __get_user_asm("ldrb", "ldtrb", "%w", __gu_val, (ptr), \
145 (err), ARM64_HAS_UAO); \
148 __get_user_asm("ldrh", "ldtrh", "%w", __gu_val, (ptr), \
149 (err), ARM64_HAS_UAO); \
152 __get_user_asm("ldr", "ldtr", "%w", __gu_val, (ptr), \
153 (err), ARM64_HAS_UAO); \
156 __get_user_asm("ldr", "ldtr", "%", __gu_val, (ptr), \
157 (err), ARM64_HAS_UAO); \
162 (x) = (__force __typeof__(*(ptr)))__gu_val; \
163 asm(ALTERNATIVE("nop", SET_PSTATE_PAN(1), ARM64_ALT_PAN_NOT_UAO,\
164 CONFIG_ARM64_PAN)); \
167 #define __get_user(x, ptr) \
170 __get_user_err((x), (ptr), __gu_err); \
174 #define __get_user_error(x, ptr, err) \
176 __get_user_err((x), (ptr), (err)); \
180 #define __get_user_unaligned __get_user
182 #define get_user(x, ptr) \
184 __typeof__(*(ptr)) __user *__p = (ptr); \
186 access_ok(VERIFY_READ, __p, sizeof(*__p)) ? \
187 __get_user((x), __p) : \
188 ((x) = 0, -EFAULT); \
191 #define __put_user_asm(instr, alt_instr, reg, x, addr, err, feature) \
193 "1:"ALTERNATIVE(instr " " reg "1, [%2]\n", \
194 alt_instr " " reg "1, [%2]\n", feature) \
196 " .section .fixup,\"ax\"\n" \
201 _ASM_EXTABLE(1b, 3b) \
203 : "r" (x), "r" (addr), "i" (-EFAULT))
205 #define __put_user_err(x, ptr, err) \
207 __typeof__(*(ptr)) __pu_val = (x); \
208 __chk_user_ptr(ptr); \
209 asm(ALTERNATIVE("nop", SET_PSTATE_PAN(0), ARM64_ALT_PAN_NOT_UAO,\
210 CONFIG_ARM64_PAN)); \
211 switch (sizeof(*(ptr))) { \
213 __put_user_asm("strb", "sttrb", "%w", __pu_val, (ptr), \
214 (err), ARM64_HAS_UAO); \
217 __put_user_asm("strh", "sttrh", "%w", __pu_val, (ptr), \
218 (err), ARM64_HAS_UAO); \
221 __put_user_asm("str", "sttr", "%w", __pu_val, (ptr), \
222 (err), ARM64_HAS_UAO); \
225 __put_user_asm("str", "sttr", "%", __pu_val, (ptr), \
226 (err), ARM64_HAS_UAO); \
231 asm(ALTERNATIVE("nop", SET_PSTATE_PAN(1), ARM64_ALT_PAN_NOT_UAO,\
232 CONFIG_ARM64_PAN)); \
235 #define __put_user(x, ptr) \
238 __put_user_err((x), (ptr), __pu_err); \
242 #define __put_user_error(x, ptr, err) \
244 __put_user_err((x), (ptr), (err)); \
248 #define __put_user_unaligned __put_user
250 #define put_user(x, ptr) \
252 __typeof__(*(ptr)) __user *__p = (ptr); \
254 access_ok(VERIFY_WRITE, __p, sizeof(*__p)) ? \
255 __put_user((x), __p) : \
259 extern unsigned long __must_check
__copy_from_user(void *to
, const void __user
*from
, unsigned long n
);
260 extern unsigned long __must_check
__copy_to_user(void __user
*to
, const void *from
, unsigned long n
);
261 extern unsigned long __must_check
__copy_in_user(void __user
*to
, const void __user
*from
, unsigned long n
);
262 extern unsigned long __must_check
__clear_user(void __user
*addr
, unsigned long n
);
264 static inline unsigned long __must_check
copy_from_user(void *to
, const void __user
*from
, unsigned long n
)
266 if (access_ok(VERIFY_READ
, from
, n
))
267 n
= __copy_from_user(to
, from
, n
);
268 else /* security hole - plug it */
273 static inline unsigned long __must_check
copy_to_user(void __user
*to
, const void *from
, unsigned long n
)
275 if (access_ok(VERIFY_WRITE
, to
, n
))
276 n
= __copy_to_user(to
, from
, n
);
280 static inline unsigned long __must_check
copy_in_user(void __user
*to
, const void __user
*from
, unsigned long n
)
282 if (access_ok(VERIFY_READ
, from
, n
) && access_ok(VERIFY_WRITE
, to
, n
))
283 n
= __copy_in_user(to
, from
, n
);
287 #define __copy_to_user_inatomic __copy_to_user
288 #define __copy_from_user_inatomic __copy_from_user
290 static inline unsigned long __must_check
clear_user(void __user
*to
, unsigned long n
)
292 if (access_ok(VERIFY_WRITE
, to
, n
))
293 n
= __clear_user(to
, n
);
297 extern long strncpy_from_user(char *dest
, const char __user
*src
, long count
);
299 extern __must_check
long strlen_user(const char __user
*str
);
300 extern __must_check
long strnlen_user(const char __user
*str
, long n
);
302 #endif /* __ASM_UACCESS_H */
This page took 0.043292 seconds and 5 git commands to generate.