Skip to contentSkip to conversation

svm: more interrupt injection tests

PATCH New Paolo Bonzini 1 participant 3 messages Mar 5, 2020 KVM

lore
Share & tools
lore

Paolo Bonzini

author maintainer
#0 lore raw

cover svm: more interrupt injection tests

Patch 1 is just a cleanup. Patch 2 adds more coverage of interrupt injection, including running HLT in L2 when L1 does not intercept it.

Paolo

Cathy Avery (1):
svm: Add test cases around interrupt injection and halting

Paolo Bonzini (1):
svm: rename and comment the pending_event_vmask test

 x86/svm.c | 166 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 157 insertions(+), 9 deletions(-)
Signature · 1 line
1.8.3.1
author maintainer
#1 lore raw

PATCH 1/2 svm: rename and comment the pending_event_vmask test

Both the pending_event and pending_event_vmask test are using the V_INTR_MASKING field. The difference is that pending_event_vmask runs with host IF cleared, and therefore does not expect INTR vmexits. Rename the test to clarify this, and add comments to explain what's going on.

---
 x86/svm.c | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)
x86/svm.c +16 -9
index 70e5169..f300c8a 100644
@@ -1400,7 +1400,7 @@ static bool pending_event_check(struct test *test)
     return get_test_stage(test) == 2;
 }
 
-static void pending_event_prepare_vmask(struct test *test)
+static void pending_event_cli_prepare(struct test *test)
 {
     default_prepare(test);
 
@@ -1414,12 +1414,12 @@ static void pending_event_prepare_vmask(struct test *test)
     set_test_stage(test, 0);
 }
 
-static void pending_event_prepare_gif_clear_vmask(struct test *test)
+static void pending_event_cli_prepare_gif_clear(struct test *test)
 {
     asm("cli");
 }
 
-static void pending_event_test_vmask(struct test *test)
+static void pending_event_cli_test(struct test *test)
 {
     if (pending_event_ipi_fired == true) {
         set_test_stage(test, -1);
@@ -1427,6 +1427,7 @@ static void pending_event_test_vmask(struct test *test)
         vmmcall();
     }
 
+    /* VINTR_MASKING is zero.  This should cause the IPI to fire.  */
     irq_enable();
     asm volatile ("nop");
     irq_disable();
@@ -1438,12 +1439,17 @@ static void pending_event_test_vmask(struct test *test)
 
     vmmcall();
 
+    /*
+     * Now VINTR_MASKING=1, but no interrupt is pending so
+     * the VINTR interception should be clear in VMCB02.  Check
+     * that L0 did not leave a stale VINTR in the VMCB.
+     */
     irq_enable();
     asm volatile ("nop");
     irq_disable();
 }
 
-static bool pending_event_finished_vmask(struct test *test)
+static bool pending_event_cli_finished(struct test *test)
 {
     if ( test->vmcb->control.exit_code != SVM_EXIT_VMMCALL) {
         report(false, "VM_EXIT return to host is not EXIT_VMMCALL exit reason 0x%x",
@@ -1459,6 +1465,7 @@ static bool pending_event_finished_vmask(struct test *test)
 
         test->vmcb->control.int_ctl |= V_INTR_MASKING_MASK;
 
+	/* Now entering again with VINTR_MASKING=1.  */
         apic_icr_write(APIC_DEST_SELF | APIC_DEST_PHYSICAL |
               APIC_DM_FIXED | 0xf1, 0);
 
@@ -1490,7 +1497,7 @@ static bool pending_event_finished_vmask(struct test *test)
     return get_test_stage(test) == 2;
 }
 
-static bool pending_event_check_vmask(struct test *test)
+static bool pending_event_cli_check(struct test *test)
 {
     return get_test_stage(test) == 2;
 }
@@ -1571,10 +1578,10 @@ static struct test tests[] = {
     { "pending_event", default_supported, pending_event_prepare,
       default_prepare_gif_clear,
       pending_event_test, pending_event_finished, pending_event_check },
-    { "pending_event_vmask", default_supported, pending_event_prepare_vmask,
-      pending_event_prepare_gif_clear_vmask,
-      pending_event_test_vmask, pending_event_finished_vmask,
-      pending_event_check_vmask },
+    { "pending_event_cli", default_supported, pending_event_cli_prepare,
+      pending_event_cli_prepare_gif_clear,
+      pending_event_cli_test, pending_event_cli_finished,
+      pending_event_cli_check },
 };
 
 int matched;
Signature · 1 line
1.8.3.1
author maintainer
#2 lore raw

PATCH 2/2 svm: Add test cases around interrupt injection and halting

In reply to Paolo Bonzini

From: Cathy Avery <[email protected]>

This test checks for interrupt delivery to L2 and unintercepted hlt in L2. All tests are performed both with direct interrupt injection and external interrupt interception.

Based on VMX test by Jan Kiszka <[email protected]>

---
 x86/svm.c | 141 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 141 insertions(+)
x86/svm.c +141 -0
index f300c8a..df7a7c4 100644
@@ -1502,6 +1502,144 @@ static bool pending_event_cli_check(struct test *test)
     return get_test_stage(test) == 2;
 }
 
+#define TIMER_VECTOR    222
+
+static volatile bool timer_fired;
+
+static void timer_isr(isr_regs_t *regs)
+{
+    timer_fired = true;
+    apic_write(APIC_EOI, 0);
+}
+
+static void interrupt_prepare(struct test *test)
+{
+    default_prepare(test);
+    handle_irq(TIMER_VECTOR, timer_isr);
+    timer_fired = false;
+    set_test_stage(test, 0);
+}
+
+static void interrupt_test(struct test *test)
+{
+    long long start, loops;
+
+    apic_write(APIC_LVTT, TIMER_VECTOR);
+    irq_enable();
+    apic_write(APIC_TMICT, 1); //Timer Initial Count Register 0x380 one-shot
+    for (loops = 0; loops < 10000000 && !timer_fired; loops++)
+        asm volatile ("nop");
+
+    report(timer_fired, "direct interrupt while running guest");
+
+    if (!timer_fired) {
+        set_test_stage(test, -1);
+        vmmcall();
+    }
+
+    apic_write(APIC_TMICT, 0);
+    irq_disable();
+    vmmcall();
+
+    timer_fired = false;
+    apic_write(APIC_TMICT, 1);
+    for (loops = 0; loops < 10000000 && !timer_fired; loops++)
+        asm volatile ("nop");
+
+    report(timer_fired, "intercepted interrupt while running guest");
+
+    if (!timer_fired) {
+        set_test_stage(test, -1);
+        vmmcall();
+    }
+
+    irq_enable();
+    apic_write(APIC_TMICT, 0);
+    irq_disable();
+
+    timer_fired = false;
+    start = rdtsc();
+    apic_write(APIC_TMICT, 1000000);
+    asm volatile ("sti; hlt");
+
+    report(rdtsc() - start > 10000 && timer_fired,
+          "direct interrupt + hlt");
+
+    if (!timer_fired) {
+        set_test_stage(test, -1);
+        vmmcall();
+    }
+
+    apic_write(APIC_TMICT, 0);
+    irq_disable();
+    vmmcall();
+
+    timer_fired = false;
+    start = rdtsc();
+    apic_write(APIC_TMICT, 1000000);
+    asm volatile ("hlt");
+
+    report(rdtsc() - start > 10000 && timer_fired,
+           "intercepted interrupt + hlt");
+
+    if (!timer_fired) {
+        set_test_stage(test, -1);
+        vmmcall();
+    }
+
+    apic_write(APIC_TMICT, 0);
+    irq_disable();
+}
+
+static bool interrupt_finished(struct test *test)
+{
+    switch (get_test_stage(test)) {
+    case 0:
+    case 2:
+        if (test->vmcb->control.exit_code != SVM_EXIT_VMMCALL) {
+            report(false, "VMEXIT not due to vmmcall. Exit reason 0x%x",
+                   test->vmcb->control.exit_code);
+            return true;
+        }
+        test->vmcb->save.rip += 3;
+
+        test->vmcb->control.intercept |= (1ULL << INTERCEPT_INTR);
+        test->vmcb->control.int_ctl |= V_INTR_MASKING_MASK;
+        break;
+
+    case 1:
+    case 3:
+        if (test->vmcb->control.exit_code != SVM_EXIT_INTR) {
+            report(false, "VMEXIT not due to intr intercept. Exit reason 0x%x",
+                   test->vmcb->control.exit_code);
+            return true;
+        }
+
+        irq_enable();
+        asm volatile ("nop");
+        irq_disable();
+
+        test->vmcb->control.intercept &= ~(1ULL << INTERCEPT_INTR);
+        test->vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
+        break;
+
+    case 4:
+        break;
+
+    default:
+        return true;
+    }
+
+    inc_test_stage(test);
+
+    return get_test_stage(test) == 5;
+}
+
+static bool interrupt_check(struct test *test)
+{
+    return get_test_stage(test) == 5;
+}
+
 static struct test tests[] = {
     { "null", default_supported, default_prepare,
       default_prepare_gif_clear, null_test,
@@ -1582,6 +1720,9 @@ static struct test tests[] = {
       pending_event_cli_prepare_gif_clear,
       pending_event_cli_test, pending_event_cli_finished,
       pending_event_cli_check },
+    { "interrupt", default_supported, interrupt_prepare,
+      default_prepare_gif_clear, interrupt_test,
+      interrupt_finished, interrupt_check },
 };
 
 int matched;
Signature · 1 line
1.8.3.1