Skip to contentSkip to conversation

x86: Reset lapic after boot

PATCH Superseded Nadav Amit 2 participants 4 messages Jun 25, 2019 – Jun 27, 2019 KVM

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Nadav Amit

author
#0 lore raw

PATCH 1/1 x86: Reset lapic after boot

Do not assume that the local APIC is in a xAPIC mode after reset. Instead reset it first, since it might be in x2APIC mode, from which a transition in xAPIC is invalid.

Note that we do not use the existing disable_apic() for the matter, since it also re-initializes apic_ops.

---
 x86/cstart64.S | 11 +++++++++++
 1 file changed, 11 insertions(+)
x86/cstart64.S +11 -0
index 9791282..03726a6 100644
@@ -118,6 +118,15 @@ MSR_GS_BASE = 0xc0000101
 	wrmsr
 .endm
 
+lapic_reset:
+	mov $0x1b, %ecx
+	rdmsr
+	and $~(APIC_EN | APIC_EXTD), %eax
+	wrmsr
+	or $(APIC_EN), %eax
+	wrmsr
+	ret
+
 .macro setup_segments
 	mov $MSR_GS_BASE, %ecx
 	rdmsr
@@ -228,6 +237,7 @@ save_id:
 	retq
 
 ap_start64:
+	call lapic_reset
 	call load_tss
 	call enable_apic
 	call save_id
@@ -240,6 +250,7 @@ ap_start64:
 	jmp 1b
 
 start64:
+	call lapic_reset
 	call load_tss
 	call mask_pic_interrupts
 	call enable_apic
Signature · 1 line
2.17.1
#1 lore raw
Quoted 6 lines · Nadav Amit

Do not assume that the local APIC is in a xAPIC mode after reset. Instead reset it first, since it might be in x2APIC mode, from which a transition in xAPIC is invalid.

Note that we do not use the existing disable_apic() for the matter, since it also re-initializes apic_ops.

Is there any issue if apic_ops is reset ?

Quoted 11 lines
---
  x86/cstart64.S | 11 +++++++++++
  1 file changed, 11 insertions(+)
x86/cstart64.S +0 -0
index 9791282..03726a6 100644

x86/cstart64.S MSR_GS_BASE = 0xc0000101 · line 122

Quoted 5 lines
x86/cstart64.S +2 -0
@@ -118,6 +118,15 @@ MSR_GS_BASE = 0xc0000101
  	wrmsr
  .endm
 
+lapic_reset:
+	mov $0x1b, %ecx

Why not use MSR_IA32_APICBASE instead of 0x1b ?

Quoted 26 lines
x86/cstart64.S +9 -0
+	rdmsr
+	and $~(APIC_EN | APIC_EXTD), %eax
+	wrmsr
+	or $(APIC_EN), %eax
+	wrmsr
+	ret
+
  .macro setup_segments
  	mov $MSR_GS_BASE, %ecx
  	rdmsr
@@ -228,6 +237,7 @@ save_id:
  	retq
 
  ap_start64:
+	call lapic_reset
  	call load_tss
  	call enable_apic
  	call save_id
@@ -240,6 +250,7 @@ ap_start64:
  	jmp 1b
 
  start64:
+	call lapic_reset
  	call load_tss
  	call mask_pic_interrupts
  	call enable_apic
author
#2 lore raw
Quoted 20 lines · Krish Sadhukhan
Quoted 6 lines · Nadav Amit

Do not assume that the local APIC is in a xAPIC mode after reset. Instead reset it first, since it might be in x2APIC mode, from which a transition in xAPIC is invalid.

Note that we do not use the existing disable_apic() for the matter, since it also re-initializes apic_ops.

Is there any issue if apic_ops is reset ?

Quoted 5 lines
---
 x86/cstart64.S | 11 +++++++++++
 1 file changed, 11 insertions(+)
Quoted 12 lines

x86/cstart64.S MSR_GS_BASE = 0xc0000101 · line 121

Quoted 9 lines
x86/cstart64.S +1 -0
index 9791282..03726a6 100644
@@ -118,6 +118,15 @@ MSR_GS_BASE = 0xc0000101
 	wrmsr
 .endm
 +lapic_reset:
+	mov $0x1b, %ecx

Why not use MSR_IA32_APICBASE instead of 0x1b ?

I don’t remember, but it does require taking care of MSR_GS_BASE.

I can include “msr.h” and remove MSR_GS_BASE and MSR_IA32_APICBASE. I’ll add another patch to do so (since MSR_GS_BASE must be taken care of too).

author
#3 lore raw
In reply to Krish Sadhukhan
Quoted 8 lines · Krish Sadhukhan
Quoted 4 lines · Nadav Amit

Do not assume that the local APIC is in a xAPIC mode after reset. Instead reset it first, since it might be in x2APIC mode, from which a transition in xAPIC is invalid.

Quoted 5 lines
Quoted 2 lines

Note that we do not use the existing disable_apic() for the matter, since it also re-initializes apic_ops.

Is there any issue if apic_ops is reset ?

So I checked again, and actually the problem was different. Beforehand, I used reset_apic(), which used apic_ops to write to SPIV. And the race with setting x2apic caused it to occasionally use the x2APIC MSR interface to set SPIV, which triggered an exception.

I’ll send v2 that changes reset_apic() not to use apic_ops.

Thanks.