xref: /rk3399_ARM-atf/bl31/aarch64/runtime_exceptions.S (revision 4d91838b8d01a9e99c44fd0ed72f5d81b4595f17)
14f6ad66aSAchin Gupta/*
2e0ae9fabSSandrine Bailleux * Copyright (c) 2013-2016, 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>
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 */
52*4d91838bSJulius Werner	ldr	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
53*4d91838bSJulius Werner	b	report_unhandled_exception
54dce74b89SAchin Gupta	.endm
55dce74b89SAchin Gupta
56dce74b89SAchin Gupta
57a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
58a6ef4393SDouglas Raillard	 * This macro handles FIQ or IRQ interrupts i.e. EL3, S-EL1 and NS
59a6ef4393SDouglas Raillard	 * interrupts.
60a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
61dce74b89SAchin Gupta	 */
62dce74b89SAchin Gupta	.macro	handle_interrupt_exception label
630c8d4fefSAchin Gupta	/* Enable the SError interrupt */
640c8d4fefSAchin Gupta	msr	daifclr, #DAIF_ABT_BIT
650c8d4fefSAchin Gupta
66dce74b89SAchin Gupta	str	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
67dce74b89SAchin Gupta	bl	save_gp_registers
68dce74b89SAchin Gupta
69a6ef4393SDouglas Raillard	/* Save the EL3 system registers needed to return from this exception */
705717aae1SAchin Gupta	mrs	x0, spsr_el3
715717aae1SAchin Gupta	mrs	x1, elr_el3
725717aae1SAchin Gupta	stp	x0, x1, [sp, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
735717aae1SAchin Gupta
74dce74b89SAchin Gupta	/* Switch to the runtime stack i.e. SP_EL0 */
75dce74b89SAchin Gupta	ldr	x2, [sp, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
76dce74b89SAchin Gupta	mov	x20, sp
77dce74b89SAchin Gupta	msr	spsel, #0
78dce74b89SAchin Gupta	mov	sp, x2
79dce74b89SAchin Gupta
80dce74b89SAchin Gupta	/*
81a6ef4393SDouglas Raillard	 * Find out whether this is a valid interrupt type.
82a6ef4393SDouglas Raillard	 * If the interrupt controller reports a spurious interrupt then return
83a6ef4393SDouglas Raillard	 * to where we came from.
84dce74b89SAchin Gupta	 */
859865ac15SDan Handley	bl	plat_ic_get_pending_interrupt_type
86dce74b89SAchin Gupta	cmp	x0, #INTR_TYPE_INVAL
87dce74b89SAchin Gupta	b.eq	interrupt_exit_\label
88dce74b89SAchin Gupta
89dce74b89SAchin Gupta	/*
90a6ef4393SDouglas Raillard	 * Get the registered handler for this interrupt type.
91a6ef4393SDouglas Raillard	 * A NULL return value could be 'cause of the following conditions:
925717aae1SAchin Gupta	 *
93a6ef4393SDouglas Raillard	 * a. An interrupt of a type was routed correctly but a handler for its
94a6ef4393SDouglas Raillard	 *    type was not registered.
955717aae1SAchin Gupta	 *
96a6ef4393SDouglas Raillard	 * b. An interrupt of a type was not routed correctly so a handler for
97a6ef4393SDouglas Raillard	 *    its type was not registered.
985717aae1SAchin Gupta	 *
99a6ef4393SDouglas Raillard	 * c. An interrupt of a type was routed correctly to EL3, but was
100a6ef4393SDouglas Raillard	 *    deasserted before its pending state could be read. Another
101a6ef4393SDouglas Raillard	 *    interrupt of a different type pended at the same time and its
102a6ef4393SDouglas Raillard	 *    type was reported as pending instead. However, a handler for this
103a6ef4393SDouglas Raillard	 *    type was not registered.
1045717aae1SAchin Gupta	 *
105a6ef4393SDouglas Raillard	 * a. and b. can only happen due to a programming error. The
106a6ef4393SDouglas Raillard	 * occurrence of c. could be beyond the control of Trusted Firmware.
107a6ef4393SDouglas Raillard	 * It makes sense to return from this exception instead of reporting an
108a6ef4393SDouglas Raillard	 * error.
109dce74b89SAchin Gupta	 */
110dce74b89SAchin Gupta	bl	get_interrupt_type_handler
1115717aae1SAchin Gupta	cbz	x0, interrupt_exit_\label
112dce74b89SAchin Gupta	mov	x21, x0
113dce74b89SAchin Gupta
114dce74b89SAchin Gupta	mov	x0, #INTR_ID_UNAVAILABLE
115dce74b89SAchin Gupta
116dce74b89SAchin Gupta	/* Set the current security state in the 'flags' parameter */
117dce74b89SAchin Gupta	mrs	x2, scr_el3
118dce74b89SAchin Gupta	ubfx	x1, x2, #0, #1
119dce74b89SAchin Gupta
120dce74b89SAchin Gupta	/* Restore the reference to the 'handle' i.e. SP_EL3 */
121dce74b89SAchin Gupta	mov	x2, x20
122dce74b89SAchin Gupta
123b460b8bfSSoby Mathew	/* x3 will point to a cookie (not used now) */
124b460b8bfSSoby Mathew	mov	x3, xzr
125b460b8bfSSoby Mathew
126dce74b89SAchin Gupta	/* Call the interrupt type handler */
127dce74b89SAchin Gupta	blr	x21
128dce74b89SAchin Gupta
129dce74b89SAchin Guptainterrupt_exit_\label:
130dce74b89SAchin Gupta	/* Return from exception, possibly in a different security state */
131dce74b89SAchin Gupta	b	el3_exit
132dce74b89SAchin Gupta
133dce74b89SAchin Gupta	.endm
134dce74b89SAchin Gupta
135dce74b89SAchin Gupta
136c3260f9bSSoby Mathew	.macro save_x18_to_x29_sp_el0
137c3260f9bSSoby Mathew	stp	x18, x19, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X18]
138c3260f9bSSoby Mathew	stp	x20, x21, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X20]
139c3260f9bSSoby Mathew	stp	x22, x23, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X22]
140c3260f9bSSoby Mathew	stp	x24, x25, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X24]
141c3260f9bSSoby Mathew	stp	x26, x27, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X26]
142c3260f9bSSoby Mathew	stp	x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28]
143c3260f9bSSoby Mathew	mrs	x18, sp_el0
144c3260f9bSSoby Mathew	str	x18, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_SP_EL0]
145c3260f9bSSoby Mathew	.endm
146c3260f9bSSoby Mathew
147e0ae9fabSSandrine Bailleux
148e0ae9fabSSandrine Bailleuxvector_base runtime_exceptions
149e0ae9fabSSandrine Bailleux
150a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
151a6ef4393SDouglas Raillard	 * Current EL with SP_EL0 : 0x0 - 0x200
152a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
1534f6ad66aSAchin Gupta	 */
154e0ae9fabSSandrine Bailleuxvector_entry sync_exception_sp_el0
155a6ef4393SDouglas Raillard	/* We don't expect any synchronous exceptions from EL3 */
156*4d91838bSJulius Werner	b	report_unhandled_exception
157a7934d69SJeenu Viswambharan	check_vector_size sync_exception_sp_el0
1584f6ad66aSAchin Gupta
159e0ae9fabSSandrine Bailleuxvector_entry irq_sp_el0
160a6ef4393SDouglas Raillard	/*
161a6ef4393SDouglas Raillard	 * EL3 code is non-reentrant. Any asynchronous exception is a serious
162a6ef4393SDouglas Raillard	 * error. Loop infinitely.
163a6ef4393SDouglas Raillard	 */
164*4d91838bSJulius Werner	b	report_unhandled_interrupt
165a7934d69SJeenu Viswambharan	check_vector_size irq_sp_el0
1664f6ad66aSAchin Gupta
167e0ae9fabSSandrine Bailleux
168e0ae9fabSSandrine Bailleuxvector_entry fiq_sp_el0
169*4d91838bSJulius Werner	b	report_unhandled_interrupt
170a7934d69SJeenu Viswambharan	check_vector_size fiq_sp_el0
1714f6ad66aSAchin Gupta
172e0ae9fabSSandrine Bailleux
173e0ae9fabSSandrine Bailleuxvector_entry serror_sp_el0
174*4d91838bSJulius Werner	b	report_unhandled_exception
175a7934d69SJeenu Viswambharan	check_vector_size serror_sp_el0
1764f6ad66aSAchin Gupta
177a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
178a6ef4393SDouglas Raillard	 * Current EL with SP_ELx: 0x200 - 0x400
179a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
1804f6ad66aSAchin Gupta	 */
181e0ae9fabSSandrine Bailleuxvector_entry sync_exception_sp_elx
182a6ef4393SDouglas Raillard	/*
183a6ef4393SDouglas Raillard	 * This exception will trigger if anything went wrong during a previous
184a6ef4393SDouglas Raillard	 * exception entry or exit or while handling an earlier unexpected
185a6ef4393SDouglas Raillard	 * synchronous exception. There is a high probability that SP_EL3 is
186a6ef4393SDouglas Raillard	 * corrupted.
187caa84939SJeenu Viswambharan	 */
188*4d91838bSJulius Werner	b	report_unhandled_exception
189a7934d69SJeenu Viswambharan	check_vector_size sync_exception_sp_elx
1904f6ad66aSAchin Gupta
191e0ae9fabSSandrine Bailleuxvector_entry irq_sp_elx
192*4d91838bSJulius Werner	b	report_unhandled_interrupt
193a7934d69SJeenu Viswambharan	check_vector_size irq_sp_elx
194a7934d69SJeenu Viswambharan
195e0ae9fabSSandrine Bailleuxvector_entry fiq_sp_elx
196*4d91838bSJulius Werner	b	report_unhandled_interrupt
197a7934d69SJeenu Viswambharan	check_vector_size fiq_sp_elx
198a7934d69SJeenu Viswambharan
199e0ae9fabSSandrine Bailleuxvector_entry serror_sp_elx
200*4d91838bSJulius Werner	b	report_unhandled_exception
201a7934d69SJeenu Viswambharan	check_vector_size serror_sp_elx
2024f6ad66aSAchin Gupta
203a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
20444804252SSandrine Bailleux	 * Lower EL using AArch64 : 0x400 - 0x600
205a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
2064f6ad66aSAchin Gupta	 */
207e0ae9fabSSandrine Bailleuxvector_entry sync_exception_aarch64
208a6ef4393SDouglas Raillard	/*
209a6ef4393SDouglas Raillard	 * This exception vector will be the entry point for SMCs and traps
210a6ef4393SDouglas Raillard	 * that are unhandled at lower ELs most commonly. SP_EL3 should point
211a6ef4393SDouglas Raillard	 * to a valid cpu context where the general purpose and system register
212a6ef4393SDouglas Raillard	 * state can be saved.
213caa84939SJeenu Viswambharan	 */
214caa84939SJeenu Viswambharan	handle_sync_exception
215a7934d69SJeenu Viswambharan	check_vector_size sync_exception_aarch64
2164f6ad66aSAchin Gupta
217e0ae9fabSSandrine Bailleuxvector_entry irq_aarch64
218dce74b89SAchin Gupta	handle_interrupt_exception irq_aarch64
219a7934d69SJeenu Viswambharan	check_vector_size irq_aarch64
2204f6ad66aSAchin Gupta
221e0ae9fabSSandrine Bailleuxvector_entry fiq_aarch64
222dce74b89SAchin Gupta	handle_interrupt_exception fiq_aarch64
223a7934d69SJeenu Viswambharan	check_vector_size fiq_aarch64
2244f6ad66aSAchin Gupta
225e0ae9fabSSandrine Bailleuxvector_entry serror_aarch64
226a6ef4393SDouglas Raillard	/*
227a6ef4393SDouglas Raillard	 * SError exceptions from lower ELs are not currently supported.
228a6ef4393SDouglas Raillard	 * Report their occurrence.
229a6ef4393SDouglas Raillard	 */
230*4d91838bSJulius Werner	b	report_unhandled_exception
231a7934d69SJeenu Viswambharan	check_vector_size serror_aarch64
2324f6ad66aSAchin Gupta
233a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
23444804252SSandrine Bailleux	 * Lower EL using AArch32 : 0x600 - 0x800
235a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
2364f6ad66aSAchin Gupta	 */
237e0ae9fabSSandrine Bailleuxvector_entry sync_exception_aarch32
238a6ef4393SDouglas Raillard	/*
239a6ef4393SDouglas Raillard	 * This exception vector will be the entry point for SMCs and traps
240a6ef4393SDouglas Raillard	 * that are unhandled at lower ELs most commonly. SP_EL3 should point
241a6ef4393SDouglas Raillard	 * to a valid cpu context where the general purpose and system register
242a6ef4393SDouglas Raillard	 * state can be saved.
243caa84939SJeenu Viswambharan	 */
244caa84939SJeenu Viswambharan	handle_sync_exception
245a7934d69SJeenu Viswambharan	check_vector_size sync_exception_aarch32
2464f6ad66aSAchin Gupta
247e0ae9fabSSandrine Bailleuxvector_entry irq_aarch32
248dce74b89SAchin Gupta	handle_interrupt_exception irq_aarch32
249a7934d69SJeenu Viswambharan	check_vector_size irq_aarch32
2504f6ad66aSAchin Gupta
251e0ae9fabSSandrine Bailleuxvector_entry fiq_aarch32
252dce74b89SAchin Gupta	handle_interrupt_exception fiq_aarch32
253a7934d69SJeenu Viswambharan	check_vector_size fiq_aarch32
2544f6ad66aSAchin Gupta
255e0ae9fabSSandrine Bailleuxvector_entry serror_aarch32
256a6ef4393SDouglas Raillard	/*
257a6ef4393SDouglas Raillard	 * SError exceptions from lower ELs are not currently supported.
258a6ef4393SDouglas Raillard	 * Report their occurrence.
259a6ef4393SDouglas Raillard	 */
260*4d91838bSJulius Werner	b	report_unhandled_exception
261a7934d69SJeenu Viswambharan	check_vector_size serror_aarch32
262a7934d69SJeenu Viswambharan
263caa84939SJeenu Viswambharan
264a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
265caa84939SJeenu Viswambharan	 * The following code handles secure monitor calls.
266a6ef4393SDouglas Raillard	 * Depending upon the execution state from where the SMC has been
267a6ef4393SDouglas Raillard	 * invoked, it frees some general purpose registers to perform the
268a6ef4393SDouglas Raillard	 * remaining tasks. They involve finding the runtime service handler
269a6ef4393SDouglas Raillard	 * that is the target of the SMC & switching to runtime stacks (SP_EL0)
270a6ef4393SDouglas Raillard	 * before calling the handler.
271caa84939SJeenu Viswambharan	 *
272a6ef4393SDouglas Raillard	 * Note that x30 has been explicitly saved and can be used here
273a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
274caa84939SJeenu Viswambharan	 */
2750a30cf54SAndrew Thoelkefunc smc_handler
276caa84939SJeenu Viswambharansmc_handler32:
277caa84939SJeenu Viswambharan	/* Check whether aarch32 issued an SMC64 */
278caa84939SJeenu Viswambharan	tbnz	x0, #FUNCID_CC_SHIFT, smc_prohibited
279caa84939SJeenu Viswambharan
280a6ef4393SDouglas Raillard	/*
281a6ef4393SDouglas Raillard	 * Since we're are coming from aarch32, x8-x18 need to be saved as per
282a6ef4393SDouglas Raillard	 * SMC32 calling convention. If a lower EL in aarch64 is making an
283a6ef4393SDouglas Raillard	 * SMC32 call then it must have saved x8-x17 already therein.
284caa84939SJeenu Viswambharan	 */
285caa84939SJeenu Viswambharan	stp	x8, x9, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X8]
286caa84939SJeenu Viswambharan	stp	x10, x11, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X10]
287caa84939SJeenu Viswambharan	stp	x12, x13, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X12]
288caa84939SJeenu Viswambharan	stp	x14, x15, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X14]
289caa84939SJeenu Viswambharan	stp	x16, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X16]
290caa84939SJeenu Viswambharan
291caa84939SJeenu Viswambharan	/* x4-x7, x18, sp_el0 are saved below */
292caa84939SJeenu Viswambharan
293caa84939SJeenu Viswambharansmc_handler64:
294a6ef4393SDouglas Raillard	/*
295a6ef4393SDouglas Raillard	 * Populate the parameters for the SMC handler.
296a6ef4393SDouglas Raillard	 * We already have x0-x4 in place. x5 will point to a cookie (not used
297a6ef4393SDouglas Raillard	 * now). x6 will point to the context structure (SP_EL3) and x7 will
298a6ef4393SDouglas Raillard	 * contain flags we need to pass to the handler Hence save x5-x7.
299a6ef4393SDouglas Raillard	 *
300a6ef4393SDouglas Raillard	 * Note: x4 only needs to be preserved for AArch32 callers but we do it
301a6ef4393SDouglas Raillard	 *       for AArch64 callers as well for convenience
302caa84939SJeenu Viswambharan	 */
303caa84939SJeenu Viswambharan	stp	x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4]
304caa84939SJeenu Viswambharan	stp	x6, x7, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X6]
305caa84939SJeenu Viswambharan
306c3260f9bSSoby Mathew	/* Save rest of the gpregs and sp_el0*/
307c3260f9bSSoby Mathew	save_x18_to_x29_sp_el0
308c3260f9bSSoby Mathew
309caa84939SJeenu Viswambharan	mov	x5, xzr
310caa84939SJeenu Viswambharan	mov	x6, sp
311caa84939SJeenu Viswambharan
312caa84939SJeenu Viswambharan	/* Get the unique owning entity number */
313caa84939SJeenu Viswambharan	ubfx	x16, x0, #FUNCID_OEN_SHIFT, #FUNCID_OEN_WIDTH
314caa84939SJeenu Viswambharan	ubfx	x15, x0, #FUNCID_TYPE_SHIFT, #FUNCID_TYPE_WIDTH
315caa84939SJeenu Viswambharan	orr	x16, x16, x15, lsl #FUNCID_OEN_WIDTH
316caa84939SJeenu Viswambharan
317caa84939SJeenu Viswambharan	adr	x11, (__RT_SVC_DESCS_START__ + RT_SVC_DESC_HANDLE)
318caa84939SJeenu Viswambharan
319caa84939SJeenu Viswambharan	/* Load descriptor index from array of indices */
320caa84939SJeenu Viswambharan	adr	x14, rt_svc_descs_indices
321caa84939SJeenu Viswambharan	ldrb	w15, [x14, x16]
322caa84939SJeenu Viswambharan
323a6ef4393SDouglas Raillard	/*
324a6ef4393SDouglas Raillard	 * Restore the saved C runtime stack value which will become the new
325a6ef4393SDouglas Raillard	 * SP_EL0 i.e. EL3 runtime stack. It was saved in the 'cpu_context'
326a6ef4393SDouglas Raillard	 * structure prior to the last ERET from EL3.
327caa84939SJeenu Viswambharan	 */
328caa84939SJeenu Viswambharan	ldr	x12, [x6, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
329caa84939SJeenu Viswambharan
330caa84939SJeenu Viswambharan	/*
331caa84939SJeenu Viswambharan	 * Any index greater than 127 is invalid. Check bit 7 for
332caa84939SJeenu Viswambharan	 * a valid index
333caa84939SJeenu Viswambharan	 */
334caa84939SJeenu Viswambharan	tbnz	w15, 7, smc_unknown
335caa84939SJeenu Viswambharan
336caa84939SJeenu Viswambharan	/* Switch to SP_EL0 */
337caa84939SJeenu Viswambharan	msr	spsel, #0
338caa84939SJeenu Viswambharan
339a6ef4393SDouglas Raillard	/*
340caa84939SJeenu Viswambharan	 * Get the descriptor using the index
341caa84939SJeenu Viswambharan	 * x11 = (base + off), x15 = index
342caa84939SJeenu Viswambharan	 *
343caa84939SJeenu Viswambharan	 * handler = (base + off) + (index << log2(size))
344caa84939SJeenu Viswambharan	 */
345caa84939SJeenu Viswambharan	lsl	w10, w15, #RT_SVC_SIZE_LOG2
346caa84939SJeenu Viswambharan	ldr	x15, [x11, w10, uxtw]
347caa84939SJeenu Viswambharan
348a6ef4393SDouglas Raillard	/*
349a6ef4393SDouglas Raillard	 * Save the SPSR_EL3, ELR_EL3, & SCR_EL3 in case there is a world
350a6ef4393SDouglas Raillard	 * switch during SMC handling.
351a6ef4393SDouglas Raillard	 * TODO: Revisit if all system registers can be saved later.
352caa84939SJeenu Viswambharan	 */
353caa84939SJeenu Viswambharan	mrs	x16, spsr_el3
354caa84939SJeenu Viswambharan	mrs	x17, elr_el3
355caa84939SJeenu Viswambharan	mrs	x18, scr_el3
356caa84939SJeenu Viswambharan	stp	x16, x17, [x6, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
357b51da821SAchin Gupta	str	x18, [x6, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3]
358caa84939SJeenu Viswambharan
359caa84939SJeenu Viswambharan	/* Copy SCR_EL3.NS bit to the flag to indicate caller's security */
360caa84939SJeenu Viswambharan	bfi	x7, x18, #0, #1
361caa84939SJeenu Viswambharan
362caa84939SJeenu Viswambharan	mov	sp, x12
363caa84939SJeenu Viswambharan
364a6ef4393SDouglas Raillard	/*
365a6ef4393SDouglas Raillard	 * Call the Secure Monitor Call handler and then drop directly into
366a6ef4393SDouglas Raillard	 * el3_exit() which will program any remaining architectural state
367a6ef4393SDouglas Raillard	 * prior to issuing the ERET to the desired lower EL.
368caa84939SJeenu Viswambharan	 */
369caa84939SJeenu Viswambharan#if DEBUG
370caa84939SJeenu Viswambharan	cbz	x15, rt_svc_fw_critical_error
371caa84939SJeenu Viswambharan#endif
372caa84939SJeenu Viswambharan	blr	x15
373caa84939SJeenu Viswambharan
374bbf8f6f9SYatharth Kochar	b	el3_exit
3754f6ad66aSAchin Gupta
376caa84939SJeenu Viswambharansmc_unknown:
377caa84939SJeenu Viswambharan	/*
378caa84939SJeenu Viswambharan	 * Here we restore x4-x18 regardless of where we came from. AArch32
379caa84939SJeenu Viswambharan	 * callers will find the registers contents unchanged, but AArch64
380caa84939SJeenu Viswambharan	 * callers will find the registers modified (with stale earlier NS
381caa84939SJeenu Viswambharan	 * content). Either way, we aren't leaking any secure information
382a6ef4393SDouglas Raillard	 * through them.
383caa84939SJeenu Viswambharan	 */
384a43d431bSSoby Mathew	mov	w0, #SMC_UNK
385a43d431bSSoby Mathew	b	restore_gp_registers_callee_eret
386caa84939SJeenu Viswambharan
387caa84939SJeenu Viswambharansmc_prohibited:
388c3260f9bSSoby Mathew	ldr	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
389caa84939SJeenu Viswambharan	mov	w0, #SMC_UNK
390caa84939SJeenu Viswambharan	eret
391caa84939SJeenu Viswambharan
392caa84939SJeenu Viswambharanrt_svc_fw_critical_error:
393a6ef4393SDouglas Raillard	/* Switch to SP_ELx */
394a6ef4393SDouglas Raillard	msr	spsel, #1
395a806dad5SJeenu Viswambharan	no_ret	report_unhandled_exception
3968b779620SKévin Petitendfunc smc_handler
397