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 10/35] KVM: selftests: Add MSR access support for SEV-ES guests
Date: Tue, 23 Sep 2025 10:39:17 +0530 [thread overview]
Message-ID: <20250923050942.206116-11-Neeraj.Upadhyay@amd.com> (raw)
In-Reply-To: <20250923050942.206116-1-Neeraj.Upadhyay@amd.com>
For SEV-ES guests, RDMSR/WRMSR of the intercepted MSRs trap and
generate a #VC (VMM Communication) exception. The guest must then
handle this exception and communicate the desired MSR operation to the
hypervisor using the GHCB protocol.
Add the necessary selftest infrastructure to support this flow, enabling
tests to correctly perform MSR operations from within an SEV-ES guest.
Two mechanisms are introduced:
1. #VC Exception Handling: Provide a #VC handler, which inspects the
trapping instruction and the register state and does a GHCB
request to forward the MSR operation to the host .
2. Paravirtual Interface: Provide a direct, "paravirtual" way to test
the MSR GHCB protocol. This allows test code to request an MSR read
or write without needing to execute a trapping instruction,
simplifying certain test scenarios.
Signed-off-by: Neeraj Upadhyay <Neeraj.Upadhyay@amd.com>
---
tools/testing/selftests/kvm/include/x86/sev.h | 2 +
tools/testing/selftests/kvm/lib/x86/sev.c | 83 ++++++++++++++++++-
2 files changed, 84 insertions(+), 1 deletion(-)
diff --git a/tools/testing/selftests/kvm/include/x86/sev.h b/tools/testing/selftests/kvm/include/x86/sev.h
index a4fbea0d3562..9c1fe6be5e68 100644
--- a/tools/testing/selftests/kvm/include/x86/sev.h
+++ b/tools/testing/selftests/kvm/include/x86/sev.h
@@ -165,4 +165,6 @@ static inline void snp_launch_update_data(struct kvm_vm *vm, vm_paddr_t gpa,
void sev_es_ucall_port_write(uint32_t port, uint64_t data);
+void sev_es_vc_handler(struct ex_regs *regs);
+void sev_es_pv_msr_rw(uint64_t msr, uint64_t *data, bool write);
#endif /* SELFTEST_KVM_SEV_H */
diff --git a/tools/testing/selftests/kvm/lib/x86/sev.c b/tools/testing/selftests/kvm/lib/x86/sev.c
index edefba7f49ce..57ae5a388b8c 100644
--- a/tools/testing/selftests/kvm/lib/x86/sev.c
+++ b/tools/testing/selftests/kvm/lib/x86/sev.c
@@ -12,7 +12,8 @@
#define IOIO_DATA_8 (1 << 4)
#define IOIO_REP (1 << 3)
-#define SW_EXIT_CODE_IOIO 0x7b
+#define SW_EXIT_CODE_IOIO 0x7b
+#define SW_EXIT_CODE_MSR 0x7c
struct ghcb_entry {
struct ghcb ghcb;
@@ -356,3 +357,83 @@ void sev_es_ucall_port_write(uint32_t port, uint64_t data)
ghcb_free(entry);
}
+
+static void __sev_es_msr_rw(struct ghcb_entry *entry, uint64_t msr,
+ uint32_t *low, uint32_t *high, bool write)
+{
+ uint64_t exitinfo1 = write ? 1 : 0;
+ struct ghcb *ghcb = &entry->ghcb;
+ uint32_t ret;
+
+ ghcb_set_sw_exit_code(ghcb, SW_EXIT_CODE_MSR);
+ ghcb_set_sw_exit_info_1(ghcb, exitinfo1);
+ ghcb_set_sw_exit_info_2(ghcb, 0);
+
+ ghcb_set_rcx(ghcb, msr);
+ if (write) {
+ ghcb_set_rax(ghcb, *low);
+ ghcb_set_rdx(ghcb, *high);
+ }
+
+ do_vmg_exit(entry->gpa);
+
+ ret = ghcb->save.sw_exit_info_1 & 0xffffffff;
+ __GUEST_ASSERT(!ret, "%smsr failed, ret: %u", write ? "wr" : "rd", ret);
+
+ if (!write) {
+ *low = ghcb->save.rax;
+ *high = ghcb->save.rdx;
+ }
+}
+
+void sev_es_pv_msr_rw(uint64_t msr, uint64_t *data, bool write)
+{
+ struct ghcb_entry *entry;
+ uint32_t low, high;
+
+ entry = ghcb_alloc();
+ register_ghcb_page(entry->gpa);
+
+ if (write) {
+ low = *data & ((1ULL << 32) - 1);
+ high = *data >> 32;
+ }
+ __sev_es_msr_rw(entry, msr, &low, &high, write);
+
+ if (!write)
+ *data = low | (uint64_t)high << 32;
+
+ ghcb_free(entry);
+}
+
+static void sev_es_vc_msr_handler(struct ex_regs *regs)
+{
+ struct ghcb_entry *entry;
+ bool write;
+
+ /* wrmsr encoding has second byte = 0x30 */
+ write = (*((char *)regs->rip + 1) == 0x30);
+
+ entry = ghcb_alloc();
+ register_ghcb_page(entry->gpa);
+
+ __sev_es_msr_rw(entry, regs->rcx, (uint32_t *)®s->rax,
+ (uint32_t *)®s->rdx, write);
+
+ ghcb_free(entry);
+}
+
+void sev_es_vc_handler(struct ex_regs *regs)
+{
+ uint64_t exit_code = regs->error_code;
+
+ switch (exit_code) {
+ case SVM_EXIT_MSR:
+ sev_es_vc_msr_handler(regs);
+ /* rdmsr/wrmsr instruction size = 2 */
+ regs->rip += 2;
+ break;
+ default:
+ __GUEST_ASSERT(0, "No VC handler\n");
+ }
+}
--
2.34.1
next prev parent reply other threads:[~2025-09-23 5:13 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 ` Neeraj Upadhyay [this message]
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 ` [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-11-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®