Skip to contentSkip to conversation

TDX KVM selftests

PATCH Superseded Sagi Shahar 3 participants 33 messages Sep 25, 2025 – Oct 28, 2025

Subsystems (4)

v11 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#0 lore raw

v11 cover TDX KVM selftests

This is v11 of the TDX selftests.

This series is based on v6.17-rc7

Changes from v10 [1]:
- Rebased on top of v6.17-rc4.
- Addressed minor comments from v10.
- Removed code for setting up X86_CR4_OSXMMEXCPT which is not needed for now.
- Added call to vm_tdx_load_common_boot_parameters() in "KVM: selftests:
Call TDX init when creating a new TDX vm" which was accidentally dropped between v9 and v10 due to code refactoring

[1] https://lore.kernel.org/lkml/[email protected]/#r

Ackerley Tng (2):
KVM: selftests: Add helpers to init TDX memory and finalize VM
KVM: selftests: Add ucall support for TDX

Erdem Aktas (2):
KVM: selftests: Add TDX boot code
KVM: selftests: Add support for TDX TDCALL from guest

Isaku Yamahata (2):
KVM: selftests: Update kvm_init_vm_address_properties() for TDX
KVM: selftests: TDX: Use KVM_TDX_CAPABILITIES to validate TDs'
attribute configuration

Sagi Shahar (15):
KVM: selftests: Allocate pgd in virt_map() as necessary
KVM: selftests: Expose functions to get default sregs values
KVM: selftests: Expose function to allocate guest vCPU stack
KVM: selftests: Expose segment definitons to assembly files
KVM: selftests: Add kbuild definitons
KVM: selftests: Define structs to pass parameters to TDX boot code
KVM: selftests: Set up TDX boot code region
KVM: selftests: Set up TDX boot parameters region
KVM: selftests: Add helper to initialize TDX VM
KVM: selftests: Call TDX init when creating a new TDX vm
KVM: selftests: Setup memory regions for TDX on vm creation
KVM: selftests: Call KVM_TDX_INIT_VCPU when creating a new TDX vcpu
KVM: selftests: Set entry point for TDX guest code
KVM: selftests: Add wrapper for TDX MMIO from guest
KVM: selftests: Add TDX lifecycle test

 tools/include/linux/kbuild.h                  |  18 +
 tools/testing/selftests/kvm/Makefile.kvm      |  32 ++
 .../selftests/kvm/include/ucall_common.h      |   1 +
 .../selftests/kvm/include/x86/processor.h     |  35 ++
 .../selftests/kvm/include/x86/processor_asm.h |  12 +
 .../selftests/kvm/include/x86/tdx/td_boot.h   |  74 ++++
 .../kvm/include/x86/tdx/td_boot_asm.h         |  16 +
 .../selftests/kvm/include/x86/tdx/tdcall.h    |  34 ++
 .../selftests/kvm/include/x86/tdx/tdx.h       |  14 +
 .../selftests/kvm/include/x86/tdx/tdx_util.h  |  86 +++++
 .../testing/selftests/kvm/include/x86/ucall.h |   6 -
 tools/testing/selftests/kvm/lib/kvm_util.c    |  10 +-
 .../testing/selftests/kvm/lib/x86/processor.c |  93 +++--
 .../selftests/kvm/lib/x86/tdx/td_boot.S       |  60 +++
 .../kvm/lib/x86/tdx/td_boot_offsets.c         |  21 ++
 .../selftests/kvm/lib/x86/tdx/tdcall.S        |  93 +++++
 .../kvm/lib/x86/tdx/tdcall_offsets.c          |  16 +
 tools/testing/selftests/kvm/lib/x86/tdx/tdx.c |  23 ++
 .../selftests/kvm/lib/x86/tdx/tdx_util.c      | 348 ++++++++++++++++++
 tools/testing/selftests/kvm/lib/x86/ucall.c   |  46 ++-
 tools/testing/selftests/kvm/x86/tdx_vm_test.c |  31 ++
 21 files changed, 1029 insertions(+), 40 deletions(-)
 create mode 100644 tools/include/linux/kbuild.h
 create mode 100644 tools/testing/selftests/kvm/include/x86/processor_asm.h
 create mode 100644 tools/testing/selftests/kvm/include/x86/tdx/td_boot.h
 create mode 100644 tools/testing/selftests/kvm/include/x86/tdx/td_boot_asm.h
 create mode 100644 tools/testing/selftests/kvm/include/x86/tdx/tdcall.h
 create mode 100644 tools/testing/selftests/kvm/include/x86/tdx/tdx.h
 create mode 100644 tools/testing/selftests/kvm/include/x86/tdx/tdx_util.h
 create mode 100644 tools/testing/selftests/kvm/lib/x86/tdx/td_boot.S
 create mode 100644 tools/testing/selftests/kvm/lib/x86/tdx/td_boot_offsets.c
 create mode 100644 tools/testing/selftests/kvm/lib/x86/tdx/tdcall.S
 create mode 100644 tools/testing/selftests/kvm/lib/x86/tdx/tdcall_offsets.c
 create mode 100644 tools/testing/selftests/kvm/lib/x86/tdx/tdx.c
 create mode 100644 tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c
 create mode 100644 tools/testing/selftests/kvm/x86/tdx_vm_test.c
Signature · 1 line
2.51.0.536.g15c5d4f767-goog
#1 lore raw

PATCH v11 1/21 KVM: selftests: Allocate pgd in virt_map() as necessary

If virt_map() is called before any call to ____vm_vaddr_alloc() it will create the mapping using an invalid pgd.

Add call to virt_pgd_alloc() as part of virt_map() before creating the mapping, similarly to ____vm_vaddr_alloc()

---
 tools/testing/selftests/kvm/lib/kvm_util.c | 1 +
 1 file changed, 1 insertion(+)
tools/testing/selftests/kvm/lib/kvm_util.c +1 -0
index c3f5142b0a54..b4c8702ba4bd 100644
@@ -1609,6 +1609,7 @@ void virt_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
 	TEST_ASSERT(vaddr + size > vaddr, "Vaddr overflow");
 	TEST_ASSERT(paddr + size > paddr, "Paddr overflow");
 
+	virt_pgd_alloc(vm);
 	while (npages--) {
 		virt_pg_map(vm, vaddr, paddr);
 		sparsebit_set(vm->vpages_mapped, vaddr >> vm->page_shift);
Signature · 1 line
2.51.0.536.g15c5d4f767-goog
#2 lore raw

PATCH v11 2/21 KVM: selftests: Expose functions to get default sregs values

In reply to Sagi Shahar

TDX can't set sregs values directly using KVM_SET_SREGS. Expose the default values of certain sregs used by TDX VMs so they can be set manually.

---
 .../selftests/kvm/include/x86/processor.h     | 33 +++++++++++++++++++
 .../testing/selftests/kvm/lib/x86/processor.c | 12 +++----
 2 files changed, 38 insertions(+), 7 deletions(-)
tools/testing/selftests/kvm/include/x86/processor.h +33 -0
index 2efb05c2f2fb..f610c09cadf4 100644
@@ -27,6 +27,10 @@ extern uint64_t guest_tsc_khz;
 #define MAX_NR_CPUID_ENTRIES 100
 #endif
 
+#ifndef NUM_INTERRUPTS
+#define NUM_INTERRUPTS 256
+#endif
+
 #define NONCANONICAL 0xaaaaaaaaaaaaaaaaull
 
 /* Forced emulation prefix, used to invoke the emulator unconditionally. */
@@ -1456,4 +1460,33 @@ void virt_map_level(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
 
 bool sys_clocksource_is_based_on_tsc(void);
 
+static inline uint16_t kvm_get_default_idt_limit(void)
+{
+	return NUM_INTERRUPTS * sizeof(struct idt_entry) - 1;
+}
+
+static inline uint16_t kvm_get_default_gdt_limit(void)
+{
+	return getpagesize() - 1;
+}
+
+static inline uint64_t kvm_get_default_cr0(void)
+{
+	return X86_CR0_PE | X86_CR0_NE | X86_CR0_PG;
+}
+
+static inline uint64_t kvm_get_default_cr4(void)
+{
+	uint64_t cr4 = X86_CR4_PAE | X86_CR4_OSFXSR;
+
+	if (kvm_cpu_has(X86_FEATURE_XSAVE))
+		cr4 |= X86_CR4_OSXSAVE;
+	return cr4;
+}
+
+static inline uint64_t kvm_get_default_efer(void)
+{
+	return EFER_LME | EFER_LMA | EFER_NX;
+}
+
 #endif /* SELFTEST_KVM_PROCESSOR_H */
tools/testing/selftests/kvm/lib/x86/processor.c +5 -7
index d4c19ac885a9..83efcf48faad 100644
@@ -498,15 +498,13 @@ static void vcpu_init_sregs(struct kvm_vm *vm, struct kvm_vcpu *vcpu)
 	vcpu_sregs_get(vcpu, &sregs);
 
 	sregs.idt.base = vm->arch.idt;
-	sregs.idt.limit = NUM_INTERRUPTS * sizeof(struct idt_entry) - 1;
+	sregs.idt.limit = kvm_get_default_idt_limit();
 	sregs.gdt.base = vm->arch.gdt;
-	sregs.gdt.limit = getpagesize() - 1;
+	sregs.gdt.limit = kvm_get_default_gdt_limit();
 
-	sregs.cr0 = X86_CR0_PE | X86_CR0_NE | X86_CR0_PG;
-	sregs.cr4 |= X86_CR4_PAE | X86_CR4_OSFXSR;
-	if (kvm_cpu_has(X86_FEATURE_XSAVE))
-		sregs.cr4 |= X86_CR4_OSXSAVE;
-	sregs.efer |= (EFER_LME | EFER_LMA | EFER_NX);
+	sregs.cr0 = kvm_get_default_cr0();
+	sregs.cr4 |= kvm_get_default_cr4();
+	sregs.efer |= kvm_get_default_efer();
 
 	kvm_seg_set_unusable(&sregs.ldt);
 	kvm_seg_set_kernel_code_64bit(&sregs.cs);
Signature · 1 line
2.51.0.536.g15c5d4f767-goog
#3 lore raw

PATCH v11 3/21 KVM: selftests: Expose function to allocate guest vCPU stack

In reply to Sagi Shahar

TDX guests' registers cannot be initialized directly using vcpu_regs_set(), hence the stack pointer needs to be initialized by the guest itself, running boot code beginning at the reset vector.

Expose the function to allocate the guest stack so that TDX initialization code can allocate it itself and skip the allocation in vm_arch_vcpu_add() in that case.

---
 .../selftests/kvm/include/x86/processor.h        |  2 ++
 tools/testing/selftests/kvm/lib/x86/processor.c  | 16 +++++++++++-----
 2 files changed, 13 insertions(+), 5 deletions(-)
tools/testing/selftests/kvm/include/x86/processor.h +2 -0
index f610c09cadf4..8e75df5e6bc9 100644
@@ -1109,6 +1109,8 @@ static inline void vcpu_clear_cpuid_feature(struct kvm_vcpu *vcpu,
 	vcpu_set_or_clear_cpuid_feature(vcpu, feature, false);
 }
 
+vm_vaddr_t kvm_allocate_vcpu_stack(struct kvm_vm *vm);
+
 uint64_t vcpu_get_msr(struct kvm_vcpu *vcpu, uint64_t msr_index);
 int _vcpu_set_msr(struct kvm_vcpu *vcpu, uint64_t msr_index, uint64_t msr_value);
 
tools/testing/selftests/kvm/lib/x86/processor.c +11 -5
index 83efcf48faad..82369373e843 100644
@@ -658,12 +658,9 @@ void vcpu_arch_set_entry_point(struct kvm_vcpu *vcpu, void *guest_code)
 	vcpu_regs_set(vcpu, &regs);
 }
 
-struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id)
+vm_vaddr_t kvm_allocate_vcpu_stack(struct kvm_vm *vm)
 {
-	struct kvm_mp_state mp_state;
-	struct kvm_regs regs;
 	vm_vaddr_t stack_vaddr;
-	struct kvm_vcpu *vcpu;
 
 	stack_vaddr = __vm_vaddr_alloc(vm, DEFAULT_STACK_PGS * getpagesize(),
 				       DEFAULT_GUEST_STACK_VADDR_MIN,
@@ -684,6 +681,15 @@ struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id)
 		    "__vm_vaddr_alloc() did not provide a page-aligned address");
 	stack_vaddr -= 8;
 
+	return stack_vaddr;
+}
+
+struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id)
+{
+	struct kvm_mp_state mp_state;
+	struct kvm_regs regs;
+	struct kvm_vcpu *vcpu;
+
 	vcpu = __vm_vcpu_add(vm, vcpu_id);
 	vcpu_init_cpuid(vcpu, kvm_get_supported_cpuid());
 	vcpu_init_sregs(vm, vcpu);
@@ -692,7 +698,7 @@ struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id)
 	/* Setup guest general purpose registers */
 	vcpu_regs_get(vcpu, &regs);
 	regs.rflags = regs.rflags | 0x2;
-	regs.rsp = stack_vaddr;
+	regs.rsp = kvm_allocate_vcpu_stack(vm);
 	vcpu_regs_set(vcpu, &regs);
 
 	/* Setup the MP state */
Signature · 1 line
2.51.0.536.g15c5d4f767-goog
#4 lore raw

PATCH v11 4/21 KVM: selftests: Update kvm_init_vm_address_properties() for TDX

In reply to Sagi Shahar

From: Isaku Yamahata <[email protected]>

Let kvm_init_vm_address_properties() initialize vm->arch.{s_bit, tag_mask} similar to SEV.

TDX sets the shared bit based on the guest physical address width and currently supports 48 and 52 widths.

---
 .../selftests/kvm/include/x86/tdx/tdx_util.h       | 14 ++++++++++++++
 tools/testing/selftests/kvm/lib/x86/processor.c    | 12 ++++++++++--
 2 files changed, 24 insertions(+), 2 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/include/x86/tdx/tdx_util.h
tools/testing/selftests/kvm/include/x86/tdx/tdx_util.h new file +14 -0
new file mode 100644
index 000000000000..286d5e3c24b1
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef SELFTESTS_TDX_TDX_UTIL_H
+#define SELFTESTS_TDX_TDX_UTIL_H
+
+#include <stdbool.h>
+
+#include "kvm_util.h"
+
+static inline bool is_tdx_vm(struct kvm_vm *vm)
+{
+	return vm->type == KVM_X86_TDX_VM;
+}
+
+#endif // SELFTESTS_TDX_TDX_UTIL_H
tools/testing/selftests/kvm/lib/x86/processor.c +10 -2
index 82369373e843..2a44831e0cc9 100644
@@ -8,6 +8,7 @@
 #include "kvm_util.h"
 #include "processor.h"
 #include "sev.h"
+#include "tdx/tdx_util.h"
 
 #ifndef NUM_INTERRUPTS
 #define NUM_INTERRUPTS 256
@@ -1160,12 +1161,19 @@ void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits)
 
 void kvm_init_vm_address_properties(struct kvm_vm *vm)
 {
+	uint32_t gpa_bits = kvm_cpu_property(X86_PROPERTY_GUEST_MAX_PHY_ADDR);
+
+	vm->arch.sev_fd = -1;
+
 	if (is_sev_vm(vm)) {
 		vm->arch.sev_fd = open_sev_dev_path_or_exit();
 		vm->arch.c_bit = BIT_ULL(this_cpu_property(X86_PROPERTY_SEV_C_BIT));
 		vm->gpa_tag_mask = vm->arch.c_bit;
-	} else {
-		vm->arch.sev_fd = -1;
+	} else if (is_tdx_vm(vm)) {
+		TEST_ASSERT(gpa_bits == 48 || gpa_bits == 52,
+			    "TDX: bad X86_PROPERTY_GUEST_MAX_PHY_ADDR value: %u", gpa_bits);
+		vm->arch.s_bit = BIT_ULL(gpa_bits - 1);
+		vm->gpa_tag_mask = vm->arch.s_bit;
 	}
 }
 
Signature · 1 line
2.51.0.536.g15c5d4f767-goog
#5 lore raw

PATCH v11 5/21 KVM: selftests: Expose segment definitons to assembly files

In reply to Sagi Shahar

Move kernel segment definitions to a separate file which can be included from assembly files.

---
 .../selftests/kvm/include/x86/processor_asm.h        | 12 ++++++++++++
 tools/testing/selftests/kvm/lib/x86/processor.c      |  5 +----
 2 files changed, 13 insertions(+), 4 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/include/x86/processor_asm.h
tools/testing/selftests/kvm/include/x86/processor_asm.h new file +12 -0
new file mode 100644
index 000000000000..7e5386a85ca8
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Used for storing defines used by both processor.c and assembly code.
+ */
+#ifndef SELFTEST_KVM_PROCESSOR_ASM_H
+#define SELFTEST_KVM_PROCESSOR_ASM_H
+
+#define KERNEL_CS	0x8
+#define KERNEL_DS	0x10
+#define KERNEL_TSS	0x18
+
+#endif  // SELFTEST_KVM_PROCESSOR_ASM_H
tools/testing/selftests/kvm/lib/x86/processor.c +1 -4
index 2a44831e0cc9..623168ea9a44 100644
@@ -7,6 +7,7 @@
 #include "test_util.h"
 #include "kvm_util.h"
 #include "processor.h"
+#include "processor_asm.h"
 #include "sev.h"
 #include "tdx/tdx_util.h"
 
@@ -14,10 +15,6 @@
 #define NUM_INTERRUPTS 256
 #endif
 
-#define KERNEL_CS	0x8
-#define KERNEL_DS	0x10
-#define KERNEL_TSS	0x18
-
 vm_vaddr_t exception_handlers;
 bool host_cpu_is_amd;
 bool host_cpu_is_intel;
Signature · 1 line
2.51.0.536.g15c5d4f767-goog
#6 lore raw

PATCH v11 6/21 KVM: selftests: Add kbuild definitons

In reply to Sagi Shahar

Add kbuild.h that can be used by files under tools/

Definitions are taken from the original definitions at include/linux/kbuild.h

This is needed to expose values from c code to assembly code.

---
 tools/include/linux/kbuild.h | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
 create mode 100644 tools/include/linux/kbuild.h
tools/include/linux/kbuild.h new file +18 -0
new file mode 100644
index 000000000000..62e20ba9380e
@@ -0,0 +1,18 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __TOOLS_LINUX_KBUILD_H
+#define __TOOLS_LINUX_KBUILD_H
+
+#include <stddef.h>
+
+#define DEFINE(sym, val) \
+	asm volatile("\n.ascii \"->" #sym " %0 " #val "\"" : : "i" (val))
+
+#define BLANK() asm volatile("\n.ascii \"->\"" : : )
+
+#define OFFSET(sym, str, mem) \
+	DEFINE(sym, offsetof(struct str, mem))
+
+#define COMMENT(x) \
+	asm volatile("\n.ascii \"->#" x "\"")
+
+#endif /* __TOOLS_LINUX_KBUILD_H */
Signature · 1 line
2.51.0.536.g15c5d4f767-goog
#7 lore raw

PATCH v11 7/21 KVM: selftests: Define structs to pass parameters to TDX boot code

In reply to Sagi Shahar

TDX registers are inaccessible to KVM. Therefore we need a different mechanism to load boot parameters for TDX code. TDX boot code will read the registers values from memory and set the registers manually.

This patch defines the data structures used to communicate between c code and the TDX assembly boot code which will be added in a later patch.

Use kbuild.h to expose the offsets into the structs from c code to assembly code.

---
 tools/testing/selftests/kvm/Makefile.kvm      | 18 +++++
 .../selftests/kvm/include/x86/tdx/td_boot.h   | 69 +++++++++++++++++++
 .../kvm/lib/x86/tdx/td_boot_offsets.c         | 21 ++++++
 3 files changed, 108 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/include/x86/tdx/td_boot.h
 create mode 100644 tools/testing/selftests/kvm/lib/x86/tdx/td_boot_offsets.c
tools/testing/selftests/kvm/Makefile.kvm +18 -0
index 41b40c676d7f..3f93c093b046 100644
@@ -19,6 +19,8 @@ LIBKVM += lib/userfaultfd_util.c
 
 LIBKVM_STRING += lib/string_override.c
 
+LIBKVM_ASM_DEFS += lib/x86/tdx/td_boot_offsets.c
+
 LIBKVM_x86 += lib/x86/apic.c
 LIBKVM_x86 += lib/x86/handlers.S
 LIBKVM_x86 += lib/x86/hyperv.c
@@ -230,6 +232,10 @@ OVERRIDE_TARGETS = 1
 include ../lib.mk
 include ../cgroup/lib/libcgroup.mk
 
+# Enable Kbuild tools.
+include $(top_srcdir)/scripts/Kbuild.include
+include $(top_srcdir)/scripts/Makefile.lib
+
 INSTALL_HDR_PATH = $(top_srcdir)/usr
 LINUX_HDR_PATH = $(INSTALL_HDR_PATH)/include/
 LINUX_TOOL_INCLUDE = $(top_srcdir)/tools/include
@@ -282,6 +288,7 @@ LIBKVM_S := $(filter %.S,$(LIBKVM))
 LIBKVM_C_OBJ := $(patsubst %.c, $(OUTPUT)/%.o, $(LIBKVM_C))
 LIBKVM_S_OBJ := $(patsubst %.S, $(OUTPUT)/%.o, $(LIBKVM_S))
 LIBKVM_STRING_OBJ := $(patsubst %.c, $(OUTPUT)/%.o, $(LIBKVM_STRING))
+LIBKVM_ASM_DEFS_OBJ += $(patsubst %.c, $(OUTPUT)/%.s, $(LIBKVM_ASM_DEFS))
 LIBKVM_OBJS = $(LIBKVM_C_OBJ) $(LIBKVM_S_OBJ) $(LIBKVM_STRING_OBJ) $(LIBCGROUP_O)
 SPLIT_TEST_GEN_PROGS := $(patsubst %, $(OUTPUT)/%, $(SPLIT_TESTS))
 SPLIT_TEST_GEN_OBJ := $(patsubst %, $(OUTPUT)/$(ARCH)/%.o, $(SPLIT_TESTS))
@@ -308,6 +315,7 @@ $(SPLIT_TEST_GEN_OBJ): $(OUTPUT)/$(ARCH)/%.o: $(ARCH)/%.c
 
 EXTRA_CLEAN += $(GEN_HDRS) \
 	       $(LIBKVM_OBJS) \
+	       $(LIBKVM_ASM_DEFS_OBJ) \
 	       $(SPLIT_TEST_GEN_OBJ) \
 	       $(TEST_DEP_FILES) \
 	       $(TEST_GEN_OBJ) \
@@ -319,18 +327,28 @@ $(LIBKVM_C_OBJ): $(OUTPUT)/%.o: %.c $(GEN_HDRS)
 $(LIBKVM_S_OBJ): $(OUTPUT)/%.o: %.S $(GEN_HDRS)
 	$(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@
 
+$(LIBKVM_ASM_DEFS_OBJ): $(OUTPUT)/%.s: %.c FORCE
+	$(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -S $< -o $@
+
 # Compile the string overrides as freestanding to prevent the compiler from
 # generating self-referential code, e.g. without "freestanding" the compiler may
 # "optimize" memcmp() by invoking memcmp(), thus causing infinite recursion.
 $(LIBKVM_STRING_OBJ): $(OUTPUT)/%.o: %.c
 	$(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c -ffreestanding $< -o $@
 
+$(OUTPUT)/include/x86/tdx/td_boot_offsets.h: $(OUTPUT)/lib/x86/tdx/td_boot_offsets.s FORCE
+	$(call filechk,offsets,__TDX_BOOT_OFFSETS_H__)
+
+EXTRA_CLEAN += $(OUTPUT)/include/x86/tdx/td_boot_offsets.h
+
 $(shell mkdir -p $(sort $(dir $(TEST_GEN_PROGS))))
 $(SPLIT_TEST_GEN_OBJ): $(GEN_HDRS)
 $(TEST_GEN_PROGS): $(LIBKVM_OBJS)
 $(TEST_GEN_PROGS_EXTENDED): $(LIBKVM_OBJS)
 $(TEST_GEN_OBJ): $(GEN_HDRS)
 
+FORCE:
+
 cscope: include_paths = $(LINUX_TOOL_INCLUDE) $(LINUX_HDR_PATH) include lib ..
 cscope:
 	$(RM) cscope.*
tools/testing/selftests/kvm/include/x86/tdx/td_boot.h new file +69 -0

69 lines not on this page · raw message

tools/testing/selftests/kvm/lib/x86/tdx/td_boot_offsets.c new file +21 -0
new file mode 100644
index 000000000000..7f76a3585b99
@@ -0,0 +1,21 @@
+// SPDX-License-Identifier: GPL-2.0
+#define COMPILE_OFFSETS
+
+#include <linux/kbuild.h>
+
+#include "tdx/td_boot.h"
+
+static void __attribute__((used)) common(void)
+{
+	OFFSET(TD_BOOT_PARAMETERS_CR0, td_boot_parameters, cr0);
+	OFFSET(TD_BOOT_PARAMETERS_CR3, td_boot_parameters, cr3);
+	OFFSET(TD_BOOT_PARAMETERS_CR4, td_boot_parameters, cr4);
+	OFFSET(TD_BOOT_PARAMETERS_GDT, td_boot_parameters, gdtr);
+	OFFSET(TD_BOOT_PARAMETERS_IDT, td_boot_parameters, idtr);
+	OFFSET(TD_BOOT_PARAMETERS_PER_VCPU, td_boot_parameters, per_vcpu);
+	OFFSET(TD_PER_VCPU_PARAMETERS_ESP_GVA, td_per_vcpu_parameters, esp_gva);
+	OFFSET(TD_PER_VCPU_PARAMETERS_GUEST_CODE, td_per_vcpu_parameters,
+	       guest_code);
+	DEFINE(SIZEOF_TD_PER_VCPU_PARAMETERS,
+	       sizeof(struct td_per_vcpu_parameters));
+}
Signature · 1 line
2.51.0.536.g15c5d4f767-goog
#8 lore raw

PATCH v11 8/21 KVM: selftests: Add TDX boot code

In reply to Sagi Shahar

From: Erdem Aktas <[email protected]>

Add code to boot a TDX test VM. Since TDX registers are inaccessible to KVM, the boot code loads the relevant values from memory into the registers before jumping to the guest code.

---
 tools/testing/selftests/kvm/Makefile.kvm      |  3 +
 .../selftests/kvm/include/x86/tdx/td_boot.h   |  5 ++
 .../kvm/include/x86/tdx/td_boot_asm.h         | 16 +++++
 .../selftests/kvm/lib/x86/tdx/td_boot.S       | 60 +++++++++++++++++++
 4 files changed, 84 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/include/x86/tdx/td_boot_asm.h
 create mode 100644 tools/testing/selftests/kvm/lib/x86/tdx/td_boot.S
tools/testing/selftests/kvm/Makefile.kvm +3 -0
index 3f93c093b046..d11d02e17cc5 100644
@@ -31,6 +31,7 @@ LIBKVM_x86 += lib/x86/sev.c
 LIBKVM_x86 += lib/x86/svm.c
 LIBKVM_x86 += lib/x86/ucall.c
 LIBKVM_x86 += lib/x86/vmx.c
+LIBKVM_x86 += lib/x86/tdx/td_boot.S
 
 LIBKVM_arm64 += lib/arm64/gic.c
 LIBKVM_arm64 += lib/arm64/gic_v3.c
@@ -336,6 +337,8 @@ $(LIBKVM_ASM_DEFS_OBJ): $(OUTPUT)/%.s: %.c FORCE
 $(LIBKVM_STRING_OBJ): $(OUTPUT)/%.o: %.c
 	$(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c -ffreestanding $< -o $@
 
+$(OUTPUT)/lib/x86/tdx/td_boot.o: $(OUTPUT)/include/x86/tdx/td_boot_offsets.h
+
 $(OUTPUT)/include/x86/tdx/td_boot_offsets.h: $(OUTPUT)/lib/x86/tdx/td_boot_offsets.s FORCE
 	$(call filechk,offsets,__TDX_BOOT_OFFSETS_H__)
 
tools/testing/selftests/kvm/include/x86/tdx/td_boot.h +5 -0
index 32631645fe13..a590516dd83c 100644
@@ -66,4 +66,9 @@ struct td_boot_parameters {
 	struct td_per_vcpu_parameters per_vcpu[];
 };
 
+void td_boot(void);
+void td_boot_code_end(void);
+
+#define TD_BOOT_CODE_SIZE (td_boot_code_end - td_boot)
+
 #endif /* SELFTEST_TDX_TD_BOOT_H */
tools/testing/selftests/kvm/include/x86/tdx/td_boot_asm.h new file +16 -0
new file mode 100644
index 000000000000..10b4b527595c
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef SELFTEST_TDX_TD_BOOT_ASM_H
+#define SELFTEST_TDX_TD_BOOT_ASM_H
+
+/*
+ * GPA where TD boot parameters will be loaded.
+ *
+ * TD_BOOT_PARAMETERS_GPA is arbitrarily chosen to
+ *
+ * + be within the 4GB address space
+ * + provide enough contiguous memory for the struct td_boot_parameters such
+ *   that there is one struct td_per_vcpu_parameters for KVM_MAX_VCPUS
+ */
+#define TD_BOOT_PARAMETERS_GPA 0xffff0000
+
+#endif  // SELFTEST_TDX_TD_BOOT_ASM_H
tools/testing/selftests/kvm/lib/x86/tdx/td_boot.S new file +60 -0

60 lines not on this page · raw message

Signature · 1 line
2.51.0.536.g15c5d4f767-goog
#9 lore raw

PATCH v11 9/21 KVM: selftests: Set up TDX boot code region

In reply to Sagi Shahar

Add memory for TDX boot code in a separate memslot.

Use virt_map() to get identity map in this memory region to allow for seamless transition from paging disabled to paging enabled code.

Copy the boot code into the memory region and set up the reset vector at this point. While it's possible to separate the memory allocation and boot code initialization into separate functions, having all the calculations for memory size and offsets in one place simplifies the code and avoids duplications.

Handcode the reset vector as suggested by Sean Christopherson.

---
 tools/testing/selftests/kvm/Makefile.kvm      |  1 +
 .../selftests/kvm/include/x86/tdx/tdx_util.h  |  2 +
 .../selftests/kvm/lib/x86/tdx/tdx_util.c      | 54 +++++++++++++++++++
 3 files changed, 57 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index d11d02e17cc5..52c90f1c0484 100644
@@ -31,6 +31,7 @@ LIBKVM_x86 += lib/x86/sev.c
 LIBKVM_x86 += lib/x86/svm.c
 LIBKVM_x86 += lib/x86/ucall.c
 LIBKVM_x86 += lib/x86/vmx.c
+LIBKVM_x86 += lib/x86/tdx/tdx_util.c
 LIBKVM_x86 += lib/x86/tdx/td_boot.S
 
 LIBKVM_arm64 += lib/arm64/gic.c
tools/testing/selftests/kvm/include/x86/tdx/tdx_util.h +2 -0
index 286d5e3c24b1..ec05bcd59145 100644
@@ -11,4 +11,6 @@ static inline bool is_tdx_vm(struct kvm_vm *vm)
 	return vm->type == KVM_X86_TDX_VM;
 }
 
+void vm_tdx_setup_boot_code_region(struct kvm_vm *vm);
+
 #endif // SELFTESTS_TDX_TDX_UTIL_H
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c new file +54 -0

54 lines not on this page · raw message

Signature · 1 line
2.51.0.536.g15c5d4f767-goog
#10 lore raw

PATCH v11 10/21 KVM: selftests: Set up TDX boot parameters region

In reply to Sagi Shahar

Allocate memory for TDX boot parameters and define the utility functions necessary to fill this memory with the boot parameters.

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

Changes from v10:
* Removed code for setting up X86_CR4_OSXMMEXCPT bit. At least for now it is not needed and the test pass without it.

---
 .../selftests/kvm/include/x86/tdx/tdx_util.h  |  4 ++
 .../selftests/kvm/lib/x86/tdx/tdx_util.c      | 72 +++++++++++++++++++
 2 files changed, 76 insertions(+)
tools/testing/selftests/kvm/include/x86/tdx/tdx_util.h +4 -0
index ec05bcd59145..dafdc7e46abe 100644
@@ -12,5 +12,9 @@ static inline bool is_tdx_vm(struct kvm_vm *vm)
 }
 
 void vm_tdx_setup_boot_code_region(struct kvm_vm *vm);
+void vm_tdx_setup_boot_parameters_region(struct kvm_vm *vm, uint32_t nr_runnable_vcpus);
+void vm_tdx_load_common_boot_parameters(struct kvm_vm *vm);
+void vm_tdx_load_vcpu_boot_parameters(struct kvm_vm *vm, struct kvm_vcpu *vcpu);
+void vm_tdx_set_vcpu_entry_point(struct kvm_vcpu *vcpu, void *guest_code);
 
 #endif // SELFTESTS_TDX_TDX_UTIL_H
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +72 -0

85 lines not on this page · raw message

Signature · 1 line
2.51.0.536.g15c5d4f767-goog
#11 lore raw

PATCH v11 11/21 KVM: selftests: Add helper to initialize TDX VM

In reply to Sagi Shahar

KVM_TDX_INIT_VM needs to be called after KVM_CREATE_VM and before creating any VCPUs, thus before KVM_SET_CPUID2. KVM_TDX_INIT_VM accepts the CPUID values directly.

Since KVM_GET_CPUID2 can't be used at this point, calculate the CPUID values manually by using kvm_get_supported_cpuid() and filter the returned CPUIDs against the supported CPUID values read from the TDX module.

---
 .../selftests/kvm/include/x86/tdx/tdx_util.h  |  54 +++++++
 .../selftests/kvm/lib/x86/tdx/tdx_util.c      | 132 ++++++++++++++++++
 2 files changed, 186 insertions(+)
tools/testing/selftests/kvm/include/x86/tdx/tdx_util.h +54 -0
index dafdc7e46abe..a2509959c7ce 100644
@@ -11,6 +11,60 @@ static inline bool is_tdx_vm(struct kvm_vm *vm)
 	return vm->type == KVM_X86_TDX_VM;
 }
 
+/*
+ * TDX ioctls
+ */
+
+#define __vm_tdx_vm_ioctl(vm, cmd, metadata, arg)			\
+({									\
+	int r;								\
+									\
+	union {								\
+		struct kvm_tdx_cmd c;					\
+		unsigned long raw;					\
+	} tdx_cmd = { .c = {						\
+		.id = (cmd),						\
+		.flags = (uint32_t)(metadata),				\
+		.data = (uint64_t)(arg),				\
+	} };								\
+									\
+	r = __vm_ioctl(vm, KVM_MEMORY_ENCRYPT_OP, &tdx_cmd.raw);	\
+	r ?: tdx_cmd.c.hw_error;					\
+})
+
+#define vm_tdx_vm_ioctl(vm, cmd, flags, arg)				\
+({									\
+	int ret = __vm_tdx_vm_ioctl(vm, cmd, flags, arg);		\
+									\
+	__TEST_ASSERT_VM_VCPU_IOCTL(!ret, #cmd,	ret, vm);		\
+})
+
+#define __vm_tdx_vcpu_ioctl(vcpu, cmd, metadata, arg)			\
+({									\
+	int r;								\
+									\
+	union {								\
+		struct kvm_tdx_cmd c;					\
+		unsigned long raw;					\
+	} tdx_cmd = { .c = {						\
+		.id = (cmd),						\
+		.flags = (uint32_t)(metadata),				\
+		.data = (uint64_t)(arg),				\
+	} };								\
+									\
+	r = __vcpu_ioctl(vcpu, KVM_MEMORY_ENCRYPT_OP, &tdx_cmd.raw);	\
+	r ?: tdx_cmd.c.hw_error;					\
+})
+
+#define vm_tdx_vcpu_ioctl(vcpu, cmd, flags, arg)			\
+({									\
+	int ret = __vm_tdx_vcpu_ioctl(vcpu, cmd, flags, arg);		\
+									\
+	__TEST_ASSERT_VM_VCPU_IOCTL(!ret, #cmd, ret, (vcpu)->vm);	\
+})
+
+void vm_tdx_init_vm(struct kvm_vm *vm, uint64_t attributes);
+
 void vm_tdx_setup_boot_code_region(struct kvm_vm *vm);
 void vm_tdx_setup_boot_parameters_region(struct kvm_vm *vm, uint32_t nr_runnable_vcpus);
 void vm_tdx_load_common_boot_parameters(struct kvm_vm *vm);
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +132 -0

135 lines not on this page · raw message

Signature · 1 line
2.51.0.536.g15c5d4f767-goog
#12 lore raw

PATCH v11 12/21 KVM: selftests: TDX: Use KVM_TDX_CAPABILITIES to validate TDs' attribute configuration

In reply to Sagi Shahar

From: Isaku Yamahata <[email protected]>

Make sure that all the attributes enabled by the test are reported as supported by the TDX module.

This also exercises the KVM_TDX_CAPABILITIES ioctl.

---
 tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +14 -0
index 7a622b4810b1..2551b3eac8f8 100644
@@ -231,6 +231,18 @@ static void vm_tdx_filter_cpuid(struct kvm_vm *vm,
 	free(tdx_cap);
 }
 
+static void tdx_check_attributes(struct kvm_vm *vm, uint64_t attributes)
+{
+	struct kvm_tdx_capabilities *tdx_cap;
+
+	tdx_cap = tdx_read_capabilities(vm);
+
+	/* Make sure all the attributes are reported as supported */
+	TEST_ASSERT_EQ(attributes & tdx_cap->supported_attrs, attributes);
+
+	free(tdx_cap);
+}
+
 void vm_tdx_init_vm(struct kvm_vm *vm, uint64_t attributes)
 {
 	struct kvm_tdx_init_vm *init_vm;
@@ -250,6 +262,8 @@ void vm_tdx_init_vm(struct kvm_vm *vm, uint64_t attributes)
 	memcpy(&init_vm->cpuid, cpuid, kvm_cpuid2_size(cpuid->nent));
 	free(cpuid);
 
+	tdx_check_attributes(vm, attributes);
+
 	init_vm->attributes = attributes;
 
 	vm_tdx_vm_ioctl(vm, KVM_TDX_INIT_VM, 0, init_vm);
Signature · 1 line
2.51.0.536.g15c5d4f767-goog
#13 lore raw

PATCH v11 13/21 KVM: selftests: Add helpers to init TDX memory and finalize VM

In reply to Sagi Shahar

From: Ackerley Tng <[email protected]>

TDX protected memory needs to be measured and encrypted before it can be used by the guest. Traverse the VM's memory regions and initialize all the protected ranges by calling KVM_TDX_INIT_MEM_REGION.

Once all the memory is initialized, the VM can be finalized by calling KVM_TDX_FINALIZE_VM.

---
 .../selftests/kvm/include/x86/tdx/tdx_util.h  |  2 +
 .../selftests/kvm/lib/x86/tdx/tdx_util.c      | 58 +++++++++++++++++++
 2 files changed, 60 insertions(+)
tools/testing/selftests/kvm/include/x86/tdx/tdx_util.h +2 -0
index a2509959c7ce..2467b6c35557 100644
@@ -71,4 +71,6 @@ void vm_tdx_load_common_boot_parameters(struct kvm_vm *vm);
 void vm_tdx_load_vcpu_boot_parameters(struct kvm_vm *vm, struct kvm_vcpu *vcpu);
 void vm_tdx_set_vcpu_entry_point(struct kvm_vcpu *vcpu, void *guest_code);
 
+void vm_tdx_finalize(struct kvm_vm *vm);
+
 #endif // SELFTESTS_TDX_TDX_UTIL_H
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +58 -0

61 lines not on this page · raw message

Signature · 1 line
2.51.0.536.g15c5d4f767-goog
#14 lore raw

Re: [PATCH v11 13/21] KVM: selftests: Add helpers to init TDX memory and finalize VM

Quoted 2 lines · Sagi Shahar

From: Ackerley Tng <[email protected]>

[snip]

Quoted 9 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +1 -0
index 2551b3eac8f8..53cfadeff8de 100644
@@ -270,3 +270,61 @@ void vm_tdx_init_vm(struct kvm_vm *vm, uint64_t attributes)
 
 	free(init_vm);
 }
+

[snip]

tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c

Quoted 2 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +2 -0
+
+void vm_tdx_finalize(struct kvm_vm *vm)

Why is this not a new kvm_arch_vm_finalize_vcpu() call?

Ira

Quoted 7 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +4 -0
+{
+	load_td_private_memory(vm);
+	vm_tdx_vm_ioctl(vm, KVM_TDX_FINALIZE_VM, 0, NULL);
+}
Signature · 1 line
2.51.0.536.g15c5d4f767-goog
#15 lore raw

Re: [PATCH v11 13/21] KVM: selftests: Add helpers to init TDX memory and finalize VM

Quoted 19 lines · Ira Weiny
Quoted 2 lines · Sagi Shahar

From: Ackerley Tng <[email protected]>

[snip]

Quoted 9 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +1 -0
index 2551b3eac8f8..53cfadeff8de 100644
@@ -270,3 +270,61 @@ void vm_tdx_init_vm(struct kvm_vm *vm, uint64_t attributes)
 
      free(init_vm);
 }
+

[snip]

Quoted 4 lines

tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c

Quoted 2 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +2 -0
+
+void vm_tdx_finalize(struct kvm_vm *vm)

Why is this not a new kvm_arch_vm_finalize_vcpu() call?

What do you mean?

Quoted 10 lines

Ira

Quoted 7 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +4 -1
+{
+     load_td_private_memory(vm);
+     vm_tdx_vm_ioctl(vm, KVM_TDX_FINALIZE_VM, 0, NULL);
+}
--

2.51.0.536.g15c5d4f767-goog

#16 lore raw

Re: [PATCH v11 13/21] KVM: selftests: Add helpers to init TDX memory and finalize VM

Quoted 20 lines · Sagi Shahar
Quoted 19 lines · Ira Weiny
Quoted 2 lines · Sagi Shahar

From: Ackerley Tng <[email protected]>

[snip]

Quoted 9 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +1 -0
index 2551b3eac8f8..53cfadeff8de 100644
@@ -270,3 +270,61 @@ void vm_tdx_init_vm(struct kvm_vm *vm, uint64_t attributes)
 
      free(init_vm);
 }
+

[snip]

Quoted 6 lines
Quoted 4 lines

tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c

Quoted 2 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +2 -0
+
+void vm_tdx_finalize(struct kvm_vm *vm)

Why is this not a new kvm_arch_vm_finalize_vcpu() call?

What do you mean?

Ira is pointing out that upstream now has kvm_arch_vm_finalize_vcpus(), so you can (and I agree, should) implement that for x86.c, and do vm_tdx_finalize() from there (based on the VM shape) instead of requiring the caller to manually finalize the TD.

Unlike SEV, where userspace can manipulate guest state prior to LAUNCH, TDX guest state is unreachable from time zero, i.e. there is unlikely to be many (any?) use cases where a selftest wants to do something between creating vCPUs and finalizing the TD.

#17 lore raw

Re: [PATCH v11 13/21] KVM: selftests: Add helpers to init TDX memory and finalize VM

Quoted 34 lines · Sean Christopherson
Quoted 20 lines · Sagi Shahar
Quoted 19 lines · Ira Weiny
Quoted 2 lines · Sagi Shahar

From: Ackerley Tng <[email protected]>

[snip]

Quoted 9 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +1 -0
index 2551b3eac8f8..53cfadeff8de 100644
@@ -270,3 +270,61 @@ void vm_tdx_init_vm(struct kvm_vm *vm, uint64_t attributes)
 
      free(init_vm);
 }
+

[snip]

Quoted 6 lines
Quoted 4 lines

tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c

Quoted 2 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +2 -0
+
+void vm_tdx_finalize(struct kvm_vm *vm)

Why is this not a new kvm_arch_vm_finalize_vcpu() call?

What do you mean?

Ira is pointing out that upstream now has kvm_arch_vm_finalize_vcpus(), so you can (and I agree, should) implement that for x86.c, and do vm_tdx_finalize() from there (based on the VM shape) instead of requiring the caller to manually finalize the TD.

Quoted 4 lines

Unlike SEV, where userspace can manipulate guest state prior to LAUNCH, TDX guest state is unreachable from time zero, i.e. there is unlikely to be many (any?) use cases where a selftest wants to do something between creating vCPUs and finalizing the TD.

There are actually a few use cases for calling vm_tdx_finalize seperately from the create phase. One such case is when a user wants to add additional memslots for testing shared memory conversions. Another one is installing interrupt handlers. But these are probably rare enough that they can call __vm_create() and vm_vcpu_add() manually instead of using the wrapper

#18 lore raw

Re: [PATCH v11 13/21] KVM: selftests: Add helpers to init TDX memory and finalize VM

Quoted 35 lines · Sagi Shahar
Quoted 34 lines · Sean Christopherson
Quoted 20 lines · Sagi Shahar
Quoted 19 lines · Ira Weiny
Quoted 2 lines · Sagi Shahar

From: Ackerley Tng <[email protected]>

[snip]

Quoted 9 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +1 -0
index 2551b3eac8f8..53cfadeff8de 100644
@@ -270,3 +270,61 @@ void vm_tdx_init_vm(struct kvm_vm *vm, uint64_t attributes)
 
      free(init_vm);
 }
+

[snip]

Quoted 6 lines
Quoted 4 lines

tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c

Quoted 2 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +2 -0
+
+void vm_tdx_finalize(struct kvm_vm *vm)

Why is this not a new kvm_arch_vm_finalize_vcpu() call?

What do you mean?

Ira is pointing out that upstream now has kvm_arch_vm_finalize_vcpus(), so you can (and I agree, should) implement that for x86.c, and do vm_tdx_finalize() from there (based on the VM shape) instead of requiring the caller to manually finalize the TD.

Quoted 8 lines
Quoted 4 lines

Unlike SEV, where userspace can manipulate guest state prior to LAUNCH, TDX guest state is unreachable from time zero, i.e. there is unlikely to be many (any?) use cases where a selftest wants to do something between creating vCPUs and finalizing the TD.

There are actually a few use cases for calling vm_tdx_finalize seperately from the create phase. One such case is when a user wants to add additional memslots for testing shared memory conversions.

Why does creating memslots need to be done before finalizing the TD? The extra memslots shouldn't _need_ to be included in the initial image, and I don't recall anything in the kernel that prevents adding a memslot after the TD is initialized.

Quoted 3 lines

Another one is installing interrupt handlers. But these are probably rare enough that they can call __vm_create() and vm_vcpu_add() manually instead of using the wrapper

Hmm, for installing interrupt handlers, we could handle that by adding a guest variant, e.g. so that the guest can install/remove exception handlers after the initial image is finalized. We probably want that anyways, otherwise tests would be unable to manipulate handlers throughout a test.

In fact, it's tempting to kill off vm_install_exception_handler() in x86 entirely, but that's probably not worth the churn (and some tests would be made more complex).

#19 lore raw

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

In reply to Sagi Shahar

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.

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

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(+)
tools/testing/selftests/kvm/lib/x86/processor.c +5 -0
index 623168ea9a44..63b751253d1b 100644
@@ -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;
Signature · 1 line
2.51.0.536.g15c5d4f767-goog
#20 lore raw

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

In reply to Sagi Shahar

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.

---
 tools/testing/selftests/kvm/lib/kvm_util.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
tools/testing/selftests/kvm/lib/kvm_util.c +8 -1
index b4c8702ba4bd..d8a944b5ada3 100644
@@ -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);
 
 	/*
Signature · 1 line
2.51.0.536.g15c5d4f767-goog
#21 lore raw

PATCH v11 16/21 KVM: selftests: Call KVM_TDX_INIT_VCPU when creating a new TDX vcpu

In reply to Sagi Shahar

TDX VMs need to issue the KVM_TDX_INIT_VCPU ioctl for each vcpu after vcpu creation.

Since the cpuids for TD are managed by the TDX module, read the values virtualized for the TD using KVM_TDX_GET_CPUID and set them in kvm using KVM_SET_CPUID2 so that kvm has an accurate view of the VM cpuid values.

---
 .../testing/selftests/kvm/lib/x86/processor.c | 35 ++++++++++++++-----
 1 file changed, 27 insertions(+), 8 deletions(-)
tools/testing/selftests/kvm/lib/x86/processor.c +27 -8
index 63b751253d1b..f7ddea3b2044 100644
@@ -687,6 +687,19 @@ vm_vaddr_t kvm_allocate_vcpu_stack(struct kvm_vm *vm)
 	return stack_vaddr;
 }
 
+static void vm_tdx_vcpu_add(struct kvm_vm *vm, struct kvm_vcpu *vcpu)
+{
+	struct kvm_cpuid2 *cpuid;
+
+	cpuid = allocate_kvm_cpuid2(MAX_NR_CPUID_ENTRIES);
+	vm_tdx_vcpu_ioctl(vcpu, KVM_TDX_GET_CPUID, 0, cpuid);
+	vcpu_init_cpuid(vcpu, cpuid);
+	free(cpuid);
+	vm_tdx_vcpu_ioctl(vcpu, KVM_TDX_INIT_VCPU, 0, NULL);
+
+	vm_tdx_load_vcpu_boot_parameters(vm, vcpu);
+}
+
 struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id)
 {
 	struct kvm_mp_state mp_state;
@@ -694,15 +707,21 @@ struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id)
 	struct kvm_vcpu *vcpu;
 
 	vcpu = __vm_vcpu_add(vm, vcpu_id);
-	vcpu_init_cpuid(vcpu, kvm_get_supported_cpuid());
-	vcpu_init_sregs(vm, vcpu);
-	vcpu_init_xcrs(vm, vcpu);
 
-	/* Setup guest general purpose registers */
-	vcpu_regs_get(vcpu, &regs);
-	regs.rflags = regs.rflags | 0x2;
-	regs.rsp = kvm_allocate_vcpu_stack(vm);
-	vcpu_regs_set(vcpu, &regs);
+	if (is_tdx_vm(vm)) {
+		vm_tdx_vcpu_add(vm, vcpu);
+	} else {
+		vcpu_init_cpuid(vcpu, kvm_get_supported_cpuid());
+
+		vcpu_init_sregs(vm, vcpu);
+		vcpu_init_xcrs(vm, vcpu);
+
+		/* Setup guest general purpose registers */
+		vcpu_regs_get(vcpu, &regs);
+		regs.rflags = regs.rflags | 0x2;
+		regs.rsp = kvm_allocate_vcpu_stack(vm);
+		vcpu_regs_set(vcpu, &regs);
+	}
 
 	/* Setup the MP state */
 	mp_state.mp_state = 0;
Signature · 1 line
2.51.0.536.g15c5d4f767-goog
#22 lore raw

PATCH v11 17/21 KVM: selftests: Set entry point for TDX guest code

In reply to Sagi Shahar

Since the rip register is inaccessible for TDX VMs, we need a different way to set the guest entry point for TDX VMs. This is done by writing the guest code address to a predefined location in the guest memory and loading it into rip as part of the TDX boot code.

---
 tools/testing/selftests/kvm/lib/x86/processor.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
tools/testing/selftests/kvm/lib/x86/processor.c +7 -3
index f7ddea3b2044..92a9cac18c56 100644
@@ -656,9 +656,13 @@ void vcpu_arch_set_entry_point(struct kvm_vcpu *vcpu, void *guest_code)
 {
 	struct kvm_regs regs;
 
-	vcpu_regs_get(vcpu, &regs);
-	regs.rip = (unsigned long) guest_code;
-	vcpu_regs_set(vcpu, &regs);
+	if (is_tdx_vm(vcpu->vm))
+		vm_tdx_set_vcpu_entry_point(vcpu, guest_code);
+	else {
+		vcpu_regs_get(vcpu, &regs);
+		regs.rip = (unsigned long) guest_code;
+		vcpu_regs_set(vcpu, &regs);
+	}
 }
 
 vm_vaddr_t kvm_allocate_vcpu_stack(struct kvm_vm *vm)
Signature · 1 line
2.51.0.536.g15c5d4f767-goog
#23 lore raw

PATCH v11 18/21 KVM: selftests: Add support for TDX TDCALL from guest

In reply to Sagi Shahar

From: Erdem Aktas <[email protected]>

Add support for TDX guests to issue TDCALLs to the TDX module.

---
 tools/testing/selftests/kvm/Makefile.kvm      |  8 ++
 .../selftests/kvm/include/x86/tdx/tdcall.h    | 34 +++++++
 .../selftests/kvm/lib/x86/tdx/tdcall.S        | 93 +++++++++++++++++++
 .../kvm/lib/x86/tdx/tdcall_offsets.c          | 16 ++++
 4 files changed, 151 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/include/x86/tdx/tdcall.h
 create mode 100644 tools/testing/selftests/kvm/lib/x86/tdx/tdcall.S
 create mode 100644 tools/testing/selftests/kvm/lib/x86/tdx/tdcall_offsets.c
tools/testing/selftests/kvm/Makefile.kvm +8 -0
index 52c90f1c0484..2d4fd68984a0 100644
@@ -20,6 +20,7 @@ LIBKVM += lib/userfaultfd_util.c
 LIBKVM_STRING += lib/string_override.c
 
 LIBKVM_ASM_DEFS += lib/x86/tdx/td_boot_offsets.c
+LIBKVM_ASM_DEFS += lib/x86/tdx/tdcall_offsets.c
 
 LIBKVM_x86 += lib/x86/apic.c
 LIBKVM_x86 += lib/x86/handlers.S
@@ -33,6 +34,7 @@ LIBKVM_x86 += lib/x86/ucall.c
 LIBKVM_x86 += lib/x86/vmx.c
 LIBKVM_x86 += lib/x86/tdx/tdx_util.c
 LIBKVM_x86 += lib/x86/tdx/td_boot.S
+LIBKVM_x86 += lib/x86/tdx/tdcall.S
 
 LIBKVM_arm64 += lib/arm64/gic.c
 LIBKVM_arm64 += lib/arm64/gic_v3.c
@@ -343,7 +345,13 @@ $(OUTPUT)/lib/x86/tdx/td_boot.o: $(OUTPUT)/include/x86/tdx/td_boot_offsets.h
 $(OUTPUT)/include/x86/tdx/td_boot_offsets.h: $(OUTPUT)/lib/x86/tdx/td_boot_offsets.s FORCE
 	$(call filechk,offsets,__TDX_BOOT_OFFSETS_H__)
 
+$(OUTPUT)/lib/x86/tdx/tdcall.o: $(OUTPUT)/include/x86/tdx/tdcall_offsets.h
+
+$(OUTPUT)/include/x86/tdx/tdcall_offsets.h: $(OUTPUT)/lib/x86/tdx/tdcall_offsets.s FORCE
+	$(call filechk,offsets,__TDCALL__OFFSETS_H__)
+
 EXTRA_CLEAN += $(OUTPUT)/include/x86/tdx/td_boot_offsets.h
+EXTRA_CLEAN += $(OUTPUT)/include/x86/tdx/tdcall_offsets.h
 
 $(shell mkdir -p $(sort $(dir $(TEST_GEN_PROGS))))
 $(SPLIT_TEST_GEN_OBJ): $(GEN_HDRS)
tools/testing/selftests/kvm/include/x86/tdx/tdcall.h new file +34 -0
new file mode 100644
index 000000000000..60c70646f876
@@ -0,0 +1,34 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/* Adapted from arch/x86/include/asm/shared/tdx.h */
+
+#ifndef SELFTESTS_TDX_TDCALL_H
+#define SELFTESTS_TDX_TDCALL_H
+
+#include <linux/bits.h>
+
+#define TDX_TDCALL_HAS_OUTPUT BIT(0)
+
+#ifndef __ASSEMBLY__
+
+#include <linux/types.h>
+
+/*
+ * Used in __tdx_tdcall() to pass down and get back registers' values of
+ * the TDCALL instruction when requesting services from the VMM.
+ *
+ * This is a software only structure and not part of the TDX module/VMM ABI.
+ */
+struct tdx_tdcall_args {
+	u64 r10;
+	u64 r11;
+	u64 r12;
+	u64 r13;
+	u64 r14;
+	u64 r15;
+};
+
+/* Used to request services from the VMM */
+u64 __tdx_tdcall(struct tdx_tdcall_args *args, unsigned long flags);
+
+#endif // __ASSEMBLY__
+#endif // SELFTESTS_TDX_TDCALL_H
tools/testing/selftests/kvm/lib/x86/tdx/tdcall.S new file +93 -0

93 lines not on this page · raw message

tools/testing/selftests/kvm/lib/x86/tdx/tdcall_offsets.c new file +16 -0
new file mode 100644
index 000000000000..dcd4457be6e5
@@ -0,0 +1,16 @@
+// SPDX-License-Identifier: GPL-2.0
+#define COMPILE_OFFSETS
+
+#include <linux/kbuild.h>
+
+#include "tdx/tdcall.h"
+
+static void __attribute__((used)) common(void)
+{
+	OFFSET(TDX_TDCALL_R10, tdx_tdcall_args, r10);
+	OFFSET(TDX_TDCALL_R11, tdx_tdcall_args, r11);
+	OFFSET(TDX_TDCALL_R12, tdx_tdcall_args, r12);
+	OFFSET(TDX_TDCALL_R13, tdx_tdcall_args, r13);
+	OFFSET(TDX_TDCALL_R14, tdx_tdcall_args, r14);
+	OFFSET(TDX_TDCALL_R15, tdx_tdcall_args, r15);
+}
Signature · 1 line
2.51.0.536.g15c5d4f767-goog
#24 lore raw

PATCH v11 19/21 KVM: selftests: Add wrapper for TDX MMIO from guest

In reply to Sagi Shahar

Add utility function to issue MMIO TDCALL from TDX guests.

---
 tools/testing/selftests/kvm/Makefile.kvm      |  1 +
 .../selftests/kvm/include/x86/tdx/tdx.h       | 14 +++++++++++
 tools/testing/selftests/kvm/lib/x86/tdx/tdx.c | 23 +++++++++++++++++++
 3 files changed, 38 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/include/x86/tdx/tdx.h
 create mode 100644 tools/testing/selftests/kvm/lib/x86/tdx/tdx.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index 2d4fd68984a0..1a73e08c8437 100644
@@ -35,6 +35,7 @@ LIBKVM_x86 += lib/x86/vmx.c
 LIBKVM_x86 += lib/x86/tdx/tdx_util.c
 LIBKVM_x86 += lib/x86/tdx/td_boot.S
 LIBKVM_x86 += lib/x86/tdx/tdcall.S
+LIBKVM_x86 += lib/x86/tdx/tdx.c
 
 LIBKVM_arm64 += lib/arm64/gic.c
 LIBKVM_arm64 += lib/arm64/gic_v3.c
tools/testing/selftests/kvm/include/x86/tdx/tdx.h new file +14 -0
new file mode 100644
index 000000000000..22b096402998
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef SELFTESTS_TDX_TDX_H
+#define SELFTESTS_TDX_TDX_H
+
+#include <stdint.h>
+
+/* MMIO direction */
+#define MMIO_READ	0
+#define MMIO_WRITE	1
+
+uint64_t tdg_vp_vmcall_ve_request_mmio_write(uint64_t address, uint64_t size,
+					     uint64_t data_in);
+
+#endif // SELFTESTS_TDX_TDX_H
tools/testing/selftests/kvm/lib/x86/tdx/tdx.c new file +23 -0
new file mode 100644
index 000000000000..f9c1acd5b30c
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include "tdx/tdcall.h"
+#include "tdx/tdx.h"
+
+#define TDG_VP_VMCALL 0
+
+#define TDG_VP_VMCALL_VE_REQUEST_MMIO	48
+
+uint64_t tdg_vp_vmcall_ve_request_mmio_write(uint64_t address, uint64_t size,
+					     uint64_t data_in)
+{
+	struct tdx_tdcall_args args = {
+		.r10 = TDG_VP_VMCALL,
+		.r11 = TDG_VP_VMCALL_VE_REQUEST_MMIO,
+		.r12 = size,
+		.r13 = MMIO_WRITE,
+		.r14 = address,
+		.r15 = data_in,
+	};
+
+	return __tdx_tdcall(&args, 0);
+}
Signature · 1 line
2.51.0.536.g15c5d4f767-goog
#25 lore raw

PATCH v11 20/21 KVM: selftests: Add ucall support for TDX

In reply to Sagi Shahar

From: Ackerley Tng <[email protected]>

ucalls for non-Coco VMs work by having the guest write to the rdi register, then perform an io instruction to exit to the host. The host then reads rdi using kvm_get_regs().

CPU registers can't be read using kvm_get_regs() for TDX, so TDX guests use MMIO to pass the struct ucall's hva to the host. MMIO was chosen because it is one of the simplest (hence unlikely to fail) mechanisms that support passing 8 bytes from guest to host.

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

Changes from v10:
* Removed ucall_arch_init() decleration from ucall.h.
* Replace vm_type type check with is_tdx_vm().
* Move mmio info initialization under is_tdx_vm() case.

---
 .../selftests/kvm/include/ucall_common.h      |  1 +
 .../testing/selftests/kvm/include/x86/ucall.h |  6 ---
 tools/testing/selftests/kvm/lib/x86/ucall.c   | 46 +++++++++++++++++--
 3 files changed, 42 insertions(+), 11 deletions(-)
tools/testing/selftests/kvm/include/ucall_common.h +1 -0
index d9d6581b8d4f..f5eebf690033 100644
@@ -4,6 +4,7 @@
  */
 #ifndef SELFTEST_KVM_UCALL_COMMON_H
 #define SELFTEST_KVM_UCALL_COMMON_H
+#include "kvm_util.h"
 #include "test_util.h"
 #include "ucall.h"
 
tools/testing/selftests/kvm/include/x86/ucall.h +0 -6
index d3825dcc3cd9..7e54ec2c1a45 100644
@@ -2,12 +2,6 @@
 #ifndef SELFTEST_KVM_UCALL_H
 #define SELFTEST_KVM_UCALL_H
 
-#include "kvm_util.h"
-
 #define UCALL_EXIT_REASON       KVM_EXIT_IO
 
-static inline void ucall_arch_init(struct kvm_vm *vm, vm_paddr_t mmio_gpa)
-{
-}
-
 #endif
tools/testing/selftests/kvm/lib/x86/ucall.c +41 -5

63 lines not on this page · raw message

Signature · 1 line
2.51.0.536.g15c5d4f767-goog
#26 lore raw

PATCH v11 21/21 KVM: selftests: Add TDX lifecycle test

In reply to Sagi Shahar

Adding a test to verify TDX lifecycle by creating a simple TD.

---
 tools/testing/selftests/kvm/Makefile.kvm      |  1 +
 .../selftests/kvm/include/x86/tdx/tdx_util.h  | 10 ++++++
 .../selftests/kvm/lib/x86/tdx/tdx_util.c      | 18 +++++++++++
 tools/testing/selftests/kvm/x86/tdx_vm_test.c | 31 +++++++++++++++++++
 4 files changed, 60 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/x86/tdx_vm_test.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index 1a73e08c8437..1a76e9fa45d6 100644
@@ -155,6 +155,7 @@ TEST_GEN_PROGS_x86 += rseq_test
 TEST_GEN_PROGS_x86 += steal_time
 TEST_GEN_PROGS_x86 += system_counter_offset_test
 TEST_GEN_PROGS_x86 += pre_fault_memory_test
+TEST_GEN_PROGS_x86 += x86/tdx_vm_test
 
 # Compiled outputs used by test targets
 TEST_GEN_PROGS_EXTENDED_x86 += x86/nx_huge_pages_test
tools/testing/selftests/kvm/include/x86/tdx/tdx_util.h +10 -0
index 2467b6c35557..775ca249f74d 100644
@@ -11,6 +11,14 @@ static inline bool is_tdx_vm(struct kvm_vm *vm)
 	return vm->type == KVM_X86_TDX_VM;
 }
 
+/*
+ * Verify that TDX is supported by KVM.
+ */
+static inline bool is_tdx_enabled(void)
+{
+	return !!(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_TDX_VM));
+}
+
 /*
  * TDX ioctls
  */
@@ -72,5 +80,7 @@ void vm_tdx_load_vcpu_boot_parameters(struct kvm_vm *vm, struct kvm_vcpu *vcpu);
 void vm_tdx_set_vcpu_entry_point(struct kvm_vcpu *vcpu, void *guest_code);
 
 void vm_tdx_finalize(struct kvm_vm *vm);
+struct kvm_vm *vm_tdx_create_with_one_vcpu(void *guest_code,
+					   struct kvm_vcpu **vcpu);
 
 #endif // SELFTESTS_TDX_TDX_UTIL_H
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +18 -0
index 53cfadeff8de..714413e062fd 100644
@@ -328,3 +328,21 @@ void vm_tdx_finalize(struct kvm_vm *vm)
 	load_td_private_memory(vm);
 	vm_tdx_vm_ioctl(vm, KVM_TDX_FINALIZE_VM, 0, NULL);
 }
+
+struct kvm_vm *vm_tdx_create_with_one_vcpu(void *guest_code,
+					   struct kvm_vcpu **vcpu)
+{
+	struct vm_shape shape = {
+		.mode = VM_MODE_DEFAULT,
+		.type = KVM_X86_TDX_VM,
+	};
+	struct kvm_vm *vm;
+	struct kvm_vcpu *vcpus[1];
+
+	vm = __vm_create_with_vcpus(shape, 1, 0, guest_code, vcpus);
+	*vcpu = vcpus[0];
+
+	vm_tdx_finalize(vm);
+
+	return vm;
+}
tools/testing/selftests/kvm/x86/tdx_vm_test.c new file +31 -0
new file mode 100644
index 000000000000..a9ee489eea1a
@@ -0,0 +1,31 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include "kvm_util.h"
+#include "tdx/tdx_util.h"
+#include "ucall_common.h"
+#include "kselftest_harness.h"
+
+static void guest_code_lifecycle(void)
+{
+	GUEST_DONE();
+}
+
+TEST(verify_td_lifecycle)
+{
+	struct kvm_vcpu *vcpu;
+	struct kvm_vm *vm;
+	struct ucall uc;
+
+	vm = vm_tdx_create_with_one_vcpu(guest_code_lifecycle, &vcpu);
+
+	vcpu_run(vcpu);
+	TEST_ASSERT_EQ(get_ucall(vcpu, &uc), UCALL_DONE);
+
+	kvm_vm_free(vm);
+}
+
+int main(int argc, char **argv)
+{
+	TEST_REQUIRE(is_tdx_enabled());
+	return test_harness_run(argc, argv);
+}
Signature · 1 line
2.51.0.536.g15c5d4f767-goog
#27 lore raw

Re: [PATCH v11 21/21] KVM: selftests: Add TDX lifecycle test

Quoted 16 lines · Sagi Shahar
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +8 -0
index 53cfadeff8de..714413e062fd 100644
@@ -328,3 +328,21 @@ void vm_tdx_finalize(struct kvm_vm *vm)
 	load_td_private_memory(vm);
 	vm_tdx_vm_ioctl(vm, KVM_TDX_FINALIZE_VM, 0, NULL);
 }
+
+struct kvm_vm *vm_tdx_create_with_one_vcpu(void *guest_code,
+					   struct kvm_vcpu **vcpu)
+{
+	struct vm_shape shape = {
+		.mode = VM_MODE_DEFAULT,
+		.type = KVM_X86_TDX_VM,
+	};

tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c void vm_tdx_finalize(struct kvm_vm *vm) · line 348

Quoted 10 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +10 -0
+	struct kvm_vm *vm;
+	struct kvm_vcpu *vcpus[1];
+
+	vm = __vm_create_with_vcpus(shape, 1, 0, guest_code, vcpus);
+	*vcpu = vcpus[0];
+
+	vm_tdx_finalize(vm);
+
+	return vm;
+}

Rather than add a full wrapper, and duplicate all of vm_sev_create_with_one_vcpu(), we should just add macros to convert a type to a shape.

E.g. with this, you can simply add:

  #define VM_SHAPE_TDX	VM_TYPE(KVM_X86_TDX_VM)

And coupled with Ira's suggestion regarding vm_tdx_finalize(), there should be no need for vm_tdx_create_with_one_vcpu().

--
From: Sean Christopherson <[email protected]>
Date: Fri, 24 Oct 2025 09:14:43 -0700
Subject: [PATCH] KVM: selftests: Add macros so simplify creating VM shapes for
non-default types

Add VM_TYPE() and __VM_TYPE() macros to create a vm_shape structure given a type (and mode), and use the macros to define VM_SHAPE_{SEV,SEV_ES,SNP} shapes for x86's SEV family of VM shapes. Providing common infrastructure will avoid having to copy+paste vm_sev_create_with_one_vcpu() for TDX.

Use the new SEV+ shapes and drop vm_sev_create_with_one_vcpu().

No functional change intended.

---
 .../testing/selftests/kvm/include/kvm_util.h  | 14 +++++++
 .../selftests/kvm/include/x86/processor.h     |  4 ++
 tools/testing/selftests/kvm/include/x86/sev.h |  2 -
 tools/testing/selftests/kvm/lib/x86/sev.c     | 16 --------
 .../selftests/kvm/x86/sev_smoke_test.c        | 40 +++++++++----------
 5 files changed, 38 insertions(+), 38 deletions(-)
tools/testing/selftests/kvm/include/kvm_util.h +14 -0
index af52cd938b50..af0b53987c06 100644
@@ -210,6 +210,20 @@ kvm_static_assert(sizeof(struct vm_shape) == sizeof(uint64_t));
 	shape;					\
 })
 
+#define __VM_TYPE(__mode, __type)		\
+({						\
+	struct vm_shape shape = {		\
+		.mode = (__mode),		\
+		.type = (__type)		\
+	};					\
+						\
+	shape;					\
+})
+
+#define VM_TYPE(__type)				\
+	__VM_TYPE(VM_MODE_DEFAULT, __type)
+
+
 #if defined(__aarch64__)
 
 extern enum vm_guest_mode vm_mode_default;
tools/testing/selftests/kvm/include/x86/processor.h +4 -0
index 51cd84b9ca66..dd21e11e1908 100644
@@ -362,6 +362,10 @@ static inline unsigned int x86_model(unsigned int eax)
 	return ((eax >> 12) & 0xf0) | ((eax >> 4) & 0x0f);
 }
 
+#define VM_SHAPE_SEV		VM_TYPE(KVM_X86_SEV_VM)
+#define VM_SHAPE_SEV_ES		VM_TYPE(KVM_X86_SEV_ES_VM)
+#define VM_SHAPE_SNP		VM_TYPE(KVM_X86_SNP_VM)
+
 /* Page table bitfield declarations */
 #define PTE_PRESENT_MASK        BIT_ULL(0)
 #define PTE_WRITABLE_MASK       BIT_ULL(1)
tools/testing/selftests/kvm/include/x86/sev.h +0 -2
index 008b4169f5e2..3c3294599ba6 100644
@@ -53,8 +53,6 @@ void snp_vm_launch_start(struct kvm_vm *vm, uint64_t policy);
 void snp_vm_launch_update(struct kvm_vm *vm);
 void snp_vm_launch_finish(struct kvm_vm *vm);
 
-struct kvm_vm *vm_sev_create_with_one_vcpu(uint32_t type, void *guest_code,
-					   struct kvm_vcpu **cpu);
 void vm_sev_launch(struct kvm_vm *vm, uint64_t policy, uint8_t *measurement);
 
 kvm_static_assert(SEV_RET_SUCCESS == 0);
tools/testing/selftests/kvm/lib/x86/sev.c +0 -16
index c3a9838f4806..1e3f6514c28d 100644
@@ -158,22 +158,6 @@ void snp_vm_launch_finish(struct kvm_vm *vm)
 	vm_sev_ioctl(vm, KVM_SEV_SNP_LAUNCH_FINISH, &launch_finish);
 }
 
-struct kvm_vm *vm_sev_create_with_one_vcpu(uint32_t type, void *guest_code,
-					   struct kvm_vcpu **cpu)
-{
-	struct vm_shape shape = {
-		.mode = VM_MODE_DEFAULT,
-		.type = type,
-	};
-	struct kvm_vm *vm;
-	struct kvm_vcpu *cpus[1];
-
-	vm = __vm_create_with_vcpus(shape, 1, 0, guest_code, cpus);
-	*cpu = cpus[0];
-
-	return vm;
-}
-
 void vm_sev_launch(struct kvm_vm *vm, uint64_t policy, uint8_t *measurement)
 {
 	if (is_sev_snp_vm(vm)) {
tools/testing/selftests/kvm/x86/sev_smoke_test.c +20 -20
index 77256c89bb8d..3903793c6750 100644
@@ -74,7 +74,7 @@ static void compare_xsave(u8 *from_host, u8 *from_guest)
 		abort();
 }
 
-static void test_sync_vmsa(uint32_t type, uint64_t policy)
+static void test_sync_vmsa(struct vm_shape shape, uint64_t policy)
 {
 	struct kvm_vcpu *vcpu;
 	struct kvm_vm *vm;
@@ -84,7 +84,7 @@ static void test_sync_vmsa(uint32_t type, uint64_t policy)
 	double x87val = M_PI;
 	struct kvm_xsave __attribute__((aligned(64))) xsave = { 0 };
 
-	vm = vm_sev_create_with_one_vcpu(type, guest_code_xsave, &vcpu);
+	vm = vm_create_shape_with_one_vcpu(shape, &vcpu, guest_code_xsave);
 	gva = vm_vaddr_alloc_shared(vm, PAGE_SIZE, KVM_UTIL_MIN_VADDR,
 				    MEM_REGION_TEST_DATA);
 	hva = addr_gva2hva(vm, gva);
@@ -120,13 +120,13 @@ static void test_sync_vmsa(uint32_t type, uint64_t policy)
 	kvm_vm_free(vm);
 }
 
-static void test_sev(void *guest_code, uint32_t type, uint64_t policy)
+static void test_sev(void *guest_code, struct vm_shape shape, uint64_t policy)
 {
 	struct kvm_vcpu *vcpu;
 	struct kvm_vm *vm;
 	struct ucall uc;
 
-	vm = vm_sev_create_with_one_vcpu(type, guest_code, &vcpu);
+	vm = vm_create_shape_with_one_vcpu(shape, &vcpu, guest_code);
 
 	/* TODO: Validate the measurement is as expected. */
 	vm_sev_launch(vm, policy, NULL);
@@ -171,12 +171,12 @@ static void guest_shutdown_code(void)
 	__asm__ __volatile__("ud2");
 }
 
-static void test_sev_shutdown(uint32_t type, uint64_t policy)
+static void test_sev_shutdown(struct vm_shape shape, uint64_t policy)
 {
 	struct kvm_vcpu *vcpu;
 	struct kvm_vm *vm;
 
-	vm = vm_sev_create_with_one_vcpu(type, guest_shutdown_code, &vcpu);
+	vm = vm_create_shape_with_one_vcpu(shape, &vcpu, guest_shutdown_code);
 
 	vm_sev_launch(vm, policy, NULL);
 
@@ -188,28 +188,28 @@ static void test_sev_shutdown(uint32_t type, uint64_t policy)
 	kvm_vm_free(vm);
 }
 
-static void test_sev_smoke(void *guest, uint32_t type, uint64_t policy)
+static void test_sev_smoke(void *guest, struct vm_shape shape, uint64_t policy)
 {
 	const u64 xf_mask = XFEATURE_MASK_X87_AVX;
 
-	if (type == KVM_X86_SNP_VM)
-		test_sev(guest, type, policy | SNP_POLICY_DBG);
+	if (shape.type == KVM_X86_SNP_VM)
+		test_sev(guest, shape, policy | SNP_POLICY_DBG);
 	else
-		test_sev(guest, type, policy | SEV_POLICY_NO_DBG);
-	test_sev(guest, type, policy);
+		test_sev(guest, shape, policy | SEV_POLICY_NO_DBG);
+	test_sev(guest, shape, policy);
 
-	if (type == KVM_X86_SEV_VM)
+	if (shape.type == KVM_X86_SEV_VM)
 		return;
 
-	test_sev_shutdown(type, policy);
+	test_sev_shutdown(shape, policy);
 
 	if (kvm_has_cap(KVM_CAP_XCRS) &&
 	    (xgetbv(0) & kvm_cpu_supported_xcr0() & xf_mask) == xf_mask) {
-		test_sync_vmsa(type, policy);
-		if (type == KVM_X86_SNP_VM)
-			test_sync_vmsa(type, policy | SNP_POLICY_DBG);
+		test_sync_vmsa(shape, policy);
+		if (shape.type == KVM_X86_SNP_VM)
+			test_sync_vmsa(shape, policy | SNP_POLICY_DBG);
 		else
-			test_sync_vmsa(type, policy | SEV_POLICY_NO_DBG);
+			test_sync_vmsa(shape, policy | SEV_POLICY_NO_DBG);
 	}
 }
 
@@ -217,13 +217,13 @@ int main(int argc, char *argv[])
 {
 	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SEV));
 
-	test_sev_smoke(guest_sev_code, KVM_X86_SEV_VM, 0);
+	test_sev_smoke(guest_sev_code, VM_SHAPE_SEV, 0);
 
 	if (kvm_cpu_has(X86_FEATURE_SEV_ES))
-		test_sev_smoke(guest_sev_es_code, KVM_X86_SEV_ES_VM, SEV_POLICY_ES);
+		test_sev_smoke(guest_sev_es_code, VM_SHAPE_SEV_ES, SEV_POLICY_ES);
 
 	if (kvm_cpu_has(X86_FEATURE_SEV_SNP))
-		test_sev_smoke(guest_snp_code, KVM_X86_SNP_VM, snp_default_policy());
+		test_sev_smoke(guest_snp_code, VM_SHAPE_SNP, snp_default_policy());
 
 	return 0;
 }

base-commit: 4cc167c50eb19d44ac7e204938724e685e3d8057 --

#28 lore raw

Re: [PATCH v11 21/21] KVM: selftests: Add TDX lifecycle test

Quoted 31 lines · Sean Christopherson
Quoted 16 lines · Sagi Shahar
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +8 -0
index 53cfadeff8de..714413e062fd 100644
@@ -328,3 +328,21 @@ void vm_tdx_finalize(struct kvm_vm *vm)
 	load_td_private_memory(vm);
 	vm_tdx_vm_ioctl(vm, KVM_TDX_FINALIZE_VM, 0, NULL);
 }
+
+struct kvm_vm *vm_tdx_create_with_one_vcpu(void *guest_code,
+					   struct kvm_vcpu **vcpu)
+{
+	struct vm_shape shape = {
+		.mode = VM_MODE_DEFAULT,
+		.type = KVM_X86_TDX_VM,
+	};

tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c void vm_tdx_finalize(struct kvm_vm *vm) · line 348

Quoted 10 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +10 -0
+	struct kvm_vm *vm;
+	struct kvm_vcpu *vcpus[1];
+
+	vm = __vm_create_with_vcpus(shape, 1, 0, guest_code, vcpus);
+	*vcpu = vcpus[0];
+
+	vm_tdx_finalize(vm);
+
+	return vm;
+}

Rather than add a full wrapper, and duplicate all of vm_sev_create_with_one_vcpu(), we should just add macros to convert a type to a shape.

Quoted 6 lines

E.g. with this, you can simply add:

  #define VM_SHAPE_TDX	VM_TYPE(KVM_X86_TDX_VM)

And coupled with Ira's suggestion regarding vm_tdx_finalize(), there should be no need for vm_tdx_create_with_one_vcpu().

All sounds reasonable to me but some questions/comments below.

[snip]

Quoted 10 lines
tools/testing/selftests/kvm/include/kvm_util.h +2 -0
index af52cd938b50..af0b53987c06 100644
@@ -210,6 +210,20 @@ kvm_static_assert(sizeof(struct vm_shape) == sizeof(uint64_t));
 	shape;					\
 })
 
+#define __VM_TYPE(__mode, __type)		\
+({						\

tools/testing/selftests/kvm/include/kvm_util.h kvm_static_assert(sizeof(struct vm_shape) == sizeof(uint64_t)); · line 224

Quoted 10 lines
tools/testing/selftests/kvm/include/kvm_util.h +10 -0
+	struct vm_shape shape = {		\
+		.mode = (__mode),		\
+		.type = (__type)		\
+	};					\
+						\
+	shape;					\
+})
+
+#define VM_TYPE(__type)				\
+	__VM_TYPE(VM_MODE_DEFAULT, __type)

We already have VM_SHAPE()? Why do we need this as well?

Quoted 10 lines
tools/testing/selftests/kvm/include/kvm_util.h +2 -0
+
+
 #if defined(__aarch64__)
 
 extern enum vm_guest_mode vm_mode_default;
tools/testing/selftests/kvm/include/x86/processor.h +0 -0
index 51cd84b9ca66..dd21e11e1908 100644

tools/testing/selftests/kvm/include/x86/processor.h static inline unsigned int x86_model(unsigned int eax) · line 367

Quoted 6 lines
tools/testing/selftests/kvm/include/x86/processor.h +3 -0
@@ -362,6 +362,10 @@ static inline unsigned int x86_model(unsigned int eax)
 	return ((eax >> 12) & 0xf0) | ((eax >> 4) & 0x0f);
 }
 
+#define VM_SHAPE_SEV		VM_TYPE(KVM_X86_SEV_VM)
+#define VM_SHAPE_SEV_ES		VM_TYPE(KVM_X86_SEV_ES_VM)
+#define VM_SHAPE_SNP		VM_TYPE(KVM_X86_SNP_VM)

FWIW I think the SEV bits should be pulled apart from the TDX bits and the TDX bits squashed back into this series with the SEV as a per-cursor patch.

Ira

[snip]

#29 lore raw

Re: [PATCH v11 21/21] KVM: selftests: Add TDX lifecycle test

Quoted 9 lines · Ira Weiny
Quoted 8 lines · Sean Christopherson
tools/testing/selftests/kvm/include/kvm_util.h +0 -0
index af52cd938b50..af0b53987c06 100644
@@ -210,6 +210,20 @@ kvm_static_assert(sizeof(struct vm_shape) == sizeof(uint64_t));
 	shape;					\
 })
 
Quoted 14 lines

tools/testing/selftests/kvm/include/kvm_util.h kvm_static_assert(sizeof(struct vm_shape) == sizeof(uint64_t)); · line 224

Quoted 12 lines
tools/testing/selftests/kvm/include/kvm_util.h +12 -0
+#define __VM_TYPE(__mode, __type)		\
+({						\
+	struct vm_shape shape = {		\
+		.mode = (__mode),		\
+		.type = (__type)		\
+	};					\
+						\
+	shape;					\
+})
+
+#define VM_TYPE(__type)				\
+	__VM_TYPE(VM_MODE_DEFAULT, __type)

We already have VM_SHAPE()? Why do we need this as well?

VM_SHAPE() takes the "mode", and assumes a default type. The alternative would be something like __VM_SHAPE(__type, __mode), but that's annoying, especially on x86 which only has one mode.

And __VM_SHAPE(__type) + ____VM_SHAPE(__type, __mode) feels even more weird.

I'm definitely open to more ideas, VM_TYPE() isn't great either, just the least awful option I came up with.

Quoted 11 lines
Quoted 11 lines
tools/testing/selftests/kvm/include/kvm_util.h +0 -0
 #if defined(__aarch64__)
 
 extern enum vm_guest_mode vm_mode_default;
tools/testing/selftests/kvm/include/x86/processor.h +0 -0
index 51cd84b9ca66..dd21e11e1908 100644
@@ -362,6 +362,10 @@ static inline unsigned int x86_model(unsigned int eax)
 	return ((eax >> 12) & 0xf0) | ((eax >> 4) & 0x0f);
 }
 
Quoted 6 lines

tools/testing/selftests/kvm/include/x86/processor.h static inline unsigned int x86_model(unsigned int eax) · line 367

Quoted 3 lines
tools/testing/selftests/kvm/include/x86/processor.h +3 -0
+#define VM_SHAPE_SEV		VM_TYPE(KVM_X86_SEV_VM)
+#define VM_SHAPE_SEV_ES		VM_TYPE(KVM_X86_SEV_ES_VM)
+#define VM_SHAPE_SNP		VM_TYPE(KVM_X86_SNP_VM)

FWIW I think the SEV bits should be pulled apart from the TDX bits and the TDX bits squashed back into this series with the SEV as a per-cursor patch.

Ya, that's my intent, "officially" post and land this SEV+ change, then have the TDX series build on top. Or did you mean something else?

#30 lore raw

Re: [PATCH v11 21/21] KVM: selftests: Add TDX lifecycle test

Quoted 53 lines · Sean Christopherson
Quoted 9 lines · Ira Weiny
Quoted 8 lines · Sean Christopherson
tools/testing/selftests/kvm/include/kvm_util.h +0 -0
index af52cd938b50..af0b53987c06 100644
@@ -210,6 +210,20 @@ kvm_static_assert(sizeof(struct vm_shape) == sizeof(uint64_t));
    shape;                                  \
 })
 
Quoted 14 lines

tools/testing/selftests/kvm/include/kvm_util.h kvm_static_assert(sizeof(struct vm_shape) == sizeof(uint64_t)); · line 224

Quoted 12 lines
tools/testing/selftests/kvm/include/kvm_util.h +12 -0
+#define __VM_TYPE(__mode, __type)          \
+({                                         \
+   struct vm_shape shape = {               \
+           .mode = (__mode),               \
+           .type = (__type)                \
+   };                                      \
+                                           \
+   shape;                                  \
+})
+
+#define VM_TYPE(__type)                            \
+   __VM_TYPE(VM_MODE_DEFAULT, __type)

We already have VM_SHAPE()? Why do we need this as well?

VM_SHAPE() takes the "mode", and assumes a default type. The alternative would be something like __VM_SHAPE(__type, __mode), but that's annoying, especially on x86 which only has one mode.

And __VM_SHAPE(__type) + ____VM_SHAPE(__type, __mode) feels even more weird.

I'm definitely open to more ideas, VM_TYPE() isn't great either, just the least awful option I came up with.

Quoted 11 lines
Quoted 11 lines
tools/testing/selftests/kvm/include/kvm_util.h +0 -0
 #if defined(__aarch64__)
 
 extern enum vm_guest_mode vm_mode_default;
tools/testing/selftests/kvm/include/x86/processor.h +0 -0
index 51cd84b9ca66..dd21e11e1908 100644
@@ -362,6 +362,10 @@ static inline unsigned int x86_model(unsigned int eax)
    return ((eax >> 12) & 0xf0) | ((eax >> 4) & 0x0f);
 }
 
Quoted 6 lines

tools/testing/selftests/kvm/include/x86/processor.h static inline unsigned int x86_model(unsigned int eax) · line 367

Quoted 3 lines
tools/testing/selftests/kvm/include/x86/processor.h +3 -0
+#define VM_SHAPE_SEV               VM_TYPE(KVM_X86_SEV_VM)
+#define VM_SHAPE_SEV_ES            VM_TYPE(KVM_X86_SEV_ES_VM)
+#define VM_SHAPE_SNP               VM_TYPE(KVM_X86_SNP_VM)

FWIW I think the SEV bits should be pulled apart from the TDX bits and the TDX bits squashed back into this series with the SEV as a per-cursor patch.

Quoted 2 lines

Ya, that's my intent, "officially" post and land this SEV+ change, then have the TDX series build on top. Or did you mean something else?

I've got v12 mostly ready to be sent for review. I was thinking of incorporating this change as part of that series. Do you prefer that I wait until this patch lands before I post v12?

#31 lore raw

Re: [PATCH v11 21/21] KVM: selftests: Add TDX lifecycle test

Quoted 4 lines · Sagi Shahar
Quoted 3 lines · Sean Christopherson
Quoted 1 line · Ira Weiny

Sean Christopherson wrote:

[snip]

Quoted 15 lines
Quoted 15 lines
Quoted 14 lines

tools/testing/selftests/kvm/include/x86/processor.h static inline unsigned int x86_model(unsigned int eax) · line 367

Quoted 11 lines
tools/testing/selftests/kvm/include/x86/processor.h +3 -0
index 51cd84b9ca66..dd21e11e1908 100644
@@ -362,6 +362,10 @@ static inline unsigned int x86_model(unsigned int eax)
    return ((eax >> 12) & 0xf0) | ((eax >> 4) & 0x0f);
 }
 
+#define VM_SHAPE_SEV               VM_TYPE(KVM_X86_SEV_VM)
+#define VM_SHAPE_SEV_ES            VM_TYPE(KVM_X86_SEV_ES_VM)
+#define VM_SHAPE_SNP               VM_TYPE(KVM_X86_SNP_VM)

FWIW I think the SEV bits should be pulled apart from the TDX bits and the TDX bits squashed back into this series with the SEV as a per-cursor patch.

Quoted 6 lines
Quoted 2 lines

Ya, that's my intent, "officially" post and land this SEV+ change, then have the TDX series build on top. Or did you mean something else?

I've got v12 mostly ready to be sent for review. I was thinking of incorporating this change as part of that series. Do you prefer that I wait until this patch lands before I post v12?

I can't speak for Sean directly. Buy my interpretation was you should make this SEV change a lead in patch of v12. IOW v12 patch 1/X is this SEV change which sets up the new VM_TYPE() macro to be used subsequently by the TDX code.

Then if there are further issues with the TDX stuff Sean can pick this off as a clean up. But my hope is that this series is pretty close to being acceptable and it will land. Then we can focus on building the tests on top of this new architecture.

Ira

#32 lore raw

Re: [PATCH v11 21/21] KVM: selftests: Add TDX lifecycle test

In reply to Sean Christopherson
Quoted 25 lines · Sean Christopherson
Quoted 9 lines · Ira Weiny
Quoted 8 lines · Sean Christopherson
tools/testing/selftests/kvm/include/kvm_util.h +0 -0
index af52cd938b50..af0b53987c06 100644
@@ -210,6 +210,20 @@ kvm_static_assert(sizeof(struct vm_shape) == sizeof(uint64_t));
 	shape;					\
 })
 
Quoted 14 lines

tools/testing/selftests/kvm/include/kvm_util.h kvm_static_assert(sizeof(struct vm_shape) == sizeof(uint64_t)); · line 224

Quoted 12 lines
tools/testing/selftests/kvm/include/kvm_util.h +12 -0
+#define __VM_TYPE(__mode, __type)		\
+({						\
+	struct vm_shape shape = {		\
+		.mode = (__mode),		\
+		.type = (__type)		\
+	};					\
+						\
+	shape;					\
+})
+
+#define VM_TYPE(__type)				\
+	__VM_TYPE(VM_MODE_DEFAULT, __type)

We already have VM_SHAPE()? Why do we need this as well?

Quoted 1 line

VM_SHAPE() takes the "mode", and assumes a default type.

Ah yea.

Quoted 8 lines

The alternative would
be something like __VM_SHAPE(__type, __mode), but that's annoying, especially on x86 which only has one mode.

And __VM_SHAPE(__type) + ____VM_SHAPE(__type, __mode) feels even more weird.

I'm definitely open to more ideas, VM_TYPE() isn't great either, just the least awful option I came up with.

No this name is fine then. I got dyslexic with mode vs type, apologies.

Quoted 19 lines
Quoted 12 lines
Quoted 12 lines
tools/testing/selftests/kvm/include/kvm_util.h +0 -0
 #if defined(__aarch64__)
 
 extern enum vm_guest_mode vm_mode_default;
tools/testing/selftests/kvm/include/x86/processor.h +0 -0
index 51cd84b9ca66..dd21e11e1908 100644
@@ -362,6 +362,10 @@ static inline unsigned int x86_model(unsigned int eax)
 	return ((eax >> 12) & 0xf0) | ((eax >> 4) & 0x0f);
 }
 
Quoted 6 lines

tools/testing/selftests/kvm/include/x86/processor.h static inline unsigned int x86_model(unsigned int eax) · line 367

Quoted 3 lines
tools/testing/selftests/kvm/include/x86/processor.h +3 -0
+#define VM_SHAPE_SEV		VM_TYPE(KVM_X86_SEV_VM)
+#define VM_SHAPE_SEV_ES		VM_TYPE(KVM_X86_SEV_ES_VM)
+#define VM_SHAPE_SNP		VM_TYPE(KVM_X86_SNP_VM)

FWIW I think the SEV bits should be pulled apart from the TDX bits and the TDX bits squashed back into this series with the SEV as a per-cursor patch.

Quoted 2 lines

Ya, that's my intent, "officially" post and land this SEV+ change, then have the TDX series build on top.

Sounds good.

Quoted 2 lines

Or did you mean something else?

No.

Ira