xref: /rk3399_ARM-atf/bl31/aarch64/runtime_exceptions.S (revision 3991b8898814ea5929e6d95cda5624291b2361c3)
14f6ad66aSAchin Gupta/*
217d07a55SGovindraj Raja * Copyright (c) 2013-2023, 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>
13ccd81f1eSAndre Przywara#include <bl31/sync_handle.h>
1409d40e0eSAntonio Nino Diaz#include <common/runtime_svc.h>
1597043ac9SDan Handley#include <context.h>
163b8456bdSManish V Badarkhe#include <el3_common_macros.S>
1709d40e0eSAntonio Nino Diaz#include <lib/el3_runtime/cpu_data.h>
1809d40e0eSAntonio Nino Diaz#include <lib/smccc.h>
194f6ad66aSAchin Gupta
204f6ad66aSAchin Gupta	.globl	runtime_exceptions
214f6ad66aSAchin Gupta
22f62ad322SDimitris Papastamos	.globl	sync_exception_sp_el0
23f62ad322SDimitris Papastamos	.globl	irq_sp_el0
24f62ad322SDimitris Papastamos	.globl	fiq_sp_el0
25f62ad322SDimitris Papastamos	.globl	serror_sp_el0
26f62ad322SDimitris Papastamos
27f62ad322SDimitris Papastamos	.globl	sync_exception_sp_elx
28f62ad322SDimitris Papastamos	.globl	irq_sp_elx
29f62ad322SDimitris Papastamos	.globl	fiq_sp_elx
30f62ad322SDimitris Papastamos	.globl	serror_sp_elx
31f62ad322SDimitris Papastamos
32f62ad322SDimitris Papastamos	.globl	sync_exception_aarch64
33f62ad322SDimitris Papastamos	.globl	irq_aarch64
34f62ad322SDimitris Papastamos	.globl	fiq_aarch64
35f62ad322SDimitris Papastamos	.globl	serror_aarch64
36f62ad322SDimitris Papastamos
37f62ad322SDimitris Papastamos	.globl	sync_exception_aarch32
38f62ad322SDimitris Papastamos	.globl	irq_aarch32
39f62ad322SDimitris Papastamos	.globl	fiq_aarch32
40f62ad322SDimitris Papastamos	.globl	serror_aarch32
41f62ad322SDimitris Papastamos
4276454abfSJeenu Viswambharan	/*
43d87c0e27SManish Pandey	 * Save LR and make x30 available as most of the routines in vector entry
44d87c0e27SManish Pandey	 * need a free register
45d87c0e27SManish Pandey	 */
46d87c0e27SManish Pandey	.macro save_x30
47d87c0e27SManish Pandey	str	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
48d87c0e27SManish Pandey	.endm
49d87c0e27SManish Pandey
50d87c0e27SManish Pandey	/*
5114c6016aSJeenu Viswambharan	 * Macro that prepares entry to EL3 upon taking an exception.
5214c6016aSJeenu Viswambharan	 *
539202d519SManish Pandey	 * With RAS_FFH_SUPPORT, this macro synchronizes pending errors with an
549202d519SManish Pandey	 * ESB instruction. When an error is thus synchronized, the handling is
5514c6016aSJeenu Viswambharan	 * delegated to platform EA handler.
5614c6016aSJeenu Viswambharan	 *
579202d519SManish Pandey	 * Without RAS_FFH_SUPPORT, this macro synchronizes pending errors using
58c2d32a5fSMadhukar Pappireddy	 * a DSB, unmasks Asynchronous External Aborts and saves X30 before
59c2d32a5fSMadhukar Pappireddy	 * setting the flag CTX_IS_IN_EL3.
6014c6016aSJeenu Viswambharan	 */
6114c6016aSJeenu Viswambharan	.macro check_and_unmask_ea
629202d519SManish Pandey#if RAS_FFH_SUPPORT
6314c6016aSJeenu Viswambharan	/* Synchronize pending External Aborts */
6414c6016aSJeenu Viswambharan	esb
6514c6016aSJeenu Viswambharan
6614c6016aSJeenu Viswambharan	/* Unmask the SError interrupt */
6714c6016aSJeenu Viswambharan	msr	daifclr, #DAIF_ABT_BIT
6814c6016aSJeenu Viswambharan
6914c6016aSJeenu Viswambharan	/* Check for SErrors synchronized by the ESB instruction */
7014c6016aSJeenu Viswambharan	mrs	x30, DISR_EL1
7114c6016aSJeenu Viswambharan	tbz	x30, #DISR_A_BIT, 1f
7214c6016aSJeenu Viswambharan
73e290a8fcSAlexei Fedorov	/*
74ed108b56SAlexei Fedorov	 * Save general purpose and ARMv8.3-PAuth registers (if enabled).
751d6d6802SBoyan Karatotev	 * Also save PMCR_EL0 and  set the PSTATE to a known state.
76e290a8fcSAlexei Fedorov	 */
7797215e0fSDaniel Boulby	bl	prepare_el3_entry
78e290a8fcSAlexei Fedorov
79df8f3188SJeenu Viswambharan	bl	handle_lower_el_ea_esb
8014c6016aSJeenu Viswambharan
81ed108b56SAlexei Fedorov	/* Restore general purpose, PMCR_EL0 and ARMv8.3-PAuth registers */
82ed108b56SAlexei Fedorov	bl	restore_gp_pmcr_pauth_regs
8314c6016aSJeenu Viswambharan1:
8414c6016aSJeenu Viswambharan#else
85c2d32a5fSMadhukar Pappireddy	/*
86c2d32a5fSMadhukar Pappireddy	 * Note 1: The explicit DSB at the entry of various exception vectors
87c2d32a5fSMadhukar Pappireddy	 * for handling exceptions from lower ELs can inadvertently trigger an
88c2d32a5fSMadhukar Pappireddy	 * SError exception in EL3 due to pending asynchronous aborts in lower
89c2d32a5fSMadhukar Pappireddy	 * ELs. This will end up being handled by serror_sp_elx which will
90c2d32a5fSMadhukar Pappireddy	 * ultimately panic and die.
91c2d32a5fSMadhukar Pappireddy	 * The way to workaround is to update a flag to indicate if the exception
92c2d32a5fSMadhukar Pappireddy	 * truly came from EL3. This flag is allocated in the cpu_context
93c2d32a5fSMadhukar Pappireddy	 * structure and located at offset "CTX_EL3STATE_OFFSET + CTX_IS_IN_EL3"
94c2d32a5fSMadhukar Pappireddy	 * This is not a bullet proof solution to the problem at hand because
95c2d32a5fSMadhukar Pappireddy	 * we assume the instructions following "isb" that help to update the
96c2d32a5fSMadhukar Pappireddy	 * flag execute without causing further exceptions.
97c2d32a5fSMadhukar Pappireddy	 */
98c2d32a5fSMadhukar Pappireddy
99c2d32a5fSMadhukar Pappireddy	/*
10076a91d87SManish Pandey	 * For SoCs which do not implement RAS, use DSB as a barrier to
10176a91d87SManish Pandey	 * synchronize pending external aborts.
102c2d32a5fSMadhukar Pappireddy	 */
103c2d32a5fSMadhukar Pappireddy	dsb	sy
104c2d32a5fSMadhukar Pappireddy
105c2d32a5fSMadhukar Pappireddy	/* Unmask the SError interrupt */
106c2d32a5fSMadhukar Pappireddy	msr	daifclr, #DAIF_ABT_BIT
107c2d32a5fSMadhukar Pappireddy
108c2d32a5fSMadhukar Pappireddy	/* Use ISB for the above unmask operation to take effect immediately */
109c2d32a5fSMadhukar Pappireddy	isb
110c2d32a5fSMadhukar Pappireddy
111d87c0e27SManish Pandey	/* Refer Note 1. */
112c2d32a5fSMadhukar Pappireddy	mov 	x30, #1
113c2d32a5fSMadhukar Pappireddy	str	x30, [sp, #CTX_EL3STATE_OFFSET + CTX_IS_IN_EL3]
114c2d32a5fSMadhukar Pappireddy	dmb	sy
115c2d32a5fSMadhukar Pappireddy#endif
11676a91d87SManish Pandey	.endm
117c2d32a5fSMadhukar Pappireddy
118a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
119a6ef4393SDouglas Raillard	 * This macro handles Synchronous exceptions.
120a6ef4393SDouglas Raillard	 * Only SMC exceptions are supported.
121a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
122dce74b89SAchin Gupta	 */
123dce74b89SAchin Gupta	.macro	handle_sync_exception
124872be88aSdp-arm#if ENABLE_RUNTIME_INSTRUMENTATION
125872be88aSdp-arm	/*
126a6ef4393SDouglas Raillard	 * Read the timestamp value and store it in per-cpu data. The value
127a6ef4393SDouglas Raillard	 * will be extracted from per-cpu data by the C level SMC handler and
128a6ef4393SDouglas Raillard	 * saved to the PMF timestamp region.
129872be88aSdp-arm	 */
130872be88aSdp-arm	mrs	x30, cntpct_el0
131872be88aSdp-arm	str	x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29]
132872be88aSdp-arm	mrs	x29, tpidr_el3
133872be88aSdp-arm	str	x30, [x29, #CPU_DATA_PMF_TS0_OFFSET]
134872be88aSdp-arm	ldr	x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29]
135872be88aSdp-arm#endif
136872be88aSdp-arm
137dce74b89SAchin Gupta	mrs	x30, esr_el3
138dce74b89SAchin Gupta	ubfx	x30, x30, #ESR_EC_SHIFT, #ESR_EC_LENGTH
139dce74b89SAchin Gupta
140a6ef4393SDouglas Raillard	/* Handle SMC exceptions separately from other synchronous exceptions */
141dce74b89SAchin Gupta	cmp	x30, #EC_AARCH32_SMC
142dce74b89SAchin Gupta	b.eq	smc_handler32
143dce74b89SAchin Gupta
144dce74b89SAchin Gupta	cmp	x30, #EC_AARCH64_SMC
145ccd81f1eSAndre Przywara	b.eq	sync_handler64
146ccd81f1eSAndre Przywara
147ccd81f1eSAndre Przywara	cmp	x30, #EC_AARCH64_SYS
148ccd81f1eSAndre Przywara	b.eq	sync_handler64
149dce74b89SAchin Gupta
150df8f3188SJeenu Viswambharan	/* Synchronous exceptions other than the above are assumed to be EA */
1514d91838bSJulius Werner	ldr	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
1526f7de9a8SManish Pandey	b	handle_lower_el_sync_ea
153dce74b89SAchin Gupta	.endm
154dce74b89SAchin Gupta
155e0ae9fabSSandrine Bailleuxvector_base runtime_exceptions
156e0ae9fabSSandrine Bailleux
157a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
158a6ef4393SDouglas Raillard	 * Current EL with SP_EL0 : 0x0 - 0x200
159a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
1604f6ad66aSAchin Gupta	 */
161e0ae9fabSSandrine Bailleuxvector_entry sync_exception_sp_el0
1621f461979SJustin Chadwell#ifdef MONITOR_TRAPS
1631f461979SJustin Chadwell	stp x29, x30, [sp, #-16]!
1641f461979SJustin Chadwell
1651f461979SJustin Chadwell	mrs	x30, esr_el3
1661f461979SJustin Chadwell	ubfx	x30, x30, #ESR_EC_SHIFT, #ESR_EC_LENGTH
1671f461979SJustin Chadwell
1681f461979SJustin Chadwell	/* Check for BRK */
1691f461979SJustin Chadwell	cmp	x30, #EC_BRK
1701f461979SJustin Chadwell	b.eq	brk_handler
1711f461979SJustin Chadwell
1721f461979SJustin Chadwell	ldp x29, x30, [sp], #16
1731f461979SJustin Chadwell#endif /* MONITOR_TRAPS */
1741f461979SJustin Chadwell
175a6ef4393SDouglas Raillard	/* We don't expect any synchronous exceptions from EL3 */
1764d91838bSJulius Werner	b	report_unhandled_exception
177a9203edaSRoberto Vargasend_vector_entry sync_exception_sp_el0
1784f6ad66aSAchin Gupta
179e0ae9fabSSandrine Bailleuxvector_entry irq_sp_el0
180a6ef4393SDouglas Raillard	/*
181a6ef4393SDouglas Raillard	 * EL3 code is non-reentrant. Any asynchronous exception is a serious
182a6ef4393SDouglas Raillard	 * error. Loop infinitely.
183a6ef4393SDouglas Raillard	 */
1844d91838bSJulius Werner	b	report_unhandled_interrupt
185a9203edaSRoberto Vargasend_vector_entry irq_sp_el0
1864f6ad66aSAchin Gupta
187e0ae9fabSSandrine Bailleux
188e0ae9fabSSandrine Bailleuxvector_entry fiq_sp_el0
1894d91838bSJulius Werner	b	report_unhandled_interrupt
190a9203edaSRoberto Vargasend_vector_entry fiq_sp_el0
1914f6ad66aSAchin Gupta
192e0ae9fabSSandrine Bailleux
193e0ae9fabSSandrine Bailleuxvector_entry serror_sp_el0
194eaeaa4d0SJeenu Viswambharan	no_ret	plat_handle_el3_ea
195a9203edaSRoberto Vargasend_vector_entry serror_sp_el0
1964f6ad66aSAchin Gupta
197a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
198a6ef4393SDouglas Raillard	 * Current EL with SP_ELx: 0x200 - 0x400
199a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
2004f6ad66aSAchin Gupta	 */
201e0ae9fabSSandrine Bailleuxvector_entry sync_exception_sp_elx
202a6ef4393SDouglas Raillard	/*
203a6ef4393SDouglas Raillard	 * This exception will trigger if anything went wrong during a previous
204a6ef4393SDouglas Raillard	 * exception entry or exit or while handling an earlier unexpected
205a6ef4393SDouglas Raillard	 * synchronous exception. There is a high probability that SP_EL3 is
206a6ef4393SDouglas Raillard	 * corrupted.
207caa84939SJeenu Viswambharan	 */
2084d91838bSJulius Werner	b	report_unhandled_exception
209a9203edaSRoberto Vargasend_vector_entry sync_exception_sp_elx
2104f6ad66aSAchin Gupta
211e0ae9fabSSandrine Bailleuxvector_entry irq_sp_elx
2124d91838bSJulius Werner	b	report_unhandled_interrupt
213a9203edaSRoberto Vargasend_vector_entry irq_sp_elx
214a7934d69SJeenu Viswambharan
215e0ae9fabSSandrine Bailleuxvector_entry fiq_sp_elx
2164d91838bSJulius Werner	b	report_unhandled_interrupt
217a9203edaSRoberto Vargasend_vector_entry fiq_sp_elx
218a7934d69SJeenu Viswambharan
219e0ae9fabSSandrine Bailleuxvector_entry serror_sp_elx
2209202d519SManish Pandey#if !RAS_FFH_SUPPORT
22176a91d87SManish Pandey	/*
22276a91d87SManish Pandey	 * This will trigger if the exception was taken due to SError in EL3 or
22376a91d87SManish Pandey	 * because of pending asynchronous external aborts from lower EL that got
22476a91d87SManish Pandey	 * triggered due to explicit synchronization in EL3. Refer Note 1.
22576a91d87SManish Pandey	 */
22676a91d87SManish Pandey	/* Assumes SP_EL3 on entry */
227d87c0e27SManish Pandey	save_x30
22876a91d87SManish Pandey	ldr	x30, [sp, #CTX_EL3STATE_OFFSET + CTX_IS_IN_EL3]
22976a91d87SManish Pandey	cbnz	x30, 1f
23076a91d87SManish Pandey
23176a91d87SManish Pandey	/* Handle asynchronous external abort from lower EL */
23276a91d87SManish Pandey	b	handle_lower_el_async_ea
23376a91d87SManish Pandey1:
234c2d32a5fSMadhukar Pappireddy#endif
235eaeaa4d0SJeenu Viswambharan	no_ret	plat_handle_el3_ea
236a9203edaSRoberto Vargasend_vector_entry serror_sp_elx
2374f6ad66aSAchin Gupta
238a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
23944804252SSandrine Bailleux	 * Lower EL using AArch64 : 0x400 - 0x600
240a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
2414f6ad66aSAchin Gupta	 */
242e0ae9fabSSandrine Bailleuxvector_entry sync_exception_aarch64
243a6ef4393SDouglas Raillard	/*
244a6ef4393SDouglas Raillard	 * This exception vector will be the entry point for SMCs and traps
245a6ef4393SDouglas Raillard	 * that are unhandled at lower ELs most commonly. SP_EL3 should point
246a6ef4393SDouglas Raillard	 * to a valid cpu context where the general purpose and system register
247a6ef4393SDouglas Raillard	 * state can be saved.
248caa84939SJeenu Viswambharan	 */
249d87c0e27SManish Pandey	save_x30
2503b8456bdSManish V Badarkhe	apply_at_speculative_wa
25114c6016aSJeenu Viswambharan	check_and_unmask_ea
252caa84939SJeenu Viswambharan	handle_sync_exception
253a9203edaSRoberto Vargasend_vector_entry sync_exception_aarch64
2544f6ad66aSAchin Gupta
255e0ae9fabSSandrine Bailleuxvector_entry irq_aarch64
256d87c0e27SManish Pandey	save_x30
2573b8456bdSManish V Badarkhe	apply_at_speculative_wa
25814c6016aSJeenu Viswambharan	check_and_unmask_ea
259*3991b889SManish Pandey	b	handle_interrupt_exception
260a9203edaSRoberto Vargasend_vector_entry irq_aarch64
2614f6ad66aSAchin Gupta
262e0ae9fabSSandrine Bailleuxvector_entry fiq_aarch64
263d87c0e27SManish Pandey	save_x30
2643b8456bdSManish V Badarkhe	apply_at_speculative_wa
26514c6016aSJeenu Viswambharan	check_and_unmask_ea
266*3991b889SManish Pandey	b 	handle_interrupt_exception
267a9203edaSRoberto Vargasend_vector_entry fiq_aarch64
2684f6ad66aSAchin Gupta
269e0ae9fabSSandrine Bailleuxvector_entry serror_aarch64
270d87c0e27SManish Pandey	save_x30
2713b8456bdSManish V Badarkhe	apply_at_speculative_wa
2729202d519SManish Pandey#if RAS_FFH_SUPPORT
27376454abfSJeenu Viswambharan	msr	daifclr, #DAIF_ABT_BIT
274c2d32a5fSMadhukar Pappireddy#else
27576a91d87SManish Pandey	check_and_unmask_ea
276c2d32a5fSMadhukar Pappireddy#endif
2776f7de9a8SManish Pandey	b	handle_lower_el_async_ea
2786f7de9a8SManish Pandey
279a9203edaSRoberto Vargasend_vector_entry serror_aarch64
2804f6ad66aSAchin Gupta
281a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
28244804252SSandrine Bailleux	 * Lower EL using AArch32 : 0x600 - 0x800
283a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
2844f6ad66aSAchin Gupta	 */
285e0ae9fabSSandrine Bailleuxvector_entry sync_exception_aarch32
286a6ef4393SDouglas Raillard	/*
287a6ef4393SDouglas Raillard	 * This exception vector will be the entry point for SMCs and traps
288a6ef4393SDouglas Raillard	 * that are unhandled at lower ELs most commonly. SP_EL3 should point
289a6ef4393SDouglas Raillard	 * to a valid cpu context where the general purpose and system register
290a6ef4393SDouglas Raillard	 * state can be saved.
291caa84939SJeenu Viswambharan	 */
292d87c0e27SManish Pandey	save_x30
2933b8456bdSManish V Badarkhe	apply_at_speculative_wa
29414c6016aSJeenu Viswambharan	check_and_unmask_ea
295caa84939SJeenu Viswambharan	handle_sync_exception
296a9203edaSRoberto Vargasend_vector_entry sync_exception_aarch32
2974f6ad66aSAchin Gupta
298e0ae9fabSSandrine Bailleuxvector_entry irq_aarch32
299d87c0e27SManish Pandey	save_x30
3003b8456bdSManish V Badarkhe	apply_at_speculative_wa
30114c6016aSJeenu Viswambharan	check_and_unmask_ea
302*3991b889SManish Pandey	b	handle_interrupt_exception
303a9203edaSRoberto Vargasend_vector_entry irq_aarch32
3044f6ad66aSAchin Gupta
305e0ae9fabSSandrine Bailleuxvector_entry fiq_aarch32
306d87c0e27SManish Pandey	save_x30
3073b8456bdSManish V Badarkhe	apply_at_speculative_wa
30814c6016aSJeenu Viswambharan	check_and_unmask_ea
309*3991b889SManish Pandey	b	handle_interrupt_exception
310a9203edaSRoberto Vargasend_vector_entry fiq_aarch32
3114f6ad66aSAchin Gupta
312e0ae9fabSSandrine Bailleuxvector_entry serror_aarch32
313d87c0e27SManish Pandey	save_x30
3143b8456bdSManish V Badarkhe	apply_at_speculative_wa
3159202d519SManish Pandey#if RAS_FFH_SUPPORT
31676454abfSJeenu Viswambharan	msr	daifclr, #DAIF_ABT_BIT
317c2d32a5fSMadhukar Pappireddy#else
31876a91d87SManish Pandey	check_and_unmask_ea
319c2d32a5fSMadhukar Pappireddy#endif
3206f7de9a8SManish Pandey	b	handle_lower_el_async_ea
3216f7de9a8SManish Pandey
322a9203edaSRoberto Vargasend_vector_entry serror_aarch32
323a7934d69SJeenu Viswambharan
3241f461979SJustin Chadwell#ifdef MONITOR_TRAPS
3251f461979SJustin Chadwell	.section .rodata.brk_string, "aS"
3261f461979SJustin Chadwellbrk_location:
3271f461979SJustin Chadwell	.asciz "Error at instruction 0x"
3281f461979SJustin Chadwellbrk_message:
3291f461979SJustin Chadwell	.asciz "Unexpected BRK instruction with value 0x"
3301f461979SJustin Chadwell#endif /* MONITOR_TRAPS */
3311f461979SJustin Chadwell
3322f370465SAntonio Nino Diaz	/* ---------------------------------------------------------------------
333caa84939SJeenu Viswambharan	 * The following code handles secure monitor calls.
334a6ef4393SDouglas Raillard	 * Depending upon the execution state from where the SMC has been
335a6ef4393SDouglas Raillard	 * invoked, it frees some general purpose registers to perform the
336a6ef4393SDouglas Raillard	 * remaining tasks. They involve finding the runtime service handler
337a6ef4393SDouglas Raillard	 * that is the target of the SMC & switching to runtime stacks (SP_EL0)
338a6ef4393SDouglas Raillard	 * before calling the handler.
339caa84939SJeenu Viswambharan	 *
340a6ef4393SDouglas Raillard	 * Note that x30 has been explicitly saved and can be used here
341a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
342caa84939SJeenu Viswambharan	 */
343ccd81f1eSAndre Przywarafunc sync_exception_handler
344caa84939SJeenu Viswambharansmc_handler32:
345caa84939SJeenu Viswambharan	/* Check whether aarch32 issued an SMC64 */
346caa84939SJeenu Viswambharan	tbnz	x0, #FUNCID_CC_SHIFT, smc_prohibited
347caa84939SJeenu Viswambharan
348ccd81f1eSAndre Przywarasync_handler64:
3495283962eSAntonio Nino Diaz	/* NOTE: The code below must preserve x0-x4 */
3505283962eSAntonio Nino Diaz
351e290a8fcSAlexei Fedorov	/*
352ed108b56SAlexei Fedorov	 * Save general purpose and ARMv8.3-PAuth registers (if enabled).
3531d6d6802SBoyan Karatotev	 * Also save PMCR_EL0 and  set the PSTATE to a known state.
354e290a8fcSAlexei Fedorov	 */
35597215e0fSDaniel Boulby	bl	prepare_el3_entry
356e290a8fcSAlexei Fedorov
357b86048c4SAntonio Nino Diaz#if ENABLE_PAUTH
358ed108b56SAlexei Fedorov	/* Load and program APIAKey firmware key */
359ed108b56SAlexei Fedorov	bl	pauth_load_bl31_apiakey
360b86048c4SAntonio Nino Diaz#endif
3615283962eSAntonio Nino Diaz
362a6ef4393SDouglas Raillard	/*
363a6ef4393SDouglas Raillard	 * Populate the parameters for the SMC handler.
364a6ef4393SDouglas Raillard	 * We already have x0-x4 in place. x5 will point to a cookie (not used
365a6ef4393SDouglas Raillard	 * now). x6 will point to the context structure (SP_EL3) and x7 will
366201ca5b6SDimitris Papastamos	 * contain flags we need to pass to the handler.
367caa84939SJeenu Viswambharan	 */
368caa84939SJeenu Viswambharan	mov	x5, xzr
369caa84939SJeenu Viswambharan	mov	x6, sp
370caa84939SJeenu Viswambharan
371a6ef4393SDouglas Raillard	/*
372a6ef4393SDouglas Raillard	 * Restore the saved C runtime stack value which will become the new
373a6ef4393SDouglas Raillard	 * SP_EL0 i.e. EL3 runtime stack. It was saved in the 'cpu_context'
374a6ef4393SDouglas Raillard	 * structure prior to the last ERET from EL3.
375caa84939SJeenu Viswambharan	 */
376caa84939SJeenu Viswambharan	ldr	x12, [x6, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
377caa84939SJeenu Viswambharan
378caa84939SJeenu Viswambharan	/* Switch to SP_EL0 */
379ed108b56SAlexei Fedorov	msr	spsel, #MODE_SP_EL0
380caa84939SJeenu Viswambharan
381a6ef4393SDouglas Raillard	/*
382e61713b0SManish Pandey	 * Save the SPSR_EL3 and ELR_EL3 in case there is a world
383a6ef4393SDouglas Raillard	 * switch during SMC handling.
384a6ef4393SDouglas Raillard	 * TODO: Revisit if all system registers can be saved later.
385caa84939SJeenu Viswambharan	 */
386caa84939SJeenu Viswambharan	mrs	x16, spsr_el3
387caa84939SJeenu Viswambharan	mrs	x17, elr_el3
388caa84939SJeenu Viswambharan	stp	x16, x17, [x6, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
389e61713b0SManish Pandey
390e61713b0SManish Pandey	/* Load SCR_EL3 */
391e61713b0SManish Pandey	mrs	x18, scr_el3
392caa84939SJeenu Viswambharan
393ccd81f1eSAndre Przywara	/* check for system register traps */
394ccd81f1eSAndre Przywara	mrs	x16, esr_el3
395ccd81f1eSAndre Przywara	ubfx	x17, x16, #ESR_EC_SHIFT, #ESR_EC_LENGTH
396ccd81f1eSAndre Przywara	cmp	x17, #EC_AARCH64_SYS
397ccd81f1eSAndre Przywara	b.eq	sysreg_handler64
398ccd81f1eSAndre Przywara
3994693ff72SZelalem Aweke	/* Clear flag register */
4004693ff72SZelalem Aweke	mov	x7, xzr
4014693ff72SZelalem Aweke
4024693ff72SZelalem Aweke#if ENABLE_RME
4034693ff72SZelalem Aweke	/* Copy SCR_EL3.NSE bit to the flag to indicate caller's security */
4044693ff72SZelalem Aweke	ubfx	x7, x18, #SCR_NSE_SHIFT, 1
4054693ff72SZelalem Aweke
4064693ff72SZelalem Aweke	/*
4074693ff72SZelalem Aweke	 * Shift copied SCR_EL3.NSE bit by 5 to create space for
4080fe7b9f2SOlivier Deprez	 * SCR_EL3.NS bit. Bit 5 of the flag corresponds to
4094693ff72SZelalem Aweke	 * the SCR_EL3.NSE bit.
4104693ff72SZelalem Aweke	 */
4114693ff72SZelalem Aweke	lsl	x7, x7, #5
4124693ff72SZelalem Aweke#endif /* ENABLE_RME */
4134693ff72SZelalem Aweke
414caa84939SJeenu Viswambharan	/* Copy SCR_EL3.NS bit to the flag to indicate caller's security */
415caa84939SJeenu Viswambharan	bfi	x7, x18, #0, #1
416caa84939SJeenu Viswambharan
417f8a35797SJayanth Dodderi Chidanand	mov	sp, x12
418f8a35797SJayanth Dodderi Chidanand
419f8a35797SJayanth Dodderi Chidanand	/*
420f8a35797SJayanth Dodderi Chidanand	 * Per SMCCC documentation, bits [23:17] must be zero for Fast
421f8a35797SJayanth Dodderi Chidanand	 * SMCs. Other values are reserved for future use. Ensure that
422f8a35797SJayanth Dodderi Chidanand	 * these bits are zeroes, if not report as unknown SMC.
423f8a35797SJayanth Dodderi Chidanand	 */
424f8a35797SJayanth Dodderi Chidanand	tbz	x0, #FUNCID_TYPE_SHIFT, 2f  /* Skip check if its a Yield Call*/
425f8a35797SJayanth Dodderi Chidanand	tst	x0, #(FUNCID_FC_RESERVED_MASK << FUNCID_FC_RESERVED_SHIFT)
426f8a35797SJayanth Dodderi Chidanand	b.ne	smc_unknown
427f8a35797SJayanth Dodderi Chidanand
4280fe7b9f2SOlivier Deprez	/*
4290fe7b9f2SOlivier Deprez	 * Per SMCCCv1.3 a caller can set the SVE hint bit in the SMC FID
4300fe7b9f2SOlivier Deprez	 * passed through x0. Copy the SVE hint bit to flags and mask the
4310fe7b9f2SOlivier Deprez	 * bit in smc_fid passed to the standard service dispatcher.
4320fe7b9f2SOlivier Deprez	 * A service/dispatcher can retrieve the SVE hint bit state from
4330fe7b9f2SOlivier Deprez	 * flags using the appropriate helper.
4340fe7b9f2SOlivier Deprez	 */
435f8a35797SJayanth Dodderi Chidanand2:
436b2d85178SOlivier Deprez	and	x16, x0, #(FUNCID_SVE_HINT_MASK << FUNCID_SVE_HINT_SHIFT)
437b2d85178SOlivier Deprez	orr	x7, x7, x16
4380fe7b9f2SOlivier Deprez	bic	x0, x0, #(FUNCID_SVE_HINT_MASK << FUNCID_SVE_HINT_SHIFT)
4390fe7b9f2SOlivier Deprez
440cc485e27SMadhukar Pappireddy	/* Get the unique owning entity number */
441cc485e27SMadhukar Pappireddy	ubfx	x16, x0, #FUNCID_OEN_SHIFT, #FUNCID_OEN_WIDTH
442cc485e27SMadhukar Pappireddy	ubfx	x15, x0, #FUNCID_TYPE_SHIFT, #FUNCID_TYPE_WIDTH
443cc485e27SMadhukar Pappireddy	orr	x16, x16, x15, lsl #FUNCID_OEN_WIDTH
444cc485e27SMadhukar Pappireddy
445cc485e27SMadhukar Pappireddy	/* Load descriptor index from array of indices */
446c367b75eSMadhukar Pappireddy	adrp	x14, rt_svc_descs_indices
447c367b75eSMadhukar Pappireddy	add	x14, x14, :lo12:rt_svc_descs_indices
448cc485e27SMadhukar Pappireddy	ldrb	w15, [x14, x16]
449cc485e27SMadhukar Pappireddy
450cc485e27SMadhukar Pappireddy	/* Any index greater than 127 is invalid. Check bit 7. */
451cc485e27SMadhukar Pappireddy	tbnz	w15, 7, smc_unknown
452cc485e27SMadhukar Pappireddy
453cc485e27SMadhukar Pappireddy	/*
454cc485e27SMadhukar Pappireddy	 * Get the descriptor using the index
455cc485e27SMadhukar Pappireddy	 * x11 = (base + off), w15 = index
456cc485e27SMadhukar Pappireddy	 *
457cc485e27SMadhukar Pappireddy	 * handler = (base + off) + (index << log2(size))
458cc485e27SMadhukar Pappireddy	 */
459cc485e27SMadhukar Pappireddy	adr	x11, (__RT_SVC_DESCS_START__ + RT_SVC_DESC_HANDLE)
460cc485e27SMadhukar Pappireddy	lsl	w10, w15, #RT_SVC_SIZE_LOG2
461cc485e27SMadhukar Pappireddy	ldr	x15, [x11, w10, uxtw]
462cc485e27SMadhukar Pappireddy
463a6ef4393SDouglas Raillard	/*
464a6ef4393SDouglas Raillard	 * Call the Secure Monitor Call handler and then drop directly into
465a6ef4393SDouglas Raillard	 * el3_exit() which will program any remaining architectural state
466a6ef4393SDouglas Raillard	 * prior to issuing the ERET to the desired lower EL.
467caa84939SJeenu Viswambharan	 */
468caa84939SJeenu Viswambharan#if DEBUG
469caa84939SJeenu Viswambharan	cbz	x15, rt_svc_fw_critical_error
470caa84939SJeenu Viswambharan#endif
471caa84939SJeenu Viswambharan	blr	x15
472caa84939SJeenu Viswambharan
473bbf8f6f9SYatharth Kochar	b	el3_exit
4744f6ad66aSAchin Gupta
475ccd81f1eSAndre Przywarasysreg_handler64:
476ccd81f1eSAndre Przywara	mov	x0, x16		/* ESR_EL3, containing syndrome information */
477ccd81f1eSAndre Przywara	mov	x1, x6		/* lower EL's context */
478ccd81f1eSAndre Przywara	mov	x19, x6		/* save context pointer for after the call */
479ccd81f1eSAndre Przywara	mov	sp, x12		/* EL3 runtime stack, as loaded above */
480ccd81f1eSAndre Przywara
481ccd81f1eSAndre Przywara	/* int handle_sysreg_trap(uint64_t esr_el3, cpu_context_t *ctx); */
482ccd81f1eSAndre Przywara	bl	handle_sysreg_trap
483ccd81f1eSAndre Przywara	/*
484ccd81f1eSAndre Przywara	 * returns:
485ccd81f1eSAndre Przywara	 *   -1: unhandled trap, panic
486ccd81f1eSAndre Przywara	 *    0: handled trap, return to the trapping instruction (repeating it)
487ccd81f1eSAndre Przywara	 *    1: handled trap, return to the next instruction
488ccd81f1eSAndre Przywara	 */
489ccd81f1eSAndre Przywara
490ccd81f1eSAndre Przywara	tst	w0, w0
49117d07a55SGovindraj Raja	b.mi	elx_panic	/* negative return value: panic */
492ccd81f1eSAndre Przywara	b.eq	1f		/* zero: do not change ELR_EL3 */
493ccd81f1eSAndre Przywara
494ccd81f1eSAndre Przywara	/* advance the PC to continue after the instruction */
495ccd81f1eSAndre Przywara	ldr	x1, [x19, #CTX_EL3STATE_OFFSET + CTX_ELR_EL3]
496ccd81f1eSAndre Przywara	add	x1, x1, #4
497ccd81f1eSAndre Przywara	str	x1, [x19, #CTX_EL3STATE_OFFSET + CTX_ELR_EL3]
498ccd81f1eSAndre Przywara1:
499ccd81f1eSAndre Przywara	b	el3_exit
500ccd81f1eSAndre Przywara
501caa84939SJeenu Viswambharansmc_unknown:
502caa84939SJeenu Viswambharan	/*
503cc485e27SMadhukar Pappireddy	 * Unknown SMC call. Populate return value with SMC_UNK and call
504cc485e27SMadhukar Pappireddy	 * el3_exit() which will restore the remaining architectural state
505cc485e27SMadhukar Pappireddy	 * i.e., SYS, GP and PAuth registers(if any) prior to issuing the ERET
506cc485e27SMadhukar Pappireddy	 * to the desired lower EL.
507caa84939SJeenu Viswambharan	 */
5084abd7fa7SAntonio Nino Diaz	mov	x0, #SMC_UNK
509cc485e27SMadhukar Pappireddy	str	x0, [x6, #CTX_GPREGS_OFFSET + CTX_GPREG_X0]
510cc485e27SMadhukar Pappireddy	b	el3_exit
511caa84939SJeenu Viswambharan
512caa84939SJeenu Viswambharansmc_prohibited:
5133b8456bdSManish V Badarkhe	restore_ptw_el1_sys_regs
5143b8456bdSManish V Badarkhe	ldp	x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28]
515c3260f9bSSoby Mathew	ldr	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
5164abd7fa7SAntonio Nino Diaz	mov	x0, #SMC_UNK
517f461fe34SAnthony Steinhauser	exception_return
518caa84939SJeenu Viswambharan
519ed108b56SAlexei Fedorov#if DEBUG
520caa84939SJeenu Viswambharanrt_svc_fw_critical_error:
521a6ef4393SDouglas Raillard	/* Switch to SP_ELx */
522ed108b56SAlexei Fedorov	msr	spsel, #MODE_SP_ELX
523a806dad5SJeenu Viswambharan	no_ret	report_unhandled_exception
524ed108b56SAlexei Fedorov#endif
525ccd81f1eSAndre Przywaraendfunc sync_exception_handler
5261f461979SJustin Chadwell
5271f461979SJustin Chadwell	/* ---------------------------------------------------------------------
528*3991b889SManish Pandey	 * This function handles FIQ or IRQ interrupts i.e. EL3, S-EL1 and NS
529*3991b889SManish Pandey	 * interrupts.
530*3991b889SManish Pandey	 *
531*3991b889SManish Pandey	 * Note that x30 has been explicitly saved and can be used here
532*3991b889SManish Pandey	 * ---------------------------------------------------------------------
533*3991b889SManish Pandey	 */
534*3991b889SManish Pandeyfunc handle_interrupt_exception
535*3991b889SManish Pandey	/*
536*3991b889SManish Pandey	 * Save general purpose and ARMv8.3-PAuth registers (if enabled).
537*3991b889SManish Pandey	 * Also save PMCR_EL0 and  set the PSTATE to a known state.
538*3991b889SManish Pandey	 */
539*3991b889SManish Pandey	bl	prepare_el3_entry
540*3991b889SManish Pandey
541*3991b889SManish Pandey#if ENABLE_PAUTH
542*3991b889SManish Pandey	/* Load and program APIAKey firmware key */
543*3991b889SManish Pandey	bl	pauth_load_bl31_apiakey
544*3991b889SManish Pandey#endif
545*3991b889SManish Pandey
546*3991b889SManish Pandey	/* Save the EL3 system registers needed to return from this exception */
547*3991b889SManish Pandey	mrs	x0, spsr_el3
548*3991b889SManish Pandey	mrs	x1, elr_el3
549*3991b889SManish Pandey	stp	x0, x1, [sp, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
550*3991b889SManish Pandey
551*3991b889SManish Pandey	/* Switch to the runtime stack i.e. SP_EL0 */
552*3991b889SManish Pandey	ldr	x2, [sp, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
553*3991b889SManish Pandey	mov	x20, sp
554*3991b889SManish Pandey	msr	spsel, #MODE_SP_EL0
555*3991b889SManish Pandey	mov	sp, x2
556*3991b889SManish Pandey
557*3991b889SManish Pandey	/*
558*3991b889SManish Pandey	 * Find out whether this is a valid interrupt type.
559*3991b889SManish Pandey	 * If the interrupt controller reports a spurious interrupt then return
560*3991b889SManish Pandey	 * to where we came from.
561*3991b889SManish Pandey	 */
562*3991b889SManish Pandey	bl	plat_ic_get_pending_interrupt_type
563*3991b889SManish Pandey	cmp	x0, #INTR_TYPE_INVAL
564*3991b889SManish Pandey	b.eq	interrupt_exit
565*3991b889SManish Pandey
566*3991b889SManish Pandey	/*
567*3991b889SManish Pandey	 * Get the registered handler for this interrupt type.
568*3991b889SManish Pandey	 * A NULL return value could be 'cause of the following conditions:
569*3991b889SManish Pandey	 *
570*3991b889SManish Pandey	 * a. An interrupt of a type was routed correctly but a handler for its
571*3991b889SManish Pandey	 *    type was not registered.
572*3991b889SManish Pandey	 *
573*3991b889SManish Pandey	 * b. An interrupt of a type was not routed correctly so a handler for
574*3991b889SManish Pandey	 *    its type was not registered.
575*3991b889SManish Pandey	 *
576*3991b889SManish Pandey	 * c. An interrupt of a type was routed correctly to EL3, but was
577*3991b889SManish Pandey	 *    deasserted before its pending state could be read. Another
578*3991b889SManish Pandey	 *    interrupt of a different type pended at the same time and its
579*3991b889SManish Pandey	 *    type was reported as pending instead. However, a handler for this
580*3991b889SManish Pandey	 *    type was not registered.
581*3991b889SManish Pandey	 *
582*3991b889SManish Pandey	 * a. and b. can only happen due to a programming error. The
583*3991b889SManish Pandey	 * occurrence of c. could be beyond the control of Trusted Firmware.
584*3991b889SManish Pandey	 * It makes sense to return from this exception instead of reporting an
585*3991b889SManish Pandey	 * error.
586*3991b889SManish Pandey	 */
587*3991b889SManish Pandey	bl	get_interrupt_type_handler
588*3991b889SManish Pandey	cbz	x0, interrupt_exit
589*3991b889SManish Pandey	mov	x21, x0
590*3991b889SManish Pandey
591*3991b889SManish Pandey	mov	x0, #INTR_ID_UNAVAILABLE
592*3991b889SManish Pandey
593*3991b889SManish Pandey	/* Set the current security state in the 'flags' parameter */
594*3991b889SManish Pandey	mrs	x2, scr_el3
595*3991b889SManish Pandey	ubfx	x1, x2, #0, #1
596*3991b889SManish Pandey
597*3991b889SManish Pandey	/* Restore the reference to the 'handle' i.e. SP_EL3 */
598*3991b889SManish Pandey	mov	x2, x20
599*3991b889SManish Pandey
600*3991b889SManish Pandey	/* x3 will point to a cookie (not used now) */
601*3991b889SManish Pandey	mov	x3, xzr
602*3991b889SManish Pandey
603*3991b889SManish Pandey	/* Call the interrupt type handler */
604*3991b889SManish Pandey	blr	x21
605*3991b889SManish Pandey
606*3991b889SManish Pandeyinterrupt_exit:
607*3991b889SManish Pandey	/* Return from exception, possibly in a different security state */
608*3991b889SManish Pandey	b	el3_exit
609*3991b889SManish Pandeyendfunc handle_interrupt_exception
610*3991b889SManish Pandey
611*3991b889SManish Pandey	/* ---------------------------------------------------------------------
6121f461979SJustin Chadwell	 * The following code handles exceptions caused by BRK instructions.
6131f461979SJustin Chadwell	 * Following a BRK instruction, the only real valid cause of action is
6141f461979SJustin Chadwell	 * to print some information and panic, as the code that caused it is
6151f461979SJustin Chadwell	 * likely in an inconsistent internal state.
6161f461979SJustin Chadwell	 *
6171f461979SJustin Chadwell	 * This is initially intended to be used in conjunction with
6181f461979SJustin Chadwell	 * __builtin_trap.
6191f461979SJustin Chadwell	 * ---------------------------------------------------------------------
6201f461979SJustin Chadwell	 */
6211f461979SJustin Chadwell#ifdef MONITOR_TRAPS
6221f461979SJustin Chadwellfunc brk_handler
6231f461979SJustin Chadwell	/* Extract the ISS */
6241f461979SJustin Chadwell	mrs	x10, esr_el3
6251f461979SJustin Chadwell	ubfx	x10, x10, #ESR_ISS_SHIFT, #ESR_ISS_LENGTH
6261f461979SJustin Chadwell
6271f461979SJustin Chadwell	/* Ensure the console is initialized */
6281f461979SJustin Chadwell	bl	plat_crash_console_init
6291f461979SJustin Chadwell
6301f461979SJustin Chadwell	adr	x4, brk_location
6311f461979SJustin Chadwell	bl	asm_print_str
6321f461979SJustin Chadwell	mrs	x4, elr_el3
6331f461979SJustin Chadwell	bl	asm_print_hex
6341f461979SJustin Chadwell	bl	asm_print_newline
6351f461979SJustin Chadwell
6361f461979SJustin Chadwell	adr	x4, brk_message
6371f461979SJustin Chadwell	bl	asm_print_str
6381f461979SJustin Chadwell	mov	x4, x10
6391f461979SJustin Chadwell	mov	x5, #28
6401f461979SJustin Chadwell	bl	asm_print_hex_bits
6411f461979SJustin Chadwell	bl	asm_print_newline
6421f461979SJustin Chadwell
6431f461979SJustin Chadwell	no_ret	plat_panic_handler
6441f461979SJustin Chadwellendfunc brk_handler
6451f461979SJustin Chadwell#endif /* MONITOR_TRAPS */
646