Skip to content

Commit 0f52e4a

Browse files
committed
KVM: selftests: Convert the MONITOR/MWAIT test to use printf guest asserts
Convert x86's MONITOR/MWAIT test to use printf-based guest asserts. Add a macro to handle reporting failures to reduce the amount of copy+paste needed for MONITOR vs. MWAIT. Link: https://lore.kernel.org/r/20230729003643.1053367-25-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent bf6c760 commit 0f52e4a

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

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

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// SPDX-License-Identifier: GPL-2.0
2+
#define USE_GUEST_ASSERT_PRINTF 1
3+
24
#include <fcntl.h>
35
#include <stdio.h>
46
#include <stdlib.h>
@@ -16,14 +18,25 @@ enum monitor_mwait_testcases {
1618
MWAIT_DISABLED = BIT(2),
1719
};
1820

21+
/*
22+
* If both MWAIT and its quirk are disabled, MONITOR/MWAIT should #UD, in all
23+
* other scenarios KVM should emulate them as nops.
24+
*/
25+
#define GUEST_ASSERT_MONITOR_MWAIT(insn, testcase, vector) \
26+
do { \
27+
bool fault_wanted = ((testcase) & MWAIT_QUIRK_DISABLED) && \
28+
((testcase) & MWAIT_DISABLED); \
29+
\
30+
if (fault_wanted) \
31+
__GUEST_ASSERT((vector) == UD_VECTOR, \
32+
"Expected #UD on " insn " for testcase '0x%x', got '0x%x'", vector); \
33+
else \
34+
__GUEST_ASSERT(!(vector), \
35+
"Expected success on " insn " for testcase '0x%x', got '0x%x'", vector); \
36+
} while (0)
37+
1938
static void guest_monitor_wait(int testcase)
2039
{
21-
/*
22-
* If both MWAIT and its quirk are disabled, MONITOR/MWAIT should #UD,
23-
* in all other scenarios KVM should emulate them as nops.
24-
*/
25-
bool fault_wanted = (testcase & MWAIT_QUIRK_DISABLED) &&
26-
(testcase & MWAIT_DISABLED);
2740
u8 vector;
2841

2942
GUEST_SYNC(testcase);
@@ -33,16 +46,10 @@ static void guest_monitor_wait(int testcase)
3346
* intercept checks, so the inputs for MONITOR and MWAIT must be valid.
3447
*/
3548
vector = kvm_asm_safe("monitor", "a"(guest_monitor_wait), "c"(0), "d"(0));
36-
if (fault_wanted)
37-
GUEST_ASSERT_2(vector == UD_VECTOR, testcase, vector);
38-
else
39-
GUEST_ASSERT_2(!vector, testcase, vector);
49+
GUEST_ASSERT_MONITOR_MWAIT("MONITOR", testcase, vector);
4050

4151
vector = kvm_asm_safe("mwait", "a"(guest_monitor_wait), "c"(0), "d"(0));
42-
if (fault_wanted)
43-
GUEST_ASSERT_2(vector == UD_VECTOR, testcase, vector);
44-
else
45-
GUEST_ASSERT_2(!vector, testcase, vector);
52+
GUEST_ASSERT_MONITOR_MWAIT("MWAIT", testcase, vector);
4653
}
4754

4855
static void guest_code(void)
@@ -85,7 +92,7 @@ int main(int argc, char *argv[])
8592
testcase = uc.args[1];
8693
break;
8794
case UCALL_ABORT:
88-
REPORT_GUEST_ASSERT_2(uc, "testcase = %lx, vector = %ld");
95+
REPORT_GUEST_ASSERT(uc);
8996
goto done;
9097
case UCALL_DONE:
9198
goto done;

0 commit comments

Comments
 (0)