KVM: arm64: Fixes to handling of restricted registers for protected VMs
v2 cover KVM: arm64: Fixes to handling of restricted registers for protected VMs
This patch series mainly fixes issues we've encountered in pKVM (in downstream Android code), related to the handling of protected VM access to restricted registers and injecting undefined exceptions into a protected guest.
The last patch in this series isn't pKVM specific, but a fix to the vgic-v2 code encountered while fixing the issues in this series. The issue it fixes was indirectly introduced into the code with hVHE.
Changes from v1 [1]:
- Dropped the refactoring patch.
- Added Reported-by tags.
Based on Linux 6.16.
Cheers,
/fuad
[1] https://lore.kernel.org/all/[email protected]/
Fuad Tabba (3):
KVM: arm64: Handle AIDR_EL1 and REVIDR_EL1 in host for protected VMs
KVM: arm64: Sync protected guest VBAR_EL1 on injecting an undef
exception
arm64: vgic-v2: Fix guest endianness check in hVHE mode
arch/arm64/kvm/hyp/nvhe/sys_regs.c | 5 +++++ arch/arm64/kvm/hyp/vgic-v2-cpuif-proxy.c | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-)
base-commit: 038d61fd642278bab63ee8ef722c50d10ab01e8f
Signature · 1 line
2.50.1.565.gc32cd1483b-goog
PATCH v2 1/3 KVM: arm64: Handle AIDR_EL1 and REVIDR_EL1 in host for protected VMs
Since commit 17efc1acee62 ("arm64: Expose AIDR_EL1 via sysfs"), AIDR_EL1 is read early during boot. Therefore, a guest running as a protected VM will fail to boot because when it attempts to access AIDR_EL1, access to that register is restricted in pKVM for protected guests.
Similar to how MIDR_EL1 is handled by the host for protected VMs, let the host handle accesses to AIDR_EL1 as well as REVIDR_EL1. However note that, unlike MIDR_EL1, AIDR_EL1 and REVIDR_EL1 are trapped by HCR_EL2.TID1. Therefore, explicitly mark them as handled by the host for protected VMs. TID1 is always set in pKVM, because it needs to restrict access to SMIDR_EL1, which is also trapped by that bit.
--- arch/arm64/kvm/hyp/nvhe/sys_regs.c | 4 ++++ 1 file changed, 4 insertions(+)
arch/arm64/kvm/hyp/nvhe/sys_regs.c +4 -0
@@ -372,6 +372,9 @@ static const struct sys_reg_desc pvm_sys_reg_descs[] = {/* Debug and Trace Registers are restricted. */ +/* Group 1 ID registers */ +HOST_HANDLED (SYS_REVIDR_EL1 ), +/* AArch64 mappings of the AArch32 ID registers */ /* CRm=1 */ AARCH32(SYS_ID_PFR0_EL1),
@@ -460,6 +463,7 @@ static const struct sys_reg_desc pvm_sys_reg_descs[] = {HOST_HANDLED (SYS_CCSIDR_EL1 ),HOST_HANDLED (SYS_CLIDR_EL1 ), +HOST_HANDLED (SYS_AIDR_EL1 ),HOST_HANDLED (SYS_CSSELR_EL1 ), HOST_HANDLED(SYS_CTR_EL0),
Signature · 1 line
2.50.1.565.gc32cd1483b-goog
PATCH v2 2/3 KVM: arm64: Sync protected guest VBAR_EL1 on injecting an undef exception
In reply to Fuad TabbaIn pKVM, a race condition can occur if a guest updates its VBAR_EL1 register and, before a vCPU exit synchronizes this change, the hypervisor needs to inject an undefined exception into a protected guest.
In this scenario, the vCPU still holds the stale VBAR_EL1 value from before the guest's update. When pKVM injects the exception, it ends up using the stale value.
Explicitly read the live value of VBAR_EL1 from the guest and update the vCPU value immediately before pending the exception. This ensures the vCPU's value is the same as the guest's and that the exception will be handled at the correct address upon resuming the guest.
--- arch/arm64/kvm/hyp/nvhe/sys_regs.c | 1 + 1 file changed, 1 insertion(+)
arch/arm64/kvm/hyp/nvhe/sys_regs.c +1 -0
@@ -253,6 +253,7 @@ static void inject_undef64(struct kvm_vcpu *vcpu) *vcpu_pc (vcpu) =read_sysreg_el2 (SYS_ELR ); *vcpu_cpsr (vcpu) =read_sysreg_el2 (SYS_SPSR ); +__vcpu_assign_sys_reg (vcpu,read_sysreg_el1 (SYS_VBAR ),VBAR_EL1 );kvm_pend_exception (vcpu,EXCEPT_AA64_EL1_SYNC );
Signature · 1 line
2.50.1.565.gc32cd1483b-goog
PATCH v2 3/3 arm64: vgic-v2: Fix guest endianness check in hVHE mode
In reply to Fuad TabbaIn hVHE when running at the hypervisor, SCTLR_EL1 refers to the hypervisor's System Control Register rather than the guest's. Make sure to access the guest's register to determine its endianness.
--- arch/arm64/kvm/hyp/vgic-v2-cpuif-proxy.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kvm/hyp/vgic-v2-cpuif-proxy.c +1 -1
@@ -20,7 +20,7 @@ static bool __is_be(struct kvm_vcpu *vcpu)if (vcpu_mode_is_32bit (vcpu))return !!(read_sysreg_el2 (SYS_SPSR ) &PSR_AA32_E_BIT ); -return !!(read_sysreg (SCTLR_EL1 ) & SCTLR_ELx_EE); +return !!(read_sysreg_el1 (SYS_SCTLR ) & SCTLR_ELx_EE); }/*
Signature · 1 line
2.50.1.565.gc32cd1483b-goog
Oliver Upton
AppliedQuoted 10 lines · Fuad Tabba
This patch series mainly fixes issues we've encountered in pKVM (in downstream Android code), related to the handling of protected VM access to restricted registers and injecting undefined exceptions into a protected guest.
The last patch in this series isn't pKVM specific, but a fix to the vgic-v2 code encountered while fixing the issues in this series. The issue it fixes was indirectly introduced into the code with hVHE.
[...]
Applied to fixes, thanks!
[1/3] KVM: arm64: Handle AIDR_EL1 and REVIDR_EL1 in host for protected VMs
https://git.kernel.org/kvmarm/kvmarm/c/eaa43934b412 [2/3] KVM: arm64: Sync protected guest VBAR_EL1 on injecting an undef exception https://git.kernel.org/kvmarm/kvmarm/c/798eb5978700 [3/3] arm64: vgic-v2: Fix guest endianness check in hVHE mode https://git.kernel.org/kvmarm/kvmarm/c/f1edb159200a
--
Best,
Oliver