Skip to contentSkip to conversation

x86/tdx: Dump TDX version During the TD Bootup

PATCH Superseded Yi Sun 3 participants 3 messages Oct 12, 2023 – Oct 13, 2023

Subsystems (2)

v5 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Yi Sun

author
#0 lore raw

PATCH v5 1/1 x86/tdx: Dump TDX version During the TD Bootup

It is essential for TD users to be aware of the vendor and version of the current TDX. Additionally, they can reference the TDX version when reporting bugs or issues.

Furthermore, the applications or device drivers running in TD can achieve enhanced reliability and flexibility by following the TDX Module ABI specification, because there are significant differences between different versions of TDX, as mentioned in the "Intel® TDX Module Incompatibilities between v1.0 and v1.5" reference.

Add function detect_tdx_version to fetch and dump the version of the TDX, which is called during TD initialization. Obtain the info by calling TDG.SYS.RD, including the major and minor version numbers and vendor ID.

The TDCALL TDG.SYS.RD originates from TDX version 1.5. If the error TDCALL_INVALID_OPERAND occurs, it should be treated as TDX version 1.0.

---
V1 -> V2:
- Move the defination of field IDs and the struct tdg_sys_info to tdx.c. (Kuppuswamy Sathyanarayanan)

V2 -> V3:
- Move the allocation of struct tdg_sys_info on stack inside tdx_early_init() and pass down to tdg_get_sysinfo() to fill. (Kirill Shutemov)

V3 -> V4:
- Rebase the patch on top of the latest tip tree. (Huang, Kai)
- Change the return value of function tdg_get_sysinfo as void, and zero out tdg_sys_info when error occurs. (Kuppuswamy Sathyanarayanan)

V4 -> V5:
- Print the version info inside the function detect_tdx_version, but not tdx_early_init(). Remove the structure tdg_sys_info, but have 3 local variables instead. (Huang, Kai)
- Check for errors on each call to TDG.SYS.RD (Nikolay & Sathyanarayanan)

arch/x86/coco/tdx/tdx.c +67 -1
index 3e6dbd2199cf..23b56952e132 100644
@@ -37,6 +37,15 @@
 
 #define TDREPORT_SUBTYPE_0	0
 
+/*
+ * TDX metadata base field id, used by TDCALL TDG.SYS.RD
+ * See TDX ABI Spec Global Metadata Fields
+ */
+#define TDX_SYS_VENDOR_ID_FID		0x0800000200000000ULL
+#define TDX_SYS_MINOR_FID		0x0800000100000003ULL
+#define TDX_SYS_MAJOR_FID		0x0800000100000004ULL
+#define TDX_VENDOR_INTEL		0x8086
+
 /* Called from __tdx_hypercall() for unrecoverable failure */
 noinstr void __noreturn __tdx_hypercall_failed(void)
 {
@@ -800,6 +809,63 @@ static bool tdx_enc_status_change_finish(unsigned long vaddr, int numpages,
 	return true;
 }
 
+/*
+ * Detect TDX Module version info from TDG.SYS.RD TDCALL
+ */
+static void detect_tdx_version(void)
+{
+	struct tdx_module_args args = {};
+	u32 vendor_id = TDX_VENDOR_INTEL;
+	u16 major_version = 0;
+	u16 minor_version = 0;
+	u64 ret;
+
+	/*
+	 * TDCALL leaf TDG_SYS_RD
+	 */
+	args.rdx = TDX_SYS_VENDOR_ID_FID;
+	ret = __tdcall_ret(TDG_SYS_RD, &args);
+	/*
+	 * The TDCALL TDG.SYS.RD originates from TDX version 1.5.
+	 * Treat TDCALL_INVALID_OPERAND error as TDX version 1.0.
+	 */
+	if (TDCALL_RETURN_CODE(ret) == TDCALL_INVALID_OPERAND)
+		goto version_1_0;
+	if (ret) {
+		WARN(1, "TDX detection: TDG.SYS.RD(VENDOR_ID) error, return %llu\n",
+		     ret);
+		return;
+	}
+	vendor_id = (u32)args.r8;
+
+	args.rdx = TDX_SYS_MAJOR_FID;
+	ret = __tdcall_ret(TDG_SYS_RD, &args);
+	if (ret) {
+		WARN(1, "TDX detection: TDG.SYS.RD(MAJOR) error, return %llu\n",
+		     ret);
+		return;
+	}
+	major_version = (u16)args.r8;
+
+	args.rdx = TDX_SYS_MINOR_FID;
+	ret = __tdcall_ret(TDG_SYS_RD, &args);
+	if (ret) {
+		WARN(1, "TDX detection: TDG.SYS.RD(MINOR) error, return %llu\n",
+		     ret);
+		return;
+	}
+	minor_version = (u16)args.r8;
+
+	pr_info("TDX detected. TDX version:%u.%u VendorID:%x\n",
+		major_version, minor_version, vendor_id);
+
+	return;
+
+	/* TDX 1.0 does not have the TDCALL TDG.SYS.RD */
+version_1_0:
+	pr_info("TDX detected. TDG.SYS.RD not available, assuming TDX version: 1.x (x<5)\n");
+}
+
 void __init tdx_early_init(void)
 {
 	struct tdx_module_args args = {
@@ -867,5 +933,5 @@ void __init tdx_early_init(void)
 	 */
 	x86_cpuinit.parallel_bringup = false;
 
-	pr_info("Guest detected\n");
+	detect_tdx_version();
 }
arch/x86/include/asm/shared/tdx.h +1 -0
index f74695dea217..1a0cacad5a0c 100644
@@ -17,6 +17,7 @@
 #define TDG_MR_REPORT			4
 #define TDG_MEM_PAGE_ACCEPT		6
 #define TDG_VM_WR			8
+#define TDG_SYS_RD			11
 
 /* TDCS fields. To be used by TDG.VM.WR and TDG.VM.RD module calls */
 #define TDCS_NOTIFY_ENABLES		0x9100000000000010
Signature · 1 line
2.34.1
#1 lore raw
Quoted 3 lines · Yi Sun

It is essential for TD users to be aware of the vendor and version of the current TDX. Additionally, they can reference the TDX version when reporting bugs or issues.

... and they will all have to pull this "essential" information out of dmesg?

If this is essential, why stick it in dmesg where it can be overwritten?

Quoted 5 lines

Furthermore, the applications or device drivers running in TD can achieve enhanced reliability and flexibility by following the TDX Module ABI specification, because there are significant differences between different versions of TDX, as mentioned in the "Intel® TDX Module Incompatibilities between v1.0 and v1.5" reference.

This is orthogonal to this patch. No applications or device drivers can do anything with the result of these version queries.

Quoted 3 lines

Add function detect_tdx_version to fetch and dump the version of the TDX, which is called during TD initialization. Obtain the info by calling TDG.SYS.RD, including the major and minor version numbers and vendor ID.

You don't need to rewrite the code in text form.

Quoted 2 lines

The TDCALL TDG.SYS.RD originates from TDX version 1.5. If the error TDCALL_INVALID_OPERAND occurs, it should be treated as TDX version 1.0.

I don't understand what this is trying to say.

Quoted 15 lines

#define TDREPORT_SUBTYPE_0 0

arch/x86/coco/tdx/tdx.c +9 -0
+/*
+ * TDX metadata base field id, used by TDCALL TDG.SYS.RD
+ * See TDX ABI Spec Global Metadata Fields
+ */
+#define TDX_SYS_VENDOR_ID_FID		0x0800000200000000ULL
+#define TDX_SYS_MINOR_FID		0x0800000100000003ULL
+#define TDX_SYS_MAJOR_FID		0x0800000100000004ULL
+#define TDX_VENDOR_INTEL		0x8086
+
 /* Called from __tdx_hypercall() for unrecoverable failure */
 noinstr void __noreturn __tdx_hypercall_failed(void)
 {

arch/x86/coco/tdx/tdx.c static bool tdx_enc_status_change_finish(unsigned long vaddr, int numpages, · line 818

Quoted 10 lines
arch/x86/coco/tdx/tdx.c +7 -0
@@ -800,6 +809,63 @@ static bool tdx_enc_status_change_finish(unsigned long vaddr, int numpages,
 	return true;
 }
 
+/*
+ * Detect TDX Module version info from TDG.SYS.RD TDCALL
+ */
+static void detect_tdx_version(void)
+{
+	struct tdx_module_args args = {};
+	u32 vendor_id = TDX_VENDOR_INTEL;

What's the purpose of this assignment?

Quoted 10 lines
arch/x86/coco/tdx/tdx.c +10 -0
+	u16 major_version = 0;
+	u16 minor_version = 0;
+	u64 ret;
+
+	/*
+	 * TDCALL leaf TDG_SYS_RD
+	 */
+	args.rdx = TDX_SYS_VENDOR_ID_FID;
+	ret = __tdcall_ret(TDG_SYS_RD, &args);
+	/*

arch/x86/coco/tdx/tdx.c static bool tdx_enc_status_change_finish(unsigned long vaddr, int numpages, · line 838

Quoted 10 lines
arch/x86/coco/tdx/tdx.c +10 -0
+	 * The TDCALL TDG.SYS.RD originates from TDX version 1.5.
+	 * Treat TDCALL_INVALID_OPERAND error as TDX version 1.0.
+	 */
+	if (TDCALL_RETURN_CODE(ret) == TDCALL_INVALID_OPERAND)
+		goto version_1_0;
+	if (ret) {
+		WARN(1, "TDX detection: TDG.SYS.RD(VENDOR_ID) error, return %llu\n",
+		     ret);
+		return;
+	}

We do not need random warnings at every step of the way. Worst case, make a version of tdcall that will spew an error in common code.

arch/x86/coco/tdx/tdx.c static bool tdx_enc_status_change_finish(unsigned long vaddr, int numpages, · line 839

Quoted 1 line
arch/x86/coco/tdx/tdx.c +1 -0
+	vendor_id = (u32)args.r8;

What's the purpose of the cast?

Review static bool tdx_enc_status_change_finish(unsigned long vaddr, int numpages, · line 841

Quoted 2 lines
(quoted diff) +2 -0
+	args.rdx = TDX_SYS_MAJOR_FID;
+	ret = __tdcall_ret(TDG_SYS_RD, &args);

Does args need to be re-zeroed between tdcalls?

Quoted 41 lines
arch/x86/coco/tdx/tdx.c +27 -1
+	if (ret) {
+		WARN(1, "TDX detection: TDG.SYS.RD(MAJOR) error, return %llu\n",
+		     ret);
+		return;
+	}
+	major_version = (u16)args.r8;
+
+	args.rdx = TDX_SYS_MINOR_FID;
+	ret = __tdcall_ret(TDG_SYS_RD, &args);
+	if (ret) {
+		WARN(1, "TDX detection: TDG.SYS.RD(MINOR) error, return %llu\n",
+		     ret);
+		return;
+	}
+	minor_version = (u16)args.r8;
+
+	pr_info("TDX detected. TDX version:%u.%u VendorID:%x\n",
+		major_version, minor_version, vendor_id);
+
+	return;
+
+	/* TDX 1.0 does not have the TDCALL TDG.SYS.RD */
+version_1_0:
+	pr_info("TDX detected. TDG.SYS.RD not available, assuming TDX version: 1.x (x<5)\n");
+}
+
 void __init tdx_early_init(void)
 {
 	struct tdx_module_args args = {
@@ -867,5 +933,5 @@ void __init tdx_early_init(void)
 	 */
 	x86_cpuinit.parallel_bringup = false;
 
-	pr_info("Guest detected\n");
+	detect_tdx_version();
 }
arch/x86/include/asm/shared/tdx.h +0 -0
index f74695dea217..1a0cacad5a0c 100644

arch/x86/include/asm/shared/tdx.h line 20

Quoted 4 lines
arch/x86/include/asm/shared/tdx.h +1 -0
@@ -17,6 +17,7 @@
 #define TDG_MR_REPORT			4
 #define TDG_MEM_PAGE_ACCEPT		6
 #define TDG_VM_WR			8
+#define TDG_SYS_RD			11

*This* is the place to document that TDG_SYS_RD is not available on old TDX modules.

Reviewed-by on v5 1/1
#2 lore raw
In reply to Yi Sun
Quoted 14 lines · Yi Sun

It is essential for TD users to be aware of the vendor and version of the current TDX. Additionally, they can reference the TDX version when reporting bugs or issues.

Furthermore, the applications or device drivers running in TD can achieve enhanced reliability and flexibility by following the TDX Module ABI specification, because there are significant differences between different versions of TDX, as mentioned in the "Intel® TDX Module Incompatibilities between v1.0 and v1.5" reference.

Add function detect_tdx_version to fetch and dump the version of the TDX, which is called during TD initialization. Obtain the info by calling TDG.SYS.RD, including the major and minor version numbers and vendor ID.

Quoted 7 lines

The TDCALL TDG.SYS.RD originates from TDX version 1.5. If the error TDCALL_INVALID_OPERAND occurs, it should be treated as TDX version 1.0.

<rant>

Intel really outdid themselves in creating the most horrendous document to read/comprehend aka tdx's 1.5 ABI ...

</rant>