xref: /rk3399_ARM-atf/lib/el3_runtime/aarch64/context.S (revision 3e61b2b54336510475fbab83b7d97538f3ac5460)
1532ed618SSoby Mathew/*
2d832aee9Sdp-arm * Copyright (c) 2013-2017, ARM Limited and Contributors. All rights reserved.
3532ed618SSoby Mathew *
482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause
5532ed618SSoby Mathew */
6532ed618SSoby Mathew
7532ed618SSoby Mathew#include <arch.h>
8532ed618SSoby Mathew#include <asm_macros.S>
9532ed618SSoby Mathew#include <context.h>
10532ed618SSoby Mathew
11532ed618SSoby Mathew	.global	el1_sysregs_context_save
12d832aee9Sdp-arm	.global el1_sysregs_context_save_post_ops
13532ed618SSoby Mathew	.global	el1_sysregs_context_restore
14532ed618SSoby Mathew#if CTX_INCLUDE_FPREGS
15532ed618SSoby Mathew	.global	fpregs_context_save
16532ed618SSoby Mathew	.global	fpregs_context_restore
17532ed618SSoby Mathew#endif
18532ed618SSoby Mathew	.global	save_gp_registers
19532ed618SSoby Mathew	.global	restore_gp_registers_eret
20532ed618SSoby Mathew	.global	restore_gp_registers_callee_eret
21532ed618SSoby Mathew	.global	el3_exit
22532ed618SSoby Mathew
23532ed618SSoby Mathew/* -----------------------------------------------------
24532ed618SSoby Mathew * The following function strictly follows the AArch64
25532ed618SSoby Mathew * PCS to use x9-x17 (temporary caller-saved registers)
26532ed618SSoby Mathew * to save EL1 system register context. It assumes that
27532ed618SSoby Mathew * 'x0' is pointing to a 'el1_sys_regs' structure where
28532ed618SSoby Mathew * the register context will be saved.
29532ed618SSoby Mathew * -----------------------------------------------------
30532ed618SSoby Mathew */
31532ed618SSoby Mathewfunc el1_sysregs_context_save
32532ed618SSoby Mathew
33532ed618SSoby Mathew	mrs	x9, spsr_el1
34532ed618SSoby Mathew	mrs	x10, elr_el1
35532ed618SSoby Mathew	stp	x9, x10, [x0, #CTX_SPSR_EL1]
36532ed618SSoby Mathew
37532ed618SSoby Mathew	mrs	x15, sctlr_el1
38532ed618SSoby Mathew	mrs	x16, actlr_el1
39532ed618SSoby Mathew	stp	x15, x16, [x0, #CTX_SCTLR_EL1]
40532ed618SSoby Mathew
41532ed618SSoby Mathew	mrs	x17, cpacr_el1
42532ed618SSoby Mathew	mrs	x9, csselr_el1
43532ed618SSoby Mathew	stp	x17, x9, [x0, #CTX_CPACR_EL1]
44532ed618SSoby Mathew
45532ed618SSoby Mathew	mrs	x10, sp_el1
46532ed618SSoby Mathew	mrs	x11, esr_el1
47532ed618SSoby Mathew	stp	x10, x11, [x0, #CTX_SP_EL1]
48532ed618SSoby Mathew
49532ed618SSoby Mathew	mrs	x12, ttbr0_el1
50532ed618SSoby Mathew	mrs	x13, ttbr1_el1
51532ed618SSoby Mathew	stp	x12, x13, [x0, #CTX_TTBR0_EL1]
52532ed618SSoby Mathew
53532ed618SSoby Mathew	mrs	x14, mair_el1
54532ed618SSoby Mathew	mrs	x15, amair_el1
55532ed618SSoby Mathew	stp	x14, x15, [x0, #CTX_MAIR_EL1]
56532ed618SSoby Mathew
57532ed618SSoby Mathew	mrs	x16, tcr_el1
58532ed618SSoby Mathew	mrs	x17, tpidr_el1
59532ed618SSoby Mathew	stp	x16, x17, [x0, #CTX_TCR_EL1]
60532ed618SSoby Mathew
61532ed618SSoby Mathew	mrs	x9, tpidr_el0
62532ed618SSoby Mathew	mrs	x10, tpidrro_el0
63532ed618SSoby Mathew	stp	x9, x10, [x0, #CTX_TPIDR_EL0]
64532ed618SSoby Mathew
65532ed618SSoby Mathew	mrs	x13, par_el1
66532ed618SSoby Mathew	mrs	x14, far_el1
67532ed618SSoby Mathew	stp	x13, x14, [x0, #CTX_PAR_EL1]
68532ed618SSoby Mathew
69532ed618SSoby Mathew	mrs	x15, afsr0_el1
70532ed618SSoby Mathew	mrs	x16, afsr1_el1
71532ed618SSoby Mathew	stp	x15, x16, [x0, #CTX_AFSR0_EL1]
72532ed618SSoby Mathew
73532ed618SSoby Mathew	mrs	x17, contextidr_el1
74532ed618SSoby Mathew	mrs	x9, vbar_el1
75532ed618SSoby Mathew	stp	x17, x9, [x0, #CTX_CONTEXTIDR_EL1]
76532ed618SSoby Mathew
77*3e61b2b5SDavid Cunado	mrs	x10, pmcr_el0
78*3e61b2b5SDavid Cunado	str	x10, [x0, #CTX_PMCR_EL0]
79*3e61b2b5SDavid Cunado
80532ed618SSoby Mathew	/* Save AArch32 system registers if the build has instructed so */
81532ed618SSoby Mathew#if CTX_INCLUDE_AARCH32_REGS
82532ed618SSoby Mathew	mrs	x11, spsr_abt
83532ed618SSoby Mathew	mrs	x12, spsr_und
84532ed618SSoby Mathew	stp	x11, x12, [x0, #CTX_SPSR_ABT]
85532ed618SSoby Mathew
86532ed618SSoby Mathew	mrs	x13, spsr_irq
87532ed618SSoby Mathew	mrs	x14, spsr_fiq
88532ed618SSoby Mathew	stp	x13, x14, [x0, #CTX_SPSR_IRQ]
89532ed618SSoby Mathew
90532ed618SSoby Mathew	mrs	x15, dacr32_el2
91532ed618SSoby Mathew	mrs	x16, ifsr32_el2
92532ed618SSoby Mathew	stp	x15, x16, [x0, #CTX_DACR32_EL2]
93532ed618SSoby Mathew
94532ed618SSoby Mathew	mrs	x17, fpexc32_el2
95532ed618SSoby Mathew	str	x17, [x0, #CTX_FP_FPEXC32_EL2]
96532ed618SSoby Mathew#endif
97532ed618SSoby Mathew
98532ed618SSoby Mathew	/* Save NS timer registers if the build has instructed so */
99532ed618SSoby Mathew#if NS_TIMER_SWITCH
100532ed618SSoby Mathew	mrs	x10, cntp_ctl_el0
101532ed618SSoby Mathew	mrs	x11, cntp_cval_el0
102532ed618SSoby Mathew	stp	x10, x11, [x0, #CTX_CNTP_CTL_EL0]
103532ed618SSoby Mathew
104532ed618SSoby Mathew	mrs	x12, cntv_ctl_el0
105532ed618SSoby Mathew	mrs	x13, cntv_cval_el0
106532ed618SSoby Mathew	stp	x12, x13, [x0, #CTX_CNTV_CTL_EL0]
107532ed618SSoby Mathew
108532ed618SSoby Mathew	mrs	x14, cntkctl_el1
109532ed618SSoby Mathew	str	x14, [x0, #CTX_CNTKCTL_EL1]
110532ed618SSoby Mathew#endif
111532ed618SSoby Mathew
112532ed618SSoby Mathew	ret
113532ed618SSoby Mathewendfunc el1_sysregs_context_save
114532ed618SSoby Mathew
115532ed618SSoby Mathew/* -----------------------------------------------------
116532ed618SSoby Mathew * The following function strictly follows the AArch64
117532ed618SSoby Mathew * PCS to use x9-x17 (temporary caller-saved registers)
118d832aee9Sdp-arm * to do post operations after saving the EL1 system
119d832aee9Sdp-arm * register context.
120d832aee9Sdp-arm * -----------------------------------------------------
121d832aee9Sdp-arm */
122d832aee9Sdp-armfunc el1_sysregs_context_save_post_ops
123d832aee9Sdp-arm#if ENABLE_SPE_FOR_LOWER_ELS
124d832aee9Sdp-arm	/* Detect if SPE is implemented */
125d832aee9Sdp-arm	mrs	x9, id_aa64dfr0_el1
126d832aee9Sdp-arm	ubfx	x9, x9, #ID_AA64DFR0_PMS_SHIFT, #ID_AA64DFR0_PMS_LENGTH
127d832aee9Sdp-arm	cmp	x9, #0x1
128d832aee9Sdp-arm	b.ne	1f
129d832aee9Sdp-arm
130d832aee9Sdp-arm	/*
131d832aee9Sdp-arm	 * Before switching from normal world to secure world
132d832aee9Sdp-arm	 * the profiling buffers need to be drained out to memory.  This is
133d832aee9Sdp-arm	 * required to avoid an invalid memory access when TTBR is switched
134d832aee9Sdp-arm	 * for entry to SEL1.
135d832aee9Sdp-arm	 */
136d832aee9Sdp-arm	.arch	armv8.2-a+profile
137d832aee9Sdp-arm	psb	csync
138d832aee9Sdp-arm	dsb	nsh
139d832aee9Sdp-arm	.arch	armv8-a
140d832aee9Sdp-arm1:
141d832aee9Sdp-arm#endif
142d832aee9Sdp-arm	ret
143d832aee9Sdp-armendfunc el1_sysregs_context_save_post_ops
144d832aee9Sdp-arm
145d832aee9Sdp-arm/* -----------------------------------------------------
146d832aee9Sdp-arm * The following function strictly follows the AArch64
147d832aee9Sdp-arm * PCS to use x9-x17 (temporary caller-saved registers)
148532ed618SSoby Mathew * to restore EL1 system register context.  It assumes
149532ed618SSoby Mathew * that 'x0' is pointing to a 'el1_sys_regs' structure
150532ed618SSoby Mathew * from where the register context will be restored
151532ed618SSoby Mathew * -----------------------------------------------------
152532ed618SSoby Mathew */
153532ed618SSoby Mathewfunc el1_sysregs_context_restore
154532ed618SSoby Mathew
155532ed618SSoby Mathew	ldp	x9, x10, [x0, #CTX_SPSR_EL1]
156532ed618SSoby Mathew	msr	spsr_el1, x9
157532ed618SSoby Mathew	msr	elr_el1, x10
158532ed618SSoby Mathew
159532ed618SSoby Mathew	ldp	x15, x16, [x0, #CTX_SCTLR_EL1]
160532ed618SSoby Mathew	msr	sctlr_el1, x15
161532ed618SSoby Mathew	msr	actlr_el1, x16
162532ed618SSoby Mathew
163532ed618SSoby Mathew	ldp	x17, x9, [x0, #CTX_CPACR_EL1]
164532ed618SSoby Mathew	msr	cpacr_el1, x17
165532ed618SSoby Mathew	msr	csselr_el1, x9
166532ed618SSoby Mathew
167532ed618SSoby Mathew	ldp	x10, x11, [x0, #CTX_SP_EL1]
168532ed618SSoby Mathew	msr	sp_el1, x10
169532ed618SSoby Mathew	msr	esr_el1, x11
170532ed618SSoby Mathew
171532ed618SSoby Mathew	ldp	x12, x13, [x0, #CTX_TTBR0_EL1]
172532ed618SSoby Mathew	msr	ttbr0_el1, x12
173532ed618SSoby Mathew	msr	ttbr1_el1, x13
174532ed618SSoby Mathew
175532ed618SSoby Mathew	ldp	x14, x15, [x0, #CTX_MAIR_EL1]
176532ed618SSoby Mathew	msr	mair_el1, x14
177532ed618SSoby Mathew	msr	amair_el1, x15
178532ed618SSoby Mathew
179532ed618SSoby Mathew	ldp	x16, x17, [x0, #CTX_TCR_EL1]
180532ed618SSoby Mathew	msr	tcr_el1, x16
181532ed618SSoby Mathew	msr	tpidr_el1, x17
182532ed618SSoby Mathew
183532ed618SSoby Mathew	ldp	x9, x10, [x0, #CTX_TPIDR_EL0]
184532ed618SSoby Mathew	msr	tpidr_el0, x9
185532ed618SSoby Mathew	msr	tpidrro_el0, x10
186532ed618SSoby Mathew
187532ed618SSoby Mathew	ldp	x13, x14, [x0, #CTX_PAR_EL1]
188532ed618SSoby Mathew	msr	par_el1, x13
189532ed618SSoby Mathew	msr	far_el1, x14
190532ed618SSoby Mathew
191532ed618SSoby Mathew	ldp	x15, x16, [x0, #CTX_AFSR0_EL1]
192532ed618SSoby Mathew	msr	afsr0_el1, x15
193532ed618SSoby Mathew	msr	afsr1_el1, x16
194532ed618SSoby Mathew
195532ed618SSoby Mathew	ldp	x17, x9, [x0, #CTX_CONTEXTIDR_EL1]
196532ed618SSoby Mathew	msr	contextidr_el1, x17
197532ed618SSoby Mathew	msr	vbar_el1, x9
198532ed618SSoby Mathew
199*3e61b2b5SDavid Cunado	ldr	x10, [x0, #CTX_PMCR_EL0]
200*3e61b2b5SDavid Cunado	msr	pmcr_el0, x10
201*3e61b2b5SDavid Cunado
202532ed618SSoby Mathew	/* Restore AArch32 system registers if the build has instructed so */
203532ed618SSoby Mathew#if CTX_INCLUDE_AARCH32_REGS
204532ed618SSoby Mathew	ldp	x11, x12, [x0, #CTX_SPSR_ABT]
205532ed618SSoby Mathew	msr	spsr_abt, x11
206532ed618SSoby Mathew	msr	spsr_und, x12
207532ed618SSoby Mathew
208532ed618SSoby Mathew	ldp	x13, x14, [x0, #CTX_SPSR_IRQ]
209532ed618SSoby Mathew	msr	spsr_irq, x13
210532ed618SSoby Mathew	msr	spsr_fiq, x14
211532ed618SSoby Mathew
212532ed618SSoby Mathew	ldp	x15, x16, [x0, #CTX_DACR32_EL2]
213532ed618SSoby Mathew	msr	dacr32_el2, x15
214532ed618SSoby Mathew	msr	ifsr32_el2, x16
215532ed618SSoby Mathew
216532ed618SSoby Mathew	ldr	x17, [x0, #CTX_FP_FPEXC32_EL2]
217532ed618SSoby Mathew	msr	fpexc32_el2, x17
218532ed618SSoby Mathew#endif
219532ed618SSoby Mathew	/* Restore NS timer registers if the build has instructed so */
220532ed618SSoby Mathew#if NS_TIMER_SWITCH
221532ed618SSoby Mathew	ldp	x10, x11, [x0, #CTX_CNTP_CTL_EL0]
222532ed618SSoby Mathew	msr	cntp_ctl_el0, x10
223532ed618SSoby Mathew	msr	cntp_cval_el0, x11
224532ed618SSoby Mathew
225532ed618SSoby Mathew	ldp	x12, x13, [x0, #CTX_CNTV_CTL_EL0]
226532ed618SSoby Mathew	msr	cntv_ctl_el0, x12
227532ed618SSoby Mathew	msr	cntv_cval_el0, x13
228532ed618SSoby Mathew
229532ed618SSoby Mathew	ldr	x14, [x0, #CTX_CNTKCTL_EL1]
230532ed618SSoby Mathew	msr	cntkctl_el1, x14
231532ed618SSoby Mathew#endif
232532ed618SSoby Mathew
233532ed618SSoby Mathew	/* No explict ISB required here as ERET covers it */
234532ed618SSoby Mathew	ret
235532ed618SSoby Mathewendfunc el1_sysregs_context_restore
236532ed618SSoby Mathew
237532ed618SSoby Mathew/* -----------------------------------------------------
238532ed618SSoby Mathew * The following function follows the aapcs_64 strictly
239532ed618SSoby Mathew * to use x9-x17 (temporary caller-saved registers
240532ed618SSoby Mathew * according to AArch64 PCS) to save floating point
241532ed618SSoby Mathew * register context. It assumes that 'x0' is pointing to
242532ed618SSoby Mathew * a 'fp_regs' structure where the register context will
243532ed618SSoby Mathew * be saved.
244532ed618SSoby Mathew *
245532ed618SSoby Mathew * Access to VFP registers will trap if CPTR_EL3.TFP is
246532ed618SSoby Mathew * set.  However currently we don't use VFP registers
247532ed618SSoby Mathew * nor set traps in Trusted Firmware, and assume it's
248532ed618SSoby Mathew * cleared
249532ed618SSoby Mathew *
250532ed618SSoby Mathew * TODO: Revisit when VFP is used in secure world
251532ed618SSoby Mathew * -----------------------------------------------------
252532ed618SSoby Mathew */
253532ed618SSoby Mathew#if CTX_INCLUDE_FPREGS
254532ed618SSoby Mathewfunc fpregs_context_save
255532ed618SSoby Mathew	stp	q0, q1, [x0, #CTX_FP_Q0]
256532ed618SSoby Mathew	stp	q2, q3, [x0, #CTX_FP_Q2]
257532ed618SSoby Mathew	stp	q4, q5, [x0, #CTX_FP_Q4]
258532ed618SSoby Mathew	stp	q6, q7, [x0, #CTX_FP_Q6]
259532ed618SSoby Mathew	stp	q8, q9, [x0, #CTX_FP_Q8]
260532ed618SSoby Mathew	stp	q10, q11, [x0, #CTX_FP_Q10]
261532ed618SSoby Mathew	stp	q12, q13, [x0, #CTX_FP_Q12]
262532ed618SSoby Mathew	stp	q14, q15, [x0, #CTX_FP_Q14]
263532ed618SSoby Mathew	stp	q16, q17, [x0, #CTX_FP_Q16]
264532ed618SSoby Mathew	stp	q18, q19, [x0, #CTX_FP_Q18]
265532ed618SSoby Mathew	stp	q20, q21, [x0, #CTX_FP_Q20]
266532ed618SSoby Mathew	stp	q22, q23, [x0, #CTX_FP_Q22]
267532ed618SSoby Mathew	stp	q24, q25, [x0, #CTX_FP_Q24]
268532ed618SSoby Mathew	stp	q26, q27, [x0, #CTX_FP_Q26]
269532ed618SSoby Mathew	stp	q28, q29, [x0, #CTX_FP_Q28]
270532ed618SSoby Mathew	stp	q30, q31, [x0, #CTX_FP_Q30]
271532ed618SSoby Mathew
272532ed618SSoby Mathew	mrs	x9, fpsr
273532ed618SSoby Mathew	str	x9, [x0, #CTX_FP_FPSR]
274532ed618SSoby Mathew
275532ed618SSoby Mathew	mrs	x10, fpcr
276532ed618SSoby Mathew	str	x10, [x0, #CTX_FP_FPCR]
277532ed618SSoby Mathew
278532ed618SSoby Mathew	ret
279532ed618SSoby Mathewendfunc fpregs_context_save
280532ed618SSoby Mathew
281532ed618SSoby Mathew/* -----------------------------------------------------
282532ed618SSoby Mathew * The following function follows the aapcs_64 strictly
283532ed618SSoby Mathew * to use x9-x17 (temporary caller-saved registers
284532ed618SSoby Mathew * according to AArch64 PCS) to restore floating point
285532ed618SSoby Mathew * register context. It assumes that 'x0' is pointing to
286532ed618SSoby Mathew * a 'fp_regs' structure from where the register context
287532ed618SSoby Mathew * will be restored.
288532ed618SSoby Mathew *
289532ed618SSoby Mathew * Access to VFP registers will trap if CPTR_EL3.TFP is
290532ed618SSoby Mathew * set.  However currently we don't use VFP registers
291532ed618SSoby Mathew * nor set traps in Trusted Firmware, and assume it's
292532ed618SSoby Mathew * cleared
293532ed618SSoby Mathew *
294532ed618SSoby Mathew * TODO: Revisit when VFP is used in secure world
295532ed618SSoby Mathew * -----------------------------------------------------
296532ed618SSoby Mathew */
297532ed618SSoby Mathewfunc fpregs_context_restore
298532ed618SSoby Mathew	ldp	q0, q1, [x0, #CTX_FP_Q0]
299532ed618SSoby Mathew	ldp	q2, q3, [x0, #CTX_FP_Q2]
300532ed618SSoby Mathew	ldp	q4, q5, [x0, #CTX_FP_Q4]
301532ed618SSoby Mathew	ldp	q6, q7, [x0, #CTX_FP_Q6]
302532ed618SSoby Mathew	ldp	q8, q9, [x0, #CTX_FP_Q8]
303532ed618SSoby Mathew	ldp	q10, q11, [x0, #CTX_FP_Q10]
304532ed618SSoby Mathew	ldp	q12, q13, [x0, #CTX_FP_Q12]
305532ed618SSoby Mathew	ldp	q14, q15, [x0, #CTX_FP_Q14]
306532ed618SSoby Mathew	ldp	q16, q17, [x0, #CTX_FP_Q16]
307532ed618SSoby Mathew	ldp	q18, q19, [x0, #CTX_FP_Q18]
308532ed618SSoby Mathew	ldp	q20, q21, [x0, #CTX_FP_Q20]
309532ed618SSoby Mathew	ldp	q22, q23, [x0, #CTX_FP_Q22]
310532ed618SSoby Mathew	ldp	q24, q25, [x0, #CTX_FP_Q24]
311532ed618SSoby Mathew	ldp	q26, q27, [x0, #CTX_FP_Q26]
312532ed618SSoby Mathew	ldp	q28, q29, [x0, #CTX_FP_Q28]
313532ed618SSoby Mathew	ldp	q30, q31, [x0, #CTX_FP_Q30]
314532ed618SSoby Mathew
315532ed618SSoby Mathew	ldr	x9, [x0, #CTX_FP_FPSR]
316532ed618SSoby Mathew	msr	fpsr, x9
317532ed618SSoby Mathew
318532ed618SSoby Mathew	ldr	x10, [x0, #CTX_FP_FPCR]
319532ed618SSoby Mathew	msr	fpcr, x10
320532ed618SSoby Mathew
321532ed618SSoby Mathew	/*
322532ed618SSoby Mathew	 * No explict ISB required here as ERET to
323532ed618SSoby Mathew	 * switch to secure EL1 or non-secure world
324532ed618SSoby Mathew	 * covers it
325532ed618SSoby Mathew	 */
326532ed618SSoby Mathew
327532ed618SSoby Mathew	ret
328532ed618SSoby Mathewendfunc fpregs_context_restore
329532ed618SSoby Mathew#endif /* CTX_INCLUDE_FPREGS */
330532ed618SSoby Mathew
331532ed618SSoby Mathew/* -----------------------------------------------------
332532ed618SSoby Mathew * The following functions are used to save and restore
333532ed618SSoby Mathew * all the general purpose registers. Ideally we would
334532ed618SSoby Mathew * only save and restore the callee saved registers when
335532ed618SSoby Mathew * a world switch occurs but that type of implementation
336532ed618SSoby Mathew * is more complex. So currently we will always save and
337532ed618SSoby Mathew * restore these registers on entry and exit of EL3.
338532ed618SSoby Mathew * These are not macros to ensure their invocation fits
339532ed618SSoby Mathew * within the 32 instructions per exception vector.
340532ed618SSoby Mathew * clobbers: x18
341532ed618SSoby Mathew * -----------------------------------------------------
342532ed618SSoby Mathew */
343532ed618SSoby Mathewfunc save_gp_registers
344532ed618SSoby Mathew	stp	x0, x1, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X0]
345532ed618SSoby Mathew	stp	x2, x3, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X2]
346532ed618SSoby Mathew	stp	x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4]
347532ed618SSoby Mathew	stp	x6, x7, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X6]
348532ed618SSoby Mathew	stp	x8, x9, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X8]
349532ed618SSoby Mathew	stp	x10, x11, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X10]
350532ed618SSoby Mathew	stp	x12, x13, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X12]
351532ed618SSoby Mathew	stp	x14, x15, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X14]
352532ed618SSoby Mathew	stp	x16, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X16]
353532ed618SSoby Mathew	stp	x18, x19, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X18]
354532ed618SSoby Mathew	stp	x20, x21, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X20]
355532ed618SSoby Mathew	stp	x22, x23, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X22]
356532ed618SSoby Mathew	stp	x24, x25, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X24]
357532ed618SSoby Mathew	stp	x26, x27, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X26]
358532ed618SSoby Mathew	stp	x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28]
359532ed618SSoby Mathew	mrs	x18, sp_el0
360532ed618SSoby Mathew	str	x18, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_SP_EL0]
361532ed618SSoby Mathew	ret
362532ed618SSoby Mathewendfunc save_gp_registers
363532ed618SSoby Mathew
364532ed618SSoby Mathewfunc restore_gp_registers_eret
365532ed618SSoby Mathew	ldp	x0, x1, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X0]
366532ed618SSoby Mathew	ldp	x2, x3, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X2]
367532ed618SSoby Mathew	b	restore_gp_registers_callee_eret
368532ed618SSoby Mathewendfunc restore_gp_registers_eret
369532ed618SSoby Mathew
370532ed618SSoby Mathewfunc restore_gp_registers_callee_eret
371532ed618SSoby Mathew	ldp	x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4]
372532ed618SSoby Mathew	ldp	x6, x7, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X6]
373532ed618SSoby Mathew	ldp	x8, x9, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X8]
374532ed618SSoby Mathew	ldp	x10, x11, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X10]
375532ed618SSoby Mathew	ldp	x12, x13, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X12]
376532ed618SSoby Mathew	ldp	x14, x15, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X14]
377532ed618SSoby Mathew	ldp	x18, x19, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X18]
378532ed618SSoby Mathew	ldp	x20, x21, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X20]
379532ed618SSoby Mathew	ldp	x22, x23, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X22]
380532ed618SSoby Mathew	ldp	x24, x25, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X24]
381532ed618SSoby Mathew	ldp	x26, x27, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X26]
382532ed618SSoby Mathew	ldp	x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28]
383532ed618SSoby Mathew	ldp	x30, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
384532ed618SSoby Mathew	msr	sp_el0, x17
385532ed618SSoby Mathew	ldp	x16, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X16]
386532ed618SSoby Mathew	eret
387532ed618SSoby Mathewendfunc	restore_gp_registers_callee_eret
388532ed618SSoby Mathew
389532ed618SSoby Mathew	/* -----------------------------------------------------
390532ed618SSoby Mathew	 * This routine assumes that the SP_EL3 is pointing to
391532ed618SSoby Mathew	 * a valid context structure from where the gp regs and
392532ed618SSoby Mathew	 * other special registers can be retrieved.
393532ed618SSoby Mathew	 * -----------------------------------------------------
394532ed618SSoby Mathew	 */
395532ed618SSoby Mathewfunc el3_exit
396532ed618SSoby Mathew	/* -----------------------------------------------------
397532ed618SSoby Mathew	 * Save the current SP_EL0 i.e. the EL3 runtime stack
398532ed618SSoby Mathew	 * which will be used for handling the next SMC. Then
399532ed618SSoby Mathew	 * switch to SP_EL3
400532ed618SSoby Mathew	 * -----------------------------------------------------
401532ed618SSoby Mathew	 */
402532ed618SSoby Mathew	mov	x17, sp
403532ed618SSoby Mathew	msr	spsel, #1
404532ed618SSoby Mathew	str	x17, [sp, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
405532ed618SSoby Mathew
406532ed618SSoby Mathew	/* -----------------------------------------------------
407532ed618SSoby Mathew	 * Restore SPSR_EL3, ELR_EL3 and SCR_EL3 prior to ERET
408532ed618SSoby Mathew	 * -----------------------------------------------------
409532ed618SSoby Mathew	 */
410532ed618SSoby Mathew	ldr	x18, [sp, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3]
411532ed618SSoby Mathew	ldp	x16, x17, [sp, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
412532ed618SSoby Mathew	msr	scr_el3, x18
413532ed618SSoby Mathew	msr	spsr_el3, x16
414532ed618SSoby Mathew	msr	elr_el3, x17
415532ed618SSoby Mathew
416532ed618SSoby Mathew	/* Restore saved general purpose registers and return */
417532ed618SSoby Mathew	b	restore_gp_registers_eret
418532ed618SSoby Mathewendfunc el3_exit
419