From: Sagi Shahar 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 Signed-off-by: Ackerley Tng Signed-off-by: Sagi Shahar Signed-off-by: Lisa Wang Reviewed-by: Xiaoyao Li Reviewed-by: Binbin Wu --- .../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 | 50 ++++++++++++++++++++++ 3 files changed, 54 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 43b4157e4a13..4330290d29e9 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 36003cfd8d6a..a215ab6c2ea4 100644 --- a/tools/testing/selftests/kvm/lib/x86/processor.c +++ b/tools/testing/selftests/kvm/lib/x86/processor.c @@ -828,6 +828,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 b4a7089b3636..e8deef076f9a 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 @@ -41,6 +42,55 @@ 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); + u64 gmem_flags = 0; + gpa_t gpa; + + if (kvm_has_gmem_attributes) + gmem_flags |= GUEST_MEMFD_FLAG_INIT_SHARED | GUEST_MEMFD_FLAG_MMAP; + 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, true, false); + TEST_ASSERT(gpa == TD_BOOT_PARAMETERS_GPA, "Failed vm_phy_pages_alloc"); + + 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; + TEST_ASSERT(vm->arch.idt < (1ULL << 32), + "idt must be within 32-bit address space for 32-bit boot code"); + params->idtr.base = vm->arch.idt; + params->idtr.limit = kvm_get_default_idt_limit(); + TEST_ASSERT(vm->arch.gdt < (1ULL << 32), + "gdt must be within 32-bit address space for 32-bit boot code"); + params->gdtr.base = vm->arch.gdt; + params->gdtr.limit = kvm_get_default_gdt_limit(); +} + static const struct kvm_tdx_capabilities *tdx_read_capabilities(struct kvm_vm *vm) { static struct kvm_tdx_capabilities *tdx_cap; -- 2.56.0.455.g6c5a503b38-goog