Skip to contentSkip to conversation

x86: Don't assume !x2APIC during boot

PATCH New Sean Christopherson 1 participant 3 messages Jul 25, 2022 KVM

lore
Share & tools
lore

Sean Christopherson

author reviewer
#0 lore raw

cover x86: Don't assume !x2APIC during boot

Get the "pre-boot" APIC ID via the x2APIC interface (RDMSR) if x2APIC is enabled (per APIC_BASE) instead of assuming xAPIC is enabled. UEFI SMP support[*] wants to use the pre-boot APIC ID helper to configure GS.base, i.e. before "resetting" the APIC to xAPIC mode. This causes problems for the SVM INIT-SIPI test, which sends APs back through the boot sequence without taking them out of x2APIC.

Posting as a separate series mostly because I don't want to respin the entire UEFI series, but also because it's not impossible that someone will want to run KUT with firmware that forces x2APIC mode.

[*] https://lore.kernel.org/all/[email protected]

Sean Christopherson (2):
x86: apic: Play nice with x2APIC being enabled when getting "pre-boot"
ID
x86: cstart64: Put APIC into xAPIC after loading TSS

 lib/x86/apic.c | 14 +++++++++-----
 x86/cstart64.S |  4 ++--
 2 files changed, 11 insertions(+), 7 deletions(-)

base-commit: 7b2e41767bb8caf91972ee32e4ca85ec630584e2

Signature · 1 line
2.37.1.359.gd136c6c3e2-goog
author reviewer
#1 lore raw

PATCH 1/2 x86: apic: Play nice with x2APIC being enabled when getting "pre-boot" ID

Retrieve the "pre-boot" APIC ID via the x2APIC interface if x2APIC is
enabled instead of assuming that the APIC is always in xAPIC mode. EFI
has a catch-22 where it needs the APID ID to initialize the per-vCPU GS.base, but calling reset_apic() = >disable_apic() needs GS.base to be correctly initialized in order to set the correct APIC ops. Play nice with either xAPIC or x2APIC so that EFI can be used for SMP tests, in particular the SVM INIT-SIPI tests which send APs back through the boot sequence while x2APIC is enabled.

Alternatively, disabling x2APIC and updating the APIC ops could be split, but there's no obvious advantage in doing so. Retrieving the pre-boot APIC ID isn't a hot path, i.e. the cost of the RDMSR is likely negligible, and letting callers force xAPIC without updating the ops isn't any less fragile.

---
 lib/x86/apic.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
lib/x86/apic.c +9 -5
index 5d4c776..eed93fa 100644
@@ -56,11 +56,6 @@ static uint32_t xapic_id(void)
 	return xapic_read(APIC_ID) >> 24;
 }
 
-uint32_t pre_boot_apic_id(void)
-{
-	return xapic_id();
-}
-
 static const struct apic_ops xapic_ops = {
 	.reg_read = xapic_read,
 	.reg_write = xapic_write,
@@ -165,6 +160,15 @@ int enable_x2apic(void)
 	}
 }
 
+uint32_t pre_boot_apic_id(void)
+{
+	u32 msr_lo, msr_hi;
+
+	asm ("rdmsr" : "=a"(msr_lo), "=d"(msr_hi) : "c"(MSR_IA32_APICBASE));
+
+	return (msr_lo & APIC_EXTD) ? x2apic_id() : xapic_id();
+}
+
 void disable_apic(void)
 {
 	wrmsr(MSR_IA32_APICBASE, rdmsr(MSR_IA32_APICBASE) & ~(APIC_EN | APIC_EXTD));
Signature · 1 line
2.37.1.359.gd136c6c3e2-goog
author reviewer
#2 lore raw

PATCH 2/2 x86: cstart64: Put APIC into xAPIC after loading TSS

In reply to Sean Christopherson

Now that pre_boot_apic_id() works with either xAPIC or x2APIC, "reset" the APIC after configuring loading the TSS. Previously, load_tss() => setup_tss() needed to run after forcing the vCPU into xAPIC mode due to pre_boot_apic_id() assuming xAPIC.

The order doesn't truly matter at this point, but loading the TSS first will allow sharing code with the EFI boot flow, which "needs" to load the TSS (more specifically, needs to configure GS.base) prior to forcing the vCPU into xAPIC (and thus setting the per-vCPU APIC ops).

---
 x86/cstart64.S | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
x86/cstart64.S +2 -2
index 7272452..5269424 100644
@@ -188,8 +188,8 @@ save_id:
 	retq
 
 ap_start64:
-	call reset_apic
 	load_tss
+	call reset_apic
 	call enable_apic
 	call save_id
 	call enable_x2apic
@@ -201,8 +201,8 @@ ap_start64:
 	jmp 1b
 
 start64:
-	call reset_apic
 	load_tss
+	call reset_apic
 	call mask_pic_interrupts
 	call enable_apic
 	call save_id
Signature · 1 line
2.37.1.359.gd136c6c3e2-goog