Skip to content

Commit 7b02d53

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 1ca854a commit 7b02d53

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,22 +305,65 @@ 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

258317
if (md->type != EFI_BOOT_SERVICES_CODE &&
259-
md->type != EFI_BOOT_SERVICES_DATA)
318+
md->type != EFI_BOOT_SERVICES_DATA) {
319+
num_entries++;
260320
continue;
321+
}
261322

262323
/* Do not free, someone else owns it: */
263-
if (md->attribute & EFI_MEMORY_RUNTIME)
324+
if (md->attribute & EFI_MEMORY_RUNTIME) {
325+
num_entries++;
264326
continue;
327+
}
265328

266329
free_bootmem_late(start, size);
267330
}
331+
332+
new_size = efi.memmap.desc_size * num_entries;
333+
new_phys = memblock_alloc(new_size, 0);
334+
if (!new_phys) {
335+
pr_err("Failed to allocate new EFI memmap\n");
336+
return;
337+
}
338+
339+
new = memremap(new_phys, new_size, MEMREMAP_WB);
340+
if (!new) {
341+
pr_err("Failed to map new EFI memmap\n");
342+
return;
343+
}
344+
345+
/*
346+
* Build a new EFI memmap that excludes any boot services
347+
* regions that are not tagged EFI_MEMORY_RUNTIME, since those
348+
* regions have now been freed.
349+
*/
350+
new_md = new;
351+
for_each_efi_memory_desc(md) {
352+
if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
353+
(md->type == EFI_BOOT_SERVICES_CODE ||
354+
md->type == EFI_BOOT_SERVICES_DATA))
355+
continue;
356+
357+
memcpy(new_md, md, efi.memmap.desc_size);
358+
new_md += efi.memmap.desc_size;
359+
}
360+
361+
memunmap(new);
362+
363+
if (efi_memmap_install(new_phys, num_entries)) {
364+
pr_err("Could not install new EFI memmap\n");
365+
return;
366+
}
268367
}
269368

270369
/*

drivers/firmware/efi/efi.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <linux/of_fdt.h>
2525
#include <linux/io.h>
2626
#include <linux/platform_device.h>
27+
#include <linux/memblock.h>
2728

2829
#include <asm/early_ioremap.h>
2930

@@ -300,6 +301,35 @@ u64 __init efi_mem_desc_end(efi_memory_desc_t *md)
300301
return end;
301302
}
302303

304+
void __init __weak efi_arch_mem_reserve(phys_addr_t addr, u64 size) {}
305+
306+
/**
307+
* efi_mem_reserve - Reserve an EFI memory region
308+
* @addr: Physical address to reserve
309+
* @size: Size of reservation
310+
*
311+
* Mark a region as reserved from general kernel allocation and
312+
* prevent it being released by efi_free_boot_services().
313+
*
314+
* This function should be called drivers once they've parsed EFI
315+
* configuration tables to figure out where their data lives, e.g.
316+
* efi_esrt_init().
317+
*/
318+
void __init efi_mem_reserve(phys_addr_t addr, u64 size)
319+
{
320+
if (!memblock_is_region_reserved(addr, size))
321+
memblock_reserve(addr, size);
322+
323+
/*
324+
* Some architectures (x86) reserve all boot services ranges
325+
* until efi_free_boot_services() because of buggy firmware
326+
* implementations. This means the above memblock_reserve() is
327+
* superfluous on x86 and instead what it needs to do is
328+
* ensure the @start, @size is not freed.
329+
*/
330+
efi_arch_mem_reserve(addr, size);
331+
}
332+
303333
static __initdata efi_config_table_type_t common_tables[] = {
304334
{ACPI_20_TABLE_GUID, "ACPI 2.0", &efi.acpi20},
305335
{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)