xref: /rk3399_ARM-atf/bl31/aarch64/runtime_exceptions.S (revision 3b8456bd1c9fd2303483f0675786e3fbda81a0af)
14f6ad66aSAchin Gupta/*
2c367b75eSMadhukar Pappireddy * Copyright (c) 2013-2020, 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>
15*3b8456bdSManish 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	 *
4814c6016aSJeenu Viswambharan	 * Without RAS_EXTENSION, this macro just saves x30, and unmasks
4914c6016aSJeenu Viswambharan	 * Asynchronous External Aborts.
5014c6016aSJeenu Viswambharan	 */
5114c6016aSJeenu Viswambharan	.macro check_and_unmask_ea
5214c6016aSJeenu Viswambharan#if RAS_EXTENSION
5314c6016aSJeenu Viswambharan	/* Synchronize pending External Aborts */
5414c6016aSJeenu Viswambharan	esb
5514c6016aSJeenu Viswambharan
5614c6016aSJeenu Viswambharan	/* Unmask the SError interrupt */
5714c6016aSJeenu Viswambharan	msr	daifclr, #DAIF_ABT_BIT
5814c6016aSJeenu Viswambharan
5914c6016aSJeenu Viswambharan	/*
6014c6016aSJeenu Viswambharan	 * Explicitly save x30 so as to free up a register and to enable
6114c6016aSJeenu Viswambharan	 * branching
6214c6016aSJeenu Viswambharan	 */
6314c6016aSJeenu Viswambharan	str	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
6414c6016aSJeenu Viswambharan
6514c6016aSJeenu Viswambharan	/* Check for SErrors synchronized by the ESB instruction */
6614c6016aSJeenu Viswambharan	mrs	x30, DISR_EL1
6714c6016aSJeenu Viswambharan	tbz	x30, #DISR_A_BIT, 1f
6814c6016aSJeenu Viswambharan
69e290a8fcSAlexei Fedorov	/*
70ed108b56SAlexei Fedorov	 * Save general purpose and ARMv8.3-PAuth registers (if enabled).
71ed108b56SAlexei Fedorov	 * If Secure Cycle Counter is not disabled in MDCR_EL3 when
72ed108b56SAlexei Fedorov	 * ARMv8.5-PMU is implemented, save PMCR_EL0 and disable Cycle Counter.
73e290a8fcSAlexei Fedorov	 */
74ed108b56SAlexei Fedorov	bl	save_gp_pmcr_pauth_regs
75e290a8fcSAlexei Fedorov
76df8f3188SJeenu Viswambharan	bl	handle_lower_el_ea_esb
7714c6016aSJeenu Viswambharan
78ed108b56SAlexei Fedorov	/* Restore general purpose, PMCR_EL0 and ARMv8.3-PAuth registers */
79ed108b56SAlexei Fedorov	bl	restore_gp_pmcr_pauth_regs
8014c6016aSJeenu Viswambharan1:
8114c6016aSJeenu Viswambharan#else
8214c6016aSJeenu Viswambharan	/* Unmask the SError interrupt */
8314c6016aSJeenu Viswambharan	msr	daifclr, #DAIF_ABT_BIT
8414c6016aSJeenu Viswambharan
8514c6016aSJeenu Viswambharan	str	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
8614c6016aSJeenu Viswambharan#endif
8714c6016aSJeenu Viswambharan	.endm
8814c6016aSJeenu Viswambharan
89a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
90a6ef4393SDouglas Raillard	 * This macro handles Synchronous exceptions.
91a6ef4393SDouglas Raillard	 * Only SMC exceptions are supported.
92a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
93dce74b89SAchin Gupta	 */
94dce74b89SAchin Gupta	.macro	handle_sync_exception
95872be88aSdp-arm#if ENABLE_RUNTIME_INSTRUMENTATION
96872be88aSdp-arm	/*
97a6ef4393SDouglas Raillard	 * Read the timestamp value and store it in per-cpu data. The value
98a6ef4393SDouglas Raillard	 * will be extracted from per-cpu data by the C level SMC handler and
99a6ef4393SDouglas Raillard	 * saved to the PMF timestamp region.
100872be88aSdp-arm	 */
101872be88aSdp-arm	mrs	x30, cntpct_el0
102872be88aSdp-arm	str	x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29]
103872be88aSdp-arm	mrs	x29, tpidr_el3
104872be88aSdp-arm	str	x30, [x29, #CPU_DATA_PMF_TS0_OFFSET]
105872be88aSdp-arm	ldr	x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29]
106872be88aSdp-arm#endif
107872be88aSdp-arm
108dce74b89SAchin Gupta	mrs	x30, esr_el3
109dce74b89SAchin Gupta	ubfx	x30, x30, #ESR_EC_SHIFT, #ESR_EC_LENGTH
110dce74b89SAchin Gupta
111a6ef4393SDouglas Raillard	/* Handle SMC exceptions separately from other synchronous exceptions */
112dce74b89SAchin Gupta	cmp	x30, #EC_AARCH32_SMC
113dce74b89SAchin Gupta	b.eq	smc_handler32
114dce74b89SAchin Gupta
115dce74b89SAchin Gupta	cmp	x30, #EC_AARCH64_SMC
116dce74b89SAchin Gupta	b.eq	smc_handler64
117dce74b89SAchin Gupta
118df8f3188SJeenu Viswambharan	/* Synchronous exceptions other than the above are assumed to be EA */
1194d91838bSJulius Werner	ldr	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
120df8f3188SJeenu Viswambharan	b	enter_lower_el_sync_ea
121dce74b89SAchin Gupta	.endm
122dce74b89SAchin Gupta
123dce74b89SAchin Gupta
124a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
125a6ef4393SDouglas Raillard	 * This macro handles FIQ or IRQ interrupts i.e. EL3, S-EL1 and NS
126a6ef4393SDouglas Raillard	 * interrupts.
127a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
128dce74b89SAchin Gupta	 */
129dce74b89SAchin Gupta	.macro	handle_interrupt_exception label
1305283962eSAntonio Nino Diaz
131e290a8fcSAlexei Fedorov	/*
132ed108b56SAlexei Fedorov	 * Save general purpose and ARMv8.3-PAuth registers (if enabled).
133ed108b56SAlexei Fedorov	 * If Secure Cycle Counter is not disabled in MDCR_EL3 when
134ed108b56SAlexei Fedorov	 * ARMv8.5-PMU is implemented, save PMCR_EL0 and disable Cycle Counter.
135e290a8fcSAlexei Fedorov	 */
136ed108b56SAlexei Fedorov	bl	save_gp_pmcr_pauth_regs
137e290a8fcSAlexei Fedorov
138b86048c4SAntonio Nino Diaz#if ENABLE_PAUTH
139ed108b56SAlexei Fedorov	/* Load and program APIAKey firmware key */
140ed108b56SAlexei Fedorov	bl	pauth_load_bl31_apiakey
141b86048c4SAntonio Nino Diaz#endif
1425283962eSAntonio Nino Diaz
143a6ef4393SDouglas Raillard	/* Save the EL3 system registers needed to return from this exception */
1445717aae1SAchin Gupta	mrs	x0, spsr_el3
1455717aae1SAchin Gupta	mrs	x1, elr_el3
1465717aae1SAchin Gupta	stp	x0, x1, [sp, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
1475717aae1SAchin Gupta
148dce74b89SAchin Gupta	/* Switch to the runtime stack i.e. SP_EL0 */
149dce74b89SAchin Gupta	ldr	x2, [sp, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
150dce74b89SAchin Gupta	mov	x20, sp
151ed108b56SAlexei Fedorov	msr	spsel, #MODE_SP_EL0
152dce74b89SAchin Gupta	mov	sp, x2
153dce74b89SAchin Gupta
154dce74b89SAchin Gupta	/*
155a6ef4393SDouglas Raillard	 * Find out whether this is a valid interrupt type.
156a6ef4393SDouglas Raillard	 * If the interrupt controller reports a spurious interrupt then return
157a6ef4393SDouglas Raillard	 * to where we came from.
158dce74b89SAchin Gupta	 */
1599865ac15SDan Handley	bl	plat_ic_get_pending_interrupt_type
160dce74b89SAchin Gupta	cmp	x0, #INTR_TYPE_INVAL
161dce74b89SAchin Gupta	b.eq	interrupt_exit_\label
162dce74b89SAchin Gupta
163dce74b89SAchin Gupta	/*
164a6ef4393SDouglas Raillard	 * Get the registered handler for this interrupt type.
165a6ef4393SDouglas Raillard	 * A NULL return value could be 'cause of the following conditions:
1665717aae1SAchin Gupta	 *
167a6ef4393SDouglas Raillard	 * a. An interrupt of a type was routed correctly but a handler for its
168a6ef4393SDouglas Raillard	 *    type was not registered.
1695717aae1SAchin Gupta	 *
170a6ef4393SDouglas Raillard	 * b. An interrupt of a type was not routed correctly so a handler for
171a6ef4393SDouglas Raillard	 *    its type was not registered.
1725717aae1SAchin Gupta	 *
173a6ef4393SDouglas Raillard	 * c. An interrupt of a type was routed correctly to EL3, but was
174a6ef4393SDouglas Raillard	 *    deasserted before its pending state could be read. Another
175a6ef4393SDouglas Raillard	 *    interrupt of a different type pended at the same time and its
176a6ef4393SDouglas Raillard	 *    type was reported as pending instead. However, a handler for this
177a6ef4393SDouglas Raillard	 *    type was not registered.
1785717aae1SAchin Gupta	 *
179a6ef4393SDouglas Raillard	 * a. and b. can only happen due to a programming error. The
180a6ef4393SDouglas Raillard	 * occurrence of c. could be beyond the control of Trusted Firmware.
181a6ef4393SDouglas Raillard	 * It makes sense to return from this exception instead of reporting an
182a6ef4393SDouglas Raillard	 * error.
183dce74b89SAchin Gupta	 */
184dce74b89SAchin Gupta	bl	get_interrupt_type_handler
1855717aae1SAchin Gupta	cbz	x0, interrupt_exit_\label
186dce74b89SAchin Gupta	mov	x21, x0
187dce74b89SAchin Gupta
188dce74b89SAchin Gupta	mov	x0, #INTR_ID_UNAVAILABLE
189dce74b89SAchin Gupta
190dce74b89SAchin Gupta	/* Set the current security state in the 'flags' parameter */
191dce74b89SAchin Gupta	mrs	x2, scr_el3
192dce74b89SAchin Gupta	ubfx	x1, x2, #0, #1
193dce74b89SAchin Gupta
194dce74b89SAchin Gupta	/* Restore the reference to the 'handle' i.e. SP_EL3 */
195dce74b89SAchin Gupta	mov	x2, x20
196dce74b89SAchin Gupta
197b460b8bfSSoby Mathew	/* x3 will point to a cookie (not used now) */
198b460b8bfSSoby Mathew	mov	x3, xzr
199b460b8bfSSoby Mathew
200dce74b89SAchin Gupta	/* Call the interrupt type handler */
201dce74b89SAchin Gupta	blr	x21
202dce74b89SAchin Gupta
203dce74b89SAchin Guptainterrupt_exit_\label:
204dce74b89SAchin Gupta	/* Return from exception, possibly in a different security state */
205dce74b89SAchin Gupta	b	el3_exit
206dce74b89SAchin Gupta
207dce74b89SAchin Gupta	.endm
208dce74b89SAchin Gupta
209dce74b89SAchin Gupta
210e0ae9fabSSandrine Bailleuxvector_base runtime_exceptions
211e0ae9fabSSandrine Bailleux
212a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
213a6ef4393SDouglas Raillard	 * Current EL with SP_EL0 : 0x0 - 0x200
214a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
2154f6ad66aSAchin Gupta	 */
216e0ae9fabSSandrine Bailleuxvector_entry sync_exception_sp_el0
2171f461979SJustin Chadwell#ifdef MONITOR_TRAPS
2181f461979SJustin Chadwell	stp x29, x30, [sp, #-16]!
2191f461979SJustin Chadwell
2201f461979SJustin Chadwell	mrs	x30, esr_el3
2211f461979SJustin Chadwell	ubfx	x30, x30, #ESR_EC_SHIFT, #ESR_EC_LENGTH
2221f461979SJustin Chadwell
2231f461979SJustin Chadwell	/* Check for BRK */
2241f461979SJustin Chadwell	cmp	x30, #EC_BRK
2251f461979SJustin Chadwell	b.eq	brk_handler
2261f461979SJustin Chadwell
2271f461979SJustin Chadwell	ldp x29, x30, [sp], #16
2281f461979SJustin Chadwell#endif /* MONITOR_TRAPS */
2291f461979SJustin Chadwell
230a6ef4393SDouglas Raillard	/* We don't expect any synchronous exceptions from EL3 */
2314d91838bSJulius Werner	b	report_unhandled_exception
232a9203edaSRoberto Vargasend_vector_entry sync_exception_sp_el0
2334f6ad66aSAchin Gupta
234e0ae9fabSSandrine Bailleuxvector_entry irq_sp_el0
235a6ef4393SDouglas Raillard	/*
236a6ef4393SDouglas Raillard	 * EL3 code is non-reentrant. Any asynchronous exception is a serious
237a6ef4393SDouglas Raillard	 * error. Loop infinitely.
238a6ef4393SDouglas Raillard	 */
2394d91838bSJulius Werner	b	report_unhandled_interrupt
240a9203edaSRoberto Vargasend_vector_entry irq_sp_el0
2414f6ad66aSAchin Gupta
242e0ae9fabSSandrine Bailleux
243e0ae9fabSSandrine Bailleuxvector_entry fiq_sp_el0
2444d91838bSJulius Werner	b	report_unhandled_interrupt
245a9203edaSRoberto Vargasend_vector_entry fiq_sp_el0
2464f6ad66aSAchin Gupta
247e0ae9fabSSandrine Bailleux
248e0ae9fabSSandrine Bailleuxvector_entry serror_sp_el0
249eaeaa4d0SJeenu Viswambharan	no_ret	plat_handle_el3_ea
250a9203edaSRoberto Vargasend_vector_entry serror_sp_el0
2514f6ad66aSAchin Gupta
252a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
253a6ef4393SDouglas Raillard	 * Current EL with SP_ELx: 0x200 - 0x400
254a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
2554f6ad66aSAchin Gupta	 */
256e0ae9fabSSandrine Bailleuxvector_entry sync_exception_sp_elx
257a6ef4393SDouglas Raillard	/*
258a6ef4393SDouglas Raillard	 * This exception will trigger if anything went wrong during a previous
259a6ef4393SDouglas Raillard	 * exception entry or exit or while handling an earlier unexpected
260a6ef4393SDouglas Raillard	 * synchronous exception. There is a high probability that SP_EL3 is
261a6ef4393SDouglas Raillard	 * corrupted.
262caa84939SJeenu Viswambharan	 */
2634d91838bSJulius Werner	b	report_unhandled_exception
264a9203edaSRoberto Vargasend_vector_entry sync_exception_sp_elx
2654f6ad66aSAchin Gupta
266e0ae9fabSSandrine Bailleuxvector_entry irq_sp_elx
2674d91838bSJulius Werner	b	report_unhandled_interrupt
268a9203edaSRoberto Vargasend_vector_entry irq_sp_elx
269a7934d69SJeenu Viswambharan
270e0ae9fabSSandrine Bailleuxvector_entry fiq_sp_elx
2714d91838bSJulius Werner	b	report_unhandled_interrupt
272a9203edaSRoberto Vargasend_vector_entry fiq_sp_elx
273a7934d69SJeenu Viswambharan
274e0ae9fabSSandrine Bailleuxvector_entry serror_sp_elx
275eaeaa4d0SJeenu Viswambharan	no_ret	plat_handle_el3_ea
276a9203edaSRoberto Vargasend_vector_entry serror_sp_elx
2774f6ad66aSAchin Gupta
278a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
27944804252SSandrine Bailleux	 * Lower EL using AArch64 : 0x400 - 0x600
280a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
2814f6ad66aSAchin Gupta	 */
282e0ae9fabSSandrine Bailleuxvector_entry sync_exception_aarch64
283a6ef4393SDouglas Raillard	/*
284a6ef4393SDouglas Raillard	 * This exception vector will be the entry point for SMCs and traps
285a6ef4393SDouglas Raillard	 * that are unhandled at lower ELs most commonly. SP_EL3 should point
286a6ef4393SDouglas Raillard	 * to a valid cpu context where the general purpose and system register
287a6ef4393SDouglas Raillard	 * state can be saved.
288caa84939SJeenu Viswambharan	 */
289*3b8456bdSManish V Badarkhe	apply_at_speculative_wa
29014c6016aSJeenu Viswambharan	check_and_unmask_ea
291caa84939SJeenu Viswambharan	handle_sync_exception
292a9203edaSRoberto Vargasend_vector_entry sync_exception_aarch64
2934f6ad66aSAchin Gupta
294e0ae9fabSSandrine Bailleuxvector_entry irq_aarch64
295*3b8456bdSManish V Badarkhe	apply_at_speculative_wa
29614c6016aSJeenu Viswambharan	check_and_unmask_ea
297dce74b89SAchin Gupta	handle_interrupt_exception irq_aarch64
298a9203edaSRoberto Vargasend_vector_entry irq_aarch64
2994f6ad66aSAchin Gupta
300e0ae9fabSSandrine Bailleuxvector_entry fiq_aarch64
301*3b8456bdSManish V Badarkhe	apply_at_speculative_wa
30214c6016aSJeenu Viswambharan	check_and_unmask_ea
303dce74b89SAchin Gupta	handle_interrupt_exception fiq_aarch64
304a9203edaSRoberto Vargasend_vector_entry fiq_aarch64
3054f6ad66aSAchin Gupta
306e0ae9fabSSandrine Bailleuxvector_entry serror_aarch64
307*3b8456bdSManish V Badarkhe	apply_at_speculative_wa
30876454abfSJeenu Viswambharan	msr	daifclr, #DAIF_ABT_BIT
309df8f3188SJeenu Viswambharan	b	enter_lower_el_async_ea
310a9203edaSRoberto Vargasend_vector_entry serror_aarch64
3114f6ad66aSAchin Gupta
312a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
31344804252SSandrine Bailleux	 * Lower EL using AArch32 : 0x600 - 0x800
314a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
3154f6ad66aSAchin Gupta	 */
316e0ae9fabSSandrine Bailleuxvector_entry sync_exception_aarch32
317a6ef4393SDouglas Raillard	/*
318a6ef4393SDouglas Raillard	 * This exception vector will be the entry point for SMCs and traps
319a6ef4393SDouglas Raillard	 * that are unhandled at lower ELs most commonly. SP_EL3 should point
320a6ef4393SDouglas Raillard	 * to a valid cpu context where the general purpose and system register
321a6ef4393SDouglas Raillard	 * state can be saved.
322caa84939SJeenu Viswambharan	 */
323*3b8456bdSManish V Badarkhe	apply_at_speculative_wa
32414c6016aSJeenu Viswambharan	check_and_unmask_ea
325caa84939SJeenu Viswambharan	handle_sync_exception
326a9203edaSRoberto Vargasend_vector_entry sync_exception_aarch32
3274f6ad66aSAchin Gupta
328e0ae9fabSSandrine Bailleuxvector_entry irq_aarch32
329*3b8456bdSManish V Badarkhe	apply_at_speculative_wa
33014c6016aSJeenu Viswambharan	check_and_unmask_ea
331dce74b89SAchin Gupta	handle_interrupt_exception irq_aarch32
332a9203edaSRoberto Vargasend_vector_entry irq_aarch32
3334f6ad66aSAchin Gupta
334e0ae9fabSSandrine Bailleuxvector_entry fiq_aarch32
335*3b8456bdSManish V Badarkhe	apply_at_speculative_wa
33614c6016aSJeenu Viswambharan	check_and_unmask_ea
337dce74b89SAchin Gupta	handle_interrupt_exception fiq_aarch32
338a9203edaSRoberto Vargasend_vector_entry fiq_aarch32
3394f6ad66aSAchin Gupta
340e0ae9fabSSandrine Bailleuxvector_entry serror_aarch32
341*3b8456bdSManish V Badarkhe	apply_at_speculative_wa
34276454abfSJeenu Viswambharan	msr	daifclr, #DAIF_ABT_BIT
343df8f3188SJeenu Viswambharan	b	enter_lower_el_async_ea
344a9203edaSRoberto Vargasend_vector_entry serror_aarch32
345a7934d69SJeenu Viswambharan
3461f461979SJustin Chadwell#ifdef MONITOR_TRAPS
3471f461979SJustin Chadwell	.section .rodata.brk_string, "aS"
3481f461979SJustin Chadwellbrk_location:
3491f461979SJustin Chadwell	.asciz "Error at instruction 0x"
3501f461979SJustin Chadwellbrk_message:
3511f461979SJustin Chadwell	.asciz "Unexpected BRK instruction with value 0x"
3521f461979SJustin Chadwell#endif /* MONITOR_TRAPS */
3531f461979SJustin Chadwell
3542f370465SAntonio Nino Diaz	/* ---------------------------------------------------------------------
355caa84939SJeenu Viswambharan	 * The following code handles secure monitor calls.
356a6ef4393SDouglas Raillard	 * Depending upon the execution state from where the SMC has been
357a6ef4393SDouglas Raillard	 * invoked, it frees some general purpose registers to perform the
358a6ef4393SDouglas Raillard	 * remaining tasks. They involve finding the runtime service handler
359a6ef4393SDouglas Raillard	 * that is the target of the SMC & switching to runtime stacks (SP_EL0)
360a6ef4393SDouglas Raillard	 * before calling the handler.
361caa84939SJeenu Viswambharan	 *
362a6ef4393SDouglas Raillard	 * Note that x30 has been explicitly saved and can be used here
363a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
364caa84939SJeenu Viswambharan	 */
3650a30cf54SAndrew Thoelkefunc smc_handler
366caa84939SJeenu Viswambharansmc_handler32:
367caa84939SJeenu Viswambharan	/* Check whether aarch32 issued an SMC64 */
368caa84939SJeenu Viswambharan	tbnz	x0, #FUNCID_CC_SHIFT, smc_prohibited
369caa84939SJeenu Viswambharan
370caa84939SJeenu Viswambharansmc_handler64:
3715283962eSAntonio Nino Diaz	/* NOTE: The code below must preserve x0-x4 */
3725283962eSAntonio Nino Diaz
373e290a8fcSAlexei Fedorov	/*
374ed108b56SAlexei Fedorov	 * Save general purpose and ARMv8.3-PAuth registers (if enabled).
375ed108b56SAlexei Fedorov	 * If Secure Cycle Counter is not disabled in MDCR_EL3 when
376ed108b56SAlexei Fedorov	 * ARMv8.5-PMU is implemented, save PMCR_EL0 and disable Cycle Counter.
377e290a8fcSAlexei Fedorov	 */
378ed108b56SAlexei Fedorov	bl	save_gp_pmcr_pauth_regs
379e290a8fcSAlexei Fedorov
380b86048c4SAntonio Nino Diaz#if ENABLE_PAUTH
381ed108b56SAlexei Fedorov	/* Load and program APIAKey firmware key */
382ed108b56SAlexei Fedorov	bl	pauth_load_bl31_apiakey
383b86048c4SAntonio Nino Diaz#endif
3845283962eSAntonio Nino Diaz
385a6ef4393SDouglas Raillard	/*
386a6ef4393SDouglas Raillard	 * Populate the parameters for the SMC handler.
387a6ef4393SDouglas Raillard	 * We already have x0-x4 in place. x5 will point to a cookie (not used
388a6ef4393SDouglas Raillard	 * now). x6 will point to the context structure (SP_EL3) and x7 will
389201ca5b6SDimitris Papastamos	 * contain flags we need to pass to the handler.
390caa84939SJeenu Viswambharan	 */
391caa84939SJeenu Viswambharan	mov	x5, xzr
392caa84939SJeenu Viswambharan	mov	x6, sp
393caa84939SJeenu Viswambharan
394a6ef4393SDouglas Raillard	/*
395a6ef4393SDouglas Raillard	 * Restore the saved C runtime stack value which will become the new
396a6ef4393SDouglas Raillard	 * SP_EL0 i.e. EL3 runtime stack. It was saved in the 'cpu_context'
397a6ef4393SDouglas Raillard	 * structure prior to the last ERET from EL3.
398caa84939SJeenu Viswambharan	 */
399caa84939SJeenu Viswambharan	ldr	x12, [x6, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
400caa84939SJeenu Viswambharan
401caa84939SJeenu Viswambharan	/* Switch to SP_EL0 */
402ed108b56SAlexei Fedorov	msr	spsel, #MODE_SP_EL0
403caa84939SJeenu Viswambharan
404a6ef4393SDouglas Raillard	/*
405a6ef4393SDouglas Raillard	 * Save the SPSR_EL3, ELR_EL3, & SCR_EL3 in case there is a world
406a6ef4393SDouglas Raillard	 * switch during SMC handling.
407a6ef4393SDouglas Raillard	 * TODO: Revisit if all system registers can be saved later.
408caa84939SJeenu Viswambharan	 */
409caa84939SJeenu Viswambharan	mrs	x16, spsr_el3
410caa84939SJeenu Viswambharan	mrs	x17, elr_el3
411caa84939SJeenu Viswambharan	mrs	x18, scr_el3
412caa84939SJeenu Viswambharan	stp	x16, x17, [x6, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
413b51da821SAchin Gupta	str	x18, [x6, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3]
414caa84939SJeenu Viswambharan
415caa84939SJeenu Viswambharan	/* Copy SCR_EL3.NS bit to the flag to indicate caller's security */
416caa84939SJeenu Viswambharan	bfi	x7, x18, #0, #1
417caa84939SJeenu Viswambharan
418caa84939SJeenu Viswambharan	mov	sp, x12
419caa84939SJeenu Viswambharan
420cc485e27SMadhukar Pappireddy	/* Get the unique owning entity number */
421cc485e27SMadhukar Pappireddy	ubfx	x16, x0, #FUNCID_OEN_SHIFT, #FUNCID_OEN_WIDTH
422cc485e27SMadhukar Pappireddy	ubfx	x15, x0, #FUNCID_TYPE_SHIFT, #FUNCID_TYPE_WIDTH
423cc485e27SMadhukar Pappireddy	orr	x16, x16, x15, lsl #FUNCID_OEN_WIDTH
424cc485e27SMadhukar Pappireddy
425cc485e27SMadhukar Pappireddy	/* Load descriptor index from array of indices */
426c367b75eSMadhukar Pappireddy	adrp	x14, rt_svc_descs_indices
427c367b75eSMadhukar Pappireddy	add	x14, x14, :lo12:rt_svc_descs_indices
428cc485e27SMadhukar Pappireddy	ldrb	w15, [x14, x16]
429cc485e27SMadhukar Pappireddy
430cc485e27SMadhukar Pappireddy	/* Any index greater than 127 is invalid. Check bit 7. */
431cc485e27SMadhukar Pappireddy	tbnz	w15, 7, smc_unknown
432cc485e27SMadhukar Pappireddy
433cc485e27SMadhukar Pappireddy	/*
434cc485e27SMadhukar Pappireddy	 * Get the descriptor using the index
435cc485e27SMadhukar Pappireddy	 * x11 = (base + off), w15 = index
436cc485e27SMadhukar Pappireddy	 *
437cc485e27SMadhukar Pappireddy	 * handler = (base + off) + (index << log2(size))
438cc485e27SMadhukar Pappireddy	 */
439cc485e27SMadhukar Pappireddy	adr	x11, (__RT_SVC_DESCS_START__ + RT_SVC_DESC_HANDLE)
440cc485e27SMadhukar Pappireddy	lsl	w10, w15, #RT_SVC_SIZE_LOG2
441cc485e27SMadhukar Pappireddy	ldr	x15, [x11, w10, uxtw]
442cc485e27SMadhukar Pappireddy
443a6ef4393SDouglas Raillard	/*
444a6ef4393SDouglas Raillard	 * Call the Secure Monitor Call handler and then drop directly into
445a6ef4393SDouglas Raillard	 * el3_exit() which will program any remaining architectural state
446a6ef4393SDouglas Raillard	 * prior to issuing the ERET to the desired lower EL.
447caa84939SJeenu Viswambharan	 */
448caa84939SJeenu Viswambharan#if DEBUG
449caa84939SJeenu Viswambharan	cbz	x15, rt_svc_fw_critical_error
450caa84939SJeenu Viswambharan#endif
451caa84939SJeenu Viswambharan	blr	x15
452caa84939SJeenu Viswambharan
453bbf8f6f9SYatharth Kochar	b	el3_exit
4544f6ad66aSAchin Gupta
455caa84939SJeenu Viswambharansmc_unknown:
456caa84939SJeenu Viswambharan	/*
457cc485e27SMadhukar Pappireddy	 * Unknown SMC call. Populate return value with SMC_UNK and call
458cc485e27SMadhukar Pappireddy	 * el3_exit() which will restore the remaining architectural state
459cc485e27SMadhukar Pappireddy	 * i.e., SYS, GP and PAuth registers(if any) prior to issuing the ERET
460cc485e27SMadhukar Pappireddy         * to the desired lower EL.
461caa84939SJeenu Viswambharan	 */
4624abd7fa7SAntonio Nino Diaz	mov	x0, #SMC_UNK
463cc485e27SMadhukar Pappireddy	str	x0, [x6, #CTX_GPREGS_OFFSET + CTX_GPREG_X0]
464cc485e27SMadhukar Pappireddy	b	el3_exit
465caa84939SJeenu Viswambharan
466caa84939SJeenu Viswambharansmc_prohibited:
467*3b8456bdSManish V Badarkhe	restore_ptw_el1_sys_regs
468*3b8456bdSManish V Badarkhe	ldp	x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28]
469c3260f9bSSoby Mathew	ldr	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
4704abd7fa7SAntonio Nino Diaz	mov	x0, #SMC_UNK
471f461fe34SAnthony Steinhauser	exception_return
472caa84939SJeenu Viswambharan
473ed108b56SAlexei Fedorov#if DEBUG
474caa84939SJeenu Viswambharanrt_svc_fw_critical_error:
475a6ef4393SDouglas Raillard	/* Switch to SP_ELx */
476ed108b56SAlexei Fedorov	msr	spsel, #MODE_SP_ELX
477a806dad5SJeenu Viswambharan	no_ret	report_unhandled_exception
478ed108b56SAlexei Fedorov#endif
4798b779620SKévin Petitendfunc smc_handler
4801f461979SJustin Chadwell
4811f461979SJustin Chadwell	/* ---------------------------------------------------------------------
4821f461979SJustin Chadwell	 * The following code handles exceptions caused by BRK instructions.
4831f461979SJustin Chadwell	 * Following a BRK instruction, the only real valid cause of action is
4841f461979SJustin Chadwell	 * to print some information and panic, as the code that caused it is
4851f461979SJustin Chadwell	 * likely in an inconsistent internal state.
4861f461979SJustin Chadwell	 *
4871f461979SJustin Chadwell	 * This is initially intended to be used in conjunction with
4881f461979SJustin Chadwell	 * __builtin_trap.
4891f461979SJustin Chadwell	 * ---------------------------------------------------------------------
4901f461979SJustin Chadwell	 */
4911f461979SJustin Chadwell#ifdef MONITOR_TRAPS
4921f461979SJustin Chadwellfunc brk_handler
4931f461979SJustin Chadwell	/* Extract the ISS */
4941f461979SJustin Chadwell	mrs	x10, esr_el3
4951f461979SJustin Chadwell	ubfx	x10, x10, #ESR_ISS_SHIFT, #ESR_ISS_LENGTH
4961f461979SJustin Chadwell
4971f461979SJustin Chadwell	/* Ensure the console is initialized */
4981f461979SJustin Chadwell	bl	plat_crash_console_init
4991f461979SJustin Chadwell
5001f461979SJustin Chadwell	adr	x4, brk_location
5011f461979SJustin Chadwell	bl	asm_print_str
5021f461979SJustin Chadwell	mrs	x4, elr_el3
5031f461979SJustin Chadwell	bl	asm_print_hex
5041f461979SJustin Chadwell	bl	asm_print_newline
5051f461979SJustin Chadwell
5061f461979SJustin Chadwell	adr	x4, brk_message
5071f461979SJustin Chadwell	bl	asm_print_str
5081f461979SJustin Chadwell	mov	x4, x10
5091f461979SJustin Chadwell	mov	x5, #28
5101f461979SJustin Chadwell	bl	asm_print_hex_bits
5111f461979SJustin Chadwell	bl	asm_print_newline
5121f461979SJustin Chadwell
5131f461979SJustin Chadwell	no_ret	plat_panic_handler
5141f461979SJustin Chadwellendfunc brk_handler
5151f461979SJustin Chadwell#endif /* MONITOR_TRAPS */
516