Skip to contentSkip to conversation

KVM: arm64: Sync SCTLR_EL1 when injecting an exception into a pVM

PATCH In review [email protected] 2 participants 3 messages Jun 12

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: arm64: Sync SCTLR_EL1 when injecting an exception into a pVM

When pKVM injects a synchronous exception into a protected guest, enter_exception64() reads SCTLR_EL1 to set the new PSTATE's PAN and SSBS bits. pKVM refreshes VBAR_EL1 from the live value before injecting but not SCTLR_EL1, which is untrapped for protected guests, so a guest that updates it and then traps takes the exception with stale PAN/SSBS.

Sync SCTLR_EL1 alongside VBAR_EL1.

Fixes 798eb5978700 ("KVM: arm64: Sync protected guest VBAR_EL1 on injecting an undef exception")
---
 arch/arm64/kvm/hyp/nvhe/sys_regs.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
arch/arm64/kvm/hyp/nvhe/sys_regs.c +3 -2
index 8c3fbb413a06..7608cc4030b8 100644
@@ -257,10 +257,11 @@ static void inject_sync64(struct kvm_vcpu *vcpu, u64 esr)
 	*vcpu_cpsr(vcpu) = read_sysreg_el2(SYS_SPSR);
 
 	/*
-	 * Make sure we have the latest update to VBAR_EL1, as pKVM
-	 * handles traps very early, before sysregs are resync'ed
+	 * 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));
 
 	kvm_pend_exception(vcpu, EXCEPT_AA64_EL1_SYNC);
 
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] inject_sync64() fails to write the trapped PSTATE into the guest's hardware SPSR_EL1 register, causing the guest to observe a stale SPSR_EL1 when it handles the injected exception. --

commit 0025a59d79e472c72e387019776838f814e85578
Author: [email protected] <[email protected]>

KVM: arm64: Sync SCTLR_EL1 when injecting an exception into a pVM

This commit updates pKVM exception injection to synchronize SCTLR_EL1 alongside VBAR_EL1 so that the newly injected exception observes the correct PAN and SSBS bits.

Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/sys_regs.c +0 -0
index 8c3fbb413a06d..7608cc4030b88 100644
@@ -257,10 +257,11 @@ static void inject_sync64(struct kvm_vcpu *vcpu, u64 esr)
 	*vcpu_cpsr(vcpu) = read_sysreg_el2(SYS_SPSR);
 
 	/*

arch/arm64/kvm/hyp/nvhe/sys_regs.c static void inject_sync64(struct kvm_vcpu *vcpu, u64 esr) · line 267

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/sys_regs.c +3 -2
-	 * Make sure we have the latest update to VBAR_EL1, as pKVM
-	 * handles traps very early, before sysregs are resync'ed
+	 * 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));
 
 	kvm_pend_exception(vcpu, EXCEPT_AA64_EL1_SYNC);
 

[Severity: High]
This is a pre-existing issue, but looking at the end of inject_sync64(), it appears we fail to write the trapped PSTATE into the guest's hardware SPSR_EL1 register.

Later in inject_sync64(), the code updates the hardware exception registers for EL1:

	write_sysreg_el1(esr, SYS_ESR);
	write_sysreg_el1(read_sysreg_el2(SYS_ELR), SYS_ELR);
	write_sysreg_el2(*vcpu_pc(vcpu), SYS_ELR);
	write_sysreg_el2(*vcpu_cpsr(vcpu), SYS_SPSR);

While SYS_ESR and SYS_ELR are manually updated for EL1, SYS_SPSR (EL1) is omitted. Since pKVM returns control directly to the guest without restoring sys_regs from memory, will the guest's exception handler read a stale SPSR_EL1 from hardware?

Should the code also sync the memory-backed SPSR_EL1 to the hardware register here to prevent corrupted exception state?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author
#2 lore raw

Hello again,

Quoted 48 lines · On Fri, 12 Jun 2026 at 11:40, <[email protected]> wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] inject_sync64() fails to write the trapped PSTATE into the guest's hardware SPSR_EL1 register, causing the guest to observe a stale SPSR_EL1 when it handles the injected exception. --

commit 0025a59d79e472c72e387019776838f814e85578
Author: [email protected] <[email protected]>

KVM: arm64: Sync SCTLR_EL1 when injecting an exception into a pVM

This commit updates pKVM exception injection to synchronize SCTLR_EL1 alongside VBAR_EL1 so that the newly injected exception observes the correct PAN and SSBS bits.

Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/sys_regs.c +0 -0
index 8c3fbb413a06d..7608cc4030b88 100644
@@ -257,10 +257,11 @@ static void inject_sync64(struct kvm_vcpu *vcpu, u64 esr)
      *vcpu_cpsr(vcpu) = read_sysreg_el2(SYS_SPSR);
 
      /*

arch/arm64/kvm/hyp/nvhe/sys_regs.c static void inject_sync64(struct kvm_vcpu *vcpu, u64 esr) · line 267

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/sys_regs.c +3 -2
-      * Make sure we have the latest update to VBAR_EL1, as pKVM
-      * handles traps very early, before sysregs are resync'ed
+      * 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));
 
      kvm_pend_exception(vcpu, EXCEPT_AA64_EL1_SYNC);
 

[Severity: High]
This is a pre-existing issue, but looking at the end of inject_sync64(), it appears we fail to write the trapped PSTATE into the guest's hardware SPSR_EL1 register.

Later in inject_sync64(), the code updates the hardware exception registers for EL1:

        write_sysreg_el1(esr, SYS_ESR);
        write_sysreg_el1(read_sysreg_el2(SYS_ELR), SYS_ELR);
        write_sysreg_el2(*vcpu_pc(vcpu), SYS_ELR);
        write_sysreg_el2(*vcpu_cpsr(vcpu), SYS_SPSR);
Quoted 7 lines

While SYS_ESR and SYS_ELR are manually updated for EL1, SYS_SPSR (EL1) is omitted. Since pKVM returns control directly to the guest without restoring sys_regs from memory, will the guest's exception handler read a stale SPSR_EL1 from hardware?

Should the code also sync the memory-backed SPSR_EL1 to the hardware register here to prevent corrupted exception state?

Good catch, real pre-existing bug. inject_sync64() omits the SPSR_EL1 write, so the guest's handler reads a stale SPSR_EL1 and eret's with the wrong PSTATE. Separate fix sent:

https://lore.kernel.org/all/[email protected]/

Cheers,
/fuad

Quoted 3 lines

--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1