Runtime TDX module update support
Subsystems (6)
v10 cover Runtime TDX module update support
Hi Dave & Rick,
Thanks for your thorough review of v9. This v10 addresses the issues you pointed out. The main changes in this version are polishing changelogs and variable renames to improve readability. Specifically:
- Patches 1-2 (new): Split the original "Consolidate TDX global initialization states" into two steps — first move the statics to file scope, then clarify the result-caching logic in try_init_module_global().
- Patch 6: Removed user-facing Kconfig help text for TDX_HOST_SERVICES (now a silent tristate auto-selected by INTEL_TDX_HOST).
- Patch 13: Renamed "size" to "data_len" in seamldr_install_module() and init_seamldr_params(); renamed "HEADER_SIZE" to "TDX_IMAGE_HEADER_SIZE"; renamed "primary" to "is_lead_cpu" in the update state machine.
- Patch 13: Added early data_len validation and explicit bounds checks on sigstruct_nr_pages/module_nr_pages against SEAMLDR_MAX_NR_* limits, removing the implicit clamping in populate_pa_list().
- Patch 22: Fixed BIT(16) -> BIT_ULL(16) for TDX_SYS_SHUTDOWN_AVOID_COMPAT_SENSITIVE.
- Patch 22: Removed unused TDX_FEATURES0_UPDATE_COMPAT definition.
- Various patches: Shortened sysfs ABI descriptions, tightened comments across seamldr.h and seamldr.c, and minor style fixes (return 0 -> return false, unfolded conditionals)
Please take a look at this new version. I hope it can still be merged for 7.2.
---
(For transparency, note that I used AI tools to help proofread this cover-letter and commit messages)
This series adds support for runtime TDX module updates that preserve running TDX guests. It is also available at:
https://github.com/gaochaointel/linux-dev/commits/tdx-module-updates-v10/
== Background ==
Intel TDX isolates Trusted Domains (TDs), or confidential guests, from the host. A key component of Intel TDX is the TDX module, which enforces security policies to protect the memory and CPU states of TDs from the host. However, the TDX module is software that requires updates.
== Problems ==
Currently, the TDX module is loaded by the BIOS at boot time, and the only way to update it is through a reboot, which results in significant system downtime. Users expect the TDX module to be updatable at runtime without disrupting TDX guests.
== Solution ==
On TDX platforms, P-SEAMLDR[1] is a component within the protected SEAM range. It is loaded by the BIOS and provides the host with functions to install a TDX module at runtime.
This series implements runtime TDX module updates through the fw_upload mechanism. That interface is a good fit because TDX module selection is not a simple "load a known file from disk" problem. The update image to load depends on module versioning, compatibility rules. fw_upload lets userspace choose the module explicitly while the kernel provides the update mechanism.
This design intentionally keeps most update validation/policy in userspace. The kernel exposes the information userspace needs, such as TDX module version and P-SEAMLDR information, but userspace is responsible for understanding TDX module's versioning and compatibility rules and for choosing an appropriate update image (see "TDX module versioning" below).
The kernel still enforces the pieces that must be handled in-kernel:
1. Validate the tdx_blob header fields that are not passed through tothe TDX module. Just the standard overflow and reserved bits defensive ABI stuff.
2. Make sure no non-update SEAMCALLs are called during the update.
3. Make sure SEAMCALLs are on the right CPU, for any the user has made available to the kernel.
4. Handle the race between updates and concurrent TD builds by returning -EBUSY to userspace.
Everything else remains a userspace responsibility.
In the unlikely event the update fails, for example userspace picks an incompatible update image, or the image is otherwise corrupted, all TDs will experience SEAMCALL failures and be killed. The recovery of TD operation from that event requires a reboot.
Given there is no mechanism to quiesce SEAMCALLs, the TDs themselves must pause execution over an update. The most straightforward way to meet the 'pause TDs while update executes' constraint is to run the update in stop_machine() context. All other evaluated solutions export more complexity to KVM, or exports more fragility to userspace.
== How to test this series ==
NOTE: This v10 uses a new tdx_blob format. The scripts and module blobs in
https://github.com/intel/tdx-module-binaries have not yet been updated to match this version. Those updates will be done separately later.
== Other information relevant to Runtime TDX module updates ==
=== TDX module versioning ===
Each TDX module is assigned a version number x.y.z, where x represents the "major" version, y the "minor" version, and z the "update" version.
Runtime TDX module updates are restricted to Z-stream releases.
Note that Z-stream releases do not necessarily guarantee compatibility. A new release may not be compatible with all previous versions. To address this, Intel provides a separate file containing compatibility information, which specifies the minimum module version required for a particular update. This information is referenced by the tool to determine if two modules are compatible.
=== TCB Stability ===
Updates change the TCB as viewed by attestation reports. In TDX there is a distinction between "launch-time" version and "current" version where runtime TDX module updates cause that "current" version number to change, subject to Z-stream constraints.
The concern that a malicious host may attack confidential VMs by loading insecure updates was addressed by Alex in [3]. Similarly, the scenario where some "theoretical paranoid tenant" in the cloud wants to audit updates and stop trusting the host after updates until audit completion was also addressed in [4]. Users not in the cloud control the host machine and can manage updates themselves, so they don't have these concerns.
See more about the implications of current TCB version changes in attestation as summarized by Dave in [5].
=== TDX module Distribution Model ===
At a high level, Intel publishes all TDX modules on the github [2], along with a mapping_file.json which documents the compatibility information about each TDX module and a userspace tool to install the TDX module. OS vendors can package these modules and distribute them. Administrators install the package and use the tool to select the appropriate TDX module and install it via the interfaces exposed by this series.
[1]: https://cdrdv2.intel.com/v1/dl/getContent/733584
[2]: https://github.com/intel/tdx-module-binaries
[3]: https://lore.kernel.org/all/[email protected]/
[4]: https://lore.kernel.org/all/[email protected]/
[5]: https://lore.kernel.org/all/[email protected]/
Chao Gao (24):
x86/virt/tdx: Clarify try_init_module_global() result caching x86/virt/tdx: Move TDX global initialization states to file scope x86/virt/tdx: Consolidate TDX global initialization states x86/virt/tdx: Move TDX_FEATURES0 bits to asm/tdx.h coco/tdx-host: Introduce a "tdx_host" device coco/tdx-host: Expose TDX module version
x86/virt/seamldr: Introduce a wrapper for P-SEAMLDR SEAMCALLs x86/virt/seamldr: Add a helper to retrieve P-SEAMLDR information coco/tdx-host: Expose P-SEAMLDR information via sysfs coco/tdx-host: Don't expose P-SEAMLDR information on CPUs with erratum coco/tdx-host: Implement firmware upload sysfs ABI for TDX module
updates
x86/virt/seamldr: Allocate and populate a module update request x86/virt/seamldr: Introduce skeleton for TDX module updates x86/virt/seamldr: Abort updates after a failed step x86/virt/seamldr: Shut down the current TDX module x86/virt/tdx: Reset software states during TDX module shutdown x86/virt/seamldr: Install a new TDX module x86/virt/seamldr: Do TDX global and per-CPU init after module
installation
x86/virt/tdx: Restore TDX module state
x86/virt/tdx: Refresh TDX module version after update x86/virt/tdx: Reject updates during compatibility-sensitive operations x86/virt/tdx: Enable TDX module runtime updates coco/tdx-host: Document TDX module update compatibility criteria x86/virt/tdx: Document TDX module update
Kai Huang (1):
x86/virt/tdx: Move low level SEAMCALL helpers out of <asm/tdx.h>
.../ABI/testing/sysfs-devices-faux-tdx-host | 66 ++++ Documentation/arch/x86/tdx.rst | 34 ++ arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/seamldr.h | 36 ++ arch/x86/include/asm/tdx.h | 66 +--- arch/x86/include/asm/tdx_global_metadata.h | 4 + arch/x86/include/asm/vmx.h | 1 + arch/x86/virt/vmx/tdx/Makefile | 2 +- arch/x86/virt/vmx/tdx/seamcall_internal.h | 109 ++++++ arch/x86/virt/vmx/tdx/seamldr.c | 324 ++++++++++++++++++ arch/x86/virt/vmx/tdx/tdx.c | 169 +++++---- arch/x86/virt/vmx/tdx/tdx.h | 8 +- arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 17 +- drivers/virt/coco/Kconfig | 2 + drivers/virt/coco/Makefile | 1 + drivers/virt/coco/tdx-host/Kconfig | 6 + drivers/virt/coco/tdx-host/Makefile | 1 + drivers/virt/coco/tdx-host/tdx-host.c | 231 +++++++++++++ 18 files changed, 965 insertions(+), 113 deletions(-) create mode 100644 Documentation/ABI/testing/sysfs-devices-faux-tdx-host create mode 100644 arch/x86/include/asm/seamldr.h create mode 100644 arch/x86/virt/vmx/tdx/seamcall_internal.h create mode 100644 arch/x86/virt/vmx/tdx/seamldr.c create mode 100644 drivers/virt/coco/tdx-host/Kconfig create mode 100644 drivers/virt/coco/tdx-host/Makefile create mode 100644 drivers/virt/coco/tdx-host/tdx-host.c
base-commit: 5209e5bfe5cab593476c3e7754e42c5e47ce36de
Signature · 1 line
2.52.0
PATCH v10 1/25 x86/virt/tdx: Clarify try_init_module_global() result caching
TDX module global initialization is executed only once. The first call caches both the result and the "done" state, and later callers reuse the saved result. A lock protects that cached state.
The current code is hard to read because sysinit_done is accessed under the lock, while sysinit_ret is not.
To improve readability, move sysinit_ret accesses within the lock.
Group sysinit_ret/sysinit_done updates right after initialization so Caching the result is separate from the initialization itself.
--- arch/x86/virt/vmx/tdx/tdx.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)
arch/x86/virt/vmx/tdx/tdx.c +10 -4
@@ -115,28 +115,34 @@ static int try_init_module_global(void)static DEFINE_RAW_SPINLOCK (sysinit_lock);static bool sysinit_done;static int sysinit_ret; +int ret;raw_spin_lock (&sysinit_lock); -if (sysinit_done) +/* Return the "cached" return code. */ +if (sysinit_done) { + ret = sysinit_ret; goto out; + }/* RCX is module attributes and all bits are reserved */ args.rcx =0 ; - sysinit_ret =seamcall_prerr (TDH_SYS_INIT , &args); + ret =seamcall_prerr (TDH_SYS_INIT , &args);/* * The first SEAMCALL also detects the TDX module, thus * it can fail due to the TDX module is not loaded. * Dump message to let the user know. */ -if (sysinit_ret == -ENODEV ) +if (ret == -ENODEV )pr_err ("module not loaded\n" ); +/* Save the return code for later callers. */ sysinit_done = true; + sysinit_ret = ret; out:raw_spin_unlock (&sysinit_lock); -return sysinit_ret; +return ret; }/**
Signature · 1 line
2.52.0
Re: [PATCH v10 01/25] x86/virt/tdx: Clarify try_init_module_global() result caching
Quoted 7 lines · Chao Gao
TDX module global initialization is executed only once. The first call caches both the result and the "done" state, and later callers reuse the saved result. A lock protects that cached state.
The current code is hard to read because
sysinit_doneis accessed under the lock, whilesysinit_retis not.
Quoted 6 lines
To improve readability, move
sysinit_retaccesses within the lock.Group sysinit_ret/sysinit_done updates right after initialization so Caching the result is separate from the initialization itself.
Signed-off-by Chao Gao <[email protected]>
Kiryl Shutsemau
maintainerRe: [PATCH v10 01/25] x86/virt/tdx: Clarify try_init_module_global() result caching
In reply to Chao GaoQuoted 8 lines · Chao Gao
TDX module global initialization is executed only once. The first call caches both the result and the "done" state, and later callers reuse the saved result. A lock protects that cached state.
The current code is hard to read because
sysinit_doneis accessed under the lock, whilesysinit_retis not.To improve readability, move
sysinit_retaccesses within the lock.
Have you considered using the guard for the lock. It would simplify the flow.
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
[tip: x86/tdx] x86/virt/tdx: Clarify try_init_module_global() result caching
In reply to Chao GaoThe following commit has been merged into the x86/tdx branch of tip:
The following commit has been merged into the x86/tdx branch of tip:
Commit-ID: 1ffa6a10253c417b281aff3cbd02bdf43b2b159d Gitweb: https://git.kernel.org/tip/1ffa6a10253c417b281aff3cbd02bdf43b2b159d Author: Chao Gao <[email protected]> AuthorDate: Wed, 20 May 2026 15:28:46 -07:00 Committer: Dave Hansen <[email protected]> CommitterDate: Wed, 03 Jun 2026 08:14:39 -07:00
x86/virt/tdx: Clarify try_init_module_global() result caching
TDX module global initialization is executed only once. The first call caches both the return code and the "done" state in static function variables. Later callers read the variables. A lock protects the saved state and serializes callers.
These variables will soon be moved to a global structure. Prepare for that by treating the variables as a unit. Assign them together and limit accesses to while the lock is held.
[ dhansen: mostly rewrite changelog ]
--- arch/x86/virt/vmx/tdx/tdx.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)
arch/x86/virt/vmx/tdx/tdx.c +10 -4
@@ -115,28 +115,34 @@ static int try_init_module_global(void)static DEFINE_RAW_SPINLOCK (sysinit_lock);static bool sysinit_done;static int sysinit_ret; +int ret;raw_spin_lock (&sysinit_lock); -if (sysinit_done) +/* Return the "cached" return code. */ +if (sysinit_done) { + ret = sysinit_ret; goto out; + }/* RCX is module attributes and all bits are reserved */ args.rcx =0 ; - sysinit_ret =seamcall_prerr (TDH_SYS_INIT , &args); + ret =seamcall_prerr (TDH_SYS_INIT , &args);/* * The first SEAMCALL also detects the TDX module, thus * it can fail due to the TDX module is not loaded. * Dump message to let the user know. */ -if (sysinit_ret == -ENODEV ) +if (ret == -ENODEV )pr_err ("module not loaded\n" ); +/* Save the return code for later callers. */ sysinit_done = true; + sysinit_ret = ret; out:raw_spin_unlock (&sysinit_lock); -return sysinit_ret; +return ret; }/**
PATCH v10 2/25 x86/virt/tdx: Move TDX global initialization states to file scope
In reply to Chao GaoTDX module global initialization is executed only once. The first call caches both the result and the "done" state, and later callers reuse the saved result. A lock protects that cached states.
Those states and the lock are currently kept as function-local statics because they are used only by try_init_module_global().
TDX module updates need to reset the cached states so TDX global initialization can be run again after an update. That will add another access site in the same file.
Move the cached states to file scope so it is accessible outside try_init_module_global(), and move the lock along with the states it protects.
No functional change intended.
--- arch/x86/virt/vmx/tdx/tdx.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
arch/x86/virt/vmx/tdx/tdx.c +4 -3
@@ -105,6 +105,10 @@ static __always_inline int sc_retry_prerr(sc_func_t func,#define seamcall_prerr_ret (__fn, __args) \ sc_retry_prerr(__seamcall_ret, seamcall_err_ret, (__fn), (__args)) +static DEFINE_RAW_SPINLOCK (sysinit_lock); +static bool sysinit_done; +static int sysinit_ret; +/* * Do the module global initialization once and return its result. * It can be done on any cpu, and from task or IRQ context.
@@ -112,9 +116,6 @@ static __always_inline int sc_retry_prerr(sc_func_t func,static int try_init_module_global (void ) {struct tdx_module_args args = {}; -static DEFINE_RAW_SPINLOCK (sysinit_lock); -static bool sysinit_done; -static int sysinit_ret;int ret;raw_spin_lock (&sysinit_lock);
Signature · 1 line
2.52.0
Re: [PATCH v10 02/25] x86/virt/tdx: Move TDX global initialization states to file scope
Quoted 11 lines · Chao Gao
TDX module global initialization is executed only once. The first call caches both the result and the "done" state, and later callers reuse the saved result. A lock protects that cached states.
Those states and the lock are currently kept as function-local statics because they are used only by
try_init_module_global().TDX module updates need to reset the cached states so TDX global initialization can be run again after an update. That will add another access site in the same file.
Quoted 7 lines
Move the cached states to file scope so it is accessible outside
try_init_module_global(), and move the lock along with the states it protects.No functional change intended.
Signed-off-by Chao Gao <[email protected]>
Re: [PATCH v10 02/25] x86/virt/tdx: Move TDX global initialization states to file scope
In reply to Chao GaoQuoted 11 lines · Chao Gao
TDX module global initialization is executed only once. The first call caches both the result and the "done" state, and later callers reuse the saved result. A lock protects that cached states.
Those states and the lock are currently kept as function-local statics because they are used only by
try_init_module_global().TDX module updates need to reset the cached states so TDX global initialization can be run again after an update. That will add another access site in the same file.
Quoted 7 lines
Move the cached states to file scope so it is accessible outside
try_init_module_global(), and move the lock along with the states it protects.No functional change intended.
Signed-off-by Chao Gao <[email protected]>
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
[tip: x86/tdx] x86/virt/tdx: Move TDX global initialization states to file scope
In reply to Chao GaoThe following commit has been merged into the x86/tdx branch of tip:
The following commit has been merged into the x86/tdx branch of tip:
Commit-ID: 9c0c68708dc48e8ae4f9043c96b387a0173361f1 Gitweb: https://git.kernel.org/tip/9c0c68708dc48e8ae4f9043c96b387a0173361f1 Author: Chao Gao <[email protected]> AuthorDate: Wed, 20 May 2026 15:28:47 -07:00 Committer: Dave Hansen <[email protected]> CommitterDate: Wed, 03 Jun 2026 08:14:50 -07:00
x86/virt/tdx: Move TDX global initialization states to file scope
TDX module global initialization is executed only once. The first call caches both the result and the "done" state, and later callers reuse the saved result. A lock protects that cached states.
Those states and the lock are currently kept as function-local statics because they are used only by try_init_module_global().
TDX module updates need to reset the cached states so TDX global initialization can be run again after an update. That will add another access site in the same file.
Move the cached states to file scope so it is accessible outside try_init_module_global(), and move the lock along with the states it protects.
No functional change intended.
--- arch/x86/virt/vmx/tdx/tdx.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
arch/x86/virt/vmx/tdx/tdx.c +4 -3
@@ -105,6 +105,10 @@ static __always_inline int sc_retry_prerr(sc_func_t func,#define seamcall_prerr_ret (__fn, __args) \ sc_retry_prerr(__seamcall_ret, seamcall_err_ret, (__fn), (__args)) +static DEFINE_RAW_SPINLOCK (sysinit_lock); +static bool sysinit_done; +static int sysinit_ret; +/* * Do the module global initialization once and return its result. * It can be done on any cpu, and from task or IRQ context.
@@ -112,9 +116,6 @@ static __always_inline int sc_retry_prerr(sc_func_t func,static int try_init_module_global (void ) {struct tdx_module_args args = {}; -static DEFINE_RAW_SPINLOCK (sysinit_lock); -static bool sysinit_done; -static int sysinit_ret;int ret;raw_spin_lock (&sysinit_lock);
PATCH v10 3/25 x86/virt/tdx: Consolidate TDX global initialization states
In reply to Chao GaoThe kernel uses several global flags to guard one-time TDX initialization flows and prevent them from being repeated.
When the TDX module is updated, all of those states must be reset so that the module can be initialized again. Today those states are kept as separate global variables, which makes the reset path awkward and easy to miss when a new state is added.
Group the states into a single structure so they can be reset together, for example with memset(), and so a newly added state won't be missed.
Drop the __ro_after_init annotation from tdx_module_initialized because the other two states do not have it. And with TDX module update support, all the states need to be writable at runtime.
--- arch/x86/virt/vmx/tdx/tdx.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-)
arch/x86/virt/vmx/tdx/tdx.c +13 -9
@@ -44,6 +44,13 @@#include <asm/virt.h> #include "tdx.h" +struct tdx_module_state { +bool initialized; +bool sysinit_done; +int sysinit_ret; +}; + +static struct tdx_module_state tdx_module_state;static u32 tdx_global_keyid__ro_after_init ;static u32 tdx_guest_keyid_start__ro_after_init ;static u32 tdx_nr_guest_keyids__ro_after_init ;
@@ -58,7 +65,6 @@ static struct tdmr_info_list tdx_tdmr_list;static LIST_HEAD (tdx_memlist);static struct tdx_sys_info tdx_sysinfo__ro_after_init ; -static bool tdx_module_initialized__ro_after_init ;typedef void (*sc_err_func_t )(u64 fn,u64 err,struct tdx_module_args *args);
@@ -106,8 +112,6 @@ static __always_inline int sc_retry_prerr(sc_func_t func, sc_retry_prerr(__seamcall_ret, seamcall_err_ret, (__fn), (__args))static DEFINE_RAW_SPINLOCK (sysinit_lock); -static bool sysinit_done; -static int sysinit_ret;/* * Do the module global initialization once and return its result.
@@ -121,8 +125,8 @@ static int try_init_module_global(void)raw_spin_lock (&sysinit_lock);/* Return the "cached" return code. */ -if (sysinit_done) { - ret = sysinit_ret; +if (tdx_module_state.sysinit_done) { + ret = tdx_module_state.sysinit_ret; goto out; }
@@ -139,8 +143,8 @@ static int try_init_module_global(void)pr_err ("module not loaded\n" );/* Save the return code for later callers. */ - sysinit_done = true; - sysinit_ret = ret; + tdx_module_state.sysinit_done = true; + tdx_module_state.sysinit_ret = ret; out:raw_spin_unlock (&sysinit_lock);return ret;
@@ -1306,7 +1310,7 @@ static __init int tdx_enable(void)register_syscore (&tdx_syscore); - tdx_module_initialized = true; + tdx_module_state.initialized = true;pr_info ("TDX-Module initialized\n" );return 0 ; }
@@ -1561,7 +1565,7 @@ void __init tdx_init(void)const struct tdx_sys_info *tdx_get_sysinfo (void ) { -if (!tdx_module_initialized) +if (!tdx_module_state.initialized)return NULL ;return (const struct tdx_sys_info *)&tdx_sysinfo;
Signature · 1 line
2.52.0
Re: [PATCH v10 03/25] x86/virt/tdx: Consolidate TDX global initialization states
Quoted 8 lines · Chao Gao
The kernel uses several global flags to guard one-time TDX initialization flows and prevent them from being repeated.
When the TDX module is updated, all of those states must be reset so that the module can be initialized again. Today those states are kept as separate global variables, which makes the reset path awkward and easy to miss when a new state is added.
Quoted 8 lines
Group the states into a single structure so they can be reset together, for example with
memset(), and so a newly added state won't be missed.Drop the
__ro_after_initannotation fromtdx_module_initializedbecause the other two states do not have it. And with TDX module update support, all the states need to be writable at runtime.Signed-off-by Chao Gao <[email protected]>
Re: [PATCH v10 03/25] x86/virt/tdx: Consolidate TDX global initialization states
In reply to Chao GaoQuoted 8 lines · Chao Gao
The kernel uses several global flags to guard one-time TDX initialization flows and prevent them from being repeated.
When the TDX module is updated, all of those states must be reset so that the module can be initialized again. Today those states are kept as separate global variables, which makes the reset path awkward and easy to miss when a new state is added.
Quoted 6 lines
Group the states into a single structure so they can be reset together, for example with
memset(), and so a newly added state won't be missed.Drop the
__ro_after_initannotation fromtdx_module_initializedbecause the other two states do not have it. And with TDX module update support, all the states need to be writable at runtime.
You still can use __read_mostly.
Quoted 1 line
Signed-off-by Chao Gao <[email protected]>
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
[tip: x86/tdx] x86/virt/tdx: Consolidate TDX global initialization states
In reply to Chao GaoThe following commit has been merged into the x86/tdx branch of tip:
The following commit has been merged into the x86/tdx branch of tip:
Commit-ID: 451735bf90bfb294bb542cb2fa39ed01822aab86 Gitweb: https://git.kernel.org/tip/451735bf90bfb294bb542cb2fa39ed01822aab86 Author: Chao Gao <[email protected]> AuthorDate: Wed, 20 May 2026 15:28:48 -07:00 Committer: Dave Hansen <[email protected]> CommitterDate: Wed, 03 Jun 2026 08:14:51 -07:00
x86/virt/tdx: Consolidate TDX global initialization states
The kernel uses several global flags to guard one-time TDX initialization flows and prevent them from being repeated.
When the TDX module is updated, all of those states must be reset so that the module can be initialized again. Today those states are kept as separate global variables, which makes the reset path awkward and easy to miss when a new state is added.
Group the states into a single structure so they can be reset together, for example with memset(), and so a newly added state won't be missed.
Drop the __ro_after_init annotation from tdx_module_initialized because the other two states do not have it. And with TDX module update support, all the states need to be writable at runtime.
--- arch/x86/virt/vmx/tdx/tdx.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-)
arch/x86/virt/vmx/tdx/tdx.c +13 -9
@@ -44,6 +44,13 @@#include <asm/virt.h> #include "tdx.h" +struct tdx_module_state { +bool initialized; +bool sysinit_done; +int sysinit_ret; +}; + +static struct tdx_module_state tdx_module_state;static u32 tdx_global_keyid__ro_after_init ;static u32 tdx_guest_keyid_start__ro_after_init ;static u32 tdx_nr_guest_keyids__ro_after_init ;
@@ -58,7 +65,6 @@ static struct tdmr_info_list tdx_tdmr_list;static LIST_HEAD (tdx_memlist);static struct tdx_sys_info tdx_sysinfo__ro_after_init ; -static bool tdx_module_initialized__ro_after_init ;typedef void (*sc_err_func_t )(u64 fn,u64 err,struct tdx_module_args *args);
@@ -106,8 +112,6 @@ static __always_inline int sc_retry_prerr(sc_func_t func, sc_retry_prerr(__seamcall_ret, seamcall_err_ret, (__fn), (__args))static DEFINE_RAW_SPINLOCK (sysinit_lock); -static bool sysinit_done; -static int sysinit_ret;/* * Do the module global initialization once and return its result.
@@ -121,8 +125,8 @@ static int try_init_module_global(void)raw_spin_lock (&sysinit_lock);/* Return the "cached" return code. */ -if (sysinit_done) { - ret = sysinit_ret; +if (tdx_module_state.sysinit_done) { + ret = tdx_module_state.sysinit_ret; goto out; }
@@ -139,8 +143,8 @@ static int try_init_module_global(void)pr_err ("module not loaded\n" );/* Save the return code for later callers. */ - sysinit_done = true; - sysinit_ret = ret; + tdx_module_state.sysinit_done = true; + tdx_module_state.sysinit_ret = ret; out:raw_spin_unlock (&sysinit_lock);return ret;
@@ -1306,7 +1310,7 @@ static __init int tdx_enable(void)register_syscore (&tdx_syscore); - tdx_module_initialized = true; + tdx_module_state.initialized = true;pr_info ("TDX-Module initialized\n" );return 0 ; }
@@ -1561,7 +1565,7 @@ void __init tdx_init(void)const struct tdx_sys_info *tdx_get_sysinfo (void ) { -if (!tdx_module_initialized) +if (!tdx_module_state.initialized)return NULL ;return (const struct tdx_sys_info *)&tdx_sysinfo;
PATCH v10 4/25 x86/virt/tdx: Move TDX_FEATURES0 bits to asm/tdx.h
In reply to Chao GaoFuture changes will add support for new TDX features exposed as TDX_FEATURES0 bits. The presence of these features will need to be checked outside of arch/x86/virt. So the feature query helpers, and the TDX_FEATURES0 defines they reference, will need to live in the widely accessible asm/tdx.h header. Move the existing TDX_FEATURES0 to asm/tdx.h so that they can all be kept together.
Opportunistically switch to BIT_ULL() since TDX_FEATURES0 is 64-bit.
No functional change intended.
--- arch/x86/include/asm/tdx.h | 3 +++ arch/x86/virt/vmx/tdx/tdx.h | 3 --- 2 files changed, 3 insertions(+), 3 deletions(-)
arch/x86/include/asm/tdx.h +3 -0
@@ -32,6 +32,9 @@#define TDX_SUCCESS 0ULL#define TDX_RND_NO_ENTROPY 0x8000020300000000ULL +/* Bit definitions of TDX_FEATURES0 metadata field */ +#define TDX_FEATURES0_NO_RBP_MOD BIT_ULL(18) +#ifndef __ASSEMBLER__#include <uapi/asm/mce.h>
arch/x86/virt/vmx/tdx/tdx.h +0 -3
@@ -85,9 +85,6 @@ 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.52.0
Re: [PATCH v10 04/25] x86/virt/tdx: Move TDX_FEATURES0 bits to asm/tdx.h
Quoted 7 lines · Chao Gao
Future changes will add support for new TDX features exposed as
TDX_FEATURES0bits. The presence of these features will need to be checked outside ofarch/x86/virt. So the feature query helpers, and theTDX_FEATURES0defines they reference, will need to live in the widely accessibleasm/tdx.hheader. Move the existingTDX_FEATURES0toasm/tdx.hso that they can all be kept together.
Quoted 7 lines
Opportunistically switch to
BIT_ULL()sinceTDX_FEATURES0is 64-bit.No functional change intended.
Signed-off-by Chao Gao <[email protected]>
Kiryl Shutsemau
maintainerRe: [PATCH v10 04/25] x86/virt/tdx: Move TDX_FEATURES0 bits to asm/tdx.h
In reply to Chao GaoQuoted 10 lines · Chao Gao
Future changes will add support for new TDX features exposed as
TDX_FEATURES0bits. The presence of these features will need to be checked outside ofarch/x86/virt. So the feature query helpers, and theTDX_FEATURES0defines they reference, will need to live in the widely accessibleasm/tdx.hheader. Move the existingTDX_FEATURES0toasm/tdx.hso that they can all be kept together.Opportunistically switch to
BIT_ULL()sinceTDX_FEATURES0is 64-bit.No functional change intended.
I don't have a problem with the patch, but it seems to be colliding with DPAMT patchset that also moves the define around.
Rick, I assume this patchset going upstream first, right?
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
[tip: x86/tdx] x86/virt/tdx: Move TDX_FEATURES0 bits to asm/tdx.h
In reply to Chao GaoThe following commit has been merged into the x86/tdx branch of tip:
The following commit has been merged into the x86/tdx branch of tip:
Commit-ID: 77525820de70afd11ad7652bda005ce0ec1343af Gitweb: https://git.kernel.org/tip/77525820de70afd11ad7652bda005ce0ec1343af Author: Chao Gao <[email protected]> AuthorDate: Wed, 20 May 2026 15:28:49 -07:00 Committer: Dave Hansen <[email protected]> CommitterDate: Wed, 03 Jun 2026 08:14:51 -07:00
x86/virt/tdx: Move TDX_FEATURES0 bits to asm/tdx.h
Future changes will add support for new TDX features exposed as TDX_FEATURES0 bits. The presence of these features will need to be checked outside of arch/x86/virt. The feature query helpers and the TDX_FEATURES0 defines they reference will need to live in the widely accessible asm/tdx.h header. Move the existing TDX_FEATURES0 to asm/tdx.h so that they can all be kept together.
Opportunistically switch to BIT_ULL() since TDX_FEATURES0 is 64-bit.
No functional change intended.
[ dhansen: grammar fixups ]
--- arch/x86/include/asm/tdx.h | 3 +++ arch/x86/virt/vmx/tdx/tdx.h | 3 --- 2 files changed, 3 insertions(+), 3 deletions(-)
arch/x86/include/asm/tdx.h +3 -0
@@ -32,6 +32,9 @@#define TDX_SUCCESS 0ULL#define TDX_RND_NO_ENTROPY 0x8000020300000000ULL +/* Bit definitions of TDX_FEATURES0 metadata field */ +#define TDX_FEATURES0_NO_RBP_MOD BIT_ULL(18) +#ifndef __ASSEMBLER__#include <uapi/asm/mce.h>
arch/x86/virt/vmx/tdx/tdx.h +0 -3
@@ -85,9 +85,6 @@ 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!
PATCH v10 5/25 x86/virt/tdx: Move low level SEAMCALL helpers out of <asm/tdx.h>
In reply to Chao GaoFrom: Kai Huang <[email protected]>
TDX host core code implements three seamcall*() helpers to make SEAMCALLs to the TDX module. Currently, they are implemented in <asm/tdx.h> and are exposed to other kernel code which includes <asm/tdx.h>.
However, other than the TDX host core, seamcall*() are not expected to be used by other kernel code directly. For instance, for all SEAMCALLs that are used by KVM, the TDX host core exports a wrapper function for each of them.
Move seamcall*() and related code out of <asm/tdx.h> and make them only visible to TDX host core.
Since TDX host core tdx.c is already very heavy, don't put low level seamcall*() code there but to a new dedicated "seamcall_internal.h". Also, currently tdx.c has seamcall_prerr*() helpers which additionally print error message when calling seamcall*() fails. Move them to "seamcall_internal.h" as well. In such way all low level SEAMCALL helpers are in a dedicated place, which is much more readable.
Copy the copyright notice from the original files and consolidate the date ranges to:
Copyright (C) 2021-2023 Intel Corporation
--- arch/x86/include/asm/tdx.h | 47 ---------- arch/x86/virt/vmx/tdx/seamcall_internal.h | 109 ++++++++++++++++++++++ arch/x86/virt/vmx/tdx/tdx.c | 47 +--------- 3 files changed, 111 insertions(+), 92 deletions(-) create mode 100644 arch/x86/virt/vmx/tdx/seamcall_internal.h
arch/x86/include/asm/tdx.h +0 -47
@@ -100,54 +100,7 @@ static inline long tdx_kvm_hypercall(unsigned int nr, unsigned long p1,#endif /* CONFIG_INTEL_TDX_GUEST && CONFIG_KVM_GUEST */ #ifdef CONFIG_INTEL_TDX_HOST -u64 __seamcall (u64 fn,struct tdx_module_args *args); -u64 __seamcall_ret (u64 fn,struct tdx_module_args *args); -u64 __seamcall_saved_ret (u64 fn,struct tdx_module_args *args);void tdx_init (void ); - -#include <linux/preempt.h> -#include <asm/archrandom.h> -#include <asm/processor.h> - -typedef u64 (*sc_func_t )(u64 fn,struct tdx_module_args *args); - -static __always_inline u64 __seamcall_dirty_cache(sc_func_t func, u64 fn, -struct tdx_module_args *args) -{ -lockdep_assert_preemption_disabled (); - -/* -* SEAMCALLs are made to the TDX module and can generate dirty -* cachelines of TDX private memory. Mark cache state incoherent -* so that the cache can be flushed during kexec. -* -* This needs to be done before actually making the SEAMCALL, -* because kexec-ing CPU could send NMI to stop remote CPUs, -* in which case even disabling IRQ won't help here. -*/ -this_cpu_write (cache_state_incoherent, true); - -return func (fn, args); -} - -static __always_inline u64 sc_retry(sc_func_t func, u64 fn, -struct tdx_module_args *args) -{ -int retry =RDRAND_RETRY_LOOPS ; -u64 ret; - -do { -preempt_disable (); - ret =__seamcall_dirty_cache (func, fn, args); -preempt_enable (); - }while (ret ==TDX_RND_NO_ENTROPY && --retry); - -return ret; -} - -#define seamcall (_fn, _args) sc_retry(__seamcall, (_fn), (_args)) -#define seamcall_ret (_fn, _args) sc_retry(__seamcall_ret, (_fn), (_args)) -#define seamcall_saved_ret (_fn, _args) sc_retry(__seamcall_saved_ret, (_fn), (_args))const char *tdx_dump_mce_info (struct mce *m);const struct tdx_sys_info *tdx_get_sysinfo (void );
arch/x86/virt/vmx/tdx/seamcall_internal.h new file +109 -0
109 lines not on this page · raw message
arch/x86/virt/vmx/tdx/tdx.c +2 -45
59 lines not on this page · raw message
Signature · 1 line
2.52.0
[tip: x86/tdx] x86/virt/tdx: Move low level SEAMCALL helpers out of <asm/tdx.h>
The following commit has been merged into the x86/tdx branch of tip:
The following commit has been merged into the x86/tdx branch of tip:
Commit-ID: 2e41297bfa035fa1cf1530d5ceecf58c527b6277 Gitweb: https://git.kernel.org/tip/2e41297bfa035fa1cf1530d5ceecf58c527b6277 Author: Kai Huang <[email protected]> AuthorDate: Wed, 20 May 2026 15:28:51 -07:00 Committer: Dave Hansen <[email protected]> CommitterDate: Wed, 03 Jun 2026 08:14:51 -07:00
x86/virt/tdx: Move low level SEAMCALL helpers out of <asm/tdx.h>
TDX host core code implements three seamcall*() helpers to make SEAMCALLs to the TDX module. Currently, they are implemented in <asm/tdx.h> and are exposed to other kernel code which includes <asm/tdx.h>.
However, other than the TDX host core, seamcall*() are not expected to be used by other kernel code directly. For instance, for all SEAMCALLs that are used by KVM, the TDX host core exports a wrapper function for each of them.
Move seamcall*() and related code out of <asm/tdx.h> and make them only visible to TDX host core.
Since TDX host core tdx.c is already very heavy, don't put low level seamcall*() code there but to a new dedicated "seamcall_internal.h". Also, currently tdx.c has seamcall_prerr*() helpers which additionally print error message when calling seamcall*() fails. Move them to "seamcall_internal.h" as well. In such way all low level SEAMCALL helpers are in a dedicated place, which is much more readable.
Copy the copyright notice from the original files and consolidate the date ranges to:
Copyright (C) 2021-2023 Intel Corporation
--- arch/x86/include/asm/tdx.h | 47 +--------- arch/x86/virt/vmx/tdx/seamcall_internal.h | 109 +++++++++++++++++++++- arch/x86/virt/vmx/tdx/tdx.c | 47 +--------- 3 files changed, 111 insertions(+), 92 deletions(-) create mode 100644 arch/x86/virt/vmx/tdx/seamcall_internal.h
arch/x86/include/asm/tdx.h +0 -47
@@ -100,54 +100,7 @@ static inline long tdx_kvm_hypercall(unsigned int nr, unsigned long p1,#endif /* CONFIG_INTEL_TDX_GUEST && CONFIG_KVM_GUEST */ #ifdef CONFIG_INTEL_TDX_HOST -u64 __seamcall (u64 fn,struct tdx_module_args *args); -u64 __seamcall_ret (u64 fn,struct tdx_module_args *args); -u64 __seamcall_saved_ret (u64 fn,struct tdx_module_args *args);void tdx_init (void ); - -#include <linux/preempt.h> -#include <asm/archrandom.h> -#include <asm/processor.h> - -typedef u64 (*sc_func_t )(u64 fn,struct tdx_module_args *args); - -static __always_inline u64 __seamcall_dirty_cache(sc_func_t func, u64 fn, -struct tdx_module_args *args) -{ -lockdep_assert_preemption_disabled (); - -/* -* SEAMCALLs are made to the TDX module and can generate dirty -* cachelines of TDX private memory. Mark cache state incoherent -* so that the cache can be flushed during kexec. -* -* This needs to be done before actually making the SEAMCALL, -* because kexec-ing CPU could send NMI to stop remote CPUs, -* in which case even disabling IRQ won't help here. -*/ -this_cpu_write (cache_state_incoherent, true); - -return func (fn, args); -} - -static __always_inline u64 sc_retry(sc_func_t func, u64 fn, -struct tdx_module_args *args) -{ -int retry =RDRAND_RETRY_LOOPS ; -u64 ret; - -do { -preempt_disable (); - ret =__seamcall_dirty_cache (func, fn, args); -preempt_enable (); - }while (ret ==TDX_RND_NO_ENTROPY && --retry); - -return ret; -} - -#define seamcall (_fn, _args) sc_retry(__seamcall, (_fn), (_args)) -#define seamcall_ret (_fn, _args) sc_retry(__seamcall_ret, (_fn), (_args)) -#define seamcall_saved_ret (_fn, _args) sc_retry(__seamcall_saved_ret, (_fn), (_args))const char *tdx_dump_mce_info (struct mce *m);const struct tdx_sys_info *tdx_get_sysinfo (void );
arch/x86/virt/vmx/tdx/seamcall_internal.h new file +109 -0
109 lines not on this page · raw message
arch/x86/virt/vmx/tdx/tdx.c +2 -45
59 lines not on this page · raw message
PATCH v10 6/25 coco/tdx-host: Introduce a "tdx_host" device
In reply to Chao GaoTDX depends on a platform firmware module that is invoked via instructions similar to vmenter (i.e. enter into a new privileged "root-mode" context to manage private memory and private device mechanisms). It is a software construct that depends on the CPU vmxon state to enable invocation of TDX module ABIs. Unlike other Trusted Execution Environment (TEE) platform implementations that employ a firmware module running on a PCI device with an MMIO mailbox for communication, TDX has no hardware device to point to as the TEE Secure Manager (TSM).
Create a virtual device not only to align with other implementations but also to make it easier to
- expose metadata (e.g., TDX module version, seamldr version etc) to the userspace as device attributes
- implement firmware uploader APIs which are tied to a device. This is needed to support TDX module runtime updates
- enable TDX Connect which will share a common infrastructure with other platform implementations. In the TDX Connect context, every architecture has a TSM, represented by a PCIe or virtual device. The new "tdx_host" device will serve the TSM role.
A faux device is used for TDX because the TDX module is singular within the system and lacks associated platform resources. Using a faux device eliminates the need to create a stub bus.
The call to tdx_get_sysinfo() ensures that the TDX module is ready to provide services.
Note that AMD has a PCI device for the PSP for SEV and ARM CCA will likely have a faux device [1].
Thanks to Dan and Yilun for all the help on this one.
---
v10:
- Drop Kconfig prompt [Dave]
--- arch/x86/virt/vmx/tdx/tdx.c | 2 +- drivers/virt/coco/Kconfig | 2 ++ drivers/virt/coco/Makefile | 1 + drivers/virt/coco/tdx-host/Kconfig | 4 +++ drivers/virt/coco/tdx-host/Makefile | 1 + drivers/virt/coco/tdx-host/tdx-host.c | 43 +++++++++++++++++++++++++++ 6 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 drivers/virt/coco/tdx-host/Kconfig create mode 100644 drivers/virt/coco/tdx-host/Makefile create mode 100644 drivers/virt/coco/tdx-host/tdx-host.c
arch/x86/virt/vmx/tdx/tdx.c +1 -1
@@ -1527,7 +1527,7 @@ const struct tdx_sys_info *tdx_get_sysinfo(void)return (const struct tdx_sys_info *)&tdx_sysinfo; } -EXPORT_SYMBOL_FOR_KVM (tdx_get_sysinfo); +EXPORT_SYMBOL_FOR_MODULES (tdx_get_sysinfo,"kvm-intel,tdx-host" );u32 tdx_get_nr_guest_keyids (void ) {
drivers/virt/coco/Kconfig +2 -0
@@ -17,5 +17,7 @@ source "drivers/virt/coco/arm-cca-guest/Kconfig" source "drivers/virt/coco/guest/Kconfig" endif +source "drivers/virt/coco/tdx-host/Kconfig" + config TSM bool
drivers/virt/coco/Makefile +1 -0
@@ -6,6 +6,7 @@ obj-$(CONFIG_EFI_SECRET) += efi_secret/ obj-$(CONFIG_ARM_PKVM_GUEST) += pkvm-guest/ obj-$(CONFIG_SEV_GUEST) += sev-guest/ obj-$(CONFIG_INTEL_TDX_GUEST) += tdx-guest/ +obj-$(CONFIG_INTEL_TDX_HOST) += tdx-host/ obj-$(CONFIG_ARM_CCA_GUEST) += arm-cca-guest/ obj-$(CONFIG_TSM) += tsm-core.o obj-$(CONFIG_TSM_GUEST) += guest/
drivers/virt/coco/tdx-host/Kconfig new file +4 -0
@@ -0,0 +1,4 @@ +config TDX_HOST_SERVICES + tristate + depends on INTEL_TDX_HOST + default m
drivers/virt/coco/tdx-host/Makefile new file +1 -0
@@ -0,0 +1 @@ +obj-$(CONFIG_TDX_HOST_SERVICES) += tdx-host.o
drivers/virt/coco/tdx-host/tdx-host.c new file +43 -0
43 lines not on this page · raw message
Signature · 1 line
2.52.0
[tip: x86/tdx] coco/tdx-host: Introduce a "tdx_host" device
The following commit has been merged into the x86/tdx branch of tip:
The following commit has been merged into the x86/tdx branch of tip:
Commit-ID: 59783353467958517a3e511394d7ab3aed03bc6a Gitweb: https://git.kernel.org/tip/59783353467958517a3e511394d7ab3aed03bc6a Author: Chao Gao <[email protected]> AuthorDate: Wed, 20 May 2026 15:28:52 -07:00 Committer: Dave Hansen <[email protected]> CommitterDate: Wed, 03 Jun 2026 08:14:51 -07:00
coco/tdx-host: Introduce a "tdx_host" device
TDX depends on a platform firmware module that runs on the CPU. Unlike other CoCo architectures, TDX has no hardware "device" running the show, just a blob on the CPU.
Create a virtual device to anchor interactions with this platform firmware. This lets later code:
- expose metadata: TDX module version, seamldr version, to userspace as device attributes
- implement firmware uploader APIs (which are tied to a device) to support TDX module runtime updates
Use a faux device because the TDX module is singular within the system and has no platform resources. Using a faux device eliminates the need to create a stub bus.
The call to tdx_get_sysinfo() ensures that the TDX module is ready to provide services.
Note that AMD has a PCI device for the PSP for SEV and ARM CCA will likely have a faux device [1].
Thanks to Dan and Yilun for all the help on this one.
[ dhansen: trim changelog ]
--- arch/x86/virt/vmx/tdx/tdx.c | 2 +- drivers/virt/coco/Kconfig | 2 +- drivers/virt/coco/Makefile | 1 +- drivers/virt/coco/tdx-host/Kconfig | 4 ++- drivers/virt/coco/tdx-host/Makefile | 1 +- drivers/virt/coco/tdx-host/tdx-host.c | 43 ++++++++++++++++++++++++++- 6 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 drivers/virt/coco/tdx-host/Kconfig create mode 100644 drivers/virt/coco/tdx-host/Makefile create mode 100644 drivers/virt/coco/tdx-host/tdx-host.c
arch/x86/virt/vmx/tdx/tdx.c +1 -1
@@ -1527,7 +1527,7 @@ const struct tdx_sys_info *tdx_get_sysinfo(void)return (const struct tdx_sys_info *)&tdx_sysinfo; } -EXPORT_SYMBOL_FOR_KVM (tdx_get_sysinfo); +EXPORT_SYMBOL_FOR_MODULES (tdx_get_sysinfo,"kvm-intel,tdx-host" );u32 tdx_get_nr_guest_keyids (void ) {
drivers/virt/coco/Kconfig +2 -0
@@ -17,5 +17,7 @@ source "drivers/virt/coco/arm-cca-guest/Kconfig" source "drivers/virt/coco/guest/Kconfig" endif +source "drivers/virt/coco/tdx-host/Kconfig" + config TSM bool
drivers/virt/coco/Makefile +1 -0
@@ -6,6 +6,7 @@ obj-$(CONFIG_EFI_SECRET) += efi_secret/ obj-$(CONFIG_ARM_PKVM_GUEST) += pkvm-guest/ obj-$(CONFIG_SEV_GUEST) += sev-guest/ obj-$(CONFIG_INTEL_TDX_GUEST) += tdx-guest/ +obj-$(CONFIG_INTEL_TDX_HOST) += tdx-host/ obj-$(CONFIG_ARM_CCA_GUEST) += arm-cca-guest/ obj-$(CONFIG_TSM) += tsm-core.o obj-$(CONFIG_TSM_GUEST) += guest/
drivers/virt/coco/tdx-host/Kconfig new file +4 -0
@@ -0,0 +1,4 @@ +config TDX_HOST_SERVICES + tristate + depends on INTEL_TDX_HOST + default m
drivers/virt/coco/tdx-host/Makefile new file +1 -0
@@ -0,0 +1 @@ +obj-$(CONFIG_TDX_HOST_SERVICES) += tdx-host.o
drivers/virt/coco/tdx-host/tdx-host.c new file +43 -0
43 lines not on this page · raw message
PATCH v10 7/25 coco/tdx-host: Expose TDX module version
In reply to Chao GaoFor TDX module updates, userspace needs to select compatible update versions based on the current module version. This design delegates module selection complexity to userspace because compatibility must satisfy constraints from both the CPU and the running TDX module version.
For example, the 1.5.x series runs on Sapphire Rapids but not Granite Rapids, which needs 2.0.x. Updates are also constrained by version distance, so a 1.5.6 module might permit updates to 1.5.7 but not to 1.5.20.
Expose the TDX module version to userspace via sysfs to aid module selection. Since the TDX faux device will drive module updates, expose the version as its attribute.
Define TDX_VERSION_FMT macro for the TDX version format since it will be used multiple times. Also convert an existing print statement to use it.
== Background ==
The "faux device + device attribute" approach compares to other update mechanisms as follows:
1. AMD SEV leverages an existing PCI device for the PSP to expose metadata. TDX uses a faux device as it doesn't have PCI device in its architecture.
2. Microcode uses per-CPU virtual devices to report microcode revisions because CPUs can have different revisions. But, there is only a single TDX module, so exposing the TDX module version through a global TDX faux device is appropriate
3. ARM's CCA implementation isn't in-tree yet, but will likely follow a similar faux device approach, though it's unclear whether they need to expose firmware version information
---
v10:
- Improve clarity of the changelog and ABI documentation [Dave]
--- .../ABI/testing/sysfs-devices-faux-tdx-host | 5 ++++ arch/x86/include/asm/tdx.h | 6 +++++ arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 2 +- drivers/virt/coco/tdx-host/tdx-host.c | 26 ++++++++++++++++++- 4 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 Documentation/ABI/testing/sysfs-devices-faux-tdx-host
Documentation/ABI/testing/sysfs-devices-faux-tdx-host new file +5 -0
@@ -0,0 +1,5 @@ +What: /sys/devices/faux/tdx_host/version +Contact: [email protected] +Description: (RO) Report the version of the loaded TDX module. + Formatted as "major.minor.update". Used by TDX module + update tooling. Example: "1.2.03".
arch/x86/include/asm/tdx.h +6 -0
@@ -41,6 +41,12 @@#include <asm/tdx_global_metadata.h> #include <linux/pgtable.h> +/* +* TDX module and P-SEAMLDR version convention: "major.minor.update" +* (e.g., "1.5.08") with zero-padded two-digit update field. +*/ +#define TDX_VERSION_FMT "%u.%u.%02u" +/* * Used by the #VE exception handler to gather the #VE exception * info from the TDX module. This is a software only structure
arch/x86/virt/vmx/tdx/tdx_global_metadata.c +1 -1
@@ -106,7 +106,7 @@ static __init int get_tdx_sys_info(struct tdx_sys_info *sysinfo) ret = ret ?:get_tdx_sys_info_version (&sysinfo->version); -pr_info ("Module version: %u.%u.%02u\n" , +pr_info ("Module version: " TDX_VERSION_FMT "\n" , sysinfo->version.major_version, sysinfo->version.minor_version, sysinfo->version.update_version);
drivers/virt/coco/tdx-host/tdx-host.c +25 -1
@@ -8,6 +8,7 @@#include <linux/device/faux.h> #include <linux/module.h> #include <linux/mod_devicetable.h> +#include <linux/sysfs.h> #include <asm/cpu_device_id.h> #include <asm/tdx.h>
@@ -18,6 +19,29 @@ static const struct x86_cpu_id tdx_host_ids[] = { };MODULE_DEVICE_TABLE (x86cpu, tdx_host_ids); +static ssize_t version_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +const struct tdx_sys_info *tdx_sysinfo =tdx_get_sysinfo (); +const struct tdx_sys_info_version *ver; + +if (!tdx_sysinfo) +return -ENXIO ; + + ver = &tdx_sysinfo->version; + +return sysfs_emit (buf,TDX_VERSION_FMT "\n" , ver->major_version, + ver->minor_version, + ver->update_version); +} +static DEVICE_ATTR_RO (version); + +static struct attribute *tdx_host_attrs[] = { + &dev_attr_version.attr, +NULL , +}; +ATTRIBUTE_GROUPS (tdx_host); +static struct faux_device *fdev;static int __init tdx_host_init (void )
@@ -25,7 +49,7 @@ static int __init tdx_host_init(void)if (!x86_match_cpu (tdx_host_ids) || !tdx_get_sysinfo ())return -ENODEV ; - fdev =faux_device_create (KBUILD_MODNAME ,NULL ,NULL ); + fdev =faux_device_create_with_groups (KBUILD_MODNAME ,NULL ,NULL , tdx_host_groups);if (!fdev)return -ENODEV ;
Signature · 1 line
2.52.0
[tip: x86/tdx] coco/tdx-host: Expose TDX module version
The following commit has been merged into the x86/tdx branch of tip:
The following commit has been merged into the x86/tdx branch of tip:
Commit-ID: 6fe8a33fdb93911934cd1c77624c1e02af45047c Gitweb: https://git.kernel.org/tip/6fe8a33fdb93911934cd1c77624c1e02af45047c Author: Chao Gao <[email protected]> AuthorDate: Wed, 20 May 2026 15:28:53 -07:00 Committer: Dave Hansen <[email protected]> CommitterDate: Wed, 03 Jun 2026 08:14:51 -07:00
coco/tdx-host: Expose TDX module version
For TDX module updates, userspace needs to select compatible update versions based on the current module version.
For example, the 1.5.x series runs on Sapphire Rapids but not Granite Rapids, which needs 2.0.x. Updates are also constrained by version distance, so a 1.5.6 module might permit updates to 1.5.7 but not to 1.5.20.
Start the process of punting the version selection logic to userspace. Expose the TDX module version in the new faux device.
Define TDX_VERSION_FMT macro for the TDX version format since it will be used multiple times. Also convert an existing print statement to use it.
== Background ==
For posterity, here's what other firmware mechanisms do:
1. AMD SEV leverages an existing PCI device for the PSP to expose metadata. TDX uses a faux device as it doesn't have PCI device in its architecture.
2. Microcode uses per-CPU virtual devices to report microcode revisions because CPUs can have different revisions. But, there is only a single TDX module, so exposing the TDX module version through a global TDX faux device is appropriate
3. ARM's CCA implementation isn't in-tree yet, but will likely follow a similar faux device approach, though it's unclear whether they need to expose firmware version information
[ dhansen: trim changelog ]
--- Documentation/ABI/testing/sysfs-devices-faux-tdx-host | 5 ++- arch/x86/include/asm/tdx.h | 6 ++- arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 2 +- drivers/virt/coco/tdx-host/tdx-host.c | 26 +++++++++- 4 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 Documentation/ABI/testing/sysfs-devices-faux-tdx-host
Documentation/ABI/testing/sysfs-devices-faux-tdx-host new file +5 -0
@@ -0,0 +1,5 @@ +What: /sys/devices/faux/tdx_host/version +Contact: [email protected] +Description: (RO) Report the version of the loaded TDX module. + Formatted as "major.minor.update". Used by TDX module + update tooling. Example: "1.2.03".
arch/x86/include/asm/tdx.h +6 -0
@@ -42,6 +42,12 @@#include <linux/pgtable.h> /* +* TDX module and P-SEAMLDR version convention: "major.minor.update" +* (e.g., "1.5.08") with zero-padded two-digit update field. +*/ +#define TDX_VERSION_FMT "%u.%u.%02u" + +/* * Used by the #VE exception handler to gather the #VE exception * info from the TDX module. This is a software only structure * and not part of the TDX module/VMM ABI.
arch/x86/virt/vmx/tdx/tdx_global_metadata.c +1 -1
@@ -106,7 +106,7 @@ static __init int get_tdx_sys_info(struct tdx_sys_info *sysinfo) ret = ret ?:get_tdx_sys_info_version (&sysinfo->version); -pr_info ("Module version: %u.%u.%02u\n" , +pr_info ("Module version: " TDX_VERSION_FMT "\n" , sysinfo->version.major_version, sysinfo->version.minor_version, sysinfo->version.update_version);
drivers/virt/coco/tdx-host/tdx-host.c +25 -1
@@ -8,6 +8,7 @@#include <linux/device/faux.h> #include <linux/module.h> #include <linux/mod_devicetable.h> +#include <linux/sysfs.h> #include <asm/cpu_device_id.h> #include <asm/tdx.h>
@@ -18,6 +19,29 @@ static const struct x86_cpu_id tdx_host_ids[] = { };MODULE_DEVICE_TABLE (x86cpu, tdx_host_ids); +static ssize_t version_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +const struct tdx_sys_info *tdx_sysinfo =tdx_get_sysinfo (); +const struct tdx_sys_info_version *ver; + +if (!tdx_sysinfo) +return -ENXIO ; + + ver = &tdx_sysinfo->version; + +return sysfs_emit (buf,TDX_VERSION_FMT "\n" , ver->major_version, + ver->minor_version, + ver->update_version); +} +static DEVICE_ATTR_RO (version); + +static struct attribute *tdx_host_attrs[] = { + &dev_attr_version.attr, +NULL , +}; +ATTRIBUTE_GROUPS (tdx_host); +static struct faux_device *fdev;static int __init tdx_host_init (void )
@@ -25,7 +49,7 @@ static int __init tdx_host_init(void)if (!x86_match_cpu (tdx_host_ids) || !tdx_get_sysinfo ())return -ENODEV ; - fdev =faux_device_create (KBUILD_MODNAME ,NULL ,NULL ); + fdev =faux_device_create_with_groups (KBUILD_MODNAME ,NULL ,NULL , tdx_host_groups);if (!fdev)return -ENODEV ;
PATCH v10 8/25 x86/virt/seamldr: Introduce a wrapper for P-SEAMLDR SEAMCALLs
In reply to Chao GaoThe TDX architecture uses the "SEAMCALL" instruction to communicate with SEAM mode software. Right now, the only SEAM mode software that the kernel communicates with is the TDX module. But, there is actually another component that runs in SEAM mode but it is separate from the TDX module:
the persistent SEAM loader or "P-SEAMLDR". Right now, the only component that communicates with it is the BIOS which loads the TDX module itself at boot. But, to support updating the TDX module, the kernel now needs to be able to talk to it.
P-SEAMLDR SEAMCALLs differ from TDX module SEAMCALLs in areas such as concurrency requirements.
Add a P-SEAMLDR wrapper to handle these differences and prepare for implementing concrete functions.
Use seamcall_prerr() (not '_ret') because current P-SEAMLDR calls do not use any output registers other than RAX.
Note: Despite the similar name, the NP-SEAMLDR ("Non-Persistent") differs sharply from the P-SEAMLDR. It is an authenticated code module (ACM) invoked exclusively by the BIOS at boot rather than a component running in SEAM mode. The kernel cannot call it at runtime. It exposes no SEAMCALL interface.
---
v10:
- make "main act" solution statement in a prominent place [Dave]
--- arch/x86/virt/vmx/tdx/Makefile | 2 +- arch/x86/virt/vmx/tdx/seamldr.c | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 arch/x86/virt/vmx/tdx/seamldr.c
arch/x86/virt/vmx/tdx/Makefile +1 -1
@@ -1,2 +1,2 @@ # SPDX-License-Identifier: GPL-2.0-only -obj-y += seamcall.o tdx.o +obj-y += seamcall.o seamldr.o tdx.o
arch/x86/virt/vmx/tdx/seamldr.c new file +25 -0
@@ -0,0 +1,25 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* P-SEAMLDR support for TDX module management features like runtime updates +* +* Copyright (C) 2025 Intel Corporation +*/ +#define pr_fmt (fmt) "seamldr: " fmt + +#include <linux/spinlock.h> + +#include "seamcall_internal.h" + +/* +* Serialize P-SEAMLDR calls since the hardware only allows a single CPU to +* interact with P-SEAMLDR simultaneously. Use raw version as the calls can +* be made with interrupts disabled, where plain spinlocks are prohibited in +* PREEMPT_RT kernels as they become sleeping locks. +*/ +static DEFINE_RAW_SPINLOCK (seamldr_lock); + +static __maybe_unused int seamldr_call (u64 fn,struct tdx_module_args *args) +{ +guard (raw_spinlock)(&seamldr_lock); +return seamcall_prerr (fn, args); +}
Signature · 1 line
2.52.0
[tip: x86/tdx] x86/virt/seamldr: Introduce a wrapper for P-SEAMLDR SEAMCALLs
The following commit has been merged into the x86/tdx branch of tip:
The following commit has been merged into the x86/tdx branch of tip:
Commit-ID: 9bc3ce2c5d3ddcbf569e573c1c65775c09a4fb75 Gitweb: https://git.kernel.org/tip/9bc3ce2c5d3ddcbf569e573c1c65775c09a4fb75 Author: Chao Gao <[email protected]> AuthorDate: Wed, 20 May 2026 15:28:55 -07:00 Committer: Dave Hansen <[email protected]> CommitterDate: Wed, 03 Jun 2026 08:14:51 -07:00
x86/virt/seamldr: Introduce a wrapper for P-SEAMLDR SEAMCALLs
The TDX architecture uses the "SEAMCALL" instruction to communicate with SEAM mode software. Right now, the only SEAM mode software that the kernel communicates with is the TDX module. But, there is actually another component that runs in SEAM mode but it is separate from the TDX module:
the persistent SEAM loader or "P-SEAMLDR". Right now, the only component that communicates with it is the BIOS which loads the TDX module itself at boot. But, to support updating the TDX module, the kernel now needs to be able to talk to it.
P-SEAMLDR SEAMCALLs differ from TDX module SEAMCALLs in areas such as concurrency requirements.
Add a P-SEAMLDR wrapper to handle these differences and prepare for implementing concrete functions.
Use seamcall_prerr() (not '_ret') because current P-SEAMLDR calls do not use any output registers other than RAX.
Note: Despite the similar name, the NP-SEAMLDR ("Non-Persistent") (ACM) invoked exclusively by the BIOS at boot rather than a component running in SEAM mode. The kernel cannot call it at runtime. It exposes no SEAMCALL interface.
--- arch/x86/virt/vmx/tdx/Makefile | 2 +- arch/x86/virt/vmx/tdx/seamldr.c | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 arch/x86/virt/vmx/tdx/seamldr.c
arch/x86/virt/vmx/tdx/Makefile +1 -1
@@ -1,2 +1,2 @@ # SPDX-License-Identifier: GPL-2.0-only -obj-y += seamcall.o tdx.o +obj-y += seamcall.o seamldr.o tdx.o
arch/x86/virt/vmx/tdx/seamldr.c new file +25 -0
@@ -0,0 +1,25 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* P-SEAMLDR support for TDX module management features like runtime updates +* +* Copyright (C) 2025 Intel Corporation +*/ +#define pr_fmt (fmt) "seamldr: " fmt + +#include <linux/spinlock.h> + +#include "seamcall_internal.h" + +/* +* Serialize P-SEAMLDR calls since the hardware only allows a single CPU to +* interact with P-SEAMLDR simultaneously. Use raw version as the calls can +* be made with interrupts disabled, where plain spinlocks are prohibited in +* PREEMPT_RT kernels as they become sleeping locks. +*/ +static DEFINE_RAW_SPINLOCK (seamldr_lock); + +static __maybe_unused int seamldr_call (u64 fn,struct tdx_module_args *args) +{ +guard (raw_spinlock)(&seamldr_lock); +return seamcall_prerr (fn, args); +}
PATCH v10 9/25 x86/virt/seamldr: Add a helper to retrieve P-SEAMLDR information
In reply to Chao GaoP-SEAMLDR reports its state via SEAMLDR.INFO, including its version and the number of remaining runtime updates.
This information is useful for userspace. For example, the admin can use the P-SEAMLDR version to determine whether a candidate TDX module is compatible with the running loader, and can use the remaining update count to determine whether another runtime update is still possible.
Add a helper to retrieve P-SEAMLDR information in preparation for exposing P-SEAMLDR version and other necessary information to userspace. Export the new kAPI for use by the "tdx_host" device.
Note that there are two distinct P-SEAMLDR APIs with similar names:
"SEAMLDR.INFO" is metadata about the loader. It's metadata for the update process.
"SEAMLDR.SEAMINFO" is metadata about SEAM mode. It is for the module init process, not for the update process.
Use SEAMLDR.INFO here.
For details, see "Intel® Trust Domain Extensions - SEAM Loader (SEAMLDR) Interface Specification".
---
v10:
- Shorten the comment above the SEAMLDR_INFO structure [Dave]
- Use tdx_host consistently when referring to the device [Dave]
- Shorten the changelog description about SEAMLDR.{INFO,SEAMINFO} [Dave]
--- arch/x86/include/asm/seamldr.h | 35 +++++++++++++++++++++++++++++++++ arch/x86/virt/vmx/tdx/seamldr.c | 20 ++++++++++++++++++- 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 arch/x86/include/asm/seamldr.h
arch/x86/include/asm/seamldr.h new file +35 -0
@@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_X86_SEAMLDR_H +#define _ASM_X86_SEAMLDR_H + +#include <linux/types.h> + +/* +* This is the "SEAMLDR_INFO" data structure defined in the +* "SEAM Loader (SEAMLDR) Interface Specification". +* +* Must be aligned to a 256-byte boundary. +*/ +struct seamldr_info { +u32 version; +u32 attributes; +u32 vendor_id; +u32 build_date; +u16 build_num; +u16 minor_version; +u16 major_version; +u16 update_version; +u32 acm_x2apicid; +u32 num_remaining_updates; +u8 seam_info[128 ]; +u8 seam_ready; +u8 seam_debug; +u8 p_seam_ready; +u8 reserved[93 ]; +} __packed__aligned(256) ; + +static_assert (sizeof (struct seamldr_info ) ==256 ); + +int seamldr_get_info (struct seamldr_info *seamldr_info); + +#endif /* _ASM_X86_SEAMLDR_H */
arch/x86/virt/vmx/tdx/seamldr.c +19 -1
@@ -8,8 +8,13 @@#include <linux/spinlock.h> +#include <asm/seamldr.h> +#include "seamcall_internal.h" +/* P-SEAMLDR SEAMCALL leaf function */ +#define P_SEAMLDR_INFO 0x8000000000000000 +/* * Serialize P-SEAMLDR calls since the hardware only allows a single CPU to * interact with P-SEAMLDR simultaneously. Use raw version as the calls can
@@ -18,8 +23,21 @@*/ static DEFINE_RAW_SPINLOCK (seamldr_lock); -static __maybe_unused int seamldr_call (u64 fn,struct tdx_module_args *args) +static int seamldr_call (u64 fn,struct tdx_module_args *args) {guard (raw_spinlock)(&seamldr_lock);return seamcall_prerr (fn, args); } + +int seamldr_get_info (struct seamldr_info *seamldr_info) +{ +struct tdx_module_args args = {}; + +/* +* Use slow_virt_to_phys() since @seamldr_info may be allocated on +* the stack. +*/ + args.rcx =slow_virt_to_phys (seamldr_info); +return seamldr_call (P_SEAMLDR_INFO , &args); +} +EXPORT_SYMBOL_FOR_MODULES (seamldr_get_info,"tdx-host" );
Signature · 1 line
2.52.0
[tip: x86/tdx] x86/virt/seamldr: Add a helper to retrieve P-SEAMLDR information
The following commit has been merged into the x86/tdx branch of tip:
The following commit has been merged into the x86/tdx branch of tip:
Commit-ID: 0988bf698a80056638f771179158059c60874ab4 Gitweb: https://git.kernel.org/tip/0988bf698a80056638f771179158059c60874ab4 Author: Chao Gao <[email protected]> AuthorDate: Wed, 20 May 2026 15:28:56 -07:00 Committer: Dave Hansen <[email protected]> CommitterDate: Wed, 03 Jun 2026 08:14:51 -07:00
x86/virt/seamldr: Add a helper to retrieve P-SEAMLDR information
P-SEAMLDR reports its state via SEAMLDR.INFO, including its version and the number of remaining runtime updates.
This information is useful for userspace. For example, userspace can use the P-SEAMLDR version to determine whether a candidate TDX module is compatible with the running loader, and can use the remaining update count to determine whether another runtime update is still possible.
Add a helper to retrieve P-SEAMLDR information in preparation for exposing P-SEAMLDR version and other necessary information to userspace. Export the new kAPI for use by the "tdx_host" device.
Note that there are two distinct P-SEAMLDR APIs with similar names:
"SEAMLDR.INFO" is metadata about the loader. It's metadata for the update process.
"SEAMLDR.SEAMINFO" is metadata about SEAM mode. It is for the module init process, not for the update process.
Use SEAMLDR.INFO here.
For details, see "Intel Trust Domain Extensions - SEAM Loader (SEAMLDR) Interface Specification".
--- arch/x86/include/asm/seamldr.h | 35 ++++++++++++++++++++++++++++++++- arch/x86/virt/vmx/tdx/seamldr.c | 20 +++++++++++++++++- 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 arch/x86/include/asm/seamldr.h
arch/x86/include/asm/seamldr.h new file +35 -0
@@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_X86_SEAMLDR_H +#define _ASM_X86_SEAMLDR_H + +#include <linux/types.h> + +/* +* This is the "SEAMLDR_INFO" data structure defined in the +* "SEAM Loader (SEAMLDR) Interface Specification". +* +* Must be aligned to a 256-byte boundary. +*/ +struct seamldr_info { +u32 version; +u32 attributes; +u32 vendor_id; +u32 build_date; +u16 build_num; +u16 minor_version; +u16 major_version; +u16 update_version; +u32 acm_x2apicid; +u32 num_remaining_updates; +u8 seam_info[128 ]; +u8 seam_ready; +u8 seam_debug; +u8 p_seam_ready; +u8 reserved[93 ]; +} __packed__aligned(256) ; + +static_assert (sizeof (struct seamldr_info ) ==256 ); + +int seamldr_get_info (struct seamldr_info *seamldr_info); + +#endif /* _ASM_X86_SEAMLDR_H */
arch/x86/virt/vmx/tdx/seamldr.c +19 -1
@@ -8,8 +8,13 @@#include <linux/spinlock.h> +#include <asm/seamldr.h> +#include "seamcall_internal.h" +/* P-SEAMLDR SEAMCALL leaf function */ +#define P_SEAMLDR_INFO 0x8000000000000000 +/* * Serialize P-SEAMLDR calls since the hardware only allows a single CPU to * interact with P-SEAMLDR simultaneously. Use raw version as the calls can
@@ -18,8 +23,21 @@*/ static DEFINE_RAW_SPINLOCK (seamldr_lock); -static __maybe_unused int seamldr_call (u64 fn,struct tdx_module_args *args) +static int seamldr_call (u64 fn,struct tdx_module_args *args) {guard (raw_spinlock)(&seamldr_lock);return seamcall_prerr (fn, args); } + +int seamldr_get_info (struct seamldr_info *seamldr_info) +{ +struct tdx_module_args args = {}; + +/* +* Use slow_virt_to_phys() since @seamldr_info may be allocated on +* the stack. +*/ + args.rcx =slow_virt_to_phys (seamldr_info); +return seamldr_call (P_SEAMLDR_INFO , &args); +} +EXPORT_SYMBOL_FOR_MODULES (seamldr_get_info,"tdx-host" );
PATCH v10 10/25 coco/tdx-host: Expose P-SEAMLDR information via sysfs
In reply to Chao GaoTDX module updates require userspace to select the appropriate module to load. Expose necessary information to facilitate this decision. Two values are needed:
- P-SEAMLDR version: for compatibility checks between TDX module and P-SEAMLDR
- num_remaining_updates: indicates how many updates can be performed
Expose them as tdx-host device attributes. Make seamldr attributes visible only when the update feature is supported, as that's their sole purpose.
Note that the underlying P-SEAMLDR attributes are available regardless of update support; this only restricts their visibility in Linux.
---
v10:
- Describe the P-SEAMLDR version by referring to the TDX module version instead of copying the text [Dave]
- add a comment for DEVICE_ATTR_ADMIN_RO() [Dave]
- Avoid the tertiary operator [Dave]
--- .../ABI/testing/sysfs-devices-faux-tdx-host | 21 ++++++ arch/x86/include/asm/tdx.h | 6 ++ drivers/virt/coco/tdx-host/tdx-host.c | 72 ++++++++++++++++++- 3 files changed, 98 insertions(+), 1 deletion(-)
Documentation/ABI/testing/sysfs-devices-faux-tdx-host +21 -0
@@ -3,3 +3,24 @@ Contact: [email protected] Description: (RO) Report the version of the loaded TDX module. Formatted as "major.minor.update". Used by TDX module update tooling. Example: "1.2.03". + +What: /sys/devices/faux/tdx_host/seamldr_version +Contact: [email protected] +Description: (RO) Report the version of the loaded P-SEAMLDR. + Formatted as a TDX module version. Used by TDX module + update tooling. + +What: /sys/devices/faux/tdx_host/num_remaining_updates +Contact: [email protected] +Description: (RO) Report the number of remaining updates. TDX maintains a + log about each TDX module that has been loaded. This log has + a finite size, which limits the number of TDX module updates + that can be performed. + + After each successful update, the number reduces by one. Once it + reaches zero, further updates will fail until next reboot. The + number is always zero if the P-SEAMLDR doesn't support updates. + + See Intel® Trust Domain Extensions - SEAM Loader (SEAMLDR) + Interface Specification, Chapter "SEAMLDR_INFO" and Chapter + "SEAMLDR.INSTALL" for more information.
arch/x86/include/asm/tdx.h +6 -0
@@ -110,6 +110,12 @@ void tdx_init(void);const char *tdx_dump_mce_info (struct mce *m);const struct tdx_sys_info *tdx_get_sysinfo (void ); +static inline bool tdx_supports_runtime_update (const struct tdx_sys_info *sysinfo) +{ +/* To be enabled when kernel is ready. */ +return false; +} +int tdx_guest_keyid_alloc (void );u32 tdx_get_nr_guest_keyids (void );void tdx_guest_keyid_free (unsigned int keyid);
drivers/virt/coco/tdx-host/tdx-host.c +71 -1
84 lines not on this page · raw message
Signature · 1 line
2.52.0
[tip: x86/tdx] coco/tdx-host: Expose P-SEAMLDR information via sysfs
The following commit has been merged into the x86/tdx branch of tip:
The following commit has been merged into the x86/tdx branch of tip:
Commit-ID: 885afcf7c859729d0d7884d0abe5343ee8f742a3 Gitweb: https://git.kernel.org/tip/885afcf7c859729d0d7884d0abe5343ee8f742a3 Author: Chao Gao <[email protected]> AuthorDate: Wed, 20 May 2026 15:28:57 -07:00 Committer: Dave Hansen <[email protected]> CommitterDate: Wed, 03 Jun 2026 08:14:51 -07:00
coco/tdx-host: Expose P-SEAMLDR information via sysfs
TDX module updates require userspace to select the appropriate module to load. Expose necessary information to facilitate this decision. Two values are needed:
- P-SEAMLDR version: for compatibility checks between TDX module and P-SEAMLDR
- num_remaining_updates: indicates how many updates can be performed
Expose them as tdx-host device attributes visible only when updates are supported.
Note that the underlying P-SEAMLDR attributes are available regardless of update support; this only restricts their visibility to userspace.
--- Documentation/ABI/testing/sysfs-devices-faux-tdx-host | 21 +++- arch/x86/include/asm/tdx.h | 6 +- drivers/virt/coco/tdx-host/tdx-host.c | 72 +++++++++- 3 files changed, 98 insertions(+), 1 deletion(-)
Documentation/ABI/testing/sysfs-devices-faux-tdx-host +21 -0
@@ -3,3 +3,24 @@ Contact: [email protected] Description: (RO) Report the version of the loaded TDX module. Formatted as "major.minor.update". Used by TDX module update tooling. Example: "1.2.03". + +What: /sys/devices/faux/tdx_host/seamldr_version +Contact: [email protected] +Description: (RO) Report the version of the loaded P-SEAMLDR. + Formatted as a TDX module version. Used by TDX module + update tooling. + +What: /sys/devices/faux/tdx_host/num_remaining_updates +Contact: [email protected] +Description: (RO) Report the number of remaining updates. TDX maintains a + log about each TDX module that has been loaded. This log has + a finite size, which limits the number of TDX module updates + that can be performed. + + After each successful update, the number reduces by one. Once it + reaches zero, further updates will fail until next reboot. The + number is always zero if the P-SEAMLDR doesn't support updates. + + See Intel Trust Domain Extensions - SEAM Loader (SEAMLDR) + Interface Specification, Chapter "SEAMLDR_INFO" and Chapter + "SEAMLDR.INSTALL" for more information.
arch/x86/include/asm/tdx.h +6 -0
@@ -110,6 +110,12 @@ void tdx_init(void);const char *tdx_dump_mce_info (struct mce *m);const struct tdx_sys_info *tdx_get_sysinfo (void ); +static inline bool tdx_supports_runtime_update (const struct tdx_sys_info *sysinfo) +{ +/* To be enabled when kernel is ready. */ +return false; +} +int tdx_guest_keyid_alloc (void );u32 tdx_get_nr_guest_keyids (void );void tdx_guest_keyid_free (unsigned int keyid);
drivers/virt/coco/tdx-host/tdx-host.c +71 -1
84 lines not on this page · raw message
PATCH v10 11/25 coco/tdx-host: Don't expose P-SEAMLDR information on CPUs with erratum
In reply to Chao GaoTDX-capable CPUs clobber the current VMCS on return from P-SEAMLDR, as documented in Intel® Trust Domain CPU Architectural Extensions:
SEAMRET from the P-SEAMLDR clears the current VMCS structure pointed to by the current-VMCS pointer. A VMM that invokes the P-SEAMLDR using SEAMCALL must reload the current-VMCS, if required, using the VMPTRLD instruction.
Clearing the current VMCS behind KVM's back will break KVM.
Future CPUs will fix this by preserving the current VMCS across P-SEAMLDR calls. A future specification update will describe the SEAMRET VMCS-clearing behavior as an erratum and to state that it does not occur when IA32_VMX_BASIC[60] is set.
Add a CPU bug bit for this erratum and refuse to expose P-SEAMLDR information on affected CPUs, because even reading the P-SEAMLDR sysfs knobs would enter and exit P-SEAMLDR.
Use a CPU bug bit to stay consistent with X86_BUG_TDX_PW_MCE. As a bonus, the bug bit is visible to userspace, which allows userspace to determine why these sysfs files are not exposed, and it can also be checked by other kernel components in the future if needed.
== Alternatives ==
Two workarounds were considered but both were rejected:
1. Save/restore the current VMCS around P-SEAMLDR calls. This produces ugly assembly code [1] and doesn't play well with #MCE or #NMI if they need to use the current VMCS.
2. Move KVM's VMCS tracking logic to the TDX core code, which would break the boundary between KVM and the TDX core code [2].
---
This is split into a separate patch rather than folded into the previous one, because the erratum handling warrants a longer changelog and discussion of the alternatives.
v10:
- Make it clear that clearing VMCS is the current architecture behavior. but will be fixed by a later doc update.
--- arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/vmx.h | 1 + arch/x86/virt/vmx/tdx/tdx.c | 11 +++++++++++ drivers/virt/coco/tdx-host/tdx-host.c | 8 ++++++++ 4 files changed, 21 insertions(+)
arch/x86/include/asm/cpufeatures.h +1 -0
@@ -573,4 +573,5 @@#define X86_BUG_ITS_NATIVE_ONLY X86_BUG( 1*32+ 8)/* "its_native_only" CPU is affected by ITS, VMX is not affected */ #define X86_BUG_TSA X86_BUG( 1*32+ 9)/* "tsa" CPU is affected by Transient Scheduler Attacks */ #define X86_BUG_VMSCAPE X86_BUG( 1*32+10)/* "vmscape" CPU is affected by VMSCAPE attacks from guests */ +#define X86_BUG_SEAMRET_INVD_VMCS X86_BUG( 1*32+11)/* "seamret_invd_vmcs" SEAMRET from P-SEAMLDR clears the current VMCS */ #endif /* _ASM_X86_CPUFEATURES_H */
arch/x86/include/asm/vmx.h +1 -0
@@ -147,6 +147,7 @@ struct vmcs {#define VMX_BASIC_INOUT BIT_ULL(54)#define VMX_BASIC_TRUE_CTLS BIT_ULL(55)#define VMX_BASIC_NO_HW_ERROR_CODE_CC BIT_ULL(56) +#define VMX_BASIC_NO_SEAMRET_INVD_VMCS BIT_ULL(60)static inline u32 vmx_basic_vmcs_revision_id (u64 vmx_basic) {
arch/x86/virt/vmx/tdx/tdx.c +11 -0
@@ -42,6 +42,7 @@#include <asm/processor.h> #include <asm/mce.h> #include <asm/virt.h> +#include <asm/vmx.h> #include "seamcall_internal.h" #include "tdx.h"
@@ -1450,6 +1451,8 @@ static struct notifier_block tdx_memory_nb = {static void __init check_tdx_erratum (void ) { +u64 basic_msr; +/* * These CPUs have an erratum. A partial write from non-TD * software (e.g. via MOVNTI variants or UC/WC mapping) to TDX
@@ -1461,6 +1464,14 @@ static void __init check_tdx_erratum(void)case INTEL_EMERALDRAPIDS_X :setup_force_cpu_bug (X86_BUG_TDX_PW_MCE ); } + +/* +* Some TDX-capable CPUs have an erratum where the current VMCS is +* cleared after calling into P-SEAMLDR. +*/ +rdmsrq (MSR_IA32_VMX_BASIC , basic_msr); +if (!(basic_msr &VMX_BASIC_NO_SEAMRET_INVD_VMCS )) +setup_force_cpu_bug (X86_BUG_SEAMRET_INVD_VMCS ); }void __init tdx_init (void )
drivers/virt/coco/tdx-host/tdx-host.c +8 -0
@@ -98,6 +98,14 @@ static umode_t seamldr_group_visible(struct kobject *kobj, struct attribute *attif (!tdx_supports_runtime_update (sysinfo))return 0 ; +/* +* Calling P-SEAMLDR on CPUs with the seamret_invd_vmcs bug clears +* the current VMCS, which breaks KVM. Verify the erratum is not +* present before exposing P-SEAMLDR features. +*/ +if (boot_cpu_has_bug (X86_BUG_SEAMRET_INVD_VMCS )) +return 0 ; +return attr->mode; }
Signature · 1 line
2.52.0
[tip: x86/tdx] coco/tdx-host: Don't expose P-SEAMLDR information on CPUs with erratum
The following commit has been merged into the x86/tdx branch of tip:
The following commit has been merged into the x86/tdx branch of tip:
Commit-ID: 5ce9cc5a232b992806cf31027e6281727a2040fc Gitweb: https://git.kernel.org/tip/5ce9cc5a232b992806cf31027e6281727a2040fc Author: Chao Gao <[email protected]> AuthorDate: Wed, 20 May 2026 15:28:59 -07:00 Committer: Dave Hansen <[email protected]> CommitterDate: Wed, 03 Jun 2026 08:14:51 -07:00
coco/tdx-host: Don't expose P-SEAMLDR information on CPUs with erratum
TDX-capable CPUs clobber the current VMCS on P-SEAMLDR calls. Clearing the current VMCS behind KVM's back breaks KVM.
Future CPUs will fix this by preserving the current VMCS across P-SEAMLDR calls. A future specification update will describe the VMCS-clearing behavior as an erratum and to state that it does not occur when IA32_VMX_BASIC[60] is set.
Add a CPU bug bit and refuse to expose P-SEAMLDR information on affected CPUs.
Use a CPU bug bit to stay consistent with X86_BUG_TDX_PW_MCE. As a bonus, the bug bit is visible to userspace, which allows userspace to determine why these sysfs files are not exposed, and it can also be checked by other kernel components in the future if needed.
== Alternatives ==
Two workarounds were considered but both were rejected:
1. Save/restore the current VMCS around P-SEAMLDR calls. This produces ugly assembly code [1] and doesn't play well with #MCE or #NMI if they need to use the current VMCS.
2. Move KVM's VMCS tracking logic to the TDX core code, which would break the boundary between KVM and the TDX core code [2].
[ dhansen: comment and changelog munging. Add seamldr_call() bug check. ]
--- arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/vmx.h | 1 + arch/x86/virt/vmx/tdx/seamldr.c | 13 +++++++++++++ arch/x86/virt/vmx/tdx/tdx.c | 11 +++++++++++ drivers/virt/coco/tdx-host/tdx-host.c | 8 ++++++++ 5 files changed, 34 insertions(+)
arch/x86/include/asm/cpufeatures.h +1 -0
@@ -573,4 +573,5 @@#define X86_BUG_ITS_NATIVE_ONLY X86_BUG( 1*32+ 8)/* "its_native_only" CPU is affected by ITS, VMX is not affected */ #define X86_BUG_TSA X86_BUG( 1*32+ 9)/* "tsa" CPU is affected by Transient Scheduler Attacks */ #define X86_BUG_VMSCAPE X86_BUG( 1*32+10)/* "vmscape" CPU is affected by VMSCAPE attacks from guests */ +#define X86_BUG_SEAMRET_INVD_VMCS X86_BUG( 1*32+11)/* "seamret_invd_vmcs" SEAMRET from P-SEAMLDR clears the current VMCS */ #endif /* _ASM_X86_CPUFEATURES_H */
arch/x86/include/asm/vmx.h +1 -0
@@ -147,6 +147,7 @@ struct vmcs {#define VMX_BASIC_INOUT BIT_ULL(54)#define VMX_BASIC_TRUE_CTLS BIT_ULL(55)#define VMX_BASIC_NO_HW_ERROR_CODE_CC BIT_ULL(56) +#define VMX_BASIC_NO_SEAMRET_INVD_VMCS BIT_ULL(60)static inline u32 vmx_basic_vmcs_revision_id (u64 vmx_basic) {
arch/x86/virt/vmx/tdx/seamldr.c +13 -0
@@ -6,8 +6,11 @@*/ #define pr_fmt (fmt) "seamldr: " fmt +#include <linux/bug.h> #include <linux/spinlock.h> +#include <asm/cpufeature.h> +#include <asm/cpufeatures.h> #include <asm/seamldr.h> #include "seamcall_internal.h"
@@ -25,6 +28,16 @@ static DEFINE_RAW_SPINLOCK(seamldr_lock);static int seamldr_call (u64 fn,struct tdx_module_args *args) { +/* +* With this bug, P-SEAMLDR calls corrupt the VMCS +* pointer and must be avoided. This path should be +* unreachable since sysfs hides the ABIs. +*/ +if (boot_cpu_has_bug (X86_BUG_SEAMRET_INVD_VMCS )) { +WARN_ON (1 ); +return -EINVAL ; + } +guard (raw_spinlock)(&seamldr_lock);return seamcall_prerr (fn, args); }
arch/x86/virt/vmx/tdx/tdx.c +11 -0
@@ -42,6 +42,7 @@#include <asm/processor.h> #include <asm/mce.h> #include <asm/virt.h> +#include <asm/vmx.h> #include "seamcall_internal.h" #include "tdx.h"
@@ -1450,6 +1451,8 @@ static struct notifier_block tdx_memory_nb = {static void __init check_tdx_erratum (void ) { +u64 basic_msr; +/* * These CPUs have an erratum. A partial write from non-TD * software (e.g. via MOVNTI variants or UC/WC mapping) to TDX
@@ -1461,6 +1464,14 @@ static void __init check_tdx_erratum(void)case INTEL_EMERALDRAPIDS_X :setup_force_cpu_bug (X86_BUG_TDX_PW_MCE ); } + +/* +* Some TDX-capable CPUs have an erratum where the current VMCS is +* cleared after calling into P-SEAMLDR. +*/ +rdmsrq (MSR_IA32_VMX_BASIC , basic_msr); +if (!(basic_msr &VMX_BASIC_NO_SEAMRET_INVD_VMCS )) +setup_force_cpu_bug (X86_BUG_SEAMRET_INVD_VMCS ); }void __init tdx_init (void )
drivers/virt/coco/tdx-host/tdx-host.c +8 -0
@@ -98,6 +98,14 @@ static umode_t seamldr_group_visible(struct kobject *kobj, struct attribute *attif (!tdx_supports_runtime_update (sysinfo))return 0 ; +/* +* This bug makes P-SEAMLDR calls clobber the current VMCS +* which breaks KVM. Avoid P-SEAMLDR calls by hiding all +* attributes if the CPU has this bug. +*/ +if (boot_cpu_has_bug (X86_BUG_SEAMRET_INVD_VMCS )) +return 0 ; +return attr->mode; }
PATCH v10 12/25 coco/tdx-host: Implement firmware upload sysfs ABI for TDX module updates
In reply to Chao Gaotl;dr: Select fw_upload for doing TDX module updates. The process of selecting among available update images is complicated and nuanced. Punt the selection process out to userspace. One existing userspace implementation today is the script in the Intel TDX Module Binaries repository.
Long Version:
The kernel supports two primary firmware update mechanisms:
1. request_firmware() - used by microcode, SEV firmware, hundreds of other drivers
2. 'struct fw_upload' - used by CXL, FPGA updates, dozens of others
The key difference between is that request_firmware() loads a named file from the filesystem where the filename is kernel-controlled, while fw_upload accepts firmware data directly from userspace.
TDX module firmware update selection policy is too complex for the kernel. Leave it to userspace and use fw_upload.
Why fw_upload instead of request_firmware()?
============================================
Selecting a TDX module update image is not a simple "load the latest" decision. Userspace needs to choose an image that is compatible with both the platform and the currently running module.
Some constraints are hard requirements:
a. Module version series are platform-specific. For example, the 1.5.x
series runs on Sapphire Rapids but not Granite Rapids, which needs 2.0.x.
b. Updates are also constrained by version distance. A 1.5.6 module
might permit updates to 1.5.7 but not to 1.5.50.
There may also be userspace policy choices:
c. Decide the update direction: upgrade or downgrade
d. Choose whether to optimize for fewer updates or smaller version
steps, for example, 1.2.3=>1.2.5 versus 1.2.3=>1.2.4=>1.2.5.
Given that complexity, leave module selection to userspace and use fw_upload.
---
Future patches add more cases to the switch in tdx_fw_write(). With only two cases, the switch looks a bit awkward right now.
v10:
- Polish the changelog [Dave]
- Point to one existing userspace implementation [Dave]
- Separate hard compatibility requirements from userspace policy choices
- Use true/false rather than 0/1 for bool return values.
- s/size/data_len
--- arch/x86/include/asm/seamldr.h | 1 + arch/x86/virt/vmx/tdx/seamldr.c | 14 ++++ drivers/virt/coco/tdx-host/Kconfig | 2 + drivers/virt/coco/tdx-host/tdx-host.c | 92 +++++++++++++++++++++++++-- 4 files changed, 105 insertions(+), 4 deletions(-)
arch/x86/include/asm/seamldr.h +1 -0
@@ -31,5 +31,6 @@ struct seamldr_info {static_assert (sizeof (struct seamldr_info ) ==256 );int seamldr_get_info (struct seamldr_info *seamldr_info); +int seamldr_install_module (const u8 *data,u32 data_len);#endif /* _ASM_X86_SEAMLDR_H */
arch/x86/virt/vmx/tdx/seamldr.c +14 -0
@@ -41,3 +41,17 @@ int seamldr_get_info(struct seamldr_info *seamldr_info)return seamldr_call (P_SEAMLDR_INFO , &args); }EXPORT_SYMBOL_FOR_MODULES (seamldr_get_info,"tdx-host" ); + +/** +* seamldr_install_module - Install a new TDX module. +* @data: Pointer to the TDX module image. +* @data_len: Size of the TDX module image. +* +* Returns 0 on success, negative error code on failure. +*/ +int seamldr_install_module (const u8 *data,u32 data_len) +{ +/* TODO: Update TDX module here */ +return 0 ; +} +EXPORT_SYMBOL_FOR_MODULES (seamldr_install_module,"tdx-host" );
drivers/virt/coco/tdx-host/Kconfig +2 -0
@@ -1,4 +1,6 @@ config TDX_HOST_SERVICES tristate depends on INTEL_TDX_HOST + select FW_LOADER + select FW_UPLOAD default m
drivers/virt/coco/tdx-host/tdx-host.c +88 -4
128 lines not on this page · raw message
Signature · 1 line
2.52.0
[tip: x86/tdx] coco/tdx-host: Implement firmware upload sysfs ABI for TDX module updates
The following commit has been merged into the x86/tdx branch of tip:
The following commit has been merged into the x86/tdx branch of tip:
Commit-ID: c3e70c5ee53f1a5e1df2e83f846185154d58111f Gitweb: https://git.kernel.org/tip/c3e70c5ee53f1a5e1df2e83f846185154d58111f Author: Chao Gao <[email protected]> AuthorDate: Wed, 20 May 2026 15:29:00 -07:00 Committer: Dave Hansen <[email protected]> CommitterDate: Wed, 03 Jun 2026 08:14:51 -07:00
coco/tdx-host: Implement firmware upload sysfs ABI for TDX module updates
tl;dr: Select fw_upload for doing TDX module updates. The process of selecting among available update images is complicated and nuanced. Punt the selection process out to userspace. One existing userspace implementation today is the script in the Intel TDX Module Binaries repository[1].
Long Version:
The kernel supports two primary firmware update mechanisms:
1. request_firmware() - used by microcode, SEV firmware, hundreds of other drivers
2. 'struct fw_upload' - used by CXL, FPGA updates, dozens of others
The key difference between is that request_firmware() loads a named file from the filesystem where the filename is kernel-controlled, while fw_upload accepts firmware data directly from userspace.
TDX module firmware update selection policy is too complex for the kernel. Leave it to userspace and use fw_upload.
Add a skeleton fw_upload implementation to be fleshed out in subsequent patches.
Refactor the sysfs visiblity attribute function so it can be used as a more generic flag for the presence of viable runtime update support.
Why fw_upload instead of request_firmware()?
============================================
Selecting a TDX module update image is not a simple "load the latest" decision. Userspace needs to choose an image that is compatible with both the platform and the currently running module.
Some constraints are hard requirements:
a. Module version series are platform-specific. For example, the 1.5.x
series runs on Sapphire Rapids but not Granite Rapids, which needs 2.0.x.
b. Updates are also constrained by version distance. A 1.5.6 module
might permit updates to 1.5.7 but not to 1.5.50.
There may also be userspace policy choices:
c. Decide the update direction: upgrade or downgrade
d. Choose whether to optimize for fewer updates or smaller version
steps, for example, 1.2.3=>1.2.5 versus 1.2.3=>1.2.4=>1.2.5.
Given that complexity, leave module selection to userspace and use fw_upload.
[ dhansen: add version script link, add more explanation of code moves,
fix some minor whitespace issues ]
--- arch/x86/include/asm/seamldr.h | 1 +- arch/x86/virt/vmx/tdx/seamldr.c | 14 ++++- drivers/virt/coco/tdx-host/Kconfig | 2 +- drivers/virt/coco/tdx-host/tdx-host.c | 93 ++++++++++++++++++++++++-- 4 files changed, 106 insertions(+), 4 deletions(-)
arch/x86/include/asm/seamldr.h +1 -0
@@ -31,5 +31,6 @@ struct seamldr_info {static_assert (sizeof (struct seamldr_info ) ==256 );int seamldr_get_info (struct seamldr_info *seamldr_info); +int seamldr_install_module (const u8 *data,u32 data_len);#endif /* _ASM_X86_SEAMLDR_H */
arch/x86/virt/vmx/tdx/seamldr.c +14 -0
@@ -54,3 +54,17 @@ int seamldr_get_info(struct seamldr_info *seamldr_info)return seamldr_call (P_SEAMLDR_INFO , &args); }EXPORT_SYMBOL_FOR_MODULES (seamldr_get_info,"tdx-host" ); + +/** +* seamldr_install_module - Install a new TDX module. +* @data: Pointer to the TDX module image. +* @data_len: Size of the TDX module image. +* +* Returns 0 on success, negative error code on failure. +*/ +int seamldr_install_module (const u8 *data,u32 data_len) +{ +/* TODO: Update TDX module here */ +return 0 ; +} +EXPORT_SYMBOL_FOR_MODULES (seamldr_install_module,"tdx-host" );
drivers/virt/coco/tdx-host/Kconfig +2 -0
@@ -1,4 +1,6 @@ config TDX_HOST_SERVICES tristate depends on INTEL_TDX_HOST + select FW_LOADER + select FW_UPLOAD default m
drivers/virt/coco/tdx-host/tdx-host.c +89 -4
129 lines not on this page · raw message
PATCH v10 13/25 x86/virt/seamldr: Allocate and populate a module update request
In reply to Chao GaoThere are two important ABIs here:
'struct tdx_image' - The on-disk and in-memory format for a TDX
module update image.
'struct seamldr_params' - The in-memory ABI passed to the TDX module
loader. Points to a single 'struct tdx_image' broken up into 4k pages.
Userspace supplies the update image in struct tdx_image format. The image consists of a header followed by a sigstruct and the module binary. P-SEAMLDR, however, consumes struct seamldr_params rather than the image directly.
Parse the struct tdx_image provided by userspace and populate a matching struct seamldr_params.
The 'tdx_image' ABI is versioned. Two public versions exist today: 0x100 and 0x200. This kernel only accepts 0x200. The older 0x100 format is being deprecated and is intentionally not supported here. Future versions of the module might be able to use the same ABIs (user/kernel and kernel/SEAMLDR) but they will not be able to use this kernel code.
Reject module images without that specific version. This ensures that the kernel is able to understand the passed-in format.
Validate the struct tdx_image header before using it, because the header is consumed solely by the kernel to locate the sigstruct and module within the image. Do not validate the payload itself. The sigstruct and module pages are passed through to P-SEAMLDR, which validates them as part of the update flow.
sigstruct_pages_pa_list currently has only one entry, but it will grow to four pages in the future. Keep it as an array for symmetry with module_pages_pa_list and for extensibility.
---
v10:
- Add comments for init_seamldr_params() and its call site [Dave]
- Use data_len/image_len rather than size [Dave]
- Do bounds check rather than implicitly truncat the update image [Dave]
- Explain why the header version is checked in changelog [Dave]
- make init_seamldr_params() accept a tdx_image pointer [Dave]
- vertical alignment for code has a pattern [Dave]
--- arch/x86/virt/vmx/tdx/seamldr.c | 145 +++++++++++++++++++++++++++++++- 1 file changed, 144 insertions(+), 1 deletion(-)
arch/x86/virt/vmx/tdx/seamldr.c +144 -1
@@ -6,6 +6,8 @@*/ #define pr_fmt (fmt) "seamldr: " fmt +#include <linux/mm.h> +#include <linux/slab.h> #include <linux/spinlock.h> #include <asm/seamldr.h>
@@ -15,6 +17,35 @@/* P-SEAMLDR SEAMCALL leaf function */ #define P_SEAMLDR_INFO 0x8000000000000000 +#define SEAMLDR_MAX_NR_MODULE_PAGES 496 +#define SEAMLDR_MAX_NR_SIG_PAGES 1 + +/* +* The seamldr_params "scenario" field specifies the operation mode: +* 0: Install TDX module from scratch (not used by kernel) +* 1: Update existing TDX module to a compatible version +*/ +#define SEAMLDR_SCENARIO_UPDATE 1 + +/* +* This is the "SEAMLDR_PARAMS" data structure defined in the +* "SEAM Loader (SEAMLDR) Interface Specification". +* +* It is the in-memory ABI that the kernel passes to the P-SEAMLDR +* to update the TDX module. It breaks the TDX module image up in +* page-size pieces. +*/ +struct seamldr_params { +u32 version; +u32 scenario; +u64 sigstruct_pages_pa_list[SEAMLDR_MAX_NR_SIG_PAGES ]; +u8 reserved[104 ]; +u64 module_nr_pages; +u64 module_pages_pa_list[SEAMLDR_MAX_NR_MODULE_PAGES ]; +} __packed; + +static_assert (sizeof (struct seamldr_params ) ==4096 ); +/* * Serialize P-SEAMLDR calls since the hardware only allows a single CPU to * interact with P-SEAMLDR simultaneously. Use raw version as the calls can
@@ -42,6 +73,98 @@ int seamldr_get_info(struct seamldr_info *seamldr_info) }EXPORT_SYMBOL_FOR_MODULES (seamldr_get_info,"tdx-host" ); +#define TDX_IMAGE_VERSION_2 0x200 + +struct tdx_image_header { +u16 version; +u16 checksum; +u8 signature[8 ]; +u32 sigstruct_nr_pages; +u32 module_nr_pages; +u8 reserved[4076 ]; +} __packed; + +#define TDX_IMAGE_HEADER_SIZE sizeof(struct tdx_image_header) +static_assert (TDX_IMAGE_HEADER_SIZE ==4096 ); + +/* +* Intel TDX module update ABI structure. aka. "TDX module blob". +* +* @payload contains sigstruct pages followed by module pages. +*/ +struct tdx_image { +struct tdx_image_header header; +u8 payload[]; +}; + +static void populate_pa_list (u64 *pa_list,const u8 *vmalloc_addr,u32 vmalloc_len_pages) +{ +int i; + +for (i =0 ; i < vmalloc_len_pages; i++) { +unsigned long offset = i *PAGE_SIZE ; +unsigned long pfn =vmalloc_to_pfn (&vmalloc_addr[offset]); + + pa_list[i] = pfn <<PAGE_SHIFT ; + } +} + +static void populate_seamldr_params (struct seamldr_params *params, +const u8 *sig,u32 sig_nr_pages, +const u8 *mod,u32 mod_nr_pages) +{ + params->version =0 ; + params->scenario =SEAMLDR_SCENARIO_UPDATE ; + params->module_nr_pages = mod_nr_pages; + +populate_pa_list (params->sigstruct_pages_pa_list, sig, sig_nr_pages); +populate_pa_list (params->module_pages_pa_list, mod, mod_nr_pages); +} + +/* +* @image points to a vmalloc()'d 'struct tdx_image'. Transform +* it into @params which is the P-SEAMLDR ABI format. +*/ +static int init_seamldr_params (struct seamldr_params *params, +const struct tdx_image *image, +u32 image_len) +{ +const struct tdx_image_header *header = &image->header; + +u32 sigstruct_len = header->sigstruct_nr_pages *PAGE_SIZE ; +u32 module_len = header->module_nr_pages *PAGE_SIZE ; + +u8 *header_start = (u8 *)header; +u8 *header_end = header_start +TDX_IMAGE_HEADER_SIZE ; + +u8 *sigstruct_start = header_end; +u8 *sigstruct_end = sigstruct_start + sigstruct_len; + +u8 *module_start = sigstruct_end; + +/* Check the calculated payload size against the image size. */ +if (TDX_IMAGE_HEADER_SIZE + sigstruct_len + module_len != image_len) +return -EINVAL ; + +/* Reject unsupported tdx_image ABI versions. */ +if (header->version !=TDX_IMAGE_VERSION_2 ) +return -EINVAL ; + +if (header->sigstruct_nr_pages >SEAMLDR_MAX_NR_SIG_PAGES || + header->module_nr_pages >SEAMLDR_MAX_NR_MODULE_PAGES ) +return -EINVAL ; + +if (memcmp (header->signature,"TDX-BLOB" ,sizeof (header->signature))) +return -EINVAL ; + +if (memchr_inv (header->reserved,0 ,sizeof (header->reserved))) +return -EINVAL ; + +populate_seamldr_params (params, sigstruct_start, header->sigstruct_nr_pages, + module_start, header->module_nr_pages); +return 0 ; +} +/** * seamldr_install_module - Install a new TDX module. * @data: Pointer to the TDX module image.
@@ -51,7 +174,27 @@ EXPORT_SYMBOL_FOR_MODULES(seamldr_get_info, "tdx-host");*/ int seamldr_install_module (const u8 *data,u32 data_len) { +struct seamldr_params *params; +const struct tdx_image *image; +int ret; + +if (data_len <TDX_IMAGE_HEADER_SIZE ) +return -EINVAL ; + + image = (const struct tdx_image *)data; + + params =kzalloc_obj (*params); +if (!params) +return -ENOMEM ; + +/* Populate 'params' from 'image'. */ + ret =init_seamldr_params (params, image, data_len); +if (ret) + goto out; +/* TODO: Update TDX module here */ -return 0 ; +out: +kfree (params); +return ret; }EXPORT_SYMBOL_FOR_MODULES (seamldr_install_module,"tdx-host" );
Signature · 1 line
2.52.0
Kiryl Shutsemau
maintainerRe: [PATCH v10 13/25] x86/virt/seamldr: Allocate and populate a module update request
arch/x86/virt/vmx/tdx/seamldr.c int seamldr_get_info(struct seamldr_info *seamldr_info) · line 106
Quoted 7 lines · Chao Gao
arch/x86/virt/vmx/tdx/seamldr.c +7
-0+static void populate_pa_list (u64 *pa_list,const u8 *vmalloc_addr,u32 vmalloc_len_pages) +{ +int i; + +for (i =0 ; i < vmalloc_len_pages; i++) { +unsigned long offset = i *PAGE_SIZE ; +unsigned long pfn =vmalloc_to_pfn (&vmalloc_addr[offset]);
I don't like that we need to assume how the image got allocated this deep in the stack.
I can imagine situation in the future when we might want to load TDX module to memory on boot, like initrd. And it won't be vmalloced in this case.
Wouldn't be better to use a neutral way to get physical address that doesn't have the assumption? Like, slow_virt_to_phys().
Quoted 5 lines
arch/x86/virt/vmx/tdx/seamldr.c +5
-0+ + pa_list[i] = pfn <<PAGE_SHIFT ; + } +} +
arch/x86/virt/vmx/tdx/seamldr.c int seamldr_get_info(struct seamldr_info *seamldr_info) · line 121
Quoted 10 lines
arch/x86/virt/vmx/tdx/seamldr.c +10
-0+static void populate_seamldr_params (struct seamldr_params *params, +const u8 *sig,u32 sig_nr_pages, +const u8 *mod,u32 mod_nr_pages) +{ + params->version =0 ; + params->scenario =SEAMLDR_SCENARIO_UPDATE ; + params->module_nr_pages = mod_nr_pages; + +populate_pa_list (params->sigstruct_pages_pa_list, sig, sig_nr_pages); +populate_pa_list (params->module_pages_pa_list, mod, mod_nr_pages);
I am not sure what the value to have this as a separate function.
Having it directly in init_seamldr_params() would be easier to follow.
Quoted 46 lines
arch/x86/virt/vmx/tdx/seamldr.c +46
-0+} + +/* +* @image points to a vmalloc()'d 'struct tdx_image'. Transform +* it into @params which is the P-SEAMLDR ABI format. +*/ +static int init_seamldr_params (struct seamldr_params *params, +const struct tdx_image *image, +u32 image_len) +{ +const struct tdx_image_header *header = &image->header; + +u32 sigstruct_len = header->sigstruct_nr_pages *PAGE_SIZE ; +u32 module_len = header->module_nr_pages *PAGE_SIZE ; + +u8 *header_start = (u8 *)header; +u8 *header_end = header_start +TDX_IMAGE_HEADER_SIZE ; + +u8 *sigstruct_start = header_end; +u8 *sigstruct_end = sigstruct_start + sigstruct_len; + +u8 *module_start = sigstruct_end; + +/* Check the calculated payload size against the image size. */ +if (TDX_IMAGE_HEADER_SIZE + sigstruct_len + module_len != image_len) +return -EINVAL ; + +/* Reject unsupported tdx_image ABI versions. */ +if (header->version !=TDX_IMAGE_VERSION_2 ) +return -EINVAL ; + +if (header->sigstruct_nr_pages >SEAMLDR_MAX_NR_SIG_PAGES || + header->module_nr_pages >SEAMLDR_MAX_NR_MODULE_PAGES ) +return -EINVAL ; + +if (memcmp (header->signature,"TDX-BLOB" ,sizeof (header->signature))) +return -EINVAL ; + +if (memchr_inv (header->reserved,0 ,sizeof (header->reserved))) +return -EINVAL ; + +populate_seamldr_params (params, sigstruct_start, header->sigstruct_nr_pages, + module_start, header->module_nr_pages); +return 0 ; +} +
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
[tip: x86/tdx] x86/virt/seamldr: Allocate and populate a module update request
In reply to Chao GaoThe following commit has been merged into the x86/tdx branch of tip:
The following commit has been merged into the x86/tdx branch of tip:
Commit-ID: 23a81e6cce154950ad203fd8bc5a016038c173b3 Gitweb: https://git.kernel.org/tip/23a81e6cce154950ad203fd8bc5a016038c173b3 Author: Chao Gao <[email protected]> AuthorDate: Wed, 20 May 2026 15:29:01 -07:00 Committer: Dave Hansen <[email protected]> CommitterDate: Wed, 03 Jun 2026 08:14:51 -07:00
x86/virt/seamldr: Allocate and populate a module update request
There are two important ABIs here:
'struct tdx_image' - The on-disk and in-memory format for a TDX
module update image.
'struct seamldr_params' - The in-memory ABI passed to the TDX module
loader. Points to a single 'struct tdx_image' broken up into 4k pages.
Userspace supplies the update image in 'struct tdx_image' format. The image consists of a header followed by a sigstruct and the module binary. P-SEAMLDR, however, consumes 'struct seamldr_params' rather than the image directly.
Parse the 'struct tdx_image' provided by userspace and populate a matching 'struct seamldr_params'.
The 'tdx_image' ABI is versioned. Two public versions exist today:0x100 and 0x200. This kernel only accepts 0x200. The older 0x100 format is being deprecated and is intentionally not supported here. Future versions of the module might be able to use the same ABIs (user/kernel and kernel/SEAMLDR) but they will not be able to use this kernel code.
Reject module images without that specific version. This ensures that the kernel is able to understand the passed-in format.
Validate the 'struct tdx_image' header before using it, because the header is consumed solely by the kernel to locate the sigstruct and module within the image. Do not validate the payload itself. The sigstruct and module pages are passed through to P-SEAMLDR, which validates them as part of the update.
sigstruct_pages_pa_list currently has only one entry, but it will grow to four pages in the future. Keep it as an array for symmetry with module_pages_pa_list and for extensibility.
[ dhansen: normal changelog clarification/munging ]
--- arch/x86/virt/vmx/tdx/seamldr.c | 157 ++++++++++++++++++++++++++++++- 1 file changed, 156 insertions(+), 1 deletion(-)
arch/x86/virt/vmx/tdx/seamldr.c +156 -1
@@ -7,6 +7,8 @@#define pr_fmt (fmt) "seamldr: " fmt#include <linux/bug.h> +#include <linux/mm.h> +#include <linux/slab.h> #include <linux/spinlock.h> #include <asm/cpufeature.h>
@@ -18,6 +20,35 @@/* P-SEAMLDR SEAMCALL leaf function */ #define P_SEAMLDR_INFO 0x8000000000000000 +#define SEAMLDR_MAX_NR_MODULE_PAGES 496 +#define SEAMLDR_MAX_NR_SIG_PAGES 1 + +/* +* The seamldr_params "scenario" field specifies the operation mode: +* 0: Install TDX module from scratch (not used by kernel) +* 1: Update existing TDX module to a compatible version +*/ +#define SEAMLDR_SCENARIO_UPDATE 1 + +/* +* This is the "SEAMLDR_PARAMS" data structure defined in the +* "SEAM Loader (SEAMLDR) Interface Specification". +* +* It is the in-memory ABI that the kernel passes to the P-SEAMLDR +* to update the TDX module. It breaks the TDX module image up in +* page-size pieces. +*/ +struct seamldr_params { +u32 version; +u32 scenario; +u64 sigstruct_pages_pa_list[SEAMLDR_MAX_NR_SIG_PAGES ]; +u8 reserved[104 ]; +u64 module_nr_pages; +u64 module_pages_pa_list[SEAMLDR_MAX_NR_MODULE_PAGES ]; +} __packed; + +static_assert (sizeof (struct seamldr_params ) ==4096 ); +/* * Serialize P-SEAMLDR calls since the hardware only allows a single CPU to * interact with P-SEAMLDR simultaneously. Use raw version as the calls can
@@ -55,6 +86,106 @@ int seamldr_get_info(struct seamldr_info *seamldr_info) }EXPORT_SYMBOL_FOR_MODULES (seamldr_get_info,"tdx-host" ); +#define TDX_IMAGE_VERSION_2 0x200 + +/* First page of the on-disk module update image: */ +struct tdx_image_header { +u16 version; +u16 checksum; +u8 signature[8 ]; +u32 sigstruct_nr_pages; +u32 module_nr_pages; +u8 reserved[4076 ]; +} __packed; + +#define TDX_IMAGE_HEADER_SIZE sizeof(struct tdx_image_header) +static_assert (TDX_IMAGE_HEADER_SIZE ==4096 ); + +/* +* Intel TDX module update ABI structure. aka. "TDX module blob". +* This is the on-disk format that fw_upload lands in a kernel +* buffer. +* +* @payload contains sigstruct pages followed by module pages. +*/ +struct tdx_image { +struct tdx_image_header header; +u8 payload[]; +}; + +/* +* Given a vmalloc() allocation, write all of the backing physical +* addresses to pa_list[]. Caller guarantees that the array is big +* enough. +*/ +static void populate_pa_list (u64 *pa_list,const u8 *vmalloc_addr,u32 vmalloc_len_pages) +{ +int i; + +for (i =0 ; i < vmalloc_len_pages; i++) { +unsigned long offset = i *PAGE_SIZE ; +unsigned long pfn =vmalloc_to_pfn (&vmalloc_addr[offset]); + + pa_list[i] = pfn <<PAGE_SHIFT ; + } +} + +static void populate_seamldr_params (struct seamldr_params *params, +const u8 *sig,u32 sig_nr_pages, +const u8 *mod,u32 mod_nr_pages) +{ + params->version =0 ; + params->scenario =SEAMLDR_SCENARIO_UPDATE ; + params->module_nr_pages = mod_nr_pages; + +populate_pa_list (params->sigstruct_pages_pa_list, sig, sig_nr_pages); +populate_pa_list (params->module_pages_pa_list, mod, mod_nr_pages); +} + +/* +* @image points to a vmalloc()'d 'struct tdx_image'. Transform +* it into @params which is the P-SEAMLDR ABI format. +*/ +static int init_seamldr_params (struct seamldr_params *params, +const struct tdx_image *image, +u32 image_len) +{ +const struct tdx_image_header *header = &image->header; + +u32 sigstruct_len = header->sigstruct_nr_pages *PAGE_SIZE ; +u32 module_len = header->module_nr_pages *PAGE_SIZE ; + +u8 *header_start = (u8 *)header; +u8 *header_end = header_start +TDX_IMAGE_HEADER_SIZE ; + +u8 *sigstruct_start = header_end; +u8 *sigstruct_end = sigstruct_start + sigstruct_len; + +u8 *module_start = sigstruct_end; + +/* Check the calculated payload size against the image size. */ +if (TDX_IMAGE_HEADER_SIZE + sigstruct_len + module_len != image_len) +return -EINVAL ; + +/* Reject unsupported tdx_image ABI versions. */ +if (header->version !=TDX_IMAGE_VERSION_2 ) +return -EINVAL ; + +if (header->sigstruct_nr_pages >SEAMLDR_MAX_NR_SIG_PAGES || + header->module_nr_pages >SEAMLDR_MAX_NR_MODULE_PAGES ) +return -EINVAL ; + +if (memcmp (header->signature,"TDX-BLOB" ,sizeof (header->signature))) +return -EINVAL ; + +if (memchr_inv (header->reserved,0 ,sizeof (header->reserved))) +return -EINVAL ; + +populate_seamldr_params (params, sigstruct_start, header->sigstruct_nr_pages, + module_start, header->module_nr_pages); +return 0 ; +} +/** * seamldr_install_module - Install a new TDX module. * @data: Pointer to the TDX module image.
@@ -64,7 +195,31 @@ EXPORT_SYMBOL_FOR_MODULES(seamldr_get_info, "tdx-host");*/ int seamldr_install_module (const u8 *data,u32 data_len) { +struct seamldr_params *params; +const struct tdx_image *image; +int ret; + +/* +* init_seamldr_params() reads the header early. +* Ensure there is enough data to do at least that. +*/ +if (data_len <TDX_IMAGE_HEADER_SIZE ) +return -EINVAL ; + + image = (const struct tdx_image *)data; + + params =kzalloc_obj (*params); +if (!params) +return -ENOMEM ; + +/* Populate 'params' from 'image'. */ + ret =init_seamldr_params (params, image, data_len); +if (ret) + goto out; +/* TODO: Update TDX module here */ -return 0 ; +out: +kfree (params); +return ret; }EXPORT_SYMBOL_FOR_MODULES (seamldr_install_module,"tdx-host" );
PATCH v10 14/25 x86/virt/seamldr: Introduce skeleton for TDX module updates
In reply to Chao GaoTDX module updates require careful synchronization with other TDX operations. The requirements are (#1/#2 reflect current behavior that must be preserved):
1. SEAMCALLs need to be callable from both process and IRQ contexts.
2. SEAMCALLs need to be able to run concurrently across CPUs
3. During updates, only update-related SEAMCALLs are permitted; all other SEAMCALLs shouldn't be called.
4. During updates, all online CPUs must participate in the update work.
No single lock primitive satisfies all requirements. For instance, rwlock_t handles #1/#2 but fails #4: CPUs spinning with IRQs disabled cannot be directed to perform update work.
Use stop_machine() as it is the only well-understood mechanism that can meet all requirements.
And TDX module updates consist of several steps (See Intel® Trust Domain Extensions (Intel® TDX) Module Base Architecture Specification, Chapter "TD-Preserving TDX module Update"). Ordering requirements between steps mandate lockstep synchronization across all CPUs.
multi_cpu_stop() provides a good example of executing a multi-step task in lockstep across CPUs, but it does not synchronize the individual steps inside the callback itself.
Implement a similar state machine as the skeleton for TDX module updates. Each state represents one step in the update flow, and the state advances only after all CPUs acknowledge completion of the current step. This acknowledgment mechanism provides the required lockstep execution.
The update flow is intentionally simpler than multi_cpu_stop() in two ways:
a) use a spinlock to protect the control data instead of atomic_t and explicit memory barriers.
b) omit touch_nmi_watchdog() and rcu_momentary_eqs(), which exist there for debugging and are not strictly needed for this update flow
Potential alternative to stop_machine()
=======================================
An alternative approach is to lock all KVM entry points and kick all vCPUs. Here, KVM entry points refer to KVM VM/vCPU ioctl entry points, implemented in KVM common code (virt/kvm). Adding a locking mechanism there would affect all architectures KVM supports. And to lock only TDX vCPUs, new logic would be needed to identify TDX vCPUs, which the KVM common code currently lacks. This would add significant complexity and maintenance overhead to KVM for this TDX-specific use case, so don't take this approach.
--- arch/x86/virt/vmx/tdx/seamldr.c | 87 ++++++++++++++++++++++++++++++++- 1 file changed, 86 insertions(+), 1 deletion(-)
arch/x86/virt/vmx/tdx/seamldr.c +86 -1
@@ -7,8 +7,10 @@#define pr_fmt (fmt) "seamldr: " fmt#include <linux/mm.h> +#include <linux/nmi.h> #include <linux/slab.h> #include <linux/spinlock.h> +#include <linux/stop_machine.h> #include <asm/seamldr.h>
@@ -165,6 +167,84 @@ static int init_seamldr_params(struct seamldr_params *params,return 0 ; } +/* +* During a TDX module update, all CPUs start from MODULE_UPDATE_START and +* progress to MODULE_UPDATE_DONE. Each state is associated with certain +* work. For some states, just one CPU needs to perform the work, while +* other CPUs just wait during those states. +*/ +enum module_update_state { +MODULE_UPDATE_START , +MODULE_UPDATE_DONE , +}; + +static struct update_ctrl { +enum module_update_state state; +int num_ack; +/* +* Protect update_ctrl. Raw spinlock as it will be acquired from +* interrupt-disabled contexts. +*/ +raw_spinlock_t lock; +} update_ctrl; + +/* Called with ctrl->lock held or during initialization. */ +static void __set_target_state (struct update_ctrl *ctrl, +enum module_update_state newstate) +{ +/* Reset ack counter. */ + ctrl->num_ack =0 ; + ctrl->state = newstate; +} + +/* Last one to ack a state moves to the next state. */ +static void ack_state (struct update_ctrl *ctrl) +{ +raw_spin_lock (&ctrl->lock); + + ctrl->num_ack++; +if (ctrl->num_ack ==num_online_cpus ()) +__set_target_state (ctrl, ctrl->state +1 ); + +raw_spin_unlock (&ctrl->lock); +} + +static void init_state (struct update_ctrl *ctrl) +{ +raw_spin_lock_init (&ctrl->lock); +__set_target_state (ctrl,MODULE_UPDATE_START +1 ); +} + +/* +* See multi_cpu_stop() from where this multi-cpu state-machine was +* adopted. +*/ +static int do_seamldr_install_module (void *seamldr_params) +{ +enum module_update_state newstate, curstate =MODULE_UPDATE_START ; +int ret =0 ; + +do { + newstate =READ_ONCE (update_ctrl.state); + +if (curstate == newstate) { +cpu_relax (); +continue ; + } + + curstate = newstate; +switch (curstate) { +/* TODO: add the update steps. */ +default : +break ; + } + +ack_state (&update_ctrl); + }while (curstate !=MODULE_UPDATE_DONE ); + +return ret; +} +/** * seamldr_install_module - Install a new TDX module. * @data: Pointer to the TDX module image.
@@ -192,7 +272,12 @@ int seamldr_install_module(const u8 *data, u32 data_len)if (ret) goto out; -/* TODO: Update TDX module here */ +/* Ensure a stable set of online CPUs for the update process. */ +cpus_read_lock (); +init_state (&update_ctrl); + ret =stop_machine_cpuslocked (do_seamldr_install_module, params, cpu_online_mask); +cpus_read_unlock (); + out:kfree (params);return ret;
Signature · 1 line
2.52.0
[tip: x86/tdx] x86/virt/seamldr: Introduce skeleton for TDX module updates
The following commit has been merged into the x86/tdx branch of tip:
The following commit has been merged into the x86/tdx branch of tip:
Commit-ID: ab6be1168cf963630335a6f08938fd510a9225bf Gitweb: https://git.kernel.org/tip/ab6be1168cf963630335a6f08938fd510a9225bf Author: Chao Gao <[email protected]> AuthorDate: Wed, 20 May 2026 15:29:02 -07:00 Committer: Dave Hansen <[email protected]> CommitterDate: Wed, 03 Jun 2026 08:14:51 -07:00
x86/virt/seamldr: Introduce skeleton for TDX module updates
tl;dr: Use stop_machine() and a state machine based on the "MULTI_STOP" pattern to implement core TDX module update logic.
Long version:
TDX module updates require careful synchronization with other TDX operations. The requirements are (#1/#2 reflect current behavior that must be preserved):
1. SEAMCALLs need to be callable from both process and IRQ contexts.
2. SEAMCALLs need to be able to run concurrently across CPUs
3. During updates, only update-related SEAMCALLs are permitted; all other SEAMCALLs shouldn't be called.
4. During updates, all online CPUs must participate in the update work.
No single lock primitive satisfies all requirements. For instance, rwlock_t handles #1/#2 but fails #4: CPUs spinning with IRQs disabled cannot be directed to perform update work.
Use stop_machine() as it is the only well-understood mechanism that can meet all requirements.
And TDX module updates consist of several steps (See Intel Trust Domain Extensions (Intel TDX) Module Base Architecture Specification, Chapter "TD-Preserving TDX module Update"). Ordering requirements between steps mandate lockstep synchronization across all CPUs.
multi_cpu_stop() provides a good example of executing a multi-step task in lockstep across CPUs, but it does not synchronize the individual steps inside the callback itself.
Implement a similar state machine as the skeleton for TDX module updates. Each state represents one step in the update flow, and the state advances only after all CPUs acknowledge completion of the current step. This acknowledgment mechanism provides the required lockstep execution.
The update flow is intentionally simpler than multi_cpu_stop() in two ways:
a) use a spinlock to protect the control data instead of atomic_t and explicit memory barriers.
b) omit touch_nmi_watchdog() and rcu_momentary_eqs(), which exist there for debugging and are not strictly needed for this update flow
Potential alternative to stop_machine()
=======================================
An alternative approach is to lock all KVM entry points and kick all vCPUs. Here, KVM entry points refer to KVM VM/vCPU ioctl entry points, implemented in KVM common code (virt/kvm). Adding a locking mechanism there would affect all architectures KVM supports. And to lock only TDX vCPUs, new logic would be needed to identify TDX vCPUs, which the KVM common code currently lacks. This would add significant complexity and maintenance overhead to KVM for this TDX-specific use case, so don't take this approach.
[ dhansen: normal changelog/style munging ]
--- arch/x86/virt/vmx/tdx/seamldr.c | 89 +++++++++++++++++++++++++++++++- 1 file changed, 88 insertions(+), 1 deletion(-)
arch/x86/virt/vmx/tdx/seamldr.c +88 -1
@@ -8,8 +8,10 @@#include <linux/bug.h> #include <linux/mm.h> +#include <linux/nmi.h> #include <linux/slab.h> #include <linux/spinlock.h> +#include <linux/stop_machine.h> #include <asm/cpufeature.h> #include <asm/cpufeatures.h>
@@ -186,6 +188,85 @@ static int init_seamldr_params(struct seamldr_params *params,return 0 ; } +/* +* During a TDX module update, all CPUs start from MODULE_UPDATE_START and +* progress to MODULE_UPDATE_DONE. Each state is associated with certain +* work. For some states, just one CPU needs to perform the work, while +* other CPUs just wait during those states. +*/ +enum module_update_state { +MODULE_UPDATE_START , +MODULE_UPDATE_DONE , +}; + +static struct update_ctrl { +enum module_update_state state; +int num_ack; +/* +* Protect update_ctrl. Raw spinlock as it will be acquired from +* interrupt-disabled contexts. +*/ +raw_spinlock_t lock; +} update_ctrl; + +/* Called with ctrl->lock held or during initialization. */ +static void __set_target_state (struct update_ctrl *ctrl, +enum module_update_state newstate) +{ +/* Reset ack counter. */ + ctrl->num_ack =0 ; + ctrl->state = newstate; +} + +/* Last one to ack a state moves to the next state. */ +static void ack_state (struct update_ctrl *ctrl) +{ +raw_spin_lock (&ctrl->lock); + + ctrl->num_ack++; +if (ctrl->num_ack ==num_online_cpus ()) +__set_target_state (ctrl, ctrl->state +1 ); + +raw_spin_unlock (&ctrl->lock); +} + +static void init_state (struct update_ctrl *ctrl) +{ +raw_spin_lock_init (&ctrl->lock); +__set_target_state (ctrl,MODULE_UPDATE_START +1 ); +} + +/* +* See multi_cpu_stop() from where this multi-cpu state-machine was +* adopted. +*/ +static int do_seamldr_install_module (void *seamldr_params) +{ +enum module_update_state curstate =MODULE_UPDATE_START ; +enum module_update_state newstate; +int ret =0 ; + +do { + newstate =READ_ONCE (update_ctrl.state); + +if (curstate == newstate) { +cpu_relax (); +continue ; + } + + curstate = newstate; +switch (curstate) { +/* TODO: add the update steps. */ +default : +break ; + } + +ack_state (&update_ctrl); + }while (curstate !=MODULE_UPDATE_DONE ); + +return ret; +} +/** * seamldr_install_module - Install a new TDX module. * @data: Pointer to the TDX module image.
@@ -217,7 +298,13 @@ int seamldr_install_module(const u8 *data, u32 data_len)if (ret) goto out; -/* TODO: Update TDX module here */ +/* Ensure a stable set of online CPUs for the update process. */ +cpus_read_lock (); +init_state (&update_ctrl); + ret =stop_machine_cpuslocked (do_seamldr_install_module, params, + cpu_online_mask); +cpus_read_unlock (); + out:kfree (params);return ret;
PATCH v10 15/25 x86/virt/seamldr: Abort updates after a failed step
In reply to Chao GaoA TDX module update is a multi-step process, and any step can fail.
The current update flow continues to later steps after an error. Continuing after a failure can cause the TDX module to enter an unrecoverable state.
But certain failures during the initial module shutdown step should simply return an error to userspace, so the update can be retried cleanly.
To preserve that recoverability, one option would be to abort the update only for those failures, since they occur before any TDX module state is changed. But special-casing specific failures in specific steps would complicate the do-while() update loop for no benefit.
Simply abort update on any failure, at any step.
Track failures for each step, stop the update loop once a failure is observed, and do not advance the state machine to the next step.
---
v9:
- Avoid nested if/else by deferring failure accounting to ack_state().
- Reduce indentation of the main flow.
- Convert the failed flag into a counter. This avoids a conditional update of the flag; the counter can simply accumulate failures.
--- arch/x86/virt/vmx/tdx/seamldr.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
arch/x86/virt/vmx/tdx/seamldr.c +7 -4
@@ -181,6 +181,7 @@ enum module_update_state { static struct update_ctrl { enum module_update_state state; int num_ack; + int num_failed; /* * Protect update_ctrl. Raw spinlock as it will be acquired from * interrupt-disabled contexts.
@@ -198,12 +199,13 @@ static void __set_target_state(struct update_ctrl *ctrl, }/* Last one to ack a state moves to the next state. */ -static void ack_state (struct update_ctrl *ctrl) +static void ack_state (struct update_ctrl *ctrl,int result) {raw_spin_lock (&ctrl->lock); + ctrl->num_failed += !!result; ctrl->num_ack++; -if (ctrl->num_ack ==num_online_cpus ()) +if (ctrl->num_ack ==num_online_cpus () && !ctrl->num_failed)__set_target_state (ctrl, ctrl->state +1 );raw_spin_unlock (&ctrl->lock);
@@ -213,6 +215,7 @@ static void init_state(struct update_ctrl *ctrl) {raw_spin_lock_init (&ctrl->lock);__set_target_state (ctrl,MODULE_UPDATE_START +1 ); + ctrl->num_failed =0 ; }/*
@@ -239,8 +242,8 @@ static int do_seamldr_install_module(void *seamldr_params)break ; } -ack_state (&update_ctrl); - }while (curstate !=MODULE_UPDATE_DONE ); +ack_state (&update_ctrl, ret); + }while (curstate !=MODULE_UPDATE_DONE && !READ_ONCE (update_ctrl.num_failed));return ret; }
Signature · 1 line
2.52.0
Re: [PATCH v10 15/25] x86/virt/seamldr: Abort updates after a failed step
arch/x86/virt/vmx/tdx/seamldr.c static void __set_target_state(struct update_ctrl *ctrl,
Quoted 6 lines · Chao Gao
arch/x86/virt/vmx/tdx/seamldr.c +2
-0+static void ack_state (struct update_ctrl *ctrl,int result) {raw_spin_lock (&ctrl->lock); + ctrl->num_failed += !!result; ctrl->num_ack++;...> @@ -239,8 +242,8 @@ static int do_seamldr_install_module(void
*seamldr_params)
arch/x86/virt/vmx/tdx/seamldr.c static int do_seamldr_install_module(void *seamldr_params) · line 246
Quoted 7 lines
arch/x86/virt/vmx/tdx/seamldr.c +2
-2break ; } -ack_state (&update_ctrl); - }while (curstate !=MODULE_UPDATE_DONE ); +ack_state (&update_ctrl, ret); + }while (curstate !=MODULE_UPDATE_DONE && !READ_ONCE (update_ctrl.num_failed));
The READ_ONCE() is cute. But it's not really effective. It's also overly-complicated.
update_ctrl.num_failed is just a single. Nothing cares if it is 1 or 2 or 999. So why have a count?
Any reason this won't work?
if (result) set_bit(0, ctrl->failed);
... and on the read side:
test_bit(0, &update_ctrl.failed)
That's 100% non-ambiguous. It doesn't have a counter where one isn't needed. It also can't even theoretically be messed up by the compiler.
[tip: x86/tdx] x86/virt/seamldr: Abort updates after a failed step
In reply to Chao GaoThe following commit has been merged into the x86/tdx branch of tip:
The following commit has been merged into the x86/tdx branch of tip:
Commit-ID: be4efe63c050be48961a5430c91e69f95af08b81 Gitweb: https://git.kernel.org/tip/be4efe63c050be48961a5430c91e69f95af08b81 Author: Chao Gao <[email protected]> AuthorDate: Wed, 20 May 2026 15:29:04 -07:00 Committer: Dave Hansen <[email protected]> CommitterDate: Wed, 03 Jun 2026 08:14:51 -07:00
x86/virt/seamldr: Abort updates after a failed step
A TDX module update is a multi-step process, and any step can fail.
The current update flow continues to later steps after an error. Continuing after a failure can cause the TDX module to enter an unrecoverable state.
But certain failures during the initial module shutdown step should simply return an error to userspace, so the update can be retried cleanly.
To preserve that recoverability, one option would be to abort the update only for those failures, since they occur before any TDX module state is changed. But special-casing specific failures in specific steps would complicate the do-while() update loop for no benefit.
Simply abort update on any failure, at any step.
Track failures for each step, stop the update loop once a failure is observed, and do not advance the state machine to the next step.
[ dhansen: style nits ]
--- arch/x86/virt/vmx/tdx/seamldr.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
arch/x86/virt/vmx/tdx/seamldr.c +8 -4
@@ -202,6 +202,7 @@ enum module_update_state { static struct update_ctrl { enum module_update_state state; int num_ack; + int num_failed; /* * Protect update_ctrl. Raw spinlock as it will be acquired from * interrupt-disabled contexts.
@@ -219,12 +220,13 @@ static void __set_target_state(struct update_ctrl *ctrl, }/* Last one to ack a state moves to the next state. */ -static void ack_state (struct update_ctrl *ctrl) +static void ack_state (struct update_ctrl *ctrl,int result) {raw_spin_lock (&ctrl->lock); + ctrl->num_failed += !!result; ctrl->num_ack++; -if (ctrl->num_ack ==num_online_cpus ()) +if (ctrl->num_ack ==num_online_cpus () && !ctrl->num_failed)__set_target_state (ctrl, ctrl->state +1 );raw_spin_unlock (&ctrl->lock);
@@ -234,6 +236,7 @@ static void init_state(struct update_ctrl *ctrl) {raw_spin_lock_init (&ctrl->lock);__set_target_state (ctrl,MODULE_UPDATE_START +1 ); + ctrl->num_failed =0 ; }/*
@@ -261,8 +264,9 @@ static int do_seamldr_install_module(void *seamldr_params)break ; } -ack_state (&update_ctrl); - }while (curstate !=MODULE_UPDATE_DONE ); +ack_state (&update_ctrl, ret); + }while (curstate !=MODULE_UPDATE_DONE && + !READ_ONCE (update_ctrl.num_failed));return ret; }
PATCH v10 16/25 x86/virt/seamldr: Shut down the current TDX module
In reply to Chao GaoThe first step of TDX module updates is shutting down the current TDX module. This step also packs state information that needs to be preserved across updates, called "handoff data". This handoff data is consumed by the updated module and stored internally in the SEAM range and hidden from the kernel.
Since handoff data layout may change between modules, the handoff data is versioned. Each module has a native handoff version and provides backward support for several older versions.
The complete handoff versioning protocol is complex as it supports both module upgrades and downgrades. See details in Intel® Trust Domain Extensions (Intel® TDX) Module Base Architecture Specification, Chapter "Handoff Versioning".
Ideally, the kernel needs to retrieve the handoff versions supported by the current module and the new module and select a version supported by both. But since this implementation only supports module upgrades, simply request handoff data from the current module using its highest supported version. That is sufficient for this upgrade-only implementation.
Retrieve the module's handoff version from TDX global metadata and add an update step to shut down the module. Module shutdown only needs to run on one CPU.
Don't cache the handoff information in tdx_sysinfo. It is used only for module shutdown, and is present only when the TDX module supports updates. Caching it in get_tdx_sys_info() would require extra update-support guards and refreshing the cached value across module updates.
---
v10:
- Polish the changelog [Rick]
- Rename "primary" to "is_lead_cpu" and polish the comment above it [Rick]
--- arch/x86/include/asm/tdx_global_metadata.h | 4 ++++ arch/x86/virt/vmx/tdx/seamldr.c | 15 ++++++++++++++- arch/x86/virt/vmx/tdx/tdx.c | 19 ++++++++++++++++++- arch/x86/virt/vmx/tdx/tdx.h | 3 +++ arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 13 +++++++++++++ 5 files changed, 52 insertions(+), 2 deletions(-)
arch/x86/include/asm/tdx_global_metadata.h +4 -0
@@ -40,6 +40,10 @@ struct tdx_sys_info_td_conf {u64 cpuid_config_values[128 ][2 ]; }; +struct tdx_sys_info_handoff { +u16 module_hv; +}; +struct tdx_sys_info {struct tdx_sys_info_version version;struct tdx_sys_info_features features;
arch/x86/virt/vmx/tdx/seamldr.c +14 -1
@@ -15,6 +15,7 @@#include <asm/seamldr.h> #include "seamcall_internal.h" +#include "tdx.h" /* P-SEAMLDR SEAMCALL leaf function */ #define P_SEAMLDR_INFO 0x8000000000000000
@@ -175,6 +176,7 @@ static int init_seamldr_params(struct seamldr_params *params,*/ enum module_update_state {MODULE_UPDATE_START , +MODULE_UPDATE_SHUTDOWN ,MODULE_UPDATE_DONE , };
@@ -225,8 +227,16 @@ static void init_state(struct update_ctrl *ctrl)static int do_seamldr_install_module (void *seamldr_params) {enum module_update_state newstate, curstate =MODULE_UPDATE_START ; +int cpu =smp_processor_id (); +bool is_lead_cpu;int ret =0 ; +/* +* Some steps must be run on exactly one CPU. Pick a "lead" CPU to +* execute those steps. Use CPU 0 because it is always online. +*/ + is_lead_cpu = cpu ==0 ; + do { newstate = READ_ONCE(update_ctrl.state);
@@ -237,7 +247,10 @@ static int do_seamldr_install_module(void *seamldr_params) curstate = newstate;switch (curstate) { -/* TODO: add the update steps. */ +case MODULE_UPDATE_SHUTDOWN : +if (is_lead_cpu) + ret =tdx_module_shutdown (); +break ;default :break ; }
arch/x86/virt/vmx/tdx/tdx.c +18 -1
@@ -328,7 +328,7 @@ static __init int build_tdx_memlist(struct list_head *tmb_list)return ret; } -static __init int read_sys_metadata_field (u64 field_id,u64 *data) +static int read_sys_metadata_field (u64 field_id,u64 *data) {struct tdx_module_args args = {};int ret;
@@ -1274,6 +1274,23 @@ static __init int tdx_enable(void) }subsys_initcall (tdx_enable); +int tdx_module_shutdown (void ) +{ +struct tdx_sys_info_handoff handoff = {}; +struct tdx_module_args args = {}; +int ret; + + ret =get_tdx_sys_info_handoff (&handoff); +WARN_ON_ONCE (ret); + +/* +* Use the module's handoff version as it is the highest the +* module can produce and most likely supported by newer modules. +*/ + args.rcx = handoff.module_hv; +return seamcall_prerr (TDH_SYS_SHUTDOWN , &args); +} +static bool is_pamt_page (unsigned long phys) {struct tdmr_info_list *tdmr_list = &tdx_tdmr_list;
arch/x86/virt/vmx/tdx/tdx.h +3 -0
@@ -46,6 +46,7 @@#define TDH_PHYMEM_PAGE_WBINVD 41#define TDH_VP_WR 43#define TDH_SYS_CONFIG 45 +#define TDH_SYS_SHUTDOWN 52#define TDH_SYS_DISABLE 69/*
@@ -108,4 +109,6 @@ struct tdmr_info_list { int max_tdmrs; /* How many 'tdmr_info's are allocated */ }; +int tdx_module_shutdown (void ); +#endif
arch/x86/virt/vmx/tdx/tdx_global_metadata.c +13 -0
@@ -100,6 +100,19 @@ 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
[tip: x86/tdx] x86/virt/seamldr: Shut down the current TDX module
The following commit has been merged into the x86/tdx branch of tip:
The following commit has been merged into the x86/tdx branch of tip:
Commit-ID: 9ea06080a680a2aa521795432396d2c27f9bb9f9 Gitweb: https://git.kernel.org/tip/9ea06080a680a2aa521795432396d2c27f9bb9f9 Author: Chao Gao <[email protected]> AuthorDate: Wed, 20 May 2026 15:29:05 -07:00 Committer: Dave Hansen <[email protected]> CommitterDate: Wed, 03 Jun 2026 08:59:24 -07:00
x86/virt/seamldr: Shut down the current TDX module
The first step of TDX module updates is shutting down the current TDX module. This step also packs state information that needs to be preserved across updates, called "handoff data". This handoff data is consumed by the updated module and stored internally in the SEAM range and hidden from the kernel.
Since the handoff data layout may change between modules, the handoff data is versioned. Each module has a native handoff version and provides backward support for several older versions.
The complete handoff versioning protocol is complex as it supports both module upgrades and downgrades. See details in "Intel Trust Domain Extensions (Intel TDX) Module Base Architecture Specification", Chapter "Handoff Versioning".
Ideally, the kernel needs to retrieve the handoff versions supported by the current module and the new module and select a version supported by both. But since this implementation only supports module upgrades, simply request handoff data from the current module using its highest supported version. That is sufficient for this upgrade-only implementation.
Retrieve the module's handoff version from TDX global metadata and add an update step to shut down the module. Module shutdown only needs to run on one CPU.
Don't cache the handoff information in tdx_sysinfo. It is used only for module shutdown, and is present only when the TDX module supports updates. Caching it in get_tdx_sys_info() would require extra update-support guards and refreshing the cached value across module updates.
[ dhansen: fix up function variables, remove 'cpu'.
Return from tdx_module_shutdown() early if handoff call fails. ]
--- arch/x86/include/asm/tdx_global_metadata.h | 4 +++- arch/x86/virt/vmx/tdx/seamldr.c | 15 +++++++++++- arch/x86/virt/vmx/tdx/tdx.c | 25 +++++++++++++++++++- arch/x86/virt/vmx/tdx/tdx.h | 3 ++- arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 13 ++++++++++- 5 files changed, 58 insertions(+), 2 deletions(-)
arch/x86/include/asm/tdx_global_metadata.h +4 -0
@@ -40,6 +40,10 @@ struct tdx_sys_info_td_conf {u64 cpuid_config_values[128 ][2 ]; }; +struct tdx_sys_info_handoff { +u16 module_hv; +}; +struct tdx_sys_info {struct tdx_sys_info_version version;struct tdx_sys_info_features features;
arch/x86/virt/vmx/tdx/seamldr.c +14 -1
@@ -18,6 +18,7 @@#include <asm/seamldr.h> #include "seamcall_internal.h" +#include "tdx.h" /* P-SEAMLDR SEAMCALL leaf function */ #define P_SEAMLDR_INFO 0x8000000000000000
@@ -196,6 +197,7 @@ static int init_seamldr_params(struct seamldr_params *params,*/ enum module_update_state {MODULE_UPDATE_START , +MODULE_UPDATE_SHUTDOWN ,MODULE_UPDATE_DONE , };
@@ -247,8 +249,16 @@ static int do_seamldr_install_module(void *seamldr_params) {enum module_update_state curstate =MODULE_UPDATE_START ;enum module_update_state newstate; +bool is_lead_cpu = false;int ret =0 ; +/* +* Some steps must be run on exactly one CPU. Pick a "lead" CPU to +* execute those steps. Use CPU 0 because it is always online. +*/ +if (smp_processor_id () ==0 ) + is_lead_cpu = true; + do { newstate = READ_ONCE(update_ctrl.state);
@@ -259,7 +269,10 @@ static int do_seamldr_install_module(void *seamldr_params) curstate = newstate;switch (curstate) { -/* TODO: add the update steps. */ +case MODULE_UPDATE_SHUTDOWN : +if (is_lead_cpu) + ret =tdx_module_shutdown (); +break ;default :break ; }
arch/x86/virt/vmx/tdx/tdx.c +24 -1
@@ -328,7 +328,7 @@ err:return ret; } -static __init int read_sys_metadata_field (u64 field_id,u64 *data) +static int read_sys_metadata_field (u64 field_id,u64 *data) {struct tdx_module_args args = {};int ret;
@@ -1274,6 +1274,29 @@ static __init int tdx_enable(void) }subsys_initcall (tdx_enable); +int tdx_module_shutdown (void ) +{ +struct tdx_sys_info_handoff handoff = {}; +struct tdx_module_args args = {}; +int ret; + + ret =get_tdx_sys_info_handoff (&handoff); +/* +* Handoff information is required for proper +* shutdown. Refuse to shut down without it. +*/ +if (ret) +return ret; + +/* +* Use the module's handoff version as it is the highest the +* module can produce and most likely supported by newer modules. +*/ + args.rcx = handoff.module_hv; + +return seamcall_prerr (TDH_SYS_SHUTDOWN , &args); +} +static bool is_pamt_page (unsigned long phys) {struct tdmr_info_list *tdmr_list = &tdx_tdmr_list;
arch/x86/virt/vmx/tdx/tdx.h +3 -0
@@ -46,6 +46,7 @@#define TDH_PHYMEM_PAGE_WBINVD 41#define TDH_VP_WR 43#define TDH_SYS_CONFIG 45 +#define TDH_SYS_SHUTDOWN 52#define TDH_SYS_DISABLE 69/*
@@ -108,4 +109,6 @@ struct tdmr_info_list { int max_tdmrs; /* How many 'tdmr_info's are allocated */ }; +int tdx_module_shutdown (void ); +#endif
arch/x86/virt/vmx/tdx/tdx_global_metadata.c +13 -0
@@ -100,6 +100,19 @@ 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 ;
PATCH v10 17/25 x86/virt/tdx: Reset software states during TDX module shutdown
In reply to Chao GaoThe TDX module requires a one-time global initialization (TDH.SYS.INIT) and per-CPU initialization (TDH.SYS.LP.INIT) before use. These initializations are guarded by software flags to prevent repetition.
After TDX module updates, the new TDX module requires the same global and per-CPU initializations, but the existing software flags prevent re-initialization.
Reset all software flags guarding the initialization flows to allow the global and per-CPU initializations to be triggered again after updates.
---
v9:
- use a global structure for TDX global state and use memset to zero the whole structure [Dave]
--- arch/x86/virt/vmx/tdx/tdx.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-)
arch/x86/virt/vmx/tdx/tdx.c +16 -2
@@ -1278,7 +1278,7 @@ int tdx_module_shutdown(void) {struct tdx_sys_info_handoff handoff = {};struct tdx_module_args args = {}; -int ret; +int ret, cpu; ret =get_tdx_sys_info_handoff (&handoff);WARN_ON_ONCE (ret);
@@ -1288,7 +1288,21 @@ int tdx_module_shutdown(void)* module can produce and most likely supported by newer modules. */ args.rcx = handoff.module_hv; -return seamcall_prerr (TDH_SYS_SHUTDOWN , &args); + ret =seamcall_prerr (TDH_SYS_SHUTDOWN , &args); +if (ret) +return ret; + +/* +* Clear global and per-CPU initialization flags so the new module +* can be fully re-initialized after a successful update. +* +* No locks needed as no concurrent accesses can occur here. +*/ +memset (&tdx_module_state,0 ,sizeof (tdx_module_state)); +for_each_possible_cpu (cpu) +per_cpu (tdx_lp_initialized, cpu) = false; + +return 0 ; }static bool is_pamt_page (unsigned long phys)
Signature · 1 line
2.52.0
[tip: x86/tdx] x86/virt/tdx: Reset software states during TDX module shutdown
The following commit has been merged into the x86/tdx branch of tip:
The following commit has been merged into the x86/tdx branch of tip:
Commit-ID: 65a6542a8144cb88ffccae386e38d61933d575c8 Gitweb: https://git.kernel.org/tip/65a6542a8144cb88ffccae386e38d61933d575c8 Author: Chao Gao <[email protected]> AuthorDate: Wed, 20 May 2026 15:29:06 -07:00 Committer: Dave Hansen <[email protected]> CommitterDate: Wed, 03 Jun 2026 08:59:43 -07:00
x86/virt/tdx: Reset software states during TDX module shutdown
The TDX module requires a one-time global initialization (TDH.SYS.INIT) and per-CPU initialization (TDH.SYS.LP.INIT) before use. These initializations are guarded by software flags to prevent repetition.
Reset all software flags guarding the initialization flows to allow the global and per-CPU initializations to be triggered again after updates.
[ dhansen: trim down changelog ]
--- arch/x86/virt/vmx/tdx/tdx.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-)
arch/x86/virt/vmx/tdx/tdx.c +16 -1
@@ -1279,6 +1279,7 @@ int tdx_module_shutdown(void)struct tdx_sys_info_handoff handoff = {};struct tdx_module_args args = {};int ret; +int cpu; ret =get_tdx_sys_info_handoff (&handoff);/*
@@ -1294,7 +1295,21 @@ int tdx_module_shutdown(void)*/ args.rcx = handoff.module_hv; -return seamcall_prerr (TDH_SYS_SHUTDOWN , &args); + ret =seamcall_prerr (TDH_SYS_SHUTDOWN , &args); +if (ret) +return ret; + +/* +* Clear global and per-CPU initialization flags so the new module +* can be fully re-initialized after a successful update. +* +* No locks needed as no concurrent accesses can occur here. +*/ +memset (&tdx_module_state,0 ,sizeof (tdx_module_state)); +for_each_possible_cpu (cpu) +per_cpu (tdx_lp_initialized, cpu) = false; + +return 0 ; }static bool is_pamt_page (unsigned long phys)
PATCH v10 18/25 x86/virt/seamldr: Install a new TDX module
In reply to Chao GaoFollowing the shutdown of the existing TDX module, the update process continues with installing the new module. P-SEAMLDR provides the SEAMLDR.INSTALL SEAMCALL to perform this installation, which must be executed on all CPUs.
Implement SEAMLDR.INSTALL and execute it on every CPU.
---
v9:
- Add a comment above seamldr_install()
--- arch/x86/virt/vmx/tdx/seamldr.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
arch/x86/virt/vmx/tdx/seamldr.c +14 -0
@@ -19,6 +19,7 @@/* P-SEAMLDR SEAMCALL leaf function */ #define P_SEAMLDR_INFO 0x8000000000000000 +#define P_SEAMLDR_INSTALL 0x8000000000000001#define SEAMLDR_MAX_NR_MODULE_PAGES 496#define SEAMLDR_MAX_NR_SIG_PAGES 1
@@ -76,6 +77,15 @@ int seamldr_get_info(struct seamldr_info *seamldr_info) }EXPORT_SYMBOL_FOR_MODULES (seamldr_get_info,"tdx-host" ); +/* Call into P-SEAMLDR to install a TDX module update */ +static int seamldr_install (const struct seamldr_params *params) +{ +struct tdx_module_args args = {}; + + args.rcx =__pa (params); +return seamldr_call (P_SEAMLDR_INSTALL , &args); +} +#define TDX_IMAGE_VERSION_2 0x200struct tdx_image_header {
@@ -177,6 +187,7 @@ static int init_seamldr_params(struct seamldr_params *params,enum module_update_state {MODULE_UPDATE_START ,MODULE_UPDATE_SHUTDOWN , +MODULE_UPDATE_CPU_INSTALL ,MODULE_UPDATE_DONE , };
@@ -251,6 +262,9 @@ static int do_seamldr_install_module(void *seamldr_params)if (is_lead_cpu) ret =tdx_module_shutdown ();break ; +case MODULE_UPDATE_CPU_INSTALL : + ret =seamldr_install (seamldr_params); +break ;default :break ; }
Signature · 1 line
2.52.0
[tip: x86/tdx] x86/virt/seamldr: Install a new TDX module
The following commit has been merged into the x86/tdx branch of tip:
The following commit has been merged into the x86/tdx branch of tip:
Commit-ID: 2bfb2ef877f510bc6ebe8a74ce0877d290dc8bcd Gitweb: https://git.kernel.org/tip/2bfb2ef877f510bc6ebe8a74ce0877d290dc8bcd Author: Chao Gao <[email protected]> AuthorDate: Wed, 20 May 2026 15:29:08 -07:00 Committer: Dave Hansen <[email protected]> CommitterDate: Wed, 03 Jun 2026 08:59:43 -07:00
x86/virt/seamldr: Install a new TDX module
Continue fleshing out the update proces. The old module is shut down and the system is ready for the new module image. Run the SEAMLDR.INSTALL SEAMCALL on all CPUs.
--- arch/x86/virt/vmx/tdx/seamldr.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
arch/x86/virt/vmx/tdx/seamldr.c +14 -0
@@ -22,6 +22,7 @@/* P-SEAMLDR SEAMCALL leaf function */ #define P_SEAMLDR_INFO 0x8000000000000000 +#define P_SEAMLDR_INSTALL 0x8000000000000001#define SEAMLDR_MAX_NR_MODULE_PAGES 496#define SEAMLDR_MAX_NR_SIG_PAGES 1
@@ -89,6 +90,15 @@ int seamldr_get_info(struct seamldr_info *seamldr_info) }EXPORT_SYMBOL_FOR_MODULES (seamldr_get_info,"tdx-host" ); +/* Call into P-SEAMLDR to install a TDX module update */ +static int seamldr_install (const struct seamldr_params *params) +{ +struct tdx_module_args args = {}; + + args.rcx =__pa (params); +return seamldr_call (P_SEAMLDR_INSTALL , &args); +} +#define TDX_IMAGE_VERSION_2 0x200/* First page of the on-disk module update image: */
@@ -198,6 +208,7 @@ static int init_seamldr_params(struct seamldr_params *params,enum module_update_state {MODULE_UPDATE_START ,MODULE_UPDATE_SHUTDOWN , +MODULE_UPDATE_CPU_INSTALL ,MODULE_UPDATE_DONE , };
@@ -273,6 +284,9 @@ static int do_seamldr_install_module(void *seamldr_params)if (is_lead_cpu) ret =tdx_module_shutdown ();break ; +case MODULE_UPDATE_CPU_INSTALL : + ret =seamldr_install (seamldr_params); +break ;default :break ; }
PATCH v10 19/25 x86/virt/seamldr: Do TDX global and per-CPU init after module installation
In reply to Chao GaoAfter installing a new TDX module, the kernel must re-initialize TDX before resuming TDX operations.
This post-update initialization differs from the initial boot-time initialization. It only needs TDX global initialization, TDX per-CPU initialization, and restoration of TDX state from the handoff data.
tdx_cpu_enable() covers the global and per-CPU initialization. Export it and invoke it on all CPUs.
--- arch/x86/include/asm/tdx.h | 1 + arch/x86/virt/vmx/tdx/seamldr.c | 4 ++++ arch/x86/virt/vmx/tdx/tdx.c | 2 +- 3 files changed, 6 insertions(+), 1 deletion(-)
arch/x86/include/asm/tdx.h +1 -0
@@ -107,6 +107,7 @@ static inline long tdx_kvm_hypercall(unsigned int nr, unsigned long p1,#ifdef CONFIG_INTEL_TDX_HOST void tdx_init (void ); +int tdx_cpu_enable (void );const char *tdx_dump_mce_info (struct mce *m);const struct tdx_sys_info *tdx_get_sysinfo (void );
arch/x86/virt/vmx/tdx/seamldr.c +4 -0
@@ -188,6 +188,7 @@ enum module_update_state {MODULE_UPDATE_START ,MODULE_UPDATE_SHUTDOWN ,MODULE_UPDATE_CPU_INSTALL , +MODULE_UPDATE_CPU_INIT , MODULE_UPDATE_DONE, };
@@ -265,6 +266,9 @@ static int do_seamldr_install_module(void *seamldr_params)case MODULE_UPDATE_CPU_INSTALL : ret =seamldr_install (seamldr_params);break ; +case MODULE_UPDATE_CPU_INIT : + ret =tdx_cpu_enable (); +break ;default :break ; }
arch/x86/virt/vmx/tdx/tdx.c +1 -1
@@ -113,7 +113,7 @@ static int try_init_module_global(void)* (and TDX module global initialization SEAMCALL if not done) on local cpu to * make this cpu be ready to run any other SEAMCALLs. */ -static int tdx_cpu_enable (void ) +int tdx_cpu_enable (void ) {struct tdx_module_args args = {};int ret;
Signature · 1 line
2.52.0
[tip: x86/tdx] x86/virt/seamldr: Initialize the newly-installed TDX module
The following commit has been merged into the x86/tdx branch of tip:
The following commit has been merged into the x86/tdx branch of tip:
Commit-ID: bf7c0ed2c3621f4f0bf56efcd95e5a5372bf0ca6 Gitweb: https://git.kernel.org/tip/bf7c0ed2c3621f4f0bf56efcd95e5a5372bf0ca6 Author: Chao Gao <[email protected]> AuthorDate: Wed, 20 May 2026 15:29:09 -07:00 Committer: Dave Hansen <[email protected]> CommitterDate: Wed, 03 Jun 2026 08:59:43 -07:00
x86/virt/seamldr: Initialize the newly-installed TDX module
Continue fleshing out the update process. At this point the new module is sitting in memory but has never been called and is not usable. It is in a similar state to the when the system first boots.
Leave the P-SEAMLDR behind. Stop making calls to it. Transition to calling the new TDX module itself to set up both global and per-cpu state.
Share tdx_cpu_enable() with the fresh-boot module initialization code. Export it and invoke it on all CPUs.
Note: "TDX global initialization" needs to be done once before "TDX per-CPU initialization". It would be a great fit for the new runtime update "is_lead_cpu" logic. But tdx_cpu_enable() already has some logic to do the global initialization properly. Just use it directly to maximize fresh-boot and runtime update code sharing.
== Background ==
The boot-time and post-update initialization flows share the same first steps:
- TDX global initialization
- TDX per-CPU initialization
After that, they diverge:
- Fresh boot:
Prepare TDMRs/PAMTs
Configure the TDX module
Configure the global KeyID
Initialize TDMRs
- Runtime update:
Restore TDX module state from handoff data
Future changes will consume the handoff data.
[ dhansen: major changelog munging ]
--- arch/x86/include/asm/tdx.h | 1 + arch/x86/virt/vmx/tdx/seamldr.c | 4 ++++ arch/x86/virt/vmx/tdx/tdx.c | 2 +- 3 files changed, 6 insertions(+), 1 deletion(-)
arch/x86/include/asm/tdx.h +1 -0
@@ -107,6 +107,7 @@ static inline long tdx_kvm_hypercall(unsigned int nr, unsigned long p1,#ifdef CONFIG_INTEL_TDX_HOST void tdx_init (void ); +int tdx_cpu_enable (void );const char *tdx_dump_mce_info (struct mce *m);const struct tdx_sys_info *tdx_get_sysinfo (void );
arch/x86/virt/vmx/tdx/seamldr.c +4 -0
@@ -209,6 +209,7 @@ enum module_update_state {MODULE_UPDATE_START ,MODULE_UPDATE_SHUTDOWN ,MODULE_UPDATE_CPU_INSTALL , +MODULE_UPDATE_CPU_INIT , MODULE_UPDATE_DONE, };
@@ -287,6 +288,9 @@ static int do_seamldr_install_module(void *seamldr_params)case MODULE_UPDATE_CPU_INSTALL : ret =seamldr_install (seamldr_params);break ; +case MODULE_UPDATE_CPU_INIT : + ret =tdx_cpu_enable (); +break ;default :break ; }
arch/x86/virt/vmx/tdx/tdx.c +1 -1
@@ -113,7 +113,7 @@ out:* (and TDX module global initialization SEAMCALL if not done) on local cpu to * make this cpu be ready to run any other SEAMCALLs. */ -static int tdx_cpu_enable (void ) +int tdx_cpu_enable (void ) {struct tdx_module_args args = {};int ret;
PATCH v10 20/25 x86/virt/tdx: Restore TDX module state
In reply to Chao GaoTDX module state was packed as handoff data during module shutdown. After per-CPU initialization, the new module can restore TDX module state from handoff data to preserve running TDs.
Once the restoration is done, the TDX module update is complete, which means the new module is ready to handle requests from the host and guests.
Implement the new TDH.SYS.UPDATE SEAMCALL to restore TDX module state and invoke it on one CPU since it only needs to be called once.
For error handling, Intel® Trust Domain Extensions (Intel® TDX) Module Base Architecture Specification, Chapter "Restore TDX Module State after a TD-Preserving Update" states
If TDH.SYS.UPDATE returns an error, then the host VMM can continue with the non-update sequence (TDH.SYS.CONFIG, TDH.SYS.KEY.CONFIG etc.). In this case all existing TDs are lost. Alternatively, the host VMM can request the P-SEAMLDR to update to another TDX module. If that update is successful, existing TDs are preserved.
No error is expected if the new module is fully compatible, and userspace should enforce that as part of update image selection. Given the complexity and uncertain value of the recovery paths above, simply propagate errors.
Note: the location and the format of handoff data is defined by the TDX module. The new module knows where to get handoff data and how to parse it. The kernel doesn't need to provide its location, format etc.
--- arch/x86/virt/vmx/tdx/seamldr.c | 5 +++++ arch/x86/virt/vmx/tdx/tdx.c | 13 +++++++++++++ arch/x86/virt/vmx/tdx/tdx.h | 2 ++ 3 files changed, 20 insertions(+)
arch/x86/virt/vmx/tdx/seamldr.c +5 -0
@@ -189,6 +189,7 @@ enum module_update_state {MODULE_UPDATE_SHUTDOWN ,MODULE_UPDATE_CPU_INSTALL ,MODULE_UPDATE_CPU_INIT , +MODULE_UPDATE_RUN_UPDATE , MODULE_UPDATE_DONE, };
@@ -269,6 +270,10 @@ static int do_seamldr_install_module(void *seamldr_params)case MODULE_UPDATE_CPU_INIT : ret =tdx_cpu_enable ();break ; +case MODULE_UPDATE_RUN_UPDATE : +if (is_lead_cpu) + ret =tdx_module_run_update (); +break ;default :break ; }
arch/x86/virt/vmx/tdx/tdx.c +13 -0
@@ -1305,6 +1305,19 @@ int tdx_module_shutdown(void)return 0 ; } +int tdx_module_run_update (void ) +{ +struct tdx_module_args args = {}; +int ret; + + ret =seamcall_prerr (TDH_SYS_UPDATE , &args); +if (ret) +return ret; + + tdx_module_state.initialized = true; +return 0 ; +} +static bool is_pamt_page (unsigned long phys) {struct tdmr_info_list *tdmr_list = &tdx_tdmr_list;
arch/x86/virt/vmx/tdx/tdx.h +2 -0
@@ -47,6 +47,7 @@#define TDH_VP_WR 43#define TDH_SYS_CONFIG 45#define TDH_SYS_SHUTDOWN 52 +#define TDH_SYS_UPDATE 53#define TDH_SYS_DISABLE 69/*
@@ -110,5 +111,6 @@ struct tdmr_info_list { };int tdx_module_shutdown (void ); +int tdx_module_run_update (void );#endif
Signature · 1 line
2.52.0
[tip: x86/tdx] x86/virt/tdx: Restore TDX module state
The following commit has been merged into the x86/tdx branch of tip:
The following commit has been merged into the x86/tdx branch of tip:
Commit-ID: b344c50a1ad4230c86bcc080a08ee65e8c944627 Gitweb: https://git.kernel.org/tip/b344c50a1ad4230c86bcc080a08ee65e8c944627 Author: Chao Gao <[email protected]> AuthorDate: Wed, 20 May 2026 15:29:10 -07:00 Committer: Dave Hansen <[email protected]> CommitterDate: Wed, 03 Jun 2026 08:59:43 -07:00
x86/virt/tdx: Restore TDX module state
After per-CPU initialization, the module is nearly functional. It is in a similar state to TDX initialization before TDH.SYS.CONFIG.
At this point, the kernel _could_ just repeat the boot-time sequence, but that would land the new module in a slightly different state than the old module. This would leave old TDs unrunnable, which is not a good outcome.
Thankfully, the "handoff" data saved during module shutdown should contain all the information needed to restore the TDX module state to exactly what it was before the update.
Restore TDX module state. The TDX module only needs a single copy so only do this on the lead CPU.
Restoration errors can theoretically be handled in a few ways. For instance, userspace could try to load a different TDX module version. Or, the kernel could give up on the handoff process and just reinitialize the new module from scratch, which would lose all existing TDs.
Simply propagate errors to userspace. Ignore the idea of a TD-destroying reinitialization. It would destroy data like a reboot and if things have gone that wrong a reboot is probably the best option anyway.
Note: the location and the format of handoff data is defined by the TDX module. The new module knows where to get handoff data and how to parse it. The kernel does not touch it at all.
--- arch/x86/virt/vmx/tdx/seamldr.c | 5 +++++ arch/x86/virt/vmx/tdx/tdx.c | 13 +++++++++++++ arch/x86/virt/vmx/tdx/tdx.h | 2 ++ 3 files changed, 20 insertions(+)
arch/x86/virt/vmx/tdx/seamldr.c +5 -0
@@ -210,6 +210,7 @@ enum module_update_state {MODULE_UPDATE_SHUTDOWN ,MODULE_UPDATE_CPU_INSTALL ,MODULE_UPDATE_CPU_INIT , +MODULE_UPDATE_RUN_UPDATE , MODULE_UPDATE_DONE, };
@@ -291,6 +292,10 @@ static int do_seamldr_install_module(void *seamldr_params)case MODULE_UPDATE_CPU_INIT : ret =tdx_cpu_enable ();break ; +case MODULE_UPDATE_RUN_UPDATE : +if (is_lead_cpu) + ret =tdx_module_run_update (); +break ;default :break ; }
arch/x86/virt/vmx/tdx/tdx.c +13 -0
@@ -1312,6 +1312,19 @@ int tdx_module_shutdown(void)return 0 ; } +int tdx_module_run_update (void ) +{ +struct tdx_module_args args = {}; +int ret; + + ret =seamcall_prerr (TDH_SYS_UPDATE , &args); +if (ret) +return ret; + + tdx_module_state.initialized = true; +return 0 ; +} +static bool is_pamt_page (unsigned long phys) {struct tdmr_info_list *tdmr_list = &tdx_tdmr_list;
arch/x86/virt/vmx/tdx/tdx.h +2 -0
@@ -47,6 +47,7 @@#define TDH_VP_WR 43#define TDH_SYS_CONFIG 45#define TDH_SYS_SHUTDOWN 52 +#define TDH_SYS_UPDATE 53#define TDH_SYS_DISABLE 69/*
@@ -110,5 +111,6 @@ struct tdmr_info_list { };int tdx_module_shutdown (void ); +int tdx_module_run_update (void );#endif
PATCH v10 21/25 x86/virt/tdx: Refresh TDX module version after update
In reply to Chao GaoThe kernel exposes the TDX module version through sysfs so userspace can check update compatibility. That information needs to remain accurate across runtime updates.
A runtime update may change the module's update_version, so refresh the cached version right after a successful update.
Drop __ro_after_init from tdx_sysinfo because it is now updated at runtime.
Do not refresh the rest of tdx_sysinfo, even if some values change across updates. TDX module updates are backward compatible, so existing tdx_sysinfo consumers, such as KVM, can continue to operate without seeing the new values.
Refreshing the full structure would be risky. A tdx_sysinfo consumer may initialize its TDX support based on the features originally reported in tdx_sysinfo. If a runtime update adds new features and the full structure is refreshed, that consumer could observe and use the newly reported features without having performed the setup required to use them safely.
---
v9:
- don't print old and new version [Dave]
- explain why it's OK to hide changes from the tdx_sysinfo users [Dave]
- update versions in stop_machine context
- don't mention major/minor versions are idential across updates. That fact is not relevant here.
--- arch/x86/virt/vmx/tdx/tdx.c | 6 +++++- arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-)
arch/x86/virt/vmx/tdx/tdx.c +5 -1
@@ -67,7 +67,7 @@ static struct tdmr_info_list tdx_tdmr_list;/* All TDX-usable memory regions. Protected by mem_hotplug_lock. */ static LIST_HEAD (tdx_memlist); -static struct tdx_sys_info tdx_sysinfo__ro_after_init ; +static struct tdx_sys_info tdx_sysinfo;static DEFINE_RAW_SPINLOCK (sysinit_lock);
@@ -1314,6 +1314,10 @@ int tdx_module_run_update(void)if (ret)return ret; +/* Shouldn't fail as the update has succeeded. */ + ret =get_tdx_sys_info_version (&tdx_sysinfo.version); +WARN_ON_ONCE (ret); + tdx_module_state.initialized = true;return 0 ; }
arch/x86/virt/vmx/tdx/tdx_global_metadata.c +1 -1
@@ -7,7 +7,7 @@* Include this file to other C file instead. */ -static __init int get_tdx_sys_info_version (struct tdx_sys_info_version *sysinfo_version) +static int get_tdx_sys_info_version (struct tdx_sys_info_version *sysinfo_version) {int ret =0 ;u64 val;
Signature · 1 line
2.52.0
Kiryl Shutsemau
maintainerRe: [PATCH v10 21/25] x86/virt/tdx: Refresh TDX module version after update
Quoted 8 lines · Chao Gao
The kernel exposes the TDX module version through sysfs so userspace can check update compatibility. That information needs to remain accurate across runtime updates.
A runtime update may change the module's
update_version, so refresh the cached version right after a successful update.Drop
__ro_after_initfromtdx_sysinfobecause it is now updated at runtime.
__read_mostly?
Quoted 39 lines
Do not refresh the rest of
tdx_sysinfo, even if some values change across updates. TDX module updates are backward compatible, so existingtdx_sysinfoconsumers, such as KVM, can continue to operate without seeing the new values.Refreshing the full structure would be risky. A
tdx_sysinfoconsumer may initialize its TDX support based on the features originally reported intdx_sysinfo. If a runtime update adds new features and the full structure is refreshed, that consumer could observe and use the newly reported features without having performed the setup required to use them safely.Signed-off-by Chao Gao <[email protected]>Reviewed-by Rick Edgecombe <[email protected]>---
v9:
- don't print old and new version [Dave]
- explain why it's OK to hide changes from thetdx_sysinfousers [Dave]
- update versions instop_machinecontext
- don't mention major/minor versions are idential across updates. That fact is not relevant here.--- arch/x86/virt/vmx/tdx/tdx.c | 6 +++++- arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-)arch/x86/virt/vmx/tdx/tdx.c +1
-1@@ -67,7 +67,7 @@ static struct tdmr_info_list tdx_tdmr_list;/* All TDX-usable memory regions. Protected by mem_hotplug_lock. */ static LIST_HEAD (tdx_memlist); -static struct tdx_sys_info tdx_sysinfo__ro_after_init ; +static struct tdx_sys_info tdx_sysinfo;static DEFINE_RAW_SPINLOCK (sysinit_lock);
arch/x86/virt/vmx/tdx/tdx.c int tdx_module_run_update(void) · line 1320
Quoted 7 lines
arch/x86/virt/vmx/tdx/tdx.c +4
-0@@ -1314,6 +1314,10 @@ int tdx_module_run_update(void)if (ret)return ret; +/* Shouldn't fail as the update has succeeded. */ + ret =get_tdx_sys_info_version (&tdx_sysinfo.version); +WARN_ON_ONCE (ret); +
Warn, but pretend that everything is fine?
Quoted 19 lines
arch/x86/virt/vmx/tdx/tdx.c +0
-0tdx_module_state.initialized = true;return 0 ; }arch/x86/virt/vmx/tdx/tdx_global_metadata.c +1
-1@@ -7,7 +7,7 @@* Include this file to other C file instead. */ -static __init int get_tdx_sys_info_version (struct tdx_sys_info_version *sysinfo_version) +static int get_tdx_sys_info_version (struct tdx_sys_info_version *sysinfo_version) {int ret =0 ;u64 val;Signature · 1 line
2.52.0
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
[tip: x86/tdx] x86/virt/tdx: Refresh TDX module version after update
In reply to Chao GaoThe following commit has been merged into the x86/tdx branch of tip:
The following commit has been merged into the x86/tdx branch of tip:
Commit-ID: 856bc8bb990e21546fadff83502cdad33660a18e Gitweb: https://git.kernel.org/tip/856bc8bb990e21546fadff83502cdad33660a18e Author: Chao Gao <[email protected]> AuthorDate: Wed, 20 May 2026 15:29:12 -07:00 Committer: Dave Hansen <[email protected]> CommitterDate: Wed, 03 Jun 2026 08:59:44 -07:00
x86/virt/tdx: Refresh TDX module version after update
The kernel exposes the TDX module version through sysfs so userspace can check update compatibility. That information needs to remain accurate across runtime updates.
A runtime update may change the module's update_version, so refresh the cached version right after a successful update.
Drop __ro_after_init from tdx_sysinfo because it is now updated at runtime.
Do not refresh the rest of tdx_sysinfo, even if some values change across updates. TDX module updates are backward compatible, so existing tdx_sysinfo consumers, such as KVM, can continue to operate without seeing the new values.
[ dhansen: trim changelog ]
--- arch/x86/virt/vmx/tdx/tdx.c | 9 ++++++++- arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-)
arch/x86/virt/vmx/tdx/tdx.c +8 -1
@@ -67,7 +67,7 @@ static struct tdmr_info_list tdx_tdmr_list;/* All TDX-usable memory regions. Protected by mem_hotplug_lock. */ static LIST_HEAD (tdx_memlist); -static struct tdx_sys_info tdx_sysinfo__ro_after_init ; +static struct tdx_sys_info tdx_sysinfo;static DEFINE_RAW_SPINLOCK (sysinit_lock);
@@ -1321,6 +1321,13 @@ int tdx_module_run_update(void)if (ret)return ret; + ret =get_tdx_sys_info_version (&tdx_sysinfo.version); +/* +* Only fails if there is something unexpected +* and severely wrong with the module. +*/ +WARN_ON_ONCE (ret); + tdx_module_state.initialized = true;return 0 ; }
arch/x86/virt/vmx/tdx/tdx_global_metadata.c +1 -1
@@ -7,7 +7,7 @@* Include this file to other C file instead. */ -static __init int get_tdx_sys_info_version (struct tdx_sys_info_version *sysinfo_version) +static int get_tdx_sys_info_version (struct tdx_sys_info_version *sysinfo_version) {int ret =0 ;u64 val;
PATCH v10 22/25 x86/virt/tdx: Reject updates during compatibility-sensitive operations
In reply to Chao GaoA TDX module erratum can cause TD state corruption if a module update races with a compatibility-sensitive operation. For example, if an update races with TD build, the TD measurement hash may be corrupted, which can later cause attestation failure.
Handle this by requesting the TDX module to detect such races during TDH.SYS.SHUTDOWN and reject the update when one is found. Report the failure to userspace as -EBUSY so the update can be retried.
The downside is that module updates can be blocked indefinitely if compatibility-sensitive operations do not quiesce. In that case, userspace must resolve the conflict and retry the update.
Do not pre-check whether the TDX module supports this race-detection capability. If it does not, rely on the TDX module to reject module shutdown.
== Alternatives ==
Two alternatives were considered and rejected [1]:
a. Fail TD build when the race occurs. This would complicate KVM error
handling and risk KVM uABI instability.
b. Allow the issue to leak through. This would make the problem harder to
detect and recover from.
---
v10:
- Don't add a "dead" TDX_FEATURE0 bit [Sashiko]
- s/BIT/BIT_ULL
--- arch/x86/include/asm/tdx.h | 5 +++-- arch/x86/virt/vmx/tdx/tdx.c | 30 ++++++++++++++++++++++++--- drivers/virt/coco/tdx-host/tdx-host.c | 2 ++ 3 files changed, 32 insertions(+), 5 deletions(-)
arch/x86/include/asm/tdx.h +3 -2
@@ -29,8 +29,9 @@/* * TDX module SEAMCALL leaf function error codes */ -#define TDX_SUCCESS 0ULL -#define TDX_RND_NO_ENTROPY 0x8000020300000000ULL +#define TDX_SUCCESS 0ULL +#define TDX_RND_NO_ENTROPY 0x8000020300000000ULL +#define TDX_UPDATE_COMPAT_SENSITIVE 0x8000051200000000ULL/* Bit definitions of TDX_FEATURES0 metadata field */ #define TDX_FEATURES0_NO_RBP_MOD BIT_ULL(18)
arch/x86/virt/vmx/tdx/tdx.c +27 -3
@@ -1274,11 +1274,14 @@ static __init int tdx_enable(void) }subsys_initcall (tdx_enable); +#define TDX_SYS_SHUTDOWN_AVOID_COMPAT_SENSITIVE BIT_ULL(16) +int tdx_module_shutdown (void ) {struct tdx_sys_info_handoff handoff = {};struct tdx_module_args args = {};int ret, cpu; +u64 err; ret =get_tdx_sys_info_handoff (&handoff);WARN_ON_ONCE (ret);
@@ -1288,9 +1291,30 @@ int tdx_module_shutdown(void)* module can produce and most likely supported by newer modules. */ args.rcx = handoff.module_hv; - ret =seamcall_prerr (TDH_SYS_SHUTDOWN , &args); -if (ret) -return ret; + +/* +* This flag tells the TDX module to reject shutdown if it races +* with a "sensitive" ongoing operation. That eliminates exposure +* to a TDX erratum which can corrupt TDX guest states. +* +* This flag is not supported by all TDX modules and may cause +* the shutdown (and subsequent update procedure) to fail. +*/ + args.rcx |=TDX_SYS_SHUTDOWN_AVOID_COMPAT_SENSITIVE ; + + err =seamcall (TDH_SYS_SHUTDOWN , &args); + +/* +* The shutdown ran into a "sensitive" ongoing operation. Signal +* to userspace that it can retry. +*/ +if ((err &TDX_SEAMCALL_STATUS_MASK ) ==TDX_UPDATE_COMPAT_SENSITIVE ) +return -EBUSY ; + +if (err) { +seamcall_err (TDH_SYS_SHUTDOWN , err, &args); +return -EIO ; + }/* * Clear global and per-CPU initialization flags so the new module
drivers/virt/coco/tdx-host/tdx-host.c +2 -0
@@ -145,6 +145,8 @@ static enum fw_upload_err tdx_fw_write(struct fw_upload *fwl, const u8 *data,case 0 : *written = data_len;return FW_UPLOAD_ERR_NONE ; +case -EBUSY : +return FW_UPLOAD_ERR_BUSY ;default :return FW_UPLOAD_ERR_FW_INVALID ; }
Signature · 1 line
2.52.0
Re: [PATCH v10 22/25] x86/virt/tdx: Reject updates during compatibility-sensitive operations
Quoted 30 lines · Chao Gao
int
tdx_module_shutdown(void)
{arch/x86/virt/vmx/tdx/tdx.c +15
-3struct tdx_sys_info_handoff handoff = {};struct tdx_module_args args = {};int ret, cpu; +u64 err; ret =get_tdx_sys_info_handoff (&handoff);WARN_ON_ONCE (ret);@@ -1288,9 +1291,30 @@ int tdx_module_shutdown(void)* module can produce and most likely supported by newer modules. */ args.rcx = handoff.module_hv; - ret =seamcall_prerr (TDH_SYS_SHUTDOWN , &args); -if (ret) -return ret; + +/* +* This flag tells the TDX module to reject shutdown if it races +* with a "sensitive" ongoing operation. That eliminates exposure +* to a TDX erratum which can corrupt TDX guest states. +* +* This flag is not supported by all TDX modules and may cause +* the shutdown (and subsequent update procedure) to fail. +*/ + args.rcx |=TDX_SYS_SHUTDOWN_AVOID_COMPAT_SENSITIVE ; + + err =seamcall (TDH_SYS_SHUTDOWN , &args); + +/*
arch/x86/virt/vmx/tdx/tdx.c int tdx_module_shutdown(void) · line 1317
Quoted 10 lines
arch/x86/virt/vmx/tdx/tdx.c +10
-0+* The shutdown ran into a "sensitive" ongoing operation. Signal +* to userspace that it can retry. +*/ +if ((err &TDX_SEAMCALL_STATUS_MASK ) ==TDX_UPDATE_COMPAT_SENSITIVE ) +return -EBUSY ; + +if (err) { +seamcall_err (TDH_SYS_SHUTDOWN , err, &args); +return -EIO ; + }
This function is pretty tidy. More or less:
ret = get_tdx_sys_info_handoff(&handoff); if (ret) return args.foo = handoff.bar; ret = seamcall_prerr(TDH_SYS_SHUTDOWN, &args); if (ret) return memset(&tdx_module_state, 0, sizeof(tdx_module_state)); for_each_possible_cpu(cpu) per_cpu(tdx_lp_initialized, cpu) = false;
The logic's not bad, right? Get the handoff data, hand it off to something, then go set some fields.
Then what does this patch do? It goes and globs a just huge blob of TDH_SYS_SHUTDOWN errata handling and implementation details right smack in the middle. Our tidy little function is no more.
I really with this would trigger folks' gag reflexes. It's *SO* easy to fix. It's *so* easy to keep the code tidy and hide the dead bodies so that the logic can still be followed.
I'm probably going to drop this for now.
Re: [PATCH v10 22/25] x86/virt/tdx: Reject updates during compatibility-sensitive operations
Quoted 18 lines
This function is pretty tidy. More or less:
ret = get_tdx_sys_info_handoff(&handoff); if (ret) return args.foo = handoff.bar; ret = seamcall_prerr(TDH_SYS_SHUTDOWN, &args); if (ret) return memset(&tdx_module_state, 0, sizeof(tdx_module_state)); for_each_possible_cpu(cpu) per_cpu(tdx_lp_initialized, cpu) = false;The logic's not bad, right? Get the handoff data, hand it off to something, then go set some fields.
Quoted 7 lines
Then what does this patch do? It goes and globs a just huge blob of
TDH_SYS_SHUTDOWNerrata handling and implementation details right smack in the middle. Our tidy little function is no more.I really with this would trigger folks' gag reflexes. It's *SO* easy to fix. It's *so* easy to keep the code tidy and hide the dead bodies so that the logic can still be followed.
Apologies.
FWIW, we can add a tdh_sys_shutdown() helper and hide those details there.
From 987b7107d79e94d1d35be93bfc48cbeb9ce6741b Mon Sep 17 00:00:00 2001
From: Chao Gao <[email protected]>
Date: Tue, 31 Mar 2026 05:41:30 -0700
Subject: [PATCH] x86/virt/tdx: Reject updates during compatibility-sensitive
operations
A TDX module erratum can cause TD state corruption if a module update races with a compatibility-sensitive operation. For example, if an update races with TD build, the TD measurement hash may be corrupted, which can later cause attestation failure.
Handle this by requesting the TDX module to detect such races during TDH.SYS.SHUTDOWN and reject the update when one is found. Report the failure to userspace as -EBUSY so the update can be retried.
The downside is that module updates can be blocked indefinitely if compatibility-sensitive operations do not quiesce. In that case, userspace must resolve the conflict and retry the update.
Do not pre-check whether the TDX module supports this race-detection capability. If it does not, rely on the TDX module to reject module shutdown.
== Alternatives ==
Two alternatives were considered and rejected [1]:
a. Fail TD build when the race occurs. This would complicate KVM error
handling and risk KVM uABI instability.
b. Allow the issue to leak through. This would make the problem harder to
detect and recover from.
---
v10:
- Don't add a "dead" TDX_FEATURE0 bit [Sashiko]
- s/BIT/BIT_ULL
--- arch/x86/include/asm/tdx.h | 5 ++-- arch/x86/virt/vmx/tdx/tdx.c | 34 ++++++++++++++++++++++++++- drivers/virt/coco/tdx-host/tdx-host.c | 2 ++ 3 files changed, 38 insertions(+), 3 deletions(-)
arch/x86/include/asm/tdx.h +3 -2
@@ -29,8 +29,9 @@/* * TDX module SEAMCALL leaf function error codes */ -#define TDX_SUCCESS 0ULL -#define TDX_RND_NO_ENTROPY 0x8000020300000000ULL +#define TDX_SUCCESS 0ULL +#define TDX_RND_NO_ENTROPY 0x8000020300000000ULL +#define TDX_UPDATE_COMPAT_SENSITIVE 0x8000051200000000ULL/* Bit definitions of TDX_FEATURES0 metadata field */ #define TDX_FEATURES0_TD_PRESERVING BIT_ULL(1)
arch/x86/virt/vmx/tdx/tdx.c +32 -0
@@ -1274,6 +1274,38 @@ static __init int tdx_enable(void) }subsys_initcall (tdx_enable); +#define TDX_SYS_SHUTDOWN_AVOID_COMPAT_SENSITIVE BIT_ULL(16) + +static int tdh_sys_shutdown (struct tdx_module_args *args) +{ +u64 err; + +/* +* This flag tells the TDX module to reject shutdown if it races +* with a "sensitive" ongoing operation. That eliminates exposure +* to a TDX erratum which can corrupt TDX guest states. +* +* This flag is not supported by all TDX modules and may cause +* the shutdown (and subsequent update procedure) to fail. +*/ + args->rcx |=TDX_SYS_SHUTDOWN_AVOID_COMPAT_SENSITIVE ; + + err =seamcall (TDH_SYS_SHUTDOWN , args); +/* +* The shutdown ran into a "sensitive" ongoing operation. Signal +* to userspace that it can retry. +*/ +if ((err &TDX_SEAMCALL_STATUS_MASK ) ==TDX_UPDATE_COMPAT_SENSITIVE ) +return -EBUSY ; + +if (err) { +seamcall_err (TDH_SYS_SHUTDOWN , err, args); +return -EIO ; + } + +return 0 ; +} +int tdx_module_shutdown (void ) {
struct tdx_sys_info_handoff handoff = {};
@@ -1295,7 +1327,7 @@ int tdx_module_shutdown(void)
*/
args.rcx = handoff.module_hv;- ret = seamcall_prerr(TDH_SYS_SHUTDOWN, &args);
+ ret = tdh_sys_shutdown(&args); if (ret) return ret;
drivers/virt/coco/tdx-host/tdx-host.c +0 -0
@@ -137,6 +137,8 @@ static enum fw_upload_err tdx_fw_write(struct fw_upload *fwl, const u8 *data,
case 0: *written = data_len; return FW_UPLOAD_ERR_NONE; + case -EBUSY: + return FW_UPLOAD_ERR_BUSY; default: return FW_UPLOAD_ERR_FW_INVALID; }
Signature · 1 line
2.52.0
PATCH v10 23/25 x86/virt/tdx: Enable TDX module runtime updates
In reply to Chao GaoAll pieces of TDX module runtime updates are in place. Enable it if it is supported.
--- arch/x86/include/asm/tdx.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/include/asm/tdx.h +2 -2
@@ -34,6 +34,7 @@#define TDX_UPDATE_COMPAT_SENSITIVE 0x8000051200000000ULL/* Bit definitions of TDX_FEATURES0 metadata field */ +#define TDX_FEATURES0_TD_PRESERVING BIT_ULL(1)#define TDX_FEATURES0_NO_RBP_MOD BIT_ULL(18)#ifndef __ASSEMBLER__
@@ -114,8 +115,7 @@ const struct tdx_sys_info *tdx_get_sysinfo(void);static inline bool tdx_supports_runtime_update (const struct tdx_sys_info *sysinfo) { -/* To be enabled when kernel is ready. */ -return false; +return sysinfo->features.tdx_features0 &TDX_FEATURES0_TD_PRESERVING ; }int tdx_guest_keyid_alloc (void );
Signature · 1 line
2.52.0
[tip: x86/tdx] x86/virt/tdx: Enable TDX module runtime updates
The following commit has been merged into the x86/tdx branch of tip:
The following commit has been merged into the x86/tdx branch of tip:
Commit-ID: 1e35f5945db9630c0602d9a9a7bbd146a96d13cc Gitweb: https://git.kernel.org/tip/1e35f5945db9630c0602d9a9a7bbd146a96d13cc Author: Chao Gao <[email protected]> AuthorDate: Wed, 20 May 2026 15:29:13 -07:00 Committer: Dave Hansen <[email protected]> CommitterDate: Wed, 03 Jun 2026 08:59:44 -07:00
x86/virt/tdx: Enable TDX module runtime updates
All pieces of TDX module runtime updates are in place. Enable it if it is supported.
--- arch/x86/include/asm/tdx.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/include/asm/tdx.h +2 -2
@@ -33,6 +33,7 @@#define TDX_RND_NO_ENTROPY 0x8000020300000000ULL/* Bit definitions of TDX_FEATURES0 metadata field */ +#define TDX_FEATURES0_TD_PRESERVING BIT_ULL(1)#define TDX_FEATURES0_NO_RBP_MOD BIT_ULL(18)#ifndef __ASSEMBLER__
@@ -113,8 +114,7 @@ const struct tdx_sys_info *tdx_get_sysinfo(void);static inline bool tdx_supports_runtime_update (const struct tdx_sys_info *sysinfo) { -/* To be enabled when kernel is ready. */ -return false; +return sysinfo->features.tdx_features0 &TDX_FEATURES0_TD_PRESERVING ; }int tdx_guest_keyid_alloc (void );
PATCH v10 24/25 coco/tdx-host: Document TDX module update compatibility criteria
In reply to Chao GaoThe TDX module update protocol facilitates compatible runtime updates.
Document the compatibility criteria and indicators of update failures.
Note that runtime TDX module updates are an "update at your own risk" operation; userspace is responsible for ensuring that the update meets the compatibility criteria.
---
v9:
- Reword the update error descriptions.
--- .../ABI/testing/sysfs-devices-faux-tdx-host | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+)
Documentation/ABI/testing/sysfs-devices-faux-tdx-host +40 -0
@@ -24,3 +24,43 @@ Description: (RO) Report the number of remaining updates. TDX maintains a See Intel® Trust Domain Extensions - SEAM Loader (SEAMLDR) Interface Specification, Chapter "SEAMLDR_INFO" and Chapter "SEAMLDR.INSTALL" for more information. + +What: /sys/devices/faux/tdx_host/firmware/tdx_module +Contact: [email protected] +Description: (Directory) The tdx_module directory implements the fw_upload + sysfs ABI, see Documentation/ABI/testing/sysfs-class-firmware + for the general description of the attributes @data, @cancel, + @error, @loading, @remaining_size, and @status. This ABI + facilitates "Compatible TDX module Updates". A compatible update + is one that meets the following criteria: + + Does not interrupt or interfere with any current TDX + operation or TD VM. + + Does not invalidate any previously consumed module metadata + values outside of the TEE_TCB_SVN_2 field (updated Security + Version Number) in TD Quotes. + + Does not require validation of new module metadata fields. By + implication, new module features and capabilities are only + available by installing the module at reboot (BIOS or EFI + helper loaded). + + See tdx_host/firmware/tdx_module/error for information on + update failure indicators. + +What: /sys/devices/faux/tdx_host/firmware/tdx_module/error +Contact: [email protected] +Description: (RO) See Documentation/ABI/testing/sysfs-class-firmware for + baseline expectations for this file. The <ERROR> part in the + <STATUS>:<ERROR> format can be: + + "device-busy": The update conflicted with an ongoing + compatibility-sensitive operation. + + "firmware-invalid": The update failed for any other reason. + + "firmware-invalid" may be fatal, causing all TDs and the TDX + module to be lost and preventing further TDX operations. This + occurs when reading /sys/devices/faux/tdx_host/version returns + -ENXIO.
Signature · 1 line
2.52.0
Re: [PATCH v10 24/25] coco/tdx-host: Document TDX module update compatibility criteria
Quoted 3 lines · Chao Gao
Note that runtime TDX module updates are an "update at your own risk" operation; userspace is responsible for ensuring that the update meets the compatibility criteria.
I feel like this was copied right out of some TDX powerpoint presentation. It's really hard to parse.
What does any of this mean to end users?
Who are they depending on getting this all right?
If it goes wrong, what happens?
This needs to be from the user's perspective.
PATCH v10 25/25 x86/virt/tdx: Document TDX module update
In reply to Chao GaoDocument TDX module update as a subsection of "TDX Host Kernel Support" to provide background information and cover key points that developers and users may need to know, for example:
- update is done in stop_machine() context
- update instructions and results
- update policy and tooling
--- Documentation/arch/x86/tdx.rst | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+)
Documentation/arch/x86/tdx.rst +34 -0
@@ -73,6 +73,40 @@ initialize:: [..] virt/tdx: TDX-Module initialization failed ... +TDX module Runtime Update +------------------------- + +The TDX architecture includes a persistent SEAM loader (P-SEAMLDR) that +runs in SEAM mode separately from the TDX module. The kernel can +communicate with P-SEAMLDR to perform runtime updates of the TDX module. + +During updates, the TDX module becomes unresponsive to other TDX +operations. To prevent components using TDX (such as KVM) from +experiencing unexpected errors during updates, updates are performed in +stop_machine() context. + +TDX module updates have complex compatibility requirements; the new module +must be compatible with the current CPU, P-SEAMLDR, and running TDX module. +Rather than implementing complex module selection and policy enforcement +logic in the kernel, userspace is responsible for auditing and selecting +appropriate updates. + +Updates use the standard firmware upload interface. See +Documentation/driver-api/firmware/fw_upload.rst for detailed instructions. + +If updates failed, running TDs may be killed and further TDX operations may +not be possible until reboot. For detailed error information, see +Documentation/ABI/testing/sysfs-devices-faux-tdx-host. + +Given the risk of losing existing TDs, userspace should verify that the +update is compatible with the current system and properly validated before +applying it. + +A reference userspace tool that implements necessary checks is available +at: + + https://github.com/intel/tdx-module-binaries + TDX Interaction to Other Kernel Components ------------------------------------------
Signature · 1 line
2.52.0
Quoted 17 lines · Chao Gao
Hi Dave & Rick,
Thanks for your thorough review of v9. This v10 addresses the issues you pointed out. The main changes in this version are polishing changelogs and variable renames to improve readability. Specifically:
- Patches 1-2 (new): Split the original "Consolidate TDX global initialization states" into two steps — first move the statics to file scope, then clarify the result-caching logic in
try_init_module_global().
- Patch 6: Removed user-facingKconfighelp text forTDX_HOST_SERVICES(now a silent tristate auto-selected byINTEL_TDX_HOST).
- Patch 13: Renamed "size" to "data_len" inseamldr_install_module()andinit_seamldr_params(); renamed "HEADER_SIZE" to "TDX_IMAGE_HEADER_SIZE"; renamed "primary" to "is_lead_cpu" in the update state machine.
- Patch 13: Added earlydata_lenvalidation and explicit bounds checks
Quoted 8 lines
on sigstruct_nr_pages/module_nr_pages against SEAMLDR_MAX_NR_* limits, removing the implicit clamping in
populate_pa_list().
- Patch 22: Fixed BIT(16) ->BIT_ULL(16)forTDX_SYS_SHUTDOWN_AVOID_COMPAT_SENSITIVE.
- Patch 22: Removed unusedTDX_FEATURES0_UPDATE_COMPATdefinition.
- Various patches: Shortened sysfs ABI descriptions, tightened comments acrossseamldr.handseamldr.c, and minor style fixes (return 0 -> return false, unfolded conditionals)
FYI, below is the diff between v9 and v10:
Documentation/ABI/testing/sysfs-devices-faux-tdx-host +6 -8
@@ -1,16 +1,14 @@ What: /sys/devices/faux/tdx_host/version Contact: [email protected] -Description: (RO) Report the version of the loaded TDX module. The TDX module - version is formatted as x.y.z, where "x" is the major version, - "y" is the minor version and "z" is the update version. Versions - are used for bug reporting, TDX module updates etc. +Description: (RO) Report the version of the loaded TDX module. + Formatted as "major.minor.update". Used by TDX module + update tooling. Example: "1.2.03". What: /sys/devices/faux/tdx_host/seamldr_version Contact: [email protected] -Description: (RO) Report the version of the loaded P-SEAMLDR. The P-SEAMLDR - version is formatted as x.y.z, where "x" is the major version, - "y" is the minor version and "z" is the update version. Versions - are used for bug reporting and compatibility checks. +Description: (RO) Report the version of the loaded P-SEAMLDR. + Formatted as a TDX module version. Used by TDX module + update tooling. What: /sys/devices/faux/tdx_host/num_remaining_updates Contact: [email protected]
arch/x86/include/asm/seamldr.h +3 -4
@@ -5,11 +5,10 @@#include <linux/types.h> /* -* This is called the "SEAMLDR_INFO" data structure and is defined -* in "SEAM Loader (SEAMLDR) Interface Specification". +* This is the "SEAMLDR_INFO" data structure defined in the +* "SEAM Loader (SEAMLDR) Interface Specification". * -* The SEAMLDR.INFO documentation requires this to be aligned to a -* 256-byte boundary. +* Must be aligned to a 256-byte boundary. */ struct seamldr_info {
u32 version;
@@ -32,6 +31,6 @@ struct seamldr_info {
static_assert(sizeof(struct seamldr_info) == 256);
int seamldr_get_info(struct seamldr_info *seamldr_info);
-int seamldr_install_module(const u8 *data, u32 size); +int seamldr_install_module(const u8 *data, u32 data_len);
#endif /* _ASM_X86_SEAMLDR_H */
arch/x86/include/asm/tdx.h +0 -1
@@ -36,7 +36,6 @@/* Bit definitions of TDX_FEATURES0 metadata field */ #define TDX_FEATURES0_TD_PRESERVING BIT_ULL(1)#define TDX_FEATURES0_NO_RBP_MOD BIT_ULL(18) -#define TDX_FEATURES0_UPDATE_COMPAT BIT_ULL(47)#ifndef __ASSEMBLER__
arch/x86/virt/vmx/tdx/seamldr.c +5 -3
@@ -32,10 +32,12 @@#define SEAMLDR_SCENARIO_UPDATE 1/* -* This is called the "SEAMLDR_PARAMS" data structure and is defined -* in "SEAM Loader (SEAMLDR) Interface Specification". +* This is the "SEAMLDR_PARAMS" data structure defined in the +* "SEAM Loader (SEAMLDR) Interface Specification". * -* It describes the TDX module that will be installed. +* It is the in-memory ABI that the kernel passes to the P-SEAMLDR +* to update the TDX module. It breaks the TDX module image up in +* page-size pieces. */ struct seamldr_params {
u32 version;
@@ -87,7 +89,7 @@ static int seamldr_install(const struct seamldr_params *params)
#define TDX_IMAGE_VERSION_2 0x200
struct tdx_image_header {
- u16 version; // This ABI is always 0x200
+ u16 version;
u16 checksum;
u8 signature[8];
u32 sigstruct_nr_pages;
@@ -95,23 +97,28 @@ struct tdx_image_header {
u8 reserved[4076]; } __packed;
-#define HEADER_SIZE sizeof(struct tdx_image_header) -static_assert(HEADER_SIZE == 4096);
+#define TDX_IMAGE_HEADER_SIZE sizeof(struct tdx_image_header) +static_assert(TDX_IMAGE_HEADER_SIZE == 4096);
-/* Intel TDX module update ABI structure. aka. "TDX module blob". */ +/*
+ * Intel TDX module update ABI structure. aka. "TDX module blob".
+ *
+ * @payload contains sigstruct pages followed by module pages.
+ */
struct tdx_image {
struct tdx_image_header header;- u8 payload[]; // Contains sigstruct pages followed by module pages
+ u8 payload[];
};
-static void populate_pa_list(u64 *pa_list, u32 max_entries, const u8 *start, u32 nr_pages) +static void populate_pa_list(u64 *pa_list, const u8 *vmalloc_addr, u32 vmalloc_len_pages)
{
int i;- nr_pages = MIN(nr_pages, max_entries);
- for (i = 0; i < nr_pages; i++) {
- pa_list[i] = vmalloc_to_pfn(start) << PAGE_SHIFT;
- start += PAGE_SIZE;
+ for (i = 0; i < vmalloc_len_pages; i++) {
+ unsigned long offset = i * PAGE_SIZE;
+ unsigned long pfn = vmalloc_to_pfn(&vmalloc_addr[offset]);
+
+ pa_list[i] = pfn << PAGE_SHIFT; }
}
@@ -123,39 +130,43 @@ static void populate_seamldr_params(struct seamldr_params *params,
params->scenario = SEAMLDR_SCENARIO_UPDATE; params->module_nr_pages = mod_nr_pages;
- populate_pa_list(params->sigstruct_pages_pa_list, SEAMLDR_MAX_NR_SIG_PAGES,
- sig, sig_nr_pages);
- populate_pa_list(params->module_pages_pa_list, SEAMLDR_MAX_NR_MODULE_PAGES,
- mod, mod_nr_pages);
+ populate_pa_list(params->sigstruct_pages_pa_list, sig, sig_nr_pages);
+ populate_pa_list(params->module_pages_pa_list, mod, mod_nr_pages);
}
-static int init_seamldr_params(struct seamldr_params *params, const u8 *data, u32 size) +/*
+ * @image points to a vmalloc()'d 'struct tdx_image'. Transform
+ * it into @params which is the P-SEAMLDR ABI format.
+ */
+static int init_seamldr_params(struct seamldr_params *params,
+ const struct tdx_image *image,
+ u32 image_len)
{
- const struct tdx_image *image = (const void *)data; const struct tdx_image_header *header = &image->header; u32 sigstruct_len = header->sigstruct_nr_pages * PAGE_SIZE; u32 module_len = header->module_nr_pages * PAGE_SIZE; u8 *header_start = (u8 *)header;
- u8 *header_end = header_start + HEADER_SIZE;
+ u8 *header_end = header_start + TDX_IMAGE_HEADER_SIZE;
u8 *sigstruct_start = header_end; u8 *sigstruct_end = sigstruct_start + sigstruct_len; u8 *module_start = sigstruct_end;
- /* Check the calculated payload size against the data size. */
- if (HEADER_SIZE + sigstruct_len + module_len != size)
+ /* Check the calculated payload size against the image size. */
+ if (TDX_IMAGE_HEADER_SIZE + sigstruct_len + module_len != image_len)
return -EINVAL;
- /*
- * Don't care about user passing the wrong file, but protect
- * kernel ABI by preventing accepting garbage.
- */
+ /* Reject unsupported tdx_image ABI versions. */
if (header->version != TDX_IMAGE_VERSION_2) return -EINVAL;
+ if (header->sigstruct_nr_pages > SEAMLDR_MAX_NR_SIG_PAGES ||
+ header->module_nr_pages > SEAMLDR_MAX_NR_MODULE_PAGES)
+ return -EINVAL;
+
if (memcmp(header->signature, "TDX-BLOB", sizeof(header->signature))) return -EINVAL;
@@ -163,7 +174,7 @@ static int init_seamldr_params(struct seamldr_params *params, const u8 *data, u3
return -EINVAL; populate_seamldr_params(params, sigstruct_start, header->sigstruct_nr_pages, - module_start, header->module_nr_pages); + module_start, header->module_nr_pages); return 0;
}
@@ -230,14 +241,14 @@ static int do_seamldr_install_module(void *seamldr_params)
{
enum module_update_state newstate, curstate = MODULE_UPDATE_START;
int cpu = smp_processor_id();
- bool primary;
+ bool is_lead_cpu;
int ret = 0;
/*- * Use CPU 0 to execute update steps that must run exactly once.
- * Note CPU 0 is always online.
+ * Some steps must be run on exactly one CPU. Pick a "lead" CPU to
+ * execute those steps. Use CPU 0 because it is always online.
*/
- primary = cpu == 0;
+ is_lead_cpu = cpu == 0;
do {
newstate = READ_ONCE(update_ctrl.state);
@@ -250,7 +261,7 @@ static int do_seamldr_install_module(void *seamldr_params)
curstate = newstate;
switch (curstate) {
case MODULE_UPDATE_SHUTDOWN:
- if (primary)
+ if (is_lead_cpu)
ret = tdx_module_shutdown();
break;
case MODULE_UPDATE_CPU_INSTALL:
@@ -260,7 +271,7 @@ static int do_seamldr_install_module(void *seamldr_params)
ret = tdx_cpu_enable();
break;
case MODULE_UPDATE_RUN_UPDATE:
- if (primary)
+ if (is_lead_cpu)
ret = tdx_module_run_update();
break;
default:@@ -276,20 +287,27 @@ static int do_seamldr_install_module(void *seamldr_params)
/**
* seamldr_install_module - Install a new TDX module.
* @data: Pointer to the TDX module image.
- * @size: Size of the TDX module image.
+ * @data_len: Size of the TDX module image.
*
* Returns 0 on success, negative error code on failure.
*/
-int seamldr_install_module(const u8 *data, u32 size)
+int seamldr_install_module(const u8 *data, u32 data_len)
{
struct seamldr_params *params;
+ const struct tdx_image *image;
int ret;+ if (data_len < TDX_IMAGE_HEADER_SIZE)
+ return -EINVAL;
+
+ image = (const struct tdx_image *)data;
+
params = kzalloc_obj(*params); if (!params) return -ENOMEM;
- ret = init_seamldr_params(params, data, size);
+ /* Populate 'params' from 'image'. */
+ ret = init_seamldr_params(params, image, data_len); if (ret) goto out;
arch/x86/virt/vmx/tdx/tdx.c +2 -0
@@ -69,6 +69,8 @@ static LIST_HEAD(tdx_memlist);static struct tdx_sys_info tdx_sysinfo; +static DEFINE_RAW_SPINLOCK (sysinit_lock); +/* * Do the module global initialization once and return its result. * It can be done on any cpu, and from task or IRQ context.
@@ -76,29 +78,34 @@ static struct tdx_sys_info tdx_sysinfo;static int try_init_module_global (void ) {
struct tdx_module_args args = {};- static DEFINE_RAW_SPINLOCK(sysinit_lock);
+ int ret;
raw_spin_lock(&sysinit_lock);
- if (tdx_module_state.sysinit_done)
+ /* Return the "cached" return code. */
+ if (tdx_module_state.sysinit_done) {
+ ret = tdx_module_state.sysinit_ret; goto out;
+ }
/* RCX is module attributes and all bits are reserved */ args.rcx = 0;
- tdx_module_state.sysinit_ret = seamcall_prerr(TDH_SYS_INIT, &args);
+ ret = seamcall_prerr(TDH_SYS_INIT, &args);
/*
* The first SEAMCALL also detects the TDX module, thus
* it can fail due to the TDX module is not loaded.
* Dump message to let the user know.
*/
- if (tdx_module_state.sysinit_ret == -ENODEV)
+ if (ret == -ENODEV)
pr_err("module not loaded\n");+ /* Save the return code for later callers. */
tdx_module_state.sysinit_done = true; + tdx_module_state.sysinit_ret = ret; out: raw_spin_unlock(&sysinit_lock);
- return tdx_module_state.sysinit_ret;
+ return ret;
}
/**
@@ -1267,7 +1274,7 @@ static __init int tdx_enable(void)
}
subsys_initcall(tdx_enable);
-#define TDX_SYS_SHUTDOWN_AVOID_COMPAT_SENSITIVE BIT(16) +#define TDX_SYS_SHUTDOWN_AVOID_COMPAT_SENSITIVE BIT_ULL(16)
int tdx_module_shutdown(void)
{
drivers/virt/coco/tdx-host/Kconfig +1 -1
@@ -1,12 +1,6 @@ config TDX_HOST_SERVICES - tristate "TDX Host Services Driver" + tristate
depends on INTEL_TDX_HOST
select FW_LOADER
select FW_UPLOAD
default m
- help
- Enable access to TDX host services like module update and
- extensions (e.g. TDX Connect).
-
- Say y or m if enabling support for confidential virtual machine
- support (CONFIG_INTEL_TDX_HOST). The module is called tdx_host.ko.
drivers/virt/coco/tdx-host/tdx-host.c +0 -0
@@ -76,6 +76,10 @@ static ssize_t num_remaining_updates_show(struct device *dev,
return sysfs_emit(buf, "%u\n", info.num_remaining_updates);
}
+/*
+ * These attributes are intended for managing TDX module updates. Reading
+ * them issues a slow, serialized P-SEAMLDR query, so keep them admin-only.
+ */
static DEVICE_ATTR_ADMIN_RO(seamldr_version);
static DEVICE_ATTR_ADMIN_RO(num_remaining_updates);
@@ -90,7 +94,10 @@ static bool supports_runtime_update(void)
const struct tdx_sys_info *sysinfo = tdx_get_sysinfo(); if (!sysinfo)
- return 0;
+ return false;
+
+ if (!tdx_supports_runtime_update(sysinfo))
+ return false;
/* * Calling P-SEAMLDR on CPUs with the seamret_invd_vmcs bug clears @@ -98,14 +105,17 @@ static bool supports_runtime_update(void) * present before exposing P-SEAMLDR features. */ if (boot_cpu_has_bug(X86_BUG_SEAMRET_INVD_VMCS))
- return 0;
+ return false;
- return tdx_supports_runtime_update(sysinfo);
+ return true;
}
static umode_t seamldr_group_visible(struct kobject *kobj, struct attribute *attr, int idx) {
- return supports_runtime_update() ? attr->mode : 0;
+ if (!supports_runtime_update())
+ return 0;
+
+ return attr->mode;
}
static const struct attribute_group seamldr_group = {
@@ -120,20 +130,20 @@ static const struct attribute_group *tdx_host_groups[] = {
};
static enum fw_upload_err tdx_fw_prepare(struct fw_upload *fwl,
- const u8 *data, u32 size)
+ const u8 *data, u32 data_len)
{
return FW_UPLOAD_ERR_NONE;}
static enum fw_upload_err tdx_fw_write(struct fw_upload *fwl, const u8 *data,
- u32 offset, u32 size, u32 *written)
+ u32 offset, u32 data_len, u32 *written)
{
int ret;- ret = seamldr_install_module(data, size);
+ ret = seamldr_install_module(data, data_len);
switch (ret) {
case 0:
- *written = size;
+ *written = data_len;
return FW_UPLOAD_ERR_NONE;
case -EBUSY:
return FW_UPLOAD_ERR_BUSY;So a little cat|sort|uniq says:
11 Reviewed-by: Rick Edgecombe <[email protected]> 17 Reviewed-by: Kiryl Shutsemau (Meta) <[email protected]>
We're on v10 here. There are 6 patches in here with no review tags:
M: Kiryl Shutsemau <[email protected]> R: Dave Hansen <[email protected]> R: Rick Edgecombe <[email protected]>
Are all of those 6 new patches? Or do the reviewers still have some work to do?