Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / arch / arm / include / asm / uaccess-asm.h
blob4bccd895d954e58774d0ac42da61ea35e96de0f6
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #ifndef __ASM_UACCESS_ASM_H__
4 #define __ASM_UACCESS_ASM_H__
6 #include <asm/asm-offsets.h>
7 #include <asm/domain.h>
8 #include <asm/page.h>
9 #include <asm/thread_info.h>
11 .macro csdb
12 #ifdef CONFIG_THUMB2_KERNEL
13 .inst.w 0xf3af8014
14 #else
15 .inst 0xe320f014
16 #endif
17 .endm
19 .macro check_uaccess, addr:req, size:req, limit:req, tmp:req, bad:req
20 #ifndef CONFIG_CPU_USE_DOMAINS
21 adds \tmp, \addr, #\size - 1
22 sbcscc \tmp, \tmp, \limit
23 bcs \bad
24 #ifdef CONFIG_CPU_SPECTRE
25 movcs \addr, #0
26 csdb
27 #endif
28 #endif
29 .endm
31 .macro uaccess_mask_range_ptr, addr:req, size:req, limit:req, tmp:req
32 #ifdef CONFIG_CPU_SPECTRE
33 sub \tmp, \limit, #1
34 subs \tmp, \tmp, \addr @ tmp = limit - 1 - addr
35 addhs \tmp, \tmp, #1 @ if (tmp >= 0) {
36 subshs \tmp, \tmp, \size @ tmp = limit - (addr + size) }
37 movlo \addr, #0 @ if (tmp < 0) addr = NULL
38 csdb
39 #endif
40 .endm
42 #if defined(CONFIG_CPU_SW_DOMAIN_PAN)
44 .macro uaccess_disable, tmp, isb=1
46 * Whenever we re-enter userspace, the domains should always be
47 * set appropriately.
49 mov \tmp, #DACR_UACCESS_DISABLE
50 mcr p15, 0, \tmp, c3, c0, 0 @ Set domain register
51 .if \isb
52 instr_sync
53 .endif
54 .endm
56 .macro uaccess_enable, tmp, isb=1
58 * Whenever we re-enter userspace, the domains should always be
59 * set appropriately.
61 mov \tmp, #DACR_UACCESS_ENABLE
62 mcr p15, 0, \tmp, c3, c0, 0
63 .if \isb
64 instr_sync
65 .endif
66 .endm
68 #elif defined(CONFIG_CPU_TTBR0_PAN)
70 .macro uaccess_disable, tmp, isb=1
72 * Disable TTBR0 page table walks (EDP0 = 1), use the reserved ASID
73 * from TTBR1 (A1 = 1) and enable TTBR1 page table walks for kernel
74 * addresses by reducing TTBR0 range to 32MB (T0SZ = 7).
76 mrc p15, 0, \tmp, c2, c0, 2 @ read TTBCR
77 orr \tmp, \tmp, #TTBCR_EPD0 | TTBCR_T0SZ_MASK
78 orr \tmp, \tmp, #TTBCR_A1
79 mcr p15, 0, \tmp, c2, c0, 2 @ write TTBCR
80 .if \isb
81 instr_sync
82 .endif
83 .endm
85 .macro uaccess_enable, tmp, isb=1
87 * Enable TTBR0 page table walks (T0SZ = 0, EDP0 = 0) and ASID from
88 * TTBR0 (A1 = 0).
90 mrc p15, 0, \tmp, c2, c0, 2 @ read TTBCR
91 bic \tmp, \tmp, #TTBCR_EPD0 | TTBCR_T0SZ_MASK
92 bic \tmp, \tmp, #TTBCR_A1
93 mcr p15, 0, \tmp, c2, c0, 2 @ write TTBCR
94 .if \isb
95 instr_sync
96 .endif
97 .endm
99 #else
101 .macro uaccess_disable, tmp, isb=1
102 .endm
104 .macro uaccess_enable, tmp, isb=1
105 .endm
107 #endif
109 #if defined(CONFIG_CPU_SW_DOMAIN_PAN) || defined(CONFIG_CPU_USE_DOMAINS)
110 #define DACR(x...) x
111 #else
112 #define DACR(x...)
113 #endif
115 #ifdef CONFIG_CPU_TTBR0_PAN
116 #define PAN(x...) x
117 #else
118 #define PAN(x...)
119 #endif
122 * Save the address limit on entry to a privileged exception.
124 * If we are using the DACR for kernel access by the user accessors
125 * (CONFIG_CPU_USE_DOMAINS=y), always reset the DACR kernel domain
126 * back to client mode, whether or not \disable is set.
128 * If we are using SW PAN, set the DACR user domain to no access
129 * if \disable is set.
131 .macro uaccess_entry, tsk, tmp0, tmp1, tmp2, disable
132 DACR( mrc p15, 0, \tmp0, c3, c0, 0)
133 DACR( str \tmp0, [sp, #SVC_DACR])
134 PAN( mrc p15, 0, \tmp0, c2, c0, 2)
135 PAN( str \tmp0, [sp, #SVC_TTBCR])
136 .if \disable && IS_ENABLED(CONFIG_CPU_SW_DOMAIN_PAN)
137 /* kernel=client, user=no access */
138 mov \tmp2, #DACR_UACCESS_DISABLE
139 mcr p15, 0, \tmp2, c3, c0, 0
140 instr_sync
141 .elseif IS_ENABLED(CONFIG_CPU_USE_DOMAINS)
142 /* kernel=client */
143 bic \tmp2, \tmp0, #domain_mask(DOMAIN_KERNEL)
144 orr \tmp2, \tmp2, #domain_val(DOMAIN_KERNEL, DOMAIN_CLIENT)
145 mcr p15, 0, \tmp2, c3, c0, 0
146 instr_sync
147 .endif
148 .endm
150 /* Restore the user access state previously saved by uaccess_entry */
151 .macro uaccess_exit, tsk, tmp0, tmp1
152 DACR( ldr \tmp0, [sp, #SVC_DACR])
153 DACR( mcr p15, 0, \tmp0, c3, c0, 0)
154 PAN( ldr \tmp0, [sp, #SVC_TTBCR])
155 PAN( mcr p15, 0, \tmp0, c2, c0, 2)
156 .endm
158 #undef DACR
159 #undef PAN
161 #endif /* __ASM_UACCESS_ASM_H__ */