Add TDX Guest Support (boot fixes)
Subsystems (3)
v2 cover Add TDX Guest Support (boot fixes)
Hi All,
Intel's Trust Domain Extensions (TDX) protect guest VMs from malicious hosts and some physical attacks. This series adds boot code support and some additional fixes required for successful boot of TDX guest.
This series is the continuation of the patch series titled "Add TDX Guest Support (Initial support)" and "Add TDX Guest Support (#VE handler support )", which added initial support and #VE handler support 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
Also please note that this series alone is not necessarily fully functional.
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:
* Rebased on top of v3 version of "Add TDX Guest Support (Initial support)" patchset. Since it had some changes at the TDCALL implementation level, we have to rebase other dependent patches.
Kuppuswamy Sathyanarayanan (2):
x86/topology: Disable CPU online/offline control for TDX guest
x86: Skip WBINVD instruction for VM guest
Sean Christopherson (3):
x86/boot: Add a trampoline for APs booting in 64-bit mode x86/boot: Avoid #VE during boot for TDX platforms x86/tdx: Forcefully disable legacy PIC for TDX guests
arch/x86/boot/compressed/head_64.S | 16 +++++-- arch/x86/boot/compressed/pgtable.h | 2 +- arch/x86/include/asm/acenv.h | 7 ++- arch/x86/include/asm/realmode.h | 12 +++++ arch/x86/kernel/head_64.S | 20 +++++++- arch/x86/kernel/smpboot.c | 2 +- arch/x86/kernel/tdx.c | 18 ++++++++ arch/x86/kernel/topology.c | 3 +- arch/x86/realmode/rm/header.S | 1 + arch/x86/realmode/rm/trampoline_64.S | 59 ++++++++++++++++++++++-- arch/x86/realmode/rm/trampoline_common.S | 12 ++++- 11 files changed, 138 insertions(+), 14 deletions(-)
Signature · 1 line
2.25.1
PATCH v2 1/5 x86/boot: Add a trampoline for APs booting in 64-bit mode
From: Sean Christopherson <[email protected]>
Add a trampoline for booting APs in 64-bit mode via a software handoff with BIOS, and use the new trampoline for the ACPI MP wake protocol used by TDX. You can find MADT MP wake protocol details in ACPI specification r6.4, sec 5.2.12.19.
Extend the real mode IDT pointer by four bytes to support LIDT in 64-bit mode. For the GDT pointer, create a new entry as the existing storage for the pointer occupies the zero entry in the GDT itself.
--- arch/x86/include/asm/realmode.h | 12 ++++++++ arch/x86/kernel/smpboot.c | 2 +- arch/x86/realmode/rm/header.S | 1 + arch/x86/realmode/rm/trampoline_64.S | 38 ++++++++++++++++++++++++ arch/x86/realmode/rm/trampoline_common.S | 12 +++++++- 5 files changed, 63 insertions(+), 2 deletions(-)
arch/x86/include/asm/realmode.h +12 -0
@@ -12,6 +12,7 @@#ifndef __ASSEMBLY__#include <linux/types.h> +#include <linux/protected_guest.h> #include <asm/io.h> /* This must match data at realmode/rm/header.S */
@@ -25,6 +26,7 @@ struct real_mode_header {u32 sev_es_trampoline_start;#endif #ifdef CONFIG_X86_64 +u32 trampoline_start64;u32 trampoline_pgd;#endif /* ACPI S3 wakeup */
@@ -88,6 +90,16 @@ static inline void set_real_mode_mem(phys_addr_t mem) real_mode_header = (struct real_mode_header *) __va(mem); } +/* Common helper function to get start IP address */ +static inline unsigned long get_trampoline_start_ip (struct real_mode_header *rmh) +{ +#ifdef CONFIG_X86_64 +if (prot_guest_has (PR_GUEST_TDX )) +return rmh->trampoline_start64; +#endif +return rmh->trampoline_start; +} +void reserve_real_mode (void );#endif /* __ASSEMBLY__ */
arch/x86/kernel/smpboot.c +1 -1
@@ -1033,7 +1033,7 @@ static int do_boot_cpu(int apicid, int cpu, struct task_struct *idle, int *cpu0_nmi_registered) {/* start_ip had better be page-aligned! */ -unsigned long start_ip = real_mode_header->trampoline_start; +unsigned long start_ip =get_trampoline_start_ip (real_mode_header);unsigned long boot_error =0 ;unsigned long timeout;
arch/x86/realmode/rm/header.S +1 -0
@@ -24,6 +24,7 @@ SYM_DATA_START(real_mode_header) .long pa_sev_es_trampoline_start #endif #ifdef CONFIG_X86_64 + .long pa_trampoline_start64 .long pa_trampoline_pgd; #endif /* ACPI S3 wakeup */
arch/x86/realmode/rm/trampoline_64.S +38 -0
@@ -161,6 +161,19 @@ SYM_CODE_START(startup_32) ljmpl $__KERNEL_CS, $pa_startup_64 SYM_CODE_END(startup_32) +SYM_CODE_START(pa_trampoline_compat) + /* + * In compatibility mode. Prep ESP and DX for startup_32, then disable + * paging and complete the switch to legacy 32-bit mode. + */ + movl $rm_stack_end, %esp + movw $__KERNEL_DS, %dx + + movl $X86_CR0_PE, %eax + movl %eax, %cr0 + ljmpl $__KERNEL32_CS, $pa_startup_32 +SYM_CODE_END(pa_trampoline_compat) + .section ".text64","ax" .code64 .balign 4
@@ -169,6 +182,20 @@ SYM_CODE_START(startup_64) jmpq *tr_start(%rip) SYM_CODE_END(startup_64) +SYM_CODE_START(trampoline_start64) + /* + * APs start here on a direct transfer from 64-bit BIOS with identity + * mapped page tables. Load the kernel's GDT in order to gear down to + * 32-bit mode (to handle 4-level vs. 5-level paging), and to (re)load + * segment registers. Load the zero IDT so any fault triggers a + * shutdown instead of jumping back into BIOS. + */ + lidt tr_idt(%rip) + lgdt tr_gdt64(%rip) + + ljmpl *tr_compat(%rip) +SYM_CODE_END(trampoline_start64) + .section ".rodata","a" # Duplicate the global descriptor table # so the kernel can live anywhere
@@ -182,6 +209,17 @@ SYM_DATA_START(tr_gdt) .quad 0x00cf93000000ffff # __KERNEL_DS SYM_DATA_END_LABEL(tr_gdt, SYM_L_LOCAL, tr_gdt_end) +SYM_DATA_START(tr_gdt64) + .short tr_gdt_end - tr_gdt - 1 # gdt limit + .long pa_tr_gdt + .long 0 +SYM_DATA_END(tr_gdt64) + +SYM_DATA_START(tr_compat) + .long pa_trampoline_compat + .short __KERNEL32_CS +SYM_DATA_END(tr_compat) + .bss .balign PAGE_SIZE SYM_DATA(trampoline_pgd, .space PAGE_SIZE)
arch/x86/realmode/rm/trampoline_common.S +11 -1
@@ -1,4 +1,14 @@ /* SPDX-License-Identifier: GPL-2.0 */ .section ".rodata","a" .balign 16 -SYM_DATA_LOCAL(tr_idt, .fill 1, 6, 0) + +/* + * When a bootloader hands off to the kernel in 32-bit mode an + * IDT with a 2-byte limit and 4-byte base is needed. When a boot + * loader hands off to a kernel 64-bit mode the base address + * extends to 8-bytes. Reserve enough space for either scenario. + */ +SYM_DATA_START_LOCAL(tr_idt) + .short 0 + .quad 0 +SYM_DATA_END(tr_idt)
Signature · 1 line
2.25.1
PATCH v2 2/5 x86/boot: Avoid #VE during boot for TDX platforms
In reply to Kuppuswamy SathyanarayananFrom: Sean Christopherson <[email protected]>
There are a few MSRs and control register bits which the kernel normally needs to modify during boot. But, TDX disallows modification of these registers to help provide consistent security guarantees. Fortunately, TDX ensures that these are all in the correct state before the kernel loads, which means the kernel has no need to modify them.
The conditions to avoid are:
* Any writes to the EFER MSR
* Clearing CR0.NE
* Clearing CR3.MCE
This theoretically makes guest boot more fragile. If, for instance, EFER was set up incorrectly and a WRMSR was performed, it will trigger early exception panic or a triple fault, if it's before early exceptions are set up. However, this is likely to trip up the guest BIOS long before control reaches the kernel. In any case, these kinds of problems are unlikely to occur in production environments, and developers have good debug tools to fix them quickly.
--- arch/x86/boot/compressed/head_64.S | 16 ++++++++++++---- arch/x86/boot/compressed/pgtable.h | 2 +- arch/x86/kernel/head_64.S | 20 ++++++++++++++++++-- arch/x86/realmode/rm/trampoline_64.S | 23 +++++++++++++++++++---- 4 files changed, 50 insertions(+), 11 deletions(-)
arch/x86/boot/compressed/head_64.S +12 -4
@@ -640,12 +640,20 @@ SYM_CODE_START(trampoline_32bit_src) movl $MSR_EFER, %ecx rdmsr btsl $_EFER_LME, %eax + /* Avoid writing EFER if no change was made (for TDX guest) */ + jc 1f wrmsr - popl %edx +1: popl %edx popl %ecx /* Enable PAE and LA57 (if required) paging modes */ - movl $X86_CR4_PAE, %eax + movl %cr4, %eax + /* + * Clear all bits except CR4.MCE, which is preserved. + * Clearing CR4.MCE will #VE in TDX guests. + */ + andl $X86_CR4_MCE, %eax + orl $X86_CR4_PAE, %eax testl %edx, %edx jz 1f orl $X86_CR4_LA57, %eax
@@ -659,8 +667,8 @@ SYM_CODE_START(trampoline_32bit_src) pushl $__KERNEL_CS pushl %eax - /* Enable paging again */ - movl $(X86_CR0_PG | X86_CR0_PE), %eax + /* Enable paging again. Avoid clearing X86_CR0_NE for TDX */ + movl $(X86_CR0_PG | X86_CR0_NE | X86_CR0_PE), %eax movl %eax, %cr0 lret
arch/x86/boot/compressed/pgtable.h +1 -1
@@ -6,7 +6,7 @@#define TRAMPOLINE_32BIT_PGTABLE_OFFSET 0#define TRAMPOLINE_32BIT_CODE_OFFSET PAGE_SIZE -#define TRAMPOLINE_32BIT_CODE_SIZE 0x70 +#define TRAMPOLINE_32BIT_CODE_SIZE 0x80#define TRAMPOLINE_32BIT_STACK_END TRAMPOLINE_32BIT_SIZE
arch/x86/kernel/head_64.S +18 -2
@@ -141,7 +141,13 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) 1: /* Enable PAE mode, PGE and LA57 */ - movl $(X86_CR4_PAE | X86_CR4_PGE), %ecx + movq %cr4, %rcx + /* + * Clear all bits except CR4.MCE, which is preserved. + * Clearing CR4.MCE will #VE in TDX guests. + */ + andl $X86_CR4_MCE, %ecx + orl $(X86_CR4_PAE | X86_CR4_PGE), %ecx #ifdef CONFIG_X86_5LEVEL testl $1, __pgtable_l5_enabled(%rip) jz 1f
@@ -229,13 +235,23 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) /* Setup EFER (Extended Feature Enable Register) */ movl $MSR_EFER, %ecx rdmsr + /* + * Preserve current value of EFER for comparison and to skip + * EFER writes if no change was made (for TDX guest) + */ + movl %eax, %edx btsl $_EFER_SCE, %eax /* Enable System Call */ btl $20,%edi /* No Execute supported? */ jnc 1f btsl $_EFER_NX, %eax btsq $_PAGE_BIT_NX,early_pmd_flags(%rip) -1: wrmsr /* Make changes effective */ + /* Avoid writing EFER if no change was made (for TDX guest) */ +1: cmpl %edx, %eax + je 1f + xor %edx, %edx + wrmsr /* Make changes effective */ +1: /* Setup cr0 */ movl $CR0_STATE, %eax /* Make changes effective */
arch/x86/realmode/rm/trampoline_64.S +19 -4
@@ -143,13 +143,27 @@ SYM_CODE_START(startup_32) movl %eax, %cr3 # Set up EFER + movl $MSR_EFER, %ecx + rdmsr + /* + * Skip writing to EFER if the register already has desired + * value (to avoid #VE for the TDX guest). + */ + cmp pa_tr_efer, %eax + jne .Lwrite_efer + cmp pa_tr_efer + 4, %edx + je .Ldone_efer +.Lwrite_efer: movl pa_tr_efer, %eax movl pa_tr_efer + 4, %edx - movl $MSR_EFER, %ecx wrmsr - # Enable paging and in turn activate Long Mode - movl $(X86_CR0_PG | X86_CR0_WP | X86_CR0_PE), %eax +.Ldone_efer: + /* + * Enable paging and in turn activate Long Mode. Avoid clearing + * X86_CR0_NE for TDX. + */ + movl $(X86_CR0_PG | X86_CR0_WP | X86_CR0_NE | X86_CR0_PE), %eax movl %eax, %cr0 /*
@@ -169,7 +183,8 @@ SYM_CODE_START(pa_trampoline_compat) movl $rm_stack_end, %esp movw $__KERNEL_DS, %dx - movl $X86_CR0_PE, %eax + /* Avoid clearing X86_CR0_NE for TDX */ + movl $(X86_CR0_NE | X86_CR0_PE), %eax movl %eax, %cr0 ljmpl $__KERNEL32_CS, $pa_startup_32 SYM_CODE_END(pa_trampoline_compat)
Signature · 1 line
2.25.1
PATCH v2 3/5 x86/topology: Disable CPU online/offline control for TDX guest
In reply to Kuppuswamy SathyanarayananAs per Intel TDX Virtual Firmware Design Guide, sec 4.3.5 and sec 9.4, all unused CPUs are put in spinning state by TDVF until OS requests for CPU bring-up via mailbox address passed by ACPI MADT table. Since by default all unused CPUs are always in spinning state, there is no point in supporting dynamic CPU online/offline feature. So current generation of TDVF does not support CPU hotplug feature. It may be supported in next generation.
---
Changes since v1:
* Replaced is_tdx_guest() check with prot_guest_has(PR_GUEST_TDX).
arch/x86/kernel/tdx.c | 15 +++++++++++++++ arch/x86/kernel/topology.c | 3 ++- 2 files changed, 17 insertions(+), 1 deletion(-)
arch/x86/kernel/tdx.c +15 -0
@@ -5,6 +5,7 @@#define pr_fmt (fmt) "x86/tdx: " fmt#include <linux/protected_guest.h> +#include <linux/cpuhotplug.h> #include <asm/tdx.h> #include <asm/vmx.h>
@@ -306,6 +307,17 @@ static int tdg_handle_mmio(struct pt_regs *regs, struct ve_info *ve)return insn.length; } +static int tdg_cpu_offline_prepare (unsigned int cpu) +{ +/* +* Per Intel TDX Virtual Firmware Design Guide, +* sec 4.3.5 and sec 9.4, Hotplug is not supported +* in TDX platforms. So don't support CPU +* offline feature once it is turned on. +*/ +return -EOPNOTSUPP ; +} +unsigned long tdg_get_ve_info (struct ve_info *ve) {u64 ret;
@@ -446,5 +458,8 @@ void __init tdx_early_init(void) pv_ops.irq.safe_halt = tdg_safe_halt; pv_ops.irq.halt = tdg_halt; +cpuhp_setup_state (CPUHP_AP_ONLINE_DYN ,"tdg:cpu_hotplug" , +NULL , tdg_cpu_offline_prepare); +pr_info ("Guest initialized\n" ); }
arch/x86/kernel/topology.c +2 -1
@@ -32,6 +32,7 @@#include <linux/init.h> #include <linux/smp.h> #include <linux/irq.h> +#include <linux/protected_guest.h> #include <asm/io_apic.h> #include <asm/cpu.h>
@@ -130,7 +131,7 @@ int arch_register_cpu(int num) } } } -if (num || cpu0_hotpluggable) +if ((num || cpu0_hotpluggable) && !prot_guest_has (PR_GUEST_TDX ))per_cpu (cpu_devices, num).cpu.hotpluggable =1 ;return register_cpu (&per_cpu (cpu_devices, num).cpu, num);
Signature · 1 line
2.25.1
PATCH v2 4/5 x86/tdx: Forcefully disable legacy PIC for TDX guests
In reply to Kuppuswamy SathyanarayananFrom: Sean Christopherson <[email protected]>
Disable the legacy PIC (8259) for TDX guests as the PIC cannot be supported by the VMM. TDX Module does not allow direct IRQ injection, and using posted interrupt style delivery requires the guest to EOI the IRQ, which diverges from the legacy PIC behavior.
--- arch/x86/kernel/tdx.c | 3 +++ 1 file changed, 3 insertions(+)
arch/x86/kernel/tdx.c +3 -0
@@ -8,6 +8,7 @@#include <linux/cpuhotplug.h> #include <asm/tdx.h> +#include <asm/i8259.h> #include <asm/vmx.h> #include <asm/insn.h> #include <asm/insn-eval.h>
@@ -458,6 +459,8 @@ void __init tdx_early_init(void) pv_ops.irq.safe_halt = tdg_safe_halt; pv_ops.irq.halt = tdg_halt; + legacy_pic = &null_legacy_pic; +cpuhp_setup_state (CPUHP_AP_ONLINE_DYN ,"tdg:cpu_hotplug" ,NULL , tdg_cpu_offline_prepare);
Signature · 1 line
2.25.1
PATCH v2 5/5 x86: Skip WBINVD instruction for VM guest
In reply to Kuppuswamy SathyanarayananVM guests that supports ACPI, use standard ACPI mechanisms to signal sleep state entry (including reboot) to the host. The ACPI specification mandates WBINVD on any sleep state entry with the expectation that the platform is only responsible for maintaining the state of memory over sleep states, not preserving dirty data in any CPU caches. ACPI cache flushing requirements pre-date the advent of virtualization. Given guest sleep state entry does not affect any host power rails it is not required to flush caches. The host is responsible for maintaining cache state over its own bare metal sleep state transitions that power-off the cache. A TDX guest, unlike a typical guest, will machine check if the CPU cache is powered off.
--- arch/x86/include/asm/acenv.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
arch/x86/include/asm/acenv.h +6 -1
@@ -10,10 +10,15 @@#define _ASM_X86_ACENV_H#include <asm/special_insns.h> +#include <asm/cpu.h> /* Asm macros */ -#define ACPI_FLUSH_CPU_CACHE () wbinvd() +#define ACPI_FLUSH_CPU_CACHE () \ +do { \ + if (!boot_cpu_has(X86_FEATURE_HYPERVISOR)) \ + wbinvd(); \ +} while (0)int __acpi_acquire_global_lock (unsigned int *lock);int __acpi_release_global_lock (unsigned int *lock);
Signature · 1 line
2.25.1