Skip to content

Commit c4e0862

Browse files
kaihuanghansendc
authored andcommitted
x86/virt/tdx: Start to track all global metadata in one structure
The TDX module provides a set of "Global Metadata Fields". They report things like TDX module version, supported features, and fields related to create/run TDX guests and so on. Today the kernel only reads "TD Memory Region" (TDMR) related fields for module initialization. KVM will need to read additional metadata fields to run TDX guests. Move towards having the TDX host core-kernel provide a centralized, canonical, and immutable structure for the global metadata that comes out from the TDX module for all kernel components to use. As the first step, introduce a new 'struct tdx_sys_info' to track all global metadata fields. TDX categorizes global metadata fields into different "Classes". E.g., the TDMR related fields are under class "TDMR Info". Instead of making 'struct tdx_sys_info' a plain structure to contain all metadata fields, organize them in smaller structures based on the "Class". This allows those metadata fields to be used in finer granularity thus makes the code clearer. E.g., construct_tdmrs() can just take the structure which contains "TDMR Info" metadata fields. Add get_tdx_sys_info() as the placeholder to read all metadata fields. Have it only call get_tdx_sys_info_tdmr() to read TDMR related fields for now. Place get_tdx_sys_info() as the first step of init_tdx_module() to enable early prerequisite checks on the metadata to support early module initialization abort. This results in moving get_tdx_sys_info_tdmr() to be before build_tdx_memlist(), but this is fine because there are no dependencies between these two functions. Signed-off-by: Kai Huang <[email protected]> Signed-off-by: Dave Hansen <[email protected]> Reviewed-by: Adrian Hunter <[email protected]> Reviewed-by: Dan Williams <[email protected]> Link: https://lore.kernel.org/all/bfacb4e90527cf79d4be0d1753e6f318eea21118.1734188033.git.kai.huang%40intel.com
1 parent e8aa393 commit c4e0862

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

arch/x86/virt/vmx/tdx/tdx.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,11 @@ static int get_tdx_sys_info_tdmr(struct tdx_sys_info_tdmr *sysinfo_tdmr)
326326
return 0;
327327
}
328328

329+
static int get_tdx_sys_info(struct tdx_sys_info *sysinfo)
330+
{
331+
return get_tdx_sys_info_tdmr(&sysinfo->tdmr);
332+
}
333+
329334
/* Calculate the actual TDMR size */
330335
static int tdmr_size_single(u16 max_reserved_per_tdmr)
331336
{
@@ -1098,9 +1103,13 @@ static int init_tdmrs(struct tdmr_info_list *tdmr_list)
10981103

10991104
static int init_tdx_module(void)
11001105
{
1101-
struct tdx_sys_info_tdmr sysinfo_tdmr;
1106+
struct tdx_sys_info sysinfo;
11021107
int ret;
11031108

1109+
ret = get_tdx_sys_info(&sysinfo);
1110+
if (ret)
1111+
return ret;
1112+
11041113
/*
11051114
* To keep things simple, assume that all TDX-protected memory
11061115
* will come from the page allocator. Make sure all pages in the
@@ -1117,17 +1126,13 @@ static int init_tdx_module(void)
11171126
if (ret)
11181127
goto out_put_tdxmem;
11191128

1120-
ret = get_tdx_sys_info_tdmr(&sysinfo_tdmr);
1121-
if (ret)
1122-
goto err_free_tdxmem;
1123-
11241129
/* Allocate enough space for constructing TDMRs */
1125-
ret = alloc_tdmr_list(&tdx_tdmr_list, &sysinfo_tdmr);
1130+
ret = alloc_tdmr_list(&tdx_tdmr_list, &sysinfo.tdmr);
11261131
if (ret)
11271132
goto err_free_tdxmem;
11281133

11291134
/* Cover all TDX-usable memory regions in TDMRs */
1130-
ret = construct_tdmrs(&tdx_memlist, &tdx_tdmr_list, &sysinfo_tdmr);
1135+
ret = construct_tdmrs(&tdx_memlist, &tdx_tdmr_list, &sysinfo.tdmr);
11311136
if (ret)
11321137
goto err_free_tdmrs;
11331138

arch/x86/virt/vmx/tdx/tdx.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,18 @@ struct tdmr_info {
8080
DECLARE_FLEX_ARRAY(struct tdmr_reserved_area, reserved_areas);
8181
} __packed __aligned(TDMR_INFO_ALIGNMENT);
8282

83+
/* Class "TDMR info" */
84+
struct tdx_sys_info_tdmr {
85+
u16 max_tdmrs;
86+
u16 max_reserved_per_tdmr;
87+
u16 pamt_entry_size[TDX_PS_NR];
88+
};
89+
90+
/* Kernel used global metadata fields */
91+
struct tdx_sys_info {
92+
struct tdx_sys_info_tdmr tdmr;
93+
};
94+
8395
/*
8496
* Do not put any hardware-defined TDX structure representations below
8597
* this comment!
@@ -99,13 +111,6 @@ struct tdx_memblock {
99111
int nid;
100112
};
101113

102-
/* "TDMR info" part of "Global Scope Metadata" for constructing TDMRs */
103-
struct tdx_sys_info_tdmr {
104-
u16 max_tdmrs;
105-
u16 max_reserved_per_tdmr;
106-
u16 pamt_entry_size[TDX_PS_NR];
107-
};
108-
109114
/* Warn if kernel has less than TDMR_NR_WARN TDMRs after allocation */
110115
#define TDMR_NR_WARN 4
111116

0 commit comments

Comments
 (0)