Skip to contentSkip to conversation

x86/tdx: Dump TDX version During the TD Bootup

PATCH Superseded Yi Sun 4 participants 7 messages Oct 6, 2023 – Oct 9, 2023

Subsystems (2)

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Yi Sun

author
#0 lore raw

PATCH v4 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. Here are a few examples:

MSR Name Index Reason
----------------------------------------------
IA32_UARCH_MISC_CTL 0x1B01 From v1.5
IA32_ARCH_CAPABILITIES 0x010A Changed in v1.5
IA32_TSX_CTRL 0x0122 Changed in v1.5

CPUID Leaf Sub-leaf Reason
---------------------------------------
0x7 2 From v1.5
0x22 0 From v1.5
0x23 0~3 From v1.5
0x80000007 0 From v1.5

During TD initialization, the TDX version info can be obtained by calling TDG.SYS.RD. This will fetch the current version of TDX, 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.

If the __tdcall_ret fails, expect a zero value for all tdx sys info. No additional error code is necessary to avoid introducing noise during the bootup.

arch/x86/coco/tdx/tdx.c +78 -1
index 3e6dbd2199cf..991f7dc695bd 100644
@@ -37,6 +37,24 @@
 
 #define TDREPORT_SUBTYPE_0	0
 
+/*
+ * TDX metadata base field id, used by TDCALL TDG.SYS.RD
+ * See TDX ABI Spec section 3.3.2.3 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
+
+/*
+ * The global-scope metadata field via TDG.SYS.RD TDCALL
+ */
+struct tdg_sys_info {
+	u32 vendor_id;
+	u16 major_version;
+	u16 minor_version;
+};
+
 /* Called from __tdx_hypercall() for unrecoverable failure */
 noinstr void __noreturn __tdx_hypercall_failed(void)
 {
@@ -800,6 +818,60 @@ static bool tdx_enc_status_change_finish(unsigned long vaddr, int numpages,
 	return true;
 }
 
+/*
+ * Parse the tdx module version info from the global-scope metadata fields.
+ *
+ * Refer to Intel TDX Application Binary Interface (ABI) section
+ * "TDG.SYS.RD".
+ */
+static void tdg_get_sysinfo(struct tdg_sys_info *td_sys)
+{
+	u64 ret;
+	struct tdx_module_args args = {
+		.rdx = TDX_SYS_VENDOR_ID_FID,
+	};
+
+	if (!td_sys)
+		return;
+
+	memset(td_sys, 0, sizeof(struct tdg_sys_info));
+
+	/*
+	 * TDCALL leaf TDX_SYS_RD
+	 * Input Field Identifier via RDX and get the output via R8.
+	 */
+	ret = __tdcall_ret(TDX_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 other errors occur, return with zero td_sys.
+	 */
+	if (TDCALL_RETURN_CODE(ret) == TDCALL_INVALID_OPERAND)
+		goto version_1_0;
+	else if (ret)
+		return;
+
+	td_sys->vendor_id = (u32)args.r8;
+
+	args.rdx = TDX_SYS_MAJOR_FID;
+	__tdcall_ret(TDX_SYS_RD, &args);
+
+	td_sys->major_version = (u16)args.r8;
+
+	args.rdx = TDX_SYS_MINOR_FID;
+	__tdcall_ret(TDX_SYS_RD, &args);
+
+	td_sys->minor_version = (u16)args.r8;
+
+	return;
+
+	/* TDX 1.0 does not have the TDCALL TDG.SYS.RD */
+version_1_0:
+	td_sys->vendor_id = TDX_VENDOR_INTEL;
+	td_sys->major_version = 1;
+	td_sys->minor_version = 0;
+}
+
 void __init tdx_early_init(void)
 {
 	struct tdx_module_args args = {
@@ -808,6 +880,7 @@ void __init tdx_early_init(void)
 	};
 	u64 cc_mask;
 	u32 eax, sig[3];
+	struct tdg_sys_info td_sys_info;
 
 	cpuid_count(TDX_CPUID_LEAF_ID, 0, &eax, &sig[0], &sig[2],  &sig[1]);
 
@@ -867,5 +940,9 @@ void __init tdx_early_init(void)
 	 */
 	x86_cpuinit.parallel_bringup = false;
 
-	pr_info("Guest detected\n");
+	tdg_get_sysinfo(&td_sys_info);
+
+	pr_info("Guest detected. TDX version:%u.%u VendorID: %x\n",
+		td_sys_info.major_version, td_sys_info.minor_version,
+		td_sys_info.vendor_id);
 }
arch/x86/include/asm/shared/tdx.h +1 -0
index f74695dea217..d326509832e6 100644
@@ -17,6 +17,7 @@
 #define TDG_MR_REPORT			4
 #define TDG_MEM_PAGE_ACCEPT		6
 #define TDG_VM_WR			8
+#define TDX_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 78 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. Here are a few examples:

MSR Name Index Reason
----------------------------------------------
IA32_UARCH_MISC_CTL 0x1B01 From v1.5
IA32_ARCH_CAPABILITIES 0x010A Changed in v1.5
IA32_TSX_CTRL 0x0122 Changed in v1.5

CPUID Leaf Sub-leaf Reason
---------------------------------------
0x7 2 From v1.5
0x22 0 From v1.5
0x23 0~3 From v1.5
0x80000007 0 From v1.5

During TD initialization, the TDX version info can be obtained by calling TDG.SYS.RD. This will fetch the current version of TDX, 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.

If the __tdcall_ret fails, expect a zero value for all tdx sys info. No additional error code is necessary to avoid introducing noise during the bootup.

arch/x86/coco/tdx/tdx.c +25 -0
index 3e6dbd2199cf..991f7dc695bd 100644
@@ -37,6 +37,24 @@
 
  #define TDREPORT_SUBTYPE_0	0
 
+/*
+ * TDX metadata base field id, used by TDCALL TDG.SYS.RD
+ * See TDX ABI Spec section 3.3.2.3 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
+
+/*
+ * The global-scope metadata field via TDG.SYS.RD TDCALL
+ */
+struct tdg_sys_info {
+	u32 vendor_id;
+	u16 major_version;
+	u16 minor_version;
+};
+
  /* Called from __tdx_hypercall() for unrecoverable failure */
  noinstr void __noreturn __tdx_hypercall_failed(void)
  {
@@ -800,6 +818,60 @@ static bool tdx_enc_status_change_finish(unsigned long vaddr, int numpages,
  	return true;
  }
 
+/*
+ * Parse the tdx module version info from the global-scope metadata fields.
+ *
+ * Refer to Intel TDX Application Binary Interface (ABI) section
+ * "TDG.SYS.RD".
+ */
+static void tdg_get_sysinfo(struct tdg_sys_info *td_sys)

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

Quoted 10 lines
arch/x86/coco/tdx/tdx.c +10 -0
+{
+	u64 ret;
+	struct tdx_module_args args = {
+		.rdx = TDX_SYS_VENDOR_ID_FID,
+	};
+
+	if (!td_sys)
+		return;
+
+	memset(td_sys, 0, sizeof(struct tdg_sys_info));

So in case of an error this function would effectively zero out tdg_sys_info and not explicitly mention that something went wrong. Dunno how sensible or likely it is to get an error while requesting the ID_FID ? Also why don't you check for errors on subsequent calls to TDG.SYS.RD ?

Quoted 71 lines
arch/x86/coco/tdx/tdx.c +43 -1
+
+	/*
+	 * TDCALL leaf TDX_SYS_RD
+	 * Input Field Identifier via RDX and get the output via R8.
+	 */
+	ret = __tdcall_ret(TDX_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 other errors occur, return with zero td_sys.
+	 */
+	if (TDCALL_RETURN_CODE(ret) == TDCALL_INVALID_OPERAND)
+		goto version_1_0;
+	else if (ret)
+		return;
+
+	td_sys->vendor_id = (u32)args.r8;
+
+	args.rdx = TDX_SYS_MAJOR_FID;
+	__tdcall_ret(TDX_SYS_RD, &args);
+
+	td_sys->major_version = (u16)args.r8;
+
+	args.rdx = TDX_SYS_MINOR_FID;
+	__tdcall_ret(TDX_SYS_RD, &args);
+
+	td_sys->minor_version = (u16)args.r8;
+
+	return;
+
+	/* TDX 1.0 does not have the TDCALL TDG.SYS.RD */
+version_1_0:
+	td_sys->vendor_id = TDX_VENDOR_INTEL;
+	td_sys->major_version = 1;
+	td_sys->minor_version = 0;
+}
+
  void __init tdx_early_init(void)
  {
  	struct tdx_module_args args = {
@@ -808,6 +880,7 @@ void __init tdx_early_init(void)
  	};
  	u64 cc_mask;
  	u32 eax, sig[3];
+	struct tdg_sys_info td_sys_info;
 
  	cpuid_count(TDX_CPUID_LEAF_ID, 0, &eax, &sig[0], &sig[2],  &sig[1]);
 
@@ -867,5 +940,9 @@ void __init tdx_early_init(void)
  	 */
  	x86_cpuinit.parallel_bringup = false;
 
-	pr_info("Guest detected\n");
+	tdg_get_sysinfo(&td_sys_info);
+
+	pr_info("Guest detected. TDX version:%u.%u VendorID: %x\n",
+		td_sys_info.major_version, td_sys_info.minor_version,
+		td_sys_info.vendor_id);
  }
arch/x86/include/asm/shared/tdx.h +1 -0
index f74695dea217..d326509832e6 100644
@@ -17,6 +17,7 @@
  #define TDG_MR_REPORT			4
  #define TDG_MEM_PAGE_ACCEPT		6
  #define TDG_VM_WR			8
+#define TDX_SYS_RD			11
 
  /* TDCS fields. To be used by TDG.VM.WR and TDG.VM.RD module calls */
  #define TDCS_NOTIFY_ENABLES		0x9100000000000010
author
#2 lore raw
Quoted 17 lines · Nikolay Borisov

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

Quoted 11 lines · Yi Sun
arch/x86/coco/tdx/tdx.c +11 -0
+static void tdg_get_sysinfo(struct tdg_sys_info *td_sys)
+{
+	u64 ret;
+	struct tdx_module_args args = {
+		.rdx = TDX_SYS_VENDOR_ID_FID,
+	};
+
+	if (!td_sys)
+		return;
+
+	memset(td_sys, 0, sizeof(struct tdg_sys_info));

So in case of an error this function would effectively zero out tdg_sys_info and not explicitly mention that something went wrong. Dunno how sensible or likely it is to get an error while requesting the ID_FID ? Also why don't you check for errors on subsequent calls to TDG.SYS.RD ?

I'll add some error info before each error return to know what going on when some error occurs.

Actually, I removed the error check for subsequent calls to TDG.SYS.RD in this version, since it's probably not necessary when the first call is successful. I can add them back with warning dumping when errors occurs.

Thanks
--Sun, Yi

#3 lore raw
In reply to Yi Sun
Quoted 16 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. Here are a few examples:

MSR Name Index Reason
----------------------------------------------
IA32_UARCH_MISC_CTL 0x1B01 From v1.5
IA32_ARCH_CAPABILITIES 0x010A Changed in v1.5
IA32_TSX_CTRL 0x0122 Changed in v1.5

Quoted 6 lines

CPUID Leaf Sub-leaf Reason
---------------------------------------
0x7 2 From v1.5
0x22 0 From v1.5
0x23 0~3 From v1.5
0x80000007 0 From v1.5

I am not sure whether you want to list these details. People cannot understand what do they mean immediately anyway (well at least me).

The justification w/o them seems already good enough.

Quoted 4 lines

During TD initialization, the TDX version info can be obtained by calling TDG.SYS.RD. This will fetch the current version of TDX, including the major and minor version numbers and vendor ID.

Although already mentioned in the patch title, perhaps still better to add one sentence to say what this patch does.

Quoted 5 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.

If the __tdcall_ret fails, expect a zero value for all tdx sys info.

__tdcall_ret -> TDCALL

And I don't quite understand "expect a zero value for all TDX sys info" mean.

Are they used somewhere else besides being printed during early boot?

Quoted 2 lines

No additional error code is necessary to avoid introducing noise during the bootup.

Perhaps better to print some error message?

Given this patch only prints module version, I think you can just remove this paragraph (as it is a detail can be seen in the patch).

Quoted 4 lines

It's better if you can maintain the code change history among different versions so that people can see what changes have been done:

	<your SoB>
	---
	code change history 
	---
	...

The "code change history" will be thrown away when the patch is applied.

arch/x86/coco/tdx/tdx.c line 42

Quoted 12 lines
arch/x86/coco/tdx/tdx.c +3 -0
index 3e6dbd2199cf..991f7dc695bd 100644
@@ -37,6 +37,24 @@
 
 #define TDREPORT_SUBTYPE_0	0
 
+/*
+ * TDX metadata base field id, used by TDCALL TDG.SYS.RD
+ * See TDX ABI Spec section 3.3.2.3 Global Metadata Fields

I believe I was told we don't need to mention the specific section number (well at least in the changelog, can't recall whether applies to comment, though), because they can be changed (especially for TDX).

Quoted 19 lines
arch/x86/coco/tdx/tdx.c +15 -0
+ */
+#define TDX_SYS_VENDOR_ID_FID		0x0800000200000000ULL
+#define TDX_SYS_MINOR_FID		0x0800000100000003ULL
+#define TDX_SYS_MAJOR_FID		0x0800000100000004ULL
+#define TDX_VENDOR_INTEL		0x8086
+
+/*
+ * The global-scope metadata field via TDG.SYS.RD TDCALL
+ */
+struct tdg_sys_info {
+	u32 vendor_id;
+	u16 major_version;
+	u16 minor_version;
+};
+
 /* 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 825

Quoted 8 lines
arch/x86/coco/tdx/tdx.c +5 -0
@@ -800,6 +818,60 @@ static bool tdx_enc_status_change_finish(unsigned long vaddr, int numpages,
 	return true;
 }
 
+/*
+ * Parse the tdx module version info from the global-scope metadata fields.
+ *
+ * Refer to Intel TDX Application Binary Interface (ABI) section
+ * "TDG.SYS.RD".

I think you don't need to mention TDG.SYS.RD here, as you will mention it couple of times later in the function anyway.

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

Quoted 7 lines
arch/x86/coco/tdx/tdx.c +7 -0
+ */
+static void tdg_get_sysinfo(struct tdg_sys_info *td_sys)
+{
+	u64 ret;
+	struct tdx_module_args args = {
+		.rdx = TDX_SYS_VENDOR_ID_FID,
+	};

Please keep these variables in reverse christmas tree style.

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

Quoted 3 lines
arch/x86/coco/tdx/tdx.c +3 -0
+
+	if (!td_sys)
+		return;

If you print the module info in this function rather than in tdx_early_init(), you can declare @td_sys as local variable in this function and remove this check.

You don't even need 'struct tdg_sys_info', but have 3 local variables instead:
vendor_id, major_version, minor_version.

In fact, perhaps you don't even need vendor_id, because it must be Intel, but printing it won't hurt I suppose.

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

Quoted 2 lines
arch/x86/coco/tdx/tdx.c +2 -0
+
+	memset(td_sys, 0, sizeof(struct tdg_sys_info));

This can be removed too. If needed, major_version and minor_version can be initialized when they are declared.

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

Quoted 5 lines
arch/x86/coco/tdx/tdx.c +5 -0
+
+	/*
+	 * TDCALL leaf TDX_SYS_RD
+	 * Input Field Identifier via RDX and get the output via R8.
+	 */

If you need to say "Input Field Identifier via RDX", then I guess it's better to explicitly initialize args.rdx here. It is explicitly initialized below for major_version and minor_version anyway.

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

Quoted 10 lines
arch/x86/coco/tdx/tdx.c +10 -0
+	ret = __tdcall_ret(TDX_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 other errors occur, return with zero td_sys.
+	 */
+	if (TDCALL_RETURN_CODE(ret) == TDCALL_INVALID_OPERAND)
+		goto version_1_0;
+	else if (ret)
+		return;

Looks you don't need the 'else'.

And as Nikolay mentioned, it's better to print some error message here to say unable to get module version. Maybe even WARN() because AFAICT it can only be kernel bug or TDX firmware bug.

Quoted 10 lines
arch/x86/coco/tdx/tdx.c +10 -0
+
+	td_sys->vendor_id = (u32)args.r8;
+
+	args.rdx = TDX_SYS_MAJOR_FID;
+	__tdcall_ret(TDX_SYS_RD, &args);
+
+	td_sys->major_version = (u16)args.r8;
+
+	args.rdx = TDX_SYS_MINOR_FID;
+	__tdcall_ret(TDX_SYS_RD, &args);

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

Quoted 10 lines
arch/x86/coco/tdx/tdx.c +10 -0
+
+	td_sys->minor_version = (u16)args.r8;
+
+	return;
+
+	/* TDX 1.0 does not have the TDCALL TDG.SYS.RD */
+version_1_0:
+	td_sys->vendor_id = TDX_VENDOR_INTEL;
+	td_sys->major_version = 1;
+	td_sys->minor_version = 0;

So the main reason that I think you can just print module version in this function instead of tdx_early_init() is because IIUC you are setting up some random value for TDX module 1.0. Probably it's better to just print it's module 1.0 w/o mentioning major/minor.

Quoted 14 lines
arch/x86/coco/tdx/tdx.c +3 -0
+}
+
 void __init tdx_early_init(void)
 {
 	struct tdx_module_args args = {
@@ -808,6 +880,7 @@ void __init tdx_early_init(void)
 	};
 	u64 cc_mask;
 	u32 eax, sig[3];
+	struct tdg_sys_info td_sys_info;
 
 	cpuid_count(TDX_CPUID_LEAF_ID, 0, &eax, &sig[0], &sig[2],  &sig[1]);
 

arch/x86/coco/tdx/tdx.c void __init tdx_early_init(void) · line 948

Quoted 10 lines
arch/x86/coco/tdx/tdx.c +5 -1
@@ -867,5 +940,9 @@ void __init tdx_early_init(void)
 	 */
 	x86_cpuinit.parallel_bringup = false;
 
-	pr_info("Guest detected\n");
+	tdg_get_sysinfo(&td_sys_info);
+
+	pr_info("Guest detected. TDX version:%u.%u VendorID: %x\n",
+		td_sys_info.major_version, td_sys_info.minor_version,
+		td_sys_info.vendor_id);
 }

As replied above, I think you can print module info in tdg_get_sysinfo().

If you do so, perhaps to rename the function to something like detect_tdx_version().

You can either still keep

	pr_info("Guest detected\n");

here, or remove it but embed it to the message when you print module version.

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

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

TDG_SYS_RD, please.

author
#4 lore raw
Quoted 6 lines · Kai

On Fri, 2023-10-06 at 14:28 +0800, Yi Sun wrote:

It's better if you can maintain the code change history among different versions so that people can see what changes have been done:

Quoted 8 lines
	<your SoB>
	---
	code change history
	---
	...

The "code change history" will be thrown away when the patch is applied.

I'll refine commit log and the change history.

...

Quoted 16 lines
Quoted 16 lines
arch/x86/coco/tdx/tdx.c +16 -0
+	td_sys->vendor_id = (u32)args.r8;
+
+	args.rdx = TDX_SYS_MAJOR_FID;
+	__tdcall_ret(TDX_SYS_RD, &args);
+
+	td_sys->major_version = (u16)args.r8;
+
+	args.rdx = TDX_SYS_MINOR_FID;
+	__tdcall_ret(TDX_SYS_RD, &args);
+
+	td_sys->minor_version = (u16)args.r8;
+
+	return;
+
+	/* TDX 1.0 does not have the TDCALL TDG.SYS.RD */
+version_1_0:
Quoted 8 lines

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

Quoted 3 lines
arch/x86/coco/tdx/tdx.c +3 -0
+	td_sys->vendor_id = TDX_VENDOR_INTEL;
+	td_sys->major_version = 1;
+	td_sys->minor_version = 0;

So the main reason that I think you can just print module version in this function instead of tdx_early_init() is because IIUC you are setting up some random value for TDX module 1.0. Probably it's better to just print it's module 1.0 w/o mentioning major/minor.

Actually, it is not a random value here; it is the fixed value for TDX 1.0 becasue there is not such version info in TDX 1.0.
I assume we can state major=1 and minor=0 when TDX 1.0 is detected. This will prevent confusion for the users.

Quoted 10 lines

As replied above, I think you can print module info in tdg_get_sysinfo().

If you do so, perhaps to rename the function to something like detect_tdx_version().

You can either still keep

	pr_info("Guest detected\n");

here, or remove it but embed it to the message when you print module version.

Get it. I'll do that.

Quoted 13 lines

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

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

TDG_SYS_RD, please.

Appreciate Kai's detailed comments.

Thanks
--Sun, Yi

#5 lore raw
Quoted 12 lines · Yi
Quoted 11 lines

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

Quoted 6 lines
arch/x86/coco/tdx/tdx.c +6 -0
+
+	/* TDX 1.0 does not have the TDCALL TDG.SYS.RD */
+version_1_0:
+	td_sys->vendor_id = TDX_VENDOR_INTEL;
+	td_sys->major_version = 1;
+	td_sys->minor_version = 0;

So the main reason that I think you can just print module version in this function instead of tdx_early_init() is because IIUC you are setting up some random value for TDX module 1.0.  Probably it's better to just print it's module 1.0 w/o mentioning major/minor.

Quoted 4 lines

Actually, it is not a random value here; it is the fixed value for TDX 1.0 becasue there is not such version info in TDX 1.0.
I assume we can state major=1 and minor=0 when TDX 1.0 is detected. This will prevent confusion for the users.

I mean we cannot assume minor always being 0, IIUC. I am not sure whether we should report it just as 0.

#6 lore raw
In reply to Yi Sun
Quoted 48 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. Here are a few examples:

MSR Name Index Reason
----------------------------------------------
IA32_UARCH_MISC_CTL 0x1B01 From v1.5
IA32_ARCH_CAPABILITIES 0x010A Changed in v1.5
IA32_TSX_CTRL 0x0122 Changed in v1.5

CPUID Leaf Sub-leaf Reason
---------------------------------------
0x7 2 From v1.5
0x22 0 From v1.5
0x23 0~3 From v1.5
0x80000007 0 From v1.5

During TD initialization, the TDX version info can be obtained by calling TDG.SYS.RD. This will fetch the current version of TDX, 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.

If the __tdcall_ret fails, expect a zero value for all tdx sys info. No additional error code is necessary to avoid introducing noise during the bootup.

arch/x86/coco/tdx/tdx.c +2 -0
index 3e6dbd2199cf..991f7dc695bd 100644
@@ -37,6 +37,24 @@
 
 #define TDREPORT_SUBTYPE_0	0
 
+/*
+ * TDX metadata base field id, used by TDCALL TDG.SYS.RD

arch/x86/coco/tdx/tdx.c line 51

Quoted 10 lines
arch/x86/coco/tdx/tdx.c +10 -0
+ * See TDX ABI Spec section 3.3.2.3 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
+
+/*
+ * The global-scope metadata field via TDG.SYS.RD TDCALL
+ */

I think you can just say "struct used to cache TDX Module version info"

Quoted 10 lines
arch/x86/coco/tdx/tdx.c +6 -0
+struct tdg_sys_info {
+	u32 vendor_id;
+	u16 major_version;
+	u16 minor_version;
+};
+
 /* 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 826

Quoted 9 lines
arch/x86/coco/tdx/tdx.c +6 -0
@@ -800,6 +818,60 @@ static bool tdx_enc_status_change_finish(unsigned long vaddr, int numpages,
 	return true;
 }
 
+/*
+ * Parse the tdx module version info from the global-scope metadata fields.
+ *
+ * Refer to Intel TDX Application Binary Interface (ABI) section
+ * "TDG.SYS.RD".
+ */

How about "Parse TDX Module version info from TDG.SYS.RD TDCALL"?

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

Quoted 9 lines
arch/x86/coco/tdx/tdx.c +9 -0
+static void tdg_get_sysinfo(struct tdg_sys_info *td_sys)
+{
+	u64 ret;
+	struct tdx_module_args args = {
+		.rdx = TDX_SYS_VENDOR_ID_FID,
+	};
+
+	if (!td_sys)
+		return;

This check can be removed. It will never be NULL as per current usage.

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

Quoted 7 lines
arch/x86/coco/tdx/tdx.c +7 -0
+
+	memset(td_sys, 0, sizeof(struct tdg_sys_info));
+
+	/*
+	 * TDCALL leaf TDX_SYS_RD
+	 * Input Field Identifier via RDX and get the output via R8.
+	 */

Since you are using a wrapper, I think you don't need to explain the register details here.

Quoted 5 lines
arch/x86/coco/tdx/tdx.c +5 -0
+	ret = __tdcall_ret(TDX_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 other errors occur, return with zero td_sys.

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

Quoted 10 lines
arch/x86/coco/tdx/tdx.c +10 -0
+	 */
+	if (TDCALL_RETURN_CODE(ret) == TDCALL_INVALID_OPERAND)
+		goto version_1_0;
+	else if (ret)
+		return;
+
+	td_sys->vendor_id = (u32)args.r8;
+
+	args.rdx = TDX_SYS_MAJOR_FID;
+	__tdcall_ret(TDX_SYS_RD, &args);

Why not check the return value?

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

Quoted 5 lines
arch/x86/coco/tdx/tdx.c +5 -0
+
+	td_sys->major_version = (u16)args.r8;
+
+	args.rdx = TDX_SYS_MINOR_FID;
+	__tdcall_ret(TDX_SYS_RD, &args);

Same as above.

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

Quoted 2 lines
arch/x86/coco/tdx/tdx.c +2 -0
+
+	td_sys->minor_version = (u16)args.r8;

I recommend updating td_sys after executing both TDCALLs.

Quoted 44 lines
arch/x86/coco/tdx/tdx.c +16 -1
+
+	return;
+
+	/* TDX 1.0 does not have the TDCALL TDG.SYS.RD */
+version_1_0:
+	td_sys->vendor_id = TDX_VENDOR_INTEL;
+	td_sys->major_version = 1;
+	td_sys->minor_version = 0;
+}
+
 void __init tdx_early_init(void)
 {
 	struct tdx_module_args args = {
@@ -808,6 +880,7 @@ void __init tdx_early_init(void)
 	};
 	u64 cc_mask;
 	u32 eax, sig[3];
+	struct tdg_sys_info td_sys_info;
 
 	cpuid_count(TDX_CPUID_LEAF_ID, 0, &eax, &sig[0], &sig[2],  &sig[1]);
 
@@ -867,5 +940,9 @@ void __init tdx_early_init(void)
 	 */
 	x86_cpuinit.parallel_bringup = false;
 
-	pr_info("Guest detected\n");
+	tdg_get_sysinfo(&td_sys_info);
+
+	pr_info("Guest detected. TDX version:%u.%u VendorID: %x\n",
+		td_sys_info.major_version, td_sys_info.minor_version,
+		td_sys_info.vendor_id);
 }
arch/x86/include/asm/shared/tdx.h +1 -0
index f74695dea217..d326509832e6 100644
@@ -17,6 +17,7 @@
 #define TDG_MR_REPORT			4
 #define TDG_MEM_PAGE_ACCEPT		6
 #define TDG_VM_WR			8
+#define TDX_SYS_RD			11
 
 /* TDCS fields. To be used by TDG.VM.WR and TDG.VM.RD module calls */
 #define TDCS_NOTIFY_ENABLES		0x9100000000000010
Signature · 2 lines
Sathyanarayanan Kuppuswamy
Linux Kernel Developer