blob: 98cee1667c49089b6961ce6b6684506721395825 [file] [log] [blame]
Achin Gupta7aea9082014-02-01 07:51:28 +00001/*
Maksims Svecovs1e25c5b2023-02-02 16:10:22 +00002 * Copyright (c) 2013-2023, Arm Limited and Contributors. All rights reserved.
Varun Wadekarcc238bb2022-09-13 12:38:47 +01003 * Copyright (c) 2022, NVIDIA Corporation. All rights reserved.
Achin Gupta7aea9082014-02-01 07:51:28 +00004 *
dp-armfa3cf0b2017-05-03 09:38:09 +01005 * SPDX-License-Identifier: BSD-3-Clause
Achin Gupta7aea9082014-02-01 07:51:28 +00006 */
7
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00008#include <assert.h>
9#include <stdbool.h>
10#include <string.h>
11
12#include <platform_def.h>
13
Achin Gupta27b895e2014-05-04 18:38:28 +010014#include <arch.h>
Achin Gupta7aea9082014-02-01 07:51:28 +000015#include <arch_helpers.h>
Soby Mathew830f0ad2019-07-12 09:23:38 +010016#include <arch_features.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000017#include <bl31/interrupt_mgmt.h>
18#include <common/bl_common.h>
Claus Pedersen785e66c2022-09-12 22:42:58 +000019#include <common/debug.h>
Dan Handley2bd4ef22014-04-09 13:14:54 +010020#include <context.h>
Zelalem Awekef92c0cb2022-01-31 16:59:42 -060021#include <drivers/arm/gicv3.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000022#include <lib/el3_runtime/context_mgmt.h>
23#include <lib/el3_runtime/pubsub_events.h>
24#include <lib/extensions/amu.h>
johpow0181865962022-01-28 17:06:20 -060025#include <lib/extensions/brbe.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000026#include <lib/extensions/mpam.h>
Boyan Karatotev05504ba2023-02-15 13:21:50 +000027#include <lib/extensions/pmuv3.h>
johpow019baade32021-07-08 14:14:00 -050028#include <lib/extensions/sme.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000029#include <lib/extensions/spe.h>
30#include <lib/extensions/sve.h>
Manish V Badarkhef356f7e2021-06-29 11:44:20 +010031#include <lib/extensions/sys_reg_trace.h>
Manish V Badarkhe20df29c2021-07-02 09:10:56 +010032#include <lib/extensions/trbe.h>
Manish V Badarkhe51a97112021-07-08 09:33:18 +010033#include <lib/extensions/trf.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000034#include <lib/utils.h>
Achin Gupta7aea9082014-02-01 07:51:28 +000035
Jayanth Dodderi Chidanand4b5489c2022-03-28 15:28:55 +010036#if ENABLE_FEAT_TWED
37/* Make sure delay value fits within the range(0-15) */
38CASSERT(((TWED_DELAY & ~SCR_TWEDEL_MASK) == 0U), assert_twed_delay_value_check);
39#endif /* ENABLE_FEAT_TWED */
Achin Gupta7aea9082014-02-01 07:51:28 +000040
Boyan Karatotev36cebf92023-03-08 11:56:49 +000041static void manage_extensions_nonsecure(cpu_context_t *ctx);
Jayanth Dodderi Chidanand4b5489c2022-03-28 15:28:55 +010042static void manage_extensions_secure(cpu_context_t *ctx);
Zelalem Aweke20126002022-04-08 16:48:05 -050043
44static void setup_el1_context(cpu_context_t *ctx, const struct entry_point_info *ep)
45{
46 u_register_t sctlr_elx, actlr_elx;
47
48 /*
49 * Initialise SCTLR_EL1 to the reset value corresponding to the target
50 * execution state setting all fields rather than relying on the hw.
51 * Some fields have architecturally UNKNOWN reset values and these are
52 * set to zero.
53 *
54 * SCTLR.EE: Endianness is taken from the entrypoint attributes.
55 *
56 * SCTLR.M, SCTLR.C and SCTLR.I: These fields must be zero (as
57 * required by PSCI specification)
58 */
59 sctlr_elx = (EP_GET_EE(ep->h.attr) != 0U) ? SCTLR_EE_BIT : 0UL;
60 if (GET_RW(ep->spsr) == MODE_RW_64) {
61 sctlr_elx |= SCTLR_EL1_RES1;
62 } else {
63 /*
64 * If the target execution state is AArch32 then the following
65 * fields need to be set.
66 *
67 * SCTRL_EL1.nTWE: Set to one so that EL0 execution of WFE
68 * instructions are not trapped to EL1.
69 *
70 * SCTLR_EL1.nTWI: Set to one so that EL0 execution of WFI
71 * instructions are not trapped to EL1.
72 *
73 * SCTLR_EL1.CP15BEN: Set to one to enable EL0 execution of the
74 * CP15DMB, CP15DSB, and CP15ISB instructions.
75 */
76 sctlr_elx |= SCTLR_AARCH32_EL1_RES1 | SCTLR_CP15BEN_BIT
77 | SCTLR_NTWI_BIT | SCTLR_NTWE_BIT;
78 }
79
80#if ERRATA_A75_764081
81 /*
82 * If workaround of errata 764081 for Cortex-A75 is used then set
83 * SCTLR_EL1.IESB to enable Implicit Error Synchronization Barrier.
84 */
85 sctlr_elx |= SCTLR_IESB_BIT;
86#endif
87 /* Store the initialised SCTLR_EL1 value in the cpu_context */
88 write_ctx_reg(get_el1_sysregs_ctx(ctx), CTX_SCTLR_EL1, sctlr_elx);
89
90 /*
91 * Base the context ACTLR_EL1 on the current value, as it is
92 * implementation defined. The context restore process will write
93 * the value from the context to the actual register and can cause
94 * problems for processor cores that don't expect certain bits to
95 * be zero.
96 */
97 actlr_elx = read_actlr_el1();
98 write_ctx_reg((get_el1_sysregs_ctx(ctx)), (CTX_ACTLR_EL1), (actlr_elx));
99}
100
Zelalem Aweke42401112022-01-05 17:12:24 -0600101/******************************************************************************
102 * This function performs initializations that are specific to SECURE state
103 * and updates the cpu context specified by 'ctx'.
104 *****************************************************************************/
105static void setup_secure_context(cpu_context_t *ctx, const struct entry_point_info *ep)
Achin Gupta7aea9082014-02-01 07:51:28 +0000106{
Zelalem Aweke42401112022-01-05 17:12:24 -0600107 u_register_t scr_el3;
108 el3_state_t *state;
109
110 state = get_el3state_ctx(ctx);
111 scr_el3 = read_ctx_reg(state, CTX_SCR_EL3);
112
113#if defined(IMAGE_BL31) && !defined(SPD_spmd)
Achin Gupta7aea9082014-02-01 07:51:28 +0000114 /*
Zelalem Aweke42401112022-01-05 17:12:24 -0600115 * SCR_EL3.IRQ, SCR_EL3.FIQ: Enable the physical FIQ and IRQ routing as
116 * indicated by the interrupt routing model for BL31.
117 */
118 scr_el3 |= get_scr_el3_from_routing_model(SECURE);
119#endif
120
121#if !CTX_INCLUDE_MTE_REGS || ENABLE_ASSERTIONS
122 /* Get Memory Tagging Extension support level */
123 unsigned int mte = get_armv8_5_mte_support();
124#endif
125 /*
126 * Allow access to Allocation Tags when CTX_INCLUDE_MTE_REGS
127 * is set, or when MTE is only implemented at EL0.
Achin Gupta7aea9082014-02-01 07:51:28 +0000128 */
Zelalem Aweke42401112022-01-05 17:12:24 -0600129#if CTX_INCLUDE_MTE_REGS
130 assert((mte == MTE_IMPLEMENTED_ELX) || (mte == MTE_IMPLEMENTED_ASY));
131 scr_el3 |= SCR_ATA_BIT;
132#else
133 if (mte == MTE_IMPLEMENTED_EL0) {
134 scr_el3 |= SCR_ATA_BIT;
135 }
136#endif /* CTX_INCLUDE_MTE_REGS */
137
Zelalem Aweke42401112022-01-05 17:12:24 -0600138 write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
139
Zelalem Aweke20126002022-04-08 16:48:05 -0500140 /*
141 * Initialize EL1 context registers unless SPMC is running
142 * at S-EL2.
143 */
144#if !SPMD_SPM_AT_SEL2
145 setup_el1_context(ctx, ep);
146#endif
147
Zelalem Aweke42401112022-01-05 17:12:24 -0600148 manage_extensions_secure(ctx);
Achin Gupta7aea9082014-02-01 07:51:28 +0000149}
150
Zelalem Aweke42401112022-01-05 17:12:24 -0600151#if ENABLE_RME
152/******************************************************************************
153 * This function performs initializations that are specific to REALM state
154 * and updates the cpu context specified by 'ctx'.
155 *****************************************************************************/
156static void setup_realm_context(cpu_context_t *ctx, const struct entry_point_info *ep)
157{
158 u_register_t scr_el3;
159 el3_state_t *state;
160
161 state = get_el3state_ctx(ctx);
162 scr_el3 = read_ctx_reg(state, CTX_SCR_EL3);
163
Maksims Svecovs1e25c5b2023-02-02 16:10:22 +0000164 scr_el3 |= SCR_NS_BIT | SCR_NSE_BIT;
165
Andre Przywara902c9022022-11-17 17:30:43 +0000166 if (is_feat_csv2_2_supported()) {
167 /* Enable access to the SCXTNUM_ELx registers. */
168 scr_el3 |= SCR_EnSCXT_BIT;
169 }
Zelalem Aweke42401112022-01-05 17:12:24 -0600170
171 write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
172}
173#endif /* ENABLE_RME */
174
175/******************************************************************************
176 * This function performs initializations that are specific to NON-SECURE state
177 * and updates the cpu context specified by 'ctx'.
178 *****************************************************************************/
179static void setup_ns_context(cpu_context_t *ctx, const struct entry_point_info *ep)
180{
181 u_register_t scr_el3;
182 el3_state_t *state;
183
184 state = get_el3state_ctx(ctx);
185 scr_el3 = read_ctx_reg(state, CTX_SCR_EL3);
186
187 /* SCR_NS: Set the NS bit */
188 scr_el3 |= SCR_NS_BIT;
189
Boyan Karatotev8ae58f02023-04-20 11:00:50 +0100190 /* Allow access to Allocation Tags when MTE is implemented. */
191 scr_el3 |= SCR_ATA_BIT;
192
Zelalem Aweke42401112022-01-05 17:12:24 -0600193#if !CTX_INCLUDE_PAUTH_REGS
194 /*
Boyan Karatotev8ae58f02023-04-20 11:00:50 +0100195 * Pointer Authentication feature, if present, is always enabled by default
196 * for Non secure lower exception levels. We do not have an explicit
197 * flag to set it.
198 * CTX_INCLUDE_PAUTH_REGS flag, is explicitly used to enable for lower
199 * exception levels of secure and realm worlds.
Zelalem Aweke42401112022-01-05 17:12:24 -0600200 *
Boyan Karatotev8ae58f02023-04-20 11:00:50 +0100201 * To prevent the leakage between the worlds during world switch,
202 * we enable it only for the non-secure world.
203 *
204 * If the Secure/realm world wants to use pointer authentication,
205 * CTX_INCLUDE_PAUTH_REGS must be explicitly set to 1, in which case
206 * it will be enabled globally for all the contexts.
207 *
208 * SCR_EL3.API: Set to one to not trap any PAuth instructions at ELs
209 * other than EL3
210 *
211 * SCR_EL3.APK: Set to one to not trap any PAuth key values at ELs other
212 * than EL3
Zelalem Aweke42401112022-01-05 17:12:24 -0600213 */
214 scr_el3 |= SCR_API_BIT | SCR_APK_BIT;
Zelalem Aweke42401112022-01-05 17:12:24 -0600215
Boyan Karatotev8ae58f02023-04-20 11:00:50 +0100216#endif /* CTX_INCLUDE_PAUTH_REGS */
Zelalem Aweke42401112022-01-05 17:12:24 -0600217
Manish Pandey0e3379d2022-10-10 11:43:08 +0100218#if HANDLE_EA_EL3_FIRST_NS
219 /* SCR_EL3.EA: Route External Abort and SError Interrupt to EL3. */
220 scr_el3 |= SCR_EA_BIT;
221#endif
222
Manish Pandey7c6fcb42022-09-27 14:30:34 +0100223#if RAS_TRAP_NS_ERR_REC_ACCESS
224 /*
225 * SCR_EL3.TERR: Trap Error record accesses. Accesses to the RAS ERR
226 * and RAS ERX registers from EL1 and EL2(from any security state)
227 * are trapped to EL3.
228 * Set here to trap only for NS EL1/EL2
229 *
230 */
231 scr_el3 |= SCR_TERR_BIT;
232#endif
233
Andre Przywara902c9022022-11-17 17:30:43 +0000234 if (is_feat_csv2_2_supported()) {
235 /* Enable access to the SCXTNUM_ELx registers. */
236 scr_el3 |= SCR_EnSCXT_BIT;
237 }
Maksims Svecovs1e25c5b2023-02-02 16:10:22 +0000238
Zelalem Aweke42401112022-01-05 17:12:24 -0600239#ifdef IMAGE_BL31
240 /*
241 * SCR_EL3.IRQ, SCR_EL3.FIQ: Enable the physical FIQ and IRQ routing as
242 * indicated by the interrupt routing model for BL31.
243 */
244 scr_el3 |= get_scr_el3_from_routing_model(NON_SECURE);
245#endif
246 write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
Zelalem Awekef92c0cb2022-01-31 16:59:42 -0600247
Zelalem Aweke20126002022-04-08 16:48:05 -0500248 /* Initialize EL1 context registers */
249 setup_el1_context(ctx, ep);
250
Zelalem Awekef92c0cb2022-01-31 16:59:42 -0600251 /* Initialize EL2 context registers */
252#if CTX_INCLUDE_EL2_REGS
253
254 /*
255 * Initialize SCTLR_EL2 context register using Endianness value
256 * taken from the entrypoint attribute.
257 */
258 u_register_t sctlr_el2 = (EP_GET_EE(ep->h.attr) != 0U) ? SCTLR_EE_BIT : 0UL;
259 sctlr_el2 |= SCTLR_EL2_RES1;
260 write_ctx_reg(get_el2_sysregs_ctx(ctx), CTX_SCTLR_EL2,
261 sctlr_el2);
262
Juan Pablo Conde72e0da12023-02-22 10:09:52 -0600263 if (is_feat_hcx_supported()) {
264 /*
265 * Initialize register HCRX_EL2 with its init value.
266 * As the value of HCRX_EL2 is UNKNOWN on reset, there is a
267 * chance that this can lead to unexpected behavior in lower
268 * ELs that have not been updated since the introduction of
269 * this feature if not properly initialized, especially when
270 * it comes to those bits that enable/disable traps.
271 */
272 write_ctx_reg(get_el2_sysregs_ctx(ctx), CTX_HCRX_EL2,
273 HCRX_EL2_INIT_VAL);
274 }
Juan Pablo Condef7252982023-07-10 16:00:41 -0500275
276 if (is_feat_fgt_supported()) {
277 /*
278 * Initialize HFG*_EL2 registers with a default value so legacy
279 * systems unaware of FEAT_FGT do not get trapped due to their lack
280 * of initialization for this feature.
281 */
282 write_ctx_reg(get_el2_sysregs_ctx(ctx), CTX_HFGITR_EL2,
283 HFGITR_EL2_INIT_VAL);
284 write_ctx_reg(get_el2_sysregs_ctx(ctx), CTX_HFGRTR_EL2,
285 HFGRTR_EL2_INIT_VAL);
286 write_ctx_reg(get_el2_sysregs_ctx(ctx), CTX_HFGWTR_EL2,
287 HFGWTR_EL2_INIT_VAL);
288 }
Zelalem Awekef92c0cb2022-01-31 16:59:42 -0600289#endif /* CTX_INCLUDE_EL2_REGS */
Boyan Karatotev36cebf92023-03-08 11:56:49 +0000290
291 manage_extensions_nonsecure(ctx);
Zelalem Aweke42401112022-01-05 17:12:24 -0600292}
293
Achin Gupta7aea9082014-02-01 07:51:28 +0000294/*******************************************************************************
Zelalem Aweke42401112022-01-05 17:12:24 -0600295 * The following function performs initialization of the cpu_context 'ctx'
296 * for first use that is common to all security states, and sets the
297 * initial entrypoint state as specified by the entry_point_info structure.
Andrew Thoelke4e126072014-06-04 21:10:52 +0100298 *
Paul Beesley1fbc97b2019-01-11 18:26:51 +0000299 * The EE and ST attributes are used to configure the endianness and secure
Soby Mathewb0082d22015-04-09 13:40:55 +0100300 * timer availability for the new execution context.
Andrew Thoelke4e126072014-06-04 21:10:52 +0100301 ******************************************************************************/
Zelalem Aweke42401112022-01-05 17:12:24 -0600302static void setup_context_common(cpu_context_t *ctx, const entry_point_info_t *ep)
Andrew Thoelke4e126072014-06-04 21:10:52 +0100303{
Boyan Karatotev8ae58f02023-04-20 11:00:50 +0100304 u_register_t cptr_el3;
Louis Mayencourt1c819c32020-01-24 13:30:28 +0000305 u_register_t scr_el3;
Andrew Thoelke4e126072014-06-04 21:10:52 +0100306 el3_state_t *state;
307 gp_regs_t *gp_regs;
Andrew Thoelke4e126072014-06-04 21:10:52 +0100308
Boyan Karatotev8ae58f02023-04-20 11:00:50 +0100309 state = get_el3state_ctx(ctx);
310
Andrew Thoelke4e126072014-06-04 21:10:52 +0100311 /* Clear any residual register values from the context */
Douglas Raillarda8954fc2017-01-26 15:54:44 +0000312 zeromem(ctx, sizeof(*ctx));
Andrew Thoelke4e126072014-06-04 21:10:52 +0100313
314 /*
Boyan Karatotevef25db32023-05-23 12:04:00 +0100315 * The lower-EL context is zeroed so that no stale values leak to a world.
316 * It is assumed that an all-zero lower-EL context is good enough for it
317 * to boot correctly. However, there are very few registers where this
318 * is not true and some values need to be recreated.
319 */
320#if CTX_INCLUDE_EL2_REGS
321 el2_sysregs_t *el2_ctx = get_el2_sysregs_ctx(ctx);
322
323 /*
324 * These bits are set in the gicv3 driver. Losing them (especially the
325 * SRE bit) is problematic for all worlds. Henceforth recreate them.
326 */
327 u_register_t icc_sre_el2 = ICC_SRE_DIB_BIT | ICC_SRE_DFB_BIT |
328 ICC_SRE_EN_BIT | ICC_SRE_SRE_BIT;
329 write_ctx_reg(el2_ctx, CTX_ICC_SRE_EL2, icc_sre_el2);
330#endif /* CTX_INCLUDE_EL2_REGS */
331
Boyan Karatotev2e9e6f02023-05-22 15:53:58 +0100332 /* Start with a clean SCR_EL3 copy as all relevant values are set */
333 scr_el3 = SCR_RESET_VAL;
Zelalem Awekeb6301e62021-07-09 17:54:30 -0500334
David Cunadofee86532017-04-13 22:38:29 +0100335 /*
Boyan Karatotev8ae58f02023-04-20 11:00:50 +0100336 * SCR_EL3.TWE: Set to zero so that execution of WFE instructions at
337 * EL2, EL1 and EL0 are not trapped to EL3.
338 *
339 * SCR_EL3.TWI: Set to zero so that execution of WFI instructions at
340 * EL2, EL1 and EL0 are not trapped to EL3.
341 *
342 * SCR_EL3.SMD: Set to zero to enable SMC calls at EL1 and above, from
343 * both Security states and both Execution states.
344 *
345 * SCR_EL3.SIF: Set to one to disable secure instruction execution from
346 * Non-secure memory.
347 */
348 scr_el3 &= ~(SCR_TWE_BIT | SCR_TWI_BIT | SCR_SMD_BIT);
349
350 scr_el3 |= SCR_SIF_BIT;
351
352 /*
David Cunadofee86532017-04-13 22:38:29 +0100353 * SCR_EL3.RW: Set the execution state, AArch32 or AArch64, for next
354 * Exception level as specified by SPSR.
355 */
Zelalem Awekeb6301e62021-07-09 17:54:30 -0500356 if (GET_RW(ep->spsr) == MODE_RW_64) {
Andrew Thoelke4e126072014-06-04 21:10:52 +0100357 scr_el3 |= SCR_RW_BIT;
Zelalem Awekeb6301e62021-07-09 17:54:30 -0500358 }
Zelalem Aweke42401112022-01-05 17:12:24 -0600359
David Cunadofee86532017-04-13 22:38:29 +0100360 /*
361 * SCR_EL3.ST: Traps Secure EL1 accesses to the Counter-timer Physical
Zelalem Aweke20126002022-04-08 16:48:05 -0500362 * Secure timer registers to EL3, from AArch64 state only, if specified
363 * by the entrypoint attributes. If SEL2 is present and enabled, the ST
364 * bit always behaves as 1 (i.e. secure physical timer register access
365 * is not trapped)
David Cunadofee86532017-04-13 22:38:29 +0100366 */
Zelalem Awekeb6301e62021-07-09 17:54:30 -0500367 if (EP_GET_ST(ep->h.attr) != 0U) {
Andrew Thoelke4e126072014-06-04 21:10:52 +0100368 scr_el3 |= SCR_ST_BIT;
Zelalem Awekeb6301e62021-07-09 17:54:30 -0500369 }
Andrew Thoelke4e126072014-06-04 21:10:52 +0100370
johpow01f91e59f2021-08-04 19:38:18 -0500371 /*
372 * If FEAT_HCX is enabled, enable access to HCRX_EL2 by setting
373 * SCR_EL3.HXEn.
374 */
Andre Przywara1d8795e2022-11-15 11:45:19 +0000375 if (is_feat_hcx_supported()) {
376 scr_el3 |= SCR_HXEn_BIT;
377 }
johpow01f91e59f2021-08-04 19:38:18 -0500378
Juan Pablo Conde42305f22022-07-12 16:40:29 -0400379 /*
380 * If FEAT_RNG_TRAP is enabled, all reads of the RNDR and RNDRRS
381 * registers are trapped to EL3.
382 */
383#if ENABLE_FEAT_RNG_TRAP
384 scr_el3 |= SCR_TRNDR_BIT;
385#endif
386
Jeenu Viswambharanf00da742017-12-08 12:13:51 +0000387#if FAULT_INJECTION_SUPPORT
388 /* Enable fault injection from lower ELs */
389 scr_el3 |= SCR_FIEN_BIT;
390#endif
391
Boyan Karatotev8ae58f02023-04-20 11:00:50 +0100392#if CTX_INCLUDE_PAUTH_REGS
393 /*
394 * Enable Pointer Authentication globally for all the worlds.
395 *
396 * SCR_EL3.API: Set to one to not trap any PAuth instructions at ELs
397 * other than EL3
398 *
399 * SCR_EL3.APK: Set to one to not trap any PAuth key values at ELs other
400 * than EL3
401 */
402 scr_el3 |= SCR_API_BIT | SCR_APK_BIT;
403#endif /* CTX_INCLUDE_PAUTH_REGS */
404
Antonio Nino Diaz594811b2019-01-31 11:58:00 +0000405 /*
Mark Brownc37eee72023-03-14 20:13:03 +0000406 * SCR_EL3.TCR2EN: Enable access to TCR2_ELx for AArch64 if present.
407 */
408 if (is_feat_tcr2_supported() && (GET_RW(ep->spsr) == MODE_RW_64)) {
409 scr_el3 |= SCR_TCR2EN_BIT;
410 }
411
412 /*
Mark Brown293a6612023-03-14 20:48:43 +0000413 * SCR_EL3.PIEN: Enable permission indirection and overlay
414 * registers for AArch64 if present.
415 */
416 if (is_feat_sxpie_supported() || is_feat_sxpoe_supported()) {
417 scr_el3 |= SCR_PIEN_BIT;
418 }
419
420 /*
Mark Brown326f2952023-03-14 21:33:04 +0000421 * SCR_EL3.GCSEn: Enable GCS registers for AArch64 if present.
422 */
423 if ((is_feat_gcs_supported()) && (GET_RW(ep->spsr) == MODE_RW_64)) {
424 scr_el3 |= SCR_GCSEn_BIT;
425 }
426
427 /*
Boyan Karatotev8ae58f02023-04-20 11:00:50 +0100428 * Initialise CPTR_EL3, setting all fields rather than relying on hw.
429 * All fields are architecturally UNKNOWN on reset.
430 *
431 * CPTR_EL3.TFP: Set to zero so that accesses to the V- or Z- registers
432 * by Advanced SIMD, floating-point or SVE instructions (if
433 * implemented) do not trap to EL3.
434 *
435 * CPTR_EL3.TCPAC: Set to zero so that accesses to CPACR_EL1,
436 * CPTR_EL2,CPACR, or HCPTR do not trap to EL3.
Antonio Nino Diaz594811b2019-01-31 11:58:00 +0000437 */
Boyan Karatotev8ae58f02023-04-20 11:00:50 +0100438 cptr_el3 = CPTR_EL3_RESET_VAL & ~(TFP_BIT | TCPAC_BIT);
439
440 write_ctx_reg(state, CTX_CPTR_EL3, cptr_el3);
Max Shvetsovc4502772021-03-22 11:59:37 +0000441
Andrew Thoelke4e126072014-06-04 21:10:52 +0100442 /*
David Cunadofee86532017-04-13 22:38:29 +0100443 * SCR_EL3.HCE: Enable HVC instructions if next execution state is
444 * AArch64 and next EL is EL2, or if next execution state is AArch32 and
445 * next mode is Hyp.
Jimmy Brissonecc3c672020-04-16 10:47:56 -0500446 * SCR_EL3.FGTEn: Enable Fine Grained Virtualization Traps under the
447 * same conditions as HVC instructions and when the processor supports
448 * ARMv8.6-FGT.
Jimmy Brisson83573892020-04-16 10:48:02 -0500449 * SCR_EL3.ECVEn: Enable Enhanced Counter Virtualization (ECV)
450 * CNTPOFF_EL2 register under the same conditions as HVC instructions
451 * and when the processor supports ECV.
David Cunadofee86532017-04-13 22:38:29 +0100452 */
Antonio Nino Diaz864ca6f2018-10-31 15:25:35 +0000453 if (((GET_RW(ep->spsr) == MODE_RW_64) && (GET_EL(ep->spsr) == MODE_EL2))
454 || ((GET_RW(ep->spsr) != MODE_RW_64)
455 && (GET_M32(ep->spsr) == MODE32_hyp))) {
David Cunadofee86532017-04-13 22:38:29 +0100456 scr_el3 |= SCR_HCE_BIT;
Jimmy Brissonecc3c672020-04-16 10:47:56 -0500457
Andre Przywarae8920f62022-11-10 14:28:01 +0000458 if (is_feat_fgt_supported()) {
Jimmy Brissonecc3c672020-04-16 10:47:56 -0500459 scr_el3 |= SCR_FGTEN_BIT;
460 }
Jimmy Brisson83573892020-04-16 10:48:02 -0500461
Andre Przywarac3464182022-11-17 17:30:43 +0000462 if (is_feat_ecv_supported()) {
Jimmy Brisson83573892020-04-16 10:48:02 -0500463 scr_el3 |= SCR_ECVEN_BIT;
464 }
David Cunadofee86532017-04-13 22:38:29 +0100465 }
466
johpow013e24c162020-04-22 14:05:13 -0500467 /* Enable WFE trap delay in SCR_EL3 if supported and configured */
Andre Przywara0cf77402023-01-27 12:25:49 +0000468 if (is_feat_twed_supported()) {
469 /* Set delay in SCR_EL3 */
470 scr_el3 &= ~(SCR_TWEDEL_MASK << SCR_TWEDEL_SHIFT);
471 scr_el3 |= ((TWED_DELAY & SCR_TWEDEL_MASK)
472 << SCR_TWEDEL_SHIFT);
johpow013e24c162020-04-22 14:05:13 -0500473
Andre Przywara0cf77402023-01-27 12:25:49 +0000474 /* Enable WFE delay */
475 scr_el3 |= SCR_TWEDEn_BIT;
476 }
Jayanth Dodderi Chidanandf870cf62023-09-22 15:30:13 +0100477
478#if IMAGE_BL31 && defined(SPD_spmd) && SPMD_SPM_AT_SEL2
479 /* Enable S-EL2 if FEAT_SEL2 is implemented for all the contexts. */
480 if (is_feat_sel2_supported()) {
481 scr_el3 |= SCR_EEL2_BIT;
482 }
483#endif /* (IMAGE_BL31 && defined(SPD_spmd) && SPMD_SPM_AT_SEL2) */
johpow013e24c162020-04-22 14:05:13 -0500484
Arvind Ram Prakashab28d4b2023-10-11 12:10:56 -0500485 if (is_feat_mpam_supported()) {
486 write_ctx_reg(get_el3state_ctx(ctx), CTX_MPAM3_EL3, \
487 MPAM3_EL3_RESET_VAL);
488 }
489
David Cunadofee86532017-04-13 22:38:29 +0100490 /*
Alexei Fedorov503bbf32019-08-13 15:17:53 +0100491 * Populate EL3 state so that we've the right context
492 * before doing ERET
493 */
Andrew Thoelke4e126072014-06-04 21:10:52 +0100494 write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
495 write_ctx_reg(state, CTX_ELR_EL3, ep->pc);
496 write_ctx_reg(state, CTX_SPSR_EL3, ep->spsr);
497
498 /*
499 * Store the X0-X7 value from the entrypoint into the context
500 * Use memcpy as we are in control of the layout of the structures
501 */
502 gp_regs = get_gpregs_ctx(ctx);
503 memcpy(gp_regs, (void *)&ep->args, sizeof(aapcs64_params_t));
504}
505
506/*******************************************************************************
Zelalem Aweke42401112022-01-05 17:12:24 -0600507 * Context management library initialization routine. This library is used by
508 * runtime services to share pointers to 'cpu_context' structures for secure
509 * non-secure and realm states. Management of the structures and their associated
510 * memory is not done by the context management library e.g. the PSCI service
511 * manages the cpu context used for entry from and exit to the non-secure state.
512 * The Secure payload dispatcher service manages the context(s) corresponding to
513 * the secure state. It also uses this library to get access to the non-secure
514 * state cpu context pointers.
515 * Lastly, this library provides the API to make SP_EL3 point to the cpu context
516 * which will be used for programming an entry into a lower EL. The same context
517 * will be used to save state upon exception entry from that EL.
518 ******************************************************************************/
519void __init cm_init(void)
520{
521 /*
Elyes Haouas2be03c02023-02-13 09:14:48 +0100522 * The context management library has only global data to initialize, but
Zelalem Aweke42401112022-01-05 17:12:24 -0600523 * that will be done when the BSS is zeroed out.
524 */
525}
526
527/*******************************************************************************
528 * This is the high-level function used to initialize the cpu_context 'ctx' for
529 * first use. It performs initializations that are common to all security states
530 * and initializations specific to the security state specified in 'ep'
531 ******************************************************************************/
532void cm_setup_context(cpu_context_t *ctx, const entry_point_info_t *ep)
533{
534 unsigned int security_state;
535
536 assert(ctx != NULL);
537
538 /*
539 * Perform initializations that are common
540 * to all security states
541 */
542 setup_context_common(ctx, ep);
543
544 security_state = GET_SECURITY_STATE(ep->h.attr);
545
546 /* Perform security state specific initializations */
547 switch (security_state) {
548 case SECURE:
549 setup_secure_context(ctx, ep);
550 break;
551#if ENABLE_RME
552 case REALM:
553 setup_realm_context(ctx, ep);
554 break;
555#endif
556 case NON_SECURE:
557 setup_ns_context(ctx, ep);
558 break;
559 default:
560 ERROR("Invalid security state\n");
561 panic();
562 break;
563 }
564}
565
566/*******************************************************************************
Boyan Karatotev6468d4a2023-02-16 15:12:45 +0000567 * Enable architecture extensions for EL3 execution. This function only updates
568 * registers in-place which are expected to either never change or be
569 * overwritten by el3_exit.
570 ******************************************************************************/
571#if IMAGE_BL31
572void cm_manage_extensions_el3(void)
573{
574 if (is_feat_spe_supported()) {
575 spe_init_el3();
Boyan Karatotev7f5dcc72023-03-08 16:29:26 +0000576 }
577
Boyan Karatotev1e966f32023-03-27 17:02:43 +0100578 if (is_feat_amu_supported()) {
579 amu_init_el3();
580 }
581
Jayanth Dodderi Chidanand605419a2023-03-06 23:56:14 +0000582 if (is_feat_sme_supported()) {
Boyan Karatotev6468d4a2023-02-16 15:12:45 +0000583 sme_init_el3();
Jayanth Dodderi Chidanand605419a2023-03-06 23:56:14 +0000584 }
Arunachalam Ganapathycac7d162021-07-08 09:35:57 +0100585
Andre Przywara191eff62022-11-17 16:42:09 +0000586 if (is_feat_trbe_supported()) {
Boyan Karatotev6468d4a2023-02-16 15:12:45 +0000587 trbe_init_el3();
Andre Przywara191eff62022-11-17 16:42:09 +0000588 }
Manish V Badarkhe20df29c2021-07-02 09:10:56 +0100589
Andre Przywarac97c5512022-11-17 16:42:09 +0000590 if (is_feat_brbe_supported()) {
Boyan Karatotev6468d4a2023-02-16 15:12:45 +0000591 brbe_init_el3();
Andre Przywarac97c5512022-11-17 16:42:09 +0000592 }
johpow0181865962022-01-28 17:06:20 -0600593
Andre Przywara06ea44e2022-11-17 17:30:43 +0000594 if (is_feat_trf_supported()) {
Boyan Karatotev6468d4a2023-02-16 15:12:45 +0000595 trf_init_el3();
Andre Przywara06ea44e2022-11-17 17:30:43 +0000596 }
Dimitris Papastamos1e6f93e2017-11-07 09:55:29 +0000597
Boyan Karatotev6468d4a2023-02-16 15:12:45 +0000598 pmuv3_init_el3();
Boyan Karatotev36cebf92023-03-08 11:56:49 +0000599}
600#endif /* IMAGE_BL31 */
601
602/*******************************************************************************
603 * Enable architecture extensions on first entry to Non-secure world.
604 ******************************************************************************/
605static void manage_extensions_nonsecure(cpu_context_t *ctx)
606{
607#if IMAGE_BL31
Boyan Karatotev1e966f32023-03-27 17:02:43 +0100608 if (is_feat_amu_supported()) {
609 amu_enable(ctx);
610 }
611
Boyan Karatotev6468d4a2023-02-16 15:12:45 +0000612 /* Enable SVE and FPU/SIMD */
613 if (is_feat_sve_supported()) {
614 sve_enable(ctx);
615 }
616
617 if (is_feat_sme_supported()) {
618 sme_enable(ctx);
619 }
620
621 if (is_feat_sys_reg_trace_supported()) {
622 sys_reg_trace_enable(ctx);
623 }
624
Arvind Ram Prakashab28d4b2023-10-11 12:10:56 -0500625 if (is_feat_mpam_supported()) {
626 mpam_enable(ctx);
627 }
Boyan Karatotev05504ba2023-02-15 13:21:50 +0000628 pmuv3_enable(ctx);
Boyan Karatotev36cebf92023-03-08 11:56:49 +0000629#endif /* IMAGE_BL31 */
630}
631
Boyan Karatotevfe1cd942023-03-08 17:04:00 +0000632/* TODO: move to lib/extensions/pauth when it has been ported to FEAT_STATE */
633static __unused void enable_pauth_el2(void)
634{
635 u_register_t hcr_el2 = read_hcr_el2();
636 /*
637 * For Armv8.3 pointer authentication feature, disable traps to EL2 when
638 * accessing key registers or using pointer authentication instructions
639 * from lower ELs.
640 */
641 hcr_el2 |= (HCR_API_BIT | HCR_APK_BIT);
642
643 write_hcr_el2(hcr_el2);
644}
645
Boyan Karatotev36cebf92023-03-08 11:56:49 +0000646/*******************************************************************************
647 * Enable architecture extensions in-place at EL2 on first entry to Non-secure
648 * world when EL2 is empty and unused.
649 ******************************************************************************/
650static void manage_extensions_nonsecure_el2_unused(void)
651{
652#if IMAGE_BL31
Boyan Karatotev6468d4a2023-02-16 15:12:45 +0000653 if (is_feat_spe_supported()) {
654 spe_init_el2_unused();
655 }
656
Boyan Karatotev1e966f32023-03-27 17:02:43 +0100657 if (is_feat_amu_supported()) {
658 amu_init_el2_unused();
659 }
660
Boyan Karatotev6468d4a2023-02-16 15:12:45 +0000661 if (is_feat_mpam_supported()) {
662 mpam_init_el2_unused();
663 }
664
665 if (is_feat_trbe_supported()) {
666 trbe_init_el2_unused();
667 }
668
669 if (is_feat_sys_reg_trace_supported()) {
670 sys_reg_trace_init_el2_unused();
671 }
672
673 if (is_feat_trf_supported()) {
674 trf_init_el2_unused();
675 }
676
Boyan Karatotev05504ba2023-02-15 13:21:50 +0000677 pmuv3_init_el2_unused();
Boyan Karatotev6468d4a2023-02-16 15:12:45 +0000678
679 if (is_feat_sve_supported()) {
680 sve_init_el2_unused();
681 }
682
683 if (is_feat_sme_supported()) {
684 sme_init_el2_unused();
685 }
Boyan Karatotevfe1cd942023-03-08 17:04:00 +0000686
687#if ENABLE_PAUTH
688 enable_pauth_el2();
689#endif /* ENABLE_PAUTH */
Boyan Karatotev36cebf92023-03-08 11:56:49 +0000690#endif /* IMAGE_BL31 */
691}
692
693/*******************************************************************************
Arunachalam Ganapathycac7d162021-07-08 09:35:57 +0100694 * Enable architecture extensions on first entry to Secure world.
695 ******************************************************************************/
johpow019baade32021-07-08 14:14:00 -0500696static void manage_extensions_secure(cpu_context_t *ctx)
Arunachalam Ganapathycac7d162021-07-08 09:35:57 +0100697{
698#if IMAGE_BL31
Boyan Karatotev7f5dcc72023-03-08 16:29:26 +0000699 if (is_feat_sve_supported()) {
Jayanth Dodderi Chidanandd62c6812023-03-07 10:43:19 +0000700 if (ENABLE_SVE_FOR_SWD) {
701 /*
702 * Enable SVE and FPU in secure context, secure manager must
703 * ensure that the SVE and FPU register contexts are properly
704 * managed.
705 */
706 sve_enable(ctx);
707 } else {
708 /*
709 * Disable SVE and FPU in secure context so non-secure world
710 * can safely use them.
711 */
712 sve_disable(ctx);
713 }
714 }
715
Boyan Karatotev7f5dcc72023-03-08 16:29:26 +0000716 if (is_feat_sme_supported()) {
717 if (ENABLE_SME_FOR_SWD) {
718 /*
719 * Enable SME, SVE, FPU/SIMD in secure context, secure manager
720 * must ensure SME, SVE, and FPU/SIMD context properly managed.
721 */
Boyan Karatotev6468d4a2023-02-16 15:12:45 +0000722 sme_init_el3();
Boyan Karatotev7f5dcc72023-03-08 16:29:26 +0000723 sme_enable(ctx);
724 } else {
725 /*
726 * Disable SME, SVE, FPU/SIMD in secure context so non-secure
727 * world can safely use the associated registers.
728 */
729 sme_disable(ctx);
730 }
731 }
Boyan Karatotev919d3c82023-02-13 16:32:47 +0000732
733 /* NS can access this but Secure shouldn't */
734 if (is_feat_sys_reg_trace_supported()) {
735 sys_reg_trace_disable(ctx);
736 }
johpow019baade32021-07-08 14:14:00 -0500737#endif /* IMAGE_BL31 */
Arunachalam Ganapathycac7d162021-07-08 09:35:57 +0100738}
739
740/*******************************************************************************
Soby Mathewb0082d22015-04-09 13:40:55 +0100741 * The following function initializes the cpu_context for a CPU specified by
742 * its `cpu_idx` for first use, and sets the initial entrypoint state as
743 * specified by the entry_point_info structure.
744 ******************************************************************************/
745void cm_init_context_by_index(unsigned int cpu_idx,
746 const entry_point_info_t *ep)
747{
748 cpu_context_t *ctx;
749 ctx = cm_get_context_by_index(cpu_idx, GET_SECURITY_STATE(ep->h.attr));
Antonio Nino Diaz28dce9e2018-05-22 10:09:10 +0100750 cm_setup_context(ctx, ep);
Soby Mathewb0082d22015-04-09 13:40:55 +0100751}
752
753/*******************************************************************************
754 * The following function initializes the cpu_context for the current CPU
755 * for first use, and sets the initial entrypoint state as specified by the
756 * entry_point_info structure.
757 ******************************************************************************/
758void cm_init_my_context(const entry_point_info_t *ep)
759{
760 cpu_context_t *ctx;
761 ctx = cm_get_context(GET_SECURITY_STATE(ep->h.attr));
Antonio Nino Diaz28dce9e2018-05-22 10:09:10 +0100762 cm_setup_context(ctx, ep);
Soby Mathewb0082d22015-04-09 13:40:55 +0100763}
764
Boyan Karatotevfe1cd942023-03-08 17:04:00 +0000765/* EL2 present but unused, need to disable safely. SCTLR_EL2 can be ignored */
766static __unused void init_nonsecure_el2_unused(cpu_context_t *ctx)
767{
768 u_register_t hcr_el2 = HCR_RESET_VAL;
769 u_register_t mdcr_el2;
770 u_register_t scr_el3;
771
772 scr_el3 = read_ctx_reg(get_el3state_ctx(ctx), CTX_SCR_EL3);
773
774 /* Set EL2 register width: Set HCR_EL2.RW to match SCR_EL3.RW */
775 if ((scr_el3 & SCR_RW_BIT) != 0U) {
776 hcr_el2 |= HCR_RW_BIT;
777 }
778
779 write_hcr_el2(hcr_el2);
780
781 /*
782 * Initialise CPTR_EL2 setting all fields rather than relying on the hw.
783 * All fields have architecturally UNKNOWN reset values.
784 */
785 write_cptr_el2(CPTR_EL2_RESET_VAL);
786
787 /*
788 * Initialise CNTHCTL_EL2. All fields are architecturally UNKNOWN on
789 * reset and are set to zero except for field(s) listed below.
790 *
791 * CNTHCTL_EL2.EL1PTEN: Set to one to disable traps to Hyp mode of
792 * Non-secure EL0 and EL1 accesses to the physical timer registers.
793 *
794 * CNTHCTL_EL2.EL1PCTEN: Set to one to disable traps to Hyp mode of
795 * Non-secure EL0 and EL1 accesses to the physical counter registers.
796 */
797 write_cnthctl_el2(CNTHCTL_RESET_VAL | EL1PCEN_BIT | EL1PCTEN_BIT);
798
799 /*
800 * Initialise CNTVOFF_EL2 to zero as it resets to an architecturally
801 * UNKNOWN value.
802 */
803 write_cntvoff_el2(0);
804
805 /*
806 * Set VPIDR_EL2 and VMPIDR_EL2 to match MIDR_EL1 and MPIDR_EL1
807 * respectively.
808 */
809 write_vpidr_el2(read_midr_el1());
810 write_vmpidr_el2(read_mpidr_el1());
811
812 /*
813 * Initialise VTTBR_EL2. All fields are architecturally UNKNOWN on reset.
814 *
815 * VTTBR_EL2.VMID: Set to zero. Even though EL1&0 stage 2 address
816 * translation is disabled, cache maintenance operations depend on the
817 * VMID.
818 *
819 * VTTBR_EL2.BADDR: Set to zero as EL1&0 stage 2 address translation is
820 * disabled.
821 */
822 write_vttbr_el2(VTTBR_RESET_VAL &
823 ~((VTTBR_VMID_MASK << VTTBR_VMID_SHIFT) |
824 (VTTBR_BADDR_MASK << VTTBR_BADDR_SHIFT)));
825
826 /*
827 * Initialise MDCR_EL2, setting all fields rather than relying on hw.
828 * Some fields are architecturally UNKNOWN on reset.
829 *
830 * MDCR_EL2.TDRA: Set to zero so that Non-secure EL0 and EL1 System
831 * register accesses to the Debug ROM registers are not trapped to EL2.
832 *
833 * MDCR_EL2.TDOSA: Set to zero so that Non-secure EL1 System register
834 * accesses to the powerdown debug registers are not trapped to EL2.
835 *
836 * MDCR_EL2.TDA: Set to zero so that System register accesses to the
837 * debug registers do not trap to EL2.
838 *
839 * MDCR_EL2.TDE: Set to zero so that debug exceptions are not routed to
840 * EL2.
841 */
842 mdcr_el2 = MDCR_EL2_RESET_VAL &
843 ~(MDCR_EL2_TDRA_BIT | MDCR_EL2_TDOSA_BIT | MDCR_EL2_TDA_BIT |
844 MDCR_EL2_TDE_BIT);
845
846 write_mdcr_el2(mdcr_el2);
847
848 /*
849 * Initialise HSTR_EL2. All fields are architecturally UNKNOWN on reset.
850 *
851 * HSTR_EL2.T<n>: Set all these fields to zero so that Non-secure EL0 or
852 * EL1 accesses to System registers do not trap to EL2.
853 */
854 write_hstr_el2(HSTR_EL2_RESET_VAL & ~(HSTR_EL2_T_MASK));
855
856 /*
857 * Initialise CNTHP_CTL_EL2. All fields are architecturally UNKNOWN on
858 * reset.
859 *
860 * CNTHP_CTL_EL2:ENABLE: Set to zero to disable the EL2 physical timer
861 * and prevent timer interrupts.
862 */
863 write_cnthp_ctl_el2(CNTHP_CTL_RESET_VAL & ~(CNTHP_CTL_ENABLE_BIT));
864
865 manage_extensions_nonsecure_el2_unused();
866}
867
Soby Mathewb0082d22015-04-09 13:40:55 +0100868/*******************************************************************************
Zelalem Awekeb6301e62021-07-09 17:54:30 -0500869 * Prepare the CPU system registers for first entry into realm, secure, or
870 * normal world.
Andrew Thoelke4e126072014-06-04 21:10:52 +0100871 *
872 * If execution is requested to EL2 or hyp mode, SCTLR_EL2 is initialized
873 * If execution is requested to non-secure EL1 or svc mode, and the CPU supports
874 * EL2 then EL2 is disabled by configuring all necessary EL2 registers.
875 * For all entries, the EL1 registers are initialized from the cpu_context
876 ******************************************************************************/
877void cm_prepare_el3_exit(uint32_t security_state)
878{
Boyan Karatotevfe1cd942023-03-08 17:04:00 +0000879 u_register_t sctlr_elx, scr_el3;
Andrew Thoelke4e126072014-06-04 21:10:52 +0100880 cpu_context_t *ctx = cm_get_context(security_state);
881
Antonio Nino Diaz864ca6f2018-10-31 15:25:35 +0000882 assert(ctx != NULL);
Andrew Thoelke4e126072014-06-04 21:10:52 +0100883
884 if (security_state == NON_SECURE) {
Juan Pablo Conde72e0da12023-02-22 10:09:52 -0600885 uint64_t el2_implemented = el_implemented(2);
886
Louis Mayencourt1c819c32020-01-24 13:30:28 +0000887 scr_el3 = read_ctx_reg(get_el3state_ctx(ctx),
Antonio Nino Diaz864ca6f2018-10-31 15:25:35 +0000888 CTX_SCR_EL3);
Juan Pablo Conde72e0da12023-02-22 10:09:52 -0600889
890 if (((scr_el3 & SCR_HCE_BIT) != 0U)
891 || (el2_implemented != EL_IMPL_NONE)) {
892 /*
893 * If context is not being used for EL2, initialize
894 * HCRX_EL2 with its init value here.
895 */
896 if (is_feat_hcx_supported()) {
897 write_hcrx_el2(HCRX_EL2_INIT_VAL);
898 }
Juan Pablo Condef7252982023-07-10 16:00:41 -0500899
900 /*
901 * Initialize Fine-grained trap registers introduced
902 * by FEAT_FGT so all traps are initially disabled when
903 * switching to EL2 or a lower EL, preventing undesired
904 * behavior.
905 */
906 if (is_feat_fgt_supported()) {
907 /*
908 * Initialize HFG*_EL2 registers with a default
909 * value so legacy systems unaware of FEAT_FGT
910 * do not get trapped due to their lack of
911 * initialization for this feature.
912 */
913 write_hfgitr_el2(HFGITR_EL2_INIT_VAL);
914 write_hfgrtr_el2(HFGRTR_EL2_INIT_VAL);
915 write_hfgwtr_el2(HFGWTR_EL2_INIT_VAL);
916 }
Juan Pablo Conde72e0da12023-02-22 10:09:52 -0600917 }
918
Juan Pablo Condef7252982023-07-10 16:00:41 -0500919
Antonio Nino Diaz864ca6f2018-10-31 15:25:35 +0000920 if ((scr_el3 & SCR_HCE_BIT) != 0U) {
Andrew Thoelke4e126072014-06-04 21:10:52 +0100921 /* Use SCTLR_EL1.EE value to initialise sctlr_el2 */
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000922 sctlr_elx = read_ctx_reg(get_el1_sysregs_ctx(ctx),
Antonio Nino Diaz864ca6f2018-10-31 15:25:35 +0000923 CTX_SCTLR_EL1);
Ken Kuang00eac152017-08-23 16:03:29 +0800924 sctlr_elx &= SCTLR_EE_BIT;
Andrew Thoelke4e126072014-06-04 21:10:52 +0100925 sctlr_elx |= SCTLR_EL2_RES1;
Louis Mayencourt78a0aed2019-02-20 12:11:41 +0000926#if ERRATA_A75_764081
927 /*
928 * If workaround of errata 764081 for Cortex-A75 is used
929 * then set SCTLR_EL2.IESB to enable Implicit Error
930 * Synchronization Barrier.
931 */
932 sctlr_elx |= SCTLR_IESB_BIT;
933#endif
Andrew Thoelke4e126072014-06-04 21:10:52 +0100934 write_sctlr_el2(sctlr_elx);
Juan Pablo Conde72e0da12023-02-22 10:09:52 -0600935 } else if (el2_implemented != EL_IMPL_NONE) {
Boyan Karatotevfe1cd942023-03-08 17:04:00 +0000936 init_nonsecure_el2_unused(ctx);
Andrew Thoelke4e126072014-06-04 21:10:52 +0100937 }
938 }
939
Dimitris Papastamosa7921b92017-10-13 15:27:58 +0100940 cm_el1_sysregs_context_restore(security_state);
941 cm_set_next_eret_context(security_state);
Andrew Thoelke4e126072014-06-04 21:10:52 +0100942}
943
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000944#if CTX_INCLUDE_EL2_REGS
Andre Przywara5d6d2ab2022-11-10 14:40:37 +0000945
946static void el2_sysregs_context_save_fgt(el2_sysregs_t *ctx)
947{
Andre Przywara8258f142023-02-15 15:56:15 +0000948 write_ctx_reg(ctx, CTX_HDFGRTR_EL2, read_hdfgrtr_el2());
949 if (is_feat_amu_supported()) {
950 write_ctx_reg(ctx, CTX_HAFGRTR_EL2, read_hafgrtr_el2());
Andre Przywara5d6d2ab2022-11-10 14:40:37 +0000951 }
Andre Przywara8258f142023-02-15 15:56:15 +0000952 write_ctx_reg(ctx, CTX_HDFGWTR_EL2, read_hdfgwtr_el2());
953 write_ctx_reg(ctx, CTX_HFGITR_EL2, read_hfgitr_el2());
954 write_ctx_reg(ctx, CTX_HFGRTR_EL2, read_hfgrtr_el2());
955 write_ctx_reg(ctx, CTX_HFGWTR_EL2, read_hfgwtr_el2());
Andre Przywara5d6d2ab2022-11-10 14:40:37 +0000956}
957
958static void el2_sysregs_context_restore_fgt(el2_sysregs_t *ctx)
959{
Andre Przywara8258f142023-02-15 15:56:15 +0000960 write_hdfgrtr_el2(read_ctx_reg(ctx, CTX_HDFGRTR_EL2));
961 if (is_feat_amu_supported()) {
962 write_hafgrtr_el2(read_ctx_reg(ctx, CTX_HAFGRTR_EL2));
Andre Przywara5d6d2ab2022-11-10 14:40:37 +0000963 }
Andre Przywara8258f142023-02-15 15:56:15 +0000964 write_hdfgwtr_el2(read_ctx_reg(ctx, CTX_HDFGWTR_EL2));
965 write_hfgitr_el2(read_ctx_reg(ctx, CTX_HFGITR_EL2));
966 write_hfgrtr_el2(read_ctx_reg(ctx, CTX_HFGRTR_EL2));
967 write_hfgwtr_el2(read_ctx_reg(ctx, CTX_HFGWTR_EL2));
Andre Przywara5d6d2ab2022-11-10 14:40:37 +0000968}
969
Andre Przywara84b86532022-11-17 16:42:09 +0000970static void el2_sysregs_context_save_mpam(el2_sysregs_t *ctx)
971{
972 u_register_t mpam_idr = read_mpamidr_el1();
973
974 write_ctx_reg(ctx, CTX_MPAM2_EL2, read_mpam2_el2());
975
976 /*
977 * The context registers that we intend to save would be part of the
978 * PE's system register frame only if MPAMIDR_EL1.HAS_HCR == 1.
979 */
980 if ((mpam_idr & MPAMIDR_HAS_HCR_BIT) == 0U) {
981 return;
982 }
983
984 /*
985 * MPAMHCR_EL2, MPAMVPMV_EL2 and MPAMVPM0_EL2 are always present if
986 * MPAMIDR_HAS_HCR_BIT == 1.
987 */
988 write_ctx_reg(ctx, CTX_MPAMHCR_EL2, read_mpamhcr_el2());
989 write_ctx_reg(ctx, CTX_MPAMVPM0_EL2, read_mpamvpm0_el2());
990 write_ctx_reg(ctx, CTX_MPAMVPMV_EL2, read_mpamvpmv_el2());
991
992 /*
993 * The number of MPAMVPM registers is implementation defined, their
994 * number is stored in the MPAMIDR_EL1 register.
995 */
996 switch ((mpam_idr >> MPAMIDR_EL1_VPMR_MAX_SHIFT) & MPAMIDR_EL1_VPMR_MAX_MASK) {
997 case 7:
998 write_ctx_reg(ctx, CTX_MPAMVPM7_EL2, read_mpamvpm7_el2());
999 __fallthrough;
1000 case 6:
1001 write_ctx_reg(ctx, CTX_MPAMVPM6_EL2, read_mpamvpm6_el2());
1002 __fallthrough;
1003 case 5:
1004 write_ctx_reg(ctx, CTX_MPAMVPM5_EL2, read_mpamvpm5_el2());
1005 __fallthrough;
1006 case 4:
1007 write_ctx_reg(ctx, CTX_MPAMVPM4_EL2, read_mpamvpm4_el2());
1008 __fallthrough;
1009 case 3:
1010 write_ctx_reg(ctx, CTX_MPAMVPM3_EL2, read_mpamvpm3_el2());
1011 __fallthrough;
1012 case 2:
1013 write_ctx_reg(ctx, CTX_MPAMVPM2_EL2, read_mpamvpm2_el2());
1014 __fallthrough;
1015 case 1:
1016 write_ctx_reg(ctx, CTX_MPAMVPM1_EL2, read_mpamvpm1_el2());
1017 break;
1018 }
1019}
1020
1021static void el2_sysregs_context_restore_mpam(el2_sysregs_t *ctx)
1022{
1023 u_register_t mpam_idr = read_mpamidr_el1();
1024
1025 write_mpam2_el2(read_ctx_reg(ctx, CTX_MPAM2_EL2));
1026
1027 if ((mpam_idr & MPAMIDR_HAS_HCR_BIT) == 0U) {
1028 return;
1029 }
1030
1031 write_mpamhcr_el2(read_ctx_reg(ctx, CTX_MPAMHCR_EL2));
1032 write_mpamvpm0_el2(read_ctx_reg(ctx, CTX_MPAMVPM0_EL2));
1033 write_mpamvpmv_el2(read_ctx_reg(ctx, CTX_MPAMVPMV_EL2));
1034
1035 switch ((mpam_idr >> MPAMIDR_EL1_VPMR_MAX_SHIFT) & MPAMIDR_EL1_VPMR_MAX_MASK) {
1036 case 7:
1037 write_mpamvpm7_el2(read_ctx_reg(ctx, CTX_MPAMVPM7_EL2));
1038 __fallthrough;
1039 case 6:
1040 write_mpamvpm6_el2(read_ctx_reg(ctx, CTX_MPAMVPM6_EL2));
1041 __fallthrough;
1042 case 5:
1043 write_mpamvpm5_el2(read_ctx_reg(ctx, CTX_MPAMVPM5_EL2));
1044 __fallthrough;
1045 case 4:
1046 write_mpamvpm4_el2(read_ctx_reg(ctx, CTX_MPAMVPM4_EL2));
1047 __fallthrough;
1048 case 3:
1049 write_mpamvpm3_el2(read_ctx_reg(ctx, CTX_MPAMVPM3_EL2));
1050 __fallthrough;
1051 case 2:
1052 write_mpamvpm2_el2(read_ctx_reg(ctx, CTX_MPAMVPM2_EL2));
1053 __fallthrough;
1054 case 1:
1055 write_mpamvpm1_el2(read_ctx_reg(ctx, CTX_MPAMVPM1_EL2));
1056 break;
1057 }
1058}
1059
Boyan Karatoteva6989892023-05-15 15:09:16 +01001060/* -----------------------------------------------------
1061 * The following registers are not added:
1062 * AMEVCNTVOFF0<n>_EL2
1063 * AMEVCNTVOFF1<n>_EL2
1064 * ICH_AP0R<n>_EL2
1065 * ICH_AP1R<n>_EL2
1066 * ICH_LR<n>_EL2
1067 * -----------------------------------------------------
1068 */
1069static void el2_sysregs_context_save_common(el2_sysregs_t *ctx)
1070{
1071 write_ctx_reg(ctx, CTX_ACTLR_EL2, read_actlr_el2());
1072 write_ctx_reg(ctx, CTX_AFSR0_EL2, read_afsr0_el2());
1073 write_ctx_reg(ctx, CTX_AFSR1_EL2, read_afsr1_el2());
1074 write_ctx_reg(ctx, CTX_AMAIR_EL2, read_amair_el2());
1075 write_ctx_reg(ctx, CTX_CNTHCTL_EL2, read_cnthctl_el2());
1076 write_ctx_reg(ctx, CTX_CNTVOFF_EL2, read_cntvoff_el2());
1077 write_ctx_reg(ctx, CTX_CPTR_EL2, read_cptr_el2());
1078 if (CTX_INCLUDE_AARCH32_REGS) {
1079 write_ctx_reg(ctx, CTX_DBGVCR32_EL2, read_dbgvcr32_el2());
1080 }
1081 write_ctx_reg(ctx, CTX_ELR_EL2, read_elr_el2());
1082 write_ctx_reg(ctx, CTX_ESR_EL2, read_esr_el2());
1083 write_ctx_reg(ctx, CTX_FAR_EL2, read_far_el2());
1084 write_ctx_reg(ctx, CTX_HACR_EL2, read_hacr_el2());
1085 write_ctx_reg(ctx, CTX_HCR_EL2, read_hcr_el2());
1086 write_ctx_reg(ctx, CTX_HPFAR_EL2, read_hpfar_el2());
1087 write_ctx_reg(ctx, CTX_HSTR_EL2, read_hstr_el2());
Boyan Karatotev2e9e6f02023-05-22 15:53:58 +01001088
1089 /*
1090 * Set the NS bit to be able to access the ICC_SRE_EL2 register
1091 * TODO: remove with root context
1092 */
1093 u_register_t scr_el3 = read_scr_el3();
1094
1095 write_scr_el3(scr_el3 | SCR_NS_BIT);
1096 isb();
Boyan Karatoteva6989892023-05-15 15:09:16 +01001097 write_ctx_reg(ctx, CTX_ICC_SRE_EL2, read_icc_sre_el2());
Boyan Karatotev2e9e6f02023-05-22 15:53:58 +01001098
1099 write_scr_el3(scr_el3);
1100 isb();
1101
Boyan Karatoteva6989892023-05-15 15:09:16 +01001102 write_ctx_reg(ctx, CTX_ICH_HCR_EL2, read_ich_hcr_el2());
1103 write_ctx_reg(ctx, CTX_ICH_VMCR_EL2, read_ich_vmcr_el2());
1104 write_ctx_reg(ctx, CTX_MAIR_EL2, read_mair_el2());
1105 write_ctx_reg(ctx, CTX_MDCR_EL2, read_mdcr_el2());
1106 write_ctx_reg(ctx, CTX_SCTLR_EL2, read_sctlr_el2());
1107 write_ctx_reg(ctx, CTX_SPSR_EL2, read_spsr_el2());
1108 write_ctx_reg(ctx, CTX_SP_EL2, read_sp_el2());
1109 write_ctx_reg(ctx, CTX_TCR_EL2, read_tcr_el2());
1110 write_ctx_reg(ctx, CTX_TPIDR_EL2, read_tpidr_el2());
1111 write_ctx_reg(ctx, CTX_TTBR0_EL2, read_ttbr0_el2());
1112 write_ctx_reg(ctx, CTX_VBAR_EL2, read_vbar_el2());
1113 write_ctx_reg(ctx, CTX_VMPIDR_EL2, read_vmpidr_el2());
1114 write_ctx_reg(ctx, CTX_VPIDR_EL2, read_vpidr_el2());
1115 write_ctx_reg(ctx, CTX_VTCR_EL2, read_vtcr_el2());
1116 write_ctx_reg(ctx, CTX_VTTBR_EL2, read_vttbr_el2());
1117}
1118
1119static void el2_sysregs_context_restore_common(el2_sysregs_t *ctx)
1120{
1121 write_actlr_el2(read_ctx_reg(ctx, CTX_ACTLR_EL2));
1122 write_afsr0_el2(read_ctx_reg(ctx, CTX_AFSR0_EL2));
1123 write_afsr1_el2(read_ctx_reg(ctx, CTX_AFSR1_EL2));
1124 write_amair_el2(read_ctx_reg(ctx, CTX_AMAIR_EL2));
1125 write_cnthctl_el2(read_ctx_reg(ctx, CTX_CNTHCTL_EL2));
1126 write_cntvoff_el2(read_ctx_reg(ctx, CTX_CNTVOFF_EL2));
1127 write_cptr_el2(read_ctx_reg(ctx, CTX_CPTR_EL2));
1128 if (CTX_INCLUDE_AARCH32_REGS) {
1129 write_dbgvcr32_el2(read_ctx_reg(ctx, CTX_DBGVCR32_EL2));
1130 }
1131 write_elr_el2(read_ctx_reg(ctx, CTX_ELR_EL2));
1132 write_esr_el2(read_ctx_reg(ctx, CTX_ESR_EL2));
1133 write_far_el2(read_ctx_reg(ctx, CTX_FAR_EL2));
1134 write_hacr_el2(read_ctx_reg(ctx, CTX_HACR_EL2));
1135 write_hcr_el2(read_ctx_reg(ctx, CTX_HCR_EL2));
1136 write_hpfar_el2(read_ctx_reg(ctx, CTX_HPFAR_EL2));
1137 write_hstr_el2(read_ctx_reg(ctx, CTX_HSTR_EL2));
Boyan Karatotev2e9e6f02023-05-22 15:53:58 +01001138
1139 /*
1140 * Set the NS bit to be able to access the ICC_SRE_EL2 register
1141 * TODO: remove with root context
1142 */
1143 u_register_t scr_el3 = read_scr_el3();
1144
1145 write_scr_el3(scr_el3 | SCR_NS_BIT);
1146 isb();
Boyan Karatoteva6989892023-05-15 15:09:16 +01001147 write_icc_sre_el2(read_ctx_reg(ctx, CTX_ICC_SRE_EL2));
Boyan Karatotev2e9e6f02023-05-22 15:53:58 +01001148
1149 write_scr_el3(scr_el3);
1150 isb();
1151
Boyan Karatoteva6989892023-05-15 15:09:16 +01001152 write_ich_hcr_el2(read_ctx_reg(ctx, CTX_ICH_HCR_EL2));
1153 write_ich_vmcr_el2(read_ctx_reg(ctx, CTX_ICH_VMCR_EL2));
1154 write_mair_el2(read_ctx_reg(ctx, CTX_MAIR_EL2));
1155 write_mdcr_el2(read_ctx_reg(ctx, CTX_MDCR_EL2));
1156 write_sctlr_el2(read_ctx_reg(ctx, CTX_SCTLR_EL2));
1157 write_spsr_el2(read_ctx_reg(ctx, CTX_SPSR_EL2));
1158 write_sp_el2(read_ctx_reg(ctx, CTX_SP_EL2));
1159 write_tcr_el2(read_ctx_reg(ctx, CTX_TCR_EL2));
1160 write_tpidr_el2(read_ctx_reg(ctx, CTX_TPIDR_EL2));
1161 write_ttbr0_el2(read_ctx_reg(ctx, CTX_TTBR0_EL2));
1162 write_vbar_el2(read_ctx_reg(ctx, CTX_VBAR_EL2));
1163 write_vmpidr_el2(read_ctx_reg(ctx, CTX_VMPIDR_EL2));
1164 write_vpidr_el2(read_ctx_reg(ctx, CTX_VPIDR_EL2));
1165 write_vtcr_el2(read_ctx_reg(ctx, CTX_VTCR_EL2));
1166 write_vttbr_el2(read_ctx_reg(ctx, CTX_VTTBR_EL2));
1167}
1168
Max Shvetsovbdf502d2020-02-25 13:56:19 +00001169/*******************************************************************************
1170 * Save EL2 sysreg context
1171 ******************************************************************************/
1172void cm_el2_sysregs_context_save(uint32_t security_state)
1173{
Boyan Karatotev2e9e6f02023-05-22 15:53:58 +01001174 cpu_context_t *ctx;
1175 el2_sysregs_t *el2_sysregs_ctx;
Max Shvetsovbdf502d2020-02-25 13:56:19 +00001176
Boyan Karatotev2e9e6f02023-05-22 15:53:58 +01001177 ctx = cm_get_context(security_state);
1178 assert(ctx != NULL);
Max Shvetsovbdf502d2020-02-25 13:56:19 +00001179
Boyan Karatotev2e9e6f02023-05-22 15:53:58 +01001180 el2_sysregs_ctx = get_el2_sysregs_ctx(ctx);
Zelalem Aweke5362beb2022-04-04 17:42:48 -05001181
Boyan Karatotev2e9e6f02023-05-22 15:53:58 +01001182 el2_sysregs_context_save_common(el2_sysregs_ctx);
Zelalem Aweke5362beb2022-04-04 17:42:48 -05001183#if CTX_INCLUDE_MTE_REGS
Boyan Karatotev2e9e6f02023-05-22 15:53:58 +01001184 write_ctx_reg(el2_sysregs_ctx, CTX_TFSR_EL2, read_tfsr_el2());
Zelalem Aweke5362beb2022-04-04 17:42:48 -05001185#endif
Boyan Karatotev2e9e6f02023-05-22 15:53:58 +01001186 if (is_feat_mpam_supported()) {
1187 el2_sysregs_context_save_mpam(el2_sysregs_ctx);
1188 }
Andre Przywara5d6d2ab2022-11-10 14:40:37 +00001189
Boyan Karatotev2e9e6f02023-05-22 15:53:58 +01001190 if (is_feat_fgt_supported()) {
1191 el2_sysregs_context_save_fgt(el2_sysregs_ctx);
1192 }
Andre Przywara5d6d2ab2022-11-10 14:40:37 +00001193
Boyan Karatotev2e9e6f02023-05-22 15:53:58 +01001194 if (is_feat_ecv_v2_supported()) {
1195 write_ctx_reg(el2_sysregs_ctx, CTX_CNTPOFF_EL2, read_cntpoff_el2());
1196 }
Andre Przywarac3464182022-11-17 17:30:43 +00001197
Boyan Karatotev2e9e6f02023-05-22 15:53:58 +01001198 if (is_feat_vhe_supported()) {
1199 write_ctx_reg(el2_sysregs_ctx, CTX_CONTEXTIDR_EL2, read_contextidr_el2());
1200 write_ctx_reg(el2_sysregs_ctx, CTX_TTBR1_EL2, read_ttbr1_el2());
1201 }
Andre Przywara870627e2023-01-27 12:25:49 +00001202
Boyan Karatotev2e9e6f02023-05-22 15:53:58 +01001203 if (is_feat_ras_supported()) {
1204 write_ctx_reg(el2_sysregs_ctx, CTX_VDISR_EL2, read_vdisr_el2());
1205 write_ctx_reg(el2_sysregs_ctx, CTX_VSESR_EL2, read_vsesr_el2());
1206 }
Andre Przywaraedc449d2023-01-27 14:09:20 +00001207
Boyan Karatotev2e9e6f02023-05-22 15:53:58 +01001208 if (is_feat_nv2_supported()) {
1209 write_ctx_reg(el2_sysregs_ctx, CTX_VNCR_EL2, read_vncr_el2());
1210 }
Andre Przywaraedc449d2023-01-27 14:09:20 +00001211
Boyan Karatotev2e9e6f02023-05-22 15:53:58 +01001212 if (is_feat_trf_supported()) {
1213 write_ctx_reg(el2_sysregs_ctx, CTX_TRFCR_EL2, read_trfcr_el2());
1214 }
Andre Przywara902c9022022-11-17 17:30:43 +00001215
Boyan Karatotev2e9e6f02023-05-22 15:53:58 +01001216 if (is_feat_csv2_2_supported()) {
1217 write_ctx_reg(el2_sysregs_ctx, CTX_SCXTNUM_EL2, read_scxtnum_el2());
1218 }
Andre Przywara902c9022022-11-17 17:30:43 +00001219
Boyan Karatotev2e9e6f02023-05-22 15:53:58 +01001220 if (is_feat_hcx_supported()) {
1221 write_ctx_reg(el2_sysregs_ctx, CTX_HCRX_EL2, read_hcrx_el2());
1222 }
1223 if (is_feat_tcr2_supported()) {
1224 write_ctx_reg(el2_sysregs_ctx, CTX_TCR2_EL2, read_tcr2_el2());
1225 }
1226 if (is_feat_sxpie_supported()) {
1227 write_ctx_reg(el2_sysregs_ctx, CTX_PIRE0_EL2, read_pire0_el2());
1228 write_ctx_reg(el2_sysregs_ctx, CTX_PIR_EL2, read_pir_el2());
1229 }
1230 if (is_feat_s2pie_supported()) {
1231 write_ctx_reg(el2_sysregs_ctx, CTX_S2PIR_EL2, read_s2pir_el2());
1232 }
1233 if (is_feat_sxpoe_supported()) {
1234 write_ctx_reg(el2_sysregs_ctx, CTX_POR_EL2, read_por_el2());
1235 }
1236 if (is_feat_gcs_supported()) {
1237 write_ctx_reg(el2_sysregs_ctx, CTX_GCSPR_EL2, read_gcspr_el2());
1238 write_ctx_reg(el2_sysregs_ctx, CTX_GCSCR_EL2, read_gcscr_el2());
Max Shvetsovbdf502d2020-02-25 13:56:19 +00001239 }
1240}
1241
1242/*******************************************************************************
1243 * Restore EL2 sysreg context
1244 ******************************************************************************/
1245void cm_el2_sysregs_context_restore(uint32_t security_state)
1246{
Boyan Karatotev2e9e6f02023-05-22 15:53:58 +01001247 cpu_context_t *ctx;
1248 el2_sysregs_t *el2_sysregs_ctx;
Max Shvetsovbdf502d2020-02-25 13:56:19 +00001249
Boyan Karatotev2e9e6f02023-05-22 15:53:58 +01001250 ctx = cm_get_context(security_state);
1251 assert(ctx != NULL);
Max Shvetsovbdf502d2020-02-25 13:56:19 +00001252
Boyan Karatotev2e9e6f02023-05-22 15:53:58 +01001253 el2_sysregs_ctx = get_el2_sysregs_ctx(ctx);
Zelalem Aweke5362beb2022-04-04 17:42:48 -05001254
Boyan Karatotev2e9e6f02023-05-22 15:53:58 +01001255 el2_sysregs_context_restore_common(el2_sysregs_ctx);
Zelalem Aweke5362beb2022-04-04 17:42:48 -05001256#if CTX_INCLUDE_MTE_REGS
Boyan Karatotev2e9e6f02023-05-22 15:53:58 +01001257 write_tfsr_el2(read_ctx_reg(el2_sysregs_ctx, CTX_TFSR_EL2));
Zelalem Aweke5362beb2022-04-04 17:42:48 -05001258#endif
Boyan Karatotev2e9e6f02023-05-22 15:53:58 +01001259 if (is_feat_mpam_supported()) {
1260 el2_sysregs_context_restore_mpam(el2_sysregs_ctx);
1261 }
Andre Przywara5d6d2ab2022-11-10 14:40:37 +00001262
Boyan Karatotev2e9e6f02023-05-22 15:53:58 +01001263 if (is_feat_fgt_supported()) {
1264 el2_sysregs_context_restore_fgt(el2_sysregs_ctx);
1265 }
Andre Przywara5d6d2ab2022-11-10 14:40:37 +00001266
Boyan Karatotev2e9e6f02023-05-22 15:53:58 +01001267 if (is_feat_ecv_v2_supported()) {
1268 write_cntpoff_el2(read_ctx_reg(el2_sysregs_ctx, CTX_CNTPOFF_EL2));
1269 }
Andre Przywarac3464182022-11-17 17:30:43 +00001270
Boyan Karatotev2e9e6f02023-05-22 15:53:58 +01001271 if (is_feat_vhe_supported()) {
1272 write_contextidr_el2(read_ctx_reg(el2_sysregs_ctx, CTX_CONTEXTIDR_EL2));
1273 write_ttbr1_el2(read_ctx_reg(el2_sysregs_ctx, CTX_TTBR1_EL2));
1274 }
Andre Przywara870627e2023-01-27 12:25:49 +00001275
Boyan Karatotev2e9e6f02023-05-22 15:53:58 +01001276 if (is_feat_ras_supported()) {
1277 write_vdisr_el2(read_ctx_reg(el2_sysregs_ctx, CTX_VDISR_EL2));
1278 write_vsesr_el2(read_ctx_reg(el2_sysregs_ctx, CTX_VSESR_EL2));
1279 }
Andre Przywaraedc449d2023-01-27 14:09:20 +00001280
Boyan Karatotev2e9e6f02023-05-22 15:53:58 +01001281 if (is_feat_nv2_supported()) {
1282 write_vncr_el2(read_ctx_reg(el2_sysregs_ctx, CTX_VNCR_EL2));
1283 }
1284 if (is_feat_trf_supported()) {
1285 write_trfcr_el2(read_ctx_reg(el2_sysregs_ctx, CTX_TRFCR_EL2));
1286 }
Andre Przywara902c9022022-11-17 17:30:43 +00001287
Boyan Karatotev2e9e6f02023-05-22 15:53:58 +01001288 if (is_feat_csv2_2_supported()) {
1289 write_scxtnum_el2(read_ctx_reg(el2_sysregs_ctx, CTX_SCXTNUM_EL2));
1290 }
Andre Przywara902c9022022-11-17 17:30:43 +00001291
Boyan Karatotev2e9e6f02023-05-22 15:53:58 +01001292 if (is_feat_hcx_supported()) {
1293 write_hcrx_el2(read_ctx_reg(el2_sysregs_ctx, CTX_HCRX_EL2));
1294 }
1295 if (is_feat_tcr2_supported()) {
1296 write_tcr2_el2(read_ctx_reg(el2_sysregs_ctx, CTX_TCR2_EL2));
1297 }
1298 if (is_feat_sxpie_supported()) {
1299 write_pire0_el2(read_ctx_reg(el2_sysregs_ctx, CTX_PIRE0_EL2));
1300 write_pir_el2(read_ctx_reg(el2_sysregs_ctx, CTX_PIR_EL2));
1301 }
1302 if (is_feat_s2pie_supported()) {
1303 write_s2pir_el2(read_ctx_reg(el2_sysregs_ctx, CTX_S2PIR_EL2));
1304 }
1305 if (is_feat_sxpoe_supported()) {
1306 write_por_el2(read_ctx_reg(el2_sysregs_ctx, CTX_POR_EL2));
1307 }
1308 if (is_feat_gcs_supported()) {
1309 write_gcscr_el2(read_ctx_reg(el2_sysregs_ctx, CTX_GCSCR_EL2));
1310 write_gcspr_el2(read_ctx_reg(el2_sysregs_ctx, CTX_GCSPR_EL2));
Max Shvetsovbdf502d2020-02-25 13:56:19 +00001311 }
1312}
1313#endif /* CTX_INCLUDE_EL2_REGS */
1314
Andrew Thoelke4e126072014-06-04 21:10:52 +01001315/*******************************************************************************
Zelalem Awekef92c0cb2022-01-31 16:59:42 -06001316 * This function is used to exit to Non-secure world. If CTX_INCLUDE_EL2_REGS
1317 * is enabled, it restores EL1 and EL2 sysreg contexts instead of directly
1318 * updating EL1 and EL2 registers. Otherwise, it calls the generic
1319 * cm_prepare_el3_exit function.
1320 ******************************************************************************/
1321void cm_prepare_el3_exit_ns(void)
1322{
1323#if CTX_INCLUDE_EL2_REGS
Boyan Karatotev1e966f32023-03-27 17:02:43 +01001324#if ENABLE_ASSERTIONS
Zelalem Awekef92c0cb2022-01-31 16:59:42 -06001325 cpu_context_t *ctx = cm_get_context(NON_SECURE);
1326 assert(ctx != NULL);
1327
Zelalem Aweke20126002022-04-08 16:48:05 -05001328 /* Assert that EL2 is used. */
Boyan Karatotev1e966f32023-03-27 17:02:43 +01001329 u_register_t scr_el3 = read_ctx_reg(get_el3state_ctx(ctx), CTX_SCR_EL3);
Zelalem Aweke20126002022-04-08 16:48:05 -05001330 assert(((scr_el3 & SCR_HCE_BIT) != 0UL) &&
1331 (el_implemented(2U) != EL_IMPL_NONE));
Boyan Karatotev1e966f32023-03-27 17:02:43 +01001332#endif /* ENABLE_ASSERTIONS */
Zelalem Awekef92c0cb2022-01-31 16:59:42 -06001333
Zelalem Awekef92c0cb2022-01-31 16:59:42 -06001334 /* Restore EL2 and EL1 sysreg contexts */
1335 cm_el2_sysregs_context_restore(NON_SECURE);
1336 cm_el1_sysregs_context_restore(NON_SECURE);
1337 cm_set_next_eret_context(NON_SECURE);
1338#else
1339 cm_prepare_el3_exit(NON_SECURE);
1340#endif /* CTX_INCLUDE_EL2_REGS */
1341}
1342
1343/*******************************************************************************
Soby Mathew2ed46e92014-07-04 16:02:26 +01001344 * The next four functions are used by runtime services to save and restore
1345 * EL1 context on the 'cpu_context' structure for the specified security
Achin Gupta7aea9082014-02-01 07:51:28 +00001346 * state.
1347 ******************************************************************************/
Achin Gupta7aea9082014-02-01 07:51:28 +00001348void cm_el1_sysregs_context_save(uint32_t security_state)
1349{
Dan Handleye2712bc2014-04-10 15:37:22 +01001350 cpu_context_t *ctx;
Achin Gupta7aea9082014-02-01 07:51:28 +00001351
Andrew Thoelkea2f65532014-05-14 17:09:32 +01001352 ctx = cm_get_context(security_state);
Antonio Nino Diaz864ca6f2018-10-31 15:25:35 +00001353 assert(ctx != NULL);
Achin Gupta7aea9082014-02-01 07:51:28 +00001354
Max Shvetsovc9e2c922020-02-17 16:15:47 +00001355 el1_sysregs_context_save(get_el1_sysregs_ctx(ctx));
Dimitris Papastamosa7921b92017-10-13 15:27:58 +01001356
1357#if IMAGE_BL31
1358 if (security_state == SECURE)
1359 PUBLISH_EVENT(cm_exited_secure_world);
1360 else
1361 PUBLISH_EVENT(cm_exited_normal_world);
1362#endif
Achin Gupta7aea9082014-02-01 07:51:28 +00001363}
1364
1365void cm_el1_sysregs_context_restore(uint32_t security_state)
1366{
Dan Handleye2712bc2014-04-10 15:37:22 +01001367 cpu_context_t *ctx;
Achin Gupta7aea9082014-02-01 07:51:28 +00001368
Andrew Thoelkea2f65532014-05-14 17:09:32 +01001369 ctx = cm_get_context(security_state);
Antonio Nino Diaz864ca6f2018-10-31 15:25:35 +00001370 assert(ctx != NULL);
Achin Gupta7aea9082014-02-01 07:51:28 +00001371
Max Shvetsovc9e2c922020-02-17 16:15:47 +00001372 el1_sysregs_context_restore(get_el1_sysregs_ctx(ctx));
Dimitris Papastamosa7921b92017-10-13 15:27:58 +01001373
1374#if IMAGE_BL31
1375 if (security_state == SECURE)
1376 PUBLISH_EVENT(cm_entering_secure_world);
1377 else
1378 PUBLISH_EVENT(cm_entering_normal_world);
1379#endif
Achin Gupta7aea9082014-02-01 07:51:28 +00001380}
1381
1382/*******************************************************************************
Andrew Thoelke4e126072014-06-04 21:10:52 +01001383 * This function populates ELR_EL3 member of 'cpu_context' pertaining to the
1384 * given security state with the given entrypoint
Jeenu Viswambharancaa84932014-02-06 10:36:15 +00001385 ******************************************************************************/
Soby Mathewa0fedc42016-06-16 14:52:04 +01001386void cm_set_elr_el3(uint32_t security_state, uintptr_t entrypoint)
Jeenu Viswambharancaa84932014-02-06 10:36:15 +00001387{
Dan Handleye2712bc2014-04-10 15:37:22 +01001388 cpu_context_t *ctx;
1389 el3_state_t *state;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +00001390
Andrew Thoelkea2f65532014-05-14 17:09:32 +01001391 ctx = cm_get_context(security_state);
Antonio Nino Diaz864ca6f2018-10-31 15:25:35 +00001392 assert(ctx != NULL);
Jeenu Viswambharancaa84932014-02-06 10:36:15 +00001393
Andrew Thoelke4e126072014-06-04 21:10:52 +01001394 /* Populate EL3 state so that ERET jumps to the correct entry */
Jeenu Viswambharancaa84932014-02-06 10:36:15 +00001395 state = get_el3state_ctx(ctx);
Jeenu Viswambharancaa84932014-02-06 10:36:15 +00001396 write_ctx_reg(state, CTX_ELR_EL3, entrypoint);
Jeenu Viswambharancaa84932014-02-06 10:36:15 +00001397}
1398
1399/*******************************************************************************
Andrew Thoelke4e126072014-06-04 21:10:52 +01001400 * This function populates ELR_EL3 and SPSR_EL3 members of 'cpu_context'
1401 * pertaining to the given security state
Achin Gupta607084e2014-02-09 18:24:19 +00001402 ******************************************************************************/
Andrew Thoelke4e126072014-06-04 21:10:52 +01001403void cm_set_elr_spsr_el3(uint32_t security_state,
Soby Mathewa0fedc42016-06-16 14:52:04 +01001404 uintptr_t entrypoint, uint32_t spsr)
Achin Gupta607084e2014-02-09 18:24:19 +00001405{
Dan Handleye2712bc2014-04-10 15:37:22 +01001406 cpu_context_t *ctx;
1407 el3_state_t *state;
Achin Gupta607084e2014-02-09 18:24:19 +00001408
Andrew Thoelkea2f65532014-05-14 17:09:32 +01001409 ctx = cm_get_context(security_state);
Antonio Nino Diaz864ca6f2018-10-31 15:25:35 +00001410 assert(ctx != NULL);
Achin Gupta607084e2014-02-09 18:24:19 +00001411
1412 /* Populate EL3 state so that ERET jumps to the correct entry */
1413 state = get_el3state_ctx(ctx);
1414 write_ctx_reg(state, CTX_ELR_EL3, entrypoint);
Andrew Thoelke4e126072014-06-04 21:10:52 +01001415 write_ctx_reg(state, CTX_SPSR_EL3, spsr);
Achin Gupta607084e2014-02-09 18:24:19 +00001416}
1417
1418/*******************************************************************************
Achin Gupta27b895e2014-05-04 18:38:28 +01001419 * This function updates a single bit in the SCR_EL3 member of the 'cpu_context'
1420 * pertaining to the given security state using the value and bit position
1421 * specified in the parameters. It preserves all other bits.
1422 ******************************************************************************/
1423void cm_write_scr_el3_bit(uint32_t security_state,
1424 uint32_t bit_pos,
1425 uint32_t value)
1426{
1427 cpu_context_t *ctx;
1428 el3_state_t *state;
Louis Mayencourt1c819c32020-01-24 13:30:28 +00001429 u_register_t scr_el3;
Achin Gupta27b895e2014-05-04 18:38:28 +01001430
Andrew Thoelkea2f65532014-05-14 17:09:32 +01001431 ctx = cm_get_context(security_state);
Antonio Nino Diaz864ca6f2018-10-31 15:25:35 +00001432 assert(ctx != NULL);
Achin Gupta27b895e2014-05-04 18:38:28 +01001433
1434 /* Ensure that the bit position is a valid one */
Jimmy Brissoned202072020-08-04 16:18:52 -05001435 assert(((1UL << bit_pos) & SCR_VALID_BIT_MASK) != 0U);
Achin Gupta27b895e2014-05-04 18:38:28 +01001436
1437 /* Ensure that the 'value' is only a bit wide */
Antonio Nino Diaz864ca6f2018-10-31 15:25:35 +00001438 assert(value <= 1U);
Achin Gupta27b895e2014-05-04 18:38:28 +01001439
1440 /*
1441 * Get the SCR_EL3 value from the cpu context, clear the desired bit
1442 * and set it to its new value.
1443 */
1444 state = get_el3state_ctx(ctx);
Louis Mayencourt1c819c32020-01-24 13:30:28 +00001445 scr_el3 = read_ctx_reg(state, CTX_SCR_EL3);
Jimmy Brissoned202072020-08-04 16:18:52 -05001446 scr_el3 &= ~(1UL << bit_pos);
Louis Mayencourt1c819c32020-01-24 13:30:28 +00001447 scr_el3 |= (u_register_t)value << bit_pos;
Achin Gupta27b895e2014-05-04 18:38:28 +01001448 write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
1449}
1450
1451/*******************************************************************************
1452 * This function retrieves SCR_EL3 member of 'cpu_context' pertaining to the
1453 * given security state.
1454 ******************************************************************************/
Louis Mayencourt1c819c32020-01-24 13:30:28 +00001455u_register_t cm_get_scr_el3(uint32_t security_state)
Achin Gupta27b895e2014-05-04 18:38:28 +01001456{
1457 cpu_context_t *ctx;
1458 el3_state_t *state;
1459
Andrew Thoelkea2f65532014-05-14 17:09:32 +01001460 ctx = cm_get_context(security_state);
Antonio Nino Diaz864ca6f2018-10-31 15:25:35 +00001461 assert(ctx != NULL);
Achin Gupta27b895e2014-05-04 18:38:28 +01001462
1463 /* Populate EL3 state so that ERET jumps to the correct entry */
1464 state = get_el3state_ctx(ctx);
Louis Mayencourt1c819c32020-01-24 13:30:28 +00001465 return read_ctx_reg(state, CTX_SCR_EL3);
Achin Gupta27b895e2014-05-04 18:38:28 +01001466}
1467
1468/*******************************************************************************
Jeenu Viswambharancaa84932014-02-06 10:36:15 +00001469 * This function is used to program the context that's used for exception
1470 * return. This initializes the SP_EL3 to a pointer to a 'cpu_context' set for
1471 * the required security state
Achin Gupta7aea9082014-02-01 07:51:28 +00001472 ******************************************************************************/
1473void cm_set_next_eret_context(uint32_t security_state)
1474{
Dan Handleye2712bc2014-04-10 15:37:22 +01001475 cpu_context_t *ctx;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +00001476
Andrew Thoelkea2f65532014-05-14 17:09:32 +01001477 ctx = cm_get_context(security_state);
Antonio Nino Diaz864ca6f2018-10-31 15:25:35 +00001478 assert(ctx != NULL);
Achin Gupta7aea9082014-02-01 07:51:28 +00001479
Andrew Thoelke4e126072014-06-04 21:10:52 +01001480 cm_set_next_context(ctx);
Achin Gupta7aea9082014-02-01 07:51:28 +00001481}