blob: 0432d96b17a8005f796b26c3a00f63795ac237ab [file] [log] [blame]
Achin Gupta9ac63c52014-01-16 12:08:03 +00001/*
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +00002 * Copyright (c) 2013-2022, Arm Limited and Contributors. All rights reserved.
Achin Gupta9ac63c52014-01-16 12:08:03 +00003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Achin Gupta9ac63c52014-01-16 12:08:03 +00005 */
6
Dan Handley2bd4ef22014-04-09 13:14:54 +01007#include <arch.h>
Andrew Thoelke38bde412014-03-18 13:46:55 +00008#include <asm_macros.S>
Jan Dabrosfa015982019-12-02 13:30:03 +01009#include <assert_macros.S>
Dan Handley2bd4ef22014-04-09 13:14:54 +010010#include <context.h>
Manish V Badarkhee07e8082020-07-23 12:43:25 +010011#include <el3_common_macros.S>
Achin Gupta9ac63c52014-01-16 12:08:03 +000012
Max Shvetsovbdf502d2020-02-25 13:56:19 +000013#if CTX_INCLUDE_EL2_REGS
14 .global el2_sysregs_context_save
15 .global el2_sysregs_context_restore
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +000016#endif /* CTX_INCLUDE_EL2_REGS */
Max Shvetsovbdf502d2020-02-25 13:56:19 +000017
Yatharth Kochar6c0566c2015-10-02 17:56:48 +010018 .global el1_sysregs_context_save
19 .global el1_sysregs_context_restore
20#if CTX_INCLUDE_FPREGS
21 .global fpregs_context_save
22 .global fpregs_context_restore
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +000023#endif /* CTX_INCLUDE_FPREGS */
Daniel Boulby95fb1aa2022-01-19 11:20:05 +000024 .global prepare_el3_entry
Alexei Fedorovf41355c2019-09-13 14:11:59 +010025 .global restore_gp_pmcr_pauth_regs
Manish V Badarkhee07e8082020-07-23 12:43:25 +010026 .global save_and_update_ptw_el1_sys_regs
Yatharth Kochar6c0566c2015-10-02 17:56:48 +010027 .global el3_exit
28
Max Shvetsovbdf502d2020-02-25 13:56:19 +000029#if CTX_INCLUDE_EL2_REGS
30
31/* -----------------------------------------------------
32 * The following function strictly follows the AArch64
Max Shvetsovcf784f72021-03-31 19:00:38 +010033 * PCS to use x9-x16 (temporary caller-saved registers)
Max Shvetsovc9e2c922020-02-17 16:15:47 +000034 * to save EL2 system register context. It assumes that
35 * 'x0' is pointing to a 'el2_sys_regs' structure where
Max Shvetsovbdf502d2020-02-25 13:56:19 +000036 * the register context will be saved.
Max Shvetsovc9e2c922020-02-17 16:15:47 +000037 *
38 * The following registers are not added.
39 * AMEVCNTVOFF0<n>_EL2
40 * AMEVCNTVOFF1<n>_EL2
41 * ICH_AP0R<n>_EL2
42 * ICH_AP1R<n>_EL2
43 * ICH_LR<n>_EL2
Max Shvetsovbdf502d2020-02-25 13:56:19 +000044 * -----------------------------------------------------
45 */
Max Shvetsovc9e2c922020-02-17 16:15:47 +000046func el2_sysregs_context_save
Max Shvetsovbdf502d2020-02-25 13:56:19 +000047 mrs x9, actlr_el2
Max Shvetsovc9e2c922020-02-17 16:15:47 +000048 mrs x10, afsr0_el2
49 stp x9, x10, [x0, #CTX_ACTLR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +000050
Max Shvetsovc9e2c922020-02-17 16:15:47 +000051 mrs x11, afsr1_el2
52 mrs x12, amair_el2
53 stp x11, x12, [x0, #CTX_AFSR1_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +000054
Max Shvetsovc9e2c922020-02-17 16:15:47 +000055 mrs x13, cnthctl_el2
Max Shvetsovcf784f72021-03-31 19:00:38 +010056 mrs x14, cntvoff_el2
Max Shvetsovc9e2c922020-02-17 16:15:47 +000057 stp x13, x14, [x0, #CTX_CNTHCTL_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +000058
Max Shvetsovcf784f72021-03-31 19:00:38 +010059 mrs x15, cptr_el2
60 str x15, [x0, #CTX_CPTR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +000061
Arunachalam Ganapathydca591b2020-05-26 11:32:35 +010062#if CTX_INCLUDE_AARCH32_REGS
Max Shvetsovcf784f72021-03-31 19:00:38 +010063 mrs x16, dbgvcr32_el2
64 str x16, [x0, #CTX_DBGVCR32_EL2]
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +000065#endif /* CTX_INCLUDE_AARCH32_REGS */
Max Shvetsovbdf502d2020-02-25 13:56:19 +000066
Max Shvetsovcf784f72021-03-31 19:00:38 +010067 mrs x9, elr_el2
68 mrs x10, esr_el2
69 stp x9, x10, [x0, #CTX_ELR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +000070
Max Shvetsovcf784f72021-03-31 19:00:38 +010071 mrs x11, far_el2
72 mrs x12, hacr_el2
73 stp x11, x12, [x0, #CTX_FAR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +000074
Max Shvetsovcf784f72021-03-31 19:00:38 +010075 mrs x13, hcr_el2
76 mrs x14, hpfar_el2
77 stp x13, x14, [x0, #CTX_HCR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +000078
Max Shvetsovcf784f72021-03-31 19:00:38 +010079 mrs x15, hstr_el2
80 mrs x16, ICC_SRE_EL2
81 stp x15, x16, [x0, #CTX_HSTR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +000082
Max Shvetsovcf784f72021-03-31 19:00:38 +010083 mrs x9, ICH_HCR_EL2
84 mrs x10, ICH_VMCR_EL2
85 stp x9, x10, [x0, #CTX_ICH_HCR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +000086
Max Shvetsovcf784f72021-03-31 19:00:38 +010087 mrs x11, mair_el2
88 mrs x12, mdcr_el2
89 stp x11, x12, [x0, #CTX_MAIR_EL2]
90
Arunachalam Ganapathy04b7e432020-10-09 14:51:41 +010091#if ENABLE_SPE_FOR_LOWER_ELS
Max Shvetsovcf784f72021-03-31 19:00:38 +010092 mrs x13, PMSCR_EL2
93 str x13, [x0, #CTX_PMSCR_EL2]
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +000094#endif /* ENABLE_SPE_FOR_LOWER_ELS */
95
Max Shvetsovcf784f72021-03-31 19:00:38 +010096 mrs x14, sctlr_el2
97 str x14, [x0, #CTX_SCTLR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +000098
Max Shvetsovcf784f72021-03-31 19:00:38 +010099 mrs x15, spsr_el2
100 mrs x16, sp_el2
101 stp x15, x16, [x0, #CTX_SPSR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000102
Max Shvetsovcf784f72021-03-31 19:00:38 +0100103 mrs x9, tcr_el2
104 mrs x10, tpidr_el2
105 stp x9, x10, [x0, #CTX_TCR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000106
Max Shvetsovcf784f72021-03-31 19:00:38 +0100107 mrs x11, ttbr0_el2
108 mrs x12, vbar_el2
109 stp x11, x12, [x0, #CTX_TTBR0_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000110
Max Shvetsovcf784f72021-03-31 19:00:38 +0100111 mrs x13, vmpidr_el2
112 mrs x14, vpidr_el2
113 stp x13, x14, [x0, #CTX_VMPIDR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000114
Max Shvetsovcf784f72021-03-31 19:00:38 +0100115 mrs x15, vtcr_el2
116 mrs x16, vttbr_el2
117 stp x15, x16, [x0, #CTX_VTCR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000118
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000119#if CTX_INCLUDE_MTE_REGS
Max Shvetsovcf784f72021-03-31 19:00:38 +0100120 mrs x9, TFSR_EL2
121 str x9, [x0, #CTX_TFSR_EL2]
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000122#endif /* CTX_INCLUDE_MTE_REGS */
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000123
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000124#if ENABLE_MPAM_FOR_LOWER_ELS
Max Shvetsovcf784f72021-03-31 19:00:38 +0100125 mrs x10, MPAM2_EL2
126 str x10, [x0, #CTX_MPAM2_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000127
Max Shvetsovcf784f72021-03-31 19:00:38 +0100128 mrs x11, MPAMHCR_EL2
129 mrs x12, MPAMVPM0_EL2
130 stp x11, x12, [x0, #CTX_MPAMHCR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000131
Max Shvetsovcf784f72021-03-31 19:00:38 +0100132 mrs x13, MPAMVPM1_EL2
133 mrs x14, MPAMVPM2_EL2
134 stp x13, x14, [x0, #CTX_MPAMVPM1_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000135
Max Shvetsovcf784f72021-03-31 19:00:38 +0100136 mrs x15, MPAMVPM3_EL2
137 mrs x16, MPAMVPM4_EL2
138 stp x15, x16, [x0, #CTX_MPAMVPM3_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000139
Max Shvetsovcf784f72021-03-31 19:00:38 +0100140 mrs x9, MPAMVPM5_EL2
141 mrs x10, MPAMVPM6_EL2
142 stp x9, x10, [x0, #CTX_MPAMVPM5_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000143
Max Shvetsovcf784f72021-03-31 19:00:38 +0100144 mrs x11, MPAMVPM7_EL2
145 mrs x12, MPAMVPMV_EL2
146 stp x11, x12, [x0, #CTX_MPAMVPM7_EL2]
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000147#endif /* ENABLE_MPAM_FOR_LOWER_ELS */
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000148
Jayanth Dodderi Chidanand13ae0f42021-11-25 14:59:30 +0000149#if ENABLE_FEAT_FGT
150 mrs x13, HDFGRTR_EL2
151#if ENABLE_FEAT_AMUv1
152 mrs x14, HAFGRTR_EL2
153 stp x13, x14, [x0, #CTX_HDFGRTR_EL2]
154#else
155 str x13, [x0, #CTX_HDFGRTR_EL2]
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000156#endif /* ENABLE_FEAT_AMUv1 */
Max Shvetsovcf784f72021-03-31 19:00:38 +0100157 mrs x15, HDFGWTR_EL2
158 mrs x16, HFGITR_EL2
159 stp x15, x16, [x0, #CTX_HDFGWTR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000160
Max Shvetsovcf784f72021-03-31 19:00:38 +0100161 mrs x9, HFGRTR_EL2
162 mrs x10, HFGWTR_EL2
163 stp x9, x10, [x0, #CTX_HFGRTR_EL2]
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000164#endif /* ENABLE_FEAT_FGT */
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000165
Jayanth Dodderi Chidanand13ae0f42021-11-25 14:59:30 +0000166#if ENABLE_FEAT_ECV
Max Shvetsovcf784f72021-03-31 19:00:38 +0100167 mrs x11, CNTPOFF_EL2
168 str x11, [x0, #CTX_CNTPOFF_EL2]
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000169#endif /* ENABLE_FEAT_ECV */
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000170
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000171#if ENABLE_FEAT_VHE
172 /*
173 * CONTEXTIDR_EL2 register is saved only when FEAT_VHE or
174 * FEAT_Debugv8p2 (currently not in TF-A) is supported.
175 */
176 mrs x9, contextidr_el2
177 mrs x10, ttbr1_el2
178 stp x9, x10, [x0, #CTX_CONTEXTIDR_EL2]
179#endif /* ENABLE_FEAT_VHE */
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000180
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000181#if RAS_EXTENSION
182 /*
183 * VDISR_EL2 and VSESR_EL2 registers are saved only when
184 * FEAT_RAS is supported.
185 */
186 mrs x11, vdisr_el2
187 mrs x12, vsesr_el2
188 stp x11, x12, [x0, #CTX_VDISR_EL2]
189#endif /* RAS_EXTENSION */
190
191#if ENABLE_FEAT_SEL2
192 /*
193 * VSTCR_EL2 and VSTTBR_EL2 registers are saved only
194 * when FEAT_SEL2 is supported.
195 */
196 mrs x13, vstcr_el2
197 mrs x14, vsttbr_el2
198 stp x13, x14, [x0, #CTX_VSTCR_EL2]
199#endif /* ENABLE_FEAT_SEL2 */
200
201#if CTX_INCLUDE_AARCH32_REGS && ENABLE_FEAT_SEL2
202 /*
203 * SDER32_EL2 register is saved only when EL2 and EL1
204 * capable of using Aarch32 and FEAT_SEL2 is supported.
205 */
206 mrs x15, sder32_el2
207 str x15, [x0, #CTX_SDER32_EL2]
208#endif /* CTX_INCLUDE_AARCH32_REGS && ENABLE_FEAT_SEL2 */
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000209
Arunachalam Ganapathydd3ec7e2020-05-28 11:57:09 +0100210#if CTX_INCLUDE_NEVE_REGS
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000211 /*
212 * VNCR_EL2 register is saved only when FEAT_NV2 is supported.
213 */
Max Shvetsovcf784f72021-03-31 19:00:38 +0100214 mrs x16, vncr_el2
215 str x16, [x0, #CTX_VNCR_EL2]
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000216#endif /* CTX_INCLUDE_NEVE_REGS */
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000217
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000218#if ENABLE_TRF_FOR_NS
219 /*
220 * TRFCR_EL2 register is saved only when FEAT_TRF is supported.
221 */
Max Shvetsovcf784f72021-03-31 19:00:38 +0100222 mrs x12, TRFCR_EL2
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000223 str x12, [x0, #CTX_TRFCR_EL2]
224#endif /* ENABLE_TRF_FOR_NS */
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000225
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000226#if ENABLE_FEAT_CSV2_2
227 /*
228 * SCXTNUM_EL2 register is saved only when FEAT_CSV2_2 is supported.
229 */
Max Shvetsovcf784f72021-03-31 19:00:38 +0100230 mrs x13, scxtnum_el2
231 str x13, [x0, #CTX_SCXTNUM_EL2]
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000232#endif /* ENABLE_FEAT_CSV2_2 */
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000233
johpow01f91e59f2021-08-04 19:38:18 -0500234#if ENABLE_FEAT_HCX
235 mrs x14, hcrx_el2
236 str x14, [x0, #CTX_HCRX_EL2]
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000237#endif /* ENABLE_FEAT_HCX */
johpow01f91e59f2021-08-04 19:38:18 -0500238
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000239 ret
240endfunc el2_sysregs_context_save
241
Max Shvetsovcf784f72021-03-31 19:00:38 +0100242
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000243/* -----------------------------------------------------
244 * The following function strictly follows the AArch64
Max Shvetsovcf784f72021-03-31 19:00:38 +0100245 * PCS to use x9-x16 (temporary caller-saved registers)
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000246 * to restore EL2 system register context. It assumes
247 * that 'x0' is pointing to a 'el2_sys_regs' structure
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000248 * from where the register context will be restored
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000249
250 * The following registers are not restored
251 * AMEVCNTVOFF0<n>_EL2
252 * AMEVCNTVOFF1<n>_EL2
253 * ICH_AP0R<n>_EL2
254 * ICH_AP1R<n>_EL2
255 * ICH_LR<n>_EL2
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000256 * -----------------------------------------------------
257 */
258func el2_sysregs_context_restore
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000259 ldp x9, x10, [x0, #CTX_ACTLR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000260 msr actlr_el2, x9
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000261 msr afsr0_el2, x10
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000262
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000263 ldp x11, x12, [x0, #CTX_AFSR1_EL2]
264 msr afsr1_el2, x11
265 msr amair_el2, x12
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000266
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000267 ldp x13, x14, [x0, #CTX_CNTHCTL_EL2]
268 msr cnthctl_el2, x13
Max Shvetsovcf784f72021-03-31 19:00:38 +0100269 msr cntvoff_el2, x14
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000270
Max Shvetsovcf784f72021-03-31 19:00:38 +0100271 ldr x15, [x0, #CTX_CPTR_EL2]
272 msr cptr_el2, x15
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000273
Arunachalam Ganapathydca591b2020-05-26 11:32:35 +0100274#if CTX_INCLUDE_AARCH32_REGS
Max Shvetsovcf784f72021-03-31 19:00:38 +0100275 ldr x16, [x0, #CTX_DBGVCR32_EL2]
276 msr dbgvcr32_el2, x16
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000277#endif /* CTX_INCLUDE_AARCH32_REGS */
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000278
Max Shvetsovcf784f72021-03-31 19:00:38 +0100279 ldp x9, x10, [x0, #CTX_ELR_EL2]
280 msr elr_el2, x9
281 msr esr_el2, x10
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000282
Max Shvetsovcf784f72021-03-31 19:00:38 +0100283 ldp x11, x12, [x0, #CTX_FAR_EL2]
284 msr far_el2, x11
285 msr hacr_el2, x12
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000286
Max Shvetsovcf784f72021-03-31 19:00:38 +0100287 ldp x13, x14, [x0, #CTX_HCR_EL2]
288 msr hcr_el2, x13
289 msr hpfar_el2, x14
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000290
Max Shvetsovcf784f72021-03-31 19:00:38 +0100291 ldp x15, x16, [x0, #CTX_HSTR_EL2]
292 msr hstr_el2, x15
293 msr ICC_SRE_EL2, x16
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000294
Max Shvetsovcf784f72021-03-31 19:00:38 +0100295 ldp x9, x10, [x0, #CTX_ICH_HCR_EL2]
296 msr ICH_HCR_EL2, x9
297 msr ICH_VMCR_EL2, x10
298
299 ldp x11, x12, [x0, #CTX_MAIR_EL2]
300 msr mair_el2, x11
301 msr mdcr_el2, x12
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000302
Arunachalam Ganapathy04b7e432020-10-09 14:51:41 +0100303#if ENABLE_SPE_FOR_LOWER_ELS
Max Shvetsovcf784f72021-03-31 19:00:38 +0100304 ldr x13, [x0, #CTX_PMSCR_EL2]
305 msr PMSCR_EL2, x13
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000306#endif /* ENABLE_SPE_FOR_LOWER_ELS */
307
Max Shvetsovcf784f72021-03-31 19:00:38 +0100308 ldr x14, [x0, #CTX_SCTLR_EL2]
309 msr sctlr_el2, x14
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000310
Max Shvetsovcf784f72021-03-31 19:00:38 +0100311 ldp x15, x16, [x0, #CTX_SPSR_EL2]
312 msr spsr_el2, x15
313 msr sp_el2, x16
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000314
Max Shvetsovcf784f72021-03-31 19:00:38 +0100315 ldp x9, x10, [x0, #CTX_TCR_EL2]
316 msr tcr_el2, x9
317 msr tpidr_el2, x10
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000318
Max Shvetsovcf784f72021-03-31 19:00:38 +0100319 ldp x11, x12, [x0, #CTX_TTBR0_EL2]
320 msr ttbr0_el2, x11
321 msr vbar_el2, x12
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000322
Max Shvetsovcf784f72021-03-31 19:00:38 +0100323 ldp x13, x14, [x0, #CTX_VMPIDR_EL2]
324 msr vmpidr_el2, x13
325 msr vpidr_el2, x14
Manish V Badarkhed73c1ba2020-07-28 07:12:56 +0100326
Max Shvetsovcf784f72021-03-31 19:00:38 +0100327 ldp x15, x16, [x0, #CTX_VTCR_EL2]
328 msr vtcr_el2, x15
329 msr vttbr_el2, x16
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000330
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000331#if CTX_INCLUDE_MTE_REGS
Manish V Badarkhed73c1ba2020-07-28 07:12:56 +0100332 ldr x9, [x0, #CTX_TFSR_EL2]
333 msr TFSR_EL2, x9
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000334#endif /* CTX_INCLUDE_MTE_REGS */
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000335
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000336#if ENABLE_MPAM_FOR_LOWER_ELS
Max Shvetsovcf784f72021-03-31 19:00:38 +0100337 ldr x10, [x0, #CTX_MPAM2_EL2]
Manish V Badarkhed73c1ba2020-07-28 07:12:56 +0100338 msr MPAM2_EL2, x10
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000339
Max Shvetsovcf784f72021-03-31 19:00:38 +0100340 ldp x11, x12, [x0, #CTX_MPAMHCR_EL2]
341 msr MPAMHCR_EL2, x11
Manish V Badarkhed73c1ba2020-07-28 07:12:56 +0100342 msr MPAMVPM0_EL2, x12
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000343
Max Shvetsovcf784f72021-03-31 19:00:38 +0100344 ldp x13, x14, [x0, #CTX_MPAMVPM1_EL2]
345 msr MPAMVPM1_EL2, x13
Manish V Badarkhed73c1ba2020-07-28 07:12:56 +0100346 msr MPAMVPM2_EL2, x14
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000347
Max Shvetsovcf784f72021-03-31 19:00:38 +0100348 ldp x15, x16, [x0, #CTX_MPAMVPM3_EL2]
349 msr MPAMVPM3_EL2, x15
Manish V Badarkhed73c1ba2020-07-28 07:12:56 +0100350 msr MPAMVPM4_EL2, x16
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000351
Max Shvetsovcf784f72021-03-31 19:00:38 +0100352 ldp x9, x10, [x0, #CTX_MPAMVPM5_EL2]
353 msr MPAMVPM5_EL2, x9
354 msr MPAMVPM6_EL2, x10
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000355
Max Shvetsovcf784f72021-03-31 19:00:38 +0100356 ldp x11, x12, [x0, #CTX_MPAMVPM7_EL2]
357 msr MPAMVPM7_EL2, x11
358 msr MPAMVPMV_EL2, x12
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000359#endif /* ENABLE_MPAM_FOR_LOWER_ELS */
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000360
Jayanth Dodderi Chidanand13ae0f42021-11-25 14:59:30 +0000361#if ENABLE_FEAT_FGT
362#if ENABLE_FEAT_AMUv1
363 ldp x13, x14, [x0, #CTX_HDFGRTR_EL2]
364 msr HAFGRTR_EL2, x14
365#else
366 ldr x13, [x0, #CTX_HDFGRTR_EL2]
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000367#endif /* ENABLE_FEAT_AMUv1 */
Jayanth Dodderi Chidanand13ae0f42021-11-25 14:59:30 +0000368 msr HDFGRTR_EL2, x13
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000369
Max Shvetsovcf784f72021-03-31 19:00:38 +0100370 ldp x15, x16, [x0, #CTX_HDFGWTR_EL2]
371 msr HDFGWTR_EL2, x15
372 msr HFGITR_EL2, x16
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000373
Max Shvetsovcf784f72021-03-31 19:00:38 +0100374 ldp x9, x10, [x0, #CTX_HFGRTR_EL2]
375 msr HFGRTR_EL2, x9
376 msr HFGWTR_EL2, x10
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000377#endif /* ENABLE_FEAT_FGT */
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000378
Jayanth Dodderi Chidanand13ae0f42021-11-25 14:59:30 +0000379#if ENABLE_FEAT_ECV
Max Shvetsovcf784f72021-03-31 19:00:38 +0100380 ldr x11, [x0, #CTX_CNTPOFF_EL2]
381 msr CNTPOFF_EL2, x11
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000382#endif /* ENABLE_FEAT_ECV */
383
384#if ENABLE_FEAT_VHE
385 /*
386 * CONTEXTIDR_EL2 register is restored only when FEAT_VHE or
387 * FEAT_Debugv8p2 (currently not in TF-A) is supported.
388 */
389 ldp x9, x10, [x0, #CTX_CONTEXTIDR_EL2]
390 msr contextidr_el2, x9
391 msr ttbr1_el2, x10
392#endif /* ENABLE_FEAT_VHE */
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000393
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000394#if RAS_EXTENSION
395 /*
396 * VDISR_EL2 and VSESR_EL2 registers are restored only when FEAT_RAS
397 * is supported.
398 */
399 ldp x11, x12, [x0, #CTX_VDISR_EL2]
400 msr vdisr_el2, x11
401 msr vsesr_el2, x12
402#endif /* RAS_EXTENSION */
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000403
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000404#if ENABLE_FEAT_SEL2
405 /*
406 * VSTCR_EL2 and VSTTBR_EL2 registers are restored only when FEAT_SEL2
407 * is supported.
408 */
409 ldp x13, x14, [x0, #CTX_VSTCR_EL2]
410 msr vstcr_el2, x13
411 msr vsttbr_el2, x14
412#endif /* ENABLE_FEAT_SEL2 */
413
414#if CTX_INCLUDE_AARCH32_REGS && ENABLE_FEAT_SEL2
415 /*
416 * SDER32_EL2 register is restored only when EL2 and EL1 capable of using
417 * Aarch32 and FEAT_SEL2 is supported.
418 */
419 ldr x15, [x0, #CTX_SDER32_EL2]
420 msr sder32_el2, x15
421#endif /* CTX_INCLUDE_AARCH32_REGS && ENABLE_FEAT_SEL2 */
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000422
Arunachalam Ganapathydd3ec7e2020-05-28 11:57:09 +0100423#if CTX_INCLUDE_NEVE_REGS
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000424 /*
425 * VNCR_EL2 register is restored only when FEAT_NV2 is supported.
426 */
Max Shvetsovcf784f72021-03-31 19:00:38 +0100427 ldr x16, [x0, #CTX_VNCR_EL2]
428 msr vncr_el2, x16
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000429#endif /* CTX_INCLUDE_NEVE_REGS */
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000430
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000431#if ENABLE_TRF_FOR_NS
432 /*
433 * TRFCR_EL2 register is restored only when FEAT_TRF is supported.
434 */
435 ldr x12, [x0, #CTX_TRFCR_EL2]
Max Shvetsovcf784f72021-03-31 19:00:38 +0100436 msr TRFCR_EL2, x12
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000437#endif /* ENABLE_TRF_FOR_NS */
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000438
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000439#if ENABLE_FEAT_CSV2_2
440 /*
441 * SCXTNUM_EL2 register is restored only when FEAT_CSV2_2 is supported.
442 */
Max Shvetsovcf784f72021-03-31 19:00:38 +0100443 ldr x13, [x0, #CTX_SCXTNUM_EL2]
444 msr scxtnum_el2, x13
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000445#endif /* ENABLE_FEAT_CSV2_2 */
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000446
johpow01f91e59f2021-08-04 19:38:18 -0500447#if ENABLE_FEAT_HCX
448 ldr x14, [x0, #CTX_HCRX_EL2]
449 msr hcrx_el2, x14
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000450#endif /* ENABLE_FEAT_HCX */
johpow01f91e59f2021-08-04 19:38:18 -0500451
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000452 ret
453endfunc el2_sysregs_context_restore
454
455#endif /* CTX_INCLUDE_EL2_REGS */
456
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100457/* ------------------------------------------------------------------
458 * The following function strictly follows the AArch64 PCS to use
459 * x9-x17 (temporary caller-saved registers) to save EL1 system
460 * register context. It assumes that 'x0' is pointing to a
461 * 'el1_sys_regs' structure where the register context will be saved.
462 * ------------------------------------------------------------------
Achin Gupta9ac63c52014-01-16 12:08:03 +0000463 */
Andrew Thoelke38bde412014-03-18 13:46:55 +0000464func el1_sysregs_context_save
Achin Gupta9ac63c52014-01-16 12:08:03 +0000465
466 mrs x9, spsr_el1
467 mrs x10, elr_el1
468 stp x9, x10, [x0, #CTX_SPSR_EL1]
469
Manish V Badarkhee07e8082020-07-23 12:43:25 +0100470#if !ERRATA_SPECULATIVE_AT
Achin Gupta9ac63c52014-01-16 12:08:03 +0000471 mrs x15, sctlr_el1
Manish V Badarkhe2b0ee972020-07-28 07:22:30 +0100472 mrs x16, tcr_el1
Achin Gupta9ac63c52014-01-16 12:08:03 +0000473 stp x15, x16, [x0, #CTX_SCTLR_EL1]
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000474#endif /* ERRATA_SPECULATIVE_AT */
Achin Gupta9ac63c52014-01-16 12:08:03 +0000475
476 mrs x17, cpacr_el1
477 mrs x9, csselr_el1
478 stp x17, x9, [x0, #CTX_CPACR_EL1]
479
480 mrs x10, sp_el1
481 mrs x11, esr_el1
482 stp x10, x11, [x0, #CTX_SP_EL1]
483
484 mrs x12, ttbr0_el1
485 mrs x13, ttbr1_el1
486 stp x12, x13, [x0, #CTX_TTBR0_EL1]
487
488 mrs x14, mair_el1
489 mrs x15, amair_el1
490 stp x14, x15, [x0, #CTX_MAIR_EL1]
491
Manish V Badarkhe2b0ee972020-07-28 07:22:30 +0100492 mrs x16, actlr_el1
Achin Gupta9ac63c52014-01-16 12:08:03 +0000493 mrs x17, tpidr_el1
Manish V Badarkhe2b0ee972020-07-28 07:22:30 +0100494 stp x16, x17, [x0, #CTX_ACTLR_EL1]
Achin Gupta9ac63c52014-01-16 12:08:03 +0000495
496 mrs x9, tpidr_el0
497 mrs x10, tpidrro_el0
498 stp x9, x10, [x0, #CTX_TPIDR_EL0]
499
Achin Gupta9ac63c52014-01-16 12:08:03 +0000500 mrs x13, par_el1
501 mrs x14, far_el1
502 stp x13, x14, [x0, #CTX_PAR_EL1]
503
504 mrs x15, afsr0_el1
505 mrs x16, afsr1_el1
506 stp x15, x16, [x0, #CTX_AFSR0_EL1]
507
508 mrs x17, contextidr_el1
509 mrs x9, vbar_el1
510 stp x17, x9, [x0, #CTX_CONTEXTIDR_EL1]
511
Soby Mathewd75d2ba2016-05-17 14:01:32 +0100512 /* Save AArch32 system registers if the build has instructed so */
513#if CTX_INCLUDE_AARCH32_REGS
514 mrs x11, spsr_abt
515 mrs x12, spsr_und
516 stp x11, x12, [x0, #CTX_SPSR_ABT]
517
518 mrs x13, spsr_irq
519 mrs x14, spsr_fiq
520 stp x13, x14, [x0, #CTX_SPSR_IRQ]
521
522 mrs x15, dacr32_el2
523 mrs x16, ifsr32_el2
524 stp x15, x16, [x0, #CTX_DACR32_EL2]
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000525#endif /* CTX_INCLUDE_AARCH32_REGS */
Soby Mathewd75d2ba2016-05-17 14:01:32 +0100526
Jeenu Viswambharand1b60152014-05-12 15:28:47 +0100527 /* Save NS timer registers if the build has instructed so */
528#if NS_TIMER_SWITCH
Achin Gupta9ac63c52014-01-16 12:08:03 +0000529 mrs x10, cntp_ctl_el0
530 mrs x11, cntp_cval_el0
531 stp x10, x11, [x0, #CTX_CNTP_CTL_EL0]
532
533 mrs x12, cntv_ctl_el0
534 mrs x13, cntv_cval_el0
535 stp x12, x13, [x0, #CTX_CNTV_CTL_EL0]
536
537 mrs x14, cntkctl_el1
Jeenu Viswambharand1b60152014-05-12 15:28:47 +0100538 str x14, [x0, #CTX_CNTKCTL_EL1]
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000539#endif /* NS_TIMER_SWITCH */
Jeenu Viswambharand1b60152014-05-12 15:28:47 +0100540
Justin Chadwell1c7c13a2019-07-18 14:25:33 +0100541 /* Save MTE system registers if the build has instructed so */
542#if CTX_INCLUDE_MTE_REGS
543 mrs x15, TFSRE0_EL1
544 mrs x16, TFSR_EL1
545 stp x15, x16, [x0, #CTX_TFSRE0_EL1]
546
547 mrs x9, RGSR_EL1
548 mrs x10, GCR_EL1
549 stp x9, x10, [x0, #CTX_RGSR_EL1]
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000550#endif /* CTX_INCLUDE_MTE_REGS */
Justin Chadwell1c7c13a2019-07-18 14:25:33 +0100551
Achin Gupta9ac63c52014-01-16 12:08:03 +0000552 ret
Kévin Petita877c252015-03-24 14:03:57 +0000553endfunc el1_sysregs_context_save
Achin Gupta9ac63c52014-01-16 12:08:03 +0000554
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100555/* ------------------------------------------------------------------
556 * The following function strictly follows the AArch64 PCS to use
557 * x9-x17 (temporary caller-saved registers) to restore EL1 system
558 * register context. It assumes that 'x0' is pointing to a
559 * 'el1_sys_regs' structure from where the register context will be
560 * restored
561 * ------------------------------------------------------------------
Achin Gupta9ac63c52014-01-16 12:08:03 +0000562 */
Andrew Thoelke38bde412014-03-18 13:46:55 +0000563func el1_sysregs_context_restore
Achin Gupta9ac63c52014-01-16 12:08:03 +0000564
565 ldp x9, x10, [x0, #CTX_SPSR_EL1]
566 msr spsr_el1, x9
567 msr elr_el1, x10
568
Manish V Badarkhee07e8082020-07-23 12:43:25 +0100569#if !ERRATA_SPECULATIVE_AT
Manish V Badarkhed73c1ba2020-07-28 07:12:56 +0100570 ldp x15, x16, [x0, #CTX_SCTLR_EL1]
571 msr sctlr_el1, x15
Manish V Badarkhe2b0ee972020-07-28 07:22:30 +0100572 msr tcr_el1, x16
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000573#endif /* ERRATA_SPECULATIVE_AT */
Achin Gupta9ac63c52014-01-16 12:08:03 +0000574
575 ldp x17, x9, [x0, #CTX_CPACR_EL1]
576 msr cpacr_el1, x17
577 msr csselr_el1, x9
578
579 ldp x10, x11, [x0, #CTX_SP_EL1]
580 msr sp_el1, x10
581 msr esr_el1, x11
582
583 ldp x12, x13, [x0, #CTX_TTBR0_EL1]
584 msr ttbr0_el1, x12
585 msr ttbr1_el1, x13
586
587 ldp x14, x15, [x0, #CTX_MAIR_EL1]
588 msr mair_el1, x14
589 msr amair_el1, x15
590
Manish V Badarkhe2b0ee972020-07-28 07:22:30 +0100591 ldp x16, x17, [x0, #CTX_ACTLR_EL1]
592 msr actlr_el1, x16
Manish V Badarkhed73c1ba2020-07-28 07:12:56 +0100593 msr tpidr_el1, x17
Achin Gupta9ac63c52014-01-16 12:08:03 +0000594
595 ldp x9, x10, [x0, #CTX_TPIDR_EL0]
596 msr tpidr_el0, x9
597 msr tpidrro_el0, x10
598
Achin Gupta9ac63c52014-01-16 12:08:03 +0000599 ldp x13, x14, [x0, #CTX_PAR_EL1]
600 msr par_el1, x13
601 msr far_el1, x14
602
603 ldp x15, x16, [x0, #CTX_AFSR0_EL1]
604 msr afsr0_el1, x15
605 msr afsr1_el1, x16
606
607 ldp x17, x9, [x0, #CTX_CONTEXTIDR_EL1]
608 msr contextidr_el1, x17
609 msr vbar_el1, x9
610
Soby Mathewd75d2ba2016-05-17 14:01:32 +0100611 /* Restore AArch32 system registers if the build has instructed so */
612#if CTX_INCLUDE_AARCH32_REGS
613 ldp x11, x12, [x0, #CTX_SPSR_ABT]
614 msr spsr_abt, x11
615 msr spsr_und, x12
616
617 ldp x13, x14, [x0, #CTX_SPSR_IRQ]
618 msr spsr_irq, x13
619 msr spsr_fiq, x14
620
621 ldp x15, x16, [x0, #CTX_DACR32_EL2]
622 msr dacr32_el2, x15
623 msr ifsr32_el2, x16
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000624#endif /* CTX_INCLUDE_AARCH32_REGS */
625
Jeenu Viswambharand1b60152014-05-12 15:28:47 +0100626 /* Restore NS timer registers if the build has instructed so */
627#if NS_TIMER_SWITCH
Achin Gupta9ac63c52014-01-16 12:08:03 +0000628 ldp x10, x11, [x0, #CTX_CNTP_CTL_EL0]
629 msr cntp_ctl_el0, x10
630 msr cntp_cval_el0, x11
631
632 ldp x12, x13, [x0, #CTX_CNTV_CTL_EL0]
633 msr cntv_ctl_el0, x12
634 msr cntv_cval_el0, x13
635
Jeenu Viswambharand1b60152014-05-12 15:28:47 +0100636 ldr x14, [x0, #CTX_CNTKCTL_EL1]
Achin Gupta9ac63c52014-01-16 12:08:03 +0000637 msr cntkctl_el1, x14
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000638#endif /* NS_TIMER_SWITCH */
639
Justin Chadwell1c7c13a2019-07-18 14:25:33 +0100640 /* Restore MTE system registers if the build has instructed so */
641#if CTX_INCLUDE_MTE_REGS
642 ldp x11, x12, [x0, #CTX_TFSRE0_EL1]
643 msr TFSRE0_EL1, x11
644 msr TFSR_EL1, x12
645
646 ldp x13, x14, [x0, #CTX_RGSR_EL1]
647 msr RGSR_EL1, x13
648 msr GCR_EL1, x14
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000649#endif /* CTX_INCLUDE_MTE_REGS */
Jeenu Viswambharand1b60152014-05-12 15:28:47 +0100650
Achin Gupta9ac63c52014-01-16 12:08:03 +0000651 /* No explict ISB required here as ERET covers it */
Achin Gupta9ac63c52014-01-16 12:08:03 +0000652 ret
Kévin Petita877c252015-03-24 14:03:57 +0000653endfunc el1_sysregs_context_restore
Achin Gupta9ac63c52014-01-16 12:08:03 +0000654
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100655/* ------------------------------------------------------------------
656 * The following function follows the aapcs_64 strictly to use
657 * x9-x17 (temporary caller-saved registers according to AArch64 PCS)
658 * to save floating point register context. It assumes that 'x0' is
659 * pointing to a 'fp_regs' structure where the register context will
Achin Gupta9ac63c52014-01-16 12:08:03 +0000660 * be saved.
661 *
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100662 * Access to VFP registers will trap if CPTR_EL3.TFP is set.
663 * However currently we don't use VFP registers nor set traps in
664 * Trusted Firmware, and assume it's cleared.
Achin Gupta9ac63c52014-01-16 12:08:03 +0000665 *
666 * TODO: Revisit when VFP is used in secure world
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100667 * ------------------------------------------------------------------
Achin Gupta9ac63c52014-01-16 12:08:03 +0000668 */
Juan Castillo258e94f2014-06-25 17:26:36 +0100669#if CTX_INCLUDE_FPREGS
Andrew Thoelke38bde412014-03-18 13:46:55 +0000670func fpregs_context_save
Achin Gupta9ac63c52014-01-16 12:08:03 +0000671 stp q0, q1, [x0, #CTX_FP_Q0]
672 stp q2, q3, [x0, #CTX_FP_Q2]
673 stp q4, q5, [x0, #CTX_FP_Q4]
674 stp q6, q7, [x0, #CTX_FP_Q6]
675 stp q8, q9, [x0, #CTX_FP_Q8]
676 stp q10, q11, [x0, #CTX_FP_Q10]
677 stp q12, q13, [x0, #CTX_FP_Q12]
678 stp q14, q15, [x0, #CTX_FP_Q14]
679 stp q16, q17, [x0, #CTX_FP_Q16]
680 stp q18, q19, [x0, #CTX_FP_Q18]
681 stp q20, q21, [x0, #CTX_FP_Q20]
682 stp q22, q23, [x0, #CTX_FP_Q22]
683 stp q24, q25, [x0, #CTX_FP_Q24]
684 stp q26, q27, [x0, #CTX_FP_Q26]
685 stp q28, q29, [x0, #CTX_FP_Q28]
686 stp q30, q31, [x0, #CTX_FP_Q30]
687
688 mrs x9, fpsr
689 str x9, [x0, #CTX_FP_FPSR]
690
691 mrs x10, fpcr
692 str x10, [x0, #CTX_FP_FPCR]
693
David Cunadod1a1fd42017-10-20 11:30:57 +0100694#if CTX_INCLUDE_AARCH32_REGS
695 mrs x11, fpexc32_el2
696 str x11, [x0, #CTX_FP_FPEXC32_EL2]
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000697#endif /* CTX_INCLUDE_AARCH32_REGS */
Achin Gupta9ac63c52014-01-16 12:08:03 +0000698 ret
Kévin Petita877c252015-03-24 14:03:57 +0000699endfunc fpregs_context_save
Achin Gupta9ac63c52014-01-16 12:08:03 +0000700
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100701/* ------------------------------------------------------------------
702 * The following function follows the aapcs_64 strictly to use x9-x17
703 * (temporary caller-saved registers according to AArch64 PCS) to
704 * restore floating point register context. It assumes that 'x0' is
705 * pointing to a 'fp_regs' structure from where the register context
Achin Gupta9ac63c52014-01-16 12:08:03 +0000706 * will be restored.
707 *
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100708 * Access to VFP registers will trap if CPTR_EL3.TFP is set.
709 * However currently we don't use VFP registers nor set traps in
710 * Trusted Firmware, and assume it's cleared.
Achin Gupta9ac63c52014-01-16 12:08:03 +0000711 *
712 * TODO: Revisit when VFP is used in secure world
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100713 * ------------------------------------------------------------------
Achin Gupta9ac63c52014-01-16 12:08:03 +0000714 */
Andrew Thoelke38bde412014-03-18 13:46:55 +0000715func fpregs_context_restore
Achin Gupta9ac63c52014-01-16 12:08:03 +0000716 ldp q0, q1, [x0, #CTX_FP_Q0]
717 ldp q2, q3, [x0, #CTX_FP_Q2]
718 ldp q4, q5, [x0, #CTX_FP_Q4]
719 ldp q6, q7, [x0, #CTX_FP_Q6]
720 ldp q8, q9, [x0, #CTX_FP_Q8]
721 ldp q10, q11, [x0, #CTX_FP_Q10]
722 ldp q12, q13, [x0, #CTX_FP_Q12]
723 ldp q14, q15, [x0, #CTX_FP_Q14]
724 ldp q16, q17, [x0, #CTX_FP_Q16]
725 ldp q18, q19, [x0, #CTX_FP_Q18]
726 ldp q20, q21, [x0, #CTX_FP_Q20]
727 ldp q22, q23, [x0, #CTX_FP_Q22]
728 ldp q24, q25, [x0, #CTX_FP_Q24]
729 ldp q26, q27, [x0, #CTX_FP_Q26]
730 ldp q28, q29, [x0, #CTX_FP_Q28]
731 ldp q30, q31, [x0, #CTX_FP_Q30]
732
733 ldr x9, [x0, #CTX_FP_FPSR]
734 msr fpsr, x9
735
Soby Mathewe77e1162015-12-03 09:42:50 +0000736 ldr x10, [x0, #CTX_FP_FPCR]
Achin Gupta9ac63c52014-01-16 12:08:03 +0000737 msr fpcr, x10
738
David Cunadod1a1fd42017-10-20 11:30:57 +0100739#if CTX_INCLUDE_AARCH32_REGS
740 ldr x11, [x0, #CTX_FP_FPEXC32_EL2]
741 msr fpexc32_el2, x11
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000742#endif /* CTX_INCLUDE_AARCH32_REGS */
743
Achin Gupta9ac63c52014-01-16 12:08:03 +0000744 /*
745 * No explict ISB required here as ERET to
Sandrine Bailleuxf4119ec2015-12-17 13:58:58 +0000746 * switch to secure EL1 or non-secure world
Achin Gupta9ac63c52014-01-16 12:08:03 +0000747 * covers it
748 */
749
750 ret
Kévin Petita877c252015-03-24 14:03:57 +0000751endfunc fpregs_context_restore
Juan Castillo258e94f2014-06-25 17:26:36 +0100752#endif /* CTX_INCLUDE_FPREGS */
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100753
Daniel Boulby928747f2021-05-25 18:09:34 +0100754 /*
755 * Set the PSTATE bits not set when the exception was taken as
756 * described in the AArch64.TakeException() pseudocode function
757 * in ARM DDI 0487F.c page J1-7635 to a default value.
758 */
759 .macro set_unset_pstate_bits
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000760 /*
761 * If Data Independent Timing (DIT) functionality is implemented,
762 * always enable DIT in EL3
763 */
Daniel Boulby928747f2021-05-25 18:09:34 +0100764#if ENABLE_FEAT_DIT
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000765 mov x8, #DIT_BIT
766 msr DIT, x8
Daniel Boulby928747f2021-05-25 18:09:34 +0100767#endif /* ENABLE_FEAT_DIT */
768 .endm /* set_unset_pstate_bits */
769
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100770/* ------------------------------------------------------------------
Daniel Boulby95fb1aa2022-01-19 11:20:05 +0000771 * The following macro is used to save and restore all the general
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100772 * purpose and ARMv8.3-PAuth (if enabled) registers.
773 * It also checks if Secure Cycle Counter is not disabled in MDCR_EL3
774 * when ARMv8.5-PMU is implemented, and if called from Non-secure
775 * state saves PMCR_EL0 and disables Cycle Counter.
776 *
777 * Ideally we would only save and restore the callee saved registers
778 * when a world switch occurs but that type of implementation is more
779 * complex. So currently we will always save and restore these
780 * registers on entry and exit of EL3.
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100781 * clobbers: x18
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100782 * ------------------------------------------------------------------
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100783 */
Daniel Boulby95fb1aa2022-01-19 11:20:05 +0000784 .macro save_gp_pmcr_pauth_regs
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100785 stp x0, x1, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X0]
786 stp x2, x3, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X2]
787 stp x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4]
788 stp x6, x7, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X6]
789 stp x8, x9, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X8]
790 stp x10, x11, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X10]
791 stp x12, x13, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X12]
792 stp x14, x15, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X14]
793 stp x16, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X16]
794 stp x18, x19, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X18]
795 stp x20, x21, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X20]
796 stp x22, x23, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X22]
797 stp x24, x25, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X24]
798 stp x26, x27, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X26]
799 stp x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28]
800 mrs x18, sp_el0
801 str x18, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_SP_EL0]
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100802
803 /* ----------------------------------------------------------
Alexei Fedorov307f34b2021-05-14 11:21:56 +0100804 * Check if earlier initialization MDCR_EL3.SCCD/MCCD to 1
805 * failed, meaning that FEAT_PMUv3p5/7 is not implemented and
806 * PMCR_EL0 should be saved in non-secure context.
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100807 * ----------------------------------------------------------
808 */
Alexei Fedorov307f34b2021-05-14 11:21:56 +0100809 mov_imm x10, (MDCR_SCCD_BIT | MDCR_MCCD_BIT)
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100810 mrs x9, mdcr_el3
Alexei Fedorov307f34b2021-05-14 11:21:56 +0100811 tst x9, x10
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100812 bne 1f
813
814 /* Secure Cycle Counter is not disabled */
815 mrs x9, pmcr_el0
816
817 /* Check caller's security state */
818 mrs x10, scr_el3
819 tst x10, #SCR_NS_BIT
820 beq 2f
821
822 /* Save PMCR_EL0 if called from Non-secure state */
823 str x9, [sp, #CTX_EL3STATE_OFFSET + CTX_PMCR_EL0]
824
825 /* Disable cycle counter when event counting is prohibited */
8262: orr x9, x9, #PMCR_EL0_DP_BIT
827 msr pmcr_el0, x9
828 isb
8291:
830#if CTX_INCLUDE_PAUTH_REGS
831 /* ----------------------------------------------------------
832 * Save the ARMv8.3-PAuth keys as they are not banked
833 * by exception level
834 * ----------------------------------------------------------
835 */
836 add x19, sp, #CTX_PAUTH_REGS_OFFSET
837
838 mrs x20, APIAKeyLo_EL1 /* x21:x20 = APIAKey */
839 mrs x21, APIAKeyHi_EL1
840 mrs x22, APIBKeyLo_EL1 /* x23:x22 = APIBKey */
841 mrs x23, APIBKeyHi_EL1
842 mrs x24, APDAKeyLo_EL1 /* x25:x24 = APDAKey */
843 mrs x25, APDAKeyHi_EL1
844 mrs x26, APDBKeyLo_EL1 /* x27:x26 = APDBKey */
845 mrs x27, APDBKeyHi_EL1
846 mrs x28, APGAKeyLo_EL1 /* x29:x28 = APGAKey */
847 mrs x29, APGAKeyHi_EL1
848
849 stp x20, x21, [x19, #CTX_PACIAKEY_LO]
850 stp x22, x23, [x19, #CTX_PACIBKEY_LO]
851 stp x24, x25, [x19, #CTX_PACDAKEY_LO]
852 stp x26, x27, [x19, #CTX_PACDBKEY_LO]
853 stp x28, x29, [x19, #CTX_PACGAKEY_LO]
854#endif /* CTX_INCLUDE_PAUTH_REGS */
Daniel Boulby95fb1aa2022-01-19 11:20:05 +0000855 .endm /* save_gp_pmcr_pauth_regs */
856
857/* -----------------------------------------------------------------
Daniel Boulby928747f2021-05-25 18:09:34 +0100858 * This function saves the context and sets the PSTATE to a known
859 * state, preparing entry to el3.
Daniel Boulby95fb1aa2022-01-19 11:20:05 +0000860 * Save all the general purpose and ARMv8.3-PAuth (if enabled)
861 * registers.
Daniel Boulby928747f2021-05-25 18:09:34 +0100862 * Then set any of the PSTATE bits that are not set by hardware
863 * according to the Aarch64.TakeException pseudocode in the Arm
864 * Architecture Reference Manual to a default value for EL3.
865 * clobbers: x17
Daniel Boulby95fb1aa2022-01-19 11:20:05 +0000866 * -----------------------------------------------------------------
867 */
868func prepare_el3_entry
869 save_gp_pmcr_pauth_regs
Daniel Boulby928747f2021-05-25 18:09:34 +0100870 /*
871 * Set the PSTATE bits not described in the Aarch64.TakeException
872 * pseudocode to their default values.
873 */
874 set_unset_pstate_bits
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100875 ret
Daniel Boulby95fb1aa2022-01-19 11:20:05 +0000876endfunc prepare_el3_entry
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100877
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100878/* ------------------------------------------------------------------
879 * This function restores ARMv8.3-PAuth (if enabled) and all general
880 * purpose registers except x30 from the CPU context.
881 * x30 register must be explicitly restored by the caller.
882 * ------------------------------------------------------------------
Jeenu Viswambharan23d05a82017-11-29 16:59:34 +0000883 */
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100884func restore_gp_pmcr_pauth_regs
885#if CTX_INCLUDE_PAUTH_REGS
886 /* Restore the ARMv8.3 PAuth keys */
887 add x10, sp, #CTX_PAUTH_REGS_OFFSET
888
889 ldp x0, x1, [x10, #CTX_PACIAKEY_LO] /* x1:x0 = APIAKey */
890 ldp x2, x3, [x10, #CTX_PACIBKEY_LO] /* x3:x2 = APIBKey */
891 ldp x4, x5, [x10, #CTX_PACDAKEY_LO] /* x5:x4 = APDAKey */
892 ldp x6, x7, [x10, #CTX_PACDBKEY_LO] /* x7:x6 = APDBKey */
893 ldp x8, x9, [x10, #CTX_PACGAKEY_LO] /* x9:x8 = APGAKey */
894
895 msr APIAKeyLo_EL1, x0
896 msr APIAKeyHi_EL1, x1
897 msr APIBKeyLo_EL1, x2
898 msr APIBKeyHi_EL1, x3
899 msr APDAKeyLo_EL1, x4
900 msr APDAKeyHi_EL1, x5
901 msr APDBKeyLo_EL1, x6
902 msr APDBKeyHi_EL1, x7
903 msr APGAKeyLo_EL1, x8
904 msr APGAKeyHi_EL1, x9
905#endif /* CTX_INCLUDE_PAUTH_REGS */
906
907 /* ----------------------------------------------------------
908 * Restore PMCR_EL0 when returning to Non-secure state if
909 * Secure Cycle Counter is not disabled in MDCR_EL3 when
910 * ARMv8.5-PMU is implemented.
911 * ----------------------------------------------------------
912 */
913 mrs x0, scr_el3
914 tst x0, #SCR_NS_BIT
915 beq 2f
916
917 /* ----------------------------------------------------------
918 * Back to Non-secure state.
Alexei Fedorov307f34b2021-05-14 11:21:56 +0100919 * Check if earlier initialization MDCR_EL3.SCCD/MCCD to 1
920 * failed, meaning that FEAT_PMUv3p5/7 is not implemented and
921 * PMCR_EL0 should be restored from non-secure context.
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100922 * ----------------------------------------------------------
923 */
Alexei Fedorov307f34b2021-05-14 11:21:56 +0100924 mov_imm x1, (MDCR_SCCD_BIT | MDCR_MCCD_BIT)
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100925 mrs x0, mdcr_el3
Alexei Fedorov307f34b2021-05-14 11:21:56 +0100926 tst x0, x1
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100927 bne 2f
928 ldr x0, [sp, #CTX_EL3STATE_OFFSET + CTX_PMCR_EL0]
929 msr pmcr_el0, x0
9302:
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100931 ldp x0, x1, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X0]
932 ldp x2, x3, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X2]
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100933 ldp x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4]
934 ldp x6, x7, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X6]
935 ldp x8, x9, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X8]
936 ldp x10, x11, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X10]
937 ldp x12, x13, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X12]
938 ldp x14, x15, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X14]
Jeenu Viswambharan23d05a82017-11-29 16:59:34 +0000939 ldp x16, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X16]
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100940 ldp x18, x19, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X18]
941 ldp x20, x21, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X20]
942 ldp x22, x23, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X22]
943 ldp x24, x25, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X24]
944 ldp x26, x27, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X26]
Jeenu Viswambharan23d05a82017-11-29 16:59:34 +0000945 ldr x28, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_SP_EL0]
946 msr sp_el0, x28
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100947 ldp x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28]
Jeenu Viswambharan23d05a82017-11-29 16:59:34 +0000948 ret
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100949endfunc restore_gp_pmcr_pauth_regs
Jeenu Viswambharan23d05a82017-11-29 16:59:34 +0000950
Manish V Badarkhee07e8082020-07-23 12:43:25 +0100951/*
952 * In case of ERRATA_SPECULATIVE_AT, save SCTLR_EL1 and TCR_EL1
953 * registers and update EL1 registers to disable stage1 and stage2
954 * page table walk
955 */
956func save_and_update_ptw_el1_sys_regs
957 /* ----------------------------------------------------------
958 * Save only sctlr_el1 and tcr_el1 registers
959 * ----------------------------------------------------------
960 */
961 mrs x29, sctlr_el1
962 str x29, [sp, #(CTX_EL1_SYSREGS_OFFSET + CTX_SCTLR_EL1)]
963 mrs x29, tcr_el1
964 str x29, [sp, #(CTX_EL1_SYSREGS_OFFSET + CTX_TCR_EL1)]
965
966 /* ------------------------------------------------------------
967 * Must follow below order in order to disable page table
968 * walk for lower ELs (EL1 and EL0). First step ensures that
969 * page table walk is disabled for stage1 and second step
970 * ensures that page table walker should use TCR_EL1.EPDx
971 * bits to perform address translation. ISB ensures that CPU
972 * does these 2 steps in order.
973 *
974 * 1. Update TCR_EL1.EPDx bits to disable page table walk by
975 * stage1.
976 * 2. Enable MMU bit to avoid identity mapping via stage2
977 * and force TCR_EL1.EPDx to be used by the page table
978 * walker.
979 * ------------------------------------------------------------
980 */
981 orr x29, x29, #(TCR_EPD0_BIT)
982 orr x29, x29, #(TCR_EPD1_BIT)
983 msr tcr_el1, x29
984 isb
985 mrs x29, sctlr_el1
986 orr x29, x29, #SCTLR_M_BIT
987 msr sctlr_el1, x29
988 isb
989
990 ret
991endfunc save_and_update_ptw_el1_sys_regs
992
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100993/* ------------------------------------------------------------------
994 * This routine assumes that the SP_EL3 is pointing to a valid
995 * context structure from where the gp regs and other special
996 * registers can be retrieved.
997 * ------------------------------------------------------------------
Antonio Nino Diaz13adfb12019-01-30 20:41:31 +0000998 */
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100999func el3_exit
Jan Dabrosfa015982019-12-02 13:30:03 +01001000#if ENABLE_ASSERTIONS
1001 /* el3_exit assumes SP_EL0 on entry */
1002 mrs x17, spsel
1003 cmp x17, #MODE_SP_EL0
1004 ASM_ASSERT(eq)
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +00001005#endif /* ENABLE_ASSERTIONS */
Jan Dabrosfa015982019-12-02 13:30:03 +01001006
Alexei Fedorovf41355c2019-09-13 14:11:59 +01001007 /* ----------------------------------------------------------
1008 * Save the current SP_EL0 i.e. the EL3 runtime stack which
1009 * will be used for handling the next SMC.
1010 * Then switch to SP_EL3.
1011 * ----------------------------------------------------------
Yatharth Kochar6c0566c2015-10-02 17:56:48 +01001012 */
1013 mov x17, sp
Alexei Fedorovf41355c2019-09-13 14:11:59 +01001014 msr spsel, #MODE_SP_ELX
Yatharth Kochar6c0566c2015-10-02 17:56:48 +01001015 str x17, [sp, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
1016
Alexei Fedorovf41355c2019-09-13 14:11:59 +01001017 /* ----------------------------------------------------------
Yatharth Kochar6c0566c2015-10-02 17:56:48 +01001018 * Restore SPSR_EL3, ELR_EL3 and SCR_EL3 prior to ERET
Alexei Fedorovf41355c2019-09-13 14:11:59 +01001019 * ----------------------------------------------------------
Yatharth Kochar6c0566c2015-10-02 17:56:48 +01001020 */
1021 ldr x18, [sp, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3]
1022 ldp x16, x17, [sp, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
1023 msr scr_el3, x18
1024 msr spsr_el3, x16
1025 msr elr_el3, x17
1026
Max Shvetsovc4502772021-03-22 11:59:37 +00001027#if IMAGE_BL31
1028 /* ----------------------------------------------------------
Arunachalam Ganapathycac7d162021-07-08 09:35:57 +01001029 * Restore CPTR_EL3.
Max Shvetsovc4502772021-03-22 11:59:37 +00001030 * ZCR is only restored if SVE is supported and enabled.
1031 * Synchronization is required before zcr_el3 is addressed.
1032 * ----------------------------------------------------------
1033 */
Max Shvetsovc4502772021-03-22 11:59:37 +00001034 ldp x19, x20, [sp, #CTX_EL3STATE_OFFSET + CTX_CPTR_EL3]
1035 msr cptr_el3, x19
1036
1037 ands x19, x19, #CPTR_EZ_BIT
1038 beq sve_not_enabled
1039
1040 isb
1041 msr S3_6_C1_C2_0, x20 /* zcr_el3 */
1042sve_not_enabled:
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +00001043#endif /* IMAGE_BL31 */
Max Shvetsovc4502772021-03-22 11:59:37 +00001044
Dimitris Papastamosba51d9e2018-05-16 11:36:14 +01001045#if IMAGE_BL31 && DYNAMIC_WORKAROUND_CVE_2018_3639
Alexei Fedorovf41355c2019-09-13 14:11:59 +01001046 /* ----------------------------------------------------------
1047 * Restore mitigation state as it was on entry to EL3
1048 * ----------------------------------------------------------
1049 */
Dimitris Papastamosba51d9e2018-05-16 11:36:14 +01001050 ldr x17, [sp, #CTX_CVE_2018_3639_OFFSET + CTX_CVE_2018_3639_DISABLE]
Alexei Fedorovf41355c2019-09-13 14:11:59 +01001051 cbz x17, 1f
Dimitris Papastamosba51d9e2018-05-16 11:36:14 +01001052 blr x17
Antonio Nino Diaz13adfb12019-01-30 20:41:31 +000010531:
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +00001054#endif /* IMAGE_BL31 && DYNAMIC_WORKAROUND_CVE_2018_3639 */
1055
Manish V Badarkhee07e8082020-07-23 12:43:25 +01001056 restore_ptw_el1_sys_regs
1057
Alexei Fedorovf41355c2019-09-13 14:11:59 +01001058 /* ----------------------------------------------------------
1059 * Restore general purpose (including x30), PMCR_EL0 and
1060 * ARMv8.3-PAuth registers.
1061 * Exit EL3 via ERET to a lower exception level.
1062 * ----------------------------------------------------------
1063 */
1064 bl restore_gp_pmcr_pauth_regs
1065 ldr x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
Dimitris Papastamosba51d9e2018-05-16 11:36:14 +01001066
Alexei Fedorovf41355c2019-09-13 14:11:59 +01001067#if IMAGE_BL31 && RAS_EXTENSION
1068 /* ----------------------------------------------------------
1069 * Issue Error Synchronization Barrier to synchronize SErrors
1070 * before exiting EL3. We're running with EAs unmasked, so
1071 * any synchronized errors would be taken immediately;
1072 * therefore no need to inspect DISR_EL1 register.
1073 * ----------------------------------------------------------
1074 */
1075 esb
Madhukar Pappireddyfba25722020-07-24 03:27:12 -05001076#else
1077 dsb sy
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +00001078#endif /* IMAGE_BL31 && RAS_EXTENSION */
1079
Madhukar Pappireddyfba25722020-07-24 03:27:12 -05001080#ifdef IMAGE_BL31
1081 str xzr, [sp, #CTX_EL3STATE_OFFSET + CTX_IS_IN_EL3]
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +00001082#endif /* IMAGE_BL31 */
1083
Anthony Steinhauser0f7e6012020-01-07 15:44:06 -08001084 exception_return
Antonio Nino Diaz594811b2019-01-31 11:58:00 +00001085
Yatharth Kochar6c0566c2015-10-02 17:56:48 +01001086endfunc el3_exit