1 /* SPDX-License-Identifier: GPL-2.0-only */
5 * Copyright (C) 2012 ARM Ltd.
6 * Author: Marc Zyngier <marc.zyngier@arm.com>
9 #include <linux/init.h>
10 #include <linux/linkage.h>
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>
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
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
38 .macro check_override idreg, fld, pass, fail
40 __check_override \idreg \fld 4 \pass \fail
44 .pushsection .hyp.text, "ax"
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
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
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
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)
72 SYM_CODE_START_LOCAL(elx_sync)
73 cmp x0, #HVC_SET_VECTORS
78 1: cmp x0, #HVC_FINALISE_EL2
81 2: cmp x0, #HVC_SOFT_RESTART
89 3: cmp x0, #HVC_RESET_VECTORS
90 beq 9f // Nothing to reset!
92 /* Someone called kvm_call_hyp() against the hyp-stub... */
93 mov_q x0, HVC_STUB_ERR
98 SYM_CODE_END(elx_sync)
100 SYM_CODE_START_LOCAL(__finalise_el2)
101 check_override id_aa64pfr0 ID_AA64PFR0_SVE_SHIFT .Linit_sve .Lskip_sve
103 .Linit_sve: /* SVE register access */
104 mrs x0, cptr_el2 // Disable SVE traps
105 bic x0, x0, #CPTR_EL2_TZ
108 mov x1, #ZCR_ELx_LEN_MASK // SVE: Enable full vector
109 msr_s SYS_ZCR_EL2, x1 // length for EL1.
112 check_override id_aa64pfr1 ID_AA64PFR1_SME_SHIFT .Linit_sme .Lskip_sme
114 .Linit_sme: /* SME register access and priority mapping */
115 mrs x0, cptr_el2 // Disable SME traps
116 bic x0, x0, #CPTR_EL2_TSM
121 orr x1, x1, #SCTLR_ELx_ENTP2 // Disable TPIDR2 traps
125 mov x1, #0 // SMCR controls
127 mrs_s x2, SYS_ID_AA64SMFR0_EL1
128 ubfx x2, x2, #ID_AA64SMFR0_FA64_SHIFT, #1 // Full FP in SM?
129 cbz x2, .Lskip_sme_fa64
131 orr x1, x1, SMCR_ELx_FA64_MASK
134 orr x1, x1, #SMCR_ELx_LEN_MASK // Enable full SME vector
135 msr_s SYS_SMCR_EL2, x1 // length for EL1.
137 mrs_s x1, SYS_SMIDR_EL1 // Priority mapping supported?
138 ubfx x1, x1, #SMIDR_EL1_SMPS_SHIFT, #1
141 msr_s SYS_SMPRIMAP_EL2, xzr // Make all priorities equal
143 mrs x1, id_aa64mmfr1_el1 // HCRX_EL2 present?
144 ubfx x1, x1, #ID_AA64MMFR1_HCX_SHIFT, #4
147 mrs_s x1, SYS_HCRX_EL2
148 orr x1, x1, #HCRX_EL2_SMPME_MASK // Enable priority mapping
149 msr_s SYS_HCRX_EL2, x1
153 // nVHE? No way! Give me the real thing!
154 // Sanity check: MMU *must* be off
158 // Needs to be VHE capable, obviously
159 check_override id_aa64mmfr1 ID_AA64MMFR1_VHE_SHIFT 2f 1f
161 1: mov_q x0, HVC_STUB_ERR
164 // Engage the VHE magic!
165 mov_q x0, HCR_HOST_VHE_FLAGS
169 // Use the EL1 allocated stack, per-cpu offset
175 // FP configuration, vectors
176 mrs_s x0, SYS_CPACR_EL12
178 mrs_s x0, SYS_VBAR_EL12
181 // Use EL2 translations for SPE & TRBE and disable access from EL1
183 bic x0, x0, #(MDCR_EL2_E2PB_MASK << MDCR_EL2_E2PB_SHIFT)
184 bic x0, x0, #(MDCR_EL2_E2TB_MASK << MDCR_EL2_E2TB_SHIFT)
187 // Transfer the MM state from EL1 to EL2
188 mrs_s x0, SYS_TCR_EL12
190 mrs_s x0, SYS_TTBR0_EL12
192 mrs_s x0, SYS_TTBR1_EL12
194 mrs_s x0, SYS_MAIR_EL12
198 // Hack the exception return to stay at EL2
200 and x0, x0, #~PSR_MODE_MASK
201 mov x1, #PSR_MODE_EL2h
206 SYM_CODE_END(__finalise_el2)
208 // At the point where we reach enter_vhe(), we run with
209 // the MMU off (which is enforced by __finalise_el2()).
210 // We thus need to be in the idmap, or everything will
211 // explode when enabling the MMU.
213 .pushsection .idmap.text, "ax"
215 SYM_CODE_START_LOCAL(enter_vhe)
216 // Invalidate TLBs before enabling the MMU
221 // Enable the EL2 S1 MMU, as set up from EL1
222 mrs_s x0, SYS_SCTLR_EL12
225 // Disable the EL1 S1 MMU for a good measure
226 mov_q x0, INIT_SCTLR_EL1_MMU_OFF
227 msr_s SYS_SCTLR_EL12, x0
232 SYM_CODE_END(enter_vhe)
236 .macro invalid_vector label
237 SYM_CODE_START_LOCAL(\label)
242 invalid_vector el2_sync_invalid
243 invalid_vector el2_irq_invalid
244 invalid_vector el2_fiq_invalid
245 invalid_vector el2_error_invalid
246 invalid_vector el1_sync_invalid
247 invalid_vector el1_irq_invalid
248 invalid_vector el1_fiq_invalid
249 invalid_vector el1_error_invalid
254 * __hyp_set_vectors: Call this after boot to set the initial hypervisor
255 * vectors as part of hypervisor installation. On an SMP system, this should
256 * be called on each CPU.
258 * x0 must be the physical address of the new vector table, and must be
261 * Before calling this, you must check that the stub hypervisor is installed
262 * everywhere, by waiting for any secondary CPUs to be brought up and then
263 * checking that is_hyp_mode_available() is true.
265 * If not, there is a pre-existing hypervisor, some CPUs failed to boot, or
266 * something else went wrong... in such cases, trying to install a new
267 * hypervisor is unlikely to work as desired.
269 * When you call into your shiny new hypervisor, sp_el2 will contain junk,
270 * so you will need to set that to something sensible at the new hypervisor's
271 * initialisation entry point.
274 SYM_FUNC_START(__hyp_set_vectors)
276 mov x0, #HVC_SET_VECTORS
279 SYM_FUNC_END(__hyp_set_vectors)
281 SYM_FUNC_START(__hyp_reset_vectors)
282 mov x0, #HVC_RESET_VECTORS
285 SYM_FUNC_END(__hyp_reset_vectors)
288 * Entry point to finalise EL2 and switch to VHE if deemed capable
290 * w0: boot mode, as returned by init_kernel_el()
292 SYM_FUNC_START(finalise_el2)
293 // Need to have booted at EL2
294 cmp w0, #BOOT_CPU_MODE_EL2
297 // and still be at EL1
299 cmp x0, #CurrentEL_EL1
302 mov x0, #HVC_FINALISE_EL2
306 SYM_FUNC_END(finalise_el2)