316 lines
9.7 KiB
Diff
316 lines
9.7 KiB
Diff
From e31406cad5a27e2a807dfacb92d9d44efd1615fe Mon Sep 17 00:00:00 2001
|
|
From: Paolo Bonzini <pbonzini@redhat.com>
|
|
Date: Fri, 18 Jul 2025 18:03:46 +0200
|
|
Subject: [PATCH 046/115] i386/tdvf: Introduce function to parse TDVF metadata
|
|
|
|
RH-Author: Paolo Bonzini <pbonzini@redhat.com>
|
|
RH-MergeRequest: 391: TDX support, including attestation and device assignment
|
|
RH-Jira: RHEL-15710 RHEL-20798 RHEL-49728
|
|
RH-Acked-by: Yash Mankad <None>
|
|
RH-Acked-by: Peter Xu <peterx@redhat.com>
|
|
RH-Acked-by: David Hildenbrand <david@redhat.com>
|
|
RH-Commit: [46/115] 5eed49eea4f80f57a310e69a47ee220bd41f6188 (bonzini/rhel-qemu-kvm)
|
|
|
|
TDX VM needs to boot with its specialized firmware, Trusted Domain
|
|
Virtual Firmware (TDVF). QEMU needs to parse TDVF and map it in TD
|
|
guest memory prior to running the TDX VM.
|
|
|
|
A TDVF Metadata in TDVF image describes the structure of firmware.
|
|
QEMU refers to it to setup memory for TDVF. Introduce function
|
|
tdvf_parse_metadata() to parse the metadata from TDVF image and store
|
|
the info of each TDVF section.
|
|
|
|
TDX metadata is located by a TDX metadata offset block, which is a
|
|
GUID-ed structure. The data portion of the GUID structure contains
|
|
only an 4-byte field that is the offset of TDX metadata to the end
|
|
of firmware file.
|
|
|
|
Select X86_FW_OVMF when TDX is enable to leverage existing functions
|
|
to parse and search OVMF's GUID-ed structures.
|
|
|
|
Signed-off-by: Isaku Yamahata <isaku.yamahata@intel.com>
|
|
Co-developed-by: Xiaoyao Li <xiaoyao.li@intel.com>
|
|
Signed-off-by: Xiaoyao Li <xiaoyao.li@intel.com>
|
|
Acked-by: Gerd Hoffmann <kraxel@redhat.com>
|
|
Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
|
|
Link: https://lore.kernel.org/r/20250508150002.689633-18-xiaoyao.li@intel.com
|
|
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
|
|
(cherry picked from commit b65a6011d16c4f7cb2eb227ab1bc735850475288)
|
|
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
|
|
|
|
Conflicts: system/ -> sysemu/
|
|
---
|
|
hw/i386/Kconfig | 1 +
|
|
hw/i386/meson.build | 1 +
|
|
hw/i386/tdvf.c | 188 +++++++++++++++++++++++++++++++++++++++++
|
|
include/hw/i386/tdvf.h | 38 +++++++++
|
|
4 files changed, 228 insertions(+)
|
|
create mode 100644 hw/i386/tdvf.c
|
|
create mode 100644 include/hw/i386/tdvf.h
|
|
|
|
diff --git a/hw/i386/Kconfig b/hw/i386/Kconfig
|
|
index edd61cd2aa..31e50e2ebf 100644
|
|
--- a/hw/i386/Kconfig
|
|
+++ b/hw/i386/Kconfig
|
|
@@ -12,6 +12,7 @@ config SGX
|
|
|
|
config TDX
|
|
bool
|
|
+ select X86_FW_OVMF
|
|
depends on KVM
|
|
|
|
config PC
|
|
diff --git a/hw/i386/meson.build b/hw/i386/meson.build
|
|
index 03aad10df7..d6d8023664 100644
|
|
--- a/hw/i386/meson.build
|
|
+++ b/hw/i386/meson.build
|
|
@@ -31,6 +31,7 @@ i386_ss.add(when: 'CONFIG_PC', if_true: files(
|
|
'port92.c'))
|
|
i386_ss.add(when: 'CONFIG_X86_FW_OVMF', if_true: files('pc_sysfw_ovmf.c'),
|
|
if_false: files('pc_sysfw_ovmf-stubs.c'))
|
|
+i386_ss.add(when: 'CONFIG_TDX', if_true: files('tdvf.c'))
|
|
|
|
subdir('kvm')
|
|
subdir('xen')
|
|
diff --git a/hw/i386/tdvf.c b/hw/i386/tdvf.c
|
|
new file mode 100644
|
|
index 0000000000..824a387d42
|
|
--- /dev/null
|
|
+++ b/hw/i386/tdvf.c
|
|
@@ -0,0 +1,188 @@
|
|
+/*
|
|
+ * Copyright (c) 2025 Intel Corporation
|
|
+ * Author: Isaku Yamahata <isaku.yamahata at gmail.com>
|
|
+ * <isaku.yamahata at intel.com>
|
|
+ * Xiaoyao Li <xiaoyao.li@intel.com>
|
|
+ *
|
|
+ * SPDX-License-Identifier: GPL-2.0-or-later
|
|
+ */
|
|
+
|
|
+#include "qemu/osdep.h"
|
|
+#include "qemu/error-report.h"
|
|
+
|
|
+#include "hw/i386/pc.h"
|
|
+#include "hw/i386/tdvf.h"
|
|
+#include "sysemu/kvm.h"
|
|
+
|
|
+#define TDX_METADATA_OFFSET_GUID "e47a6535-984a-4798-865e-4685a7bf8ec2"
|
|
+#define TDX_METADATA_VERSION 1
|
|
+#define TDVF_SIGNATURE 0x46564454 /* TDVF as little endian */
|
|
+#define TDVF_ALIGNMENT 4096
|
|
+
|
|
+/*
|
|
+ * the raw structs read from TDVF keeps the name convention in
|
|
+ * TDVF Design Guide spec.
|
|
+ */
|
|
+typedef struct {
|
|
+ uint32_t DataOffset;
|
|
+ uint32_t RawDataSize;
|
|
+ uint64_t MemoryAddress;
|
|
+ uint64_t MemoryDataSize;
|
|
+ uint32_t Type;
|
|
+ uint32_t Attributes;
|
|
+} TdvfSectionEntry;
|
|
+
|
|
+typedef struct {
|
|
+ uint32_t Signature;
|
|
+ uint32_t Length;
|
|
+ uint32_t Version;
|
|
+ uint32_t NumberOfSectionEntries;
|
|
+ TdvfSectionEntry SectionEntries[];
|
|
+} TdvfMetadata;
|
|
+
|
|
+struct tdx_metadata_offset {
|
|
+ uint32_t offset;
|
|
+};
|
|
+
|
|
+static TdvfMetadata *tdvf_get_metadata(void *flash_ptr, int size)
|
|
+{
|
|
+ TdvfMetadata *metadata;
|
|
+ uint32_t offset = 0;
|
|
+ uint8_t *data;
|
|
+
|
|
+ if ((uint32_t) size != size) {
|
|
+ return NULL;
|
|
+ }
|
|
+
|
|
+ if (pc_system_ovmf_table_find(TDX_METADATA_OFFSET_GUID, &data, NULL)) {
|
|
+ offset = size - le32_to_cpu(((struct tdx_metadata_offset *)data)->offset);
|
|
+
|
|
+ if (offset + sizeof(*metadata) > size) {
|
|
+ return NULL;
|
|
+ }
|
|
+ } else {
|
|
+ error_report("Cannot find TDX_METADATA_OFFSET_GUID");
|
|
+ return NULL;
|
|
+ }
|
|
+
|
|
+ metadata = flash_ptr + offset;
|
|
+
|
|
+ /* Finally, verify the signature to determine if this is a TDVF image. */
|
|
+ metadata->Signature = le32_to_cpu(metadata->Signature);
|
|
+ if (metadata->Signature != TDVF_SIGNATURE) {
|
|
+ error_report("Invalid TDVF signature in metadata!");
|
|
+ return NULL;
|
|
+ }
|
|
+
|
|
+ /* Sanity check that the TDVF doesn't overlap its own metadata. */
|
|
+ metadata->Length = le32_to_cpu(metadata->Length);
|
|
+ if (offset + metadata->Length > size) {
|
|
+ return NULL;
|
|
+ }
|
|
+
|
|
+ /* Only version 1 is supported/defined. */
|
|
+ metadata->Version = le32_to_cpu(metadata->Version);
|
|
+ if (metadata->Version != TDX_METADATA_VERSION) {
|
|
+ return NULL;
|
|
+ }
|
|
+
|
|
+ return metadata;
|
|
+}
|
|
+
|
|
+static int tdvf_parse_and_check_section_entry(const TdvfSectionEntry *src,
|
|
+ TdxFirmwareEntry *entry)
|
|
+{
|
|
+ entry->data_offset = le32_to_cpu(src->DataOffset);
|
|
+ entry->data_len = le32_to_cpu(src->RawDataSize);
|
|
+ entry->address = le64_to_cpu(src->MemoryAddress);
|
|
+ entry->size = le64_to_cpu(src->MemoryDataSize);
|
|
+ entry->type = le32_to_cpu(src->Type);
|
|
+ entry->attributes = le32_to_cpu(src->Attributes);
|
|
+
|
|
+ /* sanity check */
|
|
+ if (entry->size < entry->data_len) {
|
|
+ error_report("Broken metadata RawDataSize 0x%x MemoryDataSize 0x%lx",
|
|
+ entry->data_len, entry->size);
|
|
+ return -1;
|
|
+ }
|
|
+ if (!QEMU_IS_ALIGNED(entry->address, TDVF_ALIGNMENT)) {
|
|
+ error_report("MemoryAddress 0x%lx not page aligned", entry->address);
|
|
+ return -1;
|
|
+ }
|
|
+ if (!QEMU_IS_ALIGNED(entry->size, TDVF_ALIGNMENT)) {
|
|
+ error_report("MemoryDataSize 0x%lx not page aligned", entry->size);
|
|
+ return -1;
|
|
+ }
|
|
+
|
|
+ switch (entry->type) {
|
|
+ case TDVF_SECTION_TYPE_BFV:
|
|
+ case TDVF_SECTION_TYPE_CFV:
|
|
+ /* The sections that must be copied from firmware image to TD memory */
|
|
+ if (entry->data_len == 0) {
|
|
+ error_report("%d section with RawDataSize == 0", entry->type);
|
|
+ return -1;
|
|
+ }
|
|
+ break;
|
|
+ case TDVF_SECTION_TYPE_TD_HOB:
|
|
+ case TDVF_SECTION_TYPE_TEMP_MEM:
|
|
+ /* The sections that no need to be copied from firmware image */
|
|
+ if (entry->data_len != 0) {
|
|
+ error_report("%d section with RawDataSize 0x%x != 0",
|
|
+ entry->type, entry->data_len);
|
|
+ return -1;
|
|
+ }
|
|
+ break;
|
|
+ default:
|
|
+ error_report("TDVF contains unsupported section type %d", entry->type);
|
|
+ return -1;
|
|
+ }
|
|
+
|
|
+ return 0;
|
|
+}
|
|
+
|
|
+int tdvf_parse_metadata(TdxFirmware *fw, void *flash_ptr, int size)
|
|
+{
|
|
+ g_autofree TdvfSectionEntry *sections = NULL;
|
|
+ TdvfMetadata *metadata;
|
|
+ ssize_t entries_size;
|
|
+ int i;
|
|
+
|
|
+ metadata = tdvf_get_metadata(flash_ptr, size);
|
|
+ if (!metadata) {
|
|
+ return -EINVAL;
|
|
+ }
|
|
+
|
|
+ /* load and parse metadata entries */
|
|
+ fw->nr_entries = le32_to_cpu(metadata->NumberOfSectionEntries);
|
|
+ if (fw->nr_entries < 2) {
|
|
+ error_report("Invalid number of fw entries (%u) in TDVF Metadata",
|
|
+ fw->nr_entries);
|
|
+ return -EINVAL;
|
|
+ }
|
|
+
|
|
+ entries_size = fw->nr_entries * sizeof(TdvfSectionEntry);
|
|
+ if (metadata->Length != sizeof(*metadata) + entries_size) {
|
|
+ error_report("TDVF metadata len (0x%x) mismatch, expected (0x%x)",
|
|
+ metadata->Length,
|
|
+ (uint32_t)(sizeof(*metadata) + entries_size));
|
|
+ return -EINVAL;
|
|
+ }
|
|
+
|
|
+ fw->entries = g_new(TdxFirmwareEntry, fw->nr_entries);
|
|
+ sections = g_new(TdvfSectionEntry, fw->nr_entries);
|
|
+
|
|
+ memcpy(sections, (void *)metadata + sizeof(*metadata), entries_size);
|
|
+
|
|
+ for (i = 0; i < fw->nr_entries; i++) {
|
|
+ if (tdvf_parse_and_check_section_entry(§ions[i], &fw->entries[i])) {
|
|
+ goto err;
|
|
+ }
|
|
+ }
|
|
+
|
|
+ return 0;
|
|
+
|
|
+err:
|
|
+ fw->entries = 0;
|
|
+ g_free(fw->entries);
|
|
+ return -EINVAL;
|
|
+}
|
|
diff --git a/include/hw/i386/tdvf.h b/include/hw/i386/tdvf.h
|
|
new file mode 100644
|
|
index 0000000000..7ebcac42a3
|
|
--- /dev/null
|
|
+++ b/include/hw/i386/tdvf.h
|
|
@@ -0,0 +1,38 @@
|
|
+/*
|
|
+ * Copyright (c) 2025 Intel Corporation
|
|
+ * Author: Isaku Yamahata <isaku.yamahata at gmail.com>
|
|
+ * <isaku.yamahata at intel.com>
|
|
+ *
|
|
+ * SPDX-License-Identifier: GPL-2.0-or-later
|
|
+ */
|
|
+
|
|
+#ifndef HW_I386_TDVF_H
|
|
+#define HW_I386_TDVF_H
|
|
+
|
|
+#include "qemu/osdep.h"
|
|
+
|
|
+#define TDVF_SECTION_TYPE_BFV 0
|
|
+#define TDVF_SECTION_TYPE_CFV 1
|
|
+#define TDVF_SECTION_TYPE_TD_HOB 2
|
|
+#define TDVF_SECTION_TYPE_TEMP_MEM 3
|
|
+
|
|
+#define TDVF_SECTION_ATTRIBUTES_MR_EXTEND (1U << 0)
|
|
+#define TDVF_SECTION_ATTRIBUTES_PAGE_AUG (1U << 1)
|
|
+
|
|
+typedef struct TdxFirmwareEntry {
|
|
+ uint32_t data_offset;
|
|
+ uint32_t data_len;
|
|
+ uint64_t address;
|
|
+ uint64_t size;
|
|
+ uint32_t type;
|
|
+ uint32_t attributes;
|
|
+} TdxFirmwareEntry;
|
|
+
|
|
+typedef struct TdxFirmware {
|
|
+ uint32_t nr_entries;
|
|
+ TdxFirmwareEntry *entries;
|
|
+} TdxFirmware;
|
|
+
|
|
+int tdvf_parse_metadata(TdxFirmware *fw, void *flash_ptr, int size);
|
|
+
|
|
+#endif /* HW_I386_TDVF_H */
|
|
--
|
|
2.50.1
|
|
|