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;
48 /* On user-mode return, check fs is correct */
49 set_thread_flag(TIF_FSCHECK);
52 * Enable/disable UAO so that copy_to_user() etc can access
53 * kernel memory with the unprivileged instructions.
55 if (IS_ENABLED(CONFIG_ARM64_UAO) && fs == KERNEL_DS)
56 asm(ALTERNATIVE("nop", SET_PSTATE_UAO(1), ARM64_HAS_UAO));
58 asm(ALTERNATIVE("nop", SET_PSTATE_UAO(0), ARM64_HAS_UAO,
62 #define segment_eq(a, b) ((a) == (b))
65 * Test whether a block of memory is a valid user space address.
66 * Returns 1 if the range is valid, 0 otherwise.
68 * This is equivalent to the following test:
69 * (u65)addr + (u65)size <= current->addr_limit
71 * This needs 65-bit arithmetic.
73 #define __range_ok(addr, size) \
75 unsigned long __addr = (unsigned long)(addr); \
76 unsigned long flag, roksum; \
77 __chk_user_ptr(addr); \
78 asm("adds %1, %1, %3; ccmp %1, %4, #2, cc; cset %0, ls" \
79 : "=&r" (flag), "=&r" (roksum) \
80 : "1" (__addr), "Ir" (size), \
81 "r" (current_thread_info()->addr_limit) \
87 * When dealing with data aborts, watchpoints, or instruction traps we may end
88 * up with a tagged userland pointer. Clear the tag to get a sane pointer to
89 * pass on to access_ok(), for instance.
91 #define untagged_addr(addr) sign_extend64(addr, 55)
93 #define access_ok(type, addr, size) __range_ok(addr, size)
94 #define user_addr_max get_fs
96 #define _ASM_EXTABLE(from, to) \
97 " .pushsection __ex_table, \"a\"\n" \
99 " .long (" #from " - .), (" #to " - .)\n" \
103 * User access enabling/disabling.
105 #ifdef CONFIG_ARM64_SW_TTBR0_PAN
106 static inline void __uaccess_ttbr0_disable(void)
110 /* reserved_ttbr0 placed at the end of swapper_pg_dir */
111 ttbr = read_sysreg(ttbr1_el1) + SWAPPER_DIR_SIZE;
112 write_sysreg(ttbr, ttbr0_el1);
116 static inline void __uaccess_ttbr0_enable(void)
121 * Disable interrupts to avoid preemption between reading the 'ttbr0'
122 * variable and the MSR. A context switch could trigger an ASID
123 * roll-over and an update of 'ttbr0'.
125 local_irq_save(flags);
126 write_sysreg(current_thread_info()->ttbr0, ttbr0_el1);
128 local_irq_restore(flags);
131 static inline bool uaccess_ttbr0_disable(void)
133 if (!system_uses_ttbr0_pan())
135 __uaccess_ttbr0_disable();
139 static inline bool uaccess_ttbr0_enable(void)
141 if (!system_uses_ttbr0_pan())
143 __uaccess_ttbr0_enable();
147 static inline bool uaccess_ttbr0_disable(void)
152 static inline bool uaccess_ttbr0_enable(void)
158 #define __uaccess_disable(alt) \
160 if (!uaccess_ttbr0_disable()) \
161 asm(ALTERNATIVE("nop", SET_PSTATE_PAN(1), alt, \
162 CONFIG_ARM64_PAN)); \
165 #define __uaccess_enable(alt) \
167 if (!uaccess_ttbr0_enable()) \
168 asm(ALTERNATIVE("nop", SET_PSTATE_PAN(0), alt, \
169 CONFIG_ARM64_PAN)); \
172 static inline void uaccess_disable(void)
174 __uaccess_disable(ARM64_HAS_PAN);
177 static inline void uaccess_enable(void)
179 __uaccess_enable(ARM64_HAS_PAN);
183 * These functions are no-ops when UAO is present.
185 static inline void uaccess_disable_not_uao(void)
187 __uaccess_disable(ARM64_ALT_PAN_NOT_UAO);
190 static inline void uaccess_enable_not_uao(void)
192 __uaccess_enable(ARM64_ALT_PAN_NOT_UAO);
196 * The "__xxx" versions of the user access functions do not verify the address
197 * space - it must have been done previously with a separate "access_ok()"
200 * The "__xxx_error" versions set the third argument to -EFAULT if an error
201 * occurs, and leave it unchanged on success.
203 #define __get_user_asm(instr, alt_instr, reg, x, addr, err, feature) \
205 "1:"ALTERNATIVE(instr " " reg "1, [%2]\n", \
206 alt_instr " " reg "1, [%2]\n", feature) \
208 " .section .fixup, \"ax\"\n" \
214 _ASM_EXTABLE(1b, 3b) \
215 : "+r" (err), "=&r" (x) \
216 : "r" (addr), "i" (-EFAULT))
218 #define __get_user_err(x, ptr, err) \
220 unsigned long __gu_val; \
221 __chk_user_ptr(ptr); \
222 uaccess_enable_not_uao(); \
223 switch (sizeof(*(ptr))) { \
225 __get_user_asm("ldrb", "ldtrb", "%w", __gu_val, (ptr), \
226 (err), ARM64_HAS_UAO); \
229 __get_user_asm("ldrh", "ldtrh", "%w", __gu_val, (ptr), \
230 (err), ARM64_HAS_UAO); \
233 __get_user_asm("ldr", "ldtr", "%w", __gu_val, (ptr), \
234 (err), ARM64_HAS_UAO); \
237 __get_user_asm("ldr", "ldtr", "%x", __gu_val, (ptr), \
238 (err), ARM64_HAS_UAO); \
243 uaccess_disable_not_uao(); \
244 (x) = (__force __typeof__(*(ptr)))__gu_val; \
247 #define __get_user(x, ptr) \
250 __get_user_err((x), (ptr), __gu_err); \
254 #define __get_user_error(x, ptr, err) \
256 __get_user_err((x), (ptr), (err)); \
260 #define get_user(x, ptr) \
262 __typeof__(*(ptr)) __user *__p = (ptr); \
264 access_ok(VERIFY_READ, __p, sizeof(*__p)) ? \
265 __get_user((x), __p) : \
266 ((x) = 0, -EFAULT); \
269 #define __put_user_asm(instr, alt_instr, reg, x, addr, err, feature) \
271 "1:"ALTERNATIVE(instr " " reg "1, [%2]\n", \
272 alt_instr " " reg "1, [%2]\n", feature) \
274 " .section .fixup,\"ax\"\n" \
279 _ASM_EXTABLE(1b, 3b) \
281 : "r" (x), "r" (addr), "i" (-EFAULT))
283 #define __put_user_err(x, ptr, err) \
285 __typeof__(*(ptr)) __pu_val = (x); \
286 __chk_user_ptr(ptr); \
287 uaccess_enable_not_uao(); \
288 switch (sizeof(*(ptr))) { \
290 __put_user_asm("strb", "sttrb", "%w", __pu_val, (ptr), \
291 (err), ARM64_HAS_UAO); \
294 __put_user_asm("strh", "sttrh", "%w", __pu_val, (ptr), \
295 (err), ARM64_HAS_UAO); \
298 __put_user_asm("str", "sttr", "%w", __pu_val, (ptr), \
299 (err), ARM64_HAS_UAO); \
302 __put_user_asm("str", "sttr", "%x", __pu_val, (ptr), \
303 (err), ARM64_HAS_UAO); \
308 uaccess_disable_not_uao(); \
311 #define __put_user(x, ptr) \
314 __put_user_err((x), (ptr), __pu_err); \
318 #define __put_user_error(x, ptr, err) \
320 __put_user_err((x), (ptr), (err)); \
324 #define put_user(x, ptr) \
326 __typeof__(*(ptr)) __user *__p = (ptr); \
328 access_ok(VERIFY_WRITE, __p, sizeof(*__p)) ? \
329 __put_user((x), __p) : \
333 extern unsigned long __must_check __arch_copy_from_user(void *to, const void __user *from, unsigned long n);
334 #define raw_copy_from_user __arch_copy_from_user
335 extern unsigned long __must_check __arch_copy_to_user(void __user *to, const void *from, unsigned long n);
336 #define raw_copy_to_user __arch_copy_to_user
337 extern unsigned long __must_check raw_copy_in_user(void __user *to, const void __user *from, unsigned long n);
338 extern unsigned long __must_check __clear_user(void __user *addr, unsigned long n);
339 #define INLINE_COPY_TO_USER
340 #define INLINE_COPY_FROM_USER
342 static inline unsigned long __must_check clear_user(void __user *to, unsigned long n)
344 if (access_ok(VERIFY_WRITE, to, n))
345 n = __clear_user(to, n);
349 extern long strncpy_from_user(char *dest, const char __user *src, long count);
351 extern __must_check long strnlen_user(const char __user *str, long n);
353 #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
355 void memcpy_page_flushcache(char *to, struct page *page, size_t offset, size_t len);
356 extern unsigned long __must_check __copy_user_flushcache(void *to, const void __user *from, unsigned long n);
358 static inline int __copy_from_user_flushcache(void *dst, const void __user *src, unsigned size)
360 kasan_check_write(dst, size);
361 return __copy_user_flushcache(dst, src, size);
365 #endif /* __ASM_UACCESS_H */