2023-01-12 17:36:52

by Isaku Yamahata

[permalink] [raw]
Subject: [RFC PATCH v3 03/16] KVM: TDX: Pass KVM page level to tdh_mem_page_add() and tdh_mem_page_aug()

From: Xiaoyao Li <[email protected]>

Level info is needed in tdh_clflush_page() to generate the correct page
size.

Besides, explicitly pass level info to SEAMCALL instead of assuming
it's zero. It works naturally when 2MB support lands.

Signed-off-by: Xiaoyao Li <[email protected]>
Signed-off-by: Isaku Yamahata <[email protected]>
---
arch/x86/kvm/vmx/tdx.c | 7 ++++---
arch/x86/kvm/vmx/tdx_ops.h | 19 ++++++++++++-------
2 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c
index 487ba90a0b7c..8959a019b87a 100644
--- a/arch/x86/kvm/vmx/tdx.c
+++ b/arch/x86/kvm/vmx/tdx.c
@@ -1324,6 +1324,7 @@ static void tdx_unpin(struct kvm *kvm, kvm_pfn_t pfn)
static int tdx_sept_set_private_spte(struct kvm *kvm, gfn_t gfn,
enum pg_level level, kvm_pfn_t pfn)
{
+ int tdx_level = pg_level_to_tdx_sept_level(level);
struct kvm_tdx *kvm_tdx = to_kvm_tdx(kvm);
hpa_t hpa = pfn_to_hpa(pfn);
gpa_t gpa = gfn_to_gpa(gfn);
@@ -1348,7 +1349,7 @@ static int tdx_sept_set_private_spte(struct kvm *kvm, gfn_t gfn,
if (KVM_BUG_ON(level != PG_LEVEL_4K, kvm))
return -EINVAL;

- err = tdh_mem_page_aug(kvm_tdx->tdr_pa, gpa, hpa, &out);
+ err = tdh_mem_page_aug(kvm_tdx->tdr_pa, gpa, tdx_level, hpa, &out);
if (err == TDX_ERROR_SEPT_BUSY) {
tdx_unpin(kvm, pfn);
return -EAGAIN;
@@ -1387,8 +1388,8 @@ static int tdx_sept_set_private_spte(struct kvm *kvm, gfn_t gfn,
kvm_tdx->source_pa = INVALID_PAGE;

do {
- err = tdh_mem_page_add(kvm_tdx->tdr_pa, gpa, hpa, source_pa,
- &out);
+ err = tdh_mem_page_add(kvm_tdx->tdr_pa, gpa, tdx_level, hpa,
+ source_pa, &out);
/*
* This path is executed during populating initial guest memory
* image. i.e. before running any vcpu. Race is rare.
diff --git a/arch/x86/kvm/vmx/tdx_ops.h b/arch/x86/kvm/vmx/tdx_ops.h
index 4db983b1dc94..4b03acce5003 100644
--- a/arch/x86/kvm/vmx/tdx_ops.h
+++ b/arch/x86/kvm/vmx/tdx_ops.h
@@ -19,6 +19,11 @@

void pr_tdx_error(u64 op, u64 error_code, const struct tdx_module_output *out);

+static inline enum pg_level tdx_sept_level_to_pg_level(int tdx_level)
+{
+ return tdx_level + 1;
+}
+
static inline void tdx_clflush_page(hpa_t addr, enum pg_level level)
{
clflush_cache_range(__va(addr), KVM_HPAGE_SIZE(level));
@@ -60,11 +65,11 @@ static inline u64 tdh_mng_addcx(hpa_t tdr, hpa_t addr)
return __seamcall(TDH_MNG_ADDCX, addr, tdr, 0, 0, NULL);
}

-static inline u64 tdh_mem_page_add(hpa_t tdr, gpa_t gpa, hpa_t hpa, hpa_t source,
- struct tdx_module_output *out)
+static inline u64 tdh_mem_page_add(hpa_t tdr, gpa_t gpa, int level, hpa_t hpa,
+ hpa_t source, struct tdx_module_output *out)
{
- tdx_clflush_page(hpa, PG_LEVEL_4K);
- return seamcall_sept(TDH_MEM_PAGE_ADD, gpa, tdr, hpa, source, out);
+ tdx_clflush_page(hpa, tdx_sept_level_to_pg_level(level));
+ return seamcall_sept(TDH_MEM_PAGE_ADD, gpa | level, tdr, hpa, source, out);
}

static inline u64 tdh_mem_sept_add(hpa_t tdr, gpa_t gpa, int level, hpa_t page,
@@ -93,11 +98,11 @@ static inline u64 tdh_mem_page_relocate(hpa_t tdr, gpa_t gpa, hpa_t hpa,
return __seamcall(TDH_MEM_PAGE_RELOCATE, gpa, tdr, hpa, 0, out);
}

-static inline u64 tdh_mem_page_aug(hpa_t tdr, gpa_t gpa, hpa_t hpa,
+static inline u64 tdh_mem_page_aug(hpa_t tdr, gpa_t gpa, int level, hpa_t hpa,
struct tdx_module_output *out)
{
- tdx_clflush_page(hpa, PG_LEVEL_4K);
- return seamcall_sept(TDH_MEM_PAGE_AUG, gpa, tdr, hpa, 0, out);
+ tdx_clflush_page(hpa, tdx_sept_level_to_pg_level(level));
+ return seamcall_sept(TDH_MEM_PAGE_AUG, gpa | level, tdr, hpa, 0, out);
}

static inline u64 tdh_mem_range_block(hpa_t tdr, gpa_t gpa, int level,
--
2.25.1