xref: /rk3399_ARM-atf/lib/el3_runtime/aarch64/context.S (revision f74cb0be8ac80eb3072555cb04eb09375d4cb31f)
1532ed618SSoby Mathew/*
2c2d32a5fSMadhukar Pappireddy * Copyright (c) 2013-2021, 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>
9bb9549baSJan Dabros#include <assert_macros.S>
10532ed618SSoby Mathew#include <context.h>
113b8456bdSManish V Badarkhe#include <el3_common_macros.S>
12532ed618SSoby Mathew
1328f39f02SMax Shvetsov#if CTX_INCLUDE_EL2_REGS
1428f39f02SMax Shvetsov	.global	el2_sysregs_context_save
1528f39f02SMax Shvetsov	.global	el2_sysregs_context_restore
1628f39f02SMax Shvetsov#endif
1728f39f02SMax Shvetsov
18532ed618SSoby Mathew	.global	el1_sysregs_context_save
19532ed618SSoby Mathew	.global	el1_sysregs_context_restore
20532ed618SSoby Mathew#if CTX_INCLUDE_FPREGS
21532ed618SSoby Mathew	.global	fpregs_context_save
22532ed618SSoby Mathew	.global	fpregs_context_restore
23532ed618SSoby Mathew#endif
24ed108b56SAlexei Fedorov	.global	save_gp_pmcr_pauth_regs
25ed108b56SAlexei Fedorov	.global	restore_gp_pmcr_pauth_regs
263b8456bdSManish V Badarkhe	.global save_and_update_ptw_el1_sys_regs
27532ed618SSoby Mathew	.global	el3_exit
28532ed618SSoby Mathew
2928f39f02SMax Shvetsov#if CTX_INCLUDE_EL2_REGS
3028f39f02SMax Shvetsov
3128f39f02SMax Shvetsov/* -----------------------------------------------------
3228f39f02SMax Shvetsov * The following function strictly follows the AArch64
33a7cf2743SMax Shvetsov * PCS to use x9-x16 (temporary caller-saved registers)
342825946eSMax Shvetsov * to save EL2 system register context. It assumes that
352825946eSMax Shvetsov * 'x0' is pointing to a 'el2_sys_regs' structure where
3628f39f02SMax Shvetsov * the register context will be saved.
372825946eSMax Shvetsov *
382825946eSMax Shvetsov * The following registers are not added.
392825946eSMax Shvetsov * AMEVCNTVOFF0<n>_EL2
402825946eSMax Shvetsov * AMEVCNTVOFF1<n>_EL2
412825946eSMax Shvetsov * ICH_AP0R<n>_EL2
422825946eSMax Shvetsov * ICH_AP1R<n>_EL2
432825946eSMax Shvetsov * ICH_LR<n>_EL2
4428f39f02SMax Shvetsov * -----------------------------------------------------
4528f39f02SMax Shvetsov */
4628f39f02SMax Shvetsovfunc el2_sysregs_context_save
4728f39f02SMax Shvetsov	mrs	x9, actlr_el2
482825946eSMax Shvetsov	mrs	x10, afsr0_el2
492825946eSMax Shvetsov	stp	x9, x10, [x0, #CTX_ACTLR_EL2]
5028f39f02SMax Shvetsov
512825946eSMax Shvetsov	mrs	x11, afsr1_el2
522825946eSMax Shvetsov	mrs	x12, amair_el2
532825946eSMax Shvetsov	stp	x11, x12, [x0, #CTX_AFSR1_EL2]
5428f39f02SMax Shvetsov
552825946eSMax Shvetsov	mrs	x13, cnthctl_el2
56a7cf2743SMax Shvetsov	mrs	x14, cntvoff_el2
572825946eSMax Shvetsov	stp	x13, x14, [x0, #CTX_CNTHCTL_EL2]
5828f39f02SMax Shvetsov
59a7cf2743SMax Shvetsov	mrs	x15, cptr_el2
60a7cf2743SMax Shvetsov	str	x15, [x0, #CTX_CPTR_EL2]
6128f39f02SMax Shvetsov
620f777eabSArunachalam Ganapathy#if CTX_INCLUDE_AARCH32_REGS
63a7cf2743SMax Shvetsov	mrs	x16, dbgvcr32_el2
64a7cf2743SMax Shvetsov	str	x16, [x0, #CTX_DBGVCR32_EL2]
650f777eabSArunachalam Ganapathy#endif
6628f39f02SMax Shvetsov
67a7cf2743SMax Shvetsov	mrs	x9, elr_el2
68a7cf2743SMax Shvetsov	mrs	x10, esr_el2
69a7cf2743SMax Shvetsov	stp	x9, x10, [x0, #CTX_ELR_EL2]
7028f39f02SMax Shvetsov
71a7cf2743SMax Shvetsov	mrs	x11, far_el2
72a7cf2743SMax Shvetsov	mrs	x12, hacr_el2
73a7cf2743SMax Shvetsov	stp	x11, x12, [x0, #CTX_FAR_EL2]
7428f39f02SMax Shvetsov
75a7cf2743SMax Shvetsov	mrs	x13, hcr_el2
76a7cf2743SMax Shvetsov	mrs	x14, hpfar_el2
77a7cf2743SMax Shvetsov	stp	x13, x14, [x0, #CTX_HCR_EL2]
7828f39f02SMax Shvetsov
79a7cf2743SMax Shvetsov	mrs	x15, hstr_el2
80a7cf2743SMax Shvetsov	mrs	x16, ICC_SRE_EL2
81a7cf2743SMax Shvetsov	stp	x15, x16, [x0, #CTX_HSTR_EL2]
8228f39f02SMax Shvetsov
83a7cf2743SMax Shvetsov	mrs	x9, ICH_HCR_EL2
84a7cf2743SMax Shvetsov	mrs	x10, ICH_VMCR_EL2
85a7cf2743SMax Shvetsov	stp	x9, x10, [x0, #CTX_ICH_HCR_EL2]
8628f39f02SMax Shvetsov
87a7cf2743SMax Shvetsov	mrs	x11, mair_el2
88a7cf2743SMax Shvetsov	mrs	x12, mdcr_el2
89a7cf2743SMax Shvetsov	stp	x11, x12, [x0, #CTX_MAIR_EL2]
90a7cf2743SMax Shvetsov
912b036b79SArunachalam Ganapathy#if ENABLE_SPE_FOR_LOWER_ELS
92a7cf2743SMax Shvetsov	mrs	x13, PMSCR_EL2
93a7cf2743SMax Shvetsov	str	x13, [x0, #CTX_PMSCR_EL2]
942b036b79SArunachalam Ganapathy#endif
95a7cf2743SMax Shvetsov	mrs	x14, sctlr_el2
96a7cf2743SMax Shvetsov	str	x14, [x0, #CTX_SCTLR_EL2]
9728f39f02SMax Shvetsov
98a7cf2743SMax Shvetsov	mrs	x15, spsr_el2
99a7cf2743SMax Shvetsov	mrs	x16, sp_el2
100a7cf2743SMax Shvetsov	stp	x15, x16, [x0, #CTX_SPSR_EL2]
10128f39f02SMax Shvetsov
102a7cf2743SMax Shvetsov	mrs	x9, tcr_el2
103a7cf2743SMax Shvetsov	mrs	x10, tpidr_el2
104a7cf2743SMax Shvetsov	stp	x9, x10, [x0, #CTX_TCR_EL2]
10528f39f02SMax Shvetsov
106a7cf2743SMax Shvetsov	mrs	x11, ttbr0_el2
107a7cf2743SMax Shvetsov	mrs	x12, vbar_el2
108a7cf2743SMax Shvetsov	stp	x11, x12, [x0, #CTX_TTBR0_EL2]
10928f39f02SMax Shvetsov
110a7cf2743SMax Shvetsov	mrs	x13, vmpidr_el2
111a7cf2743SMax Shvetsov	mrs	x14, vpidr_el2
112a7cf2743SMax Shvetsov	stp	x13, x14, [x0, #CTX_VMPIDR_EL2]
11328f39f02SMax Shvetsov
114a7cf2743SMax Shvetsov	mrs	x15, vtcr_el2
115a7cf2743SMax Shvetsov	mrs	x16, vttbr_el2
116a7cf2743SMax Shvetsov	stp	x15, x16, [x0, #CTX_VTCR_EL2]
11728f39f02SMax Shvetsov
1182825946eSMax Shvetsov#if CTX_INCLUDE_MTE_REGS
119a7cf2743SMax Shvetsov	mrs	x9, TFSR_EL2
120a7cf2743SMax Shvetsov	str	x9, [x0, #CTX_TFSR_EL2]
1212825946eSMax Shvetsov#endif
12228f39f02SMax Shvetsov
1232825946eSMax Shvetsov#if ENABLE_MPAM_FOR_LOWER_ELS
124a7cf2743SMax Shvetsov	mrs	x10, MPAM2_EL2
125a7cf2743SMax Shvetsov	str	x10, [x0, #CTX_MPAM2_EL2]
1262825946eSMax Shvetsov
127a7cf2743SMax Shvetsov	mrs	x11, MPAMHCR_EL2
128a7cf2743SMax Shvetsov	mrs	x12, MPAMVPM0_EL2
129a7cf2743SMax Shvetsov	stp	x11, x12, [x0, #CTX_MPAMHCR_EL2]
1302825946eSMax Shvetsov
131a7cf2743SMax Shvetsov	mrs	x13, MPAMVPM1_EL2
132a7cf2743SMax Shvetsov	mrs	x14, MPAMVPM2_EL2
133a7cf2743SMax Shvetsov	stp	x13, x14, [x0, #CTX_MPAMVPM1_EL2]
1342825946eSMax Shvetsov
135a7cf2743SMax Shvetsov	mrs	x15, MPAMVPM3_EL2
136a7cf2743SMax Shvetsov	mrs	x16, MPAMVPM4_EL2
137a7cf2743SMax Shvetsov	stp	x15, x16, [x0, #CTX_MPAMVPM3_EL2]
1382825946eSMax Shvetsov
139a7cf2743SMax Shvetsov	mrs	x9, MPAMVPM5_EL2
140a7cf2743SMax Shvetsov	mrs	x10, MPAMVPM6_EL2
141a7cf2743SMax Shvetsov	stp	x9, x10, [x0, #CTX_MPAMVPM5_EL2]
1422825946eSMax Shvetsov
143a7cf2743SMax Shvetsov	mrs	x11, MPAMVPM7_EL2
144a7cf2743SMax Shvetsov	mrs	x12, MPAMVPMV_EL2
145a7cf2743SMax Shvetsov	stp	x11, x12, [x0, #CTX_MPAMVPM7_EL2]
1462825946eSMax Shvetsov#endif
1472825946eSMax Shvetsov
148*f74cb0beSJayanth Dodderi Chidanand#if ENABLE_FEAT_FGT
149*f74cb0beSJayanth Dodderi Chidanand	mrs	x13, HDFGRTR_EL2
150*f74cb0beSJayanth Dodderi Chidanand#if ENABLE_FEAT_AMUv1
151*f74cb0beSJayanth Dodderi Chidanand   	mrs	x14, HAFGRTR_EL2
152*f74cb0beSJayanth Dodderi Chidanand   	stp	x13, x14, [x0, #CTX_HDFGRTR_EL2]
153*f74cb0beSJayanth Dodderi Chidanand#else
154*f74cb0beSJayanth Dodderi Chidanand   	str	x13, [x0, #CTX_HDFGRTR_EL2]
155*f74cb0beSJayanth Dodderi Chidanand#endif
156a7cf2743SMax Shvetsov	mrs	x15, HDFGWTR_EL2
157a7cf2743SMax Shvetsov	mrs	x16, HFGITR_EL2
158a7cf2743SMax Shvetsov	stp	x15, x16, [x0, #CTX_HDFGWTR_EL2]
1592825946eSMax Shvetsov
160a7cf2743SMax Shvetsov	mrs	x9, HFGRTR_EL2
161a7cf2743SMax Shvetsov	mrs	x10, HFGWTR_EL2
162a7cf2743SMax Shvetsov	stp	x9, x10, [x0, #CTX_HFGRTR_EL2]
163*f74cb0beSJayanth Dodderi Chidanand#endif
1642825946eSMax Shvetsov
165*f74cb0beSJayanth Dodderi Chidanand#if ENABLE_FEAT_ECV
166a7cf2743SMax Shvetsov	mrs	x11, CNTPOFF_EL2
167a7cf2743SMax Shvetsov	str	x11, [x0, #CTX_CNTPOFF_EL2]
1682825946eSMax Shvetsov#endif
1692825946eSMax Shvetsov
1702825946eSMax Shvetsov#if ARM_ARCH_AT_LEAST(8, 4)
171a7cf2743SMax Shvetsov	mrs	x12, contextidr_el2
172a7cf2743SMax Shvetsov	str	x12, [x0, #CTX_CONTEXTIDR_EL2]
1732825946eSMax Shvetsov
1740f777eabSArunachalam Ganapathy#if CTX_INCLUDE_AARCH32_REGS
175a7cf2743SMax Shvetsov	mrs	x13, sder32_el2
176a7cf2743SMax Shvetsov	str	x13, [x0, #CTX_SDER32_EL2]
1770f777eabSArunachalam Ganapathy#endif
178a7cf2743SMax Shvetsov	mrs	x14, ttbr1_el2
179a7cf2743SMax Shvetsov	mrs	x15, vdisr_el2
180a7cf2743SMax Shvetsov	stp	x14, x15, [x0, #CTX_TTBR1_EL2]
1812825946eSMax Shvetsov
182062f8aafSArunachalam Ganapathy#if CTX_INCLUDE_NEVE_REGS
183a7cf2743SMax Shvetsov	mrs	x16, vncr_el2
184a7cf2743SMax Shvetsov	str	x16, [x0, #CTX_VNCR_EL2]
185062f8aafSArunachalam Ganapathy#endif
1862825946eSMax Shvetsov
187a7cf2743SMax Shvetsov	mrs	x9, vsesr_el2
188a7cf2743SMax Shvetsov	mrs	x10, vstcr_el2
189a7cf2743SMax Shvetsov	stp	x9, x10, [x0, #CTX_VSESR_EL2]
1902825946eSMax Shvetsov
191a7cf2743SMax Shvetsov	mrs	x11, vsttbr_el2
192a7cf2743SMax Shvetsov	mrs	x12, TRFCR_EL2
193a7cf2743SMax Shvetsov	stp	x11, x12, [x0, #CTX_VSTTBR_EL2]
1942825946eSMax Shvetsov#endif
1952825946eSMax Shvetsov
1962825946eSMax Shvetsov#if ARM_ARCH_AT_LEAST(8, 5)
197a7cf2743SMax Shvetsov	mrs	x13, scxtnum_el2
198a7cf2743SMax Shvetsov	str	x13, [x0, #CTX_SCXTNUM_EL2]
1992825946eSMax Shvetsov#endif
20028f39f02SMax Shvetsov
201cb4ec47bSjohpow01#if ENABLE_FEAT_HCX
202cb4ec47bSjohpow01	mrs	x14, hcrx_el2
203cb4ec47bSjohpow01	str	x14, [x0, #CTX_HCRX_EL2]
204cb4ec47bSjohpow01#endif
205cb4ec47bSjohpow01
20628f39f02SMax Shvetsov	ret
20728f39f02SMax Shvetsovendfunc el2_sysregs_context_save
20828f39f02SMax Shvetsov
209a7cf2743SMax Shvetsov
21028f39f02SMax Shvetsov/* -----------------------------------------------------
21128f39f02SMax Shvetsov * The following function strictly follows the AArch64
212a7cf2743SMax Shvetsov * PCS to use x9-x16 (temporary caller-saved registers)
2132825946eSMax Shvetsov * to restore EL2 system register context.  It assumes
2142825946eSMax Shvetsov * that 'x0' is pointing to a 'el2_sys_regs' structure
21528f39f02SMax Shvetsov * from where the register context will be restored
2162825946eSMax Shvetsov
2172825946eSMax Shvetsov * The following registers are not restored
2182825946eSMax Shvetsov * AMEVCNTVOFF0<n>_EL2
2192825946eSMax Shvetsov * AMEVCNTVOFF1<n>_EL2
2202825946eSMax Shvetsov * ICH_AP0R<n>_EL2
2212825946eSMax Shvetsov * ICH_AP1R<n>_EL2
2222825946eSMax Shvetsov * ICH_LR<n>_EL2
22328f39f02SMax Shvetsov * -----------------------------------------------------
22428f39f02SMax Shvetsov */
22528f39f02SMax Shvetsovfunc el2_sysregs_context_restore
2262825946eSMax Shvetsov	ldp	x9, x10, [x0, #CTX_ACTLR_EL2]
22728f39f02SMax Shvetsov	msr	actlr_el2, x9
2282825946eSMax Shvetsov	msr	afsr0_el2, x10
22928f39f02SMax Shvetsov
2302825946eSMax Shvetsov	ldp	x11, x12, [x0, #CTX_AFSR1_EL2]
2312825946eSMax Shvetsov	msr	afsr1_el2, x11
2322825946eSMax Shvetsov	msr	amair_el2, x12
23328f39f02SMax Shvetsov
2342825946eSMax Shvetsov	ldp	x13, x14, [x0, #CTX_CNTHCTL_EL2]
2352825946eSMax Shvetsov	msr	cnthctl_el2, x13
236a7cf2743SMax Shvetsov	msr	cntvoff_el2, x14
23728f39f02SMax Shvetsov
238a7cf2743SMax Shvetsov	ldr	x15, [x0, #CTX_CPTR_EL2]
239a7cf2743SMax Shvetsov	msr	cptr_el2, x15
24028f39f02SMax Shvetsov
2410f777eabSArunachalam Ganapathy#if CTX_INCLUDE_AARCH32_REGS
242a7cf2743SMax Shvetsov	ldr	x16, [x0, #CTX_DBGVCR32_EL2]
243a7cf2743SMax Shvetsov	msr	dbgvcr32_el2, x16
2440f777eabSArunachalam Ganapathy#endif
24528f39f02SMax Shvetsov
246a7cf2743SMax Shvetsov	ldp	x9, x10, [x0, #CTX_ELR_EL2]
247a7cf2743SMax Shvetsov	msr	elr_el2, x9
248a7cf2743SMax Shvetsov	msr	esr_el2, x10
24928f39f02SMax Shvetsov
250a7cf2743SMax Shvetsov	ldp	x11, x12, [x0, #CTX_FAR_EL2]
251a7cf2743SMax Shvetsov	msr	far_el2, x11
252a7cf2743SMax Shvetsov	msr	hacr_el2, x12
25328f39f02SMax Shvetsov
254a7cf2743SMax Shvetsov	ldp	x13, x14, [x0, #CTX_HCR_EL2]
255a7cf2743SMax Shvetsov	msr	hcr_el2, x13
256a7cf2743SMax Shvetsov	msr	hpfar_el2, x14
25728f39f02SMax Shvetsov
258a7cf2743SMax Shvetsov	ldp	x15, x16, [x0, #CTX_HSTR_EL2]
259a7cf2743SMax Shvetsov	msr	hstr_el2, x15
260a7cf2743SMax Shvetsov	msr	ICC_SRE_EL2, x16
26128f39f02SMax Shvetsov
262a7cf2743SMax Shvetsov	ldp	x9, x10, [x0, #CTX_ICH_HCR_EL2]
263a7cf2743SMax Shvetsov	msr	ICH_HCR_EL2, x9
264a7cf2743SMax Shvetsov	msr	ICH_VMCR_EL2, x10
265a7cf2743SMax Shvetsov
266a7cf2743SMax Shvetsov	ldp	x11, x12, [x0, #CTX_MAIR_EL2]
267a7cf2743SMax Shvetsov	msr	mair_el2, x11
268a7cf2743SMax Shvetsov	msr	mdcr_el2, x12
26928f39f02SMax Shvetsov
2702b036b79SArunachalam Ganapathy#if ENABLE_SPE_FOR_LOWER_ELS
271a7cf2743SMax Shvetsov	ldr	x13, [x0, #CTX_PMSCR_EL2]
272a7cf2743SMax Shvetsov	msr	PMSCR_EL2, x13
2732b036b79SArunachalam Ganapathy#endif
274a7cf2743SMax Shvetsov	ldr	x14, [x0, #CTX_SCTLR_EL2]
275a7cf2743SMax Shvetsov	msr	sctlr_el2, x14
27628f39f02SMax Shvetsov
277a7cf2743SMax Shvetsov	ldp	x15, x16, [x0, #CTX_SPSR_EL2]
278a7cf2743SMax Shvetsov	msr	spsr_el2, x15
279a7cf2743SMax Shvetsov	msr	sp_el2, x16
28028f39f02SMax Shvetsov
281a7cf2743SMax Shvetsov	ldp	x9, x10, [x0, #CTX_TCR_EL2]
282a7cf2743SMax Shvetsov	msr	tcr_el2, x9
283a7cf2743SMax Shvetsov	msr	tpidr_el2, x10
28428f39f02SMax Shvetsov
285a7cf2743SMax Shvetsov	ldp	x11, x12, [x0, #CTX_TTBR0_EL2]
286a7cf2743SMax Shvetsov	msr	ttbr0_el2, x11
287a7cf2743SMax Shvetsov	msr	vbar_el2, x12
28828f39f02SMax Shvetsov
289a7cf2743SMax Shvetsov	ldp	x13, x14, [x0, #CTX_VMPIDR_EL2]
290a7cf2743SMax Shvetsov	msr	vmpidr_el2, x13
291a7cf2743SMax Shvetsov	msr	vpidr_el2, x14
29228f39f02SMax Shvetsov
293a7cf2743SMax Shvetsov	ldp	x15, x16, [x0, #CTX_VTCR_EL2]
294a7cf2743SMax Shvetsov	msr	vtcr_el2, x15
295a7cf2743SMax Shvetsov	msr	vttbr_el2, x16
29628f39f02SMax Shvetsov
2972825946eSMax Shvetsov#if CTX_INCLUDE_MTE_REGS
298fb2072b0SManish V Badarkhe	ldr	x9, [x0, #CTX_TFSR_EL2]
299fb2072b0SManish V Badarkhe	msr	TFSR_EL2, x9
3002825946eSMax Shvetsov#endif
30128f39f02SMax Shvetsov
3022825946eSMax Shvetsov#if ENABLE_MPAM_FOR_LOWER_ELS
303a7cf2743SMax Shvetsov	ldr	x10, [x0, #CTX_MPAM2_EL2]
304fb2072b0SManish V Badarkhe	msr	MPAM2_EL2, x10
305a7cf2743SMax Shvetsov
306a7cf2743SMax Shvetsov	ldp	x11, x12, [x0, #CTX_MPAMHCR_EL2]
307fb2072b0SManish V Badarkhe	msr	MPAMHCR_EL2, x11
308fb2072b0SManish V Badarkhe	msr	MPAMVPM0_EL2, x12
309a7cf2743SMax Shvetsov
310a7cf2743SMax Shvetsov	ldp	x13, x14, [x0, #CTX_MPAMVPM1_EL2]
311fb2072b0SManish V Badarkhe	msr	MPAMVPM1_EL2, x13
312fb2072b0SManish V Badarkhe	msr	MPAMVPM2_EL2, x14
313a7cf2743SMax Shvetsov
314a7cf2743SMax Shvetsov	ldp	x15, x16, [x0, #CTX_MPAMVPM3_EL2]
315fb2072b0SManish V Badarkhe	msr	MPAMVPM3_EL2, x15
316fb2072b0SManish V Badarkhe	msr	MPAMVPM4_EL2, x16
3172825946eSMax Shvetsov
318a7cf2743SMax Shvetsov	ldp	x9, x10, [x0, #CTX_MPAMVPM5_EL2]
319a7cf2743SMax Shvetsov	msr	MPAMVPM5_EL2, x9
320a7cf2743SMax Shvetsov	msr	MPAMVPM6_EL2, x10
3212825946eSMax Shvetsov
322a7cf2743SMax Shvetsov	ldp	x11, x12, [x0, #CTX_MPAMVPM7_EL2]
323a7cf2743SMax Shvetsov	msr	MPAMVPM7_EL2, x11
324a7cf2743SMax Shvetsov	msr	MPAMVPMV_EL2, x12
3252825946eSMax Shvetsov#endif
3262825946eSMax Shvetsov
327*f74cb0beSJayanth Dodderi Chidanand#if ENABLE_FEAT_FGT
328*f74cb0beSJayanth Dodderi Chidanand#if ENABLE_FEAT_AMUv1
329*f74cb0beSJayanth Dodderi Chidanand	ldp	x13, x14, [x0, #CTX_HDFGRTR_EL2]
330*f74cb0beSJayanth Dodderi Chidanand	msr	HAFGRTR_EL2, x14
331*f74cb0beSJayanth Dodderi Chidanand#else
332*f74cb0beSJayanth Dodderi Chidanand	ldr	x13, [x0, #CTX_HDFGRTR_EL2]
333*f74cb0beSJayanth Dodderi Chidanand#endif
334*f74cb0beSJayanth Dodderi Chidanand	msr	HDFGRTR_EL2, x13
3352825946eSMax Shvetsov
336a7cf2743SMax Shvetsov	ldp	x15, x16, [x0, #CTX_HDFGWTR_EL2]
337a7cf2743SMax Shvetsov	msr	HDFGWTR_EL2, x15
338a7cf2743SMax Shvetsov	msr	HFGITR_EL2, x16
3392825946eSMax Shvetsov
340a7cf2743SMax Shvetsov	ldp	x9, x10, [x0, #CTX_HFGRTR_EL2]
341a7cf2743SMax Shvetsov	msr	HFGRTR_EL2, x9
342a7cf2743SMax Shvetsov	msr	HFGWTR_EL2, x10
343*f74cb0beSJayanth Dodderi Chidanand#endif
3442825946eSMax Shvetsov
345*f74cb0beSJayanth Dodderi Chidanand#if ENABLE_FEAT_ECV
346a7cf2743SMax Shvetsov	ldr	x11, [x0, #CTX_CNTPOFF_EL2]
347a7cf2743SMax Shvetsov	msr	CNTPOFF_EL2, x11
3482825946eSMax Shvetsov#endif
3492825946eSMax Shvetsov
3502825946eSMax Shvetsov#if ARM_ARCH_AT_LEAST(8, 4)
351a7cf2743SMax Shvetsov	ldr	x12, [x0, #CTX_CONTEXTIDR_EL2]
352a7cf2743SMax Shvetsov	msr	contextidr_el2, x12
3532825946eSMax Shvetsov
3540f777eabSArunachalam Ganapathy#if CTX_INCLUDE_AARCH32_REGS
355a7cf2743SMax Shvetsov	ldr	x13, [x0, #CTX_SDER32_EL2]
356a7cf2743SMax Shvetsov	msr	sder32_el2, x13
3570f777eabSArunachalam Ganapathy#endif
358a7cf2743SMax Shvetsov	ldp	x14, x15, [x0, #CTX_TTBR1_EL2]
359a7cf2743SMax Shvetsov	msr	ttbr1_el2, x14
360a7cf2743SMax Shvetsov	msr	vdisr_el2, x15
3612825946eSMax Shvetsov
362062f8aafSArunachalam Ganapathy#if CTX_INCLUDE_NEVE_REGS
363a7cf2743SMax Shvetsov	ldr	x16, [x0, #CTX_VNCR_EL2]
364a7cf2743SMax Shvetsov	msr	vncr_el2, x16
365062f8aafSArunachalam Ganapathy#endif
3662825946eSMax Shvetsov
367a7cf2743SMax Shvetsov	ldp	x9, x10, [x0, #CTX_VSESR_EL2]
368a7cf2743SMax Shvetsov	msr	vsesr_el2, x9
369a7cf2743SMax Shvetsov	msr	vstcr_el2, x10
3702825946eSMax Shvetsov
371a7cf2743SMax Shvetsov	ldp	x11, x12, [x0, #CTX_VSTTBR_EL2]
372a7cf2743SMax Shvetsov	msr	vsttbr_el2, x11
373a7cf2743SMax Shvetsov	msr	TRFCR_EL2, x12
3742825946eSMax Shvetsov#endif
3752825946eSMax Shvetsov
3762825946eSMax Shvetsov#if ARM_ARCH_AT_LEAST(8, 5)
377a7cf2743SMax Shvetsov	ldr	x13, [x0, #CTX_SCXTNUM_EL2]
378a7cf2743SMax Shvetsov	msr	scxtnum_el2, x13
3792825946eSMax Shvetsov#endif
38028f39f02SMax Shvetsov
381cb4ec47bSjohpow01#if ENABLE_FEAT_HCX
382cb4ec47bSjohpow01	ldr	x14, [x0, #CTX_HCRX_EL2]
383cb4ec47bSjohpow01	msr	hcrx_el2, x14
384cb4ec47bSjohpow01#endif
385cb4ec47bSjohpow01
38628f39f02SMax Shvetsov	ret
38728f39f02SMax Shvetsovendfunc el2_sysregs_context_restore
38828f39f02SMax Shvetsov
38928f39f02SMax Shvetsov#endif /* CTX_INCLUDE_EL2_REGS */
39028f39f02SMax Shvetsov
391ed108b56SAlexei Fedorov/* ------------------------------------------------------------------
392ed108b56SAlexei Fedorov * The following function strictly follows the AArch64 PCS to use
393ed108b56SAlexei Fedorov * x9-x17 (temporary caller-saved registers) to save EL1 system
394ed108b56SAlexei Fedorov * register context. It assumes that 'x0' is pointing to a
395ed108b56SAlexei Fedorov * 'el1_sys_regs' structure where the register context will be saved.
396ed108b56SAlexei Fedorov * ------------------------------------------------------------------
397532ed618SSoby Mathew */
398532ed618SSoby Mathewfunc el1_sysregs_context_save
399532ed618SSoby Mathew
400532ed618SSoby Mathew	mrs	x9, spsr_el1
401532ed618SSoby Mathew	mrs	x10, elr_el1
402532ed618SSoby Mathew	stp	x9, x10, [x0, #CTX_SPSR_EL1]
403532ed618SSoby Mathew
4043b8456bdSManish V Badarkhe#if !ERRATA_SPECULATIVE_AT
405532ed618SSoby Mathew	mrs	x15, sctlr_el1
406cb55615cSManish V Badarkhe	mrs	x16, tcr_el1
407532ed618SSoby Mathew	stp	x15, x16, [x0, #CTX_SCTLR_EL1]
4083b8456bdSManish V Badarkhe#endif
409532ed618SSoby Mathew
410532ed618SSoby Mathew	mrs	x17, cpacr_el1
411532ed618SSoby Mathew	mrs	x9, csselr_el1
412532ed618SSoby Mathew	stp	x17, x9, [x0, #CTX_CPACR_EL1]
413532ed618SSoby Mathew
414532ed618SSoby Mathew	mrs	x10, sp_el1
415532ed618SSoby Mathew	mrs	x11, esr_el1
416532ed618SSoby Mathew	stp	x10, x11, [x0, #CTX_SP_EL1]
417532ed618SSoby Mathew
418532ed618SSoby Mathew	mrs	x12, ttbr0_el1
419532ed618SSoby Mathew	mrs	x13, ttbr1_el1
420532ed618SSoby Mathew	stp	x12, x13, [x0, #CTX_TTBR0_EL1]
421532ed618SSoby Mathew
422532ed618SSoby Mathew	mrs	x14, mair_el1
423532ed618SSoby Mathew	mrs	x15, amair_el1
424532ed618SSoby Mathew	stp	x14, x15, [x0, #CTX_MAIR_EL1]
425532ed618SSoby Mathew
426cb55615cSManish V Badarkhe	mrs	x16, actlr_el1
427532ed618SSoby Mathew	mrs	x17, tpidr_el1
428cb55615cSManish V Badarkhe	stp	x16, x17, [x0, #CTX_ACTLR_EL1]
429532ed618SSoby Mathew
430532ed618SSoby Mathew	mrs	x9, tpidr_el0
431532ed618SSoby Mathew	mrs	x10, tpidrro_el0
432532ed618SSoby Mathew	stp	x9, x10, [x0, #CTX_TPIDR_EL0]
433532ed618SSoby Mathew
434532ed618SSoby Mathew	mrs	x13, par_el1
435532ed618SSoby Mathew	mrs	x14, far_el1
436532ed618SSoby Mathew	stp	x13, x14, [x0, #CTX_PAR_EL1]
437532ed618SSoby Mathew
438532ed618SSoby Mathew	mrs	x15, afsr0_el1
439532ed618SSoby Mathew	mrs	x16, afsr1_el1
440532ed618SSoby Mathew	stp	x15, x16, [x0, #CTX_AFSR0_EL1]
441532ed618SSoby Mathew
442532ed618SSoby Mathew	mrs	x17, contextidr_el1
443532ed618SSoby Mathew	mrs	x9, vbar_el1
444532ed618SSoby Mathew	stp	x17, x9, [x0, #CTX_CONTEXTIDR_EL1]
445532ed618SSoby Mathew
446532ed618SSoby Mathew	/* Save AArch32 system registers if the build has instructed so */
447532ed618SSoby Mathew#if CTX_INCLUDE_AARCH32_REGS
448532ed618SSoby Mathew	mrs	x11, spsr_abt
449532ed618SSoby Mathew	mrs	x12, spsr_und
450532ed618SSoby Mathew	stp	x11, x12, [x0, #CTX_SPSR_ABT]
451532ed618SSoby Mathew
452532ed618SSoby Mathew	mrs	x13, spsr_irq
453532ed618SSoby Mathew	mrs	x14, spsr_fiq
454532ed618SSoby Mathew	stp	x13, x14, [x0, #CTX_SPSR_IRQ]
455532ed618SSoby Mathew
456532ed618SSoby Mathew	mrs	x15, dacr32_el2
457532ed618SSoby Mathew	mrs	x16, ifsr32_el2
458532ed618SSoby Mathew	stp	x15, x16, [x0, #CTX_DACR32_EL2]
459532ed618SSoby Mathew#endif
460532ed618SSoby Mathew
461532ed618SSoby Mathew	/* Save NS timer registers if the build has instructed so */
462532ed618SSoby Mathew#if NS_TIMER_SWITCH
463532ed618SSoby Mathew	mrs	x10, cntp_ctl_el0
464532ed618SSoby Mathew	mrs	x11, cntp_cval_el0
465532ed618SSoby Mathew	stp	x10, x11, [x0, #CTX_CNTP_CTL_EL0]
466532ed618SSoby Mathew
467532ed618SSoby Mathew	mrs	x12, cntv_ctl_el0
468532ed618SSoby Mathew	mrs	x13, cntv_cval_el0
469532ed618SSoby Mathew	stp	x12, x13, [x0, #CTX_CNTV_CTL_EL0]
470532ed618SSoby Mathew
471532ed618SSoby Mathew	mrs	x14, cntkctl_el1
472532ed618SSoby Mathew	str	x14, [x0, #CTX_CNTKCTL_EL1]
473532ed618SSoby Mathew#endif
474532ed618SSoby Mathew
4759dd94382SJustin Chadwell	/* Save MTE system registers if the build has instructed so */
4769dd94382SJustin Chadwell#if CTX_INCLUDE_MTE_REGS
4779dd94382SJustin Chadwell	mrs	x15, TFSRE0_EL1
4789dd94382SJustin Chadwell	mrs	x16, TFSR_EL1
4799dd94382SJustin Chadwell	stp	x15, x16, [x0, #CTX_TFSRE0_EL1]
4809dd94382SJustin Chadwell
4819dd94382SJustin Chadwell	mrs	x9, RGSR_EL1
4829dd94382SJustin Chadwell	mrs	x10, GCR_EL1
4839dd94382SJustin Chadwell	stp	x9, x10, [x0, #CTX_RGSR_EL1]
4849dd94382SJustin Chadwell#endif
4859dd94382SJustin Chadwell
486532ed618SSoby Mathew	ret
487532ed618SSoby Mathewendfunc el1_sysregs_context_save
488532ed618SSoby Mathew
489ed108b56SAlexei Fedorov/* ------------------------------------------------------------------
490ed108b56SAlexei Fedorov * The following function strictly follows the AArch64 PCS to use
491ed108b56SAlexei Fedorov * x9-x17 (temporary caller-saved registers) to restore EL1 system
492ed108b56SAlexei Fedorov * register context.  It assumes that 'x0' is pointing to a
493ed108b56SAlexei Fedorov * 'el1_sys_regs' structure from where the register context will be
494ed108b56SAlexei Fedorov * restored
495ed108b56SAlexei Fedorov * ------------------------------------------------------------------
496532ed618SSoby Mathew */
497532ed618SSoby Mathewfunc el1_sysregs_context_restore
498532ed618SSoby Mathew
499532ed618SSoby Mathew	ldp	x9, x10, [x0, #CTX_SPSR_EL1]
500532ed618SSoby Mathew	msr	spsr_el1, x9
501532ed618SSoby Mathew	msr	elr_el1, x10
502532ed618SSoby Mathew
5033b8456bdSManish V Badarkhe#if !ERRATA_SPECULATIVE_AT
504fb2072b0SManish V Badarkhe	ldp	x15, x16, [x0, #CTX_SCTLR_EL1]
505fb2072b0SManish V Badarkhe	msr	sctlr_el1, x15
506cb55615cSManish V Badarkhe	msr	tcr_el1, x16
5073b8456bdSManish V Badarkhe#endif
508532ed618SSoby Mathew
509532ed618SSoby Mathew	ldp	x17, x9, [x0, #CTX_CPACR_EL1]
510532ed618SSoby Mathew	msr	cpacr_el1, x17
511532ed618SSoby Mathew	msr	csselr_el1, x9
512532ed618SSoby Mathew
513532ed618SSoby Mathew	ldp	x10, x11, [x0, #CTX_SP_EL1]
514532ed618SSoby Mathew	msr	sp_el1, x10
515532ed618SSoby Mathew	msr	esr_el1, x11
516532ed618SSoby Mathew
517532ed618SSoby Mathew	ldp	x12, x13, [x0, #CTX_TTBR0_EL1]
518532ed618SSoby Mathew	msr	ttbr0_el1, x12
519532ed618SSoby Mathew	msr	ttbr1_el1, x13
520532ed618SSoby Mathew
521532ed618SSoby Mathew	ldp	x14, x15, [x0, #CTX_MAIR_EL1]
522532ed618SSoby Mathew	msr	mair_el1, x14
523532ed618SSoby Mathew	msr	amair_el1, x15
524532ed618SSoby Mathew
525cb55615cSManish V Badarkhe	ldp 	x16, x17, [x0, #CTX_ACTLR_EL1]
526cb55615cSManish V Badarkhe	msr	actlr_el1, x16
527fb2072b0SManish V Badarkhe	msr	tpidr_el1, x17
528532ed618SSoby Mathew
529532ed618SSoby Mathew	ldp	x9, x10, [x0, #CTX_TPIDR_EL0]
530532ed618SSoby Mathew	msr	tpidr_el0, x9
531532ed618SSoby Mathew	msr	tpidrro_el0, x10
532532ed618SSoby Mathew
533532ed618SSoby Mathew	ldp	x13, x14, [x0, #CTX_PAR_EL1]
534532ed618SSoby Mathew	msr	par_el1, x13
535532ed618SSoby Mathew	msr	far_el1, x14
536532ed618SSoby Mathew
537532ed618SSoby Mathew	ldp	x15, x16, [x0, #CTX_AFSR0_EL1]
538532ed618SSoby Mathew	msr	afsr0_el1, x15
539532ed618SSoby Mathew	msr	afsr1_el1, x16
540532ed618SSoby Mathew
541532ed618SSoby Mathew	ldp	x17, x9, [x0, #CTX_CONTEXTIDR_EL1]
542532ed618SSoby Mathew	msr	contextidr_el1, x17
543532ed618SSoby Mathew	msr	vbar_el1, x9
544532ed618SSoby Mathew
545532ed618SSoby Mathew	/* Restore AArch32 system registers if the build has instructed so */
546532ed618SSoby Mathew#if CTX_INCLUDE_AARCH32_REGS
547532ed618SSoby Mathew	ldp	x11, x12, [x0, #CTX_SPSR_ABT]
548532ed618SSoby Mathew	msr	spsr_abt, x11
549532ed618SSoby Mathew	msr	spsr_und, x12
550532ed618SSoby Mathew
551532ed618SSoby Mathew	ldp	x13, x14, [x0, #CTX_SPSR_IRQ]
552532ed618SSoby Mathew	msr	spsr_irq, x13
553532ed618SSoby Mathew	msr	spsr_fiq, x14
554532ed618SSoby Mathew
555532ed618SSoby Mathew	ldp	x15, x16, [x0, #CTX_DACR32_EL2]
556532ed618SSoby Mathew	msr	dacr32_el2, x15
557532ed618SSoby Mathew	msr	ifsr32_el2, x16
558532ed618SSoby Mathew#endif
559532ed618SSoby Mathew	/* Restore NS timer registers if the build has instructed so */
560532ed618SSoby Mathew#if NS_TIMER_SWITCH
561532ed618SSoby Mathew	ldp	x10, x11, [x0, #CTX_CNTP_CTL_EL0]
562532ed618SSoby Mathew	msr	cntp_ctl_el0, x10
563532ed618SSoby Mathew	msr	cntp_cval_el0, x11
564532ed618SSoby Mathew
565532ed618SSoby Mathew	ldp	x12, x13, [x0, #CTX_CNTV_CTL_EL0]
566532ed618SSoby Mathew	msr	cntv_ctl_el0, x12
567532ed618SSoby Mathew	msr	cntv_cval_el0, x13
568532ed618SSoby Mathew
569532ed618SSoby Mathew	ldr	x14, [x0, #CTX_CNTKCTL_EL1]
570532ed618SSoby Mathew	msr	cntkctl_el1, x14
571532ed618SSoby Mathew#endif
5729dd94382SJustin Chadwell	/* Restore MTE system registers if the build has instructed so */
5739dd94382SJustin Chadwell#if CTX_INCLUDE_MTE_REGS
5749dd94382SJustin Chadwell	ldp	x11, x12, [x0, #CTX_TFSRE0_EL1]
5759dd94382SJustin Chadwell	msr	TFSRE0_EL1, x11
5769dd94382SJustin Chadwell	msr	TFSR_EL1, x12
5779dd94382SJustin Chadwell
5789dd94382SJustin Chadwell	ldp	x13, x14, [x0, #CTX_RGSR_EL1]
5799dd94382SJustin Chadwell	msr	RGSR_EL1, x13
5809dd94382SJustin Chadwell	msr	GCR_EL1, x14
5819dd94382SJustin Chadwell#endif
582532ed618SSoby Mathew
583532ed618SSoby Mathew	/* No explict ISB required here as ERET covers it */
584532ed618SSoby Mathew	ret
585532ed618SSoby Mathewendfunc el1_sysregs_context_restore
586532ed618SSoby Mathew
587ed108b56SAlexei Fedorov/* ------------------------------------------------------------------
588ed108b56SAlexei Fedorov * The following function follows the aapcs_64 strictly to use
589ed108b56SAlexei Fedorov * x9-x17 (temporary caller-saved registers according to AArch64 PCS)
590ed108b56SAlexei Fedorov * to save floating point register context. It assumes that 'x0' is
591ed108b56SAlexei Fedorov * pointing to a 'fp_regs' structure where the register context will
592532ed618SSoby Mathew * be saved.
593532ed618SSoby Mathew *
594ed108b56SAlexei Fedorov * Access to VFP registers will trap if CPTR_EL3.TFP is set.
595ed108b56SAlexei Fedorov * However currently we don't use VFP registers nor set traps in
596ed108b56SAlexei Fedorov * Trusted Firmware, and assume it's cleared.
597532ed618SSoby Mathew *
598532ed618SSoby Mathew * TODO: Revisit when VFP is used in secure world
599ed108b56SAlexei Fedorov * ------------------------------------------------------------------
600532ed618SSoby Mathew */
601532ed618SSoby Mathew#if CTX_INCLUDE_FPREGS
602532ed618SSoby Mathewfunc fpregs_context_save
603532ed618SSoby Mathew	stp	q0, q1, [x0, #CTX_FP_Q0]
604532ed618SSoby Mathew	stp	q2, q3, [x0, #CTX_FP_Q2]
605532ed618SSoby Mathew	stp	q4, q5, [x0, #CTX_FP_Q4]
606532ed618SSoby Mathew	stp	q6, q7, [x0, #CTX_FP_Q6]
607532ed618SSoby Mathew	stp	q8, q9, [x0, #CTX_FP_Q8]
608532ed618SSoby Mathew	stp	q10, q11, [x0, #CTX_FP_Q10]
609532ed618SSoby Mathew	stp	q12, q13, [x0, #CTX_FP_Q12]
610532ed618SSoby Mathew	stp	q14, q15, [x0, #CTX_FP_Q14]
611532ed618SSoby Mathew	stp	q16, q17, [x0, #CTX_FP_Q16]
612532ed618SSoby Mathew	stp	q18, q19, [x0, #CTX_FP_Q18]
613532ed618SSoby Mathew	stp	q20, q21, [x0, #CTX_FP_Q20]
614532ed618SSoby Mathew	stp	q22, q23, [x0, #CTX_FP_Q22]
615532ed618SSoby Mathew	stp	q24, q25, [x0, #CTX_FP_Q24]
616532ed618SSoby Mathew	stp	q26, q27, [x0, #CTX_FP_Q26]
617532ed618SSoby Mathew	stp	q28, q29, [x0, #CTX_FP_Q28]
618532ed618SSoby Mathew	stp	q30, q31, [x0, #CTX_FP_Q30]
619532ed618SSoby Mathew
620532ed618SSoby Mathew	mrs	x9, fpsr
621532ed618SSoby Mathew	str	x9, [x0, #CTX_FP_FPSR]
622532ed618SSoby Mathew
623532ed618SSoby Mathew	mrs	x10, fpcr
624532ed618SSoby Mathew	str	x10, [x0, #CTX_FP_FPCR]
625532ed618SSoby Mathew
62691089f36SDavid Cunado#if CTX_INCLUDE_AARCH32_REGS
62791089f36SDavid Cunado	mrs	x11, fpexc32_el2
62891089f36SDavid Cunado	str	x11, [x0, #CTX_FP_FPEXC32_EL2]
62991089f36SDavid Cunado#endif
630532ed618SSoby Mathew	ret
631532ed618SSoby Mathewendfunc fpregs_context_save
632532ed618SSoby Mathew
633ed108b56SAlexei Fedorov/* ------------------------------------------------------------------
634ed108b56SAlexei Fedorov * The following function follows the aapcs_64 strictly to use x9-x17
635ed108b56SAlexei Fedorov * (temporary caller-saved registers according to AArch64 PCS) to
636ed108b56SAlexei Fedorov * restore floating point register context. It assumes that 'x0' is
637ed108b56SAlexei Fedorov * pointing to a 'fp_regs' structure from where the register context
638532ed618SSoby Mathew * will be restored.
639532ed618SSoby Mathew *
640ed108b56SAlexei Fedorov * Access to VFP registers will trap if CPTR_EL3.TFP is set.
641ed108b56SAlexei Fedorov * However currently we don't use VFP registers nor set traps in
642ed108b56SAlexei Fedorov * Trusted Firmware, and assume it's cleared.
643532ed618SSoby Mathew *
644532ed618SSoby Mathew * TODO: Revisit when VFP is used in secure world
645ed108b56SAlexei Fedorov * ------------------------------------------------------------------
646532ed618SSoby Mathew */
647532ed618SSoby Mathewfunc fpregs_context_restore
648532ed618SSoby Mathew	ldp	q0, q1, [x0, #CTX_FP_Q0]
649532ed618SSoby Mathew	ldp	q2, q3, [x0, #CTX_FP_Q2]
650532ed618SSoby Mathew	ldp	q4, q5, [x0, #CTX_FP_Q4]
651532ed618SSoby Mathew	ldp	q6, q7, [x0, #CTX_FP_Q6]
652532ed618SSoby Mathew	ldp	q8, q9, [x0, #CTX_FP_Q8]
653532ed618SSoby Mathew	ldp	q10, q11, [x0, #CTX_FP_Q10]
654532ed618SSoby Mathew	ldp	q12, q13, [x0, #CTX_FP_Q12]
655532ed618SSoby Mathew	ldp	q14, q15, [x0, #CTX_FP_Q14]
656532ed618SSoby Mathew	ldp	q16, q17, [x0, #CTX_FP_Q16]
657532ed618SSoby Mathew	ldp	q18, q19, [x0, #CTX_FP_Q18]
658532ed618SSoby Mathew	ldp	q20, q21, [x0, #CTX_FP_Q20]
659532ed618SSoby Mathew	ldp	q22, q23, [x0, #CTX_FP_Q22]
660532ed618SSoby Mathew	ldp	q24, q25, [x0, #CTX_FP_Q24]
661532ed618SSoby Mathew	ldp	q26, q27, [x0, #CTX_FP_Q26]
662532ed618SSoby Mathew	ldp	q28, q29, [x0, #CTX_FP_Q28]
663532ed618SSoby Mathew	ldp	q30, q31, [x0, #CTX_FP_Q30]
664532ed618SSoby Mathew
665532ed618SSoby Mathew	ldr	x9, [x0, #CTX_FP_FPSR]
666532ed618SSoby Mathew	msr	fpsr, x9
667532ed618SSoby Mathew
668532ed618SSoby Mathew	ldr	x10, [x0, #CTX_FP_FPCR]
669532ed618SSoby Mathew	msr	fpcr, x10
670532ed618SSoby Mathew
67191089f36SDavid Cunado#if CTX_INCLUDE_AARCH32_REGS
67291089f36SDavid Cunado	ldr	x11, [x0, #CTX_FP_FPEXC32_EL2]
67391089f36SDavid Cunado	msr	fpexc32_el2, x11
67491089f36SDavid Cunado#endif
675532ed618SSoby Mathew	/*
676532ed618SSoby Mathew	 * No explict ISB required here as ERET to
677532ed618SSoby Mathew	 * switch to secure EL1 or non-secure world
678532ed618SSoby Mathew	 * covers it
679532ed618SSoby Mathew	 */
680532ed618SSoby Mathew
681532ed618SSoby Mathew	ret
682532ed618SSoby Mathewendfunc fpregs_context_restore
683532ed618SSoby Mathew#endif /* CTX_INCLUDE_FPREGS */
684532ed618SSoby Mathew
685ed108b56SAlexei Fedorov/* ------------------------------------------------------------------
686ed108b56SAlexei Fedorov * The following function is used to save and restore all the general
687ed108b56SAlexei Fedorov * purpose and ARMv8.3-PAuth (if enabled) registers.
688ed108b56SAlexei Fedorov * It also checks if Secure Cycle Counter is not disabled in MDCR_EL3
689ed108b56SAlexei Fedorov * when ARMv8.5-PMU is implemented, and if called from Non-secure
690ed108b56SAlexei Fedorov * state saves PMCR_EL0 and disables Cycle Counter.
691ed108b56SAlexei Fedorov *
692ed108b56SAlexei Fedorov * Ideally we would only save and restore the callee saved registers
693ed108b56SAlexei Fedorov * when a world switch occurs but that type of implementation is more
694ed108b56SAlexei Fedorov * complex. So currently we will always save and restore these
695ed108b56SAlexei Fedorov * registers on entry and exit of EL3.
696ed108b56SAlexei Fedorov * These are not macros to ensure their invocation fits within the 32
697ed108b56SAlexei Fedorov * instructions per exception vector.
698532ed618SSoby Mathew * clobbers: x18
699ed108b56SAlexei Fedorov * ------------------------------------------------------------------
700532ed618SSoby Mathew */
701ed108b56SAlexei Fedorovfunc save_gp_pmcr_pauth_regs
702532ed618SSoby Mathew	stp	x0, x1, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X0]
703532ed618SSoby Mathew	stp	x2, x3, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X2]
704532ed618SSoby Mathew	stp	x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4]
705532ed618SSoby Mathew	stp	x6, x7, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X6]
706532ed618SSoby Mathew	stp	x8, x9, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X8]
707532ed618SSoby Mathew	stp	x10, x11, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X10]
708532ed618SSoby Mathew	stp	x12, x13, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X12]
709532ed618SSoby Mathew	stp	x14, x15, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X14]
710532ed618SSoby Mathew	stp	x16, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X16]
711532ed618SSoby Mathew	stp	x18, x19, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X18]
712532ed618SSoby Mathew	stp	x20, x21, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X20]
713532ed618SSoby Mathew	stp	x22, x23, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X22]
714532ed618SSoby Mathew	stp	x24, x25, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X24]
715532ed618SSoby Mathew	stp	x26, x27, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X26]
716532ed618SSoby Mathew	stp	x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28]
717532ed618SSoby Mathew	mrs	x18, sp_el0
718532ed618SSoby Mathew	str	x18, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_SP_EL0]
719532ed618SSoby Mathew
720ed108b56SAlexei Fedorov	/* ----------------------------------------------------------
72112f6c064SAlexei Fedorov	 * Check if earlier initialization MDCR_EL3.SCCD/MCCD to 1
72212f6c064SAlexei Fedorov	 * failed, meaning that FEAT_PMUv3p5/7 is not implemented and
72312f6c064SAlexei Fedorov	 * PMCR_EL0 should be saved in non-secure context.
724ed108b56SAlexei Fedorov	 * ----------------------------------------------------------
725ef653d93SJeenu Viswambharan	 */
72612f6c064SAlexei Fedorov	mov_imm	x10, (MDCR_SCCD_BIT | MDCR_MCCD_BIT)
727ed108b56SAlexei Fedorov	mrs	x9, mdcr_el3
72812f6c064SAlexei Fedorov	tst	x9, x10
729ed108b56SAlexei Fedorov	bne	1f
730ed108b56SAlexei Fedorov
731ed108b56SAlexei Fedorov	/* Secure Cycle Counter is not disabled */
732ed108b56SAlexei Fedorov	mrs	x9, pmcr_el0
733ed108b56SAlexei Fedorov
734ed108b56SAlexei Fedorov	/* Check caller's security state */
735ed108b56SAlexei Fedorov	mrs	x10, scr_el3
736ed108b56SAlexei Fedorov	tst	x10, #SCR_NS_BIT
737ed108b56SAlexei Fedorov	beq	2f
738ed108b56SAlexei Fedorov
739ed108b56SAlexei Fedorov	/* Save PMCR_EL0 if called from Non-secure state */
740ed108b56SAlexei Fedorov	str	x9, [sp, #CTX_EL3STATE_OFFSET + CTX_PMCR_EL0]
741ed108b56SAlexei Fedorov
742ed108b56SAlexei Fedorov	/* Disable cycle counter when event counting is prohibited */
743ed108b56SAlexei Fedorov2:	orr	x9, x9, #PMCR_EL0_DP_BIT
744ed108b56SAlexei Fedorov	msr	pmcr_el0, x9
745ed108b56SAlexei Fedorov	isb
746ed108b56SAlexei Fedorov1:
747ed108b56SAlexei Fedorov#if CTX_INCLUDE_PAUTH_REGS
748ed108b56SAlexei Fedorov	/* ----------------------------------------------------------
749ed108b56SAlexei Fedorov 	 * Save the ARMv8.3-PAuth keys as they are not banked
750ed108b56SAlexei Fedorov 	 * by exception level
751ed108b56SAlexei Fedorov	 * ----------------------------------------------------------
752ed108b56SAlexei Fedorov	 */
753ed108b56SAlexei Fedorov	add	x19, sp, #CTX_PAUTH_REGS_OFFSET
754ed108b56SAlexei Fedorov
755ed108b56SAlexei Fedorov	mrs	x20, APIAKeyLo_EL1	/* x21:x20 = APIAKey */
756ed108b56SAlexei Fedorov	mrs	x21, APIAKeyHi_EL1
757ed108b56SAlexei Fedorov	mrs	x22, APIBKeyLo_EL1	/* x23:x22 = APIBKey */
758ed108b56SAlexei Fedorov	mrs	x23, APIBKeyHi_EL1
759ed108b56SAlexei Fedorov	mrs	x24, APDAKeyLo_EL1	/* x25:x24 = APDAKey */
760ed108b56SAlexei Fedorov	mrs	x25, APDAKeyHi_EL1
761ed108b56SAlexei Fedorov	mrs	x26, APDBKeyLo_EL1	/* x27:x26 = APDBKey */
762ed108b56SAlexei Fedorov	mrs	x27, APDBKeyHi_EL1
763ed108b56SAlexei Fedorov	mrs	x28, APGAKeyLo_EL1	/* x29:x28 = APGAKey */
764ed108b56SAlexei Fedorov	mrs	x29, APGAKeyHi_EL1
765ed108b56SAlexei Fedorov
766ed108b56SAlexei Fedorov	stp	x20, x21, [x19, #CTX_PACIAKEY_LO]
767ed108b56SAlexei Fedorov	stp	x22, x23, [x19, #CTX_PACIBKEY_LO]
768ed108b56SAlexei Fedorov	stp	x24, x25, [x19, #CTX_PACDAKEY_LO]
769ed108b56SAlexei Fedorov	stp	x26, x27, [x19, #CTX_PACDBKEY_LO]
770ed108b56SAlexei Fedorov	stp	x28, x29, [x19, #CTX_PACGAKEY_LO]
771ed108b56SAlexei Fedorov#endif /* CTX_INCLUDE_PAUTH_REGS */
772ed108b56SAlexei Fedorov
773ed108b56SAlexei Fedorov	ret
774ed108b56SAlexei Fedorovendfunc save_gp_pmcr_pauth_regs
775ed108b56SAlexei Fedorov
776ed108b56SAlexei Fedorov/* ------------------------------------------------------------------
777ed108b56SAlexei Fedorov * This function restores ARMv8.3-PAuth (if enabled) and all general
778ed108b56SAlexei Fedorov * purpose registers except x30 from the CPU context.
779ed108b56SAlexei Fedorov * x30 register must be explicitly restored by the caller.
780ed108b56SAlexei Fedorov * ------------------------------------------------------------------
781ed108b56SAlexei Fedorov */
782ed108b56SAlexei Fedorovfunc restore_gp_pmcr_pauth_regs
783ed108b56SAlexei Fedorov#if CTX_INCLUDE_PAUTH_REGS
784ed108b56SAlexei Fedorov 	/* Restore the ARMv8.3 PAuth keys */
785ed108b56SAlexei Fedorov	add	x10, sp, #CTX_PAUTH_REGS_OFFSET
786ed108b56SAlexei Fedorov
787ed108b56SAlexei Fedorov	ldp	x0, x1, [x10, #CTX_PACIAKEY_LO]	/* x1:x0 = APIAKey */
788ed108b56SAlexei Fedorov	ldp	x2, x3, [x10, #CTX_PACIBKEY_LO]	/* x3:x2 = APIBKey */
789ed108b56SAlexei Fedorov	ldp	x4, x5, [x10, #CTX_PACDAKEY_LO]	/* x5:x4 = APDAKey */
790ed108b56SAlexei Fedorov	ldp	x6, x7, [x10, #CTX_PACDBKEY_LO]	/* x7:x6 = APDBKey */
791ed108b56SAlexei Fedorov	ldp	x8, x9, [x10, #CTX_PACGAKEY_LO]	/* x9:x8 = APGAKey */
792ed108b56SAlexei Fedorov
793ed108b56SAlexei Fedorov	msr	APIAKeyLo_EL1, x0
794ed108b56SAlexei Fedorov	msr	APIAKeyHi_EL1, x1
795ed108b56SAlexei Fedorov	msr	APIBKeyLo_EL1, x2
796ed108b56SAlexei Fedorov	msr	APIBKeyHi_EL1, x3
797ed108b56SAlexei Fedorov	msr	APDAKeyLo_EL1, x4
798ed108b56SAlexei Fedorov	msr	APDAKeyHi_EL1, x5
799ed108b56SAlexei Fedorov	msr	APDBKeyLo_EL1, x6
800ed108b56SAlexei Fedorov	msr	APDBKeyHi_EL1, x7
801ed108b56SAlexei Fedorov	msr	APGAKeyLo_EL1, x8
802ed108b56SAlexei Fedorov	msr	APGAKeyHi_EL1, x9
803ed108b56SAlexei Fedorov#endif /* CTX_INCLUDE_PAUTH_REGS */
804ed108b56SAlexei Fedorov
805ed108b56SAlexei Fedorov	/* ----------------------------------------------------------
806ed108b56SAlexei Fedorov	 * Restore PMCR_EL0 when returning to Non-secure state if
807ed108b56SAlexei Fedorov	 * Secure Cycle Counter is not disabled in MDCR_EL3 when
808ed108b56SAlexei Fedorov	 * ARMv8.5-PMU is implemented.
809ed108b56SAlexei Fedorov	 * ----------------------------------------------------------
810ed108b56SAlexei Fedorov	 */
811ed108b56SAlexei Fedorov	mrs	x0, scr_el3
812ed108b56SAlexei Fedorov	tst	x0, #SCR_NS_BIT
813ed108b56SAlexei Fedorov	beq	2f
814ed108b56SAlexei Fedorov
815ed108b56SAlexei Fedorov	/* ----------------------------------------------------------
816ed108b56SAlexei Fedorov	 * Back to Non-secure state.
81712f6c064SAlexei Fedorov	 * Check if earlier initialization MDCR_EL3.SCCD/MCCD to 1
81812f6c064SAlexei Fedorov	 * failed, meaning that FEAT_PMUv3p5/7 is not implemented and
81912f6c064SAlexei Fedorov	 * PMCR_EL0 should be restored from non-secure context.
820ed108b56SAlexei Fedorov	 * ----------------------------------------------------------
821ed108b56SAlexei Fedorov	 */
82212f6c064SAlexei Fedorov	mov_imm	x1, (MDCR_SCCD_BIT | MDCR_MCCD_BIT)
823ed108b56SAlexei Fedorov	mrs	x0, mdcr_el3
82412f6c064SAlexei Fedorov	tst	x0, x1
825ed108b56SAlexei Fedorov	bne	2f
826ed108b56SAlexei Fedorov	ldr	x0, [sp, #CTX_EL3STATE_OFFSET + CTX_PMCR_EL0]
827ed108b56SAlexei Fedorov	msr	pmcr_el0, x0
828ed108b56SAlexei Fedorov2:
829532ed618SSoby Mathew	ldp	x0, x1, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X0]
830532ed618SSoby Mathew	ldp	x2, x3, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X2]
831532ed618SSoby Mathew	ldp	x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4]
832532ed618SSoby Mathew	ldp	x6, x7, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X6]
833532ed618SSoby Mathew	ldp	x8, x9, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X8]
834532ed618SSoby Mathew	ldp	x10, x11, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X10]
835532ed618SSoby Mathew	ldp	x12, x13, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X12]
836532ed618SSoby Mathew	ldp	x14, x15, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X14]
837ef653d93SJeenu Viswambharan	ldp	x16, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X16]
838532ed618SSoby Mathew	ldp	x18, x19, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X18]
839532ed618SSoby Mathew	ldp	x20, x21, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X20]
840532ed618SSoby Mathew	ldp	x22, x23, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X22]
841532ed618SSoby Mathew	ldp	x24, x25, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X24]
842532ed618SSoby Mathew	ldp	x26, x27, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X26]
843ef653d93SJeenu Viswambharan	ldr	x28, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_SP_EL0]
844ef653d93SJeenu Viswambharan	msr	sp_el0, x28
845532ed618SSoby Mathew	ldp	x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28]
846ef653d93SJeenu Viswambharan	ret
847ed108b56SAlexei Fedorovendfunc restore_gp_pmcr_pauth_regs
848ef653d93SJeenu Viswambharan
8493b8456bdSManish V Badarkhe/*
8503b8456bdSManish V Badarkhe * In case of ERRATA_SPECULATIVE_AT, save SCTLR_EL1 and TCR_EL1
8513b8456bdSManish V Badarkhe * registers and update EL1 registers to disable stage1 and stage2
8523b8456bdSManish V Badarkhe * page table walk
8533b8456bdSManish V Badarkhe */
8543b8456bdSManish V Badarkhefunc save_and_update_ptw_el1_sys_regs
8553b8456bdSManish V Badarkhe	/* ----------------------------------------------------------
8563b8456bdSManish V Badarkhe	 * Save only sctlr_el1 and tcr_el1 registers
8573b8456bdSManish V Badarkhe	 * ----------------------------------------------------------
8583b8456bdSManish V Badarkhe	 */
8593b8456bdSManish V Badarkhe	mrs	x29, sctlr_el1
8603b8456bdSManish V Badarkhe	str	x29, [sp, #(CTX_EL1_SYSREGS_OFFSET + CTX_SCTLR_EL1)]
8613b8456bdSManish V Badarkhe	mrs	x29, tcr_el1
8623b8456bdSManish V Badarkhe	str	x29, [sp, #(CTX_EL1_SYSREGS_OFFSET + CTX_TCR_EL1)]
8633b8456bdSManish V Badarkhe
8643b8456bdSManish V Badarkhe	/* ------------------------------------------------------------
8653b8456bdSManish V Badarkhe	 * Must follow below order in order to disable page table
8663b8456bdSManish V Badarkhe	 * walk for lower ELs (EL1 and EL0). First step ensures that
8673b8456bdSManish V Badarkhe	 * page table walk is disabled for stage1 and second step
8683b8456bdSManish V Badarkhe	 * ensures that page table walker should use TCR_EL1.EPDx
8693b8456bdSManish V Badarkhe	 * bits to perform address translation. ISB ensures that CPU
8703b8456bdSManish V Badarkhe	 * does these 2 steps in order.
8713b8456bdSManish V Badarkhe	 *
8723b8456bdSManish V Badarkhe	 * 1. Update TCR_EL1.EPDx bits to disable page table walk by
8733b8456bdSManish V Badarkhe	 *    stage1.
8743b8456bdSManish V Badarkhe	 * 2. Enable MMU bit to avoid identity mapping via stage2
8753b8456bdSManish V Badarkhe	 *    and force TCR_EL1.EPDx to be used by the page table
8763b8456bdSManish V Badarkhe	 *    walker.
8773b8456bdSManish V Badarkhe	 * ------------------------------------------------------------
8783b8456bdSManish V Badarkhe	 */
8793b8456bdSManish V Badarkhe	orr	x29, x29, #(TCR_EPD0_BIT)
8803b8456bdSManish V Badarkhe	orr	x29, x29, #(TCR_EPD1_BIT)
8813b8456bdSManish V Badarkhe	msr	tcr_el1, x29
8823b8456bdSManish V Badarkhe	isb
8833b8456bdSManish V Badarkhe	mrs	x29, sctlr_el1
8843b8456bdSManish V Badarkhe	orr	x29, x29, #SCTLR_M_BIT
8853b8456bdSManish V Badarkhe	msr	sctlr_el1, x29
8863b8456bdSManish V Badarkhe	isb
8873b8456bdSManish V Badarkhe
8883b8456bdSManish V Badarkhe	ret
8893b8456bdSManish V Badarkheendfunc save_and_update_ptw_el1_sys_regs
8903b8456bdSManish V Badarkhe
891ed108b56SAlexei Fedorov/* ------------------------------------------------------------------
892ed108b56SAlexei Fedorov * This routine assumes that the SP_EL3 is pointing to a valid
893ed108b56SAlexei Fedorov * context structure from where the gp regs and other special
894ed108b56SAlexei Fedorov * registers can be retrieved.
895ed108b56SAlexei Fedorov * ------------------------------------------------------------------
896532ed618SSoby Mathew */
897532ed618SSoby Mathewfunc el3_exit
898bb9549baSJan Dabros#if ENABLE_ASSERTIONS
899bb9549baSJan Dabros	/* el3_exit assumes SP_EL0 on entry */
900bb9549baSJan Dabros	mrs	x17, spsel
901bb9549baSJan Dabros	cmp	x17, #MODE_SP_EL0
902bb9549baSJan Dabros	ASM_ASSERT(eq)
903bb9549baSJan Dabros#endif
904bb9549baSJan Dabros
905ed108b56SAlexei Fedorov	/* ----------------------------------------------------------
906ed108b56SAlexei Fedorov	 * Save the current SP_EL0 i.e. the EL3 runtime stack which
907ed108b56SAlexei Fedorov	 * will be used for handling the next SMC.
908ed108b56SAlexei Fedorov	 * Then switch to SP_EL3.
909ed108b56SAlexei Fedorov	 * ----------------------------------------------------------
910532ed618SSoby Mathew	 */
911532ed618SSoby Mathew	mov	x17, sp
912ed108b56SAlexei Fedorov	msr	spsel, #MODE_SP_ELX
913532ed618SSoby Mathew	str	x17, [sp, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
914532ed618SSoby Mathew
915ed108b56SAlexei Fedorov	/* ----------------------------------------------------------
916532ed618SSoby Mathew	 * Restore SPSR_EL3, ELR_EL3 and SCR_EL3 prior to ERET
917ed108b56SAlexei Fedorov	 * ----------------------------------------------------------
918532ed618SSoby Mathew	 */
919532ed618SSoby Mathew	ldr	x18, [sp, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3]
920532ed618SSoby Mathew	ldp	x16, x17, [sp, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
921532ed618SSoby Mathew	msr	scr_el3, x18
922532ed618SSoby Mathew	msr	spsr_el3, x16
923532ed618SSoby Mathew	msr	elr_el3, x17
924532ed618SSoby Mathew
9250c5e7d1cSMax Shvetsov#if IMAGE_BL31
9260c5e7d1cSMax Shvetsov	/* ----------------------------------------------------------
92768ac5ed0SArunachalam Ganapathy	 * Restore CPTR_EL3.
9280c5e7d1cSMax Shvetsov	 * ZCR is only restored if SVE is supported and enabled.
9290c5e7d1cSMax Shvetsov	 * Synchronization is required before zcr_el3 is addressed.
9300c5e7d1cSMax Shvetsov	 * ----------------------------------------------------------
9310c5e7d1cSMax Shvetsov	 */
9320c5e7d1cSMax Shvetsov	ldp	x19, x20, [sp, #CTX_EL3STATE_OFFSET + CTX_CPTR_EL3]
9330c5e7d1cSMax Shvetsov	msr	cptr_el3, x19
9340c5e7d1cSMax Shvetsov
9350c5e7d1cSMax Shvetsov	ands	x19, x19, #CPTR_EZ_BIT
9360c5e7d1cSMax Shvetsov	beq	sve_not_enabled
9370c5e7d1cSMax Shvetsov
9380c5e7d1cSMax Shvetsov	isb
9390c5e7d1cSMax Shvetsov	msr	S3_6_C1_C2_0, x20 /* zcr_el3 */
9400c5e7d1cSMax Shvetsovsve_not_enabled:
9410c5e7d1cSMax Shvetsov#endif
9420c5e7d1cSMax Shvetsov
943fe007b2eSDimitris Papastamos#if IMAGE_BL31 && DYNAMIC_WORKAROUND_CVE_2018_3639
944ed108b56SAlexei Fedorov	/* ----------------------------------------------------------
945ed108b56SAlexei Fedorov	 * Restore mitigation state as it was on entry to EL3
946ed108b56SAlexei Fedorov	 * ----------------------------------------------------------
947ed108b56SAlexei Fedorov	 */
948fe007b2eSDimitris Papastamos	ldr	x17, [sp, #CTX_CVE_2018_3639_OFFSET + CTX_CVE_2018_3639_DISABLE]
949ed108b56SAlexei Fedorov	cbz	x17, 1f
950fe007b2eSDimitris Papastamos	blr	x17
9514d1ccf0eSAntonio Nino Diaz1:
952fe007b2eSDimitris Papastamos#endif
9533b8456bdSManish V Badarkhe	restore_ptw_el1_sys_regs
9543b8456bdSManish V Badarkhe
955ed108b56SAlexei Fedorov	/* ----------------------------------------------------------
956ed108b56SAlexei Fedorov	 * Restore general purpose (including x30), PMCR_EL0 and
957ed108b56SAlexei Fedorov	 * ARMv8.3-PAuth registers.
958ed108b56SAlexei Fedorov	 * Exit EL3 via ERET to a lower exception level.
959ed108b56SAlexei Fedorov 	 * ----------------------------------------------------------
960ed108b56SAlexei Fedorov 	 */
961ed108b56SAlexei Fedorov	bl	restore_gp_pmcr_pauth_regs
962ed108b56SAlexei Fedorov	ldr	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
963fe007b2eSDimitris Papastamos
964ed108b56SAlexei Fedorov#if IMAGE_BL31 && RAS_EXTENSION
965ed108b56SAlexei Fedorov	/* ----------------------------------------------------------
966ed108b56SAlexei Fedorov	 * Issue Error Synchronization Barrier to synchronize SErrors
967ed108b56SAlexei Fedorov	 * before exiting EL3. We're running with EAs unmasked, so
968ed108b56SAlexei Fedorov	 * any synchronized errors would be taken immediately;
969ed108b56SAlexei Fedorov	 * therefore no need to inspect DISR_EL1 register.
970ed108b56SAlexei Fedorov 	 * ----------------------------------------------------------
971ed108b56SAlexei Fedorov	 */
972ed108b56SAlexei Fedorov	esb
973c2d32a5fSMadhukar Pappireddy#else
974c2d32a5fSMadhukar Pappireddy	dsb	sy
975c2d32a5fSMadhukar Pappireddy#endif
976c2d32a5fSMadhukar Pappireddy#ifdef IMAGE_BL31
977c2d32a5fSMadhukar Pappireddy	str	xzr, [sp, #CTX_EL3STATE_OFFSET + CTX_IS_IN_EL3]
9785283962eSAntonio Nino Diaz#endif
979f461fe34SAnthony Steinhauser	exception_return
9805283962eSAntonio Nino Diaz
981532ed618SSoby Mathewendfunc el3_exit
982