From: Sagi Shahar <[email protected]>

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

Co-developed-by: Ackerley Tng <[email protected]>
Signed-off-by: Ackerley Tng <[email protected]>
Signed-off-by: Sagi Shahar <[email protected]>
Signed-off-by: Lisa Wang <[email protected]>
---
 .../selftests/kvm/include/x86/tdx/tdx_util.h       |  2 +
 tools/testing/selftests/kvm/lib/x86/processor.c    |  2 +
 tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c | 56 ++++++++++++++++++++++
 3 files changed, 60 insertions(+)

diff --git a/tools/testing/selftests/kvm/include/x86/tdx/tdx_util.h 
b/tools/testing/selftests/kvm/include/x86/tdx/tdx_util.h
index d66ea7bc85f9..9660ea9d2f31 100644
--- a/tools/testing/selftests/kvm/include/x86/tdx/tdx_util.h
+++ b/tools/testing/selftests/kvm/include/x86/tdx/tdx_util.h
@@ -41,5 +41,7 @@ static inline bool is_tdx_vm(struct kvm_vm *vm)
 
 void tdx_init_vm(struct kvm_vm *vm, u64 attributes);
 void tdx_vm_setup_boot_code_region(struct kvm_vm *vm);
+void tdx_vm_setup_boot_parameters_region(struct kvm_vm *vm, u32 
nr_runnable_vcpus);
+void tdx_vm_load_common_boot_parameters(struct kvm_vm *vm);
 
 #endif /* SELFTESTS_TDX_TDX_UTIL_H */
diff --git a/tools/testing/selftests/kvm/lib/x86/processor.c 
b/tools/testing/selftests/kvm/lib/x86/processor.c
index dfabdfd17976..c7c4a37b3170 100644
--- a/tools/testing/selftests/kvm/lib/x86/processor.c
+++ b/tools/testing/selftests/kvm/lib/x86/processor.c
@@ -794,6 +794,8 @@ void kvm_arch_vm_post_create(struct kvm_vm *vm, unsigned 
int nr_vcpus)
        if (is_tdx_vm(vm)) {
                tdx_init_vm(vm, 0);
                tdx_vm_setup_boot_code_region(vm);
+               tdx_vm_setup_boot_parameters_region(vm, nr_vcpus);
+               tdx_vm_load_common_boot_parameters(vm);
        }
 
        r = __vm_ioctl(vm, KVM_GET_TSC_KHZ, NULL);
diff --git a/tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c 
b/tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c
index bbfaa9af9c60..b16bf24f3ef1 100644
--- a/tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c
+++ b/tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c
@@ -3,10 +3,12 @@
 #include "kvm_util.h"
 #include "processor.h"
 #include "tdx/td_boot.h"
+#include "tdx/td_boot_asm.h"
 #include "tdx/tdx_util.h"
 
 /* Arbitrarily selected to avoid overlaps with anything else */
 #define TD_BOOT_CODE_SLOT      20
+#define TD_BOOT_PARAMETERS_SLOT        21
 
 #define X86_RESET_VECTOR       0xfffffff0ul
 #define X86_RESET_VECTOR_SIZE  16
@@ -51,6 +53,60 @@ void tdx_vm_setup_boot_code_region(struct kvm_vm *vm)
        hva[2] = 0xcc;
 }
 
+void tdx_vm_setup_boot_parameters_region(struct kvm_vm *vm, u32 
nr_runnable_vcpus)
+{
+       size_t boot_params_size =
+               sizeof(struct td_boot_parameters) +
+               nr_runnable_vcpus * sizeof(struct td_per_vcpu_parameters);
+       int npages = DIV_ROUND_UP(boot_params_size, PAGE_SIZE);
+       gpa_t gpa;
+
+       vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
+                                   TD_BOOT_PARAMETERS_GPA,
+                                   TD_BOOT_PARAMETERS_SLOT, npages,
+                                   KVM_MEM_GUEST_MEMFD);
+       gpa = vm_phy_pages_alloc(vm, npages, TD_BOOT_PARAMETERS_GPA, 
TD_BOOT_PARAMETERS_SLOT);
+       TEST_ASSERT(gpa == TD_BOOT_PARAMETERS_GPA, "Failed 
vm_phy_pages_alloc\n");
+
+       virt_map(vm, TD_BOOT_PARAMETERS_GPA, TD_BOOT_PARAMETERS_GPA, npages);
+}
+
+void tdx_vm_load_common_boot_parameters(struct kvm_vm *vm)
+{
+       struct td_boot_parameters *params =
+               addr_gpa2hva(vm, TD_BOOT_PARAMETERS_GPA);
+       u32 cr4;
+
+       TEST_ASSERT_EQ(vm->mode, VM_MODE_PXXVYY_4K);
+
+       cr4 = kvm_get_default_cr4();
+       if (vm->mmu.pgtable_levels == 5)
+               cr4 |= X86_CR4_LA57;
+
+       /* TDX spec 11.6.2: CR4 bit MCE is fixed to 1 */
+       cr4 |= X86_CR4_MCE;
+
+       /* TDX spec 11.6.2: CR4 bit VMXE and SMXE are fixed to 0 */
+       cr4 &= ~(X86_CR4_VMXE | X86_CR4_SMXE);
+
+       /* Set parameters! */
+       params->cr0 = kvm_get_default_cr0();
+       TEST_ASSERT(vm->mmu.pgd < (1ULL << 32),
+                   "PGD must be within 32-bit address space for 32-bit boot 
code");
+       params->cr3 = vm->mmu.pgd;
+       params->cr4 = cr4;
+       params->idtr.base = vm->arch.idt;
+       params->idtr.limit = kvm_get_default_idt_limit();
+       params->gdtr.base = vm->arch.gdt;
+       params->gdtr.limit = kvm_get_default_gdt_limit();
+
+       TEST_ASSERT(params->cr0 != 0, "cr0 should not be 0");
+       TEST_ASSERT(params->cr3 != 0, "cr3 should not be 0");
+       TEST_ASSERT(params->cr4 != 0, "cr4 should not be 0");
+       TEST_ASSERT(params->gdtr.base != 0, "gdt base address should not be 0");
+       TEST_ASSERT(params->idtr.base != 0, "idt base address should not be 0");
+}
+
 static struct kvm_tdx_capabilities *tdx_read_capabilities(struct kvm_vm *vm)
 {
        struct kvm_tdx_capabilities *tdx_cap = NULL;

-- 
2.54.0.746.g67dd491aae-goog


Reply via email to