Add TDX Guest Support
RFC 1/1 x86: Introduce generic protected guest abstraction
In reply to Borislav PetkovAdd a generic way to check if we run with an encrypted guest, without requiring x86 specific ifdefs. This can then be used in non architecture specific code.
prot_guest_has() is used to check for protected guest feature flags.
---
Changes since RFC v2-fix-v3:
* Introduced ARCH_HAS_PROTECTED_GUEST and moved arch specific checks to asm/protected_guest.h
Changes since RFC v2-fix-v2:
* Renamed protected_guest_has() to prot_guest_has().
* Changed flag prefix from VM_ to PR_GUEST_
* Merged Borislav AMD implementation fix.
arch/Kconfig | 3 +++ arch/x86/Kconfig | 2 ++ arch/x86/include/asm/protected_guest.h | 20 ++++++++++++++++++++ arch/x86/include/asm/sev.h | 3 +++ arch/x86/include/asm/tdx.h | 7 +++++++ arch/x86/kernel/sev.c | 15 +++++++++++++++ arch/x86/kernel/tdx.c | 15 +++++++++++++++ arch/x86/mm/mem_encrypt.c | 1 + include/linux/protected_guest.h | 24 ++++++++++++++++++++++++ 9 files changed, 90 insertions(+) create mode 100644 arch/x86/include/asm/protected_guest.h create mode 100644 include/linux/protected_guest.h
arch/Kconfig +3 -0
@@ -1011,6 +1011,9 @@ config HAVE_ARCH_NVRAM_OPS config ISA_BUS_API def_bool ISA +config ARCH_HAS_PROTECTED_GUEST + bool + # # ABI hall of shame #
arch/x86/Kconfig +2 -0
@@ -883,6 +883,7 @@ config INTEL_TDX_GUEST select PARAVIRT_XL select X86_X2APIC select SECURITY_LOCKDOWN_LSM + select ARCH_HAS_PROTECTED_GUEST help Provide support for running in a trusted domain on Intel processors equipped with Trusted Domain eXtenstions. TDX is a new Intel
@@ -1544,6 +1545,7 @@ config AMD_MEM_ENCRYPT select ARCH_HAS_FORCE_DMA_UNENCRYPTED select INSTRUCTION_DECODER select ARCH_HAS_RESTRICTED_VIRTIO_MEMORY_ACCESS + select ARCH_HAS_PROTECTED_GUEST help Say yes to enable support for the encryption of system memory. This requires an AMD processor that supports Secure Memory
arch/x86/include/asm/protected_guest.h new file +20 -0
@@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* Copyright (C) 2020 Intel Corporation */ +#ifndef _ASM_PROTECTED_GUEST_H +#define _ASM_PROTECTED_GUEST_H 1 + +#include <asm/processor.h> +#include <asm/tdx.h> +#include <asm/sev.h> + +static inline bool prot_guest_has (unsigned long flag) +{ +if (is_tdx_guest ()) +return tdx_protected_guest_has (flag); +else if (boot_cpu_data.x86_vendor ==X86_VENDOR_AMD ) +return sev_protected_guest_has (flag); + +return false; +} + +#endif /* _ASM_PROTECTED_GUEST_H */
arch/x86/include/asm/sev.h +3 -0
@@ -81,12 +81,15 @@ static __always_inline void sev_es_nmi_complete(void)__sev_es_nmi_complete (); }extern int __init sev_es_efi_map_ghcbs (pgd_t *pgd); +bool sev_protected_guest_has (unsigned long flag); +#else static inline void sev_es_ist_enter (struct pt_regs *regs) { }static inline void sev_es_ist_exit (void ) { }static inline int sev_es_setup_ap_jump_table (struct real_mode_header *rmh) {return 0 ; }static inline void sev_es_nmi_complete (void ) { }static inline int sev_es_efi_map_ghcbs (pgd_t *pgd) {return 0 ; } +static inline bool sev_protected_guest_has (unsigned long flag) {return false; }#endif #endif
arch/x86/include/asm/tdx.h +7 -0
@@ -71,6 +71,8 @@ u64 __tdx_module_call(u64 fn, u64 rcx, u64 rdx, u64 r8, u64 r9,u64 __tdx_hypercall (u64 fn,u64 r12,u64 r13,u64 r14,u64 r15,struct tdx_hypercall_output *out); +bool tdx_protected_guest_has (unsigned long flag); +#else // !CONFIG_INTEL_TDX_GUEST static inline bool is_tdx_guest (void )
@@ -80,6 +82,11 @@ static inline bool is_tdx_guest(void)static inline void tdx_early_init (void ) { }; +static inline bool tdx_protected_guest_has (unsigned long flag) +{ +return false; +} +#endif /* CONFIG_INTEL_TDX_GUEST */ #ifdef CONFIG_INTEL_TDX_GUEST_KVM
arch/x86/kernel/sev.c +15 -0
@@ -19,6 +19,7 @@#include <linux/memblock.h> #include <linux/kernel.h> #include <linux/mm.h> +#include <linux/protected_guest.h> #include <asm/cpu_entry_area.h> #include <asm/stacktrace.h>
@@ -1493,3 +1494,17 @@ bool __init handle_vc_boot_ghcb(struct pt_regs *regs)while (true)halt (); } + +bool sev_protected_guest_has (unsigned long flag) +{ +switch (flag) { +case PR_GUEST_MEM_ENCRYPT : +case PR_GUEST_MEM_ENCRYPT_ACTIVE : +case PR_GUEST_UNROLL_STRING_IO : +case PR_GUEST_HOST_MEM_ENCRYPT : +return true; + } + +return false; +} +EXPORT_SYMBOL_GPL (sev_protected_guest_has);
arch/x86/kernel/tdx.c +15 -0
@@ -7,6 +7,7 @@#include <asm/vmx.h> #include <linux/cpu.h> +#include <linux/protected_guest.h> /* TDX Module call Leaf IDs */ #define TDINFO 1
@@ -75,6 +76,20 @@ bool is_tdx_guest(void) }EXPORT_SYMBOL_GPL (is_tdx_guest); +bool tdx_protected_guest_has (unsigned long flag) +{ +switch (flag) { +case PR_GUEST_MEM_ENCRYPT : +case PR_GUEST_MEM_ENCRYPT_ACTIVE : +case PR_GUEST_UNROLL_STRING_IO : +case PR_GUEST_SHARED_MAPPING_INIT : +return true; + } + +return false; +} +EXPORT_SYMBOL_GPL (tdx_protected_guest_has); +static void tdg_get_info (void ) {u64 ret;
arch/x86/mm/mem_encrypt.c +1 -0
@@ -20,6 +20,7 @@#include <linux/bitops.h> #include <linux/dma-mapping.h> #include <linux/virtio_config.h> +#include <linux/protected_guest.h> #include <asm/tlbflush.h> #include <asm/fixmap.h>
include/linux/protected_guest.h new file +24 -0
@@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _LINUX_PROTECTED_GUEST_H +#define _LINUX_PROTECTED_GUEST_H 1 + +/* Protected Guest Feature Flags (leave 0-0xff for arch specific flags) */ + +/* Support for guest encryption */ +#define PR_GUEST_MEM_ENCRYPT 0x100 +/* Encryption support is active */ +#define PR_GUEST_MEM_ENCRYPT_ACTIVE 0x101 +/* Support for unrolled string IO */ +#define PR_GUEST_UNROLL_STRING_IO 0x102 +/* Support for host memory encryption */ +#define PR_GUEST_HOST_MEM_ENCRYPT 0x103 +/* Support for shared mapping initialization (after early init) */ +#define PR_GUEST_SHARED_MAPPING_INIT 0x104 + +#ifdef CONFIG_ARCH_HAS_PROTECTED_GUEST +#include <asm/protected_guest.h> +#else +static inline bool prot_guest_has (unsigned long flag) {return false; } +#endif + +#endif /* _LINUX_PROTECTED_GUEST_H */
Signature · 1 line
2.25.1
Re: [RFC v2-fix-v4 1/1] x86: Introduce generic protected guest abstraction
Hi All,
Quoted 11 lines · Kuppuswamy Sathyanarayanan
Add a generic way to check if we run with an encrypted guest, without requiring x86 specific ifdefs. This can then be used in non architecture specific code.
prot_guest_has()is used to check for protected guest feature flags.Originally-by Andi Kleen <[email protected]>Signed-off-by Kuppuswamy Sathyanarayanan <[email protected]>---
I have sent a non RFC version of this patch for x86 review. Please use it for further
discussion.
https://lore.kernel.org/patchwork/patch/1444184/
Quoted 8 lines
Changes since RFC v2-fix-v3:
* IntroducedARCH_HAS_PROTECTED_GUESTand moved arch specific checks toasm/protected_guest.hChanges since RFC v2-fix-v2:
* Renamedprotected_guest_has()toprot_guest_has().
* Changed flag prefix fromVM_toPR_GUEST_
* Merged Borislav AMD implementation fix.
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
RFC 2/3 x86/tdx: Handle early IO operations
In reply to Kuppuswamy SathyanarayananFrom: Andi Kleen <[email protected]>
Add an early #VE handler to convert early port IOs into TDCALLs.
TDX cannot do port IO directly. The TDX module triggers a #VE exception to let the guest kernel to emulate operations like IO ports, by converting them into TDCALLs to call the host.
A fully featured #VE handler support for port IO will be added later in this patch set (in patch titled "x86/tdx: Handle port I/O). But it can be used only at later point in the boot process. So to support port IO in early boot code, add a minimal support in early exception handler. This is similar to what AMD SEV does.
This is mainly to support early_printk's serial driver, as well as potentially the VGA driver (although it is expected not to be used).
The early handler only does IO calls and nothing else, and anything that goes wrong results in a normal early exception panic.
It cannot share the code paths with the normal #VE handler because it needs to avoid using trace calls or printk.
This early handler allows us to use the normal in*/out* macros without patching them for every driver. We don't expect IO port IO to be performance critical at all, so an extra #VE exception is no problem. There are also no concerns with nesting, since there should be no NMIs this early.
--- arch/x86/include/asm/tdx.h | 6 ++++ arch/x86/kernel/head64.c | 4 +++ arch/x86/kernel/tdx.c | 59 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+)
arch/x86/include/asm/tdx.h +6 -0
@@ -72,6 +72,7 @@ u64 __tdx_hypercall(u64 fn, u64 r12, u64 r13, u64 r14, u64 r15, struct tdx_hypercall_output *out);bool tdx_protected_guest_has (unsigned long flag); +bool tdg_early_handle_ve (struct pt_regs *regs);#else // !CONFIG_INTEL_TDX_GUEST
@@ -87,6 +88,11 @@ static inline bool tdx_protected_guest_has(unsigned long flag) return false; } +static inline bool tdg_early_handle_ve (struct pt_regs *regs) +{ +return false; +} +#endif /* CONFIG_INTEL_TDX_GUEST */ #ifdef CONFIG_INTEL_TDX_GUEST_KVM
arch/x86/kernel/head64.c +4 -0
@@ -410,6 +410,10 @@ void __init do_early_exception(struct pt_regs *regs, int trapnr) trapnr == X86_TRAP_VC && handle_vc_boot_ghcb(regs)) return; +if (IS_ENABLED (CONFIG_INTEL_TDX_GUEST ) && + trapnr ==X86_TRAP_VE &&tdg_early_handle_ve (regs)) +return ; +early_fixup_exception (regs, trapnr); }
arch/x86/kernel/tdx.c +59 -0
71 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [RFC v2-fix-v1 2/3] x86/tdx: Handle early IO operations
Quoted 7 lines · Kuppuswamy Sathyanarayanan
From: Andi Kleen <[email protected]>
Add an early #VE handler to convert early port IOs into TDCALLs.
TDX cannot do port IO directly. The TDX module triggers a #VE exception to let the guest kernel to emulate operations like IO ports, by converting them into TDCALLs to call the host.
s,kernel to emulate operations like IO ports,kernel emulate port I/O,
Quoted 7 lines
A fully featured #VE handler support for port IO will be added later in this patch set (in patch titled "x86/tdx: Handle port I/O). But it can be used only at later point in the boot process. So to support port IO in early boot code, add a minimal support in early exception handler. This is similar to what AMD SEV does.
Clarify "fully featured". I naively thought that the notes below about trace and printk were implying that the full featured #VE handler will use printk() so it can't also use #VE since printk() would recurse into the #VE handler if the serial console is using port IO.
...but that does not seem to be the reason since:
http://lore.kernel.org/r/20210527042356.3983284-4-sathyanarayanan.kuppuswamy@linux.intel.com
...is also using #VE for port IO emulation?
Quoted 8 lines
This is mainly to support early_printk's serial driver, as well as potentially the VGA driver (although it is expected not to be used).
The early handler only does IO calls and nothing else, and anything that goes wrong results in a normal early exception panic.
Quoted 7 lines
It cannot share the code paths with the normal #VE handler because it needs to avoid using trace calls or printk.
This early handler allows us to use the normal in*/out* macros without patching them for every driver. We don't expect IO port IO to be performance critical at all, so an extra #VE exception is no problem.
"There is no expectation that early port IO is performance critical, so the #VE emulation cost is worth the simplicity benefit of not patching out port IO usage in early code."
Quoted 4 lines
There are also no concerns
with nesting, since there should be no NMIs this early.Signed-off-by Andi Kleen <[email protected]>
Missing a Sathya signed-off-by.
Quoted 37 lines
---
arch/x86/include/asm/tdx.h | 6 ++++ arch/x86/kernel/head64.c | 4 +++
arch/x86/kernel/tdx.c | 59 ++++++++++++++++++++++++++++++++++++++3 files changed, 69 insertions(+)arch/x86/include/asm/tdx.h +0
-0@@ -72,6 +72,7 @@ u64 __tdx_hypercall(u64 fn, u64 r12, u64 r13, u64 r14, u64 r15, struct tdx_hypercall_output *out);
booltdx_protected_guest_has(unsigned long flag); +booltdg_early_handle_ve(struct pt_regs *regs);
#else // !CONFIG_INTEL_TDX_GUESTarch/x86/include/asm/tdx.h +0
-0@@ -87,6 +88,11 @@ static inline bool tdx_protected_guest_has(unsigned long flag) return false; }arch/x86/include/asm/tdx.h +5
-0+static inline bool tdg_early_handle_ve (struct pt_regs *regs) +{ + return false; +} +#endif /*
CONFIG_INTEL_TDX_GUEST*/
#ifdefCONFIG_INTEL_TDX_GUEST_KVMarch/x86/kernel/head64.c +0
-0@@ -410,6 +410,10 @@ void __init do_early_exception(struct pt_regs *regs, int trapnr) trapnr == X86_TRAP_VC && handle_vc_boot_ghcb(regs)) return;
Quoted 2 lines
+ if (IS_ENABLED(CONFIG_INTEL_TDX_GUEST)&&
This explicit IS_ENABLED() is unnecessary given tdg_early_handle_ve() returns false in the CONFIG_INTEL_TDX_GUEST=n case as defined above.
Quoted 13 lines
+ trapnr ==
X86_TRAP_VE&&tdg_early_handle_ve(regs)) + return;
+
early_fixup_exception(regs, trapnr);
}arch/x86/kernel/tdx.c +0
-0@@ -13,6 +13,10 @@ #define TDINFO 1 #define TDGETVEINFO 3
Quoted 2 lines
+#define VE_GET_IO_TYPE(exit_qual) (((exit_qual) & 8) ? 0 : 1)
How about VE_IS_IO_OUT()? To match its usage as a flag below...
Quoted 17 lines
arch/x86/kernel/tdx.c +3
-0+#define VE_GET_IO_SIZE (exit_qual) (((exit_qual) & 7) + 1) +#define VE_GET_PORT_NUM (exit_qual) ((exit_qual) >> 16) +static struct {
unsigned intgpa_width;
unsigned long attributes;arch/x86/kernel/tdx.c +0
-0@@ -256,6 +260,61 @@ int tdg_handle_virtualization_exception(struct pt_regs *regs, return ret; }arch/x86/kernel/tdx.c +7
-0+/* +* Handle early IO, mainly for early printks serial output. +* This avoids anything that doesn't work early on, like tracing +* or printks, by calling the low level functions directly. Any +* problems are handled by falling back to a standard early exception. +* +* Assumes the IO instruction was using ax, which is enforced
arch/x86/kernel/tdx.c int tdg_handle_virtualization_exception(struct pt_regs *regs, · line 279
Quoted 10 lines
arch/x86/kernel/tdx.c +10
-0+* by the standard io.h macros. +*/ +static __init bool tdx_early_io (struct ve_info *ve,struct pt_regs *regs) +{ + struct tdx_hypercall_output outh; + int out = VE_GET_IO_TYPE(ve->exit_qual); + int size = VE_GET_IO_SIZE(ve->exit_qual); + int port = VE_GET_PORT_NUM(ve->exit_qual); + int ret; +
...and if @out is a flag then the below can be simplified to:
ret = __tdx_hypercall(EXIT_REASON_IO_INSTRUCTION, size, out, port,
regs->ax, &outh);
if (!out && !ret) {
u64 mask = GENMASK(8 * size, 0);
regs->ax &= ~mask;
regs->ax |= outh.r11 & mask;
}
return !ret;With the above fixups and clarifications you can add:
...but the discussion here about fully featured leaves me confused about the approach taken in the next patch.
Quoted 41 lines
+ if (out) {
+ ret = __tdx_hypercall(EXIT_REASON_IO_INSTRUCTION, + size, 1, port,
+regs->ax,
+ &outh);
+ } else {
+ u64 mask =GENMASK(8 * size, 0);arch/x86/kernel/tdx.c +31
-0+ + ret = __tdx_hypercall(EXIT_REASON_IO_INSTRUCTION, + size, 0, port, + regs->ax, &outh); + if (!ret) { + regs->ax &= ~mask; + regs->ax |= outh.r11 & mask; + } + } + + return !ret; +} + +/* +* Early #VE exception handler. Just used to handle port IOs +* for early_printk. If anything goes wrong handle it like +* a normal early exception. +*/ +__init bool tdg_early_handle_ve (struct pt_regs *regs) +{ + struct ve_info ve; + + if (tdg_get_ve_info(&ve)) + return false; + + if (ve.exit_reason == EXIT_REASON_IO_INSTRUCTION) + return tdx_early_io(&ve, regs); + + return false; +} +void
__inittdx_early_init(void)
{
if (!cpuid_has_tdx_guest())
RFC 3/3 x86/tdx: Handle port I/O
In reply to Kuppuswamy SathyanarayananFrom: "Kirill A. Shutemov" <[email protected]>
TDX hypervisors cannot emulate instructions directly. This includes port IO which is normally emulated in the hypervisor. All port IO instructions inside TDX trigger the #VE exception in the guest and would be normally emulated there.
For the really early code in the decompressor, #VE cannot be used because the IDT needed for handling the exception is not set-up, and some other infrastructure needed by the handler is missing. So to support port IO in decompressor code, add support for paravirt based I/O port virtualization.
Also string I/O is not supported in TDX guest. So, unroll the string I/O operation into a loop operating on one element at a time. This method is similar to AMD SEV, so just extend the support for TDX guest platform.
--- arch/x86/boot/compressed/Makefile | 1 + arch/x86/boot/compressed/tdcall.S | 3 ++ arch/x86/boot/compressed/tdx.c | 28 ++++++++++++++++++ arch/x86/include/asm/io.h | 7 +++-- arch/x86/include/asm/tdx.h | 47 ++++++++++++++++++++++++++++++- arch/x86/kernel/tdx.c | 39 +++++++++++++++++++++++++ 6 files changed, 122 insertions(+), 3 deletions(-) create mode 100644 arch/x86/boot/compressed/tdcall.S
arch/x86/boot/compressed/Makefile +1 -0
@@ -97,6 +97,7 @@ endif vmlinux-objs-$(CONFIG_ACPI) += $(obj)/acpi.o vmlinux-objs-$(CONFIG_INTEL_TDX_GUEST) += $(obj)/tdx.o +vmlinux-objs-$(CONFIG_INTEL_TDX_GUEST) += $(obj)/tdcall.o vmlinux-objs-$(CONFIG_EFI_MIXED) += $(obj)/efi_thunk_$(BITS).o efi-obj-$(CONFIG_EFI_STUB) = $(objtree)/drivers/firmware/efi/libstub/lib.a
arch/x86/boot/compressed/tdcall.S new file +3 -0
@@ -0,0 +1,3 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#include "../../kernel/tdcall.S"
arch/x86/boot/compressed/tdx.c +28 -0
@@ -4,6 +4,8 @@*/ #include <asm/tdx.h> +#include <asm/vmx.h> +#include <vdso/limits.h> static int __ro_after_init tdx_guest =-1 ;
@@ -30,3 +32,29 @@ bool is_tdx_guest(void)return !!tdx_guest; } +/* +* Helper function used for making hypercall for "out" +* instruction. It will be called from __out IO +* macro (in tdx.h). +*/ +void tdg_out (int size,int port,unsigned int value) +{ +__tdx_hypercall (EXIT_REASON_IO_INSTRUCTION , size,1 , + port, value,NULL ); +} + +/* +* Helper function used for making hypercall for "in" +* instruction. It will be called from __in IO macro +* (in tdx.h). If IO is failed, it will return all 1s. +*/ +unsigned int tdg_in (int size,int port) +{ +struct tdx_hypercall_output out = {0 }; +int err; + + err =__tdx_hypercall (EXIT_REASON_IO_INSTRUCTION , size,0 , + port,0 , &out); + +return err ?UINT_MAX : out.r11; +}
arch/x86/include/asm/io.h +5 -2
@@ -40,6 +40,7 @@#include <linux/string.h> #include <linux/compiler.h> +#include <linux/protected_guest.h> #include <asm/page.h> #include <asm/early_ioremap.h> #include <asm/pgtable_types.h>
@@ -309,7 +310,8 @@ static inline unsigned type in##bwl##_p(int port) \ \ static inline void outs##bwl(int port, const void *addr, unsigned long count) \ { \ - if (sev_key_active()) { \ + if (sev_key_active() || \ + protected_guest_has(VM_UNROLL_STRING_IO)) { \ unsigned type *value = (unsigned type *)addr; \ while (count) { \ out##bwl(*value, port); \
@@ -325,7 +327,8 @@ static inline void outs##bwl(int port, const void *addr, unsigned long count) \ \ static inline void ins##bwl(int port, void *addr, unsigned long count) \ { \ - if (sev_key_active()) { \ + if (sev_key_active() || \ + protected_guest_has(VM_UNROLL_STRING_IO)) { \ unsigned type *value = (unsigned type *)addr; \ while (count) { \ *value = in##bwl(port); \
arch/x86/include/asm/tdx.h +46 -1
64 lines not on this page · raw message
arch/x86/kernel/tdx.c +39 -0
@@ -202,6 +202,42 @@ static void tdg_handle_cpuid(struct pt_regs *regs) regs->dx = out.r15; } +void tdg_out (int size,int port,unsigned int value) +{ +tdx_hypercall (EXIT_REASON_IO_INSTRUCTION , size,1 , port, value); +} + +unsigned int tdg_in (int size,int port) +{ +struct tdx_hypercall_output out = {0 }; +u64 err; + + err =__tdx_hypercall (EXIT_REASON_IO_INSTRUCTION , size,0 , + port,0 , &out); + +return err ?UINT_MAX : out.r11; +} + +static void tdg_handle_io (struct pt_regs *regs,u32 exit_qual) +{ +bool string = exit_qual &16 ; +int out, size, port; + +/* I/O strings ops are unrolled at build time. */ +BUG_ON (string); + + out =VE_GET_IO_TYPE (exit_qual); + size =VE_GET_IO_SIZE (exit_qual); + port =VE_GET_PORT_NUM (exit_qual); + +if (out) { +tdg_out (size, port, regs->ax); + }else { + regs->ax &= ~GENMASK (8 * size,0 ); + regs->ax |=tdg_in (size, port) &GENMASK (8 * size,0 ); + } +} +unsigned long tdg_get_ve_info (struct ve_info *ve) {u64 ret;
@@ -248,6 +284,9 @@ int tdg_handle_virtualization_exception(struct pt_regs *regs,case EXIT_REASON_CPUID :tdg_handle_cpuid (regs);break ; +case EXIT_REASON_IO_INSTRUCTION : +tdg_handle_io (regs, ve->exit_qual); +break ;default :pr_warn ("Unexpected #VE: %lld\n" , ve->exit_reason);return -EFAULT ;
Signature · 1 line
2.25.1
Re: [RFC v2-fix-v1 3/3] x86/tdx: Handle port I/O
Quoted 14 lines · Kuppuswamy Sathyanarayanan
From: "Kirill A. Shutemov" <[email protected]>
TDX hypervisors cannot emulate instructions directly. This includes port IO which is normally emulated in the hypervisor. All port IO instructions inside TDX trigger the #VE exception in the guest and would be normally emulated there.
For the really early code in the decompressor, #VE cannot be used because the IDT needed for handling the exception is not set-up, and some other infrastructure needed by the handler is missing. So to support port IO in decompressor code, add support for paravirt based I/O port virtualization.
Quoted 4 lines
Also string I/O is not supported in TDX guest. So, unroll the string I/O operation into a loop operating on one element at a time. This method is similar to AMD SEV, so just extend the support for TDX guest platform.
Given early port IO is broken out in its own previous I think it makes sense to break out the decompressor port IO enabling from final runtime port IO support.
The argument in the previous patch about using #VE emulation in the early code was collisions with trace and printk support in the "fully featured" #VE handler later in the series. My interpretation of that collision was due to the possibility of the #VE handler going into infinite recursion if a printk in the handler triggered port IO. It seems I do not have the right picture of the constraints. Given the runtime kernel can direct replace in/out macros I would expect a statement of the tradeoff with #VE emulation and why the post decompressor code is still using emulation.
Quoted 69 lines
Co-developed-by Kuppuswamy Sathyanarayanan <[email protected]>Signed-off-by Kuppuswamy Sathyanarayanan <[email protected]>Signed-off-by Kirill A. Shutemov <[email protected]>Reviewed-by Andi Kleen <[email protected]>--- arch/x86/boot/compressed/Makefile | 1 + arch/x86/boot/compressed/tdcall.S | 3 ++ arch/x86/boot/compressed/tdx.c | 28 ++++++++++++++++++ arch/x86/include/asm/io.h | 7 +++-- arch/x86/include/asm/tdx.h | 47 ++++++++++++++++++++++++++++++- arch/x86/kernel/tdx.c | 39 +++++++++++++++++++++++++ 6 files changed, 122 insertions(+), 3 deletions(-) create mode 100644 arch/x86/boot/compressed/tdcall.Sarch/x86/boot/compressed/Makefile +1
-0@@ -97,6 +97,7 @@ endif vmlinux-objs-$(CONFIG_ACPI) += $(obj)/acpi.o vmlinux-objs-$(CONFIG_INTEL_TDX_GUEST) += $(obj)/tdx.o +vmlinux-objs-$(CONFIG_INTEL_TDX_GUEST) += $(obj)/tdcall.o vmlinux-objs-$(CONFIG_EFI_MIXED) += $(obj)/efi_thunk_$(BITS).o efi-obj-$(CONFIG_EFI_STUB) = $(objtree)/drivers/firmware/efi/libstub/lib.aarch/x86/boot/compressed/tdcall.S new file +3
-0@@ -0,0 +1,3 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#include "../../kernel/tdcall.S"arch/x86/boot/compressed/tdx.c +18
-0@@ -4,6 +4,8 @@*/ #include <asm/tdx.h> +#include <asm/vmx.h> +#include <vdso/limits.h> static int __ro_after_init tdx_guest =-1 ;@@ -30,3 +32,29 @@ bool is_tdx_guest(void)return !!tdx_guest; } +/* +* Helper function used for making hypercall for "out" +* instruction. It will be called from __out IO +* macro (in tdx.h). +*/ +void tdg_out (int size,int port,unsigned int value) +{ +__tdx_hypercall (EXIT_REASON_IO_INSTRUCTION , size,1 , + port, value,NULL ); +} + +/* +* Helper function used for making hypercall for "in" +* instruction. It will be called from __in IO macro +* (in tdx.h). If IO is failed, it will return all 1s. +*/
arch/x86/boot/compressed/tdx.c bool is_tdx_guest(void) · line 60
Quoted 10 lines
arch/x86/boot/compressed/tdx.c +10
-0+unsigned int tdg_in (int size,int port) +{ +struct tdx_hypercall_output out = {0 }; +int err; + + err =__tdx_hypercall (EXIT_REASON_IO_INSTRUCTION , size,0 , + port,0 , &out); + +return err ?UINT_MAX : out.r11; +}
The previous patch open coded tdg_{in,out} and this one provides helpers. I think at a minimum they should be consistent and pick one style.
Quoted 52 lines
arch/x86/include/asm/io.h +5
-2@@ -40,6 +40,7 @@#include <linux/string.h> #include <linux/compiler.h> +#include <linux/protected_guest.h> #include <asm/page.h> #include <asm/early_ioremap.h> #include <asm/pgtable_types.h> @@ -309,7 +310,8 @@ static inline unsigned type in##bwl##_p(int port) \ \ static inline void outs##bwl(int port, const void *addr, unsigned long count) \ { \ - if (sev_key_active()) { \ + if (sev_key_active() || \ + protected_guest_has(VM_UNROLL_STRING_IO)) { \ unsigned type *value = (unsigned type *)addr; \ while (count) { \ out##bwl(*value, port); \@@ -325,7 +327,8 @@ static inline void outs##bwl(int port, const void *addr, unsigned long count) \ \ static inline void ins##bwl(int port, void *addr, unsigned long count) \ { \ - if (sev_key_active()) { \ + if (sev_key_active() || \ + protected_guest_has(VM_UNROLL_STRING_IO)) { \ unsigned type *value = (unsigned type *)addr; \ while (count) { \ *value = in##bwl(port); \arch/x86/include/asm/tdx.h +5
-0@@ -5,6 +5,8 @@#define TDX_CPUID_LEAF_ID 0x21 +#ifndef __ASSEMBLY__ +#ifdef CONFIG_INTEL_TDX_GUEST #include <asm/cpufeature.h> @@ -74,6 +76,48 @@ u64 __tdx_hypercall(u64 fn, u64 r12, u64 r13, u64 r14, u64 r15,bool tdx_protected_guest_has (unsigned long flag);bool tdg_early_handle_ve (struct pt_regs *regs); +void tdg_out (int size,int port,unsigned int value); +unsigned int tdg_in (int size,int port); +
arch/x86/include/asm/tdx.h u64 __tdx_hypercall(u64 fn, u64 r12, u64 r13, u64 r14, u64 r15, · line 91
Quoted 10 lines
arch/x86/include/asm/tdx.h +10
-0+/* Helper function for converting {b,w,l} to byte size */ +static inline int tdx_get_iosize (char *str) +{ +if (str[0 ] =='w' ) +return 2 ; +else if (str[0 ] =='l' ) +return 4 ; + +return 1 ; +}
This seems like an unnecessary novelty. The BUILDIO() macro in arch/x86/include/asm/io.h takes a type argument, why can't the size be explicitly specified rather than inferred from string parsing?
Quoted 62 lines
arch/x86/include/asm/tdx.h +31
-1+ +/* +* To support I/O port access in decompressor or early kernel init +* code, since #VE exception handler cannot be used, use paravirt +* model to implement __in/__out macros which will in turn be used +* by in{b,w,l}()/out{b,w,l} I/O helper macros used in kernel. You +* can find the __in/__out macro usage in arch/x86/include/asm/io.h +*/ +#ifdef BOOT_COMPRESSED_MISC_H +#define __out (bwl, bw) \ +do { \ + if (is_tdx_guest()) { \ + tdg_out(tdx_get_iosize(#bwl), port, value); \ + } else { \ + asm volatile("out" #bwl " %" #bw "0, %w1" : : \ + "a"(value), "Nd"(port)); \ + } \ +} while (0) +#define __in (bwl, bw) \ +do { \ + if (is_tdx_guest()) { \ + value = tdg_in(tdx_get_iosize(#bwl), port); \ + } else { \ + asm volatile("in" #bwl " %w1, %" #bw "0" : \ + "=a"(value) : "Nd"(port)); \ + } \ +} while (0) +#endif +#else // !CONFIG_INTEL_TDX_GUEST static inline bool is_tdx_guest (void )@@ -161,6 +205,7 @@ static inline long tdx_kvm_hypercall4(unsigned int nr, unsigned long p1, {return -ENODEV ; } -#endif /* CONFIG_INTEL_TDX_GUEST_KVM */ +#endif /* CONFIG_INTEL_TDX_GUEST_KVM */ +#endif /* __ASSEMBLY__ */ #endif /* _ASM_X86_TDX_H */ arch/x86/kernel/tdx.c +13
-0@@ -202,6 +202,42 @@ static void tdg_handle_cpuid(struct pt_regs *regs) regs->dx = out.r15; } +void tdg_out (int size,int port,unsigned int value) +{ +tdx_hypercall (EXIT_REASON_IO_INSTRUCTION , size,1 , port, value); +} + +unsigned int tdg_in (int size,int port) +{ +struct tdx_hypercall_output out = {0 }; +u64 err; + + err =__tdx_hypercall (EXIT_REASON_IO_INSTRUCTION , size,0 , + port,0 , &out); +
arch/x86/kernel/tdx.c static void tdg_handle_cpuid(struct pt_regs *regs) · line 227
Quoted 10 lines
arch/x86/kernel/tdx.c +10
-0+return err ?UINT_MAX : out.r11; +} + +static void tdg_handle_io (struct pt_regs *regs,u32 exit_qual) +{ +bool string = exit_qual &16 ; +int out, size, port; + +/* I/O strings ops are unrolled at build time. */ +BUG_ON (string);
...and here is why I think the WBINVD patch is bogus, or at least inconsistent with the decision taken here. If it's ok to BUG_ON instructions that "can't happen" due to care taken to ensure build time guarantees then it is ok to skip WBINVD handling with the same care taken to prevent its usage at build time.
Quoted 29 lines
arch/x86/kernel/tdx.c +16
-1+ + out =VE_GET_IO_TYPE (exit_qual); + size =VE_GET_IO_SIZE (exit_qual); + port =VE_GET_PORT_NUM (exit_qual); + +if (out) { +tdg_out (size, port, regs->ax); + }else { + regs->ax &= ~GENMASK (8 * size,0 ); + regs->ax |=tdg_in (size, port) &GENMASK (8 * size,0 ); + } +} +unsigned long tdg_get_ve_info (struct ve_info *ve) {u64 ret;@@ -248,6 +284,9 @@ int tdg_handle_virtualization_exception(struct pt_regs *regs,case EXIT_REASON_CPUID :tdg_handle_cpuid (regs);break ; +case EXIT_REASON_IO_INSTRUCTION : +tdg_handle_io (regs, ve->exit_qual); +break ;default :pr_warn ("Unexpected #VE: %lld\n" , ve->exit_reason);return -EFAULT ; --2.25.1
Re: [RFC v2-fix-v1 3/3] x86/tdx: Handle port I/O
Quoted 16 lines · Dan Williams
Quoted 14 lines · Kuppuswamy Sathyanarayanan
From: "Kirill A. Shutemov" <[email protected]>
TDX hypervisors cannot emulate instructions directly. This includes port IO which is normally emulated in the hypervisor. All port IO instructions inside TDX trigger the #VE exception in the guest and would be normally emulated there.
For the really early code in the decompressor, #VE cannot be used because the IDT needed for handling the exception is not set-up, and some other infrastructure needed by the handler is missing. So to support port IO in decompressor code, add support for paravirt based I/O port virtualization.
Quoted 8 lines
Quoted 4 lines
Also string I/O is not supported in TDX guest. So, unroll the string I/O operation into a loop operating on one element at a time. This method is similar to AMD SEV, so just extend the support for TDX guest platform.
Given early port IO is broken out in its own previous I think it makes sense to break out the decompressor port IO enabling from final runtime port IO support.
Patch titled "x86/tdx: Handle early IO operations" mainly adds IO #VE support in early exception handler. Decompression code IO support does not have dependency on it. You still think it is better to move it that patch?
Quoted 6 lines
The argument in the previous patch about using #VE emulation in the early code was collisions with trace and printk support in the "fully featured" #VE handler later in the series. My interpretation of that collision was due to the possibility of the #VE handler going into infinite recursion if a printk in the handler triggered port IO. It
No. AFAIK, It has nothing to do with infinite recursion. We are just highlighting the fact that when kernel uses early exception handler support, we cannot use code path that enables tracing support. So we use simplest way to trigger IO hypercalls.
if (early #VE exception path)
handle_io_ve()
__tdx_hypercallif (normal #VE path)
handle_io_ve()
__tdx_hypercall (current version)
// Later on when adding tracing support, we will replace it
// with trace hypercalls.
__trace_tdx_hypercallAs you can see in above design flow, later on when adding tracing support we will have split the early #IO handling code from normal IO handling code. So instead of using common code now and refactor it later on, we just use different code path for both of them.
Quoted 4 lines
seems I do not have the right picture of the constraints. Given the runtime kernel can direct replace in/out macros I would expect a statement of the tradeoff with #VE emulation and why the post decompressor code is still using emulation.
Currently decompression code cannot use #VE based IO emulation. It does not know how to handle #VE exceptions. Also, It is much easier to replace IO calls with TDX hypercalls in decompression code when compared with teaching how to handle #VE exceptions in decompression code.
Quoted 51 lines
Quoted 51 lines
Co-developed-by Kuppuswamy Sathyanarayanan <[email protected]>Signed-off-by Kuppuswamy Sathyanarayanan <[email protected]>Signed-off-by Kirill A. Shutemov <[email protected]>Reviewed-by Andi Kleen <[email protected]>--- arch/x86/boot/compressed/Makefile | 1 + arch/x86/boot/compressed/tdcall.S | 3 ++ arch/x86/boot/compressed/tdx.c | 28 ++++++++++++++++++ arch/x86/include/asm/io.h | 7 +++-- arch/x86/include/asm/tdx.h | 47 ++++++++++++++++++++++++++++++- arch/x86/kernel/tdx.c | 39 +++++++++++++++++++++++++ 6 files changed, 122 insertions(+), 3 deletions(-)create mode 100644
arch/x86/boot/compressed/tdcall.Sarch/x86/boot/compressed/Makefile +22
-0@@ -97,6 +97,7 @@ endif static int __ro_after_init tdx_guest = -1;@@ -30,3 +32,29 @@ bool is_tdx_guest(void) return !!tdx_guest; } +/* + * Helper function used for making hypercall for "out" + * instruction. It will be called from __out IO + * macro (in tdx.h). + */ +void tdg_out(int size, int port, unsigned int value) +{ + __tdx_hypercall(EXIT_REASON_IO_INSTRUCTION, size, 1, + port, value, NULL); +} + +/* + * Helper function used for making hypercall for "in" + * instruction. It will be called from __in IO macro + * (in tdx.h). If IO is failed, it will return all 1s. + */ +unsigned int tdg_in(int size, int port) +{ + struct tdx_hypercall_output out = {0}; + int err; + + err = __tdx_hypercall(EXIT_REASON_IO_INSTRUCTION, size, 0,
Quoted 8 lines
arch/x86/boot/compressed/Makefile bool is_tdx_guest(void) · line 60
Quoted 4 lines
arch/x86/boot/compressed/Makefile +4
-0+ port, 0, &out); + + return err ? UINT_MAX : out.r11; +}The previous patch open coded tdg_{in,out} and this one provides helpers. I think at a minimum they should be consistent and pick one style.
As I have mentioned above, early IO #VE handler is a special case. we don't want to complicate its code path with debug or tracing support. So it is not a good comparison target.
In this case, the reason for adding helper function is to make it easier for calling it from tdx.h.
Quoted 7 lines
Quoted 7 lines
arch/x86/include/asm/io.h +0
-0@@ -40,6 +40,7 @@
snip
Quoted 7 lines
Quoted 7 lines
arch/x86/include/asm/io.h +7
-0+ +/* Helper function for converting {b,w,l} to byte size */ +static inline int tdx_get_iosize (char *str) +{ +if (str[0 ] =='w' ) +return 2 ; +else if (str[0 ] =='l' )
Quoted 8 lines
arch/x86/include/asm/io.h
Quoted 4 lines
arch/x86/include/asm/io.h +4
-0+return 4 ; + +return 1 ; +}This seems like an unnecessary novelty. The
BUILDIO()macro inarch/x86/include/asm/io.htakes a type argument, why can't the size be explicitly specified rather than inferred from string parsing?
I don't want to make changes to generic macros in io.h if it can be avoided. It follows similar argument/type in all arch/* code. Also, it is easier to handle TDX as a special case here.
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Re: [RFC v2-fix-v1 3/3] x86/tdx: Handle port I/O
Quoted 25 lines · Sathyanarayanan
Quoted 21 lines · Dan Williams
Quoted 18 lines · Kuppuswamy Sathyanarayanan
From: "Kirill A. Shutemov" <[email protected]>
TDX hypervisors cannot emulate instructions directly. This includes port IO which is normally emulated in the hypervisor. All port IO instructions inside TDX trigger the #VE exception in the guest and would be normally emulated there.
For the really early code in the decompressor, #VE cannot be used because the IDT needed for handling the exception is not set-up, and some other infrastructure needed by the handler is missing. So to support port IO in decompressor code, add support for paravirt based I/O port virtualization.
Also string I/O is not supported in TDX guest. So, unroll the string I/O operation into a loop operating on one element at a time. This method is similar to AMD SEV, so just extend the support for TDX guest platform.
Quoted 9 lines
Quoted 3 lines
Given early port IO is broken out in its own previous I think it makes sense to break out the decompressor port IO enabling from final runtime port IO support.
Patch titled "x86/tdx: Handle early IO operations" mainly adds IO #VE support in early exception handler. Decompression code IO support does not have dependency on it. You still think it is better to move it that patch?
No, I was suggesting three patches instead of 2:
early
decompressor
final-runtime
Quoted 11 lines
Quoted 6 lines
The argument in the previous patch about using #VE emulation in the early code was collisions with trace and printk support in the "fully featured" #VE handler later in the series. My interpretation of that collision was due to the possibility of the #VE handler going into infinite recursion if a printk in the handler triggered port IO. It
No. AFAIK, It has nothing to do with infinite recursion. We are just highlighting the fact that when kernel uses early exception handler support, we cannot use code path that enables tracing support. So we use simplest way to trigger IO hypercalls.
Ok, then how does this approach handle printk from the #VE handler if printk issues port IO?
Quoted 12 lines
if (early #VE exception path)
handle_io_ve() __tdx_hypercallif (normal #VE path)
handle_io_ve() __tdx_hypercall (current version) // Later on when adding tracing support, we will replace it // with trace hypercalls. __trace_tdx_hypercall
Quoted 5 lines
As you can see in above design flow, later on when adding tracing support we will have split the early #IO handling code from normal IO handling code. So instead of using common code now and refactor it later on, we just use different code path for both of them.
Could you put that in the changelog, it was non-obvious to me.
Quoted 9 lines
Quoted 4 lines
seems I do not have the right picture of the constraints. Given the runtime kernel can direct replace in/out macros I would expect a statement of the tradeoff with #VE emulation and why the post decompressor code is still using emulation.
Currently decompression code cannot use #VE based IO emulation. It does not know how to handle #VE exceptions. Also, It is much easier to replace IO calls with TDX hypercalls in decompression code when compared with teaching how to handle #VE exceptions in decompression code.
Ok, but that does not answer the background behind the decision to use emulation rather than direct replacement of port IO instructions in the final kernel runtime image.
This patch mixes those 2 concerns and I think it deserves to be broken out and explained.
Quoted 61 lines
Quoted 52 lines
Quoted 52 lines
Co-developed-by Kuppuswamy Sathyanarayanan <[email protected]>Signed-off-by Kuppuswamy Sathyanarayanan <[email protected]>Signed-off-by Kirill A. Shutemov <[email protected]>Reviewed-by Andi Kleen <[email protected]>--- arch/x86/boot/compressed/Makefile | 1 + arch/x86/boot/compressed/tdcall.S | 3 ++ arch/x86/boot/compressed/tdx.c | 28 ++++++++++++++++++ arch/x86/include/asm/io.h | 7 +++-- arch/x86/include/asm/tdx.h | 47 ++++++++++++++++++++++++++++++- arch/x86/kernel/tdx.c | 39 +++++++++++++++++++++++++ 6 files changed, 122 insertions(+), 3 deletions(-)create mode 100644
arch/x86/boot/compressed/tdcall.Sarch/x86/boot/compressed/Makefile +22
-0@@ -97,6 +97,7 @@ endif static int __ro_after_init tdx_guest = -1;@@ -30,3 +32,29 @@ bool is_tdx_guest(void) return !!tdx_guest; } +/* + * Helper function used for making hypercall for "out" + * instruction. It will be called from __out IO + * macro (in tdx.h). + */ +void tdg_out(int size, int port, unsigned int value) +{ + __tdx_hypercall(EXIT_REASON_IO_INSTRUCTION, size, 1, + port, value, NULL); +} + +/* + * Helper function used for making hypercall for "in" + * instruction. It will be called from __in IO macro + * (in tdx.h). If IO is failed, it will return all 1s. + */ +unsigned int tdg_in(int size, int port) +{ + struct tdx_hypercall_output out = {0}; + int err; + + err = __tdx_hypercall(EXIT_REASON_IO_INSTRUCTION, size, 0,Quoted 8 lines
arch/x86/boot/compressed/Makefile bool is_tdx_guest(void) · line 60
Quoted 4 lines
arch/x86/boot/compressed/Makefile +4
-0+ port, 0, &out); + + return err ? UINT_MAX : out.r11; +}The previous patch open coded tdg_{in,out} and this one provides helpers. I think at a minimum they should be consistent and pick one style.
Quoted 3 lines
As I have mentioned above, early IO #VE handler is a special case. we don't want to complicate its code path with debug or tracing support. So it is not a good comparison target.
This patch and the last do the same thing in 2 different ways. One of them should match the other even if the helpers are not directly reused.
Quoted 28 lines
In this case, the reason for adding helper function is to make it easier for calling it from
tdx.h.Quoted 7 lines
Quoted 7 lines
arch/x86/include/asm/io.h +0
-0@@ -40,6 +40,7 @@snip
Quoted 7 lines
Quoted 7 lines
arch/x86/include/asm/io.h +7
-0+ +/* Helper function for converting {b,w,l} to byte size */ +static inline int tdx_get_iosize (char *str) +{ +if (str[0 ] =='w' ) +return 2 ; +else if (str[0 ] =='l' )Quoted 8 lines
arch/x86/include/asm/io.h
Quoted 4 lines
arch/x86/include/asm/io.h +4
-0+return 4 ; + +return 1 ; +}This seems like an unnecessary novelty. The
BUILDIO()macro inarch/x86/include/asm/io.htakes a type argument, why can't the size be explicitly specified rather than inferred from string parsing?
Quoted 4 lines
I don't want to make changes to generic macros in
io.hif it can be avoided. It follows similar argument/type in allarch/*code. Also, it is easier to handle TDX as a special case here.
What changes are you talking about to the generic macros? The BUILDIO macro passes in a size parameter explicitly rather than inferring the size from the string name of an argument. BUILDIO does not need to change, it's backend just needs to do the right thing in the TDX case.
Otherwise, "I don't want to" is not a sufficient justification for avoiding needlessly new design patterns.
Re: [RFC v2-fix-v1 3/3] x86/tdx: Handle port I/O
Quoted 31 lines · Dan Williams
Quoted 29 lines · Sathyanarayanan
Quoted 24 lines · Dan Williams
Quoted 14 lines · Kuppuswamy Sathyanarayanan
From: "Kirill A. Shutemov" <[email protected]>
TDX hypervisors cannot emulate instructions directly. This includes port IO which is normally emulated in the hypervisor. All port IO instructions inside TDX trigger the #VE exception in the guest and would be normally emulated there.
For the really early code in the decompressor, #VE cannot be used because the IDT needed for handling the exception is not set-up, and some other infrastructure needed by the handler is missing. So to support port IO in decompressor code, add support for paravirt based I/O port virtualization.
Quoted 4 lines
Also string I/O is not supported in TDX guest. So, unroll the string I/O operation into a loop operating on one element at a time. This method is similar to AMD SEV, so just extend the support for TDX guest platform.
Given early port IO is broken out in its own previous I think it makes sense to break out the decompressor port IO enabling from final runtime port IO support.
Quoted 7 lines
Quoted 5 lines
Patch titled "x86/tdx: Handle early IO operations" mainly adds IO #VE support in early exception handler. Decompression code IO support does not have dependency on it. You still think it is better to move it that patch?
No, I was suggesting three patches instead of 2:
Ok. I will move it to separate patch.
Quoted 1 line
snip
Quoted 9 lines
Quoted 5 lines
Currently decompression code cannot use #VE based IO emulation. It does not know how to handle #VE exceptions. Also, It is much easier to replace IO calls with TDX hypercalls in decompression code when compared with teaching how to handle #VE exceptions in decompression code.
Ok, but that does not answer the background behind the decision to use emulation rather than direct replacement of port IO instructions in the final kernel runtime image.
The reason for using #VE based emulation is,
1. It does not require changes to all usages of emulated instructions in all the drivers.
2. Directly replacing instructions with TDX hypercalls will lead to bloated image. So we cannot universally adapt this approach.
Reason for not adapting to use #VE approach for decompression code is,
1. #VE handler support does not exist for de-compressor code.
2. Adding such support is more complex and in-efficient (just for single use case of handling IO instructions).
So we have replaced IO instructions with TDX hypercalls in decompression code.
Did it answer your query?
Quoted 38 lines
This patch mixes those 2 concerns and I think it deserves to be broken out and explained.
Quoted 31 lines
Quoted 30 lines
Quoted 16 lines
(quoted diff) +16
-0+/* + * Helper function used for making hypercall for "out" + * instruction. It will be called from __out IO + * macro (in tdx.h). + */ +void tdg_out(int size, int port, unsigned int value) +{ + __tdx_hypercall(EXIT_REASON_IO_INSTRUCTION, size, 1, + port, value, NULL); +} + +/* + * Helper function used for making hypercall for "in" + * instruction. It will be called from __in IO macro + * (in tdx.h). If IO is failed, it will return all 1s. + */Review
Quoted 10 lines
(quoted diff) +10
-0+unsigned int tdg_in(int size, int port) +{ + struct tdx_hypercall_output out = {0}; + int err; + + err = __tdx_hypercall(EXIT_REASON_IO_INSTRUCTION, size, 0, + port, 0, &out); + + return err ? UINT_MAX : out.r11; +}The previous patch open coded tdg_{in,out} and this one provides helpers. I think at a minimum they should be consistent and pick one style.
Quoted 7 lines
Quoted 3 lines
As I have mentioned above, early IO #VE handler is a special case. we don't want to complicate its code path with debug or tracing support. So it is not a good comparison target.
This patch and the last do the same thing in 2 different ways. One of them should match the other even if the helpers are not directly reused.
I am not sure whether I understand your question. But if the question is about different implementation, the difference is due to where it is being called.
In early IO support patch, tdx_early_io() is called from #VE handler. So there is extra buffer code to extract exception information from struct ve_info.
In this case, the caller is __in/__out macros. So there is no need for above mentioned buffer code.
Actual hypercall usage is similar in both cases.
Quoted 34 lines
Quoted 29 lines
In this case, the reason for adding helper function is to make it easier for calling it from
tdx.h.Quoted 7 lines
Quoted 7 lines
arch/x86/include/asm/io.h +0
-0@@ -40,6 +40,7 @@snip
Quoted 7 lines
Quoted 7 lines
arch/x86/include/asm/io.h +7
-0+ +/* Helper function for converting {b,w,l} to byte size */ +static inline int tdx_get_iosize (char *str) +{ +if (str[0 ] =='w' ) +return 2 ; +else if (str[0 ] =='l' )Quoted 8 lines
arch/x86/include/asm/io.h
Quoted 4 lines
arch/x86/include/asm/io.h +4
-0+return 4 ; + +return 1 ; +}This seems like an unnecessary novelty. The
BUILDIO()macro inarch/x86/include/asm/io.htakes a type argument, why can't the size be explicitly specified rather than inferred from string parsing?Quoted 4 lines
I don't want to make changes to generic macros in
io.hif it can be avoided. It follows similar argument/type in allarch/*code. Also, it is easier to handle TDX as a special case here.
Quoted 7 lines
What changes are you talking about to the generic macros? The BUILDIO macro passes in a size parameter explicitly rather than inferring the size from the string name of an argument. BUILDIO does not need to change, it's backend just needs to do the right thing in the TDX case.
Otherwise, "I don't want to" is not a sufficient justification for avoiding needlessly new design patterns.
I hope this is what you meant?
arch/x86/include/asm/io.h +4 -4
@@ -273,25 +273,25 @@ static inline bool sev_key_active(void) { return false; }#endif /* CONFIG_AMD_MEM_ENCRYPT */ #ifndef __out -#define __out (bwl, bw) \ +#define __out (bwl, bw, sz) \ asm volatile("out" #bwl " %" #bw "0, %w1" : : "a"(value), "Nd"(port))#endif #ifndef __in -#define __in (bwl, bw) \ +#define __in (bwl, bw, sz) \ asm volatile("in" #bwl " %w1, %" #bw "0" : "=a"(value) : "Nd"(port))#endif #define BUILDIO (bwl, bw, type) \ static inline void out##bwl(unsigned type value, int port) \ { \ - __out(bwl, bw); \ + __out(bwl, bw, sizeof(type)); \ } \ \ static inline unsigned type in##bwl(int port) \ { \ unsigned type value; \ - __in(bwl, bw); \ + __in(bwl, bw, sizeof(type)); \ return value; \ }
Quoted 1 line
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Re: [RFC v2-fix-v1 3/3] x86/tdx: Handle port I/O
Quoted 68 lines · Sathyanarayanan
Quoted 31 lines · Dan Williams
Quoted 29 lines · Sathyanarayanan
Quoted 24 lines · Dan Williams
Quoted 14 lines · Kuppuswamy Sathyanarayanan
From: "Kirill A. Shutemov" <[email protected]>
TDX hypervisors cannot emulate instructions directly. This includes port IO which is normally emulated in the hypervisor. All port IO instructions inside TDX trigger the #VE exception in the guest and would be normally emulated there.
For the really early code in the decompressor, #VE cannot be used because the IDT needed for handling the exception is not set-up, and some other infrastructure needed by the handler is missing. So to support port IO in decompressor code, add support for paravirt based I/O port virtualization.
Quoted 4 lines
Also string I/O is not supported in TDX guest. So, unroll the string I/O operation into a loop operating on one element at a time. This method is similar to AMD SEV, so just extend the support for TDX guest platform.
Given early port IO is broken out in its own previous I think it makes sense to break out the decompressor port IO enabling from final runtime port IO support.
Quoted 7 lines
Quoted 5 lines
Patch titled "x86/tdx: Handle early IO operations" mainly adds IO #VE support in early exception handler. Decompression code IO support does not have dependency on it. You still think it is better to move it that patch?
No, I was suggesting three patches instead of 2:
Ok. I will move it to separate patch.
Quoted 1 line
snip
Quoted 9 lines
Quoted 5 lines
Currently decompression code cannot use #VE based IO emulation. It does not know how to handle #VE exceptions. Also, It is much easier to replace IO calls with TDX hypercalls in decompression code when compared with teaching how to handle #VE exceptions in decompression code.
Ok, but that does not answer the background behind the decision to use emulation rather than direct replacement of port IO instructions in the final kernel runtime image.
The reason for using #VE based emulation is,
1. It does not require changes to all usages of emulated instructions in all the drivers.
2. Directly replacing instructions with TDX hypercalls will lead to bloated image. So we cannot universally adapt this approach.Reason for not adapting to use #VE approach for decompression code is,
Quoted 7 lines
1. #VE handler support does not exist for de-compressor code.
2. Adding such support is more complex and in-efficient (just for single use case of handling IO instructions).So we have replaced IO instructions with TDX hypercalls in decompression code.
Did it answer your query?
Yes, all but the concern of printk recursion.
Quoted 51 lines
Quoted 39 lines
This patch mixes those 2 concerns and I think it deserves to be broken out and explained.
Quoted 31 lines
Quoted 30 lines
Quoted 16 lines
(quoted diff) +16
-0+/* + * Helper function used for making hypercall for "out" + * instruction. It will be called from __out IO + * macro (in tdx.h). + */ +void tdg_out(int size, int port, unsigned int value) +{ + __tdx_hypercall(EXIT_REASON_IO_INSTRUCTION, size, 1, + port, value, NULL); +} + +/* + * Helper function used for making hypercall for "in" + * instruction. It will be called from __in IO macro + * (in tdx.h). If IO is failed, it will return all 1s. + */Review
Quoted 10 lines
(quoted diff) +10
-0+unsigned int tdg_in(int size, int port) +{ + struct tdx_hypercall_output out = {0}; + int err; + + err = __tdx_hypercall(EXIT_REASON_IO_INSTRUCTION, size, 0, + port, 0, &out); + + return err ? UINT_MAX : out.r11; +}The previous patch open coded tdg_{in,out} and this one provides helpers. I think at a minimum they should be consistent and pick one style.
Quoted 7 lines
Quoted 3 lines
As I have mentioned above, early IO #VE handler is a special case. we don't want to complicate its code path with debug or tracing support. So it is not a good comparison target.
This patch and the last do the same thing in 2 different ways. One of them should match the other even if the helpers are not directly reused.
I am not sure whether I understand your question. But if the question is about different implementation, the difference is due to where it is being called.
Quoted 8 lines
In early IO support patch,
tdx_early_io()is called from #VE handler. So there is extra buffer code to extract exception information from structve_info.In this case, the caller is __in/__out macros. So there is no need for above mentioned buffer code.
Actual hypercall usage is similar in both cases.
It's similar when it can be identical. It simply looks like 2 authors wrote 2 different code paths which is the case. Just huddle and code it one way, I don't much care which.
Quoted 66 lines
Quoted 35 lines
Quoted 30 lines
In this case, the reason for adding helper function is to make it easier for calling it from
tdx.h.Quoted 7 lines
Quoted 7 lines
arch/x86/include/asm/io.h +0
-0@@ -40,6 +40,7 @@snip
Quoted 7 lines
Quoted 7 lines
arch/x86/include/asm/io.h +7
-0+ +/* Helper function for converting {b,w,l} to byte size */ +static inline int tdx_get_iosize (char *str) +{ +if (str[0 ] =='w' ) +return 2 ; +else if (str[0 ] =='l' )Quoted 8 lines
arch/x86/include/asm/io.h
Quoted 4 lines
arch/x86/include/asm/io.h +4
-0+return 4 ; + +return 1 ; +}This seems like an unnecessary novelty. The
BUILDIO()macro inarch/x86/include/asm/io.htakes a type argument, why can't the size be explicitly specified rather than inferred from string parsing?Quoted 4 lines
I don't want to make changes to generic macros in
io.hif it can be avoided. It follows similar argument/type in allarch/*code. Also, it is easier to handle TDX as a special case here.Quoted 7 lines
What changes are you talking about to the generic macros? The BUILDIO macro passes in a size parameter explicitly rather than inferring the size from the string name of an argument. BUILDIO does not need to change, it's backend just needs to do the right thing in the TDX case.
Otherwise, "I don't want to" is not a sufficient justification for avoiding needlessly new design patterns.
I hope this is what you meant?
arch/x86/include/asm/io.h +2
-3@@ -273,25 +273,25 @@ static inline bool sev_key_active(void) { return false; }#endif /* CONFIG_AMD_MEM_ENCRYPT */ #ifndef __out -#define __out (bwl, bw) \ +#define __out (bwl, bw, sz) \ asm volatile("out" #bwl " %" #bw "0, %w1" : : "a"(value), "Nd"(port))#endif #ifndef __in -#define __in (bwl, bw) \ +#define __in (bwl, bw, sz) \ asm volatile("in" #bwl " %w1, %" #bw "0" : "=a"(value) : "Nd"(port))#endif #define BUILDIO (bwl, bw, type) \ static inline void out##bwl(unsigned type value, int port) \ { \ - __out(bwl, bw); \
arch/x86/include/asm/io.h static inline bool sev_key_active(void) { return false; } · line 296
Quoted 10 lines
arch/x86/include/asm/io.h +2
-1+__out (bwl, bw,sizeof (type)); \ } \ \ static inline unsigned type in##bwl(int port) \ { \ unsigned type value; \ - __in(bwl, bw); \ + __in(bwl, bw, sizeof(type)); \ return value; \ }
Looks good.
Re: [RFC v2-fix-v1 3/3] x86/tdx: Handle port I/O
Quoted 2 lines · Dan Williams
Quoted 1 line
Did it answer your query?
Yes, all but the concern of printk recursion.
I think recursion is not possible because printk will handle it (using console_lock). If another print is triggered during the current printk handling, it will be directed to logbuf and delayed.
https://elixir.bootlin.com/linux/latest/source/kernel/printk/printk_safe.c#L382
Quoted 1 line
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Re: [RFC v2-fix-v1 3/3] x86/tdx: Handle port I/O
Quoted 5 lines · Sathyanarayanan
Quoted 1 line · Dan Williams
Quoted 1 line
Did it answer your query?
Quoted 8 lines
Quoted 1 line
Yes, all but the concern of printk recursion.
I think recursion is not possible because printk will handle it (using
console_lock). If another print is triggered during the current printk handling, it will be directed to logbuf and delayed.https://elixir.bootlin.com/linux/latest/source/kernel/printk/printk_safe.c#L382
That depends on printk_nmi_direct_enter() to set the context, wouldn't an equivalent printk_ve_direct_enter() context flag be needed as well?
Re: [RFC v2-fix-v1 3/3] x86/tdx: Handle port I/O
ps://elixir.bootlin.com/linux/latest/source/kernel/printk/printk_safe.c#L382
Quoted 2 lines
That depends on
printk_nmi_direct_enter()to set the context, wouldn't an equivalentprintk_ve_direct_enter()context flag be needed as well?
Even without it the console semaphore is always trylocked. So recursion is just not possible.
What would be possible is a endless loop (printk adding more information to the log buffer, which is then printed etc.), but that's true for everywhere in the console/serial driver subsystem.
-Andi
cover x86/tdx: Handle port I/O
In reply to Dan WilliamsThis patchset addresses the review comments in the patch titled "[RFC v2 14/32] x86/tdx: Handle port I/O". Since it requires patch split, sending these together.
Changes since RFC v2-fix-v1:
* Splitted TDX decompression IO support into a seperate patch.
* Implemented tdg_handle_io() and tdx_early_io() in the similar way as per review suggestion.
* Added VE_IS_IO_OUT() macro as per review suggestion.
* Added VE_IS_IO_STRING() to check the string I/O case in tdx_early_io()
* Removed helper function tdg_in() and tdg_out() and directly called IO hypercall to make the implementation uniform in decompression code, early IO code and normal IO handler code.
Changes since RFC v2:
* Removed assembly implementation of port IO emulation code and modified __in/__out IO helpers to directly call C function for in/out instruction emulation in decompression code.
* Added helper function tdx_get_iosize() to make it easier for calling tdg_out/tdg_int() C functions from decompression code.
* Added support for early exception handler to support IO instruction emulation in early boot kernel code.
* Removed alternative_ usage and made kernel only use #VE based IO instruction emulation support outside the decompression module.
* Added support for protection_guest_has() API to generalize AMD SEV/TDX specific initialization code in common drivers.
* Fixed commit log and comments as per review comments.
Andi Kleen (1):
x86/tdx: Handle early IO operations
Kirill A. Shutemov (1):
x86/tdx: Handle port I/O
Kuppuswamy Sathyanarayanan (1):
x86/tdx: Handle port I/O in decompression code
arch/x86/boot/compressed/Makefile | 1 + arch/x86/boot/compressed/tdcall.S | 3 ++ arch/x86/include/asm/io.h | 15 +++--- arch/x86/include/asm/tdx.h | 54 ++++++++++++++++++++ arch/x86/kernel/head64.c | 3 ++ arch/x86/kernel/tdx.c | 84 +++++++++++++++++++++++++++++++ 6 files changed, 154 insertions(+), 6 deletions(-) create mode 100644 arch/x86/boot/compressed/tdcall.S
Signature · 1 line
2.25.1
RFC 1/3 x86/tdx: Handle port I/O in decompression code
Add support to replace in/out instructions in
decompression code with TDX IO hypercalls.
TDX cannot do port IO directly. The TDX module triggers a #VE exception to let the guest kernel to emulate port I/O, by converting them into TDX hypercalls to call the host.
But for the really early code in the decompressor, #VE cannot be used because the IDT needed for handling the exception is not set-up, and some other infrastructure needed by the handler is missing. So to support port IO in decompressor code, directly replace in/out instructions with TDX IO hypercalls. This can beeasily achieved by modifying __in/__out macros.
Also, since TDX IO hypercall requires an IO size parameter, modify __in/__out macros to accept size as input parameter.
--- arch/x86/boot/compressed/Makefile | 1 + arch/x86/boot/compressed/tdcall.S | 3 ++ arch/x86/include/asm/io.h | 9 +++--- arch/x86/include/asm/tdx.h | 48 +++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 arch/x86/boot/compressed/tdcall.S
arch/x86/boot/compressed/Makefile +1 -0
@@ -99,6 +99,7 @@ endif vmlinux-objs-$(CONFIG_ACPI) += $(obj)/acpi.o vmlinux-objs-$(CONFIG_INTEL_TDX_GUEST) += $(obj)/tdx.o +vmlinux-objs-$(CONFIG_INTEL_TDX_GUEST) += $(obj)/tdcall.o vmlinux-objs-$(CONFIG_EFI_MIXED) += $(obj)/efi_thunk_$(BITS).o efi-obj-$(CONFIG_EFI_STUB) = $(objtree)/drivers/firmware/efi/libstub/lib.a
arch/x86/boot/compressed/tdcall.S new file +3 -0
@@ -0,0 +1,3 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#include "../../kernel/tdcall.S"
arch/x86/include/asm/io.h +5 -4
@@ -40,6 +40,7 @@#include <linux/string.h> #include <linux/compiler.h> +#include <linux/protected_guest.h> #include <asm/page.h> #include <asm/early_ioremap.h> #include <asm/pgtable_types.h>
@@ -272,25 +273,25 @@ static inline bool sev_key_active(void) { return false; }#endif /* CONFIG_AMD_MEM_ENCRYPT */ #ifndef __out -#define __out (bwl, bw) \ +#define __out (bwl, bw, sz) \ asm volatile("out" #bwl " %" #bw "0, %w1" : : "a"(value), "Nd"(port))#endif #ifndef __in -#define __in (bwl, bw) \ +#define __in (bwl, bw, sz) \ asm volatile("in" #bwl " %w1, %" #bw "0" : "=a"(value) : "Nd"(port))#endif #define BUILDIO (bwl, bw, type) \ static inline void out##bwl(unsigned type value, int port) \ { \ - __out(bwl, bw); \ + __out(bwl, bw, sizeof(type)); \ } \ \ static inline unsigned type in##bwl(int port) \ { \ unsigned type value; \ - __in(bwl, bw); \ + __in(bwl, bw, sizeof(type)); \ return value; \ } \ \
arch/x86/include/asm/tdx.h +48 -0
60 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [RFC v2-fix-v2 1/3] x86/tdx: Handle port I/O in decompression code
Quoted 17 lines · Kuppuswamy Sathyanarayanan
Add support to replace in/out instructions in
decompression code with TDX IO hypercalls.TDX cannot do port IO directly. The TDX module triggers a #VE exception to let the guest kernel to emulate port I/O, by converting them into TDX hypercalls to call the host.
But for the really early code in the decompressor, #VE cannot be used because the IDT needed for handling the exception is not set-up, and some other infrastructure needed by the handler is missing. So to support port IO in decompressor code, directly replace in/out instructions with TDX IO hypercalls. This can beeasily achieved by modifying __in/__out macros.
Quoted 2 lines
Also, since TDX IO hypercall requires an IO size parameter, modify __in/__out macros to accept size as input parameter.
Looks good to me:
RFC 2/3 x86/tdx: Handle early IO operations
In reply to Kuppuswamy SathyanarayananFrom: Andi Kleen <[email protected]>
Add an early #VE handler to convert early port IOs into TDCALLs.
TDX cannot do port IO directly. The TDX module triggers a #VE exception to let the guest kernel to emulate port I/O, by converting them into TDCALLs to call the host.
To support port IO in early boot code, add a minimal support in early exception handlers. This is similar to what AMD SEV does. This is mainly to support early_printk's serial driver, as well as potentially the VGA driver (although it is expected not to be used).
The early handler only does IO calls and nothing else, and anything that goes wrong results in a normal early exception panic.
It cannot share the code paths with the normal #VE handler because it needs to avoid using trace calls or printk.
This early handler allows us to use the normal in*/out* macros without patching them for every driver. Since, there is no expectation that early port IO is performance critical, the #VE emulation cost is worth the simplicity benefit of not patching out port IO usage in early code. There are also no concerns with nesting, since there should be no NMIs this early.
---
Changes since RFC v2-fix-v1:
* Fixed commit log as per review comments.
* Removed IS_ENABLED(CONFIG_INTEL_TDX_GUEST) check for tdg_early_handle_ve()
* Changed VE_GET_IO_TYPE to VE_IS_IO_OUT() and modified tdx_early_io() as per review suggestion.
* Added support to check string I/O case in tdx_early_io().
* Modified tdx_early_io() to pass exit_qual instead of ve_info.
arch/x86/include/asm/tdx.h | 6 ++++ arch/x86/kernel/head64.c | 3 ++ arch/x86/kernel/tdx.c | 56 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+)
arch/x86/include/asm/tdx.h +6 -0
@@ -74,6 +74,7 @@ u64 __tdx_hypercall(u64 fn, u64 r12, u64 r13, u64 r14, u64 r15, struct tdx_hypercall_output *out);bool tdx_protected_guest_has (unsigned long flag); +bool tdg_early_handle_ve (struct pt_regs *regs);/* * To support I/O port access in decompressor or early kernel init
@@ -135,6 +136,11 @@ static inline bool tdx_protected_guest_has(unsigned long flag) return false; } +static inline bool tdg_early_handle_ve (struct pt_regs *regs) +{ +return false; +} +#endif /* CONFIG_INTEL_TDX_GUEST */ #ifdef CONFIG_INTEL_TDX_GUEST_KVM
arch/x86/kernel/head64.c +3 -0
@@ -410,6 +410,9 @@ void __init do_early_exception(struct pt_regs *regs, int trapnr) trapnr == X86_TRAP_VC && handle_vc_boot_ghcb(regs)) return; +if (trapnr ==X86_TRAP_VE &&tdg_early_handle_ve (regs)) +return ; +early_fixup_exception (regs, trapnr); }
arch/x86/kernel/tdx.c +56 -0
68 lines not on this page · raw message
Signature · 1 line
2.25.1
RFC 3/3 x86/tdx: Handle port I/O
In reply to Kuppuswamy SathyanarayananFrom: "Kirill A. Shutemov" <[email protected]>
TDX hypervisors cannot emulate instructions directly. This includes port IO which is normally emulated in the hypervisor. All port IO instructions inside TDX trigger the #VE exception in the guest and would be normally emulated there.
Also string I/O is not supported in TDX guest. So, unroll the string I/O operation into a loop operating on one element at a time. This method is similar to AMD SEV, so just extend the support for TDX guest platform.
---
Changes since RFC v2-fix-v1:
* Fixed commit log to adapt to decompression support code split.
arch/x86/include/asm/io.h | 6 ++++-- arch/x86/kernel/tdx.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-)
arch/x86/include/asm/io.h +4 -2
@@ -310,7 +310,8 @@ static inline unsigned type in##bwl##_p(int port) \ \ static inline void outs##bwl(int port, const void *addr, unsigned long count) \ { \ - if (sev_key_active()) { \ + if (sev_key_active() || \ + protected_guest_has(VM_UNROLL_STRING_IO)) { \ unsigned type *value = (unsigned type *)addr; \ while (count) { \ out##bwl(*value, port); \
@@ -326,7 +327,8 @@ static inline void outs##bwl(int port, const void *addr, unsigned long count) \ \ static inline void ins##bwl(int port, void *addr, unsigned long count) \ { \ - if (sev_key_active()) { \ + if (sev_key_active() || \ + protected_guest_has(VM_UNROLL_STRING_IO)) { \ unsigned type *value = (unsigned type *)addr; \ while (count) { \ *value = in##bwl(port); \
arch/x86/kernel/tdx.c +28 -0
@@ -201,6 +201,31 @@ static void tdg_handle_cpuid(struct pt_regs *regs) regs->dx = out.r15; } +/* +* Since the way we fail for string case is different we cannot +* reuse tdx_handle_early_io(). +*/ +static void tdg_handle_io (struct pt_regs *regs,u32 exit_qual) +{ +struct tdx_hypercall_output outh; +int out =VE_IS_IO_OUT (exit_qual); +int size =VE_GET_IO_SIZE (exit_qual); +int port =VE_GET_PORT_NUM (exit_qual); +u64 mask =GENMASK (8 * size,0 ); +bool string =VE_IS_IO_STRING (exit_qual); +int ret; + +/* I/O strings ops are unrolled at build time. */ +BUG_ON (string); + + ret =__tdx_hypercall (EXIT_REASON_IO_INSTRUCTION , size, out, port, + regs->ax, &outh); +if (!out) { + regs->ax &= ~mask; + regs->ax |= (ret ?UINT_MAX : outh.r11) & mask; + } +} +unsigned long tdg_get_ve_info (struct ve_info *ve) {u64 ret;
@@ -247,6 +272,9 @@ int tdg_handle_virtualization_exception(struct pt_regs *regs,case EXIT_REASON_CPUID :tdg_handle_cpuid (regs);break ; +case EXIT_REASON_IO_INSTRUCTION : +tdg_handle_io (regs, ve->exit_qual); +break ;default :pr_warn ("Unexpected #VE: %lld\n" , ve->exit_reason);return -EFAULT ;
Signature · 1 line
2.25.1
Re: [RFC v2-fix-v2 3/3] x86/tdx: Handle port I/O
Quoted 13 lines · Kuppuswamy Sathyanarayanan
From: "Kirill A. Shutemov" <[email protected]>
TDX hypervisors cannot emulate instructions directly. This includes port IO which is normally emulated in the hypervisor. All port IO instructions inside TDX trigger the #VE exception in the guest and would be normally emulated there.
Also string I/O is not supported in TDX guest. So, unroll the string I/O operation into a loop operating on one element at a time. This method is similar to AMD SEV, so just extend the support for TDX guest platform.
Quoted 7 lines
Co-developed-by Kuppuswamy Sathyanarayanan <[email protected]>Signed-off-by Kuppuswamy Sathyanarayanan <[email protected]>Signed-off-by Kirill A. Shutemov <[email protected]>Reviewed-by Andi Kleen <[email protected]>---
Changes since RFC v2-fix-v1:
* Fixed commit log to adapt to decompression support code split.
Looks good to me:
RFC v2 15/32 x86/tdx: Handle in-kernel MMIO
In reply to Kuppuswamy SathyanarayananFrom: "Kirill A. Shutemov" <[email protected]>
Handle #VE due to MMIO operations. MMIO triggers #VE with EPT_VIOLATION exit reason.
For now we only handle subset of instruction that kernel uses for MMIO oerations. User-space access triggers SIGBUS.
--- arch/x86/kernel/tdx.c | 100 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+)
arch/x86/kernel/tdx.c +100 -0
@@ -5,6 +5,8 @@#include <asm/tdx.h> #include <asm/vmx.h> +#include <asm/insn.h> +#include <linux/sched/signal.h> /* force_sig_fault() */ #include <linux/cpu.h>
@@ -219,6 +221,101 @@ static void tdg_handle_io(struct pt_regs *regs, u32 exit_qual) } } +static unsigned long tdg_mmio (int size,bool write,unsigned long addr, +unsigned long val) +{ +return tdvmcall_out_r11 (EXIT_REASON_EPT_VIOLATION , size, + write, addr, val); +} + +static inline void *get_reg_ptr (struct pt_regs *regs,struct insn *insn) +{ +static const int regoff[] = { + offsetof(struct pt_regs , ax), + offsetof(struct pt_regs , cx), + offsetof(struct pt_regs , dx), + offsetof(struct pt_regs , bx), + offsetof(struct pt_regs , sp), + offsetof(struct pt_regs , bp), + offsetof(struct pt_regs , si), + offsetof(struct pt_regs , di), + offsetof(struct pt_regs , r8), + offsetof(struct pt_regs , r9), + offsetof(struct pt_regs , r10), + offsetof(struct pt_regs , r11), + offsetof(struct pt_regs , r12), + offsetof(struct pt_regs , r13), + offsetof(struct pt_regs , r14), + offsetof(struct pt_regs , r15), + }; +int regno; + + regno =X86_MODRM_REG (insn->modrm.value); +if (X86_REX_R (insn->rex_prefix.value)) + regno +=8 ; + +return (void *)regs + regoff[regno]; +} + +static int tdg_handle_mmio (struct pt_regs *regs,struct ve_info *ve) +{ +int size; +bool write; +unsigned long *reg; +struct insn insn; +unsigned long val =0 ; + +/* +* User mode would mean the kernel exposed a device directly +* to ring3, which shouldn't happen except for things like +* DPDK. +*/ +if (user_mode (regs)) { +pr_err ("Unexpected user-mode MMIO access.\n" ); +force_sig_fault (SIGBUS ,BUS_ADRERR , (void __user *) ve->gla); +return 0 ; + } + +kernel_insn_init (&insn, (void *) regs->ip,MAX_INSN_SIZE ); +insn_get_length (&insn); +insn_get_opcode (&insn); + + write = ve->exit_qual &0x2 ; + + size = insn.opnd_bytes; +switch (insn.opcode.bytes[0 ]) { +/* MOV r/m8 r8 */ +case 0x88 : +/* MOV r8 r/m8 */ +case 0x8A : +/* MOV r/m8 imm8 */ +case 0xC6 : + size =1 ; +break ; + } + +if (inat_has_immediate (insn.attr)) { +BUG_ON (!write); + val = insn.immediate.value; +tdg_mmio (size, write, ve->gpa, val); +return insn.length; + } + +BUG_ON (!inat_has_modrm (insn.attr)); + + reg =get_reg_ptr (regs, &insn); + +if (write) { +memcpy (&val, reg, size); +tdg_mmio (size, write, ve->gpa, val); + }else { + val =tdg_mmio (size, write, ve->gpa, val); +memset (reg,0 , size); +memcpy (reg, &val, size); + } +return insn.length; +} +unsigned long tdg_get_ve_info (struct ve_info *ve) {u64 ret;
@@ -271,6 +368,9 @@ int tdg_handle_virtualization_exception(struct pt_regs *regs,case EXIT_REASON_IO_INSTRUCTION :tdg_handle_io (regs, ve->exit_qual);break ; +case EXIT_REASON_EPT_VIOLATION : + ve->instr_len =tdg_handle_mmio (regs, ve); +break ;default :pr_warn ("Unexpected #VE: %lld\n" , ve->exit_reason);return -EFAULT ;
Signature · 1 line
2.25.1
Re: [RFC v2 15/32] x86/tdx: Handle in-kernel MMIO
Quoted 2 lines · Kuppuswamy Sathyanarayanan
Handle #VE due to MMIO operations. MMIO triggers #VE with
EPT_VIOLATIONexit reason.
This needs a bit of a history lesson. "In traditional VMs, MMIO tends to be implemented by giving a guest access to an mapping which will cause a VMEXIT on access. That's not possible in a TDX guest..."
Quoted 2 lines
For now we only handle subset of instruction that kernel uses for MMIO oerations. User-space access triggers SIGBUS.
I still don't think that TDX guests should be doing things that they *KNOW* will cause #VE, including MMIO. I really want to hear a more discrete story about why this is the *best* way to do this for Linux instead of just a hack from the Windows binary driver ecosystem that seemed expedient.
RFC 1/1 x86/tdx: Handle in-kernel MMIO
From: "Kirill A. Shutemov" <[email protected]>
In traditional VMs, MMIO tends to be implemented by giving a guest access to a mapping which will cause a VMEXIT on access. That's not possible in TDX guest. So use #VE to implement MMIO support. In TDX guest, MMIO triggers #VE with EPT_VIOLATION exit reason.
For now we only handle a subset of instructions that the kernel uses for MMIO operations. User-space access triggers SIGBUS.
Also, reasons for supporting #VE based MMIO in TDX guest are,
* MMIO is widely used and we'll have more drivers in the future.
* We don't want to annotate every TDX specific MMIO readl/writel etc.
* If we didn't annotate we would need to add an alternative to every MMIO access in the kernel (even though 99.9% will never be used on TDX) which would be a complete waste and incredible binary bloat for nothing.
---
Changes since RFC v2:
* Fixed commit log as per Dave's review.
arch/x86/kernel/tdx.c | 100 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+)
arch/x86/kernel/tdx.c +100 -0
@@ -5,6 +5,8 @@#include <asm/tdx.h> #include <asm/vmx.h> +#include <asm/insn.h> +#include <linux/sched/signal.h> /* force_sig_fault() */ #include <linux/cpu.h> #include <linux/protected_guest.h>
@@ -209,6 +211,101 @@ static void tdg_handle_io(struct pt_regs *regs, u32 exit_qual) } } +static unsigned long tdg_mmio (int size,bool write,unsigned long addr, +unsigned long val) +{ +return tdx_hypercall_out_r11 (EXIT_REASON_EPT_VIOLATION , size, + write, addr, val); +} + +static inline void *get_reg_ptr (struct pt_regs *regs,struct insn *insn) +{ +static const int regoff[] = { + offsetof(struct pt_regs , ax), + offsetof(struct pt_regs , cx), + offsetof(struct pt_regs , dx), + offsetof(struct pt_regs , bx), + offsetof(struct pt_regs , sp), + offsetof(struct pt_regs , bp), + offsetof(struct pt_regs , si), + offsetof(struct pt_regs , di), + offsetof(struct pt_regs , r8), + offsetof(struct pt_regs , r9), + offsetof(struct pt_regs , r10), + offsetof(struct pt_regs , r11), + offsetof(struct pt_regs , r12), + offsetof(struct pt_regs , r13), + offsetof(struct pt_regs , r14), + offsetof(struct pt_regs , r15), + }; +int regno; + + regno =X86_MODRM_REG (insn->modrm.value); +if (X86_REX_R (insn->rex_prefix.value)) + regno +=8 ; + +return (void *)regs + regoff[regno]; +} + +static int tdg_handle_mmio (struct pt_regs *regs,struct ve_info *ve) +{ +int size; +bool write; +unsigned long *reg; +struct insn insn; +unsigned long val =0 ; + +/* +* User mode would mean the kernel exposed a device directly +* to ring3, which shouldn't happen except for things like +* DPDK. +*/ +if (user_mode (regs)) { +pr_err ("Unexpected user-mode MMIO access.\n" ); +force_sig_fault (SIGBUS ,BUS_ADRERR , (void __user *) ve->gla); +return 0 ; + } + +kernel_insn_init (&insn, (void *) regs->ip,MAX_INSN_SIZE ); +insn_get_length (&insn); +insn_get_opcode (&insn); + + write = ve->exit_qual &0x2 ; + + size = insn.opnd_bytes; +switch (insn.opcode.bytes[0 ]) { +/* MOV r/m8 r8 */ +case 0x88 : +/* MOV r8 r/m8 */ +case 0x8A : +/* MOV r/m8 imm8 */ +case 0xC6 : + size =1 ; +break ; + } + +if (inat_has_immediate (insn.attr)) { +BUG_ON (!write); + val = insn.immediate.value; +tdg_mmio (size, write, ve->gpa, val); +return insn.length; + } + +BUG_ON (!inat_has_modrm (insn.attr)); + + reg =get_reg_ptr (regs, &insn); + +if (write) { +memcpy (&val, reg, size); +tdg_mmio (size, write, ve->gpa, val); + }else { + val =tdg_mmio (size, write, ve->gpa, val); +memset (reg,0 , size); +memcpy (reg, &val, size); + } +return insn.length; +} +unsigned long tdg_get_ve_info (struct ve_info *ve) {u64 ret;
@@ -258,6 +355,9 @@ int tdg_handle_virtualization_exception(struct pt_regs *regs,case EXIT_REASON_IO_INSTRUCTION :tdg_handle_io (regs, ve->exit_qual);break ; +case EXIT_REASON_EPT_VIOLATION : + ve->instr_len =tdg_handle_mmio (regs, ve); +break ;default :pr_warn ("Unexpected #VE: %lld\n" , ve->exit_reason);return -EFAULT ;
Signature · 1 line
2.25.1
Re: [RFC v2-fix 1/1] x86/tdx: Handle in-kernel MMIO
Quoted 5 lines · Kuppuswamy Sathyanarayanan
From: "Kirill A. Shutemov" <[email protected]>
In traditional VMs, MMIO tends to be implemented by giving a guest access to a mapping which will cause a VMEXIT on access. That's not possible in TDX guest.
Why is it not possible?
Quoted 2 lines
So use #VE to implement MMIO support. In TDX guest, MMIO triggers #VE with
EPT_VIOLATIONexit reason.
What does the #VE handler do to resolve the exception?
Quoted 2 lines
For now we only handle a subset of instructions that the kernel uses for MMIO operations. User-space access triggers SIGBUS.
How do you know which instructions the kernel uses? How do you know that the compiler won't change them?
I guess the kernel won't boot far if this happens, but this still sounds like trial-and-error programming.
Quoted 3 lines
Also, reasons for supporting #VE based MMIO in TDX guest are,
* MMIO is widely used and we'll have more drivers in the future.
OK, but you've also made a big deal about having to go explicitly audit these drivers. I would imagine converting these over to stop using MMIO would be _relatively_ minor compared to a big security audit and new fuzzing infrastructure.
Quoted 1 line
* We don't want to annotate every TDX specific MMIO readl/writel etc.
^ TDX-specific
Quoted 4 lines
* If we didn't annotate we would need to add an alternative to every MMIO access in the kernel (even though 99.9% will never be used on TDX) which would be a complete waste and incredible binary bloat for nothing.
That sounds like something objective we can measure. Does this cost 1 byte of extra text per readl/writel? 10? 100?
You're also being rather indirect about what solutions you ruled out. Why not just say: we considered doing ____, but ruled that out because it would have required ____. Above you just tell us what the solution required without mentioning the solution.
Quoted 42 lines
arch/x86/kernel/tdx.c +27
-0@@ -5,6 +5,8 @@#include <asm/tdx.h> #include <asm/vmx.h> +#include <asm/insn.h> +#include <linux/sched/signal.h> /* force_sig_fault() */ #include <linux/cpu.h> #include <linux/protected_guest.h> @@ -209,6 +211,101 @@ static void tdg_handle_io(struct pt_regs *regs, u32 exit_qual) } } +static unsigned long tdg_mmio (int size,bool write,unsigned long addr, +unsigned long val) +{ +return tdx_hypercall_out_r11 (EXIT_REASON_EPT_VIOLATION , size, + write, addr, val); +} + +static inline void *get_reg_ptr (struct pt_regs *regs,struct insn *insn) +{ +static const int regoff[] = { + offsetof(struct pt_regs , ax), + offsetof(struct pt_regs , cx), + offsetof(struct pt_regs , dx), + offsetof(struct pt_regs , bx), + offsetof(struct pt_regs , sp), + offsetof(struct pt_regs , bp), + offsetof(struct pt_regs , si), + offsetof(struct pt_regs , di), + offsetof(struct pt_regs , r8), + offsetof(struct pt_regs , r9), + offsetof(struct pt_regs , r10), + offsetof(struct pt_regs , r11), + offsetof(struct pt_regs , r12), + offsetof(struct pt_regs , r13), + offsetof(struct pt_regs , r14),
arch/x86/kernel/tdx.c static void tdg_handle_io(struct pt_regs *regs, u32 exit_qual) · line 248
Quoted 10 lines
arch/x86/kernel/tdx.c +10
-0+ offsetof(struct pt_regs, r15), + }; +int regno; + + regno =X86_MODRM_REG (insn->modrm.value); +if (X86_REX_R (insn->rex_prefix.value)) + regno +=8 ; + +return (void *)regs + regoff[regno]; +}
Was there a reason you copied and pasted this from get_reg_offset() instead of refactoring? This looks like almost entirely a subset of get_reg_offset().
arch/x86/kernel/tdx.c static void tdg_handle_io(struct pt_regs *regs, u32 exit_qual) · line 262
Quoted 13 lines
arch/x86/kernel/tdx.c +13
-0+static int tdg_handle_mmio (struct pt_regs *regs,struct ve_info *ve) +{ +int size; +bool write; +unsigned long *reg; +struct insn insn; +unsigned long val =0 ; + +/* +* User mode would mean the kernel exposed a device directly +* to ring3, which shouldn't happen except for things like +* DPDK. +*/
Uhh....
https://www.kernel.org/doc/html/v4.14/driver-api/uio-howto.html
I thought there were more than a few ways that userspace could get access to MMIO mappings.
Also, do most people know what DPDK is? Should we even be talking about silly out-of-tree kernel bypass schemes in kernel comments?
arch/x86/kernel/tdx.c static void tdg_handle_io(struct pt_regs *regs, u32 exit_qual) · line 265
Quoted 3 lines
arch/x86/kernel/tdx.c +3
-0+if (user_mode (regs)) { +pr_err ("Unexpected user-mode MMIO access.\n" ); +force_sig_fault (SIGBUS ,BUS_ADRERR , (void __user *) ve->gla);
extra space ^
Is a non-ratelimited pr_err() appropriate here? I guess there shouldn't be any MMIO passthrough to userspace on these systems.
Quoted 7 lines
arch/x86/kernel/tdx.c +7
-0+return 0 ; + } + +kernel_insn_init (&insn, (void *) regs->ip,MAX_INSN_SIZE ); +insn_get_length (&insn); +insn_get_opcode (&insn); +
arch/x86/kernel/tdx.c static void tdg_handle_io(struct pt_regs *regs, u32 exit_qual) · line 282
Quoted 10 lines
arch/x86/kernel/tdx.c +10
-0+ write = ve->exit_qual &0x2 ; + + size = insn.opnd_bytes; +switch (insn.opcode.bytes[0 ]) { +/* MOV r/m8 r8 */ +case 0x88 : +/* MOV r8 r/m8 */ +case 0x8A : +/* MOV r/m8 imm8 */ +case 0xC6 :
FWIW, I find that *REALLY* hard to read.
Check out is_string_insn() for a more readable example.
Oh, and I misread that. I read it as "these are all the opcodes we care about". When, in fact, I _think_ it's all the opcodes that don't have a size in insn.opnd_bytes.
Could you spell that out, please?
arch/x86/kernel/tdx.c static void tdg_handle_io(struct pt_regs *regs, u32 exit_qual) · line 289
Quoted 7 lines
arch/x86/kernel/tdx.c +7
-0+ size =1 ; +break ; + } + +if (inat_has_immediate (insn.attr)) { +BUG_ON (!write); + val = insn.immediate.value;
This is pretty interesting. This won't work with implicit accesses. I guess the limited opcodes above limit how much imprecision will result.
But, it would still be nice to hear something about that.
For instance, if someone pointed a mid-level page table to MMIO, we'd get a va->gpa that had zero to do with the instruction. Granted, that's only going to happen if something bonkers is going on, but maybe I'm missing some simpler cases of implicit accesses.
arch/x86/kernel/tdx.c static void tdg_handle_io(struct pt_regs *regs, u32 exit_qual) · line 289
Quoted 1 line
arch/x86/kernel/tdx.c +1
-0+tdg_mmio (size, write, ve->gpa, val);
What happens if this is an MMIO operation that *partially* touches MMIO and partially touches normal memory? Let's say I wrote two bytes (0x1234), starting at the last byte of a RAM page that ran over into an MMIO page. The fault would occur trying to write 0x34 to the MMIO, but the instruction cracking would result in trying to write 0x1234 into the MMIO.
It doesn't seem *that* outlandish that an MMIO might cross a page boundary. Would this work for a two-byte MMIO that crosses a page?
arch/x86/kernel/tdx.c static void tdg_handle_io(struct pt_regs *regs, u32 exit_qual) · line 294
Quoted 4 lines
arch/x86/kernel/tdx.c +4
-0+return insn.length; + } + +BUG_ON (!inat_has_modrm (insn.attr));
A comment would be nice here about the BUG_ON().
It would also be nice to give a high-level view of what's going on and what we know about the instruction at this point.
Quoted 27 lines
arch/x86/kernel/tdx.c +16
-0+ reg =get_reg_ptr (regs, &insn); + +if (write) { +memcpy (&val, reg, size); +tdg_mmio (size, write, ve->gpa, val); + }else { + val =tdg_mmio (size, write, ve->gpa, val); +memset (reg,0 , size); +memcpy (reg, &val, size); + } +return insn.length; +} +unsigned long tdg_get_ve_info (struct ve_info *ve) {u64 ret;@@ -258,6 +355,9 @@ int tdg_handle_virtualization_exception(struct pt_regs *regs,case EXIT_REASON_IO_INSTRUCTION :tdg_handle_io (regs, ve->exit_qual);break ; +case EXIT_REASON_EPT_VIOLATION : + ve->instr_len =tdg_handle_mmio (regs, ve); +break ;default :pr_warn ("Unexpected #VE: %lld\n" , ve->exit_reason);return -EFAULT ;
Re: [RFC v2-fix 1/1] x86/tdx: Handle in-kernel MMIO
Quoted 7 lines · Dave Hansen
Quoted 5 lines · Kuppuswamy Sathyanarayanan
From: "Kirill A. Shutemov" <[email protected]>
In traditional VMs, MMIO tends to be implemented by giving a guest access to a mapping which will cause a VMEXIT on access. That's not possible in TDX guest.
Why is it not possible?
For once the TDX module doesn't support uncached mappings (IgnorePAT is always 1)
Quoted 4 lines
Quoted 3 lines
For now we only handle a subset of instructions that the kernel uses for MMIO operations. User-space access triggers SIGBUS.
How do you know which instructions the kernel uses?
They're all in MMIO macros.
Quoted 2 lines
How do you know
that the compiler won't change them?
The macros try hard to prevent that because it would likely break real MMIO too.
Besides it works for others, like AMD-SEV today and of course all the hypervisors that do the same.
Quoted 2 lines
That sounds like something objective we can measure. Does this cost 1 byte of extra text per readl/writel? 10? 100?
Alternatives are at least a pointer, but also the extra alternative code. It's definitely more than 10, I would guess 40+
Quoted 3 lines
I thought there were more than a few ways that userspace could get access to MMIO mappings.
Yes and they will all fault in TDX guests.
Quoted 7 lines
arch/x86/kernel/tdx.c static void tdg_handle_io(struct pt_regs *regs, u32 exit_qual) · line 265
Quoted 3 lines
arch/x86/kernel/tdx.c +3
-0+if (user_mode (regs)) { +pr_err ("Unexpected user-mode MMIO access.\n" ); +force_sig_fault (SIGBUS ,BUS_ADRERR , (void __user *) ve->gla);extra space ^
Is a non-ratelimited
pr_err()appropriate here? I guess there shouldn't be any MMIO passthrough to userspace on these systems.
Yes rate limiting makes sense.
Re: [RFC v2-fix 1/1] x86/tdx: Handle in-kernel MMIO
Quoted 11 lines · Andi Kleen
Quoted 7 lines · Dave Hansen
Quoted 5 lines · Kuppuswamy Sathyanarayanan
From: "Kirill A. Shutemov" <[email protected]>
In traditional VMs, MMIO tends to be implemented by giving a guest access to a mapping which will cause a VMEXIT on access. That's not possible in TDX guest.
Why is it not possible?
For once the TDX module doesn't support uncached mappings (IgnorePAT is always 1)
Actually, I was thinking more along the lines of why the architecture doesn't have VMEXITs: VMEXITs expose guest state to the host and VMMs use that state to emulate MMIO. TDX guests don't trust the host and can't have that arbitrary state exposed to the host. So, they sanitize the state in the #VE handler and make a *controlled* transition into the host with a TDCALL rather than an uncontrolled VMEXIT.
Quoted 5 lines
Quoted 3 lines
Quoted 2 lines
For now we only handle a subset of instructions that the kernel uses for MMIO operations. User-space access triggers SIGBUS.
How do you know which instructions the kernel uses?
They're all in MMIO macros.
I've heard exactly the opposite from the TDX team in the past. What I remember was a claim that one can not just leverage the MMIO macros as a single point to avoid MMIO. I remember being told that not all code in the kernel that does MMIO uses these macros. APIC MMIO's were called out as a place that does not use the MMIO macros.
I'm confused now.
Quoted 7 lines
Quoted 1 line
How do you know that the compiler won't change them?
The macros try hard to prevent that because it would likely break real MMIO too.
Besides it works for others, like AMD-SEV today and of course all the hypervisors that do the same.
That would be some excellent information for the changelog.
Re: [RFC v2-fix 1/1] x86/tdx: Handle in-kernel MMIO
Quoted 10 lines
Quoted 5 lines
Quoted 4 lines
Quoted 3 lines
For now we only handle a subset of instructions that the kernel uses for MMIO operations. User-space access triggers SIGBUS.
How do you know which instructions the kernel uses?
They're all in MMIO macros.
I've heard exactly the opposite from the TDX team in the past. What I remember was a claim that one can not just leverage the MMIO macros as a single point to avoid MMIO. I remember being told that not all code in the kernel that does MMIO uses these macros. APIC MMIO's were called out as a place that does not use the MMIO macros.
Yes x86 APIC has its own macros, but we don't use the MMIO based APIC, only X2APIC in TDX.
I'm not aware of any other places that would do MMIO without using the standard io.h macros, although it might happen in theory on x86 (but would likely break on some other architectures)
-Andi
Re: [RFC v2-fix 1/1] x86/tdx: Handle in-kernel MMIO
Quoted 3 lines · Andi Kleen
I'm not aware of any other places that would do MMIO without using the standard
io.hmacros, although it might happen in theory on x86 (but would likely break on some other architectures)
Can we please connect all of the dots and turn this into a coherent changelog?
* In-kernel MMIO is handled via exceptions (#VE) and instruction cracking
* Arbitrary MMIO instructions are not handled (and would result in...)
* The limited set of MMIO instructions that are handled are known and come from the io.h macros, ultimately build_mmio_read/write().
* This approach is also used for SEV-ES???
* Some x86 code that avoids the MMIO code is known to exist (APIC). But, this code is not used in TDX guests
BTW, in perusing arch/x86/include/asm/io.h, I was reminded of movdir64b.
That seems like one we'd want to take care of sooner rather than later. Or, do we expect the first folks who expose a movdir64b-using driver to
TDX to go and update this code?
Also, the sev_key_active() stuff in there makes me nervous. Does this scheme work with these:
Quoted 2 lines
static inline void outs##bwl(int port, const void *addr, unsigned long count) \
static inline void ins##bwl(int port, void *addr, unsigned long count) \
?
Re: [RFC v2-fix 1/1] x86/tdx: Handle in-kernel MMIO
Quoted 3 lines
Or, do we expect the first folks who expose a movdir64b-using driver to
TDX to go and update this code?
That's what we want to do.
Quoted 7 lines
Also, the
sev_key_active()stuff in there makes me nervous. Does this scheme work with these:Quoted 2 lines
static inline void outs##bwl(int port, const void *addr, unsigned long count) \
static inline void ins##bwl(int port, void *addr, unsigned long count) \?
This is not MMIO, but port IO. We do similar changes as AMD for TDX.
-Andi
Re: [RFC v2-fix 1/1] x86/tdx: Handle in-kernel MMIO
In reply to Andi KleenQuoted 4 lines · Andi Kleen
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
For now we only handle a subset of instructions that the kernel uses for MMIO operations. User-space access triggers SIGBUS.
How do you know which instructions the kernel uses?
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
They're all in MMIO macros.
I've heard exactly the opposite from the TDX team in the past. What I remember was a claim that one can not just leverage the MMIO macros as a single point to avoid MMIO. I remember being told that not all code in the kernel that does MMIO uses these macros. APIC MMIO's were called out as a place that does not use the MMIO macros.
Yes x86 APIC has its own macros, but we don't use the MMIO based APIC, only X2APIC in TDX.
I must correct myself here. We actually use #VE to handle MSRs, or at least those that are not context switched by the TDX module. So there can be #VE nested in NMI in normal operation, since MSR accesses in NMI can happen.
I don't think it needs any changes to the code -- this should all work -- but we need to update the commit log to document this case.
-Andi
Sean Christopherson
maintainerRe: [RFC v2-fix 1/1] x86/tdx: Handle in-kernel MMIO
In reply to Andi KleenQuoted 6 lines · Andi Kleen
Quoted 2 lines · Dave Hansen
That sounds like something objective we can measure. Does this cost 1 byte of extra text per readl/writel? 10? 100?
Alternatives are at least a pointer, but also the extra alternative code. It's definitely more than 10, I would guess 40+
The extra bytes for .altinstructions is very different than the extra bytes for the code itself. The .altinstructions section is freed after init, so yes it bloats the kernel size a bit, but the runtime footprint is unaffected by the patching metadata.
IIRC, patching read/write{b,w,l,q}() can be done with 3 bytes of .text overhead.
The other option to explore is to hook/patch IO_COND(), which can be done with neglible overhead because the helpers that use IO_COND() are not inlined. In a TDX guest, redirecting IO_COND() to a paravirt helper would likely cover the majority of IO/MMIO since virtio-pci exclusively uses the IO_COND() wrappers. And if there are TDX VMMs that want to deploy virtio-mmio, hooking drivers/virtio/virtio_mmio.c directly would be a viable option.
Re: [RFC v2-fix 1/1] x86/tdx: Handle in-kernel MMIO
Quoted 6 lines
The extra bytes for .altinstructions is very different than the extra bytes for the code itself. The .altinstructions section is freed after init, so yes it bloats the kernel size a bit, but the runtime footprint is unaffected by the patching metadata.
Quoted 8 lines
IIRC, patching read/write{b,w,l,q}() can be done with 3 bytes of .text overhead.
The other option to explore is to hook/patch
IO_COND(), which can be done with neglible overhead because the helpers that useIO_COND()are not inlined. In a TDX guest, redirectingIO_COND()to a paravirt helper would likely cover the majority of IO/MMIO since virtio-pci exclusively uses theIO_COND()wrappers. And if there are TDX VMMs that want to deploy virtio-mmio, hookingdrivers/virtio/virtio_mmio.cdirectly would be a viable option.
Yes but what's the point of all that?
Even if it's only 3 bytes we still have a lot of MMIO all over the kernel which never needs it.
And I don't even see what TDX (or SEV which already does the decoding and has been merged) would get out of it. We handle all the #VEs just fine. And the instruction handling code is fairly straight forward too.
Besides instruction decoding works fine for all the existing hypervisors. All we really want to do is to do the same thing as KVM would do.
-Andi
Re: [RFC v2-fix 1/1] x86/tdx: Handle in-kernel MMIO
Quoted 3 lines · Andi Kleen
Besides instruction decoding works fine for all the existing hypervisors. All we really want to do is to do the same thing as KVM would do.
Dumb question of the day: If you want to do the same thing that KVM does, why don't you share more code with KVM? Wouldn't you, for instance, need to crack the same instruction opcodes?
I'd feel a lot better about this if you said:
Listen, this doesn't work for everything. But, it will run every single driver as a TDX guest that KVM can handle as a host. So, if the TDX code is broken, so is the KVM host code.
Sean Christopherson
maintainerRe: [RFC v2-fix 1/1] x86/tdx: Handle in-kernel MMIO
Quoted 8 lines · Dave Hansen
Quoted 3 lines · Andi Kleen
Besides instruction decoding works fine for all the existing hypervisors. All we really want to do is to do the same thing as KVM would do.
Dumb question of the day: If you want to do the same thing that KVM does, why don't you share more code with KVM? Wouldn't you, for instance, need to crack the same instruction opcodes?
Pulling in all pf KVM's emulator is a bad idea from a security perspective. That could be mitigated to some extent by teaching the emulator to emulate only select instructions, but it'd still be much higher risk than a barebones guest-specific implementations. Because old Intel CPUs don't support unrestricted guest, the set of instructions that KVM _can_ emulate in total is far, far larger than what is needed for MMIO.
Allowed instructions aside, KVM needs to handle a large number things a TDX/SEV guest does not, e.g. segmentation, CPUID model, A/D bit updates, and so on and so forth.
Refactoring KVM's emulator would also be a monumental task.
Re: [RFC v2-fix 1/1] x86/tdx: Handle in-kernel MMIO
In reply to Dave HansenQuoted 7 lines · Dave Hansen
Quoted 3 lines · Andi Kleen
Besides instruction decoding works fine for all the existing hypervisors. All we really want to do is to do the same thing as KVM would do.
Dumb question of the day: If you want to do the same thing that KVM does, why don't you share more code with KVM? Wouldn't you, for instance, need to crack the same instruction opcodes?
We're talking about ~60 lines of codes that calls an established standard library.
You're proposing a gigantic refactoring to avoid 60 lines of straight forward code.
That's not a practical proposal.
Quoted 6 lines
I'd feel a lot better about this if you said:
Listen, this doesn't work for everything. But, it will run every single driver as a TDX guest that KVM can handle as a host. So, if the TDX code is broken, so is the KVM host code.
I don't really know what problem you're trying to solve here. We only have a small number of drivers and we tested them and they work fine. There are special macros that limit the number of instructions. If there are ever more instructions and the macros break somehow we'll add them. There will be a clean error if it ever happens. We're not trying to solve hypothetical problems here.
-Andi
Re: [RFC v2-fix 1/1] x86/tdx: Handle in-kernel MMIO
Quoted 13 lines · Andi Kleen
Quoted 7 lines · Dave Hansen
Quoted 3 lines · Andi Kleen
Besides instruction decoding works fine for all the existing hypervisors. All we really want to do is to do the same thing as KVM would do.
Dumb question of the day: If you want to do the same thing that KVM does, why don't you share more code with KVM? Wouldn't you, for instance, need to crack the same instruction opcodes?
We're talking about ~60 lines of codes that calls an established standard library.
Quoted 6 lines
You're proposing a gigantic refactoring to avoid 60 lines of straight forward code.
That's not a practical proposal.
Hi Andi,
I'm not actually trying to propose things. I'm really just trying to get an idea why the implementation ended up how it did. I actually entirely respect the position that the KVM code is a monster and shouldn't get reused. That seems totally reasonable.
What isn't reasonable is the lack of documentation of these design decisions in the changelogs. My goal here is to raise the quality of the changelogs so that other reviewers and maintainers don't have to ask these questions when they perform their reviews.
This is honestly the best way I know to help get this code merged as soon as possible. If I'm not helping, please let me know. I'm happy to spend my time elsewhere.
Re: [RFC v2-fix 1/1] x86/tdx: Handle in-kernel MMIO
Quoted 5 lines
I'm not actually trying to propose things. I'm really just trying to get an idea why the implementation ended up how it did. I actually entirely respect the position that the KVM code is a monster and shouldn't get reused. That seems totally reasonable.
Mainly because it's relatively simple and straight forward to do it this way, Yes I know, that's a shocking concept, but sometimes it works even in Linux code.
Quoted 9 lines
What isn't reasonable is the lack of documentation of these design decisions in the changelogs. My goal here is to raise the quality of the changelogs so that other reviewers and maintainers don't have to ask these questions when they perform their reviews.
This is honestly the best way I know to help get this code merged as soon as possible. If I'm not helping, please let me know. I'm happy to spend my time elsewhere.
I'm sure the commit logs can be improved and I appreciate your feedback.
I don't think every commit log needs to be an extended essay meandering all over the possible design space, talking about everything that could have been and wasn't. The way code is normally written is that we don't do an exhaustive search of possible options, but instead we pick a reasonable path and as long as that works and doesn't have too many problems we just stick to it. The commit log reflects that single path chosen, with only rare exceptions to talk about dead alleys.
In this case you can even see that multiple independent efforts (AMD and Intel) came mostly to fairly similar implementations, so the path chosen wasn't really that strange or non obvious.
Also overall I would appreciate if people would focus more on the code than the commit logs. Commit logs are important, but in the end what really matters is that the code is correct.
-Andi
Sean Christopherson
maintainerRe: [RFC v2-fix 1/1] x86/tdx: Handle in-kernel MMIO
In reply to Andi KleenQuoted 8 lines · Andi Kleen
Quoted 8 lines
The extra bytes for .altinstructions is very different than the extra bytes for the code itself. The .altinstructions section is freed after init, so yes it bloats the kernel size a bit, but the runtime footprint is unaffected by the patching metadata.
IIRC, patching read/write{b,w,l,q}() can be done with 3 bytes of .text overhead.
Quoted 8 lines
Quoted 6 lines
The other option to explore is to hook/patch
IO_COND(), which can be done with neglible overhead because the helpers that useIO_COND()are not inlined. In a TDX guest, redirectingIO_COND()to a paravirt helper would likely cover the majority of IO/MMIO since virtio-pci exclusively uses theIO_COND()wrappers. And if there are TDX VMMs that want to deploy virtio-mmio, hookingdrivers/virtio/virtio_mmio.cdirectly would be a viable option.Yes but what's the point of all that?
Patching IO_COND() is relatively low effort. With some clever refactoring, I suspect the net lines of code added would be less than 10. That seems like a worthwhile effort to avoid millions of faults over the lifetime of the guest.
Quoted 9 lines
Even if it's only 3 bytes we still have a lot of MMIO all over the kernel which never needs it.
And I don't even see what TDX (or SEV which already does the decoding and has been merged) would get out of it. We handle all the #VEs just fine. And the instruction handling code is fairly straight forward too.
Besides instruction decoding works fine for all the existing hypervisors. All we really want to do is to do the same thing as KVM would do.
Heh, trust me, you don't want to do the same thing KVM does :-)
Re: [RFC v2-fix 1/1] x86/tdx: Handle in-kernel MMIO
Quoted 14 lines · Sean Christopherson
Quoted 13 lines · Andi Kleen
Quoted 13 lines
The extra bytes for .altinstructions is very different than the extra bytes for the code itself. The .altinstructions section is freed after init, so yes it bloats the kernel size a bit, but the runtime footprint is unaffected by the patching metadata.
IIRC, patching read/write{b,w,l,q}() can be done with 3 bytes of .text overhead.
The other option to explore is to hook/patch
IO_COND(), which can be done with neglible overhead because the helpers that useIO_COND()are not inlined. In a TDX guest, redirectingIO_COND()to a paravirt helper would likely cover the majority of IO/MMIO since virtio-pci exclusively uses theIO_COND()wrappers. And if there are TDX VMMs that want to deploy virtio-mmio, hookingdrivers/virtio/virtio_mmio.cdirectly would be a viable option.
Quoted 4 lines
Quoted 1 line
Yes but what's the point of all that?
Patching
IO_COND()is relatively low effort. With some clever refactoring, I suspect the net lines of code added would be less than 10. That seems like a worthwhile effort to avoid millions of faults over the lifetime of the guest.
AFAIK IO_COND is only for iomap users. But most drivers don't even use iomap. virtio doesn't for example, and that's really the only case we currently care about.
Also millions of faults is nothing for a CPU.
The only case I can see it making sense is the virtio (and vmbus) door bells. Everything else should be slow path anyways.
But doing that now would be premature optimization and that's usually a bad idea. If it's a problem we can fix it later.
Quoted 11 lines
Quoted 10 lines
Even if it's only 3 bytes we still have a lot of MMIO all over the kernel which never needs it.
And I don't even see what TDX (or SEV which already does the decoding and has been merged) would get out of it. We handle all the #VEs just fine. And the instruction handling code is fairly straight forward too.
Besides instruction decoding works fine for all the existing hypervisors. All we really want to do is to do the same thing as KVM would do.
Heh, trust me, you don't want to do the same thing KVM does :-)
We want the same behavior.
Yes probably not the same code.
-Andi
Sean Christopherson
maintainerRe: [RFC v2-fix 1/1] x86/tdx: Handle in-kernel MMIO
Quoted 16 lines · Andi Kleen
Quoted 14 lines · Sean Christopherson
Quoted 13 lines · Andi Kleen
Quoted 13 lines
The extra bytes for .altinstructions is very different than the extra bytes for the code itself. The .altinstructions section is freed after init, so yes it bloats the kernel size a bit, but the runtime footprint is unaffected by the patching metadata.
IIRC, patching read/write{b,w,l,q}() can be done with 3 bytes of .text overhead.
The other option to explore is to hook/patch
IO_COND(), which can be done with neglible overhead because the helpers that useIO_COND()are not inlined. In a TDX guest, redirectingIO_COND()to a paravirt helper would likely cover the majority of IO/MMIO since virtio-pci exclusively uses theIO_COND()wrappers. And if there are TDX VMMs that want to deploy virtio-mmio, hookingdrivers/virtio/virtio_mmio.cdirectly would be a viable option.
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
Yes but what's the point of all that?
Patching
IO_COND()is relatively low effort. With some clever refactoring, I suspect the net lines of code added would be less than 10. That seems like a worthwhile effort to avoid millions of faults over the lifetime of the guest.AFAIK
IO_CONDis only for iomap users. But most drivers don't even use iomap. virtio doesn't for example, and that's really the only case we currently care about.
virtio-pci, which is going to used by pretty much all traditional VMs, uses iomap. See vp_get(), vp_set(), and all the vp_io{read,write}*() wrappers.
Re: [RFC v2-fix 1/1] x86/tdx: Handle in-kernel MMIO
Quoted 4 lines
virtio-pci, which is going to used by pretty much all traditional VMs, uses iomap. See
vp_get(),vp_set(), and all the vp_io{read,write}*() wrappers.
That's true. But there are still all the other users. So it doesn't solve the problem. In the end I'm fairly sure we would need to patch readl/writel and friends.
-Andi
Sean Christopherson
maintainerRe: [RFC v2-fix 1/1] x86/tdx: Handle in-kernel MMIO
In reply to Dave HansenQuoted 8 lines · Dave Hansen
Quoted 5 lines · Kuppuswamy Sathyanarayanan
From: "Kirill A. Shutemov" <[email protected]>
In traditional VMs, MMIO tends to be implemented by giving a guest access to a mapping which will cause a VMEXIT on access. That's not possible in TDX guest.
Why is it not possible?
It is possible, and in fact KVM will cause a VM-Exit on the first access to a given MMIO page. The problem is that guest state is inaccessible and so the VMM cannot do the front end of MMIO instruction emulation.
Quoted 2 lines
Quoted 2 lines
So use #VE to implement MMIO support. In TDX guest, MMIO triggers #VE with
EPT_VIOLATIONexit reason.
It's more accurate to say that the VMM will configure EPT entries for pages that require instruction emulation to cause #VE.
Quoted 10 lines
What does the #VE handler do to resolve the exception?
Quoted 2 lines
For now we only handle a subset of instructions that the kernel uses for MMIO operations. User-space access triggers SIGBUS.
How do you know which instructions the kernel uses? How do you know that the compiler won't change them?
I guess the kernel won't boot far if this happens, but this still sounds like trial-and-error programming.
If a driver accesses MMIO through a struct overlay, all bets are off. The I/O APIC code does this, but that problem is "solved" by forcefully disabling the I/O APIC since it's useless for the current incarnation of TDX. IIRC, some of the console code also accesses MMIO via a struct (or maybe just through generic C code), and the compiler does indeed employ a wider variety of instructions.
So yeah, whack-a-mole.
Quoted 7 lines
Quoted 3 lines
Also, reasons for supporting #VE based MMIO in TDX guest are,
* MMIO is widely used and we'll have more drivers in the future.
OK, but you've also made a big deal about having to go explicitly audit these drivers. I would imagine converting these over to stop using MMIO would be _relatively_ minor compared
For drivers that use the kernel's macros, converting them to use TDVMCALL directly will be trivial and shouldn't even require any modifications to the driver. For drivers that use a struct overlay or generic C code, the "conversion" could require a complete rewrite of the driver.
Quoted 6 lines
to a big security audit and new fuzzing infrastructure.
Quoted 1 line
* We don't want to annotate every TDX specific MMIO readl/writel etc.
^ TDX-specific
Quoted 7 lines
Quoted 4 lines
* If we didn't annotate we would need to add an alternative to every MMIO access in the kernel (even though 99.9% will never be used on TDX) which would be a complete waste and incredible binary bloat for nothing.
That sounds like something objective we can measure. Does this cost 1 byte of extra text per readl/writel? 10? 100?
Agreed. And IMO, it's worth converting the common case (macros) if the overhead is acceptable, while leaving the #VE handling in place for non-standard code.
Quoted 1 line
Quoted 1 line
+static int
tdg_handle_mmio(struct pt_regs *regs, struct ve_info *ve)
...
Quoted 11 lines
Quoted 11 lines
arch/x86/kernel/tdx.c +11
-0+return 0 ; + } + +kernel_insn_init (&insn, (void *) regs->ip,MAX_INSN_SIZE ); +insn_get_length (&insn); +insn_get_opcode (&insn); + + write = ve->exit_qual &0x2 ; + + size = insn.opnd_bytes; +switch (insn.opcode.bytes[0 ]) {
Quoted 8 lines
arch/x86/kernel/tdx.c static void tdg_handle_io(struct pt_regs *regs, u32 exit_qual) · line 282
Quoted 6 lines
arch/x86/kernel/tdx.c +6
-0+/* MOV r/m8 r8 */ +case 0x88 : +/* MOV r8 r/m8 */ +case 0x8A : +/* MOV r/m8 imm8 */ +case 0xC6 :FWIW, I find that *REALLY* hard to read.
Why does this code exist at all? TDX and SEV-ES absolutely must share code for handling MMIO reflection. It will require a fair amount of refactoring to move the guts of vc_handle_mmio() to common code, but there is zero reason to maintain two separate versions of the opcode cracking.
Ditto for string I/O in vc_handle_ioio().
Quoted 9 lines
What happens if this is an MMIO operation that *partially* touches MMIO and partially touches normal memory? Let's say I wrote two bytes (
0x1234), starting at the last byte of a RAM page that ran over into an MMIO page. The fault would occur trying to write0x34to the MMIO, but the instruction cracking would result in trying to write0x1234into the MMIO.It doesn't seem *that* outlandish that an MMIO might cross a page boundary. Would this work for a two-byte MMIO that crosses a page?
I'm pretty sure we can get away with panic (kernel) and SIGBUS (userspace) on a reflected memory access that splits a page. Yes, it's theoretically possible and probably even "works", but practically speaking no emulated MMIO device is going to have a single logical register/thing split a page, and I can't think of any reason to allow accessing multiple registers/things across a page split.
The existing SEV-ES #VC handlers appear to be missing page split checks, so that needs to be fixed.
Re: [RFC v2-fix 1/1] x86/tdx: Handle in-kernel MMIO
Quoted 9 lines
Quoted 7 lines
Quoted 5 lines
* If we didn't annotate we would need to add an alternative to every MMIO access in the kernel (even though 99.9% will never be used on TDX) which would be a complete waste and incredible binary bloat for nothing.
That sounds like something objective we can measure. Does this cost 1 byte of extra text per readl/writel? 10? 100?
Agreed. And IMO, it's worth converting the common case (macros) if the overhead is acceptable, while leaving the #VE handling in place for non-standard code.
We have many millions of lines of MMIO using driver code in the kernel 99.99% of which never runs in TDX. I don't see any point in impacting everything for this. That would be just against all good code change hygiene practices, and also just be bloated.
But we also don't don't want to touch every driver, for similar reasons.
What I think would make sense is to convert something to a direct TDCALL if we figure out the extra #VE is a real life performance problem. AFAIK the only candidate that I have in mind for this is the virtio doorbell write (and potentially later its VMBus equivalent). But we should really only do that if some measurements show it's needed.
Quoted 4 lines
Why does this code exist at all? TDX and SEV-ES absolutely must share code for handling MMIO reflection. It will require a fair amount of refactoring to move the guts of
vc_handle_mmio()to common code, but there is zero reason to maintain two separate versions of the opcode cracking.
While that's true on the high level, all the low level details are different. We looked at unifying at some point, but it would have been a callback hell. I don't think unifying would make anything cleaner.
Besides the bulk of the decoding work is already unified in the common x86 instruction decoder. The actual actions are different, and the code fetching is also different, so on the rest there isn't that much to unify.
Quoted 2 lines
The existing SEV-ES #VC handlers appear to be missing page split checks, so that needs to be fixed.
Only if anyone in the kernel actually relies on it?
-Andi
Sean Christopherson
maintainerRe: [RFC v2-fix 1/1] x86/tdx: Handle in-kernel MMIO
Quoted 8 lines · Andi Kleen
Quoted 4 lines
Why does this code exist at all? TDX and SEV-ES absolutely must share code for handling MMIO reflection. It will require a fair amount of refactoring to move the guts of
vc_handle_mmio()to common code, but there is zero reason to maintain two separate versions of the opcode cracking.While that's true on the high level, all the low level details are different. We looked at unifying at some point, but it would have been a callback hell. I don't think unifying would make anything cleaner.
How hard did you look? The only part that _must_ be different between SEV and TDX is the hypercall itself, which is wholly contained at the very end of vc_do_mmio().
Despite vc_slow_virt_to_phys() taking a pointer to the ghcb, it's unused and thus the function is 100% generic.
The ghcb->shared_buffer usage throughout the upper levels can be eliminated by refactoring the stack to take a "u64 *val", since MMIO accesses are currently bounded to 8 bytes.
Quoted 3 lines
Besides the bulk of the decoding work is already unified in the common x86 instruction decoder. The actual actions are different, and the code fetching is also different
Huh? What do you mean by "actual actions"? Why is the code fetch different?
Re: [RFC v2-fix 1/1] x86/tdx: Handle in-kernel MMIO
Quoted 6 lines · Sean Christopherson
Quoted 5 lines · Andi Kleen
Quoted 4 lines
Why does this code exist at all? TDX and SEV-ES absolutely must share code for handling MMIO reflection. It will require a fair amount of refactoring to move the guts of
vc_handle_mmio()to common code, but there is zero reason to maintain two separate versions of the opcode cracking.
Quoted 7 lines
Quoted 3 lines
While that's true on the high level, all the low level details are different. We looked at unifying at some point, but it would have been a callback hell. I don't think unifying would make anything cleaner.
How hard did you look? The only part that _must_ be different between SEV and TDX is the hypercall itself, which is wholly contained at the very end of
vc_do_mmio().
I've come up with the code below. decode_mmio() can be shared with SEV.
I don't have a testing setup for AMD. I can do a blind patch, but it would be much more productive if someone on AMD side could look into this.
Any opinions?
enum mmio_type {
MMIO_DECODE_FAILED,
MMIO_WRITE,
MMIO_WRITE_IMM,
MMIO_READ,
MMIO_READ_ZERO_EXTEND,
MMIO_READ_SIGN_EXTEND,
MMIO_MOVS,
};
static enum mmio_type decode_mmio(struct insn *insn, struct pt_regs *regs,
int *bytes)
{
int type = MMIO_DECODE_FAILED;
*bytes = 0;
switch (insn->opcode.bytes[0]) {
case 0x88: /* MOV m8,r8 */
*bytes = 1;
fallthrough;
case 0x89: /* MOV m16/m32/m64, r16/m32/m64 */
if (!*bytes)
*bytes = insn->opnd_bytes;
type = MMIO_WRITE;
break;
case 0xc6: /* MOV m8, imm8 */
*bytes = 1;
fallthrough;
case 0xc7: /* MOV m16/m32/m64, imm16/imm32/imm64 */
if (!*bytes)
*bytes = insn->opnd_bytes;
type = MMIO_WRITE_IMM;
break;
case 0x8a: /* MOV r8, m8 */
*bytes = 1;
fallthrough;
case 0x8b: /* MOV r16/r32/r64, m16/m32/m64 */
if (!*bytes)
*bytes = insn->opnd_bytes;
type = MMIO_READ;
break;
case 0xa4: /* MOVS m8, m8 */
*bytes = 1;
fallthrough;
case 0xa5: /* MOVS m16/m32/m64, m16/m32/m64 */
if (!*bytes)
*bytes = insn->opnd_bytes;
type = MMIO_MOVS;
break;
case 0x0f: /* Two-byte instruction */
switch (insn->opcode.bytes[1]) {
case 0xb6: /* MOVZX r16/r32/r64, m8 */
*bytes = 1;
fallthrough;
case 0xb7: /* MOVZX r32/r64, m16 */
if (!*bytes)
*bytes = 2;
type = MMIO_READ_ZERO_EXTEND;
break;
case 0xbe: /* MOVSX r16/r32/r64, m8 */
*bytes = 1;
fallthrough;
case 0xbf: /* MOVSX r32/r64, m16 */
if (!*bytes)
*bytes = 2;
type = MMIO_READ_SIGN_EXTEND;
break;
}
break;
}
return type;
}static int tdg_handle_mmio(struct pt_regs *regs, struct ve_info *ve)
{
int size;
unsigned long *reg;
struct insn insn;
unsigned long val = 0;
kernel_insn_init(&insn, (void *) regs->ip, MAX_INSN_SIZE);
insn_get_length(&insn);
insn_get_opcode(&insn);
reg = get_reg_ptr(&insn, regs);
switch (decode_mmio(&insn, regs, &size)) {
case MMIO_WRITE:
memcpy(&val, reg, size);
tdg_mmio(size, true, ve->gpa, val);
break;
case MMIO_WRITE_IMM:
val = insn.immediate.value;
tdg_mmio(size, true, ve->gpa, val);
break;
case MMIO_READ:
val = tdg_mmio(size, false, ve->gpa, val);
/* Zero-extend for 32-bit operation */
if (size == 4)
*reg = 0;
memcpy(reg, &val, size);
break;
case MMIO_READ_ZERO_EXTEND:
val = tdg_mmio(size, false, ve->gpa, val);
/* Zero extend based on operand size */
memset(reg, 0, insn.opnd_bytes);
memcpy(reg, &val, size);
break;
case MMIO_READ_SIGN_EXTEND:
val = tdg_mmio(size, false, ve->gpa, val);
/* Sign extend based on operand size */
if (val & (size == 1 ? 0x80 : 0x8000))
memset(reg, 0xff, insn.opnd_bytes);
else
memset(reg, 0, insn.opnd_bytes);
memcpy(reg, &val, size);
break;
case MMIO_MOVS:
case MMIO_DECODE_FAILED:
force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *) ve->gla);
return 0;
}
return insn.length;
}Signature · 1 line
Kirill A. Shutemov
cover x86/tdx: Handle in-kernel MMIO
In reply to Dave HansenThis patchset addresses the review comments in the patch titled "x86/tdx: Handle in-kernel MMIO". Since it requires patch split, sending these together.
Changes since RFC v2-fix:
* Introduced "x86/sev-es: Abstract out MMIO instruction decoding" patch for sharing common code between TDX and SEV.
* Modified TDX MMIO code to utilize common shared functions.
* Modified commit log to reflect latest changes and to address review comments.
Changes since RFC v2:
* Fixed commit log as per Dave's review.
Kirill A. Shutemov (2):
x86/sev-es: Abstract out MMIO instruction decoding x86/tdx: Handle in-kernel MMIO
arch/x86/include/asm/insn-eval.h | 13 +++ arch/x86/kernel/sev.c | 171 ++++++++----------------------- arch/x86/kernel/tdx.c | 108 +++++++++++++++++++ arch/x86/lib/insn-eval.c | 102 ++++++++++++++++++ 4 files changed, 263 insertions(+), 131 deletions(-)
Signature · 1 line
2.25.1
RFC 1/2 x86/sev-es: Abstract out MMIO instruction decoding
From: "Kirill A. Shutemov" <[email protected]>
For regular virtual machine, MMIO is handled by the VMM: KVM emulates instruction that caused MMIO. But, this model doesn't work for a secure VMs (like SEV or TDX) as VMM doesn't have access to the guest memory and register state. VMM needs assistance in handling MMIO: it induces exception in the guest. Guest has to decode the instruction and handle it on its own.
Instruction decoding logic is similar between AMD SEV and TDX code. So extract the decoding code to insn-eval.c where it can be used by both SEV and TDX.
This code adds no functional changes. It is only build-tested for SEV.
--- arch/x86/include/asm/insn-eval.h | 13 +++ arch/x86/kernel/sev.c | 171 ++++++++----------------------- arch/x86/lib/insn-eval.c | 102 ++++++++++++++++++ 3 files changed, 155 insertions(+), 131 deletions(-)
arch/x86/include/asm/insn-eval.h +13 -0
@@ -19,6 +19,7 @@ bool insn_has_rep_prefix(struct insn *insn);void __user *insn_get_addr_ref (struct insn *insn,struct pt_regs *regs);int insn_get_modrm_rm_off (struct insn *insn,struct pt_regs *regs);int insn_get_modrm_reg_off (struct insn *insn,struct pt_regs *regs); +void *insn_get_modrm_reg_ptr (struct insn *insn,struct pt_regs *regs);unsigned long insn_get_seg_base (struct pt_regs *regs,int seg_reg_idx);int insn_get_code_seg_params (struct pt_regs *regs); int insn_fetch_from_user(struct pt_regs *regs,
@@ -28,4 +29,16 @@ int insn_fetch_from_user_inatomic(struct pt_regs *regs,bool insn_decode_from_regs (struct insn *insn,struct pt_regs *regs,unsigned char buf[MAX_INSN_SIZE ],int buf_size); +enum mmio_type { +MMIO_DECODE_FAILED , +MMIO_WRITE , +MMIO_WRITE_IMM , +MMIO_READ , +MMIO_READ_ZERO_EXTEND , +MMIO_READ_SIGN_EXTEND , +MMIO_MOVS , +}; + +enum mmio_type insn_decode_mmio (struct insn *insn,int *bytes); +#endif /* _ASM_X86_INSN_EVAL_H */
arch/x86/kernel/sev.c +40 -131
221 lines not on this page · raw message
arch/x86/lib/insn-eval.c +102 -0
111 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [RFC v2-fix-v2 1/2] x86/sev-es: Abstract out MMIO instruction decoding
Quoted 3 lines · Kuppuswamy Sathyanarayanan
From: "Kirill A. Shutemov" <[email protected]>
Lead in with the what because I read 2 paragraphs to figure out that this was a prep patch.
"In preparation for sharing MMIO instruction decode between SEV-ES and TDX factor out the common decode into a new insn_decode_mmio() helper."
Quoted 7 lines
For regular virtual machine, MMIO is handled by the VMM: KVM emulates instruction that caused MMIO. But, this model doesn't work for a secure VMs (like SEV or TDX) as VMM doesn't have access to the guest memory and register state. VMM needs assistance in handling MMIO: it induces exception in the guest. Guest has to decode the instruction and handle it on its own.
Quoted 6 lines
Instruction decoding logic is similar between AMD SEV and TDX code. So extract the decoding code to
insn-eval.cwhere it can be used by both SEV and TDX.This code adds no functional changes. It is only build-tested for SEV.
The diff is such that I could not verify "no functional change" change without doing more careful analysis. Typically with non-trivial refactoring they are split out over a few patches with a final removal of replaced infra at the end. This does the entire conversion all at once.
How about an approach that has vc_handle_mmio() handle MMIO_DECODE_FAILED for missing support in the common helper until the final patch that can do:
arch/x86/kernel/sev.c static enum es_result vc_handle_mmio_movs(struct es_em_ctxt *ctxt, · line 917
Quoted 3 lines
arch/x86/kernel/sev.c +3
-0+ mmio =insn_decode_mmio (insn, &bytes); +if (mmio ==MMIO_DECODE_FAILED ) +return ES_DECODE_FAILED ;
...i.e. insn_decode_mmio() is finally prepared to handle all scenarios and vc_handle_mmio_twobyte_ops() can finally be deleted. This also helps a future bisect that finds "whoops, 'no functional changes' was incorrect".
Quoted 4 lines
Signed-off-by Kirill A. Shutemov <[email protected]>Cc Tom Lendacky <[email protected]>Cc Joerg Roedel <[email protected]>
Missing Sathya signed-off-by...
cover x86/tdx: Handle in-kernel MMIO
This patchset addresses the review comments in the patch titled "x86/tdx: Handle in-kernel MMIO". Since it requires patch split, sending these together.
Changes since RFC-v2-fix-v2:
* Further divided patch "x86/sev-es: Abstract out MMIO instruction decoding", and moved generic APIs common to both TDX/SEV into seperate patches. Also introduced new patch for AMD code to utilize the generic APIs.
* Fixed commit log as per review comments.
Changes since RFC v2-fix:
* Introduced "x86/sev-es: Abstract out MMIO instruction decoding" patch for sharing common code between TDX and SEV.
* Modified TDX MMIO code to utilize common shared functions.
* Modified commit log to reflect latest changes and to address review comments.
Changes since RFC v2:
* Fixed commit log as per Dave's review.
Kirill A. Shutemov (4):
x86/insn-eval: Introduce insn_get_modrm_reg_ptr() x86/insn-eval: Introduce insn_decode_mmio() x86/sev-es: Use insn_decode_mmio() for MMIO implementation x86/tdx: Handle in-kernel MMIO
arch/x86/include/asm/insn-eval.h | 13 +++ arch/x86/kernel/sev.c | 171 ++++++++----------------------- arch/x86/kernel/tdx.c | 109 ++++++++++++++++++++ arch/x86/lib/insn-eval.c | 102 ++++++++++++++++++ 4 files changed, 264 insertions(+), 131 deletions(-)
Signature · 1 line
2.25.1
RFC 1/4 x86/insn-eval: Introduce insn_get_modrm_reg_ptr()
From: "Kirill A. Shutemov" <[email protected]>
The helper returns a pointer to the register indicated by ModRM byte.
It's going to replace vc_insn_get_reg() in the SEV MMIO implementation. TDX MMIO implementation will also use it.
--- arch/x86/include/asm/insn-eval.h | 1 + arch/x86/lib/insn-eval.c | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+)
arch/x86/include/asm/insn-eval.h +1 -0
@@ -19,6 +19,7 @@ bool insn_has_rep_prefix(struct insn *insn);void __user *insn_get_addr_ref (struct insn *insn,struct pt_regs *regs);int insn_get_modrm_rm_off (struct insn *insn,struct pt_regs *regs);int insn_get_modrm_reg_off (struct insn *insn,struct pt_regs *regs); +void *insn_get_modrm_reg_ptr (struct insn *insn,struct pt_regs *regs);unsigned long insn_get_seg_base (struct pt_regs *regs,int seg_reg_idx);int insn_get_code_seg_params (struct pt_regs *regs); int insn_fetch_from_user(struct pt_regs *regs,
arch/x86/lib/insn-eval.c +20 -0
@@ -850,6 +850,26 @@ int insn_get_modrm_reg_off(struct insn *insn, struct pt_regs *regs)return get_reg_offset (insn, regs,REG_TYPE_REG ); } +/** +* insn_get_modrm_reg_ptr() - Obtain register pointer based on ModRM byte +* @insn: Instruction containing the ModRM byte +* @regs: Register values as seen when entering kernel mode +* +* Returns: +* +* The register indicated by the reg part of the ModRM byte. +* The register is obtained as a pointer within pt_regs. +*/ +void *insn_get_modrm_reg_ptr (struct insn *insn,struct pt_regs *regs) +{ +int offset; + + offset =insn_get_modrm_reg_off (insn, regs); +if (offset <0 ) +return NULL ; +return (void *)regs + offset; +} +/** * get_seg_base_limit() - obtain base address and limit of a segment * @insn: Instruction. Must be valid.
Signature · 1 line
2.25.1
RFC 2/4 x86/insn-eval: Introduce insn_decode_mmio()
In reply to Kuppuswamy SathyanarayananFrom: "Kirill A. Shutemov" <[email protected]>
In preparation for sharing MMIO instruction decode between SEV-ES and TDX, factor out the common decode into a new insn_decode_mmio() helper.
For regular virtual machine, MMIO is handled by the VMM and KVM emulates instructions that caused MMIO. But, this model doesn't work for a secure VMs (like SEV or TDX) as VMM doesn't have access to the guest memory and register state. So, for TDX or SEV VMM needs assistance in handling MMIO. It induces exception in the guest. Guest has to decode the instruction and handle it on its own.
The code is based on the current SEV MMIO implementation.
--- arch/x86/include/asm/insn-eval.h | 12 +++++ arch/x86/lib/insn-eval.c | 82 ++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+)
arch/x86/include/asm/insn-eval.h +12 -0
@@ -29,4 +29,16 @@ int insn_fetch_from_user_inatomic(struct pt_regs *regs,bool insn_decode_from_regs (struct insn *insn,struct pt_regs *regs,unsigned char buf[MAX_INSN_SIZE ],int buf_size); +enum mmio_type { +MMIO_DECODE_FAILED , +MMIO_WRITE , +MMIO_WRITE_IMM , +MMIO_READ , +MMIO_READ_ZERO_EXTEND , +MMIO_READ_SIGN_EXTEND , +MMIO_MOVS , +}; + +enum mmio_type insn_decode_mmio (struct insn *insn,int *bytes); +#endif /* _ASM_X86_INSN_EVAL_H */
arch/x86/lib/insn-eval.c +82 -0
85 lines not on this page · raw message
Signature · 1 line
2.25.1
RFC 3/4 x86/sev-es: Use insn_decode_mmio() for MMIO implementation
In reply to Kuppuswamy SathyanarayananFrom: "Kirill A. Shutemov" <[email protected]>
Switch SEV implementation to insn_decode_mmio(). The helper is going to be used by TDX too.
No functional changes. It is only build-tested.
--- arch/x86/kernel/sev.c | 171 ++++++++++-------------------------------- 1 file changed, 40 insertions(+), 131 deletions(-)
arch/x86/kernel/sev.c +40 -131
@@ -790,22 +790,6 @@ static void __init vc_early_forward_exception(struct es_em_ctxt *ctxt)do_early_exception (ctxt->regs, trapnr); } -static long *vc_insn_get_reg (struct es_em_ctxt *ctxt) -{ -long *reg_array; -int offset; - - reg_array = (long *)ctxt->regs; - offset =insn_get_modrm_reg_off (&ctxt->insn, ctxt->regs); - -if (offset <0 ) -return NULL ; - - offset /=sizeof (long ); - -return reg_array + offset; -} -static long *vc_insn_get_rm (struct es_em_ctxt *ctxt) {long *reg_array;
@@ -853,76 +837,6 @@ static enum es_result vc_do_mmio(struct ghcb *ghcb, struct es_em_ctxt *ctxt,return sev_es_ghcb_hv_call (ghcb, ctxt, exit_code, exit_info_1, exit_info_2); } -static enum es_result vc_handle_mmio_twobyte_ops (struct ghcb *ghcb, -struct es_em_ctxt *ctxt) -{ -struct insn *insn = &ctxt->insn; -unsigned int bytes =0 ; -enum es_result ret; -int sign_byte; -long *reg_data; - -switch (insn->opcode.bytes[1 ]) { -/* MMIO Read w/ zero-extension */ -case 0xb6 : - bytes =1 ; - fallthrough; -case 0xb7 : -if (!bytes) - bytes =2 ; - - ret =vc_do_mmio (ghcb, ctxt, bytes, true); -if (ret) -break ; - -/* Zero extend based on operand size */ - reg_data =vc_insn_get_reg (ctxt); -if (!reg_data) -return ES_DECODE_FAILED ; - -memset (reg_data,0 , insn->opnd_bytes); - -memcpy (reg_data, ghcb->shared_buffer, bytes); -break ; - -/* MMIO Read w/ sign-extension */ -case 0xbe : - bytes =1 ; - fallthrough; -case 0xbf : -if (!bytes) - bytes =2 ; - - ret =vc_do_mmio (ghcb, ctxt, bytes, true); -if (ret) -break ; - -/* Sign extend based on operand size */ - reg_data =vc_insn_get_reg (ctxt); -if (!reg_data) -return ES_DECODE_FAILED ; - -if (bytes ==1 ) { -u8 *val = (u8 *)ghcb->shared_buffer; - - sign_byte = (*val &0x80 ) ?0xff :0x00 ; - }else { -u16 *val = (u16 *)ghcb->shared_buffer; - - sign_byte = (*val &0x8000 ) ?0xff :0x00 ; - } -memset (reg_data, sign_byte, insn->opnd_bytes); - -memcpy (reg_data, ghcb->shared_buffer, bytes); -break ; - -default : - ret =ES_UNSUPPORTED ; - } - -return ret; -} -/* * The MOVS instruction has two memory operands, which raises the * problem that it is not known whether the access to the source or the
@@ -990,83 +904,78 @@ static enum es_result vc_handle_mmio_movs(struct es_em_ctxt *ctxt,return ES_RETRY ; } -static enum es_result vc_handle_mmio (struct ghcb *ghcb, -struct es_em_ctxt *ctxt) +static enum es_result vc_handle_mmio (struct ghcb *ghcb,struct es_em_ctxt *ctxt) {struct insn *insn = &ctxt->insn;unsigned int bytes =0 ; +enum mmio_type mmio;enum es_result ret; +u8 sign_byte;long *reg_data; -switch (insn->opcode.bytes[0 ]) { -/* MMIO Write */ -case 0x88 : - bytes =1 ; - fallthrough; -case 0x89 : -if (!bytes) - bytes = insn->opnd_bytes; + mmio =insn_decode_mmio (insn, &bytes); +if (mmio ==MMIO_DECODE_FAILED ) +return ES_DECODE_FAILED ; - reg_data =vc_insn_get_reg (ctxt); +if (mmio !=MMIO_WRITE_IMM && mmio !=MMIO_MOVS ) { + reg_data =insn_get_modrm_reg_ptr (insn, ctxt->regs);if (!reg_data)return ES_DECODE_FAILED ; + } +switch (mmio) { +case MMIO_WRITE :memcpy (ghcb->shared_buffer, reg_data, bytes); - ret =vc_do_mmio (ghcb, ctxt, bytes, false);break ; - -case 0xc6 : - bytes =1 ; - fallthrough; -case 0xc7 : -if (!bytes) - bytes = insn->opnd_bytes; - +case MMIO_WRITE_IMM :memcpy (ghcb->shared_buffer, insn->immediate1.bytes, bytes); - ret =vc_do_mmio (ghcb, ctxt, bytes, false);break ; - -/* MMIO Read */ -case 0x8a : - bytes =1 ; - fallthrough; -case 0x8b : -if (!bytes) - bytes = insn->opnd_bytes; - +case MMIO_READ : ret =vc_do_mmio (ghcb, ctxt, bytes, true);if (ret)break ; - reg_data =vc_insn_get_reg (ctxt); -if (!reg_data) -return ES_DECODE_FAILED ; -/* Zero-extend for 32-bit operation */ if (bytes ==4 ) *reg_data =0 ;memcpy (reg_data, ghcb->shared_buffer, bytes);break ; +case MMIO_READ_ZERO_EXTEND : + ret =vc_do_mmio (ghcb, ctxt, bytes, true); +if (ret) +break ; + +memset (reg_data,0 , insn->opnd_bytes); +memcpy (reg_data, ghcb->shared_buffer, bytes); +break ; +case MMIO_READ_SIGN_EXTEND : + ret =vc_do_mmio (ghcb, ctxt, bytes, true); +if (ret) +break ; -/* MOVS instruction */ -case 0xa4 : - bytes =1 ; - fallthrough; -case 0xa5 : -if (!bytes) - bytes = insn->opnd_bytes; +if (bytes ==1 ) { +u8 *val = (u8 *)ghcb->shared_buffer; - ret =vc_handle_mmio_movs (ctxt, bytes); + sign_byte = (*val &0x80 ) ?0xff :0x00 ; + }else { +u16 *val = (u16 *)ghcb->shared_buffer; + + sign_byte = (*val &0x8000 ) ?0xff :0x00 ; + } + +/* Sign extend based on operand size */ +memset (reg_data, sign_byte, insn->opnd_bytes); +memcpy (reg_data, ghcb->shared_buffer, bytes);break ; -/* Two-Byte Opcodes */ -case 0x0f : - ret =vc_handle_mmio_twobyte_ops (ghcb, ctxt); +case MMIO_MOVS : + ret =vc_handle_mmio_movs (ctxt, bytes);break ;default : ret =ES_UNSUPPORTED ; +break ; }return ret;
Signature · 1 line
2.25.1
RFC 4/4 x86/tdx: Handle in-kernel MMIO
In reply to Kuppuswamy SathyanarayananFrom: "Kirill A. Shutemov" <[email protected]>
In traditional VMs, MMIO is usually implemented by giving a guest access to a mapping which will cause a VMEXIT on access and then the VMM emulating the access. That's not possible in TDX guest because VMEXIT will expose the register state to the host. TDX guests don't trust the host and can't have its state exposed to the host. In TDX the MMIO regions are instead configured to trigger a #VE exception in the guest. The guest #VE handler then emulates the MMIO instruction inside the guest and converts them into a controlled TDCALL to the host, rather than completely exposing the state to the host.
Currently, we only support MMIO for instructions that are known to come from io.h macros (build_mmio_read/write()). For drivers that don't use the io.h macros or uses structure overlay to do MMIO are currently not supported in TDX guest (for example the MMIO based XAPIC is disable at runtime for TDX).
This way of handling is similar to AMD SEV.
Also, reasons for supporting #VE based MMIO in TDX guest are,
* MMIO is widely used and we'll have more drivers in the future.
* We don't want to annotate every TDX specific MMIO readl/writel etc.
* If we didn't annotate we would need to add an alternative to every MMIO access in the kernel (even though 99.9% will never be used on TDX) which would be a complete waste and incredible binary bloat for nothing.
--- arch/x86/kernel/tdx.c | 109 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+)
arch/x86/kernel/tdx.c +109 -0
@@ -5,6 +5,9 @@#include <asm/tdx.h> #include <asm/vmx.h> +#include <asm/insn.h> +#include <asm/insn-eval.h> +#include <linux/sched/signal.h> /* force_sig_fault() */ #include <linux/cpu.h> #include <linux/protected_guest.h>
@@ -226,6 +229,104 @@ static void tdg_handle_io(struct pt_regs *regs, u32 exit_qual) } } +static unsigned long tdg_mmio (int size,bool write,unsigned long addr, +unsigned long *val) +{ +struct tdx_hypercall_output out = {0 }; +u64 err; + + err =__tdx_hypercall (EXIT_REASON_EPT_VIOLATION , size, write, + addr, *val, &out); + *val = out.r11; +return err; +} + +static int tdg_handle_mmio (struct pt_regs *regs,struct ve_info *ve) +{ +struct insn insn = {}; +char buffer[MAX_INSN_SIZE ]; +enum mmio_type mmio; +unsigned long *reg; +int size, ret; +u8 sign_byte; +unsigned long val; + +if (user_mode (regs)) { + ret =insn_fetch_from_user (regs, buffer); +if (!ret) +return -EFAULT ; +if (!insn_decode_from_regs (&insn, regs, buffer, ret)) +return -EFAULT ; + }else { + ret =copy_from_kernel_nofault (buffer, (void *)regs->ip, +MAX_INSN_SIZE ); +if (ret) +return -EFAULT ; +insn_init (&insn, buffer,MAX_INSN_SIZE ,1 ); +insn_get_length (&insn); + } + + mmio =insn_decode_mmio (&insn, &size); +if (mmio ==MMIO_DECODE_FAILED ) +return -EFAULT ; + +if (mmio !=MMIO_WRITE_IMM && mmio !=MMIO_MOVS ) { + reg =insn_get_modrm_reg_ptr (&insn, regs); +if (!reg) +return -EFAULT ; + } + +switch (mmio) { +case MMIO_WRITE : +memcpy (&val, reg, size); + ret =tdg_mmio (size, true, ve->gpa, &val); +break ; +case MMIO_WRITE_IMM : + val = insn.immediate.value; + ret =tdg_mmio (size, true, ve->gpa, &val); +break ; +case MMIO_READ : + ret =tdg_mmio (size, false, ve->gpa, &val); +if (ret) +break ; +/* Zero-extend for 32-bit operation */ +if (size ==4 ) + *reg =0 ; +memcpy (reg, &val, size); +break ; +case MMIO_READ_ZERO_EXTEND : + ret =tdg_mmio (size, false, ve->gpa, &val); +if (ret) +break ; + +/* Zero extend based on operand size */ +memset (reg,0 , insn.opnd_bytes); +memcpy (reg, &val, size); +break ; +case MMIO_READ_SIGN_EXTEND : + ret =tdg_mmio (size, false, ve->gpa, &val); +if (ret) +break ; + +if (size ==1 ) + sign_byte = (val &0x80 ) ?0xff :0x00 ; +else + sign_byte = (val &0x8000 ) ?0xff :0x00 ; + +/* Sign extend based on operand size */ +memset (reg, sign_byte, insn.opnd_bytes); +memcpy (reg, &val, size); +break ; +case MMIO_MOVS : +case MMIO_DECODE_FAILED : +return -EFAULT ; + } + +if (ret) +return -EFAULT ; +return insn.length; +} +unsigned long tdg_get_ve_info (struct ve_info *ve) {u64 ret;
@@ -275,6 +376,14 @@ int tdg_handle_virtualization_exception(struct pt_regs *regs,case EXIT_REASON_IO_INSTRUCTION :tdg_handle_io (regs, ve->exit_qual);break ; +case EXIT_REASON_EPT_VIOLATION : +/* Currently only MMIO triggers EPT violation */ + ve->instr_len =tdg_handle_mmio (regs, ve); +if (ve->instr_len <0 ) { +pr_warn_once ("MMIO failed\n" ); +return -EFAULT ; + } +break ;default :pr_warn ("Unexpected #VE: %lld\n" , ve->exit_reason);return -EFAULT ;
Signature · 1 line
2.25.1
RFC 2/2 x86/tdx: Handle in-kernel MMIO
In reply to Kuppuswamy SathyanarayananFrom: "Kirill A. Shutemov" <[email protected]>
In traditional VMs, MMIO is usually implemented by giving a guest access to a mapping which will cause a VMEXIT on access and then the VMM emulating the access. That's not possible in TDX guest because VMEXIT will expose the register state to the host. TDX guests don't trust the host and can't have its state exposed to the host. In TDX the MMIO regions are instead configured to trigger a #VE exception in the guest. The guest #VE handler then emulates the MMIO instruction inside the guest and converts them into a controlled TDCALL to the host, rather than completely exposing the state to the host.
Currently, we only support MMIO for instructions that are known to come from io.h macros (build_mmio_read/write()). For drivers that don't use the io.h macros or uses structure overlay to do MMIO are currently not supported in TDX guest (for example the MMIO based XAPIC is disable at runtime for TDX). User-space access triggers SIGBUS.
This way of handling is similar to AMD SEV.
Also, reasons for supporting #VE based MMIO in TDX guest are,
* MMIO is widely used and we'll have more drivers in the future.
* We don't want to annotate every TDX specific MMIO readl/writel etc.
* If we didn't annotate we would need to add an alternative to every MMIO access in the kernel (even though 99.9% will never be used on TDX) which would be a complete waste and incredible binary bloat for nothing.
--- arch/x86/kernel/tdx.c | 108 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+)
arch/x86/kernel/tdx.c +108 -0
@@ -5,6 +5,9 @@#include <asm/tdx.h> #include <asm/vmx.h> +#include <asm/insn.h> +#include <asm/insn-eval.h> +#include <linux/sched/signal.h> /* force_sig_fault() */ #include <linux/cpu.h> #include <linux/protected_guest.h>
@@ -236,6 +239,104 @@ static void tdg_handle_io(struct pt_regs *regs, u32 exit_qual) } } +static unsigned long tdg_mmio (int size,bool write,unsigned long addr, +unsigned long *val) +{ +struct tdx_hypercall_output out = {0 }; +u64 err; + + err =__tdx_hypercall (EXIT_REASON_EPT_VIOLATION , size, write, + addr, *val, &out); + *val = out.r11; +return err; +} + +static int tdg_handle_mmio (struct pt_regs *regs,struct ve_info *ve) +{ +struct insn insn = {}; +char buffer[MAX_INSN_SIZE ]; +enum mmio_type mmio; +unsigned long *reg; +int size, ret; +u8 sign_byte; +unsigned long val; + +if (user_mode (regs)) { + ret =insn_fetch_from_user (regs, buffer); +if (!ret) +return -EFAULT ; +if (!insn_decode_from_regs (&insn, regs, buffer, ret)) +return -EFAULT ; + }else { + ret =copy_from_kernel_nofault (buffer, (void *)regs->ip, +MAX_INSN_SIZE ); +if (ret) +return -EFAULT ; +insn_init (&insn, buffer,MAX_INSN_SIZE ,1 ); +insn_get_length (&insn); + } + + mmio =insn_decode_mmio (&insn, &size); +if (mmio ==MMIO_DECODE_FAILED ) +return -EFAULT ; + +if (mmio !=MMIO_WRITE_IMM && mmio !=MMIO_MOVS ) { + reg =insn_get_modrm_reg_ptr (&insn, regs); +if (!reg) +return -EFAULT ; + } + +switch (mmio) { +case MMIO_WRITE : +memcpy (&val, reg, size); + ret =tdg_mmio (size, true, ve->gpa, &val); +break ; +case MMIO_WRITE_IMM : + val = insn.immediate.value; + ret =tdg_mmio (size, true, ve->gpa, &val); +break ; +case MMIO_READ : + ret =tdg_mmio (size, false, ve->gpa, &val); +if (ret) +break ; +/* Zero-extend for 32-bit operation */ +if (size ==4 ) + *reg =0 ; +memcpy (reg, &val, size); +break ; +case MMIO_READ_ZERO_EXTEND : + ret =tdg_mmio (size, false, ve->gpa, &val); +if (ret) +break ; + +/* Zero extend based on operand size */ +memset (reg,0 , insn.opnd_bytes); +memcpy (reg, &val, size); +break ; +case MMIO_READ_SIGN_EXTEND : + ret =tdg_mmio (size, false, ve->gpa, &val); +if (ret) +break ; + +if (size ==1 ) + sign_byte = (val &0x80 ) ?0xff :0x00 ; +else + sign_byte = (val &0x8000 ) ?0xff :0x00 ; + +/* Sign extend based on operand size */ +memset (reg, sign_byte, insn.opnd_bytes); +memcpy (reg, &val, size); +break ; +case MMIO_MOVS : +case MMIO_DECODE_FAILED : +return -EFAULT ; + } + +if (ret) +return -EFAULT ; +return insn.length; +} +unsigned long tdg_get_ve_info (struct ve_info *ve) {u64 ret;
@@ -285,6 +386,13 @@ int tdg_handle_virtualization_exception(struct pt_regs *regs,case EXIT_REASON_IO_INSTRUCTION :tdg_handle_io (regs, ve->exit_qual);break ; +case EXIT_REASON_EPT_VIOLATION : + ve->instr_len =tdg_handle_mmio (regs, ve); +if (ve->instr_len <0 ) { +pr_warn_once ("MMIO failed\n" ); +return -EFAULT ; + } +break ;default :pr_warn ("Unexpected #VE: %lld\n" , ve->exit_reason);return -EFAULT ;
Signature · 1 line
2.25.1
Re: [RFC v2-fix-v2 2/2] x86/tdx: Handle in-kernel MMIO
User-space
Quoted 1 line
access triggers SIGBUS.
Actually it looks like it's implemented below now, so that sentence could be dropped.
Quoted 16 lines
arch/x86/kernel/tdx.c +16
-0+ +if (user_mode (regs)) { + ret =insn_fetch_from_user (regs, buffer); +if (!ret) +return -EFAULT ; +if (!insn_decode_from_regs (&insn, regs, buffer, ret)) +return -EFAULT ; + }else { + ret =copy_from_kernel_nofault (buffer, (void *)regs->ip, +MAX_INSN_SIZE ); +if (ret) +return -EFAULT ; +insn_init (&insn, buffer,MAX_INSN_SIZE ,1 ); +insn_get_length (&insn); + } +
Re: [RFC v2-fix-v2 2/2] x86/tdx: Handle in-kernel MMIO
Quoted 1 line · Andi Kleen
Actually it looks like it's implemented below now, so that sentence could be dropped.
Will fix it in next version.
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
RFC v2 16/32 x86/tdx: Handle MWAIT, MONITOR and WBINVD
In reply to Kuppuswamy SathyanarayananWhen running as a TDX guest, there are a number of existing, privileged instructions that do not work. If the guest kernel uses these instructions, the hardware generates a #VE.
You can find the list of unsupported instructions in Intel Trust Domain Extensions (Intel® TDX) Module specification, sec 9.2.2 and in Guest-Host Communication Interface (GHCI) Specification for Intel TDX, sec 2.4.1.
To prevent TD guest from using MWAIT/MONITOR instructions, support for these instructions are already disabled by TDX module (SEAM). So CPUID flags for these instructions should be in disabled state.
After the above mentioned preventive measures, if TD guests still execute these instructions, add appropriate warning messages in #VE handler. For WBIND instruction, since it's related to memory writeback and cache flushes, it's mainly used in context of IO devices. Since TDX 1.0 does not support non-virtual I/O devices, skipping it should not cause any fatal issues. But to let users know about its usage, use WARN() to report about it.. For MWAIT/MONITOR instruction, since its unsupported use WARN() to report unsupported usage.
--- arch/x86/kernel/tdx.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
arch/x86/kernel/tdx.c +15 -0
@@ -371,6 +371,21 @@ int tdg_handle_virtualization_exception(struct pt_regs *regs,case EXIT_REASON_EPT_VIOLATION : ve->instr_len =tdg_handle_mmio (regs, ve);break ; +case EXIT_REASON_WBINVD : +/* +* WBINVD is not supported inside TDX guests. All in- +* kernel uses should have been disabled. +*/ +WARN_ONCE (1 ,"TD Guest used unsupported WBINVD instruction\n" ); +break ; +case EXIT_REASON_MONITOR_INSTRUCTION : +case EXIT_REASON_MWAIT_INSTRUCTION : +/* +* Something in the kernel used MONITOR or MWAIT despite +* X86_FEATURE_MWAIT being cleared for TDX guests. +*/ +WARN_ONCE (1 ,"TD Guest used unsupported MWAIT/MONITOR instruction\n" ); +break ;default :pr_warn ("Unexpected #VE: %lld\n" , ve->exit_reason);return -EFAULT ;
Signature · 1 line
2.25.1
Re: [RFC v2 16/32] x86/tdx: Handle MWAIT, MONITOR and WBINVD
Quoted 10 lines · Kuppuswamy Sathyanarayanan
When running as a TDX guest, there are a number of existing, privileged instructions that do not work. If the guest kernel uses these instructions, the hardware generates a #VE.
You can find the list of unsupported instructions in Intel Trust Domain Extensions (Intel® TDX) Module specification, sec 9.2.2 and in Guest-Host Communication Interface (GHCI) Specification for Intel TDX, sec 2.4.1.
Ah, better than the "handle port io" patch, these details at least give the reader a chance.
Quoted 4 lines
To prevent TD guest from using MWAIT/MONITOR instructions, support for these instructions are already disabled by TDX module (SEAM). So CPUID flags for these instructions should be in disabled state.
Why does this not result in a #UD if the instruction is disabled by SEAM? How is it possible to execute a disabled instruction (one precluded by CPUID) to the point where it triggers #VE instead of #UD?
Quoted 6 lines
After the above mentioned preventive measures, if TD guests still execute these instructions, add appropriate warning messages in #VE handler. For WBIND instruction, since it's related to memory writeback and cache flushes, it's mainly used in context of IO devices. Since TDX 1.0 does not support non-virtual I/O devices, skipping it should not cause any fatal issues.
WBINVD is in a different class than MWAIT/MONITOR since it is not identified by CPUID, it can't possibly have the same #UD behaviour. It's not clear why WBINVD is included in the same patch as MWAIT/MONITOR?
I disagree with the assertion that WBINVD is mainly used in the context of I/O devices, it's also used for ACPI power management paths. WBINVD dependent functionality should be dynamically disabled rather than warned about.
Does a TDX guest support out-of-tree modules? The kernel is already tainted when out-of-tree modules are loaded. In other words in-tree modules preclude forbidden instructions because they can just be audited, and out-of-tree modules are ok to trigger abrupt failure if they attempt to use forbidden instructions.
Quoted 3 lines
But to let users know about its usage, use
WARN()to report about it.. For MWAIT/MONITOR instruction, since its unsupported useWARN()to report unsupported usage.
I'm not sure how useful warning is outside of a kernel developer's debug environment. The kernel should know what instructions are disabled and which are available. WBINVD in particular has potential data integrity implications. Code that might lead to a WBINVD usage should be disabled, not run all the way up to where WBINVD is attempted and then trigger an after-the-fact WARN_ONCE().
The WBINVD change deserves to be split off from MWAIT/MONITOR, and more thought needs to be put into where these spurious instruction usages are arising.
Quoted 36 lines
Signed-off-by Kuppuswamy Sathyanarayanan <[email protected]>Reviewed-by Andi Kleen <[email protected]>--- arch/x86/kernel/tdx.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)arch/x86/kernel/tdx.c +15
-1@@ -371,6 +371,21 @@ int tdg_handle_virtualization_exception(struct pt_regs *regs,case EXIT_REASON_EPT_VIOLATION : ve->instr_len =tdg_handle_mmio (regs, ve);break ; +case EXIT_REASON_WBINVD : +/* +* WBINVD is not supported inside TDX guests. All in- +* kernel uses should have been disabled. +*/ +WARN_ONCE (1 ,"TD Guest used unsupported WBINVD instruction\n" ); +break ; +case EXIT_REASON_MONITOR_INSTRUCTION : +case EXIT_REASON_MWAIT_INSTRUCTION : +/* +* Something in the kernel used MONITOR or MWAIT despite +* X86_FEATURE_MWAIT being cleared for TDX guests. +*/ +WARN_ONCE (1 ,"TD Guest used unsupported MWAIT/MONITOR instruction\n" ); +break ;default :pr_warn ("Unexpected #VE: %lld\n" , ve->exit_reason);return -EFAULT ; --2.25.1
Re: [RFC v2 16/32] x86/tdx: Handle MWAIT, MONITOR and WBINVD
Quoted 6 lines
Quoted 4 lines
To prevent TD guest from using MWAIT/MONITOR instructions, support for these instructions are already disabled by TDX module (SEAM). So CPUID flags for these instructions should be in disabled state.
Why does this not result in a #UD if the instruction is disabled by SEAM?
It's just the TDX module (SEAM is the execution mode used by the TDX module)
Quoted 2 lines
How is it possible to execute a disabled instruction (one precluded by CPUID) to the point where it triggers #VE instead of #UD?
That's how the TDX module works. It never injects anything else other than #VE. You can still get other exceptions of course, but they won't come from the TDX module.
Quoted 10 lines
Quoted 6 lines
After the above mentioned preventive measures, if TD guests still execute these instructions, add appropriate warning messages in #VE handler. For WBIND instruction, since it's related to memory writeback and cache flushes, it's mainly used in context of IO devices. Since TDX 1.0 does not support non-virtual I/O devices, skipping it should not cause any fatal issues.
WBINVD is in a different class than MWAIT/MONITOR since it is not identified by CPUID, it can't possibly have the same #UD behaviour. It's not clear why WBINVD is included in the same patch as MWAIT/MONITOR?
Because these are all instructions we never expect to execute, so nothing special is needed for them. That's a unique class that logically fits together.
Quoted 4 lines
I disagree with the assertion that WBINVD is mainly used in the context of I/O devices, it's also used for ACPI power management paths.
You mean S3? That's of course also not supported inside TDX.
Quoted 8 lines
WBINVD dependent functionality should be dynamically disabled
rather than warned about.Does a TDX guest support out-of-tree modules? The kernel is already tainted when out-of-tree modules are loaded. In other words in-tree modules preclude forbidden instructions because they can just be audited, and out-of-tree modules are ok to trigger abrupt failure if they attempt to use forbidden instructions.
We already did a lot of bi^wdiscussion on this on the last review.
Originally we had a different handling, this was the result of previous feedback.
It doesn't really matter because it should never happen.
Quoted 10 lines
Quoted 4 lines
But to let users know about its usage, use
WARN()to report about it.. For MWAIT/MONITOR instruction, since its unsupported useWARN()to report unsupported usage.I'm not sure how useful warning is outside of a kernel developer's debug environment. The kernel should know what instructions are disabled and which are available. WBINVD in particular has potential data integrity implications. Code that might lead to a WBINVD usage should be disabled, not run all the way up to where WBINVD is attempted and then trigger an after-the-fact
WARN_ONCE().
We don't expect the warning to ever happen. Yes all of this will be disabled. Nearly all are in code paths that cannot happen inside TDX anyways due to missing PCI-IDs or different cpuids, and S3 is explicitly disabled and would be impossible anyways due to lack of BIOS support.
Quoted 4 lines
The WBINVD change deserves to be split off from MWAIT/MONITOR, and more thought needs to be put into where these spurious instruction usages are arising.
I disagree. We already spent a lot of cycles on this. WBINVD makes never sense in current TDX and all the code will be disabled.
-Andi
Re: [RFC v2 16/32] x86/tdx: Handle MWAIT, MONITOR and WBINVD
Quoted 8 lines · Andi Kleen
Quoted 6 lines
Quoted 4 lines
To prevent TD guest from using MWAIT/MONITOR instructions, support for these instructions are already disabled by TDX module (SEAM). So CPUID flags for these instructions should be in disabled state.
Why does this not result in a #UD if the instruction is disabled by SEAM?
It's just the TDX module (SEAM is the execution mode used by the TDX module)
If it is disabled by the TDX Module, we should never execute it. But for some reason, if we still come across this instruction (buggy TDX module?), we add appropriate warning in #VE handler.
Quoted 13 lines
Quoted 4 lines
How is it possible to execute a disabled instruction (one precluded by CPUID) to the point where it triggers #VE instead of #UD?
That's how the TDX module works. It never injects anything else other than #VE. You can still get other exceptions of course, but they won't come from the TDX module.
Quoted 6 lines
Quoted 6 lines
After the above mentioned preventive measures, if TD guests still execute these instructions, add appropriate warning messages in #VE handler. For WBIND instruction, since it's related to memory writeback and cache flushes, it's mainly used in context of IO devices. Since TDX 1.0 does not support non-virtual I/O devices, skipping it should not cause any fatal issues.
Quoted 6 lines
Quoted 4 lines
WBINVD is in a different class than MWAIT/MONITOR since it is not identified by CPUID, it can't possibly have the same #UD behaviour. It's not clear why WBINVD is included in the same patch as MWAIT/MONITOR?
Because these are all instructions we never expect to execute, so nothing special is needed for them. That's a unique class that logically fits together.
Yes, for all these three instruction we don't need any special handling code. So they are grouped together.
Quoted 30 lines
Quoted 6 lines
I disagree with the assertion that WBINVD is mainly used in the context of I/O devices, it's also used for ACPI power management paths.
You mean S3? That's of course also not supported inside TDX.
Quoted 8 lines
WBINVD dependent functionality should be dynamically disabled rather than warned about.
Does a TDX guest support out-of-tree modules? The kernel is already tainted when out-of-tree modules are loaded. In other words in-tree modules preclude forbidden instructions because they can just be audited, and out-of-tree modules are ok to trigger abrupt failure if they attempt to use forbidden instructions.
We already did a lot of bi^wdiscussion on this on the last review.
Originally we had a different handling, this was the result of previous feedback.
It doesn't really matter because it should never happen.
Quoted 4 lines
Quoted 4 lines
But to let users know about its usage, use
WARN()to report about it.. For MWAIT/MONITOR instruction, since its unsupported useWARN()to report unsupported usage.
Quoted 8 lines
Quoted 6 lines
I'm not sure how useful warning is outside of a kernel developer's debug environment. The kernel should know what instructions are disabled and which are available. WBINVD in particular has potential data integrity implications. Code that might lead to a WBINVD usage should be disabled, not run all the way up to where WBINVD is attempted and then trigger an after-the-fact
WARN_ONCE().We don't expect the warning to ever happen. Yes all of this will be disabled. Nearly all are in code paths that cannot happen inside TDX anyways due to missing PCI-IDs or different cpuids, and S3 is explicitly disabled and would be impossible anyways due to lack of BIOS support.
We have added WARN to let user know about its usage and fix it. By default we should never hit this path.
Quoted 15 lines
Quoted 8 lines
The WBINVD change deserves to be split off from MWAIT/MONITOR, and more thought needs to be put into where these spurious instruction usages are arising.
I disagree. We already spent a lot of cycles on this. WBINVD makes never sense in current TDX and all the code will be disabled.
-Andi
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Re: [RFC v2 16/32] x86/tdx: Handle MWAIT, MONITOR and WBINVD
Quoted 7 lines · Sathyanarayanan
Quoted 4 lines · Andi Kleen
Quoted 4 lines
Quoted 4 lines
To prevent TD guest from using MWAIT/MONITOR instructions, support for these instructions are already disabled by TDX module (SEAM). So CPUID flags for these instructions should be in disabled state.
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
Why does this not result in a #UD if the instruction is disabled by SEAM?
It's just the TDX module (SEAM is the execution mode used by the TDX module)
If it is disabled by the TDX Module, we should never execute it. But for some reason, if we still come across this instruction (buggy TDX module?), we add appropriate warning in #VE handler.
I think the only case where it could happen is if the kernel jumps to a random address due to a bug and the destination happens to be these instruction bytes. Of course it is exceedingly unlikely.
Or we make some mistake, but that's hopefully fixed quickly.
-Andi
Re: [RFC v2 16/32] x86/tdx: Handle MWAIT, MONITOR and WBINVD
In reply to Andi KleenQuoted 79 lines · Andi Kleen
Quoted 7 lines
Quoted 5 lines
To prevent TD guest from using MWAIT/MONITOR instructions, support for these instructions are already disabled by TDX module (SEAM). So CPUID flags for these instructions should be in disabled state.
Why does this not result in a #UD if the instruction is disabled by SEAM?
It's just the TDX module (SEAM is the execution mode used by the TDX module)
Quoted 2 lines
How is it possible to execute a disabled instruction (one precluded by CPUID) to the point where it triggers #VE instead of #UD?
That's how the TDX module works. It never injects anything else other than #VE. You can still get other exceptions of course, but they won't come from the TDX module.
Quoted 10 lines
Quoted 6 lines
After the above mentioned preventive measures, if TD guests still execute these instructions, add appropriate warning messages in #VE handler. For WBIND instruction, since it's related to memory writeback and cache flushes, it's mainly used in context of IO devices. Since TDX 1.0 does not support non-virtual I/O devices, skipping it should not cause any fatal issues.
WBINVD is in a different class than MWAIT/MONITOR since it is not identified by CPUID, it can't possibly have the same #UD behaviour. It's not clear why WBINVD is included in the same patch as MWAIT/MONITOR?
Because these are all instructions we never expect to execute, so nothing special is needed for them. That's a unique class that logically fits together.
Quoted 4 lines
I disagree with the assertion that WBINVD is mainly used in the context of I/O devices, it's also used for ACPI power management paths.
You mean S3? That's of course also not supported inside TDX.
Quoted 8 lines
WBINVD dependent functionality should be dynamically disabled
rather than warned about.Does a TDX guest support out-of-tree modules? The kernel is already tainted when out-of-tree modules are loaded. In other words in-tree modules preclude forbidden instructions because they can just be audited, and out-of-tree modules are ok to trigger abrupt failure if they attempt to use forbidden instructions.
We already did a lot of bi^wdiscussion on this on the last review.
Originally we had a different handling, this was the result of previous feedback.
It doesn't really matter because it should never happen.
Quoted 10 lines
Quoted 4 lines
But to let users know about its usage, use
WARN()to report about it.. For MWAIT/MONITOR instruction, since its unsupported useWARN()to report unsupported usage.I'm not sure how useful warning is outside of a kernel developer's debug environment. The kernel should know what instructions are disabled and which are available. WBINVD in particular has potential data integrity implications. Code that might lead to a WBINVD usage should be disabled, not run all the way up to where WBINVD is attempted and then trigger an after-the-fact
WARN_ONCE().We don't expect the warning to ever happen. Yes all of this will be disabled. Nearly all are in code paths that cannot happen inside TDX anyways due to missing PCI-IDs or different cpuids, and S3 is explicitly disabled and would be impossible anyways due to lack of BIOS support.
Quoted 1 line
Quoted 6 lines
Quoted 3 lines
The WBINVD change deserves to be split off from MWAIT/MONITOR, and more thought needs to be put into where these spurious instruction usages are arising.
I disagree. We already spent a lot of cycles on this. WBINVD makes never sense in current TDX and all the code will be disabled.
Why not just drop the patch if it continues to cause people to spend cycles on it and it addresses a problem that will never happen?
Re: [RFC v2 16/32] x86/tdx: Handle MWAIT, MONITOR and WBINVD
Quoted 3 lines · Dan Williams
O
Why not just drop the patch if it continues to cause people to spend cycles on it and it addresses a problem that will never happen?
We want to at least get some kind of warning if there is really a mistake. Just dropping such an ability wouldn't seem right.
That's all that the patch does really.
-Andi
Re: [RFC v2 16/32] x86/tdx: Handle MWAIT, MONITOR and WBINVD
In reply to Dan WilliamsQuoted 4 lines · Dan Williams
Quoted 2 lines
I disagree. We already spent a lot of cycles on this. WBINVD makes never sense in current TDX and all the code will be disabled.
Why not just drop the patch if it continues to cause people to spend cycles on it and it addresses a problem that will never happen?
If someone calls WBINVD, we have a bug. Not a little bug, either. It probably means there's some horribly confused kernel code that's now facing broken cache coherency. To me, it's a textbook place to use BUG_ON().
This also doesn't "address" the problem, it just helps produce a more coherent warning message. It's why we have OOPS messages in the page fault handler: it never makes any sense to dereference a NULL pointer, yet we have code to make debugging them easier. It's well worth the ~20 lines of code that this costs us for ease of debugging.
Re: [RFC v2 16/32] x86/tdx: Handle MWAIT, MONITOR and WBINVD
Quoted 12 lines · Dave Hansen
Quoted 4 lines · Dan Williams
Quoted 2 lines
I disagree. We already spent a lot of cycles on this. WBINVD makes never sense in current TDX and all the code will be disabled.
Why not just drop the patch if it continues to cause people to spend cycles on it and it addresses a problem that will never happen?
If someone calls WBINVD, we have a bug. Not a little bug, either. It probably means there's some horribly confused kernel code that's now facing broken cache coherency. To me, it's a textbook place to use
BUG_ON().
Quoted 5 lines
This also doesn't "address" the problem, it just helps produce a more coherent warning message. It's why we have OOPS messages in the page fault handler: it never makes any sense to dereference a NULL pointer, yet we have code to make debugging them easier. It's well worth the ~20 lines of code that this costs us for ease of debugging.
The 'default' case in this 'switch' prints the exit reason and faults, can't that also trigger a backtrace that dumps the exception stack and the faulting instruction? In other words shouldn't this just fail with a common way to provide better debug on any unhandled #VE and not try to continue running past something that "can't" happen?
Re: [RFC v2 16/32] x86/tdx: Handle MWAIT, MONITOR and WBINVD
Quoted 6 lines
The 'default' case in this 'switch' prints the exit reason and faults, can't that also trigger a backtrace that dumps the exception stack and the faulting instruction? In other words shouldn't this just fail with a common way to provide better debug on any unhandled #VE and not try to continue running past something that "can't" happen?
It will use the #GP common code which will do all the backtracing etc.
We didn't think we would need anything else than what #GP already does.
-Andi
Re: [RFC v2 16/32] x86/tdx: Handle MWAIT, MONITOR and WBINVD
Quoted 9 lines · Andi Kleen
Quoted 5 lines
The 'default' case in this 'switch' prints the exit reason and faults, can't that also trigger a backtrace that dumps the exception stack and the faulting instruction? In other words shouldn't this just fail with a common way to provide better debug on any unhandled #VE and not try to continue running past something that "can't" happen?
It will use the #GP common code which will do all the backtracing etc.
We didn't think we would need anything else than what #GP already does.
How do these end up in practice? Do they still say "general protection fault..."?
Isn't that really mean for anyone that goes trying to figure out what caused these? If they see a "general protection fault" from WBINVD and go digging in the SDM for how a #GP can come from WBINVD, won't they be sorely disappointed?
Re: [RFC v2 16/32] x86/tdx: Handle MWAIT, MONITOR and WBINVD
need anything else than what #GP already does.
Quoted 2 lines
How do these end up in practice? Do they still say "general protection fault..."?
Yes, but there's a #VE specific message before it that prints the exit reason.
Quoted 5 lines
Isn't that really mean for anyone that goes trying to figure out what caused these? If they see a "general protection fault" from WBINVD and go digging in the SDM for how a #GP can come from WBINVD, won't they be sorely disappointed?
They'll see both the message and also that it isn't a true #VE in the backtrace.
-Andi
Re: [RFC v2 16/32] x86/tdx: Handle MWAIT, MONITOR and WBINVD
Quoted 6 lines · Andi Kleen
Quoted 2 lines
How do these end up in practice? Do they still say "general protection fault..."?
Yes, but there's a #VE specific message before it that prints the exit reason.
Quoted 7 lines
Quoted 4 lines
Isn't that really mean for anyone that goes trying to figure out what caused these? If they see a "general protection fault" from WBINVD and go digging in the SDM for how a #GP can come from WBINVD, won't they be sorely disappointed?
They'll see both the message and also that it isn't a true #VE in the backtrace.
Is there a good reason for the enduring "general protection fault..." message other than an aversion to refactoring the code?
Re: [RFC v2 16/32] x86/tdx: Handle MWAIT, MONITOR and WBINVD
Quoted 3 lines
Is there a good reason for the enduring "general protection fault..." message other than an aversion to refactoring the code?
You're the first ever to think it's a problem.
We're assuming that kernel developers are smart enough to understand this.
Please I implore everyone to move on from this patch. This is my last email on this topic.
-Andi
RFC 1/2 x86/tdx: Handle MWAIT and MONITOR
When running as a TDX guest, there are a number of existing, privileged instructions that do not work. If the guest kernel uses these instructions, the hardware generates a #VE.
You can find the list of unsupported instructions in Intel Trust Domain Extensions (Intel® TDX) Module specification, sec 9.2.2 and in Guest-Host Communication Interface (GHCI) Specification for Intel TDX, sec 2.4.1.
To prevent TD guests from using MWAIT/MONITOR instructions, the CPUID flags for these instructions are already disabled by the TDX module.
After the above mentioned preventive measures, if TD guests still execute these instructions, add appropriate warning message (WARN_ONCE()) in #VE handler. This handling behavior is same as KVM (which also treats MWAIT/MONITOR as nops with warning once in unsupported platforms).
---
Changes since RFC v2:
* Moved WBINVD related changes to a new patch.
* Fixed commit log as per review comments.
arch/x86/kernel/tdx.c | 8 ++++++++ 1 file changed, 8 insertions(+)
arch/x86/kernel/tdx.c +8 -0
@@ -511,6 +511,14 @@ int tdg_handle_virtualization_exception(struct pt_regs *regs,case EXIT_REASON_EPT_VIOLATION : ve->instr_len =tdg_handle_mmio (regs, ve);break ; +case EXIT_REASON_MONITOR_INSTRUCTION : +case EXIT_REASON_MWAIT_INSTRUCTION : +/* +* Something in the kernel used MONITOR or MWAIT despite +* X86_FEATURE_MWAIT being cleared for TDX guests. +*/ +WARN_ONCE (1 ,"TD Guest used unsupported MWAIT/MONITOR instruction\n" ); +break ;default :pr_warn ("Unexpected #VE: %lld\n" , ve->exit_reason);return -EFAULT ;
Signature · 1 line
2.25.1
RFC 2/2 x86/tdx: Ignore WBINVD instruction for TDX guest
Functionally only DMA devices can notice a side effect from WBINVD's cache flushing. But, TDX does not support DMA, because DMA typically needs uncached access for MMIO, and the current TDX module always sets the IgnorePAT bit, which prevents that.
So handle the WBINVD instruction as nop. Currently, we did not include any warning for WBINVD handling because ACPI reboot code uses it. This is the same behavior as KVM. It only allows WBINVD in a guest when the guest supports VT-d (=DMA), but just handles it as a nop if it doesn't .
If TDX ever gets DMA support, a hypercall will be added to implement it similar to AMD-SEV. But current TDX does not support direct DMA.
---
Changes since RFC v2:
* Fixed commit log as per review comments.
* Removed WARN_ONCE for WBINVD #VE support.
arch/x86/kernel/tdx.c | 6 ++++++ 1 file changed, 6 insertions(+)
arch/x86/kernel/tdx.c +6 -0
@@ -511,6 +511,12 @@ int tdg_handle_virtualization_exception(struct pt_regs *regs,case EXIT_REASON_EPT_VIOLATION : ve->instr_len =tdg_handle_mmio (regs, ve);break ; +case EXIT_REASON_WBINVD : +/* +* Non coherent DMA is not supported in TDX guest. +* So ignore WBINVD and treat it nop. +*/ +break ;case EXIT_REASON_MONITOR_INSTRUCTION :case EXIT_REASON_MWAIT_INSTRUCTION :/*
Signature · 1 line
2.25.1
Re: [RFC v2-fix-v2 2/2] x86/tdx: Ignore WBINVD instruction for TDX guest
Quoted 6 lines · Kuppuswamy Sathyanarayanan
Functionally only DMA devices can notice a side effect from WBINVD's cache flushing. But, TDX does not support DMA, because DMA typically needs uncached access for MMIO, and the current TDX module always sets the IgnorePAT bit, which prevents that.
I thought we discussed that there are other considerations for wbinvd besides DMA? In any event this paragraph is actively misleading because it disregards ACPI and Persistent Memory secure-erase whose usages of wbinvd have nothing to do with DMA. I would much prefer a patch to shutdown all the known wbinvd users as a precursor to this patch rather than assuming it's ok to simply ignore it. You have mentioned that TDX does not need to use those paths, but rather than assume they can't be used why not do the audit to explicitly disable them? Otherwise this statement seems to imply that the audit has not been done.
Re: [RFC v2-fix-v2 2/2] x86/tdx: Ignore WBINVD instruction for TDX guest
Quoted 6 lines · Dan Williams
Quoted 5 lines
Functionally only DMA devices can notice a side effect from WBINVD's cache flushing. But, TDX does not support DMA, because DMA typically needs uncached access for MMIO, and the current TDX module always sets the IgnorePAT bit, which prevents that.
Quoted 10 lines
I thought we discussed that there are other considerations for wbinvd besides DMA? In any event this paragraph is actively misleading because it disregards ACPI and Persistent Memory secure-erase whose usages of wbinvd have nothing to do with DMA. I would much prefer a patch to shutdown all the known wbinvd users as a precursor to this patch rather than assuming it's ok to simply ignore it. You have mentioned that TDX does not need to use those paths, but rather than assume they can't be used why not do the audit to explicitly disable them? Otherwise this statement seems to imply that the audit has not been done.
But KVM also emulates WBINVD only if DMA is supported. Otherwise it will be treated as noop.
static bool need_emulate_wbinvd(struct kvm_vcpu *vcpu)
{
return kvm_arch_has_noncoherent_dma(vcpu->kvm);
}Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Re: [RFC v2-fix-v2 2/2] x86/tdx: Ignore WBINVD instruction for TDX guest
Quoted 21 lines · Sathyanarayanan
Quoted 6 lines · Dan Williams
Quoted 5 lines
Functionally only DMA devices can notice a side effect from WBINVD's cache flushing. But, TDX does not support DMA, because DMA typically needs uncached access for MMIO, and the current TDX module always sets the IgnorePAT bit, which prevents that.
Quoted 10 lines
I thought we discussed that there are other considerations for wbinvd besides DMA? In any event this paragraph is actively misleading because it disregards ACPI and Persistent Memory secure-erase whose usages of wbinvd have nothing to do with DMA. I would much prefer a patch to shutdown all the known wbinvd users as a precursor to this patch rather than assuming it's ok to simply ignore it. You have mentioned that TDX does not need to use those paths, but rather than assume they can't be used why not do the audit to explicitly disable them? Otherwise this statement seems to imply that the audit has not been done.
Quoted 7 lines
But KVM also emulates WBINVD only if DMA is supported. Otherwise it will be treated as noop.
static bool
need_emulate_wbinvd(struct kvm_vcpu *vcpu){ return kvm_arch_has_noncoherent_dma(vcpu->kvm); }
That makes KVM also broken for the cases where wbinvd is needed, but it does not make the description of this patch correct.
Sean Christopherson
maintainerRe: [RFC v2-fix-v2 2/2] x86/tdx: Ignore WBINVD instruction for TDX guest
Quoted 26 lines · Dan Williams
Quoted 24 lines · Sathyanarayanan
Quoted 6 lines · Dan Williams
Quoted 5 lines
Functionally only DMA devices can notice a side effect from WBINVD's cache flushing. But, TDX does not support DMA, because DMA typically needs uncached access for MMIO, and the current TDX module always sets the IgnorePAT bit, which prevents that.
Quoted 10 lines
I thought we discussed that there are other considerations for wbinvd besides DMA? In any event this paragraph is actively misleading because it disregards ACPI and Persistent Memory secure-erase whose usages of wbinvd have nothing to do with DMA. I would much prefer a patch to shutdown all the known wbinvd users as a precursor to this patch rather than assuming it's ok to simply ignore it. You have mentioned that TDX does not need to use those paths, but rather than assume they can't be used why not do the audit to explicitly disable them? Otherwise this statement seems to imply that the audit has not been done.
But KVM also emulates WBINVD only if DMA is supported. Otherwise it will be treated as noop.
Quoted 7 lines
Quoted 4 lines
static bool
need_emulate_wbinvd(struct kvm_vcpu *vcpu){ return kvm_arch_has_noncoherent_dma(vcpu->kvm); }That makes KVM also broken for the cases where wbinvd is needed, but it does not make the description of this patch correct.
Yep! KVM has a long and dubious history of making things work for specific use cases without stricitly adhering to the architecture.
KVM also has to worry about malicious/buggy guests, e.g. letting the guest do WBINVD at will would be a massive noisy neighbor problem (at best), while ratelimiting might unnecessarily harm legitimate use case. I.e. KVM has a somewhat sane reason for "emulating" WBINVD as a nop.
And FWIW, IIRC all modern hardware has a coherent IOMMU, though that could be me making things up.
Re: [RFC v2-fix-v2 2/2] x86/tdx: Ignore WBINVD instruction for TDX guest
In reply to Dan WilliamsQuoted 2 lines
That makes KVM also broken for the cases where wbinvd is needed,
Or maybe your analysis is wrong?
Quoted 2 lines
but
it does not make the description of this patch correct.
If KVM was broken I'm sure we would hear about it.
The ACPI cases are for S3, which is not supported in guests, or for the old style manual IO port C6, which isn't supported either.
The persistent memory cases would require working DMA mappings, which we currently don't support. If DMA mappings were added we would need to para virtualized WBINVD, like the comments say.
AFAIK all the rest is for some caching attribute change, which is not possible in KVM (because it uses EPT.IgnorePAT=1) nor in TDX (which does the same). Some are for MTRR which is completely disabled if you're running under EPT.
-Andi
Re: [RFC v2-fix-v2 2/2] x86/tdx: Ignore WBINVD instruction for TDX guest
Quoted 6 lines · Andi Kleen
Quoted 3 lines
That makes KVM also broken for the cases where wbinvd is needed,
Or maybe your analysis is wrong?
I'm well aware of the fact that wbinvd is problematic for hypervisors and is an attack vector for a guest to DOS the host.
Quoted 6 lines
Quoted 4 lines
but
it does not make the description of this patch correct.If KVM was broken I'm sure we would hear about it.
KVM does not try to support the cases where wbinvd being unavailable would break the system. That is not the claim being made in this patch.
Quoted 4 lines
The ACPI cases are for S3, which is not supported in guests, or for the old style manual IO port C6, which isn't supported either.
The persistent memory cases would require working DMA mappings,
No, that analysis is wrong.The wbinvd audit would have found that persistent memory secure-erase and unlock, which has nothing to do with DMA, needs wbinvd to ensure that the CPU has not retained a copy of the PMEM contents from before the unlock happened and it needs to make sure that any data that was meant to be destroyed by an erasure is not retained in cache.
Quoted 8 lines
which we
currently don't support. If DMA mappings were added we would need to para virtualized WBINVD, like the comments say.AFAIK all the rest is for some caching attribute change, which is not possible in KVM (because it uses EPT.IgnorePAT=1) nor in TDX (which does the same). Some are for MTRR which is completely disabled if you're running under EPT.
It's fine to not support the above cases, I am asking for the explanation to demonstrate the known risks and the known mitigations. IgnorePAT is not the mitigation, the mitigation is an audit to describe why the known users are unlikely to be triggered. Even better would be an addition patch that does something like:
iff --git a/drivers/nvdimm/security.c b/drivers/nvdimm/security.c index 4b80150e4afa..a6b13a1ae319 100644
drivers/nvdimm/security.c +3 -0
@@ -170,6 +170,9 @@ static int __nvdimm_security_unlock(struct nvdimm *nvdimm)const void *data;int rc; +if (is_protected_guest ()) +return -ENXIO ; +/* The bus lock should be held at the top level of the call stack */ lockdep_assert_held (&nvdimm_bus->reconfig_mutex);
...to explicitly error out a wbinvd use case before data is altered and wbinvd is needed.
Re: [RFC v2-fix-v2 2/2] x86/tdx: Ignore WBINVD instruction for TDX guest
Quoted 8 lines · Dan Williams
Quoted 5 lines
Quoted 4 lines
but
it does not make the description of this patch correct.If KVM was broken I'm sure we would hear about it.
KVM does not try to support the cases where wbinvd being unavailable would break the system. That is not the claim being made in this patch.
I thought we made that claim.
"We just want to be the same as KVM"
Quoted 10 lines
Quoted 4 lines
The ACPI cases are for S3, which is not supported in guests, or for the old style manual IO port C6, which isn't supported either. The persistent memory cases would require working DMA mappings,
No, that analysis is wrong.The wbinvd audit would have found that persistent memory secure-erase and unlock, which has nothing to do with DMA, needs wbinvd to ensure that the CPU has not retained a copy of the PMEM contents from before the unlock happened and it needs to make sure that any data that was meant to be destroyed by an erasure is not retained in cache.
But that's all not supported in TDX.
And the only way it could work in KVM is when there is some DMA, likely at least an IOMMU, e.g. to set up the persistent memory. That's what I meant with working DMA mappings.
Otherwise KVM would be really broken, but I don't really believe that without some real evidence.
Quoted 3 lines
It's fine to not support the above cases, I am asking for the explanation to demonstrate the known risks and the known mitigations.
The analysis is that all this stuff that you are worried about cannot be enabled in a TDX guest
(it would be a nightmare if it could, we would need to actually make it secure against a malicious host)
Quoted 18 lines
IgnorePAT is not the mitigation, the mitigation is an audit to describe why the known users are unlikely to be triggered. Even better would be an addition patch that does something like:
iff --git
a/drivers/nvdimm/security.cb/drivers/nvdimm/security.cindex4b80150e4afa..a6b13a1ae319100644drivers/nvdimm/security.c +3
-0@@ -170,6 +170,9 @@ static int __nvdimm_security_unlock(struct nvdimm *nvdimm)const void *data;int rc; +if (is_protected_guest ()) +return -ENXIO ; +/* The bus lock should be held at the top level of the call stack */ lockdep_assert_held (&nvdimm_bus->reconfig_mutex);
Quoted 2 lines
...to explicitly error out a wbinvd use case before data is altered and wbinvd is needed.
I don't see any point of all of this. We really just want to be the same as KVM. Not get into the business of patching a bazillion sub systems that cannot be used in TDX anyways.
-Andi
Re: [RFC v2-fix-v2 2/2] x86/tdx: Ignore WBINVD instruction for TDX guest
On Mon, May 24, 2021 at 7:13 PM Andi Kleen <[email protected]> wrote:
[..]
Quoted 6 lines
Quoted 2 lines
...to explicitly error out a wbinvd use case before data is altered and wbinvd is needed.
I don't see any point of all of this. We really just want to be the same as KVM. Not get into the business of patching a bazillion sub systems that cannot be used in TDX anyways.
Please let's not start this patch off with dubious claims of safety afforded by IgnorePAT. Instead make the true argument that wbinvd is known to be problematic in guests and for that reason many bare metal use cases that require wbinvd have not been ported to guests (like PMEM unlock), and others that only use wbinvd to opportunistically enforce a cache state (like ACPI sleep states) do not see ill effects from missing wbinvd. Given KVM ships with a policy to elide wbinvd in many scenarios adopt the same policy for TDX guests.
Re: [RFC v2-fix-v2 2/2] x86/tdx: Ignore WBINVD instruction for TDX guest
Quoted 10 lines · Dan Williams
On Mon, May 24, 2021 at 7:13 PM Andi Kleen <[email protected]> wrote:
[..]Quoted 5 lines
Quoted 2 lines
...to explicitly error out a wbinvd use case before data is altered and wbinvd is needed.
I don't see any point of all of this. We really just want to be the same as KVM. Not get into the business of patching a bazillion sub systems that cannot be used in TDX anyways.
Please let's not start this patch off with dubious claims of safety afforded by IgnorePAT. Instead make the true argument that wbinvd is known to be problematic in guests
That's just another reason to not support WBINVD, but I don't think it's the main reason. The main reason is that it is simply not needed, unless you do DMA in some form.
(and yes I consider direct mapping of persistent memory with a complex setup procedure a form of DMA -- my guess is that the reason that it works in KVM is that it somehow activates the DMA code paths in KVM)
IMNSHO that's the true reason.
Quoted 4 lines
and for that reason many bare metal
use cases that require wbinvd have not been ported to guests (like PMEM unlock), and others that only use wbinvd to opportunistically enforce a cache state (like ACPI sleep states)
ACPI sleep states are not supported or needed in virtualization. They are mostly obsolete on real hardware too.
Quoted 3 lines
do not see ill effects
from missing wbinvd. Given KVM ships with a policy to elide wbinvd in many scenarios adopt the same policy for TDX guests.
Re: [RFC v2-fix-v2 2/2] x86/tdx: Ignore WBINVD instruction for TDX guest
Quoted 14 lines · Andi Kleen
Quoted 10 lines · Dan Williams
On Mon, May 24, 2021 at 7:13 PM Andi Kleen <[email protected]> wrote:
[..]Quoted 5 lines
Quoted 2 lines
...to explicitly error out a wbinvd use case before data is altered and wbinvd is needed.
I don't see any point of all of this. We really just want to be the same as KVM. Not get into the business of patching a bazillion sub systems that cannot be used in TDX anyways.
Please let's not start this patch off with dubious claims of safety afforded by IgnorePAT. Instead make the true argument that wbinvd is known to be problematic in guests
Quoted 7 lines
That's just another reason to not support WBINVD, but I don't think it's the main reason. The main reason is that it is simply not needed, unless you do DMA in some form.
(and yes I consider direct mapping of persistent memory with a complex setup procedure a form of DMA -- my guess is that the reason that it works in KVM is that it somehow activates the DMA code paths in KVM)
No, it doesn't. Simply no one has tried to pass through the security interface of bare metal nvdimm to a guest, or enabled the security commands in a virtualized nvdimm. If a guest supports a memory map it supports PMEM I struggle to see DMA anywhere in that equation.
Quoted 2 lines
IMNSHO that's the true reason.
I do see why it would be attractive if IgnorePAT was a solid signal to ditch wbinvd support. However, it simply isn't, and to date nothing has cared trip over that gap.
Re: [RFC v2-fix-v2 2/2] x86/tdx: Ignore WBINVD instruction for TDX guest
Quoted 17 lines · Dan Williams
Quoted 16 lines · Andi Kleen
Quoted 10 lines · Dan Williams
On Mon, May 24, 2021 at 7:13 PM Andi Kleen <[email protected]> wrote:
[..]Quoted 5 lines
Quoted 2 lines
...to explicitly error out a wbinvd use case before data is altered and wbinvd is needed.
I don't see any point of all of this. We really just want to be the same as KVM. Not get into the business of patching a bazillion sub systems that cannot be used in TDX anyways.
Please let's not start this patch off with dubious claims of safety afforded by IgnorePAT. Instead make the true argument that wbinvd is known to be problematic in guests
That's just another reason to not support WBINVD, but I don't think it's the main reason. The main reason is that it is simply not needed, unless you do DMA in some form.
Quoted 6 lines
Quoted 3 lines
(and yes I consider direct mapping of persistent memory with a complex setup procedure a form of DMA -- my guess is that the reason that it works in KVM is that it somehow activates the DMA code paths in KVM)
No, it doesn't. Simply no one has tried to pass through the security interface of bare metal nvdimm to a guest, or enabled the security commands in a virtualized nvdimm.
Maybe a better term would be "external side effects". If you have something in IO domain which can notice a difference.
Quoted 1 line
If a guest supports a memory map it supports PMEM I struggle to see DMA anywhere in that equation.
Okay if that's happen to a TDX guest we have to start emulate WBINVD. But right now we don't need it.
I guess we can add a comment that says
"if someone wants to implement NVDIMM secure delete they would also need to implement this new hypercall"
Quoted 5 lines
Quoted 2 lines
IMNSHO that's the true reason.
I do see why it would be attractive if IgnorePAT was a solid signal to ditch wbinvd support. However, it simply isn't, and to date nothing has cared trip over that gap.
I think we're getting into angels on a pinhead here.
The key point is that current TDX does not need WBINVD. I believe we agree on that.
-Andi
RFC 1/1 x86/tdx: Ignore WBINVD instruction for TDX guest
Functionally only devices outside the CPU (such as DMA devices, or persistent memory for flushing) can notice the external side effects from WBINVD's cache flushing for write back mappings. One exception here is MKTME, but that is not visible outside the TDX module and not possible inside a TDX guest.
Currently TDX does not support DMA, because DMA typically needs uncached access for MMIO, and the current TDX module always sets the IgnorePAT bit, which prevents that.
Persistent memory is also currently not supported. There are some other cases that use WBINVD, such as the legacy ACPI sleeps, but these are all not supported in virtualization and there are better mechanisms inside a guest anyways. The guests usually are not aware of power management. Another code path that uses WBINVD is the MTRR driver, but EPT/virtualization always disables MTRRs so those are not needed. This all implies WBINVD is not needed with current TDX.
So handle the WBINVD instruction as nop. Currently, #VE exception handler does not include any warning for WBINVD handling because ACPI reboot code uses it. This is the same behavior as KVM. It only allows WBINVD in a guest when the guest supports VT-d (=DMA), but just handles it as a nop if it doesn't .
If TDX ever gets DMA support, or persistent memory support, or some other devices that can observe flushing side effects, a hypercall can be added to implement it similar to AMD-SEV. But current TDX does not need it.
---
Changes since RFC v2-2:
* Added more details to commit log and comments to address review comments.
Changes since RFC v2:
* Fixed commit log as per review comments.
* Removed WARN_ONCE for WBINVD #VE support.
arch/x86/kernel/tdx.c | 7 +++++++ 1 file changed, 7 insertions(+)
arch/x86/kernel/tdx.c +7 -0
@@ -455,6 +455,13 @@ int tdg_handle_virtualization_exception(struct pt_regs *regs,case EXIT_REASON_EPT_VIOLATION : ve->instr_len =tdg_handle_mmio (regs, ve);break ; +case EXIT_REASON_WBINVD : +/* +* Non coherent DMA, persistent memory, MTRRs or +* outdated ACPI sleeps are not supported in TDX guest. +* So ignore WBINVD and treat it nop. +*/ +break ;case EXIT_REASON_MONITOR_INSTRUCTION :case EXIT_REASON_MWAIT_INSTRUCTION :/*
Signature · 1 line
2.25.1
Re: [RFC v2-fix-v3 1/1] x86/tdx: Ignore WBINVD instruction for TDX guest
Quoted 26 lines · Kuppuswamy Sathyanarayanan
Functionally only devices outside the CPU (such as DMA devices, or persistent memory for flushing) can notice the external side effects from WBINVD's cache flushing for write back mappings. One exception here is MKTME, but that is not visible outside the TDX module and not possible inside a TDX guest.
Currently TDX does not support DMA, because DMA typically needs uncached access for MMIO, and the current TDX module always sets the IgnorePAT bit, which prevents that.
Persistent memory is also currently not supported. There are some other cases that use WBINVD, such as the legacy ACPI sleeps, but these are all not supported in virtualization and there are better mechanisms inside a guest anyways. The guests usually are not aware of power management. Another code path that uses WBINVD is the MTRR driver, but EPT/virtualization always disables MTRRs so those are not needed. This all implies WBINVD is not needed with current TDX.
So handle the WBINVD instruction as nop. Currently, #VE exception handler does not include any warning for WBINVD handling because ACPI reboot code uses it. This is the same behavior as KVM. It only allows WBINVD in a guest when the guest supports VT-d (=DMA), but just handles it as a nop if it doesn't .
Quoted 4 lines
If TDX ever gets DMA support, or persistent memory support, or some other devices that can observe flushing side effects, a hypercall can be added to implement it similar to AMD-SEV. But current TDX does not need it.
Please just drop this patch. It serves no purpose especially when the assertion is that nothing in TDX will miss WBINVD. Why would Linux merge a patch that has no claimed end user benefit? If the only known usage of WBINVD in a TDX guest is the ACPI reboot path then add an is_protected_guest() to that one usage.
If a TDX guest runs an unexpected WBINVD that's a bug that needs a kernel fix.
RFC 1/1 x86/tdx: Skip WBINVD instruction for TDX guest
Current TDX spec does not have support to emulate the WBINVD instruction. So, add support to skip WBINVD instruction in drivers that are currently enabled in the TDX guest.
Functionally only devices outside the CPU (such as DMA devices, or persistent memory for flushing) can notice the external side effects from WBINVD's cache flushing for write back mappings. One exception here is MKTME, but that is not visible outside the TDX module and not possible inside a TDX guest.
Currently TDX does not support DMA, because DMA typically needs uncached access for MMIO, and the current TDX module always sets the IgnorePAT bit, which prevents that.
Persistent memory is also currently not supported. Another code path that uses WBINVD is the MTRR driver, but EPT/virtualization always disables MTRRs so those are not needed. This all implies WBINVD is not needed with current TDX.
So, most drivers/code-paths that use wbinvd instructions are already disabled for TDX guest platforms via config-option/BIOS. Following are the list of drivers that use wbinvd instruction and are still enabled for TDX guests.drivers/acpi/sleep.cdrivers/acpi/acpica/hwsleep.c
Since cache is always coherent in TDX guests, making wbinvd as noop should not cause any issues. This behavior is the same as KVM guest.
Also, hwsleep shouldn't happen for TDX guest because the TDX BIOS won't enable it, but it's better to disable it anyways
---
Changes since RFC v2-fix-v2:
* Instead of handling WBINVD #VE exception as nop, we skip its usage in currently enabled drivers.
* Adapted commit log for above change.
arch/x86/kernel/tdx.c | 1 + drivers/acpi/acpica/hwsleep.c | 12 +++++++++--- drivers/acpi/sleep.c | 26 +++++++++++++++++++++++--- include/linux/protected_guest.h | 2 ++ 4 files changed, 35 insertions(+), 6 deletions(-)
arch/x86/kernel/tdx.c +1 -0
@@ -100,6 +100,7 @@ bool tdx_protected_guest_has(unsigned long flag)case PR_GUEST_MEM_ENCRYPT_ACTIVE :case PR_GUEST_UNROLL_STRING_IO :case PR_GUEST_SHARED_MAPPING_INIT : +case PR_GUEST_DISABLE_WBINVD :return true; }
drivers/acpi/acpica/hwsleep.c +9 -3
@@ -9,6 +9,7 @@*****************************************************************************/ #include <acpi/acpi.h> +#include <linux/protected_guest.h> #include "accommon.h" #define _COMPONENT ACPI_HARDWARE
@@ -108,9 +109,14 @@ acpi_status acpi_hw_legacy_sleep(u8 sleep_state) pm1a_control |= sleep_enable_reg_info->access_bit_mask; pm1b_control |= sleep_enable_reg_info->access_bit_mask; -/* Flush caches, as per ACPI specification */ - -ACPI_FLUSH_CPU_CACHE (); +/* +* WBINVD instruction is not supported in TDX +* guest. Since ACPI_FLUSH_CPU_CACHE() uses +* WBINVD, skip cache flushes for TDX guests. +*/ +if (prot_guest_has (PR_GUEST_DISABLE_WBINVD )) +/* Flush caches, as per ACPI specification */ +ACPI_FLUSH_CPU_CACHE (); status =acpi_os_enter_sleep (sleep_state, pm1a_control, pm1b_control);if (status ==AE_CTRL_TERMINATE ) {
drivers/acpi/sleep.c +23 -3
@@ -18,6 +18,7 @@#include <linux/acpi.h> #include <linux/module.h> #include <linux/syscore_ops.h> +#include <linux/protected_guest.h> #include <asm/io.h> #include <trace/events/power.h>
@@ -71,7 +72,14 @@ static int acpi_sleep_prepare(u32 acpi_state) acpi_set_waking_vector(acpi_wakeup_address); } -ACPI_FLUSH_CPU_CACHE (); + +/* +* WBINVD instruction is not supported in TDX +* guest. Since ACPI_FLUSH_CPU_CACHE() uses +* WBINVD, skip cache flushes for TDX guests. +*/ +if (prot_guest_has (PR_GUEST_DISABLE_WBINVD )) +ACPI_FLUSH_CPU_CACHE ();#endif printk(KERN_INFO PREFIX "Preparing to enter system sleep state S%d\n", acpi_state);
@@ -566,7 +574,13 @@ static int acpi_suspend_enter(suspend_state_t pm_state)u32 acpi_state = acpi_target_sleep_state;int error; -ACPI_FLUSH_CPU_CACHE (); +/* +* WBINVD instruction is not supported in TDX +* guest. Since ACPI_FLUSH_CPU_CACHE() uses +* WBINVD, skip cache flushes for TDX guests. +*/ +if (prot_guest_has (PR_GUEST_DISABLE_WBINVD )) +ACPI_FLUSH_CPU_CACHE ();trace_suspend_resume (TPS ("acpi_suspend" ), acpi_state, true);switch (acpi_state) {
@@ -899,7 +913,13 @@ static int acpi_hibernation_enter(void) {acpi_status status =AE_OK ; -ACPI_FLUSH_CPU_CACHE (); +/* +* WBINVD instruction is not supported in TDX +* guest. Since ACPI_FLUSH_CPU_CACHE() uses +* WBINVD, skip cache flushes for TDX guests. +*/ +if (prot_guest_has (PR_GUEST_DISABLE_WBINVD )) +ACPI_FLUSH_CPU_CACHE ();/* This shouldn't return. If it returns, we have a problem */ status =acpi_enter_sleep_state (ACPI_STATE_S4 );
include/linux/protected_guest.h +2 -0
@@ -18,6 +18,8 @@#define PR_GUEST_HOST_MEM_ENCRYPT 0x103/* Support for shared mapping initialization (after early init) */ #define PR_GUEST_SHARED_MAPPING_INIT 0x104 +/* Support to disable WBINVD */ +#define PR_GUEST_DISABLE_WBINVD 0x105#if defined(CONFIG_INTEL_TDX_GUEST ) || defined(CONFIG_AMD_MEM_ENCRYPT )
Signature · 1 line
2.25.1
Re: [RFC v2-fix-v3 1/1] x86/tdx: Skip WBINVD instruction for TDX guest
[ add Rafael and linux-acpi ]
Quoted 165 lines · Kuppuswamy Sathyanarayanan
Current TDX spec does not have support to emulate the WBINVD instruction. So, add support to skip WBINVD instruction in drivers that are currently enabled in the TDX guest.
Functionally only devices outside the CPU (such as DMA devices, or persistent memory for flushing) can notice the external side effects from WBINVD's cache flushing for write back mappings. One exception here is MKTME, but that is not visible outside the TDX module and not possible inside a TDX guest.
Currently TDX does not support DMA, because DMA typically needs uncached access for MMIO, and the current TDX module always sets the IgnorePAT bit, which prevents that.
Persistent memory is also currently not supported. Another code path that uses WBINVD is the MTRR driver, but EPT/virtualization always disables MTRRs so those are not needed. This all implies WBINVD is not needed with current TDX.
So, most
drivers/code-pathsthat use wbinvd instructions are already disabled for TDX guest platforms via config-option/BIOS. Following are the list of drivers that use wbinvd instruction and are still enabled for TDX guests.
drivers/acpi/sleep.cdrivers/acpi/acpica/hwsleep.cSince cache is always coherent in TDX guests, making wbinvd as noop should not cause any issues. This behavior is the same as KVM guest.
Also, hwsleep shouldn't happen for TDX guest because the TDX BIOS won't enable it, but it's better to disable it anyways
Signed-off-by Kuppuswamy Sathyanarayanan <[email protected]>---
Changes since RFC v2-fix-v2:
* Instead of handling WBINVD #VE exception as nop, we skip its usage in currently enabled drivers.
* Adapted commit log for above change.arch/x86/kernel/tdx.c | 1 + drivers/acpi/acpica/hwsleep.c | 12 +++++++++--- drivers/acpi/sleep.c | 26 +++++++++++++++++++++++--- include/linux/protected_guest.h | 2 ++ 4 files changed, 35 insertions(+), 6 deletions(-)arch/x86/kernel/tdx.c +1
-0@@ -100,6 +100,7 @@ bool tdx_protected_guest_has(unsigned long flag)case PR_GUEST_MEM_ENCRYPT_ACTIVE :case PR_GUEST_UNROLL_STRING_IO :case PR_GUEST_SHARED_MAPPING_INIT : +case PR_GUEST_DISABLE_WBINVD :return true; }drivers/acpi/acpica/hwsleep.c +9
-3@@ -9,6 +9,7 @@*****************************************************************************/ #include <acpi/acpi.h> +#include <linux/protected_guest.h> #include "accommon.h" #define _COMPONENT ACPI_HARDWARE@@ -108,9 +109,14 @@ acpi_status acpi_hw_legacy_sleep(u8 sleep_state) pm1a_control |= sleep_enable_reg_info->access_bit_mask; pm1b_control |= sleep_enable_reg_info->access_bit_mask; -/* Flush caches, as per ACPI specification */ - -ACPI_FLUSH_CPU_CACHE (); +/* +* WBINVD instruction is not supported in TDX +* guest. Since ACPI_FLUSH_CPU_CACHE() uses +* WBINVD, skip cache flushes for TDX guests. +*/ +if (prot_guest_has (PR_GUEST_DISABLE_WBINVD )) +/* Flush caches, as per ACPI specification */ +ACPI_FLUSH_CPU_CACHE (); status =acpi_os_enter_sleep (sleep_state, pm1a_control, pm1b_control);if (status ==AE_CTRL_TERMINATE ) {drivers/acpi/sleep.c +23
-3@@ -18,6 +18,7 @@#include <linux/acpi.h> #include <linux/module.h> #include <linux/syscore_ops.h> +#include <linux/protected_guest.h> #include <asm/io.h> #include <trace/events/power.h> @@ -71,7 +72,14 @@ static int acpi_sleep_prepare(u32 acpi_state) acpi_set_waking_vector(acpi_wakeup_address); } -ACPI_FLUSH_CPU_CACHE (); + +/* +* WBINVD instruction is not supported in TDX +* guest. Since ACPI_FLUSH_CPU_CACHE() uses +* WBINVD, skip cache flushes for TDX guests. +*/ +if (prot_guest_has (PR_GUEST_DISABLE_WBINVD )) +ACPI_FLUSH_CPU_CACHE ();#endif printk(KERN_INFO PREFIX "Preparing to enter system sleep state S%d\n", acpi_state);@@ -566,7 +574,13 @@ static int acpi_suspend_enter(suspend_state_t pm_state)u32 acpi_state = acpi_target_sleep_state;int error; -ACPI_FLUSH_CPU_CACHE (); +/* +* WBINVD instruction is not supported in TDX +* guest. Since ACPI_FLUSH_CPU_CACHE() uses +* WBINVD, skip cache flushes for TDX guests. +*/ +if (prot_guest_has (PR_GUEST_DISABLE_WBINVD )) +ACPI_FLUSH_CPU_CACHE ();trace_suspend_resume (TPS ("acpi_suspend" ), acpi_state, true);switch (acpi_state) {@@ -899,7 +913,13 @@ static int acpi_hibernation_enter(void) {acpi_status status =AE_OK ; -ACPI_FLUSH_CPU_CACHE (); +/* +* WBINVD instruction is not supported in TDX +* guest. Since ACPI_FLUSH_CPU_CACHE() uses +* WBINVD, skip cache flushes for TDX guests. +*/ +if (prot_guest_has (PR_GUEST_DISABLE_WBINVD )) +ACPI_FLUSH_CPU_CACHE ();/* This shouldn't return. If it returns, we have a problem */ status =acpi_enter_sleep_state (ACPI_STATE_S4 );include/linux/protected_guest.h +2
-1@@ -18,6 +18,8 @@#define PR_GUEST_HOST_MEM_ENCRYPT 0x103/* Support for shared mapping initialization (after early init) */ #define PR_GUEST_SHARED_MAPPING_INIT 0x104 +/* Support to disable WBINVD */ +#define PR_GUEST_DISABLE_WBINVD 0x105#if defined(CONFIG_INTEL_TDX_GUEST ) || defined(CONFIG_AMD_MEM_ENCRYPT ) --2.25.1
Re: [RFC v2-fix-v3 1/1] x86/tdx: Skip WBINVD instruction for TDX guest
In reply to Kuppuswamy SathyanarayananQuoted 4 lines · Kuppuswamy Sathyanarayanan
Persistent memory is also currently not supported. Another code path that uses WBINVD is the MTRR driver, but EPT/virtualization always disables MTRRs so those are not needed. This all implies WBINVD is not needed with current TDX.
It's one thing to declare something unsupported. It's quite another to declare it unsupported and then back it up with code to ensure that any attempted use is thwarted.
This patch certainly shows us half of the solution. But, to be complete, we also need to see the other half: where is the patch or documentation for why it is not *possible* to encounter persistent memory in a TDX guest?
BTW, "persistent memory" is much more than Intel Optane DCPMM.
Re: [RFC v2-fix-v3 1/1] x86/tdx: Skip WBINVD instruction for TDX guest
Quoted 11 lines · Dave Hansen
Quoted 4 lines · Kuppuswamy Sathyanarayanan
Persistent memory is also currently not supported. Another code path that uses WBINVD is the MTRR driver, but EPT/virtualization always disables MTRRs so those are not needed. This all implies WBINVD is not needed with current TDX.
It's one thing to declare something unsupported. It's quite another to declare it unsupported and then back it up with code to ensure that any attempted use is thwarted.
This patch certainly shows us half of the solution. But, to be complete, we also need to see the other half: where is the patch
We had multiple patches to handle it earlier (by ignoring it which is the right way and deployed successfully everywhere in KVM), but you guys all didn't like them.
So they got removed.
You can't have your cake and eat it. Either you have the ignore or warn on patches or you have panic.
In this iteration now you have panic (through the exception handler) except we explicitely ignore it for the cases we know that can happen (which is reboot)
Quoted 3 lines
or
documentation for why it is not *possible* to encounter persistent memory in a TDX guest?
I thought we already went over this ad nauseam.
The current TDX VMMs don't support anything else than plain DRAM.
If there is support for anything else in the future we'll need to add a new GHCI call that implements WBINVD through the host, but right now we don't need it.
-Andi.
Re: [RFC v2-fix-v3 1/1] x86/tdx: Skip WBINVD instruction for TDX guest
In reply to Dave HansenQuoted 9 lines · Dave Hansen
Quoted 4 lines · Kuppuswamy Sathyanarayanan
Persistent memory is also currently not supported. Another code path that uses WBINVD is the MTRR driver, but EPT/virtualization always disables MTRRs so those are not needed. This all implies WBINVD is not needed with current TDX.
It's one thing to declare something unsupported. It's quite another to declare it unsupported and then back it up with code to ensure that any attempted use is thwarted.
Only audited and supported drivers will be allowed to enumerate after device filter support patch is merged. Till we merge that patch, If any of these unsupported features (with WBINVD usage) are enabled in TDX, it will lead to sigfault (due to unhandled #VE).
In this patch we only create exception for ACPI sleep driver code. If commit log is confusing, I can remove information about other unsupported feature (with WBINVD usage).
Quoted 8 lines
This patch certainly shows us half of the solution. But, to be complete, we also need to see the other half: where is the patch or documentation for why it is not *possible* to encounter persistent memory in a TDX guest?
BTW, "persistent memory" is much more than Intel Optane DCPMM.
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Re: [RFC v2-fix-v3 1/1] x86/tdx: Skip WBINVD instruction for TDX guest
Quoted 7 lines · Sathyanarayanan
Quoted 6 lines · Dave Hansen
Quoted 4 lines · Kuppuswamy Sathyanarayanan
Persistent memory is also currently not supported. Another code path that uses WBINVD is the MTRR driver, but EPT/virtualization always disables MTRRs so those are not needed. This all implies WBINVD is not needed with current TDX.
Quoted 8 lines
Quoted 3 lines
It's one thing to declare something unsupported. It's quite another to declare it unsupported and then back it up with code to ensure that any attempted use is thwarted.
Only audited and supported drivers will be allowed to enumerate after device filter support patch is merged. Till we merge that patch, If any of these unsupported features (with WBINVD usage) are enabled in TDX, it will lead to sigfault (due to unhandled #VE).
A kernel driver using WBINVD will "sigfault"? I'm not sure what that means. How does the kernel "sigfault"?
Quoted 3 lines
In this patch we only create exception for ACPI sleep driver code. If commit log is confusing, I can remove information about other unsupported feature (with WBINVD usage).
Yes, the changelog is horribly confusing. But simply removing this information is insufficient to rectify the deficiency.
I've lost trust that due diligence will be performed on this series on its own. I've seen too many broken promises and too many holes.
Here's what I want to see: a list of all of the unique call sites for WBINVD in the kernel. I want a written down methodology for how the list of call sites was generated. I want to see an item-by-item list of why those call sites are unreachable with the TDX guest code. It might be because they've been patched in this patch, or the driver has been disabled, or because the TDX architecture spec would somehow prohibit the situation where it might be needed. But, there needs to be a list, and you have to show your work. If you refer to code from this series as helping to prevent WBINVD, then it has to be earlier in this series, not in some other series and not later in this series.
Just eyeballing it, there are ~50 places in the kernel that need auditing.
Right now, we mostly have indiscriminate hand-waving about this not being a problem. It's a hard NAK from me on this patch until this audit is in place.
Re: [RFC v2-fix-v3 1/1] x86/tdx: Skip WBINVD instruction for TDX guest
\
Quoted 2 lines
A kernel driver using WBINVD will "sigfault"? I'm not sure what that means. How does the kernel "sigfault"?
It panics. Please, you know exactly what Sathya meant because you've read the code.
Quoted 11 lines
Here's what I want to see: a list of all of the unique call sites for WBINVD in the kernel. I want a written down methodology for how the list of call sites was generated. I want to see an item-by-item list of why those call sites are unreachable with the TDX guest code. It might be because they've been patched in this patch, or the driver has been disabled, or because the TDX architecture spec would somehow prohibit the situation where it might be needed. But, there needs to be a list, and you have to show your work. If you refer to code from this series as helping to prevent WBINVD, then it has to be earlier in this series, not in some other series and not later in this series.
Sorry this is ridiculous. We're not in a make-work project here. We're about practical engineering not make out life artificially complicated.
If that is what is required then the change requests to NOT ignore but patch every site were just not practical.
Quoted 6 lines
Just eyeballing it, there are ~50 places in the kernel that need auditing.
Right now, we mostly have indiscriminate hand-waving about this not being a problem. It's a hard NAK from me on this patch until this audit is in place.
Okay then we just go back to ignore like the rest of the KVM world.
That's what we had originally and it it's fine because it's exactly what KVM does, which is all we want.
It was the sane thing to do and it's still the sane thing to do because it has been always done this way.
-And
Re: [RFC v2-fix-v3 1/1] x86/tdx: Skip WBINVD instruction for TDX guest
In reply to Dave HansenQuoted 7 lines · Dave Hansen
Quoted 2 lines · Sathyanarayanan
Quoted 1 line · Dave Hansen
On 6/8/21 2:35 PM, Kuppuswamy Sathyanarayanan wrote:
A kernel driver using WBINVD will "sigfault"? I'm not sure what that means. How does the kernel "sigfault"?
Sorry, un-supported #VE is handled similar to #GP fault.
Quoted 7 lines
Quoted 4 lines
In this patch we only create exception for ACPI sleep driver code. If commit log is confusing, I can remove information about other unsupported feature (with WBINVD usage).
Yes, the changelog is horribly confusing. But simply removing this information is insufficient to rectify the deficiency.
I will remove all the unrelated information from this commit log. As long as commit log *only* talks and handles the exception for ACPI sleep driver, it should be acceptable for you right? I will also add a note about, if any other feature with WBINVD usage is enabled, it would lead to #GP fault.
Quoted 21 lines
I've lost trust that due diligence will be performed on this series on its own. I've seen too many broken promises and too many holes.
Here's what I want to see: a list of all of the unique call sites for WBINVD in the kernel. I want a written down methodology for how the list of call sites was generated. I want to see an item-by-item list of why those call sites are unreachable with the TDX guest code. It might be because they've been patched in this patch, or the driver has been disabled, or because the TDX architecture spec would somehow prohibit the situation where it might be needed. But, there needs to be a list, and you have to show your work. If you refer to code from this series as helping to prevent WBINVD, then it has to be earlier in this series, not in some other series and not later in this series.
Just eyeballing it, there are ~50 places in the kernel that need auditing.
Right now, we mostly have indiscriminate hand-waving about this not being a problem. It's a hard NAK from me on this patch until this audit is in place.
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Re: [RFC v2-fix-v3 1/1] x86/tdx: Skip WBINVD instruction for TDX guest
In reply to Kuppuswamy SathyanarayananQuoted 11 lines · Kuppuswamy Sathyanarayanan
Current TDX spec does not have support to emulate the WBINVD instruction. So, add support to skip WBINVD instruction in drivers that are currently enabled in the TDX guest.
Functionally only devices outside the CPU (such as DMA devices, or persistent memory for flushing) can notice the external side effects from WBINVD's cache flushing for write back mappings. One exception here is MKTME, but that is not visible outside the TDX module and not possible inside a TDX guest.
Quoted 8 lines
Currently TDX does not support DMA, because DMA typically needs uncached access for MMIO, and the current TDX module always sets the IgnorePAT bit, which prevents that.
Persistent memory is also currently not supported. Another code path that uses WBINVD is the MTRR driver, but EPT/virtualization always disables MTRRs so those are not needed. This all implies WBINVD is not needed with current TDX.
Let's drop the last three paragraphs and just say something like:
"This is one of a series of patches to usages of wbinvd for protected guests. For now this just addresses the one known path that TDX executes, ACPI reboot. Its usage can be elided because FOO reason and all the other ACPI_FLUSH_CPU_CACHE usages can be elided because BAR reason"
Quoted 59 lines
So, most
drivers/code-pathsthat use wbinvd instructions are already disabled for TDX guest platforms via config-option/BIOS. Following are the list of drivers that use wbinvd instruction and are still enabled for TDX guests.
drivers/acpi/sleep.cdrivers/acpi/acpica/hwsleep.cSince cache is always coherent in TDX guests, making wbinvd as noop should not cause any issues. This behavior is the same as KVM guest.
Also, hwsleep shouldn't happen for TDX guest because the TDX BIOS won't enable it, but it's better to disable it anyways
Signed-off-by Kuppuswamy Sathyanarayanan <[email protected]>---
Changes since RFC v2-fix-v2:
* Instead of handling WBINVD #VE exception as nop, we skip its usage in currently enabled drivers.
* Adapted commit log for above change.arch/x86/kernel/tdx.c | 1 + drivers/acpi/acpica/hwsleep.c | 12 +++++++++--- drivers/acpi/sleep.c | 26 +++++++++++++++++++++++--- include/linux/protected_guest.h | 2 ++ 4 files changed, 35 insertions(+), 6 deletions(-)arch/x86/kernel/tdx.c +1
-0@@ -100,6 +100,7 @@ bool tdx_protected_guest_has(unsigned long flag)case PR_GUEST_MEM_ENCRYPT_ACTIVE :case PR_GUEST_UNROLL_STRING_IO :case PR_GUEST_SHARED_MAPPING_INIT : +case PR_GUEST_DISABLE_WBINVD :return true; }drivers/acpi/acpica/hwsleep.c +1
-1@@ -9,6 +9,7 @@*****************************************************************************/ #include <acpi/acpi.h> +#include <linux/protected_guest.h> #include "accommon.h" #define _COMPONENT ACPI_HARDWARE@@ -108,9 +109,14 @@ acpi_status acpi_hw_legacy_sleep(u8 sleep_state) pm1a_control |= sleep_enable_reg_info->access_bit_mask; pm1b_control |= sleep_enable_reg_info->access_bit_mask; -/* Flush caches, as per ACPI specification */
drivers/acpi/acpica/hwsleep.c acpi_status acpi_hw_legacy_sleep(u8 sleep_state) · line 119
Quoted 10 lines
drivers/acpi/acpica/hwsleep.c +8
-2- -ACPI_FLUSH_CPU_CACHE (); +/* +* WBINVD instruction is not supported in TDX +* guest. Since ACPI_FLUSH_CPU_CACHE() uses +* WBINVD, skip cache flushes for TDX guests. +*/ +if (prot_guest_has (PR_GUEST_DISABLE_WBINVD )) +/* Flush caches, as per ACPI specification */ +ACPI_FLUSH_CPU_CACHE ();
ACPICA uses OS abstractions like ACPI_FLUSH_CPU_CACHE and Linux patches rarely (never?) change ACPICA directly. If you want to change ACPICA it goes through the ACPICA project first and is then "Linux-ized", but in this case I believe you do not need to go that path. Instead, this wants to change the definition of ACPI_FLUSH_CPU_CACHE() directly in arch/x86/include/asm/acenv.h and explain why the other ACPI cache flushing paths / requirements do not apply to TDX guests.
Re: [RFC v2-fix-v3 1/1] x86/tdx: Skip WBINVD instruction for TDX guest
Quoted 10 lines · Dan Williams
Quoted 4 lines
Persistent memory is also currently not supported. Another code path that uses WBINVD is the MTRR driver, but EPT/virtualization always disables MTRRs so those are not needed. This all implies WBINVD is not needed with current TDX.
Let's drop the last three paragraphs and just say something like:
"This is one of a series of patches to usages of wbinvd for protected guests. For now this just addresses the one known path that TDX executes, ACPI reboot. Its usage can be elided because FOO reason and all the otherACPI_FLUSH_CPU_CACHEusages can be elided because BAR reason"
A better effort at transparency can be made here:
This patches the one WBINVD instance which has been encountered in practice: ACPI reboot. Assume no other instance will be encountered.
Re: [RFC v2-fix-v3 1/1] x86/tdx: Skip WBINVD instruction for TDX guest
Quoted 13 lines · Dave Hansen
Quoted 10 lines · Dan Williams
Quoted 4 lines
Persistent memory is also currently not supported. Another code path that uses WBINVD is the MTRR driver, but EPT/virtualization always disables MTRRs so those are not needed. This all implies WBINVD is not needed with current TDX.
Let's drop the last three paragraphs and just say something like:
"This is one of a series of patches to usages of wbinvd for protected guests. For now this just addresses the one known path that TDX executes, ACPI reboot. Its usage can be elided because FOO reason and all the otherACPI_FLUSH_CPU_CACHEusages can be elided because BAR reason"
Quoted 6 lines
A better effort at transparency can be made here:
This patches the one WBINVD instance which has been encountered in practice: ACPI reboot. Assume no other instance will be encountered.
That works too, but I assume if ACPI_FLUSH_CPU_CACHE() itself is going to be changed rather than sprinkling protected_guest_has() checks in a few places it will need to assert why changing all of those at once is correct. Otherwise I expect Rafael to ask why this global change of the ACPI_FLUSH_CPU_CACHE() policy is ok.
Re: [RFC v2-fix-v3 1/1] x86/tdx: Skip WBINVD instruction for TDX guest
Quoted 5 lines · Dan Williams
That works too, but I assume if
ACPI_FLUSH_CPU_CACHE()itself is going to be changed rather than sprinklingprotected_guest_has()checks in a few places it will need to assert why changing all of those at once is correct. Otherwise I expect Rafael to ask why this global change of theACPI_FLUSH_CPU_CACHE()policy is ok.
Yes. I am fixing it as below.
arch/x86/include/asm/acenv.h +6 -2
@@ -10,10 +10,15 @@#define _ASM_X86_ACENV_H#include <asm/special_insns.h> +#include <asm/protected_guest.h> /* Asm macros */ -#define ACPI_FLUSH_CPU_CACHE () wbinvd() +#define ACPI_FLUSH_CPU_CACHE () \ +do { \ + if (!prot_guest_has(PR_GUEST_DISABLE_WBINVD)) \ + wbinvd(); \ +} while (0) --
Sathyanarayanan Kuppuswamy
Linux Kernel Developer
RFC 1/1 x86/tdx: Skip WBINVD instruction for TDX guest
In reply to Dan WilliamsCurrent TDX spec does not have support to emulate the WBINVD instruction. If any feature that uses WBINVD is enabled/used in TDX guest, it will lead to un-handled #VE exception, which will be handled as #GP fault.
ACPI drivers also uses WBINVD instruction for cache flushes in reboot or shutdown code path. Since TDX guest has requirement to support shutdown feature, skip WBINVD instruction usage in ACPI drivers for TDX guest.
Since cache is always coherent in TDX guests, making wbinvd as noop should not cause any issues in above mentioned code path. The end-behavior is the same as KVM guest (treat as noops).
In future, once TDX guest specification adds support for WBINVD hypercall, we can pass the handle to KVM to handle it.
---
Changes since RFC v2-fix-v3:
* Fixed commit log as per review comments.
* Instead of fixing all usages of ACPI_FLUSH_CPU_CACHE(), created TDX specific exception for it in its implementation.
Changes since RFC v2-fix-v2:
* Instead of handling WBINVD #VE exception as nop, we skip its usage in currently enabled drivers.
* Adapted commit log for above change.
arch/x86/include/asm/acenv.h | 7 ++++++- arch/x86/kernel/tdx.c | 1 + include/linux/protected_guest.h | 2 ++ 3 files changed, 9 insertions(+), 1 deletion(-)
arch/x86/include/asm/acenv.h +6 -1
@@ -10,10 +10,15 @@#define _ASM_X86_ACENV_H#include <asm/special_insns.h> +#include <linux/protected_guest.h> /* Asm macros */ -#define ACPI_FLUSH_CPU_CACHE () wbinvd() +#define ACPI_FLUSH_CPU_CACHE () \ +do { \ + if (!prot_guest_has(PR_GUEST_DISABLE_WBINVD)) \ + wbinvd(); \ +} while (0)int __acpi_acquire_global_lock (unsigned int *lock);int __acpi_release_global_lock (unsigned int *lock);
arch/x86/kernel/tdx.c +1 -0
@@ -92,6 +92,7 @@ bool tdx_protected_guest_has(unsigned long flag)case PR_GUEST_MEM_ENCRYPT_ACTIVE :case PR_GUEST_UNROLL_STRING_IO :case PR_GUEST_SHARED_MAPPING_INIT : +case PR_GUEST_DISABLE_WBINVD :return true; }
include/linux/protected_guest.h +2 -0
@@ -18,6 +18,8 @@#define PR_GUEST_HOST_MEM_ENCRYPT 0x103/* Support for shared mapping initialization (after early init) */ #define PR_GUEST_SHARED_MAPPING_INIT 0x104 +/* Support to disable WBINVD */ +#define PR_GUEST_DISABLE_WBINVD 0x105#if defined(CONFIG_INTEL_TDX_GUEST ) || defined(CONFIG_AMD_MEM_ENCRYPT )
Signature · 1 line
2.25.1
Re: [RFC v2-fix-v4 1/1] x86/tdx: Skip WBINVD instruction for TDX guest
Quoted 10 lines · Kuppuswamy Sathyanarayanan
Current TDX spec does not have support to emulate the WBINVD instruction. If any feature that uses WBINVD is enabled/used in TDX guest, it will lead to un-handled #VE exception, which will be handled as #GP fault.
ACPI drivers also uses WBINVD instruction for cache flushes in reboot or shutdown code path. Since TDX guest has requirement to support shutdown feature, skip WBINVD instruction usage in ACPI drivers for TDX guest.
This sounds awkward...
Quoted 1 line
Since cache is always coherent in TDX guests, making wbinvd as
This is incorrect, ACPI cache flushing is not about I/O or CPU coherency...
Quoted 1 line
noop should not cause any issues in above mentioned code path.
..."should" is a famous last word...
Quoted 1 line
The end-behavior is the same as KVM guest (treat as noops).
..."KVM gets away with it" is not a justification that TDX can stand on otherwise we would not be here fixing up ACPICA properly.
How about:
"TDX guests use standard ACPI mechanisms to signal sleep state entry (including reboot) to the host. The ACPI specification mandates WBINVD on any sleep state entry with the expectation that the platform is only responsible for maintaining the state of memory over sleep states, not preserving dirty data in any CPU caches. ACPI cache flushing requirements pre-date the advent of virtualization. Given TDX guest sleep state entry does not affect any host power rails it is not required to flush caches. The host is responsible for maintaining cache state over its own bare metal sleep state transitions that power-off the cache. If the host fails to manage caches over its sleep state transitions the guest..."
I don't know how to finish the last sentence. What does TDX do if it is resumed after host suspend and the host somehow arranged for dirty TDX lines to be lost. Will that be noticed by TDX integrity mechanisms? I did not immediately find an answer to this with a brief look at the specs.
Quoted 3 lines
In future, once TDX guest specification adds support for WBINVD hypercall, we can pass the handle to KVM to handle it.
I expect if the specification wanted operating systems to plan for this eventuality it would have made a note of it. I expect this sentence can just be deleted.
Re: [RFC v2-fix-v4 1/1] x86/tdx: Skip WBINVD instruction for TDX guest
Quoted 18 lines · Dan Williams
..."KVM gets away with it" is not a justification that TDX can stand on otherwise we would not be here fixing up ACPICA properly.
How about:
"TDX guests use standard ACPI mechanisms to signal sleep state entry (including reboot) to the host. The ACPI specification mandates WBINVD on any sleep state entry with the expectation that the platform is only responsible for maintaining the state of memory over sleep states, not preserving dirty data in any CPU caches. ACPI cache flushing requirements pre-date the advent of virtualization. Given TDX guest sleep state entry does not affect any host power rails it is not required to flush caches. The host is responsible for maintaining cache state over its own bare metal sleep state transitions that power-off the cache. If the host fails to manage caches over its sleep state transitions the guest..."
Quoted 3 lines
I don't know how to finish the last sentence. What does TDX do if it is resumed after host suspend and the host somehow arranged for dirty TDX lines to be lost.
TDX guest does not support S3. It will be disabled in ACPI tables. It is a TDX firmware spec requirement. Please check the following spec, sec 2.1
In TDX guest, we encounter cache flushes only in shutdown and reboot path. So there is no resume path.
Will that be noticed by TDX integrity
Quoted 2 lines
mechanisms? I did not immediately find an answer to this with a brief look at the specs.
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Re: [RFC v2-fix-v4 1/1] x86/tdx: Skip WBINVD instruction for TDX guest
Quoted 26 lines · Sathyanarayanan
Quoted 18 lines · Dan Williams
..."KVM gets away with it" is not a justification that TDX can stand on otherwise we would not be here fixing up ACPICA properly.
How about:
"TDX guests use standard ACPI mechanisms to signal sleep state entry (including reboot) to the host. The ACPI specification mandates WBINVD on any sleep state entry with the expectation that the platform is only responsible for maintaining the state of memory over sleep states, not preserving dirty data in any CPU caches. ACPI cache flushing requirements pre-date the advent of virtualization. Given TDX guest sleep state entry does not affect any host power rails it is not required to flush caches. The host is responsible for maintaining cache state over its own bare metal sleep state transitions that power-off the cache. If the host fails to manage caches over its sleep state transitions the guest..."
Quoted 3 lines
I don't know how to finish the last sentence. What does TDX do if it is resumed after host suspend and the host somehow arranged for dirty TDX lines to be lost.
Quoted 5 lines
TDX guest does not support S3. It will be disabled in ACPI tables. It is a TDX firmware spec requirement. Please check the following spec, sec 2.1
I'm not asking about TDX guest entering S3...
Quoted 3 lines
In TDX guest, we encounter cache flushes only in shutdown and reboot path. So there is no resume path.
Host is free to go into S3 independent of any guest state. A hostile host is free to do just enough cache management so that it can resume from S3 while arranging for TDX guest dirty data to be lost. Does a TDX guest go fatal if the cache loses power?
Re: [RFC v2-fix-v4 1/1] x86/tdx: Skip WBINVD instruction for TDX guest
here is no resume path.
Quoted 1 line
Host is free to go into S3 independent of any guest state.
Actually my understanding is that none of the systems which support TDX support S3. S3 has been deprecated for a long time.
Quoted 4 lines
A hostile
host is free to do just enough cache management so that it can resume from S3 while arranging for TDX guest dirty data to be lost. Does a TDX guest go fatal if the cache loses power?
That would be a machine check, and yes it would be fatal.
-Andi
Re: [RFC v2-fix-v4 1/1] x86/tdx: Skip WBINVD instruction for TDX guest
Quoted 8 lines · Andi Kleen
here is no resume path.
Quoted 1 line
Host is free to go into S3 independent of any guest state.
Actually my understanding is that none of the systems which support TDX support S3. S3 has been deprecated for a long time.
Ok, I wanted to imply any power state that might power-off caches.
Quoted 8 lines
Quoted 6 lines
A hostile
host is free to do just enough cache management so that it can resume from S3 while arranging for TDX guest dirty data to be lost. Does a TDX guest go fatal if the cache loses power?That would be a machine check, and yes it would be fatal.
Sounds good, so incorporating this and Andy's feedback:
"TDX guests, like other typical guests, use standard ACPI mechanisms to signal sleep state entry (including reboot) to the host. The ACPI specification mandates WBINVD on any sleep state entry with the expectation that the platform is only responsible for maintaining the state of memory over sleep states, not preserving dirty data in any CPU caches. ACPI cache flushing requirements pre-date the advent of virtualization. Given guest sleep state entry does not affect any host power rails it is not required to flush caches. The host is responsible for maintaining cache state over its own bare metal sleep state transitions that power-off the cache. A TDX guest, unlike a typical guest, will machine check if the CPU cache is powered off."
Andi, is that machine check behavior relative to power states mentioned in the docs?
Andy Lutomirski
maintainerRe: [RFC v2-fix-v4 1/1] x86/tdx: Skip WBINVD instruction for TDX guest
Quoted 35 lines · Dan Williams
Quoted 8 lines · Andi Kleen
here is no resume path.
Quoted 1 line
Host is free to go into S3 independent of any guest state.
Actually my understanding is that none of the systems which support TDX support S3. S3 has been deprecated for a long time.
Ok, I wanted to imply any power state that might power-off caches.
Quoted 8 lines
Quoted 6 lines
A hostile
host is free to do just enough cache management so that it can resume from S3 while arranging for TDX guest dirty data to be lost. Does a TDX guest go fatal if the cache loses power?That would be a machine check, and yes it would be fatal.
Sounds good, so incorporating this and Andy's feedback:
"TDX guests, like other typical guests, use standard ACPI mechanisms to signal sleep state entry (including reboot) to the host. The ACPI specification mandates WBINVD on any sleep state entry with the expectation that the platform is only responsible for maintaining the state of memory over sleep states, not preserving dirty data in any CPU caches. ACPI cache flushing requirements pre-date the advent of virtualization. Given guest sleep state entry does not affect any host power rails it is not required to flush caches. The host is responsible for maintaining cache state over its own bare metal sleep state transitions that power-off the cache. A TDX guest, unlike a typical guest, will machine check if the CPU cache is powered off."
Quoted 2 lines
Andi, is that machine check behavior relative to power states mentioned in the docs?
I don't think there's anything about power states. There is a general documented mechanism to integrity-check TD guest memory, but it is *not* replay-resistant. So, if the guest dirties a cache line, and the cache line is lost, it seems entirely plausible that the guest would get silently corrupted.
I would argue that, if this happens, it's a host, TD module, or architecture bug, and it's not the guest's fault.
--Andy
Re: [RFC v2-fix-v4 1/1] x86/tdx: Skip WBINVD instruction for TDX guest
Quoted 39 lines · Andy Lutomirski
Quoted 35 lines · Dan Williams
Quoted 8 lines · Andi Kleen
here is no resume path.
Quoted 1 line
Host is free to go into S3 independent of any guest state.
Actually my understanding is that none of the systems which support TDX support S3. S3 has been deprecated for a long time.
Ok, I wanted to imply any power state that might power-off caches.
Quoted 8 lines
Quoted 6 lines
A hostile
host is free to do just enough cache management so that it can resume from S3 while arranging for TDX guest dirty data to be lost. Does a TDX guest go fatal if the cache loses power?That would be a machine check, and yes it would be fatal.
Sounds good, so incorporating this and Andy's feedback:
"TDX guests, like other typical guests, use standard ACPI mechanisms to signal sleep state entry (including reboot) to the host. The ACPI specification mandates WBINVD on any sleep state entry with the expectation that the platform is only responsible for maintaining the state of memory over sleep states, not preserving dirty data in any CPU caches. ACPI cache flushing requirements pre-date the advent of virtualization. Given guest sleep state entry does not affect any host power rails it is not required to flush caches. The host is responsible for maintaining cache state over its own bare metal sleep state transitions that power-off the cache. A TDX guest, unlike a typical guest, will machine check if the CPU cache is powered off."
Quoted 2 lines
Andi, is that machine check behavior relative to power states mentioned in the docs?
Quoted 8 lines
I don't think there's anything about power states. There is a general documented mechanism to integrity-check TD guest memory, but it is *not* replay-resistant. So, if the guest dirties a cache line, and the cache line is lost, it seems entirely plausible that the guest would get silently corrupted.
I would argue that, if this happens, it's a host, TD module, or architecture bug, and it's not the guest's fault.
If you want to apply this fix for all hypervisors (using boot_cpu_has (X86_FEATURE_HYPERVISOR) check), then we don't need any TDX specific reference in commit log right? It can be generalized for all VM guests.
agree?
Quoted 3 lines
--Andy
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Re: [RFC v2-fix-v4 1/1] x86/tdx: Skip WBINVD instruction for TDX guest
Quoted 49 lines · Sathyanarayanan
Quoted 45 lines · Andy Lutomirski
Quoted 35 lines · Dan Williams
Quoted 8 lines · Andi Kleen
here is no resume path.
Quoted 1 line
Host is free to go into S3 independent of any guest state.
Actually my understanding is that none of the systems which support TDX support S3. S3 has been deprecated for a long time.
Ok, I wanted to imply any power state that might power-off caches.
Quoted 8 lines
Quoted 6 lines
A hostile
host is free to do just enough cache management so that it can resume from S3 while arranging for TDX guest dirty data to be lost. Does a TDX guest go fatal if the cache loses power?That would be a machine check, and yes it would be fatal.
Sounds good, so incorporating this and Andy's feedback:
"TDX guests, like other typical guests, use standard ACPI mechanisms to signal sleep state entry (including reboot) to the host. The ACPI specification mandates WBINVD on any sleep state entry with the expectation that the platform is only responsible for maintaining the state of memory over sleep states, not preserving dirty data in any CPU caches. ACPI cache flushing requirements pre-date the advent of virtualization. Given guest sleep state entry does not affect any host power rails it is not required to flush caches. The host is responsible for maintaining cache state over its own bare metal sleep state transitions that power-off the cache. A TDX guest, unlike a typical guest, will machine check if the CPU cache is powered off."
Quoted 2 lines
Andi, is that machine check behavior relative to power states mentioned in the docs?
I don't think there's anything about power states. There is a general documented mechanism to integrity-check TD guest memory, but it is *not* replay-resistant. So, if the guest dirties a cache line, and the cache line is lost, it seems entirely plausible that the guest would get silently corrupted.
Quoted 8 lines
Quoted 2 lines
I would argue that, if this happens, it's a host, TD module, or architecture bug, and it's not the guest's fault.
If you want to apply this fix for all hypervisors (using
boot_cpu_has(X86_FEATURE_HYPERVISOR) check), then we don't need any TDX specific reference in commit log right? It can be generalized for all VM guests.agree?
No, because there is a note needed about the integrity implications in the TDX case that makes it distinct from typical hypervisor enabling.
Re: [RFC v2-fix-v4 1/1] x86/tdx: Skip WBINVD instruction for TDX guest
Quoted 7 lines · Dan Williams
Quoted 5 lines
If you want to apply this fix for all hypervisors (using
boot_cpu_has(X86_FEATURE_HYPERVISOR) check), then we don't need any TDX specific reference in commit log right? It can be generalized for all VM guests.agree?
No, because there is a note needed about the integrity implications in the TDX case that makes it distinct from typical hypervisor enabling.
Generalized the commit log (but left the TDX related info). Final version will look like below.
x86: Skip WBINVD instruction for VM guest
VM guests that supports ACPI, use standard ACPI mechanisms to signal sleep state entry (including reboot) to the host. The ACPI specification mandates WBINVD on any sleep state entry with the expectation that the platform is only responsible for maintaining the state of memory over sleep states, not preserving dirty data in any CPU caches. ACPI cache flushing requirements pre-date the advent of virtualization. Given guest sleep state entry does not affect any host power rails it is not required to flush caches. The host is responsible for maintaining cache state over its own bare metal sleep state transitions that power-off the cache. A TDX guest, unlike a typical guest, will machine check if the CPU cache is powered off.
arch/x86/include/asm/acenv.h +6 -1
@@ -10,10 +10,15 @@#define _ASM_X86_ACENV_H#include <asm/special_insns.h> +#include <asm/cpu.h> /* Asm macros */ -#define ACPI_FLUSH_CPU_CACHE () wbinvd() +#define ACPI_FLUSH_CPU_CACHE () \ +do { \ + if (!boot_cpu_has(X86_FEATURE_HYPERVISOR)) \ + wbinvd(); \ +} while (0)int __acpi_acquire_global_lock (unsigned int *lock);int __acpi_release_global_lock (unsigned int *lock);
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
RFC 1/1 x86: Skip WBINVD instruction for VM guest
VM guests that supports ACPI, use standard ACPI mechanisms to signal sleep state entry (including reboot) to the host. The ACPI specification mandates WBINVD on any sleep state entry with the expectation that the platform is only responsible for maintaining the state of memory over sleep states, not preserving dirty data in any CPU caches. ACPI cache flushing requirements pre-date the advent of virtualization. Given guest sleep state entry does not affect any host power rails it is not required to flush caches. The host is responsible for maintaining cache state over its own bare metal sleep state transitions that power-off the cache. A TDX guest, unlike a typical guest, will machine check if the CPU cache is powered off.
---
Changes since RFC v2-fix-v4:
* Fixed commit log as per Dan's comments.
* Used boot_cpu_has(X86_FEATURE_HYPERVISOR) instead of prot_guest_has(PR_GUEST_DISABLE_WBINVD) check.
Changes since RFC v2-fix-v3:
* Fixed commit log as per review comments.
* Instead of fixing all usages of ACPI_FLUSH_CPU_CACHE(), created TDX specific exception for it in its implementation.
Changes since RFC v2-fix-v2:
* Instead of handling WBINVD #VE exception as nop, we skip its usage in currently enabled drivers.
* Adapted commit log for above change.
arch/x86/include/asm/acenv.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
arch/x86/include/asm/acenv.h +6 -1
@@ -10,10 +10,15 @@#define _ASM_X86_ACENV_H#include <asm/special_insns.h> +#include <asm/cpu.h> /* Asm macros */ -#define ACPI_FLUSH_CPU_CACHE () wbinvd() +#define ACPI_FLUSH_CPU_CACHE () \ +do { \ + if (!boot_cpu_has(X86_FEATURE_HYPERVISOR)) \ + wbinvd(); \ +} while (0)int __acpi_acquire_global_lock (unsigned int *lock);int __acpi_release_global_lock (unsigned int *lock);
Signature · 1 line
2.25.1
Re: [RFC v2-fix-v5 1/1] x86: Skip WBINVD instruction for VM guest
[ add back linux-acpi and Rafael ]
Quoted 13 lines · Kuppuswamy Sathyanarayanan
VM guests that supports ACPI, use standard ACPI mechanisms to signal sleep state entry (including reboot) to the host. The ACPI specification mandates WBINVD on any sleep state entry with the expectation that the platform is only responsible for maintaining the state of memory over sleep states, not preserving dirty data in any CPU caches. ACPI cache flushing requirements pre-date the advent of virtualization. Given guest sleep state entry does not affect any host power rails it is not required to flush caches. The host is responsible for maintaining cache state over its own bare metal sleep state transitions that power-off the cache. A TDX guest, unlike a typical guest, will machine check if the CPU cache is powered off.
Looks like you are wrapping at column 62 than 72, double check that for the final submission of this series. Other than that this looks good to me.
Re: [RFC v2-fix-v5 1/1] x86: Skip WBINVD instruction for VM guest
In reply to Kuppuswamy SathyanarayananThis changelog lacks both clear problem statements and a clear solution implemented within the patch.
Here's a proposed changelog. It clearly spells out the two problems caused by WBINVD within a guest, and the proposed solution which fixes those two problems.
Is this missing anything?
--
VM guests that support ACPI use standard ACPI mechanisms to signal sleep state entry to the host. To ACPI, reboot is simply another sleep state.
ACPI specifies that the platform preserve memory contents over (some) sleep states. It does not specify any requirements for data preservation in CPU caches. The ACPI specification mandates the use of WBINVD to flush the contents of the CPU caches to memory before entering specific sleep states, thus ensuring data in caches can survive sleep state transitions.e
Unlike when entering sleep states bare metal, no actions within a guest can cause data in processor caches to be lost. That makes these WBINVD invocations harmless but superfluous within a guest. (<--- problem #1)
In TDX guests, these WBINVD operations cause #VE exceptions. For debug, it would be ideal for the #VE handler to be able to WARN() when an unexpected WBINVD occurs. (<--- problem #2)
Avoid WBINVD for all ACPI cache-flushing operations which occur while running under a hypervisor, which includes TDX guests. This both avoids TDX warnings and optimizes away superfluous WBINVD invocations. (<---- solution)
Re: [RFC v2-fix-v5 1/1] x86: Skip WBINVD instruction for VM guest
Quoted 7 lines · Dave Hansen
This changelog lacks both clear problem statements and a clear solution implemented within the patch.
Here's a proposed changelog. It clearly spells out the two problems caused by WBINVD within a guest, and the proposed solution which fixes those two problems.
Looks good to me modulo the comment below...
Quoted 15 lines
Is this missing anything?
--
VM guests that support ACPI use standard ACPI mechanisms to signal sleep state entry to the host. To ACPI, reboot is simply another sleep state.
ACPI specifies that the platform preserve memory contents over (some) sleep states. It does not specify any requirements for data preservation in CPU caches. The ACPI specification mandates the use of WBINVD to flush the contents of the CPU caches to memory before entering specific sleep states, thus ensuring data in caches can survive sleep state transitions.e
Quoted 7 lines
Unlike when entering sleep states bare metal, no actions within a guest can cause data in processor caches to be lost. That makes these WBINVD invocations harmless but superfluous within a guest. (<--- problem #1)
In TDX guests, these WBINVD operations cause #VE exceptions. For debug, it would be ideal for the #VE handler to be able to
WARN()when an unexpected WBINVD occurs. (<--- problem #2)
...but it doesn't WARN() it triggers unhandled #VE, unless I missed another patch that precedes this that turns it into a WARN()? If a code path expects WBINVD for correct operation and the guest can't execute that sounds fatal, not a WARN to me.
Quoted 5 lines
Avoid WBINVD for all ACPI cache-flushing operations which occur while running under a hypervisor, which includes TDX guests. This both avoids TDX warnings and optimizes away superfluous WBINVD invocations. (<---- solution)
Re: [RFC v2-fix-v5 1/1] x86: Skip WBINVD instruction for VM guest
Quoted 7 lines · Dan Williams
Quoted 3 lines
In TDX guests, these WBINVD operations cause #VE exceptions. For debug, it would be ideal for the #VE handler to be able to
WARN()when an unexpected WBINVD occurs. (<--- problem #2)...but it doesn't
WARN()it triggers unhandled #VE, unless I missed another patch that precedes this that turns it into aWARN()? If a code path expects WBINVD for correct operation and the guest can't execute that sounds fatal, not a WARN to me.
Yes. It is not WARN. It is a fatal unhandled exception.
Quoted 1 line
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Re: [RFC v2-fix-v5 1/1] x86: Skip WBINVD instruction for VM guest
Quoted 10 lines · Sathyanarayanan
Quoted 7 lines · Dan Williams
Quoted 3 lines
In TDX guests, these WBINVD operations cause #VE exceptions. For debug, it would be ideal for the #VE handler to be able to
WARN()when an unexpected WBINVD occurs. (<--- problem #2)...but it doesn't
WARN()it triggers unhandled #VE, unless I missed another patch that precedes this that turns it into aWARN()? If a code path expects WBINVD for correct operation and the guest can't execute that sounds fatal, not a WARN to me.Yes. It is not WARN. It is a fatal unhandled exception.
That makes the problem statement a wee bit different, but it should still be pretty easy to explain:
In TDX guests, these WBINVD operations cause #VE exceptions. While some #VE exceptions can be handled, there is no recourse for a TDX guest to handle a WBINVD and it will panic(). (<--- problem #2)
Andy Lutomirski
maintainerRe: [RFC v2-fix-v4 1/1] x86/tdx: Skip WBINVD instruction for TDX guest
In reply to Dan WilliamsQuoted 30 lines · Dan Williams
Quoted 10 lines · Kuppuswamy Sathyanarayanan
Current TDX spec does not have support to emulate the WBINVD instruction. If any feature that uses WBINVD is enabled/used in TDX guest, it will lead to un-handled #VE exception, which will be handled as #GP fault.
ACPI drivers also uses WBINVD instruction for cache flushes in reboot or shutdown code path. Since TDX guest has requirement to support shutdown feature, skip WBINVD instruction usage in ACPI drivers for TDX guest.
This sounds awkward...
Quoted 1 line
Since cache is always coherent in TDX guests, making wbinvd as
This is incorrect, ACPI cache flushing is not about I/O or CPU coherency...
Quoted 1 line
noop should not cause any issues in above mentioned code path.
..."should" is a famous last word...
Quoted 1 line
The end-behavior is the same as KVM guest (treat as noops).
..."KVM gets away with it" is not a justification that TDX can stand on otherwise we would not be here fixing up ACPICA properly.
How about:
Quoted 12 lines
"TDX guests use standard ACPI mechanisms to signal sleep state entry (including reboot) to the host. The ACPI specification mandates WBINVD on any sleep state entry with the expectation that the platform is only responsible for maintaining the state of memory over sleep states, not preserving dirty data in any CPU caches. ACPI cache flushing requirements pre-date the advent of virtualization. Given TDX guest sleep state entry does not affect any host power rails it is not required to flush caches. The host is responsible for maintaining cache state over its own bare metal sleep state transitions that power-off the cache. If the host fails to manage caches over its sleep state transitions the guest..."
I like this description, but shouldn't the logic be:
if (!CPUID has hypervisor bit set)
wbinvd();
As far as I know, most hypervisors will turn WBINVD into a noop and, even if they don't, it seems to be that something must be really quite wrong for a guest to need to WBINVD for ACPI purposes.
-Andy
Re: [RFC v2-fix-v4 1/1] x86/tdx: Skip WBINVD instruction for TDX guest
Quoted 45 lines · Andy Lutomirski
Quoted 30 lines · Dan Williams
Quoted 10 lines · Kuppuswamy Sathyanarayanan
Current TDX spec does not have support to emulate the WBINVD instruction. If any feature that uses WBINVD is enabled/used in TDX guest, it will lead to un-handled #VE exception, which will be handled as #GP fault.
ACPI drivers also uses WBINVD instruction for cache flushes in reboot or shutdown code path. Since TDX guest has requirement to support shutdown feature, skip WBINVD instruction usage in ACPI drivers for TDX guest.
This sounds awkward...
Quoted 1 line
Since cache is always coherent in TDX guests, making wbinvd as
This is incorrect, ACPI cache flushing is not about I/O or CPU coherency...
Quoted 1 line
noop should not cause any issues in above mentioned code path.
..."should" is a famous last word...
Quoted 1 line
The end-behavior is the same as KVM guest (treat as noops).
..."KVM gets away with it" is not a justification that TDX can stand on otherwise we would not be here fixing up ACPICA properly.
How about:
Quoted 12 lines
"TDX guests use standard ACPI mechanisms to signal sleep state entry (including reboot) to the host. The ACPI specification mandates WBINVD on any sleep state entry with the expectation that the platform is only responsible for maintaining the state of memory over sleep states, not preserving dirty data in any CPU caches. ACPI cache flushing requirements pre-date the advent of virtualization. Given TDX guest sleep state entry does not affect any host power rails it is not required to flush caches. The host is responsible for maintaining cache state over its own bare metal sleep state transitions that power-off the cache. If the host fails to manage caches over its sleep state transitions the guest..."
Quoted 8 lines
I like this description, but shouldn't the logic be:
if (!CPUID has hypervisor bit set)
wbinvd();As far as I know, most hypervisors will turn WBINVD into a noop and, even if they don't, it seems to be that something must be really quite wrong for a guest to need to WBINVD for ACPI purposes.
Agree, a well behaved guest should not pretend its callouts to the virtual ACPI BIOS actually affect a host power rail.
Re: [RFC v2-fix-v4 1/1] x86/tdx: Skip WBINVD instruction for TDX guest
In reply to Andy LutomirskiQuoted 9 lines
I like this description, but shouldn't the logic be:
if (!CPUID has hypervisor bit set)
wbinvd();As far as I know, most hypervisors will turn WBINVD into a noop and, even if they don't, it seems to be that something must be really quite wrong for a guest to need to WBINVD for ACPI purposes.
KVM only turns it into a noop if there is no VT-d, because with VT-d you might need it to turn mappings into uncached and vice versa.
But yes the change would make sense for reboot. BTW I suspect for the reboot path it isn't really needed anywhere modern, so it might actually be ok to completely disable it. But that's some risk, so doing it only for hypervisor is reasonable.
I can see it making sense for the S3 path, but nobody supports S3 for guests.
-Andi
Quoted 2 lines
-Andy
Andy Lutomirski
maintainerRe: [RFC v2-fix-v4 1/1] x86/tdx: Skip WBINVD instruction for TDX guest
Quoted 6 lines · Andi Kleen
Quoted 6 lines
I like this description, but shouldn't the logic be:
if (!CPUID has hypervisor bit set)
wbinvd();
Quoted 6 lines
Quoted 3 lines
As far as I know, most hypervisors will turn WBINVD into a noop and, even if they don't, it seems to be that something must be really quite wrong for a guest to need to WBINVD for ACPI purposes.
KVM only turns it into a noop if there is no VT-d, because with VT-d you might need it to turn mappings into uncached and vice versa.
Wow, I found the kvm_arch_register_noncoherent_dma() stuff. That's horrifying. What's it for? Are there actually guests that use devices exposed by VFIO that expect WBINVD to work? That's a giant DoS hole.
Quoted 5 lines
But yes the change would make sense for reboot. BTW I suspect for the reboot path it isn't really needed anywhere modern, so it might actually be ok to completely disable it. But that's some risk, so doing it only for hypervisor is reasonable.
I agree.
Re: [RFC v2-fix-v4 1/1] x86/tdx: Skip WBINVD instruction for TDX guest
Quoted 4 lines
Quoted 3 lines
KVM only turns it into a noop if there is no VT-d, because with VT-d you might need it to turn mappings into uncached and vice versa.
Wow, I found the
kvm_arch_register_noncoherent_dma()stuff. That's horrifying. What's it for? e
e.g. if you want to run a GPU it really needs some uncached memory. Same is true for other more complex devices.
Now modern Linux of course will be preferring CLFLUSH instead for the conversion, but there are old versions that preferred WBINVD.
I don't think it's a DoS, as long as you're not too picky about latencies on the host.
-Andi
Re: [RFC v2-fix-v4 1/1] x86/tdx: Skip WBINVD instruction for TDX guest
Quoted 5 lines · Andi Kleen
Quoted 4 lines
Quoted 3 lines
KVM only turns it into a noop if there is no VT-d, because with VT-d you might need it to turn mappings into uncached and vice versa.
Wow, I found the
kvm_arch_register_noncoherent_dma()stuff. That's horrifying. What's it for? e
Quoted 8 lines
e.g. if you want to run a GPU it really needs some uncached memory. Same is true for other more complex devices.
Now modern Linux of course will be preferring CLFLUSH instead for the conversion, but there are old versions that preferred WBINVD.
I don't think it's a DoS, as long as you're not too picky about latencies on the host.
-Andi
Currently we use prot_guest_has(PR_GUEST_DISABLE_WBINVD)) check for disabling the wbinvd() usage (which can be selectively enabled for tested guests).
Is it alright to generalize it with boot_cpu_has(X86_FEATURE_HYPERVISOR) without verify it?
Quoted 2 lines
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Re: [RFC v2-fix-v4 1/1] x86/tdx: Skip WBINVD instruction for TDX guest
In reply to Kuppuswamy SathyanarayananQuoted 3 lines · Kuppuswamy Sathyanarayanan
Since cache is always coherent in TDX guests, making wbinvd as noop should not cause any issues in above mentioned code path. The end-behavior is the same as KVM guest (treat as noops).
I don't see anything in the specs to back up such a broad statement.
For Secure-EPT, I see in the TDX "EAS" that "Ignore PAT" is "Set to 1".
This, presumably along with the "TD VMCS Guest MSRs... IA32_PAT" being
set to 0x0007040600070406 (I didn't decode it, I'm just guessing), ensures that guests using Secure-EPT have no architectural way of creating non-coherent mappings using the guest x86 page tables.
That covers one of the memory types to which guests have access.
Guests can also access TD-shared memory. Those mappings are controlled by the VMM and not mapped by Secure-EPT. This is the part that concerns me and is not consistent with the statement above. Is it architecturally impossible for a VMM to create an non-coherent mapping and expose it to a guest? If it is impossible, please include citations of the spec or the logic behind this so that a reader can understand, just as I did above.
If it is possible to have non-coherent mappings in a guest, then please remove the above statement.
Re: [RFC v2-fix-v2 2/2] x86/tdx: Ignore WBINVD instruction for TDX guest
In reply to Andi KleenQuoted 3 lines · Andi Kleen
I don't see any point of all of this. We really just want to be the same as KVM. Not get into the business of patching a bazillion sub systems that cannot be used in TDX anyways.
Andi, there's a fundamental difference between KVM the hypervisor and a TDX guest: KVM the hypervisor runs unknown guests, and lots of them.
TD guest support as a whole has to handle one thing: running *one* Linux kernel. Further, the guest support shares a source tree with that kernel. TD guest support doesn't have to run random binaries for which there is no source. All of the source is *RIGHT* *THERE*.
The only reason TD guest support would have to fall back to KVM's dirty tricks is a desire to treat the rest of the kernel like a black box. KVM frankly has no other choice. TD guest support has all the choices in the world.
Re: [RFC v2-fix-v2 2/2] x86/tdx: Ignore WBINVD instruction for TDX guest
In reply to Dan Williamsthat.
Quoted 4 lines
I thought we discussed that there are other considerations for wbinvd besides DMA? In any event this paragraph is actively misleading because it disregards ACPI and Persistent Memory secure-erase whose usages of wbinvd have nothing to do with DMA.
In this case they would be broken in KVM too.
Quoted 7 lines
I would much prefer a
patch to shutdown all the known wbinvd users as a precursor to this patch rather than assuming it's ok to simply ignore it. You have mentioned that TDX does not need to use those paths, but rather than assume they can't be used why not do the audit to explicitly disable them? Otherwise this statement seems to imply that the audit has not been done.
We're not assuming it. We know it because KVM does it since forever.
All we want to do is do the same as KVM.
-Andi
Re: [RFC v2-fix-v2 2/2] x86/tdx: Ignore WBINVD instruction for TDX guest
In reply to Kuppuswamy SathyanarayananQuoted 2 lines · Kuppuswamy Sathyanarayanan
Functionally only DMA devices can notice a side effect from WBINVD's cache flushing.
This seems to be trying to make some kind of case that the only visible effects from WBINVD are for DMA devices. That's flat out wrong. It might be arguable that none of the other cases exist in a TDX guest, but it doesn't excuse making such a broad statement without qualification.
Just grep in the kernel for a bunch of reasons this is wrong.
Where did this come from?
Re: [RFC v2-fix-v2 2/2] x86/tdx: Ignore WBINVD instruction for TDX guest
Quoted 7 lines · Dave Hansen
Quoted 2 lines · Kuppuswamy Sathyanarayanan
Functionally only DMA devices can notice a side effect from WBINVD's cache flushing.
This seems to be trying to make some kind of case that the only visible effects from WBINVD are for DMA devices. That's flat out wrong. It might be arguable that none of the other cases exist in a TDX guest, but it doesn't excuse making such a broad statement without qualification.
We're describing a few sentences down that guests run with EPT IgnorePAT=1, which is the qualification.
Quoted 4 lines
Just grep in the kernel for a bunch of reasons this is wrong.
Where did this come from?
Again the logic is very simple: TDX guest code is (mostly) about replacing KVM code with in kernel code, so we're just doing the same as KVM. You cannot get any more proven than that.
-Andi
Re: [RFC v2-fix-v2 2/2] x86/tdx: Ignore WBINVD instruction for TDX guest
Quoted 15 lines · Andi Kleen
Quoted 7 lines · Dave Hansen
Quoted 2 lines · Kuppuswamy Sathyanarayanan
Functionally only DMA devices can notice a side effect from WBINVD's cache flushing.
This seems to be trying to make some kind of case that the only visible effects from WBINVD are for DMA devices. That's flat out wrong. It might be arguable that none of the other cases exist in a TDX guest, but it doesn't excuse making such a broad statement without qualification.
We're describing a few sentences down that guests run with EPT IgnorePAT=1, which is the qualification.
Quoted 1 line
Quoted 8 lines
Quoted 3 lines
Just grep in the kernel for a bunch of reasons this is wrong.
Where did this come from?
Again the logic is very simple: TDX guest code is (mostly) about replacing KVM code with in kernel code, so we're just doing the same as KVM. You cannot get any more proven than that.
I have no problem pointing at KVM as to why the risk is mitigated, but I do have a problem with misrepresenting the scope of the risk.
Re: [RFC v2-fix-v2 1/2] x86/tdx: Handle MWAIT and MONITOR
In reply to Kuppuswamy SathyanarayananQuoted 20 lines · Kuppuswamy Sathyanarayanan
When running as a TDX guest, there are a number of existing, privileged instructions that do not work. If the guest kernel uses these instructions, the hardware generates a #VE.
You can find the list of unsupported instructions in Intel Trust Domain Extensions (Intel® TDX) Module specification, sec 9.2.2 and in Guest-Host Communication Interface (GHCI) Specification for Intel TDX, sec 2.4.1.
To prevent TD guests from using MWAIT/MONITOR instructions, the CPUID flags for these instructions are already disabled by the TDX module.
After the above mentioned preventive measures, if TD guests still execute these instructions, add appropriate warning message (
WARN_ONCE()) in #VE handler. This handling behavior is same as KVM (which also treats MWAIT/MONITOR as nops with warning once in unsupported platforms).
Quoted 7 lines
Signed-off-by Kuppuswamy Sathyanarayanan <[email protected]>Reviewed-by Andi Kleen <[email protected]>---
Changes since RFC v2:
* Moved WBINVD related changes to a new patch.
* Fixed commit log as per review comments.
Looks good.
Re: [RFC v2 16/32] x86/tdx: Handle MWAIT, MONITOR and WBINVD
In reply to Dan WilliamsQuoted 7 lines · Dan Williams
Quoted 4 lines
To prevent TD guest from using MWAIT/MONITOR instructions, support for these instructions are already disabled by TDX module (SEAM). So CPUID flags for these instructions should be in disabled state.
Why does this not result in a #UD if the instruction is disabled by SEAM? How is it possible to execute a disabled instruction (one precluded by CPUID) to the point where it triggers #VE instead of #UD?
This is actually a vestige of VMX. It's quite possible toady to have a feature which isn't enumerated in CPUID which still exists and "works" in the silicon. There are all kinds of pitfalls to doing this, but folks evidently do it in public clouds all the time.
The CPUID virtualization basically just traps into the hypervisor and lets the hypervisor set whatever register values it wants to appear when CPUID "returns".
But, the controls for what instructions generate #UD are actually quite separate and unrelated to CPUID itself.
Sean Christopherson
maintainerRe: [RFC v2 16/32] x86/tdx: Handle MWAIT, MONITOR and WBINVD
Quoted 5 lines · Dave Hansen
Quoted 4 lines · Dan Williams
Quoted 4 lines
To prevent TD guest from using MWAIT/MONITOR instructions, support for these instructions are already disabled by TDX module (SEAM). So CPUID flags for these instructions should be in disabled state.
Quoted 7 lines
Quoted 3 lines
Why does this not result in a #UD if the instruction is disabled by SEAM? How is it possible to execute a disabled instruction (one precluded by CPUID) to the point where it triggers #VE instead of #UD?
This is actually a vestige of VMX. It's quite possible toady to have a feature which isn't enumerated in CPUID which still exists and "works" in the silicon.
No, virtualization holes are something else entirely.
MONITOR/MWAIT are a bit weird; they do have an enable bit in IA32_MISC_ENABLE, but most VMMs don't context switch IA32_MISC_ENABLE (load guest value on entry, load host value on exit) because that would add ~250 cycles to every host<->guest transition. And IA32_MISC_ENABLE is shared between SMT siblings, which further complicates loading the guest's value into hardware. In the end, it's easier to leave MONITOR/MWAIT enabled in hardware and instead force a VM-Exit.
As for why TDX injects #VE instead of #UD, I suspect it's for the same reason that KVM emulates MONITOR/MWAIT as nops instead of injecting a #UD. The CPUID bit for MONITOR/MWAIT reflects their enabling in IA32_MISC_ENABLE, not raw support in hardware. That means there's no definitive way to enumerate to BIOS that MONITOR/MWAIT are not supported, e.g. AFAICT, EDKII blindly assumes it can enable MONITOR/MWAIT in IA32_MISC_ENABLE. To justify #UD instead of #VE, TDX would have to inject #GP on WRMSR to set IA32_MISC_ENABLE.ENABLE_MONITOR, and even then there would be weirdness with respect to VMM behavior in response to TDVMCALL(WRMSR) since the VMM could allow the virtual write. In the end, it's again simpler to inject #VE.
Quoted 2 lines
There are all kinds of pitfalls to doing this, but folks evidently do it in public clouds all the time.
Virtualization holes are when instructions/features are enumerated via CPUID, but don't have a control to hide the feature from the guest (or in the case of CET, multiple feature are buried behind a single control). So even if the VMM hides the feature via CPUID, the guest can still _cleanly_ execute the instruction if it's supported by the underlying hardware.
Quoted 6 lines
The CPUID virtualization basically just traps into the hypervisor and lets the hypervisor set whatever register values it wants to appear when CPUID "returns".
But, the controls for what instructions generate #UD are actually quite separate and unrelated to CPUID itself.
Eh, any sane VMM will accurately represent its virtual CPU model via CPUID insofar as possible, there are just too many creaky corners in x86 to make things 100% bombproof.
Re: [RFC v2 16/32] x86/tdx: Handle MWAIT, MONITOR and WBINVD
Quoted 7 lines · Sean Christopherson
Quoted 6 lines
Quoted 3 lines
Why does this not result in a #UD if the instruction is disabled by SEAM? How is it possible to execute a disabled instruction (one precluded by CPUID) to the point where it triggers #VE instead of #UD?
This is actually a vestige of VMX. It's quite possible toady to have a feature which isn't enumerated in CPUID which still exists and "works" in the silicon.
No, virtualization holes are something else entirely.
I think the bigger point is that *CPUID* doesn't enable or disable instructions in and of itself.
It can *reflect* enabling (like OSPKE), but nothing is actually enabled or disabled via CPUID.
Re: [RFC v2 16/32] x86/tdx: Handle MWAIT, MONITOR and WBINVD
In reply to Kuppuswamy SathyanarayananQuoted 1 line · Kuppuswamy Sathyanarayanan
For WBIND instruction, since it's related to memory writeback
^ WBINVD
Quoted 3 lines
and cache flushes, it's mainly used in context of IO devices. Since TDX 1.0 does not support non-virtual I/O devices, skipping it should not cause any fatal issues. But
Do me a favor:
grep -ri wbinvd arch/x86/
How many I/O devices do you see?
Please get your ducks in a row here. Come up with a coherent changelog about why the arch/x86 use of WBINVD doesn't apply to TDX guests. Explain the audit that you did. You *DID* do an audit, right?
RFC v2 17/32 ACPICA: ACPI 6.4: MADT: add Multiprocessor Wakeup Structure
In reply to Kuppuswamy SathyanarayananFrom: Erik Kaneda <[email protected]>
ACPICA commit b9eb6f3a19b816824d6f47a6bc86fd8ce690e04b
--- include/acpi/actbl2.h | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
include/acpi/actbl2.h +11 -1
@@ -516,7 +516,8 @@ enum acpi_madt_type {ACPI_MADT_TYPE_GENERIC_MSI_FRAME =13 ,ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR =14 ,ACPI_MADT_TYPE_GENERIC_TRANSLATOR =15 , - ACPI_MADT_TYPE_RESERVED = 16 /* 16 and greater are reserved */ +ACPI_MADT_TYPE_MULTIPROC_WAKEUP =16 , + ACPI_MADT_TYPE_RESERVED = 17 /* 17 and greater are reserved */ };/*
@@ -723,6 +724,15 @@ struct acpi_madt_generic_translator {u32 reserved2; }; +/* 16: Multiprocessor wakeup (ACPI 6.4) */ + +struct acpi_madt_multiproc_wakeup { +struct acpi_subtable_header header; +u16 mailbox_version; +u32 reserved;/* reserved - must be zero */ +u64 base_address; +}; +/* * Common flags fields for MADT subtables */
Signature · 1 line
2.25.1
RFC v2 18/32 ACPICA: ACPI 6.4: MADT: add Multiprocessor Wakeup Mailbox Structure
In reply to Kuppuswamy SathyanarayananACPICA commit f1ee04207a212f6c519441e7e25397649ebc4cea
Add Multiprocessor Wakeup Mailbox Structure definition. It is useful in parsing MADT Wake table.
--- include/acpi/actbl2.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
include/acpi/actbl2.h +14 -0
@@ -733,6 +733,20 @@ struct acpi_madt_multiproc_wakeup {u64 base_address; }; +#define ACPI_MULTIPROC_WAKEUP_MB_OS_SIZE 2032 +#define ACPI_MULTIPROC_WAKEUP_MB_FIRMWARE_SIZE 2048 + +struct acpi_madt_multiproc_wakeup_mailbox { +u16 command; +u16 reserved;/* reserved - must be zero */ +u32 apic_id; +u64 wakeup_vector; +u8 reserved_os[ACPI_MULTIPROC_WAKEUP_MB_OS_SIZE ];/* reserved for OS use */ +u8 reserved_firmware[ACPI_MULTIPROC_WAKEUP_MB_FIRMWARE_SIZE ];/* reserved for firmware use */ +}; + +#define ACPI_MP_WAKE_COMMAND_WAKEUP 1 +/* * Common flags fields for MADT subtables */
Signature · 1 line
2.25.1
RFC v2 19/32 ACPI/table: Print MADT Wake table information
In reply to Kuppuswamy SathyanarayananWhen MADT is parsed, print MADT Wake table information as debug message. It will be useful to debug CPU boot issues related to MADT wake table.
--- drivers/acpi/tables.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
drivers/acpi/tables.c +11 -0
@@ -207,6 +207,17 @@ void acpi_table_print_madt_entry(struct acpi_subtable_header *header) }break ; +case ACPI_MADT_TYPE_MULTIPROC_WAKEUP : + { +struct acpi_madt_multiproc_wakeup *p; + + p = (struct acpi_madt_multiproc_wakeup *) header; + +pr_debug ("MP Wake (Mailbox version[%d] base_address[%llx])\n" , + p->mailbox_version, p->base_address); + } +break ; +default :pr_warn ("Found unsupported MADT entry (type = 0x%x)\n" , header->type);
Signature · 1 line
2.25.1
RFC v2 20/32 x86/acpi, x86/boot: Add multiprocessor wake-up support
In reply to Kuppuswamy SathyanarayananAs per ACPI specification r6.4, sec 5.2.12.19, a new sub structure – multiprocessor wake-up structure - is added to the ACPI Multiple APIC Description Table (MADT) to describe the information of the mailbox. If a platform firmware produces the multiprocessor wake-up structure, then OS may use this new mailbox-based mechanism to wake up the APs.
Add ACPI MADT wake table parsing support for x86 platform and if MADT wake table is present, update apic->wakeup_secondary_cpu with new API which uses MADT wake mailbox to wake-up CPU.
--- arch/x86/include/asm/apic.h | 3 ++ arch/x86/kernel/acpi/boot.c | 79 +++++++++++++++++++++++++++++++++++++ arch/x86/kernel/apic/apic.c | 8 ++++ 3 files changed, 90 insertions(+)
arch/x86/include/asm/apic.h +3 -0
@@ -487,6 +487,9 @@ static inline unsigned int read_apic_id(void)return apic->get_apic_id (reg); } +typedef int (*wakeup_cpu_handler )(int apicid,unsigned long start_eip); +extern void acpi_wake_cpu_handler_update (wakeup_cpu_handler handler); +extern int default_apic_id_valid (u32 apicid);extern int default_acpi_madt_oem_check (char *,char *);extern void default_setup_apic_routing (void );
arch/x86/kernel/acpi/boot.c +79 -0
103 lines not on this page · raw message
arch/x86/kernel/apic/apic.c +8 -0
@@ -2554,6 +2554,14 @@ u32 x86_msi_msg_get_destid(struct msi_msg *msg, bool extid) }EXPORT_SYMBOL_GPL (x86_msi_msg_get_destid); +void __init acpi_wake_cpu_handler_update (wakeup_cpu_handler handler) +{ +struct apic **drv; + +for (drv = __apicdrivers; drv < __apicdrivers_end; drv++) + (*drv)->wakeup_secondary_cpu = handler; +} +/* * Override the generic EOI implementation with an optimized version. * Only called during early boot when only one CPU is active and with
Signature · 1 line
2.25.1
RFC v2 21/32 x86/boot: Add a trampoline for APs booting in 64-bit mode
In reply to Kuppuswamy SathyanarayananFrom: Sean Christopherson <[email protected]>
Add a trampoline for booting APs in 64-bit mode via a software handoff with BIOS, and use the new trampoline for the ACPI MP wake protocol used by TDX.
Extend the real mode IDT pointer by four bytes to support LIDT in 64-bit mode. For the GDT pointer, create a new entry as the existing storage for the pointer occupies the zero entry in the GDT itself.
--- arch/x86/include/asm/realmode.h | 1 + arch/x86/kernel/smpboot.c | 5 +++ arch/x86/realmode/rm/header.S | 1 + arch/x86/realmode/rm/trampoline_64.S | 49 +++++++++++++++++++++++- arch/x86/realmode/rm/trampoline_common.S | 5 ++- 5 files changed, 58 insertions(+), 3 deletions(-)
arch/x86/include/asm/realmode.h +1 -0
@@ -25,6 +25,7 @@ struct real_mode_header {u32 sev_es_trampoline_start;#endif #ifdef CONFIG_X86_64 +u32 trampoline_start64;u32 trampoline_pgd;#endif /* ACPI S3 wakeup */
arch/x86/kernel/smpboot.c +5 -0
@@ -1036,6 +1036,11 @@ static int do_boot_cpu(int apicid, int cpu, struct task_struct *idle,unsigned long boot_error =0 ;unsigned long timeout; +#ifdef CONFIG_X86_64 +if (is_tdx_guest ()) + start_ip = real_mode_header->trampoline_start64; +#endif + idle->thread.sp = (unsigned long )task_pt_regs (idle); early_gdt_descr.address = (unsigned long )get_cpu_gdt_rw (cpu); initial_code = (unsigned long )start_secondary;
arch/x86/realmode/rm/header.S +1 -0
@@ -24,6 +24,7 @@ SYM_DATA_START(real_mode_header) .long pa_sev_es_trampoline_start #endif #ifdef CONFIG_X86_64 + .long pa_trampoline_start64 .long pa_trampoline_pgd; #endif /* ACPI S3 wakeup */
arch/x86/realmode/rm/trampoline_64.S +47 -2
78 lines not on this page · raw message
arch/x86/realmode/rm/trampoline_common.S +4 -1
@@ -1,4 +1,7 @@ /* SPDX-License-Identifier: GPL-2.0 */ .section ".rodata","a" .balign 16 -SYM_DATA_LOCAL(tr_idt, .fill 1, 6, 0) +SYM_DATA_START_LOCAL(tr_idt) + .short 0 + .quad 0 +SYM_DATA_END(tr_idt)
Signature · 1 line
2.25.1
Re: [RFC v2 21/32] x86/boot: Add a trampoline for APs booting in 64-bit mode
Quoted 6 lines · Kuppuswamy Sathyanarayanan
From: Sean Christopherson <[email protected]>
Add a trampoline for booting APs in 64-bit mode via a software handoff with BIOS, and use the new trampoline for the ACPI MP wake protocol used by TDX.
Lets add a spec reference:
See section "4.1 ACPI-MADT-AP-Wakeup Table" in the Guest-Host Communication Interface specification for TDX.
Although, there is not much "wake protocol" in this patch, this appears to be the end of the process after the CPU has been messaged to start.
Quoted 33 lines
Extend the real mode IDT pointer by four bytes to support LIDT in 64-bit mode. For the GDT pointer, create a new entry as the existing storage for the pointer occupies the zero entry in the GDT itself.
Reported-by Kai Huang <[email protected]>Signed-off-by Sean Christopherson <[email protected]>Reviewed-by Andi Kleen <[email protected]>Signed-off-by Kuppuswamy Sathyanarayanan <[email protected]>--- arch/x86/include/asm/realmode.h | 1 + arch/x86/kernel/smpboot.c | 5 +++ arch/x86/realmode/rm/header.S | 1 + arch/x86/realmode/rm/trampoline_64.S | 49 +++++++++++++++++++++++- arch/x86/realmode/rm/trampoline_common.S | 5 ++- 5 files changed, 58 insertions(+), 3 deletions(-)arch/x86/include/asm/realmode.h +1
-0@@ -25,6 +25,7 @@ struct real_mode_header {u32 sev_es_trampoline_start;#endif #ifdef CONFIG_X86_64 +u32 trampoline_start64;u32 trampoline_pgd;#endif /* ACPI S3 wakeup */ arch/x86/kernel/smpboot.c +0
-0
arch/x86/kernel/smpboot.c static int do_boot_cpu(int apicid, int cpu, struct task_struct *idle, · line 1042
Quoted 7 lines
arch/x86/kernel/smpboot.c +4
-0@@ -1036,6 +1036,11 @@ static int do_boot_cpu(int apicid, int cpu, struct task_struct *idle,unsigned long boot_error =0 ;unsigned long timeout; +#ifdef CONFIG_X86_64 +if (is_tdx_guest ()) + start_ip = real_mode_header->trampoline_start64; +#endif
Perhaps wrap this into an inline helper in arch/x86/include/asm/realmode.h so that this routine only does one assignment to @start_ip at function entry?
Quoted 25 lines
arch/x86/kernel/smpboot.c +1
-0+ idle->thread.sp = (unsigned long )task_pt_regs (idle); early_gdt_descr.address = (unsigned long )get_cpu_gdt_rw (cpu); initial_code = (unsigned long )start_secondary;arch/x86/realmode/rm/header.S +1
-0@@ -24,6 +24,7 @@ SYM_DATA_START(real_mode_header) .long pa_sev_es_trampoline_start #endif #ifdef CONFIG_X86_64 + .long pa_trampoline_start64 .long pa_trampoline_pgd; #endif /* ACPI S3 wakeup */arch/x86/realmode/rm/trampoline_64.S +1
-0@@ -143,13 +143,20 @@ SYM_CODE_START(startup_32) movl %eax, %cr3 # Set up EFER + movl $MSR_EFER, %ecx
arch/x86/realmode/rm/trampoline_64.S SYM_CODE_START(startup_32) · line 155
Quoted 10 lines
arch/x86/realmode/rm/trampoline_64.S +6
-1+ rdmsr + cmp pa_tr_efer, %eax + jne .Lwrite_efer + cmp pa_tr_efer + 4, %edx + je .Ldone_efer +.Lwrite_efer: movl pa_tr_efer, %eax movl pa_tr_efer + 4, %edx - movl $MSR_EFER, %ecx wrmsr
Is this hunk just a performance optimization to save an unnecessary wrmsr when it is pre-populated with the right value? Is it required for this patch? If "yes", it was not clear to me from the changelog, if "no" seems like it belongs in a standalone optimization patch.
arch/x86/realmode/rm/trampoline_64.S SYM_CODE_START(startup_32) · line 159
Quoted 5 lines
arch/x86/realmode/rm/trampoline_64.S +2
-1+.Ldone_efer: # Enable paging and in turn activate Long Mode - movl $(X86_CR0_PG | X86_CR0_WP | X86_CR0_PE), %eax + movl $(X86_CR0_PG | X86_CR0_WP | X86_CR0_NE | X86_CR0_PE), %eax
It seems setting X86_CR0_NE is redundant when coming through pa_trampoline_compat, is this a standalone fix to make sure that 'numeric-error' is enabled before startup_64?
Quoted 67 lines
arch/x86/realmode/rm/trampoline_64.S +38
-0movl %eax, %cr0 /*@@ -161,6 +168,19 @@ SYM_CODE_START(startup_32) ljmpl $__KERNEL_CS, $pa_startup_64 SYM_CODE_END(startup_32) +SYM_CODE_START(pa_trampoline_compat) + /* + * In compatibility mode. Prep ESP and DX for startup_32, then disable + * paging and complete the switch to legacy 32-bit mode. + */ + movl $rm_stack_end, %esp + movw $__KERNEL_DS, %dx + + movl $(X86_CR0_NE | X86_CR0_PE), %eax + movl %eax, %cr0 + ljmpl $__KERNEL32_CS, $pa_startup_32 +SYM_CODE_END(pa_trampoline_compat) + .section ".text64","ax" .code64 .balign 4@@ -169,6 +189,20 @@ SYM_CODE_START(startup_64) jmpq *tr_start(%rip) SYM_CODE_END(startup_64) +SYM_CODE_START(trampoline_start64) + /* + * APs start here on a direct transfer from 64-bit BIOS with identity + * mapped page tables. Load the kernel's GDT in order to gear down to + * 32-bit mode (to handle 4-level vs. 5-level paging), and to (re)load + * segment registers. Load the zero IDT so any fault triggers a + * shutdown instead of jumping back into BIOS. + */ + lidt tr_idt(%rip) + lgdt tr_gdt64(%rip) + + ljmpl *tr_compat(%rip) +SYM_CODE_END(trampoline_start64) + .section ".rodata","a" # Duplicate the global descriptor table # so the kernel can live anywhere@@ -182,6 +216,17 @@ SYM_DATA_START(tr_gdt) .quad 0x00cf93000000ffff # __KERNEL_DS SYM_DATA_END_LABEL(tr_gdt, SYM_L_LOCAL, tr_gdt_end) +SYM_DATA_START(tr_gdt64) + .short tr_gdt_end - tr_gdt - 1 # gdt limit + .long pa_tr_gdt + .long 0 +SYM_DATA_END(tr_gdt64) + +SYM_DATA_START(tr_compat) + .long pa_trampoline_compat + .short __KERNEL32_CS +SYM_DATA_END(tr_compat) + .bss .balign PAGE_SIZE SYM_DATA(trampoline_pgd, .space PAGE_SIZE)arch/x86/realmode/rm/trampoline_common.S +0
-0
arch/x86/realmode/rm/trampoline_common.S line 7
Quoted 8 lines
arch/x86/realmode/rm/trampoline_common.S +4
-1@@ -1,4 +1,7 @@ /* SPDX-License-Identifier: GPL-2.0 */ .section ".rodata","a" .balign 16 -SYM_DATA_LOCAL(tr_idt, .fill 1, 6, 0) +SYM_DATA_START_LOCAL(tr_idt) + .short 0 + .quad 0 +SYM_DATA_END(tr_idt)
Curious, is the following not equivalent?
-SYM_DATA_LOCAL(tr_idt, .fill 1, 6, 0)
+SYM_DATA_LOCAL(tr_idt, .fill 1, 10, 0)
RFC 1/1 x86/boot: Add a trampoline for APs booting in 64-bit mode
From: Sean Christopherson <[email protected]>
Add a trampoline for booting APs in 64-bit mode via a software handoff with BIOS, and use the new trampoline for the ACPI MP wake protocol used by TDX. You can find MADT MP wake protocol details in ACPI specification r6.4, sec 5.2.12.19.
Extend the real mode IDT pointer by four bytes to support LIDT in 64-bit mode. For the GDT pointer, create a new entry as the existing storage for the pointer occupies the zero entry in the GDT itself.
---
Changes since RFC v2:
* Removed X86_CR0_NE and EFER related changes from this changes and moved it to patch titled "x86/boot: Avoid #VE during boot for TDX platforms"
* Fixed commit log as per Dan's suggestion.
* Added inline get_trampoline_start_ip() to set start_ip.
arch/x86/boot/compressed/pgtable.h | 2 +- arch/x86/include/asm/realmode.h | 10 +++++++ arch/x86/kernel/smpboot.c | 2 +- arch/x86/realmode/rm/header.S | 1 + arch/x86/realmode/rm/trampoline_64.S | 38 ++++++++++++++++++++++++ arch/x86/realmode/rm/trampoline_common.S | 7 ++++- 6 files changed, 57 insertions(+), 3 deletions(-)
arch/x86/boot/compressed/pgtable.h +1 -1
@@ -6,7 +6,7 @@#define TRAMPOLINE_32BIT_PGTABLE_OFFSET 0#define TRAMPOLINE_32BIT_CODE_OFFSET PAGE_SIZE -#define TRAMPOLINE_32BIT_CODE_SIZE 0x70 +#define TRAMPOLINE_32BIT_CODE_SIZE 0x80#define TRAMPOLINE_32BIT_STACK_END TRAMPOLINE_32BIT_SIZE
arch/x86/include/asm/realmode.h +10 -0
@@ -25,6 +25,7 @@ struct real_mode_header {u32 sev_es_trampoline_start;#endif #ifdef CONFIG_X86_64 +u32 trampoline_start64;u32 trampoline_pgd;#endif /* ACPI S3 wakeup */
@@ -88,6 +89,15 @@ static inline void set_real_mode_mem(phys_addr_t mem) real_mode_header = (struct real_mode_header *) __va(mem); } +static inline unsigned long get_trampoline_start_ip (void ) +{ +#ifdef CONFIG_X86_64 +if (is_tdx_guest ()) +return real_mode_header->trampoline_start64; +#endif +return real_mode_header->trampoline_start; +} +void reserve_real_mode (void );#endif /* __ASSEMBLY__ */
arch/x86/kernel/smpboot.c +1 -1
@@ -1031,7 +1031,7 @@ static int do_boot_cpu(int apicid, int cpu, struct task_struct *idle, int *cpu0_nmi_registered) {/* start_ip had better be page-aligned! */ -unsigned long start_ip = real_mode_header->trampoline_start; +unsigned long start_ip =get_trampoline_start_ip ();unsigned long boot_error =0 ;unsigned long timeout;
arch/x86/realmode/rm/header.S +1 -0
@@ -24,6 +24,7 @@ SYM_DATA_START(real_mode_header) .long pa_sev_es_trampoline_start #endif #ifdef CONFIG_X86_64 + .long pa_trampoline_start64 .long pa_trampoline_pgd; #endif /* ACPI S3 wakeup */
arch/x86/realmode/rm/trampoline_64.S +38 -0
@@ -161,6 +161,19 @@ SYM_CODE_START(startup_32) ljmpl $__KERNEL_CS, $pa_startup_64 SYM_CODE_END(startup_32) +SYM_CODE_START(pa_trampoline_compat) + /* + * In compatibility mode. Prep ESP and DX for startup_32, then disable + * paging and complete the switch to legacy 32-bit mode. + */ + movl $rm_stack_end, %esp + movw $__KERNEL_DS, %dx + + movl $(X86_CR0_NE | X86_CR0_PE), %eax + movl %eax, %cr0 + ljmpl $__KERNEL32_CS, $pa_startup_32 +SYM_CODE_END(pa_trampoline_compat) + .section ".text64","ax" .code64 .balign 4
@@ -169,6 +182,20 @@ SYM_CODE_START(startup_64) jmpq *tr_start(%rip) SYM_CODE_END(startup_64) +SYM_CODE_START(trampoline_start64) + /* + * APs start here on a direct transfer from 64-bit BIOS with identity + * mapped page tables. Load the kernel's GDT in order to gear down to + * 32-bit mode (to handle 4-level vs. 5-level paging), and to (re)load + * segment registers. Load the zero IDT so any fault triggers a + * shutdown instead of jumping back into BIOS. + */ + lidt tr_idt(%rip) + lgdt tr_gdt64(%rip) + + ljmpl *tr_compat(%rip) +SYM_CODE_END(trampoline_start64) + .section ".rodata","a" # Duplicate the global descriptor table # so the kernel can live anywhere
@@ -182,6 +209,17 @@ SYM_DATA_START(tr_gdt) .quad 0x00cf93000000ffff # __KERNEL_DS SYM_DATA_END_LABEL(tr_gdt, SYM_L_LOCAL, tr_gdt_end) +SYM_DATA_START(tr_gdt64) + .short tr_gdt_end - tr_gdt - 1 # gdt limit + .long pa_tr_gdt + .long 0 +SYM_DATA_END(tr_gdt64) + +SYM_DATA_START(tr_compat) + .long pa_trampoline_compat + .short __KERNEL32_CS +SYM_DATA_END(tr_compat) + .bss .balign PAGE_SIZE SYM_DATA(trampoline_pgd, .space PAGE_SIZE)
arch/x86/realmode/rm/trampoline_common.S +6 -1
@@ -1,4 +1,9 @@ /* SPDX-License-Identifier: GPL-2.0 */ .section ".rodata","a" .balign 16 -SYM_DATA_LOCAL(tr_idt, .fill 1, 6, 0) + +/* .fill cannot be used for size > 8. So use short and quad */ +SYM_DATA_START_LOCAL(tr_idt) + .short 0 + .quad 0 +SYM_DATA_END(tr_idt)
Signature · 1 line
2.25.1
Re: [RFC v2-fix 1/1] x86/boot: Add a trampoline for APs booting in 64-bit mode
I notice that you have [RFC v2-fix 1/1] as the prefix for this patch. b4 recently gained support for partial series re-rolls [1], but I think you would need to bump the version number [RFC PATCH v3 21/32] and maintain the patch numbering. In this case with changes moving between patches, and those other patches being squashed any chance of automated reconstruction of this series is likely lost.
Just wanted to note that for future reference in case you were hoping to avoid resending full series in the future. For now, some more comments below:
[1]: https://lore.kernel.org/tools/[email protected]/
Quoted 12 lines · Kuppuswamy Sathyanarayanan
From: Sean Christopherson <[email protected]>
Add a trampoline for booting APs in 64-bit mode via a software handoff with BIOS, and use the new trampoline for the ACPI MP wake protocol used by TDX. You can find MADT MP wake protocol details in ACPI specification r6.4, sec 5.2.12.19.
Extend the real mode IDT pointer by four bytes to support LIDT in 64-bit mode. For the GDT pointer, create a new entry as the existing storage for the pointer occupies the zero entry in the GDT itself.
Quoted 8 lines
Reported-by Kai Huang <[email protected]>Signed-off-by Sean Christopherson <[email protected]>Reviewed-by Andi Kleen <[email protected]>Signed-off-by Kuppuswamy Sathyanarayanan <[email protected]>---
Changes since RFC v2:
* RemovedX86_CR0_NEand EFER related changes from this changes
This was only partially done, see below...
Quoted 4 lines
and moved it to patch titled "x86/boot: Avoid #VE during boot for TDX platforms"
* Fixed commit log as per Dan's suggestion.
* Added inlineget_trampoline_start_ip()to setstart_ip.
You also added a comment to tr_idt, but didn't mention it here, so I went to double check. Please take care to document all changes to the patch from the previous review.
Quoted 35 lines
arch/x86/boot/compressed/pgtable.h | 2 +- arch/x86/include/asm/realmode.h | 10 +++++++ arch/x86/kernel/smpboot.c | 2 +- arch/x86/realmode/rm/header.S | 1 + arch/x86/realmode/rm/trampoline_64.S | 38 ++++++++++++++++++++++++ arch/x86/realmode/rm/trampoline_common.S | 7 ++++- 6 files changed, 57 insertions(+), 3 deletions(-)arch/x86/boot/compressed/pgtable.h +1
-1@@ -6,7 +6,7 @@#define TRAMPOLINE_32BIT_PGTABLE_OFFSET 0#define TRAMPOLINE_32BIT_CODE_OFFSET PAGE_SIZE -#define TRAMPOLINE_32BIT_CODE_SIZE 0x70 +#define TRAMPOLINE_32BIT_CODE_SIZE 0x80#define TRAMPOLINE_32BIT_STACK_END TRAMPOLINE_32BIT_SIZEarch/x86/include/asm/realmode.h +1
-0@@ -25,6 +25,7 @@ struct real_mode_header {u32 sev_es_trampoline_start;#endif #ifdef CONFIG_X86_64 +u32 trampoline_start64;u32 trampoline_pgd;#endif /* ACPI S3 wakeup */
arch/x86/include/asm/realmode.h static inline void set_real_mode_mem(phys_addr_t mem) · line 92
Quoted 4 lines
arch/x86/include/asm/realmode.h +1
-0@@ -88,6 +89,15 @@ static inline void set_real_mode_mem(phys_addr_t mem) real_mode_header = (struct real_mode_header *)__va (mem); } +static inline unsigned long get_trampoline_start_ip (void )
I'd prefer this helper take a 'struct real_mode_header *rmh' as an argument rather than assume a global variable.
Quoted 43 lines
arch/x86/include/asm/realmode.h +8
-0+{ +#ifdef CONFIG_X86_64 +if (is_tdx_guest ()) +return real_mode_header->trampoline_start64; +#endif +return real_mode_header->trampoline_start; +} +void reserve_real_mode (void );#endif /* __ASSEMBLY__ */ arch/x86/kernel/smpboot.c +1
-1@@ -1031,7 +1031,7 @@ static int do_boot_cpu(int apicid, int cpu, struct task_struct *idle, int *cpu0_nmi_registered) {/* start_ip had better be page-aligned! */ -unsigned long start_ip = real_mode_header->trampoline_start; +unsigned long start_ip =get_trampoline_start_ip ();unsigned long boot_error =0 ;unsigned long timeout;arch/x86/realmode/rm/header.S +1
-0@@ -24,6 +24,7 @@ SYM_DATA_START(real_mode_header) .long pa_sev_es_trampoline_start #endif #ifdef CONFIG_X86_64 + .long pa_trampoline_start64 .long pa_trampoline_pgd; #endif /* ACPI S3 wakeup */arch/x86/realmode/rm/trampoline_64.S +0
-0@@ -161,6 +161,19 @@ SYM_CODE_START(startup_32) ljmpl $__KERNEL_CS, $pa_startup_64 SYM_CODE_END(startup_32)
arch/x86/realmode/rm/trampoline_64.S SYM_CODE_START(startup_32) · line 172
Quoted 10 lines
arch/x86/realmode/rm/trampoline_64.S +9
-0+SYM_CODE_START(pa_trampoline_compat) + /* + * In compatibility mode. Prep ESP and DX for startup_32, then disable + * paging and complete the switch to legacy 32-bit mode. + */ + movl $rm_stack_end, %esp + movw $__KERNEL_DS, %dx + + movl $(X86_CR0_NE | X86_CR0_PE), %eax
Before this patch the startup path did not touch X86_CR0_NE. I assume it was added opportunistically for the TDX case? If it is to stay in this patch it deserves a code comment / mention in the changelog, or it needs to move to the other patch that fixes up the CR0 setup for TDX.
Quoted 51 lines
arch/x86/realmode/rm/trampoline_64.S +29
-0+ movl %eax, %cr0 + ljmpl $__KERNEL32_CS, $pa_startup_32 +SYM_CODE_END(pa_trampoline_compat) + .section ".text64","ax" .code64 .balign 4@@ -169,6 +182,20 @@ SYM_CODE_START(startup_64) jmpq *tr_start(%rip) SYM_CODE_END(startup_64) +SYM_CODE_START(trampoline_start64) + /* + * APs start here on a direct transfer from 64-bit BIOS with identity + * mapped page tables. Load the kernel's GDT in order to gear down to + * 32-bit mode (to handle 4-level vs. 5-level paging), and to (re)load + * segment registers. Load the zero IDT so any fault triggers a + * shutdown instead of jumping back into BIOS. + */ + lidt tr_idt(%rip) + lgdt tr_gdt64(%rip) + + ljmpl *tr_compat(%rip) +SYM_CODE_END(trampoline_start64) + .section ".rodata","a" # Duplicate the global descriptor table # so the kernel can live anywhere@@ -182,6 +209,17 @@ SYM_DATA_START(tr_gdt) .quad 0x00cf93000000ffff # __KERNEL_DS SYM_DATA_END_LABEL(tr_gdt, SYM_L_LOCAL, tr_gdt_end) +SYM_DATA_START(tr_gdt64) + .short tr_gdt_end - tr_gdt - 1 # gdt limit + .long pa_tr_gdt + .long 0 +SYM_DATA_END(tr_gdt64) + +SYM_DATA_START(tr_compat) + .long pa_trampoline_compat + .short __KERNEL32_CS +SYM_DATA_END(tr_compat) + .bss .balign PAGE_SIZE SYM_DATA(trampoline_pgd, .space PAGE_SIZE)arch/x86/realmode/rm/trampoline_common.S +0
-0
arch/x86/realmode/rm/trampoline_common.S line 5
Quoted 6 lines
arch/x86/realmode/rm/trampoline_common.S +2
-1@@ -1,4 +1,9 @@ /* SPDX-License-Identifier: GPL-2.0 */ .section ".rodata","a" .balign 16 -SYM_DATA_LOCAL(tr_idt, .fill 1, 6, 0) + +/* .fill cannot be used for size > 8. So use short and quad */
If there is to be a comment here it should be to clarify why @tr_idt is 10 bytes, not necessarily a quirk of the assembler.
arch/x86/realmode/rm/trampoline_common.S line 6
Quoted 1 line
arch/x86/realmode/rm/trampoline_common.S +1
-0+SYM_DATA_START_LOCAL(tr_idt)
The .fill restriction is only for @size, not @repeat. So, what's wrong with SYM_DATA_LOCAL(tr_idt, .fill 2, 5, 0)?
Re: [RFC v2-fix 1/1] x86/boot: Add a trampoline for APs booting in 64-bit mode
Quoted 6 lines · Dan Williams
I notice that you have [RFC v2-fix 1/1] as the prefix for this patch. b4 recently gained support for partial series re-rolls [1], but I think you would need to bump the version number [RFC PATCH v3 21/32] and maintain the patch numbering. In this case with changes moving between patches, and those other patches being squashed any chance of automated reconstruction of this series is likely lost.
Ok. I will make sure to bump the version in next partial re-roll.
If I am fixing this patch as per your comments, do I need bump the patch version for it as well?
Quoted 4 lines
Just wanted to note that for future reference in case you were hoping to avoid resending full series in the future. For now, some more comments below:
Thanks.
Quoted 28 lines
[1]: https://lore.kernel.org/tools/[email protected]/
Quoted 12 lines · Kuppuswamy Sathyanarayanan
From: Sean Christopherson <[email protected]>
Add a trampoline for booting APs in 64-bit mode via a software handoff with BIOS, and use the new trampoline for the ACPI MP wake protocol used by TDX. You can find MADT MP wake protocol details in ACPI specification r6.4, sec 5.2.12.19.
Extend the real mode IDT pointer by four bytes to support LIDT in 64-bit mode. For the GDT pointer, create a new entry as the existing storage for the pointer occupies the zero entry in the GDT itself.
Quoted 8 lines
Reported-by Kai Huang <[email protected]>Signed-off-by Sean Christopherson <[email protected]>Reviewed-by Andi Kleen <[email protected]>Signed-off-by Kuppuswamy Sathyanarayanan <[email protected]>---
Changes since RFC v2:
* RemovedX86_CR0_NEand EFER related changes from this changesThis was only partially done, see below...
Quoted 8 lines
Quoted 4 lines
and moved it to patch titled "x86/boot: Avoid #VE during boot for TDX platforms"
* Fixed commit log as per Dan's suggestion.
* Added inlineget_trampoline_start_ip()to setstart_ip.You also added a comment to
tr_idt, but didn't mention it here, so I went to double check. Please take care to document all changes to the patch from the previous review.
Ok. I will make sure change log is current.
Quoted 39 lines
Quoted 39 lines
arch/x86/boot/compressed/pgtable.h | 2 +- arch/x86/include/asm/realmode.h | 10 +++++++ arch/x86/kernel/smpboot.c | 2 +- arch/x86/realmode/rm/header.S | 1 + arch/x86/realmode/rm/trampoline_64.S | 38 ++++++++++++++++++++++++ arch/x86/realmode/rm/trampoline_common.S | 7 ++++- 6 files changed, 57 insertions(+), 3 deletions(-)arch/x86/boot/compressed/pgtable.h +1
-1@@ -6,7 +6,7 @@#define TRAMPOLINE_32BIT_PGTABLE_OFFSET 0#define TRAMPOLINE_32BIT_CODE_OFFSET PAGE_SIZE -#define TRAMPOLINE_32BIT_CODE_SIZE 0x70 +#define TRAMPOLINE_32BIT_CODE_SIZE 0x80#define TRAMPOLINE_32BIT_STACK_END TRAMPOLINE_32BIT_SIZEarch/x86/include/asm/realmode.h +1
-0@@ -25,6 +25,7 @@ struct real_mode_header {u32 sev_es_trampoline_start;#endif #ifdef CONFIG_X86_64 +u32 trampoline_start64;u32 trampoline_pgd;#endif /* ACPI S3 wakeup */ @@ -88,6 +89,15 @@ static inline void set_real_mode_mem(phys_addr_t mem) real_mode_header = (struct real_mode_header *)__va (mem); }
Quoted 4 lines
arch/x86/include/asm/realmode.h static inline void set_real_mode_mem(phys_addr_t mem) · line 92
Quoted 1 line
arch/x86/include/asm/realmode.h +1
-0+static inline unsigned long get_trampoline_start_ip (void )I'd prefer this helper take a 'struct
real_mode_header*rmh' as an argument rather than assume a global variable.
I am fine with it. But existing inline functions also directly read/writes the real_mode_header. So I just followed the same format.
I will fix this in next version.
Quoted 45 lines
Quoted 45 lines
arch/x86/include/asm/realmode.h +8
-0+{ +#ifdef CONFIG_X86_64 +if (is_tdx_guest ()) +return real_mode_header->trampoline_start64; +#endif +return real_mode_header->trampoline_start; +} +void reserve_real_mode (void );#endif /* __ASSEMBLY__ */ arch/x86/kernel/smpboot.c +1
-1@@ -1031,7 +1031,7 @@ static int do_boot_cpu(int apicid, int cpu, struct task_struct *idle, int *cpu0_nmi_registered) {/* start_ip had better be page-aligned! */ -unsigned long start_ip = real_mode_header->trampoline_start; +unsigned long start_ip =get_trampoline_start_ip ();unsigned long boot_error =0 ;unsigned long timeout;arch/x86/realmode/rm/header.S +1
-0@@ -24,6 +24,7 @@ SYM_DATA_START(real_mode_header) .long pa_sev_es_trampoline_start #endif #ifdef CONFIG_X86_64 + .long pa_trampoline_start64 .long pa_trampoline_pgd; #endif /* ACPI S3 wakeup */arch/x86/realmode/rm/trampoline_64.S +0
-0@@ -161,6 +161,19 @@ SYM_CODE_START(startup_32) ljmpl $__KERNEL_CS, $pa_startup_64 SYM_CODE_END(startup_32)
Quoted 15 lines
arch/x86/realmode/rm/trampoline_64.S SYM_CODE_START(startup_32) · line 172
Quoted 9 lines
arch/x86/realmode/rm/trampoline_64.S +9
-0+SYM_CODE_START(pa_trampoline_compat) + /* + * In compatibility mode. Prep ESP and DX for startup_32, then disable + * paging and complete the switch to legacy 32-bit mode. + */ + movl $rm_stack_end, %esp + movw $__KERNEL_DS, %dx + + movl $(X86_CR0_NE | X86_CR0_PE), %eaxBefore this patch the startup path did not touch
X86_CR0_NE. I assume it was added opportunistically for the TDX case? If it is to stay in this patch it deserves a code comment / mention in the changelog, or it needs to move to the other patch that fixes up the CR0 setup for TDX.
I will move X86_CR0_NE related update to the patch that has other X86_CR0_NE related updates.
Quoted 54 lines
Quoted 54 lines
arch/x86/realmode/rm/trampoline_64.S +29
-0+ movl %eax, %cr0 + ljmpl $__KERNEL32_CS, $pa_startup_32 +SYM_CODE_END(pa_trampoline_compat) + .section ".text64","ax" .code64 .balign 4@@ -169,6 +182,20 @@ SYM_CODE_START(startup_64) jmpq *tr_start(%rip) SYM_CODE_END(startup_64) +SYM_CODE_START(trampoline_start64) + /* + * APs start here on a direct transfer from 64-bit BIOS with identity + * mapped page tables. Load the kernel's GDT in order to gear down to + * 32-bit mode (to handle 4-level vs. 5-level paging), and to (re)load + * segment registers. Load the zero IDT so any fault triggers a + * shutdown instead of jumping back into BIOS. + */ + lidt tr_idt(%rip) + lgdt tr_gdt64(%rip) + + ljmpl *tr_compat(%rip) +SYM_CODE_END(trampoline_start64) + .section ".rodata","a" # Duplicate the global descriptor table # so the kernel can live anywhere@@ -182,6 +209,17 @@ SYM_DATA_START(tr_gdt) .quad 0x00cf93000000ffff # __KERNEL_DS SYM_DATA_END_LABEL(tr_gdt, SYM_L_LOCAL, tr_gdt_end) +SYM_DATA_START(tr_gdt64) + .short tr_gdt_end - tr_gdt - 1 # gdt limit + .long pa_tr_gdt + .long 0 +SYM_DATA_END(tr_gdt64) + +SYM_DATA_START(tr_compat) + .long pa_trampoline_compat + .short __KERNEL32_CS +SYM_DATA_END(tr_compat) + .bss .balign PAGE_SIZE SYM_DATA(trampoline_pgd, .space PAGE_SIZE)arch/x86/realmode/rm/trampoline_common.S +0
-0@@ -1,4 +1,9 @@ /* SPDX-License-Identifier: GPL-2.0 */
Quoted 8 lines
arch/x86/realmode/rm/trampoline_common.S line 5
Quoted 5 lines
arch/x86/realmode/rm/trampoline_common.S +2
-1.section ".rodata","a" .balign 16 -SYM_DATA_LOCAL(tr_idt, .fill 1, 6, 0) + +/* .fill cannot be used for size > 8. So use short and quad */If there is to be a comment here it should be to clarify why @tr_idt is 10 bytes, not necessarily a quirk of the assembler.
Got it. I will fix the comment or remove it.
Quoted 5 lines
arch/x86/realmode/rm/trampoline_common.S line 6
Quoted 2 lines
arch/x86/realmode/rm/trampoline_common.S +1
-0+SYM_DATA_START_LOCAL(tr_idt)The .fill restriction is only for @size, not @repeat. So, what's wrong with
SYM_DATA_LOCAL(tr_idt, .fill 2, 5, 0)?
Any reason to prefer above change over previous code ?
SYM_DATA_START_LOCAL(tr_idt)
.short 0
.quad 0SYM_DATA_END(tr_idt)
Quoted 1 line
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Re: [RFC v2-fix 1/1] x86/boot: Add a trampoline for APs booting in 64-bit mode
Quoted 11 lines · Sathyanarayanan
Quoted 6 lines · Dan Williams
I notice that you have [RFC v2-fix 1/1] as the prefix for this patch. b4 recently gained support for partial series re-rolls [1], but I think you would need to bump the version number [RFC PATCH v3 21/32] and maintain the patch numbering. In this case with changes moving between patches, and those other patches being squashed any chance of automated reconstruction of this series is likely lost.
Quoted 4 lines
Ok. I will make sure to bump the version in next partial re-roll.
If I am fixing this patch as per your comments, do I need bump the patch version for it as well?
I don't think it matters too much in this case as I don't think I can use b4 to assemble this series. So just for future reference on other patch sets. That said, I wouldn't mind a link to your work-in-progress branch to see all the changes together in one place.
[..]
Quoted 5 lines
Quoted 2 lines
I'd prefer this helper take a 'struct
real_mode_header*rmh' as an argument rather than assume a global variable.I am fine with it. But existing inline functions also directly read/writes the
real_mode_header. So I just followed the same format.
I notice the SEV-ES code passes an @rmh variable around for this purpose.
[..]
Quoted 6 lines
Quoted 2 lines
If there is to be a comment here it should be to clarify why @tr_idt is 10 bytes, not necessarily a quirk of the assembler.
Got it. I will fix the comment or remove it.
Quoted 1 line
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
+SYM_DATA_START_LOCAL(tr_idt)
The .fill restriction is only for @size, not @repeat. So, what's wrong with
SYM_DATA_LOCAL(tr_idt, .fill 2, 5, 0)?Any reason to prefer above change over previous code ?
What I'm really after is capturing why this size needs to be adjusted for future reference. Maybe it's plainly obvious to someone who has worked with this code, but it was not immediately obvious to me.
Quoted 5 lines
SYM_DATA_START_LOCAL(tr_idt)
.short 0
.quad 0SYM_DATA_END(tr_idt)
This format implies that tr_idt is reserving space for 2 distinct data structure attributes of those sizes, can you just put those names here as comments? Otherwise the .fill format is more compact.
Re: [RFC v2-fix 1/1] x86/boot: Add a trampoline for APs booting in 64-bit mode
Hi Dan,
Quoted 7 lines · Dan Williams
Quoted 4 lines
SYM_DATA_START_LOCAL(tr_idt)
.short 0
.quad 0SYM_DATA_END(tr_idt)This format implies that
tr_idtis reserving space for 2 distinct data structure attributes of those sizes, can you just put those names here as comments? Otherwise the .fill format is more compact.
Initially its 6 bytes (2 bytes for IDT limit, 4 bytes for 32 bit linear start address). This patch extends it by another 4 bytes for supporting 64 bit mode.
2 bytes IDT limit (.short)
8 bytes for 64 bit IDT start address (.quad)
This info is included in commit log. But I will add comment here as you have mentioned.
Will following comment log do ?
/* Use 10 bytes for IDT (in 64 bit mode), 8 bytes for IDT start address
2 bytes for IDT limit size */
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Re: [RFC v2-fix 1/1] x86/boot: Add a trampoline for APs booting in 64-bit mode
Quoted 16 lines · Sathyanarayanan
Hi Dan,
Quoted 7 lines · Dan Williams
Quoted 4 lines
SYM_DATA_START_LOCAL(tr_idt)
.short 0
.quad 0SYM_DATA_END(tr_idt)This format implies that
tr_idtis reserving space for 2 distinct data structure attributes of those sizes, can you just put those names here as comments? Otherwise the .fill format is more compact.Initially its 6 bytes (2 bytes for IDT limit, 4 bytes for 32 bit linear start address). This patch extends it by another 4 bytes for supporting 64 bit mode.
Quoted 5 lines
2 bytes IDT limit (.short)
8 bytes for 64 bit IDT start address (.quad)This info is included in commit log. But I will add comment here as you have mentioned.
Thanks. I only read commit logs when code comments fail.
Quoted 5 lines
Will following comment log do ?
/* Use 10 bytes for IDT (in 64 bit mode), 8 bytes for IDT start address
2 bytes for IDT limit size */
I would clarify how the boot code uses this:
"When a bootloader hands off to the kernel in 32-bit mode an IDT with a 2-byte limit and 4-byte base is needed. When a boot loader hands off to a kernel 64-bit mode the base address extends to 8-bytes. Reserve enough space for either scenario."
Re: [RFC v2-fix 1/1] x86/boot: Add a trampoline for APs booting in 64-bit mode
Quoted 6 lines · Dan Williams
I would clarify how the boot code uses this:
"When a bootloader hands off to the kernel in 32-bit mode an IDT with a 2-byte limit and 4-byte base is needed. When a boot loader hands off to a kernel 64-bit mode the base address extends to 8-bytes. Reserve enough space for either scenario."
I will add it. Thanks.
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
RFC 1/1 x86/boot: Add a trampoline for APs booting in 64-bit mode
From: Sean Christopherson <[email protected]>
Add a trampoline for booting APs in 64-bit mode via a software handoff with BIOS, and use the new trampoline for the ACPI MP wake protocol used by TDX. You can find MADT MP wake protocol details in ACPI specification r6.4, sec 5.2.12.19.
Extend the real mode IDT pointer by four bytes to support LIDT in 64-bit mode. For the GDT pointer, create a new entry as the existing storage for the pointer occupies the zero entry in the GDT itself.
---
Changes since RFC v2-fix:
* Passed rmh as argument to get_trampoline_start_ip().
* Added a comment line for get_trampoline_start_ip().
* Moved X86_CR0_NE change from pa_trampoline_compat() to patch "x86/boot: Avoid #VE during boot for TDX platforms".
* Fixed comments for tr_idt as per Dan's comments.
* Moved TRAMPOLINE_32BIT_CODE_SIZE change to "x86/boot: Avoid #VE during boot for TDX platforms" patch.
arch/x86/include/asm/realmode.h | 11 +++++++ arch/x86/kernel/smpboot.c | 2 +- arch/x86/realmode/rm/header.S | 1 + arch/x86/realmode/rm/trampoline_64.S | 38 ++++++++++++++++++++++++ arch/x86/realmode/rm/trampoline_common.S | 12 +++++++- 5 files changed, 62 insertions(+), 2 deletions(-)
arch/x86/include/asm/realmode.h +11 -0
@@ -25,6 +25,7 @@ struct real_mode_header {u32 sev_es_trampoline_start;#endif #ifdef CONFIG_X86_64 +u32 trampoline_start64;u32 trampoline_pgd;#endif /* ACPI S3 wakeup */
@@ -88,6 +89,16 @@ static inline void set_real_mode_mem(phys_addr_t mem) real_mode_header = (struct real_mode_header *) __va(mem); } +/* Common helper function to get start IP address */ +static inline unsigned long get_trampoline_start_ip (struct real_mode_header *rmh) +{ +#ifdef CONFIG_X86_64 +if (is_tdx_guest ()) +return rmh->trampoline_start64; +#endif +return rmh->trampoline_start; +} +void reserve_real_mode (void );#endif /* __ASSEMBLY__ */
arch/x86/kernel/smpboot.c +1 -1
@@ -1031,7 +1031,7 @@ static int do_boot_cpu(int apicid, int cpu, struct task_struct *idle, int *cpu0_nmi_registered) {/* start_ip had better be page-aligned! */ -unsigned long start_ip = real_mode_header->trampoline_start; +unsigned long start_ip =get_trampoline_start_ip (real_mode_header);unsigned long boot_error =0 ;unsigned long timeout;
arch/x86/realmode/rm/header.S +1 -0
@@ -24,6 +24,7 @@ SYM_DATA_START(real_mode_header) .long pa_sev_es_trampoline_start #endif #ifdef CONFIG_X86_64 + .long pa_trampoline_start64 .long pa_trampoline_pgd; #endif /* ACPI S3 wakeup */
arch/x86/realmode/rm/trampoline_64.S +38 -0
@@ -161,6 +161,19 @@ SYM_CODE_START(startup_32) ljmpl $__KERNEL_CS, $pa_startup_64 SYM_CODE_END(startup_32) +SYM_CODE_START(pa_trampoline_compat) + /* + * In compatibility mode. Prep ESP and DX for startup_32, then disable + * paging and complete the switch to legacy 32-bit mode. + */ + movl $rm_stack_end, %esp + movw $__KERNEL_DS, %dx + + movl $X86_CR0_PE, %eax + movl %eax, %cr0 + ljmpl $__KERNEL32_CS, $pa_startup_32 +SYM_CODE_END(pa_trampoline_compat) + .section ".text64","ax" .code64 .balign 4
@@ -169,6 +182,20 @@ SYM_CODE_START(startup_64) jmpq *tr_start(%rip) SYM_CODE_END(startup_64) +SYM_CODE_START(trampoline_start64) + /* + * APs start here on a direct transfer from 64-bit BIOS with identity + * mapped page tables. Load the kernel's GDT in order to gear down to + * 32-bit mode (to handle 4-level vs. 5-level paging), and to (re)load + * segment registers. Load the zero IDT so any fault triggers a + * shutdown instead of jumping back into BIOS. + */ + lidt tr_idt(%rip) + lgdt tr_gdt64(%rip) + + ljmpl *tr_compat(%rip) +SYM_CODE_END(trampoline_start64) + .section ".rodata","a" # Duplicate the global descriptor table # so the kernel can live anywhere
@@ -182,6 +209,17 @@ SYM_DATA_START(tr_gdt) .quad 0x00cf93000000ffff # __KERNEL_DS SYM_DATA_END_LABEL(tr_gdt, SYM_L_LOCAL, tr_gdt_end) +SYM_DATA_START(tr_gdt64) + .short tr_gdt_end - tr_gdt - 1 # gdt limit + .long pa_tr_gdt + .long 0 +SYM_DATA_END(tr_gdt64) + +SYM_DATA_START(tr_compat) + .long pa_trampoline_compat + .short __KERNEL32_CS +SYM_DATA_END(tr_compat) + .bss .balign PAGE_SIZE SYM_DATA(trampoline_pgd, .space PAGE_SIZE)
arch/x86/realmode/rm/trampoline_common.S +11 -1
@@ -1,4 +1,14 @@ /* SPDX-License-Identifier: GPL-2.0 */ .section ".rodata","a" .balign 16 -SYM_DATA_LOCAL(tr_idt, .fill 1, 6, 0) + +/* + * When a bootloader hands off to the kernel in 32-bit mode an + * IDT with a 2-byte limit and 4-byte base is needed. When a boot + * loader hands off to a kernel 64-bit mode the base address + * extends to 8-bytes. Reserve enough space for either scenario. + */ +SYM_DATA_START_LOCAL(tr_idt) + .short 0 + .quad 0 +SYM_DATA_END(tr_idt)
Signature · 1 line
2.25.1
Re: [RFC v2-fix-v2 1/1] x86/boot: Add a trampoline for APs booting in 64-bit mode
Quoted 18 lines · Kuppuswamy Sathyanarayanan
From: Sean Christopherson <[email protected]>
Add a trampoline for booting APs in 64-bit mode via a software handoff with BIOS, and use the new trampoline for the ACPI MP wake protocol used by TDX. You can find MADT MP wake protocol details in ACPI specification r6.4, sec 5.2.12.19.
Extend the real mode IDT pointer by four bytes to support LIDT in 64-bit mode. For the GDT pointer, create a new entry as the existing storage for the pointer occupies the zero entry in the GDT itself.
Reported-by Kai Huang <[email protected]>Signed-off-by Sean Christopherson <[email protected]>Reviewed-by Andi Kleen <[email protected]>Signed-off-by Kuppuswamy Sathyanarayanan <[email protected]>---
Quoted 8 lines
Changes since RFC v2-fix:
* Passed rmh as argument toget_trampoline_start_ip().
* Added a comment line forget_trampoline_start_ip().
* MovedX86_CR0_NEchange frompa_trampoline_compat()to patch "x86/boot: Avoid #VE during boot for TDX platforms".
* Fixed comments fortr_idtas per Dan's comments.
* MovedTRAMPOLINE_32BIT_CODE_SIZEchange to "x86/boot: Avoid #VE during boot for TDX platforms" patch.
Thanks, looks good, no more comments from me:
kernel test robot
build botRe: [RFC v2-fix-v2 1/1] x86/boot: Add a trampoline for APs booting in 64-bit mode
In reply to Kuppuswamy Sathyanarayanan[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch]
Hi Kuppuswamy,
[FYI, it's a private test report for your RFC patch.]
[auto build test ERROR on tip/x86/core]
[also build test ERROR on tip/master linux/master linus/master v5.13-rc2 next-20210521] [cannot apply to luto/next]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Kuppuswamy-Sathyanarayanan/x86-boot-Add-a-trampoline-for-APs-booting-in-64-bit-mode/20210522-211940 base: https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git 2c88d45edbb89029c1190bb3b136d2602f057c98 config: x86_64-randconfig-a001-20210522 (attached as .config) compiler: gcc-9 (Debian 9.3.0-22) 9.3.0 reproduce (this is a W=1 build): # https://github.com/0day-ci/linux/commit/290ddae8f6cfa354bd4b518bfa86823478e53f5e git remote add linux-review https://github.com/0day-ci/linux git fetch --no-tags linux-review Kuppuswamy-Sathyanarayanan/x86-boot-Add-a-trampoline-for-APs-booting-in-64-bit-mode/20210522-211940 git checkout 290ddae8f6cfa354bd4b518bfa86823478e53f5e # save the attached .config to linux build tree make W=1 ARCH=x86_64
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
In file included from arch/x86/realmode/init.c:9:
arch/x86/include/asm/realmode.h: In function 'get_trampoline_start_ip':
>> arch/x86/include/asm/realmode.h:96:6: error: implicit declaration of function 'is_tdx_guest' [-Werror=implicit-function-declaration]
96 | if (is_tdx_guest())
| ^~~~~~~~~~~~cc1: some warnings being treated as errors
vim +/is_tdx_guest +96 arch/x86/include/asm/realmode.h
91
92 /* Common helper function to get start IP address */
93 static inline unsigned long get_trampoline_start_ip(struct real_mode_header *rmh)
94 {
95 #ifdef CONFIG_X86_64
> 96 if (is_tdx_guest())
97 return rmh->trampoline_start64;
98 #endif
99 return rmh->trampoline_start; 100 }
101
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
kernel test robot
build botRe: [RFC v2-fix-v2 1/1] x86/boot: Add a trampoline for APs booting in 64-bit mode
In reply to Kuppuswamy Sathyanarayanan[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch]
Hi Kuppuswamy,
[FYI, it's a private test report for your RFC patch.]
[auto build test ERROR on tip/x86/core]
[also build test ERROR on tip/master linux/master linus/master v5.13-rc2 next-20210521] [cannot apply to luto/next]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Kuppuswamy-Sathyanarayanan/x86-boot-Add-a-trampoline-for-APs-booting-in-64-bit-mode/20210522-211940 base: https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git 2c88d45edbb89029c1190bb3b136d2602f057c98 config: x86_64-randconfig-a013-20210522 (attached as .config) compiler: clang version 13.0.0 (https://github.com/llvm/llvm-project e84a9b9bb3051c35dea993cdad7b3d2575638f85)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# install x86_64 cross compiling tool for clang build
# apt-get install binutils-x86-64-linux-gnu
# https://github.com/0day-ci/linux/commit/290ddae8f6cfa354bd4b518bfa86823478e53f5e
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Kuppuswamy-Sathyanarayanan/x86-boot-Add-a-trampoline-for-APs-booting-in-64-bit-mode/20210522-211940 git checkout 290ddae8f6cfa354bd4b518bfa86823478e53f5e
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=x86_64
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
In file included from arch/x86/realmode/init.c:9:
>> arch/x86/include/asm/realmode.h:96:6: error: implicit declaration of function 'is_tdx_guest' [-Werror,-Wimplicit-function-declaration]
if (is_tdx_guest())
^1 error generated.
vim +/is_tdx_guest +96 arch/x86/include/asm/realmode.h
91
92 /* Common helper function to get start IP address */
93 static inline unsigned long get_trampoline_start_ip(struct real_mode_header *rmh)
94 {
95 #ifdef CONFIG_X86_64
> 96 if (is_tdx_guest())
97 return rmh->trampoline_start64;
98 #endif
99 return rmh->trampoline_start; 100 }
101
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
RFC v2 22/32 x86/boot: Avoid #VE during compressed boot for TDX platforms
In reply to Kuppuswamy SathyanarayananFrom: Sean Christopherson <[email protected]>
Avoid operations which will inject #VE during compressed boot, which is obviously fatal for TDX platforms.
Details are,
1. TDX module injects #VE if a TDX guest attempts to write EFER. So skip the WRMSR to set EFER.LME=1 if it's already set. TDX also forces EFER.LME=1, i.e. the branch will always be taken and thus the #VE avoided.
2. TDX module also injects a #VE if the guest attempts to clear CR0.NE. Ensure CR0.NE is set when loading CR0 during compressed boot. The Setting CR0.NE should be a nop on all CPUs that support 64-bit mode.
--- arch/x86/boot/compressed/head_64.S | 5 +++-- arch/x86/boot/compressed/pgtable.h | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-)
arch/x86/boot/compressed/head_64.S +3 -2
@@ -616,8 +616,9 @@ SYM_CODE_START(trampoline_32bit_src) movl $MSR_EFER, %ecx rdmsr btsl $_EFER_LME, %eax + jc 1f wrmsr - popl %edx +1: popl %edx popl %ecx /* Enable PAE and LA57 (if required) paging modes */
@@ -636,7 +637,7 @@ SYM_CODE_START(trampoline_32bit_src) pushl %eax /* Enable paging again */ - movl $(X86_CR0_PG | X86_CR0_PE), %eax + movl $(X86_CR0_PG | X86_CR0_NE | X86_CR0_PE), %eax movl %eax, %cr0 lret
arch/x86/boot/compressed/pgtable.h +1 -1
@@ -6,7 +6,7 @@#define TRAMPOLINE_32BIT_PGTABLE_OFFSET 0#define TRAMPOLINE_32BIT_CODE_OFFSET PAGE_SIZE -#define TRAMPOLINE_32BIT_CODE_SIZE 0x70 +#define TRAMPOLINE_32BIT_CODE_SIZE 0x80#define TRAMPOLINE_32BIT_STACK_END TRAMPOLINE_32BIT_SIZE
Signature · 1 line
2.25.1
Re: [RFC v2 22/32] x86/boot: Avoid #VE during compressed boot for TDX platforms
Quoted 6 lines · Kuppuswamy Sathyanarayanan
From: Sean Christopherson <[email protected]>
Avoid operations which will inject #VE during compressed boot, which is obviously fatal for TDX platforms.
Quoted 6 lines
Details are,
1. TDX module injects #VE if a TDX guest attempts to write EFER. So skip the WRMSR to set EFER.LME=1 if it's already set. TDX also forces EFER.LME=1, i.e. the branch will always be taken and thus the #VE avoided.
Ah here's the justification for that hunk in the previous patch, are you sure that hunk belongs in the trampoline patch?
Quoted 5 lines
2. TDX module also injects a #VE if the guest attempts to clear CR0.NE. Ensure CR0.NE is set when loading CR0 during compressed boot. The Setting CR0.NE should be a nop on all CPUs that support 64-bit mode.
Ah, here's the justification for CR0.NE in the previous patch. Did something go wrong in the patch splitting?
Quoted 49 lines
Signed-off-by Sean Christopherson <[email protected]>Reviewed-by Andi Kleen <[email protected]>Signed-off-by Kuppuswamy Sathyanarayanan <[email protected]>--- arch/x86/boot/compressed/head_64.S | 5 +++-- arch/x86/boot/compressed/pgtable.h | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-)arch/x86/boot/compressed/head_64.S +3
-2@@ -616,8 +616,9 @@ SYM_CODE_START(trampoline_32bit_src) movl $MSR_EFER, %ecx rdmsr btsl $_EFER_LME, %eax + jc 1f wrmsr - popl %edx +1: popl %edx popl %ecx /* Enable PAE and LA57 (if required) paging modes */@@ -636,7 +637,7 @@ SYM_CODE_START(trampoline_32bit_src) pushl %eax /* Enable paging again */ - movl $(X86_CR0_PG | X86_CR0_PE), %eax + movl $(X86_CR0_PG | X86_CR0_NE | X86_CR0_PE), %eax movl %eax, %cr0 lretarch/x86/boot/compressed/pgtable.h +1
-2@@ -6,7 +6,7 @@#define TRAMPOLINE_32BIT_PGTABLE_OFFSET 0#define TRAMPOLINE_32BIT_CODE_OFFSET PAGE_SIZE -#define TRAMPOLINE_32BIT_CODE_SIZE 0x70 +#define TRAMPOLINE_32BIT_CODE_SIZE 0x80#define TRAMPOLINE_32BIT_STACK_END TRAMPOLINE_32BIT_SIZE --2.25.1
RFC v2 23/32 x86/boot: Avoid unnecessary #VE during boot process
In reply to Kuppuswamy SathyanarayananFrom: Sean Christopherson <[email protected]>
Skip writing EFER during secondary_startup_64() if the current value is also the desired value. This avoids a #VE when running as a TDX guest, as the TDX-Module does not allow writes to EFER (even when writing the current, fixed value).
Also, preserve CR4.MCE instead of clearing it during boot to avoid a #VE when running as a TDX guest. The TDX-Module (effectively part of the hypervisor) requires CR4.MCE to be set at all times and injects a #VE if the guest attempts to clear CR4.MCE.
--- arch/x86/boot/compressed/head_64.S | 5 ++++- arch/x86/kernel/head_64.S | 13 +++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-)
arch/x86/boot/compressed/head_64.S +4 -1
@@ -622,7 +622,10 @@ SYM_CODE_START(trampoline_32bit_src) popl %ecx /* Enable PAE and LA57 (if required) paging modes */ - movl $X86_CR4_PAE, %eax + movl %cr4, %eax + /* Clearing CR4.MCE will #VE on TDX guests. Leave it alone. */ + andl $X86_CR4_MCE, %eax + orl $X86_CR4_PAE, %eax testl %edx, %edx jz 1f orl $X86_CR4_LA57, %eax
arch/x86/kernel/head_64.S +11 -2
@@ -141,7 +141,10 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) 1: /* Enable PAE mode, PGE and LA57 */ - movl $(X86_CR4_PAE | X86_CR4_PGE), %ecx + movq %cr4, %rcx + /* Clearing CR4.MCE will #VE on TDX guests. Leave it alone. */ + andl $X86_CR4_MCE, %ecx + orl $(X86_CR4_PAE | X86_CR4_PGE), %ecx #ifdef CONFIG_X86_5LEVEL testl $1, __pgtable_l5_enabled(%rip) jz 1f
@@ -229,13 +232,19 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) /* Setup EFER (Extended Feature Enable Register) */ movl $MSR_EFER, %ecx rdmsr + movl %eax, %edx btsl $_EFER_SCE, %eax /* Enable System Call */ btl $20,%edi /* No Execute supported? */ jnc 1f btsl $_EFER_NX, %eax btsq $_PAGE_BIT_NX,early_pmd_flags(%rip) -1: wrmsr /* Make changes effective */ + /* Skip the WRMSR if the current value matches the desired value. */ +1: cmpl %edx, %eax + je 1f + xor %edx, %edx + wrmsr /* Make changes effective */ +1: /* Setup cr0 */ movl $CR0_STATE, %eax /* Make changes effective */
Signature · 1 line
2.25.1
Re: [RFC v2 23/32] x86/boot: Avoid unnecessary #VE during boot process
Quoted 54 lines · Kuppuswamy Sathyanarayanan
From: Sean Christopherson <[email protected]>
Skip writing EFER during
secondary_startup_64()if the current value is also the desired value. This avoids a #VE when running as a TDX guest, as the TDX-Module does not allow writes to EFER (even when writing the current, fixed value).Also, preserve CR4.MCE instead of clearing it during boot to avoid a #VE when running as a TDX guest. The TDX-Module (effectively part of the hypervisor) requires CR4.MCE to be set at all times and injects a #VE if the guest attempts to clear CR4.MCE.
Signed-off-by Sean Christopherson <[email protected]>Reviewed-by Andi Kleen <[email protected]>Signed-off-by Kuppuswamy Sathyanarayanan <[email protected]>--- arch/x86/boot/compressed/head_64.S | 5 ++++- arch/x86/kernel/head_64.S | 13 +++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-)arch/x86/boot/compressed/head_64.S +4
-1@@ -622,7 +622,10 @@ SYM_CODE_START(trampoline_32bit_src) popl %ecx /* Enable PAE and LA57 (if required) paging modes */ - movl $X86_CR4_PAE, %eax + movl %cr4, %eax + /* Clearing CR4.MCE will #VE on TDX guests. Leave it alone. */ + andl $X86_CR4_MCE, %eax + orl $X86_CR4_PAE, %eax testl %edx, %edx jz 1f orl $X86_CR4_LA57, %eaxarch/x86/kernel/head_64.S +4
-1@@ -141,7 +141,10 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) 1: /* Enable PAE mode, PGE and LA57 */ - movl $(X86_CR4_PAE | X86_CR4_PGE), %ecx + movq %cr4, %rcx + /* Clearing CR4.MCE will #VE on TDX guests. Leave it alone. */ + andl $X86_CR4_MCE, %ecx + orl $(X86_CR4_PAE | X86_CR4_PGE), %ecx #ifdef CONFIG_X86_5LEVEL testl $1, __pgtable_l5_enabled(%rip) jz 1f
arch/x86/kernel/head_64.S SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) · line 235
Quoted 4 lines
arch/x86/kernel/head_64.S +1
-0@@ -229,13 +232,19 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) /* Setup EFER (Extended Feature Enable Register) */ movl $MSR_EFER, %ecx rdmsr + movl %eax, %edx
Maybe comment that EFER is being saved here to check if the following enables are nops, but not a big deal.
...modulo whether the EFER wrmsr avoidance in PATCH 21 should move here.
Quoted 19 lines
arch/x86/kernel/head_64.S +6
-2btsl $_EFER_SCE, %eax /* Enable System Call */ btl $20,%edi /* No Execute supported? */ jnc 1f btsl $_EFER_NX, %eax btsq $_PAGE_BIT_NX,early_pmd_flags(%rip) -1: wrmsr /* Make changes effective */ + /* Skip the WRMSR if the current value matches the desired value. */ +1: cmpl %edx, %eax + je 1f + xor %edx, %edx + wrmsr /* Make changes effective */ +1: /* Setup cr0 */ movl $CR0_STATE, %eax /* Make changes effective */ --2.25.1
RFC 1/1 x86/boot: Avoid #VE during boot for TDX platforms
From: Sean Christopherson <[email protected]>
Avoid operations which will inject #VE during boot process, which is obviously fatal for TDX platforms.
Details are,
1. TDX module injects #VE if a TDX guest attempts to write EFER.
Boot code updates EFER in following cases:
* When enabling Long Mode configuration, EFER.LME bit will be set. Since TDX forces EFER.LME=1, we can skip updating it again. Check for EFER.LME before updating it and skip it if it is already set.
* EFER is also updated to enable support for features like System call and No Execute page setting. In TDX, these features are set up by the TDX module. So check whether it is already enabled, and skip enabling it again.
2. TDX module also injects a #VE if the guest attempts to clear CR0.NE. Ensure CR0.NE is set when loading CR0 during compressed boot. The Setting CR0.NE should be a nop on all CPUs that support 64-bit mode.
3. The TDX-Module (effectively part of the hypervisor) requires CR4.MCE to be set at all times and injects a #VE if the guest attempts to clear CR4.MCE. So, preserve CR4.MCE instead of clearing it during boot to avoid #VE.
---
Changes since RFC v2:
* Merged Avoid #VE related changes together.
* [RFC v2 22/32] x86/boot: Avoid #VE during compressed boot for TDX platforms
* [RFC v2 23/32] x86/boot: Avoid unnecessary #VE during boot process.
* Fixed commit log as per review comments.
arch/x86/boot/compressed/head_64.S | 10 +++++++--- arch/x86/kernel/head_64.S | 13 +++++++++++-- arch/x86/realmode/rm/trampoline_64.S | 11 +++++++++-- 3 files changed, 27 insertions(+), 7 deletions(-)
arch/x86/boot/compressed/head_64.S +7 -3
@@ -616,12 +616,16 @@ SYM_CODE_START(trampoline_32bit_src) movl $MSR_EFER, %ecx rdmsr btsl $_EFER_LME, %eax + jc 1f wrmsr - popl %edx +1: popl %edx popl %ecx /* Enable PAE and LA57 (if required) paging modes */ - movl $X86_CR4_PAE, %eax + movl %cr4, %eax + /* Clearing CR4.MCE will #VE on TDX guests. Leave it alone. */ + andl $X86_CR4_MCE, %eax + orl $X86_CR4_PAE, %eax testl %edx, %edx jz 1f orl $X86_CR4_LA57, %eax
@@ -636,7 +640,7 @@ SYM_CODE_START(trampoline_32bit_src) pushl %eax /* Enable paging again */ - movl $(X86_CR0_PG | X86_CR0_PE), %eax + movl $(X86_CR0_PG | X86_CR0_NE | X86_CR0_PE), %eax movl %eax, %cr0 lret
arch/x86/kernel/head_64.S +11 -2
@@ -141,7 +141,10 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) 1: /* Enable PAE mode, PGE and LA57 */ - movl $(X86_CR4_PAE | X86_CR4_PGE), %ecx + movq %cr4, %rcx + /* Clearing CR4.MCE will #VE on TDX guests. Leave it alone. */ + andl $X86_CR4_MCE, %ecx + orl $(X86_CR4_PAE | X86_CR4_PGE), %ecx #ifdef CONFIG_X86_5LEVEL testl $1, __pgtable_l5_enabled(%rip) jz 1f
@@ -229,13 +232,19 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) /* Setup EFER (Extended Feature Enable Register) */ movl $MSR_EFER, %ecx rdmsr + movl %eax, %edx btsl $_EFER_SCE, %eax /* Enable System Call */ btl $20,%edi /* No Execute supported? */ jnc 1f btsl $_EFER_NX, %eax btsq $_PAGE_BIT_NX,early_pmd_flags(%rip) -1: wrmsr /* Make changes effective */ + /* Skip the WRMSR if the current value matches the desired value. */ +1: cmpl %edx, %eax + je 1f + xor %edx, %edx + wrmsr /* Make changes effective */ +1: /* Setup cr0 */ movl $CR0_STATE, %eax /* Make changes effective */
arch/x86/realmode/rm/trampoline_64.S +9 -2
@@ -143,13 +143,20 @@ SYM_CODE_START(startup_32) movl %eax, %cr3 # Set up EFER + movl $MSR_EFER, %ecx + rdmsr + cmp pa_tr_efer, %eax + jne .Lwrite_efer + cmp pa_tr_efer + 4, %edx + je .Ldone_efer +.Lwrite_efer: movl pa_tr_efer, %eax movl pa_tr_efer + 4, %edx - movl $MSR_EFER, %ecx wrmsr +.Ldone_efer: # Enable paging and in turn activate Long Mode - movl $(X86_CR0_PG | X86_CR0_WP | X86_CR0_PE), %eax + movl $(X86_CR0_PG | X86_CR0_WP | X86_CR0_NE | X86_CR0_PE), %eax movl %eax, %cr0 /*
Signature · 1 line
2.25.1
Re: [RFC v2-fix 1/1] x86/boot: Avoid #VE during boot for TDX platforms
Quoted 4 lines · Kuppuswamy Sathyanarayanan
From: Sean Christopherson <[email protected]>
Avoid operations which will inject #VE during boot process, which is obviously fatal for TDX platforms.
It's not "obviously fatal". We actually have early exception handlers.
Please give an actual reason. "They're easy to avoid, and that sure
beats handling the exceptions" is a perfectly fine reason.
Quoted 12 lines
Details are,
1. TDX module injects #VE if a TDX guest attempts to write EFER.
Boot code updates EFER in following cases:
* When enabling Long Mode configuration, EFER.LME bit will be set. Since TDX forces EFER.LME=1, we can skip updating it again. Check for EFER.LME before updating it and skip it if it is already set.
Quoted 11 lines
* EFER is also updated to enable support for features like System call and No Execute page setting. In TDX, these features are set up by the TDX module. So check whether it is already enabled, and skip enabling it again.
2. TDX module also injects a #VE if the guest attempts to clear CR0.NE. Ensure CR0.NE is set when loading CR0 during compressed boot. The Setting CR0.NE should be a nop on all CPUs that support 64-bit mode.
3. The TDX-Module (effectively part of the hypervisor) requires
So, after we've mentioned the TDX module a few times, *NOW* we feel the need to explain what it is? I'm also baffled by this little aside. Literally the WHOLE POINT FOR SEAM TO EXIST is that it is NOT PART OF THE HYPERVISOR. The whole point. Literally.
Quoted 3 lines
CR4.MCE to be set at all times and injects a #VE if the guest attempts to clear CR4.MCE. So, preserve CR4.MCE instead of clearing it during boot to avoid #VE.
This is a good example of a changelog run amok. It doesn't need to be an English language reproduction of the code. This is getting close.
This can all be replaced and improved with a high-level discussion of what is going on:
There are a few MSRs and control register bits which the kernel normally needs to modify during boot. But, TDX disallows modification of these registers to help provide consistent security guarantees. Fortunately, TDX ensures that these are all in the correct state before the kernel loads, which means the kernel has no need to modify them.
The conditions we need to avoid are:
1. Any writes to the EFER MSR
2. Clearing CR0.NE
3. Clearing CR3.MCE
arch/x86/boot/compressed/head_64.S SYM_CODE_START(trampoline_32bit_src) · line 621
Quoted 12 lines
arch/x86/boot/compressed/head_64.S +2
-1@@ -616,12 +616,16 @@ SYM_CODE_START(trampoline_32bit_src) movl $MSR_EFER, %ecx rdmsr btsl $_EFER_LME, %eax + jc 1f wrmsr - popl %edx +1: popl %edx
A comment would be nice:
/* Avoid writing EFER if no change was made (for TDX guest) */
arch/x86/boot/compressed/head_64.S SYM_CODE_START(trampoline_32bit_src) · line 627
Quoted 7 lines
arch/x86/boot/compressed/head_64.S +3
-1popl %ecx /* Enable PAE and LA57 (if required) paging modes */ - movl $X86_CR4_PAE, %eax + movl %cr4, %eax + /* Clearing CR4.MCE will #VE on TDX guests. Leave it alone. */ + andl $X86_CR4_MCE, %eax
Maybe I'm just dense today, but I was boggling about what this 'andl' is actually doing. This would help:
/* * Clear all bits except CR4.MCE, which is preserved. * Clearing CR4.MCE will #VE in TDX guests. */
arch/x86/boot/compressed/head_64.S SYM_CODE_START(trampoline_32bit_src) · line 644
Quoted 11 lines
arch/x86/boot/compressed/head_64.S +2
-1+ orl $X86_CR4_PAE, %eax testl %edx, %edx jz 1f orl $X86_CR4_LA57, %eax@@ -636,7 +640,7 @@ SYM_CODE_START(trampoline_32bit_src) pushl %eax /* Enable paging again */ - movl $(X86_CR0_PG | X86_CR0_PE), %eax + movl $(X86_CR0_PG | X86_CR0_NE | X86_CR0_PE), %eax movl %eax, %cr0
Shouldn't we also comment the X86_CR0_NE?
/* Enable paging again. Avoid clearing X86_CR0_NE for TDX. */
Quoted 6 lines
arch/x86/boot/compressed/head_64.S +0
-0lretarch/x86/kernel/head_64.S +0
-0
arch/x86/kernel/head_64.S SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) · line 147
Quoted 8 lines
arch/x86/kernel/head_64.S +4
-1@@ -141,7 +141,10 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) 1: /* Enable PAE mode, PGE and LA57 */ - movl $(X86_CR4_PAE | X86_CR4_PGE), %ecx + movq %cr4, %rcx + /* Clearing CR4.MCE will #VE on TDX guests. Leave it alone. */ + andl $X86_CR4_MCE, %ecx + orl $(X86_CR4_PAE | X86_CR4_PGE), %ecx
Ditto on the comment from above about clearing/preserving bits.
arch/x86/kernel/head_64.S SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) · line 235
Quoted 8 lines
arch/x86/kernel/head_64.S +1
-0#ifdef CONFIG_X86_5LEVEL testl $1, __pgtable_l5_enabled(%rip) jz 1f@@ -229,13 +232,19 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) /* Setup EFER (Extended Feature Enable Register) */ movl $MSR_EFER, %ecx rdmsr + movl %eax, %edx
Comment, please.
arch/x86/kernel/head_64.S SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) · line 242
Quoted 8 lines
arch/x86/kernel/head_64.S +1
-1btsl $_EFER_SCE, %eax /* Enable System Call */ btl $20,%edi /* No Execute supported? */ jnc 1f btsl $_EFER_NX, %eax btsq $_PAGE_BIT_NX,early_pmd_flags(%rip) -1: wrmsr /* Make changes effective */ + /* Skip the WRMSR if the current value matches the desired value. */
If I read this comment in 5 years, I'm going to ask "Why bother?". Please mention TDX.
Quoted 13 lines
arch/x86/kernel/head_64.S +5
-0+1: cmpl %edx, %eax + je 1f + xor %edx, %edx + wrmsr /* Make changes effective */ +1: /* Setup cr0 */ movl $CR0_STATE, %eax /* Make changes effective */arch/x86/realmode/rm/trampoline_64.S +0
-0
arch/x86/realmode/rm/trampoline_64.S SYM_CODE_START(startup_32) · line 150
Quoted 8 lines
arch/x86/realmode/rm/trampoline_64.S +5
-0@@ -143,13 +143,20 @@ SYM_CODE_START(startup_32) movl %eax, %cr3 # Set up EFER + movl $MSR_EFER, %ecx + rdmsr + cmp pa_tr_efer, %eax + jne .Lwrite_efer + cmp pa_tr_efer + 4, %edx
Comment, please:
# Skip EFER writes to avoid faults in TDX guests
Quoted 15 lines
arch/x86/realmode/rm/trampoline_64.S +4
-2+ je .Ldone_efer +.Lwrite_efer: movl pa_tr_efer, %eax movl pa_tr_efer + 4, %edx - movl $MSR_EFER, %ecx wrmsr +.Ldone_efer: # Enable paging and in turn activate Long Mode - movl $(X86_CR0_PG | X86_CR0_WP | X86_CR0_PE), %eax + movl $(X86_CR0_PG | X86_CR0_WP | X86_CR0_NE | X86_CR0_PE), %eax movl %eax, %cr0 /*
RFC 1/1 x86/boot: Avoid #VE during boot for TDX platforms
From: Sean Christopherson <[email protected]>
Avoid operations which will inject #VE during boot process. They're easy to avoid and it is less complex than handling the exceptions.
There are a few MSRs and control register bits which the kernel normally needs to modify during boot. But, TDX disallows modification of these registers to help provide consistent security guarantees ( and avoid generating #VE when updating them). Fortunately, TDX ensures that these are all in the correct state before the kernel loads, which means the kernel has no need to modify them.
The conditions we need to avoid are:
* Any writes to the EFER MSR
* Clearing CR0.NE
* Clearing CR3.MCE
---
Changes since RFC v2-fix:
* Fixed commit and comments as per Dave and Dan's suggestions.
* Merged CR0.NE related change in pa_trampoline_compat() from patch titled "x86/boot: Add a trampoline for APs booting in 64-bit mode" to this patch. It belongs in this patch.
* Merged TRAMPOLINE_32BIT_CODE_SIZE related change from patch titled "x86/boot: Add a trampoline for APs booting in 64-bit mode" to this patch (since it was wrongly merged to that patch during patch split).
arch/x86/boot/compressed/head_64.S | 16 ++++++++++++---- arch/x86/boot/compressed/pgtable.h | 2 +- arch/x86/kernel/head_64.S | 20 ++++++++++++++++++-- arch/x86/realmode/rm/trampoline_64.S | 23 +++++++++++++++++++---- 4 files changed, 50 insertions(+), 11 deletions(-)
arch/x86/boot/compressed/head_64.S +12 -4
@@ -616,12 +616,20 @@ SYM_CODE_START(trampoline_32bit_src) movl $MSR_EFER, %ecx rdmsr btsl $_EFER_LME, %eax + /* Avoid writing EFER if no change was made (for TDX guest) */ + jc 1f wrmsr - popl %edx +1: popl %edx popl %ecx /* Enable PAE and LA57 (if required) paging modes */ - movl $X86_CR4_PAE, %eax + movl %cr4, %eax + /* + * Clear all bits except CR4.MCE, which is preserved. + * Clearing CR4.MCE will #VE in TDX guests. + */ + andl $X86_CR4_MCE, %eax + orl $X86_CR4_PAE, %eax testl %edx, %edx jz 1f orl $X86_CR4_LA57, %eax
@@ -635,8 +643,8 @@ SYM_CODE_START(trampoline_32bit_src) pushl $__KERNEL_CS pushl %eax - /* Enable paging again */ - movl $(X86_CR0_PG | X86_CR0_PE), %eax + /* Enable paging again. Avoid clearing X86_CR0_NE for TDX */ + movl $(X86_CR0_PG | X86_CR0_NE | X86_CR0_PE), %eax movl %eax, %cr0 lret
arch/x86/boot/compressed/pgtable.h +1 -1
@@ -6,7 +6,7 @@#define TRAMPOLINE_32BIT_PGTABLE_OFFSET 0#define TRAMPOLINE_32BIT_CODE_OFFSET PAGE_SIZE -#define TRAMPOLINE_32BIT_CODE_SIZE 0x70 +#define TRAMPOLINE_32BIT_CODE_SIZE 0x80#define TRAMPOLINE_32BIT_STACK_END TRAMPOLINE_32BIT_SIZE
arch/x86/kernel/head_64.S +18 -2
@@ -141,7 +141,13 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) 1: /* Enable PAE mode, PGE and LA57 */ - movl $(X86_CR4_PAE | X86_CR4_PGE), %ecx + movq %cr4, %rcx + /* + * Clear all bits except CR4.MCE, which is preserved. + * Clearing CR4.MCE will #VE in TDX guests. + */ + andl $X86_CR4_MCE, %ecx + orl $(X86_CR4_PAE | X86_CR4_PGE), %ecx #ifdef CONFIG_X86_5LEVEL testl $1, __pgtable_l5_enabled(%rip) jz 1f
@@ -229,13 +235,23 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) /* Setup EFER (Extended Feature Enable Register) */ movl $MSR_EFER, %ecx rdmsr + /* + * Preserve current value of EFER for comparison and to skip + * EFER writes if no change was made (for TDX guest) + */ + movl %eax, %edx btsl $_EFER_SCE, %eax /* Enable System Call */ btl $20,%edi /* No Execute supported? */ jnc 1f btsl $_EFER_NX, %eax btsq $_PAGE_BIT_NX,early_pmd_flags(%rip) -1: wrmsr /* Make changes effective */ + /* Avoid writing EFER if no change was made (for TDX guest) */ +1: cmpl %edx, %eax + je 1f + xor %edx, %edx + wrmsr /* Make changes effective */ +1: /* Setup cr0 */ movl $CR0_STATE, %eax /* Make changes effective */
arch/x86/realmode/rm/trampoline_64.S +19 -4
@@ -143,13 +143,27 @@ SYM_CODE_START(startup_32) movl %eax, %cr3 # Set up EFER + movl $MSR_EFER, %ecx + rdmsr + /* + * Skip writing to EFER if the register already has desiered + * value (to avoid #VE for TDX guest). + */ + cmp pa_tr_efer, %eax + jne .Lwrite_efer + cmp pa_tr_efer + 4, %edx + je .Ldone_efer +.Lwrite_efer: movl pa_tr_efer, %eax movl pa_tr_efer + 4, %edx - movl $MSR_EFER, %ecx wrmsr - # Enable paging and in turn activate Long Mode - movl $(X86_CR0_PG | X86_CR0_WP | X86_CR0_PE), %eax +.Ldone_efer: + /* + * Enable paging and in turn activate Long Mode. Avoid clearing + * X86_CR0_NE for TDX. + */ + movl $(X86_CR0_PG | X86_CR0_WP | X86_CR0_NE | X86_CR0_PE), %eax movl %eax, %cr0 /*
@@ -169,7 +183,8 @@ SYM_CODE_START(pa_trampoline_compat) movl $rm_stack_end, %esp movw $__KERNEL_DS, %dx - movl $X86_CR0_PE, %eax + /* Avoid clearing X86_CR0_NE for TDX */ + movl $(X86_CR0_NE | X86_CR0_PE), %eax movl %eax, %cr0 ljmpl $__KERNEL32_CS, $pa_startup_32 SYM_CODE_END(pa_trampoline_compat)
Signature · 1 line
2.25.1
Re: [RFC v2-fix-v2 1/1] x86/boot: Avoid #VE during boot for TDX platforms
Quoted 3 lines
Avoid operations which will inject #VE during boot process. They're easy to avoid and it is less complex than handling the exceptions.
This puts the solution before the problem. I'd also make sure to clearly connect this solution to the problem. For instance, if you refer to register "modification", ensure that you reflect that language here. Don't call them "modifications" in one part of the changelog and "operations" here. I'd also qualify them as "superfluous".
Please reorder this in the following form:
1. Background
2. Problem
3. Solution
Please do this for all of your patches.
Quoted 5 lines
There are a few MSRs and control register bits which the kernel normally needs to modify during boot. But, TDX disallows modification of these registers to help provide consistent security guarantees ( and avoid generating #VE when updating them).
No, the TDX architecture does not avoid generating #VE. The *kernel* does that. This sentence conflates those two things.
Quoted 9 lines
Fortunately, TDX ensures that these are
all in the correct state before the kernel loads, which means the kernel has no need to modify them.The conditions we need to avoid are:
* Any writes to the EFER MSR
* Clearing CR0.NE
* Clearing CR3.MCE
Sathya, there have been repeated issues in your changelogs with "we's".
Remember, speak in imperative voice. Please fix this in your tooling
to find these so that reviewers don't have to.
Quoted 8 lines
arch/x86/kernel/head_64.S +5
-0+ /* + * Preserve current value of EFER for comparison and to skip + * EFER writes if no change was made (for TDX guest) + */ + movl %eax, %edx btsl $_EFER_SCE, %eax /* Enable System Call */ btl $20,%edi /* No Execute supported? */ jnc 1f
arch/x86/kernel/head_64.S SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) · line 254
Quoted 10 lines
arch/x86/kernel/head_64.S +6
-1btsl $_EFER_NX, %eax btsq $_PAGE_BIT_NX,early_pmd_flags(%rip) -1: wrmsr /* Make changes effective */ + /* Avoid writing EFER if no change was made (for TDX guest) */ +1: cmpl %edx, %eax + je 1f + xor %edx, %edx + wrmsr /* Make changes effective */ +1:
Just curious, but what if this goes wrong? Say the TDX firmware didn't set up EFER correctly and this code does the WRMSR. What ends up happening? Do we get anything out on the console, or is it essentially undebuggable?
arch/x86/realmode/rm/trampoline_64.S SYM_CODE_START(startup_32) · line 151
Quoted 5 lines
arch/x86/realmode/rm/trampoline_64.S +4
-0+ /* + * Skip writing to EFER if the register already has desiered + * value (to avoid #VE for TDX guest). + */
spelling ^
There are lots of editors that can do spell checking, even in C comments. You might want to look into that for your editor.
Sean Christopherson
maintainerRe: [RFC v2-fix-v2 1/1] x86/boot: Avoid #VE during boot for TDX platforms
Quoted 12 lines · Dave Hansen
Quoted 12 lines
arch/x86/kernel/head_64.S +5
-1+ /* + * Preserve current value of EFER for comparison and to skip + * EFER writes if no change was made (for TDX guest) + */ + movl %eax, %edx btsl $_EFER_SCE, %eax /* Enable System Call */ btl $20,%edi /* No Execute supported? */ jnc 1f btsl $_EFER_NX, %eax btsq $_PAGE_BIT_NX,early_pmd_flags(%rip) -1: wrmsr /* Make changes effective */
Quoted 9 lines
arch/x86/kernel/head_64.S SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) · line 254
Quoted 6 lines
arch/x86/kernel/head_64.S +6
-0+ /* Avoid writing EFER if no change was made (for TDX guest) */ +1: cmpl %edx, %eax + je 1f + xor %edx, %edx + wrmsr /* Make changes effective */ +1:Just curious, but what if this goes wrong? Say the TDX firmware didn't set up EFER correctly and this code does the WRMSR.
By firmware, do you mean TDX-module, or guest firmware? EFER is read-only in a TDX guest, i.e. the guest firmware can't change it either.
Quoted 2 lines
What ends up happening? Do we get anything out on the console, or is it essentially undebuggable?
Assuming "firmware" means TDX-module, if TDX-Module botches EFER (and only EFER) then odds are very, very good that the guest will never get to the kernel as it will have died long before in guest BIOS.
If the bug is such that EFER is correct in hardware, but RDMSR returns the wrong value (due to MSR interception), IIRC this will triple fault and so nothing will get logged. But, the odds of that type of bug being hit in production are practically zero because the EFER setup is very static, i.e. any such bug should be hit during qualification of the VMM+TDX-Module.
In any case, even if a bug escapes, the shutdown is relatively easy to debug even without logs because the failure will cleary point at the WRMSR (that info can be had by running a debug TD or a debug TDX-Module). By TDX standards, debugging shutdowns on a specific instruction is downright trivial :-).
Re: [RFC v2-fix-v2 1/1] x86/boot: Avoid #VE during boot for TDX platforms
Quoted 23 lines · Sean Christopherson
Quoted 12 lines · Dave Hansen
Quoted 12 lines
arch/x86/kernel/head_64.S +5
-1+ /* + * Preserve current value of EFER for comparison and to skip + * EFER writes if no change was made (for TDX guest) + */ + movl %eax, %edx btsl $_EFER_SCE, %eax /* Enable System Call */ btl $20,%edi /* No Execute supported? */ jnc 1f btsl $_EFER_NX, %eax btsq $_PAGE_BIT_NX,early_pmd_flags(%rip) -1: wrmsr /* Make changes effective */Quoted 9 lines
arch/x86/kernel/head_64.S SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) · line 254
Quoted 6 lines
arch/x86/kernel/head_64.S +6
-0+ /* Avoid writing EFER if no change was made (for TDX guest) */ +1: cmpl %edx, %eax + je 1f + xor %edx, %edx + wrmsr /* Make changes effective */ +1:Just curious, but what if this goes wrong? Say the TDX firmware didn't set up EFER correctly and this code does the WRMSR.
Quoted 2 lines
By firmware, do you mean TDX-module, or guest firmware? EFER is read-only in a TDX guest, i.e. the guest firmware can't change it either.
I guess I was assuming that the trusted BIOS was going to do the setup of EFER before it hands control over to the kernel. So, I *meant* the BIOS.
But, I see from below that it's probably the TDX-module that's responsible for this behavior.
Quoted 13 lines
Quoted 2 lines
What ends up happening? Do we get anything out on the console, or is it essentially undebuggable?
Assuming "firmware" means TDX-module, if TDX-Module botches EFER (and only EFER) then odds are very, very good that the guest will never get to the kernel as it will have died long before in guest BIOS.
If the bug is such that EFER is correct in hardware, but RDMSR returns the wrong value (due to MSR interception), IIRC this will triple fault and so nothing will get logged. But, the odds of that type of bug being hit in production are practically zero because the EFER setup is very static, i.e. any such bug should be hit during qualification of the VMM+TDX-Module.
Quoted 4 lines
In any case, even if a bug escapes, the shutdown is relatively easy to debug even without logs because the failure will cleary point at the WRMSR (that info can be had by running a debug TD or a debug TDX-Module). By TDX standards, debugging shutdowns on a specific instruction is downright trivial :-).
That sounds sane to me. It would be nice to get this into the changelog. Perhaps:
This theoretically makes guest boot more fragile. If, for instance, EER was set up incorrectly and a WRMSR was performed, the resulting (unhandled) #VE would triple fault. However, this is likely to trip up the guest BIOS long before control reaches the kernel. In any case, these kinds of problems are unlikely to occur in production environments, and developers have good debug tools to fix them quickly.
That would put my mind at ease a bit.
Re: [RFC v2-fix-v2 1/1] x86/boot: Avoid #VE during boot for TDX platforms
Quoted 11 lines · Dave Hansen
That sounds sane to me. It would be nice to get this into the changelog. Perhaps:
This theoretically makes guest boot more fragile. If, for instance, EER was set up incorrectly and a WRMSR was performed, the resulting (unhandled) #VE would triple fault. However, this is likely to trip up the guest BIOS long before control reaches the kernel. In any case, these kinds of problems are unlikely to occur in production environments, and developers have good debug tools to fix them quickly.
Quoted 1 line
That would put my mind at ease a bit.
I can add it to change log.
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
RFC 1/1 x86/boot: Avoid #VE during boot for TDX platforms
From: Sean Christopherson <[email protected]>
In TDX guests, Virtualization Exceptions (#VE) are delivered to TDX guests due to specific guest actions like MSR writes, CPUID leaf accesses or I/O access. But in early boot code, #VE cannot be allowed because the required exception handler setup support code is missing. If #VE is triggered without proper handler support, it would lead to triple fault or kernel hang. So, avoid operations which will inject #VE during boot process. They're easy to avoid and it is less complex than handling the exceptions.
There are a few MSRs and control register bits which the kernel normally needs to modify during boot. But, TDX disallows modification of these registers to help provide consistent security guarantees. Fortunately, TDX ensures that these are all in the correct state before the kernel loads, which means the kernel has no need to modify them.
The conditions to avoid are:
* Any writes to the EFER MSR
* Clearing CR0.NE
* Clearing CR3.MCE
This theoretically makes guest boot more fragile. If, for instance, EFER was set up incorrectly and a WRMSR was performed, the resulting (unhandled) #VE would triple fault. However, this is likely to trip up the guest BIOS long before control reaches the kernel. In any case, these kinds of problems are unlikely to occur in production environments, and developers have good debug tools to fix them quickly.
---
Changes since RFC v2-fix-v2:
* Fixed commit log as per review comments.
Changes since RFC v2-fix:
* Fixed commit and comments as per Dave and Dan's suggestions.
* Merged CR0.NE related change in pa_trampoline_compat() from patch titled "x86/boot: Add a trampoline for APs booting in 64-bit mode" to this patch. It belongs in this patch.
* Merged TRAMPOLINE_32BIT_CODE_SIZE related change from patch titled "x86/boot: Add a trampoline for APs booting in 64-bit mode" to this patch (since it was wrongly merged to that patch during patch split).
arch/x86/boot/compressed/head_64.S | 16 ++++++++++++---- arch/x86/boot/compressed/pgtable.h | 2 +- arch/x86/kernel/head_64.S | 20 ++++++++++++++++++-- arch/x86/realmode/rm/trampoline_64.S | 23 +++++++++++++++++++---- 4 files changed, 50 insertions(+), 11 deletions(-)
arch/x86/boot/compressed/head_64.S +12 -4
@@ -616,12 +616,20 @@ SYM_CODE_START(trampoline_32bit_src) movl $MSR_EFER, %ecx rdmsr btsl $_EFER_LME, %eax + /* Avoid writing EFER if no change was made (for TDX guest) */ + jc 1f wrmsr - popl %edx +1: popl %edx popl %ecx /* Enable PAE and LA57 (if required) paging modes */ - movl $X86_CR4_PAE, %eax + movl %cr4, %eax + /* + * Clear all bits except CR4.MCE, which is preserved. + * Clearing CR4.MCE will #VE in TDX guests. + */ + andl $X86_CR4_MCE, %eax + orl $X86_CR4_PAE, %eax testl %edx, %edx jz 1f orl $X86_CR4_LA57, %eax
@@ -635,8 +643,8 @@ SYM_CODE_START(trampoline_32bit_src) pushl $__KERNEL_CS pushl %eax - /* Enable paging again */ - movl $(X86_CR0_PG | X86_CR0_PE), %eax + /* Enable paging again. Avoid clearing X86_CR0_NE for TDX */ + movl $(X86_CR0_PG | X86_CR0_NE | X86_CR0_PE), %eax movl %eax, %cr0 lret
arch/x86/boot/compressed/pgtable.h +1 -1
@@ -6,7 +6,7 @@#define TRAMPOLINE_32BIT_PGTABLE_OFFSET 0#define TRAMPOLINE_32BIT_CODE_OFFSET PAGE_SIZE -#define TRAMPOLINE_32BIT_CODE_SIZE 0x70 +#define TRAMPOLINE_32BIT_CODE_SIZE 0x80#define TRAMPOLINE_32BIT_STACK_END TRAMPOLINE_32BIT_SIZE
arch/x86/kernel/head_64.S +18 -2
@@ -141,7 +141,13 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) 1: /* Enable PAE mode, PGE and LA57 */ - movl $(X86_CR4_PAE | X86_CR4_PGE), %ecx + movq %cr4, %rcx + /* + * Clear all bits except CR4.MCE, which is preserved. + * Clearing CR4.MCE will #VE in TDX guests. + */ + andl $X86_CR4_MCE, %ecx + orl $(X86_CR4_PAE | X86_CR4_PGE), %ecx #ifdef CONFIG_X86_5LEVEL testl $1, __pgtable_l5_enabled(%rip) jz 1f
@@ -229,13 +235,23 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) /* Setup EFER (Extended Feature Enable Register) */ movl $MSR_EFER, %ecx rdmsr + /* + * Preserve current value of EFER for comparison and to skip + * EFER writes if no change was made (for TDX guest) + */ + movl %eax, %edx btsl $_EFER_SCE, %eax /* Enable System Call */ btl $20,%edi /* No Execute supported? */ jnc 1f btsl $_EFER_NX, %eax btsq $_PAGE_BIT_NX,early_pmd_flags(%rip) -1: wrmsr /* Make changes effective */ + /* Avoid writing EFER if no change was made (for TDX guest) */ +1: cmpl %edx, %eax + je 1f + xor %edx, %edx + wrmsr /* Make changes effective */ +1: /* Setup cr0 */ movl $CR0_STATE, %eax /* Make changes effective */
arch/x86/realmode/rm/trampoline_64.S +19 -4
@@ -143,13 +143,27 @@ SYM_CODE_START(startup_32) movl %eax, %cr3 # Set up EFER + movl $MSR_EFER, %ecx + rdmsr + /* + * Skip writing to EFER if the register already has desiered + * value (to avoid #VE for TDX guest). + */ + cmp pa_tr_efer, %eax + jne .Lwrite_efer + cmp pa_tr_efer + 4, %edx + je .Ldone_efer +.Lwrite_efer: movl pa_tr_efer, %eax movl pa_tr_efer + 4, %edx - movl $MSR_EFER, %ecx wrmsr - # Enable paging and in turn activate Long Mode - movl $(X86_CR0_PG | X86_CR0_WP | X86_CR0_PE), %eax +.Ldone_efer: + /* + * Enable paging and in turn activate Long Mode. Avoid clearing + * X86_CR0_NE for TDX. + */ + movl $(X86_CR0_PG | X86_CR0_WP | X86_CR0_NE | X86_CR0_PE), %eax movl %eax, %cr0 /*
@@ -169,7 +183,8 @@ SYM_CODE_START(pa_trampoline_compat) movl $rm_stack_end, %esp movw $__KERNEL_DS, %dx - movl $X86_CR0_PE, %eax + /* Avoid clearing X86_CR0_NE for TDX */ + movl $(X86_CR0_NE | X86_CR0_PE), %eax movl %eax, %cr0 ljmpl $__KERNEL32_CS, $pa_startup_32 SYM_CODE_END(pa_trampoline_compat)
Signature · 1 line
2.25.1
RFC 1/1 x86/boot: Avoid #VE during boot for TDX platforms
From: Sean Christopherson <[email protected]>
There are a few MSRs and control register bits which the kernel normally needs to modify during boot. But, TDX disallows modification of these registers to help provide consistent security guarantees. Fortunately, TDX ensures that these are all in the correct state before the kernel loads, which means the kernel has no need to modify them.
The conditions to avoid are:
* Any writes to the EFER MSR
* Clearing CR0.NE
* Clearing CR3.MCE
This theoretically makes guest boot more fragile. If, for instance, EFER was set up incorrectly and a WRMSR was performed, it will trigger early exception panic or a triple fault, if it's before early exceptions are set up. However, this is likely to trip up the guest BIOS long before control reaches the kernel. In any case, these kinds of problems are unlikely to occur in production environments, and developers have good debug tools to fix them quickly.
---
Changes since RFC v2-fix-v3:
* Removed uncessary contents from commit log. No code changes.
Changes since RFC v2-fix-v2:
* Fixed commit log as per review comments.
Changes since RFC v2-fix:
* Fixed commit and comments as per Dave and Dan's suggestions.
* Merged CR0.NE related change in pa_trampoline_compat() from patch titled "x86/boot: Add a trampoline for APs booting in 64-bit mode" to this patch. It belongs in this patch.
* Merged TRAMPOLINE_32BIT_CODE_SIZE related change from patch titled "x86/boot: Add a trampoline for APs booting in 64-bit mode" to this patch (since it was wrongly merged to that patch during patch split).
arch/x86/boot/compressed/head_64.S | 16 ++++++++++++---- arch/x86/boot/compressed/pgtable.h | 2 +- arch/x86/kernel/head_64.S | 20 ++++++++++++++++++-- arch/x86/realmode/rm/trampoline_64.S | 23 +++++++++++++++++++---- 4 files changed, 50 insertions(+), 11 deletions(-)
arch/x86/boot/compressed/head_64.S +12 -4
@@ -616,12 +616,20 @@ SYM_CODE_START(trampoline_32bit_src) movl $MSR_EFER, %ecx rdmsr btsl $_EFER_LME, %eax + /* Avoid writing EFER if no change was made (for TDX guest) */ + jc 1f wrmsr - popl %edx +1: popl %edx popl %ecx /* Enable PAE and LA57 (if required) paging modes */ - movl $X86_CR4_PAE, %eax + movl %cr4, %eax + /* + * Clear all bits except CR4.MCE, which is preserved. + * Clearing CR4.MCE will #VE in TDX guests. + */ + andl $X86_CR4_MCE, %eax + orl $X86_CR4_PAE, %eax testl %edx, %edx jz 1f orl $X86_CR4_LA57, %eax
@@ -635,8 +643,8 @@ SYM_CODE_START(trampoline_32bit_src) pushl $__KERNEL_CS pushl %eax - /* Enable paging again */ - movl $(X86_CR0_PG | X86_CR0_PE), %eax + /* Enable paging again. Avoid clearing X86_CR0_NE for TDX */ + movl $(X86_CR0_PG | X86_CR0_NE | X86_CR0_PE), %eax movl %eax, %cr0 lret
arch/x86/boot/compressed/pgtable.h +1 -1
@@ -6,7 +6,7 @@#define TRAMPOLINE_32BIT_PGTABLE_OFFSET 0#define TRAMPOLINE_32BIT_CODE_OFFSET PAGE_SIZE -#define TRAMPOLINE_32BIT_CODE_SIZE 0x70 +#define TRAMPOLINE_32BIT_CODE_SIZE 0x80#define TRAMPOLINE_32BIT_STACK_END TRAMPOLINE_32BIT_SIZE
arch/x86/kernel/head_64.S +18 -2
@@ -141,7 +141,13 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) 1: /* Enable PAE mode, PGE and LA57 */ - movl $(X86_CR4_PAE | X86_CR4_PGE), %ecx + movq %cr4, %rcx + /* + * Clear all bits except CR4.MCE, which is preserved. + * Clearing CR4.MCE will #VE in TDX guests. + */ + andl $X86_CR4_MCE, %ecx + orl $(X86_CR4_PAE | X86_CR4_PGE), %ecx #ifdef CONFIG_X86_5LEVEL testl $1, __pgtable_l5_enabled(%rip) jz 1f
@@ -229,13 +235,23 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) /* Setup EFER (Extended Feature Enable Register) */ movl $MSR_EFER, %ecx rdmsr + /* + * Preserve current value of EFER for comparison and to skip + * EFER writes if no change was made (for TDX guest) + */ + movl %eax, %edx btsl $_EFER_SCE, %eax /* Enable System Call */ btl $20,%edi /* No Execute supported? */ jnc 1f btsl $_EFER_NX, %eax btsq $_PAGE_BIT_NX,early_pmd_flags(%rip) -1: wrmsr /* Make changes effective */ + /* Avoid writing EFER if no change was made (for TDX guest) */ +1: cmpl %edx, %eax + je 1f + xor %edx, %edx + wrmsr /* Make changes effective */ +1: /* Setup cr0 */ movl $CR0_STATE, %eax /* Make changes effective */
arch/x86/realmode/rm/trampoline_64.S +19 -4
@@ -143,13 +143,27 @@ SYM_CODE_START(startup_32) movl %eax, %cr3 # Set up EFER + movl $MSR_EFER, %ecx + rdmsr + /* + * Skip writing to EFER if the register already has desired + * value (to avoid #VE for the TDX guest). + */ + cmp pa_tr_efer, %eax + jne .Lwrite_efer + cmp pa_tr_efer + 4, %edx + je .Ldone_efer +.Lwrite_efer: movl pa_tr_efer, %eax movl pa_tr_efer + 4, %edx - movl $MSR_EFER, %ecx wrmsr - # Enable paging and in turn activate Long Mode - movl $(X86_CR0_PG | X86_CR0_WP | X86_CR0_PE), %eax +.Ldone_efer: + /* + * Enable paging and in turn activate Long Mode. Avoid clearing + * X86_CR0_NE for TDX. + */ + movl $(X86_CR0_PG | X86_CR0_WP | X86_CR0_NE | X86_CR0_PE), %eax movl %eax, %cr0 /*
@@ -169,7 +183,8 @@ SYM_CODE_START(pa_trampoline_compat) movl $rm_stack_end, %esp movw $__KERNEL_DS, %dx - movl $X86_CR0_PE, %eax + /* Avoid clearing X86_CR0_NE for TDX */ + movl $(X86_CR0_NE | X86_CR0_PE), %eax movl %eax, %cr0 ljmpl $__KERNEL32_CS, $pa_startup_32 SYM_CODE_END(pa_trampoline_compat)
Signature · 1 line
2.25.1
Re: [RFC v2-fix-v4 1/1] x86/boot: Avoid #VE during boot for TDX platforms
Quoted 25 lines · Kuppuswamy Sathyanarayanan
From: Sean Christopherson <[email protected]>
There are a few MSRs and control register bits which the kernel normally needs to modify during boot. But, TDX disallows modification of these registers to help provide consistent security guarantees. Fortunately, TDX ensures that these are all in the correct state before the kernel loads, which means the kernel has no need to modify them.
The conditions to avoid are:
* Any writes to the EFER MSR
* Clearing CR0.NE
* Clearing CR3.MCEThis theoretically makes guest boot more fragile. If, for instance, EFER was set up incorrectly and a WRMSR was performed, it will trigger early exception panic or a triple fault, if it's before early exceptions are set up. However, this is likely to trip up the guest BIOS long before control reaches the kernel. In any case, these kinds of problems are unlikely to occur in production environments, and developers have good debug tools to fix them quickly.
Quoted 3 lines
Signed-off-by Sean Christopherson <[email protected]>Reviewed-by Andi Kleen <[email protected]>Signed-off-by Kuppuswamy Sathyanarayanan <[email protected]>
Looks good to me:
Re: [RFC v2-fix-v2 1/1] x86/boot: Avoid #VE during boot for TDX platforms
In reply to Dave HansenQuoted 33 lines · Dave Hansen
Quoted 3 lines
Avoid operations which will inject #VE during boot process. They're easy to avoid and it is less complex than handling the exceptions.
This puts the solution before the problem. I'd also make sure to clearly connect this solution to the problem. For instance, if you refer to register "modification", ensure that you reflect that language here. Don't call them "modifications" in one part of the changelog and "operations" here. I'd also qualify them as "superfluous".
Please reorder this in the following form:
1. Background
2. Problem
3. SolutionPlease do this for all of your patches.
Quoted 5 lines
There are a few MSRs and control register bits which the kernel normally needs to modify during boot. But, TDX disallows modification of these registers to help provide consistent security guarantees ( and avoid generating #VE when updating them).
No, the TDX architecture does not avoid generating #VE. The *kernel* does that. This sentence conflates those two things.
Quoted 6 lines
Fortunately, TDX ensures that these are
all in the correct state before the kernel loads, which means the kernel has no need to modify them.The conditions we need to avoid are:
Quoted 7 lines
Quoted 3 lines
* Any writes to the EFER MSR
* Clearing CR0.NE
* Clearing CR3.MCESathya, there have been repeated issues in your changelogs with "we's".
Remember, speak in imperative voice. Please fix this in your tooling
to find these so that reviewers don't have to.
How about the following commit log?
In TDX guests, Virtualization Exceptions (#VE) are delivered to TDX guests due to specific guest actions like MSR writes, CPUID leaf accesses or I/O access. But in early boot code, #VE cannot be allowed because the required exception handler setup support code is missing. If #VE is triggered without proper handler support, it would lead to triple fault or kernel hang. So, avoid operations which will inject #VE during boot process. They're easy to avoid and it is less complex than handling the exceptions.
There are a few MSRs and control register bits which the kernel normally needs to modify during boot. But, TDX disallows modification of these registers to help provide consistent security guarantees. Fortunately, TDX ensures that these are all in the correct state before the kernel loads, which means the kernel has no need to modify them.
The conditions to avoid are:
* Any writes to the EFER MSR
* Clearing CR0.NE
* Clearing CR3.MCE
If above conditions are not avoided, it would lead to triple fault or kernel hang.
Quoted 13 lines
Quoted 13 lines
arch/x86/kernel/head_64.S +5
-1+ /* + * Preserve current value of EFER for comparison and to skip + * EFER writes if no change was made (for TDX guest) + */ + movl %eax, %edx btsl $_EFER_SCE, %eax /* Enable System Call */ btl $20,%edi /* No Execute supported? */ jnc 1f btsl $_EFER_NX, %eax btsq $_PAGE_BIT_NX,early_pmd_flags(%rip) -1: wrmsr /* Make changes effective */
Quoted 10 lines
arch/x86/kernel/head_64.S SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) · line 254
Quoted 6 lines
arch/x86/kernel/head_64.S +6
-0+ /* Avoid writing EFER if no change was made (for TDX guest) */ +1: cmpl %edx, %eax + je 1f + xor %edx, %edx + wrmsr /* Make changes effective */ +1:Just curious, but what if this goes wrong? Say the TDX firmware didn't set up EFER correctly and this code does the WRMSR. What ends up happening?
It would lead to triple fault.
Do we get anything out on the console, or is it essentially
Quoted 2 lines
undebuggable?
We can still get logs with debug TDX module. So it is still debugable.
Quoted 12 lines
arch/x86/realmode/rm/trampoline_64.S SYM_CODE_START(startup_32) · line 151
Quoted 5 lines
arch/x86/realmode/rm/trampoline_64.S +4
-0+ /* + * Skip writing to EFER if the register already has desiered + * value (to avoid #VE for TDX guest). + */spelling ^
There are lots of editors that can do spell checking, even in C comments. You might want to look into that for your editor.
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
RFC v2 24/32 x86/topology: Disable CPU online/offline control for TDX guest
In reply to Kuppuswamy SathyanarayananAs per Intel TDX Virtual Firmware Design Guide, sec 4.3.5 and sec 9.4, all unused CPUs are put in spinning state by TDVF until OS requests for CPU bring-up via mailbox address passed by ACPI MADT table. Since by default all unused CPUs are always in spinning state, there is no point in supporting dynamic CPU online/offline feature. So current generation of TDVF does not support CPU hotplug feature. It may be supported in next generation.
--- arch/x86/kernel/tdx.c | 14 ++++++++++++++ arch/x86/kernel/topology.c | 3 ++- 2 files changed, 16 insertions(+), 1 deletion(-)
arch/x86/kernel/tdx.c +14 -0
@@ -316,6 +316,17 @@ static int tdg_handle_mmio(struct pt_regs *regs, struct ve_info *ve)return insn.length; } +static int tdg_cpu_offline_prepare (unsigned int cpu) +{ +/* +* Per Intel TDX Virtual Firmware Design Guide, +* sec 4.3.5 and sec 9.4, Hotplug is not supported +* in TDX platforms. So don't support CPU +* offline feature once its turned on. +*/ +return -EOPNOTSUPP ; +} +unsigned long tdg_get_ve_info (struct ve_info *ve) {u64 ret;
@@ -410,5 +421,8 @@ void __init tdx_early_init(void) pv_ops.irq.safe_halt = tdg_safe_halt; pv_ops.irq.halt = tdg_halt; +cpuhp_setup_state (CPUHP_AP_ONLINE_DYN ,"tdg:cpu_hotplug" , +NULL , tdg_cpu_offline_prepare); +pr_info ("TDX guest is initialized\n" ); }
arch/x86/kernel/topology.c +2 -1
@@ -34,6 +34,7 @@#include <linux/irq.h> #include <asm/io_apic.h> #include <asm/cpu.h> +#include <asm/tdx.h> static DEFINE_PER_CPU(struct x86_cpu, cpu_devices);
@@ -130,7 +131,7 @@ int arch_register_cpu(int num) } } } -if (num || cpu0_hotpluggable) +if ((num || cpu0_hotpluggable) && !is_tdx_guest ())per_cpu (cpu_devices, num).cpu.hotpluggable =1 ;return register_cpu (&per_cpu (cpu_devices, num).cpu, num);
Signature · 1 line
2.25.1
RFC v2 25/32 x86/tdx: Forcefully disable legacy PIC for TDX guests
In reply to Kuppuswamy SathyanarayananFrom: Sean Christopherson <[email protected]>
Disable the legacy PIC (8259) for TDX guests as the PIC cannot be supported by the VMM. TDX Module does not allow direct IRQ injection, and using posted interrupt style delivery requires the guest to EOI the IRQ, which diverges from the legacy PIC behavior.
--- arch/x86/kernel/tdx.c | 3 +++ 1 file changed, 3 insertions(+)
arch/x86/kernel/tdx.c +3 -0
@@ -4,6 +4,7 @@#define pr_fmt (fmt) "TDX: " fmt#include <asm/tdx.h> +#include <asm/i8259.h> #include <asm/vmx.h> #include <asm/insn.h> #include <linux/sched/signal.h> /* force_sig_fault() */
@@ -421,6 +422,8 @@ void __init tdx_early_init(void) pv_ops.irq.safe_halt = tdg_safe_halt; pv_ops.irq.halt = tdg_halt; + legacy_pic = &null_legacy_pic; +cpuhp_setup_state (CPUHP_AP_ONLINE_DYN ,"tdg:cpu_hotplug" ,NULL , tdg_cpu_offline_prepare);
Signature · 1 line
2.25.1
RFC v2 26/32 x86/mm: Move force_dma_unencrypted() to common code
In reply to Kuppuswamy SathyanarayananFrom: "Kirill A. Shutemov" <[email protected]>
Intel TDX doesn't allow VMM to access guest memory. Any memory that is required for communication with VMM must be shared explicitly by setting the bit in page table entry. And, after setting the shared bit, the conversion must be completed with MapGPA TDVMALL. The call informs VMM about the conversion and makes it remove the GPA from the S-EPT mapping. The shared memory is similar to unencrypted memory in AMD SME/SEV terminology but the underlying process of sharing/un-sharing the memory is different for Intel TDX guest platform.
SEV assumes that I/O devices can only do DMA to "decrypted" physical addresses without the C-bit set. In order for the CPU to interact with this memory, the CPU needs a decrypted mapping. To add this support, AMD SME code forces force_dma_unencrypted() to return true for platforms that support AMD SEV feature. It will be used for DMA memory allocation API to trigger set_memory_decrypted() for platforms that support AMD SEV feature.
TDX is similar. TDX architecturally prevents access to private guest memory by anything other than the guest itself. This means that any DMA buffers must be shared.
So move force_dma_unencrypted() out of AMD specific code.
It will be modified to return true for Intel TDX guest platform, similar to AMD SEV feature.
Introduce new config option X86_MEM_ENCRYPT_COMMON that has to be selected by all x86 memory encryption features. This will be selected by both AMD SEV and Intel TDX guest config options.
This is preparation for TDX changes in DMA code and it has not functional change.
--- arch/x86/Kconfig | 8 +++++-- arch/x86/mm/Makefile | 2 ++ arch/x86/mm/mem_encrypt.c | 30 ------------------------- arch/x86/mm/mem_encrypt_common.c | 38 ++++++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 32 deletions(-) create mode 100644 arch/x86/mm/mem_encrypt_common.c
arch/x86/Kconfig +6 -2
@@ -1529,14 +1529,18 @@ config X86_CPA_STATISTICS helps to determine the effectiveness of preserving large and huge page mappings when mapping protections are changed. +config X86_MEM_ENCRYPT_COMMON + select ARCH_HAS_FORCE_DMA_UNENCRYPTED + select DYNAMIC_PHYSICAL_MASK + def_bool n + config AMD_MEM_ENCRYPT bool "AMD Secure Memory Encryption (SME) support" depends on X86_64 && CPU_SUP_AMD select DMA_COHERENT_POOL - select DYNAMIC_PHYSICAL_MASK select ARCH_USE_MEMREMAP_PROT - select ARCH_HAS_FORCE_DMA_UNENCRYPTED select INSTRUCTION_DECODER + select X86_MEM_ENCRYPT_COMMON help Say yes to enable support for the encryption of system memory. This requires an AMD processor that supports Secure Memory
arch/x86/mm/Makefile +2 -0
@@ -52,6 +52,8 @@ obj-$(CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS) += pkeys.o obj-$(CONFIG_RANDOMIZE_MEMORY) += kaslr.o obj-$(CONFIG_PAGE_TABLE_ISOLATION) += pti.o +obj-$(CONFIG_X86_MEM_ENCRYPT_COMMON) += mem_encrypt_common.o + obj-$(CONFIG_AMD_MEM_ENCRYPT) += mem_encrypt.o obj-$(CONFIG_AMD_MEM_ENCRYPT) += mem_encrypt_identity.o obj-$(CONFIG_AMD_MEM_ENCRYPT) += mem_encrypt_boot.o
arch/x86/mm/mem_encrypt.c +0 -30
@@ -15,10 +15,6 @@#include <linux/dma-direct.h> #include <linux/swiotlb.h> #include <linux/mem_encrypt.h> -#include <linux/device.h> -#include <linux/kernel.h> -#include <linux/bitops.h> -#include <linux/dma-mapping.h> #include <asm/tlbflush.h> #include <asm/fixmap.h>
@@ -390,32 +386,6 @@ bool noinstr sev_es_active(void)return sev_status &MSR_AMD64_SEV_ES_ENABLED ; } -/* Override for DMA direct allocation check - ARCH_HAS_FORCE_DMA_UNENCRYPTED */ -bool force_dma_unencrypted (struct device *dev) -{ -/* -* For SEV, all DMA must be to unencrypted addresses. -*/ -if (sev_active ()) -return true; - -/* -* For SME, all DMA must be to unencrypted addresses if the -* device does not support DMA to addresses that include the -* encryption mask. -*/ -if (sme_active ()) { -u64 dma_enc_mask =DMA_BIT_MASK (__ffs64 (sme_me_mask)); -u64 dma_dev_mask =min_not_zero (dev->coherent_dma_mask, - dev->bus_dma_limit); - -if (dma_dev_mask <= dma_enc_mask) -return true; - } - -return false; -} -void __init mem_encrypt_free_decrypted_mem (void ) {unsigned long vaddr, vaddr_end, npages;
arch/x86/mm/mem_encrypt_common.c new file +38 -0
@@ -0,0 +1,38 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* AMD Memory Encryption Support +* +* Copyright (C) 2016 Advanced Micro Devices, Inc. +* +* Author: Tom Lendacky <[email protected]> +*/ + +#include <linux/mm.h> +#include <linux/mem_encrypt.h> +#include <linux/dma-mapping.h> + +/* Override for DMA direct allocation check - ARCH_HAS_FORCE_DMA_UNENCRYPTED */ +bool force_dma_unencrypted (struct device *dev) +{ +/* +* For SEV, all DMA must be to unencrypted/shared addresses. +*/ +if (sev_active ()) +return true; + +/* +* For SME, all DMA must be to unencrypted addresses if the +* device does not support DMA to addresses that include the +* encryption mask. +*/ +if (sme_active ()) { +u64 dma_enc_mask =DMA_BIT_MASK (__ffs64 (sme_me_mask)); +u64 dma_dev_mask =min_not_zero (dev->coherent_dma_mask, + dev->bus_dma_limit); + +if (dma_dev_mask <= dma_enc_mask) +return true; + } + +return false; +}
Signature · 1 line
2.25.1
Re: [RFC v2 26/32] x86/mm: Move force_dma_unencrypted() to common code
Quoted 1 line
+++
b/arch/x86/mm/mem_encrypt_common.c
...
Quoted 15 lines
arch/x86/mm/mem_encrypt_common.c new file +15
-0+/* Override for DMA direct allocation check - ARCH_HAS_FORCE_DMA_UNENCRYPTED */ +bool force_dma_unencrypted (struct device *dev) +{ +/* +* For SEV, all DMA must be to unencrypted/shared addresses. +*/ +if (sev_active ()) +return true; + +/* +* For SME, all DMA must be to unencrypted addresses if the +* device does not support DMA to addresses that include the +* encryption mask. +*/ +if (sme_active ()) {
arch/x86/mm/mem_encrypt_common.c line 38
Quoted 10 lines
arch/x86/mm/mem_encrypt_common.c new file +10
-0+u64 dma_enc_mask =DMA_BIT_MASK (__ffs64 (sme_me_mask)); +u64 dma_dev_mask =min_not_zero (dev->coherent_dma_mask, + dev->bus_dma_limit); + +if (dma_dev_mask <= dma_enc_mask) +return true; + } + +return false; +}
This doesn't seem much like common code to me. It seems like 100% SEV code. Is this really where we want to move it?
Re: [RFC v2 26/32] x86/mm: Move force_dma_unencrypted() to common code
Quoted 2 lines · Dave Hansen
This doesn't seem much like common code to me. It seems like 100% SEV code. Is this really where we want to move it?
Both SEV and TDX code has requirement to enable CONFIG_ARCH_HAS_FORCE_DMA_UNENCRYPTED and define force_dma_unencrypted() function.
force_dma_unencrypted() is modified by patch titled "x86/tdx: Make DMA pages shared" to add TDX guest specific support.
Since both SEV and TDX code uses it, its moved to common file.
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Re: [RFC v2 26/32] x86/mm: Move force_dma_unencrypted() to common code
Quoted 4 lines · Sathyanarayanan
Quoted 2 lines · Dave Hansen
This doesn't seem much like common code to me. It seems like 100% SEV code. Is this really where we want to move it?
Quoted 8 lines
Both SEV and TDX code has requirement to enable
CONFIG_ARCH_HAS_FORCE_DMA_UNENCRYPTEDand defineforce_dma_unencrypted()function.
force_dma_unencrypted()is modified by patch titled "x86/tdx: Make DMA pages shared" to add TDX guest specific support.Since both SEV and TDX code uses it, its moved to common file.
That's not an excuse to have a bunch of AMD (or Intel) feature-specific code in a file named "common". I'd make an attempt to keep them separate and then call into the two separate functions *from* the common function.
Re: [RFC v2 26/32] x86/mm: Move force_dma_unencrypted() to common code
Quoted 12 lines · Dave Hansen
Quoted 11 lines · Sathyanarayanan
Quoted 2 lines · Dave Hansen
This doesn't seem much like common code to me. It seems like 100% SEV code. Is this really where we want to move it?
Both SEV and TDX code has requirement to enable
CONFIG_ARCH_HAS_FORCE_DMA_UNENCRYPTEDand defineforce_dma_unencrypted()function.
force_dma_unencrypted()is modified by patch titled "x86/tdx: Make DMA pages shared" to add TDX guest specific support.
Quoted 6 lines
Quoted 1 line
Since both SEV and TDX code uses it, its moved to common file.
That's not an excuse to have a bunch of AMD (or Intel) feature-specific code in a file named "common". I'd make an attempt to keep them separate and then call into the two separate functions *from* the common function.
But why? What good does the additional level of inderection brings?
It's like saying arch/x86/kernel/cpu/common.c shouldn't have anything AMD or Intel specific. If a function can cover both vendors I don't see a point for additinal complexity.
Signature · 1 line
Kirill A. Shutemov
Re: [RFC v2 26/32] x86/mm: Move force_dma_unencrypted() to common code
Quoted 9 lines · Kirill A. Shutemov
Quoted 4 lines
That's not an excuse to have a bunch of AMD (or Intel) feature-specific code in a file named "common". I'd make an attempt to keep them separate and then call into the two separate functions *from* the common function.
But why? What good does the additional level of inderection brings?
It's like saying
arch/x86/kernel/cpu/common.cshouldn't have anything AMD or Intel specific. If a function can cover both vendors I don't see a point for additinal complexity.
Because the code is already separate. You're actually going to some trouble to move the SEV-specific code and then combine it with the TDX-specific code.
Anyway, please just give it a shot. Should take all of ten minutes. If it doesn't work out in practice, fine. You'll have a good paragraph for the changelog.
Sean Christopherson
maintainerRe: [RFC v2 26/32] x86/mm: Move force_dma_unencrypted() to common code
Quoted 11 lines · Dave Hansen
Quoted 9 lines · Kirill A. Shutemov
Quoted 4 lines
That's not an excuse to have a bunch of AMD (or Intel) feature-specific code in a file named "common". I'd make an attempt to keep them separate and then call into the two separate functions *from* the common function.
But why? What good does the additional level of inderection brings?
It's like saying
arch/x86/kernel/cpu/common.cshouldn't have anything AMD or Intel specific. If a function can cover both vendors I don't see a point for additinal complexity.
Quoted 7 lines
Because the code is already separate. You're actually going to some trouble to move the SEV-specific code and then combine it with the TDX-specific code.
Anyway, please just give it a shot. Should take all of ten minutes. If it doesn't work out in practice, fine. You'll have a good paragraph for the changelog.
Or maybe wait to see how Boris' propose protected_guest_has() pans out? E.g. if we can do "protected_guest_has(MEMORY_ENCRYPTION)" or whatever, then the truly common bits could be placed into common.c without any vendor-specific logic.
Re: [RFC v2 26/32] x86/mm: Move force_dma_unencrypted() to common code
Quoted 16 lines · Sean Christopherson
Quoted 15 lines · Dave Hansen
Quoted 9 lines · Kirill A. Shutemov
Quoted 4 lines
That's not an excuse to have a bunch of AMD (or Intel) feature-specific code in a file named "common". I'd make an attempt to keep them separate and then call into the two separate functions *from* the common function.
But why? What good does the additional level of inderection brings?
It's like saying
arch/x86/kernel/cpu/common.cshouldn't have anything AMD or Intel specific. If a function can cover both vendors I don't see a point for additinal complexity.Because the code is already separate. You're actually going to some trouble to move the SEV-specific code and then combine it with the TDX-specific code.
Quoted 7 lines
Quoted 3 lines
Anyway, please just give it a shot. Should take all of ten minutes. If it doesn't work out in practice, fine. You'll have a good paragraph for the changelog.
Or maybe wait to see how Boris' propose
protected_guest_has()pans out? E.g. if we can do "protected_guest_has(MEMORY_ENCRYPTION)" or whatever, then the truly common bits could be placed intocommon.cwithout any vendor-specific logic.
How about following abstraction? This patch was initially created to enable us use is_tdx_guest() outside of arch/x86 code. But extended it to support bitmap flags.
commit 188bdd3c97e49020b2bda9efd992a22091423b85
Author: Kuppuswamy Sathyanarayanan <[email protected]> Date: Wed May 12 11:35:13 2021 -0700
tdx: Introduce generic protected_guest abstraction
Add a generic way to check if we run with an encrypted guest, without requiring x86 specific ifdefs. This can then be used in non architecture specific code. Enablethis when running under TDX/SEV.
Also add helper functions to set/test encrypted guest feature flags.
arch/Kconfig +3 -0
@@ -956,6 +956,9 @@ config HAVE_ARCH_NVRAM_OPS config ISA_BUS_API def_bool ISA +config ARCH_HAS_PROTECTED_GUEST + bool + # # ABI hall of shame #
arch/x86/Kconfig +2 -0
@@ -882,6 +882,7 @@ config INTEL_TDX_GUEST select PARAVIRT_XL select X86_X2APIC select SECURITY_LOCKDOWN_LSM + select ARCH_HAS_PROTECTED_GUEST help Provide support for running in a trusted domain on Intel processors equipped with Trusted Domain eXtenstions. TDX is a new Intel
@@ -1537,6 +1538,7 @@ config AMD_MEM_ENCRYPT select ARCH_USE_MEMREMAP_PROT select ARCH_HAS_FORCE_DMA_UNENCRYPTED select INSTRUCTION_DECODER + select ARCH_HAS_PROTECTED_GUEST help Say yes to enable support for the encryption of system memory. This requires an AMD processor that supports Secure Memory
arch/x86/kernel/setup.c +5 -0
@@ -21,6 +21,7 @@#include <linux/usb/xhci-dbgp.h> #include <linux/static_call.h> #include <linux/swiotlb.h> +#include <linux/protected_guest.h> #include <uapi/linux/mount.h>
@@ -107,6 +108,10 @@ static struct resource bss_resource = { .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM }; +#ifdef CONFIG_ARCH_HAS_PROTECTED_GUEST +DECLARE_BITMAP (protected_guest_flags,PROTECTED_GUEST_BITMAP_LEN ); +EXPORT_SYMBOL (protected_guest_flags); +#endif #ifdef CONFIG_X86_32 /* CPU data as detected by the assembly code in head_32.S */
arch/x86/kernel/sev-es.c +4 -0
@@ -19,6 +19,7 @@#include <linux/memblock.h> #include <linux/kernel.h> #include <linux/mm.h> +#include <linux/protected_guest.h> #include <asm/cpu_entry_area.h> #include <asm/stacktrace.h>
@@ -680,6 +681,9 @@ static void __init init_ghcb(int cpu) data->ghcb_active = false; data->backup_ghcb_active = false; + +set_protected_guest_flag (GUEST_TYPE_SEV ); +set_protected_guest_flag (MEMORY_ENCRYPTION ); }void __init sev_es_init_vc_handling (void )
arch/x86/kernel/tdx.c +4 -0
@@ -7,6 +7,7 @@#include <asm/vmx.h> #include <linux/cpu.h> +#include <linux/protected_guest.h> static struct { unsigned int gpa_width;
@@ -92,6 +93,9 @@ void __init tdx_early_init(void)setup_force_cpu_cap (X86_FEATURE_TDX_GUEST ); +set_protected_guest_flag (GUEST_TYPE_TDX ); +set_protected_guest_flag (MEMORY_ENCRYPTION ); +tdg_get_info ();pr_info ("TDX guest is initialized\n" );
include/linux/protected_guest.h new file +37 -0
@@ -0,0 +1,37 @@ +#ifndef _LINUX_PROTECTED_GUEST_H +#define _LINUX_PROTECTED_GUEST_H 1 + +#define PROTECTED_GUEST_BITMAP_LEN 128 + +/* Protected Guest vendor types */ +#define GUEST_TYPE_TDX (1) +#define GUEST_TYPE_SEV (2) + +/* Protected Guest features */ +#define MEMORY_ENCRYPTION (20) + +#ifdef CONFIG_ARCH_HAS_PROTECTED_GUEST +extern DECLARE_BITMAP(protected_guest_flags, PROTECTED_GUEST_BITMAP_LEN); + +static bool protected_guest_has (unsigned long flag) +{ +return test_bit (flag, protected_guest_flags); +} + +static inline void set_protected_guest_flag (unsigned long flag) +{ +__set_bit (flag, protected_guest_flags); +} + +static inline bool is_protected_guest (void ) +{ +return (protected_guest_has (GUEST_TYPE_TDX ) | +protected_guest_has (GUEST_TYPE_SEV ) ); +} +#else +static inline bool protected_guest_has (unsigned long flag) {return false; } +static inline void set_protected_guest_flag (unsigned long flag) { } +static inline bool is_protected_guest (void ) {return false; } +#endif + +#endif
Quoted 1 line
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Re: [RFC v2 26/32] x86/mm: Move force_dma_unencrypted() to common code
include/linux/protected_guest.h line 11
Quoted 9 lines · Sathyanarayanan
include/linux/protected_guest.h new file +8
-0+#define PROTECTED_GUEST_BITMAP_LEN 128 + +/* Protected Guest vendor types */ +#define GUEST_TYPE_TDX (1) +#define GUEST_TYPE_SEV (2) + +/* Protected Guest features */ +#define MEMORY_ENCRYPTION (20)
I was assuming we'd reuse the X86_FEATURE infrastructure somehow. Is there a good reason not to?
That gives us all the compile-time optimization (via en/disabled-features.h) and static branches for "free".
Re: [RFC v2 26/32] x86/mm: Move force_dma_unencrypted() to common code
Quoted 13 lines · Dave Hansen
include/linux/protected_guest.h line 11
Quoted 9 lines · Sathyanarayanan
include/linux/protected_guest.h new file +8
-0+#define PROTECTED_GUEST_BITMAP_LEN 128 + +/* Protected Guest vendor types */ +#define GUEST_TYPE_TDX (1) +#define GUEST_TYPE_SEV (2) + +/* Protected Guest features */ +#define MEMORY_ENCRYPTION (20)I was assuming we'd reuse the
X86_FEATUREinfrastructure somehow. Is there a good reason not to?
My assumption is, protected guest abstraction can be also used by non-x86 arch's in future. So I have tried to keep these definitions in common code.
Quoted 4 lines
That gives us all the compile-time optimization (via
en/disabled-features.h) and static branches for "free".
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Re: [RFC v2 26/32] x86/mm: Move force_dma_unencrypted() to common code
In reply to Dave HansenQuoted 11 lines · Dave Hansen
include/linux/protected_guest.h line 11
Quoted 8 lines · Sathyanarayanan
include/linux/protected_guest.h new file +8
-0+#define PROTECTED_GUEST_BITMAP_LEN 128 + +/* Protected Guest vendor types */ +#define GUEST_TYPE_TDX (1) +#define GUEST_TYPE_SEV (2) + +/* Protected Guest features */ +#define MEMORY_ENCRYPTION (20)I was assuming we'd reuse the
X86_FEATUREinfrastructure somehow. Is there a good reason not to?
This for generic code. Would be a gigantic lift and lots of refactoring to move that out.
Quoted 3 lines
That gives us all the compile-time optimization (via
en/disabled-features.h) and static branches for "free".
There's no user so far which is anywhere near performance critical, so that would be total overkil
BTW right now I'm not even sure we need the bitmap for anything, but I guess it doesn't hurt.
-Andi
Re: [RFC v2 26/32] x86/mm: Move force_dma_unencrypted() to common code
Quoted 14 lines · Andi Kleen
Quoted 11 lines · Dave Hansen
include/linux/protected_guest.h line 11
Quoted 8 lines · Sathyanarayanan
include/linux/protected_guest.h new file +8
-0+#define PROTECTED_GUEST_BITMAP_LEN 128 + +/* Protected Guest vendor types */ +#define GUEST_TYPE_TDX (1) +#define GUEST_TYPE_SEV (2) + +/* Protected Guest features */ +#define MEMORY_ENCRYPTION (20)I was assuming we'd reuse the
X86_FEATUREinfrastructure somehow. Is there a good reason not to?
Quoted 2 lines
This for generic code. Would be a gigantic lift and lots of refactoring to move that out.
Ahh, forgot about that. The whole "x86/mm" subject threw me off.
Quoted 5 lines
Quoted 2 lines
That gives us all the compile-time optimization (via
en/disabled-features.h) and static branches for "free".There's no user so far which is anywhere near performance critical, so that would be total overkil
The *REALLY* nice thing is that it keeps you from having to create stub functions or #ifdefs and yet the compiler can still optimize the code to nothing.
Anyway, thanks for the clarification about it being in non-arch code.
Sean Christopherson
maintainerRe: [RFC v2 26/32] x86/mm: Move force_dma_unencrypted() to common code
In reply to Andi KleenQuoted 14 lines · Andi Kleen
Quoted 11 lines · Dave Hansen
include/linux/protected_guest.h line 11
Quoted 8 lines · Sathyanarayanan
include/linux/protected_guest.h new file +8
-0+#define PROTECTED_GUEST_BITMAP_LEN 128 + +/* Protected Guest vendor types */ +#define GUEST_TYPE_TDX (1) +#define GUEST_TYPE_SEV (2) + +/* Protected Guest features */ +#define MEMORY_ENCRYPTION (20)I was assuming we'd reuse the
X86_FEATUREinfrastructure somehow. Is there a good reason not to?
Quoted 2 lines
This for generic code. Would be a gigantic lift and lots of refactoring to move that out.
What generic code needs access to SEV vs. TDX? force_dma_unencrypted() is called from generic code, but its implementation is x86 specific.
Quoted 5 lines
Quoted 2 lines
That gives us all the compile-time optimization (via
en/disabled-features.h) and static branches for "free".There's no user so far which is anywhere near performance critical, so that would be total overkil
SEV already has the sev_enable_key static key that it uses for unrolling string I/O, so there's at least one (debatable) case that wants to use static branches.
For SEV-ES and TDX, there's a better argument as using X86_FEATURE_* would unlock alternatives.
Quoted 6 lines
BTW right now I'm not even sure we need the bitmap for anything, but I guess it doesn't hurt.
-Andi
Re: [RFC v2 26/32] x86/mm: Move force_dma_unencrypted() to common code
Quoted 2 lines · Sean Christopherson
What generic code needs access to SEV vs. TDX?
force_dma_unencrypted()is called from generic code, but its implementation is x86 specific.
When the hardening the drivers for TDX usage, we will have requirement to check for is_protected_guest() to add code specific to protected guests. Since this will be outside arch/x86, we need common framework for it.
Few examples are,
* ACPI sleep driver uses WBINVD (when doing cache flushes). We want to skip it for TDX.
* Forcing virtio to use dma API when running with untrusted host.
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Re: [RFC v2 26/32] x86/mm: Move force_dma_unencrypted() to common code
Quoted 9 lines · Sathyanarayanan
Quoted 3 lines · Sean Christopherson
What generic code needs access to SEV vs. TDX?
force_dma_unencrypted()is called from generic code, but its implementation is x86 specific.When the hardening the drivers for TDX usage, we will have requirement to check for
is_protected_guest()to add code specific to protected guests. Since this will be outsidearch/x86, we need common framework for it.
Just remember, a "common framework" doesn't mean that it can't be backed by extremely arch-specific mechanisms.
For instance, there's a lot of pkey-specific code in mm/mprotect.c. It still gets optimized away on x86 with all the goodness of X86_FEATUREs.
Sean Christopherson
maintainerRe: [RFC v2 26/32] x86/mm: Move force_dma_unencrypted() to common code
Quoted 11 lines · Dave Hansen
Quoted 9 lines · Sathyanarayanan
Quoted 3 lines · Sean Christopherson
What generic code needs access to SEV vs. TDX?
force_dma_unencrypted()is called from generic code, but its implementation is x86 specific.When the hardening the drivers for TDX usage, we will have requirement to check for
is_protected_guest()to add code specific to protected guests. Since this will be outsidearch/x86, we need common framework for it.
Quoted 5 lines
Just remember, a "common framework" doesn't mean that it can't be backed by extremely arch-specific mechanisms.
For instance, there's a lot of pkey-specific code in
mm/mprotect.c. It still gets optimized away on x86 with all the goodness ofX86_FEATUREs.
Ya, exactly. Ideally, generic code shouldn't have to differentiate between SEV, SEV-ES, SEV-SNP, TDX, etc..., a vanilla "bool is_protected_guest(void)" should suffice. Under the hood, x86's implementation for is_protected_guest() can be boot_cpu_has() checks (if we want).
Re: [RFC v2 26/32] x86/mm: Move force_dma_unencrypted() to common code
Quoted 9 lines · Sean Christopherson
Quoted 5 lines
Just remember, a "common framework" doesn't mean that it can't be backed by extremely arch-specific mechanisms.
For instance, there's a lot of pkey-specific code in
mm/mprotect.c. It still gets optimized away on x86 with all the goodness ofX86_FEATUREs.Ya, exactly. Ideally, generic code shouldn't have to differentiate between SEV, SEV-ES, SEV-SNP, TDX, etc..., a vanilla "bool
is_protected_guest(void)" should suffice. Under the hood, x86's implementation foris_protected_guest()can beboot_cpu_has()checks (if we want).
What about the use case of protected_guest_has(flag)? Do you want to call it with with X86_FEATURE_* flags outside arch/x86 code ?
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Re: [RFC v2 26/32] x86/mm: Move force_dma_unencrypted() to common code
Quoted 8 lines · Sathyanarayanan
Quoted 5 lines · Sean Christopherson
Quoted 5 lines
Just remember, a "common framework" doesn't mean that it can't be backed by extremely arch-specific mechanisms.
For instance, there's a lot of pkey-specific code in mm/mprotect.c. It still gets optimized away on x86 with all the goodness of
X86_FEATUREs.
Quoted 7 lines
Quoted 4 lines
Ya, exactly. Ideally, generic code shouldn't have to differentiate between SEV, SEV-ES, SEV-SNP, TDX, etc..., a vanilla "bool
is_protected_guest(void)" should suffice. Under the hood, x86's implementation foris_protected_guest()can beboot_cpu_has()checks (if we want).What about the use case of
protected_guest_has(flag)? Do you want to call it with with X86_FEATURE_* flags outsidearch/x86code ?
I don't think we need any flags in the generic code. Just a simple bool is enough.
-Andi