[PATCH v11 14/21] KVM: selftests: Call TDX init when creating a new TDX vm

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

TDX VMs need to issue the KVM_TDX_INIT_VM ioctl after VM creation to
initialize the TD. This ioctl also sets the cpuids and attributes for
the VM.

At this point we can also set the common boot parameters such as CR3,
CR4, etc. These parameters will get copied to the relevant registers by
the TD boot code trampoline.

Signed-off-by: Sagi Shahar <sagis@google.com>

---------------------------------------------

Changes from v10:
* The call to vm_tdx_load_common_boot_parameters() was accidently
dropped as part of the refactor from v9 to v10. I re-added it here.
---
tools/testing/selftests/kvm/lib/x86/processor.c | 5 +++++
1 file changed, 5 insertions(+)

diff --git a/tools/testing/selftests/kvm/lib/x86/processor.c b/tools/testing/selftests/kvm/lib/x86/processor.c
index 623168ea9a44..63b751253d1b 100644
--- a/tools/testing/selftests/kvm/lib/x86/processor.c
+++ b/tools/testing/selftests/kvm/lib/x86/processor.c
@@ -641,6 +641,11 @@ void kvm_arch_vm_post_create(struct kvm_vm *vm)
vm_sev_ioctl(vm, KVM_SEV_INIT2, &init);
}

+ if (is_tdx_vm(vm)) {
+ vm_tdx_init_vm(vm, 0);
+ vm_tdx_load_common_boot_parameters(vm);
+ }
+
r = __vm_ioctl(vm, KVM_GET_TSC_KHZ, NULL);
TEST_ASSERT(r > 0, "KVM_GET_TSC_KHZ did not provide a valid TSC frequency.");
guest_tsc_khz = r;
--
2.51.0.536.g15c5d4f767-goog