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 | 48 ++++++++++++++++++++++
 3 files changed, 52 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 e14566919601..e529c587aeae 100644
--- a/tools/testing/selftests/kvm/include/x86/tdx/tdx_util.h
+++ b/tools/testing/selftests/kvm/include/x86/tdx/tdx_util.h
@@ -48,5 +48,7 @@ static inline bool is_tdx_vm(struct kvm_vm *vm)
 
 void tdx_init_vm(struct kvm_vm *vm);
 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 e3cf56c9c852..da430d4e7765 100644
--- a/tools/testing/selftests/kvm/lib/x86/processor.c
+++ b/tools/testing/selftests/kvm/lib/x86/processor.c
@@ -821,6 +821,8 @@ void kvm_arch_vm_post_create(struct kvm_vm *vm, unsigned 
int nr_vcpus)
        if (is_tdx_vm(vm)) {
                tdx_init_vm(vm);
                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 6187cfcf6a74..e348507a6be8 100644
--- a/tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c
+++ b/tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c
@@ -8,6 +8,7 @@
 
 /* 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
 
@@ -38,6 +39,53 @@ void tdx_vm_setup_boot_code_region(struct kvm_vm *vm)
                    (unsigned long)addr_gpa2hva(vm, X86_RESET_VECTOR), 
(unsigned long)hva);
 }
 
+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);
+       const u64 gmem_flags = 0;
+       gpa_t gpa;
+
+       vm_mem_add(vm, VM_MEM_SRC_SHMEM, TD_BOOT_PARAMETERS_GPA,
+                  TD_BOOT_PARAMETERS_SLOT, npages,
+                  KVM_MEM_GUEST_MEMFD, -1, 0, gmem_flags);
+       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;
+
+       cr4 = kvm_get_default_cr4(vm->mmu.pgtable_levels);
+
+       /* Per the TDX spec, CR4 bit MCE is fixed to 1 */
+       cr4 |= X86_CR4_MCE;
+
+       /* Per the TDX spec, 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->idtr.base < (1ULL << 32),
+                   "idt must be within 32-bit address space for 32-bit boot 
code");
+       TEST_ASSERT(params->gdtr.base < (1ULL << 32),
+                   "gdt must be within 32-bit address space for 32-bit boot 
code");
+}
+
 static const struct kvm_tdx_capabilities *tdx_read_capabilities(struct kvm_vm 
*vm)
 {
        static struct kvm_tdx_capabilities *tdx_cap;

-- 
2.56.0.rc1.315.gc6ed9934b7-goog


Reply via email to