TDX host: metadata reading tweaks and feature mandate
Subsystems (2)
v9 cover TDX host: metadata reading tweaks and feature mandate
This series replaces the existing TDX module metadata reading code with a new auto-generated global metadata infrastructure for basic KVM TDX support:
1) read "TDX_FEATURES0" field to mandate the "not clobbering RBP when exiting from TDX guest" feature is supported during the module initialization.
Some old modules can clobber host's RBP when exiting from the TDX guest, and currently they can be initialized successfully. We don't want to run TDX guests using such modules. We should just fail to initialize them to avoid memory/CPU cycle cost of initializing TDX module [1].
2) provide metadata reading infrastructure which is required to read more metadata fields to support KVM TDX. The patches which do so can be found at [2][3].
Hi Dave,
This series targets x86 tip. This is the bare minimum pre-work to support KVM TDX. I appreciate if you could review and take this as we think we have addressed all the comments.
This series has dropped all changes that are not required for TDX KVM base support. It includes only:
- Host metadata infrastructure, which will be used by the KVM patches to get TDX module information to boot a TD.
- The NO_RBP_MOD enforcement, which is required to avoid clobbering RBP when running TDX guests using old TDX modules.
The script used to auto-generate the metadata reading code in patch 3 can be found in [4].
History:
v8 -> v9:
- Take out the "running out of reserved areas" bugfix:
- Remove CMR reading code in patch 3.
- Remove patch 7-8.
- Take out the patch 5 which adds nice-to-have header file inclusions but it is not mandatory for KVM TDX.
- Minor changelog improvement suggested by Rick and Reinette.
- Rebase to tip/x86/tdx.
v8: https://lore.kernel.org/lkml/[email protected]/
v7 -> v8:
- Address Dave's comments to remove the code to print module version and CMRs:
- Remove the code which reads module version in the auto-generated code.
- Remove the patch which prints module version (patch 10 in v7)
- Remove the code which prints CMRs in patch 7.
- Update the changelog of some patches that mentioned "reading module version" and "print CMRs".
- Collect Nikolay's tag.
Previous versions and more background info please see:
[1]: https://lore.kernel.org/[email protected]/
[2]: https://git.kernel.org/pub/scm/virt/kvm/kvm.git/commit/?h=kvm-coco-queue&id=594fc3256733fe2c5b8bd9007effea2083431861
[3]: https://git.kernel.org/pub/scm/virt/kvm/kvm.git/commit/?h=kvm-coco-queue&id=8bb81c97ec53b8a3dea29572cdb6ecfa4ff3a181
[4]: https://lore.kernel.org/[email protected]/
Kai Huang (5):
x86/virt/tdx: Rename 'struct tdx_tdmr_sysinfo' to reflect the spec
better
x86/virt/tdx: Start to track all global metadata in one structure x86/virt/tdx: Use dedicated struct members for PAMT entry sizes x86/virt/tdx: Switch to use auto-generated global metadata reading
code
x86/virt/tdx: Require the module to assert it has the NO_RBP_MOD
mitigation
Paolo Bonzini (1):
x86/virt/tdx: Use auto-generated code to read global metadata
arch/x86/virt/vmx/tdx/tdx.c | 95 +++++++-------------- arch/x86/virt/vmx/tdx/tdx.h | 40 +-------- arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 48 +++++++++++ arch/x86/virt/vmx/tdx/tdx_global_metadata.h | 25 ++++++ 4 files changed, 108 insertions(+), 100 deletions(-) create mode 100644 arch/x86/virt/vmx/tdx/tdx_global_metadata.c create mode 100644 arch/x86/virt/vmx/tdx/tdx_global_metadata.h
base-commit: 564ea84c8c14b007d7838bfb1327295b873573be
Signature · 1 line
2.47.1
PATCH v9 1/6 x86/virt/tdx: Rename 'struct tdx_tdmr_sysinfo' to reflect the spec better
The TDX module provides a set of "Global Metadata Fields". They report things like TDX module version, supported features, and fields related to create/run TDX guests and so on.
TDX organizes those metadata fields by "Classes" based on the meaning of those fields. E.g., for now the kernel only reads "TD Memory Region" (TDMR) related fields for module initialization. Those fields are defined under class "TDMR Info".
Today the kernel reads some of the global metadata to initialize the TDX module. KVM will need to read additional metadata fields to run TDX guests. Move towards having the TDX host core-kernel provide a centralized, canonical, and immutable structure for the global metadata that comes out from the TDX module for all kernel components to use.
More specifically, prepare the code to end up with an organization like:
struct tdx_sys_info {
struct tdx_sys_info_classA a;
struct tdx_sys_info_classB b;
...
};Currently the kernel organizes all fields under "TDMR Info" class in 'struct tdx_tdmr_sysinfo'. Prepare for the above by renaming the structure to 'struct tdx_sys_info_tdmr' to follow the class name better.
No functional change intended.
---
v8 -> v9:
- Minor changelog improvement suggested by Rick and Reniette.
--- arch/x86/virt/vmx/tdx/tdx.c | 36 ++++++++++++++++++------------------ arch/x86/virt/vmx/tdx/tdx.h | 2 +- 2 files changed, 19 insertions(+), 19 deletions(-)
arch/x86/virt/vmx/tdx/tdx.c +18 -18
@@ -272,7 +272,7 @@ static int read_sys_metadata_field(u64 field_id, u64 *data)static int read_sys_metadata_field16 (u64 field_id,int offset, -struct tdx_tdmr_sysinfo *ts) +struct tdx_sys_info_tdmr *ts) {u16 *ts_member = ((void *)ts) + offset;u64 tmp;
@@ -298,9 +298,9 @@ struct field_mapping {#define TD_SYSINFO_MAP (_field_id, _offset) \ { .field_id = MD_FIELD_ID_##_field_id, \ - .offset = offsetof(struct tdx_tdmr_sysinfo, _offset) } + .offset = offsetof(struct tdx_sys_info_tdmr, _offset) } -/* Map TD_SYSINFO fields into 'struct tdx_tdmr_sysinfo': */ +/* Map TD_SYSINFO fields into 'struct tdx_sys_info_tdmr': */ static const struct field_mapping fields[] = {TD_SYSINFO_MAP (MAX_TDMRS , max_tdmrs),TD_SYSINFO_MAP (MAX_RESERVED_PER_TDMR , max_reserved_per_tdmr),
@@ -309,16 +309,16 @@ static const struct field_mapping fields[] = { TD_SYSINFO_MAP(PAMT_1G_ENTRY_SIZE, pamt_entry_size[TDX_PS_1G]), }; -static int get_tdx_tdmr_sysinfo (struct tdx_tdmr_sysinfo *tdmr_sysinfo) +static int get_tdx_sys_info_tdmr (struct tdx_sys_info_tdmr *sysinfo_tdmr) {int ret;int i; -/* Populate 'tdmr_sysinfo' fields using the mapping structure above: */ +/* Populate 'sysinfo_tdmr' fields using the mapping structure above: */ for (i =0 ; i <ARRAY_SIZE (fields); i++) { ret =read_sys_metadata_field16 (fields[i].field_id, fields[i].offset, - tdmr_sysinfo); + sysinfo_tdmr);if (ret)return ret; }
@@ -342,13 +342,13 @@ static int tdmr_size_single(u16 max_reserved_per_tdmr) }static int alloc_tdmr_list (struct tdmr_info_list *tdmr_list, -struct tdx_tdmr_sysinfo *tdmr_sysinfo) +struct tdx_sys_info_tdmr *sysinfo_tdmr) {size_t tdmr_sz, tdmr_array_sz;void *tdmr_array; - tdmr_sz =tdmr_size_single (tdmr_sysinfo->max_reserved_per_tdmr); - tdmr_array_sz = tdmr_sz * tdmr_sysinfo->max_tdmrs; + tdmr_sz =tdmr_size_single (sysinfo_tdmr->max_reserved_per_tdmr); + tdmr_array_sz = tdmr_sz * sysinfo_tdmr->max_tdmrs;/* * To keep things simple, allocate all TDMRs together.
@@ -367,7 +367,7 @@ static int alloc_tdmr_list(struct tdmr_info_list *tdmr_list,* at a given index in the TDMR list. */ tdmr_list->tdmr_sz = tdmr_sz; - tdmr_list->max_tdmrs = tdmr_sysinfo->max_tdmrs; + tdmr_list->max_tdmrs = sysinfo_tdmr->max_tdmrs; tdmr_list->nr_consumed_tdmrs =0 ;return 0 ;
@@ -921,11 +921,11 @@ static int tdmrs_populate_rsvd_areas_all(struct tdmr_info_list *tdmr_list,/* * Construct a list of TDMRs on the preallocated space in @tdmr_list * to cover all TDX memory regions in @tmb_list based on the TDX module -* TDMR global information in @tdmr_sysinfo. +* TDMR global information in @sysinfo_tdmr. */ static int construct_tdmrs (struct list_head *tmb_list,struct tdmr_info_list *tdmr_list, -struct tdx_tdmr_sysinfo *tdmr_sysinfo) +struct tdx_sys_info_tdmr *sysinfo_tdmr) {int ret;
@@ -934,12 +934,12 @@ static int construct_tdmrs(struct list_head *tmb_list,return ret; ret =tdmrs_set_up_pamt_all (tdmr_list, tmb_list, - tdmr_sysinfo->pamt_entry_size); + sysinfo_tdmr->pamt_entry_size);if (ret)return ret; ret =tdmrs_populate_rsvd_areas_all (tdmr_list, tmb_list, - tdmr_sysinfo->max_reserved_per_tdmr); + sysinfo_tdmr->max_reserved_per_tdmr);if (ret)tdmrs_free_pamt_all (tdmr_list);
@@ -1098,7 +1098,7 @@ static int init_tdmrs(struct tdmr_info_list *tdmr_list)static int init_tdx_module (void ) { -struct tdx_tdmr_sysinfo tdmr_sysinfo; +struct tdx_sys_info_tdmr sysinfo_tdmr;int ret;/*
@@ -1117,17 +1117,17 @@ static int init_tdx_module(void)if (ret) goto out_put_tdxmem; - ret =get_tdx_tdmr_sysinfo (&tdmr_sysinfo); + ret =get_tdx_sys_info_tdmr (&sysinfo_tdmr);if (ret) goto err_free_tdxmem;/* Allocate enough space for constructing TDMRs */ - ret =alloc_tdmr_list (&tdx_tdmr_list, &tdmr_sysinfo); + ret =alloc_tdmr_list (&tdx_tdmr_list, &sysinfo_tdmr);if (ret) goto err_free_tdxmem;/* Cover all TDX-usable memory regions in TDMRs */ - ret =construct_tdmrs (&tdx_memlist, &tdx_tdmr_list, &tdmr_sysinfo); + ret =construct_tdmrs (&tdx_memlist, &tdx_tdmr_list, &sysinfo_tdmr);if (ret) goto err_free_tdmrs;
arch/x86/virt/vmx/tdx/tdx.h +1 -1
@@ -100,7 +100,7 @@ struct tdx_memblock { };/* "TDMR info" part of "Global Scope Metadata" for constructing TDMRs */ -struct tdx_tdmr_sysinfo { +struct tdx_sys_info_tdmr {u16 max_tdmrs;u16 max_reserved_per_tdmr;u16 pamt_entry_size[TDX_PS_NR ];
Signature · 1 line
2.47.1
PATCH v9 2/6 x86/virt/tdx: Start to track all global metadata in one structure
In reply to Kai HuangThe TDX module provides a set of "Global Metadata Fields". They report things like TDX module version, supported features, and fields related to create/run TDX guests and so on.
Today the kernel only reads "TD Memory Region" (TDMR) related fields for module initialization. KVM will need to read additional metadata fields to run TDX guests. Move towards having the TDX host core-kernel provide a centralized, canonical, and immutable structure for the global metadata that comes out from the TDX module for all kernel components to use.
As the first step, introduce a new 'struct tdx_sys_info' to track all global metadata fields.
TDX categorizes global metadata fields into different "Classes". E.g., the TDMR related fields are under class "TDMR Info". Instead of making 'struct tdx_sys_info' a plain structure to contain all metadata fields, organize them in smaller structures based on the "Class".
This allows those metadata fields to be used in finer granularity thus makes the code clearer. E.g., construct_tdmrs() can just take the structure which contains "TDMR Info" metadata fields.
Add get_tdx_sys_info() as the placeholder to read all metadata fields. Have it only call get_tdx_sys_info_tdmr() to read TDMR related fields for now.
Place get_tdx_sys_info() as the first step of init_tdx_module() to enable early prerequisite checks on the metadata to support early module initialization abort. This results in moving get_tdx_sys_info_tdmr() to be before build_tdx_memlist(), but this is fine because there are no dependencies between these two functions.
---
v8 -> v9:
- Minor changelog improvement suggested by Rick and Reniette.
--- arch/x86/virt/vmx/tdx/tdx.c | 19 ++++++++++++------- arch/x86/virt/vmx/tdx/tdx.h | 19 ++++++++++++------- 2 files changed, 24 insertions(+), 14 deletions(-)
arch/x86/virt/vmx/tdx/tdx.c +12 -7
@@ -326,6 +326,11 @@ static int get_tdx_sys_info_tdmr(struct tdx_sys_info_tdmr *sysinfo_tdmr)return 0 ; } +static int get_tdx_sys_info (struct tdx_sys_info *sysinfo) +{ +return get_tdx_sys_info_tdmr (&sysinfo->tdmr); +} +/* Calculate the actual TDMR size */ static int tdmr_size_single (u16 max_reserved_per_tdmr) {
@@ -1098,9 +1103,13 @@ static int init_tdmrs(struct tdmr_info_list *tdmr_list)static int init_tdx_module (void ) { -struct tdx_sys_info_tdmr sysinfo_tdmr; +struct tdx_sys_info sysinfo;int ret; + ret =get_tdx_sys_info (&sysinfo); +if (ret) +return ret; +/* * To keep things simple, assume that all TDX-protected memory * will come from the page allocator. Make sure all pages in the
@@ -1117,17 +1126,13 @@ static int init_tdx_module(void)if (ret) goto out_put_tdxmem; - ret =get_tdx_sys_info_tdmr (&sysinfo_tdmr); -if (ret) - goto err_free_tdxmem; -/* Allocate enough space for constructing TDMRs */ - ret =alloc_tdmr_list (&tdx_tdmr_list, &sysinfo_tdmr); + ret =alloc_tdmr_list (&tdx_tdmr_list, &sysinfo.tdmr);if (ret) goto err_free_tdxmem;/* Cover all TDX-usable memory regions in TDMRs */ - ret =construct_tdmrs (&tdx_memlist, &tdx_tdmr_list, &sysinfo_tdmr); + ret =construct_tdmrs (&tdx_memlist, &tdx_tdmr_list, &sysinfo.tdmr);if (ret) goto err_free_tdmrs;
arch/x86/virt/vmx/tdx/tdx.h +12 -7
@@ -80,6 +80,18 @@ struct tdmr_info { DECLARE_FLEX_ARRAY(struct tdmr_reserved_area, reserved_areas); } __packed__aligned(TDMR_INFO_ALIGNMENT) ; +/* Class "TDMR info" */ +struct tdx_sys_info_tdmr { +u16 max_tdmrs; +u16 max_reserved_per_tdmr; +u16 pamt_entry_size[TDX_PS_NR ]; +}; + +/* Kernel used global metadata fields */ +struct tdx_sys_info { +struct tdx_sys_info_tdmr tdmr; +}; +/* * Do not put any hardware-defined TDX structure representations below * this comment!
@@ -99,13 +111,6 @@ struct tdx_memblock {int nid; }; -/* "TDMR info" part of "Global Scope Metadata" for constructing TDMRs */ -struct tdx_sys_info_tdmr { -u16 max_tdmrs; -u16 max_reserved_per_tdmr; -u16 pamt_entry_size[TDX_PS_NR ]; -}; -/* Warn if kernel has less than TDMR_NR_WARN TDMRs after allocation */ #define TDMR_NR_WARN 4
Signature · 1 line
2.47.1
PATCH v9 3/6 x86/virt/tdx: Use auto-generated code to read global metadata
In reply to Kai HuangFrom: Paolo Bonzini <[email protected]>
The TDX module provides a set of "Global Metadata Fields". They report things like TDX module version, supported features, and fields related to create/run TDX guests and so on.
Currently the kernel only reads "TD Memory Region" (TDMR) related fields for module initialization. There are needs to read more global metadata fields for future use:
- Supported features ("TDX_FEATURES0") to fail module initialization when the module doesn't support "not clobbering host RBP when exiting from TDX guest" feature [1].
- KVM TDX baseline support and other features like TDX Connect will need to read more.
The current global metadata reading code has limitations (e.g., it only has a primitive helper to read metadata field with 16-bit element size, while TDX supports 8/16/32/64 bits metadata element sizes). It needs tweaks in order to read more metadata fields.
But even with the tweaks, when new code is added to read a new field, the reviewers will still need to review against the spec to make sure the new code doesn't screw up things like using the wrong metadata field ID (each metadata field is associated with a unique field ID, which is a TDX-defined u64 constant) etc.
TDX documents all global metadata fields in a 'global_metadata.json' file as part of TDX spec [2]. JSON format is machine readable. Instead of tweaking the metadata reading code, use a script to generate the code so that:
1) Using the generated C is simple.
2) Adding a field is simple, e.g., the script just pulls the field ID out of the JSON for a given field thus no manual review is needed.
Specifically, to match the layout of the 'struct tdx_sys_info' and its sub-structures, the script uses a table with each entry containing the the name of the sub-structures (which reflects the "Class") and the "Field Name" of all its fields, and auto-generate:
1) The 'struct tdx_sys_info' and all 'struct tdx_sys_info_xx' sub-structures in 'tdx_global_metadata.h'.
2) The main function 'get_tdx_sys_info()' which reads all metadata to 'struct tdx_sys_info' and the 'get_tdx_sys_info_xx()' functions which read 'struct tdx_sys_info_xx()' in 'tdx_global_metadata.c'.
Using the generated C is simple: 1) include "tdx_global_metadata.h" to the local "tdx.h"; 2) explicitly include "tdx_global_metadata.c" to the local "tdx.c" after the read_sys_metadata_field() primitive (which is a wrapper of TDH.SYS.RD SEAMCALL to read global metadata).
Adding a field is also simple: 1) just add the new field to an existing structure, or add it with a new structure; 2) re-run the script to generate the new code; 3) update the existing tdx_global_metadata.{hc} with the new ones.
For now, use the auto-generated code to read the TDMR related fields and the aforesaid metadata field "TDX_FEATURES0".
The tdx_global_metadata.{hc} can be generated by running below:
#python tdx_global_metadata.py global_metadata.json \
tdx_global_metadata.h tdx_global_metadata.c
.. where the 'global_metadata.json' can be fetched from [2] and the 'tdx_global_metadata.py' can be found from [3].
---
v8 -> v9:
- Remove CMR reading code and update changelog accordingly.
--- arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 48 +++++++++++++++++++++ arch/x86/virt/vmx/tdx/tdx_global_metadata.h | 25 +++++++++++ 2 files changed, 73 insertions(+) create mode 100644 arch/x86/virt/vmx/tdx/tdx_global_metadata.c create mode 100644 arch/x86/virt/vmx/tdx/tdx_global_metadata.h
arch/x86/virt/vmx/tdx/tdx_global_metadata.c new file +48 -0
@@ -0,0 +1,48 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Automatically generated 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 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 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 int get_tdx_sys_info (struct tdx_sys_info *sysinfo) +{ +int ret =0 ; + + ret = ret ?:get_tdx_sys_info_features (&sysinfo->features); + ret = ret ?:get_tdx_sys_info_tdmr (&sysinfo->tdmr); + +return ret; +}
arch/x86/virt/vmx/tdx/tdx_global_metadata.h new file +25 -0
@@ -0,0 +1,25 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Automatically generated TDX global metadata structures. */ +#ifndef _X86_VIRT_TDX_AUTO_GENERATED_TDX_GLOBAL_METADATA_H +#define _X86_VIRT_TDX_AUTO_GENERATED_TDX_GLOBAL_METADATA_H + +#include <linux/types.h> + +struct tdx_sys_info_features { +u64 tdx_features0; +}; + +struct tdx_sys_info_tdmr { +u16 max_tdmrs; +u16 max_reserved_per_tdmr; +u16 pamt_4k_entry_size; +u16 pamt_2m_entry_size; +u16 pamt_1g_entry_size; +}; + +struct tdx_sys_info { +struct tdx_sys_info_features features; +struct tdx_sys_info_tdmr tdmr; +}; + +#endif
Signature · 1 line
2.47.1
PATCH v9 4/6 x86/virt/tdx: Use dedicated struct members for PAMT entry sizes
In reply to Kai HuangCurrently, the 'struct tdmr_sys_info_tdmr' which includes TDMR related fields defines the PAMT entry sizes for TDX supported page sizes (4KB, 2MB and 1GB) as an array:
struct tdx_sys_info_tdmr {
...
u16 pamt_entry_sizes[TDX_PS_NR];
};PAMT entry sizes are needed when allocating PAMTs for each TDMR. Using the array to contain PAMT entry sizes reduces the number of arguments that need to be passed when calling tdmr_set_up_pamt(). It also makes the code pattern like below clearer:
for (pgsz = TDX_PS_4K; pgsz < TDX_PS_NR; pgsz++) {
pamt_size[pgsz] = tdmr_get_pamt_sz(tdmr, pgsz,
pamt_entry_size[pgsz]);
tdmr_pamt_size += pamt_size[pgsz];
}However, the auto-generated metadata reading code generates a structure member for each field. The 'global_metadata.json' has a dedicated field for each PAMT entry size, and the new 'struct tdx_sys_info_tdmr' looks like:
struct tdx_sys_info_tdmr {
...
u16 pamt_4k_entry_size;
u16 pamt_2m_entry_size;
u16 pamt_1g_entry_size;
};Prepare to use the autogenerated code by making the existing 'struct tdx_sys_info_tdmr' look like the generated one. When passing to tdmrs_set_up_pamt_all(), build a local array of PAMT entry sizes from the structure so the code to allocate PAMTs can stay the same.
--- arch/x86/virt/vmx/tdx/tdx.c | 14 +++++++++----- arch/x86/virt/vmx/tdx/tdx.h | 4 +++- 2 files changed, 12 insertions(+), 6 deletions(-)
arch/x86/virt/vmx/tdx/tdx.c +9 -5
@@ -304,9 +304,9 @@ struct field_mapping {static const struct field_mapping fields[] = {TD_SYSINFO_MAP (MAX_TDMRS , max_tdmrs),TD_SYSINFO_MAP (MAX_RESERVED_PER_TDMR , max_reserved_per_tdmr), -TD_SYSINFO_MAP (PAMT_4K_ENTRY_SIZE , pamt_entry_size[TDX_PS_4K ]), -TD_SYSINFO_MAP (PAMT_2M_ENTRY_SIZE , pamt_entry_size[TDX_PS_2M ]), -TD_SYSINFO_MAP (PAMT_1G_ENTRY_SIZE , pamt_entry_size[TDX_PS_1G ]), +TD_SYSINFO_MAP (PAMT_4K_ENTRY_SIZE , pamt_4k_entry_size), +TD_SYSINFO_MAP (PAMT_2M_ENTRY_SIZE , pamt_2m_entry_size), +TD_SYSINFO_MAP (PAMT_1G_ENTRY_SIZE , pamt_1g_entry_size), };static int get_tdx_sys_info_tdmr (struct tdx_sys_info_tdmr *sysinfo_tdmr)
@@ -932,14 +932,18 @@ static int construct_tdmrs(struct list_head *tmb_list,struct tdmr_info_list *tdmr_list, struct tdx_sys_info_tdmr *sysinfo_tdmr) { + u16 pamt_entry_size[TDX_PS_NR] = { + sysinfo_tdmr->pamt_4k_entry_size, + sysinfo_tdmr->pamt_2m_entry_size, + sysinfo_tdmr->pamt_1g_entry_size, + };int ret; ret =fill_out_tdmrs (tmb_list, tdmr_list);if (ret)return ret; - ret =tdmrs_set_up_pamt_all (tdmr_list, tmb_list, - sysinfo_tdmr->pamt_entry_size); + ret =tdmrs_set_up_pamt_all (tdmr_list, tmb_list, pamt_entry_size);if (ret)return ret;
arch/x86/virt/vmx/tdx/tdx.h +3 -1
@@ -84,7 +84,9 @@ struct tdmr_info {struct tdx_sys_info_tdmr {u16 max_tdmrs;u16 max_reserved_per_tdmr; -u16 pamt_entry_size[TDX_PS_NR ]; +u16 pamt_4k_entry_size; +u16 pamt_2m_entry_size; +u16 pamt_1g_entry_size; };/* Kernel used global metadata fields */
Signature · 1 line
2.47.1
PATCH v9 5/6 x86/virt/tdx: Switch to use auto-generated global metadata reading code
In reply to Kai HuangContinue the process to have a centralized solution for TDX global metadata reading. Now that the new autogenerated solution is ready for use, switch to it and remove the old one.
---
v8 -> v9:
- Improve changelog suggested by Rick.
--- arch/x86/virt/vmx/tdx/tdx.c | 61 +------------------------------------ arch/x86/virt/vmx/tdx/tdx.h | 45 +-------------------------- 2 files changed, 2 insertions(+), 104 deletions(-)
arch/x86/virt/vmx/tdx/tdx.c +1 -60
@@ -270,66 +270,7 @@ static int read_sys_metadata_field(u64 field_id, u64 *data)return 0 ; } -static int read_sys_metadata_field16 (u64 field_id, -int offset, -struct tdx_sys_info_tdmr *ts) -{ -u16 *ts_member = ((void *)ts) + offset; -u64 tmp; -int ret; - -if (WARN_ON_ONCE (MD_FIELD_ID_ELE_SIZE_CODE (field_id) != -MD_FIELD_ID_ELE_SIZE_16BIT )) -return -EINVAL ; - - ret =read_sys_metadata_field (field_id, &tmp); -if (ret) -return ret; - - *ts_member = tmp; - -return 0 ; -} - -struct field_mapping { -u64 field_id; -int offset; -}; - -#define TD_SYSINFO_MAP (_field_id, _offset) \ - { .field_id = MD_FIELD_ID_##_field_id, \ - .offset = offsetof(struct tdx_sys_info_tdmr, _offset) } - -/* Map TD_SYSINFO fields into 'struct tdx_sys_info_tdmr': */ -static const struct field_mapping fields[] = { -TD_SYSINFO_MAP (MAX_TDMRS , max_tdmrs), -TD_SYSINFO_MAP (MAX_RESERVED_PER_TDMR , max_reserved_per_tdmr), -TD_SYSINFO_MAP (PAMT_4K_ENTRY_SIZE , pamt_4k_entry_size), -TD_SYSINFO_MAP (PAMT_2M_ENTRY_SIZE , pamt_2m_entry_size), -TD_SYSINFO_MAP (PAMT_1G_ENTRY_SIZE , pamt_1g_entry_size), -}; - -static int get_tdx_sys_info_tdmr (struct tdx_sys_info_tdmr *sysinfo_tdmr) -{ -int ret; -int i; - -/* Populate 'sysinfo_tdmr' fields using the mapping structure above: */ -for (i =0 ; i <ARRAY_SIZE (fields); i++) { - ret =read_sys_metadata_field16 (fields[i].field_id, - fields[i].offset, - sysinfo_tdmr); -if (ret) -return ret; - } - -return 0 ; -} - -static int get_tdx_sys_info (struct tdx_sys_info *sysinfo) -{ -return get_tdx_sys_info_tdmr (&sysinfo->tdmr); -} +#include "tdx_global_metadata.c" /* Calculate the actual TDMR size */ static int tdmr_size_single (u16 max_reserved_per_tdmr)
arch/x86/virt/vmx/tdx/tdx.h +1 -44
63 lines not on this page · raw message
Signature · 1 line
2.47.1
PATCH v9 6/6 x86/virt/tdx: Require the module to assert it has the NO_RBP_MOD mitigation
In reply to Kai HuangOld TDX modules can clobber RBP in the TDH.VP.ENTER SEAMCALL. However RBP is used as frame pointer in the x86_64 calling convention, and clobbering RBP could result in bad things like being unable to unwind the stack if any non-maskable exceptions (NMI, #MC etc) happens in that gap.
A new "NO_RBP_MOD" feature was introduced to more recent TDX modules to not clobber RBP. KVM will need to use the TDH.VP.ENTER SEAMCALL to run TDX guests. It won't be safe to run TDX guests w/o this feature. To prevent it, just don't initialize the TDX module if this feature is not supported [1].
Note the bit definitions of TDX_FEATURES0 are not auto-generated in tdx_global_metadata.h. Manually define a macro for it in "tdx.h".
---
v8 -> v9:
- Minor changelog improvement suggested by Rick and Reniette.
- Rebase due to the removal of CMR reading code.
Note:
This doesn't need to be included to stable kernels because TDH.VP.ENTER won't be used until KVM is able to support TDX.
--- arch/x86/virt/vmx/tdx/tdx.c | 17 +++++++++++++++++ arch/x86/virt/vmx/tdx/tdx.h | 4 ++++ 2 files changed, 21 insertions(+)
arch/x86/virt/vmx/tdx/tdx.c +17 -0
@@ -272,6 +272,18 @@ static int read_sys_metadata_field(u64 field_id, u64 *data)#include "tdx_global_metadata.c" +static int check_features (struct tdx_sys_info *sysinfo) +{ +u64 tdx_features0 = sysinfo->features.tdx_features0; + +if (!(tdx_features0 &TDX_FEATURES0_NO_RBP_MOD )) { +pr_err ("frame pointer (RBP) clobber bug present, upgrade TDX module\n" ); +return -EINVAL ; + } + +return 0 ; +} +/* Calculate the actual TDMR size */ static int tdmr_size_single (u16 max_reserved_per_tdmr) {
@@ -1055,6 +1067,11 @@ static int init_tdx_module(void)if (ret)return ret; +/* Check whether the kernel can support this module */ + ret =check_features (&sysinfo); +if (ret) +return ret; +/* * To keep things simple, assume that all TDX-protected memory * will come from the page allocator. Make sure all pages in the
arch/x86/virt/vmx/tdx/tdx.h +4 -0
@@ -2,6 +2,7 @@#ifndef _X86_VIRT_TDX_H#define _X86_VIRT_TDX_H +#include <linux/bits.h> #include "tdx_global_metadata.h" /*
@@ -51,6 +52,9 @@ struct tdmr_info { DECLARE_FLEX_ARRAY(struct tdmr_reserved_area, reserved_areas); } __packed__aligned(TDMR_INFO_ALIGNMENT) ; +/* Bit definitions of TDX_FEATURES0 metadata field */ +#define TDX_FEATURES0_NO_RBP_MOD BIT(18) +/* * Do not put any hardware-defined TDX structure representations below * this comment!
Signature · 1 line
2.47.1