Re: [PATCH v16 09/20] KVM: selftests: Set up TDX boot code region

From: Ackerley Tng

Date: Mon Nov 09 2026 - 15:02:56 EST


Lisa Wang <wyihan@xxxxxxxxxx> writes:

>
> [...snip...]
>
> /* Leave marker so size of td_boot code can be computed. */
> .globl td_boot_code_end
> 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 564144c40f0a..b4a7089b3636 100644
> --- a/tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c
> +++ b/tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c
> @@ -1,8 +1,46 @@
> // SPDX-License-Identifier: GPL-2.0-only
>
> +#include <linux/sizes.h>
> +
> #include "processor.h"
> +#include "tdx/td_boot.h"
> #include "tdx/tdx_util.h"
>
> +/* Arbitrarily selected to avoid overlaps with anything else */
> +#define TD_BOOT_CODE_SLOT 20
> +
> +#define X86_RESET_VECTOR 0xfffffff0ul
> +
> +void tdx_vm_setup_boot_code_region(struct kvm_vm *vm)
> +{
> + const size_t total_size = TD_BOOT_CODE_SIZE;

This caught my attention because it wasn't quite reverse-fir-tree
ordering.

Naming is confusing here since there's both TD_BOOT_CODE_SIZE and
boot_code_size, so which is the boot code and what is the right size?

If the only use of TD_BOOT_CODE_SIZE is here then maybe we could just
directly memcpy (td_boot_code_end - td_boot) and not have the macro.

> + const size_t boot_code_size = td_boot_reset_vector_trampoline - td_boot;
> + const gpa_t boot_code_gpa = X86_RESET_VECTOR - boot_code_size;
> + gpa_t alloc_gpa = round_down(boot_code_gpa, PAGE_SIZE);
> + size_t nr_pages = (SZ_4G - alloc_gpa) / PAGE_SIZE;
> + u64 gmem_flags = 0;
> + gpa_t gpa;
> + u8 *hva;
> +
> + if (kvm_has_gmem_attributes)
> + gmem_flags |= GUEST_MEMFD_FLAG_INIT_SHARED | GUEST_MEMFD_FLAG_MMAP;
> + vm_mem_add(vm, VM_MEM_SRC_SHMEM, alloc_gpa, TD_BOOT_CODE_SLOT,
> + nr_pages, KVM_MEM_GUEST_MEMFD, -1, 0, gmem_flags);
> +
> + gpa = ____vm_phy_pages_alloc(vm, nr_pages, alloc_gpa,
> + TD_BOOT_CODE_SLOT, true, false);
> + TEST_ASSERT(gpa == alloc_gpa, "Failed vm_phy_pages_alloc");
> +
> + virt_map(vm, alloc_gpa, alloc_gpa, nr_pages);
> + hva = addr_gpa2hva(vm, boot_code_gpa);
> + memcpy(hva, td_boot, total_size);
> +
> + hva += boot_code_size;
> + TEST_ASSERT(hva == addr_gpa2hva(vm, X86_RESET_VECTOR),
> + "Expected RESET vector at hva 0x%lx, got %lx",
> + (unsigned long)addr_gpa2hva(vm, X86_RESET_VECTOR), (unsigned long)hva);

TEST_ASSERT_EQ would work here, same for the == assertion above.

Not a huge issue,

Reviewed-by: Ackerley Tng <ackerleytng@xxxxxxxxxx>

> +}
> +
> 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