Linux Audio

Check our new training course

Loading...
Note: File does not exist in v3.1.
  1// SPDX-License-Identifier: GPL-2.0
  2// Copyright (C) 2017 Arm Ltd.
  3#define pr_fmt(fmt) "sdei: " fmt
  4
  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>
 12
 13#include <asm/alternative.h>
 14#include <asm/exception.h>
 15#include <asm/kprobes.h>
 16#include <asm/mmu.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>
 22
 23unsigned long sdei_exit_mode;
 24
 25/*
 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
 31 * sdei stack.
 32 * For now, we allocate stacks when the driver is probed.
 33 */
 34DECLARE_PER_CPU(unsigned long *, sdei_stack_normal_ptr);
 35DECLARE_PER_CPU(unsigned long *, sdei_stack_critical_ptr);
 36
 37#ifdef CONFIG_VMAP_STACK
 38DEFINE_PER_CPU(unsigned long *, sdei_stack_normal_ptr);
 39DEFINE_PER_CPU(unsigned long *, sdei_stack_critical_ptr);
 40#endif
 41
 42DECLARE_PER_CPU(unsigned long *, sdei_shadow_call_stack_normal_ptr);
 43DECLARE_PER_CPU(unsigned long *, sdei_shadow_call_stack_critical_ptr);
 44
 45#ifdef CONFIG_SHADOW_CALL_STACK
 46DEFINE_PER_CPU(unsigned long *, sdei_shadow_call_stack_normal_ptr);
 47DEFINE_PER_CPU(unsigned long *, sdei_shadow_call_stack_critical_ptr);
 48#endif
 49
 50static void _free_sdei_stack(unsigned long * __percpu *ptr, int cpu)
 51{
 52	unsigned long *p;
 53
 54	p = per_cpu(*ptr, cpu);
 55	if (p) {
 56		per_cpu(*ptr, cpu) = NULL;
 57		vfree(p);
 58	}
 59}
 60
 61static void free_sdei_stacks(void)
 62{
 63	int cpu;
 64
 65	if (!IS_ENABLED(CONFIG_VMAP_STACK))
 66		return;
 67
 68	for_each_possible_cpu(cpu) {
 69		_free_sdei_stack(&sdei_stack_normal_ptr, cpu);
 70		_free_sdei_stack(&sdei_stack_critical_ptr, cpu);
 71	}
 72}
 73
 74static int _init_sdei_stack(unsigned long * __percpu *ptr, int cpu)
 75{
 76	unsigned long *p;
 77
 78	p = arch_alloc_vmap_stack(SDEI_STACK_SIZE, cpu_to_node(cpu));
 79	if (!p)
 80		return -ENOMEM;
 81	per_cpu(*ptr, cpu) = p;
 82
 83	return 0;
 84}
 85
 86static int init_sdei_stacks(void)
 87{
 88	int cpu;
 89	int err = 0;
 90
 91	if (!IS_ENABLED(CONFIG_VMAP_STACK))
 92		return 0;
 93
 94	for_each_possible_cpu(cpu) {
 95		err = _init_sdei_stack(&sdei_stack_normal_ptr, cpu);
 96		if (err)
 97			break;
 98		err = _init_sdei_stack(&sdei_stack_critical_ptr, cpu);
 99		if (err)
100			break;
101	}
102
103	if (err)
104		free_sdei_stacks();
105
106	return err;
107}
108
109static void _free_sdei_scs(unsigned long * __percpu *ptr, int cpu)
110{
111	void *s;
112
113	s = per_cpu(*ptr, cpu);
114	if (s) {
115		per_cpu(*ptr, cpu) = NULL;
116		scs_free(s);
117	}
118}
119
120static void free_sdei_scs(void)
121{
122	int cpu;
123
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);
127	}
128}
129
130static int _init_sdei_scs(unsigned long * __percpu *ptr, int cpu)
131{
132	void *s;
133
134	s = scs_alloc(cpu_to_node(cpu));
135	if (!s)
136		return -ENOMEM;
137	per_cpu(*ptr, cpu) = s;
138
139	return 0;
140}
141
142static int init_sdei_scs(void)
143{
144	int cpu;
145	int err = 0;
146
147	if (!scs_is_enabled())
148		return 0;
149
150	for_each_possible_cpu(cpu) {
151		err = _init_sdei_scs(&sdei_shadow_call_stack_normal_ptr, cpu);
152		if (err)
153			break;
154		err = _init_sdei_scs(&sdei_shadow_call_stack_critical_ptr, cpu);
155		if (err)
156			break;
157	}
158
159	if (err)
160		free_sdei_scs();
161
162	return err;
163}
164
165unsigned long sdei_arch_get_entry_point(int conduit)
166{
167	/*
168	 * SDEI works between adjacent exception levels. If we booted at EL1 we
169	 * assume a hypervisor is marshalling events. If we booted at EL2 and
170	 * dropped to EL1 because we don't support VHE, then we can't support
171	 * SDEI.
172	 */
173	if (is_hyp_nvhe()) {
174		pr_err("Not supported on this hardware/boot configuration\n");
175		goto out_err;
176	}
177
178	if (init_sdei_stacks())
179		goto out_err;
180
181	if (init_sdei_scs())
182		goto out_err_free_stacks;
183
184	sdei_exit_mode = (conduit == SMCCC_CONDUIT_HVC) ? SDEI_EXIT_HVC : SDEI_EXIT_SMC;
185
186#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
187	if (arm64_kernel_unmapped_at_el0()) {
188		unsigned long offset;
189
190		offset = (unsigned long)__sdei_asm_entry_trampoline -
191			 (unsigned long)__entry_tramp_text_start;
192		return TRAMP_VALIAS + offset;
193	} else
194#endif /* CONFIG_UNMAP_KERNEL_AT_EL0 */
195		return (unsigned long)__sdei_asm_handler;
196
197out_err_free_stacks:
198	free_sdei_stacks();
199out_err:
200	return 0;
201}
202
203/*
204 * do_sdei_event() returns one of:
205 *  SDEI_EV_HANDLED -  success, return to the interrupted context.
206 *  SDEI_EV_FAILED  -  failure, return this error code to firmare.
207 *  virtual-address -  success, return to this address.
208 */
209unsigned long __kprobes do_sdei_event(struct pt_regs *regs,
210				      struct sdei_registered_event *arg)
211{
212	u32 mode;
213	int i, err = 0;
214	int clobbered_registers = 4;
215	u64 elr = read_sysreg(elr_el1);
216	u32 kernel_mode = read_sysreg(CurrentEL) | 1;	/* +SPSel */
217	unsigned long vbar = read_sysreg(vbar_el1);
218
219	if (arm64_kernel_unmapped_at_el0())
220		clobbered_registers++;
221
222	/* Retrieve the missing registers values */
223	for (i = 0; i < clobbered_registers; i++) {
224		/* from within the handler, this call always succeeds */
225		sdei_api_event_context(i, &regs->regs[i]);
226	}
227
228	err = sdei_event_handler(regs, arg);
229	if (err)
230		return SDEI_EV_FAILED;
231
232	if (elr != read_sysreg(elr_el1)) {
233		/*
234		 * We took a synchronous exception from the SDEI handler.
235		 * This could deadlock, and if you interrupt KVM it will
236		 * hyp-panic instead.
237		 */
238		pr_warn("unsafe: exception during handler\n");
239	}
240
241	mode = regs->pstate & (PSR_MODE32_BIT | PSR_MODE_MASK);
242
243	/*
244	 * If we interrupted the kernel with interrupts masked, we always go
245	 * back to wherever we came from.
246	 */
247	if (mode == kernel_mode && !interrupts_enabled(regs))
248		return SDEI_EV_HANDLED;
249
250	/*
251	 * Otherwise, we pretend this was an IRQ. This lets user space tasks
252	 * receive signals before we return to them, and KVM to invoke it's
253	 * world switch to do the same.
254	 *
255	 * See DDI0487B.a Table D1-7 'Vector offsets from vector table base
256	 * address'.
257	 */
258	if (mode == kernel_mode)
259		return vbar + 0x280;
260	else if (mode & PSR_MODE32_BIT)
261		return vbar + 0x680;
262
263	return vbar + 0x480;
264}