Skip to content

Commit 0e72a6a

Browse files
jwrdegoedeardbiesheuvel
authored andcommitted
efi: Export boot-services code and data as debugfs-blobs
Sometimes it is useful to be able to dump the efi boot-services code and data. This commit adds these as debugfs-blobs to /sys/kernel/debug/efi, but only if efi=debug is passed on the kernel-commandline as this requires not freeing those memory-regions, which costs 20+ MB of RAM. Reviewed-by: Greg Kroah-Hartman <[email protected]> Acked-by: Ard Biesheuvel <[email protected]> Signed-off-by: Hans de Goede <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Ard Biesheuvel <[email protected]>
1 parent bb6d3fb commit 0e72a6a

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

arch/x86/platform/efi/efi.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ int __init efi_memblock_x86_reserve_range(void)
243243
efi.memmap.desc_version);
244244

245245
memblock_reserve(pmap, efi.memmap.nr_map * efi.memmap.desc_size);
246+
set_bit(EFI_PRESERVE_BS_REGIONS, &efi.flags);
246247

247248
return 0;
248249
}

arch/x86/platform/efi/quirks.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,10 @@ void __init efi_free_boot_services(void)
410410
int num_entries = 0;
411411
void *new, *new_md;
412412

413+
/* Keep all regions for /sys/kernel/debug/efi */
414+
if (efi_enabled(EFI_DBG))
415+
return;
416+
413417
for_each_efi_memory_desc(md) {
414418
unsigned long long start = md->phys_addr;
415419
unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;

drivers/firmware/efi/efi.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <linux/kobject.h>
1818
#include <linux/module.h>
1919
#include <linux/init.h>
20+
#include <linux/debugfs.h>
2021
#include <linux/device.h>
2122
#include <linux/efi.h>
2223
#include <linux/of.h>
@@ -325,6 +326,59 @@ static __init int efivar_ssdt_load(void)
325326
static inline int efivar_ssdt_load(void) { return 0; }
326327
#endif
327328

329+
#ifdef CONFIG_DEBUG_FS
330+
331+
#define EFI_DEBUGFS_MAX_BLOBS 32
332+
333+
static struct debugfs_blob_wrapper debugfs_blob[EFI_DEBUGFS_MAX_BLOBS];
334+
335+
static void __init efi_debugfs_init(void)
336+
{
337+
struct dentry *efi_debugfs;
338+
efi_memory_desc_t *md;
339+
char name[32];
340+
int type_count[EFI_BOOT_SERVICES_DATA + 1] = {};
341+
int i = 0;
342+
343+
efi_debugfs = debugfs_create_dir("efi", NULL);
344+
if (IS_ERR_OR_NULL(efi_debugfs))
345+
return;
346+
347+
for_each_efi_memory_desc(md) {
348+
switch (md->type) {
349+
case EFI_BOOT_SERVICES_CODE:
350+
snprintf(name, sizeof(name), "boot_services_code%d",
351+
type_count[md->type]++);
352+
break;
353+
case EFI_BOOT_SERVICES_DATA:
354+
snprintf(name, sizeof(name), "boot_services_data%d",
355+
type_count[md->type]++);
356+
break;
357+
default:
358+
continue;
359+
}
360+
361+
if (i >= EFI_DEBUGFS_MAX_BLOBS) {
362+
pr_warn("More then %d EFI boot service segments, only showing first %d in debugfs\n",
363+
EFI_DEBUGFS_MAX_BLOBS, EFI_DEBUGFS_MAX_BLOBS);
364+
break;
365+
}
366+
367+
debugfs_blob[i].size = md->num_pages << EFI_PAGE_SHIFT;
368+
debugfs_blob[i].data = memremap(md->phys_addr,
369+
debugfs_blob[i].size,
370+
MEMREMAP_WB);
371+
if (!debugfs_blob[i].data)
372+
continue;
373+
374+
debugfs_create_blob(name, 0400, efi_debugfs, &debugfs_blob[i]);
375+
i++;
376+
}
377+
}
378+
#else
379+
static inline void efi_debugfs_init(void) {}
380+
#endif
381+
328382
/*
329383
* We register the efi subsystem with the firmware subsystem and the
330384
* efivars subsystem with the efi subsystem, if the system was booted with
@@ -381,6 +435,9 @@ static int __init efisubsys_init(void)
381435
goto err_remove_group;
382436
}
383437

438+
if (efi_enabled(EFI_DBG) && efi_enabled(EFI_PRESERVE_BS_REGIONS))
439+
efi_debugfs_init();
440+
384441
return 0;
385442

386443
err_remove_group:

include/linux/efi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,7 @@ extern int __init efi_setup_pcdp_console(char *);
11241124
#define EFI_NX_PE_DATA 9 /* Can runtime data regions be mapped non-executable? */
11251125
#define EFI_MEM_ATTR 10 /* Did firmware publish an EFI_MEMORY_ATTRIBUTES table? */
11261126
#define EFI_MEM_NO_SOFT_RESERVE 11 /* Is the kernel configured to ignore soft reservations? */
1127+
#define EFI_PRESERVE_BS_REGIONS 12 /* Are EFI boot-services memory segments available? */
11271128

11281129
#ifdef CONFIG_EFI
11291130
/*

0 commit comments

Comments
 (0)