xref: /rk3399_ARM-atf/bl31/aarch64/ea_delegate.S (revision 6f7de9a87148a6024af4a1ced7f26e5bff55683b)
1df8f3188SJeenu Viswambharan/*
297215e0fSDaniel Boulby * Copyright (c) 2018-2022, ARM Limited and Contributors. All rights reserved.
3df56e9d1SVarun Wadekar * Copyright (c) 2022, NVIDIA Corporation. All rights reserved.
4df8f3188SJeenu Viswambharan *
5df8f3188SJeenu Viswambharan * SPDX-License-Identifier: BSD-3-Clause
6df8f3188SJeenu Viswambharan */
7df8f3188SJeenu Viswambharan
8df8f3188SJeenu Viswambharan
9d5a23af5SJeenu Viswambharan#include <assert_macros.S>
10df8f3188SJeenu Viswambharan#include <asm_macros.S>
11ee6ff1bbSJeenu Viswambharan#include <assert_macros.S>
1209d40e0eSAntonio Nino Diaz#include <bl31/ea_handle.h>
13df8f3188SJeenu Viswambharan#include <context.h>
1409d40e0eSAntonio Nino Diaz#include <lib/extensions/ras_arch.h>
1580942622Slaurenw-arm#include <cpu_macros.S>
1680942622Slaurenw-arm#include <context.h>
17df8f3188SJeenu Viswambharan
18df8f3188SJeenu Viswambharan	.globl	handle_lower_el_ea_esb
19*6f7de9a8SManish Pandey	.globl	handle_lower_el_sync_ea
20c2d32a5fSMadhukar Pappireddy	.globl	handle_lower_el_async_ea
21df8f3188SJeenu Viswambharan
22df8f3188SJeenu Viswambharan
23df8f3188SJeenu Viswambharan/*
24df8f3188SJeenu Viswambharan * Function to delegate External Aborts synchronized by ESB instruction at EL3
25df8f3188SJeenu Viswambharan * vector entry. This function assumes GP registers x0-x29 have been saved, and
26df8f3188SJeenu Viswambharan * are available for use. It delegates the handling of the EA to platform
27df8f3188SJeenu Viswambharan * handler, and returns only upon successfully handling the EA; otherwise
28df8f3188SJeenu Viswambharan * panics. On return from this function, the original exception handler is
29df8f3188SJeenu Viswambharan * expected to resume.
30df8f3188SJeenu Viswambharan */
31df8f3188SJeenu Viswambharanfunc handle_lower_el_ea_esb
32df8f3188SJeenu Viswambharan	mov	x0, #ERROR_EA_ESB
33df8f3188SJeenu Viswambharan	mrs	x1, DISR_EL1
34df8f3188SJeenu Viswambharan	b	ea_proceed
35df8f3188SJeenu Viswambharanendfunc handle_lower_el_ea_esb
36df8f3188SJeenu Viswambharan
37df8f3188SJeenu Viswambharan
38df8f3188SJeenu Viswambharan/*
39df8f3188SJeenu Viswambharan * This function forms the tail end of Synchronous Exception entry from lower
4080942622Slaurenw-arm * EL, and expects to handle Synchronous External Aborts from lower EL and CPU
4180942622Slaurenw-arm * Implementation Defined Exceptions. If any other kind of exception is detected,
4280942622Slaurenw-arm * then this function reports unhandled exception.
43df8f3188SJeenu Viswambharan *
44*6f7de9a8SManish Pandey * It delegates the handling of the EA to platform handler, and upon successfully
45*6f7de9a8SManish Pandey * handling the EA, exits EL3; otherwise panics.
46*6f7de9a8SManish Pandey *
47*6f7de9a8SManish Pandey * This function assumes x30 has been saved.
48df8f3188SJeenu Viswambharan */
49*6f7de9a8SManish Pandeyfunc handle_lower_el_sync_ea
50df8f3188SJeenu Viswambharan	mrs	x30, esr_el3
51df8f3188SJeenu Viswambharan	ubfx	x30, x30, #ESR_EC_SHIFT, #ESR_EC_LENGTH
52df8f3188SJeenu Viswambharan
53df8f3188SJeenu Viswambharan	/* Check for I/D aborts from lower EL */
54df8f3188SJeenu Viswambharan	cmp	x30, #EC_IABORT_LOWER_EL
55df8f3188SJeenu Viswambharan	b.eq	1f
56df8f3188SJeenu Viswambharan
57df8f3188SJeenu Viswambharan	cmp	x30, #EC_DABORT_LOWER_EL
5880942622Slaurenw-arm	b.eq	1f
5980942622Slaurenw-arm
6080942622Slaurenw-arm	/* Save GP registers */
6180942622Slaurenw-arm	stp	x0, x1, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X0]
6280942622Slaurenw-arm	stp	x2, x3, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X2]
6380942622Slaurenw-arm	stp	x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4]
6480942622Slaurenw-arm
6580942622Slaurenw-arm	/* Get the cpu_ops pointer */
6680942622Slaurenw-arm	bl	get_cpu_ops_ptr
6780942622Slaurenw-arm
6880942622Slaurenw-arm	/* Get the cpu_ops exception handler */
6980942622Slaurenw-arm	ldr	x0, [x0, #CPU_E_HANDLER_FUNC]
7080942622Slaurenw-arm
7180942622Slaurenw-arm	/*
7280942622Slaurenw-arm	 * If the reserved function pointer is NULL, this CPU does not have an
7380942622Slaurenw-arm	 * implementation defined exception handler function
7480942622Slaurenw-arm	 */
7580942622Slaurenw-arm	cbz	x0, 2f
7680942622Slaurenw-arm	mrs	x1, esr_el3
7780942622Slaurenw-arm	ubfx	x1, x1, #ESR_EC_SHIFT, #ESR_EC_LENGTH
7880942622Slaurenw-arm	blr	x0
7980942622Slaurenw-arm	b	2f
80df8f3188SJeenu Viswambharan
81df8f3188SJeenu Viswambharan1:
82e290a8fcSAlexei Fedorov	/*
83ed108b56SAlexei Fedorov	 * Save general purpose and ARMv8.3-PAuth registers (if enabled).
84ed108b56SAlexei Fedorov	 * If Secure Cycle Counter is not disabled in MDCR_EL3 when
85ed108b56SAlexei Fedorov	 * ARMv8.5-PMU is implemented, save PMCR_EL0 and disable Cycle Counter.
867d33ffe4SDaniel Boulby	 * Also set the PSTATE to a known state.
87e290a8fcSAlexei Fedorov	 */
8897215e0fSDaniel Boulby	bl	prepare_el3_entry
89e290a8fcSAlexei Fedorov
90b86048c4SAntonio Nino Diaz#if ENABLE_PAUTH
91ed108b56SAlexei Fedorov	/* Load and program APIAKey firmware key */
92ed108b56SAlexei Fedorov	bl	pauth_load_bl31_apiakey
93b86048c4SAntonio Nino Diaz#endif
945283962eSAntonio Nino Diaz
95df8f3188SJeenu Viswambharan	/* Setup exception class and syndrome arguments for platform handler */
96df8f3188SJeenu Viswambharan	mov	x0, #ERROR_EA_SYNC
97df8f3188SJeenu Viswambharan	mrs	x1, esr_el3
98bb9549baSJan Dabros	bl	delegate_sync_ea
99df8f3188SJeenu Viswambharan
100bb9549baSJan Dabros	/* el3_exit assumes SP_EL0 on entry */
101bb9549baSJan Dabros	msr	spsel, #MODE_SP_EL0
102bb9549baSJan Dabros	b	el3_exit
103df8f3188SJeenu Viswambharan2:
10480942622Slaurenw-arm	ldp	x0, x1, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X0]
10580942622Slaurenw-arm	ldp	x2, x3, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X2]
10680942622Slaurenw-arm	ldp	x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4]
10780942622Slaurenw-arm
108df8f3188SJeenu Viswambharan	/* Synchronous exceptions other than the above are assumed to be EA */
109df8f3188SJeenu Viswambharan	ldr	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
110df8f3188SJeenu Viswambharan	no_ret	report_unhandled_exception
111*6f7de9a8SManish Pandeyendfunc handle_lower_el_sync_ea
112df8f3188SJeenu Viswambharan
113df8f3188SJeenu Viswambharan
114df8f3188SJeenu Viswambharan/*
115df8f3188SJeenu Viswambharan * This function handles SErrors from lower ELs.
116df8f3188SJeenu Viswambharan *
117*6f7de9a8SManish Pandey * It delegates the handling of the EA to platform handler, and upon successfully
118*6f7de9a8SManish Pandey * handling the EA, exits EL3; otherwise panics.
119*6f7de9a8SManish Pandey *
120*6f7de9a8SManish Pandey * This function assumes x30 has been saved.
121df8f3188SJeenu Viswambharan */
122*6f7de9a8SManish Pandeyfunc handle_lower_el_async_ea
123df8f3188SJeenu Viswambharan
124e290a8fcSAlexei Fedorov	/*
125ed108b56SAlexei Fedorov	 * Save general purpose and ARMv8.3-PAuth registers (if enabled).
126ed108b56SAlexei Fedorov	 * If Secure Cycle Counter is not disabled in MDCR_EL3 when
127ed108b56SAlexei Fedorov	 * ARMv8.5-PMU is implemented, save PMCR_EL0 and disable Cycle Counter.
1287d33ffe4SDaniel Boulby	 * Also set the PSTATE to a known state.
129e290a8fcSAlexei Fedorov	 */
13097215e0fSDaniel Boulby	bl	prepare_el3_entry
131e290a8fcSAlexei Fedorov
132b86048c4SAntonio Nino Diaz#if ENABLE_PAUTH
133ed108b56SAlexei Fedorov	/* Load and program APIAKey firmware key */
134ed108b56SAlexei Fedorov	bl	pauth_load_bl31_apiakey
135b86048c4SAntonio Nino Diaz#endif
1365283962eSAntonio Nino Diaz
137df8f3188SJeenu Viswambharan	/* Setup exception class and syndrome arguments for platform handler */
138df8f3188SJeenu Viswambharan	mov	x0, #ERROR_EA_ASYNC
139df8f3188SJeenu Viswambharan	mrs	x1, esr_el3
140bb9549baSJan Dabros	bl	delegate_async_ea
141bb9549baSJan Dabros
142bb9549baSJan Dabros	/* el3_exit assumes SP_EL0 on entry */
143bb9549baSJan Dabros	msr	spsel, #MODE_SP_EL0
144bb9549baSJan Dabros	b	el3_exit
145*6f7de9a8SManish Pandeyendfunc handle_lower_el_async_ea
146df8f3188SJeenu Viswambharan
147df8f3188SJeenu Viswambharan
148df8f3188SJeenu Viswambharan/*
149b56dc2a9SJeenu Viswambharan * Prelude for Synchronous External Abort handling. This function assumes that
150b56dc2a9SJeenu Viswambharan * all GP registers have been saved by the caller.
151b56dc2a9SJeenu Viswambharan *
152b56dc2a9SJeenu Viswambharan * x0: EA reason
153b56dc2a9SJeenu Viswambharan * x1: EA syndrome
154b56dc2a9SJeenu Viswambharan */
155b56dc2a9SJeenu Viswambharanfunc delegate_sync_ea
156b56dc2a9SJeenu Viswambharan#if RAS_EXTENSION
157b56dc2a9SJeenu Viswambharan	/*
158b56dc2a9SJeenu Viswambharan	 * Check for Uncontainable error type. If so, route to the platform
159b56dc2a9SJeenu Viswambharan	 * fatal error handler rather than the generic EA one.
160b56dc2a9SJeenu Viswambharan	 */
161b56dc2a9SJeenu Viswambharan	ubfx    x2, x1, #EABORT_SET_SHIFT, #EABORT_SET_WIDTH
162b56dc2a9SJeenu Viswambharan	cmp     x2, #ERROR_STATUS_SET_UC
163b56dc2a9SJeenu Viswambharan	b.ne    1f
164b56dc2a9SJeenu Viswambharan
165b56dc2a9SJeenu Viswambharan	/* Check fault status code */
166b56dc2a9SJeenu Viswambharan	ubfx    x3, x1, #EABORT_DFSC_SHIFT, #EABORT_DFSC_WIDTH
167b56dc2a9SJeenu Viswambharan	cmp     x3, #SYNC_EA_FSC
168b56dc2a9SJeenu Viswambharan	b.ne    1f
169b56dc2a9SJeenu Viswambharan
170b56dc2a9SJeenu Viswambharan	no_ret  plat_handle_uncontainable_ea
171b56dc2a9SJeenu Viswambharan1:
172b56dc2a9SJeenu Viswambharan#endif
173b56dc2a9SJeenu Viswambharan
174b56dc2a9SJeenu Viswambharan	b       ea_proceed
175b56dc2a9SJeenu Viswambharanendfunc delegate_sync_ea
176b56dc2a9SJeenu Viswambharan
177b56dc2a9SJeenu Viswambharan
178b56dc2a9SJeenu Viswambharan/*
179b56dc2a9SJeenu Viswambharan * Prelude for Asynchronous External Abort handling. This function assumes that
180b56dc2a9SJeenu Viswambharan * all GP registers have been saved by the caller.
181b56dc2a9SJeenu Viswambharan *
182b56dc2a9SJeenu Viswambharan * x0: EA reason
183b56dc2a9SJeenu Viswambharan * x1: EA syndrome
184b56dc2a9SJeenu Viswambharan */
185b56dc2a9SJeenu Viswambharanfunc delegate_async_ea
186b56dc2a9SJeenu Viswambharan#if RAS_EXTENSION
187d435238dSManish Pandey	/* Check Exception Class to ensure SError, as this function should
188d435238dSManish Pandey	 * only be invoked for SError. If that is not the case, which implies
189d435238dSManish Pandey	 * either an HW error or programming error, panic.
190d435238dSManish Pandey	 */
191d435238dSManish Pandey	ubfx	x2, x1, #ESR_EC_SHIFT, #ESR_EC_LENGTH
192d435238dSManish Pandey	cmp	x2, EC_SERROR
193d435238dSManish Pandey	b.ne	do_panic
194b56dc2a9SJeenu Viswambharan	/*
195b56dc2a9SJeenu Viswambharan	 * Check for Implementation Defined Syndrome. If so, skip checking
196b56dc2a9SJeenu Viswambharan	 * Uncontainable error type from the syndrome as the format is unknown.
197b56dc2a9SJeenu Viswambharan	 */
198b56dc2a9SJeenu Viswambharan	tbnz	x1, #SERROR_IDS_BIT, 1f
199b56dc2a9SJeenu Viswambharan
200d435238dSManish Pandey	/* AET only valid when DFSC is 0x11 */
201d435238dSManish Pandey	ubfx	x2, x1, #EABORT_DFSC_SHIFT, #EABORT_DFSC_WIDTH
202d435238dSManish Pandey	cmp	x2, #DFSC_SERROR
203d435238dSManish Pandey	b.ne	1f
204d435238dSManish Pandey
205b56dc2a9SJeenu Viswambharan	/*
206b56dc2a9SJeenu Viswambharan	 * Check for Uncontainable error type. If so, route to the platform
207b56dc2a9SJeenu Viswambharan	 * fatal error handler rather than the generic EA one.
208b56dc2a9SJeenu Viswambharan	 */
209d435238dSManish Pandey	ubfx	x3, x1, #EABORT_AET_SHIFT, #EABORT_AET_WIDTH
210d435238dSManish Pandey	cmp	x3, #ERROR_STATUS_UET_UC
211b56dc2a9SJeenu Viswambharan	b.ne	1f
212b56dc2a9SJeenu Viswambharan
213b56dc2a9SJeenu Viswambharan	no_ret	plat_handle_uncontainable_ea
214b56dc2a9SJeenu Viswambharan1:
215b56dc2a9SJeenu Viswambharan#endif
216b56dc2a9SJeenu Viswambharan
217b56dc2a9SJeenu Viswambharan	b	ea_proceed
218b56dc2a9SJeenu Viswambharanendfunc delegate_async_ea
219b56dc2a9SJeenu Viswambharan
220b56dc2a9SJeenu Viswambharan
221b56dc2a9SJeenu Viswambharan/*
222df8f3188SJeenu Viswambharan * Delegate External Abort handling to platform's EA handler. This function
223df8f3188SJeenu Viswambharan * assumes that all GP registers have been saved by the caller.
224df8f3188SJeenu Viswambharan *
225df8f3188SJeenu Viswambharan * x0: EA reason
226df8f3188SJeenu Viswambharan * x1: EA syndrome
227df8f3188SJeenu Viswambharan */
228df8f3188SJeenu Viswambharanfunc ea_proceed
229d5a23af5SJeenu Viswambharan	/*
230d5a23af5SJeenu Viswambharan	 * If the ESR loaded earlier is not zero, we were processing an EA
231d5a23af5SJeenu Viswambharan	 * already, and this is a double fault.
232d5a23af5SJeenu Viswambharan	 */
233d5a23af5SJeenu Viswambharan	ldr	x5, [sp, #CTX_EL3STATE_OFFSET + CTX_ESR_EL3]
234d5a23af5SJeenu Viswambharan	cbz	x5, 1f
235d5a23af5SJeenu Viswambharan	no_ret	plat_handle_double_fault
236d5a23af5SJeenu Viswambharan
237d5a23af5SJeenu Viswambharan1:
238df8f3188SJeenu Viswambharan	/* Save EL3 state */
239df8f3188SJeenu Viswambharan	mrs	x2, spsr_el3
240df8f3188SJeenu Viswambharan	mrs	x3, elr_el3
241df8f3188SJeenu Viswambharan	stp	x2, x3, [sp, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
242df8f3188SJeenu Viswambharan
243df8f3188SJeenu Viswambharan	/*
244df8f3188SJeenu Viswambharan	 * Save ESR as handling might involve lower ELs, and returning back to
245df8f3188SJeenu Viswambharan	 * EL3 from there would trample the original ESR.
246df8f3188SJeenu Viswambharan	 */
247df8f3188SJeenu Viswambharan	mrs	x4, scr_el3
248df8f3188SJeenu Viswambharan	mrs	x5, esr_el3
249df8f3188SJeenu Viswambharan	stp	x4, x5, [sp, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3]
250df8f3188SJeenu Viswambharan
251df8f3188SJeenu Viswambharan	/*
252df8f3188SJeenu Viswambharan	 * Setup rest of arguments, and call platform External Abort handler.
253df8f3188SJeenu Viswambharan	 *
254df8f3188SJeenu Viswambharan	 * x0: EA reason (already in place)
255df8f3188SJeenu Viswambharan	 * x1: Exception syndrome (already in place).
256df8f3188SJeenu Viswambharan	 * x2: Cookie (unused for now).
257df8f3188SJeenu Viswambharan	 * x3: Context pointer.
258df8f3188SJeenu Viswambharan	 * x4: Flags (security state from SCR for now).
259df8f3188SJeenu Viswambharan	 */
260df8f3188SJeenu Viswambharan	mov	x2, xzr
261df8f3188SJeenu Viswambharan	mov	x3, sp
262df8f3188SJeenu Viswambharan	ubfx	x4, x4, #0, #1
263df8f3188SJeenu Viswambharan
264df8f3188SJeenu Viswambharan	/* Switch to runtime stack */
265df8f3188SJeenu Viswambharan	ldr	x5, [sp, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
266ed108b56SAlexei Fedorov	msr	spsel, #MODE_SP_EL0
267df8f3188SJeenu Viswambharan	mov	sp, x5
268df8f3188SJeenu Viswambharan
269df8f3188SJeenu Viswambharan	mov	x29, x30
270ee6ff1bbSJeenu Viswambharan#if ENABLE_ASSERTIONS
271ee6ff1bbSJeenu Viswambharan	/* Stash the stack pointer */
272ee6ff1bbSJeenu Viswambharan	mov	x28, sp
273ee6ff1bbSJeenu Viswambharan#endif
274df8f3188SJeenu Viswambharan	bl	plat_ea_handler
275df8f3188SJeenu Viswambharan
276ee6ff1bbSJeenu Viswambharan#if ENABLE_ASSERTIONS
277ee6ff1bbSJeenu Viswambharan	/*
278ee6ff1bbSJeenu Viswambharan	 * Error handling flows might involve long jumps; so upon returning from
279ee6ff1bbSJeenu Viswambharan	 * the platform error handler, validate that the we've completely
280ee6ff1bbSJeenu Viswambharan	 * unwound the stack.
281ee6ff1bbSJeenu Viswambharan	 */
282ee6ff1bbSJeenu Viswambharan	mov	x27, sp
283ee6ff1bbSJeenu Viswambharan	cmp	x28, x27
284ee6ff1bbSJeenu Viswambharan	ASM_ASSERT(eq)
285ee6ff1bbSJeenu Viswambharan#endif
286ee6ff1bbSJeenu Viswambharan
287df8f3188SJeenu Viswambharan	/* Make SP point to context */
288ed108b56SAlexei Fedorov	msr	spsel, #MODE_SP_ELX
289df8f3188SJeenu Viswambharan
290d5a23af5SJeenu Viswambharan	/* Restore EL3 state and ESR */
291df8f3188SJeenu Viswambharan	ldp	x1, x2, [sp, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
292df8f3188SJeenu Viswambharan	msr	spsr_el3, x1
293df8f3188SJeenu Viswambharan	msr	elr_el3, x2
294df8f3188SJeenu Viswambharan
295df8f3188SJeenu Viswambharan	/* Restore ESR_EL3 and SCR_EL3 */
296df8f3188SJeenu Viswambharan	ldp	x3, x4, [sp, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3]
297df8f3188SJeenu Viswambharan	msr	scr_el3, x3
298df8f3188SJeenu Viswambharan	msr	esr_el3, x4
299df8f3188SJeenu Viswambharan
300d5a23af5SJeenu Viswambharan#if ENABLE_ASSERTIONS
301d5a23af5SJeenu Viswambharan	cmp	x4, xzr
302d5a23af5SJeenu Viswambharan	ASM_ASSERT(ne)
303d5a23af5SJeenu Viswambharan#endif
304d5a23af5SJeenu Viswambharan
305d5a23af5SJeenu Viswambharan	/* Clear ESR storage */
306d5a23af5SJeenu Viswambharan	str	xzr, [sp, #CTX_EL3STATE_OFFSET + CTX_ESR_EL3]
307d5a23af5SJeenu Viswambharan
308d5a23af5SJeenu Viswambharan	ret	x29
309df8f3188SJeenu Viswambharanendfunc ea_proceed
310