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>
31 #include <linux/thread_info.h>
33 #include <asm/cpufeature.h>
34 #include <asm/ptrace.h>
35 #include <asm/errno.h>
36 #include <asm/memory.h>
37 #include <asm/compiler.h>
40 * The exception table consists of pairs of relative offsets: the first
41 * is the relative offset to an instruction that is allowed to fault,
42 * and the second is the relative offset at which the program should
43 * continue. No registers are modified, so it is entirely up to the
44 * continuation code to figure out what to do.
46 * All the routines below use bits of fixup code that are out of line
47 * with the main instruction path. This means when everything is well,
48 * we don't even have to jump over them. Further, they do not intrude
49 * on our cache or tlb entries.
52 struct exception_table_entry
57 #define ARCH_HAS_RELATIVE_EXTABLE
59 extern int fixup_exception(struct pt_regs *regs);
61 #define KERNEL_DS (-1UL)
62 #define get_ds() (KERNEL_DS)
64 #define USER_DS TASK_SIZE_64
65 #define get_fs() (current_thread_info()->addr_limit)
67 static inline void set_fs(mm_segment_t fs)
69 current_thread_info()->addr_limit = fs;
72 * Enable/disable UAO so that copy_to_user() etc can access
73 * kernel memory with the unprivileged instructions.
75 if (IS_ENABLED(CONFIG_ARM64_UAO) && fs == KERNEL_DS)
76 asm(ALTERNATIVE("nop", SET_PSTATE_UAO(1), ARM64_HAS_UAO));
78 asm(ALTERNATIVE("nop", SET_PSTATE_UAO(0), ARM64_HAS_UAO,
82 #define segment_eq(a, b) ((a) == (b))
85 * Test whether a block of memory is a valid user space address.
86 * Returns 1 if the range is valid, 0 otherwise.
88 * This is equivalent to the following test:
89 * (u65)addr + (u65)size <= current->addr_limit
91 * This needs 65-bit arithmetic.
93 #define __range_ok(addr, size) \
95 unsigned long flag, roksum; \
96 __chk_user_ptr(addr); \
97 asm("adds %1, %1, %3; ccmp %1, %4, #2, cc; cset %0, ls" \
98 : "=&r" (flag), "=&r" (roksum) \
99 : "1" (addr), "Ir" (size), \
100 "r" (current_thread_info()->addr_limit) \
106 * When dealing with data aborts or instruction traps we may end up with
107 * a tagged userland pointer. Clear the tag to get a sane pointer to pass
108 * on to access_ok(), for instance.
110 #define untagged_addr(addr) sign_extend64(addr, 55)
112 #define access_ok(type, addr, size) __range_ok(addr, size)
113 #define user_addr_max get_fs
115 #define _ASM_EXTABLE(from, to) \
116 " .pushsection __ex_table, \"a\"\n" \
118 " .long (" #from " - .), (" #to " - .)\n" \
122 * User access enabling/disabling.
124 #ifdef CONFIG_ARM64_SW_TTBR0_PAN
125 static inline void __uaccess_ttbr0_disable(void)
129 /* reserved_ttbr0 placed at the end of swapper_pg_dir */
130 ttbr = read_sysreg(ttbr1_el1) + SWAPPER_DIR_SIZE;
131 write_sysreg(ttbr, ttbr0_el1);
135 static inline void __uaccess_ttbr0_enable(void)
140 * Disable interrupts to avoid preemption between reading the 'ttbr0'
141 * variable and the MSR. A context switch could trigger an ASID
142 * roll-over and an update of 'ttbr0'.
144 local_irq_save(flags);
145 write_sysreg(current_thread_info()->ttbr0, ttbr0_el1);
147 local_irq_restore(flags);
150 static inline bool uaccess_ttbr0_disable(void)
152 if (!system_uses_ttbr0_pan())
154 __uaccess_ttbr0_disable();
158 static inline bool uaccess_ttbr0_enable(void)
160 if (!system_uses_ttbr0_pan())
162 __uaccess_ttbr0_enable();
166 static inline bool uaccess_ttbr0_disable(void)
171 static inline bool uaccess_ttbr0_enable(void)
177 #define __uaccess_disable(alt) \
179 if (!uaccess_ttbr0_disable()) \
180 asm(ALTERNATIVE("nop", SET_PSTATE_PAN(1), alt, \
181 CONFIG_ARM64_PAN)); \
184 #define __uaccess_enable(alt) \
186 if (!uaccess_ttbr0_enable()) \
187 asm(ALTERNATIVE("nop", SET_PSTATE_PAN(0), alt, \
188 CONFIG_ARM64_PAN)); \
191 static inline void uaccess_disable(void)
193 __uaccess_disable(ARM64_HAS_PAN);
196 static inline void uaccess_enable(void)
198 __uaccess_enable(ARM64_HAS_PAN);
202 * These functions are no-ops when UAO is present.
204 static inline void uaccess_disable_not_uao(void)
206 __uaccess_disable(ARM64_ALT_PAN_NOT_UAO);
209 static inline void uaccess_enable_not_uao(void)
211 __uaccess_enable(ARM64_ALT_PAN_NOT_UAO);
215 * The "__xxx" versions of the user access functions do not verify the address
216 * space - it must have been done previously with a separate "access_ok()"
219 * The "__xxx_error" versions set the third argument to -EFAULT if an error
220 * occurs, and leave it unchanged on success.
222 #define __get_user_asm(instr, alt_instr, reg, x, addr, err, feature) \
224 "1:"ALTERNATIVE(instr " " reg "1, [%2]\n", \
225 alt_instr " " reg "1, [%2]\n", feature) \
227 " .section .fixup, \"ax\"\n" \
233 _ASM_EXTABLE(1b, 3b) \
234 : "+r" (err), "=&r" (x) \
235 : "r" (addr), "i" (-EFAULT))
237 #define __get_user_err(x, ptr, err) \
239 unsigned long __gu_val; \
240 __chk_user_ptr(ptr); \
241 uaccess_enable_not_uao(); \
242 switch (sizeof(*(ptr))) { \
244 __get_user_asm("ldrb", "ldtrb", "%w", __gu_val, (ptr), \
245 (err), ARM64_HAS_UAO); \
248 __get_user_asm("ldrh", "ldtrh", "%w", __gu_val, (ptr), \
249 (err), ARM64_HAS_UAO); \
252 __get_user_asm("ldr", "ldtr", "%w", __gu_val, (ptr), \
253 (err), ARM64_HAS_UAO); \
256 __get_user_asm("ldr", "ldtr", "%", __gu_val, (ptr), \
257 (err), ARM64_HAS_UAO); \
262 uaccess_disable_not_uao(); \
263 (x) = (__force __typeof__(*(ptr)))__gu_val; \
266 #define __get_user(x, ptr) \
269 __get_user_err((x), (ptr), __gu_err); \
273 #define __get_user_error(x, ptr, err) \
275 __get_user_err((x), (ptr), (err)); \
279 #define __get_user_unaligned __get_user
281 #define get_user(x, ptr) \
283 __typeof__(*(ptr)) __user *__p = (ptr); \
285 access_ok(VERIFY_READ, __p, sizeof(*__p)) ? \
286 __get_user((x), __p) : \
287 ((x) = 0, -EFAULT); \
290 #define __put_user_asm(instr, alt_instr, reg, x, addr, err, feature) \
292 "1:"ALTERNATIVE(instr " " reg "1, [%2]\n", \
293 alt_instr " " reg "1, [%2]\n", feature) \
295 " .section .fixup,\"ax\"\n" \
300 _ASM_EXTABLE(1b, 3b) \
302 : "r" (x), "r" (addr), "i" (-EFAULT))
304 #define __put_user_err(x, ptr, err) \
306 __typeof__(*(ptr)) __pu_val = (x); \
307 __chk_user_ptr(ptr); \
308 uaccess_enable_not_uao(); \
309 switch (sizeof(*(ptr))) { \
311 __put_user_asm("strb", "sttrb", "%w", __pu_val, (ptr), \
312 (err), ARM64_HAS_UAO); \
315 __put_user_asm("strh", "sttrh", "%w", __pu_val, (ptr), \
316 (err), ARM64_HAS_UAO); \
319 __put_user_asm("str", "sttr", "%w", __pu_val, (ptr), \
320 (err), ARM64_HAS_UAO); \
323 __put_user_asm("str", "sttr", "%", __pu_val, (ptr), \
324 (err), ARM64_HAS_UAO); \
329 uaccess_disable_not_uao(); \
332 #define __put_user(x, ptr) \
335 __put_user_err((x), (ptr), __pu_err); \
339 #define __put_user_error(x, ptr, err) \
341 __put_user_err((x), (ptr), (err)); \
345 #define __put_user_unaligned __put_user
347 #define put_user(x, ptr) \
349 __typeof__(*(ptr)) __user *__p = (ptr); \
351 access_ok(VERIFY_WRITE, __p, sizeof(*__p)) ? \
352 __put_user((x), __p) : \
356 extern unsigned long __must_check __arch_copy_from_user(void *to, const void __user *from, unsigned long n);
357 extern unsigned long __must_check __arch_copy_to_user(void __user *to, const void *from, unsigned long n);
358 extern unsigned long __must_check __copy_in_user(void __user *to, const void __user *from, unsigned long n);
359 extern unsigned long __must_check __clear_user(void __user *addr, unsigned long n);
361 static inline unsigned long __must_check __copy_from_user(void *to, const void __user *from, unsigned long n)
363 kasan_check_write(to, n);
364 check_object_size(to, n, false);
365 return __arch_copy_from_user(to, from, n);
368 static inline unsigned long __must_check __copy_to_user(void __user *to, const void *from, unsigned long n)
370 kasan_check_read(from, n);
371 check_object_size(from, n, true);
372 return __arch_copy_to_user(to, from, n);
375 static inline unsigned long __must_check copy_from_user(void *to, const void __user *from, unsigned long n)
377 unsigned long res = n;
378 kasan_check_write(to, n);
379 check_object_size(to, n, false);
381 if (access_ok(VERIFY_READ, from, n)) {
382 res = __arch_copy_from_user(to, from, n);
385 memset(to + (n - res), 0, res);
389 static inline unsigned long __must_check copy_to_user(void __user *to, const void *from, unsigned long n)
391 kasan_check_read(from, n);
392 check_object_size(from, n, true);
394 if (access_ok(VERIFY_WRITE, to, n)) {
395 n = __arch_copy_to_user(to, from, n);
400 static inline unsigned long __must_check copy_in_user(void __user *to, const void __user *from, unsigned long n)
402 if (access_ok(VERIFY_READ, from, n) && access_ok(VERIFY_WRITE, to, n))
403 n = __copy_in_user(to, from, n);
407 #define __copy_to_user_inatomic __copy_to_user
408 #define __copy_from_user_inatomic __copy_from_user
410 static inline unsigned long __must_check clear_user(void __user *to, unsigned long n)
412 if (access_ok(VERIFY_WRITE, to, n))
413 n = __clear_user(to, n);
417 extern long strncpy_from_user(char *dest, const char __user *src, long count);
419 extern __must_check long strlen_user(const char __user *str);
420 extern __must_check long strnlen_user(const char __user *str, long n);
422 #endif /* __ASM_UACCESS_H */