xref: /rk3399_ARM-atf/bl31/aarch64/runtime_exceptions.S (revision 201ca5b6b49197a2c22b3bb0b519d63758fff0b7)
14f6ad66aSAchin Gupta/*
2*201ca5b6SDimitris 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>
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
17f62ad322SDimitris Papastamos	.globl	sync_exception_sp_el0
18f62ad322SDimitris Papastamos	.globl	irq_sp_el0
19f62ad322SDimitris Papastamos	.globl	fiq_sp_el0
20f62ad322SDimitris Papastamos	.globl	serror_sp_el0
21f62ad322SDimitris Papastamos
22f62ad322SDimitris Papastamos	.globl	sync_exception_sp_elx
23f62ad322SDimitris Papastamos	.globl	irq_sp_elx
24f62ad322SDimitris Papastamos	.globl	fiq_sp_elx
25f62ad322SDimitris Papastamos	.globl	serror_sp_elx
26f62ad322SDimitris Papastamos
27f62ad322SDimitris Papastamos	.globl	sync_exception_aarch64
28f62ad322SDimitris Papastamos	.globl	irq_aarch64
29f62ad322SDimitris Papastamos	.globl	fiq_aarch64
30f62ad322SDimitris Papastamos	.globl	serror_aarch64
31f62ad322SDimitris Papastamos
32f62ad322SDimitris Papastamos	.globl	sync_exception_aarch32
33f62ad322SDimitris Papastamos	.globl	irq_aarch32
34f62ad322SDimitris Papastamos	.globl	fiq_aarch32
35f62ad322SDimitris Papastamos	.globl	serror_aarch32
36f62ad322SDimitris Papastamos
37a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
38a6ef4393SDouglas Raillard	 * This macro handles Synchronous exceptions.
39a6ef4393SDouglas Raillard	 * Only SMC exceptions are supported.
40a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
41dce74b89SAchin Gupta	 */
42dce74b89SAchin Gupta	.macro	handle_sync_exception
430c8d4fefSAchin Gupta	/* Enable the SError interrupt */
440c8d4fefSAchin Gupta	msr	daifclr, #DAIF_ABT_BIT
450c8d4fefSAchin Gupta
46dce74b89SAchin Gupta	str	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
47872be88aSdp-arm
48872be88aSdp-arm#if ENABLE_RUNTIME_INSTRUMENTATION
49872be88aSdp-arm	/*
50a6ef4393SDouglas Raillard	 * Read the timestamp value and store it in per-cpu data. The value
51a6ef4393SDouglas Raillard	 * will be extracted from per-cpu data by the C level SMC handler and
52a6ef4393SDouglas Raillard	 * saved to the PMF timestamp region.
53872be88aSdp-arm	 */
54872be88aSdp-arm	mrs	x30, cntpct_el0
55872be88aSdp-arm	str	x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29]
56872be88aSdp-arm	mrs	x29, tpidr_el3
57872be88aSdp-arm	str	x30, [x29, #CPU_DATA_PMF_TS0_OFFSET]
58872be88aSdp-arm	ldr	x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29]
59872be88aSdp-arm#endif
60872be88aSdp-arm
61dce74b89SAchin Gupta	mrs	x30, esr_el3
62dce74b89SAchin Gupta	ubfx	x30, x30, #ESR_EC_SHIFT, #ESR_EC_LENGTH
63dce74b89SAchin Gupta
64a6ef4393SDouglas Raillard	/* Handle SMC exceptions separately from other synchronous exceptions */
65dce74b89SAchin Gupta	cmp	x30, #EC_AARCH32_SMC
66dce74b89SAchin Gupta	b.eq	smc_handler32
67dce74b89SAchin Gupta
68dce74b89SAchin Gupta	cmp	x30, #EC_AARCH64_SMC
69dce74b89SAchin Gupta	b.eq	smc_handler64
70dce74b89SAchin Gupta
71a6ef4393SDouglas Raillard	/* Other kinds of synchronous exceptions are not handled */
724d91838bSJulius Werner	ldr	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
734d91838bSJulius Werner	b	report_unhandled_exception
74dce74b89SAchin Gupta	.endm
75dce74b89SAchin Gupta
76dce74b89SAchin Gupta
77a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
78a6ef4393SDouglas Raillard	 * This macro handles FIQ or IRQ interrupts i.e. EL3, S-EL1 and NS
79a6ef4393SDouglas Raillard	 * interrupts.
80a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
81dce74b89SAchin Gupta	 */
82dce74b89SAchin Gupta	.macro	handle_interrupt_exception label
830c8d4fefSAchin Gupta	/* Enable the SError interrupt */
840c8d4fefSAchin Gupta	msr	daifclr, #DAIF_ABT_BIT
850c8d4fefSAchin Gupta
86dce74b89SAchin Gupta	str	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
87dce74b89SAchin Gupta	bl	save_gp_registers
88dce74b89SAchin Gupta
89a6ef4393SDouglas Raillard	/* Save the EL3 system registers needed to return from this exception */
905717aae1SAchin Gupta	mrs	x0, spsr_el3
915717aae1SAchin Gupta	mrs	x1, elr_el3
925717aae1SAchin Gupta	stp	x0, x1, [sp, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
935717aae1SAchin Gupta
94dce74b89SAchin Gupta	/* Switch to the runtime stack i.e. SP_EL0 */
95dce74b89SAchin Gupta	ldr	x2, [sp, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
96dce74b89SAchin Gupta	mov	x20, sp
97dce74b89SAchin Gupta	msr	spsel, #0
98dce74b89SAchin Gupta	mov	sp, x2
99dce74b89SAchin Gupta
100dce74b89SAchin Gupta	/*
101a6ef4393SDouglas Raillard	 * Find out whether this is a valid interrupt type.
102a6ef4393SDouglas Raillard	 * If the interrupt controller reports a spurious interrupt then return
103a6ef4393SDouglas Raillard	 * to where we came from.
104dce74b89SAchin Gupta	 */
1059865ac15SDan Handley	bl	plat_ic_get_pending_interrupt_type
106dce74b89SAchin Gupta	cmp	x0, #INTR_TYPE_INVAL
107dce74b89SAchin Gupta	b.eq	interrupt_exit_\label
108dce74b89SAchin Gupta
109dce74b89SAchin Gupta	/*
110a6ef4393SDouglas Raillard	 * Get the registered handler for this interrupt type.
111a6ef4393SDouglas Raillard	 * A NULL return value could be 'cause of the following conditions:
1125717aae1SAchin Gupta	 *
113a6ef4393SDouglas Raillard	 * a. An interrupt of a type was routed correctly but a handler for its
114a6ef4393SDouglas Raillard	 *    type was not registered.
1155717aae1SAchin Gupta	 *
116a6ef4393SDouglas Raillard	 * b. An interrupt of a type was not routed correctly so a handler for
117a6ef4393SDouglas Raillard	 *    its type was not registered.
1185717aae1SAchin Gupta	 *
119a6ef4393SDouglas Raillard	 * c. An interrupt of a type was routed correctly to EL3, but was
120a6ef4393SDouglas Raillard	 *    deasserted before its pending state could be read. Another
121a6ef4393SDouglas Raillard	 *    interrupt of a different type pended at the same time and its
122a6ef4393SDouglas Raillard	 *    type was reported as pending instead. However, a handler for this
123a6ef4393SDouglas Raillard	 *    type was not registered.
1245717aae1SAchin Gupta	 *
125a6ef4393SDouglas Raillard	 * a. and b. can only happen due to a programming error. The
126a6ef4393SDouglas Raillard	 * occurrence of c. could be beyond the control of Trusted Firmware.
127a6ef4393SDouglas Raillard	 * It makes sense to return from this exception instead of reporting an
128a6ef4393SDouglas Raillard	 * error.
129dce74b89SAchin Gupta	 */
130dce74b89SAchin Gupta	bl	get_interrupt_type_handler
1315717aae1SAchin Gupta	cbz	x0, interrupt_exit_\label
132dce74b89SAchin Gupta	mov	x21, x0
133dce74b89SAchin Gupta
134dce74b89SAchin Gupta	mov	x0, #INTR_ID_UNAVAILABLE
135dce74b89SAchin Gupta
136dce74b89SAchin Gupta	/* Set the current security state in the 'flags' parameter */
137dce74b89SAchin Gupta	mrs	x2, scr_el3
138dce74b89SAchin Gupta	ubfx	x1, x2, #0, #1
139dce74b89SAchin Gupta
140dce74b89SAchin Gupta	/* Restore the reference to the 'handle' i.e. SP_EL3 */
141dce74b89SAchin Gupta	mov	x2, x20
142dce74b89SAchin Gupta
143b460b8bfSSoby Mathew	/* x3 will point to a cookie (not used now) */
144b460b8bfSSoby Mathew	mov	x3, xzr
145b460b8bfSSoby Mathew
146dce74b89SAchin Gupta	/* Call the interrupt type handler */
147dce74b89SAchin Gupta	blr	x21
148dce74b89SAchin Gupta
149dce74b89SAchin Guptainterrupt_exit_\label:
150dce74b89SAchin Gupta	/* Return from exception, possibly in a different security state */
151dce74b89SAchin Gupta	b	el3_exit
152dce74b89SAchin Gupta
153dce74b89SAchin Gupta	.endm
154dce74b89SAchin Gupta
155dce74b89SAchin Gupta
156*201ca5b6SDimitris Papastamos	.macro save_x4_to_x29_sp_el0
157*201ca5b6SDimitris Papastamos	stp	x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4]
158*201ca5b6SDimitris Papastamos	stp	x6, x7, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X6]
159*201ca5b6SDimitris Papastamos	stp	x8, x9, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X8]
160*201ca5b6SDimitris Papastamos	stp	x10, x11, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X10]
161*201ca5b6SDimitris Papastamos	stp	x12, x13, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X12]
162*201ca5b6SDimitris Papastamos	stp	x14, x15, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X14]
163*201ca5b6SDimitris Papastamos	stp	x16, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X16]
164c3260f9bSSoby Mathew	stp	x18, x19, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X18]
165c3260f9bSSoby Mathew	stp	x20, x21, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X20]
166c3260f9bSSoby Mathew	stp	x22, x23, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X22]
167c3260f9bSSoby Mathew	stp	x24, x25, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X24]
168c3260f9bSSoby Mathew	stp	x26, x27, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X26]
169c3260f9bSSoby Mathew	stp	x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28]
170c3260f9bSSoby Mathew	mrs	x18, sp_el0
171c3260f9bSSoby Mathew	str	x18, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_SP_EL0]
172c3260f9bSSoby Mathew	.endm
173c3260f9bSSoby Mathew
174e0ae9fabSSandrine Bailleux
175e0ae9fabSSandrine Bailleuxvector_base runtime_exceptions
176e0ae9fabSSandrine Bailleux
177a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
178a6ef4393SDouglas Raillard	 * Current EL with SP_EL0 : 0x0 - 0x200
179a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
1804f6ad66aSAchin Gupta	 */
181e0ae9fabSSandrine Bailleuxvector_entry sync_exception_sp_el0
182a6ef4393SDouglas Raillard	/* We don't expect any synchronous exceptions from EL3 */
1834d91838bSJulius Werner	b	report_unhandled_exception
184a7934d69SJeenu Viswambharan	check_vector_size sync_exception_sp_el0
1854f6ad66aSAchin Gupta
186e0ae9fabSSandrine Bailleuxvector_entry irq_sp_el0
187a6ef4393SDouglas Raillard	/*
188a6ef4393SDouglas Raillard	 * EL3 code is non-reentrant. Any asynchronous exception is a serious
189a6ef4393SDouglas Raillard	 * error. Loop infinitely.
190a6ef4393SDouglas Raillard	 */
1914d91838bSJulius Werner	b	report_unhandled_interrupt
192a7934d69SJeenu Viswambharan	check_vector_size irq_sp_el0
1934f6ad66aSAchin Gupta
194e0ae9fabSSandrine Bailleux
195e0ae9fabSSandrine Bailleuxvector_entry fiq_sp_el0
1964d91838bSJulius Werner	b	report_unhandled_interrupt
197a7934d69SJeenu Viswambharan	check_vector_size fiq_sp_el0
1984f6ad66aSAchin Gupta
199e0ae9fabSSandrine Bailleux
200e0ae9fabSSandrine Bailleuxvector_entry serror_sp_el0
2014d91838bSJulius Werner	b	report_unhandled_exception
202a7934d69SJeenu Viswambharan	check_vector_size serror_sp_el0
2034f6ad66aSAchin Gupta
204a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
205a6ef4393SDouglas Raillard	 * Current EL with SP_ELx: 0x200 - 0x400
206a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
2074f6ad66aSAchin Gupta	 */
208e0ae9fabSSandrine Bailleuxvector_entry sync_exception_sp_elx
209a6ef4393SDouglas Raillard	/*
210a6ef4393SDouglas Raillard	 * This exception will trigger if anything went wrong during a previous
211a6ef4393SDouglas Raillard	 * exception entry or exit or while handling an earlier unexpected
212a6ef4393SDouglas Raillard	 * synchronous exception. There is a high probability that SP_EL3 is
213a6ef4393SDouglas Raillard	 * corrupted.
214caa84939SJeenu Viswambharan	 */
2154d91838bSJulius Werner	b	report_unhandled_exception
216a7934d69SJeenu Viswambharan	check_vector_size sync_exception_sp_elx
2174f6ad66aSAchin Gupta
218e0ae9fabSSandrine Bailleuxvector_entry irq_sp_elx
2194d91838bSJulius Werner	b	report_unhandled_interrupt
220a7934d69SJeenu Viswambharan	check_vector_size irq_sp_elx
221a7934d69SJeenu Viswambharan
222e0ae9fabSSandrine Bailleuxvector_entry fiq_sp_elx
2234d91838bSJulius Werner	b	report_unhandled_interrupt
224a7934d69SJeenu Viswambharan	check_vector_size fiq_sp_elx
225a7934d69SJeenu Viswambharan
226e0ae9fabSSandrine Bailleuxvector_entry serror_sp_elx
2274d91838bSJulius Werner	b	report_unhandled_exception
228a7934d69SJeenu Viswambharan	check_vector_size serror_sp_elx
2294f6ad66aSAchin Gupta
230a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
23144804252SSandrine Bailleux	 * Lower EL using AArch64 : 0x400 - 0x600
232a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
2334f6ad66aSAchin Gupta	 */
234e0ae9fabSSandrine Bailleuxvector_entry sync_exception_aarch64
235a6ef4393SDouglas Raillard	/*
236a6ef4393SDouglas Raillard	 * This exception vector will be the entry point for SMCs and traps
237a6ef4393SDouglas Raillard	 * that are unhandled at lower ELs most commonly. SP_EL3 should point
238a6ef4393SDouglas Raillard	 * to a valid cpu context where the general purpose and system register
239a6ef4393SDouglas Raillard	 * state can be saved.
240caa84939SJeenu Viswambharan	 */
241caa84939SJeenu Viswambharan	handle_sync_exception
242a7934d69SJeenu Viswambharan	check_vector_size sync_exception_aarch64
2434f6ad66aSAchin Gupta
244e0ae9fabSSandrine Bailleuxvector_entry irq_aarch64
245dce74b89SAchin Gupta	handle_interrupt_exception irq_aarch64
246a7934d69SJeenu Viswambharan	check_vector_size irq_aarch64
2474f6ad66aSAchin Gupta
248e0ae9fabSSandrine Bailleuxvector_entry fiq_aarch64
249dce74b89SAchin Gupta	handle_interrupt_exception fiq_aarch64
250a7934d69SJeenu Viswambharan	check_vector_size fiq_aarch64
2514f6ad66aSAchin Gupta
252e0ae9fabSSandrine Bailleuxvector_entry serror_aarch64
253a6ef4393SDouglas Raillard	/*
254a6ef4393SDouglas Raillard	 * SError exceptions from lower ELs are not currently supported.
255a6ef4393SDouglas Raillard	 * Report their occurrence.
256a6ef4393SDouglas Raillard	 */
2574d91838bSJulius Werner	b	report_unhandled_exception
258a7934d69SJeenu Viswambharan	check_vector_size serror_aarch64
2594f6ad66aSAchin Gupta
260a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
26144804252SSandrine Bailleux	 * Lower EL using AArch32 : 0x600 - 0x800
262a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
2634f6ad66aSAchin Gupta	 */
264e0ae9fabSSandrine Bailleuxvector_entry sync_exception_aarch32
265a6ef4393SDouglas Raillard	/*
266a6ef4393SDouglas Raillard	 * This exception vector will be the entry point for SMCs and traps
267a6ef4393SDouglas Raillard	 * that are unhandled at lower ELs most commonly. SP_EL3 should point
268a6ef4393SDouglas Raillard	 * to a valid cpu context where the general purpose and system register
269a6ef4393SDouglas Raillard	 * state can be saved.
270caa84939SJeenu Viswambharan	 */
271caa84939SJeenu Viswambharan	handle_sync_exception
272a7934d69SJeenu Viswambharan	check_vector_size sync_exception_aarch32
2734f6ad66aSAchin Gupta
274e0ae9fabSSandrine Bailleuxvector_entry irq_aarch32
275dce74b89SAchin Gupta	handle_interrupt_exception irq_aarch32
276a7934d69SJeenu Viswambharan	check_vector_size irq_aarch32
2774f6ad66aSAchin Gupta
278e0ae9fabSSandrine Bailleuxvector_entry fiq_aarch32
279dce74b89SAchin Gupta	handle_interrupt_exception fiq_aarch32
280a7934d69SJeenu Viswambharan	check_vector_size fiq_aarch32
2814f6ad66aSAchin Gupta
282e0ae9fabSSandrine Bailleuxvector_entry serror_aarch32
283a6ef4393SDouglas Raillard	/*
284a6ef4393SDouglas Raillard	 * SError exceptions from lower ELs are not currently supported.
285a6ef4393SDouglas Raillard	 * Report their occurrence.
286a6ef4393SDouglas Raillard	 */
2874d91838bSJulius Werner	b	report_unhandled_exception
288a7934d69SJeenu Viswambharan	check_vector_size serror_aarch32
289a7934d69SJeenu Viswambharan
290caa84939SJeenu Viswambharan
291a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
292caa84939SJeenu Viswambharan	 * The following code handles secure monitor calls.
293a6ef4393SDouglas Raillard	 * Depending upon the execution state from where the SMC has been
294a6ef4393SDouglas Raillard	 * invoked, it frees some general purpose registers to perform the
295a6ef4393SDouglas Raillard	 * remaining tasks. They involve finding the runtime service handler
296a6ef4393SDouglas Raillard	 * that is the target of the SMC & switching to runtime stacks (SP_EL0)
297a6ef4393SDouglas Raillard	 * before calling the handler.
298caa84939SJeenu Viswambharan	 *
299a6ef4393SDouglas Raillard	 * Note that x30 has been explicitly saved and can be used here
300a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
301caa84939SJeenu Viswambharan	 */
3020a30cf54SAndrew Thoelkefunc smc_handler
303caa84939SJeenu Viswambharansmc_handler32:
304caa84939SJeenu Viswambharan	/* Check whether aarch32 issued an SMC64 */
305caa84939SJeenu Viswambharan	tbnz	x0, #FUNCID_CC_SHIFT, smc_prohibited
306caa84939SJeenu Viswambharan
307caa84939SJeenu Viswambharansmc_handler64:
308a6ef4393SDouglas Raillard	/*
309a6ef4393SDouglas Raillard	 * Populate the parameters for the SMC handler.
310a6ef4393SDouglas Raillard	 * We already have x0-x4 in place. x5 will point to a cookie (not used
311a6ef4393SDouglas Raillard	 * now). x6 will point to the context structure (SP_EL3) and x7 will
312*201ca5b6SDimitris Papastamos	 * contain flags we need to pass to the handler.
313a6ef4393SDouglas Raillard	 *
314*201ca5b6SDimitris Papastamos	 * Save x4-x29 and sp_el0.  Refer to SMCCC v1.1.
315caa84939SJeenu Viswambharan	 */
316*201ca5b6SDimitris Papastamos	save_x4_to_x29_sp_el0
317c3260f9bSSoby Mathew
318caa84939SJeenu Viswambharan	mov	x5, xzr
319caa84939SJeenu Viswambharan	mov	x6, sp
320caa84939SJeenu Viswambharan
321caa84939SJeenu Viswambharan	/* Get the unique owning entity number */
322caa84939SJeenu Viswambharan	ubfx	x16, x0, #FUNCID_OEN_SHIFT, #FUNCID_OEN_WIDTH
323caa84939SJeenu Viswambharan	ubfx	x15, x0, #FUNCID_TYPE_SHIFT, #FUNCID_TYPE_WIDTH
324caa84939SJeenu Viswambharan	orr	x16, x16, x15, lsl #FUNCID_OEN_WIDTH
325caa84939SJeenu Viswambharan
326caa84939SJeenu Viswambharan	adr	x11, (__RT_SVC_DESCS_START__ + RT_SVC_DESC_HANDLE)
327caa84939SJeenu Viswambharan
328caa84939SJeenu Viswambharan	/* Load descriptor index from array of indices */
329caa84939SJeenu Viswambharan	adr	x14, rt_svc_descs_indices
330caa84939SJeenu Viswambharan	ldrb	w15, [x14, x16]
331caa84939SJeenu Viswambharan
332a6ef4393SDouglas Raillard	/*
333a6ef4393SDouglas Raillard	 * Restore the saved C runtime stack value which will become the new
334a6ef4393SDouglas Raillard	 * SP_EL0 i.e. EL3 runtime stack. It was saved in the 'cpu_context'
335a6ef4393SDouglas Raillard	 * structure prior to the last ERET from EL3.
336caa84939SJeenu Viswambharan	 */
337caa84939SJeenu Viswambharan	ldr	x12, [x6, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
338caa84939SJeenu Viswambharan
339caa84939SJeenu Viswambharan	/*
340caa84939SJeenu Viswambharan	 * Any index greater than 127 is invalid. Check bit 7 for
341caa84939SJeenu Viswambharan	 * a valid index
342caa84939SJeenu Viswambharan	 */
343caa84939SJeenu Viswambharan	tbnz	w15, 7, smc_unknown
344caa84939SJeenu Viswambharan
345caa84939SJeenu Viswambharan	/* Switch to SP_EL0 */
346caa84939SJeenu Viswambharan	msr	spsel, #0
347caa84939SJeenu Viswambharan
348a6ef4393SDouglas Raillard	/*
349caa84939SJeenu Viswambharan	 * Get the descriptor using the index
350caa84939SJeenu Viswambharan	 * x11 = (base + off), x15 = index
351caa84939SJeenu Viswambharan	 *
352caa84939SJeenu Viswambharan	 * handler = (base + off) + (index << log2(size))
353caa84939SJeenu Viswambharan	 */
354caa84939SJeenu Viswambharan	lsl	w10, w15, #RT_SVC_SIZE_LOG2
355caa84939SJeenu Viswambharan	ldr	x15, [x11, w10, uxtw]
356caa84939SJeenu Viswambharan
357a6ef4393SDouglas Raillard	/*
358a6ef4393SDouglas Raillard	 * Save the SPSR_EL3, ELR_EL3, & SCR_EL3 in case there is a world
359a6ef4393SDouglas Raillard	 * switch during SMC handling.
360a6ef4393SDouglas Raillard	 * TODO: Revisit if all system registers can be saved later.
361caa84939SJeenu Viswambharan	 */
362caa84939SJeenu Viswambharan	mrs	x16, spsr_el3
363caa84939SJeenu Viswambharan	mrs	x17, elr_el3
364caa84939SJeenu Viswambharan	mrs	x18, scr_el3
365caa84939SJeenu Viswambharan	stp	x16, x17, [x6, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
366b51da821SAchin Gupta	str	x18, [x6, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3]
367caa84939SJeenu Viswambharan
368caa84939SJeenu Viswambharan	/* Copy SCR_EL3.NS bit to the flag to indicate caller's security */
369caa84939SJeenu Viswambharan	bfi	x7, x18, #0, #1
370caa84939SJeenu Viswambharan
371caa84939SJeenu Viswambharan	mov	sp, x12
372caa84939SJeenu Viswambharan
373a6ef4393SDouglas Raillard	/*
374a6ef4393SDouglas Raillard	 * Call the Secure Monitor Call handler and then drop directly into
375a6ef4393SDouglas Raillard	 * el3_exit() which will program any remaining architectural state
376a6ef4393SDouglas Raillard	 * prior to issuing the ERET to the desired lower EL.
377caa84939SJeenu Viswambharan	 */
378caa84939SJeenu Viswambharan#if DEBUG
379caa84939SJeenu Viswambharan	cbz	x15, rt_svc_fw_critical_error
380caa84939SJeenu Viswambharan#endif
381caa84939SJeenu Viswambharan	blr	x15
382caa84939SJeenu Viswambharan
383bbf8f6f9SYatharth Kochar	b	el3_exit
3844f6ad66aSAchin Gupta
385caa84939SJeenu Viswambharansmc_unknown:
386caa84939SJeenu Viswambharan	/*
387caa84939SJeenu Viswambharan	 * Here we restore x4-x18 regardless of where we came from. AArch32
388caa84939SJeenu Viswambharan	 * callers will find the registers contents unchanged, but AArch64
389caa84939SJeenu Viswambharan	 * callers will find the registers modified (with stale earlier NS
390caa84939SJeenu Viswambharan	 * content). Either way, we aren't leaking any secure information
391a6ef4393SDouglas Raillard	 * through them.
392caa84939SJeenu Viswambharan	 */
393a43d431bSSoby Mathew	mov	w0, #SMC_UNK
394a43d431bSSoby Mathew	b	restore_gp_registers_callee_eret
395caa84939SJeenu Viswambharan
396caa84939SJeenu Viswambharansmc_prohibited:
397c3260f9bSSoby Mathew	ldr	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
398caa84939SJeenu Viswambharan	mov	w0, #SMC_UNK
399caa84939SJeenu Viswambharan	eret
400caa84939SJeenu Viswambharan
401caa84939SJeenu Viswambharanrt_svc_fw_critical_error:
402a6ef4393SDouglas Raillard	/* Switch to SP_ELx */
403a6ef4393SDouglas Raillard	msr	spsel, #1
404a806dad5SJeenu Viswambharan	no_ret	report_unhandled_exception
4058b779620SKévin Petitendfunc smc_handler
406