Linux 4.18.10
[linux/fpc-iii.git] / arch / arm64 / kernel / sdei.c
blob6b8d90d5ceaece66e937e1f57651fd1719cf536c
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2017 Arm Ltd.
3 #define pr_fmt(fmt) "sdei: " fmt
5 #include <linux/arm_sdei.h>
6 #include <linux/hardirq.h>
7 #include <linux/irqflags.h>
8 #include <linux/sched/task_stack.h>
9 #include <linux/uaccess.h>
11 #include <asm/alternative.h>
12 #include <asm/kprobes.h>
13 #include <asm/mmu.h>
14 #include <asm/ptrace.h>
15 #include <asm/sections.h>
16 #include <asm/sysreg.h>
17 #include <asm/vmap_stack.h>
19 unsigned long sdei_exit_mode;
22 * VMAP'd stacks checking for stack overflow on exception using sp as a scratch
23 * register, meaning SDEI has to switch to its own stack. We need two stacks as
24 * a critical event may interrupt a normal event that has just taken a
25 * synchronous exception, and is using sp as scratch register. For a critical
26 * event interrupting a normal event, we can't reliably tell if we were on the
27 * sdei stack.
28 * For now, we allocate stacks when the driver is probed.
30 DECLARE_PER_CPU(unsigned long *, sdei_stack_normal_ptr);
31 DECLARE_PER_CPU(unsigned long *, sdei_stack_critical_ptr);
33 #ifdef CONFIG_VMAP_STACK
34 DEFINE_PER_CPU(unsigned long *, sdei_stack_normal_ptr);
35 DEFINE_PER_CPU(unsigned long *, sdei_stack_critical_ptr);
36 #endif
38 static void _free_sdei_stack(unsigned long * __percpu *ptr, int cpu)
40 unsigned long *p;
42 p = per_cpu(*ptr, cpu);
43 if (p) {
44 per_cpu(*ptr, cpu) = NULL;
45 vfree(p);
49 static void free_sdei_stacks(void)
51 int cpu;
53 for_each_possible_cpu(cpu) {
54 _free_sdei_stack(&sdei_stack_normal_ptr, cpu);
55 _free_sdei_stack(&sdei_stack_critical_ptr, cpu);
59 static int _init_sdei_stack(unsigned long * __percpu *ptr, int cpu)
61 unsigned long *p;
63 p = arch_alloc_vmap_stack(SDEI_STACK_SIZE, cpu_to_node(cpu));
64 if (!p)
65 return -ENOMEM;
66 per_cpu(*ptr, cpu) = p;
68 return 0;
71 static int init_sdei_stacks(void)
73 int cpu;
74 int err = 0;
76 for_each_possible_cpu(cpu) {
77 err = _init_sdei_stack(&sdei_stack_normal_ptr, cpu);
78 if (err)
79 break;
80 err = _init_sdei_stack(&sdei_stack_critical_ptr, cpu);
81 if (err)
82 break;
85 if (err)
86 free_sdei_stacks();
88 return err;
91 bool _on_sdei_stack(unsigned long sp)
93 unsigned long low, high;
95 if (!IS_ENABLED(CONFIG_VMAP_STACK))
96 return false;
98 low = (unsigned long)raw_cpu_read(sdei_stack_critical_ptr);
99 high = low + SDEI_STACK_SIZE;
101 if (low <= sp && sp < high)
102 return true;
104 low = (unsigned long)raw_cpu_read(sdei_stack_normal_ptr);
105 high = low + SDEI_STACK_SIZE;
107 return (low <= sp && sp < high);
110 unsigned long sdei_arch_get_entry_point(int conduit)
113 * SDEI works between adjacent exception levels. If we booted at EL1 we
114 * assume a hypervisor is marshalling events. If we booted at EL2 and
115 * dropped to EL1 because we don't support VHE, then we can't support
116 * SDEI.
118 if (is_hyp_mode_available() && !is_kernel_in_hyp_mode()) {
119 pr_err("Not supported on this hardware/boot configuration\n");
120 return 0;
123 if (IS_ENABLED(CONFIG_VMAP_STACK)) {
124 if (init_sdei_stacks())
125 return 0;
128 sdei_exit_mode = (conduit == CONDUIT_HVC) ? SDEI_EXIT_HVC : SDEI_EXIT_SMC;
130 #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
131 if (arm64_kernel_unmapped_at_el0()) {
132 unsigned long offset;
134 offset = (unsigned long)__sdei_asm_entry_trampoline -
135 (unsigned long)__entry_tramp_text_start;
136 return TRAMP_VALIAS + offset;
137 } else
138 #endif /* CONFIG_UNMAP_KERNEL_AT_EL0 */
139 return (unsigned long)__sdei_asm_handler;
144 * __sdei_handler() returns one of:
145 * SDEI_EV_HANDLED - success, return to the interrupted context.
146 * SDEI_EV_FAILED - failure, return this error code to firmare.
147 * virtual-address - success, return to this address.
149 static __kprobes unsigned long _sdei_handler(struct pt_regs *regs,
150 struct sdei_registered_event *arg)
152 u32 mode;
153 int i, err = 0;
154 int clobbered_registers = 4;
155 u64 elr = read_sysreg(elr_el1);
156 u32 kernel_mode = read_sysreg(CurrentEL) | 1; /* +SPSel */
157 unsigned long vbar = read_sysreg(vbar_el1);
159 if (arm64_kernel_unmapped_at_el0())
160 clobbered_registers++;
162 /* Retrieve the missing registers values */
163 for (i = 0; i < clobbered_registers; i++) {
164 /* from within the handler, this call always succeeds */
165 sdei_api_event_context(i, &regs->regs[i]);
169 * We didn't take an exception to get here, set PAN. UAO will be cleared
170 * by sdei_event_handler()s set_fs(USER_DS) call.
172 __uaccess_enable_hw_pan();
174 err = sdei_event_handler(regs, arg);
175 if (err)
176 return SDEI_EV_FAILED;
178 if (elr != read_sysreg(elr_el1)) {
180 * We took a synchronous exception from the SDEI handler.
181 * This could deadlock, and if you interrupt KVM it will
182 * hyp-panic instead.
184 pr_warn("unsafe: exception during handler\n");
187 mode = regs->pstate & (PSR_MODE32_BIT | PSR_MODE_MASK);
190 * If we interrupted the kernel with interrupts masked, we always go
191 * back to wherever we came from.
193 if (mode == kernel_mode && !interrupts_enabled(regs))
194 return SDEI_EV_HANDLED;
197 * Otherwise, we pretend this was an IRQ. This lets user space tasks
198 * receive signals before we return to them, and KVM to invoke it's
199 * world switch to do the same.
201 * See DDI0487B.a Table D1-7 'Vector offsets from vector table base
202 * address'.
204 if (mode == kernel_mode)
205 return vbar + 0x280;
206 else if (mode & PSR_MODE32_BIT)
207 return vbar + 0x680;
209 return vbar + 0x480;
213 asmlinkage __kprobes notrace unsigned long
214 __sdei_handler(struct pt_regs *regs, struct sdei_registered_event *arg)
216 unsigned long ret;
217 bool do_nmi_exit = false;
220 * nmi_enter() deals with printk() re-entrance and use of RCU when
221 * RCU believed this CPU was idle. Because critical events can
222 * interrupt normal events, we may already be in_nmi().
224 if (!in_nmi()) {
225 nmi_enter();
226 do_nmi_exit = true;
229 ret = _sdei_handler(regs, arg);
231 if (do_nmi_exit)
232 nmi_exit();
234 return ret;