Skip to contentSkip to conversation

Test nmi-after-sti

PATCH New Avi Kivity 1 participant 3 messages Nov 1, 2010 KVM

lore
Share & tools
lore

Avi Kivity

author
#0 lore raw

cover Test nmi-after-sti

Add a unit test for NMI immediately after an IF-enabling STI.

Avi Kivity (2):
apic: use boot idt instead of a locally allocated idt
apic: test nmi-after-sti

 x86/apic.c |   83 +++++++++++++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 68 insertions(+), 15 deletions(-)
Signature · 1 line
1.7.3.1
author
#1 lore raw

PATCH 1/2 apic: use boot idt instead of a locally allocated idt

This allows the smp support, which uses the boot idt, to work.

---
 x86/apic.c |   16 +---------------
 1 files changed, 1 insertions(+), 15 deletions(-)
x86/apic.c +1 -15
index 48fa0f7..165f820 100644
@@ -89,7 +89,7 @@ asm (
 #endif
     );
 
-static idt_entry_t idt[256];
+static idt_entry_t *idt = 0;
 
 static int g_fail;
 static int g_tests;
@@ -127,19 +127,6 @@ void test_enable_x2apic(void)
     }
 }
 
-static void init_idt(void)
-{
-    struct {
-        u16 limit;
-        ulong idt;
-    } __attribute__((packed)) idt_ptr = {
-        sizeof(idt_entry_t) * 256 - 1,
-        (ulong)&idt,
-    };
-
-    asm volatile("lidt %0" : : "m"(idt_ptr));
-}
-
 static void set_idt_entry(unsigned vec, void (*func)(isr_regs_t *regs))
 {
     u8 *thunk = vmalloc(50);
@@ -296,7 +283,6 @@ int main()
     mask_pic_interrupts();
     enable_apic();
     test_enable_x2apic();
-    init_idt();
 
     test_self_ipi();
 
Signature · 1 line
1.7.3.1
author
#2 lore raw

PATCH 2/2 apic: test nmi-after-sti

In reply to Avi Kivity

While not required by the spec, some guests (Linux) rely on nmi being blocked by an IF-enabling sti. Add a unit test for this condition.

---
 x86/apic.c |   67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 67 insertions(+), 0 deletions(-)
x86/apic.c +67 -0
index 165f820..2207040 100644
@@ -1,6 +1,7 @@
 #include "libcflat.h"
 #include "apic.h"
 #include "vm.h"
+#include "smp.h"
 
 typedef struct {
     unsigned short offset0;
@@ -274,9 +275,74 @@ static void test_ioapic_simultaneous(void)
            g_66 && g_78 && g_66_after_78 && g_66_rip == g_78_rip);
 }
 
+volatile int nmi_counter_private, nmi_counter, nmi_hlt_counter, sti_loop_active;
+
+void sti_nop(char *p)
+{
+    asm volatile (
+		  ".globl post_sti \n\t"
+		  "sti \n"
+		  /*
+		   * vmx won't exit on external interrupt if blocked-by-sti,
+		   * so give it a reason to exit by accessing an unmapped page.
+		   */
+		  "post_sti: testb $0, %0 \n\t"
+		  "nop \n\t"
+		  "cli"
+		  : : "m"(*p)
+		  );
+    nmi_counter = nmi_counter_private;
+}
+
+static void sti_loop(void *ignore)
+{
+    unsigned k = 0;
+
+    while (sti_loop_active) {
+	sti_nop((char *)(ulong)((k++ * 4096) % (128 * 1024 * 1024)));
+    }
+}
+
+static void nmi_handler(isr_regs_t *regs)
+{
+    extern void post_sti(void);
+    ++nmi_counter_private;
+    nmi_hlt_counter += regs->rip == (ulong)post_sti;
+}
+
+static void update_cr3(void *cr3)
+{
+    write_cr3((ulong)cr3);
+}
+
+static void test_sti_nmi(void)
+{
+    unsigned old_counter;
+
+    if (cpu_count() < 2) {
+	return;
+    }
+
+    set_idt_entry(2, nmi_handler);
+    on_cpu(1, update_cr3, (void *)read_cr3());
+
+    sti_loop_active = 1;
+    on_cpu_async(1, sti_loop, 0);
+    while (nmi_counter < 30000) {
+	old_counter = nmi_counter;
+	apic_icr_write(APIC_DEST_PHYSICAL | APIC_DM_NMI | APIC_INT_ASSERT, 1);
+	while (nmi_counter == old_counter) {
+	    ;
+	}
+    }
+    sti_loop_active = 0;
+    report("nmi-after-sti", nmi_hlt_counter == 0);
+}
+
 int main()
 {
     setup_vm();
+    smp_init();
 
     test_lapic_existence();
 
@@ -288,6 +354,7 @@ int main()
 
     test_ioapic_intr();
     test_ioapic_simultaneous();
+    test_sti_nmi();
 
     printf("\nsummary: %d tests, %d failures\n", g_tests, g_fail);
 
Signature · 1 line
1.7.3.1