[PATCH v3 48/70] i386/tdx: register TDVF as private memory

Xiaoyao Li
2023-11-15T07:22:06Z

From: Chao Peng <chao.p.peng@linux.intel.com>

Allocate private guest memfd memory for BIOS if it's TD VM.

Signed-off-by: Chao Peng <chao.p.peng@linux.intel.com>
Co-developed-by: Xiaoyao Li <xiaoyao.li@intel.com>
Signed-off-by: Xiaoyao Li <xiaoyao.li@intel.com>
---
hw/i386/x86.c | 10 +++++++++-
target/i386/kvm/tdx.c | 18 ++++++++++++++++++
target/i386/kvm/tdx.h | 2 ++
3 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/hw/i386/x86.c b/hw/i386/x86.c
index 2f299355a5e3..0f69b55c5219 100644
--- a/hw/i386/x86.c
+++ b/hw/i386/x86.c
@@ -1146,8 +1146,16 @@ void x86_bios_rom_init(MachineState *ms, const char *default_firmware,
(bios_size % 65536) != 0) {
goto bios_error;
}
+
bios = g_malloc(sizeof(*bios));
- memory_region_init_ram(bios, NULL, "pc.bios", bios_size, &error_fatal);
+ if (is_tdx_vm()) {
+ memory_region_init_ram_guest_memfd(bios, NULL, "pc.bios", bios_size,
+ &error_fatal);
+ tdx_set_tdvf_region(bios);
+ } else {
+ memory_region_init_ram(bios, NULL, "pc.bios", bios_size, &error_fatal);
+ }
+
if (sev_enabled() || is_tdx_vm()) {
/*
* The concept of a "reset" simply doesn't exist for
diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c
index 6bb3249fa610..4b8c13890b11 100644
--- a/target/i386/kvm/tdx.c
+++ b/target/i386/kvm/tdx.c
@@ -21,6 +21,7 @@
#include "sysemu/kvm.h"
#include "sysemu/sysemu.h"
#include "exec/address-spaces.h"
+#include "exec/ramblock.h"

#include "hw/i386/e820_memory_layout.h"
#include "hw/i386/x86.h"
@@ -461,6 +462,12 @@ static void update_tdx_cpuid_lookup_by_tdx_caps(void)
(tdx_caps->xfam_fixed1 & CPUID_XSTATE_XSS_MASK) >> 32;
}

+void tdx_set_tdvf_region(MemoryRegion *tdvf_region)
+{
+ assert(!tdx_guest->tdvf_region);
+ tdx_guest->tdvf_region = tdvf_region;
+}
+
static TdxFirmwareEntry *tdx_get_hob_entry(TdxGuest *tdx)
{
TdxFirmwareEntry *entry;
@@ -582,6 +589,7 @@ static void tdx_finalize_vm(Notifier *notifier, void *unused)
{
TdxFirmware *tdvf = &tdx_guest->tdvf;
TdxFirmwareEntry *entry;
+ RAMBlock *ram_block;
int r;

tdx_init_ram_entries();
@@ -616,6 +624,12 @@ static void tdx_finalize_vm(Notifier *notifier, void *unused)
.nr_pages = entry->size / 4096,
};

+ r = kvm_set_memory_attributes_private(entry->address, entry->size);
+ if (r < 0) {
+ error_report("Reserve initial private memory failed %s", strerror(-r));
+ exit(1);
+ }
+
__u32 flags = entry->attributes & TDVF_SECTION_ATTRIBUTES_MR_EXTEND ?
KVM_TDX_MEASURE_MEMORY_REGION : 0;

@@ -631,6 +645,10 @@ static void tdx_finalize_vm(Notifier *notifier, void *unused)
entry->mem_ptr = NULL;
}
}
+
+ /* Tdvf image was copied into private region above. It becomes unnecessary. */
+ ram_block = tdx_guest->tdvf_region->ram_block;
+ ram_block_discard_range(ram_block, 0, ram_block->max_length);
}

static Notifier tdx_machine_done_notify = {
diff --git a/target/i386/kvm/tdx.h b/target/i386/kvm/tdx.h
index 3a35a2bc0900..5fb20a5f06bb 100644
--- a/target/i386/kvm/tdx.h
+++ b/target/i386/kvm/tdx.h
@@ -38,6 +38,7 @@ typedef struct TdxGuest {
char *mrownerconfig; /* base64 encoded sha348 digest */

TdxFirmware tdvf;
+ MemoryRegion *tdvf_region;

uint32_t nr_ram_entries;
TdxRamEntry *ram_entries;
@@ -53,6 +54,7 @@ int tdx_kvm_init(MachineState *ms, Error **errp);
void tdx_get_supported_cpuid(uint32_t function, uint32_t index, int reg,
uint32_t *ret);
int tdx_pre_create_vcpu(CPUState *cpu, Error **errp);
+void tdx_set_tdvf_region(MemoryRegion *tdvf_region);
int tdx_parse_tdvf(void *flash_ptr, int size);

#endif /* QEMU_I386_TDX_H */
--
2.34.1