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 25/35] KVM: selftests: Add #VC handler for unaccelerated Secure AVIC MSRs
Date: Tue, 23 Sep 2025 10:39:32 +0530 [thread overview]
Message-ID: <20250923050942.206116-26-Neeraj.Upadhyay@amd.com> (raw)
In-Reply-To: <20250923050942.206116-1-Neeraj.Upadhyay@amd.com>
When Secure AVIC (SAVIC) is enabled for an SEV-SNP guest, most APIC
register accesses are accelerated via a guest-visible backing page.
However, not all APIC registers are handled this way. Accesses to
certain "unaccelerated" registers via RDMSR or WRMSR are designed
to trigger a #VC (Virtualization Exception) with the new exit code
SVM_EXIT_AVIC_UNACCELERATED_ACCESS.
Without a handler for this exit code, any guest attempt to access
these MSRs would result in an unhandled fault, crashing the guest.
Implement the necessary #VC handler to emulate these accesses.
The new handler (handle_savic_unaccel_access) emulates the MSR operation.
For MSR reads/writes, it applies different strategies based on the
register:
- Read/Write APIC timer, APIC Spurious Interrupt Vector and LVT registers'
from the hypervisor using the existing paravirtual MSR interface as LVT
and APIC timers are emulated by the hypervisor.
- Propagate EOI write to the hypervisor. This is required for
acknowledging emulated IOAPIC interrupts.
- Keep APIC_ID in-sync between the guest and the hypervisor.
- For rest of the valid APIC register writes, write the value only in the
APIC backing page of the guest vCPU.
This emulation provides the necessary glue to allow guest code to run
seamlessly when SAVIC is active, enabling comprehensive testing of
APIC functionality in this mode.
Signed-off-by: Neeraj Upadhyay <Neeraj.Upadhyay@amd.com>
---
.../testing/selftests/kvm/include/x86/apic.h | 1 +
.../testing/selftests/kvm/include/x86/savic.h | 1 +
tools/testing/selftests/kvm/lib/x86/savic.c | 145 ++++++++++++++++++
3 files changed, 147 insertions(+)
diff --git a/tools/testing/selftests/kvm/include/x86/apic.h b/tools/testing/selftests/kvm/include/x86/apic.h
index 6ba5d0545bf8..aa3a5d54c404 100644
--- a/tools/testing/selftests/kvm/include/x86/apic.h
+++ b/tools/testing/selftests/kvm/include/x86/apic.h
@@ -33,6 +33,7 @@
#define APIC_SPIV 0xF0
#define APIC_SPIV_FOCUS_DISABLED (1 << 9)
#define APIC_SPIV_APIC_ENABLED (1 << 8)
+#define APIC_TMR 0x180
#define APIC_IRR 0x200
#define APIC_ICR 0x300
#define APIC_LVTCMCI 0x2f0
diff --git a/tools/testing/selftests/kvm/include/x86/savic.h b/tools/testing/selftests/kvm/include/x86/savic.h
index 1ab92dad00c1..238d7450ab6e 100644
--- a/tools/testing/selftests/kvm/include/x86/savic.h
+++ b/tools/testing/selftests/kvm/include/x86/savic.h
@@ -16,4 +16,5 @@ void savic_hv_write_reg(uint32_t reg, uint64_t val);
uint64_t savic_hv_read_reg(uint32_t reg);
void savic_enable(void);
int savic_nr_pages_required(uint64_t page_size);
+void savic_vc_handler(struct ex_regs *regs);
#endif
diff --git a/tools/testing/selftests/kvm/lib/x86/savic.c b/tools/testing/selftests/kvm/lib/x86/savic.c
index 72ad43d4797e..9b1ea5d15338 100644
--- a/tools/testing/selftests/kvm/lib/x86/savic.c
+++ b/tools/testing/selftests/kvm/lib/x86/savic.c
@@ -41,6 +41,8 @@ enum lapic_lvt_entry {
#define MSR_AMD64_SECURE_AVIC_EN_BIT 0
#define MSR_AMD64_SECURE_AVIC_ALLOWED_NMI_BIT 1
+#define SVM_EXIT_AVIC_UNACCELERATED_ACCESS 0x402
+
/*
* Initial pool of guest apic backing page.
*/
@@ -203,3 +205,146 @@ void savic_enable(void)
"SAVIC Control msr unexpected val : 0x%lx, expected : 0x%lx",
savic_ctrl_msr_val, exp_msr_val);
}
+
+static bool savic_reg_access_is_trapped(uint32_t reg)
+{
+ switch (reg) {
+ case APIC_ID:
+ case APIC_TASKPRI:
+ case APIC_EOI:
+ case APIC_LDR:
+ case APIC_SPIV:
+ case APIC_ICR:
+ case APIC_ICR2:
+ case APIC_LVTT:
+ case APIC_LVTTHMR:
+ case APIC_LVTPC:
+ case APIC_LVT0:
+ case APIC_LVT1:
+ case APIC_LVTERR:
+ case APIC_TMICT:
+ case APIC_TDCR:
+ return true;
+ case APIC_LVR:
+ case APIC_PROCPRI:
+ case APIC_TMR:
+ case APIC_IRR ... APIC_IRR + 0x70:
+ case APIC_TMCCT:
+ return false;
+ default:
+ return false;
+ }
+}
+
+static void savic_unaccel_apic_msrs_read(struct guest_apic_page *apic_page,
+ uint32_t reg, uint64_t *val)
+{
+ switch (reg) {
+ case APIC_TMICT:
+ case APIC_TMCCT:
+ case APIC_TDCR:
+ case APIC_LVTT:
+ case APIC_LVTTHMR:
+ case APIC_LVTPC:
+ case APIC_LVT0:
+ case APIC_LVT1:
+ case APIC_LVTERR:
+ *val = savic_hv_read_reg(reg);
+ break;
+ default:
+ __GUEST_ASSERT(0, "Unexpected unaccelerated read trap for reg: %x\n", reg);
+ }
+}
+
+static void savic_unaccel_apic_msrs_write(struct guest_apic_page *apic_page,
+ uint32_t reg, uint64_t val)
+{
+ switch (reg) {
+ /*
+ * APIC_ID value is in sync between guest apic backing page and
+ * hv.
+ * LVT* registers and APIC timer register updates are propagated to hv.
+ */
+ case APIC_ID:
+ case APIC_LVTT:
+ case APIC_LVTTHMR:
+ case APIC_LVTPC:
+ case APIC_LVT0:
+ case APIC_LVT1:
+ case APIC_LVTERR:
+ case APIC_SPIV:
+ case APIC_TMICT:
+ case APIC_TMCCT:
+ case APIC_TDCR:
+ savic_write_reg(apic_page, reg, val);
+ savic_hv_write_reg(reg, val);
+ break;
+ /*
+ * LDR is derived in hv from APIC_ID.
+ * TPR, IRR information is not propagated to hv.
+ */
+ case APIC_LDR:
+ case APIC_TASKPRI:
+ case APIC_IRR:
+ savic_write_reg(apic_page, reg, val);
+ break;
+ /*
+ * EOI write need to be propagated to hv for level-triggered
+ * interrupts.
+ */
+ case APIC_EOI:
+ savic_hv_write_reg(reg, val);
+ break;
+ default:
+ __GUEST_ASSERT(0, "Write not permitted for reg: %x\n", reg);
+ }
+}
+
+static void handle_savic_unaccel_access(struct ex_regs *regs)
+{
+ bool write;;
+ uint64_t msr = regs->rcx;
+ uint32_t reg = (msr - APIC_BASE_MSR) << 4;
+ struct guest_apic_page *apic_page;
+ uint64_t low = regs->rax;
+ uint64_t high = regs->rdx;
+ uint64_t val = 0;
+
+ apic_page = &apic_page_pool->guest_apic_page[x2apic_read_reg(APIC_ID)];
+
+ switch (msr) {
+ case APIC_BASE_MSR ... APIC_BASE_MSR + 0xff:
+ if (savic_reg_access_is_trapped(reg))
+ write = *((uint8_t *)regs->rip - 1) == 0x30;
+ else
+ write = *((uint8_t *)regs->rip + 1) == 0x30;
+ if (write) {
+ savic_unaccel_apic_msrs_write(apic_page, reg,
+ high << 32 | low);
+ } else {
+ savic_unaccel_apic_msrs_read(apic_page, reg, &val);
+ regs->rax = val & ((1ULL << 32) - 1);
+ regs->rdx = val >> 32;
+ }
+ if (!savic_reg_access_is_trapped(reg))
+ regs->rip += 2;
+ break;
+ default:
+ __GUEST_ASSERT(0, "Unknown unaccelerated msr: %lx\n", msr);
+ break;
+ }
+}
+
+void savic_vc_handler(struct ex_regs *regs)
+{
+ uint64_t exit_code = regs->error_code;
+
+ switch (exit_code) {
+ case SVM_EXIT_AVIC_UNACCELERATED_ACCESS:
+ handle_savic_unaccel_access(regs);
+ break;
+ default:
+ sev_es_vc_handler(regs);
+ break;
+ }
+}
--
2.34.1
next prev 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 ` Neeraj Upadhyay [this message]
2025-09-23 5:09 ` [RFC PATCH v2 26/35] KVM: selftests: Add IPI handling support for Secure AVIC Neeraj Upadhyay
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-26-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®