arm64: Add the arm64.nosme command line option
[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, and expect x1 to contain
21 // the id register value as read from the HW
22 .macro __check_override idreg, fld, width, pass, fail
23         ubfx    x1, x1, #\fld, #\width
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, #\width
30         ubfx    x1, x1, #\fld, #\width
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 .macro check_override idreg, fld, pass, fail
39         mrs     x1, \idreg\()_el1
40         __check_override \idreg \fld 4 \pass \fail
41 .endm
42
43         .text
44         .pushsection    .hyp.text, "ax"
45
46         .align 11
47
48 SYM_CODE_START(__hyp_stub_vectors)
49         ventry  el2_sync_invalid                // Synchronous EL2t
50         ventry  el2_irq_invalid                 // IRQ EL2t
51         ventry  el2_fiq_invalid                 // FIQ EL2t
52         ventry  el2_error_invalid               // Error EL2t
53
54         ventry  elx_sync                        // Synchronous EL2h
55         ventry  el2_irq_invalid                 // IRQ EL2h
56         ventry  el2_fiq_invalid                 // FIQ EL2h
57         ventry  el2_error_invalid               // Error EL2h
58
59         ventry  elx_sync                        // Synchronous 64-bit EL1
60         ventry  el1_irq_invalid                 // IRQ 64-bit EL1
61         ventry  el1_fiq_invalid                 // FIQ 64-bit EL1
62         ventry  el1_error_invalid               // Error 64-bit EL1
63
64         ventry  el1_sync_invalid                // Synchronous 32-bit EL1
65         ventry  el1_irq_invalid                 // IRQ 32-bit EL1
66         ventry  el1_fiq_invalid                 // FIQ 32-bit EL1
67         ventry  el1_error_invalid               // Error 32-bit EL1
68 SYM_CODE_END(__hyp_stub_vectors)
69
70         .align 11
71
72 SYM_CODE_START_LOCAL(elx_sync)
73         cmp     x0, #HVC_SET_VECTORS
74         b.ne    1f
75         msr     vbar_el2, x1
76         b       9f
77
78 1:      cmp     x0, #HVC_FINALISE_EL2
79         b.eq    __finalise_el2
80
81 2:      cmp     x0, #HVC_SOFT_RESTART
82         b.ne    3f
83         mov     x0, x2
84         mov     x2, x4
85         mov     x4, x1
86         mov     x1, x3
87         br      x4                              // no return
88
89 3:      cmp     x0, #HVC_RESET_VECTORS
90         beq     9f                              // Nothing to reset!
91
92         /* Someone called kvm_call_hyp() against the hyp-stub... */
93         mov_q   x0, HVC_STUB_ERR
94         eret
95
96 9:      mov     x0, xzr
97         eret
98 SYM_CODE_END(elx_sync)
99
100 SYM_CODE_START_LOCAL(__finalise_el2)
101         check_override id_aa64pfr1 ID_AA64PFR1_SME_SHIFT .Linit_sme .Lskip_sme
102
103 .Linit_sme:     /* SME register access and priority mapping */
104         mrs     x0, cptr_el2                    // Disable SME traps
105         bic     x0, x0, #CPTR_EL2_TSM
106         msr     cptr_el2, x0
107         isb
108
109         mrs     x1, sctlr_el2
110         orr     x1, x1, #SCTLR_ELx_ENTP2        // Disable TPIDR2 traps
111         msr     sctlr_el2, x1
112         isb
113
114         mov     x1, #0                          // SMCR controls
115
116         mrs_s   x2, SYS_ID_AA64SMFR0_EL1
117         ubfx    x2, x2, #ID_AA64SMFR0_FA64_SHIFT, #1 // Full FP in SM?
118         cbz     x2, .Lskip_sme_fa64
119
120         orr     x1, x1, SMCR_ELx_FA64_MASK
121 .Lskip_sme_fa64:
122
123         orr     x1, x1, #SMCR_ELx_LEN_MASK      // Enable full SME vector
124         msr_s   SYS_SMCR_EL2, x1                // length for EL1.
125
126         mrs_s   x1, SYS_SMIDR_EL1               // Priority mapping supported?
127         ubfx    x1, x1, #SMIDR_EL1_SMPS_SHIFT, #1
128         cbz     x1, .Lskip_sme
129
130         msr_s   SYS_SMPRIMAP_EL2, xzr           // Make all priorities equal
131
132         mrs     x1, id_aa64mmfr1_el1            // HCRX_EL2 present?
133         ubfx    x1, x1, #ID_AA64MMFR1_HCX_SHIFT, #4
134         cbz     x1, .Lskip_sme
135
136         mrs_s   x1, SYS_HCRX_EL2
137         orr     x1, x1, #HCRX_EL2_SMPME_MASK    // Enable priority mapping
138         msr_s   SYS_HCRX_EL2, x1
139
140 .Lskip_sme:
141
142         // nVHE? No way! Give me the real thing!
143         // Sanity check: MMU *must* be off
144         mrs     x1, sctlr_el2
145         tbnz    x1, #0, 1f
146
147         // Needs to be VHE capable, obviously
148         check_override id_aa64mmfr1 ID_AA64MMFR1_VHE_SHIFT 2f 1f
149
150 1:      mov_q   x0, HVC_STUB_ERR
151         eret
152 2:
153         // Engage the VHE magic!
154         mov_q   x0, HCR_HOST_VHE_FLAGS
155         msr     hcr_el2, x0
156         isb
157
158         // Use the EL1 allocated stack, per-cpu offset
159         mrs     x0, sp_el1
160         mov     sp, x0
161         mrs     x0, tpidr_el1
162         msr     tpidr_el2, x0
163
164         // FP configuration, vectors
165         mrs_s   x0, SYS_CPACR_EL12
166         msr     cpacr_el1, x0
167         mrs_s   x0, SYS_VBAR_EL12
168         msr     vbar_el1, x0
169
170         // Use EL2 translations for SPE & TRBE and disable access from EL1
171         mrs     x0, mdcr_el2
172         bic     x0, x0, #(MDCR_EL2_E2PB_MASK << MDCR_EL2_E2PB_SHIFT)
173         bic     x0, x0, #(MDCR_EL2_E2TB_MASK << MDCR_EL2_E2TB_SHIFT)
174         msr     mdcr_el2, x0
175
176         // Transfer the MM state from EL1 to EL2
177         mrs_s   x0, SYS_TCR_EL12
178         msr     tcr_el1, x0
179         mrs_s   x0, SYS_TTBR0_EL12
180         msr     ttbr0_el1, x0
181         mrs_s   x0, SYS_TTBR1_EL12
182         msr     ttbr1_el1, x0
183         mrs_s   x0, SYS_MAIR_EL12
184         msr     mair_el1, x0
185         isb
186
187         // Hack the exception return to stay at EL2
188         mrs     x0, spsr_el1
189         and     x0, x0, #~PSR_MODE_MASK
190         mov     x1, #PSR_MODE_EL2h
191         orr     x0, x0, x1
192         msr     spsr_el1, x0
193
194         b       enter_vhe
195 SYM_CODE_END(__finalise_el2)
196
197         // At the point where we reach enter_vhe(), we run with
198         // the MMU off (which is enforced by __finalise_el2()).
199         // We thus need to be in the idmap, or everything will
200         // explode when enabling the MMU.
201
202         .pushsection    .idmap.text, "ax"
203
204 SYM_CODE_START_LOCAL(enter_vhe)
205         // Invalidate TLBs before enabling the MMU
206         tlbi    vmalle1
207         dsb     nsh
208         isb
209
210         // Enable the EL2 S1 MMU, as set up from EL1
211         mrs_s   x0, SYS_SCTLR_EL12
212         set_sctlr_el1   x0
213
214         // Disable the EL1 S1 MMU for a good measure
215         mov_q   x0, INIT_SCTLR_EL1_MMU_OFF
216         msr_s   SYS_SCTLR_EL12, x0
217
218         mov     x0, xzr
219
220         eret
221 SYM_CODE_END(enter_vhe)
222
223         .popsection
224
225 .macro invalid_vector   label
226 SYM_CODE_START_LOCAL(\label)
227         b \label
228 SYM_CODE_END(\label)
229 .endm
230
231         invalid_vector  el2_sync_invalid
232         invalid_vector  el2_irq_invalid
233         invalid_vector  el2_fiq_invalid
234         invalid_vector  el2_error_invalid
235         invalid_vector  el1_sync_invalid
236         invalid_vector  el1_irq_invalid
237         invalid_vector  el1_fiq_invalid
238         invalid_vector  el1_error_invalid
239
240         .popsection
241
242 /*
243  * __hyp_set_vectors: Call this after boot to set the initial hypervisor
244  * vectors as part of hypervisor installation.  On an SMP system, this should
245  * be called on each CPU.
246  *
247  * x0 must be the physical address of the new vector table, and must be
248  * 2KB aligned.
249  *
250  * Before calling this, you must check that the stub hypervisor is installed
251  * everywhere, by waiting for any secondary CPUs to be brought up and then
252  * checking that is_hyp_mode_available() is true.
253  *
254  * If not, there is a pre-existing hypervisor, some CPUs failed to boot, or
255  * something else went wrong... in such cases, trying to install a new
256  * hypervisor is unlikely to work as desired.
257  *
258  * When you call into your shiny new hypervisor, sp_el2 will contain junk,
259  * so you will need to set that to something sensible at the new hypervisor's
260  * initialisation entry point.
261  */
262
263 SYM_FUNC_START(__hyp_set_vectors)
264         mov     x1, x0
265         mov     x0, #HVC_SET_VECTORS
266         hvc     #0
267         ret
268 SYM_FUNC_END(__hyp_set_vectors)
269
270 SYM_FUNC_START(__hyp_reset_vectors)
271         mov     x0, #HVC_RESET_VECTORS
272         hvc     #0
273         ret
274 SYM_FUNC_END(__hyp_reset_vectors)
275
276 /*
277  * Entry point to finalise EL2 and switch to VHE if deemed capable
278  *
279  * w0: boot mode, as returned by init_kernel_el()
280  */
281 SYM_FUNC_START(finalise_el2)
282         // Need to have booted at EL2
283         cmp     w0, #BOOT_CPU_MODE_EL2
284         b.ne    1f
285
286         // and still be at EL1
287         mrs     x0, CurrentEL
288         cmp     x0, #CurrentEL_EL1
289         b.ne    1f
290
291         mov     x0, #HVC_FINALISE_EL2
292         hvc     #0
293 1:
294         ret
295 SYM_FUNC_END(finalise_el2)