Skip to contentSkip to conversation

x86: FEP related cleanups and fix

PATCH Applied Sean Christopherson 1 participant 8 messages Jun 4, 2025 – Jun 10, 2025 KVM

lore
Share & tools
lore

Sean Christopherson

author reviewer
#0 lore raw

cover x86: FEP related cleanups and fix

The ultimate goal of this series is to be able to check for forced emulation support in a nVMX test that runs with a garbage IDT (the test currently assumed forced emulation is always available, which fails for obvious reasons).

Getting there is a bit annoying, mostly because the EFI path happens to load the IDT after setup_idt(). I _could_ have just tweaked the EFI path, but opted for a slightly bigger overhaul, e.g. so that it's easier to see that the BSP is responsible for loading the IDT, and so that setup_idt() can _guarantee_ it can handle a #UD without exploding.

Sean Christopherson (6):
x86: Call setup_idt() from start{32,64}(), not from smp_init()
x86: Drop protection against setup_idt() being called multiple times
x86: Move call to load_idt() out of setup_tr_and_percpu macro
x86: Load IDT on BSP as part of setup_idt()
x86: Cache availability of forced emulation during setup_idt()
nVMX: Force emulation of LGDT/LIDT in iff FEP is available

 lib/x86/desc.c   | 29 ++++++++++++++++++++++++-----
 lib/x86/desc.h   | 14 +-------------
 lib/x86/setup.c  |  1 -
 lib/x86/smp.c    |  1 -
 x86/access.c     |  2 +-
 x86/cstart.S     |  3 ++-
 x86/cstart64.S   |  2 +-
 x86/emulator.c   | 11 +++++------
 x86/emulator64.c |  2 +-
 x86/la57.c       |  2 +-
 x86/lam.c        |  2 +-
 x86/msr.c        |  2 +-
 x86/pmu.c        |  2 +-
 x86/vmx_tests.c  |  7 ++++---
 14 files changed, 43 insertions(+), 37 deletions(-)

base-commit: 72d110d8286baf1b355301cc8c8bdb42be2663fb

Signature · 1 line
2.49.0.1266.g31b7d2e469-goog
author reviewer
#1 lore raw

PATCH 1/6 x86: Call setup_idt() from start{32,64}(), not from smp_init()

Call setup_idt() from the (non-EFI) 32-bit and 64-bit BSP boot paths so that setup_idt() is called exactly once for all flavors. To be able to handle #VC, EFI calls setup_idt() early on, before smp_init().

This will allow moving the call to load_idt() into setup_idt(), without creating weirdness, which in turn will allow taking faults in setup_idt(), e.g. to probe for forced emulation support.

---
 lib/x86/smp.c  | 1 -
 x86/cstart.S   | 1 +
 x86/cstart64.S | 1 +
 3 files changed, 2 insertions(+), 1 deletion(-)
lib/x86/smp.c +0 -1
index 9706072a..366e184c 100644
@@ -146,7 +146,6 @@ void smp_init(void)
 	int i;
 	void ipi_entry(void);
 
-	setup_idt();
 	init_apic_map();
 	set_idt_entry(IPI_VECTOR, ipi_entry, 0);
 
x86/cstart.S +1 -0
index 96e79a47..ded6f91b 100644
@@ -109,6 +109,7 @@ ap_start32:
 
 start32:
 	setup_tr_and_percpu
+	call setup_idt
 	call reset_apic
 	call save_id
 	call mask_pic_interrupts
x86/cstart64.S +1 -0
index f3c398a5..9c1adad9 100644
@@ -106,6 +106,7 @@ gdt32_end:
 .code64
 start64:
 	call load_idt
+	call setup_idt
 	load_tss
 	call reset_apic
 	call mask_pic_interrupts
Signature · 1 line
2.49.0.1266.g31b7d2e469-goog
author reviewer
#2 lore raw

PATCH 2/6 x86: Drop protection against setup_idt() being called multiple times

In reply to Sean Christopherson

Now that setup_idt() is called exactly one for 32-bit, 64-bit, and EFI, drop the "do once" protection.

Long, long ago, setup_idt() was called by individual tests, and so the "do once" protection made a lot more sense. Now that (most) core setup has been moved to the BSP's boot path, playing nice with calling setup_idt() multiple times doesn't make any sense.

---
 lib/x86/desc.c | 5 -----
 1 file changed, 5 deletions(-)
lib/x86/desc.c +0 -5
index acfada26..bf6c62bc 100644
@@ -301,12 +301,7 @@ static void *idt_handlers[32] = {
 void setup_idt(void)
 {
 	int i;
-	static bool idt_initialized = false;
 
-	if (idt_initialized)
-		return;
-
-	idt_initialized = true;
 	for (i = 0; i < 32; i++) {
 		if (!idt_handlers[i])
 			continue;
Signature · 1 line
2.49.0.1266.g31b7d2e469-goog
author reviewer
#3 lore raw

PATCH 3/6 x86: Move call to load_idt() out of setup_tr_and_percpu macro

In reply to Sean Christopherson

Move the call to load_idt() out of setup_tr_and_percpu and into the two "callers" of the macro. This will allow moving the BSP's invocation of load_idt() into setup_idt(), without creating a confusing scenario where load_idt() is called multiple times, e.g. so that it's clear the BSP is responsible for configuring the IDT.

No functional change intended.

---
 x86/cstart.S | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
x86/cstart.S +2 -1
index ded6f91b..0229627a 100644
@@ -36,7 +36,6 @@ mb_flags = 0x0
 mb_cmdline = 16
 
 .macro setup_tr_and_percpu
-	call load_idt
 	push %esp
 	call setup_tss
 	addl $4, %esp
@@ -97,6 +96,7 @@ ap_start32:
 	mov $-per_cpu_size, %esp
 	lock xaddl %esp, smp_stacktop
 	setup_tr_and_percpu
+	call load_idt
 	call prepare_32
 	call reset_apic
 	call save_id
@@ -109,6 +109,7 @@ ap_start32:
 
 start32:
 	setup_tr_and_percpu
+	call load_idt
 	call setup_idt
 	call reset_apic
 	call save_id
Signature · 1 line
2.49.0.1266.g31b7d2e469-goog
author reviewer
#4 lore raw

PATCH 4/6 x86: Load IDT on BSP as part of setup_idt()

In reply to Sean Christopherson

Load the IDT on the BSP as part of setup_idt(), to guarantee that the IDT is loaded when setup_idt() runs (currently, the EFI boot path loads the IDT _after_ setup_idt(). This will allow probing for forced emulation, which requires being able to handle a #UD, during setup_idt().

---
 lib/x86/desc.c  | 2 ++
 lib/x86/setup.c | 1 -
 x86/cstart.S    | 1 -
 x86/cstart64.S  | 1 -
 4 files changed, 2 insertions(+), 3 deletions(-)
lib/x86/desc.c +2 -0
index bf6c62bc..5748f900 100644
@@ -309,6 +309,8 @@ void setup_idt(void)
                 set_idt_entry(i, idt_handlers[i], 0);
                 handle_exception(i, check_exception_table);
 	}
+
+	load_idt();
 }
 
 void load_idt(void)
lib/x86/setup.c +0 -1
index b4b7fec0..122f0af3 100644
@@ -345,7 +345,6 @@ efi_status_t setup_efi(efi_bootinfo_t *efi_bootinfo)
 	setup_gdt_tss();
 	setup_segments64();
 	setup_idt();
-	load_idt();
 	/*
 	 * Load GS.base with the per-vCPU data.  This must be done after
 	 * loading the IDT as reading the APIC ID may #VC when running
x86/cstart.S +0 -1
index 0229627a..8fb7bdef 100644
@@ -109,7 +109,6 @@ ap_start32:
 
 start32:
 	setup_tr_and_percpu
-	call load_idt
 	call setup_idt
 	call reset_apic
 	call save_id
x86/cstart64.S +0 -1
index 9c1adad9..3e332c88 100644
@@ -105,7 +105,6 @@ gdt32_end:
 
 .code64
 start64:
-	call load_idt
 	call setup_idt
 	load_tss
 	call reset_apic
Signature · 1 line
2.49.0.1266.g31b7d2e469-goog
author reviewer
#5 lore raw

PATCH 5/6 x86: Cache availability of forced emulation during setup_idt()

In reply to Sean Christopherson

Cache whether or not forced emulation is availability during setup_idt() so that tests can force emulation (or not) in contexts where taking an exception of any kind will fail.

---
 lib/x86/desc.c   | 22 ++++++++++++++++++++++
 lib/x86/desc.h   | 14 +-------------
 x86/access.c     |  2 +-
 x86/emulator.c   | 11 +++++------
 x86/emulator64.c |  2 +-
 x86/la57.c       |  2 +-
 x86/lam.c        |  2 +-
 x86/msr.c        |  2 +-
 x86/pmu.c        |  2 +-
 9 files changed, 34 insertions(+), 25 deletions(-)
lib/x86/desc.c +22 -0
index 5748f900..fca37b9a 100644
@@ -298,6 +298,22 @@ static void *idt_handlers[32] = {
 	[21] = &cp_fault,
 };
 
+bool is_fep_available;
+
+static bool __is_fep_available(void)
+{
+	/*
+	 * Use the non-FEP ASM_TRY() as KVM will inject a #UD on the prefix
+	 * itself if forced emulation is not available.
+	 */
+	asm goto(ASM_TRY("%l[fep_unavailable]")
+		 KVM_FEP "nop\n\t"
+		 ::: "memory" : fep_unavailable);
+	return true;
+fep_unavailable:
+	return false;
+}
+
 void setup_idt(void)
 {
 	int i;
@@ -311,6 +327,12 @@ void setup_idt(void)
 	}
 
 	load_idt();
+
+	/*
+	 * Detect support for forced emulation *after* loading the IDT, as this
+	 * will #UD if FEP is unavailable.
+	 */
+	is_fep_available = __is_fep_available();
 }
 
 void load_idt(void)
lib/x86/desc.h +1 -13
index a1e60e78..68f38f3d 100644
@@ -164,19 +164,7 @@ typedef struct  __attribute__((packed)) {
 #define KVM_FEP "ud2; .byte 'k', 'v', 'm';"
 #define ASM_TRY_FEP(catch) __ASM_TRY(KVM_FEP, catch)
 
-static inline bool is_fep_available(void)
-{
-	/*
-	 * Use the non-FEP ASM_TRY() as KVM will inject a #UD on the prefix
-	 * itself if forced emulation is not available.
-	 */
-	asm goto(ASM_TRY("%l[fep_unavailable]")
-		 KVM_FEP "nop\n\t"
-		 ::: "memory" : fep_unavailable);
-	return true;
-fep_unavailable:
-	return false;
-}
+extern bool is_fep_available;
 
 typedef struct {
 	unsigned short offset0;
x86/access.c +1 -1
index f90a72d6..d94910bf 100644
@@ -1232,7 +1232,7 @@ void ac_test_run(int pt_levels, bool force_emulation)
 	ac_pt_env_t pt_env;
 	int i, tests, successes;
 
-	if (force_emulation && !is_fep_available()) {
+	if (force_emulation && !is_fep_available) {
 		report_skip("Forced emulation prefix (FEP) not available\n");
 		return;
 	}
x86/emulator.c +5 -6
index f8bdc26b..4e1ba12a 100644
@@ -728,7 +728,6 @@ static void handle_db(struct ex_regs *regs)
 static void test_mov_pop_ss_code_db(void)
 {
 	handler old_db_handler = handle_exception(DB_VECTOR, handle_db);
-	bool fep_available = is_fep_available();
 	/* On Intel, code #DBs are inhibited when MOV/POP SS blocking is active. */
 	int nr_expected = is_intel() ? 0 : 1;
 
@@ -761,7 +760,7 @@ static void test_mov_pop_ss_code_db(void)
 		      "mov %%ss, %0\n\t", "mov %0, %%ss\n\t")
 
 	MOV_SS_DB("no fep", "", "");
-	if (fep_available) {
+	if (is_fep_available) {
 		MOV_SS_DB("fep MOV-SS", KVM_FEP, "");
 		MOV_SS_DB("fep XOR", "", KVM_FEP);
 		MOV_SS_DB("fep MOV-SS/fep XOR", KVM_FEP, KVM_FEP);
@@ -774,7 +773,7 @@ static void test_mov_pop_ss_code_db(void)
 		      "push %%ss\n\t", "pop %%ss\n\t")
 
 	POP_SS_DB("no fep", "", "");
-	if (fep_available) {
+	if (is_fep_available) {
 		POP_SS_DB("fep POP-SS", KVM_FEP, "");
 		POP_SS_DB("fep XOR", "", KVM_FEP);
 		POP_SS_DB("fep POP-SS/fep XOR", KVM_FEP, KVM_FEP);
@@ -791,8 +790,8 @@ int main(void)
 	void *mem;
 	void *cross_mem;
 
-	if (!is_fep_available())
-		report_skip("Skipping tests the require forced emulation, "
+	if (!is_fep_available)
+		report_skip("Skipping tests that require forced emulation, "
 			    "use kvm.force_emulation_prefix=1 to enable");
 
 	setup_vm();
@@ -821,7 +820,7 @@ int main(void)
 	//test_lldt(mem);
 	test_ltr(mem);
 
-	if (is_fep_available()) {
+	if (is_fep_available) {
 		test_smsw_reg(mem);
 		test_nop(mem);
 		test_mov_dr(mem);
x86/emulator64.c +1 -1
index 5d1bb0f0..138903af 100644
@@ -478,7 +478,7 @@ static void test_emulator_64(void *mem)
 	test_sreg(mem);
 	test_cmov(mem);
 
-	if (is_fep_available()) {
+	if (is_fep_available) {
 		test_mmx_movq_mf(mem);
 		test_movabs(mem);
 		test_user_load_dpl0_seg();
x86/la57.c +1 -1
index 41764110..d93e286c 100644
@@ -313,7 +313,7 @@ static void test_canonical_checks(void)
 {
 	__test_canonical_checks(false);
 
-	if (is_fep_available())
+	if (is_fep_available)
 		__test_canonical_checks(true);
 	else
 		report_skip("Force emulation prefix not enabled");
x86/lam.c +1 -1
index a1c98949..8ad68178 100644
@@ -184,7 +184,7 @@ static void __test_lam_sup(void *vaddr, void *vaddr_mmio)
 	test_ptr(vaddr_mmio, true);
 	test_invpcid(vaddr);
 	test_invlpg(vaddr, false);
-	if (is_fep_available())
+	if (is_fep_available)
 		test_invlpg(vaddr, true);
 }
 
x86/msr.c +1 -1
index e21ff0ac..f582a584 100644
@@ -313,7 +313,7 @@ static void test_cmd_msrs(void)
 		test_wrmsr_fault(MSR_IA32_FLUSH_CMD, "FLUSH_CMD", L1D_FLUSH);
 	}
 
-	if (is_fep_available()) {
+	if (is_fep_available) {
 		for (i = 1; i < 64; i++)
 			test_wrmsr_fep_fault(MSR_IA32_FLUSH_CMD, "FLUSH_CMD", BIT_ULL(i));
 	}
x86/pmu.c +1 -1
index 8cf26b12..b794b7d8 100644
@@ -823,7 +823,7 @@ static void warm_up(void)
 
 static void check_counters(void)
 {
-	if (is_fep_available())
+	if (is_fep_available)
 		check_emulated_instr();
 
 	warm_up();
Signature · 1 line
2.49.0.1266.g31b7d2e469-goog
author reviewer
#6 lore raw

PATCH 6/6 nVMX: Force emulation of LGDT/LIDT in iff FEP is available

In reply to Sean Christopherson

Force emulation of LGDT/LIDT in the canonical host values test if and only if forced emulation is available, otherwise the resulting #UD will result in failures.

Note, use of the cached is_fep_available is critical for the LIDT test as the "slow" check for FEP support requires being able to eat a #UD, i.e. would fail when the current IDT is garbage (loaded with one of the arbitrary canonical test values).

---
 x86/vmx_tests.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
x86/vmx_tests.c +4 -3
index 2f178227..2cc6d5a5 100644
@@ -10881,12 +10881,13 @@ static int set_host_value(u64 vmcs_field, u64 value)
 	case HOST_BASE_GDTR:
 		sgdt(&dt_ptr);
 		dt_ptr.base = value;
-		lgdt(&dt_ptr);
-		return lgdt_fep_safe(&dt_ptr);
+		return is_fep_available ? lgdt_fep_safe(&dt_ptr) :
+					  lgdt_safe(&dt_ptr);
 	case HOST_BASE_IDTR:
 		sidt(&dt_ptr);
 		dt_ptr.base = value;
-		return lidt_fep_safe(&dt_ptr);
+		return is_fep_available ? lidt_fep_safe(&dt_ptr) :
+					  lidt_safe(&dt_ptr);
 	case HOST_BASE_TR:
 		/* Set the base and clear the busy bit */
 		set_gdt_entry(FIRST_SPARE_SEL, value, 0x200, 0x89, 0);
Signature · 1 line
2.49.0.1266.g31b7d2e469-goog
author reviewer Applied
#7 lore raw
In reply to Sean Christopherson
Quoted 11 lines · Sean Christopherson

The ultimate goal of this series is to be able to check for forced emulation support in a nVMX test that runs with a garbage IDT (the test currently assumed forced emulation is always available, which fails for obvious reasons).

Getting there is a bit annoying, mostly because the EFI path happens to load the IDT after setup_idt(). I _could_ have just tweaked the EFI path, but opted for a slightly bigger overhaul, e.g. so that it's easier to see that the BSP is responsible for loading the IDT, and so that setup_idt() can _guarantee_ it can handle a #UD without exploding.

[...]

Applied to kvm-x86 next, thanks!

[1/6] x86: Call setup_idt() from start{32,64}(), not from smp_init()

      https://github.com/kvm-x86/kvm-unit-tests/commit/588887078688
[2/6] x86: Drop protection against setup_idt() being called multiple times
      https://github.com/kvm-x86/kvm-unit-tests/commit/3fcc85172af3
[3/6] x86: Move call to load_idt() out of setup_tr_and_percpu macro
      https://github.com/kvm-x86/kvm-unit-tests/commit/27fe927996be
[4/6] x86: Load IDT on BSP as part of setup_idt()
      https://github.com/kvm-x86/kvm-unit-tests/commit/63f9b9871322
[5/6] x86: Cache availability of forced emulation during setup_idt()
      https://github.com/kvm-x86/kvm-unit-tests/commit/486a097c9842
[6/6] nVMX: Force emulation of LGDT/LIDT in iff FEP is available
      https://github.com/kvm-x86/kvm-unit-tests/commit/3d677db42e66

--
https://github.com/kvm-x86/kvm-unit-tests/tree/next