[PATCH v12 15/23] KVM: selftests: Call TDX init when creating a new TDX vm

Sagi Shahar posted 23 patches 1 month, 2 weeks ago
[PATCH v12 15/23] KVM: selftests: Call TDX init when creating a new TDX vm
Posted by Sagi Shahar 1 month, 2 weeks ago
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 5f75bd48623b..990f2769c5d8 100644
--- a/tools/testing/selftests/kvm/lib/x86/processor.c
+++ b/tools/testing/selftests/kvm/lib/x86/processor.c
@@ -676,6 +676,11 @@ void kvm_arch_vm_post_create(struct kvm_vm *vm, unsigned int nr_vcpus)
 		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.1.851.g4ebd6896fd-goog
Re: [PATCH v12 15/23] KVM: selftests: Call TDX init when creating a new TDX vm
Posted by Reinette Chatre 1 month, 2 weeks ago
Hi Sagi,

On 10/28/25 2:20 PM, Sagi Shahar wrote:
> 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.

"TDX VMs need to issue the KVM_TDX_INIT_VM ioctl" ... take care with the language
here since it is not the VM that issues the ioctl() (same in patch #17 changelog).

"after VM creation to initialize the TD" ... is the switching between terms
("VM" and "TD") necessary? Always referring to the same "thing" using the same
term really helps to make the text easier to read.

Finally, please do stick to imperative tone. For example,
	Initialize the VM with the TDX specific parameters, such as guest CPUIDs
	emulated by the TDX module, that the VM can support.

Reinette
Re: [PATCH v12 15/23] KVM: selftests: Call TDX init when creating a new TDX vm
Posted by Ira Weiny 1 month, 2 weeks ago
Sagi Shahar wrote:
> 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,

Avoid 'we'.

> 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.

This can be dropped as well.

Ira

[snip]