Skip to content

Commit df1f294

Browse files
committed
KVM: selftests: Add ex_str() to print human friendly name of exception vectors
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 <[email protected]> Signed-off-by: Chao Gao <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Sean Christopherson <[email protected]>
1 parent ff86b48 commit df1f294

File tree

7 files changed

+57
-22
lines changed

7 files changed

+57
-22
lines changed

tools/testing/selftests/kvm/include/x86/processor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ extern uint64_t guest_tsc_khz;
3434

3535
#define NMI_VECTOR 0x02
3636

37+
const char *ex_str(int vector);
38+
3739
#define X86_EFLAGS_FIXED (1u << 1)
3840

3941
#define X86_CR4_VME (1ul << 0)

tools/testing/selftests/kvm/lib/x86/processor.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,39 @@ bool host_cpu_is_intel;
2424
bool is_forced_emulation_enabled;
2525
uint64_t guest_tsc_khz;
2626

27+
const char *ex_str(int vector)
28+
{
29+
switch (vector) {
30+
#define VEC_STR(v) case v##_VECTOR: return "#" #v
31+
case DE_VECTOR: return "no exception";
32+
case KVM_MAGIC_DE_VECTOR: return "#DE";
33+
VEC_STR(DB);
34+
VEC_STR(NMI);
35+
VEC_STR(BP);
36+
VEC_STR(OF);
37+
VEC_STR(BR);
38+
VEC_STR(UD);
39+
VEC_STR(NM);
40+
VEC_STR(DF);
41+
VEC_STR(TS);
42+
VEC_STR(NP);
43+
VEC_STR(SS);
44+
VEC_STR(GP);
45+
VEC_STR(PF);
46+
VEC_STR(MF);
47+
VEC_STR(AC);
48+
VEC_STR(MC);
49+
VEC_STR(XM);
50+
VEC_STR(VE);
51+
VEC_STR(CP);
52+
VEC_STR(HV);
53+
VEC_STR(VC);
54+
VEC_STR(SX);
55+
default: return "#??";
56+
#undef VEC_STR
57+
}
58+
}
59+
2760
static void regs_dump(FILE *stream, struct kvm_regs *regs, uint8_t indent)
2861
{
2962
fprintf(stream, "%*srax: 0x%.16llx rbx: 0x%.16llx "

tools/testing/selftests/kvm/x86/hyperv_features.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ static void guest_msr(struct msr_data *msr)
5454

5555
if (msr->fault_expected)
5656
__GUEST_ASSERT(vector == GP_VECTOR,
57-
"Expected #GP on %sMSR(0x%x), got vector '0x%x'",
58-
msr->write ? "WR" : "RD", msr->idx, vector);
57+
"Expected #GP on %sMSR(0x%x), got %s",
58+
msr->write ? "WR" : "RD", msr->idx, ex_str(vector));
5959
else
6060
__GUEST_ASSERT(!vector,
61-
"Expected success on %sMSR(0x%x), got vector '0x%x'",
62-
msr->write ? "WR" : "RD", msr->idx, vector);
61+
"Expected success on %sMSR(0x%x), got %s",
62+
msr->write ? "WR" : "RD", msr->idx, ex_str(vector));
6363

6464
if (vector || is_write_only_msr(msr->idx))
6565
goto done;
@@ -102,12 +102,12 @@ static void guest_hcall(vm_vaddr_t pgs_gpa, struct hcall_data *hcall)
102102
vector = __hyperv_hypercall(hcall->control, input, output, &res);
103103
if (hcall->ud_expected) {
104104
__GUEST_ASSERT(vector == UD_VECTOR,
105-
"Expected #UD for control '%lu', got vector '0x%x'",
106-
hcall->control, vector);
105+
"Expected #UD for control '%lu', got %s",
106+
hcall->control, ex_str(vector));
107107
} else {
108108
__GUEST_ASSERT(!vector,
109-
"Expected no exception for control '%lu', got vector '0x%x'",
110-
hcall->control, vector);
109+
"Expected no exception for control '%lu', got %s",
110+
hcall->control, ex_str(vector));
111111
GUEST_ASSERT_EQ(res, hcall->expect);
112112
}
113113

tools/testing/selftests/kvm/x86/monitor_mwait_test.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ do { \
3030
\
3131
if (fault_wanted) \
3232
__GUEST_ASSERT((vector) == UD_VECTOR, \
33-
"Expected #UD on " insn " for testcase '0x%x', got '0x%x'", \
34-
testcase, vector); \
33+
"Expected #UD on " insn " for testcase '0x%x', got %s", \
34+
testcase, ex_str(vector)); \
3535
else \
3636
__GUEST_ASSERT(!(vector), \
37-
"Expected success on " insn " for testcase '0x%x', got '0x%x'", \
38-
testcase, vector); \
37+
"Expected success on " insn " for testcase '0x%x', got %s", \
38+
testcase, ex_str(vector)); \
3939
} while (0)
4040

4141
static void guest_monitor_wait(void *arg)

tools/testing/selftests/kvm/x86/pmu_counters_test.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,8 +363,8 @@ static void test_arch_events(uint8_t pmu_version, uint64_t perf_capabilities,
363363

364364
#define GUEST_ASSERT_PMC_MSR_ACCESS(insn, msr, expect_gp, vector) \
365365
__GUEST_ASSERT(expect_gp ? vector == GP_VECTOR : !vector, \
366-
"Expected %s on " #insn "(0x%x), got vector %u", \
367-
expect_gp ? "#GP" : "no fault", msr, vector) \
366+
"Expected %s on " #insn "(0x%x), got %s", \
367+
expect_gp ? "#GP" : "no fault", msr, ex_str(vector)) \
368368

369369
#define GUEST_ASSERT_PMC_VALUE(insn, msr, val, expected) \
370370
__GUEST_ASSERT(val == expected, \

tools/testing/selftests/kvm/x86/vmx_pmu_caps_test.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ static void guest_test_perf_capabilities_gp(uint64_t val)
5757
uint8_t vector = wrmsr_safe(MSR_IA32_PERF_CAPABILITIES, val);
5858

5959
__GUEST_ASSERT(vector == GP_VECTOR,
60-
"Expected #GP for value '0x%lx', got vector '0x%x'",
61-
val, vector);
60+
"Expected #GP for value '0x%lx', got %s",
61+
val, ex_str(vector));
6262
}
6363

6464
static void guest_code(uint64_t current_val)

tools/testing/selftests/kvm/x86/xcr0_cpuid_test.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,22 +81,22 @@ static void guest_code(void)
8181

8282
vector = xsetbv_safe(0, XFEATURE_MASK_FP);
8383
__GUEST_ASSERT(!vector,
84-
"Expected success on XSETBV(FP), got vector '0x%x'",
85-
vector);
84+
"Expected success on XSETBV(FP), got %s",
85+
ex_str(vector));
8686

8787
vector = xsetbv_safe(0, supported_xcr0);
8888
__GUEST_ASSERT(!vector,
89-
"Expected success on XSETBV(0x%lx), got vector '0x%x'",
90-
supported_xcr0, vector);
89+
"Expected success on XSETBV(0x%lx), got %s",
90+
supported_xcr0, ex_str(vector));
9191

9292
for (i = 0; i < 64; i++) {
9393
if (supported_xcr0 & BIT_ULL(i))
9494
continue;
9595

9696
vector = xsetbv_safe(0, supported_xcr0 | BIT_ULL(i));
9797
__GUEST_ASSERT(vector == GP_VECTOR,
98-
"Expected #GP on XSETBV(0x%llx), supported XCR0 = %lx, got vector '0x%x'",
99-
BIT_ULL(i), supported_xcr0, vector);
98+
"Expected #GP on XSETBV(0x%llx), supported XCR0 = %lx, got %s",
99+
BIT_ULL(i), supported_xcr0, ex_str(vector));
100100
}
101101

102102
GUEST_DONE();

0 commit comments

Comments
 (0)