X86: TDX framework support
Subsystems (3)
v2 cover X86: TDX framework support
* What's TDX?
TDX stands for Trust Domain Extensions which isolates VMs from the virtual machine manager (VMM)/hypervisor and any other software on the platform.
To support TDX, multiple software components, not only KVM but also QEMU, guest kernel, and virtual bios, need to be updated. For more details, please check link[1], there are TDX spec and public repository links at github for each software component.
* What do we add?
This patch set adds a basic framework to support running existing and future test cases in TDX-protected environments to verify the function of the *TDX 1.5* software stack. Appreciate any comments and suggestions.
This framework depends on UEFI support.
The supported test cases are marked in a "tdx" test group. Most of the unsupported test cases are due to testing features not supported by TDX, a few are due to their special design being unsuitable for running in UEFI.
This series is also available on github:
https://github.com/intel/kvm-unit-tests-tdx/tree/tdx
To run a test case in TDX:
EFI_TDX=y [EFI_UEFI=/path/to/TDVF.fd] [QEMU=/path/to/qemu-tdx]
./x86/efi/run x86/msr.efi
To run all the tdx-supported test cases:
EFI_TDX=y [EFI_UEFI=/path/to/TDVF.fd] [QEMU=/path/to/qemu-tdx]./run_tests.sh -g tdx
[EFI_UEFI=/path/to/TDVF.fd] [QEMU=/path/to/qemu-tdx] customization can be removed after released packages of OVMF and qemu have TDX support. The current OVMF upstream code has TDX support, but its package doesn't have full TDX features.
* Patch organization
patch 1-8: add initial support for TDX, some simple test cases could run
with them.
patch 9: TDVF supports accepting part of the whole memory and this patch
adds support for accepting remaining memory.
patch10-12: add multiprocessor support.
patch13-14: enable the lvl5 page table as TDVF uses it.
patch15-16: bypass and modify unsupported sub-test to be compatible with
TDX.
patch 17: TDX-specific test case, may add more sub-tests in the future.
patch 18: enable all the TDX-supported test cases to run in a batch with
run_tests.sh
TODO:
1. add more TDX specific sub-test
2. add mmio simulation in #VE handler
[1] "KVM TDX basic feature support"
https://lwn.net/ml/linux-kernel/[email protected]/
---
Changes RFC v1 -> RFC v2:
- rebase to the latest kvm-unit-tests repo.
- modify the TDCALL helper using one micro TDX_MODULE_CALL as the TD guest kernel does. And split patch1 of RFC v1 into two patches: guest code porting and TDX framework setup.
- using printf instead of tdx_printf, as TDVF provides default #VE handler before unit test setup. (patch 2)
- change the return of each handler in #VE. (patch 3)
- change implementation of private memory acceptance, i.e., tdx_accept_memory_regions. (patch 9)
- move the content of lib/x86/acpi.c to lib/acpi.c. (patch 10)
- refine AP bring-up process and integrate TDX MP to existing UEFI MP.
(patch 11)
- drop patch 16 of RFC v1 "x86 UEFI: Add support for parameter passing" as code base already has support.
- add checks for the fixed value of virtualized CPUID. (patch 17)
- some order adjustments and fixes.
RFC v1:
https://lore.kernel.org/all/[email protected]/
Zhenzhong Duan (18):
x86 TDX: Port tdx basic functions from TDX guest code x86 TDX: Add support functions for TDX framework x86 TDX: Add #VE handler
x86 TDX: Bypass APIC and enable x2APIC directly x86 TDX: Add exception table support
x86 TDX: Bypass wrmsr simulation on some specific MSRs x86 TDX: Simulate single step on #VE handled instruction x86 TDX: Extend EFI run script to support TDX x86 TDX: Add support for memory accept
acpi: Add MADT table parse code
x86 TDX: Add multi processor support
x86 TDX: Add a formal IPI handler
x86 TDX: Enable lvl5 boot page table
x86 TDX: Add lvl5 page table support to virtual memory x86 TDX: bypass unsupported syscall TF for TDX x86 TDX: Modify the MSR test to be compatible with TDX x86 TDX: Add TDX specific test case
x86 TDX: Make run_tests.sh work with TDX
README.md | 6 + lib/acpi.c | 160 +++++++++++ lib/acpi.h | 59 +++- lib/asm-generic/page.h | 7 +- lib/linux/efi.h | 23 +- lib/x86/apic.c | 4 + lib/x86/asm/page.h | 19 ++ lib/x86/asm/setup.h | 1 + lib/x86/desc.c | 18 +- lib/x86/desc.h | 11 + lib/x86/setup.c | 67 ++++- lib/x86/smp.c | 44 ++- lib/x86/smp.h | 2 + lib/x86/tdcall.S | 66 +++++ lib/x86/tdx.c | 637 +++++++++++++++++++++++++++++++++++++++++ lib/x86/tdx.h | 167 +++++++++++ lib/x86/tdxcall.S | 249 ++++++++++++++++ lib/x86/vm.c | 15 +- x86/Makefile.common | 3 + x86/Makefile.x86_64 | 1 + x86/efi/README.md | 6 + x86/efi/efistart64.S | 51 ++++ x86/efi/run | 19 ++ x86/intel_tdx.c | 326 +++++++++++++++++++++ x86/msr.c | 46 ++- x86/syscall.c | 3 +- x86/unittests.cfg | 21 +- 27 files changed, 1984 insertions(+), 47 deletions(-) create mode 100644 lib/x86/tdcall.S create mode 100644 lib/x86/tdx.c create mode 100644 lib/x86/tdx.h create mode 100644 lib/x86/tdxcall.S create mode 100644 x86/intel_tdx.c
base-commit: 6b31aa76a038bb56b144825f55301b2ab64c02e9
Signature · 1 line
2.25.1
RFC v2 1/18 x86 TDX: Port tdx basic functions from TDX guest code
From: Zhenzhong Duan <[email protected]>
Port tdxcall.S, tdcall.S and tdx.c from TDX guest kernel source code, simplify and keep only code which is useful for TDX kvm-unit-test framework.
lib/x86/tdxcall.S contains one common helper macro for both TDCALL and SEAMCALL instructions: TDX_MODULE_CALL.
Although the SEAMCALL path is not used in this series, the macro is not modified for simplicity.
lib/x86/tdcall.S contains three helper functions for TDCALL.
- __tdcall, TDX guests to request services from the TDX module.
- __tdcall_ret, TDX guests to request services from the TDX module (does not include VMM services).
- __tdcall_saved_ret, TDX guests to request services from the TDX module (including VMM services).
The __tdx_hypercall is wrapper of __tdcall_saved_ret, used to request services from the VMM.
lib/x86/tdx.c contains wrapper functions for simulating various instructions through tdvmcall. Currently below instructions are simulated:
IO read/write
MSR read/write
cpuid
hlt
--- lib/x86/tdcall.S | 66 +++++++++++ lib/x86/tdx.c | 278 ++++++++++++++++++++++++++++++++++++++++++++ lib/x86/tdx.h | 141 ++++++++++++++++++++++ lib/x86/tdxcall.S | 249 +++++++++++++++++++++++++++++++++++++++ x86/Makefile.common | 3 + 5 files changed, 737 insertions(+) create mode 100644 lib/x86/tdcall.S create mode 100644 lib/x86/tdx.c create mode 100644 lib/x86/tdx.h create mode 100644 lib/x86/tdxcall.S
lib/x86/tdcall.S new file +66 -0
@@ -0,0 +1,66 @@ +/* + * Low level helpers for tdcall + * + * Copyright (c) 2023, Intel Inc + * + * Authors: + * Zhenzhong Duan <[email protected]> + * Qian Wen <[email protected]> + * + * SPDX-License-Identifier: GPL-2.0 + */ +#include <errno.h> +#include "tdxcall.S" + +/* + * __tdcall() - Used by TDX guests to request services from the TDX + * module (does not include VMM services) using TDCALL instruction. + * + * __tdcall() function ABI: + * + * @fn (RDI) - TDCALL Leaf ID, moved to RAX + * @args (RSI) - struct tdx_module_args for input + * + * Only RCX/RDX/R8-R11 are used as input registers. + * + * Return status of TDCALL via RAX. + */ +.global __tdcall +__tdcall: + TDX_MODULE_CALL host=0 + +/* + * __tdcall_ret() - Used by TDX guests to request services from the TDX + * module (does not include VMM services) using TDCALL instruction, with + * saving output registers to the 'struct tdx_module_args' used as input. + * + * __tdcall_ret() function ABI: + * + * @fn (RDI) - TDCALL Leaf ID, moved to RAX + * @args (RSI) - struct tdx_module_args for input and output + * + * Only RCX/RDX/R8-R11 are used as input/output registers. + * + * Return status of TDCALL via RAX. + */ +.global __tdcall_ret +__tdcall_ret: + TDX_MODULE_CALL host=0 ret=1 + +/* + * __tdcall_saved_ret() - Used by TDX guests to request services from the + * TDX module (including VMM services) using TDCALL instruction, with + * saving output registers to the 'struct tdx_module_args' used as input. + * + * __tdcall_saved_ret() function ABI: + * + * @fn (RDI) - TDCALL leaf ID, moved to RAX + * @args (RSI) - struct tdx_module_args for input/output + * + * All registers in @args are used as input/output registers. + * + * On successful completion, return the hypercall error code. + */ +.global __tdcall_saved_ret +__tdcall_saved_ret: + TDX_MODULE_CALL host=0 ret=1 saved=1
lib/x86/tdx.c new file +278 -0
278 lines not on this page · raw message
lib/x86/tdx.h new file +141 -0
141 lines not on this page · raw message
lib/x86/tdxcall.S new file +249 -0
249 lines not on this page · raw message
x86/Makefile.common +3 -0
@@ -27,6 +27,9 @@ ifeq ($(CONFIG_EFI),y) cflatobjs += lib/x86/amd_sev.o cflatobjs += lib/efi.o cflatobjs += x86/efi/reloc_x86_64.o +cflatobjs += lib/x86/tdxcall.o +cflatobjs += lib/x86/tdcall.o +cflatobjs += lib/x86/tdx.o endif OBJDIRS += lib/x86
Signature · 1 line
2.25.1
RFC v2 2/18 x86 TDX: Add support functions for TDX framework
In reply to Qian WenFrom: Zhenzhong Duan <[email protected]>
Detect TDX during at start of efi setup. And define a dummy is_tdx_guest() if CONFIG_EFI is undefined as this function will be used globally in the future.
In addition, it is fine to use the print function even before the #VE handler of the unit test has complete configuration.
TDVF provides the default #VE exception handler, which will convert some of the forbidden instructions to TDCALL [TDG.VP.VMCALL] <INSTRUCTION>, e.g., IO => TDCALL [TDG.VP.VMCALL] <Instruction.IO> (see “10 Exception Handling” in TDVF spec [1]).
[1] TDVF spec: https://cdrdv2.intel.com/v1/dl/getContent/733585
--- lib/x86/asm/setup.h | 1 + lib/x86/setup.c | 6 ++++++ lib/x86/tdx.c | 39 +++++++++++++++++++++++++++++++++++++++ lib/x86/tdx.h | 9 +++++++++ 4 files changed, 55 insertions(+)
lib/x86/asm/setup.h +1 -0
@@ -15,6 +15,7 @@ unsigned long setup_tss(u8 *stacktop);efi_status_t setup_efi (efi_bootinfo_t *efi_bootinfo);void setup_5level_page_table (void );#endif /* CONFIG_EFI */ +#include "x86/tdx.h" void save_id (void );void bsp_rest_init (void );
lib/x86/setup.c +6 -0
@@ -308,6 +308,12 @@ efi_status_t setup_efi(efi_bootinfo_t *efi_bootinfo)efi_status_t status;const char *phase; + status =setup_tdx (); +if (status !=EFI_SUCCESS && status !=EFI_UNSUPPORTED ) { +printf ("INTEL TDX setup failed, error = 0x%lx\n" , status); +return status; + } + status =setup_memory_allocator (efi_bootinfo);if (status !=EFI_SUCCESS ) {printf ("Failed to set up memory allocator: " );
lib/x86/tdx.c +39 -0
@@ -276,3 +276,42 @@ static int handle_io(struct ex_regs *regs, struct ve_info *ve)return ve_instr_len (ve); } + +bool is_tdx_guest (void ) +{ +static int tdx_guest =-1 ; +struct cpuid c; +u32 sig[3 ]; + +if (tdx_guest >=0 ) + goto done; + +if (cpuid (0 ).a <TDX_CPUID_LEAF_ID ) { + tdx_guest =0 ; + goto done; + } + + c =cpuid (TDX_CPUID_LEAF_ID ); + sig[0 ] = c.b; + sig[1 ] = c.d; + sig[2 ] = c.c; + + tdx_guest = !memcmp (TDX_IDENT , sig,sizeof (sig)); + +done: +return !!tdx_guest; +} + +efi_status_t setup_tdx (void ) +{ +if (!is_tdx_guest ()) +return EFI_UNSUPPORTED ; + +/* The printf can work here. Since TDVF default exception handler +* can handle the #VE caused by IO read/write during printf() before +* finalizing configuration of the unit test's #VE handler. +*/ +printf ("Initialized TDX.\n" ); + +return EFI_SUCCESS ; +}
lib/x86/tdx.h +9 -0
@@ -21,6 +21,9 @@#define TDX_HYPERCALL_STANDARD 0 +#define TDX_CPUID_LEAF_ID 0x21 +#define TDX_IDENT "IntelTDX " +/* TDX module Call Leaf IDs */ #define TDG_VP_VMCALL 0
@@ -136,6 +139,12 @@ struct ve_info {u32 instr_info; }; +bool is_tdx_guest (void ); +efi_status_t setup_tdx (void ); + +#else +inline bool is_tdx_guest (void ) {return false; } +#endif /* CONFIG_EFI */ #endif /* _ASM_X86_TDX_H */
Signature · 1 line
2.25.1
Re: [kvm-unit-tests RFC v2 02/18] x86 TDX: Add support functions for TDX framework
Quoted 87 lines · Qian Wen
From: Zhenzhong Duan <[email protected]>
Detect TDX during at start of efi setup. And define a dummy
is_tdx_guest()ifCONFIG_EFIis undefined as this function will be used globally in the future.In addition, it is fine to use the print function even before the #VE handler of the unit test has complete configuration.
TDVF provides the default #VE exception handler, which will convert some of the forbidden instructions to TDCALL [TDG.VP.VMCALL] <INSTRUCTION>, e.g., IO => TDCALL [TDG.VP.VMCALL] <Instruction.IO> (see “10 Exception Handling” in TDVF spec [1]).
[1] TDVF spec: https://cdrdv2.intel.com/v1/dl/getContent/733585
Signed-off-by Zhenzhong Duan <[email protected]>Reviewed-by Yu Zhang <[email protected]>Co-developed-by Qian Wen <[email protected]>Signed-off-by Qian Wen <[email protected]>--- lib/x86/asm/setup.h | 1 + lib/x86/setup.c | 6 ++++++ lib/x86/tdx.c | 39 +++++++++++++++++++++++++++++++++++++++ lib/x86/tdx.h | 9 +++++++++ 4 files changed, 55 insertions(+)lib/x86/asm/setup.h +1
-0@@ -15,6 +15,7 @@ unsigned long setup_tss(u8 *stacktop);efi_status_t setup_efi (efi_bootinfo_t *efi_bootinfo);void setup_5level_page_table (void );#endif /* CONFIG_EFI */ +#include "x86/tdx.h" void save_id (void );void bsp_rest_init (void );lib/x86/setup.c +6
-0@@ -308,6 +308,12 @@ efi_status_t setup_efi(efi_bootinfo_t *efi_bootinfo)efi_status_t status;const char *phase; + status =setup_tdx (); +if (status !=EFI_SUCCESS && status !=EFI_UNSUPPORTED ) { +printf ("INTEL TDX setup failed, error = 0x%lx\n" , status); +return status; + } + status =setup_memory_allocator (efi_bootinfo);if (status !=EFI_SUCCESS ) {printf ("Failed to set up memory allocator: " );lib/x86/tdx.c +22
-0@@ -276,3 +276,42 @@ static int handle_io(struct ex_regs *regs, struct ve_info *ve)return ve_instr_len (ve); } + +bool is_tdx_guest (void ) +{ +static int tdx_guest =-1 ; +struct cpuid c; +u32 sig[3 ]; + +if (tdx_guest >=0 ) + goto done; + +if (cpuid (0 ).a <TDX_CPUID_LEAF_ID ) { + tdx_guest =0 ; + goto done; + } + + c =cpuid (TDX_CPUID_LEAF_ID ); + sig[0 ] = c.b; + sig[1 ] = c.d; + sig[2 ] = c.c; + + tdx_guest = !memcmp (TDX_IDENT , sig,sizeof (sig)); +
lib/x86/tdx.c static int handle_io(struct ex_regs *regs, struct ve_info *ve) · line 310
Quoted 10 lines
lib/x86/tdx.c +10
-0+done: +return !!tdx_guest; +} + +efi_status_t setup_tdx (void ) +{ +if (!is_tdx_guest ()) +return EFI_UNSUPPORTED ; + +/* The printf can work here. Since TDVF default exception handler
Comments need start at another new line with leading asterisk.
Quoted 34 lines
lib/x86/tdx.c +7
-0+* can handle the #VE caused by IO read/write during printf() before +* finalizing configuration of the unit test's #VE handler. +*/ +printf ("Initialized TDX.\n" ); + +return EFI_SUCCESS ; +}lib/x86/tdx.h +9
-0@@ -21,6 +21,9 @@#define TDX_HYPERCALL_STANDARD 0 +#define TDX_CPUID_LEAF_ID 0x21 +#define TDX_IDENT "IntelTDX " +/* TDX module Call Leaf IDs */ #define TDG_VP_VMCALL 0@@ -136,6 +139,12 @@ struct ve_info {u32 instr_info; }; +bool is_tdx_guest (void ); +efi_status_t setup_tdx (void ); + +#else +inline bool is_tdx_guest (void ) {return false; } +#endif /* CONFIG_EFI */ #endif /* _ASM_X86_TDX_H */
Re: [kvm-unit-tests RFC v2 02/18] x86 TDX: Add support functions for TDX framework
Quoted 89 lines · Zeng Guang
Quoted 87 lines · Qian Wen
From: Zhenzhong Duan <[email protected]>
Detect TDX during at start of efi setup. And define a dummy
is_tdx_guest()ifCONFIG_EFIis undefined as this function will be used globally in the future.In addition, it is fine to use the print function even before the #VE handler of the unit test has complete configuration.
TDVF provides the default #VE exception handler, which will convert some of the forbidden instructions to TDCALL [TDG.VP.VMCALL] <INSTRUCTION>, e.g., IO => TDCALL [TDG.VP.VMCALL] <Instruction.IO> (see “10 Exception Handling” in TDVF spec [1]).
[1] TDVF spec: https://cdrdv2.intel.com/v1/dl/getContent/733585
Signed-off-by Zhenzhong Duan <[email protected]>Reviewed-by Yu Zhang <[email protected]>Co-developed-by Qian Wen <[email protected]>Signed-off-by Qian Wen <[email protected]>---
lib/x86/asm/setup.h| 1 +
lib/x86/setup.c | 6 ++++++
lib/x86/tdx.c | 39 +++++++++++++++++++++++++++++++++++++++ lib/x86/tdx.h | 9 +++++++++4 files changed, 55 insertions(+)lib/x86/asm/setup.h +1
-0@@ -15,6 +15,7 @@ unsigned long setup_tss(u8 *stacktop); efi_status_t setup_efi(efi_bootinfo_t *efi_bootinfo); void setup_5level_page_table(void); #endif /* CONFIG_EFI */ +#include "x86/tdx.h" void save_id(void); void bsp_rest_init(void);lib/x86/setup.c +5
-0@@ -308,6 +308,12 @@ efi_status_t setup_efi(efi_bootinfo_t *efi_bootinfo) efi_status_t status; const char *phase; + status = setup_tdx(); + if (status != EFI_SUCCESS && status != EFI_UNSUPPORTED) { + printf("INTEL TDX setup failed, error = 0x%lx\n", status); + return status; + } + status = setup_memory_allocator(efi_bootinfo); if (status != EFI_SUCCESS) { printf("Failed to set up memory allocator: ");lib/x86/tdx.c +25
-0@@ -276,3 +276,42 @@ static int handle_io(struct ex_regs *regs, struct ve_info *ve) return ve_instr_len(ve); } + +bool is_tdx_guest (void ) +{ + static int tdx_guest = -1; + struct cpuid c; + u32 sig[3]; + + if (tdx_guest >= 0) + goto done; + + if (cpuid(0).a < TDX_CPUID_LEAF_ID) { + tdx_guest = 0; + goto done; + } + + c = cpuid(TDX_CPUID_LEAF_ID); + sig[0] = c.b; + sig[1] = c.d; + sig[2] = c.c; + + tdx_guest = !memcmp(TDX_IDENT, sig, sizeof(sig)); + +done: + return !!tdx_guest; +}
Quoted 8 lines
lib/x86/tdx.c static int handle_io(struct ex_regs *regs, struct ve_info *ve) · line 310
Quoted 7 lines
lib/x86/tdx.c +7
-0+ +efi_status_t setup_tdx (void ) +{ + if (!is_tdx_guest()) + return EFI_UNSUPPORTED; + + /* The printf can work here. Since TDVF default exception handlerComments need start at another new line with leading asterisk.
Ooh, good catch, thanks!
Thanks,
Qian
Quoted 30 lines
Quoted 29 lines
lib/x86/tdx.c +7
-0+* can handle the #VE caused by IO read/write during printf() before +* finalizing configuration of the unit test's #VE handler. +*/ + printf("Initialized TDX.\n"); + + return EFI_SUCCESS; +}lib/x86/tdx.h +0
-0@@ -21,6 +21,9 @@ #define TDX_HYPERCALL_STANDARD 0+#define TDX_CPUID_LEAF_ID
0x21lib/x86/tdx.h +2
-0+#define TDX_IDENT "IntelTDX " +/* TDX module Call Leaf IDs */
#define TDG_VP_VMCALL 0
@@ -136,6 +139,12 @@ structve_info{
u32instr_info;
};
+boolis_tdx_guest(void);lib/x86/tdx.h +5
-0+efi_status_t setup_tdx (void ); + +#else +inline bool is_tdx_guest (void ) {return false; } +#endif /*
CONFIG_EFI*/
#endif /*_ASM_X86_TDX_H*/
RFC v2 3/18 x86 TDX: Add #VE handler
In reply to Qian WenFrom: Zhenzhong Duan <[email protected]>
Some instructions execution trigger #VE and are simulated in #VE handler.
Add such a handler, currently support simulation of IO and MSR read/write, cpuid and hlt instructions.
--- lib/x86/desc.c | 6 +++- lib/x86/tdx.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++++- lib/x86/tdx.h | 1 + 3 files changed, 85 insertions(+), 2 deletions(-)
lib/x86/desc.c +5 -1
@@ -249,6 +249,7 @@ EX(mf, 16);EX_E (ac,17 );EX (mc,18 );EX (xm,19 ); +EX (ve,20 );EX_E (cp,21 ); asm (".pushsection .text \n\t"
@@ -295,6 +296,7 @@ static void *idt_handlers[32] = { [17] = &ac_fault, [18] = &mc_fault, [19] = &xm_fault, + [20] = &ve_fault, [21] = &cp_fault, };
@@ -312,7 +314,9 @@ void setup_idt(void)continue ;set_idt_entry (i, idt_handlers[i],0 ); -handle_exception (i, check_exception_table); + +if (!exception_handlers[i]) +handle_exception (i, check_exception_table); } }
lib/x86/tdx.c +79 -1
97 lines not on this page · raw message
lib/x86/tdx.h +1 -0
@@ -26,6 +26,7 @@/* TDX module Call Leaf IDs */ #define TDG_VP_VMCALL 0 +#define TDG_VP_VEINFO_GET 3/* * Bitmasks of exposed registers (with VMM).
Signature · 1 line
2.25.1
RFC v2 4/18 x86 TDX: Bypass APIC and enable x2APIC directly
In reply to Qian WenFrom: Zhenzhong Duan <[email protected]>
According to TDX Module 1.5 Base Sepc, 11.9 Interrupt Handling and APIC Virtualization:
1. Guest TDs must use virtualized x2APIC mode. xAPIC mode(using memory mapped APIC access) is not allowed.
2. Guest TDs attempts to RDMSR or WRMSR the IA32_APIC_BASE MSR cause a VE to the guest TD. The guest TD cannot disable the APIC.
Bypass xAPIC initialization and enable x2APIC directly. Set software enable bit in x2APIC initializaion.
Use uid/apicid mapping to get apicid in setup_tss(). Initially I enabled x2APIC early so apic_id() could be used. But that brings issue for multiprocessor support as reading APIC_ID in AP triggers #VE and require gdt/tss/idt to be initialized early, so setup_gdt_tss() early.
--- lib/x86/apic.c | 4 ++++ lib/x86/setup.c | 13 ++++++++----- 2 files changed, 12 insertions(+), 5 deletions(-)
lib/x86/apic.c +4 -0
@@ -153,6 +153,10 @@ int enable_x2apic(void) asm ("rdmsr" :"=a" (a),"=d" (d) :"c" (MSR_IA32_APICBASE )); a |=1 <<10 ; asm ("wrmsr" : :"a" (a),"d" (d),"c" (MSR_IA32_APICBASE )); + +/* software APIC enabled bit is cleared after reset in TD-guest */ +x2apic_write (APIC_SPIV ,0x1ff ); +this_cpu_write_apic_ops ((void *)&x2apic_ops);return 1 ; }else {
lib/x86/setup.c +8 -5
@@ -112,8 +112,9 @@ unsigned long setup_tss(u8 *stacktop) {u32 id;tss64_t *tss_entry; +static u32 cpus =0 ; - id =pre_boot_apic_id (); + id =is_tdx_guest () ? id_map[cpus++] :pre_boot_apic_id ();/* Runtime address of current TSS */ tss_entry = &tss[id];
@@ -362,11 +363,13 @@ efi_status_t setup_efi(efi_bootinfo_t *efi_bootinfo)* Resetting the APIC sets the per-vCPU APIC ops and so must be * done after loading GS.base with the per-vCPU data. */ -reset_apic (); -mask_pic_interrupts (); +if (!is_tdx_guest ()) { +reset_apic (); +mask_pic_interrupts (); +enable_apic (); +save_id (); + }setup_page_table (); -enable_apic (); -save_id ();bsp_rest_init ();return EFI_SUCCESS ;
Signature · 1 line
2.25.1
RFC v2 5/18 x86 TDX: Add exception table support
In reply to Qian WenFrom: Zhenzhong Duan <[email protected]>
Exception table is used to fixup from a faulty instruction execution.
In TDX scenario, some instructions trigger #VE and are simulated through tdvmcall. If the simulation fails, the instruction is treated as faulty and should be checked with the exception table to fixup.
Move struct ex_record, exception_table_[start|end] to lib/x86/desc.h as it's a general declaration and will be used in TDX.
--- lib/x86/desc.c | 7 ------- lib/x86/desc.h | 6 ++++++ lib/x86/tdx.c | 23 ++++++++++++++++++++++- 3 files changed, 28 insertions(+), 8 deletions(-)
lib/x86/desc.c +0 -7
@@ -100,13 +100,6 @@ void set_idt_sel(int vec, u16 sel) e->selector = sel; } -struct ex_record { -unsigned long rip; -unsigned long handler; -}; - -extern struct ex_record exception_table_start, exception_table_end; -const char *exception_mnemonic (int vector) {switch (vector) {
lib/x86/desc.h +6 -0
@@ -237,6 +237,12 @@ extern tss64_t tss[];#endif extern gdt_entry_t gdt[]; +struct ex_record { +unsigned long rip; +unsigned long handler; +}; +extern struct ex_record exception_table_start, exception_table_end; +unsigned exception_vector (void );unsigned exception_error_code (void );bool exception_rflags_rf (void );
lib/x86/tdx.c +22 -1
@@ -303,6 +303,22 @@ done:return !!tdx_guest; } +static bool tdx_check_exception_table (struct ex_regs *regs) +{ +struct ex_record *ex; + +for (ex = &exception_table_start; ex != &exception_table_end; ++ex) { +if (ex->rip == regs->rip) { + regs->rip = ex->handler; +return true; + } + } +unhandled_exception (regs, false); + +/* never reached */ +return false; +} +static bool tdx_get_ve_info (struct ve_info *ve) {struct tdx_module_args args = {};
@@ -334,8 +350,13 @@ static bool tdx_get_ve_info(struct ve_info *ve)static bool tdx_handle_virt_exception (struct ex_regs *regs,struct ve_info *ve) { +unsigned int ex_val;int insn_len = -EIO ; +/* #VE exit_reason in bit16-32 */ + ex_val = regs->vector | (ve->exit_reason <<16 ); + asm("mov %0, %%gs:4" : :"r" (ex_val)); +switch (ve->exit_reason) {case EXIT_REASON_HLT : insn_len =handle_halt (regs, ve);
@@ -357,7 +378,7 @@ static bool tdx_handle_virt_exception(struct ex_regs *regs,return false; }if (insn_len <0 ) -return false; +return tdx_check_exception_table (regs);/* After successful #VE handling, move the IP */ regs->rip += insn_len;
Signature · 1 line
2.25.1
RFC v2 6/18 x86 TDX: Bypass wrmsr simulation on some specific MSRs
In reply to Qian WenFrom: Zhenzhong Duan <[email protected]>
In TDX scenario, some MSRs are initialized with expected value and not expected to be changed in TD-guest.
Writing to MSR_IA32_TSC, MSR_IA32_APICBASE, MSR_EFER in TD-guest triggers #VE. In #VE handler these MSR accesses are simulated with tdvmcall. But in current TDX host side implementation, they are bypassed and return failure.
In order to let test cases touching those MSRs run smoothly, bypass writing to those MSRs in #VE handler just like writing succeed.
--- lib/x86/tdx.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
lib/x86/tdx.c +16 -0
@@ -144,8 +144,23 @@ static int read_msr(struct ex_regs *regs, struct ve_info *ve)return ve_instr_len (ve); } +static bool tdx_is_bypassed_msr (u32 index) +{ +switch (index) { +case MSR_IA32_TSC : +case MSR_IA32_APICBASE : +case MSR_EFER : +return true; +default : +return false; + } +} +static int write_msr (struct ex_regs *regs,struct ve_info *ve) { +if (tdx_is_bypassed_msr (regs->rcx)) + goto finish_wrmsr; +struct tdx_module_args args = { .r10 =TDX_HYPERCALL_STANDARD , .r11 =hcall_func (EXIT_REASON_MSR_WRITE ),
@@ -161,6 +176,7 @@ static int write_msr(struct ex_regs *regs, struct ve_info *ve)if (__tdx_hypercall (&args))return -EIO ; +finish_wrmsr:return ve_instr_len (ve); }
Signature · 1 line
2.25.1
RFC v2 7/18 x86 TDX: Simulate single step on #VE handled instruction
In reply to Qian WenFrom: Zhenzhong Duan <[email protected]>
According to TDX spec, specific instructions are simulated in #VE handler.
To avoid missing single step on these instructions, such as cpuid(0xb) and wrmsr(0x1a0), we have to simulate #DB processing in #VE handler.
Move declaration of do_handle_exception() in header file, so it can be used in #VE handler for #DB processing.
--- lib/x86/desc.c | 5 ----- lib/x86/desc.h | 5 +++++ lib/x86/tdx.c | 6 ++++++ 3 files changed, 11 insertions(+), 5 deletions(-)
lib/x86/desc.c +0 -5
@@ -52,11 +52,6 @@ struct descriptor_table_ptr gdt_descr = { .base = (unsigned long)gdt, }; -#ifndef __x86_64__ -__attribute__((regparm(1))) -#endif -void do_handle_exception (struct ex_regs *regs); -/* * Fill an idt_entry_t or call gate entry, clearing e_sz bytes first. *
lib/x86/desc.h +5 -0
@@ -255,6 +255,11 @@ void set_gdt_entry(int sel, unsigned long base, u32 limit, u8 access, u8 gran);void load_gdt_tss (size_t tss_offset);void set_intr_alt_stack (int e,void *fn);void print_current_tss_info (void ); + +#ifndef __x86_64__ +__attribute__((regparm(1))) +#endif +void do_handle_exception (struct ex_regs *regs);handler handle_exception (u8 v,handler fn);void unhandled_exception (struct ex_regs *regs,bool cpu);const char *exception_mnemonic (int vector);
lib/x86/tdx.c +6 -0
@@ -398,6 +398,12 @@ static bool tdx_handle_virt_exception(struct ex_regs *regs,/* After successful #VE handling, move the IP */ regs->rip += insn_len; +/* Simulate single step on simulated instruction */ +if (regs->rflags &X86_EFLAGS_TF ) { + regs->vector =DB_VECTOR ; +write_dr6 (read_dr6 () | (1 <<14 )); +do_handle_exception (regs); + }return true; }
Signature · 1 line
2.25.1
RFC v2 8/18 x86 TDX: Extend EFI run script to support TDX
In reply to Qian WenFrom: Zhenzhong Duan <[email protected]>
Currently TDX framework is based on EFI support and running test case in TDX environment requires special QEMU command line parameters.
Add an environment variable EFI_TDX. When set, enable test case to run in TDX protected environment with special QEMU parameters.
Force "-cpu host" to be the last parameter as qemu doesn't support to customize CPU feature for TD guest currently.
Using "-bios" to load TDVF (OVMF with TDX support).
--- x86/efi/README.md | 6 ++++++ x86/efi/run | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+)
x86/efi/README.md +6 -0
@@ -30,6 +30,12 @@ the env variable `EFI_UEFI`: EFI_UEFI=/path/to/OVMF.fd ./x86/efi/run ./x86/msr.efi +### Run test cases with UEFI in TDX environment + +To run a test case with UEFI and TDX enabled: + + EFI_TDX=y ./x86/efi/run ./x86/msr.efi + ## Code structure ### Code from GNU-EFI
x86/efi/run +19 -0
@@ -18,6 +18,7 @@ source config.mak : "${EFI_TEST:=efi-tests}" : "${EFI_SMP:=1}" : "${EFI_CASE:=$(basename $1 .efi)}" +: "${EFI_TDX:=n}" if [ ! -f "$EFI_UEFI" ]; then echo "UEFI firmware not found: $EFI_UEFI"
@@ -29,6 +30,24 @@ fi # Remove the TEST_CASE from $@ shift 1 +# TDX support -kernel QEMU parameter, could utilize the original way of +# verifying QEMU's configuration. CPU feature customization isn't supported +# in TDX currently, so pass through all the features with `-cpu host` +if [ "$EFI_TDX" == "y" ]; then + "$TEST_DIR/run" \ + -bios "$EFI_UEFI" \ + -object tdx-guest,id=tdx0 \ + -machine q35,kernel_irqchip=split,confidential-guest-support=tdx0 \ + -kernel "$EFI_SRC/$EFI_CASE.efi" \ + -net none \ + -nographic \ + -m 256 \ + "$@" \ + -cpu host + + exit $? +fi + if [ "$EFI_CASE" = "_NO_FILE_4Uhere_" ]; then EFI_CASE=dummy fi
Signature · 1 line
2.25.1
RFC v2 9/18 x86 TDX: Add support for memory accept
In reply to Qian WenFrom: Zhenzhong Duan <[email protected]>
TDVF supports partial memory acceptance to optimize boot time and leaves remaining memory acceptance to OS.
Accept remaining memory of type EFI_UNACCEPTED_MEMORY at bootup. Try 1G page accept first even though hugepage memory isn't used in qemu command line currently.
Export below functions so they can be used by TDX specific sub-test in the future.
tdx_shared_mask()
tdx_accept_memory()
tdx_enc_status_changed()
--- lib/asm-generic/page.h | 7 +- lib/linux/efi.h | 23 ++++- lib/x86/asm/page.h | 19 ++++ lib/x86/setup.c | 6 +- lib/x86/tdx.c | 201 ++++++++++++++++++++++++++++++++++++++++- lib/x86/tdx.h | 18 +++- 6 files changed, 268 insertions(+), 6 deletions(-)
lib/asm-generic/page.h +5 -2
@@ -12,8 +12,11 @@#include <linux/const.h> #define PAGE_SHIFT 12 -#define PAGE_SIZE (_AC(1,UL) << PAGE_SHIFT) -#define PAGE_MASK (~(PAGE_SIZE-1)) +#define PAGE_SIZE (_AC(1, UL) << PAGE_SHIFT) +#define PAGE_MASK (~(PAGE_SIZE - 1)) +#define PMD_SHIFT 21 +#define PMD_SIZE (_AC(1, UL) << PMD_SHIFT) +#define PMD_MASK (~(PMD_SIZE - 1))#ifndef __ASSEMBLY__
lib/linux/efi.h +22 -1
@@ -104,7 +104,8 @@ typedef struct {#define EFI_MEMORY_MAPPED_IO_PORT_SPACE 12#define EFI_PAL_CODE 13#define EFI_PERSISTENT_MEMORY 14 -#define EFI_MAX_MEMORY_TYPE 15 +#define EFI_UNACCEPTED_MEMORY 15 +#define EFI_MAX_MEMORY_TYPE 16/* Attribute values: */ #define EFI_MEMORY_UC ((u64)0x0000000000000001ULL)/* uncached */
@@ -542,6 +543,26 @@ typedef struct {efi_char16_t file_name[1 ]; } efi_file_info_t; +/* +* efi_memdesc_ptr - get the n-th EFI memmap descriptor +* @map: the start of efi memmap +* @desc_size: the size of space for each EFI memmap descriptor +* @n: the index of efi memmap descriptor +* +* EFI boot service provides the GetMemoryMap() function to get a copy of the +* current memory map which is an array of memory descriptors, each of +* which describes a contiguous block of memory. It also gets the size of the +* map, and the size of each descriptor, etc. +* +* Note that per section 6.2 of UEFI Spec 2.6 Errata A, the returned size of +* each descriptor might not be equal to sizeof(efi_memory_memdesc_t), +* since efi_memory_memdesc_t may be extended in the future. Thus the OS +* MUST use the returned size of the descriptor to find the start of each +* efi_memory_memdesc_t in the memory map array. +*/ +#define efi_memdesc_ptr (map, desc_size, n) \ + ((efi_memory_desc_t *)((void *)(map) + ((n) * (desc_size)))) +#define efi_bs_call (func, ...) efi_system_table->boottime->func(__VA_ARGS__)#define efi_rs_call (func, ...) efi_system_table->runtime->func(__VA_ARGS__)
lib/x86/asm/page.h +19 -0
@@ -79,5 +79,24 @@ extern unsigned long long get_amd_sev_addr_upperbound(void);#define PGDIR_BITS (lvl) (((lvl) - 1) * PGDIR_WIDTH + PAGE_SHIFT)#define PGDIR_OFFSET (va, lvl) (((va) >> PGDIR_BITS(lvl)) & PGDIR_MASK) +enum pg_level { +PG_LEVEL_NONE , +PG_LEVEL_4K , +PG_LEVEL_2M , +PG_LEVEL_1G , +PG_LEVEL_512G , +PG_LEVEL_NUM +}; + +#define PTE_SHIFT 9 +static inline int page_level_shift (enum pg_level level) +{ +return (PAGE_SHIFT -PTE_SHIFT ) + level *PTE_SHIFT ; +} +static inline unsigned long page_level_size (enum pg_level level) +{ +return 1UL <<page_level_shift (level); +} +#endif /* !__ASSEMBLY__ */ #endif
lib/x86/setup.c +5 -1
@@ -309,7 +309,11 @@ efi_status_t setup_efi(efi_bootinfo_t *efi_bootinfo)efi_status_t status;const char *phase; - status =setup_tdx (); +/* +* TDVF support partial memory accept, accept remaining memory +* early so memory allocator can use it. +*/ + status =setup_tdx (efi_bootinfo);if (status !=EFI_SUCCESS && status !=EFI_UNSUPPORTED ) {printf ("INTEL TDX setup failed, error = 0x%lx\n" , status);return status;
lib/x86/tdx.c +200 -1
227 lines not on this page · raw message
lib/x86/tdx.h +17 -1
@@ -26,7 +26,14 @@/* TDX module Call Leaf IDs */ #define TDG_VP_VMCALL 0 +#define TDG_VP_INFO 1#define TDG_VP_VEINFO_GET 3 +#define TDG_MEM_PAGE_ACCEPT 6 + +/* TDX hypercall Leaf IDs */ +#define TDVMCALL_MAP_GPA 0x10001 + +#define TDVMCALL_STATUS_RETRY 1/* * Bitmasks of exposed registers (with VMM).
@@ -57,6 +64,12 @@#define BUG_ON (condition) do { if (condition) abort(); } while (0) +/* TDX supported page sizes from the TDX module ABI. */ +#define TDX_PS_4K 0 +#define TDX_PS_2M 1 +#define TDX_PS_1G 2 +#define TDX_PS_NR (TDX_PS_1G + 1) +#define EXIT_REASON_CPUID 10#define EXIT_REASON_HLT 12#define EXIT_REASON_IO_INSTRUCTION 30
@@ -141,7 +154,10 @@ struct ve_info { };bool is_tdx_guest (void ); -efi_status_t setup_tdx (void ); +phys_addr_t tdx_shared_mask (void ); +bool tdx_accept_memory (phys_addr_t start,phys_addr_t end); +bool tdx_enc_status_changed (phys_addr_t start,phys_addr_t end,bool enc); +efi_status_t setup_tdx (efi_bootinfo_t *efi_bootinfo);#else inline bool is_tdx_guest (void ) {return false; }
Signature · 1 line
2.25.1
RFC v2 10/18 acpi: Add MADT table parse code
In reply to Qian WenFrom: Zhenzhong Duan <[email protected]>
Support LAPIC, X2APIC and WAKEUP sub-table, other sub-table are ignored for now. Also add a wakeup wrapping function which is used by TDX.
The parsed result is stored in id_map[] and acpi_mp_wake_mailbox.
--- lib/acpi.c | 160 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/acpi.h | 59 +++++++++++++++++- lib/x86/setup.c | 4 ++ 3 files changed, 222 insertions(+), 1 deletion(-)
lib/acpi.c +160 -0
@@ -1,5 +1,7 @@#include "libcflat.h" #include "acpi.h" +#include "errno.h" +#include "asm/barrier.h" #ifdef CONFIG_EFI struct acpi_table_rsdp *efi_rsdp =NULL ;
@@ -127,3 +129,161 @@ int acpi_table_parse_madt(enum acpi_madt_type mtype, acpi_table_handler handler)return count; } + +struct acpi_madt_multiproc_wakeup_mailbox *acpi_mp_wake_mailbox; + +#define smp_store_release (p, val) \ +do { \ + barrier(); \ + WRITE_ONCE(*p, val); \ +} while (0) + +static inline bool test_bit (int nr,const void *addr) +{ +const u32 *p = (const u32 *)addr; +return ((1UL << (nr &31 )) & (p[nr >>5 ])) !=0 ; +} + +int acpi_wakeup_cpu (int apicid,unsigned long start_ip,unsigned char * cpus) +{ +u8 timeout =0xFF ; + +/* +* According to the ACPI specification r6.4, sec 5.2.12.19, the +* mailbox-based wakeup mechanism cannot be used more than once +* for the same CPU, so skip sending wake commands to already +* awake CPU. +*/ +if (test_bit (apicid, cpus)) { +printf ("CPU already awake (APIC ID %x), skipping wakeup\n" , + apicid); +return -EINVAL ; + } + +/* +* Mailbox memory is shared between firmware and OS. Firmware will +* listen on mailbox command address, and once it receives the wakeup +* command, CPU associated with the given apicid will be booted. So, +* the value of apic_id and wakeup_vector has to be set before updating +* the wakeup command. So use smp_store_release to let the compiler know +* about it and preserve the order of writes. +*/ +smp_store_release (&acpi_mp_wake_mailbox->apic_id, apicid); +smp_store_release (&acpi_mp_wake_mailbox->wakeup_vector, start_ip); +smp_store_release (&acpi_mp_wake_mailbox->command, +ACPI_MP_WAKE_COMMAND_WAKEUP ); + +/* +* After writing wakeup command, wait for maximum timeout of 0xFF +* for firmware to reset the command address back zero to indicate +* the successful reception of command. +* NOTE: 255 as timeout value is decided based on our experiments. +* +* XXX: Change the timeout once ACPI specification comes up with +* standard maximum timeout value. +*/ +while (READ_ONCE (acpi_mp_wake_mailbox->command) && timeout--) +cpu_relax (); + +/* If timed out (timeout == 0), return error. +* Otherwise, the CPU wakes up successfully. +*/ +return timeout ==0 ? -EIO :0 ; +} + +static bool parse_madt_table (struct acpi_table *madt,unsigned char * id_map) +{ +u64 table_start = (unsigned long )madt +sizeof (struct acpi_table_madt ); +u64 table_end = (unsigned long )madt + madt->length; +struct acpi_subtable_header *sub_table; +bool failed = false; +u32 uid, apic_id; +u8 enabled; + +while (table_start < table_end && !failed) { +struct acpi_madt_local_apic *processor; +struct acpi_madt_local_x2apic *processor2; +struct acpi_madt_multiproc_wakeup *mp_wake; + + sub_table = (struct acpi_subtable_header *)table_start; + +switch (sub_table->type) { +case ACPI_MADT_TYPE_LOCAL_APIC : + processor = (struct acpi_madt_local_apic *)sub_table; + +if (BAD_MADT_ENTRY (processor, table_end)) { + failed = true; +break ; + } + + uid = processor->processor_id; + apic_id = processor->id; + enabled = processor->lapic_flags &ACPI_MADT_ENABLED ; + +/* Ignore invalid ID */ +if (apic_id ==0xff ) +break ; +if (enabled) + id_map[uid] = apic_id; + +printf ("apicid %x uid %x %s\n" , apic_id, uid, + enabled ?"enabled" :"disabled" ); +break ; + +case ACPI_MADT_TYPE_LOCAL_X2APIC : + processor2 = (struct acpi_madt_local_x2apic *)sub_table; + +if (BAD_MADT_ENTRY (processor2, table_end)) { + failed = true; +break ; + } + + uid = processor2->uid; + apic_id = processor2->local_apic_id; + enabled = processor2->lapic_flags &ACPI_MADT_ENABLED ; + +/* Ignore invalid ID */ +if (apic_id ==0xffffffff ) +break ; +if (enabled) + id_map[uid] = apic_id; + +printf ("x2apicid %x uid %x %s\n" , apic_id, uid, + enabled ?"enabled" :"disabled" ); +break ; +case ACPI_MADT_TYPE_MULTIPROC_WAKEUP : + mp_wake = (struct acpi_madt_multiproc_wakeup *)sub_table; + +if (BAD_MADT_ENTRY (mp_wake, table_end)) { + failed = true; +break ; + } + +if (acpi_mp_wake_mailbox) +printf ("WARN: duplicate mailbox %lx\n" , (u64 )acpi_mp_wake_mailbox); + + acpi_mp_wake_mailbox = (void *)mp_wake->base_address; +printf ("MP Wake (Mailbox version[%d] base_address[%lx])\n" , + mp_wake->mailbox_version, mp_wake->base_address); +break ; +default : +/* Ignored currently */ +break ; + } +if (!failed) + table_start += sub_table->length; + } + +return !failed; +} + +bool parse_acpi_table (unsigned char * id_map) +{ +struct acpi_table *tb; + + tb =find_acpi_table_addr (MADT_SIGNATURE ); +if (tb) +return parse_madt_table (tb, id_map); + +return false; +}
lib/acpi.h +58 -1
72 lines not on this page · raw message
lib/x86/setup.c +4 -0
@@ -339,6 +339,10 @@ efi_status_t setup_efi(efi_bootinfo_t *efi_bootinfo)return status; } +/* Parse all acpi tables, currently only MADT table */ +if (!parse_acpi_table (id_map)) +return EFI_NOT_FOUND ; + phase ="AMD SEV" ; status =setup_amd_sev ();
Signature · 1 line
2.25.1
RFC v2 11/18 x86 TDX: Add multi processor support
In reply to Qian WenFrom: Zhenzhong Duan <[email protected]>
In TD-guest, multiprocessor support is different from normal guest.
In normal guest, BSP send startup IPI to all APs to trigger APs starting from 16bit real mode.
While in TD-guest, TDVF initializes APs into 64bit mode before pass to OS/bootloader. OS enumerates uid/apicid mapping information through MADT table and wake up APs one by one through MP wakeup mechanism. So the entry code for APs is 64bit.
--- lib/x86/setup.c | 16 +++++++++++---- lib/x86/smp.c | 26 +++++++++++++++++++++++++ lib/x86/smp.h | 2 ++ x86/efi/efistart64.S | 46 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 4 deletions(-)
lib/x86/setup.c +12 -4
@@ -407,17 +407,25 @@ void save_id(void)void ap_start64 (void ) {setup_gdt_tss (); -reset_apic ();load_idt (); -save_id (); -enable_apic (); +if (!is_tdx_guest ()) { +reset_apic (); +save_id (); +enable_apic (); + }enable_x2apic ();ap_online (); } +extern void tdx_ap_start64 (void );void bsp_rest_init (void ) { -bringup_aps (); +if (!is_tdx_guest ()) { +bringup_aps (); + }else { +/* TDX uses ACPI WAKE UP mechanism to wake up APs instead of SIPI */ +bringup_aps_acpi ((u64 )tdx_ap_start64); + }enable_x2apic ();smp_init ();pmu_init ();
lib/x86/smp.c +26 -0
@@ -11,6 +11,7 @@#include "desc.h" #include "alloc_page.h" #include "asm/page.h" +#include "acpi.h" #define IPI_VECTOR 0x20
@@ -288,3 +289,28 @@ void bringup_aps(void)while (_cpu_count !=atomic_read (&cpu_online_count))cpu_relax (); } + +/* wakeup APs by sending the OS commands to ACPI mailbox. */ +efi_status_t bringup_aps_acpi (unsigned long start_ip) +{ +u32 i; + _cpu_count =fwcfg_get_nb_cpus (); + +/* BSP is already online */ +set_bit (id_map[0 ], online_cpus); + +#ifdef CONFIG_EFI + smp_stacktop = ((u64) (&stacktop)) -PAGE_SIZE ; +#endif + +for (i =1 ; i < _cpu_count; i++) { +if (acpi_wakeup_cpu (id_map[i], start_ip, online_cpus)) +return EFI_DEVICE_ERROR ; +set_bit (id_map[i], online_cpus); + } + +while (atomic_read (&cpu_online_count) != _cpu_count) +cpu_relax (); + +return EFI_SUCCESS ; +}
lib/x86/smp.h +2 -0
@@ -6,6 +6,7 @@#include "libcflat.h" #include "atomic.h" #include "apic-defs.h" +#include "efi.h" /* Address where to store the address of realmode GDT descriptor. */ #define REALMODE_GDT_LOWMEM (PAGE_SIZE - 2)
@@ -86,6 +87,7 @@ void on_cpus(void (*function)(void *data), void *data);void smp_reset_apic (void );void bringup_aps (void );void ap_online (void ); +efi_status_t bringup_aps_acpi (unsigned long start_ip);extern atomic_t cpu_online_count;extern unsigned char online_cpus[(MAX_TEST_CPUS +7 ) /8 ];
x86/efi/efistart64.S +46 -0
52 lines not on this page · raw message
Signature · 1 line
2.25.1
RFC v2 12/18 x86 TDX: Add a formal IPI handler
In reply to Qian WenFrom: Zhenzhong Duan <[email protected]>
Current IPI handler may corrupt cpu context, it's not an big issue as AP only enable interrupt in idle loop.
But in TD-guest, hlt instruction is simulated though tdvmcall in #VE handler. IPI will corrupt #VE context.
Save and restore cpu context in IPI handler to avoid crash.
--- lib/x86/smp.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-)
lib/x86/smp.c +13 -5
@@ -60,12 +60,20 @@ static __attribute__((used)) void ipi(void) asm ( "ipi_entry: \n" - " call ipi \n" -#ifndef __x86_64__ - " iret" -#else -" iretq" +#ifdef __x86_64__ + "push %r15; push %r14; push %r13; push %r12 \n\t" + "push %r11; push %r10; push %r9; push %r8 \n\t" #endif + "push %"R "di; push %"R "si; push %"R "bp; \n\t" + "push %"R "bx; push %"R "dx; push %"R "cx; push %"R "ax \n\t" + "call ipi \n\t" + "pop %"R "ax; pop %"R "cx; pop %"R "dx; pop %"R "bx \n\t" + "pop %"R "bp; pop %"R "si; pop %"R "di \n\t" +#ifdef __x86_64__ + "pop %r8; pop %r9; pop %r10; pop %r11 \n\t" + "pop %r12; pop %r13; pop %r14; pop %r15 \n\t" +#endif +"iret" W " \n\t" );int cpu_count (void )
Signature · 1 line
2.25.1
RFC v2 13/18 x86 TDX: Enable lvl5 boot page table
In reply to Qian WenFrom: Zhenzhong Duan <[email protected]>
TDVF enables lvl5 page table before pass to OS/bootloader while OVMF enables lvl4 page table.
Check CR4.X86_CR4_LA57 to decide which page table level to use and initialize our own lvl5 page table if TDX.
Refactor the common part of setting cr3 in a wrapper function load_page_table().
--- lib/x86/setup.c | 19 +++++++++++++++++-- x86/efi/efistart64.S | 5 +++++ 2 files changed, 22 insertions(+), 2 deletions(-)
lib/x86/setup.c +17 -2
@@ -265,10 +265,23 @@ static efi_status_t setup_rsdp(efi_bootinfo_t *efi_bootinfo) }/* Defined in cstart64.S or efistart64.S */ +extern u8 ptl5;extern u8 ptl4;extern u8 ptl3;extern u8 ptl2; +static void load_page_table (void ) +{ +/* +* Load new page table based on the level of firmware provided page +* table. +*/ +if (read_cr4 () &X86_CR4_LA57 ) +write_cr3 ((ulong)&ptl5); +else +write_cr3 ((ulong)&ptl4); +} +static void setup_page_table (void ) {pgd_t *curr_pt;
@@ -281,6 +294,9 @@ static void setup_page_table(void)/* Set AMD SEV C-Bit for page table entries */ flags |=get_amd_sev_c_bit_mask (); +/* Level 5 */ + curr_pt = (pgd_t *)&ptl5; + curr_pt[0 ] = ((phys_addr_t)&ptl4) | flags;/* Level 4 */ curr_pt = (pgd_t *)&ptl4; curr_pt[0 ] = ((phys_addr_t)&ptl3) | flags;
@@ -300,8 +316,7 @@ static void setup_page_table(void)setup_ghcb_pte ((pgd_t *)&ptl4); } -/* Load 4-level page table */ -write_cr3 ((ulong)&ptl4); +load_page_table (); }efi_status_t setup_efi (efi_bootinfo_t *efi_bootinfo)
x86/efi/efistart64.S +5 -0
@@ -31,6 +31,11 @@ ptl4: . = . + PAGE_SIZE .align PAGE_SIZE +.globl ptl5 +ptl5: + . = . + PAGE_SIZE +.align PAGE_SIZE + .section .init .code64 .text
Signature · 1 line
2.25.1
RFC v2 14/18 x86 TDX: Add lvl5 page table support to virtual memory
In reply to Qian WenFrom: Zhenzhong Duan <[email protected]>
Currently in TDX test case init stage, it setup an initial lvl5 boot page table, but VM code support only lvl4 page table. This mismatch make the test cases requiring virtual memory crash.
Add below changes to support lvl5 page table for virtual memory:
1. skip finding high memory
2. check X86_CR4_LA57 to decide to initialize lvl5 or lvl4 page table
3. always set X86_CR0_NE for TDX test case
--- lib/x86/setup.c | 5 +++++ lib/x86/vm.c | 15 +++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-)
lib/x86/setup.c +5 -0
@@ -68,6 +68,11 @@ static struct mbi_bootinfo *bootinfo;#ifdef __x86_64__void find_highmem (void ) { +#ifdef CONFIG_EFI +/* The largest free memory region is already chosen in setup_efi() */ +return ; +#endif /* CONFIG_EFI */ +/* Memory above 4 GB is only supported on 64-bit systems. */ if (!(bootinfo->flags &64 ))return ;
lib/x86/vm.c +11 -4
@@ -3,8 +3,10 @@#include "vmalloc.h" #include "alloc_page.h" #include "smp.h" +#include "tdx.h" static pteval_t pte_opt_mask; +static int page_level; pteval_t *install_pte(pgd_t *cr3, int pte_level,
@@ -16,7 +18,7 @@ pteval_t *install_pte(pgd_t *cr3,pteval_t *pt = cr3;unsigned offset; -for (level =PAGE_LEVEL ; level > pte_level; --level) { +for (level = page_level; level > pte_level; --level) { offset =PGDIR_OFFSET ((uintptr_t )virt, level);if (!(pt[offset] &PT_PRESENT_MASK )) {pteval_t *new_pt = pt_page;
@@ -49,9 +51,9 @@ struct pte_search find_pte_level(pgd_t *cr3, void *virt,unsigned shift;struct pte_search r; -assert (lowest_level >=1 && lowest_level <=PAGE_LEVEL ); +assert (lowest_level >=1 && lowest_level <= page_level); -for (r.level =PAGE_LEVEL ;; --r.level) { +for (r.level = page_level;; --r.level) { shift = (r.level -1 ) *PGDIR_WIDTH +12 ; offset = ((uintptr_t )virt >> shift) &PGDIR_MASK ; r.pte = &pt[offset];
@@ -185,6 +187,7 @@ void *setup_mmu(phys_addr_t end_of_memory, void *opt_mask) pte_opt_mask =PT_USER_MASK ;memset (cr3,0 ,PAGE_SIZE ); + page_level = !!(read_cr4 () &X86_CR4_LA57 ) ?5 :PAGE_LEVEL ;#ifdef __x86_64__if (end_of_memory < (1ul <<32 ))
@@ -201,7 +204,11 @@ void *setup_mmu(phys_addr_t end_of_memory, void *opt_mask)#ifndef __x86_64__write_cr4 (X86_CR4_PSE );#endif -write_cr0 (X86_CR0_PG |X86_CR0_PE |X86_CR0_WP ); +/* According to TDX module spec 10.6.1 CR0.NE should be 1 */ +if (is_tdx_guest ()) +write_cr0 (X86_CR0_PG |X86_CR0_PE |X86_CR0_WP |X86_CR0_NE ); +else +write_cr0 (X86_CR0_PG |X86_CR0_PE |X86_CR0_WP );printf ("paging enabled\n" );printf ("cr0 = %lx\n" ,read_cr0 ());
Signature · 1 line
2.25.1
RFC v2 15/18 x86 TDX: bypass unsupported syscall TF for TDX
In reply to Qian WenFrom: Zhenzhong Duan <[email protected]>
The syscall TF is unsupported in TDX, therefore skip it directly.
--- x86/syscall.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
x86/syscall.c +2 -1
@@ -5,6 +5,7 @@#include "msr.h" #include "desc.h" #include "fwcfg.h" +#include "tdx.h" static void test_syscall_lazy_load (void ) {
@@ -106,7 +107,7 @@ int main(int ac, char **av) {test_syscall_lazy_load (); -if (!no_test_device || !is_intel ()) +if ((!no_test_device || !is_intel ()) && !is_tdx_guest ())test_syscall_tf ();else report_skip ("syscall TF handling" );
Signature · 1 line
2.25.1
RFC v2 16/18 x86 TDX: Modify the MSR test to be compatible with TDX
In reply to Qian WenFrom: Zhenzhong Duan <[email protected]>
According to TDX Module 1.5 ABI Sepc, Table 2.1 MSR Virtualization, the following modifications are made in MSRs test.
1. Skip sub-tests about MSR_IA32_MISC_ENABLE and MSR_CSTAR, since changing those MSRs are unsupported:
- MSR_IA32_MISC_ENABLE is reading native and #VE in writing.
- MSR_CSTAR is #VE in reading/writing and its simulation is not supported in TDX host side.
2. Skip the x2apic-msrs test in x2apic disabled mode. The TDX guest only supports X2APIC, and cannot disable APIC.
3. Add readable flag for x2apic MSRs if TDX guest.
There is a gap between VMX and TDX about read registers in the virtual APIC page. KVM will intercept reads to non-existent MSRs[1], the TDX doesn't validate it and directly reads **native**. So add a readable flag for x2apic MSRs if TDX guest to avoid #GP checking for RDMSR.
4. Add #VE check for MSR operation fault.
For some MSRs, e.g., MCE-related MSR, they are virtualized by #VE in tdx. The exception of invalid RDMSR/WRMSR for it is #VE instead of #GP, so correct it in the test_rdmsr_fault() and test_wrmsr_fault().
[1] KVM: VMX: Always intercept accesses to unsupported "extended" x2APIC regs, https://lore.kernel.org/r/[email protected]
--- x86/msr.c | 46 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 8 deletions(-)
x86/msr.c +38 -8
@@ -5,6 +5,7 @@#include "processor.h" #include "msr.h" #include <stdlib.h> +#include "tdx.h" /* * This test allows two modes:
@@ -97,9 +98,11 @@ static void test_wrmsr(u32 msr, const char *name, unsigned long long val)static void test_wrmsr_fault (u32 msr,const char *name,unsigned long long val) {unsigned char vector =wrmsr_safe (msr, val); - -report (vector ==GP_VECTOR , -"Expected #GP on WRSMR(%s, 0x%llx), got vector %d" , +bool pass = false; +if (vector ==GP_VECTOR || (vector ==VE_VECTOR &&is_tdx_guest ())) + pass = true; +report (pass, +"Expected #GP/#VE on WRSMR(%s, 0x%llx), got vector %d" , name, val, vector); }
@@ -107,13 +110,20 @@ static void test_rdmsr_fault(u32 msr, const char *name) {uint64_t ignored;unsigned char vector =rdmsr_safe (msr, &ignored); - -report (vector ==GP_VECTOR , -"Expected #GP on RDSMR(%s), got vector %d" , name, vector); +bool pass = false; +if (vector ==GP_VECTOR || (vector ==VE_VECTOR &&is_tdx_guest ())) + pass = true; +report (pass, +"Expected #GP/#VE on RDSMR(%s), got vector %d" , name, vector); }static void test_msr (struct msr_info *msr,bool is_64bit_host) { +/* Changing MSR_IA32_MISC_ENABLE and MSR_CSTAR is unsupported in TDX */ +if ((msr->index ==MSR_IA32_MISC_ENABLE || msr->index ==MSR_CSTAR ) && +is_tdx_guest ()) +return ; +if (is_64bit_host || !msr->is_64bit_only) {__test_msr_rw (msr->index, msr->name, msr->value, msr->keep);
@@ -223,6 +233,24 @@ static void test_mce_msrs(void) } } +static enum x2apic_reg_semantics get_x2apic_reg_semantics2 (u32 reg) +{ +enum x2apic_reg_semantics ret; + ret =get_x2apic_reg_semantics (reg); + +if (is_tdx_guest ()) { +switch (reg) { +case APIC_ARBPRI : +case APIC_EOI : +case APIC_RRR : +case APIC_DFR : +case APIC_SELF_IPI : + ret |=X2APIC_RO ; + } + } +return ret; +} +static void __test_x2apic_msrs (bool x2apic_enabled) {enum x2apic_reg_semantics semantics;
@@ -234,7 +262,7 @@ static void __test_x2apic_msrs(bool x2apic_enabled)snprintf (msr_name,sizeof (msr_name),"x2APIC MSR 0x%x" , index);if (x2apic_enabled) - semantics =get_x2apic_reg_semantics (i); + semantics =get_x2apic_reg_semantics2 (i);else semantics =X2APIC_INVALID ;
@@ -270,13 +298,15 @@ static void __test_x2apic_msrs(bool x2apic_enabled)static void test_x2apic_msrs (void ) { +if (is_tdx_guest ()) + goto test_x2apic;reset_apic ();__test_x2apic_msrs (false);if (!enable_x2apic ())return ; - +test_x2apic:__test_x2apic_msrs (true); }
Signature · 1 line
2.25.1
RFC v2 17/18 x86 TDX: Add TDX specific test case
In reply to Qian WenFrom: Zhenzhong Duan <[email protected]>
Sub-test1:
Test APIC self IPI with vector < 16 trigger #VE.
Sub-test2:
Test single step on simulation instructions work well with single step emulation in #VE handler, we choose cpuid(0xb, Extended Topology Enumeration Leaf) and wrmsr(0x828, IA32_X2APIC_ESR) to test.
Please note not all cpuid trigger #VE, e.g., cpuid(0) will not. And not all MSRs could be used for single step test, e.g., the MSR_IA32_MISC_ENABLE (0x1a0) is reading native (if PERFMON) and #VE in writing. If ~PERFMON, the guest read value will be changed by TDX
module: the bit 7 = 0, bit 12 = 1. So the write value usually is not equal to the kvm stored value, and single step test breaks.
Sub-test3:
Support checks for the fixed value specified in TDX Module 1.5 ABI Spec, Table 2.2 CPUID Virtualization.
--- x86/Makefile.x86_64 | 1 + x86/intel_tdx.c | 326 ++++++++++++++++++++++++++++++++++++++++++++ x86/unittests.cfg | 4 + 3 files changed, 331 insertions(+) create mode 100644 x86/intel_tdx.c
x86/Makefile.x86_64 +1 -0
@@ -41,6 +41,7 @@ tests += $(TEST_DIR)/pmu_pebs.$(exe) ifeq ($(CONFIG_EFI),y) tests += $(TEST_DIR)/amd_sev.$(exe) +tests += $(TEST_DIR)/intel_tdx.$(exe) endif # The following test cases are disabled when building EFI tests because they
x86/intel_tdx.c new file +326 -0
326 lines not on this page · raw message
x86/unittests.cfg +4 -0
@@ -491,3 +491,7 @@ file = cet.flat arch = x86_64 smp = 2 extra_params = -enable-kvm -m 2048 -cpu host + +[intel_tdx] +file = intel_tdx.flat +arch = x86_64
Signature · 1 line
2.25.1
RFC v2 18/18 x86 TDX: Make run_tests.sh work with TDX
In reply to Qian WenFrom: Zhenzhong Duan <[email protected]>
Define a special group 'tdx' for those test cases supported by TDX. So that when group 'tdx' specified, these test cases run in TDX protected environment if EFI_TDX=y.
For example:
EFI_TDX=y ./run_tests.sh -g tdx
--- README.md | 6 ++++++ x86/unittests.cfg | 17 ++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-)
README.md +6 -0
@@ -137,6 +137,12 @@ when the user does not provide an environ, then an environ generated from the ./errata.txt file and the host's kernel version is provided to all unit tests. +# Unit test in TDX environment + + All the test cases supported by TDX belong to 'tdx' group, by this + command: "EFI_TDX=y ./run_tests.sh -g tdx", all these test cases run + in a TDX protected environment. + # Contributing ## Directory structure
x86/unittests.cfg +16 -1
@@ -69,10 +69,12 @@ arch = i386 [smptest] file = smptest.flat smp = 2 +groups = tdx [smptest3] file = smptest.flat smp = 3 +groups = tdx [vmexit_cpuid] file = vmexit.flat
@@ -186,6 +188,7 @@ file = hypercall.flat [idt_test] file = idt_test.flat arch = x86_64 +groups = tdx #[init] #file = init.flat
@@ -194,6 +197,7 @@ arch = x86_64 file = memory.flat extra_params = -cpu max arch = x86_64 +groups = tdx [msr] # Use GenuineIntel to ensure SYSENTER MSRs are fully preserved, and to test
@@ -202,6 +206,7 @@ arch = x86_64 # will fail due to shortcomings in KVM. file = msr.flat extra_params = -cpu max,vendor=GenuineIntel +groups = tdx [pmu] file = pmu.flat
@@ -241,6 +246,7 @@ file = s3.flat [setjmp] file = setjmp.flat +groups = tdx [sieve] file = sieve.flat
@@ -250,10 +256,12 @@ timeout = 180 file = syscall.flat arch = x86_64 extra_params = -cpu Opteron_G1,vendor=AuthenticAMD +groups = tdx [tsc] file = tsc.flat extra_params = -cpu max +groups = tdx [tsc_adjust] file = tsc_adjust.flat
@@ -263,10 +271,12 @@ extra_params = -cpu max file = xsave.flat arch = x86_64 extra_params = -cpu max +groups = tdx [rmap_chain] file = rmap_chain.flat arch = x86_64 +groups = tdx [svm] file = svm.flat
@@ -306,7 +316,7 @@ extra_params = --append "10000000 `date +%s`" file = pcid.flat extra_params = -cpu qemu64,+pcid,+invpcid arch = x86_64 -groups = pcid +groups = pcid tdx [pcid-disabled] file = pcid.flat
@@ -324,10 +334,12 @@ groups = pcid file = rdpru.flat extra_params = -cpu max arch = x86_64 +groups = tdx [umip] file = umip.flat extra_params = -cpu qemu64,+umip +groups = tdx [la57] file = la57.flat
@@ -447,6 +459,7 @@ check = /sys/module/kvm_intel/parameters/allow_smaller_maxphyaddr=Y [debug] file = debug.flat arch = x86_64 +groups = tdx [hyperv_synic] file = hyperv_synic.flat
@@ -485,6 +498,7 @@ extra_params = -M q35,kernel-irqchip=split -device intel-iommu,intremap=on,eim=o file = tsx-ctrl.flat extra_params = -cpu max groups = tsx-ctrl +groups = tdx [intel_cet] file = cet.flat
@@ -495,3 +509,4 @@ extra_params = -enable-kvm -m 2048 -cpu host [intel_tdx] file = intel_tdx.flat arch = x86_64 +groups = tdx nodefault
Signature · 1 line
2.25.1