1/*
2 * Copyright (c) 2014-2020, ARM Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <plat_macros.S>
8#include <platform_def.h>
9
10#include <arch.h>
11#include <asm_macros.S>
12#include <context.h>
13#include <lib/el3_runtime/cpu_data.h>
14#include <lib/utils_def.h>
15
16	.globl	report_unhandled_exception
17	.globl	report_unhandled_interrupt
18	.globl	el3_panic
19	.globl	elx_panic
20
21#if CRASH_REPORTING
22
23	/* ------------------------------------------------------
24	 * The below section deals with dumping the system state
25	 * when an unhandled exception is taken in EL3.
26	 * The layout and the names of the registers which will
27	 * be dumped during a unhandled exception is given below.
28	 * ------------------------------------------------------
29	 */
30.section .rodata.crash_prints, "aS"
31print_spacer:
32	.asciz	"             = 0x"
33
34gp_regs:
35	.asciz	"x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",\
36		"x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",\
37		"x16", "x17", "x18", "x19", "x20", "x21", "x22",\
38		"x23", "x24", "x25", "x26", "x27", "x28", "x29", ""
39el3_sys_regs:
40	.asciz	"scr_el3", "sctlr_el3", "cptr_el3", "tcr_el3",\
41		"daif", "mair_el3", "spsr_el3", "elr_el3", "ttbr0_el3",\
42		"esr_el3", "far_el3", ""
43
44non_el3_sys_regs:
45	.asciz	"spsr_el1", "elr_el1", "spsr_abt", "spsr_und",\
46		"spsr_irq", "spsr_fiq", "sctlr_el1", "actlr_el1", "cpacr_el1",\
47		"csselr_el1", "sp_el1", "esr_el1", "ttbr0_el1", "ttbr1_el1",\
48		"mair_el1", "amair_el1", "tcr_el1", "tpidr_el1", "tpidr_el0",\
49		"tpidrro_el0",  "par_el1", "mpidr_el1", "afsr0_el1", "afsr1_el1",\
50		"contextidr_el1", "vbar_el1", "cntp_ctl_el0", "cntp_cval_el0",\
51		"cntv_ctl_el0", "cntv_cval_el0", "cntkctl_el1", "sp_el0", "isr_el1", ""
52
53#if CTX_INCLUDE_AARCH32_REGS
54aarch32_regs:
55	.asciz	"dacr32_el2", "ifsr32_el2", ""
56#endif /* CTX_INCLUDE_AARCH32_REGS */
57
58panic_msg:
59	.asciz "PANIC in EL3.\nx30"
60excpt_msg:
61	.asciz "Unhandled Exception in EL3.\nx30"
62intr_excpt_msg:
63	.ascii "Unhandled Interrupt Exception in EL3.\n"
64x30_msg:
65	.asciz "x30"
66excpt_msg_el:
67	.asciz "Unhandled Exception from EL"
68
69	/*
70	 * Helper function to print from crash buf.
71	 * The print loop is controlled by the buf size and
72	 * ascii reg name list which is passed in x6. The
73	 * function returns the crash buf address in x0.
74	 * Clobbers : x0 - x7, sp
75	 */
76func size_controlled_print
77	/* Save the lr */
78	mov	sp, x30
79	/* load the crash buf address */
80	mrs	x7, tpidr_el3
81test_size_list:
82	/* Calculate x5 always as it will be clobbered by asm_print_hex */
83	mrs	x5, tpidr_el3
84	add	x5, x5, #CPU_DATA_CRASH_BUF_SIZE
85	/* Test whether we have reached end of crash buf */
86	cmp	x7, x5
87	b.eq	exit_size_print
88	ldrb	w4, [x6]
89	/* Test whether we are at end of list */
90	cbz	w4, exit_size_print
91	mov	x4, x6
92	/* asm_print_str updates x4 to point to next entry in list */
93	bl	asm_print_str
94	/* x0 = number of symbols printed + 1 */
95	sub	x0, x4, x6
96	/* update x6 with the updated list pointer */
97	mov	x6, x4
98	bl	print_alignment
99	ldr	x4, [x7], #REGSZ
100	bl	asm_print_hex
101	bl	asm_print_newline
102	b	test_size_list
103exit_size_print:
104	mov	x30, sp
105	ret
106endfunc size_controlled_print
107
108	/* -----------------------------------------------------
109	 * This function calculates and prints required number
110	 * of space characters followed by "= 0x", based on the
111	 * length of ascii register name.
112 	 * x0: length of ascii register name + 1
113	 * ------------------------------------------------------
114 	 */
115func print_alignment
116	/* The minimum ascii length is 3, e.g. for "x0" */
117	adr	x4, print_spacer - 3
118	add	x4, x4, x0
119	b	asm_print_str
120endfunc print_alignment
121
122	/*
123	 * Helper function to store x8 - x15 registers to
124	 * the crash buf. The system registers values are
125	 * copied to x8 to x15 by the caller which are then
126	 * copied to the crash buf by this function.
127	 * x0 points to the crash buf. It then calls
128	 * size_controlled_print to print to console.
129	 * Clobbers : x0 - x7, sp
130	 */
131func str_in_crash_buf_print
132	/* restore the crash buf address in x0 */
133	mrs	x0, tpidr_el3
134	stp	x8, x9, [x0]
135	stp	x10, x11, [x0, #REGSZ * 2]
136	stp	x12, x13, [x0, #REGSZ * 4]
137	stp	x14, x15, [x0, #REGSZ * 6]
138	b	size_controlled_print
139endfunc str_in_crash_buf_print
140
141	/* ------------------------------------------------------
142	 * This macro calculates the offset to crash buf from
143	 * cpu_data and stores it in tpidr_el3. It also saves x0
144	 * and x1 in the crash buf by using sp as a temporary
145	 * register.
146	 * ------------------------------------------------------
147	 */
148	.macro prepare_crash_buf_save_x0_x1
149	/* we can corrupt this reg to free up x0 */
150	mov	sp, x0
151	/* tpidr_el3 contains the address to cpu_data structure */
152	mrs	x0, tpidr_el3
153	/* Calculate the Crash buffer offset in cpu_data */
154	add	x0, x0, #CPU_DATA_CRASH_BUF_OFFSET
155	/* Store crash buffer address in tpidr_el3 */
156	msr	tpidr_el3, x0
157	str	x1, [x0, #REGSZ]
158	mov	x1, sp
159	str	x1, [x0]
160	.endm
161
162	/* -----------------------------------------------------
163	 * This function allows to report a crash (if crash
164	 * reporting is enabled) when an unhandled exception
165	 * occurs. It prints the CPU state via the crash console
166	 * making use of the crash buf. This function will
167	 * not return.
168	 * -----------------------------------------------------
169	 */
170func report_unhandled_exception
171	prepare_crash_buf_save_x0_x1
172	adr	x0, excpt_msg
173	mov	sp, x0
174	/* This call will not return */
175	b	do_crash_reporting
176endfunc report_unhandled_exception
177
178	/* -----------------------------------------------------
179	 * This function allows to report a crash (if crash
180	 * reporting is enabled) when an unhandled interrupt
181	 * occurs. It prints the CPU state via the crash console
182	 * making use of the crash buf. This function will
183	 * not return.
184	 * -----------------------------------------------------
185	 */
186func report_unhandled_interrupt
187	prepare_crash_buf_save_x0_x1
188	adr	x0, intr_excpt_msg
189	mov	sp, x0
190	/* This call will not return */
191	b	do_crash_reporting
192endfunc report_unhandled_interrupt
193
194	/* -----------------------------------------------------
195	 * This function allows to report a crash from the lower
196	 * exception level (if crash reporting is enabled) when
197	 * panic() is invoked from C Runtime.
198	 * It prints the CPU state via the crash console making
199	 * use of 'cpu_context' structure where general purpose
200	 * registers are saved and the crash buf.
201	 * This function will not return.
202	 *
203 	 * x0: Exception level
204	 * -----------------------------------------------------
205	 */
206func elx_panic
207	msr	spsel, #MODE_SP_ELX
208	mov	x8, x0
209
210	/* Print the crash message */
211	adr	x4, excpt_msg_el
212	bl	asm_print_str
213
214	/* Print exception level */
215	add	x0, x8, #'0'
216	bl	plat_crash_console_putc
217	bl	asm_print_newline
218
219	/* Report x0 - x29 values stored in 'gpregs_ctx' structure */
220	/* Store the ascii list pointer in x6 */
221	adr	x6, gp_regs
222	add	x7, sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X0
223
224print_next:
225	ldrb	w4, [x6]
226	/* Test whether we are at end of list */
227	cbz	w4, print_x30
228	mov	x4, x6
229	/* asm_print_str updates x4 to point to next entry in list */
230	bl	asm_print_str
231	/* x0 = number of symbols printed + 1 */
232	sub	x0, x4, x6
233	/* Update x6 with the updated list pointer */
234	mov	x6, x4
235	bl	print_alignment
236	ldr	x4, [x7], #REGSZ
237	bl	asm_print_hex
238	bl	asm_print_newline
239	b	print_next
240
241print_x30:
242	adr	x4, x30_msg
243	bl	asm_print_str
244
245	/* Print spaces to align "x30" string */
246	mov	x0, #4
247	bl	print_alignment
248
249	/* Report x30 */
250	ldr	x4, [x7]
251
252	/* ----------------------------------------------------------------
253	 * Different virtual address space size can be defined for each EL.
254	 * Ensure that we use the proper one by reading the corresponding
255	 * TCR_ELx register.
256	 * ----------------------------------------------------------------
257	 */
258	cmp	x8, #MODE_EL2
259	b.lt	from_el1	/* EL1 */
260	mrs	x2, sctlr_el2
261	mrs	x1, tcr_el2
262
263	/* ----------------------------------------------------------------
264	 * Check if pointer authentication is enabled at the specified EL.
265	 * If it isn't, we can then skip stripping a PAC code.
266	 * ----------------------------------------------------------------
267	 */
268test_pauth:
269	tst	x2, #(SCTLR_EnIA_BIT | SCTLR_EnIB_BIT)
270	b.eq	no_pauth
271
272	/* Demangle address */
273	and	x1, x1, #0x3F	/* T0SZ = TCR_ELx[5:0] */
274	sub	x1, x1, #64
275	neg	x1, x1		/* bottom_pac_bit = 64 - T0SZ */
276	mov	x2, #-1
277	lsl	x2, x2, x1
278	bic	x4, x4, x2
279
280no_pauth:
281	bl	asm_print_hex
282	bl	asm_print_newline
283
284	/* tpidr_el3 contains the address to cpu_data structure */
285	mrs	x0, tpidr_el3
286	/* Calculate the Crash buffer offset in cpu_data */
287	add	x0, x0, #CPU_DATA_CRASH_BUF_OFFSET
288	/* Store crash buffer address in tpidr_el3 */
289	msr	tpidr_el3, x0
290
291	/* Print the rest of crash dump */
292	b	print_el3_sys_regs
293
294from_el1:
295	mrs	x2, sctlr_el1
296	mrs	x1, tcr_el1
297	b	test_pauth
298endfunc	elx_panic
299
300	/* -----------------------------------------------------
301	 * This function allows to report a crash (if crash
302	 * reporting is enabled) when panic() is invoked from
303	 * C Runtime. It prints the CPU state via the crash
304	 * console making use of the crash buf. This function
305	 * will not return.
306	 * -----------------------------------------------------
307	 */
308func el3_panic
309	msr	spsel, #MODE_SP_ELX
310	prepare_crash_buf_save_x0_x1
311	adr	x0, panic_msg
312	mov	sp, x0
313	/* Fall through to 'do_crash_reporting' */
314
315	/* ------------------------------------------------------------
316	 * The common crash reporting functionality. It requires x0
317	 * and x1 has already been stored in crash buf, sp points to
318	 * crash message and tpidr_el3 contains the crash buf address.
319	 * The function does the following:
320	 *   - Retrieve the crash buffer from tpidr_el3
321	 *   - Store x2 to x6 in the crash buffer
322	 *   - Initialise the crash console.
323	 *   - Print the crash message by using the address in sp.
324	 *   - Print x30 value to the crash console.
325	 *   - Print x0 - x7 from the crash buf to the crash console.
326	 *   - Print x8 - x29 (in groups of 8 registers) using the
327	 *     crash buf to the crash console.
328	 *   - Print el3 sys regs (in groups of 8 registers) using the
329	 *     crash buf to the crash console.
330	 *   - Print non el3 sys regs (in groups of 8 registers) using
331	 *     the crash buf to the crash console.
332	 * ------------------------------------------------------------
333	 */
334do_crash_reporting:
335	/* Retrieve the crash buf from tpidr_el3 */
336	mrs	x0, tpidr_el3
337	/* Store x2 - x6, x30 in the crash buffer */
338	stp	x2, x3, [x0, #REGSZ * 2]
339	stp	x4, x5, [x0, #REGSZ * 4]
340	stp	x6, x30, [x0, #REGSZ * 6]
341	/* Initialize the crash console */
342	bl	plat_crash_console_init
343	/* Verify the console is initialized */
344	cbz	x0, crash_panic
345	/* Print the crash message. sp points to the crash message */
346	mov	x4, sp
347	bl	asm_print_str
348	/* Print spaces to align "x30" string */
349	mov	x0, #4
350	bl	print_alignment
351	/* Load the crash buf address */
352	mrs	x0, tpidr_el3
353	/* Report x30 first from the crash buf */
354	ldr	x4, [x0, #REGSZ * 7]
355
356#if ENABLE_PAUTH
357	/* Demangle address */
358	xpaci	x4
359#endif
360	bl	asm_print_hex
361	bl	asm_print_newline
362	/* Load the crash buf address */
363	mrs	x0, tpidr_el3
364	/* Now mov x7 into crash buf */
365	str	x7, [x0, #REGSZ * 7]
366
367	/* Report x0 - x29 values stored in crash buf */
368	/* Store the ascii list pointer in x6 */
369	adr	x6, gp_regs
370	/* Print x0 to x7 from the crash buf */
371	bl	size_controlled_print
372	/* Store x8 - x15 in crash buf and print */
373	bl	str_in_crash_buf_print
374	/* Load the crash buf address */
375	mrs	x0, tpidr_el3
376	/* Store the rest of gp regs and print */
377	stp	x16, x17, [x0]
378	stp	x18, x19, [x0, #REGSZ * 2]
379	stp	x20, x21, [x0, #REGSZ * 4]
380	stp	x22, x23, [x0, #REGSZ * 6]
381	bl	size_controlled_print
382	/* Load the crash buf address */
383	mrs	x0, tpidr_el3
384	stp	x24, x25, [x0]
385	stp	x26, x27, [x0, #REGSZ * 2]
386	stp	x28, x29, [x0, #REGSZ * 4]
387	bl	size_controlled_print
388
389	/* Print the el3 sys registers */
390print_el3_sys_regs:
391	adr	x6, el3_sys_regs
392	mrs	x8, scr_el3
393	mrs	x9, sctlr_el3
394	mrs	x10, cptr_el3
395	mrs	x11, tcr_el3
396	mrs	x12, daif
397	mrs	x13, mair_el3
398	mrs	x14, spsr_el3
399	mrs	x15, elr_el3
400	bl	str_in_crash_buf_print
401	mrs	x8, ttbr0_el3
402	mrs	x9, esr_el3
403	mrs	x10, far_el3
404	bl	str_in_crash_buf_print
405
406	/* Print the non el3 sys registers */
407	adr	x6, non_el3_sys_regs
408	mrs	x8, spsr_el1
409	mrs	x9, elr_el1
410	mrs	x10, spsr_abt
411	mrs	x11, spsr_und
412	mrs	x12, spsr_irq
413	mrs	x13, spsr_fiq
414	mrs	x14, sctlr_el1
415	mrs	x15, actlr_el1
416	bl	str_in_crash_buf_print
417	mrs	x8, cpacr_el1
418	mrs	x9, csselr_el1
419	mrs	x10, sp_el1
420	mrs	x11, esr_el1
421	mrs	x12, ttbr0_el1
422	mrs	x13, ttbr1_el1
423	mrs	x14, mair_el1
424	mrs	x15, amair_el1
425	bl	str_in_crash_buf_print
426	mrs	x8, tcr_el1
427	mrs	x9, tpidr_el1
428	mrs	x10, tpidr_el0
429	mrs	x11, tpidrro_el0
430	mrs	x12, par_el1
431	mrs	x13, mpidr_el1
432	mrs	x14, afsr0_el1
433	mrs	x15, afsr1_el1
434	bl	str_in_crash_buf_print
435	mrs	x8, contextidr_el1
436	mrs	x9, vbar_el1
437	mrs	x10, cntp_ctl_el0
438	mrs	x11, cntp_cval_el0
439	mrs	x12, cntv_ctl_el0
440	mrs	x13, cntv_cval_el0
441	mrs	x14, cntkctl_el1
442	mrs	x15, sp_el0
443	bl	str_in_crash_buf_print
444	mrs	x8, isr_el1
445	bl	str_in_crash_buf_print
446
447#if CTX_INCLUDE_AARCH32_REGS
448	/* Print the AArch32 registers */
449	adr	x6, aarch32_regs
450	mrs	x8, dacr32_el2
451	mrs	x9, ifsr32_el2
452	bl	str_in_crash_buf_print
453#endif /* CTX_INCLUDE_AARCH32_REGS */
454
455	/* Get the cpu specific registers to report */
456	bl	do_cpu_reg_dump
457	bl	str_in_crash_buf_print
458
459	/* Print some platform registers */
460	plat_crash_print_regs
461
462	bl	plat_crash_console_flush
463
464	/* Done reporting */
465	no_ret	plat_panic_handler
466endfunc el3_panic
467
468#else	/* CRASH_REPORTING */
469func report_unhandled_exception
470report_unhandled_interrupt:
471	no_ret	plat_panic_handler
472endfunc report_unhandled_exception
473#endif	/* CRASH_REPORTING */
474
475func crash_panic
476	no_ret	plat_panic_handler
477endfunc crash_panic
478