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>
20 .pushsection .hyp.text, "ax"
24 SYM_CODE_START(__hyp_stub_vectors)
25 ventry el2_sync_invalid // Synchronous EL2t
26 ventry el2_irq_invalid // IRQ EL2t
27 ventry el2_fiq_invalid // FIQ EL2t
28 ventry el2_error_invalid // Error EL2t
30 ventry elx_sync // Synchronous EL2h
31 ventry el2_irq_invalid // IRQ EL2h
32 ventry el2_fiq_invalid // FIQ EL2h
33 ventry el2_error_invalid // Error EL2h
35 ventry elx_sync // Synchronous 64-bit EL1
36 ventry el1_irq_invalid // IRQ 64-bit EL1
37 ventry el1_fiq_invalid // FIQ 64-bit EL1
38 ventry el1_error_invalid // Error 64-bit EL1
40 ventry el1_sync_invalid // Synchronous 32-bit EL1
41 ventry el1_irq_invalid // IRQ 32-bit EL1
42 ventry el1_fiq_invalid // FIQ 32-bit EL1
43 ventry el1_error_invalid // Error 32-bit EL1
44 SYM_CODE_END(__hyp_stub_vectors)
48 SYM_CODE_START_LOCAL(elx_sync)
49 cmp x0, #HVC_SET_VECTORS
54 1: cmp x0, #HVC_FINALISE_EL2
57 2: cmp x0, #HVC_SOFT_RESTART
65 3: cmp x0, #HVC_RESET_VECTORS
66 beq 9f // Nothing to reset!
68 /* Someone called kvm_call_hyp() against the hyp-stub... */
69 mov_q x0, HVC_STUB_ERR
74 SYM_CODE_END(elx_sync)
76 SYM_CODE_START_LOCAL(__finalise_el2)
79 // nVHE? No way! Give me the real thing!
80 // Sanity check: MMU *must* be off
84 // Needs to be VHE capable, obviously
85 check_override id_aa64mmfr1 ID_AA64MMFR1_EL1_VH_SHIFT 2f 1f x1 x2
87 1: mov_q x0, HVC_STUB_ERR
90 // Engage the VHE magic!
91 mov_q x0, HCR_HOST_VHE_FLAGS
95 // Use the EL1 allocated stack, per-cpu offset
101 // FP configuration, vectors
102 mrs_s x0, SYS_CPACR_EL12
104 mrs_s x0, SYS_VBAR_EL12
107 // Use EL2 translations for SPE & TRBE and disable access from EL1
109 bic x0, x0, #(MDCR_EL2_E2PB_MASK << MDCR_EL2_E2PB_SHIFT)
110 bic x0, x0, #(MDCR_EL2_E2TB_MASK << MDCR_EL2_E2TB_SHIFT)
113 // Transfer the MM state from EL1 to EL2
114 mrs_s x0, SYS_TCR_EL12
116 mrs_s x0, SYS_TTBR0_EL12
118 mrs_s x0, SYS_TTBR1_EL12
120 mrs_s x0, SYS_MAIR_EL12
122 mrs x1, REG_ID_AA64MMFR3_EL1
123 ubfx x1, x1, #ID_AA64MMFR3_EL1_TCRX_SHIFT, #4
125 mrs x0, REG_TCR2_EL12
128 // Transfer permission indirection state
129 mrs x1, REG_ID_AA64MMFR3_EL1
130 ubfx x1, x1, #ID_AA64MMFR3_EL1_S1PIE_SHIFT, #4
131 cbz x1, .Lskip_indirection
132 mrs x0, REG_PIRE0_EL12
133 msr REG_PIRE0_EL1, x0
142 // Hack the exception return to stay at EL2
144 and x0, x0, #~PSR_MODE_MASK
145 mov x1, #PSR_MODE_EL2h
150 SYM_CODE_END(__finalise_el2)
152 // At the point where we reach enter_vhe(), we run with
153 // the MMU off (which is enforced by __finalise_el2()).
154 // We thus need to be in the idmap, or everything will
155 // explode when enabling the MMU.
157 .pushsection .idmap.text, "ax"
159 SYM_CODE_START_LOCAL(enter_vhe)
160 // Invalidate TLBs before enabling the MMU
165 // Enable the EL2 S1 MMU, as set up from EL1
166 mrs_s x0, SYS_SCTLR_EL12
169 // Disable the EL1 S1 MMU for a good measure
170 mov_q x0, INIT_SCTLR_EL1_MMU_OFF
171 msr_s SYS_SCTLR_EL12, x0
176 SYM_CODE_END(enter_vhe)
180 .macro invalid_vector label
181 SYM_CODE_START_LOCAL(\label)
186 invalid_vector el2_sync_invalid
187 invalid_vector el2_irq_invalid
188 invalid_vector el2_fiq_invalid
189 invalid_vector el2_error_invalid
190 invalid_vector el1_sync_invalid
191 invalid_vector el1_irq_invalid
192 invalid_vector el1_fiq_invalid
193 invalid_vector el1_error_invalid
198 * __hyp_set_vectors: Call this after boot to set the initial hypervisor
199 * vectors as part of hypervisor installation. On an SMP system, this should
200 * be called on each CPU.
202 * x0 must be the physical address of the new vector table, and must be
205 * Before calling this, you must check that the stub hypervisor is installed
206 * everywhere, by waiting for any secondary CPUs to be brought up and then
207 * checking that is_hyp_mode_available() is true.
209 * If not, there is a pre-existing hypervisor, some CPUs failed to boot, or
210 * something else went wrong... in such cases, trying to install a new
211 * hypervisor is unlikely to work as desired.
213 * When you call into your shiny new hypervisor, sp_el2 will contain junk,
214 * so you will need to set that to something sensible at the new hypervisor's
215 * initialisation entry point.
218 SYM_FUNC_START(__hyp_set_vectors)
220 mov x0, #HVC_SET_VECTORS
223 SYM_FUNC_END(__hyp_set_vectors)
225 SYM_FUNC_START(__hyp_reset_vectors)
226 mov x0, #HVC_RESET_VECTORS
229 SYM_FUNC_END(__hyp_reset_vectors)
232 * Entry point to finalise EL2 and switch to VHE if deemed capable
234 * w0: boot mode, as returned by init_kernel_el()
236 SYM_FUNC_START(finalise_el2)
237 // Need to have booted at EL2
238 cmp w0, #BOOT_CPU_MODE_EL2
241 // and still be at EL1
243 cmp x0, #CurrentEL_EL1
246 mov x0, #HVC_FINALISE_EL2
250 SYM_FUNC_END(finalise_el2)