Skip to contentSkip to conversation

x86: apic: add physical broadcast test

PATCH In review Radim Krčmář 2 participants 6 messages Dec 19, 2016 – Dec 22, 2016 KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86: apic: add physical broadcast test

Broadcast to address 0xff is not exercised by seabios+linux so we better test it.

---
The bounded wait loop is bad ... time to introduce TSC magic?

---
 x86/apic.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)
x86/apic.c +46 -0
index 39c7fd19913d..b25ba45e4b8f 100644
@@ -5,6 +5,7 @@
 #include "desc.h"
 #include "isr.h"
 #include "msr.h"
+#include "atomic.h"
 
 static void test_lapic_existence(void)
 {
@@ -16,6 +17,7 @@ static void test_lapic_existence(void)
 }
 
 #define TSC_DEADLINE_TIMER_VECTOR 0xef
+#define BROADCAST_VECTOR 0xcf
 
 static int tdt_count;
 
@@ -411,6 +413,49 @@ static void test_apic_timer_one_shot(void)
            (tsc2 - tsc1 >= interval));
 }
 
+static atomic_t broadcast_counter;
+
+static void broadcast_handler(isr_regs_t *regs)
+{
+	atomic_inc(&broadcast_counter);
+	eoi();
+}
+
+static bool broadcast_received(unsigned ncpus)
+{
+	unsigned counter;
+
+	for (int i = 123456789; i; i--) {
+		counter = atomic_read(&broadcast_counter);
+		if (counter >= ncpus)
+			break;
+		pause();
+	}
+
+	atomic_set(&broadcast_counter, 0);
+
+	return counter == ncpus;
+}
+
+static void test_physical_broadcast(void)
+{
+	unsigned ncpus = cpu_count();
+	unsigned long cr3 = read_cr3();
+	u32 broadcast_address = enable_x2apic() ? 0xffffffff : 0xff;
+
+	handle_irq(BROADCAST_VECTOR, broadcast_handler);
+	for (int c = 1; c < ncpus; c++)
+		on_cpu(c, update_cr3, (void *)cr3);
+
+	apic_icr_write(APIC_DEST_PHYSICAL | APIC_DM_FIXED | APIC_INT_ASSERT |
+			BROADCAST_VECTOR, broadcast_address);
+	report("APIC physical broadcast address", broadcast_received(ncpus));
+
+	apic_icr_write(APIC_DEST_PHYSICAL | APIC_DM_FIXED | APIC_INT_ASSERT |
+			BROADCAST_VECTOR | APIC_DEST_ALLINC, 0);
+	report("APIC physical broadcast shorthand", broadcast_received(ncpus));
+}
+
 int main()
 {
     setup_vm();
@@ -425,6 +470,7 @@ int main()
     test_apicbase();
 
     test_self_ipi();
+    test_physical_broadcast();
 
     test_sti_nmi();
     test_multiple_nmi();
Signature · 1 line
2.11.0
maintainer
#1 lore raw
Quoted 6 lines · Radim Krčmář

Broadcast to address 0xff is not exercised by seabios+linux so we better test it.

---
The bounded wait loop is bad ... time to introduce TSC magic?

Yeah, the VMX tests already use the TSC (though in the other direction:
they check that _at least_ some time passed). Roughly speaking, 1 billion TSC units should be plenty, even on a loaded hosts.

Paolo

Quoted 82 lines
 x86/apic.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)
x86/apic.c +46 -0
index 39c7fd19913d..b25ba45e4b8f 100644
@@ -5,6 +5,7 @@
 #include "desc.h"
 #include "isr.h"
 #include "msr.h"
+#include "atomic.h"
 
 static void test_lapic_existence(void)
 {
@@ -16,6 +17,7 @@ static void test_lapic_existence(void)
 }
 
 #define TSC_DEADLINE_TIMER_VECTOR 0xef
+#define BROADCAST_VECTOR 0xcf
 
 static int tdt_count;
 
@@ -411,6 +413,49 @@ static void test_apic_timer_one_shot(void)
            (tsc2 - tsc1 >= interval));
 }
 
+static atomic_t broadcast_counter;
+
+static void broadcast_handler(isr_regs_t *regs)
+{
+	atomic_inc(&broadcast_counter);
+	eoi();
+}
+
+static bool broadcast_received(unsigned ncpus)
+{
+	unsigned counter;
+
+	for (int i = 123456789; i; i--) {
+		counter = atomic_read(&broadcast_counter);
+		if (counter >= ncpus)
+			break;
+		pause();
+	}
+
+	atomic_set(&broadcast_counter, 0);
+
+	return counter == ncpus;
+}
+
+static void test_physical_broadcast(void)
+{
+	unsigned ncpus = cpu_count();
+	unsigned long cr3 = read_cr3();
+	u32 broadcast_address = enable_x2apic() ? 0xffffffff : 0xff;
+
+	handle_irq(BROADCAST_VECTOR, broadcast_handler);
+	for (int c = 1; c < ncpus; c++)
+		on_cpu(c, update_cr3, (void *)cr3);
+
+	apic_icr_write(APIC_DEST_PHYSICAL | APIC_DM_FIXED | APIC_INT_ASSERT |
+			BROADCAST_VECTOR, broadcast_address);
+	report("APIC physical broadcast address", broadcast_received(ncpus));
+
+	apic_icr_write(APIC_DEST_PHYSICAL | APIC_DM_FIXED | APIC_INT_ASSERT |
+			BROADCAST_VECTOR | APIC_DEST_ALLINC, 0);
+	report("APIC physical broadcast shorthand", broadcast_received(ncpus));
+}
+
 int main()
 {
     setup_vm();
@@ -425,6 +470,7 @@ int main()
     test_apicbase();
 
     test_self_ipi();
+    test_physical_broadcast();
 
     test_sti_nmi();
     test_multiple_nmi();
maintainer
#3 lore raw
In reply to Radim Krčmář
Quoted 61 lines · Radim Krčmář

Broadcast to address 0xff is not exercised by seabios+linux so we better test it.

---
The bounded wait loop is bad ... time to introduce TSC magic?

---
 x86/apic.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)
x86/apic.c +29 -0
index 39c7fd19913d..b25ba45e4b8f 100644
@@ -5,6 +5,7 @@
 #include "desc.h"
 #include "isr.h"
 #include "msr.h"
+#include "atomic.h"
 
 static void test_lapic_existence(void)
 {
@@ -16,6 +17,7 @@ static void test_lapic_existence(void)
 }
 
 #define TSC_DEADLINE_TIMER_VECTOR 0xef
+#define BROADCAST_VECTOR 0xcf
 
 static int tdt_count;
 
@@ -411,6 +413,49 @@ static void test_apic_timer_one_shot(void)
            (tsc2 - tsc1 >= interval));
 }
 
+static atomic_t broadcast_counter;
+
+static void broadcast_handler(isr_regs_t *regs)
+{
+	atomic_inc(&broadcast_counter);
+	eoi();
+}
+
+static bool broadcast_received(unsigned ncpus)
+{
+	unsigned counter;
+
+	for (int i = 123456789; i; i--) {
+		counter = atomic_read(&broadcast_counter);
+		if (counter >= ncpus)
+			break;
+		pause();
+	}
+
+	atomic_set(&broadcast_counter, 0);
+
+	return counter == ncpus;
+}
+
+static void test_physical_broadcast(void)
+{
+	unsigned ncpus = cpu_count();

x86/apic.c static void test_apic_timer_one_shot(void) · line 452

Quoted 10 lines
x86/apic.c +10 -0
+	unsigned long cr3 = read_cr3();
+	u32 broadcast_address = enable_x2apic() ? 0xffffffff : 0xff;
+
+	handle_irq(BROADCAST_VECTOR, broadcast_handler);
+	for (int c = 1; c < ncpus; c++)
+		on_cpu(c, update_cr3, (void *)cr3);
+
+	apic_icr_write(APIC_DEST_PHYSICAL | APIC_DM_FIXED | APIC_INT_ASSERT |
+			BROADCAST_VECTOR, broadcast_address);
+	report("APIC physical broadcast address", broadcast_received(ncpus));

This test is broken by

commit f11e8b051ef613670f9db77a79726534dbc667ec
Author: Radim Krčmář <[email protected]>
Date: Tue Dec 13 17:30:00 2016 +0100

KVM: x86: make interrupt delivery fast and slow path behave the same

Slow path tried to prevent IPIs from x2APIC VCPUs from being delivered to xAPIC VCPUs and vice-versa. Make slow path behave like fast path, which never distinguished that.

Fixes 682f732ecf73 ("KVM: x86: bump MAX_VCPUS to 288")

when run with "./x86/run x86/apic.flat -smp 2 -cpu Nehalem,-x2apic", so I'm removing that series from kvm/queue.

Paolo

#4 lore raw

2016-12-22 11:45+0100, Paolo Bonzini:

Quoted 19 lines · Radim Krčmář
Quoted 5 lines

Broadcast to address 0xff is not exercised by seabios+linux so we better test it.

---

This test is broken by

commit f11e8b051ef613670f9db77a79726534dbc667ec
Author: Radim Krčmář <[email protected]>
Date: Tue Dec 13 17:30:00 2016 +0100

KVM: x86: make interrupt delivery fast and slow path behave the same

Slow path tried to prevent IPIs from x2APIC VCPUs from being delivered to xAPIC VCPUs and vice-versa. Make slow path behave like fast path, which never distinguished that.

Quoted 6 lines
Fixes 682f732ecf73 ("KVM: x86: bump MAX_VCPUS to 288")

when run with "./x86/run x86/apic.flat -smp 2 -cpu Nehalem,-x2apic", so I'm removing that series from kvm/queue.

Yes, kvm/queue had v2 of the series and I have added the test because David noticed the broadcast bug while reviewing v3 of that series.
(http://www.spinics.net/lists/kvm/msg142614.html)

The bug was fixed in v4. I have only posted v4 of this one patch as the rest seemed ok in v3.

#2 lore raw
In reply to Paolo Bonzini

2016-12-22 11:02+0100, Paolo Bonzini:

Quoted 11 lines · Radim Krčmář
Quoted 6 lines

Broadcast to address 0xff is not exercised by seabios+linux so we better test it.

---
The bounded wait loop is bad ... time to introduce TSC magic?

Yeah, the VMX tests already use the TSC (though in the other direction:
they check that _at least_ some time passed). Roughly speaking, 1 billion TSC units should be plenty, even on a loaded hosts.

I will convert it, thanks.

maintainer
#5 lore raw
In reply to Radim Krčmář
Quoted 27 lines · Radim Krčmář

2016-12-22 11:45+0100, Paolo Bonzini:

Quoted 19 lines · Radim Krčmář
Quoted 5 lines

Broadcast to address 0xff is not exercised by seabios+linux so we better test it.

---

This test is broken by

commit f11e8b051ef613670f9db77a79726534dbc667ec
Author: Radim Krčmář <[email protected]>
Date: Tue Dec 13 17:30:00 2016 +0100

KVM: x86: make interrupt delivery fast and slow path behave the same

Slow path tried to prevent IPIs from x2APIC VCPUs from being delivered to xAPIC VCPUs and vice-versa. Make slow path behave like fast path, which never distinguished that.

Quoted 6 lines
Fixes 682f732ecf73 ("KVM: x86: bump MAX_VCPUS to 288")

when run with "./x86/run x86/apic.flat -smp 2 -cpu Nehalem,-x2apic", so I'm removing that series from kvm/queue.

Quoted 6 lines

Yes, kvm/queue had v2 of the series and I have added the test because David noticed the broadcast bug while reviewing v3 of that series.
(http://www.spinics.net/lists/kvm/msg142614.html)

The bug was fixed in v4. I have only posted v4 of this one patch as the rest seemed ok in v3.

Oh, okay, then I'll recheck and fix it.

Currently I'm seeing WinXP/2003 failures with apicv=0 and Win2008r2 failures with apicv=0 ept=0. I guess I'll have to have some fun bisecting.

Paolo