Skip to contentSkip to conversation

KVM unit tests for SVM options features

PATCH In review Maxim Levitsky 1 participant 11 messages Mar 22, 2022 KVM

lore
Share & tools
lore
#0 lore raw

cover KVM unit tests for SVM options features

Those are few fixes and unit tests I used to test svm optional features and few more svm feaures.

Best regards,
Maxim Levitsky

Maxim Levitsky (9):
pmu_lbr: few fixes
svm: Fix reg_corruption test, to avoid timer interrupt firing in later
tests.
svm: NMI is an "exception" and not interrupt in x86 land
svm: intercept shutdown in all svm tests by default
svm: add SVM_BARE_VMRUN
svm: add tests for LBR virtualization
svm: add tests for case when L1 intercepts various hardware interrupts
svm: add test for nested tsc scaling
svm: add test for pause filter and threshold

 lib/x86/msr.h       |   1 +
 lib/x86/processor.h |   4 +
 x86/pmu_lbr.c       |   6 +
 x86/svm.c           |  57 ++---
 x86/svm.h           |  71 +++++-
 x86/svm_tests.c     | 582 +++++++++++++++++++++++++++++++++++++++++++-
 x86/unittests.cfg   |   8 +-
 7 files changed, 688 insertions(+), 41 deletions(-)
Signature · 1 line
2.26.3
#1 lore raw

PATCH 1/9 pmu_lbr: few fixes

* don't run this test on AMD since AMD's LBR is not the same as Intel's LBR and needs a different test.

* don't run this test on 32 bit as it is not built for 32 bit anyway

---
 x86/pmu_lbr.c     | 6 ++++++
 x86/unittests.cfg | 1 +
 2 files changed, 7 insertions(+)
x86/pmu_lbr.c +6 -0
index 5ff805a..688634d 100644
@@ -68,6 +68,12 @@ int main(int ac, char **av)
 	int max, i;
 
 	setup_vm();
+
+	if (!is_intel()) {
+		report_skip("PMU_LBR test is for intel CPU's only");
+		return 0;
+	}
+
 	perf_cap = rdmsr(MSR_IA32_PERF_CAPABILITIES);
 	eax.full = id.a;
 
x86/unittests.cfg +1 -0
index 9a70ba3..89ff949 100644
@@ -179,6 +179,7 @@ check = /proc/sys/kernel/nmi_watchdog=0
 accel = kvm
 
 [pmu_lbr]
+arch = x86_64
 file = pmu_lbr.flat
 extra_params = -cpu host,migratable=no
 check = /sys/module/kvm/parameters/ignore_msrs=N
Signature · 1 line
2.26.3
#2 lore raw

PATCH 2/9 svm: Fix reg_corruption test, to avoid timer interrupt firing in later tests.

In reply to Maxim Levitsky

The test were setting APIC periodic timer but not disabling it later.

Fixes da338a3 ("SVM: add test for nested guest RIP corruption")
---
 x86/svm_tests.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
x86/svm_tests.c +7 -2
index 0707786..7a97847 100644
@@ -1847,7 +1847,7 @@ static bool reg_corruption_finished(struct svm_test *test)
         report_pass("No RIP corruption detected after %d timer interrupts",
                     isr_cnt);
         set_test_stage(test, 1);
-        return true;
+        goto cleanup;
     }
 
     if (vmcb->control.exit_code == SVM_EXIT_INTR) {
@@ -1861,11 +1861,16 @@ static bool reg_corruption_finished(struct svm_test *test)
         if (guest_rip == insb_instruction_label && io_port_var != 0xAA) {
             report_fail("RIP corruption detected after %d timer interrupts",
                         isr_cnt);
-            return true;
+            goto cleanup;
         }
 
     }
     return false;
+cleanup:
+    apic_write(APIC_LVTT, APIC_LVT_TIMER_MASK);
+    apic_write(APIC_TMICT, 0);
+    return true;
+
 }
 
 static bool reg_corruption_check(struct svm_test *test)
Signature · 1 line
2.26.3
#3 lore raw

PATCH 3/9 svm: NMI is an "exception" and not interrupt in x86 land

In reply to Maxim Levitsky

This can interfere with later tests which do treat it as an exception.

Fixes d4db486 ("svm: Add test cases around NMI injection")
---
 x86/svm_tests.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
x86/svm_tests.c +2 -3
index 7a97847..7586ef7 100644
@@ -1384,17 +1384,16 @@ static bool interrupt_check(struct svm_test *test)
 
 static volatile bool nmi_fired;
 
-static void nmi_handler(isr_regs_t *regs)
+static void nmi_handler(struct ex_regs *regs)
 {
     nmi_fired = true;
-    apic_write(APIC_EOI, 0);
 }
 
 static void nmi_prepare(struct svm_test *test)
 {
     default_prepare(test);
     nmi_fired = false;
-    handle_irq(NMI_VECTOR, nmi_handler);
+    handle_exception(NMI_VECTOR, nmi_handler);
     set_test_stage(test, 0);
 }
 
Signature · 1 line
2.26.3
#4 lore raw

PATCH 4/9 svm: intercept shutdown in all svm tests by default

In reply to Maxim Levitsky

If L1 doesn't intercept shutdown, then L1 itself gets it, which doesn't allow it to report the error that happened.

---
 x86/svm.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
x86/svm.c +3 -1
index 3f94b2a..62da2af 100644
@@ -174,7 +174,9 @@ void vmcb_ident(struct vmcb *vmcb)
 	save->cr2 = read_cr2();
 	save->g_pat = rdmsr(MSR_IA32_CR_PAT);
 	save->dbgctl = rdmsr(MSR_IA32_DEBUGCTLMSR);
-	ctrl->intercept = (1ULL << INTERCEPT_VMRUN) | (1ULL << INTERCEPT_VMMCALL);
+	ctrl->intercept = (1ULL << INTERCEPT_VMRUN) |
+			  (1ULL << INTERCEPT_VMMCALL) |
+			  (1ULL << INTERCEPT_SHUTDOWN);
 	ctrl->iopm_base_pa = virt_to_phys(io_bitmap);
 	ctrl->msrpm_base_pa = virt_to_phys(msr_bitmap);
 
Signature · 1 line
2.26.3
#5 lore raw

PATCH 5/9 svm: add SVM_BARE_VMRUN

In reply to Maxim Levitsky

This will be useful in nested LBR tests to ensure that no extra branches are made in the guest entry.

---
 x86/svm.c | 32 --------------------------------
 x86/svm.h | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+), 32 deletions(-)
x86/svm.c +0 -32
index 62da2af..6f4e023 100644
@@ -196,41 +196,9 @@ struct regs get_regs(void)
 
 // rax handled specially below
 
-#define SAVE_GPR_C                              \
-        "xchg %%rbx, regs+0x8\n\t"              \
-        "xchg %%rcx, regs+0x10\n\t"             \
-        "xchg %%rdx, regs+0x18\n\t"             \
-        "xchg %%rbp, regs+0x28\n\t"             \
-        "xchg %%rsi, regs+0x30\n\t"             \
-        "xchg %%rdi, regs+0x38\n\t"             \
-        "xchg %%r8, regs+0x40\n\t"              \
-        "xchg %%r9, regs+0x48\n\t"              \
-        "xchg %%r10, regs+0x50\n\t"             \
-        "xchg %%r11, regs+0x58\n\t"             \
-        "xchg %%r12, regs+0x60\n\t"             \
-        "xchg %%r13, regs+0x68\n\t"             \
-        "xchg %%r14, regs+0x70\n\t"             \
-        "xchg %%r15, regs+0x78\n\t"
-
-#define LOAD_GPR_C      SAVE_GPR_C
 
 struct svm_test *v2_test;
 
-#define ASM_PRE_VMRUN_CMD                       \
-                "vmload %%rax\n\t"              \
-                "mov regs+0x80, %%r15\n\t"      \
-                "mov %%r15, 0x170(%%rax)\n\t"   \
-                "mov regs, %%r15\n\t"           \
-                "mov %%r15, 0x1f8(%%rax)\n\t"   \
-                LOAD_GPR_C                      \
-
-#define ASM_POST_VMRUN_CMD                      \
-                SAVE_GPR_C                      \
-                "mov 0x170(%%rax), %%r15\n\t"   \
-                "mov %%r15, regs+0x80\n\t"      \
-                "mov 0x1f8(%%rax), %%r15\n\t"   \
-                "mov %%r15, regs\n\t"           \
-                "vmsave %%rax\n\t"              \
 
 u64 guest_stack[10000];
 
x86/svm.h +47 -0

57 lines not on this page · raw message

Signature · 1 line
2.26.3
#6 lore raw

PATCH 6/9 svm: add tests for LBR virtualization

In reply to Maxim Levitsky
---
 lib/x86/processor.h |   1 +
 x86/svm.c           |   5 +
 x86/svm.h           |   5 +-
 x86/svm_tests.c     | 239 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 249 insertions(+), 1 deletion(-)
lib/x86/processor.h +1 -0
index 117032a..b01c3d0 100644
@@ -187,6 +187,7 @@ static inline bool is_intel(void)
 #define	X86_FEATURE_RDPRU		(CPUID(0x80000008, 0, EBX, 4))
 #define	X86_FEATURE_AMD_IBPB		(CPUID(0x80000008, 0, EBX, 12))
 #define	X86_FEATURE_NPT			(CPUID(0x8000000A, 0, EDX, 0))
+#define	X86_FEATURE_LBRV		(CPUID(0x8000000A, 0, EDX, 1))
 #define	X86_FEATURE_NRIPS		(CPUID(0x8000000A, 0, EDX, 3))
 #define	X86_FEATURE_VGIF		(CPUID(0x8000000A, 0, EDX, 16))
 
x86/svm.c +5 -0
index 6f4e023..bb58d7c 100644
@@ -70,6 +70,11 @@ bool vgif_supported(void)
 	return this_cpu_has(X86_FEATURE_VGIF);
 }
 
+bool lbrv_supported(void)
+{
+    return this_cpu_has(X86_FEATURE_LBRV);
+}
+
 void default_prepare(struct svm_test *test)
 {
 	vmcb_ident(vmcb);
x86/svm.h +4 -1
index 6d072f4..58b9410 100644
@@ -98,7 +98,7 @@ struct __attribute__ ((__packed__)) vmcb_control_area {
 	u32 event_inj;
 	u32 event_inj_err;
 	u64 nested_cr3;
-	u64 lbr_ctl;
+	u64 virt_ext;
 	u32 clean;
 	u32 reserved_5;
 	u64 next_rip;
@@ -360,6 +360,8 @@ struct __attribute__ ((__packed__)) vmcb {
 
 #define MSR_BITMAP_SIZE 8192
 
+#define LBR_CTL_ENABLE_MASK BIT_ULL(0)
+
 struct svm_test {
 	const char *name;
 	bool (*supported)(void);
@@ -405,6 +407,7 @@ u64 *npt_get_pml4e(void);
 bool smp_supported(void);
 bool default_supported(void);
 bool vgif_supported(void);
+bool lbrv_supported(void);
 void default_prepare(struct svm_test *test);
 void default_prepare_gif_clear(struct svm_test *test);
 bool default_finished(struct svm_test *test);
x86/svm_tests.c +239 -0

250 lines not on this page · raw message

Signature · 1 line
2.26.3
#7 lore raw

PATCH 7/9 svm: add tests for case when L1 intercepts various hardware interrupts

In reply to Maxim Levitsky

(an interrupt, SMI, NMI), but lets L2 control either EFLAG.IF or GIF

---
 x86/svm.h       |  11 +++
 x86/svm_tests.c | 194 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 205 insertions(+)
x86/svm.h +11 -0
index 58b9410..df1b1ac 100644
@@ -426,6 +426,17 @@ void test_set_guest(test_guest_func func);
 extern struct vmcb *vmcb;
 extern struct svm_test svm_tests[];
 
+static inline void stgi(void)
+{
+    asm volatile ("stgi");
+}
+
+static inline void clgi(void)
+{
+    asm volatile ("clgi");
+}
+
+
 
 #define SAVE_GPR_C                              \
         "xchg %%rbx, regs+0x8\n\t"              \
x86/svm_tests.c +194 -0

205 lines not on this page · raw message

Signature · 1 line
2.26.3
#8 lore raw

PATCH 8/9 svm: add test for nested tsc scaling

In reply to Maxim Levitsky
---
 lib/x86/msr.h       |  1 +
 lib/x86/processor.h |  2 ++
 x86/svm.c           |  5 ++++
 x86/svm.h           |  3 +++
 x86/svm_tests.c     | 65 +++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 76 insertions(+)
lib/x86/msr.h +1 -0
index 5001b16..fa1c0c8 100644
@@ -431,6 +431,7 @@
 
 /* AMD-V MSRs */
 
+#define MSR_AMD64_TSC_RATIO             0xc0000104
 #define MSR_VM_CR                       0xc0010114
 #define MSR_VM_IGNNE                    0xc0010115
 #define MSR_VM_HSAVE_PA                 0xc0010117
lib/x86/processor.h +2 -0
index b01c3d0..b3fe924 100644
@@ -189,9 +189,11 @@ static inline bool is_intel(void)
 #define	X86_FEATURE_NPT			(CPUID(0x8000000A, 0, EDX, 0))
 #define	X86_FEATURE_LBRV		(CPUID(0x8000000A, 0, EDX, 1))
 #define	X86_FEATURE_NRIPS		(CPUID(0x8000000A, 0, EDX, 3))
+#define X86_FEATURE_TSCRATEMSR  (CPUID(0x8000000A, 0, EDX, 4))
 #define	X86_FEATURE_VGIF		(CPUID(0x8000000A, 0, EDX, 16))
 
 
+
 static inline bool this_cpu_has(u64 feature)
 {
 	u32 input_eax = feature >> 32;
x86/svm.c +5 -0
index bb58d7c..460fc59 100644
@@ -75,6 +75,11 @@ bool lbrv_supported(void)
     return this_cpu_has(X86_FEATURE_LBRV);
 }
 
+bool tsc_scale_supported(void)
+{
+    return this_cpu_has(X86_FEATURE_TSCRATEMSR);
+}
+
 void default_prepare(struct svm_test *test)
 {
 	vmcb_ident(vmcb);
x86/svm.h +3 -0
index df1b1ac..d92c4f2 100644
@@ -147,6 +147,8 @@ struct __attribute__ ((__packed__)) vmcb_control_area {
 #define SVM_VM_CR_SVM_LOCK_MASK 0x0008ULL
 #define SVM_VM_CR_SVM_DIS_MASK  0x0010ULL
 
+#define TSC_RATIO_DEFAULT   0x0100000000ULL
+
 struct __attribute__ ((__packed__)) vmcb_seg {
 	u16 selector;
 	u16 attrib;
@@ -408,6 +410,7 @@ bool smp_supported(void);
 bool default_supported(void);
 bool vgif_supported(void);
 bool lbrv_supported(void);
+bool tsc_scale_supported(void);
 void default_prepare(struct svm_test *test);
 void default_prepare_gif_clear(struct svm_test *test);
 bool default_finished(struct svm_test *test);
x86/svm_tests.c +65 -0

76 lines not on this page · raw message

Signature · 1 line
2.26.3
#9 lore raw

PATCH 9/9 svm: add test for pause filter and threshold

In reply to Maxim Levitsky
---
 lib/x86/processor.h |  3 +-
 x86/svm.c           | 11 +++++++
 x86/svm.h           |  5 +++-
 x86/svm_tests.c     | 70 +++++++++++++++++++++++++++++++++++++++++++++
 x86/unittests.cfg   |  7 ++++-
 5 files changed, 93 insertions(+), 3 deletions(-)
lib/x86/processor.h +2 -1
index b3fe924..9a0dad6 100644
@@ -190,10 +190,11 @@ static inline bool is_intel(void)
 #define	X86_FEATURE_LBRV		(CPUID(0x8000000A, 0, EDX, 1))
 #define	X86_FEATURE_NRIPS		(CPUID(0x8000000A, 0, EDX, 3))
 #define X86_FEATURE_TSCRATEMSR  (CPUID(0x8000000A, 0, EDX, 4))
+#define X86_FEATURE_PAUSEFILTER     (CPUID(0x8000000A, 0, EDX, 10))
+#define X86_FEATURE_PFTHRESHOLD     (CPUID(0x8000000A, 0, EDX, 12))
 #define	X86_FEATURE_VGIF		(CPUID(0x8000000A, 0, EDX, 16))
 
 
-
 static inline bool this_cpu_has(u64 feature)
 {
 	u32 input_eax = feature >> 32;
x86/svm.c +11 -0
index 460fc59..f6896f0 100644
@@ -80,6 +80,17 @@ bool tsc_scale_supported(void)
     return this_cpu_has(X86_FEATURE_TSCRATEMSR);
 }
 
+bool pause_filter_supported(void)
+{
+    return this_cpu_has(X86_FEATURE_PAUSEFILTER);
+}
+
+bool pause_threshold_supported(void)
+{
+    return this_cpu_has(X86_FEATURE_PFTHRESHOLD);
+}
+
+
 void default_prepare(struct svm_test *test)
 {
 	vmcb_ident(vmcb);
x86/svm.h +4 -1
index d92c4f2..e93822b 100644
@@ -75,7 +75,8 @@ struct __attribute__ ((__packed__)) vmcb_control_area {
 	u16 intercept_dr_write;
 	u32 intercept_exceptions;
 	u64 intercept;
-	u8 reserved_1[42];
+	u8 reserved_1[40];
+	u16 pause_filter_thresh;
 	u16 pause_filter_count;
 	u64 iopm_base_pa;
 	u64 msrpm_base_pa;
@@ -411,6 +412,8 @@ bool default_supported(void);
 bool vgif_supported(void);
 bool lbrv_supported(void);
 bool tsc_scale_supported(void);
+bool pause_filter_supported(void);
+bool pause_threshold_supported(void);
 void default_prepare(struct svm_test *test);
 void default_prepare_gif_clear(struct svm_test *test);
 bool default_finished(struct svm_test *test);
x86/svm_tests.c +70 -0

81 lines not on this page · raw message

x86/unittests.cfg +6 -1
index 89ff949..c277088 100644
@@ -238,7 +238,12 @@ arch = x86_64
 [svm]
 file = svm.flat
 smp = 2
-extra_params = -cpu max,+svm -m 4g
+extra_params = -cpu max,+svm -m 4g -append "-pause_filter_test"
+arch = x86_64
+
+[svm_pause_filter]
+file = svm.flat
+extra_params = -cpu max,+svm -overcommit cpu-pm=on -m 4g -append pause_filter_test
 arch = x86_64
 
 [taskswitch]
Signature · 1 line
2.26.3
#10 lore raw
In reply to Maxim Levitsky
Quoted 18 lines · Maxim Levitsky

Those are few fixes and unit tests I used to test svm optional features and few more svm feaures.

Best regards,
Maxim Levitsky

Maxim Levitsky (9):
pmu_lbr: few fixes
svm: Fix reg_corruption test, to avoid timer interrupt firing in later
tests.
svm: NMI is an "exception" and not interrupt in x86 land
svm: intercept shutdown in all svm tests by default
svm: add SVM_BARE_VMRUN
svm: add tests for LBR virtualization
svm: add tests for case when L1 intercepts various hardware interrupts
svm: add test for nested tsc scaling
svm: add test for pause filter and threshold

Quoted 13 lines
 lib/x86/msr.h       |   1 +
 lib/x86/processor.h |   4 +
 x86/pmu_lbr.c       |   6 +
 x86/svm.c           |  57 ++---
 x86/svm.h           |  71 +++++-
 x86/svm_tests.c     | 582 +++++++++++++++++++++++++++++++++++++++++++-
 x86/unittests.cfg   |   8 +-
 7 files changed, 688 insertions(+), 41 deletions(-)
Signature · 1 line
2.26.3

typo: I mean SVM optional features.

Best regards,
Maxim Levitsky