Skip to content

Commit f5df269

Browse files
James Morsectmarinas
authored andcommitted
arm64: kernel: Add arch-specific SDEI entry code and CPU masking
The Software Delegated Exception Interface (SDEI) is an ARM standard for registering callbacks from the platform firmware into the OS. This is typically used to implement RAS notifications. Such notifications enter the kernel at the registered entry-point with the register values of the interrupted CPU context. Because this is not a CPU exception, it cannot reuse the existing entry code. (crucially we don't implicitly know which exception level we interrupted), Add the entry point to entry.S to set us up for calling into C code. If the event interrupted code that had interrupts masked, we always return to that location. Otherwise we pretend this was an IRQ, and use SDEI's complete_and_resume call to return to vbar_el1 + offset. This allows the kernel to deliver signals to user space processes. For KVM this triggers the world switch, a quick spin round vcpu_run, then back into the guest, unless there are pending signals. Add sdei_mask_local_cpu() calls to the smp_send_stop() code, this covers the panic() code-path, which doesn't invoke cpuhotplug notifiers. Because we can interrupt entry-from/exit-to another EL, we can't trust the value in sp_el0 or x29, even if we interrupted the kernel, in this case the code in entry.S will save/restore sp_el0 and use the value in __entry_task. When we have VMAP stacks we can interrupt the stack-overflow test, which stirs x0 into sp, meaning we have to have our own VMAP stacks. For now these are allocated when we probe the interface. Future patches will add refcounting hooks to allow the arch code to allocate them lazily. Signed-off-by: James Morse <james.morse@arm.com> Reviewed-by: Catalin Marinas <catalin.marinas@arm.com> Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
1 parent e1281f5 commit f5df269

7 files changed

Lines changed: 384 additions & 3 deletions

File tree

‎arch/arm64/include/asm/sdei.h‎

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,49 @@
33
#ifndef __ASM_SDEI_H
44
#define __ASM_SDEI_H
55

6-
/* Later patches add the arch specific bits */
6+
/* Values for sdei_exit_mode */
7+
#define SDEI_EXIT_HVC 0
8+
#define SDEI_EXIT_SMC 1
79

8-
#endif /* __ASM_SDEI_H */
10+
#define SDEI_STACK_SIZE IRQ_STACK_SIZE
11+
12+
#ifndef __ASSEMBLY__
13+
14+
#include <linux/linkage.h>
15+
#include <linux/preempt.h>
16+
#include <linux/types.h>
17+
18+
#include <asm/virt.h>
19+
20+
extern unsigned long sdei_exit_mode;
21+
22+
/* Software Delegated Exception entry point from firmware*/
23+
asmlinkage void __sdei_asm_handler(unsigned long event_num, unsigned long arg,
24+
unsigned long pc, unsigned long pstate);
25+
26+
/*
27+
* The above entry point does the minimum to call C code. This function does
28+
* anything else, before calling the driver.
29+
*/
30+
struct sdei_registered_event;
31+
asmlinkage unsigned long __sdei_handler(struct pt_regs *regs,
32+
struct sdei_registered_event *arg);
33+
34+
unsigned long sdei_arch_get_entry_point(int conduit);
35+
#define sdei_arch_get_entry_point(x) sdei_arch_get_entry_point(x)
36+
37+
bool _on_sdei_stack(unsigned long sp);
38+
static inline bool on_sdei_stack(unsigned long sp)
39+
{
40+
if (!IS_ENABLED(CONFIG_VMAP_STACK))
41+
return false;
42+
if (!IS_ENABLED(CONFIG_ARM_SDE_INTERFACE))
43+
return false;
44+
if (in_nmi())
45+
return _on_sdei_stack(sp);
46+
47+
return false;
48+
}
49+
50+
#endif /* __ASSEMBLY__ */
51+
#endif /* __ASM_SDEI_H */

‎arch/arm64/include/asm/stacktrace.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include <asm/memory.h>
2424
#include <asm/ptrace.h>
25+
#include <asm/sdei.h>
2526

2627
struct stackframe {
2728
unsigned long fp;
@@ -85,6 +86,8 @@ static inline bool on_accessible_stack(struct task_struct *tsk, unsigned long sp
8586
return true;
8687
if (on_overflow_stack(sp))
8788
return true;
89+
if (on_sdei_stack(sp))
90+
return true;
8891

8992
return false;
9093
}

‎arch/arm64/kernel/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ arm64-obj-$(CONFIG_KEXEC) += machine_kexec.o relocate_kernel.o \
5252
arm64-obj-$(CONFIG_ARM64_RELOC_TEST) += arm64-reloc-test.o
5353
arm64-reloc-test-y := reloc_test_core.o reloc_test_syms.o
5454
arm64-obj-$(CONFIG_CRASH_DUMP) += crash_dump.o
55+
arm64-obj-$(CONFIG_ARM_SDE_INTERFACE) += sdei.o
5556

5657
ifeq ($(CONFIG_KVM),y)
5758
arm64-obj-$(CONFIG_HARDEN_BRANCH_PREDICTOR) += bpi.o

‎arch/arm64/kernel/asm-offsets.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1919
*/
2020

21+
#include <linux/arm_sdei.h>
2122
#include <linux/sched.h>
2223
#include <linux/mm.h>
2324
#include <linux/dma-mapping.h>
@@ -157,6 +158,10 @@ int main(void)
157158
BLANK();
158159
#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
159160
DEFINE(TRAMP_VALIAS, TRAMP_VALIAS);
161+
#endif
162+
#ifdef CONFIG_ARM_SDE_INTERFACE
163+
DEFINE(SDEI_EVENT_INTREGS, offsetof(struct sdei_registered_event, interrupted_regs));
164+
DEFINE(SDEI_EVENT_PRIORITY, offsetof(struct sdei_registered_event, priority));
160165
#endif
161166
return 0;
162167
}

‎arch/arm64/kernel/entry.S‎

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,3 +1153,104 @@ ENTRY(ret_from_fork)
11531153
b ret_to_user
11541154
ENDPROC(ret_from_fork)
11551155
NOKPROBE(ret_from_fork)
1156+
1157+
#ifdef CONFIG_ARM_SDE_INTERFACE
1158+
1159+
#include <asm/sdei.h>
1160+
#include <uapi/linux/arm_sdei.h>
1161+
1162+
/*
1163+
* Software Delegated Exception entry point.
1164+
*
1165+
* x0: Event number
1166+
* x1: struct sdei_registered_event argument from registration time.
1167+
* x2: interrupted PC
1168+
* x3: interrupted PSTATE
1169+
*
1170+
* Firmware has preserved x0->x17 for us, we must save/restore the rest to
1171+
* follow SMC-CC. We save (or retrieve) all the registers as the handler may
1172+
* want them.
1173+
*/
1174+
ENTRY(__sdei_asm_handler)
1175+
stp x2, x3, [x1, #SDEI_EVENT_INTREGS + S_PC]
1176+
stp x4, x5, [x1, #SDEI_EVENT_INTREGS + 16 * 2]
1177+
stp x6, x7, [x1, #SDEI_EVENT_INTREGS + 16 * 3]
1178+
stp x8, x9, [x1, #SDEI_EVENT_INTREGS + 16 * 4]
1179+
stp x10, x11, [x1, #SDEI_EVENT_INTREGS + 16 * 5]
1180+
stp x12, x13, [x1, #SDEI_EVENT_INTREGS + 16 * 6]
1181+
stp x14, x15, [x1, #SDEI_EVENT_INTREGS + 16 * 7]
1182+
stp x16, x17, [x1, #SDEI_EVENT_INTREGS + 16 * 8]
1183+
stp x18, x19, [x1, #SDEI_EVENT_INTREGS + 16 * 9]
1184+
stp x20, x21, [x1, #SDEI_EVENT_INTREGS + 16 * 10]
1185+
stp x22, x23, [x1, #SDEI_EVENT_INTREGS + 16 * 11]
1186+
stp x24, x25, [x1, #SDEI_EVENT_INTREGS + 16 * 12]
1187+
stp x26, x27, [x1, #SDEI_EVENT_INTREGS + 16 * 13]
1188+
stp x28, x29, [x1, #SDEI_EVENT_INTREGS + 16 * 14]
1189+
mov x4, sp
1190+
stp lr, x4, [x1, #SDEI_EVENT_INTREGS + S_LR]
1191+
1192+
mov x19, x1
1193+
1194+
#ifdef CONFIG_VMAP_STACK
1195+
/*
1196+
* entry.S may have been using sp as a scratch register, find whether
1197+
* this is a normal or critical event and switch to the appropriate
1198+
* stack for this CPU.
1199+
*/
1200+
ldrb w4, [x19, #SDEI_EVENT_PRIORITY]
1201+
cbnz w4, 1f
1202+
ldr_this_cpu dst=x5, sym=sdei_stack_normal_ptr, tmp=x6
1203+
b 2f
1204+
1: ldr_this_cpu dst=x5, sym=sdei_stack_critical_ptr, tmp=x6
1205+
2: mov x6, #SDEI_STACK_SIZE
1206+
add x5, x5, x6
1207+
mov sp, x5
1208+
#endif
1209+
1210+
/*
1211+
* We may have interrupted userspace, or a guest, or exit-from or
1212+
* return-to either of these. We can't trust sp_el0, restore it.
1213+
*/
1214+
mrs x28, sp_el0
1215+
ldr_this_cpu dst=x0, sym=__entry_task, tmp=x1
1216+
msr sp_el0, x0
1217+
1218+
/* If we interrupted the kernel point to the previous stack/frame. */
1219+
and x0, x3, #0xc
1220+
mrs x1, CurrentEL
1221+
cmp x0, x1
1222+
csel x29, x29, xzr, eq // fp, or zero
1223+
csel x4, x2, xzr, eq // elr, or zero
1224+
1225+
stp x29, x4, [sp, #-16]!
1226+
mov x29, sp
1227+
1228+
add x0, x19, #SDEI_EVENT_INTREGS
1229+
mov x1, x19
1230+
bl __sdei_handler
1231+
1232+
msr sp_el0, x28
1233+
/* restore regs >x17 that we clobbered */
1234+
ldp x28, x29, [x19, #SDEI_EVENT_INTREGS + 16 * 14]
1235+
ldp lr, x4, [x19, #SDEI_EVENT_INTREGS + S_LR]
1236+
mov sp, x4
1237+
ldp x18, x19, [x19, #SDEI_EVENT_INTREGS + 16 * 9]
1238+
1239+
mov x1, x0 // address to complete_and_resume
1240+
/* x0 = (x0 <= 1) ? EVENT_COMPLETE:EVENT_COMPLETE_AND_RESUME */
1241+
cmp x0, #1
1242+
mov_q x2, SDEI_1_0_FN_SDEI_EVENT_COMPLETE
1243+
mov_q x3, SDEI_1_0_FN_SDEI_EVENT_COMPLETE_AND_RESUME
1244+
csel x0, x2, x3, ls
1245+
1246+
/* On success, this call never returns... */
1247+
ldr_l x2, sdei_exit_mode
1248+
cmp x2, #SDEI_EXIT_SMC
1249+
b.ne 1f
1250+
smc #0
1251+
b .
1252+
1: hvc #0
1253+
b .
1254+
ENDPROC(__sdei_asm_handler)
1255+
NOKPROBE(__sdei_asm_handler)
1256+
#endif /* CONFIG_ARM_SDE_INTERFACE */

0 commit comments

Comments
 (0)