xref: /rk3399_ARM-atf/bl31/aarch64/runtime_exceptions.S (revision c3260f9b82c5017ca078f090c03cd7135ee8f8c9)
14f6ad66aSAchin Gupta/*
2e83b0cadSDan Handley * Copyright (c) 2013-2014, 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>
3335e98e55SDan Handley#include <cm_macros.S>
3497043ac9SDan Handley#include <context.h>
3597043ac9SDan Handley#include <platform.h>
3697043ac9SDan Handley#include <runtime_svc.h>
374f6ad66aSAchin Gupta
384f6ad66aSAchin Gupta	.globl	runtime_exceptions
39caa84939SJeenu Viswambharan	.globl	el3_exit
40caa84939SJeenu Viswambharan	.globl	get_exception_stack
414f6ad66aSAchin Gupta
42*c3260f9bSSoby Mathew	.macro save_x18_to_x29_sp_el0
43*c3260f9bSSoby Mathew	stp	x18, x19, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X18]
44*c3260f9bSSoby Mathew	stp	x20, x21, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X20]
45*c3260f9bSSoby Mathew	stp	x22, x23, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X22]
46*c3260f9bSSoby Mathew	stp	x24, x25, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X24]
47*c3260f9bSSoby Mathew	stp	x26, x27, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X26]
48*c3260f9bSSoby Mathew	stp	x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28]
49*c3260f9bSSoby Mathew	mrs	x18, sp_el0
50*c3260f9bSSoby Mathew	str	x18, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_SP_EL0]
51*c3260f9bSSoby Mathew	.endm
52*c3260f9bSSoby Mathew
53b739f22aSAchin Gupta	.section	.vectors, "ax"; .align 11
544f6ad66aSAchin Gupta
554f6ad66aSAchin Gupta	.align	7
564f6ad66aSAchin Guptaruntime_exceptions:
574f6ad66aSAchin Gupta	/* -----------------------------------------------------
584f6ad66aSAchin Gupta	 * Current EL with _sp_el0 : 0x0 - 0x180
594f6ad66aSAchin Gupta	 * -----------------------------------------------------
604f6ad66aSAchin Gupta	 */
614f6ad66aSAchin Guptasync_exception_sp_el0:
62caa84939SJeenu Viswambharan	/* -----------------------------------------------------
63caa84939SJeenu Viswambharan	 * We don't expect any synchronous exceptions from EL3
64caa84939SJeenu Viswambharan	 * -----------------------------------------------------
65caa84939SJeenu Viswambharan	 */
66caa84939SJeenu Viswambharan	wfi
67caa84939SJeenu Viswambharan	b	sync_exception_sp_el0
68a7934d69SJeenu Viswambharan	check_vector_size sync_exception_sp_el0
694f6ad66aSAchin Gupta
704f6ad66aSAchin Gupta	.align	7
71caa84939SJeenu Viswambharan	/* -----------------------------------------------------
72caa84939SJeenu Viswambharan	 * EL3 code is non-reentrant. Any asynchronous exception
73caa84939SJeenu Viswambharan	 * is a serious error. Loop infinitely.
74caa84939SJeenu Viswambharan	 * -----------------------------------------------------
75caa84939SJeenu Viswambharan	 */
764f6ad66aSAchin Guptairq_sp_el0:
77caa84939SJeenu Viswambharan	handle_async_exception IRQ_SP_EL0
78caa84939SJeenu Viswambharan	b	irq_sp_el0
79a7934d69SJeenu Viswambharan	check_vector_size irq_sp_el0
804f6ad66aSAchin Gupta
814f6ad66aSAchin Gupta	.align	7
824f6ad66aSAchin Guptafiq_sp_el0:
83caa84939SJeenu Viswambharan	handle_async_exception FIQ_SP_EL0
84caa84939SJeenu Viswambharan	b	fiq_sp_el0
85a7934d69SJeenu Viswambharan	check_vector_size fiq_sp_el0
864f6ad66aSAchin Gupta
874f6ad66aSAchin Gupta	.align	7
884f6ad66aSAchin Guptaserror_sp_el0:
89caa84939SJeenu Viswambharan	handle_async_exception SERROR_SP_EL0
90caa84939SJeenu Viswambharan	b	serror_sp_el0
91a7934d69SJeenu Viswambharan	check_vector_size serror_sp_el0
924f6ad66aSAchin Gupta
934f6ad66aSAchin Gupta	/* -----------------------------------------------------
944f6ad66aSAchin Gupta	 * Current EL with SPx: 0x200 - 0x380
954f6ad66aSAchin Gupta	 * -----------------------------------------------------
964f6ad66aSAchin Gupta	 */
974f6ad66aSAchin Gupta	.align	7
984f6ad66aSAchin Guptasync_exception_sp_elx:
99caa84939SJeenu Viswambharan	/* -----------------------------------------------------
100caa84939SJeenu Viswambharan	 * This exception will trigger if anything went wrong
101caa84939SJeenu Viswambharan	 * during a previous exception entry or exit or while
102caa84939SJeenu Viswambharan	 * handling an earlier unexpected synchronous exception.
103caa84939SJeenu Viswambharan	 * In any case we cannot rely on SP_EL3. Switching to a
104caa84939SJeenu Viswambharan	 * known safe area of memory will corrupt at least a
105caa84939SJeenu Viswambharan	 * single register. It is best to enter wfi in loop as
106caa84939SJeenu Viswambharan	 * that will preserve the system state for analysis
107caa84939SJeenu Viswambharan	 * through a debugger later.
108caa84939SJeenu Viswambharan	 * -----------------------------------------------------
109caa84939SJeenu Viswambharan	 */
110caa84939SJeenu Viswambharan	wfi
111caa84939SJeenu Viswambharan	b	sync_exception_sp_elx
112a7934d69SJeenu Viswambharan	check_vector_size sync_exception_sp_elx
1134f6ad66aSAchin Gupta
114caa84939SJeenu Viswambharan	/* -----------------------------------------------------
115caa84939SJeenu Viswambharan	 * As mentioned in the previous comment, all bets are
116caa84939SJeenu Viswambharan	 * off if SP_EL3 cannot be relied upon. Report their
117caa84939SJeenu Viswambharan	 * occurrence.
118caa84939SJeenu Viswambharan	 * -----------------------------------------------------
119caa84939SJeenu Viswambharan	 */
1204f6ad66aSAchin Gupta	.align	7
1214f6ad66aSAchin Guptairq_sp_elx:
122caa84939SJeenu Viswambharan	b	irq_sp_elx
123a7934d69SJeenu Viswambharan	check_vector_size irq_sp_elx
124a7934d69SJeenu Viswambharan
1254f6ad66aSAchin Gupta	.align	7
1264f6ad66aSAchin Guptafiq_sp_elx:
127caa84939SJeenu Viswambharan	b	fiq_sp_elx
128a7934d69SJeenu Viswambharan	check_vector_size fiq_sp_elx
129a7934d69SJeenu Viswambharan
1304f6ad66aSAchin Gupta	.align	7
1314f6ad66aSAchin Guptaserror_sp_elx:
132caa84939SJeenu Viswambharan	b	serror_sp_elx
133a7934d69SJeenu Viswambharan	check_vector_size serror_sp_elx
1344f6ad66aSAchin Gupta
1354f6ad66aSAchin Gupta	/* -----------------------------------------------------
1364f6ad66aSAchin Gupta	 * Lower EL using AArch64 : 0x400 - 0x580
1374f6ad66aSAchin Gupta	 * -----------------------------------------------------
1384f6ad66aSAchin Gupta	 */
1394f6ad66aSAchin Gupta	.align	7
1404f6ad66aSAchin Guptasync_exception_aarch64:
141caa84939SJeenu Viswambharan	/* -----------------------------------------------------
142caa84939SJeenu Viswambharan	 * This exception vector will be the entry point for
143caa84939SJeenu Viswambharan	 * SMCs and traps that are unhandled at lower ELs most
144caa84939SJeenu Viswambharan	 * commonly. SP_EL3 should point to a valid cpu context
145caa84939SJeenu Viswambharan	 * where the general purpose and system register state
146caa84939SJeenu Viswambharan	 * can be saved.
147caa84939SJeenu Viswambharan	 * -----------------------------------------------------
148caa84939SJeenu Viswambharan	 */
149caa84939SJeenu Viswambharan	handle_sync_exception
150a7934d69SJeenu Viswambharan	check_vector_size sync_exception_aarch64
1514f6ad66aSAchin Gupta
1524f6ad66aSAchin Gupta	.align	7
153caa84939SJeenu Viswambharan	/* -----------------------------------------------------
154caa84939SJeenu Viswambharan	 * Asynchronous exceptions from lower ELs are not
155caa84939SJeenu Viswambharan	 * currently supported. Report their occurrence.
156caa84939SJeenu Viswambharan	 * -----------------------------------------------------
157caa84939SJeenu Viswambharan	 */
1584f6ad66aSAchin Guptairq_aarch64:
159caa84939SJeenu Viswambharan	handle_async_exception IRQ_AARCH64
160caa84939SJeenu Viswambharan	b	irq_aarch64
161a7934d69SJeenu Viswambharan	check_vector_size irq_aarch64
1624f6ad66aSAchin Gupta
1634f6ad66aSAchin Gupta	.align	7
1644f6ad66aSAchin Guptafiq_aarch64:
165caa84939SJeenu Viswambharan	handle_async_exception FIQ_AARCH64
166caa84939SJeenu Viswambharan	b	fiq_aarch64
167a7934d69SJeenu Viswambharan	check_vector_size fiq_aarch64
1684f6ad66aSAchin Gupta
1694f6ad66aSAchin Gupta	.align	7
1704f6ad66aSAchin Guptaserror_aarch64:
171caa84939SJeenu Viswambharan	handle_async_exception SERROR_AARCH64
172caa84939SJeenu Viswambharan	b	serror_aarch64
173a7934d69SJeenu Viswambharan	check_vector_size serror_aarch64
1744f6ad66aSAchin Gupta
1754f6ad66aSAchin Gupta	/* -----------------------------------------------------
1764f6ad66aSAchin Gupta	 * Lower EL using AArch32 : 0x600 - 0x780
1774f6ad66aSAchin Gupta	 * -----------------------------------------------------
1784f6ad66aSAchin Gupta	 */
1794f6ad66aSAchin Gupta	.align	7
1804f6ad66aSAchin Guptasync_exception_aarch32:
181caa84939SJeenu Viswambharan	/* -----------------------------------------------------
182caa84939SJeenu Viswambharan	 * This exception vector will be the entry point for
183caa84939SJeenu Viswambharan	 * SMCs and traps that are unhandled at lower ELs most
184caa84939SJeenu Viswambharan	 * commonly. SP_EL3 should point to a valid cpu context
185caa84939SJeenu Viswambharan	 * where the general purpose and system register state
186caa84939SJeenu Viswambharan	 * can be saved.
187caa84939SJeenu Viswambharan	 * -----------------------------------------------------
188caa84939SJeenu Viswambharan	 */
189caa84939SJeenu Viswambharan	handle_sync_exception
190a7934d69SJeenu Viswambharan	check_vector_size sync_exception_aarch32
1914f6ad66aSAchin Gupta
1924f6ad66aSAchin Gupta	.align	7
193caa84939SJeenu Viswambharan	/* -----------------------------------------------------
194caa84939SJeenu Viswambharan	 * Asynchronous exceptions from lower ELs are not
195caa84939SJeenu Viswambharan	 * currently supported. Report their occurrence.
196caa84939SJeenu Viswambharan	 * -----------------------------------------------------
197caa84939SJeenu Viswambharan	 */
1984f6ad66aSAchin Guptairq_aarch32:
199caa84939SJeenu Viswambharan	handle_async_exception IRQ_AARCH32
200caa84939SJeenu Viswambharan	b	irq_aarch32
201a7934d69SJeenu Viswambharan	check_vector_size irq_aarch32
2024f6ad66aSAchin Gupta
2034f6ad66aSAchin Gupta	.align	7
2044f6ad66aSAchin Guptafiq_aarch32:
205caa84939SJeenu Viswambharan	handle_async_exception FIQ_AARCH32
206caa84939SJeenu Viswambharan	b	fiq_aarch32
207a7934d69SJeenu Viswambharan	check_vector_size fiq_aarch32
2084f6ad66aSAchin Gupta
2094f6ad66aSAchin Gupta	.align	7
2104f6ad66aSAchin Guptaserror_aarch32:
211caa84939SJeenu Viswambharan	handle_async_exception SERROR_AARCH32
212caa84939SJeenu Viswambharan	b	serror_aarch32
213a7934d69SJeenu Viswambharan	check_vector_size serror_aarch32
214a7934d69SJeenu Viswambharan
215caa84939SJeenu Viswambharan	.align	7
216caa84939SJeenu Viswambharan
217caa84939SJeenu Viswambharan	/* -----------------------------------------------------
218caa84939SJeenu Viswambharan	 * The following code handles secure monitor calls.
219caa84939SJeenu Viswambharan	 * Depending upon the execution state from where the SMC
220caa84939SJeenu Viswambharan	 * has been invoked, it frees some general purpose
221caa84939SJeenu Viswambharan	 * registers to perform the remaining tasks. They
222caa84939SJeenu Viswambharan	 * involve finding the runtime service handler that is
223caa84939SJeenu Viswambharan	 * the target of the SMC & switching to runtime stacks
224caa84939SJeenu Viswambharan	 * (SP_EL0) before calling the handler.
225caa84939SJeenu Viswambharan	 *
226caa84939SJeenu Viswambharan	 * Note that x30 has been explicitly saved and can be
227caa84939SJeenu Viswambharan	 * used here
228caa84939SJeenu Viswambharan	 * -----------------------------------------------------
229caa84939SJeenu Viswambharan	 */
2300a30cf54SAndrew Thoelkefunc smc_handler
231caa84939SJeenu Viswambharansmc_handler32:
232caa84939SJeenu Viswambharan	/* Check whether aarch32 issued an SMC64 */
233caa84939SJeenu Viswambharan	tbnz	x0, #FUNCID_CC_SHIFT, smc_prohibited
234caa84939SJeenu Viswambharan
235caa84939SJeenu Viswambharan	/* -----------------------------------------------------
236caa84939SJeenu Viswambharan	 * Since we're are coming from aarch32, x8-x18 need to
237caa84939SJeenu Viswambharan	 * be saved as per SMC32 calling convention. If a lower
238caa84939SJeenu Viswambharan	 * EL in aarch64 is making an SMC32 call then it must
239caa84939SJeenu Viswambharan	 * have saved x8-x17 already therein.
240caa84939SJeenu Viswambharan	 * -----------------------------------------------------
241caa84939SJeenu Viswambharan	 */
242caa84939SJeenu Viswambharan	stp	x8, x9, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X8]
243caa84939SJeenu Viswambharan	stp	x10, x11, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X10]
244caa84939SJeenu Viswambharan	stp	x12, x13, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X12]
245caa84939SJeenu Viswambharan	stp	x14, x15, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X14]
246caa84939SJeenu Viswambharan	stp	x16, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X16]
247caa84939SJeenu Viswambharan
248caa84939SJeenu Viswambharan	/* x4-x7, x18, sp_el0 are saved below */
249caa84939SJeenu Viswambharan
250caa84939SJeenu Viswambharansmc_handler64:
251caa84939SJeenu Viswambharan	/* -----------------------------------------------------
252caa84939SJeenu Viswambharan	 * Populate the parameters for the SMC handler. We
253caa84939SJeenu Viswambharan	 * already have x0-x4 in place. x5 will point to a
254caa84939SJeenu Viswambharan	 * cookie (not used now). x6 will point to the context
255caa84939SJeenu Viswambharan	 * structure (SP_EL3) and x7 will contain flags we need
256caa84939SJeenu Viswambharan	 * to pass to the handler Hence save x5-x7. Note that x4
257caa84939SJeenu Viswambharan	 * only needs to be preserved for AArch32 callers but we
258caa84939SJeenu Viswambharan	 * do it for AArch64 callers as well for convenience
259caa84939SJeenu Viswambharan	 * -----------------------------------------------------
260caa84939SJeenu Viswambharan	 */
261caa84939SJeenu Viswambharan	stp	x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4]
262caa84939SJeenu Viswambharan	stp	x6, x7, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X6]
263caa84939SJeenu Viswambharan
264*c3260f9bSSoby Mathew	/* Save rest of the gpregs and sp_el0*/
265*c3260f9bSSoby Mathew	save_x18_to_x29_sp_el0
266*c3260f9bSSoby Mathew
267caa84939SJeenu Viswambharan	mov	x5, xzr
268caa84939SJeenu Viswambharan	mov	x6, sp
269caa84939SJeenu Viswambharan
270caa84939SJeenu Viswambharan	/* Get the unique owning entity number */
271caa84939SJeenu Viswambharan	ubfx	x16, x0, #FUNCID_OEN_SHIFT, #FUNCID_OEN_WIDTH
272caa84939SJeenu Viswambharan	ubfx	x15, x0, #FUNCID_TYPE_SHIFT, #FUNCID_TYPE_WIDTH
273caa84939SJeenu Viswambharan	orr	x16, x16, x15, lsl #FUNCID_OEN_WIDTH
274caa84939SJeenu Viswambharan
275caa84939SJeenu Viswambharan	adr	x11, (__RT_SVC_DESCS_START__ + RT_SVC_DESC_HANDLE)
276caa84939SJeenu Viswambharan
277caa84939SJeenu Viswambharan	/* Load descriptor index from array of indices */
278caa84939SJeenu Viswambharan	adr	x14, rt_svc_descs_indices
279caa84939SJeenu Viswambharan	ldrb	w15, [x14, x16]
280caa84939SJeenu Viswambharan
281caa84939SJeenu Viswambharan	/* -----------------------------------------------------
282caa84939SJeenu Viswambharan	 * Restore the saved C runtime stack value which will
283caa84939SJeenu Viswambharan	 * become the new SP_EL0 i.e. EL3 runtime stack. It was
284caa84939SJeenu Viswambharan	 * saved in the 'cpu_context' structure prior to the last
285caa84939SJeenu Viswambharan	 * ERET from EL3.
286caa84939SJeenu Viswambharan	 * -----------------------------------------------------
287caa84939SJeenu Viswambharan	 */
288caa84939SJeenu Viswambharan	ldr	x12, [x6, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
289caa84939SJeenu Viswambharan
290caa84939SJeenu Viswambharan	/*
291caa84939SJeenu Viswambharan	 * Any index greater than 127 is invalid. Check bit 7 for
292caa84939SJeenu Viswambharan	 * a valid index
293caa84939SJeenu Viswambharan	 */
294caa84939SJeenu Viswambharan	tbnz	w15, 7, smc_unknown
295caa84939SJeenu Viswambharan
296caa84939SJeenu Viswambharan	/* Switch to SP_EL0 */
297caa84939SJeenu Viswambharan	msr	spsel, #0
298caa84939SJeenu Viswambharan
299caa84939SJeenu Viswambharan	/* -----------------------------------------------------
300caa84939SJeenu Viswambharan	 * Get the descriptor using the index
301caa84939SJeenu Viswambharan	 * x11 = (base + off), x15 = index
302caa84939SJeenu Viswambharan	 *
303caa84939SJeenu Viswambharan	 * handler = (base + off) + (index << log2(size))
304caa84939SJeenu Viswambharan	 * -----------------------------------------------------
305caa84939SJeenu Viswambharan	 */
306caa84939SJeenu Viswambharan	lsl	w10, w15, #RT_SVC_SIZE_LOG2
307caa84939SJeenu Viswambharan	ldr	x15, [x11, w10, uxtw]
308caa84939SJeenu Viswambharan
309caa84939SJeenu Viswambharan	/* -----------------------------------------------------
310caa84939SJeenu Viswambharan	 * Save the SPSR_EL3, ELR_EL3, & SCR_EL3 in case there
311caa84939SJeenu Viswambharan	 * is a world switch during SMC handling.
312caa84939SJeenu Viswambharan	 * TODO: Revisit if all system registers can be saved
313caa84939SJeenu Viswambharan	 * later.
314caa84939SJeenu Viswambharan	 * -----------------------------------------------------
315caa84939SJeenu Viswambharan	 */
316caa84939SJeenu Viswambharan	mrs	x16, spsr_el3
317caa84939SJeenu Viswambharan	mrs	x17, elr_el3
318caa84939SJeenu Viswambharan	mrs	x18, scr_el3
319caa84939SJeenu Viswambharan	stp	x16, x17, [x6, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
320caa84939SJeenu Viswambharan	stp	x18, xzr, [x6, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3]
321caa84939SJeenu Viswambharan
322caa84939SJeenu Viswambharan	/* Copy SCR_EL3.NS bit to the flag to indicate caller's security */
323caa84939SJeenu Viswambharan	bfi	x7, x18, #0, #1
324caa84939SJeenu Viswambharan
325caa84939SJeenu Viswambharan	mov	sp, x12
326caa84939SJeenu Viswambharan
327caa84939SJeenu Viswambharan	/* -----------------------------------------------------
328caa84939SJeenu Viswambharan	 * Call the Secure Monitor Call handler and then drop
329caa84939SJeenu Viswambharan	 * directly into el3_exit() which will program any
330caa84939SJeenu Viswambharan	 * remaining architectural state prior to issuing the
331caa84939SJeenu Viswambharan	 * ERET to the desired lower EL.
332caa84939SJeenu Viswambharan	 * -----------------------------------------------------
333caa84939SJeenu Viswambharan	 */
334caa84939SJeenu Viswambharan#if DEBUG
335caa84939SJeenu Viswambharan	cbz	x15, rt_svc_fw_critical_error
336caa84939SJeenu Viswambharan#endif
337caa84939SJeenu Viswambharan	blr	x15
338caa84939SJeenu Viswambharan
339caa84939SJeenu Viswambharan	/* -----------------------------------------------------
340caa84939SJeenu Viswambharan	 * This routine assumes that the SP_EL3 is pointing to
341caa84939SJeenu Viswambharan	 * a valid context structure from where the gp regs and
342caa84939SJeenu Viswambharan	 * other special registers can be retrieved.
3430a30cf54SAndrew Thoelke	 *
3440a30cf54SAndrew Thoelke	 * Keep it in the same section as smc_handler as this
3450a30cf54SAndrew Thoelke	 * function uses a fall-through to el3_exit
346caa84939SJeenu Viswambharan	 * -----------------------------------------------------
347caa84939SJeenu Viswambharan	 */
348caa84939SJeenu Viswambharanel3_exit: ; .type el3_exit, %function
349caa84939SJeenu Viswambharan	/* -----------------------------------------------------
350caa84939SJeenu Viswambharan	 * Save the current SP_EL0 i.e. the EL3 runtime stack
351caa84939SJeenu Viswambharan	 * which will be used for handling the next SMC. Then
352caa84939SJeenu Viswambharan	 * switch to SP_EL3
353caa84939SJeenu Viswambharan	 * -----------------------------------------------------
354caa84939SJeenu Viswambharan	 */
355caa84939SJeenu Viswambharan	mov	x17, sp
356caa84939SJeenu Viswambharan	msr	spsel, #1
357caa84939SJeenu Viswambharan	str	x17, [sp, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
358caa84939SJeenu Viswambharan
359caa84939SJeenu Viswambharan	/* -----------------------------------------------------
360caa84939SJeenu Viswambharan	 * Restore SPSR_EL3, ELR_EL3 and SCR_EL3 prior to ERET
361caa84939SJeenu Viswambharan	 * -----------------------------------------------------
362caa84939SJeenu Viswambharan	 */
363caa84939SJeenu Viswambharan	ldp	x18, xzr, [sp, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3]
364caa84939SJeenu Viswambharan	ldp	x16, x17, [sp, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
365caa84939SJeenu Viswambharan	msr	scr_el3, x18
366caa84939SJeenu Viswambharan	msr	spsr_el3, x16
367caa84939SJeenu Viswambharan	msr	elr_el3, x17
368caa84939SJeenu Viswambharan
369caa84939SJeenu Viswambharan	/* Restore saved general purpose registers and return */
370*c3260f9bSSoby Mathew	bl	restore_gp_registers
371*c3260f9bSSoby Mathew	ldr	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
3724f6ad66aSAchin Gupta	eret
3734f6ad66aSAchin Gupta
374caa84939SJeenu Viswambharansmc_unknown:
375caa84939SJeenu Viswambharan	/*
376caa84939SJeenu Viswambharan	 * Here we restore x4-x18 regardless of where we came from. AArch32
377caa84939SJeenu Viswambharan	 * callers will find the registers contents unchanged, but AArch64
378caa84939SJeenu Viswambharan	 * callers will find the registers modified (with stale earlier NS
379caa84939SJeenu Viswambharan	 * content). Either way, we aren't leaking any secure information
380caa84939SJeenu Viswambharan	 * through them
381caa84939SJeenu Viswambharan	 */
382*c3260f9bSSoby Mathew	bl	restore_gp_registers_callee
383caa84939SJeenu Viswambharan
384caa84939SJeenu Viswambharansmc_prohibited:
385*c3260f9bSSoby Mathew	ldr	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
386caa84939SJeenu Viswambharan	mov	w0, #SMC_UNK
387caa84939SJeenu Viswambharan	eret
388caa84939SJeenu Viswambharan
389caa84939SJeenu Viswambharanrt_svc_fw_critical_error:
390caa84939SJeenu Viswambharan	b	rt_svc_fw_critical_error
391caa84939SJeenu Viswambharan
392caa84939SJeenu Viswambharan	/* -----------------------------------------------------
393caa84939SJeenu Viswambharan	 * The following functions are used to saved and restore
394*c3260f9bSSoby Mathew	 * all the general pupose registers. Ideally we would
395*c3260f9bSSoby Mathew	 * only save and restore the callee saved registers when
396*c3260f9bSSoby Mathew	 * a world switch occurs but that type of implementation
397*c3260f9bSSoby Mathew	 * is more complex. So currently we will always save and
398*c3260f9bSSoby Mathew	 * restore these registers on entry and exit of EL3.
399caa84939SJeenu Viswambharan	 * These are not macros to ensure their invocation fits
400caa84939SJeenu Viswambharan	 * within the 32 instructions per exception vector.
401caa84939SJeenu Viswambharan	 * -----------------------------------------------------
402caa84939SJeenu Viswambharan	 */
403*c3260f9bSSoby Mathewfunc save_gp_registers
404caa84939SJeenu Viswambharan	stp	x0, x1, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X0]
405caa84939SJeenu Viswambharan	stp	x2, x3, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X2]
406caa84939SJeenu Viswambharan	stp	x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4]
407caa84939SJeenu Viswambharan	stp	x6, x7, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X6]
408caa84939SJeenu Viswambharan	stp	x8, x9, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X8]
409caa84939SJeenu Viswambharan	stp	x10, x11, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X10]
410caa84939SJeenu Viswambharan	stp	x12, x13, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X12]
411caa84939SJeenu Viswambharan	stp	x14, x15, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X14]
412caa84939SJeenu Viswambharan	stp	x16, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X16]
413*c3260f9bSSoby Mathew	save_x18_to_x29_sp_el0
414caa84939SJeenu Viswambharan	ret
415caa84939SJeenu Viswambharan
416*c3260f9bSSoby Mathewfunc restore_gp_registers
417caa84939SJeenu Viswambharan	ldp	x0, x1, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X0]
418caa84939SJeenu Viswambharan	ldp	x2, x3, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X2]
419caa84939SJeenu Viswambharan
420*c3260f9bSSoby Mathewrestore_gp_registers_callee:
421*c3260f9bSSoby Mathew	ldr	x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_SP_EL0]
422caa84939SJeenu Viswambharan
423caa84939SJeenu Viswambharan	ldp	x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4]
424caa84939SJeenu Viswambharan	ldp	x6, x7, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X6]
425caa84939SJeenu Viswambharan	ldp	x8, x9, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X8]
426caa84939SJeenu Viswambharan	ldp	x10, x11, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X10]
427caa84939SJeenu Viswambharan	ldp	x12, x13, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X12]
428caa84939SJeenu Viswambharan	ldp	x14, x15, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X14]
429caa84939SJeenu Viswambharan	msr	sp_el0, x17
430caa84939SJeenu Viswambharan	ldp	x16, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X16]
431*c3260f9bSSoby Mathew	ldp	x18, x19, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X18]
432*c3260f9bSSoby Mathew	ldp	x20, x21, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X20]
433*c3260f9bSSoby Mathew	ldp	x22, x23, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X22]
434*c3260f9bSSoby Mathew	ldp	x24, x25, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X24]
435*c3260f9bSSoby Mathew	ldp	x26, x27, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X26]
436*c3260f9bSSoby Mathew	ldp	x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28]
437caa84939SJeenu Viswambharan	ret
438caa84939SJeenu Viswambharan
439caa84939SJeenu Viswambharan	/* -----------------------------------------------------
440caa84939SJeenu Viswambharan	 * 256 bytes of exception stack for each cpu
441caa84939SJeenu Viswambharan	 * -----------------------------------------------------
442caa84939SJeenu Viswambharan	 */
443caa84939SJeenu Viswambharan#if DEBUG
444caa84939SJeenu Viswambharan#define PCPU_EXCEPTION_STACK_SIZE	0x300
445caa84939SJeenu Viswambharan#else
446caa84939SJeenu Viswambharan#define PCPU_EXCEPTION_STACK_SIZE	0x100
447caa84939SJeenu Viswambharan#endif
448caa84939SJeenu Viswambharan	/* -----------------------------------------------------
449caa84939SJeenu Viswambharan	 * void get_exception_stack (uint64_t mpidr) : This
450caa84939SJeenu Viswambharan	 * function is used to allocate a small stack for
451caa84939SJeenu Viswambharan	 * reporting unhandled exceptions
452caa84939SJeenu Viswambharan	 * -----------------------------------------------------
453caa84939SJeenu Viswambharan	 */
4540a30cf54SAndrew Thoelkefunc get_exception_stack
455caa84939SJeenu Viswambharan	mov	x10, x30 // lr
4562bf28e62SAndrew Thoelke	get_mp_stack pcpu_exception_stack, PCPU_EXCEPTION_STACK_SIZE
457caa84939SJeenu Viswambharan	ret	x10
458caa84939SJeenu Viswambharan
459caa84939SJeenu Viswambharan	/* -----------------------------------------------------
460caa84939SJeenu Viswambharan	 * Per-cpu exception stacks in normal memory.
461caa84939SJeenu Viswambharan	 * -----------------------------------------------------
462caa84939SJeenu Viswambharan	 */
4632bf28e62SAndrew Thoelkedeclare_stack pcpu_exception_stack, tzfw_normal_stacks, \
4642bf28e62SAndrew Thoelke		PCPU_EXCEPTION_STACK_SIZE, PLATFORM_CORE_COUNT
465