License cleanup: add SPDX GPL-2.0 license identifier to files with no license
Many source files in the tree are missing licensing information, which
makes it harder for compliance tools to determine the correct license.
By default all files without license information are under the default
license of the kernel, which is GPL version 2.
Update the files which contain no license information with the 'GPL-2.0'
SPDX license identifier. The SPDX identifier is a legally binding
shorthand, which can be used instead of the full boiler plate text.
This patch is based on work done by Thomas Gleixner and Kate Stewart and
Philippe Ombredanne.
How this work was done:
Patches were generated and checked against linux-4.14-rc6 for a subset of
the use cases:
- file had no licensing information it it.
- file was a */uapi/* one with no licensing information in it,
- file was a */uapi/* one with existing licensing information,
Further patches will be generated in subsequent months to fix up cases
where non-standard license headers were used, and references to license
had to be inferred by heuristics based on keywords.
The analysis to determine which SPDX License Identifier to be applied to
a file was done in a spreadsheet of side by side results from of the
output of two independent scanners (ScanCode & Windriver) producing SPDX
tag:value files created by Philippe Ombredanne. Philippe prepared the
base worksheet, and did an initial spot review of a few 1000 files.
The 4.13 kernel was the starting point of the analysis with 60,537 files
assessed. Kate Stewart did a file by file comparison of the scanner
results in the spreadsheet to determine which SPDX license identifier(s)
to be applied to the file. She confirmed any determination that was not
immediately clear with lawyers working with the Linux Foundation.
Criteria used to select files for SPDX license identifier tagging was:
- Files considered eligible had to be source code files.
- Make and config files were included as candidates if they contained >5
lines of source
- File already had some variant of a license header in it (even if <5
lines).
All documentation files were explicitly excluded.
The following heuristics were used to determine which SPDX license
identifiers to apply.
- when both scanners couldn't find any license traces, file was
considered to have no license information in it, and the top level
COPYING file license applied.
For non */uapi/* files that summary was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 11139
and resulted in the first patch in this series.
If that file was a */uapi/* path one, it was "GPL-2.0 WITH
Linux-syscall-note" otherwise it was "GPL-2.0". Results of that was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 WITH Linux-syscall-note 930
and resulted in the second patch in this series.
- if a file had some form of licensing information in it, and was one
of the */uapi/* ones, it was denoted with the Linux-syscall-note if
any GPL family license was found in the file or had no licensing in
it (per prior point). Results summary:
SPDX license identifier # files
---------------------------------------------------|------
GPL-2.0 WITH Linux-syscall-note 270
GPL-2.0+ WITH Linux-syscall-note 169
((GPL-2.0 WITH Linux-syscall-note) OR BSD-2-Clause) 21
((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) 17
LGPL-2.1+ WITH Linux-syscall-note 15
GPL-1.0+ WITH Linux-syscall-note 14
((GPL-2.0+ WITH Linux-syscall-note) OR BSD-3-Clause) 5
LGPL-2.0+ WITH Linux-syscall-note 4
LGPL-2.1 WITH Linux-syscall-note 3
((GPL-2.0 WITH Linux-syscall-note) OR MIT) 3
((GPL-2.0 WITH Linux-syscall-note) AND MIT) 1
and that resulted in the third patch in this series.
- when the two scanners agreed on the detected license(s), that became
the concluded license(s).
- when there was disagreement between the two scanners (one detected a
license but the other didn't, or they both detected different
licenses) a manual inspection of the file occurred.
- In most cases a manual inspection of the information in the file
resulted in a clear resolution of the license that should apply (and
which scanner probably needed to revisit its heuristics).
- When it was not immediately clear, the license identifier was
confirmed with lawyers working with the Linux Foundation.
- If there was any question as to the appropriate license identifier,
the file was flagged for further research and to be revisited later
in time.
In total, over 70 hours of logged manual review was done on the
spreadsheet to determine the SPDX license identifiers to apply to the
source files by Kate, Philippe, Thomas and, in some cases, confirmation
by lawyers working with the Linux Foundation.
Kate also obtained a third independent scan of the 4.13 code base from
FOSSology, and compared selected files where the other two scanners
disagreed against that SPDX file, to see if there was new insights. The
Windriver scanner is based on an older version of FOSSology in part, so
they are related.
Thomas did random spot checks in about 500 files from the spreadsheets
for the uapi headers and agreed with SPDX license identifier in the
files he inspected. For the non-uapi files Thomas did random spot checks
in about 15000 files.
In initial set of patches against 4.14-rc6, 3 files were found to have
copy/paste license identifier errors, and have been fixed to reflect the
correct identifier.
Additionally Philippe spent 10 hours this week doing a detailed manual
inspection and review of the 12,461 patched files from the initial patch
version early this week with:
- a full scancode scan run, collecting the matched texts, detected
license ids and scores
- reviewing anything where there was a license detected (about 500+
files) to ensure that the applied SPDX license was correct
- reviewing anything where there was no detection but the patch license
was not GPL-2.0 WITH Linux-syscall-note to ensure that the applied
SPDX license was correct
This produced a worksheet with 20 files needing minor correction. This
worksheet was then exported into 3 different .csv files for the
different types of files to be modified.
These .csv files were then reviewed by Greg. Thomas wrote a script to
parse the csv files and add the proper SPDX tag to the file, in the
format that the file expected. This script was further refined by Greg
based on the output to detect more types of files automatically and to
distinguish between header and source .c files (which need different
comment types.) Finally Greg ran the script using the .csv files to
generate the patches.
Reviewed-by: Kate Stewart <kstewart@linuxfoundation.org>
Reviewed-by: Philippe Ombredanne <pombredanne@nexb.com>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-11-01 14:07:57 +00:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
x86/efi: Avoid triple faults during EFI mixed mode calls
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.
Reported-by: Andy Lutomirski <luto@amacapital.net>
Tested-by: Borislav Petkov <bp@suse.de>
Cc: <stable@vger.kernel.org>
Signed-off-by: Matt Fleming <matt.fleming@intel.com>
2015-01-13 15:25:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014, 2015 Intel Corporation; author Matt Fleming
|
|
|
|
*
|
|
|
|
* Early support for invoking 32-bit EFI services from a 64-bit kernel.
|
|
|
|
*
|
|
|
|
* Because this thunking occurs before ExitBootServices() we have to
|
2021-08-20 12:57:03 +00:00
|
|
|
* restore the firmware's 32-bit GDT and IDT before we make EFI service
|
|
|
|
* calls.
|
x86/efi: Avoid triple faults during EFI mixed mode calls
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.
Reported-by: Andy Lutomirski <luto@amacapital.net>
Tested-by: Borislav Petkov <bp@suse.de>
Cc: <stable@vger.kernel.org>
Signed-off-by: Matt Fleming <matt.fleming@intel.com>
2015-01-13 15:25:00 +00:00
|
|
|
*
|
|
|
|
* On the plus side, we don't have to worry about mangling 64-bit
|
2019-12-24 15:10:25 +00:00
|
|
|
* addresses into 32-bits because we're executing with an identity
|
x86/efi: Avoid triple faults during EFI mixed mode calls
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.
Reported-by: Andy Lutomirski <luto@amacapital.net>
Tested-by: Borislav Petkov <bp@suse.de>
Cc: <stable@vger.kernel.org>
Signed-off-by: Matt Fleming <matt.fleming@intel.com>
2015-01-13 15:25:00 +00:00
|
|
|
* mapped pagetable and haven't transitioned to 64-bit virtual addresses
|
|
|
|
* yet.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/linkage.h>
|
|
|
|
#include <asm/msr.h>
|
|
|
|
#include <asm/page_types.h>
|
|
|
|
#include <asm/processor-flags.h>
|
|
|
|
#include <asm/segment.h>
|
|
|
|
|
|
|
|
.code64
|
|
|
|
.text
|
2022-11-22 16:10:03 +00:00
|
|
|
/*
|
|
|
|
* When booting in 64-bit mode on 32-bit EFI firmware, startup_64_mixed_mode()
|
|
|
|
* is the first thing that runs after switching to long mode. Depending on
|
|
|
|
* whether the EFI handover protocol or the compat entry point was used to
|
2023-08-07 16:27:01 +00:00
|
|
|
* enter the kernel, it will either branch to the common 64-bit EFI stub
|
|
|
|
* entrypoint efi_stub_entry() directly, or via the 64-bit EFI PE/COFF
|
2022-11-22 16:10:03 +00:00
|
|
|
* entrypoint efi_pe_entry(). In the former case, the bootloader must provide a
|
|
|
|
* struct bootparams pointer as the third argument, so the presence of such a
|
|
|
|
* pointer is used to disambiguate.
|
|
|
|
*
|
|
|
|
* +--------------+
|
|
|
|
* +------------------+ +------------+ +------>| efi_pe_entry |
|
|
|
|
* | efi32_pe_entry |---->| | | +-----------+--+
|
|
|
|
* +------------------+ | | +------+----------------+ |
|
|
|
|
* | startup_32 |---->| startup_64_mixed_mode | |
|
2023-08-07 16:27:01 +00:00
|
|
|
* +------------------+ | | +------+----------------+ |
|
|
|
|
* | efi32_stub_entry |---->| | | |
|
|
|
|
* +------------------+ +------------+ | |
|
|
|
|
* V |
|
|
|
|
* +------------+ +----------------+ |
|
|
|
|
* | startup_64 |<----| efi_stub_entry |<--------+
|
|
|
|
* +------------+ +----------------+
|
2022-11-22 16:10:03 +00:00
|
|
|
*/
|
|
|
|
SYM_FUNC_START(startup_64_mixed_mode)
|
|
|
|
lea efi32_boot_args(%rip), %rdx
|
|
|
|
mov 0(%rdx), %edi
|
|
|
|
mov 4(%rdx), %esi
|
x86/efistub: Call mixed mode boot services on the firmware's stack
Normally, the EFI stub calls into the EFI boot services using the stack
that was live when the stub was entered. According to the UEFI spec,
this stack needs to be at least 128k in size - this might seem large but
all asynchronous processing and event handling in EFI runs from the same
stack and so quite a lot of space may be used in practice.
In mixed mode, the situation is a bit different: the bootloader calls
the 32-bit EFI stub entry point, which calls the decompressor's 32-bit
entry point, where the boot stack is set up, using a fixed allocation
of 16k. This stack is still in use when the EFI stub is started in
64-bit mode, and so all calls back into the EFI firmware will be using
the decompressor's limited boot stack.
Due to the placement of the boot stack right after the boot heap, any
stack overruns have gone unnoticed. However, commit
5c4feadb0011983b ("x86/decompressor: Move global symbol references to C code")
moved the definition of the boot heap into C code, and now the boot
stack is placed right at the base of BSS, where any overruns will
corrupt the end of the .data section.
While it would be possible to work around this by increasing the size of
the boot stack, doing so would affect all x86 systems, and mixed mode
systems are a tiny (and shrinking) fraction of the x86 installed base.
So instead, record the firmware stack pointer value when entering from
the 32-bit firmware, and switch to this stack every time a EFI boot
service call is made.
Cc: <stable@kernel.org> # v6.1+
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
2024-03-22 15:03:58 +00:00
|
|
|
|
|
|
|
/* Switch to the firmware's stack */
|
|
|
|
movl efi32_boot_sp(%rip), %esp
|
|
|
|
andl $~7, %esp
|
|
|
|
|
2023-08-07 16:27:01 +00:00
|
|
|
#ifdef CONFIG_EFI_HANDOVER_PROTOCOL
|
2022-11-22 16:10:03 +00:00
|
|
|
mov 8(%rdx), %edx // saved bootparams pointer
|
|
|
|
test %edx, %edx
|
2023-08-07 16:27:01 +00:00
|
|
|
jnz efi_stub_entry
|
|
|
|
#endif
|
2022-11-22 16:10:03 +00:00
|
|
|
/*
|
|
|
|
* efi_pe_entry uses MS calling convention, which requires 32 bytes of
|
|
|
|
* shadow space on the stack even if all arguments are passed in
|
|
|
|
* registers. We also need an additional 8 bytes for the space that
|
|
|
|
* would be occupied by the return address, and this also results in
|
|
|
|
* the correct stack alignment for entry.
|
|
|
|
*/
|
|
|
|
sub $40, %rsp
|
|
|
|
mov %rdi, %rcx // MS calling convention
|
|
|
|
mov %rsi, %rdx
|
|
|
|
jmp efi_pe_entry
|
|
|
|
SYM_FUNC_END(startup_64_mixed_mode)
|
|
|
|
|
2020-01-03 11:39:48 +00:00
|
|
|
SYM_FUNC_START(__efi64_thunk)
|
x86/efi: Avoid triple faults during EFI mixed mode calls
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.
Reported-by: Andy Lutomirski <luto@amacapital.net>
Tested-by: Borislav Petkov <bp@suse.de>
Cc: <stable@vger.kernel.org>
Signed-off-by: Matt Fleming <matt.fleming@intel.com>
2015-01-13 15:25:00 +00:00
|
|
|
push %rbp
|
|
|
|
push %rbx
|
|
|
|
|
|
|
|
movl %ds, %eax
|
|
|
|
push %rax
|
|
|
|
movl %es, %eax
|
|
|
|
push %rax
|
|
|
|
movl %ss, %eax
|
|
|
|
push %rax
|
|
|
|
|
2021-11-19 11:47:43 +00:00
|
|
|
/* Copy args passed on stack */
|
|
|
|
movq 0x30(%rsp), %rbp
|
|
|
|
movq 0x38(%rsp), %rbx
|
|
|
|
movq 0x40(%rsp), %rax
|
|
|
|
|
x86/efi: Avoid triple faults during EFI mixed mode calls
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.
Reported-by: Andy Lutomirski <luto@amacapital.net>
Tested-by: Borislav Petkov <bp@suse.de>
Cc: <stable@vger.kernel.org>
Signed-off-by: Matt Fleming <matt.fleming@intel.com>
2015-01-13 15:25:00 +00:00
|
|
|
/*
|
|
|
|
* Convert x86-64 ABI params to i386 ABI
|
|
|
|
*/
|
2021-08-20 12:57:03 +00:00
|
|
|
subq $64, %rsp
|
x86/efi: Avoid triple faults during EFI mixed mode calls
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.
Reported-by: Andy Lutomirski <luto@amacapital.net>
Tested-by: Borislav Petkov <bp@suse.de>
Cc: <stable@vger.kernel.org>
Signed-off-by: Matt Fleming <matt.fleming@intel.com>
2015-01-13 15:25:00 +00:00
|
|
|
movl %esi, 0x0(%rsp)
|
|
|
|
movl %edx, 0x4(%rsp)
|
|
|
|
movl %ecx, 0x8(%rsp)
|
2020-01-13 17:22:34 +00:00
|
|
|
movl %r8d, 0xc(%rsp)
|
|
|
|
movl %r9d, 0x10(%rsp)
|
2021-11-19 11:47:43 +00:00
|
|
|
movl %ebp, 0x14(%rsp)
|
|
|
|
movl %ebx, 0x18(%rsp)
|
|
|
|
movl %eax, 0x1c(%rsp)
|
x86/efi: Avoid triple faults during EFI mixed mode calls
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.
Reported-by: Andy Lutomirski <luto@amacapital.net>
Tested-by: Borislav Petkov <bp@suse.de>
Cc: <stable@vger.kernel.org>
Signed-off-by: Matt Fleming <matt.fleming@intel.com>
2015-01-13 15:25:00 +00:00
|
|
|
|
2021-11-19 11:47:43 +00:00
|
|
|
leaq 0x20(%rsp), %rbx
|
2020-05-23 22:15:13 +00:00
|
|
|
sgdt (%rbx)
|
2022-11-22 16:10:08 +00:00
|
|
|
sidt 16(%rbx)
|
2021-08-20 12:57:03 +00:00
|
|
|
|
2021-11-19 11:47:43 +00:00
|
|
|
leaq 1f(%rip), %rbp
|
|
|
|
|
x86/efi: Avoid triple faults during EFI mixed mode calls
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.
Reported-by: Andy Lutomirski <luto@amacapital.net>
Tested-by: Borislav Petkov <bp@suse.de>
Cc: <stable@vger.kernel.org>
Signed-off-by: Matt Fleming <matt.fleming@intel.com>
2015-01-13 15:25:00 +00:00
|
|
|
/*
|
2022-11-22 16:10:08 +00:00
|
|
|
* Switch to IDT and GDT with 32-bit segments. These are the firmware
|
|
|
|
* GDT and IDT that were installed when the kernel started executing.
|
|
|
|
* The pointers were saved by the efi32_entry() routine below.
|
2020-02-02 17:13:48 +00:00
|
|
|
*
|
|
|
|
* Pass the saved DS selector to the 32-bit code, and use far return to
|
|
|
|
* restore the saved CS selector.
|
x86/efi: Avoid triple faults during EFI mixed mode calls
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.
Reported-by: Andy Lutomirski <luto@amacapital.net>
Tested-by: Borislav Petkov <bp@suse.de>
Cc: <stable@vger.kernel.org>
Signed-off-by: Matt Fleming <matt.fleming@intel.com>
2015-01-13 15:25:00 +00:00
|
|
|
*/
|
2022-11-22 16:10:08 +00:00
|
|
|
lidt efi32_boot_idt(%rip)
|
|
|
|
lgdt efi32_boot_gdt(%rip)
|
x86/efi: Avoid triple faults during EFI mixed mode calls
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.
Reported-by: Andy Lutomirski <luto@amacapital.net>
Tested-by: Borislav Petkov <bp@suse.de>
Cc: <stable@vger.kernel.org>
Signed-off-by: Matt Fleming <matt.fleming@intel.com>
2015-01-13 15:25:00 +00:00
|
|
|
|
2020-02-02 17:13:48 +00:00
|
|
|
movzwl efi32_boot_ds(%rip), %edx
|
|
|
|
movzwq efi32_boot_cs(%rip), %rax
|
|
|
|
pushq %rax
|
x86/efi: Avoid triple faults during EFI mixed mode calls
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.
Reported-by: Andy Lutomirski <luto@amacapital.net>
Tested-by: Borislav Petkov <bp@suse.de>
Cc: <stable@vger.kernel.org>
Signed-off-by: Matt Fleming <matt.fleming@intel.com>
2015-01-13 15:25:00 +00:00
|
|
|
leaq efi_enter32(%rip), %rax
|
|
|
|
pushq %rax
|
|
|
|
lretq
|
|
|
|
|
2021-08-20 12:57:03 +00:00
|
|
|
1: addq $64, %rsp
|
2019-12-24 15:10:25 +00:00
|
|
|
movq %rdi, %rax
|
x86/efi: Avoid triple faults during EFI mixed mode calls
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.
Reported-by: Andy Lutomirski <luto@amacapital.net>
Tested-by: Borislav Petkov <bp@suse.de>
Cc: <stable@vger.kernel.org>
Signed-off-by: Matt Fleming <matt.fleming@intel.com>
2015-01-13 15:25:00 +00:00
|
|
|
|
|
|
|
pop %rbx
|
|
|
|
movl %ebx, %ss
|
|
|
|
pop %rbx
|
|
|
|
movl %ebx, %es
|
|
|
|
pop %rbx
|
|
|
|
movl %ebx, %ds
|
2020-02-02 17:13:48 +00:00
|
|
|
/* Clear out 32-bit selector from FS and GS */
|
|
|
|
xorl %ebx, %ebx
|
|
|
|
movl %ebx, %fs
|
|
|
|
movl %ebx, %gs
|
x86/efi: Avoid triple faults during EFI mixed mode calls
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.
Reported-by: Andy Lutomirski <luto@amacapital.net>
Tested-by: Borislav Petkov <bp@suse.de>
Cc: <stable@vger.kernel.org>
Signed-off-by: Matt Fleming <matt.fleming@intel.com>
2015-01-13 15:25:00 +00:00
|
|
|
|
|
|
|
pop %rbx
|
|
|
|
pop %rbp
|
2021-12-04 13:43:40 +00:00
|
|
|
RET
|
2020-01-03 11:39:48 +00:00
|
|
|
SYM_FUNC_END(__efi64_thunk)
|
x86/efi: Avoid triple faults during EFI mixed mode calls
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.
Reported-by: Andy Lutomirski <luto@amacapital.net>
Tested-by: Borislav Petkov <bp@suse.de>
Cc: <stable@vger.kernel.org>
Signed-off-by: Matt Fleming <matt.fleming@intel.com>
2015-01-13 15:25:00 +00:00
|
|
|
|
|
|
|
.code32
|
2023-08-07 16:27:02 +00:00
|
|
|
#ifdef CONFIG_EFI_HANDOVER_PROTOCOL
|
|
|
|
SYM_FUNC_START(efi32_stub_entry)
|
2023-08-07 16:27:03 +00:00
|
|
|
call 1f
|
|
|
|
1: popl %ecx
|
|
|
|
|
|
|
|
/* Clear BSS */
|
|
|
|
xorl %eax, %eax
|
|
|
|
leal (_bss - 1b)(%ecx), %edi
|
|
|
|
leal (_ebss - 1b)(%ecx), %ecx
|
|
|
|
subl %edi, %ecx
|
|
|
|
shrl $2, %ecx
|
|
|
|
cld
|
|
|
|
rep stosl
|
|
|
|
|
2023-08-07 16:27:02 +00:00
|
|
|
add $0x4, %esp /* Discard return address */
|
|
|
|
popl %ecx
|
|
|
|
popl %edx
|
|
|
|
popl %esi
|
|
|
|
jmp efi32_entry
|
|
|
|
SYM_FUNC_END(efi32_stub_entry)
|
|
|
|
#endif
|
|
|
|
|
x86/efi: Avoid triple faults during EFI mixed mode calls
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.
Reported-by: Andy Lutomirski <luto@amacapital.net>
Tested-by: Borislav Petkov <bp@suse.de>
Cc: <stable@vger.kernel.org>
Signed-off-by: Matt Fleming <matt.fleming@intel.com>
2015-01-13 15:25:00 +00:00
|
|
|
/*
|
|
|
|
* EFI service pointer must be in %edi.
|
|
|
|
*
|
|
|
|
* The stack should represent the 32-bit calling convention.
|
|
|
|
*/
|
2019-10-11 11:51:00 +00:00
|
|
|
SYM_FUNC_START_LOCAL(efi_enter32)
|
2020-02-02 17:13:48 +00:00
|
|
|
/* Load firmware selector into data and stack segment registers */
|
|
|
|
movl %edx, %ds
|
|
|
|
movl %edx, %es
|
|
|
|
movl %edx, %fs
|
|
|
|
movl %edx, %gs
|
|
|
|
movl %edx, %ss
|
x86/efi: Avoid triple faults during EFI mixed mode calls
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.
Reported-by: Andy Lutomirski <luto@amacapital.net>
Tested-by: Borislav Petkov <bp@suse.de>
Cc: <stable@vger.kernel.org>
Signed-off-by: Matt Fleming <matt.fleming@intel.com>
2015-01-13 15:25:00 +00:00
|
|
|
|
|
|
|
/* Reload pgtables */
|
|
|
|
movl %cr3, %eax
|
|
|
|
movl %eax, %cr3
|
|
|
|
|
|
|
|
/* Disable paging */
|
|
|
|
movl %cr0, %eax
|
|
|
|
btrl $X86_CR0_PG_BIT, %eax
|
|
|
|
movl %eax, %cr0
|
|
|
|
|
|
|
|
/* Disable long mode via EFER */
|
|
|
|
movl $MSR_EFER, %ecx
|
|
|
|
rdmsr
|
|
|
|
btrl $_EFER_LME, %eax
|
|
|
|
wrmsr
|
|
|
|
|
|
|
|
call *%edi
|
|
|
|
|
|
|
|
/* We must preserve return value */
|
|
|
|
movl %eax, %edi
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Some firmware will return with interrupts enabled. Be sure to
|
2021-08-20 12:57:03 +00:00
|
|
|
* disable them before we switch GDTs and IDTs.
|
x86/efi: Avoid triple faults during EFI mixed mode calls
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.
Reported-by: Andy Lutomirski <luto@amacapital.net>
Tested-by: Borislav Petkov <bp@suse.de>
Cc: <stable@vger.kernel.org>
Signed-off-by: Matt Fleming <matt.fleming@intel.com>
2015-01-13 15:25:00 +00:00
|
|
|
*/
|
|
|
|
cli
|
|
|
|
|
2022-11-22 16:10:08 +00:00
|
|
|
lidtl 16(%ebx)
|
2020-01-13 17:22:34 +00:00
|
|
|
lgdtl (%ebx)
|
x86/efi: Avoid triple faults during EFI mixed mode calls
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.
Reported-by: Andy Lutomirski <luto@amacapital.net>
Tested-by: Borislav Petkov <bp@suse.de>
Cc: <stable@vger.kernel.org>
Signed-off-by: Matt Fleming <matt.fleming@intel.com>
2015-01-13 15:25:00 +00:00
|
|
|
|
|
|
|
movl %cr4, %eax
|
|
|
|
btsl $(X86_CR4_PAE_BIT), %eax
|
|
|
|
movl %eax, %cr4
|
|
|
|
|
|
|
|
movl %cr3, %eax
|
|
|
|
movl %eax, %cr3
|
|
|
|
|
|
|
|
movl $MSR_EFER, %ecx
|
|
|
|
rdmsr
|
|
|
|
btsl $_EFER_LME, %eax
|
|
|
|
wrmsr
|
|
|
|
|
|
|
|
xorl %eax, %eax
|
|
|
|
lldt %ax
|
|
|
|
|
|
|
|
pushl $__KERNEL_CS
|
2020-01-13 17:22:34 +00:00
|
|
|
pushl %ebp
|
x86/efi: Avoid triple faults during EFI mixed mode calls
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.
Reported-by: Andy Lutomirski <luto@amacapital.net>
Tested-by: Borislav Petkov <bp@suse.de>
Cc: <stable@vger.kernel.org>
Signed-off-by: Matt Fleming <matt.fleming@intel.com>
2015-01-13 15:25:00 +00:00
|
|
|
|
|
|
|
/* Enable paging */
|
|
|
|
movl %cr0, %eax
|
|
|
|
btsl $X86_CR0_PG_BIT, %eax
|
|
|
|
movl %eax, %cr0
|
|
|
|
lret
|
2019-10-11 11:51:00 +00:00
|
|
|
SYM_FUNC_END(efi_enter32)
|
x86/efi: Avoid triple faults during EFI mixed mode calls
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.
Reported-by: Andy Lutomirski <luto@amacapital.net>
Tested-by: Borislav Petkov <bp@suse.de>
Cc: <stable@vger.kernel.org>
Signed-off-by: Matt Fleming <matt.fleming@intel.com>
2015-01-13 15:25:00 +00:00
|
|
|
|
2022-11-22 16:10:05 +00:00
|
|
|
/*
|
|
|
|
* This is the common EFI stub entry point for mixed mode.
|
|
|
|
*
|
|
|
|
* Arguments: %ecx image handle
|
|
|
|
* %edx EFI system table pointer
|
|
|
|
* %esi struct bootparams pointer (or NULL when not using
|
|
|
|
* the EFI handover protocol)
|
|
|
|
*
|
|
|
|
* Since this is the point of no return for ordinary execution, no registers
|
|
|
|
* are considered live except for the function parameters. [Note that the EFI
|
|
|
|
* stub may still exit and return to the firmware using the Exit() EFI boot
|
|
|
|
* service.]
|
|
|
|
*/
|
2023-08-07 16:27:02 +00:00
|
|
|
SYM_FUNC_START_LOCAL(efi32_entry)
|
2022-11-22 16:10:05 +00:00
|
|
|
call 1f
|
|
|
|
1: pop %ebx
|
|
|
|
|
|
|
|
/* Save firmware GDTR and code/data selectors */
|
|
|
|
sgdtl (efi32_boot_gdt - 1b)(%ebx)
|
|
|
|
movw %cs, (efi32_boot_cs - 1b)(%ebx)
|
|
|
|
movw %ds, (efi32_boot_ds - 1b)(%ebx)
|
|
|
|
|
|
|
|
/* Store firmware IDT descriptor */
|
|
|
|
sidtl (efi32_boot_idt - 1b)(%ebx)
|
|
|
|
|
x86/efistub: Call mixed mode boot services on the firmware's stack
Normally, the EFI stub calls into the EFI boot services using the stack
that was live when the stub was entered. According to the UEFI spec,
this stack needs to be at least 128k in size - this might seem large but
all asynchronous processing and event handling in EFI runs from the same
stack and so quite a lot of space may be used in practice.
In mixed mode, the situation is a bit different: the bootloader calls
the 32-bit EFI stub entry point, which calls the decompressor's 32-bit
entry point, where the boot stack is set up, using a fixed allocation
of 16k. This stack is still in use when the EFI stub is started in
64-bit mode, and so all calls back into the EFI firmware will be using
the decompressor's limited boot stack.
Due to the placement of the boot stack right after the boot heap, any
stack overruns have gone unnoticed. However, commit
5c4feadb0011983b ("x86/decompressor: Move global symbol references to C code")
moved the definition of the boot heap into C code, and now the boot
stack is placed right at the base of BSS, where any overruns will
corrupt the end of the .data section.
While it would be possible to work around this by increasing the size of
the boot stack, doing so would affect all x86 systems, and mixed mode
systems are a tiny (and shrinking) fraction of the x86 installed base.
So instead, record the firmware stack pointer value when entering from
the 32-bit firmware, and switch to this stack every time a EFI boot
service call is made.
Cc: <stable@kernel.org> # v6.1+
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
2024-03-22 15:03:58 +00:00
|
|
|
/* Store firmware stack pointer */
|
|
|
|
movl %esp, (efi32_boot_sp - 1b)(%ebx)
|
|
|
|
|
2022-11-22 16:10:05 +00:00
|
|
|
/* Store boot arguments */
|
|
|
|
leal (efi32_boot_args - 1b)(%ebx), %ebx
|
|
|
|
movl %ecx, 0(%ebx)
|
|
|
|
movl %edx, 4(%ebx)
|
|
|
|
movl %esi, 8(%ebx)
|
|
|
|
movb $0x0, 12(%ebx) // efi_is64
|
|
|
|
|
|
|
|
/* Disable paging */
|
|
|
|
movl %cr0, %eax
|
|
|
|
btrl $X86_CR0_PG_BIT, %eax
|
|
|
|
movl %eax, %cr0
|
|
|
|
|
|
|
|
jmp startup_32
|
|
|
|
SYM_FUNC_END(efi32_entry)
|
|
|
|
|
2022-11-22 16:10:06 +00:00
|
|
|
/*
|
|
|
|
* efi_status_t efi32_pe_entry(efi_handle_t image_handle,
|
|
|
|
* efi_system_table_32_t *sys_table)
|
|
|
|
*/
|
|
|
|
SYM_FUNC_START(efi32_pe_entry)
|
|
|
|
pushl %ebp
|
|
|
|
movl %esp, %ebp
|
|
|
|
pushl %ebx // save callee-save registers
|
|
|
|
pushl %edi
|
|
|
|
|
|
|
|
call verify_cpu // check for long mode support
|
|
|
|
testl %eax, %eax
|
|
|
|
movl $0x80000003, %eax // EFI_UNSUPPORTED
|
|
|
|
jnz 2f
|
|
|
|
|
|
|
|
movl 8(%ebp), %ecx // image_handle
|
|
|
|
movl 12(%ebp), %edx // sys_table
|
|
|
|
xorl %esi, %esi
|
|
|
|
jmp efi32_entry // pass %ecx, %edx, %esi
|
|
|
|
// no other registers remain live
|
|
|
|
|
|
|
|
2: popl %edi // restore callee-save registers
|
|
|
|
popl %ebx
|
|
|
|
leave
|
|
|
|
RET
|
|
|
|
SYM_FUNC_END(efi32_pe_entry)
|
|
|
|
|
2023-08-07 16:27:02 +00:00
|
|
|
#ifdef CONFIG_EFI_HANDOVER_PROTOCOL
|
|
|
|
.org efi32_stub_entry + 0x200
|
|
|
|
.code64
|
|
|
|
SYM_FUNC_START_NOALIGN(efi64_stub_entry)
|
2023-08-07 16:27:03 +00:00
|
|
|
jmp efi_handover_entry
|
2023-08-07 16:27:02 +00:00
|
|
|
SYM_FUNC_END(efi64_stub_entry)
|
|
|
|
#endif
|
|
|
|
|
x86/efi: Avoid triple faults during EFI mixed mode calls
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.
Reported-by: Andy Lutomirski <luto@amacapital.net>
Tested-by: Borislav Petkov <bp@suse.de>
Cc: <stable@vger.kernel.org>
Signed-off-by: Matt Fleming <matt.fleming@intel.com>
2015-01-13 15:25:00 +00:00
|
|
|
.data
|
|
|
|
.balign 8
|
2022-11-22 16:10:05 +00:00
|
|
|
SYM_DATA_START_LOCAL(efi32_boot_gdt)
|
2019-10-11 11:50:52 +00:00
|
|
|
.word 0
|
|
|
|
.quad 0
|
|
|
|
SYM_DATA_END(efi32_boot_gdt)
|
|
|
|
|
2022-11-22 16:10:05 +00:00
|
|
|
SYM_DATA_START_LOCAL(efi32_boot_idt)
|
2021-08-20 12:57:03 +00:00
|
|
|
.word 0
|
|
|
|
.quad 0
|
|
|
|
SYM_DATA_END(efi32_boot_idt)
|
|
|
|
|
2022-11-22 16:10:05 +00:00
|
|
|
SYM_DATA_LOCAL(efi32_boot_cs, .word 0)
|
|
|
|
SYM_DATA_LOCAL(efi32_boot_ds, .word 0)
|
x86/efistub: Call mixed mode boot services on the firmware's stack
Normally, the EFI stub calls into the EFI boot services using the stack
that was live when the stub was entered. According to the UEFI spec,
this stack needs to be at least 128k in size - this might seem large but
all asynchronous processing and event handling in EFI runs from the same
stack and so quite a lot of space may be used in practice.
In mixed mode, the situation is a bit different: the bootloader calls
the 32-bit EFI stub entry point, which calls the decompressor's 32-bit
entry point, where the boot stack is set up, using a fixed allocation
of 16k. This stack is still in use when the EFI stub is started in
64-bit mode, and so all calls back into the EFI firmware will be using
the decompressor's limited boot stack.
Due to the placement of the boot stack right after the boot heap, any
stack overruns have gone unnoticed. However, commit
5c4feadb0011983b ("x86/decompressor: Move global symbol references to C code")
moved the definition of the boot heap into C code, and now the boot
stack is placed right at the base of BSS, where any overruns will
corrupt the end of the .data section.
While it would be possible to work around this by increasing the size of
the boot stack, doing so would affect all x86 systems, and mixed mode
systems are a tiny (and shrinking) fraction of the x86 installed base.
So instead, record the firmware stack pointer value when entering from
the 32-bit firmware, and switch to this stack every time a EFI boot
service call is made.
Cc: <stable@kernel.org> # v6.1+
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
2024-03-22 15:03:58 +00:00
|
|
|
SYM_DATA_LOCAL(efi32_boot_sp, .long 0)
|
2022-11-22 16:10:05 +00:00
|
|
|
SYM_DATA_LOCAL(efi32_boot_args, .long 0, 0, 0)
|
|
|
|
SYM_DATA(efi_is64, .byte 1)
|