Skip to contentSkip to conversation

x86/efi: Avoid triple faults during EFI mixed mode calls

PATCH In review Matt Fleming 2 participants 2 messages Jan 27, 2015 – Feb 3, 2015

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

PATCH 1/1 x86/efi: Avoid triple faults during EFI mixed mode calls

From: Matt Fleming <[email protected]>

Andy pointed out that if an NMI or MCE is received while we're in the middle of an EFI mixed mode call a triple fault will occur. This can happen, for example, when issuing an EFI mixed mode call while running perf.

The reason for the triple fault is that we execute the mixed mode call in 32-bit mode with paging disabled but with 64-bit kernel IDT handlers installed throughout the call.

At Andy's suggestion, stop playing the games we currently do at runtime, such as disabling paging and installing a 32-bit GDT for __KERNEL_CS. We can simply switch to the __KERNEL32_CS descriptor before invoking firmware services, and run in compatibility mode. This way, if an NMI/MCE does occur the kernel IDT handler will execute correctly, since it'll jump to __KERNEL_CS automatically.

However, this change is only possible post-ExitBootServices(). Before then the firmware "owns" the machine and expects for its 32-bit IDT handlers to be left intact to service interrupts, etc.

So, we now need to distinguish between early boot and runtime invocations of EFI services. During early boot, we need to restore the GDT that the firmware expects to be present. We can only jump to the __KERNEL32_CS code segment for mixed mode calls after ExitBootServices() has been invoked.

A liberal sprinkling of comments in the thunking code should make the differences in early and late environments more apparent.

---
 arch/x86/boot/compressed/Makefile       |   1 +
 arch/x86/boot/compressed/efi_stub_64.S  |  25 ----
 arch/x86/boot/compressed/efi_thunk_64.S | 196 ++++++++++++++++++++++++++++++++
 arch/x86/platform/efi/efi_stub_64.S     | 161 --------------------------
 arch/x86/platform/efi/efi_thunk_64.S    | 121 +++++++++++++++++---
 5 files changed, 301 insertions(+), 203 deletions(-)
 create mode 100644 arch/x86/boot/compressed/efi_thunk_64.S
arch/x86/boot/compressed/Makefile +1 -0
index 20db5b3609de..911bfc692c16 100644
@@ -37,6 +37,7 @@ $(obj)/eboot.o: KBUILD_CFLAGS += -fshort-wchar -mno-red-zone
 
 vmlinux-objs-$(CONFIG_EFI_STUB) += $(obj)/eboot.o $(obj)/efi_stub_$(BITS).o \
 	$(objtree)/drivers/firmware/efi/libstub/lib.a
+vmlinux-objs-$(CONFIG_EFI_MIXED) += $(obj)/efi_thunk_$(BITS).o
 
 $(obj)/vmlinux: $(vmlinux-objs-y) FORCE
 	$(call if_changed,ld)
arch/x86/boot/compressed/efi_stub_64.S +0 -25
index 7ff3632806b1..99494dff2113 100644
@@ -3,28 +3,3 @@
 #include <asm/processor-flags.h>
 
 #include "../../platform/efi/efi_stub_64.S"
-
-#ifdef CONFIG_EFI_MIXED
-	.code64
-	.text
-ENTRY(efi64_thunk)
-	push	%rbp
-	push	%rbx
-
-	subq	$16, %rsp
-	leaq	efi_exit32(%rip), %rax
-	movl	%eax, 8(%rsp)
-	leaq	efi_gdt64(%rip), %rax
-	movl	%eax, 4(%rsp)
-	movl	%eax, 2(%rax)		/* Fixup the gdt base address */
-	leaq	efi32_boot_gdt(%rip), %rax
-	movl	%eax, (%rsp)
-
-	call	__efi64_thunk
-
-	addq	$16, %rsp
-	pop	%rbx
-	pop	%rbp
-	ret
-ENDPROC(efi64_thunk)
-#endif /* CONFIG_EFI_MIXED */
arch/x86/boot/compressed/efi_thunk_64.S new file +196 -0

196 lines not on this page · raw message

arch/x86/platform/efi/efi_stub_64.S +0 -161

167 lines not on this page · raw message

arch/x86/platform/efi/efi_thunk_64.S +104 -17

141 lines not on this page · raw message

Signature · 1 line
1.9.3
maintainer Tested-by on v1 1/1
#1 lore raw
Quoted 28 lines · Matt Fleming

From: Matt Fleming <[email protected]>

Andy pointed out that if an NMI or MCE is received while we're in the middle of an EFI mixed mode call a triple fault will occur. This can happen, for example, when issuing an EFI mixed mode call while running perf.

The reason for the triple fault is that we execute the mixed mode call in 32-bit mode with paging disabled but with 64-bit kernel IDT handlers installed throughout the call.

At Andy's suggestion, stop playing the games we currently do at runtime, such as disabling paging and installing a 32-bit GDT for __KERNEL_CS. We can simply switch to the __KERNEL32_CS descriptor before invoking firmware services, and run in compatibility mode. This way, if an NMI/MCE does occur the kernel IDT handler will execute correctly, since it'll jump to __KERNEL_CS automatically.

However, this change is only possible post-ExitBootServices(). Before then the firmware "owns" the machine and expects for its 32-bit IDT handlers to be left intact to service interrupts, etc.

So, we now need to distinguish between early boot and runtime invocations of EFI services. During early boot, we need to restore the GDT that the firmware expects to be present. We can only jump to the __KERNEL32_CS code segment for mixed mode calls after ExitBootServices() has been invoked.

Quoted 6 lines

A liberal sprinkling of comments in the thunking code should make the differences in early and late environments more apparent.

Boots fine on my Dell box.

Signature · 5 lines
Regards/Gruss,
    Boris.

ECO tip #101: Trim your mails when you reply.
--