xref: /rk3399_ARM-atf/bl31/aarch64/runtime_exceptions.S (revision f461fe346b728d0e88142fd7b8f2816415af18bc)
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
68e290a8fcSAlexei Fedorov	/*
69ed108b56SAlexei Fedorov	 * Save general purpose and ARMv8.3-PAuth registers (if enabled).
70ed108b56SAlexei Fedorov	 * If Secure Cycle Counter is not disabled in MDCR_EL3 when
71ed108b56SAlexei Fedorov	 * ARMv8.5-PMU is implemented, save PMCR_EL0 and disable Cycle Counter.
72e290a8fcSAlexei Fedorov	 */
73ed108b56SAlexei Fedorov	bl	save_gp_pmcr_pauth_regs
74e290a8fcSAlexei Fedorov
75df8f3188SJeenu Viswambharan	bl	handle_lower_el_ea_esb
7614c6016aSJeenu Viswambharan
77ed108b56SAlexei Fedorov	/* Restore general purpose, PMCR_EL0 and ARMv8.3-PAuth registers */
78ed108b56SAlexei Fedorov	bl	restore_gp_pmcr_pauth_regs
7914c6016aSJeenu Viswambharan1:
8014c6016aSJeenu Viswambharan#else
8114c6016aSJeenu Viswambharan	/* Unmask the SError interrupt */
8214c6016aSJeenu Viswambharan	msr	daifclr, #DAIF_ABT_BIT
8314c6016aSJeenu Viswambharan
8414c6016aSJeenu Viswambharan	str	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
8514c6016aSJeenu Viswambharan#endif
8614c6016aSJeenu Viswambharan	.endm
8714c6016aSJeenu Viswambharan
88a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
89a6ef4393SDouglas Raillard	 * This macro handles Synchronous exceptions.
90a6ef4393SDouglas Raillard	 * Only SMC exceptions are supported.
91a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
92dce74b89SAchin Gupta	 */
93dce74b89SAchin Gupta	.macro	handle_sync_exception
94872be88aSdp-arm#if ENABLE_RUNTIME_INSTRUMENTATION
95872be88aSdp-arm	/*
96a6ef4393SDouglas Raillard	 * Read the timestamp value and store it in per-cpu data. The value
97a6ef4393SDouglas Raillard	 * will be extracted from per-cpu data by the C level SMC handler and
98a6ef4393SDouglas Raillard	 * saved to the PMF timestamp region.
99872be88aSdp-arm	 */
100872be88aSdp-arm	mrs	x30, cntpct_el0
101872be88aSdp-arm	str	x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29]
102872be88aSdp-arm	mrs	x29, tpidr_el3
103872be88aSdp-arm	str	x30, [x29, #CPU_DATA_PMF_TS0_OFFSET]
104872be88aSdp-arm	ldr	x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29]
105872be88aSdp-arm#endif
106872be88aSdp-arm
107dce74b89SAchin Gupta	mrs	x30, esr_el3
108dce74b89SAchin Gupta	ubfx	x30, x30, #ESR_EC_SHIFT, #ESR_EC_LENGTH
109dce74b89SAchin Gupta
110a6ef4393SDouglas Raillard	/* Handle SMC exceptions separately from other synchronous exceptions */
111dce74b89SAchin Gupta	cmp	x30, #EC_AARCH32_SMC
112dce74b89SAchin Gupta	b.eq	smc_handler32
113dce74b89SAchin Gupta
114dce74b89SAchin Gupta	cmp	x30, #EC_AARCH64_SMC
115dce74b89SAchin Gupta	b.eq	smc_handler64
116dce74b89SAchin Gupta
117df8f3188SJeenu Viswambharan	/* Synchronous exceptions other than the above are assumed to be EA */
1184d91838bSJulius Werner	ldr	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
119df8f3188SJeenu Viswambharan	b	enter_lower_el_sync_ea
120dce74b89SAchin Gupta	.endm
121dce74b89SAchin Gupta
122dce74b89SAchin Gupta
123a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
124a6ef4393SDouglas Raillard	 * This macro handles FIQ or IRQ interrupts i.e. EL3, S-EL1 and NS
125a6ef4393SDouglas Raillard	 * interrupts.
126a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
127dce74b89SAchin Gupta	 */
128dce74b89SAchin Gupta	.macro	handle_interrupt_exception label
1295283962eSAntonio Nino Diaz
130e290a8fcSAlexei Fedorov	/*
131ed108b56SAlexei Fedorov	 * Save general purpose and ARMv8.3-PAuth registers (if enabled).
132ed108b56SAlexei Fedorov	 * If Secure Cycle Counter is not disabled in MDCR_EL3 when
133ed108b56SAlexei Fedorov	 * ARMv8.5-PMU is implemented, save PMCR_EL0 and disable Cycle Counter.
134e290a8fcSAlexei Fedorov	 */
135ed108b56SAlexei Fedorov	bl	save_gp_pmcr_pauth_regs
136e290a8fcSAlexei Fedorov
137b86048c4SAntonio Nino Diaz#if ENABLE_PAUTH
138ed108b56SAlexei Fedorov	/* Load and program APIAKey firmware key */
139ed108b56SAlexei Fedorov	bl	pauth_load_bl31_apiakey
140b86048c4SAntonio Nino Diaz#endif
1415283962eSAntonio Nino Diaz
142a6ef4393SDouglas Raillard	/* Save the EL3 system registers needed to return from this exception */
1435717aae1SAchin Gupta	mrs	x0, spsr_el3
1445717aae1SAchin Gupta	mrs	x1, elr_el3
1455717aae1SAchin Gupta	stp	x0, x1, [sp, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
1465717aae1SAchin Gupta
147dce74b89SAchin Gupta	/* Switch to the runtime stack i.e. SP_EL0 */
148dce74b89SAchin Gupta	ldr	x2, [sp, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
149dce74b89SAchin Gupta	mov	x20, sp
150ed108b56SAlexei Fedorov	msr	spsel, #MODE_SP_EL0
151dce74b89SAchin Gupta	mov	sp, x2
152dce74b89SAchin Gupta
153dce74b89SAchin Gupta	/*
154a6ef4393SDouglas Raillard	 * Find out whether this is a valid interrupt type.
155a6ef4393SDouglas Raillard	 * If the interrupt controller reports a spurious interrupt then return
156a6ef4393SDouglas Raillard	 * to where we came from.
157dce74b89SAchin Gupta	 */
1589865ac15SDan Handley	bl	plat_ic_get_pending_interrupt_type
159dce74b89SAchin Gupta	cmp	x0, #INTR_TYPE_INVAL
160dce74b89SAchin Gupta	b.eq	interrupt_exit_\label
161dce74b89SAchin Gupta
162dce74b89SAchin Gupta	/*
163a6ef4393SDouglas Raillard	 * Get the registered handler for this interrupt type.
164a6ef4393SDouglas Raillard	 * A NULL return value could be 'cause of the following conditions:
1655717aae1SAchin Gupta	 *
166a6ef4393SDouglas Raillard	 * a. An interrupt of a type was routed correctly but a handler for its
167a6ef4393SDouglas Raillard	 *    type was not registered.
1685717aae1SAchin Gupta	 *
169a6ef4393SDouglas Raillard	 * b. An interrupt of a type was not routed correctly so a handler for
170a6ef4393SDouglas Raillard	 *    its type was not registered.
1715717aae1SAchin Gupta	 *
172a6ef4393SDouglas Raillard	 * c. An interrupt of a type was routed correctly to EL3, but was
173a6ef4393SDouglas Raillard	 *    deasserted before its pending state could be read. Another
174a6ef4393SDouglas Raillard	 *    interrupt of a different type pended at the same time and its
175a6ef4393SDouglas Raillard	 *    type was reported as pending instead. However, a handler for this
176a6ef4393SDouglas Raillard	 *    type was not registered.
1775717aae1SAchin Gupta	 *
178a6ef4393SDouglas Raillard	 * a. and b. can only happen due to a programming error. The
179a6ef4393SDouglas Raillard	 * occurrence of c. could be beyond the control of Trusted Firmware.
180a6ef4393SDouglas Raillard	 * It makes sense to return from this exception instead of reporting an
181a6ef4393SDouglas Raillard	 * error.
182dce74b89SAchin Gupta	 */
183dce74b89SAchin Gupta	bl	get_interrupt_type_handler
1845717aae1SAchin Gupta	cbz	x0, interrupt_exit_\label
185dce74b89SAchin Gupta	mov	x21, x0
186dce74b89SAchin Gupta
187dce74b89SAchin Gupta	mov	x0, #INTR_ID_UNAVAILABLE
188dce74b89SAchin Gupta
189dce74b89SAchin Gupta	/* Set the current security state in the 'flags' parameter */
190dce74b89SAchin Gupta	mrs	x2, scr_el3
191dce74b89SAchin Gupta	ubfx	x1, x2, #0, #1
192dce74b89SAchin Gupta
193dce74b89SAchin Gupta	/* Restore the reference to the 'handle' i.e. SP_EL3 */
194dce74b89SAchin Gupta	mov	x2, x20
195dce74b89SAchin Gupta
196b460b8bfSSoby Mathew	/* x3 will point to a cookie (not used now) */
197b460b8bfSSoby Mathew	mov	x3, xzr
198b460b8bfSSoby Mathew
199dce74b89SAchin Gupta	/* Call the interrupt type handler */
200dce74b89SAchin Gupta	blr	x21
201dce74b89SAchin Gupta
202dce74b89SAchin Guptainterrupt_exit_\label:
203dce74b89SAchin Gupta	/* Return from exception, possibly in a different security state */
204dce74b89SAchin Gupta	b	el3_exit
205dce74b89SAchin Gupta
206dce74b89SAchin Gupta	.endm
207dce74b89SAchin Gupta
208dce74b89SAchin Gupta
209e0ae9fabSSandrine Bailleuxvector_base runtime_exceptions
210e0ae9fabSSandrine Bailleux
211a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
212a6ef4393SDouglas Raillard	 * Current EL with SP_EL0 : 0x0 - 0x200
213a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
2144f6ad66aSAchin Gupta	 */
215e0ae9fabSSandrine Bailleuxvector_entry sync_exception_sp_el0
2161f461979SJustin Chadwell#ifdef MONITOR_TRAPS
2171f461979SJustin Chadwell	stp x29, x30, [sp, #-16]!
2181f461979SJustin Chadwell
2191f461979SJustin Chadwell	mrs	x30, esr_el3
2201f461979SJustin Chadwell	ubfx	x30, x30, #ESR_EC_SHIFT, #ESR_EC_LENGTH
2211f461979SJustin Chadwell
2221f461979SJustin Chadwell	/* Check for BRK */
2231f461979SJustin Chadwell	cmp	x30, #EC_BRK
2241f461979SJustin Chadwell	b.eq	brk_handler
2251f461979SJustin Chadwell
2261f461979SJustin Chadwell	ldp x29, x30, [sp], #16
2271f461979SJustin Chadwell#endif /* MONITOR_TRAPS */
2281f461979SJustin Chadwell
229a6ef4393SDouglas Raillard	/* We don't expect any synchronous exceptions from EL3 */
2304d91838bSJulius Werner	b	report_unhandled_exception
231a9203edaSRoberto Vargasend_vector_entry sync_exception_sp_el0
2324f6ad66aSAchin Gupta
233e0ae9fabSSandrine Bailleuxvector_entry irq_sp_el0
234a6ef4393SDouglas Raillard	/*
235a6ef4393SDouglas Raillard	 * EL3 code is non-reentrant. Any asynchronous exception is a serious
236a6ef4393SDouglas Raillard	 * error. Loop infinitely.
237a6ef4393SDouglas Raillard	 */
2384d91838bSJulius Werner	b	report_unhandled_interrupt
239a9203edaSRoberto Vargasend_vector_entry irq_sp_el0
2404f6ad66aSAchin Gupta
241e0ae9fabSSandrine Bailleux
242e0ae9fabSSandrine Bailleuxvector_entry fiq_sp_el0
2434d91838bSJulius Werner	b	report_unhandled_interrupt
244a9203edaSRoberto Vargasend_vector_entry fiq_sp_el0
2454f6ad66aSAchin Gupta
246e0ae9fabSSandrine Bailleux
247e0ae9fabSSandrine Bailleuxvector_entry serror_sp_el0
248eaeaa4d0SJeenu Viswambharan	no_ret	plat_handle_el3_ea
249a9203edaSRoberto Vargasend_vector_entry serror_sp_el0
2504f6ad66aSAchin Gupta
251a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
252a6ef4393SDouglas Raillard	 * Current EL with SP_ELx: 0x200 - 0x400
253a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
2544f6ad66aSAchin Gupta	 */
255e0ae9fabSSandrine Bailleuxvector_entry sync_exception_sp_elx
256a6ef4393SDouglas Raillard	/*
257a6ef4393SDouglas Raillard	 * This exception will trigger if anything went wrong during a previous
258a6ef4393SDouglas Raillard	 * exception entry or exit or while handling an earlier unexpected
259a6ef4393SDouglas Raillard	 * synchronous exception. There is a high probability that SP_EL3 is
260a6ef4393SDouglas Raillard	 * corrupted.
261caa84939SJeenu Viswambharan	 */
2624d91838bSJulius Werner	b	report_unhandled_exception
263a9203edaSRoberto Vargasend_vector_entry sync_exception_sp_elx
2644f6ad66aSAchin Gupta
265e0ae9fabSSandrine Bailleuxvector_entry irq_sp_elx
2664d91838bSJulius Werner	b	report_unhandled_interrupt
267a9203edaSRoberto Vargasend_vector_entry irq_sp_elx
268a7934d69SJeenu Viswambharan
269e0ae9fabSSandrine Bailleuxvector_entry fiq_sp_elx
2704d91838bSJulius Werner	b	report_unhandled_interrupt
271a9203edaSRoberto Vargasend_vector_entry fiq_sp_elx
272a7934d69SJeenu Viswambharan
273e0ae9fabSSandrine Bailleuxvector_entry serror_sp_elx
274eaeaa4d0SJeenu Viswambharan	no_ret	plat_handle_el3_ea
275a9203edaSRoberto Vargasend_vector_entry serror_sp_elx
2764f6ad66aSAchin Gupta
277a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
27844804252SSandrine Bailleux	 * Lower EL using AArch64 : 0x400 - 0x600
279a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
2804f6ad66aSAchin Gupta	 */
281e0ae9fabSSandrine Bailleuxvector_entry sync_exception_aarch64
282a6ef4393SDouglas Raillard	/*
283a6ef4393SDouglas Raillard	 * This exception vector will be the entry point for SMCs and traps
284a6ef4393SDouglas Raillard	 * that are unhandled at lower ELs most commonly. SP_EL3 should point
285a6ef4393SDouglas Raillard	 * to a valid cpu context where the general purpose and system register
286a6ef4393SDouglas Raillard	 * state can be saved.
287caa84939SJeenu Viswambharan	 */
28814c6016aSJeenu Viswambharan	check_and_unmask_ea
289caa84939SJeenu Viswambharan	handle_sync_exception
290a9203edaSRoberto Vargasend_vector_entry sync_exception_aarch64
2914f6ad66aSAchin Gupta
292e0ae9fabSSandrine Bailleuxvector_entry irq_aarch64
29314c6016aSJeenu Viswambharan	check_and_unmask_ea
294dce74b89SAchin Gupta	handle_interrupt_exception irq_aarch64
295a9203edaSRoberto Vargasend_vector_entry irq_aarch64
2964f6ad66aSAchin Gupta
297e0ae9fabSSandrine Bailleuxvector_entry fiq_aarch64
29814c6016aSJeenu Viswambharan	check_and_unmask_ea
299dce74b89SAchin Gupta	handle_interrupt_exception fiq_aarch64
300a9203edaSRoberto Vargasend_vector_entry fiq_aarch64
3014f6ad66aSAchin Gupta
302e0ae9fabSSandrine Bailleuxvector_entry serror_aarch64
30376454abfSJeenu Viswambharan	msr	daifclr, #DAIF_ABT_BIT
304df8f3188SJeenu Viswambharan	b	enter_lower_el_async_ea
305a9203edaSRoberto Vargasend_vector_entry serror_aarch64
3064f6ad66aSAchin Gupta
307a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
30844804252SSandrine Bailleux	 * Lower EL using AArch32 : 0x600 - 0x800
309a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
3104f6ad66aSAchin Gupta	 */
311e0ae9fabSSandrine Bailleuxvector_entry sync_exception_aarch32
312a6ef4393SDouglas Raillard	/*
313a6ef4393SDouglas Raillard	 * This exception vector will be the entry point for SMCs and traps
314a6ef4393SDouglas Raillard	 * that are unhandled at lower ELs most commonly. SP_EL3 should point
315a6ef4393SDouglas Raillard	 * to a valid cpu context where the general purpose and system register
316a6ef4393SDouglas Raillard	 * state can be saved.
317caa84939SJeenu Viswambharan	 */
31814c6016aSJeenu Viswambharan	check_and_unmask_ea
319caa84939SJeenu Viswambharan	handle_sync_exception
320a9203edaSRoberto Vargasend_vector_entry sync_exception_aarch32
3214f6ad66aSAchin Gupta
322e0ae9fabSSandrine Bailleuxvector_entry irq_aarch32
32314c6016aSJeenu Viswambharan	check_and_unmask_ea
324dce74b89SAchin Gupta	handle_interrupt_exception irq_aarch32
325a9203edaSRoberto Vargasend_vector_entry irq_aarch32
3264f6ad66aSAchin Gupta
327e0ae9fabSSandrine Bailleuxvector_entry fiq_aarch32
32814c6016aSJeenu Viswambharan	check_and_unmask_ea
329dce74b89SAchin Gupta	handle_interrupt_exception fiq_aarch32
330a9203edaSRoberto Vargasend_vector_entry fiq_aarch32
3314f6ad66aSAchin Gupta
332e0ae9fabSSandrine Bailleuxvector_entry serror_aarch32
33376454abfSJeenu Viswambharan	msr	daifclr, #DAIF_ABT_BIT
334df8f3188SJeenu Viswambharan	b	enter_lower_el_async_ea
335a9203edaSRoberto Vargasend_vector_entry serror_aarch32
336a7934d69SJeenu Viswambharan
3371f461979SJustin Chadwell#ifdef MONITOR_TRAPS
3381f461979SJustin Chadwell	.section .rodata.brk_string, "aS"
3391f461979SJustin Chadwellbrk_location:
3401f461979SJustin Chadwell	.asciz "Error at instruction 0x"
3411f461979SJustin Chadwellbrk_message:
3421f461979SJustin Chadwell	.asciz "Unexpected BRK instruction with value 0x"
3431f461979SJustin Chadwell#endif /* MONITOR_TRAPS */
3441f461979SJustin Chadwell
3452f370465SAntonio Nino Diaz	/* ---------------------------------------------------------------------
346caa84939SJeenu Viswambharan	 * The following code handles secure monitor calls.
347a6ef4393SDouglas Raillard	 * Depending upon the execution state from where the SMC has been
348a6ef4393SDouglas Raillard	 * invoked, it frees some general purpose registers to perform the
349a6ef4393SDouglas Raillard	 * remaining tasks. They involve finding the runtime service handler
350a6ef4393SDouglas Raillard	 * that is the target of the SMC & switching to runtime stacks (SP_EL0)
351a6ef4393SDouglas Raillard	 * before calling the handler.
352caa84939SJeenu Viswambharan	 *
353a6ef4393SDouglas Raillard	 * Note that x30 has been explicitly saved and can be used here
354a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
355caa84939SJeenu Viswambharan	 */
3560a30cf54SAndrew Thoelkefunc smc_handler
357caa84939SJeenu Viswambharansmc_handler32:
358caa84939SJeenu Viswambharan	/* Check whether aarch32 issued an SMC64 */
359caa84939SJeenu Viswambharan	tbnz	x0, #FUNCID_CC_SHIFT, smc_prohibited
360caa84939SJeenu Viswambharan
361caa84939SJeenu Viswambharansmc_handler64:
3625283962eSAntonio Nino Diaz	/* NOTE: The code below must preserve x0-x4 */
3635283962eSAntonio Nino Diaz
364e290a8fcSAlexei Fedorov	/*
365ed108b56SAlexei Fedorov	 * Save general purpose and ARMv8.3-PAuth registers (if enabled).
366ed108b56SAlexei Fedorov	 * If Secure Cycle Counter is not disabled in MDCR_EL3 when
367ed108b56SAlexei Fedorov	 * ARMv8.5-PMU is implemented, save PMCR_EL0 and disable Cycle Counter.
368e290a8fcSAlexei Fedorov	 */
369ed108b56SAlexei Fedorov	bl	save_gp_pmcr_pauth_regs
370e290a8fcSAlexei Fedorov
371b86048c4SAntonio Nino Diaz#if ENABLE_PAUTH
372ed108b56SAlexei Fedorov	/* Load and program APIAKey firmware key */
373ed108b56SAlexei Fedorov	bl	pauth_load_bl31_apiakey
374b86048c4SAntonio Nino Diaz#endif
3755283962eSAntonio Nino Diaz
376a6ef4393SDouglas Raillard	/*
377a6ef4393SDouglas Raillard	 * Populate the parameters for the SMC handler.
378a6ef4393SDouglas Raillard	 * We already have x0-x4 in place. x5 will point to a cookie (not used
379a6ef4393SDouglas Raillard	 * now). x6 will point to the context structure (SP_EL3) and x7 will
380201ca5b6SDimitris Papastamos	 * contain flags we need to pass to the handler.
381caa84939SJeenu Viswambharan	 */
382caa84939SJeenu Viswambharan	mov	x5, xzr
383caa84939SJeenu Viswambharan	mov	x6, sp
384caa84939SJeenu Viswambharan
385a6ef4393SDouglas Raillard	/*
386a6ef4393SDouglas Raillard	 * Restore the saved C runtime stack value which will become the new
387a6ef4393SDouglas Raillard	 * SP_EL0 i.e. EL3 runtime stack. It was saved in the 'cpu_context'
388a6ef4393SDouglas Raillard	 * structure prior to the last ERET from EL3.
389caa84939SJeenu Viswambharan	 */
390caa84939SJeenu Viswambharan	ldr	x12, [x6, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
391caa84939SJeenu Viswambharan
392caa84939SJeenu Viswambharan	/* Switch to SP_EL0 */
393ed108b56SAlexei Fedorov	msr	spsel, #MODE_SP_EL0
394caa84939SJeenu Viswambharan
395a6ef4393SDouglas Raillard	/*
396a6ef4393SDouglas Raillard	 * Save the SPSR_EL3, ELR_EL3, & SCR_EL3 in case there is a world
397a6ef4393SDouglas Raillard	 * switch during SMC handling.
398a6ef4393SDouglas Raillard	 * TODO: Revisit if all system registers can be saved later.
399caa84939SJeenu Viswambharan	 */
400caa84939SJeenu Viswambharan	mrs	x16, spsr_el3
401caa84939SJeenu Viswambharan	mrs	x17, elr_el3
402caa84939SJeenu Viswambharan	mrs	x18, scr_el3
403caa84939SJeenu Viswambharan	stp	x16, x17, [x6, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
404b51da821SAchin Gupta	str	x18, [x6, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3]
405caa84939SJeenu Viswambharan
406caa84939SJeenu Viswambharan	/* Copy SCR_EL3.NS bit to the flag to indicate caller's security */
407caa84939SJeenu Viswambharan	bfi	x7, x18, #0, #1
408caa84939SJeenu Viswambharan
409caa84939SJeenu Viswambharan	mov	sp, x12
410caa84939SJeenu Viswambharan
411cc485e27SMadhukar Pappireddy	/* Get the unique owning entity number */
412cc485e27SMadhukar Pappireddy	ubfx	x16, x0, #FUNCID_OEN_SHIFT, #FUNCID_OEN_WIDTH
413cc485e27SMadhukar Pappireddy	ubfx	x15, x0, #FUNCID_TYPE_SHIFT, #FUNCID_TYPE_WIDTH
414cc485e27SMadhukar Pappireddy	orr	x16, x16, x15, lsl #FUNCID_OEN_WIDTH
415cc485e27SMadhukar Pappireddy
416cc485e27SMadhukar Pappireddy	/* Load descriptor index from array of indices */
417cc485e27SMadhukar Pappireddy	adr	x14, rt_svc_descs_indices
418cc485e27SMadhukar Pappireddy	ldrb	w15, [x14, x16]
419cc485e27SMadhukar Pappireddy
420cc485e27SMadhukar Pappireddy	/* Any index greater than 127 is invalid. Check bit 7. */
421cc485e27SMadhukar Pappireddy	tbnz	w15, 7, smc_unknown
422cc485e27SMadhukar Pappireddy
423cc485e27SMadhukar Pappireddy	/*
424cc485e27SMadhukar Pappireddy	 * Get the descriptor using the index
425cc485e27SMadhukar Pappireddy	 * x11 = (base + off), w15 = index
426cc485e27SMadhukar Pappireddy	 *
427cc485e27SMadhukar Pappireddy	 * handler = (base + off) + (index << log2(size))
428cc485e27SMadhukar Pappireddy	 */
429cc485e27SMadhukar Pappireddy	adr	x11, (__RT_SVC_DESCS_START__ + RT_SVC_DESC_HANDLE)
430cc485e27SMadhukar Pappireddy	lsl	w10, w15, #RT_SVC_SIZE_LOG2
431cc485e27SMadhukar Pappireddy	ldr	x15, [x11, w10, uxtw]
432cc485e27SMadhukar Pappireddy
433a6ef4393SDouglas Raillard	/*
434a6ef4393SDouglas Raillard	 * Call the Secure Monitor Call handler and then drop directly into
435a6ef4393SDouglas Raillard	 * el3_exit() which will program any remaining architectural state
436a6ef4393SDouglas Raillard	 * prior to issuing the ERET to the desired lower EL.
437caa84939SJeenu Viswambharan	 */
438caa84939SJeenu Viswambharan#if DEBUG
439caa84939SJeenu Viswambharan	cbz	x15, rt_svc_fw_critical_error
440caa84939SJeenu Viswambharan#endif
441caa84939SJeenu Viswambharan	blr	x15
442caa84939SJeenu Viswambharan
443bbf8f6f9SYatharth Kochar	b	el3_exit
4444f6ad66aSAchin Gupta
445caa84939SJeenu Viswambharansmc_unknown:
446caa84939SJeenu Viswambharan	/*
447cc485e27SMadhukar Pappireddy	 * Unknown SMC call. Populate return value with SMC_UNK and call
448cc485e27SMadhukar Pappireddy	 * el3_exit() which will restore the remaining architectural state
449cc485e27SMadhukar Pappireddy	 * i.e., SYS, GP and PAuth registers(if any) prior to issuing the ERET
450cc485e27SMadhukar Pappireddy         * to the desired lower EL.
451caa84939SJeenu Viswambharan	 */
4524abd7fa7SAntonio Nino Diaz	mov	x0, #SMC_UNK
453cc485e27SMadhukar Pappireddy	str	x0, [x6, #CTX_GPREGS_OFFSET + CTX_GPREG_X0]
454cc485e27SMadhukar Pappireddy	b	el3_exit
455caa84939SJeenu Viswambharan
456caa84939SJeenu Viswambharansmc_prohibited:
457c3260f9bSSoby Mathew	ldr	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
4584abd7fa7SAntonio Nino Diaz	mov	x0, #SMC_UNK
459*f461fe34SAnthony Steinhauser	exception_return
460caa84939SJeenu Viswambharan
461ed108b56SAlexei Fedorov#if DEBUG
462caa84939SJeenu Viswambharanrt_svc_fw_critical_error:
463a6ef4393SDouglas Raillard	/* Switch to SP_ELx */
464ed108b56SAlexei Fedorov	msr	spsel, #MODE_SP_ELX
465a806dad5SJeenu Viswambharan	no_ret	report_unhandled_exception
466ed108b56SAlexei Fedorov#endif
4678b779620SKévin Petitendfunc smc_handler
4681f461979SJustin Chadwell
4691f461979SJustin Chadwell	/* ---------------------------------------------------------------------
4701f461979SJustin Chadwell	 * The following code handles exceptions caused by BRK instructions.
4711f461979SJustin Chadwell	 * Following a BRK instruction, the only real valid cause of action is
4721f461979SJustin Chadwell	 * to print some information and panic, as the code that caused it is
4731f461979SJustin Chadwell	 * likely in an inconsistent internal state.
4741f461979SJustin Chadwell	 *
4751f461979SJustin Chadwell	 * This is initially intended to be used in conjunction with
4761f461979SJustin Chadwell	 * __builtin_trap.
4771f461979SJustin Chadwell	 * ---------------------------------------------------------------------
4781f461979SJustin Chadwell	 */
4791f461979SJustin Chadwell#ifdef MONITOR_TRAPS
4801f461979SJustin Chadwellfunc brk_handler
4811f461979SJustin Chadwell	/* Extract the ISS */
4821f461979SJustin Chadwell	mrs	x10, esr_el3
4831f461979SJustin Chadwell	ubfx	x10, x10, #ESR_ISS_SHIFT, #ESR_ISS_LENGTH
4841f461979SJustin Chadwell
4851f461979SJustin Chadwell	/* Ensure the console is initialized */
4861f461979SJustin Chadwell	bl	plat_crash_console_init
4871f461979SJustin Chadwell
4881f461979SJustin Chadwell	adr	x4, brk_location
4891f461979SJustin Chadwell	bl	asm_print_str
4901f461979SJustin Chadwell	mrs	x4, elr_el3
4911f461979SJustin Chadwell	bl	asm_print_hex
4921f461979SJustin Chadwell	bl	asm_print_newline
4931f461979SJustin Chadwell
4941f461979SJustin Chadwell	adr	x4, brk_message
4951f461979SJustin Chadwell	bl	asm_print_str
4961f461979SJustin Chadwell	mov	x4, x10
4971f461979SJustin Chadwell	mov	x5, #28
4981f461979SJustin Chadwell	bl	asm_print_hex_bits
4991f461979SJustin Chadwell	bl	asm_print_newline
5001f461979SJustin Chadwell
5011f461979SJustin Chadwell	no_ret	plat_panic_handler
5021f461979SJustin Chadwellendfunc brk_handler
5031f461979SJustin Chadwell#endif /* MONITOR_TRAPS */
504