EFI urgent fixes
Subsystems (4)
From: Matt Fleming <[email protected]>
Folks,
The patches in this pull request fix kernel crashes when booting Linux on UEFI v2.5 machines with the Properties Table feature enabled.
Essentially, when this feature is enabled the firmware allocates separate entries in the EFI memory map for the code and data sections of PE/COFF images, whereas previously only one memory map entry would have existed.
Because we've now got two entries that reference each other we *must* map them into the kernel virtual address space with the same offsets and in the same order as they appear in the EFI memory map. Failure to do so causes the firmware to access unmapped/invalid addresses.
These patches were intentionally kept as small as possible so that they can be backported by distributions, aggressively.
The following changes since commit 1f93e4a96c9109378204c147b3eec0d0e8100fde:
Linux 4.3-rc2 (2015-09-20 14:32:34 -0700)
are available in the git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/mfleming/efi.git tags/efi-urgent
for you to fetch changes up to 1fa25e09ca2ce07f03bca93ad71800c312fd4951:
arm64/efi: Don't pad between EFI_MEMORY_RUNTIME regions (2015-09-25 22:35:15 +0100)
----------------------------------------------------------------
* arm64 bug fix for UEFI 2.5 firmware that has the Properties Table feature enabled. The fix avoids a kernel crash by removing the padding between runtime regions that we currently do in the kernel so we don't break the EFI's cross-region references - Ard Biesheuvel
* Map EFI memory regions in-order on x86 so that we maintain the relative offset between regions and fix a crash when booting on UEFI 2.5 machines with the Properties Table feature enabled.
----------------------------------------------------------------
Ard Biesheuvel (1):
arm64/efi: Don't pad between EFI_MEMORY_RUNTIME regions
Matt Fleming (1):
x86/efi: Map EFI memmap entries in-order at runtime
arch/arm64/kernel/efi.c | 3 +- arch/x86/platform/efi/efi.c | 67 ++++++++++++++++++++++++- drivers/firmware/efi/libstub/arm-stub.c | 88 +++++++++++++++++++++++++++------ 3 files changed, 141 insertions(+), 17 deletions(-)
PATCH 1/2 x86/efi: Map EFI memmap entries in-order at runtime
From: Matt Fleming <[email protected]>
Beginning with UEFI v2.5 EFI_PROPERTIES_TABLE was introduced that signals that the firmware PE/COFF loader supports splitting code and data sections of PE/COFF images into separate EFI memory map entries. This allows the kernel to map those regions with strict memory protections, e.g. EFI_MEMORY_RO for code, EFI_MEMORY_XP for data, etc.
Unfortunately, an unwritten requirement of this new feature is that the regions need to be mapped with the same offsets relative to each other as observed in the EFI memory map. If this is not done crashes like this may occur,
[ 0.006391] BUG: unable to handle kernel paging request at fffffffefe6086dd [ 0.006923] IP: [<fffffffefe6086dd>] 0xfffffffefe6086dd [ 0.007000] Call Trace: [ 0.007000] [<ffffffff8104c90e>] efi_call+0x7e/0x100 [ 0.007000] [<ffffffff81602091>] ? virt_efi_set_variable+0x61/0x90 [ 0.007000] [<ffffffff8104c583>] efi_delete_dummy_variable+0x63/0x70 [ 0.007000] [<ffffffff81f4e4aa>] efi_enter_virtual_mode+0x383/0x392 [ 0.007000] [<ffffffff81f37e1b>] start_kernel+0x38a/0x417 [ 0.007000] [<ffffffff81f37495>] x86_64_start_reservations+0x2a/0x2c [ 0.007000] [<ffffffff81f37582>] x86_64_start_kernel+0xeb/0xef
Here 0xfffffffefe6086dd refers to an address the firmware expects to be mapped but which the OS never claimed was mapped. The issue is that included in these regions are relative addresses to other regions which were emitted by the firmware toolchain before the "splitting" of sections occurred at runtime.
Needless to say, we don't satisfy this unwritten requirement on x86_64 and instead map the EFI memory map entries in reverse order. The above crash is almost certainly triggerable with any kernel newer than v3.13 because that's when we rewrote the EFI runtime region mapping code, in commit d2f7cbe7b26a ("x86/efi: Runtime services virtual mapping"). For kernel versions before v3.13 things may work by pure luck depending on the fragmentation of the kernel virtual address space at the time we map the EFI regions.
Instead of mapping the EFI memory map entries in reverse order, where entry N has a higher virtual address than entry N+1, map them in the same order as they appear in the EFI memory map to preserve this relative offset between regions.
This patch has been kept as small as possible with the intention that it should be applied aggressively to stable and distribution kernels. It is very much a bugfix rather than support for a new feature, since when EFI_PROPERTIES_TABLE is enabled we must map things as outlined above to even boot - we have no way of asking the firmware not to split the code/data regions.
In fact, this patch doesn't even make use of the more strict memory protections available in UEFI v2.5. That will come later.
--- arch/x86/platform/efi/efi.c | 67 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-)
arch/x86/platform/efi/efi.c +66 -1
@@ -705,6 +705,70 @@ out: }/* +* Iterate the EFI memory map in reverse order because the regions +* will be mapped top-down. The end result is the same as if we had +* mapped things forward, but doesn't require us to change the +* existing implementation of efi_map_region(). +*/ +static inline void *efi_map_next_entry_reverse (void *entry) +{ +/* Initial call */ +if (!entry) +return memmap.map_end - memmap.desc_size; + + entry -= memmap.desc_size; +if (entry < memmap.map) +return NULL ; + +return entry; +} + +/* +* efi_map_next_entry - Return the next EFI memory map descriptor +* @entry: Previous EFI memory map descriptor +* +* This is a helper function to iterate over the EFI memory map, which +* we do in different orders depending on the current configuration. +* +* To begin traversing the memory map @entry must be %NULL. +* +* Returns %NULL when we reach the end of the memory map. +*/ +static void *efi_map_next_entry (void *entry) +{ +if (!efi_enabled (EFI_OLD_MEMMAP ) &&efi_enabled (EFI_64BIT )) { +/* +* Starting in UEFI v2.5 the EFI_PROPERTIES_TABLE +* config table feature requires us to map all entries +* in the same order as they appear in the EFI memory +* map. That is to say, entry N must have a lower +* virtual address than entry N+1. This is because the +* firmware toolchain leaves relative references in +* the code/data sections, which are split and become +* separate EFI memory regions. Mapping things +* out-of-order leads to the firmware accessing +* unmapped addresses. +* +* Since we need to map things this way whether or not +* the kernel actually makes use of +* EFI_PROPERTIES_TABLE, let's just switch to this +* scheme by default for 64-bit. +*/ +return efi_map_next_entry_reverse (entry); + } + +/* Initial call */ +if (!entry) +return memmap.map; + + entry += memmap.desc_size; +if (entry >= memmap.map_end) +return NULL ; + +return entry; +} + +/* * Map the efi memory ranges of the runtime services and update new_mmap with * virtual addresses. */
@@ -714,7 +778,8 @@ static void * __init efi_map_regions(int *count, int *pg_shift)unsigned long left =0 ;efi_memory_desc_t *md; -for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { + p =NULL ; +while ((p =efi_map_next_entry (p))) { md = p;if (!(md->attribute &EFI_MEMORY_RUNTIME )) {#ifdef CONFIG_X86_64
Signature · 1 line
2.1.0
PATCH 2/2 arm64/efi: Don't pad between EFI_MEMORY_RUNTIME regions
In reply to Matt FlemingFrom: Ard Biesheuvel <[email protected]>
The new Properties Table feature introduced in UEFIv2.5 may split memory regions that cover PE/COFF memory images into separate code and data regions. Since these regions only differ in the type (runtime code vs runtime data) and the permission bits, but not in the memory type attributes (UC/WC/WT/WB), the spec does not require them to be aligned to 64 KB.
Since the relative offset of PE/COFF .text and .data segments cannot be changed on the fly, this means that we can no longer pad out those regions to be mappable using 64 KB pages.
Unfortunately, there is no annotation in the UEFI memory map that identifies data regions that were split off from a code region, so we must apply this logic to all adjacent runtime regions whose attributes only differ in the permission bits.
So instead of rounding each memory region to 64 KB alignment at both ends, only round down regions that are not directly preceded by another runtime region with the same type attributes. Since the UEFI spec does not mandate that the memory map be sorted, this means we also need to sort it first.
Note that this change will result in all EFI_MEMORY_RUNTIME regions whose start addresses are not aligned to the OS page size to be mapped with executable permissions (i.e., on kernels compiled with 64 KB pages). However, since these mappings are only active during the time that UEFI Runtime Services are being invoked, the window for abuse is rather small.
--- arch/arm64/kernel/efi.c | 3 +- drivers/firmware/efi/libstub/arm-stub.c | 88 +++++++++++++++++++++++++++------ 2 files changed, 75 insertions(+), 16 deletions(-)
arch/arm64/kernel/efi.c +2 -1
@@ -258,7 +258,8 @@ static bool __init efi_virtmap_init(void)*/ if (!is_normal_ram (md)) prot =__pgprot (PROT_DEVICE_nGnRE); -else if (md->type ==EFI_RUNTIME_SERVICES_CODE ) +else if (md->type ==EFI_RUNTIME_SERVICES_CODE || + !PAGE_ALIGNED (md->phys_addr)) prot =PAGE_KERNEL_EXEC ;else prot =PAGE_KERNEL ;
drivers/firmware/efi/libstub/arm-stub.c +73 -15
118 lines not on this page · raw message
Signature · 1 line
2.1.0
Re: [PATCH 1/2] x86/efi: Map EFI memmap entries in-order at runtime
In reply to Matt FlemingSo this commit worries me.
This bug is a good find, and the fix is obviously needed and urgent, but I'm not sure about the implementation at all. (I've Cc:-ed a few more x86 low level gents.)
Quoted 39 lines · Matt Fleming
/*
+ * Iterate the EFI memory map in reverse order because the regions
+ * will be mapped top-down. The end result is the same as if we had
+ * mapped things forward, but doesn't require us to change the
+ * existing implementation ofefi_map_region().
+ */arch/x86/platform/efi/efi.c +33
-0+static inline void *efi_map_next_entry_reverse (void *entry) +{ +/* Initial call */ +if (!entry) +return memmap.map_end - memmap.desc_size; + + entry -= memmap.desc_size; +if (entry < memmap.map) +return NULL ; + +return entry; +} + +/* +* efi_map_next_entry - Return the next EFI memory map descriptor +* @entry: Previous EFI memory map descriptor +* +* This is a helper function to iterate over the EFI memory map, which +* we do in different orders depending on the current configuration. +* +* To begin traversing the memory map @entry must be %NULL. +* +* Returns %NULL when we reach the end of the memory map. +*/ +static void *efi_map_next_entry (void *entry) +{ +if (!efi_enabled (EFI_OLD_MEMMAP ) &&efi_enabled (EFI_64BIT )) { +/* +* Starting in UEFI v2.5 the EFI_PROPERTIES_TABLE +* config table feature requires us to map all entries +* in the same order as they appear in the EFI memory +* map. That is to say, entry N must have a lower +* virtual address than entry N+1. This is because the
arch/x86/platform/efi/efi.c out: · line 755
Quoted 10 lines
arch/x86/platform/efi/efi.c +10
-0+* firmware toolchain leaves relative references in +* the code/data sections, which are split and become +* separate EFI memory regions. Mapping things +* out-of-order leads to the firmware accessing +* unmapped addresses. +* +* Since we need to map things this way whether or not +* the kernel actually makes use of +* EFI_PROPERTIES_TABLE, let's just switch to this +* scheme by default for 64-bit.
The thing is, if relative accesses between these 'sections' do happen then the requirement is much stronger than just 'ordered by addresses' - then we must map them continuously and as a single block!
So at minimum the comment should say that. But I think we want more:
Quoted 20 lines
arch/x86/platform/efi/efi.c +16
-0+*/ +return efi_map_next_entry_reverse (entry); + } + +/* Initial call */ +if (!entry) +return memmap.map; + + entry += memmap.desc_size; +if (entry >= memmap.map_end) +return NULL ; + +return entry; +} + +/* * Map the efi memory ranges of the runtime services and update new_mmap with * virtual addresses. */
arch/x86/platform/efi/efi.c static void * __init efi_map_regions(int *count, int *pg_shift) · line 784
Quoted 8 lines
arch/x86/platform/efi/efi.c +2
-1@@ -714,7 +778,8 @@ static void * __init efi_map_regions(int *count, int *pg_shift)unsigned long left =0 ;efi_memory_desc_t *md; -for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { + p =NULL ; +while ((p =efi_map_next_entry (p))) { md = p;if (!(md->attribute &EFI_MEMORY_RUNTIME )) {
So why is this 64-bit only? Is 32-bit not affected because there we allocate virtual addresses bottom-up?
This would be a lot clearer if we just mapped the entries in order, no questions asked. Conditions like this:
arch/x86/platform/efi/efi.c out: · line 739
Quoted 1 line
arch/x86/platform/efi/efi.c +1
-0+if (!efi_enabled (EFI_OLD_MEMMAP ) &&efi_enabled (EFI_64BIT )) {
... just invite confusion and possible corner cases where we end up mapping them wrong.
So could we make the whole code obviously bottom-up? Such as first calculating the size of virtual memory needed, then allocating a _single_, obviously continuous mapping, and then doing a very clear in-order mapping within that window? That would remove any bitness and legacy dependencies.
Good virtual memory layout is so critical for any third party code that this should be the central property of the whole approach, not just some side condition somewhere in an iteration loop.
Thanks,
Ingo
Re: [PATCH 2/2] arm64/efi: Don't pad between EFI_MEMORY_RUNTIME regions
In reply to Matt FlemingQuoted 17 lines · Matt Fleming
From: Ard Biesheuvel <[email protected]>
The new Properties Table feature introduced in UEFIv2.5 may split memory regions that cover PE/COFF memory images into separate code and data regions. Since these regions only differ in the type (runtime code vs runtime data) and the permission bits, but not in the memory type attributes (UC/WC/WT/WB), the spec does not require them to be aligned to 64 KB.
Since the relative offset of PE/COFF .text and .data segments cannot be changed on the fly, this means that we can no longer pad out those regions to be mappable using 64 KB pages.
Unfortunately, there is no annotation in the UEFI memory map that identifies data regions that were split off from a code region, so we must apply this logic to all adjacent runtime regions whose attributes only differ in the permission bits.
Quoted 5 lines
So instead of rounding each memory region to 64 KB alignment at both ends, only round down regions that are not directly preceded by another runtime region with the same type attributes. Since the UEFI spec does not mandate that the memory map be sorted, this means we also need to sort it first.
So I think this is fundamentally wrong as well, similarly to the related x86 fix.
I think for compatibility reasons the whole 'EFI runtime image' should be mapped in a single go, as closely matching the EFI layouts and offsets as possible. We are not talking about gigabytes here, right?
Even if technically they are 'separate sections', the x86 bug shows that they aren't. So we should not pretend so on the Linux side either and we should not tear them apart (and then work hard to preserve the interdependencies, some declared, some hidden!).
If we allocate the EFI runtime as a single virtual memory block then issues like rounding between sections does not even come up as a problem: we map the original offsets and sizes byte by byte.
Thanks,
Ingo
Re: [PATCH 1/2] x86/efi: Map EFI memmap entries in-order at runtime
In reply to Ingo MolnarQuoted 54 lines · Ingo Molnar
So this commit worries me.
This bug is a good find, and the fix is obviously needed and urgent, but I'm not sure about the implementation at all. (I've Cc:-ed a few more x86 low level gents.)
Quoted 45 lines · Matt Fleming
/*
+ * Iterate the EFI memory map in reverse order because the regions
+ * will be mapped top-down. The end result is the same as if we had
+ * mapped things forward, but doesn't require us to change the
+ * existing implementation ofefi_map_region().
+ */arch/x86/platform/efi/efi.c +39
-0+static inline void *efi_map_next_entry_reverse (void *entry) +{ +/* Initial call */ +if (!entry) +return memmap.map_end - memmap.desc_size; + + entry -= memmap.desc_size; +if (entry < memmap.map) +return NULL ; + +return entry; +} + +/* +* efi_map_next_entry - Return the next EFI memory map descriptor +* @entry: Previous EFI memory map descriptor +* +* This is a helper function to iterate over the EFI memory map, which +* we do in different orders depending on the current configuration. +* +* To begin traversing the memory map @entry must be %NULL. +* +* Returns %NULL when we reach the end of the memory map. +*/ +static void *efi_map_next_entry (void *entry) +{ +if (!efi_enabled (EFI_OLD_MEMMAP ) &&efi_enabled (EFI_64BIT )) { +/* +* Starting in UEFI v2.5 the EFI_PROPERTIES_TABLE +* config table feature requires us to map all entries +* in the same order as they appear in the EFI memory +* map. That is to say, entry N must have a lower +* virtual address than entry N+1. This is because the +* firmware toolchain leaves relative references in +* the code/data sections, which are split and become +* separate EFI memory regions. Mapping things +* out-of-order leads to the firmware accessing +* unmapped addresses. +*
Quoted 9 lines
arch/x86/platform/efi/efi.c out: · line 755
Quoted 4 lines
arch/x86/platform/efi/efi.c +4
-0+* Since we need to map things this way whether or not +* the kernel actually makes use of +* EFI_PROPERTIES_TABLE, let's just switch to this +* scheme by default for 64-bit. The thing is, if relative accesses between these 'sections' do happen then the requirement is much stronger than just 'ordered by addresses' - then we must map them continuously and as a single block!
The primary difference between pre-2.5 and 2.5 with this feature enabled is that formerly, each PE/COFF image in memory would be covered by at most a single EfiRuntimeServicesCode region, and now, a single PE/COFF image may be split into different regions. It is only relative references *inside* such a PE/COFF image that we are concerned about, since no symbol references exist between separate ones.
Also, it is not only relative references inside the PE/COFF image that cause the problem. Another aspect is that the offset that is applied to all absolute references at relocation time is derived from the offset of the base of the PE/COFF image. If part of the PE/COFF image (the .data section) is moved relatively to the code section, these absolute references are fixed up incorrectly. This is actually a problem that we could solve at the firmware side, but since PE/COFF does not really tolerate being split up like that, the correct fix is to keep all regions belonging to a single PE/COFF image adjacent. Since we can't tell which those regions are, the next best approach is to keep all adjacent regions with the EFI_MEMORY_RUNTIME attribute adjacent in the VA mapping.
Signature · 1 line
Ard.
Re: [PATCH 2/2] arm64/efi: Don't pad between EFI_MEMORY_RUNTIME regions
In reply to Ingo MolnarQuoted 26 lines · Ingo Molnar
Quoted 17 lines · Matt Fleming
From: Ard Biesheuvel <[email protected]>
The new Properties Table feature introduced in UEFIv2.5 may split memory regions that cover PE/COFF memory images into separate code and data regions. Since these regions only differ in the type (runtime code vs runtime data) and the permission bits, but not in the memory type attributes (UC/WC/WT/WB), the spec does not require them to be aligned to 64 KB.
Since the relative offset of PE/COFF .text and .data segments cannot be changed on the fly, this means that we can no longer pad out those regions to be mappable using 64 KB pages.
Unfortunately, there is no annotation in the UEFI memory map that identifies data regions that were split off from a code region, so we must apply this logic to all adjacent runtime regions whose attributes only differ in the permission bits.Quoted 5 lines
So instead of rounding each memory region to 64 KB alignment at both ends, only round down regions that are not directly preceded by another runtime region with the same type attributes. Since the UEFI spec does not mandate that the memory map be sorted, this means we also need to sort it first.
Quoted 6 lines
So I think this is fundamentally wrong as well, similarly to the related x86 fix.
I think for compatibility reasons the whole 'EFI runtime image' should be mapped in a single go, as closely matching the EFI layouts and offsets as possible. We are not talking about gigabytes here, right?
As I explained in the other thread, this is really not necessary, and never has been until the firmware started splitting up PE/COFF images into several sections each. As long as we keep those PE/COFF images together, everything will work as before, and the only complication is that the memory map does not contain any clues about which regions belong to a single PE/COFF image, so we need to keep all adjacent EFI_MEMORY_RUNTIME regions adjacent in the VA mapping.
Quoted 5 lines
Even if technically they are 'separate sections', the x86 bug shows that they aren't. So we should not pretend so on the Linux side either and we should not tear them apart (and then work hard to preserve the interdependencies, some declared, some hidden!).
This is about relocations and interdependencies at the symbol level, and such interdependencies only exist internally inside PE/COFF images.
Quoted 4 lines
If we allocate the EFI runtime as a single virtual memory block then issues like rounding between sections does not even come up as a problem: we map the original offsets and sizes byte by byte.
Well, by that reasoning, we should not call SetVirtualAddressMap() in the first place, and just use the 1:1 mapping UEFI uses natively. This is more than feasible on arm64, and I actually fought hard against using SetVirtualAddressMap() at all, but I was overruled by others. I think this is also trivially possible on X64, since the 1:1 mapping is already active alongside the VA mapping.
Signature · 1 line
Ard.
Re: [PATCH 1/2] x86/efi: Map EFI memmap entries in-order at runtime
In reply to Ingo MolnarQuoted 6 lines · Ingo Molnar
So this commit worries me.
This bug is a good find, and the fix is obviously needed and urgent, but I'm not sure about the implementation at all. (I've Cc:-ed a few more x86 low level gents.)
Thanks, the more the merrier.
Quoted 56 lines · Matt Fleming
Quoted 40 lines
/*
+ * Iterate the EFI memory map in reverse order because the regions
+ * will be mapped top-down. The end result is the same as if we had
+ * mapped things forward, but doesn't require us to change the
+ * existing implementation ofefi_map_region().
+ */arch/x86/platform/efi/efi.c +33
-0+static inline void *efi_map_next_entry_reverse (void *entry) +{ +/* Initial call */ +if (!entry) +return memmap.map_end - memmap.desc_size; + + entry -= memmap.desc_size; +if (entry < memmap.map) +return NULL ; + +return entry; +} + +/* +* efi_map_next_entry - Return the next EFI memory map descriptor +* @entry: Previous EFI memory map descriptor +* +* This is a helper function to iterate over the EFI memory map, which +* we do in different orders depending on the current configuration. +* +* To begin traversing the memory map @entry must be %NULL. +* +* Returns %NULL when we reach the end of the memory map. +*/ +static void *efi_map_next_entry (void *entry) +{ +if (!efi_enabled (EFI_OLD_MEMMAP ) &&efi_enabled (EFI_64BIT )) { +/* +* Starting in UEFI v2.5 the EFI_PROPERTIES_TABLE +* config table feature requires us to map all entries +* in the same order as they appear in the EFI memory +* map. That is to say, entry N must have a lower +* virtual address than entry N+1. This is because the arch/x86/platform/efi/efi.c out: · line 755
Quoted 10 lines
arch/x86/platform/efi/efi.c +10
-0+* firmware toolchain leaves relative references in +* the code/data sections, which are split and become +* separate EFI memory regions. Mapping things +* out-of-order leads to the firmware accessing +* unmapped addresses. +* +* Since we need to map things this way whether or not +* the kernel actually makes use of +* EFI_PROPERTIES_TABLE, let's just switch to this +* scheme by default for 64-bit. The thing is, if relative accesses between these 'sections' do happen then the requirement is much stronger than just 'ordered by addresses' - then we must map them continuously and as a single block!
Quoted 1 line
So at minimum the comment should say that. But I think we want more:
Well, the firmware doesn't place arbitrary gaps between these runtime image sections in memory, they still get loaded into memory by the firmware the old-fashioned way (as one contiguous blob). It's just that we've now got two memory map entries for the single PE/COFF image. Also, it's only sections from the same PE/COFF image that reference each other (but like Ard said, we can't tell from the memmap which entries are for a single PE/COFF image).
Because EFI memory map entries that are part of the same PE/COFF image will be at consecutive addresses, we'll also map them contiguously in the kernel virtual address space, because that's how the current code works - it's just that the current code maps them backwards.
Yeah, I'm completely in favour of improving any of the comments.
Quoted 23 lines
Quoted 23 lines
arch/x86/platform/efi/efi.c +16
-0+*/ +return efi_map_next_entry_reverse (entry); + } + +/* Initial call */ +if (!entry) +return memmap.map; + + entry += memmap.desc_size; +if (entry >= memmap.map_end) +return NULL ; + +return entry; +} + +/* * Map the efi memory ranges of the runtime services and update new_mmap with * virtual addresses. */ @@ -714,7 +778,8 @@ static void * __init efi_map_regions(int *count, int *pg_shift)unsigned long left =0 ;efi_memory_desc_t *md;
Quoted 8 lines
arch/x86/platform/efi/efi.c static void * __init efi_map_regions(int *count, int *pg_shift) · line 784
Quoted 5 lines
arch/x86/platform/efi/efi.c +2
-1-for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { + p =NULL ; +while ((p =efi_map_next_entry (p))) { md = p;if (!(md->attribute &EFI_MEMORY_RUNTIME )) {So why is this 64-bit only? Is 32-bit not affected because there we allocate virtual addresses bottom-up?
32-bit would potentially be effected if 32-bit firmware ever enabled the EFI_PROPERTIES_TABLE feature. Whether or not it worked would depend on the fragemntation of the virtual memory space when we map the EFI regions.
But I'm not aware of any 32-bit firmware shipping with Properties Table support, and fixing it in the kernel like we do for x86-64 would require changing the EFI region mapping code. That's something I wanted to avoid for this patch since it needs to be applied to stable, and especially when we haven't seen the feature being used in the wild.
Quoted 7 lines
This would be a lot clearer if we just mapped the entries in order, no questions asked. Conditions like this:
arch/x86/platform/efi/efi.c out: · line 739
Quoted 1 line
arch/x86/platform/efi/efi.c +1
-0+if (!efi_enabled (EFI_OLD_MEMMAP ) &&efi_enabled (EFI_64BIT )) {... just invite confusion and possible corner cases where we end up mapping them wrong.
When we introduced the top-down scheme in commit d2f7cbe7b26a ("x86/efi: Runtime services virtual mapping") we also introduced the "efi=old_map" kernel parameter to force the old virtual address allocation scheme exactly because we were concerned we might run into firmware "issues" with the way we mapped things.
That concern hasn't gone away now - it's intensified.
The above conditional just codifies a config combination that we've always treated differently, and the conditional logic is only necessary because this is generic x86 code.
I agree that it would be nicer without the conditional code, but forcing the same allocation scheme across 32-bit and 64-bit is a *much* bigger change than what is proposed here. Not just in terms of patch size, but also in terms of risk.
Quoted 4 lines
So could we make the whole code obviously bottom-up? Such as first calculating the size of virtual memory needed, then allocating a _single_, obviously continuous mapping, and then doing a very clear in-order mapping within that window? That would remove any bitness and legacy dependencies.
So, we could, and in fact the first version of this patch did just that. You can find it here,
https://lkml.kernel.org/r/[email protected]
But Ard suggested re-using the existing code and simply changing the order we map the memmap entries in. And given the constraint for a small patch for backporting, I think it's a better solution. The actual virtual addresses we pick are exactly the same with the two patches.
Signature · 1 line
Matt Fleming, Intel Open Source Technology Center
Re: [PATCH 1/2] x86/efi: Map EFI memmap entries in-order at runtime
In reply to Ingo MolnarQuoted 20 lines · Ingo Molnar
So this commit worries me.
This bug is a good find, and the fix is obviously needed and urgent, but I'm not sure about the implementation at all. (I've Cc:-ed a few more x86 low level gents.)
Quoted 12 lines · Matt Fleming
arch/x86/platform/efi/efi.c +12
-0+/* +* Starting in UEFI v2.5 the EFI_PROPERTIES_TABLE +* config table feature requires us to map all entries +* in the same order as they appear in the EFI memory +* map. That is to say, entry N must have a lower +* virtual address than entry N+1. This is because the +* firmware toolchain leaves relative references in +* the code/data sections, which are split and become +* separate EFI memory regions. Mapping things +* out-of-order leads to the firmware accessing +* unmapped addresses. +*
I'm clearly missing something. What is EFI doing that it doesn't care how big the gap between sections is but it still requires them to be in order? It's not as though x86_64 has an addressing mode that allows only non-negative offsets.
--Andy
H. Peter Anvin
reviewerRe: [PATCH 1/2] x86/efi: Map EFI memmap entries in-order at runtime
I think it "works" because the affected BIOSes don't put spaces between the chunks. I have discussed this with Matt.
Quoted 30 lines · Andy Lutomirski
Quoted 4 lines · Ingo Molnar
So this commit worries me.
This bug is a good find, and the fix is obviously needed and urgent,
but I'm not
Quoted 1 line
sure about the implementation at all. (I've Cc:-ed a few more x86 low
level
Quoted 15 lines
gents.)
Quoted 12 lines · Matt Fleming
arch/x86/platform/efi/efi.c +12
-0+/* +* Starting in UEFI v2.5 the EFI_PROPERTIES_TABLE +* config table feature requires us to map all entries +* in the same order as they appear in the EFI memory +* map. That is to say, entry N must have a lower +* virtual address than entry N+1. This is because the +* firmware toolchain leaves relative references in +* the code/data sections, which are split and become +* separate EFI memory regions. Mapping things +* out-of-order leads to the firmware accessing +* unmapped addresses. +* I'm clearly missing something. What is EFI doing that it doesn't care how big the gap between sections is but it still requires them to be in order? It's not as though
x86_64has an addressing mode that allows only non-negative offsets.--Andy
Signature · 1 line
Sent from my Android device with K-9 Mail. Please excuse my brevity.
Re: [PATCH 1/2] x86/efi: Map EFI memmap entries in-order at runtime
Quoted 2 lines · H. Peter Anvin
I think it "works" because the affected BIOSes don't put spaces between the chunks. I have discussed this with Matt.
Forgive the ASCII art but perhaps an illustration might help:
before the 2.5 feature, PE/COFF runtime images were remapped as illustrated here:
PA VA
+---------------+ +---------------+ | | | | | PE/COFF .text | | EFI | | | | Runtime | +- - - - - - - -+ => | Services |----+ | | | Code | | : : | PE/COFF .data | | | | : : | | | | | +---------------+ +---------------+ +---------------+ | | | | | | | | | EFI | : : : : | | Runtime | : : : : +--->| Services | | | | | | Code | +---------------+ +---------------+ | | | | | | | | | PE/COFF .text | | EFI | +---------------+ | | | Runtime | : gap : +- - - - - - - -+ => | Services |---+ +---------------+ | | | Code | | | | | PE/COFF .data | | | | | EFI | | | | | | | Runtime | +---------------+ +---------------+ +---->| Services | | | | | | Code | : : : : | | : : : : | | : : : : +---------------+ : : : : : :
Since the affected symbol references only exist between PE/COFF .text and PE/COFF .data, there is never a problem since each is PE/COFF image is mapped as a single region.
However, with the new feature enabled, this no longer holds:
PA VA
+---------------+ +---------------+ | | | | | PE/COFF .text | | RtServices |----+ | | | Code | | +- - - - - - - -+ => +---------------+ | +---------------+ | | | RtServices | +--->| RtServices | | PE/COFF .data | | Data | | Code | | | | |----+ +---------------+ +---------------+ +---------------+ | : gap : | | | | | +---------------+ : : : : +--->| RtServices | : : : : | Data | | | | | +---------------+ +---------------+ +---------------+ : gap : | | | | +---------------+ | PE/COFF .text | | RtServices |-------->| RtServices | | | | Code | | Code | +- - - - - - - -+ => +---------------+ +---------------+ | | | RtServices | : gap : | PE/COFF .data | | Data |---+ +---------------+ | | | | | | RtServices | +---------------+ +---------------+ +---->| Data | | | | | | | : : : : +---------------+ : : : : : : : : : : : :
The illustration uses gaps, but obviously, this applies equally to inverting the mapping order, since the PE/COFF .text and .data sections will end up out of order.
Signature · 1 line
Ard.
Quoted 34 lines · Andy Lutomirski
Quoted 24 lines · Ingo Molnar
Quoted 4 lines
So this commit worries me.
This bug is a good find, and the fix is obviously needed and urgent,
but I'm not
Quoted 1 line
sure about the implementation at all. (I've Cc:-ed a few more x86 low
level
Quoted 15 lines
gents.)
Quoted 12 lines · Matt Fleming
arch/x86/platform/efi/efi.c +12
-0+/* +* Starting in UEFI v2.5 the EFI_PROPERTIES_TABLE +* config table feature requires us to map all entries +* in the same order as they appear in the EFI memory +* map. That is to say, entry N must have a lower +* virtual address than entry N+1. This is because the +* firmware toolchain leaves relative references in +* the code/data sections, which are split and become +* separate EFI memory regions. Mapping things +* out-of-order leads to the firmware accessing +* unmapped addresses. +* Quoted 6 lines
I'm clearly missing something. What is EFI doing that it doesn't care how big the gap between sections is but it still requires them to be in order? It's not as though
x86_64has an addressing mode that allows only non-negative offsets.--Andy
--
Sent from my Android device with K-9 Mail. Please excuse my brevity.
H. Peter Anvin
reviewerRe: [PATCH 1/2] x86/efi: Map EFI memmap entries in-order at runtime
It is still a hack unless all relative offsets are preserved. That is actually simpler, even: no sorting necessary.
Quoted 74 lines · Ard Biesheuvel
Quoted 1 line · H. Peter Anvin
I think it "works" because the affected BIOSes don't put spaces
between the chunks. I have discussed this with Matt.
Quoted 1 line
Forgive the ASCII art but perhaps an illustration might help:
before the 2.5 feature, PE/COFF runtime images were remapped as illustrated here:
PA VA
+---------------+ +---------------+ | | | | | PE/COFF .text | | EFI | | | | Runtime | +- - - - - - - -+ => | Services |----+ | | | Code | | : : | PE/COFF .data | | | | : : | | | | | +---------------+ +---------------+ +---------------+ | | | | | | | | | EFI | : : : : | | Runtime | : : : : +--->| Services | | | | | | Code | +---------------+ +---------------+ | | | | | | | | | PE/COFF .text | | EFI | +---------------+ | | | Runtime | : gap : +- - - - - - - -+ => | Services |---+ +---------------+ | | | Code | | | | | PE/COFF .data | | | | | EFI | | | | | | | Runtime | +---------------+ +---------------+ +---->| Services | | | | | | Code | : : : : | | : : : : | | : : : : +---------------+ : : : : : :Since the affected symbol references only exist between PE/COFF .text and PE/COFF .data, there is never a problem since each is PE/COFF image is mapped as a single region.
However, with the new feature enabled, this no longer holds:
PA VA+---------------+ +---------------+ | | | | | PE/COFF .text | | RtServices |----+ | | | Code | | +- - - - - - - -+ => +---------------+ | +---------------+ | | | RtServices | +--->| RtServices | | PE/COFF .data | | Data | | Code | | | | |----+ +---------------+ +---------------+ +---------------+ | : gap : | | | | | +---------------+ : : : : +--->| RtServices | : : : : | Data | | | | | +---------------+ +---------------+ +---------------+ : gap : | | | | +---------------+ | PE/COFF .text | | RtServices |-------->| RtServices | | | | Code | | Code | +- - - - - - - -+ => +---------------+ +---------------+ | | | RtServices | : gap : | PE/COFF .data | | Data |---+ +---------------+ | | | | | | RtServices | +---------------+ +---------------+ +---->| Data | | | | | | | : : : : +---------------+ : : : : : : : : : : : :The illustration uses gaps, but obviously, this applies equally to inverting the mapping order, since the PE/COFF .text and .data sections will end up out of order.
Signature · 1 line
Sent from my Android device with K-9 Mail. Please excuse my brevity.
Re: [PATCH 1/2] x86/efi: Map EFI memmap entries in-order at runtime
In reply to H. Peter AnvinQuoted 3 lines · H. Peter Anvin
I think it "works" because the affected BIOSes don't put spaces between the chunks. I have discussed this with Matt.
Right, that's very true. Though note that the current mapping algorithm will handle a gap <= PMD_SIZE, it's just anything larger than PMD_SIZE we "squash" to the next multiple of PMD_SIZE.
It's unclear whether the firmware toolchains would even support references between sections with a PMD_SIZE gap between them, and I think the firmware engineers would have to go out of their way to actually insert such a gap.
Signature · 1 line
Matt Fleming, Intel Open Source Technology Center
Re: [PATCH 1/2] x86/efi: Map EFI memmap entries in-order at runtime
In reply to H. Peter AnvinQuoted 3 lines · H. Peter Anvin
It is still a hack unless all relative offsets are preserved. That is actually simpler, even: no sorting necessary.
Unless I'm missing something, preserving relative offsets is exactly what we do today, modulo PMD_SIZE gaps.
Signature · 1 line
Matt Fleming, Intel Open Source Technology Center
Re: [PATCH 1/2] x86/efi: Map EFI memmap entries in-order at runtime
Quoted 11 lines · Matt Fleming
Quoted 5 lines · H. Peter Anvin
It is still a hack unless all relative offsets are preserved. That is actually simpler, even: no sorting necessary.
Unless I'm missing something, preserving relative offsets is exactly what we do today, modulo
PMD_SIZEgaps.
I think what Peter means is preserving the relative offsets inside the entire 1:1 space.
This is not at all what we do currently, and i don't think it is generally feasible on 32-bit (since the physical range may conflict with the virtual kernel mappings)
However, on 64 bit (both arm and x86), this boils down to not calling setVA() in the first place, which i'm all in favor of.
Signature · 1 line
Ard.
Quoted 2 lines
Signature · 1 line
Matt Fleming, Intel Open Source Technology Center
H. Peter Anvin
reviewerRe: [PATCH 1/2] x86/efi: Map EFI memmap entries in-order at runtime
Sadly a lot of firmware is known to fail in that configuration :( That was very much our guest choice.
I don't actually think it is all that infeasible to keep relative offsets consistent for the regions we have to map. PMD_SIZE is not a very large chunk so it could be a problem.
Quoted 22 lines · Ard Biesheuvel
Quoted 2 lines
On 26 sep. 2015, at 12:57, Matt Fleming <[email protected]>
Quoted 9 lines · wrote:
Quoted 5 lines · H. Peter Anvin
It is still a hack unless all relative offsets are preserved. That is actually simpler, even: no sorting necessary.
Unless I'm missing something, preserving relative offsets is exactly what we do today, modulo
PMD_SIZEgaps.I think what Peter means is preserving the relative offsets inside the entire 1:1 space.
This is not at all what we do currently, and i don't think it is generally feasible on 32-bit (since the physical range may conflict with the virtual kernel mappings)
However, on 64 bit (both arm and x86), this boils down to not calling
setVA()in the first place, which i'm all in favor of.
Signature · 1 line
Sent from my Android device with K-9 Mail. Please excuse my brevity.
Re: [PATCH 1/2] x86/efi: Map EFI memmap entries in-order at runtime
In reply to Andy LutomirskiQuoted 22 lines · Andy Lutomirski
Quoted 20 lines · Ingo Molnar
So this commit worries me.
This bug is a good find, and the fix is obviously needed and urgent, but I'm not sure about the implementation at all. (I've Cc:-ed a few more x86 low level gents.)
Quoted 12 lines · Matt Fleming
arch/x86/platform/efi/efi.c +12
-0+/* +* Starting in UEFI v2.5 the EFI_PROPERTIES_TABLE +* config table feature requires us to map all entries +* in the same order as they appear in the EFI memory +* map. That is to say, entry N must have a lower +* virtual address than entry N+1. This is because the +* firmware toolchain leaves relative references in +* the code/data sections, which are split and become +* separate EFI memory regions. Mapping things +* out-of-order leads to the firmware accessing +* unmapped addresses. +*
Quoted 3 lines
I'm clearly missing something. What is EFI doing that it doesn't care how big the gap between sections is but it still requires them to be in order? It's not as though
x86_64has an addressing mode that allows only non-negative offsets.
It appears the problem is that what we think to be 'different sections' are in reality smaller parts of the same section.
Any relative address calculation will be broken if we don't preserve the relative positions of these sections/sub-sections. Any CPU that supports addition is affected, it doesn't need any special addressing modes.
Thanks,
Ingo
Re: [PATCH 1/2] x86/efi: Map EFI memmap entries in-order at runtime
In reply to Matt FlemingQuoted 5 lines · Matt Fleming
Quoted 4 lines
So could we make the whole code obviously bottom-up? Such as first calculating the size of virtual memory needed, then allocating a _single_, obviously continuous mapping, and then doing a very clear in-order mapping within that window? That would remove any bitness and legacy dependencies.
Quoted 7 lines
So, we could, and in fact the first version of this patch did just that. You can find it here,
But Ard suggested re-using the existing code and simply changing the order we map the memmap entries in.
Such implementational arguments (which are an internal cost) never trump compatibility and robustness concerns (which are an external constraint).
Quoted 2 lines
[...] And given the constraint for a small patch for backporting, I think it's a better solution. [...]
Ugh, backporting size is _even less_ of a valid argument when it comes to firmware support correctness!
We can (perhaps) use these already existing patches as a simpler backport, but there's absolutely no reason to keep that code as a solution:
Quoted 2 lines
[...] The actual virtual addresses we pick are exactly the same with the two patches.
So I'm NAK-ing this for now:
- The code is it reads today pretends to be an 'allocator'. It is _NOT_ an allocator, because all the sections have already been determined by the firmware, and, as we just learned the hard way, we do not want to deviate from that! There's nothing to 'allocate'!
What these patches seem to implement is an elaborate 'allocator' that ends up doing nothing on 'new 64-bit' ...
- The 32-bit and 64-bit and 'old_mmap' asymmetries:
if (!efi_enabled(EFI_OLD_MEMMAP) && efi_enabled(EFI_64BIT)) { seem fragile and nonsensical. The question is: is it possible for the whole EFI image to be larger than a couple of megabytes? If not then 32-bit should just mirror the firmware layout as well, and if EFI_OLD_MEMMAP does anything differently from this _obvious_ 1:1 mapping of the EFI memory offsets then it's not worth keeping as a legacy, because there's just nothing better than mirroring the firmware layout.
My suggestion would be to just 1:1 map what the EFI tables describe, modulo the single absolute offset by which we shift the whole thing to a single base.
Is there any technical reason why we'd want to deviate from that? Gigabytes of tables or gigabytes of holes that 32-bit cannot handle? Firmware that wants an OS layout that differs from the firmware layout?
Also, nobody seems to be asking the obvious hardware compatibility question when trying to implement a standard influenced in great part by an entity that is partly ignorant of and partly hostile to Linux: how does Windows map the EFI sections, under what OSs are these firmware versions tested? I suspect no firmware is released that crashes on bootup on all OSs that can run on that hardware, right?
Thanks,
Ingo
Re: [PATCH 2/2] arm64/efi: Don't pad between EFI_MEMORY_RUNTIME regions
In reply to Ard BiesheuvelQuoted 10 lines · Ard Biesheuvel
Quoted 3 lines
If we allocate the EFI runtime as a single virtual memory block then issues like rounding between sections does not even come up as a problem: we map the original offsets and sizes byte by byte.
Well, by that reasoning, we should not call
SetVirtualAddressMap()in the first place, and just use the 1:1 mapping UEFI uses natively. This is more than feasible on arm64, and I actually fought hard against usingSetVirtualAddressMap()at all, but I was overruled by others. I think this is also trivially possible on X64, since the 1:1 mapping is already active alongside the VA mapping.
Could we please re-list all the arguments pro and contra of 1:1 physical mappings, in a post that also explains the background so that more people can chime in, not just people versed in EFI internals? It's very much possible that a bad decision was made.
Thanks,
Ingo
Borislav Petkov
maintainerRe: [PATCH 2/2] arm64/efi: Don't pad between EFI_MEMORY_RUNTIME regions
Quoted 4 lines · Ingo Molnar
Could we please re-list all the arguments pro and contra of 1:1 physical mappings, in a post that also explains the background so that more people can chime in, not just people versed in EFI internals? It's very much possible that a bad decision was made.
The main reason why we did the additional, top-down mapping was kexec kernel wanting to use UEFI runtime facilities too and the braindead design of SetVirtualAddressMap() being callable only once per system boot. So we had to have stable mappings which are valid in the kexec-ed kernel too.
But this was long time ago and I most certainly have forgotten all the details.
And now I'm wondering why didn't we do the 1:1 thing and rebuild the exact same EFI pagetable in the kexec-ed kernel? Because when we do an EFI call, we switch to the special pagetable so why didn't we make the kexec-ed kernel rebuild the 1:1 pagetable which it can use for EFI calls...
Hmm, again, I've forgotten a lot of details so I'm sure Matt will come in and say "No, you can't do that because..."
Signature · 4 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.Re: [PATCH 1/2] x86/efi: Map EFI memmap entries in-order at runtime
In reply to H. Peter AnvinQuoted 3 lines · H. Peter Anvin
Sadly a lot of firmware is known to fail in that configuration :( That was very much our guest choice.
Why can't we map everything completely 1:1 (VA = PA) and call the setVA thing but pass it literally the identity.
Firmwares that need it to be called will work, and firmwares that fail to update all offsets will be fine because there's nothing to update.
--Andy
Re: [PATCH 1/2] x86/efi: Map EFI memmap entries in-order at runtime
Quoted 7 lines · Andy Lutomirski
Quoted 3 lines · H. Peter Anvin
Sadly a lot of firmware is known to fail in that configuration :( That was very much our guest choice.
Why can't we map everything completely 1:1 (VA = PA) and call the setVA thing but pass it literally the identity.
Last time I tried this I found that some firmware makes assumptions about having high addresses.
Signature · 1 line
Matthew Garrett | [email protected]
Re: [PATCH 1/2] x86/efi: Map EFI memmap entries in-order at runtime
Quoted 11 lines · Matthew Garrett
Quoted 7 lines · Andy Lutomirski
Quoted 3 lines · H. Peter Anvin
Sadly a lot of firmware is known to fail in that configuration :( That was very much our guest choice.
Why can't we map everything completely 1:1 (VA = PA) and call the setVA thing but pass it literally the identity.
Last time I tried this I found that some firmware makes assumptions about having high addresses.
So the question is, what does Windows do?
PC firmware is a hostile environment for Linux, to be compatible the best we can do is to mimic the environment that the firmware is tested under - i.e. try to use the firmware in the way Windows uses it.
Thanks,
Ingo
Re: [PATCH 2/2] arm64/efi: Don't pad between EFI_MEMORY_RUNTIME regions
In reply to Borislav PetkovQuoted 12 lines · Borislav Petkov
Quoted 4 lines · Ingo Molnar
Could we please re-list all the arguments pro and contra of 1:1 physical mappings, in a post that also explains the background so that more people can chime in, not just people versed in EFI internals? It's very much possible that a bad decision was made.
The main reason why we did the additional, top-down mapping was kexec kernel wanting to use UEFI runtime facilities too and the braindead design of
SetVirtualAddressMap()being callable only once per system boot. So we had to have stable mappings which are valid in the kexec-ed kernel too.
Quoted 6 lines
But this was long time ago and I most certainly have forgotten all the details.
And now I'm wondering why didn't we do the 1:1 thing and rebuild the exact same EFI pagetable in the kexec-ed kernel? Because when we do an EFI call, we switch to the special pagetable so why didn't we make the kexec-ed kernel rebuild the 1:1 pagetable which it can use for EFI calls...
Yeah.
Quoted 2 lines
Hmm, again, I've forgotten a lot of details so I'm sure Matt will come in and say "No, you can't do that because..."
Would be nice to re-examine all this.
Thanks,
Ingo
Re: [PATCH 1/2] x86/efi: Map EFI memmap entries in-order at runtime
In reply to Ingo MolnarQuoted 1 line · Ingo Molnar
So the question is, what does Windows do?
It's pretty trivial to hack OVMF to dump the SetVirtualAddressMap() arguments to the qemu debug port. Unfortunately I'm about to drop mostly offline for a week, otherwise I'd give it a go...
Signature · 1 line
Matthew Garrett | [email protected]
Re: [PATCH 1/2] x86/efi: Map EFI memmap entries in-order at runtime
In reply to Ingo MolnarQuoted 3 lines · Ingo Molnar
* Matt Fleming <[email protected]> wrote:
[...]
Quoted 24 lines
Quoted 2 lines
[...] The actual virtual addresses we pick are exactly the same with the two patches.
So I'm NAK-ing this for now:
- The code is it reads today pretends to be an 'allocator'. It is _NOT_ an allocator, because all the sections have already been determined by the firmware, and, as we just learned the hard way, we do not want to deviate from that! There's nothing to 'allocate'!
What these patches seem to implement is an elaborate 'allocator' that ends up doing nothing on 'new 64-bit' ...
- The 32-bit and 64-bit and '
old_mmap' asymmetries:if (!efi_enabled(EFI_OLD_MEMMAP) && efi_enabled(EFI_64BIT)) {seem fragile and nonsensical. The question is: is it possible for the whole EFI image to be larger than a couple of megabytes? If not then 32-bit should just mirror the firmware layout as well, and if
EFI_OLD_MEMMAPdoes anything differently from this _obvious_ 1:1 mapping of the EFI memory offsets then it's not worth keeping as a legacy, because there's just nothing better than mirroring the firmware layout.
Quoted 7 lines
My suggestion would be to just 1:1 map what the EFI tables describe, modulo the single absolute offset by which we shift the whole thing to a single base.
Is there any technical reason why we'd want to deviate from that? Gigabytes of tables or gigabytes of holes that 32-bit cannot handle? Firmware that wants an OS layout that differs from the firmware layout?
The combined EFI_MEMORY_RUNTIME regions could span the entire 1:1 addressable PA space. They usually don't but it is a possibility, which means 32-bit will not generally be able to support this approach. For 64-bit ARM, there are some minor complications when the base of RAM is up very high in physical memory, but we already fixed that for the boot time ID map and for KVM.
Quoted 7 lines
Also, nobody seems to be asking the obvious hardware compatibility question when trying to implement a standard influenced in great part by an entity that is partly ignorant of and partly hostile to Linux: how does Windows map the EFI sections, under what OSs are these firmware versions tested? I suspect no firmware is released that crashes on bootup on all OSs that can run on that hardware, right?
Interestingly, it was the other way around this time. The engineers that implemented this feature for EDK2 could not boot Windows 8 anymore, because it supposedly maps the regions in reverse order as well (and MS too will need to backport a fix that inverts the mapping order). The engineers also tested Linux/x86, by means of a SUSE installer image, which booted fine, most likely due to the fact that it is an older version which still uses the old memmap layout.
My concern with all of this is that this security feature will become an obscure opt-in feature rather than something UEFIv2.5 firmware implementations can enable by default.
Signature · 1 line
Ard.
Re: [PATCH 1/2] x86/efi: Map EFI memmap entries in-order at runtime
Quoted 32 lines · Ard Biesheuvel
Quoted 3 lines · Ingo Molnar
* Matt Fleming <matt-mF/unelCI9GS6iBeEJttW/[email protected]> wrote:
[...]
Quoted 27 lines
Quoted 2 lines
[...] The actual virtual addresses we pick are exactly the same with the two patches.
So I'm NAK-ing this for now:
- The code is it reads today pretends to be an 'allocator'. It is _NOT_ an allocator, because all the sections have already been determined by the firmware, and, as we just learned the hard way, we do not want to deviate from that! There's nothing to 'allocate'!
What these patches seem to implement is an elaborate 'allocator' that ends up doing nothing on 'new 64-bit' ...
- The 32-bit and 64-bit and '
old_mmap' asymmetries:if (!efi_enabled(EFI_OLD_MEMMAP) && efi_enabled(EFI_64BIT)) {seem fragile and nonsensical. The question is: is it possible for the whole EFI image to be larger than a couple of megabytes? If not then 32-bit should just mirror the firmware layout as well, and if
EFI_OLD_MEMMAPdoes anything differently from this _obvious_ 1:1 mapping of the EFI memory offsets then it's not worth keeping as a legacy, because there's just nothing better than mirroring the firmware layout.My suggestion would be to just 1:1 map what the EFI tables describe, modulo the single absolute offset by which we shift the whole thing to a single base.
Quoted 8 lines
Quoted 4 lines
Is there any technical reason why we'd want to deviate from that? Gigabytes of tables or gigabytes of holes that 32-bit cannot handle? Firmware that wants an OS layout that differs from the firmware layout?
The combined
EFI_MEMORY_RUNTIMEregions could span the entire 1:1 addressable PA space. They usually don't but it is a possibility, which means 32-bit will not generally be able to support this approach. [...]
Ok, that's a good argument which invalidates my NAK.
Quoted 11 lines
[...] For 64-bit ARM, there are some minor complications when the base of RAM is up very high in physical memory, but we already fixed that for the boot time ID map and for KVM.
Quoted 6 lines
Also, nobody seems to be asking the obvious hardware compatibility question when trying to implement a standard influenced in great part by an entity that is partly ignorant of and partly hostile to Linux: how does Windows map the EFI sections, under what OSs are these firmware versions tested? I suspect no firmware is released that crashes on bootup on all OSs that can run on that hardware, right?
Quoted 7 lines
Interestingly, it was the other way around this time. The engineers that implemented this feature for EDK2 could not boot Windows 8 anymore, because it supposedly maps the regions in reverse order as well (and MS too will need to backport a fix that inverts the mapping order). The engineers also tested Linux/x86, by means of a SUSE installer image, which booted fine, most likely due to the fact that it is an older version which still uses the old memmap layout.
That's nice to hear!
Quoted 3 lines
My concern with all of this is that this security feature will become an obscure opt-in feature rather than something UEFIv2.5 firmware implementations can enable by default.
Ok, so I think the patches are mostly fine after all, except that I don't think the condition on 64-bit makes any sense:
+ if (!efi_enabled(EFI_OLD_MEMMAP) && efi_enabled(EFI_64BIT)) {
I can see us being nervous wrt. backported patches, but is there any strong reason to not follow this up with a third (non-backported) patch that changes this to:
+ if (!efi_enabled(EFI_OLD_MEMMAP)) {
for v4.4?
Thanks,
Ingo
Re: [PATCH 1/2] x86/efi: Map EFI memmap entries in-order at runtime
Quoted 65 lines · Ingo Molnar
Quoted 32 lines · Ard Biesheuvel
Quoted 3 lines · Ingo Molnar
* Matt Fleming <matt-mF/unelCI9GS6iBeEJttW/[email protected]> wrote:
[...]
Quoted 27 lines
Quoted 2 lines
[...] The actual virtual addresses we pick are exactly the same with the two patches.
So I'm NAK-ing this for now:
- The code is it reads today pretends to be an 'allocator'. It is _NOT_ an allocator, because all the sections have already been determined by the firmware, and, as we just learned the hard way, we do not want to deviate from that! There's nothing to 'allocate'!
What these patches seem to implement is an elaborate 'allocator' that ends up doing nothing on 'new 64-bit' ...
- The 32-bit and 64-bit and '
old_mmap' asymmetries:if (!efi_enabled(EFI_OLD_MEMMAP) && efi_enabled(EFI_64BIT)) {seem fragile and nonsensical. The question is: is it possible for the whole EFI image to be larger than a couple of megabytes? If not then 32-bit should just mirror the firmware layout as well, and if
EFI_OLD_MEMMAPdoes anything differently from this _obvious_ 1:1 mapping of the EFI memory offsets then it's not worth keeping as a legacy, because there's just nothing better than mirroring the firmware layout.My suggestion would be to just 1:1 map what the EFI tables describe, modulo the single absolute offset by which we shift the whole thing to a single base.
Quoted 8 lines
Quoted 4 lines
Is there any technical reason why we'd want to deviate from that? Gigabytes of tables or gigabytes of holes that 32-bit cannot handle? Firmware that wants an OS layout that differs from the firmware layout?
The combined
EFI_MEMORY_RUNTIMEregions could span the entire 1:1 addressable PA space. They usually don't but it is a possibility, which means 32-bit will not generally be able to support this approach. [...]Ok, that's a good argument which invalidates my NAK.
Quoted 11 lines
[...] For 64-bit ARM, there are some minor complications when the base of RAM is up very high in physical memory, but we already fixed that for the boot time ID map and for KVM.
Quoted 6 lines
Also, nobody seems to be asking the obvious hardware compatibility question when trying to implement a standard influenced in great part by an entity that is partly ignorant of and partly hostile to Linux: how does Windows map the EFI sections, under what OSs are these firmware versions tested? I suspect no firmware is released that crashes on bootup on all OSs that can run on that hardware, right?
Quoted 7 lines
Interestingly, it was the other way around this time. The engineers that implemented this feature for EDK2 could not boot Windows 8 anymore, because it supposedly maps the regions in reverse order as well (and MS too will need to backport a fix that inverts the mapping order). The engineers also tested Linux/x86, by means of a SUSE installer image, which booted fine, most likely due to the fact that it is an older version which still uses the old memmap layout.
Quoted 7 lines
That's nice to hear!
Quoted 3 lines
My concern with all of this is that this security feature will become an obscure opt-in feature rather than something UEFIv2.5 firmware implementations can enable by default.
Ok, so I think the patches are mostly fine after all,
That is good to hear.
Quoted 12 lines
except that I don't think
the condition on 64-bit makes any sense:+ if (!efi_enabled(EFI_OLD_MEMMAP) &&
efi_enabled(EFI_64BIT)) {I can see us being nervous wrt. backported patches, but is there any strong reason to not follow this up with a third (non-backported) patch that changes this to:
+ if (!efi_enabled(EFI_OLD_MEMMAP)) {
for v4.4?
The 32-bit side essentially implements the old memmap only, which is the the bottom-up version. So old memmap will be implied by 32-bit but not set in the EFI flags, resulting in the reverse enumeration being used with the bottom-up mapping logic. The net result of that is that we create the same problem for 32-bit that we are trying to solve for 64-bit, i.e., the regions will end up in reverse order in the VA mapping.
To deobfuscate this particular conditional, we could set EFI_OLD_MEMMAP unconditionally on 32-bit x86. Or we could reshuffle variables and conditionals in various other way. I am not convinced that the overall end result will be any better though.
Signature · 1 line
Ard.
Re: [PATCH 1/2] x86/efi: Map EFI memmap entries in-order at runtime
Quoted 20 lines · Ard Biesheuvel
Quoted 12 lines
except that I don't think
the condition on 64-bit makes any sense:+ if (!efi_enabled(EFI_OLD_MEMMAP) &&
efi_enabled(EFI_64BIT)) {I can see us being nervous wrt. backported patches, but is there any strong reason to not follow this up with a third (non-backported) patch that changes this to:
+ if (!efi_enabled(EFI_OLD_MEMMAP)) {
for v4.4?
The 32-bit side essentially implements the old memmap only, which is the the bottom-up version. So old memmap will be implied by 32-bit but not set in the EFI flags, resulting in the reverse enumeration being used with the bottom-up mapping logic. The net result of that is that we create the same problem for 32-bit that we are trying to solve for 64-bit, i.e., the regions will end up in reverse order in the VA mapping.
Quoted 3 lines
To deobfuscate this particular conditional, we could set
EFI_OLD_MEMMAPunconditionally on 32-bit x86. Or we could reshuffle variables and conditionals in various other way.
Setting EFI_OLD_MEMMAP would be fine, if doing that has no bad side effects.
Quoted 1 line
[...] I am not convinced that the overall end result will be any better though.
That's not true, we change an obscure, implicit dependency on 32-bit detail to an explicit EFI_OLD_MEMMAP flag that shows exactly what's happening. That's a clear improvement.
Thanks,
Ingo
Re: [PATCH 2/2] arm64/efi: Don't pad between EFI_MEMORY_RUNTIME regions
In reply to Borislav PetkovQuoted 15 lines · Borislav Petkov
Quoted 4 lines · Ingo Molnar
Could we please re-list all the arguments pro and contra of 1:1 physical mappings, in a post that also explains the background so that more people can chime in, not just people versed in EFI internals? It's very much possible that a bad decision was made.
The main reason why we did the additional, top-down mapping was kexec kernel wanting to use UEFI runtime facilities too and the braindead design of
SetVirtualAddressMap()being callable only once per system boot. So we had to have stable mappings which are valid in the kexec-ed kernel too.But this was long time ago and I most certainly have forgotten all the details.
Quoted 9 lines
And now I'm wondering why didn't we do the 1:1 thing and rebuild the exact same EFI pagetable in the kexec-ed kernel? Because when we do an EFI call, we switch to the special pagetable so why didn't we make the kexec-ed kernel rebuild the 1:1 pagetable which it can use for EFI calls...
Hmm, again, I've forgotten a lot of details so I'm sure Matt will come in and say "No, you can't do that because..."
http://permalink.gmane.org/gmane.linux.kernel.efi/822
And more replies here:
http://comments.gmane.org/gmane.linux.kernel.efi/820
Thanks
Dave
Borislav Petkov
maintainerRe: [PATCH 2/2] arm64/efi: Don't pad between EFI_MEMORY_RUNTIME regions
Quoted 4 lines · Dave Young
http://permalink.gmane.org/gmane.linux.kernel.efi/822
And more replies here:
http://comments.gmane.org/gmane.linux.kernel.efi/820
Whatever we did think back then, it all is moot as long as some UEFI vendors do funky crap or the spec has documentation holes.
So we need a reality check and just do exactly what the OS does with which UEFI writers are testing with. Be it windoze, be it some open firmware testing tool or whatever. Provided there's even a tool with which UEFI validation is done and it is used by everybody...
Signature · 4 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.Re: [PATCH 1/2] x86/efi: Map EFI memmap entries in-order at runtime
In reply to Ingo MolnarQuoted 27 lines · Ingo Molnar
Quoted 20 lines · Ard Biesheuvel
Quoted 12 lines
except that I don't think
the condition on 64-bit makes any sense:+ if (!efi_enabled(EFI_OLD_MEMMAP) &&
efi_enabled(EFI_64BIT)) {I can see us being nervous wrt. backported patches, but is there any strong reason to not follow this up with a third (non-backported) patch that changes this to:
+ if (!efi_enabled(EFI_OLD_MEMMAP)) {
for v4.4?
The 32-bit side essentially implements the old memmap only, which is the the bottom-up version. So old memmap will be implied by 32-bit but not set in the EFI flags, resulting in the reverse enumeration being used with the bottom-up mapping logic. The net result of that is that we create the same problem for 32-bit that we are trying to solve for 64-bit, i.e., the regions will end up in reverse order in the VA mapping.
Quoted 3 lines
To deobfuscate this particular conditional, we could set
EFI_OLD_MEMMAPunconditionally on 32-bit x86. Or we could reshuffle variables and conditionals in various other way.
Quoted 8 lines
Setting
EFI_OLD_MEMMAPwould be fine, if doing that has no bad side effects.Quoted 1 line
[...] I am not convinced that the overall end result will be any better though.
That's not true, we change an obscure, implicit dependency on 32-bit detail to an explicit
EFI_OLD_MEMMAPflag that shows exactly what's happening. That's a clear improvement.
OK, fair enough. I agree that setting the flag for 32-bit would be semantically correct. I will leave it to Matt to comment whether it is reasonable in terms of changes to other parts of the code.
Thanks,
Ard.
Re: [PATCH 1/2] x86/efi: Map EFI memmap entries in-order at runtime
In reply to Ingo MolnarQuoted 23 lines · Ingo Molnar
Quoted 20 lines · Ard Biesheuvel
Quoted 12 lines
except that I don't think
the condition on 64-bit makes any sense:+ if (!efi_enabled(EFI_OLD_MEMMAP) &&
efi_enabled(EFI_64BIT)) {I can see us being nervous wrt. backported patches, but is there any strong reason to not follow this up with a third (non-backported) patch that changes this to:
+ if (!efi_enabled(EFI_OLD_MEMMAP)) {
for v4.4?
The 32-bit side essentially implements the old memmap only, which is the the bottom-up version. So old memmap will be implied by 32-bit but not set in the EFI flags, resulting in the reverse enumeration being used with the bottom-up mapping logic. The net result of that is that we create the same problem for 32-bit that we are trying to solve for 64-bit, i.e., the regions will end up in reverse order in the VA mapping.
Quoted 5 lines
Quoted 3 lines
To deobfuscate this particular conditional, we could set
EFI_OLD_MEMMAPunconditionally on 32-bit x86. Or we could reshuffle variables and conditionals in various other way.Setting
EFI_OLD_MEMMAPwould be fine, if doing that has no bad side effects.
Right, I think that's a very good suggestion, because like Ard mentioned, since EFI_OLD_MEMMAP is implied for 32-bit (there's no other way to map stuff currently), so it makes sense to force set the bit.
Quoted 5 lines
Quoted 1 line
[...] I am not convinced that the overall end result will be any better though.
That's not true, we change an obscure, implicit dependency on 32-bit detail to an explicit
EFI_OLD_MEMMAPflag that shows exactly what's happening. That's a clear improvement.
Agreed.
Signature · 1 line
Matt Fleming, Intel Open Source Technology Center
Re: [PATCH 1/2] x86/efi: Map EFI memmap entries in-order at runtime
In reply to Ard BiesheuvelQuoted 4 lines · Ard Biesheuvel
OK, fair enough. I agree that setting the flag for 32-bit would be semantically correct. I will leave it to Matt to comment whether it is reasonable in terms of changes to other parts of the code.
It should be pretty minimal. Let me take a swing at the patch.
Signature · 1 line
Matt Fleming, Intel Open Source Technology Center
Re: [PATCH 2/2] arm64/efi: Don't pad between EFI_MEMORY_RUNTIME regions
In reply to Borislav PetkovQuoted 6 lines · Borislav Petkov
Quoted 4 lines · Ingo Molnar
Could we please re-list all the arguments pro and contra of 1:1 physical mappings, in a post that also explains the background so that more people can chime in, not just people versed in EFI internals? It's very much possible that a bad decision was made.
Quoted 8 lines
The main reason why we did the additional, top-down mapping was kexec kernel wanting to use UEFI runtime facilities too and the braindead design of
SetVirtualAddressMap()being callable only once per system boot. So we had to have stable mappings which are valid in the kexec-ed kernel too.But this was long time ago and I most certainly have forgotten all the details.
That's a pretty good summary for x86. I think specifically the reason we map the EFI memmap entries "backwards" (entry N has higher VA than entry N+1) is because the code was easier to write that way, but you'll know better than me ;-)
Quoted 8 lines
And now I'm wondering why didn't we do the 1:1 thing and rebuild the exact same EFI pagetable in the kexec-ed kernel? Because when we do an EFI call, we switch to the special pagetable so why didn't we make the kexec-ed kernel rebuild the 1:1 pagetable which it can use for EFI calls...
Hmm, again, I've forgotten a lot of details so I'm sure Matt will come in and say "No, you can't do that because..."
I *think* the only reason was the Apple firmware problem where it explodes if you pass the 1:1 mappings to SetVirtualAddressMap(). And obviously people do want to use kexec with Apple machines.
It's probably worth revisiting this whole thing from the x86 side.
Signature · 1 line
Matt Fleming, Intel Open Source Technology Center
Re: [PATCH 1/2] x86/efi: Map EFI memmap entries in-order at runtime
In reply to Matthew GarrettQuoted 7 lines · Matthew Garrett
Quoted 1 line · Ingo Molnar
So the question is, what does Windows do?
It's pretty trivial to hack OVMF to dump the
SetVirtualAddressMap()arguments to the qemu debug port. Unfortunately I'm about to drop mostly offline for a week, otherwise I'd give it a go...
In order to enable the properties table feature in OVMF, two conditions have to be satisfied:
(a) set gEfiMdeModulePkgTokenSpaceGuid.PcdPropertiesTableEnable to TRUE
(b) build DXE_RUNTIME_DRIVER modules with 4KB section alignment
(a) used to be possible only statically (ie. at build time), but since edk2 commit ab081a50e5 [1] it can be done dynamically too, on the qemu command line. See that edk2 commit for details.
This OVMF feature depends on QEMU commit 81b2b81062 [2]. Another patch from Gabriel is under review that enables such simple settings without host-side files [3].
(b) is satisfied by the edk2 patch that Ard posted today [4].
Because I know how much people like building OVMF, I uploaded a fresh OVMF binary (which includes a number of other patches from my personal tree, but those are irrelevent here), with a matching varstore template, to [5]. The relevant edk2 patches (ie. the one from Ard [4], and a debug patch like you mention above) can also be found under [5].
(
I recommend to start QEMU like this:
# Create a new (empty) varstore for the virtual machine, from the
# varstore template.
cp OVMF_VARS.fd my-vars.fd
# Start the VM, with direct kernel boot.
qemu-system-x86_64 \ -m 2048 \ -M pc,accel=kvm \ \ -device qxl-vga \ \ -drive if=pflash,format=raw,unit=0,file=OVMF_CODE.fd,readonly=on \ -drive if=pflash,format=raw,unit=1,file=my-vars.fd \ \ -debugcon file:debug.log \ -global isa-debugcon.iobase=0x402 \ \ -chardev stdio,signal=off,mux=on,id=char0 \ -mon chardev=char0,mode=readline,default \ -serial chardev:char0 \ \ -kernel vmlinuz... \ -initrd initramfs... \ -append "root=..." \ \ ...
The guest will have 2GB of RAM, it will use KVM, the virtual video card will be QXL; the OVMF debug log will be written to "debug.log". The key combination [Control-A C] will switch between the QEMU monitor prompt and the serial port of the guest. The above should be suitable for rapid testing of kernels built on the host.
)
Then I booted my Windows Server 2012 R2, Windows 8.1, and Windows 10 guests, with the properties table feature enabled vs. disabled in the firmware. (All three Windows guests were updated first though.)
All three Windows OSes adapt their SetVirtualAddressMap() calls, when the feature is enabled in the firmware. However, Windows 8.1 crashes nonetheless (BSOD, I forget the fault details, sorry). Windows Server 2012 R2 and Windows 10 boot fine.
I uploaded the verbose OVMF log files from all six guest boots to [5]. The tables you might be interested in are dumped at the ends of the log files.
All three guests had 2GB of RAM. They had different VM configurations, but between disabling and enabling the properties table feature, no other knob was tweaked. Therefore the two log files of the same guest should be comparable against each other, for each guest. For example:
$ colordiff -u ovmf.win10.prop.{disabled,enabled}.logBecause stuff hosted on the web privately tends to go away, I'll quote that diff here, for posterity:
Quoted 62 lines
ovmf.win10.prop.enabled.log renamed from ovmf.win10.prop.disabled.log +26
-14@@ -24,7 +24,7 @@ QemuFwCfg interface is supported. Platform PEIM Loaded CMOS: -00: 38 00 01 00 22 00 03 29 09 15 26 02 00 80 00 00 +00: 48 00 50 00 21 00 03 29 09 15 26 02 00 80 00 00 10: 00 00 00 00 06 80 02 FF FF 00 00 00 00 00 00 00 20: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 30: FF FF 20 00 00 7F 00 20 30 00 00 00 00 12 00 00@@ -1110,6 +1110,17 @@ InstallProtocolInterface: 4CF5B200-68B8-4CA5-9EEC-B23E3F50029A 7F271568 [Variable]END_OF_DXE is signaled Initialize variable error flag (FF) +MemoryProtectionAttribute - 0x0000000000000001 +Total Image Count - 0x8 +Dump ImageRecord: + Image[0]: 0x000000007EC26000 - 0x000000000000A000 + Image[1]: 0x000000007EC35000 - 0x0000000000008000 + Image[2]: 0x000000007EC65000 - 0x000000000000C000 + Image[3]: 0x000000007ED76000 - 0x00000000000A3000 + Image[4]: 0x000000007FE9E000 - 0x0000000000009000 + Image[5]: 0x000000007FEA7000 - 0x000000000000A000 + Image[6]: 0x000000007FEB1000 - 0x000000000000C000 + Image[7]: 0x000000007FEBD000 - 0x000000000000C000 S3Ready! SaveLockBox: Guid=DEA652B0-D587-4C54-B5B4-C682E7A0AA3D Buffer=7FEEE000 Length=0xA SetLockBoxAttributes: Guid=DEA652B0-D587-4C54-B5B4-C682E7A0AA3D Attributes=0x1@@ -1822,18 +1833,29 @@ InstallProtocolInterface: 5B1B31A1-9562-11D2-8E3F-00A0C969723B 7F0EBA00 Loading driver at 0x00010000000 EntryPoint=0x00010014790 bootmgfw.efi InstallProtocolInterface: BC62157E-3E33-4FEC-9920-2D3B36D750DF 7F0B8898 -RuntimeDriverSetVirtualAddressMap: 12 descriptors +RuntimeDriverSetVirtualAddressMap: 23 descriptors # Memory Type PhysicalStart 0x VirtualStart 0x Size 0x Attributes -- ------------ ---------------- ---------------- ---------------- -------------------------------------- - 0 RuntimeData 000000007EC21000 FFFFFFFFFF7E4000 0000000000005000 [UC|WC|WT|WB| | | | | | | |RT] - 1 RuntimeCode 000000007EC26000 FFFFFFFFFF7E9000 000000000000A000 [UC|WC|WT|WB| | | | | | | |RT] - 2 RuntimeData 000000007EC30000 FFFFFFFFFF7F3000 0000000000005000 [UC|WC|WT|WB| | | | | | | |RT] - 3 RuntimeCode 000000007EC35000 FFFFFFFFFF7F8000 0000000000008000 [UC|WC|WT|WB| | | | | | | |RT] - 4 RuntimeData 000000007EC60000 FFFFFFFFFF800000 0000000000005000 [UC|WC|WT|WB| | | | | | | |RT] - 5 RuntimeCode 000000007EC65000 FFFFFFFFFF805000 000000000000C000 [UC|WC|WT|WB| | | | | | | |RT] - 6 RuntimeData 000000007EC9E000 FFFFFFFFFF811000 00000000000D8000 [UC|WC|WT|WB| | | | | | | |RT] - 7 RuntimeCode 000000007ED76000 FFFFFFFFFF8E9000 00000000000A3000 [UC|WC|WT|WB| | | | | | | |RT] - 8 RuntimeCode 000000007FE99000 FFFFFFFFFF98C000 0000000000030000 [UC|WC|WT|WB| | | | | | | |RT] - 9 RuntimeData 000000007FEC9000 FFFFFFFFFF9BC000 0000000000024000 [UC|WC|WT|WB| | | | | | | |RT] -10 RuntimeData 000000007FFD0000 FFFFFFFFFF9E0000 0000000000020000 [UC|WC|WT|WB| | | | | | | |RT] -11 RuntimeData 00000000FFE00000 FFFFFFFFFFA00000 0000000000200000 [UC| | | | | | | | | | |RT] + 0 RuntimeData 000000007EC21000 FFFFFFFFFF7E4000 0000000000006000 [UC|WC|WT|WB| | | |XP| | | |RT] + 1 RuntimeCode 000000007EC27000 FFFFFFFFFF7EA000 0000000000007000 [UC|WC|WT|WB| | | | |RO| | |RT] + 2 RuntimeData 000000007EC2E000 FFFFFFFFFF7F1000 0000000000007000 [UC|WC|WT|WB| | | |XP| | | |RT] + 3 RuntimeData 000000007EC35000 FFFFFFFFFF7F8000 0000000000001000 [UC|WC|WT|WB| | | |XP| | | |RT] + 4 RuntimeCode 000000007EC36000 FFFFFFFFFF7F9000 0000000000005000 [UC|WC|WT|WB| | | | |RO| | |RT] + 5 RuntimeData 000000007EC3B000 FFFFFFFFFF7FE000 0000000000002000 [UC|WC|WT|WB| | | |XP| | | |RT] + 6 RuntimeData 000000007EC60000 FFFFFFFFFF800000 0000000000006000 [UC|WC|WT|WB| | | |XP| | | |RT] + 7 RuntimeCode 000000007EC66000 FFFFFFFFFF806000 0000000000009000 [UC|WC|WT|WB| | | | |RO| | |RT] + 8 RuntimeData 000000007EC6F000 FFFFFFFFFF80F000 0000000000002000 [UC|WC|WT|WB| | | |XP| | | |RT] + 9 RuntimeData 000000007EC9E000 FFFFFFFFFF811000 00000000000D9000 [UC|WC|WT|WB| | | |XP| | | |RT] +10 RuntimeCode 000000007ED77000 FFFFFFFFFF8EA000 0000000000097000 [UC|WC|WT|WB| | | | |RO| | |RT] +11 RuntimeData 000000007EE0E000 FFFFFFFFFF981000 000000000000B000 [UC|WC|WT|WB| | | |XP| | | |RT] +12 RuntimeData 000000007FE99000 FFFFFFFFFF98C000 0000000000006000 [UC|WC|WT|WB| | | |XP| | | |RT]
ovmf.win10.prop.enabled.log line 1861
Quoted 10 lines
ovmf.win10.prop.enabled.log renamed from ovmf.win10.prop.disabled.log +10
-0+13 RuntimeCode 000000007FE9F000 FFFFFFFFFF992000 0000000000006000 [UC|WC|WT|WB| | | | |RO| | |RT] +14 RuntimeData 000000007FEA5000 FFFFFFFFFF998000 0000000000003000 [UC|WC|WT|WB| | | |XP| | | |RT] +15 RuntimeCode 000000007FEA8000 FFFFFFFFFF99B000 0000000000007000 [UC|WC|WT|WB| | | | |RO| | |RT] +16 RuntimeData 000000007FEAF000 FFFFFFFFFF9A2000 0000000000003000 [UC|WC|WT|WB| | | |XP| | | |RT] +17 RuntimeCode 000000007FEB2000 FFFFFFFFFF9A5000 0000000000009000 [UC|WC|WT|WB| | | | |RO| | |RT] +18 RuntimeData 000000007FEBB000 FFFFFFFFFF9AE000 0000000000003000 [UC|WC|WT|WB| | | |XP| | | |RT] +19 RuntimeCode 000000007FEBE000 FFFFFFFFFF9B1000 0000000000009000 [UC|WC|WT|WB| | | | |RO| | |RT] +20 RuntimeData 000000007FEC7000 FFFFFFFFFF9BA000 0000000000026000 [UC|WC|WT|WB| | | |XP| | | |RT] +21 RuntimeData 000000007FFD0000 FFFFFFFFFF9E0000 0000000000020000 [UC|WC|WT|WB| | | |XP| | | |RT] +22 RuntimeData 00000000FFE00000 FFFFFFFFFFA00000 0000000000200000 [UC| | | | | | |XP| | | |RT]
Thanks
Laszlo
[1] https://github.com/tianocore/edk2/commit/ab081a50e5
[2] https://github.com/qemu/qemu/commit/81b2b81062
[3] http://news.gmane.org/find-root.php?message_id=%3C1443544141-26568-1-git-send-email-somlo-D+Gtc/[email protected]%3E
[4] http://thread.gmane.org/gmane.comp.bios.edk2.devel/2640
[5] http://people.redhat.com/~lersek/ovmf_prop_table/
H. Peter Anvin
reviewerRe: [PATCH 1/2] x86/efi: Map EFI memmap entries in-order at runtime
In reply to Ingo MolnarQuoted 7 lines · Ingo Molnar
So the question is, what does Windows do?
PC firmware is a hostile environment for Linux, to be compatible the best we can do is to mimic the environment that the firmware is tested under - i.e. try to use the firmware in the way Windows uses it.
Windows apparently went through the same exercise of breakage followed by a fix. It is unknown if Windows will preserve gaps since those are not manifest on existing firmware.
-hpa
H. Peter Anvin
reviewerRe: [PATCH 2/2] arm64/efi: Don't pad between EFI_MEMORY_RUNTIME regions
In reply to Matt FlemingQuoted 6 lines · Matt Fleming
That's a pretty good summary for x86. I think specifically the reason we map the EFI memmap entries "backwards" (entry N has higher VA than entry N+1) is because the code was easier to write that way, but you'll know better than me ;-)
There were two reasons:
1. The code was easier to write.
2. Windows did it that way.
Windows apparently broke and was changed due to this feature, too.
-hpa
H. Peter Anvin
reviewerRe: [PATCH 2/2] arm64/efi: Don't pad between EFI_MEMORY_RUNTIME regions
In reply to Ingo MolnarQuoted 14 lines · Ingo Molnar
Quoted 10 lines · Ard Biesheuvel
Quoted 3 lines
If we allocate the EFI runtime as a single virtual memory block then issues like rounding between sections does not even come up as a problem: we map the original offsets and sizes byte by byte.
Well, by that reasoning, we should not call
SetVirtualAddressMap()in the first place, and just use the 1:1 mapping UEFI uses natively. This is more than feasible on arm64, and I actually fought hard against usingSetVirtualAddressMap()at all, but I was overruled by others. I think this is also trivially possible on X64, since the 1:1 mapping is already active alongside the VA mapping.
Quoted 5 lines
Could we please re-list all the arguments pro and contra of 1:1 physical mappings, in a post that also explains the background so that more people can chime in, not just people versed in EFI internals? It's very much possible that a bad decision was made.
Pro: by far the sanest way to map the UEFI tables.
Con: doesn't actually work (breaks on several known platforms.)
-hpa
Re: [PATCH 2/2] arm64/efi: Don't pad between EFI_MEMORY_RUNTIME regions
Quoted 15 lines · H. Peter Anvin
Quoted 14 lines · Ingo Molnar
Quoted 10 lines · Ard Biesheuvel
Quoted 3 lines
If we allocate the EFI runtime as a single virtual memory block then issues like rounding between sections does not even come up as a problem: we map the original offsets and sizes byte by byte.
Well, by that reasoning, we should not call
SetVirtualAddressMap()in the first place, and just use the 1:1 mapping UEFI uses natively. This is more than feasible on arm64, and I actually fought hard against usingSetVirtualAddressMap()at all, but I was overruled by others. I think this is also trivially possible on X64, since the 1:1 mapping is already active alongside the VA mapping.
Quoted 8 lines
Quoted 5 lines
Could we please re-list all the arguments pro and contra of 1:1 physical mappings, in a post that also explains the background so that more people can chime in, not just people versed in EFI internals? It's very much possible that a bad decision was made.
Pro: by far the sanest way to map the UEFI tables.
Con: doesn't actually work (breaks on several known platforms.)
Can we at least do 1:1 without an offset on arm64? Given that Linux seems to be more of a reference on arm64 than Windows is, maybe that would give everyone something vaguely sane to work with.
--Andy
H. Peter Anvin
reviewerRe: [PATCH 2/2] arm64/efi: Don't pad between EFI_MEMORY_RUNTIME regions
Quoted 5 lines · Andy Lutomirski
Can we at least do 1:1 without an offset on arm64? Given that Linux seems to be more of a reference on arm64 than Windows is, maybe that would give everyone something vaguely sane to work with.
I have no idea. That's a question for the ARM folks.
-hpa
Re: [PATCH 2/2] arm64/efi: Don't pad between EFI_MEMORY_RUNTIME regions
In reply to Andy LutomirskiQuoted 22 lines · Andy Lutomirski
Quoted 21 lines · H. Peter Anvin
Quoted 19 lines · Ingo Molnar
Quoted 10 lines · Ard Biesheuvel
Quoted 3 lines
If we allocate the EFI runtime as a single virtual memory block then issues like rounding between sections does not even come up as a problem: we map the original offsets and sizes byte by byte.
Well, by that reasoning, we should not call
SetVirtualAddressMap()in the first place, and just use the 1:1 mapping UEFI uses natively. This is more than feasible on arm64, and I actually fought hard against usingSetVirtualAddressMap()at all, but I was overruled by others. I think this is also trivially possible on X64, since the 1:1 mapping is already active alongside the VA mapping.Could we please re-list all the arguments pro and contra of 1:1 physical mappings, in a post that also explains the background so that more people can chime in, not just people versed in EFI internals? It's very much possible that a bad decision was made.
Quoted 7 lines
Quoted 2 lines
Pro: by far the sanest way to map the UEFI tables.
Con: doesn't actually work (breaks on several known platforms.)Can we at least do 1:1 without an offset on arm64? Given that Linux seems to be more of a reference on arm64 than Windows is, maybe that would give everyone something vaguely sane to work with.
Yes, as I mentioned before in this thread, on arm64 this is very much feasible, and it was my strong preference all along. However, the arguments made by others that outweighed my preference were
(a) x86 uses it
(b) if we don't use it now, we will never be able to start using it later since it will undoubtedly be broken in /some/ implementation in the field.
As I also mentioned, the only minor complication is that arm64's VA space may be configured to be smaller than the physical base of DRAM, but I already had to address that for the boot ID map and KVM as well.
I will cook up a patch later today.
Signature · 1 line
Ard.
Borislav Petkov
maintainerRe: [PATCH 2/2] arm64/efi: Don't pad between EFI_MEMORY_RUNTIME regions
In reply to H. Peter AnvinQuoted 8 lines · H. Peter Anvin
Quoted 6 lines · Matt Fleming
That's a pretty good summary for x86. I think specifically the reason we map the EFI memmap entries "backwards" (entry N has higher VA than entry N+1) is because the code was easier to write that way, but you'll know better than me ;-)
Quoted 6 lines
There were two reasons:
1. The code was easier to write.
2. Windows did it that way.Windows apparently broke and was changed due to this feature, too.
So can we do the 1:1 thing again?
I mean, we do create a special pagetable for EFI anyway, we can put in there whatever we want.
I know, some apple boxes reportedly fail when 1:1 mapping is in use but we can do the VA mapping as a workaround for them. I.e., have the 1:1 mapping be the default.
Apparently, there's not a single OS or tool which is used by fw writers to test their brain dumplings. Windoze breakage case-in-point.
Because if there were, we'd simply do what that OS/tool does and be done with it.
What really makes me climb the walls is when half-cooked, untested fw hits the wild and we have to support it indefinitely.
Signature · 4 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.Re: [PATCH 1/2] x86/efi: Map EFI memmap entries in-order at runtime
In reply to Laszlo ErsekQuoted 8 lines · Laszlo Ersek
Quoted 7 lines · Matthew Garrett
Quoted 1 line · Ingo Molnar
So the question is, what does Windows do?
It's pretty trivial to hack OVMF to dump the
SetVirtualAddressMap()arguments to the qemu debug port. Unfortunately I'm about to drop mostly offline for a week, otherwise I'd give it a go...
[...]
Quoted 9 lines
Then I booted my Windows Server 2012 R2, Windows 8.1, and Windows 10 guests, with the properties table feature enabled vs. disabled in the firmware. (All three Windows guests were updated first though.)
All three Windows OSes adapt their
SetVirtualAddressMap()calls, when the feature is enabled in the firmware. However, Windows 8.1 crashes nonetheless (BSOD, I forget the fault details, sorry). Windows Server 2012 R2 and Windows 10 boot fine.
Looking at the log, it seems the VA mapping strategy is actually the same (i.e., bottom-up for Win10), and the difference can be explained by the differences in the memory map provided by the firmware to the OS. And indeed, the Win8.1 log shows the following:
# MemType Phys 0x Virt 0x Size 0x Attributes
-- ------- -------- -------- ------- -------------------------------
0 RtData 7EC21000 FFBFA000 0006000 [UC|WC|WT|WB| |XP| | | |RT] 1 RtCode 7EC27000 FFBF3000 0007000 [UC|WC|WT|WB| | |RO| | |RT] 2 RtData 7EC2E000 FFBEC000 0007000 [UC|WC|WT|WB| |XP| | | |RT] 3 RtData 7EC35000 FFBEB000 0001000 [UC|WC|WT|WB| |XP| | | |RT] 4 RtCode 7EC36000 FFBE6000 0005000 [UC|WC|WT|WB| | |RO| | |RT] 5 RtData 7EC3B000 FFBE4000 0002000 [UC|WC|WT|WB| |XP| | | |RT] 6 RtData 7EC60000 FFBDE000 0006000 [UC|WC|WT|WB| |XP| | | |RT] 7 RtCode 7EC66000 FFBD5000 0009000 [UC|WC|WT|WB| | |RO| | |RT] 8 RtData 7EC6F000 FFBD3000 0002000 [UC|WC|WT|WB| |XP| | | |RT] 9 RtData 7EC9E000 FFAFA000 00D9000 [UC|WC|WT|WB| |XP| | | |RT] 10 RtCode 7ED77000 FFA63000 0097000 [UC|WC|WT|WB| | |RO| | |RT] 11 RtData 7EE0E000 FFA58000 000B000 [UC|WC|WT|WB| |XP| | | |RT] 12 RtData 7FE99000 FFA52000 0006000 [UC|WC|WT|WB| |XP| | | |RT] 13 RtCode 7FE9F000 FFA4C000 0006000 [UC|WC|WT|WB| | |RO| | |RT] 14 RtData 7FEA5000 FFA49000 0003000 [UC|WC|WT|WB| |XP| | | |RT] 15 RtCode 7FEA8000 FFA42000 0007000 [UC|WC|WT|WB| | |RO| | |RT] 16 RtData 7FEAF000 FFA3F000 0003000 [UC|WC|WT|WB| |XP| | | |RT] 17 RtCode 7FEB2000 FFA36000 0009000 [UC|WC|WT|WB| | |RO| | |RT] 18 RtData 7FEBB000 FFA33000 0003000 [UC|WC|WT|WB| |XP| | | |RT] 19 RtCode 7FEBE000 FFA2A000 0009000 [UC|WC|WT|WB| | |RO| | |RT] 20 RtData 7FEC7000 FFA04000 0026000 [UC|WC|WT|WB| |XP| | | |RT] 21 RtData 7FFD0000 FF9E4000 0020000 [UC|WC|WT|WB| |XP| | | |RT] 22 RtData FFE00000 FF7E4000 0200000 [UC| | | | |XP| | | |RT]
I.e., the physical addresses increase while the virtual addresses decrease, and since each consecutive RuntimeCode/RuntimeData pair constitutes a PE/COFF image (.text and .data, respectively), the PE/COFF images appear corrupted in the virtual space.
Quoted 9 lines
I uploaded the verbose OVMF log files from all six guest boots to [5]. The tables you might be interested in are dumped at the ends of the log files.
All three guests had 2GB of RAM. They had different VM configurations, but between disabling and enabling the properties table feature, no other knob was tweaked. Therefore the two log files of the same guest should be comparable against each other, for each guest. For example:
Quoted 7 lines
$ colordiff -u ovmf.win10.prop.{disabled,enabled}.logBecause stuff hosted on the web privately tends to go away, I'll quote that diff here, for posterity:
ovmf.win10.prop.enabled.log 2015-09-29 21:50:54.579475078 +0200
Quoted 2 lines
ovmf.win10.prop.enabled.log 2015-09-29 21:50:54.579475078 +0200 renamed from ovmf.win10.prop.disabled.log 2015-09-29 22:01:45.252126086 +0200 +0
-0
[...]
Thanks a lot for taking the time, this is very useful info.
Signature · 1 line
Ard.
Re: [PATCH 1/2] x86/efi: Map EFI memmap entries in-order at runtime
Quoted 51 lines · Ard Biesheuvel
Quoted 8 lines · Laszlo Ersek
Quoted 7 lines · Matthew Garrett
Quoted 1 line · Ingo Molnar
So the question is, what does Windows do?
It's pretty trivial to hack OVMF to dump the
SetVirtualAddressMap()arguments to the qemu debug port. Unfortunately I'm about to drop mostly offline for a week, otherwise I'd give it a go...[...]
Quoted 9 lines
Then I booted my Windows Server 2012 R2, Windows 8.1, and Windows 10 guests, with the properties table feature enabled vs. disabled in the firmware. (All three Windows guests were updated first though.)
All three Windows OSes adapt their
SetVirtualAddressMap()calls, when the feature is enabled in the firmware. However, Windows 8.1 crashes nonetheless (BSOD, I forget the fault details, sorry). Windows Server 2012 R2 and Windows 10 boot fine.Looking at the log, it seems the VA mapping strategy is actually the same (i.e., bottom-up for Win10), and the difference can be explained by the differences in the memory map provided by the firmware to the OS. And indeed, the Win8.1 log shows the following:
# MemType Phys 0x Virt 0x Size 0x Attributes
-- ------- -------- -------- ------- -------------------------------0 RtData 7EC21000 FFBFA000 0006000 [UC|WC|WT|WB| |XP| | | |RT] 1 RtCode 7EC27000 FFBF3000 0007000 [UC|WC|WT|WB| | |RO| | |RT] 2 RtData 7EC2E000 FFBEC000 0007000 [UC|WC|WT|WB| |XP| | | |RT] 3 RtData 7EC35000 FFBEB000 0001000 [UC|WC|WT|WB| |XP| | | |RT] 4 RtCode 7EC36000 FFBE6000 0005000 [UC|WC|WT|WB| | |RO| | |RT] 5 RtData 7EC3B000 FFBE4000 0002000 [UC|WC|WT|WB| |XP| | | |RT] 6 RtData 7EC60000 FFBDE000 0006000 [UC|WC|WT|WB| |XP| | | |RT] 7 RtCode 7EC66000 FFBD5000 0009000 [UC|WC|WT|WB| | |RO| | |RT] 8 RtData 7EC6F000 FFBD3000 0002000 [UC|WC|WT|WB| |XP| | | |RT] 9 RtData 7EC9E000 FFAFA000 00D9000 [UC|WC|WT|WB| |XP| | | |RT] 10 RtCode 7ED77000 FFA63000 0097000 [UC|WC|WT|WB| | |RO| | |RT] 11 RtData 7EE0E000 FFA58000 000B000 [UC|WC|WT|WB| |XP| | | |RT] 12 RtData 7FE99000 FFA52000 0006000 [UC|WC|WT|WB| |XP| | | |RT] 13 RtCode 7FE9F000 FFA4C000 0006000 [UC|WC|WT|WB| | |RO| | |RT] 14 RtData 7FEA5000 FFA49000 0003000 [UC|WC|WT|WB| |XP| | | |RT] 15 RtCode 7FEA8000 FFA42000 0007000 [UC|WC|WT|WB| | |RO| | |RT] 16 RtData 7FEAF000 FFA3F000 0003000 [UC|WC|WT|WB| |XP| | | |RT] 17 RtCode 7FEB2000 FFA36000 0009000 [UC|WC|WT|WB| | |RO| | |RT] 18 RtData 7FEBB000 FFA33000 0003000 [UC|WC|WT|WB| |XP| | | |RT] 19 RtCode 7FEBE000 FFA2A000 0009000 [UC|WC|WT|WB| | |RO| | |RT] 20 RtData 7FEC7000 FFA04000 0026000 [UC|WC|WT|WB| |XP| | | |RT] 21 RtData 7FFD0000 FF9E4000 0020000 [UC|WC|WT|WB| |XP| | | |RT] 22 RtData FFE00000 FF7E4000 0200000 [UC| | | | |XP| | | |RT]
Quoted 4 lines
I.e., the physical addresses increase while the virtual addresses decrease, and since each consecutive RuntimeCode/RuntimeData pair constitutes a PE/COFF image (.text and .data, respectively), the PE/COFF images appear corrupted in the virtual space.
All of this garbage makes me want to ask a rhetorical question:
Why on Earth did anyone think it's a good idea to invoke EFI functions at CPL0 once the OS is booted?
And a more practical question:
Do we actually have to invoke EFI functions at CPL0?
I really mean it. Sure, for things like reboot where we give up control and don't get it back, we need to do that. But for things like variable access, the EFI code should really only need access to EFI memor (with a known PA -> VA map) and the ability to trigger an SMI. Doing it at CPL3 could require more fixups than would really make sense, but could we virtualize it instead?
Actually, CPL3 + IOPL3 just might work.
Heck, on mixed-mode, we're already invoke EFI functions in compat mode, and that seems okay, so those functions can't be poking at any CPU state that varies between long and 32-bit modes.
--Andy
Re: [PATCH 1/2] x86/efi: Map EFI memmap entries in-order at runtime
Quoted 58 lines · Andy Lutomirski
Quoted 51 lines · Ard Biesheuvel
Quoted 8 lines · Laszlo Ersek
Quoted 7 lines · Matthew Garrett
Quoted 1 line · Ingo Molnar
So the question is, what does Windows do?
It's pretty trivial to hack OVMF to dump the
SetVirtualAddressMap()arguments to the qemu debug port. Unfortunately I'm about to drop mostly offline for a week, otherwise I'd give it a go...[...]
Quoted 9 lines
Then I booted my Windows Server 2012 R2, Windows 8.1, and Windows 10 guests, with the properties table feature enabled vs. disabled in the firmware. (All three Windows guests were updated first though.)
All three Windows OSes adapt their
SetVirtualAddressMap()calls, when the feature is enabled in the firmware. However, Windows 8.1 crashes nonetheless (BSOD, I forget the fault details, sorry). Windows Server 2012 R2 and Windows 10 boot fine.Looking at the log, it seems the VA mapping strategy is actually the same (i.e., bottom-up for Win10), and the difference can be explained by the differences in the memory map provided by the firmware to the OS. And indeed, the Win8.1 log shows the following:
# MemType Phys 0x Virt 0x Size 0x Attributes
-- ------- -------- -------- ------- -------------------------------0 RtData 7EC21000 FFBFA000 0006000 [UC|WC|WT|WB| |XP| | | |RT] 1 RtCode 7EC27000 FFBF3000 0007000 [UC|WC|WT|WB| | |RO| | |RT] 2 RtData 7EC2E000 FFBEC000 0007000 [UC|WC|WT|WB| |XP| | | |RT] 3 RtData 7EC35000 FFBEB000 0001000 [UC|WC|WT|WB| |XP| | | |RT] 4 RtCode 7EC36000 FFBE6000 0005000 [UC|WC|WT|WB| | |RO| | |RT] 5 RtData 7EC3B000 FFBE4000 0002000 [UC|WC|WT|WB| |XP| | | |RT] 6 RtData 7EC60000 FFBDE000 0006000 [UC|WC|WT|WB| |XP| | | |RT] 7 RtCode 7EC66000 FFBD5000 0009000 [UC|WC|WT|WB| | |RO| | |RT] 8 RtData 7EC6F000 FFBD3000 0002000 [UC|WC|WT|WB| |XP| | | |RT] 9 RtData 7EC9E000 FFAFA000 00D9000 [UC|WC|WT|WB| |XP| | | |RT] 10 RtCode 7ED77000 FFA63000 0097000 [UC|WC|WT|WB| | |RO| | |RT] 11 RtData 7EE0E000 FFA58000 000B000 [UC|WC|WT|WB| |XP| | | |RT] 12 RtData 7FE99000 FFA52000 0006000 [UC|WC|WT|WB| |XP| | | |RT] 13 RtCode 7FE9F000 FFA4C000 0006000 [UC|WC|WT|WB| | |RO| | |RT] 14 RtData 7FEA5000 FFA49000 0003000 [UC|WC|WT|WB| |XP| | | |RT] 15 RtCode 7FEA8000 FFA42000 0007000 [UC|WC|WT|WB| | |RO| | |RT] 16 RtData 7FEAF000 FFA3F000 0003000 [UC|WC|WT|WB| |XP| | | |RT] 17 RtCode 7FEB2000 FFA36000 0009000 [UC|WC|WT|WB| | |RO| | |RT] 18 RtData 7FEBB000 FFA33000 0003000 [UC|WC|WT|WB| |XP| | | |RT] 19 RtCode 7FEBE000 FFA2A000 0009000 [UC|WC|WT|WB| | |RO| | |RT] 20 RtData 7FEC7000 FFA04000 0026000 [UC|WC|WT|WB| |XP| | | |RT] 21 RtData 7FFD0000 FF9E4000 0020000 [UC|WC|WT|WB| |XP| | | |RT] 22 RtData FFE00000 FF7E4000 0200000 [UC| | | | |XP| | | |RT]Quoted 4 lines
I.e., the physical addresses increase while the virtual addresses decrease, and since each consecutive RuntimeCode/RuntimeData pair constitutes a PE/COFF image (.text and .data, respectively), the PE/COFF images appear corrupted in the virtual space.
Quoted 4 lines
All of this garbage makes me want to ask a rhetorical question:
Why on Earth did anyone think it's a good idea to invoke EFI functions at CPL0 once the OS is booted?
I'm afraid the originators of EFI (Intel) look on it as a DOS replacement ... with the same OS support.
Quoted 11 lines
And a more practical question:
Do we actually have to invoke EFI functions at CPL0?
I really mean it. Sure, for things like reboot where we give up control and don't get it back, we need to do that. But for things like variable access, the EFI code should really only need access to EFI memor (with a known PA -> VA map) and the ability to trigger an SMI. Doing it at CPL3 could require more fixups than would really make sense, but could we virtualize it instead?
Quoted 5 lines
Actually, CPL3 + IOPL3 just might work.
Heck, on mixed-mode, we're already invoke EFI functions in compat mode, and that seems okay, so those functions can't be poking at any CPU state that varies between long and 32-bit modes.
It's hard. The EFI functions expect to interact directly with kernel memory, which they can't at CPL3. We could vector all that through a CPL3 readable buffer but anything within EFI that uses privileged instructions will fault and we'll have to handle it ... this really sounds like a can of worms. Especially as windows will be no help testing all of this because it will call in at CPL0.
James
Re: [PATCH 2/2] arm64/efi: Don't pad between EFI_MEMORY_RUNTIME regions
In reply to H. Peter AnvinQuoted 15 lines · H. Peter Anvin
Quoted 14 lines · Ingo Molnar
Quoted 10 lines · Ard Biesheuvel
Quoted 3 lines
If we allocate the EFI runtime as a single virtual memory block then issues like rounding between sections does not even come up as a problem: we map the original offsets and sizes byte by byte.
Well, by that reasoning, we should not call
SetVirtualAddressMap()in the first place, and just use the 1:1 mapping UEFI uses natively. This is more than feasible on arm64, and I actually fought hard against usingSetVirtualAddressMap()at all, but I was overruled by others. I think this is also trivially possible on X64, since the 1:1 mapping is already active alongside the VA mapping.
Quoted 8 lines
Quoted 5 lines
Could we please re-list all the arguments pro and contra of 1:1 physical mappings, in a post that also explains the background so that more people can chime in, not just people versed in EFI internals? It's very much possible that a bad decision was made.
Pro: by far the sanest way to map the UEFI tables.
Con: doesn't actually work (breaks on several known platforms.)
You knew this next question was coming: in what way does it break on known platforms?
I.e. do those platforms require a SetVirtualAddressMap() call and break if one does not come?
Note that there's 3 models possible:
- pure 1:1
- 1:1 plus offset, with SetVirtualAddressMap(offset)
- bottom up allocator
I don't think we want 'pure' 1:1 physical/virtual (for security reasons, etc.).
So the question is, in what way does our current proposed bottom-up allocator differ from 1:1 plus offset? My impression is that they are mostly identical.
Thanks,
Ingo
tip-bot for Matt Fleming
merge bot[tip:core/urgent] x86/efi: Fix boot crash by mapping EFI memmap entries bottom-up at runtime, instead of top-down
In reply to Matt Flemingx86/efi: Fix boot crash by mapping EFI memmap entries bottom-up at runtime, instead of top-down
Commit-ID: a5caa209ba9c29c6421292e7879d2387a2ef39c9 Gitweb: http://git.kernel.org/tip/a5caa209ba9c29c6421292e7879d2387a2ef39c9 Author: Matt Fleming <[email protected]> AuthorDate: Fri, 25 Sep 2015 23:02:18 +0100 Committer: Ingo Molnar <[email protected]> CommitDate: Thu, 1 Oct 2015 12:51:28 +0200
x86/efi: Fix boot crash by mapping EFI memmap entries bottom-up at runtime, instead of top-down
Beginning with UEFI v2.5 EFI_PROPERTIES_TABLE was introduced that signals that the firmware PE/COFF loader supports splitting code and data sections of PE/COFF images into separate EFI memory map entries. This allows the kernel to map those regions with strict memory protections, e.g. EFI_MEMORY_RO for code, EFI_MEMORY_XP for data, etc.
Unfortunately, an unwritten requirement of this new feature is that the regions need to be mapped with the same offsets relative to each other as observed in the EFI memory map. If this is not done crashes like this may occur,
BUG: unable to handle kernel paging request at fffffffefe6086dd IP: [<fffffffefe6086dd>] 0xfffffffefe6086dd Call Trace: [<ffffffff8104c90e>] efi_call+0x7e/0x100 [<ffffffff81602091>] ? virt_efi_set_variable+0x61/0x90 [<ffffffff8104c583>] efi_delete_dummy_variable+0x63/0x70 [<ffffffff81f4e4aa>] efi_enter_virtual_mode+0x383/0x392 [<ffffffff81f37e1b>] start_kernel+0x38a/0x417 [<ffffffff81f37495>] x86_64_start_reservations+0x2a/0x2c [<ffffffff81f37582>] x86_64_start_kernel+0xeb/0xef
Here 0xfffffffefe6086dd refers to an address the firmware expects to be mapped but which the OS never claimed was mapped. The issue is that included in these regions are relative addresses to other regions which were emitted by the firmware toolchain before the "splitting" of sections occurred at runtime.
Needless to say, we don't satisfy this unwritten requirement on x86_64 and instead map the EFI memory map entries in reverse order. The above crash is almost certainly triggerable with any kernel newer than v3.13 because that's when we rewrote the EFI runtime region mapping code, in commit d2f7cbe7b26a ("x86/efi:
Runtime services virtual mapping"). For kernel versions before v3.13 things may work by pure luck depending on the fragmentation of the kernel virtual address space at the time we map the EFI regions.
Instead of mapping the EFI memory map entries in reverse order, where entry N has a higher virtual address than entry N+1, map them in the same order as they appear in the EFI memory map to preserve this relative offset between regions.
This patch has been kept as small as possible with the intention that it should be applied aggressively to stable and distribution kernels. It is very much a bugfix rather than support for a new feature, since when EFI_PROPERTIES_TABLE is enabled we must map things as outlined above to even boot - we have no way of asking the firmware not to split the code/data regions.
In fact, this patch doesn't even make use of the more strict memory protections available in UEFI v2.5. That will come later.
--- arch/x86/platform/efi/efi.c | 67 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-)
arch/x86/platform/efi/efi.c +66 -1
@@ -705,6 +705,70 @@ out: }/* +* Iterate the EFI memory map in reverse order because the regions +* will be mapped top-down. The end result is the same as if we had +* mapped things forward, but doesn't require us to change the +* existing implementation of efi_map_region(). +*/ +static inline void *efi_map_next_entry_reverse (void *entry) +{ +/* Initial call */ +if (!entry) +return memmap.map_end - memmap.desc_size; + + entry -= memmap.desc_size; +if (entry < memmap.map) +return NULL ; + +return entry; +} + +/* +* efi_map_next_entry - Return the next EFI memory map descriptor +* @entry: Previous EFI memory map descriptor +* +* This is a helper function to iterate over the EFI memory map, which +* we do in different orders depending on the current configuration. +* +* To begin traversing the memory map @entry must be %NULL. +* +* Returns %NULL when we reach the end of the memory map. +*/ +static void *efi_map_next_entry (void *entry) +{ +if (!efi_enabled (EFI_OLD_MEMMAP ) &&efi_enabled (EFI_64BIT )) { +/* +* Starting in UEFI v2.5 the EFI_PROPERTIES_TABLE +* config table feature requires us to map all entries +* in the same order as they appear in the EFI memory +* map. That is to say, entry N must have a lower +* virtual address than entry N+1. This is because the +* firmware toolchain leaves relative references in +* the code/data sections, which are split and become +* separate EFI memory regions. Mapping things +* out-of-order leads to the firmware accessing +* unmapped addresses. +* +* Since we need to map things this way whether or not +* the kernel actually makes use of +* EFI_PROPERTIES_TABLE, let's just switch to this +* scheme by default for 64-bit. +*/ +return efi_map_next_entry_reverse (entry); + } + +/* Initial call */ +if (!entry) +return memmap.map; + + entry += memmap.desc_size; +if (entry >= memmap.map_end) +return NULL ; + +return entry; +} + +/* * Map the efi memory ranges of the runtime services and update new_mmap with * virtual addresses. */
@@ -714,7 +778,8 @@ static void * __init efi_map_regions(int *count, int *pg_shift)unsigned long left =0 ;efi_memory_desc_t *md; -for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { + p =NULL ; +while ((p =efi_map_next_entry (p))) { md = p;if (!(md->attribute &EFI_MEMORY_RUNTIME )) {#ifdef CONFIG_X86_64
tip-bot for Ard Biesheuvel
merge bot[tip:core/urgent] arm64/efi: Fix boot crash by not padding between EFI_MEMORY_RUNTIME regions
In reply to Matt Flemingarm64/efi: Fix boot crash by not padding between EFI_MEMORY_RUNTIME regions
Commit-ID: 0ce3cc008ec04258b6a6314b09f1a6012810881a Gitweb: http://git.kernel.org/tip/0ce3cc008ec04258b6a6314b09f1a6012810881a Author: Ard Biesheuvel <[email protected]> AuthorDate: Fri, 25 Sep 2015 23:02:19 +0100 Committer: Ingo Molnar <[email protected]> CommitDate: Thu, 1 Oct 2015 12:51:28 +0200
arm64/efi: Fix boot crash by not padding between EFI_MEMORY_RUNTIME regions
The new Properties Table feature introduced in UEFIv2.5 may split memory regions that cover PE/COFF memory images into separate code and data regions. Since these regions only differ in the type (runtime code vs runtime data) and the permission bits, but not in the memory type attributes (UC/WC/WT/WB), the spec does not require them to be aligned to 64 KB.
Since the relative offset of PE/COFF .text and .data segments cannot be changed on the fly, this means that we can no longer pad out those regions to be mappable using 64 KB pages. Unfortunately, there is no annotation in the UEFI memory map that identifies data regions that were split off from a code region, so we must apply this logic to all adjacent runtime regions whose attributes only differ in the permission bits.
So instead of rounding each memory region to 64 KB alignment at both ends, only round down regions that are not directly preceded by another runtime region with the same type attributes. Since the UEFI spec does not mandate that the memory map be sorted, this means we also need to sort it first.
Note that this change will result in all EFI_MEMORY_RUNTIME regions whose start addresses are not aligned to the OS page size to be mapped with executable permissions (i.e., on kernels compiled with 64 KB pages). However, since these mappings are only active during the time that UEFI Runtime Services are being invoked, the window for abuse is rather small.
--- arch/arm64/kernel/efi.c | 3 +- drivers/firmware/efi/libstub/arm-stub.c | 88 +++++++++++++++++++++++++++------ 2 files changed, 75 insertions(+), 16 deletions(-)
arch/arm64/kernel/efi.c +2 -1
@@ -258,7 +258,8 @@ static bool __init efi_virtmap_init(void)*/ if (!is_normal_ram (md)) prot =__pgprot (PROT_DEVICE_nGnRE); -else if (md->type ==EFI_RUNTIME_SERVICES_CODE ) +else if (md->type ==EFI_RUNTIME_SERVICES_CODE || + !PAGE_ALIGNED (md->phys_addr)) prot =PAGE_KERNEL_EXEC ;else prot =PAGE_KERNEL ;
drivers/firmware/efi/libstub/arm-stub.c +73 -15
118 lines not on this page · raw message
Re: [tip:core/urgent] x86/efi: Fix boot crash by mapping EFI memmap entries bottom-up at runtime, instead of top-down
In reply to tip-bot for Matt FlemingQuoted 77 lines · tip-bot for Matt Fleming
Commit-ID: a5caa209ba9c29c6421292e7879d2387a2ef39c9 Gitweb: http://git.kernel.org/tip/a5caa209ba9c29c6421292e7879d2387a2ef39c9 Author: Matt Fleming <[email protected]> AuthorDate: Fri, 25 Sep 2015 23:02:18 +0100 Committer: Ingo Molnar <[email protected]> CommitDate: Thu, 1 Oct 2015 12:51:28 +0200x86/efi: Fix boot crash by mapping EFI memmap entries bottom-up at runtime, instead of top-down
Beginning with UEFI v2.5
EFI_PROPERTIES_TABLEwas introduced that signals that the firmware PE/COFF loader supports splitting code and data sections of PE/COFF images into separate EFI memory map entries. This allows the kernel to map those regions with strict memory protections, e.g.EFI_MEMORY_ROfor code,EFI_MEMORY_XPfor data, etc.Unfortunately, an unwritten requirement of this new feature is that the regions need to be mapped with the same offsets relative to each other as observed in the EFI memory map. If this is not done crashes like this may occur,
BUG: unable to handle kernel paging request at fffffffefe6086dd IP: [<fffffffefe6086dd>] 0xfffffffefe6086dd Call Trace: [<ffffffff8104c90e>] efi_call+0x7e/0x100 [<ffffffff81602091>] ? virt_efi_set_variable+0x61/0x90 [<ffffffff8104c583>] efi_delete_dummy_variable+0x63/0x70 [<ffffffff81f4e4aa>] efi_enter_virtual_mode+0x383/0x392 [<ffffffff81f37e1b>] start_kernel+0x38a/0x417 [<ffffffff81f37495>] x86_64_start_reservations+0x2a/0x2c [<ffffffff81f37582>] x86_64_start_kernel+0xeb/0xefHere
0xfffffffefe6086ddrefers to an address the firmware expects to be mapped but which the OS never claimed was mapped. The issue is that included in these regions are relative addresses to other regions which were emitted by the firmware toolchain before the "splitting" of sections occurred at runtime.Needless to say, we don't satisfy this unwritten requirement on
x86_64and instead map the EFI memory map entries in reverse order. The above crash is almost certainly triggerable with any kernel newer than v3.13 because that's when we rewrote the EFI runtime region mapping code, in commitd2f7cbe7b26a("x86/efi:
Runtime services virtual mapping"). For kernel versions before v3.13 things may work by pure luck depending on the fragmentation of the kernel virtual address space at the time we map the EFI regions.Instead of mapping the EFI memory map entries in reverse order, where entry N has a higher virtual address than entry N+1, map them in the same order as they appear in the EFI memory map to preserve this relative offset between regions.
This patch has been kept as small as possible with the intention that it should be applied aggressively to stable and distribution kernels. It is very much a bugfix rather than support for a new feature, since when
EFI_PROPERTIES_TABLEis enabled we must map things as outlined above to even boot - we have no way of asking the firmware not to split the code/data regions.In fact, this patch doesn't even make use of the more strict memory protections available in UEFI v2.5. That will come later.
Suggested-by Ard Biesheuvel <[email protected]>Reported-by Ard Biesheuvel <[email protected]>Signed-off-by Matt Fleming <[email protected]>Cc <[email protected]>Cc Borislav Petkov <[email protected]>Cc Chun-Yi <[email protected]>Cc Dave Young <[email protected]>Cc H. Peter Anvin <[email protected]>Cc James Bottomley <[email protected]>Cc Lee, Chun-Yi <[email protected]>Cc Leif Lindholm <[email protected]>Cc Linus Torvalds <[email protected]>
Quoted 8 lines
Cc Matthew Garrett <[email protected]>Cc Mike Galbraith <[email protected]>Cc Peter Jones <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Thomas Gleixner <[email protected]>Signed-off-by Ingo Molnar <[email protected]>
It's probably a little late to change this now, but I just realised that I dropped Joey's Tested-by tags from this patch. Sorry Joey!
Signature · 1 line
Matt Fleming, Intel Open Source Technology Center