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/cpufeature.h>
22 #include <asm/ptrace.h>
23 #include <asm/memory.h>
24 #include <asm/extable.h>
26 #define get_fs() (current_thread_info()->addr_limit)
28 static inline void set_fs(mm_segment_t fs
)
30 current_thread_info()->addr_limit
= fs
;
33 * Prevent a mispredicted conditional call to set_fs from forwarding
34 * the wrong address limit to access_ok under speculation.
38 /* On user-mode return, check fs is correct */
39 set_thread_flag(TIF_FSCHECK
);
42 * Enable/disable UAO so that copy_to_user() etc can access
43 * kernel memory with the unprivileged instructions.
45 if (IS_ENABLED(CONFIG_ARM64_UAO
) && fs
== KERNEL_DS
)
46 asm(ALTERNATIVE("nop", SET_PSTATE_UAO(1), ARM64_HAS_UAO
));
48 asm(ALTERNATIVE("nop", SET_PSTATE_UAO(0), ARM64_HAS_UAO
,
52 #define segment_eq(a, b) ((a) == (b))
55 * Test whether a block of memory is a valid user space address.
56 * Returns 1 if the range is valid, 0 otherwise.
58 * This is equivalent to the following test:
59 * (u65)addr + (u65)size <= (u65)current->addr_limit + 1
61 static inline unsigned long __range_ok(const void __user
*addr
, unsigned long size
)
63 unsigned long ret
, limit
= current_thread_info()->addr_limit
;
67 // A + B <= C + 1 for all A,B,C, in four easy steps:
68 // 1: X = A + B; X' = X % 2^64
70 // 2: Set C = 0 if X > 2^64, to guarantee X' > C in step 4
71 " csel %1, xzr, %1, hi\n"
72 // 3: Set X' = ~0 if X >= 2^64. For X == 2^64, this decrements X'
73 // to compensate for the carry flag being set in step 4. For
74 // X > 2^64, X' merely has to remain nonzero, which it does.
75 " csinv %0, %0, xzr, cc\n"
76 // 4: For X < 2^64, this gives us X' - C - 1 <= 0, where the -1
77 // comes from the carry in being clear. Otherwise, we are
78 // testing X' - C == 0, subject to the previous adjustments.
81 : "=&r" (ret
), "+r" (limit
) : "Ir" (size
), "0" (addr
) : "cc");
86 #define access_ok(addr, size) __range_ok(addr, size)
87 #define user_addr_max get_fs
89 #define _ASM_EXTABLE(from, to) \
90 " .pushsection __ex_table, \"a\"\n" \
92 " .long (" #from " - .), (" #to " - .)\n" \
96 * User access enabling/disabling.
98 #ifdef CONFIG_ARM64_SW_TTBR0_PAN
99 static inline void __uaccess_ttbr0_disable(void)
101 unsigned long flags
, ttbr
;
103 local_irq_save(flags
);
104 ttbr
= read_sysreg(ttbr1_el1
);
105 ttbr
&= ~TTBR_ASID_MASK
;
106 /* reserved_ttbr0 placed before swapper_pg_dir */
107 write_sysreg(ttbr
- RESERVED_TTBR0_SIZE
, ttbr0_el1
);
109 /* Set reserved ASID */
110 write_sysreg(ttbr
, ttbr1_el1
);
112 local_irq_restore(flags
);
115 static inline void __uaccess_ttbr0_enable(void)
117 unsigned long flags
, ttbr0
, ttbr1
;
120 * Disable interrupts to avoid preemption between reading the 'ttbr0'
121 * variable and the MSR. A context switch could trigger an ASID
122 * roll-over and an update of 'ttbr0'.
124 local_irq_save(flags
);
125 ttbr0
= READ_ONCE(current_thread_info()->ttbr0
);
127 /* Restore active ASID */
128 ttbr1
= read_sysreg(ttbr1_el1
);
129 ttbr1
&= ~TTBR_ASID_MASK
; /* safety measure */
130 ttbr1
|= ttbr0
& TTBR_ASID_MASK
;
131 write_sysreg(ttbr1
, ttbr1_el1
);
134 /* Restore user page table */
135 write_sysreg(ttbr0
, ttbr0_el1
);
137 local_irq_restore(flags
);
140 static inline bool uaccess_ttbr0_disable(void)
142 if (!system_uses_ttbr0_pan())
144 __uaccess_ttbr0_disable();
148 static inline bool uaccess_ttbr0_enable(void)
150 if (!system_uses_ttbr0_pan())
152 __uaccess_ttbr0_enable();
156 static inline bool uaccess_ttbr0_disable(void)
161 static inline bool uaccess_ttbr0_enable(void)
167 static inline void __uaccess_disable_hw_pan(void)
169 asm(ALTERNATIVE("nop", SET_PSTATE_PAN(0), ARM64_HAS_PAN
,
173 static inline void __uaccess_enable_hw_pan(void)
175 asm(ALTERNATIVE("nop", SET_PSTATE_PAN(1), ARM64_HAS_PAN
,
179 #define __uaccess_disable(alt) \
181 if (!uaccess_ttbr0_disable()) \
182 asm(ALTERNATIVE("nop", SET_PSTATE_PAN(1), alt, \
183 CONFIG_ARM64_PAN)); \
186 #define __uaccess_enable(alt) \
188 if (!uaccess_ttbr0_enable()) \
189 asm(ALTERNATIVE("nop", SET_PSTATE_PAN(0), alt, \
190 CONFIG_ARM64_PAN)); \
193 static inline void uaccess_disable(void)
195 __uaccess_disable(ARM64_HAS_PAN
);
198 static inline void uaccess_enable(void)
200 __uaccess_enable(ARM64_HAS_PAN
);
204 * These functions are no-ops when UAO is present.
206 static inline void uaccess_disable_not_uao(void)
208 __uaccess_disable(ARM64_ALT_PAN_NOT_UAO
);
211 static inline void uaccess_enable_not_uao(void)
213 __uaccess_enable(ARM64_ALT_PAN_NOT_UAO
);
217 * Sanitise a uaccess pointer such that it becomes NULL if above the
218 * current addr_limit.
220 #define uaccess_mask_ptr(ptr) (__typeof__(ptr))__uaccess_mask_ptr(ptr)
221 static inline void __user
*__uaccess_mask_ptr(const void __user
*ptr
)
223 void __user
*safe_ptr
;
226 " bics xzr, %1, %2\n"
227 " csel %0, %1, xzr, eq\n"
229 : "r" (ptr
), "r" (current_thread_info()->addr_limit
)
237 * The "__xxx" versions of the user access functions do not verify the address
238 * space - it must have been done previously with a separate "access_ok()"
241 * The "__xxx_error" versions set the third argument to -EFAULT if an error
242 * occurs, and leave it unchanged on success.
244 #define __get_user_asm(instr, alt_instr, reg, x, addr, err, feature) \
246 "1:"ALTERNATIVE(instr " " reg "1, [%2]\n", \
247 alt_instr " " reg "1, [%2]\n", feature) \
249 " .section .fixup, \"ax\"\n" \
255 _ASM_EXTABLE(1b, 3b) \
256 : "+r" (err), "=&r" (x) \
257 : "r" (addr), "i" (-EFAULT))
259 #define __raw_get_user(x, ptr, err) \
261 unsigned long __gu_val; \
262 __chk_user_ptr(ptr); \
263 uaccess_enable_not_uao(); \
264 switch (sizeof(*(ptr))) { \
266 __get_user_asm("ldrb", "ldtrb", "%w", __gu_val, (ptr), \
267 (err), ARM64_HAS_UAO); \
270 __get_user_asm("ldrh", "ldtrh", "%w", __gu_val, (ptr), \
271 (err), ARM64_HAS_UAO); \
274 __get_user_asm("ldr", "ldtr", "%w", __gu_val, (ptr), \
275 (err), ARM64_HAS_UAO); \
278 __get_user_asm("ldr", "ldtr", "%x", __gu_val, (ptr), \
279 (err), ARM64_HAS_UAO); \
284 uaccess_disable_not_uao(); \
285 (x) = (__force __typeof__(*(ptr)))__gu_val; \
288 #define __get_user_error(x, ptr, err) \
290 __typeof__(*(ptr)) __user *__p = (ptr); \
292 if (access_ok(__p, sizeof(*__p))) { \
293 __p = uaccess_mask_ptr(__p); \
294 __raw_get_user((x), __p, (err)); \
296 (x) = 0; (err) = -EFAULT; \
300 #define __get_user(x, ptr) \
303 __get_user_error((x), (ptr), __gu_err); \
307 #define get_user __get_user
309 #define __put_user_asm(instr, alt_instr, reg, x, addr, err, feature) \
311 "1:"ALTERNATIVE(instr " " reg "1, [%2]\n", \
312 alt_instr " " reg "1, [%2]\n", feature) \
314 " .section .fixup,\"ax\"\n" \
319 _ASM_EXTABLE(1b, 3b) \
321 : "r" (x), "r" (addr), "i" (-EFAULT))
323 #define __raw_put_user(x, ptr, err) \
325 __typeof__(*(ptr)) __pu_val = (x); \
326 __chk_user_ptr(ptr); \
327 uaccess_enable_not_uao(); \
328 switch (sizeof(*(ptr))) { \
330 __put_user_asm("strb", "sttrb", "%w", __pu_val, (ptr), \
331 (err), ARM64_HAS_UAO); \
334 __put_user_asm("strh", "sttrh", "%w", __pu_val, (ptr), \
335 (err), ARM64_HAS_UAO); \
338 __put_user_asm("str", "sttr", "%w", __pu_val, (ptr), \
339 (err), ARM64_HAS_UAO); \
342 __put_user_asm("str", "sttr", "%x", __pu_val, (ptr), \
343 (err), ARM64_HAS_UAO); \
348 uaccess_disable_not_uao(); \
351 #define __put_user_error(x, ptr, err) \
353 __typeof__(*(ptr)) __user *__p = (ptr); \
355 if (access_ok(__p, sizeof(*__p))) { \
356 __p = uaccess_mask_ptr(__p); \
357 __raw_put_user((x), __p, (err)); \
363 #define __put_user(x, ptr) \
366 __put_user_error((x), (ptr), __pu_err); \
370 #define put_user __put_user
372 extern unsigned long __must_check
__arch_copy_from_user(void *to
, const void __user
*from
, unsigned long n
);
373 #define raw_copy_from_user(to, from, n) \
375 __arch_copy_from_user((to), __uaccess_mask_ptr(from), (n)); \
378 extern unsigned long __must_check
__arch_copy_to_user(void __user
*to
, const void *from
, unsigned long n
);
379 #define raw_copy_to_user(to, from, n) \
381 __arch_copy_to_user(__uaccess_mask_ptr(to), (from), (n)); \
384 extern unsigned long __must_check
__arch_copy_in_user(void __user
*to
, const void __user
*from
, unsigned long n
);
385 #define raw_copy_in_user(to, from, n) \
387 __arch_copy_in_user(__uaccess_mask_ptr(to), \
388 __uaccess_mask_ptr(from), (n)); \
391 #define INLINE_COPY_TO_USER
392 #define INLINE_COPY_FROM_USER
394 extern unsigned long __must_check
__arch_clear_user(void __user
*to
, unsigned long n
);
395 static inline unsigned long __must_check
__clear_user(void __user
*to
, unsigned long n
)
397 if (access_ok(to
, n
))
398 n
= __arch_clear_user(__uaccess_mask_ptr(to
), n
);
401 #define clear_user __clear_user
403 extern long strncpy_from_user(char *dest
, const char __user
*src
, long count
);
405 extern __must_check
long strnlen_user(const char __user
*str
, long n
);
407 #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
409 void memcpy_page_flushcache(char *to
, struct page
*page
, size_t offset
, size_t len
);
410 extern unsigned long __must_check
__copy_user_flushcache(void *to
, const void __user
*from
, unsigned long n
);
412 static inline int __copy_from_user_flushcache(void *dst
, const void __user
*src
, unsigned size
)
414 kasan_check_write(dst
, size
);
415 return __copy_user_flushcache(dst
, __uaccess_mask_ptr(src
), size
);
419 #endif /* __ASM_UACCESS_H */