Skip to contentSkip to conversation

X86: TDX framework support

RFC PATCH New Zhenzhong Duan 1 participant 18 messages Mar 3, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

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 link at github for each software component.

* What we add?
This patchset adds a basic framework to support running exist and future test cases in TDX protected environment, so as to verify the function of the TDX software stack. Appreciate any comment and suggestion.

This framework depends on UEFI support from Zixuan and Marc.

The supported test cases are marked in a test group named "tdx". Most of the unsupported test cases are due to testing features not supported by TDX, a few are due to their special design not suitable for running in UEFI.

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 upstream OVMF and qemu have TDX support.

* Patch organization
patch 1-7: add initial support for TDX, some simple test cases could run with them.
patch 8: TDVF support accepting part of the whole memory and this patch add
support for accepting remain memory.
patch 9-11: add multiprocessor support
patch12-13: enable lvl5 page table as TDVF use lvl5 page table
patch 14: TDX specific test case, may add more sub-test in the future
patch 15: bypass unsupported sub-test
patch16-17: enable all the TDX supported test cases to run in a batch with run_tests.sh

TODO:
1. merge TDX multiprocessor code in UEFI MP code framework when it's ready
2. add more TDX specific sub-test
3. add mmio simuation in #VE handler

[1] https://lwn.net/Articles/876997

Zhenzhong Duan (17):
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: Add TDX specific test case
x86 TDX: bypass unsupported sub-test for TDX x86 UEFI: Add support for parameter passing
x86 TDX: Make run_tests.sh work with TDX

 README.md                 |   6 +
 lib/argv.c                |   2 +-
 lib/argv.h                |   1 +
 lib/asm-generic/page.h    |   7 +-
 lib/efi.c                 |  73 +++++
 lib/linux/efi.h           |  41 ++-
 lib/x86/acpi.c            | 171 +++++++++++
 lib/x86/acpi.h            |  85 ++++++
 lib/x86/apic.c            |   4 +
 lib/x86/asm/setup.h       |   2 +
 lib/x86/desc.c            |  15 +-
 lib/x86/desc.h            |  10 +
 lib/x86/processor.h       |   2 +
 lib/x86/setup.c           |  82 +++++-
 lib/x86/smp.c             |  18 +-
 lib/x86/tdcall.S          | 303 ++++++++++++++++++++
 lib/x86/tdx.c             | 576 ++++++++++++++++++++++++++++++++++++++
 lib/x86/tdx.h             | 113 ++++++++
 lib/x86/vm.c              |  14 +-
 x86/Makefile.common       |   2 +
 x86/Makefile.x86_64       |   1 +
 x86/efi/README.md         |   6 +
 x86/efi/crt0-efi-x86_64.S |  12 +-
 x86/efi/efistart64.S      |  10 +
 x86/efi/run               |  19 ++
 x86/intel_tdx.c           |  94 +++++++
 x86/msr.c                 |   6 +
 x86/syscall.c             |   3 +-
 x86/unittests.cfg         |  22 +-
 29 files changed, 1668 insertions(+), 32 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 x86/intel_tdx.c
Signature · 1 line
2.25.1
#1 lore raw

RFC PATCH 1/17 x86 TDX: Add support functions for TDX framework

Port tdcall.S and tdx.c from TDX guest kernel source in arch/x86/kernel directory, simplified and keep only code which is useful for TDX kvm-unit-test framework.

tdcall.S contains two low level ABI functions:
__tdx_module_call and __tdx_hypercall.

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

Define a dummy is_tdx_guest() if TARGET_EFI is undefined as this function will be used globally in the future.

---
 lib/x86/asm/setup.h |   1 +
 lib/x86/setup.c     |  10 ++
 lib/x86/tdcall.S    | 303 ++++++++++++++++++++++++++++++++++++++++++++
 lib/x86/tdx.c       | 276 ++++++++++++++++++++++++++++++++++++++++
 lib/x86/tdx.h       |  76 +++++++++++
 x86/Makefile.common |   2 +
 6 files changed, 668 insertions(+)
 create mode 100644 lib/x86/tdcall.S
 create mode 100644 lib/x86/tdx.c
 create mode 100644 lib/x86/tdx.h
lib/x86/asm/setup.h +1 -0
index dbfb2a22bc1b..c467a2e94861 100644
@@ -15,5 +15,6 @@ unsigned long setup_tss(u8 *stacktop);
 efi_status_t setup_efi(efi_bootinfo_t *efi_bootinfo);
 void setup_5level_page_table(void);
 #endif /* TARGET_EFI */
+#include "x86/tdx.h"
 
 #endif /* _X86_ASM_SETUP_H_ */
lib/x86/setup.c +10 -0
index bbd34682b79e..fbcd188ebb8f 100644
@@ -283,6 +283,16 @@ efi_status_t setup_efi(efi_bootinfo_t *efi_bootinfo)
 	efi_status_t status;
 	const char *phase;
 
+	/*
+	 * TDVF support partial memory accept, accept remaining memory
+	 * early so memory allocator can use it.
+	 */
+	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/tdcall.S new file +303 -0

303 lines not on this page · raw message

lib/x86/tdx.c new file +276 -0

276 lines not on this page · raw message

lib/x86/tdx.h new file +76 -0

76 lines not on this page · raw message

x86/Makefile.common +2 -0
index ff02d9822321..8e2970b1cfc4 100644
@@ -26,6 +26,8 @@ ifeq ($(TARGET_EFI),y)
 cflatobjs += lib/x86/amd_sev.o
 cflatobjs += lib/efi.o
 cflatobjs += x86/efi/reloc_x86_64.o
+cflatobjs += lib/x86/tdcall.o
+cflatobjs += lib/x86/tdx.o
 endif
 
 OBJDIRS += lib/x86
Signature · 1 line
2.25.1
#2 lore raw

RFC PATCH 2/17 x86 TDX: Add #VE handler

In reply to Zhenzhong Duan

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      |  3 ++
 lib/x86/processor.h |  1 +
 lib/x86/tdx.c       | 87 +++++++++++++++++++++++++++++++++++++++++++++
 lib/x86/tdx.h       | 17 +++++++++
 4 files changed, 108 insertions(+)
lib/x86/desc.c +3 -0
index c2eb16e91fa1..b35274e44a8d 100644
@@ -112,6 +112,7 @@ const char* exception_mnemonic(int vector)
 	case 17: return "#AC";
 	case 18: return "#MC";
 	case 19: return "#XM";
+	case 20: return "#VE";
 	default: return "#??";
 	}
 }
@@ -227,6 +228,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"
@@ -273,6 +275,7 @@ static void *idt_handlers[32] = {
 	[17] = &ac_fault,
 	[18] = &mc_fault,
 	[19] = &xm_fault,
+	[20] = &ve_fault,
 	[21] = &cp_fault,
 };
 
lib/x86/processor.h +1 -0
index 117032a4895c..865269fd3857 100644
@@ -28,6 +28,7 @@
 #define GP_VECTOR 13
 #define PF_VECTOR 14
 #define AC_VECTOR 17
+#define VE_VECTOR 20
 #define CP_VECTOR 21
 
 #define X86_CR0_PE	0x00000001
lib/x86/tdx.c +87 -0

97 lines not on this page · raw message

lib/x86/tdx.h +17 -0
index 92ae5277b04d..68ddc136d1d9 100644
@@ -29,6 +29,9 @@
 #define EXIT_REASON_MSR_READ            31
 #define EXIT_REASON_MSR_WRITE           32
 
+/* TDX Module call Leaf IDs */
+#define TDX_GET_VEINFO			3
+
 /*
  * Used in __tdx_module_call() helper function to gather the
  * output registers' values of TDCALL instruction when requesting
@@ -59,6 +62,20 @@ struct tdx_hypercall_output {
 	u64 r15;
 };
 
+/*
+ * Used by #VE exception handler to gather the #VE exception
+ * info from the TDX module. This is software only structure
+ * and not related to TDX module/VMM.
+ */
+struct ve_info {
+	u64 exit_reason;
+	u64 exit_qual;
+	u64 gla;	/* Guest Linear (virtual) Address */
+	u64 gpa;	/* Guest Physical (virtual) Address */
+	u32 instr_len;
+	u32 instr_info;
+};
+
 bool is_tdx_guest(void);
 efi_status_t setup_tdx(void);
 
Signature · 1 line
2.25.1
#3 lore raw

RFC PATCH 3/17 x86 TDX: Bypass APIC and enable x2APIC directly

In reply to Zhenzhong Duan

According to TDX Architecture Specification, 9.8 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 | 10 +++++++---
 2 files changed, 11 insertions(+), 3 deletions(-)
lib/x86/apic.c +4 -0
index da8f30134b22..84bfe98c58ff 100644
@@ -147,6 +147,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);
+
         apic_ops = &x2apic_ops;
         return 1;
     } else {
lib/x86/setup.c +7 -3
index fbcd188ebb8f..e834fdfd290c 100644
@@ -108,8 +108,9 @@ unsigned long setup_tss(u8 *stacktop)
 {
 	u32 id;
 	tss64_t *tss_entry;
+	static u32 cpus = 0;
 
-	id = apic_id();
+	id = is_tdx_guest() ? id_map[cpus++] : apic_id();
 
 	/* Runtime address of current TSS */
 	tss_entry = &tss[id];
@@ -327,12 +328,15 @@ efi_status_t setup_efi(efi_bootinfo_t *efi_bootinfo)
 		return status;
 	}
 
-	reset_apic();
+	/* xAPIC mode isn't allowed in TDX */
+	if (!is_tdx_guest())
+		reset_apic();
 	setup_gdt_tss();
 	setup_idt();
 	load_idt();
 	mask_pic_interrupts();
-	enable_apic();
+	if (!is_tdx_guest())
+		enable_apic();
 	enable_x2apic();
 	smp_init();
 	setup_page_table();
Signature · 1 line
2.25.1
#4 lore raw

RFC PATCH 4/17 x86 TDX: Add exception table support

In reply to Zhenzhong Duan

Exception table is used to fixup from a faulty instruction execution.

In TDX scenario, some instructions trigger #VE and simulated through tdvmcall. If the simulation fail, the instruction is treated as faulty and should be checked with the exception table to fixup.

Move struct ex_record, exception_table_[start|end] in 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, 29 insertions(+), 7 deletions(-)
lib/x86/desc.c +0 -7
index b35274e44a8d..52eb4152385a 100644
@@ -84,13 +84,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
index ad6277ba600a..068ec2394df9 100644
@@ -212,6 +212,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);
 int write_cr4_checking(unsigned long val);
 unsigned exception_error_code(void);
lib/x86/tdx.c +23 -0
index 42ab25f47e57..62e0e2842822 100644
@@ -267,6 +267,22 @@ static bool tdx_handle_io(struct ex_regs *regs, u32 exit_qual)
 	return ret ? false : true;
 }
 
+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_output out;
@@ -298,10 +314,15 @@ static bool tdx_get_ve_info(struct ve_info *ve)
 static bool tdx_handle_virtualization_exception(struct ex_regs *regs,
 		struct ve_info *ve)
 {
+	unsigned int ex_val;
 	bool ret = true;
 	u64 val = ~0ULL;
 	bool do_sti;
 
+	/* #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:
 		do_sti = !!(regs->rflags & X86_EFLAGS_IF);
@@ -333,6 +354,8 @@ static bool tdx_handle_virtualization_exception(struct ex_regs *regs,
 	/* After successful #VE handling, move the IP */
 	if (ret)
 		regs->rip += ve->instr_len;
+	else
+		ret = tdx_check_exception_table(regs);
 
 	return ret;
 }
Signature · 1 line
2.25.1
#5 lore raw

RFC PATCH 5/17 x86 TDX: bypass wrmsr simulation on some specific MSRs

In reply to Zhenzhong Duan

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 access 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 | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
lib/x86/tdx.c +14 -1
index 62e0e2842822..1fc8030c34fa 100644
@@ -311,6 +311,18 @@ static bool tdx_get_ve_info(struct ve_info *ve)
 	return true;
 }
 
+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 bool tdx_handle_virtualization_exception(struct ex_regs *regs,
 		struct ve_info *ve)
 {
@@ -338,7 +350,8 @@ static bool tdx_handle_virtualization_exception(struct ex_regs *regs,
 		}
 		break;
 	case EXIT_REASON_MSR_WRITE:
-		ret = tdx_write_msr(regs->rcx, regs->rax, regs->rdx);
+		if (!tdx_is_bypassed_msr(regs->rcx))
+			ret = tdx_write_msr(regs->rcx, regs->rax, regs->rdx);
 		break;
 	case EXIT_REASON_CPUID:
 		ret = tdx_handle_cpuid(regs);
Signature · 1 line
2.25.1
#6 lore raw

RFC PATCH 6/17 x86 TDX: Simulate single step on #VE handled instruction

In reply to Zhenzhong Duan

According to TDX spec, specific instructions are simulated in #VE handler, such as cpuid(0xb) and wrmsr(0x1a0).

To avoid missing single step on these instructions, 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 | 4 ++++
 lib/x86/tdx.c  | 9 ++++++++-
 3 files changed, 12 insertions(+), 6 deletions(-)
lib/x86/desc.c +0 -5
index 52eb4152385a..78f4b6576888 100644
@@ -51,11 +51,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);
-
 void set_idt_entry(int vec, void *addr, int dpl)
 {
     idt_entry_t *e = &boot_idt[vec];
lib/x86/desc.h +4 -0
index 068ec2394df9..2cd819574374 100644
@@ -222,6 +222,10 @@ unsigned exception_vector(void);
 int write_cr4_checking(unsigned long val);
 unsigned exception_error_code(void);
 bool exception_rflags_rf(void);
+#ifndef __x86_64__
+__attribute__((regparm(1)))
+#endif
+void do_handle_exception(struct ex_regs *regs);
 void set_idt_entry(int vec, void *addr, int dpl);
 void set_idt_sel(int vec, u16 sel);
 void set_idt_dpl(int vec, u16 dpl);
lib/x86/tdx.c +8 -1
index 1fc8030c34fa..2b2e3164be33 100644
@@ -365,8 +365,15 @@ static bool tdx_handle_virtualization_exception(struct ex_regs *regs,
 	}
 
 	/* After successful #VE handling, move the IP */
-	if (ret)
+	if (ret) {
 		regs->rip += ve->instr_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);
+		}
+	}
 	else
 		ret = tdx_check_exception_table(regs);
 
Signature · 1 line
2.25.1
#7 lore raw

RFC PATCH 7/17 x86 TDX: Extend EFI run script to support TDX

In reply to Zhenzhong Duan

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.

---
 x86/efi/README.md |  6 ++++++
 x86/efi/run       | 19 +++++++++++++++++++
 2 files changed, 25 insertions(+)
x86/efi/README.md +6 -0
index a39f509cd9aa..b6f1fc68b0f3 100644
@@ -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
index ac368a59ba9f..2af0a303ea0e 100755
@@ -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" \
+	-device loader,file="$EFI_UEFI",id=fd0 \
+	-object tdx-guest,id=tdx0 \
+	-machine q35,kvm-type=tdx,pic=no,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
#8 lore raw

RFC PATCH 8/17 x86 TDX: Add support for memory accept

In reply to Zhenzhong Duan

TDVF supports partial memory accept to optimize boot time and leaves remaining memory accept to OS.

Accept remaining memory of EFI_UNACCEPTED_MEMORY type at bootup. Try 2M 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_hcall_gpa_intent()
tdx_accept_memory()

---
 lib/asm-generic/page.h |   7 ++-
 lib/linux/efi.h        |  23 ++++++-
 lib/x86/setup.c        |   2 +-
 lib/x86/tdx.c          | 139 ++++++++++++++++++++++++++++++++++++++++-
 lib/x86/tdx.h          |  20 +++++-
 5 files changed, 185 insertions(+), 6 deletions(-)
lib/asm-generic/page.h +5 -2
index 5ed086129657..56f539ac45eb 100644
@@ -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
index 455625aa155d..df9fa7974d87 100644
@@ -96,7 +96,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 */
@@ -416,6 +417,26 @@ struct efi_boot_memmap {
 	unsigned long           *buff_size;
 };
 
+/*
+ * 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/setup.c +1 -1
index e834fdfd290c..29202478ae0d 100644
@@ -288,7 +288,7 @@ efi_status_t setup_efi(efi_bootinfo_t *efi_bootinfo)
 	 * TDVF support partial memory accept, accept remaining memory
 	 * early so memory allocator can use it.
 	 */
-	status = setup_tdx();
+	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 +138 -1

164 lines not on this page · raw message

lib/x86/tdx.h +19 -1
index 68ddc136d1d9..2f938038dc45 100644
@@ -30,7 +30,12 @@
 #define EXIT_REASON_MSR_WRITE           32
 
 /* TDX Module call Leaf IDs */
+#define TDX_GET_INFO			1
 #define TDX_GET_VEINFO			3
+#define TDX_ACCEPT_PAGE			6
+
+/* TDX hypercall Leaf IDs */
+#define TDVMCALL_MAP_GPA		0x10001
 
 /*
  * Used in __tdx_module_call() helper function to gather the
@@ -76,8 +81,21 @@ 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,
+};
+
 bool is_tdx_guest(void);
-efi_status_t setup_tdx(void);
+phys_addr_t tdx_shared_mask(void);
+int tdx_hcall_gpa_intent(phys_addr_t start, phys_addr_t end,
+			 enum tdx_map_type map_type);
+bool tdx_accept_memory(phys_addr_t start, phys_addr_t end);
+efi_status_t setup_tdx(efi_bootinfo_t *efi_bootinfo);
 
 /* Helper function used to communicate with the TDX module */
 u64 __tdx_module_call(u64 fn, u64 rcx, u64 rdx, u64 r8, u64 r9,
Signature · 1 line
2.25.1
#9 lore raw

RFC PATCH 9/17 acpi: Add MADT table parse code

In reply to Zhenzhong Duan

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/x86/acpi.c  | 171 ++++++++++++++++++++++++++++++++++++++++++++++++
 lib/x86/acpi.h  |  85 ++++++++++++++++++++++++
 lib/x86/setup.c |   4 ++
 3 files changed, 260 insertions(+)
lib/x86/acpi.c +171 -0
index 1a82ced0b90f..6fb8ece7eabe 100644
@@ -1,7 +1,12 @@
 #include "libcflat.h"
+#include "errno.h"
 #include "acpi.h"
+#include "apic.h"
+#include "asm/barrier.h"
+#include "processor.h"
 
 #ifdef TARGET_EFI
+unsigned char online_cpus[(MAX_TEST_CPUS + 7) / 8];
 struct rsdp_descriptor *efi_rsdp = NULL;
 
 void set_efi_rsdp(struct rsdp_descriptor *rsdp)
@@ -16,6 +21,172 @@ static struct rsdp_descriptor *get_rsdp(void)
 	}
 	return efi_rsdp;
 }
+
+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)
+{
+	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, online_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 (timeout) {
+		/*
+		 * If the CPU wakeup process is successful, store the
+		 * status in online_cpus to prevent re-wakeup
+		 * requests.
+		 */
+		set_bit(apicid, online_cpus);
+		return 0;
+	}
+
+	/* If timed out (timeout == 0), return error */
+	return -EIO;
+}
+
+static bool parse_madt_table(struct acpi_table *madt)
+{
+	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(void)
+{
+	struct acpi_table *tb;
+
+	tb = find_acpi_table_addr(MADT_SIGNATURE);
+	if (tb)
+		return parse_madt_table(tb);
+
+	return false;
+}
 #else
 static struct rsdp_descriptor *get_rsdp(void)
 {
lib/x86/acpi.h +85 -0

102 lines not on this page · raw message

lib/x86/setup.c +4 -0
index 29202478ae0d..63c4dbb25064 100644
@@ -314,6 +314,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())
+		return EFI_NOT_FOUND;
+
 	phase = "AMD SEV";
 	status = setup_amd_sev();
 
Signature · 1 line
2.25.1
#10 lore raw

RFC PATCH 10/17 x86 TDX: Add multi processor support

In reply to Zhenzhong Duan

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.

Though it is targeting TDX MP support, there are consideration about integration with normal UEFI MP support in the future.

---
 lib/x86/asm/setup.h       |  1 +
 lib/x86/setup.c           | 31 +++++++++++++++++++++++++++++++
 lib/x86/tdx.c             | 33 +++++++++++++++++++++++++++++++++
 lib/x86/tdx.h             |  2 ++
 x86/efi/crt0-efi-x86_64.S | 12 +++++++++++-
 x86/efi/efistart64.S      |  5 +++++
 6 files changed, 83 insertions(+), 1 deletion(-)
lib/x86/asm/setup.h +1 -0
index c467a2e94861..5e7aa2eb4332 100644
@@ -14,6 +14,7 @@ unsigned long setup_tss(u8 *stacktop);
 
 efi_status_t setup_efi(efi_bootinfo_t *efi_bootinfo);
 void setup_5level_page_table(void);
+void secondary_startup_64(void);
 #endif /* TARGET_EFI */
 #include "x86/tdx.h"
 
lib/x86/setup.c +31 -0
index 63c4dbb25064..7bf5d431f2a8 100644
@@ -279,6 +279,35 @@ static void setup_gdt_tss(void)
 	load_gdt_tss(tss_offset);
 }
 
+static void setup_percpu_area(void)
+{
+	u64 rsp;
+
+	asm volatile ("mov %%rsp, %0" : "=m"(rsp) :: "memory");
+
+	/* per cpu stack size is PAGE_SIZE */
+	rsp &= ~((u64)PAGE_SIZE - 1);
+	wrmsr(MSR_GS_BASE, rsp);
+}
+
+void secondary_startup_64(void)
+{
+	setup_gdt_tss();
+	load_idt();
+	setup_percpu_area();
+	enable_x2apic();
+	tdx_ap_init();
+
+	while (1)
+		safe_halt();
+}
+
+static void aps_init(void)
+{
+	if (is_tdx_guest())
+		tdx_aps_init();
+}
+
 efi_status_t setup_efi(efi_bootinfo_t *efi_bootinfo)
 {
 	efi_status_t status;
@@ -332,6 +361,7 @@ efi_status_t setup_efi(efi_bootinfo_t *efi_bootinfo)
 		return status;
 	}
 
+	setup_percpu_area();
 	/* xAPIC mode isn't allowed in TDX */
 	if (!is_tdx_guest())
 		reset_apic();
@@ -342,6 +372,7 @@ efi_status_t setup_efi(efi_bootinfo_t *efi_bootinfo)
 	if (!is_tdx_guest())
 		enable_apic();
 	enable_x2apic();
+	aps_init();
 	smp_init();
 	setup_page_table();
 
lib/x86/tdx.c +33 -0
index b74c697353d9..4bd658b95028 100644
@@ -12,9 +12,14 @@
 #include "tdx.h"
 #include "errno.h"
 #include "bitops.h"
+#include "atomic.h"
+#include "fwcfg.h"
+#include "x86/acpi.h"
 #include "x86/processor.h"
 #include "x86/smp.h"
+#include "x86/apic.h"
 #include "asm/page.h"
+#include "asm/barrier.h"
 
 #define VE_IS_IO_OUT(exit_qual)		(((exit_qual) & 8) ? 0 : 1)
 #define VE_GET_IO_SIZE(exit_qual)	(((exit_qual) & 7) + 1)
@@ -541,3 +546,31 @@ efi_status_t setup_tdx(efi_bootinfo_t *efi_bootinfo)
 
 	return EFI_SUCCESS;
 }
+
+static atomic_t cpu_online_count = {1};
+extern unsigned char online_cpus[(MAX_TEST_CPUS + 7) / 8];
+extern void ap_start64(void);
+
+/* TDX uses ACPI WAKE UP mechanism to wake up APs instead of SIPI */
+efi_status_t tdx_aps_init(void)
+{
+	u32 i, total_cpus = fwcfg_get_nb_cpus();
+
+	/* BSP is already online */
+	set_bit(id_map[0], online_cpus);
+
+	for (i = 1; i < total_cpus; i++) {
+		if (acpi_wakeup_cpu(id_map[i], (u64)ap_start64))
+			return EFI_DEVICE_ERROR;
+	}
+
+	while (atomic_read(&cpu_online_count) != total_cpus)
+		cpu_relax();
+
+	return EFI_SUCCESS;
+}
+
+void tdx_ap_init(void)
+{
+	atomic_inc(&cpu_online_count);
+}
lib/x86/tdx.h +2 -0
index 2f938038dc45..4b75fcec7367 100644
@@ -96,6 +96,8 @@ int tdx_hcall_gpa_intent(phys_addr_t start, phys_addr_t end,
 			 enum tdx_map_type map_type);
 bool tdx_accept_memory(phys_addr_t start, phys_addr_t end);
 efi_status_t setup_tdx(efi_bootinfo_t *efi_bootinfo);
+efi_status_t tdx_aps_init(void);
+void tdx_ap_init(void);
 
 /* Helper function used to communicate with the TDX module */
 u64 __tdx_module_call(u64 fn, u64 rcx, u64 rdx, u64 r8, u64 r9,
x86/efi/crt0-efi-x86_64.S +11 -1
index eaf165649591..f3309f6e35ef 100644
@@ -41,6 +41,8 @@
 
 	.globl _start
 _start:
+	lea stacktop(%rip), %rsp
+	add %rsp, smp_stacktop(%rip)
 	subq $8, %rsp
 	pushq %rcx
 	pushq %rdx
@@ -61,12 +63,20 @@ _start:
 	call efi_main
 	addq $8, %rsp
 
+	.globl ap_start64
+ap_start64:
+	mov $-PAGE_SIZE, %rsp
+	lock xadd %rsp, smp_stacktop(%rip)
+	call secondary_startup_64
+
 .exit:	
   	ret
 
+	.data
+smp_stacktop:  .quad	-PAGE_SIZE
+
  	// hand-craft a dummy .reloc section so EFI knows it's a relocatable executable:
  
- 	.data
 dummy:	.long	0
 
 #define IMAGE_REL_ABSOLUTE	0
x86/efi/efistart64.S +5 -0
index 017abba85a68..648d047febb5 100644
@@ -22,6 +22,11 @@ ptl4:
 	. = . + PAGE_SIZE
 .align PAGE_SIZE
 
+.globl stacktop
+	. = . + PAGE_SIZE * MAX_TEST_CPUS
+stacktop:
+.align PAGE_SIZE
+
 .section .init
 .code64
 .text
Signature · 1 line
2.25.1
#11 lore raw

RFC PATCH 11/17 x86 TDX: Add a formal IPI handler

In reply to Zhenzhong Duan

Current IPI handler may currupts 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 currupt #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
index 2ac0ef74f264..8a37143c6d78 100644
@@ -39,12 +39,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
#12 lore raw

RFC PATCH 12/17 x86 TDX: Enable lvl5 boot page table

In reply to Zhenzhong Duan

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.

Move setup_page_table() before APs startup so that lvl5 page table is ready for APs. Refactor the common part of setting cr3 in a wrapper function load_page_table().

---
 lib/x86/setup.c      | 22 +++++++++++++++++++---
 x86/efi/efistart64.S |  5 +++++
 2 files changed, 24 insertions(+), 3 deletions(-)
lib/x86/setup.c +19 -3
index 7bf5d431f2a8..3a60762494d6 100644
@@ -231,10 +231,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;
@@ -247,6 +260,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;
@@ -266,8 +282,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();
 }
 
 static void setup_gdt_tss(void)
@@ -297,6 +312,7 @@ void secondary_startup_64(void)
 	setup_percpu_area();
 	enable_x2apic();
 	tdx_ap_init();
+	load_page_table();
 
 	while (1)
 		safe_halt();
@@ -372,9 +388,9 @@ efi_status_t setup_efi(efi_bootinfo_t *efi_bootinfo)
 	if (!is_tdx_guest())
 		enable_apic();
 	enable_x2apic();
+	setup_page_table();
 	aps_init();
 	smp_init();
-	setup_page_table();
 
 	return EFI_SUCCESS;
 }
x86/efi/efistart64.S +5 -0
index 648d047febb5..ef3db0110c3c 100644
@@ -22,6 +22,11 @@ ptl4:
 	. = . + PAGE_SIZE
 .align PAGE_SIZE
 
+.globl ptl5
+ptl5:
+	. = . + PAGE_SIZE
+.align PAGE_SIZE
+
 .globl stacktop
 	. = . + PAGE_SIZE * MAX_TEST_CPUS
 stacktop:
Signature · 1 line
2.25.1
#13 lore raw

RFC PATCH 13/17 x86 TDX: Add lvl5 page table support to virtual memory

In reply to Zhenzhong Duan

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/processor.h |  1 +
 lib/x86/setup.c     |  5 +++++
 lib/x86/vm.c        | 14 ++++++++++++--
 3 files changed, 18 insertions(+), 2 deletions(-)
lib/x86/processor.h +1 -0
index 865269fd3857..4deff9ebe044 100644
@@ -35,6 +35,7 @@
 #define X86_CR0_MP	0x00000002
 #define X86_CR0_EM	0x00000004
 #define X86_CR0_TS	0x00000008
+#define X86_CR0_NE	0x00000020
 #define X86_CR0_WP	0x00010000
 #define X86_CR0_AM	0x00040000
 #define X86_CR0_NW	0x20000000
lib/x86/setup.c +5 -0
index 3a60762494d6..0c299d3dd9bc 100644
@@ -64,6 +64,11 @@ static struct mbi_bootinfo *bootinfo;
 #ifdef __x86_64__
 void find_highmem(void)
 {
+#ifdef TARGET_EFI
+	/* The largest free memory region is already chosen in setup_efi() */
+	return;
+#endif /* TARGET_EFI */
+
 	/* Memory above 4 GB is only supported on 64-bit systems.  */
 	if (!(bootinfo->flags & 64))
 	    	return;
lib/x86/vm.c +12 -2
index 56be57be673a..4ead6ed358ae 100644
@@ -3,6 +3,7 @@
 #include "vmalloc.h"
 #include "alloc_page.h"
 #include "smp.h"
+#include "tdx.h"
 
 static pteval_t pte_opt_mask;
 
@@ -16,7 +17,12 @@ pteval_t *install_pte(pgd_t *cr3,
     pteval_t *pt = cr3;
     unsigned offset;
 
-    for (level = PAGE_LEVEL; level > pte_level; --level) {
+    if (read_cr4() & X86_CR4_LA57)
+        level = 5;
+    else
+        level = PAGE_LEVEL;
+
+    for (; level > pte_level; --level) {
 	offset = PGDIR_OFFSET((uintptr_t)virt, level);
 	if (!(pt[offset] & PT_PRESENT_MASK)) {
 	    pteval_t *new_pt = pt_page;
@@ -187,7 +193,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
#14 lore raw

RFC PATCH 14/17 x86 TDX: Add TDX specific test case

In reply to Zhenzhong Duan

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) and wrmsr(0x1a0) to test. Please note not all cpuid trigger #VE, e.x. cpuid(0) will not.

---
 x86/Makefile.x86_64 |  1 +
 x86/intel_tdx.c     | 94 +++++++++++++++++++++++++++++++++++++++++++++
 x86/unittests.cfg   |  4 ++
 3 files changed, 99 insertions(+)
 create mode 100644 x86/intel_tdx.c
x86/Makefile.x86_64 +1 -0
index a3cb75ae5868..de79212951a3 100644
@@ -31,6 +31,7 @@ tests += $(TEST_DIR)/vmware_backdoors.$(exe)
 tests += $(TEST_DIR)/rdpru.$(exe)
 tests += $(TEST_DIR)/pks.$(exe)
 tests += $(TEST_DIR)/pmu_lbr.$(exe)
+tests += $(TEST_DIR)/intel_tdx.$(exe)
 
 ifeq ($(TARGET_EFI),y)
 tests += $(TEST_DIR)/amd_sev.$(exe)
x86/intel_tdx.c new file +94 -0

94 lines not on this page · raw message

x86/unittests.cfg +4 -0
index 9a70ba3b4f2e..840e2054d54d 100644
@@ -437,3 +437,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
#15 lore raw

RFC PATCH 15/17 x86 TDX: bypass unsupported sub-test for TDX

In reply to Zhenzhong Duan

According to TDX Module V0.931 Table 18.2: MSR Virtualization:

1. MSR_IA32_MISC_ENABLE is reading native and #VE in writing.
2. MSR_CSTAR is #VE in reading/writing.

MSR_CSTAR simulation is also not supported in TDX host side.

That means changing those MSRs are unsupported. So bypass related sub-test.

---
 x86/msr.c     | 6 ++++++
 x86/syscall.c | 3 ++-
 2 files changed, 8 insertions(+), 1 deletion(-)
x86/msr.c +6 -0
index 44fbb3b233e9..3a538c9ba693 100644
@@ -4,6 +4,7 @@
 #include "processor.h"
 #include "msr.h"
 #include <stdlib.h>
+#include "tdx.h"
 
 /**
  * This test allows two modes:
@@ -89,6 +90,11 @@ static void test_rdmsr_fault(struct msr_info *msr)
 
 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, msr->value);
 
x86/syscall.c +2 -1
index b0df07200f50..270dfdfcce19 100644
@@ -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
#16 lore raw

RFC PATCH 16/17 x86 UEFI: Add support for parameter passing

In reply to Zhenzhong Duan

UEFI supports command line in unicode format, translate it into ascii format and pass to main() as parameters. Only support general characters with ascii < 0x80.

---
 lib/argv.c      |  2 +-
 lib/argv.h      |  1 +
 lib/efi.c       | 73 +++++++++++++++++++++++++++++++++++++++++++++++++
 lib/linux/efi.h | 18 ++++++++++++
 4 files changed, 93 insertions(+), 1 deletion(-)
lib/argv.c +1 -1
index 0312d74011d3..4d5c318a4bc4 100644
@@ -44,7 +44,7 @@ void __setup_args(void)
 	__argc = argv - __argv;
 }
 
-static void setup_args(const char *args)
+void setup_args(const char *args)
 {
 	if (!args)
 		return;
lib/argv.h +1 -0
index 1fd746dc2177..0fa7772549da 100644
@@ -9,6 +9,7 @@
 #define _ARGV_H_
 
 extern void __setup_args(void);
+extern void setup_args(const char *args);
 extern void setup_args_progname(const char *args);
 extern void setup_env(char *env, int size);
 extern void add_setup_arg(const char *arg);
lib/efi.c +73 -0

91 lines not on this page · raw message

lib/linux/efi.h +18 -0
index df9fa7974d87..8b9fa06f84ba 100644
@@ -59,6 +59,7 @@ typedef guid_t efi_guid_t;
 	(c) & 0xff, ((c) >> 8) & 0xff, d } }
 
 #define ACPI_TABLE_GUID EFI_GUID(0xeb9d2d30, 0x2d88, 0x11d3, 0x9a, 0x16, 0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d)
+#define LOADED_IMAGE_PROTOCOL_GUID EFI_GUID(0x5b1b31a1, 0x9562, 0x11d2,  0x8e, 0x3f, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b)
 
 typedef struct {
 	efi_guid_t guid;
@@ -417,6 +418,23 @@ struct efi_boot_memmap {
 	unsigned long           *buff_size;
 };
 
+#define __aligned_u64 u64 __attribute__((aligned(8)))
+typedef struct {
+	u32			revision;
+	efi_handle_t		parent_handle;
+	efi_system_table_t	*system_table;
+	efi_handle_t		device_handle;
+	void			*file_path;
+	void			*reserved;
+	u32			load_options_size;
+	void			*load_options;
+	void			*image_base;
+	__aligned_u64		image_size;
+	unsigned int		image_code_type;
+	unsigned int		image_data_type;
+	efi_status_t		(__efiapi *unload)(efi_handle_t image_handle);
+} efi_loaded_image_t;
+
 /*
  * efi_memdesc_ptr - get the n-th EFI memmap descriptor
  * @map: the start of efi memmap
Signature · 1 line
2.25.1
#17 lore raw

RFC PATCH 17/17 x86 TDX: Make run_tests.sh work with TDX

In reply to Zhenzhong Duan

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 | 18 +++++++++++++++++-
 2 files changed, 23 insertions(+), 1 deletion(-)
README.md +6 -0
index 6e82dc22570e..a84460e9f96b 100644
@@ -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 +17 -1
index 840e2054d54d..8cb32e6e7bee 100644
@@ -56,10 +56,12 @@ arch = i386
 [smptest]
 file = smptest.flat
 smp = 2
+groups = tdx
 
 [smptest3]
 file = smptest.flat
 smp = 3
+groups = tdx
 
 [vmexit_cpuid]
 file = vmexit.flat
@@ -155,6 +157,7 @@ file = hypercall.flat
 [idt_test]
 file = idt_test.flat
 arch = x86_64
+groups = tdx
 
 #[init]
 #file = init.flat
@@ -163,6 +166,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
@@ -171,6 +175,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
@@ -207,6 +212,7 @@ file = s3.flat
 
 [setjmp]
 file = setjmp.flat
+groups = tdx
 
 [sieve]
 file = sieve.flat
@@ -216,23 +222,28 @@ timeout = 180
 file = syscall.flat
 arch = x86_64
 extra_params = -cpu Opteron_G1,vendor=AuthenticAMD
+groups = tdx
 
 [tsc]
 file = tsc.flat
 extra_params = -cpu kvm64,+rdtscp
+groups = tdx
 
 [tsc_adjust]
 file = tsc_adjust.flat
 extra_params = -cpu max
+groups = tdx
 
 [xsave]
 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
@@ -259,7 +270,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
@@ -277,10 +288,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
@@ -393,6 +406,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
@@ -431,6 +445,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
@@ -441,3 +456,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