x86/virt/tdx: Formalize SEAMCALL version encoding support
Subsystems (2)
PATCH 1/1 x86/virt/tdx: Formalize SEAMCALL version encoding support
TDX uses the SEAMCALL instruction to invoke various TDX module functions. Just like the syscall, a SEAMCALL specifies the operation using a function number and parameters. Moreover, TDX also uses SEAMCALL versions to extend the functionalities of existing SEAMCALLs while keeping backward compatibility. Unlike syscall versions that assign brand new numbers, TDX segments the function number into a basic function number field and a version field. Together, they encode the new function number.
An existing SEAMCALL (TDH.VP.INIT) helper is already using the version field. However, having the caller pack the version into the function number open-codes the ABI layout, making the SEAMCALL helper definition obscure and error prone.
Add a version field in struct tdx_module_args, so that most existing SEAMCALL helpers get a default "version == 0" behavior without code churn, while callers requiring extended functionalities can specify the version descriptively. As an internal implementation detail, encode the tdx_module_args.version in the function number before calling into assembly code.
Two alternative schemes were considered:
1. Define versioned macros like TDH_VP_INIT_V0, TDH_VP_INIT_V1, etc. However, this breaks naming consistency unless all existing stable function macros are changed to TDH_XXX_V0.
2. Add an explicit 'version' parameter to the base seamcall() API. This unnecessarily forces all stable SEAMCALL helpers to pass a meaningless '0' argument. Additionally, the magic '0' or '1' values at caller sites are not descriptive.
--- arch/x86/include/asm/shared/tdx.h | 2 ++ arch/x86/virt/vmx/tdx/seamcall_internal.h | 19 ++++++++++++++++++- arch/x86/virt/vmx/tdx/tdx.h | 8 -------- arch/x86/virt/vmx/tdx/tdx.c | 5 +++-- 4 files changed, 23 insertions(+), 11 deletions(-)
arch/x86/include/asm/shared/tdx.h +2 -0
@@ -143,6 +143,8 @@ struct tdx_module_args {u64 rbx;u64 rdi;u64 rsi; +/* ABI version, encoded in rax */ +u8 version; };/* Used to communicate with the TDX module */
arch/x86/virt/vmx/tdx/seamcall_internal.h +18 -1
@@ -11,6 +11,7 @@#ifndef _X86_VIRT_SEAMCALL_INTERNAL_H#define _X86_VIRT_SEAMCALL_INTERNAL_H +#include <linux/bitfield.h> #include <linux/printk.h> #include <linux/types.h> #include <asm/archrandom.h>
@@ -23,6 +24,22 @@ u64 __seamcall_saved_ret(u64 fn, struct tdx_module_args *args);typedef u64 (*sc_func_t )(u64 fn,struct tdx_module_args *args); +/* +* SEAMCALL leaf: +* +* Bit 15:0 Leaf number +* Bit 23:16 Version number +*/ +#define SEAMCALL_VERSION_MASK GENMASK_U64(23, 16) + +static __always_inline u64 __seamcall_encode_fn(sc_func_t func, u64 fn, +struct tdx_module_args *args) +{ +FIELD_MODIFY (SEAMCALL_VERSION_MASK , &fn, args->version); + +return func (fn, args); +} + static __always_inline u64 __seamcall_dirty_cache(sc_func_t func, u64 fn,struct tdx_module_args *args) {
@@ -39,7 +56,7 @@ static __always_inline u64 __seamcall_dirty_cache(sc_func_t func, u64 fn,*/ this_cpu_write (cache_state_incoherent, true); -return func (fn, args); +return __seamcall_encode_fn (func, fn, args); } static __always_inline u64 sc_retry(sc_func_t func, u64 fn,
arch/x86/virt/vmx/tdx/tdx.h +0 -8
@@ -50,14 +50,6 @@#define TDH_SYS_UPDATE 53#define TDH_SYS_DISABLE 69 -/* -* SEAMCALL leaf: -* -* Bit 15:0 Leaf number -* Bit 23:16 Version number -*/ -#define TDX_VERSION_SHIFT 16 -/* TDX page types */ #define PT_NDA 0x0#define PT_RSVD 0x1
arch/x86/virt/vmx/tdx/tdx.c +3 -2
@@ -1910,10 +1910,11 @@ u64 tdh_vp_init(struct tdx_vp *vp, u64 initial_rcx, u32 x2apicid) .rcx = vp->tdvpr_pa, .rdx = initial_rcx, .r8 = x2apicid, + /* apicid requires version == 1. */ + .version = 1, }; -/* apicid requires version == 1. */ -return seamcall (TDH_VP_INIT | (1ULL <<TDX_VERSION_SHIFT ), &args); +return seamcall (TDH_VP_INIT , &args); }EXPORT_SYMBOL_FOR_KVM (tdh_vp_init);
Signature · 1 line
2.25.1
Quoted 20 lines · Xu Yilun
arch/x86/virt/vmx/tdx/seamcall_internal.h +16
-0+/* +* SEAMCALL leaf: +* +* Bit 15:0 Leaf number +* Bit 23:16 Version number +*/ +#define SEAMCALL_VERSION_MASK GENMASK_U64(23, 16) + +static __always_inline u64 __seamcall_encode_fn(sc_func_t func, u64 fn, +struct tdx_module_args *args) +{ +FIELD_MODIFY (SEAMCALL_VERSION_MASK , &fn, args->version); + +return func (fn, args); +} + static __always_inline u64 __seamcall_dirty_cache(sc_func_t func, u64 fn,struct tdx_module_args *args) {
arch/x86/virt/vmx/tdx/seamcall_internal.h static __always_inline u64 __seamcall_dirty_cache(sc_func_t func, u64 fn, · line 60
Quoted 6 lines
arch/x86/virt/vmx/tdx/seamcall_internal.h +1
-1@@ -39,7 +56,7 @@ static __always_inline u64 __seamcall_dirty_cache(sc_func_t func, u64 fn,*/ this_cpu_write (cache_state_incoherent, true); -return func (fn, args); +return __seamcall_encode_fn (func, fn, args); }
Can we drop the new wrapper? I don't see why we need it. The wrapper makes the code harder to read.
Quoted 21 lines · Xiaoyao Li
Quoted 20 lines · Xu Yilun
arch/x86/virt/vmx/tdx/seamcall_internal.h +16
-0+/* +* SEAMCALL leaf: +* +* Bit 15:0 Leaf number +* Bit 23:16 Version number +*/ +#define SEAMCALL_VERSION_MASK GENMASK_U64(23, 16) + +static __always_inline u64 __seamcall_encode_fn(sc_func_t func, u64 fn, +struct tdx_module_args *args) +{ +FIELD_MODIFY (SEAMCALL_VERSION_MASK , &fn, args->version); + +return func (fn, args); +} + static __always_inline u64 __seamcall_dirty_cache(sc_func_t func, u64 fn,struct tdx_module_args *args) {@@ -39,7 +56,7 @@ static __always_inline u64 __seamcall_dirty_cache(sc_func_t func, u64 fn,
Quoted 8 lines
arch/x86/virt/vmx/tdx/seamcall_internal.h static __always_inline u64 __seamcall_dirty_cache(sc_func_t func, u64 fn, · line 60
Quoted 5 lines
arch/x86/virt/vmx/tdx/seamcall_internal.h +1
-1*/ this_cpu_write (cache_state_incoherent, true); -return func (fn, args); +return __seamcall_encode_fn (func, fn, args); }Can we drop the new wrapper? I don't see why we need it. The wrapper makes the code harder to read.
OK, will call FIELD_MODIFY() directly in __seamcall_dirty_cache()
Quoted 2 lines
Edgecombe, Rick P
maintainerQuoted 8 lines · Xu Yilun
TDX uses the SEAMCALL instruction to invoke various TDX module functions. Just like the syscall, a SEAMCALL specifies the operation using a function number and parameters. Moreover, TDX also uses SEAMCALL versions to extend the functionalities of existing SEAMCALLs while keeping backward compatibility. Unlike syscall versions that assign brand new numbers, TDX segments the function number into a basic function number field and a version field. Together, they encode the new function number.
So isn't this pretty much like numbered seamcalls, except there is a special format for generating seamcall2, seamcall3, etc? In the end you just use a different number for a different version of the call. So it's just like syscalls, except there is a pattern in the specific number for calls of the same family.
Quoted 2 lines
An existing SEAMCALL (TDH.VP.INIT) helper is already using the version
^ maybe swap the order of these two?
Quoted 1 line
field. However, having the caller pack the version into the function
^Can drop this?
Quoted 2 lines
number open-codes the ABI layout, making the SEAMCALL helper definition obscure and error prone.
Do we need the second part of the sentence?
Quoted 7 lines
Add a version field in struct
tdx_module_args, so that most existing SEAMCALL helpers get a default "version == 0" behavior without code churn, while callers requiring extended functionalities can specify the version descriptively.As an internal implementation detail,
^ Is this needed to make it clear?
Quoted 6 lines
encode the
tdx_module_args.version in the function number before calling into assembly code.Two alternative schemes were considered:
Quoted 9 lines
1. Define versioned macros like
TDH_VP_INIT_V0,TDH_VP_INIT_V1, etc. However, this breaks naming consistency unless all existing stable function macros are changed toTDH_XXX_V0.2. Add an explicit 'version' parameter to the base
seamcall()API. This unnecessarily forces all stable SEAMCALL helpers to pass a meaningless '0' argument. Additionally, the magic '0' or '1' values at caller sites are not descriptive.
Dave was recently saying something to the effect of "make every word count". I think we could lose some filler words.
Quoted 1 line
Signed-off-by Xu Yilun <[email protected]>
How about a link to the thread where this was suggested.
Quoted 33 lines
--- arch/x86/include/asm/shared/tdx.h | 2 ++ arch/x86/virt/vmx/tdx/seamcall_internal.h | 19 ++++++++++++++++++- arch/x86/virt/vmx/tdx/tdx.h | 8 -------- arch/x86/virt/vmx/tdx/tdx.c | 5 +++-- 4 files changed, 23 insertions(+), 11 deletions(-)arch/x86/include/asm/shared/tdx.h +2
-0@@ -143,6 +143,8 @@ struct tdx_module_args {u64 rbx;u64 rdi;u64 rsi; +/* ABI version, encoded in rax */ +u8 version; };/* Used to communicate with the TDX module */ arch/x86/virt/vmx/tdx/seamcall_internal.h +1
-0@@ -11,6 +11,7 @@#ifndef _X86_VIRT_SEAMCALL_INTERNAL_H#define _X86_VIRT_SEAMCALL_INTERNAL_H +#include <linux/bitfield.h> #include <linux/printk.h> #include <linux/types.h> #include <asm/archrandom.h>
arch/x86/virt/vmx/tdx/seamcall_internal.h u64 __seamcall_saved_ret(u64 fn, struct tdx_module_args *args); · line 33
Quoted 10 lines
arch/x86/virt/vmx/tdx/seamcall_internal.h +7
-0@@ -23,6 +24,22 @@ u64 __seamcall_saved_ret(u64 fn, struct tdx_module_args *args);typedef u64 (*sc_func_t )(u64 fn,struct tdx_module_args *args); +/* +* SEAMCALL leaf: +* +* Bit 15:0 Leaf number +* Bit 23:16 Version number +*/ +#define SEAMCALL_VERSION_MASK GENMASK_U64(23, 16)
The annoying thing is that the path touched here is also used for seamldr calls now, which afaict has no concept of version. Underscoring how much of a mess the wrapper stack is.
arch/x86/virt/vmx/tdx/seamcall_internal.h u64 __seamcall_saved_ret(u64 fn, struct tdx_module_args *args); · line 37
Quoted 4 lines
arch/x86/virt/vmx/tdx/seamcall_internal.h +4
-0+ +static __always_inline u64 __seamcall_encode_fn(sc_func_t func, u64 fn, +struct tdx_module_args *args) +{
I have the same question as Xiaoyao. We have so many wrappers already.
Quoted 54 lines
arch/x86/virt/vmx/tdx/seamcall_internal.h +6
-1+FIELD_MODIFY (SEAMCALL_VERSION_MASK , &fn, args->version); + +return func (fn, args); +} + static __always_inline u64 __seamcall_dirty_cache(sc_func_t func, u64 fn,struct tdx_module_args *args) {@@ -39,7 +56,7 @@ static __always_inline u64 __seamcall_dirty_cache(sc_func_t func, u64 fn,*/ this_cpu_write (cache_state_incoherent, true); -return func (fn, args); +return __seamcall_encode_fn (func, fn, args); } static __always_inline u64 sc_retry(sc_func_t func, u64 fn,arch/x86/virt/vmx/tdx/tdx.h +0
-8@@ -50,14 +50,6 @@#define TDH_SYS_UPDATE 53#define TDH_SYS_DISABLE 69 -/* -* SEAMCALL leaf: -* -* Bit 15:0 Leaf number -* Bit 23:16 Version number -*/ -#define TDX_VERSION_SHIFT 16 -/* TDX page types */ #define PT_NDA 0x0#define PT_RSVD 0x1arch/x86/virt/vmx/tdx/tdx.c +3
-2@@ -1910,10 +1910,11 @@ u64 tdh_vp_init(struct tdx_vp *vp, u64 initial_rcx, u32 x2apicid) .rcx = vp->tdvpr_pa, .rdx = initial_rcx, .r8 = x2apicid, + /* apicid requires version == 1. */ + .version = 1, }; -/* apicid requires version == 1. */ -return seamcall (TDH_VP_INIT | (1ULL <<TDX_VERSION_SHIFT ), &args); +return seamcall (TDH_VP_INIT , &args); }EXPORT_SYMBOL_FOR_KVM (tdh_vp_init);
Quoted 10 lines · Rick P
Quoted 8 lines · Xu Yilun
TDX uses the SEAMCALL instruction to invoke various TDX module functions. Just like the syscall, a SEAMCALL specifies the operation using a function number and parameters. Moreover, TDX also uses SEAMCALL versions to extend the functionalities of existing SEAMCALLs while keeping backward compatibility. Unlike syscall versions that assign brand new numbers, TDX segments the function number into a basic function number field and a version field. Together, they encode the new function number.
Quoted 5 lines
So isn't this pretty much like numbered seamcalls, except there is a special format for generating seamcall2, seamcall3, etc? In the end you just use a different number for a different version of the call. So it's just like syscalls, except there is a pattern in the specific number for calls of the same family.
OK. I will refactored this paragraph:
SEAMCALL invokes TDX module functions using a function number and parameters. To extend the functionalities of existing SEAMCALLs while keeping backward compatibility, TDX adds more numbered SEAMCALLs of the same family. This is just like syscalls, except that TDX defines a specific function number encoding pattern for the same family: a base function number and a version together encode the full function number.
Quoted 12 lines
Quoted 3 lines
An existing SEAMCALL (TDH.VP.INIT) helper is already using the version
^ maybe swap the order of these two?
Quoted 1 line
field. However, having the caller pack the version into the function
^Can drop this?
Quoted 2 lines
number open-codes the ABI layout, making the SEAMCALL helper definition obscure and error prone.
Do we need the second part of the sentence?
Quoted 1 line
Quoted 8 lines
Quoted 6 lines
Add a version field in struct
tdx_module_args, so that most existing SEAMCALL helpers get a default "version == 0" behavior without code churn, while callers requiring extended functionalities can specify the version descriptively.As an internal implementation detail,
^ Is this needed to make it clear?
I think these are also about "make every word count", I'll drop all 3 places above.
Quoted 11 lines
Quoted 11 lines
encode the
tdx_module_args.version in the function number before calling into assembly code.Two alternative schemes were considered:
1. Define versioned macros like
TDH_VP_INIT_V0,TDH_VP_INIT_V1, etc. However, this breaks naming consistency unless all existing stable function macros are changed toTDH_XXX_V0.
Quoted 8 lines
Quoted 5 lines
2. Add an explicit 'version' parameter to the base
seamcall()API. This unnecessarily forces all stable SEAMCALL helpers to pass a meaningless '0' argument. Additionally, the magic '0' or '1' values at caller sites are not descriptive.Dave was recently saying something to the effect of "make every word count". I think we could lose some filler words.
OK. I'll delete these adverbs.
Quoted 41 lines
Quoted 2 lines
Signed-off-by Xu Yilun <[email protected]>How about a link to the thread where this was suggested.
Quoted 36 lines
--- arch/x86/include/asm/shared/tdx.h | 2 ++ arch/x86/virt/vmx/tdx/seamcall_internal.h | 19 ++++++++++++++++++- arch/x86/virt/vmx/tdx/tdx.h | 8 -------- arch/x86/virt/vmx/tdx/tdx.c | 5 +++-- 4 files changed, 23 insertions(+), 11 deletions(-)arch/x86/include/asm/shared/tdx.h +2
-0@@ -143,6 +143,8 @@ struct tdx_module_args {u64 rbx;u64 rdi;u64 rsi; +/* ABI version, encoded in rax */ +u8 version; };/* Used to communicate with the TDX module */ arch/x86/virt/vmx/tdx/seamcall_internal.h +1
-0@@ -11,6 +11,7 @@#ifndef _X86_VIRT_SEAMCALL_INTERNAL_H#define _X86_VIRT_SEAMCALL_INTERNAL_H +#include <linux/bitfield.h> #include <linux/printk.h> #include <linux/types.h> #include <asm/archrandom.h> @@ -23,6 +24,22 @@ u64 __seamcall_saved_ret(u64 fn, struct tdx_module_args *args);typedef u64 (*sc_func_t )(u64 fn,struct tdx_module_args *args);
Quoted 11 lines
arch/x86/virt/vmx/tdx/seamcall_internal.h u64 __seamcall_saved_ret(u64 fn, struct tdx_module_args *args); · line 33
Quoted 7 lines
arch/x86/virt/vmx/tdx/seamcall_internal.h +7
-0+/* +* SEAMCALL leaf: +* +* Bit 15:0 Leaf number +* Bit 23:16 Version number +*/ +#define SEAMCALL_VERSION_MASK GENMASK_U64(23, 16)The annoying thing is that the path touched here is also used for seamldr calls now, which afaict has no concept of version. Underscoring how much of a mess the wrapper stack is.
OK, I'll drop the wrapper.
Quoted 23 lines
arch/x86/virt/vmx/tdx/seamcall_internal.h u64 __seamcall_saved_ret(u64 fn, struct tdx_module_args *args); · line 37
Quoted 5 lines
arch/x86/virt/vmx/tdx/seamcall_internal.h +4
-0+ +static __always_inline u64 __seamcall_encode_fn(sc_func_t func, u64 fn, +struct tdx_module_args *args) +{I have the same question as Xiaoyao. We have so many wrappers already.
Quoted 15 lines
arch/x86/virt/vmx/tdx/seamcall_internal.h +6
-1+FIELD_MODIFY (SEAMCALL_VERSION_MASK , &fn, args->version); + +return func (fn, args); +} + static __always_inline u64 __seamcall_dirty_cache(sc_func_t func, u64 fn,struct tdx_module_args *args) {@@ -39,7 +56,7 @@ static __always_inline u64 __seamcall_dirty_cache(sc_func_t func, u64 fn,*/ this_cpu_write (cache_state_incoherent, true); -return func (fn, args); +return __seamcall_encode_fn (func, fn, args); }
Quoted 6 lines · Rick P
Quoted 6 lines
Two alternative schemes were considered:
1. Define versioned macros like
TDH_VP_INIT_V0,TDH_VP_INIT_V1, etc. However, this breaks naming consistency unless all existing stable function macros are changed toTDH_XXX_V0.
Quoted 7 lines
Quoted 5 lines
2. Add an explicit 'version' parameter to the base
seamcall()API. This unnecessarily forces all stable SEAMCALL helpers to pass a meaningless '0' argument. Additionally, the magic '0' or '1' values at caller sites are not descriptive.Dave was recently saying something to the effect of "make every word count". I think we could lose some filler words.
This can also just go below the --- so it doesn't make it to the final changelog.
I like the discussion happening so far. I, too, dislike adding another level of C wrappers. I'm looking forward to v2.