1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Based on arch/arm/include/asm/uaccess.h
5 * Copyright (C) 2012 ARM Ltd.
7 #ifndef __ASM_UACCESS_H
8 #define __ASM_UACCESS_H
10 #include <asm/alternative.h>
11 #include <asm/kernel-pgtable.h>
12 #include <asm/sysreg.h>
15 * User space memory access functions
17 #include <linux/bitops.h>
18 #include <linux/kasan-checks.h>
19 #include <linux/string.h>
21 #include <asm/asm-extable.h>
22 #include <asm/cpufeature.h>
25 #include <asm/ptrace.h>
26 #include <asm/memory.h>
27 #include <asm/extable.h>
29 static inline int __access_ok(const void __user *ptr, unsigned long size);
32 * Test whether a block of memory is a valid user space address.
33 * Returns 1 if the range is valid, 0 otherwise.
35 * This is equivalent to the following test:
36 * (u65)addr + (u65)size <= (u65)TASK_SIZE_MAX
38 static inline int access_ok(const void __user *addr, unsigned long size)
41 * Asynchronous I/O running in a kernel thread does not have the
42 * TIF_TAGGED_ADDR flag of the process owning the mm, so always untag
43 * the user address before checking.
45 if (IS_ENABLED(CONFIG_ARM64_TAGGED_ADDR_ABI) &&
46 (current->flags & PF_KTHREAD || test_thread_flag(TIF_TAGGED_ADDR)))
47 addr = untagged_addr(addr);
49 return likely(__access_ok(addr, size));
51 #define access_ok access_ok
53 #include <asm-generic/access_ok.h>
56 * User access enabling/disabling.
58 #ifdef CONFIG_ARM64_SW_TTBR0_PAN
59 static inline void __uaccess_ttbr0_disable(void)
61 unsigned long flags, ttbr;
63 local_irq_save(flags);
64 ttbr = read_sysreg(ttbr1_el1);
65 ttbr &= ~TTBR_ASID_MASK;
66 /* reserved_pg_dir placed before swapper_pg_dir */
67 write_sysreg(ttbr - RESERVED_SWAPPER_OFFSET, ttbr0_el1);
69 /* Set reserved ASID */
70 write_sysreg(ttbr, ttbr1_el1);
72 local_irq_restore(flags);
75 static inline void __uaccess_ttbr0_enable(void)
77 unsigned long flags, ttbr0, ttbr1;
80 * Disable interrupts to avoid preemption between reading the 'ttbr0'
81 * variable and the MSR. A context switch could trigger an ASID
82 * roll-over and an update of 'ttbr0'.
84 local_irq_save(flags);
85 ttbr0 = READ_ONCE(current_thread_info()->ttbr0);
87 /* Restore active ASID */
88 ttbr1 = read_sysreg(ttbr1_el1);
89 ttbr1 &= ~TTBR_ASID_MASK; /* safety measure */
90 ttbr1 |= ttbr0 & TTBR_ASID_MASK;
91 write_sysreg(ttbr1, ttbr1_el1);
94 /* Restore user page table */
95 write_sysreg(ttbr0, ttbr0_el1);
97 local_irq_restore(flags);
100 static inline bool uaccess_ttbr0_disable(void)
102 if (!system_uses_ttbr0_pan())
104 __uaccess_ttbr0_disable();
108 static inline bool uaccess_ttbr0_enable(void)
110 if (!system_uses_ttbr0_pan())
112 __uaccess_ttbr0_enable();
116 static inline bool uaccess_ttbr0_disable(void)
121 static inline bool uaccess_ttbr0_enable(void)
127 static inline void __uaccess_disable_hw_pan(void)
129 asm(ALTERNATIVE("nop", SET_PSTATE_PAN(0), ARM64_HAS_PAN,
133 static inline void __uaccess_enable_hw_pan(void)
135 asm(ALTERNATIVE("nop", SET_PSTATE_PAN(1), ARM64_HAS_PAN,
140 * The Tag Check Flag (TCF) mode for MTE is per EL, hence TCF0
141 * affects EL0 and TCF affects EL1 irrespective of which TTBR is
143 * The kernel accesses TTBR0 usually with LDTR/STTR instructions
144 * when UAO is available, so these would act as EL0 accesses using
146 * However futex.h code uses exclusives which would be executed as
147 * EL1, this can potentially cause a tag check fault even if the
148 * user disables TCF0.
150 * To address the problem we set the PSTATE.TCO bit in uaccess_enable()
151 * and reset it in uaccess_disable().
153 * The Tag check override (TCO) bit disables temporarily the tag checking
154 * preventing the issue.
156 static inline void __uaccess_disable_tco(void)
158 asm volatile(ALTERNATIVE("nop", SET_PSTATE_TCO(0),
159 ARM64_MTE, CONFIG_KASAN_HW_TAGS));
162 static inline void __uaccess_enable_tco(void)
164 asm volatile(ALTERNATIVE("nop", SET_PSTATE_TCO(1),
165 ARM64_MTE, CONFIG_KASAN_HW_TAGS));
169 * These functions disable tag checking only if in MTE async mode
170 * since the sync mode generates exceptions synchronously and the
171 * nofault or load_unaligned_zeropad can handle them.
173 static inline void __uaccess_disable_tco_async(void)
175 if (system_uses_mte_async_or_asymm_mode())
176 __uaccess_disable_tco();
179 static inline void __uaccess_enable_tco_async(void)
181 if (system_uses_mte_async_or_asymm_mode())
182 __uaccess_enable_tco();
185 static inline void uaccess_disable_privileged(void)
187 __uaccess_disable_tco();
189 if (uaccess_ttbr0_disable())
192 __uaccess_enable_hw_pan();
195 static inline void uaccess_enable_privileged(void)
197 __uaccess_enable_tco();
199 if (uaccess_ttbr0_enable())
202 __uaccess_disable_hw_pan();
206 * Sanitise a uaccess pointer such that it becomes NULL if above the maximum
207 * user address. In case the pointer is tagged (has the top byte set), untag
208 * the pointer before checking.
210 #define uaccess_mask_ptr(ptr) (__typeof__(ptr))__uaccess_mask_ptr(ptr)
211 static inline void __user *__uaccess_mask_ptr(const void __user *ptr)
213 void __user *safe_ptr;
216 " bics xzr, %3, %2\n"
217 " csel %0, %1, xzr, eq\n"
219 : "r" (ptr), "r" (TASK_SIZE_MAX - 1),
220 "r" (untagged_addr(ptr))
228 * The "__xxx" versions of the user access functions do not verify the address
229 * space - it must have been done previously with a separate "access_ok()"
232 * The "__xxx_error" versions set the third argument to -EFAULT if an error
233 * occurs, and leave it unchanged on success.
235 #define __get_mem_asm(load, reg, x, addr, err) \
237 "1: " load " " reg "1, [%2]\n" \
239 _ASM_EXTABLE_UACCESS_ERR_ZERO(1b, 2b, %w0, %w1) \
240 : "+r" (err), "=&r" (x) \
243 #define __raw_get_mem(ldr, x, ptr, err) \
245 unsigned long __gu_val; \
246 switch (sizeof(*(ptr))) { \
248 __get_mem_asm(ldr "b", "%w", __gu_val, (ptr), (err)); \
251 __get_mem_asm(ldr "h", "%w", __gu_val, (ptr), (err)); \
254 __get_mem_asm(ldr, "%w", __gu_val, (ptr), (err)); \
257 __get_mem_asm(ldr, "%x", __gu_val, (ptr), (err)); \
262 (x) = (__force __typeof__(*(ptr)))__gu_val; \
266 * We must not call into the scheduler between uaccess_ttbr0_enable() and
267 * uaccess_ttbr0_disable(). As `x` and `ptr` could contain blocking functions,
268 * we must evaluate these outside of the critical section.
270 #define __raw_get_user(x, ptr, err) \
272 __typeof__(*(ptr)) __user *__rgu_ptr = (ptr); \
273 __typeof__(x) __rgu_val; \
274 __chk_user_ptr(ptr); \
276 uaccess_ttbr0_enable(); \
277 __raw_get_mem("ldtr", __rgu_val, __rgu_ptr, err); \
278 uaccess_ttbr0_disable(); \
283 #define __get_user_error(x, ptr, err) \
285 __typeof__(*(ptr)) __user *__p = (ptr); \
287 if (access_ok(__p, sizeof(*__p))) { \
288 __p = uaccess_mask_ptr(__p); \
289 __raw_get_user((x), __p, (err)); \
291 (x) = (__force __typeof__(x))0; (err) = -EFAULT; \
295 #define __get_user(x, ptr) \
298 __get_user_error((x), (ptr), __gu_err); \
302 #define get_user __get_user
305 * We must not call into the scheduler between __uaccess_enable_tco_async() and
306 * __uaccess_disable_tco_async(). As `dst` and `src` may contain blocking
307 * functions, we must evaluate these outside of the critical section.
309 #define __get_kernel_nofault(dst, src, type, err_label) \
311 __typeof__(dst) __gkn_dst = (dst); \
312 __typeof__(src) __gkn_src = (src); \
315 __uaccess_enable_tco_async(); \
316 __raw_get_mem("ldr", *((type *)(__gkn_dst)), \
317 (__force type *)(__gkn_src), __gkn_err); \
318 __uaccess_disable_tco_async(); \
320 if (unlikely(__gkn_err)) \
324 #define __put_mem_asm(store, reg, x, addr, err) \
326 "1: " store " " reg "1, [%2]\n" \
328 _ASM_EXTABLE_UACCESS_ERR(1b, 2b, %w0) \
330 : "r" (x), "r" (addr))
332 #define __raw_put_mem(str, x, ptr, err) \
334 __typeof__(*(ptr)) __pu_val = (x); \
335 switch (sizeof(*(ptr))) { \
337 __put_mem_asm(str "b", "%w", __pu_val, (ptr), (err)); \
340 __put_mem_asm(str "h", "%w", __pu_val, (ptr), (err)); \
343 __put_mem_asm(str, "%w", __pu_val, (ptr), (err)); \
346 __put_mem_asm(str, "%x", __pu_val, (ptr), (err)); \
354 * We must not call into the scheduler between uaccess_ttbr0_enable() and
355 * uaccess_ttbr0_disable(). As `x` and `ptr` could contain blocking functions,
356 * we must evaluate these outside of the critical section.
358 #define __raw_put_user(x, ptr, err) \
360 __typeof__(*(ptr)) __user *__rpu_ptr = (ptr); \
361 __typeof__(*(ptr)) __rpu_val = (x); \
362 __chk_user_ptr(__rpu_ptr); \
364 uaccess_ttbr0_enable(); \
365 __raw_put_mem("sttr", __rpu_val, __rpu_ptr, err); \
366 uaccess_ttbr0_disable(); \
369 #define __put_user_error(x, ptr, err) \
371 __typeof__(*(ptr)) __user *__p = (ptr); \
373 if (access_ok(__p, sizeof(*__p))) { \
374 __p = uaccess_mask_ptr(__p); \
375 __raw_put_user((x), __p, (err)); \
381 #define __put_user(x, ptr) \
384 __put_user_error((x), (ptr), __pu_err); \
388 #define put_user __put_user
391 * We must not call into the scheduler between __uaccess_enable_tco_async() and
392 * __uaccess_disable_tco_async(). As `dst` and `src` may contain blocking
393 * functions, we must evaluate these outside of the critical section.
395 #define __put_kernel_nofault(dst, src, type, err_label) \
397 __typeof__(dst) __pkn_dst = (dst); \
398 __typeof__(src) __pkn_src = (src); \
401 __uaccess_enable_tco_async(); \
402 __raw_put_mem("str", *((type *)(__pkn_src)), \
403 (__force type *)(__pkn_dst), __pkn_err); \
404 __uaccess_disable_tco_async(); \
406 if (unlikely(__pkn_err)) \
410 extern unsigned long __must_check __arch_copy_from_user(void *to, const void __user *from, unsigned long n);
411 #define raw_copy_from_user(to, from, n) \
413 unsigned long __acfu_ret; \
414 uaccess_ttbr0_enable(); \
415 __acfu_ret = __arch_copy_from_user((to), \
416 __uaccess_mask_ptr(from), (n)); \
417 uaccess_ttbr0_disable(); \
421 extern unsigned long __must_check __arch_copy_to_user(void __user *to, const void *from, unsigned long n);
422 #define raw_copy_to_user(to, from, n) \
424 unsigned long __actu_ret; \
425 uaccess_ttbr0_enable(); \
426 __actu_ret = __arch_copy_to_user(__uaccess_mask_ptr(to), \
428 uaccess_ttbr0_disable(); \
432 #define INLINE_COPY_TO_USER
433 #define INLINE_COPY_FROM_USER
435 extern unsigned long __must_check __arch_clear_user(void __user *to, unsigned long n);
436 static inline unsigned long __must_check __clear_user(void __user *to, unsigned long n)
438 if (access_ok(to, n)) {
439 uaccess_ttbr0_enable();
440 n = __arch_clear_user(__uaccess_mask_ptr(to), n);
441 uaccess_ttbr0_disable();
445 #define clear_user __clear_user
447 extern long strncpy_from_user(char *dest, const char __user *src, long count);
449 extern __must_check long strnlen_user(const char __user *str, long n);
451 #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
453 void memcpy_page_flushcache(char *to, struct page *page, size_t offset, size_t len);
454 extern unsigned long __must_check __copy_user_flushcache(void *to, const void __user *from, unsigned long n);
456 static inline int __copy_from_user_flushcache(void *dst, const void __user *src, unsigned size)
458 kasan_check_write(dst, size);
459 return __copy_user_flushcache(dst, __uaccess_mask_ptr(src), size);
463 #ifdef CONFIG_ARCH_HAS_SUBPAGE_FAULTS
466 * Return 0 on success, the number of bytes not probed otherwise.
468 static inline size_t probe_subpage_writeable(const char __user *uaddr,
471 if (!system_supports_mte())
473 return mte_probe_user_range(uaddr, size);
476 #endif /* CONFIG_ARCH_HAS_SUBPAGE_FAULTS */
478 #endif /* __ASM_UACCESS_H */