[PATCH 09/17] KVM: TDX: Add helper to handle mapping leaf SPTE into S-EPT

Rick Edgecombe
2026-03-27T20:14:33Z

From: Sean Christopherson <seanjc@google.com>

Add a helper, tdx_sept_map_leaf_spte(), to wrap and isolate PAGE.ADD and
PAGE.AUG operations, and thus complete tdx_sept_set_private_spte()'s
transition into a "dispatch" routine for setting/writing S-EPT entries.

No functional change intended.

Signed-off-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
---
arch/x86/kvm/vmx/tdx.c | 21 +++++++++++++--------
1 file changed, 13 insertions(+), 8 deletions(-)

diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c
index 92a846b91bac..361a75b42ae7 100644
--- a/arch/x86/kvm/vmx/tdx.c
+++ b/arch/x86/kvm/vmx/tdx.c
@@ -1689,18 +1689,12 @@ static int tdx_sept_link_private_spt(struct kvm *kvm, gfn_t gfn,
return 0;
}

-static int tdx_sept_set_private_spte(struct kvm *kvm, gfn_t gfn,
- enum pg_level level, u64 mirror_spte)
+static int tdx_sept_map_leaf_spte(struct kvm *kvm, gfn_t gfn, enum pg_level level,
+ u64 mirror_spte)
{
struct kvm_tdx *kvm_tdx = to_kvm_tdx(kvm);
kvm_pfn_t pfn = spte_to_pfn(mirror_spte);

- if (KVM_BUG_ON(!is_shadow_present_pte(mirror_spte), kvm))
- return -EIO;
-
- if (!is_last_spte(mirror_spte, level))
- return tdx_sept_link_private_spt(kvm, gfn, level, mirror_spte);
-
/* TODO: handle large pages. */
if (KVM_BUG_ON(level != PG_LEVEL_4K, kvm))
return -EIO;
@@ -1725,7 +1719,18 @@ static int tdx_sept_set_private_spte(struct kvm *kvm, gfn_t gfn,
return tdx_mem_page_aug(kvm, gfn, level, pfn);
}

+static int tdx_sept_set_private_spte(struct kvm *kvm, gfn_t gfn,
+ enum pg_level level, u64 mirror_spte)
+{

+ if (KVM_BUG_ON(!is_shadow_present_pte(mirror_spte), kvm))
+ return -EIO;
+
+ if (!is_last_spte(mirror_spte, level))
+ return tdx_sept_link_private_spt(kvm, gfn, level, mirror_spte);
+
+ return tdx_sept_map_leaf_spte(kvm, gfn, level, mirror_spte);
+}

/*
* Ensure shared and private EPTs to be flushed on all vCPUs.
--
2.53.0