Skip to content

Commit de3fbb2

Browse files
committed
Merge branch 'efi-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull EFI updates from Ingo Molnar: "The main changes are: - Add support for enlisting the help of the EFI firmware to create memory reservations that persist across kexec. - Add page fault handling to the runtime services support code on x86 so we can more gracefully recover from buggy EFI firmware. - Fix command line handling on x86 for the boot path that omits the stub's PE/COFF entry point. - Other assorted fixes and updates" * 'efi-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: x86: boot: Fix EFI stub alignment efi/x86: Call efi_parse_options() from efi_main() efi/x86: earlyprintk - Add 64bit efi fb address support efi/x86: drop task_lock() from efi_switch_mm() efi/x86: Handle page faults occurring while running EFI runtime services efi: Make efi_rts_work accessible to efi page fault handler efi/efi_test: add exporting ResetSystem runtime service efi/libstub: arm: support building with clang efi: add API to reserve memory persistently across kexec reboot efi/arm: libstub: add a root memreserve config table efi: honour memory reservations passed via a linux specific config table
2 parents cee1352 + fa70f0d commit de3fbb2

14 files changed

Lines changed: 307 additions & 54 deletions

File tree

‎arch/x86/boot/compressed/eboot.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,7 @@ efi_main(struct efi_config *c, struct boot_params *boot_params)
738738
struct desc_struct *desc;
739739
void *handle;
740740
efi_system_table_t *_table;
741+
unsigned long cmdline_paddr;
741742

742743
efi_early = c;
743744

@@ -755,6 +756,15 @@ efi_main(struct efi_config *c, struct boot_params *boot_params)
755756
else
756757
setup_boot_services32(efi_early);
757758

759+
/*
760+
* make_boot_params() may have been called before efi_main(), in which
761+
* case this is the second time we parse the cmdline. This is ok,
762+
* parsing the cmdline multiple times does not have side-effects.
763+
*/
764+
cmdline_paddr = ((u64)hdr->cmd_line_ptr |
765+
((u64)boot_params->ext_cmd_line_ptr << 32));
766+
efi_parse_options((char *)cmdline_paddr);
767+
758768
/*
759769
* If the boot loader gave us a value for secure_boot then we use that,
760770
* otherwise we ask the BIOS.

‎arch/x86/boot/tools/build.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,13 @@ int main(int argc, char ** argv)
391391
die("Unable to mmap '%s': %m", argv[2]);
392392
/* Number of 16-byte paragraphs, including space for a 4-byte CRC */
393393
sys_size = (sz + 15 + 4) / 16;
394+
#ifdef CONFIG_EFI_STUB
395+
/*
396+
* COFF requires minimum 32-byte alignment of sections, and
397+
* adding a signature is problematic without that alignment.
398+
*/
399+
sys_size = (sys_size + 1) & ~1;
400+
#endif
394401

395402
/* Patch the setup code with the appropriate size parameters */
396403
buf[0x1f1] = setup_sectors-1;

‎arch/x86/include/asm/efi.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ extern void __init efi_apply_memmap_quirks(void);
140140
extern int __init efi_reuse_config(u64 tables, int nr_tables);
141141
extern void efi_delete_dummy_variable(void);
142142
extern void efi_switch_mm(struct mm_struct *mm);
143+
extern void efi_recover_from_page_fault(unsigned long phys_addr);
143144

144145
struct efi_setup_data {
145146
u64 fw_vendor;

‎arch/x86/mm/fault.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <linux/prefetch.h> /* prefetchw */
1717
#include <linux/context_tracking.h> /* exception_enter(), ... */
1818
#include <linux/uaccess.h> /* faulthandler_disabled() */
19+
#include <linux/efi.h> /* efi_recover_from_page_fault()*/
1920
#include <linux/mm_types.h>
2021

2122
#include <asm/cpufeature.h> /* boot_cpu_has, ... */
@@ -25,6 +26,7 @@
2526
#include <asm/vsyscall.h> /* emulate_vsyscall */
2627
#include <asm/vm86.h> /* struct vm86 */
2728
#include <asm/mmu_context.h> /* vma_pkey() */
29+
#include <asm/efi.h> /* efi_recover_from_page_fault()*/
2830

2931
#define CREATE_TRACE_POINTS
3032
#include <asm/trace/exceptions.h>
@@ -788,6 +790,13 @@ no_context(struct pt_regs *regs, unsigned long error_code,
788790
if (is_errata93(regs, address))
789791
return;
790792

793+
/*
794+
* Buggy firmware could access regions which might page fault, try to
795+
* recover from such faults.
796+
*/
797+
if (IS_ENABLED(CONFIG_EFI))
798+
efi_recover_from_page_fault(address);
799+
791800
/*
792801
* Oops. The kernel tried to access some bad page. We'll have to
793802
* terminate things with extreme prejudice:

‎arch/x86/platform/efi/early_printk.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ static bool early_efi_keep;
2626
*/
2727
static __init int early_efi_map_fb(void)
2828
{
29-
unsigned long base, size;
29+
u64 base, size;
3030

3131
if (!early_efi_keep)
3232
return 0;
3333

3434
base = boot_params.screen_info.lfb_base;
35+
if (boot_params.screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE)
36+
base |= (u64)boot_params.screen_info.ext_lfb_base << 32;
3537
size = boot_params.screen_info.lfb_size;
3638
efi_fb = ioremap(base, size);
3739

@@ -46,9 +48,11 @@ early_initcall(early_efi_map_fb);
4648
*/
4749
static __ref void *early_efi_map(unsigned long start, unsigned long len)
4850
{
49-
unsigned long base;
51+
u64 base;
5052

5153
base = boot_params.screen_info.lfb_base;
54+
if (boot_params.screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE)
55+
base |= (u64)boot_params.screen_info.ext_lfb_base << 32;
5256

5357
if (efi_fb)
5458
return (efi_fb + start);

‎arch/x86/platform/efi/efi_64.c‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -619,18 +619,16 @@ void __init efi_dump_pagetable(void)
619619

620620
/*
621621
* Makes the calling thread switch to/from efi_mm context. Can be used
622-
* for SetVirtualAddressMap() i.e. current->active_mm == init_mm as well
623-
* as during efi runtime calls i.e current->active_mm == current_mm.
624-
* We are not mm_dropping()/mm_grabbing() any mm, because we are not
625-
* losing/creating any references.
622+
* in a kernel thread and user context. Preemption needs to remain disabled
623+
* while the EFI-mm is borrowed. mmgrab()/mmdrop() is not used because the mm
624+
* can not change under us.
625+
* It should be ensured that there are no concurent calls to this function.
626626
*/
627627
void efi_switch_mm(struct mm_struct *mm)
628628
{
629-
task_lock(current);
630629
efi_scratch.prev_mm = current->active_mm;
631630
current->active_mm = mm;
632631
switch_mm(efi_scratch.prev_mm, mm, NULL);
633-
task_unlock(current);
634632
}
635633

636634
#ifdef CONFIG_EFI_MIXED

‎arch/x86/platform/efi/quirks.c‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <asm/efi.h>
1717
#include <asm/uv/uv.h>
1818
#include <asm/cpu_device_id.h>
19+
#include <asm/reboot.h>
1920

2021
#define EFI_MIN_RESERVE 5120
2122

@@ -654,3 +655,80 @@ int efi_capsule_setup_info(struct capsule_info *cap_info, void *kbuff,
654655
}
655656

656657
#endif
658+
659+
/*
660+
* If any access by any efi runtime service causes a page fault, then,
661+
* 1. If it's efi_reset_system(), reboot through BIOS.
662+
* 2. If any other efi runtime service, then
663+
* a. Return error status to the efi caller process.
664+
* b. Disable EFI Runtime Services forever and
665+
* c. Freeze efi_rts_wq and schedule new process.
666+
*
667+
* @return: Returns, if the page fault is not handled. This function
668+
* will never return if the page fault is handled successfully.
669+
*/
670+
void efi_recover_from_page_fault(unsigned long phys_addr)
671+
{
672+
if (!IS_ENABLED(CONFIG_X86_64))
673+
return;
674+
675+
/*
676+
* Make sure that an efi runtime service caused the page fault.
677+
* "efi_mm" cannot be used to check if the page fault had occurred
678+
* in the firmware context because efi=old_map doesn't use efi_pgd.
679+
*/
680+
if (efi_rts_work.efi_rts_id == NONE)
681+
return;
682+
683+
/*
684+
* Address range 0x0000 - 0x0fff is always mapped in the efi_pgd, so
685+
* page faulting on these addresses isn't expected.
686+
*/
687+
if (phys_addr >= 0x0000 && phys_addr <= 0x0fff)
688+
return;
689+
690+
/*
691+
* Print stack trace as it might be useful to know which EFI Runtime
692+
* Service is buggy.
693+
*/
694+
WARN(1, FW_BUG "Page fault caused by firmware at PA: 0x%lx\n",
695+
phys_addr);
696+
697+
/*
698+
* Buggy efi_reset_system() is handled differently from other EFI
699+
* Runtime Services as it doesn't use efi_rts_wq. Although,
700+
* native_machine_emergency_restart() says that machine_real_restart()
701+
* could fail, it's better not to compilcate this fault handler
702+
* because this case occurs *very* rarely and hence could be improved
703+
* on a need by basis.
704+
*/
705+
if (efi_rts_work.efi_rts_id == RESET_SYSTEM) {
706+
pr_info("efi_reset_system() buggy! Reboot through BIOS\n");
707+
machine_real_restart(MRR_BIOS);
708+
return;
709+
}
710+
711+
/*
712+
* Before calling EFI Runtime Service, the kernel has switched the
713+
* calling process to efi_mm. Hence, switch back to task_mm.
714+
*/
715+
arch_efi_call_virt_teardown();
716+
717+
/* Signal error status to the efi caller process */
718+
efi_rts_work.status = EFI_ABORTED;
719+
complete(&efi_rts_work.efi_rts_comp);
720+
721+
clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
722+
pr_info("Froze efi_rts_wq and disabled EFI Runtime Services\n");
723+
724+
/*
725+
* Call schedule() in an infinite loop, so that any spurious wake ups
726+
* will never run efi_rts_wq again.
727+
*/
728+
for (;;) {
729+
set_current_state(TASK_IDLE);
730+
schedule();
731+
}
732+
733+
return;
734+
}

‎drivers/firmware/efi/efi.c‎

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ struct efi __read_mostly efi = {
5252
.properties_table = EFI_INVALID_TABLE_ADDR,
5353
.mem_attr_table = EFI_INVALID_TABLE_ADDR,
5454
.rng_seed = EFI_INVALID_TABLE_ADDR,
55-
.tpm_log = EFI_INVALID_TABLE_ADDR
55+
.tpm_log = EFI_INVALID_TABLE_ADDR,
56+
.mem_reserve = EFI_INVALID_TABLE_ADDR,
5657
};
5758
EXPORT_SYMBOL(efi);
5859

@@ -484,6 +485,7 @@ static __initdata efi_config_table_type_t common_tables[] = {
484485
{EFI_MEMORY_ATTRIBUTES_TABLE_GUID, "MEMATTR", &efi.mem_attr_table},
485486
{LINUX_EFI_RANDOM_SEED_TABLE_GUID, "RNG", &efi.rng_seed},
486487
{LINUX_EFI_TPM_EVENT_LOG_GUID, "TPMEventLog", &efi.tpm_log},
488+
{LINUX_EFI_MEMRESERVE_TABLE_GUID, "MEMRESERVE", &efi.mem_reserve},
487489
{NULL_GUID, NULL, NULL},
488490
};
489491

@@ -591,6 +593,29 @@ int __init efi_config_parse_tables(void *config_tables, int count, int sz,
591593
early_memunmap(tbl, sizeof(*tbl));
592594
}
593595

596+
if (efi.mem_reserve != EFI_INVALID_TABLE_ADDR) {
597+
unsigned long prsv = efi.mem_reserve;
598+
599+
while (prsv) {
600+
struct linux_efi_memreserve *rsv;
601+
602+
/* reserve the entry itself */
603+
memblock_reserve(prsv, sizeof(*rsv));
604+
605+
rsv = early_memremap(prsv, sizeof(*rsv));
606+
if (rsv == NULL) {
607+
pr_err("Could not map UEFI memreserve entry!\n");
608+
return -ENOMEM;
609+
}
610+
611+
if (rsv->size)
612+
memblock_reserve(rsv->base, rsv->size);
613+
614+
prsv = rsv->next;
615+
early_memunmap(rsv, sizeof(*rsv));
616+
}
617+
}
618+
594619
return 0;
595620
}
596621

@@ -937,6 +962,38 @@ bool efi_is_table_address(unsigned long phys_addr)
937962
return false;
938963
}
939964

965+
static DEFINE_SPINLOCK(efi_mem_reserve_persistent_lock);
966+
967+
int efi_mem_reserve_persistent(phys_addr_t addr, u64 size)
968+
{
969+
struct linux_efi_memreserve *rsv, *parent;
970+
971+
if (efi.mem_reserve == EFI_INVALID_TABLE_ADDR)
972+
return -ENODEV;
973+
974+
rsv = kmalloc(sizeof(*rsv), GFP_KERNEL);
975+
if (!rsv)
976+
return -ENOMEM;
977+
978+
parent = memremap(efi.mem_reserve, sizeof(*rsv), MEMREMAP_WB);
979+
if (!parent) {
980+
kfree(rsv);
981+
return -ENOMEM;
982+
}
983+
984+
rsv->base = addr;
985+
rsv->size = size;
986+
987+
spin_lock(&efi_mem_reserve_persistent_lock);
988+
rsv->next = parent->next;
989+
parent->next = __pa(rsv);
990+
spin_unlock(&efi_mem_reserve_persistent_lock);
991+
992+
memunmap(parent);
993+
994+
return 0;
995+
}
996+
940997
#ifdef CONFIG_KEXEC
941998
static int update_efi_random_seed(struct notifier_block *nb,
942999
unsigned long code, void *unused)

‎drivers/firmware/efi/libstub/Makefile‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ cflags-$(CONFIG_X86) += -m$(BITS) -D__KERNEL__ -O2 \
1616
cflags-$(CONFIG_ARM64) := $(subst -pg,,$(KBUILD_CFLAGS)) -fpie \
1717
$(DISABLE_STACKLEAK_PLUGIN)
1818
cflags-$(CONFIG_ARM) := $(subst -pg,,$(KBUILD_CFLAGS)) \
19-
-fno-builtin -fpic -mno-single-pic-base
19+
-fno-builtin -fpic \
20+
$(call cc-option,-mno-single-pic-base)
2021

2122
cflags-$(CONFIG_EFI_ARMSTUB) += -I$(srctree)/scripts/dtc/libfdt
2223

‎drivers/firmware/efi/libstub/arm-stub.c‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,31 @@ static struct screen_info *setup_graphics(efi_system_table_t *sys_table_arg)
6969
return si;
7070
}
7171

72+
void install_memreserve_table(efi_system_table_t *sys_table_arg)
73+
{
74+
struct linux_efi_memreserve *rsv;
75+
efi_guid_t memreserve_table_guid = LINUX_EFI_MEMRESERVE_TABLE_GUID;
76+
efi_status_t status;
77+
78+
status = efi_call_early(allocate_pool, EFI_LOADER_DATA, sizeof(*rsv),
79+
(void **)&rsv);
80+
if (status != EFI_SUCCESS) {
81+
pr_efi_err(sys_table_arg, "Failed to allocate memreserve entry!\n");
82+
return;
83+
}
84+
85+
rsv->next = 0;
86+
rsv->base = 0;
87+
rsv->size = 0;
88+
89+
status = efi_call_early(install_configuration_table,
90+
&memreserve_table_guid,
91+
rsv);
92+
if (status != EFI_SUCCESS)
93+
pr_efi_err(sys_table_arg, "Failed to install memreserve config table!\n");
94+
}
95+
96+
7297
/*
7398
* This function handles the architcture specific differences between arm and
7499
* arm64 regarding where the kernel image must be loaded and any memory that
@@ -235,6 +260,8 @@ unsigned long efi_entry(void *handle, efi_system_table_t *sys_table,
235260
}
236261
}
237262

263+
install_memreserve_table(sys_table);
264+
238265
new_fdt_addr = fdt_addr;
239266
status = allocate_new_fdt_and_exit_boot(sys_table, handle,
240267
&new_fdt_addr, efi_get_max_fdt_addr(dram_base),

0 commit comments

Comments
 (0)