[PATCH v9 14/18] x86/virt/tdx: Configure TDX module with the TDMRs and global KeyID

Kai Huang
2023-02-13T12:03:47Z

The TDX module uses a private KeyID as the "global KeyID" for mapping
things like the PAMT and other TDX metadata. This KeyID has already
been reserved when detecting TDX during the kernel early boot.

After the list of "TD Memory Regions" (TDMRs) has been constructed to
cover all TDX-usable memory regions, the next step is to pass them to
the TDX module together with the global KeyID.

Signed-off-by: Kai Huang <kai.huang@intel.com>
Reviewed-by: Isaku Yamahata <isaku.yamahata@intel.com>
---

v8 -> v9:
- Removed 'i.e.' in changelog and removed the passive voice (Dave).
- Improved changelog (Dave).
- Changed the way to allocate aligned PA array (Dave).
- Moved reserving the TDX global KeyID to the second patch, and also
changed 'tdx_keyid_start' and 'nr_tdx_keyid' to guest's KeyIDs in
that patch (Dave).

v7 -> v8:
- Merged "Reserve TDX module global KeyID" patch to this patch, and
removed 'tdx_global_keyid' but use 'tdx_keyid_start' directly.
- Changed changelog accordingly.
- Changed how to allocate aligned array (Dave).

---
arch/x86/virt/vmx/tdx/tdx.c | 41 ++++++++++++++++++++++++++++++++++++-
arch/x86/virt/vmx/tdx/tdx.h | 2 ++
2 files changed, 42 insertions(+), 1 deletion(-)

diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c
index 34aed2aa9f4b..5a4163d40f58 100644
--- a/arch/x86/virt/vmx/tdx/tdx.c
+++ b/arch/x86/virt/vmx/tdx/tdx.c
@@ -24,6 +24,7 @@
#include <linux/pfn.h>
#include <linux/align.h>
#include <linux/sort.h>
+#include <linux/log2.h>
#include <asm/msr-index.h>
#include <asm/msr.h>
#include <asm/page.h>
@@ -997,6 +998,39 @@ static int construct_tdmrs(struct list_head *tmb_list,
return ret;
}

+static int config_tdx_module(struct tdmr_info_list *tdmr_list, u64 global_keyid)
+{
+ u64 *tdmr_pa_array;
+ size_t array_sz;
+ int i, ret;
+
+ /*
+ * TDMRs are passed to the TDX module via an array of physical
+ * addresses of each TDMR. The array itself also has certain
+ * alignment requirement.
+ */
+ array_sz = tdmr_list->nr_consumed_tdmrs * sizeof(u64);
+ array_sz = roundup_pow_of_two(array_sz);
+ if (array_sz < TDMR_INFO_PA_ARRAY_ALIGNMENT)
+ array_sz = TDMR_INFO_PA_ARRAY_ALIGNMENT;
+
+ tdmr_pa_array = kzalloc(array_sz, GFP_KERNEL);
+ if (!tdmr_pa_array)
+ return -ENOMEM;
+
+ for (i = 0; i < tdmr_list->nr_consumed_tdmrs; i++)
+ tdmr_pa_array[i] = __pa(tdmr_entry(tdmr_list, i));
+
+ ret = seamcall(TDH_SYS_CONFIG, __pa(tdmr_pa_array),
+ tdmr_list->nr_consumed_tdmrs,
+ global_keyid, 0, NULL, NULL);
+
+ /* Free the array as it is not required anymore. */
+ kfree(tdmr_pa_array);
+
+ return ret;
+}
+
static int init_tdx_module(void)
{
static DECLARE_PADDED_STRUCT(tdsysinfo_struct, tdsysinfo,
@@ -1061,10 +1095,14 @@ static int init_tdx_module(void)
if (ret)
goto out_free_tdmrs;

+ /* Pass the TDMRs and the global KeyID to the TDX module */
+ ret = config_tdx_module(&tdx_tdmr_list, tdx_global_keyid);
+ if (ret)
+ goto out_free_pamts;
+
/*
* TODO:
*
- * - Configure the TDMRs and the global KeyID to the TDX module.
* - Configure the global KeyID on all packages.
* - Initialize all TDMRs.
*
@@ -1072,6 +1110,7 @@ static int init_tdx_module(void)
*/

ret = -EINVAL;
+out_free_pamts:
if (ret)
tdmrs_free_pamt_all(&tdx_tdmr_list);
else
diff --git a/arch/x86/virt/vmx/tdx/tdx.h b/arch/x86/virt/vmx/tdx/tdx.h
index 7348ffdfc287..7b34ac257b9a 100644
--- a/arch/x86/virt/vmx/tdx/tdx.h
+++ b/arch/x86/virt/vmx/tdx/tdx.h
@@ -17,6 +17,7 @@
* TDX module SEAMCALL leaf functions
*/
#define TDH_SYS_INFO 32
+#define TDH_SYS_CONFIG 45

struct cmr_info {
u64 base;
@@ -93,6 +94,7 @@ struct tdmr_reserved_area {
} __packed;

#define TDMR_INFO_ALIGNMENT 512
+#define TDMR_INFO_PA_ARRAY_ALIGNMENT 512

struct tdmr_info {
u64 base;
--
2.39.1