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 04/35] Add GHCB allocations and helpers
Date: Tue, 23 Sep 2025 10:39:11 +0530	[thread overview]
Message-ID: <20250923050942.206116-5-Neeraj.Upadhyay@amd.com> (raw)
In-Reply-To: <20250923050942.206116-1-Neeraj.Upadhyay@amd.com>

From: Peter Gonda <pgonda@google.com>

Add GHCB management functionality similar to the ucall management.
Allows for selftest vCPUs to acquire GHCBs for their usage.

Cc: Vishal Annapurve <vannapurve@google.com>
Cc: Ackerley Tng <ackerleytng@google.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Claudio Imbrenda <imbrenda@linux.ibm.com>
Cc: Sean Christopherson <seanjc@google.com>
Cc: Carlos Bilbao <carlos.bilbao@amd.com>
Cc: Tom Lendacky <thomas.lendacky@amd.com>
Cc: Michael Roth <michael.roth@amd.com>
Cc: kvm@vger.kernel.org
Cc: linux-kselftest@vger.kernel.org
Signed-off-by: Peter Gonda <pgonda@google.com>
Signed-off-by: Neeraj Upadhyay <Neeraj.Upadhyay@amd.com>
---
 tools/testing/selftests/kvm/include/x86/sev.h |  2 +
 .../testing/selftests/kvm/lib/x86/processor.c |  9 +++
 tools/testing/selftests/kvm/lib/x86/sev.c     | 78 +++++++++++++++++++
 3 files changed, 89 insertions(+)

diff --git a/tools/testing/selftests/kvm/include/x86/sev.h b/tools/testing/selftests/kvm/include/x86/sev.h
index 008b4169f5e2..6cda0acd22e4 100644
--- a/tools/testing/selftests/kvm/include/x86/sev.h
+++ b/tools/testing/selftests/kvm/include/x86/sev.h
@@ -46,6 +46,8 @@ static inline bool is_sev_vm(struct kvm_vm *vm)
 	return is_sev_es_vm(vm) || vm->type == KVM_X86_SEV_VM;
 }
 
+int ghcb_nr_pages_required(uint64_t page_size);
+
 void sev_vm_launch(struct kvm_vm *vm, uint32_t policy);
 void sev_vm_launch_measure(struct kvm_vm *vm, uint8_t *measurement);
 void sev_vm_launch_finish(struct kvm_vm *vm);
diff --git a/tools/testing/selftests/kvm/lib/x86/processor.c b/tools/testing/selftests/kvm/lib/x86/processor.c
index d4c19ac885a9..d72eb96efb7c 100644
--- a/tools/testing/selftests/kvm/lib/x86/processor.c
+++ b/tools/testing/selftests/kvm/lib/x86/processor.c
@@ -651,6 +651,15 @@ void kvm_arch_vm_post_create(struct kvm_vm *vm)
 	sync_global_to_guest(vm, guest_tsc_khz);
 }
 
+int kvm_arch_vm_additional_pages_required(struct vm_shape shape, uint64_t page_size)
+{
+	if (shape.type == KVM_X86_SEV_ES_VM ||
+	    shape.type == KVM_X86_SNP_VM)
+		return  ghcb_nr_pages_required(page_size);
+
+	return 0;
+}
+
 void vcpu_arch_set_entry_point(struct kvm_vcpu *vcpu, void *guest_code)
 {
 	struct kvm_regs regs;
diff --git a/tools/testing/selftests/kvm/lib/x86/sev.c b/tools/testing/selftests/kvm/lib/x86/sev.c
index c3a9838f4806..c400faa9cc5f 100644
--- a/tools/testing/selftests/kvm/lib/x86/sev.c
+++ b/tools/testing/selftests/kvm/lib/x86/sev.c
@@ -3,6 +3,80 @@
 #include <stdbool.h>
 
 #include "sev.h"
+#include "linux/bitmap.h"
+#include "svm.h"
+#include "svm_util.h"
+
+struct ghcb_entry {
+	struct ghcb ghcb;
+
+	/* Guest physical address of this GHCB. */
+	void *gpa;
+
+	/* Host virtual address of this struct. */
+	struct ghcb_entry *hva;
+};
+
+struct ghcb_header {
+	struct ghcb_entry ghcbs[KVM_MAX_VCPUS];
+	DECLARE_BITMAP(in_use, KVM_MAX_VCPUS);
+};
+
+static struct ghcb_header *ghcb_pool;
+
+int ghcb_nr_pages_required(uint64_t page_size)
+{
+	return align_up(sizeof(struct ghcb_header), page_size) / page_size;
+}
+
+void ghcb_init(struct kvm_vm *vm)
+{
+	struct ghcb_header *hdr;
+	struct ghcb_entry *entry;
+	vm_vaddr_t vaddr;
+	int i;
+
+	vaddr = vm_vaddr_alloc_shared(vm, sizeof(*hdr), KVM_UTIL_MIN_VADDR,
+				      MEM_REGION_DATA);
+	hdr = (struct ghcb_header *)addr_gva2hva(vm, vaddr);
+	memset(hdr, 0, sizeof(*hdr));
+
+	for (i = 0; i < KVM_MAX_VCPUS; ++i) {
+		entry = &hdr->ghcbs[i];
+		entry->hva = entry;
+		entry->gpa = (void *)addr_hva2gpa(vm, &entry->ghcb);
+	}
+
+	write_guest_global(vm, ghcb_pool, (struct ghcb_header *)vaddr);
+}
+
+static struct ghcb_entry *ghcb_alloc(void)
+{
+	return &ghcb_pool->ghcbs[0];
+	struct ghcb_entry *entry;
+	int i;
+
+	if (!ghcb_pool)
+		goto ucall_failed;
+
+	for (i = 0; i < KVM_MAX_VCPUS; ++i) {
+		if (!test_and_set_bit(i, ghcb_pool->in_use)) {
+			entry = &ghcb_pool->ghcbs[i];
+			memset(&entry->ghcb, 0, sizeof(entry->ghcb));
+			return entry;
+		}
+	}
+
+ucall_failed:
+	return NULL;
+}
+
+static void ghcb_free(struct ghcb_entry *entry)
+{
+	/* Beware, here be pointer arithmetic.  */
+	clear_bit(entry - ghcb_pool->ghcbs, ghcb_pool->in_use);
+}
+
 
 /*
  * sparsebit_next_clear() can return 0 if [x, 2**64-1] are all set, and the
@@ -88,7 +162,11 @@ void sev_vm_launch(struct kvm_vm *vm, uint32_t policy)
 	struct kvm_sev_guest_status status;
 	int ctr;
 
+	if (is_sev_es_vm(vm))
+		ghcb_init(vm);
+
 	vm_sev_ioctl(vm, KVM_SEV_LAUNCH_START, &launch_start);
+
 	vm_sev_ioctl(vm, KVM_SEV_GUEST_STATUS, &status);
 
 	TEST_ASSERT_EQ(status.policy, policy);
-- 
2.34.1


  parent reply	other threads:[~2025-09-23  5:11 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 ` Neeraj Upadhyay [this message]
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 ` [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-5-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®