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>
24 #include <asm/sysreg.h>
29 * User space memory access functions
31 #include <linux/bitops.h>
32 #include <linux/kasan-checks.h>
33 #include <linux/string.h>
34 #include <linux/thread_info.h>
36 #include <asm/cpufeature.h>
37 #include <asm/ptrace.h>
38 #include <asm/errno.h>
39 #include <asm/memory.h>
40 #include <asm/compiler.h>
43 #define VERIFY_WRITE 1
46 * The exception table consists of pairs of relative offsets: the first
47 * is the relative offset to an instruction that is allowed to fault,
48 * and the second is the relative offset at which the program should
49 * continue. No registers are modified, so it is entirely up to the
50 * continuation code to figure out what to do.
52 * All the routines below use bits of fixup code that are out of line
53 * with the main instruction path. This means when everything is well,
54 * we don't even have to jump over them. Further, they do not intrude
55 * on our cache or tlb entries.
58 struct exception_table_entry
63 #define ARCH_HAS_RELATIVE_EXTABLE
65 extern int fixup_exception(struct pt_regs *regs);
67 #define KERNEL_DS (-1UL)
68 #define get_ds() (KERNEL_DS)
70 #define USER_DS TASK_SIZE_64
71 #define get_fs() (current_thread_info()->addr_limit)
73 static inline void set_fs(mm_segment_t fs)
75 current_thread_info()->addr_limit = fs;
78 * Enable/disable UAO so that copy_to_user() etc can access
79 * kernel memory with the unprivileged instructions.
81 if (IS_ENABLED(CONFIG_ARM64_UAO) && fs == KERNEL_DS)
82 asm(ALTERNATIVE("nop", SET_PSTATE_UAO(1), ARM64_HAS_UAO));
84 asm(ALTERNATIVE("nop", SET_PSTATE_UAO(0), ARM64_HAS_UAO,
88 #define segment_eq(a, b) ((a) == (b))
91 * Test whether a block of memory is a valid user space address.
92 * Returns 1 if the range is valid, 0 otherwise.
94 * This is equivalent to the following test:
95 * (u65)addr + (u65)size <= current->addr_limit
97 * This needs 65-bit arithmetic.
99 #define __range_ok(addr, size) \
101 unsigned long __addr = (unsigned long __force)(addr); \
102 unsigned long flag, roksum; \
103 __chk_user_ptr(addr); \
104 asm("adds %1, %1, %3; ccmp %1, %4, #2, cc; cset %0, ls" \
105 : "=&r" (flag), "=&r" (roksum) \
106 : "1" (__addr), "Ir" (size), \
107 "r" (current_thread_info()->addr_limit) \
113 * When dealing with data aborts, watchpoints, or instruction traps we may end
114 * up with a tagged userland pointer. Clear the tag to get a sane pointer to
115 * pass on to access_ok(), for instance.
117 #define untagged_addr(addr) sign_extend64(addr, 55)
119 #define access_ok(type, addr, size) __range_ok(addr, size)
120 #define user_addr_max get_fs
122 #define _ASM_EXTABLE(from, to) \
123 " .pushsection __ex_table, \"a\"\n" \
125 " .long (" #from " - .), (" #to " - .)\n" \
129 * User access enabling/disabling.
131 #ifdef CONFIG_ARM64_SW_TTBR0_PAN
132 static inline void __uaccess_ttbr0_disable(void)
134 unsigned long flags, ttbr;
136 local_irq_save(flags);
137 ttbr = read_sysreg(ttbr1_el1);
138 ttbr &= ~TTBR_ASID_MASK;
139 /* reserved_ttbr0 placed at the end of swapper_pg_dir */
140 write_sysreg(ttbr + SWAPPER_DIR_SIZE, ttbr0_el1);
142 /* Set reserved ASID */
143 write_sysreg(ttbr, ttbr1_el1);
145 local_irq_restore(flags);
148 static inline void __uaccess_ttbr0_enable(void)
150 unsigned long flags, ttbr0, ttbr1;
153 * Disable interrupts to avoid preemption between reading the 'ttbr0'
154 * variable and the MSR. A context switch could trigger an ASID
155 * roll-over and an update of 'ttbr0'.
157 local_irq_save(flags);
158 ttbr0 = READ_ONCE(current_thread_info()->ttbr0);
160 /* Restore active ASID */
161 ttbr1 = read_sysreg(ttbr1_el1);
162 ttbr1 &= ~TTBR_ASID_MASK; /* safety measure */
163 ttbr1 |= ttbr0 & TTBR_ASID_MASK;
164 write_sysreg(ttbr1, ttbr1_el1);
167 /* Restore user page table */
168 write_sysreg(ttbr0, ttbr0_el1);
170 local_irq_restore(flags);
173 static inline bool uaccess_ttbr0_disable(void)
175 if (!system_uses_ttbr0_pan())
177 __uaccess_ttbr0_disable();
181 static inline bool uaccess_ttbr0_enable(void)
183 if (!system_uses_ttbr0_pan())
185 __uaccess_ttbr0_enable();
189 static inline bool uaccess_ttbr0_disable(void)
194 static inline bool uaccess_ttbr0_enable(void)
200 #define __uaccess_disable(alt) \
202 if (!uaccess_ttbr0_disable()) \
203 asm(ALTERNATIVE("nop", SET_PSTATE_PAN(1), alt, \
204 CONFIG_ARM64_PAN)); \
207 #define __uaccess_enable(alt) \
209 if (!uaccess_ttbr0_enable()) \
210 asm(ALTERNATIVE("nop", SET_PSTATE_PAN(0), alt, \
211 CONFIG_ARM64_PAN)); \
214 static inline void uaccess_disable(void)
216 __uaccess_disable(ARM64_HAS_PAN);
219 static inline void uaccess_enable(void)
221 __uaccess_enable(ARM64_HAS_PAN);
225 * These functions are no-ops when UAO is present.
227 static inline void uaccess_disable_not_uao(void)
229 __uaccess_disable(ARM64_ALT_PAN_NOT_UAO);
232 static inline void uaccess_enable_not_uao(void)
234 __uaccess_enable(ARM64_ALT_PAN_NOT_UAO);
238 * The "__xxx" versions of the user access functions do not verify the address
239 * space - it must have been done previously with a separate "access_ok()"
242 * The "__xxx_error" versions set the third argument to -EFAULT if an error
243 * occurs, and leave it unchanged on success.
245 #define __get_user_asm(instr, alt_instr, reg, x, addr, err, feature) \
247 "1:"ALTERNATIVE(instr " " reg "1, [%2]\n", \
248 alt_instr " " reg "1, [%2]\n", feature) \
250 " .section .fixup, \"ax\"\n" \
256 _ASM_EXTABLE(1b, 3b) \
257 : "+r" (err), "=&r" (x) \
258 : "r" (addr), "i" (-EFAULT))
260 #define __get_user_err(x, ptr, err) \
262 unsigned long __gu_val; \
263 __chk_user_ptr(ptr); \
264 uaccess_enable_not_uao(); \
265 switch (sizeof(*(ptr))) { \
267 __get_user_asm("ldrb", "ldtrb", "%w", __gu_val, (ptr), \
268 (err), ARM64_HAS_UAO); \
271 __get_user_asm("ldrh", "ldtrh", "%w", __gu_val, (ptr), \
272 (err), ARM64_HAS_UAO); \
275 __get_user_asm("ldr", "ldtr", "%w", __gu_val, (ptr), \
276 (err), ARM64_HAS_UAO); \
279 __get_user_asm("ldr", "ldtr", "%", __gu_val, (ptr), \
280 (err), ARM64_HAS_UAO); \
285 uaccess_disable_not_uao(); \
286 (x) = (__force __typeof__(*(ptr)))__gu_val; \
289 #define __get_user(x, ptr) \
292 __get_user_err((x), (ptr), __gu_err); \
296 #define __get_user_error(x, ptr, err) \
298 __get_user_err((x), (ptr), (err)); \
302 #define __get_user_unaligned __get_user
304 #define get_user(x, ptr) \
306 __typeof__(*(ptr)) __user *__p = (ptr); \
308 access_ok(VERIFY_READ, __p, sizeof(*__p)) ? \
309 __get_user((x), __p) : \
310 ((x) = 0, -EFAULT); \
313 #define __put_user_asm(instr, alt_instr, reg, x, addr, err, feature) \
315 "1:"ALTERNATIVE(instr " " reg "1, [%2]\n", \
316 alt_instr " " reg "1, [%2]\n", feature) \
318 " .section .fixup,\"ax\"\n" \
323 _ASM_EXTABLE(1b, 3b) \
325 : "r" (x), "r" (addr), "i" (-EFAULT))
327 #define __put_user_err(x, ptr, err) \
329 __typeof__(*(ptr)) __pu_val = (x); \
330 __chk_user_ptr(ptr); \
331 uaccess_enable_not_uao(); \
332 switch (sizeof(*(ptr))) { \
334 __put_user_asm("strb", "sttrb", "%w", __pu_val, (ptr), \
335 (err), ARM64_HAS_UAO); \
338 __put_user_asm("strh", "sttrh", "%w", __pu_val, (ptr), \
339 (err), ARM64_HAS_UAO); \
342 __put_user_asm("str", "sttr", "%w", __pu_val, (ptr), \
343 (err), ARM64_HAS_UAO); \
346 __put_user_asm("str", "sttr", "%", __pu_val, (ptr), \
347 (err), ARM64_HAS_UAO); \
352 uaccess_disable_not_uao(); \
355 #define __put_user(x, ptr) \
358 __put_user_err((x), (ptr), __pu_err); \
362 #define __put_user_error(x, ptr, err) \
364 __put_user_err((x), (ptr), (err)); \
368 #define __put_user_unaligned __put_user
370 #define put_user(x, ptr) \
372 __typeof__(*(ptr)) __user *__p = (ptr); \
374 access_ok(VERIFY_WRITE, __p, sizeof(*__p)) ? \
375 __put_user((x), __p) : \
379 extern unsigned long __must_check __arch_copy_from_user(void *to, const void __user *from, unsigned long n);
380 extern unsigned long __must_check __arch_copy_to_user(void __user *to, const void *from, unsigned long n);
381 extern unsigned long __must_check __copy_in_user(void __user *to, const void __user *from, unsigned long n);
382 extern unsigned long __must_check __clear_user(void __user *addr, unsigned long n);
384 static inline unsigned long __must_check __copy_from_user(void *to, const void __user *from, unsigned long n)
386 kasan_check_write(to, n);
387 check_object_size(to, n, false);
388 return __arch_copy_from_user(to, from, n);
391 static inline unsigned long __must_check __copy_to_user(void __user *to, const void *from, unsigned long n)
393 kasan_check_read(from, n);
394 check_object_size(from, n, true);
395 return __arch_copy_to_user(to, from, n);
398 static inline unsigned long __must_check copy_from_user(void *to, const void __user *from, unsigned long n)
400 unsigned long res = n;
401 kasan_check_write(to, n);
403 if (access_ok(VERIFY_READ, from, n)) {
404 check_object_size(to, n, false);
405 res = __arch_copy_from_user(to, from, n);
408 memset(to + (n - res), 0, res);
412 static inline unsigned long __must_check copy_to_user(void __user *to, const void *from, unsigned long n)
414 kasan_check_read(from, n);
416 if (access_ok(VERIFY_WRITE, to, n)) {
417 check_object_size(from, n, true);
418 n = __arch_copy_to_user(to, from, n);
423 static inline unsigned long __must_check copy_in_user(void __user *to, const void __user *from, unsigned long n)
425 if (access_ok(VERIFY_READ, from, n) && access_ok(VERIFY_WRITE, to, n))
426 n = __copy_in_user(to, from, n);
430 #define __copy_to_user_inatomic __copy_to_user
431 #define __copy_from_user_inatomic __copy_from_user
433 static inline unsigned long __must_check clear_user(void __user *to, unsigned long n)
435 if (access_ok(VERIFY_WRITE, to, n))
436 n = __clear_user(to, n);
440 extern long strncpy_from_user(char *dest, const char __user *src, long count);
442 extern __must_check long strlen_user(const char __user *str);
443 extern __must_check long strnlen_user(const char __user *str, long n);
445 #else /* __ASSEMBLY__ */
447 #include <asm/assembler.h>
450 * User access enabling/disabling macros.
452 #ifdef CONFIG_ARM64_SW_TTBR0_PAN
453 .macro __uaccess_ttbr0_disable, tmp1
454 mrs \tmp1, ttbr1_el1 // swapper_pg_dir
455 bic \tmp1, \tmp1, #TTBR_ASID_MASK
456 add \tmp1, \tmp1, #SWAPPER_DIR_SIZE // reserved_ttbr0 at the end of swapper_pg_dir
457 msr ttbr0_el1, \tmp1 // set reserved TTBR0_EL1
459 sub \tmp1, \tmp1, #SWAPPER_DIR_SIZE
460 msr ttbr1_el1, \tmp1 // set reserved ASID
464 .macro __uaccess_ttbr0_enable, tmp1, tmp2
465 get_thread_info \tmp1
466 ldr \tmp1, [\tmp1, #TSK_TI_TTBR0] // load saved TTBR0_EL1
468 extr \tmp2, \tmp2, \tmp1, #48
469 ror \tmp2, \tmp2, #16
470 msr ttbr1_el1, \tmp2 // set the active ASID
472 msr ttbr0_el1, \tmp1 // set the non-PAN TTBR0_EL1
476 .macro uaccess_ttbr0_disable, tmp1, tmp2
477 alternative_if_not ARM64_HAS_PAN
478 save_and_disable_irq \tmp2 // avoid preemption
479 __uaccess_ttbr0_disable \tmp1
481 alternative_else_nop_endif
484 .macro uaccess_ttbr0_enable, tmp1, tmp2, tmp3
485 alternative_if_not ARM64_HAS_PAN
486 save_and_disable_irq \tmp3 // avoid preemption
487 __uaccess_ttbr0_enable \tmp1, \tmp2
489 alternative_else_nop_endif
492 .macro uaccess_ttbr0_disable, tmp1, tmp2
495 .macro uaccess_ttbr0_enable, tmp1, tmp2, tmp3
500 * These macros are no-ops when UAO is present.
502 .macro uaccess_disable_not_uao, tmp1, tmp2
503 uaccess_ttbr0_disable \tmp1, \tmp2
504 alternative_if ARM64_ALT_PAN_NOT_UAO
506 alternative_else_nop_endif
509 .macro uaccess_enable_not_uao, tmp1, tmp2, tmp3
510 uaccess_ttbr0_enable \tmp1, \tmp2, \tmp3
511 alternative_if ARM64_ALT_PAN_NOT_UAO
513 alternative_else_nop_endif
516 #endif /* __ASSEMBLY__ */
518 #endif /* __ASM_UACCESS_H */