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>
28 * User space memory access functions
30 #include <linux/bitops.h>
31 #include <linux/kasan-checks.h>
32 #include <linux/string.h>
33 #include <linux/thread_info.h>
35 #include <asm/cpufeature.h>
36 #include <asm/ptrace.h>
37 #include <asm/errno.h>
38 #include <asm/memory.h>
39 #include <asm/compiler.h>
42 #define VERIFY_WRITE 1
45 * The exception table consists of pairs of relative offsets: the first
46 * is the relative offset to an instruction that is allowed to fault,
47 * and the second is the relative offset at which the program should
48 * continue. No registers are modified, so it is entirely up to the
49 * continuation code to figure out what to do.
51 * All the routines below use bits of fixup code that are out of line
52 * with the main instruction path. This means when everything is well,
53 * we don't even have to jump over them. Further, they do not intrude
54 * on our cache or tlb entries.
57 struct exception_table_entry
62 #define ARCH_HAS_RELATIVE_EXTABLE
64 extern int fixup_exception(struct pt_regs *regs);
66 #define KERNEL_DS (-1UL)
67 #define get_ds() (KERNEL_DS)
69 #define USER_DS TASK_SIZE_64
70 #define get_fs() (current_thread_info()->addr_limit)
72 static inline void set_fs(mm_segment_t fs)
74 current_thread_info()->addr_limit = fs;
77 * Enable/disable UAO so that copy_to_user() etc can access
78 * kernel memory with the unprivileged instructions.
80 if (IS_ENABLED(CONFIG_ARM64_UAO) && fs == KERNEL_DS)
81 asm(ALTERNATIVE("nop", SET_PSTATE_UAO(1), ARM64_HAS_UAO));
83 asm(ALTERNATIVE("nop", SET_PSTATE_UAO(0), ARM64_HAS_UAO,
87 #define segment_eq(a, b) ((a) == (b))
90 * Test whether a block of memory is a valid user space address.
91 * Returns 1 if the range is valid, 0 otherwise.
93 * This is equivalent to the following test:
94 * (u65)addr + (u65)size <= current->addr_limit
96 * This needs 65-bit arithmetic.
98 #define __range_ok(addr, size) \
100 unsigned long __addr = (unsigned long __force)(addr); \
101 unsigned long flag, roksum; \
102 __chk_user_ptr(addr); \
103 asm("adds %1, %1, %3; ccmp %1, %4, #2, cc; cset %0, ls" \
104 : "=&r" (flag), "=&r" (roksum) \
105 : "1" (__addr), "Ir" (size), \
106 "r" (current_thread_info()->addr_limit) \
112 * When dealing with data aborts, watchpoints, or instruction traps we may end
113 * up with a tagged userland pointer. Clear the tag to get a sane pointer to
114 * pass on to access_ok(), for instance.
116 #define untagged_addr(addr) sign_extend64(addr, 55)
118 #define access_ok(type, addr, size) __range_ok(addr, size)
119 #define user_addr_max get_fs
121 #define _ASM_EXTABLE(from, to) \
122 " .pushsection __ex_table, \"a\"\n" \
124 " .long (" #from " - .), (" #to " - .)\n" \
128 * User access enabling/disabling.
130 #ifdef CONFIG_ARM64_SW_TTBR0_PAN
131 static inline void __uaccess_ttbr0_disable(void)
135 /* reserved_ttbr0 placed at the end of swapper_pg_dir */
136 ttbr = read_sysreg(ttbr1_el1) + SWAPPER_DIR_SIZE;
137 write_sysreg(ttbr, ttbr0_el1);
141 static inline void __uaccess_ttbr0_enable(void)
146 * Disable interrupts to avoid preemption between reading the 'ttbr0'
147 * variable and the MSR. A context switch could trigger an ASID
148 * roll-over and an update of 'ttbr0'.
150 local_irq_save(flags);
151 write_sysreg(current_thread_info()->ttbr0, ttbr0_el1);
153 local_irq_restore(flags);
156 static inline bool uaccess_ttbr0_disable(void)
158 if (!system_uses_ttbr0_pan())
160 __uaccess_ttbr0_disable();
164 static inline bool uaccess_ttbr0_enable(void)
166 if (!system_uses_ttbr0_pan())
168 __uaccess_ttbr0_enable();
172 static inline bool uaccess_ttbr0_disable(void)
177 static inline bool uaccess_ttbr0_enable(void)
183 #define __uaccess_disable(alt) \
185 if (!uaccess_ttbr0_disable()) \
186 asm(ALTERNATIVE("nop", SET_PSTATE_PAN(1), alt, \
187 CONFIG_ARM64_PAN)); \
190 #define __uaccess_enable(alt) \
192 if (!uaccess_ttbr0_enable()) \
193 asm(ALTERNATIVE("nop", SET_PSTATE_PAN(0), alt, \
194 CONFIG_ARM64_PAN)); \
197 static inline void uaccess_disable(void)
199 __uaccess_disable(ARM64_HAS_PAN);
202 static inline void uaccess_enable(void)
204 __uaccess_enable(ARM64_HAS_PAN);
208 * These functions are no-ops when UAO is present.
210 static inline void uaccess_disable_not_uao(void)
212 __uaccess_disable(ARM64_ALT_PAN_NOT_UAO);
215 static inline void uaccess_enable_not_uao(void)
217 __uaccess_enable(ARM64_ALT_PAN_NOT_UAO);
221 * The "__xxx" versions of the user access functions do not verify the address
222 * space - it must have been done previously with a separate "access_ok()"
225 * The "__xxx_error" versions set the third argument to -EFAULT if an error
226 * occurs, and leave it unchanged on success.
228 #define __get_user_asm(instr, alt_instr, reg, x, addr, err, feature) \
230 "1:"ALTERNATIVE(instr " " reg "1, [%2]\n", \
231 alt_instr " " reg "1, [%2]\n", feature) \
233 " .section .fixup, \"ax\"\n" \
239 _ASM_EXTABLE(1b, 3b) \
240 : "+r" (err), "=&r" (x) \
241 : "r" (addr), "i" (-EFAULT))
243 #define __get_user_err(x, ptr, err) \
245 unsigned long __gu_val; \
246 __chk_user_ptr(ptr); \
247 uaccess_enable_not_uao(); \
248 switch (sizeof(*(ptr))) { \
250 __get_user_asm("ldrb", "ldtrb", "%w", __gu_val, (ptr), \
251 (err), ARM64_HAS_UAO); \
254 __get_user_asm("ldrh", "ldtrh", "%w", __gu_val, (ptr), \
255 (err), ARM64_HAS_UAO); \
258 __get_user_asm("ldr", "ldtr", "%w", __gu_val, (ptr), \
259 (err), ARM64_HAS_UAO); \
262 __get_user_asm("ldr", "ldtr", "%", __gu_val, (ptr), \
263 (err), ARM64_HAS_UAO); \
268 uaccess_disable_not_uao(); \
269 (x) = (__force __typeof__(*(ptr)))__gu_val; \
272 #define __get_user(x, ptr) \
275 __get_user_err((x), (ptr), __gu_err); \
279 #define __get_user_error(x, ptr, err) \
281 __get_user_err((x), (ptr), (err)); \
285 #define __get_user_unaligned __get_user
287 #define get_user(x, ptr) \
289 __typeof__(*(ptr)) __user *__p = (ptr); \
291 access_ok(VERIFY_READ, __p, sizeof(*__p)) ? \
292 __get_user((x), __p) : \
293 ((x) = 0, -EFAULT); \
296 #define __put_user_asm(instr, alt_instr, reg, x, addr, err, feature) \
298 "1:"ALTERNATIVE(instr " " reg "1, [%2]\n", \
299 alt_instr " " reg "1, [%2]\n", feature) \
301 " .section .fixup,\"ax\"\n" \
306 _ASM_EXTABLE(1b, 3b) \
308 : "r" (x), "r" (addr), "i" (-EFAULT))
310 #define __put_user_err(x, ptr, err) \
312 __typeof__(*(ptr)) __pu_val = (x); \
313 __chk_user_ptr(ptr); \
314 uaccess_enable_not_uao(); \
315 switch (sizeof(*(ptr))) { \
317 __put_user_asm("strb", "sttrb", "%w", __pu_val, (ptr), \
318 (err), ARM64_HAS_UAO); \
321 __put_user_asm("strh", "sttrh", "%w", __pu_val, (ptr), \
322 (err), ARM64_HAS_UAO); \
325 __put_user_asm("str", "sttr", "%w", __pu_val, (ptr), \
326 (err), ARM64_HAS_UAO); \
329 __put_user_asm("str", "sttr", "%", __pu_val, (ptr), \
330 (err), ARM64_HAS_UAO); \
335 uaccess_disable_not_uao(); \
338 #define __put_user(x, ptr) \
341 __put_user_err((x), (ptr), __pu_err); \
345 #define __put_user_error(x, ptr, err) \
347 __put_user_err((x), (ptr), (err)); \
351 #define __put_user_unaligned __put_user
353 #define put_user(x, ptr) \
355 __typeof__(*(ptr)) __user *__p = (ptr); \
357 access_ok(VERIFY_WRITE, __p, sizeof(*__p)) ? \
358 __put_user((x), __p) : \
362 extern unsigned long __must_check __arch_copy_from_user(void *to, const void __user *from, unsigned long n);
363 extern unsigned long __must_check __arch_copy_to_user(void __user *to, const void *from, unsigned long n);
364 extern unsigned long __must_check __copy_in_user(void __user *to, const void __user *from, unsigned long n);
365 extern unsigned long __must_check __clear_user(void __user *addr, unsigned long n);
367 static inline unsigned long __must_check __copy_from_user(void *to, const void __user *from, unsigned long n)
369 kasan_check_write(to, n);
370 check_object_size(to, n, false);
371 return __arch_copy_from_user(to, from, n);
374 static inline unsigned long __must_check __copy_to_user(void __user *to, const void *from, unsigned long n)
376 kasan_check_read(from, n);
377 check_object_size(from, n, true);
378 return __arch_copy_to_user(to, from, n);
381 static inline unsigned long __must_check copy_from_user(void *to, const void __user *from, unsigned long n)
383 unsigned long res = n;
384 kasan_check_write(to, n);
386 if (access_ok(VERIFY_READ, from, n)) {
387 check_object_size(to, n, false);
388 res = __arch_copy_from_user(to, from, n);
391 memset(to + (n - res), 0, res);
395 static inline unsigned long __must_check copy_to_user(void __user *to, const void *from, unsigned long n)
397 kasan_check_read(from, n);
399 if (access_ok(VERIFY_WRITE, to, n)) {
400 check_object_size(from, n, true);
401 n = __arch_copy_to_user(to, from, n);
406 static inline unsigned long __must_check copy_in_user(void __user *to, const void __user *from, unsigned long n)
408 if (access_ok(VERIFY_READ, from, n) && access_ok(VERIFY_WRITE, to, n))
409 n = __copy_in_user(to, from, n);
413 #define __copy_to_user_inatomic __copy_to_user
414 #define __copy_from_user_inatomic __copy_from_user
416 static inline unsigned long __must_check clear_user(void __user *to, unsigned long n)
418 if (access_ok(VERIFY_WRITE, to, n))
419 n = __clear_user(to, n);
423 extern long strncpy_from_user(char *dest, const char __user *src, long count);
425 extern __must_check long strlen_user(const char __user *str);
426 extern __must_check long strnlen_user(const char __user *str, long n);
428 #else /* __ASSEMBLY__ */
430 #include <asm/assembler.h>
433 * User access enabling/disabling macros.
435 #ifdef CONFIG_ARM64_SW_TTBR0_PAN
436 .macro __uaccess_ttbr0_disable, tmp1
437 mrs \tmp1, ttbr1_el1 // swapper_pg_dir
438 add \tmp1, \tmp1, #SWAPPER_DIR_SIZE // reserved_ttbr0 at the end of swapper_pg_dir
439 msr ttbr0_el1, \tmp1 // set reserved TTBR0_EL1
443 .macro __uaccess_ttbr0_enable, tmp1
444 get_thread_info \tmp1
445 ldr \tmp1, [\tmp1, #TSK_TI_TTBR0] // load saved TTBR0_EL1
446 msr ttbr0_el1, \tmp1 // set the non-PAN TTBR0_EL1
450 .macro uaccess_ttbr0_disable, tmp1
451 alternative_if_not ARM64_HAS_PAN
452 __uaccess_ttbr0_disable \tmp1
453 alternative_else_nop_endif
456 .macro uaccess_ttbr0_enable, tmp1, tmp2
457 alternative_if_not ARM64_HAS_PAN
458 save_and_disable_irq \tmp2 // avoid preemption
459 __uaccess_ttbr0_enable \tmp1
461 alternative_else_nop_endif
464 .macro uaccess_ttbr0_disable, tmp1
467 .macro uaccess_ttbr0_enable, tmp1, tmp2
472 * These macros are no-ops when UAO is present.
474 .macro uaccess_disable_not_uao, tmp1
475 uaccess_ttbr0_disable \tmp1
476 alternative_if ARM64_ALT_PAN_NOT_UAO
478 alternative_else_nop_endif
481 .macro uaccess_enable_not_uao, tmp1, tmp2
482 uaccess_ttbr0_enable \tmp1, \tmp2
483 alternative_if ARM64_ALT_PAN_NOT_UAO
485 alternative_else_nop_endif
488 #endif /* __ASSEMBLY__ */
490 #endif /* __ASM_UACCESS_H */