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 get_ds() (KERNEL_DS)
39 #define get_fs() (current_thread_info()->addr_limit)
41 static inline void set_fs(mm_segment_t fs)
43 current_thread_info()->addr_limit = fs;
45 /* On user-mode return, check fs is correct */
46 set_thread_flag(TIF_FSCHECK);
49 * Enable/disable UAO so that copy_to_user() etc can access
50 * kernel memory with the unprivileged instructions.
52 if (IS_ENABLED(CONFIG_ARM64_UAO) && fs == KERNEL_DS)
53 asm(ALTERNATIVE("nop", SET_PSTATE_UAO(1), ARM64_HAS_UAO));
55 asm(ALTERNATIVE("nop", SET_PSTATE_UAO(0), ARM64_HAS_UAO,
59 #define segment_eq(a, b) ((a) == (b))
62 * Test whether a block of memory is a valid user space address.
63 * Returns 1 if the range is valid, 0 otherwise.
65 * This is equivalent to the following test:
66 * (u65)addr + (u65)size <= (u65)current->addr_limit + 1
68 static inline unsigned long __range_ok(unsigned long addr, unsigned long size)
70 unsigned long limit = current_thread_info()->addr_limit;
74 // A + B <= C + 1 for all A,B,C, in four easy steps:
75 // 1: X = A + B; X' = X % 2^64
77 // 2: Set C = 0 if X > 2^64, to guarantee X' > C in step 4
78 " csel %1, xzr, %1, hi\n"
79 // 3: Set X' = ~0 if X >= 2^64. For X == 2^64, this decrements X'
80 // to compensate for the carry flag being set in step 4. For
81 // X > 2^64, X' merely has to remain nonzero, which it does.
82 " csinv %0, %0, xzr, cc\n"
83 // 4: For X < 2^64, this gives us X' - C - 1 <= 0, where the -1
84 // comes from the carry in being clear. Otherwise, we are
85 // testing X' - C == 0, subject to the previous adjustments.
88 : "+r" (addr), "+r" (limit) : "Ir" (size) : "cc");
94 * When dealing with data aborts, watchpoints, or instruction traps we may end
95 * up with a tagged userland pointer. Clear the tag to get a sane pointer to
96 * pass on to access_ok(), for instance.
98 #define untagged_addr(addr) sign_extend64(addr, 55)
100 #define access_ok(type, addr, size) __range_ok((unsigned long)(addr), size)
101 #define user_addr_max get_fs
103 #define _ASM_EXTABLE(from, to) \
104 " .pushsection __ex_table, \"a\"\n" \
106 " .long (" #from " - .), (" #to " - .)\n" \
110 * User access enabling/disabling.
112 #ifdef CONFIG_ARM64_SW_TTBR0_PAN
113 static inline void __uaccess_ttbr0_disable(void)
115 unsigned long flags, ttbr;
117 local_irq_save(flags);
118 ttbr = read_sysreg(ttbr1_el1);
119 ttbr &= ~TTBR_ASID_MASK;
120 /* reserved_ttbr0 placed before swapper_pg_dir */
121 write_sysreg(ttbr - RESERVED_TTBR0_SIZE, ttbr0_el1);
123 /* Set reserved ASID */
124 write_sysreg(ttbr, ttbr1_el1);
126 local_irq_restore(flags);
129 static inline void __uaccess_ttbr0_enable(void)
131 unsigned long flags, ttbr0, ttbr1;
134 * Disable interrupts to avoid preemption between reading the 'ttbr0'
135 * variable and the MSR. A context switch could trigger an ASID
136 * roll-over and an update of 'ttbr0'.
138 local_irq_save(flags);
139 ttbr0 = READ_ONCE(current_thread_info()->ttbr0);
141 /* Restore active ASID */
142 ttbr1 = read_sysreg(ttbr1_el1);
143 ttbr1 &= ~TTBR_ASID_MASK; /* safety measure */
144 ttbr1 |= ttbr0 & TTBR_ASID_MASK;
145 write_sysreg(ttbr1, ttbr1_el1);
148 /* Restore user page table */
149 write_sysreg(ttbr0, ttbr0_el1);
151 local_irq_restore(flags);
154 static inline bool uaccess_ttbr0_disable(void)
156 if (!system_uses_ttbr0_pan())
158 __uaccess_ttbr0_disable();
162 static inline bool uaccess_ttbr0_enable(void)
164 if (!system_uses_ttbr0_pan())
166 __uaccess_ttbr0_enable();
170 static inline bool uaccess_ttbr0_disable(void)
175 static inline bool uaccess_ttbr0_enable(void)
181 static inline void __uaccess_disable_hw_pan(void)
183 asm(ALTERNATIVE("nop", SET_PSTATE_PAN(0), ARM64_HAS_PAN,
187 static inline void __uaccess_enable_hw_pan(void)
189 asm(ALTERNATIVE("nop", SET_PSTATE_PAN(1), ARM64_HAS_PAN,
193 #define __uaccess_disable(alt) \
195 if (!uaccess_ttbr0_disable()) \
196 asm(ALTERNATIVE("nop", SET_PSTATE_PAN(1), alt, \
197 CONFIG_ARM64_PAN)); \
200 #define __uaccess_enable(alt) \
202 if (!uaccess_ttbr0_enable()) \
203 asm(ALTERNATIVE("nop", SET_PSTATE_PAN(0), alt, \
204 CONFIG_ARM64_PAN)); \
207 static inline void uaccess_disable(void)
209 __uaccess_disable(ARM64_HAS_PAN);
212 static inline void uaccess_enable(void)
214 __uaccess_enable(ARM64_HAS_PAN);
218 * These functions are no-ops when UAO is present.
220 static inline void uaccess_disable_not_uao(void)
222 __uaccess_disable(ARM64_ALT_PAN_NOT_UAO);
225 static inline void uaccess_enable_not_uao(void)
227 __uaccess_enable(ARM64_ALT_PAN_NOT_UAO);
231 * The "__xxx" versions of the user access functions do not verify the address
232 * space - it must have been done previously with a separate "access_ok()"
235 * The "__xxx_error" versions set the third argument to -EFAULT if an error
236 * occurs, and leave it unchanged on success.
238 #define __get_user_asm(instr, alt_instr, reg, x, addr, err, feature) \
240 "1:"ALTERNATIVE(instr " " reg "1, [%2]\n", \
241 alt_instr " " reg "1, [%2]\n", feature) \
243 " .section .fixup, \"ax\"\n" \
249 _ASM_EXTABLE(1b, 3b) \
250 : "+r" (err), "=&r" (x) \
251 : "r" (addr), "i" (-EFAULT))
253 #define __get_user_err(x, ptr, err) \
255 unsigned long __gu_val; \
256 __chk_user_ptr(ptr); \
257 uaccess_enable_not_uao(); \
258 switch (sizeof(*(ptr))) { \
260 __get_user_asm("ldrb", "ldtrb", "%w", __gu_val, (ptr), \
261 (err), ARM64_HAS_UAO); \
264 __get_user_asm("ldrh", "ldtrh", "%w", __gu_val, (ptr), \
265 (err), ARM64_HAS_UAO); \
268 __get_user_asm("ldr", "ldtr", "%w", __gu_val, (ptr), \
269 (err), ARM64_HAS_UAO); \
272 __get_user_asm("ldr", "ldtr", "%x", __gu_val, (ptr), \
273 (err), ARM64_HAS_UAO); \
278 uaccess_disable_not_uao(); \
279 (x) = (__force __typeof__(*(ptr)))__gu_val; \
282 #define __get_user(x, ptr) \
285 __get_user_err((x), (ptr), __gu_err); \
289 #define __get_user_error(x, ptr, err) \
291 __get_user_err((x), (ptr), (err)); \
295 #define get_user(x, ptr) \
297 __typeof__(*(ptr)) __user *__p = (ptr); \
299 access_ok(VERIFY_READ, __p, sizeof(*__p)) ? \
300 __get_user((x), __p) : \
301 ((x) = 0, -EFAULT); \
304 #define __put_user_asm(instr, alt_instr, reg, x, addr, err, feature) \
306 "1:"ALTERNATIVE(instr " " reg "1, [%2]\n", \
307 alt_instr " " reg "1, [%2]\n", feature) \
309 " .section .fixup,\"ax\"\n" \
314 _ASM_EXTABLE(1b, 3b) \
316 : "r" (x), "r" (addr), "i" (-EFAULT))
318 #define __put_user_err(x, ptr, err) \
320 __typeof__(*(ptr)) __pu_val = (x); \
321 __chk_user_ptr(ptr); \
322 uaccess_enable_not_uao(); \
323 switch (sizeof(*(ptr))) { \
325 __put_user_asm("strb", "sttrb", "%w", __pu_val, (ptr), \
326 (err), ARM64_HAS_UAO); \
329 __put_user_asm("strh", "sttrh", "%w", __pu_val, (ptr), \
330 (err), ARM64_HAS_UAO); \
333 __put_user_asm("str", "sttr", "%w", __pu_val, (ptr), \
334 (err), ARM64_HAS_UAO); \
337 __put_user_asm("str", "sttr", "%x", __pu_val, (ptr), \
338 (err), ARM64_HAS_UAO); \
343 uaccess_disable_not_uao(); \
346 #define __put_user(x, ptr) \
349 __put_user_err((x), (ptr), __pu_err); \
353 #define __put_user_error(x, ptr, err) \
355 __put_user_err((x), (ptr), (err)); \
359 #define put_user(x, ptr) \
361 __typeof__(*(ptr)) __user *__p = (ptr); \
363 access_ok(VERIFY_WRITE, __p, sizeof(*__p)) ? \
364 __put_user((x), __p) : \
368 extern unsigned long __must_check __arch_copy_from_user(void *to, const void __user *from, unsigned long n);
369 #define raw_copy_from_user __arch_copy_from_user
370 extern unsigned long __must_check __arch_copy_to_user(void __user *to, const void *from, unsigned long n);
371 #define raw_copy_to_user __arch_copy_to_user
372 extern unsigned long __must_check raw_copy_in_user(void __user *to, const void __user *from, unsigned long n);
373 extern unsigned long __must_check __clear_user(void __user *addr, unsigned long n);
374 #define INLINE_COPY_TO_USER
375 #define INLINE_COPY_FROM_USER
377 static inline unsigned long __must_check clear_user(void __user *to, unsigned long n)
379 if (access_ok(VERIFY_WRITE, to, n))
380 n = __clear_user(to, n);
384 extern long strncpy_from_user(char *dest, const char __user *src, long count);
386 extern __must_check long strnlen_user(const char __user *str, long n);
388 #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
390 void memcpy_page_flushcache(char *to, struct page *page, size_t offset, size_t len);
391 extern unsigned long __must_check __copy_user_flushcache(void *to, const void __user *from, unsigned long n);
393 static inline int __copy_from_user_flushcache(void *dst, const void __user *src, unsigned size)
395 kasan_check_write(dst, size);
396 return __copy_user_flushcache(dst, src, size);
400 #endif /* __ASM_UACCESS_H */