1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (C) 2012,2013 - ARM Ltd
4 * Author: Marc Zyngier <marc.zyngier@arm.com>
7 #include <linux/arm-smccc.h>
8 #include <linux/linkage.h>
10 #include <asm/alternative.h>
11 #include <asm/assembler.h>
12 #include <asm/el2_setup.h>
13 #include <asm/kvm_arm.h>
14 #include <asm/kvm_asm.h>
15 #include <asm/kvm_mmu.h>
16 #include <asm/pgtable-hwdef.h>
17 #include <asm/sysreg.h>
21 .pushsection .hyp.idmap.text, "ax"
25 SYM_CODE_START(__kvm_hyp_init)
26 ventry __invalid // Synchronous EL2t
27 ventry __invalid // IRQ EL2t
28 ventry __invalid // FIQ EL2t
29 ventry __invalid // Error EL2t
31 ventry __invalid // Synchronous EL2h
32 ventry __invalid // IRQ EL2h
33 ventry __invalid // FIQ EL2h
34 ventry __invalid // Error EL2h
36 ventry __do_hyp_init // Synchronous 64-bit EL1
37 ventry __invalid // IRQ 64-bit EL1
38 ventry __invalid // FIQ 64-bit EL1
39 ventry __invalid // Error 64-bit EL1
41 ventry __invalid // Synchronous 32-bit EL1
42 ventry __invalid // IRQ 32-bit EL1
43 ventry __invalid // FIQ 32-bit EL1
44 ventry __invalid // Error 32-bit EL1
50 * x0: SMCCC function ID
51 * x1: struct kvm_nvhe_init_params PA
54 /* Check for a stub HVC call */
55 cmp x0, #HVC_STUB_HCALL_NR
56 b.lo __kvm_handle_stub_hvc
58 // We only actively check bits [24:31], and everything
59 // else has to be zero, which we check at build time.
60 #if (KVM_HOST_SMCCC_FUNC(__kvm_hyp_init) & 0xFFFFFFFF00FFFFFF)
61 #error Unexpected __KVM_HOST_SMCCC_FUNC___kvm_hyp_init value
65 eor x0, x0, #((KVM_HOST_SMCCC_FUNC(__kvm_hyp_init) >> 24) & 0xF)
67 eor x0, x0, #((KVM_HOST_SMCCC_FUNC(__kvm_hyp_init) >> 28) & 0xF)
69 mov x0, #SMCCC_RET_NOT_SUPPORTED
78 mov x0, #SMCCC_RET_SUCCESS
80 SYM_CODE_END(__kvm_hyp_init)
83 * Initialize the hypervisor in EL2.
85 * Only uses x0..x3 so as to not clobber callee-saved SMCCC registers
86 * and leave x4 for the caller.
88 * x0: struct kvm_nvhe_init_params PA
90 SYM_CODE_START_LOCAL(___kvm_hyp_init)
91 alternative_if ARM64_KVM_PROTECTED_MODE
92 mov_q x1, HCR_HOST_NVHE_PROTECTED_FLAGS
94 alternative_else_nop_endif
96 ldr x1, [x0, #NVHE_INIT_TPIDR_EL2]
99 ldr x1, [x0, #NVHE_INIT_STACK_HYP_VA]
102 ldr x1, [x0, #NVHE_INIT_MAIR_EL2]
105 ldr x1, [x0, #NVHE_INIT_PGD_PA]
107 alternative_if ARM64_HAS_CNP
108 orr x2, x2, #TTBR_CNP_BIT
109 alternative_else_nop_endif
113 * Set the PS bits in TCR_EL2.
115 ldr x1, [x0, #NVHE_INIT_TCR_EL2]
116 tcr_compute_pa_size x1, #TCR_EL2_PS_SHIFT, x2, x3
121 /* Invalidate the stale TLBs from Bootloader */
126 * Preserve all the RES1 bits while setting the default flags,
127 * as well as the EE bit on BE. Drop the A flag since the compiler
128 * is allowed to generate unaligned accesses.
130 mov_q x0, (SCTLR_EL2_RES1 | (SCTLR_ELx_FLAGS & ~SCTLR_ELx_A))
131 CPU_BE( orr x0, x0, #SCTLR_ELx_EE)
132 alternative_if ARM64_HAS_ADDRESS_AUTH
133 mov_q x1, (SCTLR_ELx_ENIA | SCTLR_ELx_ENIB | \
134 SCTLR_ELx_ENDA | SCTLR_ELx_ENDB)
136 alternative_else_nop_endif
140 /* Set the host vector */
141 ldr x0, =__kvm_hyp_host_vector
146 SYM_CODE_END(___kvm_hyp_init)
149 * PSCI CPU_ON entry point
151 * x0: struct kvm_nvhe_init_params PA
153 SYM_CODE_START(kvm_hyp_cpu_entry)
154 mov x1, #1 // is_cpu_on = true
156 SYM_CODE_END(kvm_hyp_cpu_entry)
159 * PSCI CPU_SUSPEND / SYSTEM_SUSPEND entry point
161 * x0: struct kvm_nvhe_init_params PA
163 SYM_CODE_START(kvm_hyp_cpu_resume)
164 mov x1, #0 // is_cpu_on = false
166 SYM_CODE_END(kvm_hyp_cpu_resume)
169 * Common code for CPU entry points. Initializes EL2 state and
170 * installs the hypervisor before handing over to a C handler.
172 * x0: struct kvm_nvhe_init_params PA
175 SYM_CODE_START_LOCAL(__kvm_hyp_init_cpu)
176 mov x28, x0 // Stash arguments
179 /* Check that the core was booted in EL2. */
181 cmp x0, #CurrentEL_EL2
184 /* The core booted in EL1. KVM cannot be initialized on it. */
189 2: msr SPsel, #1 // We want to use SP_EL{1,2}
191 /* Initialize EL2 CPU state to sane values. */
192 init_el2_state nvhe // Clobbers x0..x2
194 /* Enable MMU, set vectors and stack. */
196 bl ___kvm_hyp_init // Clobbers x0..x3
200 ldr x1, =kvm_host_psci_cpu_entry
203 SYM_CODE_END(__kvm_hyp_init_cpu)
205 SYM_CODE_START(__kvm_handle_stub_hvc)
206 cmp x0, #HVC_SOFT_RESTART
209 /* This is where we're about to jump, staying at EL2 */
211 mov x0, #(PSR_F_BIT | PSR_I_BIT | PSR_A_BIT | PSR_D_BIT | PSR_MODE_EL2h)
214 /* Shuffle the arguments, and don't come back */
220 1: cmp x0, #HVC_RESET_VECTORS
224 * Set the HVC_RESET_VECTORS return code before entering the common
225 * path so that we do not clobber x0-x2 in case we are coming via
230 /* Reset kvm back to the hyp stub. */
232 mov_q x6, SCTLR_ELx_FLAGS
233 bic x5, x5, x6 // Clear SCTL_M and etc
234 pre_disable_mmu_workaround
238 alternative_if ARM64_KVM_PROTECTED_MODE
239 mov_q x5, HCR_HOST_NVHE_FLAGS
241 alternative_else_nop_endif
243 /* Install stub vectors */
244 adr_l x5, __hyp_stub_vectors
248 1: /* Bad stub call */
249 mov_q x0, HVC_STUB_ERR
252 SYM_CODE_END(__kvm_handle_stub_hvc)