xref: /rk3399_ARM-atf/bl31/aarch64/runtime_exceptions.S (revision 82cb2c1ad9897473743f08437d0a3995bed561b9)
14f6ad66aSAchin Gupta/*
2e0ae9fabSSandrine Bailleux * Copyright (c) 2013-2016, ARM Limited and Contributors. All rights reserved.
34f6ad66aSAchin Gupta *
4*82cb2c1aSdp-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>
11dce74b89SAchin Gupta#include <interrupt_mgmt.h>
125f0cdb05SDan Handley#include <platform_def.h>
1397043ac9SDan Handley#include <runtime_svc.h>
144f6ad66aSAchin Gupta
154f6ad66aSAchin Gupta	.globl	runtime_exceptions
164f6ad66aSAchin Gupta
17a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
18a6ef4393SDouglas Raillard	 * This macro handles Synchronous exceptions.
19a6ef4393SDouglas Raillard	 * Only SMC exceptions are supported.
20a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
21dce74b89SAchin Gupta	 */
22dce74b89SAchin Gupta	.macro	handle_sync_exception
230c8d4fefSAchin Gupta	/* Enable the SError interrupt */
240c8d4fefSAchin Gupta	msr	daifclr, #DAIF_ABT_BIT
250c8d4fefSAchin Gupta
26dce74b89SAchin Gupta	str	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
27872be88aSdp-arm
28872be88aSdp-arm#if ENABLE_RUNTIME_INSTRUMENTATION
29872be88aSdp-arm	/*
30a6ef4393SDouglas Raillard	 * Read the timestamp value and store it in per-cpu data. The value
31a6ef4393SDouglas Raillard	 * will be extracted from per-cpu data by the C level SMC handler and
32a6ef4393SDouglas Raillard	 * saved to the PMF timestamp region.
33872be88aSdp-arm	 */
34872be88aSdp-arm	mrs	x30, cntpct_el0
35872be88aSdp-arm	str	x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29]
36872be88aSdp-arm	mrs	x29, tpidr_el3
37872be88aSdp-arm	str	x30, [x29, #CPU_DATA_PMF_TS0_OFFSET]
38872be88aSdp-arm	ldr	x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29]
39872be88aSdp-arm#endif
40872be88aSdp-arm
41dce74b89SAchin Gupta	mrs	x30, esr_el3
42dce74b89SAchin Gupta	ubfx	x30, x30, #ESR_EC_SHIFT, #ESR_EC_LENGTH
43dce74b89SAchin Gupta
44a6ef4393SDouglas Raillard	/* Handle SMC exceptions separately from other synchronous exceptions */
45dce74b89SAchin Gupta	cmp	x30, #EC_AARCH32_SMC
46dce74b89SAchin Gupta	b.eq	smc_handler32
47dce74b89SAchin Gupta
48dce74b89SAchin Gupta	cmp	x30, #EC_AARCH64_SMC
49dce74b89SAchin Gupta	b.eq	smc_handler64
50dce74b89SAchin Gupta
51a6ef4393SDouglas Raillard	/* Other kinds of synchronous exceptions are not handled */
52a806dad5SJeenu Viswambharan	no_ret	report_unhandled_exception
53dce74b89SAchin Gupta	.endm
54dce74b89SAchin Gupta
55dce74b89SAchin Gupta
56a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
57a6ef4393SDouglas Raillard	 * This macro handles FIQ or IRQ interrupts i.e. EL3, S-EL1 and NS
58a6ef4393SDouglas Raillard	 * interrupts.
59a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
60dce74b89SAchin Gupta	 */
61dce74b89SAchin Gupta	.macro	handle_interrupt_exception label
620c8d4fefSAchin Gupta	/* Enable the SError interrupt */
630c8d4fefSAchin Gupta	msr	daifclr, #DAIF_ABT_BIT
640c8d4fefSAchin Gupta
65dce74b89SAchin Gupta	str	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
66dce74b89SAchin Gupta	bl	save_gp_registers
67dce74b89SAchin Gupta
68a6ef4393SDouglas Raillard	/* Save the EL3 system registers needed to return from this exception */
695717aae1SAchin Gupta	mrs	x0, spsr_el3
705717aae1SAchin Gupta	mrs	x1, elr_el3
715717aae1SAchin Gupta	stp	x0, x1, [sp, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
725717aae1SAchin Gupta
73dce74b89SAchin Gupta	/* Switch to the runtime stack i.e. SP_EL0 */
74dce74b89SAchin Gupta	ldr	x2, [sp, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
75dce74b89SAchin Gupta	mov	x20, sp
76dce74b89SAchin Gupta	msr	spsel, #0
77dce74b89SAchin Gupta	mov	sp, x2
78dce74b89SAchin Gupta
79dce74b89SAchin Gupta	/*
80a6ef4393SDouglas Raillard	 * Find out whether this is a valid interrupt type.
81a6ef4393SDouglas Raillard	 * If the interrupt controller reports a spurious interrupt then return
82a6ef4393SDouglas Raillard	 * to where we came from.
83dce74b89SAchin Gupta	 */
849865ac15SDan Handley	bl	plat_ic_get_pending_interrupt_type
85dce74b89SAchin Gupta	cmp	x0, #INTR_TYPE_INVAL
86dce74b89SAchin Gupta	b.eq	interrupt_exit_\label
87dce74b89SAchin Gupta
88dce74b89SAchin Gupta	/*
89a6ef4393SDouglas Raillard	 * Get the registered handler for this interrupt type.
90a6ef4393SDouglas Raillard	 * A NULL return value could be 'cause of the following conditions:
915717aae1SAchin Gupta	 *
92a6ef4393SDouglas Raillard	 * a. An interrupt of a type was routed correctly but a handler for its
93a6ef4393SDouglas Raillard	 *    type was not registered.
945717aae1SAchin Gupta	 *
95a6ef4393SDouglas Raillard	 * b. An interrupt of a type was not routed correctly so a handler for
96a6ef4393SDouglas Raillard	 *    its type was not registered.
975717aae1SAchin Gupta	 *
98a6ef4393SDouglas Raillard	 * c. An interrupt of a type was routed correctly to EL3, but was
99a6ef4393SDouglas Raillard	 *    deasserted before its pending state could be read. Another
100a6ef4393SDouglas Raillard	 *    interrupt of a different type pended at the same time and its
101a6ef4393SDouglas Raillard	 *    type was reported as pending instead. However, a handler for this
102a6ef4393SDouglas Raillard	 *    type was not registered.
1035717aae1SAchin Gupta	 *
104a6ef4393SDouglas Raillard	 * a. and b. can only happen due to a programming error. The
105a6ef4393SDouglas Raillard	 * occurrence of c. could be beyond the control of Trusted Firmware.
106a6ef4393SDouglas Raillard	 * It makes sense to return from this exception instead of reporting an
107a6ef4393SDouglas Raillard	 * error.
108dce74b89SAchin Gupta	 */
109dce74b89SAchin Gupta	bl	get_interrupt_type_handler
1105717aae1SAchin Gupta	cbz	x0, interrupt_exit_\label
111dce74b89SAchin Gupta	mov	x21, x0
112dce74b89SAchin Gupta
113dce74b89SAchin Gupta	mov	x0, #INTR_ID_UNAVAILABLE
114dce74b89SAchin Gupta
115dce74b89SAchin Gupta	/* Set the current security state in the 'flags' parameter */
116dce74b89SAchin Gupta	mrs	x2, scr_el3
117dce74b89SAchin Gupta	ubfx	x1, x2, #0, #1
118dce74b89SAchin Gupta
119dce74b89SAchin Gupta	/* Restore the reference to the 'handle' i.e. SP_EL3 */
120dce74b89SAchin Gupta	mov	x2, x20
121dce74b89SAchin Gupta
122b460b8bfSSoby Mathew	/* x3 will point to a cookie (not used now) */
123b460b8bfSSoby Mathew	mov	x3, xzr
124b460b8bfSSoby Mathew
125dce74b89SAchin Gupta	/* Call the interrupt type handler */
126dce74b89SAchin Gupta	blr	x21
127dce74b89SAchin Gupta
128dce74b89SAchin Guptainterrupt_exit_\label:
129dce74b89SAchin Gupta	/* Return from exception, possibly in a different security state */
130dce74b89SAchin Gupta	b	el3_exit
131dce74b89SAchin Gupta
132dce74b89SAchin Gupta	.endm
133dce74b89SAchin Gupta
134dce74b89SAchin Gupta
135c3260f9bSSoby Mathew	.macro save_x18_to_x29_sp_el0
136c3260f9bSSoby Mathew	stp	x18, x19, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X18]
137c3260f9bSSoby Mathew	stp	x20, x21, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X20]
138c3260f9bSSoby Mathew	stp	x22, x23, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X22]
139c3260f9bSSoby Mathew	stp	x24, x25, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X24]
140c3260f9bSSoby Mathew	stp	x26, x27, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X26]
141c3260f9bSSoby Mathew	stp	x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28]
142c3260f9bSSoby Mathew	mrs	x18, sp_el0
143c3260f9bSSoby Mathew	str	x18, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_SP_EL0]
144c3260f9bSSoby Mathew	.endm
145c3260f9bSSoby Mathew
146e0ae9fabSSandrine Bailleux
147e0ae9fabSSandrine Bailleuxvector_base runtime_exceptions
148e0ae9fabSSandrine Bailleux
149a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
150a6ef4393SDouglas Raillard	 * Current EL with SP_EL0 : 0x0 - 0x200
151a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
1524f6ad66aSAchin Gupta	 */
153e0ae9fabSSandrine Bailleuxvector_entry sync_exception_sp_el0
154a6ef4393SDouglas Raillard	/* We don't expect any synchronous exceptions from EL3 */
155a806dad5SJeenu Viswambharan	no_ret	report_unhandled_exception
156a7934d69SJeenu Viswambharan	check_vector_size sync_exception_sp_el0
1574f6ad66aSAchin Gupta
158e0ae9fabSSandrine Bailleuxvector_entry irq_sp_el0
159a6ef4393SDouglas Raillard	/*
160a6ef4393SDouglas Raillard	 * EL3 code is non-reentrant. Any asynchronous exception is a serious
161a6ef4393SDouglas Raillard	 * error. Loop infinitely.
162a6ef4393SDouglas Raillard	 */
163a806dad5SJeenu Viswambharan	no_ret	report_unhandled_interrupt
164a7934d69SJeenu Viswambharan	check_vector_size irq_sp_el0
1654f6ad66aSAchin Gupta
166e0ae9fabSSandrine Bailleux
167e0ae9fabSSandrine Bailleuxvector_entry fiq_sp_el0
168a806dad5SJeenu Viswambharan	no_ret	report_unhandled_interrupt
169a7934d69SJeenu Viswambharan	check_vector_size fiq_sp_el0
1704f6ad66aSAchin Gupta
171e0ae9fabSSandrine Bailleux
172e0ae9fabSSandrine Bailleuxvector_entry serror_sp_el0
173a806dad5SJeenu Viswambharan	no_ret	report_unhandled_exception
174a7934d69SJeenu Viswambharan	check_vector_size serror_sp_el0
1754f6ad66aSAchin Gupta
176a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
177a6ef4393SDouglas Raillard	 * Current EL with SP_ELx: 0x200 - 0x400
178a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
1794f6ad66aSAchin Gupta	 */
180e0ae9fabSSandrine Bailleuxvector_entry sync_exception_sp_elx
181a6ef4393SDouglas Raillard	/*
182a6ef4393SDouglas Raillard	 * This exception will trigger if anything went wrong during a previous
183a6ef4393SDouglas Raillard	 * exception entry or exit or while handling an earlier unexpected
184a6ef4393SDouglas Raillard	 * synchronous exception. There is a high probability that SP_EL3 is
185a6ef4393SDouglas Raillard	 * corrupted.
186caa84939SJeenu Viswambharan	 */
187a806dad5SJeenu Viswambharan	no_ret	report_unhandled_exception
188a7934d69SJeenu Viswambharan	check_vector_size sync_exception_sp_elx
1894f6ad66aSAchin Gupta
190e0ae9fabSSandrine Bailleuxvector_entry irq_sp_elx
191a806dad5SJeenu Viswambharan	no_ret	report_unhandled_interrupt
192a7934d69SJeenu Viswambharan	check_vector_size irq_sp_elx
193a7934d69SJeenu Viswambharan
194e0ae9fabSSandrine Bailleuxvector_entry fiq_sp_elx
195a806dad5SJeenu Viswambharan	no_ret	report_unhandled_interrupt
196a7934d69SJeenu Viswambharan	check_vector_size fiq_sp_elx
197a7934d69SJeenu Viswambharan
198e0ae9fabSSandrine Bailleuxvector_entry serror_sp_elx
199a806dad5SJeenu Viswambharan	no_ret	report_unhandled_exception
200a7934d69SJeenu Viswambharan	check_vector_size serror_sp_elx
2014f6ad66aSAchin Gupta
202a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
20344804252SSandrine Bailleux	 * Lower EL using AArch64 : 0x400 - 0x600
204a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
2054f6ad66aSAchin Gupta	 */
206e0ae9fabSSandrine Bailleuxvector_entry sync_exception_aarch64
207a6ef4393SDouglas Raillard	/*
208a6ef4393SDouglas Raillard	 * This exception vector will be the entry point for SMCs and traps
209a6ef4393SDouglas Raillard	 * that are unhandled at lower ELs most commonly. SP_EL3 should point
210a6ef4393SDouglas Raillard	 * to a valid cpu context where the general purpose and system register
211a6ef4393SDouglas Raillard	 * state can be saved.
212caa84939SJeenu Viswambharan	 */
213caa84939SJeenu Viswambharan	handle_sync_exception
214a7934d69SJeenu Viswambharan	check_vector_size sync_exception_aarch64
2154f6ad66aSAchin Gupta
216e0ae9fabSSandrine Bailleuxvector_entry irq_aarch64
217dce74b89SAchin Gupta	handle_interrupt_exception irq_aarch64
218a7934d69SJeenu Viswambharan	check_vector_size irq_aarch64
2194f6ad66aSAchin Gupta
220e0ae9fabSSandrine Bailleuxvector_entry fiq_aarch64
221dce74b89SAchin Gupta	handle_interrupt_exception fiq_aarch64
222a7934d69SJeenu Viswambharan	check_vector_size fiq_aarch64
2234f6ad66aSAchin Gupta
224e0ae9fabSSandrine Bailleuxvector_entry serror_aarch64
225a6ef4393SDouglas Raillard	/*
226a6ef4393SDouglas Raillard	 * SError exceptions from lower ELs are not currently supported.
227a6ef4393SDouglas Raillard	 * Report their occurrence.
228a6ef4393SDouglas Raillard	 */
229a806dad5SJeenu Viswambharan	no_ret	report_unhandled_exception
230a7934d69SJeenu Viswambharan	check_vector_size serror_aarch64
2314f6ad66aSAchin Gupta
232a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
23344804252SSandrine Bailleux	 * Lower EL using AArch32 : 0x600 - 0x800
234a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
2354f6ad66aSAchin Gupta	 */
236e0ae9fabSSandrine Bailleuxvector_entry sync_exception_aarch32
237a6ef4393SDouglas Raillard	/*
238a6ef4393SDouglas Raillard	 * This exception vector will be the entry point for SMCs and traps
239a6ef4393SDouglas Raillard	 * that are unhandled at lower ELs most commonly. SP_EL3 should point
240a6ef4393SDouglas Raillard	 * to a valid cpu context where the general purpose and system register
241a6ef4393SDouglas Raillard	 * state can be saved.
242caa84939SJeenu Viswambharan	 */
243caa84939SJeenu Viswambharan	handle_sync_exception
244a7934d69SJeenu Viswambharan	check_vector_size sync_exception_aarch32
2454f6ad66aSAchin Gupta
246e0ae9fabSSandrine Bailleuxvector_entry irq_aarch32
247dce74b89SAchin Gupta	handle_interrupt_exception irq_aarch32
248a7934d69SJeenu Viswambharan	check_vector_size irq_aarch32
2494f6ad66aSAchin Gupta
250e0ae9fabSSandrine Bailleuxvector_entry fiq_aarch32
251dce74b89SAchin Gupta	handle_interrupt_exception fiq_aarch32
252a7934d69SJeenu Viswambharan	check_vector_size fiq_aarch32
2534f6ad66aSAchin Gupta
254e0ae9fabSSandrine Bailleuxvector_entry serror_aarch32
255a6ef4393SDouglas Raillard	/*
256a6ef4393SDouglas Raillard	 * SError exceptions from lower ELs are not currently supported.
257a6ef4393SDouglas Raillard	 * Report their occurrence.
258a6ef4393SDouglas Raillard	 */
259a806dad5SJeenu Viswambharan	no_ret	report_unhandled_exception
260a7934d69SJeenu Viswambharan	check_vector_size serror_aarch32
261a7934d69SJeenu Viswambharan
262caa84939SJeenu Viswambharan
263a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
264caa84939SJeenu Viswambharan	 * The following code handles secure monitor calls.
265a6ef4393SDouglas Raillard	 * Depending upon the execution state from where the SMC has been
266a6ef4393SDouglas Raillard	 * invoked, it frees some general purpose registers to perform the
267a6ef4393SDouglas Raillard	 * remaining tasks. They involve finding the runtime service handler
268a6ef4393SDouglas Raillard	 * that is the target of the SMC & switching to runtime stacks (SP_EL0)
269a6ef4393SDouglas Raillard	 * before calling the handler.
270caa84939SJeenu Viswambharan	 *
271a6ef4393SDouglas Raillard	 * Note that x30 has been explicitly saved and can be used here
272a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
273caa84939SJeenu Viswambharan	 */
2740a30cf54SAndrew Thoelkefunc smc_handler
275caa84939SJeenu Viswambharansmc_handler32:
276caa84939SJeenu Viswambharan	/* Check whether aarch32 issued an SMC64 */
277caa84939SJeenu Viswambharan	tbnz	x0, #FUNCID_CC_SHIFT, smc_prohibited
278caa84939SJeenu Viswambharan
279a6ef4393SDouglas Raillard	/*
280a6ef4393SDouglas Raillard	 * Since we're are coming from aarch32, x8-x18 need to be saved as per
281a6ef4393SDouglas Raillard	 * SMC32 calling convention. If a lower EL in aarch64 is making an
282a6ef4393SDouglas Raillard	 * SMC32 call then it must have saved x8-x17 already therein.
283caa84939SJeenu Viswambharan	 */
284caa84939SJeenu Viswambharan	stp	x8, x9, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X8]
285caa84939SJeenu Viswambharan	stp	x10, x11, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X10]
286caa84939SJeenu Viswambharan	stp	x12, x13, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X12]
287caa84939SJeenu Viswambharan	stp	x14, x15, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X14]
288caa84939SJeenu Viswambharan	stp	x16, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X16]
289caa84939SJeenu Viswambharan
290caa84939SJeenu Viswambharan	/* x4-x7, x18, sp_el0 are saved below */
291caa84939SJeenu Viswambharan
292caa84939SJeenu Viswambharansmc_handler64:
293a6ef4393SDouglas Raillard	/*
294a6ef4393SDouglas Raillard	 * Populate the parameters for the SMC handler.
295a6ef4393SDouglas Raillard	 * We already have x0-x4 in place. x5 will point to a cookie (not used
296a6ef4393SDouglas Raillard	 * now). x6 will point to the context structure (SP_EL3) and x7 will
297a6ef4393SDouglas Raillard	 * contain flags we need to pass to the handler Hence save x5-x7.
298a6ef4393SDouglas Raillard	 *
299a6ef4393SDouglas Raillard	 * Note: x4 only needs to be preserved for AArch32 callers but we do it
300a6ef4393SDouglas Raillard	 *       for AArch64 callers as well for convenience
301caa84939SJeenu Viswambharan	 */
302caa84939SJeenu Viswambharan	stp	x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4]
303caa84939SJeenu Viswambharan	stp	x6, x7, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X6]
304caa84939SJeenu Viswambharan
305c3260f9bSSoby Mathew	/* Save rest of the gpregs and sp_el0*/
306c3260f9bSSoby Mathew	save_x18_to_x29_sp_el0
307c3260f9bSSoby Mathew
308caa84939SJeenu Viswambharan	mov	x5, xzr
309caa84939SJeenu Viswambharan	mov	x6, sp
310caa84939SJeenu Viswambharan
311caa84939SJeenu Viswambharan	/* Get the unique owning entity number */
312caa84939SJeenu Viswambharan	ubfx	x16, x0, #FUNCID_OEN_SHIFT, #FUNCID_OEN_WIDTH
313caa84939SJeenu Viswambharan	ubfx	x15, x0, #FUNCID_TYPE_SHIFT, #FUNCID_TYPE_WIDTH
314caa84939SJeenu Viswambharan	orr	x16, x16, x15, lsl #FUNCID_OEN_WIDTH
315caa84939SJeenu Viswambharan
316caa84939SJeenu Viswambharan	adr	x11, (__RT_SVC_DESCS_START__ + RT_SVC_DESC_HANDLE)
317caa84939SJeenu Viswambharan
318caa84939SJeenu Viswambharan	/* Load descriptor index from array of indices */
319caa84939SJeenu Viswambharan	adr	x14, rt_svc_descs_indices
320caa84939SJeenu Viswambharan	ldrb	w15, [x14, x16]
321caa84939SJeenu Viswambharan
322a6ef4393SDouglas Raillard	/*
323a6ef4393SDouglas Raillard	 * Restore the saved C runtime stack value which will become the new
324a6ef4393SDouglas Raillard	 * SP_EL0 i.e. EL3 runtime stack. It was saved in the 'cpu_context'
325a6ef4393SDouglas Raillard	 * structure prior to the last ERET from EL3.
326caa84939SJeenu Viswambharan	 */
327caa84939SJeenu Viswambharan	ldr	x12, [x6, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
328caa84939SJeenu Viswambharan
329caa84939SJeenu Viswambharan	/*
330caa84939SJeenu Viswambharan	 * Any index greater than 127 is invalid. Check bit 7 for
331caa84939SJeenu Viswambharan	 * a valid index
332caa84939SJeenu Viswambharan	 */
333caa84939SJeenu Viswambharan	tbnz	w15, 7, smc_unknown
334caa84939SJeenu Viswambharan
335caa84939SJeenu Viswambharan	/* Switch to SP_EL0 */
336caa84939SJeenu Viswambharan	msr	spsel, #0
337caa84939SJeenu Viswambharan
338a6ef4393SDouglas Raillard	/*
339caa84939SJeenu Viswambharan	 * Get the descriptor using the index
340caa84939SJeenu Viswambharan	 * x11 = (base + off), x15 = index
341caa84939SJeenu Viswambharan	 *
342caa84939SJeenu Viswambharan	 * handler = (base + off) + (index << log2(size))
343caa84939SJeenu Viswambharan	 */
344caa84939SJeenu Viswambharan	lsl	w10, w15, #RT_SVC_SIZE_LOG2
345caa84939SJeenu Viswambharan	ldr	x15, [x11, w10, uxtw]
346caa84939SJeenu Viswambharan
347a6ef4393SDouglas Raillard	/*
348a6ef4393SDouglas Raillard	 * Save the SPSR_EL3, ELR_EL3, & SCR_EL3 in case there is a world
349a6ef4393SDouglas Raillard	 * switch during SMC handling.
350a6ef4393SDouglas Raillard	 * TODO: Revisit if all system registers can be saved later.
351caa84939SJeenu Viswambharan	 */
352caa84939SJeenu Viswambharan	mrs	x16, spsr_el3
353caa84939SJeenu Viswambharan	mrs	x17, elr_el3
354caa84939SJeenu Viswambharan	mrs	x18, scr_el3
355caa84939SJeenu Viswambharan	stp	x16, x17, [x6, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
356b51da821SAchin Gupta	str	x18, [x6, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3]
357caa84939SJeenu Viswambharan
358caa84939SJeenu Viswambharan	/* Copy SCR_EL3.NS bit to the flag to indicate caller's security */
359caa84939SJeenu Viswambharan	bfi	x7, x18, #0, #1
360caa84939SJeenu Viswambharan
361caa84939SJeenu Viswambharan	mov	sp, x12
362caa84939SJeenu Viswambharan
363a6ef4393SDouglas Raillard	/*
364a6ef4393SDouglas Raillard	 * Call the Secure Monitor Call handler and then drop directly into
365a6ef4393SDouglas Raillard	 * el3_exit() which will program any remaining architectural state
366a6ef4393SDouglas Raillard	 * prior to issuing the ERET to the desired lower EL.
367caa84939SJeenu Viswambharan	 */
368caa84939SJeenu Viswambharan#if DEBUG
369caa84939SJeenu Viswambharan	cbz	x15, rt_svc_fw_critical_error
370caa84939SJeenu Viswambharan#endif
371caa84939SJeenu Viswambharan	blr	x15
372caa84939SJeenu Viswambharan
373bbf8f6f9SYatharth Kochar	b	el3_exit
3744f6ad66aSAchin Gupta
375caa84939SJeenu Viswambharansmc_unknown:
376caa84939SJeenu Viswambharan	/*
377caa84939SJeenu Viswambharan	 * Here we restore x4-x18 regardless of where we came from. AArch32
378caa84939SJeenu Viswambharan	 * callers will find the registers contents unchanged, but AArch64
379caa84939SJeenu Viswambharan	 * callers will find the registers modified (with stale earlier NS
380caa84939SJeenu Viswambharan	 * content). Either way, we aren't leaking any secure information
381a6ef4393SDouglas Raillard	 * through them.
382caa84939SJeenu Viswambharan	 */
383a43d431bSSoby Mathew	mov	w0, #SMC_UNK
384a43d431bSSoby Mathew	b	restore_gp_registers_callee_eret
385caa84939SJeenu Viswambharan
386caa84939SJeenu Viswambharansmc_prohibited:
387c3260f9bSSoby Mathew	ldr	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
388caa84939SJeenu Viswambharan	mov	w0, #SMC_UNK
389caa84939SJeenu Viswambharan	eret
390caa84939SJeenu Viswambharan
391caa84939SJeenu Viswambharanrt_svc_fw_critical_error:
392a6ef4393SDouglas Raillard	/* Switch to SP_ELx */
393a6ef4393SDouglas Raillard	msr	spsel, #1
394a806dad5SJeenu Viswambharan	no_ret	report_unhandled_exception
3958b779620SKévin Petitendfunc smc_handler
396