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