blob: ab9d4b638ac075df95a17ca2ce2eaba2a178e9a3 [file] [log] [blame]
Achin Gupta9ac63c52014-01-16 12:08:03 +00001/*
Govindraj Raja77922ca2024-01-25 08:09:39 -06002 * Copyright (c) 2013-2024, 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>
Madhukar Pappireddy78728cd2024-06-17 15:22:36 -050012#include <platform_def.h>
Achin Gupta9ac63c52014-01-16 12:08:03 +000013
Yatharth Kochar6c0566c2015-10-02 17:56:48 +010014#if CTX_INCLUDE_FPREGS
15 .global fpregs_context_save
16 .global fpregs_context_restore
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +000017#endif /* CTX_INCLUDE_FPREGS */
Jayanth Dodderi Chidanand3a71df62024-06-05 11:13:05 +010018
Madhukar Pappireddy78728cd2024-06-17 15:22:36 -050019#if CTX_INCLUDE_SVE_REGS
20 .global sve_context_save
21 .global sve_context_restore
22#endif /* CTX_INCLUDE_SVE_REGS */
23
Jayanth Dodderi Chidanand3a71df62024-06-05 11:13:05 +010024#if ERRATA_SPECULATIVE_AT
25 .global save_and_update_ptw_el1_sys_regs
26#endif /* ERRATA_SPECULATIVE_AT */
27
Daniel Boulby95fb1aa2022-01-19 11:20:05 +000028 .global prepare_el3_entry
Alexei Fedorovf41355c2019-09-13 14:11:59 +010029 .global restore_gp_pmcr_pauth_regs
Yatharth Kochar6c0566c2015-10-02 17:56:48 +010030 .global el3_exit
31
Madhukar Pappireddy78728cd2024-06-17 15:22:36 -050032/* Following macros will be used if any of CTX_INCLUDE_FPREGS or CTX_INCLUDE_SVE_REGS is enabled */
33#if CTX_INCLUDE_FPREGS || CTX_INCLUDE_SVE_REGS
34.macro fpregs_state_save base:req hold:req
35 mrs \hold, fpsr
36 str \hold, [\base, #CTX_SIMD_FPSR]
37
38 mrs \hold, fpcr
39 str \hold, [\base, #CTX_SIMD_FPCR]
40
41#if CTX_INCLUDE_AARCH32_REGS && CTX_INCLUDE_FPREGS
42 mrs \hold, fpexc32_el2
43 str \hold, [\base, #CTX_SIMD_FPEXC32]
44#endif
45.endm
46
47.macro fpregs_state_restore base:req hold:req
48 ldr \hold, [\base, #CTX_SIMD_FPSR]
49 msr fpsr, \hold
50
51 ldr \hold, [\base, #CTX_SIMD_FPCR]
52 msr fpcr, \hold
53
54#if CTX_INCLUDE_AARCH32_REGS && CTX_INCLUDE_FPREGS
55 ldr \hold, [\base, #CTX_SIMD_FPEXC32]
56 msr fpexc32_el2, \hold
57#endif
58.endm
59
60#endif /* CTX_INCLUDE_FPREGS || CTX_INCLUDE_SVE_REGS */
61
Alexei Fedorovf41355c2019-09-13 14:11:59 +010062/* ------------------------------------------------------------------
63 * The following function follows the aapcs_64 strictly to use
64 * x9-x17 (temporary caller-saved registers according to AArch64 PCS)
65 * to save floating point register context. It assumes that 'x0' is
66 * pointing to a 'fp_regs' structure where the register context will
Achin Gupta9ac63c52014-01-16 12:08:03 +000067 * be saved.
68 *
Alexei Fedorovf41355c2019-09-13 14:11:59 +010069 * Access to VFP registers will trap if CPTR_EL3.TFP is set.
70 * However currently we don't use VFP registers nor set traps in
71 * Trusted Firmware, and assume it's cleared.
Achin Gupta9ac63c52014-01-16 12:08:03 +000072 *
73 * TODO: Revisit when VFP is used in secure world
Alexei Fedorovf41355c2019-09-13 14:11:59 +010074 * ------------------------------------------------------------------
Achin Gupta9ac63c52014-01-16 12:08:03 +000075 */
Juan Castillo258e94f2014-06-25 17:26:36 +010076#if CTX_INCLUDE_FPREGS
Andrew Thoelke38bde412014-03-18 13:46:55 +000077func fpregs_context_save
Madhukar Pappireddy78728cd2024-06-17 15:22:36 -050078 stp q0, q1, [x0], #32
79 stp q2, q3, [x0], #32
80 stp q4, q5, [x0], #32
81 stp q6, q7, [x0], #32
82 stp q8, q9, [x0], #32
83 stp q10, q11, [x0], #32
84 stp q12, q13, [x0], #32
85 stp q14, q15, [x0], #32
86 stp q16, q17, [x0], #32
87 stp q18, q19, [x0], #32
88 stp q20, q21, [x0], #32
89 stp q22, q23, [x0], #32
90 stp q24, q25, [x0], #32
91 stp q26, q27, [x0], #32
92 stp q28, q29, [x0], #32
93 stp q30, q31, [x0], #32
Achin Gupta9ac63c52014-01-16 12:08:03 +000094
Madhukar Pappireddy78728cd2024-06-17 15:22:36 -050095 fpregs_state_save x0, x9
Achin Gupta9ac63c52014-01-16 12:08:03 +000096
97 ret
Kévin Petita877c252015-03-24 14:03:57 +000098endfunc fpregs_context_save
Achin Gupta9ac63c52014-01-16 12:08:03 +000099
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100100/* ------------------------------------------------------------------
101 * The following function follows the aapcs_64 strictly to use x9-x17
102 * (temporary caller-saved registers according to AArch64 PCS) to
103 * restore floating point register context. It assumes that 'x0' is
104 * pointing to a 'fp_regs' structure from where the register context
Achin Gupta9ac63c52014-01-16 12:08:03 +0000105 * will be restored.
106 *
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100107 * Access to VFP registers will trap if CPTR_EL3.TFP is set.
108 * However currently we don't use VFP registers nor set traps in
109 * Trusted Firmware, and assume it's cleared.
Achin Gupta9ac63c52014-01-16 12:08:03 +0000110 *
111 * TODO: Revisit when VFP is used in secure world
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100112 * ------------------------------------------------------------------
Achin Gupta9ac63c52014-01-16 12:08:03 +0000113 */
Andrew Thoelke38bde412014-03-18 13:46:55 +0000114func fpregs_context_restore
Madhukar Pappireddy78728cd2024-06-17 15:22:36 -0500115 ldp q0, q1, [x0], #32
116 ldp q2, q3, [x0], #32
117 ldp q4, q5, [x0], #32
118 ldp q6, q7, [x0], #32
119 ldp q8, q9, [x0], #32
120 ldp q10, q11, [x0], #32
121 ldp q12, q13, [x0], #32
122 ldp q14, q15, [x0], #32
123 ldp q16, q17, [x0], #32
124 ldp q18, q19, [x0], #32
125 ldp q20, q21, [x0], #32
126 ldp q22, q23, [x0], #32
127 ldp q24, q25, [x0], #32
128 ldp q26, q27, [x0], #32
129 ldp q28, q29, [x0], #32
130 ldp q30, q31, [x0], #32
Achin Gupta9ac63c52014-01-16 12:08:03 +0000131
Madhukar Pappireddy78728cd2024-06-17 15:22:36 -0500132 fpregs_state_restore x0, x9
Achin Gupta9ac63c52014-01-16 12:08:03 +0000133
Madhukar Pappireddy78728cd2024-06-17 15:22:36 -0500134 ret
135endfunc fpregs_context_restore
136#endif /* CTX_INCLUDE_FPREGS */
Achin Gupta9ac63c52014-01-16 12:08:03 +0000137
Madhukar Pappireddy78728cd2024-06-17 15:22:36 -0500138#if CTX_INCLUDE_SVE_REGS
139/*
140 * Helper macros for SVE predicates save/restore operations.
141 */
142.macro sve_predicate_op op:req reg:req
143 \op p0, [\reg, #0, MUL VL]
144 \op p1, [\reg, #1, MUL VL]
145 \op p2, [\reg, #2, MUL VL]
146 \op p3, [\reg, #3, MUL VL]
147 \op p4, [\reg, #4, MUL VL]
148 \op p5, [\reg, #5, MUL VL]
149 \op p6, [\reg, #6, MUL VL]
150 \op p7, [\reg, #7, MUL VL]
151 \op p8, [\reg, #8, MUL VL]
152 \op p9, [\reg, #9, MUL VL]
153 \op p10, [\reg, #10, MUL VL]
154 \op p11, [\reg, #11, MUL VL]
155 \op p12, [\reg, #12, MUL VL]
156 \op p13, [\reg, #13, MUL VL]
157 \op p14, [\reg, #14, MUL VL]
158 \op p15, [\reg, #15, MUL VL]
159.endm
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000160
Madhukar Pappireddy78728cd2024-06-17 15:22:36 -0500161.macro sve_vectors_op op:req reg:req
162 \op z0, [\reg, #0, MUL VL]
163 \op z1, [\reg, #1, MUL VL]
164 \op z2, [\reg, #2, MUL VL]
165 \op z3, [\reg, #3, MUL VL]
166 \op z4, [\reg, #4, MUL VL]
167 \op z5, [\reg, #5, MUL VL]
168 \op z6, [\reg, #6, MUL VL]
169 \op z7, [\reg, #7, MUL VL]
170 \op z8, [\reg, #8, MUL VL]
171 \op z9, [\reg, #9, MUL VL]
172 \op z10, [\reg, #10, MUL VL]
173 \op z11, [\reg, #11, MUL VL]
174 \op z12, [\reg, #12, MUL VL]
175 \op z13, [\reg, #13, MUL VL]
176 \op z14, [\reg, #14, MUL VL]
177 \op z15, [\reg, #15, MUL VL]
178 \op z16, [\reg, #16, MUL VL]
179 \op z17, [\reg, #17, MUL VL]
180 \op z18, [\reg, #18, MUL VL]
181 \op z19, [\reg, #19, MUL VL]
182 \op z20, [\reg, #20, MUL VL]
183 \op z21, [\reg, #21, MUL VL]
184 \op z22, [\reg, #22, MUL VL]
185 \op z23, [\reg, #23, MUL VL]
186 \op z24, [\reg, #24, MUL VL]
187 \op z25, [\reg, #25, MUL VL]
188 \op z26, [\reg, #26, MUL VL]
189 \op z27, [\reg, #27, MUL VL]
190 \op z28, [\reg, #28, MUL VL]
191 \op z29, [\reg, #29, MUL VL]
192 \op z30, [\reg, #30, MUL VL]
193 \op z31, [\reg, #31, MUL VL]
194.endm
195
196/* ------------------------------------------------------------------
197 * The following function follows the aapcs_64 strictly to use x9-x17
198 * (temporary caller-saved registers according to AArch64 PCS) to
199 * restore SVE register context. It assumes that 'x0' is
200 * pointing to a 'sve_regs_t' structure to which the register context
201 * will be saved.
202 * ------------------------------------------------------------------
203 */
204func sve_context_save
205.arch_extension sve
206 /* Temporarily enable SVE */
207 mrs x10, cptr_el3
208 orr x11, x10, #CPTR_EZ_BIT
209 bic x11, x11, #TFP_BIT
210 msr cptr_el3, x11
211 isb
212
213 /* zcr_el3 */
214 mrs x12, S3_6_C1_C2_0
215 mov x13, #((SVE_VECTOR_LEN >> 7) - 1)
216 msr S3_6_C1_C2_0, x13
217 isb
218
219 /* Predicate registers */
220 mov x13, #CTX_SIMD_PREDICATES
221 add x9, x0, x13
222 sve_predicate_op str, x9
223
224 /* Save FFR after predicates */
225 mov x13, #CTX_SIMD_FFR
226 add x9, x0, x13
227 rdffr p0.b
228 str p0, [x9]
229
230 /* Save vector registers */
231 mov x13, #CTX_SIMD_VECTORS
232 add x9, x0, x13
233 sve_vectors_op str, x9
234
235 /* Restore SVE enablement */
236 msr S3_6_C1_C2_0, x12 /* zcr_el3 */
237 msr cptr_el3, x10
238 isb
239.arch_extension nosve
240
241 /* Save FPSR, FPCR and FPEXC32 */
242 fpregs_state_save x0, x9
Achin Gupta9ac63c52014-01-16 12:08:03 +0000243
244 ret
Madhukar Pappireddy78728cd2024-06-17 15:22:36 -0500245endfunc sve_context_save
246
247/* ------------------------------------------------------------------
248 * The following function follows the aapcs_64 strictly to use x9-x17
249 * (temporary caller-saved registers according to AArch64 PCS) to
250 * restore SVE register context. It assumes that 'x0' is pointing to
251 * a 'sve_regs_t' structure from where the register context will be
252 * restored.
253 * ------------------------------------------------------------------
254 */
255func sve_context_restore
256.arch_extension sve
257 /* Temporarily enable SVE for EL3 */
258 mrs x10, cptr_el3
259 orr x11, x10, #CPTR_EZ_BIT
260 bic x11, x11, #TFP_BIT
261 msr cptr_el3, x11
262 isb
263
264 /* zcr_el3 */
265 mrs x12, S3_6_C1_C2_0
266 mov x13, #((SVE_VECTOR_LEN >> 7) - 1)
267 msr S3_6_C1_C2_0, x13
268 isb
269
270 /* Restore FFR register before predicates */
271 mov x13, #CTX_SIMD_FFR
272 add x9, x0, x13
273 ldr p0, [x9]
274 wrffr p0.b
275
276 /* Restore predicate registers */
277 mov x13, #CTX_SIMD_PREDICATES
278 add x9, x0, x13
279 sve_predicate_op ldr, x9
280
281 /* Restore vector registers */
282 mov x13, #CTX_SIMD_VECTORS
283 add x9, x0, x13
284 sve_vectors_op ldr, x9
285
286 /* Restore SVE enablement */
287 msr S3_6_C1_C2_0, x12 /* zcr_el3 */
288 msr cptr_el3, x10
289 isb
290.arch_extension nosve
291
292 /* Restore FPSR, FPCR and FPEXC32 */
293 fpregs_state_restore x0, x9
294 ret
295endfunc sve_context_restore
296#endif /* CTX_INCLUDE_SVE_REGS */
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100297
Daniel Boulby928747f2021-05-25 18:09:34 +0100298 /*
Manish Pandey62d532a2022-11-17 15:47:05 +0000299 * Set SCR_EL3.EA bit to enable SErrors at EL3
300 */
301 .macro enable_serror_at_el3
Madhukar Pappireddy78728cd2024-06-17 15:22:36 -0500302 mrs x8, scr_el3
303 orr x8, x8, #SCR_EA_BIT
304 msr scr_el3, x8
Manish Pandey62d532a2022-11-17 15:47:05 +0000305 .endm
306
307 /*
Daniel Boulby928747f2021-05-25 18:09:34 +0100308 * Set the PSTATE bits not set when the exception was taken as
309 * described in the AArch64.TakeException() pseudocode function
310 * in ARM DDI 0487F.c page J1-7635 to a default value.
311 */
312 .macro set_unset_pstate_bits
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000313 /*
314 * If Data Independent Timing (DIT) functionality is implemented,
315 * always enable DIT in EL3
316 */
Daniel Boulby928747f2021-05-25 18:09:34 +0100317#if ENABLE_FEAT_DIT
Manish Pandey771d40d2024-07-18 15:18:20 +0100318#if ENABLE_FEAT_DIT >= 2
Andre Przywara1f55c412023-01-26 16:47:52 +0000319 mrs x8, id_aa64pfr0_el1
320 and x8, x8, #(ID_AA64PFR0_DIT_MASK << ID_AA64PFR0_DIT_SHIFT)
321 cbz x8, 1f
322#endif
Madhukar Pappireddy78728cd2024-06-17 15:22:36 -0500323 mov x8, #DIT_BIT
324 msr DIT, x8
Andre Przywara1f55c412023-01-26 16:47:52 +00003251:
Daniel Boulby928747f2021-05-25 18:09:34 +0100326#endif /* ENABLE_FEAT_DIT */
327 .endm /* set_unset_pstate_bits */
328
Arvind Ram Prakashab28d4b2023-10-11 12:10:56 -0500329/*-------------------------------------------------------------------------
330 * This macro checks the ENABLE_FEAT_MPAM state, performs ID register
331 * check to see if the platform supports MPAM extension and restores MPAM3
332 * register value if it is FEAT_STATE_ENABLED/FEAT_STATE_CHECKED.
333 *
334 * This is particularly more complicated because we can't check
335 * if the platform supports MPAM by looking for status of a particular bit
336 * in the MDCR_EL3 or CPTR_EL3 register like other extensions.
337 * ------------------------------------------------------------------------
338 */
339
340 .macro restore_mpam3_el3
341#if ENABLE_FEAT_MPAM
Manish Pandey771d40d2024-07-18 15:18:20 +0100342#if ENABLE_FEAT_MPAM >= 2
Arvind Ram Prakashab28d4b2023-10-11 12:10:56 -0500343 mrs x8, id_aa64pfr0_el1
344 lsr x8, x8, #(ID_AA64PFR0_MPAM_SHIFT)
345 and x8, x8, #(ID_AA64PFR0_MPAM_MASK)
346 mrs x7, id_aa64pfr1_el1
347 lsr x7, x7, #(ID_AA64PFR1_MPAM_FRAC_SHIFT)
348 and x7, x7, #(ID_AA64PFR1_MPAM_FRAC_MASK)
349 orr x7, x7, x8
350 cbz x7, no_mpam
351#endif
352 /* -----------------------------------------------------------
353 * Restore MPAM3_EL3 register as per context state
354 * Currently we only enable MPAM for NS world and trap to EL3
355 * for MPAM access in lower ELs of Secure and Realm world
Arvind Ram Prakashb5d95592023-11-08 12:28:30 -0600356 * x9 holds address of the per_world context
Arvind Ram Prakashab28d4b2023-10-11 12:10:56 -0500357 * -----------------------------------------------------------
358 */
Arvind Ram Prakashb5d95592023-11-08 12:28:30 -0600359
360 ldr x17, [x9, #CTX_MPAM3_EL3]
Arvind Ram Prakashab28d4b2023-10-11 12:10:56 -0500361 msr S3_6_C10_C5_0, x17 /* mpam3_el3 */
362
363no_mpam:
364#endif
365 .endm /* restore_mpam3_el3 */
366
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100367/* ------------------------------------------------------------------
Daniel Boulby95fb1aa2022-01-19 11:20:05 +0000368 * The following macro is used to save and restore all the general
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100369 * purpose and ARMv8.3-PAuth (if enabled) registers.
Jayanth Dodderi Chidanand4ec78ad2022-09-19 23:32:08 +0100370 * It also checks if the Secure Cycle Counter (PMCCNTR_EL0)
371 * is disabled in EL3/Secure (ARMv8.5-PMU), wherein PMCCNTR_EL0
372 * needs not to be saved/restored during world switch.
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100373 *
374 * Ideally we would only save and restore the callee saved registers
375 * when a world switch occurs but that type of implementation is more
376 * complex. So currently we will always save and restore these
377 * registers on entry and exit of EL3.
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100378 * clobbers: x18
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100379 * ------------------------------------------------------------------
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100380 */
Daniel Boulby95fb1aa2022-01-19 11:20:05 +0000381 .macro save_gp_pmcr_pauth_regs
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100382 stp x0, x1, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X0]
383 stp x2, x3, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X2]
384 stp x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4]
385 stp x6, x7, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X6]
386 stp x8, x9, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X8]
387 stp x10, x11, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X10]
388 stp x12, x13, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X12]
389 stp x14, x15, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X14]
390 stp x16, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X16]
391 stp x18, x19, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X18]
392 stp x20, x21, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X20]
393 stp x22, x23, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X22]
394 stp x24, x25, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X24]
395 stp x26, x27, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X26]
396 stp x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28]
397 mrs x18, sp_el0
398 str x18, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_SP_EL0]
Boyan Karatotev05504ba2023-02-15 13:21:50 +0000399
400 /* PMUv3 is presumed to be always present */
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100401 mrs x9, pmcr_el0
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100402 str x9, [sp, #CTX_EL3STATE_OFFSET + CTX_PMCR_EL0]
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100403 /* Disable cycle counter when event counting is prohibited */
Boyan Karatoteved85cf72022-12-06 09:03:42 +0000404 orr x9, x9, #PMCR_EL0_DP_BIT
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100405 msr pmcr_el0, x9
406 isb
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100407#if CTX_INCLUDE_PAUTH_REGS
408 /* ----------------------------------------------------------
409 * Save the ARMv8.3-PAuth keys as they are not banked
410 * by exception level
411 * ----------------------------------------------------------
412 */
413 add x19, sp, #CTX_PAUTH_REGS_OFFSET
414
415 mrs x20, APIAKeyLo_EL1 /* x21:x20 = APIAKey */
416 mrs x21, APIAKeyHi_EL1
417 mrs x22, APIBKeyLo_EL1 /* x23:x22 = APIBKey */
418 mrs x23, APIBKeyHi_EL1
419 mrs x24, APDAKeyLo_EL1 /* x25:x24 = APDAKey */
420 mrs x25, APDAKeyHi_EL1
421 mrs x26, APDBKeyLo_EL1 /* x27:x26 = APDBKey */
422 mrs x27, APDBKeyHi_EL1
423 mrs x28, APGAKeyLo_EL1 /* x29:x28 = APGAKey */
424 mrs x29, APGAKeyHi_EL1
425
426 stp x20, x21, [x19, #CTX_PACIAKEY_LO]
427 stp x22, x23, [x19, #CTX_PACIBKEY_LO]
428 stp x24, x25, [x19, #CTX_PACDAKEY_LO]
429 stp x26, x27, [x19, #CTX_PACDBKEY_LO]
430 stp x28, x29, [x19, #CTX_PACGAKEY_LO]
431#endif /* CTX_INCLUDE_PAUTH_REGS */
Daniel Boulby95fb1aa2022-01-19 11:20:05 +0000432 .endm /* save_gp_pmcr_pauth_regs */
433
434/* -----------------------------------------------------------------
Daniel Boulby928747f2021-05-25 18:09:34 +0100435 * This function saves the context and sets the PSTATE to a known
436 * state, preparing entry to el3.
Daniel Boulby95fb1aa2022-01-19 11:20:05 +0000437 * Save all the general purpose and ARMv8.3-PAuth (if enabled)
438 * registers.
Daniel Boulby928747f2021-05-25 18:09:34 +0100439 * Then set any of the PSTATE bits that are not set by hardware
440 * according to the Aarch64.TakeException pseudocode in the Arm
441 * Architecture Reference Manual to a default value for EL3.
442 * clobbers: x17
Daniel Boulby95fb1aa2022-01-19 11:20:05 +0000443 * -----------------------------------------------------------------
444 */
445func prepare_el3_entry
446 save_gp_pmcr_pauth_regs
Manish Pandey62d532a2022-11-17 15:47:05 +0000447 enable_serror_at_el3
Daniel Boulby928747f2021-05-25 18:09:34 +0100448 /*
449 * Set the PSTATE bits not described in the Aarch64.TakeException
450 * pseudocode to their default values.
451 */
452 set_unset_pstate_bits
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100453 ret
Daniel Boulby95fb1aa2022-01-19 11:20:05 +0000454endfunc prepare_el3_entry
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100455
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100456/* ------------------------------------------------------------------
457 * This function restores ARMv8.3-PAuth (if enabled) and all general
458 * purpose registers except x30 from the CPU context.
459 * x30 register must be explicitly restored by the caller.
460 * ------------------------------------------------------------------
Jeenu Viswambharan23d05a82017-11-29 16:59:34 +0000461 */
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100462func restore_gp_pmcr_pauth_regs
463#if CTX_INCLUDE_PAUTH_REGS
464 /* Restore the ARMv8.3 PAuth keys */
465 add x10, sp, #CTX_PAUTH_REGS_OFFSET
466
467 ldp x0, x1, [x10, #CTX_PACIAKEY_LO] /* x1:x0 = APIAKey */
468 ldp x2, x3, [x10, #CTX_PACIBKEY_LO] /* x3:x2 = APIBKey */
469 ldp x4, x5, [x10, #CTX_PACDAKEY_LO] /* x5:x4 = APDAKey */
470 ldp x6, x7, [x10, #CTX_PACDBKEY_LO] /* x7:x6 = APDBKey */
471 ldp x8, x9, [x10, #CTX_PACGAKEY_LO] /* x9:x8 = APGAKey */
472
473 msr APIAKeyLo_EL1, x0
474 msr APIAKeyHi_EL1, x1
475 msr APIBKeyLo_EL1, x2
476 msr APIBKeyHi_EL1, x3
477 msr APDAKeyLo_EL1, x4
478 msr APDAKeyHi_EL1, x5
479 msr APDBKeyLo_EL1, x6
480 msr APDBKeyHi_EL1, x7
481 msr APGAKeyLo_EL1, x8
482 msr APGAKeyHi_EL1, x9
483#endif /* CTX_INCLUDE_PAUTH_REGS */
Boyan Karatotev05504ba2023-02-15 13:21:50 +0000484
485 /* PMUv3 is presumed to be always present */
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100486 ldr x0, [sp, #CTX_EL3STATE_OFFSET + CTX_PMCR_EL0]
487 msr pmcr_el0, x0
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100488 ldp x0, x1, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X0]
489 ldp x2, x3, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X2]
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100490 ldp x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4]
491 ldp x6, x7, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X6]
492 ldp x8, x9, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X8]
493 ldp x10, x11, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X10]
494 ldp x12, x13, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X12]
495 ldp x14, x15, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X14]
Jeenu Viswambharan23d05a82017-11-29 16:59:34 +0000496 ldp x16, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X16]
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100497 ldp x18, x19, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X18]
498 ldp x20, x21, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X20]
499 ldp x22, x23, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X22]
500 ldp x24, x25, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X24]
501 ldp x26, x27, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X26]
Jeenu Viswambharan23d05a82017-11-29 16:59:34 +0000502 ldr x28, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_SP_EL0]
503 msr sp_el0, x28
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100504 ldp x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28]
Jeenu Viswambharan23d05a82017-11-29 16:59:34 +0000505 ret
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100506endfunc restore_gp_pmcr_pauth_regs
Jeenu Viswambharan23d05a82017-11-29 16:59:34 +0000507
Jayanth Dodderi Chidanand3a71df62024-06-05 11:13:05 +0100508#if ERRATA_SPECULATIVE_AT
509/* --------------------------------------------------------------------
Manish V Badarkhee07e8082020-07-23 12:43:25 +0100510 * In case of ERRATA_SPECULATIVE_AT, save SCTLR_EL1 and TCR_EL1
511 * registers and update EL1 registers to disable stage1 and stage2
Jayanth Dodderi Chidanand3a71df62024-06-05 11:13:05 +0100512 * page table walk.
513 * --------------------------------------------------------------------
Manish V Badarkhee07e8082020-07-23 12:43:25 +0100514 */
515func save_and_update_ptw_el1_sys_regs
516 /* ----------------------------------------------------------
517 * Save only sctlr_el1 and tcr_el1 registers
518 * ----------------------------------------------------------
519 */
520 mrs x29, sctlr_el1
Jayanth Dodderi Chidanand3a71df62024-06-05 11:13:05 +0100521 str x29, [sp, #(CTX_ERRATA_SPEC_AT_OFFSET + CTX_ERRATA_SPEC_AT_SCTLR_EL1)]
Manish V Badarkhee07e8082020-07-23 12:43:25 +0100522 mrs x29, tcr_el1
Jayanth Dodderi Chidanand3a71df62024-06-05 11:13:05 +0100523 str x29, [sp, #(CTX_ERRATA_SPEC_AT_OFFSET + CTX_ERRATA_SPEC_AT_TCR_EL1)]
Manish V Badarkhee07e8082020-07-23 12:43:25 +0100524
525 /* ------------------------------------------------------------
526 * Must follow below order in order to disable page table
527 * walk for lower ELs (EL1 and EL0). First step ensures that
528 * page table walk is disabled for stage1 and second step
529 * ensures that page table walker should use TCR_EL1.EPDx
530 * bits to perform address translation. ISB ensures that CPU
531 * does these 2 steps in order.
532 *
533 * 1. Update TCR_EL1.EPDx bits to disable page table walk by
534 * stage1.
535 * 2. Enable MMU bit to avoid identity mapping via stage2
536 * and force TCR_EL1.EPDx to be used by the page table
537 * walker.
538 * ------------------------------------------------------------
539 */
540 orr x29, x29, #(TCR_EPD0_BIT)
541 orr x29, x29, #(TCR_EPD1_BIT)
542 msr tcr_el1, x29
543 isb
544 mrs x29, sctlr_el1
545 orr x29, x29, #SCTLR_M_BIT
546 msr sctlr_el1, x29
547 isb
Manish V Badarkhee07e8082020-07-23 12:43:25 +0100548 ret
549endfunc save_and_update_ptw_el1_sys_regs
550
Jayanth Dodderi Chidanand3a71df62024-06-05 11:13:05 +0100551#endif /* ERRATA_SPECULATIVE_AT */
552
Elizabeth Ho4fc00d22023-07-18 14:10:25 +0100553/* -----------------------------------------------------------------
554* The below macro returns the address of the per_world context for
555* the security state, retrieved through "get_security_state" macro.
556* The per_world context address is returned in the register argument.
557* Clobbers: x9, x10
558* ------------------------------------------------------------------
559*/
560
561.macro get_per_world_context _reg:req
562 ldr x10, [sp, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3]
563 get_security_state x9, x10
Jayanth Dodderi Chidanand56aa3822023-12-11 11:22:02 +0000564 mov_imm x10, (CTX_PERWORLD_EL3STATE_END - CTX_CPTR_EL3)
Elizabeth Ho4fc00d22023-07-18 14:10:25 +0100565 mul x9, x9, x10
566 adrp x10, per_world_context
567 add x10, x10, :lo12:per_world_context
568 add x9, x9, x10
569 mov \_reg, x9
570.endm
571
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100572/* ------------------------------------------------------------------
573 * This routine assumes that the SP_EL3 is pointing to a valid
574 * context structure from where the gp regs and other special
575 * registers can be retrieved.
576 * ------------------------------------------------------------------
Antonio Nino Diaz13adfb12019-01-30 20:41:31 +0000577 */
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100578func el3_exit
Jan Dabrosfa015982019-12-02 13:30:03 +0100579#if ENABLE_ASSERTIONS
580 /* el3_exit assumes SP_EL0 on entry */
581 mrs x17, spsel
582 cmp x17, #MODE_SP_EL0
583 ASM_ASSERT(eq)
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000584#endif /* ENABLE_ASSERTIONS */
Jan Dabrosfa015982019-12-02 13:30:03 +0100585
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100586 /* ----------------------------------------------------------
587 * Save the current SP_EL0 i.e. the EL3 runtime stack which
588 * will be used for handling the next SMC.
589 * Then switch to SP_EL3.
590 * ----------------------------------------------------------
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100591 */
592 mov x17, sp
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100593 msr spsel, #MODE_SP_ELX
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100594 str x17, [sp, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
595
Max Shvetsovc4502772021-03-22 11:59:37 +0000596 /* ----------------------------------------------------------
Arunachalam Ganapathycac7d162021-07-08 09:35:57 +0100597 * Restore CPTR_EL3.
Max Shvetsovc4502772021-03-22 11:59:37 +0000598 * ZCR is only restored if SVE is supported and enabled.
599 * Synchronization is required before zcr_el3 is addressed.
600 * ----------------------------------------------------------
601 */
Elizabeth Ho4fc00d22023-07-18 14:10:25 +0100602
603 /* The address of the per_world context is stored in x9 */
604 get_per_world_context x9
605
606 ldp x19, x20, [x9, #CTX_CPTR_EL3]
Max Shvetsovc4502772021-03-22 11:59:37 +0000607 msr cptr_el3, x19
608
Boyan Karatotev8ae58f02023-04-20 11:00:50 +0100609#if IMAGE_BL31
Max Shvetsovc4502772021-03-22 11:59:37 +0000610 ands x19, x19, #CPTR_EZ_BIT
611 beq sve_not_enabled
612
613 isb
614 msr S3_6_C1_C2_0, x20 /* zcr_el3 */
615sve_not_enabled:
Arvind Ram Prakashab28d4b2023-10-11 12:10:56 -0500616
617 restore_mpam3_el3
618
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000619#endif /* IMAGE_BL31 */
Max Shvetsovc4502772021-03-22 11:59:37 +0000620
Dimitris Papastamosba51d9e2018-05-16 11:36:14 +0100621#if IMAGE_BL31 && DYNAMIC_WORKAROUND_CVE_2018_3639
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100622 /* ----------------------------------------------------------
623 * Restore mitigation state as it was on entry to EL3
624 * ----------------------------------------------------------
625 */
Dimitris Papastamosba51d9e2018-05-16 11:36:14 +0100626 ldr x17, [sp, #CTX_CVE_2018_3639_OFFSET + CTX_CVE_2018_3639_DISABLE]
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100627 cbz x17, 1f
Dimitris Papastamosba51d9e2018-05-16 11:36:14 +0100628 blr x17
Antonio Nino Diaz13adfb12019-01-30 20:41:31 +00006291:
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000630#endif /* IMAGE_BL31 && DYNAMIC_WORKAROUND_CVE_2018_3639 */
631
Manish Pandey6b5721f2023-06-26 17:46:14 +0100632#if IMAGE_BL31
633 synchronize_errors
634#endif /* IMAGE_BL31 */
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000635
Jayanth Dodderi Chidanand118b3352024-06-18 15:22:54 +0100636 /* --------------------------------------------------------------
637 * Restore MDCR_EL3, SPSR_EL3, ELR_EL3 and SCR_EL3 prior to ERET
638 * --------------------------------------------------------------
Manish Pandey53bc59a2022-11-17 14:43:15 +0000639 */
Manish Pandey53bc59a2022-11-17 14:43:15 +0000640 ldp x16, x17, [sp, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
Jayanth Dodderi Chidanand118b3352024-06-18 15:22:54 +0100641 ldr x18, [sp, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3]
642 ldr x19, [sp, #CTX_EL3STATE_OFFSET + CTX_MDCR_EL3]
Manish Pandey53bc59a2022-11-17 14:43:15 +0000643 msr spsr_el3, x16
644 msr elr_el3, x17
Jayanth Dodderi Chidanand118b3352024-06-18 15:22:54 +0100645 msr scr_el3, x18
646 msr mdcr_el3, x19
Manish Pandey53bc59a2022-11-17 14:43:15 +0000647
648 restore_ptw_el1_sys_regs
649
650 /* ----------------------------------------------------------
651 * Restore general purpose (including x30), PMCR_EL0 and
652 * ARMv8.3-PAuth registers.
653 * Exit EL3 via ERET to a lower exception level.
654 * ----------------------------------------------------------
655 */
656 bl restore_gp_pmcr_pauth_regs
657 ldr x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
658
Madhukar Pappireddyfba25722020-07-24 03:27:12 -0500659#ifdef IMAGE_BL31
Manish Pandey07952fb2023-05-25 13:46:14 +0100660 /* Clear the EL3 flag as we are exiting el3 */
661 str xzr, [sp, #CTX_EL3STATE_OFFSET + CTX_NESTED_EA_FLAG]
Jayanth Dodderi Chidanand72b69b82022-01-26 17:14:43 +0000662#endif /* IMAGE_BL31 */
663
Anthony Steinhauser0f7e6012020-01-07 15:44:06 -0800664 exception_return
Antonio Nino Diaz594811b2019-01-31 11:58:00 +0000665
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100666endfunc el3_exit