KVM: arm64: Trap RAS error records whenever the host has RAS
Subsystems (3)
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH 1/2 KVM: arm64: Trap RAS error records whenever the host has RAS
From: Minhang Zhang <[email protected]>
From: Minhang Zhang <[email protected]>
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.
--- arch/arm64/kvm/hyp/nvhe/pkvm.c | 8 +++++--- arch/arm64/kvm/sys_regs.c | 14 +++++++++++--- 2 files changed, 16 insertions(+), 6 deletions(-)
arch/arm64/kvm/hyp/nvhe/pkvm.c +5 -3
@@ -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 ;
arch/arm64/kvm/sys_regs.c +11 -3
@@ -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 ;
Signature · 1 line
2.25.1
PATCH 2/2 KVM: arm64: Only clamp MTE_frac when the VM has MTE
In reply to a message not in the archiveFrom: Minhang Zhang <[email protected]>
From: Minhang Zhang <[email protected]>
On a host with ID_AA64PFR1_EL1.MTE == 2 and MTE_frac == 0xf, set_id_aa64pfr1_el1() rewrites a userspace MTE_frac value of 0 to 0xf. This preserves compatibility with older KVM versions that hid MTE_frac, without advertising asynchronous tag check faults that the host does not support.
The rewrite also applies to VMs without MTE. For these VMs, sanitise_id_aa64pfr1_el1() clears both MTE and MTE_frac, so userspace reads MTE_frac == 0. Writing that value back changes it to 0xf, which does not match the sanitised value of this non-writable field.
When restoring the register to a VM whose ID registers have not yet been finalised, this causes arm64_check_features() to return -E2BIG, which set_id_reg() translates to -EINVAL for KVM_SET_ONE_REG. This can prevent userspace from restoring a VM even though MTE is disabled.
Checking the VM's MTE capability alone is insufficient: nested virtualisation also hides MTE and MTE_frac, even when that capability is enabled.
Use the MTE field from KVM's sanitised ID register, which includes both the VM's MTE capability and the nested virtualisation restrictions. Only apply the compatibility rewrite when that field reports MTE2, leaving MTE_frac at zero for VMs where KVM hides MTE.
--- arch/arm64/kvm/sys_regs.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/kvm/sys_regs.c +2 -1
@@ -2364,8 +2364,9 @@ static int set_id_aa64pfr1_el1(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, u64 user_val) {u64 hw_val =read_sanitised_ftr_reg (SYS_ID_AA64PFR1_EL1 ); +u64 vm_val =kvm_read_sanitised_id_reg (vcpu, rd);u64 mpam_mask = ID_AA64PFR1_EL1_MPAM_frac_MASK; -u8 mte =SYS_FIELD_GET (ID_AA64PFR1_EL1 ,MTE , hw_val); +u8 mte =SYS_FIELD_GET (ID_AA64PFR1_EL1 ,MTE , vm_val);u8 user_mte_frac =SYS_FIELD_GET (ID_AA64PFR1_EL1 , MTE_frac, user_val);u8 hw_mte_frac =SYS_FIELD_GET (ID_AA64PFR1_EL1 , MTE_frac, hw_val);
Signature · 1 line
2.25.1