Skip to contentSkip to conversation

arm64/sme: Fix SMPRI_EL1 traps for KVM guests

PATCH Superseded Mark Brown 4 participants 7 messages Oct 27, 2022 – Oct 31, 2022

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Mark Brown

author
#0 lore raw

cover arm64/sme: Fix SMPRI_EL1 traps for KVM guests

When adding support for SME I missed management of traps for SMPRI_EL1 in KVM guests. This series adds that management, and also updates the documentation to help avoid such issues in future.

This was noticed due to Vincent Donnefort noticing that qemu has implemented support for SME virtualisation but not fine grained traps which was causing problems for qemu in nVHE mode since that already used fine grained traps with SME, the FGT registers are not implemented and UNDEF. As a result of this change VHE will also be affected so a separate series "KVM: arm: Refuse to enable KVM on systems with SME but not FGT" has been sent which will ensure we don't run on such systems.

Mark Brown (2):
arm64: booting: Document our requirements for fine grained traps with
SME
KVM: arm64: Trap access to SMPRI_EL1 in VHE mode

 Documentation/arm64/booting.rst |  8 ++++++++
 arch/arm64/kvm/hyp/vhe/switch.c | 24 ++++++++++++++++++++++--
 2 files changed, 30 insertions(+), 2 deletions(-)

base-commit: 9abf2313adc1ca1b6180c508c25f22f9395cc780

Signature · 1 line
2.30.2
author
#1 lore raw

PATCH 1/2 arm64: booting: Document our requirements for fine grained traps with SME

With SME we require that fine grained traps on access to TPIDR2_EL0 and SMPRI_EL1 are disabled but did not document that fact. Add the relevant register bits.

---
 Documentation/arm64/booting.rst | 8 ++++++++
 1 file changed, 8 insertions(+)
Documentation/arm64/booting.rst +8 -0
index 8aefa1001ae5..8c324ad638de 100644
@@ -340,6 +340,14 @@ Before jumping into the kernel, the following conditions must be met:
     - SMCR_EL2.LEN must be initialised to the same value for all CPUs the
       kernel will execute on.
 
+    - HWFGRTR_EL2.nTPIDR2_EL0 (bit 55) must be initialised to 0b01.
+
+    - HWFGWTR_EL2.nTPIDR2_EL0 (bit 55) must be initialised to 0b01.
+
+    - HWFGRTR_EL2.nSMPRI_EL1 (bit 54) must be initialised to 0b01.
+
+    - HWFGWTR_EL2.nSMPRI_EL1 (bit 54) must be initialised to 0b01.
+
   For CPUs with the Scalable Matrix Extension FA64 feature (FEAT_SME_FA64)
 
   - If EL3 is present:
Signature · 1 line
2.30.2
author
#3 lore raw

PATCH 2/2 KVM: arm64: Trap access to SMPRI_EL1 in VHE mode

In reply to Mark Brown

On systems with SME access to the SMPRI_EL1 priority management register is controlled by the nSMPRI_EL1 fine grained trap. We manage this trap in nVHE mode but do not do so when in VHE mode, add the required management.

On systems which do not implement priority mapping not enabling this trap will allow the guest to discover if the host support SME since the register will be RES0 rather than UNDEF. On systems implementing priority mapping the register could be used as a side channel by guests.

Fixes 861262ab8627 ("KVM: arm64: Handle SME host state when running guests")
---
 arch/arm64/kvm/hyp/vhe/switch.c | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)
arch/arm64/kvm/hyp/vhe/switch.c +22 -2
index 7acb87eaa092..cae581e8dd56 100644
@@ -63,10 +63,20 @@ static void __activate_traps(struct kvm_vcpu *vcpu)
 		__activate_traps_fpsimd32(vcpu);
 	}
 
-	if (cpus_have_final_cap(ARM64_SME))
+	if (cpus_have_final_cap(ARM64_SME)) {
 		write_sysreg(read_sysreg(sctlr_el2) & ~SCTLR_ELx_ENTP2,
 			     sctlr_el2);
 
+		/*
+		 * Disable access to SMPRI_EL1 - we don't need to control
+		 * nTPIDR2_EL0 in VHE mode.
+		 */
+		sysreg_clear_set_s(SYS_HFGRTR_EL2, HFGxTR_EL2_nSMPRI_EL1_MASK,
+				   0);
+		sysreg_clear_set_s(SYS_HFGWTR_EL2, HFGxTR_EL2_nSMPRI_EL1_MASK,
+				   0);
+	}
+
 	write_sysreg(val, cpacr_el1);
 
 	write_sysreg(__this_cpu_read(kvm_hyp_vector), vbar_el1);
@@ -88,9 +98,19 @@ static void __deactivate_traps(struct kvm_vcpu *vcpu)
 	 */
 	asm(ALTERNATIVE("nop", "isb", ARM64_WORKAROUND_SPECULATIVE_AT));
 
-	if (cpus_have_final_cap(ARM64_SME))
+	if (cpus_have_final_cap(ARM64_SME)) {
+		/*
+		 * Enable access to SMPRI_EL1 - we don't need to
+		 * control nTPIDR2_EL0 in VHE mode.
+		 */
+		sysreg_clear_set_s(SYS_HFGRTR_EL2, 0,
+				   HFGxTR_EL2_nSMPRI_EL1_MASK);
+		sysreg_clear_set_s(SYS_HFGWTR_EL2, 0,
+				   HFGxTR_EL2_nSMPRI_EL1_MASK);
+
 		write_sysreg(read_sysreg(sctlr_el2) | SCTLR_ELx_ENTP2,
 			     sctlr_el2);
+	}
 
 	write_sysreg(CPACR_EL1_DEFAULT, cpacr_el1);
 
Signature · 1 line
2.30.2
maintainer
#4 lore raw

Re: [PATCH v1 2/2] KVM: arm64: Trap access to SMPRI_EL1 in VHE mode

Quoted 23 lines · Mark Brown

On systems with SME access to the SMPRI_EL1 priority management register is controlled by the nSMPRI_EL1 fine grained trap. We manage this trap in nVHE mode but do not do so when in VHE mode, add the required management.

On systems which do not implement priority mapping not enabling this trap will allow the guest to discover if the host support SME since the register will be RES0 rather than UNDEF. On systems implementing priority mapping the register could be used as a side channel by guests.

Fixes 861262ab8627 ("KVM: arm64: Handle SME host state when running guests")
---
 arch/arm64/kvm/hyp/vhe/switch.c | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)
arch/arm64/kvm/hyp/vhe/switch.c +0 -0
index 7acb87eaa092..cae581e8dd56 100644
@@ -63,10 +63,20 @@ static void __activate_traps(struct kvm_vcpu *vcpu)
 		__activate_traps_fpsimd32(vcpu);

arch/arm64/kvm/hyp/vhe/switch.c static void __activate_traps(struct kvm_vcpu *vcpu) · line 72

Quoted 10 lines
arch/arm64/kvm/hyp/vhe/switch.c +4 -1
 	}
 
-	if (cpus_have_final_cap(ARM64_SME))
+	if (cpus_have_final_cap(ARM64_SME)) {
 		write_sysreg(read_sysreg(sctlr_el2) & ~SCTLR_ELx_ENTP2,
 			     sctlr_el2);
 
+		/*
+		 * Disable access to SMPRI_EL1 - we don't need to control
+		 * nTPIDR2_EL0 in VHE mode.

It really isn't obvious to me why this is the case. The pseudocode says for a 'MSR TPIDR2_EL0, <Xt>' (DDI0616 A.a p225):

<quote>
elsif PSTATE.EL == EL1 then

	if Halted() && HaveEL(EL3) && EDSCR.SDD == '1' &&
	   boolean IMPLEMENTATION_DEFINED "EL3 trap priority, when SDD == '1'" &&
	   SCR_EL3.EnTP2 == '0' then
		UNDEFINED;
	elsif EL2Enabled() && (!HaveEL(EL3) || SCR_EL3.FGTEn == '1') &&
	   HFGWTR_EL2.nTPIDR2_EL0 == '0' then
		AArch64.SystemAccessTrap(EL2, 0x18);
	elsif HaveEL(EL3) && SCR_EL3.EnTP2 == '0' then
		if Halted() && EDSCR.SDD == '1' then
			UNDEFINED;
		else
			AArch64.SystemAccessTrap(EL3, 0x18);
	else
		TPIDR2_EL0 = X[t, 64];

</quote>

So when running at EL1, and short of clearing nTPIDR2_EL0, EL1 will have access to TPIDR2_EL0. What prevents that?

The write to SCTLR_EL2.EnTP2 is also pretty dubious, and doesn't really cover the access to EL0 (think SCTLR_EL1.EnTP2=1 and HCR_EL2.{E2H,TGE}={1,0}, for example).

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
author
#5 lore raw

Re: [PATCH v1 2/2] KVM: arm64: Trap access to SMPRI_EL1 in VHE mode

Quoted 8 lines · Marc Zyngier

arch/arm64/kvm/hyp/vhe/switch.c static void __activate_traps(struct kvm_vcpu *vcpu) · line 72

Quoted 3 lines · Mark Brown
arch/arm64/kvm/hyp/vhe/switch.c +3 -0
+		/*
+		 * Disable access to SMPRI_EL1 - we don't need to control
+		 * nTPIDR2_EL0 in VHE mode.

It really isn't obvious to me why this is the case. The pseudocode says for a 'MSR TPIDR2_EL0, <Xt>' (DDI0616 A.a p225):

Yes, I was just discovering that while checking and replying to your earlier mail about the other series. I'll respin.

Reviewed-by on the v1 series
#6 lore raw
In reply to Mark Brown
Quoted 12 lines · Mark Brown

When adding support for SME I missed management of traps for SMPRI_EL1 in KVM guests. This series adds that management, and also updates the documentation to help avoid such issues in future.

This was noticed due to Vincent Donnefort noticing that qemu has implemented support for SME virtualisation but not fine grained traps which was causing problems for qemu in nVHE mode since that already used fine grained traps with SME, the FGT registers are not implemented and UNDEF. As a result of this change VHE will also be affected so a separate series "KVM: arm: Refuse to enable KVM on systems with SME but not FGT" has been sent which will ensure we don't run on such systems.

Quoted 4 lines

Mark Brown (2):
arm64: booting: Document our requirements for fine grained traps with
SME
KVM: arm64: Trap access to SMPRI_EL1 in VHE mode

For the series:

--
Thanks,
Oliver