Re: [PATCH 07/13] x86/virt/tdx: Add SEAMCALL wrapper tdh_mem_sept_add() to add SEPT pages

Yan Zhao
2025-01-07T05:29:16Z

Here's the proposed new version with changelog:
SEAMCALL RFC:
- Use struct tdx_td instead of raw TDR u64.
- Use "struct page *" for sept_page instead of raw hpa.
- Change "u64 level" to "int level".
- Rename "level" to "tdx_level" as it's tdx specfic. (Rick)
- Change "u64 gpa" to "gfn_t gfn". (Reinette)
- Introduce a union tdx_sept_gpa_mapping_info to initialize args.rcx.
(Reinette)
- Call tdx_clflush_page() instead of clflush_cache_range().
- Use extended_err1/2 instead of rcx/rdx for output.

diff --git a/arch/x86/include/asm/shared/tdx.h b/arch/x86/include/asm/shared/tdx.h
index c6d0668de167..ea5a98fd1dd5 100644
--- a/arch/x86/include/asm/shared/tdx.h
+++ b/arch/x86/include/asm/shared/tdx.h
@@ -105,6 +105,20 @@ struct tdx_module_args {
u64 rsi;
};

+/*
+ * Used to specify SEPT GPA mapping information for SEPT related SEAMCALLs and
+ * TDCALLs.
+ */
+union tdx_sept_gpa_mapping_info {
+ struct {
+ u64 level : 3;
+ u64 reserved1 : 9;
+ u64 gfn : 40;
+ u64 reserved2 : 12;
+ };
+ u64 full;
+};
+
/* Used to communicate with the TDX module */
u64 __tdcall(u64 fn, struct tdx_module_args *args);
u64 __tdcall_ret(u64 fn, struct tdx_module_args *args);
diff --git a/arch/x86/include/asm/tdx.h b/arch/x86/include/asm/tdx.h
index bbb8f0bae9ba..787c359a5fc9 100644
--- a/arch/x86/include/asm/tdx.h
+++ b/arch/x86/include/asm/tdx.h
@@ -33,6 +33,7 @@
#ifndef __ASSEMBLY__

#include <uapi/asm/mce.h>
+#include <linux/kvm_types.h>
#include "tdx_global_metadata.h"

/*
@@ -143,6 +144,8 @@ struct tdx_vp {
};

u64 tdh_mng_addcx(struct tdx_td *td, struct page *tdcs_page);
+u64 tdh_mem_sept_add(struct tdx_td *td, gfn_t gfn, int tdx_level, struct page *sept_page,
+ u64 *extended_err1, u64 *extended_err2);
u64 tdh_vp_addcx(struct tdx_vp *vp, struct page *tdcx_page);
u64 tdh_mng_key_config(struct tdx_td *td);
u64 tdh_mng_create(struct tdx_td *td, u64 hkid);
diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c
index a16c507296df..adb2059b6b5f 100644
--- a/arch/x86/virt/vmx/tdx/tdx.c
+++ b/arch/x86/virt/vmx/tdx/tdx.c
@@ -1583,6 +1583,27 @@ u64 tdh_mng_addcx(struct tdx_td *td, struct page *tdcs_page)
}
EXPORT_SYMBOL_GPL(tdh_mng_addcx);

+u64 tdh_mem_sept_add(struct tdx_td *td, gfn_t gfn, int tdx_level, struct page *sept_page,
+ u64 *extended_err1, u64 *extended_err2)
+{
+ union tdx_sept_gpa_mapping_info gpa_info = { .level = tdx_level, .gfn = gfn, };
+ struct tdx_module_args args = {
+ .rcx = gpa_info.full,
+ .rdx = tdx_tdr_pa(td),
+ .r8 = page_to_phys(sept_page),
+ };
+ u64 ret;
+
+ tdx_clflush_page(sept_page);
+ ret = seamcall_ret(TDH_MEM_SEPT_ADD, &args);
+
+ *extended_err1 = args.rcx;
+ *extended_err2 = args.rdx;
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(tdh_mem_sept_add);
+
u64 tdh_vp_addcx(struct tdx_vp *vp, struct page *tdcx_page)
{
struct tdx_module_args args = {
diff --git a/arch/x86/virt/vmx/tdx/tdx.h b/arch/x86/virt/vmx/tdx/tdx.h
index 08b01b7fe7c2..0d1ba0d0ac82 100644
--- a/arch/x86/virt/vmx/tdx/tdx.h
+++ b/arch/x86/virt/vmx/tdx/tdx.h
@@ -18,6 +18,7 @@
* TDX module SEAMCALL leaf functions
*/
#define TDH_MNG_ADDCX 1
+#define TDH_MEM_SEPT_ADD 3
#define TDH_VP_ADDCX 4
#define TDH_MNG_KEY_CONFIG 8
#define TDH_MNG_CREATE 9