From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 58F8C3002C8 for ; Sat, 30 May 2026 00:23:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780100622; cv=none; b=K6+5xZA97RyYWpErZjFUQRb8dqRVik/1bDycRxCE9/XY+v2dK9PfgT4tMa07abvddCpWi/3F2ZOQiUC5DIo3exqzkPNVKCkx16kuz38grvR4bBgCEc1ySFL10w53ov8vCdNm6rlojgvrBWPuu8S39rHbP2ExrTwhQNxWZEzHd+k= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780100622; c=relaxed/simple; bh=3yeVP6qjqV8GM2bvDNGyj/WO9RKz1xKmC/mcaxsDUX0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ZDpd0lSRncYjyVPmpBH4pHmTISlGEuKKMCM3GFvofS4X+v0xPAGxQoUm2z6KUQPg3vOfKMX9qBymCXK1Qu51FKYmK8rJG9JtgTBfTqjsUmyNfoc0rUCj5YTC5cPSonLlbR1xAdb5mKJcOxIGqHP7PwBpWGRg29J/dODk1NhV2V0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jrhilke.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=gtcmJBoD; arc=none smtp.client-ip=209.85.215.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jrhilke.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="gtcmJBoD" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-c8581f7723aso317192a12.0 for ; Fri, 29 May 2026 17:23:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780100621; x=1780705421; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=dNSvW6+2aoUGl/zEwvlnmm46oR9NmJ5Z6dcbh7LJmwQ=; b=gtcmJBoDn1d70iLDrnkld6+5I8r6c/mbFyfNk6zLm69/truUfLheU4KJTy9kPqzuyz plEisHTduqxdRh9vNqs49WyvK/ilhY5tTNnNtmNVu7FFUvSB9OU9i0xWlm/Otn6PWGU1 5gSqqdDW7a7aMEbcWxRG9dZW+COj6kD2SD2kJJtLB53LerWHodJo4e/jtaH75XMXxHBc RM703HvTBtxJ2KaFy4pEQ4ZA2SDBUrvTaKf9JM7uCRa/osyPIoOxOis4331buFj9Paex 5+fhHWnVZa77zHwWwXpNuX33Q1VgSAUsWUq8zANKePpbS6EbC8N+32qa9VbMJt+j9Rxi tdDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780100621; x=1780705421; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=dNSvW6+2aoUGl/zEwvlnmm46oR9NmJ5Z6dcbh7LJmwQ=; b=lryv13yLawxrkoN1671HtSw3s13olLGVq3I6tQrY0Yvd9ZcSM5yCFu7RGlVk23yBdA 2LaVsUZcQAW4Atboc6E/Yh4jWTNoW+7X59JyXq+cQG3ShYRR/cFfM1wdqDg88JheqynF J1ZXJXzJDZGVNqNt9CB/Cw5uD0ueumOOb2fMxOBtGv49GO3J4d+WBWpoHmc4koWZA1AG i4ndZCof3FanrMINBab79Y22qyCqcdAhmq2JpBN7gFU3jIrgCkWJBVJfzdUYSgxXjIIt XP0Kx+qxNevtT0BnUGIZlXCz7Rqm9IRngKeAPoUWLB+pMDYKBeJ3IWnI+cztSmHGlfPf 5Wng== X-Forwarded-Encrypted: i=1; AFNElJ9BUQDGh5kWpnesHePdeSbhjTLpzfhQ7thQbIEk+/CFVR9QS86xdGnM2i3OqeOZE7FAkYRhCMnWIlJElJw=@vger.kernel.org X-Gm-Message-State: AOJu0YyiDMKtp/pQH/KU2FEwnibS7PBtR2p6ILuEaawo3rktjFk7yhMp csHflYTXpS0toMrfH0JrJQYDLkKEzCcgrG52VNoJettclKLivfQVffMgbPWpdVM0LggWUQ0QLCW 8q5n9+y2c X-Received: from pglc6.prod.google.com ([2002:a63:d06:0:b0:c82:7498:e3b5]) (user=jrhilke job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:ce4e:b0:3a2:d79c:416c with SMTP id adf61e73a8af0-3b427f97563mr1614544637.32.1780100620429; Fri, 29 May 2026 17:23:40 -0700 (PDT) Date: Sat, 30 May 2026 00:21:34 +0000 In-Reply-To: <20260530002134.558837-1-jrhilke@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260530002134.558837-1-jrhilke@google.com> X-Mailer: git-send-email 2.54.0.929.g9b7fa37559-goog Message-ID: <20260530002134.558837-20-jrhilke@google.com> Subject: [PATCH v4 19/19] KVM: selftests: Add xAPIC support in IRQ test From: Josh Hilke To: Paolo Bonzini , Sean Christopherson Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Matlack , Alex Williamson , Josh Hilke Content-Type: text/plain; charset="UTF-8" From: David Matlack 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 Signed-off-by: Josh Hilke Signed-off-by: David Matlack --- 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 967083b8e2ff..29f54a435c8e 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 ] [-h] [-i nr_irqs] [-m] [-n] [-v nr_vcpus]\n", name); + printf("Usage: %s [-a] [-b] [-c] [-d ] [-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); } @@ -188,7 +199,7 @@ int main(int argc, char **argv) struct kvm_vm *vm; cpu_set_t available_cpus; - 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; @@ -214,6 +225,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]); @@ -226,6 +240,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); @@ -248,6 +265,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.929.g9b7fa37559-goog