xref: /rk3399_ARM-atf/bl31/aarch64/crash_reporting.S (revision a43d431b80541ea436b71f967c5749babf978c7a)
1/*
2 * Copyright (c) 2014, ARM Limited and Contributors. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * Neither the name of ARM nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific
16 * prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30#include <arch.h>
31#include <asm_macros.S>
32#include <context.h>
33#include <plat_macros.S>
34
35	.globl	get_crash_stack
36	.globl	dump_state_and_die
37	.globl	dump_intr_state_and_die
38
39	/* ------------------------------------------------------
40	 * The below section deals with dumping the system state
41	 * when an unhandled exception is taken in EL3.
42	 * The layout and the names of the registers which will
43	 * be dumped during a unhandled exception is given below.
44	 * ------------------------------------------------------
45	 */
46.section .rodata.dump_reg_name, "aS"
47caller_saved_regs:	.asciz	"x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",\
48	 "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15", "x16",\
49	 "x17", "x18", ""
50
51callee_saved_regs: .asciz	"x19", "x20", "x21", "x22", "x23", "x24",\
52	 "x25", "x26", "x27", "x28", "x29", "x30", ""
53
54el3_sys_regs: .asciz	"scr_el3", "sctlr_el3", "cptr_el3", "tcr_el3",\
55	 "daif", "mair_el3", "spsr_el3", "elr_el3", "ttbr0_el3", "esr_el3",\
56	 "sp_el3", "far_el3", ""
57
58non_el3_sys_0_regs: .asciz "spsr_el1", "elr_el1", "spsr_abt", "spsr_und",\
59	"spsr_irq", "spsr_fiq", "sctlr_el1", "actlr_el1", "cpacr_el1",\
60	"csselr_el1", "sp_el1", "esr_el1", "ttbr0_el1", "ttbr1_el1",\
61	"mair_el1", "amair_el1", "tcr_el1", "tpidr_el1", ""
62
63non_el3_sys_1_regs: .asciz "tpidr_el0", "tpidrro_el0", "dacr32_el2",\
64	"ifsr32_el2", "par_el1", "far_el1", "afsr0_el1", "afsr1_el1",\
65	"contextidr_el1", "vbar_el1", "cntp_ctl_el0", "cntp_cval_el0",\
66	"cntv_ctl_el0", "cntv_cval_el0", "cntkctl_el1", "fpexc32_el2",\
67	"sp_el0", ""
68
69	/* -----------------------------------------------------
70	 * Currently we are stack limited. Hence make sure that
71	 * we dont try to dump more than 20 registers using the
72	 * stack.
73	 * -----------------------------------------------------
74	 */
75
76#define REG_SIZE 0x8
77
78/* The caller saved registers are X0 to X18 */
79#define CALLER_SAVED_REG_SIZE 		(20 * REG_SIZE)
80/* The caller saved registers are X19 to X30 */
81#define CALLEE_SAVED_REG_SIZE 		(12 * REG_SIZE)
82/* The EL3 sys regs*/
83#define EL3_SYS_REG_SIZE 			(12 * REG_SIZE)
84/* The non EL3 sys regs set-0 */
85#define NON_EL3_SYS_0_REG_SIZE 		(18 * REG_SIZE)
86/* The non EL3 sys regs set-1 */
87#define NON_EL3_SYS_1_REG_SIZE 		(18 * REG_SIZE)
88
89	.macro print_caller_saved_regs
90	sub	sp, sp, #CALLER_SAVED_REG_SIZE
91	stp	x0, x1, [sp]
92	stp	x2, x3, [sp, #(REG_SIZE * 2)]
93	stp	x4, x5, [sp, #(REG_SIZE * 4)]
94	stp	x6, x7, [sp, #(REG_SIZE * 6)]
95	stp	x8, x9, [sp, #(REG_SIZE * 8)]
96	stp	x10, x11, [sp, #(REG_SIZE * 10)]
97	stp	x12, x13, [sp, #(REG_SIZE * 12)]
98	stp	x14, x15, [sp, #(REG_SIZE * 14)]
99	stp	x16, x17, [sp, #(REG_SIZE * 16)]
100	stp	x18, xzr, [sp, #(REG_SIZE * 18)]
101	adr	x0, caller_saved_regs
102	mov	x1, sp
103	bl	print_string_value
104	add	sp, sp, #CALLER_SAVED_REG_SIZE
105	.endm
106
107	.macro print_callee_saved_regs
108	sub	sp, sp, CALLEE_SAVED_REG_SIZE
109	stp	x19, x20, [sp]
110	stp	x21, x22, [sp, #(REG_SIZE * 2)]
111	stp	x23, x24, [sp, #(REG_SIZE * 4)]
112	stp	x25, x26, [sp, #(REG_SIZE * 6)]
113	stp	x27, x28, [sp, #(REG_SIZE * 8)]
114	stp	x29, x30, [sp, #(REG_SIZE * 10)]
115	adr	x0, callee_saved_regs
116	mov	x1, sp
117	bl	print_string_value
118	add	sp, sp, #CALLEE_SAVED_REG_SIZE
119	.endm
120
121	.macro print_el3_sys_regs
122	sub	sp, sp, #EL3_SYS_REG_SIZE
123	mrs	x9, scr_el3
124	mrs	x10, sctlr_el3
125	mrs	x11, cptr_el3
126	mrs	x12, tcr_el3
127	mrs	x13, daif
128	mrs	x14, mair_el3
129	mrs	x15, spsr_el3 /*save the elr and spsr regs seperately*/
130	mrs	x16, elr_el3
131	mrs	x17, ttbr0_el3
132	mrs	x8, esr_el3
133	mrs	x7, far_el3
134
135	stp	x9, x10, [sp]
136	stp	x11, x12, [sp, #(REG_SIZE * 2)]
137	stp	x13, x14, [sp, #(REG_SIZE * 4)]
138	stp	x15, x16, [sp, #(REG_SIZE * 6)]
139	stp	x17, x8, [sp, #(REG_SIZE * 8)]
140	stp	x0, x7, [sp, #(REG_SIZE * 10)] /* sp_el3 is in x0 */
141
142	adr	x0, el3_sys_regs
143	mov	x1, sp
144	bl	print_string_value
145	add	sp, sp, #EL3_SYS_REG_SIZE
146	.endm
147
148	.macro print_non_el3_sys_0_regs
149	sub	sp, sp, #NON_EL3_SYS_0_REG_SIZE
150	mrs	x9, spsr_el1
151	mrs	x10, elr_el1
152	mrs	x11, spsr_abt
153	mrs	x12, spsr_und
154	mrs	x13, spsr_irq
155	mrs	x14, spsr_fiq
156	mrs	x15, sctlr_el1
157	mrs	x16, actlr_el1
158	mrs	x17, cpacr_el1
159	mrs	x8, csselr_el1
160
161	stp	x9, x10, [sp]
162	stp	x11, x12, [sp, #(REG_SIZE * 2)]
163	stp	x13, x14, [sp, #(REG_SIZE * 4)]
164	stp	x15, x16, [sp, #(REG_SIZE * 6)]
165	stp	x17, x8, [sp, #(REG_SIZE * 8)]
166
167	mrs	x10, sp_el1
168	mrs	x11, esr_el1
169	mrs	x12, ttbr0_el1
170	mrs	x13, ttbr1_el1
171	mrs	x14, mair_el1
172	mrs	x15, amair_el1
173	mrs	x16, tcr_el1
174	mrs	x17, tpidr_el1
175
176	stp	x10, x11, [sp, #(REG_SIZE * 10)]
177	stp	x12, x13, [sp, #(REG_SIZE * 12)]
178	stp	x14, x15, [sp, #(REG_SIZE * 14)]
179	stp	x16, x17, [sp, #(REG_SIZE * 16)]
180
181	adr	x0, non_el3_sys_0_regs
182	mov	x1, sp
183	bl	print_string_value
184	add	sp, sp, #NON_EL3_SYS_0_REG_SIZE
185	.endm
186
187	.macro print_non_el3_sys_1_regs
188	sub	sp, sp, #NON_EL3_SYS_1_REG_SIZE
189
190	mrs	x9, tpidr_el0
191	mrs	x10, tpidrro_el0
192	mrs	x11, dacr32_el2
193	mrs	x12, ifsr32_el2
194	mrs	x13, par_el1
195	mrs	x14, far_el1
196	mrs	x15, afsr0_el1
197	mrs	x16, afsr1_el1
198	mrs	x17, contextidr_el1
199	mrs	x8, vbar_el1
200
201	stp	x9, x10, [sp]
202	stp	x11, x12, [sp, #(REG_SIZE * 2)]
203	stp	x13, x14, [sp, #(REG_SIZE * 4)]
204	stp	x15, x16, [sp, #(REG_SIZE * 6)]
205	stp	x17, x8, [sp, #(REG_SIZE * 8)]
206
207	mrs	x10, cntp_ctl_el0
208	mrs	x11, cntp_cval_el0
209	mrs	x12, cntv_ctl_el0
210	mrs	x13, cntv_cval_el0
211	mrs	x14, cntkctl_el1
212	mrs	x15, fpexc32_el2
213	mrs	x8, sp_el0
214
215	stp	x10, x11, [sp, #(REG_SIZE *10)]
216	stp	x12, x13, [sp, #(REG_SIZE * 12)]
217	stp	x14, x15, [sp, #(REG_SIZE * 14)]
218	stp	x8, xzr, [sp, #(REG_SIZE * 16)]
219
220	adr	x0, non_el3_sys_1_regs
221	mov	x1, sp
222	bl	print_string_value
223	add	sp, sp, #NON_EL3_SYS_1_REG_SIZE
224	.endm
225
226	.macro init_crash_stack
227	msr	cntfrq_el0, x0 /* we can corrupt this reg to free up x0 */
228	mrs	x0, tpidr_el3
229
230	/* Check if tpidr is initialized */
231	cbz	x0, infinite_loop
232
233	ldr	x0, [x0, #PTR_CACHE_CRASH_STACK_OFFSET]
234	/* store the x30 and sp to stack */
235	str	x30, [x0, #-(REG_SIZE)]!
236	mov	x30, sp
237	str	x30, [x0, #-(REG_SIZE)]!
238	mov	sp, x0
239	mrs	x0, cntfrq_el0
240	.endm
241
242	/* ---------------------------------------------------
243	 * The below function initializes the crash dump stack ,
244	 * and prints the system state. This function
245	 * will not return.
246	 * ---------------------------------------------------
247	 */
248func dump_state_and_die
249	init_crash_stack
250	print_caller_saved_regs
251	b	print_state
252
253func dump_intr_state_and_die
254	init_crash_stack
255	print_caller_saved_regs
256	plat_print_gic_regs /* fall through to print_state */
257
258print_state:
259	/* copy the original x30 from stack */
260	ldr	x30, [sp, #REG_SIZE]
261	print_callee_saved_regs
262	/* copy the original SP_EL3 from stack to x0 and rewind stack */
263	ldr x0, [sp], #(REG_SIZE * 2)
264	print_el3_sys_regs
265	print_non_el3_sys_0_regs
266	print_non_el3_sys_1_regs
267	b	infinite_loop
268
269func infinite_loop
270	b	infinite_loop
271
272
273#define PCPU_CRASH_STACK_SIZE	0x140
274
275	/* -----------------------------------------------------
276	 * void get_crash_stack (uint64_t mpidr) : This
277	 * function is used to allocate a small stack for
278	 * reporting unhandled exceptions
279	 * -----------------------------------------------------
280	 */
281func get_crash_stack
282	mov	x10, x30 // lr
283	get_mp_stack pcpu_crash_stack, PCPU_CRASH_STACK_SIZE
284	ret	x10
285
286	/* -----------------------------------------------------
287	 * Per-cpu crash stacks in normal memory.
288	 * -----------------------------------------------------
289	 */
290declare_stack pcpu_crash_stack, tzfw_normal_stacks, \
291		PCPU_CRASH_STACK_SIZE, PLATFORM_CORE_COUNT
292