[RFC v1 04/26] x86/tdx: Get TD execution environment information via TDINFO

From: Kuppuswamy Sathyanarayanan
Date: Fri Feb 05 2021 - 23:30:22 EST


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

Per Guest-Host-Communication Interface (GHCI) for Intel Trust
Domain Extensions (Intel TDX) specification, sec 2.4.2,
TDCALL[TDINFO] provides basic TD execution environment information, not
provided by CPUID.

Call TDINFO during early boot to be used for following system
initialization.

The call provides info on which bit in pfn is used to indicate that the
page is shared with the host and attributes of the TD, such as debug.

We don't save information about the number of cpus as there's no users
so far.

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 | 9 +++++++++
arch/x86/kernel/tdx.c | 27 +++++++++++++++++++++++++++
2 files changed, 36 insertions(+)

diff --git a/arch/x86/include/asm/tdx.h b/arch/x86/include/asm/tdx.h
index 0b9d571b1f95..f8cdc8eb1046 100644
--- a/arch/x86/include/asm/tdx.h
+++ b/arch/x86/include/asm/tdx.h
@@ -7,6 +7,15 @@

#ifdef CONFIG_INTEL_TDX_GUEST

+/*
+ * TDCALL instruction is newly added in TDX architecture,
+ * used by TD for requesting the host VMM to provide
+ * (untrusted) services.
+ */
+#define TDCALL ".byte 0x66,0x0f,0x01,0xcc"
+
+#define TDINFO 1
+
/* Common API to check TDX support in decompression and common kernel code. */
bool is_tdx_guest(void);

diff --git a/arch/x86/kernel/tdx.c b/arch/x86/kernel/tdx.c
index e44e55d1e519..13303bfdfdd1 100644
--- a/arch/x86/kernel/tdx.c
+++ b/arch/x86/kernel/tdx.c
@@ -3,6 +3,14 @@

#include <asm/tdx.h>
#include <asm/cpufeature.h>
+#include <linux/cpu.h>
+#include <asm/tdx.h>
+#include <asm/vmx.h>
+
+static struct {
+ unsigned int gpa_width;
+ unsigned long attributes;
+} td_info __ro_after_init;

static inline bool cpuid_has_tdx_guest(void)
{
@@ -26,6 +34,23 @@ bool is_tdx_guest(void)
}
EXPORT_SYMBOL_GPL(is_tdx_guest);

+static void tdx_get_info(void)
+{
+ register long rcx asm("rcx");
+ register long rdx asm("rdx");
+ register long r8 asm("r8");
+ long ret;
+
+ asm volatile(TDCALL
+ : "=a"(ret), "=c"(rcx), "=r"(rdx), "=r"(r8)
+ : "a"(TDINFO)
+ : "r9", "r10", "r11", "memory");
+ BUG_ON(ret);
+
+ td_info.gpa_width = rcx & GENMASK(5, 0);
+ td_info.attributes = rdx;
+}
+
void __init tdx_early_init(void)
{
if (!cpuid_has_tdx_guest())
@@ -33,5 +58,7 @@ void __init tdx_early_init(void)

setup_force_cpu_cap(X86_FEATURE_TDX_GUEST);

+ tdx_get_info();
+
pr_info("TDX guest is initialized\n");
}
--
2.25.1