xref: /rk3399_ARM-atf/lib/el3_runtime/aarch64/context.S (revision 5283962ebaf77850d68bb457608ede5174e43159)
1532ed618SSoby Mathew/*
24d1ccf0eSAntonio Nino Diaz * Copyright (c) 2013-2019, 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
12532ed618SSoby Mathew	.global	el1_sysregs_context_restore
13532ed618SSoby Mathew#if CTX_INCLUDE_FPREGS
14532ed618SSoby Mathew	.global	fpregs_context_save
15532ed618SSoby Mathew	.global	fpregs_context_restore
16532ed618SSoby Mathew#endif
17*5283962eSAntonio Nino Diaz#if CTX_INCLUDE_PAUTH_REGS
18*5283962eSAntonio Nino Diaz	.global	pauth_context_restore
19*5283962eSAntonio Nino Diaz	.global	pauth_context_save
20*5283962eSAntonio Nino Diaz#endif
21532ed618SSoby Mathew	.global	save_gp_registers
22ef653d93SJeenu Viswambharan	.global	restore_gp_registers
23532ed618SSoby Mathew	.global	restore_gp_registers_eret
24532ed618SSoby Mathew	.global	el3_exit
25532ed618SSoby Mathew
26532ed618SSoby Mathew/* -----------------------------------------------------
27532ed618SSoby Mathew * The following function strictly follows the AArch64
28532ed618SSoby Mathew * PCS to use x9-x17 (temporary caller-saved registers)
29532ed618SSoby Mathew * to save EL1 system register context. It assumes that
30532ed618SSoby Mathew * 'x0' is pointing to a 'el1_sys_regs' structure where
31532ed618SSoby Mathew * the register context will be saved.
32532ed618SSoby Mathew * -----------------------------------------------------
33532ed618SSoby Mathew */
34532ed618SSoby Mathewfunc el1_sysregs_context_save
35532ed618SSoby Mathew
36532ed618SSoby Mathew	mrs	x9, spsr_el1
37532ed618SSoby Mathew	mrs	x10, elr_el1
38532ed618SSoby Mathew	stp	x9, x10, [x0, #CTX_SPSR_EL1]
39532ed618SSoby Mathew
40532ed618SSoby Mathew	mrs	x15, sctlr_el1
41532ed618SSoby Mathew	mrs	x16, actlr_el1
42532ed618SSoby Mathew	stp	x15, x16, [x0, #CTX_SCTLR_EL1]
43532ed618SSoby Mathew
44532ed618SSoby Mathew	mrs	x17, cpacr_el1
45532ed618SSoby Mathew	mrs	x9, csselr_el1
46532ed618SSoby Mathew	stp	x17, x9, [x0, #CTX_CPACR_EL1]
47532ed618SSoby Mathew
48532ed618SSoby Mathew	mrs	x10, sp_el1
49532ed618SSoby Mathew	mrs	x11, esr_el1
50532ed618SSoby Mathew	stp	x10, x11, [x0, #CTX_SP_EL1]
51532ed618SSoby Mathew
52532ed618SSoby Mathew	mrs	x12, ttbr0_el1
53532ed618SSoby Mathew	mrs	x13, ttbr1_el1
54532ed618SSoby Mathew	stp	x12, x13, [x0, #CTX_TTBR0_EL1]
55532ed618SSoby Mathew
56532ed618SSoby Mathew	mrs	x14, mair_el1
57532ed618SSoby Mathew	mrs	x15, amair_el1
58532ed618SSoby Mathew	stp	x14, x15, [x0, #CTX_MAIR_EL1]
59532ed618SSoby Mathew
60532ed618SSoby Mathew	mrs	x16, tcr_el1
61532ed618SSoby Mathew	mrs	x17, tpidr_el1
62532ed618SSoby Mathew	stp	x16, x17, [x0, #CTX_TCR_EL1]
63532ed618SSoby Mathew
64532ed618SSoby Mathew	mrs	x9, tpidr_el0
65532ed618SSoby Mathew	mrs	x10, tpidrro_el0
66532ed618SSoby Mathew	stp	x9, x10, [x0, #CTX_TPIDR_EL0]
67532ed618SSoby Mathew
68532ed618SSoby Mathew	mrs	x13, par_el1
69532ed618SSoby Mathew	mrs	x14, far_el1
70532ed618SSoby Mathew	stp	x13, x14, [x0, #CTX_PAR_EL1]
71532ed618SSoby Mathew
72532ed618SSoby Mathew	mrs	x15, afsr0_el1
73532ed618SSoby Mathew	mrs	x16, afsr1_el1
74532ed618SSoby Mathew	stp	x15, x16, [x0, #CTX_AFSR0_EL1]
75532ed618SSoby Mathew
76532ed618SSoby Mathew	mrs	x17, contextidr_el1
77532ed618SSoby Mathew	mrs	x9, vbar_el1
78532ed618SSoby Mathew	stp	x17, x9, [x0, #CTX_CONTEXTIDR_EL1]
79532ed618SSoby Mathew
803e61b2b5SDavid Cunado	mrs	x10, pmcr_el0
813e61b2b5SDavid Cunado	str	x10, [x0, #CTX_PMCR_EL0]
823e61b2b5SDavid Cunado
83532ed618SSoby Mathew	/* Save AArch32 system registers if the build has instructed so */
84532ed618SSoby Mathew#if CTX_INCLUDE_AARCH32_REGS
85532ed618SSoby Mathew	mrs	x11, spsr_abt
86532ed618SSoby Mathew	mrs	x12, spsr_und
87532ed618SSoby Mathew	stp	x11, x12, [x0, #CTX_SPSR_ABT]
88532ed618SSoby Mathew
89532ed618SSoby Mathew	mrs	x13, spsr_irq
90532ed618SSoby Mathew	mrs	x14, spsr_fiq
91532ed618SSoby Mathew	stp	x13, x14, [x0, #CTX_SPSR_IRQ]
92532ed618SSoby Mathew
93532ed618SSoby Mathew	mrs	x15, dacr32_el2
94532ed618SSoby Mathew	mrs	x16, ifsr32_el2
95532ed618SSoby Mathew	stp	x15, x16, [x0, #CTX_DACR32_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)
118532ed618SSoby Mathew * to restore EL1 system register context.  It assumes
119532ed618SSoby Mathew * that 'x0' is pointing to a 'el1_sys_regs' structure
120532ed618SSoby Mathew * from where the register context will be restored
121532ed618SSoby Mathew * -----------------------------------------------------
122532ed618SSoby Mathew */
123532ed618SSoby Mathewfunc el1_sysregs_context_restore
124532ed618SSoby Mathew
125532ed618SSoby Mathew	ldp	x9, x10, [x0, #CTX_SPSR_EL1]
126532ed618SSoby Mathew	msr	spsr_el1, x9
127532ed618SSoby Mathew	msr	elr_el1, x10
128532ed618SSoby Mathew
129532ed618SSoby Mathew	ldp	x15, x16, [x0, #CTX_SCTLR_EL1]
130532ed618SSoby Mathew	msr	sctlr_el1, x15
131532ed618SSoby Mathew	msr	actlr_el1, x16
132532ed618SSoby Mathew
133532ed618SSoby Mathew	ldp	x17, x9, [x0, #CTX_CPACR_EL1]
134532ed618SSoby Mathew	msr	cpacr_el1, x17
135532ed618SSoby Mathew	msr	csselr_el1, x9
136532ed618SSoby Mathew
137532ed618SSoby Mathew	ldp	x10, x11, [x0, #CTX_SP_EL1]
138532ed618SSoby Mathew	msr	sp_el1, x10
139532ed618SSoby Mathew	msr	esr_el1, x11
140532ed618SSoby Mathew
141532ed618SSoby Mathew	ldp	x12, x13, [x0, #CTX_TTBR0_EL1]
142532ed618SSoby Mathew	msr	ttbr0_el1, x12
143532ed618SSoby Mathew	msr	ttbr1_el1, x13
144532ed618SSoby Mathew
145532ed618SSoby Mathew	ldp	x14, x15, [x0, #CTX_MAIR_EL1]
146532ed618SSoby Mathew	msr	mair_el1, x14
147532ed618SSoby Mathew	msr	amair_el1, x15
148532ed618SSoby Mathew
149532ed618SSoby Mathew	ldp	x16, x17, [x0, #CTX_TCR_EL1]
150532ed618SSoby Mathew	msr	tcr_el1, x16
151532ed618SSoby Mathew	msr	tpidr_el1, x17
152532ed618SSoby Mathew
153532ed618SSoby Mathew	ldp	x9, x10, [x0, #CTX_TPIDR_EL0]
154532ed618SSoby Mathew	msr	tpidr_el0, x9
155532ed618SSoby Mathew	msr	tpidrro_el0, x10
156532ed618SSoby Mathew
157532ed618SSoby Mathew	ldp	x13, x14, [x0, #CTX_PAR_EL1]
158532ed618SSoby Mathew	msr	par_el1, x13
159532ed618SSoby Mathew	msr	far_el1, x14
160532ed618SSoby Mathew
161532ed618SSoby Mathew	ldp	x15, x16, [x0, #CTX_AFSR0_EL1]
162532ed618SSoby Mathew	msr	afsr0_el1, x15
163532ed618SSoby Mathew	msr	afsr1_el1, x16
164532ed618SSoby Mathew
165532ed618SSoby Mathew	ldp	x17, x9, [x0, #CTX_CONTEXTIDR_EL1]
166532ed618SSoby Mathew	msr	contextidr_el1, x17
167532ed618SSoby Mathew	msr	vbar_el1, x9
168532ed618SSoby Mathew
1693e61b2b5SDavid Cunado	ldr	x10, [x0, #CTX_PMCR_EL0]
1703e61b2b5SDavid Cunado	msr	pmcr_el0, x10
1713e61b2b5SDavid Cunado
172532ed618SSoby Mathew	/* Restore AArch32 system registers if the build has instructed so */
173532ed618SSoby Mathew#if CTX_INCLUDE_AARCH32_REGS
174532ed618SSoby Mathew	ldp	x11, x12, [x0, #CTX_SPSR_ABT]
175532ed618SSoby Mathew	msr	spsr_abt, x11
176532ed618SSoby Mathew	msr	spsr_und, x12
177532ed618SSoby Mathew
178532ed618SSoby Mathew	ldp	x13, x14, [x0, #CTX_SPSR_IRQ]
179532ed618SSoby Mathew	msr	spsr_irq, x13
180532ed618SSoby Mathew	msr	spsr_fiq, x14
181532ed618SSoby Mathew
182532ed618SSoby Mathew	ldp	x15, x16, [x0, #CTX_DACR32_EL2]
183532ed618SSoby Mathew	msr	dacr32_el2, x15
184532ed618SSoby Mathew	msr	ifsr32_el2, x16
185532ed618SSoby Mathew#endif
186532ed618SSoby Mathew	/* Restore NS timer registers if the build has instructed so */
187532ed618SSoby Mathew#if NS_TIMER_SWITCH
188532ed618SSoby Mathew	ldp	x10, x11, [x0, #CTX_CNTP_CTL_EL0]
189532ed618SSoby Mathew	msr	cntp_ctl_el0, x10
190532ed618SSoby Mathew	msr	cntp_cval_el0, x11
191532ed618SSoby Mathew
192532ed618SSoby Mathew	ldp	x12, x13, [x0, #CTX_CNTV_CTL_EL0]
193532ed618SSoby Mathew	msr	cntv_ctl_el0, x12
194532ed618SSoby Mathew	msr	cntv_cval_el0, x13
195532ed618SSoby Mathew
196532ed618SSoby Mathew	ldr	x14, [x0, #CTX_CNTKCTL_EL1]
197532ed618SSoby Mathew	msr	cntkctl_el1, x14
198532ed618SSoby Mathew#endif
199532ed618SSoby Mathew
200532ed618SSoby Mathew	/* No explict ISB required here as ERET covers it */
201532ed618SSoby Mathew	ret
202532ed618SSoby Mathewendfunc el1_sysregs_context_restore
203532ed618SSoby Mathew
204532ed618SSoby Mathew/* -----------------------------------------------------
205532ed618SSoby Mathew * The following function follows the aapcs_64 strictly
206532ed618SSoby Mathew * to use x9-x17 (temporary caller-saved registers
207532ed618SSoby Mathew * according to AArch64 PCS) to save floating point
208532ed618SSoby Mathew * register context. It assumes that 'x0' is pointing to
209532ed618SSoby Mathew * a 'fp_regs' structure where the register context will
210532ed618SSoby Mathew * be saved.
211532ed618SSoby Mathew *
212532ed618SSoby Mathew * Access to VFP registers will trap if CPTR_EL3.TFP is
213532ed618SSoby Mathew * set.  However currently we don't use VFP registers
214532ed618SSoby Mathew * nor set traps in Trusted Firmware, and assume it's
215532ed618SSoby Mathew * cleared
216532ed618SSoby Mathew *
217532ed618SSoby Mathew * TODO: Revisit when VFP is used in secure world
218532ed618SSoby Mathew * -----------------------------------------------------
219532ed618SSoby Mathew */
220532ed618SSoby Mathew#if CTX_INCLUDE_FPREGS
221532ed618SSoby Mathewfunc fpregs_context_save
222532ed618SSoby Mathew	stp	q0, q1, [x0, #CTX_FP_Q0]
223532ed618SSoby Mathew	stp	q2, q3, [x0, #CTX_FP_Q2]
224532ed618SSoby Mathew	stp	q4, q5, [x0, #CTX_FP_Q4]
225532ed618SSoby Mathew	stp	q6, q7, [x0, #CTX_FP_Q6]
226532ed618SSoby Mathew	stp	q8, q9, [x0, #CTX_FP_Q8]
227532ed618SSoby Mathew	stp	q10, q11, [x0, #CTX_FP_Q10]
228532ed618SSoby Mathew	stp	q12, q13, [x0, #CTX_FP_Q12]
229532ed618SSoby Mathew	stp	q14, q15, [x0, #CTX_FP_Q14]
230532ed618SSoby Mathew	stp	q16, q17, [x0, #CTX_FP_Q16]
231532ed618SSoby Mathew	stp	q18, q19, [x0, #CTX_FP_Q18]
232532ed618SSoby Mathew	stp	q20, q21, [x0, #CTX_FP_Q20]
233532ed618SSoby Mathew	stp	q22, q23, [x0, #CTX_FP_Q22]
234532ed618SSoby Mathew	stp	q24, q25, [x0, #CTX_FP_Q24]
235532ed618SSoby Mathew	stp	q26, q27, [x0, #CTX_FP_Q26]
236532ed618SSoby Mathew	stp	q28, q29, [x0, #CTX_FP_Q28]
237532ed618SSoby Mathew	stp	q30, q31, [x0, #CTX_FP_Q30]
238532ed618SSoby Mathew
239532ed618SSoby Mathew	mrs	x9, fpsr
240532ed618SSoby Mathew	str	x9, [x0, #CTX_FP_FPSR]
241532ed618SSoby Mathew
242532ed618SSoby Mathew	mrs	x10, fpcr
243532ed618SSoby Mathew	str	x10, [x0, #CTX_FP_FPCR]
244532ed618SSoby Mathew
24591089f36SDavid Cunado#if CTX_INCLUDE_AARCH32_REGS
24691089f36SDavid Cunado	mrs	x11, fpexc32_el2
24791089f36SDavid Cunado	str	x11, [x0, #CTX_FP_FPEXC32_EL2]
24891089f36SDavid Cunado#endif
249532ed618SSoby Mathew	ret
250532ed618SSoby Mathewendfunc fpregs_context_save
251532ed618SSoby Mathew
252532ed618SSoby Mathew/* -----------------------------------------------------
253532ed618SSoby Mathew * The following function follows the aapcs_64 strictly
254532ed618SSoby Mathew * to use x9-x17 (temporary caller-saved registers
255532ed618SSoby Mathew * according to AArch64 PCS) to restore floating point
256532ed618SSoby Mathew * register context. It assumes that 'x0' is pointing to
257532ed618SSoby Mathew * a 'fp_regs' structure from where the register context
258532ed618SSoby Mathew * will be restored.
259532ed618SSoby Mathew *
260532ed618SSoby Mathew * Access to VFP registers will trap if CPTR_EL3.TFP is
261532ed618SSoby Mathew * set.  However currently we don't use VFP registers
262532ed618SSoby Mathew * nor set traps in Trusted Firmware, and assume it's
263532ed618SSoby Mathew * cleared
264532ed618SSoby Mathew *
265532ed618SSoby Mathew * TODO: Revisit when VFP is used in secure world
266532ed618SSoby Mathew * -----------------------------------------------------
267532ed618SSoby Mathew */
268532ed618SSoby Mathewfunc fpregs_context_restore
269532ed618SSoby Mathew	ldp	q0, q1, [x0, #CTX_FP_Q0]
270532ed618SSoby Mathew	ldp	q2, q3, [x0, #CTX_FP_Q2]
271532ed618SSoby Mathew	ldp	q4, q5, [x0, #CTX_FP_Q4]
272532ed618SSoby Mathew	ldp	q6, q7, [x0, #CTX_FP_Q6]
273532ed618SSoby Mathew	ldp	q8, q9, [x0, #CTX_FP_Q8]
274532ed618SSoby Mathew	ldp	q10, q11, [x0, #CTX_FP_Q10]
275532ed618SSoby Mathew	ldp	q12, q13, [x0, #CTX_FP_Q12]
276532ed618SSoby Mathew	ldp	q14, q15, [x0, #CTX_FP_Q14]
277532ed618SSoby Mathew	ldp	q16, q17, [x0, #CTX_FP_Q16]
278532ed618SSoby Mathew	ldp	q18, q19, [x0, #CTX_FP_Q18]
279532ed618SSoby Mathew	ldp	q20, q21, [x0, #CTX_FP_Q20]
280532ed618SSoby Mathew	ldp	q22, q23, [x0, #CTX_FP_Q22]
281532ed618SSoby Mathew	ldp	q24, q25, [x0, #CTX_FP_Q24]
282532ed618SSoby Mathew	ldp	q26, q27, [x0, #CTX_FP_Q26]
283532ed618SSoby Mathew	ldp	q28, q29, [x0, #CTX_FP_Q28]
284532ed618SSoby Mathew	ldp	q30, q31, [x0, #CTX_FP_Q30]
285532ed618SSoby Mathew
286532ed618SSoby Mathew	ldr	x9, [x0, #CTX_FP_FPSR]
287532ed618SSoby Mathew	msr	fpsr, x9
288532ed618SSoby Mathew
289532ed618SSoby Mathew	ldr	x10, [x0, #CTX_FP_FPCR]
290532ed618SSoby Mathew	msr	fpcr, x10
291532ed618SSoby Mathew
29291089f36SDavid Cunado#if CTX_INCLUDE_AARCH32_REGS
29391089f36SDavid Cunado	ldr	x11, [x0, #CTX_FP_FPEXC32_EL2]
29491089f36SDavid Cunado	msr	fpexc32_el2, x11
29591089f36SDavid Cunado#endif
296532ed618SSoby Mathew	/*
297532ed618SSoby Mathew	 * No explict ISB required here as ERET to
298532ed618SSoby Mathew	 * switch to secure EL1 or non-secure world
299532ed618SSoby Mathew	 * covers it
300532ed618SSoby Mathew	 */
301532ed618SSoby Mathew
302532ed618SSoby Mathew	ret
303532ed618SSoby Mathewendfunc fpregs_context_restore
304532ed618SSoby Mathew#endif /* CTX_INCLUDE_FPREGS */
305532ed618SSoby Mathew
306*5283962eSAntonio Nino Diaz#if CTX_INCLUDE_PAUTH_REGS
307*5283962eSAntonio Nino Diaz/* -----------------------------------------------------
308*5283962eSAntonio Nino Diaz * The following function strictly follows the AArch64
309*5283962eSAntonio Nino Diaz * PCS to use x9-x17 (temporary caller-saved registers)
310*5283962eSAntonio Nino Diaz * to save the ARMv8.3-PAuth register context. It assumes
311*5283962eSAntonio Nino Diaz * that 'sp' is pointing to a 'cpu_context_t' structure
312*5283962eSAntonio Nino Diaz * to where the register context will be saved.
313*5283962eSAntonio Nino Diaz * -----------------------------------------------------
314*5283962eSAntonio Nino Diaz */
315*5283962eSAntonio Nino Diazfunc pauth_context_save
316*5283962eSAntonio Nino Diaz	add	x11, sp, #CTX_PAUTH_REGS_OFFSET
317*5283962eSAntonio Nino Diaz
318*5283962eSAntonio Nino Diaz	mrs	x9, APIAKeyLo_EL1
319*5283962eSAntonio Nino Diaz	mrs	x10, APIAKeyHi_EL1
320*5283962eSAntonio Nino Diaz	stp	x9, x10, [x11, #CTX_PACIAKEY_LO]
321*5283962eSAntonio Nino Diaz
322*5283962eSAntonio Nino Diaz	mrs	x9, APIBKeyLo_EL1
323*5283962eSAntonio Nino Diaz	mrs	x10, APIBKeyHi_EL1
324*5283962eSAntonio Nino Diaz	stp	x9, x10, [x11, #CTX_PACIBKEY_LO]
325*5283962eSAntonio Nino Diaz
326*5283962eSAntonio Nino Diaz	mrs	x9, APDAKeyLo_EL1
327*5283962eSAntonio Nino Diaz	mrs	x10, APDAKeyHi_EL1
328*5283962eSAntonio Nino Diaz	stp	x9, x10, [x11, #CTX_PACDAKEY_LO]
329*5283962eSAntonio Nino Diaz
330*5283962eSAntonio Nino Diaz	mrs	x9, APDBKeyLo_EL1
331*5283962eSAntonio Nino Diaz	mrs	x10, APDBKeyHi_EL1
332*5283962eSAntonio Nino Diaz	stp	x9, x10, [x11, #CTX_PACDBKEY_LO]
333*5283962eSAntonio Nino Diaz
334*5283962eSAntonio Nino Diaz	mrs	x9, APGAKeyLo_EL1
335*5283962eSAntonio Nino Diaz	mrs	x10, APGAKeyHi_EL1
336*5283962eSAntonio Nino Diaz	stp	x9, x10, [x11, #CTX_PACGAKEY_LO]
337*5283962eSAntonio Nino Diaz
338*5283962eSAntonio Nino Diaz	ret
339*5283962eSAntonio Nino Diazendfunc pauth_context_save
340*5283962eSAntonio Nino Diaz
341*5283962eSAntonio Nino Diaz/* -----------------------------------------------------
342*5283962eSAntonio Nino Diaz * The following function strictly follows the AArch64
343*5283962eSAntonio Nino Diaz * PCS to use x9-x17 (temporary caller-saved registers)
344*5283962eSAntonio Nino Diaz * to restore the ARMv8.3-PAuth register context. It assumes
345*5283962eSAntonio Nino Diaz * that 'sp' is pointing to a 'cpu_context_t' structure
346*5283962eSAntonio Nino Diaz * from where the register context will be restored.
347*5283962eSAntonio Nino Diaz * -----------------------------------------------------
348*5283962eSAntonio Nino Diaz */
349*5283962eSAntonio Nino Diazfunc pauth_context_restore
350*5283962eSAntonio Nino Diaz	add	x11, sp, #CTX_PAUTH_REGS_OFFSET
351*5283962eSAntonio Nino Diaz
352*5283962eSAntonio Nino Diaz	ldp	x9, x10, [x11, #CTX_PACIAKEY_LO]
353*5283962eSAntonio Nino Diaz	msr	APIAKeyLo_EL1, x9
354*5283962eSAntonio Nino Diaz	msr	APIAKeyHi_EL1, x10
355*5283962eSAntonio Nino Diaz
356*5283962eSAntonio Nino Diaz	ldp	x9, x10, [x11, #CTX_PACIAKEY_LO]
357*5283962eSAntonio Nino Diaz	msr	APIBKeyLo_EL1, x9
358*5283962eSAntonio Nino Diaz	msr	APIBKeyHi_EL1, x10
359*5283962eSAntonio Nino Diaz
360*5283962eSAntonio Nino Diaz	ldp	x9, x10, [x11, #CTX_PACDAKEY_LO]
361*5283962eSAntonio Nino Diaz	msr	APDAKeyLo_EL1, x9
362*5283962eSAntonio Nino Diaz	msr	APDAKeyHi_EL1, x10
363*5283962eSAntonio Nino Diaz
364*5283962eSAntonio Nino Diaz	ldp	x9, x10, [x11, #CTX_PACDBKEY_LO]
365*5283962eSAntonio Nino Diaz	msr	APDBKeyLo_EL1, x9
366*5283962eSAntonio Nino Diaz	msr	APDBKeyHi_EL1, x10
367*5283962eSAntonio Nino Diaz
368*5283962eSAntonio Nino Diaz	ldp	x9, x10, [x11, #CTX_PACGAKEY_LO]
369*5283962eSAntonio Nino Diaz	msr	APGAKeyLo_EL1, x9
370*5283962eSAntonio Nino Diaz	msr	APGAKeyHi_EL1, x10
371*5283962eSAntonio Nino Diaz
372*5283962eSAntonio Nino Diaz	ret
373*5283962eSAntonio Nino Diazendfunc pauth_context_restore
374*5283962eSAntonio Nino Diaz#endif /* CTX_INCLUDE_PAUTH_REGS */
375*5283962eSAntonio Nino Diaz
376532ed618SSoby Mathew/* -----------------------------------------------------
377532ed618SSoby Mathew * The following functions are used to save and restore
378532ed618SSoby Mathew * all the general purpose registers. Ideally we would
379532ed618SSoby Mathew * only save and restore the callee saved registers when
380532ed618SSoby Mathew * a world switch occurs but that type of implementation
381532ed618SSoby Mathew * is more complex. So currently we will always save and
382532ed618SSoby Mathew * restore these registers on entry and exit of EL3.
383532ed618SSoby Mathew * These are not macros to ensure their invocation fits
384532ed618SSoby Mathew * within the 32 instructions per exception vector.
385532ed618SSoby Mathew * clobbers: x18
386532ed618SSoby Mathew * -----------------------------------------------------
387532ed618SSoby Mathew */
388532ed618SSoby Mathewfunc save_gp_registers
389532ed618SSoby Mathew	stp	x0, x1, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X0]
390532ed618SSoby Mathew	stp	x2, x3, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X2]
391532ed618SSoby Mathew	stp	x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4]
392532ed618SSoby Mathew	stp	x6, x7, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X6]
393532ed618SSoby Mathew	stp	x8, x9, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X8]
394532ed618SSoby Mathew	stp	x10, x11, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X10]
395532ed618SSoby Mathew	stp	x12, x13, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X12]
396532ed618SSoby Mathew	stp	x14, x15, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X14]
397532ed618SSoby Mathew	stp	x16, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X16]
398532ed618SSoby Mathew	stp	x18, x19, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X18]
399532ed618SSoby Mathew	stp	x20, x21, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X20]
400532ed618SSoby Mathew	stp	x22, x23, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X22]
401532ed618SSoby Mathew	stp	x24, x25, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X24]
402532ed618SSoby Mathew	stp	x26, x27, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X26]
403532ed618SSoby Mathew	stp	x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28]
404532ed618SSoby Mathew	mrs	x18, sp_el0
405532ed618SSoby Mathew	str	x18, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_SP_EL0]
406532ed618SSoby Mathew	ret
407532ed618SSoby Mathewendfunc save_gp_registers
408532ed618SSoby Mathew
4094d1ccf0eSAntonio Nino Diaz/* -----------------------------------------------------
410ef653d93SJeenu Viswambharan * This function restores all general purpose registers except x30 from the
411ef653d93SJeenu Viswambharan * CPU context. x30 register must be explicitly restored by the caller.
4124d1ccf0eSAntonio Nino Diaz * -----------------------------------------------------
413ef653d93SJeenu Viswambharan */
414ef653d93SJeenu Viswambharanfunc restore_gp_registers
415532ed618SSoby Mathew	ldp	x0, x1, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X0]
416532ed618SSoby Mathew	ldp	x2, x3, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X2]
417532ed618SSoby Mathew	ldp	x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4]
418532ed618SSoby Mathew	ldp	x6, x7, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X6]
419532ed618SSoby Mathew	ldp	x8, x9, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X8]
420532ed618SSoby Mathew	ldp	x10, x11, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X10]
421532ed618SSoby Mathew	ldp	x12, x13, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X12]
422532ed618SSoby Mathew	ldp	x14, x15, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X14]
423ef653d93SJeenu Viswambharan	ldp	x16, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X16]
424532ed618SSoby Mathew	ldp	x18, x19, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X18]
425532ed618SSoby Mathew	ldp	x20, x21, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X20]
426532ed618SSoby Mathew	ldp	x22, x23, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X22]
427532ed618SSoby Mathew	ldp	x24, x25, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X24]
428532ed618SSoby Mathew	ldp	x26, x27, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X26]
429ef653d93SJeenu Viswambharan	ldr	x28, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_SP_EL0]
430ef653d93SJeenu Viswambharan	msr	sp_el0, x28
431532ed618SSoby Mathew	ldp	x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28]
432ef653d93SJeenu Viswambharan	ret
433ef653d93SJeenu Viswambharanendfunc restore_gp_registers
434ef653d93SJeenu Viswambharan
4354d1ccf0eSAntonio Nino Diaz/* -----------------------------------------------------
436ef653d93SJeenu Viswambharan * Restore general purpose registers (including x30), and exit EL3 via. ERET to
437ef653d93SJeenu Viswambharan * a lower exception level.
4384d1ccf0eSAntonio Nino Diaz * -----------------------------------------------------
439ef653d93SJeenu Viswambharan */
440ef653d93SJeenu Viswambharanfunc restore_gp_registers_eret
441ef653d93SJeenu Viswambharan	bl	restore_gp_registers
442ef653d93SJeenu Viswambharan	ldr	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
44314c6016aSJeenu Viswambharan
44414c6016aSJeenu Viswambharan#if IMAGE_BL31 && RAS_EXTENSION
44514c6016aSJeenu Viswambharan	/*
44614c6016aSJeenu Viswambharan	 * Issue Error Synchronization Barrier to synchronize SErrors before
44714c6016aSJeenu Viswambharan	 * exiting EL3. We're running with EAs unmasked, so any synchronized
44814c6016aSJeenu Viswambharan	 * errors would be taken immediately; therefore no need to inspect
44914c6016aSJeenu Viswambharan	 * DISR_EL1 register.
45014c6016aSJeenu Viswambharan	 */
45114c6016aSJeenu Viswambharan	esb
45214c6016aSJeenu Viswambharan#endif
453532ed618SSoby Mathew	eret
454ef653d93SJeenu Viswambharanendfunc	restore_gp_registers_eret
455532ed618SSoby Mathew
456532ed618SSoby Mathew/* -----------------------------------------------------
457532ed618SSoby Mathew * This routine assumes that the SP_EL3 is pointing to
458532ed618SSoby Mathew * a valid context structure from where the gp regs and
459532ed618SSoby Mathew * other special registers can be retrieved.
460532ed618SSoby Mathew * -----------------------------------------------------
461532ed618SSoby Mathew */
462532ed618SSoby Mathewfunc el3_exit
463532ed618SSoby Mathew	/* -----------------------------------------------------
464532ed618SSoby Mathew	 * Save the current SP_EL0 i.e. the EL3 runtime stack
465532ed618SSoby Mathew	 * which will be used for handling the next SMC. Then
466532ed618SSoby Mathew	 * switch to SP_EL3
467532ed618SSoby Mathew	 * -----------------------------------------------------
468532ed618SSoby Mathew	 */
469532ed618SSoby Mathew	mov	x17, sp
470532ed618SSoby Mathew	msr	spsel, #1
471532ed618SSoby Mathew	str	x17, [sp, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
472532ed618SSoby Mathew
473532ed618SSoby Mathew	/* -----------------------------------------------------
474532ed618SSoby Mathew	 * Restore SPSR_EL3, ELR_EL3 and SCR_EL3 prior to ERET
475532ed618SSoby Mathew	 * -----------------------------------------------------
476532ed618SSoby Mathew	 */
477532ed618SSoby Mathew	ldr	x18, [sp, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3]
478532ed618SSoby Mathew	ldp	x16, x17, [sp, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
479532ed618SSoby Mathew	msr	scr_el3, x18
480532ed618SSoby Mathew	msr	spsr_el3, x16
481532ed618SSoby Mathew	msr	elr_el3, x17
482532ed618SSoby Mathew
483fe007b2eSDimitris Papastamos#if IMAGE_BL31 && DYNAMIC_WORKAROUND_CVE_2018_3639
484fe007b2eSDimitris Papastamos	/* Restore mitigation state as it was on entry to EL3 */
485fe007b2eSDimitris Papastamos	ldr	x17, [sp, #CTX_CVE_2018_3639_OFFSET + CTX_CVE_2018_3639_DISABLE]
486fe007b2eSDimitris Papastamos	cmp	x17, xzr
487fe007b2eSDimitris Papastamos	beq	1f
488fe007b2eSDimitris Papastamos	blr	x17
4894d1ccf0eSAntonio Nino Diaz1:
490fe007b2eSDimitris Papastamos#endif
491fe007b2eSDimitris Papastamos
492*5283962eSAntonio Nino Diaz#if CTX_INCLUDE_PAUTH_REGS
493*5283962eSAntonio Nino Diaz	/* Restore ARMv8.3-PAuth registers */
494*5283962eSAntonio Nino Diaz	bl	pauth_context_restore
495*5283962eSAntonio Nino Diaz#endif
496*5283962eSAntonio Nino Diaz
497532ed618SSoby Mathew	/* Restore saved general purpose registers and return */
498532ed618SSoby Mathew	b	restore_gp_registers_eret
499532ed618SSoby Mathewendfunc el3_exit
500