mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Neeraj Upadhyay <Neeraj.Upadhyay@amd.com>
To: <kvm@vger.kernel.org>, <seanjc@google.com>, <pbonzini@redhat.com>
Cc: <linux-kernel@vger.kernel.org>, <Thomas.Lendacky@amd.com>,
	<nikunj@amd.com>, <Santosh.Shukla@amd.com>,
	<Vasant.Hegde@amd.com>, <Suravee.Suthikulpanit@amd.com>,
	<bp@alien8.de>, <David.Kaplan@amd.com>, <huibo.wang@amd.com>,
	<naveen.rao@amd.com>, <pgonda@google.com>,
	<linux-kselftest@vger.kernel.org>, <shuah@kernel.org>,
	<tiala@microsoft.com>
Subject: [RFC PATCH v2 26/35] KVM: selftests: Add IPI handling support for Secure AVIC
Date: Tue, 23 Sep 2025 10:39:33 +0530	[thread overview]
Message-ID: <20250923050942.206116-27-Neeraj.Upadhyay@amd.com> (raw)
In-Reply-To: <20250923050942.206116-1-Neeraj.Upadhyay@amd.com>

When a guest vCPU has Secure AVIC (SAVIC) enabled, an attempt to send an
Inter-Processor Interrupt (IPI) by writing to the Interrupt Command
Register (APIC_ICR) does not complete in hardware. Instead, it triggers
a #VC exception with the exit code SVM_EXIT_AVIC_INCOMPLETE_IPI. This
design delegates the responsibility of IPI delivery to the guest's #VC
handler.

Implement the necessary #VC handler to process these IPI requests,
enabling multi-vCPU IPI communication for SAVIC guests.

Add support for all IPI modes: physical and logical destination modes,
as well as "broadcast" and "broadcast-but-self" destination shorthands.
Inject the IPI into each target vCPU by directly setting the appropriate
bit in the Interrupt Request Register (IRR) of its backing page. In
addition, propagate the ICR write to the hypervisor via a VMGEXIT. This
is crucial for KVM to perform actions like waking up a halted vCPU.

APIC_ICR based NMI delivery is also supported via a the NMIReq field in
the backing page.

This functionality is a prerequisite for testing IPIs in SAVIC guests.
It allows selftests like xapic_ipi_test to correctly validate IPI delivery
for SAVIC enabled guests.

Signed-off-by: Neeraj Upadhyay <Neeraj.Upadhyay@amd.com>
---
 tools/testing/selftests/kvm/lib/x86/savic.c | 111 ++++++++++++++++++++
 1 file changed, 111 insertions(+)

diff --git a/tools/testing/selftests/kvm/lib/x86/savic.c b/tools/testing/selftests/kvm/lib/x86/savic.c
index 9b1ea5d15338..016e5e9e43f6 100644
--- a/tools/testing/selftests/kvm/lib/x86/savic.c
+++ b/tools/testing/selftests/kvm/lib/x86/savic.c
@@ -42,6 +42,12 @@ enum lapic_lvt_entry {
 #define MSR_AMD64_SECURE_AVIC_ALLOWED_NMI_BIT       1
 
 #define SVM_EXIT_AVIC_UNACCELERATED_ACCESS      0x402
+#define SVM_EXIT_AVIC_INCOMPLETE_IPI            0x401
+
+#define REG_OFF(VEC)		(VEC / 32 * 16)
+#define VEC_POS(VEC)		(VEC % 32)
+
+#define SAVIC_NMI_REQ_OFFSET            0x278
 
 /*
  * Initial pool of guest apic backing page.
@@ -335,6 +341,104 @@ static void handle_savic_unaccel_access(struct ex_regs *regs)
 	}
 }
 
+static void send_ipi(int cpu, int vector, bool nmi)
+{
+	struct guest_apic_page *apic_page;
+
+	apic_page = &apic_page_pool->guest_apic_page[cpu];
+
+	if (nmi)
+		savic_write_reg(apic_page, SAVIC_NMI_REQ_OFFSET, 1);
+	else
+		savic_write_reg(apic_page, APIC_IRR + REG_OFF(vector), BIT(VEC_POS(vector)));
+}
+
+static bool is_cpu_present(int cpu)
+{
+	struct guest_apic_page *apic_page;
+
+	if (cpu >= KVM_MAX_VCPUS)
+		return false;
+
+	apic_page = &apic_page_pool->guest_apic_page[cpu];
+
+	return savic_read_reg(apic_page, APIC_ID) != 0;
+}
+
+static void savic_send_ipi_all_but(int vector, bool nmi)
+{
+	int cpu;
+	int mycpu = x2apic_read_reg(APIC_ID);
+
+	for (cpu = 0; cpu < KVM_MAX_VCPUS; cpu++) {
+		if (cpu == mycpu)
+			continue;
+		if (!(cpu == 0 || is_cpu_present(cpu)))
+			break;
+		send_ipi(cpu, vector, nmi);
+	}
+}
+
+static bool ipi_match_dest(uint32_t dest, bool logical, int dest_cpu)
+{
+	struct guest_apic_page *apic_page;
+
+	apic_page = &apic_page_pool->guest_apic_page[dest_cpu];
+	uint32_t ldr;
+
+	if (logical) {
+		ldr = savic_read_reg(apic_page, APIC_LDR);
+		return ((ldr >> 16) == (dest >> 16)) &&
+			(ldr & dest & 0xffff) != 0;
+	} else {
+		return dest == savic_read_reg(apic_page, APIC_ID);
+	}
+}
+
+static void savic_send_ipi_target(uint32_t dest, int vector, bool logical,
+				  bool nmi)
+{
+	int cpu;
+	int mycpu = x2apic_read_reg(APIC_ID);
+
+	for (cpu = 0; cpu < KVM_MAX_VCPUS; cpu++) {
+		if (cpu == mycpu)
+			continue;
+		if (!(cpu == 0 || is_cpu_present(cpu)))
+			break;
+		if (ipi_match_dest(dest, logical, cpu))
+			send_ipi(cpu, vector, nmi);
+	}
+}
+
+static void savic_handle_icr_write(uint64_t icr_data)
+{
+	int dsh                = icr_data & APIC_DEST_ALLBUT;
+	int vector             = icr_data & APIC_VECTOR_MASK;
+	bool logical           = icr_data & APIC_DEST_LOGICAL;
+	bool nmi               = (icr_data & APIC_DM_FIXED_MASK) == APIC_DM_NMI;
+	uint64_t self_icr_data = APIC_DEST_SELF | APIC_INT_ASSERT | vector;
+
+	if (nmi)
+		self_icr_data |= APIC_DM_NMI;
+
+	switch (dsh) {
+	case APIC_DEST_ALLINC:
+		savic_send_ipi_all_but(vector, nmi);
+		savic_hv_write_reg(APIC_ICR, icr_data);
+		x2apic_write_reg(APIC_ICR, self_icr_data);
+		break;
+	case APIC_DEST_ALLBUT:
+		savic_send_ipi_all_but(vector, nmi);
+		savic_hv_write_reg(APIC_ICR, icr_data);
+		break;
+	default:
+		savic_send_ipi_target(icr_data >> 32, vector, logical, nmi);
+		savic_hv_write_reg(APIC_ICR, icr_data);
+		break;
+	}
+}
+
 void savic_vc_handler(struct ex_regs *regs)
 {
 	uint64_t exit_code = regs->error_code;
@@ -343,6 +447,13 @@ void savic_vc_handler(struct ex_regs *regs)
 	case SVM_EXIT_AVIC_UNACCELERATED_ACCESS:
 		handle_savic_unaccel_access(regs);
 		break;
+	case SVM_EXIT_AVIC_INCOMPLETE_IPI:
+		uint64_t icr_data = regs->rax | (regs->rdx << 32);
+		uint32_t reg = (regs->rcx - APIC_BASE_MSR) << 4;
+
+		GUEST_ASSERT(reg == APIC_ICR);
+		savic_handle_icr_write(icr_data);
+		break;
 	default:
 		sev_es_vc_handler(regs);
 		break;
-- 
2.34.1


  parent reply	other threads:[~2025-09-23  5:17 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-23  5:09 [RFC PATCH v2 00/35] AMD: Add Secure AVIC KVM selftests Neeraj Upadhyay
2025-09-23  5:09 ` [RFC PATCH v2 01/35] Add GHCB with setters and getters Neeraj Upadhyay
2025-09-23  5:09 ` [RFC PATCH v2 02/35] Add arch specific additional guest pages Neeraj Upadhyay
2025-09-23  5:09 ` [RFC PATCH v2 03/35] Add vm_vaddr_alloc_pages_shared() Neeraj Upadhyay
2025-09-23  5:09 ` [RFC PATCH v2 04/35] Add GHCB allocations and helpers Neeraj Upadhyay
2025-09-23  5:09 ` [RFC PATCH v2 05/35] Add is_sev_enabled() helpers Neeraj Upadhyay
2025-09-23  5:09 ` [RFC PATCH v2 06/35] Add ability for SEV-ES guests to use ucalls via GHCB Neeraj Upadhyay
2025-09-23  5:09 ` [RFC PATCH v2 07/35] KVM: selftests: Return an unused GHCB from the pool Neeraj Upadhyay
2025-09-23  5:09 ` [RFC PATCH v2 08/35] KVM: selftests: Align GHCB entry struct to page size Neeraj Upadhyay
2025-09-23  5:09 ` [RFC PATCH v2 09/35] KVM: selftests: Add #VC exception handler with error code support Neeraj Upadhyay
2025-09-23  5:09 ` [RFC PATCH v2 10/35] KVM: selftests: Add MSR access support for SEV-ES guests Neeraj Upadhyay
2025-09-23  5:09 ` [RFC PATCH v2 11/35] KVM: selftests: Skip guest memory checks for APIC MMIO mapping Neeraj Upadhyay
2025-09-23  5:09 ` [RFC PATCH v2 12/35] KVM: selftests: Add x86 instruction decoding library Neeraj Upadhyay
2025-09-23  5:09 ` [RFC PATCH v2 13/35] KVM: selftests: Adapt instruction decoder library for userspace Neeraj Upadhyay
2025-09-23  5:09 ` [RFC PATCH v2 14/35] KVM: selftests: Restrict instruction decoder to x86_64 only Neeraj Upadhyay
2025-09-23  5:09 ` [RFC PATCH v2 15/35] KVM: selftests: Remove unneeded functions from instruction decoder Neeraj Upadhyay
2025-09-23  5:09 ` [RFC PATCH v2 16/35] KVM: selftests: Fix missing definitions in x86 " Neeraj Upadhyay
2025-09-23  5:09 ` [RFC PATCH v2 17/35] KVM: selftests: Change pt_regs to ex_regs for selftest use Neeraj Upadhyay
2025-09-23  5:09 ` [RFC PATCH v2 18/35] KVM: selftests: Add compilation for instruction decoder library Neeraj Upadhyay
2025-09-23  5:09 ` [RFC PATCH v2 19/35] KVM: selftests: Add MMIO #VC exception handling for SEV-ES guests Neeraj Upadhyay
2025-09-23  5:09 ` [RFC PATCH v2 20/35] KVM: selftests: Add instruction decoding for movabs instructions Neeraj Upadhyay
2025-09-23  5:09 ` [RFC PATCH v2 21/35] KVM: selftests: Add SEV guest support in xapic_state_test Neeraj Upadhyay
2025-09-23  5:09 ` [RFC PATCH v2 22/35] KVM: selftests: Add x2apic mode testing in xapic_ipi_test Neeraj Upadhyay
2025-09-23  5:09 ` [RFC PATCH v2 23/35] KVM: selftests: Add SEV VM support " Neeraj Upadhyay
2025-09-23  5:09 ` [RFC PATCH v2 24/35] KVM: selftests: Add Secure AVIC library Neeraj Upadhyay
2025-09-23  5:09 ` [RFC PATCH v2 25/35] KVM: selftests: Add #VC handler for unaccelerated Secure AVIC MSRs Neeraj Upadhyay
2025-09-23  5:09 ` Neeraj Upadhyay [this message]
2025-09-23  5:09 ` [RFC PATCH v2 27/35] KVM: selftests: Add args parameter to kvm_arch_vm_post_create() Neeraj Upadhyay
2025-09-23  5:09 ` [RFC PATCH v2 28/35] KVM: selftests: Add GHCB call for SAVIC backing page notification Neeraj Upadhyay
2025-09-23  5:09 ` [RFC PATCH v2 29/35] KVM: selftests: Add Secure AVIC mode to xapic_ipi_test Neeraj Upadhyay
2025-09-23  5:09 ` [RFC PATCH v2 30/35] KVM: selftests: Add test to verify APIC MSR accesses for SAVIC guest Neeraj Upadhyay
2025-09-23  5:09 ` [RFC PATCH v2 31/35] KVM: selftests: Extend savic_test with idle halt testing Neeraj Upadhyay
2025-09-23  5:09 ` [RFC PATCH v2 32/35] KVM: selftests: Add IOAPIC tests for Secure AVIC Neeraj Upadhyay
2025-09-23  5:09 ` [RFC PATCH v2 33/35] KVM: selftests: Add cross-vCPU IPI testing for SAVIC guests Neeraj Upadhyay
2025-09-23  5:09 ` [RFC PATCH v2 34/35] KVM: selftests: Add NMI test " Neeraj Upadhyay
2025-09-23  5:09 ` [RFC PATCH v2 35/35] KVM: selftests: Add MSI injection test for SAVIC Neeraj Upadhyay

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=20250923050942.206116-27-Neeraj.Upadhyay@amd.com \
    --to=neeraj.upadhyay@amd.com \
    --cc=David.Kaplan@amd.com \
    --cc=Santosh.Shukla@amd.com \
    --cc=Suravee.Suthikulpanit@amd.com \
    --cc=Thomas.Lendacky@amd.com \
    --cc=Vasant.Hegde@amd.com \
    --cc=bp@alien8.de \
    --cc=huibo.wang@amd.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=naveen.rao@amd.com \
    --cc=nikunj@amd.com \
    --cc=pbonzini@redhat.com \
    --cc=pgonda@google.com \
    --cc=seanjc@google.com \
    --cc=shuah@kernel.org \
    --cc=tiala@microsoft.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

all inboxes | Powered by JetHome®