x86/virt/tdx: Detect TDX during kernel boot
authorKai Huang <kai.huang@intel.com>
Fri, 8 Dec 2023 17:07:21 +0000 (09:07 -0800)
committerDave Hansen <dave.hansen@linux.intel.com>
Fri, 8 Dec 2023 17:11:58 +0000 (09:11 -0800)
Intel Trust Domain Extensions (TDX) protects guest VMs from malicious
host and certain physical attacks.  A CPU-attested software module
called 'the TDX module' runs inside a new isolated memory range as a
trusted hypervisor to manage and run protected VMs.

Pre-TDX Intel hardware has support for a memory encryption architecture
called MKTME.  The memory encryption hardware underpinning MKTME is also
used for Intel TDX.  TDX ends up "stealing" some of the physical address
space from the MKTME architecture for crypto-protection to VMs.  The
BIOS is responsible for partitioning the "KeyID" space between legacy
MKTME and TDX.  The KeyIDs reserved for TDX are called 'TDX private
KeyIDs' or 'TDX KeyIDs' for short.

During machine boot, TDX microcode verifies that the BIOS programmed TDX
private KeyIDs consistently and correctly programmed across all CPU
packages.  The MSRs are locked in this state after verification.  This
is why MSR_IA32_MKTME_KEYID_PARTITIONING gets used for TDX enumeration:
it indicates not just that the hardware supports TDX, but that all the
boot-time security checks passed.

The TDX module is expected to be loaded by the BIOS when it enables TDX,
but the kernel needs to properly initialize it before it can be used to
create and run any TDX guests.  The TDX module will be initialized by
the KVM subsystem when KVM wants to use TDX.

Detect platform TDX support by detecting TDX private KeyIDs.

The TDX module itself requires one TDX KeyID as the 'TDX global KeyID'
to protect its metadata.  Each TDX guest also needs a TDX KeyID for its
own protection.  Just use the first TDX KeyID as the global KeyID and
leave the rest for TDX guests.  If no TDX KeyID is left for TDX guests,
disable TDX as initializing the TDX module alone is useless.

[ dhansen: add X86_FEATURE, replace helper function ]

Signed-off-by: Kai Huang <kai.huang@intel.com>
Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com>
Reviewed-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Reviewed-by: Isaku Yamahata <isaku.yamahata@intel.com>
Reviewed-by: David Hildenbrand <david@redhat.com>
Reviewed-by: Dave Hansen <dave.hansen@linux.intel.com>
Reviewed-by: Kuppuswamy Sathyanarayanan <sathyanarayanan.kuppuswamy@linux.intel.com>
Link: https://lore.kernel.org/all/20231208170740.53979-1-dave.hansen%40intel.com
arch/x86/include/asm/cpufeatures.h
arch/x86/include/asm/msr-index.h
arch/x86/include/asm/tdx.h
arch/x86/kernel/cpu/common.c
arch/x86/virt/vmx/tdx/Makefile
arch/x86/virt/vmx/tdx/tdx.c [new file with mode: 0644]

index 4af140cf5719e6d75f5206d614002210bed782ca..45ddc6b6baaa1bb1a801d676300e4eb0dd58a7d1 100644 (file)
 #define X86_FEATURE_CAT_L3             ( 7*32+ 4) /* Cache Allocation Technology L3 */
 #define X86_FEATURE_CAT_L2             ( 7*32+ 5) /* Cache Allocation Technology L2 */
 #define X86_FEATURE_CDP_L3             ( 7*32+ 6) /* Code and Data Prioritization L3 */
+#define X86_FEATURE_TDX_HOST_PLATFORM  ( 7*32+ 7) /* Platform supports being a TDX host */
 #define X86_FEATURE_HW_PSTATE          ( 7*32+ 8) /* AMD HW-PState */
 #define X86_FEATURE_PROC_FEEDBACK      ( 7*32+ 9) /* AMD ProcFeedbackInterface */
 #define X86_FEATURE_XCOMPACTED         ( 7*32+10) /* "" Use compacted XSTATE (XSAVES or XSAVEC) */
index 1d51e1850ed03d46e84c71de0c451067d0baac5b..66c12d4efa31a26d526ef5b9a0dcc4c02f15ffc9 100644 (file)
 #define MSR_RELOAD_PMC0                        0x000014c1
 #define MSR_RELOAD_FIXED_CTR0          0x00001309
 
+/* KeyID partitioning between MKTME and TDX */
+#define MSR_IA32_MKTME_KEYID_PARTITIONING      0x00000087
+
 /*
  * AMD64 MSRs. Not complete. See the architecture manual for a more
  * complete list.
index f3d5305a60fc50b13708d80ffecab0a529239ee0..e5dd1cb0e1a100a2b982b0624ac95d38587aced4 100644 (file)
@@ -83,6 +83,9 @@ static inline long tdx_kvm_hypercall(unsigned int nr, unsigned long p1,
 u64 __seamcall(u64 fn, struct tdx_module_args *args);
 u64 __seamcall_ret(u64 fn, struct tdx_module_args *args);
 u64 __seamcall_saved_ret(u64 fn, struct tdx_module_args *args);
+void tdx_init(void);
+#else
+static inline void tdx_init(void) { }
 #endif /* CONFIG_INTEL_TDX_HOST */
 
 #endif /* !__ASSEMBLY__ */
index b14fc8c1c9538fb7dc827ed93e3174dacbc6d204..b968a2496e80b9dd908a15e6b9c1382ba21db62a 100644 (file)
@@ -66,6 +66,7 @@
 #include <asm/set_memory.h>
 #include <asm/traps.h>
 #include <asm/sev.h>
+#include <asm/tdx.h>
 
 #include "cpu.h"
 
@@ -1987,6 +1988,7 @@ static __init void identify_boot_cpu(void)
        setup_cr_pinning();
 
        tsx_init();
+       tdx_init();
        lkgs_init();
 }
 
index 46ef8f73aebbb1520b12233ac208db33355b0678..90da47eb85eec3a0f4d64b2ffa796f6d0c71013f 100644 (file)
@@ -1,2 +1,2 @@
 # SPDX-License-Identifier: GPL-2.0-only
-obj-y += seamcall.o
+obj-y += seamcall.o tdx.o
diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c
new file mode 100644 (file)
index 0000000..94689ae
--- /dev/null
@@ -0,0 +1,81 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright(c) 2023 Intel Corporation.
+ *
+ * Intel Trusted Domain Extensions (TDX) support
+ */
+
+#define pr_fmt(fmt)    "virt/tdx: " fmt
+
+#include <linux/types.h>
+#include <linux/cache.h>
+#include <linux/init.h>
+#include <linux/errno.h>
+#include <linux/printk.h>
+#include <asm/msr-index.h>
+#include <asm/msr.h>
+#include <asm/cpufeature.h>
+#include <asm/tdx.h>
+
+static u32 tdx_global_keyid __ro_after_init;
+static u32 tdx_guest_keyid_start __ro_after_init;
+static u32 tdx_nr_guest_keyids __ro_after_init;
+
+static __init int record_keyid_partitioning(u32 *tdx_keyid_start,
+                                           u32 *nr_tdx_keyids)
+{
+       u32 _nr_mktme_keyids, _tdx_keyid_start, _nr_tdx_keyids;
+       int ret;
+
+       /*
+        * IA32_MKTME_KEYID_PARTIONING:
+        *   Bit [31:0]:        Number of MKTME KeyIDs.
+        *   Bit [63:32]:       Number of TDX private KeyIDs.
+        */
+       ret = rdmsr_safe(MSR_IA32_MKTME_KEYID_PARTITIONING, &_nr_mktme_keyids,
+                       &_nr_tdx_keyids);
+       if (ret || !_nr_tdx_keyids)
+               return -EINVAL;
+
+       /* TDX KeyIDs start after the last MKTME KeyID. */
+       _tdx_keyid_start = _nr_mktme_keyids + 1;
+
+       *tdx_keyid_start = _tdx_keyid_start;
+       *nr_tdx_keyids = _nr_tdx_keyids;
+
+       return 0;
+}
+
+void __init tdx_init(void)
+{
+       u32 tdx_keyid_start, nr_tdx_keyids;
+       int err;
+
+       err = record_keyid_partitioning(&tdx_keyid_start, &nr_tdx_keyids);
+       if (err)
+               return;
+
+       pr_info("BIOS enabled: private KeyID range [%u, %u)\n",
+                       tdx_keyid_start, tdx_keyid_start + nr_tdx_keyids);
+
+       /*
+        * The TDX module itself requires one 'global KeyID' to protect
+        * its metadata.  If there's only one TDX KeyID, there won't be
+        * any left for TDX guests thus there's no point to enable TDX
+        * at all.
+        */
+       if (nr_tdx_keyids < 2) {
+               pr_err("initialization failed: too few private KeyIDs available.\n");
+               return;
+       }
+
+       /*
+        * Just use the first TDX KeyID as the 'global KeyID' and
+        * leave the rest for TDX guests.
+        */
+       tdx_global_keyid = tdx_keyid_start;
+       tdx_guest_keyid_start = tdx_keyid_start + 1;
+       tdx_nr_guest_keyids = nr_tdx_keyids - 1;
+
+       setup_force_cpu_cap(X86_FEATURE_TDX_HOST_PLATFORM);
+}