Skip to content

Commit 23715a2

Browse files
committed
arm64: efi: Recover from synchronous exceptions occurring in firmware
Unlike x86, which has machinery to deal with page faults that occur during the execution of EFI runtime services, arm64 has nothing like that, and a synchronous exception raised by firmware code brings down the whole system. With more EFI based systems appearing that were not built to run Linux (such as the Windows-on-ARM laptops based on Qualcomm SOCs), as well as the introduction of PRM (platform specific firmware routines that are callable just like EFI runtime services), we are more likely to run into issues of this sort, and it is much more likely that we can identify and work around such issues if they don't bring down the system entirely. Since we already use a EFI runtime services call wrapper in assembler, we can quite easily add some code that captures the execution state at the point where the call is made, allowing us to revert to this state and proceed execution if the call triggered a synchronous exception. Given that the kernel and the firmware don't share any data structures that could end up in an indeterminate state, we can happily continue running, as long as we mark the EFI runtime services as unavailable from that point on. Signed-off-by: Ard Biesheuvel <[email protected]> Acked-by: Catalin Marinas <[email protected]>
1 parent f11a74b commit 23715a2

File tree

4 files changed

+69
-2
lines changed

4 files changed

+69
-2
lines changed

arch/arm64/include/asm/efi.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,16 @@
1414

1515
#ifdef CONFIG_EFI
1616
extern void efi_init(void);
17+
18+
bool efi_runtime_fixup_exception(struct pt_regs *regs, const char *msg);
1719
#else
1820
#define efi_init()
21+
22+
static inline
23+
bool efi_runtime_fixup_exception(struct pt_regs *regs, const char *msg)
24+
{
25+
return false;
26+
}
1927
#endif
2028

2129
int efi_create_mapping(struct mm_struct *mm, efi_memory_desc_t *md);

arch/arm64/kernel/efi-rt-wrapper.S

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <linux/linkage.h>
77

88
SYM_FUNC_START(__efi_rt_asm_wrapper)
9-
stp x29, x30, [sp, #-32]!
9+
stp x29, x30, [sp, #-112]!
1010
mov x29, sp
1111

1212
/*
@@ -16,6 +16,20 @@ SYM_FUNC_START(__efi_rt_asm_wrapper)
1616
*/
1717
stp x1, x18, [sp, #16]
1818

19+
/*
20+
* Preserve all callee saved registers and record the stack pointer
21+
* value in a per-CPU variable so we can recover from synchronous
22+
* exceptions occurring while running the firmware routines.
23+
*/
24+
stp x19, x20, [sp, #32]
25+
stp x21, x22, [sp, #48]
26+
stp x23, x24, [sp, #64]
27+
stp x25, x26, [sp, #80]
28+
stp x27, x28, [sp, #96]
29+
30+
adr_this_cpu x8, __efi_rt_asm_recover_sp, x9
31+
str x29, [x8]
32+
1933
/*
2034
* We are lucky enough that no EFI runtime services take more than
2135
* 5 arguments, so all are passed in registers rather than via the
@@ -31,7 +45,7 @@ SYM_FUNC_START(__efi_rt_asm_wrapper)
3145

3246
ldp x1, x2, [sp, #16]
3347
cmp x2, x18
34-
ldp x29, x30, [sp], #32
48+
ldp x29, x30, [sp], #112
3549
b.ne 0f
3650
ret
3751
0:
@@ -45,3 +59,18 @@ SYM_FUNC_START(__efi_rt_asm_wrapper)
4559
mov x18, x2
4660
b efi_handle_corrupted_x18 // tail call
4761
SYM_FUNC_END(__efi_rt_asm_wrapper)
62+
63+
SYM_FUNC_START(__efi_rt_asm_recover)
64+
ldr_this_cpu x8, __efi_rt_asm_recover_sp, x9
65+
mov sp, x8
66+
67+
ldp x0, x18, [sp, #16]
68+
ldp x19, x20, [sp, #32]
69+
ldp x21, x22, [sp, #48]
70+
ldp x23, x24, [sp, #64]
71+
ldp x25, x26, [sp, #80]
72+
ldp x27, x28, [sp, #96]
73+
ldp x29, x30, [sp], #112
74+
75+
b efi_handle_runtime_exception
76+
SYM_FUNC_END(__efi_rt_asm_recover)

arch/arm64/kernel/efi.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <linux/efi.h>
1111
#include <linux/init.h>
12+
#include <linux/percpu.h>
1213

1314
#include <asm/efi.h>
1415

@@ -128,3 +129,28 @@ asmlinkage efi_status_t efi_handle_corrupted_x18(efi_status_t s, const char *f)
128129
pr_err_ratelimited(FW_BUG "register x18 corrupted by EFI %s\n", f);
129130
return s;
130131
}
132+
133+
asmlinkage DEFINE_PER_CPU(u64, __efi_rt_asm_recover_sp);
134+
135+
asmlinkage efi_status_t __efi_rt_asm_recover(void);
136+
137+
asmlinkage efi_status_t efi_handle_runtime_exception(const char *f)
138+
{
139+
pr_err(FW_BUG "Synchronous exception occurred in EFI runtime service %s()\n", f);
140+
clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
141+
return EFI_ABORTED;
142+
}
143+
144+
bool efi_runtime_fixup_exception(struct pt_regs *regs, const char *msg)
145+
{
146+
/* Check whether the exception occurred while running the firmware */
147+
if (current_work() != &efi_rts_work.work || regs->pc >= TASK_SIZE_64)
148+
return false;
149+
150+
pr_err(FW_BUG "Unable to handle %s in EFI runtime service\n", msg);
151+
add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK);
152+
dump_stack();
153+
154+
regs->pc = (u64)__efi_rt_asm_recover;
155+
return true;
156+
}

arch/arm64/mm/fault.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <asm/bug.h>
3131
#include <asm/cmpxchg.h>
3232
#include <asm/cpufeature.h>
33+
#include <asm/efi.h>
3334
#include <asm/exception.h>
3435
#include <asm/daifflags.h>
3536
#include <asm/debug-monitors.h>
@@ -391,6 +392,9 @@ static void __do_kernel_fault(unsigned long addr, unsigned long esr,
391392
msg = "paging request";
392393
}
393394

395+
if (efi_runtime_fixup_exception(regs, msg))
396+
return;
397+
394398
die_kernel_fault(msg, addr, esr, regs);
395399
}
396400

0 commit comments

Comments
 (0)