On 8/8/2025 4:16 AM, Sagi Shahar wrote:
From: Ackerley Tng <ackerley...@google.com>

virt_map() enforces a private mapping for private memory. Introduce
virt_map_shared() that creates a shared mapping for private as
well as shared memory. This way, the TD does not have to remap its
page tables at runtime.

Signed-off-by: Ackerley Tng <ackerley...@google.com>
Signed-off-by: Sagi Shahar <sa...@google.com>
---
  .../testing/selftests/kvm/include/kvm_util.h  | 23 +++++++++++++
  tools/testing/selftests/kvm/lib/kvm_util.c    | 34 +++++++++++++++++++
  .../testing/selftests/kvm/lib/x86/processor.c | 15 ++++++--
  3 files changed, 70 insertions(+), 2 deletions(-)

[...]
-void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, int level)
+static void ___virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
+                          int level, bool protected)
  {
        const uint64_t pg_size = PG_LEVEL_SIZE(level);
        uint64_t *pml4e, *pdpe, *pde;
@@ -231,17 +232,27 @@ void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, 
uint64_t paddr, int level)
         * Neither SEV nor TDX supports shared page tables, so only the final
         * leaf PTE needs manually set the C/S-bit.
         */
-       if (vm_is_gpa_protected(vm, paddr))

Since the original code has already create shared/private mapping based on the
paddr, why not just use virt_map() and shared GPA as paddr, then no need to
duplicate the code?

+       if (protected)
                *pte |= vm->arch.c_bit;
        else
                *pte |= vm->arch.s_bit;
  }
+void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, int level)
+{
+       ___virt_pg_map(vm, vaddr, paddr, level, vm_is_gpa_protected(vm, paddr));
+}
+
  void virt_arch_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr)
  {
        __virt_pg_map(vm, vaddr, paddr, PG_LEVEL_4K);
  }
+void virt_arch_pg_map_shared(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr)
+{
+       ___virt_pg_map(vm, vaddr, paddr, PG_LEVEL_4K, false);
+}
+
  void virt_map_level(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
                    uint64_t nr_bytes, int level)
  {


Reply via email to