* [PATCH v2] KVM: arm64: Sync a pVM's control state before CPU_ON reads its endianness
@ 2026-11-12 7:59 Fuad Tabba
0 siblings, 0 replies; only message in thread
From: Fuad Tabba @ 2026-11-12 7:59 UTC (permalink / raw)
To: Marc Zyngier, Oliver Upton
Cc: kvmarm, linux-arm-kernel, linux-kernel, Joey Gouly, Steffen Eiden,
Suzuki K Poulose, Zenghui Yu, Catalin Marinas, Will Deacon,
Mark Rutland, Vincent Donnefort, Fuad Tabba
pKVM handles a protected guest's PSCI CPU_ON at EL2 before the guest's
EL1 sysregs are saved, so kvm_vcpu_is_be() reads a stale SCTLR_EL1. A
guest that changes SCTLR_EL1.EE and calls CPU_ON before its next exit
to the host starts the target vCPU with the old endianness.
Have CPU_ON sync the caller's PC, PSTATE, VBAR_EL1 and SCTLR_EL1 before
reading its endianness, through a helper factored out of
inject_sync64(), so that both handlers sync the same state.
Fixes: 9d4ec80be2eb4 ("KVM: arm64: Handle PSCI calls for protected VMs at EL2")
Suggested-by: Marc Zyngier <maz@kernel.org>
Signed-off-by: Fuad Tabba <fuad.tabba@linux.dev>
---
Notes:
Changes in v2:
- Factor inject_sync64()'s sync into a helper and call it from CPU_ON,
as Marc suggested. Named pkvm_sync_vcpu_ctrl_state() rather than
..._before_exception(), since CPU_ON injects no exception.
- Subject and message reworded to match.
v1: https://lore.kernel.org/r/20261111082746.1064567-1-fuad.tabba@linux.dev
arch/arm64/kvm/hyp/include/nvhe/pkvm.h | 1 +
arch/arm64/kvm/hyp/nvhe/pkvm.c | 2 ++
arch/arm64/kvm/hyp/nvhe/sys_regs.c | 16 ++++++++++------
3 files changed, 13 insertions(+), 6 deletions(-)
diff --git a/arch/arm64/kvm/hyp/include/nvhe/pkvm.h b/arch/arm64/kvm/hyp/include/nvhe/pkvm.h
index 0261b2bc681f7..5c68c9184bda6 100644
--- a/arch/arm64/kvm/hyp/include/nvhe/pkvm.h
+++ b/arch/arm64/kvm/hyp/include/nvhe/pkvm.h
@@ -133,6 +133,7 @@ bool kvm_handle_pvm_restricted(struct kvm_vcpu *vcpu, u64 *exit_code);
void kvm_init_pvm_id_regs(struct kvm_vcpu *vcpu);
void kvm_reset_pvm_sys_regs(struct kvm_vcpu *vcpu);
int kvm_check_pvm_sysreg_table(void);
+void pkvm_sync_vcpu_ctrl_state(struct kvm_vcpu *vcpu);
int pkvm_reset_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu);
struct pkvm_hyp_vcpu *pkvm_mpidr_to_hyp_vcpu(struct pkvm_hyp_vm *vm, unsigned long mpidr);
diff --git a/arch/arm64/kvm/hyp/nvhe/pkvm.c b/arch/arm64/kvm/hyp/nvhe/pkvm.c
index fc0475f0877d1..eedc1dd81ee56 100644
--- a/arch/arm64/kvm/hyp/nvhe/pkvm.c
+++ b/arch/arm64/kvm/hyp/nvhe/pkvm.c
@@ -1321,6 +1321,8 @@ static bool pvm_psci_vcpu_on(struct pkvm_hyp_vcpu *hyp_vcpu)
goto error;
}
+ pkvm_sync_vcpu_ctrl_state(&hyp_vcpu->vcpu);
+
reset_state = &target->vcpu.arch.reset_state;
WRITE_ONCE(reset_state->pc, smccc_get_arg2(&hyp_vcpu->vcpu));
WRITE_ONCE(reset_state->r0, smccc_get_arg3(&hyp_vcpu->vcpu));
diff --git a/arch/arm64/kvm/hyp/nvhe/sys_regs.c b/arch/arm64/kvm/hyp/nvhe/sys_regs.c
index 32e82653ebf7e..be785eec2ec96 100644
--- a/arch/arm64/kvm/hyp/nvhe/sys_regs.c
+++ b/arch/arm64/kvm/hyp/nvhe/sys_regs.c
@@ -254,17 +254,21 @@ static u64 pvm_calc_id_reg(const struct kvm_vcpu *vcpu, u32 id)
}
}
-static void inject_sync64(struct kvm_vcpu *vcpu, u64 esr)
+/*
+ * pKVM handles some exits at EL2 before it saves the guest's EL1 state, so
+ * sync the copies that exception injection and PSCI CPU_ON read.
+ */
+void pkvm_sync_vcpu_ctrl_state(struct kvm_vcpu *vcpu)
{
*vcpu_pc(vcpu) = read_sysreg_el2(SYS_ELR);
*vcpu_cpsr(vcpu) = read_sysreg_el2(SYS_SPSR);
-
- /*
- * Sync VBAR_EL1 and SCTLR_EL1, both read by enter_exception64(),
- * as pKVM handles traps before sysregs are resync'ed.
- */
__vcpu_assign_sys_reg(vcpu, VBAR_EL1, read_sysreg_el1(SYS_VBAR));
__vcpu_assign_sys_reg(vcpu, SCTLR_EL1, read_sysreg_el1(SYS_SCTLR));
+}
+
+static void inject_sync64(struct kvm_vcpu *vcpu, u64 esr)
+{
+ pkvm_sync_vcpu_ctrl_state(vcpu);
kvm_pend_exception(vcpu, EXCEPT_AA64_EL1_SYNC);
base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb
--
2.47.3
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2026-11-12 7:59 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-12 7:59 [PATCH v2] KVM: arm64: Sync a pVM's control state before CPU_ON reads its endianness Fuad Tabba
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox