blob: baabd3ba51e1aea7704c2b1e4bcaf3a784c3dc19 [file] [log] [blame]
Achin Gupta9ac63c52014-01-16 12:08:03 +00001/*
Rohit Mathew3dc3cad2022-11-11 18:45:11 +00002 * Copyright (c) 2013-2023, 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
Zelalem Aweke5362beb2022-04-04 17:42:48 -050014 .global el2_sysregs_context_save_common
15 .global el2_sysregs_context_restore_common
Zelalem Aweke5362beb2022-04-04 17:42:48 -050016#if CTX_INCLUDE_MTE_REGS
17 .global el2_sysregs_context_save_mte
18 .global el2_sysregs_context_restore_mte
19#endif /* CTX_INCLUDE_MTE_REGS */
Zelalem Aweke5362beb2022-04-04 17:42:48 -050020#if RAS_EXTENSION
21 .global el2_sysregs_context_save_ras
22 .global el2_sysregs_context_restore_ras
23#endif /* RAS_EXTENSION */
24#if CTX_INCLUDE_NEVE_REGS
25 .global el2_sysregs_context_save_nv2
26 .global el2_sysregs_context_restore_nv2
27#endif /* CTX_INCLUDE_NEVE_REGS */
Zelalem Aweke5362beb2022-04-04 17:42:48 -050028#if ENABLE_FEAT_CSV2_2
29 .global el2_sysregs_context_save_csv2
30 .global el2_sysregs_context_restore_csv2
31#endif /* ENABLE_FEAT_CSV2_2 */
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +000032#endif /* CTX_INCLUDE_EL2_REGS */
Max Shvetsovbdf502d2020-02-25 13:56:19 +000033
Yatharth Kochar6c0566c2015-10-02 17:56:48 +010034 .global el1_sysregs_context_save
35 .global el1_sysregs_context_restore
36#if CTX_INCLUDE_FPREGS
37 .global fpregs_context_save
38 .global fpregs_context_restore
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +000039#endif /* CTX_INCLUDE_FPREGS */
Daniel Boulby95fb1aa2022-01-19 11:20:05 +000040 .global prepare_el3_entry
Alexei Fedorovf41355c2019-09-13 14:11:59 +010041 .global restore_gp_pmcr_pauth_regs
Manish V Badarkhee07e8082020-07-23 12:43:25 +010042 .global save_and_update_ptw_el1_sys_regs
Yatharth Kochar6c0566c2015-10-02 17:56:48 +010043 .global el3_exit
44
Max Shvetsovbdf502d2020-02-25 13:56:19 +000045#if CTX_INCLUDE_EL2_REGS
46
47/* -----------------------------------------------------
Zelalem Aweke5362beb2022-04-04 17:42:48 -050048 * The following functions strictly follow the AArch64
Max Shvetsovcf784f72021-03-31 19:00:38 +010049 * PCS to use x9-x16 (temporary caller-saved registers)
Zelalem Aweke5362beb2022-04-04 17:42:48 -050050 * to save/restore EL2 system register context.
51 * el2_sysregs_context_save/restore_common functions
52 * save and restore registers that are common to all
53 * configurations. The rest of the functions save and
54 * restore EL2 system registers that are present when a
55 * particular feature is enabled. All functions assume
56 * that 'x0' is pointing to a 'el2_sys_regs' structure
57 * where the register context will be saved/restored.
Max Shvetsovc9e2c922020-02-17 16:15:47 +000058 *
59 * The following registers are not added.
60 * AMEVCNTVOFF0<n>_EL2
61 * AMEVCNTVOFF1<n>_EL2
62 * ICH_AP0R<n>_EL2
63 * ICH_AP1R<n>_EL2
64 * ICH_LR<n>_EL2
Max Shvetsovbdf502d2020-02-25 13:56:19 +000065 * -----------------------------------------------------
66 */
Zelalem Aweke5362beb2022-04-04 17:42:48 -050067func el2_sysregs_context_save_common
Max Shvetsovbdf502d2020-02-25 13:56:19 +000068 mrs x9, actlr_el2
Max Shvetsovc9e2c922020-02-17 16:15:47 +000069 mrs x10, afsr0_el2
70 stp x9, x10, [x0, #CTX_ACTLR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +000071
Max Shvetsovc9e2c922020-02-17 16:15:47 +000072 mrs x11, afsr1_el2
73 mrs x12, amair_el2
74 stp x11, x12, [x0, #CTX_AFSR1_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +000075
Max Shvetsovc9e2c922020-02-17 16:15:47 +000076 mrs x13, cnthctl_el2
Max Shvetsovcf784f72021-03-31 19:00:38 +010077 mrs x14, cntvoff_el2
Max Shvetsovc9e2c922020-02-17 16:15:47 +000078 stp x13, x14, [x0, #CTX_CNTHCTL_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +000079
Max Shvetsovcf784f72021-03-31 19:00:38 +010080 mrs x15, cptr_el2
81 str x15, [x0, #CTX_CPTR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +000082
Arunachalam Ganapathydca591b2020-05-26 11:32:35 +010083#if CTX_INCLUDE_AARCH32_REGS
Max Shvetsovcf784f72021-03-31 19:00:38 +010084 mrs x16, dbgvcr32_el2
85 str x16, [x0, #CTX_DBGVCR32_EL2]
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +000086#endif /* CTX_INCLUDE_AARCH32_REGS */
Max Shvetsovbdf502d2020-02-25 13:56:19 +000087
Max Shvetsovcf784f72021-03-31 19:00:38 +010088 mrs x9, elr_el2
89 mrs x10, esr_el2
90 stp x9, x10, [x0, #CTX_ELR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +000091
Max Shvetsovcf784f72021-03-31 19:00:38 +010092 mrs x11, far_el2
93 mrs x12, hacr_el2
94 stp x11, x12, [x0, #CTX_FAR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +000095
Max Shvetsovcf784f72021-03-31 19:00:38 +010096 mrs x13, hcr_el2
97 mrs x14, hpfar_el2
98 stp x13, x14, [x0, #CTX_HCR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +000099
Max Shvetsovcf784f72021-03-31 19:00:38 +0100100 mrs x15, hstr_el2
101 mrs x16, ICC_SRE_EL2
102 stp x15, x16, [x0, #CTX_HSTR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000103
Max Shvetsovcf784f72021-03-31 19:00:38 +0100104 mrs x9, ICH_HCR_EL2
105 mrs x10, ICH_VMCR_EL2
106 stp x9, x10, [x0, #CTX_ICH_HCR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000107
Max Shvetsovcf784f72021-03-31 19:00:38 +0100108 mrs x11, mair_el2
109 mrs x12, mdcr_el2
110 stp x11, x12, [x0, #CTX_MAIR_EL2]
111
Max Shvetsovcf784f72021-03-31 19:00:38 +0100112 mrs x14, sctlr_el2
113 str x14, [x0, #CTX_SCTLR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000114
Max Shvetsovcf784f72021-03-31 19:00:38 +0100115 mrs x15, spsr_el2
116 mrs x16, sp_el2
117 stp x15, x16, [x0, #CTX_SPSR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000118
Max Shvetsovcf784f72021-03-31 19:00:38 +0100119 mrs x9, tcr_el2
120 mrs x10, tpidr_el2
121 stp x9, x10, [x0, #CTX_TCR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000122
Max Shvetsovcf784f72021-03-31 19:00:38 +0100123 mrs x11, ttbr0_el2
124 mrs x12, vbar_el2
125 stp x11, x12, [x0, #CTX_TTBR0_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000126
Max Shvetsovcf784f72021-03-31 19:00:38 +0100127 mrs x13, vmpidr_el2
128 mrs x14, vpidr_el2
129 stp x13, x14, [x0, #CTX_VMPIDR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000130
Max Shvetsovcf784f72021-03-31 19:00:38 +0100131 mrs x15, vtcr_el2
132 mrs x16, vttbr_el2
133 stp x15, x16, [x0, #CTX_VTCR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000134 ret
Zelalem Aweke5362beb2022-04-04 17:42:48 -0500135endfunc el2_sysregs_context_save_common
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000136
Zelalem Aweke5362beb2022-04-04 17:42:48 -0500137func el2_sysregs_context_restore_common
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000138 ldp x9, x10, [x0, #CTX_ACTLR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000139 msr actlr_el2, x9
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000140 msr afsr0_el2, x10
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000141
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000142 ldp x11, x12, [x0, #CTX_AFSR1_EL2]
143 msr afsr1_el2, x11
144 msr amair_el2, x12
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000145
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000146 ldp x13, x14, [x0, #CTX_CNTHCTL_EL2]
147 msr cnthctl_el2, x13
Max Shvetsovcf784f72021-03-31 19:00:38 +0100148 msr cntvoff_el2, x14
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000149
Max Shvetsovcf784f72021-03-31 19:00:38 +0100150 ldr x15, [x0, #CTX_CPTR_EL2]
151 msr cptr_el2, x15
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000152
Arunachalam Ganapathydca591b2020-05-26 11:32:35 +0100153#if CTX_INCLUDE_AARCH32_REGS
Max Shvetsovcf784f72021-03-31 19:00:38 +0100154 ldr x16, [x0, #CTX_DBGVCR32_EL2]
155 msr dbgvcr32_el2, x16
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000156#endif /* CTX_INCLUDE_AARCH32_REGS */
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000157
Max Shvetsovcf784f72021-03-31 19:00:38 +0100158 ldp x9, x10, [x0, #CTX_ELR_EL2]
159 msr elr_el2, x9
160 msr esr_el2, x10
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000161
Max Shvetsovcf784f72021-03-31 19:00:38 +0100162 ldp x11, x12, [x0, #CTX_FAR_EL2]
163 msr far_el2, x11
164 msr hacr_el2, x12
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000165
Max Shvetsovcf784f72021-03-31 19:00:38 +0100166 ldp x13, x14, [x0, #CTX_HCR_EL2]
167 msr hcr_el2, x13
168 msr hpfar_el2, x14
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000169
Max Shvetsovcf784f72021-03-31 19:00:38 +0100170 ldp x15, x16, [x0, #CTX_HSTR_EL2]
171 msr hstr_el2, x15
172 msr ICC_SRE_EL2, x16
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000173
Max Shvetsovcf784f72021-03-31 19:00:38 +0100174 ldp x9, x10, [x0, #CTX_ICH_HCR_EL2]
175 msr ICH_HCR_EL2, x9
176 msr ICH_VMCR_EL2, x10
177
178 ldp x11, x12, [x0, #CTX_MAIR_EL2]
179 msr mair_el2, x11
180 msr mdcr_el2, x12
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000181
Max Shvetsovcf784f72021-03-31 19:00:38 +0100182 ldr x14, [x0, #CTX_SCTLR_EL2]
183 msr sctlr_el2, x14
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000184
Max Shvetsovcf784f72021-03-31 19:00:38 +0100185 ldp x15, x16, [x0, #CTX_SPSR_EL2]
186 msr spsr_el2, x15
187 msr sp_el2, x16
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000188
Max Shvetsovcf784f72021-03-31 19:00:38 +0100189 ldp x9, x10, [x0, #CTX_TCR_EL2]
190 msr tcr_el2, x9
191 msr tpidr_el2, x10
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000192
Max Shvetsovcf784f72021-03-31 19:00:38 +0100193 ldp x11, x12, [x0, #CTX_TTBR0_EL2]
194 msr ttbr0_el2, x11
195 msr vbar_el2, x12
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000196
Max Shvetsovcf784f72021-03-31 19:00:38 +0100197 ldp x13, x14, [x0, #CTX_VMPIDR_EL2]
198 msr vmpidr_el2, x13
199 msr vpidr_el2, x14
Manish V Badarkhed73c1ba2020-07-28 07:12:56 +0100200
Max Shvetsovcf784f72021-03-31 19:00:38 +0100201 ldp x15, x16, [x0, #CTX_VTCR_EL2]
202 msr vtcr_el2, x15
203 msr vttbr_el2, x16
Zelalem Aweke5362beb2022-04-04 17:42:48 -0500204 ret
205endfunc el2_sysregs_context_restore_common
206
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000207#if CTX_INCLUDE_MTE_REGS
Zelalem Aweke5362beb2022-04-04 17:42:48 -0500208func el2_sysregs_context_save_mte
209 mrs x9, TFSR_EL2
210 str x9, [x0, #CTX_TFSR_EL2]
211 ret
212endfunc el2_sysregs_context_save_mte
213
214func el2_sysregs_context_restore_mte
Manish V Badarkhed73c1ba2020-07-28 07:12:56 +0100215 ldr x9, [x0, #CTX_TFSR_EL2]
216 msr TFSR_EL2, x9
Zelalem Aweke5362beb2022-04-04 17:42:48 -0500217 ret
218endfunc el2_sysregs_context_restore_mte
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000219#endif /* CTX_INCLUDE_MTE_REGS */
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000220
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000221#if RAS_EXTENSION
Zelalem Aweke5362beb2022-04-04 17:42:48 -0500222func el2_sysregs_context_save_ras
223 /*
224 * VDISR_EL2 and VSESR_EL2 registers are saved only when
225 * FEAT_RAS is supported.
226 */
227 mrs x11, vdisr_el2
228 mrs x12, vsesr_el2
229 stp x11, x12, [x0, #CTX_VDISR_EL2]
230 ret
231endfunc el2_sysregs_context_save_ras
232
233func el2_sysregs_context_restore_ras
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000234 /*
235 * VDISR_EL2 and VSESR_EL2 registers are restored only when FEAT_RAS
236 * is supported.
237 */
238 ldp x11, x12, [x0, #CTX_VDISR_EL2]
239 msr vdisr_el2, x11
240 msr vsesr_el2, x12
Zelalem Aweke5362beb2022-04-04 17:42:48 -0500241 ret
242endfunc el2_sysregs_context_restore_ras
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000243#endif /* RAS_EXTENSION */
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000244
Arunachalam Ganapathydd3ec7e2020-05-28 11:57:09 +0100245#if CTX_INCLUDE_NEVE_REGS
Zelalem Aweke5362beb2022-04-04 17:42:48 -0500246func el2_sysregs_context_save_nv2
247 /*
248 * VNCR_EL2 register is saved only when FEAT_NV2 is supported.
249 */
250 mrs x16, vncr_el2
251 str x16, [x0, #CTX_VNCR_EL2]
252 ret
253endfunc el2_sysregs_context_save_nv2
254
255func el2_sysregs_context_restore_nv2
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000256 /*
257 * VNCR_EL2 register is restored only when FEAT_NV2 is supported.
258 */
Max Shvetsovcf784f72021-03-31 19:00:38 +0100259 ldr x16, [x0, #CTX_VNCR_EL2]
260 msr vncr_el2, x16
Zelalem Aweke5362beb2022-04-04 17:42:48 -0500261 ret
262endfunc el2_sysregs_context_restore_nv2
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000263#endif /* CTX_INCLUDE_NEVE_REGS */
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000264
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000265#if ENABLE_FEAT_CSV2_2
Zelalem Aweke5362beb2022-04-04 17:42:48 -0500266func el2_sysregs_context_save_csv2
267 /*
268 * SCXTNUM_EL2 register is saved only when FEAT_CSV2_2 is supported.
269 */
270 mrs x13, scxtnum_el2
271 str x13, [x0, #CTX_SCXTNUM_EL2]
272 ret
273endfunc el2_sysregs_context_save_csv2
274
275func el2_sysregs_context_restore_csv2
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000276 /*
277 * SCXTNUM_EL2 register is restored only when FEAT_CSV2_2 is supported.
278 */
Max Shvetsovcf784f72021-03-31 19:00:38 +0100279 ldr x13, [x0, #CTX_SCXTNUM_EL2]
280 msr scxtnum_el2, x13
Zelalem Aweke5362beb2022-04-04 17:42:48 -0500281 ret
282endfunc el2_sysregs_context_restore_csv2
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000283#endif /* ENABLE_FEAT_CSV2_2 */
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000284
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000285#endif /* CTX_INCLUDE_EL2_REGS */
286
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100287/* ------------------------------------------------------------------
288 * The following function strictly follows the AArch64 PCS to use
289 * x9-x17 (temporary caller-saved registers) to save EL1 system
290 * register context. It assumes that 'x0' is pointing to a
291 * 'el1_sys_regs' structure where the register context will be saved.
292 * ------------------------------------------------------------------
Achin Gupta9ac63c52014-01-16 12:08:03 +0000293 */
Andrew Thoelke38bde412014-03-18 13:46:55 +0000294func el1_sysregs_context_save
Achin Gupta9ac63c52014-01-16 12:08:03 +0000295
296 mrs x9, spsr_el1
297 mrs x10, elr_el1
298 stp x9, x10, [x0, #CTX_SPSR_EL1]
299
Manish V Badarkhee07e8082020-07-23 12:43:25 +0100300#if !ERRATA_SPECULATIVE_AT
Achin Gupta9ac63c52014-01-16 12:08:03 +0000301 mrs x15, sctlr_el1
Manish V Badarkhe2b0ee972020-07-28 07:22:30 +0100302 mrs x16, tcr_el1
Achin Gupta9ac63c52014-01-16 12:08:03 +0000303 stp x15, x16, [x0, #CTX_SCTLR_EL1]
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000304#endif /* ERRATA_SPECULATIVE_AT */
Achin Gupta9ac63c52014-01-16 12:08:03 +0000305
306 mrs x17, cpacr_el1
307 mrs x9, csselr_el1
308 stp x17, x9, [x0, #CTX_CPACR_EL1]
309
310 mrs x10, sp_el1
311 mrs x11, esr_el1
312 stp x10, x11, [x0, #CTX_SP_EL1]
313
314 mrs x12, ttbr0_el1
315 mrs x13, ttbr1_el1
316 stp x12, x13, [x0, #CTX_TTBR0_EL1]
317
318 mrs x14, mair_el1
319 mrs x15, amair_el1
320 stp x14, x15, [x0, #CTX_MAIR_EL1]
321
Manish V Badarkhe2b0ee972020-07-28 07:22:30 +0100322 mrs x16, actlr_el1
Achin Gupta9ac63c52014-01-16 12:08:03 +0000323 mrs x17, tpidr_el1
Manish V Badarkhe2b0ee972020-07-28 07:22:30 +0100324 stp x16, x17, [x0, #CTX_ACTLR_EL1]
Achin Gupta9ac63c52014-01-16 12:08:03 +0000325
326 mrs x9, tpidr_el0
327 mrs x10, tpidrro_el0
328 stp x9, x10, [x0, #CTX_TPIDR_EL0]
329
Achin Gupta9ac63c52014-01-16 12:08:03 +0000330 mrs x13, par_el1
331 mrs x14, far_el1
332 stp x13, x14, [x0, #CTX_PAR_EL1]
333
334 mrs x15, afsr0_el1
335 mrs x16, afsr1_el1
336 stp x15, x16, [x0, #CTX_AFSR0_EL1]
337
338 mrs x17, contextidr_el1
339 mrs x9, vbar_el1
340 stp x17, x9, [x0, #CTX_CONTEXTIDR_EL1]
341
Soby Mathewd75d2ba2016-05-17 14:01:32 +0100342 /* Save AArch32 system registers if the build has instructed so */
343#if CTX_INCLUDE_AARCH32_REGS
344 mrs x11, spsr_abt
345 mrs x12, spsr_und
346 stp x11, x12, [x0, #CTX_SPSR_ABT]
347
348 mrs x13, spsr_irq
349 mrs x14, spsr_fiq
350 stp x13, x14, [x0, #CTX_SPSR_IRQ]
351
352 mrs x15, dacr32_el2
353 mrs x16, ifsr32_el2
354 stp x15, x16, [x0, #CTX_DACR32_EL2]
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000355#endif /* CTX_INCLUDE_AARCH32_REGS */
Soby Mathewd75d2ba2016-05-17 14:01:32 +0100356
Jeenu Viswambharand1b60152014-05-12 15:28:47 +0100357 /* Save NS timer registers if the build has instructed so */
358#if NS_TIMER_SWITCH
Achin Gupta9ac63c52014-01-16 12:08:03 +0000359 mrs x10, cntp_ctl_el0
360 mrs x11, cntp_cval_el0
361 stp x10, x11, [x0, #CTX_CNTP_CTL_EL0]
362
363 mrs x12, cntv_ctl_el0
364 mrs x13, cntv_cval_el0
365 stp x12, x13, [x0, #CTX_CNTV_CTL_EL0]
366
367 mrs x14, cntkctl_el1
Jeenu Viswambharand1b60152014-05-12 15:28:47 +0100368 str x14, [x0, #CTX_CNTKCTL_EL1]
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000369#endif /* NS_TIMER_SWITCH */
Jeenu Viswambharand1b60152014-05-12 15:28:47 +0100370
Justin Chadwell1c7c13a2019-07-18 14:25:33 +0100371 /* Save MTE system registers if the build has instructed so */
372#if CTX_INCLUDE_MTE_REGS
373 mrs x15, TFSRE0_EL1
374 mrs x16, TFSR_EL1
375 stp x15, x16, [x0, #CTX_TFSRE0_EL1]
376
377 mrs x9, RGSR_EL1
378 mrs x10, GCR_EL1
379 stp x9, x10, [x0, #CTX_RGSR_EL1]
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000380#endif /* CTX_INCLUDE_MTE_REGS */
Justin Chadwell1c7c13a2019-07-18 14:25:33 +0100381
Achin Gupta9ac63c52014-01-16 12:08:03 +0000382 ret
Kévin Petita877c252015-03-24 14:03:57 +0000383endfunc el1_sysregs_context_save
Achin Gupta9ac63c52014-01-16 12:08:03 +0000384
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100385/* ------------------------------------------------------------------
386 * The following function strictly follows the AArch64 PCS to use
387 * x9-x17 (temporary caller-saved registers) to restore EL1 system
388 * register context. It assumes that 'x0' is pointing to a
389 * 'el1_sys_regs' structure from where the register context will be
390 * restored
391 * ------------------------------------------------------------------
Achin Gupta9ac63c52014-01-16 12:08:03 +0000392 */
Andrew Thoelke38bde412014-03-18 13:46:55 +0000393func el1_sysregs_context_restore
Achin Gupta9ac63c52014-01-16 12:08:03 +0000394
395 ldp x9, x10, [x0, #CTX_SPSR_EL1]
396 msr spsr_el1, x9
397 msr elr_el1, x10
398
Manish V Badarkhee07e8082020-07-23 12:43:25 +0100399#if !ERRATA_SPECULATIVE_AT
Manish V Badarkhed73c1ba2020-07-28 07:12:56 +0100400 ldp x15, x16, [x0, #CTX_SCTLR_EL1]
401 msr sctlr_el1, x15
Manish V Badarkhe2b0ee972020-07-28 07:22:30 +0100402 msr tcr_el1, x16
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000403#endif /* ERRATA_SPECULATIVE_AT */
Achin Gupta9ac63c52014-01-16 12:08:03 +0000404
405 ldp x17, x9, [x0, #CTX_CPACR_EL1]
406 msr cpacr_el1, x17
407 msr csselr_el1, x9
408
409 ldp x10, x11, [x0, #CTX_SP_EL1]
410 msr sp_el1, x10
411 msr esr_el1, x11
412
413 ldp x12, x13, [x0, #CTX_TTBR0_EL1]
414 msr ttbr0_el1, x12
415 msr ttbr1_el1, x13
416
417 ldp x14, x15, [x0, #CTX_MAIR_EL1]
418 msr mair_el1, x14
419 msr amair_el1, x15
420
Manish V Badarkhe2b0ee972020-07-28 07:22:30 +0100421 ldp x16, x17, [x0, #CTX_ACTLR_EL1]
422 msr actlr_el1, x16
Manish V Badarkhed73c1ba2020-07-28 07:12:56 +0100423 msr tpidr_el1, x17
Achin Gupta9ac63c52014-01-16 12:08:03 +0000424
425 ldp x9, x10, [x0, #CTX_TPIDR_EL0]
426 msr tpidr_el0, x9
427 msr tpidrro_el0, x10
428
Achin Gupta9ac63c52014-01-16 12:08:03 +0000429 ldp x13, x14, [x0, #CTX_PAR_EL1]
430 msr par_el1, x13
431 msr far_el1, x14
432
433 ldp x15, x16, [x0, #CTX_AFSR0_EL1]
434 msr afsr0_el1, x15
435 msr afsr1_el1, x16
436
437 ldp x17, x9, [x0, #CTX_CONTEXTIDR_EL1]
438 msr contextidr_el1, x17
439 msr vbar_el1, x9
440
Soby Mathewd75d2ba2016-05-17 14:01:32 +0100441 /* Restore AArch32 system registers if the build has instructed so */
442#if CTX_INCLUDE_AARCH32_REGS
443 ldp x11, x12, [x0, #CTX_SPSR_ABT]
444 msr spsr_abt, x11
445 msr spsr_und, x12
446
447 ldp x13, x14, [x0, #CTX_SPSR_IRQ]
448 msr spsr_irq, x13
449 msr spsr_fiq, x14
450
451 ldp x15, x16, [x0, #CTX_DACR32_EL2]
452 msr dacr32_el2, x15
453 msr ifsr32_el2, x16
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000454#endif /* CTX_INCLUDE_AARCH32_REGS */
455
Jeenu Viswambharand1b60152014-05-12 15:28:47 +0100456 /* Restore NS timer registers if the build has instructed so */
457#if NS_TIMER_SWITCH
Achin Gupta9ac63c52014-01-16 12:08:03 +0000458 ldp x10, x11, [x0, #CTX_CNTP_CTL_EL0]
459 msr cntp_ctl_el0, x10
460 msr cntp_cval_el0, x11
461
462 ldp x12, x13, [x0, #CTX_CNTV_CTL_EL0]
463 msr cntv_ctl_el0, x12
464 msr cntv_cval_el0, x13
465
Jeenu Viswambharand1b60152014-05-12 15:28:47 +0100466 ldr x14, [x0, #CTX_CNTKCTL_EL1]
Achin Gupta9ac63c52014-01-16 12:08:03 +0000467 msr cntkctl_el1, x14
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000468#endif /* NS_TIMER_SWITCH */
469
Justin Chadwell1c7c13a2019-07-18 14:25:33 +0100470 /* Restore MTE system registers if the build has instructed so */
471#if CTX_INCLUDE_MTE_REGS
472 ldp x11, x12, [x0, #CTX_TFSRE0_EL1]
473 msr TFSRE0_EL1, x11
474 msr TFSR_EL1, x12
475
476 ldp x13, x14, [x0, #CTX_RGSR_EL1]
477 msr RGSR_EL1, x13
478 msr GCR_EL1, x14
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000479#endif /* CTX_INCLUDE_MTE_REGS */
Jeenu Viswambharand1b60152014-05-12 15:28:47 +0100480
Achin Gupta9ac63c52014-01-16 12:08:03 +0000481 /* No explict ISB required here as ERET covers it */
Achin Gupta9ac63c52014-01-16 12:08:03 +0000482 ret
Kévin Petita877c252015-03-24 14:03:57 +0000483endfunc el1_sysregs_context_restore
Achin Gupta9ac63c52014-01-16 12:08:03 +0000484
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100485/* ------------------------------------------------------------------
486 * The following function follows the aapcs_64 strictly to use
487 * x9-x17 (temporary caller-saved registers according to AArch64 PCS)
488 * to save floating point register context. It assumes that 'x0' is
489 * pointing to a 'fp_regs' structure where the register context will
Achin Gupta9ac63c52014-01-16 12:08:03 +0000490 * be saved.
491 *
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100492 * Access to VFP registers will trap if CPTR_EL3.TFP is set.
493 * However currently we don't use VFP registers nor set traps in
494 * Trusted Firmware, and assume it's cleared.
Achin Gupta9ac63c52014-01-16 12:08:03 +0000495 *
496 * TODO: Revisit when VFP is used in secure world
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100497 * ------------------------------------------------------------------
Achin Gupta9ac63c52014-01-16 12:08:03 +0000498 */
Juan Castillo258e94f2014-06-25 17:26:36 +0100499#if CTX_INCLUDE_FPREGS
Andrew Thoelke38bde412014-03-18 13:46:55 +0000500func fpregs_context_save
Achin Gupta9ac63c52014-01-16 12:08:03 +0000501 stp q0, q1, [x0, #CTX_FP_Q0]
502 stp q2, q3, [x0, #CTX_FP_Q2]
503 stp q4, q5, [x0, #CTX_FP_Q4]
504 stp q6, q7, [x0, #CTX_FP_Q6]
505 stp q8, q9, [x0, #CTX_FP_Q8]
506 stp q10, q11, [x0, #CTX_FP_Q10]
507 stp q12, q13, [x0, #CTX_FP_Q12]
508 stp q14, q15, [x0, #CTX_FP_Q14]
509 stp q16, q17, [x0, #CTX_FP_Q16]
510 stp q18, q19, [x0, #CTX_FP_Q18]
511 stp q20, q21, [x0, #CTX_FP_Q20]
512 stp q22, q23, [x0, #CTX_FP_Q22]
513 stp q24, q25, [x0, #CTX_FP_Q24]
514 stp q26, q27, [x0, #CTX_FP_Q26]
515 stp q28, q29, [x0, #CTX_FP_Q28]
516 stp q30, q31, [x0, #CTX_FP_Q30]
517
518 mrs x9, fpsr
519 str x9, [x0, #CTX_FP_FPSR]
520
521 mrs x10, fpcr
522 str x10, [x0, #CTX_FP_FPCR]
523
David Cunadod1a1fd42017-10-20 11:30:57 +0100524#if CTX_INCLUDE_AARCH32_REGS
525 mrs x11, fpexc32_el2
526 str x11, [x0, #CTX_FP_FPEXC32_EL2]
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000527#endif /* CTX_INCLUDE_AARCH32_REGS */
Achin Gupta9ac63c52014-01-16 12:08:03 +0000528 ret
Kévin Petita877c252015-03-24 14:03:57 +0000529endfunc fpregs_context_save
Achin Gupta9ac63c52014-01-16 12:08:03 +0000530
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100531/* ------------------------------------------------------------------
532 * The following function follows the aapcs_64 strictly to use x9-x17
533 * (temporary caller-saved registers according to AArch64 PCS) to
534 * restore floating point register context. It assumes that 'x0' is
535 * pointing to a 'fp_regs' structure from where the register context
Achin Gupta9ac63c52014-01-16 12:08:03 +0000536 * will be restored.
537 *
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100538 * Access to VFP registers will trap if CPTR_EL3.TFP is set.
539 * However currently we don't use VFP registers nor set traps in
540 * Trusted Firmware, and assume it's cleared.
Achin Gupta9ac63c52014-01-16 12:08:03 +0000541 *
542 * TODO: Revisit when VFP is used in secure world
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100543 * ------------------------------------------------------------------
Achin Gupta9ac63c52014-01-16 12:08:03 +0000544 */
Andrew Thoelke38bde412014-03-18 13:46:55 +0000545func fpregs_context_restore
Achin Gupta9ac63c52014-01-16 12:08:03 +0000546 ldp q0, q1, [x0, #CTX_FP_Q0]
547 ldp q2, q3, [x0, #CTX_FP_Q2]
548 ldp q4, q5, [x0, #CTX_FP_Q4]
549 ldp q6, q7, [x0, #CTX_FP_Q6]
550 ldp q8, q9, [x0, #CTX_FP_Q8]
551 ldp q10, q11, [x0, #CTX_FP_Q10]
552 ldp q12, q13, [x0, #CTX_FP_Q12]
553 ldp q14, q15, [x0, #CTX_FP_Q14]
554 ldp q16, q17, [x0, #CTX_FP_Q16]
555 ldp q18, q19, [x0, #CTX_FP_Q18]
556 ldp q20, q21, [x0, #CTX_FP_Q20]
557 ldp q22, q23, [x0, #CTX_FP_Q22]
558 ldp q24, q25, [x0, #CTX_FP_Q24]
559 ldp q26, q27, [x0, #CTX_FP_Q26]
560 ldp q28, q29, [x0, #CTX_FP_Q28]
561 ldp q30, q31, [x0, #CTX_FP_Q30]
562
563 ldr x9, [x0, #CTX_FP_FPSR]
564 msr fpsr, x9
565
Soby Mathewe77e1162015-12-03 09:42:50 +0000566 ldr x10, [x0, #CTX_FP_FPCR]
Achin Gupta9ac63c52014-01-16 12:08:03 +0000567 msr fpcr, x10
568
David Cunadod1a1fd42017-10-20 11:30:57 +0100569#if CTX_INCLUDE_AARCH32_REGS
570 ldr x11, [x0, #CTX_FP_FPEXC32_EL2]
571 msr fpexc32_el2, x11
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000572#endif /* CTX_INCLUDE_AARCH32_REGS */
573
Achin Gupta9ac63c52014-01-16 12:08:03 +0000574 /*
575 * No explict ISB required here as ERET to
Sandrine Bailleuxf4119ec2015-12-17 13:58:58 +0000576 * switch to secure EL1 or non-secure world
Achin Gupta9ac63c52014-01-16 12:08:03 +0000577 * covers it
578 */
579
580 ret
Kévin Petita877c252015-03-24 14:03:57 +0000581endfunc fpregs_context_restore
Juan Castillo258e94f2014-06-25 17:26:36 +0100582#endif /* CTX_INCLUDE_FPREGS */
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100583
Daniel Boulby928747f2021-05-25 18:09:34 +0100584 /*
Manish Pandey62d532a2022-11-17 15:47:05 +0000585 * Set SCR_EL3.EA bit to enable SErrors at EL3
586 */
587 .macro enable_serror_at_el3
588 mrs x8, scr_el3
589 orr x8, x8, #SCR_EA_BIT
590 msr scr_el3, x8
591 .endm
592
593 /*
Daniel Boulby928747f2021-05-25 18:09:34 +0100594 * Set the PSTATE bits not set when the exception was taken as
595 * described in the AArch64.TakeException() pseudocode function
596 * in ARM DDI 0487F.c page J1-7635 to a default value.
597 */
598 .macro set_unset_pstate_bits
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000599 /*
600 * If Data Independent Timing (DIT) functionality is implemented,
601 * always enable DIT in EL3
602 */
Daniel Boulby928747f2021-05-25 18:09:34 +0100603#if ENABLE_FEAT_DIT
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000604 mov x8, #DIT_BIT
605 msr DIT, x8
Daniel Boulby928747f2021-05-25 18:09:34 +0100606#endif /* ENABLE_FEAT_DIT */
607 .endm /* set_unset_pstate_bits */
608
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100609/* ------------------------------------------------------------------
Daniel Boulby95fb1aa2022-01-19 11:20:05 +0000610 * The following macro is used to save and restore all the general
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100611 * purpose and ARMv8.3-PAuth (if enabled) registers.
Jayanth Dodderi Chidanand4ec78ad2022-09-19 23:32:08 +0100612 * It also checks if the Secure Cycle Counter (PMCCNTR_EL0)
613 * is disabled in EL3/Secure (ARMv8.5-PMU), wherein PMCCNTR_EL0
614 * needs not to be saved/restored during world switch.
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100615 *
616 * Ideally we would only save and restore the callee saved registers
617 * when a world switch occurs but that type of implementation is more
618 * complex. So currently we will always save and restore these
619 * registers on entry and exit of EL3.
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100620 * clobbers: x18
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100621 * ------------------------------------------------------------------
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100622 */
Daniel Boulby95fb1aa2022-01-19 11:20:05 +0000623 .macro save_gp_pmcr_pauth_regs
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100624 stp x0, x1, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X0]
625 stp x2, x3, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X2]
626 stp x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4]
627 stp x6, x7, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X6]
628 stp x8, x9, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X8]
629 stp x10, x11, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X10]
630 stp x12, x13, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X12]
631 stp x14, x15, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X14]
632 stp x16, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X16]
633 stp x18, x19, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X18]
634 stp x20, x21, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X20]
635 stp x22, x23, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X22]
636 stp x24, x25, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X24]
637 stp x26, x27, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X26]
638 stp x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28]
639 mrs x18, sp_el0
640 str x18, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_SP_EL0]
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100641
642 /* ----------------------------------------------------------
Jayanth Dodderi Chidanand4ec78ad2022-09-19 23:32:08 +0100643 * Check if earlier initialization of MDCR_EL3.SCCD/MCCD to 1
644 * has failed.
645 *
646 * MDCR_EL3:
647 * MCCD bit set, Prohibits the Cycle Counter PMCCNTR_EL0 from
648 * counting at EL3.
649 * SCCD bit set, Secure Cycle Counter Disable. Prohibits PMCCNTR_EL0
650 * from counting in Secure state.
651 * If these bits are not set, meaning that FEAT_PMUv3p5/7 is
652 * not implemented and PMCR_EL0 should be saved in non-secure
653 * context.
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100654 * ----------------------------------------------------------
655 */
Alexei Fedorov307f34b2021-05-14 11:21:56 +0100656 mov_imm x10, (MDCR_SCCD_BIT | MDCR_MCCD_BIT)
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100657 mrs x9, mdcr_el3
Alexei Fedorov307f34b2021-05-14 11:21:56 +0100658 tst x9, x10
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100659 bne 1f
660
Jayanth Dodderi Chidanand4ec78ad2022-09-19 23:32:08 +0100661 /* ----------------------------------------------------------
662 * If control reaches here, it ensures the Secure Cycle
663 * Counter (PMCCNTR_EL0) is not prohibited from counting at
664 * EL3 and in secure states.
665 * Henceforth, PMCR_EL0 to be saved before world switch.
666 * ----------------------------------------------------------
667 */
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100668 mrs x9, pmcr_el0
669
670 /* Check caller's security state */
671 mrs x10, scr_el3
672 tst x10, #SCR_NS_BIT
673 beq 2f
674
675 /* Save PMCR_EL0 if called from Non-secure state */
676 str x9, [sp, #CTX_EL3STATE_OFFSET + CTX_PMCR_EL0]
677
678 /* Disable cycle counter when event counting is prohibited */
6792: orr x9, x9, #PMCR_EL0_DP_BIT
680 msr pmcr_el0, x9
681 isb
6821:
683#if CTX_INCLUDE_PAUTH_REGS
684 /* ----------------------------------------------------------
685 * Save the ARMv8.3-PAuth keys as they are not banked
686 * by exception level
687 * ----------------------------------------------------------
688 */
689 add x19, sp, #CTX_PAUTH_REGS_OFFSET
690
691 mrs x20, APIAKeyLo_EL1 /* x21:x20 = APIAKey */
692 mrs x21, APIAKeyHi_EL1
693 mrs x22, APIBKeyLo_EL1 /* x23:x22 = APIBKey */
694 mrs x23, APIBKeyHi_EL1
695 mrs x24, APDAKeyLo_EL1 /* x25:x24 = APDAKey */
696 mrs x25, APDAKeyHi_EL1
697 mrs x26, APDBKeyLo_EL1 /* x27:x26 = APDBKey */
698 mrs x27, APDBKeyHi_EL1
699 mrs x28, APGAKeyLo_EL1 /* x29:x28 = APGAKey */
700 mrs x29, APGAKeyHi_EL1
701
702 stp x20, x21, [x19, #CTX_PACIAKEY_LO]
703 stp x22, x23, [x19, #CTX_PACIBKEY_LO]
704 stp x24, x25, [x19, #CTX_PACDAKEY_LO]
705 stp x26, x27, [x19, #CTX_PACDBKEY_LO]
706 stp x28, x29, [x19, #CTX_PACGAKEY_LO]
707#endif /* CTX_INCLUDE_PAUTH_REGS */
Daniel Boulby95fb1aa2022-01-19 11:20:05 +0000708 .endm /* save_gp_pmcr_pauth_regs */
709
710/* -----------------------------------------------------------------
Daniel Boulby928747f2021-05-25 18:09:34 +0100711 * This function saves the context and sets the PSTATE to a known
712 * state, preparing entry to el3.
Daniel Boulby95fb1aa2022-01-19 11:20:05 +0000713 * Save all the general purpose and ARMv8.3-PAuth (if enabled)
714 * registers.
Daniel Boulby928747f2021-05-25 18:09:34 +0100715 * Then set any of the PSTATE bits that are not set by hardware
716 * according to the Aarch64.TakeException pseudocode in the Arm
717 * Architecture Reference Manual to a default value for EL3.
718 * clobbers: x17
Daniel Boulby95fb1aa2022-01-19 11:20:05 +0000719 * -----------------------------------------------------------------
720 */
721func prepare_el3_entry
722 save_gp_pmcr_pauth_regs
Manish Pandey62d532a2022-11-17 15:47:05 +0000723 enable_serror_at_el3
Daniel Boulby928747f2021-05-25 18:09:34 +0100724 /*
725 * Set the PSTATE bits not described in the Aarch64.TakeException
726 * pseudocode to their default values.
727 */
728 set_unset_pstate_bits
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100729 ret
Daniel Boulby95fb1aa2022-01-19 11:20:05 +0000730endfunc prepare_el3_entry
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100731
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100732/* ------------------------------------------------------------------
733 * This function restores ARMv8.3-PAuth (if enabled) and all general
734 * purpose registers except x30 from the CPU context.
735 * x30 register must be explicitly restored by the caller.
736 * ------------------------------------------------------------------
Jeenu Viswambharan23d05a82017-11-29 16:59:34 +0000737 */
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100738func restore_gp_pmcr_pauth_regs
739#if CTX_INCLUDE_PAUTH_REGS
740 /* Restore the ARMv8.3 PAuth keys */
741 add x10, sp, #CTX_PAUTH_REGS_OFFSET
742
743 ldp x0, x1, [x10, #CTX_PACIAKEY_LO] /* x1:x0 = APIAKey */
744 ldp x2, x3, [x10, #CTX_PACIBKEY_LO] /* x3:x2 = APIBKey */
745 ldp x4, x5, [x10, #CTX_PACDAKEY_LO] /* x5:x4 = APDAKey */
746 ldp x6, x7, [x10, #CTX_PACDBKEY_LO] /* x7:x6 = APDBKey */
747 ldp x8, x9, [x10, #CTX_PACGAKEY_LO] /* x9:x8 = APGAKey */
748
749 msr APIAKeyLo_EL1, x0
750 msr APIAKeyHi_EL1, x1
751 msr APIBKeyLo_EL1, x2
752 msr APIBKeyHi_EL1, x3
753 msr APDAKeyLo_EL1, x4
754 msr APDAKeyHi_EL1, x5
755 msr APDBKeyLo_EL1, x6
756 msr APDBKeyHi_EL1, x7
757 msr APGAKeyLo_EL1, x8
758 msr APGAKeyHi_EL1, x9
759#endif /* CTX_INCLUDE_PAUTH_REGS */
760
761 /* ----------------------------------------------------------
762 * Restore PMCR_EL0 when returning to Non-secure state if
763 * Secure Cycle Counter is not disabled in MDCR_EL3 when
764 * ARMv8.5-PMU is implemented.
765 * ----------------------------------------------------------
766 */
767 mrs x0, scr_el3
768 tst x0, #SCR_NS_BIT
769 beq 2f
770
771 /* ----------------------------------------------------------
772 * Back to Non-secure state.
Alexei Fedorov307f34b2021-05-14 11:21:56 +0100773 * Check if earlier initialization MDCR_EL3.SCCD/MCCD to 1
774 * failed, meaning that FEAT_PMUv3p5/7 is not implemented and
775 * PMCR_EL0 should be restored from non-secure context.
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100776 * ----------------------------------------------------------
777 */
Alexei Fedorov307f34b2021-05-14 11:21:56 +0100778 mov_imm x1, (MDCR_SCCD_BIT | MDCR_MCCD_BIT)
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100779 mrs x0, mdcr_el3
Alexei Fedorov307f34b2021-05-14 11:21:56 +0100780 tst x0, x1
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100781 bne 2f
782 ldr x0, [sp, #CTX_EL3STATE_OFFSET + CTX_PMCR_EL0]
783 msr pmcr_el0, x0
7842:
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100785 ldp x0, x1, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X0]
786 ldp x2, x3, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X2]
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100787 ldp x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4]
788 ldp x6, x7, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X6]
789 ldp x8, x9, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X8]
790 ldp x10, x11, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X10]
791 ldp x12, x13, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X12]
792 ldp x14, x15, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X14]
Jeenu Viswambharan23d05a82017-11-29 16:59:34 +0000793 ldp x16, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X16]
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100794 ldp x18, x19, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X18]
795 ldp x20, x21, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X20]
796 ldp x22, x23, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X22]
797 ldp x24, x25, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X24]
798 ldp x26, x27, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X26]
Jeenu Viswambharan23d05a82017-11-29 16:59:34 +0000799 ldr x28, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_SP_EL0]
800 msr sp_el0, x28
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100801 ldp x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28]
Jeenu Viswambharan23d05a82017-11-29 16:59:34 +0000802 ret
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100803endfunc restore_gp_pmcr_pauth_regs
Jeenu Viswambharan23d05a82017-11-29 16:59:34 +0000804
Manish V Badarkhee07e8082020-07-23 12:43:25 +0100805/*
806 * In case of ERRATA_SPECULATIVE_AT, save SCTLR_EL1 and TCR_EL1
807 * registers and update EL1 registers to disable stage1 and stage2
808 * page table walk
809 */
810func save_and_update_ptw_el1_sys_regs
811 /* ----------------------------------------------------------
812 * Save only sctlr_el1 and tcr_el1 registers
813 * ----------------------------------------------------------
814 */
815 mrs x29, sctlr_el1
816 str x29, [sp, #(CTX_EL1_SYSREGS_OFFSET + CTX_SCTLR_EL1)]
817 mrs x29, tcr_el1
818 str x29, [sp, #(CTX_EL1_SYSREGS_OFFSET + CTX_TCR_EL1)]
819
820 /* ------------------------------------------------------------
821 * Must follow below order in order to disable page table
822 * walk for lower ELs (EL1 and EL0). First step ensures that
823 * page table walk is disabled for stage1 and second step
824 * ensures that page table walker should use TCR_EL1.EPDx
825 * bits to perform address translation. ISB ensures that CPU
826 * does these 2 steps in order.
827 *
828 * 1. Update TCR_EL1.EPDx bits to disable page table walk by
829 * stage1.
830 * 2. Enable MMU bit to avoid identity mapping via stage2
831 * and force TCR_EL1.EPDx to be used by the page table
832 * walker.
833 * ------------------------------------------------------------
834 */
835 orr x29, x29, #(TCR_EPD0_BIT)
836 orr x29, x29, #(TCR_EPD1_BIT)
837 msr tcr_el1, x29
838 isb
839 mrs x29, sctlr_el1
840 orr x29, x29, #SCTLR_M_BIT
841 msr sctlr_el1, x29
842 isb
843
844 ret
845endfunc save_and_update_ptw_el1_sys_regs
846
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100847/* ------------------------------------------------------------------
848 * This routine assumes that the SP_EL3 is pointing to a valid
849 * context structure from where the gp regs and other special
850 * registers can be retrieved.
851 * ------------------------------------------------------------------
Antonio Nino Diaz13adfb12019-01-30 20:41:31 +0000852 */
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100853func el3_exit
Jan Dabrosfa015982019-12-02 13:30:03 +0100854#if ENABLE_ASSERTIONS
855 /* el3_exit assumes SP_EL0 on entry */
856 mrs x17, spsel
857 cmp x17, #MODE_SP_EL0
858 ASM_ASSERT(eq)
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000859#endif /* ENABLE_ASSERTIONS */
Jan Dabrosfa015982019-12-02 13:30:03 +0100860
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100861 /* ----------------------------------------------------------
862 * Save the current SP_EL0 i.e. the EL3 runtime stack which
863 * will be used for handling the next SMC.
864 * Then switch to SP_EL3.
865 * ----------------------------------------------------------
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100866 */
867 mov x17, sp
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100868 msr spsel, #MODE_SP_ELX
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100869 str x17, [sp, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
870
Max Shvetsovc4502772021-03-22 11:59:37 +0000871#if IMAGE_BL31
872 /* ----------------------------------------------------------
Arunachalam Ganapathycac7d162021-07-08 09:35:57 +0100873 * Restore CPTR_EL3.
Max Shvetsovc4502772021-03-22 11:59:37 +0000874 * ZCR is only restored if SVE is supported and enabled.
875 * Synchronization is required before zcr_el3 is addressed.
876 * ----------------------------------------------------------
877 */
Max Shvetsovc4502772021-03-22 11:59:37 +0000878 ldp x19, x20, [sp, #CTX_EL3STATE_OFFSET + CTX_CPTR_EL3]
879 msr cptr_el3, x19
880
881 ands x19, x19, #CPTR_EZ_BIT
882 beq sve_not_enabled
883
884 isb
885 msr S3_6_C1_C2_0, x20 /* zcr_el3 */
886sve_not_enabled:
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000887#endif /* IMAGE_BL31 */
Max Shvetsovc4502772021-03-22 11:59:37 +0000888
Dimitris Papastamosba51d9e2018-05-16 11:36:14 +0100889#if IMAGE_BL31 && DYNAMIC_WORKAROUND_CVE_2018_3639
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100890 /* ----------------------------------------------------------
891 * Restore mitigation state as it was on entry to EL3
892 * ----------------------------------------------------------
893 */
Dimitris Papastamosba51d9e2018-05-16 11:36:14 +0100894 ldr x17, [sp, #CTX_CVE_2018_3639_OFFSET + CTX_CVE_2018_3639_DISABLE]
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100895 cbz x17, 1f
Dimitris Papastamosba51d9e2018-05-16 11:36:14 +0100896 blr x17
Antonio Nino Diaz13adfb12019-01-30 20:41:31 +00008971:
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000898#endif /* IMAGE_BL31 && DYNAMIC_WORKAROUND_CVE_2018_3639 */
899
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100900#if IMAGE_BL31 && RAS_EXTENSION
901 /* ----------------------------------------------------------
902 * Issue Error Synchronization Barrier to synchronize SErrors
903 * before exiting EL3. We're running with EAs unmasked, so
904 * any synchronized errors would be taken immediately;
905 * therefore no need to inspect DISR_EL1 register.
906 * ----------------------------------------------------------
907 */
908 esb
Madhukar Pappireddyfba25722020-07-24 03:27:12 -0500909#else
910 dsb sy
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000911#endif /* IMAGE_BL31 && RAS_EXTENSION */
912
Manish Pandey53bc59a2022-11-17 14:43:15 +0000913 /* ----------------------------------------------------------
914 * Restore SPSR_EL3, ELR_EL3 and SCR_EL3 prior to ERET
915 * ----------------------------------------------------------
916 */
917 ldr x18, [sp, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3]
918 ldp x16, x17, [sp, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
919 msr scr_el3, x18
920 msr spsr_el3, x16
921 msr elr_el3, x17
922
923 restore_ptw_el1_sys_regs
924
925 /* ----------------------------------------------------------
926 * Restore general purpose (including x30), PMCR_EL0 and
927 * ARMv8.3-PAuth registers.
928 * Exit EL3 via ERET to a lower exception level.
929 * ----------------------------------------------------------
930 */
931 bl restore_gp_pmcr_pauth_regs
932 ldr x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
933
Madhukar Pappireddyfba25722020-07-24 03:27:12 -0500934#ifdef IMAGE_BL31
935 str xzr, [sp, #CTX_EL3STATE_OFFSET + CTX_IS_IN_EL3]
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000936#endif /* IMAGE_BL31 */
937
Anthony Steinhauser0f7e6012020-01-07 15:44:06 -0800938 exception_return
Antonio Nino Diaz594811b2019-01-31 11:58:00 +0000939
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100940endfunc el3_exit