]> www.infradead.org Git - users/hch/misc.git/commitdiff
KVM: selftests: Add ex_str() to print human friendly name of exception vectors
authorSean Christopherson <seanjc@google.com>
Fri, 19 Sep 2025 22:32:51 +0000 (15:32 -0700)
committerSean Christopherson <seanjc@google.com>
Tue, 23 Sep 2025 15:39:02 +0000 (08:39 -0700)
Steal exception_mnemonic() from KVM-Unit-Tests as ex_str() (to keep line
lengths reasonable) and use it in assert messages that currently print the
raw vector number.

Co-developed-by: Chao Gao <chao.gao@intel.com>
Signed-off-by: Chao Gao <chao.gao@intel.com>
Link: https://lore.kernel.org/r/20250919223258.1604852-45-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
tools/testing/selftests/kvm/include/x86/processor.h
tools/testing/selftests/kvm/lib/x86/processor.c
tools/testing/selftests/kvm/x86/hyperv_features.c
tools/testing/selftests/kvm/x86/monitor_mwait_test.c
tools/testing/selftests/kvm/x86/pmu_counters_test.c
tools/testing/selftests/kvm/x86/vmx_pmu_caps_test.c
tools/testing/selftests/kvm/x86/xcr0_cpuid_test.c

index e8bad89fbb7f6ce4f2f48cdd06afb61350ed3010..fbe875eafca573c5d45ce00b26a702a686117035 100644 (file)
@@ -34,6 +34,8 @@ extern uint64_t guest_tsc_khz;
 
 #define NMI_VECTOR             0x02
 
+const char *ex_str(int vector);
+
 #define X86_EFLAGS_FIXED        (1u << 1)
 
 #define X86_CR4_VME            (1ul << 0)
index 4402d2e1ea69244b80dd9dc65147a00deeeb7544..33b0c436b0062d8d7999fda4793c4f0d07a2ef73 100644 (file)
@@ -24,6 +24,39 @@ bool host_cpu_is_intel;
 bool is_forced_emulation_enabled;
 uint64_t guest_tsc_khz;
 
+const char *ex_str(int vector)
+{
+       switch (vector) {
+#define VEC_STR(v) case v##_VECTOR: return "#" #v
+       case DE_VECTOR: return "no exception";
+       case KVM_MAGIC_DE_VECTOR: return "#DE";
+       VEC_STR(DB);
+       VEC_STR(NMI);
+       VEC_STR(BP);
+       VEC_STR(OF);
+       VEC_STR(BR);
+       VEC_STR(UD);
+       VEC_STR(NM);
+       VEC_STR(DF);
+       VEC_STR(TS);
+       VEC_STR(NP);
+       VEC_STR(SS);
+       VEC_STR(GP);
+       VEC_STR(PF);
+       VEC_STR(MF);
+       VEC_STR(AC);
+       VEC_STR(MC);
+       VEC_STR(XM);
+       VEC_STR(VE);
+       VEC_STR(CP);
+       VEC_STR(HV);
+       VEC_STR(VC);
+       VEC_STR(SX);
+       default: return "#??";
+#undef VEC_STR
+       }
+}
+
 static void regs_dump(FILE *stream, struct kvm_regs *regs, uint8_t indent)
 {
        fprintf(stream, "%*srax: 0x%.16llx rbx: 0x%.16llx "
index 068e9c69710d2e05f4561ddf4e49efc34e1b6e34..99d327084172f959f02ae86cfc5e122a4a42a783 100644 (file)
@@ -54,12 +54,12 @@ static void guest_msr(struct msr_data *msr)
 
        if (msr->fault_expected)
                __GUEST_ASSERT(vector == GP_VECTOR,
-                              "Expected #GP on %sMSR(0x%x), got vector '0x%x'",
-                              msr->write ? "WR" : "RD", msr->idx, vector);
+                              "Expected #GP on %sMSR(0x%x), got %s",
+                              msr->write ? "WR" : "RD", msr->idx, ex_str(vector));
        else
                __GUEST_ASSERT(!vector,
-                              "Expected success on %sMSR(0x%x), got vector '0x%x'",
-                              msr->write ? "WR" : "RD", msr->idx, vector);
+                              "Expected success on %sMSR(0x%x), got %s",
+                              msr->write ? "WR" : "RD", msr->idx, ex_str(vector));
 
        if (vector || is_write_only_msr(msr->idx))
                goto done;
@@ -102,12 +102,12 @@ static void guest_hcall(vm_vaddr_t pgs_gpa, struct hcall_data *hcall)
        vector = __hyperv_hypercall(hcall->control, input, output, &res);
        if (hcall->ud_expected) {
                __GUEST_ASSERT(vector == UD_VECTOR,
-                              "Expected #UD for control '%lu', got vector '0x%x'",
-                              hcall->control, vector);
+                              "Expected #UD for control '%lu', got %s",
+                              hcall->control, ex_str(vector));
        } else {
                __GUEST_ASSERT(!vector,
-                              "Expected no exception for control '%lu', got vector '0x%x'",
-                              hcall->control, vector);
+                              "Expected no exception for control '%lu', got %s",
+                              hcall->control, ex_str(vector));
                GUEST_ASSERT_EQ(res, hcall->expect);
        }
 
index 0eb371c62ab876f6a4ba15557d026d2cf849e778..e45c028d2a7ec0c7f5a29465fbcf0225eabe9c27 100644 (file)
@@ -30,12 +30,12 @@ do {                                                                        \
                                                                        \
        if (fault_wanted)                                               \
                __GUEST_ASSERT((vector) == UD_VECTOR,                   \
-                              "Expected #UD on " insn " for testcase '0x%x', got '0x%x'", \
-                              testcase, vector);                       \
+                              "Expected #UD on " insn " for testcase '0x%x', got %s", \
+                              testcase, ex_str(vector));               \
        else                                                            \
                __GUEST_ASSERT(!(vector),                               \
-                              "Expected success on " insn " for testcase '0x%x', got '0x%x'", \
-                              testcase, vector);                       \
+                              "Expected success on " insn " for testcase '0x%x', got %s", \
+                              testcase, ex_str(vector));               \
 } while (0)
 
 static void guest_monitor_wait(void *arg)
index eb6c12a2cdd48d71c2f79fc83e2c0d205b033f28..bb215230cc8a286ea3d7efff037b7c1294e79f62 100644 (file)
@@ -363,8 +363,8 @@ static void test_arch_events(uint8_t pmu_version, uint64_t perf_capabilities,
 
 #define GUEST_ASSERT_PMC_MSR_ACCESS(insn, msr, expect_gp, vector)              \
 __GUEST_ASSERT(expect_gp ? vector == GP_VECTOR : !vector,                      \
-              "Expected %s on " #insn "(0x%x), got vector %u",                 \
-              expect_gp ? "#GP" : "no fault", msr, vector)                     \
+              "Expected %s on " #insn "(0x%x), got %s",                        \
+              expect_gp ? "#GP" : "no fault", msr, ex_str(vector))             \
 
 #define GUEST_ASSERT_PMC_VALUE(insn, msr, val, expected)                       \
        __GUEST_ASSERT(val == expected,                                 \
index f8deea22015689471e7196417838041e113b41b6..7ff6f62e20a360c091edc27927762283796f9f8b 100644 (file)
@@ -57,8 +57,8 @@ static void guest_test_perf_capabilities_gp(uint64_t val)
        uint8_t vector = wrmsr_safe(MSR_IA32_PERF_CAPABILITIES, val);
 
        __GUEST_ASSERT(vector == GP_VECTOR,
-                      "Expected #GP for value '0x%lx', got vector '0x%x'",
-                      val, vector);
+                      "Expected #GP for value '0x%lx', got %s",
+                      val, ex_str(vector));
 }
 
 static void guest_code(uint64_t current_val)
index c8a5c5e516619de79c7f0435e84bb9b9a78cb2f1..d038c1571729c9b6732079ca0986c7bb4663afe1 100644 (file)
@@ -81,13 +81,13 @@ static void guest_code(void)
 
        vector = xsetbv_safe(0, XFEATURE_MASK_FP);
        __GUEST_ASSERT(!vector,
-                      "Expected success on XSETBV(FP), got vector '0x%x'",
-                      vector);
+                      "Expected success on XSETBV(FP), got %s",
+                      ex_str(vector));
 
        vector = xsetbv_safe(0, supported_xcr0);
        __GUEST_ASSERT(!vector,
-                      "Expected success on XSETBV(0x%lx), got vector '0x%x'",
-                      supported_xcr0, vector);
+                      "Expected success on XSETBV(0x%lx), got %s",
+                      supported_xcr0, ex_str(vector));
 
        for (i = 0; i < 64; i++) {
                if (supported_xcr0 & BIT_ULL(i))
@@ -95,8 +95,8 @@ static void guest_code(void)
 
                vector = xsetbv_safe(0, supported_xcr0 | BIT_ULL(i));
                __GUEST_ASSERT(vector == GP_VECTOR,
-                              "Expected #GP on XSETBV(0x%llx), supported XCR0 = %lx, got vector '0x%x'",
-                              BIT_ULL(i), supported_xcr0, vector);
+                              "Expected #GP on XSETBV(0x%llx), supported XCR0 = %lx, got %s",
+                              BIT_ULL(i), supported_xcr0, ex_str(vector));
        }
 
        GUEST_DONE();