xref: /rk3399_ARM-atf/bl31/aarch64/runtime_exceptions.S (revision ef653d93ccd6ba1888c61706469021fc623c3318)
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>
142f370465SAntonio 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
157e0ae9fabSSandrine Bailleuxvector_base runtime_exceptions
158e0ae9fabSSandrine Bailleux
159a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
160a6ef4393SDouglas Raillard	 * Current EL with SP_EL0 : 0x0 - 0x200
161a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
1624f6ad66aSAchin Gupta	 */
163e0ae9fabSSandrine Bailleuxvector_entry sync_exception_sp_el0
164a6ef4393SDouglas Raillard	/* We don't expect any synchronous exceptions from EL3 */
1654d91838bSJulius Werner	b	report_unhandled_exception
166a7934d69SJeenu Viswambharan	check_vector_size sync_exception_sp_el0
1674f6ad66aSAchin Gupta
168e0ae9fabSSandrine Bailleuxvector_entry irq_sp_el0
169a6ef4393SDouglas Raillard	/*
170a6ef4393SDouglas Raillard	 * EL3 code is non-reentrant. Any asynchronous exception is a serious
171a6ef4393SDouglas Raillard	 * error. Loop infinitely.
172a6ef4393SDouglas Raillard	 */
1734d91838bSJulius Werner	b	report_unhandled_interrupt
174a7934d69SJeenu Viswambharan	check_vector_size irq_sp_el0
1754f6ad66aSAchin Gupta
176e0ae9fabSSandrine Bailleux
177e0ae9fabSSandrine Bailleuxvector_entry fiq_sp_el0
1784d91838bSJulius Werner	b	report_unhandled_interrupt
179a7934d69SJeenu Viswambharan	check_vector_size fiq_sp_el0
1804f6ad66aSAchin Gupta
181e0ae9fabSSandrine Bailleux
182e0ae9fabSSandrine Bailleuxvector_entry serror_sp_el0
1834d91838bSJulius Werner	b	report_unhandled_exception
184a7934d69SJeenu Viswambharan	check_vector_size serror_sp_el0
1854f6ad66aSAchin Gupta
186a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
187a6ef4393SDouglas Raillard	 * Current EL with SP_ELx: 0x200 - 0x400
188a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
1894f6ad66aSAchin Gupta	 */
190e0ae9fabSSandrine Bailleuxvector_entry sync_exception_sp_elx
191a6ef4393SDouglas Raillard	/*
192a6ef4393SDouglas Raillard	 * This exception will trigger if anything went wrong during a previous
193a6ef4393SDouglas Raillard	 * exception entry or exit or while handling an earlier unexpected
194a6ef4393SDouglas Raillard	 * synchronous exception. There is a high probability that SP_EL3 is
195a6ef4393SDouglas Raillard	 * corrupted.
196caa84939SJeenu Viswambharan	 */
1974d91838bSJulius Werner	b	report_unhandled_exception
198a7934d69SJeenu Viswambharan	check_vector_size sync_exception_sp_elx
1994f6ad66aSAchin Gupta
200e0ae9fabSSandrine Bailleuxvector_entry irq_sp_elx
2014d91838bSJulius Werner	b	report_unhandled_interrupt
202a7934d69SJeenu Viswambharan	check_vector_size irq_sp_elx
203a7934d69SJeenu Viswambharan
204e0ae9fabSSandrine Bailleuxvector_entry fiq_sp_elx
2054d91838bSJulius Werner	b	report_unhandled_interrupt
206a7934d69SJeenu Viswambharan	check_vector_size fiq_sp_elx
207a7934d69SJeenu Viswambharan
208e0ae9fabSSandrine Bailleuxvector_entry serror_sp_elx
2094d91838bSJulius Werner	b	report_unhandled_exception
210a7934d69SJeenu Viswambharan	check_vector_size serror_sp_elx
2114f6ad66aSAchin Gupta
212a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
21344804252SSandrine Bailleux	 * Lower EL using AArch64 : 0x400 - 0x600
214a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
2154f6ad66aSAchin Gupta	 */
216e0ae9fabSSandrine Bailleuxvector_entry sync_exception_aarch64
217a6ef4393SDouglas Raillard	/*
218a6ef4393SDouglas Raillard	 * This exception vector will be the entry point for SMCs and traps
219a6ef4393SDouglas Raillard	 * that are unhandled at lower ELs most commonly. SP_EL3 should point
220a6ef4393SDouglas Raillard	 * to a valid cpu context where the general purpose and system register
221a6ef4393SDouglas Raillard	 * state can be saved.
222caa84939SJeenu Viswambharan	 */
223caa84939SJeenu Viswambharan	handle_sync_exception
224a7934d69SJeenu Viswambharan	check_vector_size sync_exception_aarch64
2254f6ad66aSAchin Gupta
226e0ae9fabSSandrine Bailleuxvector_entry irq_aarch64
227dce74b89SAchin Gupta	handle_interrupt_exception irq_aarch64
228a7934d69SJeenu Viswambharan	check_vector_size irq_aarch64
2294f6ad66aSAchin Gupta
230e0ae9fabSSandrine Bailleuxvector_entry fiq_aarch64
231dce74b89SAchin Gupta	handle_interrupt_exception fiq_aarch64
232a7934d69SJeenu Viswambharan	check_vector_size fiq_aarch64
2334f6ad66aSAchin Gupta
234e0ae9fabSSandrine Bailleuxvector_entry serror_aarch64
235a6ef4393SDouglas Raillard	/*
236a6ef4393SDouglas Raillard	 * SError exceptions from lower ELs are not currently supported.
237a6ef4393SDouglas Raillard	 * Report their occurrence.
238a6ef4393SDouglas Raillard	 */
2394d91838bSJulius Werner	b	report_unhandled_exception
240a7934d69SJeenu Viswambharan	check_vector_size serror_aarch64
2414f6ad66aSAchin Gupta
242a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
24344804252SSandrine Bailleux	 * Lower EL using AArch32 : 0x600 - 0x800
244a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
2454f6ad66aSAchin Gupta	 */
246e0ae9fabSSandrine Bailleuxvector_entry sync_exception_aarch32
247a6ef4393SDouglas Raillard	/*
248a6ef4393SDouglas Raillard	 * This exception vector will be the entry point for SMCs and traps
249a6ef4393SDouglas Raillard	 * that are unhandled at lower ELs most commonly. SP_EL3 should point
250a6ef4393SDouglas Raillard	 * to a valid cpu context where the general purpose and system register
251a6ef4393SDouglas Raillard	 * state can be saved.
252caa84939SJeenu Viswambharan	 */
253caa84939SJeenu Viswambharan	handle_sync_exception
254a7934d69SJeenu Viswambharan	check_vector_size sync_exception_aarch32
2554f6ad66aSAchin Gupta
256e0ae9fabSSandrine Bailleuxvector_entry irq_aarch32
257dce74b89SAchin Gupta	handle_interrupt_exception irq_aarch32
258a7934d69SJeenu Viswambharan	check_vector_size irq_aarch32
2594f6ad66aSAchin Gupta
260e0ae9fabSSandrine Bailleuxvector_entry fiq_aarch32
261dce74b89SAchin Gupta	handle_interrupt_exception fiq_aarch32
262a7934d69SJeenu Viswambharan	check_vector_size fiq_aarch32
2634f6ad66aSAchin Gupta
264e0ae9fabSSandrine Bailleuxvector_entry serror_aarch32
265a6ef4393SDouglas Raillard	/*
266a6ef4393SDouglas Raillard	 * SError exceptions from lower ELs are not currently supported.
267a6ef4393SDouglas Raillard	 * Report their occurrence.
268a6ef4393SDouglas Raillard	 */
2694d91838bSJulius Werner	b	report_unhandled_exception
270a7934d69SJeenu Viswambharan	check_vector_size serror_aarch32
271a7934d69SJeenu Viswambharan
272caa84939SJeenu Viswambharan
273a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
2742f370465SAntonio Nino Diaz	 * This macro takes an argument in x16 that is the index in the
2752f370465SAntonio Nino Diaz	 * 'rt_svc_descs_indices' array, checks that the value in the array is
2762f370465SAntonio Nino Diaz	 * valid, and loads in x15 the pointer to the handler of that service.
2772f370465SAntonio Nino Diaz	 * ---------------------------------------------------------------------
2782f370465SAntonio Nino Diaz	 */
2792f370465SAntonio Nino Diaz	.macro	load_rt_svc_desc_pointer
2802f370465SAntonio Nino Diaz	/* Load descriptor index from array of indices */
2812f370465SAntonio Nino Diaz	adr	x14, rt_svc_descs_indices
2822f370465SAntonio Nino Diaz	ldrb	w15, [x14, x16]
2832f370465SAntonio Nino Diaz
2842f370465SAntonio Nino Diaz#if SMCCC_MAJOR_VERSION == 1
2852f370465SAntonio Nino Diaz	/* Any index greater than 127 is invalid. Check bit 7. */
2862f370465SAntonio Nino Diaz	tbnz	w15, 7, smc_unknown
2872f370465SAntonio Nino Diaz#elif SMCCC_MAJOR_VERSION == 2
2882f370465SAntonio Nino Diaz	/* Verify that the top 3 bits of the loaded index are 0 (w15 <= 31) */
2892f370465SAntonio Nino Diaz	cmp	w15, #31
2902f370465SAntonio Nino Diaz	b.hi	smc_unknown
2912f370465SAntonio Nino Diaz#endif /* SMCCC_MAJOR_VERSION */
2922f370465SAntonio Nino Diaz
2932f370465SAntonio Nino Diaz	/*
2942f370465SAntonio Nino Diaz	 * Get the descriptor using the index
2952f370465SAntonio Nino Diaz	 * x11 = (base + off), w15 = index
2962f370465SAntonio Nino Diaz	 *
2972f370465SAntonio Nino Diaz	 * handler = (base + off) + (index << log2(size))
2982f370465SAntonio Nino Diaz	 */
2992f370465SAntonio Nino Diaz	adr	x11, (__RT_SVC_DESCS_START__ + RT_SVC_DESC_HANDLE)
3002f370465SAntonio Nino Diaz	lsl	w10, w15, #RT_SVC_SIZE_LOG2
3012f370465SAntonio Nino Diaz	ldr	x15, [x11, w10, uxtw]
3022f370465SAntonio Nino Diaz	.endm
3032f370465SAntonio Nino Diaz
3042f370465SAntonio Nino Diaz	/* ---------------------------------------------------------------------
305caa84939SJeenu Viswambharan	 * The following code handles secure monitor calls.
306a6ef4393SDouglas Raillard	 * Depending upon the execution state from where the SMC has been
307a6ef4393SDouglas Raillard	 * invoked, it frees some general purpose registers to perform the
308a6ef4393SDouglas Raillard	 * remaining tasks. They involve finding the runtime service handler
309a6ef4393SDouglas Raillard	 * that is the target of the SMC & switching to runtime stacks (SP_EL0)
310a6ef4393SDouglas Raillard	 * before calling the handler.
311caa84939SJeenu Viswambharan	 *
312a6ef4393SDouglas Raillard	 * Note that x30 has been explicitly saved and can be used here
313a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
314caa84939SJeenu Viswambharan	 */
3150a30cf54SAndrew Thoelkefunc smc_handler
316caa84939SJeenu Viswambharansmc_handler32:
317caa84939SJeenu Viswambharan	/* Check whether aarch32 issued an SMC64 */
318caa84939SJeenu Viswambharan	tbnz	x0, #FUNCID_CC_SHIFT, smc_prohibited
319caa84939SJeenu Viswambharan
320caa84939SJeenu Viswambharansmc_handler64:
321a6ef4393SDouglas Raillard	/*
322a6ef4393SDouglas Raillard	 * Populate the parameters for the SMC handler.
323a6ef4393SDouglas Raillard	 * We already have x0-x4 in place. x5 will point to a cookie (not used
324a6ef4393SDouglas Raillard	 * now). x6 will point to the context structure (SP_EL3) and x7 will
325201ca5b6SDimitris Papastamos	 * contain flags we need to pass to the handler.
326a6ef4393SDouglas Raillard	 *
3272f370465SAntonio Nino Diaz	 * Save x4-x29 and sp_el0.
328caa84939SJeenu Viswambharan	 */
329*ef653d93SJeenu Viswambharan	stp	x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4]
330*ef653d93SJeenu Viswambharan	stp	x6, x7, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X6]
331*ef653d93SJeenu Viswambharan	stp	x8, x9, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X8]
332*ef653d93SJeenu Viswambharan	stp	x10, x11, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X10]
333*ef653d93SJeenu Viswambharan	stp	x12, x13, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X12]
334*ef653d93SJeenu Viswambharan	stp	x14, x15, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X14]
335*ef653d93SJeenu Viswambharan	stp	x16, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X16]
336*ef653d93SJeenu Viswambharan	stp	x18, x19, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X18]
337*ef653d93SJeenu Viswambharan	stp	x20, x21, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X20]
338*ef653d93SJeenu Viswambharan	stp	x22, x23, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X22]
339*ef653d93SJeenu Viswambharan	stp	x24, x25, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X24]
340*ef653d93SJeenu Viswambharan	stp	x26, x27, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X26]
341*ef653d93SJeenu Viswambharan	stp	x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28]
342*ef653d93SJeenu Viswambharan	mrs	x18, sp_el0
343*ef653d93SJeenu Viswambharan	str	x18, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_SP_EL0]
344c3260f9bSSoby Mathew
345caa84939SJeenu Viswambharan	mov	x5, xzr
346caa84939SJeenu Viswambharan	mov	x6, sp
347caa84939SJeenu Viswambharan
3482f370465SAntonio Nino Diaz#if SMCCC_MAJOR_VERSION == 1
3492f370465SAntonio Nino Diaz
350caa84939SJeenu Viswambharan	/* Get the unique owning entity number */
351caa84939SJeenu Viswambharan	ubfx	x16, x0, #FUNCID_OEN_SHIFT, #FUNCID_OEN_WIDTH
352caa84939SJeenu Viswambharan	ubfx	x15, x0, #FUNCID_TYPE_SHIFT, #FUNCID_TYPE_WIDTH
353caa84939SJeenu Viswambharan	orr	x16, x16, x15, lsl #FUNCID_OEN_WIDTH
354caa84939SJeenu Viswambharan
3552f370465SAntonio Nino Diaz	load_rt_svc_desc_pointer
356caa84939SJeenu Viswambharan
3572f370465SAntonio Nino Diaz#elif SMCCC_MAJOR_VERSION == 2
3582f370465SAntonio Nino Diaz
3592f370465SAntonio Nino Diaz	/* Bit 31 must be set */
3602f370465SAntonio Nino Diaz	tbz	x0, #FUNCID_TYPE_SHIFT, smc_unknown
3612f370465SAntonio Nino Diaz
3622f370465SAntonio Nino Diaz	/*
3632f370465SAntonio Nino Diaz	 * Check MSB of namespace to decide between compatibility/vendor and
3642f370465SAntonio Nino Diaz	 * SPCI/SPRT
3652f370465SAntonio Nino Diaz	 */
3662f370465SAntonio Nino Diaz	tbz	x0, #(FUNCID_NAMESPACE_SHIFT + 1), compat_or_vendor
3672f370465SAntonio Nino Diaz
3682f370465SAntonio Nino Diaz	/* Namespaces SPRT and SPCI currently unimplemented */
3692f370465SAntonio Nino Diaz	b	smc_unknown
3702f370465SAntonio Nino Diaz
3712f370465SAntonio Nino Diazcompat_or_vendor:
3722f370465SAntonio Nino Diaz
3732f370465SAntonio Nino Diaz	/* Namespace is b'00 (compatibility) or b'01 (vendor) */
3742f370465SAntonio Nino Diaz
3752f370465SAntonio Nino Diaz	/*
3762f370465SAntonio Nino Diaz	 * Add the LSB of the namespace (bit [28]) to the OEN [27:24] to create
3772f370465SAntonio Nino Diaz	 * a 5-bit index into the rt_svc_descs_indices array.
3782f370465SAntonio Nino Diaz	 *
3792f370465SAntonio Nino Diaz	 * The low 16 entries of the rt_svc_descs_indices array correspond to
3802f370465SAntonio Nino Diaz	 * OENs of the compatibility namespace and the top 16 entries of the
3812f370465SAntonio Nino Diaz	 * array are assigned to the vendor namespace descriptor.
3822f370465SAntonio Nino Diaz	 */
3832f370465SAntonio Nino Diaz	ubfx	x16, x0, #FUNCID_OEN_SHIFT, #(FUNCID_OEN_WIDTH + 1)
3842f370465SAntonio Nino Diaz
3852f370465SAntonio Nino Diaz	load_rt_svc_desc_pointer
3862f370465SAntonio Nino Diaz
3872f370465SAntonio Nino Diaz#endif /* SMCCC_MAJOR_VERSION */
388caa84939SJeenu Viswambharan
389a6ef4393SDouglas Raillard	/*
390a6ef4393SDouglas Raillard	 * Restore the saved C runtime stack value which will become the new
391a6ef4393SDouglas Raillard	 * SP_EL0 i.e. EL3 runtime stack. It was saved in the 'cpu_context'
392a6ef4393SDouglas Raillard	 * structure prior to the last ERET from EL3.
393caa84939SJeenu Viswambharan	 */
394caa84939SJeenu Viswambharan	ldr	x12, [x6, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
395caa84939SJeenu Viswambharan
396caa84939SJeenu Viswambharan	/* Switch to SP_EL0 */
397caa84939SJeenu Viswambharan	msr	spsel, #0
398caa84939SJeenu Viswambharan
399a6ef4393SDouglas Raillard	/*
400a6ef4393SDouglas Raillard	 * Save the SPSR_EL3, ELR_EL3, & SCR_EL3 in case there is a world
401a6ef4393SDouglas Raillard	 * switch during SMC handling.
402a6ef4393SDouglas Raillard	 * TODO: Revisit if all system registers can be saved later.
403caa84939SJeenu Viswambharan	 */
404caa84939SJeenu Viswambharan	mrs	x16, spsr_el3
405caa84939SJeenu Viswambharan	mrs	x17, elr_el3
406caa84939SJeenu Viswambharan	mrs	x18, scr_el3
407caa84939SJeenu Viswambharan	stp	x16, x17, [x6, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
408b51da821SAchin Gupta	str	x18, [x6, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3]
409caa84939SJeenu Viswambharan
410caa84939SJeenu Viswambharan	/* Copy SCR_EL3.NS bit to the flag to indicate caller's security */
411caa84939SJeenu Viswambharan	bfi	x7, x18, #0, #1
412caa84939SJeenu Viswambharan
413caa84939SJeenu Viswambharan	mov	sp, x12
414caa84939SJeenu Viswambharan
415a6ef4393SDouglas Raillard	/*
416a6ef4393SDouglas Raillard	 * Call the Secure Monitor Call handler and then drop directly into
417a6ef4393SDouglas Raillard	 * el3_exit() which will program any remaining architectural state
418a6ef4393SDouglas Raillard	 * prior to issuing the ERET to the desired lower EL.
419caa84939SJeenu Viswambharan	 */
420caa84939SJeenu Viswambharan#if DEBUG
421caa84939SJeenu Viswambharan	cbz	x15, rt_svc_fw_critical_error
422caa84939SJeenu Viswambharan#endif
423caa84939SJeenu Viswambharan	blr	x15
424caa84939SJeenu Viswambharan
425bbf8f6f9SYatharth Kochar	b	el3_exit
4264f6ad66aSAchin Gupta
427caa84939SJeenu Viswambharansmc_unknown:
428caa84939SJeenu Viswambharan	/*
429*ef653d93SJeenu Viswambharan	 * Unknown SMC call. Populate return value with SMC_UNK, restore
430*ef653d93SJeenu Viswambharan	 * GP registers, and return to caller.
431caa84939SJeenu Viswambharan	 */
4324abd7fa7SAntonio Nino Diaz	mov	x0, #SMC_UNK
433*ef653d93SJeenu Viswambharan	str	x0, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X0]
434*ef653d93SJeenu Viswambharan	b	restore_gp_registers_eret
435caa84939SJeenu Viswambharan
436caa84939SJeenu Viswambharansmc_prohibited:
437c3260f9bSSoby Mathew	ldr	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
4384abd7fa7SAntonio Nino Diaz	mov	x0, #SMC_UNK
439caa84939SJeenu Viswambharan	eret
440caa84939SJeenu Viswambharan
441caa84939SJeenu Viswambharanrt_svc_fw_critical_error:
442a6ef4393SDouglas Raillard	/* Switch to SP_ELx */
443a6ef4393SDouglas Raillard	msr	spsel, #1
444a806dad5SJeenu Viswambharan	no_ret	report_unhandled_exception
4458b779620SKévin Petitendfunc smc_handler
446