• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifdef CONFIG_VMAP_STACK
38 DEFINE_PER_CPU(unsigned long *, sdei_stack_normal_ptr);
39 DEFINE_PER_CPU(unsigned long *, sdei_stack_critical_ptr);
40 #endif
41 
42 DEFINE_PER_CPU(struct sdei_registered_event *, sdei_active_normal_event);
43 DEFINE_PER_CPU(struct sdei_registered_event *, sdei_active_critical_event);
44 
45 DECLARE_PER_CPU(unsigned long *, sdei_shadow_call_stack_normal_ptr);
46 DECLARE_PER_CPU(unsigned long *, sdei_shadow_call_stack_critical_ptr);
47 
48 #ifdef CONFIG_SHADOW_CALL_STACK
49 DEFINE_PER_CPU(unsigned long *, sdei_shadow_call_stack_normal_ptr);
50 DEFINE_PER_CPU(unsigned long *, sdei_shadow_call_stack_critical_ptr);
51 #endif
52 
_free_sdei_stack(unsigned long * __percpu * ptr,int cpu)53 static void _free_sdei_stack(unsigned long * __percpu *ptr, int cpu)
54 {
55 	unsigned long *p;
56 
57 	p = per_cpu(*ptr, cpu);
58 	if (p) {
59 		per_cpu(*ptr, cpu) = NULL;
60 		vfree(p);
61 	}
62 }
63 
free_sdei_stacks(void)64 static void free_sdei_stacks(void)
65 {
66 	int cpu;
67 
68 	if (!IS_ENABLED(CONFIG_VMAP_STACK))
69 		return;
70 
71 	for_each_possible_cpu(cpu) {
72 		_free_sdei_stack(&sdei_stack_normal_ptr, cpu);
73 		_free_sdei_stack(&sdei_stack_critical_ptr, cpu);
74 	}
75 }
76 
_init_sdei_stack(unsigned long * __percpu * ptr,int cpu)77 static int _init_sdei_stack(unsigned long * __percpu *ptr, int cpu)
78 {
79 	unsigned long *p;
80 
81 	p = arch_alloc_vmap_stack(SDEI_STACK_SIZE, cpu_to_node(cpu));
82 	if (!p)
83 		return -ENOMEM;
84 	per_cpu(*ptr, cpu) = p;
85 
86 	return 0;
87 }
88 
init_sdei_stacks(void)89 static int init_sdei_stacks(void)
90 {
91 	int cpu;
92 	int err = 0;
93 
94 	if (!IS_ENABLED(CONFIG_VMAP_STACK))
95 		return 0;
96 
97 	for_each_possible_cpu(cpu) {
98 		err = _init_sdei_stack(&sdei_stack_normal_ptr, cpu);
99 		if (err)
100 			break;
101 		err = _init_sdei_stack(&sdei_stack_critical_ptr, cpu);
102 		if (err)
103 			break;
104 	}
105 
106 	if (err)
107 		free_sdei_stacks();
108 
109 	return err;
110 }
111 
_free_sdei_scs(unsigned long * __percpu * ptr,int cpu)112 static void _free_sdei_scs(unsigned long * __percpu *ptr, int cpu)
113 {
114 	void *s;
115 
116 	s = per_cpu(*ptr, cpu);
117 	if (s) {
118 		per_cpu(*ptr, cpu) = NULL;
119 		scs_free(s);
120 	}
121 }
122 
free_sdei_scs(void)123 static void free_sdei_scs(void)
124 {
125 	int cpu;
126 
127 	for_each_possible_cpu(cpu) {
128 		_free_sdei_scs(&sdei_shadow_call_stack_normal_ptr, cpu);
129 		_free_sdei_scs(&sdei_shadow_call_stack_critical_ptr, cpu);
130 	}
131 }
132 
_init_sdei_scs(unsigned long * __percpu * ptr,int cpu)133 static int _init_sdei_scs(unsigned long * __percpu *ptr, int cpu)
134 {
135 	void *s;
136 
137 	s = scs_alloc(cpu_to_node(cpu));
138 	if (!s)
139 		return -ENOMEM;
140 	per_cpu(*ptr, cpu) = s;
141 
142 	return 0;
143 }
144 
init_sdei_scs(void)145 static int init_sdei_scs(void)
146 {
147 	int cpu;
148 	int err = 0;
149 
150 	if (!IS_ENABLED(CONFIG_SHADOW_CALL_STACK))
151 		return 0;
152 
153 	for_each_possible_cpu(cpu) {
154 		err = _init_sdei_scs(&sdei_shadow_call_stack_normal_ptr, cpu);
155 		if (err)
156 			break;
157 		err = _init_sdei_scs(&sdei_shadow_call_stack_critical_ptr, cpu);
158 		if (err)
159 			break;
160 	}
161 
162 	if (err)
163 		free_sdei_scs();
164 
165 	return err;
166 }
167 
on_sdei_normal_stack(unsigned long sp,struct stack_info * info)168 static bool on_sdei_normal_stack(unsigned long sp, struct stack_info *info)
169 {
170 	unsigned long low = (unsigned long)raw_cpu_read(sdei_stack_normal_ptr);
171 	unsigned long high = low + SDEI_STACK_SIZE;
172 
173 	return on_stack(sp, low, high, STACK_TYPE_SDEI_NORMAL, info);
174 }
175 
on_sdei_critical_stack(unsigned long sp,struct stack_info * info)176 static bool on_sdei_critical_stack(unsigned long sp, struct stack_info *info)
177 {
178 	unsigned long low = (unsigned long)raw_cpu_read(sdei_stack_critical_ptr);
179 	unsigned long high = low + SDEI_STACK_SIZE;
180 
181 	return on_stack(sp, low, high, STACK_TYPE_SDEI_CRITICAL, info);
182 }
183 
_on_sdei_stack(unsigned long sp,struct stack_info * info)184 bool _on_sdei_stack(unsigned long sp, struct stack_info *info)
185 {
186 	if (!IS_ENABLED(CONFIG_VMAP_STACK))
187 		return false;
188 
189 	if (on_sdei_critical_stack(sp, info))
190 		return true;
191 
192 	if (on_sdei_normal_stack(sp, info))
193 		return true;
194 
195 	return false;
196 }
197 
sdei_arch_get_entry_point(int conduit)198 unsigned long sdei_arch_get_entry_point(int conduit)
199 {
200 	/*
201 	 * SDEI works between adjacent exception levels. If we booted at EL1 we
202 	 * assume a hypervisor is marshalling events. If we booted at EL2 and
203 	 * dropped to EL1 because we don't support VHE, then we can't support
204 	 * SDEI.
205 	 */
206 	if (is_hyp_mode_available() && !is_kernel_in_hyp_mode()) {
207 		pr_err("Not supported on this hardware/boot configuration\n");
208 		goto out_err;
209 	}
210 
211 	if (init_sdei_stacks())
212 		goto out_err;
213 
214 	if (init_sdei_scs())
215 		goto out_err_free_stacks;
216 
217 	sdei_exit_mode = (conduit == SMCCC_CONDUIT_HVC) ? SDEI_EXIT_HVC : SDEI_EXIT_SMC;
218 
219 #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
220 	if (arm64_kernel_unmapped_at_el0()) {
221 		unsigned long offset;
222 
223 		offset = (unsigned long)__sdei_asm_entry_trampoline -
224 			 (unsigned long)__entry_tramp_text_start;
225 		return TRAMP_VALIAS + offset;
226 	} else
227 #endif /* CONFIG_UNMAP_KERNEL_AT_EL0 */
228 		return (unsigned long)__sdei_asm_handler;
229 
230 out_err_free_stacks:
231 	free_sdei_stacks();
232 out_err:
233 	return 0;
234 }
235 
236 /*
237  * __sdei_handler() returns one of:
238  *  SDEI_EV_HANDLED -  success, return to the interrupted context.
239  *  SDEI_EV_FAILED  -  failure, return this error code to firmare.
240  *  virtual-address -  success, return to this address.
241  */
_sdei_handler(struct pt_regs * regs,struct sdei_registered_event * arg)242 static __kprobes unsigned long _sdei_handler(struct pt_regs *regs,
243 					     struct sdei_registered_event *arg)
244 {
245 	u32 mode;
246 	int i, err = 0;
247 	int clobbered_registers = 4;
248 	u64 elr = read_sysreg(elr_el1);
249 	u32 kernel_mode = read_sysreg(CurrentEL) | 1;	/* +SPSel */
250 	unsigned long vbar = read_sysreg(vbar_el1);
251 
252 	if (arm64_kernel_unmapped_at_el0())
253 		clobbered_registers++;
254 
255 	/* Retrieve the missing registers values */
256 	for (i = 0; i < clobbered_registers; i++) {
257 		/* from within the handler, this call always succeeds */
258 		sdei_api_event_context(i, &regs->regs[i]);
259 	}
260 
261 	/*
262 	 * We didn't take an exception to get here, set PAN. UAO will be cleared
263 	 * by sdei_event_handler()s force_uaccess_begin() call.
264 	 */
265 	__uaccess_enable_hw_pan();
266 
267 	err = sdei_event_handler(regs, arg);
268 	if (err)
269 		return SDEI_EV_FAILED;
270 
271 	if (elr != read_sysreg(elr_el1)) {
272 		/*
273 		 * We took a synchronous exception from the SDEI handler.
274 		 * This could deadlock, and if you interrupt KVM it will
275 		 * hyp-panic instead.
276 		 */
277 		pr_warn("unsafe: exception during handler\n");
278 	}
279 
280 	mode = regs->pstate & (PSR_MODE32_BIT | PSR_MODE_MASK);
281 
282 	/*
283 	 * If we interrupted the kernel with interrupts masked, we always go
284 	 * back to wherever we came from.
285 	 */
286 	if (mode == kernel_mode && !interrupts_enabled(regs))
287 		return SDEI_EV_HANDLED;
288 
289 	/*
290 	 * Otherwise, we pretend this was an IRQ. This lets user space tasks
291 	 * receive signals before we return to them, and KVM to invoke it's
292 	 * world switch to do the same.
293 	 *
294 	 * See DDI0487B.a Table D1-7 'Vector offsets from vector table base
295 	 * address'.
296 	 */
297 	if (mode == kernel_mode)
298 		return vbar + 0x280;
299 	else if (mode & PSR_MODE32_BIT)
300 		return vbar + 0x680;
301 
302 	return vbar + 0x480;
303 }
304 
305 
306 asmlinkage noinstr unsigned long
__sdei_handler(struct pt_regs * regs,struct sdei_registered_event * arg)307 __sdei_handler(struct pt_regs *regs, struct sdei_registered_event *arg)
308 {
309 	unsigned long ret;
310 
311 	arm64_enter_nmi(regs);
312 
313 	ret = _sdei_handler(regs, arg);
314 
315 	arm64_exit_nmi(regs);
316 
317 	return ret;
318 }
319