xref: /rk3399_ARM-atf/bl31/aarch64/runtime_exceptions.S (revision 1f4619796af5baf4b41b5723bbf708355f8597fa)
14f6ad66aSAchin Gupta/*
20709055eSAntonio Nino Diaz * Copyright (c) 2013-2019, 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>
1509d40e0eSAntonio Nino Diaz#include <lib/el3_runtime/cpu_data.h>
1609d40e0eSAntonio Nino Diaz#include <lib/smccc.h>
174f6ad66aSAchin Gupta
184f6ad66aSAchin Gupta	.globl	runtime_exceptions
194f6ad66aSAchin Gupta
20f62ad322SDimitris Papastamos	.globl	sync_exception_sp_el0
21f62ad322SDimitris Papastamos	.globl	irq_sp_el0
22f62ad322SDimitris Papastamos	.globl	fiq_sp_el0
23f62ad322SDimitris Papastamos	.globl	serror_sp_el0
24f62ad322SDimitris Papastamos
25f62ad322SDimitris Papastamos	.globl	sync_exception_sp_elx
26f62ad322SDimitris Papastamos	.globl	irq_sp_elx
27f62ad322SDimitris Papastamos	.globl	fiq_sp_elx
28f62ad322SDimitris Papastamos	.globl	serror_sp_elx
29f62ad322SDimitris Papastamos
30f62ad322SDimitris Papastamos	.globl	sync_exception_aarch64
31f62ad322SDimitris Papastamos	.globl	irq_aarch64
32f62ad322SDimitris Papastamos	.globl	fiq_aarch64
33f62ad322SDimitris Papastamos	.globl	serror_aarch64
34f62ad322SDimitris Papastamos
35f62ad322SDimitris Papastamos	.globl	sync_exception_aarch32
36f62ad322SDimitris Papastamos	.globl	irq_aarch32
37f62ad322SDimitris Papastamos	.globl	fiq_aarch32
38f62ad322SDimitris Papastamos	.globl	serror_aarch32
39f62ad322SDimitris Papastamos
4076454abfSJeenu Viswambharan	/*
4114c6016aSJeenu Viswambharan	 * Macro that prepares entry to EL3 upon taking an exception.
4214c6016aSJeenu Viswambharan	 *
4314c6016aSJeenu Viswambharan	 * With RAS_EXTENSION, this macro synchronizes pending errors with an ESB
4414c6016aSJeenu Viswambharan	 * instruction. When an error is thus synchronized, the handling is
4514c6016aSJeenu Viswambharan	 * delegated to platform EA handler.
4614c6016aSJeenu Viswambharan	 *
4714c6016aSJeenu Viswambharan	 * Without RAS_EXTENSION, this macro just saves x30, and unmasks
4814c6016aSJeenu Viswambharan	 * Asynchronous External Aborts.
4914c6016aSJeenu Viswambharan	 */
5014c6016aSJeenu Viswambharan	.macro check_and_unmask_ea
5114c6016aSJeenu Viswambharan#if RAS_EXTENSION
5214c6016aSJeenu Viswambharan	/* Synchronize pending External Aborts */
5314c6016aSJeenu Viswambharan	esb
5414c6016aSJeenu Viswambharan
5514c6016aSJeenu Viswambharan	/* Unmask the SError interrupt */
5614c6016aSJeenu Viswambharan	msr	daifclr, #DAIF_ABT_BIT
5714c6016aSJeenu Viswambharan
5814c6016aSJeenu Viswambharan	/*
5914c6016aSJeenu Viswambharan	 * Explicitly save x30 so as to free up a register and to enable
6014c6016aSJeenu Viswambharan	 * branching
6114c6016aSJeenu Viswambharan	 */
6214c6016aSJeenu Viswambharan	str	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
6314c6016aSJeenu Viswambharan
6414c6016aSJeenu Viswambharan	/* Check for SErrors synchronized by the ESB instruction */
6514c6016aSJeenu Viswambharan	mrs	x30, DISR_EL1
6614c6016aSJeenu Viswambharan	tbz	x30, #DISR_A_BIT, 1f
6714c6016aSJeenu Viswambharan
6814c6016aSJeenu Viswambharan	/* Save GP registers and restore them afterwards */
6914c6016aSJeenu Viswambharan	bl	save_gp_registers
70e290a8fcSAlexei Fedorov
71e290a8fcSAlexei Fedorov	/*
72e290a8fcSAlexei Fedorov	 * If Secure Cycle Counter is not disabled in MDCR_EL3
73e290a8fcSAlexei Fedorov	 * when ARMv8.5-PMU is implemented, save PMCR_EL0 and
74e290a8fcSAlexei Fedorov	 * disable all event counters and cycle counter.
75e290a8fcSAlexei Fedorov	 */
76e290a8fcSAlexei Fedorov	bl	save_pmcr_disable_pmu
77e290a8fcSAlexei Fedorov
78df8f3188SJeenu Viswambharan	bl	handle_lower_el_ea_esb
7914c6016aSJeenu Viswambharan	bl	restore_gp_registers
8014c6016aSJeenu Viswambharan
8114c6016aSJeenu Viswambharan1:
8214c6016aSJeenu Viswambharan#else
8314c6016aSJeenu Viswambharan	/* Unmask the SError interrupt */
8414c6016aSJeenu Viswambharan	msr	daifclr, #DAIF_ABT_BIT
8514c6016aSJeenu Viswambharan
8614c6016aSJeenu Viswambharan	str	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
8714c6016aSJeenu Viswambharan#endif
8814c6016aSJeenu Viswambharan	.endm
8914c6016aSJeenu Viswambharan
90a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
91a6ef4393SDouglas Raillard	 * This macro handles Synchronous exceptions.
92a6ef4393SDouglas Raillard	 * Only SMC exceptions are supported.
93a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
94dce74b89SAchin Gupta	 */
95dce74b89SAchin Gupta	.macro	handle_sync_exception
96872be88aSdp-arm#if ENABLE_RUNTIME_INSTRUMENTATION
97872be88aSdp-arm	/*
98a6ef4393SDouglas Raillard	 * Read the timestamp value and store it in per-cpu data. The value
99a6ef4393SDouglas Raillard	 * will be extracted from per-cpu data by the C level SMC handler and
100a6ef4393SDouglas Raillard	 * saved to the PMF timestamp region.
101872be88aSdp-arm	 */
102872be88aSdp-arm	mrs	x30, cntpct_el0
103872be88aSdp-arm	str	x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29]
104872be88aSdp-arm	mrs	x29, tpidr_el3
105872be88aSdp-arm	str	x30, [x29, #CPU_DATA_PMF_TS0_OFFSET]
106872be88aSdp-arm	ldr	x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29]
107872be88aSdp-arm#endif
108872be88aSdp-arm
109dce74b89SAchin Gupta	mrs	x30, esr_el3
110dce74b89SAchin Gupta	ubfx	x30, x30, #ESR_EC_SHIFT, #ESR_EC_LENGTH
111dce74b89SAchin Gupta
112a6ef4393SDouglas Raillard	/* Handle SMC exceptions separately from other synchronous exceptions */
113dce74b89SAchin Gupta	cmp	x30, #EC_AARCH32_SMC
114dce74b89SAchin Gupta	b.eq	smc_handler32
115dce74b89SAchin Gupta
116dce74b89SAchin Gupta	cmp	x30, #EC_AARCH64_SMC
117dce74b89SAchin Gupta	b.eq	smc_handler64
118dce74b89SAchin Gupta
119df8f3188SJeenu Viswambharan	/* Synchronous exceptions other than the above are assumed to be EA */
1204d91838bSJulius Werner	ldr	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
121df8f3188SJeenu Viswambharan	b	enter_lower_el_sync_ea
122dce74b89SAchin Gupta	.endm
123dce74b89SAchin Gupta
124dce74b89SAchin Gupta
125a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
126a6ef4393SDouglas Raillard	 * This macro handles FIQ or IRQ interrupts i.e. EL3, S-EL1 and NS
127a6ef4393SDouglas Raillard	 * interrupts.
128a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
129dce74b89SAchin Gupta	 */
130dce74b89SAchin Gupta	.macro	handle_interrupt_exception label
1315283962eSAntonio Nino Diaz
132dce74b89SAchin Gupta	bl	save_gp_registers
1335283962eSAntonio Nino Diaz
134e290a8fcSAlexei Fedorov	/*
135e290a8fcSAlexei Fedorov	 * If Secure Cycle Counter is not disabled in MDCR_EL3
136e290a8fcSAlexei Fedorov	 * when ARMv8.5-PMU is implemented, save PMCR_EL0 and
137e290a8fcSAlexei Fedorov	 * disable all event counters and cycle counter.
138e290a8fcSAlexei Fedorov	 */
139e290a8fcSAlexei Fedorov	bl	save_pmcr_disable_pmu
140e290a8fcSAlexei Fedorov
141b86048c4SAntonio Nino Diaz	/* Save ARMv8.3-PAuth registers and load firmware key */
1425283962eSAntonio Nino Diaz#if CTX_INCLUDE_PAUTH_REGS
1435283962eSAntonio Nino Diaz	bl	pauth_context_save
1445283962eSAntonio Nino Diaz#endif
145b86048c4SAntonio Nino Diaz#if ENABLE_PAUTH
146b86048c4SAntonio Nino Diaz	bl	pauth_load_bl_apiakey
147b86048c4SAntonio Nino Diaz#endif
1485283962eSAntonio Nino Diaz
149a6ef4393SDouglas Raillard	/* Save the EL3 system registers needed to return from this exception */
1505717aae1SAchin Gupta	mrs	x0, spsr_el3
1515717aae1SAchin Gupta	mrs	x1, elr_el3
1525717aae1SAchin Gupta	stp	x0, x1, [sp, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
1535717aae1SAchin Gupta
154dce74b89SAchin Gupta	/* Switch to the runtime stack i.e. SP_EL0 */
155dce74b89SAchin Gupta	ldr	x2, [sp, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
156dce74b89SAchin Gupta	mov	x20, sp
157dce74b89SAchin Gupta	msr	spsel, #0
158dce74b89SAchin Gupta	mov	sp, x2
159dce74b89SAchin Gupta
160dce74b89SAchin Gupta	/*
161a6ef4393SDouglas Raillard	 * Find out whether this is a valid interrupt type.
162a6ef4393SDouglas Raillard	 * If the interrupt controller reports a spurious interrupt then return
163a6ef4393SDouglas Raillard	 * to where we came from.
164dce74b89SAchin Gupta	 */
1659865ac15SDan Handley	bl	plat_ic_get_pending_interrupt_type
166dce74b89SAchin Gupta	cmp	x0, #INTR_TYPE_INVAL
167dce74b89SAchin Gupta	b.eq	interrupt_exit_\label
168dce74b89SAchin Gupta
169dce74b89SAchin Gupta	/*
170a6ef4393SDouglas Raillard	 * Get the registered handler for this interrupt type.
171a6ef4393SDouglas Raillard	 * A NULL return value could be 'cause of the following conditions:
1725717aae1SAchin Gupta	 *
173a6ef4393SDouglas Raillard	 * a. An interrupt of a type was routed correctly but a handler for its
174a6ef4393SDouglas Raillard	 *    type was not registered.
1755717aae1SAchin Gupta	 *
176a6ef4393SDouglas Raillard	 * b. An interrupt of a type was not routed correctly so a handler for
177a6ef4393SDouglas Raillard	 *    its type was not registered.
1785717aae1SAchin Gupta	 *
179a6ef4393SDouglas Raillard	 * c. An interrupt of a type was routed correctly to EL3, but was
180a6ef4393SDouglas Raillard	 *    deasserted before its pending state could be read. Another
181a6ef4393SDouglas Raillard	 *    interrupt of a different type pended at the same time and its
182a6ef4393SDouglas Raillard	 *    type was reported as pending instead. However, a handler for this
183a6ef4393SDouglas Raillard	 *    type was not registered.
1845717aae1SAchin Gupta	 *
185a6ef4393SDouglas Raillard	 * a. and b. can only happen due to a programming error. The
186a6ef4393SDouglas Raillard	 * occurrence of c. could be beyond the control of Trusted Firmware.
187a6ef4393SDouglas Raillard	 * It makes sense to return from this exception instead of reporting an
188a6ef4393SDouglas Raillard	 * error.
189dce74b89SAchin Gupta	 */
190dce74b89SAchin Gupta	bl	get_interrupt_type_handler
1915717aae1SAchin Gupta	cbz	x0, interrupt_exit_\label
192dce74b89SAchin Gupta	mov	x21, x0
193dce74b89SAchin Gupta
194dce74b89SAchin Gupta	mov	x0, #INTR_ID_UNAVAILABLE
195dce74b89SAchin Gupta
196dce74b89SAchin Gupta	/* Set the current security state in the 'flags' parameter */
197dce74b89SAchin Gupta	mrs	x2, scr_el3
198dce74b89SAchin Gupta	ubfx	x1, x2, #0, #1
199dce74b89SAchin Gupta
200dce74b89SAchin Gupta	/* Restore the reference to the 'handle' i.e. SP_EL3 */
201dce74b89SAchin Gupta	mov	x2, x20
202dce74b89SAchin Gupta
203b460b8bfSSoby Mathew	/* x3 will point to a cookie (not used now) */
204b460b8bfSSoby Mathew	mov	x3, xzr
205b460b8bfSSoby Mathew
206dce74b89SAchin Gupta	/* Call the interrupt type handler */
207dce74b89SAchin Gupta	blr	x21
208dce74b89SAchin Gupta
209dce74b89SAchin Guptainterrupt_exit_\label:
210dce74b89SAchin Gupta	/* Return from exception, possibly in a different security state */
211dce74b89SAchin Gupta	b	el3_exit
212dce74b89SAchin Gupta
213dce74b89SAchin Gupta	.endm
214dce74b89SAchin Gupta
215dce74b89SAchin Gupta
216e0ae9fabSSandrine Bailleuxvector_base runtime_exceptions
217e0ae9fabSSandrine Bailleux
218a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
219a6ef4393SDouglas Raillard	 * Current EL with SP_EL0 : 0x0 - 0x200
220a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
2214f6ad66aSAchin Gupta	 */
222e0ae9fabSSandrine Bailleuxvector_entry sync_exception_sp_el0
223*1f461979SJustin Chadwell#ifdef MONITOR_TRAPS
224*1f461979SJustin Chadwell	stp x29, x30, [sp, #-16]!
225*1f461979SJustin Chadwell
226*1f461979SJustin Chadwell	mrs	x30, esr_el3
227*1f461979SJustin Chadwell	ubfx	x30, x30, #ESR_EC_SHIFT, #ESR_EC_LENGTH
228*1f461979SJustin Chadwell
229*1f461979SJustin Chadwell	/* Check for BRK */
230*1f461979SJustin Chadwell	cmp	x30, #EC_BRK
231*1f461979SJustin Chadwell	b.eq	brk_handler
232*1f461979SJustin Chadwell
233*1f461979SJustin Chadwell	ldp x29, x30, [sp], #16
234*1f461979SJustin Chadwell#endif /* MONITOR_TRAPS */
235*1f461979SJustin Chadwell
236a6ef4393SDouglas Raillard	/* We don't expect any synchronous exceptions from EL3 */
2374d91838bSJulius Werner	b	report_unhandled_exception
238a9203edaSRoberto Vargasend_vector_entry sync_exception_sp_el0
2394f6ad66aSAchin Gupta
240e0ae9fabSSandrine Bailleuxvector_entry irq_sp_el0
241a6ef4393SDouglas Raillard	/*
242a6ef4393SDouglas Raillard	 * EL3 code is non-reentrant. Any asynchronous exception is a serious
243a6ef4393SDouglas Raillard	 * error. Loop infinitely.
244a6ef4393SDouglas Raillard	 */
2454d91838bSJulius Werner	b	report_unhandled_interrupt
246a9203edaSRoberto Vargasend_vector_entry irq_sp_el0
2474f6ad66aSAchin Gupta
248e0ae9fabSSandrine Bailleux
249e0ae9fabSSandrine Bailleuxvector_entry fiq_sp_el0
2504d91838bSJulius Werner	b	report_unhandled_interrupt
251a9203edaSRoberto Vargasend_vector_entry fiq_sp_el0
2524f6ad66aSAchin Gupta
253e0ae9fabSSandrine Bailleux
254e0ae9fabSSandrine Bailleuxvector_entry serror_sp_el0
255eaeaa4d0SJeenu Viswambharan	no_ret	plat_handle_el3_ea
256a9203edaSRoberto Vargasend_vector_entry serror_sp_el0
2574f6ad66aSAchin Gupta
258a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
259a6ef4393SDouglas Raillard	 * Current EL with SP_ELx: 0x200 - 0x400
260a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
2614f6ad66aSAchin Gupta	 */
262e0ae9fabSSandrine Bailleuxvector_entry sync_exception_sp_elx
263a6ef4393SDouglas Raillard	/*
264a6ef4393SDouglas Raillard	 * This exception will trigger if anything went wrong during a previous
265a6ef4393SDouglas Raillard	 * exception entry or exit or while handling an earlier unexpected
266a6ef4393SDouglas Raillard	 * synchronous exception. There is a high probability that SP_EL3 is
267a6ef4393SDouglas Raillard	 * corrupted.
268caa84939SJeenu Viswambharan	 */
2694d91838bSJulius Werner	b	report_unhandled_exception
270a9203edaSRoberto Vargasend_vector_entry sync_exception_sp_elx
2714f6ad66aSAchin Gupta
272e0ae9fabSSandrine Bailleuxvector_entry irq_sp_elx
2734d91838bSJulius Werner	b	report_unhandled_interrupt
274a9203edaSRoberto Vargasend_vector_entry irq_sp_elx
275a7934d69SJeenu Viswambharan
276e0ae9fabSSandrine Bailleuxvector_entry fiq_sp_elx
2774d91838bSJulius Werner	b	report_unhandled_interrupt
278a9203edaSRoberto Vargasend_vector_entry fiq_sp_elx
279a7934d69SJeenu Viswambharan
280e0ae9fabSSandrine Bailleuxvector_entry serror_sp_elx
281eaeaa4d0SJeenu Viswambharan	no_ret	plat_handle_el3_ea
282a9203edaSRoberto Vargasend_vector_entry serror_sp_elx
2834f6ad66aSAchin Gupta
284a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
28544804252SSandrine Bailleux	 * Lower EL using AArch64 : 0x400 - 0x600
286a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
2874f6ad66aSAchin Gupta	 */
288e0ae9fabSSandrine Bailleuxvector_entry sync_exception_aarch64
289a6ef4393SDouglas Raillard	/*
290a6ef4393SDouglas Raillard	 * This exception vector will be the entry point for SMCs and traps
291a6ef4393SDouglas Raillard	 * that are unhandled at lower ELs most commonly. SP_EL3 should point
292a6ef4393SDouglas Raillard	 * to a valid cpu context where the general purpose and system register
293a6ef4393SDouglas Raillard	 * state can be saved.
294caa84939SJeenu Viswambharan	 */
29514c6016aSJeenu Viswambharan	check_and_unmask_ea
296caa84939SJeenu Viswambharan	handle_sync_exception
297a9203edaSRoberto Vargasend_vector_entry sync_exception_aarch64
2984f6ad66aSAchin Gupta
299e0ae9fabSSandrine Bailleuxvector_entry irq_aarch64
30014c6016aSJeenu Viswambharan	check_and_unmask_ea
301dce74b89SAchin Gupta	handle_interrupt_exception irq_aarch64
302a9203edaSRoberto Vargasend_vector_entry irq_aarch64
3034f6ad66aSAchin Gupta
304e0ae9fabSSandrine Bailleuxvector_entry fiq_aarch64
30514c6016aSJeenu Viswambharan	check_and_unmask_ea
306dce74b89SAchin Gupta	handle_interrupt_exception fiq_aarch64
307a9203edaSRoberto Vargasend_vector_entry fiq_aarch64
3084f6ad66aSAchin Gupta
309e0ae9fabSSandrine Bailleuxvector_entry serror_aarch64
31076454abfSJeenu Viswambharan	msr	daifclr, #DAIF_ABT_BIT
311df8f3188SJeenu Viswambharan	b	enter_lower_el_async_ea
312a9203edaSRoberto Vargasend_vector_entry serror_aarch64
3134f6ad66aSAchin Gupta
314a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
31544804252SSandrine Bailleux	 * Lower EL using AArch32 : 0x600 - 0x800
316a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
3174f6ad66aSAchin Gupta	 */
318e0ae9fabSSandrine Bailleuxvector_entry sync_exception_aarch32
319a6ef4393SDouglas Raillard	/*
320a6ef4393SDouglas Raillard	 * This exception vector will be the entry point for SMCs and traps
321a6ef4393SDouglas Raillard	 * that are unhandled at lower ELs most commonly. SP_EL3 should point
322a6ef4393SDouglas Raillard	 * to a valid cpu context where the general purpose and system register
323a6ef4393SDouglas Raillard	 * state can be saved.
324caa84939SJeenu Viswambharan	 */
32514c6016aSJeenu Viswambharan	check_and_unmask_ea
326caa84939SJeenu Viswambharan	handle_sync_exception
327a9203edaSRoberto Vargasend_vector_entry sync_exception_aarch32
3284f6ad66aSAchin Gupta
329e0ae9fabSSandrine Bailleuxvector_entry irq_aarch32
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
33514c6016aSJeenu Viswambharan	check_and_unmask_ea
336dce74b89SAchin Gupta	handle_interrupt_exception fiq_aarch32
337a9203edaSRoberto Vargasend_vector_entry fiq_aarch32
3384f6ad66aSAchin Gupta
339e0ae9fabSSandrine Bailleuxvector_entry serror_aarch32
34076454abfSJeenu Viswambharan	msr	daifclr, #DAIF_ABT_BIT
341df8f3188SJeenu Viswambharan	b	enter_lower_el_async_ea
342a9203edaSRoberto Vargasend_vector_entry serror_aarch32
343a7934d69SJeenu Viswambharan
344*1f461979SJustin Chadwell#ifdef MONITOR_TRAPS
345*1f461979SJustin Chadwell	.section .rodata.brk_string, "aS"
346*1f461979SJustin Chadwellbrk_location:
347*1f461979SJustin Chadwell	.asciz "Error at instruction 0x"
348*1f461979SJustin Chadwellbrk_message:
349*1f461979SJustin Chadwell	.asciz "Unexpected BRK instruction with value 0x"
350*1f461979SJustin Chadwell#endif /* MONITOR_TRAPS */
351*1f461979SJustin Chadwell
3522f370465SAntonio Nino Diaz	/* ---------------------------------------------------------------------
353caa84939SJeenu Viswambharan	 * The following code handles secure monitor calls.
354a6ef4393SDouglas Raillard	 * Depending upon the execution state from where the SMC has been
355a6ef4393SDouglas Raillard	 * invoked, it frees some general purpose registers to perform the
356a6ef4393SDouglas Raillard	 * remaining tasks. They involve finding the runtime service handler
357a6ef4393SDouglas Raillard	 * that is the target of the SMC & switching to runtime stacks (SP_EL0)
358a6ef4393SDouglas Raillard	 * before calling the handler.
359caa84939SJeenu Viswambharan	 *
360a6ef4393SDouglas Raillard	 * Note that x30 has been explicitly saved and can be used here
361a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
362caa84939SJeenu Viswambharan	 */
3630a30cf54SAndrew Thoelkefunc smc_handler
364caa84939SJeenu Viswambharansmc_handler32:
365caa84939SJeenu Viswambharan	/* Check whether aarch32 issued an SMC64 */
366caa84939SJeenu Viswambharan	tbnz	x0, #FUNCID_CC_SHIFT, smc_prohibited
367caa84939SJeenu Viswambharan
368caa84939SJeenu Viswambharansmc_handler64:
3695283962eSAntonio Nino Diaz	/* NOTE: The code below must preserve x0-x4 */
3705283962eSAntonio Nino Diaz
3715283962eSAntonio Nino Diaz	/* Save general purpose registers */
3725283962eSAntonio Nino Diaz	bl	save_gp_registers
3735283962eSAntonio Nino Diaz
374e290a8fcSAlexei Fedorov	/*
375e290a8fcSAlexei Fedorov	 * If Secure Cycle Counter is not disabled in MDCR_EL3
376e290a8fcSAlexei Fedorov	 * when ARMv8.5-PMU is implemented, save PMCR_EL0 and
377e290a8fcSAlexei Fedorov	 * disable all event counters and cycle counter.
378e290a8fcSAlexei Fedorov	 */
379e290a8fcSAlexei Fedorov	bl	save_pmcr_disable_pmu
380e290a8fcSAlexei Fedorov
381b86048c4SAntonio Nino Diaz	/* Save ARMv8.3-PAuth registers and load firmware key */
3825283962eSAntonio Nino Diaz#if CTX_INCLUDE_PAUTH_REGS
3835283962eSAntonio Nino Diaz	bl	pauth_context_save
3845283962eSAntonio Nino Diaz#endif
385b86048c4SAntonio Nino Diaz#if ENABLE_PAUTH
386b86048c4SAntonio Nino Diaz	bl	pauth_load_bl_apiakey
387b86048c4SAntonio Nino Diaz#endif
3885283962eSAntonio Nino Diaz
389a6ef4393SDouglas Raillard	/*
390a6ef4393SDouglas Raillard	 * Populate the parameters for the SMC handler.
391a6ef4393SDouglas Raillard	 * We already have x0-x4 in place. x5 will point to a cookie (not used
392a6ef4393SDouglas Raillard	 * now). x6 will point to the context structure (SP_EL3) and x7 will
393201ca5b6SDimitris Papastamos	 * contain flags we need to pass to the handler.
394caa84939SJeenu Viswambharan	 */
395caa84939SJeenu Viswambharan	mov	x5, xzr
396caa84939SJeenu Viswambharan	mov	x6, sp
397caa84939SJeenu Viswambharan
398a6ef4393SDouglas Raillard	/*
399a6ef4393SDouglas Raillard	 * Restore the saved C runtime stack value which will become the new
400a6ef4393SDouglas Raillard	 * SP_EL0 i.e. EL3 runtime stack. It was saved in the 'cpu_context'
401a6ef4393SDouglas Raillard	 * structure prior to the last ERET from EL3.
402caa84939SJeenu Viswambharan	 */
403caa84939SJeenu Viswambharan	ldr	x12, [x6, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
404caa84939SJeenu Viswambharan
405caa84939SJeenu Viswambharan	/* Switch to SP_EL0 */
406caa84939SJeenu Viswambharan	msr	spsel, #0
407caa84939SJeenu Viswambharan
408a6ef4393SDouglas Raillard	/*
409a6ef4393SDouglas Raillard	 * Save the SPSR_EL3, ELR_EL3, & SCR_EL3 in case there is a world
410a6ef4393SDouglas Raillard	 * switch during SMC handling.
411a6ef4393SDouglas Raillard	 * TODO: Revisit if all system registers can be saved later.
412caa84939SJeenu Viswambharan	 */
413caa84939SJeenu Viswambharan	mrs	x16, spsr_el3
414caa84939SJeenu Viswambharan	mrs	x17, elr_el3
415caa84939SJeenu Viswambharan	mrs	x18, scr_el3
416caa84939SJeenu Viswambharan	stp	x16, x17, [x6, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
417b51da821SAchin Gupta	str	x18, [x6, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3]
418caa84939SJeenu Viswambharan
419caa84939SJeenu Viswambharan	/* Copy SCR_EL3.NS bit to the flag to indicate caller's security */
420caa84939SJeenu Viswambharan	bfi	x7, x18, #0, #1
421caa84939SJeenu Viswambharan
422caa84939SJeenu Viswambharan	mov	sp, x12
423caa84939SJeenu Viswambharan
424cc485e27SMadhukar Pappireddy	/* Get the unique owning entity number */
425cc485e27SMadhukar Pappireddy	ubfx	x16, x0, #FUNCID_OEN_SHIFT, #FUNCID_OEN_WIDTH
426cc485e27SMadhukar Pappireddy	ubfx	x15, x0, #FUNCID_TYPE_SHIFT, #FUNCID_TYPE_WIDTH
427cc485e27SMadhukar Pappireddy	orr	x16, x16, x15, lsl #FUNCID_OEN_WIDTH
428cc485e27SMadhukar Pappireddy
429cc485e27SMadhukar Pappireddy	/* Load descriptor index from array of indices */
430cc485e27SMadhukar Pappireddy	adr	x14, rt_svc_descs_indices
431cc485e27SMadhukar Pappireddy	ldrb	w15, [x14, x16]
432cc485e27SMadhukar Pappireddy
433cc485e27SMadhukar Pappireddy	/* Any index greater than 127 is invalid. Check bit 7. */
434cc485e27SMadhukar Pappireddy	tbnz	w15, 7, smc_unknown
435cc485e27SMadhukar Pappireddy
436cc485e27SMadhukar Pappireddy	/*
437cc485e27SMadhukar Pappireddy	 * Get the descriptor using the index
438cc485e27SMadhukar Pappireddy	 * x11 = (base + off), w15 = index
439cc485e27SMadhukar Pappireddy	 *
440cc485e27SMadhukar Pappireddy	 * handler = (base + off) + (index << log2(size))
441cc485e27SMadhukar Pappireddy	 */
442cc485e27SMadhukar Pappireddy	adr	x11, (__RT_SVC_DESCS_START__ + RT_SVC_DESC_HANDLE)
443cc485e27SMadhukar Pappireddy	lsl	w10, w15, #RT_SVC_SIZE_LOG2
444cc485e27SMadhukar Pappireddy	ldr	x15, [x11, w10, uxtw]
445cc485e27SMadhukar Pappireddy
446a6ef4393SDouglas Raillard	/*
447a6ef4393SDouglas Raillard	 * Call the Secure Monitor Call handler and then drop directly into
448a6ef4393SDouglas Raillard	 * el3_exit() which will program any remaining architectural state
449a6ef4393SDouglas Raillard	 * prior to issuing the ERET to the desired lower EL.
450caa84939SJeenu Viswambharan	 */
451caa84939SJeenu Viswambharan#if DEBUG
452caa84939SJeenu Viswambharan	cbz	x15, rt_svc_fw_critical_error
453caa84939SJeenu Viswambharan#endif
454caa84939SJeenu Viswambharan	blr	x15
455caa84939SJeenu Viswambharan
456bbf8f6f9SYatharth Kochar	b	el3_exit
4574f6ad66aSAchin Gupta
458caa84939SJeenu Viswambharansmc_unknown:
459caa84939SJeenu Viswambharan	/*
460cc485e27SMadhukar Pappireddy	 * Unknown SMC call. Populate return value with SMC_UNK and call
461cc485e27SMadhukar Pappireddy	 * el3_exit() which will restore the remaining architectural state
462cc485e27SMadhukar Pappireddy	 * i.e., SYS, GP and PAuth registers(if any) prior to issuing the ERET
463cc485e27SMadhukar Pappireddy         * to the desired lower EL.
464caa84939SJeenu Viswambharan	 */
4654abd7fa7SAntonio Nino Diaz	mov	x0, #SMC_UNK
466cc485e27SMadhukar Pappireddy	str	x0, [x6, #CTX_GPREGS_OFFSET + CTX_GPREG_X0]
467cc485e27SMadhukar Pappireddy	b	el3_exit
468caa84939SJeenu Viswambharan
469caa84939SJeenu Viswambharansmc_prohibited:
470c3260f9bSSoby Mathew	ldr	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
4714abd7fa7SAntonio Nino Diaz	mov	x0, #SMC_UNK
472caa84939SJeenu Viswambharan	eret
473caa84939SJeenu Viswambharan
474caa84939SJeenu Viswambharanrt_svc_fw_critical_error:
475a6ef4393SDouglas Raillard	/* Switch to SP_ELx */
476a6ef4393SDouglas Raillard	msr	spsel, #1
477a806dad5SJeenu Viswambharan	no_ret	report_unhandled_exception
4788b779620SKévin Petitendfunc smc_handler
479*1f461979SJustin Chadwell
480*1f461979SJustin Chadwell	/* ---------------------------------------------------------------------
481*1f461979SJustin Chadwell	 * The following code handles exceptions caused by BRK instructions.
482*1f461979SJustin Chadwell	 * Following a BRK instruction, the only real valid cause of action is
483*1f461979SJustin Chadwell	 * to print some information and panic, as the code that caused it is
484*1f461979SJustin Chadwell	 * likely in an inconsistent internal state.
485*1f461979SJustin Chadwell	 *
486*1f461979SJustin Chadwell	 * This is initially intended to be used in conjunction with
487*1f461979SJustin Chadwell	 * __builtin_trap.
488*1f461979SJustin Chadwell	 * ---------------------------------------------------------------------
489*1f461979SJustin Chadwell	 */
490*1f461979SJustin Chadwell#ifdef MONITOR_TRAPS
491*1f461979SJustin Chadwellfunc brk_handler
492*1f461979SJustin Chadwell	/* Extract the ISS */
493*1f461979SJustin Chadwell	mrs	x10, esr_el3
494*1f461979SJustin Chadwell	ubfx	x10, x10, #ESR_ISS_SHIFT, #ESR_ISS_LENGTH
495*1f461979SJustin Chadwell
496*1f461979SJustin Chadwell	/* Ensure the console is initialized */
497*1f461979SJustin Chadwell	bl	plat_crash_console_init
498*1f461979SJustin Chadwell
499*1f461979SJustin Chadwell	adr	x4, brk_location
500*1f461979SJustin Chadwell	bl	asm_print_str
501*1f461979SJustin Chadwell	mrs	x4, elr_el3
502*1f461979SJustin Chadwell	bl	asm_print_hex
503*1f461979SJustin Chadwell	bl	asm_print_newline
504*1f461979SJustin Chadwell
505*1f461979SJustin Chadwell	adr	x4, brk_message
506*1f461979SJustin Chadwell	bl	asm_print_str
507*1f461979SJustin Chadwell	mov	x4, x10
508*1f461979SJustin Chadwell	mov	x5, #28
509*1f461979SJustin Chadwell	bl	asm_print_hex_bits
510*1f461979SJustin Chadwell	bl	asm_print_newline
511*1f461979SJustin Chadwell
512*1f461979SJustin Chadwell	no_ret	plat_panic_handler
513*1f461979SJustin Chadwellendfunc brk_handler
514*1f461979SJustin Chadwell#endif /* MONITOR_TRAPS */
515