xref: /rk3399_ARM-atf/bl31/aarch64/runtime_exceptions.S (revision 4693ff7225faadc5ad1bcd1c2fb3fbbb8fe1aed0)
14f6ad66aSAchin Gupta/*
2c2d32a5fSMadhukar Pappireddy * Copyright (c) 2013-2021, ARM Limited and Contributors. All rights reserved.
34f6ad66aSAchin Gupta *
482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause
54f6ad66aSAchin Gupta */
64f6ad66aSAchin Gupta
709d40e0eSAntonio Nino Diaz#include <platform_def.h>
809d40e0eSAntonio Nino Diaz
94f6ad66aSAchin Gupta#include <arch.h>
1035e98e55SDan Handley#include <asm_macros.S>
1109d40e0eSAntonio Nino Diaz#include <bl31/ea_handle.h>
1209d40e0eSAntonio Nino Diaz#include <bl31/interrupt_mgmt.h>
1309d40e0eSAntonio Nino Diaz#include <common/runtime_svc.h>
1497043ac9SDan Handley#include <context.h>
153b8456bdSManish V Badarkhe#include <el3_common_macros.S>
1609d40e0eSAntonio Nino Diaz#include <lib/el3_runtime/cpu_data.h>
1709d40e0eSAntonio Nino Diaz#include <lib/smccc.h>
184f6ad66aSAchin Gupta
194f6ad66aSAchin Gupta	.globl	runtime_exceptions
204f6ad66aSAchin Gupta
21f62ad322SDimitris Papastamos	.globl	sync_exception_sp_el0
22f62ad322SDimitris Papastamos	.globl	irq_sp_el0
23f62ad322SDimitris Papastamos	.globl	fiq_sp_el0
24f62ad322SDimitris Papastamos	.globl	serror_sp_el0
25f62ad322SDimitris Papastamos
26f62ad322SDimitris Papastamos	.globl	sync_exception_sp_elx
27f62ad322SDimitris Papastamos	.globl	irq_sp_elx
28f62ad322SDimitris Papastamos	.globl	fiq_sp_elx
29f62ad322SDimitris Papastamos	.globl	serror_sp_elx
30f62ad322SDimitris Papastamos
31f62ad322SDimitris Papastamos	.globl	sync_exception_aarch64
32f62ad322SDimitris Papastamos	.globl	irq_aarch64
33f62ad322SDimitris Papastamos	.globl	fiq_aarch64
34f62ad322SDimitris Papastamos	.globl	serror_aarch64
35f62ad322SDimitris Papastamos
36f62ad322SDimitris Papastamos	.globl	sync_exception_aarch32
37f62ad322SDimitris Papastamos	.globl	irq_aarch32
38f62ad322SDimitris Papastamos	.globl	fiq_aarch32
39f62ad322SDimitris Papastamos	.globl	serror_aarch32
40f62ad322SDimitris Papastamos
4176454abfSJeenu Viswambharan	/*
4214c6016aSJeenu Viswambharan	 * Macro that prepares entry to EL3 upon taking an exception.
4314c6016aSJeenu Viswambharan	 *
4414c6016aSJeenu Viswambharan	 * With RAS_EXTENSION, this macro synchronizes pending errors with an ESB
4514c6016aSJeenu Viswambharan	 * instruction. When an error is thus synchronized, the handling is
4614c6016aSJeenu Viswambharan	 * delegated to platform EA handler.
4714c6016aSJeenu Viswambharan	 *
48c2d32a5fSMadhukar Pappireddy	 * Without RAS_EXTENSION, this macro synchronizes pending errors using
49c2d32a5fSMadhukar Pappireddy         * a DSB, unmasks Asynchronous External Aborts and saves X30 before
50c2d32a5fSMadhukar Pappireddy	 * setting the flag CTX_IS_IN_EL3.
5114c6016aSJeenu Viswambharan	 */
5214c6016aSJeenu Viswambharan	.macro check_and_unmask_ea
5314c6016aSJeenu Viswambharan#if RAS_EXTENSION
5414c6016aSJeenu Viswambharan	/* Synchronize pending External Aborts */
5514c6016aSJeenu Viswambharan	esb
5614c6016aSJeenu Viswambharan
5714c6016aSJeenu Viswambharan	/* Unmask the SError interrupt */
5814c6016aSJeenu Viswambharan	msr	daifclr, #DAIF_ABT_BIT
5914c6016aSJeenu Viswambharan
6014c6016aSJeenu Viswambharan	/*
6114c6016aSJeenu Viswambharan	 * Explicitly save x30 so as to free up a register and to enable
6214c6016aSJeenu Viswambharan	 * branching
6314c6016aSJeenu Viswambharan	 */
6414c6016aSJeenu Viswambharan	str	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
6514c6016aSJeenu Viswambharan
6614c6016aSJeenu Viswambharan	/* Check for SErrors synchronized by the ESB instruction */
6714c6016aSJeenu Viswambharan	mrs	x30, DISR_EL1
6814c6016aSJeenu Viswambharan	tbz	x30, #DISR_A_BIT, 1f
6914c6016aSJeenu Viswambharan
70e290a8fcSAlexei Fedorov	/*
71ed108b56SAlexei Fedorov	 * Save general purpose and ARMv8.3-PAuth registers (if enabled).
72ed108b56SAlexei Fedorov	 * If Secure Cycle Counter is not disabled in MDCR_EL3 when
73ed108b56SAlexei Fedorov	 * ARMv8.5-PMU is implemented, save PMCR_EL0 and disable Cycle Counter.
74e290a8fcSAlexei Fedorov	 */
75ed108b56SAlexei Fedorov	bl	save_gp_pmcr_pauth_regs
76e290a8fcSAlexei Fedorov
77df8f3188SJeenu Viswambharan	bl	handle_lower_el_ea_esb
7814c6016aSJeenu Viswambharan
79ed108b56SAlexei Fedorov	/* Restore general purpose, PMCR_EL0 and ARMv8.3-PAuth registers */
80ed108b56SAlexei Fedorov	bl	restore_gp_pmcr_pauth_regs
8114c6016aSJeenu Viswambharan1:
8214c6016aSJeenu Viswambharan#else
83c2d32a5fSMadhukar Pappireddy	/*
84c2d32a5fSMadhukar Pappireddy	 * For SoCs which do not implement RAS, use DSB as a barrier to
85c2d32a5fSMadhukar Pappireddy	 * synchronize pending external aborts.
86c2d32a5fSMadhukar Pappireddy	 */
87c2d32a5fSMadhukar Pappireddy	dsb	sy
88c2d32a5fSMadhukar Pappireddy
8914c6016aSJeenu Viswambharan	/* Unmask the SError interrupt */
9014c6016aSJeenu Viswambharan	msr	daifclr, #DAIF_ABT_BIT
9114c6016aSJeenu Viswambharan
92c2d32a5fSMadhukar Pappireddy	/* Use ISB for the above unmask operation to take effect immediately */
93c2d32a5fSMadhukar Pappireddy	isb
94c2d32a5fSMadhukar Pappireddy
95c2d32a5fSMadhukar Pappireddy	/*
96c2d32a5fSMadhukar Pappireddy	 * Refer Note 1. No need to restore X30 as both handle_sync_exception
97c2d32a5fSMadhukar Pappireddy	 * and handle_interrupt_exception macro which follow this macro modify
98c2d32a5fSMadhukar Pappireddy	 * X30 anyway.
99c2d32a5fSMadhukar Pappireddy	 */
10014c6016aSJeenu Viswambharan	str	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
101c2d32a5fSMadhukar Pappireddy	mov 	x30, #1
102c2d32a5fSMadhukar Pappireddy	str	x30, [sp, #CTX_EL3STATE_OFFSET + CTX_IS_IN_EL3]
103c2d32a5fSMadhukar Pappireddy	dmb	sy
10414c6016aSJeenu Viswambharan#endif
10514c6016aSJeenu Viswambharan	.endm
10614c6016aSJeenu Viswambharan
107c2d32a5fSMadhukar Pappireddy#if !RAS_EXTENSION
108c2d32a5fSMadhukar Pappireddy	/*
109c2d32a5fSMadhukar Pappireddy	 * Note 1: The explicit DSB at the entry of various exception vectors
110c2d32a5fSMadhukar Pappireddy	 * for handling exceptions from lower ELs can inadvertently trigger an
111c2d32a5fSMadhukar Pappireddy	 * SError exception in EL3 due to pending asynchronous aborts in lower
112c2d32a5fSMadhukar Pappireddy	 * ELs. This will end up being handled by serror_sp_elx which will
113c2d32a5fSMadhukar Pappireddy	 * ultimately panic and die.
114c2d32a5fSMadhukar Pappireddy	 * The way to workaround is to update a flag to indicate if the exception
115c2d32a5fSMadhukar Pappireddy	 * truly came from EL3. This flag is allocated in the cpu_context
116c2d32a5fSMadhukar Pappireddy	 * structure and located at offset "CTX_EL3STATE_OFFSET + CTX_IS_IN_EL3"
117c2d32a5fSMadhukar Pappireddy	 * This is not a bullet proof solution to the problem at hand because
118c2d32a5fSMadhukar Pappireddy	 * we assume the instructions following "isb" that help to update the
119c2d32a5fSMadhukar Pappireddy	 * flag execute without causing further exceptions.
120c2d32a5fSMadhukar Pappireddy	 */
121c2d32a5fSMadhukar Pappireddy
122c2d32a5fSMadhukar Pappireddy	/* ---------------------------------------------------------------------
123c2d32a5fSMadhukar Pappireddy	 * This macro handles Asynchronous External Aborts.
124c2d32a5fSMadhukar Pappireddy	 * ---------------------------------------------------------------------
125c2d32a5fSMadhukar Pappireddy	 */
126c2d32a5fSMadhukar Pappireddy	.macro	handle_async_ea
127c2d32a5fSMadhukar Pappireddy	/*
128c2d32a5fSMadhukar Pappireddy	 * Use a barrier to synchronize pending external aborts.
129c2d32a5fSMadhukar Pappireddy	 */
130c2d32a5fSMadhukar Pappireddy	dsb	sy
131c2d32a5fSMadhukar Pappireddy
132c2d32a5fSMadhukar Pappireddy	/* Unmask the SError interrupt */
133c2d32a5fSMadhukar Pappireddy	msr	daifclr, #DAIF_ABT_BIT
134c2d32a5fSMadhukar Pappireddy
135c2d32a5fSMadhukar Pappireddy	/* Use ISB for the above unmask operation to take effect immediately */
136c2d32a5fSMadhukar Pappireddy	isb
137c2d32a5fSMadhukar Pappireddy
138c2d32a5fSMadhukar Pappireddy	/* Refer Note 1 */
139c2d32a5fSMadhukar Pappireddy	str	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
140c2d32a5fSMadhukar Pappireddy	mov 	x30, #1
141c2d32a5fSMadhukar Pappireddy	str	x30, [sp, #CTX_EL3STATE_OFFSET + CTX_IS_IN_EL3]
142c2d32a5fSMadhukar Pappireddy	dmb	sy
143c2d32a5fSMadhukar Pappireddy
144c2d32a5fSMadhukar Pappireddy	b	handle_lower_el_async_ea
145c2d32a5fSMadhukar Pappireddy	.endm
146c2d32a5fSMadhukar Pappireddy
147c2d32a5fSMadhukar Pappireddy	/*
148c2d32a5fSMadhukar Pappireddy	 * This macro checks if the exception was taken due to SError in EL3 or
149c2d32a5fSMadhukar Pappireddy	 * because of pending asynchronous external aborts from lower EL that got
150c2d32a5fSMadhukar Pappireddy	 * triggered due to explicit synchronization in EL3. Refer Note 1.
151c2d32a5fSMadhukar Pappireddy	 */
152c2d32a5fSMadhukar Pappireddy	.macro check_if_serror_from_EL3
153c2d32a5fSMadhukar Pappireddy	/* Assumes SP_EL3 on entry */
154c2d32a5fSMadhukar Pappireddy	str	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
155c2d32a5fSMadhukar Pappireddy	ldr	x30, [sp, #CTX_EL3STATE_OFFSET + CTX_IS_IN_EL3]
156c2d32a5fSMadhukar Pappireddy	cbnz	x30, exp_from_EL3
157c2d32a5fSMadhukar Pappireddy
158c2d32a5fSMadhukar Pappireddy	/* Handle asynchronous external abort from lower EL */
159c2d32a5fSMadhukar Pappireddy	b	handle_lower_el_async_ea
160c2d32a5fSMadhukar Pappireddy
161c2d32a5fSMadhukar Pappireddyexp_from_EL3:
162c2d32a5fSMadhukar Pappireddy	/* Jump to plat_handle_el3_ea which does not return */
163c2d32a5fSMadhukar Pappireddy	.endm
164c2d32a5fSMadhukar Pappireddy#endif
165c2d32a5fSMadhukar Pappireddy
166a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
167a6ef4393SDouglas Raillard	 * This macro handles Synchronous exceptions.
168a6ef4393SDouglas Raillard	 * Only SMC exceptions are supported.
169a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
170dce74b89SAchin Gupta	 */
171dce74b89SAchin Gupta	.macro	handle_sync_exception
172872be88aSdp-arm#if ENABLE_RUNTIME_INSTRUMENTATION
173872be88aSdp-arm	/*
174a6ef4393SDouglas Raillard	 * Read the timestamp value and store it in per-cpu data. The value
175a6ef4393SDouglas Raillard	 * will be extracted from per-cpu data by the C level SMC handler and
176a6ef4393SDouglas Raillard	 * saved to the PMF timestamp region.
177872be88aSdp-arm	 */
178872be88aSdp-arm	mrs	x30, cntpct_el0
179872be88aSdp-arm	str	x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29]
180872be88aSdp-arm	mrs	x29, tpidr_el3
181872be88aSdp-arm	str	x30, [x29, #CPU_DATA_PMF_TS0_OFFSET]
182872be88aSdp-arm	ldr	x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29]
183872be88aSdp-arm#endif
184872be88aSdp-arm
185dce74b89SAchin Gupta	mrs	x30, esr_el3
186dce74b89SAchin Gupta	ubfx	x30, x30, #ESR_EC_SHIFT, #ESR_EC_LENGTH
187dce74b89SAchin Gupta
188a6ef4393SDouglas Raillard	/* Handle SMC exceptions separately from other synchronous exceptions */
189dce74b89SAchin Gupta	cmp	x30, #EC_AARCH32_SMC
190dce74b89SAchin Gupta	b.eq	smc_handler32
191dce74b89SAchin Gupta
192dce74b89SAchin Gupta	cmp	x30, #EC_AARCH64_SMC
193dce74b89SAchin Gupta	b.eq	smc_handler64
194dce74b89SAchin Gupta
195df8f3188SJeenu Viswambharan	/* Synchronous exceptions other than the above are assumed to be EA */
1964d91838bSJulius Werner	ldr	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
197df8f3188SJeenu Viswambharan	b	enter_lower_el_sync_ea
198dce74b89SAchin Gupta	.endm
199dce74b89SAchin Gupta
200dce74b89SAchin Gupta
201a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
202a6ef4393SDouglas Raillard	 * This macro handles FIQ or IRQ interrupts i.e. EL3, S-EL1 and NS
203a6ef4393SDouglas Raillard	 * interrupts.
204a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
205dce74b89SAchin Gupta	 */
206dce74b89SAchin Gupta	.macro	handle_interrupt_exception label
2075283962eSAntonio Nino Diaz
208e290a8fcSAlexei Fedorov	/*
209ed108b56SAlexei Fedorov	 * Save general purpose and ARMv8.3-PAuth registers (if enabled).
210ed108b56SAlexei Fedorov	 * If Secure Cycle Counter is not disabled in MDCR_EL3 when
211ed108b56SAlexei Fedorov	 * ARMv8.5-PMU is implemented, save PMCR_EL0 and disable Cycle Counter.
212e290a8fcSAlexei Fedorov	 */
213ed108b56SAlexei Fedorov	bl	save_gp_pmcr_pauth_regs
214e290a8fcSAlexei Fedorov
215b86048c4SAntonio Nino Diaz#if ENABLE_PAUTH
216ed108b56SAlexei Fedorov	/* Load and program APIAKey firmware key */
217ed108b56SAlexei Fedorov	bl	pauth_load_bl31_apiakey
218b86048c4SAntonio Nino Diaz#endif
2195283962eSAntonio Nino Diaz
220a6ef4393SDouglas Raillard	/* Save the EL3 system registers needed to return from this exception */
2215717aae1SAchin Gupta	mrs	x0, spsr_el3
2225717aae1SAchin Gupta	mrs	x1, elr_el3
2235717aae1SAchin Gupta	stp	x0, x1, [sp, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
2245717aae1SAchin Gupta
225dce74b89SAchin Gupta	/* Switch to the runtime stack i.e. SP_EL0 */
226dce74b89SAchin Gupta	ldr	x2, [sp, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
227dce74b89SAchin Gupta	mov	x20, sp
228ed108b56SAlexei Fedorov	msr	spsel, #MODE_SP_EL0
229dce74b89SAchin Gupta	mov	sp, x2
230dce74b89SAchin Gupta
231dce74b89SAchin Gupta	/*
232a6ef4393SDouglas Raillard	 * Find out whether this is a valid interrupt type.
233a6ef4393SDouglas Raillard	 * If the interrupt controller reports a spurious interrupt then return
234a6ef4393SDouglas Raillard	 * to where we came from.
235dce74b89SAchin Gupta	 */
2369865ac15SDan Handley	bl	plat_ic_get_pending_interrupt_type
237dce74b89SAchin Gupta	cmp	x0, #INTR_TYPE_INVAL
238dce74b89SAchin Gupta	b.eq	interrupt_exit_\label
239dce74b89SAchin Gupta
240dce74b89SAchin Gupta	/*
241a6ef4393SDouglas Raillard	 * Get the registered handler for this interrupt type.
242a6ef4393SDouglas Raillard	 * A NULL return value could be 'cause of the following conditions:
2435717aae1SAchin Gupta	 *
244a6ef4393SDouglas Raillard	 * a. An interrupt of a type was routed correctly but a handler for its
245a6ef4393SDouglas Raillard	 *    type was not registered.
2465717aae1SAchin Gupta	 *
247a6ef4393SDouglas Raillard	 * b. An interrupt of a type was not routed correctly so a handler for
248a6ef4393SDouglas Raillard	 *    its type was not registered.
2495717aae1SAchin Gupta	 *
250a6ef4393SDouglas Raillard	 * c. An interrupt of a type was routed correctly to EL3, but was
251a6ef4393SDouglas Raillard	 *    deasserted before its pending state could be read. Another
252a6ef4393SDouglas Raillard	 *    interrupt of a different type pended at the same time and its
253a6ef4393SDouglas Raillard	 *    type was reported as pending instead. However, a handler for this
254a6ef4393SDouglas Raillard	 *    type was not registered.
2555717aae1SAchin Gupta	 *
256a6ef4393SDouglas Raillard	 * a. and b. can only happen due to a programming error. The
257a6ef4393SDouglas Raillard	 * occurrence of c. could be beyond the control of Trusted Firmware.
258a6ef4393SDouglas Raillard	 * It makes sense to return from this exception instead of reporting an
259a6ef4393SDouglas Raillard	 * error.
260dce74b89SAchin Gupta	 */
261dce74b89SAchin Gupta	bl	get_interrupt_type_handler
2625717aae1SAchin Gupta	cbz	x0, interrupt_exit_\label
263dce74b89SAchin Gupta	mov	x21, x0
264dce74b89SAchin Gupta
265dce74b89SAchin Gupta	mov	x0, #INTR_ID_UNAVAILABLE
266dce74b89SAchin Gupta
267dce74b89SAchin Gupta	/* Set the current security state in the 'flags' parameter */
268dce74b89SAchin Gupta	mrs	x2, scr_el3
269dce74b89SAchin Gupta	ubfx	x1, x2, #0, #1
270dce74b89SAchin Gupta
271dce74b89SAchin Gupta	/* Restore the reference to the 'handle' i.e. SP_EL3 */
272dce74b89SAchin Gupta	mov	x2, x20
273dce74b89SAchin Gupta
274b460b8bfSSoby Mathew	/* x3 will point to a cookie (not used now) */
275b460b8bfSSoby Mathew	mov	x3, xzr
276b460b8bfSSoby Mathew
277dce74b89SAchin Gupta	/* Call the interrupt type handler */
278dce74b89SAchin Gupta	blr	x21
279dce74b89SAchin Gupta
280dce74b89SAchin Guptainterrupt_exit_\label:
281dce74b89SAchin Gupta	/* Return from exception, possibly in a different security state */
282dce74b89SAchin Gupta	b	el3_exit
283dce74b89SAchin Gupta
284dce74b89SAchin Gupta	.endm
285dce74b89SAchin Gupta
286dce74b89SAchin Gupta
287e0ae9fabSSandrine Bailleuxvector_base runtime_exceptions
288e0ae9fabSSandrine Bailleux
289a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
290a6ef4393SDouglas Raillard	 * Current EL with SP_EL0 : 0x0 - 0x200
291a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
2924f6ad66aSAchin Gupta	 */
293e0ae9fabSSandrine Bailleuxvector_entry sync_exception_sp_el0
2941f461979SJustin Chadwell#ifdef MONITOR_TRAPS
2951f461979SJustin Chadwell	stp x29, x30, [sp, #-16]!
2961f461979SJustin Chadwell
2971f461979SJustin Chadwell	mrs	x30, esr_el3
2981f461979SJustin Chadwell	ubfx	x30, x30, #ESR_EC_SHIFT, #ESR_EC_LENGTH
2991f461979SJustin Chadwell
3001f461979SJustin Chadwell	/* Check for BRK */
3011f461979SJustin Chadwell	cmp	x30, #EC_BRK
3021f461979SJustin Chadwell	b.eq	brk_handler
3031f461979SJustin Chadwell
3041f461979SJustin Chadwell	ldp x29, x30, [sp], #16
3051f461979SJustin Chadwell#endif /* MONITOR_TRAPS */
3061f461979SJustin Chadwell
307a6ef4393SDouglas Raillard	/* We don't expect any synchronous exceptions from EL3 */
3084d91838bSJulius Werner	b	report_unhandled_exception
309a9203edaSRoberto Vargasend_vector_entry sync_exception_sp_el0
3104f6ad66aSAchin Gupta
311e0ae9fabSSandrine Bailleuxvector_entry irq_sp_el0
312a6ef4393SDouglas Raillard	/*
313a6ef4393SDouglas Raillard	 * EL3 code is non-reentrant. Any asynchronous exception is a serious
314a6ef4393SDouglas Raillard	 * error. Loop infinitely.
315a6ef4393SDouglas Raillard	 */
3164d91838bSJulius Werner	b	report_unhandled_interrupt
317a9203edaSRoberto Vargasend_vector_entry irq_sp_el0
3184f6ad66aSAchin Gupta
319e0ae9fabSSandrine Bailleux
320e0ae9fabSSandrine Bailleuxvector_entry fiq_sp_el0
3214d91838bSJulius Werner	b	report_unhandled_interrupt
322a9203edaSRoberto Vargasend_vector_entry fiq_sp_el0
3234f6ad66aSAchin Gupta
324e0ae9fabSSandrine Bailleux
325e0ae9fabSSandrine Bailleuxvector_entry serror_sp_el0
326eaeaa4d0SJeenu Viswambharan	no_ret	plat_handle_el3_ea
327a9203edaSRoberto Vargasend_vector_entry serror_sp_el0
3284f6ad66aSAchin Gupta
329a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
330a6ef4393SDouglas Raillard	 * Current EL with SP_ELx: 0x200 - 0x400
331a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
3324f6ad66aSAchin Gupta	 */
333e0ae9fabSSandrine Bailleuxvector_entry sync_exception_sp_elx
334a6ef4393SDouglas Raillard	/*
335a6ef4393SDouglas Raillard	 * This exception will trigger if anything went wrong during a previous
336a6ef4393SDouglas Raillard	 * exception entry or exit or while handling an earlier unexpected
337a6ef4393SDouglas Raillard	 * synchronous exception. There is a high probability that SP_EL3 is
338a6ef4393SDouglas Raillard	 * corrupted.
339caa84939SJeenu Viswambharan	 */
3404d91838bSJulius Werner	b	report_unhandled_exception
341a9203edaSRoberto Vargasend_vector_entry sync_exception_sp_elx
3424f6ad66aSAchin Gupta
343e0ae9fabSSandrine Bailleuxvector_entry irq_sp_elx
3444d91838bSJulius Werner	b	report_unhandled_interrupt
345a9203edaSRoberto Vargasend_vector_entry irq_sp_elx
346a7934d69SJeenu Viswambharan
347e0ae9fabSSandrine Bailleuxvector_entry fiq_sp_elx
3484d91838bSJulius Werner	b	report_unhandled_interrupt
349a9203edaSRoberto Vargasend_vector_entry fiq_sp_elx
350a7934d69SJeenu Viswambharan
351e0ae9fabSSandrine Bailleuxvector_entry serror_sp_elx
352c2d32a5fSMadhukar Pappireddy#if !RAS_EXTENSION
353c2d32a5fSMadhukar Pappireddy	check_if_serror_from_EL3
354c2d32a5fSMadhukar Pappireddy#endif
355eaeaa4d0SJeenu Viswambharan	no_ret	plat_handle_el3_ea
356a9203edaSRoberto Vargasend_vector_entry serror_sp_elx
3574f6ad66aSAchin Gupta
358a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
35944804252SSandrine Bailleux	 * Lower EL using AArch64 : 0x400 - 0x600
360a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
3614f6ad66aSAchin Gupta	 */
362e0ae9fabSSandrine Bailleuxvector_entry sync_exception_aarch64
363a6ef4393SDouglas Raillard	/*
364a6ef4393SDouglas Raillard	 * This exception vector will be the entry point for SMCs and traps
365a6ef4393SDouglas Raillard	 * that are unhandled at lower ELs most commonly. SP_EL3 should point
366a6ef4393SDouglas Raillard	 * to a valid cpu context where the general purpose and system register
367a6ef4393SDouglas Raillard	 * state can be saved.
368caa84939SJeenu Viswambharan	 */
3693b8456bdSManish V Badarkhe	apply_at_speculative_wa
37014c6016aSJeenu Viswambharan	check_and_unmask_ea
371caa84939SJeenu Viswambharan	handle_sync_exception
372a9203edaSRoberto Vargasend_vector_entry sync_exception_aarch64
3734f6ad66aSAchin Gupta
374e0ae9fabSSandrine Bailleuxvector_entry irq_aarch64
3753b8456bdSManish V Badarkhe	apply_at_speculative_wa
37614c6016aSJeenu Viswambharan	check_and_unmask_ea
377dce74b89SAchin Gupta	handle_interrupt_exception irq_aarch64
378a9203edaSRoberto Vargasend_vector_entry irq_aarch64
3794f6ad66aSAchin Gupta
380e0ae9fabSSandrine Bailleuxvector_entry fiq_aarch64
3813b8456bdSManish V Badarkhe	apply_at_speculative_wa
38214c6016aSJeenu Viswambharan	check_and_unmask_ea
383dce74b89SAchin Gupta	handle_interrupt_exception fiq_aarch64
384a9203edaSRoberto Vargasend_vector_entry fiq_aarch64
3854f6ad66aSAchin Gupta
386e0ae9fabSSandrine Bailleuxvector_entry serror_aarch64
3873b8456bdSManish V Badarkhe	apply_at_speculative_wa
388c2d32a5fSMadhukar Pappireddy#if RAS_EXTENSION
38976454abfSJeenu Viswambharan	msr	daifclr, #DAIF_ABT_BIT
390df8f3188SJeenu Viswambharan	b	enter_lower_el_async_ea
391c2d32a5fSMadhukar Pappireddy#else
392c2d32a5fSMadhukar Pappireddy	handle_async_ea
393c2d32a5fSMadhukar Pappireddy#endif
394a9203edaSRoberto Vargasend_vector_entry serror_aarch64
3954f6ad66aSAchin Gupta
396a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
39744804252SSandrine Bailleux	 * Lower EL using AArch32 : 0x600 - 0x800
398a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
3994f6ad66aSAchin Gupta	 */
400e0ae9fabSSandrine Bailleuxvector_entry sync_exception_aarch32
401a6ef4393SDouglas Raillard	/*
402a6ef4393SDouglas Raillard	 * This exception vector will be the entry point for SMCs and traps
403a6ef4393SDouglas Raillard	 * that are unhandled at lower ELs most commonly. SP_EL3 should point
404a6ef4393SDouglas Raillard	 * to a valid cpu context where the general purpose and system register
405a6ef4393SDouglas Raillard	 * state can be saved.
406caa84939SJeenu Viswambharan	 */
4073b8456bdSManish V Badarkhe	apply_at_speculative_wa
40814c6016aSJeenu Viswambharan	check_and_unmask_ea
409caa84939SJeenu Viswambharan	handle_sync_exception
410a9203edaSRoberto Vargasend_vector_entry sync_exception_aarch32
4114f6ad66aSAchin Gupta
412e0ae9fabSSandrine Bailleuxvector_entry irq_aarch32
4133b8456bdSManish V Badarkhe	apply_at_speculative_wa
41414c6016aSJeenu Viswambharan	check_and_unmask_ea
415dce74b89SAchin Gupta	handle_interrupt_exception irq_aarch32
416a9203edaSRoberto Vargasend_vector_entry irq_aarch32
4174f6ad66aSAchin Gupta
418e0ae9fabSSandrine Bailleuxvector_entry fiq_aarch32
4193b8456bdSManish V Badarkhe	apply_at_speculative_wa
42014c6016aSJeenu Viswambharan	check_and_unmask_ea
421dce74b89SAchin Gupta	handle_interrupt_exception fiq_aarch32
422a9203edaSRoberto Vargasend_vector_entry fiq_aarch32
4234f6ad66aSAchin Gupta
424e0ae9fabSSandrine Bailleuxvector_entry serror_aarch32
4253b8456bdSManish V Badarkhe	apply_at_speculative_wa
426c2d32a5fSMadhukar Pappireddy#if RAS_EXTENSION
42776454abfSJeenu Viswambharan	msr	daifclr, #DAIF_ABT_BIT
428df8f3188SJeenu Viswambharan	b	enter_lower_el_async_ea
429c2d32a5fSMadhukar Pappireddy#else
430c2d32a5fSMadhukar Pappireddy	handle_async_ea
431c2d32a5fSMadhukar Pappireddy#endif
432a9203edaSRoberto Vargasend_vector_entry serror_aarch32
433a7934d69SJeenu Viswambharan
4341f461979SJustin Chadwell#ifdef MONITOR_TRAPS
4351f461979SJustin Chadwell	.section .rodata.brk_string, "aS"
4361f461979SJustin Chadwellbrk_location:
4371f461979SJustin Chadwell	.asciz "Error at instruction 0x"
4381f461979SJustin Chadwellbrk_message:
4391f461979SJustin Chadwell	.asciz "Unexpected BRK instruction with value 0x"
4401f461979SJustin Chadwell#endif /* MONITOR_TRAPS */
4411f461979SJustin Chadwell
4422f370465SAntonio Nino Diaz	/* ---------------------------------------------------------------------
443caa84939SJeenu Viswambharan	 * The following code handles secure monitor calls.
444a6ef4393SDouglas Raillard	 * Depending upon the execution state from where the SMC has been
445a6ef4393SDouglas Raillard	 * invoked, it frees some general purpose registers to perform the
446a6ef4393SDouglas Raillard	 * remaining tasks. They involve finding the runtime service handler
447a6ef4393SDouglas Raillard	 * that is the target of the SMC & switching to runtime stacks (SP_EL0)
448a6ef4393SDouglas Raillard	 * before calling the handler.
449caa84939SJeenu Viswambharan	 *
450a6ef4393SDouglas Raillard	 * Note that x30 has been explicitly saved and can be used here
451a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
452caa84939SJeenu Viswambharan	 */
4530a30cf54SAndrew Thoelkefunc smc_handler
454caa84939SJeenu Viswambharansmc_handler32:
455caa84939SJeenu Viswambharan	/* Check whether aarch32 issued an SMC64 */
456caa84939SJeenu Viswambharan	tbnz	x0, #FUNCID_CC_SHIFT, smc_prohibited
457caa84939SJeenu Viswambharan
458caa84939SJeenu Viswambharansmc_handler64:
4595283962eSAntonio Nino Diaz	/* NOTE: The code below must preserve x0-x4 */
4605283962eSAntonio Nino Diaz
461e290a8fcSAlexei Fedorov	/*
462ed108b56SAlexei Fedorov	 * Save general purpose and ARMv8.3-PAuth registers (if enabled).
463ed108b56SAlexei Fedorov	 * If Secure Cycle Counter is not disabled in MDCR_EL3 when
464ed108b56SAlexei Fedorov	 * ARMv8.5-PMU is implemented, save PMCR_EL0 and disable Cycle Counter.
465e290a8fcSAlexei Fedorov	 */
466ed108b56SAlexei Fedorov	bl	save_gp_pmcr_pauth_regs
467e290a8fcSAlexei Fedorov
468b86048c4SAntonio Nino Diaz#if ENABLE_PAUTH
469ed108b56SAlexei Fedorov	/* Load and program APIAKey firmware key */
470ed108b56SAlexei Fedorov	bl	pauth_load_bl31_apiakey
471b86048c4SAntonio Nino Diaz#endif
4725283962eSAntonio Nino Diaz
473a6ef4393SDouglas Raillard	/*
474a6ef4393SDouglas Raillard	 * Populate the parameters for the SMC handler.
475a6ef4393SDouglas Raillard	 * We already have x0-x4 in place. x5 will point to a cookie (not used
476a6ef4393SDouglas Raillard	 * now). x6 will point to the context structure (SP_EL3) and x7 will
477201ca5b6SDimitris Papastamos	 * contain flags we need to pass to the handler.
478caa84939SJeenu Viswambharan	 */
479caa84939SJeenu Viswambharan	mov	x5, xzr
480caa84939SJeenu Viswambharan	mov	x6, sp
481caa84939SJeenu Viswambharan
482a6ef4393SDouglas Raillard	/*
483a6ef4393SDouglas Raillard	 * Restore the saved C runtime stack value which will become the new
484a6ef4393SDouglas Raillard	 * SP_EL0 i.e. EL3 runtime stack. It was saved in the 'cpu_context'
485a6ef4393SDouglas Raillard	 * structure prior to the last ERET from EL3.
486caa84939SJeenu Viswambharan	 */
487caa84939SJeenu Viswambharan	ldr	x12, [x6, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
488caa84939SJeenu Viswambharan
489caa84939SJeenu Viswambharan	/* Switch to SP_EL0 */
490ed108b56SAlexei Fedorov	msr	spsel, #MODE_SP_EL0
491caa84939SJeenu Viswambharan
492a6ef4393SDouglas Raillard	/*
493a6ef4393SDouglas Raillard	 * Save the SPSR_EL3, ELR_EL3, & SCR_EL3 in case there is a world
494a6ef4393SDouglas Raillard	 * switch during SMC handling.
495a6ef4393SDouglas Raillard	 * TODO: Revisit if all system registers can be saved later.
496caa84939SJeenu Viswambharan	 */
497caa84939SJeenu Viswambharan	mrs	x16, spsr_el3
498caa84939SJeenu Viswambharan	mrs	x17, elr_el3
499caa84939SJeenu Viswambharan	mrs	x18, scr_el3
500caa84939SJeenu Viswambharan	stp	x16, x17, [x6, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
501b51da821SAchin Gupta	str	x18, [x6, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3]
502caa84939SJeenu Viswambharan
503*4693ff72SZelalem Aweke	/* Clear flag register */
504*4693ff72SZelalem Aweke	mov	x7, xzr
505*4693ff72SZelalem Aweke
506*4693ff72SZelalem Aweke#if ENABLE_RME
507*4693ff72SZelalem Aweke	/* Copy SCR_EL3.NSE bit to the flag to indicate caller's security */
508*4693ff72SZelalem Aweke	ubfx	x7, x18, #SCR_NSE_SHIFT, 1
509*4693ff72SZelalem Aweke
510*4693ff72SZelalem Aweke	/*
511*4693ff72SZelalem Aweke	 * Shift copied SCR_EL3.NSE bit by 5 to create space for
512*4693ff72SZelalem Aweke	 * SCR_EL3.NS bit. Bit 5 of the flag correspondes to
513*4693ff72SZelalem Aweke	 * the SCR_EL3.NSE bit.
514*4693ff72SZelalem Aweke	 */
515*4693ff72SZelalem Aweke	lsl	x7, x7, #5
516*4693ff72SZelalem Aweke#endif /* ENABLE_RME */
517*4693ff72SZelalem Aweke
518caa84939SJeenu Viswambharan	/* Copy SCR_EL3.NS bit to the flag to indicate caller's security */
519caa84939SJeenu Viswambharan	bfi	x7, x18, #0, #1
520caa84939SJeenu Viswambharan
521caa84939SJeenu Viswambharan	mov	sp, x12
522caa84939SJeenu Viswambharan
523cc485e27SMadhukar Pappireddy	/* Get the unique owning entity number */
524cc485e27SMadhukar Pappireddy	ubfx	x16, x0, #FUNCID_OEN_SHIFT, #FUNCID_OEN_WIDTH
525cc485e27SMadhukar Pappireddy	ubfx	x15, x0, #FUNCID_TYPE_SHIFT, #FUNCID_TYPE_WIDTH
526cc485e27SMadhukar Pappireddy	orr	x16, x16, x15, lsl #FUNCID_OEN_WIDTH
527cc485e27SMadhukar Pappireddy
528cc485e27SMadhukar Pappireddy	/* Load descriptor index from array of indices */
529c367b75eSMadhukar Pappireddy	adrp	x14, rt_svc_descs_indices
530c367b75eSMadhukar Pappireddy	add	x14, x14, :lo12:rt_svc_descs_indices
531cc485e27SMadhukar Pappireddy	ldrb	w15, [x14, x16]
532cc485e27SMadhukar Pappireddy
533cc485e27SMadhukar Pappireddy	/* Any index greater than 127 is invalid. Check bit 7. */
534cc485e27SMadhukar Pappireddy	tbnz	w15, 7, smc_unknown
535cc485e27SMadhukar Pappireddy
536cc485e27SMadhukar Pappireddy	/*
537cc485e27SMadhukar Pappireddy	 * Get the descriptor using the index
538cc485e27SMadhukar Pappireddy	 * x11 = (base + off), w15 = index
539cc485e27SMadhukar Pappireddy	 *
540cc485e27SMadhukar Pappireddy	 * handler = (base + off) + (index << log2(size))
541cc485e27SMadhukar Pappireddy	 */
542cc485e27SMadhukar Pappireddy	adr	x11, (__RT_SVC_DESCS_START__ + RT_SVC_DESC_HANDLE)
543cc485e27SMadhukar Pappireddy	lsl	w10, w15, #RT_SVC_SIZE_LOG2
544cc485e27SMadhukar Pappireddy	ldr	x15, [x11, w10, uxtw]
545cc485e27SMadhukar Pappireddy
546a6ef4393SDouglas Raillard	/*
547a6ef4393SDouglas Raillard	 * Call the Secure Monitor Call handler and then drop directly into
548a6ef4393SDouglas Raillard	 * el3_exit() which will program any remaining architectural state
549a6ef4393SDouglas Raillard	 * prior to issuing the ERET to the desired lower EL.
550caa84939SJeenu Viswambharan	 */
551caa84939SJeenu Viswambharan#if DEBUG
552caa84939SJeenu Viswambharan	cbz	x15, rt_svc_fw_critical_error
553caa84939SJeenu Viswambharan#endif
554caa84939SJeenu Viswambharan	blr	x15
555caa84939SJeenu Viswambharan
556bbf8f6f9SYatharth Kochar	b	el3_exit
5574f6ad66aSAchin Gupta
558caa84939SJeenu Viswambharansmc_unknown:
559caa84939SJeenu Viswambharan	/*
560cc485e27SMadhukar Pappireddy	 * Unknown SMC call. Populate return value with SMC_UNK and call
561cc485e27SMadhukar Pappireddy	 * el3_exit() which will restore the remaining architectural state
562cc485e27SMadhukar Pappireddy	 * i.e., SYS, GP and PAuth registers(if any) prior to issuing the ERET
563cc485e27SMadhukar Pappireddy         * to the desired lower EL.
564caa84939SJeenu Viswambharan	 */
5654abd7fa7SAntonio Nino Diaz	mov	x0, #SMC_UNK
566cc485e27SMadhukar Pappireddy	str	x0, [x6, #CTX_GPREGS_OFFSET + CTX_GPREG_X0]
567cc485e27SMadhukar Pappireddy	b	el3_exit
568caa84939SJeenu Viswambharan
569caa84939SJeenu Viswambharansmc_prohibited:
5703b8456bdSManish V Badarkhe	restore_ptw_el1_sys_regs
5713b8456bdSManish V Badarkhe	ldp	x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28]
572c3260f9bSSoby Mathew	ldr	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
5734abd7fa7SAntonio Nino Diaz	mov	x0, #SMC_UNK
574f461fe34SAnthony Steinhauser	exception_return
575caa84939SJeenu Viswambharan
576ed108b56SAlexei Fedorov#if DEBUG
577caa84939SJeenu Viswambharanrt_svc_fw_critical_error:
578a6ef4393SDouglas Raillard	/* Switch to SP_ELx */
579ed108b56SAlexei Fedorov	msr	spsel, #MODE_SP_ELX
580a806dad5SJeenu Viswambharan	no_ret	report_unhandled_exception
581ed108b56SAlexei Fedorov#endif
5828b779620SKévin Petitendfunc smc_handler
5831f461979SJustin Chadwell
5841f461979SJustin Chadwell	/* ---------------------------------------------------------------------
5851f461979SJustin Chadwell	 * The following code handles exceptions caused by BRK instructions.
5861f461979SJustin Chadwell	 * Following a BRK instruction, the only real valid cause of action is
5871f461979SJustin Chadwell	 * to print some information and panic, as the code that caused it is
5881f461979SJustin Chadwell	 * likely in an inconsistent internal state.
5891f461979SJustin Chadwell	 *
5901f461979SJustin Chadwell	 * This is initially intended to be used in conjunction with
5911f461979SJustin Chadwell	 * __builtin_trap.
5921f461979SJustin Chadwell	 * ---------------------------------------------------------------------
5931f461979SJustin Chadwell	 */
5941f461979SJustin Chadwell#ifdef MONITOR_TRAPS
5951f461979SJustin Chadwellfunc brk_handler
5961f461979SJustin Chadwell	/* Extract the ISS */
5971f461979SJustin Chadwell	mrs	x10, esr_el3
5981f461979SJustin Chadwell	ubfx	x10, x10, #ESR_ISS_SHIFT, #ESR_ISS_LENGTH
5991f461979SJustin Chadwell
6001f461979SJustin Chadwell	/* Ensure the console is initialized */
6011f461979SJustin Chadwell	bl	plat_crash_console_init
6021f461979SJustin Chadwell
6031f461979SJustin Chadwell	adr	x4, brk_location
6041f461979SJustin Chadwell	bl	asm_print_str
6051f461979SJustin Chadwell	mrs	x4, elr_el3
6061f461979SJustin Chadwell	bl	asm_print_hex
6071f461979SJustin Chadwell	bl	asm_print_newline
6081f461979SJustin Chadwell
6091f461979SJustin Chadwell	adr	x4, brk_message
6101f461979SJustin Chadwell	bl	asm_print_str
6111f461979SJustin Chadwell	mov	x4, x10
6121f461979SJustin Chadwell	mov	x5, #28
6131f461979SJustin Chadwell	bl	asm_print_hex_bits
6141f461979SJustin Chadwell	bl	asm_print_newline
6151f461979SJustin Chadwell
6161f461979SJustin Chadwell	no_ret	plat_panic_handler
6171f461979SJustin Chadwellendfunc brk_handler
6181f461979SJustin Chadwell#endif /* MONITOR_TRAPS */
619