mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Josh Hilke <jrhilke@google.com>
To: Paolo Bonzini <pbonzini@redhat.com>,
	Sean Christopherson <seanjc@google.com>
Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
	 David Matlack <dmatlack@google.com>,
	Alex Williamson <alex.williamson@redhat.com>,
	 Josh Hilke <jrhilke@google.com>
Subject: [PATCH v5 21/21] KVM: selftests: Add xAPIC support in IRQ test
Date: Thu,  4 Jun 2026 02:01:43 +0000	[thread overview]
Message-ID: <20260604020143.748245-22-jrhilke@google.com> (raw)
In-Reply-To: <20260604020143.748245-1-jrhilke@google.com>

From: David Matlack <dmatlack@google.com>

Add the '-x' flag to tools/testing/selftests/kvm/irq_test.c to configure
the guest to use xAPIC mode instead of the default x2APIC mode. This
expands test coverage to ensure that IRQ injection and routing work
correctly when the guest uses the older xAPIC interface.

Co-developed-by: Josh Hilke <jrhilke@google.com>
Signed-off-by: Josh Hilke <jrhilke@google.com>
Signed-off-by: David Matlack <dmatlack@google.com>
---
 tools/testing/selftests/kvm/irq_test.c | 28 +++++++++++++++++++++-----
 1 file changed, 23 insertions(+), 5 deletions(-)

diff --git a/tools/testing/selftests/kvm/irq_test.c b/tools/testing/selftests/kvm/irq_test.c
index 41c50caa928e..7e421cb7bc69 100644
--- a/tools/testing/selftests/kvm/irq_test.c
+++ b/tools/testing/selftests/kvm/irq_test.c
@@ -20,6 +20,7 @@ static bool guest_received_irq[KVM_MAX_VCPUS];
 static bool guest_received_nmi[KVM_MAX_VCPUS];
 static pid_t vcpu_tids[KVM_MAX_VCPUS];
 static bool migrate_vcpus;
+static bool x2apic = true;
 static bool irq_affinity;
 static bool block_vcpus;
 static bool done;
@@ -31,14 +32,20 @@ static bool done;
 
 static u32 guest_get_vcpu_id(void)
 {
-	return x2apic_read_reg(APIC_ID);
+	if (x2apic)
+		return x2apic_read_reg(APIC_ID);
+	else
+		return xapic_read_reg(APIC_ID) >> 24;
 }
 
 static void guest_irq_handler(struct ex_regs *regs)
 {
 	WRITE_ONCE(guest_received_irq[guest_get_vcpu_id()], true);
 
-	x2apic_write_reg(APIC_EOI, 0);
+	if (x2apic)
+		x2apic_write_reg(APIC_EOI, 0);
+	else
+		xapic_write_reg(APIC_EOI, 0);
 }
 
 static void guest_nmi_handler(struct ex_regs *regs)
@@ -48,7 +55,10 @@ static void guest_nmi_handler(struct ex_regs *regs)
 
 static void guest_code(void)
 {
-	x2apic_enable();
+	if (x2apic)
+		x2apic_enable();
+	else
+		xapic_enable();
 
 	sti_nop();
 
@@ -144,7 +154,7 @@ static void kvm_clear_gsi_routes(struct kvm_vm *vm)
 
 static void help(const char *name)
 {
-	printf("Usage: %s [-a] [-b] [-c] [-d <segment:bus:device.function>] [-h] [-i nr_irqs] [-m] [-n] [-v nr_vcpus]\n", name);
+	printf("Usage: %s [-a] [-b] [-c] [-d <segment:bus:device.function>] [-h] [-i nr_irqs] [-m] [-n] [-v nr_vcpus] [-x]\n", name);
 	printf("\n");
 	printf("Tests KVM IRQ injection via irqfd using an emulated eventfd.\n");
 	printf("-a	Randomly affinitize the device's host IRQ to different physical CPUs throughout the test\n");
@@ -155,6 +165,7 @@ static void help(const char *name)
 	printf("-m	Pin vCPU threads to random physical CPUs throughout the test\n");
 	printf("-n	Deliver 50 percent of IRQs as non-maskable interrupts\n");
 	printf("-v	Number of vCPUS to run\n");
+	printf("-x	Use xAPIC mode instead of x2APIC mode in the guest\n");
 	printf("\n");
 	exit(KSFT_FAIL);
 }
@@ -191,7 +202,7 @@ int main(int argc, char **argv)
 	unsigned int irq;
 	int irq_cpu;
 
-	while ((c = getopt(argc, argv, "abcd:hi:mnv:")) != -1) {
+	while ((c = getopt(argc, argv, "abcd:hi:mnv:x")) != -1) {
 		switch (c) {
 		case 'a':
 			irq_affinity = true;
@@ -217,6 +228,9 @@ int main(int argc, char **argv)
 		case 'v':
 			nr_vcpus = atoi_positive("Number of vCPUS", optarg);
 			break;
+		case 'x':
+			x2apic = false;
+			break;
 		case 'h':
 		default:
 			help(argv[0]);
@@ -229,6 +243,9 @@ int main(int argc, char **argv)
 	vm_install_exception_handler(vm, vector, guest_irq_handler);
 	vm_install_exception_handler(vm, NMI_VECTOR, guest_nmi_handler);
 
+	if (!x2apic)
+		virt_pg_map(vm, APIC_DEFAULT_GPA, APIC_DEFAULT_GPA);
+
 	if (device_bdf) {
 		iommu = iommu_init(default_iommu_mode);
 		device = vfio_pci_device_init(device_bdf, iommu);
@@ -250,6 +267,7 @@ int main(int argc, char **argv)
 
 	kvm_assign_irqfd(vm, gsi, eventfd);
 
+	sync_global_to_guest(vm, x2apic);
 	sync_global_to_guest(vm, block_vcpus);
 
 	for (i = 0; i < nr_vcpus; i++)
-- 
2.54.0.1032.g2f8565e1d1-goog


  parent reply	other threads:[~2026-06-04  2:02 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-04  2:01 [PATCH v5 00/21] KVM: selftests: Link with VFIO selftests lib and test device interrupts Josh Hilke
2026-06-04  2:01 ` [PATCH v5 01/21] KVM: selftests: Build and link selftests/vfio/lib into KVM selftests Josh Hilke
2026-06-04  2:01 ` [PATCH v5 02/21] KVM: selftests: Add guest read/write macros Josh Hilke
2026-06-04  2:01 ` [PATCH v5 03/21] KVM: selftests: Rename guest_rng to kvm_rng Josh Hilke
2026-06-04  2:01 ` [PATCH v5 04/21] KVM: selftests: Add helper to generate random u64 in range [min,max] Josh Hilke
2026-06-04  2:01 ` [PATCH v5 05/21] KVM: selftests: Add IRQ injection test Josh Hilke
2026-06-04 16:19   ` Sean Christopherson
2026-06-04 16:26     ` Sean Christopherson
2026-06-04  2:01 ` [PATCH v5 06/21] KVM: selftests: Add helper to get host IRQ from device MSIX for IRQ bypass test Josh Hilke
2026-06-04  2:01 ` [PATCH v5 07/21] KVM: selftests: Verify IRQ bypass works in IRQ test Josh Hilke
2026-06-04 16:22   ` Sean Christopherson
2026-06-04 17:56   ` Sean Christopherson
2026-06-04 19:25     ` Sean Christopherson
2026-06-04 19:52       ` Sean Christopherson
2026-06-04 23:14       ` Sean Christopherson
2026-06-04 23:35         ` David Matlack
2026-06-04  2:01 ` [PATCH v5 08/21] KVM: selftests: Add helpers to write proc IRQ affinity for " Josh Hilke
2026-06-04 19:35   ` Sean Christopherson
2026-06-04  2:01 ` [PATCH v5 09/21] KVM: selftests: Add helpers to print " Josh Hilke
2026-06-04  2:01 ` [PATCH v5 10/21] KVM: selftests: Verify interrupts are received when IRQ affinity changes in " Josh Hilke
2026-06-04  2:01 ` [PATCH v5 11/21] KVM: selftests: Verify IRQs wake up halted vCPUs " Josh Hilke
2026-06-04  2:01 ` [PATCH v5 12/21] KVM: selftests: Verify interrupts are received after modifying IRQ routes " Josh Hilke
2026-06-04 17:22   ` Sean Christopherson
2026-06-04  2:01 ` [PATCH v5 13/21] KVM: selftests: Make number of IRQs configurable " Josh Hilke
2026-06-04 17:35   ` Sean Christopherson
2026-06-04  2:01 ` [PATCH v5 14/21] KVM: selftests: Verify non-postable IRQ remapping " Josh Hilke
2026-06-04 17:22   ` Sean Christopherson
2026-06-04  2:01 ` [PATCH v5 15/21] KVM: selftests: Add kvm_gettid() wrapper and convert users Josh Hilke
2026-06-04  2:01 ` [PATCH v5 16/21] KVM: selftests: Add kvm_sched_getaffinity() " Josh Hilke
2026-06-04 17:23   ` Sean Christopherson
2026-06-04  2:01 ` [PATCH v5 17/21] KVM: selftests: Add pin_task_to_random_cpu() helper function for IRQ test Josh Hilke
2026-06-04  2:01 ` [PATCH v5 18/21] KVM: selftests: Verify vCPU migration during IRQ delivery in " Josh Hilke
2026-06-04 17:27   ` Sean Christopherson
2026-06-04  2:01 ` [PATCH v5 19/21] KVM: selftests: Print vCPU affinity on timeout during " Josh Hilke
2026-06-04  2:01 ` [PATCH v5 20/21] KVM: selftests: Make number of vCPUs configurable in " Josh Hilke
2026-06-04  2:01 ` Josh Hilke [this message]
2026-06-04 20:22 ` [PATCH v5 00/21] KVM: selftests: Link with VFIO selftests lib and test device interrupts Sean Christopherson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260604020143.748245-22-jrhilke@google.com \
    --to=jrhilke@google.com \
    --cc=alex.williamson@redhat.com \
    --cc=dmatlack@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome