TDX supports 4K, 2M and 1G page sizes. When TDX guest accepts one page
via try_accept_one(), it passes the page size level to the TDX module.
Currently try_accept_one() uses hard-coded magic number for that.
Introduce a new enum type to represent the page level of TDX supported
page sizes to replace the hard-coded values. Both initializing the TDX
module and KVM TDX support will need to use that too.
Also, currently try_accept_one() uses an open-coded switch statement to
get the TDX page level from the kernel page level. As KVM will also
need to do the same thing, introduce a common helper to convert the
kernel page level to the TDX page level.
Reviewed-by: Kirill A. Shutemov <kirill.shutemov@xxxxxxxxxxxxxxx>
Signed-off-by: Kai Huang <kai.huang@xxxxxxxxx>
---
arch/x86/coco/tdx/tdx.c | 20 ++++----------------
arch/x86/include/asm/tdx.h | 33 +++++++++++++++++++++++++++++++++
2 files changed, 37 insertions(+), 16 deletions(-)
diff --git a/arch/x86/coco/tdx/tdx.c b/arch/x86/coco/tdx/tdx.c
index 928dcf7a20d9..c5ff9647213d 100644
--- a/arch/x86/coco/tdx/tdx.c
+++ b/arch/x86/coco/tdx/tdx.c
@@ -655,7 +655,6 @@ static bool try_accept_one(phys_addr_t *start, unsigned long len,
{
unsigned long accept_size = page_level_size(pg_level);
u64 tdcall_rcx;
- u8 page_size;
if (!IS_ALIGNED(*start, accept_size))
return false;
@@ -663,27 +662,16 @@ static bool try_accept_one(phys_addr_t *start, unsigned long len,
if (len < accept_size)
return false;
+ /* TDX only supports 4K/2M/1G page sizes */
+ if (pg_level < PG_LEVEL_4K || pg_level > PG_LEVEL_1G)
+ return false;
/*
* Pass the page physical address to the TDX module to accept the
* pending, private page.
*
* Bits 2:0 of RCX encode page size: 0 - 4K, 1 - 2M, 2 - 1G.
*/
- switch (pg_level) {
- case PG_LEVEL_4K:
- page_size = 0;
- break;
- case PG_LEVEL_2M:
- page_size = 1;
- break;
- case PG_LEVEL_1G:
- page_size = 2;
- break;
- default:
- return false;
- }
-
- tdcall_rcx = *start | page_size;
+ tdcall_rcx = *start | to_tdx_pg_level(pg_level);
if (__tdx_module_call(TDX_ACCEPT_PAGE, tdcall_rcx, 0, 0, 0, NULL))
return false;
diff --git a/arch/x86/include/asm/tdx.h b/arch/x86/include/asm/tdx.h
index 020c81a7c729..1c166fb9c22f 100644
--- a/arch/x86/include/asm/tdx.h
+++ b/arch/x86/include/asm/tdx.h
@@ -20,6 +20,39 @@
#ifndef __ASSEMBLY__
+#include <asm/pgtable_types.h>
+
+/*
+ * The page levels of TDX supported page sizes (4K/2M/1G).
+ *
+ * Those values are part of the TDX module ABI. Do not change them.
+ */
+enum tdx_pg_level {
+ TDX_PG_LEVEL_4K,
+ TDX_PG_LEVEL_2M,
+ TDX_PG_LEVEL_1G,
+ TDX_PG_LEVEL_NUM
+};
+
+/*
+ * Get the TDX page level based on the kernel page level. The caller
+ * to make sure only pass 4K/2M/1G kernel page level.
+ */
+static inline enum tdx_pg_level to_tdx_pg_level(enum pg_level pglvl)
+{
+ switch (pglvl) {
+ case PG_LEVEL_4K:
+ return TDX_PG_LEVEL_4K;
+ case PG_LEVEL_2M:
+ return TDX_PG_LEVEL_2M;
+ case PG_LEVEL_1G:
+ return TDX_PG_LEVEL_1G;
+ default:
+ WARN_ON_ONCE(1);
+ }
+ return TDX_PG_LEVEL_NUM;
+}
+
/*
* Used to gather the output registers values of the TDCALL and SEAMCALL
* instructions when requesting services from the TDX module.