TDX: Stop auto-generating the global metadata code
Subsystems (3)
v4 cover TDX: Stop auto-generating the global metadata code
This series cleans up the TDX global metadata code. It has two goals:
1. Replace the generated code with a table-driven metadata reader.
2. Make the existing code easier to read and maintain, and simplify adding new metadata fields.
Most of the feedback on v3 was about style and small nits, and eight of the ten patches now have Reviewed-by tags. Patches 7 and 10 still need review.
Dave, if there are no further comments, please consider merging this series.
This series conflicts with the TDX Module Extensions series, which adds new metadata fields. If needed, I am happy to provide a patch that resolves the conflicts.
Changes since v3:
=================
The per-class macros are gone. Dave and Nikolay found them to be an unnecessary layer, so every table entry now uses FIELD_MAP() directly.
Spelling out the full structure type in each entry would make most entries 80-100 columns wide, and two would go over 100. As Yilun suggested, FIELD_MAP() now takes only the part of the structure name after tdx_sys_info_ and builds the full name from it, which keeps the entries readable.
All entries now fit in 80 columns, with two of them wrapped onto a second line.
Other changes:
- Patch 7: Read the two cpuid_config_values[] entries with two explicit calls instead of an inner loop [Nikolay]
- Patch 9: Reword the justification for printing the module version only after a successful read [Rick]
- Patch 10: Encode the member size and compare it with the size bits in the field ID, instead of decoding the field ID with shifts and masks [Rick]
- Collect Reviewed-by tags from Rick and Nikolay
- v3: https://lore.kernel.org/kvm/[email protected]/
Problem & Solution
==================
The TDX module reports its capabilities and limits through a set of global metadata fields, each read using a 64-bit field ID via TDH.SYS.RD. The fields are grouped into classes, and the kernel mirrors each class it needs in a sub-structure of struct tdx_sys_info.
Both those structures and the code that fills them were generated by an out-of-tree script from a JSON file.
The script was not the first approach. Kai's first attempt paired each field ID with its destination C member in a table and walked the table in a loop to read every field. Two pieces of feedback on it drove everything that followed [1]:
1. Compile-time type checking. Metadata fields have different sizes (u16 and u64), so a common helper takes a void * and a size instead of a typed destination.
2. The check that a field ID's encoded size matches its destination member ran at runtime, although both sizes are known at build time.
The discussion did not converge after several rounds of review. Dave noted that, despite the void *, the size check provides the safety that matters:
it catches mismatched field and member widths [2]. That left one problem:
moving the size check from runtime to build time.
Before that was settled, the direction shifted to generating the code with a script. At the time, the TDX ABI definitions were published as JSON, so checking a field ID required consulting a machine-readable file by hand. The script parsed the JSON and generated both the structures and their readers [3]. Generation also made the size/type check unnecessary. The field IDs and destination members came from the same input, so a mismatch could only result from a bug in the script, which is less likely than a mistake in hand-written code.
Dave concluded that the JSON experiment had failed [4] for two reasons:
1. The JSON file is not stable. The CPUID config arrays here were once sized for a maximum of 32 entries, which has since increased to 128 [5].
2. The JSON file is not authoritative enough to write code from by itself.
TDX ABI definitions are now available in human-readable PDF specifications [6]. So, stop relying on the out-of-tree script and maintain the code by hand.
Yilun later proposed a single table whose entries carry the offset and size of each mapped member in struct tdx_sys_info [7]. That design does not cover the new handoff metadata because it is not cached in struct tdx_sys_info.
This series gives every class its own table: each entry pairs a field ID with the member that holds it, and a loop walks the table. This also covers handoff metadata, which is read into a local structure rather than into struct tdx_sys_info.
The common reader still takes a void *, but each mapping verifies at build time that the destination member size matches the size encoded in the field ID. A field/member width mismatch therefore fails the build.
AI usage
========
I used LLM tools to review the patches and refine the wording of the cover letter and changelogs from my drafts. I reviewed all suggestions and adopted those I agreed with. For example, AI review suggested the read_sys_metadata_table() macro, which avoids repeating the table name when passing both the table and its size.
Testing
=======
Built each patch individually and successfully launched TDs.
Chao Gao (10):
x86/virt/tdx: Add a helper to read a table of metadata fields x86/virt/tdx: Convert the version metadata reader x86/virt/tdx: Convert the features metadata reader x86/virt/tdx: Convert the tdmr metadata reader x86/virt/tdx: Convert the td_ctrl metadata reader x86/virt/tdx: Convert the handoff metadata reader x86/virt/tdx: Convert the td_conf metadata reader x86/virt/tdx: Remove tdx_global_metadata.c
x86/virt/tdx: Use early returns in get_tdx_sys_info() x86/virt/tdx: Verify structure member sizes against metadata field IDs
arch/x86/virt/vmx/tdx/tdx.c | 190 +++++++++++++++++++- arch/x86/virt/vmx/tdx/tdx.h | 52 ++++++ arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 154 ---------------- 3 files changed, 239 insertions(+), 157 deletions(-) delete mode 100644 arch/x86/virt/vmx/tdx/tdx_global_metadata.c
base-commit: 5b8212d45d99b77c84e3ad305a295e9e65d5ef20
Signature · 1 line
2.52.0
PATCH v4 1/10 x86/virt/tdx: Add a helper to read a table of metadata fields
The metadata field readers get_tdx_sys_info_<class>() in tdx_global_metadata.c were generated by an out-of-tree script. That has not worked out: the JSON file they were generated from is neither stable nor authoritative enough [1]. The goal now is to maintain the readers by hand and to establish one standard way of adding a metadata field.
Take get_tdx_sys_info_version() as an example:
if (!ret && !(ret = read_sys_metadata_field(0x0800000100000003, &val)))
sysinfo_version->minor_version = val;
if (!ret && !(ret = read_sys_metadata_field(0x0800000100000004, &val)))
sysinfo_version->major_version = val;
if (!ret && !(ret = read_sys_metadata_field(0x0800000100000005, &val)))
sysinfo_version->update_version = val;Two patterns stand out: the read-check-store sequence repeats once per field, and the error of each read is chained into the reads that follow. Neither is common in hand-written code.
Prepare to eliminate both with a loop that reads each field, stores the value into its structure member, and returns on the first error.
Add 'struct field_mapping' to describe one field as its ID plus the offset and size of the member that receives its value. Add FIELD_MAP() to build such an entry from a field ID, a structure type and a member name. Add __read_sys_metadata_table() helper to read every field in a table. Annotate that helper __maybe_unused as there is no caller right now.
Following changes will convert the existing readers to use the new helper.
AI was used under supervision to review code and workshop logs. It suggested adding read_sys_metadata_table() macro, which avoids repeating the table name when passing both the table and its size.
---
v4:
- Collect tags [Nikolay, Rick]
- Rename TDX_SYSINFO_MAP() to FIELD_MAP() to keep table entries short
- Builds the full structure name from its tdx_sys_info_ suffix [Yilun]
v3:
- Pass the field ID to TDX_SYSINFO_MAP() instead of pasting the TDX_MD_FIELD_ID_ prefix onto a suffix inside the macro [Rick]
- Say that this patch only prepares to eliminate the two patterns. [Rick]
- Define @offset in struct field_mapping as size_t [Binbin]
--- arch/x86/virt/vmx/tdx/tdx.c | 42 +++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+)
arch/x86/virt/vmx/tdx/tdx.c +42 -0
@@ -395,6 +395,48 @@ static int read_sys_metadata_field(u64 field_id, u64 *data)return 0 ; } +/* +* Map a TDX global metadata field to a structure member. +* @field_id: The TDX global metadata field ID. +* @size: The size of the structure member. +* @offset: The member's offset within its containing structure. +*/ +struct field_mapping { +u64 field_id; +size_t size; +size_t offset; +}; + +/* Read each metadata field listed in @mappings[] into @data. */ +static int __maybe_unused __read_sys_metadata_table (const struct field_mapping *mappings, +int num_mappings,void *data) +{ +int i, ret; +u64 val; + +for (i =0 ; i < num_mappings; i++) { + ret =read_sys_metadata_field (mappings[i].field_id, &val); +if (ret) +return ret; +memcpy ((char *)data + mappings[i].offset, &val, mappings[i].size); + } + +return 0 ; +} + +#define read_sys_metadata_table (_mappings, _data) \ + __read_sys_metadata_table(_mappings, ARRAY_SIZE(_mappings), _data) + +#define __FIELD_MAP (_field, _type, _member) \ +{ \ + .field_id = _field, \ + .offset = offsetof(_type, _member), \ + .size = sizeof_field(_type, _member), \ +} + +#define FIELD_MAP (_field, _type, _member) \ + __FIELD_MAP(_field, struct tdx_sys_info_##_type, _member) +#include "tdx_global_metadata.c" static __init int check_features (struct tdx_sys_info *sysinfo)
Signature · 1 line
2.52.0
Re: [PATCH v4 01/10] x86/virt/tdx: Add a helper to read a table of metadata fields
Quoted 30 lines · Chao Gao
The metadata field readers get_tdx_sys_info_<class>() in
tdx_global_metadata.cwere generated by an out-of-tree script. That has not worked out: the JSON file they were generated from is neither stable nor authoritative enough [1]. The goal now is to maintain the readers by hand and to establish one standard way of adding a metadata field.Take
get_tdx_sys_info_version()as an example:if (!ret && !(ret = read_sys_metadata_field(0x0800000100000003, &val))) sysinfo_version->minor_version = val; if (!ret && !(ret = read_sys_metadata_field(0x0800000100000004, &val))) sysinfo_version->major_version = val; if (!ret && !(ret = read_sys_metadata_field(0x0800000100000005, &val))) sysinfo_version->update_version = val;Two patterns stand out: the read-check-store sequence repeats once per field, and the error of each read is chained into the reads that follow. Neither is common in hand-written code.
Prepare to eliminate both with a loop that reads each field, stores the value into its structure member, and returns on the first error.
Add 'struct
field_mapping' to describe one field as its ID plus the offset and size of the member that receives its value. AddFIELD_MAP()to build such an entry from a field ID, a structure type and a member name. Add__read_sys_metadata_table()helper to read every field in a table. Annotate that helper__maybe_unusedas there is no caller right now.Following changes will convert the existing readers to use the new helper.
Quoted 8 lines
AI was used under supervision to review code and workshop logs. It suggested adding
read_sys_metadata_table()macro, which avoids repeating the table name when passing both the table and its size.Signed-off-by Chao Gao <[email protected]>Reviewed-by Nikolay Borisov <[email protected]>Reviewed-by Rick Edgecombe <[email protected]>
Re: [PATCH v4 01/10] x86/virt/tdx: Add a helper to read a table of metadata fields
In reply to Chao GaoQuoted 30 lines · Chao Gao
The metadata field readers get_tdx_sys_info_<class>() in
tdx_global_metadata.cwere generated by an out-of-tree script. That has not worked out: the JSON file they were generated from is neither stable nor authoritative enough [1]. The goal now is to maintain the readers by hand and to establish one standard way of adding a metadata field.Take
get_tdx_sys_info_version()as an example:if (!ret && !(ret = read_sys_metadata_field(0x0800000100000003, &val))) sysinfo_version->minor_version = val; if (!ret && !(ret = read_sys_metadata_field(0x0800000100000004, &val))) sysinfo_version->major_version = val; if (!ret && !(ret = read_sys_metadata_field(0x0800000100000005, &val))) sysinfo_version->update_version = val;Two patterns stand out: the read-check-store sequence repeats once per field, and the error of each read is chained into the reads that follow. Neither is common in hand-written code.
Prepare to eliminate both with a loop that reads each field, stores the value into its structure member, and returns on the first error.
Add 'struct
field_mapping' to describe one field as its ID plus the offset and size of the member that receives its value. AddFIELD_MAP()to build such an entry from a field ID, a structure type and a member name. Add__read_sys_metadata_table()helper to read every field in a table. Annotate that helper__maybe_unusedas there is no caller right now.Following changes will convert the existing readers to use the new helper.
Quoted 8 lines
AI was used under supervision to review code and workshop logs. It suggested adding
read_sys_metadata_table()macro, which avoids repeating the table name when passing both the table and its size.Signed-off-by Chao Gao <[email protected]>Reviewed-by Nikolay Borisov <[email protected]>Reviewed-by Rick Edgecombe <[email protected]>
PATCH v4 2/10 x86/virt/tdx: Convert the version metadata reader
In reply to Chao GaoWith the helper to read a table of metadata fields in place, the existing metadata readers can be standardized on it.
Convert the version metadata reader: add a table that pairs each field ID with the 'struct tdx_sys_info_version' member that holds its value, and read all version fields by walking that table.
Name the field IDs for readability, so the table entries don't carry raw hex literals.
AI was used under supervision to review code and workshop logs.
---
v4:
- Collect tags [Rick]
- Drop per-class macros [Nikolay, Dave]
v3:
- Drop the get_tdx_sys_info_*() wrappers to remove an unnecessary layer of abstraction. [Rick, Dave]
- Shorten the field ID defines from TDX_MD_FIELD_ID_* to TDX_FIELD_* [Rick]
--- arch/x86/virt/vmx/tdx/tdx.c | 12 +++++++++--- arch/x86/virt/vmx/tdx/tdx.h | 10 ++++++++++ arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 17 +---------------- 3 files changed, 20 insertions(+), 19 deletions(-)
arch/x86/virt/vmx/tdx/tdx.c +9 -3
@@ -408,8 +408,8 @@ struct field_mapping { };/* Read each metadata field listed in @mappings[] into @data. */ -static int __maybe_unused __read_sys_metadata_table (const struct field_mapping *mappings, -int num_mappings,void *data) +static int __read_sys_metadata_table (const struct field_mapping *mappings, +int num_mappings,void *data) {int i, ret;u64 val;
@@ -437,6 +437,12 @@ static int __maybe_unused __read_sys_metadata_table(const struct field_mapping *#define FIELD_MAP (_field, _type, _member) \ __FIELD_MAP(_field, struct tdx_sys_info_##_type, _member) +static const struct field_mapping version_mappings[] = { +FIELD_MAP (TDX_FIELD_MINOR_VERSION , version, minor_version), +FIELD_MAP (TDX_FIELD_MAJOR_VERSION , version, major_version), +FIELD_MAP (TDX_FIELD_UPDATE_VERSION , version, update_version), +}; +#include "tdx_global_metadata.c" static __init int check_features (struct tdx_sys_info *sysinfo)
@@ -1404,7 +1410,7 @@ int tdx_module_run_update(void)if (ret)return ret; - ret =get_tdx_sys_info_version (&tdx_sysinfo.version); + ret =read_sys_metadata_table (version_mappings, &tdx_sysinfo.version);/* * Only fails if there is something unexpected * and severely wrong with the module.
arch/x86/virt/vmx/tdx/tdx.h +10 -0
@@ -52,6 +52,16 @@#define TDH_PHYMEM_PAMT_REMOVE 59#define TDH_SYS_DISABLE 69 +/* +* TDX global metadata field IDs. +* +* See "global_metadata.pdf" in Intel TDX Module ABI Definitions. +*/ +/* Class "TDX Module Version" */ +#define TDX_FIELD_MINOR_VERSION 0x0800000100000003ULL +#define TDX_FIELD_MAJOR_VERSION 0x0800000100000004ULL +#define TDX_FIELD_UPDATE_VERSION 0x0800000100000005ULL +/* TDX page types */ #define PT_NDA 0x0#define PT_RSVD 0x1
arch/x86/virt/vmx/tdx/tdx_global_metadata.c +1 -16
@@ -7,21 +7,6 @@* Include this file to other C file instead. */ -static int get_tdx_sys_info_version (struct tdx_sys_info_version *sysinfo_version) -{ -int ret =0 ; -u64 val; - -if (!ret && !(ret =read_sys_metadata_field (0x0800000100000003 , &val))) - sysinfo_version->minor_version = val; -if (!ret && !(ret =read_sys_metadata_field (0x0800000100000004 , &val))) - sysinfo_version->major_version = val; -if (!ret && !(ret =read_sys_metadata_field (0x0800000100000005 , &val))) - sysinfo_version->update_version = val; - -return ret; -} -static __init int get_tdx_sys_info_features (struct tdx_sys_info_features *sysinfo_features) {int ret =0 ;
@@ -129,7 +114,7 @@ static __init int get_tdx_sys_info(struct tdx_sys_info *sysinfo) {int ret =0 ; - ret = ret ?:get_tdx_sys_info_version (&sysinfo->version); + ret = ret ?:read_sys_metadata_table (version_mappings, &sysinfo->version); pr_info("Module version: " TDX_VERSION_FMT "\n", sysinfo->version.major_version,
Signature · 1 line
2.52.0
Re: [PATCH v4 02/10] x86/virt/tdx: Convert the version metadata reader
Quoted 7 lines · Chao Gao
With the helper to read a table of metadata fields in place, the existing metadata readers can be standardized on it.
Convert the version metadata reader: add a table that pairs each field ID with the 'struct
tdx_sys_info_version' member that holds its value, and read all version fields by walking that table.
Quoted 7 lines
Name the field IDs for readability, so the table entries don't carry raw hex literals.
AI was used under supervision to review code and workshop logs.
Signed-off-by Chao Gao <[email protected]>Reviewed-by Rick Edgecombe <[email protected]>
One nit below.
[...]
arch/x86/virt/vmx/tdx/tdx.c static int __maybe_unused __read_sys_metadata_table(const struct field_mapping * · line 444
Quoted 9 lines
arch/x86/virt/vmx/tdx/tdx.c +5
-0@@ -437,6 +437,12 @@ static int __maybe_unused __read_sys_metadata_table(const struct field_mapping *#define FIELD_MAP (_field, _type, _member) \ __FIELD_MAP(_field, struct tdx_sys_info_##_type, _member) +static const struct field_mapping version_mappings[] = { +FIELD_MAP (TDX_FIELD_MINOR_VERSION , version, minor_version), +FIELD_MAP (TDX_FIELD_MAJOR_VERSION , version, major_version), +FIELD_MAP (TDX_FIELD_UPDATE_VERSION , version, update_version), +};
Nit:
I personally prefer the version provided in
https://lore.kernel.org/kvm/[email protected]
The full types keep the code greppable and __FIELD_MAP() could be dropped.
Also, it is a bit weird that FIELD_MAP() use _type as the name for the second input, but the shortened one is not really a type.
Xu Yilun
Reviewed-byRe: [PATCH v4 02/10] x86/virt/tdx: Convert the version metadata reader
Quoted 37 lines · Binbin Wu
Quoted 7 lines · Chao Gao
With the helper to read a table of metadata fields in place, the existing metadata readers can be standardized on it.
Convert the version metadata reader: add a table that pairs each field ID with the 'struct
tdx_sys_info_version' member that holds its value, and read all version fields by walking that table.Quoted 7 lines
Name the field IDs for readability, so the table entries don't carry raw hex literals.
AI was used under supervision to review code and workshop logs.
Signed-off-by Chao Gao <[email protected]>Reviewed-by Rick Edgecombe <[email protected]>Reviewed-by Binbin Wu <[email protected]>One nit below.
[...]
arch/x86/virt/vmx/tdx/tdx.c static int __maybe_unused __read_sys_metadata_table(const struct field_mapping * · line 444
Quoted 9 lines
arch/x86/virt/vmx/tdx/tdx.c +5
-0@@ -437,6 +437,12 @@ static int __maybe_unused __read_sys_metadata_table(const struct field_mapping *#define FIELD_MAP (_field, _type, _member) \ __FIELD_MAP(_field, struct tdx_sys_info_##_type, _member) +static const struct field_mapping version_mappings[] = { +FIELD_MAP (TDX_FIELD_MINOR_VERSION , version, minor_version), +FIELD_MAP (TDX_FIELD_MAJOR_VERSION , version, major_version), +FIELD_MAP (TDX_FIELD_UPDATE_VERSION , version, update_version), +};Nit:
I personally prefer the version provided in
https://lore.kernel.org/kvm/[email protected]
The full types keep the code greppable and__FIELD_MAP()could be dropped.
Quoted 2 lines
Also, it is a bit weird that
FIELD_MAP()use_typeas the name for the second input, but the shortened one is not really a type.
I think if we see the mapping array as a mirror of the table in global_metadata.pdf, then this short pattern is easier to review. In this case is it clearer we rename FIELD_MAP()'s args to use the same terms in the SPEC? like the following:
FIELD_MAP(_id, _class, _field)
BTW: I still think "without field id macro" makes the pattern even clearer:
FIELD_MAP(0x0800000100000003ULL, version, minor_version)
Direct and easier to map to global_metadata.pdf. Only leave 2 special cases - CPUID_CONFIG_LEAVES/VALUES - but they are special enough anyway.
I'm still OK with the current patch. With or without macro is trivial.
Quoted 2 lines
PATCH v4 3/10 x86/virt/tdx: Convert the features metadata reader
In reply to Chao GaoContinue converting the metadata readers to the table-driven helper.
Add a table that pairs each field ID with the 'struct tdx_sys_info_features' member that holds its value, and read all fields by walking that table.
Even though the structure has only one field, add a table anyway for symmetry with the other classes. Adding a field later then becomes a one-line change.
Annotate the table as __initconst as it is referenced only during init.
AI was used under supervision to review code and workshop logs.
---
v4:
- Collect tags [Rick]
- Drop per-class macros [Nikolay, Dave]
- Rename feature_mappings to features_mappings to match struct tdx_sys_info_features [LLM]
--- arch/x86/virt/vmx/tdx/tdx.c | 4 ++++ arch/x86/virt/vmx/tdx/tdx.h | 3 +++ arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 13 +------------ 3 files changed, 8 insertions(+), 12 deletions(-)
arch/x86/virt/vmx/tdx/tdx.c +4 -0
@@ -443,6 +443,10 @@ static const struct field_mapping version_mappings[] = { FIELD_MAP(TDX_FIELD_UPDATE_VERSION, version, update_version), }; +static const struct field_mapping features_mappings[]__initconst = { +FIELD_MAP (TDX_FIELD_TDX_FEATURES0 , features, tdx_features0), +}; +#include "tdx_global_metadata.c" static __init int check_features (struct tdx_sys_info *sysinfo)
arch/x86/virt/vmx/tdx/tdx.h +3 -0
@@ -62,6 +62,9 @@#define TDX_FIELD_MAJOR_VERSION 0x0800000100000004ULL#define TDX_FIELD_UPDATE_VERSION 0x0800000100000005ULL +/* Class "TDX Module Info" */ +#define TDX_FIELD_TDX_FEATURES0 0x0A00000300000008ULL +/* TDX page types */ #define PT_NDA 0x0#define PT_RSVD 0x1
arch/x86/virt/vmx/tdx/tdx_global_metadata.c +1 -12
@@ -7,17 +7,6 @@* Include this file to other C file instead. */ -static __init int get_tdx_sys_info_features (struct tdx_sys_info_features *sysinfo_features) -{ -int ret =0 ; -u64 val; - -if (!ret && !(ret =read_sys_metadata_field (0x0A00000300000008 , &val))) - sysinfo_features->tdx_features0 = val; - -return ret; -} -static __init int get_tdx_sys_info_tdmr_dpamt (struct tdx_sys_info_tdmr *sysinfo_tdmr) {int ret;
@@ -121,7 +110,7 @@ static __init int get_tdx_sys_info(struct tdx_sys_info *sysinfo) sysinfo->version.minor_version, sysinfo->version.update_version); - ret = ret ?:get_tdx_sys_info_features (&sysinfo->features); + ret = ret ?:read_sys_metadata_table (features_mappings, &sysinfo->features); ret = ret ?:get_tdx_sys_info_tdmr (&sysinfo->tdmr); ret = ret ?:get_tdx_sys_info_td_ctrl (&sysinfo->td_ctrl); ret = ret ?:get_tdx_sys_info_td_conf (&sysinfo->td_conf);
Signature · 1 line
2.52.0
Re: [PATCH v4 03/10] x86/virt/tdx: Convert the features metadata reader
Quoted 10 lines · Chao Gao
Continue converting the metadata readers to the table-driven helper.
Add a table that pairs each field ID with the 'struct
tdx_sys_info_features' member that holds its value, and read all fields by walking that table.Even though the structure has only one field, add a table anyway for symmetry with the other classes. Adding a field later then becomes a one-line change.
Quoted 6 lines
Annotate the table as
__initconstas it is referenced only during init.AI was used under supervision to review code and workshop logs.
Signed-off-by Chao Gao <[email protected]>Reviewed-by Rick Edgecombe <[email protected]>
Re: [PATCH v4 03/10] x86/virt/tdx: Convert the features metadata reader
In reply to Chao GaoQuoted 4 lines
arch/x86/virt/vmx/tdx/tdx.c +4
-0+static const struct field_mapping features_mappings[]__initconst = { +FIELD_MAP (TDX_FIELD_TDX_FEATURES0 , features, tdx_features0), +}; +
[...]
arch/x86/virt/vmx/tdx/tdx.h line 66
Quoted 2 lines
arch/x86/virt/vmx/tdx/tdx.h +2
-0+/* Class "TDX Module Info" */ +#define TDX_FIELD_TDX_FEATURES0 0x0A00000300000008ULL
mm.. the SPEC defines the class as "TDX Module Info" but Linux defines it "features". mm.. it's OK, we shouldn't churn the code that use the features class.
PATCH v4 4/10 x86/virt/tdx: Convert the tdmr metadata reader
In reply to Chao GaoContinue converting the metadata readers to the table-driven helper.
The "TDMR info" metadata class has two readers: one for the fields that are always present, and one for the fields that exist only when the module supports Dynamic PAMT.
Add a table for each, both pairing field IDs with the 'struct tdx_sys_info_tdmr' members that hold their values, and read all fields by walking the tables.
Annotate both tables as __initconst since they are referenced only during init.
AI was used under supervision to review the code and workshop the changelog.
---
v4:
- Collect tags [Rick]
- Drop per-class macros [Nikolay, Dave]
--- arch/x86/virt/vmx/tdx/tdx.c | 14 +++++++++ arch/x86/virt/vmx/tdx/tdx.h | 8 +++++ arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 35 ++------------------- 3 files changed, 24 insertions(+), 33 deletions(-)
arch/x86/virt/vmx/tdx/tdx.c +14 -0
@@ -447,6 +447,20 @@ static const struct field_mapping features_mappings[] __initconst = { FIELD_MAP(TDX_FIELD_TDX_FEATURES0, features, tdx_features0), }; +static const struct field_mapping tdmr_mappings[]__initconst = { +FIELD_MAP (TDX_FIELD_MAX_TDMRS , tdmr, max_tdmrs), +FIELD_MAP (TDX_FIELD_PAMT_4K_ENTRY_SIZE , tdmr, pamt_4k_entry_size), +FIELD_MAP (TDX_FIELD_PAMT_2M_ENTRY_SIZE , tdmr, pamt_2m_entry_size), +FIELD_MAP (TDX_FIELD_PAMT_1G_ENTRY_SIZE , tdmr, pamt_1g_entry_size), +FIELD_MAP (TDX_FIELD_MAX_RESERVED_PER_TDMR , + tdmr, max_reserved_per_tdmr), +}; + +static const struct field_mapping dpamt_mappings[]__initconst = { +FIELD_MAP (TDX_FIELD_PAMT_PAGE_BITMAP_ENTRY_BITS , + tdmr, pamt_page_bitmap_entry_bits), +}; +#include "tdx_global_metadata.c" static __init int check_features (struct tdx_sys_info *sysinfo)
arch/x86/virt/vmx/tdx/tdx.h +8 -0
@@ -65,6 +65,14 @@/* Class "TDX Module Info" */ #define TDX_FIELD_TDX_FEATURES0 0x0A00000300000008ULL +/* Class "TDMR Info" */ +#define TDX_FIELD_MAX_TDMRS 0x9100000100000008ULL +#define TDX_FIELD_MAX_RESERVED_PER_TDMR 0x9100000100000009ULL +#define TDX_FIELD_PAMT_4K_ENTRY_SIZE 0x9100000100000010ULL +#define TDX_FIELD_PAMT_2M_ENTRY_SIZE 0x9100000100000011ULL +#define TDX_FIELD_PAMT_1G_ENTRY_SIZE 0x9100000100000012ULL +#define TDX_FIELD_PAMT_PAGE_BITMAP_ENTRY_BITS 0x9100000000000013ULL +/* TDX page types */ #define PT_NDA 0x0#define PT_RSVD 0x1
arch/x86/virt/vmx/tdx/tdx_global_metadata.c +2 -33
@@ -7,37 +7,6 @@* Include this file to other C file instead. */ -static __init int get_tdx_sys_info_tdmr_dpamt (struct tdx_sys_info_tdmr *sysinfo_tdmr) -{ -int ret; -u64 val; - - ret =read_sys_metadata_field (0x9100000000000013 , &val); -if (!ret) - sysinfo_tdmr->pamt_page_bitmap_entry_bits = val; - -return ret; -} - -static __init int get_tdx_sys_info_tdmr (struct tdx_sys_info_tdmr *sysinfo_tdmr) -{ -int ret =0 ; -u64 val; - -if (!ret && !(ret =read_sys_metadata_field (0x9100000100000008 , &val))) - sysinfo_tdmr->max_tdmrs = val; -if (!ret && !(ret =read_sys_metadata_field (0x9100000100000009 , &val))) - sysinfo_tdmr->max_reserved_per_tdmr = val; -if (!ret && !(ret =read_sys_metadata_field (0x9100000100000010 , &val))) - sysinfo_tdmr->pamt_4k_entry_size = val; -if (!ret && !(ret =read_sys_metadata_field (0x9100000100000011 , &val))) - sysinfo_tdmr->pamt_2m_entry_size = val; -if (!ret && !(ret =read_sys_metadata_field (0x9100000100000012 , &val))) - sysinfo_tdmr->pamt_1g_entry_size = val; - -return ret; -} -static __init int get_tdx_sys_info_td_ctrl (struct tdx_sys_info_td_ctrl *sysinfo_td_ctrl) {int ret =0 ;
@@ -111,7 +80,7 @@ static __init int get_tdx_sys_info(struct tdx_sys_info *sysinfo) sysinfo->version.update_version); ret = ret ?:read_sys_metadata_table (features_mappings, &sysinfo->features); - ret = ret ?:get_tdx_sys_info_tdmr (&sysinfo->tdmr); + ret = ret ?:read_sys_metadata_table (tdmr_mappings, &sysinfo->tdmr); ret = ret ?:get_tdx_sys_info_td_ctrl (&sysinfo->td_ctrl); ret = ret ?:get_tdx_sys_info_td_conf (&sysinfo->td_conf);
@@ -122,7 +91,7 @@ static __init int get_tdx_sys_info(struct tdx_sys_info *sysinfo)* DPAMT but don't expose the metadata. */ if (!ret &&tdx_supports_dynamic_pamt (sysinfo)) - ret =get_tdx_sys_info_tdmr_dpamt (&sysinfo->tdmr); + ret =read_sys_metadata_table (dpamt_mappings, &sysinfo->tdmr);return ret; }
Signature · 1 line
2.52.0
Re: [PATCH v4 04/10] x86/virt/tdx: Convert the tdmr metadata reader
Quoted 10 lines · Chao Gao
Continue converting the metadata readers to the table-driven helper.
The "TDMR info" metadata class has two readers: one for the fields that are always present, and one for the fields that exist only when the module supports Dynamic PAMT.
Add a table for each, both pairing field IDs with the 'struct
tdx_sys_info_tdmr' members that hold their values, and read all fields by walking the tables.
Quoted 8 lines
Annotate both tables as
__initconstsince they are referenced only during init.AI was used under supervision to review the code and workshop the changelog.
Signed-off-by Chao Gao <[email protected]>Reviewed-by Rick Edgecombe <[email protected]>
[...]
arch/x86/virt/vmx/tdx/tdx_global_metadata.c static __init int get_tdx_sys_info(struct tdx_sys_info *sysinfo) · line 94
Quoted 7 lines
arch/x86/virt/vmx/tdx/tdx_global_metadata.c +1
-1@@ -122,7 +91,7 @@ static __init int get_tdx_sys_info(struct tdx_sys_info *sysinfo)* DPAMT but don't expose the metadata. */ if (!ret &&tdx_supports_dynamic_pamt (sysinfo)) - ret =get_tdx_sys_info_tdmr_dpamt (&sysinfo->tdmr); + ret =read_sys_metadata_table (dpamt_mappings, &sysinfo->tdmr);
Nit:dpamt_mappings is a bit inconsistent with others.
May be tdmr_dpampt_mappings?
Quoted 3 lines
arch/x86/virt/vmx/tdx/tdx_global_metadata.c +0
-0return ret; }
Re: [PATCH v4 04/10] x86/virt/tdx: Convert the tdmr metadata reader
In reply to Chao GaoQuoted 10 lines · Chao Gao
Continue converting the metadata readers to the table-driven helper.
The "TDMR info" metadata class has two readers: one for the fields that are always present, and one for the fields that exist only when the module supports Dynamic PAMT.
Add a table for each, both pairing field IDs with the 'struct
tdx_sys_info_tdmr' members that hold their values, and read all fields by walking the tables.
Quoted 8 lines
Annotate both tables as
__initconstsince they are referenced only during init.AI was used under supervision to review the code and workshop the changelog.
Signed-off-by Chao Gao <[email protected]>Reviewed-by Rick Edgecombe <[email protected]>
PATCH v4 5/10 x86/virt/tdx: Convert the td_ctrl metadata reader
In reply to Chao GaoContinue converting the metadata readers to the table-driven helper.
Add a table that pairs each field ID with the 'struct tdx_sys_info_td_ctrl' member that holds its value, and read all fields by walking that table.
Annotate the table as __initconst as it is referenced only during init.
AI was used under supervision to review code and workshop logs
---
v4:
- Collect tags [Rick]
- Drop per-class macros [Nikolay, Dave]
--- arch/x86/virt/vmx/tdx/tdx.c | 6 ++++++ arch/x86/virt/vmx/tdx/tdx.h | 5 +++++ arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 17 +---------------- 3 files changed, 12 insertions(+), 16 deletions(-)
arch/x86/virt/vmx/tdx/tdx.c +6 -0
@@ -461,6 +461,12 @@ static const struct field_mapping dpamt_mappings[] __initconst = { tdmr, pamt_page_bitmap_entry_bits), }; +static const struct field_mapping td_ctrl_mappings[]__initconst = { +FIELD_MAP (TDX_FIELD_TDR_BASE_SIZE , td_ctrl, tdr_base_size), +FIELD_MAP (TDX_FIELD_TDCS_BASE_SIZE , td_ctrl, tdcs_base_size), +FIELD_MAP (TDX_FIELD_TDVPS_BASE_SIZE , td_ctrl, tdvps_base_size), +}; +#include "tdx_global_metadata.c" static __init int check_features (struct tdx_sys_info *sysinfo)
arch/x86/virt/vmx/tdx/tdx.h +5 -0
@@ -73,6 +73,11 @@#define TDX_FIELD_PAMT_1G_ENTRY_SIZE 0x9100000100000012ULL#define TDX_FIELD_PAMT_PAGE_BITMAP_ENTRY_BITS 0x9100000000000013ULL +/* Class "TD Control Structures" */ +#define TDX_FIELD_TDR_BASE_SIZE 0x9800000100000000ULL +#define TDX_FIELD_TDCS_BASE_SIZE 0x9800000100000100ULL +#define TDX_FIELD_TDVPS_BASE_SIZE 0x9800000100000200ULL +/* TDX page types */ #define PT_NDA 0x0#define PT_RSVD 0x1
arch/x86/virt/vmx/tdx/tdx_global_metadata.c +1 -16
@@ -7,21 +7,6 @@* Include this file to other C file instead. */ -static __init int get_tdx_sys_info_td_ctrl (struct tdx_sys_info_td_ctrl *sysinfo_td_ctrl) -{ -int ret =0 ; -u64 val; - -if (!ret && !(ret =read_sys_metadata_field (0x9800000100000000 , &val))) - sysinfo_td_ctrl->tdr_base_size = val; -if (!ret && !(ret =read_sys_metadata_field (0x9800000100000100 , &val))) - sysinfo_td_ctrl->tdcs_base_size = val; -if (!ret && !(ret =read_sys_metadata_field (0x9800000100000200 , &val))) - sysinfo_td_ctrl->tdvps_base_size = val; - -return ret; -} -static __init int get_tdx_sys_info_td_conf (struct tdx_sys_info_td_conf *sysinfo_td_conf) {int ret =0 ;
@@ -81,7 +66,7 @@ static __init int get_tdx_sys_info(struct tdx_sys_info *sysinfo) ret = ret ?:read_sys_metadata_table (features_mappings, &sysinfo->features); ret = ret ?:read_sys_metadata_table (tdmr_mappings, &sysinfo->tdmr); - ret = ret ?:get_tdx_sys_info_td_ctrl (&sysinfo->td_ctrl); + ret = ret ?:read_sys_metadata_table (td_ctrl_mappings, &sysinfo->td_ctrl); ret = ret ?:get_tdx_sys_info_td_conf (&sysinfo->td_conf);/*
Signature · 1 line
2.52.0
Re: [PATCH v4 05/10] x86/virt/tdx: Convert the td_ctrl metadata reader
Quoted 9 lines · Chao Gao
Continue converting the metadata readers to the table-driven helper.
Add a table that pairs each field ID with the 'struct
tdx_sys_info_td_ctrl' member that holds its value, and read all fields by walking that table.Annotate the table as
__initconstas it is referenced only during init.AI was used under supervision to review code and workshop logs
^
Nit: Missing a period.
Quoted 3 lines
Signed-off-by Chao Gao <[email protected]>Reviewed-by Rick Edgecombe <[email protected]>
Re: [PATCH v4 05/10] x86/virt/tdx: Convert the td_ctrl metadata reader
In reply to Chao GaoQuoted 6 lines · Chao Gao
Continue converting the metadata readers to the table-driven helper.
Add a table that pairs each field ID with the 'struct
tdx_sys_info_td_ctrl' member that holds its value, and read all fields by walking that table.
Quoted 6 lines
Annotate the table as
__initconstas it is referenced only during init.AI was used under supervision to review code and workshop logs
Signed-off-by Chao Gao <[email protected]>Reviewed-by Rick Edgecombe <[email protected]>
PATCH v4 6/10 x86/virt/tdx: Convert the handoff metadata reader
In reply to Chao GaoContinue converting the metadata readers to the table-driven helper.
Add a table that pairs each field ID with the 'struct tdx_sys_info_handoff' member that holds its value, and read all fields by walking that table.
AI was used under supervision to review code and workshop logs
---
v4:
- Collect tags [Rick]
- Drop per-class macros [Nikolay, Dave]
--- arch/x86/virt/vmx/tdx/tdx.c | 6 +++++- arch/x86/virt/vmx/tdx/tdx.h | 3 +++ arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 13 ------------- 3 files changed, 8 insertions(+), 14 deletions(-)
arch/x86/virt/vmx/tdx/tdx.c +5 -1
@@ -467,6 +467,10 @@ static const struct field_mapping td_ctrl_mappings[] __initconst = { FIELD_MAP(TDX_FIELD_TDVPS_BASE_SIZE, td_ctrl, tdvps_base_size), }; +static const struct field_mapping handoff_mappings[] = { +FIELD_MAP (TDX_FIELD_MODULE_HV , handoff, module_hv), +}; +#include "tdx_global_metadata.c" static __init int check_features (struct tdx_sys_info *sysinfo)
@@ -1394,7 +1398,7 @@ int tdx_module_shutdown(void)int ret;int cpu; - ret =get_tdx_sys_info_handoff (&handoff); + ret =read_sys_metadata_table (handoff_mappings, &handoff);/* * Handoff information is required for proper * shutdown. Refuse to shut down without it.
arch/x86/virt/vmx/tdx/tdx.h +3 -0
@@ -78,6 +78,9 @@#define TDX_FIELD_TDCS_BASE_SIZE 0x9800000100000100ULL#define TDX_FIELD_TDVPS_BASE_SIZE 0x9800000100000200ULL +/* Class "TDX Module Handoff" */ +#define TDX_FIELD_MODULE_HV 0x8900000100000000ULL +/* TDX page types */ #define PT_NDA 0x0#define PT_RSVD 0x1
arch/x86/virt/vmx/tdx/tdx_global_metadata.c +0 -13
@@ -40,19 +40,6 @@ static __init int get_tdx_sys_info_td_conf(struct tdx_sys_info_td_conf *sysinfo_return ret; } -static int get_tdx_sys_info_handoff (struct tdx_sys_info_handoff *sysinfo_handoff) -{ -int ret; -u64 val; - - ret =read_sys_metadata_field (0x8900000100000000 , &val); -if (ret) -return ret; - - sysinfo_handoff->module_hv = val; -return 0 ; -} -static __init int get_tdx_sys_info (struct tdx_sys_info *sysinfo) {int ret =0 ;
Signature · 1 line
2.52.0
Re: [PATCH v4 06/10] x86/virt/tdx: Convert the handoff metadata reader
Quoted 10 lines · Chao Gao
Continue converting the metadata readers to the table-driven helper.
Add a table that pairs each field ID with the 'struct
tdx_sys_info_handoff' member that holds its value, and read all fields by walking that table.AI was used under supervision to review code and workshop logs
Signed-off-by Chao Gao <[email protected]>Reviewed-by Rick Edgecombe <[email protected]>
Re: [PATCH v4 06/10] x86/virt/tdx: Convert the handoff metadata reader
In reply to Chao GaoQuoted 10 lines · Chao Gao
Continue converting the metadata readers to the table-driven helper.
Add a table that pairs each field ID with the 'struct
tdx_sys_info_handoff' member that holds its value, and read all fields by walking that table.AI was used under supervision to review code and workshop logs
Signed-off-by Chao Gao <[email protected]>Reviewed-by Rick Edgecombe <[email protected]>
PATCH v4 7/10 x86/virt/tdx: Convert the td_conf metadata reader
In reply to Chao GaoContinue converting the metadata readers to the table-driven helper.
The "TD Configurability" class holds several scalar fields plus two CPUID arrays. The arrays have a fixed capacity, but the number of entries to read is variable and reported by the num_cpuid_config scalar field.
Add a table that pairs each scalar field ID with the 'struct tdx_sys_info_td_conf' member that holds its value, and read those fields by walking that table.
Annotate the table as __initconst as it is referenced only during init.
Leave the two arrays open coded. 'struct field_mapping' pairs one field ID with one structure member, so describing an array would require a field ID per field plus an entry count that is unknown until num_cpuid_config has been read. That is not worth building for the only two arrays the kernel reads.
Read both arrays in a single loop, as both have num_cpuid_config entries. Store each value directly into its array member instead of into a temporary u64 first. The members are u64 already, so the extra copies serve no purpose.
The inner dimension of cpuid_config_values[] is fixed at 2. Read the two values with two calls instead of an inner loop. This saves a level of indentation and is simpler to read.
AI was used under supervision to review code and workshop logs.
---
v4:
- Drop per-class macros [Nikolay, Dave]
- Unroll the inner loop that reads cpuid_config_values[] [Nikolay]
v3:
- Reword the comment above the CPUID loops
--- arch/x86/virt/vmx/tdx/tdx.c | 56 +++++++++++++++++++++ arch/x86/virt/vmx/tdx/tdx.h | 10 ++++ arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 33 ------------ 3 files changed, 66 insertions(+), 33 deletions(-)
arch/x86/virt/vmx/tdx/tdx.c +56 -0
@@ -471,6 +471,62 @@ static const struct field_mapping handoff_mappings[] = { FIELD_MAP(TDX_FIELD_MODULE_HV, handoff, module_hv), }; +static const struct field_mapping td_conf_mappings[]__initconst = { +FIELD_MAP (TDX_FIELD_ATTRIBUTES_FIXED0 , td_conf, attributes_fixed0), +FIELD_MAP (TDX_FIELD_ATTRIBUTES_FIXED1 , td_conf, attributes_fixed1), +FIELD_MAP (TDX_FIELD_XFAM_FIXED0 , td_conf, xfam_fixed0), +FIELD_MAP (TDX_FIELD_XFAM_FIXED1 , td_conf, xfam_fixed1), +FIELD_MAP (TDX_FIELD_NUM_CPUID_CONFIG , td_conf, num_cpuid_config), +FIELD_MAP (TDX_FIELD_MAX_VCPUS_PER_TD , td_conf, max_vcpus_per_td), +}; + +static __init int get_tdx_sys_info_td_conf (struct tdx_sys_info_td_conf *td_conf) +{ +int ret, i; + + ret =read_sys_metadata_table (td_conf_mappings, td_conf); +if (ret) +return ret; + +/* +* The number of CPUID config entries must not exceed the array +* sizes. +*/ +if (td_conf->num_cpuid_config >ARRAY_SIZE (td_conf->cpuid_config_leaves) || + td_conf->num_cpuid_config >ARRAY_SIZE (td_conf->cpuid_config_values)) +return -EINVAL ; + +/* +* cpuid_config_leaves[i] holds a CPUID leaf and sub-leaf number, +* and the two u64s of cpuid_config_values[i] mark which bits of +* that leaf's EAX/EBX/ECX/EDX the host can set to 0 or 1 for a TD. +* +* TDX_FIELD_CPUID_CONFIG_LEAVES and TDX_FIELD_CPUID_CONFIG_VALUES +* give the field ID of each array's first element, and the rest +* follow in order. +*/ +for (i =0 ; i < td_conf->num_cpuid_config; i++) { +u64 cpuid_config_values_id =TDX_FIELD_CPUID_CONFIG_VALUES + i *2 ; + + ret =read_sys_metadata_field (TDX_FIELD_CPUID_CONFIG_LEAVES + i, + &td_conf->cpuid_config_leaves[i]); +if (ret) +return ret; + + ret =read_sys_metadata_field (cpuid_config_values_id, + &td_conf->cpuid_config_values[i][0 ]); +if (ret) +return ret; + + ret =read_sys_metadata_field (cpuid_config_values_id +1 , + &td_conf->cpuid_config_values[i][1 ]); +if (ret) +return ret; + } + +return 0 ; +} +#include "tdx_global_metadata.c" static __init int check_features (struct tdx_sys_info *sysinfo)
arch/x86/virt/vmx/tdx/tdx.h +10 -0
@@ -78,6 +78,16 @@#define TDX_FIELD_TDCS_BASE_SIZE 0x9800000100000100ULL#define TDX_FIELD_TDVPS_BASE_SIZE 0x9800000100000200ULL +/* Class "TD Configurability" */ +#define TDX_FIELD_ATTRIBUTES_FIXED0 0x1900000300000000ULL +#define TDX_FIELD_ATTRIBUTES_FIXED1 0x1900000300000001ULL +#define TDX_FIELD_XFAM_FIXED0 0x1900000300000002ULL +#define TDX_FIELD_XFAM_FIXED1 0x1900000300000003ULL +#define TDX_FIELD_NUM_CPUID_CONFIG 0x9900000100000004ULL +#define TDX_FIELD_MAX_VCPUS_PER_TD 0x9900000100000008ULL +#define TDX_FIELD_CPUID_CONFIG_LEAVES 0x9900000300000400ULL +#define TDX_FIELD_CPUID_CONFIG_VALUES 0x9900000300000500ULL +/* Class "TDX Module Handoff" */ #define TDX_FIELD_MODULE_HV 0x8900000100000000ULL
arch/x86/virt/vmx/tdx/tdx_global_metadata.c +0 -33
@@ -7,39 +7,6 @@* Include this file to other C file instead. */ -static __init int get_tdx_sys_info_td_conf (struct tdx_sys_info_td_conf *sysinfo_td_conf) -{ -int ret =0 ; -u64 val; -int i, j; - -if (!ret && !(ret =read_sys_metadata_field (0x1900000300000000 , &val))) - sysinfo_td_conf->attributes_fixed0 = val; -if (!ret && !(ret =read_sys_metadata_field (0x1900000300000001 , &val))) - sysinfo_td_conf->attributes_fixed1 = val; -if (!ret && !(ret =read_sys_metadata_field (0x1900000300000002 , &val))) - sysinfo_td_conf->xfam_fixed0 = val; -if (!ret && !(ret =read_sys_metadata_field (0x1900000300000003 , &val))) - sysinfo_td_conf->xfam_fixed1 = val; -if (!ret && !(ret =read_sys_metadata_field (0x9900000100000004 , &val))) - sysinfo_td_conf->num_cpuid_config = val; -if (!ret && !(ret =read_sys_metadata_field (0x9900000100000008 , &val))) - sysinfo_td_conf->max_vcpus_per_td = val; -if (sysinfo_td_conf->num_cpuid_config >ARRAY_SIZE (sysinfo_td_conf->cpuid_config_leaves)) -return -EINVAL ; -for (i =0 ; i < sysinfo_td_conf->num_cpuid_config; i++) -if (!ret && !(ret =read_sys_metadata_field (0x9900000300000400 + i, &val))) - sysinfo_td_conf->cpuid_config_leaves[i] = val; -if (sysinfo_td_conf->num_cpuid_config >ARRAY_SIZE (sysinfo_td_conf->cpuid_config_values)) -return -EINVAL ; -for (i =0 ; i < sysinfo_td_conf->num_cpuid_config; i++) -for (j =0 ; j <2 ; j++) -if (!ret && !(ret =read_sys_metadata_field (0x9900000300000500 + i *2 + j, &val))) - sysinfo_td_conf->cpuid_config_values[i][j] = val; - -return ret; -} -static __init int get_tdx_sys_info (struct tdx_sys_info *sysinfo) {int ret =0 ;
Signature · 1 line
2.52.0
Re: [PATCH v4 07/10] x86/virt/tdx: Convert the td_conf metadata reader
Quoted 23 lines · Chao Gao
Continue converting the metadata readers to the table-driven helper.
The "TD Configurability" class holds several scalar fields plus two CPUID arrays. The arrays have a fixed capacity, but the number of entries to read is variable and reported by the
num_cpuid_configscalar field.Add a table that pairs each scalar field ID with the 'struct
tdx_sys_info_td_conf' member that holds its value, and read those fields by walking that table.Annotate the table as
__initconstas it is referenced only during init.Leave the two arrays open coded. 'struct
field_mapping' pairs one field ID with one structure member, so describing an array would require a field ID per field plus an entry count that is unknown untilnum_cpuid_confighas been read. That is not worth building for the only two arrays the kernel reads.Read both arrays in a single loop, as both have
num_cpuid_configentries. Store each value directly into its array member instead of into a temporary u64 first. The members are u64 already, so the extra copies serve no purpose.
Quoted 8 lines
The inner dimension of cpuid_config_values[] is fixed at 2. Read the two values with two calls instead of an inner loop. This saves a level of indentation and is simpler to read.
AI was used under supervision to review code and workshop logs.
Signed-off-by Chao Gao <[email protected]>---
arch/x86/virt/vmx/tdx/tdx.c static const struct field_mapping handoff_mappings[] = { · line 494
Quoted 12 lines
arch/x86/virt/vmx/tdx/tdx.c +12
-0+static __init int get_tdx_sys_info_td_conf (struct tdx_sys_info_td_conf *td_conf) +{ +int ret, i; + + ret =read_sys_metadata_table (td_conf_mappings, td_conf); +if (ret) +return ret; + +/* +* The number of CPUID config entries must not exceed the array +* sizes. +*/
Nit:
The comment seems to be redundant since the code itself tells it.
Quoted 4 lines
arch/x86/virt/vmx/tdx/tdx.c +4
-0+if (td_conf->num_cpuid_config >ARRAY_SIZE (td_conf->cpuid_config_leaves) || + td_conf->num_cpuid_config >ARRAY_SIZE (td_conf->cpuid_config_values)) +return -EINVAL ; +
[...]
Re: [PATCH v4 07/10] x86/virt/tdx: Convert the td_conf metadata reader
In reply to Chao GaoQuoted 23 lines · Chao Gao
Continue converting the metadata readers to the table-driven helper.
The "TD Configurability" class holds several scalar fields plus two CPUID arrays. The arrays have a fixed capacity, but the number of entries to read is variable and reported by the
num_cpuid_configscalar field.Add a table that pairs each scalar field ID with the 'struct
tdx_sys_info_td_conf' member that holds its value, and read those fields by walking that table.Annotate the table as
__initconstas it is referenced only during init.Leave the two arrays open coded. 'struct
field_mapping' pairs one field ID with one structure member, so describing an array would require a field ID per field plus an entry count that is unknown untilnum_cpuid_confighas been read. That is not worth building for the only two arrays the kernel reads.Read both arrays in a single loop, as both have
num_cpuid_configentries. Store each value directly into its array member instead of into a temporary u64 first. The members are u64 already, so the extra copies serve no purpose.
Quoted 7 lines
The inner dimension of cpuid_config_values[] is fixed at 2. Read the two values with two calls instead of an inner loop. This saves a level of indentation and is simpler to read.
AI was used under supervision to review code and workshop logs.
Signed-off-by Chao Gao <[email protected]>
PATCH v4 8/10 x86/virt/tdx: Remove tdx_global_metadata.c
In reply to Chao Gaotdx_global_metadata.c held the script-generated metadata readers, deliberately kept in their own file so that generated code stayed separate from hand-written code. It cannot be compiled on its own because it lacks the low-level SEAMCALL wrappers, so tdx.c #includes it directly.
Including one C file into another is unusual, and now that the readers are maintained by hand there is nothing left to isolate. get_tdx_sys_info() is the only function still in the file.
Move get_tdx_sys_info() verbatim into tdx.c and delete tdx_global_metadata.c along with its #include.
AI was used under supervision to review code and workshop logs.
--- arch/x86/virt/vmx/tdx/tdx.c | 28 +++++++++++++++- arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 36 --------------------- 2 files changed, 27 insertions(+), 37 deletions(-) delete mode 100644 arch/x86/virt/vmx/tdx/tdx_global_metadata.c
arch/x86/virt/vmx/tdx/tdx.c +27 -1
@@ -527,7 +527,33 @@ static __init int get_tdx_sys_info_td_conf(struct tdx_sys_info_td_conf *td_conf)return 0 ; } -#include "tdx_global_metadata.c" +static __init int get_tdx_sys_info (struct tdx_sys_info *sysinfo) +{ +int ret =0 ; + + ret = ret ?:read_sys_metadata_table (version_mappings, &sysinfo->version); + +pr_info ("Module version: " TDX_VERSION_FMT "\n" , + sysinfo->version.major_version, + sysinfo->version.minor_version, + sysinfo->version.update_version); + + ret = ret ?:read_sys_metadata_table (features_mappings, &sysinfo->features); + ret = ret ?:read_sys_metadata_table (tdmr_mappings, &sysinfo->tdmr); + ret = ret ?:read_sys_metadata_table (td_ctrl_mappings, &sysinfo->td_ctrl); + ret = ret ?:get_tdx_sys_info_td_conf (&sysinfo->td_conf); + +/* +* The kernel supports using TDX without DPAMT, so +* avoid reporting failure if it's not supported. Don't +* try to support buggy TDX modules that advertise +* DPAMT but don't expose the metadata. +*/ +if (!ret &&tdx_supports_dynamic_pamt (sysinfo)) + ret =read_sys_metadata_table (dpamt_mappings, &sysinfo->tdmr); + +return ret; +}static __init int check_features (struct tdx_sys_info *sysinfo) {
arch/x86/virt/vmx/tdx/tdx_global_metadata.c deleted +0 -36
@@ -1,36 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0 -/* -* Functions to read TDX global metadata. -* -* This file doesn't compile on its own as it lacks of inclusion -* of SEAMCALL wrapper primitive which reads global metadata. -* Include this file to other C file instead. -*/ - -static __init int get_tdx_sys_info (struct tdx_sys_info *sysinfo) -{ -int ret =0 ; - - ret = ret ?:read_sys_metadata_table (version_mappings, &sysinfo->version); - -pr_info ("Module version: " TDX_VERSION_FMT "\n" , - sysinfo->version.major_version, - sysinfo->version.minor_version, - sysinfo->version.update_version); - - ret = ret ?:read_sys_metadata_table (features_mappings, &sysinfo->features); - ret = ret ?:read_sys_metadata_table (tdmr_mappings, &sysinfo->tdmr); - ret = ret ?:read_sys_metadata_table (td_ctrl_mappings, &sysinfo->td_ctrl); - ret = ret ?:get_tdx_sys_info_td_conf (&sysinfo->td_conf); - -/* -* The kernel supports using TDX without DPAMT, so -* avoid reporting failure if it's not supported. Don't -* try to support buggy TDX modules that advertise -* DPAMT but don't expose the metadata. -*/ -if (!ret &&tdx_supports_dynamic_pamt (sysinfo)) - ret =read_sys_metadata_table (dpamt_mappings, &sysinfo->tdmr); - -return ret; -}
Signature · 1 line
2.52.0
Re: [PATCH v4 08/10] x86/virt/tdx: Remove tdx_global_metadata.c
Quoted 9 lines · Chao Gao
tdx_global_metadata.cheld the script-generated metadata readers, deliberately kept in their own file so that generated code stayed separate from hand-written code. It cannot be compiled on its own because it lacks the low-level SEAMCALL wrappers, sotdx.c#includes it directly.Including one C file into another is unusual, and now that the readers are maintained by hand there is nothing left to isolate.
get_tdx_sys_info()is the only function still in the file.
Quoted 7 lines
Move
get_tdx_sys_info()verbatim intotdx.cand deletetdx_global_metadata.calong with its #include.AI was used under supervision to review code and workshop logs.
Signed-off-by Chao Gao <[email protected]>Reviewed-by Rick Edgecombe <[email protected]>
Re: [PATCH v4 08/10] x86/virt/tdx: Remove tdx_global_metadata.c
In reply to Chao GaoQuoted 9 lines · Chao Gao
tdx_global_metadata.cheld the script-generated metadata readers, deliberately kept in their own file so that generated code stayed separate from hand-written code. It cannot be compiled on its own because it lacks the low-level SEAMCALL wrappers, sotdx.c#includes it directly.Including one C file into another is unusual, and now that the readers are maintained by hand there is nothing left to isolate.
get_tdx_sys_info()is the only function still in the file.
Quoted 7 lines
Move
get_tdx_sys_info()verbatim intotdx.cand deletetdx_global_metadata.calong with its #include.AI was used under supervision to review code and workshop logs.
Signed-off-by Chao Gao <[email protected]>Reviewed-by Rick Edgecombe <[email protected]>
PATCH v4 9/10 x86/virt/tdx: Use early returns in get_tdx_sys_info()
In reply to Chao Gaoget_tdx_sys_info() was generated by a script. It chains its metadata reads with:
ret = ret ?: get_tdx_sys_info_foo(...);
The function is maintained by hand now. Use conventional early returns instead.
Print the module version only after its metadata has been read successfully. The "ret ?:" chain has no early exit, so the pr_info() after the version read prints a partially read or all-zero version when that read fails. That is harmless, but once every other read returns on error, a print after a failed read would look like a bug.
AI was used under supervision to review code and workshop logs.
---
v4:
- Collect tags [Rick]
- Reword the justification for printing the version only after a successful read [Rick]
v3:
- Print the module version only after reading its metadata succeeds [Rick]
--- arch/x86/virt/vmx/tdx/tdx.c | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-)
arch/x86/virt/vmx/tdx/tdx.c +24 -8
@@ -529,19 +529,32 @@ static __init int get_tdx_sys_info_td_conf(struct tdx_sys_info_td_conf *td_conf)static __init int get_tdx_sys_info (struct tdx_sys_info *sysinfo) { -int ret =0 ; +int ret; - ret = ret ?:read_sys_metadata_table (version_mappings, &sysinfo->version); + ret =read_sys_metadata_table (version_mappings, &sysinfo->version); +if (ret) +return ret;pr_info ("Module version: " TDX_VERSION_FMT "\n" , sysinfo->version.major_version, sysinfo->version.minor_version, sysinfo->version.update_version); - ret = ret ?:read_sys_metadata_table (features_mappings, &sysinfo->features); - ret = ret ?:read_sys_metadata_table (tdmr_mappings, &sysinfo->tdmr); - ret = ret ?:read_sys_metadata_table (td_ctrl_mappings, &sysinfo->td_ctrl); - ret = ret ?:get_tdx_sys_info_td_conf (&sysinfo->td_conf); + ret =read_sys_metadata_table (features_mappings, &sysinfo->features); +if (ret) +return ret; + + ret =read_sys_metadata_table (tdmr_mappings, &sysinfo->tdmr); +if (ret) +return ret; + + ret =read_sys_metadata_table (td_ctrl_mappings, &sysinfo->td_ctrl); +if (ret) +return ret; + + ret =get_tdx_sys_info_td_conf (&sysinfo->td_conf); +if (ret) +return ret;/* * The kernel supports using TDX without DPAMT, so
@@ -549,10 +562,13 @@ static __init int get_tdx_sys_info(struct tdx_sys_info *sysinfo)* try to support buggy TDX modules that advertise * DPAMT but don't expose the metadata. */ -if (!ret &&tdx_supports_dynamic_pamt (sysinfo)) +if (tdx_supports_dynamic_pamt (sysinfo)) { ret =read_sys_metadata_table (dpamt_mappings, &sysinfo->tdmr); +if (ret) +return ret; + } -return ret; +return 0 ; }static __init int check_features (struct tdx_sys_info *sysinfo)
Signature · 1 line
2.52.0
Re: [PATCH v4 09/10] x86/virt/tdx: Use early returns in get_tdx_sys_info()
Quoted 14 lines · Chao Gao
get_tdx_sys_info()was generated by a script. It chains its metadata reads with:ret = ret ?: get_tdx_sys_info_foo(...);The function is maintained by hand now. Use conventional early returns instead.
Print the module version only after its metadata has been read successfully. The "ret ?:" chain has no early exit, so the
pr_info()after the version read prints a partially read or all-zero version when that read fails. That is harmless, but once every other read returns on error, a print after a failed read would look like a bug.
Quoted 4 lines
AI was used under supervision to review code and workshop logs.
Signed-off-by Chao Gao <[email protected]>Reviewed-by Rick Edgecombe <[email protected]>
Re: [PATCH v4 09/10] x86/virt/tdx: Use early returns in get_tdx_sys_info()
In reply to Chao GaoQuoted 5 lines · Chao Gao
get_tdx_sys_info()was generated by a script. It chains its metadata reads with:ret = ret ?: get_tdx_sys_info_foo(...);
Quoted 7 lines
The function is maintained by hand now. Use conventional early returns instead.
Print the module version only after its metadata has been read successfully. The "ret ?:" chain has no early exit, so the
pr_info()after the version read prints a partially read or all-zero version when that read fails. That is harmless, but once every other read returns on error, a
I would not say this is harmless, my version FYR:
That is wrong but not fatal. But once every ... a print after a failed read is an obvious bug.
Quoted 6 lines
print after a failed read would look like a bug.
AI was used under supervision to review code and workshop logs.
Signed-off-by Chao Gao <[email protected]>Reviewed-by Rick Edgecombe <[email protected]>
Otherwise
PATCH v4 10/10 x86/virt/tdx: Verify structure member sizes against metadata field IDs
In reply to Chao GaoThe TDX module reports its capabilities and limits as a set of metadata fields, each identified by a field ID, and TDH.SYS.RD reads one field by its ID. TDH.SYS.RD returns the value as a u64, but metadata fields are not all 64 bits wide. A field ID therefore encodes the field's size into bits 33:32.
The kernel mirrors these fields in C structures, and each member's size must match the size encoded in the field ID. FIELD_MAP() stores the member size and uses it to decide how many bytes to copy from the value returned by TDH.SYS.RD. The size in the field ID is never consulted, so any mismatch goes unnoticed. Declaring a u32 member for a 64-bit field, for example, would silently store only its low 4 bytes.
Add a build-time check that the size encoded in each field ID matches the size of the corresponding C structure member. A wrongly typed member then fails the build with no runtime cost.
BUILD_BUG_ON() cannot be used in a structure initializer, so use BUILD_BUG_ON_ZERO() and add its zero result to the .size initializer. This performs the build-time check without changing the stored size.
An alternative would be to leave the size bits out of the field ID definitions and construct the IDs from the member sizes, which makes a mismatch impossible. But the TDX module ABI definitions list the full field IDs, and definitions with the size bits stripped would match nothing in the docs, making them harder to verify. Keep the IDs exactly as documented and check the size they encode against the C type instead.
AI was used under supervision to review code and workshop logs. It suggested moving the size check into its own macro to keep the .size line short. Rick also used AI to write TDX_FIELD_SIZE_CODE(), which converts a member size to the size encoding in the field ID.
---
v4:
- Encode the member size and compare it with the field ID's size bits, instead of decoding the field ID with shifts and masks. [Rick]
- Reword the comment above TDX_FIELD_SIZE_MASK().
v3:
- Add background on the size bits encoded in a field ID. [Rick]
- Add rationale for checking the size in the field ID instead of building the ID from the member size. [Rick]
- Squash TDX_MD_FIELD_ELE_SIZE_CODE() and TDX_MD_FIELD_ELE_SIZE() into a single TDX_FIELD_SIZE(). [Rick]
--- arch/x86/virt/vmx/tdx/tdx.c | 12 +++++++++++- arch/x86/virt/vmx/tdx/tdx.h | 13 +++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-)
arch/x86/virt/vmx/tdx/tdx.c +11 -1
@@ -427,11 +427,21 @@ static int __read_sys_metadata_table(const struct field_mapping *mappings,#define read_sys_metadata_table (_mappings, _data) \ __read_sys_metadata_table(_mappings, ARRAY_SIZE(_mappings), _data) +/* +* The size encoded in the field ID and the size of the destination C +* member must agree. +*/ +#define TDX_FIELD_SIZE_CHECK (_field, _type, _member) \ + BUILD_BUG_ON_ZERO(((_field) & TDX_FIELD_SIZE_MASK) != \ + TDX_FIELD_SIZE_CODE(sizeof_field(_type, _member))) +#define __FIELD_MAP (_field, _type, _member) \ { \ .field_id = _field, \ .offset = offsetof(_type, _member), \ - .size = sizeof_field(_type, _member), \ + .size = sizeof_field(_type, _member) + \ + TDX_FIELD_SIZE_CHECK( \ + _field, _type, _member), \ }#define FIELD_MAP (_field, _type, _member) \
arch/x86/virt/vmx/tdx/tdx.h +13 -0
@@ -91,6 +91,19 @@/* Class "TDX Module Handoff" */ #define TDX_FIELD_MODULE_HV 0x8900000100000000ULL +/* +* Bits 33:32 of a field ID hold the field size code. See +* "Metadata Field Identifier" in the Intel TDX Module ABI Specification. +*/ +#define TDX_FIELD_SIZE_MASK GENMASK_ULL(33, 32) + +#define TDX_FIELD_SIZE_CODE (_size) \ + ((_size) == 1 ? 0 : \ + (_size) == 2 ? BIT_ULL(32) : \ + (_size) == 4 ? BIT_ULL(33) : \ + (_size) == 8 ? TDX_FIELD_SIZE_MASK : \ + ~0ULL) +/* TDX page types */ #define PT_NDA 0x0#define PT_RSVD 0x1
Signature · 1 line
2.52.0
Re: [PATCH v4 10/10] x86/virt/tdx: Verify structure member sizes against metadata field IDs
Quoted 28 lines · Chao Gao
The TDX module reports its capabilities and limits as a set of metadata fields, each identified by a field ID, and TDH.SYS.RD reads one field by its ID. TDH.SYS.RD returns the value as a u64, but metadata fields are not all 64 bits wide. A field ID therefore encodes the field's size into bits 33:32.
The kernel mirrors these fields in C structures, and each member's size must match the size encoded in the field ID.
FIELD_MAP()stores the member size and uses it to decide how many bytes to copy from the value returned by TDH.SYS.RD. The size in the field ID is never consulted, so any mismatch goes unnoticed. Declaring a u32 member for a 64-bit field, for example, would silently store only its low 4 bytes.Add a build-time check that the size encoded in each field ID matches the size of the corresponding C structure member. A wrongly typed member then fails the build with no runtime cost.
BUILD_BUG_ON()cannot be used in a structure initializer, so useBUILD_BUG_ON_ZERO()and add its zero result to the .size initializer. This performs the build-time check without changing the stored size.An alternative would be to leave the size bits out of the field ID definitions and construct the IDs from the member sizes, which makes a mismatch impossible. But the TDX module ABI definitions list the full field IDs, and definitions with the size bits stripped would match nothing in the docs, making them harder to verify. Keep the IDs exactly as documented and check the size they encode against the C type instead.
Quoted 6 lines
AI was used under supervision to review code and workshop logs. It suggested moving the size check into its own macro to keep the .size line short. Rick also used AI to write
TDX_FIELD_SIZE_CODE(), which converts a member size to the size encoding in the field ID.Signed-off-by Chao Gao <[email protected]>
One nit below.
[...]
Quoted 9 lines
arch/x86/virt/vmx/tdx/tdx.c +7
-0+/* +* The size encoded in the field ID and the size of the destination C +* member must agree. +*/ +#define TDX_FIELD_SIZE_CHECK (_field, _type, _member) \ + BUILD_BUG_ON_ZERO(((_field) & TDX_FIELD_SIZE_MASK) != \ + TDX_FIELD_SIZE_CODE(sizeof_field(_type, _member)))
arch/x86/virt/vmx/tdx/tdx.c static int __read_sys_metadata_table(const struct field_mapping *mappings, · line 445
Quoted 10 lines
arch/x86/virt/vmx/tdx/tdx.c +4
-1+#define __FIELD_MAP (_field, _type, _member) \ { \ .field_id = _field, \ .offset = offsetof(_type, _member), \ - .size = sizeof_field(_type, _member), \ + .size = sizeof_field(_type, _member) + \ + TDX_FIELD_SIZE_CHECK( \ + _field, _type, _member), \ }
Nit:
This doesn't cover the TDX_FIELD_CPUID_CONFIG_LEAVES and TDX_FIELD_CPUID_CONFIG_VALUES.
Not sure it's worth to add static_assert() in get_tdx_sys_info_td_conf(). But if not, maybe it can be mentioned it in the changelog.
Re: [PATCH v4 10/10] x86/virt/tdx: Verify structure member sizes against metadata field IDs
Quoted 29 lines · Binbin Wu
Quoted 28 lines · Chao Gao
The TDX module reports its capabilities and limits as a set of metadata fields, each identified by a field ID, and TDH.SYS.RD reads one field by its ID. TDH.SYS.RD returns the value as a u64, but metadata fields are not all 64 bits wide. A field ID therefore encodes the field's size into bits 33:32.
The kernel mirrors these fields in C structures, and each member's size must match the size encoded in the field ID.
FIELD_MAP()stores the member size and uses it to decide how many bytes to copy from the value returned by TDH.SYS.RD. The size in the field ID is never consulted, so any mismatch goes unnoticed. Declaring a u32 member for a 64-bit field, for example, would silently store only its low 4 bytes.Add a build-time check that the size encoded in each field ID matches the size of the corresponding C structure member. A wrongly typed member then fails the build with no runtime cost.
BUILD_BUG_ON()cannot be used in a structure initializer, so useBUILD_BUG_ON_ZERO()and add its zero result to the .size initializer. This performs the build-time check without changing the stored size.An alternative would be to leave the size bits out of the field ID definitions and construct the IDs from the member sizes, which makes a mismatch impossible. But the TDX module ABI definitions list the full field IDs, and definitions with the size bits stripped would match nothing in the docs, making them harder to verify. Keep the IDs exactly as documented and check the size they encode against the C type instead.
Quoted 8 lines
Quoted 6 lines
AI was used under supervision to review code and workshop logs. It suggested moving the size check into its own macro to keep the .size line short. Rick also used AI to write
TDX_FIELD_SIZE_CODE(), which converts a member size to the size encoding in the field ID.Signed-off-by Chao Gao <[email protected]>Reviewed-by Binbin Wu <[email protected]>
Thanks!
Quoted 21 lines
Quoted 8 lines
arch/x86/virt/vmx/tdx/tdx.c +7
-0+/* +* The size encoded in the field ID and the size of the destination C +* member must agree. +*/ +#define TDX_FIELD_SIZE_CHECK (_field, _type, _member) \ + BUILD_BUG_ON_ZERO(((_field) & TDX_FIELD_SIZE_MASK) != \ + TDX_FIELD_SIZE_CODE(sizeof_field(_type, _member)))arch/x86/virt/vmx/tdx/tdx.c static int __read_sys_metadata_table(const struct field_mapping *mappings, · line 445
Quoted 10 lines
arch/x86/virt/vmx/tdx/tdx.c +4
-1+#define __FIELD_MAP (_field, _type, _member) \ { \ .field_id = _field, \ .offset = offsetof(_type, _member), \ - .size = sizeof_field(_type, _member), \ + .size = sizeof_field(_type, _member) + \ + TDX_FIELD_SIZE_CHECK( \ + _field, _type, _member), \ }Nit:
Quoted 2 lines
This doesn't cover the
TDX_FIELD_CPUID_CONFIG_LEAVESandTDX_FIELD_CPUID_CONFIG_VALUES.
Yes.
Quoted 3 lines
Not sure it's worth to add
static_assert()inget_tdx_sys_info_td_conf(). But if not, maybe it can be mentioned it in the changelog.
I'll add checks in get_tdx_sys_info_td_conf():
(void)TDX_FIELD_SIZE_CHECK(TDX_FIELD_CPUID_CONFIG_LEAVES, struct tdx_sys_info_td_conf, cpuid_config_leaves[0]); (void)TDX_FIELD_SIZE_CHECK(TDX_FIELD_CPUID_CONFIG_VALUES, struct tdx_sys_info_td_conf, cpuid_config_values[0][0]);
TDX_FIELD_SIZE_CHECK() uses BUILD_BUG_ON_ZERO() so it can be used in structure initializers. The (void) casts avoid unused-value warnings when used as standalone statements.
Re: [PATCH v4 10/10] x86/virt/tdx: Verify structure member sizes against metadata field IDs
In reply to Chao GaoQuoted 28 lines · Chao Gao
The TDX module reports its capabilities and limits as a set of metadata fields, each identified by a field ID, and TDH.SYS.RD reads one field by its ID. TDH.SYS.RD returns the value as a u64, but metadata fields are not all 64 bits wide. A field ID therefore encodes the field's size into bits 33:32.
The kernel mirrors these fields in C structures, and each member's size must match the size encoded in the field ID.
FIELD_MAP()stores the member size and uses it to decide how many bytes to copy from the value returned by TDH.SYS.RD. The size in the field ID is never consulted, so any mismatch goes unnoticed. Declaring a u32 member for a 64-bit field, for example, would silently store only its low 4 bytes.Add a build-time check that the size encoded in each field ID matches the size of the corresponding C structure member. A wrongly typed member then fails the build with no runtime cost.
BUILD_BUG_ON()cannot be used in a structure initializer, so useBUILD_BUG_ON_ZERO()and add its zero result to the .size initializer. This performs the build-time check without changing the stored size.An alternative would be to leave the size bits out of the field ID definitions and construct the IDs from the member sizes, which makes a mismatch impossible. But the TDX module ABI definitions list the full field IDs, and definitions with the size bits stripped would match nothing in the docs, making them harder to verify. Keep the IDs exactly as documented and check the size they encode against the C type instead.
Quoted 6 lines
AI was used under supervision to review code and workshop logs. It suggested moving the size check into its own macro to keep the .size line short. Rick also used AI to write
TDX_FIELD_SIZE_CODE(), which converts a member size to the size encoding in the field ID.Signed-off-by Chao Gao <[email protected]>