[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v6 25/60] i386/tdx: Add TDVF memory via KVM_TDX_INIT_MEM_REGION
From: |
Xiaoyao Li |
Subject: |
[PATCH v6 25/60] i386/tdx: Add TDVF memory via KVM_TDX_INIT_MEM_REGION |
Date: |
Tue, 5 Nov 2024 01:23:33 -0500 |
From: Isaku Yamahata <isaku.yamahata@intel.com>
TDVF firmware (CODE and VARS) needs to be copied to TD's private
memory via KVM_TDX_INIT_MEM_REGION, as well as TD HOB and TEMP memory.
If the TDVF section has TDVF_SECTION_ATTRIBUTES_MR_EXTEND set in the
flag, calling KVM_TDX_EXTEND_MEMORY to extend the measurement.
After populating the TDVF memory, the original image located in shared
ramblock can be discarded.
Signed-off-by: Isaku Yamahata <isaku.yamahata@intel.com>
Signed-off-by: Xiaoyao Li <xiaoyao.li@intel.com>
Acked-by: Gerd Hoffmann <kraxel@redhat.com>
---
Changes in v6:
- switch back to use KVM_TDX_INIT_MEM_REGION according to KVM's change;
---
target/i386/kvm/tdx.c | 39 +++++++++++++++++++++++++++++++++++++++
1 file changed, 39 insertions(+)
diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c
index 6720c785a4ad..0a6ac62de7ff 100644
--- a/target/i386/kvm/tdx.c
+++ b/target/i386/kvm/tdx.c
@@ -18,6 +18,7 @@
#include "qapi/error.h"
#include "qom/object_interfaces.h"
#include "sysemu/sysemu.h"
+#include "exec/ramblock.h"
#include "hw/i386/e820_memory_layout.h"
#include "hw/i386/x86.h"
@@ -253,6 +254,8 @@ 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();
@@ -278,6 +281,42 @@ static void tdx_finalize_vm(Notifier *notifier, void
*unused)
sizeof(TdxRamEntry), &tdx_ram_entry_compare);
tdvf_hob_create(tdx_guest, tdx_get_hob_entry(tdx_guest));
+
+ for_each_tdx_fw_entry(tdvf, entry) {
+ struct kvm_tdx_init_mem_region region;
+ uint32_t flags;
+
+ region = (struct kvm_tdx_init_mem_region) {
+ .source_addr = (uint64_t)entry->mem_ptr,
+ .gpa = entry->address,
+ .nr_pages = entry->size >> 12,
+ };
+
+ flags = entry->attributes & TDVF_SECTION_ATTRIBUTES_MR_EXTEND ?
+ KVM_TDX_MEASURE_MEMORY_REGION : 0;
+
+ do {
+ r = tdx_vcpu_ioctl(first_cpu, KVM_TDX_INIT_MEM_REGION, flags,
+ ®ion);
+ } while (r == -EAGAIN || r == -EINTR);
+ if (r < 0) {
+ error_report("KVM_TDX_INIT_MEM_REGION failed %s", strerror(-r));
+ exit(1);
+ }
+
+ if (entry->type == TDVF_SECTION_TYPE_TD_HOB ||
+ entry->type == TDVF_SECTION_TYPE_TEMP_MEM) {
+ qemu_ram_munmap(-1, entry->mem_ptr, entry->size);
+ entry->mem_ptr = NULL;
+ }
+ }
+
+ /*
+ * TDVF image has been copied into private region above via
+ * KVM_MEMORY_MAPPING. It becomes useless.
+ */
+ ram_block = tdx_guest->tdvf_mr->ram_block;
+ ram_block_discard_range(ram_block, 0, ram_block->max_length);
}
static Notifier tdx_machine_done_notify = {
--
2.34.1
- [PATCH v6 15/60] i386/tdx: Set APIC bus rate to match with what TDX module enforces, (continued)
- [PATCH v6 15/60] i386/tdx: Set APIC bus rate to match with what TDX module enforces, Xiaoyao Li, 2024/11/05
- [PATCH v6 17/60] i386/tdx: load TDVF for TD guest, Xiaoyao Li, 2024/11/05
- [PATCH v6 16/60] i386/tdx: Implement user specified tsc frequency, Xiaoyao Li, 2024/11/05
- [PATCH v6 18/60] i386/tdvf: Introduce function to parse TDVF metadata, Xiaoyao Li, 2024/11/05
- [PATCH v6 19/60] i386/tdx: Parse TDVF metadata for TDX VM, Xiaoyao Li, 2024/11/05
- [PATCH v6 20/60] i386/tdx: Don't initialize pc.rom for TDX VMs, Xiaoyao Li, 2024/11/05
- [PATCH v6 21/60] i386/tdx: Track mem_ptr for each firmware entry of TDVF, Xiaoyao Li, 2024/11/05
- [PATCH v6 23/60] headers: Add definitions from UEFI spec for volumes, resources, etc..., Xiaoyao Li, 2024/11/05
- [PATCH v6 25/60] i386/tdx: Add TDVF memory via KVM_TDX_INIT_MEM_REGION,
Xiaoyao Li <=
- [PATCH v6 22/60] i386/tdx: Track RAM entries for TDX VM, Xiaoyao Li, 2024/11/05
- [PATCH v6 26/60] i386/tdx: Call KVM_TDX_INIT_VCPU to initialize TDX vcpu, Xiaoyao Li, 2024/11/05
- [PATCH v6 27/60] i386/tdx: Finalize TDX VM, Xiaoyao Li, 2024/11/05
- [PATCH v6 29/60] i386/tdx: Handle KVM_SYSTEM_EVENT_TDX_FATAL, Xiaoyao Li, 2024/11/05
- [PATCH v6 24/60] i386/tdx: Setup the TD HOB list, Xiaoyao Li, 2024/11/05
- [PATCH v6 28/60] i386/tdx: Enable user exit on KVM_HC_MAP_GPA_RANGE, Xiaoyao Li, 2024/11/05
- [PATCH v6 30/60] i386/tdx: Wire TDX_REPORT_FATAL_ERROR with GuestPanic facility, Xiaoyao Li, 2024/11/05