Skip to contentSkip to conversation

KVM: selftests: Add guest-side test for AMD HWCR.McStatusWrEn

PATCH Superseded Costas Argyris 2 participants 4 messages May 7 – Jul 6

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: selftests: Add guest-side test for AMD HWCR.McStatusWrEn

The existing hwcr_msr_test exercises MSR_K7_HWCR only via KVM_SET_MSRS, which sets host_initiated=true in KVM. The gate in set_msr_mce() that calls can_set_mci_status() short-circuits when host_initiated=true, so can_set_mci_status() is never reached by the existing test.

Add a guest-side test that verifies HWCR.McStatusWrEn (bit 18) correctly gates non-zero writes to MCi_STATUS MSRs. With the bit set, the write must succeed and read back unchanged. With the bit clear, the write must raise #GP and leave the register unmodified.

---
 .../testing/selftests/kvm/x86/hwcr_msr_test.c | 65 ++++++++++++++++++-
 1 file changed, 63 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/x86/hwcr_msr_test.c +63 -2
index 10b1b0ba374e..4e249bffcbb1 100644
@@ -6,9 +6,9 @@
 
 #include "test_util.h"
 #include "kvm_util.h"
-#include "vmx.h"
+#include "processor.h"
 
-void test_hwcr_bit(struct kvm_vcpu *vcpu, unsigned int bit)
+static void test_hwcr_bit(struct kvm_vcpu *vcpu, unsigned int bit)
 {
 	const uint64_t ignored = BIT_ULL(3) | BIT_ULL(6) | BIT_ULL(8);
 	const uint64_t valid = BIT_ULL(18) | BIT_ULL(24);
@@ -30,6 +30,61 @@ void test_hwcr_bit(struct kvm_vcpu *vcpu, unsigned int bit)
 	vcpu_set_msr(vcpu, MSR_K7_HWCR, 0);
 }
 
+/*
+ * AMD-specific: test that HWCR.McStatusWrEn (bit 18) gates guest writes to
+ * MCi_STATUS MSRs.  With the bit set, a non-zero write to MC0_STATUS must
+ * succeed and read back unchanged.  With the bit clear, the write must take
+ * a #GP.
+ *
+ * This exercises arch/x86/kvm/x86.c:can_set_mci_status(), which is only
+ * reachable via the guest WRMSR path (host_initiated=false); test_hwcr_bit()
+ * uses KVM_SET_MSRS (host_initiated=true) and never triggers it.
+ */
+static void guest_code(void)
+{
+	uint8_t vector;
+	uint64_t val;
+
+	/* McStatusWrEn=1: non-zero write to MCi_STATUS must succeed. */
+	wrmsr(MSR_K7_HWCR, BIT_ULL(18));
+	wrmsr(MSR_IA32_MC0_STATUS, 1);
+	val = rdmsr(MSR_IA32_MC0_STATUS);
+	GUEST_ASSERT_EQ(val, 1);
+
+	/* Clear the status register before disabling the write-enable bit. */
+	wrmsr(MSR_IA32_MC0_STATUS, 0);
+
+	/* McStatusWrEn=0: non-zero write to MCi_STATUS must #GP. */
+	wrmsr(MSR_K7_HWCR, 0);
+	vector = wrmsr_safe(MSR_IA32_MC0_STATUS, 1);
+	GUEST_ASSERT_EQ(vector, GP_VECTOR);
+
+	/* Confirm the failed write left the register at zero. */
+	val = rdmsr(MSR_IA32_MC0_STATUS);
+	GUEST_ASSERT_EQ(val, 0);
+
+	GUEST_DONE();
+}
+
+static void enter_guest(struct kvm_vcpu *vcpu)
+{
+	struct ucall uc;
+
+	while (true) {
+		vcpu_run(vcpu);
+		TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
+
+		switch (get_ucall(vcpu, &uc)) {
+		case UCALL_DONE:
+			return;
+		case UCALL_ABORT:
+			REPORT_GUEST_ASSERT(uc);
+		default:
+			TEST_FAIL("Unexpected ucall %lu", uc.cmd);
+		}
+	}
+}
+
 int main(int argc, char *argv[])
 {
 	struct kvm_vm *vm;
@@ -42,4 +97,10 @@ int main(int argc, char *argv[])
 		test_hwcr_bit(vcpu, bit);
 
 	kvm_vm_free(vm);
+
+	if (host_cpu_is_amd_compatible) {
+		vm = vm_create_with_one_vcpu(&vcpu, guest_code);
+		enter_guest(vcpu);
+		kvm_vm_free(vm);
+	}
 }
Signature · 1 line
2.43.0
#1 lore raw
Quoted 10 lines · Costas Argyris

The existing hwcr_msr_test exercises MSR_K7_HWCR only via KVM_SET_MSRS, which sets host_initiated=true in KVM. The gate in set_msr_mce() that calls can_set_mci_status() short-circuits when host_initiated=true, so can_set_mci_status() is never reached by the existing test.

Add a guest-side test that verifies HWCR.McStatusWrEn (bit 18) correctly gates non-zero writes to MCi_STATUS MSRs. With the bit set, the write must succeed and read back unchanged. With the bit clear, the write must raise #GP and leave the register unmodified.

Quoted 4 lines
---
 .../testing/selftests/kvm/x86/hwcr_msr_test.c | 65 ++++++++++++++++++-
 1 file changed, 63 insertions(+), 2 deletions(-)

Rather than pile code into a one-off MSR test, how hard would it be to fold this into msrs_test.c?

#2 lore raw

PATCH v2 1/1 KVM: selftests: Add guest-side test for AMD HWCR.McStatusWrEn

Add a guest-side test to msrs_test that verifies HWCR.McStatusWrEn (bit 18) correctly gates non-zero writes to MCi_STATUS MSRs. With the bit set, the write must succeed and read back unchanged. With the bit clear, the write must raise #GP and leave the register unmodified.

This exercises can_set_mci_status(), which is only reachable via the guest WRMSR path (host_initiated=false).

---
v1 -> v2:
- Fold test into msrs_test.c
- Reuse existing helpers for the guest code
- Drop unnecessary while loop

I tried fitting into the existing __msrs[] table but it doesn't seem to map well — the proposed test has one MSR (HWCR) gating writes to another (MCi_STATUS), which doesn't seem to fit the per-MSR CPUID-feature model. Tried a self-contained function alongside test_msrs() instead.

 tools/testing/selftests/kvm/x86/msrs_test.c | 59 +++++++++++++++++++++
 1 file changed, 59 insertions(+)
tools/testing/selftests/kvm/x86/msrs_test.c +59 -0
index f7e39bf887ad..348811559831 100644
@@ -476,6 +476,62 @@ static void test_msrs(void)
 	kvm_vm_free(vm);
 }
 
+/*
+ * AMD-specific: test that HWCR.McStatusWrEn (bit 18) gates guest writes to
+ * MCi_STATUS MSRs.  With the bit set, a non-zero write to MC0_STATUS must
+ * succeed and read back unchanged.  With the bit clear, the write must take
+ * a #GP.
+ *
+ * This exercises can_set_mci_status(), which is only reachable via the guest
+ * WRMSR path (host_initiated=false).
+ */
+static void guest_test_mcstatus_wren(void)
+{
+	const u64 val = 1;
+	u8 vec;
+
+	/* McStatusWrEn=1: non-zero write to MCi_STATUS must succeed. */
+	wrmsr(MSR_K7_HWCR, BIT_ULL(18));
+	__wrmsr(MSR_IA32_MC0_STATUS, val);
+
+	/* Clear before disabling the write-enable bit. */
+	__wrmsr(MSR_IA32_MC0_STATUS, 0);
+
+	/* McStatusWrEn=0: non-zero write to MCi_STATUS must #GP. */
+	wrmsr(MSR_K7_HWCR, 0);
+	vec = wrmsr_safe(MSR_IA32_MC0_STATUS, val);
+	__GUEST_ASSERT(vec == GP_VECTOR,
+		       "Wanted #GP on WRMSR(0x%x, 0x%lx), got %s",
+		       MSR_IA32_MC0_STATUS, val, ex_str(vec));
+
+	/* Confirm the failed write left the register at zero. */
+	__rdmsr(MSR_IA32_MC0_STATUS, 0);
+
+	GUEST_DONE();
+}
+
+static void test_mcstatus_wren(void)
+{
+	struct kvm_vcpu *vcpu;
+	struct kvm_vm *vm;
+	struct ucall uc;
+
+	vm = vm_create_with_one_vcpu(&vcpu, guest_test_mcstatus_wren);
+	vcpu_run(vcpu);
+	TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
+
+	switch (get_ucall(vcpu, &uc)) {
+	case UCALL_DONE:
+		break;
+	case UCALL_ABORT:
+		REPORT_GUEST_ASSERT(uc);
+	default:
+		TEST_FAIL("Unexpected ucall %lu", uc.cmd);
+	}
+
+	kvm_vm_free(vm);
+}
+
 int main(void)
 {
 	has_one_reg = kvm_has_cap(KVM_CAP_ONE_REG);
@@ -486,4 +542,7 @@ int main(void)
 		use_one_reg = true;
 		test_msrs();
 	}
+
+	if (host_cpu_is_amd_compatible)
+		test_mcstatus_wren();
 }
Signature · 1 line
2.43.0