xref: /rk3399_ARM-atf/bl31/aarch64/ea_delegate.S (revision c2d32a5f85a5889742cb7a971558944ecf330936)
1df8f3188SJeenu Viswambharan/*
2*c2d32a5fSMadhukar Pappireddy * Copyright (c) 2018-2021, ARM Limited and Contributors. All rights reserved.
3df8f3188SJeenu Viswambharan *
4df8f3188SJeenu Viswambharan * SPDX-License-Identifier: BSD-3-Clause
5df8f3188SJeenu Viswambharan */
6df8f3188SJeenu Viswambharan
7df8f3188SJeenu Viswambharan
8d5a23af5SJeenu Viswambharan#include <assert_macros.S>
9df8f3188SJeenu Viswambharan#include <asm_macros.S>
10ee6ff1bbSJeenu Viswambharan#include <assert_macros.S>
1109d40e0eSAntonio Nino Diaz#include <bl31/ea_handle.h>
12df8f3188SJeenu Viswambharan#include <context.h>
1309d40e0eSAntonio Nino Diaz#include <lib/extensions/ras_arch.h>
1480942622Slaurenw-arm#include <cpu_macros.S>
1580942622Slaurenw-arm#include <context.h>
16df8f3188SJeenu Viswambharan
17df8f3188SJeenu Viswambharan	.globl	handle_lower_el_ea_esb
18*c2d32a5fSMadhukar Pappireddy	.globl  handle_lower_el_async_ea
19df8f3188SJeenu Viswambharan	.globl	enter_lower_el_sync_ea
20df8f3188SJeenu Viswambharan	.globl	enter_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 *
44df8f3188SJeenu Viswambharan * Since it's part of exception vector, this function doesn't expect any GP
45df8f3188SJeenu Viswambharan * registers to have been saved. It delegates the handling of the EA to platform
46df8f3188SJeenu Viswambharan * handler, and upon successfully handling the EA, exits EL3; otherwise panics.
47df8f3188SJeenu Viswambharan */
48df8f3188SJeenu Viswambharanfunc enter_lower_el_sync_ea
49df8f3188SJeenu Viswambharan	/*
50df8f3188SJeenu Viswambharan	 * Explicitly save x30 so as to free up a register and to enable
51df8f3188SJeenu Viswambharan	 * branching.
52df8f3188SJeenu Viswambharan	 */
53df8f3188SJeenu Viswambharan	str	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
54df8f3188SJeenu Viswambharan
55df8f3188SJeenu Viswambharan	mrs	x30, esr_el3
56df8f3188SJeenu Viswambharan	ubfx	x30, x30, #ESR_EC_SHIFT, #ESR_EC_LENGTH
57df8f3188SJeenu Viswambharan
58df8f3188SJeenu Viswambharan	/* Check for I/D aborts from lower EL */
59df8f3188SJeenu Viswambharan	cmp	x30, #EC_IABORT_LOWER_EL
60df8f3188SJeenu Viswambharan	b.eq	1f
61df8f3188SJeenu Viswambharan
62df8f3188SJeenu Viswambharan	cmp	x30, #EC_DABORT_LOWER_EL
6380942622Slaurenw-arm	b.eq	1f
6480942622Slaurenw-arm
6580942622Slaurenw-arm	/* Save GP registers */
6680942622Slaurenw-arm	stp	x0, x1, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X0]
6780942622Slaurenw-arm	stp	x2, x3, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X2]
6880942622Slaurenw-arm	stp	x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4]
6980942622Slaurenw-arm
7080942622Slaurenw-arm	/* Get the cpu_ops pointer */
7180942622Slaurenw-arm	bl	get_cpu_ops_ptr
7280942622Slaurenw-arm
7380942622Slaurenw-arm	/* Get the cpu_ops exception handler */
7480942622Slaurenw-arm	ldr	x0, [x0, #CPU_E_HANDLER_FUNC]
7580942622Slaurenw-arm
7680942622Slaurenw-arm	/*
7780942622Slaurenw-arm	 * If the reserved function pointer is NULL, this CPU does not have an
7880942622Slaurenw-arm	 * implementation defined exception handler function
7980942622Slaurenw-arm	 */
8080942622Slaurenw-arm	cbz	x0, 2f
8180942622Slaurenw-arm	mrs	x1, esr_el3
8280942622Slaurenw-arm	ubfx	x1, x1, #ESR_EC_SHIFT, #ESR_EC_LENGTH
8380942622Slaurenw-arm	blr	x0
8480942622Slaurenw-arm	b	2f
85df8f3188SJeenu Viswambharan
86df8f3188SJeenu Viswambharan1:
87df8f3188SJeenu Viswambharan	/* Test for EA bit in the instruction syndrome */
88df8f3188SJeenu Viswambharan	mrs	x30, esr_el3
8980942622Slaurenw-arm	tbz	x30, #ESR_ISS_EABORT_EA_BIT, 3f
90df8f3188SJeenu Viswambharan
91e290a8fcSAlexei Fedorov	/*
92ed108b56SAlexei Fedorov	 * Save general purpose and ARMv8.3-PAuth registers (if enabled).
93ed108b56SAlexei Fedorov	 * If Secure Cycle Counter is not disabled in MDCR_EL3 when
94ed108b56SAlexei Fedorov	 * ARMv8.5-PMU is implemented, save PMCR_EL0 and disable Cycle Counter.
95e290a8fcSAlexei Fedorov	 */
96ed108b56SAlexei Fedorov	bl	save_gp_pmcr_pauth_regs
97e290a8fcSAlexei Fedorov
98b86048c4SAntonio Nino Diaz#if ENABLE_PAUTH
99ed108b56SAlexei Fedorov	/* Load and program APIAKey firmware key */
100ed108b56SAlexei Fedorov	bl	pauth_load_bl31_apiakey
101b86048c4SAntonio Nino Diaz#endif
1025283962eSAntonio Nino Diaz
103df8f3188SJeenu Viswambharan	/* Setup exception class and syndrome arguments for platform handler */
104df8f3188SJeenu Viswambharan	mov	x0, #ERROR_EA_SYNC
105df8f3188SJeenu Viswambharan	mrs	x1, esr_el3
106bb9549baSJan Dabros	bl	delegate_sync_ea
107df8f3188SJeenu Viswambharan
108bb9549baSJan Dabros	/* el3_exit assumes SP_EL0 on entry */
109bb9549baSJan Dabros	msr	spsel, #MODE_SP_EL0
110bb9549baSJan Dabros	b	el3_exit
111df8f3188SJeenu Viswambharan2:
11280942622Slaurenw-arm	ldp	x0, x1, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X0]
11380942622Slaurenw-arm	ldp	x2, x3, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X2]
11480942622Slaurenw-arm	ldp	x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4]
11580942622Slaurenw-arm
11680942622Slaurenw-arm3:
117df8f3188SJeenu Viswambharan	/* Synchronous exceptions other than the above are assumed to be EA */
118df8f3188SJeenu Viswambharan	ldr	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
119df8f3188SJeenu Viswambharan	no_ret	report_unhandled_exception
120df8f3188SJeenu Viswambharanendfunc enter_lower_el_sync_ea
121df8f3188SJeenu Viswambharan
122df8f3188SJeenu Viswambharan
123df8f3188SJeenu Viswambharan/*
124df8f3188SJeenu Viswambharan * This function handles SErrors from lower ELs.
125df8f3188SJeenu Viswambharan *
126df8f3188SJeenu Viswambharan * Since it's part of exception vector, this function doesn't expect any GP
127df8f3188SJeenu Viswambharan * registers to have been saved. It delegates the handling of the EA to platform
128df8f3188SJeenu Viswambharan * handler, and upon successfully handling the EA, exits EL3; otherwise panics.
129df8f3188SJeenu Viswambharan */
130df8f3188SJeenu Viswambharanfunc enter_lower_el_async_ea
131df8f3188SJeenu Viswambharan	/*
132df8f3188SJeenu Viswambharan	 * Explicitly save x30 so as to free up a register and to enable
133df8f3188SJeenu Viswambharan	 * branching
134df8f3188SJeenu Viswambharan	 */
135df8f3188SJeenu Viswambharan	str	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
136df8f3188SJeenu Viswambharan
137*c2d32a5fSMadhukar Pappireddyhandle_lower_el_async_ea:
138e290a8fcSAlexei Fedorov	/*
139ed108b56SAlexei Fedorov	 * Save general purpose and ARMv8.3-PAuth registers (if enabled).
140ed108b56SAlexei Fedorov	 * If Secure Cycle Counter is not disabled in MDCR_EL3 when
141ed108b56SAlexei Fedorov	 * ARMv8.5-PMU is implemented, save PMCR_EL0 and disable Cycle Counter.
142e290a8fcSAlexei Fedorov	 */
143ed108b56SAlexei Fedorov	bl	save_gp_pmcr_pauth_regs
144e290a8fcSAlexei Fedorov
145b86048c4SAntonio Nino Diaz#if ENABLE_PAUTH
146ed108b56SAlexei Fedorov	/* Load and program APIAKey firmware key */
147ed108b56SAlexei Fedorov	bl	pauth_load_bl31_apiakey
148b86048c4SAntonio Nino Diaz#endif
1495283962eSAntonio Nino Diaz
150df8f3188SJeenu Viswambharan	/* Setup exception class and syndrome arguments for platform handler */
151df8f3188SJeenu Viswambharan	mov	x0, #ERROR_EA_ASYNC
152df8f3188SJeenu Viswambharan	mrs	x1, esr_el3
153bb9549baSJan Dabros	bl	delegate_async_ea
154bb9549baSJan Dabros
155bb9549baSJan Dabros	/* el3_exit assumes SP_EL0 on entry */
156bb9549baSJan Dabros	msr	spsel, #MODE_SP_EL0
157bb9549baSJan Dabros	b	el3_exit
158df8f3188SJeenu Viswambharanendfunc enter_lower_el_async_ea
159df8f3188SJeenu Viswambharan
160df8f3188SJeenu Viswambharan
161df8f3188SJeenu Viswambharan/*
162b56dc2a9SJeenu Viswambharan * Prelude for Synchronous External Abort handling. This function assumes that
163b56dc2a9SJeenu Viswambharan * all GP registers have been saved by the caller.
164b56dc2a9SJeenu Viswambharan *
165b56dc2a9SJeenu Viswambharan * x0: EA reason
166b56dc2a9SJeenu Viswambharan * x1: EA syndrome
167b56dc2a9SJeenu Viswambharan */
168b56dc2a9SJeenu Viswambharanfunc delegate_sync_ea
169b56dc2a9SJeenu Viswambharan#if RAS_EXTENSION
170b56dc2a9SJeenu Viswambharan	/*
171b56dc2a9SJeenu Viswambharan	 * Check for Uncontainable error type. If so, route to the platform
172b56dc2a9SJeenu Viswambharan	 * fatal error handler rather than the generic EA one.
173b56dc2a9SJeenu Viswambharan	 */
174b56dc2a9SJeenu Viswambharan	ubfx    x2, x1, #EABORT_SET_SHIFT, #EABORT_SET_WIDTH
175b56dc2a9SJeenu Viswambharan	cmp     x2, #ERROR_STATUS_SET_UC
176b56dc2a9SJeenu Viswambharan	b.ne    1f
177b56dc2a9SJeenu Viswambharan
178b56dc2a9SJeenu Viswambharan	/* Check fault status code */
179b56dc2a9SJeenu Viswambharan	ubfx    x3, x1, #EABORT_DFSC_SHIFT, #EABORT_DFSC_WIDTH
180b56dc2a9SJeenu Viswambharan	cmp     x3, #SYNC_EA_FSC
181b56dc2a9SJeenu Viswambharan	b.ne    1f
182b56dc2a9SJeenu Viswambharan
183b56dc2a9SJeenu Viswambharan	no_ret  plat_handle_uncontainable_ea
184b56dc2a9SJeenu Viswambharan1:
185b56dc2a9SJeenu Viswambharan#endif
186b56dc2a9SJeenu Viswambharan
187b56dc2a9SJeenu Viswambharan	b       ea_proceed
188b56dc2a9SJeenu Viswambharanendfunc delegate_sync_ea
189b56dc2a9SJeenu Viswambharan
190b56dc2a9SJeenu Viswambharan
191b56dc2a9SJeenu Viswambharan/*
192b56dc2a9SJeenu Viswambharan * Prelude for Asynchronous External Abort handling. This function assumes that
193b56dc2a9SJeenu Viswambharan * all GP registers have been saved by the caller.
194b56dc2a9SJeenu Viswambharan *
195b56dc2a9SJeenu Viswambharan * x0: EA reason
196b56dc2a9SJeenu Viswambharan * x1: EA syndrome
197b56dc2a9SJeenu Viswambharan */
198b56dc2a9SJeenu Viswambharanfunc delegate_async_ea
199b56dc2a9SJeenu Viswambharan#if RAS_EXTENSION
200b56dc2a9SJeenu Viswambharan	/*
201b56dc2a9SJeenu Viswambharan	 * Check for Implementation Defined Syndrome. If so, skip checking
202b56dc2a9SJeenu Viswambharan	 * Uncontainable error type from the syndrome as the format is unknown.
203b56dc2a9SJeenu Viswambharan	 */
204b56dc2a9SJeenu Viswambharan	tbnz	x1, #SERROR_IDS_BIT, 1f
205b56dc2a9SJeenu Viswambharan
206b56dc2a9SJeenu Viswambharan	/*
207b56dc2a9SJeenu Viswambharan	 * Check for Uncontainable error type. If so, route to the platform
208b56dc2a9SJeenu Viswambharan	 * fatal error handler rather than the generic EA one.
209b56dc2a9SJeenu Viswambharan	 */
210b56dc2a9SJeenu Viswambharan	ubfx	x2, x1, #EABORT_AET_SHIFT, #EABORT_AET_WIDTH
211b56dc2a9SJeenu Viswambharan	cmp	x2, #ERROR_STATUS_UET_UC
212b56dc2a9SJeenu Viswambharan	b.ne	1f
213b56dc2a9SJeenu Viswambharan
214b56dc2a9SJeenu Viswambharan	/* Check DFSC for SError type */
215b56dc2a9SJeenu Viswambharan	ubfx	x3, x1, #EABORT_DFSC_SHIFT, #EABORT_DFSC_WIDTH
216b56dc2a9SJeenu Viswambharan	cmp	x3, #DFSC_SERROR
217b56dc2a9SJeenu Viswambharan	b.ne	1f
218b56dc2a9SJeenu Viswambharan
219b56dc2a9SJeenu Viswambharan	no_ret	plat_handle_uncontainable_ea
220b56dc2a9SJeenu Viswambharan1:
221b56dc2a9SJeenu Viswambharan#endif
222b56dc2a9SJeenu Viswambharan
223b56dc2a9SJeenu Viswambharan	b	ea_proceed
224b56dc2a9SJeenu Viswambharanendfunc delegate_async_ea
225b56dc2a9SJeenu Viswambharan
226b56dc2a9SJeenu Viswambharan
227b56dc2a9SJeenu Viswambharan/*
228df8f3188SJeenu Viswambharan * Delegate External Abort handling to platform's EA handler. This function
229df8f3188SJeenu Viswambharan * assumes that all GP registers have been saved by the caller.
230df8f3188SJeenu Viswambharan *
231df8f3188SJeenu Viswambharan * x0: EA reason
232df8f3188SJeenu Viswambharan * x1: EA syndrome
233df8f3188SJeenu Viswambharan */
234df8f3188SJeenu Viswambharanfunc ea_proceed
235d5a23af5SJeenu Viswambharan	/*
236d5a23af5SJeenu Viswambharan	 * If the ESR loaded earlier is not zero, we were processing an EA
237d5a23af5SJeenu Viswambharan	 * already, and this is a double fault.
238d5a23af5SJeenu Viswambharan	 */
239d5a23af5SJeenu Viswambharan	ldr	x5, [sp, #CTX_EL3STATE_OFFSET + CTX_ESR_EL3]
240d5a23af5SJeenu Viswambharan	cbz	x5, 1f
241d5a23af5SJeenu Viswambharan	no_ret	plat_handle_double_fault
242d5a23af5SJeenu Viswambharan
243d5a23af5SJeenu Viswambharan1:
244df8f3188SJeenu Viswambharan	/* Save EL3 state */
245df8f3188SJeenu Viswambharan	mrs	x2, spsr_el3
246df8f3188SJeenu Viswambharan	mrs	x3, elr_el3
247df8f3188SJeenu Viswambharan	stp	x2, x3, [sp, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
248df8f3188SJeenu Viswambharan
249df8f3188SJeenu Viswambharan	/*
250df8f3188SJeenu Viswambharan	 * Save ESR as handling might involve lower ELs, and returning back to
251df8f3188SJeenu Viswambharan	 * EL3 from there would trample the original ESR.
252df8f3188SJeenu Viswambharan	 */
253df8f3188SJeenu Viswambharan	mrs	x4, scr_el3
254df8f3188SJeenu Viswambharan	mrs	x5, esr_el3
255df8f3188SJeenu Viswambharan	stp	x4, x5, [sp, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3]
256df8f3188SJeenu Viswambharan
257df8f3188SJeenu Viswambharan	/*
258df8f3188SJeenu Viswambharan	 * Setup rest of arguments, and call platform External Abort handler.
259df8f3188SJeenu Viswambharan	 *
260df8f3188SJeenu Viswambharan	 * x0: EA reason (already in place)
261df8f3188SJeenu Viswambharan	 * x1: Exception syndrome (already in place).
262df8f3188SJeenu Viswambharan	 * x2: Cookie (unused for now).
263df8f3188SJeenu Viswambharan	 * x3: Context pointer.
264df8f3188SJeenu Viswambharan	 * x4: Flags (security state from SCR for now).
265df8f3188SJeenu Viswambharan	 */
266df8f3188SJeenu Viswambharan	mov	x2, xzr
267df8f3188SJeenu Viswambharan	mov	x3, sp
268df8f3188SJeenu Viswambharan	ubfx	x4, x4, #0, #1
269df8f3188SJeenu Viswambharan
270df8f3188SJeenu Viswambharan	/* Switch to runtime stack */
271df8f3188SJeenu Viswambharan	ldr	x5, [sp, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
272ed108b56SAlexei Fedorov	msr	spsel, #MODE_SP_EL0
273df8f3188SJeenu Viswambharan	mov	sp, x5
274df8f3188SJeenu Viswambharan
275df8f3188SJeenu Viswambharan	mov	x29, x30
276ee6ff1bbSJeenu Viswambharan#if ENABLE_ASSERTIONS
277ee6ff1bbSJeenu Viswambharan	/* Stash the stack pointer */
278ee6ff1bbSJeenu Viswambharan	mov	x28, sp
279ee6ff1bbSJeenu Viswambharan#endif
280df8f3188SJeenu Viswambharan	bl	plat_ea_handler
281df8f3188SJeenu Viswambharan
282ee6ff1bbSJeenu Viswambharan#if ENABLE_ASSERTIONS
283ee6ff1bbSJeenu Viswambharan	/*
284ee6ff1bbSJeenu Viswambharan	 * Error handling flows might involve long jumps; so upon returning from
285ee6ff1bbSJeenu Viswambharan	 * the platform error handler, validate that the we've completely
286ee6ff1bbSJeenu Viswambharan	 * unwound the stack.
287ee6ff1bbSJeenu Viswambharan	 */
288ee6ff1bbSJeenu Viswambharan	mov	x27, sp
289ee6ff1bbSJeenu Viswambharan	cmp	x28, x27
290ee6ff1bbSJeenu Viswambharan	ASM_ASSERT(eq)
291ee6ff1bbSJeenu Viswambharan#endif
292ee6ff1bbSJeenu Viswambharan
293df8f3188SJeenu Viswambharan	/* Make SP point to context */
294ed108b56SAlexei Fedorov	msr	spsel, #MODE_SP_ELX
295df8f3188SJeenu Viswambharan
296d5a23af5SJeenu Viswambharan	/* Restore EL3 state and ESR */
297df8f3188SJeenu Viswambharan	ldp	x1, x2, [sp, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
298df8f3188SJeenu Viswambharan	msr	spsr_el3, x1
299df8f3188SJeenu Viswambharan	msr	elr_el3, x2
300df8f3188SJeenu Viswambharan
301df8f3188SJeenu Viswambharan	/* Restore ESR_EL3 and SCR_EL3 */
302df8f3188SJeenu Viswambharan	ldp	x3, x4, [sp, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3]
303df8f3188SJeenu Viswambharan	msr	scr_el3, x3
304df8f3188SJeenu Viswambharan	msr	esr_el3, x4
305df8f3188SJeenu Viswambharan
306d5a23af5SJeenu Viswambharan#if ENABLE_ASSERTIONS
307d5a23af5SJeenu Viswambharan	cmp	x4, xzr
308d5a23af5SJeenu Viswambharan	ASM_ASSERT(ne)
309d5a23af5SJeenu Viswambharan#endif
310d5a23af5SJeenu Viswambharan
311d5a23af5SJeenu Viswambharan	/* Clear ESR storage */
312d5a23af5SJeenu Viswambharan	str	xzr, [sp, #CTX_EL3STATE_OFFSET + CTX_ESR_EL3]
313d5a23af5SJeenu Viswambharan
314d5a23af5SJeenu Viswambharan	ret	x29
315df8f3188SJeenu Viswambharanendfunc ea_proceed
316