1/*
2 * Copyright (c) 2014-2023, 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	report_el3_panic
19	.globl	report_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 lower EL.\n"
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	 * lower_el_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	 */
204func report_elx_panic
205	msr	spsel, #MODE_SP_ELX
206
207	/* Print the crash message */
208	adr	x4, excpt_msg_el
209	bl	asm_print_str
210
211	/* Report x0 - x29 values stored in 'gpregs_ctx' structure */
212	/* Store the ascii list pointer in x6 */
213	adr	x6, gp_regs
214	add	x7, sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X0
215
216print_next:
217	ldrb	w4, [x6]
218	/* Test whether we are at end of list */
219	cbz	w4, print_x30
220	mov	x4, x6
221	/* asm_print_str updates x4 to point to next entry in list */
222	bl	asm_print_str
223	/* x0 = number of symbols printed + 1 */
224	sub	x0, x4, x6
225	/* Update x6 with the updated list pointer */
226	mov	x6, x4
227	bl	print_alignment
228	ldr	x4, [x7], #REGSZ
229	bl	asm_print_hex
230	bl	asm_print_newline
231	b	print_next
232
233print_x30:
234	adr	x4, x30_msg
235	bl	asm_print_str
236
237	/* Print spaces to align "x30" string */
238	mov	x0, #4
239	bl	print_alignment
240
241	/* Report x30 */
242	ldr	x4, [x7]
243
244	/* ----------------------------------------------------------------
245	 * Different virtual address space size can be defined for each EL.
246	 * Ensure that we use the proper one by reading the corresponding
247	 * TCR_ELx register.
248	 * ----------------------------------------------------------------
249	 */
250	cmp	x8, #MODE_EL2
251	b.lt	from_el1	/* EL1 */
252	mrs	x2, sctlr_el2
253	mrs	x1, tcr_el2
254
255	/* ----------------------------------------------------------------
256	 * Check if pointer authentication is enabled at the specified EL.
257	 * If it isn't, we can then skip stripping a PAC code.
258	 * ----------------------------------------------------------------
259	 */
260test_pauth:
261	tst	x2, #(SCTLR_EnIA_BIT | SCTLR_EnIB_BIT)
262	b.eq	no_pauth
263
264	/* Demangle address */
265	and	x1, x1, #0x3F	/* T0SZ = TCR_ELx[5:0] */
266	sub	x1, x1, #64
267	neg	x1, x1		/* bottom_pac_bit = 64 - T0SZ */
268	mov	x2, #-1
269	lsl	x2, x2, x1
270	bic	x4, x4, x2
271
272no_pauth:
273	bl	asm_print_hex
274	bl	asm_print_newline
275
276	/* tpidr_el3 contains the address to cpu_data structure */
277	mrs	x0, tpidr_el3
278	/* Calculate the Crash buffer offset in cpu_data */
279	add	x0, x0, #CPU_DATA_CRASH_BUF_OFFSET
280	/* Store crash buffer address in tpidr_el3 */
281	msr	tpidr_el3, x0
282
283	/* Print the rest of crash dump */
284	b	print_el3_sys_regs
285
286from_el1:
287	mrs	x2, sctlr_el1
288	mrs	x1, tcr_el1
289	b	test_pauth
290endfunc	report_elx_panic
291
292	/* -----------------------------------------------------
293	 * This function allows to report a crash (if crash
294	 * reporting is enabled) when panic() is invoked from
295	 * C Runtime. It prints the CPU state via the crash
296	 * console making use of the crash buf. This function
297	 * will not return.
298	 * -----------------------------------------------------
299	 */
300func report_el3_panic
301	msr	spsel, #MODE_SP_ELX
302	prepare_crash_buf_save_x0_x1
303	adr	x0, panic_msg
304	mov	sp, x0
305	/* Fall through to 'do_crash_reporting' */
306
307	/* ------------------------------------------------------------
308	 * The common crash reporting functionality. It requires x0
309	 * and x1 has already been stored in crash buf, sp points to
310	 * crash message and tpidr_el3 contains the crash buf address.
311	 * The function does the following:
312	 *   - Retrieve the crash buffer from tpidr_el3
313	 *   - Store x2 to x6 in the crash buffer
314	 *   - Initialise the crash console.
315	 *   - Print the crash message by using the address in sp.
316	 *   - Print x30 value to the crash console.
317	 *   - Print x0 - x7 from the crash buf to the crash console.
318	 *   - Print x8 - x29 (in groups of 8 registers) using the
319	 *     crash buf to the crash console.
320	 *   - Print el3 sys regs (in groups of 8 registers) using the
321	 *     crash buf to the crash console.
322	 *   - Print non el3 sys regs (in groups of 8 registers) using
323	 *     the crash buf to the crash console.
324	 * ------------------------------------------------------------
325	 */
326do_crash_reporting:
327	/* Retrieve the crash buf from tpidr_el3 */
328	mrs	x0, tpidr_el3
329	/* Store x2 - x6, x30 in the crash buffer */
330	stp	x2, x3, [x0, #REGSZ * 2]
331	stp	x4, x5, [x0, #REGSZ * 4]
332	stp	x6, x30, [x0, #REGSZ * 6]
333	/* Initialize the crash console */
334	bl	plat_crash_console_init
335	/* Verify the console is initialized */
336	cbz	x0, crash_panic
337	/* Print the crash message. sp points to the crash message */
338	mov	x4, sp
339	bl	asm_print_str
340	/* Print spaces to align "x30" string */
341	mov	x0, #4
342	bl	print_alignment
343	/* Load the crash buf address */
344	mrs	x0, tpidr_el3
345	/* Report x30 first from the crash buf */
346	ldr	x4, [x0, #REGSZ * 7]
347
348#if ENABLE_PAUTH
349	/* Demangle address */
350	xpaci	x4
351#endif
352	bl	asm_print_hex
353	bl	asm_print_newline
354	/* Load the crash buf address */
355	mrs	x0, tpidr_el3
356	/* Now mov x7 into crash buf */
357	str	x7, [x0, #REGSZ * 7]
358
359	/* Report x0 - x29 values stored in crash buf */
360	/* Store the ascii list pointer in x6 */
361	adr	x6, gp_regs
362	/* Print x0 to x7 from the crash buf */
363	bl	size_controlled_print
364	/* Store x8 - x15 in crash buf and print */
365	bl	str_in_crash_buf_print
366	/* Load the crash buf address */
367	mrs	x0, tpidr_el3
368	/* Store the rest of gp regs and print */
369	stp	x16, x17, [x0]
370	stp	x18, x19, [x0, #REGSZ * 2]
371	stp	x20, x21, [x0, #REGSZ * 4]
372	stp	x22, x23, [x0, #REGSZ * 6]
373	bl	size_controlled_print
374	/* Load the crash buf address */
375	mrs	x0, tpidr_el3
376	stp	x24, x25, [x0]
377	stp	x26, x27, [x0, #REGSZ * 2]
378	stp	x28, x29, [x0, #REGSZ * 4]
379	bl	size_controlled_print
380
381	/* Print the el3 sys registers */
382print_el3_sys_regs:
383	adr	x6, el3_sys_regs
384	mrs	x8, scr_el3
385	mrs	x9, sctlr_el3
386	mrs	x10, cptr_el3
387	mrs	x11, tcr_el3
388	mrs	x12, daif
389	mrs	x13, mair_el3
390	mrs	x14, spsr_el3
391	mrs	x15, elr_el3
392	bl	str_in_crash_buf_print
393	mrs	x8, ttbr0_el3
394	mrs	x9, esr_el3
395	mrs	x10, far_el3
396	bl	str_in_crash_buf_print
397
398	/* Print the non el3 sys registers */
399	adr	x6, non_el3_sys_regs
400	mrs	x8, spsr_el1
401	mrs	x9, elr_el1
402	mrs	x10, spsr_abt
403	mrs	x11, spsr_und
404	mrs	x12, spsr_irq
405	mrs	x13, spsr_fiq
406	mrs	x14, sctlr_el1
407	mrs	x15, actlr_el1
408	bl	str_in_crash_buf_print
409	mrs	x8, cpacr_el1
410	mrs	x9, csselr_el1
411	mrs	x10, sp_el1
412	mrs	x11, esr_el1
413	mrs	x12, ttbr0_el1
414	mrs	x13, ttbr1_el1
415	mrs	x14, mair_el1
416	mrs	x15, amair_el1
417	bl	str_in_crash_buf_print
418	mrs	x8, tcr_el1
419	mrs	x9, tpidr_el1
420	mrs	x10, tpidr_el0
421	mrs	x11, tpidrro_el0
422	mrs	x12, par_el1
423	mrs	x13, mpidr_el1
424	mrs	x14, afsr0_el1
425	mrs	x15, afsr1_el1
426	bl	str_in_crash_buf_print
427	mrs	x8, contextidr_el1
428	mrs	x9, vbar_el1
429	mrs	x10, cntp_ctl_el0
430	mrs	x11, cntp_cval_el0
431	mrs	x12, cntv_ctl_el0
432	mrs	x13, cntv_cval_el0
433	mrs	x14, cntkctl_el1
434	mrs	x15, sp_el0
435	bl	str_in_crash_buf_print
436	mrs	x8, isr_el1
437	bl	str_in_crash_buf_print
438
439#if CTX_INCLUDE_AARCH32_REGS
440	/* Print the AArch32 registers */
441	adr	x6, aarch32_regs
442	mrs	x8, dacr32_el2
443	mrs	x9, ifsr32_el2
444	bl	str_in_crash_buf_print
445#endif /* CTX_INCLUDE_AARCH32_REGS */
446
447	/* Get the cpu specific registers to report */
448	bl	do_cpu_reg_dump
449	bl	str_in_crash_buf_print
450
451	/* Print some platform registers */
452	plat_crash_print_regs
453
454	bl	plat_crash_console_flush
455
456	/* Done reporting */
457	no_ret	plat_panic_handler
458endfunc report_el3_panic
459
460#else	/* CRASH_REPORTING */
461func report_unhandled_exception
462report_unhandled_interrupt:
463	no_ret	plat_panic_handler
464endfunc report_unhandled_exception
465#endif	/* CRASH_REPORTING */
466
467func crash_panic
468	no_ret	plat_panic_handler
469endfunc crash_panic
470