[PATCH 1/2] KVM: arm64: Trap RAS error records whenever the host has RAS

Minhang Zhang sesame_h at qq.com
Fri Dec 4 01:12:24 PST 2026


From: Minhang Zhang <zhangminhang at kylinos.cn>

From: Minhang Zhang <zhangminhang at kylinos.cn>

KVM uses HCR_EL2.TERR to trap guest accesses to the RAS error record
registers and emulate them as RAZ/WI or UNDEF, according to the features
exposed to the guest. However, vcpu_set_hcr() and pkvm_vcpu_reset_hcr()
enable the trap only when cpus_have_final_cap(ARM64_HAS_RAS_EXTN) is true.

That capability is only registered when CONFIG_ARM64_RAS_EXTN is enabled.
On a RAS-capable host built without this option, accesses can reach the
physical error record registers instead of KVM's emulation.

Commit 1fab657cb2a0 ("KVM: arm64: Make ID_AA64PFR0_EL1.RAS writable")
also allows userspace to hide RAS from the guest. Without TERR, accesses
by such a guest are not trapped for access_ras() to inject an UNDEF.

Set HCR_EL2.TERR based on the RAS field in the host's sanitised
ID_AA64PFR0_EL1, independently of CONFIG_ARM64_RAS_EXTN. Do this in both
the host and hyp initialisation paths: in protected mode, hyp rebuilds
HCR_EL2 and does not import TERR from the host. Use hyp's existing copy
of the sanitised ID register for the latter.

Leave the capability check for HCR_EL2.TEA unchanged, as this change only
concerns accesses to the error record registers.

Fixes: 558daf693e0c ("KVM: arm64: Emulate RAS error registers and set HCR_EL2's TERR & TEA")
Signed-off-by: Minhang Zhang <zhangminhang at kylinos.cn>
Cc: stable at vger.kernel.org
---
 arch/arm64/kvm/hyp/nvhe/pkvm.c |  8 +++++---
 arch/arm64/kvm/sys_regs.c      | 14 +++++++++++---
 2 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/arch/arm64/kvm/hyp/nvhe/pkvm.c b/arch/arm64/kvm/hyp/nvhe/pkvm.c
index 6442200cd40b..3ed8716d9257 100644
--- a/arch/arm64/kvm/hyp/nvhe/pkvm.c
+++ b/arch/arm64/kvm/hyp/nvhe/pkvm.c
@@ -52,12 +52,14 @@ static void pkvm_vcpu_reset_hcr(struct kvm_vcpu *vcpu)
 	if (has_hvhe())
 		vcpu->arch.hcr_el2 |= HCR_EL2_E2H;
 
-	if (cpus_have_final_cap(ARM64_HAS_RAS_EXTN)) {
+	if (cpus_have_final_cap(ARM64_HAS_RAS_EXTN))
 		/* route synchronous external abort exceptions to EL2 */
 		vcpu->arch.hcr_el2 |= HCR_EL2_TEA;
-		/* trap error record accesses */
+
+	/* Trap error record accesses whenever the host implements RAS. */
+	if (SYS_FIELD_GET(ID_AA64PFR0_EL1, RAS,
+			  id_aa64pfr0_el1_sys_val) != ID_AA64PFR0_EL1_RAS_NI)
 		vcpu->arch.hcr_el2 |= HCR_EL2_TERR;
-	}
 
 	if (cpus_have_final_cap(ARM64_HAS_STAGE2_FWB))
 		vcpu->arch.hcr_el2 |= HCR_EL2_FWB;
diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c
index 5555e17a7710..3eb872c11428 100644
--- a/arch/arm64/kvm/sys_regs.c
+++ b/arch/arm64/kvm/sys_regs.c
@@ -5831,12 +5831,20 @@ static void vcpu_set_hcr(struct kvm_vcpu *vcpu)
 
 	if (has_vhe() || has_hvhe())
 		vcpu->arch.hcr_el2 |= HCR_EL2_E2H;
-	if (cpus_have_final_cap(ARM64_HAS_RAS_EXTN)) {
+	if (cpus_have_final_cap(ARM64_HAS_RAS_EXTN))
 		/* route synchronous external abort exceptions to EL2 */
 		vcpu->arch.hcr_el2 |= HCR_EL2_TEA;
-		/* trap error record accesses */
+
+	/*
+	 * Trap error record accesses whenever the host implements RAS. This is
+	 * a property of the hardware, not of the kernel configuration, and the
+	 * trap is what allows access_ras() to hide the registers from a guest
+	 * that has been given ID_AA64PFR0_EL1.RAS == NI.
+	 */
+	if (SYS_FIELD_GET(ID_AA64PFR0_EL1, RAS,
+			  read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1)) !=
+	    ID_AA64PFR0_EL1_RAS_NI)
 		vcpu->arch.hcr_el2 |= HCR_EL2_TERR;
-	}
 
 	if (cpus_have_final_cap(ARM64_HAS_STAGE2_FWB))
 		vcpu->arch.hcr_el2 |= HCR_EL2_FWB;
-- 
2.25.1




More information about the linux-arm-kernel mailing list