Add TDX Guest Support (shared-mm support)
Subsystems (3)
v2 cover Add TDX Guest Support (shared-mm support)
Hi All,
Intel's Trust Domain Extensions (TDX) protect guest VMs from malicious hosts and some physical attacks. Since VMM is untrusted entity, it does not allow VMM to access guest private memory. Any memory that is required for communication with VMM must be shared explicitly. This series adds support to securely share guest memory with VMM when it is required by guest.
This series is the continuation of the patch series titled "Add TDX Guest Support (Initial support)", "Add TDX Guest Support (#VE handler support)" and "Add TDX Guest Support (boot fixes)" which added initial support,
#VE handler support and boot fixes for TDX guests. You can find the
related patchsets in the following links.
[set 1] - https://lore.kernel.org/patchwork/project/lkml/list/?series=505232
[set 2] - https://lore.kernel.org/patchwork/project/lkml/list/?series=506230
[set 3] - https://lore.kernel.org/patchwork/project/lkml/list/?series=506231
Also please note that this series alone is not necessarily fully functional. You need to apply all the above 3 patch series to get a fully functional TDX guest.
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
Isaku Yamahata (1):
x86/tdx: ioapic: Add shared bit for IOAPIC base address
Kirill A. Shutemov (6):
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 hypercall x86/tdx: Make DMA pages shared
x86/kvm: Use bounce buffers for TD guest
arch/x86/Kconfig | 9 +++- arch/x86/include/asm/mem_encrypt_common.h | 20 ++++++++ arch/x86/include/asm/pgtable.h | 5 ++ arch/x86/include/asm/tdx.h | 22 +++++++++ arch/x86/kernel/apic/io_apic.c | 18 ++++++- arch/x86/kernel/tdx.c | 60 +++++++++++++++++++++++ arch/x86/mm/Makefile | 2 + arch/x86/mm/ioremap.c | 10 ++-- arch/x86/mm/mem_encrypt.c | 10 ++-- arch/x86/mm/mem_encrypt_common.c | 38 ++++++++++++++ arch/x86/mm/pat/set_memory.c | 49 +++++++++++++++--- 11 files changed, 222 insertions(+), 21 deletions(-) create mode 100644 arch/x86/include/asm/mem_encrypt_common.h create mode 100644 arch/x86/mm/mem_encrypt_common.c
Signature · 1 line
2.25.1
PATCH v2 1/7 x86/mm: Move force_dma_unencrypted() to common code
From: "Kirill A. Shutemov" <[email protected]>
Intel TDX doesn't allow VMM to access guest private memory. Any memory that is required for communication with VMM must be shared explicitly by setting the bit in page table entry. After setting the shared bit, the conversion must be completed with MapGPA hypercall. You can find details about MapGPA hypercall in [1], sec 3.2.
The call informs VMM about the conversion between private/shared mappings. The shared memory is similar to unencrypted memory in AMD SME/SEV terminology but the underlying process of sharing/un-sharing the memory is different for Intel TDX guest platform.
SEV assumes that I/O devices can only do DMA to "decrypted" physical addresses without the C-bit set. In order for the CPU to interact with this memory, the CPU needs a decrypted mapping. To add this support, AMD SME code forces force_dma_unencrypted() to return true for platforms that support AMD SEV feature. It will be used for DMA memory allocation API to trigger set_memory_decrypted() for platforms that support AMD SEV feature.
TDX is similar. So, to communicate with I/O devices, related pages need to be marked as shared. As mentioned above, shared memory in TDX architecture is similar to decrypted memory in AMD SME/SEV. So similar to AMD SEV, force_dma_unencrypted() has to forced to return true. This support is added in other patches in this series.
So move force_dma_unencrypted() out of AMD specific code and call AMD specific (amd_force_dma_unencrypted()) initialization function from it. force_dma_unencrypted() will be modified by later patches to include Intel TDX guest platform specific initialization.
Also, introduce new config option X86_MEM_ENCRYPT_COMMON that has to be selected by all x86 memory encryption features. This will be selected by both AMD SEV and Intel TDX guest config options.
This is preparation for TDX changes in DMA code and it has no functional change.
---
Changes since v1:
* Removed sev_active(), sme_active() checks in force_dma_unencrypted().
arch/x86/Kconfig | 8 ++++++-- arch/x86/include/asm/mem_encrypt_common.h | 18 ++++++++++++++++++ arch/x86/mm/Makefile | 2 ++ arch/x86/mm/mem_encrypt.c | 5 +++-- arch/x86/mm/mem_encrypt_common.c | 17 +++++++++++++++++ 5 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 arch/x86/include/asm/mem_encrypt_common.h create mode 100644 arch/x86/mm/mem_encrypt_common.c
arch/x86/Kconfig +6 -2
@@ -1535,16 +1535,20 @@ config X86_CPA_STATISTICS helps to determine the effectiveness of preserving large and huge page mappings when mapping protections are changed. +config X86_MEM_ENCRYPT_COMMON + select ARCH_HAS_FORCE_DMA_UNENCRYPTED + select DYNAMIC_PHYSICAL_MASK + def_bool n + config AMD_MEM_ENCRYPT bool "AMD Secure Memory Encryption (SME) support" depends on X86_64 && CPU_SUP_AMD select DMA_COHERENT_POOL - select DYNAMIC_PHYSICAL_MASK select ARCH_USE_MEMREMAP_PROT - select ARCH_HAS_FORCE_DMA_UNENCRYPTED select INSTRUCTION_DECODER select ARCH_HAS_RESTRICTED_VIRTIO_MEMORY_ACCESS select ARCH_HAS_PROTECTED_GUEST + select X86_MEM_ENCRYPT_COMMON help Say yes to enable support for the encryption of system memory. This requires an AMD processor that supports Secure Memory
arch/x86/include/asm/mem_encrypt_common.h new file +18 -0
@@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (C) 2020 Intel Corporation */ +#ifndef _ASM_X86_MEM_ENCRYPT_COMMON_H +#define _ASM_X86_MEM_ENCRYPT_COMMON_H + +#include <linux/mem_encrypt.h> +#include <linux/device.h> + +#ifdef CONFIG_AMD_MEM_ENCRYPT +bool amd_force_dma_unencrypted (struct device *dev); +#else /* CONFIG_AMD_MEM_ENCRYPT */ +static inline bool amd_force_dma_unencrypted (struct device *dev) +{ +return false; +} +#endif /* CONFIG_AMD_MEM_ENCRYPT */ + +#endif
arch/x86/mm/Makefile +2 -0
@@ -52,6 +52,8 @@ obj-$(CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS) += pkeys.o obj-$(CONFIG_RANDOMIZE_MEMORY) += kaslr.o obj-$(CONFIG_PAGE_TABLE_ISOLATION) += pti.o +obj-$(CONFIG_X86_MEM_ENCRYPT_COMMON) += mem_encrypt_common.o + obj-$(CONFIG_AMD_MEM_ENCRYPT) += mem_encrypt.o obj-$(CONFIG_AMD_MEM_ENCRYPT) += mem_encrypt_identity.o obj-$(CONFIG_AMD_MEM_ENCRYPT) += mem_encrypt_boot.o
arch/x86/mm/mem_encrypt.c +3 -2
@@ -30,6 +30,7 @@#include <asm/processor-flags.h> #include <asm/msr.h> #include <asm/cmdline.h> +#include <asm/mem_encrypt_common.h> #include "mm_internal.h"
@@ -389,8 +390,8 @@ bool noinstr sev_es_active(void)return sev_status &MSR_AMD64_SEV_ES_ENABLED ; } -/* Override for DMA direct allocation check - ARCH_HAS_FORCE_DMA_UNENCRYPTED */ -bool force_dma_unencrypted (struct device *dev) +/* Override for DMA direct allocation check - AMD specific initialization */ +bool amd_force_dma_unencrypted (struct device *dev) {/* * For SEV, all DMA must be to unencrypted addresses.
arch/x86/mm/mem_encrypt_common.c new file +17 -0
@@ -0,0 +1,17 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Memory Encryption Support Common Code +* +* Copyright (C) 2021 Intel Corporation +* +* Author: Kuppuswamy Sathyanarayanan <[email protected]> +*/ + +#include <asm/mem_encrypt_common.h> +#include <linux/dma-mapping.h> + +/* Override for DMA direct allocation check - ARCH_HAS_FORCE_DMA_UNENCRYPTED */ +bool force_dma_unencrypted (struct device *dev) +{ +return amd_force_dma_unencrypted (dev); +}
Signature · 1 line
2.25.1
PATCH v2 2/7 x86/tdx: Exclude Shared bit from physical_mask
In reply to Kuppuswamy SathyanarayananFrom: "Kirill A. Shutemov" <[email protected]>
Just like MKTME, TDX reassigns bits of the physical address for metadata. MKTME used several bits for an encryption KeyID. TDX uses a single bit in guests to communicate whether a physical page should be protected by TDX as private memory (bit set to 0) or unprotected and shared with the VMM (bit set to 1).
Add a helper, tdg_shared_mask() to generate the mask. The processor enumerates its physical address width to include the shared bit, which means it gets included in __PHYSICAL_MASK by default.
Remove the shared mask from 'physical_mask' since any bits in tdg_shared_mask() are not used for physical addresses in page table entries.
Also, note that shared mapping configuration cannot be clubbed between AMD SME and Intel TDX Guest platforms in common function. SME has to do it very early in __startup_64() as it sets the bit on all memory, except what is used for communication. TDX can postpone it, as it don't need any shared mapping in very early boot.
---
Changes since v1:
* Fixed format issues in commit log.
arch/x86/Kconfig | 1 + arch/x86/include/asm/tdx.h | 4 ++++ arch/x86/kernel/tdx.c | 9 +++++++++ 3 files changed, 14 insertions(+)
arch/x86/Kconfig +1 -0
@@ -883,6 +883,7 @@ config INTEL_TDX_GUEST select X86_X2APIC select SECURITY_LOCKDOWN_LSM select ARCH_HAS_PROTECTED_GUEST + select X86_MEM_ENCRYPT_COMMON help Provide support for running in a trusted domain on Intel processors equipped with Trusted Domain eXtensions. TDX is a new Intel
arch/x86/include/asm/tdx.h +4 -0
@@ -77,6 +77,8 @@ int tdg_handle_virtualization_exception(struct pt_regs *regs,bool tdg_early_handle_ve (struct pt_regs *regs); +extern phys_addr_t tdg_shared_mask (void ); +/* * To support I/O port access in decompressor or early kernel init * code, since #VE exception handler cannot be used, use paravirt
@@ -145,6 +147,8 @@ static inline bool tdx_prot_guest_has(unsigned long flag) { return false; }static inline bool tdg_early_handle_ve (struct pt_regs *regs) {return false; } +static inline phys_addr_t tdg_shared_mask (void ) {return 0 ; } +#endif /* CONFIG_INTEL_TDX_GUEST */ #ifdef CONFIG_INTEL_TDX_GUEST_KVM
arch/x86/kernel/tdx.c +9 -0
@@ -78,6 +78,12 @@ bool tdx_prot_guest_has(unsigned long flag) }EXPORT_SYMBOL_GPL (tdx_prot_guest_has); +/* The highest bit of a guest physical address is the "sharing" bit */ +phys_addr_t tdg_shared_mask (void ) +{ +return 1ULL << (td_info.gpa_width -1 ); +} +static void tdg_get_info (void ) {u64 ret;
@@ -89,6 +95,9 @@ static void tdg_get_info(void) td_info.gpa_width = out.rcx &GENMASK (5 ,0 ); td_info.attributes = out.rdx; + +/* Exclude Shared bit from the __PHYSICAL_MASK */ + physical_mask &= ~tdg_shared_mask (); } static __cpuidle void tdg_halt(void)
Signature · 1 line
2.25.1
PATCH v2 3/7 x86/tdx: Make pages shared in ioremap()
In reply to Kuppuswamy SathyanarayananFrom: "Kirill A. Shutemov" <[email protected]>
All ioremap()ed pages that are not backed by normal memory (NONE or RESERVED) have to be mapped as shared.
Reuse the infrastructure from AMD SEV code.
Note that DMA code doesn't use ioremap() to convert memory to shared as DMA buffers backed by normal memory. DMA code make buffer shared with set_memory_decrypted().
---
Changes since v1:
* Fixed format issues in commit log.
arch/x86/include/asm/pgtable.h | 4 ++++ arch/x86/mm/ioremap.c | 10 +++++++--- 2 files changed, 11 insertions(+), 3 deletions(-)
arch/x86/include/asm/pgtable.h +4 -0
@@ -21,6 +21,10 @@#define pgprot_encrypted (prot) __pgprot(__sme_set(pgprot_val(prot)))#define pgprot_decrypted (prot) __pgprot(__sme_clr(pgprot_val(prot))) +/* Make the page accesable by VMM for protected guests */ +#define pgprot_protected_guest (prot) __pgprot(pgprot_val(prot) | \ + tdg_shared_mask()) +#ifndef __ASSEMBLY__#include <asm/x86_init.h> #include <asm/fpu/xstate.h>
arch/x86/mm/ioremap.c +7 -3
@@ -17,6 +17,7 @@#include <linux/mem_encrypt.h> #include <linux/efi.h> #include <linux/pgtable.h> +#include <linux/protected_guest.h> #include <asm/set_memory.h> #include <asm/e820/api.h>
@@ -26,6 +27,7 @@#include <asm/pgalloc.h> #include <asm/memtype.h> #include <asm/setup.h> +#include <asm/tdx.h> #include "physaddr.h"
@@ -87,12 +89,12 @@ static unsigned int __ioremap_check_ram(struct resource *res) }/* -* In a SEV guest, NONE and RESERVED should not be mapped encrypted because -* there the whole memory is already encrypted. +* In a SEV or TDX guest, NONE and RESERVED should not be mapped encrypted (or +* private in TDX case) because there the whole memory is already encrypted. */ static unsigned int __ioremap_check_encrypted (struct resource *res) { -if (!sev_active ()) +if (!sev_active () && !prot_guest_has (PR_GUEST_MEM_ENCRYPT ))return 0 ;switch (res->desc) {
@@ -244,6 +246,8 @@ __ioremap_caller(resource_size_t phys_addr, unsigned long size, prot =PAGE_KERNEL_IO ;if ((io_desc.flags &IORES_MAP_ENCRYPTED ) || encrypted) prot =pgprot_encrypted (prot); +else if (prot_guest_has (PR_GUEST_SHARED_MAPPING_INIT )) + prot =pgprot_protected_guest (prot);switch (pcm) {case _PAGE_CACHE_MODE_UC:
Signature · 1 line
2.25.1
PATCH v2 4/7 x86/tdx: Add helper to do MapGPA hypercall
In reply to Kuppuswamy SathyanarayananFrom: "Kirill A. Shutemov" <[email protected]>
MapGPA hypercall is used by TDX guests to request VMM convert the existing mapping of given GPA address range between private/shared.
tdx_hcall_gpa_intent() is the wrapper used for making MapGPA hypercall.
---
Changes since v1:
* Modified tdx_hcall_gpa_intent() to use _tdx_hypercall() instead of tdx_hypercall().
arch/x86/include/asm/tdx.h | 18 ++++++++++++++++++ arch/x86/kernel/tdx.c | 25 +++++++++++++++++++++++++ 2 files changed, 43 insertions(+)
arch/x86/include/asm/tdx.h +18 -0
@@ -56,6 +56,15 @@ struct ve_info {u32 instr_info; }; +/* +* Page mapping type enum. This is software construct not +* part of any hardware or VMM ABI. +*/ +enum tdx_map_type { +TDX_MAP_PRIVATE , +TDX_MAP_SHARED , +}; +#ifdef CONFIG_INTEL_TDX_GUEST void __init tdx_early_init (void );
@@ -79,6 +88,9 @@ bool tdg_early_handle_ve(struct pt_regs *regs);extern phys_addr_t tdg_shared_mask (void ); +extern int tdx_hcall_gpa_intent (phys_addr_t gpa,int numpages, +enum tdx_map_type map_type); +/* * To support I/O port access in decompressor or early kernel init * code, since #VE exception handler cannot be used, use paravirt
@@ -149,6 +161,12 @@ static inline bool tdg_early_handle_ve(struct pt_regs *regs) { return false; }static inline phys_addr_t tdg_shared_mask (void ) {return 0 ; } +static inline int tdx_hcall_gpa_intent (phys_addr_t gpa,int numpages, +enum tdx_map_type map_type) +{ +return -ENODEV ; +} +#endif /* CONFIG_INTEL_TDX_GUEST */ #ifdef CONFIG_INTEL_TDX_GUEST_KVM
arch/x86/kernel/tdx.c +25 -0
@@ -18,6 +18,9 @@#define TDINFO 1#define TDGETVEINFO 3 +/* TDX hypercall Leaf IDs */ +#define TDVMCALL_MAP_GPA 0x10001 +#define VE_IS_IO_OUT (exit_qual) (((exit_qual) & 8) ? 0 : 1)#define VE_GET_IO_SIZE (exit_qual) (((exit_qual) & 7) + 1)#define VE_GET_PORT_NUM (exit_qual) ((exit_qual) >> 16)
@@ -100,6 +103,28 @@ static void tdg_get_info(void) physical_mask &= ~tdg_shared_mask (); } +/* +* Inform the VMM of the guest's intent for this physical page: +* shared with the VMM or private to the guest. The VMM is +* expected to change its mapping of the page in response. +* +* Note: shared->private conversions require further guest +* action to accept the page. +*/ +int tdx_hcall_gpa_intent (phys_addr_t gpa,int numpages, +enum tdx_map_type map_type) +{ +u64 ret; + +if (map_type ==TDX_MAP_SHARED ) + gpa |=tdg_shared_mask (); + + ret =_tdx_hypercall (TDVMCALL_MAP_GPA , gpa,PAGE_SIZE * numpages,0 ,0 , +NULL ); + +return ret ? -EIO :0 ; +} + static __cpuidle void tdg_halt(void) {u64 ret;
Signature · 1 line
2.25.1
PATCH v2 5/7 x86/tdx: Make DMA pages shared
In reply to Kuppuswamy SathyanarayananFrom: "Kirill A. Shutemov" <[email protected]>
Just like MKTME, TDX reassigns bits of the physical address for metadata. MKTME used several bits for an encryption KeyID. TDX uses a single bit in guests to communicate whether a physical page should be protected by TDX as private memory (bit set to 0) or unprotected and shared with the VMM (bit set to 1).
__set_memory_enc_dec() is now aware about TDX and sets Shared bit accordingly following with relevant TDX hypercall.
Also, Do TDACCEPTPAGE on every 4k page after mapping the GPA range when converting memory to private. Using 4k page size limit is due to current TDX spec restriction. Also, If the GPA (range) was already mapped as an active, private page, the host VMM may remove the private page from the TD by following the “Removing TD Private Pages” sequence in the Intel TDX-module specification [1] to safely block the mapping(s), flush the TLB and cache, and remove the mapping(s).
BUG() if TDACCEPTPAGE fails (except "previously accepted page" case) , as the guest is completely hosed if it can't access memory.
---
Changes since v1:
* Removed "we" or "I" usages in comment section.
* Replaced is_tdx_guest() checks with prot_guest_has() checks.
arch/x86/include/asm/pgtable.h | 1 + arch/x86/kernel/tdx.c | 33 +++++++++++++++++---- arch/x86/mm/mem_encrypt_common.c | 9 +++++- arch/x86/mm/pat/set_memory.c | 49 ++++++++++++++++++++++++++------ 4 files changed, 78 insertions(+), 14 deletions(-)
arch/x86/include/asm/pgtable.h +1 -0
@@ -24,6 +24,7 @@/* Make the page accesable by VMM for protected guests */ #define pgprot_protected_guest (prot) __pgprot(pgprot_val(prot) | \ tdg_shared_mask()) +#define pgprot_pg_shared_mask () __pgprot(tdg_shared_mask())#ifndef __ASSEMBLY__#include <asm/x86_init.h>
arch/x86/kernel/tdx.c +28 -5
@@ -17,10 +17,14 @@/* TDX Module call Leaf IDs */ #define TDINFO 1#define TDGETVEINFO 3 +#define TDACCEPTPAGE 6/* TDX hypercall Leaf IDs */ #define TDVMCALL_MAP_GPA 0x10001 +/* TDX Module call error codes */ +#define TDX_PAGE_ALREADY_ACCEPTED 0x8000000000000001 +#define VE_IS_IO_OUT (exit_qual) (((exit_qual) & 8) ? 0 : 1)#define VE_GET_IO_SIZE (exit_qual) (((exit_qual) & 7) + 1)#define VE_GET_PORT_NUM (exit_qual) ((exit_qual) >> 16)
@@ -103,26 +107,45 @@ static void tdg_get_info(void) physical_mask &= ~tdg_shared_mask (); } +static void tdg_accept_page (phys_addr_t gpa) +{ +u64 ret; + + ret =__tdx_module_call (TDACCEPTPAGE , gpa,0 ,0 ,0 ,NULL ); + +BUG_ON (ret && ret !=TDX_PAGE_ALREADY_ACCEPTED ); +} +/* * Inform the VMM of the guest's intent for this physical page: * shared with the VMM or private to the guest. The VMM is * expected to change its mapping of the page in response. -* -* Note: shared->private conversions require further guest -* action to accept the page. */ int tdx_hcall_gpa_intent (phys_addr_t gpa,int numpages,enum tdx_map_type map_type) { -u64 ret; +u64 ret =0 ; +int i;if (map_type ==TDX_MAP_SHARED ) gpa |=tdg_shared_mask (); ret =_tdx_hypercall (TDVMCALL_MAP_GPA , gpa,PAGE_SIZE * numpages,0 ,0 ,NULL ); +if (ret) + ret = -EIO ; -return ret ? -EIO :0 ; +if (ret || map_type ==TDX_MAP_SHARED ) +return ret; + +/* +* For shared->private conversion, accept the page using TDACCEPTPAGE +* TDX module call. +*/ +for (i =0 ; i < numpages; i++) +tdg_accept_page (gpa + i *PAGE_SIZE ); + +return 0 ; } static __cpuidle void tdg_halt(void)
arch/x86/mm/mem_encrypt_common.c +8 -1
@@ -9,9 +9,16 @@#include <asm/mem_encrypt_common.h> #include <linux/dma-mapping.h> +#include <linux/protected_guest.h> /* Override for DMA direct allocation check - ARCH_HAS_FORCE_DMA_UNENCRYPTED */ bool force_dma_unencrypted (struct device *dev) { -return amd_force_dma_unencrypted (dev); +if (sev_active () ||sme_active ()) +return amd_force_dma_unencrypted (dev); + +if (prot_guest_has (PR_GUEST_MEM_ENCRYPT )) +return true; + +return false; }
arch/x86/mm/pat/set_memory.c +41 -8
101 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH v2 6/7 x86/kvm: Use bounce buffers for TD guest
In reply to Kuppuswamy SathyanarayananFrom: "Kirill A. Shutemov" <[email protected]>
Intel TDX doesn't allow VMM to directly access guest private memory. Any memory that is required for communication with VMM must be shared explicitly. The same rule applies for any any DMA to and fromTDX guest. All DMA pages had to marked as shared pages. A generic way to achieve this without any changes to device drivers is to use the SWIOTLB framework.
This method of handling is similar to AMD SEV. So extend this support for TDX guest as well. Also since there are some common code between AMD SEV and TDX guest in mem_encrypt_init(), move it to mem_encrypt_common.c and call AMD specific init function from it
---
Changes since v1:
* Removed sme_me_mask check for amd_mem_encrypt_init() in mem_encrypt_init().
arch/x86/include/asm/mem_encrypt_common.h | 2 ++ arch/x86/kernel/tdx.c | 3 +++ arch/x86/mm/mem_encrypt.c | 5 +---- arch/x86/mm/mem_encrypt_common.c | 14 ++++++++++++++ 4 files changed, 20 insertions(+), 4 deletions(-)
arch/x86/include/asm/mem_encrypt_common.h +2 -0
@@ -8,11 +8,13 @@#ifdef CONFIG_AMD_MEM_ENCRYPT bool amd_force_dma_unencrypted (struct device *dev); +void __init amd_mem_encrypt_init (void );#else /* CONFIG_AMD_MEM_ENCRYPT */ static inline bool amd_force_dma_unencrypted (struct device *dev) {return false; } +static inline void amd_mem_encrypt_init (void ) {}#endif /* CONFIG_AMD_MEM_ENCRYPT */ #endif
arch/x86/kernel/tdx.c +3 -0
@@ -13,6 +13,7 @@#include <asm/insn.h> #include <asm/insn-eval.h> #include <linux/sched/signal.h> /* force_sig_fault() */ +#include <linux/swiotlb.h> /* TDX Module call Leaf IDs */ #define TDINFO 1
@@ -518,6 +519,8 @@ void __init tdx_early_init(void) legacy_pic = &null_legacy_pic; + swiotlb_force =SWIOTLB_FORCE ; +cpuhp_setup_state (CPUHP_AP_ONLINE_DYN ,"tdg:cpu_hotplug" ,NULL , tdg_cpu_offline_prepare);
arch/x86/mm/mem_encrypt.c +1 -4
@@ -466,14 +466,11 @@ static void print_mem_encrypt_feature_info(void) }/* Architecture __weak replacement functions */ -void __init mem_encrypt_init (void ) +void __init amd_mem_encrypt_init (void ) {if (!sme_me_mask)return ; -/* Call into SWIOTLB to update the SWIOTLB DMA buffers */ -swiotlb_update_mem_attributes (); -/* * With SEV, we need to unroll the rep string I/O instructions, * but SEV-ES supports them through the #VC handler.
arch/x86/mm/mem_encrypt_common.c +14 -0
@@ -10,6 +10,7 @@#include <asm/mem_encrypt_common.h> #include <linux/dma-mapping.h> #include <linux/protected_guest.h> +#include <linux/swiotlb.h> /* Override for DMA direct allocation check - ARCH_HAS_FORCE_DMA_UNENCRYPTED */ bool force_dma_unencrypted (struct device *dev)
@@ -22,3 +23,16 @@ bool force_dma_unencrypted(struct device *dev)return false; } + +/* Architecture __weak replacement functions */ +void __init mem_encrypt_init (void ) +{ +/* +* For TDX guest or SEV/SME, call into SWIOTLB to update +* the SWIOTLB DMA buffers +*/ +if (sme_me_mask ||prot_guest_has (PR_GUEST_MEM_ENCRYPT )) +swiotlb_update_mem_attributes (); + +amd_mem_encrypt_init (); +}
Signature · 1 line
2.25.1
PATCH v2 7/7 x86/tdx: ioapic: Add shared bit for IOAPIC base address
In reply to Kuppuswamy SathyanarayananFrom: Isaku Yamahata <[email protected]>
The kernel interacts with each bare-metal IOAPIC with a special MMIO page. When running under KVM, the guest's IOAPICs are emulated by KVM.
When running as a TDX guest, the guest needs to mark each IOAPIC mapping as "shared" with the host. This ensures that TDX private protections are not applied to the page, which allows the TDX host emulation to work.
Earlier patches in this series modified ioremap() so that ioremap()-created mappings such as virtio will be marked as shared. However, the IOAPIC code does not use ioremap() and instead uses the fixmap mechanism.
Introduce a special fixmap helper just for the IOAPIC code. Ensure that it marks IOAPIC pages as "shared". This replaces set_fixmap_nocache() with __set_fixmap() since __set_fixmap() allows custom 'prot' values.
--- arch/x86/kernel/apic/io_apic.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-)
arch/x86/kernel/apic/io_apic.c +16 -2
@@ -49,6 +49,7 @@#include <linux/slab.h> #include <linux/memblock.h> #include <linux/msi.h> +#include <linux/protected_guest.h> #include <asm/irqdomain.h> #include <asm/io.h>
@@ -65,6 +66,7 @@#include <asm/irq_remapping.h> #include <asm/hw_irq.h> #include <asm/apic.h> +#include <asm/tdx.h> #define for_each_ioapic (idx) \ for ((idx) = 0; (idx) < nr_ioapics; (idx)++)
@@ -2675,6 +2677,18 @@ static struct resource * __init ioapic_setup_resources(void)return res; } +static void io_apic_set_fixmap_nocache (enum fixed_addresses idx, +phys_addr_t phys) +{ +pgprot_t flags =FIXMAP_PAGE_NOCACHE ; + +/* Set TDX guest shared bit in pgprot flags */ +if (prot_guest_has (PR_GUEST_SHARED_MAPPING_INIT )) + flags =pgprot_protected_guest (flags); + +__set_fixmap (idx, phys, flags); +} +void __init io_apic_init_mappings (void ) {unsigned long ioapic_phys, idx =FIX_IO_APIC_BASE_0 ;
@@ -2707,7 +2721,7 @@ void __init io_apic_init_mappings(void) __func__, PAGE_SIZE, PAGE_SIZE); ioapic_phys =__pa (ioapic_phys); } -set_fixmap_nocache (idx, ioapic_phys); +io_apic_set_fixmap_nocache (idx, ioapic_phys);apic_printk (APIC_VERBOSE ,"mapped IOAPIC to %08lx (%08lx)\n" ,__fix_to_virt (idx) + (ioapic_phys & ~PAGE_MASK ), ioapic_phys);
@@ -2836,7 +2850,7 @@ int mp_register_ioapic(int id, u32 address, u32 gsi_base, ioapics[idx].mp_config.flags =MPC_APIC_USABLE ; ioapics[idx].mp_config.apicaddr = address; -set_fixmap_nocache (FIX_IO_APIC_BASE_0 + idx, address); +io_apic_set_fixmap_nocache (FIX_IO_APIC_BASE_0 + idx, address);if (bad_ioapic_register (idx)) {clear_fixmap (FIX_IO_APIC_BASE_0 + idx);return -ENODEV ;
Signature · 1 line
2.25.1