TDX architecturally supports up to 32 CMRs. The global metadata field
"NUM_CMRS" reports the number of CMR entries that can be read by the
kernel. However, that field may just report the maximum number of CMRs
albeit the actual number of CMRs is smaller, in which case there are
tail null CMRs (size is 0).
Trim away those null CMRs, and print valid CMRs since they are useful
at least to developers.
More information about CMR can be found at "Intel TDX ISA Background:
Convertible Memory Ranges (CMRs)" in TDX 1.5 base spec [1], and
"CMR_INFO" in TDX 1.5 ABI spec [2].
Now get_tdx_sys_info() just reads kernel-needed global metadata to
kernel structure, and it is auto-generated. Add a wrapper function
init_tdx_sys_info() to invoke get_tdx_sys_info() and provide room to do
additional things like dealing with CMRs.
Link: https://cdrdv2.intel.com/v1/dl/getContent/733575 [1]
Link: https://cdrdv2.intel.com/v1/dl/getContent/733579 [2]
Signed-off-by: Kai Huang <kai.huang@intel.com>
---
arch/x86/virt/vmx/tdx/tdx.c | 56 ++++++++++++++++++++++++++++++++++++-
1 file changed, 55 insertions(+), 1 deletion(-)
diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c
index 43ec56db5084..e81bdcfc20bf 100644
--- a/arch/x86/virt/vmx/tdx/tdx.c
+++ b/arch/x86/virt/vmx/tdx/tdx.c
@@ -272,6 +272,60 @@ static int read_sys_metadata_field(u64 field_id, u64 *data)
#include "tdx_global_metadata.c"
+/* Update the @sysinfo_cmr->num_cmrs to trim tail null CMRs */
+static void trim_null_tail_cmrs(struct tdx_sys_info_cmr *sysinfo_cmr)
+{
+ int i;
+
+ /*
+ * The TDX module may report the maximum number of CMRs that
+ * TDX architecturally supports as the actual number of CMRs,
+ * despite the latter is smaller. In this case some tail
+ * CMR(s) will be null (size is 0). Trim them away.
+ *
+ * Note the CMRs are generated by the BIOS, but the MCHECK
+ * verifies CMRs before enabling TDX on hardware. Skip other
+ * sanity checks (e.g., verify CMR is 4KB aligned) but trust
+ * MCHECK to work properly.
+ *
+ * The spec doesn't say whether it's legal to have null CMRs
+ * in the middle of valid CMRs. For now assume no sane BIOS
+ * would do that (even MCHECK allows).
+ */
+ for (i = 0; i < sysinfo_cmr->num_cmrs; i++)
+ if (!sysinfo_cmr->cmr_size[i])
+ break;
+
+ sysinfo_cmr->num_cmrs = i;
+}
+
+static void print_cmrs(struct tdx_sys_info_cmr *sysinfo_cmr)
+{
+ int i;
+
+ for (i = 0; i < sysinfo_cmr->num_cmrs; i++) {
+ u64 cmr_base = sysinfo_cmr->cmr_base[i];
+ u64 cmr_size = sysinfo_cmr->cmr_size[i];
+
+ pr_info("CMR[%d]: [0x%llx, 0x%llx)\n", i, cmr_base,
+ cmr_base + cmr_size);
+ }
+}
+
+static int init_tdx_sys_info(struct tdx_sys_info *sysinfo)
+{
+ int ret;
+
+ ret = get_tdx_sys_info(sysinfo);
+ if (ret)
+ return ret;
+
+ trim_null_tail_cmrs(&sysinfo->cmr);
+ print_cmrs(&sysinfo->cmr);
+
+ return 0;
+}
+
/* Calculate the actual TDMR size */
static int tdmr_size_single(u16 max_reserved_per_tdmr)
{
@@ -1051,7 +1105,7 @@ static int init_tdx_module(void)
struct tdx_sys_info sysinfo;
int ret;
- ret = get_tdx_sys_info(&sysinfo);
+ ret = init_tdx_sys_info(&sysinfo);
if (ret)
return ret;
--
2.46.2