xref: /rk3399_ARM-atf/bl31/aarch64/runtime_exceptions.S (revision 14c6016ad5ff4ac22861dff03129a646ad02c6dd)
14f6ad66aSAchin Gupta/*
2201ca5b6SDimitris Papastamos * Copyright (c) 2013-2018, ARM Limited and Contributors. All rights reserved.
34f6ad66aSAchin Gupta *
482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause
54f6ad66aSAchin Gupta */
64f6ad66aSAchin Gupta
74f6ad66aSAchin Gupta#include <arch.h>
835e98e55SDan Handley#include <asm_macros.S>
997043ac9SDan Handley#include <context.h>
10872be88aSdp-arm#include <cpu_data.h>
1176454abfSJeenu Viswambharan#include <ea_handle.h>
12dce74b89SAchin Gupta#include <interrupt_mgmt.h>
135f0cdb05SDan Handley#include <platform_def.h>
1497043ac9SDan Handley#include <runtime_svc.h>
152f370465SAntonio Nino Diaz#include <smccc.h>
164f6ad66aSAchin Gupta
174f6ad66aSAchin Gupta	.globl	runtime_exceptions
184f6ad66aSAchin Gupta
19f62ad322SDimitris Papastamos	.globl	sync_exception_sp_el0
20f62ad322SDimitris Papastamos	.globl	irq_sp_el0
21f62ad322SDimitris Papastamos	.globl	fiq_sp_el0
22f62ad322SDimitris Papastamos	.globl	serror_sp_el0
23f62ad322SDimitris Papastamos
24f62ad322SDimitris Papastamos	.globl	sync_exception_sp_elx
25f62ad322SDimitris Papastamos	.globl	irq_sp_elx
26f62ad322SDimitris Papastamos	.globl	fiq_sp_elx
27f62ad322SDimitris Papastamos	.globl	serror_sp_elx
28f62ad322SDimitris Papastamos
29f62ad322SDimitris Papastamos	.globl	sync_exception_aarch64
30f62ad322SDimitris Papastamos	.globl	irq_aarch64
31f62ad322SDimitris Papastamos	.globl	fiq_aarch64
32f62ad322SDimitris Papastamos	.globl	serror_aarch64
33f62ad322SDimitris Papastamos
34f62ad322SDimitris Papastamos	.globl	sync_exception_aarch32
35f62ad322SDimitris Papastamos	.globl	irq_aarch32
36f62ad322SDimitris Papastamos	.globl	fiq_aarch32
37f62ad322SDimitris Papastamos	.globl	serror_aarch32
38f62ad322SDimitris Papastamos
3976454abfSJeenu Viswambharan	/*
40*14c6016aSJeenu Viswambharan	 * Macro that prepares entry to EL3 upon taking an exception.
41*14c6016aSJeenu Viswambharan	 *
42*14c6016aSJeenu Viswambharan	 * With RAS_EXTENSION, this macro synchronizes pending errors with an ESB
43*14c6016aSJeenu Viswambharan	 * instruction. When an error is thus synchronized, the handling is
44*14c6016aSJeenu Viswambharan	 * delegated to platform EA handler.
45*14c6016aSJeenu Viswambharan	 *
46*14c6016aSJeenu Viswambharan	 * Without RAS_EXTENSION, this macro just saves x30, and unmasks
47*14c6016aSJeenu Viswambharan	 * Asynchronous External Aborts.
48*14c6016aSJeenu Viswambharan	 */
49*14c6016aSJeenu Viswambharan	.macro check_and_unmask_ea
50*14c6016aSJeenu Viswambharan#if RAS_EXTENSION
51*14c6016aSJeenu Viswambharan	/* Synchronize pending External Aborts */
52*14c6016aSJeenu Viswambharan	esb
53*14c6016aSJeenu Viswambharan
54*14c6016aSJeenu Viswambharan	/* Unmask the SError interrupt */
55*14c6016aSJeenu Viswambharan	msr	daifclr, #DAIF_ABT_BIT
56*14c6016aSJeenu Viswambharan
57*14c6016aSJeenu Viswambharan	/*
58*14c6016aSJeenu Viswambharan	 * Explicitly save x30 so as to free up a register and to enable
59*14c6016aSJeenu Viswambharan	 * branching
60*14c6016aSJeenu Viswambharan	 */
61*14c6016aSJeenu Viswambharan	str	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
62*14c6016aSJeenu Viswambharan
63*14c6016aSJeenu Viswambharan	/* Check for SErrors synchronized by the ESB instruction */
64*14c6016aSJeenu Viswambharan	mrs	x30, DISR_EL1
65*14c6016aSJeenu Viswambharan	tbz	x30, #DISR_A_BIT, 1f
66*14c6016aSJeenu Viswambharan
67*14c6016aSJeenu Viswambharan	/* Save GP registers and restore them afterwards */
68*14c6016aSJeenu Viswambharan	bl	save_gp_registers
69*14c6016aSJeenu Viswambharan	mov	x0, #ERROR_EA_ESB
70*14c6016aSJeenu Viswambharan	mrs	x1, DISR_EL1
71*14c6016aSJeenu Viswambharan	bl	delegate_ea
72*14c6016aSJeenu Viswambharan	bl	restore_gp_registers
73*14c6016aSJeenu Viswambharan
74*14c6016aSJeenu Viswambharan1:
75*14c6016aSJeenu Viswambharan#else
76*14c6016aSJeenu Viswambharan	/* Unmask the SError interrupt */
77*14c6016aSJeenu Viswambharan	msr	daifclr, #DAIF_ABT_BIT
78*14c6016aSJeenu Viswambharan
79*14c6016aSJeenu Viswambharan	str	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
80*14c6016aSJeenu Viswambharan#endif
81*14c6016aSJeenu Viswambharan	.endm
82*14c6016aSJeenu Viswambharan
83*14c6016aSJeenu Viswambharan	/*
8476454abfSJeenu Viswambharan	 * Handle External Abort by delegating to the platform's EA handler.
8576454abfSJeenu Viswambharan	 * Once the platform handler returns, the macro exits EL3 and returns to
8676454abfSJeenu Viswambharan	 * where the abort was taken from.
8776454abfSJeenu Viswambharan	 *
8876454abfSJeenu Viswambharan	 * This macro assumes that x30 is available for use.
8976454abfSJeenu Viswambharan	 *
9076454abfSJeenu Viswambharan	 * 'abort_type' is a constant passed to the platform handler, indicating
9176454abfSJeenu Viswambharan	 * the cause of the External Abort.
9276454abfSJeenu Viswambharan	 */
9376454abfSJeenu Viswambharan	.macro handle_ea abort_type
9476454abfSJeenu Viswambharan	/* Save GP registers */
9576454abfSJeenu Viswambharan	bl	save_gp_registers
9676454abfSJeenu Viswambharan
9776454abfSJeenu Viswambharan	/* Setup exception class and syndrome arguments for platform handler */
9876454abfSJeenu Viswambharan	mov	x0, \abort_type
9976454abfSJeenu Viswambharan	mrs	x1, esr_el3
10076454abfSJeenu Viswambharan	adr	x30, el3_exit
10176454abfSJeenu Viswambharan	b	delegate_ea
10276454abfSJeenu Viswambharan	.endm
10376454abfSJeenu Viswambharan
104a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
105a6ef4393SDouglas Raillard	 * This macro handles Synchronous exceptions.
106a6ef4393SDouglas Raillard	 * Only SMC exceptions are supported.
107a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
108dce74b89SAchin Gupta	 */
109dce74b89SAchin Gupta	.macro	handle_sync_exception
110872be88aSdp-arm#if ENABLE_RUNTIME_INSTRUMENTATION
111872be88aSdp-arm	/*
112a6ef4393SDouglas Raillard	 * Read the timestamp value and store it in per-cpu data. The value
113a6ef4393SDouglas Raillard	 * will be extracted from per-cpu data by the C level SMC handler and
114a6ef4393SDouglas Raillard	 * saved to the PMF timestamp region.
115872be88aSdp-arm	 */
116872be88aSdp-arm	mrs	x30, cntpct_el0
117872be88aSdp-arm	str	x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29]
118872be88aSdp-arm	mrs	x29, tpidr_el3
119872be88aSdp-arm	str	x30, [x29, #CPU_DATA_PMF_TS0_OFFSET]
120872be88aSdp-arm	ldr	x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29]
121872be88aSdp-arm#endif
122872be88aSdp-arm
123dce74b89SAchin Gupta	mrs	x30, esr_el3
124dce74b89SAchin Gupta	ubfx	x30, x30, #ESR_EC_SHIFT, #ESR_EC_LENGTH
125dce74b89SAchin Gupta
126a6ef4393SDouglas Raillard	/* Handle SMC exceptions separately from other synchronous exceptions */
127dce74b89SAchin Gupta	cmp	x30, #EC_AARCH32_SMC
128dce74b89SAchin Gupta	b.eq	smc_handler32
129dce74b89SAchin Gupta
130dce74b89SAchin Gupta	cmp	x30, #EC_AARCH64_SMC
131dce74b89SAchin Gupta	b.eq	smc_handler64
132dce74b89SAchin Gupta
13376454abfSJeenu Viswambharan	/* Check for I/D aborts from lower EL */
13476454abfSJeenu Viswambharan	cmp	x30, #EC_IABORT_LOWER_EL
13576454abfSJeenu Viswambharan	b.eq	1f
13676454abfSJeenu Viswambharan
13776454abfSJeenu Viswambharan	cmp	x30, #EC_DABORT_LOWER_EL
13876454abfSJeenu Viswambharan	b.ne	2f
13976454abfSJeenu Viswambharan
14076454abfSJeenu Viswambharan1:
14176454abfSJeenu Viswambharan	/* Test for EA bit in the instruction syndrome */
14276454abfSJeenu Viswambharan	mrs	x30, esr_el3
14376454abfSJeenu Viswambharan	tbz	x30, #ESR_ISS_EABORT_EA_BIT, 2f
14476454abfSJeenu Viswambharan	handle_ea #ERROR_EA_SYNC
14576454abfSJeenu Viswambharan
14676454abfSJeenu Viswambharan2:
147a6ef4393SDouglas Raillard	/* Other kinds of synchronous exceptions are not handled */
1484d91838bSJulius Werner	ldr	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
1494d91838bSJulius Werner	b	report_unhandled_exception
150dce74b89SAchin Gupta	.endm
151dce74b89SAchin Gupta
152dce74b89SAchin Gupta
153a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
154a6ef4393SDouglas Raillard	 * This macro handles FIQ or IRQ interrupts i.e. EL3, S-EL1 and NS
155a6ef4393SDouglas Raillard	 * interrupts.
156a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
157dce74b89SAchin Gupta	 */
158dce74b89SAchin Gupta	.macro	handle_interrupt_exception label
159dce74b89SAchin Gupta	bl	save_gp_registers
160a6ef4393SDouglas Raillard	/* Save the EL3 system registers needed to return from this exception */
1615717aae1SAchin Gupta	mrs	x0, spsr_el3
1625717aae1SAchin Gupta	mrs	x1, elr_el3
1635717aae1SAchin Gupta	stp	x0, x1, [sp, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
1645717aae1SAchin Gupta
165dce74b89SAchin Gupta	/* Switch to the runtime stack i.e. SP_EL0 */
166dce74b89SAchin Gupta	ldr	x2, [sp, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
167dce74b89SAchin Gupta	mov	x20, sp
168dce74b89SAchin Gupta	msr	spsel, #0
169dce74b89SAchin Gupta	mov	sp, x2
170dce74b89SAchin Gupta
171dce74b89SAchin Gupta	/*
172a6ef4393SDouglas Raillard	 * Find out whether this is a valid interrupt type.
173a6ef4393SDouglas Raillard	 * If the interrupt controller reports a spurious interrupt then return
174a6ef4393SDouglas Raillard	 * to where we came from.
175dce74b89SAchin Gupta	 */
1769865ac15SDan Handley	bl	plat_ic_get_pending_interrupt_type
177dce74b89SAchin Gupta	cmp	x0, #INTR_TYPE_INVAL
178dce74b89SAchin Gupta	b.eq	interrupt_exit_\label
179dce74b89SAchin Gupta
180dce74b89SAchin Gupta	/*
181a6ef4393SDouglas Raillard	 * Get the registered handler for this interrupt type.
182a6ef4393SDouglas Raillard	 * A NULL return value could be 'cause of the following conditions:
1835717aae1SAchin Gupta	 *
184a6ef4393SDouglas Raillard	 * a. An interrupt of a type was routed correctly but a handler for its
185a6ef4393SDouglas Raillard	 *    type was not registered.
1865717aae1SAchin Gupta	 *
187a6ef4393SDouglas Raillard	 * b. An interrupt of a type was not routed correctly so a handler for
188a6ef4393SDouglas Raillard	 *    its type was not registered.
1895717aae1SAchin Gupta	 *
190a6ef4393SDouglas Raillard	 * c. An interrupt of a type was routed correctly to EL3, but was
191a6ef4393SDouglas Raillard	 *    deasserted before its pending state could be read. Another
192a6ef4393SDouglas Raillard	 *    interrupt of a different type pended at the same time and its
193a6ef4393SDouglas Raillard	 *    type was reported as pending instead. However, a handler for this
194a6ef4393SDouglas Raillard	 *    type was not registered.
1955717aae1SAchin Gupta	 *
196a6ef4393SDouglas Raillard	 * a. and b. can only happen due to a programming error. The
197a6ef4393SDouglas Raillard	 * occurrence of c. could be beyond the control of Trusted Firmware.
198a6ef4393SDouglas Raillard	 * It makes sense to return from this exception instead of reporting an
199a6ef4393SDouglas Raillard	 * error.
200dce74b89SAchin Gupta	 */
201dce74b89SAchin Gupta	bl	get_interrupt_type_handler
2025717aae1SAchin Gupta	cbz	x0, interrupt_exit_\label
203dce74b89SAchin Gupta	mov	x21, x0
204dce74b89SAchin Gupta
205dce74b89SAchin Gupta	mov	x0, #INTR_ID_UNAVAILABLE
206dce74b89SAchin Gupta
207dce74b89SAchin Gupta	/* Set the current security state in the 'flags' parameter */
208dce74b89SAchin Gupta	mrs	x2, scr_el3
209dce74b89SAchin Gupta	ubfx	x1, x2, #0, #1
210dce74b89SAchin Gupta
211dce74b89SAchin Gupta	/* Restore the reference to the 'handle' i.e. SP_EL3 */
212dce74b89SAchin Gupta	mov	x2, x20
213dce74b89SAchin Gupta
214b460b8bfSSoby Mathew	/* x3 will point to a cookie (not used now) */
215b460b8bfSSoby Mathew	mov	x3, xzr
216b460b8bfSSoby Mathew
217dce74b89SAchin Gupta	/* Call the interrupt type handler */
218dce74b89SAchin Gupta	blr	x21
219dce74b89SAchin Gupta
220dce74b89SAchin Guptainterrupt_exit_\label:
221dce74b89SAchin Gupta	/* Return from exception, possibly in a different security state */
222dce74b89SAchin Gupta	b	el3_exit
223dce74b89SAchin Gupta
224dce74b89SAchin Gupta	.endm
225dce74b89SAchin Gupta
226dce74b89SAchin Gupta
227e0ae9fabSSandrine Bailleuxvector_base runtime_exceptions
228e0ae9fabSSandrine Bailleux
229a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
230a6ef4393SDouglas Raillard	 * Current EL with SP_EL0 : 0x0 - 0x200
231a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
2324f6ad66aSAchin Gupta	 */
233e0ae9fabSSandrine Bailleuxvector_entry sync_exception_sp_el0
234a6ef4393SDouglas Raillard	/* We don't expect any synchronous exceptions from EL3 */
2354d91838bSJulius Werner	b	report_unhandled_exception
236a7934d69SJeenu Viswambharan	check_vector_size sync_exception_sp_el0
2374f6ad66aSAchin Gupta
238e0ae9fabSSandrine Bailleuxvector_entry irq_sp_el0
239a6ef4393SDouglas Raillard	/*
240a6ef4393SDouglas Raillard	 * EL3 code is non-reentrant. Any asynchronous exception is a serious
241a6ef4393SDouglas Raillard	 * error. Loop infinitely.
242a6ef4393SDouglas Raillard	 */
2434d91838bSJulius Werner	b	report_unhandled_interrupt
244a7934d69SJeenu Viswambharan	check_vector_size irq_sp_el0
2454f6ad66aSAchin Gupta
246e0ae9fabSSandrine Bailleux
247e0ae9fabSSandrine Bailleuxvector_entry fiq_sp_el0
2484d91838bSJulius Werner	b	report_unhandled_interrupt
249a7934d69SJeenu Viswambharan	check_vector_size fiq_sp_el0
2504f6ad66aSAchin Gupta
251e0ae9fabSSandrine Bailleux
252e0ae9fabSSandrine Bailleuxvector_entry serror_sp_el0
2534d91838bSJulius Werner	b	report_unhandled_exception
254a7934d69SJeenu Viswambharan	check_vector_size serror_sp_el0
2554f6ad66aSAchin Gupta
256a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
257a6ef4393SDouglas Raillard	 * Current EL with SP_ELx: 0x200 - 0x400
258a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
2594f6ad66aSAchin Gupta	 */
260e0ae9fabSSandrine Bailleuxvector_entry sync_exception_sp_elx
261a6ef4393SDouglas Raillard	/*
262a6ef4393SDouglas Raillard	 * This exception will trigger if anything went wrong during a previous
263a6ef4393SDouglas Raillard	 * exception entry or exit or while handling an earlier unexpected
264a6ef4393SDouglas Raillard	 * synchronous exception. There is a high probability that SP_EL3 is
265a6ef4393SDouglas Raillard	 * corrupted.
266caa84939SJeenu Viswambharan	 */
2674d91838bSJulius Werner	b	report_unhandled_exception
268a7934d69SJeenu Viswambharan	check_vector_size sync_exception_sp_elx
2694f6ad66aSAchin Gupta
270e0ae9fabSSandrine Bailleuxvector_entry irq_sp_elx
2714d91838bSJulius Werner	b	report_unhandled_interrupt
272a7934d69SJeenu Viswambharan	check_vector_size irq_sp_elx
273a7934d69SJeenu Viswambharan
274e0ae9fabSSandrine Bailleuxvector_entry fiq_sp_elx
2754d91838bSJulius Werner	b	report_unhandled_interrupt
276a7934d69SJeenu Viswambharan	check_vector_size fiq_sp_elx
277a7934d69SJeenu Viswambharan
278e0ae9fabSSandrine Bailleuxvector_entry serror_sp_elx
2794d91838bSJulius Werner	b	report_unhandled_exception
280a7934d69SJeenu Viswambharan	check_vector_size serror_sp_elx
2814f6ad66aSAchin Gupta
282a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
28344804252SSandrine Bailleux	 * Lower EL using AArch64 : 0x400 - 0x600
284a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
2854f6ad66aSAchin Gupta	 */
286e0ae9fabSSandrine Bailleuxvector_entry sync_exception_aarch64
287a6ef4393SDouglas Raillard	/*
288a6ef4393SDouglas Raillard	 * This exception vector will be the entry point for SMCs and traps
289a6ef4393SDouglas Raillard	 * that are unhandled at lower ELs most commonly. SP_EL3 should point
290a6ef4393SDouglas Raillard	 * to a valid cpu context where the general purpose and system register
291a6ef4393SDouglas Raillard	 * state can be saved.
292caa84939SJeenu Viswambharan	 */
293*14c6016aSJeenu Viswambharan	check_and_unmask_ea
294caa84939SJeenu Viswambharan	handle_sync_exception
295a7934d69SJeenu Viswambharan	check_vector_size sync_exception_aarch64
2964f6ad66aSAchin Gupta
297e0ae9fabSSandrine Bailleuxvector_entry irq_aarch64
298*14c6016aSJeenu Viswambharan	check_and_unmask_ea
299dce74b89SAchin Gupta	handle_interrupt_exception irq_aarch64
300a7934d69SJeenu Viswambharan	check_vector_size irq_aarch64
3014f6ad66aSAchin Gupta
302e0ae9fabSSandrine Bailleuxvector_entry fiq_aarch64
303*14c6016aSJeenu Viswambharan	check_and_unmask_ea
304dce74b89SAchin Gupta	handle_interrupt_exception fiq_aarch64
305a7934d69SJeenu Viswambharan	check_vector_size fiq_aarch64
3064f6ad66aSAchin Gupta
307e0ae9fabSSandrine Bailleuxvector_entry serror_aarch64
30876454abfSJeenu Viswambharan	msr	daifclr, #DAIF_ABT_BIT
30976454abfSJeenu Viswambharan
310a6ef4393SDouglas Raillard	/*
31176454abfSJeenu Viswambharan	 * Explicitly save x30 so as to free up a register and to enable
31276454abfSJeenu Viswambharan	 * branching
313a6ef4393SDouglas Raillard	 */
31476454abfSJeenu Viswambharan	str	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
31576454abfSJeenu Viswambharan	handle_ea #ERROR_EA_ASYNC
316a7934d69SJeenu Viswambharan	check_vector_size serror_aarch64
3174f6ad66aSAchin Gupta
318a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
31944804252SSandrine Bailleux	 * Lower EL using AArch32 : 0x600 - 0x800
320a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
3214f6ad66aSAchin Gupta	 */
322e0ae9fabSSandrine Bailleuxvector_entry sync_exception_aarch32
323a6ef4393SDouglas Raillard	/*
324a6ef4393SDouglas Raillard	 * This exception vector will be the entry point for SMCs and traps
325a6ef4393SDouglas Raillard	 * that are unhandled at lower ELs most commonly. SP_EL3 should point
326a6ef4393SDouglas Raillard	 * to a valid cpu context where the general purpose and system register
327a6ef4393SDouglas Raillard	 * state can be saved.
328caa84939SJeenu Viswambharan	 */
329*14c6016aSJeenu Viswambharan	check_and_unmask_ea
330caa84939SJeenu Viswambharan	handle_sync_exception
331a7934d69SJeenu Viswambharan	check_vector_size sync_exception_aarch32
3324f6ad66aSAchin Gupta
333e0ae9fabSSandrine Bailleuxvector_entry irq_aarch32
334*14c6016aSJeenu Viswambharan	check_and_unmask_ea
335dce74b89SAchin Gupta	handle_interrupt_exception irq_aarch32
336a7934d69SJeenu Viswambharan	check_vector_size irq_aarch32
3374f6ad66aSAchin Gupta
338e0ae9fabSSandrine Bailleuxvector_entry fiq_aarch32
339*14c6016aSJeenu Viswambharan	check_and_unmask_ea
340dce74b89SAchin Gupta	handle_interrupt_exception fiq_aarch32
341a7934d69SJeenu Viswambharan	check_vector_size fiq_aarch32
3424f6ad66aSAchin Gupta
343e0ae9fabSSandrine Bailleuxvector_entry serror_aarch32
34476454abfSJeenu Viswambharan	msr	daifclr, #DAIF_ABT_BIT
34576454abfSJeenu Viswambharan
346a6ef4393SDouglas Raillard	/*
34776454abfSJeenu Viswambharan	 * Explicitly save x30 so as to free up a register and to enable
34876454abfSJeenu Viswambharan	 * branching
349a6ef4393SDouglas Raillard	 */
35076454abfSJeenu Viswambharan	str	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
35176454abfSJeenu Viswambharan	handle_ea #ERROR_EA_ASYNC
352a7934d69SJeenu Viswambharan	check_vector_size serror_aarch32
353a7934d69SJeenu Viswambharan
354caa84939SJeenu Viswambharan
355a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
3562f370465SAntonio Nino Diaz	 * This macro takes an argument in x16 that is the index in the
3572f370465SAntonio Nino Diaz	 * 'rt_svc_descs_indices' array, checks that the value in the array is
3582f370465SAntonio Nino Diaz	 * valid, and loads in x15 the pointer to the handler of that service.
3592f370465SAntonio Nino Diaz	 * ---------------------------------------------------------------------
3602f370465SAntonio Nino Diaz	 */
3612f370465SAntonio Nino Diaz	.macro	load_rt_svc_desc_pointer
3622f370465SAntonio Nino Diaz	/* Load descriptor index from array of indices */
3632f370465SAntonio Nino Diaz	adr	x14, rt_svc_descs_indices
3642f370465SAntonio Nino Diaz	ldrb	w15, [x14, x16]
3652f370465SAntonio Nino Diaz
3662f370465SAntonio Nino Diaz#if SMCCC_MAJOR_VERSION == 1
3672f370465SAntonio Nino Diaz	/* Any index greater than 127 is invalid. Check bit 7. */
3682f370465SAntonio Nino Diaz	tbnz	w15, 7, smc_unknown
3692f370465SAntonio Nino Diaz#elif SMCCC_MAJOR_VERSION == 2
3702f370465SAntonio Nino Diaz	/* Verify that the top 3 bits of the loaded index are 0 (w15 <= 31) */
3712f370465SAntonio Nino Diaz	cmp	w15, #31
3722f370465SAntonio Nino Diaz	b.hi	smc_unknown
3732f370465SAntonio Nino Diaz#endif /* SMCCC_MAJOR_VERSION */
3742f370465SAntonio Nino Diaz
3752f370465SAntonio Nino Diaz	/*
3762f370465SAntonio Nino Diaz	 * Get the descriptor using the index
3772f370465SAntonio Nino Diaz	 * x11 = (base + off), w15 = index
3782f370465SAntonio Nino Diaz	 *
3792f370465SAntonio Nino Diaz	 * handler = (base + off) + (index << log2(size))
3802f370465SAntonio Nino Diaz	 */
3812f370465SAntonio Nino Diaz	adr	x11, (__RT_SVC_DESCS_START__ + RT_SVC_DESC_HANDLE)
3822f370465SAntonio Nino Diaz	lsl	w10, w15, #RT_SVC_SIZE_LOG2
3832f370465SAntonio Nino Diaz	ldr	x15, [x11, w10, uxtw]
3842f370465SAntonio Nino Diaz	.endm
3852f370465SAntonio Nino Diaz
3862f370465SAntonio Nino Diaz	/* ---------------------------------------------------------------------
387caa84939SJeenu Viswambharan	 * The following code handles secure monitor calls.
388a6ef4393SDouglas Raillard	 * Depending upon the execution state from where the SMC has been
389a6ef4393SDouglas Raillard	 * invoked, it frees some general purpose registers to perform the
390a6ef4393SDouglas Raillard	 * remaining tasks. They involve finding the runtime service handler
391a6ef4393SDouglas Raillard	 * that is the target of the SMC & switching to runtime stacks (SP_EL0)
392a6ef4393SDouglas Raillard	 * before calling the handler.
393caa84939SJeenu Viswambharan	 *
394a6ef4393SDouglas Raillard	 * Note that x30 has been explicitly saved and can be used here
395a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
396caa84939SJeenu Viswambharan	 */
3970a30cf54SAndrew Thoelkefunc smc_handler
398caa84939SJeenu Viswambharansmc_handler32:
399caa84939SJeenu Viswambharan	/* Check whether aarch32 issued an SMC64 */
400caa84939SJeenu Viswambharan	tbnz	x0, #FUNCID_CC_SHIFT, smc_prohibited
401caa84939SJeenu Viswambharan
402caa84939SJeenu Viswambharansmc_handler64:
403a6ef4393SDouglas Raillard	/*
404a6ef4393SDouglas Raillard	 * Populate the parameters for the SMC handler.
405a6ef4393SDouglas Raillard	 * We already have x0-x4 in place. x5 will point to a cookie (not used
406a6ef4393SDouglas Raillard	 * now). x6 will point to the context structure (SP_EL3) and x7 will
407201ca5b6SDimitris Papastamos	 * contain flags we need to pass to the handler.
408a6ef4393SDouglas Raillard	 *
4092f370465SAntonio Nino Diaz	 * Save x4-x29 and sp_el0.
410caa84939SJeenu Viswambharan	 */
411ef653d93SJeenu Viswambharan	stp	x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4]
412ef653d93SJeenu Viswambharan	stp	x6, x7, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X6]
413ef653d93SJeenu Viswambharan	stp	x8, x9, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X8]
414ef653d93SJeenu Viswambharan	stp	x10, x11, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X10]
415ef653d93SJeenu Viswambharan	stp	x12, x13, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X12]
416ef653d93SJeenu Viswambharan	stp	x14, x15, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X14]
417ef653d93SJeenu Viswambharan	stp	x16, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X16]
418ef653d93SJeenu Viswambharan	stp	x18, x19, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X18]
419ef653d93SJeenu Viswambharan	stp	x20, x21, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X20]
420ef653d93SJeenu Viswambharan	stp	x22, x23, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X22]
421ef653d93SJeenu Viswambharan	stp	x24, x25, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X24]
422ef653d93SJeenu Viswambharan	stp	x26, x27, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X26]
423ef653d93SJeenu Viswambharan	stp	x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28]
424ef653d93SJeenu Viswambharan	mrs	x18, sp_el0
425ef653d93SJeenu Viswambharan	str	x18, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_SP_EL0]
426c3260f9bSSoby Mathew
427caa84939SJeenu Viswambharan	mov	x5, xzr
428caa84939SJeenu Viswambharan	mov	x6, sp
429caa84939SJeenu Viswambharan
4302f370465SAntonio Nino Diaz#if SMCCC_MAJOR_VERSION == 1
4312f370465SAntonio Nino Diaz
432caa84939SJeenu Viswambharan	/* Get the unique owning entity number */
433caa84939SJeenu Viswambharan	ubfx	x16, x0, #FUNCID_OEN_SHIFT, #FUNCID_OEN_WIDTH
434caa84939SJeenu Viswambharan	ubfx	x15, x0, #FUNCID_TYPE_SHIFT, #FUNCID_TYPE_WIDTH
435caa84939SJeenu Viswambharan	orr	x16, x16, x15, lsl #FUNCID_OEN_WIDTH
436caa84939SJeenu Viswambharan
4372f370465SAntonio Nino Diaz	load_rt_svc_desc_pointer
438caa84939SJeenu Viswambharan
4392f370465SAntonio Nino Diaz#elif SMCCC_MAJOR_VERSION == 2
4402f370465SAntonio Nino Diaz
4412f370465SAntonio Nino Diaz	/* Bit 31 must be set */
4422f370465SAntonio Nino Diaz	tbz	x0, #FUNCID_TYPE_SHIFT, smc_unknown
4432f370465SAntonio Nino Diaz
4442f370465SAntonio Nino Diaz	/*
4452f370465SAntonio Nino Diaz	 * Check MSB of namespace to decide between compatibility/vendor and
4462f370465SAntonio Nino Diaz	 * SPCI/SPRT
4472f370465SAntonio Nino Diaz	 */
4482f370465SAntonio Nino Diaz	tbz	x0, #(FUNCID_NAMESPACE_SHIFT + 1), compat_or_vendor
4492f370465SAntonio Nino Diaz
4502f370465SAntonio Nino Diaz	/* Namespaces SPRT and SPCI currently unimplemented */
4512f370465SAntonio Nino Diaz	b	smc_unknown
4522f370465SAntonio Nino Diaz
4532f370465SAntonio Nino Diazcompat_or_vendor:
4542f370465SAntonio Nino Diaz
4552f370465SAntonio Nino Diaz	/* Namespace is b'00 (compatibility) or b'01 (vendor) */
4562f370465SAntonio Nino Diaz
4572f370465SAntonio Nino Diaz	/*
4582f370465SAntonio Nino Diaz	 * Add the LSB of the namespace (bit [28]) to the OEN [27:24] to create
4592f370465SAntonio Nino Diaz	 * a 5-bit index into the rt_svc_descs_indices array.
4602f370465SAntonio Nino Diaz	 *
4612f370465SAntonio Nino Diaz	 * The low 16 entries of the rt_svc_descs_indices array correspond to
4622f370465SAntonio Nino Diaz	 * OENs of the compatibility namespace and the top 16 entries of the
4632f370465SAntonio Nino Diaz	 * array are assigned to the vendor namespace descriptor.
4642f370465SAntonio Nino Diaz	 */
4652f370465SAntonio Nino Diaz	ubfx	x16, x0, #FUNCID_OEN_SHIFT, #(FUNCID_OEN_WIDTH + 1)
4662f370465SAntonio Nino Diaz
4672f370465SAntonio Nino Diaz	load_rt_svc_desc_pointer
4682f370465SAntonio Nino Diaz
4692f370465SAntonio Nino Diaz#endif /* SMCCC_MAJOR_VERSION */
470caa84939SJeenu Viswambharan
471a6ef4393SDouglas Raillard	/*
472a6ef4393SDouglas Raillard	 * Restore the saved C runtime stack value which will become the new
473a6ef4393SDouglas Raillard	 * SP_EL0 i.e. EL3 runtime stack. It was saved in the 'cpu_context'
474a6ef4393SDouglas Raillard	 * structure prior to the last ERET from EL3.
475caa84939SJeenu Viswambharan	 */
476caa84939SJeenu Viswambharan	ldr	x12, [x6, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
477caa84939SJeenu Viswambharan
478caa84939SJeenu Viswambharan	/* Switch to SP_EL0 */
479caa84939SJeenu Viswambharan	msr	spsel, #0
480caa84939SJeenu Viswambharan
481a6ef4393SDouglas Raillard	/*
482a6ef4393SDouglas Raillard	 * Save the SPSR_EL3, ELR_EL3, & SCR_EL3 in case there is a world
483a6ef4393SDouglas Raillard	 * switch during SMC handling.
484a6ef4393SDouglas Raillard	 * TODO: Revisit if all system registers can be saved later.
485caa84939SJeenu Viswambharan	 */
486caa84939SJeenu Viswambharan	mrs	x16, spsr_el3
487caa84939SJeenu Viswambharan	mrs	x17, elr_el3
488caa84939SJeenu Viswambharan	mrs	x18, scr_el3
489caa84939SJeenu Viswambharan	stp	x16, x17, [x6, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
490b51da821SAchin Gupta	str	x18, [x6, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3]
491caa84939SJeenu Viswambharan
492caa84939SJeenu Viswambharan	/* Copy SCR_EL3.NS bit to the flag to indicate caller's security */
493caa84939SJeenu Viswambharan	bfi	x7, x18, #0, #1
494caa84939SJeenu Viswambharan
495caa84939SJeenu Viswambharan	mov	sp, x12
496caa84939SJeenu Viswambharan
497a6ef4393SDouglas Raillard	/*
498a6ef4393SDouglas Raillard	 * Call the Secure Monitor Call handler and then drop directly into
499a6ef4393SDouglas Raillard	 * el3_exit() which will program any remaining architectural state
500a6ef4393SDouglas Raillard	 * prior to issuing the ERET to the desired lower EL.
501caa84939SJeenu Viswambharan	 */
502caa84939SJeenu Viswambharan#if DEBUG
503caa84939SJeenu Viswambharan	cbz	x15, rt_svc_fw_critical_error
504caa84939SJeenu Viswambharan#endif
505caa84939SJeenu Viswambharan	blr	x15
506caa84939SJeenu Viswambharan
507bbf8f6f9SYatharth Kochar	b	el3_exit
5084f6ad66aSAchin Gupta
509caa84939SJeenu Viswambharansmc_unknown:
510caa84939SJeenu Viswambharan	/*
511ef653d93SJeenu Viswambharan	 * Unknown SMC call. Populate return value with SMC_UNK, restore
512ef653d93SJeenu Viswambharan	 * GP registers, and return to caller.
513caa84939SJeenu Viswambharan	 */
5144abd7fa7SAntonio Nino Diaz	mov	x0, #SMC_UNK
515ef653d93SJeenu Viswambharan	str	x0, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X0]
516ef653d93SJeenu Viswambharan	b	restore_gp_registers_eret
517caa84939SJeenu Viswambharan
518caa84939SJeenu Viswambharansmc_prohibited:
519c3260f9bSSoby Mathew	ldr	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
5204abd7fa7SAntonio Nino Diaz	mov	x0, #SMC_UNK
521caa84939SJeenu Viswambharan	eret
522caa84939SJeenu Viswambharan
523caa84939SJeenu Viswambharanrt_svc_fw_critical_error:
524a6ef4393SDouglas Raillard	/* Switch to SP_ELx */
525a6ef4393SDouglas Raillard	msr	spsel, #1
526a806dad5SJeenu Viswambharan	no_ret	report_unhandled_exception
5278b779620SKévin Petitendfunc smc_handler
52876454abfSJeenu Viswambharan
52976454abfSJeenu Viswambharan/*
53076454abfSJeenu Viswambharan * Delegate External Abort handling to platform's EA handler. This function
53176454abfSJeenu Viswambharan * assumes that all GP registers have been saved by the caller.
53276454abfSJeenu Viswambharan *
53376454abfSJeenu Viswambharan * x0: EA reason
53476454abfSJeenu Viswambharan * x1: EA syndrome
53576454abfSJeenu Viswambharan */
53676454abfSJeenu Viswambharanfunc delegate_ea
53776454abfSJeenu Viswambharan	/* Save EL3 state */
53876454abfSJeenu Viswambharan	mrs	x2, spsr_el3
53976454abfSJeenu Viswambharan	mrs	x3, elr_el3
54076454abfSJeenu Viswambharan	stp	x2, x3, [sp, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
54176454abfSJeenu Viswambharan
54276454abfSJeenu Viswambharan	/*
54376454abfSJeenu Viswambharan	 * Save ESR as handling might involve lower ELs, and returning back to
54476454abfSJeenu Viswambharan	 * EL3 from there would trample the original ESR.
54576454abfSJeenu Viswambharan	 */
54676454abfSJeenu Viswambharan	mrs	x4, scr_el3
54776454abfSJeenu Viswambharan	mrs	x5, esr_el3
54876454abfSJeenu Viswambharan	stp	x4, x5, [sp, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3]
54976454abfSJeenu Viswambharan
55076454abfSJeenu Viswambharan	/*
55176454abfSJeenu Viswambharan	 * Setup rest of arguments, and call platform External Abort handler.
55276454abfSJeenu Viswambharan	 *
55376454abfSJeenu Viswambharan	 * x0: EA reason (already in place)
55476454abfSJeenu Viswambharan	 * x1: Exception syndrome (already in place).
55576454abfSJeenu Viswambharan	 * x2: Cookie (unused for now).
55676454abfSJeenu Viswambharan	 * x3: Context pointer.
55776454abfSJeenu Viswambharan	 * x4: Flags (security state from SCR for now).
55876454abfSJeenu Viswambharan	 */
55976454abfSJeenu Viswambharan	mov	x2, xzr
56076454abfSJeenu Viswambharan	mov	x3, sp
56176454abfSJeenu Viswambharan	ubfx	x4, x4, #0, #1
56276454abfSJeenu Viswambharan
56376454abfSJeenu Viswambharan	/* Switch to runtime stack */
56476454abfSJeenu Viswambharan	ldr	x5, [sp, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
56576454abfSJeenu Viswambharan	msr	spsel, #0
56676454abfSJeenu Viswambharan	mov	sp, x5
56776454abfSJeenu Viswambharan
56876454abfSJeenu Viswambharan	mov	x29, x30
56976454abfSJeenu Viswambharan	bl	plat_ea_handler
57076454abfSJeenu Viswambharan	mov	x30, x29
57176454abfSJeenu Viswambharan
57276454abfSJeenu Viswambharan	/* Make SP point to context */
57376454abfSJeenu Viswambharan	msr	spsel, #1
57476454abfSJeenu Viswambharan
57576454abfSJeenu Viswambharan	/* Restore EL3 state */
57676454abfSJeenu Viswambharan	ldp	x1, x2, [sp, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
57776454abfSJeenu Viswambharan	msr	spsr_el3, x1
57876454abfSJeenu Viswambharan	msr	elr_el3, x2
57976454abfSJeenu Viswambharan
58076454abfSJeenu Viswambharan	/* Restore ESR_EL3 and SCR_EL3 */
58176454abfSJeenu Viswambharan	ldp	x3, x4, [sp, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3]
58276454abfSJeenu Viswambharan	msr	scr_el3, x3
58376454abfSJeenu Viswambharan	msr	esr_el3, x4
58476454abfSJeenu Viswambharan
58576454abfSJeenu Viswambharan	ret
58676454abfSJeenu Viswambharanendfunc delegate_ea
587