[RFC v1 24/26] x86/tdx: Add helper to do MapGPA TDVMALL

From: Kuppuswamy Sathyanarayanan
Date: Fri Feb 05 2021 - 22:39:50 EST


From: "Kirill A. Shutemov" <kirill.shutemov@xxxxxxxxxxxxxxx>

MapGPA TDVMCALL requests the host VMM to map a GPA range as private or
shared memory mappings. Shared GPA mappings can be used for
communication beteen TD guest and host VMM, for example for
paravirtualized IO.

The new helper tdx_map_gpa() provides access to the operation.

Signed-off-by: Kirill A. Shutemov <kirill.shutemov@xxxxxxxxxxxxxxx>
Reviewed-by: Andi Kleen <ak@xxxxxxxxxxxxxxx>
Signed-off-by: Kuppuswamy Sathyanarayanan <sathyanarayanan.kuppuswamy@xxxxxxxxxxxxxxx>
---
arch/x86/include/asm/tdx.h | 2 ++
arch/x86/kernel/tdx.c | 28 ++++++++++++++++++++++++++++
2 files changed, 30 insertions(+)

diff --git a/arch/x86/include/asm/tdx.h b/arch/x86/include/asm/tdx.h
index 9bbfe6520ea4..efffdef35c78 100644
--- a/arch/x86/include/asm/tdx.h
+++ b/arch/x86/include/asm/tdx.h
@@ -105,5 +105,7 @@ long tdx_kvm_hypercall4(unsigned int nr, unsigned long p1, unsigned long p2,
unsigned long p3, unsigned long p4);

phys_addr_t tdx_shared_mask(void);
+
+int tdx_map_gpa(phys_addr_t gpa, int numpages, bool private);
#endif
#endif /* _ASM_X86_TDX_H */
diff --git a/arch/x86/kernel/tdx.c b/arch/x86/kernel/tdx.c
index 9681f4a0b4e0..f99fe54b4f88 100644
--- a/arch/x86/kernel/tdx.c
+++ b/arch/x86/kernel/tdx.c
@@ -14,6 +14,8 @@
#include "tdx-kvm.c"
#endif

+#define TDVMCALL_MAP_GPA 0x10001
+
static struct {
unsigned int gpa_width;
unsigned long attributes;
@@ -66,6 +68,32 @@ static void tdx_get_info(void)
physical_mask &= ~tdx_shared_mask();
}

+int tdx_map_gpa(phys_addr_t gpa, int numpages, bool private)
+{
+ register long r10 asm("r10") = TDVMCALL_STANDARD;
+ register long r11 asm("r11") = TDVMCALL_MAP_GPA;
+ register long r12 asm("r12") = gpa;
+ register long r13 asm("r13") = PAGE_SIZE * numpages;
+ register long rcx asm("rcx");
+ long ret;
+
+ if (!private)
+ r12 |= tdx_shared_mask();
+
+ /* Allow to pass R10, R11, R12 and R13 down to the VMM */
+ rcx = BIT(10) | BIT(11) | BIT(12) | BIT(13);
+
+ asm volatile(TDCALL
+ : "=a"(ret), "=r"(r10)
+ : "a"(TDVMCALL), "r"(rcx), "r"(r10), "r"(r11), "r"(r12),
+ "r"(r13)
+ : );
+
+ // Host kernel doesn't implement it yet.
+ // WARN_ON(ret || r10);
+ return ret || r10 ? -EIO : 0;
+}
+
static __cpuidle void tdx_halt(void)
{
register long r10 asm("r10") = TDVMCALL_STANDARD;
--
2.25.1