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
21 #include <asm/alternative.h>
22 #include <asm/kernel-pgtable.h>
23 #include <asm/sysreg.h>
26 * User space memory access functions
28 #include <linux/bitops.h>
29 #include <linux/kasan-checks.h>
30 #include <linux/string.h>
32 #include <asm/cpufeature.h>
33 #include <asm/ptrace.h>
34 #include <asm/memory.h>
35 #include <asm/compiler.h>
36 #include <asm/extable.h>
38 #define KERNEL_DS (-1UL)
39 #define get_ds() (KERNEL_DS)
41 #define USER_DS TASK_SIZE_64
42 #define get_fs() (current_thread_info()->addr_limit)
44 static inline void set_fs(mm_segment_t fs)
46 current_thread_info()->addr_limit = fs;
49 * Enable/disable UAO so that copy_to_user() etc can access
50 * kernel memory with the unprivileged instructions.
52 if (IS_ENABLED(CONFIG_ARM64_UAO) && fs == KERNEL_DS)
53 asm(ALTERNATIVE("nop", SET_PSTATE_UAO(1), ARM64_HAS_UAO));
55 asm(ALTERNATIVE("nop", SET_PSTATE_UAO(0), ARM64_HAS_UAO,
59 #define segment_eq(a, b) ((a) == (b))
62 * Test whether a block of memory is a valid user space address.
63 * Returns 1 if the range is valid, 0 otherwise.
65 * This is equivalent to the following test:
66 * (u65)addr + (u65)size <= current->addr_limit
68 * This needs 65-bit arithmetic.
70 #define __range_ok(addr, size) \
72 unsigned long __addr = (unsigned long)(addr); \
73 unsigned long flag, roksum; \
74 __chk_user_ptr(addr); \
75 asm("adds %1, %1, %3; ccmp %1, %4, #2, cc; cset %0, ls" \
76 : "=&r" (flag), "=&r" (roksum) \
77 : "1" (__addr), "Ir" (size), \
78 "r" (current_thread_info()->addr_limit) \
84 * When dealing with data aborts, watchpoints, or instruction traps we may end
85 * up with a tagged userland pointer. Clear the tag to get a sane pointer to
86 * pass on to access_ok(), for instance.
88 #define untagged_addr(addr) sign_extend64(addr, 55)
90 #define access_ok(type, addr, size) __range_ok(addr, size)
91 #define user_addr_max get_fs
93 #define _ASM_EXTABLE(from, to) \
94 " .pushsection __ex_table, \"a\"\n" \
96 " .long (" #from " - .), (" #to " - .)\n" \
100 * User access enabling/disabling.
102 #ifdef CONFIG_ARM64_SW_TTBR0_PAN
103 static inline void __uaccess_ttbr0_disable(void)
107 /* reserved_ttbr0 placed at the end of swapper_pg_dir */
108 ttbr = read_sysreg(ttbr1_el1) + SWAPPER_DIR_SIZE;
109 write_sysreg(ttbr, ttbr0_el1);
113 static inline void __uaccess_ttbr0_enable(void)
118 * Disable interrupts to avoid preemption between reading the 'ttbr0'
119 * variable and the MSR. A context switch could trigger an ASID
120 * roll-over and an update of 'ttbr0'.
122 local_irq_save(flags);
123 write_sysreg(current_thread_info()->ttbr0, ttbr0_el1);
125 local_irq_restore(flags);
128 static inline bool uaccess_ttbr0_disable(void)
130 if (!system_uses_ttbr0_pan())
132 __uaccess_ttbr0_disable();
136 static inline bool uaccess_ttbr0_enable(void)
138 if (!system_uses_ttbr0_pan())
140 __uaccess_ttbr0_enable();
144 static inline bool uaccess_ttbr0_disable(void)
149 static inline bool uaccess_ttbr0_enable(void)
155 #define __uaccess_disable(alt) \
157 if (!uaccess_ttbr0_disable()) \
158 asm(ALTERNATIVE("nop", SET_PSTATE_PAN(1), alt, \
159 CONFIG_ARM64_PAN)); \
162 #define __uaccess_enable(alt) \
164 if (!uaccess_ttbr0_enable()) \
165 asm(ALTERNATIVE("nop", SET_PSTATE_PAN(0), alt, \
166 CONFIG_ARM64_PAN)); \
169 static inline void uaccess_disable(void)
171 __uaccess_disable(ARM64_HAS_PAN);
174 static inline void uaccess_enable(void)
176 __uaccess_enable(ARM64_HAS_PAN);
180 * These functions are no-ops when UAO is present.
182 static inline void uaccess_disable_not_uao(void)
184 __uaccess_disable(ARM64_ALT_PAN_NOT_UAO);
187 static inline void uaccess_enable_not_uao(void)
189 __uaccess_enable(ARM64_ALT_PAN_NOT_UAO);
193 * The "__xxx" versions of the user access functions do not verify the address
194 * space - it must have been done previously with a separate "access_ok()"
197 * The "__xxx_error" versions set the third argument to -EFAULT if an error
198 * occurs, and leave it unchanged on success.
200 #define __get_user_asm(instr, alt_instr, reg, x, addr, err, feature) \
202 "1:"ALTERNATIVE(instr " " reg "1, [%2]\n", \
203 alt_instr " " reg "1, [%2]\n", feature) \
205 " .section .fixup, \"ax\"\n" \
211 _ASM_EXTABLE(1b, 3b) \
212 : "+r" (err), "=&r" (x) \
213 : "r" (addr), "i" (-EFAULT))
215 #define __get_user_err(x, ptr, err) \
217 unsigned long __gu_val; \
218 __chk_user_ptr(ptr); \
219 uaccess_enable_not_uao(); \
220 switch (sizeof(*(ptr))) { \
222 __get_user_asm("ldrb", "ldtrb", "%w", __gu_val, (ptr), \
223 (err), ARM64_HAS_UAO); \
226 __get_user_asm("ldrh", "ldtrh", "%w", __gu_val, (ptr), \
227 (err), ARM64_HAS_UAO); \
230 __get_user_asm("ldr", "ldtr", "%w", __gu_val, (ptr), \
231 (err), ARM64_HAS_UAO); \
234 __get_user_asm("ldr", "ldtr", "%x", __gu_val, (ptr), \
235 (err), ARM64_HAS_UAO); \
240 uaccess_disable_not_uao(); \
241 (x) = (__force __typeof__(*(ptr)))__gu_val; \
244 #define __get_user(x, ptr) \
247 __get_user_err((x), (ptr), __gu_err); \
251 #define __get_user_error(x, ptr, err) \
253 __get_user_err((x), (ptr), (err)); \
257 #define get_user(x, ptr) \
259 __typeof__(*(ptr)) __user *__p = (ptr); \
261 access_ok(VERIFY_READ, __p, sizeof(*__p)) ? \
262 __get_user((x), __p) : \
263 ((x) = 0, -EFAULT); \
266 #define __put_user_asm(instr, alt_instr, reg, x, addr, err, feature) \
268 "1:"ALTERNATIVE(instr " " reg "1, [%2]\n", \
269 alt_instr " " reg "1, [%2]\n", feature) \
271 " .section .fixup,\"ax\"\n" \
276 _ASM_EXTABLE(1b, 3b) \
278 : "r" (x), "r" (addr), "i" (-EFAULT))
280 #define __put_user_err(x, ptr, err) \
282 __typeof__(*(ptr)) __pu_val = (x); \
283 __chk_user_ptr(ptr); \
284 uaccess_enable_not_uao(); \
285 switch (sizeof(*(ptr))) { \
287 __put_user_asm("strb", "sttrb", "%w", __pu_val, (ptr), \
288 (err), ARM64_HAS_UAO); \
291 __put_user_asm("strh", "sttrh", "%w", __pu_val, (ptr), \
292 (err), ARM64_HAS_UAO); \
295 __put_user_asm("str", "sttr", "%w", __pu_val, (ptr), \
296 (err), ARM64_HAS_UAO); \
299 __put_user_asm("str", "sttr", "%x", __pu_val, (ptr), \
300 (err), ARM64_HAS_UAO); \
305 uaccess_disable_not_uao(); \
308 #define __put_user(x, ptr) \
311 __put_user_err((x), (ptr), __pu_err); \
315 #define __put_user_error(x, ptr, err) \
317 __put_user_err((x), (ptr), (err)); \
321 #define put_user(x, ptr) \
323 __typeof__(*(ptr)) __user *__p = (ptr); \
325 access_ok(VERIFY_WRITE, __p, sizeof(*__p)) ? \
326 __put_user((x), __p) : \
330 extern unsigned long __must_check __arch_copy_from_user(void *to, const void __user *from, unsigned long n);
331 #define raw_copy_from_user __arch_copy_from_user
332 extern unsigned long __must_check __arch_copy_to_user(void __user *to, const void *from, unsigned long n);
333 #define raw_copy_to_user __arch_copy_to_user
334 extern unsigned long __must_check raw_copy_in_user(void __user *to, const void __user *from, unsigned long n);
335 extern unsigned long __must_check __clear_user(void __user *addr, unsigned long n);
336 #define INLINE_COPY_TO_USER
337 #define INLINE_COPY_FROM_USER
339 static inline unsigned long __must_check clear_user(void __user *to, unsigned long n)
341 if (access_ok(VERIFY_WRITE, to, n))
342 n = __clear_user(to, n);
346 extern long strncpy_from_user(char *dest, const char __user *src, long count);
348 extern __must_check long strnlen_user(const char __user *str, long n);
350 #endif /* __ASM_UACCESS_H */