Skip to content

Commit f65aa0a

Browse files
kirylhansendc
authored andcommitted
x86/tdx: Dynamically disable SEPT violations from causing #VEs
Memory access #VEs are hard for Linux to handle in contexts like the entry code or NMIs. But other OSes need them for functionality. There's a static (pre-guest-boot) way for a VMM to choose one or the other. But VMMs don't always know which OS they are booting, so they choose to deliver those #VEs so the "other" OSes will work. That, unfortunately has left us in the lurch and exposed to these hard-to-handle #VEs. The TDX module has introduced a new feature. Even if the static configuration is set to "send nasty #VEs", the kernel can dynamically request that they be disabled. Once they are disabled, access to private memory that is not in the Mapped state in the Secure-EPT (SEPT) will result in an exit to the VMM rather than injecting a #VE. Check if the feature is available and disable SEPT #VE if possible. If the TD is allowed to disable/enable SEPT #VEs, the ATTR_SEPT_VE_DISABLE attribute is no longer reliable. It reflects the initial state of the control for the TD, but it will not be updated if someone (e.g. bootloader) changes it before the kernel starts. Kernel must check TDCS_TD_CTLS bit to determine if SEPT #VEs are enabled or disabled. [ dhansen: remove 'return' at end of function ] Fixes: 373e715 ("x86/tdx: Panic on bad configs that #VE on "private" memory access") Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com> Acked-by: Kai Huang <kai.huang@intel.com> Link: https://lore.kernel.org/all/20241104103803.195705-4-kirill.shutemov%40linux.intel.com
1 parent b064043 commit f65aa0a

File tree

2 files changed

+67
-17
lines changed

2 files changed

+67
-17
lines changed

arch/x86/coco/tdx/tdx.c

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ static inline void tdcall(u64 fn, struct tdx_module_args *args)
7979
}
8080

8181
/* Read TD-scoped metadata */
82-
static inline u64 __maybe_unused tdg_vm_rd(u64 field, u64 *value)
82+
static inline u64 tdg_vm_rd(u64 field, u64 *value)
8383
{
8484
struct tdx_module_args args = {
8585
.rdx = field,
@@ -194,6 +194,60 @@ static void __noreturn tdx_panic(const char *msg)
194194
__tdx_hypercall(&args);
195195
}
196196

197+
/*
198+
* The kernel cannot handle #VEs when accessing normal kernel memory. Ensure
199+
* that no #VE will be delivered for accesses to TD-private memory.
200+
*
201+
* TDX 1.0 does not allow the guest to disable SEPT #VE on its own. The VMM
202+
* controls if the guest will receive such #VE with TD attribute
203+
* ATTR_SEPT_VE_DISABLE.
204+
*
205+
* Newer TDX modules allow the guest to control if it wants to receive SEPT
206+
* violation #VEs.
207+
*
208+
* Check if the feature is available and disable SEPT #VE if possible.
209+
*
210+
* If the TD is allowed to disable/enable SEPT #VEs, the ATTR_SEPT_VE_DISABLE
211+
* attribute is no longer reliable. It reflects the initial state of the
212+
* control for the TD, but it will not be updated if someone (e.g. bootloader)
213+
* changes it before the kernel starts. Kernel must check TDCS_TD_CTLS bit to
214+
* determine if SEPT #VEs are enabled or disabled.
215+
*/
216+
static void disable_sept_ve(u64 td_attr)
217+
{
218+
const char *msg = "TD misconfiguration: SEPT #VE has to be disabled";
219+
bool debug = td_attr & ATTR_DEBUG;
220+
u64 config, controls;
221+
222+
/* Is this TD allowed to disable SEPT #VE */
223+
tdg_vm_rd(TDCS_CONFIG_FLAGS, &config);
224+
if (!(config & TDCS_CONFIG_FLEXIBLE_PENDING_VE)) {
225+
/* No SEPT #VE controls for the guest: check the attribute */
226+
if (td_attr & ATTR_SEPT_VE_DISABLE)
227+
return;
228+
229+
/* Relax SEPT_VE_DISABLE check for debug TD for backtraces */
230+
if (debug)
231+
pr_warn("%s\n", msg);
232+
else
233+
tdx_panic(msg);
234+
return;
235+
}
236+
237+
/* Check if SEPT #VE has been disabled before us */
238+
tdg_vm_rd(TDCS_TD_CTLS, &controls);
239+
if (controls & TD_CTLS_PENDING_VE_DISABLE)
240+
return;
241+
242+
/* Keep #VEs enabled for splats in debugging environments */
243+
if (debug)
244+
return;
245+
246+
/* Disable SEPT #VEs */
247+
tdg_vm_wr(TDCS_TD_CTLS, TD_CTLS_PENDING_VE_DISABLE,
248+
TD_CTLS_PENDING_VE_DISABLE);
249+
}
250+
197251
static void tdx_setup(u64 *cc_mask)
198252
{
199253
struct tdx_module_args args = {};
@@ -219,24 +273,12 @@ static void tdx_setup(u64 *cc_mask)
219273
gpa_width = args.rcx & GENMASK(5, 0);
220274
*cc_mask = BIT_ULL(gpa_width - 1);
221275

276+
td_attr = args.rdx;
277+
222278
/* Kernel does not use NOTIFY_ENABLES and does not need random #VEs */
223279
tdg_vm_wr(TDCS_NOTIFY_ENABLES, 0, -1ULL);
224280

225-
/*
226-
* The kernel can not handle #VE's when accessing normal kernel
227-
* memory. Ensure that no #VE will be delivered for accesses to
228-
* TD-private memory. Only VMM-shared memory (MMIO) will #VE.
229-
*/
230-
td_attr = args.rdx;
231-
if (!(td_attr & ATTR_SEPT_VE_DISABLE)) {
232-
const char *msg = "TD misconfiguration: SEPT_VE_DISABLE attribute must be set.";
233-
234-
/* Relax SEPT_VE_DISABLE check for debug TD. */
235-
if (td_attr & ATTR_DEBUG)
236-
pr_warn("%s\n", msg);
237-
else
238-
tdx_panic(msg);
239-
}
281+
disable_sept_ve(td_attr);
240282
}
241283

242284
/*

arch/x86/include/asm/shared/tdx.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,17 @@
1919
#define TDG_VM_RD 7
2020
#define TDG_VM_WR 8
2121

22-
/* TDCS fields. To be used by TDG.VM.WR and TDG.VM.RD module calls */
22+
/* TDX TD-Scope Metadata. To be used by TDG.VM.WR and TDG.VM.RD */
23+
#define TDCS_CONFIG_FLAGS 0x1110000300000016
24+
#define TDCS_TD_CTLS 0x1110000300000017
2325
#define TDCS_NOTIFY_ENABLES 0x9100000000000010
2426

27+
/* TDCS_CONFIG_FLAGS bits */
28+
#define TDCS_CONFIG_FLEXIBLE_PENDING_VE BIT_ULL(1)
29+
30+
/* TDCS_TD_CTLS bits */
31+
#define TD_CTLS_PENDING_VE_DISABLE BIT_ULL(0)
32+
2533
/* TDX hypercall Leaf IDs */
2634
#define TDVMCALL_MAP_GPA 0x10001
2735
#define TDVMCALL_GET_QUOTE 0x10002

0 commit comments

Comments
 (0)