Add TDX Guest Support
v2 cover Add TDX Guest Support
Hi All,
NOTE: This series is not ready for wide public review. It is being specifically posted so that Peter Z and other experts on the entry code can look for problems with the new exception handler (#VE). That's also why x86@ is not being spammed.
Intel's Trust Domain Extensions (TDX) protect guest VMs from malicious hosts and some physical attacks. This series adds the bare-minimum support to run a TDX guest. The host-side support will be submitted separately. Also support for advanced TD guest features like attestation or debug-mode will be submitted separately. Also, at this point it is not secure with some known holes in drivers, and also hasn’t been fully audited and fuzzed yet.
TDX has a lot of similarities to SEV. It enhances confidentiality and of guest memory and state (like registers) and includes a new exception (#VE) for the same basic reasons as SEV-ES. Like SEV-SNP (not merged yet), TDX limits the host's ability to effect changes in the guest physical address space.
In contrast to the SEV code in the kernel, TDX guest memory is integrity protected and isolated; the host is prevented from accessing guest memory (even ciphertext).
The TDX architecture also includes a new CPU mode called Secure-Arbitration Mode (SEAM). The software (TDX module) running in this mode arbitrates interactions between host and guest and implements many of the guarantees of the TDX architecture.
Some of the key differences between TD and regular VM is,
1. Multi CPU bring-up is done using the ACPI MADT wake-up table.
2. A new #VE exception handler is added. The TDX module injects #VE exception to the guest TD in cases of instructions that need to be emulated, disallowed MSR accesses, subset of CPUID leaves, etc.
3. By default memory is marked as private, and TD will selectively share it with VMM based on need.
4. Remote attestation is supported to enable a third party (either the owner of the workload or a user of the services provided by the workload) to establish that the workload is running on an Intel-TDX-enabled platform located within a TD prior to providing that workload data.
You can find TDX related documents in the following link.
https://software.intel.com/content/www/br/pt/develop/articles/intel-trust-domain-extensions.html
Changes since v1:
* Implemented tdcall() and tdvmcall() helper functions in assembly and renamed them as __tdcall() and __tdvmcall().
* Added do_general_protection() helper function to re-use protection code between #GP exception and TDX #VE exception handlers.
* Addressed syscall gap issue in #VE handler support (for details check the commit log in "x86/traps: Add #VE support for TDX guest").
* Modified patch titled "x86/tdx: Handle port I/O" to re-use common tdvmcall() helper function.
* Added error handling support to MADT CPU wakeup code.
* Introduced enum tdx_map_type to identify SHARED vs PRIVATE memory type.
* Enabled shared memory in IOAPIC driver.
* Added BINUTILS version info for TDCALL.
* Changed the TDVMCALL vendor id from 0 to "TDX.KVM".
* Replaced WARN() with pr_warn_ratelimited() in __tdvmcall() wrappers.
* Fixed commit log and code comments related review comments.
* Renamed patch titled # "x86/topology: Disable CPU hotplug support for TDX platforms" to "x86/topology: Disable CPU online/offline control for TDX guest"
* Rebased on top of v5.12 kernel.
Erik Kaneda (1):
ACPICA: ACPI 6.4: MADT: add Multiprocessor Wakeup Structure
Isaku Yamahata (1):
x86/tdx: ioapic: Add shared bit for IOAPIC base address
Kirill A. Shutemov (16):
x86/paravirt: Introduce CONFIG_PARAVIRT_XL x86/tdx: Get TD execution environment information via TDINFO x86/traps: Add #VE support for TDX guest
x86/tdx: Add HLT support for TDX guest
x86/tdx: Wire up KVM hypercalls
x86/tdx: Add MSR support for TDX guest
x86/tdx: Handle CPUID via #VE
x86/io: Allow to override inX() and outX() implementation x86/tdx: Handle port I/O
x86/tdx: Handle in-kernel MMIO
x86/mm: Move force_dma_unencrypted() to common code x86/tdx: Exclude Shared bit from __PHYSICAL_MASK x86/tdx: Make pages shared in ioremap()
x86/tdx: Add helper to do MapGPA TDVMALL
x86/tdx: Make DMA pages shared
x86/kvm: Use bounce buffers for TD guest
Kuppuswamy Sathyanarayanan (10):
x86/tdx: Introduce INTEL_TDX_GUEST config option x86/cpufeatures: Add TDX Guest CPU feature
x86/x86: Add is_tdx_guest() interface
x86/tdx: Add __tdcall() and __tdvmcall() helper functions x86/traps: Add do_general_protection() helper function x86/tdx: Handle MWAIT, MONITOR and WBINVD
ACPICA: ACPI 6.4: MADT: add Multiprocessor Wakeup Mailbox Structure ACPI/table: Print MADT Wake table information
x86/acpi, x86/boot: Add multiprocessor wake-up support x86/topology: Disable CPU online/offline control for TDX guest
Sean Christopherson (4):
x86/boot: Add a trampoline for APs booting in 64-bit mode x86/boot: Avoid #VE during compressed boot for TDX platforms x86/boot: Avoid unnecessary #VE during boot process x86/tdx: Forcefully disable legacy PIC for TDX guests
arch/x86/Kconfig | 28 +- arch/x86/boot/compressed/Makefile | 2 + arch/x86/boot/compressed/head_64.S | 10 +- arch/x86/boot/compressed/misc.h | 1 + arch/x86/boot/compressed/pgtable.h | 2 +- arch/x86/boot/compressed/tdcall.S | 9 + arch/x86/boot/compressed/tdx.c | 32 ++ arch/x86/include/asm/apic.h | 3 + arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/idtentry.h | 4 + arch/x86/include/asm/io.h | 24 +- arch/x86/include/asm/irqflags.h | 38 +- arch/x86/include/asm/kvm_para.h | 21 + arch/x86/include/asm/paravirt.h | 22 +- arch/x86/include/asm/paravirt_types.h | 3 +- arch/x86/include/asm/pgtable.h | 3 + arch/x86/include/asm/realmode.h | 1 + arch/x86/include/asm/tdx.h | 176 +++++++++ arch/x86/kernel/Makefile | 1 + arch/x86/kernel/acpi/boot.c | 79 ++++ arch/x86/kernel/apic/apic.c | 8 + arch/x86/kernel/apic/io_apic.c | 12 +- arch/x86/kernel/asm-offsets.c | 22 ++ arch/x86/kernel/head64.c | 3 + arch/x86/kernel/head_64.S | 13 +- arch/x86/kernel/idt.c | 6 + arch/x86/kernel/paravirt.c | 4 +- arch/x86/kernel/pci-swiotlb.c | 2 +- arch/x86/kernel/smpboot.c | 5 + arch/x86/kernel/tdcall.S | 361 +++++++++++++++++ arch/x86/kernel/tdx-kvm.c | 45 +++ arch/x86/kernel/tdx.c | 480 +++++++++++++++++++++++ arch/x86/kernel/topology.c | 3 +- arch/x86/kernel/traps.c | 81 ++-- arch/x86/mm/Makefile | 2 + arch/x86/mm/ioremap.c | 8 +- arch/x86/mm/mem_encrypt.c | 75 ---- arch/x86/mm/mem_encrypt_common.c | 85 ++++ arch/x86/mm/mem_encrypt_identity.c | 1 + arch/x86/mm/pat/set_memory.c | 48 ++- arch/x86/realmode/rm/header.S | 1 + arch/x86/realmode/rm/trampoline_64.S | 49 ++- arch/x86/realmode/rm/trampoline_common.S | 5 +- drivers/acpi/tables.c | 11 + include/acpi/actbl2.h | 26 +- 45 files changed, 1654 insertions(+), 162 deletions(-) create mode 100644 arch/x86/boot/compressed/tdcall.S create mode 100644 arch/x86/boot/compressed/tdx.c create mode 100644 arch/x86/include/asm/tdx.h create mode 100644 arch/x86/kernel/tdcall.S create mode 100644 arch/x86/kernel/tdx-kvm.c create mode 100644 arch/x86/kernel/tdx.c create mode 100644 arch/x86/mm/mem_encrypt_common.c
Signature · 1 line
2.25.1
RFC v2 1/32 x86/paravirt: Introduce CONFIG_PARAVIRT_XL
From: "Kirill A. Shutemov" <[email protected]>
Split off halt paravirt calls from CONFIG_PARAVIRT_XXL into a separate config option. It provides a middle ground for not-so-deep paravirtulized environments.
CONFIG_PARAVIRT_XL will be used by TDX that needs couple of paravirt calls that were hidden under CONFIG_PARAVIRT_XXL, but the rest of the config would be a bloat for TDX.
--- arch/x86/Kconfig | 4 +++ arch/x86/boot/compressed/misc.h | 1 + arch/x86/include/asm/irqflags.h | 38 +++++++++++++++------------ arch/x86/include/asm/paravirt.h | 22 +++++++++------- arch/x86/include/asm/paravirt_types.h | 3 ++- arch/x86/kernel/paravirt.c | 4 ++- arch/x86/mm/mem_encrypt_identity.c | 1 + 7 files changed, 44 insertions(+), 29 deletions(-)
arch/x86/Kconfig +4 -0
@@ -783,8 +783,12 @@ config PARAVIRT over full virtualization. However, when run without a hypervisor the kernel is theoretically slower and slightly larger. +config PARAVIRT_XL + bool + config PARAVIRT_XXL bool + select PARAVIRT_XL config PARAVIRT_DEBUG bool "paravirt-ops debugging"
arch/x86/boot/compressed/misc.h +1 -0
@@ -9,6 +9,7 @@* paravirt and debugging variants are added.) */ #undef CONFIG_PARAVIRT +#undef CONFIG_PARAVIRT_XL#undef CONFIG_PARAVIRT_XXL#undef CONFIG_PARAVIRT_SPINLOCKS#undef CONFIG_KASAN
arch/x86/include/asm/irqflags.h +21 -17
@@ -59,27 +59,11 @@ static inline __cpuidle void native_halt(void)#endif -#ifdef CONFIG_PARAVIRT_XXL +#ifdef CONFIG_PARAVIRT_XL #include <asm/paravirt.h> #else #ifndef __ASSEMBLY__#include <linux/types.h> - -static __always_inline unsigned long arch_local_save_flags (void ) -{ -return native_save_fl (); -} - -static __always_inline void arch_local_irq_disable(void) -{ -native_irq_disable (); -} - -static __always_inline void arch_local_irq_enable(void) -{ -native_irq_enable (); -} -/* * Used in the idle loop; sti takes one instruction cycle * to complete:
@@ -97,6 +81,26 @@ static inline __cpuidle void halt(void) {native_halt (); } +#endif /* !__ASSEMBLY__ */ +#endif /* CONFIG_PARAVIRT_XL */ + +#ifndef CONFIG_PARAVIRT_XXL +#ifndef __ASSEMBLY__ + +static __always_inline unsigned long arch_local_save_flags (void ) +{ +return native_save_fl (); +} + +static __always_inline void arch_local_irq_disable(void) +{ +native_irq_disable (); +} + +static __always_inline void arch_local_irq_enable(void) +{ +native_irq_enable (); +}/* * For spinlocks, etc:
arch/x86/include/asm/paravirt.h +12 -10
@@ -84,6 +84,18 @@ static inline void paravirt_arch_exit_mmap(struct mm_struct *mm)PVOP_VCALL1 (mmu.exit_mmap, mm); } +#ifdef CONFIG_PARAVIRT_XL +static inline void arch_safe_halt (void ) +{ +PVOP_VCALL0 (irq.safe_halt); +} + +static inline void halt (void ) +{ +PVOP_VCALL0 (irq.halt); +} +#endif +#ifdef CONFIG_PARAVIRT_XXL static inline void load_sp0 (unsigned long sp0) {
@@ -145,16 +157,6 @@ static inline void __write_cr4(unsigned long x)PVOP_VCALL1 (cpu.write_cr4, x); } -static inline void arch_safe_halt (void ) -{ -PVOP_VCALL0 (irq.safe_halt); -} - -static inline void halt (void ) -{ -PVOP_VCALL0 (irq.halt); -} -static inline void wbinvd (void ) {PVOP_VCALL0 (cpu.wbinvd);
arch/x86/include/asm/paravirt_types.h +2 -1
@@ -177,7 +177,8 @@ struct pv_irq_ops {struct paravirt_callee_save save_fl;struct paravirt_callee_save irq_disable;struct paravirt_callee_save irq_enable; - +#endif +#ifdef CONFIG_PARAVIRT_XL void (*safe_halt)(void );void (*halt)(void );#endif
arch/x86/kernel/paravirt.c +3 -1
@@ -322,9 +322,11 @@ struct paravirt_patch_template pv_ops = { .irq.save_fl = __PV_IS_CALLEE_SAVE(native_save_fl), .irq.irq_disable = __PV_IS_CALLEE_SAVE(native_irq_disable), .irq.irq_enable = __PV_IS_CALLEE_SAVE(native_irq_enable), +#endif /* CONFIG_PARAVIRT_XXL */ +#ifdef CONFIG_PARAVIRT_XL .irq.safe_halt = native_safe_halt, .irq.halt = native_halt, -#endif /* CONFIG_PARAVIRT_XXL */ +#endif /* CONFIG_PARAVIRT_XL */ /* Mmu ops. */ .mmu.flush_tlb_user = native_flush_tlb_local,
arch/x86/mm/mem_encrypt_identity.c +1 -0
@@ -24,6 +24,7 @@* be extended when new paravirt and debugging variants are added.) */ #undef CONFIG_PARAVIRT +#undef CONFIG_PARAVIRT_XL#undef CONFIG_PARAVIRT_XXL#undef CONFIG_PARAVIRT_SPINLOCKS
Signature · 1 line
2.25.1
Borislav Petkov
maintainerRe: [RFC v2 01/32] x86/paravirt: Introduce CONFIG_PARAVIRT_XL
+ Jürgen.
Quoted 5 lines · Kuppuswamy Sathyanarayanan
From: "Kirill A. Shutemov" <[email protected]>
Split off halt paravirt calls from
CONFIG_PARAVIRT_XXLinto a separate config option. It provides a middle ground for not-so-deep paravirtulized environments.
Please introduce a spellchecker into your patch creation workflow.
Also, what does "not-so-deep" mean?
Quoted 3 lines
CONFIG_PARAVIRT_XLwill be used by TDX that needs couple of paravirt calls that were hidden underCONFIG_PARAVIRT_XXL, but the rest of the config would be a bloat for TDX.
Used how? Why is it bloat for TDX?
I'm sure that'll become clear in the remainder of the patches but you should state it here so that it is clear why you're doing what you're doing.
Quoted 37 lines
Signed-off-by Kirill A. Shutemov <[email protected]>Reviewed-by Andi Kleen <[email protected]>Reviewed-by Tony Luck <[email protected]>Signed-off-by Kuppuswamy Sathyanarayanan <[email protected]>--- arch/x86/Kconfig | 4 +++ arch/x86/boot/compressed/misc.h | 1 + arch/x86/include/asm/irqflags.h | 38 +++++++++++++++------------ arch/x86/include/asm/paravirt.h | 22 +++++++++------- arch/x86/include/asm/paravirt_types.h | 3 ++- arch/x86/kernel/paravirt.c | 4 ++- arch/x86/mm/mem_encrypt_identity.c | 1 + 7 files changed, 44 insertions(+), 29 deletions(-)arch/x86/Kconfig +4
-0@@ -783,8 +783,12 @@ config PARAVIRT over full virtualization. However, when run without a hypervisor the kernel is theoretically slower and slightly larger. +config PARAVIRT_XL + bool + config PARAVIRT_XXL bool + select PARAVIRT_XL config PARAVIRT_DEBUG bool "paravirt-ops debugging"arch/x86/boot/compressed/misc.h +0
-0
arch/x86/boot/compressed/misc.h line 13
Quoted 5 lines
arch/x86/boot/compressed/misc.h +1
-0@@ -9,6 +9,7 @@* paravirt and debugging variants are added.) */ #undef CONFIG_PARAVIRT +#undef CONFIG_PARAVIRT_XL#undef CONFIG_PARAVIRT_XXL
So what happens if someone else needs even less pv and defines CONFIG_PARAVIRT_L. Or _M? Or _S?
Are we going to teleport into a clothing store each time we look at paravirt now? :)
So before this goes out of hand let's define explicitly, pls, what XXL means and XL. And rename them. They could be called PARAVIRT_FULL and PARAVIRT_HLT as apparently that thing is exposing only the PV ops related to HLT.
Or something to that effect.
Dunno, maybe Jürgen has a better idea, leaving in the rest quoted for him.
Thx.
Quoted 148 lines
arch/x86/boot/compressed/misc.h +0
-0#undef CONFIG_PARAVIRT_SPINLOCKS#undef CONFIG_KASANarch/x86/include/asm/irqflags.h +21
-17@@ -59,27 +59,11 @@ static inline __cpuidle void native_halt(void)#endif -#ifdef CONFIG_PARAVIRT_XXL +#ifdef CONFIG_PARAVIRT_XL #include <asm/paravirt.h> #else #ifndef __ASSEMBLY__#include <linux/types.h> - -static __always_inline unsigned long arch_local_save_flags (void ) -{ -return native_save_fl (); -} - -static __always_inline void arch_local_irq_disable(void) -{ -native_irq_disable (); -} - -static __always_inline void arch_local_irq_enable(void) -{ -native_irq_enable (); -} -/* * Used in the idle loop; sti takes one instruction cycle * to complete: @@ -97,6 +81,26 @@ static inline __cpuidle void halt(void) {native_halt (); } +#endif /* !__ASSEMBLY__ */ +#endif /* CONFIG_PARAVIRT_XL */ + +#ifndef CONFIG_PARAVIRT_XXL +#ifndef __ASSEMBLY__ + +static __always_inline unsigned long arch_local_save_flags (void ) +{ +return native_save_fl (); +} + +static __always_inline void arch_local_irq_disable(void) +{ +native_irq_disable (); +} + +static __always_inline void arch_local_irq_enable(void) +{ +native_irq_enable (); +}/* * For spinlocks, etc: arch/x86/include/asm/paravirt.h +12
-10@@ -84,6 +84,18 @@ static inline void paravirt_arch_exit_mmap(struct mm_struct *mm)PVOP_VCALL1 (mmu.exit_mmap, mm); } +#ifdef CONFIG_PARAVIRT_XL +static inline void arch_safe_halt (void ) +{ +PVOP_VCALL0 (irq.safe_halt); +} + +static inline void halt (void ) +{ +PVOP_VCALL0 (irq.halt); +} +#endif +#ifdef CONFIG_PARAVIRT_XXL static inline void load_sp0 (unsigned long sp0) {@@ -145,16 +157,6 @@ static inline void __write_cr4(unsigned long x)PVOP_VCALL1 (cpu.write_cr4, x); } -static inline void arch_safe_halt (void ) -{ -PVOP_VCALL0 (irq.safe_halt); -} - -static inline void halt (void ) -{ -PVOP_VCALL0 (irq.halt); -} -static inline void wbinvd (void ) {PVOP_VCALL0 (cpu.wbinvd);arch/x86/include/asm/paravirt_types.h +2
-1@@ -177,7 +177,8 @@ struct pv_irq_ops {struct paravirt_callee_save save_fl;struct paravirt_callee_save irq_disable;struct paravirt_callee_save irq_enable; - +#endif +#ifdef CONFIG_PARAVIRT_XL void (*safe_halt)(void );void (*halt)(void );#endif arch/x86/kernel/paravirt.c +3
-1@@ -322,9 +322,11 @@ struct paravirt_patch_template pv_ops = { .irq.save_fl = __PV_IS_CALLEE_SAVE(native_save_fl), .irq.irq_disable = __PV_IS_CALLEE_SAVE(native_irq_disable), .irq.irq_enable = __PV_IS_CALLEE_SAVE(native_irq_enable), +#endif /* CONFIG_PARAVIRT_XXL */ +#ifdef CONFIG_PARAVIRT_XL .irq.safe_halt = native_safe_halt, .irq.halt = native_halt, -#endif /* CONFIG_PARAVIRT_XXL */ +#endif /* CONFIG_PARAVIRT_XL */ /* Mmu ops. */ .mmu.flush_tlb_user = native_flush_tlb_local,arch/x86/mm/mem_encrypt_identity.c +1
-0@@ -24,6 +24,7 @@* be extended when new paravirt and debugging variants are added.) */ #undef CONFIG_PARAVIRT +#undef CONFIG_PARAVIRT_XL#undef CONFIG_PARAVIRT_XXL#undef CONFIG_PARAVIRT_SPINLOCKSSignature · 1 line
2.25.1
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [RFC v2 01/32] x86/paravirt: Introduce CONFIG_PARAVIRT_XL
Quoted 1 line · Borislav Petkov
Or something to that effect.
See the couple of attached patches. Does look along the lines you wanted?
The first one renames PARAVIRT_XXL and the second one introduces PARAVIRT_HLT.
Signature · 1 line
Kirill A. Shutemov
From 2671a044687da0e0c7105beb3467a270b8863a1b Mon Sep 17 00:00:00 2001
From: "Kirill A. Shutemov" <[email protected]>
Date: Thu, 6 May 2021 17:04:42 +0300
Subject: [PATCH 1/2] x86/paravirt: Rename PARAVIRT_XXL to PARAVIRT_FULL
PARAVIRT_XXL provides a way to hook up a full set paravirt ops. Rename it to PARAVIRT_FULL to be more self-descriptive.
It's a preparation for the next patch.
--- arch/x86/Kconfig | 2 +- arch/x86/boot/compressed/misc.h | 2 +- arch/x86/entry/vdso/vdso32/vclock_gettime.c | 2 +- arch/x86/include/asm/debugreg.h | 2 +- arch/x86/include/asm/desc.h | 4 ++-- arch/x86/include/asm/fixmap.h | 4 ++-- arch/x86/include/asm/io_bitmap.h | 2 +- arch/x86/include/asm/irqflags.h | 4 ++-- arch/x86/include/asm/mmu_context.h | 4 ++-- arch/x86/include/asm/msr.h | 4 ++-- arch/x86/include/asm/paravirt.h | 12 ++++++------ arch/x86/include/asm/paravirt_types.h | 10 +++++----- arch/x86/include/asm/pgalloc.h | 2 +- arch/x86/include/asm/pgtable.h | 6 +++--- arch/x86/include/asm/processor.h | 4 ++-- arch/x86/include/asm/ptrace.h | 2 +- arch/x86/include/asm/required-features.h | 2 +- arch/x86/include/asm/special_insns.h | 4 ++-- arch/x86/kernel/asm-offsets.c | 2 +- arch/x86/kernel/asm-offsets_64.c | 2 +- arch/x86/kernel/paravirt.c | 18 +++++++++--------- arch/x86/kernel/paravirt_patch.c | 6 +++--- arch/x86/mm/mem_encrypt_identity.c | 2 +- arch/x86/xen/Kconfig | 2 +- tools/arch/x86/include/asm/required-features.h | 2 +- 25 files changed, 53 insertions(+), 53 deletions(-)
arch/x86/Kconfig +1 -1
@@ -783,7 +783,7 @@ config PARAVIRT over full virtualization. However, when run without a hypervisor the kernel is theoretically slower and slightly larger. -config PARAVIRT_XXL +config PARAVIRT_FULL bool config PARAVIRT_DEBUG
arch/x86/boot/compressed/misc.h +1 -1
@@ -9,7 +9,7 @@* paravirt and debugging variants are added.) */ #undef CONFIG_PARAVIRT -#undef CONFIG_PARAVIRT_XXL +#undef CONFIG_PARAVIRT_FULL#undef CONFIG_PARAVIRT_SPINLOCKS#undef CONFIG_KASAN#undef CONFIG_KASAN_GENERIC
arch/x86/entry/vdso/vdso32/vclock_gettime.c +1 -1
@@ -14,7 +14,7 @@#undef CONFIG_ILLEGAL_POINTER_VALUE#undef CONFIG_SPARSEMEM_VMEMMAP#undef CONFIG_NR_CPUS -#undef CONFIG_PARAVIRT_XXL +#undef CONFIG_PARAVIRT_FULL#define CONFIG_X86_32 1#define CONFIG_PGTABLE_LEVELS 2
arch/x86/include/asm/debugreg.h +1 -1
@@ -8,7 +8,7 @@ DECLARE_PER_CPU(unsigned long, cpu_dr7); -#ifndef CONFIG_PARAVIRT_XXL +#ifndef CONFIG_PARAVIRT_FULL /* * These special macros can be used to get or set a debugging register */
arch/x86/include/asm/desc.h +2 -2
@@ -103,7 +103,7 @@ static inline int desc_empty(const void *ptr)return !(desc[0 ] | desc[1 ]); } -#ifdef CONFIG_PARAVIRT_XXL +#ifdef CONFIG_PARAVIRT_FULL #include <asm/paravirt.h> #else #define load_TR_desc () native_load_tr_desc()
@@ -129,7 +129,7 @@ static inline void paravirt_alloc_ldt(struct desc_struct *ldt, unsigned entries)static inline void paravirt_free_ldt (struct desc_struct *ldt,unsigned entries) { } -#endif /* CONFIG_PARAVIRT_XXL */ +#endif /* CONFIG_PARAVIRT_FULL */ #define store_ldt (ldt) asm("sldt %0" : "=m"(ldt))
arch/x86/include/asm/fixmap.h +2 -2
@@ -105,7 +105,7 @@ enum fixed_addresses { FIX_PCIE_MCFG,#endif #endif -#ifdef CONFIG_PARAVIRT_XXL +#ifdef CONFIG_PARAVIRT_FULL FIX_PARAVIRT_BOOTMAP,#endif
@@ -160,7 +160,7 @@ void __native_set_fixmap(enum fixed_addresses idx, pte_t pte);void native_set_fixmap (unsigned /* enum fixed_addresses */ idx,phys_addr_t phys,pgprot_t flags); -#ifndef CONFIG_PARAVIRT_XXL +#ifndef CONFIG_PARAVIRT_FULL static inline void __set_fixmap (enum fixed_addresses idx,phys_addr_t phys,pgprot_t flags) {
arch/x86/include/asm/io_bitmap.h +1 -1
@@ -36,7 +36,7 @@ static inline void native_tss_invalidate_io_bitmap(void)void native_tss_update_io_bitmap (void ); -#ifdef CONFIG_PARAVIRT_XXL +#ifdef CONFIG_PARAVIRT_FULL #include <asm/paravirt.h> #else #define tss_update_io_bitmap native_tss_update_io_bitmap
arch/x86/include/asm/irqflags.h +2 -2
@@ -59,7 +59,7 @@ static inline __cpuidle void native_halt(void)#endif -#ifdef CONFIG_PARAVIRT_XXL +#ifdef CONFIG_PARAVIRT_FULL #include <asm/paravirt.h> #else #ifndef __ASSEMBLY__
@@ -124,7 +124,7 @@ static __always_inline unsigned long arch_local_irq_save(void)#endif #endif /* __ASSEMBLY__ */ -#endif /* CONFIG_PARAVIRT_XXL */ +#endif /* CONFIG_PARAVIRT_FULL */ #ifndef __ASSEMBLY__ static __always_inline int arch_irqs_disabled_flags(unsigned long flags)
arch/x86/include/asm/mmu_context.h +2 -2
@@ -15,12 +15,12 @@extern atomic64_t last_mm_ctx_id; -#ifndef CONFIG_PARAVIRT_XXL +#ifndef CONFIG_PARAVIRT_FULL static inline void paravirt_activate_mm (struct mm_struct *prev,struct mm_struct *next) { } -#endif /* !CONFIG_PARAVIRT_XXL */ +#endif /* !CONFIG_PARAVIRT_FULL */ #ifdef CONFIG_PERF_EVENTS DECLARE_STATIC_KEY_FALSE (rdpmc_never_available_key);
arch/x86/include/asm/msr.h +2 -2
@@ -251,7 +251,7 @@ static inline unsigned long long native_read_pmc(int counter)return EAX_EDX_VAL (val, low, high); } -#ifdef CONFIG_PARAVIRT_XXL +#ifdef CONFIG_PARAVIRT_FULL #include <asm/paravirt.h> #else #include <linux/errno.h>
@@ -314,7 +314,7 @@ do { \#define rdpmcl (counter, val) ((val) = native_read_pmc(counter)) -#endif /* !CONFIG_PARAVIRT_XXL */ +#endif /* !CONFIG_PARAVIRT_FULL */ /* * 64-bit version of wrmsr_safe():
arch/x86/include/asm/paravirt.h +6 -6
@@ -84,7 +84,7 @@ static inline void paravirt_arch_exit_mmap(struct mm_struct *mm)PVOP_VCALL1 (mmu.exit_mmap, mm); } -#ifdef CONFIG_PARAVIRT_XXL +#ifdef CONFIG_PARAVIRT_FULL static inline void load_sp0 (unsigned long sp0) {PVOP_VCALL1 (cpu.load_sp0, sp0);
@@ -642,7 +642,7 @@ bool __raw_callee_save___native_vcpu_is_preempted(long cpu);#define __PV_IS_CALLEE_SAVE (func) \ ((struct paravirt_callee_save) { func }) -#ifdef CONFIG_PARAVIRT_XXL +#ifdef CONFIG_PARAVIRT_FULL static inline notrace unsigned long arch_local_save_flags (void ) { return PVOP_CALLEE0(unsigned long, irq.save_fl);
@@ -748,7 +748,7 @@ extern void default_banner(void);#define PARA_INDIRECT (addr) *%cs:addr#endif -#ifdef CONFIG_PARAVIRT_XXL +#ifdef CONFIG_PARAVIRT_FULL #define INTERRUPT_RETURN \ PARA_SITE(PARA_PATCH(PV_CPU_iret), \ ANNOTATE_RETPOLINE_SAFE; \
@@ -770,7 +770,7 @@ extern void default_banner(void);#endif #ifdef CONFIG_X86_64 -#ifdef CONFIG_PARAVIRT_XXL +#ifdef CONFIG_PARAVIRT_FULL #ifdef CONFIG_DEBUG_ENTRY #define SAVE_FLAGS (clobbers) \ PARA_SITE(PARA_PATCH(PV_IRQ_save_fl), \
@@ -779,7 +779,7 @@ extern void default_banner(void); call PARA_INDIRECT(pv_ops+PV_IRQ_save_fl); \ PV_RESTORE_REGS(clobbers | CLBR_CALLEE_SAVE);)#endif -#endif /* CONFIG_PARAVIRT_XXL */ +#endif /* CONFIG_PARAVIRT_FULL */ #endif /* CONFIG_X86_64 */ #endif /* __ASSEMBLY__ */
@@ -788,7 +788,7 @@ extern void default_banner(void);#endif /* !CONFIG_PARAVIRT */ #ifndef __ASSEMBLY__ -#ifndef CONFIG_PARAVIRT_XXL +#ifndef CONFIG_PARAVIRT_FULL static inline void paravirt_arch_dup_mmap(struct mm_struct *oldmm, struct mm_struct *mm) {
arch/x86/include/asm/paravirt_types.h +5 -5
@@ -66,7 +66,7 @@ struct paravirt_callee_save {/* general info */ struct pv_info { -#ifdef CONFIG_PARAVIRT_XXL +#ifdef CONFIG_PARAVIRT_FULL u16 extra_user_64bit_cs;/* __USER_CS if none */ #endif
@@ -86,7 +86,7 @@ struct pv_init_ops { unsigned long addr, unsigned len); } __no_randomize_layout; -#ifdef CONFIG_PARAVIRT_XXL +#ifdef CONFIG_PARAVIRT_FULL struct pv_lazy_ops { /* Set deferred update mode, used for batching operations. */ void (*enter)(void);
@@ -104,7 +104,7 @@ struct pv_cpu_ops {/* hooks for various privileged instructions */ void (*io_delay)(void ); -#ifdef CONFIG_PARAVIRT_XXL +#ifdef CONFIG_PARAVIRT_FULL unsigned long (*get_debugreg)(int regno);void (*set_debugreg)(int regno,unsigned long value);
@@ -166,7 +166,7 @@ struct pv_cpu_ops { } __no_randomize_layout;struct pv_irq_ops { -#ifdef CONFIG_PARAVIRT_XXL +#ifdef CONFIG_PARAVIRT_FULL /* * Get/set interrupt state. save_fl is expected to use X86_EFLAGS_IF; * all other bits returned from save_fl are undefined.
@@ -196,7 +196,7 @@ struct pv_mmu_ops {/* Hook for intercepting the destruction of an mm_struct. */ void (*exit_mmap)(struct mm_struct *mm); -#ifdef CONFIG_PARAVIRT_XXL +#ifdef CONFIG_PARAVIRT_FULL struct paravirt_callee_save read_cr2;void (*write_cr2)(unsigned long );
arch/x86/include/asm/pgalloc.h +1 -1
@@ -12,7 +12,7 @@static inline int __paravirt_pgd_alloc (struct mm_struct *mm) {return 0 ; } -#ifdef CONFIG_PARAVIRT_XXL +#ifdef CONFIG_PARAVIRT_FULL #include <asm/paravirt.h> #else #define paravirt_pgd_alloc (mm) __paravirt_pgd_alloc(mm)
arch/x86/include/asm/pgtable.h +3 -3
@@ -59,9 +59,9 @@ extern struct mm_struct *pgd_page_get_mm(struct page *page);extern pmdval_t early_pmd_flags; -#ifdef CONFIG_PARAVIRT_XXL +#ifdef CONFIG_PARAVIRT_FULL #include <asm/paravirt.h> -#else /* !CONFIG_PARAVIRT_XXL */ +#else /* !CONFIG_PARAVIRT_FULL */ #define set_pte (ptep, pte) native_set_pte(ptep, pte)#define set_pte_atomic (ptep, pte) \
@@ -115,7 +115,7 @@ extern pmdval_t early_pmd_flags;#define __pte (x) native_make_pte(x)#define arch_end_context_switch (prev) do {} while(0) -#endif /* CONFIG_PARAVIRT_XXL */ +#endif /* CONFIG_PARAVIRT_FULL */ /* * The following only work if pte_present() is true.
arch/x86/include/asm/processor.h +2 -2
@@ -580,7 +580,7 @@ static inline bool on_thread_stack(void) current_stack_pointer) < THREAD_SIZE; } -#ifdef CONFIG_PARAVIRT_XXL +#ifdef CONFIG_PARAVIRT_FULL #include <asm/paravirt.h> #else #define __cpuid native_cpuid
@@ -590,7 +590,7 @@ static inline void load_sp0(unsigned long sp0) native_load_sp0(sp0); } -#endif /* CONFIG_PARAVIRT_XXL */ +#endif /* CONFIG_PARAVIRT_FULL */ /* Free all resources held by a thread. */ extern void release_thread (struct task_struct *);
arch/x86/include/asm/ptrace.h +1 -1
@@ -146,7 +146,7 @@ static inline int v8086_mode(struct pt_regs *regs)static inline bool user_64bit_mode (struct pt_regs *regs) {#ifdef CONFIG_X86_64 -#ifndef CONFIG_PARAVIRT_XXL +#ifndef CONFIG_PARAVIRT_FULL /* * On non-paravirt systems, this is the only long mode CPL 3 * selector. We do not allow long mode selectors in the LDT.
arch/x86/include/asm/required-features.h +1 -1
@@ -54,7 +54,7 @@#endif #ifdef CONFIG_X86_64 -#ifdef CONFIG_PARAVIRT_XXL +#ifdef CONFIG_PARAVIRT_FULL /* Paravirtualized systems may not have PSE or PGE available */ #define NEED_PSE 0#define NEED_PGE 0
arch/x86/include/asm/special_insns.h +2 -2
@@ -148,7 +148,7 @@ static inline unsigned long __read_cr4(void)return native_read_cr4 (); } -#ifdef CONFIG_PARAVIRT_XXL +#ifdef CONFIG_PARAVIRT_FULL #include <asm/paravirt.h> #else
@@ -205,7 +205,7 @@ static inline void load_gs_index(unsigned int selector)#endif -#endif /* CONFIG_PARAVIRT_XXL */ +#endif /* CONFIG_PARAVIRT_FULL */ static inline void clflush (volatile void *__p) {
arch/x86/kernel/asm-offsets.c +1 -1
@@ -61,7 +61,7 @@ static void __used common(void)OFFSET (IA32_RT_SIGFRAME_sigcontext, rt_sigframe_ia32, uc.uc_mcontext);#endif -#ifdef CONFIG_PARAVIRT_XXL +#ifdef CONFIG_PARAVIRT_FULL BLANK ();OFFSET (PV_IRQ_irq_disable, paravirt_patch_template, irq.irq_disable);OFFSET (PV_IRQ_irq_enable, paravirt_patch_template, irq.irq_enable);
arch/x86/kernel/asm-offsets_64.c +1 -1
@@ -12,7 +12,7 @@int main (void ) {#ifdef CONFIG_PARAVIRT -#ifdef CONFIG_PARAVIRT_XXL +#ifdef CONFIG_PARAVIRT_FULL #ifdef CONFIG_DEBUG_ENTRY OFFSET (PV_IRQ_save_fl, paravirt_patch_template, irq.save_fl);#endif
arch/x86/kernel/paravirt.c +9 -9
@@ -79,7 +79,7 @@ static unsigned paravirt_patch_call(void *insn_buff, const void *target,return call_len; } -#ifdef CONFIG_PARAVIRT_XXL +#ifdef CONFIG_PARAVIRT_FULL /* identity function, which can be inlined */ u64 notrace _paravirt_ident_64(u64 x) {
@@ -130,7 +130,7 @@ unsigned paravirt_patch_default(u8 type, void *insn_buff,else if (opfunc == _paravirt_nop) ret =0 ; -#ifdef CONFIG_PARAVIRT_XXL +#ifdef CONFIG_PARAVIRT_FULL /* identity functions just return their single argument */ else if (opfunc == _paravirt_ident_64) ret =paravirt_patch_ident_64 (insn_buff, len);
@@ -227,7 +227,7 @@ void paravirt_flush_lazy_mmu(void)preempt_enable (); } -#ifdef CONFIG_PARAVIRT_XXL +#ifdef CONFIG_PARAVIRT_FULL void paravirt_start_context_switch (struct task_struct *prev) {BUG_ON (preemptible ());
@@ -260,7 +260,7 @@ enum paravirt_lazy_mode paravirt_get_lazy_mode(void)struct pv_info pv_info = { .name ="bare hardware" , -#ifdef CONFIG_PARAVIRT_XXL +#ifdef CONFIG_PARAVIRT_FULL .extra_user_64bit_cs = __USER_CS, #endif };
@@ -279,7 +279,7 @@ struct paravirt_patch_template pv_ops = {/* Cpu ops. */ .cpu.io_delay = native_io_delay, -#ifdef CONFIG_PARAVIRT_XXL +#ifdef CONFIG_PARAVIRT_FULL .cpu.cpuid = native_cpuid, .cpu.get_debugreg = native_get_debugreg, .cpu.set_debugreg = native_set_debugreg,
@@ -324,7 +324,7 @@ struct paravirt_patch_template pv_ops = { .irq.irq_enable = __PV_IS_CALLEE_SAVE(native_irq_enable), .irq.safe_halt = native_safe_halt, .irq.halt = native_halt, -#endif /* CONFIG_PARAVIRT_XXL */ +#endif /* CONFIG_PARAVIRT_FULL */ /* Mmu ops. */ .mmu.flush_tlb_user = native_flush_tlb_local,
@@ -336,7 +336,7 @@ struct paravirt_patch_template pv_ops = { .mmu.exit_mmap = paravirt_nop, -#ifdef CONFIG_PARAVIRT_XXL +#ifdef CONFIG_PARAVIRT_FULL .mmu.read_cr2 =__PV_IS_CALLEE_SAVE (native_read_cr2), .mmu.write_cr2 = native_write_cr2, .mmu.read_cr3 = __native_read_cr3,
@@ -393,7 +393,7 @@ struct paravirt_patch_template pv_ops = { }, .mmu.set_fixmap = native_set_fixmap, -#endif /* CONFIG_PARAVIRT_XXL */ +#endif /* CONFIG_PARAVIRT_FULL */ #if defined(CONFIG_PARAVIRT_SPINLOCKS )/* Lock ops. */
@@ -409,7 +409,7 @@ struct paravirt_patch_template pv_ops = {#endif }; -#ifdef CONFIG_PARAVIRT_XXL +#ifdef CONFIG_PARAVIRT_FULL /* At this point, native_get/set_debugreg has real function entries */ NOKPROBE_SYMBOL (native_get_debugreg);NOKPROBE_SYMBOL (native_set_debugreg);
arch/x86/kernel/paravirt_patch.c +3 -3
@@ -17,7 +17,7 @@case PARAVIRT_PATCH (ops.m): \ return PATCH(data, ops##_##m, insn_buff, len) -#ifdef CONFIG_PARAVIRT_XXL +#ifdef CONFIG_PARAVIRT_FULL struct patch_xxl { const unsigned char irq_irq_disable[1]; const unsigned char irq_irq_enable[1];
@@ -44,7 +44,7 @@ unsigned int paravirt_patch_ident_64(void *insn_buff, unsigned int len) {return PATCH (xxl, mov64, insn_buff, len); } -# endif /* CONFIG_PARAVIRT_XXL */ +# endif /* CONFIG_PARAVIRT_FULL */ #ifdef CONFIG_PARAVIRT_SPINLOCKS struct patch_lock {
@@ -68,7 +68,7 @@ unsigned int native_patch(u8 type, void *insn_buff, unsigned long addr, {switch (type) { -#ifdef CONFIG_PARAVIRT_XXL +#ifdef CONFIG_PARAVIRT_FULL PATCH_CASE (irq, save_fl, xxl, insn_buff, len);PATCH_CASE (irq, irq_enable, xxl, insn_buff, len);PATCH_CASE (irq, irq_disable, xxl, insn_buff, len);
arch/x86/mm/mem_encrypt_identity.c +1 -1
@@ -24,7 +24,7 @@* be extended when new paravirt and debugging variants are added.) */ #undef CONFIG_PARAVIRT -#undef CONFIG_PARAVIRT_XXL +#undef CONFIG_PARAVIRT_FULL#undef CONFIG_PARAVIRT_SPINLOCKS#include <linux/kernel.h>
arch/x86/xen/Kconfig +1 -1
@@ -20,7 +20,7 @@ config XEN_PV default y depends on XEN depends on X86_64 - select PARAVIRT_XXL + select PARAVIRT_FULL select XEN_HAVE_PVMMU select XEN_HAVE_VPMU help
tools/arch/x86/include/asm/required-features.h +1 -1
@@ -54,7 +54,7 @@#endif #ifdef CONFIG_X86_64 -#ifdef CONFIG_PARAVIRT_XXL +#ifdef CONFIG_PARAVIRT_FULL /* Paravirtualized systems may not have PSE or PGE available */ #define NEED_PSE 0#define NEED_PGE 0
Signature · 1 line
2.26.3
From 08e03cebb65b48e6b7e5bf0d805762cc661d09f0 Mon Sep 17 00:00:00 2001
From: "Kirill A. Shutemov" <[email protected]>
Date: Tue, 19 Nov 2019 14:56:03 +0300
Subject: [PATCH 2/2] x86/paravirt: Introduce CONFIG_PARAVIRT_HLT
CONFIG_PARAVIRT_FULL provides a way to hook up the full set of paravirt ops, but TDX only needs two of them: halt() and safe_halt().
Split off halt paravirt calls from CONFIG_PARAVIRT_FULL into a separate config option -- CONFIG_PARAVIRT_HLT.
--- arch/x86/Kconfig | 4 +++ arch/x86/boot/compressed/misc.h | 1 + arch/x86/include/asm/irqflags.h | 38 +++++++++++++++------------ arch/x86/include/asm/paravirt.h | 22 +++++++++------- arch/x86/include/asm/paravirt_types.h | 3 ++- arch/x86/kernel/paravirt.c | 4 ++- arch/x86/mm/mem_encrypt_identity.c | 1 + 7 files changed, 44 insertions(+), 29 deletions(-)
arch/x86/Kconfig +4 -0
@@ -783,8 +783,12 @@ config PARAVIRT over full virtualization. However, when run without a hypervisor the kernel is theoretically slower and slightly larger. +config PARAVIRT_HLT + bool + config PARAVIRT_FULL bool + select PARAVIRT_HLT config PARAVIRT_DEBUG bool "paravirt-ops debugging"
arch/x86/boot/compressed/misc.h +1 -0
@@ -9,6 +9,7 @@* paravirt and debugging variants are added.) */ #undef CONFIG_PARAVIRT +#undef CONFIG_PARAVIRT_HLT#undef CONFIG_PARAVIRT_FULL#undef CONFIG_PARAVIRT_SPINLOCKS#undef CONFIG_KASAN
arch/x86/include/asm/irqflags.h +21 -17
@@ -59,27 +59,11 @@ static inline __cpuidle void native_halt(void)#endif -#ifdef CONFIG_PARAVIRT_FULL +#ifdef CONFIG_PARAVIRT_HLT #include <asm/paravirt.h> #else #ifndef __ASSEMBLY__#include <linux/types.h> - -static __always_inline unsigned long arch_local_save_flags (void ) -{ -return native_save_fl (); -} - -static __always_inline void arch_local_irq_disable(void) -{ -native_irq_disable (); -} - -static __always_inline void arch_local_irq_enable(void) -{ -native_irq_enable (); -} -/* * Used in the idle loop; sti takes one instruction cycle * to complete:
@@ -97,6 +81,26 @@ static inline __cpuidle void halt(void) {native_halt (); } +#endif /* !__ASSEMBLY__ */ +#endif /* CONFIG_PARAVIRT_HLT */ + +#ifndef CONFIG_PARAVIRT_FULL +#ifndef __ASSEMBLY__ + +static __always_inline unsigned long arch_local_save_flags (void ) +{ +return native_save_fl (); +} + +static __always_inline void arch_local_irq_disable(void) +{ +native_irq_disable (); +} + +static __always_inline void arch_local_irq_enable(void) +{ +native_irq_enable (); +}/* * For spinlocks, etc:
arch/x86/include/asm/paravirt.h +12 -10
@@ -84,6 +84,18 @@ static inline void paravirt_arch_exit_mmap(struct mm_struct *mm)PVOP_VCALL1 (mmu.exit_mmap, mm); } +#ifdef CONFIG_PARAVIRT_HLT +static inline void arch_safe_halt (void ) +{ +PVOP_VCALL0 (irq.safe_halt); +} + +static inline void halt (void ) +{ +PVOP_VCALL0 (irq.halt); +} +#endif +#ifdef CONFIG_PARAVIRT_FULL static inline void load_sp0 (unsigned long sp0) {
@@ -145,16 +157,6 @@ static inline void __write_cr4(unsigned long x)PVOP_VCALL1 (cpu.write_cr4, x); } -static inline void arch_safe_halt (void ) -{ -PVOP_VCALL0 (irq.safe_halt); -} - -static inline void halt (void ) -{ -PVOP_VCALL0 (irq.halt); -} -static inline void wbinvd (void ) {PVOP_VCALL0 (cpu.wbinvd);
arch/x86/include/asm/paravirt_types.h +2 -1
@@ -177,7 +177,8 @@ struct pv_irq_ops {struct paravirt_callee_save save_fl;struct paravirt_callee_save irq_disable;struct paravirt_callee_save irq_enable; - +#endif +#ifdef CONFIG_PARAVIRT_HLT void (*safe_halt)(void );void (*halt)(void );#endif
arch/x86/kernel/paravirt.c +3 -1
@@ -322,9 +322,11 @@ struct paravirt_patch_template pv_ops = { .irq.save_fl = __PV_IS_CALLEE_SAVE(native_save_fl), .irq.irq_disable = __PV_IS_CALLEE_SAVE(native_irq_disable), .irq.irq_enable = __PV_IS_CALLEE_SAVE(native_irq_enable), +#endif /* CONFIG_PARAVIRT_FULL */ +#ifdef CONFIG_PARAVIRT_HLT .irq.safe_halt = native_safe_halt, .irq.halt = native_halt, -#endif /* CONFIG_PARAVIRT_FULL */ +#endif /* CONFIG_PARAVIRT_HLT */ /* Mmu ops. */ .mmu.flush_tlb_user = native_flush_tlb_local,
arch/x86/mm/mem_encrypt_identity.c +1 -0
@@ -24,6 +24,7 @@* be extended when new paravirt and debugging variants are added.) */ #undef CONFIG_PARAVIRT +#undef CONFIG_PARAVIRT_HLT#undef CONFIG_PARAVIRT_FULL#undef CONFIG_PARAVIRT_SPINLOCKS
Signature · 1 line
2.26.3
Juergen Gross
maintainerRe: [RFC v2 01/32] x86/paravirt: Introduce CONFIG_PARAVIRT_XL
In reply to Borislav PetkovQuoted 11 lines · Borislav Petkov
+ Jürgen.
Quoted 5 lines · Kuppuswamy Sathyanarayanan
From: "Kirill A. Shutemov" <[email protected]>
Split off halt paravirt calls from
CONFIG_PARAVIRT_XXLinto a separate config option. It provides a middle ground for not-so-deep paravirtulized environments.Please introduce a spellchecker into your patch creation workflow.
Quoted 7 lines
Also, what does "not-so-deep" mean?
Quoted 3 lines
CONFIG_PARAVIRT_XLwill be used by TDX that needs couple of paravirt calls that were hidden underCONFIG_PARAVIRT_XXL, but the rest of the config would be a bloat for TDX.Used how? Why is it bloat for TDX?
Is there any major downside to move the halt related pvops functions from CONFIG_PARAVIRT_XXL to CONFIG_PARAVIRT?
I'd rather introduce a new PARAVIRT level only in case of multiple pvops functions needed for a new guest type, or if a real hot path would be affected.
Juergen
Re: [RFC v2 01/32] x86/paravirt: Introduce CONFIG_PARAVIRT_XL
\
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
CONFIG_PARAVIRT_XLwill be used by TDX that needs couple of paravirt calls that were hidden underCONFIG_PARAVIRT_XXL, but the rest of the config would be a bloat for TDX.Used how? Why is it bloat for TDX?
Is there any major downside to move the halt related pvops functions from
CONFIG_PARAVIRT_XXLtoCONFIG_PARAVIRT?
I think the main motivation is to get rid of all the page table related hooks for modern configurations. These are the bulk of the annotations and cause bloat and worse code. Shadow page tables are really obscure these days and very few people still need them and it's totally reasonable to build even widely used distribution kernels without them. On contrast most of the other hooks are comparatively few and also on comparatively slow paths, so don't really matter too much.
I think it would be ok to have a CONFIG_PARAVIRT that does not have page table support, and a separate config option for those (that could be eventually deprecated).
But that would break existing .configs for those shadow stack users, that's why I think Kirill did it the other way around.
-Andi
Juergen Gross
maintainerRe: [RFC v2 01/32] x86/paravirt: Introduce CONFIG_PARAVIRT_XL
Quoted 7 lines · Andi Kleen
\
Quoted 6 lines
Quoted 5 lines
Quoted 3 lines
CONFIG_PARAVIRT_XLwill be used by TDX that needs couple of paravirt calls that were hidden underCONFIG_PARAVIRT_XXL, but the rest of the config would be a bloat for TDX.Used how? Why is it bloat for TDX?
Quoted 8 lines
Quoted 2 lines
Is there any major downside to move the halt related pvops functions from
CONFIG_PARAVIRT_XXLtoCONFIG_PARAVIRT?I think the main motivation is to get rid of all the page table related hooks for modern configurations. These are the bulk of the annotations and cause bloat and worse code. Shadow page tables are really obscure these days and very few people still need them and it's totally reasonable to build even widely used distribution kernels without them. On contrast most of the other hooks are comparatively few and also on comparatively slow paths, so don't really matter too much.
I think it would be ok to have a
CONFIG_PARAVIRTthat does not have page table support, and a separate config option for those (that could be eventually deprecated).But that would break existing .configs for those shadow stack users, that's why I think Kirill did it the other way around.
No. We have PARAVIRT_XXL for Xen PV guests, and we have PARAVIRT for other hypervisor's guests, supporting basically the TLB flush operations and time related operations only. Adding the halt related operations to PARAVIRT wouldn't break anything.
Juergen
Re: [RFC v2 01/32] x86/paravirt: Introduce CONFIG_PARAVIRT_XL
Quoted 23 lines · Juergen Gross
Quoted 22 lines · Andi Kleen
\
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
CONFIG_PARAVIRT_XLwill be used by TDX that needs couple of paravirt calls that were hidden underCONFIG_PARAVIRT_XXL, but the rest of the config would be a bloat for TDX.Used how? Why is it bloat for TDX?
Is there any major downside to move the halt related pvops functions from
CONFIG_PARAVIRT_XXLtoCONFIG_PARAVIRT?I think the main motivation is to get rid of all the page table related hooks for modern configurations. These are the bulk of the annotations and cause bloat and worse code. Shadow page tables are really obscure these days and very few people still need them and it's totally reasonable to build even widely used distribution kernels without them. On contrast most of the other hooks are comparatively few and also on comparatively slow paths, so don't really matter too much.
I think it would be ok to have a
CONFIG_PARAVIRTthat does not have page table support, and a separate config option for those (that could be eventually deprecated).
Quoted 7 lines
Quoted 2 lines
But that would break existing .configs for those shadow stack users, that's why I think Kirill did it the other way around.
No. We have
PARAVIRT_XXLfor Xen PV guests, and we have PARAVIRT for other hypervisor's guests, supporting basically the TLB flush operations and time related operations only. Adding the halt related operations to PARAVIRT wouldn't break anything.
Yeah, I think we can do this. It should be fine.
Signature · 1 line
Kirill A. Shutemov
Peter Zijlstra
maintainerRe: [RFC v2 01/32] x86/paravirt: Introduce CONFIG_PARAVIRT_XL
In reply to Juergen GrossQuoted 4 lines · Juergen Gross
No. We have
PARAVIRT_XXLfor Xen PV guests, and we have PARAVIRT for other hypervisor's guests, supporting basically the TLB flush operations and time related operations only. Adding the halt related operations to PARAVIRT wouldn't break anything.
Also, I don't think anything modern should actually ever hit any of the HLT instructions, most everything should end up at an MWAIT.
Still, do we wants to give arch_safe_halt() and halt() the PVOP_ALT_VCALL0() treatment?
Re: [RFC v2 01/32] x86/paravirt: Introduce CONFIG_PARAVIRT_XL
Quoted 11 lines · Peter Zijlstra
Quoted 4 lines · Juergen Gross
No. We have
PARAVIRT_XXLfor Xen PV guests, and we have PARAVIRT for other hypervisor's guests, supporting basically the TLB flush operations and time related operations only. Adding the halt related operations to PARAVIRT wouldn't break anything.Also, I don't think anything modern should actually ever hit any of the HLT instructions, most everything should end up at an MWAIT.
Still, do we wants to give
arch_safe_halt()andhalt()thePVOP_ALT_VCALL0()treatment?
From performance reasons it's pointless to patch. HLT (and MWAIT) are so slow anyways that using patching or an indirect pointer is completely in the noise. So I would use whatever is cleanest in the code.
-Andi
Juergen Gross
maintainerRe: [RFC v2 01/32] x86/paravirt: Introduce CONFIG_PARAVIRT_XL
Quoted 8 lines · Andi Kleen
Quoted 6 lines · Peter Zijlstra
Quoted 4 lines · Juergen Gross
No. We have
PARAVIRT_XXLfor Xen PV guests, and we have PARAVIRT for other hypervisor's guests, supporting basically the TLB flush operations and time related operations only. Adding the halt related operations to PARAVIRT wouldn't break anything.
Quoted 8 lines
Quoted 5 lines
Also, I don't think anything modern should actually ever hit any of the HLT instructions, most everything should end up at an MWAIT.
Still, do we wants to give
arch_safe_halt()andhalt()thePVOP_ALT_VCALL0()treatment?From performance reasons it's pointless to patch. HLT (and MWAIT) are
so slow anyways that using patching or an indirect pointer is completely in the noise. So I would use whatever is cleanest in the code.
This would probably be x86_platform_ops.hyper hooks.
Juergen
RFC 1/1 x86/paravirt: Move halt paravirt calls under CONFIG_PARAVIRT
From: "Kirill A. Shutemov" <[email protected]>
CONFIG_PARAVIRT_XXL is mainly defined/used by XEN PV guests. For other VM guest types, features supported under CONFIG_PARAVIRT are self sufficient. CONFIG_PARAVIRT mainly provides support for TLB flush operations and time related operations.
For TDX guest as well, paravirt calls under CONFIG_PARVIRT meets most of its requirement except the need of HLT and SAFE_HLT paravirt calls, which is currently defined under COFNIG_PARAVIRT_XXL.
Since enabling CONFIG_PARAVIRT_XXL is too bloated for TDX guest like platforms, move HLT and SAFE_HLT paravirt calls under CONFIG_PARAVIRT.
Moving HLT and SAFE_HLT paravirt calls are not fatal and should not break any functionality for current users of CONFIG_PARAVIRT.
---
Changes since v1:
* Removed CONFIG_PARAVIRT_XL
* Moved HLT and SAFE_HLT under CONFIG_PARAVIRT
arch/x86/include/asm/irqflags.h | 40 +++++++++++++++------------ arch/x86/include/asm/paravirt.h | 20 +++++++------- arch/x86/include/asm/paravirt_types.h | 3 +- arch/x86/kernel/paravirt.c | 4 ++- 4 files changed, 36 insertions(+), 31 deletions(-)
arch/x86/include/asm/irqflags.h +22 -18
@@ -59,6 +59,28 @@ static inline __cpuidle void native_halt(void)#endif +#ifndef CONFIG_PARAVIRT +#ifndef __ASSEMBLY__ +/* +* Used in the idle loop; sti takes one instruction cycle +* to complete: +*/ +static inline __cpuidle void arch_safe_halt(void) +{ +native_safe_halt (); +} + +/* +* Used when interrupts are already enabled or to +* shutdown the processor: +*/ +static inline __cpuidle void halt(void) +{ +native_halt (); +} +#endif /* __ASSEMBLY__ */ +#endif /* CONFIG_PARAVIRT */ +#ifdef CONFIG_PARAVIRT_XXL #include <asm/paravirt.h> #else
@@ -80,24 +102,6 @@ static __always_inline void arch_local_irq_enable(void)native_irq_enable (); } -/* -* Used in the idle loop; sti takes one instruction cycle -* to complete: -*/ -static inline __cpuidle void arch_safe_halt(void) -{ -native_safe_halt (); -} - -/* -* Used when interrupts are already enabled or to -* shutdown the processor: -*/ -static inline __cpuidle void halt(void) -{ -native_halt (); -} -/* * For spinlocks, etc: */
arch/x86/include/asm/paravirt.h +10 -10
@@ -84,6 +84,16 @@ static inline void paravirt_arch_exit_mmap(struct mm_struct *mm)PVOP_VCALL1 (mmu.exit_mmap, mm); } +static inline void arch_safe_halt (void ) +{ +PVOP_VCALL0 (irq.safe_halt); +} + +static inline void halt (void ) +{ +PVOP_VCALL0 (irq.halt); +} +#ifdef CONFIG_PARAVIRT_XXL static inline void load_sp0 (unsigned long sp0) {
@@ -145,16 +155,6 @@ static inline void __write_cr4(unsigned long x)PVOP_VCALL1 (cpu.write_cr4, x); } -static inline void arch_safe_halt (void ) -{ -PVOP_VCALL0 (irq.safe_halt); -} - -static inline void halt (void ) -{ -PVOP_VCALL0 (irq.halt); -} -static inline void wbinvd (void ) {PVOP_VCALL0 (cpu.wbinvd);
arch/x86/include/asm/paravirt_types.h +1 -2
@@ -177,10 +177,9 @@ struct pv_irq_ops {struct paravirt_callee_save save_fl;struct paravirt_callee_save irq_disable;struct paravirt_callee_save irq_enable; - +#endif void (*safe_halt)(void );void (*halt)(void ); -#endif } __no_randomize_layout;struct pv_mmu_ops {
arch/x86/kernel/paravirt.c +3 -1
@@ -322,9 +322,11 @@ struct paravirt_patch_template pv_ops = { .irq.save_fl = __PV_IS_CALLEE_SAVE(native_save_fl), .irq.irq_disable = __PV_IS_CALLEE_SAVE(native_irq_disable), .irq.irq_enable = __PV_IS_CALLEE_SAVE(native_irq_enable), +#endif /* CONFIG_PARAVIRT_XXL */ + +/* Irq HLT ops. */ .irq.safe_halt = native_safe_halt, .irq.halt = native_halt, -#endif /* CONFIG_PARAVIRT_XXL */ /* Mmu ops. */ .mmu.flush_tlb_user = native_flush_tlb_local,
Signature · 1 line
2.25.1
RFC v2 2/32 x86/tdx: Introduce INTEL_TDX_GUEST config option
In reply to Kuppuswamy SathyanarayananAdd INTEL_TDX_GUEST config option to selectively compile TDX guest support.
--- arch/x86/Kconfig | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
arch/x86/Kconfig +15 -0
@@ -875,6 +875,21 @@ config ACRN_GUEST IOT with small footprint and real-time features. More details can be found in https://projectacrn.org/. +config INTEL_TDX_GUEST + bool "Intel Trusted Domain eXtensions Guest Support" + depends on X86_64 && CPU_SUP_INTEL && PARAVIRT + depends on SECURITY + select PARAVIRT_XL + select X86_X2APIC + select SECURITY_LOCKDOWN_LSM + help + Provide support for running in a trusted domain on Intel processors + equipped with Trusted Domain eXtenstions. TDX is an new Intel + technology that extends VMX and Memory Encryption with a new kind of + virtual machine guest called Trust Domain (TD). A TD is designed to + run in a CPU mode that protects the confidentiality of TD memory + contents and the TD’s CPU state from other software, including VMM. + endif #HYPERVISOR_GUEST source "arch/x86/Kconfig.cpu"
Signature · 1 line
2.25.1
Re: [RFC v2 02/32] x86/tdx: Introduce INTEL_TDX_GUEST config option
Quoted 18 lines · Kuppuswamy Sathyanarayanan
Add
INTEL_TDX_GUESTconfig option to selectively compile TDX guest support.Signed-off-by Kuppuswamy Sathyanarayanan <[email protected]>Reviewed-by Andi Kleen <[email protected]>Reviewed-by Tony Luck <[email protected]>--- arch/x86/Kconfig | 15 +++++++++++++++ 1 file changed, 15 insertions(+)arch/x86/Kconfig +0
-0@@ -875,6 +875,21 @@ config ACRN_GUEST IOT with small footprint and real-time features. More details can be found in https://projectacrn.org/.
arch/x86/Kconfig config ACRN_GUEST · line 887
Quoted 10 lines
arch/x86/Kconfig +10
-0+config INTEL_TDX_GUEST + bool "Intel Trusted Domain eXtensions Guest Support" + depends on X86_64 && CPU_SUP_INTEL && PARAVIRT + depends on SECURITY + select PARAVIRT_XL + select X86_X2APIC + select SECURITY_LOCKDOWN_LSM + help + Provide support for running in a trusted domain on Intel processors + equipped with Trusted Domain eXtenstions. TDX is an new Intel
a new Intel
Quoted 9 lines
arch/x86/Kconfig +5
-0+ technology that extends VMX and Memory Encryption with a new kind of + virtual machine guest called Trust Domain (TD). A TD is designed to + run in a CPU mode that protects the confidentiality of TD memory + contents and the TD’s CPU state from other software, including VMM. + endif #HYPERVISOR_GUEST source "arch/x86/Kconfig.cpu"
Signature · 1 line
~Randy
Re: [RFC v2 02/32] x86/tdx: Introduce INTEL_TDX_GUEST config option
Quoted 19 lines · Randy Dunlap
Quoted 18 lines · Kuppuswamy Sathyanarayanan
Add
INTEL_TDX_GUESTconfig option to selectively compile TDX guest support.Signed-off-by Kuppuswamy Sathyanarayanan <[email protected]>Reviewed-by Andi Kleen <[email protected]>Reviewed-by Tony Luck <[email protected]>--- arch/x86/Kconfig | 15 +++++++++++++++ 1 file changed, 15 insertions(+)arch/x86/Kconfig +0
-0@@ -875,6 +875,21 @@ config ACRN_GUEST IOT with small footprint and real-time features. More details can be found in https://projectacrn.org/.
Quoted 13 lines
arch/x86/Kconfig config ACRN_GUEST · line 887
Quoted 10 lines
arch/x86/Kconfig +10
-0+config INTEL_TDX_GUEST + bool "Intel Trusted Domain eXtensions Guest Support" + depends on X86_64 && CPU_SUP_INTEL && PARAVIRT + depends on SECURITY + select PARAVIRT_XL + select X86_X2APIC + select SECURITY_LOCKDOWN_LSM + help + Provide support for running in a trusted domain on Intel processors + equipped with Trusted Domain eXtenstions. TDX is an new Intela new Intel
Good catch. I will fix it in next version.
Quoted 11 lines
Quoted 9 lines
arch/x86/Kconfig +5
-0+ technology that extends VMX and Memory Encryption with a new kind of + virtual machine guest called Trust Domain (TD). A TD is designed to + run in a CPU mode that protects the confidentiality of TD memory + contents and the TD’s CPU state from other software, including VMM. + endif #HYPERVISOR_GUEST source "arch/x86/Kconfig.cpu"
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
RFC v2 3/32 x86/cpufeatures: Add TDX Guest CPU feature
In reply to Kuppuswamy SathyanarayananAdd CPU feature detection for Trusted Domain Extensions support. TDX feature adds capabilities to keep guest register state and memory isolated from hypervisor.
For TDX guest platforms, executing CPUID(0x21, 0) will return following values in EAX, EBX, ECX and EDX.
EAX: Maximum sub-leaf number: 0 EBX/EDX/ECX: Vendor string:
EBX = "Inte" EDX = "lTDX" ECX = " "
So when above condition is true, set X86_FEATURE_TDX_GUEST feature cap bit
--- arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/tdx.h | 20 ++++++++++++++++++++ arch/x86/kernel/Makefile | 1 + arch/x86/kernel/head64.c | 3 +++ arch/x86/kernel/tdx.c | 30 ++++++++++++++++++++++++++++++ 5 files changed, 55 insertions(+) create mode 100644 arch/x86/include/asm/tdx.h create mode 100644 arch/x86/kernel/tdx.c
arch/x86/include/asm/cpufeatures.h +1 -0
@@ -236,6 +236,7 @@#define X86_FEATURE_EPT_AD ( 8*32+17)/* Intel Extended Page Table access-dirty bit */ #define X86_FEATURE_VMCALL ( 8*32+18)/* "" Hypervisor supports the VMCALL instruction */ #define X86_FEATURE_VMW_VMMCALL ( 8*32+19)/* "" VMware prefers VMMCALL hypercall instruction */ +#define X86_FEATURE_TDX_GUEST ( 8*32+20)/* Trusted Domain Extensions Guest */ /* Intel-defined CPU features, CPUID level 0x00000007:0 (EBX), word 9 */ #define X86_FEATURE_FSGSBASE ( 9*32+ 0)/* RDFSBASE, WRFSBASE, RDGSBASE, WRGSBASE instructions*/
arch/x86/include/asm/tdx.h new file +20 -0
@@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (C) 2020 Intel Corporation */ +#ifndef _ASM_X86_TDX_H +#define _ASM_X86_TDX_H + +#define TDX_CPUID_LEAF_ID 0x21 + +#ifdef CONFIG_INTEL_TDX_GUEST + +#include <asm/cpufeature.h> + +void __init tdx_early_init (void ); + +#else // !CONFIG_INTEL_TDX_GUEST + +static inline void tdx_early_init (void ) { }; + +#endif /* CONFIG_INTEL_TDX_GUEST */ + +#endif /* _ASM_X86_TDX_H */
arch/x86/kernel/Makefile +1 -0
@@ -127,6 +127,7 @@ obj-$(CONFIG_PARAVIRT_CLOCK) += pvclock.o obj-$(CONFIG_X86_PMEM_LEGACY_DEVICE) += pmem.o obj-$(CONFIG_JAILHOUSE_GUEST) += jailhouse.o +obj-$(CONFIG_INTEL_TDX_GUEST) += tdx.o obj-$(CONFIG_EISA) += eisa.o obj-$(CONFIG_PCSPKR_PLATFORM) += pcspeaker.o
arch/x86/kernel/head64.c +3 -0
@@ -40,6 +40,7 @@#include <asm/extable.h> #include <asm/trapnr.h> #include <asm/sev-es.h> +#include <asm/tdx.h> /* * Manage page tables very early on.
@@ -491,6 +492,8 @@ asmlinkage __visible void __init x86_64_start_kernel(char * real_mode_data)kasan_early_init (); +tdx_early_init (); +idt_setup_early_handler ();copy_bootdata (__va (real_mode_data));
arch/x86/kernel/tdx.c new file +30 -0
@@ -0,0 +1,30 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (C) 2020 Intel Corporation */ + +#include <asm/tdx.h> + +static inline bool cpuid_has_tdx_guest (void ) +{ +u32 eax, signature[3 ]; + +if (cpuid_eax (0 ) <TDX_CPUID_LEAF_ID ) +return false; + +cpuid_count (TDX_CPUID_LEAF_ID ,0 , &eax, &signature[0 ], + &signature[1 ], &signature[2 ]); + +if (memcmp ("IntelTDX " , signature,12 )) +return false; + +return true; +} + +void __init tdx_early_init (void ) +{ +if (!cpuid_has_tdx_guest ()) +return ; + +setup_force_cpu_cap (X86_FEATURE_TDX_GUEST ); + +pr_info ("TDX guest is initialized\n" ); +}
Signature · 1 line
2.25.1
RFC v2 4/32 x86/x86: Add is_tdx_guest() interface
In reply to Kuppuswamy SathyanarayananAdd helper function to detect TDX feature support. It will be used to protect TDX specific code.
--- arch/x86/boot/compressed/Makefile | 1 + arch/x86/boot/compressed/tdx.c | 32 +++++++++++++++++++++++++++++++ arch/x86/include/asm/tdx.h | 8 ++++++++ arch/x86/kernel/tdx.c | 6 ++++++ 4 files changed, 47 insertions(+) create mode 100644 arch/x86/boot/compressed/tdx.c
arch/x86/boot/compressed/Makefile +1 -0
@@ -96,6 +96,7 @@ ifdef CONFIG_X86_64 endif vmlinux-objs-$(CONFIG_ACPI) += $(obj)/acpi.o +vmlinux-objs-$(CONFIG_INTEL_TDX_GUEST) += $(obj)/tdx.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/tdx.c new file +32 -0
@@ -0,0 +1,32 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* tdx.c - Early boot code for TDX +*/ + +#include <asm/tdx.h> + +static int __ro_after_init tdx_guest =-1 ; + +static inline bool native_cpuid_has_tdx_guest (void ) +{ +u32 eax =TDX_CPUID_LEAF_ID , signature[3 ] = {0 }; + +if (native_cpuid_eax (0 ) <TDX_CPUID_LEAF_ID ) +return false; + +native_cpuid (&eax, &signature[0 ], &signature[1 ], &signature[2 ]); + +if (memcmp ("IntelTDX " , signature,12 )) +return false; + +return true; +} + +bool is_tdx_guest (void ) +{ +if (tdx_guest <0 ) + tdx_guest =native_cpuid_has_tdx_guest (); + +return !!tdx_guest; +} +
arch/x86/include/asm/tdx.h +8 -0
@@ -9,10 +9,18 @@#include <asm/cpufeature.h> +/* Common API to check TDX support in decompression and common kernel code. */ +bool is_tdx_guest (void ); +void __init tdx_early_init (void );#else // !CONFIG_INTEL_TDX_GUEST +static inline bool is_tdx_guest (void ) +{ +return false; +} +static inline void tdx_early_init (void ) { };#endif /* CONFIG_INTEL_TDX_GUEST */
arch/x86/kernel/tdx.c +6 -0
@@ -19,6 +19,12 @@ static inline bool cpuid_has_tdx_guest(void)return true; } +bool is_tdx_guest (void ) +{ +return static_cpu_has (X86_FEATURE_TDX_GUEST ); +} +EXPORT_SYMBOL_GPL (is_tdx_guest); +void __init tdx_early_init (void ) {if (!cpuid_has_tdx_guest ())
Signature · 1 line
2.25.1
RFC v2 5/32 x86/tdx: Add __tdcall() and __tdvmcall() helper functions
In reply to Kuppuswamy SathyanarayananGuests communicate with VMMs with hypercalls. Historically, these are implemented using instructions that are known to cause VMEXITs like vmcall, vmlaunch, etc. However, with TDX, VMEXITs no longer expose guest state to the host. This prevents the old hypercall mechanisms from working. So to communicate with VMM, TDX specification defines a new instruction called "tdcall".
In TDX based VM, since VMM is an untrusted entity, a intermediary layer (TDX module) exists between host and guest to facilitate the secure communication. And "tdcall" instruction is used by the guest to request services from TDX module. And a variant of "tdcall" instruction (with specific arguments as defined by GHCI) is used by the guest to request services from VMM via the TDX module.
Implement common helper functions to communicate with the TDX Module and VMM (using TDCALL instruction).__tdvmcall() - function can be used to request services from the VMM.
__tdcall() - function can be used to communicate with the TDX Module.
Also define two additional wrappers, tdvmcall() and tdvmcall_out_r11() to cover common use cases of __tdvmcall() function. Since each use case of __tdcall() is different, we don't need such wrappers for it.
Implement __tdcall() and __tdvmcall() helper functions in assembly. Rationale behind choosing to use assembly over inline assembly are,
1. Since the number of lines of instructions (with comments) in __tdvmcall() implementation is over 70, using inline assembly to implement it will make it hard to read.
2. Also, since many registers (R8-R15, R[A-D]X)) will be used in TDVMCAL/TDCALL operation, if all these registers are included in in-line assembly constraints, some of the older compilers may not be able to meet this requirement.
Also, just like syscalls, not all TDVMCALL/TDCALLs use cases need to use the same set of argument registers. The implementation here picks the current worst-case scenario for TDCALL (4 registers). For TDCALLs with fewer than 4 arguments, there will end up being a few superfluous (cheap) instructions. But, this approach maximizes code reuse. The same argument applies to __tdvmcall() function as well.
Current implementation of __tdvmcall() includes error handling (ud2 on failure case) in assembly function instead of doing it in C wrapper function. The reason behind this choice is, when adding support for in/out instructions (refer to patch titled "x86/tdx: Handle port I/O" in this series), we use alternative_io() to substitute in/out instruction with __tdvmcall() calls. So use of C wrappers is not trivial in this case because the input parameters will be in the wrong registers and it's tricky to include proper buffer code to make this happen.
For registers used by TDCALL instruction, please check TDX GHCI specification, sec 2.4 and 3.
--- arch/x86/include/asm/tdx.h | 26 +++++ arch/x86/kernel/Makefile | 2 +- arch/x86/kernel/asm-offsets.c | 22 ++++ arch/x86/kernel/tdcall.S | 200 ++++++++++++++++++++++++++++++++++ arch/x86/kernel/tdx.c | 36 ++++++ 5 files changed, 285 insertions(+), 1 deletion(-) create mode 100644 arch/x86/kernel/tdcall.S
arch/x86/include/asm/tdx.h +26 -0
@@ -8,12 +8,38 @@#ifdef CONFIG_INTEL_TDX_GUEST #include <asm/cpufeature.h> +#include <linux/types.h> + +struct tdcall_output { +u64 rcx; +u64 rdx; +u64 r8; +u64 r9; +u64 r10; +u64 r11; +}; + +struct tdvmcall_output { +u64 r11; +u64 r12; +u64 r13; +u64 r14; +u64 r15; +};/* Common API to check TDX support in decompression and common kernel code. */ bool is_tdx_guest (void );void __init tdx_early_init (void ); +/* Helper function used to communicate with the TDX module */ +u64 __tdcall (u64 fn,u64 rcx,u64 rdx,u64 r8,u64 r9, +struct tdcall_output *out); + +/* Helper function used to request services from VMM */ +u64 __tdvmcall (u64 fn,u64 r12,u64 r13,u64 r14,u64 r15, +struct tdvmcall_output *out); +#else // !CONFIG_INTEL_TDX_GUEST static inline bool is_tdx_guest (void )
arch/x86/kernel/Makefile +1 -1
@@ -127,7 +127,7 @@ obj-$(CONFIG_PARAVIRT_CLOCK) += pvclock.o obj-$(CONFIG_X86_PMEM_LEGACY_DEVICE) += pmem.o obj-$(CONFIG_JAILHOUSE_GUEST) += jailhouse.o -obj-$(CONFIG_INTEL_TDX_GUEST) += tdx.o +obj-$(CONFIG_INTEL_TDX_GUEST) += tdcall.o tdx.o obj-$(CONFIG_EISA) += eisa.o obj-$(CONFIG_PCSPKR_PLATFORM) += pcspeaker.o
arch/x86/kernel/asm-offsets.c +22 -0
@@ -23,6 +23,10 @@#include <xen/interface/xen.h> #endif +#ifdef CONFIG_INTEL_TDX_GUEST +#include <asm/tdx.h> +#endif +#ifdef CONFIG_X86_32 # include "asm-offsets_32.c" #else
@@ -75,6 +79,24 @@ static void __used common(void)OFFSET (XEN_vcpu_info_arch_cr2, vcpu_info, arch.cr2);#endif +#ifdef CONFIG_INTEL_TDX_GUEST +BLANK (); +/* Offset for fields in tdcall_output */ +OFFSET (TDCALL_rcx, tdcall_output, rcx); +OFFSET (TDCALL_rdx, tdcall_output, rdx); +OFFSET (TDCALL_r8, tdcall_output, r8); +OFFSET (TDCALL_r9, tdcall_output, r9); +OFFSET (TDCALL_r10, tdcall_output, r10); +OFFSET (TDCALL_r11, tdcall_output, r11); + +/* Offset for fields in tdvmcall_output */ +OFFSET (TDVMCALL_r11, tdvmcall_output, r11); +OFFSET (TDVMCALL_r12, tdvmcall_output, r12); +OFFSET (TDVMCALL_r13, tdvmcall_output, r13); +OFFSET (TDVMCALL_r14, tdvmcall_output, r14); +OFFSET (TDVMCALL_r15, tdvmcall_output, r15); +#endif +BLANK ();OFFSET (BP_scratch, boot_params, scratch);OFFSET (BP_secure_boot, boot_params, secure_boot);
arch/x86/kernel/tdcall.S new file +200 -0
200 lines not on this page · raw message
arch/x86/kernel/tdx.c +36 -0
@@ -1,8 +1,44 @@// SPDX-License-Identifier: GPL-2.0 /* Copyright (C) 2020 Intel Corporation */ +#define pr_fmt (fmt) "TDX: " fmt +#include <asm/tdx.h> +/* +* Wrapper for use case that checks for error code and print warning message. +*/ +static inline u64 tdvmcall (u64 fn,u64 r12,u64 r13,u64 r14,u64 r15) +{ +u64 err; + + err =__tdvmcall (fn, r12, r13, r14, r15,NULL ); + +if (err) +pr_warn_ratelimited ("TDVMCALL fn:%llx failed with err:%llx\n" , + fn, err); + +return err; +} + +/* +* Wrapper for the semi-common case where we need single output value (R11). +*/ +static inline u64 tdvmcall_out_r11 (u64 fn,u64 r12,u64 r13,u64 r14,u64 r15) +{ + +struct tdvmcall_output out = {0 }; +u64 err; + + err =__tdvmcall (fn, r12, r13, r14, r15, &out); + +if (err) +pr_warn_ratelimited ("TDVMCALL fn:%llx failed with err:%llx\n" , + fn, err); + +return out.r11; +} +static inline bool cpuid_has_tdx_guest (void ) {u32 eax, signature[3 ];
Signature · 1 line
2.25.1
Re: [RFC v2 05/32] x86/tdx: Add __tdcall() and __tdvmcall() helper functions
arch/x86/kernel/tdcall.S line 13
Quoted 5 lines
arch/x86/kernel/tdcall.S new file +5
-0+/* + * Expose registers R10-R15 to VMM (for bitfield info + * refer to TDX GHCI specification). + */ +#define TDVMCALL_EXPOSE_REGS_MASK 0xfc00
Why can't we do:
#define TDC_R10 BIT(18)
#define TDC_R11 BIT(19)
and:
#define TDVMCALL_EXPOSE_REGS_MASK (TDX_R10 | TDX_R11 | TDX_R12 ...
or at least:
#define TDVMCALL_EXPOSE_REGS_MASK BIT(18) | BIT(19) ...
?
arch/x86/kernel/tdcall.S line 23
Quoted 9 lines
arch/x86/kernel/tdcall.S new file +9
-0+/* + * TDX guests use the TDCALL instruction to make + * hypercalls to the VMM. It is supported in + * Binutils >= 2.36. + */ +#define tdcall .byte 0x66,0x0f,0x01,0xcc + +/* + * __tdcall() - Used to communicate with the TDX module
Why is this function here? What does it do? Why do we need it?
I'd like this to actually talk about doing impedance matching between the function call and TDCALL ABIs.
Quoted 9 lines
+ * @arg1 (RDI) - TDCALL Leaf ID
+ * @arg2 (RSI) - Input parameter 1 passed to TDX module
+ * via register RCX
+ * @arg2 (RDX) - Input parameter 2 passed to TDX module
+ * via register RDX
+ * @arg3 (RCX) - Input parameter 3 passed to TDX module
+ * via register R8
+ * @arg4 (R8) - Input parameter 4 passed to TDX module
+ * via register R9
The unnecessary repitition and verbosity actually make this harder to read. This looks like it was easy to write, but not much effort is being made to make it easy to consume. Could you please apply some consideration to making it more readable?
Quoted 3 lines
+ * @arg5 (R9) - struct
tdcall_outputpointer
+ *
+ * @out - Return status of tdcall via RAX.
Don't comments usually just say "returns ... foo"? Also, the @params usually refer to *REAL* variable names. Where the heck does "out" come from? Why are you even putting argX? Shouldn't these be @'s be their literal function argument names?
@rdi - Input parameter, moved to RCX
arch/x86/kernel/tdcall.S line 48
Quoted 11 lines
+ * NOTE: This function should only used for non TDVMCALL
+ * use cases
+ */arch/x86/kernel/tdcall.S new file +8
-0+SYM_FUNC_START(__tdcall) + FRAME_BEGIN + + /* Save non-volatile GPRs that are exposed to the VMM. */ + push %r15 + push %r14 + push %r13 + push %r12
Why do we have to save these? Because they might be clobbered? If so, let's say *THAT* instead of just "exposed". "Exposed" could mean "VMM can read".
Also, this just told me that this function can't be used to talk to the VMM. Why is this talking about exposure to the VMM?
arch/x86/kernel/tdcall.S line 53
Quoted 4 lines
arch/x86/kernel/tdcall.S new file +4
-0+ /* Move TDCALL Leaf ID to RAX */ + mov %rdi, %rax + /* Move output pointer to R12 */ + mov %r9, %r12
I thought 'struct tdcall_output' was a purely software construct. Why are we passing a pointer to it into TDCALL?
arch/x86/kernel/tdcall.S line 60
Quoted 7 lines
arch/x86/kernel/tdcall.S new file +7
-0+ /* Move input param 4 to R9 */ + mov %r8, %r9 + /* Move input param 3 to R8 */ + mov %rcx, %r8 + /* Leave input param 2 in RDX */ + /* Move input param 1 to RCX */ + mov %rsi, %rcx
With a little work, this can be made a *LOT* more readable:
/* Mangle function call ABI into TDCALL ABI: */ mov %rdi, %rax /* Move TDCALL Leaf ID to RAX */ mov %r9, %r12 /* Move output pointer to R12 */ mov %r8, %r9 /* Move input 4 to R9 */ mov %rcx, %r8 /* Move input 3 to R8 */ mov %rsi, %rcx /* Move input 1 to RCX */ /* Leave input param 2 in RDX */
arch/x86/kernel/tdcall.S line 70
Quoted 9 lines
arch/x86/kernel/tdcall.S new file +9
-0+ tdcall + + /* Check for TDCALL success: 0 - Successful, otherwise failed */ + test %rax, %rax + jnz 1f + + /* Check for a TDCALL output struct */ + test %r12, %r12 + jz 1f
Does some universal status come back in r12? Aren't we dealing with a VMM/SEAM-controlled register here? Isn't this dangerous?
Quoted 5 lines
arch/x86/kernel/tdcall.S new file +5
-0+ /* Copy TDCALL result registers to output struct: */ + movq %rcx, TDCALL_rcx(%r12) + movq %rdx, TDCALL_rdx(%r12) + movq %r8, TDCALL_r8(%r12) + movq %r9, TDCALL_r9(%r12)
arch/x86/kernel/tdcall.S line 86
Quoted 10 lines
arch/x86/kernel/tdcall.S new file +10
-0+ movq %r10, TDCALL_r10(%r12) + movq %r11, TDCALL_r11(%r12) +1: + /* Zero out registers exposed to the TDX Module. */ + xor %rcx, %rcx + xor %rdx, %rdx + xor %r8d, %r8d + xor %r9d, %r9d + xor %r10d, %r10d + xor %r11d, %r11d
... why?
Quoted 17 lines
arch/x86/kernel/tdcall.S new file +17
-0+ /* Restore non-volatile GPRs that are exposed to the VMM. */ + pop %r12 + pop %r13 + pop %r14 + pop %r15 + + FRAME_END + ret +SYM_FUNC_END(__tdcall) + +/* + * do_tdvmcall() - Used to communicate with the VMM. + * + * @arg1 (RDI) - TDVMCALL function, e.g. exit reason + * @arg2 (RSI) - Input parameter 1 passed to VMM + * via register R12 + * @arg3 (RDX) - Input parameter 2 passed to VMM
arch/x86/kernel/tdcall.S line 114
Quoted 10 lines
arch/x86/kernel/tdcall.S new file +10
-0+ * via register R13 + * @arg4 (RCX) - Input parameter 3 passed to VMM + * via register R14 + * @arg5 (R8) - Input parameter 4 passed to VMM + * via register R15 + * @arg6 (R9) - struct tdvmcall_output pointer + * + * @out - Return status of tdvmcall(R10) via RAX. + * + */
Same comments on the sparse comment style.
arch/x86/kernel/tdcall.S line 124
Quoted 10 lines
arch/x86/kernel/tdcall.S new file +10
-0+SYM_CODE_START_LOCAL(do_tdvmcall) + FRAME_BEGIN + + /* Save non-volatile GPRs that are exposed to the VMM. */ + push %r15 + push %r14 + push %r13 + push %r12 + + /* Set TDCALL leaf ID to TDVMCALL (0) in RAX */
I think there needs to be some discussion of what TDCALL and TDVMCALL are. They are named too similarly not to do so.
Quoted 5 lines
arch/x86/kernel/tdcall.S new file +5
-0+ xor %eax, %eax + /* Move TDVMCALL function id (1st argument) to R11 */ + mov %rdi, %r11> + /* Move Input parameter 1-4 to R12-R15 */ + mov %rsi, %r12 + mov %rdx, %r13
arch/x86/kernel/tdcall.S line 140
Quoted 10 lines
arch/x86/kernel/tdcall.S new file +10
-0+ mov %rcx, %r14 + mov %r8, %r15 + /* Leave tdvmcall output pointer in R9 */ + + /* + * Value of RCX is used by the TDX Module to determine which + * registers are exposed to VMM. Each bit in RCX represents a + * register id. You can find the bitmap details from TDX GHCI + * spec. + */
This doesn't belong here. Put it along with theTDVMCALL_EXPOSE_REGS_MASK, please.
arch/x86/kernel/tdcall.S line 151
Quoted 11 lines
arch/x86/kernel/tdcall.S new file +11
-0+ movl $TDVMCALL_EXPOSE_REGS_MASK, %ecx + + tdcall + + /* + * Check for TDCALL success: 0 - Successful, otherwise failed. + * If failed, there is an issue with TDX Module which is fatal + * for the guest. So panic. + */ + test %rax, %rax + jnz 2f
So, just to be clear: %RAX is under the control of the SEAM module. The VMM has no control over it. Right?
Shouldn't we say that explicitly?
arch/x86/kernel/tdcall.S line 162
Quoted 10 lines
arch/x86/kernel/tdcall.S new file +10
-0+ /* Move TDVMCALL success/failure to RAX to return to user */ + mov %r10, %rax + + /* Check for TDVMCALL success: 0 - Successful, otherwise failed */ + test %rax, %rax + jnz 1f + + /* Check for a TDVMCALL output struct */ + test %r9, %r9 + jz 1f
I'd also include a note that %r9 was neither writable nor its value exposed to the VMM.
Quoted 27 lines
arch/x86/kernel/tdcall.S new file +27
-0+ /* Copy TDVMCALL result registers to output struct: */ + movq %r11, TDVMCALL_r11(%r9) + movq %r12, TDVMCALL_r12(%r9) + movq %r13, TDVMCALL_r13(%r9) + movq %r14, TDVMCALL_r14(%r9) + movq %r15, TDVMCALL_r15(%r9) +1: + /* + * Zero out registers exposed to the VMM to avoid + * speculative execution with VMM-controlled values. + */ + xor %r10d, %r10d + xor %r11d, %r11d + xor %r12d, %r12d + xor %r13d, %r13d + xor %r14d, %r14d + xor %r15d, %r15d + + /* Restore non-volatile GPRs that are exposed to the VMM. */ + pop %r12 + pop %r13 + pop %r14 + pop %r15 + + FRAME_END + ret +2:
arch/x86/kernel/tdcall.S line 200
Quoted 10 lines
arch/x86/kernel/tdcall.S new file +10
-0+ ud2 +SYM_CODE_END(do_tdvmcall) + +/* Helper function for standard type of TDVMCALL */ +SYM_FUNC_START(__tdvmcall) + /* Set TDVMCALL type info (0 - Standard, > 0 - vendor) in R10 */ + xor %r10, %r10 + call do_tdvmcall + retq +SYM_FUNC_END(__tdvmcall)
Why do we need this helper? Why does it need to be in assembly?
Quoted 5 lines
arch/x86/kernel/tdx.c +0
-0
arch/x86/kernel/tdx.c line 10
Quoted 10 lines
arch/x86/kernel/tdx.c +5
-0@@ -1,8 +1,44 @@// SPDX-License-Identifier: GPL-2.0 /* Copyright (C) 2020 Intel Corporation */ +#define pr_fmt (fmt) "TDX: " fmt +#include <asm/tdx.h> +/* +* Wrapper for use case that checks for error code and print warning message. +*/
This comment isn't very useful. I can see the error check and warning by reading the code.
Quoted 20 lines
arch/x86/kernel/tdx.c +20
-0+static inline u64 tdvmcall (u64 fn,u64 r12,u64 r13,u64 r14,u64 r15) +{ +u64 err; + + err =__tdvmcall (fn, r12, r13, r14, r15,NULL ); + +if (err) +pr_warn_ratelimited ("TDVMCALL fn:%llx failed with err:%llx\n" , + fn, err); + +return err; +} + +/* +* Wrapper for the semi-common case where we need single output value (R11). +*/ +static inline u64 tdvmcall_out_r11 (u64 fn,u64 r12,u64 r13,u64 r14,u64 r15) +{ + +struct tdvmcall_output out = {0 };
arch/x86/kernel/tdx.c line 40
Quoted 10 lines
arch/x86/kernel/tdx.c +10
-0+u64 err; + + err =__tdvmcall (fn, r12, r13, r14, r15, &out); + +if (err) +pr_warn_ratelimited ("TDVMCALL fn:%llx failed with err:%llx\n" , + fn, err); + +return out.r11; +}
How do callers check for errors? Is the error value superfluously returned in r11 and another output register?
Re: [RFC v2 05/32] x86/tdx: Add __tdcall() and __tdvmcall() helper functions
Quoted 11 lines · Dave Hansen
arch/x86/kernel/tdcall.S line 13
Quoted 5 lines
arch/x86/kernel/tdcall.S new file +5
-0+/* + * Expose registers R10-R15 to VMM (for bitfield info + * refer to TDX GHCI specification). + */ +#define TDVMCALL_EXPOSE_REGS_MASK 0xfc00Why can't we do:
#define
TDC_R10BIT(18)
#defineTDC_R11BIT(19)
Quoted 7 lines
and:
#define
TDVMCALL_EXPOSE_REGS_MASK(TDX_R10|TDX_R11|TDX_R12...or at least:
#define
TDVMCALL_EXPOSE_REGS_MASKBIT(18) | BIT(19) ...
If this is the preferred way, I will change it use macros (TDX_Rxx).
Quoted 14 lines
?
arch/x86/kernel/tdcall.S line 23
Quoted 9 lines
arch/x86/kernel/tdcall.S new file +9
-0+/* + * TDX guests use the TDCALL instruction to make + * hypercalls to the VMM. It is supported in + * Binutils >= 2.36. + */ +#define tdcall .byte 0x66,0x0f,0x01,0xcc + +/* + * __tdcall() - Used to communicate with the TDX moduleWhy is this function here? What does it do? Why do we need it?
__tdcall() function is used to request services from the TDX Module. Example use cases are, TDREPORT, VEINFO, TDINFO, etc.
Quoted 20 lines
I'd like this to actually talk about doing impedance matching between the function call and TDCALL ABIs.
Quoted 9 lines
+ * @arg1 (RDI) - TDCALL Leaf ID
+ * @arg2 (RSI) - Input parameter 1 passed to TDX module
+ * via register RCX
+ * @arg2 (RDX) - Input parameter 2 passed to TDX module
+ * via register RDX
+ * @arg3 (RCX) - Input parameter 3 passed to TDX module
+ * via register R8
+ * @arg4 (R8) - Input parameter 4 passed to TDX module
+ * via register R9The unnecessary repitition and verbosity actually make this harder to read. This looks like it was easy to write, but not much effort is being made to make it easy to consume. Could you please apply some consideration to making it more readable?
Quoted 8 lines
Quoted 3 lines
+ * @arg5 (R9) - struct
tdcall_outputpointer
+ *
+ * @out - Return status of tdcall via RAX.Don't comments usually just say "returns ... foo"? Also, the @params usually refer to *REAL* variable names. Where the heck does "out" come from? Why are you even putting argX? Shouldn't these be @'s be their literal function argument names?
I have added this comment block to make it easier for us to understand the register mapping between function arguments and TDCALL ABI. But I got your point. Usage of @arg1 or @out does not comply the function comment standards. I will fix this in next version.
Quoted 2 lines
@rdi - Input parameter, moved to RCX
I will use the above format to document function arguments.
Quoted 17 lines
arch/x86/kernel/tdcall.S line 48
Quoted 12 lines
+ * NOTE: This function should only used for non TDVMCALL
+ * use cases
+ */arch/x86/kernel/tdcall.S new file +8
-0+SYM_FUNC_START(__tdcall) + FRAME_BEGIN + + /* Save non-volatile GPRs that are exposed to the VMM. */ + push %r15 + push %r14 + push %r13 + push %r12Why do we have to save these? Because they might be clobbered? If so, let's say *THAT* instead of just "exposed". "Exposed" could mean "VMM can read".
Quoted 2 lines
Also, this just told me that this function can't be used to talk to the VMM. Why is this talking about exposure to the VMM?
Although __tdcall() is only used to communicate with the TDX module and the TDX module is not supposed to touch these registers, just to be on the safe side, I have tried to save the context of registers R12-R15. Anyway cycles used by instructions are less compared to tdcall.
Quoted 8 lines
arch/x86/kernel/tdcall.S line 53
Quoted 5 lines
arch/x86/kernel/tdcall.S new file +4
-0+ /* Move TDCALL Leaf ID to RAX */ + mov %rdi, %rax + /* Move output pointer to R12 */ + mov %r9, %r12I thought 'struct
tdcall_output' was a purely software construct. Why are we passing a pointer to it into TDCALL?
Its used to store the TDCALL result (RCX, RDX, R8-R11). As far as this function is concerned, its just a block of memory (accessed using base address + TDCALL_r* offsets).
Quoted 11 lines
arch/x86/kernel/tdcall.S line 60
Quoted 8 lines
arch/x86/kernel/tdcall.S new file +7
-0+ /* Move input param 4 to R9 */ + mov %r8, %r9 + /* Move input param 3 to R8 */ + mov %rcx, %r8 + /* Leave input param 2 in RDX */ + /* Move input param 1 to RCX */ + mov %rsi, %rcxWith a little work, this can be made a *LOT* more readable:
Quoted 7 lines
/* Mangle function call ABI into TDCALL ABI: */ mov %rdi, %rax /* Move TDCALL Leaf ID to RAX */ mov %r9, %r12 /* Move output pointer to R12 */ mov %r8, %r9 /* Move input 4 to R9 */ mov %rcx, %r8 /* Move input 3 to R8 */ mov %rsi, %rcx /* Move input 1 to RCX */ /* Leave input param 2 in RDX */
Ok. I will use your version.
Quoted 14 lines
arch/x86/kernel/tdcall.S line 70
Quoted 11 lines
arch/x86/kernel/tdcall.S new file +9
-0+ tdcall + + /* Check for TDCALL success: 0 - Successful, otherwise failed */ + test %rax, %rax + jnz 1f + + /* Check for a TDCALL output struct */ + test %r12, %r12 + jz 1fDoes some universal status come back in r12? Aren't we dealing with a VMM/SEAM-controlled register here? Isn't this dangerous?
R12 is the temporary register we have used to store the address of user passed output pointer. We just check for NULL condition here. R12 will not be used by the TDX module.
If you prefer, we can just push the output pointer to stack and get it after we make the tdcall.
Quoted 10 lines
Quoted 10 lines
arch/x86/kernel/tdcall.S new file +9
-0+ /* Copy TDCALL result registers to output struct: */ + movq %rcx, TDCALL_rcx(%r12) + movq %rdx, TDCALL_rdx(%r12) + movq %r8, TDCALL_r8(%r12) + movq %r9, TDCALL_r9(%r12) + movq %r10, TDCALL_r10(%r12) + movq %r11, TDCALL_r11(%r12) +1: + /* Zero out registers exposed to the TDX Module. */
Quoted 8 lines
arch/x86/kernel/tdcall.S line 86
Quoted 6 lines
arch/x86/kernel/tdcall.S new file +6
-0+ xor %rcx, %rcx + xor %rdx, %rdx + xor %r8d, %r8d + xor %r9d, %r9d + xor %r10d, %r10d + xor %r11d, %r11d... why?
These registers are used by the TDX Module. Why pass the stale values back to the user? So we clear them here.
Quoted 22 lines
Quoted 22 lines
arch/x86/kernel/tdcall.S new file +21
-0+ /* Restore non-volatile GPRs that are exposed to the VMM. */ + pop %r12 + pop %r13 + pop %r14 + pop %r15 + + FRAME_END + ret +SYM_FUNC_END(__tdcall) + +/* + * do_tdvmcall() - Used to communicate with the VMM. + * + * @arg1 (RDI) - TDVMCALL function, e.g. exit reason + * @arg2 (RSI) - Input parameter 1 passed to VMM + * via register R12 + * @arg3 (RDX) - Input parameter 2 passed to VMM + * via register R13 + * @arg4 (RCX) - Input parameter 3 passed to VMM + * via register R14 + * @arg5 (R8) - Input parameter 4 passed to VMM
Quoted 8 lines
Quoted 6 lines
+ * via register R15
+ * @arg6 (R9) - structtdvmcall_outputpointer
+ *
+ * @out - Return status of tdvmcall(R10) via RAX.
+ *
+ */Same comments on the sparse comment style.
will fix it similar to __tdcall().
Quoted 14 lines
arch/x86/kernel/tdcall.S line 124
Quoted 11 lines
arch/x86/kernel/tdcall.S new file +10
-0+SYM_CODE_START_LOCAL(do_tdvmcall) + FRAME_BEGIN + + /* Save non-volatile GPRs that are exposed to the VMM. */ + push %r15 + push %r14 + push %r13 + push %r12 + + /* Set TDCALL leaf ID to TDVMCALL (0) in RAX */I think there needs to be some discussion of what TDCALL and TDVMCALL are. They are named too similarly not to do so.
TDVMCALL is the sub function of TDCALL (selected by setting RAX register to 0). TDVMCALL is used to request services from VMM.
Quoted 11 lines
Quoted 11 lines
arch/x86/kernel/tdcall.S new file +10
-0+ xor %eax, %eax + /* Move TDVMCALL function id (1st argument) to R11 */ + mov %rdi, %r11> + /* Move Input parameter 1-4 to R12-R15 */ + mov %rsi, %r12 + mov %rdx, %r13 + mov %rcx, %r14 + mov %r8, %r15 + /* Leave tdvmcall output pointer in R9 */ + + /*
Quoted 8 lines
arch/x86/kernel/tdcall.S line 140
Quoted 5 lines
arch/x86/kernel/tdcall.S new file +5
-0+ * Value of RCX is used by the TDX Module to determine which + * registers are exposed to VMM. Each bit in RCX represents a + * register id. You can find the bitmap details from TDX GHCI + * spec. + */This doesn't belong here. Put it along with the
TDVMCALL_EXPOSE_REGS_MASK, please.
Ok. I will do it.
Quoted 15 lines
arch/x86/kernel/tdcall.S line 151
Quoted 12 lines
arch/x86/kernel/tdcall.S new file +11
-0+ movl $TDVMCALL_EXPOSE_REGS_MASK, %ecx + + tdcall + + /* + * Check for TDCALL success: 0 - Successful, otherwise failed. + * If failed, there is an issue with TDX Module which is fatal + * for the guest. So panic. + */ + test %rax, %rax + jnz 2fSo, just to be clear: %RAX is under the control of the SEAM module. The VMM has no control over it. Right?
AFAIK, VMM will not touch it.
Sean, please confirm it.
Quoted 2 lines
Shouldn't we say that explicitly?
I can add it to above comment.
Quoted 14 lines
arch/x86/kernel/tdcall.S line 162
Quoted 11 lines
arch/x86/kernel/tdcall.S new file +10
-0+ /* Move TDVMCALL success/failure to RAX to return to user */ + mov %r10, %rax + + /* Check for TDVMCALL success: 0 - Successful, otherwise failed */ + test %rax, %rax + jnz 1f + + /* Check for a TDVMCALL output struct */ + test %r9, %r9 + jz 1fI'd also include a note that %r9 was neither writable nor its value exposed to the VMM.
will do it.
Quoted 32 lines
Quoted 32 lines
arch/x86/kernel/tdcall.S new file +31
-0+ /* Copy TDVMCALL result registers to output struct: */ + movq %r11, TDVMCALL_r11(%r9) + movq %r12, TDVMCALL_r12(%r9) + movq %r13, TDVMCALL_r13(%r9) + movq %r14, TDVMCALL_r14(%r9) + movq %r15, TDVMCALL_r15(%r9) +1: + /* + * Zero out registers exposed to the VMM to avoid + * speculative execution with VMM-controlled values. + */ + xor %r10d, %r10d + xor %r11d, %r11d + xor %r12d, %r12d + xor %r13d, %r13d + xor %r14d, %r14d + xor %r15d, %r15d + + /* Restore non-volatile GPRs that are exposed to the VMM. */ + pop %r12 + pop %r13 + pop %r14 + pop %r15 + + FRAME_END + ret +2: + ud2 +SYM_CODE_END(do_tdvmcall) + +/* Helper function for standard type of TDVMCALL */
Quoted 8 lines
arch/x86/kernel/tdcall.S line 200
Quoted 6 lines
arch/x86/kernel/tdcall.S new file +6
-0+SYM_FUNC_START(__tdvmcall) + /* Set TDVMCALL type info (0 - Standard, > 0 - vendor) in R10 */ + xor %r10, %r10 + call do_tdvmcall + retq +SYM_FUNC_END(__tdvmcall)Why do we need this helper? Why does it need to be in assembly?
Its simpler to do it in assembly. Also, grouping all register updates in the same file will make it easier for us to read or debug issues. Another reason is, we also call do_tdvmcall() from in/out instruction use case.
Quoted 13 lines
Quoted 13 lines
arch/x86/kernel/tdx.c +2
-0@@ -1,8 +1,44 @@// SPDX-License-Identifier: GPL-2.0 /* Copyright (C) 2020 Intel Corporation */ +#define pr_fmt (fmt) "TDX: " fmt +#include <asm/tdx.h>
Quoted 6 lines
arch/x86/kernel/tdx.c line 10
Quoted 3 lines
arch/x86/kernel/tdx.c +3
-0+/* +* Wrapper for use case that checks for error code and print warning message. +*/ This comment isn't very useful. I can see the error check and warning by reading the code.
Its just a helper function that covers common case of checking for error and print the warning message. If this comment is superfluous, I can remove it.
Quoted 26 lines
Quoted 26 lines
arch/x86/kernel/tdx.c +25
-0+static inline u64 tdvmcall (u64 fn,u64 r12,u64 r13,u64 r14,u64 r15) +{ +u64 err; + + err =__tdvmcall (fn, r12, r13, r14, r15,NULL ); + +if (err) +pr_warn_ratelimited ("TDVMCALL fn:%llx failed with err:%llx\n" , + fn, err); + +return err; +} + +/* +* Wrapper for the semi-common case where we need single output value (R11). +*/ +static inline u64 tdvmcall_out_r11 (u64 fn,u64 r12,u64 r13,u64 r14,u64 r15) +{ + +struct tdvmcall_output out = {0 }; +u64 err; + + err =__tdvmcall (fn, r12, r13, r14, r15, &out); + +if (err)
Quoted 8 lines
arch/x86/kernel/tdx.c line 40
Quoted 5 lines
arch/x86/kernel/tdx.c +5
-0+pr_warn_ratelimited ("TDVMCALL fn:%llx failed with err:%llx\n" , + fn, err); + +return out.r11; +}How do callers check for errors? Is the error value superfluously returned in r11 and another output register?
We already check for error in this helper function. User of this function only cares about output value (R11). Mainly for in/out use case.
Quoted 1 line
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Re: [RFC v2 05/32] x86/tdx: Add __tdcall() and __tdvmcall() helper functions
Quoted 9 lines · Sathyanarayanan
Quoted 6 lines
arch/x86/kernel/tdcall.S line 23
Quoted 4 lines
arch/x86/kernel/tdcall.S new file +4
-0+#define tdcall .byte 0x66,0x0f,0x01,0xcc + +/* + * __tdcall() - Used to communicate with the TDX moduleWhy is this function here? What does it do? Why do we need it?
__tdcall()function is used to request services from the TDX Module. Example use cases are, TDREPORT, VEINFO, TDINFO, etc.
I think there might be some misinterpretation of my question. What you are describing is what *TDCALL* does. Why do we need a wrapper function? What purpose does this wrapper function serve? Why do we need this wrapper function?
Quoted 16 lines
Quoted 16 lines
arch/x86/kernel/tdcall.S line 48
Quoted 11 lines
+ * NOTE: This function should only used for non TDVMCALL
+ * use cases
+ */arch/x86/kernel/tdcall.S new file +8
-0+SYM_FUNC_START(__tdcall) + FRAME_BEGIN + + /* Save non-volatile GPRs that are exposed to the VMM. */ + push %r15 + push %r14 + push %r13 + push %r12Why do we have to save these? Because they might be clobbered? If so, let's say *THAT* instead of just "exposed". "Exposed" could mean "VMM can read".
Quoted 7 lines
Quoted 2 lines
Also, this just told me that this function can't be used to talk to the VMM. Why is this talking about exposure to the VMM?
Although
__tdcall()is only used to communicate with the TDX module and the TDX module is not supposed to touch these registers, just to be on the safe side, I have tried to save the context of registers R12-R15. Anyway cycles used by instructions are less compared to tdcall.
Why are you talking about the VMM if this is a call to the SEAM module?
Let's say someone is reading the TDCALL architecture spec. It will say something like, "blah blah, in this case TDCALL will not modify %r12->%r15". Then someone goes and looks at this code that basically says (or implies) "save these before the SEAM module modifies them". What is a coder to do?
Please remove the ambiguity, either by removing this superfluous (according to the spec) code, or documenting why it is not superfluous.
Quoted 11 lines
Quoted 7 lines
Quoted 4 lines
+ /* Move TDCALL Leaf ID to RAX */
+ mov %rdi, %rax
+ /* Move output pointer to R12 */
+ mov %r9, %r12I thought 'struct
tdcall_output' was a purely software construct. Why are we passing a pointer to it into TDCALL?Its used to store the TDCALL result (RCX, RDX, R8-R11). As far as this function is concerned, its just a block of memory (accessed using base address + TDCALL_r* offsets).
Is 'struct tdcall_output' a hardware architectural structure or a software structure?
If it's a software structure, then why are we passing a pointer to a software structure into a hardware ABI?
If it's a hardware architecture structure, where is the documentation for it?
Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
+ tdcall
+
+ /* Check for TDCALL success: 0 - Successful, otherwise failed */ + test %rax, %raxQuoted 8 lines
Quoted 5 lines
+ jnz 1f
+
+ /* Check for a TDCALL output struct */
+ test %r12, %r12
+ jz 1fDoes some universal status come back in r12? Aren't we dealing with a VMM/SEAM-controlled register here? Isn't this dangerous?
Quoted 3 lines
R12 is the temporary register we have used to store the address of user passed output pointer. We just check for NULL condition here. R12 will not be used by the TDX module.
OK, so how do you know this? Could you share your logic, please?
Quoted 2 lines
If you prefer, we can just push the output pointer to stack and get it after we make the tdcall.
I prefer that the code be understandable and be written for a clear purpose. If you're using r12 for temporary storage, I expect to see at least one reference *SOMEWHERE* to its use as temporary storage. Right now.... nothing.
Quoted 16 lines
Quoted 16 lines
Quoted 15 lines
+ /* Copy TDCALL result registers to output struct: */ + movq %rcx,
TDCALL_rcx(%r12)
+ movq %rdx,TDCALL_rdx(%r12)
+ movq %r8,TDCALL_r8(%r12)
+ movq %r9,TDCALL_r9(%r12)
+ movq %r10,TDCALL_r10(%r12)
+ movq %r11,TDCALL_r11(%r12)
+1:
+ /* Zero out registers exposed to the TDX Module. */ + xor %rcx, %rcx
+ xor %rdx, %rdx
+ xor %r8d, %r8d
+ xor %r9d, %r9d
+ xor %r10d, %r10d
+ xor %r11d, %r11d
Quoted 4 lines
Quoted 1 line
... why?
These registers are used by the TDX Module. Why pass the stale values back to the user? So we clear them here.
Please go look at some other assembly code in the kernel called from C.
Do those functions do this? Why? Why not? Do they care about
"passing stale values back up"?
Quoted 14 lines
Quoted 5 lines
Quoted 5 lines
arch/x86/kernel/tdcall.S new file +5
-0+SYM_CODE_START_LOCAL(do_tdvmcall) + FRAME_BEGIN + + /* Save non-volatile GPRs that are exposed to the VMM. */ + push %r15Quoted 8 lines
Quoted 5 lines
+ push %r14
+ push %r13
+ push %r12
+
+ /* Set TDCALL leaf ID to TDVMCALL (0) in RAX */I think there needs to be some discussion of what TDCALL and TDVMCALL are. They are named too similarly not to do so.
Quoted 2 lines
TDVMCALL is the sub function of TDCALL (selected by setting RAX register to 0). TDVMCALL is used to request services from VMM.
Actually, I think these functions are horribly misnamed.
I think we should make them
__tdx_seam_call()
or __tdx_module_call()
and
__tdx_hypercall() __tdcall() and __tdvmcall()
are really nonsensical in this context, especially since TDVMCALL is implemented with the TDCALL instruction, but not the __tdcall() function.
Quoted 10 lines
Quoted 9 lines
arch/x86/kernel/tdcall.S line 200
Quoted 7 lines
arch/x86/kernel/tdcall.S new file +7
-0+/* Helper function for standard type of TDVMCALL */ +SYM_FUNC_START(__tdvmcall) + /* Set TDVMCALL type info (0 - Standard, > 0 - vendor) in R10 */ + xor %r10, %r10 + call do_tdvmcall + retq +SYM_FUNC_END(__tdvmcall)Why do we need this helper? Why does it need to be in assembly?
Quoted 4 lines
Its simpler to do it in assembly. Also, grouping all register updates in the same file will make it easier for us to read or debug issues. Another
reason is, we also calldo_tdvmcall()from in/out instruction use case.
Sathya, I seem to have to reverse-engineer what you are doing for all this stuff. Your answers to my questions are almost entirely orthogonal to the things I really want to know. I guess I need to be more precise with the questions I'm asking. But, this is yet another case where I think the burden for this series continues to fall on the reviewer rather than the submitter. Not the way I think it is best.
So, trying to reverse-engineer what you are doing here... it seems that you can't *practically* call do_tdvmcall() directly because %r10 would be garbage. That makes this (or a wrapper like it) required for every practical call to do_tdvmcall().
But, even if that's the case, you need to *DOCUMENT* that up in do_tdvmcall(): Hey, this function is worthless without something that sets up %r10 before calling it.
I'm also not *SURE* this is simpler to do in assembly.
Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
arch/x86/kernel/tdx.c +0
-0@@ -1,8 +1,44 @@ // SPDX-License-Identifier: GPL-2.0 /* Copyright (C) 2020 Intel Corporation */+#define
pr_fmt(fmt)"TDX: " fmt
+Quoted 8 lines
Quoted 5 lines
#include <
asm/tdx.h>
+/*
+ * Wrapper for use case that checks for error code and print warning message.
+ */This comment isn't very useful. I can see the error check and warning by reading the code.
Quoted 3 lines
Its just a helper function that covers common case of checking for error and print the warning message. If this comment is superfluous, I can remove it.
I'd prefer that you actually write a comment about what the function is doing, maybe:
/*
* Wrapper for simple hypercalls that only return a success/error code. */
... or *SOMETHING* that tells what its purpose in life is.
Quoted 36 lines
Quoted 27 lines
Quoted 27 lines
arch/x86/kernel/tdx.c +15
-0+static inline u64 tdvmcall (u64 fn,u64 r12,u64 r13,u64 r14,u64 r15) +{ + u64 err; + + err = __tdvmcall(fn, r12, r13, r14, r15, NULL); + + if (err) + pr_warn_ratelimited("TDVMCALL fn:%llx failed with err:%llx\n", + fn, err); + + return err; +} + +/* +* Wrapper for the semi-common case where we need single output value (R11).
+ */
+static inline u64tdvmcall_out_r11(u64 fn, u64 r12, u64 r13, u64 r14, u64 r15)arch/x86/kernel/tdx.c +8
-0+{ + + struct tdvmcall_output out = {0}; + u64 err; + + err = __tdvmcall(fn, r12, r13, r14, r15, &out); + + if (err)Quoted 8 lines
arch/x86/kernel/tdx.c line 40
Quoted 5 lines
+
pr_warn_ratelimited("TDVMCALL fn:%llx failed with err:%llx\n", + fn, err);arch/x86/kernel/tdx.c +3
-0+ + return out.r11; +}How do callers check for errors? Is the error value superfluously returned in r11 and another output register?
Quoted 2 lines
We already check for error in this helper function. User of this function only cares about output value (R11). Mainly for in/out use case.
That's pretty valuable information.
Re: [RFC v2 05/32] x86/tdx: Add __tdcall() and __tdvmcall() helper functions
Quoted 8 lines · Dave Hansen
Quoted 7 lines · Sathyanarayanan
Quoted 6 lines
arch/x86/kernel/tdcall.S line 23
Quoted 4 lines
arch/x86/kernel/tdcall.S new file +4
-0+#define tdcall .byte 0x66,0x0f,0x01,0xcc + +/* + * __tdcall() - Used to communicate with the TDX moduleWhy is this function here? What does it do? Why do we need it?
Quoted 8 lines
Quoted 2 lines
__tdcall()function is used to request services from the TDX Module. Example use cases are, TDREPORT, VEINFO, TDINFO, etc.I think there might be some misinterpretation of my question. What you are describing is what *TDCALL* does. Why do we need a wrapper function? What purpose does this wrapper function serve? Why do we need this wrapper function?
How about following explanation?
Helper function for "tdcall" instruction, which can be used to request services from the TDX module (does not include VMM). Few examples of valid TDX module services are, "TDREPORT", "MEM PAGE ACCEPT", "VEINFO", etc.
This function serves as a wrapper to move user call arguments to the correct registers as specified by "tdcall" ABI and shares it with the TDX module. If the "tdcall" operation is successful and a valid "struct tdcall_out" pointer is available (in "out" argument), output from the TDX module (RCX, RDX, R8-R11) is saved to the memory specified in the "out" pointer. Also the status of the "tdcall" operation is returned back to the user as a function return value.
Quoted 14 lines
Quoted 11 lines
Quoted 6 lines
Why do we have to save these? Because they might be clobbered? If so, let's say *THAT* instead of just "exposed". "Exposed" could mean "VMM can read".
Also, this just told me that this function can't be used to talk to the VMM. Why is this talking about exposure to the VMM?
Although
__tdcall()is only used to communicate with the TDX module and the TDX module is not supposed to touch these registers, just to be on the safe side, I have tried to save the context of registers R12-R15. Anyway cycles used by instructions are less compared to tdcall.Why are you talking about the VMM if this is a call to the SEAM module?
Quoted 8 lines
Let's say someone is reading the TDCALL architecture spec. It will say something like, "blah blah, in this case TDCALL will not modify %r12->%r15". Then someone goes and looks at this code that basically says (or implies) "save these before the SEAM module modifies them". What is a coder to do?
Please remove the ambiguity, either by removing this superfluous (according to the spec) code, or documenting why it is not superfluous.
Agree. I will remove the save/restore context code.
Quoted 13 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
+ /* Move TDCALL Leaf ID to RAX */
+ mov %rdi, %rax
+ /* Move output pointer to R12 */
+ mov %r9, %r12Quoted 6 lines
Quoted 2 lines
I thought 'struct
tdcall_output' was a purely software construct. Why are we passing a pointer to it into TDCALL?Its used to store the TDCALL result (RCX, RDX, R8-R11). As far as this function is concerned, its just a block of memory (accessed using base address + TDCALL_r* offsets).
Quoted 9 lines
Is 'struct
tdcall_output' a hardware architectural structure or a software structure?If it's a software structure, then why are we passing a pointer to a software structure into a hardware ABI?
If it's a hardware architecture structure, where is the documentation for it?
I think there is a misunderstanding here. We don't share the tdcall_output pointer with the TDX module. Current use cases of TDCALL (other than TDVMCALL) do not use registers from R12-R15. Since the registers R12-R15 are free and available, we are using R12 as temporary storage to hold the tdcall_output pointer.
I will include some comment about using it as temporary storage.
Quoted 6 lines
I prefer that the code be understandable and be written for a clear purpose. If you're using r12 for temporary storage, I expect to see at least one reference *SOMEWHERE* to its use as temporary storage. Right now.... nothing.
I will include some reference to it.
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 15 lines
+ /* Copy TDCALL result registers to output struct: */ + movq %rcx,
TDCALL_rcx(%r12)
+ movq %rdx,TDCALL_rdx(%r12)
+ movq %r8,TDCALL_r8(%r12)
+ movq %r9,TDCALL_r9(%r12)
+ movq %r10,TDCALL_r10(%r12)
+ movq %r11,TDCALL_r11(%r12)
+1:
+ /* Zero out registers exposed to the TDX Module. */ + xor %rcx, %rcx
+ xor %rdx, %rdx
+ xor %r8d, %r8d
+ xor %r9d, %r9d
+ xor %r10d, %r10d
+ xor %r11d, %r11d
Quoted 9 lines
Quoted 4 lines
Quoted 1 line
... why?
These registers are used by the TDX Module. Why pass the stale values back to the user? So we clear them here.
Please go look at some other assembly code in the kernel called from C.
Do those functions do this? Why? Why not? Do they care about
"passing stale values back up"?
Maybe I am being overly cautious here. Since TDX module is the trusted code, speculation attack is not a consideration here. I will remove this block of code.
Quoted 33 lines
Quoted 14 lines
Quoted 5 lines
Quoted 5 lines
arch/x86/kernel/tdcall.S new file +5
-0+SYM_CODE_START_LOCAL(do_tdvmcall) + FRAME_BEGIN + + /* Save non-volatile GPRs that are exposed to the VMM. */ + push %r15Quoted 8 lines
Quoted 5 lines
+ push %r14
+ push %r13
+ push %r12
+
+ /* Set TDCALL leaf ID to TDVMCALL (0) in RAX */I think there needs to be some discussion of what TDCALL and TDVMCALL are. They are named too similarly not to do so.
Quoted 2 lines
TDVMCALL is the sub function of TDCALL (selected by setting RAX register to 0). TDVMCALL is used to request services from VMM.
Actually, I think these functions are horribly misnamed.
I think we should make them
__tdx_seam_call()or
__tdx_module_call()and
__tdx_hypercall() __tdcall() and __tdvmcall()
Quoted 3 lines
are really nonsensical in this context, especially since TDVMCALL is implemented with the TDCALL instruction, but not the
__tdcall()function.
TDVMCALL is a short form of "TDG.VP.VMCALL". This term usage came from GHCI document. We can read it as "Trusted Domain VMCALL". Maybe because we are used to GHCI spec, we don't find it confusing. I agree that if you consider the "tdcall" instruction usage, it is confusing.
But if it's confusing for new readers and rename is preferred,
Do we need to rename the helper functions ?
tdvmcall(), tdvmcall_out_r11()
Also what about output structs?
struct tdcall_output
struct tdvmcall_output
Quoted 15 lines
Quoted 10 lines
Quoted 9 lines
arch/x86/kernel/tdcall.S line 200
Quoted 7 lines
arch/x86/kernel/tdcall.S new file +7
-0+/* Helper function for standard type of TDVMCALL */ +SYM_FUNC_START(__tdvmcall) + /* Set TDVMCALL type info (0 - Standard, > 0 - vendor) in R10 */ + xor %r10, %r10 + call do_tdvmcall + retq +SYM_FUNC_END(__tdvmcall)Why do we need this helper? Why does it need to be in assembly?
Quoted 4 lines
Its simpler to do it in assembly. Also, grouping all register updates in the same file will make it easier for us to read or debug issues. Another
reason is, we also calldo_tdvmcall()from in/out instruction use case.
Quoted 6 lines
Sathya, I seem to have to reverse-engineer what you are doing for all this stuff. Your answers to my questions are almost entirely orthogonal to the things I really want to know. I guess I need to be more precise with the questions I'm asking. But, this is yet another case where I think the burden for this series continues to fall on the reviewer rather than the submitter. Not the way I think it is best.
I have assumed that you are aware of reason for the existence of do_tdvmcall() helper function. It is mainly created to hold common code between vendor specific and standard type of tdvmcall's.
But it is a mistake from my end. I will try to be elaborate in my future replies.
Quoted 9 lines
So, trying to reverse-engineer what you are doing here... it seems that you can't *practically* call
do_tdvmcall()directly because %r10 would be garbage. That makes this (or a wrapper like it) required for every practical call todo_tdvmcall().But, even if that's the case, you need to *DOCUMENT* that up in
do_tdvmcall(): Hey, this function is worthless without something that sets up %r10 before calling it.
Agree. This needs to be documented. I will add it in next version.
Quoted 25 lines
I'm also not *SURE* this is simpler to do in assembly.
Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
arch/x86/kernel/tdx.c +1
-0@@ -1,8 +1,44 @@ // SPDX-License-Identifier: GPL-2.0 /* Copyright (C) 2020 Intel Corporation */ +#define pr_fmt(fmt) "TDX: " fmt +Quoted 8 lines
Quoted 5 lines
arch/x86/kernel/tdx.c +1
-0#include <asm/tdx.h> +/* +* Wrapper for use case that checks for error code and print warning message.
+ */This comment isn't very useful. I can see the error check and warning by reading the code.
Quoted 3 lines
Its just a helper function that covers common case of checking for error and print the warning message. If this comment is superfluous, I can remove it.
Quoted 8 lines
I'd prefer that you actually write a comment about what the function is doing, maybe:
/*
* Wrapper for simple hypercalls that only return a success/error code. */... or *SOMETHING* that tells what its purpose in life is.
I will fix it in next version.
Quoted 37 lines
Quoted 37 lines
Quoted 36 lines
Quoted 23 lines
arch/x86/kernel/tdx.c +15
-0+static inline u64 tdvmcall (u64 fn,u64 r12,u64 r13,u64 r14,u64 r15) +{ + u64 err; + + err = __tdvmcall(fn, r12, r13, r14, r15, NULL); + + if (err) + pr_warn_ratelimited("TDVMCALL fn:%llx failed with err:%llx\n", + fn, err); + + return err; +} + +/* +* Wrapper for the semi-common case where we need single output value (R11).
+ */
+static inline u64tdvmcall_out_r11(u64 fn, u64 r12, u64 r13, u64 r14, u64 r15)arch/x86/kernel/tdx.c +3
-0+{ + + struct tdvmcall_output out = {0};arch/x86/kernel/tdx.c line 40
Quoted 10 lines
arch/x86/kernel/tdx.c +10
-0+ u64 err; + + err = __tdvmcall(fn, r12, r13, r14, r15, &out); + + if (err) + pr_warn_ratelimited("TDVMCALL fn:%llx failed with err:%llx\n", + fn, err); + + return out.r11; +}How do callers check for errors? Is the error value superfluously returned in r11 and another output register?
Quoted 4 lines
Quoted 2 lines
We already check for error in this helper function. User of this function only cares about output value (R11). Mainly for in/out use case.
That's pretty valuable information.
I will include this note in the function comment.
Quoted 1 line
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Re: [RFC v2 05/32] x86/tdx: Add __tdcall() and __tdvmcall() helper functions
Quoted 17 lines · Sathyanarayanan
Quoted 8 lines · Dave Hansen
Quoted 7 lines · Sathyanarayanan
Quoted 6 lines
arch/x86/kernel/tdcall.S line 23
Quoted 4 lines
arch/x86/kernel/tdcall.S new file +4
-0+#define tdcall .byte 0x66,0x0f,0x01,0xcc + +/* + * __tdcall() - Used to communicate with the TDX moduleWhy is this function here? What does it do? Why do we need it?
Quoted 8 lines
Quoted 2 lines
__tdcall()function is used to request services from the TDX Module. Example use cases are, TDREPORT, VEINFO, TDINFO, etc.I think there might be some misinterpretation of my question. What you are describing is what *TDCALL* does. Why do we need a wrapper function? What purpose does this wrapper function serve? Why do we need this wrapper function?
Quoted 6 lines
How about following explanation?
Helper function for "tdcall" instruction, which can be used to request services from the TDX module (does not include VMM). Few examples of valid TDX module services are, "TDREPORT", "MEM PAGE ACCEPT", "VEINFO", etc.
Naming the services here is not useful. If I want to know who calls this, I'll just literally do that: look up the callers of this function.
Quoted 7 lines
This function serves as a wrapper to move user call arguments to the correct registers as specified by "tdcall" ABI and shares it with the TDX module. If the "tdcall" operation is successful and a valid "struct
tdcall_out" pointer is available (in "out" argument), output from the TDX module (RCX, RDX, R8-R11) is saved to the memory specified in the "out" pointer. Also the status of the "tdcall" operation is returned back to the user as a function return value.
I tend to prefer function comments that talk high-level about what the function does rather than waste space on the exact registers used in the ABI. I also tend not to talk about things that can be trivially grepped for, like the callers of this function.
I'd trim the fat out of there, but it's generally OK, although too rotund for my taste.
Quoted 22 lines
Quoted 12 lines
Quoted 11 lines
Quoted 7 lines
Quoted 4 lines
+ /* Move TDCALL Leaf ID to RAX */
+ mov %rdi, %rax
+ /* Move output pointer to R12 */
+ mov %r9, %r12I thought 'struct
tdcall_output' was a purely software construct. Why are we passing a pointer to it into TDCALL?Its used to store the TDCALL result (RCX, RDX, R8-R11). As far as this function is concerned, its just a block of memory (accessed using base address + TDCALL_r* offsets).
Quoted 9 lines
Is 'struct
tdcall_output' a hardware architectural structure or a software structure?If it's a software structure, then why are we passing a pointer to a software structure into a hardware ABI?
If it's a hardware architecture structure, where is the documentation for it?
Quoted 6 lines
I think there is a misunderstanding here. We don't share the
tdcall_outputpointer with the TDX module. Current use cases of TDCALL (other than TDVMCALL)
do not use registers from R12-R15. Since the registers R12-R15 are free and available, we are using R12 as temporary storage to hold thetdcall_outputpointer.
In other words, 'struct tdcall_output' is a purely software concept. However, its pointer is manipulated literally next to all of the TDCALL register arguments and it has an *IDENTICAL* comment to all of those other moves.
Please make it clear that %r12 is not being used at all for the TDCALL instruction itself.
But, the bigger point here is that the code needs to be structured in a way that makes the function and interactions clear. If I want to know more about the "output pointer", where do I go? Do I go looking at the calling functions or the TDINFO instruction reference?
...
Quoted 7 lines
Quoted 3 lines
Please go look at some other assembly code in the kernel called from C. Do those functions do this? Why? Why not? Do they care about "passing stale values back up"?
Maybe I am being overly cautious here. Since TDX module is the trusted code, speculation attack is not a consideration here. I will remove this block of code.
Caution is OK. Caution without explanation somewhere as to why it is warranted is not.
Quoted 3 lines
Do we need to rename the helper functions ?
tdvmcall(),tdvmcall_out_r11()
Yes.
Quoted 4 lines
Also what about output structs?
struct
tdcall_output
structtdvmcall_output
Yes, they need sane, straightforward names which are not confusing too.
Quoted 22 lines
Quoted 15 lines
Quoted 10 lines
Quoted 9 lines
arch/x86/kernel/tdcall.S line 200
Quoted 7 lines
arch/x86/kernel/tdcall.S new file +7
-0+/* Helper function for standard type of TDVMCALL */ +SYM_FUNC_START(__tdvmcall) + /* Set TDVMCALL type info (0 - Standard, > 0 - vendor) in R10 */ + xor %r10, %r10 + call do_tdvmcall + retq +SYM_FUNC_END(__tdvmcall)Why do we need this helper? Why does it need to be in assembly?
Quoted 4 lines
Its simpler to do it in assembly. Also, grouping all register updates in the same file will make it easier for us to read or debug issues. Another
reason is, we also calldo_tdvmcall()from in/out instruction use case.Quoted 6 lines
Sathya, I seem to have to reverse-engineer what you are doing for all this stuff. Your answers to my questions are almost entirely orthogonal to the things I really want to know. I guess I need to be more precise with the questions I'm asking. But, this is yet another case where I think the burden for this series continues to fall on the reviewer rather than the submitter. Not the way I think it is best.
Quoted 3 lines
I have assumed that you are aware of reason for the existence of
do_tdvmcall()helper function. It is mainly created to hold common code between vendor specific and standard type of tdvmcall's.
No, I was not aware of that. Remember, you're not doing this for *ME*.
You're doing it for the hundred other people that are going to look
over the code and who won't have been aware of your reasoning.
Re: [RFC v2 05/32] x86/tdx: Add __tdcall() and __tdvmcall() helper functions
Hi Dave,
Quoted 11 lines · Dave Hansen
Quoted 3 lines
Do we need to rename the helper functions ?
tdvmcall(),tdvmcall_out_r11()Yes.
Quoted 4 lines
Also what about output structs?
struct
tdcall_output
structtdvmcall_outputYes, they need sane, straightforward names which are not confusing too.
Following is the rename diff. Please let me know if you agree with the names used.
arch/x86/include/asm/tdx.h +0 -0
-struct tdcall_output {
+struct tdx_module_output {
u64 rcx;
u64 rdx;
u64 r8;
@@ -19,7 +19,7 @@ struct tdcall_output {
u64 r11;};
-struct tdvmcall_output {
+struct tdx_hypercall_output {
u64 r11;
u64 r12;
u64 r13;@@ -33,12 +33,12 @@ bool is_tdx_guest(void);
void __init tdx_early_init(void);
/* Helper function used to communicate with the TDX module */
-u64 __tdcall(u64 fn, u64 rcx, u64 rdx, u64 r8, u64 r9,
- struct tdcall_output *out);
+u64 __tdx_module_call(u64 fn, u64 rcx, u64 rdx, u64 r8, u64 r9,
+ struct tdx_module_output *out);
/* Helper function used to request services from VMM */
-u64 __tdvmcall(u64 fn, u64 r12, u64 r13, u64 r14, u64 r15,
- struct tdvmcall_output *out);
+u64 __tdx_hypercall(u64 fn, u64 r12, u64 r13, u64 r14, u64 r15,
+ struct tdx_hypercall_output *out);
arch/x86/kernel/tdx.c +4 -4
@@ -8,11 +8,11 @@/* * Wrapper for use case that checks for error code and print warning message. */ -static inline u64 tdvmcall (u64 fn,u64 r12,u64 r13,u64 r14,u64 r15) +static inline u64 tdx_hypercall (u64 fn,u64 r12,u64 r13,u64 r14,u64 r15) {u64 err; - err =__tdvmcall (fn, r12, r13, r14, r15,NULL ); + err =__tdx_hypercall (fn, r12, r13, r14, r15,NULL );if (err) pr_warn_ratelimited("TDVMCALL fn:%llx failed with err:%llx\n",
@@ -24,13 +24,14 @@ static inline u64 tdvmcall(u64 fn, u64 r12, u64 r13, u64 r14, u64 r15)/* * Wrapper for the semi-common case where we need single output value (R11). */ -static inline u64 tdvmcall_out_r11 (u64 fn,u64 r12,u64 r13,u64 r14,u64 r15) +static inline u64 tdx_hypercall_out_r11 (u64 fn,u64 r12,u64 r13, +u64 r14,u64 r15) --
Sathyanarayanan Kuppuswamy
Linux Kernel Developer
Re: [RFC v2 05/32] x86/tdx: Add __tdcall() and __tdvmcall() helper functions
Quoted 2 lines · Sathyanarayanan
Following is the rename diff. Please let me know if you agree with the names used.
Look fine at a glance, but the real key is how they look when they get used.
PATCH 1/1 x86/tdx: Add __tdx_module_call() and __tdx_hypercall() helper functions
Guests communicate with VMMs with hypercalls. Historically, these are implemented using instructions that are known to cause VMEXITs like vmcall, vmlaunch, etc. However, with TDX, VMEXITs no longer expose guest state to the host. This prevents the old hypercall mechanisms from working. So to communicate with VMM, TDX specification defines a new instruction called "tdcall".
In TDX based VM, since VMM is an untrusted entity, a intermediary layer (TDX module) exists between host and guest to facilitate the secure communication. And "tdcall" instruction is used by the guest to request services from TDX module. And a variant of "tdcall" instruction (with specific arguments as defined by GHCI) is used by the guest to request services from VMM via the TDX module.
Implement common helper functions to communicate with the TDX Module and VMM (using TDCALL instruction).__tdx_hypercall() - function can be used to request services from
the VMM.
__tdx_module_call() - function can be used to communicate with the
TDX Module.
Also define two additional wrappers, tdx_hypercall() and tdx_hypercall_out_r11() to cover common use cases of __tdx_hypercall() function. Since each use case of __tdx_module_call() is different, we don't need such wrappers for it.
Implement __tdx_module_call() and __tdx_hypercall() helper functions in assembly.
Rationale behind choosing to use assembly over inline assembly are,
1. Since the number of lines of instructions (with comments) in __tdx_hypercall() implementation is over 70, using inline assembly to implement it will make it hard to read.
2. Also, since many registers (R8-R15, R[A-D]X)) will be used in TDCALL operation, if all these registers are included in in-line assembly constraints, some of the older compilers may not be able to meet this requirement.
Also, just like syscalls, not all TDVMCALL/TDCALLs use cases need to use the same set of argument registers. The implementation here picks the current worst-case scenario for TDCALL (4 registers). For TDCALLs with fewer than 4 arguments, there will end up being a few superfluous (cheap) instructions. But, this approach maximizes code reuse. The same argument applies to __tdx_hypercall() function as well.
Current implementation of __tdx_hypercall() includes error handling (ud2 on failure case) in assembly function instead of doing it in C wrapper function. The reason behind this choice is, when adding support for in/out instructions (refer to patch titled "x86/tdx: Handle port I/O" in this series), we use alternative_io() to substitute in/out instruction with __tdx_hypercall() calls. So use of C wrappers is not trivial in this case because the input parameters will be in the wrong registers and it's tricky to include proper buffer code to make this happen.
For registers used by TDCALL instruction, please check TDX GHCI specification, sec 2.4 and 3.
---
Hi Dave,
It includes all fixes suggested by you. Please let me know your comments.
Changes since v1:
* Renamed __tdcall()/__tdvmcall() to __tdx_module_call()/__tdx_hypercall().
* Used BIT() to derive TDVMCALL_EXPOSE_REGS_MASK.
* Removed unnecessary code in __tdcall() function.
* Fixed comments as per Dave's review.
arch/x86/include/asm/tdx.h | 26 ++++ arch/x86/kernel/Makefile | 2 +- arch/x86/kernel/asm-offsets.c | 22 ++++ arch/x86/kernel/tdcall.S | 215 ++++++++++++++++++++++++++++++++++ arch/x86/kernel/tdx.c | 39 ++++++ 5 files changed, 303 insertions(+), 1 deletion(-) create mode 100644 arch/x86/kernel/tdcall.S
arch/x86/include/asm/tdx.h +26 -0
@@ -8,12 +8,38 @@#ifdef CONFIG_INTEL_TDX_GUEST #include <asm/cpufeature.h> +#include <linux/types.h> + +struct tdx_module_output { +u64 rcx; +u64 rdx; +u64 r8; +u64 r9; +u64 r10; +u64 r11; +}; + +struct tdx_hypercall_output { +u64 r11; +u64 r12; +u64 r13; +u64 r14; +u64 r15; +};/* Common API to check TDX support in decompression and common kernel code. */ bool is_tdx_guest (void );void __init tdx_early_init (void ); +/* Helper function used to communicate with the TDX module */ +u64 __tdx_module_call (u64 fn,u64 rcx,u64 rdx,u64 r8,u64 r9, +struct tdx_module_output *out); + +/* Helper function used to request services from VMM */ +u64 __tdx_hypercall (u64 fn,u64 r12,u64 r13,u64 r14,u64 r15, +struct tdx_hypercall_output *out); +#else // !CONFIG_INTEL_TDX_GUEST static inline bool is_tdx_guest (void )
arch/x86/kernel/Makefile +1 -1
@@ -127,7 +127,7 @@ obj-$(CONFIG_PARAVIRT_CLOCK) += pvclock.o obj-$(CONFIG_X86_PMEM_LEGACY_DEVICE) += pmem.o obj-$(CONFIG_JAILHOUSE_GUEST) += jailhouse.o -obj-$(CONFIG_INTEL_TDX_GUEST) += tdx.o +obj-$(CONFIG_INTEL_TDX_GUEST) += tdcall.o tdx.o obj-$(CONFIG_EISA) += eisa.o obj-$(CONFIG_PCSPKR_PLATFORM) += pcspeaker.o
arch/x86/kernel/asm-offsets.c +22 -0
@@ -23,6 +23,10 @@#include <xen/interface/xen.h> #endif +#ifdef CONFIG_INTEL_TDX_GUEST +#include <asm/tdx.h> +#endif +#ifdef CONFIG_X86_32 # include "asm-offsets_32.c" #else
@@ -75,6 +79,24 @@ static void __used common(void)OFFSET (XEN_vcpu_info_arch_cr2, vcpu_info, arch.cr2);#endif +#ifdef CONFIG_INTEL_TDX_GUEST +BLANK (); +/* Offset for fields in tdcall_output */ +OFFSET (TDX_MODULE_rcx, tdx_module_output, rcx); +OFFSET (TDX_MODULE_rdx, tdx_module_output, rdx); +OFFSET (TDX_MODULE_r8, tdx_module_output, r8); +OFFSET (TDX_MODULE_r9, tdx_module_output, r9); +OFFSET (TDX_MODULE_r10, tdx_module_output, r10); +OFFSET (TDX_MODULE_r11, tdx_module_output, r11); + +/* Offset for fields in tdvmcall_output */ +OFFSET (TDX_HYPERCALL_r11, tdx_hypercall_output, r11); +OFFSET (TDX_HYPERCALL_r12, tdx_hypercall_output, r12); +OFFSET (TDX_HYPERCALL_r13, tdx_hypercall_output, r13); +OFFSET (TDX_HYPERCALL_r14, tdx_hypercall_output, r14); +OFFSET (TDX_HYPERCALL_r15, tdx_hypercall_output, r15); +#endif +BLANK ();OFFSET (BP_scratch, boot_params, scratch);OFFSET (BP_secure_boot, boot_params, secure_boot);
arch/x86/kernel/tdcall.S new file +215 -0
215 lines not on this page · raw message
arch/x86/kernel/tdx.c +39 -0
@@ -1,8 +1,47 @@// SPDX-License-Identifier: GPL-2.0 /* Copyright (C) 2020 Intel Corporation */ +#define pr_fmt (fmt) "TDX: " fmt +#include <asm/tdx.h> +/* +* Wrapper for simple hypercalls that only return a success/error code. +*/ +static inline u64 tdx_hypercall (u64 fn,u64 r12,u64 r13,u64 r14,u64 r15) +{ +u64 err; + + err =__tdx_hypercall (fn, r12, r13, r14, r15,NULL ); + +if (err) +pr_warn_ratelimited ("TDVMCALL fn:%llx failed with err:%llx\n" , + fn, err); + +return err; +} + +/* +* Wrapper for the semi-common case where we need single output +* value (R11). Callers of this function does not care about the +* hypercall error code (mainly for IN or MMIO usecase). +*/ +static inline u64 tdx_hypercall_out_r11 (u64 fn,u64 r12,u64 r13, +u64 r14,u64 r15) +{ + +struct tdx_hypercall_output out = {0 }; +u64 err; + + err =__tdx_hypercall (fn, r12, r13, r14, r15, &out); + +if (err) +pr_warn_ratelimited ("TDVMCALL fn:%llx failed with err:%llx\n" , + fn, err); + +return out.r11; +} +static inline bool cpuid_has_tdx_guest (void ) {u32 eax, signature[3 ];
Signature · 1 line
2.25.1
RFC 1/1 x86/tdx: Add __tdx_module_call() and __tdx_hypercall() helper functions
In reply to Dave HansenGuests communicate with VMMs with hypercalls. Historically, these are implemented using instructions that are known to cause VMEXITs like vmcall, vmlaunch, etc. However, with TDX, VMEXITs no longer expose guest state to the host. This prevents the old hypercall mechanisms from working. So to communicate with VMM, TDX specification defines a new instruction called "tdcall".
In TDX based VM, since VMM is an untrusted entity, a intermediary layer (TDX module) exists between host and guest to facilitate the secure communication. And "tdcall" instruction is used by the guest to request services from TDX module. And a variant of "tdcall" instruction (with specific arguments as defined by GHCI) is used by the guest to request services from VMM via the TDX module.
Implement common helper functions to communicate with the TDX Module and VMM (using TDCALL instruction).__tdx_hypercall() - function can be used to request services from
the VMM.
__tdx_module_call() - function can be used to communicate with the
TDX Module.
Also define two additional wrappers, tdx_hypercall() and tdx_hypercall_out_r11() to cover common use cases of __tdx_hypercall() function. Since each use case of __tdx_module_call() is different, we don't need such wrappers for it.
Implement __tdx_module_call() and __tdx_hypercall() helper functions in assembly.
Rationale behind choosing to use assembly over inline assembly are,
1. Since the number of lines of instructions (with comments) in __tdx_hypercall() implementation is over 70, using inline assembly to implement it will make it hard to read.
2. Also, since many registers (R8-R15, R[A-D]X)) will be used in TDCALL operation, if all these registers are included in in-line assembly constraints, some of the older compilers may not be able to meet this requirement.
Also, just like syscalls, not all TDVMCALL/TDCALLs use cases need to use the same set of argument registers. The implementation here picks the current worst-case scenario for TDCALL (4 registers). For TDCALLs with fewer than 4 arguments, there will end up being a few superfluous (cheap) instructions. But, this approach maximizes code reuse. The same argument applies to __tdx_hypercall() function as well.
Current implementation of __tdx_hypercall() includes error handling (ud2 on failure case) in assembly function instead of doing it in C wrapper function. The reason behind this choice is, when adding support for in/out instructions (refer to patch titled "x86/tdx: Handle port I/O" in this series), we use alternative_io() to substitute in/out instruction with __tdx_hypercall() calls. So use of C wrappers is not trivial in this case because the input parameters will be in the wrong registers and it's tricky to include proper buffer code to make this happen.
For registers used by TDCALL instruction, please check TDX GHCI specification, sec 2.4 and 3.
---
Changes since RFC v2:
* Renamed __tdcall()/__tdvmcall() to __tdx_module_call()/__tdx_hypercall().
* Renamed reg offsets from TDCALL_rx to TDX_MODULE_rx.
* Renamed reg offsets from TDVMCALL_rx to TDX_HYPERCALL_rx.
* Renamed struct tdcall_output to struct tdx_module_output.
* Renamed struct tdvmcall_output to struct tdx_hypercall_output.
* Used BIT() to derive TDVMCALL_EXPOSE_REGS_MASK.
* Removed unnecessary push/pop sequence in __tdcall() function.
* Fixed comments as per Dave's review.
arch/x86/include/asm/tdx.h | 38 ++++++ arch/x86/kernel/Makefile | 2 +- arch/x86/kernel/asm-offsets.c | 22 ++++ arch/x86/kernel/tdcall.S | 222 ++++++++++++++++++++++++++++++++++ arch/x86/kernel/tdx.c | 39 ++++++ 5 files changed, 322 insertions(+), 1 deletion(-) create mode 100644 arch/x86/kernel/tdcall.S
arch/x86/include/asm/tdx.h +38 -0
@@ -8,12 +8,50 @@#ifdef CONFIG_INTEL_TDX_GUEST #include <asm/cpufeature.h> +#include <linux/types.h> + +/* +* Used in __tdx_module_call() helper function to gather the +* output registers values of TDCALL instruction when requesting +* services from the TDX module. This is software only structure +* and not related to TDX module/VMM. +*/ +struct tdx_module_output { +u64 rcx; +u64 rdx; +u64 r8; +u64 r9; +u64 r10; +u64 r11; +}; + +/* +* Used in __tdx_hypercall() helper function to gather the +* output registers values of TDCALL instruction when requesting +* services from the VMM. This is software only structure +* and not related to TDX module/VMM. +*/ +struct tdx_hypercall_output { +u64 r11; +u64 r12; +u64 r13; +u64 r14; +u64 r15; +};/* Common API to check TDX support in decompression and common kernel code. */ bool is_tdx_guest (void );void __init tdx_early_init (void ); +/* Helper function used to communicate with the TDX module */ +u64 __tdx_module_call (u64 fn,u64 rcx,u64 rdx,u64 r8,u64 r9, +struct tdx_module_output *out); + +/* Helper function used to request services from VMM */ +u64 __tdx_hypercall (u64 fn,u64 r12,u64 r13,u64 r14,u64 r15, +struct tdx_hypercall_output *out); +#else // !CONFIG_INTEL_TDX_GUEST static inline bool is_tdx_guest (void )
arch/x86/kernel/Makefile +1 -1
@@ -127,7 +127,7 @@ obj-$(CONFIG_PARAVIRT_CLOCK) += pvclock.o obj-$(CONFIG_X86_PMEM_LEGACY_DEVICE) += pmem.o obj-$(CONFIG_JAILHOUSE_GUEST) += jailhouse.o -obj-$(CONFIG_INTEL_TDX_GUEST) += tdx.o +obj-$(CONFIG_INTEL_TDX_GUEST) += tdcall.o tdx.o obj-$(CONFIG_EISA) += eisa.o obj-$(CONFIG_PCSPKR_PLATFORM) += pcspeaker.o
arch/x86/kernel/asm-offsets.c +22 -0
@@ -23,6 +23,10 @@#include <xen/interface/xen.h> #endif +#ifdef CONFIG_INTEL_TDX_GUEST +#include <asm/tdx.h> +#endif +#ifdef CONFIG_X86_32 # include "asm-offsets_32.c" #else
@@ -75,6 +79,24 @@ static void __used common(void)OFFSET (XEN_vcpu_info_arch_cr2, vcpu_info, arch.cr2);#endif +#ifdef CONFIG_INTEL_TDX_GUEST +BLANK (); +/* Offset for fields in tdcall_output */ +OFFSET (TDX_MODULE_rcx, tdx_module_output, rcx); +OFFSET (TDX_MODULE_rdx, tdx_module_output, rdx); +OFFSET (TDX_MODULE_r8, tdx_module_output, r8); +OFFSET (TDX_MODULE_r9, tdx_module_output, r9); +OFFSET (TDX_MODULE_r10, tdx_module_output, r10); +OFFSET (TDX_MODULE_r11, tdx_module_output, r11); + +/* Offset for fields in tdvmcall_output */ +OFFSET (TDX_HYPERCALL_r11, tdx_hypercall_output, r11); +OFFSET (TDX_HYPERCALL_r12, tdx_hypercall_output, r12); +OFFSET (TDX_HYPERCALL_r13, tdx_hypercall_output, r13); +OFFSET (TDX_HYPERCALL_r14, tdx_hypercall_output, r14); +OFFSET (TDX_HYPERCALL_r15, tdx_hypercall_output, r15); +#endif +BLANK ();OFFSET (BP_scratch, boot_params, scratch);OFFSET (BP_secure_boot, boot_params, secure_boot);
arch/x86/kernel/tdcall.S new file +222 -0
222 lines not on this page · raw message
arch/x86/kernel/tdx.c +39 -0
@@ -1,8 +1,47 @@// SPDX-License-Identifier: GPL-2.0 /* Copyright (C) 2020 Intel Corporation */ +#define pr_fmt (fmt) "TDX: " fmt +#include <asm/tdx.h> +/* +* Wrapper for simple hypercalls that only return a success/error code. +*/ +static inline u64 tdx_hypercall (u64 fn,u64 r12,u64 r13,u64 r14,u64 r15) +{ +u64 err; + + err =__tdx_hypercall (fn, r12, r13, r14, r15,NULL ); + +if (err) +pr_warn_ratelimited ("TDVMCALL fn:%llx failed with err:%llx\n" , + fn, err); + +return err; +} + +/* +* Wrapper for the semi-common case where we need single output +* value (R11). Callers of this function does not care about the +* hypercall error code (mainly for IN or MMIO usecase). +*/ +static inline u64 tdx_hypercall_out_r11 (u64 fn,u64 r12,u64 r13, +u64 r14,u64 r15) +{ + +struct tdx_hypercall_output out = {0 }; +u64 err; + + err =__tdx_hypercall (fn, r12, r13, r14, r15, &out); + +if (err) +pr_warn_ratelimited ("TDVMCALL fn:%llx failed with err:%llx\n" , + fn, err); + +return out.r11; +} +static inline bool cpuid_has_tdx_guest (void ) {u32 eax, signature[3 ];
Signature · 1 line
2.25.1
Re: [RFC v2-fix-v1 1/1] x86/tdx: Add __tdx_module_call() and __tdx_hypercall() helper functions
Hi Dave,
Quoted 58 lines · Kuppuswamy Sathyanarayanan
Guests communicate with VMMs with hypercalls. Historically, these are implemented using instructions that are known to cause VMEXITs like vmcall, vmlaunch, etc. However, with TDX, VMEXITs no longer expose guest state to the host. This prevents the old hypercall mechanisms from working. So to communicate with VMM, TDX specification defines a new instruction called "tdcall".
In TDX based VM, since VMM is an untrusted entity, a intermediary layer (TDX module) exists between host and guest to facilitate the secure communication. And "tdcall" instruction is used by the guest to request services from TDX module. And a variant of "tdcall" instruction (with specific arguments as defined by GHCI) is used by the guest to request services from VMM via the TDX module.
Implement common helper functions to communicate with the TDX Module and VMM (using TDCALL instruction).
__tdx_hypercall()- function can be used to request services from
the VMM.
__tdx_module_call() - function can be used to communicate with the
TDX Module.Also define two additional wrappers,
tdx_hypercall()andtdx_hypercall_out_r11()to cover common use cases of__tdx_hypercall()function. Since each use case of__tdx_module_call()is different, we don't need such wrappers for it.Implement
__tdx_module_call()and__tdx_hypercall()helper functions in assembly.Rationale behind choosing to use assembly over inline assembly are,
1. Since the number of lines of instructions (with comments) in
__tdx_hypercall()implementation is over 70, using inline assembly to implement it will make it hard to read.2. Also, since many registers (R8-R15, R[A-D]X)) will be used in TDCALL operation, if all these registers are included in in-line assembly constraints, some of the older compilers may not be able to meet this requirement.
Also, just like syscalls, not all TDVMCALL/TDCALLs use cases need to use the same set of argument registers. The implementation here picks the current worst-case scenario for TDCALL (4 registers). For TDCALLs with fewer than 4 arguments, there will end up being a few superfluous (cheap) instructions. But, this approach maximizes code reuse. The same argument applies to __tdx_hypercall() function as well.
Current implementation of __tdx_hypercall() includes error handling (ud2 on failure case) in assembly function instead of doing it in C wrapper function. The reason behind this choice is, when adding support for in/out instructions (refer to patch titled "x86/tdx: Handle port I/O" in this series), we use
alternative_io()to substitute in/out instruction with__tdx_hypercall()calls. So use of C wrappers is not trivial in this case because the input parameters will be in the wrong registers and it's tricky to include proper buffer code to make this happen.
Quoted 7 lines
For registers used by TDCALL instruction, please check TDX GHCI specification, sec 2.4 and 3.
Originally-by Sean Christopherson<[email protected]>Signed-off-by Kuppuswamy Sathyanarayanan<[email protected]>
I did send it as in-reply-to message id [email protected] (your last reply mail id), but for some reason its not detected as reply to original patch "[RFC v2 05/32] x86/tdx: Add __tdcall() and __tdvmcall() helper functions".
I am not sure whats going on, but please review as reply to original patch.
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Re: [RFC v2-fix-v1 1/1] x86/tdx: Add __tdx_module_call() and __tdx_hypercall() helper functions
In reply to Kuppuswamy SathyanarayananQuoted 8 lines · Kuppuswamy Sathyanarayanan
Guests communicate with VMMs with hypercalls. Historically, these are implemented using instructions that are known to cause VMEXITs like vmcall, vmlaunch, etc. However, with TDX, VMEXITs no longer expose guest state to the host. This prevents the old hypercall mechanisms from working. So to communicate with VMM, TDX specification defines a new instruction called "tdcall".
In TDX based VM, since VMM is an untrusted entity, a intermediary
"In a TDX-based VM..."
Quoted 5 lines
layer (TDX module) exists between host and guest to facilitate the secure communication. And "tdcall" instruction is used by the guest to request services from TDX module. And a variant of "tdcall" instruction (with specific arguments as defined by GHCI) is used by the guest to request services from VMM via the TDX module.
I'd just say:
TDX guests communicate with the TDX module and with the VMM using a new instruction: TDCALL.
The rest of that is noise.
Quoted 7 lines
Implement common helper functions to communicate with the TDX Module and VMM (using TDCALL instruction).
__tdx_hypercall()- function can be used to request services from
the VMM.
__tdx_module_call() - function can be used to communicate with the
TDX Module.
s/function can be used to//
Quoted 10 lines
Also define two additional wrappers,
tdx_hypercall()andtdx_hypercall_out_r11()to cover common use cases of__tdx_hypercall()function. Since each use case of__tdx_module_call()is different, we don't need such wrappers for it.Implement
__tdx_module_call()and__tdx_hypercall()helper functions in assembly.Rationale behind choosing to use assembly over inline assembly are,
Quoted 8 lines
1. Since the number of lines of instructions (with comments) in
__tdx_hypercall()implementation is over 70, using inline assembly to implement it will make it hard to read.
2. Also, since many registers (R8-R15, R[A-D]X)) will be used in TDCALL operation, if all these registers are included in in-line assembly constraints, some of the older compilers may not be able to meet this requirement.
Was this "older compiler" argument really the reason?
Quoted 17 lines
Also, just like syscalls, not all TDVMCALL/TDCALLs use cases need to use the same set of argument registers. The implementation here picks the current worst-case scenario for TDCALL (4 registers). For TDCALLs with fewer than 4 arguments, there will end up being a few superfluous (cheap) instructions. But, this approach maximizes code reuse. The same argument applies to __tdx_hypercall() function as well.
Current implementation of __tdx_hypercall() includes error handling (ud2 on failure case) in assembly function instead of doing it in C wrapper function. The reason behind this choice is, when adding support for in/out instructions (refer to patch titled "x86/tdx: Handle port I/O" in this series), we use
alternative_io()to substitute in/out instruction with__tdx_hypercall()calls. So use of C wrappers is not trivial in this case because the input parameters will be in the wrong registers and it's tricky to include proper buffer code to make this happen.
Quoted 7 lines
For registers used by TDCALL instruction, please check TDX GHCI specification, sec 2.4 and 3.
Originally-by Sean Christopherson <[email protected]>Signed-off-by Kuppuswamy Sathyanarayanan <[email protected]>
For what it's worth, that changelog really starts to ramble after the "rationale" part.
arch/x86/include/asm/tdx.h line 15
Quoted 13 lines
arch/x86/include/asm/tdx.h +5
-0@@ -8,12 +8,50 @@#ifdef CONFIG_INTEL_TDX_GUEST #include <asm/cpufeature.h> +#include <linux/types.h> + +/* +* Used in __tdx_module_call() helper function to gather the +* output registers values of TDCALL instruction when requesting
There's something wrong in this sentence. This needs to be "output register values" or "output regisers' values".
Quoted 132 lines
arch/x86/include/asm/tdx.h +33
-0+* services from the TDX module. This is software only structure +* and not related to TDX module/VMM. +*/ +struct tdx_module_output { +u64 rcx; +u64 rdx; +u64 r8; +u64 r9; +u64 r10; +u64 r11; +}; + +/* +* Used in __tdx_hypercall() helper function to gather the +* output registers values of TDCALL instruction when requesting +* services from the VMM. This is software only structure +* and not related to TDX module/VMM. +*/ +struct tdx_hypercall_output { +u64 r11; +u64 r12; +u64 r13; +u64 r14; +u64 r15; +};/* Common API to check TDX support in decompression and common kernel code. */ bool is_tdx_guest (void );void __init tdx_early_init (void ); +/* Helper function used to communicate with the TDX module */ +u64 __tdx_module_call (u64 fn,u64 rcx,u64 rdx,u64 r8,u64 r9, +struct tdx_module_output *out); + +/* Helper function used to request services from VMM */ +u64 __tdx_hypercall (u64 fn,u64 r12,u64 r13,u64 r14,u64 r15, +struct tdx_hypercall_output *out); +#else // !CONFIG_INTEL_TDX_GUEST static inline bool is_tdx_guest (void )arch/x86/kernel/Makefile +1
-1@@ -127,7 +127,7 @@ obj-$(CONFIG_PARAVIRT_CLOCK) += pvclock.o obj-$(CONFIG_X86_PMEM_LEGACY_DEVICE) += pmem.o obj-$(CONFIG_JAILHOUSE_GUEST) += jailhouse.o -obj-$(CONFIG_INTEL_TDX_GUEST) += tdx.o +obj-$(CONFIG_INTEL_TDX_GUEST) += tdcall.o tdx.o obj-$(CONFIG_EISA) += eisa.o obj-$(CONFIG_PCSPKR_PLATFORM) += pcspeaker.oarch/x86/kernel/asm-offsets.c +22
-0@@ -23,6 +23,10 @@#include <xen/interface/xen.h> #endif +#ifdef CONFIG_INTEL_TDX_GUEST +#include <asm/tdx.h> +#endif +#ifdef CONFIG_X86_32 # include "asm-offsets_32.c" #else @@ -75,6 +79,24 @@ static void __used common(void)OFFSET (XEN_vcpu_info_arch_cr2, vcpu_info, arch.cr2);#endif +#ifdef CONFIG_INTEL_TDX_GUEST +BLANK (); +/* Offset for fields in tdcall_output */ +OFFSET (TDX_MODULE_rcx, tdx_module_output, rcx); +OFFSET (TDX_MODULE_rdx, tdx_module_output, rdx); +OFFSET (TDX_MODULE_r8, tdx_module_output, r8); +OFFSET (TDX_MODULE_r9, tdx_module_output, r9); +OFFSET (TDX_MODULE_r10, tdx_module_output, r10); +OFFSET (TDX_MODULE_r11, tdx_module_output, r11); + +/* Offset for fields in tdvmcall_output */ +OFFSET (TDX_HYPERCALL_r11, tdx_hypercall_output, r11); +OFFSET (TDX_HYPERCALL_r12, tdx_hypercall_output, r12); +OFFSET (TDX_HYPERCALL_r13, tdx_hypercall_output, r13); +OFFSET (TDX_HYPERCALL_r14, tdx_hypercall_output, r14); +OFFSET (TDX_HYPERCALL_r15, tdx_hypercall_output, r15); +#endif +BLANK ();OFFSET (BP_scratch, boot_params, scratch);OFFSET (BP_secure_boot, boot_params, secure_boot);arch/x86/kernel/tdcall.S new file +31
-0@@ -0,0 +1,222 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#include <asm/asm-offsets.h> +#include <asm/asm.h> +#include <asm/frame.h> +#include <asm/unwind_hints.h> + +#include <linux/linkage.h> +#include <linux/bits.h> + +#define TDG_R10 BIT(10) +#define TDG_R11 BIT(11) +#define TDG_R12 BIT(12) +#define TDG_R13 BIT(13) +#define TDG_R14 BIT(14) +#define TDG_R15 BIT(15) + +/* + * Expose registers R10-R15 to VMM. It is passed via RCX register + * to the TDX Module, which will be used by the TDX module to + * identify the list of registers exposed to VMM. Each bit in this + * mask represents a register ID. You can find the bit field details + * in TDX GHCI specification. + */ +#define TDVMCALL_EXPOSE_REGS_MASK ( TDG_R10 | TDG_R11 | \ + TDG_R12 | TDG_R13 | \ + TDG_R14 | TDG_R15 ) + +/* + * TDX guests use the TDCALL instruction to make requests to the + * TDX module and hypercalls to the VMM. It is supported in + * Binutils >= 2.36.
arch/x86/kernel/tdcall.S line 41
Quoted 10 lines
arch/x86/kernel/tdcall.S new file +10
-0+ */ +#define tdcall .byte 0x66,0x0f,0x01,0xcc + +/* + * __tdx_module_call() - Helper function used by TDX guests to request + * services from the TDX module (does not include VMM services). + * + * This function serves as a wrapper to move user call arguments to the + * correct registers as specified by "tdcall" ABI and shares it with the + * TDX module. And if the "tdcall" operation is successful and a valid
It's frequently taught to never start a sentence with "And" in formal writing. You use it fairly frequently. Simply removing it increase readability, IMNHO.
Quoted 61 lines
arch/x86/kernel/tdcall.S new file +61
-0+ * "struct tdx_module_output" pointer is available (in "out" argument), + * output from the TDX module is saved to the memory specified in the + * "out" pointer. Also the status of the "tdcall" operation is returned + * back to the user as a function return value. + * + * @fn (RDI) - TDCALL Leaf ID, moved to RAX + * @rcx (RSI) - Input parameter 1, moved to RCX + * @rdx (RDX) - Input parameter 2, moved to RDX + * @r8 (RCX) - Input parameter 3, moved to R8 + * @r9 (R8) - Input parameter 4, moved to R9 + * + * @out (R9) - struct tdx_module_output pointer + * stored temporarily in R12 (not + * shared with the TDX module) + * + * Return status of tdcall via RAX. + * + * NOTE: This function should not be used for TDX hypercall + * use cases. + */ +SYM_FUNC_START(__tdx_module_call) + FRAME_BEGIN + + /* + * R12 will be used as temporary storage for + * struct tdx_module_output pointer. You can + * find struct tdx_module_output details in + * arch/x86/include/asm/tdx.h. Also note that + * registers R12-R15 are not used by TDCALL + * services supported by this helper function. + */ + push %r12 /* Callee saved, so preserve it */ + mov %r9, %r12 /* Move output pointer to R12 */ + + /* Mangle function call ABI into TDCALL ABI: */ + mov %rdi, %rax /* Move TDCALL Leaf ID to RAX */ + mov %r8, %r9 /* Move input 4 to R9 */ + mov %rcx, %r8 /* Move input 3 to R8 */ + mov %rsi, %rcx /* Move input 1 to RCX */ + /* Leave input param 2 in RDX */ + + tdcall + + /* Check for TDCALL success: 0 - Successful, otherwise failed */ + test %rax, %rax + jnz 1f + + /* Check for TDCALL output struct != NULL */ + test %r12, %r12 + jz 1f + + /* Copy TDCALL result registers to output struct: */ + movq %rcx, TDX_MODULE_rcx(%r12) + movq %rdx, TDX_MODULE_rdx(%r12) + movq %r8, TDX_MODULE_r8(%r12) + movq %r9, TDX_MODULE_r9(%r12) + movq %r10, TDX_MODULE_r10(%r12) + movq %r11, TDX_MODULE_r11(%r12) +1: + pop %r12 /* Restore the state of R12 register */ +
arch/x86/kernel/tdcall.S line 112
Quoted 10 lines
arch/x86/kernel/tdcall.S new file +10
-0+ FRAME_END + ret +SYM_FUNC_END(__tdx_module_call) + +/* + * do_tdx_hypercall() - Helper function used by TDX guests to request + * services from the VMM. All requests are made via the TDX module + * using "TDCALL" instruction. + * + * This function is created to contain common between vendor specific
This sentence seems wrong. Common... what?
arch/x86/kernel/tdcall.S line 113
Quoted 1 line
arch/x86/kernel/tdcall.S new file +1
-0+ * and standard type tdx hypercalls. So the caller of this function had
Please capitalize "tdx" consistently.
Quoted 11 lines
arch/x86/kernel/tdcall.S new file +10
-0+ * to set the TDVMCALL type in the R10 register before calling it. + * This function serves as a wrapper to move user call arguments to the + * correct registers as specified by "tdcall" ABI and shares it with VMM + * via the TDX module. And if the "tdcall" operation is successful and a + * valid "struct tdx_hypercall_output" pointer is available (in "out" + * argument), output from the VMM is saved to the memory specified in the + * "out" pointer. + * + * @fn (RDI) - TDVMCALL function, moved to R11 + * @r12 (RSI) - Input parameter 1, moved to R12
arch/x86/kernel/tdcall.S line 134
Quoted 10 lines
arch/x86/kernel/tdcall.S new file +10
-0+ * @r13 (RDX) - Input parameter 2, moved to R13 + * @r14 (RCX) - Input parameter 3, moved to R14 + * @r15 (R8) - Input parameter 4, moved to R15 + * + * @out (R9) - struct tdx_hypercall_output pointer + * + * On successful completion, return TDX hypercall error code. + * If the "tdcall" operation fails, panic. + * + */
This sounds scary. Can you try to differentate a hypercall failure from a "tdcall" failure?
Actually, I think that's done OK below. Just remove this mention of panic().
Quoted 18 lines
arch/x86/kernel/tdcall.S new file +18
-0+SYM_FUNC_START_LOCAL(do_tdx_hypercall) + /* Save non-volatile GPRs that are exposed to the VMM. */ + push %r15 + push %r14 + push %r13 + push %r12 + + /* Leave hypercall output pointer in R9, it's not clobbered by VMM */ + + /* Mangle function call ABI into TDCALL ABI: */ + xor %eax, %eax /* Move TDCALL leaf ID (TDVMCALL (0)) to RAX */ + mov %rdi, %r11 /* Move TDVMCALL function id to R11 */ + mov %rsi, %r12 /* Move input 1 to R12 */ + mov %rdx, %r13 /* Move input 2 to R13 */ + mov %rcx, %r14 /* Move input 1 to R14 */ + mov %r8, %r15 /* Move input 1 to R15 */ + /* Caller of do_tdx_hypercall() will set TDVMCALL type in R10 */ +
arch/x86/kernel/tdcall.S line 162
Quoted 10 lines
arch/x86/kernel/tdcall.S new file +10
-0+ movl $TDVMCALL_EXPOSE_REGS_MASK, %ecx + + tdcall + + /* + * Check for TDCALL success: 0 - Successful, otherwise failed. + * If failed, there is an issue with TDX Module which is fatal + * for the guest. So panic. Also note that RAX is controlled + * only by the TDX module and not exposed to VMM. + */
I'd probably just say:
/*
* Non-zero RAX values indicate a failure of TDCALL itself.
* Panic for those. This value is unrelated to the hypercall
* result in R10.
*/
arch/x86/kernel/tdcall.S line 173
Quoted 11 lines
arch/x86/kernel/tdcall.S new file +11
-0+ test %rax, %rax + jnz 2f + + /* Move hypercall error code to RAX to return to user */ + mov %r10, %rax + + /* Check for hypercall success: 0 - Successful, otherwise failed */ + test %rax, %rax + jnz 1f + + /* Check for hypercall output struct != NULL */
This is a great example of a comment that's not using its space widely.
If you're reading this, you *KNOW* that it's checking for NULL. But
what does that *MEAN*?
Wh not:
/* Check if caller provided an output struct */
Quoted 4 lines
arch/x86/kernel/tdcall.S new file +4
-0+ test %r9, %r9 + jz 1f + + /* Copy hypercall result registers to output struct: */
arch/x86/kernel/tdcall.S line 187
Quoted 10 lines
arch/x86/kernel/tdcall.S new file +10
-0+ movq %r11, TDX_HYPERCALL_r11(%r9) + movq %r12, TDX_HYPERCALL_r12(%r9) + movq %r13, TDX_HYPERCALL_r13(%r9) + movq %r14, TDX_HYPERCALL_r14(%r9) + movq %r15, TDX_HYPERCALL_r15(%r9) +1: + /* + * Zero out registers exposed to the VMM to avoid + * speculative execution with VMM-controlled values. + */
You can even say:
This needs to include all registers present in TDVMCALL_EXPOSE_REGS_MASK
Quoted 13 lines
arch/x86/kernel/tdcall.S new file +13
-0+ xor %r10d, %r10d + xor %r11d, %r11d + xor %r12d, %r12d + xor %r13d, %r13d + xor %r14d, %r14d + xor %r15d, %r15d + + /* Restore non-volatile GPRs that are exposed to the VMM. */ + pop %r12 + pop %r13 + pop %r14 + pop %r15 +
arch/x86/kernel/tdcall.S line 210
Quoted 10 lines
arch/x86/kernel/tdcall.S new file +10
-0+ ret +2: + ud2 +SYM_FUNC_END(do_tdx_hypercall) + +/* + * Helper function for for standard type of TDVMCALLs. This assembly + * wrapper lets us reuse do_tdvmcall() for standard type of hypercalls + * (R10 is set as zero). + */
Remember, no "us", "we" in changelogs or comments.
arch/x86/kernel/tdcall.S line 222
Quoted 12 lines
arch/x86/kernel/tdcall.S new file +12
-0+SYM_FUNC_START(__tdx_hypercall) + FRAME_BEGIN + /* + * R10 is not part of the function call ABI, but it is a part + * of the TDVMCALL ABI. So set it 0 for standard type TDVMCALL + * before making call to the do_tdx_hypercall(). + */ + xor %r10, %r10 + call do_tdx_hypercall + FRAME_END + retq +SYM_FUNC_END(__tdx_hypercall)
The rest of it is fine. Probably just one more rev to beef up the comments and changelogs.
RFC 1/1 x86/tdx: Add __tdx_module_call() and __tdx_hypercall() helper functions
Guests communicate with VMMs with hypercalls. Historically, these are implemented using instructions that are known to cause VMEXITs like vmcall, vmlaunch, etc. However, with TDX, VMEXITs no longer expose guest state to the host. This prevents the old hypercall mechanisms from working. So to communicate with VMM, TDX specification defines a new instruction called "tdcall".
In a TDX based VM, since VMM is an untrusted entity, a intermediary layer (TDX module) exists between host and guest to facilitate the secure communication. TDX guests communicate with the TDX module and with the VMM using a new instruction: TDCALL.
Implement common helper functions to communicate with the TDX Module and VMM (using TDCALL instruction).__tdx_hypercall() - request services from the VMM.
__tdx_module_call() - communicate with the TDX Module.
Also define two additional wrappers, tdx_hypercall() and tdx_hypercall_out_r11() to cover common use cases of __tdx_hypercall() function. Since each use case of __tdx_module_call() is different, we don't need such wrappers for it.
Implement __tdx_module_call() and __tdx_hypercall() helper functions in assembly.
Rationale behind choosing to use assembly over inline assembly is, since the number of lines of instructions (with comments) in __tdx_hypercall() implementation is over 70, using inline assembly to implement it will make it hard to read.
Also, just like syscalls, not all TDVMCALL/TDCALLs use cases need to use the same set of argument registers. The implementation here picks the current worst-case scenario for TDCALL (4 registers). For TDCALLs with fewer than 4 arguments, there will end up being a few superfluous (cheap) instructions. But, this approach maximizes code reuse. The same argument applies to __tdx_hypercall() function as well.
Current implementation of __tdx_hypercall() includes error handling (ud2 on failure case) in assembly function instead of doing it in C wrapper function. The reason behind this choice is, when adding support for in/out instructions (refer to patch titled "x86/tdx: Handle port I/O" in this series), we use alternative_io() to substitute in/out instruction with __tdx_hypercall() calls. So use of C wrappers is not trivial in this case because the input parameters will be in the wrong registers and it's tricky to include proper buffer code to make this happen.
For registers used by TDCALL instruction, please check TDX GHCI specification, sec 2.4 and 3.
---
Changes since RFC v2-fix-v1:
* Fixed commit log and comment corrections as suggested by Dave.
arch/x86/include/asm/tdx.h | 38 ++++++ arch/x86/kernel/Makefile | 2 +- arch/x86/kernel/asm-offsets.c | 22 ++++ arch/x86/kernel/tdcall.S | 223 ++++++++++++++++++++++++++++++++++ arch/x86/kernel/tdx.c | 39 ++++++ 5 files changed, 323 insertions(+), 1 deletion(-) create mode 100644 arch/x86/kernel/tdcall.S
arch/x86/include/asm/tdx.h +38 -0
@@ -8,12 +8,50 @@#ifdef CONFIG_INTEL_TDX_GUEST #include <asm/cpufeature.h> +#include <linux/types.h> + +/* +* Used in __tdx_module_call() helper function to gather the +* output registers' values of TDCALL instruction when requesting +* services from the TDX module. This is software only structure +* and not related to TDX module/VMM. +*/ +struct tdx_module_output { +u64 rcx; +u64 rdx; +u64 r8; +u64 r9; +u64 r10; +u64 r11; +}; + +/* +* Used in __tdx_hypercall() helper function to gather the +* output registers' values of TDCALL instruction when requesting +* services from the VMM. This is software only structure +* and not related to TDX module/VMM. +*/ +struct tdx_hypercall_output { +u64 r11; +u64 r12; +u64 r13; +u64 r14; +u64 r15; +};/* Common API to check TDX support in decompression and common kernel code. */ bool is_tdx_guest (void );void __init tdx_early_init (void ); +/* Helper function used to communicate with the TDX module */ +u64 __tdx_module_call (u64 fn,u64 rcx,u64 rdx,u64 r8,u64 r9, +struct tdx_module_output *out); + +/* Helper function used to request services from VMM */ +u64 __tdx_hypercall (u64 fn,u64 r12,u64 r13,u64 r14,u64 r15, +struct tdx_hypercall_output *out); +#else // !CONFIG_INTEL_TDX_GUEST static inline bool is_tdx_guest (void )
arch/x86/kernel/Makefile +1 -1
@@ -127,7 +127,7 @@ obj-$(CONFIG_PARAVIRT_CLOCK) += pvclock.o obj-$(CONFIG_X86_PMEM_LEGACY_DEVICE) += pmem.o obj-$(CONFIG_JAILHOUSE_GUEST) += jailhouse.o -obj-$(CONFIG_INTEL_TDX_GUEST) += tdx.o +obj-$(CONFIG_INTEL_TDX_GUEST) += tdcall.o tdx.o obj-$(CONFIG_EISA) += eisa.o obj-$(CONFIG_PCSPKR_PLATFORM) += pcspeaker.o
arch/x86/kernel/asm-offsets.c +22 -0
@@ -23,6 +23,10 @@#include <xen/interface/xen.h> #endif +#ifdef CONFIG_INTEL_TDX_GUEST +#include <asm/tdx.h> +#endif +#ifdef CONFIG_X86_32 # include "asm-offsets_32.c" #else
@@ -75,6 +79,24 @@ static void __used common(void)OFFSET (XEN_vcpu_info_arch_cr2, vcpu_info, arch.cr2);#endif +#ifdef CONFIG_INTEL_TDX_GUEST +BLANK (); +/* Offset for fields in tdcall_output */ +OFFSET (TDX_MODULE_rcx, tdx_module_output, rcx); +OFFSET (TDX_MODULE_rdx, tdx_module_output, rdx); +OFFSET (TDX_MODULE_r8, tdx_module_output, r8); +OFFSET (TDX_MODULE_r9, tdx_module_output, r9); +OFFSET (TDX_MODULE_r10, tdx_module_output, r10); +OFFSET (TDX_MODULE_r11, tdx_module_output, r11); + +/* Offset for fields in tdvmcall_output */ +OFFSET (TDX_HYPERCALL_r11, tdx_hypercall_output, r11); +OFFSET (TDX_HYPERCALL_r12, tdx_hypercall_output, r12); +OFFSET (TDX_HYPERCALL_r13, tdx_hypercall_output, r13); +OFFSET (TDX_HYPERCALL_r14, tdx_hypercall_output, r14); +OFFSET (TDX_HYPERCALL_r15, tdx_hypercall_output, r15); +#endif +BLANK ();OFFSET (BP_scratch, boot_params, scratch);OFFSET (BP_secure_boot, boot_params, secure_boot);
arch/x86/kernel/tdcall.S new file +223 -0
223 lines not on this page · raw message
arch/x86/kernel/tdx.c +39 -0
@@ -1,8 +1,47 @@// SPDX-License-Identifier: GPL-2.0 /* Copyright (C) 2020 Intel Corporation */ +#define pr_fmt (fmt) "TDX: " fmt +#include <asm/tdx.h> +/* +* Wrapper for simple hypercalls that only return a success/error code. +*/ +static inline u64 tdx_hypercall (u64 fn,u64 r12,u64 r13,u64 r14,u64 r15) +{ +u64 err; + + err =__tdx_hypercall (fn, r12, r13, r14, r15,NULL ); + +if (err) +pr_warn_ratelimited ("TDVMCALL fn:%llx failed with err:%llx\n" , + fn, err); + +return err; +} + +/* +* Wrapper for the semi-common case where we need single output +* value (R11). Callers of this function does not care about the +* hypercall error code (mainly for IN or MMIO usecase). +*/ +static inline u64 tdx_hypercall_out_r11 (u64 fn,u64 r12,u64 r13, +u64 r14,u64 r15) +{ + +struct tdx_hypercall_output out = {0 }; +u64 err; + + err =__tdx_hypercall (fn, r12, r13, r14, r15, &out); + +if (err) +pr_warn_ratelimited ("TDVMCALL fn:%llx failed with err:%llx\n" , + fn, err); + +return out.r11; +} +static inline bool cpuid_has_tdx_guest (void ) {u32 eax, signature[3 ];
Signature · 1 line
2.25.1
Re: [RFC v2-fix-v1 1/1] x86/tdx: Add __tdx_module_call() and __tdx_hypercall() helper functions
In reply to Dave HansenQuoted 1 line · Dave Hansen
Was this "older compiler" argument really the reason?
It is a speculation. I haven't tried to reproduce it with old compiler. So I have removed that point.
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Sean Christopherson
maintainerRe: [RFC v2-fix-v1 1/1] x86/tdx: Add __tdx_module_call() and __tdx_hypercall() helper functions
Quoted 6 lines · Sathyanarayanan
Quoted 1 line · Dave Hansen
Was this "older compiler" argument really the reason?
It is a speculation. I haven't tried to reproduce it with old compiler. So I have removed that point.
It's not "older" compilers. gcc does not support R8-R15 as input/output constraints, which means inline asm needs to do register shenanigans, and those are horribly fragile because the compiler does not ensure register variables are preserved outside of asm blobs. E.g. adding a print like so can corrupt r10, which makes it an absolute nightmare to debug/trace flows that pass r8-r15 to asm blobs since looking at the code the wrong way can break things.
register unsigned long r10 asm("r10") = __r10;
pr_info("TDCALL: RAX = %lx, R10 = %lx\n", rax, __r10);
asm volatile("tdcall"
: "=a"(rax) :
: "a"(rax), "r"(r10));Re: [RFC v2-fix-v1 1/1] x86/tdx: Add __tdx_module_call() and __tdx_hypercall() helper functions
Quoted 7 lines · Sean Christopherson
Quoted 4 lines · Sathyanarayanan
Quoted 1 line · Dave Hansen
Was this "older compiler" argument really the reason?
It is a speculation. I haven't tried to reproduce it with old compiler. So I have removed that point.
It's not "older" compilers. gcc does not support R8-R15 as input/output constraints,
Yes that's true, but they can be in clobbers. So it usually just needs a mov from the input arguments for output, or a mov to the output arguments for output.
RFC 1/1 x86/tdx: Add __tdx_module_call() and __tdx_hypercall() helper functions
Guests communicate with VMMs with hypercalls. Historically, these are implemented using instructions that are known to cause VMEXITs like vmcall, vmlaunch, etc. However, with TDX, VMEXITs no longer expose guest state to the host. This prevents the old hypercall mechanisms from working. So to communicate with VMM, TDX specification defines a new instruction called "tdcall".
In a TDX based VM, since VMM is an untrusted entity, a intermediary layer (TDX module) exists between host and guest to facilitate the secure communication. TDX guests communicate with the TDX module and with the VMM using a new instruction: TDCALL.
Implement common helper functions to communicate with the TDX Module and VMM (using TDCALL instruction).__tdx_hypercall() - request services from the VMM.
__tdx_module_call() - communicate with the TDX Module.
Also define two additional wrappers, tdx_hypercall() and tdx_hypercall_out_r11() to cover common use cases of __tdx_hypercall() function. Since each use case of __tdx_module_call() is different, it does not need multiple wrappers.
Implement __tdx_module_call() and __tdx_hypercall() helper functions in assembly.
Rationale behind choosing to use assembly over inline assembly is, since the number of lines of instructions (with comments) in __tdx_hypercall() implementation is over 70, using inline assembly to implement it will make it hard to read.
Also, just like syscalls, not all TDVMCALL/TDCALLs use cases need to use the same set of argument registers. The implementation here picks the current worst-case scenario for TDCALL (4 registers). For TDCALLs with fewer than 4 arguments, there will end up being a few superfluous (cheap) instructions. But, this approach maximizes code reuse. The same argument applies to __tdx_hypercall() function as well.
For registers used by TDCALL instruction, please check TDX GHCI specification, sec 2.4 and 3.
---
Changes since RFC v2-fix-v1:
* No code changes (Adding Tony to "To" list)
Changes since RFC v2:
* Renamed __tdcall()/__tdvmcall() to __tdx_module_call()/__tdx_hypercall().
* Renamed reg offsets from TDCALL_rx to TDX_MODULE_rx.
* Renamed reg offsets from TDVMCALL_rx to TDX_HYPERCALL_rx.
* Renamed struct tdcall_output to struct tdx_module_output.
* Renamed struct tdvmcall_output to struct tdx_hypercall_output.
* Used BIT() to derive TDVMCALL_EXPOSE_REGS_MASK.
* Removed unnecessary push/pop sequence in __tdcall() function.
* Fixed comments as per Dave's review.
arch/x86/include/asm/tdx.h | 38 ++++++ arch/x86/kernel/Makefile | 2 +- arch/x86/kernel/asm-offsets.c | 22 ++++ arch/x86/kernel/tdcall.S | 223 ++++++++++++++++++++++++++++++++++ arch/x86/kernel/tdx.c | 38 ++++++ 5 files changed, 322 insertions(+), 1 deletion(-) create mode 100644 arch/x86/kernel/tdcall.S
arch/x86/include/asm/tdx.h +38 -0
@@ -8,12 +8,50 @@#ifdef CONFIG_INTEL_TDX_GUEST #include <asm/cpufeature.h> +#include <linux/types.h> + +/* +* Used in __tdx_module_call() helper function to gather the +* output registers' values of TDCALL instruction when requesting +* services from the TDX module. This is software only structure +* and not related to TDX module/VMM. +*/ +struct tdx_module_output { +u64 rcx; +u64 rdx; +u64 r8; +u64 r9; +u64 r10; +u64 r11; +}; + +/* +* Used in __tdx_hypercall() helper function to gather the +* output registers' values of TDCALL instruction when requesting +* services from the VMM. This is software only structure +* and not related to TDX module/VMM. +*/ +struct tdx_hypercall_output { +u64 r11; +u64 r12; +u64 r13; +u64 r14; +u64 r15; +};/* Common API to check TDX support in decompression and common kernel code. */ bool is_tdx_guest (void );void __init tdx_early_init (void ); +/* Helper function used to communicate with the TDX module */ +u64 __tdx_module_call (u64 fn,u64 rcx,u64 rdx,u64 r8,u64 r9, +struct tdx_module_output *out); + +/* Helper function used to request services from VMM */ +u64 __tdx_hypercall (u64 fn,u64 r12,u64 r13,u64 r14,u64 r15, +struct tdx_hypercall_output *out); +#else // !CONFIG_INTEL_TDX_GUEST static inline bool is_tdx_guest (void )
arch/x86/kernel/Makefile +1 -1
@@ -127,7 +127,7 @@ obj-$(CONFIG_PARAVIRT_CLOCK) += pvclock.o obj-$(CONFIG_X86_PMEM_LEGACY_DEVICE) += pmem.o obj-$(CONFIG_JAILHOUSE_GUEST) += jailhouse.o -obj-$(CONFIG_INTEL_TDX_GUEST) += tdx.o +obj-$(CONFIG_INTEL_TDX_GUEST) += tdcall.o tdx.o obj-$(CONFIG_EISA) += eisa.o obj-$(CONFIG_PCSPKR_PLATFORM) += pcspeaker.o
arch/x86/kernel/asm-offsets.c +22 -0
@@ -23,6 +23,10 @@#include <xen/interface/xen.h> #endif +#ifdef CONFIG_INTEL_TDX_GUEST +#include <asm/tdx.h> +#endif +#ifdef CONFIG_X86_32 # include "asm-offsets_32.c" #else
@@ -75,6 +79,24 @@ static void __used common(void)OFFSET (XEN_vcpu_info_arch_cr2, vcpu_info, arch.cr2);#endif +#ifdef CONFIG_INTEL_TDX_GUEST +BLANK (); +/* Offset for fields in tdx_module_output */ +OFFSET (TDX_MODULE_rcx, tdx_module_output, rcx); +OFFSET (TDX_MODULE_rdx, tdx_module_output, rdx); +OFFSET (TDX_MODULE_r8, tdx_module_output, r8); +OFFSET (TDX_MODULE_r9, tdx_module_output, r9); +OFFSET (TDX_MODULE_r10, tdx_module_output, r10); +OFFSET (TDX_MODULE_r11, tdx_module_output, r11); + +/* Offset for fields in tdx_hypercall_output */ +OFFSET (TDX_HYPERCALL_r11, tdx_hypercall_output, r11); +OFFSET (TDX_HYPERCALL_r12, tdx_hypercall_output, r12); +OFFSET (TDX_HYPERCALL_r13, tdx_hypercall_output, r13); +OFFSET (TDX_HYPERCALL_r14, tdx_hypercall_output, r14); +OFFSET (TDX_HYPERCALL_r15, tdx_hypercall_output, r15); +#endif +BLANK ();OFFSET (BP_scratch, boot_params, scratch);OFFSET (BP_secure_boot, boot_params, secure_boot);
arch/x86/kernel/tdcall.S new file +223 -0
223 lines not on this page · raw message
arch/x86/kernel/tdx.c +38 -0
@@ -1,8 +1,46 @@// SPDX-License-Identifier: GPL-2.0 /* Copyright (C) 2020 Intel Corporation */ +#define pr_fmt (fmt) "TDX: " fmt +#include <asm/tdx.h> +/* +* Wrapper for simple hypercalls that only return a success/error code. +*/ +static inline u64 tdx_hypercall (u64 fn,u64 r12,u64 r13,u64 r14,u64 r15) +{ +u64 err; + + err =__tdx_hypercall (fn, r12, r13, r14, r15,NULL ); + +if (err) +pr_warn_ratelimited ("TDVMCALL fn:%llx failed with err:%llx\n" , + fn, err); + +return err; +} + +/* +* Wrapper for the semi-common case where user need single output +* value (R11). Callers of this function does not care about the +* hypercall error code (mainly for IN or MMIO usecase). +*/ +static inline u64 tdx_hypercall_out_r11 (u64 fn,u64 r12,u64 r13, +u64 r14,u64 r15) +{ +struct tdx_hypercall_output out = {0 }; +u64 err; + + err =__tdx_hypercall (fn, r12, r13, r14, r15, &out); + +if (err) +pr_warn_ratelimited ("TDVMCALL fn:%llx failed with err:%llx\n" , + fn, err); + +return out.r11; +} +static inline bool cpuid_has_tdx_guest (void ) {u32 eax, signature[3 ];
Signature · 1 line
2.25.1
RE: [RFC v2-fix-v2 1/1] x86/tdx: Add __tdx_module_call() and __tdx_hypercall() helper functions
Quoted 6 lines
Guests communicate with VMMs with hypercalls. Historically, these are implemented using instructions that are known to cause VMEXITs like vmcall, vmlaunch, etc. However, with TDX, VMEXITs no longer expose guest state to the host. This prevents the old hypercall mechanisms from working. So to communicate with VMM, TDX specification defines a new instruction called "tdcall".
You use all caps TDCALL everywhere else in this commit message. Looks odd to have quoted lower case here.
Quoted 4 lines
In a TDX based VM, since VMM is an untrusted entity, a intermediary layer (TDX module) exists between host and guest to facilitate the secure communication. TDX guests communicate with the TDX module and with the VMM using a new instruction: TDCALL.
Seems both repeat what was in the first paragraph, but also fail to explain how this TDCALL is different from that first TDCALL.
Quoted 5 lines
Implement common helper functions to communicate with the TDX Module and VMM (using TDCALL instruction).
__tdx_hypercall()- request services from the VMM.
__tdx_module_call() - communicate with the TDX Module.
Looking at the code, the hypercall can return an error if TDCALL fails, but module_call forces a panic with UD2 on error. This difference isn't explained anywhere.
-Tony
Re: [RFC v2-fix-v2 1/1] x86/tdx: Add __tdx_module_call() and __tdx_hypercall() helper functions
Quoted 9 lines · Tony
Quoted 6 lines
Guests communicate with VMMs with hypercalls. Historically, these are implemented using instructions that are known to cause VMEXITs like vmcall, vmlaunch, etc. However, with TDX, VMEXITs no longer expose guest state to the host. This prevents the old hypercall mechanisms from working. So to communicate with VMM, TDX specification defines a new instruction called "tdcall".
You use all caps TDCALL everywhere else in this commit message. Looks odd to have quoted lower case here.
I will use TDCALL uniformly.
Quoted 8 lines
Quoted 5 lines
In a TDX based VM, since VMM is an untrusted entity, a intermediary layer (TDX module) exists between host and guest to facilitate the secure communication. TDX guests communicate with the TDX module and with the VMM using a new instruction: TDCALL.
Seems both repeat what was in the first paragraph, but also fail to explain how this TDCALL is different from that first TDCALL.
Both cases uses TDCALL instruction. Arguments we pass confirms the type of TDCALL ( one used to communicate with TDX module vs one used to communicate with VMM).
I can modify the description to convey the difference between both cases.
Quoted 10 lines
Quoted 6 lines
Implement common helper functions to communicate with the TDX Module and VMM (using TDCALL instruction).
__tdx_hypercall()- request services from the VMM.
__tdx_module_call() - communicate with the TDX Module.Looking at the code, the hypercall can return an error if TDCALL fails, but
module_callforces a panic with UD2 on error. This difference isn't explained anywhere.
I think you meant hypercall will panic vs module call will not.
In hypercall case, since we use same TDCALL instruction, we will have two return values. One is for TDCALL failure (at the TDX module level) and other is return value from VMM. So in hypercall case, we return VMM value to the user but panic for TDCALL failures. As per TDX spec, for hypercall use case, if everything is in order, TDCALL will never fail. If we notice TDCALL failure error then it means, we are working with the broken TDX module. So we panic.
Quoted 2 lines
-Tony
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Luck, Tony
Reviewed-byRE: [RFC v2-fix-v2 1/1] x86/tdx: Add __tdx_module_call() and __tdx_hypercall() helper functions
Quoted 5 lines
Quoted 3 lines
Looking at the code, the hypercall can return an error if TDCALL fails, but
module_callforces a panic with UD2 on error. This difference isn't explained anywhere.I think you meant hypercall will panic vs module call will not.
yes
Quoted 7 lines
In hypercall case, since we use same TDCALL instruction, we will have two return values. One is for TDCALL failure (at the TDX module level) and other is return value from VMM. So in hypercall case, we return VMM value to the user but panic for TDCALL failures. As per TDX spec, for hypercall use case, if everything is in order, TDCALL will never fail. If we notice TDCALL failure error then it means, we are working with the broken TDX module. So we panic.
Add a comment in the .S file right before that ud2 explaining this. That should help anyone tracking down that panic understand that the problem is in the TDX module.
Otherwise looks ok.
-Tony
RFC v2 6/32 x86/tdx: Get TD execution environment information via TDINFO
In reply to Kuppuswamy SathyanarayananFrom: "Kirill A. Shutemov" <[email protected]>
Per Guest-Host-Communication Interface (GHCI) for Intel Trust Domain Extensions (Intel TDX) specification, sec 2.4.2, TDCALL[TDINFO] provides basic TD execution environment information, not provided by CPUID.
Call TDINFO during early boot to be used for following system initialization.
The call provides info on which bit in pfn is used to indicate that the page is shared with the host and attributes of the TD, such as debug.
We don't save information about the number of cpus as there's no users so far.
--- arch/x86/include/asm/tdx.h | 2 ++ arch/x86/kernel/tdx.c | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+)
arch/x86/include/asm/tdx.h +2 -0
@@ -10,6 +10,8 @@#include <asm/cpufeature.h> #include <linux/types.h> +#define TDINFO 1 +struct tdcall_output {u64 rcx;u64 rdx;
arch/x86/kernel/tdx.c +23 -0
@@ -4,6 +4,14 @@#define pr_fmt (fmt) "TDX: " fmt#include <asm/tdx.h> +#include <asm/vmx.h> + +#include <linux/cpu.h> + +static struct { +unsigned int gpa_width; +unsigned long attributes; +} td_info__ro_after_init ;/* * Wrapper for use case that checks for error code and print warning message.
@@ -61,6 +69,19 @@ bool is_tdx_guest(void) }EXPORT_SYMBOL_GPL (is_tdx_guest); +static void tdg_get_info (void ) +{ +u64 ret; +struct tdcall_output out = {0 }; + + ret =__tdcall (TDINFO ,0 ,0 ,0 ,0 , &out); + +BUG_ON (ret); + + td_info.gpa_width = out.rcx &GENMASK (5 ,0 ); + td_info.attributes = out.rdx; +} +void __init tdx_early_init (void ) {if (!cpuid_has_tdx_guest ())
@@ -68,5 +89,7 @@ void __init tdx_early_init(void)setup_force_cpu_cap (X86_FEATURE_TDX_GUEST ); +tdg_get_info (); +pr_info ("TDX guest is initialized\n" ); }
Signature · 1 line
2.25.1
RFC v2 7/32 x86/traps: Add do_general_protection() helper function
In reply to Kuppuswamy SathyanarayananTDX guest #VE exception handler treats unsupported exceptions as #GP. So to handle the #GP, move the protection fault handler code to out of exc_general_protection() and create new helper function for it.
Also since exception handler is responsible to decide when to turn on/off IRQ, move cond_local_irq_{enable/disable)() calls out of do_general_protection().
This is a preparatory patch for adding #VE exception handler support for TDX guests.
--- arch/x86/kernel/traps.c | 51 ++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 24 deletions(-)
arch/x86/kernel/traps.c +27 -24
@@ -527,44 +527,28 @@ static enum kernel_gp_hint get_kernel_gp_address(struct pt_regs *regs,#define GPFSTR "general protection fault" -DEFINE_IDTENTRY_ERRORCODE (exc_general_protection) +static void do_general_protection (struct pt_regs *regs,long error_code) {char desc[sizeof (GPFSTR ) +50 +2 *sizeof (unsigned long ) +1 ] =GPFSTR ;enum kernel_gp_hint hint =GP_NO_HINT ; -struct task_struct *tsk; +struct task_struct *tsk = current;unsigned long gp_addr;int ret; -cond_local_irq_enable (regs); - -if (static_cpu_has (X86_FEATURE_UMIP )) { -if (user_mode (regs) &&fixup_umip_exception (regs)) - goto exit; - } - -if (v8086_mode (regs)) { -local_irq_enable (); -handle_vm86_fault ((struct kernel_vm86_regs *) regs, error_code); -local_irq_disable (); -return ; - } - - tsk = current; -if (user_mode (regs)) { tsk->thread.error_code = error_code; tsk->thread.trap_nr =X86_TRAP_GP ;if (fixup_vdso_exception (regs,X86_TRAP_GP , error_code,0 )) - goto exit; +return ;show_signal (tsk,SIGSEGV ,"" , desc, regs, error_code);force_sig (SIGSEGV ); - goto exit; +return ; }if (fixup_exception (regs,X86_TRAP_GP , error_code,0 )) - goto exit; +return ; tsk->thread.error_code = error_code; tsk->thread.trap_nr =X86_TRAP_GP ;
@@ -576,11 +560,11 @@ DEFINE_IDTENTRY_ERRORCODE(exc_general_protection)if (!preemptible () &&kprobe_running () &&kprobe_fault_handler (regs,X86_TRAP_GP )) - goto exit; +return ; ret =notify_die (DIE_GPF , desc, regs, error_code,X86_TRAP_GP ,SIGSEGV );if (ret ==NOTIFY_STOP ) - goto exit; +return ;if (error_code)snprintf (desc,sizeof (desc),"segment-related " GPFSTR );
@@ -601,8 +585,27 @@ DEFINE_IDTENTRY_ERRORCODE(exc_general_protection) gp_addr =0 ;die_addr (desc, regs, error_code, gp_addr); +} -exit: +DEFINE_IDTENTRY_ERRORCODE (exc_general_protection) +{ +cond_local_irq_enable (regs); + +if (static_cpu_has (X86_FEATURE_UMIP )) { +if (user_mode (regs) &&fixup_umip_exception (regs)) { +cond_local_irq_disable (regs); +return ; + } + } + +if (v8086_mode (regs)) { +local_irq_enable (); +handle_vm86_fault ((struct kernel_vm86_regs *) regs, error_code); +local_irq_disable (); +return ; + } + +do_general_protection (regs, error_code);cond_local_irq_disable (regs); }
Signature · 1 line
2.25.1
Re: [RFC v2 07/32] x86/traps: Add do_general_protection() helper function
Quoted 1 line · Kuppuswamy Sathyanarayanan
TDX guest #VE exception handler treats unsupported exceptions
^ The
Quoted 1 line
as #GP. So to handle the #GP, move the protection fault handler
s/So to/To/
Also, it does not "treat them as #GP". It handles them in the same way that a #GP is handled. There's a difference between literally making them a #GP and having a similar end result. This description conflates them.
Quoted 2 lines
code to out of
exc_general_protection()and create new helper function for it.
I wouldn't name the functions. Just say that you want the #GP behavior from #VE so you need a common helper.
Quoted 1 line
Also since exception handler is responsible to decide when to
^ an
Quoted 2 lines
turn on/off IRQ, move cond_local_irq_{enable/disable)() calls out of do_general_protection().
This paragraph doesn't really say anything meaningful. Yes, exception handlers reenable interrupts. Try to *SAY* something about why they do this and why you have to move the code around. Or, just axe it.
Quoted 63 lines
This is a preparatory patch for adding #VE exception handler support for TDX guests.
Signed-off-by Kuppuswamy Sathyanarayanan <[email protected]>--- arch/x86/kernel/traps.c | 51 ++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 24 deletions(-)arch/x86/kernel/traps.c +5
-21@@ -527,44 +527,28 @@ static enum kernel_gp_hint get_kernel_gp_address(struct pt_regs *regs,#define GPFSTR "general protection fault" -DEFINE_IDTENTRY_ERRORCODE (exc_general_protection) +static void do_general_protection (struct pt_regs *regs,long error_code) {char desc[sizeof (GPFSTR ) +50 +2 *sizeof (unsigned long ) +1 ] =GPFSTR ;enum kernel_gp_hint hint =GP_NO_HINT ; -struct task_struct *tsk; +struct task_struct *tsk = current;unsigned long gp_addr;int ret; -cond_local_irq_enable (regs); - -if (static_cpu_has (X86_FEATURE_UMIP )) { -if (user_mode (regs) &&fixup_umip_exception (regs)) - goto exit; - } - -if (v8086_mode (regs)) { -local_irq_enable (); -handle_vm86_fault ((struct kernel_vm86_regs *) regs, error_code); -local_irq_disable (); -return ; - } - - tsk = current; -if (user_mode (regs)) { tsk->thread.error_code = error_code; tsk->thread.trap_nr =X86_TRAP_GP ;if (fixup_vdso_exception (regs,X86_TRAP_GP , error_code,0 )) - goto exit; +return ;show_signal (tsk,SIGSEGV ,"" , desc, regs, error_code);force_sig (SIGSEGV ); - goto exit; +return ; }if (fixup_exception (regs,X86_TRAP_GP , error_code,0 )) - goto exit; +return ; tsk->thread.error_code = error_code; tsk->thread.trap_nr =X86_TRAP_GP ;
arch/x86/kernel/traps.c DEFINE_IDTENTRY_ERRORCODE(exc_general_protection) · line 565
Quoted 7 lines
arch/x86/kernel/traps.c +1
-1@@ -576,11 +560,11 @@ DEFINE_IDTENTRY_ERRORCODE(exc_general_protection)if (!preemptible () &&kprobe_running () &&kprobe_fault_handler (regs,X86_TRAP_GP )) - goto exit; +return ; ret =notify_die (DIE_GPF , desc, regs, error_code,X86_TRAP_GP ,SIGSEGV );
So... We're going to send signals based on #VE which use this bit in the ABI which is documented as:
#define X86_TRAP_GP 13 /* General Protection Fault */
Considering that there is also a:
#define X86_TRAP_VE 20 /* Virtualization Exception */
this seems like a stretch.
Also, isnt there a lot of truly #GP-specific code in there, like fixup_exception()? Why do you need to call that for #VE? How did you decide what remains in the handler versus what gets separated out?
Quoted 34 lines
arch/x86/kernel/traps.c +21
-2if (ret ==NOTIFY_STOP ) - goto exit; +return ;if (error_code)snprintf (desc,sizeof (desc),"segment-related " GPFSTR );@@ -601,8 +585,27 @@ DEFINE_IDTENTRY_ERRORCODE(exc_general_protection) gp_addr =0 ;die_addr (desc, regs, error_code, gp_addr); +} -exit: +DEFINE_IDTENTRY_ERRORCODE (exc_general_protection) +{ +cond_local_irq_enable (regs); + +if (static_cpu_has (X86_FEATURE_UMIP )) { +if (user_mode (regs) &&fixup_umip_exception (regs)) { +cond_local_irq_disable (regs); +return ; + } + } + +if (v8086_mode (regs)) { +local_irq_enable (); +handle_vm86_fault ((struct kernel_vm86_regs *) regs, error_code); +local_irq_disable (); +return ; + } + +do_general_protection (regs, error_code);cond_local_irq_disable (regs); }
RFC v2 8/32 x86/traps: Add #VE support for TDX guest
In reply to Kuppuswamy SathyanarayananFrom: "Kirill A. Shutemov" <[email protected]>
The TDX module injects #VE exception to the guest TD in cases of disallowed instructions, disallowed MSR accesses and subset of CPUID leaves. The TDX module guarantees that no #VE is injected on an EPT violation on guest physical addresses that are memory. We can still get #VE on MMIO mappings. This avoids any problems with the “system call gap”.
Add basic infrastructure to handle #VE. If there is no handler for a given #VE, since it is an unexpected event (fault case), treat it as a general protection fault and handle it using do_general_protection() call.
TDCALL[TDGETVEINFO] provides information about #VE such as exit reason.
The #VE cannot be nested before TDGETVEINFO is called, if there is any reason for it to nest the TD would shut down. The TDX module guarantees that no NMIs (or #MC or similar) can happen in this window. After TDGETVEINFO the #VE handler can nest if needed, although we don’t expect it to happen normally.
--- arch/x86/include/asm/idtentry.h | 4 ++++ arch/x86/include/asm/tdx.h | 15 +++++++++++++ arch/x86/kernel/idt.c | 6 ++++++ arch/x86/kernel/tdx.c | 38 +++++++++++++++++++++++++++++++++ arch/x86/kernel/traps.c | 30 ++++++++++++++++++++++++++ 5 files changed, 93 insertions(+)
arch/x86/include/asm/idtentry.h +4 -0
@@ -619,6 +619,10 @@ DECLARE_IDTENTRY_XENCB(X86_TRAP_OTHER, exc_xen_hypervisor_callback);DECLARE_IDTENTRY_RAW (X86_TRAP_OTHER , exc_xen_unknown_trap);#endif +#ifdef CONFIG_INTEL_TDX_GUEST +DECLARE_IDTENTRY (X86_TRAP_VE , exc_virtualization_exception); +#endif +/* Device interrupts common/spurious */ DECLARE_IDTENTRY_IRQ (X86_TRAP_OTHER , common_interrupt);#ifdef CONFIG_X86_LOCAL_APIC
arch/x86/include/asm/tdx.h +15 -0
@@ -11,6 +11,7 @@#include <linux/types.h> #define TDINFO 1 +#define TDGETVEINFO 3struct tdcall_output {u64 rcx;
@@ -29,6 +30,20 @@ struct tdvmcall_output {u64 r15; }; +struct ve_info { +u64 exit_reason; +u64 exit_qual; +u64 gla; +u64 gpa; +u32 instr_len; +u32 instr_info; +}; + +unsigned long tdg_get_ve_info (struct ve_info *ve); + +int tdg_handle_virtualization_exception (struct pt_regs *regs, +struct ve_info *ve); +/* Common API to check TDX support in decompression and common kernel code. */ bool is_tdx_guest (void );
arch/x86/kernel/idt.c +6 -0
@@ -64,6 +64,9 @@ static const __initconst struct idt_data early_idts[] = {*/ INTG(X86_TRAP_PF, asm_exc_page_fault),#endif +#ifdef CONFIG_INTEL_TDX_GUEST + INTG(X86_TRAP_VE, asm_exc_virtualization_exception), +#endif };/*
@@ -87,6 +90,9 @@ static const __initconst struct idt_data def_idts[] = {INTG (X86_TRAP_MF , asm_exc_coprocessor_error),INTG (X86_TRAP_AC , asm_exc_alignment_check),INTG (X86_TRAP_XF , asm_exc_simd_coprocessor_error), +#ifdef CONFIG_INTEL_TDX_GUEST +INTG (X86_TRAP_VE , asm_exc_virtualization_exception), +#endif #ifdef CONFIG_X86_32 TSKG(X86_TRAP_DF, GDT_ENTRY_DOUBLEFAULT_TSS),
arch/x86/kernel/tdx.c +38 -0
@@ -82,6 +82,44 @@ static void tdg_get_info(void) td_info.attributes = out.rdx; } +unsigned long tdg_get_ve_info (struct ve_info *ve) +{ +u64 ret; +struct tdcall_output out = {0 }; + +/* +* The #VE cannot be nested before TDGETVEINFO is called, +* if there is any reason for it to nest the TD would shut +* down. The TDX module guarantees that no NMIs (or #MC or +* similar) can happen in this window. After TDGETVEINFO +* the #VE handler can nest if needed, although we don’t +* expect it to happen normally. +*/ + + ret =__tdcall (TDGETVEINFO ,0 ,0 ,0 ,0 , &out); + + ve->exit_reason = out.rcx; + ve->exit_qual = out.rdx; + ve->gla = out.r8; + ve->gpa = out.r9; + ve->instr_len = out.r10 &UINT_MAX ; + ve->instr_info = out.r10 >>32 ; + +return ret; +} + +int tdg_handle_virtualization_exception (struct pt_regs *regs, +struct ve_info *ve) +{ +/* +* TODO: Add handler support for various #VE exit +* reasons. It will be added by other patches in +* the series. +*/ +pr_warn ("Unexpected #VE: %lld\n" , ve->exit_reason); +return -EFAULT ; +} +void __init tdx_early_init (void ) {if (!cpuid_has_tdx_guest ())
arch/x86/kernel/traps.c +30 -0
@@ -61,6 +61,7 @@#include <asm/insn.h> #include <asm/insn-eval.h> #include <asm/vdso.h> +#include <asm/tdx.h> #ifdef CONFIG_X86_64 #include <asm/x86_init.h>
@@ -1140,6 +1141,35 @@ DEFINE_IDTENTRY(exc_device_not_available) } } +#ifdef CONFIG_INTEL_TDX_GUEST +DEFINE_IDTENTRY (exc_virtualization_exception) +{ +struct ve_info ve; +int ret; + +RCU_LOCKDEP_WARN (!rcu_is_watching (),"entry code didn't wake RCU" ); + +/* +* Consume #VE info before re-enabling interrupts. It will be +* re-enabled after executing the TDGETVEINFO TDCALL. +*/ + ret =tdg_get_ve_info (&ve); + +cond_local_irq_enable (regs); + +if (!ret) + ret =tdg_handle_virtualization_exception (regs, &ve); +/* +* If tdg_handle_virtualization_exception() could not process +* it successfully, treat it as #GP(0) and handle it. +*/ +if (ret) +do_general_protection (regs,0 ); + +cond_local_irq_disable (regs); +} +#endif +#ifdef CONFIG_X86_32 DEFINE_IDTENTRY_SW (iret_error) {
Signature · 1 line
2.25.1
Re: [RFC v2 08/32] x86/traps: Add #VE support for TDX guest
On 4/26/21 11:01 AM, Kuppuswamy Sathyanarayanan wrote:
...
Quoted 5 lines
The #VE cannot be nested before TDGETVEINFO is called, if there is any reason for it to nest the TD would shut down. The TDX module guarantees that no NMIs (or #MC or similar) can happen in this window. After TDGETVEINFO the #VE handler can nest if needed, although we don’t expect it to happen normally.
I think this description really needs some work. Does "The #VE cannot be nested" mean that "hardware guarantees that #VE will not be generated", or "the #VE must not be nested"?
What does "the TD would shut down" mean? I think you mean that instead of delivering a nested #VE the hardware would actually exit to the host and TDX would prevent the guest from being reentered. Right?
Quoted 29 lines
arch/x86/include/asm/idtentry.h +4
-0@@ -619,6 +619,10 @@ DECLARE_IDTENTRY_XENCB(X86_TRAP_OTHER, exc_xen_hypervisor_callback);DECLARE_IDTENTRY_RAW (X86_TRAP_OTHER , exc_xen_unknown_trap);#endif +#ifdef CONFIG_INTEL_TDX_GUEST +DECLARE_IDTENTRY (X86_TRAP_VE , exc_virtualization_exception); +#endif +/* Device interrupts common/spurious */ DECLARE_IDTENTRY_IRQ (X86_TRAP_OTHER , common_interrupt);#ifdef CONFIG_X86_LOCAL_APIC arch/x86/include/asm/tdx.h +1
-0@@ -11,6 +11,7 @@#include <linux/types.h> #define TDINFO 1 +#define TDGETVEINFO 3struct tdcall_output {u64 rcx;@@ -29,6 +30,20 @@ struct tdvmcall_output {u64 r15;
arch/x86/include/asm/tdx.h struct tdvmcall_output { · line 40
Quoted 10 lines
arch/x86/include/asm/tdx.h +8
-0}; +struct ve_info { +u64 exit_reason; +u64 exit_qual; +u64 gla; +u64 gpa; +u32 instr_len; +u32 instr_info; +};
Is this an architectural structure or some software construct?
Quoted 43 lines
arch/x86/include/asm/tdx.h +5
-0+unsigned long tdg_get_ve_info (struct ve_info *ve); + +int tdg_handle_virtualization_exception (struct pt_regs *regs, +struct ve_info *ve); +/* Common API to check TDX support in decompression and common kernel code. */ bool is_tdx_guest (void );arch/x86/kernel/idt.c +6
-0@@ -64,6 +64,9 @@ static const __initconst struct idt_data early_idts[] = {*/ INTG(X86_TRAP_PF, asm_exc_page_fault),#endif +#ifdef CONFIG_INTEL_TDX_GUEST + INTG(X86_TRAP_VE, asm_exc_virtualization_exception), +#endif };/* @@ -87,6 +90,9 @@ static const __initconst struct idt_data def_idts[] = {INTG (X86_TRAP_MF , asm_exc_coprocessor_error),INTG (X86_TRAP_AC , asm_exc_alignment_check),INTG (X86_TRAP_XF , asm_exc_simd_coprocessor_error), +#ifdef CONFIG_INTEL_TDX_GUEST +INTG (X86_TRAP_VE , asm_exc_virtualization_exception), +#endif #ifdef CONFIG_X86_32 TSKG(X86_TRAP_DF, GDT_ENTRY_DOUBLEFAULT_TSS),arch/x86/kernel/tdx.c +3
-0@@ -82,6 +82,44 @@ static void tdg_get_info(void) td_info.attributes = out.rdx; } +unsigned long tdg_get_ve_info (struct ve_info *ve) +{ +u64 ret;
arch/x86/kernel/tdx.c static void tdg_get_info(void) · line 97
Quoted 10 lines
arch/x86/kernel/tdx.c +10
-0+struct tdcall_output out = {0 }; + +/* +* The #VE cannot be nested before TDGETVEINFO is called, +* if there is any reason for it to nest the TD would shut +* down. The TDX module guarantees that no NMIs (or #MC or +* similar) can happen in this window. After TDGETVEINFO +* the #VE handler can nest if needed, although we don’t +* expect it to happen normally. +*/
I find that description a bit unsatisfying. Could we make this a bit more concrete? By the way, what about *normal* interrupts?
Maybe we should talk about this in terms of *rules* that folks need to follow. Maybe:
NMIs and machine checks are suppressed. Before this point any #VE is fatal. After this point, NMIs and additional #VEs are permitted.
Quoted 45 lines
arch/x86/kernel/tdx.c +24
-0+ ret =__tdcall (TDGETVEINFO ,0 ,0 ,0 ,0 , &out); + + ve->exit_reason = out.rcx; + ve->exit_qual = out.rdx; + ve->gla = out.r8; + ve->gpa = out.r9; + ve->instr_len = out.r10 &UINT_MAX ; + ve->instr_info = out.r10 >>32 ; + +return ret; +} + +int tdg_handle_virtualization_exception (struct pt_regs *regs, +struct ve_info *ve) +{ +/* +* TODO: Add handler support for various #VE exit +* reasons. It will be added by other patches in +* the series. +*/ +pr_warn ("Unexpected #VE: %lld\n" , ve->exit_reason); +return -EFAULT ; +} +void __init tdx_early_init (void ) {if (!cpuid_has_tdx_guest ())arch/x86/kernel/traps.c +3
-0@@ -61,6 +61,7 @@#include <asm/insn.h> #include <asm/insn-eval.h> #include <asm/vdso.h> +#include <asm/tdx.h> #ifdef CONFIG_X86_64 #include <asm/x86_init.h> @@ -1140,6 +1141,35 @@ DEFINE_IDTENTRY(exc_device_not_available) } } +#ifdef CONFIG_INTEL_TDX_GUEST +DEFINE_IDTENTRY (exc_virtualization_exception)
arch/x86/kernel/traps.c DEFINE_IDTENTRY(exc_device_not_available) · line 1155
Quoted 10 lines
arch/x86/kernel/traps.c +10
-0+{ +struct ve_info ve; +int ret; + +RCU_LOCKDEP_WARN (!rcu_is_watching (),"entry code didn't wake RCU" ); + +/* +* Consume #VE info before re-enabling interrupts. It will be +* re-enabled after executing the TDGETVEINFO TDCALL. +*/
"It" is nebulous here. Is this talking about NMIs, or the cond_local_irq_enable() that is "after" TDGETVEINFO?
Quoted 21 lines
arch/x86/kernel/traps.c +17
-0+ ret =tdg_get_ve_info (&ve); + +cond_local_irq_enable (regs); + +if (!ret) + ret =tdg_handle_virtualization_exception (regs, &ve); +/* +* If tdg_handle_virtualization_exception() could not process +* it successfully, treat it as #GP(0) and handle it. +*/ +if (ret) +do_general_protection (regs,0 ); + +cond_local_irq_disable (regs); +} +#endif +#ifdef CONFIG_X86_32 DEFINE_IDTENTRY_SW (iret_error) {
Re: [RFC v2 08/32] x86/traps: Add #VE support for TDX guest
Quoted 10 lines · Dave Hansen
On 4/26/21 11:01 AM, Kuppuswamy Sathyanarayanan wrote:
...Quoted 5 lines
The #VE cannot be nested before TDGETVEINFO is called, if there is any reason for it to nest the TD would shut down. The TDX module guarantees that no NMIs (or #MC or similar) can happen in this window. After TDGETVEINFO the #VE handler can nest if needed, although we don’t expect it to happen normally.
I think this description really needs some work. Does "The #VE cannot be nested" mean that "hardware guarantees that #VE will not be generated", or "the #VE must not be nested"?
The next half sentence answers this question..
"if there is any reason for it to nest the TD would shut down."
So it cannot nest.
Quoted 4 lines
What does "the TD would shut down" mean? I think you mean that instead of delivering a nested #VE the hardware would actually exit to the host and TDX would prevent the guest from being reentered. Right?
Yes that's a shutdown. I Suppose we could add your sentence.
Quoted 2 lines
I find that description a bit unsatisfying. Could we make this a bit more concrete?
I don't see what could be added. If you have concrete suggestions please just propose something.
Quoted 1 line
By the way, what about *normal* interrupts?
Normal interrupts are blocked of course like in every other exception or interrupt entry.
Quoted 7 lines
Maybe we should talk about this in terms of *rules* that folks need to follow. Maybe:
NMIs and machine checks are suppressed. Before this point any #VE is fatal. After this point, NMIs and additional #VEs are permitted.
Okay that's fine for me.
-Andi
Re: [RFC v2 08/32] x86/traps: Add #VE support for TDX guest
Quoted 1 line · Andi Kleen
"if there is any reason for it to nest the TD would shut down."
The TDX EAS says:
Quoted 4 lines
If, when attempting to inject a #VE, the Intel TDX module discovers that the guest TD has not yet retrieved the information for a previous #VE (i.e., VE_INFO.VALID is not 0), the TDX module injects a #DF into the guest TD to indicate a #VE overrun.
How does that result in a shut down?
Re: [RFC v2 08/32] x86/traps: Add #VE support for TDX guest
Quoted 9 lines · Dave Hansen
Quoted 1 line · Andi Kleen
"if there is any reason for it to nest the TD would shut down."
The TDX EAS says:
Quoted 4 lines
If, when attempting to inject a #VE, the Intel TDX module discovers that the guest TD has not yet retrieved the information for a previous #VE (i.e., VE_INFO.VALID is not 0), the TDX module injects a #DF into the guest TD to indicate a #VE overrun.
How does that result in a shut down?
You're right. It's not a shutdown, but a panic. We'll need to fix the comment and replace 'shutdown' with 'panic'
-And
Re: [RFC v2 08/32] x86/traps: Add #VE support for TDX guest
In reply to Andi KleenQuoted 2 lines · Andi Kleen
I don't see what could be added. If you have concrete suggestions please just propose something.
Oh, boy, I love writing changelogs! I was hoping that the TDX folks would chip in to write their own changelogs, but oh well. You made my day!
--
Virtualization Exceptions (#VE) are delivered to TDX guests due to specific guest actions which may happen in either userspace or the kernel:
* Specific instructions (WBINVD, for example)
* Specific MSR accesses
* Specific CPUID leaf accesses
* Access to TD-shared memory, which includes MMIO
#VE exceptions are never generated on accesses to normal, TD-private memory.
The entry paths do not access TD-shared memory or use those specific MSRs, instructions, CPUID leaves. In addition, all interrupts including NMIs are blocked by the hardware starting with #VE delivery until TDGETVEINFO is called. This eliminates the chance of a #VE during the syscall gap or paranoid entry paths and simplifies #VE handling.
If a guest kernel action which would normally cause a #VE occurs in the interrupt-disabled region before TDGETVEINFO, a #DF is delivered to the guest.
Add basic infrastructure to handle any #VE which occurs in the kernel or userspace. Later patches will add handling for specific #VE scenarios.
Convert unhandled #VE's (everything, until later in this series) so that they appear just like a #GP by calling do_general_protection() directly.
--
Did I miss anything?
RFC 1/1 x86/traps: Add #VE support for TDX guest
In reply to Dave HansenFrom: "Kirill A. Shutemov" <[email protected]>
Virtualization Exceptions (#VE) are delivered to TDX guests due to specific guest actions which may happen in either user space or the kernel:
* Specific instructions (WBINVD, for example)
* Specific MSR accesses
* Specific CPUID leaf accesses
* Access to TD-shared memory, which includes MMIO
In the settings that Linux will run in, virtual exceptions are never generated on accesses to normal, TD-private memory that has been accepted.
The entry paths do not access TD-shared memory, MMIO regions or use those specific MSRs, instructions, CPUID leaves that might generate #VE. In addition, all interrupts including NMIs are blocked by the hardware starting with #VE delivery until TDGETVEINFO is called. This eliminates the chance of a #VE during the syscall gap or paranoid entry paths and simplifies #VE handling.
After TDGETVEINFO #VE could happen in theory (e.g. through an NMI), although we don't expect it to happen because we don't expect NMIs to trigger #VEs. Another case where they could happen is if the #VE exception panics, but in this case there are no guarantees on anything anyways.
If a guest kernel action which would normally cause a #VE occurs in the interrupt-disabled region before TDGETVEINFO, a #DF is delivered to the guest which will result in an oops (and should eventually be a panic, as we would like to set panic_on_oops to 1 for TDX guests).
Add basic infrastructure to handle any #VE which occurs in the kernel or userspace. Later patches will add handling for specific #VE scenarios.
Convert unhandled #VE's (everything, until later in this series) so that they appear just like a #GP by calling ve_raise_fault() directly. ve_raise_fault() is similar to #GP handler and is responsible for sending SIGSEGV to userspace and cpu die and notifying debuggers and other die chain users.
---
Changes since v1:
* Removed [RFC v2 07/32] x86/traps: Add do_general_protection() helper function.
* Instead of resuing #GP handler, defined a custom handler.
* Fixed commit log as per review comments.
arch/x86/include/asm/idtentry.h | 4 ++ arch/x86/include/asm/tdx.h | 20 ++++++++++ arch/x86/kernel/idt.c | 6 +++ arch/x86/kernel/tdx.c | 35 +++++++++++++++++ arch/x86/kernel/traps.c | 70 +++++++++++++++++++++++++++++++++ 5 files changed, 135 insertions(+)
arch/x86/include/asm/idtentry.h +4 -0
@@ -619,6 +619,10 @@ DECLARE_IDTENTRY_XENCB(X86_TRAP_OTHER, exc_xen_hypervisor_callback);DECLARE_IDTENTRY_RAW (X86_TRAP_OTHER , exc_xen_unknown_trap);#endif +#ifdef CONFIG_INTEL_TDX_GUEST +DECLARE_IDTENTRY (X86_TRAP_VE , exc_virtualization_exception); +#endif +/* Device interrupts common/spurious */ DECLARE_IDTENTRY_IRQ (X86_TRAP_OTHER , common_interrupt);#ifdef CONFIG_X86_LOCAL_APIC
arch/x86/include/asm/tdx.h +20 -0
@@ -11,6 +11,7 @@#include <linux/types.h> #define TDINFO 1 +#define TDGETVEINFO 3struct tdx_module_output {u64 rcx;
@@ -29,6 +30,25 @@ struct tdx_hypercall_output {u64 r15; }; +/* +* Used by #VE exception handler to gather the #VE exception +* info from the TDX module. This is software only structure +* and not related to TDX module/VMM. +*/ +struct ve_info { +u64 exit_reason; +u64 exit_qual; +u64 gla; +u64 gpa; +u32 instr_len; +u32 instr_info; +}; + +unsigned long tdg_get_ve_info (struct ve_info *ve); + +int tdg_handle_virtualization_exception (struct pt_regs *regs, +struct ve_info *ve); +/* Common API to check TDX support in decompression and common kernel code. */ bool is_tdx_guest (void );
arch/x86/kernel/idt.c +6 -0
@@ -64,6 +64,9 @@ static const __initconst struct idt_data early_idts[] = {*/ INTG(X86_TRAP_PF, asm_exc_page_fault),#endif +#ifdef CONFIG_INTEL_TDX_GUEST + INTG(X86_TRAP_VE, asm_exc_virtualization_exception), +#endif };/*
@@ -87,6 +90,9 @@ static const __initconst struct idt_data def_idts[] = {INTG (X86_TRAP_MF , asm_exc_coprocessor_error),INTG (X86_TRAP_AC , asm_exc_alignment_check),INTG (X86_TRAP_XF , asm_exc_simd_coprocessor_error), +#ifdef CONFIG_INTEL_TDX_GUEST +INTG (X86_TRAP_VE , asm_exc_virtualization_exception), +#endif #ifdef CONFIG_X86_32 TSKG(X86_TRAP_DF, GDT_ENTRY_DOUBLEFAULT_TSS),
arch/x86/kernel/tdx.c +35 -0
@@ -85,6 +85,41 @@ static void tdg_get_info(void) td_info.attributes = out.rdx; } +unsigned long tdg_get_ve_info (struct ve_info *ve) +{ +u64 ret; +struct tdx_module_output out = {0 }; + +/* +* NMIs and machine checks are suppressed. Before this point any +* #VE is fatal. After this point (TDGETVEINFO call), NMIs and +* additional #VEs are permitted (but we don't expect them to +* happen unless you panic). +*/ + ret =__tdx_module_call (TDGETVEINFO ,0 ,0 ,0 ,0 , &out); + + ve->exit_reason = out.rcx; + ve->exit_qual = out.rdx; + ve->gla = out.r8; + ve->gpa = out.r9; + ve->instr_len = out.r10 &UINT_MAX ; + ve->instr_info = out.r10 >>32 ; + +return ret; +} + +int tdg_handle_virtualization_exception (struct pt_regs *regs, +struct ve_info *ve) +{ +/* +* TODO: Add handler support for various #VE exit +* reasons. It will be added by other patches in +* the series. +*/ +pr_warn ("Unexpected #VE: %lld\n" , ve->exit_reason); +return -EFAULT ; +} +void __init tdx_early_init (void ) {if (!cpuid_has_tdx_guest ())
arch/x86/kernel/traps.c +70 -0
82 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [RFC v2-fix 1/1] x86/traps: Add #VE support for TDX guest
Quoted 5 lines · Kuppuswamy Sathyanarayanan
After TDGETVEINFO #VE could happen in theory (e.g. through an NMI), although we don't expect it to happen because we don't expect NMIs to trigger #VEs. Another case where they could happen is if the #VE exception panics, but in this case there are no guarantees on anything anyways.
This implies: "we do not expect any NMI to do MMIO". Is that true? Why?
Re: [RFC v2-fix 1/1] x86/traps: Add #VE support for TDX guest
Quoted 7 lines · Dave Hansen
Quoted 5 lines · Kuppuswamy Sathyanarayanan
After TDGETVEINFO #VE could happen in theory (e.g. through an NMI), although we don't expect it to happen because we don't expect NMIs to trigger #VEs. Another case where they could happen is if the #VE exception panics, but in this case there are no guarantees on anything anyways.
This implies: "we do not expect any NMI to do MMIO". Is that true? Why?
Only drivers that are not supported in TDX anyways could do it (mainly watchdog drivers)
panic is an exception, but that has been already covered.
-Andi
Re: [RFC v2-fix 1/1] x86/traps: Add #VE support for TDX guest
Quoted 8 lines · Andi Kleen
Quoted 6 lines · Dave Hansen
Quoted 5 lines · Kuppuswamy Sathyanarayanan
After TDGETVEINFO #VE could happen in theory (e.g. through an NMI), although we don't expect it to happen because we don't expect NMIs to trigger #VEs. Another case where they could happen is if the #VE exception panics, but in this case there are no guarantees on anything anyways.
Quoted 4 lines
Quoted 1 line
This implies: "we do not expect any NMI to do MMIO". Is that true? Why?
Only drivers that are not supported in TDX anyways could do it (mainly watchdog drivers)
No APIC access either?
Also, shouldn't we have at least a:
WARN_ON_ONCE(in_nmi());
if we don't expect (or handle well) #VE in NMIs?
Re: [RFC v2-fix 1/1] x86/traps: Add #VE support for TDX guest
Quoted 2 lines
No APIC access either?
It's all X2APIC inside TDX which uses MSRs
Quoted 6 lines
Also, shouldn't we have at least a:
WARN_ON_ONCE(in_nmi());if we don't expect (or handle well) #VE in NMIs?
We handle it perfectly fine. It's just not needed.
-Andi
Re: [RFC v2-fix 1/1] x86/traps: Add #VE support for TDX guest
In reply to Andi KleenQuoted 9 lines · Andi Kleen
Quoted 6 lines · Dave Hansen
Quoted 5 lines · Kuppuswamy Sathyanarayanan
After TDGETVEINFO #VE could happen in theory (e.g. through an NMI), although we don't expect it to happen because we don't expect NMIs to trigger #VEs. Another case where they could happen is if the #VE exception panics, but in this case there are no guarantees on anything anyways.
Quoted 4 lines
Quoted 1 line
This implies: "we do not expect any NMI to do MMIO". Is that true? Why?
Only drivers that are not supported in TDX anyways could do it (mainly watchdog drivers)
What about apei_{read,write}() for ACPI error handling? Those are called in NMI to do MMIO accesses. It's not just watchdog drivers.
Re: [RFC v2-fix 1/1] x86/traps: Add #VE support for TDX guest
Quoted 5 lines
Quoted 3 lines
Only drivers that are not supported in TDX anyways could do it (mainly watchdog drivers)
What about apei_{read,write}() for ACPI error handling? Those are called in NMI to do MMIO accesses. It's not just watchdog drivers.
We expect the APEI stuff to be filtered in the normal case to reduce the attack surface. There's no use case for APEI error reporting in a normally operating TDX guest.
But yes that's why I wrote mainly. It should work in any case, we fully support #VE nesting after TDVEREPORT.
-Andi
RFC 1/1 x86/traps: Add #VE support for TDX guest
From: "Kirill A. Shutemov" <[email protected]>
Virtualization Exceptions (#VE) are delivered to TDX guests due to specific guest actions which may happen in either user space or the kernel:
* Specific instructions (WBINVD, for example)
* Specific MSR accesses
* Specific CPUID leaf accesses
* Access to TD-shared memory, which includes MMIO
In the settings that Linux will run in, virtual exceptions are never generated on accesses to normal, TD-private memory that has been accepted.
The entry paths do not access TD-shared memory, MMIO regions or use those specific MSRs, instructions, CPUID leaves that might generate #VE. In addition, all interrupts including NMIs are blocked by the hardware starting with #VE delivery until TDGETVEINFO is called. This eliminates the chance of a #VE during the syscall gap or paranoid entry paths and simplifies #VE handling.
After TDGETVEINFO #VE could happen in theory (e.g. through an NMI), but it is expected not to happen because TDX expects NMIs not to trigger #VEs. Another case where they could happen is if the #VE exception panics, but in this case there are no guarantees on anything anyways.
If a guest kernel action which would normally cause a #VE occurs in the interrupt-disabled region before TDGETVEINFO, a #DF is delivered to the guest which will result in an oops (and should eventually be a panic, as we would like to set panic_on_oops to 1 for TDX guests).
Add basic infrastructure to handle any #VE which occurs in the kernel or userspace. Later patches will add handling for specific #VE scenarios.
Convert unhandled #VE's (everything, until later in this series) so that they appear just like a #GP by calling ve_raise_fault() directly. ve_raise_fault() is similar to #GP handler and is responsible for sending SIGSEGV to userspace and cpu die and notifying debuggers and other die chain users.
---
Changes since RFC v2-fix:
* No code changes (Added Tony to "To" list)
Changes since v1:
* Removed [RFC v2 07/32] x86/traps: Add do_general_protection() helper function.
* Instead of resuing #GP handler, defined a custom handler.
* Fixed commit log as per review comments.
arch/x86/include/asm/idtentry.h | 4 ++ arch/x86/include/asm/tdx.h | 19 +++++++++ arch/x86/kernel/idt.c | 6 +++ arch/x86/kernel/tdx.c | 36 +++++++++++++++++ arch/x86/kernel/traps.c | 69 +++++++++++++++++++++++++++++++++ 5 files changed, 134 insertions(+)
arch/x86/include/asm/idtentry.h +4 -0
@@ -619,6 +619,10 @@ DECLARE_IDTENTRY_XENCB(X86_TRAP_OTHER, exc_xen_hypervisor_callback);DECLARE_IDTENTRY_RAW (X86_TRAP_OTHER , exc_xen_unknown_trap);#endif +#ifdef CONFIG_INTEL_TDX_GUEST +DECLARE_IDTENTRY (X86_TRAP_VE , exc_virtualization_exception); +#endif +/* Device interrupts common/spurious */ DECLARE_IDTENTRY_IRQ (X86_TRAP_OTHER , common_interrupt);#ifdef CONFIG_X86_LOCAL_APIC
arch/x86/include/asm/tdx.h +19 -0
@@ -39,6 +39,25 @@ struct tdx_hypercall_output {u64 r15; }; +/* +* Used by #VE exception handler to gather the #VE exception +* info from the TDX module. This is software only structure +* and not related to TDX module/VMM. +*/ +struct ve_info { +u64 exit_reason; +u64 exit_qual; +u64 gla; +u64 gpa; +u32 instr_len; +u32 instr_info; +}; + +unsigned long tdg_get_ve_info (struct ve_info *ve); + +int tdg_handle_virtualization_exception (struct pt_regs *regs, +struct ve_info *ve); +/* Common API to check TDX support in decompression and common kernel code. */ bool is_tdx_guest (void );
arch/x86/kernel/idt.c +6 -0
@@ -64,6 +64,9 @@ static const __initconst struct idt_data early_idts[] = {*/ INTG(X86_TRAP_PF, asm_exc_page_fault),#endif +#ifdef CONFIG_INTEL_TDX_GUEST + INTG(X86_TRAP_VE, asm_exc_virtualization_exception), +#endif };/*
@@ -87,6 +90,9 @@ static const __initconst struct idt_data def_idts[] = {INTG (X86_TRAP_MF , asm_exc_coprocessor_error),INTG (X86_TRAP_AC , asm_exc_alignment_check),INTG (X86_TRAP_XF , asm_exc_simd_coprocessor_error), +#ifdef CONFIG_INTEL_TDX_GUEST +INTG (X86_TRAP_VE , asm_exc_virtualization_exception), +#endif #ifdef CONFIG_X86_32 TSKG(X86_TRAP_DF, GDT_ENTRY_DOUBLEFAULT_TSS),
arch/x86/kernel/tdx.c +36 -0
@@ -10,6 +10,7 @@/* TDX Module call Leaf IDs */ #define TDINFO 1 +#define TDGETVEINFO 3 static struct { unsigned int gpa_width;
@@ -87,6 +88,41 @@ static void tdg_get_info(void) td_info.attributes = out.rdx; } +unsigned long tdg_get_ve_info (struct ve_info *ve) +{ +u64 ret; +struct tdx_module_output out = {0 }; + +/* +* NMIs and machine checks are suppressed. Before this point any +* #VE is fatal. After this point (TDGETVEINFO call), NMIs and +* additional #VEs are permitted (but we don't expect them to +* happen unless you panic). +*/ + ret =__tdx_module_call (TDGETVEINFO ,0 ,0 ,0 ,0 , &out); + + ve->exit_reason = out.rcx; + ve->exit_qual = out.rdx; + ve->gla = out.r8; + ve->gpa = out.r9; + ve->instr_len = out.r10 &UINT_MAX ; + ve->instr_info = out.r10 >>32 ; + +return ret; +} + +int tdg_handle_virtualization_exception (struct pt_regs *regs, +struct ve_info *ve) +{ +/* +* TODO: Add handler support for various #VE exit +* reasons. It will be added by other patches in +* the series. +*/ +pr_warn ("Unexpected #VE: %lld\n" , ve->exit_reason); +return -EFAULT ; +} +void __init tdx_early_init (void ) {if (!cpuid_has_tdx_guest ())
arch/x86/kernel/traps.c +69 -0
81 lines not on this page · raw message
Signature · 1 line
2.25.1
RE: [RFC v2-fix-v2 1/1] x86/traps: Add #VE support for TDX guest
+struct ve_info {
+ u64 exit_reason;
+ u64 exit_qual;
+ u64 gla;
+ u64 gpa;
+ u32 instr_len;
+ u32 instr_info;
+};
I guess that "gla" = Guest Linear Address ... which is a very "Intel" way of describing what everyone else would call a Guest Virtual Address.
I don't feel strongly about this though. If this has already been hashed out already then stick with this name.
Otherwise:
-Tony
Sean Christopherson
maintainerRe: [RFC v2-fix-v2 1/1] x86/traps: Add #VE support for TDX guest
Quoted 9 lines · Tony
arch/x86/include/asm/tdx.h +8
-0+struct ve_info { +u64 exit_reason; +u64 exit_qual; +u64 gla; +u64 gpa; +u32 instr_len; +u32 instr_info; +};
Quoted 5 lines
I guess that "gla" = Guest Linear Address ... which is a very "Intel" way of describing what everyone else would call a Guest Virtual Address.
I don't feel strongly about this though. If this has already been hashed out already then stick with this name.
The "real" #VE information area that TDX is usurping is an architectural struct that defines exit_reason, exit_qual, gla, and gpa, and those fields in turn come directly from their corresponding VMCS fields with longer versions of the same names, e.g. ve_info->gla is a reflection of vmcs.GUEST_LINEAR_ADDRESS.
So normally I would agree that the "linear" terminology is obnoxious, but in this specific case I think it's warranted.
Re: [RFC v2-fix-v2 1/1] x86/traps: Add #VE support for TDX guest
Quoted 20 lines · Sean Christopherson
Quoted 9 lines · Tony
arch/x86/include/asm/tdx.h +8
-0+struct ve_info { +u64 exit_reason; +u64 exit_qual; +u64 gla; +u64 gpa; +u32 instr_len; +u32 instr_info; +};Quoted 5 lines
I guess that "gla" = Guest Linear Address ... which is a very "Intel" way of describing what everyone else would call a Guest Virtual Address.
I don't feel strongly about this though. If this has already been hashed out already then stick with this name.
The "real" #VE information area that TDX is usurping is an architectural struct that defines
exit_reason,exit_qual, gla, and gpa, and those fields in turn come directly from their corresponding VMCS fields with longer versions of the same names, e.g.ve_info->glais a reflection of vmcs.GUEST_LINEAR_ADDRESS.
Quoted 2 lines
So normally I would agree that the "linear" terminology is obnoxious, but in this specific case I think it's warranted.
The architectural name needs to be *somewhere*. But, we do diverge from the naming in plenty of places. The architectural name "XSTATE_BV" is called xstate.xfeatures in the FPU code, for instance.
In this case, the _least_ we can do is:
u64 gla; /* Guest Linear (virtual) Address */
although I also wouldn't mind if we did something like:
u64 guest_vaddr; /* Guest Linear Address (gla) in the spec */
either.
Re: [RFC v2-fix 1/1] x86/traps: Add #VE support for TDX guest
In reply to Kuppuswamy SathyanarayananHi Dave,
Quoted 41 lines · Kuppuswamy Sathyanarayanan
From: "Kirill A. Shutemov"<[email protected]>
Virtualization Exceptions (#VE) are delivered to TDX guests due to specific guest actions which may happen in either user space or the kernel:
* Specific instructions (WBINVD, for example)
* Specific MSR accesses
* Specific CPUID leaf accesses
* Access to TD-shared memory, which includes MMIOIn the settings that Linux will run in, virtual exceptions are never generated on accesses to normal, TD-private memory that has been accepted.
The entry paths do not access TD-shared memory, MMIO regions or use those specific MSRs, instructions, CPUID leaves that might generate #VE. In addition, all interrupts including NMIs are blocked by the hardware starting with #VE delivery until TDGETVEINFO is called. This eliminates the chance of a #VE during the syscall gap or paranoid entry paths and simplifies #VE handling.
After TDGETVEINFO #VE could happen in theory (e.g. through an NMI), although we don't expect it to happen because we don't expect NMIs to trigger #VEs. Another case where they could happen is if the #VE exception panics, but in this case there are no guarantees on anything anyways.
If a guest kernel action which would normally cause a #VE occurs in the interrupt-disabled region before TDGETVEINFO, a #DF is delivered to the guest which will result in an oops (and should eventually be a panic, as we would like to set
panic_on_oopsto 1 for TDX guests).Add basic infrastructure to handle any #VE which occurs in the kernel or userspace. Later patches will add handling for specific #VE scenarios.
Convert unhandled #VE's (everything, until later in this series) so that they appear just like a #GP by calling
ve_raise_fault()directly.ve_raise_fault()is similar to #GP handler and is responsible for sending SIGSEGV to userspace and cpu die and notifying debuggers and other die chain users.
Quoted 6 lines
Co-developed-by Sean Christopherson<[email protected]>Signed-off-by Sean Christopherson<[email protected]>Signed-off-by Kirill A. Shutemov<[email protected]>Reviewed-by Andi Kleen<[email protected]>Signed-off-by Kuppuswamy Sathyanarayanan<[email protected]>---
You have any other comments on this patch? If not, can you reply with your Reviewed-by tag?
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Re: [RFC v2-fix 1/1] x86/traps: Add #VE support for TDX guest
Quoted 2 lines · Sathyanarayanan
You have any other comments on this patch? If not, can you reply with your Reviewed-by tag?
Sathya, I've been rather busy with your own patches and your colleagues TDX patches. I've clearly communicated to you which patches I plan to provide a review for. I'll get to them, although not quite at the speed you would like.
If you would like to get a quicker review, I'd highly suggest you go find some of your TDX colleagues' code that needs its quality improved and help by providing them reviews. Reviews are a two-way street, not just a service provided by maintainers to contributors.
You could also make good use of your time by going back over all of the review comments I've made up to this point and doing a pass over your work to ensure that I don't have to continue to repeat myself and waste review efforts. You could add a spell checker to your workflow, or scripting to check for language conventions like avoiding "us" and "we".
You could also seek out help to raise the quality of your
communications. It isn't just reviewers that can help raise the quality of your contributions.
Re: [RFC v2-fix 1/1] x86/traps: Add #VE support for TDX guest
Quoted 9 lines · Dave Hansen
Quoted 2 lines · Sathyanarayanan
You have any other comments on this patch? If not, can you reply with your Reviewed-by tag?
Sathya, I've been rather busy with your own patches and your colleagues TDX patches. I've clearly communicated to you which patches I plan to provide a review for. I'll get to them, although not quite at the speed you would like.
My impression so far is, for TDX patch submissions, you usually reply to the patch submission/comments in 1-2 days (sorry if this assumption is incorrect). Since I did not see any major objections for this patch, I was just checking with you to understand if this patch review is pending due to something missing from my end. My intention was not to rush you, but just to understand if it needs some work from my end.
Sorry if the reminder emails trouble you. Since we are aiming for v5.14 merge window, I am trying to avoid any delays from my end.
Quoted 9 lines
If you would like to get a quicker review, I'd highly suggest you go find some of your TDX colleagues' code that needs its quality improved and help by providing them reviews. Reviews are a two-way street, not just a service provided by maintainers to contributors.
You could also make good use of your time by going back over all of the review comments I've made up to this point and doing a pass over your work to ensure that I don't have to continue to repeat myself and waste review efforts.
I have considered your comments and fixed the common issues reported by you in this patch-set. But when addressing recent comments and while updating the commit log, some of these issues got introduced again. I will try to avoid them in future.
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Re: [RFC v2 08/32] x86/traps: Add #VE support for TDX guest
In reply to Kuppuswamy Sathyanarayananarch/x86/kernel/traps.c DEFINE_IDTENTRY(exc_device_not_available) · line 1156
Quoted 13 lines · Kuppuswamy Sathyanarayanan
arch/x86/kernel/traps.c +13
-0+#ifdef CONFIG_INTEL_TDX_GUEST +DEFINE_IDTENTRY (exc_virtualization_exception) +{ +struct ve_info ve; +int ret; + +RCU_LOCKDEP_WARN (!rcu_is_watching (),"entry code didn't wake RCU" ); + +/* +* Consume #VE info before re-enabling interrupts. It will be +* re-enabled after executing the TDGETVEINFO TDCALL. +*/ + ret =tdg_get_ve_info (&ve);
Is it safe to have *anything* before the tdg_get_ve_info()? For instance, say that RCU_LOCKDEP_WARN() triggers. Will anything in there do MMIO?
Sean Christopherson
maintainerRe: [RFC v2 08/32] x86/traps: Add #VE support for TDX guest
Quoted 10 lines · Dave Hansen
Quoted 9 lines · Kuppuswamy Sathyanarayanan
arch/x86/kernel/traps.c +9
-0+#ifdef CONFIG_INTEL_TDX_GUEST +DEFINE_IDTENTRY (exc_virtualization_exception) +{ +struct ve_info ve; +int ret; + +RCU_LOCKDEP_WARN (!rcu_is_watching (),"entry code didn't wake RCU" ); + +/*
Quoted 8 lines
arch/x86/kernel/traps.c DEFINE_IDTENTRY(exc_device_not_available) · line 1156
Quoted 4 lines
arch/x86/kernel/traps.c +4
-0+* Consume #VE info before re-enabling interrupts. It will be +* re-enabled after executing the TDGETVEINFO TDCALL. +*/ + ret =tdg_get_ve_info (&ve);Is it safe to have *anything* before the
tdg_get_ve_info()? For instance, say thatRCU_LOCKDEP_WARN()triggers. Will anything in there do MMIO?
I doubt it's safe, anything that's doing printing has the potential to trigger #VE. Even if we can prove it's safe for all possible paths, I can't think of a reason to allow anything that's not absolutely necessary before retrieving the #VE info.
Re: [RFC v2 08/32] x86/traps: Add #VE support for TDX guest
Quoted 18 lines · Sean Christopherson
Quoted 17 lines · Dave Hansen
arch/x86/kernel/traps.c DEFINE_IDTENTRY(exc_device_not_available) · line 1156
Quoted 13 lines · Kuppuswamy Sathyanarayanan
arch/x86/kernel/traps.c +13
-0+#ifdef CONFIG_INTEL_TDX_GUEST +DEFINE_IDTENTRY (exc_virtualization_exception) +{ +struct ve_info ve; +int ret; + +RCU_LOCKDEP_WARN (!rcu_is_watching (),"entry code didn't wake RCU" ); + +/* +* Consume #VE info before re-enabling interrupts. It will be +* re-enabled after executing the TDGETVEINFO TDCALL. +*/ + ret =tdg_get_ve_info (&ve);Is it safe to have *anything* before the
tdg_get_ve_info()? For instance, say thatRCU_LOCKDEP_WARN()triggers. Will anything in there do MMIO?
Quoted 4 lines
I doubt it's safe, anything that's doing printing has the potential to trigger #VE. Even if we can prove it's safe for all possible paths, I can't think of a reason to allow anything that's not absolutely necessary before retrieving the #VE info.
What about tracing? Can I plop a kprobe in here or turn on ftrace?
Re: [RFC v2 08/32] x86/traps: Add #VE support for TDX guest
Quoted 19 lines · Dave Hansen
Quoted 18 lines · Sean Christopherson
Quoted 17 lines · Dave Hansen
arch/x86/kernel/traps.c DEFINE_IDTENTRY(exc_device_not_available) · line 1156
Quoted 13 lines · Kuppuswamy Sathyanarayanan
arch/x86/kernel/traps.c +13
-0+#ifdef CONFIG_INTEL_TDX_GUEST +DEFINE_IDTENTRY (exc_virtualization_exception) +{ +struct ve_info ve; +int ret; + +RCU_LOCKDEP_WARN (!rcu_is_watching (),"entry code didn't wake RCU" ); + +/* +* Consume #VE info before re-enabling interrupts. It will be +* re-enabled after executing the TDGETVEINFO TDCALL. +*/ + ret =tdg_get_ve_info (&ve);Is it safe to have *anything* before the
tdg_get_ve_info()? For instance, say thatRCU_LOCKDEP_WARN()triggers. Will anything in there do MMIO?
Quoted 5 lines
Quoted 4 lines
I doubt it's safe, anything that's doing printing has the potential to trigger #VE. Even if we can prove it's safe for all possible paths, I can't think of a reason to allow anything that's not absolutely necessary before retrieving the #VE info.
What about tracing? Can I plop a kprobe in here or turn on ftrace?
I believe neither does mmio/msr normally (except maybe ftrace+tp_printk, but that will likely work because it shouldn't recurse more than once due to ftrace's reentry protection)
-Andi
Re: [RFC v2 08/32] x86/traps: Add #VE support for TDX guest
Quoted 3 lines · Andi Kleen
I believe neither does mmio/msr normally (except maybe ftrace+tp_printk, but that will likely work because it shouldn't recurse more than once due to ftrace's reentry protection)
Can it do MMIO:
arch/x86/kernel/traps.c DEFINE_IDTENTRY(exc_device_not_available) · line 1146
Quoted 2 lines
arch/x86/kernel/traps.c +2
-0+DEFINE_IDTENTRY (exc_virtualization_exception) +{
=======> HERE
Quoted 1 line
+ ret =
tdg_get_ve_info(&ve);
Recursion isn't the problem. It would double-fault there, right?
Re: [RFC v2 08/32] x86/traps: Add #VE support for TDX guest
Quoted 11 lines · Dave Hansen
Quoted 3 lines · Andi Kleen
I believe neither does mmio/msr normally (except maybe ftrace+tp_printk, but that will likely work because it shouldn't recurse more than once due to ftrace's reentry protection)
Can it do MMIO:
arch/x86/kernel/traps.c DEFINE_IDTENTRY(exc_device_not_available) · line 1146
Quoted 2 lines
arch/x86/kernel/traps.c +2
-0+DEFINE_IDTENTRY (exc_virtualization_exception) +{=======> HERE
Quoted 1 line
+ ret =
tdg_get_ve_info(&ve);Recursion isn't the problem. It would double-fault there, right?
Yes that's right. tp_printk already has a lot of other corner cases that break though, so it's not a real issue.
-Andi
Andy Lutomirski
maintainerRe: [RFC v2 08/32] x86/traps: Add #VE support for TDX guest
In reply to Dave HansenQuoted 5 lines · Dave Hansen
Quoted 3 lines · Andi Kleen
I believe neither does mmio/msr normally (except maybe ftrace+tp_printk, but that will likely work because it shouldn't recurse more than once due to ftrace's reentry protection)
Quoted 9 lines
Can it do MMIO:
arch/x86/kernel/traps.c DEFINE_IDTENTRY(exc_device_not_available) · line 1146
Quoted 2 lines
arch/x86/kernel/traps.c +2
-0+DEFINE_IDTENTRY (exc_virtualization_exception) +{=======> HERE
Quoted 1 line
+ ret =
tdg_get_ve_info(&ve);Recursion isn't the problem. It would double-fault there, right?
We should do the get_ve_info in a noinstr region.
RFC v2 9/32 x86/tdx: Add HLT support for TDX guest
In reply to Kuppuswamy SathyanarayananFrom: "Kirill A. Shutemov" <[email protected]>
Per Guest-Host-Communication Interface (GHCI) for Intel Trust Domain Extensions (Intel TDX) specification, sec 3.8, TDVMCALL[Instruction.HLT] provides HLT operation. Use it to implement halt() and safe_halt() paravirtualization calls.
The same TDVMCALL is used to handle #VE exception due to EXIT_REASON_HLT.
--- arch/x86/kernel/tdx.c | 44 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-)
arch/x86/kernel/tdx.c +37 -7
@@ -82,6 +82,27 @@ static void tdg_get_info(void) td_info.attributes = out.rdx; } +static __cpuidle void tdg_halt(void) +{ +u64 ret; + + ret =__tdvmcall (EXIT_REASON_HLT ,0 ,0 ,0 ,0 ,NULL ); + +/* It should never fail */ +BUG_ON (ret); +} + +static __cpuidle void tdg_safe_halt(void) +{ +/* +* Enable interrupts next to the TDVMCALL to avoid +* performance degradation. +*/ + asmvolatile ("sti\n\t" ); + +tdg_halt (); +} +unsigned long tdg_get_ve_info (struct ve_info *ve) {u64 ret;
@@ -111,13 +132,19 @@ unsigned long tdg_get_ve_info(struct ve_info *ve)int tdg_handle_virtualization_exception (struct pt_regs *regs,struct ve_info *ve) { -/* -* TODO: Add handler support for various #VE exit -* reasons. It will be added by other patches in -* the series. -*/ -pr_warn ("Unexpected #VE: %lld\n" , ve->exit_reason); -return -EFAULT ; +switch (ve->exit_reason) { +case EXIT_REASON_HLT : +tdg_halt (); +break ; +default : +pr_warn ("Unexpected #VE: %lld\n" , ve->exit_reason); +return -EFAULT ; + } + +/* After successful #VE handling, move the IP */ + regs->ip += ve->instr_len; + +return 0 ; }void __init tdx_early_init (void )
@@ -129,5 +156,8 @@ void __init tdx_early_init(void)tdg_get_info (); + pv_ops.irq.safe_halt = tdg_safe_halt; + pv_ops.irq.halt = tdg_halt; +pr_info ("TDX guest is initialized\n" ); }
Signature · 1 line
2.25.1
RFC v2 10/32 x86/tdx: Wire up KVM hypercalls
In reply to Kuppuswamy SathyanarayananFrom: "Kirill A. Shutemov" <[email protected]>
KVM hypercalls have to be wrapped into vendor-specific TDVMCALLs.
--- arch/x86/include/asm/kvm_para.h | 21 +++++++++++++++ arch/x86/include/asm/tdx.h | 39 ++++++++++++++++++++++++++++ arch/x86/kernel/tdcall.S | 7 +++++ arch/x86/kernel/tdx-kvm.c | 45 +++++++++++++++++++++++++++++++++ arch/x86/kernel/tdx.c | 4 +++ 5 files changed, 116 insertions(+) create mode 100644 arch/x86/kernel/tdx-kvm.c
arch/x86/include/asm/kvm_para.h +21 -0
@@ -6,6 +6,7 @@#include <asm/alternative.h> #include <linux/interrupt.h> #include <uapi/asm/kvm_para.h> +#include <asm/tdx.h> extern void kvmclock_init (void );
@@ -34,6 +35,10 @@ static inline bool kvm_check_and_clear_guest_paused(void)static inline long kvm_hypercall0 (unsigned int nr) {long ret; + +if (is_tdx_guest ()) +return tdx_kvm_hypercall0 (nr); + asm volatile(KVM_HYPERCALL : "=a"(ret) : "a"(nr)
@@ -44,6 +49,10 @@ static inline long kvm_hypercall0(unsigned int nr)static inline long kvm_hypercall1 (unsigned int nr,unsigned long p1) {long ret; + +if (is_tdx_guest ()) +return tdx_kvm_hypercall1 (nr, p1); + asm volatile(KVM_HYPERCALL : "=a"(ret) : "a"(nr), "b"(p1)
@@ -55,6 +64,10 @@ static inline long kvm_hypercall2(unsigned int nr, unsigned long p1, unsigned long p2) {long ret; + +if (is_tdx_guest ()) +return tdx_kvm_hypercall2 (nr, p1, p2); + asm volatile(KVM_HYPERCALL : "=a"(ret) : "a"(nr), "b"(p1), "c"(p2)
@@ -66,6 +79,10 @@ static inline long kvm_hypercall3(unsigned int nr, unsigned long p1, unsigned long p2, unsigned long p3) { long ret; + +if (is_tdx_guest ()) +return tdx_kvm_hypercall3 (nr, p1, p2, p3); + asm volatile(KVM_HYPERCALL : "=a"(ret) : "a"(nr), "b"(p1), "c"(p2), "d"(p3)
@@ -78,6 +95,10 @@ static inline long kvm_hypercall4(unsigned int nr, unsigned long p1, unsigned long p4) {long ret; + +if (is_tdx_guest ()) +return tdx_kvm_hypercall4 (nr, p1, p2, p3, p4); + asm volatile(KVM_HYPERCALL : "=a"(ret) : "a"(nr), "b"(p1), "c"(p2), "d"(p3), "S"(p4)
arch/x86/include/asm/tdx.h +39 -0
@@ -56,6 +56,16 @@ u64 __tdcall(u64 fn, u64 rcx, u64 rdx, u64 r8, u64 r9,/* Helper function used to request services from VMM */ u64 __tdvmcall (u64 fn,u64 r12,u64 r13,u64 r14,u64 r15,struct tdvmcall_output *out); +u64 __tdvmcall_vendor_kvm (u64 fn,u64 r12,u64 r13,u64 r14,u64 r15, +struct tdvmcall_output *out); + +long tdx_kvm_hypercall0 (unsigned int nr); +long tdx_kvm_hypercall1 (unsigned int nr,unsigned long p1); +long tdx_kvm_hypercall2 (unsigned int nr,unsigned long p1,unsigned long p2); +long tdx_kvm_hypercall3 (unsigned int nr,unsigned long p1,unsigned long p2, +unsigned long p3); +long tdx_kvm_hypercall4 (unsigned int nr,unsigned long p1,unsigned long p2, +unsigned long p3,unsigned long p4);#else // !CONFIG_INTEL_TDX_GUEST
@@ -66,6 +76,35 @@ static inline bool is_tdx_guest(void)static inline void tdx_early_init (void ) { }; +static inline long tdx_kvm_hypercall0 (unsigned int nr) +{ +return -ENODEV ; +} + +static inline long tdx_kvm_hypercall1 (unsigned int nr,unsigned long p1) +{ +return -ENODEV ; +} + +static inline long tdx_kvm_hypercall2 (unsigned int nr,unsigned long p1, +unsigned long p2) +{ +return -ENODEV ; +} + +static inline long tdx_kvm_hypercall3 (unsigned int nr,unsigned long p1, +unsigned long p2,unsigned long p3) +{ +return -ENODEV ; +} + +static inline long tdx_kvm_hypercall4 (unsigned int nr,unsigned long p1, +unsigned long p2,unsigned long p3, +unsigned long p4) +{ +return -ENODEV ; +} +#endif /* CONFIG_INTEL_TDX_GUEST */ #endif /* _ASM_X86_TDX_H */
arch/x86/kernel/tdcall.S +7 -0
@@ -11,6 +11,7 @@ * refer to TDX GHCI specification). */ #define TDVMCALL_EXPOSE_REGS_MASK 0xfc00 +#define TDVMCALL_VENDOR_KVM 0x4d564b2e584454 /* "TDX.KVM" */ /* * TDX guests use the TDCALL instruction to make
@@ -198,3 +199,9 @@ SYM_FUNC_START(__tdvmcall) call do_tdvmcall retq SYM_FUNC_END(__tdvmcall) + +SYM_FUNC_START(__tdvmcall_vendor_kvm) + movq $TDVMCALL_VENDOR_KVM, %r10 + call do_tdvmcall + retq +SYM_FUNC_END(__tdvmcall_vendor_kvm)
arch/x86/kernel/tdx-kvm.c new file +45 -0
45 lines not on this page · raw message
arch/x86/kernel/tdx.c +4 -0
@@ -8,6 +8,10 @@#include <linux/cpu.h> +#ifdef CONFIG_KVM_GUEST +#include "tdx-kvm.c" +#endif + static struct { unsigned int gpa_width; unsigned long attributes;
Signature · 1 line
2.25.1
Re: [RFC v2 10/32] x86/tdx: Wire up KVM hypercalls
Quoted 3 lines · Kuppuswamy Sathyanarayanan
From: "Kirill A. Shutemov" <[email protected]>
KVM hypercalls have to be wrapped into vendor-specific TDVMCALLs.
How about:
KVM hypercalls use the "vmcall" or "vmmcall" instructions. Although the ABI is similar, those instructions no longer function for TDX guests. Make TDVMCALLs instead of VMCALL/VMCALL.
Quoted 13 lines
arch/x86/include/asm/kvm_para.h +1
-0@@ -6,6 +6,7 @@#include <asm/alternative.h> #include <linux/interrupt.h> #include <uapi/asm/kvm_para.h> +#include <asm/tdx.h> extern void kvmclock_init (void );
arch/x86/include/asm/kvm_para.h static inline bool kvm_check_and_clear_guest_paused(void) · line 40
Quoted 6 lines
arch/x86/include/asm/kvm_para.h +3
-0@@ -34,6 +35,10 @@ static inline bool kvm_check_and_clear_guest_paused(void)static inline long kvm_hypercall0 (unsigned int nr) {long ret; + +if (is_tdx_guest ()) +return tdx_kvm_hypercall0 (nr);
... all of these look OK.
Quoted 14 lines
arch/x86/include/asm/kvm_para.h +0
-0#endif /* _ASM_X86_TDX_H */ arch/x86/kernel/tdcall.S +1
-0@@ -11,6 +11,7 @@ * refer to TDX GHCI specification). */ #define TDVMCALL_EXPOSE_REGS_MASK 0xfc00 +#define TDVMCALL_VENDOR_KVM 0x4d564b2e584454 /* "TDX.KVM" */ /* * TDX guests use the TDCALL instruction to make
arch/x86/kernel/tdcall.S SYM_FUNC_START(__tdvmcall) · line 207
Quoted 9 lines
arch/x86/kernel/tdcall.S +6
-0@@ -198,3 +199,9 @@ SYM_FUNC_START(__tdvmcall) call do_tdvmcall retq SYM_FUNC_END(__tdvmcall) + +SYM_FUNC_START(__tdvmcall_vendor_kvm) + movq $TDVMCALL_VENDOR_KVM, %r10 + call do_tdvmcall + retq +SYM_FUNC_END(__tdvmcall_vendor_kvm)
Granted, this is not a ton of assembly. But, it does look a bit weird.
It needs a comment and/or a mention in the changelog.
R10 is not part of the function call ABI, but it is a part of the TDVMCALL ABI. This little assembly wrapper lets us reuse do_tdvmcall() for both KVM-specific hypercalls TDVMCALL_VENDOR_KVM and the more generic __tdvmcalls.
arch/x86/kernel/tdx.c line 17
Quoted 13 lines
arch/x86/kernel/tdx.c +4
-0@@ -8,6 +8,10 @@#include <linux/cpu.h> +#ifdef CONFIG_KVM_GUEST +#include "tdx-kvm.c" +#endif + static struct { unsigned int gpa_width; unsigned long attributes;
I know KVM does weird stuff. But, this is *really* weird. Why are we #including a .c file into another .c file?
Re: [RFC v2 10/32] x86/tdx: Wire up KVM hypercalls
Quoted 2 lines · Dave Hansen
I know KVM does weird stuff. But, this is*really* weird. Why are we #including a .c file into another .c file?
I think Kirill implemented it this way to skip Makefile changes for it. I don't see any other KVM direct dependencies in tdx.c.
I will fix it in next version.
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Re: [RFC v2 10/32] x86/tdx: Wire up KVM hypercalls
Quoted 10 lines · Sathyanarayanan
Quoted 2 lines · Dave Hansen
I know KVM does weird stuff. But, this is*really* weird. Why are we #including a .c file into another .c file?
I think Kirill implemented it this way to skip
Makefilechanges for it. I don't see any other KVM direct dependencies intdx.c.I will fix it in next version.
This has to be compiled only for TDX+KVM.
Signature · 1 line
Kirill A. Shutemov
Re: [RFC v2 10/32] x86/tdx: Wire up KVM hypercalls
Quoted 1 line · Kirill A. Shutemov
This has to be compiled only for TDX+KVM.
Got it. So if we want to remove the "C" file include, we will have to add #ifdef CONFIG_KVM_GUEST in Makefile.
ifdef CONFIG_KVM_GUEST
obj-$(CONFIG_INTEL_TDX_GUEST) += tdx-kvm.o
#endif
Dave, do you prefer above change over "C" file include?
25 #ifdef CONFIG_KVM_GUEST
26 #include "tdx-kvm.c"
27 #endif
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Re: [RFC v2 10/32] x86/tdx: Wire up KVM hypercalls
Quoted 9 lines · Sathyanarayanan
Quoted 1 line · Kirill A. Shutemov
This has to be compiled only for TDX+KVM.
Got it. So if we want to remove the "C" file include, we will have to add #ifdef
CONFIG_KVM_GUESTinMakefile.ifdef
CONFIG_KVM_GUEST
obj-$(CONFIG_INTEL_TDX_GUEST) += tdx-kvm.o
#endif
Is there truly no dependency between CONFIG_KVM_GUEST and CONFIG_INTEL_TDX_GUEST?
If there isn't, then the way we do it is adding another (invisible) Kconfig variable to express the dependency for tdx-kvm.o:
config INTEL_TDX_GUEST_KVM
bool depends on KVM_GUEST && INTEL_TDX_GUEST
Re: [RFC v2 10/32] x86/tdx: Wire up KVM hypercalls
Quoted 7 lines · Dave Hansen
Quoted 6 lines · Sathyanarayanan
Quoted 1 line · Kirill A. Shutemov
This has to be compiled only for TDX+KVM.
Got it. So if we want to remove the "C" file include, we will have to add #ifdef
CONFIG_KVM_GUESTinMakefile.
Quoted 6 lines
Quoted 3 lines
ifdef
CONFIG_KVM_GUEST
obj-$(CONFIG_INTEL_TDX_GUEST) += tdx-kvm.o
#endifIs there truly no dependency between
CONFIG_KVM_GUESTandCONFIG_INTEL_TDX_GUEST?
We want to re-use TDX code with other hypervisors/guests as well. So we can't create direct dependency with CONFIG_KVM_GUEST in Kconfig.
Quoted 7 lines
If there isn't, then the way we do it is adding another (invisible)
Kconfigvariable to express the dependency for tdx-kvm.o:config
INTEL_TDX_GUEST_KVMbool depends on KVM_GUEST && INTEL_TDX_GUEST
Currently it will only be used for KVM hypercall code. Will it to be overkill to create a new config over #ifdefs for this use case ? But, if this is the preferred approach, I will go with this suggestion.
Quoted 1 line
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Re: [RFC v2 10/32] x86/tdx: Wire up KVM hypercalls
Quoted 10 lines · Sathyanarayanan
Quoted 6 lines
If there isn't, then the way we do it is adding another (invisible)
Kconfigvariable to express the dependency for tdx-kvm.o:config
INTEL_TDX_GUEST_KVM
bool
depends onKVM_GUEST&&INTEL_TDX_GUESTCurrently it will only be used for KVM hypercall code. Will it to be overkill to create a new config over #ifdefs for this use case ? But, if this is the preferred approach, I will go with this suggestion.
You'll see this done lots of different (valid) ways over the kernel. (#ifdef'd #including C files is not one of them.)
*My* preference is to use Kconfig in the way I described. It keeps makefiles and #ifdef's clean and obvious, relegating the logic to Kconfig.
RFC 1/1 x86/tdx: Wire up KVM hypercalls
From: "Kirill A. Shutemov" <[email protected]>
KVM hypercalls use the "vmcall" or "vmmcall" instructions. Although the ABI is similar, those instructions no longer function for TDX guests. Make vendor specififc TDVMCALLs instead of VMCALL.
---
Changes since RFC v2:
* Introduced INTEL_TDX_GUEST_KVM config for TDX+KVM related changes.
* Removed "C" include file.
* Fixed commit log as per Dave's comments.
arch/x86/Kconfig | 6 +++++ arch/x86/include/asm/kvm_para.h | 21 +++++++++++++++ arch/x86/include/asm/tdx.h | 41 ++++++++++++++++++++++++++++ arch/x86/kernel/Makefile | 1 + arch/x86/kernel/tdcall.S | 20 ++++++++++++++ arch/x86/kernel/tdx-kvm.c | 48 +++++++++++++++++++++++++++++++++ 6 files changed, 137 insertions(+) create mode 100644 arch/x86/kernel/tdx-kvm.c
arch/x86/Kconfig +6 -0
@@ -886,6 +886,12 @@ config INTEL_TDX_GUEST run in a CPU mode that protects the confidentiality of TD memory contents and the TD’s CPU state from other software, including VMM. +config INTEL_TDX_GUEST_KVM + def_bool y + depends on KVM_GUEST && INTEL_TDX_GUEST + help + This option enables KVM specific hypercalls in TDX guest. + endif #HYPERVISOR_GUEST source "arch/x86/Kconfig.cpu"
arch/x86/include/asm/kvm_para.h +21 -0
@@ -6,6 +6,7 @@#include <asm/alternative.h> #include <linux/interrupt.h> #include <uapi/asm/kvm_para.h> +#include <asm/tdx.h> extern void kvmclock_init (void );
@@ -34,6 +35,10 @@ static inline bool kvm_check_and_clear_guest_paused(void)static inline long kvm_hypercall0 (unsigned int nr) {long ret; + +if (is_tdx_guest ()) +return tdx_kvm_hypercall0 (nr); + asm volatile(KVM_HYPERCALL : "=a"(ret) : "a"(nr)
@@ -44,6 +49,10 @@ static inline long kvm_hypercall0(unsigned int nr)static inline long kvm_hypercall1 (unsigned int nr,unsigned long p1) {long ret; + +if (is_tdx_guest ()) +return tdx_kvm_hypercall1 (nr, p1); + asm volatile(KVM_HYPERCALL : "=a"(ret) : "a"(nr), "b"(p1)
@@ -55,6 +64,10 @@ static inline long kvm_hypercall2(unsigned int nr, unsigned long p1, unsigned long p2) {long ret; + +if (is_tdx_guest ()) +return tdx_kvm_hypercall2 (nr, p1, p2); + asm volatile(KVM_HYPERCALL : "=a"(ret) : "a"(nr), "b"(p1), "c"(p2)
@@ -66,6 +79,10 @@ static inline long kvm_hypercall3(unsigned int nr, unsigned long p1, unsigned long p2, unsigned long p3) { long ret; + +if (is_tdx_guest ()) +return tdx_kvm_hypercall3 (nr, p1, p2, p3); + asm volatile(KVM_HYPERCALL : "=a"(ret) : "a"(nr), "b"(p1), "c"(p2), "d"(p3)
@@ -78,6 +95,10 @@ static inline long kvm_hypercall4(unsigned int nr, unsigned long p1, unsigned long p4) {long ret; + +if (is_tdx_guest ()) +return tdx_kvm_hypercall4 (nr, p1, p2, p3, p4); + asm volatile(KVM_HYPERCALL : "=a"(ret) : "a"(nr), "b"(p1), "c"(p2), "d"(p3), "S"(p4)
arch/x86/include/asm/tdx.h +41 -0
45 lines not on this page · raw message
arch/x86/kernel/Makefile +1 -0
@@ -128,6 +128,7 @@ obj-$(CONFIG_X86_PMEM_LEGACY_DEVICE) += pmem.o obj-$(CONFIG_JAILHOUSE_GUEST) += jailhouse.o obj-$(CONFIG_INTEL_TDX_GUEST) += tdcall.o tdx.o +obj-$(CONFIG_INTEL_TDX_GUEST_KVM) += tdx-kvm.o obj-$(CONFIG_EISA) += eisa.o obj-$(CONFIG_PCSPKR_PLATFORM) += pcspeaker.o
arch/x86/kernel/tdcall.S +20 -0
@@ -25,6 +25,8 @@ TDG_R12 | TDG_R13 | \ TDG_R14 | TDG_R15 ) +#define TDVMCALL_VENDOR_KVM 0x4d564b2e584454 /* "TDX.KVM" */ + /* * TDX guests use the TDCALL instruction to make requests to the * TDX module and hypercalls to the VMM. It is supported in
@@ -213,3 +215,21 @@ SYM_FUNC_START(__tdx_hypercall) call do_tdx_hypercall retq SYM_FUNC_END(__tdx_hypercall) + +#ifdef CONFIG_INTEL_TDX_GUEST_KVM +/* + * Helper function for KVM vendor TDVMCALLs. This assembly wrapper + * lets us reuse do_tdvmcall() for KVM-specific hypercalls ( + * TDVMCALL_VENDOR_KVM). + */ +SYM_FUNC_START(__tdx_hypercall_vendor_kvm) + /* + * R10 is not part of the function call ABI, but it is a part + * of the TDVMCALL ABI. So set it before making call to the + * do_tdx_hypercall(). + */ + movq $TDVMCALL_VENDOR_KVM, %r10 + call do_tdx_hypercall + retq +SYM_FUNC_END(__tdx_hypercall_vendor_kvm) +#endif /* CONFIG_INTEL_TDX_GUEST_KVM */
arch/x86/kernel/tdx-kvm.c new file +48 -0
48 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [RFC v2-fix 1/1] x86/tdx: Wire up KVM hypercalls
Question for KVM folks: Should all of these guest patches say:
"x86/tdx/guest:" or something? It seems like that would put us all in the right frame of mind as we review these. It's kinda easy (for me at least) to get lost about which side I'm looking at sometimes.
Quoted 5 lines · Kuppuswamy Sathyanarayanan
From: "Kirill A. Shutemov" <[email protected]>
KVM hypercalls use the "vmcall" or "vmmcall" instructions. Although the ABI is similar, those instructions no longer function for TDX guests. Make vendor specififc TDVMCALLs
"vendor-specific"
Hyphen and spelling ^
Quoted 1 line
instead of VMCALL.
This would also be a great place to say:
This enables TDX guests to run with KVM acting as the hypervisor. TDX guests running under other hypervisors will continue to use those hypervisors hypercalls.
Quoted 5 lines
[Isaku: proposed KVM VENDOR string]Signed-off-by Kirill A. Shutemov <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>Reviewed-by Andi Kleen <[email protected]>Signed-off-by Kuppuswamy Sathyanarayanan <[email protected]>
This SoB chain is odd. Kirill wrote this, sent it to Isaku, who sent it to Sathya?
arch/x86/Kconfig config INTEL_TDX_GUEST · line 893
Quoted 13 lines
arch/x86/Kconfig +5
-0@@ -886,6 +886,12 @@ config INTEL_TDX_GUEST run in a CPU mode that protects the confidentiality of TD memory contents and the TD’s CPU state from other software, including VMM. +config INTEL_TDX_GUEST_KVM + def_bool y + depends on KVM_GUEST && INTEL_TDX_GUEST + help + This option enables KVM specific hypercalls in TDX guest.
For something that's not user-visible, I'd probably just add a Kconfig comment rather than help text.
...
arch/x86/kernel/Makefile obj-$(CONFIG_X86_PMEM_LEGACY_DEVICE) += pmem.o · line 131
Quoted 9 lines
arch/x86/kernel/Makefile +1
-0@@ -128,6 +128,7 @@ obj-$(CONFIG_X86_PMEM_LEGACY_DEVICE) += pmem.o obj-$(CONFIG_JAILHOUSE_GUEST) += jailhouse.o obj-$(CONFIG_INTEL_TDX_GUEST) += tdcall.o tdx.o +obj-$(CONFIG_INTEL_TDX_GUEST_KVM) += tdx-kvm.o
Is the indentation consistent with the other items near "tdx-kvm.o" in the Makefile?
...
arch/x86/kernel/Makefile obj-$(CONFIG_X86_PMEM_LEGACY_DEVICE) += pmem.o
Quoted 7 lines
arch/x86/kernel/Makefile +7
-0+/* Used by kvm_hypercall4() to trigger hypercall in TDX guest */ +long tdx_kvm_hypercall4(unsigned int nr, unsigned long p1, unsigned long p2, + unsigned long p3, unsigned long p4) +{ + return tdx_kvm_hypercall(nr, p1, p2, p3, p4); +} +EXPORT_SYMBOL_GPL(tdx_kvm_hypercall4);
I always forget that KVM code is goofy and needs to have things in C files so you can export the symbols. Could you add a sentence to the changelog to this effect?
Code-wise, this is fine. Just a few tweaks and I'll be happy to ack this one.
Sean Christopherson
maintainerRe: [RFC v2-fix 1/1] x86/tdx: Wire up KVM hypercalls
Quoted 2 lines · Dave Hansen
Question for KVM folks: Should all of these guest patches say:
"x86/tdx/guest:" or something?
x86/tdx is fine. The KVM convention is to use "KVM: xxx:" for KVM host code and "x86/kvm" for KVM guest code. E.g. for KVM TDX host code, the subjects will be "KVM: x86:", "KVM: VMX:" or "KVM: TDX:".
The one I really don't like is using "tdg_" as the acronym for guest functions. I find that really confusion and grep-unfriendly.
Re: [RFC v2-fix 1/1] x86/tdx: Wire up KVM hypercalls
In reply to Dave HansenQuoted 8 lines · Dave Hansen
Question for KVM folks: Should all of these guest patches say:
"x86/tdx/guest:" or something? It seems like that would put us all in the right frame of mind as we review these. It's kinda easy (for me at least) to get lost about which side I'm looking at sometimes.Quoted 2 lines · Kuppuswamy Sathyanarayanan
From: "Kirill A. Shutemov" <[email protected]>
Quoted 7 lines
Quoted 3 lines
KVM hypercalls use the "vmcall" or "vmmcall" instructions. Although the ABI is similar, those instructions no longer function for TDX guests. Make vendor specififc TDVMCALLs
"vendor-specific"
Hyphen and spelling ^
I will fix it next version.
Quoted 8 lines
Quoted 2 lines
instead of VMCALL.
This would also be a great place to say:
This enables TDX guests to run with KVM acting as the hypervisor. TDX guests running under other hypervisors will continue to use those hypervisors hypercalls.
I will include it.
Quoted 9 lines
Quoted 6 lines
[Isaku: proposed KVM VENDOR string]Signed-off-by Kirill A. Shutemov <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>Reviewed-by Andi Kleen <[email protected]>Signed-off-by Kuppuswamy Sathyanarayanan <[email protected]>This SoB chain is odd. Kirill wrote this, sent it to Isaku, who sent it to Sathya?
Initially we have used "0" as vendor ID for KVM. But Isaku proposed a new value for it and sent a patch to fix it. But, I did not want to carry it as separate patch (for one line change). So I have merged his change with this patch, and added his signed-off with comment ([Isaku: proposed KVM VENDOR string])
+#define TDVMCALL_VENDOR_KVM 0x4d564b2e584454 /* "TDX.KVM" */
Quoted 9 lines
Quoted 9 lines
arch/x86/Kconfig +0
-0@@ -886,6 +886,12 @@ config INTEL_TDX_GUEST run in a CPU mode that protects the confidentiality of TD memory contents and the TD’s CPU state from other software, including VMM.
Quoted 8 lines
arch/x86/Kconfig config INTEL_TDX_GUEST · line 893
Quoted 5 lines
arch/x86/Kconfig +5
-0+config INTEL_TDX_GUEST_KVM + def_bool y + depends on KVM_GUEST && INTEL_TDX_GUEST + help + This option enables KVM specific hypercalls in TDX guest.For something that's not user-visible, I'd probably just add a
Kconfigcomment rather than help text.
If it is the preferred approach, I can remove it.
Quoted 8 lines
...
Quoted 6 lines
arch/x86/kernel/Makefile +0
-0@@ -128,6 +128,7 @@ obj-$(CONFIG_X86_PMEM_LEGACY_DEVICE) += pmem.o
Quoted 6 lines
arch/x86/kernel/Makefile obj-$(CONFIG_X86_PMEM_LEGACY_DEVICE) += pmem.o · line 131
Quoted 3 lines
arch/x86/kernel/Makefile +1
-0obj-$(CONFIG_JAILHOUSE_GUEST) += jailhouse.o obj-$(CONFIG_INTEL_TDX_GUEST) += tdcall.o tdx.o +obj-$(CONFIG_INTEL_TDX_GUEST_KVM) += tdx-kvm.oIs the indentation consistent with the other items near "tdx-kvm.o" in the
Makefile?
Yes. For longer config names, common indentation is not maintained. Please check the PMEM example.
126 obj-$(CONFIG_PARAVIRT_CLOCK) += pvclock.o
127 obj-$(CONFIG_X86_PMEM_LEGACY_DEVICE) += pmem.o 128
129 obj-$(CONFIG_JAILHOUSE_GUEST) += jailhouse.o
130 obj-$(CONFIG_INTEL_TDX_GUEST) += tdcall.o tdx.o
131 obj-$(CONFIG_INTEL_TDX_GUEST_KVM) += tdx-kvm.o
Quoted 14 lines
...
arch/x86/kernel/Makefile
Quoted 7 lines
arch/x86/kernel/Makefile +7
-0+/* Used by kvm_hypercall4() to trigger hypercall in TDX guest */ +long tdx_kvm_hypercall4(unsigned int nr, unsigned long p1, unsigned long p2, + unsigned long p3, unsigned long p4) +{ + return tdx_kvm_hypercall(nr, p1, p2, p3, p4); +} +EXPORT_SYMBOL_GPL(tdx_kvm_hypercall4);I always forget that KVM code is goofy and needs to have things in C files so you can export the symbols. Could you add a sentence to the changelog to this effect?
Quoted 2 lines
Code-wise, this is fine. Just a few tweaks and I'll be happy to ack this one.
Will add it.
Since KVM hypercall functions can be included and called from kernel modules, export tdx_kvm_hypercall*() functions to avoid symbol errors
Quoted 1 line
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Re: [RFC v2-fix 1/1] x86/tdx: Wire up KVM hypercalls
Quoted 10 lines · Sathyanarayanan
Quoted 9 lines
Quoted 6 lines
[Isaku: proposed KVM VENDOR string]Signed-off-by Kirill A. Shutemov <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>Reviewed-by Andi Kleen <[email protected]>Signed-off-by Kuppuswamy SathyanarayananThis SoB chain is odd. Kirill wrote this, sent it to Isaku, who sent it to Sathya?
Quoted 7 lines
Initially we have used "0" as vendor ID for KVM. But Isaku proposed a new value for it and sent a patch to fix it. But, I did not want to carry it as separate patch (for one line change). So I have merged his change with this patch, and added his signed-off with comment ([Isaku: proposed KVM VENDOR string])
+#define TDVMCALL_VENDOR_KVM
0x4d564b2e584454/* "TDX.KVM" */
That's a combined Co-developed-by+Signed-off-by situation. You don't add a bare SoB for that.
But, seriously, you don't need to preserve a SoB for a one-line patch. Just pull the line in and make a note in the changelog.
Re: [RFC v2-fix 1/1] x86/tdx: Wire up KVM hypercalls
Quoted 2 lines · Dave Hansen
But, seriously, you don't need to preserve a SoB for a one-line patch. Just pull the line in and make a note in the changelog.
Ok. Makes sense. I will leave the comment and remove SOB from Isaku.
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
RFC 1/1 x86/tdx: Wire up KVM hypercalls
In reply to Dave HansenFrom: "Kirill A. Shutemov" <[email protected]>
KVM hypercalls use the "vmcall" or "vmmcall" instructions. Although the ABI is similar, those instructions no longer function for TDX guests. Make vendor-specific TDVMCALLs instead of VMCALL. This enables TDX guests to run with KVM acting as the hypervisor. TDX guests running under other hypervisors will continue to use those hypervisor's hypercalls.
Since KVM hypercall functions can be included and called from kernel modules, export tdx_kvm_hypercall*() functions to avoid symbol errors
---
Changes since RFC v2-fix:
* Removed "user help" for INTEL_TDX_GUEST_KVM config option and added a comment for it.
* Added details about exporting symbols in the commit log.
* Removed Isaku's sign-off.
Changes since RFC v2:
* Introduced INTEL_TDX_GUEST_KVM config for TDX+KVM related changes.
* Removed "C" include file.
* Fixed commit log as per Dave's comments.
arch/x86/Kconfig | 5 ++++ arch/x86/include/asm/kvm_para.h | 21 +++++++++++++++ arch/x86/include/asm/tdx.h | 41 ++++++++++++++++++++++++++++ arch/x86/kernel/Makefile | 1 + arch/x86/kernel/tdcall.S | 20 ++++++++++++++ arch/x86/kernel/tdx-kvm.c | 48 +++++++++++++++++++++++++++++++++ 6 files changed, 136 insertions(+) create mode 100644 arch/x86/kernel/tdx-kvm.c
arch/x86/Kconfig +5 -0
@@ -886,6 +886,11 @@ config INTEL_TDX_GUEST run in a CPU mode that protects the confidentiality of TD memory contents and the TD’s CPU state from other software, including VMM. +# This option enables KVM specific hypercalls in TDX guest. +config INTEL_TDX_GUEST_KVM + def_bool y + depends on KVM_GUEST && INTEL_TDX_GUEST + endif #HYPERVISOR_GUEST source "arch/x86/Kconfig.cpu"
arch/x86/include/asm/kvm_para.h +21 -0
@@ -6,6 +6,7 @@#include <asm/alternative.h> #include <linux/interrupt.h> #include <uapi/asm/kvm_para.h> +#include <asm/tdx.h> extern void kvmclock_init (void );
@@ -34,6 +35,10 @@ static inline bool kvm_check_and_clear_guest_paused(void)static inline long kvm_hypercall0 (unsigned int nr) {long ret; + +if (is_tdx_guest ()) +return tdx_kvm_hypercall0 (nr); + asm volatile(KVM_HYPERCALL : "=a"(ret) : "a"(nr)
@@ -44,6 +49,10 @@ static inline long kvm_hypercall0(unsigned int nr)static inline long kvm_hypercall1 (unsigned int nr,unsigned long p1) {long ret; + +if (is_tdx_guest ()) +return tdx_kvm_hypercall1 (nr, p1); + asm volatile(KVM_HYPERCALL : "=a"(ret) : "a"(nr), "b"(p1)
@@ -55,6 +64,10 @@ static inline long kvm_hypercall2(unsigned int nr, unsigned long p1, unsigned long p2) {long ret; + +if (is_tdx_guest ()) +return tdx_kvm_hypercall2 (nr, p1, p2); + asm volatile(KVM_HYPERCALL : "=a"(ret) : "a"(nr), "b"(p1), "c"(p2)
@@ -66,6 +79,10 @@ static inline long kvm_hypercall3(unsigned int nr, unsigned long p1, unsigned long p2, unsigned long p3) { long ret; + +if (is_tdx_guest ()) +return tdx_kvm_hypercall3 (nr, p1, p2, p3); + asm volatile(KVM_HYPERCALL : "=a"(ret) : "a"(nr), "b"(p1), "c"(p2), "d"(p3)
@@ -78,6 +95,10 @@ static inline long kvm_hypercall4(unsigned int nr, unsigned long p1, unsigned long p4) {long ret; + +if (is_tdx_guest ()) +return tdx_kvm_hypercall4 (nr, p1, p2, p3, p4); + asm volatile(KVM_HYPERCALL : "=a"(ret) : "a"(nr), "b"(p1), "c"(p2), "d"(p3), "S"(p4)
arch/x86/include/asm/tdx.h +41 -0
45 lines not on this page · raw message
arch/x86/kernel/Makefile +1 -0
@@ -128,6 +128,7 @@ obj-$(CONFIG_X86_PMEM_LEGACY_DEVICE) += pmem.o obj-$(CONFIG_JAILHOUSE_GUEST) += jailhouse.o obj-$(CONFIG_INTEL_TDX_GUEST) += tdcall.o tdx.o +obj-$(CONFIG_INTEL_TDX_GUEST_KVM) += tdx-kvm.o obj-$(CONFIG_EISA) += eisa.o obj-$(CONFIG_PCSPKR_PLATFORM) += pcspeaker.o
arch/x86/kernel/tdcall.S +20 -0
@@ -25,6 +25,8 @@ TDG_R12 | TDG_R13 | \ TDG_R14 | TDG_R15 ) +#define TDVMCALL_VENDOR_KVM 0x4d564b2e584454 /* "TDX.KVM" */ + /* * TDX guests use the TDCALL instruction to make requests to the * TDX module and hypercalls to the VMM. It is supported in
@@ -213,3 +215,21 @@ SYM_FUNC_START(__tdx_hypercall) call do_tdx_hypercall retq SYM_FUNC_END(__tdx_hypercall) + +#ifdef CONFIG_INTEL_TDX_GUEST_KVM +/* + * Helper function for KVM vendor TDVMCALLs. This assembly wrapper + * lets us reuse do_tdvmcall() for KVM-specific hypercalls ( + * TDVMCALL_VENDOR_KVM). + */ +SYM_FUNC_START(__tdx_hypercall_vendor_kvm) + /* + * R10 is not part of the function call ABI, but it is a part + * of the TDVMCALL ABI. So set it before making call to the + * do_tdx_hypercall(). + */ + movq $TDVMCALL_VENDOR_KVM, %r10 + call do_tdx_hypercall + retq +SYM_FUNC_END(__tdx_hypercall_vendor_kvm) +#endif /* CONFIG_INTEL_TDX_GUEST_KVM */
arch/x86/kernel/tdx-kvm.c new file +48 -0
48 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [RFC v2-fix-v2 1/1] x86/tdx: Wire up KVM hypercalls
Quoted 9 lines · Kuppuswamy Sathyanarayanan
From: "Kirill A. Shutemov" <[email protected]>
KVM hypercalls use the "vmcall" or "vmmcall" instructions. Although the ABI is similar, those instructions no longer function for TDX guests. Make vendor-specific TDVMCALLs instead of VMCALL. This enables TDX guests to run with KVM acting as the hypervisor. TDX guests running under other hypervisors will continue to use those hypervisor's hypercalls.
Well, I screwed this up when I typed it too, but it is:
TDX guests running under other hypervisors will continue to use those hypervisors' hypercalls.
I hate how that reads, but oh well.
Quoted 3 lines
Since KVM hypercall functions can be included and called from kernel modules, export tdx_kvm_hypercall*() functions to avoid symbol errors
No, you're not avoiding errors, you're exporting the symbol so it can be *USED*. The error comes from it not being exported.
It also helps to be specific here: Export tdx_kvm_hypercall*() to make the symbols visible to kvm.ko.
Quoted 4 lines
[Isaku Yamahata: proposed KVM VENDOR string]Signed-off-by Kirill A. Shutemov <[email protected]>Reviewed-by Andi Kleen <[email protected]>Signed-off-by Kuppuswamy Sathyanarayanan <[email protected]>
Also, FWIW, if you did this in the header:
+static inline long tdx_kvm_hypercall0(unsigned int nr) +{
+ return tdx_kvm_hypercall(nr, 0, 0, 0, 0);
+}
You could get away with just exporting tdx_kvm_hypercall() instead of 4 symbols. The rest of the code would look the same.
RFC 1/1 x86/tdx: Wire up KVM hypercalls
From: "Kirill A. Shutemov" <[email protected]>
KVM hypercalls use the "vmcall" or "vmmcall" instructions. Although the ABI is similar, those instructions no longer function for TDX guests. Make vendor-specific TDVMCALLs instead of VMCALL. This enables TDX guests to run with KVM acting as the hypervisor. TDX guests running under other hypervisors will continue to use those hypervisors' hypercalls.
Since KVM driver can be built as a kernel module, export tdx_kvm_hypercall*() to make the symbols visible to kvm.ko.
--- arch/x86/Kconfig | 5 +++ arch/x86/include/asm/kvm_para.h | 21 ++++++++++ arch/x86/include/asm/tdx.h | 68 +++++++++++++++++++++++++++++++++ arch/x86/kernel/tdcall.S | 26 +++++++++++++ 4 files changed, 120 insertions(+)
arch/x86/Kconfig +5 -0
@@ -886,6 +886,11 @@ config INTEL_TDX_GUEST run in a CPU mode that protects the confidentiality of TD memory contents and the TD’s CPU state from other software, including VMM. +# This option enables KVM specific hypercalls in TDX guest. +config INTEL_TDX_GUEST_KVM + def_bool y + depends on KVM_GUEST && INTEL_TDX_GUEST + endif #HYPERVISOR_GUEST source "arch/x86/Kconfig.cpu"
arch/x86/include/asm/kvm_para.h +21 -0
@@ -6,6 +6,7 @@#include <asm/alternative.h> #include <linux/interrupt.h> #include <uapi/asm/kvm_para.h> +#include <asm/tdx.h> extern void kvmclock_init (void );
@@ -34,6 +35,10 @@ static inline bool kvm_check_and_clear_guest_paused(void)static inline long kvm_hypercall0 (unsigned int nr) {long ret; + +if (is_tdx_guest ()) +return tdx_kvm_hypercall0 (nr); + asm volatile(KVM_HYPERCALL : "=a"(ret) : "a"(nr)
@@ -44,6 +49,10 @@ static inline long kvm_hypercall0(unsigned int nr)static inline long kvm_hypercall1 (unsigned int nr,unsigned long p1) {long ret; + +if (is_tdx_guest ()) +return tdx_kvm_hypercall1 (nr, p1); + asm volatile(KVM_HYPERCALL : "=a"(ret) : "a"(nr), "b"(p1)
@@ -55,6 +64,10 @@ static inline long kvm_hypercall2(unsigned int nr, unsigned long p1, unsigned long p2) {long ret; + +if (is_tdx_guest ()) +return tdx_kvm_hypercall2 (nr, p1, p2); + asm volatile(KVM_HYPERCALL : "=a"(ret) : "a"(nr), "b"(p1), "c"(p2)
@@ -66,6 +79,10 @@ static inline long kvm_hypercall3(unsigned int nr, unsigned long p1, unsigned long p2, unsigned long p3) { long ret; + +if (is_tdx_guest ()) +return tdx_kvm_hypercall3 (nr, p1, p2, p3); + asm volatile(KVM_HYPERCALL : "=a"(ret) : "a"(nr), "b"(p1), "c"(p2), "d"(p3)
@@ -78,6 +95,10 @@ static inline long kvm_hypercall4(unsigned int nr, unsigned long p1, unsigned long p4) {long ret; + +if (is_tdx_guest ()) +return tdx_kvm_hypercall4 (nr, p1, p2, p3, p4); + asm volatile(KVM_HYPERCALL : "=a"(ret) : "a"(nr), "b"(p1), "c"(p2), "d"(p3), "S"(p4)
arch/x86/include/asm/tdx.h +68 -0
72 lines not on this page · raw message
arch/x86/kernel/tdcall.S +26 -0
@@ -3,6 +3,7 @@ #include <asm/asm.h> #include <asm/frame.h> #include <asm/unwind_hints.h> +#include <asm/export.h> #include <linux/linkage.h> #include <linux/bits.h>
@@ -25,6 +26,8 @@ TDG_R12 | TDG_R13 | \ TDG_R14 | TDG_R15 ) +#define TDVMCALL_VENDOR_KVM 0x4d564b2e584454 /* "TDX.KVM" */ + /* * TDX guests use the TDCALL instruction to make requests to the * TDX module and hypercalls to the VMM. It is supported in
@@ -212,3 +215,26 @@ SYM_FUNC_START(__tdx_hypercall) FRAME_END retq SYM_FUNC_END(__tdx_hypercall) + +#ifdef CONFIG_INTEL_TDX_GUEST_KVM + +/* + * Helper function for KVM vendor TDVMCALLs. This assembly wrapper + * lets us reuse do_tdvmcall() for KVM-specific hypercalls ( + * TDVMCALL_VENDOR_KVM). + */ +SYM_FUNC_START(__tdx_hypercall_vendor_kvm) + FRAME_BEGIN + /* + * R10 is not part of the function call ABI, but it is a part + * of the TDVMCALL ABI. So set it before making call to the + * do_tdx_hypercall(). + */ + movq $TDVMCALL_VENDOR_KVM, %r10 + call do_tdx_hypercall + FRAME_END + retq +SYM_FUNC_END(__tdx_hypercall_vendor_kvm) + +EXPORT_SYMBOL(__tdx_hypercall_vendor_kvm); +#endif /* CONFIG_INTEL_TDX_GUEST_KVM */
Signature · 1 line
2.25.1
Re: [RFC v2-fix-v3 1/1] x86/tdx: Wire up KVM hypercalls
Sorry, I have missed to include a change log.
* Removed tdx-kvm.c and implemented tdx_kvm_hypercall*() functions in tdx.h
* Exported __tdx_hypercall_vendor_kvm() symbol for kvm.ko.
* Fixed commit log as per Dave's suggestion.
* Added Reviewed-by from Dave
* Added FRAME_BEGIN/FRAME_END for __tdx_hypercall_vendor_kvm() to fix compiler warnings.
Quoted 237 lines · Kuppuswamy Sathyanarayanan
From: "Kirill A. Shutemov" <[email protected]>
KVM hypercalls use the "vmcall" or "vmmcall" instructions. Although the ABI is similar, those instructions no longer function for TDX guests. Make vendor-specific TDVMCALLs instead of VMCALL. This enables TDX guests to run with KVM acting as the hypervisor. TDX guests running under other hypervisors will continue to use those hypervisors' hypercalls.
Since KVM driver can be built as a kernel module, export tdx_kvm_hypercall*() to make the symbols visible to kvm.ko.
[Isaku Yamahata: proposed KVM VENDOR string]Signed-off-by Kirill A. Shutemov <[email protected]>Reviewed-by Andi Kleen <[email protected]>Reviewed-by Dave Hansen <[email protected]>Signed-off-by Kuppuswamy Sathyanarayanan <[email protected]>--- arch/x86/Kconfig | 5 +++ arch/x86/include/asm/kvm_para.h | 21 ++++++++++ arch/x86/include/asm/tdx.h | 68 +++++++++++++++++++++++++++++++++ arch/x86/kernel/tdcall.S | 26 +++++++++++++ 4 files changed, 120 insertions(+)arch/x86/Kconfig +5
-0@@ -886,6 +886,11 @@ config INTEL_TDX_GUEST run in a CPU mode that protects the confidentiality of TD memory contents and the TD’s CPU state from other software, including VMM. +# This option enables KVM specific hypercalls in TDX guest. +config INTEL_TDX_GUEST_KVM + def_bool y + depends on KVM_GUEST && INTEL_TDX_GUEST + endif #HYPERVISOR_GUEST source "arch/x86/Kconfig.cpu"arch/x86/include/asm/kvm_para.h +21
-0@@ -6,6 +6,7 @@#include <asm/alternative.h> #include <linux/interrupt.h> #include <uapi/asm/kvm_para.h> +#include <asm/tdx.h> extern void kvmclock_init (void );@@ -34,6 +35,10 @@ static inline bool kvm_check_and_clear_guest_paused(void)static inline long kvm_hypercall0 (unsigned int nr) {long ret; + +if (is_tdx_guest ()) +return tdx_kvm_hypercall0 (nr); + asm volatile(KVM_HYPERCALL : "=a"(ret) : "a"(nr)@@ -44,6 +49,10 @@ static inline long kvm_hypercall0(unsigned int nr)static inline long kvm_hypercall1 (unsigned int nr,unsigned long p1) {long ret; + +if (is_tdx_guest ()) +return tdx_kvm_hypercall1 (nr, p1); + asm volatile(KVM_HYPERCALL : "=a"(ret) : "a"(nr), "b"(p1)@@ -55,6 +64,10 @@ static inline long kvm_hypercall2(unsigned int nr, unsigned long p1, unsigned long p2) {long ret; + +if (is_tdx_guest ()) +return tdx_kvm_hypercall2 (nr, p1, p2); + asm volatile(KVM_HYPERCALL : "=a"(ret) : "a"(nr), "b"(p1), "c"(p2)@@ -66,6 +79,10 @@ static inline long kvm_hypercall3(unsigned int nr, unsigned long p1, unsigned long p2, unsigned long p3) { long ret; + +if (is_tdx_guest ()) +return tdx_kvm_hypercall3 (nr, p1, p2, p3); + asm volatile(KVM_HYPERCALL : "=a"(ret) : "a"(nr), "b"(p1), "c"(p2), "d"(p3)@@ -78,6 +95,10 @@ static inline long kvm_hypercall4(unsigned int nr, unsigned long p1, unsigned long p4) {long ret; + +if (is_tdx_guest ()) +return tdx_kvm_hypercall4 (nr, p1, p2, p3, p4); + asm volatile(KVM_HYPERCALL : "=a"(ret) : "a"(nr), "b"(p1), "c"(p2), "d"(p3), "S"(p4)arch/x86/include/asm/tdx.h +68
-0@@ -73,4 +73,72 @@ static inline void tdx_early_init(void) { };#endif /* CONFIG_INTEL_TDX_GUEST */ +#ifdef CONFIG_INTEL_TDX_GUEST_KVM +u64 __tdx_hypercall_vendor_kvm (u64 fn,u64 r12,u64 r13,u64 r14, +u64 r15,struct tdx_hypercall_output *out); + +/* Used by kvm_hypercall0() to trigger hypercall in TDX guest */ +static inline long tdx_kvm_hypercall0 (unsigned int nr) +{ +return __tdx_hypercall_vendor_kvm (nr,0 ,0 ,0 ,0 ,NULL ); +} + +/* Used by kvm_hypercall1() to trigger hypercall in TDX guest */ +static inline long tdx_kvm_hypercall1 (unsigned int nr,unsigned long p1) +{ +return __tdx_hypercall_vendor_kvm (nr, p1,0 ,0 ,0 ,NULL ); +} + +/* Used by kvm_hypercall2() to trigger hypercall in TDX guest */ +static inline long tdx_kvm_hypercall2 (unsigned int nr,unsigned long p1, +unsigned long p2) +{ +return __tdx_hypercall_vendor_kvm (nr, p1, p2,0 ,0 ,NULL ); +} + +/* Used by kvm_hypercall3() to trigger hypercall in TDX guest */ +static inline long tdx_kvm_hypercall3 (unsigned int nr,unsigned long p1, +unsigned long p2,unsigned long p3) +{ +return __tdx_hypercall_vendor_kvm (nr, p1, p2, p3,0 ,NULL ); +} + +/* Used by kvm_hypercall4() to trigger hypercall in TDX guest */ +static inline long tdx_kvm_hypercall4 (unsigned int nr,unsigned long p1, +unsigned long p2,unsigned long p3, +unsigned long p4) +{ +return __tdx_hypercall_vendor_kvm (nr, p1, p2, p3, p4,NULL ); +} +#else +static inline long tdx_kvm_hypercall0 (unsigned int nr) +{ +return -ENODEV ; +} + +static inline long tdx_kvm_hypercall1 (unsigned int nr,unsigned long p1) +{ +return -ENODEV ; +} + +static inline long tdx_kvm_hypercall2 (unsigned int nr,unsigned long p1, +unsigned long p2) +{ +return -ENODEV ; +} + +static inline long tdx_kvm_hypercall3 (unsigned int nr,unsigned long p1, +unsigned long p2,unsigned long p3) +{ +return -ENODEV ; +} + +static inline long tdx_kvm_hypercall4 (unsigned int nr,unsigned long p1, +unsigned long p2,unsigned long p3, +unsigned long p4) +{ +return -ENODEV ; +} +#endif /* CONFIG_INTEL_TDX_GUEST_KVM */ +#endif /* _ASM_X86_TDX_H */ arch/x86/kernel/tdcall.S +26
-1@@ -3,6 +3,7 @@ #include <asm/asm.h> #include <asm/frame.h> #include <asm/unwind_hints.h> +#include <asm/export.h> #include <linux/linkage.h> #include <linux/bits.h>@@ -25,6 +26,8 @@ TDG_R12 | TDG_R13 | \ TDG_R14 | TDG_R15 ) +#define TDVMCALL_VENDOR_KVM 0x4d564b2e584454 /* "TDX.KVM" */ + /* * TDX guests use the TDCALL instruction to make requests to the * TDX module and hypercalls to the VMM. It is supported in@@ -212,3 +215,26 @@ SYM_FUNC_START(__tdx_hypercall) FRAME_END retq SYM_FUNC_END(__tdx_hypercall) + +#ifdef CONFIG_INTEL_TDX_GUEST_KVM + +/* + * Helper function for KVM vendor TDVMCALLs. This assembly wrapper + * lets us reuse do_tdvmcall() for KVM-specific hypercalls ( + * TDVMCALL_VENDOR_KVM). + */ +SYM_FUNC_START(__tdx_hypercall_vendor_kvm) + FRAME_BEGIN + /* + * R10 is not part of the function call ABI, but it is a part + * of the TDVMCALL ABI. So set it before making call to the + * do_tdx_hypercall(). + */ + movq $TDVMCALL_VENDOR_KVM, %r10 + call do_tdx_hypercall + FRAME_END + retq +SYM_FUNC_END(__tdx_hypercall_vendor_kvm) + +EXPORT_SYMBOL(__tdx_hypercall_vendor_kvm); +#endif /* CONFIG_INTEL_TDX_GUEST_KVM */ --2.25.1
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
RFC v2 11/32 x86/tdx: Add MSR support for TDX guest
In reply to Kuppuswamy SathyanarayananFrom: "Kirill A. Shutemov" <[email protected]>
Operations on context-switched MSRs can be run natively. The rest of MSRs should be handled through TDVMCALLs.
TDVMCALL[Instruction.RDMSR] and TDVMCALL[Instruction.WRMSR] provide MSR oprations.
You can find RDMSR and WRMSR details in Guest-Host-Communication Interface (GHCI) for Intel Trust Domain Extensions (Intel TDX) specification, sec 3.10, 3.11.
Also, since CSTAR MSR is not used on Intel CPUs as SYSCALL instruction, ignore accesses to CSTAR MSR.
--- arch/x86/kernel/tdx.c | 85 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 83 insertions(+), 2 deletions(-)
arch/x86/kernel/tdx.c +83 -2
@@ -107,6 +107,73 @@ static __cpuidle void tdg_safe_halt(void)tdg_halt (); } +static bool tdg_is_context_switched_msr (unsigned int msr) +{ +/* XXX: Update the list of context-switched MSRs */ + +switch (msr) { +case MSR_EFER : +case MSR_IA32_CR_PAT : +case MSR_FS_BASE : +case MSR_GS_BASE : +case MSR_KERNEL_GS_BASE : +case MSR_IA32_SYSENTER_CS : +case MSR_IA32_SYSENTER_EIP : +case MSR_IA32_SYSENTER_ESP : +case MSR_STAR : +case MSR_LSTAR : +case MSR_SYSCALL_MASK : +case MSR_IA32_XSS : +case MSR_TSC_AUX : +case MSR_IA32_BNDCFGS : +return true; + } +return false; +} + +static u64 tdg_read_msr_safe (unsigned int msr,int *err) +{ +u64 ret; +struct tdvmcall_output out = {0 }; + +WARN_ON_ONCE (tdg_is_context_switched_msr (msr)); + +/* +* Since CSTAR MSR is not used by Intel CPUs as SYSCALL +* instruction, just ignore it. Even raising TDVMCALL +* will lead to same result. +*/ +if (msr ==MSR_CSTAR ) +return 0 ; + + ret =__tdvmcall (EXIT_REASON_MSR_READ , msr,0 ,0 ,0 , &out); + + *err = (ret) ? -EIO :0 ; + +return out.r11; +} + +static int tdg_write_msr_safe (unsigned int msr,unsigned int low, +unsigned int high) +{ +u64 ret; + +WARN_ON_ONCE (tdg_is_context_switched_msr (msr)); + +/* +* Since CSTAR MSR is not used by Intel CPUs as SYSCALL +* instruction, just ignore it. Even raising TDVMCALL +* will lead to same result. +*/ +if (msr ==MSR_CSTAR ) +return 0 ; + + ret =__tdvmcall (EXIT_REASON_MSR_WRITE , msr, (u64 )high <<32 | low, +0 ,0 ,NULL ); + +return ret ? -EIO :0 ; +} +unsigned long tdg_get_ve_info (struct ve_info *ve) {u64 ret;
@@ -136,19 +203,33 @@ unsigned long tdg_get_ve_info(struct ve_info *ve)int tdg_handle_virtualization_exception (struct pt_regs *regs,struct ve_info *ve) { +unsigned long val; +int ret =0 ; +switch (ve->exit_reason) {case EXIT_REASON_HLT :tdg_halt ();break ; +case EXIT_REASON_MSR_READ : + val =tdg_read_msr_safe (regs->cx, (unsigned int *)&ret); +if (!ret) { + regs->ax = val &UINT_MAX ; + regs->dx = val >>32 ; + } +break ; +case EXIT_REASON_MSR_WRITE : + ret =tdg_write_msr_safe (regs->cx, regs->ax, regs->dx); +break ;default :pr_warn ("Unexpected #VE: %lld\n" , ve->exit_reason);return -EFAULT ; }/* After successful #VE handling, move the IP */ - regs->ip += ve->instr_len; +if (!ret) + regs->ip += ve->instr_len; -return 0 ; +return ret; }void __init tdx_early_init (void )
Signature · 1 line
2.25.1
RFC v2 12/32 x86/tdx: Handle CPUID via #VE
In reply to Kuppuswamy SathyanarayananFrom: "Kirill A. Shutemov" <[email protected]>
TDX has three classes of CPUID leaves: some CPUID leaves are always handled by the CPU, others are handled by the TDX module, and some others are handled by the VMM. Since the VMM cannot directly intercept the instruction these are reflected with a #VE exception to the guest, which then converts it into a TDCALL to the VMM, or handled directly.
The TDX module EAS has a full list of CPUID leaves which are handled natively or by the TDX module in 16.2. Only unknown CPUIDs are handled by the #VE method. In practice this typically only applies to the hypervisor specific CPUIDs unknown to the native CPU.
Therefore there is no risk of causing this in early CPUID code which runs before the #VE handler is set up because it will never access those exotic CPUID leaves.
--- arch/x86/kernel/tdx.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
arch/x86/kernel/tdx.c +18 -0
@@ -174,6 +174,21 @@ static int tdg_write_msr_safe(unsigned int msr, unsigned int low,return ret ? -EIO :0 ; } +static void tdg_handle_cpuid (struct pt_regs *regs) +{ +u64 ret; +struct tdvmcall_output out = {0 }; + + ret =__tdvmcall (EXIT_REASON_CPUID , regs->ax, regs->cx,0 ,0 , &out); + +WARN_ON (ret); + + regs->ax = out.r12; + regs->bx = out.r13; + regs->cx = out.r14; + regs->dx = out.r15; +} +unsigned long tdg_get_ve_info (struct ve_info *ve) {u64 ret;
@@ -220,6 +235,9 @@ int tdg_handle_virtualization_exception(struct pt_regs *regs,case EXIT_REASON_MSR_WRITE : ret =tdg_write_msr_safe (regs->cx, regs->ax, regs->dx);break ; +case EXIT_REASON_CPUID : +tdg_handle_cpuid (regs); +break ;default :pr_warn ("Unexpected #VE: %lld\n" , ve->exit_reason);return -EFAULT ;
Signature · 1 line
2.25.1
RFC v2 13/32 x86/io: Allow to override inX() and outX() implementation
In reply to Kuppuswamy SathyanarayananFrom: "Kirill A. Shutemov" <[email protected]>
The patch allows to override the implementation of the port IO helpers. TDX code will provide an implementation that redirect the helpers to paravirt calls.
--- arch/x86/include/asm/io.h | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)
arch/x86/include/asm/io.h +12 -4
@@ -271,18 +271,26 @@ static inline bool sev_key_active(void) { return false; }#endif /* CONFIG_AMD_MEM_ENCRYPT */ +#ifndef __out +#define __out (bwl, bw) \ + asm volatile("out" #bwl " %" #bw "0, %w1" : : "a"(value), "Nd"(port)) +#endif + +#ifndef __in +#define __in (bwl, bw) \ + 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) \ { \ - asm volatile("out" #bwl " %" #bw "0, %w1" \ - : : "a"(value), "Nd"(port)); \ + __out(bwl, bw); \ } \ \ static inline unsigned type in##bwl(int port) \ { \ unsigned type value; \ - asm volatile("in" #bwl " %w1, %" #bw "0" \ - : "=a"(value) : "Nd"(port)); \ + __in(bwl, bw); \ return value; \ } \ \
Signature · 1 line
2.25.1
RFC v2 14/32 x86/tdx: Handle port I/O
In reply to Kuppuswamy SathyanarayananFrom: "Kirill A. Shutemov" <[email protected]>
Unroll string operations and handle port I/O through TDVMCALLs. Also handle #VE due to I/O operations with the same TDVMCALLs.
Decompression code uses port IO for earlyprintk. We must use paravirt calls there too if we want to allow earlyprintk.
Decompresion code cannot deal with alternatives: use branches instead to implement inX() and outX() helpers.
Since we use call instruction in place of in/out instruction, the argument passed to call instruction has to be in a register, it cannot be an immediate value like in/out instruction. So change constraint flag from "Nd" to "d"
--- arch/x86/boot/compressed/Makefile | 1 + arch/x86/boot/compressed/tdcall.S | 9 ++ arch/x86/include/asm/io.h | 5 +- arch/x86/include/asm/tdx.h | 46 ++++++++- arch/x86/kernel/tdcall.S | 154 ++++++++++++++++++++++++++++++ arch/x86/kernel/tdx.c | 33 +++++++ 6 files changed, 245 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 +9 -0
@@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#include <asm/export.h> + +/* Do not export symbols in decompression code */ +#undef EXPORT_SYMBOL +#define EXPORT_SYMBOL(sym) + +#include "../../kernel/tdcall.S"
arch/x86/include/asm/io.h +3 -2
@@ -43,6 +43,7 @@#include <asm/page.h> #include <asm/early_ioremap.h> #include <asm/pgtable_types.h> +#include <asm/tdx.h> #define build_mmio_read (name, size, type, reg, barrier) \ static inline type name(const volatile void __iomem *addr) \
@@ -309,7 +310,7 @@ 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() || is_tdx_guest()) { \ unsigned type *value = (unsigned type *)addr; \ while (count) { \ out##bwl(*value, port); \
@@ -325,7 +326,7 @@ 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() || is_tdx_guest()) { \ unsigned type *value = (unsigned type *)addr; \ while (count) { \ *value = in##bwl(port); \
arch/x86/include/asm/tdx.h +45 -1
62 lines not on this page · raw message
arch/x86/kernel/tdcall.S +154 -0
169 lines not on this page · raw message
arch/x86/kernel/tdx.c +33 -0
@@ -189,6 +189,36 @@ static void tdg_handle_cpuid(struct pt_regs *regs) regs->dx = out.r15; } +static void tdg_out (int size,int port,unsigned int value) +{ +tdvmcall (EXIT_REASON_IO_INSTRUCTION , size,1 , port, value); +} + +static unsigned int tdg_in (int size,int port) +{ +return tdvmcall_out_r11 (EXIT_REASON_IO_INSTRUCTION , size,0 , port,0 ); +} + +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 = (exit_qual &8 ) ?0 :1 ; + size = (exit_qual &7 ) +1 ; + port = exit_qual >>16 ; + +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;
@@ -238,6 +268,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 14/32] x86/tdx: Handle port I/O
Quoted 2 lines · Kuppuswamy Sathyanarayanan
From: "Kirill A. Shutemov" <[email protected]>
While I do not expect that a patch in the middle of a series needs the full introduction of all concepts, the high expectations of the reader's context in this changelog make the patch actively painful to read.
Some connective tissue commentary to list assumptions and pointers to definitions is needed to make this patch stand alone when a future bisect lands on it and someone wonders where to get started debugging it.
Quoted 2 lines
Unroll string operations and handle port I/O through TDVMCALLs. Also handle #VE due to I/O operations with the same TDVMCALLs.
There is a mix of direct-TDVMCALL usage and handling #VE when and why is either approached used?
Quoted 2 lines
Decompression code uses port IO for earlyprintk. We must use paravirt calls there too if we want to allow earlyprintk.
What is the tradeoff between teaching the decompression code to handle #VE (the implied assumption) vs teaching it to avoid #VE with direct TDVMCALLs (the chosen direction)?
Rewrite without "we":
"Given the need to support earlyprintk for protected guests, deploy paravirt calls for the io*() and out*() usage in the decompress code."
This raises the question of why the cover letter switched from explicitly saying TDVMCALL to "paravirt" where it could be confused with the typical paravirt helpers?
Quoted 2 lines
Decompresion code cannot deal with alternatives: use branches
s/Decompresion/Decompression/
Quoted 6 lines
instead to implement
inX()andoutX()helpers.Since we use call instruction in place of in/out instruction, the argument passed to call instruction has to be in a register, it cannot be an immediate value like in/out instruction. So change constraint flag from "Nd" to "d"
Rewrite without "we":
With the approach to use a "call" instruction as an alternative for an "in/out" instruction it is no longer the case that the argument can be an immediate value. Change the asm constraint flag from "Nd" to "d" to accomodate.
Quoted 11 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 | 9 ++ arch/x86/include/asm/io.h | 5 +- arch/x86/include/asm/tdx.h | 46 ++++++++- arch/x86/kernel/tdcall.S | 154 ++++++++++++++++++++++++++++++
Why is this named "tdcall" when it is implementing tdvmcalls? I must say those names don't really help me understand what they do. Can we have Linux names that don't mandate keeping the spec terminology in my brain's translation cache?
Quoted 22 lines
arch/x86/kernel/tdx.c | 33 +++++++ 6 files changed, 245 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 +0
-0
arch/x86/boot/compressed/tdcall.S line 7
Quoted 7 lines
arch/x86/boot/compressed/tdcall.S new file +7
-0@@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#include <asm/export.h> + +/* Do not export symbols in decompression code */ +#undef EXPORT_SYMBOL +#define EXPORT_SYMBOL(sym)
What's wrong with the existing:
KBUILD_CFLAGS += -D__DISABLE_EXPORTS
...in arch/x86/boot/compressed/Makefile?
Quoted 15 lines
arch/x86/boot/compressed/tdcall.S new file +2
-0+ +#include "../../kernel/tdcall.S"arch/x86/include/asm/io.h +1
-0@@ -43,6 +43,7 @@#include <asm/page.h> #include <asm/early_ioremap.h> #include <asm/pgtable_types.h> +#include <asm/tdx.h> #define build_mmio_read (name, size, type, reg, barrier) \ static inline type name(const volatile void __iomem *addr) \
arch/x86/include/asm/io.h static inline unsigned type in##bwl##_p(int port) \ · line 313
Quoted 5 lines
arch/x86/include/asm/io.h +1
-1@@ -309,7 +310,7 @@ 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() || is_tdx_guest()) { \
Is there a unified Linux name these can be given to stop the proliferation of poor vendor names for similar concepts?
That routine
Quoted 26 lines
arch/x86/include/asm/io.h +1
-1unsigned type *value = (unsigned type *)addr; \while (count) { \ out##bwl(*value, port); \@@ -325,7 +326,7 @@ 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() || is_tdx_guest()) { \ unsigned type *value = (unsigned type *)addr; \ while (count) { \ *value = in##bwl(port); \arch/x86/include/asm/tdx.h +2
-0@@ -5,6 +5,8 @@#define TDX_CPUID_LEAF_ID 0x21 +#ifndef __ASSEMBLY__ +#ifdef CONFIG_INTEL_TDX_GUEST #include <asm/cpufeature.h>
arch/x86/include/asm/tdx.h long tdx_kvm_hypercall3(unsigned int nr, unsigned long p1, unsigned long p2, · line 72
Quoted 4 lines
arch/x86/include/asm/tdx.h +1
-0@@ -67,6 +69,48 @@ long tdx_kvm_hypercall3(unsigned int nr, unsigned long p1, unsigned long p2,long tdx_kvm_hypercall4 (unsigned int nr,unsigned long p1,unsigned long p2,unsigned long p3,unsigned long p4); +/* Decompression code doesn't know how to handle alternatives */
Does it also not know how to handle #VE to keep it aligned with the runtime code?
Quoted 21 lines
arch/x86/include/asm/tdx.h +21
-0+#ifdef BOOT_COMPRESSED_MISC_H +#define __out (bwl, bw) \ +do { \ + if (is_tdx_guest()) { \ + asm volatile("call tdg_out" #bwl : : \ + "a"(value), "d"(port)); \ + } else { \ + asm volatile("out" #bwl " %" #bw "0, %w1" : : \ + "a"(value), "Nd"(port)); \ + } \ +} while (0) +#define __in (bwl, bw) \ +do { \ + if (is_tdx_guest()) { \ + asm volatile("call tdg_in" #bwl : \ + "=a"(value) : "d"(port)); \ + } else { \ + asm volatile("in" #bwl " %w1, %" #bw "0" : \ + "=a"(value) : "Nd"(port)); \ + } \ +} while (0)
arch/x86/include/asm/tdx.h long tdx_kvm_hypercall3(unsigned int nr, unsigned long p1, unsigned long p2, · line 103
Quoted 10 lines
arch/x86/include/asm/tdx.h +10
-0+#else +#define __out (bwl, bw) \ + alternative_input("out" #bwl " %" #bw "1, %w2", \ + "call tdg_out" #bwl, X86_FEATURE_TDX_GUEST, \ + "a"(value), "d"(port)) + +#define __in (bwl, bw) \ + alternative_io("in" #bwl " %w2, %" #bw "0", \ + "call tdg_in" #bwl, X86_FEATURE_TDX_GUEST, \ + "=a"(value), "d"(port))
Outside the boot decompression code isn't this branch of the "ifdef BOOT_COMPRESSED_MISC_H" handled by #VE? I also don't see any usage of __{in,out}() in this patch.
Quoted 33 lines
arch/x86/include/asm/tdx.h +11
-1+#endif + +void tdg_outb (unsigned char value,unsigned short port); +void tdg_outw (unsigned short value,unsigned short port); +void tdg_outl (unsigned int value,unsigned short port); + +unsigned char tdg_inb (unsigned short port); +unsigned short tdg_inw (unsigned short port); +unsigned int tdg_inl (unsigned short port); +#else // !CONFIG_INTEL_TDX_GUEST static inline bool is_tdx_guest (void )@@ -106,5 +150,5 @@ static inline long tdx_kvm_hypercall4(unsigned int nr, unsigned long p1, }#endif /* CONFIG_INTEL_TDX_GUEST */ - +#endif /* __ASSEMBLY__ */ #endif /* _ASM_X86_TDX_H */ arch/x86/kernel/tdcall.S +1
-0@@ -3,6 +3,7 @@ #include <asm/asm.h> #include <asm/frame.h> #include <asm/unwind_hints.h> +#include <asm/export.h> #include <linux/linkage.h>
arch/x86/kernel/tdcall.S line 19
Quoted 7 lines
arch/x86/kernel/tdcall.S +4
-0@@ -12,6 +13,12 @@ */ #define TDVMCALL_EXPOSE_REGS_MASK 0xfc00 #define TDVMCALL_VENDOR_KVM 0x4d564b2e584454 /* "TDX.KVM" */ +#define EXIT_REASON_IO_INSTRUCTION 30 +/* + * Current size of struct tdvmcall_output is 40 bytes, + * but allocate double to account future changes.
What future changes? Why could they not be handled as future code changes?
arch/x86/kernel/tdcall.S line 21
Quoted 2 lines
arch/x86/kernel/tdcall.S +2
-0+ */ +#define TDVMCALL_OUTPUT_SIZE 80
Perhaps "PAYLOAD_SIZE" since it is used for both input and output?
If the ABI does not include the size of the payload then how would code detect if even 80 bytes was violated in the future?
Quoted 13 lines
arch/x86/kernel/tdcall.S +6
-0/* * TDX guests use the TDCALL instruction to make@@ -205,3 +212,150 @@ SYM_FUNC_START(__tdvmcall_vendor_kvm) call do_tdvmcall retq SYM_FUNC_END(__tdvmcall_vendor_kvm) + +.macro io_save_registers + push %rbp + push %rbx + push %rcx + push %rdx
arch/x86/kernel/tdcall.S SYM_FUNC_START(__tdvmcall_vendor_kvm) · line 230
Quoted 10 lines
arch/x86/kernel/tdcall.S +10
-0+ push %rdi + push %rsi + push %r8 + push %r9 + push %r10 + push %r11 + push %r12 + push %r13 + push %r14 + push %r15
Surely there's an existing macro for this pattern? Would PUSH_AND_CLEAR_REGS + POP_REGS be suitable? Besides code sharing it would eliminate clearing of %r8.
Quoted 104 lines
arch/x86/kernel/tdcall.S +104
-0+.endm +.macro io_restore_registers + pop %r15 + pop %r14 + pop %r13 + pop %r12 + pop %r11 + pop %r10 + pop %r9 + pop %r8 + pop %rsi + pop %rdi + pop %rdx + pop %rcx + pop %rbx + pop %rbp +.endm + +/* + * tdg_out{b,w,l}() - Write given data to the specified port. + * + * @arg1 (RAX) - Value to be written (passed via R8 to do_tdvmcall()). + * @arg2 (RDX) - Port id (passed via RCX to do_tdvmcall()). + * + */ +SYM_FUNC_START(tdg_outb) + io_save_registers + xor %r8, %r8 + /* Move data to R8 register */ + mov %al, %r8b + /* Set data width to 1 byte */ + mov $1, %rsi + jmp 1f + +SYM_FUNC_START(tdg_outw) + io_save_registers + xor %r8, %r8 + /* Move data to R8 register */ + mov %ax, %r8w + /* Set data width to 2 bytes */ + mov $2, %rsi + jmp 1f + +SYM_FUNC_START(tdg_outl) + io_save_registers + xor %r8, %r8 + /* Move data to R8 register */ + mov %eax, %r8d + /* Set data width to 4 bytes */ + mov $4, %rsi +1: + /* + * Since io_save_registers does not save rax + * state, save it here so that we can preserve + * the caller register state. + */ + push %rax + + mov %rdx, %rcx + /* Set 1 in RDX to select out operation */ + mov $1, %rdx + /* Set TDVMCALL function id in RDI */ + mov $EXIT_REASON_IO_INSTRUCTION, %rdi + /* Set TDVMCALL type info (0 - Standard, > 0 - vendor) in R10 */ + xor %r10, %r10 + /* Since we don't use tdvmcall output, set it to NULL */ + xor %r9, %r9 + + call do_tdvmcall + + pop %rax + io_restore_registers + ret +SYM_FUNC_END(tdg_outb) +SYM_FUNC_END(tdg_outw) +SYM_FUNC_END(tdg_outl) +EXPORT_SYMBOL(tdg_outb) +EXPORT_SYMBOL(tdg_outw) +EXPORT_SYMBOL(tdg_outl) + +/* + * tdg_in{b,w,l}() - Read data to the specified port. + * + * @arg1 (RDX) - Port id (passed via RCX to do_tdvmcall()). + * + * Returns data read via RAX register. + * + */ +SYM_FUNC_START(tdg_inb) + io_save_registers + /* Set data width to 1 byte */ + mov $1, %rsi + jmp 1f + +SYM_FUNC_START(tdg_inw) + io_save_registers + /* Set data width to 2 bytes */ + mov $2, %rsi + jmp 1f + +SYM_FUNC_START(tdg_inl) + io_save_registers + /* Set data width to 4 bytes */ + mov $4, %rsi
arch/x86/kernel/tdcall.S SYM_FUNC_START(__tdvmcall_vendor_kvm) · line 344
Quoted 10 lines
arch/x86/kernel/tdcall.S +10
-0+1: + mov %rdx, %rcx + /* Set 0 in RDX to select in operation */ + mov $0, %rdx + /* Set TDVMCALL function id in RDI */ + mov $EXIT_REASON_IO_INSTRUCTION, %rdi + /* Set TDVMCALL type info (0 - Standard, > 0 - vendor) in R10 */ + xor %r10, %r10 + /* Allocate memory in stack for Output */ + subq $TDVMCALL_OUTPUT_SIZE, %rsp
Why is this leaf function responsibility? I would expect the core do_tdvmcall (or whatever it is renamed to) helper to hide output buffer payload handling. tdg_in* only wants 1, 2, or 4 bytes, not 40 bytes of payload to handle.
arch/x86/kernel/tdcall.S SYM_FUNC_START(__tdvmcall_vendor_kvm) · line 351
Quoted 7 lines
arch/x86/kernel/tdcall.S +7
-0+ /* Move tdvmcall_output pointer to R9 */ + movq %rsp, %r9 + + call do_tdvmcall + + /* Move data read from port to RAX */ + mov TDVMCALL_r11(%r9), %eax
"TDVMCALL_r11" is unreadable, what is that doing?
Shouldn't failed in* calls signal failure with an all ones result?
Quoted 29 lines
arch/x86/kernel/tdcall.S +10
-0+ /* Free allocated memory */ + addq $TDVMCALL_OUTPUT_SIZE, %rsp + io_restore_registers + ret +SYM_FUNC_END(tdg_inb) +SYM_FUNC_END(tdg_inw) +SYM_FUNC_END(tdg_inl) +EXPORT_SYMBOL(tdg_inb) +EXPORT_SYMBOL(tdg_inw) +EXPORT_SYMBOL(tdg_inl)arch/x86/kernel/tdx.c +11
-0@@ -189,6 +189,36 @@ static void tdg_handle_cpuid(struct pt_regs *regs) regs->dx = out.r15; } +static void tdg_out (int size,int port,unsigned int value) +{ +tdvmcall (EXIT_REASON_IO_INSTRUCTION , size,1 , port, value); +} + +static unsigned int tdg_in (int size,int port) +{ +return tdvmcall_out_r11 (EXIT_REASON_IO_INSTRUCTION , size,0 , port,0 ); +} + +static void tdg_handle_io (struct pt_regs *regs,u32 exit_qual)
arch/x86/kernel/tdx.c static void tdg_handle_cpuid(struct pt_regs *regs) · line 212
Quoted 10 lines
arch/x86/kernel/tdx.c +10
-0+{ +bool string = exit_qual &16 ; +int out, size, port; + +/* I/O strings ops are unrolled at build time. */ +BUG_ON (string); + + out = (exit_qual &8 ) ?0 :1 ; + size = (exit_qual &7 ) +1 ; + port = exit_qual >>16 ;
This seems to be begging for exit_qual helpers to put symbolic names on these operations.
Quoted 25 lines
arch/x86/kernel/tdx.c +12
-1+ +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;@@ -238,6 +268,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 14/32] x86/tdx: Handle port I/O
Quoted 3 lines · Dan Williams
There is a mix of direct-TDVMCALL usage and handling #VE when and why is either approached used?
For the really early code in the decompressor or the main kernel we can't use #VE because the IDT needed for handling the exception is not set up, and some other infrastructure needed by the handler is missing. The early code needs to do port IO to be able to write the early serial console. To keep it all common it ended up that all port IO is paravirt. Actually for most the main kernel port IO calls we could just use #VE and it would result in smaller binaries, but then we would need to annotate all early portio with some special name. That's why port IO is all TDCALL.
For some others the only thing that really has to be #VE is MMIO because we don't want to annotate every MMIO read*/write* with an alternative (which would result in incredible binary bloat) For the others they have mostly become now direct calls.
Quoted 6 lines
Quoted 3 lines
Decompression code uses port IO for earlyprintk. We must use paravirt calls there too if we want to allow earlyprintk.
What is the tradeoff between teaching the decompression code to handle #VE (the implied assumption) vs teaching it to avoid #VE with direct TDVMCALLs (the chosen direction)?
The decompression code only really needs it to output something. But you couldn't debug anything until #VE is set up. Also the decompression code has a very basic environment that doesn't supply most kernel services, and the #VE handler is relatively complicated. It would probably need to be duplicated and the instruction decoder be ported to work in this environment. It would be all a lot of work, just to make the debug output work.
Quoted 11 lines
Quoted 11 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 | 9 ++ arch/x86/include/asm/io.h | 5 +- arch/x86/include/asm/tdx.h | 46 ++++++++- arch/x86/kernel/tdcall.S | 154 ++++++++++++++++++++++++++++++
Quoted 4 lines
Why is this named "tdcall" when it is implementing tdvmcalls? I must say those names don't really help me understand what they do. Can we have Linux names that don't mandate keeping the spec terminology in my brain's translation cache?
The instruction is called TDCALL. It's always the same instruction
TDVMCALL is the variant when the host processes it (as opposed to the TDX module), but it's just a different name space in the call number.
\
Quoted 2 lines
Is there a unified Linux name these can be given to stop the proliferation of poor vendor names for similar concepts?
We could use protected_guest()
Quoted 3 lines
Does it also not know how to handle #VE to keep it aligned with the runtime code?
Not sure I understand the question, but the decompression code supports neither alternatives nor #VE. It's a very limited environment.
Quoted 4 lines
Outside the boot decompression code isn't this branch of the "ifdef
BOOT_COMPRESSED_MISC_H" handled by #VE? I also don't see any usage of __{in,out}() in this patch.
I thought it was all alternative after decompression, so the #VE code shouldn't be called. We still have it for some reason though.
Quoted 5 lines
Perhaps "
PAYLOAD_SIZE" since it is used for both input and output?If the ABI does not include the size of the payload then how would code detect if even 80 bytes was violated in the future?
The payload in memory is just a Linux concept. At the TDCALL level it's only registers.
Quoted 5 lines
5
Surely there's an existing macro for this pattern? WouldPUSH_AND_CLEAR_REGS+POP_REGSbe suitable? Besides code sharing it would eliminate clearing of %r8.
There used to be SAVE_ALL/SAVE_REGS, but they have been all removed in some past refactorings.
-Andi
Re: [RFC v2 14/32] x86/tdx: Handle port I/O
Quoted 7 lines · Andi Kleen
Quoted 3 lines · Dan Williams
There is a mix of direct-TDVMCALL usage and handling #VE when and why is either approached used?
Quoted 9 lines
For the really early code in the decompressor or the main kernel we can't use #VE because the IDT needed for handling the exception is not set up, and some other infrastructure needed by the handler is missing. The early code needs to do port IO to be able to write the early serial console. To keep it all common it ended up that all port IO is paravirt. Actually for most the main kernel port IO calls we could just use #VE and it would result in smaller binaries, but then we would need to annotate all early portio with some special name. That's why port IO is all TDCALL.
Thanks Andi. Sathya, please include the above in the next posting.
Quoted 38 lines
For some others the only thing that really has to be #VE is MMIO because we don't want to annotate every MMIO read*/write* with an alternative (which would result in incredible binary bloat) For the others they have mostly become now direct calls.
Quoted 6 lines
Quoted 3 lines
Decompression code uses port IO for earlyprintk. We must use paravirt calls there too if we want to allow earlyprintk.
What is the tradeoff between teaching the decompression code to handle #VE (the implied assumption) vs teaching it to avoid #VE with direct TDVMCALLs (the chosen direction)?
The decompression code only really needs it to output something. But you couldn't debug anything until #VE is set up. Also the decompression code has a very basic environment that doesn't supply most kernel services, and the #VE handler is relatively complicated. It would probably need to be duplicated and the instruction decoder be ported to work in this environment. It would be all a lot of work, just to make the debug output work.
Quoted 11 lines
Quoted 11 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 | 9 ++ arch/x86/include/asm/io.h | 5 +- arch/x86/include/asm/tdx.h | 46 ++++++++- arch/x86/kernel/tdcall.S | 154 ++++++++++++++++++++++++++++++Quoted 4 lines
Why is this named "tdcall" when it is implementing tdvmcalls? I must say those names don't really help me understand what they do. Can we have Linux names that don't mandate keeping the spec terminology in my brain's translation cache?
Quoted 6 lines
The instruction is called TDCALL. It's always the same instruction
TDVMCALL is the variant when the host processes it (as opposed to the TDX module), but it's just a different name space in the call number.
Ok.
Quoted 6 lines
\Quoted 2 lines
Is there a unified Linux name these can be given to stop the proliferation of poor vendor names for similar concepts?
We could use
protected_guest()
Looks good.
Quoted 9 lines
Quoted 5 lines
Does it also not know how to handle #VE to keep it aligned with the runtime code?
Not sure I understand the question, but the decompression code supports neither alternatives nor #VE. It's a very limited environment.
Yes, that addresses the question.
Quoted 8 lines
Quoted 5 lines
Outside the boot decompression code isn't this branch of the "ifdef
BOOT_COMPRESSED_MISC_H" handled by #VE? I also don't see any usage of __{in,out}() in this patch.I thought it was all alternative after decompression, so the #VE code shouldn't be called. We still have it for some reason though.
Right, I'm struggling to understand where these spurious in/out instructions are coming from that are not replaced by the alternative's code? Shouldn't those be dropped on the floor and warned about rather than handled? I.e. shouldn't port-io instruction escapes that would cause #VE be precluded at build-time?
Quoted 12 lines
Quoted 7 lines
Perhaps "
PAYLOAD_SIZE" since it is used for both input and output?If the ABI does not include the size of the payload then how would code detect if even 80 bytes was violated in the future?
The payload in memory is just a Linux concept. At the TDCALL level it's only registers.
If it's only a Linux concept why does this code need to "prepare for the future"?
Quoted 9 lines
Quoted 5 lines
5
Surely there's an existing macro for this pattern? WouldPUSH_AND_CLEAR_REGS+POP_REGSbe suitable? Besides code sharing it would eliminate clearing of %r8.There used to be SAVE_ALL/SAVE_REGS, but they have been all removed in some past refactorings.
Not a huge deal, but at a minimum it seems a generic construct that deserves to be declared centrally rather than tdx-guest-port-io local.
Re: [RFC v2 14/32] x86/tdx: Handle port I/O
Quoted 10 lines · Dan Williams
Quoted 6 lines
Quoted 3 lines
Outside the boot decompression code isn't this branch of the "ifdef
BOOT_COMPRESSED_MISC_H" handled by #VE? I also don't see any usage of __{in,out}() in this patch.I thought it was all alternative after decompression, so the #VE code shouldn't be called. We still have it for some reason though.
Right, I'm struggling to understand where these spurious in/out instructions are coming from that are not replaced by the alternative's code?
There should be nothing in the main tree at least.
Quoted 2 lines
Shouldn't those be dropped on the floor and warned about rather than handled?
It might be related to eventually handling them in ring 3, but I believe we disallow that currently too and it's not all that useful anyways. So yes it could be forbidden.
Quoted 2 lines
I.e. shouldn't port-io instruction escapes
that would cause #VE be precluded at build-time?
You mean in objtool? That would seem like overkill for a more theoretical problem.
Quoted 5 lines
Quoted 2 lines
There used to be SAVE_ALL/SAVE_REGS, but they have been all removed in some past refactorings.
Not a huge deal, but at a minimum it seems a generic construct that deserves to be declared centrally rather than tdx-guest-port-io local.
Yes I agree. We should just bring SAVE_ALL/SAVE_REGS back.
-Andi
Re: [RFC v2 14/32] x86/tdx: Handle port I/O
On Mon, May 10, 2021 at 5:01 PM Andi Kleen <ak@lin [..]
Quoted 5 lines
Quoted 2 lines
I.e. shouldn't port-io instruction escapes
that would cause #VE be precluded at build-time?You mean in objtool? That would seem like overkill for a more theoretical problem.
Oh, sorry, no, I was not implying objtool overkill, just that the mainline kernel should not be surprised by spurious instruction usage.
Re: [RFC v2 14/32] x86/tdx: Handle port I/O
In reply to Dan WilliamsQuoted 18 lines · Dan Williams
Quoted 7 lines · Andi Kleen
Quoted 3 lines · Dan Williams
There is a mix of direct-TDVMCALL usage and handling #VE when and why is either approached used?
Quoted 9 lines
For the really early code in the decompressor or the main kernel we can't use #VE because the IDT needed for handling the exception is not set up, and some other infrastructure needed by the handler is missing. The early code needs to do port IO to be able to write the early serial console. To keep it all common it ended up that all port IO is paravirt. Actually for most the main kernel port IO calls we could just use #VE and it would result in smaller binaries, but then we would need to annotate all early portio with some special name. That's why port IO is all TDCALL.
Quoted 1 line
Thanks Andi. Sathya, please include the above in the next posting.
Will include it.
Quoted 93 lines
Quoted 39 lines
For some others the only thing that really has to be #VE is MMIO because we don't want to annotate every MMIO read*/write* with an alternative (which would result in incredible binary bloat) For the others they have mostly become now direct calls.
Quoted 6 lines
Quoted 3 lines
Decompression code uses port IO for earlyprintk. We must use paravirt calls there too if we want to allow earlyprintk.
What is the tradeoff between teaching the decompression code to handle #VE (the implied assumption) vs teaching it to avoid #VE with direct TDVMCALLs (the chosen direction)?
The decompression code only really needs it to output something. But you couldn't debug anything until #VE is set up. Also the decompression code has a very basic environment that doesn't supply most kernel services, and the #VE handler is relatively complicated. It would probably need to be duplicated and the instruction decoder be ported to work in this environment. It would be all a lot of work, just to make the debug output work.
Quoted 11 lines
Quoted 11 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 | 9 ++ arch/x86/include/asm/io.h | 5 +- arch/x86/include/asm/tdx.h | 46 ++++++++- arch/x86/kernel/tdcall.S | 154 ++++++++++++++++++++++++++++++Quoted 4 lines
Why is this named "tdcall" when it is implementing tdvmcalls? I must say those names don't really help me understand what they do. Can we have Linux names that don't mandate keeping the spec terminology in my brain's translation cache?
Quoted 6 lines
The instruction is called TDCALL. It's always the same instruction
TDVMCALL is the variant when the host processes it (as opposed to the TDX module), but it's just a different name space in the call number.
Ok.
Quoted 6 lines
\Quoted 2 lines
Is there a unified Linux name these can be given to stop the proliferation of poor vendor names for similar concepts?
We could use
protected_guest()Looks good.
Quoted 9 lines
Quoted 5 lines
Does it also not know how to handle #VE to keep it aligned with the runtime code?
Not sure I understand the question, but the decompression code supports neither alternatives nor #VE. It's a very limited environment.
Yes, that addresses the question.
Quoted 8 lines
Quoted 5 lines
Outside the boot decompression code isn't this branch of the "ifdef
BOOT_COMPRESSED_MISC_H" handled by #VE? I also don't see any usage of __{in,out}() in this patch.I thought it was all alternative after decompression, so the #VE code shouldn't be called. We still have it for some reason though.
Right, I'm struggling to understand where these spurious in/out instructions are coming from that are not replaced by the alternative's code? Shouldn't those be dropped on the floor and warned about rather than handled? I.e. shouldn't port-io instruction escapes that would cause #VE be precluded at build-time?
Quoted 9 lines
Quoted 7 lines
Perhaps "
PAYLOAD_SIZE" since it is used for both input and output?If the ABI does not include the size of the payload then how would code detect if even 80 bytes was violated in the future?
Quoted 6 lines
Quoted 3 lines
The payload in memory is just a Linux concept. At the TDCALL level it's only registers.
If it's only a Linux concept why does this code need to "prepare for the future"?
It is the software only structure. It is created to group all the output registers used by VMM. You can find more details about it in patch titled # "[RFC v2 05/32] x86/tdx: Add __tdcall() and __tdvmcall() helper functions"
It is mainly used by functions like __tdx_hypercall(),__tdx_hypercall_vendor_kvm() and tdx_in{b,w,l}.
u64 __tdx_hypercall(u64 fn, u64 r12, u64 r13, u64 r14, u64 r15,
struct tdx_hypercall_output *out);
u64 __tdx_hypercall_vendor_kvm(u64 fn, u64 r12, u64 r13, u64 r14,
u64 r15, struct tdx_hypercall_output *out);struct tdx_hypercall_output {
u64 r11;
u64 r12;
u64 r13;
u64 r14;
u64 r15;
};Functions like __tdx_hypercall() and __tdx_hypercall_vendor_kvm() are used by TDX guest to request services (like RDMSR, WRMSR,GetQuote, etc) from VMM using TDCALL instruction. do_tdx_hypercall() is the helper function (in tdcall.S) which actually implements this ABI.
As per current ABI, VMM will use registers R11-R15 to share the output values with the guest. So we have defined the structure
struct tdx_hypercall_output to group all output registers and make it easier to share it with users of the TDCALLs. This is Linux defined structure.
If there are any changes in TDCALL ABI for VMM, we might have to extend this structure to accommodate new output register changes. So if we define TDVMCALL_OUTPUT_SIZE as 40, we will have modify this value for any future struct tdx_hypercall_output changes. So to avoid it, we have allocated double the size.
May be I should define it as,
#define TDVMCALL_OUTPUT_SIZE sizeof(struct tdx_hypercall_output)
But currently we don't include the asm/tdx.h (which defines struct tdx_hypercall_output) in tdcall.S. So I have defined the size as constant value.
Quoted 15 lines
Quoted 11 lines
Quoted 7 lines
5
Surely there's an existing macro for this pattern? WouldPUSH_AND_CLEAR_REGS+POP_REGSbe suitable? Besides code sharing it would eliminate clearing of %r8.There used to be SAVE_ALL/SAVE_REGS, but they have been all removed in some past refactorings.
Not a huge deal, but at a minimum it seems a generic construct that deserves to be declared centrally rather than tdx-guest-port-io local.
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Re: [RFC v2 14/32] x86/tdx: Handle port I/O
On Mon, May 10, 2021 at 5:30 PM Kuppuswamy, Sathyanarayanan <[email protected]> wrote:
[..]
Quoted 8 lines
It is mainly used by functions like __tdx_hypercall(),__tdx_hypercall_vendor_kvm() and tdx_in{b,w,l}.
u64 __tdx_hypercall(u64 fn, u64 r12, u64 r13, u64 r14, u64 r15,
struct tdx_hypercall_output *out); u64 __tdx_hypercall_vendor_kvm(u64 fn, u64 r12, u64 r13, u64 r14, u64 r15, struct tdx_hypercall_output *out);
Quoted 7 lines
struct tdx_hypercall_output {
u64 r11;
u64 r12;
u64 r13;
u64 r14;
u64 r15;
};Why is this by register name and not something like:
struct tdx_hypercall_payload {
u64 data[5];
};
...because the code in this patch is reading the payload out of a stack relative offset, not r11.
Quoted 9 lines
Functions like
__tdx_hypercall()and__tdx_hypercall_vendor_kvm()are used by TDX guest to request services (like RDMSR, WRMSR,GetQuote, etc) from VMM using TDCALL instruction.do_tdx_hypercall()is the helper function (intdcall.S) which actually implements this ABI.As per current ABI, VMM will use registers R11-R15 to share the output values with the guest.
Which ABI, __tdx_hypercall_vendor_kvm()? The code is putting the payload on the stack, so I'm not sure what ABI you are referring to?
Quoted 10 lines
So we have defined the structure
structtdx_hypercall_outputto group all output registers and make it easier to share it with users of the TDCALLs. This is Linux defined structure.If there are any changes in TDCALL ABI for VMM, we might have to extend this structure to accommodate new output register changes. So if we define
TDVMCALL_OUTPUT_SIZEas 40, we will have modify this value for any future structtdx_hypercall_outputchanges. So to avoid it, we have allocated double the size.
Quoted 3 lines
May be I should define it as,
#define
TDVMCALL_OUTPUT_SIZEsizeof(struct tdx_hypercall_output)
An arrangement like that seems more reasonable than a seemingly arbitrary number and an ominous warning about things that may happen in the future.
Re: [RFC v2 14/32] x86/tdx: Handle port I/O
Quoted 19 lines · Dan Williams
On Mon, May 10, 2021 at 5:30 PM Kuppuswamy, Sathyanarayanan <[email protected]> wrote:
[..]Quoted 8 lines
It is mainly used by functions like __tdx_hypercall(),__tdx_hypercall_vendor_kvm() and tdx_in{b,w,l}.
u64 __tdx_hypercall(u64 fn, u64 r12, u64 r13, u64 r14, u64 r15,
struct tdx_hypercall_output *out); u64 __tdx_hypercall_vendor_kvm(u64 fn, u64 r12, u64 r13, u64 r14, u64 r15, struct tdx_hypercall_output *out);Quoted 7 lines
struct tdx_hypercall_output { u64 r11; u64 r12; u64 r13; u64 r14; u64 r15; };
Quoted 8 lines
Why is this by register name and not something like:
struct tdx_hypercall_payload { u64 data[5]; };...because the code in this patch is reading the payload out of a stack relative offset, not r11.
Since this patch allocates this memory in ASM code, we read it via offset. If you see other use cases in tdx.c, you will notice the use of register names.
static void tdg_handle_cpuid(struct pt_regs *regs)
{
u64 ret;
struct tdx_hypercall_output out = {0};
ret = __tdx_hypercall(EXIT_REASON_CPUID, regs->ax,
regs->cx, 0, 0, &out);
WARN_ON(ret);
regs->ax = out.r12;
regs->bx = out.r13;
regs->cx = out.r14;
regs->dx = out.r15;
}static u64 tdg_read_msr_safe(unsigned int msr, int *err)
{
u64 ret;
struct tdx_hypercall_output out = {0};
WARN_ON_ONCE(tdg_is_context_switched_msr(msr));
/*
* Since CSTAR MSR is not used by Intel CPUs as SYSCALL
* instruction, just ignore it. Even raising TDVMCALL
* will lead to same result.
*/
if (msr == MSR_CSTAR)
return 0;
ret = __tdx_hypercall(EXIT_REASON_MSR_READ, msr, 0, 0, 0, &out);
*err = ret ? -EIO : 0;
return out.r11;
}Quoted 8 lines
Quoted 8 lines
Functions like
__tdx_hypercall()and__tdx_hypercall_vendor_kvm()are used by TDX guest to request services (like RDMSR, WRMSR,GetQuote, etc) from VMM using TDCALL instruction.do_tdx_hypercall()is the helper function (intdcall.S) which actually implements this ABI.
Quoted 4 lines
Quoted 2 lines
As per current ABI, VMM will use registers R11-R15 to share the output values with the guest.
Which ABI,
TDCALL ABI (see sections 3.1 to 3.12 and look for Output Operands in each TDVMCALL variant).
__tdx_hypercall_vendor_kvm()? The code is putting the
Quoted 13 lines
payload on the stack, so I'm not sure what ABI you are referring to?
Quoted 10 lines
So we have defined the structure
structtdx_hypercall_outputto group all output registers and make it easier to share it with users of the TDCALLs. This is Linux defined structure.If there are any changes in TDCALL ABI for VMM, we might have to extend this structure to accommodate new output register changes. So if we define
TDVMCALL_OUTPUT_SIZEas 40, we will have modify this value for any future structtdx_hypercall_outputchanges. So to avoid it, we have allocated double the size.
Quoted 7 lines
Quoted 3 lines
May be I should define it as,
#define
TDVMCALL_OUTPUT_SIZEsizeof(struct tdx_hypercall_output)An arrangement like that seems more reasonable than a seemingly arbitrary number and an ominous warning about things that may happen in the future.
I will use the above format.
Quoted 1 line
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Re: [RFC v2 14/32] x86/tdx: Handle port I/O
Quoted 28 lines · Sathyanarayanan
Quoted 27 lines · Dan Williams
On Mon, May 10, 2021 at 5:30 PM Kuppuswamy, Sathyanarayanan <[email protected]> wrote:
[..]Quoted 10 lines
It is mainly used by functions like
__tdx_hypercall(),__tdx_hypercall_vendor_kvm()
and tdx_in{b,w,l}.u64
__tdx_hypercall(u64 fn, u64 r12, u64 r13, u64 r14, u64 r15, struct tdx_hypercall_output *out); u64__tdx_hypercall_vendor_kvm(u64 fn, u64 r12, u64 r13, u64 r14, u64 r15, struct tdx_hypercall_output *out);Quoted 7 lines
struct
tdx_hypercall_output{
u64 r11;
u64 r12;
u64 r13;
u64 r14;
u64 r15;
};Why is this by register name and not something like:
struct
tdx_hypercall_payload{
u64 data[5];
};
Quoted 6 lines
Quoted 2 lines
...because the code in this patch is reading the payload out of a stack relative offset, not r11.
Since this patch allocates this memory in ASM code, we read it via offset. If you see other use cases in
tdx.c, you will notice the use of register names.
To what you do you refer by "this patch allocates this memory in ASM code"? Could you point to the specific ASM code that "allocates memory"?
Dan I'll try to answer your question. TDX has both a "hypercall" interface for guests to call into hosts and a "seamcall" interface where guests or hosts can talk to the TDX/SEAM module.
Both of these represent an ABI which _resembles_ a system call ABI. Values are placed in registers, including a "function" register which is very similar to a the system call number we place in RAX.
*But* those ABIs was actually designed to (IIRC) resemble the Windows/Microsoft ABI, not the Linux ABI. So the register conventions are unfamiliar. There is assembly code to convert between the ELF function call ABI and the TDX ABIs.
For instance, if you are in C code and you call:
__tdx_hypercall_vendor_kvm(u64 fn, u64 r12, ...
The value for "fn" will be placed in RAX and "r12" will be placed in RDI for the function call itself. The assembly code will, for instance, take the "r12" *VARIABLE* and ensure it gets into the R12 *REGISTER* for the hypercall.
The same thing happens on the output side. The TDX ABIs specify "return" values in certain registers (r11-r15). However, those registers are not preserved in our function return ABI. So, they must be stashed off in memory into a place where the caller can retrieve them.
Rather than being unstructured "data[]", the value in tdx_hypercall_output->r11 was actually in register R11 at some point. If you look at the spec, you can see the functions that use R11.
Let's say there's a hypercall to check for whether puppies are cute. Here's the kernel side:
bool tdx_hypercall_puppies_are_cute()
{
struct tdx_hypercall_output out;
u64 ret;
ret = __tdx_hypercall_vendor_kvm(HOST_LIKES_PUPPIES, ..., &out);
/* Did the hypercall even succeed? */
if (ret != SUCCESS)
return -EINVAL;
if (out->r11 == TDX_WHATEVER_CUTE_BIT)
return true;
// Nope, I guess puppies are not cute
return false;
}The spec would actually say, "Blah blah, puppies are cute if TDX_WHATEVER_CUTE_BIT is set in r11". So, this whole setup actually results in really nice C code that you can sit side-by-side with the spec and see if they agree.
Re: [RFC v2 14/32] x86/tdx: Handle port I/O
Quoted 2 lines · Dave Hansen
To what you do you refer by "this patch allocates this memory in ASM code"? Could you point to the specific ASM code that "allocates memory"?
We use 40 bytes in stack for storing the output register values. It is in function tdg_inl().
subq $TDVMCALL_OUTPUT_SIZE, %rsp
+SYM_FUNC_START(tdg_inl)
+ io_save_registers
+ /* Set data width to 4 bytes */
+ mov $4, %rsi
+1:
+ mov %rdx, %rcx
+ /* Set 0 in RDX to select in operation */
+ mov $0, %rdx
+ /* Set TDVMCALL function id in RDI */
+ mov $EXIT_REASON_IO_INSTRUCTION, %rdi
+ /* Set TDVMCALL type info (0 - Standard, > 0 - vendor) in R10 */
+ xor %r10, %r10
+ /* Allocate memory in stack for Output */
+ subq $TDVMCALL_OUTPUT_SIZE, %rsp
+ /* Move tdvmcall_output pointer to R9 */
+ movq %rsp, %r9
+
+ call do_tdvmcall
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Re: [RFC v2 14/32] x86/tdx: Handle port I/O
In reply to Dan WilliamsQuoted 8 lines · Dan Williams
Quoted 6 lines
Quoted 3 lines
Surely there's an existing macro for this pattern? Would
PUSH_AND_CLEAR_REGS+POP_REGSbe suitable? Besides code sharing it would eliminate clearing of %r8.There used to be SAVE_ALL/SAVE_REGS, but they have been all removed in some past refactorings.
Not a huge deal, but at a minimum it seems a generic construct that deserves to be declared centrally rather than tdx-guest-port-io local.
I can define SAVE_ALL_REGS/RESTORE_ALL_REGS. Do you want to move it outside TDX code? I don't know if there will be other users for it?
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Re: [RFC v2 14/32] x86/tdx: Handle port I/O
Quoted 7 lines · Sathyanarayanan
Quoted 4 lines · Dan Williams
Quoted 4 lines
Quoted 3 lines
Surely there's an existing macro for this pattern? Would
PUSH_AND_CLEAR_REGS+POP_REGSbe suitable? Besides code sharing it would eliminate clearing of %r8.
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
There used to be SAVE_ALL/SAVE_REGS, but they have been all removed in some past refactorings.
Not a huge deal, but at a minimum it seems a generic construct that deserves to be declared centrally rather than tdx-guest-port-io local.
I can define SAVE_ALL_REGS/RESTORE_ALL_REGS. Do you want to move it outside TDX code? I don't know if there will be other users for it?
The old name was SAVE_ALL / SAVE_REGS.
Yes please put it outside tdx code into some include file.
-Andi
Re: [RFC v2 14/32] x86/tdx: Handle port I/O
In reply to Dan WilliamsQuoted 5 lines · Dan Williams
Quoted 2 lines
Decompression code uses port IO for earlyprintk. We must use paravirt calls there too if we want to allow earlyprintk.
What is the tradeoff between teaching the decompression code to handle #VE (the implied assumption) vs teaching it to avoid #VE with direct TDVMCALLs (the chosen direction)?
To me, the tradeoff is not just "teaching" the code to handle a #VE, but ensuring that the entire architecture works.
Intentionally invoking a #VE is like making a function call that *MIGHT* recurse on itself. Sure, you can try to come up with a story about bounding the recursion. But, I don't see any semblance of that in this series.
Exception-based recursion is really nasty because it's implicit, not explicit. That's why I'm advocating for a design where the kernel never intentionally causes a #VE: it never intentionally recurses without bounds.
Re: [RFC v2 14/32] x86/tdx: Handle port I/O
Quoted 11 lines · Dave Hansen
Quoted 5 lines · Dan Williams
Quoted 2 lines
Decompression code uses port IO for earlyprintk. We must use paravirt calls there too if we want to allow earlyprintk.
What is the tradeoff between teaching the decompression code to handle #VE (the implied assumption) vs teaching it to avoid #VE with direct TDVMCALLs (the chosen direction)?
To me, the tradeoff is not just "teaching" the code to handle a #VE, but ensuring that the entire architecture works.
Quoted 8 lines
Intentionally invoking a #VE is like making a function call that *MIGHT* recurse on itself. Sure, you can try to come up with a story about bounding the recursion. But, I don't see any semblance of that in this series.
Exception-based recursion is really nasty because it's implicit, not explicit. That's why I'm advocating for a design where the kernel never intentionally causes a #VE: it never intentionally recurses without bounds.
Thanks Dave, this really helps.
Re: [RFC v2 14/32] x86/tdx: Handle port I/O
In reply to Dave HansenQuoted 11 lines · Dave Hansen
Quoted 5 lines · Dan Williams
Quoted 2 lines
Decompression code uses port IO for earlyprintk. We must use paravirt calls there too if we want to allow earlyprintk.
What is the tradeoff between teaching the decompression code to handle #VE (the implied assumption) vs teaching it to avoid #VE with direct TDVMCALLs (the chosen direction)?
To me, the tradeoff is not just "teaching" the code to handle a #VE, but ensuring that the entire architecture works.
Quoted 8 lines
Intentionally invoking a #VE is like making a function call that *MIGHT* recurse on itself. Sure, you can try to come up with a story about bounding the recursion. But, I don't see any semblance of that in this series.
Exception-based recursion is really nasty because it's implicit, not explicit. That's why I'm advocating for a design where the kernel never intentionally causes a #VE: it never intentionally recurses without bounds.
So this circles back to the common problem with the mwait/monitor/wbinvd patch and this one. "Can't happen" #VE conditions should be fatal. I.e. have a nice clear message about why the kernel failed and halt. All the uses of these #VE triggering instructions can be eliminated ahead of time with auditing and people that load unaudited out-of-tree modules that trigger #VE get to keep the pieces. Said pieces will be described to them by the #VE triggered fail message. This isn't like split lock disable where the code is difficult to audit.
What am I missing?
cover x86/tdx: Handle port I/O
This 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:
* 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):
tdx: Introduce generic protected_guest abstraction
arch/Kconfig | 3 + arch/x86/Kconfig | 1 + 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/protected_guest.h | 24 +++++ arch/x86/include/asm/tdx.h | 60 ++++++++++++- arch/x86/kernel/head64.c | 4 + arch/x86/kernel/tdx.c | 116 +++++++++++++++++++++++++ include/linux/protected_guest.h | 23 +++++ 11 files changed, 267 insertions(+), 3 deletions(-) create mode 100644 arch/x86/boot/compressed/tdcall.S create mode 100644 arch/x86/include/asm/protected_guest.h create mode 100644 include/linux/protected_guest.h
Signature · 1 line
2.25.1
RFC 1/3 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.
is_protected_guest() helper function can be implemented using arch specific CPU feature flags.
protected_guest_has() is used to check for protected guest feature flags.
--- arch/Kconfig | 3 +++ arch/x86/Kconfig | 1 + arch/x86/include/asm/protected_guest.h | 24 ++++++++++++++++++++++++ arch/x86/include/asm/tdx.h | 7 +++++++ arch/x86/kernel/tdx.c | 18 ++++++++++++++++++ include/linux/protected_guest.h | 23 +++++++++++++++++++++++ 6 files changed, 76 insertions(+) create mode 100644 arch/x86/include/asm/protected_guest.h create mode 100644 include/linux/protected_guest.h
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 +1 -0
@@ -878,6 +878,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
arch/x86/include/asm/protected_guest.h new file +24 -0
@@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (C) 2020 Intel Corporation */ +#ifndef _ASM_PROTECTED_GUEST +#define _ASM_PROTECTED_GUEST 1 + +#include <asm/cpufeature.h> +#include <asm/tdx.h> + +/* Only include through linux/protected_guest.h */ + +static inline bool is_protected_guest (void ) +{ +return boot_cpu_has (X86_FEATURE_TDX_GUEST ); +} + +static inline bool protected_guest_has (unsigned long flag) +{ +if (boot_cpu_has (X86_FEATURE_TDX_GUEST )) +return tdx_protected_guest_has (flag); + +return false; +} + +#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/tdx.c +18 -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,23 @@ bool is_tdx_guest(void) }EXPORT_SYMBOL_GPL (is_tdx_guest); +bool tdx_protected_guest_has (unsigned long flag) +{ +if (!is_tdx_guest ()) +return false; + +switch (flag) { +case VM_MEM_ENCRYPT : +case VM_MEM_ENCRYPT_ACTIVE : +case VM_UNROLL_STRING_IO : +case VM_HOST_MEM_ENCRYPT : +return true; + } + +return false; +} +EXPORT_SYMBOL_GPL (tdx_protected_guest_has); +static void tdg_get_info (void ) {u64 ret;
include/linux/protected_guest.h new file +23 -0
@@ -0,0 +1,23 @@ +/* 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 VM_MEM_ENCRYPT 0x100 +/* Encryption support is active */ +#define VM_MEM_ENCRYPT_ACTIVE 0x101 +/* Support for unrolled string IO */ +#define VM_UNROLL_STRING_IO 0x102 +/* Support for host memory encryption */ +#define VM_HOST_MEM_ENCRYPT 0x103 + +#ifdef CONFIG_ARCH_HAS_PROTECTED_GUEST +#include <asm/protected_guest.h> +#else +static inline bool is_protected_guest (void ) {return false; } +static inline bool protected_guest_has (unsigned long flag) {return false; } +#endif + +#endif
Signature · 1 line
2.25.1
RFC 1/1 x86: 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.
protected_guest_has() is used to check for protected guest feature flags.
---
Changes since RFC v2-fix-v1:
* Changed the title from "tdx: Introduce generic protected_guest abstraction" to "x86: Introduce generic protected guest"
* Removed usage of ARCH_HAS_PROTECTED_GUEST and directly called TDX and AMD specific xx_protected_guest_has() variants from linux/protected_guest.h.
* Added support for amd_protected_guest_has() helper function.
* Removed redundant is_tdx_guest() check in tdx_protected_guest_has() function.
* Fixed commit log to reflect the latest changes.
arch/x86/include/asm/mem_encrypt.h | 4 +++ arch/x86/include/asm/tdx.h | 7 ++++++ arch/x86/kernel/tdx.c | 16 ++++++++++++ arch/x86/mm/mem_encrypt.c | 13 ++++++++++ include/linux/protected_guest.h | 40 ++++++++++++++++++++++++++++++ 5 files changed, 80 insertions(+) create mode 100644 include/linux/protected_guest.h
arch/x86/include/asm/mem_encrypt.h +4 -0
@@ -56,6 +56,8 @@ bool sev_es_active(void);#define __bss_decrypted __section(".bss..decrypted") +bool amd_protected_guest_has (unsigned long flag); +#else /* !CONFIG_AMD_MEM_ENCRYPT */ #define sme_me_mask 0ULL
@@ -86,6 +88,8 @@ early_set_memory_encrypted(unsigned long vaddr, unsigned long size) { return 0;static inline void mem_encrypt_free_decrypted_mem (void ) { } +static inline bool amd_protected_guest_has (unsigned long flag) {return false; } +#define __bss_decrypted#endif /* CONFIG_AMD_MEM_ENCRYPT */
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/tdx.c +16 -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,21 @@ bool is_tdx_guest(void) }EXPORT_SYMBOL_GPL (is_tdx_guest); +bool tdx_protected_guest_has (unsigned long flag) +{ +switch (flag) { +case VM_MEM_ENCRYPT : +case VM_MEM_ENCRYPT_ACTIVE : +case VM_UNROLL_STRING_IO : +case VM_HOST_MEM_ENCRYPT : +case VM_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 +13 -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>
@@ -389,6 +390,18 @@ bool noinstr sev_es_active(void)return sev_status &MSR_AMD64_SEV_ES_ENABLED ; } +bool amd_protected_guest_has (unsigned long flag) +{ +switch (flag) { +case VM_MEM_ENCRYPT : +case VM_MEM_ENCRYPT_ACTIVE : +return true; + } + +return false; +} +EXPORT_SYMBOL_GPL (amd_protected_guest_has); +/* Override for DMA direct allocation check - ARCH_HAS_FORCE_DMA_UNENCRYPTED */ bool force_dma_unencrypted (struct device *dev) {
include/linux/protected_guest.h new file +40 -0
@@ -0,0 +1,40 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _LINUX_PROTECTED_GUEST_H +#define _LINUX_PROTECTED_GUEST_H 1 + +#include <linux/mem_encrypt.h> + +/* Protected Guest Feature Flags (leave 0-0xff for arch specific flags) */ + +/* Support for guest encryption */ +#define VM_MEM_ENCRYPT 0x100 +/* Encryption support is active */ +#define VM_MEM_ENCRYPT_ACTIVE 0x101 +/* Support for unrolled string IO */ +#define VM_UNROLL_STRING_IO 0x102 +/* Support for host memory encryption */ +#define VM_HOST_MEM_ENCRYPT 0x103 +/* Support for shared mapping initialization (after early init) */ +#define VM_SHARED_MAPPING_INIT 0x104 + +#if defined(CONFIG_INTEL_TDX_GUEST ) || defined(CONFIG_AMD_MEM_ENCRYPT ) + +#include <asm/tdx.h> + +static inline bool protected_guest_has (unsigned long flag) +{ +if (is_tdx_guest ()) +return tdx_protected_guest_has (flag); +else if (mem_encrypt_active ()) +return amd_protected_guest_has (flag); + +return false; +} + +#else + +static inline bool protected_guest_has (unsigned long flag) {return false; } + +#endif + +#endif
Signature · 1 line
2.25.1
Sean Christopherson
maintainerRe: [RFC v2-fix-v2 1/1] x86: Introduce generic protected guest abstraction
arch/x86/include/asm/mem_encrypt.h bool sev_es_active(void); · line 59
Quoted 9 lines · Kuppuswamy Sathyanarayanan
arch/x86/include/asm/mem_encrypt.h +1
-0@@ -56,6 +56,8 @@ bool sev_es_active(void);#define __bss_decrypted __section(".bss..decrypted") +bool amd_protected_guest_has (unsigned long flag);
Why call one by the vendor (amd) and the other by the technology (tdx)? sev_protected_guest_has() seems like the more logical name, e.g. if AMD CPUs gain a new non-SEV technology then we'll have a mess.
Quoted 9 lines
arch/x86/include/asm/tdx.h +1
-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);
...
arch/x86/include/asm/tdx.h
Quoted 5 lines
arch/x86/include/asm/tdx.h +5
-0+static inline bool protected_guest_has (unsigned long flag) +{ +if (is_tdx_guest ()) +return tdx_protected_guest_has (flag); +else if (mem_encrypt_active ())
Shouldn't this be sev_active()? mem_encrypt_active() will return true for SME, too.
Quoted 4 lines
arch/x86/include/asm/tdx.h +4
-0+return amd_protected_guest_has (flag); + +return false; +}
Re: [RFC v2-fix-v2 1/1] x86: Introduce generic protected guest abstraction
Quoted 7 lines · Sean Christopherson
Quoted 6 lines · Kuppuswamy Sathyanarayanan
arch/x86/include/asm/mem_encrypt.h +0
-0@@ -56,6 +56,8 @@ bool sev_es_active(void);
Quoted 8 lines
arch/x86/include/asm/mem_encrypt.h bool sev_es_active(void); · line 59
Quoted 3 lines
arch/x86/include/asm/mem_encrypt.h +1
-0#define __bss_decrypted __section(".bss..decrypted") +bool amd_protected_guest_has (unsigned long flag);Why call one by the vendor (amd) and the other by the technology (tdx)?
sev_protected_guest_has()seems like the more logical name, e.g. if AMD CPUs gain a new non-SEV technology then we'll have a mess.
The original suggestion from Boris, IIRC, was for protected_guest_has() function (below) to be:
if (intel) return intel_protected_guest_has(); else if (amd) return amd_protected_guest_has(); else return false;
And then you could check for TDX or SME/SEV in the respective functions.
Quoted 13 lines
Quoted 10 lines
arch/x86/include/asm/tdx.h +1
-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);...
Quoted 8 lines
arch/x86/include/asm/tdx.h
Quoted 5 lines
arch/x86/include/asm/tdx.h +5
-0+static inline bool protected_guest_has (unsigned long flag) +{ +if (is_tdx_guest ()) +return tdx_protected_guest_has (flag); +else if (mem_encrypt_active ())Shouldn't this be
sev_active()?mem_encrypt_active()will return true for SME, too.
I believe Boris was wanting to replace the areas where sme_active() was specifically checked, too. And so protected_guest_has() can be confusing...
Maybe naming it protected_os_has() or protection_attr_active() might work. This would then work SME or MKTME as well.
Thanks,
Tom
Quoted 5 lines
Quoted 5 lines
arch/x86/include/asm/tdx.h +4
-0+return amd_protected_guest_has (flag); + +return false; +}
Re: [RFC v2-fix-v2 1/1] x86: Introduce generic protected guest abstraction
Quoted 5 lines · Tom Lendacky
The original suggestion from Boris, IIRC, was for
protected_guest_has()function (below) to be:if (intel) return intel_protected_guest_has();
Yes. But for Intel, I think currently we can only check for is_tdx_guest() here.
if (is_tdx_guest())
return intel_protected_guest_has();
So if we use is_tdx_guest(), it is better to call tdx_protected_guest_has() here.
Once we start using protected_guest_has for other Intel technologies, may be we can generalize it. Let me know your comments.
Quoted 6 lines
else if (amd) return amd_protected_guest_has(); else return false;And then you could check for TDX or SME/SEV in the respective functions.
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Borislav Petkov
maintainerRe: [RFC v2-fix-v2 1/1] x86: Introduce generic protected guest abstraction
In reply to Tom LendackyQuoted 11 lines · Tom Lendacky
The original suggestion from Boris, IIRC, was for
protected_guest_has()function (below) to be:if (intel) return intel_protected_guest_has(); else if (amd) return amd_protected_guest_has(); else return false;And then you could check for TDX or SME/SEV in the respective functions.
Yeah, a single function call which calls vendor-specific functions.
If you can point me to a tree with your patches, I can try to hack up what I mean.
Quoted 2 lines
I believe Boris was wanting to replace the areas where
sme_active()was specifically checked, too. And soprotected_guest_has()can be confusing...
We can always say
protected_guest_has(SME_ACTIVE);
or so and then it is clear.
Quoted 2 lines
Maybe naming it
protected_os_has()orprotection_attr_active()might work. This would then work SME or MKTME as well.
But other names are fine too once we're done with the bikeshedding.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [RFC v2-fix-v2 1/1] x86: Introduce generic protected guest abstraction
Quoted 2 lines · Borislav Petkov
If you can point me to a tree with your patches, I can try to hack up what I mean.
https://github.com/intel/tdx/commit/8515b66a0cb27d5ab66eda201285090faee742f7
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Borislav Petkov
maintainerRe: [RFC v2-fix-v2 1/1] x86: Introduce generic protected guest abstraction
Quoted 7 lines · Sathyanarayanan
Quoted 2 lines · Borislav Petkov
If you can point me to a tree with your patches, I can try to hack up what I mean.
https://github.com/intel/tdx/commit/8515b66a0cb27d5ab66eda201285090faee742f7
Ok, and which branch or tag?
tdx-guest-v5.12-7 or "guest"?
The github interface is yuck when one wants to look at commits...
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [RFC v2-fix-v2 1/1] x86: Introduce generic protected guest abstraction
Quoted 5 lines · Borislav Petkov
Ok, and which branch or tag?
tdx-guest-v5.12-7 or "guest"?
The github interface is yuck when one wants to look at commits...
please use guest branch.
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Re: [RFC v2-fix-v2 1/1] x86: Introduce generic protected guest abstraction
In reply to Kuppuswamy SathyanarayananQuoted 20 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.
protected_guest_has()is used to check for protected guest feature flags.Originally-by Andi Kleen <[email protected]>Signed-off-by Kuppuswamy Sathyanarayanan <[email protected]>---
Changes since RFC v2-fix-v1:
* Changed the title from "tdx: Introduce genericprotected_guestabstraction" to "x86: Introduce generic protected guest"
* Removed usage ofARCH_HAS_PROTECTED_GUESTand directly called TDX and AMD specificxx_protected_guest_has()variants fromlinux/protected_guest.h.
* Added support foramd_protected_guest_has()helper function.
* Removed redundantis_tdx_guest()check intdx_protected_guest_has()function.
* Fixed commit log to reflect the latest changes.
...
arch/x86/mm/mem_encrypt.c bool noinstr sev_es_active(void) · line 403
Quoted 12 lines
arch/x86/mm/mem_encrypt.c +11
-0+bool amd_protected_guest_has (unsigned long flag) +{ +switch (flag) { +case VM_MEM_ENCRYPT : +case VM_MEM_ENCRYPT_ACTIVE : +return true; + } + +return false; +} +EXPORT_SYMBOL_GPL (amd_protected_guest_has);
This certainly doesn't capture all of the situations where true would need to be returned. For example, SEV, but not SEV-ES, requires that string I/O be unrolled, etc.
Thanks,
Tom
Re: [RFC v2-fix-v2 1/1] x86: Introduce generic protected guest abstraction
Quoted 3 lines · Tom Lendacky
This certainly doesn't capture all of the situations where true would need to be returned. For example, SEV, but not SEV-ES, requires that string I/O be unrolled, etc.
For AMD following cases should be true right? I can fix it in next version.
case VM_UNROLL_STRING_IO:
case VM_HOST_MEM_ENCRYPT:
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Borislav Petkov
maintainerRe: [RFC v2-fix-v2 1/1] x86: Introduce generic protected guest abstraction
In reply to Tom LendackyQuoted 3 lines · Tom Lendacky
This certainly doesn't capture all of the situations where true would need to be returned. For example, SEV, but not SEV-ES, requires that string I/O be unrolled, etc.
Yeah, I believe this would be better done for you guys, ontop, as you know best what needs to be queried where. So this first patch adding only a stub should be fine. Or you or someone else does the conversion ontop of the Intel patch and then all patches go together.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteBorislav Petkov
maintainerRe: [RFC v2-fix-v2 1/1] x86: Introduce generic protected guest abstraction
In reply to Kuppuswamy SathyanarayananQuoted 14 lines · Kuppuswamy Sathyanarayanan
include/linux/protected_guest.h new file +8
-0@@ -0,0 +1,40 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _LINUX_PROTECTED_GUEST_H +#define _LINUX_PROTECTED_GUEST_H 1 + +#include <linux/mem_encrypt.h> + +/* Protected Guest Feature Flags (leave 0-0xff for arch specific flags) */ +
include/linux/protected_guest.h line 18
Quoted 10 lines
include/linux/protected_guest.h new file +10
-0+/* Support for guest encryption */ +#define VM_MEM_ENCRYPT 0x100 +/* Encryption support is active */ +#define VM_MEM_ENCRYPT_ACTIVE 0x101 +/* Support for unrolled string IO */ +#define VM_UNROLL_STRING_IO 0x102 +/* Support for host memory encryption */ +#define VM_HOST_MEM_ENCRYPT 0x103 +/* Support for shared mapping initialization (after early init) */ +#define VM_SHARED_MAPPING_INIT 0x104
Ok, a couple of things:
first of all, those flags with that VM_ prefix make me think of "virtual memory" instead of "virtual machine". So they should be something else, like, say
PR_G_... for Protected Guest or so. Or PR_GUEST or ...
(yeah, good namespaces are all taken. )
Then, about the function name length, I'm fine if we did:
prot_guest_has()
or something even shorter, if you folks have a good suggestion.
Anyway, below is a diff ontop of your tree with what I think the barebones of this should be.
As a reply to this message I went and converted sme_active() to use protected_guest_has() too.
Comments, complaints?
Thx.
---
arch/x86/include/asm/mem_encrypt.h +0 -4
@@ -56,8 +56,6 @@ bool sev_es_active(void);#define __bss_decrypted __section(".bss..decrypted") -bool amd_protected_guest_has (unsigned long flag); -#else /* !CONFIG_AMD_MEM_ENCRYPT */ #define sme_me_mask 0ULL
@@ -88,8 +86,6 @@ early_set_memory_encrypted(unsigned long vaddr, unsigned long size) { return 0;static inline void mem_encrypt_free_decrypted_mem (void ) { } -static inline bool amd_protected_guest_has (unsigned long flag) {return false; } -#define __bss_decrypted#endif /* CONFIG_AMD_MEM_ENCRYPT */
arch/x86/include/asm/sev.h +5 -1
@@ -11,6 +11,7 @@#include <linux/types.h> #include <asm/insn.h> #include <asm/sev-common.h> +#include <asm/pgtable_types.h> #define GHCB_PROTO_OUR 0x0001UL#define GHCB_PROTOCOL_MAX 1ULL
@@ -81,12 +82,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 +#endif /* __ASM_ENCRYPTED_STATE_H */
arch/x86/kernel/sev.c +11 -0
@@ -1402,3 +1402,14 @@ bool __init handle_vc_boot_ghcb(struct pt_regs *regs)while (true)halt (); } + +bool sev_protected_guest_has (unsigned long flag) +{ +switch (flag) { +case VM_MEM_ENCRYPT : +case VM_MEM_ENCRYPT_ACTIVE : +return true; + } + +return false; +}
arch/x86/mm/mem_encrypt.c +0 -12
@@ -391,18 +391,6 @@ bool noinstr sev_es_active(void)return sev_status &MSR_AMD64_SEV_ES_ENABLED ; } -bool amd_protected_guest_has (unsigned long flag) -{ -switch (flag) { -case VM_MEM_ENCRYPT : -case VM_MEM_ENCRYPT_ACTIVE : -return true; - } - -return false; -} -EXPORT_SYMBOL_GPL (amd_protected_guest_has); -/* Override for DMA direct allocation check - AMD specific initialization */ bool amd_force_dma_unencrypted (struct device *dev) {
include/linux/protected_guest.h +6 -9
@@ -2,7 +2,9 @@#ifndef _LINUX_PROTECTED_GUEST_H#define _LINUX_PROTECTED_GUEST_H 1 -#include <linux/mem_encrypt.h> +#include <asm/processor.h> +#include <asm/tdx.h> +#include <asm/sev.h> /* Protected Guest Feature Flags (leave 0-0xff for arch specific flags) */
@@ -20,23 +22,18 @@#define VM_DISABLE_UNCORE_SUPPORT 0x105#if defined(CONFIG_INTEL_TDX_GUEST ) || defined(CONFIG_AMD_MEM_ENCRYPT ) - -#include <asm/tdx.h> -static inline bool protected_guest_has (unsigned long flag) {if (is_tdx_guest ())return tdx_protected_guest_has (flag); -else if (mem_encrypt_active ()) -return amd_protected_guest_has (flag); +else if (boot_cpu_data.x86_vendor ==X86_VENDOR_AMD ) +return sev_protected_guest_has (flag);return false; }#else -static inline bool protected_guest_has (unsigned long flag) {return false; } -#endif -#endif +#endif /* _LINUX_PROTECTED_GUEST_H */
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteBorislav Petkov
maintainerRe: [RFC v2-fix-v2 1/1] x86: Introduce generic protected guest abstractionn
From f1e9f051c86b09fe660f49b0307bc7c6cec5e6f4 Mon Sep 17 00:00:00 2001
From: Borislav Petkov <[email protected]>
Date: Thu, 3 Jun 2021 20:03:31 +0200
Subject: Convert sme_active()
arch/x86/include/asm/mem_encrypt.h +0 -2
@@ -50,7 +50,6 @@ void __init mem_encrypt_free_decrypted_mem(void);void __init mem_encrypt_init (void );void __init sev_es_init_vc_handling (void ); -bool sme_active (void );bool sev_active (void );bool sev_es_active (void );
@@ -75,7 +74,6 @@ static inline void __init sme_encrypt_kernel(struct boot_params *bp) { }static inline void __init sme_enable (struct boot_params *bp) { }static inline void sev_es_init_vc_handling (void ) { } -static inline bool sme_active (void ) {return false; }static inline bool sev_active (void ) {return false; }static inline bool sev_es_active (void ) {return false; }
arch/x86/kernel/machine_kexec_64.c +1 -1
@@ -387,7 +387,7 @@ void machine_kexec(struct kimage *image) (unsigned long )page_list, image->start, image->preserve_context, - sme_active()); + protected_guest_has(VM_HOST_MEM_ENCRYPT));#ifdef CONFIG_KEXEC_JUMP if (image->preserve_context)
arch/x86/kernel/pci-swiotlb.c +1 -1
@@ -49,7 +49,7 @@ int __init pci_swiotlb_detect_4gb(void)* buffers are allocated and used for devices that do not support * the addressing range required for the encryption mask. */ -if (sme_active ()) +if (protected_guest_has (VM_HOST_MEM_ENCRYPT )) swiotlb =1 ;return swiotlb;
arch/x86/kernel/sev.c +5 -0
@@ -1409,6 +1409,11 @@ bool sev_protected_guest_has(unsigned long flag)case VM_MEM_ENCRYPT :case VM_MEM_ENCRYPT_ACTIVE :return true; +case VM_HOST_MEM_ENCRYPT : +return sme_me_mask && !sev_active (); +default : +WARN_ON_ONCE (1 ); +return false; }return false;
arch/x86/mm/ioremap.c +2 -2
@@ -703,7 +703,7 @@ bool arch_memremap_can_ram_remap(resource_size_t phys_addr, unsigned long size,if (flags &MEMREMAP_DEC )return false; -if (sme_active ()) { +if (protected_guest_has (VM_HOST_MEM_ENCRYPT )) {if (memremap_is_setup_data (phys_addr, size) ||memremap_is_efi_data (phys_addr, size))return false;
@@ -729,7 +729,7 @@ pgprot_t __init early_memremap_pgprot_adjust(resource_size_t phys_addr, encrypted_prot = true; -if (sme_active ()) { +if (protected_guest_has (VM_HOST_MEM_ENCRYPT )) {if (early_memremap_is_setup_data (phys_addr, size) ||memremap_is_efi_data (phys_addr, size)) encrypted_prot = false;
arch/x86/mm/mem_encrypt.c +5 -10
@@ -145,7 +145,7 @@ void __init sme_unmap_bootdata(char *real_mode_data)struct boot_params *boot_data;unsigned long cmdline_paddr; -if (!sme_active ()) +if (!protected_guest_has (VM_HOST_MEM_ENCRYPT ))return ;/* Get the command line address before unmapping the real_mode_data */
@@ -165,7 +165,7 @@ void __init sme_map_bootdata(char *real_mode_data)struct boot_params *boot_data;unsigned long cmdline_paddr; -if (!sme_active ()) +if (!protected_guest_has (VM_HOST_MEM_ENCRYPT ))return ;__sme_early_map_unmap_mem (real_mode_data,sizeof (boot_params), true);
@@ -365,7 +365,7 @@ int __init early_set_memory_encrypted(unsigned long vaddr, unsigned long size)/* * SME and SEV are very similar but they are not the same, so there are * times that the kernel will need to distinguish between SME and SEV. The -* sme_active() and sev_active() functions are used for this. When a +* protected_guest_has(VM_HOST_MEM_ENCRYPT) and sev_active() functions are used for this. When a * distinction isn't needed, the mem_encrypt_active() function can be used. * * The trampoline code is a good example for this requirement. Before
@@ -378,11 +378,6 @@ bool sev_active(void) {return sev_status &MSR_AMD64_SEV_ENABLED ; } - -bool sme_active (void ) -{ -return sme_me_mask && !sev_active (); -}EXPORT_SYMBOL_GPL (sev_active);/* Needs to be called from non-instrumentable code */
@@ -405,7 +400,7 @@ bool amd_force_dma_unencrypted(struct device *dev)* device does not support DMA to addresses that include the * encryption mask. */ -if (sme_active ()) { +if (protected_guest_has (VM_HOST_MEM_ENCRYPT )) {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);
@@ -446,7 +441,7 @@ static void print_mem_encrypt_feature_info(void)pr_info ("AMD Memory Encryption Features active:" );/* Secure Memory Encryption */ -if (sme_active ()) { +if (protected_guest_has (VM_HOST_MEM_ENCRYPT )) {/* * SME is mutually exclusive with any of the SEV * features below.
arch/x86/mm/mem_encrypt_common.c +1 -1
@@ -15,7 +15,7 @@/* Override for DMA direct allocation check - ARCH_HAS_FORCE_DMA_UNENCRYPTED */ bool force_dma_unencrypted (struct device *dev) { -if (sev_active () ||sme_active ()) +if (sev_active () ||protected_guest_has (VM_HOST_MEM_ENCRYPT ))return amd_force_dma_unencrypted (dev);if (protected_guest_has (VM_MEM_ENCRYPT ))
arch/x86/mm/mem_encrypt_identity.c +2 -1
@@ -30,6 +30,7 @@#include <linux/kernel.h> #include <linux/mm.h> #include <linux/mem_encrypt.h> +#include <linux/protected_guest.h> #include <asm/setup.h> #include <asm/sections.h>
@@ -287,7 +288,7 @@ void __init sme_encrypt_kernel(struct boot_params *bp)unsigned long pgtable_area_len;unsigned long decrypted_base; -if (!sme_active ()) +if (!protected_guest_has (VM_HOST_MEM_ENCRYPT ))return ;/*
arch/x86/realmode/init.c +2 -2
@@ -42,7 +42,7 @@ void __init reserve_real_mode(void)static void sme_sev_setup_real_mode (struct trampoline_header *th) {#ifdef CONFIG_AMD_MEM_ENCRYPT -if (sme_active ()) +if (protected_guest_has (VM_HOST_MEM_ENCRYPT )) th->flags |=TH_FLAGS_SME_ACTIVE ;if (sev_es_active ()) {
@@ -79,7 +79,7 @@ static void __init setup_real_mode(void)* decrypted memory in order to bring up other processors * successfully. This is not needed for SEV. */ -if (sme_active ()) +if (protected_guest_has (VM_HOST_MEM_ENCRYPT ))set_memory_decrypted ((unsigned long )base, size >>PAGE_SHIFT );memcpy (base, real_mode_blob, size);
drivers/iommu/amd/init.c +2 -2
@@ -965,7 +965,7 @@ static bool copy_device_table(void)pr_err ("The address of old device table is above 4G, not trustworthy!\n" );return false; } - old_devtb = (sme_active () &&is_kdump_kernel ()) + old_devtb = (protected_guest_has (VM_HOST_MEM_ENCRYPT ) &&is_kdump_kernel ()) ? (__force void *)ioremap_encrypted (old_devtb_phys, dev_table_size) :memremap (old_devtb_phys, dev_table_size,MEMREMAP_WB );
@@ -3022,7 +3022,7 @@ static int __init amd_iommu_init(void)static bool amd_iommu_sme_check (void ) { -if (!sme_active () || (boot_cpu_data.x86 !=0x17 )) +if (!protected_guest_has (VM_HOST_MEM_ENCRYPT ) || (boot_cpu_data.x86 !=0x17 ))return true;/* For Fam17h, a specific level of support is required */
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [RFC v2-fix-v2 1/1] x86: Introduce generic protected guest abstractionn
arch/x86/kernel/pci-swiotlb.c int __init pci_swiotlb_detect_4gb(void) · line 53
Quoted 9 lines · Borislav Petkov
From
f1e9f051c86b09fe660f49b0307bc7c6cec5e6f4Mon Sep 17 00:00:00 2001
From: Borislav Petkov <[email protected]>
Date: Thu, 3 Jun 2021 20:03:31 +0200
Subject: Convertsme_active()arch/x86/kernel/pci-swiotlb.c +1
-1*/ -if (sme_active ()) +if (protected_guest_has (VM_HOST_MEM_ENCRYPT )) swiotlb =1 ;
I still feel this is confusing. SME is a host/bare-metal technology, so calling protected_guest_has() seems odd and using VM_HOST_MEM_ENCRYPT, where I assume VM is short for virtual machine, also seems odd.
How about just protected_os_has()? Then you could have
- HOST_MEM_ENCRYPT for host memory encryption
- GUEST_MEM_ENCRYPT for guest memory encryption
- MEM_ENCRYPT for either host or guest memory encryption.
The first is analogous to sme_active(), the second to sev_active() and the third to mem_encrypt_active(). Just my opinion, though...
Quoted 7 lines
arch/x86/kernel/pci-swiotlb.c +0
-0return swiotlb;arch/x86/kernel/sev.c +0
-0
arch/x86/kernel/sev.c bool sev_protected_guest_has(unsigned long flag) · line 1416
Quoted 8 lines
arch/x86/kernel/sev.c +5
-0@@ -1409,6 +1409,11 @@ bool sev_protected_guest_has(unsigned long flag)case VM_MEM_ENCRYPT :case VM_MEM_ENCRYPT_ACTIVE :return true; +case VM_HOST_MEM_ENCRYPT : +return sme_me_mask && !sev_active (); +default : +WARN_ON_ONCE (1 ); +return false;
I don't think you want a WARN_ON_ONCE() here. The code will be written to work with either SEV or TDX, so we shouldn't warn on a check for a TDX supported feature when running on AMD (or vice-versa).
Thanks,
Tom
Re: [RFC v2-fix-v2 1/1] x86: Introduce generic protected guest abstractionn
Quoted 8 lines · Tom Lendacky
arch/x86/kernel/pci-swiotlb.c int __init pci_swiotlb_detect_4gb(void) · line 53
Quoted 4 lines
*/
arch/x86/kernel/pci-swiotlb.c +1
-1-if (sme_active ()) +if (protected_guest_has (VM_HOST_MEM_ENCRYPT )) swiotlb =1 ;I still feel this is confusing. SME is a host/bare-metal technology, so calling
protected_guest_has()seems odd and usingVM_HOST_MEM_ENCRYPT, where I assume VM is short for virtual machine, also seems odd.
Quoted 8 lines
How about just
protected_os_has()? Then you could have
-HOST_MEM_ENCRYPTfor host memory encryption
-GUEST_MEM_ENCRYPTfor guest memory encryption
-MEM_ENCRYPTfor either host or guest memory encryption.The first is analogous to
sme_active(), the second tosev_active()and the third tomem_encrypt_active(). Just my opinion, though...
I am not sure whether OS makes sense here. But I am fine with it if it is maintainers choice.
Other option could be protected_boot_has()?
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Borislav Petkov
maintainerRe: [RFC v2-fix-v2 1/1] x86: Introduce generic protected guest abstractionn
In reply to Tom LendackyQuoted 2 lines · Tom Lendacky
The first is analogous to
sme_active(), the second tosev_active()and the third tomem_encrypt_active(). Just my opinion, though...
Yeah, or cc_has() where "cc" means "confidential computing". Or "coco"...
Yeah, no good idea yet.
Quoted 3 lines
I don't think you want a
WARN_ON_ONCE()here. The code will be written to work with either SEV or TDX, so we shouldn't warn on a check for a TDX supported feature when running on AMD (or vice-versa).
That's an AMD-specific path so it would warn only when a flag is used which is unknown/unused yet on AMD.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [RFC v2-fix-v2 1/1] x86: Introduce generic protected guest abstractionn
Quoted 6 lines · Borislav Petkov
Quoted 2 lines · Tom Lendacky
The first is analogous to
sme_active(), the second tosev_active()and the third tomem_encrypt_active(). Just my opinion, though...Yeah, or
cc_has()where "cc" means "confidential computing". Or "coco"...
Quoted 8 lines
Yeah, no good idea yet.
Quoted 3 lines
I don't think you want a
WARN_ON_ONCE()here. The code will be written to work with either SEV or TDX, so we shouldn't warn on a check for a TDX supported feature when running on AMD (or vice-versa).That's an AMD-specific path so it would warn only when a flag is used which is unknown/unused yet on AMD.
But the check can happen on Intel or AMD. We have lots of checks for sme_active() in common code that are executed on Intel today, but they just return false. It's the same principle, you don't want to WARN on those, just return false. E.g.:
/* some common code path */ if (cc_has(XYZ)) do_y();
If Intel has XYZ but AMD does not, you don't want to WARN, just return false.
Thanks,
Tom
Quoted 1 line
Borislav Petkov
maintainerRe: [RFC v2-fix-v2 1/1] x86: Introduce generic protected guest abstractionn
Quoted 1 line · Tom Lendacky
If Intel has XYZ but AMD does not, you don't want to WARN, just return false.
Aha, *now*, I see what you mean. Ok, so the reason why I added the WARN is to sanity-check whether we're handling all possible VM_* or PROT_GUEST_* flags properly and whether we're missing some. As a debugging help. It'll get removed before applying I guess.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [RFC v2-fix-v2 1/1] x86: Introduce generic protected guest abstractionn
Quoted 4 lines · Borislav Petkov
Aha,*now*, I see what you mean. Ok, so the reason why I added the WARN is to sanity-check whether we're handling all possible VM_* or PROT_GUEST_* flags properly and whether we're missing some. As a debugging help. It'll get removed before applying I guess.
Borislav/Tom,
Any consensus on function name and flag prefix?
Currently suggested function names are,
cc_has() or protected_guest_has() or prot_guest_has() or protected_boot_has()
For flag prefix either PR_GUEST_* or CC_*
I am planning to submit another version of this patch with suggested fixes. If we could reach some consensus on function and flag names, I can include them in it. If not, I will submit next version without any renames.
Please let me know your comments.
BTW, my choice is protected_guest_has() or CC_has().
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Borislav Petkov
maintainerRe: [RFC v2-fix-v2 1/1] x86: Introduce generic protected guest abstractionn
Quoted 1 line · Sathyanarayanan
cc_has()orprotected_guest_has()orprot_guest_has()orprotected_boot_has()
Even if I still think it is not optimal, prot_guest_has() seems to be best what we have because protected_guest_has() together with the flag will become just too long to scan at a quick glance. And if you have to do two tests, you'd have to break the line.
Quoted 1 line
For flag prefix either PR_GUEST_* or CC_*
PR_GUEST_* sounds ok to me.
The "cc" prefix stuff is nice and short but it doesn't say what it means because it is simply too short. And code readability is very important.
I'd say.
Still open for better suggestions though.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [RFC v2-fix-v2 1/1] x86: Introduce generic protected guest abstractionn
In reply to Borislav PetkovQuoted 30 lines · Borislav Petkov
From
f1e9f051c86b09fe660f49b0307bc7c6cec5e6f4Mon Sep 17 00:00:00 2001
From: Borislav Petkov <[email protected]>
Date: Thu, 3 Jun 2021 20:03:31 +0200
Subject: Convertsme_active()arch/x86/include/asm/mem_encrypt.h +0
-2@@ -50,7 +50,6 @@ void __init mem_encrypt_free_decrypted_mem(void);void __init mem_encrypt_init (void );void __init sev_es_init_vc_handling (void ); -bool sme_active (void );bool sev_active (void );bool sev_es_active (void );@@ -75,7 +74,6 @@ static inline void __init sme_encrypt_kernel(struct boot_params *bp) { }static inline void __init sme_enable (struct boot_params *bp) { }static inline void sev_es_init_vc_handling (void ) { } -static inline bool sme_active (void ) {return false; }static inline bool sev_active (void ) {return false; }static inline bool sev_es_active (void ) {return false; }arch/x86/kernel/machine_kexec_64.c +0
-0
arch/x86/kernel/machine_kexec_64.c void machine_kexec(struct kimage *image) · line 393
Quoted 8 lines
arch/x86/kernel/machine_kexec_64.c +1
-1@@ -387,7 +387,7 @@ void machine_kexec(struct kimage *image) (unsigned long )page_list, image->start, image->preserve_context, - sme_active()); + protected_guest_has(VM_HOST_MEM_ENCRYPT));#ifdef CONFIG_KEXEC_JUMP if (image->preserve_context)
I think conversions like this are wrong: relocate_kernel(), which got called here, only knows how to deal with SME, not how to handle some generic case.
(After a quick check, looks like all conversions in the patch are wrong for the same reason.)
If code is written to handle a specific technology we need to stick with a check that makes it clear. Trying to make sound generic only leads to confusion.
Also, we have host memory encryption that doesn't require any of this
code: TME makes the ectryption transparently to OS.
Maybe it's better to take a conservative path: keep a check specific until we find it can serve more than one HW feature?
Signature · 1 line
Kirill A. Shutemov
Borislav Petkov
maintainerRe: [RFC v2-fix-v2 1/1] x86: Introduce generic protected guest abstractionn
Quoted 3 lines · Kirill A. Shutemov
I think conversions like this are wrong:
relocate_kernel(), which got called here, only knows how to deal with SME, not how to handle some generic case.
What do you mean wrong? Wrong for TDX?
If so, then that can be
protected_guest_has(SME)
or so, which would be false on Intel.
And this patch was only a mechanical conversion to see how it would look like.
Quoted 3 lines
If code is written to handle a specific technology we need to stick with a check that makes it clear. Trying to make sound generic only leads to confusion.
Sure, fine by me.
And I don't want a zoo of gazillion small checking functions per technology. sev_<something>, tdx_<something>, yadda yadda.
So stuff better be unified. Even if you'd have vendor-specific defines you hand into that function - and you will have such - it is still much saner than what it turns into with the AMD side of things.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [RFC v2-fix-v2 1/1] x86: Introduce generic protected guest abstractionn
Quoted 5 lines · Borislav Petkov
Quoted 3 lines · Kirill A. Shutemov
I think conversions like this are wrong:
relocate_kernel(), which got called here, only knows how to deal with SME, not how to handle some generic case.
Quoted 7 lines
What do you mean wrong? Wrong for TDX?
If so, then that can be
protected_guest_has(SME)or so, which would be false on Intel.
I agree. Since most of the code changed in this patch is not applicable to TDX, it might need product specific or new function specific flags.
Quoted 8 lines
And this patch was only a mechanical conversion to see how it would look like.
Quoted 3 lines
If code is written to handle a specific technology we need to stick with a check that makes it clear. Trying to make sound generic only leads to confusion.
Quoted 8 lines
Sure, fine by me.
And I don't want a zoo of gazillion small checking functions per technology. sev_<something>, tdx_<something>, yadda yadda.
So stuff better be unified. Even if you'd have vendor-specific defines you hand into that function - and you will have such - it is still much saner than what it turns into with the AMD side of things.
Agree. Currently we share code with AMD SEV in memory encryption support and string I/O handling code. So defining common flag for such code is useful.
Quoted 1 line
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
RFC 1/1 x86: 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.
prot_guest_has() is used to check for protected guest feature flags.
---
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/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 | 38 +++++++++++++++++++++++++++++++++ 6 files changed, 79 insertions(+) create mode 100644 include/linux/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 +38 -0
@@ -0,0 +1,38 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _LINUX_PROTECTED_GUEST_H +#define _LINUX_PROTECTED_GUEST_H 1 + +#include <asm/processor.h> +#include <asm/tdx.h> +#include <asm/sev.h> + +/* 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 + +#if defined(CONFIG_INTEL_TDX_GUEST ) || defined(CONFIG_AMD_MEM_ENCRYPT ) + +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; +} + +#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-v2 1/1] x86: Introduce generic protected guest abstraction
In reply to Borislav PetkovQuoted 28 lines · Borislav Petkov
Quoted 14 lines · Kuppuswamy Sathyanarayanan
include/linux/protected_guest.h new file +8
-0@@ -0,0 +1,40 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _LINUX_PROTECTED_GUEST_H +#define _LINUX_PROTECTED_GUEST_H 1 + +#include <linux/mem_encrypt.h> + +/* Protected Guest Feature Flags (leave 0-0xff for arch specific flags) */ +include/linux/protected_guest.h line 18
Quoted 10 lines
include/linux/protected_guest.h new file +10
-0+/* Support for guest encryption */ +#define VM_MEM_ENCRYPT 0x100 +/* Encryption support is active */ +#define VM_MEM_ENCRYPT_ACTIVE 0x101 +/* Support for unrolled string IO */ +#define VM_UNROLL_STRING_IO 0x102 +/* Support for host memory encryption */ +#define VM_HOST_MEM_ENCRYPT 0x103 +/* Support for shared mapping initialization (after early init) */ +#define VM_SHARED_MAPPING_INIT 0x104Ok, a couple of things:
Quoted 5 lines
first of all, those flags with that
VM_prefix make me think of "virtual memory" instead of "virtual machine". So they should be something else, like, say
PR_G_... for Protected Guest or so. OrPR_GUESTor ...
I would prefer PR_GUEST over PR_G_
Quoted 80 lines
(yeah, good namespaces are all taken. )
Then, about the function name length, I'm fine if we did:
prot_guest_has()or something even shorter, if you folks have a good suggestion.
Anyway, below is a diff ontop of your tree with what I think the barebones of this should be.
As a reply to this message I went and converted
sme_active()to useprotected_guest_has()too.Comments, complaints?
Thx.
---
arch/x86/include/asm/mem_encrypt.h +0
-4@@ -56,8 +56,6 @@ bool sev_es_active(void);#define __bss_decrypted __section(".bss..decrypted") -bool amd_protected_guest_has (unsigned long flag); -#else /* !CONFIG_AMD_MEM_ENCRYPT */ #define sme_me_mask 0ULL@@ -88,8 +86,6 @@ early_set_memory_encrypted(unsigned long vaddr, unsigned long size) { return 0;static inline void mem_encrypt_free_decrypted_mem (void ) { } -static inline bool amd_protected_guest_has (unsigned long flag) {return false; } -#define __bss_decrypted#endif /* CONFIG_AMD_MEM_ENCRYPT */ arch/x86/include/asm/sev.h +5
-1@@ -11,6 +11,7 @@#include <linux/types.h> #include <asm/insn.h> #include <asm/sev-common.h> +#include <asm/pgtable_types.h> #define GHCB_PROTO_OUR 0x0001UL#define GHCB_PROTOCOL_MAX 1ULL@@ -81,12 +82,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 +#endif /* __ASM_ENCRYPTED_STATE_H */ arch/x86/kernel/sev.c +1
-0@@ -1402,3 +1402,14 @@ bool __init handle_vc_boot_ghcb(struct pt_regs *regs)while (true)halt (); } +
arch/x86/kernel/sev.c bool __init handle_vc_boot_ghcb(struct pt_regs *regs) · line 1415
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+bool sev_protected_guest_has (unsigned long flag) +{ +switch (flag) { +case VM_MEM_ENCRYPT : +case VM_MEM_ENCRYPT_ACTIVE : +return true; + } + +return false; +}
I assume this file will get compiled for both SEV and SME cases.
Quoted 43 lines
arch/x86/mm/mem_encrypt.c +0
-12@@ -391,18 +391,6 @@ bool noinstr sev_es_active(void)return sev_status &MSR_AMD64_SEV_ES_ENABLED ; } -bool amd_protected_guest_has (unsigned long flag) -{ -switch (flag) { -case VM_MEM_ENCRYPT : -case VM_MEM_ENCRYPT_ACTIVE : -return true; - } - -return false; -} -EXPORT_SYMBOL_GPL (amd_protected_guest_has); -/* Override for DMA direct allocation check - AMD specific initialization */ bool amd_force_dma_unencrypted (struct device *dev) {include/linux/protected_guest.h +3
-2@@ -2,7 +2,9 @@#ifndef _LINUX_PROTECTED_GUEST_H#define _LINUX_PROTECTED_GUEST_H 1 -#include <linux/mem_encrypt.h> +#include <asm/processor.h> +#include <asm/tdx.h> +#include <asm/sev.h> /* Protected Guest Feature Flags (leave 0-0xff for arch specific flags) */ @@ -20,23 +22,18 @@#define VM_DISABLE_UNCORE_SUPPORT 0x105#if defined(CONFIG_INTEL_TDX_GUEST ) || defined(CONFIG_AMD_MEM_ENCRYPT ) -
include/linux/protected_guest.h line 30
Quoted 10 lines
include/linux/protected_guest.h +2
-4-#include <asm/tdx.h> -static inline bool protected_guest_has (unsigned long flag) {if (is_tdx_guest ())return tdx_protected_guest_has (flag); -else if (mem_encrypt_active ()) -return amd_protected_guest_has (flag); +else if (boot_cpu_data.x86_vendor ==X86_VENDOR_AMD ) +return sev_protected_guest_has (flag);
Since you are checking for AMD vendor ID, why not use amd_protected_guest_has()?
Quoted 14 lines
include/linux/protected_guest.h +1
-3return false; }#else -static inline bool protected_guest_has (unsigned long flag) {return false; } -#endif -#endif +#endif /* _LINUX_PROTECTED_GUEST_H */
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Borislav Petkov
maintainerRe: [RFC v2-fix-v2 1/1] x86: Introduce generic protected guest abstraction
Sathya,
please trim your mails when you reply, like I've done in this reply.
Thx.
Quoted 1 line · Sathyanarayanan
I assume this file will get compiled for both SEV and SME cases.
Yap.
Quoted 1 line
Since you are checking for AMD vendor ID, why not use
amd_protected_guest_has()?
Because, as Sean already told you, we should either stick to the
technologies: TDX or SEV or to the vendors: Intel or AMD - but not either or.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [RFC v2-fix-v2 1/1] x86: Introduce generic protected guest abstraction
Quoted 4 lines · Borislav Petkov
Quoted 1 line
Since you are checking for AMD vendor ID, why not use
amd_protected_guest_has()?Because, as Sean already told you, we should either stick to the
technologies: TDX or SEV or to the vendors: Intel or AMD - but not either or.
Ok. We can go with technologies for now. In future, if protected_guest_has() is extended for other technologies like MKTME, then we can generalize it base on vendor.
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Re: [RFC v2-fix-v2 1/1] x86: Introduce generic protected guest abstraction
In reply to Borislav PetkovQuoted 1 line · Borislav Petkov
On Tue, Jun 01, 2021 at 02:14:17PM -0700, Kuppuswamy Sathyanarayanan wrote:
snip
Quoted 16 lines
include/linux/protected_guest.h +3
-1@@ -2,7 +2,9 @@#ifndef _LINUX_PROTECTED_GUEST_H#define _LINUX_PROTECTED_GUEST_H 1 -#include <linux/mem_encrypt.h> +#include <asm/processor.h> +#include <asm/tdx.h> +#include <asm/sev.h> /* Protected Guest Feature Flags (leave 0-0xff for arch specific flags) */
include/linux/protected_guest.h line 25
Quoted 6 lines
include/linux/protected_guest.h +0
-3@@ -20,23 +22,18 @@#define VM_DISABLE_UNCORE_SUPPORT 0x105#if defined(CONFIG_INTEL_TDX_GUEST ) || defined(CONFIG_AMD_MEM_ENCRYPT ) - -#include <asm/tdx.h> -
Why move this header outside CONFIG_INTEL_TDX_GUEST or CONFIG_AMD_MEM_ENCRYPT ifdef?
This header only exists in x86 arch code. So it is better to protect it with x86 specific header file.
Quoted 22 lines
include/linux/protected_guest.h +3
-5static inline bool protected_guest_has (unsigned long flag) {if (is_tdx_guest ())return tdx_protected_guest_has (flag); -else if (mem_encrypt_active ()) -return amd_protected_guest_has (flag); +else if (boot_cpu_data.x86_vendor ==X86_VENDOR_AMD ) +return sev_protected_guest_has (flag);return false; }#else -static inline bool protected_guest_has (unsigned long flag) {return false; } -#endif -#endif +#endif /* _LINUX_PROTECTED_GUEST_H */
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Borislav Petkov
maintainerRe: [RFC v2-fix-v2 1/1] x86: Introduce generic protected guest abstraction
Quoted 2 lines · Sathyanarayanan
Why move this header outside
CONFIG_INTEL_TDX_GUESTorCONFIG_AMD_MEM_ENCRYPTifdef?
Because asm headers are usually included at the beginning of another, possibly generic header. Unless you have a specially particular reason to put them in additional guarding ifdeffery. Have a look at include/linux/.
Quoted 2 lines
This header only exists in x86 arch code. So it is better to protect it with x86 specific header file.
That doesn't sound like a special reason to me. And compilers are usually very able at discarding unused symbols so I don't see a problem with keeping all includes at the top, like it is usually done.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [RFC v2-fix-v2 1/1] x86: Introduce generic protected guest abstraction
Quoted 5 lines · Borislav Petkov
Quoted 2 lines
This header only exists in x86 arch code. So it is better to protect it with x86 specific header file.
That doesn't sound like a special reason to me. And compilers are usually very able at discarding unused symbols so I don't see a problem with keeping all includes at the top, like it is usually done.
I am still not clear. What happens when a driver which includes linux/protected-guest.h is compiled for non-x86 arch (s390 or arm64)?
Since asm/sev.h and asm/tdx.h exists only in x86_64 arch, IMO, it should be placed under CONFIG_INTEL_TDX_GUEST or CONFIG_AMD_MEM_ENCRYPT
did I miss anything?
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Borislav Petkov
maintainerRe: [RFC v2-fix-v2 1/1] x86: Introduce generic protected guest abstraction
Quoted 2 lines · Sathyanarayanan
I am still not clear. What happens when a driver which includes
linux/protected-guest.his compiled for non-x86 arch (s390 or arm64)?
I was wondering what felt weird: why is prot{ected,}_guest_has() in a generic linux/ namespace header and not in an asm/ one?
I think the proper way is for the other arches should be to provide their own prot_guest_has() implementation which generic code uses and the generic header would contain only the PR_GUEST_* defines.
Take ioremap() as an example:
arch/x86/include/asm/io.harch/arm64/include/asm/io.harch/s390/include/asm/io.h
...
and pretty much every arch has that arch-specific io.h header which defines ioremap() and generic code includes include/linux/io.h which includes the respective asm/io.h header so that users can call the respective ioremap() implementation.
prot_guest_has() sounds just the same to me.
Better?
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [RFC v2-fix-v2 1/1] x86: Introduce generic protected guest abstraction
Quoted 18 lines · Borislav Petkov
Quoted 2 lines · Sathyanarayanan
I am still not clear. What happens when a driver which includes
linux/protected-guest.his compiled for non-x86 arch (s390 or arm64)?I was wondering what felt weird: why is prot{ected,}_guest_has() in a generic linux/ namespace header and not in an asm/ one?
I think the proper way is for the other arches should be to provide their own
prot_guest_has()implementation which generic code uses and the generic header would contain only the PR_GUEST_* defines.Take
ioremap()as an example:
arch/x86/include/asm/io.harch/arm64/include/asm/io.harch/s390/include/asm/io.h
...
Quoted 6 lines
and pretty much every arch has that arch-specific
io.hheader which definesioremap()and generic code includesinclude/linux/io.hwhich includes the respectiveasm/io.hheader so that users can call the respectiveioremap()implementation.
prot_guest_has()sounds just the same to me.
ioremap() is required for all architectures. So I think adding support for it and creating io.h for every arch seems valid. But are you sure every arch cares about protected guest support?
IMHO, its better to leave it to arch maintainers to decide if they want to support protected guest or not.
This can be easily achieved by defining generic arch independent config option ARCH_HAS_PORTECTED_GUEST.
And any arch which wants to support prot_guest_has() can enable above config option and create their own asm/protected_guest.h
This model is similar to linux/mem_encrypt.h.
With above suggested change, header file will look like below. And we don't need implement asm/protected_guest.h for every available arch.
--- a/include/linux/protected_guest.h
+++ b/include/linux/protected_guest.h
#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 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 */
Quoted 3 lines
Better?
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Borislav Petkov
maintainerRe: [RFC v2-fix-v2 1/1] x86: Introduce generic protected guest abstraction
Quoted 2 lines · Sathyanarayanan
And any arch which wants to support
prot_guest_has()can enable above config option and create their own asm/protected_guest.
I wouldnt've done even that but only the x86 asm version of protected_guest.h and left it to other arches to extend it. I don't like "preempting" use of functionality by other arches and would leave them to extend stuff themselves, as they see fit, but ok, ARCH_HAS_PROTECTED_GUEST sounds clean enough to me too, so sure, that's fine too.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquette