Skip to contentSkip to conversation

KVM: arm64: Advertise ID_AA64PFR0_EL1.CSV2/3 to protected VMs

PATCH Superseded Fuad Tabba 2 participants 3 messages Mar 29, 2023 – Mar 30, 2023

Subsystems (2)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Fuad Tabba

author
#0 lore raw

PATCH 1/1 KVM: arm64: Advertise ID_AA64PFR0_EL1.CSV2/3 to protected VMs

The existing pKVM code attempts to do that using a value that's initialized to 0 but never set. To advertise csv2/3 to a protected guest, store them at the hypervisor and use them for setting csv2/3.

Similar to non-protected KVM, these are tracked as a system-wide variable, rather than per cpu, for simplicity.

Fixes 6c30bfb18d0b ("KVM: arm64: Add handlers for protected VM System Registers")
---
 arch/arm64/include/asm/kvm_hyp.h   |  3 +++
 arch/arm64/kvm/arm.c               |  2 ++
 arch/arm64/kvm/hyp/nvhe/sys_regs.c | 15 ++++++++++++---
 3 files changed, 17 insertions(+), 3 deletions(-)
arch/arm64/include/asm/kvm_hyp.h +3 -0
index bdd9cf546d95..723a645af191 100644
@@ -127,4 +127,7 @@ extern u64 kvm_nvhe_sym(id_aa64smfr0_el1_sys_val);
 extern unsigned long kvm_nvhe_sym(__icache_flags);
 extern unsigned int kvm_nvhe_sym(kvm_arm_vmid_bits);
 
+extern bool kvm_nvhe_sym(spectre_unaffected);
+extern bool kvm_nvhe_sym(meltdown_unaffected);
+
 #endif /* __ARM64_KVM_HYP_H__ */
arch/arm64/kvm/arm.c +2 -0
index 3bd732eaf087..364a4440ae54 100644
@@ -1902,6 +1902,8 @@ static void kvm_hyp_init_symbols(void)
 	kvm_nvhe_sym(id_aa64smfr0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64SMFR0_EL1);
 	kvm_nvhe_sym(__icache_flags) = __icache_flags;
 	kvm_nvhe_sym(kvm_arm_vmid_bits) = kvm_arm_vmid_bits;
+	kvm_nvhe_sym(spectre_unaffected) = (arm64_get_spectre_v2_state() == SPECTRE_UNAFFECTED);
+	kvm_nvhe_sym(meltdown_unaffected) = (arm64_get_meltdown_state() == SPECTRE_UNAFFECTED);
 }
 
 static int __init kvm_hyp_init_protection(u32 hyp_va_bits)
arch/arm64/kvm/hyp/nvhe/sys_regs.c +12 -3
index 08d2b004f4b7..3f647a2f4c96 100644
@@ -28,6 +28,16 @@ u64 id_aa64mmfr1_el1_sys_val;
 u64 id_aa64mmfr2_el1_sys_val;
 u64 id_aa64smfr0_el1_sys_val;
 
+/*
+ * Track whether the system isn't affected by spectre/meltown.
+ * Although this is per-CPU, we make it global for simplicity, e.g., not to have
+ * to worry about migration.
+ *
+ * Unlike for non-protected VMs, userspace cannot override this.
+ */
+bool spectre_unaffected;
+bool meltdown_unaffected;
+
 /*
  * Inject an unknown/undefined exception to an AArch64 guest while most of its
  * sysregs are live.
@@ -85,7 +95,6 @@ static u64 get_restricted_features_unsigned(u64 sys_reg_val,
 
 static u64 get_pvm_id_aa64pfr0(const struct kvm_vcpu *vcpu)
 {
-	const struct kvm *kvm = (const struct kvm *)kern_hyp_va(vcpu->kvm);
 	u64 set_mask = 0;
 	u64 allow_mask = PVM_ID_AA64PFR0_ALLOW;
 
@@ -94,9 +103,9 @@ static u64 get_pvm_id_aa64pfr0(const struct kvm_vcpu *vcpu)
 
 	/* Spectre and Meltdown mitigation in KVM */
 	set_mask |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2),
-			       (u64)kvm->arch.pfr0_csv2);
+			       spectre_unaffected);
 	set_mask |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3),
-			       (u64)kvm->arch.pfr0_csv3);
+			       meltdown_unaffected);
 
 	return (id_aa64pfr0_el1_sys_val & allow_mask) | set_mask;
 }
Signature · 1 line
2.40.0.348.gf938b09366-goog
maintainer
#1 lore raw
Quoted 72 lines · Fuad Tabba

The existing pKVM code attempts to do that using a value that's initialized to 0 but never set. To advertise csv2/3 to a protected guest, store them at the hypervisor and use them for setting csv2/3.

Similar to non-protected KVM, these are tracked as a system-wide variable, rather than per cpu, for simplicity.

Fixes 6c30bfb18d0b ("KVM: arm64: Add handlers for protected VM System Registers")
---
 arch/arm64/include/asm/kvm_hyp.h   |  3 +++
 arch/arm64/kvm/arm.c               |  2 ++
 arch/arm64/kvm/hyp/nvhe/sys_regs.c | 15 ++++++++++++---
 3 files changed, 17 insertions(+), 3 deletions(-)
arch/arm64/include/asm/kvm_hyp.h +3 -0
index bdd9cf546d95..723a645af191 100644
@@ -127,4 +127,7 @@ extern u64 kvm_nvhe_sym(id_aa64smfr0_el1_sys_val);
 extern unsigned long kvm_nvhe_sym(__icache_flags);
 extern unsigned int kvm_nvhe_sym(kvm_arm_vmid_bits);
 
+extern bool kvm_nvhe_sym(spectre_unaffected);
+extern bool kvm_nvhe_sym(meltdown_unaffected);
+
 #endif /* __ARM64_KVM_HYP_H__ */
arch/arm64/kvm/arm.c +2 -0
index 3bd732eaf087..364a4440ae54 100644
@@ -1902,6 +1902,8 @@ static void kvm_hyp_init_symbols(void)
 	kvm_nvhe_sym(id_aa64smfr0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64SMFR0_EL1);
 	kvm_nvhe_sym(__icache_flags) = __icache_flags;
 	kvm_nvhe_sym(kvm_arm_vmid_bits) = kvm_arm_vmid_bits;
+	kvm_nvhe_sym(spectre_unaffected) = (arm64_get_spectre_v2_state() == SPECTRE_UNAFFECTED);
+	kvm_nvhe_sym(meltdown_unaffected) = (arm64_get_meltdown_state() == SPECTRE_UNAFFECTED);
 }
 
 static int __init kvm_hyp_init_protection(u32 hyp_va_bits)
arch/arm64/kvm/hyp/nvhe/sys_regs.c +10 -1
index 08d2b004f4b7..3f647a2f4c96 100644
@@ -28,6 +28,16 @@ u64 id_aa64mmfr1_el1_sys_val;
 u64 id_aa64mmfr2_el1_sys_val;
 u64 id_aa64smfr0_el1_sys_val;
 
+/*
+ * Track whether the system isn't affected by spectre/meltown.
+ * Although this is per-CPU, we make it global for simplicity, e.g., not to have
+ * to worry about migration.
+ *
+ * Unlike for non-protected VMs, userspace cannot override this.
+ */
+bool spectre_unaffected;
+bool meltdown_unaffected;
+
 /*
  * Inject an unknown/undefined exception to an AArch64 guest while most of its
  * sysregs are live.
@@ -85,7 +95,6 @@ static u64 get_restricted_features_unsigned(u64 sys_reg_val,
 
 static u64 get_pvm_id_aa64pfr0(const struct kvm_vcpu *vcpu)
 {
-	const struct kvm *kvm = (const struct kvm *)kern_hyp_va(vcpu->kvm);
 	u64 set_mask = 0;
 	u64 allow_mask = PVM_ID_AA64PFR0_ALLOW;
 

arch/arm64/kvm/hyp/nvhe/sys_regs.c static u64 get_pvm_id_aa64pfr0(const struct kvm_vcpu *vcpu) · line 108

Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/sys_regs.c +2 -2
@@ -94,9 +103,9 @@ static u64 get_pvm_id_aa64pfr0(const struct kvm_vcpu *vcpu)
 
 	/* Spectre and Meltdown mitigation in KVM */
 	set_mask |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2),
-			       (u64)kvm->arch.pfr0_csv2);
+			       spectre_unaffected);
 	set_mask |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3),
-			       (u64)kvm->arch.pfr0_csv3);
+			       meltdown_unaffected);

Since you already have a sanitised version of ID_AA64PFR0_EL1, it would seem more straightforward to directly perform this adjustment at the point where you export the value, rather than doing it at runtime.

Ideally, the proton-pack code would perform the update and set the sanitised values directly in the cpufeature repository, but this would involve me looking at it again, and I really don't want to do that.

Thanks,

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

Hi Marc,

Quoted 76 lines · Marc Zyngier
Quoted 73 lines · Fuad Tabba

The existing pKVM code attempts to do that using a value that's initialized to 0 but never set. To advertise csv2/3 to a protected guest, store them at the hypervisor and use them for setting csv2/3.

Similar to non-protected KVM, these are tracked as a system-wide variable, rather than per cpu, for simplicity.

Fixes 6c30bfb18d0b ("KVM: arm64: Add handlers for protected VM System Registers")
---
 arch/arm64/include/asm/kvm_hyp.h   |  3 +++
 arch/arm64/kvm/arm.c               |  2 ++
 arch/arm64/kvm/hyp/nvhe/sys_regs.c | 15 ++++++++++++---
 3 files changed, 17 insertions(+), 3 deletions(-)
arch/arm64/include/asm/kvm_hyp.h +3 -0
index bdd9cf546d95..723a645af191 100644
@@ -127,4 +127,7 @@ extern u64 kvm_nvhe_sym(id_aa64smfr0_el1_sys_val);
 extern unsigned long kvm_nvhe_sym(__icache_flags);
 extern unsigned int kvm_nvhe_sym(kvm_arm_vmid_bits);
 
+extern bool kvm_nvhe_sym(spectre_unaffected);
+extern bool kvm_nvhe_sym(meltdown_unaffected);
+
 #endif /* __ARM64_KVM_HYP_H__ */
arch/arm64/kvm/arm.c +2 -0
index 3bd732eaf087..364a4440ae54 100644
@@ -1902,6 +1902,8 @@ static void kvm_hyp_init_symbols(void)
      kvm_nvhe_sym(id_aa64smfr0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64SMFR0_EL1);
      kvm_nvhe_sym(__icache_flags) = __icache_flags;
      kvm_nvhe_sym(kvm_arm_vmid_bits) = kvm_arm_vmid_bits;
+     kvm_nvhe_sym(spectre_unaffected) = (arm64_get_spectre_v2_state() == SPECTRE_UNAFFECTED);
+     kvm_nvhe_sym(meltdown_unaffected) = (arm64_get_meltdown_state() == SPECTRE_UNAFFECTED);
 }
 
 static int __init kvm_hyp_init_protection(u32 hyp_va_bits)
arch/arm64/kvm/hyp/nvhe/sys_regs.c +10 -1
index 08d2b004f4b7..3f647a2f4c96 100644
@@ -28,6 +28,16 @@ u64 id_aa64mmfr1_el1_sys_val;
 u64 id_aa64mmfr2_el1_sys_val;
 u64 id_aa64smfr0_el1_sys_val;
 
+/*
+ * Track whether the system isn't affected by spectre/meltown.
+ * Although this is per-CPU, we make it global for simplicity, e.g., not to have
+ * to worry about migration.
+ *
+ * Unlike for non-protected VMs, userspace cannot override this.
+ */
+bool spectre_unaffected;
+bool meltdown_unaffected;
+
 /*
  * Inject an unknown/undefined exception to an AArch64 guest while most of its
  * sysregs are live.
@@ -85,7 +95,6 @@ static u64 get_restricted_features_unsigned(u64 sys_reg_val,
 
 static u64 get_pvm_id_aa64pfr0(const struct kvm_vcpu *vcpu)
 {
-     const struct kvm *kvm = (const struct kvm *)kern_hyp_va(vcpu->kvm);
      u64 set_mask = 0;
      u64 allow_mask = PVM_ID_AA64PFR0_ALLOW;
 
@@ -94,9 +103,9 @@ static u64 get_pvm_id_aa64pfr0(const struct kvm_vcpu *vcpu)
 
Quoted 11 lines

arch/arm64/kvm/hyp/nvhe/sys_regs.c static u64 get_pvm_id_aa64pfr0(const struct kvm_vcpu *vcpu) · line 108

Quoted 7 lines
arch/arm64/kvm/hyp/nvhe/sys_regs.c +2 -2
      /* Spectre and Meltdown mitigation in KVM */
      set_mask |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2),
-                            (u64)kvm->arch.pfr0_csv2);
+                            spectre_unaffected);
      set_mask |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3),
-                            (u64)kvm->arch.pfr0_csv3);
+                            meltdown_unaffected);

Since you already have a sanitised version of ID_AA64PFR0_EL1, it would seem more straightforward to directly perform this adjustment at the point where you export the value, rather than doing it at runtime.

That makes more sense, and results in cleaner code. I'll do that.

Quoted 4 lines

Ideally, the proton-pack code would perform the update and set the sanitised values directly in the cpufeature repository, but this would involve me looking at it again, and I really don't want to do that.

You won't have to :) I'll post v2 soon.

Thanks!
/fuad

Quoted 6 lines

Thanks,

        M.

--
Without deviation from the norm, progress is not possible.