xref: /rk3399_ARM-atf/bl31/aarch64/runtime_exceptions.S (revision 2f370465241c85b2fe38a1eb69b457b9fed97207)
14f6ad66aSAchin Gupta/*
2201ca5b6SDimitris Papastamos * Copyright (c) 2013-2018, ARM Limited and Contributors. All rights reserved.
34f6ad66aSAchin Gupta *
482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause
54f6ad66aSAchin Gupta */
64f6ad66aSAchin Gupta
74f6ad66aSAchin Gupta#include <arch.h>
835e98e55SDan Handley#include <asm_macros.S>
997043ac9SDan Handley#include <context.h>
10872be88aSdp-arm#include <cpu_data.h>
11dce74b89SAchin Gupta#include <interrupt_mgmt.h>
125f0cdb05SDan Handley#include <platform_def.h>
1397043ac9SDan Handley#include <runtime_svc.h>
14*2f370465SAntonio Nino Diaz#include <smccc.h>
154f6ad66aSAchin Gupta
164f6ad66aSAchin Gupta	.globl	runtime_exceptions
174f6ad66aSAchin Gupta
18f62ad322SDimitris Papastamos	.globl	sync_exception_sp_el0
19f62ad322SDimitris Papastamos	.globl	irq_sp_el0
20f62ad322SDimitris Papastamos	.globl	fiq_sp_el0
21f62ad322SDimitris Papastamos	.globl	serror_sp_el0
22f62ad322SDimitris Papastamos
23f62ad322SDimitris Papastamos	.globl	sync_exception_sp_elx
24f62ad322SDimitris Papastamos	.globl	irq_sp_elx
25f62ad322SDimitris Papastamos	.globl	fiq_sp_elx
26f62ad322SDimitris Papastamos	.globl	serror_sp_elx
27f62ad322SDimitris Papastamos
28f62ad322SDimitris Papastamos	.globl	sync_exception_aarch64
29f62ad322SDimitris Papastamos	.globl	irq_aarch64
30f62ad322SDimitris Papastamos	.globl	fiq_aarch64
31f62ad322SDimitris Papastamos	.globl	serror_aarch64
32f62ad322SDimitris Papastamos
33f62ad322SDimitris Papastamos	.globl	sync_exception_aarch32
34f62ad322SDimitris Papastamos	.globl	irq_aarch32
35f62ad322SDimitris Papastamos	.globl	fiq_aarch32
36f62ad322SDimitris Papastamos	.globl	serror_aarch32
37f62ad322SDimitris Papastamos
38a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
39a6ef4393SDouglas Raillard	 * This macro handles Synchronous exceptions.
40a6ef4393SDouglas Raillard	 * Only SMC exceptions are supported.
41a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
42dce74b89SAchin Gupta	 */
43dce74b89SAchin Gupta	.macro	handle_sync_exception
440c8d4fefSAchin Gupta	/* Enable the SError interrupt */
450c8d4fefSAchin Gupta	msr	daifclr, #DAIF_ABT_BIT
460c8d4fefSAchin Gupta
47dce74b89SAchin Gupta	str	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
48872be88aSdp-arm
49872be88aSdp-arm#if ENABLE_RUNTIME_INSTRUMENTATION
50872be88aSdp-arm	/*
51a6ef4393SDouglas Raillard	 * Read the timestamp value and store it in per-cpu data. The value
52a6ef4393SDouglas Raillard	 * will be extracted from per-cpu data by the C level SMC handler and
53a6ef4393SDouglas Raillard	 * saved to the PMF timestamp region.
54872be88aSdp-arm	 */
55872be88aSdp-arm	mrs	x30, cntpct_el0
56872be88aSdp-arm	str	x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29]
57872be88aSdp-arm	mrs	x29, tpidr_el3
58872be88aSdp-arm	str	x30, [x29, #CPU_DATA_PMF_TS0_OFFSET]
59872be88aSdp-arm	ldr	x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29]
60872be88aSdp-arm#endif
61872be88aSdp-arm
62dce74b89SAchin Gupta	mrs	x30, esr_el3
63dce74b89SAchin Gupta	ubfx	x30, x30, #ESR_EC_SHIFT, #ESR_EC_LENGTH
64dce74b89SAchin Gupta
65a6ef4393SDouglas Raillard	/* Handle SMC exceptions separately from other synchronous exceptions */
66dce74b89SAchin Gupta	cmp	x30, #EC_AARCH32_SMC
67dce74b89SAchin Gupta	b.eq	smc_handler32
68dce74b89SAchin Gupta
69dce74b89SAchin Gupta	cmp	x30, #EC_AARCH64_SMC
70dce74b89SAchin Gupta	b.eq	smc_handler64
71dce74b89SAchin Gupta
72a6ef4393SDouglas Raillard	/* Other kinds of synchronous exceptions are not handled */
734d91838bSJulius Werner	ldr	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
744d91838bSJulius Werner	b	report_unhandled_exception
75dce74b89SAchin Gupta	.endm
76dce74b89SAchin Gupta
77dce74b89SAchin Gupta
78a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
79a6ef4393SDouglas Raillard	 * This macro handles FIQ or IRQ interrupts i.e. EL3, S-EL1 and NS
80a6ef4393SDouglas Raillard	 * interrupts.
81a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
82dce74b89SAchin Gupta	 */
83dce74b89SAchin Gupta	.macro	handle_interrupt_exception label
840c8d4fefSAchin Gupta	/* Enable the SError interrupt */
850c8d4fefSAchin Gupta	msr	daifclr, #DAIF_ABT_BIT
860c8d4fefSAchin Gupta
87dce74b89SAchin Gupta	str	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
88dce74b89SAchin Gupta	bl	save_gp_registers
89dce74b89SAchin Gupta
90a6ef4393SDouglas Raillard	/* Save the EL3 system registers needed to return from this exception */
915717aae1SAchin Gupta	mrs	x0, spsr_el3
925717aae1SAchin Gupta	mrs	x1, elr_el3
935717aae1SAchin Gupta	stp	x0, x1, [sp, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
945717aae1SAchin Gupta
95dce74b89SAchin Gupta	/* Switch to the runtime stack i.e. SP_EL0 */
96dce74b89SAchin Gupta	ldr	x2, [sp, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
97dce74b89SAchin Gupta	mov	x20, sp
98dce74b89SAchin Gupta	msr	spsel, #0
99dce74b89SAchin Gupta	mov	sp, x2
100dce74b89SAchin Gupta
101dce74b89SAchin Gupta	/*
102a6ef4393SDouglas Raillard	 * Find out whether this is a valid interrupt type.
103a6ef4393SDouglas Raillard	 * If the interrupt controller reports a spurious interrupt then return
104a6ef4393SDouglas Raillard	 * to where we came from.
105dce74b89SAchin Gupta	 */
1069865ac15SDan Handley	bl	plat_ic_get_pending_interrupt_type
107dce74b89SAchin Gupta	cmp	x0, #INTR_TYPE_INVAL
108dce74b89SAchin Gupta	b.eq	interrupt_exit_\label
109dce74b89SAchin Gupta
110dce74b89SAchin Gupta	/*
111a6ef4393SDouglas Raillard	 * Get the registered handler for this interrupt type.
112a6ef4393SDouglas Raillard	 * A NULL return value could be 'cause of the following conditions:
1135717aae1SAchin Gupta	 *
114a6ef4393SDouglas Raillard	 * a. An interrupt of a type was routed correctly but a handler for its
115a6ef4393SDouglas Raillard	 *    type was not registered.
1165717aae1SAchin Gupta	 *
117a6ef4393SDouglas Raillard	 * b. An interrupt of a type was not routed correctly so a handler for
118a6ef4393SDouglas Raillard	 *    its type was not registered.
1195717aae1SAchin Gupta	 *
120a6ef4393SDouglas Raillard	 * c. An interrupt of a type was routed correctly to EL3, but was
121a6ef4393SDouglas Raillard	 *    deasserted before its pending state could be read. Another
122a6ef4393SDouglas Raillard	 *    interrupt of a different type pended at the same time and its
123a6ef4393SDouglas Raillard	 *    type was reported as pending instead. However, a handler for this
124a6ef4393SDouglas Raillard	 *    type was not registered.
1255717aae1SAchin Gupta	 *
126a6ef4393SDouglas Raillard	 * a. and b. can only happen due to a programming error. The
127a6ef4393SDouglas Raillard	 * occurrence of c. could be beyond the control of Trusted Firmware.
128a6ef4393SDouglas Raillard	 * It makes sense to return from this exception instead of reporting an
129a6ef4393SDouglas Raillard	 * error.
130dce74b89SAchin Gupta	 */
131dce74b89SAchin Gupta	bl	get_interrupt_type_handler
1325717aae1SAchin Gupta	cbz	x0, interrupt_exit_\label
133dce74b89SAchin Gupta	mov	x21, x0
134dce74b89SAchin Gupta
135dce74b89SAchin Gupta	mov	x0, #INTR_ID_UNAVAILABLE
136dce74b89SAchin Gupta
137dce74b89SAchin Gupta	/* Set the current security state in the 'flags' parameter */
138dce74b89SAchin Gupta	mrs	x2, scr_el3
139dce74b89SAchin Gupta	ubfx	x1, x2, #0, #1
140dce74b89SAchin Gupta
141dce74b89SAchin Gupta	/* Restore the reference to the 'handle' i.e. SP_EL3 */
142dce74b89SAchin Gupta	mov	x2, x20
143dce74b89SAchin Gupta
144b460b8bfSSoby Mathew	/* x3 will point to a cookie (not used now) */
145b460b8bfSSoby Mathew	mov	x3, xzr
146b460b8bfSSoby Mathew
147dce74b89SAchin Gupta	/* Call the interrupt type handler */
148dce74b89SAchin Gupta	blr	x21
149dce74b89SAchin Gupta
150dce74b89SAchin Guptainterrupt_exit_\label:
151dce74b89SAchin Gupta	/* Return from exception, possibly in a different security state */
152dce74b89SAchin Gupta	b	el3_exit
153dce74b89SAchin Gupta
154dce74b89SAchin Gupta	.endm
155dce74b89SAchin Gupta
156dce74b89SAchin Gupta
157201ca5b6SDimitris Papastamos	.macro save_x4_to_x29_sp_el0
158201ca5b6SDimitris Papastamos	stp	x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4]
159201ca5b6SDimitris Papastamos	stp	x6, x7, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X6]
160201ca5b6SDimitris Papastamos	stp	x8, x9, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X8]
161201ca5b6SDimitris Papastamos	stp	x10, x11, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X10]
162201ca5b6SDimitris Papastamos	stp	x12, x13, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X12]
163201ca5b6SDimitris Papastamos	stp	x14, x15, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X14]
164201ca5b6SDimitris Papastamos	stp	x16, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X16]
165c3260f9bSSoby Mathew	stp	x18, x19, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X18]
166c3260f9bSSoby Mathew	stp	x20, x21, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X20]
167c3260f9bSSoby Mathew	stp	x22, x23, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X22]
168c3260f9bSSoby Mathew	stp	x24, x25, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X24]
169c3260f9bSSoby Mathew	stp	x26, x27, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X26]
170c3260f9bSSoby Mathew	stp	x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28]
171c3260f9bSSoby Mathew	mrs	x18, sp_el0
172c3260f9bSSoby Mathew	str	x18, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_SP_EL0]
173c3260f9bSSoby Mathew	.endm
174c3260f9bSSoby Mathew
175e0ae9fabSSandrine Bailleux
176e0ae9fabSSandrine Bailleuxvector_base runtime_exceptions
177e0ae9fabSSandrine Bailleux
178a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
179a6ef4393SDouglas Raillard	 * Current EL with SP_EL0 : 0x0 - 0x200
180a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
1814f6ad66aSAchin Gupta	 */
182e0ae9fabSSandrine Bailleuxvector_entry sync_exception_sp_el0
183a6ef4393SDouglas Raillard	/* We don't expect any synchronous exceptions from EL3 */
1844d91838bSJulius Werner	b	report_unhandled_exception
185a7934d69SJeenu Viswambharan	check_vector_size sync_exception_sp_el0
1864f6ad66aSAchin Gupta
187e0ae9fabSSandrine Bailleuxvector_entry irq_sp_el0
188a6ef4393SDouglas Raillard	/*
189a6ef4393SDouglas Raillard	 * EL3 code is non-reentrant. Any asynchronous exception is a serious
190a6ef4393SDouglas Raillard	 * error. Loop infinitely.
191a6ef4393SDouglas Raillard	 */
1924d91838bSJulius Werner	b	report_unhandled_interrupt
193a7934d69SJeenu Viswambharan	check_vector_size irq_sp_el0
1944f6ad66aSAchin Gupta
195e0ae9fabSSandrine Bailleux
196e0ae9fabSSandrine Bailleuxvector_entry fiq_sp_el0
1974d91838bSJulius Werner	b	report_unhandled_interrupt
198a7934d69SJeenu Viswambharan	check_vector_size fiq_sp_el0
1994f6ad66aSAchin Gupta
200e0ae9fabSSandrine Bailleux
201e0ae9fabSSandrine Bailleuxvector_entry serror_sp_el0
2024d91838bSJulius Werner	b	report_unhandled_exception
203a7934d69SJeenu Viswambharan	check_vector_size serror_sp_el0
2044f6ad66aSAchin Gupta
205a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
206a6ef4393SDouglas Raillard	 * Current EL with SP_ELx: 0x200 - 0x400
207a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
2084f6ad66aSAchin Gupta	 */
209e0ae9fabSSandrine Bailleuxvector_entry sync_exception_sp_elx
210a6ef4393SDouglas Raillard	/*
211a6ef4393SDouglas Raillard	 * This exception will trigger if anything went wrong during a previous
212a6ef4393SDouglas Raillard	 * exception entry or exit or while handling an earlier unexpected
213a6ef4393SDouglas Raillard	 * synchronous exception. There is a high probability that SP_EL3 is
214a6ef4393SDouglas Raillard	 * corrupted.
215caa84939SJeenu Viswambharan	 */
2164d91838bSJulius Werner	b	report_unhandled_exception
217a7934d69SJeenu Viswambharan	check_vector_size sync_exception_sp_elx
2184f6ad66aSAchin Gupta
219e0ae9fabSSandrine Bailleuxvector_entry irq_sp_elx
2204d91838bSJulius Werner	b	report_unhandled_interrupt
221a7934d69SJeenu Viswambharan	check_vector_size irq_sp_elx
222a7934d69SJeenu Viswambharan
223e0ae9fabSSandrine Bailleuxvector_entry fiq_sp_elx
2244d91838bSJulius Werner	b	report_unhandled_interrupt
225a7934d69SJeenu Viswambharan	check_vector_size fiq_sp_elx
226a7934d69SJeenu Viswambharan
227e0ae9fabSSandrine Bailleuxvector_entry serror_sp_elx
2284d91838bSJulius Werner	b	report_unhandled_exception
229a7934d69SJeenu Viswambharan	check_vector_size serror_sp_elx
2304f6ad66aSAchin Gupta
231a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
23244804252SSandrine Bailleux	 * Lower EL using AArch64 : 0x400 - 0x600
233a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
2344f6ad66aSAchin Gupta	 */
235e0ae9fabSSandrine Bailleuxvector_entry sync_exception_aarch64
236a6ef4393SDouglas Raillard	/*
237a6ef4393SDouglas Raillard	 * This exception vector will be the entry point for SMCs and traps
238a6ef4393SDouglas Raillard	 * that are unhandled at lower ELs most commonly. SP_EL3 should point
239a6ef4393SDouglas Raillard	 * to a valid cpu context where the general purpose and system register
240a6ef4393SDouglas Raillard	 * state can be saved.
241caa84939SJeenu Viswambharan	 */
242caa84939SJeenu Viswambharan	handle_sync_exception
243a7934d69SJeenu Viswambharan	check_vector_size sync_exception_aarch64
2444f6ad66aSAchin Gupta
245e0ae9fabSSandrine Bailleuxvector_entry irq_aarch64
246dce74b89SAchin Gupta	handle_interrupt_exception irq_aarch64
247a7934d69SJeenu Viswambharan	check_vector_size irq_aarch64
2484f6ad66aSAchin Gupta
249e0ae9fabSSandrine Bailleuxvector_entry fiq_aarch64
250dce74b89SAchin Gupta	handle_interrupt_exception fiq_aarch64
251a7934d69SJeenu Viswambharan	check_vector_size fiq_aarch64
2524f6ad66aSAchin Gupta
253e0ae9fabSSandrine Bailleuxvector_entry serror_aarch64
254a6ef4393SDouglas Raillard	/*
255a6ef4393SDouglas Raillard	 * SError exceptions from lower ELs are not currently supported.
256a6ef4393SDouglas Raillard	 * Report their occurrence.
257a6ef4393SDouglas Raillard	 */
2584d91838bSJulius Werner	b	report_unhandled_exception
259a7934d69SJeenu Viswambharan	check_vector_size serror_aarch64
2604f6ad66aSAchin Gupta
261a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
26244804252SSandrine Bailleux	 * Lower EL using AArch32 : 0x600 - 0x800
263a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
2644f6ad66aSAchin Gupta	 */
265e0ae9fabSSandrine Bailleuxvector_entry sync_exception_aarch32
266a6ef4393SDouglas Raillard	/*
267a6ef4393SDouglas Raillard	 * This exception vector will be the entry point for SMCs and traps
268a6ef4393SDouglas Raillard	 * that are unhandled at lower ELs most commonly. SP_EL3 should point
269a6ef4393SDouglas Raillard	 * to a valid cpu context where the general purpose and system register
270a6ef4393SDouglas Raillard	 * state can be saved.
271caa84939SJeenu Viswambharan	 */
272caa84939SJeenu Viswambharan	handle_sync_exception
273a7934d69SJeenu Viswambharan	check_vector_size sync_exception_aarch32
2744f6ad66aSAchin Gupta
275e0ae9fabSSandrine Bailleuxvector_entry irq_aarch32
276dce74b89SAchin Gupta	handle_interrupt_exception irq_aarch32
277a7934d69SJeenu Viswambharan	check_vector_size irq_aarch32
2784f6ad66aSAchin Gupta
279e0ae9fabSSandrine Bailleuxvector_entry fiq_aarch32
280dce74b89SAchin Gupta	handle_interrupt_exception fiq_aarch32
281a7934d69SJeenu Viswambharan	check_vector_size fiq_aarch32
2824f6ad66aSAchin Gupta
283e0ae9fabSSandrine Bailleuxvector_entry serror_aarch32
284a6ef4393SDouglas Raillard	/*
285a6ef4393SDouglas Raillard	 * SError exceptions from lower ELs are not currently supported.
286a6ef4393SDouglas Raillard	 * Report their occurrence.
287a6ef4393SDouglas Raillard	 */
2884d91838bSJulius Werner	b	report_unhandled_exception
289a7934d69SJeenu Viswambharan	check_vector_size serror_aarch32
290a7934d69SJeenu Viswambharan
291caa84939SJeenu Viswambharan
292a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
293*2f370465SAntonio Nino Diaz	 * This macro takes an argument in x16 that is the index in the
294*2f370465SAntonio Nino Diaz	 * 'rt_svc_descs_indices' array, checks that the value in the array is
295*2f370465SAntonio Nino Diaz	 * valid, and loads in x15 the pointer to the handler of that service.
296*2f370465SAntonio Nino Diaz	 * ---------------------------------------------------------------------
297*2f370465SAntonio Nino Diaz	 */
298*2f370465SAntonio Nino Diaz	.macro	load_rt_svc_desc_pointer
299*2f370465SAntonio Nino Diaz	/* Load descriptor index from array of indices */
300*2f370465SAntonio Nino Diaz	adr	x14, rt_svc_descs_indices
301*2f370465SAntonio Nino Diaz	ldrb	w15, [x14, x16]
302*2f370465SAntonio Nino Diaz
303*2f370465SAntonio Nino Diaz#if SMCCC_MAJOR_VERSION == 1
304*2f370465SAntonio Nino Diaz	/* Any index greater than 127 is invalid. Check bit 7. */
305*2f370465SAntonio Nino Diaz	tbnz	w15, 7, smc_unknown
306*2f370465SAntonio Nino Diaz#elif SMCCC_MAJOR_VERSION == 2
307*2f370465SAntonio Nino Diaz	/* Verify that the top 3 bits of the loaded index are 0 (w15 <= 31) */
308*2f370465SAntonio Nino Diaz	cmp	w15, #31
309*2f370465SAntonio Nino Diaz	b.hi	smc_unknown
310*2f370465SAntonio Nino Diaz#endif /* SMCCC_MAJOR_VERSION */
311*2f370465SAntonio Nino Diaz
312*2f370465SAntonio Nino Diaz	/*
313*2f370465SAntonio Nino Diaz	 * Get the descriptor using the index
314*2f370465SAntonio Nino Diaz	 * x11 = (base + off), w15 = index
315*2f370465SAntonio Nino Diaz	 *
316*2f370465SAntonio Nino Diaz	 * handler = (base + off) + (index << log2(size))
317*2f370465SAntonio Nino Diaz	 */
318*2f370465SAntonio Nino Diaz	adr	x11, (__RT_SVC_DESCS_START__ + RT_SVC_DESC_HANDLE)
319*2f370465SAntonio Nino Diaz	lsl	w10, w15, #RT_SVC_SIZE_LOG2
320*2f370465SAntonio Nino Diaz	ldr	x15, [x11, w10, uxtw]
321*2f370465SAntonio Nino Diaz	.endm
322*2f370465SAntonio Nino Diaz
323*2f370465SAntonio Nino Diaz	/* ---------------------------------------------------------------------
324caa84939SJeenu Viswambharan	 * The following code handles secure monitor calls.
325a6ef4393SDouglas Raillard	 * Depending upon the execution state from where the SMC has been
326a6ef4393SDouglas Raillard	 * invoked, it frees some general purpose registers to perform the
327a6ef4393SDouglas Raillard	 * remaining tasks. They involve finding the runtime service handler
328a6ef4393SDouglas Raillard	 * that is the target of the SMC & switching to runtime stacks (SP_EL0)
329a6ef4393SDouglas Raillard	 * before calling the handler.
330caa84939SJeenu Viswambharan	 *
331a6ef4393SDouglas Raillard	 * Note that x30 has been explicitly saved and can be used here
332a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
333caa84939SJeenu Viswambharan	 */
3340a30cf54SAndrew Thoelkefunc smc_handler
335caa84939SJeenu Viswambharansmc_handler32:
336caa84939SJeenu Viswambharan	/* Check whether aarch32 issued an SMC64 */
337caa84939SJeenu Viswambharan	tbnz	x0, #FUNCID_CC_SHIFT, smc_prohibited
338caa84939SJeenu Viswambharan
339caa84939SJeenu Viswambharansmc_handler64:
340a6ef4393SDouglas Raillard	/*
341a6ef4393SDouglas Raillard	 * Populate the parameters for the SMC handler.
342a6ef4393SDouglas Raillard	 * We already have x0-x4 in place. x5 will point to a cookie (not used
343a6ef4393SDouglas Raillard	 * now). x6 will point to the context structure (SP_EL3) and x7 will
344201ca5b6SDimitris Papastamos	 * contain flags we need to pass to the handler.
345a6ef4393SDouglas Raillard	 *
346*2f370465SAntonio Nino Diaz	 * Save x4-x29 and sp_el0.
347caa84939SJeenu Viswambharan	 */
348201ca5b6SDimitris Papastamos	save_x4_to_x29_sp_el0
349c3260f9bSSoby Mathew
350caa84939SJeenu Viswambharan	mov	x5, xzr
351caa84939SJeenu Viswambharan	mov	x6, sp
352caa84939SJeenu Viswambharan
353*2f370465SAntonio Nino Diaz#if SMCCC_MAJOR_VERSION == 1
354*2f370465SAntonio Nino Diaz
355caa84939SJeenu Viswambharan	/* Get the unique owning entity number */
356caa84939SJeenu Viswambharan	ubfx	x16, x0, #FUNCID_OEN_SHIFT, #FUNCID_OEN_WIDTH
357caa84939SJeenu Viswambharan	ubfx	x15, x0, #FUNCID_TYPE_SHIFT, #FUNCID_TYPE_WIDTH
358caa84939SJeenu Viswambharan	orr	x16, x16, x15, lsl #FUNCID_OEN_WIDTH
359caa84939SJeenu Viswambharan
360*2f370465SAntonio Nino Diaz	load_rt_svc_desc_pointer
361caa84939SJeenu Viswambharan
362*2f370465SAntonio Nino Diaz#elif SMCCC_MAJOR_VERSION == 2
363*2f370465SAntonio Nino Diaz
364*2f370465SAntonio Nino Diaz	/* Bit 31 must be set */
365*2f370465SAntonio Nino Diaz	tbz	x0, #FUNCID_TYPE_SHIFT, smc_unknown
366*2f370465SAntonio Nino Diaz
367*2f370465SAntonio Nino Diaz	/*
368*2f370465SAntonio Nino Diaz	 * Check MSB of namespace to decide between compatibility/vendor and
369*2f370465SAntonio Nino Diaz	 * SPCI/SPRT
370*2f370465SAntonio Nino Diaz	 */
371*2f370465SAntonio Nino Diaz	tbz	x0, #(FUNCID_NAMESPACE_SHIFT + 1), compat_or_vendor
372*2f370465SAntonio Nino Diaz
373*2f370465SAntonio Nino Diaz	/* Namespaces SPRT and SPCI currently unimplemented */
374*2f370465SAntonio Nino Diaz	b	smc_unknown
375*2f370465SAntonio Nino Diaz
376*2f370465SAntonio Nino Diazcompat_or_vendor:
377*2f370465SAntonio Nino Diaz
378*2f370465SAntonio Nino Diaz	/* Namespace is b'00 (compatibility) or b'01 (vendor) */
379*2f370465SAntonio Nino Diaz
380*2f370465SAntonio Nino Diaz	/*
381*2f370465SAntonio Nino Diaz	 * Add the LSB of the namespace (bit [28]) to the OEN [27:24] to create
382*2f370465SAntonio Nino Diaz	 * a 5-bit index into the rt_svc_descs_indices array.
383*2f370465SAntonio Nino Diaz	 *
384*2f370465SAntonio Nino Diaz	 * The low 16 entries of the rt_svc_descs_indices array correspond to
385*2f370465SAntonio Nino Diaz	 * OENs of the compatibility namespace and the top 16 entries of the
386*2f370465SAntonio Nino Diaz	 * array are assigned to the vendor namespace descriptor.
387*2f370465SAntonio Nino Diaz	 */
388*2f370465SAntonio Nino Diaz	ubfx	x16, x0, #FUNCID_OEN_SHIFT, #(FUNCID_OEN_WIDTH + 1)
389*2f370465SAntonio Nino Diaz
390*2f370465SAntonio Nino Diaz	load_rt_svc_desc_pointer
391*2f370465SAntonio Nino Diaz
392*2f370465SAntonio Nino Diaz#endif /* SMCCC_MAJOR_VERSION */
393caa84939SJeenu Viswambharan
394a6ef4393SDouglas Raillard	/*
395a6ef4393SDouglas Raillard	 * Restore the saved C runtime stack value which will become the new
396a6ef4393SDouglas Raillard	 * SP_EL0 i.e. EL3 runtime stack. It was saved in the 'cpu_context'
397a6ef4393SDouglas Raillard	 * structure prior to the last ERET from EL3.
398caa84939SJeenu Viswambharan	 */
399caa84939SJeenu Viswambharan	ldr	x12, [x6, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
400caa84939SJeenu Viswambharan
401caa84939SJeenu Viswambharan	/* Switch to SP_EL0 */
402caa84939SJeenu Viswambharan	msr	spsel, #0
403caa84939SJeenu Viswambharan
404a6ef4393SDouglas Raillard	/*
405a6ef4393SDouglas Raillard	 * Save the SPSR_EL3, ELR_EL3, & SCR_EL3 in case there is a world
406a6ef4393SDouglas Raillard	 * switch during SMC handling.
407a6ef4393SDouglas Raillard	 * TODO: Revisit if all system registers can be saved later.
408caa84939SJeenu Viswambharan	 */
409caa84939SJeenu Viswambharan	mrs	x16, spsr_el3
410caa84939SJeenu Viswambharan	mrs	x17, elr_el3
411caa84939SJeenu Viswambharan	mrs	x18, scr_el3
412caa84939SJeenu Viswambharan	stp	x16, x17, [x6, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
413b51da821SAchin Gupta	str	x18, [x6, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3]
414caa84939SJeenu Viswambharan
415caa84939SJeenu Viswambharan	/* Copy SCR_EL3.NS bit to the flag to indicate caller's security */
416caa84939SJeenu Viswambharan	bfi	x7, x18, #0, #1
417caa84939SJeenu Viswambharan
418caa84939SJeenu Viswambharan	mov	sp, x12
419caa84939SJeenu Viswambharan
420a6ef4393SDouglas Raillard	/*
421a6ef4393SDouglas Raillard	 * Call the Secure Monitor Call handler and then drop directly into
422a6ef4393SDouglas Raillard	 * el3_exit() which will program any remaining architectural state
423a6ef4393SDouglas Raillard	 * prior to issuing the ERET to the desired lower EL.
424caa84939SJeenu Viswambharan	 */
425caa84939SJeenu Viswambharan#if DEBUG
426caa84939SJeenu Viswambharan	cbz	x15, rt_svc_fw_critical_error
427caa84939SJeenu Viswambharan#endif
428caa84939SJeenu Viswambharan	blr	x15
429caa84939SJeenu Viswambharan
430bbf8f6f9SYatharth Kochar	b	el3_exit
4314f6ad66aSAchin Gupta
432caa84939SJeenu Viswambharansmc_unknown:
433caa84939SJeenu Viswambharan	/*
434caa84939SJeenu Viswambharan	 * Here we restore x4-x18 regardless of where we came from. AArch32
435caa84939SJeenu Viswambharan	 * callers will find the registers contents unchanged, but AArch64
436caa84939SJeenu Viswambharan	 * callers will find the registers modified (with stale earlier NS
437caa84939SJeenu Viswambharan	 * content). Either way, we aren't leaking any secure information
438a6ef4393SDouglas Raillard	 * through them.
439caa84939SJeenu Viswambharan	 */
4404abd7fa7SAntonio Nino Diaz	mov	x0, #SMC_UNK
441a43d431bSSoby Mathew	b	restore_gp_registers_callee_eret
442caa84939SJeenu Viswambharan
443caa84939SJeenu Viswambharansmc_prohibited:
444c3260f9bSSoby Mathew	ldr	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
4454abd7fa7SAntonio Nino Diaz	mov	x0, #SMC_UNK
446caa84939SJeenu Viswambharan	eret
447caa84939SJeenu Viswambharan
448caa84939SJeenu Viswambharanrt_svc_fw_critical_error:
449a6ef4393SDouglas Raillard	/* Switch to SP_ELx */
450a6ef4393SDouglas Raillard	msr	spsel, #1
451a806dad5SJeenu Viswambharan	no_ret	report_unhandled_exception
4528b779620SKévin Petitendfunc smc_handler
453