From 0405a25a6ed73284bee66e85ac8d3d2aac512568 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre-Cl=C3=A9ment=20Tosi?= Date: Tue, 14 Jan 2025 19:49:04 +0000 Subject: [PATCH] ANDROID: KVM: arm64: Fix corrupted PSTATE during guest debug MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When running non-protected guests for which the host has enabled hardware-assisted debugging, pKVM "flushes" PSTATE and MDSCR_EL1 from the host-controlled vCPU struct to its internal struct before actually running the vCPU. However, on guest exit, it was failing to record ("sync") any change that the guest might have made to those registers. As a result, the following vCPU flush discards those changes and the following vCPU run happens with potentially corrupted registers. This is particularly noticeable during Linux boot when guest updates to PSTATE.{I,BTYPE} are lost. Instead, sync PSTATE and MDSCR if they were flushed before the vCPU run. Bug: 389970357 Fixes: 0a1f3a1f7b91 ("ANDROID: KVM: arm64: Monitor Debug support for non-protected guests") Change-Id: Idaf68516782dc3cd284f6a4e65f8c89cbad7d273 Signed-off-by: Pierre-Clément Tosi --- arch/arm64/kvm/hyp/nvhe/hyp-main.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/arch/arm64/kvm/hyp/nvhe/hyp-main.c b/arch/arm64/kvm/hyp/nvhe/hyp-main.c index bcf30db7a6d4..931152dc3aa8 100644 --- a/arch/arm64/kvm/hyp/nvhe/hyp-main.c +++ b/arch/arm64/kvm/hyp/nvhe/hyp-main.c @@ -567,6 +567,10 @@ static void sync_debug_state(struct pkvm_hyp_vcpu *hyp_vcpu) return; __vcpu_restore_guest_debug_regs(vcpu); + vcpu_write_sys_reg(host_vcpu, vcpu_read_sys_reg(vcpu, MDSCR_EL1), + MDSCR_EL1); + *vcpu_cpsr(host_vcpu) = *vcpu_cpsr(vcpu); + vcpu->arch.debug_ptr = &host_vcpu->arch.vcpu_debug_state; }