With nested virtualization, when we inject an SError that the guest has masked, we set the VSE bit in ->hcr_el2 and the CPU delivers it once the guest unmasks SErrors. The problem is that the bit stays set when we switch between L1 and L2, so an SError meant for L2 can be delivered to L1, and the other way around. When we switch, clear the VSE bit and set NESTED_SERROR_PENDING instead. This matches what kvm_nested_sync_hwstate() does after every guest exit. Then kvm_nested_flush_hwstate() injects the SError again and decides where it should go in the new context. Reported-by: Sashiko Closes: https://lore.kernel.org/all/sashiko-outbox-229065@kernel.org/ Fixes: 77ee70a07357 ("KVM: arm64: nv: Honor SError exception routing / masking") Cc: stable@vger.kernel.org Signed-off-by: Dan Carpenter --- v2: new patch --- arch/arm64/kvm/emulate-nested.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/arch/arm64/kvm/emulate-nested.c b/arch/arm64/kvm/emulate-nested.c index 39136a89d74b..cccaf02610e8 100644 --- a/arch/arm64/kvm/emulate-nested.c +++ b/arch/arm64/kvm/emulate-nested.c @@ -2792,6 +2792,18 @@ static u64 kvm_check_illegal_exception_return(struct kvm_vcpu *vcpu, u64 spsr) return spsr; } +/* + * HCR_EL2.VSE was set for the context we are leaving. Store the SError + * instead, so that kvm_nested_flush_hwstate() routes it in the new context. + */ +static void kvm_nested_stash_serror(struct kvm_vcpu *vcpu) +{ + if (*vcpu_hcr(vcpu) & HCR_EL2_VSE) { + *vcpu_hcr(vcpu) &= ~HCR_EL2_VSE; + vcpu_set_flag(vcpu, NESTED_SERROR_PENDING); + } +} + void kvm_emulate_nested_eret(struct kvm_vcpu *vcpu) { u64 spsr, elr, esr; @@ -2823,6 +2835,7 @@ void kvm_emulate_nested_eret(struct kvm_vcpu *vcpu) preempt_disable(); vcpu_set_flag(vcpu, IN_NESTED_ERET); kvm_arch_vcpu_put(vcpu); + kvm_nested_stash_serror(vcpu); if (!esr_iss_is_eretax(esr)) elr = __vcpu_sys_reg(vcpu, ELR_EL2); @@ -2909,6 +2922,7 @@ static int kvm_inject_nested(struct kvm_vcpu *vcpu, u64 esr_el2, __kvm_adjust_pc(vcpu); kvm_arch_vcpu_put(vcpu); + kvm_nested_stash_serror(vcpu); kvm_inject_el2_exception(vcpu, esr_el2, type); -- 2.53.0