1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2017 Arm Ltd.
3 #define pr_fmt(fmt) "sdei: " fmt
5 #include <linux/arm-smccc.h>
6 #include <linux/arm_sdei.h>
7 #include <linux/hardirq.h>
8 #include <linux/irqflags.h>
9 #include <linux/sched/task_stack.h>
10 #include <linux/scs.h>
11 #include <linux/uaccess.h>
13 #include <asm/alternative.h>
14 #include <asm/exception.h>
15 #include <asm/kprobes.h>
17 #include <asm/ptrace.h>
18 #include <asm/sections.h>
19 #include <asm/stacktrace.h>
20 #include <asm/sysreg.h>
21 #include <asm/vmap_stack.h>
23 unsigned long sdei_exit_mode;
26 * VMAP'd stacks checking for stack overflow on exception using sp as a scratch
27 * register, meaning SDEI has to switch to its own stack. We need two stacks as
28 * a critical event may interrupt a normal event that has just taken a
29 * synchronous exception, and is using sp as scratch register. For a critical
30 * event interrupting a normal event, we can't reliably tell if we were on the
32 * For now, we allocate stacks when the driver is probed.
34 DECLARE_PER_CPU(unsigned long *, sdei_stack_normal_ptr);
35 DECLARE_PER_CPU(unsigned long *, sdei_stack_critical_ptr);
37 #ifdef CONFIG_VMAP_STACK
38 DEFINE_PER_CPU(unsigned long *, sdei_stack_normal_ptr);
39 DEFINE_PER_CPU(unsigned long *, sdei_stack_critical_ptr);
42 DECLARE_PER_CPU(unsigned long *, sdei_shadow_call_stack_normal_ptr);
43 DECLARE_PER_CPU(unsigned long *, sdei_shadow_call_stack_critical_ptr);
45 #ifdef CONFIG_SHADOW_CALL_STACK
46 DEFINE_PER_CPU(unsigned long *, sdei_shadow_call_stack_normal_ptr);
47 DEFINE_PER_CPU(unsigned long *, sdei_shadow_call_stack_critical_ptr);
50 static void _free_sdei_stack(unsigned long * __percpu *ptr, int cpu)
54 p = per_cpu(*ptr, cpu);
56 per_cpu(*ptr, cpu) = NULL;
61 static void free_sdei_stacks(void)
65 if (!IS_ENABLED(CONFIG_VMAP_STACK))
68 for_each_possible_cpu(cpu) {
69 _free_sdei_stack(&sdei_stack_normal_ptr, cpu);
70 _free_sdei_stack(&sdei_stack_critical_ptr, cpu);
74 static int _init_sdei_stack(unsigned long * __percpu *ptr, int cpu)
78 p = arch_alloc_vmap_stack(SDEI_STACK_SIZE, cpu_to_node(cpu));
81 per_cpu(*ptr, cpu) = p;
86 static int init_sdei_stacks(void)
91 if (!IS_ENABLED(CONFIG_VMAP_STACK))
94 for_each_possible_cpu(cpu) {
95 err = _init_sdei_stack(&sdei_stack_normal_ptr, cpu);
98 err = _init_sdei_stack(&sdei_stack_critical_ptr, cpu);
109 static void _free_sdei_scs(unsigned long * __percpu *ptr, int cpu)
113 s = per_cpu(*ptr, cpu);
115 per_cpu(*ptr, cpu) = NULL;
120 static void free_sdei_scs(void)
124 for_each_possible_cpu(cpu) {
125 _free_sdei_scs(&sdei_shadow_call_stack_normal_ptr, cpu);
126 _free_sdei_scs(&sdei_shadow_call_stack_critical_ptr, cpu);
130 static int _init_sdei_scs(unsigned long * __percpu *ptr, int cpu)
134 s = scs_alloc(cpu_to_node(cpu));
137 per_cpu(*ptr, cpu) = s;
142 static int init_sdei_scs(void)
147 if (!IS_ENABLED(CONFIG_SHADOW_CALL_STACK))
150 for_each_possible_cpu(cpu) {
151 err = _init_sdei_scs(&sdei_shadow_call_stack_normal_ptr, cpu);
154 err = _init_sdei_scs(&sdei_shadow_call_stack_critical_ptr, cpu);
165 static bool on_sdei_normal_stack(unsigned long sp, unsigned long size,
166 struct stack_info *info)
168 unsigned long low = (unsigned long)raw_cpu_read(sdei_stack_normal_ptr);
169 unsigned long high = low + SDEI_STACK_SIZE;
171 return on_stack(sp, size, low, high, STACK_TYPE_SDEI_NORMAL, info);
174 static bool on_sdei_critical_stack(unsigned long sp, unsigned long size,
175 struct stack_info *info)
177 unsigned long low = (unsigned long)raw_cpu_read(sdei_stack_critical_ptr);
178 unsigned long high = low + SDEI_STACK_SIZE;
180 return on_stack(sp, size, low, high, STACK_TYPE_SDEI_CRITICAL, info);
183 bool _on_sdei_stack(unsigned long sp, unsigned long size, struct stack_info *info)
185 if (!IS_ENABLED(CONFIG_VMAP_STACK))
188 if (on_sdei_critical_stack(sp, size, info))
191 if (on_sdei_normal_stack(sp, size, info))
197 unsigned long sdei_arch_get_entry_point(int conduit)
200 * SDEI works between adjacent exception levels. If we booted at EL1 we
201 * assume a hypervisor is marshalling events. If we booted at EL2 and
202 * dropped to EL1 because we don't support VHE, then we can't support
206 pr_err("Not supported on this hardware/boot configuration\n");
210 if (init_sdei_stacks())
214 goto out_err_free_stacks;
216 sdei_exit_mode = (conduit == SMCCC_CONDUIT_HVC) ? SDEI_EXIT_HVC : SDEI_EXIT_SMC;
218 #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
219 if (arm64_kernel_unmapped_at_el0()) {
220 unsigned long offset;
222 offset = (unsigned long)__sdei_asm_entry_trampoline -
223 (unsigned long)__entry_tramp_text_start;
224 return TRAMP_VALIAS + offset;
226 #endif /* CONFIG_UNMAP_KERNEL_AT_EL0 */
227 return (unsigned long)__sdei_asm_handler;
236 * do_sdei_event() returns one of:
237 * SDEI_EV_HANDLED - success, return to the interrupted context.
238 * SDEI_EV_FAILED - failure, return this error code to firmare.
239 * virtual-address - success, return to this address.
241 unsigned long __kprobes do_sdei_event(struct pt_regs *regs,
242 struct sdei_registered_event *arg)
246 int clobbered_registers = 4;
247 u64 elr = read_sysreg(elr_el1);
248 u32 kernel_mode = read_sysreg(CurrentEL) | 1; /* +SPSel */
249 unsigned long vbar = read_sysreg(vbar_el1);
251 if (arm64_kernel_unmapped_at_el0())
252 clobbered_registers++;
254 /* Retrieve the missing registers values */
255 for (i = 0; i < clobbered_registers; i++) {
256 /* from within the handler, this call always succeeds */
257 sdei_api_event_context(i, ®s->regs[i]);
260 err = sdei_event_handler(regs, arg);
262 return SDEI_EV_FAILED;
264 if (elr != read_sysreg(elr_el1)) {
266 * We took a synchronous exception from the SDEI handler.
267 * This could deadlock, and if you interrupt KVM it will
270 pr_warn("unsafe: exception during handler\n");
273 mode = regs->pstate & (PSR_MODE32_BIT | PSR_MODE_MASK);
276 * If we interrupted the kernel with interrupts masked, we always go
277 * back to wherever we came from.
279 if (mode == kernel_mode && !interrupts_enabled(regs))
280 return SDEI_EV_HANDLED;
283 * Otherwise, we pretend this was an IRQ. This lets user space tasks
284 * receive signals before we return to them, and KVM to invoke it's
285 * world switch to do the same.
287 * See DDI0487B.a Table D1-7 'Vector offsets from vector table base
290 if (mode == kernel_mode)
292 else if (mode & PSR_MODE32_BIT)