[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250923050942.206116-29-Neeraj.Upadhyay@amd.com>
Date: Tue, 23 Sep 2025 10:39:35 +0530
From: Neeraj Upadhyay <Neeraj.Upadhyay@....com>
To: <kvm@...r.kernel.org>, <seanjc@...gle.com>, <pbonzini@...hat.com>
CC: <linux-kernel@...r.kernel.org>, <Thomas.Lendacky@....com>,
<nikunj@....com>, <Santosh.Shukla@....com>, <Vasant.Hegde@....com>,
<Suravee.Suthikulpanit@....com>, <bp@...en8.de>, <David.Kaplan@....com>,
<huibo.wang@....com>, <naveen.rao@....com>, <pgonda@...gle.com>,
<linux-kselftest@...r.kernel.org>, <shuah@...nel.org>, <tiala@...rosoft.com>
Subject: [RFC PATCH v2 28/35] KVM: selftests: Add GHCB call for SAVIC backing page notification
When a vCPU enables Secure AVIC (SAVIC), the hardware needs to directly
access the guest's APIC backing page. For this to work correctly, the
hypervisor must be aware of the guest physical address (GPA) of this
page so it can ensure the page is mapped and pinned in the nested page
tables (NPT) while the vCPU is running.
Introduce a paravirtual GHCB call that the guest uses to notify the
hypervisor of the backing page's GPA before activating SAVIC.
Signed-off-by: Neeraj Upadhyay <Neeraj.Upadhyay@....com>
---
tools/testing/selftests/kvm/include/x86/sev.h | 1 +
tools/testing/selftests/kvm/lib/x86/savic.c | 1 +
tools/testing/selftests/kvm/lib/x86/sev.c | 25 ++++++++++++++++++-
3 files changed, 26 insertions(+), 1 deletion(-)
diff --git a/tools/testing/selftests/kvm/include/x86/sev.h b/tools/testing/selftests/kvm/include/x86/sev.h
index 73a23043d6c5..3a95b13fb6c0 100644
--- a/tools/testing/selftests/kvm/include/x86/sev.h
+++ b/tools/testing/selftests/kvm/include/x86/sev.h
@@ -171,4 +171,5 @@ 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);
void sev_es_pv_mmio_rw(uint32_t *reg_gpa, uint32_t *data, bool write);
+void sev_es_savic_notify_gpa(uint64_t gpa);
#endif /* SELFTEST_KVM_SEV_H */
diff --git a/tools/testing/selftests/kvm/lib/x86/savic.c b/tools/testing/selftests/kvm/lib/x86/savic.c
index 016e5e9e43f6..24ee15cc5603 100644
--- a/tools/testing/selftests/kvm/lib/x86/savic.c
+++ b/tools/testing/selftests/kvm/lib/x86/savic.c
@@ -203,6 +203,7 @@ void savic_enable(void)
apic_page = &apic_page_pool->guest_apic_page[apic_id];
savic_init_backing_page(apic_page, apic_id);
+ sev_es_savic_notify_gpa(apic_page->gpa);
set_savic_control_msr(apic_page, true, true);
savic_ctrl_msr_val = rdmsr(MSR_AMD64_SECURE_AVIC_CONTROL);
exp_msr_val = apic_page->gpa | BIT_ULL(MSR_AMD64_SECURE_AVIC_EN_BIT) |
diff --git a/tools/testing/selftests/kvm/lib/x86/sev.c b/tools/testing/selftests/kvm/lib/x86/sev.c
index 257988fce107..840504f0243c 100644
--- a/tools/testing/selftests/kvm/lib/x86/sev.c
+++ b/tools/testing/selftests/kvm/lib/x86/sev.c
@@ -8,6 +8,7 @@
#include "linux/bitmap.h"
#include "svm.h"
#include "svm_util.h"
+#include "savic.h"
#define IOIO_TYPE_STR (1 << 2)
#define IOIO_SEG_DS (1 << 11 | 1 << 10)
@@ -17,7 +18,8 @@
#define SW_EXIT_CODE_IOIO 0x7b
#define SW_EXIT_CODE_MSR 0x7c
#define SVM_VMGEXIT_MMIO_READ 0x80000001
-#define SVM_VMGEXIT_MMIO_WRITE 0x80000002
+#define SVM_VMGEXIT_MMIO_WRITE 0x80000002
+#define SVM_VMGEXIT_SECURE_AVIC 0x8000001a
struct ghcb_entry {
struct ghcb ghcb;
@@ -727,3 +729,24 @@ void sev_es_vc_handler(struct ex_regs *regs)
__GUEST_ASSERT(0, "No VC handler\n");
}
}
+
+void sev_es_savic_notify_gpa(uint64_t gpa)
+{
+ struct ghcb_entry *entry;
+ struct ghcb *ghcb;
+ int ret;
+
+ entry = ghcb_alloc();
+ ghcb = &entry->ghcb;
+
+ register_ghcb_page(entry->gpa);
+ ghcb_set_sw_exit_code(ghcb, SVM_VMGEXIT_SECURE_AVIC);
+ ghcb_set_rax(ghcb, -1ULL);
+ ghcb_set_rbx(ghcb, gpa);
+ ghcb_set_sw_exit_info_1(ghcb, 0);
+ ghcb_set_sw_exit_info_2(ghcb, 0);
+ do_vmg_exit(entry->gpa);
+ ret = ghcb->save.sw_exit_info_1 & 0xffffffff;
+ __GUEST_ASSERT(!ret, "Secure AVIC GPA notification failed, ret: %d", ret);
+ ghcb_free(entry);
+}
--
2.34.1
Powered by blists - more mailing lists