arm64: Factor out checking of a feature against the override into a macro
[linux-2.6-microblaze.git] / arch / arm64 / kernel / hyp-stub.S
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Hypervisor stub
4  *
5  * Copyright (C) 2012 ARM Ltd.
6  * Author:      Marc Zyngier <marc.zyngier@arm.com>
7  */
8
9 #include <linux/init.h>
10 #include <linux/linkage.h>
11
12 #include <asm/assembler.h>
13 #include <asm/el2_setup.h>
14 #include <asm/kvm_arm.h>
15 #include <asm/kvm_asm.h>
16 #include <asm/ptrace.h>
17 #include <asm/virt.h>
18
19 // Warning, hardcoded register allocation
20 // This will clobber x1 and x2.
21 .macro check_override idreg, fld, pass, fail
22         mrs     x1, \idreg\()_el1
23         ubfx    x1, x1, #\fld, #4
24         cbz     x1, \fail
25
26         adr_l   x1, \idreg\()_override
27         ldr     x2, [x1, FTR_OVR_VAL_OFFSET]
28         ldr     x1, [x1, FTR_OVR_MASK_OFFSET]
29         ubfx    x2, x2, #\fld, #4
30         ubfx    x1, x1, #\fld, #4
31         cmp     x1, xzr
32         and     x2, x2, x1
33         csinv   x2, x2, xzr, ne
34         cbnz    x2, \pass
35         b       \fail
36 .endm
37
38         .text
39         .pushsection    .hyp.text, "ax"
40
41         .align 11
42
43 SYM_CODE_START(__hyp_stub_vectors)
44         ventry  el2_sync_invalid                // Synchronous EL2t
45         ventry  el2_irq_invalid                 // IRQ EL2t
46         ventry  el2_fiq_invalid                 // FIQ EL2t
47         ventry  el2_error_invalid               // Error EL2t
48
49         ventry  elx_sync                        // Synchronous EL2h
50         ventry  el2_irq_invalid                 // IRQ EL2h
51         ventry  el2_fiq_invalid                 // FIQ EL2h
52         ventry  el2_error_invalid               // Error EL2h
53
54         ventry  elx_sync                        // Synchronous 64-bit EL1
55         ventry  el1_irq_invalid                 // IRQ 64-bit EL1
56         ventry  el1_fiq_invalid                 // FIQ 64-bit EL1
57         ventry  el1_error_invalid               // Error 64-bit EL1
58
59         ventry  el1_sync_invalid                // Synchronous 32-bit EL1
60         ventry  el1_irq_invalid                 // IRQ 32-bit EL1
61         ventry  el1_fiq_invalid                 // FIQ 32-bit EL1
62         ventry  el1_error_invalid               // Error 32-bit EL1
63 SYM_CODE_END(__hyp_stub_vectors)
64
65         .align 11
66
67 SYM_CODE_START_LOCAL(elx_sync)
68         cmp     x0, #HVC_SET_VECTORS
69         b.ne    1f
70         msr     vbar_el2, x1
71         b       9f
72
73 1:      cmp     x0, #HVC_FINALISE_EL2
74         b.eq    __finalise_el2
75
76 2:      cmp     x0, #HVC_SOFT_RESTART
77         b.ne    3f
78         mov     x0, x2
79         mov     x2, x4
80         mov     x4, x1
81         mov     x1, x3
82         br      x4                              // no return
83
84 3:      cmp     x0, #HVC_RESET_VECTORS
85         beq     9f                              // Nothing to reset!
86
87         /* Someone called kvm_call_hyp() against the hyp-stub... */
88         mov_q   x0, HVC_STUB_ERR
89         eret
90
91 9:      mov     x0, xzr
92         eret
93 SYM_CODE_END(elx_sync)
94
95 SYM_CODE_START_LOCAL(__finalise_el2)
96         // nVHE? No way! Give me the real thing!
97         // Sanity check: MMU *must* be off
98         mrs     x1, sctlr_el2
99         tbnz    x1, #0, 1f
100
101         // Needs to be VHE capable, obviously
102         check_override id_aa64mmfr1 ID_AA64MMFR1_VHE_SHIFT 2f 1f
103
104 1:      mov_q   x0, HVC_STUB_ERR
105         eret
106 2:
107         // Engage the VHE magic!
108         mov_q   x0, HCR_HOST_VHE_FLAGS
109         msr     hcr_el2, x0
110         isb
111
112         // Use the EL1 allocated stack, per-cpu offset
113         mrs     x0, sp_el1
114         mov     sp, x0
115         mrs     x0, tpidr_el1
116         msr     tpidr_el2, x0
117
118         // FP configuration, vectors
119         mrs_s   x0, SYS_CPACR_EL12
120         msr     cpacr_el1, x0
121         mrs_s   x0, SYS_VBAR_EL12
122         msr     vbar_el1, x0
123
124         // Use EL2 translations for SPE & TRBE and disable access from EL1
125         mrs     x0, mdcr_el2
126         bic     x0, x0, #(MDCR_EL2_E2PB_MASK << MDCR_EL2_E2PB_SHIFT)
127         bic     x0, x0, #(MDCR_EL2_E2TB_MASK << MDCR_EL2_E2TB_SHIFT)
128         msr     mdcr_el2, x0
129
130         // Transfer the MM state from EL1 to EL2
131         mrs_s   x0, SYS_TCR_EL12
132         msr     tcr_el1, x0
133         mrs_s   x0, SYS_TTBR0_EL12
134         msr     ttbr0_el1, x0
135         mrs_s   x0, SYS_TTBR1_EL12
136         msr     ttbr1_el1, x0
137         mrs_s   x0, SYS_MAIR_EL12
138         msr     mair_el1, x0
139         isb
140
141         // Hack the exception return to stay at EL2
142         mrs     x0, spsr_el1
143         and     x0, x0, #~PSR_MODE_MASK
144         mov     x1, #PSR_MODE_EL2h
145         orr     x0, x0, x1
146         msr     spsr_el1, x0
147
148         b       enter_vhe
149 SYM_CODE_END(__finalise_el2)
150
151         // At the point where we reach enter_vhe(), we run with
152         // the MMU off (which is enforced by __finalise_el2()).
153         // We thus need to be in the idmap, or everything will
154         // explode when enabling the MMU.
155
156         .pushsection    .idmap.text, "ax"
157
158 SYM_CODE_START_LOCAL(enter_vhe)
159         // Invalidate TLBs before enabling the MMU
160         tlbi    vmalle1
161         dsb     nsh
162         isb
163
164         // Enable the EL2 S1 MMU, as set up from EL1
165         mrs_s   x0, SYS_SCTLR_EL12
166         set_sctlr_el1   x0
167
168         // Disable the EL1 S1 MMU for a good measure
169         mov_q   x0, INIT_SCTLR_EL1_MMU_OFF
170         msr_s   SYS_SCTLR_EL12, x0
171
172         mov     x0, xzr
173
174         eret
175 SYM_CODE_END(enter_vhe)
176
177         .popsection
178
179 .macro invalid_vector   label
180 SYM_CODE_START_LOCAL(\label)
181         b \label
182 SYM_CODE_END(\label)
183 .endm
184
185         invalid_vector  el2_sync_invalid
186         invalid_vector  el2_irq_invalid
187         invalid_vector  el2_fiq_invalid
188         invalid_vector  el2_error_invalid
189         invalid_vector  el1_sync_invalid
190         invalid_vector  el1_irq_invalid
191         invalid_vector  el1_fiq_invalid
192         invalid_vector  el1_error_invalid
193
194         .popsection
195
196 /*
197  * __hyp_set_vectors: Call this after boot to set the initial hypervisor
198  * vectors as part of hypervisor installation.  On an SMP system, this should
199  * be called on each CPU.
200  *
201  * x0 must be the physical address of the new vector table, and must be
202  * 2KB aligned.
203  *
204  * Before calling this, you must check that the stub hypervisor is installed
205  * everywhere, by waiting for any secondary CPUs to be brought up and then
206  * checking that is_hyp_mode_available() is true.
207  *
208  * If not, there is a pre-existing hypervisor, some CPUs failed to boot, or
209  * something else went wrong... in such cases, trying to install a new
210  * hypervisor is unlikely to work as desired.
211  *
212  * When you call into your shiny new hypervisor, sp_el2 will contain junk,
213  * so you will need to set that to something sensible at the new hypervisor's
214  * initialisation entry point.
215  */
216
217 SYM_FUNC_START(__hyp_set_vectors)
218         mov     x1, x0
219         mov     x0, #HVC_SET_VECTORS
220         hvc     #0
221         ret
222 SYM_FUNC_END(__hyp_set_vectors)
223
224 SYM_FUNC_START(__hyp_reset_vectors)
225         mov     x0, #HVC_RESET_VECTORS
226         hvc     #0
227         ret
228 SYM_FUNC_END(__hyp_reset_vectors)
229
230 /*
231  * Entry point to finalise EL2 and switch to VHE if deemed capable
232  *
233  * w0: boot mode, as returned by init_kernel_el()
234  */
235 SYM_FUNC_START(finalise_el2)
236         // Need to have booted at EL2
237         cmp     w0, #BOOT_CPU_MODE_EL2
238         b.ne    1f
239
240         // and still be at EL1
241         mrs     x0, CurrentEL
242         cmp     x0, #CurrentEL_EL1
243         b.ne    1f
244
245         mov     x0, #HVC_FINALISE_EL2
246         hvc     #0
247 1:
248         ret
249 SYM_FUNC_END(finalise_el2)