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/uaccess.h>
12 #include <asm/alternative.h>
13 #include <asm/kprobes.h>
15 #include <asm/ptrace.h>
16 #include <asm/sections.h>
17 #include <asm/stacktrace.h>
18 #include <asm/sysreg.h>
19 #include <asm/vmap_stack.h>
21 unsigned long sdei_exit_mode;
24 * VMAP'd stacks checking for stack overflow on exception using sp as a scratch
25 * register, meaning SDEI has to switch to its own stack. We need two stacks as
26 * a critical event may interrupt a normal event that has just taken a
27 * synchronous exception, and is using sp as scratch register. For a critical
28 * event interrupting a normal event, we can't reliably tell if we were on the
30 * For now, we allocate stacks when the driver is probed.
32 DECLARE_PER_CPU(unsigned long *, sdei_stack_normal_ptr);
33 DECLARE_PER_CPU(unsigned long *, sdei_stack_critical_ptr);
35 #ifdef CONFIG_VMAP_STACK
36 DEFINE_PER_CPU(unsigned long *, sdei_stack_normal_ptr);
37 DEFINE_PER_CPU(unsigned long *, sdei_stack_critical_ptr);
40 static void _free_sdei_stack(unsigned long * __percpu *ptr, int cpu)
44 p = per_cpu(*ptr, cpu);
46 per_cpu(*ptr, cpu) = NULL;
51 static void free_sdei_stacks(void)
55 for_each_possible_cpu(cpu) {
56 _free_sdei_stack(&sdei_stack_normal_ptr, cpu);
57 _free_sdei_stack(&sdei_stack_critical_ptr, cpu);
61 static int _init_sdei_stack(unsigned long * __percpu *ptr, int cpu)
65 p = arch_alloc_vmap_stack(SDEI_STACK_SIZE, cpu_to_node(cpu));
68 per_cpu(*ptr, cpu) = p;
73 static int init_sdei_stacks(void)
78 for_each_possible_cpu(cpu) {
79 err = _init_sdei_stack(&sdei_stack_normal_ptr, cpu);
82 err = _init_sdei_stack(&sdei_stack_critical_ptr, cpu);
93 static bool on_sdei_normal_stack(unsigned long sp, struct stack_info *info)
95 unsigned long low = (unsigned long)raw_cpu_read(sdei_stack_normal_ptr);
96 unsigned long high = low + SDEI_STACK_SIZE;
98 return on_stack(sp, low, high, STACK_TYPE_SDEI_NORMAL, info);
101 static bool on_sdei_critical_stack(unsigned long sp, struct stack_info *info)
103 unsigned long low = (unsigned long)raw_cpu_read(sdei_stack_critical_ptr);
104 unsigned long high = low + SDEI_STACK_SIZE;
106 return on_stack(sp, low, high, STACK_TYPE_SDEI_CRITICAL, info);
109 bool _on_sdei_stack(unsigned long sp, struct stack_info *info)
111 if (!IS_ENABLED(CONFIG_VMAP_STACK))
114 if (on_sdei_critical_stack(sp, info))
117 if (on_sdei_normal_stack(sp, info))
123 unsigned long sdei_arch_get_entry_point(int conduit)
126 * SDEI works between adjacent exception levels. If we booted at EL1 we
127 * assume a hypervisor is marshalling events. If we booted at EL2 and
128 * dropped to EL1 because we don't support VHE, then we can't support
131 if (is_hyp_mode_available() && !is_kernel_in_hyp_mode()) {
132 pr_err("Not supported on this hardware/boot configuration\n");
136 if (IS_ENABLED(CONFIG_VMAP_STACK)) {
137 if (init_sdei_stacks())
141 sdei_exit_mode = (conduit == SMCCC_CONDUIT_HVC) ? SDEI_EXIT_HVC : SDEI_EXIT_SMC;
143 #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
144 if (arm64_kernel_unmapped_at_el0()) {
145 unsigned long offset;
147 offset = (unsigned long)__sdei_asm_entry_trampoline -
148 (unsigned long)__entry_tramp_text_start;
149 return TRAMP_VALIAS + offset;
151 #endif /* CONFIG_UNMAP_KERNEL_AT_EL0 */
152 return (unsigned long)__sdei_asm_handler;
157 * __sdei_handler() returns one of:
158 * SDEI_EV_HANDLED - success, return to the interrupted context.
159 * SDEI_EV_FAILED - failure, return this error code to firmare.
160 * virtual-address - success, return to this address.
162 static __kprobes unsigned long _sdei_handler(struct pt_regs *regs,
163 struct sdei_registered_event *arg)
167 int clobbered_registers = 4;
168 u64 elr = read_sysreg(elr_el1);
169 u32 kernel_mode = read_sysreg(CurrentEL) | 1; /* +SPSel */
170 unsigned long vbar = read_sysreg(vbar_el1);
172 if (arm64_kernel_unmapped_at_el0())
173 clobbered_registers++;
175 /* Retrieve the missing registers values */
176 for (i = 0; i < clobbered_registers; i++) {
177 /* from within the handler, this call always succeeds */
178 sdei_api_event_context(i, ®s->regs[i]);
182 * We didn't take an exception to get here, set PAN. UAO will be cleared
183 * by sdei_event_handler()s set_fs(USER_DS) call.
185 __uaccess_enable_hw_pan();
187 err = sdei_event_handler(regs, arg);
189 return SDEI_EV_FAILED;
191 if (elr != read_sysreg(elr_el1)) {
193 * We took a synchronous exception from the SDEI handler.
194 * This could deadlock, and if you interrupt KVM it will
197 pr_warn("unsafe: exception during handler\n");
200 mode = regs->pstate & (PSR_MODE32_BIT | PSR_MODE_MASK);
203 * If we interrupted the kernel with interrupts masked, we always go
204 * back to wherever we came from.
206 if (mode == kernel_mode && !interrupts_enabled(regs))
207 return SDEI_EV_HANDLED;
210 * Otherwise, we pretend this was an IRQ. This lets user space tasks
211 * receive signals before we return to them, and KVM to invoke it's
212 * world switch to do the same.
214 * See DDI0487B.a Table D1-7 'Vector offsets from vector table base
217 if (mode == kernel_mode)
219 else if (mode & PSR_MODE32_BIT)
226 asmlinkage __kprobes notrace unsigned long
227 __sdei_handler(struct pt_regs *regs, struct sdei_registered_event *arg)
233 ret = _sdei_handler(regs, arg);