14f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta/*
2e83b0cadc67882c1ba7f430d16dab80c9b3a0228Dan Handley * Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved.
34f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta *
44f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * Redistribution and use in source and binary forms, with or without
54f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * modification, are permitted provided that the following conditions are met:
64f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta *
74f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * Redistributions of source code must retain the above copyright notice, this
84f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * list of conditions and the following disclaimer.
94f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta *
104f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * Redistributions in binary form must reproduce the above copyright notice,
114f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * this list of conditions and the following disclaimer in the documentation
124f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * and/or other materials provided with the distribution.
134f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta *
144f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * Neither the name of ARM nor the names of its contributors may be used
154f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * to endorse or promote products derived from this software without specific
164f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * prior written permission.
174f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta *
184f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
194f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
204f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
214f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
224f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
234f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
244f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
254f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
264f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
274f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
284f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * POSSIBILITY OF SUCH DAMAGE.
294f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta */
304f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta
314f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta#include <arch.h>
3235e98e5588d09145f7d0d4d98624f6b75321a187Dan Handley#include <asm_macros.S>
3397043ac98e13a726dbf8b3b41654dca759e3da2cDan Handley#include <context.h>
34dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta#include <interrupt_mgmt.h>
355f0cdb059d7d5c3a8a834074a7f236b85d014ddeDan Handley#include <platform_def.h>
3697043ac98e13a726dbf8b3b41654dca759e3da2cDan Handley#include <runtime_svc.h>
374f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta
384f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta	.globl	runtime_exceptions
39caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	.globl	el3_exit
404f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta
41dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	/* -----------------------------------------------------
4244804252881c11668e0af9b7803a0667922c93ebSandrine Bailleux	 * Handle SMC exceptions separately from other sync.
43dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	 * exceptions.
44dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	 * -----------------------------------------------------
45dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	 */
46dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	.macro	handle_sync_exception
470c8d4fef28768233f1f46b4d085f904293dffd2cAchin Gupta	/* Enable the SError interrupt */
480c8d4fef28768233f1f46b4d085f904293dffd2cAchin Gupta	msr	daifclr, #DAIF_ABT_BIT
490c8d4fef28768233f1f46b4d085f904293dffd2cAchin Gupta
50dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	str	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
51dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	mrs	x30, esr_el3
52dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	ubfx	x30, x30, #ESR_EC_SHIFT, #ESR_EC_LENGTH
53dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta
54dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	cmp	x30, #EC_AARCH32_SMC
55dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	b.eq	smc_handler32
56dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta
57dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	cmp	x30, #EC_AARCH64_SMC
58dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	b.eq	smc_handler64
59dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta
60dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	/* -----------------------------------------------------
61dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	 * The following code handles any synchronous exception
62dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	 * that is not an SMC.
63dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	 * -----------------------------------------------------
64dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	 */
65dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta
66626ed510f179c905a699f4663ee933c10892b4c3Soby Mathew	bl	report_unhandled_exception
67dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	.endm
68dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta
69dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta
70dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	/* -----------------------------------------------------
71dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	 * This macro handles FIQ or IRQ interrupts i.e. EL3,
72dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	 * S-EL1 and NS interrupts.
73dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	 * -----------------------------------------------------
74dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	 */
75dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	.macro	handle_interrupt_exception label
760c8d4fef28768233f1f46b4d085f904293dffd2cAchin Gupta	/* Enable the SError interrupt */
770c8d4fef28768233f1f46b4d085f904293dffd2cAchin Gupta	msr	daifclr, #DAIF_ABT_BIT
780c8d4fef28768233f1f46b4d085f904293dffd2cAchin Gupta
79dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	str	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
80dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	bl	save_gp_registers
81dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta
82dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	/* Switch to the runtime stack i.e. SP_EL0 */
83dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	ldr	x2, [sp, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
84dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	mov	x20, sp
85dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	msr	spsel, #0
86dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	mov	sp, x2
87dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta
88dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	/*
89dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	 * Find out whether this is a valid interrupt type. If the
90dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	 * interrupt controller reports a spurious interrupt then
91dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	 * return to where we came from.
92dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	 */
939865ac15765f260069047c0e7c56623eb1a70b9aDan Handley	bl	plat_ic_get_pending_interrupt_type
94dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	cmp	x0, #INTR_TYPE_INVAL
95dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	b.eq	interrupt_exit_\label
96dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta
97dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	/*
98dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	 * Get the registered handler for this interrupt type. A
99dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	 * NULL return value implies that an interrupt was generated
100dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	 * for which there is no handler registered or the interrupt
101dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	 * was routed incorrectly. This is a problem of the framework
102dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	 * so report it as an error.
103dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	 */
104dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	bl	get_interrupt_type_handler
105dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	cbz	x0, interrupt_error_\label
106dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	mov	x21, x0
107dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta
108dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	mov	x0, #INTR_ID_UNAVAILABLE
109dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta#if IMF_READ_INTERRUPT_ID
110dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	/*
111dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	 * Read the id of the highest priority pending interrupt. If
112dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	 * no interrupt is asserted then return to where we came from.
113dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	 */
114a37810853630098559d93f1b9ecdc222e63ef9cfSoby Mathew	mov	x19,  #INTR_ID_UNAVAILABLE
1159865ac15765f260069047c0e7c56623eb1a70b9aDan Handley	bl	plat_ic_get_pending_interrupt_id
116a37810853630098559d93f1b9ecdc222e63ef9cfSoby Mathew	cmp	x19, x0
117dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	b.eq	interrupt_exit_\label
118dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta#endif
119dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta
120dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	/*
121dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	 * Save the EL3 system registers needed to return from
122dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	 * this exception.
123dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	 */
124dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	mrs	x3, spsr_el3
125dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	mrs	x4, elr_el3
126dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	stp	x3, x4, [x20, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
127dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta
128dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	/* Set the current security state in the 'flags' parameter */
129dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	mrs	x2, scr_el3
130dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	ubfx	x1, x2, #0, #1
131dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta
132dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	/* Restore the reference to the 'handle' i.e. SP_EL3 */
133dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	mov	x2, x20
134dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta
135b460b8bf23633195535006b29e14c615f888fa24Soby Mathew	/*  x3 will point to a cookie (not used now) */
136b460b8bf23633195535006b29e14c615f888fa24Soby Mathew	mov	x3, xzr
137b460b8bf23633195535006b29e14c615f888fa24Soby Mathew
138dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	/* Call the interrupt type handler */
139dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	blr	x21
140dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta
141dce74b891e0e6020d0a18384e32f280133631d9bAchin Guptainterrupt_exit_\label:
142dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	/* Return from exception, possibly in a different security state */
143dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	b	el3_exit
144dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta
145dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	/*
146dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	 * This label signifies a problem with the interrupt management
147dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	 * framework where it is not safe to go back to the instruction
148dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	 * where the interrupt was generated.
149dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	 */
150dce74b891e0e6020d0a18384e32f280133631d9bAchin Guptainterrupt_error_\label:
151626ed510f179c905a699f4663ee933c10892b4c3Soby Mathew	bl	report_unhandled_interrupt
152dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	.endm
153dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta
154dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta
155c3260f9b82c5017ca078f090c03cd7135ee8f8c9Soby Mathew	.macro save_x18_to_x29_sp_el0
156c3260f9b82c5017ca078f090c03cd7135ee8f8c9Soby Mathew	stp	x18, x19, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X18]
157c3260f9b82c5017ca078f090c03cd7135ee8f8c9Soby Mathew	stp	x20, x21, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X20]
158c3260f9b82c5017ca078f090c03cd7135ee8f8c9Soby Mathew	stp	x22, x23, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X22]
159c3260f9b82c5017ca078f090c03cd7135ee8f8c9Soby Mathew	stp	x24, x25, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X24]
160c3260f9b82c5017ca078f090c03cd7135ee8f8c9Soby Mathew	stp	x26, x27, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X26]
161c3260f9b82c5017ca078f090c03cd7135ee8f8c9Soby Mathew	stp	x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28]
162c3260f9b82c5017ca078f090c03cd7135ee8f8c9Soby Mathew	mrs	x18, sp_el0
163c3260f9b82c5017ca078f090c03cd7135ee8f8c9Soby Mathew	str	x18, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_SP_EL0]
164c3260f9b82c5017ca078f090c03cd7135ee8f8c9Soby Mathew	.endm
165c3260f9b82c5017ca078f090c03cd7135ee8f8c9Soby Mathew
166b739f22a99c96d5a295f083125505b5b5ec2f8b6Achin Gupta	.section	.vectors, "ax"; .align 11
1674f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta	.align	7
1684f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Guptaruntime_exceptions:
1694f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta	/* -----------------------------------------------------
17044804252881c11668e0af9b7803a0667922c93ebSandrine Bailleux	 * Current EL with _sp_el0 : 0x0 - 0x200
1714f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta	 * -----------------------------------------------------
1724f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta	 */
1734f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Guptasync_exception_sp_el0:
174caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	/* -----------------------------------------------------
175caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * We don't expect any synchronous exceptions from EL3
176caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * -----------------------------------------------------
177caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 */
178626ed510f179c905a699f4663ee933c10892b4c3Soby Mathew	bl	report_unhandled_exception
179a7934d69508872919787742c7680b68807c24361Jeenu Viswambharan	check_vector_size sync_exception_sp_el0
1804f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta
1814f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta	.align	7
182caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	/* -----------------------------------------------------
183caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * EL3 code is non-reentrant. Any asynchronous exception
184caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * is a serious error. Loop infinitely.
185caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * -----------------------------------------------------
186caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 */
1874f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Guptairq_sp_el0:
188626ed510f179c905a699f4663ee933c10892b4c3Soby Mathew	bl	report_unhandled_interrupt
189a7934d69508872919787742c7680b68807c24361Jeenu Viswambharan	check_vector_size irq_sp_el0
1904f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta
1914f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta	.align	7
1924f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Guptafiq_sp_el0:
193626ed510f179c905a699f4663ee933c10892b4c3Soby Mathew	bl	report_unhandled_interrupt
194a7934d69508872919787742c7680b68807c24361Jeenu Viswambharan	check_vector_size fiq_sp_el0
1954f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta
1964f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta	.align	7
1974f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Guptaserror_sp_el0:
198626ed510f179c905a699f4663ee933c10892b4c3Soby Mathew	bl	report_unhandled_exception
199a7934d69508872919787742c7680b68807c24361Jeenu Viswambharan	check_vector_size serror_sp_el0
2004f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta
2014f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta	/* -----------------------------------------------------
20244804252881c11668e0af9b7803a0667922c93ebSandrine Bailleux	 * Current EL with SPx: 0x200 - 0x400
2034f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta	 * -----------------------------------------------------
2044f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta	 */
2054f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta	.align	7
2064f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Guptasync_exception_sp_elx:
207caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	/* -----------------------------------------------------
208caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * This exception will trigger if anything went wrong
209caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * during a previous exception entry or exit or while
210caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * handling an earlier unexpected synchronous exception.
211a43d431b80541ea436b71f967c5749babf978c7aSoby Mathew	 * There is a high probability that SP_EL3 is corrupted.
212caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * -----------------------------------------------------
213caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 */
214626ed510f179c905a699f4663ee933c10892b4c3Soby Mathew	bl	report_unhandled_exception
215a7934d69508872919787742c7680b68807c24361Jeenu Viswambharan	check_vector_size sync_exception_sp_elx
2164f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta
2174f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta	.align	7
2184f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Guptairq_sp_elx:
219626ed510f179c905a699f4663ee933c10892b4c3Soby Mathew	bl	report_unhandled_interrupt
220a7934d69508872919787742c7680b68807c24361Jeenu Viswambharan	check_vector_size irq_sp_elx
221a7934d69508872919787742c7680b68807c24361Jeenu Viswambharan
2224f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta	.align	7
2234f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Guptafiq_sp_elx:
224626ed510f179c905a699f4663ee933c10892b4c3Soby Mathew	bl	report_unhandled_interrupt
225a7934d69508872919787742c7680b68807c24361Jeenu Viswambharan	check_vector_size fiq_sp_elx
226a7934d69508872919787742c7680b68807c24361Jeenu Viswambharan
2274f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta	.align	7
2284f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Guptaserror_sp_elx:
229626ed510f179c905a699f4663ee933c10892b4c3Soby Mathew	bl	report_unhandled_exception
230a7934d69508872919787742c7680b68807c24361Jeenu Viswambharan	check_vector_size serror_sp_elx
2314f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta
2324f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta	/* -----------------------------------------------------
23344804252881c11668e0af9b7803a0667922c93ebSandrine Bailleux	 * Lower EL using AArch64 : 0x400 - 0x600
2344f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta	 * -----------------------------------------------------
2354f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta	 */
2364f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta	.align	7
2374f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Guptasync_exception_aarch64:
238caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	/* -----------------------------------------------------
239caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * This exception vector will be the entry point for
240caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * SMCs and traps that are unhandled at lower ELs most
241caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * commonly. SP_EL3 should point to a valid cpu context
242caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * where the general purpose and system register state
243caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * can be saved.
244caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * -----------------------------------------------------
245caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 */
246caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	handle_sync_exception
247a7934d69508872919787742c7680b68807c24361Jeenu Viswambharan	check_vector_size sync_exception_aarch64
2484f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta
2494f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta	.align	7
250caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	/* -----------------------------------------------------
251caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * Asynchronous exceptions from lower ELs are not
252caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * currently supported. Report their occurrence.
253caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * -----------------------------------------------------
254caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 */
2554f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Guptairq_aarch64:
256dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	handle_interrupt_exception irq_aarch64
257a7934d69508872919787742c7680b68807c24361Jeenu Viswambharan	check_vector_size irq_aarch64
2584f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta
2594f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta	.align	7
2604f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Guptafiq_aarch64:
261dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	handle_interrupt_exception fiq_aarch64
262a7934d69508872919787742c7680b68807c24361Jeenu Viswambharan	check_vector_size fiq_aarch64
2634f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta
2644f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta	.align	7
2654f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Guptaserror_aarch64:
266626ed510f179c905a699f4663ee933c10892b4c3Soby Mathew	bl	report_unhandled_exception
267a7934d69508872919787742c7680b68807c24361Jeenu Viswambharan	check_vector_size serror_aarch64
2684f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta
2694f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta	/* -----------------------------------------------------
27044804252881c11668e0af9b7803a0667922c93ebSandrine Bailleux	 * Lower EL using AArch32 : 0x600 - 0x800
2714f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta	 * -----------------------------------------------------
2724f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta	 */
2734f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta	.align	7
2744f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Guptasync_exception_aarch32:
275caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	/* -----------------------------------------------------
276caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * This exception vector will be the entry point for
277caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * SMCs and traps that are unhandled at lower ELs most
278caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * commonly. SP_EL3 should point to a valid cpu context
279caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * where the general purpose and system register state
280caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * can be saved.
281caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * -----------------------------------------------------
282caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 */
283caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	handle_sync_exception
284a7934d69508872919787742c7680b68807c24361Jeenu Viswambharan	check_vector_size sync_exception_aarch32
2854f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta
2864f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta	.align	7
287caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	/* -----------------------------------------------------
288caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * Asynchronous exceptions from lower ELs are not
289caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * currently supported. Report their occurrence.
290caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * -----------------------------------------------------
291caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 */
2924f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Guptairq_aarch32:
293dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	handle_interrupt_exception irq_aarch32
294a7934d69508872919787742c7680b68807c24361Jeenu Viswambharan	check_vector_size irq_aarch32
2954f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta
2964f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta	.align	7
2974f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Guptafiq_aarch32:
298dce74b891e0e6020d0a18384e32f280133631d9bAchin Gupta	handle_interrupt_exception fiq_aarch32
299a7934d69508872919787742c7680b68807c24361Jeenu Viswambharan	check_vector_size fiq_aarch32
3004f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta
3014f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta	.align	7
3024f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Guptaserror_aarch32:
303626ed510f179c905a699f4663ee933c10892b4c3Soby Mathew	bl	report_unhandled_exception
304a7934d69508872919787742c7680b68807c24361Jeenu Viswambharan	check_vector_size serror_aarch32
305a7934d69508872919787742c7680b68807c24361Jeenu Viswambharan
306caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	.align	7
307caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan
308caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	/* -----------------------------------------------------
309caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * The following code handles secure monitor calls.
310caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * Depending upon the execution state from where the SMC
311caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * has been invoked, it frees some general purpose
312caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * registers to perform the remaining tasks. They
313caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * involve finding the runtime service handler that is
314caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * the target of the SMC & switching to runtime stacks
315caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * (SP_EL0) before calling the handler.
316caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 *
317caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * Note that x30 has been explicitly saved and can be
318caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * used here
319caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * -----------------------------------------------------
320caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 */
3210a30cf54af7bb1f77b405062b1d5b44e809d0290Andrew Thoelkefunc smc_handler
322caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharansmc_handler32:
323caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	/* Check whether aarch32 issued an SMC64 */
324caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	tbnz	x0, #FUNCID_CC_SHIFT, smc_prohibited
325caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan
326caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	/* -----------------------------------------------------
327caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * Since we're are coming from aarch32, x8-x18 need to
328caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * be saved as per SMC32 calling convention. If a lower
329caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * EL in aarch64 is making an SMC32 call then it must
330caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * have saved x8-x17 already therein.
331caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * -----------------------------------------------------
332caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 */
333caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	stp	x8, x9, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X8]
334caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	stp	x10, x11, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X10]
335caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	stp	x12, x13, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X12]
336caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	stp	x14, x15, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X14]
337caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	stp	x16, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X16]
338caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan
339caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	/* x4-x7, x18, sp_el0 are saved below */
340caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan
341caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharansmc_handler64:
342caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	/* -----------------------------------------------------
343caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * Populate the parameters for the SMC handler. We
344caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * already have x0-x4 in place. x5 will point to a
345caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * cookie (not used now). x6 will point to the context
346caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * structure (SP_EL3) and x7 will contain flags we need
347caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * to pass to the handler Hence save x5-x7. Note that x4
348caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * only needs to be preserved for AArch32 callers but we
349caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * do it for AArch64 callers as well for convenience
350caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * -----------------------------------------------------
351caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 */
352caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	stp	x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4]
353caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	stp	x6, x7, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X6]
354caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan
355c3260f9b82c5017ca078f090c03cd7135ee8f8c9Soby Mathew	/* Save rest of the gpregs and sp_el0*/
356c3260f9b82c5017ca078f090c03cd7135ee8f8c9Soby Mathew	save_x18_to_x29_sp_el0
357c3260f9b82c5017ca078f090c03cd7135ee8f8c9Soby Mathew
358caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	mov	x5, xzr
359caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	mov	x6, sp
360caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan
361caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	/* Get the unique owning entity number */
362caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	ubfx	x16, x0, #FUNCID_OEN_SHIFT, #FUNCID_OEN_WIDTH
363caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	ubfx	x15, x0, #FUNCID_TYPE_SHIFT, #FUNCID_TYPE_WIDTH
364caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	orr	x16, x16, x15, lsl #FUNCID_OEN_WIDTH
365caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan
366caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	adr	x11, (__RT_SVC_DESCS_START__ + RT_SVC_DESC_HANDLE)
367caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan
368caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	/* Load descriptor index from array of indices */
369caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	adr	x14, rt_svc_descs_indices
370caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	ldrb	w15, [x14, x16]
371caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan
372caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	/* -----------------------------------------------------
373caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * Restore the saved C runtime stack value which will
374caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * become the new SP_EL0 i.e. EL3 runtime stack. It was
375caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * saved in the 'cpu_context' structure prior to the last
376caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * ERET from EL3.
377caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * -----------------------------------------------------
378caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 */
379caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	ldr	x12, [x6, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
380caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan
381caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	/*
382caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * Any index greater than 127 is invalid. Check bit 7 for
383caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * a valid index
384caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 */
385caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	tbnz	w15, 7, smc_unknown
386caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan
387caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	/* Switch to SP_EL0 */
388caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	msr	spsel, #0
389caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan
390caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	/* -----------------------------------------------------
391caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * Get the descriptor using the index
392caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * x11 = (base + off), x15 = index
393caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 *
394caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * handler = (base + off) + (index << log2(size))
395caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * -----------------------------------------------------
396caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 */
397caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	lsl	w10, w15, #RT_SVC_SIZE_LOG2
398caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	ldr	x15, [x11, w10, uxtw]
399caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan
400caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	/* -----------------------------------------------------
401caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * Save the SPSR_EL3, ELR_EL3, & SCR_EL3 in case there
402caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * is a world switch during SMC handling.
403caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * TODO: Revisit if all system registers can be saved
404caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * later.
405caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * -----------------------------------------------------
406caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 */
407caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	mrs	x16, spsr_el3
408caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	mrs	x17, elr_el3
409caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	mrs	x18, scr_el3
410caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	stp	x16, x17, [x6, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
411b51da821821cfda0d44f09a6f92fdc5933f9b23bAchin Gupta	str	x18, [x6, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3]
412caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan
413caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	/* Copy SCR_EL3.NS bit to the flag to indicate caller's security */
414caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	bfi	x7, x18, #0, #1
415caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan
416caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	mov	sp, x12
417caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan
418caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	/* -----------------------------------------------------
419caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * Call the Secure Monitor Call handler and then drop
420caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * directly into el3_exit() which will program any
421caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * remaining architectural state prior to issuing the
422caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * ERET to the desired lower EL.
423caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * -----------------------------------------------------
424caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 */
425caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan#if DEBUG
426caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	cbz	x15, rt_svc_fw_critical_error
427caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan#endif
428caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	blr	x15
429caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan
430caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	/* -----------------------------------------------------
431caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * This routine assumes that the SP_EL3 is pointing to
432caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * a valid context structure from where the gp regs and
433caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * other special registers can be retrieved.
4340a30cf54af7bb1f77b405062b1d5b44e809d0290Andrew Thoelke	 *
4350a30cf54af7bb1f77b405062b1d5b44e809d0290Andrew Thoelke	 * Keep it in the same section as smc_handler as this
4360a30cf54af7bb1f77b405062b1d5b44e809d0290Andrew Thoelke	 * function uses a fall-through to el3_exit
437caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * -----------------------------------------------------
438caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 */
439caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharanel3_exit: ; .type el3_exit, %function
440caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	/* -----------------------------------------------------
441caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * Save the current SP_EL0 i.e. the EL3 runtime stack
442caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * which will be used for handling the next SMC. Then
443caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * switch to SP_EL3
444caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * -----------------------------------------------------
445caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 */
446caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	mov	x17, sp
447caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	msr	spsel, #1
448caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	str	x17, [sp, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
449caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan
450caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	/* -----------------------------------------------------
451caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * Restore SPSR_EL3, ELR_EL3 and SCR_EL3 prior to ERET
452caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * -----------------------------------------------------
453caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 */
454b51da821821cfda0d44f09a6f92fdc5933f9b23bAchin Gupta	ldr	x18, [sp, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3]
455caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	ldp	x16, x17, [sp, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
456caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	msr	scr_el3, x18
457caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	msr	spsr_el3, x16
458caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	msr	elr_el3, x17
459caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan
460caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	/* Restore saved general purpose registers and return */
461a43d431b80541ea436b71f967c5749babf978c7aSoby Mathew	b	restore_gp_registers_eret
4624f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta
463caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharansmc_unknown:
464caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	/*
465caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * Here we restore x4-x18 regardless of where we came from. AArch32
466caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * callers will find the registers contents unchanged, but AArch64
467caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * callers will find the registers modified (with stale earlier NS
468caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * content). Either way, we aren't leaking any secure information
469caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * through them
470caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 */
471a43d431b80541ea436b71f967c5749babf978c7aSoby Mathew	mov	w0, #SMC_UNK
472a43d431b80541ea436b71f967c5749babf978c7aSoby Mathew	b	restore_gp_registers_callee_eret
473caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan
474caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharansmc_prohibited:
475c3260f9b82c5017ca078f090c03cd7135ee8f8c9Soby Mathew	ldr	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
476caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	mov	w0, #SMC_UNK
477caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	eret
478caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan
479caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharanrt_svc_fw_critical_error:
480a43d431b80541ea436b71f967c5749babf978c7aSoby Mathew	msr	spsel, #1 /* Switch to SP_ELx */
481626ed510f179c905a699f4663ee933c10892b4c3Soby Mathew	bl	report_unhandled_exception
482caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan
483caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	/* -----------------------------------------------------
484caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * The following functions are used to saved and restore
485c3260f9b82c5017ca078f090c03cd7135ee8f8c9Soby Mathew	 * all the general pupose registers. Ideally we would
486c3260f9b82c5017ca078f090c03cd7135ee8f8c9Soby Mathew	 * only save and restore the callee saved registers when
487c3260f9b82c5017ca078f090c03cd7135ee8f8c9Soby Mathew	 * a world switch occurs but that type of implementation
488c3260f9b82c5017ca078f090c03cd7135ee8f8c9Soby Mathew	 * is more complex. So currently we will always save and
489c3260f9b82c5017ca078f090c03cd7135ee8f8c9Soby Mathew	 * restore these registers on entry and exit of EL3.
490caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * These are not macros to ensure their invocation fits
491caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * within the 32 instructions per exception vector.
492caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 * -----------------------------------------------------
493caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	 */
494c3260f9b82c5017ca078f090c03cd7135ee8f8c9Soby Mathewfunc save_gp_registers
495caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	stp	x0, x1, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X0]
496caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	stp	x2, x3, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X2]
497caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	stp	x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4]
498caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	stp	x6, x7, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X6]
499caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	stp	x8, x9, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X8]
500caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	stp	x10, x11, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X10]
501caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	stp	x12, x13, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X12]
502caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	stp	x14, x15, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X14]
503caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	stp	x16, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X16]
504c3260f9b82c5017ca078f090c03cd7135ee8f8c9Soby Mathew	save_x18_to_x29_sp_el0
505caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	ret
506caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan
507a43d431b80541ea436b71f967c5749babf978c7aSoby Mathewfunc restore_gp_registers_eret
508caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	ldp	x0, x1, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X0]
509caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	ldp	x2, x3, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X2]
510caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan
511a43d431b80541ea436b71f967c5749babf978c7aSoby Mathewrestore_gp_registers_callee_eret:
512caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	ldp	x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4]
513caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	ldp	x6, x7, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X6]
514caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	ldp	x8, x9, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X8]
515caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	ldp	x10, x11, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X10]
516caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	ldp	x12, x13, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X12]
517caa84939a4d8b1189dea8619ccc57bdb3026b125Jeenu Viswambharan	ldp	x14, x15, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X14]
518c3260f9b82c5017ca078f090c03cd7135ee8f8c9Soby Mathew	ldp	x18, x19, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X18]
519c3260f9b82c5017ca078f090c03cd7135ee8f8c9Soby Mathew	ldp	x20, x21, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X20]
520c3260f9b82c5017ca078f090c03cd7135ee8f8c9Soby Mathew	ldp	x22, x23, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X22]
521c3260f9b82c5017ca078f090c03cd7135ee8f8c9Soby Mathew	ldp	x24, x25, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X24]
522c3260f9b82c5017ca078f090c03cd7135ee8f8c9Soby Mathew	ldp	x26, x27, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X26]
523c3260f9b82c5017ca078f090c03cd7135ee8f8c9Soby Mathew	ldp	x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28]
524a43d431b80541ea436b71f967c5749babf978c7aSoby Mathew	ldp	x30, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
525a43d431b80541ea436b71f967c5749babf978c7aSoby Mathew	msr	sp_el0, x17
526a43d431b80541ea436b71f967c5749babf978c7aSoby Mathew	ldp	x16, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X16]
527a43d431b80541ea436b71f967c5749babf978c7aSoby Mathew	eret
528