blob: 1cb527d990070634611572ee8ac8f7305ad85e88 [file] [log] [blame]
Achin Gupta9ac63c52014-01-16 12:08:03 +00001/*
Max Shvetsovbdf502d2020-02-25 13:56:19 +00002 * Copyright (c) 2013-2020, 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
16#endif
17
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
23#endif
Alexei Fedorovf41355c2019-09-13 14:11:59 +010024 .global save_gp_pmcr_pauth_regs
25 .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
33 * PCS to use x9-x17 (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 Shvetsovbdf502d2020-02-25 13:56:19 +000046
Max Shvetsovc9e2c922020-02-17 16:15:47 +000047func el2_sysregs_context_save
Max Shvetsovbdf502d2020-02-25 13:56:19 +000048 mrs x9, actlr_el2
Max Shvetsovc9e2c922020-02-17 16:15:47 +000049 mrs x10, afsr0_el2
50 stp x9, x10, [x0, #CTX_ACTLR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +000051
Max Shvetsovc9e2c922020-02-17 16:15:47 +000052 mrs x11, afsr1_el2
53 mrs x12, amair_el2
54 stp x11, x12, [x0, #CTX_AFSR1_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +000055
Max Shvetsovc9e2c922020-02-17 16:15:47 +000056 mrs x13, cnthctl_el2
57 mrs x14, cnthp_ctl_el2
58 stp x13, x14, [x0, #CTX_CNTHCTL_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +000059
Max Shvetsovc9e2c922020-02-17 16:15:47 +000060 mrs x15, cnthp_cval_el2
61 mrs x16, cnthp_tval_el2
62 stp x15, x16, [x0, #CTX_CNTHP_CVAL_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +000063
Max Shvetsovc9e2c922020-02-17 16:15:47 +000064 mrs x17, cntvoff_el2
Max Shvetsovbdf502d2020-02-25 13:56:19 +000065 mrs x9, cptr_el2
Max Shvetsovc9e2c922020-02-17 16:15:47 +000066 stp x17, x9, [x0, #CTX_CNTVOFF_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +000067
Max Shvetsovc9e2c922020-02-17 16:15:47 +000068 mrs x10, dbgvcr32_el2
69 mrs x11, elr_el2
70 stp x10, x11, [x0, #CTX_DBGVCR32_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +000071
Max Shvetsovc9e2c922020-02-17 16:15:47 +000072 mrs x14, esr_el2
73 mrs x15, far_el2
74 stp x14, x15, [x0, #CTX_ESR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +000075
Max Shvetsov0c16abd2020-05-13 18:15:39 +010076 mrs x16, hacr_el2
77 mrs x17, hcr_el2
78 stp x16, x17, [x0, #CTX_HACR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +000079
Max Shvetsov0c16abd2020-05-13 18:15:39 +010080 mrs x9, hpfar_el2
81 mrs x10, hstr_el2
82 stp x9, x10, [x0, #CTX_HPFAR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +000083
Max Shvetsov0c16abd2020-05-13 18:15:39 +010084 mrs x11, ICC_SRE_EL2
85 mrs x12, ICH_HCR_EL2
86 stp x11, x12, [x0, #CTX_ICC_SRE_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +000087
Max Shvetsov0c16abd2020-05-13 18:15:39 +010088 mrs x13, ICH_VMCR_EL2
89 mrs x14, mair_el2
90 stp x13, x14, [x0, #CTX_ICH_VMCR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +000091
Max Shvetsov0c16abd2020-05-13 18:15:39 +010092 mrs x15, mdcr_el2
93 mrs x16, PMSCR_EL2
94 stp x15, x16, [x0, #CTX_MDCR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +000095
Max Shvetsov0c16abd2020-05-13 18:15:39 +010096 mrs x17, sctlr_el2
97 mrs x9, spsr_el2
98 stp x17, x9, [x0, #CTX_SCTLR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +000099
Max Shvetsov0c16abd2020-05-13 18:15:39 +0100100 mrs x10, sp_el2
101 mrs x11, tcr_el2
102 stp x10, x11, [x0, #CTX_SP_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000103
Max Shvetsov0c16abd2020-05-13 18:15:39 +0100104 mrs x12, tpidr_el2
105 mrs x13, ttbr0_el2
106 stp x12, x13, [x0, #CTX_TPIDR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000107
Max Shvetsov0c16abd2020-05-13 18:15:39 +0100108 mrs x14, vbar_el2
109 mrs x15, vmpidr_el2
110 stp x14, x15, [x0, #CTX_VBAR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000111
Max Shvetsov0c16abd2020-05-13 18:15:39 +0100112 mrs x16, vpidr_el2
113 mrs x17, vtcr_el2
114 stp x16, x17, [x0, #CTX_VPIDR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000115
Max Shvetsov0c16abd2020-05-13 18:15:39 +0100116 mrs x9, vttbr_el2
117 str x9, [x0, #CTX_VTTBR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000118
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000119#if CTX_INCLUDE_MTE_REGS
Max Shvetsov0c16abd2020-05-13 18:15:39 +0100120 mrs x10, TFSR_EL2
121 str x10, [x0, #CTX_TFSR_EL2]
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000122#endif
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000123
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000124#if ENABLE_MPAM_FOR_LOWER_ELS
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000125 mrs x9, MPAM2_EL2
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000126 mrs x10, MPAMHCR_EL2
127 stp x9, x10, [x0, #CTX_MPAM2_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000128
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000129 mrs x11, MPAMVPM0_EL2
130 mrs x12, MPAMVPM1_EL2
131 stp x11, x12, [x0, #CTX_MPAMVPM0_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000132
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000133 mrs x13, MPAMVPM2_EL2
134 mrs x14, MPAMVPM3_EL2
135 stp x13, x14, [x0, #CTX_MPAMVPM2_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000136
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000137 mrs x15, MPAMVPM4_EL2
138 mrs x16, MPAMVPM5_EL2
139 stp x15, x16, [x0, #CTX_MPAMVPM4_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000140
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000141 mrs x17, MPAMVPM6_EL2
142 mrs x9, MPAMVPM7_EL2
143 stp x17, x9, [x0, #CTX_MPAMVPM6_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000144
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000145 mrs x10, MPAMVPMV_EL2
146 str x10, [x0, #CTX_MPAMVPMV_EL2]
147#endif
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000148
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000149
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000150#if ARM_ARCH_AT_LEAST(8, 6)
151 mrs x11, HAFGRTR_EL2
152 mrs x12, HDFGRTR_EL2
153 stp x11, x12, [x0, #CTX_HAFGRTR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000154
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000155 mrs x13, HDFGWTR_EL2
156 mrs x14, HFGITR_EL2
157 stp x13, x14, [x0, #CTX_HDFGWTR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000158
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000159 mrs x15, HFGRTR_EL2
160 mrs x16, HFGWTR_EL2
161 stp x15, x16, [x0, #CTX_HFGRTR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000162
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000163 mrs x17, CNTPOFF_EL2
164 str x17, [x0, #CTX_CNTPOFF_EL2]
165#endif
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000166
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000167#if ARM_ARCH_AT_LEAST(8, 4)
168 mrs x9, cnthps_ctl_el2
169 mrs x10, cnthps_cval_el2
170 stp x9, x10, [x0, #CTX_CNTHPS_CTL_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000171
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000172 mrs x11, cnthps_tval_el2
173 mrs x12, cnthvs_ctl_el2
174 stp x11, x12, [x0, #CTX_CNTHPS_TVAL_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000175
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000176 mrs x13, cnthvs_cval_el2
177 mrs x14, cnthvs_tval_el2
178 stp x13, x14, [x0, #CTX_CNTHVS_CVAL_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000179
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000180 mrs x15, cnthv_ctl_el2
181 mrs x16, cnthv_cval_el2
182 stp x15, x16, [x0, #CTX_CNTHV_CTL_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000183
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000184 mrs x17, cnthv_tval_el2
185 mrs x9, contextidr_el2
186 stp x17, x9, [x0, #CTX_CNTHV_TVAL_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000187
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000188 mrs x10, sder32_el2
189 str x10, [x0, #CTX_SDER32_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000190
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000191 mrs x11, ttbr1_el2
192 str x11, [x0, #CTX_TTBR1_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000193
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000194 mrs x12, vdisr_el2
195 str x12, [x0, #CTX_VDISR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000196
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000197 mrs x13, vncr_el2
198 str x13, [x0, #CTX_VNCR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000199
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000200 mrs x14, vsesr_el2
201 str x14, [x0, #CTX_VSESR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000202
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000203 mrs x15, vstcr_el2
204 str x15, [x0, #CTX_VSTCR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000205
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000206 mrs x16, vsttbr_el2
207 str x16, [x0, #CTX_VSTTBR_EL2]
Olivier Deprez19628912020-03-20 14:22:05 +0100208
209 mrs x17, TRFCR_EL2
210 str x17, [x0, #CTX_TRFCR_EL2]
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000211#endif
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000212
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000213#if ARM_ARCH_AT_LEAST(8, 5)
Olivier Deprez19628912020-03-20 14:22:05 +0100214 mrs x9, scxtnum_el2
215 str x9, [x0, #CTX_SCXTNUM_EL2]
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000216#endif
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000217
218 ret
219endfunc el2_sysregs_context_save
220
221/* -----------------------------------------------------
222 * The following function strictly follows the AArch64
223 * PCS to use x9-x17 (temporary caller-saved registers)
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000224 * to restore EL2 system register context. It assumes
225 * that 'x0' is pointing to a 'el2_sys_regs' structure
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000226 * from where the register context will be restored
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000227
228 * The following registers are not restored
229 * AMEVCNTVOFF0<n>_EL2
230 * AMEVCNTVOFF1<n>_EL2
231 * ICH_AP0R<n>_EL2
232 * ICH_AP1R<n>_EL2
233 * ICH_LR<n>_EL2
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000234 * -----------------------------------------------------
235 */
236func el2_sysregs_context_restore
237
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000238 ldp x9, x10, [x0, #CTX_ACTLR_EL2]
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000239 msr actlr_el2, x9
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000240 msr afsr0_el2, x10
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000241
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000242 ldp x11, x12, [x0, #CTX_AFSR1_EL2]
243 msr afsr1_el2, x11
244 msr amair_el2, x12
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000245
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000246 ldp x13, x14, [x0, #CTX_CNTHCTL_EL2]
247 msr cnthctl_el2, x13
248 msr cnthp_ctl_el2, x14
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000249
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000250 ldp x15, x16, [x0, #CTX_CNTHP_CVAL_EL2]
251 msr cnthp_cval_el2, x15
252 msr cnthp_tval_el2, x16
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000253
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000254 ldp x17, x9, [x0, #CTX_CNTVOFF_EL2]
255 msr cntvoff_el2, x17
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000256 msr cptr_el2, x9
257
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000258 ldp x10, x11, [x0, #CTX_DBGVCR32_EL2]
259 msr dbgvcr32_el2, x10
260 msr elr_el2, x11
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000261
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000262 ldp x14, x15, [x0, #CTX_ESR_EL2]
263 msr esr_el2, x14
264 msr far_el2, x15
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000265
Max Shvetsov0c16abd2020-05-13 18:15:39 +0100266 ldp x16, x17, [x0, #CTX_HACR_EL2]
267 msr hacr_el2, x16
268 msr hcr_el2, x17
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000269
Max Shvetsov0c16abd2020-05-13 18:15:39 +0100270 ldp x9, x10, [x0, #CTX_HPFAR_EL2]
271 msr hpfar_el2, x9
272 msr hstr_el2, x10
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000273
Max Shvetsov0c16abd2020-05-13 18:15:39 +0100274 ldp x11, x12, [x0, #CTX_ICC_SRE_EL2]
275 msr ICC_SRE_EL2, x11
276 msr ICH_HCR_EL2, x12
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000277
Max Shvetsov0c16abd2020-05-13 18:15:39 +0100278 ldp x13, x14, [x0, #CTX_ICH_VMCR_EL2]
279 msr ICH_VMCR_EL2, x13
280 msr mair_el2, x14
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000281
Max Shvetsov0c16abd2020-05-13 18:15:39 +0100282 ldp x15, x16, [x0, #CTX_MDCR_EL2]
283 msr mdcr_el2, x15
284 msr PMSCR_EL2, x16
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000285
Manish V Badarkhed73c1ba2020-07-28 07:12:56 +0100286 ldp x17, x9, [x0, #CTX_SCTLR_EL2]
287 msr sctlr_el2, x17
288 msr spsr_el2, x9
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000289
Manish V Badarkhed73c1ba2020-07-28 07:12:56 +0100290 ldp x10, x11, [x0, #CTX_SP_EL2]
291 msr sp_el2, x10
292 msr tcr_el2, x11
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000293
Manish V Badarkhed73c1ba2020-07-28 07:12:56 +0100294 ldp x12, x13, [x0, #CTX_TPIDR_EL2]
295 msr tpidr_el2, x12
296 msr ttbr0_el2, x13
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000297
Manish V Badarkhed73c1ba2020-07-28 07:12:56 +0100298 ldp x13, x14, [x0, #CTX_VBAR_EL2]
299 msr vbar_el2, x13
300 msr vmpidr_el2, x14
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000301
Manish V Badarkhed73c1ba2020-07-28 07:12:56 +0100302 ldp x15, x16, [x0, #CTX_VPIDR_EL2]
303 msr vpidr_el2, x15
304 msr vtcr_el2, x16
305
306 ldr x17, [x0, #CTX_VTTBR_EL2]
307 msr vttbr_el2, x17
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000308
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000309#if CTX_INCLUDE_MTE_REGS
Manish V Badarkhed73c1ba2020-07-28 07:12:56 +0100310 ldr x9, [x0, #CTX_TFSR_EL2]
311 msr TFSR_EL2, x9
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000312#endif
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000313
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000314#if ENABLE_MPAM_FOR_LOWER_ELS
Manish V Badarkhed73c1ba2020-07-28 07:12:56 +0100315 ldp x10, x11, [x0, #CTX_MPAM2_EL2]
316 msr MPAM2_EL2, x10
317 msr MPAMHCR_EL2, x11
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000318
Manish V Badarkhed73c1ba2020-07-28 07:12:56 +0100319 ldp x12, x13, [x0, #CTX_MPAMVPM0_EL2]
320 msr MPAMVPM0_EL2, x12
321 msr MPAMVPM1_EL2, x13
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000322
Manish V Badarkhed73c1ba2020-07-28 07:12:56 +0100323 ldp x14, x15, [x0, #CTX_MPAMVPM2_EL2]
324 msr MPAMVPM2_EL2, x14
325 msr MPAMVPM3_EL2, x15
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000326
Manish V Badarkhed73c1ba2020-07-28 07:12:56 +0100327 ldp x16, x17, [x0, #CTX_MPAMVPM4_EL2]
328 msr MPAMVPM4_EL2, x16
329 msr MPAMVPM5_EL2, x17
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000330
Manish V Badarkhed73c1ba2020-07-28 07:12:56 +0100331 ldp x9, x10, [x0, #CTX_MPAMVPM6_EL2]
332 msr MPAMVPM6_EL2, x9
333 msr MPAMVPM7_EL2, x10
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000334
Manish V Badarkhed73c1ba2020-07-28 07:12:56 +0100335 ldr x11, [x0, #CTX_MPAMVPMV_EL2]
336 msr MPAMVPMV_EL2, x11
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000337#endif
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000338
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000339#if ARM_ARCH_AT_LEAST(8, 6)
Manish V Badarkhed73c1ba2020-07-28 07:12:56 +0100340 ldp x12, x13, [x0, #CTX_HAFGRTR_EL2]
341 msr HAFGRTR_EL2, x12
342 msr HDFGRTR_EL2, x13
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000343
Manish V Badarkhed73c1ba2020-07-28 07:12:56 +0100344 ldp x14, x15, [x0, #CTX_HDFGWTR_EL2]
345 msr HDFGWTR_EL2, x14
346 msr HFGITR_EL2, x15
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000347
Manish V Badarkhed73c1ba2020-07-28 07:12:56 +0100348 ldp x16, x17, [x0, #CTX_HFGRTR_EL2]
349 msr HFGRTR_EL2, x16
350 msr HFGWTR_EL2, x17
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000351
Manish V Badarkhed73c1ba2020-07-28 07:12:56 +0100352 ldr x9, [x0, #CTX_CNTPOFF_EL2]
353 msr CNTPOFF_EL2, x9
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000354#endif
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000355
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000356#if ARM_ARCH_AT_LEAST(8, 4)
Manish V Badarkhed73c1ba2020-07-28 07:12:56 +0100357 ldp x10, x11, [x0, #CTX_CNTHPS_CTL_EL2]
358 msr cnthps_ctl_el2, x10
359 msr cnthps_cval_el2, x11
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000360
Manish V Badarkhed73c1ba2020-07-28 07:12:56 +0100361 ldp x12, x13, [x0, #CTX_CNTHPS_TVAL_EL2]
362 msr cnthps_tval_el2, x12
363 msr cnthvs_ctl_el2, x13
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000364
Manish V Badarkhed73c1ba2020-07-28 07:12:56 +0100365 ldp x14, x15, [x0, #CTX_CNTHVS_CVAL_EL2]
366 msr cnthvs_cval_el2, x14
367 msr cnthvs_tval_el2, x15
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000368
Manish V Badarkhed73c1ba2020-07-28 07:12:56 +0100369 ldp x16, x17, [x0, #CTX_CNTHV_CTL_EL2]
370 msr cnthv_ctl_el2, x16
371 msr cnthv_cval_el2, x17
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000372
Manish V Badarkhed73c1ba2020-07-28 07:12:56 +0100373 ldp x9, x10, [x0, #CTX_CNTHV_TVAL_EL2]
374 msr cnthv_tval_el2, x9
375 msr contextidr_el2, x10
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000376
Manish V Badarkhed73c1ba2020-07-28 07:12:56 +0100377 ldr x11, [x0, #CTX_SDER32_EL2]
378 msr sder32_el2, x11
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000379
Manish V Badarkhed73c1ba2020-07-28 07:12:56 +0100380 ldr x12, [x0, #CTX_TTBR1_EL2]
381 msr ttbr1_el2, x12
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000382
Manish V Badarkhed73c1ba2020-07-28 07:12:56 +0100383 ldr x13, [x0, #CTX_VDISR_EL2]
384 msr vdisr_el2, x13
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000385
Manish V Badarkhed73c1ba2020-07-28 07:12:56 +0100386 ldr x14, [x0, #CTX_VNCR_EL2]
387 msr vncr_el2, x14
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000388
Manish V Badarkhed73c1ba2020-07-28 07:12:56 +0100389 ldr x15, [x0, #CTX_VSESR_EL2]
390 msr vsesr_el2, x15
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000391
Manish V Badarkhed73c1ba2020-07-28 07:12:56 +0100392 ldr x16, [x0, #CTX_VSTCR_EL2]
393 msr vstcr_el2, x16
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000394
Manish V Badarkhed73c1ba2020-07-28 07:12:56 +0100395 ldr x17, [x0, #CTX_VSTTBR_EL2]
396 msr vsttbr_el2, x17
Olivier Deprez19628912020-03-20 14:22:05 +0100397
Manish V Badarkhed73c1ba2020-07-28 07:12:56 +0100398 ldr x9, [x0, #CTX_TRFCR_EL2]
399 msr TRFCR_EL2, x9
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000400#endif
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000401
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000402#if ARM_ARCH_AT_LEAST(8, 5)
Manish V Badarkhed73c1ba2020-07-28 07:12:56 +0100403 ldr x10, [x0, #CTX_SCXTNUM_EL2]
404 msr scxtnum_el2, x10
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000405#endif
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000406
407 ret
408endfunc el2_sysregs_context_restore
409
410#endif /* CTX_INCLUDE_EL2_REGS */
411
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100412/* ------------------------------------------------------------------
413 * The following function strictly follows the AArch64 PCS to use
414 * x9-x17 (temporary caller-saved registers) to save EL1 system
415 * register context. It assumes that 'x0' is pointing to a
416 * 'el1_sys_regs' structure where the register context will be saved.
417 * ------------------------------------------------------------------
Achin Gupta9ac63c52014-01-16 12:08:03 +0000418 */
Andrew Thoelke38bde412014-03-18 13:46:55 +0000419func el1_sysregs_context_save
Achin Gupta9ac63c52014-01-16 12:08:03 +0000420
421 mrs x9, spsr_el1
422 mrs x10, elr_el1
423 stp x9, x10, [x0, #CTX_SPSR_EL1]
424
Manish V Badarkhee07e8082020-07-23 12:43:25 +0100425#if !ERRATA_SPECULATIVE_AT
Achin Gupta9ac63c52014-01-16 12:08:03 +0000426 mrs x15, sctlr_el1
Manish V Badarkhe2b0ee972020-07-28 07:22:30 +0100427 mrs x16, tcr_el1
Achin Gupta9ac63c52014-01-16 12:08:03 +0000428 stp x15, x16, [x0, #CTX_SCTLR_EL1]
Manish V Badarkhee07e8082020-07-23 12:43:25 +0100429#endif
Achin Gupta9ac63c52014-01-16 12:08:03 +0000430
431 mrs x17, cpacr_el1
432 mrs x9, csselr_el1
433 stp x17, x9, [x0, #CTX_CPACR_EL1]
434
435 mrs x10, sp_el1
436 mrs x11, esr_el1
437 stp x10, x11, [x0, #CTX_SP_EL1]
438
439 mrs x12, ttbr0_el1
440 mrs x13, ttbr1_el1
441 stp x12, x13, [x0, #CTX_TTBR0_EL1]
442
443 mrs x14, mair_el1
444 mrs x15, amair_el1
445 stp x14, x15, [x0, #CTX_MAIR_EL1]
446
Manish V Badarkhe2b0ee972020-07-28 07:22:30 +0100447 mrs x16, actlr_el1
Achin Gupta9ac63c52014-01-16 12:08:03 +0000448 mrs x17, tpidr_el1
Manish V Badarkhe2b0ee972020-07-28 07:22:30 +0100449 stp x16, x17, [x0, #CTX_ACTLR_EL1]
Achin Gupta9ac63c52014-01-16 12:08:03 +0000450
451 mrs x9, tpidr_el0
452 mrs x10, tpidrro_el0
453 stp x9, x10, [x0, #CTX_TPIDR_EL0]
454
Achin Gupta9ac63c52014-01-16 12:08:03 +0000455 mrs x13, par_el1
456 mrs x14, far_el1
457 stp x13, x14, [x0, #CTX_PAR_EL1]
458
459 mrs x15, afsr0_el1
460 mrs x16, afsr1_el1
461 stp x15, x16, [x0, #CTX_AFSR0_EL1]
462
463 mrs x17, contextidr_el1
464 mrs x9, vbar_el1
465 stp x17, x9, [x0, #CTX_CONTEXTIDR_EL1]
466
Soby Mathewd75d2ba2016-05-17 14:01:32 +0100467 /* Save AArch32 system registers if the build has instructed so */
468#if CTX_INCLUDE_AARCH32_REGS
469 mrs x11, spsr_abt
470 mrs x12, spsr_und
471 stp x11, x12, [x0, #CTX_SPSR_ABT]
472
473 mrs x13, spsr_irq
474 mrs x14, spsr_fiq
475 stp x13, x14, [x0, #CTX_SPSR_IRQ]
476
477 mrs x15, dacr32_el2
478 mrs x16, ifsr32_el2
479 stp x15, x16, [x0, #CTX_DACR32_EL2]
Soby Mathewd75d2ba2016-05-17 14:01:32 +0100480#endif
481
Jeenu Viswambharand1b60152014-05-12 15:28:47 +0100482 /* Save NS timer registers if the build has instructed so */
483#if NS_TIMER_SWITCH
Achin Gupta9ac63c52014-01-16 12:08:03 +0000484 mrs x10, cntp_ctl_el0
485 mrs x11, cntp_cval_el0
486 stp x10, x11, [x0, #CTX_CNTP_CTL_EL0]
487
488 mrs x12, cntv_ctl_el0
489 mrs x13, cntv_cval_el0
490 stp x12, x13, [x0, #CTX_CNTV_CTL_EL0]
491
492 mrs x14, cntkctl_el1
Jeenu Viswambharand1b60152014-05-12 15:28:47 +0100493 str x14, [x0, #CTX_CNTKCTL_EL1]
494#endif
495
Justin Chadwell1c7c13a2019-07-18 14:25:33 +0100496 /* Save MTE system registers if the build has instructed so */
497#if CTX_INCLUDE_MTE_REGS
498 mrs x15, TFSRE0_EL1
499 mrs x16, TFSR_EL1
500 stp x15, x16, [x0, #CTX_TFSRE0_EL1]
501
502 mrs x9, RGSR_EL1
503 mrs x10, GCR_EL1
504 stp x9, x10, [x0, #CTX_RGSR_EL1]
505#endif
506
Achin Gupta9ac63c52014-01-16 12:08:03 +0000507 ret
Kévin Petita877c252015-03-24 14:03:57 +0000508endfunc el1_sysregs_context_save
Achin Gupta9ac63c52014-01-16 12:08:03 +0000509
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100510/* ------------------------------------------------------------------
511 * The following function strictly follows the AArch64 PCS to use
512 * x9-x17 (temporary caller-saved registers) to restore EL1 system
513 * register context. It assumes that 'x0' is pointing to a
514 * 'el1_sys_regs' structure from where the register context will be
515 * restored
516 * ------------------------------------------------------------------
Achin Gupta9ac63c52014-01-16 12:08:03 +0000517 */
Andrew Thoelke38bde412014-03-18 13:46:55 +0000518func el1_sysregs_context_restore
Achin Gupta9ac63c52014-01-16 12:08:03 +0000519
520 ldp x9, x10, [x0, #CTX_SPSR_EL1]
521 msr spsr_el1, x9
522 msr elr_el1, x10
523
Manish V Badarkhee07e8082020-07-23 12:43:25 +0100524#if !ERRATA_SPECULATIVE_AT
Manish V Badarkhed73c1ba2020-07-28 07:12:56 +0100525 ldp x15, x16, [x0, #CTX_SCTLR_EL1]
526 msr sctlr_el1, x15
Manish V Badarkhe2b0ee972020-07-28 07:22:30 +0100527 msr tcr_el1, x16
Manish V Badarkhee07e8082020-07-23 12:43:25 +0100528#endif
Achin Gupta9ac63c52014-01-16 12:08:03 +0000529
530 ldp x17, x9, [x0, #CTX_CPACR_EL1]
531 msr cpacr_el1, x17
532 msr csselr_el1, x9
533
534 ldp x10, x11, [x0, #CTX_SP_EL1]
535 msr sp_el1, x10
536 msr esr_el1, x11
537
538 ldp x12, x13, [x0, #CTX_TTBR0_EL1]
539 msr ttbr0_el1, x12
540 msr ttbr1_el1, x13
541
542 ldp x14, x15, [x0, #CTX_MAIR_EL1]
543 msr mair_el1, x14
544 msr amair_el1, x15
545
Manish V Badarkhe2b0ee972020-07-28 07:22:30 +0100546 ldp x16, x17, [x0, #CTX_ACTLR_EL1]
547 msr actlr_el1, x16
Manish V Badarkhed73c1ba2020-07-28 07:12:56 +0100548 msr tpidr_el1, x17
Achin Gupta9ac63c52014-01-16 12:08:03 +0000549
550 ldp x9, x10, [x0, #CTX_TPIDR_EL0]
551 msr tpidr_el0, x9
552 msr tpidrro_el0, x10
553
Achin Gupta9ac63c52014-01-16 12:08:03 +0000554 ldp x13, x14, [x0, #CTX_PAR_EL1]
555 msr par_el1, x13
556 msr far_el1, x14
557
558 ldp x15, x16, [x0, #CTX_AFSR0_EL1]
559 msr afsr0_el1, x15
560 msr afsr1_el1, x16
561
562 ldp x17, x9, [x0, #CTX_CONTEXTIDR_EL1]
563 msr contextidr_el1, x17
564 msr vbar_el1, x9
565
Soby Mathewd75d2ba2016-05-17 14:01:32 +0100566 /* Restore AArch32 system registers if the build has instructed so */
567#if CTX_INCLUDE_AARCH32_REGS
568 ldp x11, x12, [x0, #CTX_SPSR_ABT]
569 msr spsr_abt, x11
570 msr spsr_und, x12
571
572 ldp x13, x14, [x0, #CTX_SPSR_IRQ]
573 msr spsr_irq, x13
574 msr spsr_fiq, x14
575
576 ldp x15, x16, [x0, #CTX_DACR32_EL2]
577 msr dacr32_el2, x15
578 msr ifsr32_el2, x16
Soby Mathewd75d2ba2016-05-17 14:01:32 +0100579#endif
Jeenu Viswambharand1b60152014-05-12 15:28:47 +0100580 /* Restore NS timer registers if the build has instructed so */
581#if NS_TIMER_SWITCH
Achin Gupta9ac63c52014-01-16 12:08:03 +0000582 ldp x10, x11, [x0, #CTX_CNTP_CTL_EL0]
583 msr cntp_ctl_el0, x10
584 msr cntp_cval_el0, x11
585
586 ldp x12, x13, [x0, #CTX_CNTV_CTL_EL0]
587 msr cntv_ctl_el0, x12
588 msr cntv_cval_el0, x13
589
Jeenu Viswambharand1b60152014-05-12 15:28:47 +0100590 ldr x14, [x0, #CTX_CNTKCTL_EL1]
Achin Gupta9ac63c52014-01-16 12:08:03 +0000591 msr cntkctl_el1, x14
Jeenu Viswambharand1b60152014-05-12 15:28:47 +0100592#endif
Justin Chadwell1c7c13a2019-07-18 14:25:33 +0100593 /* Restore MTE system registers if the build has instructed so */
594#if CTX_INCLUDE_MTE_REGS
595 ldp x11, x12, [x0, #CTX_TFSRE0_EL1]
596 msr TFSRE0_EL1, x11
597 msr TFSR_EL1, x12
598
599 ldp x13, x14, [x0, #CTX_RGSR_EL1]
600 msr RGSR_EL1, x13
601 msr GCR_EL1, x14
602#endif
Jeenu Viswambharand1b60152014-05-12 15:28:47 +0100603
Achin Gupta9ac63c52014-01-16 12:08:03 +0000604 /* No explict ISB required here as ERET covers it */
Achin Gupta9ac63c52014-01-16 12:08:03 +0000605 ret
Kévin Petita877c252015-03-24 14:03:57 +0000606endfunc el1_sysregs_context_restore
Achin Gupta9ac63c52014-01-16 12:08:03 +0000607
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100608/* ------------------------------------------------------------------
609 * The following function follows the aapcs_64 strictly to use
610 * x9-x17 (temporary caller-saved registers according to AArch64 PCS)
611 * to save floating point register context. It assumes that 'x0' is
612 * pointing to a 'fp_regs' structure where the register context will
Achin Gupta9ac63c52014-01-16 12:08:03 +0000613 * be saved.
614 *
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100615 * Access to VFP registers will trap if CPTR_EL3.TFP is set.
616 * However currently we don't use VFP registers nor set traps in
617 * Trusted Firmware, and assume it's cleared.
Achin Gupta9ac63c52014-01-16 12:08:03 +0000618 *
619 * TODO: Revisit when VFP is used in secure world
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100620 * ------------------------------------------------------------------
Achin Gupta9ac63c52014-01-16 12:08:03 +0000621 */
Juan Castillo258e94f2014-06-25 17:26:36 +0100622#if CTX_INCLUDE_FPREGS
Andrew Thoelke38bde412014-03-18 13:46:55 +0000623func fpregs_context_save
Achin Gupta9ac63c52014-01-16 12:08:03 +0000624 stp q0, q1, [x0, #CTX_FP_Q0]
625 stp q2, q3, [x0, #CTX_FP_Q2]
626 stp q4, q5, [x0, #CTX_FP_Q4]
627 stp q6, q7, [x0, #CTX_FP_Q6]
628 stp q8, q9, [x0, #CTX_FP_Q8]
629 stp q10, q11, [x0, #CTX_FP_Q10]
630 stp q12, q13, [x0, #CTX_FP_Q12]
631 stp q14, q15, [x0, #CTX_FP_Q14]
632 stp q16, q17, [x0, #CTX_FP_Q16]
633 stp q18, q19, [x0, #CTX_FP_Q18]
634 stp q20, q21, [x0, #CTX_FP_Q20]
635 stp q22, q23, [x0, #CTX_FP_Q22]
636 stp q24, q25, [x0, #CTX_FP_Q24]
637 stp q26, q27, [x0, #CTX_FP_Q26]
638 stp q28, q29, [x0, #CTX_FP_Q28]
639 stp q30, q31, [x0, #CTX_FP_Q30]
640
641 mrs x9, fpsr
642 str x9, [x0, #CTX_FP_FPSR]
643
644 mrs x10, fpcr
645 str x10, [x0, #CTX_FP_FPCR]
646
David Cunadod1a1fd42017-10-20 11:30:57 +0100647#if CTX_INCLUDE_AARCH32_REGS
648 mrs x11, fpexc32_el2
649 str x11, [x0, #CTX_FP_FPEXC32_EL2]
650#endif
Achin Gupta9ac63c52014-01-16 12:08:03 +0000651 ret
Kévin Petita877c252015-03-24 14:03:57 +0000652endfunc fpregs_context_save
Achin Gupta9ac63c52014-01-16 12:08:03 +0000653
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100654/* ------------------------------------------------------------------
655 * The following function follows the aapcs_64 strictly to use x9-x17
656 * (temporary caller-saved registers according to AArch64 PCS) to
657 * restore floating point register context. It assumes that 'x0' is
658 * pointing to a 'fp_regs' structure from where the register context
Achin Gupta9ac63c52014-01-16 12:08:03 +0000659 * will be restored.
660 *
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100661 * Access to VFP registers will trap if CPTR_EL3.TFP is set.
662 * However currently we don't use VFP registers nor set traps in
663 * Trusted Firmware, and assume it's cleared.
Achin Gupta9ac63c52014-01-16 12:08:03 +0000664 *
665 * TODO: Revisit when VFP is used in secure world
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100666 * ------------------------------------------------------------------
Achin Gupta9ac63c52014-01-16 12:08:03 +0000667 */
Andrew Thoelke38bde412014-03-18 13:46:55 +0000668func fpregs_context_restore
Achin Gupta9ac63c52014-01-16 12:08:03 +0000669 ldp q0, q1, [x0, #CTX_FP_Q0]
670 ldp q2, q3, [x0, #CTX_FP_Q2]
671 ldp q4, q5, [x0, #CTX_FP_Q4]
672 ldp q6, q7, [x0, #CTX_FP_Q6]
673 ldp q8, q9, [x0, #CTX_FP_Q8]
674 ldp q10, q11, [x0, #CTX_FP_Q10]
675 ldp q12, q13, [x0, #CTX_FP_Q12]
676 ldp q14, q15, [x0, #CTX_FP_Q14]
677 ldp q16, q17, [x0, #CTX_FP_Q16]
678 ldp q18, q19, [x0, #CTX_FP_Q18]
679 ldp q20, q21, [x0, #CTX_FP_Q20]
680 ldp q22, q23, [x0, #CTX_FP_Q22]
681 ldp q24, q25, [x0, #CTX_FP_Q24]
682 ldp q26, q27, [x0, #CTX_FP_Q26]
683 ldp q28, q29, [x0, #CTX_FP_Q28]
684 ldp q30, q31, [x0, #CTX_FP_Q30]
685
686 ldr x9, [x0, #CTX_FP_FPSR]
687 msr fpsr, x9
688
Soby Mathewe77e1162015-12-03 09:42:50 +0000689 ldr x10, [x0, #CTX_FP_FPCR]
Achin Gupta9ac63c52014-01-16 12:08:03 +0000690 msr fpcr, x10
691
David Cunadod1a1fd42017-10-20 11:30:57 +0100692#if CTX_INCLUDE_AARCH32_REGS
693 ldr x11, [x0, #CTX_FP_FPEXC32_EL2]
694 msr fpexc32_el2, x11
695#endif
Achin Gupta9ac63c52014-01-16 12:08:03 +0000696 /*
697 * No explict ISB required here as ERET to
Sandrine Bailleuxf4119ec2015-12-17 13:58:58 +0000698 * switch to secure EL1 or non-secure world
Achin Gupta9ac63c52014-01-16 12:08:03 +0000699 * covers it
700 */
701
702 ret
Kévin Petita877c252015-03-24 14:03:57 +0000703endfunc fpregs_context_restore
Juan Castillo258e94f2014-06-25 17:26:36 +0100704#endif /* CTX_INCLUDE_FPREGS */
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100705
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100706/* ------------------------------------------------------------------
707 * The following function is used to save and restore all the general
708 * purpose and ARMv8.3-PAuth (if enabled) registers.
709 * It also checks if Secure Cycle Counter is not disabled in MDCR_EL3
710 * when ARMv8.5-PMU is implemented, and if called from Non-secure
711 * state saves PMCR_EL0 and disables Cycle Counter.
712 *
713 * Ideally we would only save and restore the callee saved registers
714 * when a world switch occurs but that type of implementation is more
715 * complex. So currently we will always save and restore these
716 * registers on entry and exit of EL3.
717 * These are not macros to ensure their invocation fits within the 32
718 * instructions per exception vector.
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100719 * clobbers: x18
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100720 * ------------------------------------------------------------------
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100721 */
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100722func save_gp_pmcr_pauth_regs
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100723 stp x0, x1, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X0]
724 stp x2, x3, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X2]
725 stp x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4]
726 stp x6, x7, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X6]
727 stp x8, x9, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X8]
728 stp x10, x11, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X10]
729 stp x12, x13, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X12]
730 stp x14, x15, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X14]
731 stp x16, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X16]
732 stp x18, x19, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X18]
733 stp x20, x21, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X20]
734 stp x22, x23, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X22]
735 stp x24, x25, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X24]
736 stp x26, x27, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X26]
737 stp x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28]
738 mrs x18, sp_el0
739 str x18, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_SP_EL0]
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100740
741 /* ----------------------------------------------------------
742 * Check if earlier initialization MDCR_EL3.SCCD to 1 failed,
743 * meaning that ARMv8-PMU is not implemented and PMCR_EL0
744 * should be saved in non-secure context.
745 * ----------------------------------------------------------
746 */
747 mrs x9, mdcr_el3
748 tst x9, #MDCR_SCCD_BIT
749 bne 1f
750
751 /* Secure Cycle Counter is not disabled */
752 mrs x9, pmcr_el0
753
754 /* Check caller's security state */
755 mrs x10, scr_el3
756 tst x10, #SCR_NS_BIT
757 beq 2f
758
759 /* Save PMCR_EL0 if called from Non-secure state */
760 str x9, [sp, #CTX_EL3STATE_OFFSET + CTX_PMCR_EL0]
761
762 /* Disable cycle counter when event counting is prohibited */
7632: orr x9, x9, #PMCR_EL0_DP_BIT
764 msr pmcr_el0, x9
765 isb
7661:
767#if CTX_INCLUDE_PAUTH_REGS
768 /* ----------------------------------------------------------
769 * Save the ARMv8.3-PAuth keys as they are not banked
770 * by exception level
771 * ----------------------------------------------------------
772 */
773 add x19, sp, #CTX_PAUTH_REGS_OFFSET
774
775 mrs x20, APIAKeyLo_EL1 /* x21:x20 = APIAKey */
776 mrs x21, APIAKeyHi_EL1
777 mrs x22, APIBKeyLo_EL1 /* x23:x22 = APIBKey */
778 mrs x23, APIBKeyHi_EL1
779 mrs x24, APDAKeyLo_EL1 /* x25:x24 = APDAKey */
780 mrs x25, APDAKeyHi_EL1
781 mrs x26, APDBKeyLo_EL1 /* x27:x26 = APDBKey */
782 mrs x27, APDBKeyHi_EL1
783 mrs x28, APGAKeyLo_EL1 /* x29:x28 = APGAKey */
784 mrs x29, APGAKeyHi_EL1
785
786 stp x20, x21, [x19, #CTX_PACIAKEY_LO]
787 stp x22, x23, [x19, #CTX_PACIBKEY_LO]
788 stp x24, x25, [x19, #CTX_PACDAKEY_LO]
789 stp x26, x27, [x19, #CTX_PACDBKEY_LO]
790 stp x28, x29, [x19, #CTX_PACGAKEY_LO]
791#endif /* CTX_INCLUDE_PAUTH_REGS */
792
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100793 ret
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100794endfunc save_gp_pmcr_pauth_regs
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100795
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100796/* ------------------------------------------------------------------
797 * This function restores ARMv8.3-PAuth (if enabled) and all general
798 * purpose registers except x30 from the CPU context.
799 * x30 register must be explicitly restored by the caller.
800 * ------------------------------------------------------------------
Jeenu Viswambharan23d05a82017-11-29 16:59:34 +0000801 */
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100802func restore_gp_pmcr_pauth_regs
803#if CTX_INCLUDE_PAUTH_REGS
804 /* Restore the ARMv8.3 PAuth keys */
805 add x10, sp, #CTX_PAUTH_REGS_OFFSET
806
807 ldp x0, x1, [x10, #CTX_PACIAKEY_LO] /* x1:x0 = APIAKey */
808 ldp x2, x3, [x10, #CTX_PACIBKEY_LO] /* x3:x2 = APIBKey */
809 ldp x4, x5, [x10, #CTX_PACDAKEY_LO] /* x5:x4 = APDAKey */
810 ldp x6, x7, [x10, #CTX_PACDBKEY_LO] /* x7:x6 = APDBKey */
811 ldp x8, x9, [x10, #CTX_PACGAKEY_LO] /* x9:x8 = APGAKey */
812
813 msr APIAKeyLo_EL1, x0
814 msr APIAKeyHi_EL1, x1
815 msr APIBKeyLo_EL1, x2
816 msr APIBKeyHi_EL1, x3
817 msr APDAKeyLo_EL1, x4
818 msr APDAKeyHi_EL1, x5
819 msr APDBKeyLo_EL1, x6
820 msr APDBKeyHi_EL1, x7
821 msr APGAKeyLo_EL1, x8
822 msr APGAKeyHi_EL1, x9
823#endif /* CTX_INCLUDE_PAUTH_REGS */
824
825 /* ----------------------------------------------------------
826 * Restore PMCR_EL0 when returning to Non-secure state if
827 * Secure Cycle Counter is not disabled in MDCR_EL3 when
828 * ARMv8.5-PMU is implemented.
829 * ----------------------------------------------------------
830 */
831 mrs x0, scr_el3
832 tst x0, #SCR_NS_BIT
833 beq 2f
834
835 /* ----------------------------------------------------------
836 * Back to Non-secure state.
837 * Check if earlier initialization MDCR_EL3.SCCD to 1 failed,
838 * meaning that ARMv8-PMU is not implemented and PMCR_EL0
839 * should be restored from non-secure context.
840 * ----------------------------------------------------------
841 */
842 mrs x0, mdcr_el3
843 tst x0, #MDCR_SCCD_BIT
844 bne 2f
845 ldr x0, [sp, #CTX_EL3STATE_OFFSET + CTX_PMCR_EL0]
846 msr pmcr_el0, x0
8472:
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100848 ldp x0, x1, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X0]
849 ldp x2, x3, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X2]
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100850 ldp x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4]
851 ldp x6, x7, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X6]
852 ldp x8, x9, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X8]
853 ldp x10, x11, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X10]
854 ldp x12, x13, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X12]
855 ldp x14, x15, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X14]
Jeenu Viswambharan23d05a82017-11-29 16:59:34 +0000856 ldp x16, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X16]
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100857 ldp x18, x19, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X18]
858 ldp x20, x21, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X20]
859 ldp x22, x23, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X22]
860 ldp x24, x25, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X24]
861 ldp x26, x27, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X26]
Jeenu Viswambharan23d05a82017-11-29 16:59:34 +0000862 ldr x28, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_SP_EL0]
863 msr sp_el0, x28
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100864 ldp x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28]
Jeenu Viswambharan23d05a82017-11-29 16:59:34 +0000865 ret
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100866endfunc restore_gp_pmcr_pauth_regs
Jeenu Viswambharan23d05a82017-11-29 16:59:34 +0000867
Manish V Badarkhee07e8082020-07-23 12:43:25 +0100868/*
869 * In case of ERRATA_SPECULATIVE_AT, save SCTLR_EL1 and TCR_EL1
870 * registers and update EL1 registers to disable stage1 and stage2
871 * page table walk
872 */
873func save_and_update_ptw_el1_sys_regs
874 /* ----------------------------------------------------------
875 * Save only sctlr_el1 and tcr_el1 registers
876 * ----------------------------------------------------------
877 */
878 mrs x29, sctlr_el1
879 str x29, [sp, #(CTX_EL1_SYSREGS_OFFSET + CTX_SCTLR_EL1)]
880 mrs x29, tcr_el1
881 str x29, [sp, #(CTX_EL1_SYSREGS_OFFSET + CTX_TCR_EL1)]
882
883 /* ------------------------------------------------------------
884 * Must follow below order in order to disable page table
885 * walk for lower ELs (EL1 and EL0). First step ensures that
886 * page table walk is disabled for stage1 and second step
887 * ensures that page table walker should use TCR_EL1.EPDx
888 * bits to perform address translation. ISB ensures that CPU
889 * does these 2 steps in order.
890 *
891 * 1. Update TCR_EL1.EPDx bits to disable page table walk by
892 * stage1.
893 * 2. Enable MMU bit to avoid identity mapping via stage2
894 * and force TCR_EL1.EPDx to be used by the page table
895 * walker.
896 * ------------------------------------------------------------
897 */
898 orr x29, x29, #(TCR_EPD0_BIT)
899 orr x29, x29, #(TCR_EPD1_BIT)
900 msr tcr_el1, x29
901 isb
902 mrs x29, sctlr_el1
903 orr x29, x29, #SCTLR_M_BIT
904 msr sctlr_el1, x29
905 isb
906
907 ret
908endfunc save_and_update_ptw_el1_sys_regs
909
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100910/* ------------------------------------------------------------------
911 * This routine assumes that the SP_EL3 is pointing to a valid
912 * context structure from where the gp regs and other special
913 * registers can be retrieved.
914 * ------------------------------------------------------------------
Antonio Nino Diaz13adfb12019-01-30 20:41:31 +0000915 */
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100916func el3_exit
Jan Dabrosfa015982019-12-02 13:30:03 +0100917#if ENABLE_ASSERTIONS
918 /* el3_exit assumes SP_EL0 on entry */
919 mrs x17, spsel
920 cmp x17, #MODE_SP_EL0
921 ASM_ASSERT(eq)
922#endif
923
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100924 /* ----------------------------------------------------------
925 * Save the current SP_EL0 i.e. the EL3 runtime stack which
926 * will be used for handling the next SMC.
927 * Then switch to SP_EL3.
928 * ----------------------------------------------------------
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100929 */
930 mov x17, sp
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100931 msr spsel, #MODE_SP_ELX
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100932 str x17, [sp, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
933
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100934 /* ----------------------------------------------------------
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100935 * Restore SPSR_EL3, ELR_EL3 and SCR_EL3 prior to ERET
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100936 * ----------------------------------------------------------
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100937 */
938 ldr x18, [sp, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3]
939 ldp x16, x17, [sp, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
940 msr scr_el3, x18
941 msr spsr_el3, x16
942 msr elr_el3, x17
943
Dimitris Papastamosba51d9e2018-05-16 11:36:14 +0100944#if IMAGE_BL31 && DYNAMIC_WORKAROUND_CVE_2018_3639
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100945 /* ----------------------------------------------------------
946 * Restore mitigation state as it was on entry to EL3
947 * ----------------------------------------------------------
948 */
Dimitris Papastamosba51d9e2018-05-16 11:36:14 +0100949 ldr x17, [sp, #CTX_CVE_2018_3639_OFFSET + CTX_CVE_2018_3639_DISABLE]
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100950 cbz x17, 1f
Dimitris Papastamosba51d9e2018-05-16 11:36:14 +0100951 blr x17
Antonio Nino Diaz13adfb12019-01-30 20:41:31 +00009521:
Dimitris Papastamosba51d9e2018-05-16 11:36:14 +0100953#endif
Manish V Badarkhee07e8082020-07-23 12:43:25 +0100954 restore_ptw_el1_sys_regs
955
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100956 /* ----------------------------------------------------------
957 * Restore general purpose (including x30), PMCR_EL0 and
958 * ARMv8.3-PAuth registers.
959 * Exit EL3 via ERET to a lower exception level.
960 * ----------------------------------------------------------
961 */
962 bl restore_gp_pmcr_pauth_regs
963 ldr x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
Dimitris Papastamosba51d9e2018-05-16 11:36:14 +0100964
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100965#if IMAGE_BL31 && RAS_EXTENSION
966 /* ----------------------------------------------------------
967 * Issue Error Synchronization Barrier to synchronize SErrors
968 * before exiting EL3. We're running with EAs unmasked, so
969 * any synchronized errors would be taken immediately;
970 * therefore no need to inspect DISR_EL1 register.
971 * ----------------------------------------------------------
972 */
973 esb
Antonio Nino Diaz594811b2019-01-31 11:58:00 +0000974#endif
Anthony Steinhauser0f7e6012020-01-07 15:44:06 -0800975 exception_return
Antonio Nino Diaz594811b2019-01-31 11:58:00 +0000976
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100977endfunc el3_exit