xref: /rk3399_ARM-atf/bl31/aarch64/runtime_exceptions.S (revision a6ef4393b6f0a346d6629ae01bb34c3d44ae5a08)
14f6ad66aSAchin Gupta/*
2e0ae9fabSSandrine Bailleux * Copyright (c) 2013-2016, ARM Limited and Contributors. All rights reserved.
34f6ad66aSAchin Gupta *
44f6ad66aSAchin Gupta * Redistribution and use in source and binary forms, with or without
54f6ad66aSAchin Gupta * modification, are permitted provided that the following conditions are met:
64f6ad66aSAchin Gupta *
74f6ad66aSAchin Gupta * Redistributions of source code must retain the above copyright notice, this
84f6ad66aSAchin Gupta * list of conditions and the following disclaimer.
94f6ad66aSAchin Gupta *
104f6ad66aSAchin Gupta * Redistributions in binary form must reproduce the above copyright notice,
114f6ad66aSAchin Gupta * this list of conditions and the following disclaimer in the documentation
124f6ad66aSAchin Gupta * and/or other materials provided with the distribution.
134f6ad66aSAchin Gupta *
144f6ad66aSAchin Gupta * Neither the name of ARM nor the names of its contributors may be used
154f6ad66aSAchin Gupta * to endorse or promote products derived from this software without specific
164f6ad66aSAchin Gupta * prior written permission.
174f6ad66aSAchin Gupta *
184f6ad66aSAchin Gupta * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
194f6ad66aSAchin Gupta * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
204f6ad66aSAchin Gupta * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
214f6ad66aSAchin Gupta * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
224f6ad66aSAchin Gupta * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
234f6ad66aSAchin Gupta * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
244f6ad66aSAchin Gupta * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
254f6ad66aSAchin Gupta * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
264f6ad66aSAchin Gupta * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
274f6ad66aSAchin Gupta * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
284f6ad66aSAchin Gupta * POSSIBILITY OF SUCH DAMAGE.
294f6ad66aSAchin Gupta */
304f6ad66aSAchin Gupta
314f6ad66aSAchin Gupta#include <arch.h>
3235e98e55SDan Handley#include <asm_macros.S>
3397043ac9SDan Handley#include <context.h>
34872be88aSdp-arm#include <cpu_data.h>
35dce74b89SAchin Gupta#include <interrupt_mgmt.h>
365f0cdb05SDan Handley#include <platform_def.h>
3797043ac9SDan Handley#include <runtime_svc.h>
384f6ad66aSAchin Gupta
394f6ad66aSAchin Gupta	.globl	runtime_exceptions
404f6ad66aSAchin Gupta
41*a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
42*a6ef4393SDouglas Raillard	 * This macro handles Synchronous exceptions.
43*a6ef4393SDouglas Raillard	 * Only SMC exceptions are supported.
44*a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
45dce74b89SAchin Gupta	 */
46dce74b89SAchin Gupta	.macro	handle_sync_exception
470c8d4fefSAchin Gupta	/* Enable the SError interrupt */
480c8d4fefSAchin Gupta	msr	daifclr, #DAIF_ABT_BIT
490c8d4fefSAchin Gupta
50dce74b89SAchin Gupta	str	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
51872be88aSdp-arm
52872be88aSdp-arm#if ENABLE_RUNTIME_INSTRUMENTATION
53872be88aSdp-arm	/*
54*a6ef4393SDouglas Raillard	 * Read the timestamp value and store it in per-cpu data. The value
55*a6ef4393SDouglas Raillard	 * will be extracted from per-cpu data by the C level SMC handler and
56*a6ef4393SDouglas Raillard	 * saved to the PMF timestamp region.
57872be88aSdp-arm	 */
58872be88aSdp-arm	mrs	x30, cntpct_el0
59872be88aSdp-arm	str	x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29]
60872be88aSdp-arm	mrs	x29, tpidr_el3
61872be88aSdp-arm	str	x30, [x29, #CPU_DATA_PMF_TS0_OFFSET]
62872be88aSdp-arm	ldr	x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29]
63872be88aSdp-arm#endif
64872be88aSdp-arm
65dce74b89SAchin Gupta	mrs	x30, esr_el3
66dce74b89SAchin Gupta	ubfx	x30, x30, #ESR_EC_SHIFT, #ESR_EC_LENGTH
67dce74b89SAchin Gupta
68*a6ef4393SDouglas Raillard	/* Handle SMC exceptions separately from other synchronous exceptions */
69dce74b89SAchin Gupta	cmp	x30, #EC_AARCH32_SMC
70dce74b89SAchin Gupta	b.eq	smc_handler32
71dce74b89SAchin Gupta
72dce74b89SAchin Gupta	cmp	x30, #EC_AARCH64_SMC
73dce74b89SAchin Gupta	b.eq	smc_handler64
74dce74b89SAchin Gupta
75*a6ef4393SDouglas Raillard	/* Other kinds of synchronous exceptions are not handled */
76626ed510SSoby Mathew	bl	report_unhandled_exception
77dce74b89SAchin Gupta	.endm
78dce74b89SAchin Gupta
79dce74b89SAchin Gupta
80*a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
81*a6ef4393SDouglas Raillard	 * This macro handles FIQ or IRQ interrupts i.e. EL3, S-EL1 and NS
82*a6ef4393SDouglas Raillard	 * interrupts.
83*a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
84dce74b89SAchin Gupta	 */
85dce74b89SAchin Gupta	.macro	handle_interrupt_exception label
860c8d4fefSAchin Gupta	/* Enable the SError interrupt */
870c8d4fefSAchin Gupta	msr	daifclr, #DAIF_ABT_BIT
880c8d4fefSAchin Gupta
89dce74b89SAchin Gupta	str	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
90dce74b89SAchin Gupta	bl	save_gp_registers
91dce74b89SAchin Gupta
92*a6ef4393SDouglas Raillard	/* Save the EL3 system registers needed to return from this exception */
935717aae1SAchin Gupta	mrs	x0, spsr_el3
945717aae1SAchin Gupta	mrs	x1, elr_el3
955717aae1SAchin Gupta	stp	x0, x1, [sp, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
965717aae1SAchin Gupta
97dce74b89SAchin Gupta	/* Switch to the runtime stack i.e. SP_EL0 */
98dce74b89SAchin Gupta	ldr	x2, [sp, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
99dce74b89SAchin Gupta	mov	x20, sp
100dce74b89SAchin Gupta	msr	spsel, #0
101dce74b89SAchin Gupta	mov	sp, x2
102dce74b89SAchin Gupta
103dce74b89SAchin Gupta	/*
104*a6ef4393SDouglas Raillard	 * Find out whether this is a valid interrupt type.
105*a6ef4393SDouglas Raillard	 * If the interrupt controller reports a spurious interrupt then return
106*a6ef4393SDouglas Raillard	 * to where we came from.
107dce74b89SAchin Gupta	 */
1089865ac15SDan Handley	bl	plat_ic_get_pending_interrupt_type
109dce74b89SAchin Gupta	cmp	x0, #INTR_TYPE_INVAL
110dce74b89SAchin Gupta	b.eq	interrupt_exit_\label
111dce74b89SAchin Gupta
112dce74b89SAchin Gupta	/*
113*a6ef4393SDouglas Raillard	 * Get the registered handler for this interrupt type.
114*a6ef4393SDouglas Raillard	 * A NULL return value could be 'cause of the following conditions:
1155717aae1SAchin Gupta	 *
116*a6ef4393SDouglas Raillard	 * a. An interrupt of a type was routed correctly but a handler for its
117*a6ef4393SDouglas Raillard	 *    type was not registered.
1185717aae1SAchin Gupta	 *
119*a6ef4393SDouglas Raillard	 * b. An interrupt of a type was not routed correctly so a handler for
120*a6ef4393SDouglas Raillard	 *    its type was not registered.
1215717aae1SAchin Gupta	 *
122*a6ef4393SDouglas Raillard	 * c. An interrupt of a type was routed correctly to EL3, but was
123*a6ef4393SDouglas Raillard	 *    deasserted before its pending state could be read. Another
124*a6ef4393SDouglas Raillard	 *    interrupt of a different type pended at the same time and its
125*a6ef4393SDouglas Raillard	 *    type was reported as pending instead. However, a handler for this
126*a6ef4393SDouglas Raillard	 *    type was not registered.
1275717aae1SAchin Gupta	 *
128*a6ef4393SDouglas Raillard	 * a. and b. can only happen due to a programming error. The
129*a6ef4393SDouglas Raillard	 * occurrence of c. could be beyond the control of Trusted Firmware.
130*a6ef4393SDouglas Raillard	 * It makes sense to return from this exception instead of reporting an
131*a6ef4393SDouglas Raillard	 * error.
132dce74b89SAchin Gupta	 */
133dce74b89SAchin Gupta	bl	get_interrupt_type_handler
1345717aae1SAchin Gupta	cbz	x0, interrupt_exit_\label
135dce74b89SAchin Gupta	mov	x21, x0
136dce74b89SAchin Gupta
137dce74b89SAchin Gupta	mov	x0, #INTR_ID_UNAVAILABLE
138dce74b89SAchin Gupta
139dce74b89SAchin Gupta	/* Set the current security state in the 'flags' parameter */
140dce74b89SAchin Gupta	mrs	x2, scr_el3
141dce74b89SAchin Gupta	ubfx	x1, x2, #0, #1
142dce74b89SAchin Gupta
143dce74b89SAchin Gupta	/* Restore the reference to the 'handle' i.e. SP_EL3 */
144dce74b89SAchin Gupta	mov	x2, x20
145dce74b89SAchin Gupta
146b460b8bfSSoby Mathew	/* x3 will point to a cookie (not used now) */
147b460b8bfSSoby Mathew	mov	x3, xzr
148b460b8bfSSoby Mathew
149dce74b89SAchin Gupta	/* Call the interrupt type handler */
150dce74b89SAchin Gupta	blr	x21
151dce74b89SAchin Gupta
152dce74b89SAchin Guptainterrupt_exit_\label:
153dce74b89SAchin Gupta	/* Return from exception, possibly in a different security state */
154dce74b89SAchin Gupta	b	el3_exit
155dce74b89SAchin Gupta
156dce74b89SAchin Gupta	.endm
157dce74b89SAchin Gupta
158dce74b89SAchin Gupta
159c3260f9bSSoby Mathew	.macro save_x18_to_x29_sp_el0
160c3260f9bSSoby Mathew	stp	x18, x19, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X18]
161c3260f9bSSoby Mathew	stp	x20, x21, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X20]
162c3260f9bSSoby Mathew	stp	x22, x23, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X22]
163c3260f9bSSoby Mathew	stp	x24, x25, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X24]
164c3260f9bSSoby Mathew	stp	x26, x27, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X26]
165c3260f9bSSoby Mathew	stp	x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28]
166c3260f9bSSoby Mathew	mrs	x18, sp_el0
167c3260f9bSSoby Mathew	str	x18, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_SP_EL0]
168c3260f9bSSoby Mathew	.endm
169c3260f9bSSoby Mathew
170e0ae9fabSSandrine Bailleux
171e0ae9fabSSandrine Bailleuxvector_base runtime_exceptions
172e0ae9fabSSandrine Bailleux
173*a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
174*a6ef4393SDouglas Raillard	 * Current EL with SP_EL0 : 0x0 - 0x200
175*a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
1764f6ad66aSAchin Gupta	 */
177e0ae9fabSSandrine Bailleuxvector_entry sync_exception_sp_el0
178*a6ef4393SDouglas Raillard	/* We don't expect any synchronous exceptions from EL3 */
179626ed510SSoby Mathew	bl	report_unhandled_exception
180a7934d69SJeenu Viswambharan	check_vector_size sync_exception_sp_el0
1814f6ad66aSAchin Gupta
182e0ae9fabSSandrine Bailleuxvector_entry irq_sp_el0
183*a6ef4393SDouglas Raillard	/*
184*a6ef4393SDouglas Raillard	 * EL3 code is non-reentrant. Any asynchronous exception is a serious
185*a6ef4393SDouglas Raillard	 * error. Loop infinitely.
186*a6ef4393SDouglas Raillard	 */
187626ed510SSoby Mathew	bl	report_unhandled_interrupt
188a7934d69SJeenu Viswambharan	check_vector_size irq_sp_el0
1894f6ad66aSAchin Gupta
190e0ae9fabSSandrine Bailleux
191e0ae9fabSSandrine Bailleuxvector_entry fiq_sp_el0
192626ed510SSoby Mathew	bl	report_unhandled_interrupt
193a7934d69SJeenu Viswambharan	check_vector_size fiq_sp_el0
1944f6ad66aSAchin Gupta
195e0ae9fabSSandrine Bailleux
196e0ae9fabSSandrine Bailleuxvector_entry serror_sp_el0
197626ed510SSoby Mathew	bl	report_unhandled_exception
198a7934d69SJeenu Viswambharan	check_vector_size serror_sp_el0
1994f6ad66aSAchin Gupta
200*a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
201*a6ef4393SDouglas Raillard	 * Current EL with SP_ELx: 0x200 - 0x400
202*a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
2034f6ad66aSAchin Gupta	 */
204e0ae9fabSSandrine Bailleuxvector_entry sync_exception_sp_elx
205*a6ef4393SDouglas Raillard	/*
206*a6ef4393SDouglas Raillard	 * This exception will trigger if anything went wrong during a previous
207*a6ef4393SDouglas Raillard	 * exception entry or exit or while handling an earlier unexpected
208*a6ef4393SDouglas Raillard	 * synchronous exception. There is a high probability that SP_EL3 is
209*a6ef4393SDouglas Raillard	 * corrupted.
210caa84939SJeenu Viswambharan	 */
211626ed510SSoby Mathew	bl	report_unhandled_exception
212a7934d69SJeenu Viswambharan	check_vector_size sync_exception_sp_elx
2134f6ad66aSAchin Gupta
214e0ae9fabSSandrine Bailleuxvector_entry irq_sp_elx
215626ed510SSoby Mathew	bl	report_unhandled_interrupt
216a7934d69SJeenu Viswambharan	check_vector_size irq_sp_elx
217a7934d69SJeenu Viswambharan
218e0ae9fabSSandrine Bailleuxvector_entry fiq_sp_elx
219626ed510SSoby Mathew	bl	report_unhandled_interrupt
220a7934d69SJeenu Viswambharan	check_vector_size fiq_sp_elx
221a7934d69SJeenu Viswambharan
222e0ae9fabSSandrine Bailleuxvector_entry serror_sp_elx
223626ed510SSoby Mathew	bl	report_unhandled_exception
224a7934d69SJeenu Viswambharan	check_vector_size serror_sp_elx
2254f6ad66aSAchin Gupta
226*a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
22744804252SSandrine Bailleux	 * Lower EL using AArch64 : 0x400 - 0x600
228*a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
2294f6ad66aSAchin Gupta	 */
230e0ae9fabSSandrine Bailleuxvector_entry sync_exception_aarch64
231*a6ef4393SDouglas Raillard	/*
232*a6ef4393SDouglas Raillard	 * This exception vector will be the entry point for SMCs and traps
233*a6ef4393SDouglas Raillard	 * that are unhandled at lower ELs most commonly. SP_EL3 should point
234*a6ef4393SDouglas Raillard	 * to a valid cpu context where the general purpose and system register
235*a6ef4393SDouglas Raillard	 * state can be saved.
236caa84939SJeenu Viswambharan	 */
237caa84939SJeenu Viswambharan	handle_sync_exception
238a7934d69SJeenu Viswambharan	check_vector_size sync_exception_aarch64
2394f6ad66aSAchin Gupta
240e0ae9fabSSandrine Bailleuxvector_entry irq_aarch64
241dce74b89SAchin Gupta	handle_interrupt_exception irq_aarch64
242a7934d69SJeenu Viswambharan	check_vector_size irq_aarch64
2434f6ad66aSAchin Gupta
244e0ae9fabSSandrine Bailleuxvector_entry fiq_aarch64
245dce74b89SAchin Gupta	handle_interrupt_exception fiq_aarch64
246a7934d69SJeenu Viswambharan	check_vector_size fiq_aarch64
2474f6ad66aSAchin Gupta
248e0ae9fabSSandrine Bailleuxvector_entry serror_aarch64
249*a6ef4393SDouglas Raillard	/*
250*a6ef4393SDouglas Raillard	 * SError exceptions from lower ELs are not currently supported.
251*a6ef4393SDouglas Raillard	 * Report their occurrence.
252*a6ef4393SDouglas Raillard	 */
253626ed510SSoby Mathew	bl	report_unhandled_exception
254a7934d69SJeenu Viswambharan	check_vector_size serror_aarch64
2554f6ad66aSAchin Gupta
256*a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
25744804252SSandrine Bailleux	 * Lower EL using AArch32 : 0x600 - 0x800
258*a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
2594f6ad66aSAchin Gupta	 */
260e0ae9fabSSandrine Bailleuxvector_entry sync_exception_aarch32
261*a6ef4393SDouglas Raillard	/*
262*a6ef4393SDouglas Raillard	 * This exception vector will be the entry point for SMCs and traps
263*a6ef4393SDouglas Raillard	 * that are unhandled at lower ELs most commonly. SP_EL3 should point
264*a6ef4393SDouglas Raillard	 * to a valid cpu context where the general purpose and system register
265*a6ef4393SDouglas Raillard	 * state can be saved.
266caa84939SJeenu Viswambharan	 */
267caa84939SJeenu Viswambharan	handle_sync_exception
268a7934d69SJeenu Viswambharan	check_vector_size sync_exception_aarch32
2694f6ad66aSAchin Gupta
270e0ae9fabSSandrine Bailleuxvector_entry irq_aarch32
271dce74b89SAchin Gupta	handle_interrupt_exception irq_aarch32
272a7934d69SJeenu Viswambharan	check_vector_size irq_aarch32
2734f6ad66aSAchin Gupta
274e0ae9fabSSandrine Bailleuxvector_entry fiq_aarch32
275dce74b89SAchin Gupta	handle_interrupt_exception fiq_aarch32
276a7934d69SJeenu Viswambharan	check_vector_size fiq_aarch32
2774f6ad66aSAchin Gupta
278e0ae9fabSSandrine Bailleuxvector_entry serror_aarch32
279*a6ef4393SDouglas Raillard	/*
280*a6ef4393SDouglas Raillard	 * SError exceptions from lower ELs are not currently supported.
281*a6ef4393SDouglas Raillard	 * Report their occurrence.
282*a6ef4393SDouglas Raillard	 */
283626ed510SSoby Mathew	bl	report_unhandled_exception
284a7934d69SJeenu Viswambharan	check_vector_size serror_aarch32
285a7934d69SJeenu Viswambharan
286caa84939SJeenu Viswambharan
287*a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
288caa84939SJeenu Viswambharan	 * The following code handles secure monitor calls.
289*a6ef4393SDouglas Raillard	 * Depending upon the execution state from where the SMC has been
290*a6ef4393SDouglas Raillard	 * invoked, it frees some general purpose registers to perform the
291*a6ef4393SDouglas Raillard	 * remaining tasks. They involve finding the runtime service handler
292*a6ef4393SDouglas Raillard	 * that is the target of the SMC & switching to runtime stacks (SP_EL0)
293*a6ef4393SDouglas Raillard	 * before calling the handler.
294caa84939SJeenu Viswambharan	 *
295*a6ef4393SDouglas Raillard	 * Note that x30 has been explicitly saved and can be used here
296*a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
297caa84939SJeenu Viswambharan	 */
2980a30cf54SAndrew Thoelkefunc smc_handler
299caa84939SJeenu Viswambharansmc_handler32:
300caa84939SJeenu Viswambharan	/* Check whether aarch32 issued an SMC64 */
301caa84939SJeenu Viswambharan	tbnz	x0, #FUNCID_CC_SHIFT, smc_prohibited
302caa84939SJeenu Viswambharan
303*a6ef4393SDouglas Raillard	/*
304*a6ef4393SDouglas Raillard	 * Since we're are coming from aarch32, x8-x18 need to be saved as per
305*a6ef4393SDouglas Raillard	 * SMC32 calling convention. If a lower EL in aarch64 is making an
306*a6ef4393SDouglas Raillard	 * SMC32 call then it must have saved x8-x17 already therein.
307caa84939SJeenu Viswambharan	 */
308caa84939SJeenu Viswambharan	stp	x8, x9, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X8]
309caa84939SJeenu Viswambharan	stp	x10, x11, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X10]
310caa84939SJeenu Viswambharan	stp	x12, x13, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X12]
311caa84939SJeenu Viswambharan	stp	x14, x15, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X14]
312caa84939SJeenu Viswambharan	stp	x16, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X16]
313caa84939SJeenu Viswambharan
314caa84939SJeenu Viswambharan	/* x4-x7, x18, sp_el0 are saved below */
315caa84939SJeenu Viswambharan
316caa84939SJeenu Viswambharansmc_handler64:
317*a6ef4393SDouglas Raillard	/*
318*a6ef4393SDouglas Raillard	 * Populate the parameters for the SMC handler.
319*a6ef4393SDouglas Raillard	 * We already have x0-x4 in place. x5 will point to a cookie (not used
320*a6ef4393SDouglas Raillard	 * now). x6 will point to the context structure (SP_EL3) and x7 will
321*a6ef4393SDouglas Raillard	 * contain flags we need to pass to the handler Hence save x5-x7.
322*a6ef4393SDouglas Raillard	 *
323*a6ef4393SDouglas Raillard	 * Note: x4 only needs to be preserved for AArch32 callers but we do it
324*a6ef4393SDouglas Raillard	 *       for AArch64 callers as well for convenience
325caa84939SJeenu Viswambharan	 */
326caa84939SJeenu Viswambharan	stp	x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4]
327caa84939SJeenu Viswambharan	stp	x6, x7, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X6]
328caa84939SJeenu Viswambharan
329c3260f9bSSoby Mathew	/* Save rest of the gpregs and sp_el0*/
330c3260f9bSSoby Mathew	save_x18_to_x29_sp_el0
331c3260f9bSSoby Mathew
332caa84939SJeenu Viswambharan	mov	x5, xzr
333caa84939SJeenu Viswambharan	mov	x6, sp
334caa84939SJeenu Viswambharan
335caa84939SJeenu Viswambharan	/* Get the unique owning entity number */
336caa84939SJeenu Viswambharan	ubfx	x16, x0, #FUNCID_OEN_SHIFT, #FUNCID_OEN_WIDTH
337caa84939SJeenu Viswambharan	ubfx	x15, x0, #FUNCID_TYPE_SHIFT, #FUNCID_TYPE_WIDTH
338caa84939SJeenu Viswambharan	orr	x16, x16, x15, lsl #FUNCID_OEN_WIDTH
339caa84939SJeenu Viswambharan
340caa84939SJeenu Viswambharan	adr	x11, (__RT_SVC_DESCS_START__ + RT_SVC_DESC_HANDLE)
341caa84939SJeenu Viswambharan
342caa84939SJeenu Viswambharan	/* Load descriptor index from array of indices */
343caa84939SJeenu Viswambharan	adr	x14, rt_svc_descs_indices
344caa84939SJeenu Viswambharan	ldrb	w15, [x14, x16]
345caa84939SJeenu Viswambharan
346*a6ef4393SDouglas Raillard	/*
347*a6ef4393SDouglas Raillard	 * Restore the saved C runtime stack value which will become the new
348*a6ef4393SDouglas Raillard	 * SP_EL0 i.e. EL3 runtime stack. It was saved in the 'cpu_context'
349*a6ef4393SDouglas Raillard	 * structure prior to the last ERET from EL3.
350caa84939SJeenu Viswambharan	 */
351caa84939SJeenu Viswambharan	ldr	x12, [x6, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
352caa84939SJeenu Viswambharan
353caa84939SJeenu Viswambharan	/*
354caa84939SJeenu Viswambharan	 * Any index greater than 127 is invalid. Check bit 7 for
355caa84939SJeenu Viswambharan	 * a valid index
356caa84939SJeenu Viswambharan	 */
357caa84939SJeenu Viswambharan	tbnz	w15, 7, smc_unknown
358caa84939SJeenu Viswambharan
359caa84939SJeenu Viswambharan	/* Switch to SP_EL0 */
360caa84939SJeenu Viswambharan	msr	spsel, #0
361caa84939SJeenu Viswambharan
362*a6ef4393SDouglas Raillard	/*
363caa84939SJeenu Viswambharan	 * Get the descriptor using the index
364caa84939SJeenu Viswambharan	 * x11 = (base + off), x15 = index
365caa84939SJeenu Viswambharan	 *
366caa84939SJeenu Viswambharan	 * handler = (base + off) + (index << log2(size))
367caa84939SJeenu Viswambharan	 */
368caa84939SJeenu Viswambharan	lsl	w10, w15, #RT_SVC_SIZE_LOG2
369caa84939SJeenu Viswambharan	ldr	x15, [x11, w10, uxtw]
370caa84939SJeenu Viswambharan
371*a6ef4393SDouglas Raillard	/*
372*a6ef4393SDouglas Raillard	 * Save the SPSR_EL3, ELR_EL3, & SCR_EL3 in case there is a world
373*a6ef4393SDouglas Raillard	 * switch during SMC handling.
374*a6ef4393SDouglas Raillard	 * TODO: Revisit if all system registers can be saved later.
375caa84939SJeenu Viswambharan	 */
376caa84939SJeenu Viswambharan	mrs	x16, spsr_el3
377caa84939SJeenu Viswambharan	mrs	x17, elr_el3
378caa84939SJeenu Viswambharan	mrs	x18, scr_el3
379caa84939SJeenu Viswambharan	stp	x16, x17, [x6, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
380b51da821SAchin Gupta	str	x18, [x6, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3]
381caa84939SJeenu Viswambharan
382caa84939SJeenu Viswambharan	/* Copy SCR_EL3.NS bit to the flag to indicate caller's security */
383caa84939SJeenu Viswambharan	bfi	x7, x18, #0, #1
384caa84939SJeenu Viswambharan
385caa84939SJeenu Viswambharan	mov	sp, x12
386caa84939SJeenu Viswambharan
387*a6ef4393SDouglas Raillard	/*
388*a6ef4393SDouglas Raillard	 * Call the Secure Monitor Call handler and then drop directly into
389*a6ef4393SDouglas Raillard	 * el3_exit() which will program any remaining architectural state
390*a6ef4393SDouglas Raillard	 * prior to issuing the ERET to the desired lower EL.
391caa84939SJeenu Viswambharan	 */
392caa84939SJeenu Viswambharan#if DEBUG
393caa84939SJeenu Viswambharan	cbz	x15, rt_svc_fw_critical_error
394caa84939SJeenu Viswambharan#endif
395caa84939SJeenu Viswambharan	blr	x15
396caa84939SJeenu Viswambharan
397bbf8f6f9SYatharth Kochar	b	el3_exit
3984f6ad66aSAchin Gupta
399caa84939SJeenu Viswambharansmc_unknown:
400caa84939SJeenu Viswambharan	/*
401caa84939SJeenu Viswambharan	 * Here we restore x4-x18 regardless of where we came from. AArch32
402caa84939SJeenu Viswambharan	 * callers will find the registers contents unchanged, but AArch64
403caa84939SJeenu Viswambharan	 * callers will find the registers modified (with stale earlier NS
404caa84939SJeenu Viswambharan	 * content). Either way, we aren't leaking any secure information
405*a6ef4393SDouglas Raillard	 * through them.
406caa84939SJeenu Viswambharan	 */
407a43d431bSSoby Mathew	mov	w0, #SMC_UNK
408a43d431bSSoby Mathew	b	restore_gp_registers_callee_eret
409caa84939SJeenu Viswambharan
410caa84939SJeenu Viswambharansmc_prohibited:
411c3260f9bSSoby Mathew	ldr	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
412caa84939SJeenu Viswambharan	mov	w0, #SMC_UNK
413caa84939SJeenu Viswambharan	eret
414caa84939SJeenu Viswambharan
415caa84939SJeenu Viswambharanrt_svc_fw_critical_error:
416*a6ef4393SDouglas Raillard	/* Switch to SP_ELx */
417*a6ef4393SDouglas Raillard	msr	spsel, #1
418626ed510SSoby Mathew	bl	report_unhandled_exception
4198b779620SKévin Petitendfunc smc_handler
420