Skip to content

Commit 6e5714b

Browse files
Ahmed Genidigregkh
authored andcommitted
KVM: arm64: Initialize SCTLR_EL1 in __kvm_hyp_init_cpu()
[ Upstream commit 3855a7b ] When KVM is in protected mode, host calls to PSCI are proxied via EL2, and cold entries from CPU_ON, CPU_SUSPEND, and SYSTEM_SUSPEND bounce through __kvm_hyp_init_cpu() at EL2 before entering the host kernel's entry point at EL1. While __kvm_hyp_init_cpu() initializes SPSR_EL2 for the exception return to EL1, it does not initialize SCTLR_EL1. Due to this, it's possible to enter EL1 with SCTLR_EL1 in an UNKNOWN state. In practice this has been seen to result in kernel crashes after CPU_ON as a result of SCTLR_EL1.M being 1 in violation of the initial core configuration specified by PSCI. Fix this by initializing SCTLR_EL1 for cold entry to the host kernel. As it's necessary to write to SCTLR_EL12 in VHE mode, this initialization is moved into __kvm_host_psci_cpu_entry() where we can use write_sysreg_el1(). The remnants of the '__init_el2_nvhe_prepare_eret' macro are folded into its only caller, as this is clearer than having the macro. Fixes: cdf3671 ("KVM: arm64: Intercept host's CPU_ON SMCs") Reported-by: Leo Yan <leo.yan@arm.com> Signed-off-by: Ahmed Genidi <ahmed.genidi@arm.com> [ Mark: clarify commit message, handle E2H, move to C, remove macro ] Signed-off-by: Mark Rutland <mark.rutland@arm.com> Cc: Ahmed Genidi <ahmed.genidi@arm.com> Cc: Ben Horgan <ben.horgan@arm.com> Cc: Catalin Marinas <catalin.marinas@arm.com> Cc: Leo Yan <leo.yan@arm.com> Cc: Marc Zyngier <maz@kernel.org> Cc: Oliver Upton <oliver.upton@linux.dev> Cc: Will Deacon <will@kernel.org> Reviewed-by: Leo Yan <leo.yan@arm.com> Link: https://lore.kernel.org/r/20250227180526.1204723-3-mark.rutland@arm.com Signed-off-by: Marc Zyngier <maz@kernel.org> [ Backport: Resolved context conflicts when removing the __init_el2_nvhe_prepare_eret macro and invocation: - arch/arm64/include/asm/el2_setup.h: conflicted because 6.6.y lacks later GCS/MPAM macros (__init_el2_gcs / __init_el2_mpam) surrounding the definition. - arch/arm64/kvm/hyp/nvhe/hyp-init.S: conflicted because __kvm_init_el2_state does not exist in 6.6.y (EL2 state is initialized inline). ] Signed-off-by: Colton Lewis <coltonlewis@google.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent a2c3433 commit 6e5714b

4 files changed

Lines changed: 5 additions & 6 deletions

File tree

arch/arm64/include/asm/el2_setup.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,6 @@
229229
.Lskip_fgt_\@:
230230
.endm
231231

232-
.macro __init_el2_nvhe_prepare_eret
233-
mov x0, #INIT_PSTATE_EL1
234-
msr spsr_el2, x0
235-
.endm
236232

237233
/**
238234
* Initialize EL2 registers to sane values. This should be called early on all

arch/arm64/kernel/head.S

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,8 @@ SYM_INNER_LABEL(init_el2, SYM_L_LOCAL)
601601
msr sctlr_el1, x1
602602
mov x2, xzr
603603
3:
604-
__init_el2_nvhe_prepare_eret
604+
mov x0, #INIT_PSTATE_EL1
605+
msr spsr_el2, x0
605606

606607
mov w0, #BOOT_CPU_MODE_EL2
607608
orr x0, x0, x2

arch/arm64/kvm/hyp/nvhe/hyp-init.S

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,6 @@ SYM_CODE_START_LOCAL(__kvm_hyp_init_cpu)
207207
/* Initialize EL2 CPU state to sane values. */
208208
init_el2_state // Clobbers x0..x2
209209
finalise_el2_state
210-
__init_el2_nvhe_prepare_eret
211210

212211
/* Enable MMU, set vectors and stack. */
213212
mov x0, x28

arch/arm64/kvm/hyp/nvhe/psci-relay.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@ asmlinkage void __noreturn __kvm_host_psci_cpu_entry(bool is_cpu_on)
218218
if (is_cpu_on)
219219
release_boot_args(boot_args);
220220

221+
write_sysreg_el1(INIT_SCTLR_EL1_MMU_OFF, SYS_SCTLR);
222+
write_sysreg(INIT_PSTATE_EL1, SPSR_EL2);
223+
221224
__host_enter(host_ctxt);
222225
}
223226

0 commit comments

Comments
 (0)