Skip to content

Commit 816e761

Browse files
committed
efi: Allow drivers to reserve boot services forever
Today, it is not possible for drivers to reserve EFI boot services for access after efi_free_boot_services() has been called on x86. For ARM/arm64 it can be done simply by calling memblock_reserve(). Having this ability for all three architectures is desirable for a couple of reasons, 1) It saves drivers copying data out of those regions 2) kexec reboot can now make use of things like ESRT Instead of using the standard memblock_reserve() which is insufficient to reserve the region on x86 (see efi_reserve_boot_services()), a new API is introduced in this patch; efi_mem_reserve(). efi.memmap now always represents which EFI memory regions are available. On x86 the EFI boot services regions that have not been reserved via efi_mem_reserve() will be removed from efi.memmap during efi_free_boot_services(). This has implications for kexec, since it is not possible for a newly kexec'd kernel to access the same boot services regions that the initial boot kernel had access to unless they are reserved by every kexec kernel in the chain. Tested-by: Dave Young <dyoung@redhat.com> [kexec/kdump] Tested-by: Ard Biesheuvel <ard.biesheuvel@linaro.org> [arm] Acked-by: Ard Biesheuvel <ard.biesheuvel@linaro.org> Cc: Leif Lindholm <leif.lindholm@linaro.org> Cc: Peter Jones <pjones@redhat.com> Cc: Borislav Petkov <bp@alien8.de> Cc: Mark Rutland <mark.rutland@arm.com> Signed-off-by: Matt Fleming <matt@codeblueprint.co.uk>
1 parent c45f4da commit 816e761

File tree

3 files changed

+141
-11
lines changed

3 files changed

+141
-11
lines changed

arch/x86/platform/efi/quirks.c

Lines changed: 110 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,71 @@ efi_status_t efi_query_variable_store(u32 attributes, unsigned long size,
163163
}
164164
EXPORT_SYMBOL_GPL(efi_query_variable_store);
165165

166+
/*
167+
* The UEFI specification makes it clear that the operating system is
168+
* free to do whatever it wants with boot services code after
169+
* ExitBootServices() has been called. Ignoring this recommendation a
170+
* significant bunch of EFI implementations continue calling into boot
171+
* services code (SetVirtualAddressMap). In order to work around such
172+
* buggy implementations we reserve boot services region during EFI
173+
* init and make sure it stays executable. Then, after
174+
* SetVirtualAddressMap(), it is discarded.
175+
*
176+
* However, some boot services regions contain data that is required
177+
* by drivers, so we need to track which memory ranges can never be
178+
* freed. This is done by tagging those regions with the
179+
* EFI_MEMORY_RUNTIME attribute.
180+
*
181+
* Any driver that wants to mark a region as reserved must use
182+
* efi_mem_reserve() which will insert a new EFI memory descriptor
183+
* into efi.memmap (splitting existing regions if necessary) and tag
184+
* it with EFI_MEMORY_RUNTIME.
185+
*/
186+
void __init efi_arch_mem_reserve(phys_addr_t addr, u64 size)
187+
{
188+
phys_addr_t new_phys, new_size;
189+
struct efi_mem_range mr;
190+
efi_memory_desc_t md;
191+
int num_entries;
192+
void *new;
193+
194+
if (efi_mem_desc_lookup(addr, &md)) {
195+
pr_err("Failed to lookup EFI memory descriptor for %pa\n", &addr);
196+
return;
197+
}
198+
199+
if (addr + size > md.phys_addr + (md.num_pages << EFI_PAGE_SHIFT)) {
200+
pr_err("Region spans EFI memory descriptors, %pa\n", &addr);
201+
return;
202+
}
203+
204+
mr.range.start = addr;
205+
mr.range.end = addr + size;
206+
mr.attribute = md.attribute | EFI_MEMORY_RUNTIME;
207+
208+
num_entries = efi_memmap_split_count(&md, &mr.range);
209+
num_entries += efi.memmap.nr_map;
210+
211+
new_size = efi.memmap.desc_size * num_entries;
212+
213+
new_phys = memblock_alloc(new_size, 0);
214+
if (!new_phys) {
215+
pr_err("Could not allocate boot services memmap\n");
216+
return;
217+
}
218+
219+
new = early_memremap(new_phys, new_size);
220+
if (!new) {
221+
pr_err("Failed to map new boot services memmap\n");
222+
return;
223+
}
224+
225+
efi_memmap_insert(&efi.memmap, new, &mr);
226+
early_memunmap(new, new_size);
227+
228+
efi_memmap_install(new_phys, num_entries);
229+
}
230+
166231
/*
167232
* Helper function for efi_reserve_boot_services() to figure out if we
168233
* can free regions in efi_free_boot_services().
@@ -184,15 +249,6 @@ static bool can_free_region(u64 start, u64 size)
184249
return true;
185250
}
186251

187-
/*
188-
* The UEFI specification makes it clear that the operating system is free to do
189-
* whatever it wants with boot services code after ExitBootServices() has been
190-
* called. Ignoring this recommendation a significant bunch of EFI implementations
191-
* continue calling into boot services code (SetVirtualAddressMap). In order to
192-
* work around such buggy implementations we reserve boot services region during
193-
* EFI init and make sure it stays executable. Then, after SetVirtualAddressMap(), it
194-
* is discarded.
195-
*/
196252
void __init efi_reserve_boot_services(void)
197253
{
198254
efi_memory_desc_t *md;
@@ -249,20 +305,27 @@ void __init efi_reserve_boot_services(void)
249305

250306
void __init efi_free_boot_services(void)
251307
{
308+
phys_addr_t new_phys, new_size;
252309
efi_memory_desc_t *md;
310+
int num_entries = 0;
311+
void *new, *new_md;
253312

254313
for_each_efi_memory_desc(md) {
255314
unsigned long long start = md->phys_addr;
256315
unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
257316
size_t rm_size;
258317

259318
if (md->type != EFI_BOOT_SERVICES_CODE &&
260-
md->type != EFI_BOOT_SERVICES_DATA)
319+
md->type != EFI_BOOT_SERVICES_DATA) {
320+
num_entries++;
261321
continue;
322+
}
262323

263324
/* Do not free, someone else owns it: */
264-
if (md->attribute & EFI_MEMORY_RUNTIME)
325+
if (md->attribute & EFI_MEMORY_RUNTIME) {
326+
num_entries++;
265327
continue;
328+
}
266329

267330
/*
268331
* Nasty quirk: if all sub-1MB memory is used for boot
@@ -286,6 +349,42 @@ void __init efi_free_boot_services(void)
286349

287350
free_bootmem_late(start, size);
288351
}
352+
353+
new_size = efi.memmap.desc_size * num_entries;
354+
new_phys = memblock_alloc(new_size, 0);
355+
if (!new_phys) {
356+
pr_err("Failed to allocate new EFI memmap\n");
357+
return;
358+
}
359+
360+
new = memremap(new_phys, new_size, MEMREMAP_WB);
361+
if (!new) {
362+
pr_err("Failed to map new EFI memmap\n");
363+
return;
364+
}
365+
366+
/*
367+
* Build a new EFI memmap that excludes any boot services
368+
* regions that are not tagged EFI_MEMORY_RUNTIME, since those
369+
* regions have now been freed.
370+
*/
371+
new_md = new;
372+
for_each_efi_memory_desc(md) {
373+
if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
374+
(md->type == EFI_BOOT_SERVICES_CODE ||
375+
md->type == EFI_BOOT_SERVICES_DATA))
376+
continue;
377+
378+
memcpy(new_md, md, efi.memmap.desc_size);
379+
new_md += efi.memmap.desc_size;
380+
}
381+
382+
memunmap(new);
383+
384+
if (efi_memmap_install(new_phys, num_entries)) {
385+
pr_err("Could not install new EFI memmap\n");
386+
return;
387+
}
289388
}
290389

291390
/*

drivers/firmware/efi/efi.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <linux/slab.h>
2828
#include <linux/acpi.h>
2929
#include <linux/ucs2_string.h>
30+
#include <linux/memblock.h>
3031

3132
#include <asm/early_ioremap.h>
3233

@@ -396,6 +397,35 @@ u64 __init efi_mem_desc_end(efi_memory_desc_t *md)
396397
return end;
397398
}
398399

400+
void __init __weak efi_arch_mem_reserve(phys_addr_t addr, u64 size) {}
401+
402+
/**
403+
* efi_mem_reserve - Reserve an EFI memory region
404+
* @addr: Physical address to reserve
405+
* @size: Size of reservation
406+
*
407+
* Mark a region as reserved from general kernel allocation and
408+
* prevent it being released by efi_free_boot_services().
409+
*
410+
* This function should be called drivers once they've parsed EFI
411+
* configuration tables to figure out where their data lives, e.g.
412+
* efi_esrt_init().
413+
*/
414+
void __init efi_mem_reserve(phys_addr_t addr, u64 size)
415+
{
416+
if (!memblock_is_region_reserved(addr, size))
417+
memblock_reserve(addr, size);
418+
419+
/*
420+
* Some architectures (x86) reserve all boot services ranges
421+
* until efi_free_boot_services() because of buggy firmware
422+
* implementations. This means the above memblock_reserve() is
423+
* superfluous on x86 and instead what it needs to do is
424+
* ensure the @start, @size is not freed.
425+
*/
426+
efi_arch_mem_reserve(addr, size);
427+
}
428+
399429
static __initdata efi_config_table_type_t common_tables[] = {
400430
{ACPI_20_TABLE_GUID, "ACPI 2.0", &efi.acpi20},
401431
{ACPI_TABLE_GUID, "ACPI", &efi.acpi},

include/linux/efi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -944,6 +944,7 @@ extern u64 efi_mem_attribute (unsigned long phys_addr, unsigned long size);
944944
extern int __init efi_uart_console_only (void);
945945
extern u64 efi_mem_desc_end(efi_memory_desc_t *md);
946946
extern int efi_mem_desc_lookup(u64 phys_addr, efi_memory_desc_t *out_md);
947+
extern void efi_mem_reserve(phys_addr_t addr, u64 size);
947948
extern void efi_initialize_iomem_resources(struct resource *code_resource,
948949
struct resource *data_resource, struct resource *bss_resource);
949950
extern void efi_reserve_boot_services(void);

0 commit comments

Comments
 (0)