Skip to content

Commit e8dfdf3

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 ff7a167 commit e8dfdf3

File tree

5 files changed

+62
-5
lines changed

5 files changed

+62
-5
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: 27 additions & 5 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,11 +16,21 @@ SYM_FUNC_START(__efi_rt_asm_wrapper)
1616
*/
1717
stp x1, x18, [sp, #16]
1818

19+
/*
20+
* Preserve all callee saved registers and preserve the stack pointer
21+
* value at the base of the EFI runtime stack so we can recover from
22+
* synchronous exceptions occurring while executing the firmware
23+
* routines.
24+
*/
25+
stp x19, x20, [sp, #32]
26+
stp x21, x22, [sp, #48]
27+
stp x23, x24, [sp, #64]
28+
stp x25, x26, [sp, #80]
29+
stp x27, x28, [sp, #96]
30+
1931
ldr_l x16, efi_rt_stack_top
2032
mov sp, x16
21-
#ifdef CONFIG_SHADOW_CALL_STACK
22-
str x18, [sp, #-16]!
23-
#endif
33+
stp x18, x29, [sp, #-16]!
2434

2535
/*
2636
* We are lucky enough that no EFI runtime services take more than
@@ -38,7 +48,7 @@ SYM_FUNC_START(__efi_rt_asm_wrapper)
3848
mov sp, x29
3949
ldp x1, x2, [sp, #16]
4050
cmp x2, x18
41-
ldp x29, x30, [sp], #32
51+
ldp x29, x30, [sp], #112
4252
b.ne 0f
4353
ret
4454
0:
@@ -56,3 +66,15 @@ SYM_FUNC_START(__efi_rt_asm_wrapper)
5666

5767
b efi_handle_corrupted_x18 // tail call
5868
SYM_FUNC_END(__efi_rt_asm_wrapper)
69+
70+
SYM_CODE_START(__efi_rt_asm_recover)
71+
mov sp, x30
72+
73+
ldp x19, x20, [sp, #32]
74+
ldp x21, x22, [sp, #48]
75+
ldp x23, x24, [sp, #64]
76+
ldp x25, x26, [sp, #80]
77+
ldp x27, x28, [sp, #96]
78+
ldp x29, x30, [sp], #112
79+
ret
80+
SYM_CODE_END(__efi_rt_asm_recover)

arch/arm64/kernel/efi.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,28 @@ DEFINE_SPINLOCK(efi_rt_lock);
149149

150150
asmlinkage u64 *efi_rt_stack_top __ro_after_init;
151151

152+
asmlinkage efi_status_t __efi_rt_asm_recover(void);
153+
154+
bool efi_runtime_fixup_exception(struct pt_regs *regs, const char *msg)
155+
{
156+
/* Check whether the exception occurred while running the firmware */
157+
if (current_work() != &efi_rts_work.work || regs->pc >= TASK_SIZE_64)
158+
return false;
159+
160+
pr_err(FW_BUG "Unable to handle %s in EFI runtime service\n", msg);
161+
add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK);
162+
clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
163+
164+
regs->regs[0] = EFI_ABORTED;
165+
regs->regs[30] = efi_rt_stack_top[-1];
166+
regs->pc = (u64)__efi_rt_asm_recover;
167+
168+
if (IS_ENABLED(CONFIG_SHADOW_CALL_STACK))
169+
regs->regs[18] = efi_rt_stack_top[-2];
170+
171+
return true;
172+
}
173+
152174
/* EFI requires 8 KiB of stack space for runtime services */
153175
static_assert(THREAD_SIZE >= SZ_8K);
154176

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

drivers/firmware/efi/runtime-wrappers.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ struct efi_runtime_work efi_rts_work;
8383
else \
8484
pr_err("Failed to queue work to efi_rts_wq.\n"); \
8585
\
86+
WARN_ON_ONCE(efi_rts_work.status == EFI_ABORTED); \
8687
exit: \
8788
efi_rts_work.efi_rts_id = EFI_NONE; \
8889
efi_rts_work.status; \

0 commit comments

Comments
 (0)