[PATCH v11 15/21] KVM: selftests: Setup memory regions for TDX on vm creation

Sagi Shahar
2025-09-25T17:29:24Z

Guest registers are inaccessible to kvm for TDX VMs. In order to set
register values for TDX we use a special boot code which loads the
register values from memory and write them into the appropriate
registers.

This patch sets up the memory regions used for the boot code and the
boot parameters for TDX.

Signed-off-by: Sagi Shahar <sagis@google.com>
---
tools/testing/selftests/kvm/lib/kvm_util.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
index b4c8702ba4bd..d8a944b5ada3 100644
--- a/tools/testing/selftests/kvm/lib/kvm_util.c
+++ b/tools/testing/selftests/kvm/lib/kvm_util.c
@@ -4,6 +4,7 @@
*
* Copyright (C) 2018, Google LLC.
*/
+#include "tdx/tdx_util.h"
#include "test_util.h"
#include "kvm_util.h"
#include "processor.h"
@@ -465,7 +466,7 @@ void kvm_set_files_rlimit(uint32_t nr_vcpus)
static bool is_guest_memfd_required(struct vm_shape shape)
{
#ifdef __x86_64__
- return shape.type == KVM_X86_SNP_VM;
+ return (shape.type == KVM_X86_SNP_VM || shape.type == KVM_X86_TDX_VM);
#else
return false;
#endif
@@ -499,6 +500,12 @@ struct kvm_vm *__vm_create(struct vm_shape shape, uint32_t nr_runnable_vcpus,
for (i = 0; i < NR_MEM_REGIONS; i++)
vm->memslots[i] = 0;

+ if (is_tdx_vm(vm)) {
+ /* Setup additional mem regions for TDX. */
+ vm_tdx_setup_boot_code_region(vm);
+ vm_tdx_setup_boot_parameters_region(vm, nr_runnable_vcpus);
+ }
+
kvm_vm_elf_load(vm, program_invocation_name);

/*
--
2.51.0.536.g15c5d4f767-goog