context_mgmt.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023
  1. /*
  2. * Copyright (c) 2013-2024, Arm Limited and Contributors. All rights reserved.
  3. * Copyright (c) 2022, NVIDIA Corporation. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. #include <assert.h>
  8. #include <stdbool.h>
  9. #include <string.h>
  10. #include <platform_def.h>
  11. #include <arch.h>
  12. #include <arch_helpers.h>
  13. #include <arch_features.h>
  14. #include <bl31/interrupt_mgmt.h>
  15. #include <common/bl_common.h>
  16. #include <common/debug.h>
  17. #include <context.h>
  18. #include <drivers/arm/gicv3.h>
  19. #include <lib/cpus/cpu_ops.h>
  20. #include <lib/cpus/errata.h>
  21. #include <lib/el3_runtime/context_mgmt.h>
  22. #include <lib/el3_runtime/cpu_data.h>
  23. #include <lib/el3_runtime/pubsub_events.h>
  24. #include <lib/extensions/amu.h>
  25. #include <lib/extensions/brbe.h>
  26. #include <lib/extensions/debug_v8p9.h>
  27. #include <lib/extensions/fgt2.h>
  28. #include <lib/extensions/mpam.h>
  29. #include <lib/extensions/pmuv3.h>
  30. #include <lib/extensions/sme.h>
  31. #include <lib/extensions/spe.h>
  32. #include <lib/extensions/sve.h>
  33. #include <lib/extensions/sysreg128.h>
  34. #include <lib/extensions/sys_reg_trace.h>
  35. #include <lib/extensions/tcr2.h>
  36. #include <lib/extensions/trbe.h>
  37. #include <lib/extensions/trf.h>
  38. #include <lib/utils.h>
  39. #if ENABLE_FEAT_TWED
  40. /* Make sure delay value fits within the range(0-15) */
  41. CASSERT(((TWED_DELAY & ~SCR_TWEDEL_MASK) == 0U), assert_twed_delay_value_check);
  42. #endif /* ENABLE_FEAT_TWED */
  43. per_world_context_t per_world_context[CPU_DATA_CONTEXT_NUM];
  44. static bool has_secure_perworld_init;
  45. static void manage_extensions_common(cpu_context_t *ctx);
  46. static void manage_extensions_nonsecure(cpu_context_t *ctx);
  47. static void manage_extensions_secure(cpu_context_t *ctx);
  48. static void manage_extensions_secure_per_world(void);
  49. #if ((IMAGE_BL1) || (IMAGE_BL31 && (!CTX_INCLUDE_EL2_REGS)))
  50. static void setup_el1_context(cpu_context_t *ctx, const struct entry_point_info *ep)
  51. {
  52. u_register_t sctlr_elx, actlr_elx;
  53. /*
  54. * Initialise SCTLR_EL1 to the reset value corresponding to the target
  55. * execution state setting all fields rather than relying on the hw.
  56. * Some fields have architecturally UNKNOWN reset values and these are
  57. * set to zero.
  58. *
  59. * SCTLR.EE: Endianness is taken from the entrypoint attributes.
  60. *
  61. * SCTLR.M, SCTLR.C and SCTLR.I: These fields must be zero (as
  62. * required by PSCI specification)
  63. */
  64. sctlr_elx = (EP_GET_EE(ep->h.attr) != 0U) ? SCTLR_EE_BIT : 0UL;
  65. if (GET_RW(ep->spsr) == MODE_RW_64) {
  66. sctlr_elx |= SCTLR_EL1_RES1;
  67. } else {
  68. /*
  69. * If the target execution state is AArch32 then the following
  70. * fields need to be set.
  71. *
  72. * SCTRL_EL1.nTWE: Set to one so that EL0 execution of WFE
  73. * instructions are not trapped to EL1.
  74. *
  75. * SCTLR_EL1.nTWI: Set to one so that EL0 execution of WFI
  76. * instructions are not trapped to EL1.
  77. *
  78. * SCTLR_EL1.CP15BEN: Set to one to enable EL0 execution of the
  79. * CP15DMB, CP15DSB, and CP15ISB instructions.
  80. */
  81. sctlr_elx |= SCTLR_AARCH32_EL1_RES1 | SCTLR_CP15BEN_BIT
  82. | SCTLR_NTWI_BIT | SCTLR_NTWE_BIT;
  83. }
  84. /*
  85. * If workaround of errata 764081 for Cortex-A75 is used then set
  86. * SCTLR_EL1.IESB to enable Implicit Error Synchronization Barrier.
  87. */
  88. if (errata_a75_764081_applies()) {
  89. sctlr_elx |= SCTLR_IESB_BIT;
  90. }
  91. /* Store the initialised SCTLR_EL1 value in the cpu_context */
  92. write_ctx_sctlr_el1_reg_errata(ctx, sctlr_elx);
  93. /*
  94. * Base the context ACTLR_EL1 on the current value, as it is
  95. * implementation defined. The context restore process will write
  96. * the value from the context to the actual register and can cause
  97. * problems for processor cores that don't expect certain bits to
  98. * be zero.
  99. */
  100. actlr_elx = read_actlr_el1();
  101. write_el1_ctx_common(get_el1_sysregs_ctx(ctx), actlr_el1, actlr_elx);
  102. }
  103. #endif /* (IMAGE_BL1) || (IMAGE_BL31 && (!CTX_INCLUDE_EL2_REGS)) */
  104. /******************************************************************************
  105. * This function performs initializations that are specific to SECURE state
  106. * and updates the cpu context specified by 'ctx'.
  107. *****************************************************************************/
  108. static void setup_secure_context(cpu_context_t *ctx, const struct entry_point_info *ep)
  109. {
  110. u_register_t scr_el3;
  111. el3_state_t *state;
  112. state = get_el3state_ctx(ctx);
  113. scr_el3 = read_ctx_reg(state, CTX_SCR_EL3);
  114. #if defined(IMAGE_BL31) && !defined(SPD_spmd)
  115. /*
  116. * SCR_EL3.IRQ, SCR_EL3.FIQ: Enable the physical FIQ and IRQ routing as
  117. * indicated by the interrupt routing model for BL31.
  118. */
  119. scr_el3 |= get_scr_el3_from_routing_model(SECURE);
  120. #endif
  121. /* Allow access to Allocation Tags when FEAT_MTE2 is implemented and enabled. */
  122. if (is_feat_mte2_supported()) {
  123. scr_el3 |= SCR_ATA_BIT;
  124. }
  125. write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
  126. /*
  127. * Initialize EL1 context registers unless SPMC is running
  128. * at S-EL2.
  129. */
  130. #if (!SPMD_SPM_AT_SEL2)
  131. setup_el1_context(ctx, ep);
  132. #endif
  133. manage_extensions_secure(ctx);
  134. /**
  135. * manage_extensions_secure_per_world api has to be executed once,
  136. * as the registers getting initialised, maintain constant value across
  137. * all the cpus for the secure world.
  138. * Henceforth, this check ensures that the registers are initialised once
  139. * and avoids re-initialization from multiple cores.
  140. */
  141. if (!has_secure_perworld_init) {
  142. manage_extensions_secure_per_world();
  143. }
  144. }
  145. #if ENABLE_RME
  146. /******************************************************************************
  147. * This function performs initializations that are specific to REALM state
  148. * and updates the cpu context specified by 'ctx'.
  149. *****************************************************************************/
  150. static void setup_realm_context(cpu_context_t *ctx, const struct entry_point_info *ep)
  151. {
  152. u_register_t scr_el3;
  153. el3_state_t *state;
  154. state = get_el3state_ctx(ctx);
  155. scr_el3 = read_ctx_reg(state, CTX_SCR_EL3);
  156. scr_el3 |= SCR_NS_BIT | SCR_NSE_BIT;
  157. /* CSV2 version 2 and above */
  158. if (is_feat_csv2_2_supported()) {
  159. /* Enable access to the SCXTNUM_ELx registers. */
  160. scr_el3 |= SCR_EnSCXT_BIT;
  161. }
  162. if (is_feat_sctlr2_supported()) {
  163. /* Set the SCTLR2En bit in SCR_EL3 to enable access to
  164. * SCTLR2_ELx registers.
  165. */
  166. scr_el3 |= SCR_SCTLR2En_BIT;
  167. }
  168. write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
  169. }
  170. #endif /* ENABLE_RME */
  171. /******************************************************************************
  172. * This function performs initializations that are specific to NON-SECURE state
  173. * and updates the cpu context specified by 'ctx'.
  174. *****************************************************************************/
  175. static void setup_ns_context(cpu_context_t *ctx, const struct entry_point_info *ep)
  176. {
  177. u_register_t scr_el3;
  178. el3_state_t *state;
  179. state = get_el3state_ctx(ctx);
  180. scr_el3 = read_ctx_reg(state, CTX_SCR_EL3);
  181. /* SCR_NS: Set the NS bit */
  182. scr_el3 |= SCR_NS_BIT;
  183. /* Allow access to Allocation Tags when FEAT_MTE2 is implemented and enabled. */
  184. if (is_feat_mte2_supported()) {
  185. scr_el3 |= SCR_ATA_BIT;
  186. }
  187. #if !CTX_INCLUDE_PAUTH_REGS
  188. /*
  189. * Pointer Authentication feature, if present, is always enabled by default
  190. * for Non secure lower exception levels. We do not have an explicit
  191. * flag to set it.
  192. * CTX_INCLUDE_PAUTH_REGS flag, is explicitly used to enable for lower
  193. * exception levels of secure and realm worlds.
  194. *
  195. * To prevent the leakage between the worlds during world switch,
  196. * we enable it only for the non-secure world.
  197. *
  198. * If the Secure/realm world wants to use pointer authentication,
  199. * CTX_INCLUDE_PAUTH_REGS must be explicitly set to 1, in which case
  200. * it will be enabled globally for all the contexts.
  201. *
  202. * SCR_EL3.API: Set to one to not trap any PAuth instructions at ELs
  203. * other than EL3
  204. *
  205. * SCR_EL3.APK: Set to one to not trap any PAuth key values at ELs other
  206. * than EL3
  207. */
  208. scr_el3 |= SCR_API_BIT | SCR_APK_BIT;
  209. #endif /* CTX_INCLUDE_PAUTH_REGS */
  210. #if HANDLE_EA_EL3_FIRST_NS
  211. /* SCR_EL3.EA: Route External Abort and SError Interrupt to EL3. */
  212. scr_el3 |= SCR_EA_BIT;
  213. #endif
  214. #if RAS_TRAP_NS_ERR_REC_ACCESS
  215. /*
  216. * SCR_EL3.TERR: Trap Error record accesses. Accesses to the RAS ERR
  217. * and RAS ERX registers from EL1 and EL2(from any security state)
  218. * are trapped to EL3.
  219. * Set here to trap only for NS EL1/EL2
  220. *
  221. */
  222. scr_el3 |= SCR_TERR_BIT;
  223. #endif
  224. /* CSV2 version 2 and above */
  225. if (is_feat_csv2_2_supported()) {
  226. /* Enable access to the SCXTNUM_ELx registers. */
  227. scr_el3 |= SCR_EnSCXT_BIT;
  228. }
  229. #ifdef IMAGE_BL31
  230. /*
  231. * SCR_EL3.IRQ, SCR_EL3.FIQ: Enable the physical FIQ and IRQ routing as
  232. * indicated by the interrupt routing model for BL31.
  233. */
  234. scr_el3 |= get_scr_el3_from_routing_model(NON_SECURE);
  235. #endif
  236. if (is_feat_the_supported()) {
  237. /* Set the RCWMASKEn bit in SCR_EL3 to enable access to
  238. * RCWMASK_EL1 and RCWSMASK_EL1 registers.
  239. */
  240. scr_el3 |= SCR_RCWMASKEn_BIT;
  241. }
  242. if (is_feat_sctlr2_supported()) {
  243. /* Set the SCTLR2En bit in SCR_EL3 to enable access to
  244. * SCTLR2_ELx registers.
  245. */
  246. scr_el3 |= SCR_SCTLR2En_BIT;
  247. }
  248. if (is_feat_d128_supported()) {
  249. /* Set the D128En bit in SCR_EL3 to enable access to 128-bit
  250. * versions of TTBR0_EL1, TTBR1_EL1, RCWMASK_EL1, RCWSMASK_EL1,
  251. * PAR_EL1 and TTBR1_EL2, TTBR0_EL2 and VTTBR_EL2 registers.
  252. */
  253. scr_el3 |= SCR_D128En_BIT;
  254. }
  255. write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
  256. /* Initialize EL2 context registers */
  257. #if (CTX_INCLUDE_EL2_REGS && IMAGE_BL31)
  258. /*
  259. * Initialize SCTLR_EL2 context register with reset value.
  260. */
  261. write_el2_ctx_common(get_el2_sysregs_ctx(ctx), sctlr_el2, SCTLR_EL2_RES1);
  262. if (is_feat_hcx_supported()) {
  263. /*
  264. * Initialize register HCRX_EL2 with its init value.
  265. * As the value of HCRX_EL2 is UNKNOWN on reset, there is a
  266. * chance that this can lead to unexpected behavior in lower
  267. * ELs that have not been updated since the introduction of
  268. * this feature if not properly initialized, especially when
  269. * it comes to those bits that enable/disable traps.
  270. */
  271. write_el2_ctx_hcx(get_el2_sysregs_ctx(ctx), hcrx_el2,
  272. HCRX_EL2_INIT_VAL);
  273. }
  274. if (is_feat_fgt_supported()) {
  275. /*
  276. * Initialize HFG*_EL2 registers with a default value so legacy
  277. * systems unaware of FEAT_FGT do not get trapped due to their lack
  278. * of initialization for this feature.
  279. */
  280. write_el2_ctx_fgt(get_el2_sysregs_ctx(ctx), hfgitr_el2,
  281. HFGITR_EL2_INIT_VAL);
  282. write_el2_ctx_fgt(get_el2_sysregs_ctx(ctx), hfgrtr_el2,
  283. HFGRTR_EL2_INIT_VAL);
  284. write_el2_ctx_fgt(get_el2_sysregs_ctx(ctx), hfgwtr_el2,
  285. HFGWTR_EL2_INIT_VAL);
  286. }
  287. #else
  288. /* Initialize EL1 context registers */
  289. setup_el1_context(ctx, ep);
  290. #endif /* (CTX_INCLUDE_EL2_REGS && IMAGE_BL31) */
  291. manage_extensions_nonsecure(ctx);
  292. }
  293. /*******************************************************************************
  294. * The following function performs initialization of the cpu_context 'ctx'
  295. * for first use that is common to all security states, and sets the
  296. * initial entrypoint state as specified by the entry_point_info structure.
  297. *
  298. * The EE and ST attributes are used to configure the endianness and secure
  299. * timer availability for the new execution context.
  300. ******************************************************************************/
  301. static void setup_context_common(cpu_context_t *ctx, const entry_point_info_t *ep)
  302. {
  303. u_register_t scr_el3;
  304. u_register_t mdcr_el3;
  305. el3_state_t *state;
  306. gp_regs_t *gp_regs;
  307. state = get_el3state_ctx(ctx);
  308. /* Clear any residual register values from the context */
  309. zeromem(ctx, sizeof(*ctx));
  310. /*
  311. * The lower-EL context is zeroed so that no stale values leak to a world.
  312. * It is assumed that an all-zero lower-EL context is good enough for it
  313. * to boot correctly. However, there are very few registers where this
  314. * is not true and some values need to be recreated.
  315. */
  316. #if (CTX_INCLUDE_EL2_REGS && IMAGE_BL31)
  317. el2_sysregs_t *el2_ctx = get_el2_sysregs_ctx(ctx);
  318. /*
  319. * These bits are set in the gicv3 driver. Losing them (especially the
  320. * SRE bit) is problematic for all worlds. Henceforth recreate them.
  321. */
  322. u_register_t icc_sre_el2_val = ICC_SRE_DIB_BIT | ICC_SRE_DFB_BIT |
  323. ICC_SRE_EN_BIT | ICC_SRE_SRE_BIT;
  324. write_el2_ctx_common(el2_ctx, icc_sre_el2, icc_sre_el2_val);
  325. /*
  326. * The actlr_el2 register can be initialized in platform's reset handler
  327. * and it may contain access control bits (e.g. CLUSTERPMUEN bit).
  328. */
  329. write_el2_ctx_common(el2_ctx, actlr_el2, read_actlr_el2());
  330. #endif /* (CTX_INCLUDE_EL2_REGS && IMAGE_BL31) */
  331. /* Start with a clean SCR_EL3 copy as all relevant values are set */
  332. scr_el3 = SCR_RESET_VAL;
  333. /*
  334. * SCR_EL3.TWE: Set to zero so that execution of WFE instructions at
  335. * EL2, EL1 and EL0 are not trapped to EL3.
  336. *
  337. * SCR_EL3.TWI: Set to zero so that execution of WFI instructions at
  338. * EL2, EL1 and EL0 are not trapped to EL3.
  339. *
  340. * SCR_EL3.SMD: Set to zero to enable SMC calls at EL1 and above, from
  341. * both Security states and both Execution states.
  342. *
  343. * SCR_EL3.SIF: Set to one to disable secure instruction execution from
  344. * Non-secure memory.
  345. */
  346. scr_el3 &= ~(SCR_TWE_BIT | SCR_TWI_BIT | SCR_SMD_BIT);
  347. scr_el3 |= SCR_SIF_BIT;
  348. /*
  349. * SCR_EL3.RW: Set the execution state, AArch32 or AArch64, for next
  350. * Exception level as specified by SPSR.
  351. */
  352. if (GET_RW(ep->spsr) == MODE_RW_64) {
  353. scr_el3 |= SCR_RW_BIT;
  354. }
  355. /*
  356. * SCR_EL3.ST: Traps Secure EL1 accesses to the Counter-timer Physical
  357. * Secure timer registers to EL3, from AArch64 state only, if specified
  358. * by the entrypoint attributes. If SEL2 is present and enabled, the ST
  359. * bit always behaves as 1 (i.e. secure physical timer register access
  360. * is not trapped)
  361. */
  362. if (EP_GET_ST(ep->h.attr) != 0U) {
  363. scr_el3 |= SCR_ST_BIT;
  364. }
  365. /*
  366. * If FEAT_HCX is enabled, enable access to HCRX_EL2 by setting
  367. * SCR_EL3.HXEn.
  368. */
  369. if (is_feat_hcx_supported()) {
  370. scr_el3 |= SCR_HXEn_BIT;
  371. }
  372. /*
  373. * If FEAT_LS64_ACCDATA is enabled, enable access to ACCDATA_EL1 by
  374. * setting SCR_EL3.ADEn and allow the ST64BV0 instruction by setting
  375. * SCR_EL3.EnAS0.
  376. */
  377. if (is_feat_ls64_accdata_supported()) {
  378. scr_el3 |= SCR_ADEn_BIT | SCR_EnAS0_BIT;
  379. }
  380. /*
  381. * If FEAT_RNG_TRAP is enabled, all reads of the RNDR and RNDRRS
  382. * registers are trapped to EL3.
  383. */
  384. #if ENABLE_FEAT_RNG_TRAP
  385. scr_el3 |= SCR_TRNDR_BIT;
  386. #endif
  387. #if FAULT_INJECTION_SUPPORT
  388. /* Enable fault injection from lower ELs */
  389. scr_el3 |= SCR_FIEN_BIT;
  390. #endif
  391. #if CTX_INCLUDE_PAUTH_REGS
  392. /*
  393. * Enable Pointer Authentication globally for all the worlds.
  394. *
  395. * SCR_EL3.API: Set to one to not trap any PAuth instructions at ELs
  396. * other than EL3
  397. *
  398. * SCR_EL3.APK: Set to one to not trap any PAuth key values at ELs other
  399. * than EL3
  400. */
  401. scr_el3 |= SCR_API_BIT | SCR_APK_BIT;
  402. #endif /* CTX_INCLUDE_PAUTH_REGS */
  403. /*
  404. * SCR_EL3.TCR2EN: Enable access to TCR2_ELx for AArch64 if present.
  405. */
  406. if (is_feat_tcr2_supported() && (GET_RW(ep->spsr) == MODE_RW_64)) {
  407. scr_el3 |= SCR_TCR2EN_BIT;
  408. }
  409. /*
  410. * SCR_EL3.PIEN: Enable permission indirection and overlay
  411. * registers for AArch64 if present.
  412. */
  413. if (is_feat_sxpie_supported() || is_feat_sxpoe_supported()) {
  414. scr_el3 |= SCR_PIEN_BIT;
  415. }
  416. /*
  417. * SCR_EL3.GCSEn: Enable GCS registers for AArch64 if present.
  418. */
  419. if ((is_feat_gcs_supported()) && (GET_RW(ep->spsr) == MODE_RW_64)) {
  420. scr_el3 |= SCR_GCSEn_BIT;
  421. }
  422. /*
  423. * SCR_EL3.HCE: Enable HVC instructions if next execution state is
  424. * AArch64 and next EL is EL2, or if next execution state is AArch32 and
  425. * next mode is Hyp.
  426. * SCR_EL3.FGTEn: Enable Fine Grained Virtualization Traps under the
  427. * same conditions as HVC instructions and when the processor supports
  428. * ARMv8.6-FGT.
  429. * SCR_EL3.ECVEn: Enable Enhanced Counter Virtualization (ECV)
  430. * CNTPOFF_EL2 register under the same conditions as HVC instructions
  431. * and when the processor supports ECV.
  432. */
  433. if (((GET_RW(ep->spsr) == MODE_RW_64) && (GET_EL(ep->spsr) == MODE_EL2))
  434. || ((GET_RW(ep->spsr) != MODE_RW_64)
  435. && (GET_M32(ep->spsr) == MODE32_hyp))) {
  436. scr_el3 |= SCR_HCE_BIT;
  437. if (is_feat_fgt_supported()) {
  438. scr_el3 |= SCR_FGTEN_BIT;
  439. }
  440. if (is_feat_ecv_supported()) {
  441. scr_el3 |= SCR_ECVEN_BIT;
  442. }
  443. }
  444. /* Enable WFE trap delay in SCR_EL3 if supported and configured */
  445. if (is_feat_twed_supported()) {
  446. /* Set delay in SCR_EL3 */
  447. scr_el3 &= ~(SCR_TWEDEL_MASK << SCR_TWEDEL_SHIFT);
  448. scr_el3 |= ((TWED_DELAY & SCR_TWEDEL_MASK)
  449. << SCR_TWEDEL_SHIFT);
  450. /* Enable WFE delay */
  451. scr_el3 |= SCR_TWEDEn_BIT;
  452. }
  453. #if IMAGE_BL31 && defined(SPD_spmd) && SPMD_SPM_AT_SEL2
  454. /* Enable S-EL2 if FEAT_SEL2 is implemented for all the contexts. */
  455. if (is_feat_sel2_supported()) {
  456. scr_el3 |= SCR_EEL2_BIT;
  457. }
  458. #endif /* (IMAGE_BL31 && defined(SPD_spmd) && SPMD_SPM_AT_SEL2) */
  459. /*
  460. * Populate EL3 state so that we've the right context
  461. * before doing ERET
  462. */
  463. write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
  464. write_ctx_reg(state, CTX_ELR_EL3, ep->pc);
  465. write_ctx_reg(state, CTX_SPSR_EL3, ep->spsr);
  466. /* Start with a clean MDCR_EL3 copy as all relevant values are set */
  467. mdcr_el3 = MDCR_EL3_RESET_VAL;
  468. /* ---------------------------------------------------------------------
  469. * Initialise MDCR_EL3, setting all fields rather than relying on hw.
  470. * Some fields are architecturally UNKNOWN on reset.
  471. *
  472. * MDCR_EL3.SDD: Set to one to disable AArch64 Secure self-hosted debug.
  473. * Debug exceptions, other than Breakpoint Instruction exceptions, are
  474. * disabled from all ELs in Secure state.
  475. *
  476. * MDCR_EL3.SPD32: Set to 0b10 to disable AArch32 Secure self-hosted
  477. * privileged debug from S-EL1.
  478. *
  479. * MDCR_EL3.TDOSA: Set to zero so that EL2 and EL2 System register
  480. * access to the powerdown debug registers do not trap to EL3.
  481. *
  482. * MDCR_EL3.TDA: Set to zero to allow EL0, EL1 and EL2 access to the
  483. * debug registers, other than those registers that are controlled by
  484. * MDCR_EL3.TDOSA.
  485. */
  486. mdcr_el3 |= ((MDCR_SDD_BIT | MDCR_SPD32(MDCR_SPD32_DISABLE))
  487. & ~(MDCR_TDA_BIT | MDCR_TDOSA_BIT)) ;
  488. write_ctx_reg(state, CTX_MDCR_EL3, mdcr_el3);
  489. /*
  490. * Configure MDCR_EL3 register as applicable for each world
  491. * (NS/Secure/Realm) context.
  492. */
  493. manage_extensions_common(ctx);
  494. /*
  495. * Store the X0-X7 value from the entrypoint into the context
  496. * Use memcpy as we are in control of the layout of the structures
  497. */
  498. gp_regs = get_gpregs_ctx(ctx);
  499. memcpy(gp_regs, (void *)&ep->args, sizeof(aapcs64_params_t));
  500. }
  501. /*******************************************************************************
  502. * Context management library initialization routine. This library is used by
  503. * runtime services to share pointers to 'cpu_context' structures for secure
  504. * non-secure and realm states. Management of the structures and their associated
  505. * memory is not done by the context management library e.g. the PSCI service
  506. * manages the cpu context used for entry from and exit to the non-secure state.
  507. * The Secure payload dispatcher service manages the context(s) corresponding to
  508. * the secure state. It also uses this library to get access to the non-secure
  509. * state cpu context pointers.
  510. * Lastly, this library provides the API to make SP_EL3 point to the cpu context
  511. * which will be used for programming an entry into a lower EL. The same context
  512. * will be used to save state upon exception entry from that EL.
  513. ******************************************************************************/
  514. void __init cm_init(void)
  515. {
  516. /*
  517. * The context management library has only global data to initialize, but
  518. * that will be done when the BSS is zeroed out.
  519. */
  520. }
  521. /*******************************************************************************
  522. * This is the high-level function used to initialize the cpu_context 'ctx' for
  523. * first use. It performs initializations that are common to all security states
  524. * and initializations specific to the security state specified in 'ep'
  525. ******************************************************************************/
  526. void cm_setup_context(cpu_context_t *ctx, const entry_point_info_t *ep)
  527. {
  528. unsigned int security_state;
  529. assert(ctx != NULL);
  530. /*
  531. * Perform initializations that are common
  532. * to all security states
  533. */
  534. setup_context_common(ctx, ep);
  535. security_state = GET_SECURITY_STATE(ep->h.attr);
  536. /* Perform security state specific initializations */
  537. switch (security_state) {
  538. case SECURE:
  539. setup_secure_context(ctx, ep);
  540. break;
  541. #if ENABLE_RME
  542. case REALM:
  543. setup_realm_context(ctx, ep);
  544. break;
  545. #endif
  546. case NON_SECURE:
  547. setup_ns_context(ctx, ep);
  548. break;
  549. default:
  550. ERROR("Invalid security state\n");
  551. panic();
  552. break;
  553. }
  554. }
  555. /*******************************************************************************
  556. * Enable architecture extensions for EL3 execution. This function only updates
  557. * registers in-place which are expected to either never change or be
  558. * overwritten by el3_exit.
  559. ******************************************************************************/
  560. #if IMAGE_BL31
  561. void cm_manage_extensions_el3(void)
  562. {
  563. if (is_feat_amu_supported()) {
  564. amu_init_el3();
  565. }
  566. if (is_feat_sme_supported()) {
  567. sme_init_el3();
  568. }
  569. pmuv3_init_el3();
  570. }
  571. #endif /* IMAGE_BL31 */
  572. /******************************************************************************
  573. * Function to initialise the registers with the RESET values in the context
  574. * memory, which are maintained per world.
  575. ******************************************************************************/
  576. #if IMAGE_BL31
  577. void cm_el3_arch_init_per_world(per_world_context_t *per_world_ctx)
  578. {
  579. /*
  580. * Initialise CPTR_EL3, setting all fields rather than relying on hw.
  581. *
  582. * CPTR_EL3.TFP: Set to zero so that accesses to the V- or Z- registers
  583. * by Advanced SIMD, floating-point or SVE instructions (if
  584. * implemented) do not trap to EL3.
  585. *
  586. * CPTR_EL3.TCPAC: Set to zero so that accesses to CPACR_EL1,
  587. * CPTR_EL2,CPACR, or HCPTR do not trap to EL3.
  588. */
  589. uint64_t cptr_el3 = CPTR_EL3_RESET_VAL & ~(TCPAC_BIT | TFP_BIT);
  590. per_world_ctx->ctx_cptr_el3 = cptr_el3;
  591. /*
  592. * Initialize MPAM3_EL3 to its default reset value
  593. *
  594. * MPAM3_EL3_RESET_VAL sets the MPAM3_EL3.TRAPLOWER bit that forces
  595. * all lower ELn MPAM3_EL3 register access to, trap to EL3
  596. */
  597. per_world_ctx->ctx_mpam3_el3 = MPAM3_EL3_RESET_VAL;
  598. }
  599. #endif /* IMAGE_BL31 */
  600. /*******************************************************************************
  601. * Initialise per_world_context for Non-Secure world.
  602. * This function enables the architecture extensions, which have same value
  603. * across the cores for the non-secure world.
  604. ******************************************************************************/
  605. #if IMAGE_BL31
  606. void manage_extensions_nonsecure_per_world(void)
  607. {
  608. cm_el3_arch_init_per_world(&per_world_context[CPU_CONTEXT_NS]);
  609. if (is_feat_sme_supported()) {
  610. sme_enable_per_world(&per_world_context[CPU_CONTEXT_NS]);
  611. }
  612. if (is_feat_sve_supported()) {
  613. sve_enable_per_world(&per_world_context[CPU_CONTEXT_NS]);
  614. }
  615. if (is_feat_amu_supported()) {
  616. amu_enable_per_world(&per_world_context[CPU_CONTEXT_NS]);
  617. }
  618. if (is_feat_sys_reg_trace_supported()) {
  619. sys_reg_trace_enable_per_world(&per_world_context[CPU_CONTEXT_NS]);
  620. }
  621. if (is_feat_mpam_supported()) {
  622. mpam_enable_per_world(&per_world_context[CPU_CONTEXT_NS]);
  623. }
  624. }
  625. #endif /* IMAGE_BL31 */
  626. /*******************************************************************************
  627. * Initialise per_world_context for Secure world.
  628. * This function enables the architecture extensions, which have same value
  629. * across the cores for the secure world.
  630. ******************************************************************************/
  631. static void manage_extensions_secure_per_world(void)
  632. {
  633. #if IMAGE_BL31
  634. cm_el3_arch_init_per_world(&per_world_context[CPU_CONTEXT_SECURE]);
  635. if (is_feat_sme_supported()) {
  636. if (ENABLE_SME_FOR_SWD) {
  637. /*
  638. * Enable SME, SVE, FPU/SIMD in secure context, SPM must ensure
  639. * SME, SVE, and FPU/SIMD context properly managed.
  640. */
  641. sme_enable_per_world(&per_world_context[CPU_CONTEXT_SECURE]);
  642. } else {
  643. /*
  644. * Disable SME, SVE, FPU/SIMD in secure context so non-secure
  645. * world can safely use the associated registers.
  646. */
  647. sme_disable_per_world(&per_world_context[CPU_CONTEXT_SECURE]);
  648. }
  649. }
  650. if (is_feat_sve_supported()) {
  651. if (ENABLE_SVE_FOR_SWD) {
  652. /*
  653. * Enable SVE and FPU in secure context, SPM must ensure
  654. * that the SVE and FPU register contexts are properly managed.
  655. */
  656. sve_enable_per_world(&per_world_context[CPU_CONTEXT_SECURE]);
  657. } else {
  658. /*
  659. * Disable SVE and FPU in secure context so non-secure world
  660. * can safely use them.
  661. */
  662. sve_disable_per_world(&per_world_context[CPU_CONTEXT_SECURE]);
  663. }
  664. }
  665. /* NS can access this but Secure shouldn't */
  666. if (is_feat_sys_reg_trace_supported()) {
  667. sys_reg_trace_disable_per_world(&per_world_context[CPU_CONTEXT_SECURE]);
  668. }
  669. has_secure_perworld_init = true;
  670. #endif /* IMAGE_BL31 */
  671. }
  672. /*******************************************************************************
  673. * Enable architecture extensions on first entry to Non-secure world only
  674. * and disable for secure world.
  675. *
  676. * NOTE: Arch features which have been provided with the capability of getting
  677. * enabled only for non-secure world and being disabled for secure world are
  678. * grouped here, as the MDCR_EL3 context value remains same across the worlds.
  679. ******************************************************************************/
  680. static void manage_extensions_common(cpu_context_t *ctx)
  681. {
  682. #if IMAGE_BL31
  683. if (is_feat_spe_supported()) {
  684. /*
  685. * Enable FEAT_SPE for Non-Secure and prohibit for Secure state.
  686. */
  687. spe_enable(ctx);
  688. }
  689. if (is_feat_trbe_supported()) {
  690. /*
  691. * Enable FEAT_TRBE for Non-Secure and prohibit for Secure and
  692. * Realm state.
  693. */
  694. trbe_enable(ctx);
  695. }
  696. if (is_feat_trf_supported()) {
  697. /*
  698. * Enable FEAT_TRF for Non-Secure and prohibit for Secure state.
  699. */
  700. trf_enable(ctx);
  701. }
  702. #endif /* IMAGE_BL31 */
  703. }
  704. /*******************************************************************************
  705. * Enable architecture extensions on first entry to Non-secure world.
  706. ******************************************************************************/
  707. static void manage_extensions_nonsecure(cpu_context_t *ctx)
  708. {
  709. #if IMAGE_BL31
  710. if (is_feat_amu_supported()) {
  711. amu_enable(ctx);
  712. }
  713. if (is_feat_sme_supported()) {
  714. sme_enable(ctx);
  715. }
  716. if (is_feat_fgt2_supported()) {
  717. fgt2_enable(ctx);
  718. }
  719. if (is_feat_debugv8p9_supported()) {
  720. debugv8p9_extended_bp_wp_enable(ctx);
  721. }
  722. if (is_feat_brbe_supported()) {
  723. brbe_enable(ctx);
  724. }
  725. pmuv3_enable(ctx);
  726. #endif /* IMAGE_BL31 */
  727. }
  728. /* TODO: move to lib/extensions/pauth when it has been ported to FEAT_STATE */
  729. static __unused void enable_pauth_el2(void)
  730. {
  731. u_register_t hcr_el2 = read_hcr_el2();
  732. /*
  733. * For Armv8.3 pointer authentication feature, disable traps to EL2 when
  734. * accessing key registers or using pointer authentication instructions
  735. * from lower ELs.
  736. */
  737. hcr_el2 |= (HCR_API_BIT | HCR_APK_BIT);
  738. write_hcr_el2(hcr_el2);
  739. }
  740. #if INIT_UNUSED_NS_EL2
  741. /*******************************************************************************
  742. * Enable architecture extensions in-place at EL2 on first entry to Non-secure
  743. * world when EL2 is empty and unused.
  744. ******************************************************************************/
  745. static void manage_extensions_nonsecure_el2_unused(void)
  746. {
  747. #if IMAGE_BL31
  748. if (is_feat_spe_supported()) {
  749. spe_init_el2_unused();
  750. }
  751. if (is_feat_amu_supported()) {
  752. amu_init_el2_unused();
  753. }
  754. if (is_feat_mpam_supported()) {
  755. mpam_init_el2_unused();
  756. }
  757. if (is_feat_trbe_supported()) {
  758. trbe_init_el2_unused();
  759. }
  760. if (is_feat_sys_reg_trace_supported()) {
  761. sys_reg_trace_init_el2_unused();
  762. }
  763. if (is_feat_trf_supported()) {
  764. trf_init_el2_unused();
  765. }
  766. pmuv3_init_el2_unused();
  767. if (is_feat_sve_supported()) {
  768. sve_init_el2_unused();
  769. }
  770. if (is_feat_sme_supported()) {
  771. sme_init_el2_unused();
  772. }
  773. #if ENABLE_PAUTH
  774. enable_pauth_el2();
  775. #endif /* ENABLE_PAUTH */
  776. #endif /* IMAGE_BL31 */
  777. }
  778. #endif /* INIT_UNUSED_NS_EL2 */
  779. /*******************************************************************************
  780. * Enable architecture extensions on first entry to Secure world.
  781. ******************************************************************************/
  782. static void manage_extensions_secure(cpu_context_t *ctx)
  783. {
  784. #if IMAGE_BL31
  785. if (is_feat_sme_supported()) {
  786. if (ENABLE_SME_FOR_SWD) {
  787. /*
  788. * Enable SME, SVE, FPU/SIMD in secure context, secure manager
  789. * must ensure SME, SVE, and FPU/SIMD context properly managed.
  790. */
  791. sme_init_el3();
  792. sme_enable(ctx);
  793. } else {
  794. /*
  795. * Disable SME, SVE, FPU/SIMD in secure context so non-secure
  796. * world can safely use the associated registers.
  797. */
  798. sme_disable(ctx);
  799. }
  800. }
  801. #endif /* IMAGE_BL31 */
  802. }
  803. #if !IMAGE_BL1
  804. /*******************************************************************************
  805. * The following function initializes the cpu_context for a CPU specified by
  806. * its `cpu_idx` for first use, and sets the initial entrypoint state as
  807. * specified by the entry_point_info structure.
  808. ******************************************************************************/
  809. void cm_init_context_by_index(unsigned int cpu_idx,
  810. const entry_point_info_t *ep)
  811. {
  812. cpu_context_t *ctx;
  813. ctx = cm_get_context_by_index(cpu_idx, GET_SECURITY_STATE(ep->h.attr));
  814. cm_setup_context(ctx, ep);
  815. }
  816. #endif /* !IMAGE_BL1 */
  817. /*******************************************************************************
  818. * The following function initializes the cpu_context for the current CPU
  819. * for first use, and sets the initial entrypoint state as specified by the
  820. * entry_point_info structure.
  821. ******************************************************************************/
  822. void cm_init_my_context(const entry_point_info_t *ep)
  823. {
  824. cpu_context_t *ctx;
  825. ctx = cm_get_context(GET_SECURITY_STATE(ep->h.attr));
  826. cm_setup_context(ctx, ep);
  827. }
  828. /* EL2 present but unused, need to disable safely. SCTLR_EL2 can be ignored */
  829. static void init_nonsecure_el2_unused(cpu_context_t *ctx)
  830. {
  831. #if INIT_UNUSED_NS_EL2
  832. u_register_t hcr_el2 = HCR_RESET_VAL;
  833. u_register_t mdcr_el2;
  834. u_register_t scr_el3;
  835. scr_el3 = read_ctx_reg(get_el3state_ctx(ctx), CTX_SCR_EL3);
  836. /* Set EL2 register width: Set HCR_EL2.RW to match SCR_EL3.RW */
  837. if ((scr_el3 & SCR_RW_BIT) != 0U) {
  838. hcr_el2 |= HCR_RW_BIT;
  839. }
  840. write_hcr_el2(hcr_el2);
  841. /*
  842. * Initialise CPTR_EL2 setting all fields rather than relying on the hw.
  843. * All fields have architecturally UNKNOWN reset values.
  844. */
  845. write_cptr_el2(CPTR_EL2_RESET_VAL);
  846. /*
  847. * Initialise CNTHCTL_EL2. All fields are architecturally UNKNOWN on
  848. * reset and are set to zero except for field(s) listed below.
  849. *
  850. * CNTHCTL_EL2.EL1PTEN: Set to one to disable traps to Hyp mode of
  851. * Non-secure EL0 and EL1 accesses to the physical timer registers.
  852. *
  853. * CNTHCTL_EL2.EL1PCTEN: Set to one to disable traps to Hyp mode of
  854. * Non-secure EL0 and EL1 accesses to the physical counter registers.
  855. */
  856. write_cnthctl_el2(CNTHCTL_RESET_VAL | EL1PCEN_BIT | EL1PCTEN_BIT);
  857. /*
  858. * Initialise CNTVOFF_EL2 to zero as it resets to an architecturally
  859. * UNKNOWN value.
  860. */
  861. write_cntvoff_el2(0);
  862. /*
  863. * Set VPIDR_EL2 and VMPIDR_EL2 to match MIDR_EL1 and MPIDR_EL1
  864. * respectively.
  865. */
  866. write_vpidr_el2(read_midr_el1());
  867. write_vmpidr_el2(read_mpidr_el1());
  868. /*
  869. * Initialise VTTBR_EL2. All fields are architecturally UNKNOWN on reset.
  870. *
  871. * VTTBR_EL2.VMID: Set to zero. Even though EL1&0 stage 2 address
  872. * translation is disabled, cache maintenance operations depend on the
  873. * VMID.
  874. *
  875. * VTTBR_EL2.BADDR: Set to zero as EL1&0 stage 2 address translation is
  876. * disabled.
  877. */
  878. write_vttbr_el2(VTTBR_RESET_VAL &
  879. ~((VTTBR_VMID_MASK << VTTBR_VMID_SHIFT) |
  880. (VTTBR_BADDR_MASK << VTTBR_BADDR_SHIFT)));
  881. /*
  882. * Initialise MDCR_EL2, setting all fields rather than relying on hw.
  883. * Some fields are architecturally UNKNOWN on reset.
  884. *
  885. * MDCR_EL2.TDRA: Set to zero so that Non-secure EL0 and EL1 System
  886. * register accesses to the Debug ROM registers are not trapped to EL2.
  887. *
  888. * MDCR_EL2.TDOSA: Set to zero so that Non-secure EL1 System register
  889. * accesses to the powerdown debug registers are not trapped to EL2.
  890. *
  891. * MDCR_EL2.TDA: Set to zero so that System register accesses to the
  892. * debug registers do not trap to EL2.
  893. *
  894. * MDCR_EL2.TDE: Set to zero so that debug exceptions are not routed to
  895. * EL2.
  896. */
  897. mdcr_el2 = MDCR_EL2_RESET_VAL &
  898. ~(MDCR_EL2_TDRA_BIT | MDCR_EL2_TDOSA_BIT | MDCR_EL2_TDA_BIT |
  899. MDCR_EL2_TDE_BIT);
  900. write_mdcr_el2(mdcr_el2);
  901. /*
  902. * Initialise HSTR_EL2. All fields are architecturally UNKNOWN on reset.
  903. *
  904. * HSTR_EL2.T<n>: Set all these fields to zero so that Non-secure EL0 or
  905. * EL1 accesses to System registers do not trap to EL2.
  906. */
  907. write_hstr_el2(HSTR_EL2_RESET_VAL & ~(HSTR_EL2_T_MASK));
  908. /*
  909. * Initialise CNTHP_CTL_EL2. All fields are architecturally UNKNOWN on
  910. * reset.
  911. *
  912. * CNTHP_CTL_EL2:ENABLE: Set to zero to disable the EL2 physical timer
  913. * and prevent timer interrupts.
  914. */
  915. write_cnthp_ctl_el2(CNTHP_CTL_RESET_VAL & ~(CNTHP_CTL_ENABLE_BIT));
  916. manage_extensions_nonsecure_el2_unused();
  917. #endif /* INIT_UNUSED_NS_EL2 */
  918. }
  919. /*******************************************************************************
  920. * Prepare the CPU system registers for first entry into realm, secure, or
  921. * normal world.
  922. *
  923. * If execution is requested to EL2 or hyp mode, SCTLR_EL2 is initialized
  924. * If execution is requested to non-secure EL1 or svc mode, and the CPU supports
  925. * EL2 then EL2 is disabled by configuring all necessary EL2 registers.
  926. * For all entries, the EL1 registers are initialized from the cpu_context
  927. ******************************************************************************/
  928. void cm_prepare_el3_exit(uint32_t security_state)
  929. {
  930. u_register_t sctlr_el2, scr_el3;
  931. cpu_context_t *ctx = cm_get_context(security_state);
  932. assert(ctx != NULL);
  933. if (security_state == NON_SECURE) {
  934. uint64_t el2_implemented = el_implemented(2);
  935. scr_el3 = read_ctx_reg(get_el3state_ctx(ctx),
  936. CTX_SCR_EL3);
  937. if (el2_implemented != EL_IMPL_NONE) {
  938. /*
  939. * If context is not being used for EL2, initialize
  940. * HCRX_EL2 with its init value here.
  941. */
  942. if (is_feat_hcx_supported()) {
  943. write_hcrx_el2(HCRX_EL2_INIT_VAL);
  944. }
  945. /*
  946. * Initialize Fine-grained trap registers introduced
  947. * by FEAT_FGT so all traps are initially disabled when
  948. * switching to EL2 or a lower EL, preventing undesired
  949. * behavior.
  950. */
  951. if (is_feat_fgt_supported()) {
  952. /*
  953. * Initialize HFG*_EL2 registers with a default
  954. * value so legacy systems unaware of FEAT_FGT
  955. * do not get trapped due to their lack of
  956. * initialization for this feature.
  957. */
  958. write_hfgitr_el2(HFGITR_EL2_INIT_VAL);
  959. write_hfgrtr_el2(HFGRTR_EL2_INIT_VAL);
  960. write_hfgwtr_el2(HFGWTR_EL2_INIT_VAL);
  961. }
  962. /* Condition to ensure EL2 is being used. */
  963. if ((scr_el3 & SCR_HCE_BIT) != 0U) {
  964. /* Initialize SCTLR_EL2 register with reset value. */
  965. sctlr_el2 = SCTLR_EL2_RES1;
  966. /*
  967. * If workaround of errata 764081 for Cortex-A75
  968. * is used then set SCTLR_EL2.IESB to enable
  969. * Implicit Error Synchronization Barrier.
  970. */
  971. if (errata_a75_764081_applies()) {
  972. sctlr_el2 |= SCTLR_IESB_BIT;
  973. }
  974. write_sctlr_el2(sctlr_el2);
  975. } else {
  976. /*
  977. * (scr_el3 & SCR_HCE_BIT==0)
  978. * EL2 implemented but unused.
  979. */
  980. init_nonsecure_el2_unused(ctx);
  981. }
  982. }
  983. }
  984. #if (!CTX_INCLUDE_EL2_REGS)
  985. /* Restore EL1 system registers, only when CTX_INCLUDE_EL2_REGS=0 */
  986. cm_el1_sysregs_context_restore(security_state);
  987. #endif
  988. cm_set_next_eret_context(security_state);
  989. }
  990. #if (CTX_INCLUDE_EL2_REGS && IMAGE_BL31)
  991. static void el2_sysregs_context_save_fgt(el2_sysregs_t *ctx)
  992. {
  993. write_el2_ctx_fgt(ctx, hdfgrtr_el2, read_hdfgrtr_el2());
  994. if (is_feat_amu_supported()) {
  995. write_el2_ctx_fgt(ctx, hafgrtr_el2, read_hafgrtr_el2());
  996. }
  997. write_el2_ctx_fgt(ctx, hdfgwtr_el2, read_hdfgwtr_el2());
  998. write_el2_ctx_fgt(ctx, hfgitr_el2, read_hfgitr_el2());
  999. write_el2_ctx_fgt(ctx, hfgrtr_el2, read_hfgrtr_el2());
  1000. write_el2_ctx_fgt(ctx, hfgwtr_el2, read_hfgwtr_el2());
  1001. }
  1002. static void el2_sysregs_context_restore_fgt(el2_sysregs_t *ctx)
  1003. {
  1004. write_hdfgrtr_el2(read_el2_ctx_fgt(ctx, hdfgrtr_el2));
  1005. if (is_feat_amu_supported()) {
  1006. write_hafgrtr_el2(read_el2_ctx_fgt(ctx, hafgrtr_el2));
  1007. }
  1008. write_hdfgwtr_el2(read_el2_ctx_fgt(ctx, hdfgwtr_el2));
  1009. write_hfgitr_el2(read_el2_ctx_fgt(ctx, hfgitr_el2));
  1010. write_hfgrtr_el2(read_el2_ctx_fgt(ctx, hfgrtr_el2));
  1011. write_hfgwtr_el2(read_el2_ctx_fgt(ctx, hfgwtr_el2));
  1012. }
  1013. static void el2_sysregs_context_save_fgt2(el2_sysregs_t *ctx)
  1014. {
  1015. write_el2_ctx_fgt2(ctx, hdfgrtr2_el2, read_hdfgrtr2_el2());
  1016. write_el2_ctx_fgt2(ctx, hdfgwtr2_el2, read_hdfgwtr2_el2());
  1017. write_el2_ctx_fgt2(ctx, hfgitr2_el2, read_hfgitr2_el2());
  1018. write_el2_ctx_fgt2(ctx, hfgrtr2_el2, read_hfgrtr2_el2());
  1019. write_el2_ctx_fgt2(ctx, hfgwtr2_el2, read_hfgwtr2_el2());
  1020. }
  1021. static void el2_sysregs_context_restore_fgt2(el2_sysregs_t *ctx)
  1022. {
  1023. write_hdfgrtr2_el2(read_el2_ctx_fgt2(ctx, hdfgrtr2_el2));
  1024. write_hdfgwtr2_el2(read_el2_ctx_fgt2(ctx, hdfgwtr2_el2));
  1025. write_hfgitr2_el2(read_el2_ctx_fgt2(ctx, hfgitr2_el2));
  1026. write_hfgrtr2_el2(read_el2_ctx_fgt2(ctx, hfgrtr2_el2));
  1027. write_hfgwtr2_el2(read_el2_ctx_fgt2(ctx, hfgwtr2_el2));
  1028. }
  1029. static void el2_sysregs_context_save_mpam(el2_sysregs_t *ctx)
  1030. {
  1031. u_register_t mpam_idr = read_mpamidr_el1();
  1032. write_el2_ctx_mpam(ctx, mpam2_el2, read_mpam2_el2());
  1033. /*
  1034. * The context registers that we intend to save would be part of the
  1035. * PE's system register frame only if MPAMIDR_EL1.HAS_HCR == 1.
  1036. */
  1037. if ((mpam_idr & MPAMIDR_HAS_HCR_BIT) == 0U) {
  1038. return;
  1039. }
  1040. /*
  1041. * MPAMHCR_EL2, MPAMVPMV_EL2 and MPAMVPM0_EL2 are always present if
  1042. * MPAMIDR_HAS_HCR_BIT == 1.
  1043. */
  1044. write_el2_ctx_mpam(ctx, mpamhcr_el2, read_mpamhcr_el2());
  1045. write_el2_ctx_mpam(ctx, mpamvpm0_el2, read_mpamvpm0_el2());
  1046. write_el2_ctx_mpam(ctx, mpamvpmv_el2, read_mpamvpmv_el2());
  1047. /*
  1048. * The number of MPAMVPM registers is implementation defined, their
  1049. * number is stored in the MPAMIDR_EL1 register.
  1050. */
  1051. switch ((mpam_idr >> MPAMIDR_EL1_VPMR_MAX_SHIFT) & MPAMIDR_EL1_VPMR_MAX_MASK) {
  1052. case 7:
  1053. write_el2_ctx_mpam(ctx, mpamvpm7_el2, read_mpamvpm7_el2());
  1054. __fallthrough;
  1055. case 6:
  1056. write_el2_ctx_mpam(ctx, mpamvpm6_el2, read_mpamvpm6_el2());
  1057. __fallthrough;
  1058. case 5:
  1059. write_el2_ctx_mpam(ctx, mpamvpm5_el2, read_mpamvpm5_el2());
  1060. __fallthrough;
  1061. case 4:
  1062. write_el2_ctx_mpam(ctx, mpamvpm4_el2, read_mpamvpm4_el2());
  1063. __fallthrough;
  1064. case 3:
  1065. write_el2_ctx_mpam(ctx, mpamvpm3_el2, read_mpamvpm3_el2());
  1066. __fallthrough;
  1067. case 2:
  1068. write_el2_ctx_mpam(ctx, mpamvpm2_el2, read_mpamvpm2_el2());
  1069. __fallthrough;
  1070. case 1:
  1071. write_el2_ctx_mpam(ctx, mpamvpm1_el2, read_mpamvpm1_el2());
  1072. break;
  1073. }
  1074. }
  1075. static void el2_sysregs_context_restore_mpam(el2_sysregs_t *ctx)
  1076. {
  1077. u_register_t mpam_idr = read_mpamidr_el1();
  1078. write_mpam2_el2(read_el2_ctx_mpam(ctx, mpam2_el2));
  1079. if ((mpam_idr & MPAMIDR_HAS_HCR_BIT) == 0U) {
  1080. return;
  1081. }
  1082. write_mpamhcr_el2(read_el2_ctx_mpam(ctx, mpamhcr_el2));
  1083. write_mpamvpm0_el2(read_el2_ctx_mpam(ctx, mpamvpm0_el2));
  1084. write_mpamvpmv_el2(read_el2_ctx_mpam(ctx, mpamvpmv_el2));
  1085. switch ((mpam_idr >> MPAMIDR_EL1_VPMR_MAX_SHIFT) & MPAMIDR_EL1_VPMR_MAX_MASK) {
  1086. case 7:
  1087. write_mpamvpm7_el2(read_el2_ctx_mpam(ctx, mpamvpm7_el2));
  1088. __fallthrough;
  1089. case 6:
  1090. write_mpamvpm6_el2(read_el2_ctx_mpam(ctx, mpamvpm6_el2));
  1091. __fallthrough;
  1092. case 5:
  1093. write_mpamvpm5_el2(read_el2_ctx_mpam(ctx, mpamvpm5_el2));
  1094. __fallthrough;
  1095. case 4:
  1096. write_mpamvpm4_el2(read_el2_ctx_mpam(ctx, mpamvpm4_el2));
  1097. __fallthrough;
  1098. case 3:
  1099. write_mpamvpm3_el2(read_el2_ctx_mpam(ctx, mpamvpm3_el2));
  1100. __fallthrough;
  1101. case 2:
  1102. write_mpamvpm2_el2(read_el2_ctx_mpam(ctx, mpamvpm2_el2));
  1103. __fallthrough;
  1104. case 1:
  1105. write_mpamvpm1_el2(read_el2_ctx_mpam(ctx, mpamvpm1_el2));
  1106. break;
  1107. }
  1108. }
  1109. /* ---------------------------------------------------------------------------
  1110. * The following registers are not added:
  1111. * ICH_AP0R<n>_EL2
  1112. * ICH_AP1R<n>_EL2
  1113. * ICH_LR<n>_EL2
  1114. *
  1115. * NOTE: For a system with S-EL2 present but not enabled, accessing
  1116. * ICC_SRE_EL2 is undefined from EL3. To workaround this change the
  1117. * SCR_EL3.NS = 1 before accessing this register.
  1118. * ---------------------------------------------------------------------------
  1119. */
  1120. static void el2_sysregs_context_save_gic(el2_sysregs_t *ctx)
  1121. {
  1122. #if defined(SPD_spmd) && SPMD_SPM_AT_SEL2
  1123. write_el2_ctx_common(ctx, icc_sre_el2, read_icc_sre_el2());
  1124. #else
  1125. u_register_t scr_el3 = read_scr_el3();
  1126. write_scr_el3(scr_el3 | SCR_NS_BIT);
  1127. isb();
  1128. write_el2_ctx_common(ctx, icc_sre_el2, read_icc_sre_el2());
  1129. write_scr_el3(scr_el3);
  1130. isb();
  1131. #endif
  1132. write_el2_ctx_common(ctx, ich_hcr_el2, read_ich_hcr_el2());
  1133. write_el2_ctx_common(ctx, ich_vmcr_el2, read_ich_vmcr_el2());
  1134. }
  1135. static void el2_sysregs_context_restore_gic(el2_sysregs_t *ctx)
  1136. {
  1137. #if defined(SPD_spmd) && SPMD_SPM_AT_SEL2
  1138. write_icc_sre_el2(read_el2_ctx_common(ctx, icc_sre_el2));
  1139. #else
  1140. u_register_t scr_el3 = read_scr_el3();
  1141. write_scr_el3(scr_el3 | SCR_NS_BIT);
  1142. isb();
  1143. write_icc_sre_el2(read_el2_ctx_common(ctx, icc_sre_el2));
  1144. write_scr_el3(scr_el3);
  1145. isb();
  1146. #endif
  1147. write_ich_hcr_el2(read_el2_ctx_common(ctx, ich_hcr_el2));
  1148. write_ich_vmcr_el2(read_el2_ctx_common(ctx, ich_vmcr_el2));
  1149. }
  1150. /* -----------------------------------------------------
  1151. * The following registers are not added:
  1152. * AMEVCNTVOFF0<n>_EL2
  1153. * AMEVCNTVOFF1<n>_EL2
  1154. * -----------------------------------------------------
  1155. */
  1156. static void el2_sysregs_context_save_common(el2_sysregs_t *ctx)
  1157. {
  1158. write_el2_ctx_common(ctx, actlr_el2, read_actlr_el2());
  1159. write_el2_ctx_common(ctx, afsr0_el2, read_afsr0_el2());
  1160. write_el2_ctx_common(ctx, afsr1_el2, read_afsr1_el2());
  1161. write_el2_ctx_common(ctx, amair_el2, read_amair_el2());
  1162. write_el2_ctx_common(ctx, cnthctl_el2, read_cnthctl_el2());
  1163. write_el2_ctx_common(ctx, cntvoff_el2, read_cntvoff_el2());
  1164. write_el2_ctx_common(ctx, cptr_el2, read_cptr_el2());
  1165. if (CTX_INCLUDE_AARCH32_REGS) {
  1166. write_el2_ctx_common(ctx, dbgvcr32_el2, read_dbgvcr32_el2());
  1167. }
  1168. write_el2_ctx_common(ctx, elr_el2, read_elr_el2());
  1169. write_el2_ctx_common(ctx, esr_el2, read_esr_el2());
  1170. write_el2_ctx_common(ctx, far_el2, read_far_el2());
  1171. write_el2_ctx_common(ctx, hacr_el2, read_hacr_el2());
  1172. write_el2_ctx_common(ctx, hcr_el2, read_hcr_el2());
  1173. write_el2_ctx_common(ctx, hpfar_el2, read_hpfar_el2());
  1174. write_el2_ctx_common(ctx, hstr_el2, read_hstr_el2());
  1175. write_el2_ctx_common(ctx, mair_el2, read_mair_el2());
  1176. write_el2_ctx_common(ctx, mdcr_el2, read_mdcr_el2());
  1177. write_el2_ctx_common(ctx, sctlr_el2, read_sctlr_el2());
  1178. write_el2_ctx_common(ctx, spsr_el2, read_spsr_el2());
  1179. write_el2_ctx_common(ctx, sp_el2, read_sp_el2());
  1180. write_el2_ctx_common(ctx, tcr_el2, read_tcr_el2());
  1181. write_el2_ctx_common(ctx, tpidr_el2, read_tpidr_el2());
  1182. write_el2_ctx_common(ctx, vbar_el2, read_vbar_el2());
  1183. write_el2_ctx_common(ctx, vmpidr_el2, read_vmpidr_el2());
  1184. write_el2_ctx_common(ctx, vpidr_el2, read_vpidr_el2());
  1185. write_el2_ctx_common(ctx, vtcr_el2, read_vtcr_el2());
  1186. write_el2_ctx_sysreg128(ctx, ttbr0_el2, read_ttbr0_el2());
  1187. write_el2_ctx_sysreg128(ctx, vttbr_el2, read_vttbr_el2());
  1188. }
  1189. static void el2_sysregs_context_restore_common(el2_sysregs_t *ctx)
  1190. {
  1191. write_actlr_el2(read_el2_ctx_common(ctx, actlr_el2));
  1192. write_afsr0_el2(read_el2_ctx_common(ctx, afsr0_el2));
  1193. write_afsr1_el2(read_el2_ctx_common(ctx, afsr1_el2));
  1194. write_amair_el2(read_el2_ctx_common(ctx, amair_el2));
  1195. write_cnthctl_el2(read_el2_ctx_common(ctx, cnthctl_el2));
  1196. write_cntvoff_el2(read_el2_ctx_common(ctx, cntvoff_el2));
  1197. write_cptr_el2(read_el2_ctx_common(ctx, cptr_el2));
  1198. if (CTX_INCLUDE_AARCH32_REGS) {
  1199. write_dbgvcr32_el2(read_el2_ctx_common(ctx, dbgvcr32_el2));
  1200. }
  1201. write_elr_el2(read_el2_ctx_common(ctx, elr_el2));
  1202. write_esr_el2(read_el2_ctx_common(ctx, esr_el2));
  1203. write_far_el2(read_el2_ctx_common(ctx, far_el2));
  1204. write_hacr_el2(read_el2_ctx_common(ctx, hacr_el2));
  1205. write_hcr_el2(read_el2_ctx_common(ctx, hcr_el2));
  1206. write_hpfar_el2(read_el2_ctx_common(ctx, hpfar_el2));
  1207. write_hstr_el2(read_el2_ctx_common(ctx, hstr_el2));
  1208. write_mair_el2(read_el2_ctx_common(ctx, mair_el2));
  1209. write_mdcr_el2(read_el2_ctx_common(ctx, mdcr_el2));
  1210. write_sctlr_el2(read_el2_ctx_common(ctx, sctlr_el2));
  1211. write_spsr_el2(read_el2_ctx_common(ctx, spsr_el2));
  1212. write_sp_el2(read_el2_ctx_common(ctx, sp_el2));
  1213. write_tcr_el2(read_el2_ctx_common(ctx, tcr_el2));
  1214. write_tpidr_el2(read_el2_ctx_common(ctx, tpidr_el2));
  1215. write_ttbr0_el2(read_el2_ctx_common(ctx, ttbr0_el2));
  1216. write_vbar_el2(read_el2_ctx_common(ctx, vbar_el2));
  1217. write_vmpidr_el2(read_el2_ctx_common(ctx, vmpidr_el2));
  1218. write_vpidr_el2(read_el2_ctx_common(ctx, vpidr_el2));
  1219. write_vtcr_el2(read_el2_ctx_common(ctx, vtcr_el2));
  1220. write_vttbr_el2(read_el2_ctx_common(ctx, vttbr_el2));
  1221. }
  1222. /*******************************************************************************
  1223. * Save EL2 sysreg context
  1224. ******************************************************************************/
  1225. void cm_el2_sysregs_context_save(uint32_t security_state)
  1226. {
  1227. cpu_context_t *ctx;
  1228. el2_sysregs_t *el2_sysregs_ctx;
  1229. ctx = cm_get_context(security_state);
  1230. assert(ctx != NULL);
  1231. el2_sysregs_ctx = get_el2_sysregs_ctx(ctx);
  1232. el2_sysregs_context_save_common(el2_sysregs_ctx);
  1233. el2_sysregs_context_save_gic(el2_sysregs_ctx);
  1234. if (is_feat_mte2_supported()) {
  1235. write_el2_ctx_mte2(el2_sysregs_ctx, tfsr_el2, read_tfsr_el2());
  1236. }
  1237. if (is_feat_mpam_supported()) {
  1238. el2_sysregs_context_save_mpam(el2_sysregs_ctx);
  1239. }
  1240. if (is_feat_fgt_supported()) {
  1241. el2_sysregs_context_save_fgt(el2_sysregs_ctx);
  1242. }
  1243. if (is_feat_fgt2_supported()) {
  1244. el2_sysregs_context_save_fgt2(el2_sysregs_ctx);
  1245. }
  1246. if (is_feat_ecv_v2_supported()) {
  1247. write_el2_ctx_ecv(el2_sysregs_ctx, cntpoff_el2, read_cntpoff_el2());
  1248. }
  1249. if (is_feat_vhe_supported()) {
  1250. write_el2_ctx_vhe(el2_sysregs_ctx, contextidr_el2,
  1251. read_contextidr_el2());
  1252. write_el2_ctx_vhe_sysreg128(el2_sysregs_ctx, ttbr1_el2, read_ttbr1_el2());
  1253. }
  1254. if (is_feat_ras_supported()) {
  1255. write_el2_ctx_ras(el2_sysregs_ctx, vdisr_el2, read_vdisr_el2());
  1256. write_el2_ctx_ras(el2_sysregs_ctx, vsesr_el2, read_vsesr_el2());
  1257. }
  1258. if (is_feat_nv2_supported()) {
  1259. write_el2_ctx_neve(el2_sysregs_ctx, vncr_el2, read_vncr_el2());
  1260. }
  1261. if (is_feat_trf_supported()) {
  1262. write_el2_ctx_trf(el2_sysregs_ctx, trfcr_el2, read_trfcr_el2());
  1263. }
  1264. if (is_feat_csv2_2_supported()) {
  1265. write_el2_ctx_csv2_2(el2_sysregs_ctx, scxtnum_el2,
  1266. read_scxtnum_el2());
  1267. }
  1268. if (is_feat_hcx_supported()) {
  1269. write_el2_ctx_hcx(el2_sysregs_ctx, hcrx_el2, read_hcrx_el2());
  1270. }
  1271. if (is_feat_tcr2_supported()) {
  1272. write_el2_ctx_tcr2(el2_sysregs_ctx, tcr2_el2, read_tcr2_el2());
  1273. }
  1274. if (is_feat_sxpie_supported()) {
  1275. write_el2_ctx_sxpie(el2_sysregs_ctx, pire0_el2, read_pire0_el2());
  1276. write_el2_ctx_sxpie(el2_sysregs_ctx, pir_el2, read_pir_el2());
  1277. }
  1278. if (is_feat_sxpoe_supported()) {
  1279. write_el2_ctx_sxpoe(el2_sysregs_ctx, por_el2, read_por_el2());
  1280. }
  1281. if (is_feat_s2pie_supported()) {
  1282. write_el2_ctx_s2pie(el2_sysregs_ctx, s2pir_el2, read_s2pir_el2());
  1283. }
  1284. if (is_feat_gcs_supported()) {
  1285. write_el2_ctx_gcs(el2_sysregs_ctx, gcscr_el2, read_gcscr_el2());
  1286. write_el2_ctx_gcs(el2_sysregs_ctx, gcspr_el2, read_gcspr_el2());
  1287. }
  1288. if (is_feat_sctlr2_supported()) {
  1289. write_el2_ctx_sctlr2(el2_sysregs_ctx, sctlr2_el2, read_sctlr2_el2());
  1290. }
  1291. }
  1292. /*******************************************************************************
  1293. * Restore EL2 sysreg context
  1294. ******************************************************************************/
  1295. void cm_el2_sysregs_context_restore(uint32_t security_state)
  1296. {
  1297. cpu_context_t *ctx;
  1298. el2_sysregs_t *el2_sysregs_ctx;
  1299. ctx = cm_get_context(security_state);
  1300. assert(ctx != NULL);
  1301. el2_sysregs_ctx = get_el2_sysregs_ctx(ctx);
  1302. el2_sysregs_context_restore_common(el2_sysregs_ctx);
  1303. el2_sysregs_context_restore_gic(el2_sysregs_ctx);
  1304. if (is_feat_mte2_supported()) {
  1305. write_tfsr_el2(read_el2_ctx_mte2(el2_sysregs_ctx, tfsr_el2));
  1306. }
  1307. if (is_feat_mpam_supported()) {
  1308. el2_sysregs_context_restore_mpam(el2_sysregs_ctx);
  1309. }
  1310. if (is_feat_fgt_supported()) {
  1311. el2_sysregs_context_restore_fgt(el2_sysregs_ctx);
  1312. }
  1313. if (is_feat_fgt2_supported()) {
  1314. el2_sysregs_context_restore_fgt2(el2_sysregs_ctx);
  1315. }
  1316. if (is_feat_ecv_v2_supported()) {
  1317. write_cntpoff_el2(read_el2_ctx_ecv(el2_sysregs_ctx, cntpoff_el2));
  1318. }
  1319. if (is_feat_vhe_supported()) {
  1320. write_contextidr_el2(read_el2_ctx_vhe(el2_sysregs_ctx,
  1321. contextidr_el2));
  1322. write_ttbr1_el2(read_el2_ctx_vhe(el2_sysregs_ctx, ttbr1_el2));
  1323. }
  1324. if (is_feat_ras_supported()) {
  1325. write_vdisr_el2(read_el2_ctx_ras(el2_sysregs_ctx, vdisr_el2));
  1326. write_vsesr_el2(read_el2_ctx_ras(el2_sysregs_ctx, vsesr_el2));
  1327. }
  1328. if (is_feat_nv2_supported()) {
  1329. write_vncr_el2(read_el2_ctx_neve(el2_sysregs_ctx, vncr_el2));
  1330. }
  1331. if (is_feat_trf_supported()) {
  1332. write_trfcr_el2(read_el2_ctx_trf(el2_sysregs_ctx, trfcr_el2));
  1333. }
  1334. if (is_feat_csv2_2_supported()) {
  1335. write_scxtnum_el2(read_el2_ctx_csv2_2(el2_sysregs_ctx,
  1336. scxtnum_el2));
  1337. }
  1338. if (is_feat_hcx_supported()) {
  1339. write_hcrx_el2(read_el2_ctx_hcx(el2_sysregs_ctx, hcrx_el2));
  1340. }
  1341. if (is_feat_tcr2_supported()) {
  1342. write_tcr2_el2(read_el2_ctx_tcr2(el2_sysregs_ctx, tcr2_el2));
  1343. }
  1344. if (is_feat_sxpie_supported()) {
  1345. write_pire0_el2(read_el2_ctx_sxpie(el2_sysregs_ctx, pire0_el2));
  1346. write_pir_el2(read_el2_ctx_sxpie(el2_sysregs_ctx, pir_el2));
  1347. }
  1348. if (is_feat_sxpoe_supported()) {
  1349. write_por_el2(read_el2_ctx_sxpoe(el2_sysregs_ctx, por_el2));
  1350. }
  1351. if (is_feat_s2pie_supported()) {
  1352. write_s2pir_el2(read_el2_ctx_s2pie(el2_sysregs_ctx, s2pir_el2));
  1353. }
  1354. if (is_feat_gcs_supported()) {
  1355. write_gcscr_el2(read_el2_ctx_gcs(el2_sysregs_ctx, gcscr_el2));
  1356. write_gcspr_el2(read_el2_ctx_gcs(el2_sysregs_ctx, gcspr_el2));
  1357. }
  1358. if (is_feat_sctlr2_supported()) {
  1359. write_sctlr2_el2(read_el2_ctx_sctlr2(el2_sysregs_ctx, sctlr2_el2));
  1360. }
  1361. }
  1362. #endif /* (CTX_INCLUDE_EL2_REGS && IMAGE_BL31) */
  1363. #if IMAGE_BL31
  1364. /*********************************************************************************
  1365. * This function allows Architecture features asymmetry among cores.
  1366. * TF-A assumes that all the cores in the platform has architecture feature parity
  1367. * and hence the context is setup on different core (e.g. primary sets up the
  1368. * context for secondary cores).This assumption may not be true for systems where
  1369. * cores are not conforming to same Arch version or there is CPU Erratum which
  1370. * requires certain feature to be be disabled only on a given core.
  1371. *
  1372. * This function is called on secondary cores to override any disparity in context
  1373. * setup by primary, this would be called during warmboot path.
  1374. *********************************************************************************/
  1375. void cm_handle_asymmetric_features(void)
  1376. {
  1377. cpu_context_t *ctx __maybe_unused = cm_get_context(NON_SECURE);
  1378. assert(ctx != NULL);
  1379. #if ENABLE_SPE_FOR_NS == FEAT_STATE_CHECK_ASYMMETRIC
  1380. if (is_feat_spe_supported()) {
  1381. spe_enable(ctx);
  1382. } else {
  1383. spe_disable(ctx);
  1384. }
  1385. #endif
  1386. #if ERRATA_A520_2938996 || ERRATA_X4_2726228
  1387. if (check_if_affected_core() == ERRATA_APPLIES) {
  1388. if (is_feat_trbe_supported()) {
  1389. trbe_disable(ctx);
  1390. }
  1391. }
  1392. #endif
  1393. #if ENABLE_FEAT_TCR2 == FEAT_STATE_CHECK_ASYMMETRIC
  1394. el3_state_t *el3_state = get_el3state_ctx(ctx);
  1395. u_register_t spsr = read_ctx_reg(el3_state, CTX_SPSR_EL3);
  1396. if (is_feat_tcr2_supported() && (GET_RW(spsr) == MODE_RW_64)) {
  1397. tcr2_enable(ctx);
  1398. } else {
  1399. tcr2_disable(ctx);
  1400. }
  1401. #endif
  1402. }
  1403. #endif
  1404. /*******************************************************************************
  1405. * This function is used to exit to Non-secure world. If CTX_INCLUDE_EL2_REGS
  1406. * is enabled, it restores EL1 and EL2 sysreg contexts instead of directly
  1407. * updating EL1 and EL2 registers. Otherwise, it calls the generic
  1408. * cm_prepare_el3_exit function.
  1409. ******************************************************************************/
  1410. void cm_prepare_el3_exit_ns(void)
  1411. {
  1412. #if IMAGE_BL31
  1413. /*
  1414. * Check and handle Architecture feature asymmetry among cores.
  1415. *
  1416. * In warmboot path secondary cores context is initialized on core which
  1417. * did CPU_ON SMC call, if there is feature asymmetry in these cores handle
  1418. * it in this function call.
  1419. * For Symmetric cores this is an empty function.
  1420. */
  1421. cm_handle_asymmetric_features();
  1422. #endif
  1423. #if (CTX_INCLUDE_EL2_REGS && IMAGE_BL31)
  1424. #if ENABLE_ASSERTIONS
  1425. cpu_context_t *ctx = cm_get_context(NON_SECURE);
  1426. assert(ctx != NULL);
  1427. /* Assert that EL2 is used. */
  1428. u_register_t scr_el3 = read_ctx_reg(get_el3state_ctx(ctx), CTX_SCR_EL3);
  1429. assert(((scr_el3 & SCR_HCE_BIT) != 0UL) &&
  1430. (el_implemented(2U) != EL_IMPL_NONE));
  1431. #endif /* ENABLE_ASSERTIONS */
  1432. /* Restore EL2 sysreg contexts */
  1433. cm_el2_sysregs_context_restore(NON_SECURE);
  1434. cm_set_next_eret_context(NON_SECURE);
  1435. #else
  1436. cm_prepare_el3_exit(NON_SECURE);
  1437. #endif /* (CTX_INCLUDE_EL2_REGS && IMAGE_BL31) */
  1438. }
  1439. #if ((IMAGE_BL1) || (IMAGE_BL31 && (!CTX_INCLUDE_EL2_REGS)))
  1440. /*******************************************************************************
  1441. * The next set of six functions are used by runtime services to save and restore
  1442. * EL1 context on the 'cpu_context' structure for the specified security state.
  1443. ******************************************************************************/
  1444. static void el1_sysregs_context_save(el1_sysregs_t *ctx)
  1445. {
  1446. write_el1_ctx_common(ctx, spsr_el1, read_spsr_el1());
  1447. write_el1_ctx_common(ctx, elr_el1, read_elr_el1());
  1448. #if (!ERRATA_SPECULATIVE_AT)
  1449. write_el1_ctx_common(ctx, sctlr_el1, read_sctlr_el1());
  1450. write_el1_ctx_common(ctx, tcr_el1, read_tcr_el1());
  1451. #endif /* (!ERRATA_SPECULATIVE_AT) */
  1452. write_el1_ctx_common(ctx, cpacr_el1, read_cpacr_el1());
  1453. write_el1_ctx_common(ctx, csselr_el1, read_csselr_el1());
  1454. write_el1_ctx_common(ctx, sp_el1, read_sp_el1());
  1455. write_el1_ctx_common(ctx, esr_el1, read_esr_el1());
  1456. write_el1_ctx_common(ctx, ttbr0_el1, read_ttbr0_el1());
  1457. write_el1_ctx_common(ctx, ttbr1_el1, read_ttbr1_el1());
  1458. write_el1_ctx_common(ctx, mair_el1, read_mair_el1());
  1459. write_el1_ctx_common(ctx, amair_el1, read_amair_el1());
  1460. write_el1_ctx_common(ctx, actlr_el1, read_actlr_el1());
  1461. write_el1_ctx_common(ctx, tpidr_el1, read_tpidr_el1());
  1462. write_el1_ctx_common(ctx, tpidr_el0, read_tpidr_el0());
  1463. write_el1_ctx_common(ctx, tpidrro_el0, read_tpidrro_el0());
  1464. write_el1_ctx_common(ctx, par_el1, read_par_el1());
  1465. write_el1_ctx_common(ctx, far_el1, read_far_el1());
  1466. write_el1_ctx_common(ctx, afsr0_el1, read_afsr0_el1());
  1467. write_el1_ctx_common(ctx, afsr1_el1, read_afsr1_el1());
  1468. write_el1_ctx_common(ctx, contextidr_el1, read_contextidr_el1());
  1469. write_el1_ctx_common(ctx, vbar_el1, read_vbar_el1());
  1470. write_el1_ctx_common(ctx, mdccint_el1, read_mdccint_el1());
  1471. write_el1_ctx_common(ctx, mdscr_el1, read_mdscr_el1());
  1472. if (CTX_INCLUDE_AARCH32_REGS) {
  1473. /* Save Aarch32 registers */
  1474. write_el1_ctx_aarch32(ctx, spsr_abt, read_spsr_abt());
  1475. write_el1_ctx_aarch32(ctx, spsr_und, read_spsr_und());
  1476. write_el1_ctx_aarch32(ctx, spsr_irq, read_spsr_irq());
  1477. write_el1_ctx_aarch32(ctx, spsr_fiq, read_spsr_fiq());
  1478. write_el1_ctx_aarch32(ctx, dacr32_el2, read_dacr32_el2());
  1479. write_el1_ctx_aarch32(ctx, ifsr32_el2, read_ifsr32_el2());
  1480. }
  1481. if (NS_TIMER_SWITCH) {
  1482. /* Save NS Timer registers */
  1483. write_el1_ctx_arch_timer(ctx, cntp_ctl_el0, read_cntp_ctl_el0());
  1484. write_el1_ctx_arch_timer(ctx, cntp_cval_el0, read_cntp_cval_el0());
  1485. write_el1_ctx_arch_timer(ctx, cntv_ctl_el0, read_cntv_ctl_el0());
  1486. write_el1_ctx_arch_timer(ctx, cntv_cval_el0, read_cntv_cval_el0());
  1487. write_el1_ctx_arch_timer(ctx, cntkctl_el1, read_cntkctl_el1());
  1488. }
  1489. if (is_feat_mte2_supported()) {
  1490. write_el1_ctx_mte2(ctx, tfsre0_el1, read_tfsre0_el1());
  1491. write_el1_ctx_mte2(ctx, tfsr_el1, read_tfsr_el1());
  1492. write_el1_ctx_mte2(ctx, rgsr_el1, read_rgsr_el1());
  1493. write_el1_ctx_mte2(ctx, gcr_el1, read_gcr_el1());
  1494. }
  1495. if (is_feat_ras_supported()) {
  1496. write_el1_ctx_ras(ctx, disr_el1, read_disr_el1());
  1497. }
  1498. if (is_feat_s1pie_supported()) {
  1499. write_el1_ctx_s1pie(ctx, pire0_el1, read_pire0_el1());
  1500. write_el1_ctx_s1pie(ctx, pir_el1, read_pir_el1());
  1501. }
  1502. if (is_feat_s1poe_supported()) {
  1503. write_el1_ctx_s1poe(ctx, por_el1, read_por_el1());
  1504. }
  1505. if (is_feat_s2poe_supported()) {
  1506. write_el1_ctx_s2poe(ctx, s2por_el1, read_s2por_el1());
  1507. }
  1508. if (is_feat_tcr2_supported()) {
  1509. write_el1_ctx_tcr2(ctx, tcr2_el1, read_tcr2_el1());
  1510. }
  1511. if (is_feat_trf_supported()) {
  1512. write_el1_ctx_trf(ctx, trfcr_el1, read_trfcr_el1());
  1513. }
  1514. if (is_feat_csv2_2_supported()) {
  1515. write_el1_ctx_csv2_2(ctx, scxtnum_el0, read_scxtnum_el0());
  1516. write_el1_ctx_csv2_2(ctx, scxtnum_el1, read_scxtnum_el1());
  1517. }
  1518. if (is_feat_gcs_supported()) {
  1519. write_el1_ctx_gcs(ctx, gcscr_el1, read_gcscr_el1());
  1520. write_el1_ctx_gcs(ctx, gcscre0_el1, read_gcscre0_el1());
  1521. write_el1_ctx_gcs(ctx, gcspr_el1, read_gcspr_el1());
  1522. write_el1_ctx_gcs(ctx, gcspr_el0, read_gcspr_el0());
  1523. }
  1524. if (is_feat_the_supported()) {
  1525. write_el1_ctx_the(ctx, rcwmask_el1, read_rcwmask_el1());
  1526. write_el1_ctx_the(ctx, rcwsmask_el1, read_rcwsmask_el1());
  1527. }
  1528. if (is_feat_sctlr2_supported()) {
  1529. write_el1_ctx_sctlr2(ctx, sctlr2_el1, read_sctlr2_el1());
  1530. }
  1531. if (is_feat_ls64_accdata_supported()) {
  1532. write_el1_ctx_ls64(ctx, accdata_el1, read_accdata_el1());
  1533. }
  1534. }
  1535. static void el1_sysregs_context_restore(el1_sysregs_t *ctx)
  1536. {
  1537. write_spsr_el1(read_el1_ctx_common(ctx, spsr_el1));
  1538. write_elr_el1(read_el1_ctx_common(ctx, elr_el1));
  1539. #if (!ERRATA_SPECULATIVE_AT)
  1540. write_sctlr_el1(read_el1_ctx_common(ctx, sctlr_el1));
  1541. write_tcr_el1(read_el1_ctx_common(ctx, tcr_el1));
  1542. #endif /* (!ERRATA_SPECULATIVE_AT) */
  1543. write_cpacr_el1(read_el1_ctx_common(ctx, cpacr_el1));
  1544. write_csselr_el1(read_el1_ctx_common(ctx, csselr_el1));
  1545. write_sp_el1(read_el1_ctx_common(ctx, sp_el1));
  1546. write_esr_el1(read_el1_ctx_common(ctx, esr_el1));
  1547. write_ttbr0_el1(read_el1_ctx_common(ctx, ttbr0_el1));
  1548. write_ttbr1_el1(read_el1_ctx_common(ctx, ttbr1_el1));
  1549. write_mair_el1(read_el1_ctx_common(ctx, mair_el1));
  1550. write_amair_el1(read_el1_ctx_common(ctx, amair_el1));
  1551. write_actlr_el1(read_el1_ctx_common(ctx, actlr_el1));
  1552. write_tpidr_el1(read_el1_ctx_common(ctx, tpidr_el1));
  1553. write_tpidr_el0(read_el1_ctx_common(ctx, tpidr_el0));
  1554. write_tpidrro_el0(read_el1_ctx_common(ctx, tpidrro_el0));
  1555. write_par_el1(read_el1_ctx_common(ctx, par_el1));
  1556. write_far_el1(read_el1_ctx_common(ctx, far_el1));
  1557. write_afsr0_el1(read_el1_ctx_common(ctx, afsr0_el1));
  1558. write_afsr1_el1(read_el1_ctx_common(ctx, afsr1_el1));
  1559. write_contextidr_el1(read_el1_ctx_common(ctx, contextidr_el1));
  1560. write_vbar_el1(read_el1_ctx_common(ctx, vbar_el1));
  1561. write_mdccint_el1(read_el1_ctx_common(ctx, mdccint_el1));
  1562. write_mdscr_el1(read_el1_ctx_common(ctx, mdscr_el1));
  1563. if (CTX_INCLUDE_AARCH32_REGS) {
  1564. /* Restore Aarch32 registers */
  1565. write_spsr_abt(read_el1_ctx_aarch32(ctx, spsr_abt));
  1566. write_spsr_und(read_el1_ctx_aarch32(ctx, spsr_und));
  1567. write_spsr_irq(read_el1_ctx_aarch32(ctx, spsr_irq));
  1568. write_spsr_fiq(read_el1_ctx_aarch32(ctx, spsr_fiq));
  1569. write_dacr32_el2(read_el1_ctx_aarch32(ctx, dacr32_el2));
  1570. write_ifsr32_el2(read_el1_ctx_aarch32(ctx, ifsr32_el2));
  1571. }
  1572. if (NS_TIMER_SWITCH) {
  1573. /* Restore NS Timer registers */
  1574. write_cntp_ctl_el0(read_el1_ctx_arch_timer(ctx, cntp_ctl_el0));
  1575. write_cntp_cval_el0(read_el1_ctx_arch_timer(ctx, cntp_cval_el0));
  1576. write_cntv_ctl_el0(read_el1_ctx_arch_timer(ctx, cntv_ctl_el0));
  1577. write_cntv_cval_el0(read_el1_ctx_arch_timer(ctx, cntv_cval_el0));
  1578. write_cntkctl_el1(read_el1_ctx_arch_timer(ctx, cntkctl_el1));
  1579. }
  1580. if (is_feat_mte2_supported()) {
  1581. write_tfsre0_el1(read_el1_ctx_mte2(ctx, tfsre0_el1));
  1582. write_tfsr_el1(read_el1_ctx_mte2(ctx, tfsr_el1));
  1583. write_rgsr_el1(read_el1_ctx_mte2(ctx, rgsr_el1));
  1584. write_gcr_el1(read_el1_ctx_mte2(ctx, gcr_el1));
  1585. }
  1586. if (is_feat_ras_supported()) {
  1587. write_disr_el1(read_el1_ctx_ras(ctx, disr_el1));
  1588. }
  1589. if (is_feat_s1pie_supported()) {
  1590. write_pire0_el1(read_el1_ctx_s1pie(ctx, pire0_el1));
  1591. write_pir_el1(read_el1_ctx_s1pie(ctx, pir_el1));
  1592. }
  1593. if (is_feat_s1poe_supported()) {
  1594. write_por_el1(read_el1_ctx_s1poe(ctx, por_el1));
  1595. }
  1596. if (is_feat_s2poe_supported()) {
  1597. write_s2por_el1(read_el1_ctx_s2poe(ctx, s2por_el1));
  1598. }
  1599. if (is_feat_tcr2_supported()) {
  1600. write_tcr2_el1(read_el1_ctx_tcr2(ctx, tcr2_el1));
  1601. }
  1602. if (is_feat_trf_supported()) {
  1603. write_trfcr_el1(read_el1_ctx_trf(ctx, trfcr_el1));
  1604. }
  1605. if (is_feat_csv2_2_supported()) {
  1606. write_scxtnum_el0(read_el1_ctx_csv2_2(ctx, scxtnum_el0));
  1607. write_scxtnum_el1(read_el1_ctx_csv2_2(ctx, scxtnum_el1));
  1608. }
  1609. if (is_feat_gcs_supported()) {
  1610. write_gcscr_el1(read_el1_ctx_gcs(ctx, gcscr_el1));
  1611. write_gcscre0_el1(read_el1_ctx_gcs(ctx, gcscre0_el1));
  1612. write_gcspr_el1(read_el1_ctx_gcs(ctx, gcspr_el1));
  1613. write_gcspr_el0(read_el1_ctx_gcs(ctx, gcspr_el0));
  1614. }
  1615. if (is_feat_the_supported()) {
  1616. write_rcwmask_el1(read_el1_ctx_the(ctx, rcwmask_el1));
  1617. write_rcwsmask_el1(read_el1_ctx_the(ctx, rcwsmask_el1));
  1618. }
  1619. if (is_feat_sctlr2_supported()) {
  1620. write_sctlr2_el1(read_el1_ctx_sctlr2(ctx, sctlr2_el1));
  1621. }
  1622. if (is_feat_ls64_accdata_supported()) {
  1623. write_accdata_el1(read_el1_ctx_ls64(ctx, accdata_el1));
  1624. }
  1625. }
  1626. /*******************************************************************************
  1627. * The next couple of functions are used by runtime services to save and restore
  1628. * EL1 context on the 'cpu_context' structure for the specified security state.
  1629. ******************************************************************************/
  1630. void cm_el1_sysregs_context_save(uint32_t security_state)
  1631. {
  1632. cpu_context_t *ctx;
  1633. ctx = cm_get_context(security_state);
  1634. assert(ctx != NULL);
  1635. el1_sysregs_context_save(get_el1_sysregs_ctx(ctx));
  1636. #if IMAGE_BL31
  1637. if (security_state == SECURE)
  1638. PUBLISH_EVENT(cm_exited_secure_world);
  1639. else
  1640. PUBLISH_EVENT(cm_exited_normal_world);
  1641. #endif
  1642. }
  1643. void cm_el1_sysregs_context_restore(uint32_t security_state)
  1644. {
  1645. cpu_context_t *ctx;
  1646. ctx = cm_get_context(security_state);
  1647. assert(ctx != NULL);
  1648. el1_sysregs_context_restore(get_el1_sysregs_ctx(ctx));
  1649. #if IMAGE_BL31
  1650. if (security_state == SECURE)
  1651. PUBLISH_EVENT(cm_entering_secure_world);
  1652. else
  1653. PUBLISH_EVENT(cm_entering_normal_world);
  1654. #endif
  1655. }
  1656. #endif /* ((IMAGE_BL1) || (IMAGE_BL31 && (!CTX_INCLUDE_EL2_REGS))) */
  1657. /*******************************************************************************
  1658. * This function populates ELR_EL3 member of 'cpu_context' pertaining to the
  1659. * given security state with the given entrypoint
  1660. ******************************************************************************/
  1661. void cm_set_elr_el3(uint32_t security_state, uintptr_t entrypoint)
  1662. {
  1663. cpu_context_t *ctx;
  1664. el3_state_t *state;
  1665. ctx = cm_get_context(security_state);
  1666. assert(ctx != NULL);
  1667. /* Populate EL3 state so that ERET jumps to the correct entry */
  1668. state = get_el3state_ctx(ctx);
  1669. write_ctx_reg(state, CTX_ELR_EL3, entrypoint);
  1670. }
  1671. /*******************************************************************************
  1672. * This function populates ELR_EL3 and SPSR_EL3 members of 'cpu_context'
  1673. * pertaining to the given security state
  1674. ******************************************************************************/
  1675. void cm_set_elr_spsr_el3(uint32_t security_state,
  1676. uintptr_t entrypoint, uint32_t spsr)
  1677. {
  1678. cpu_context_t *ctx;
  1679. el3_state_t *state;
  1680. ctx = cm_get_context(security_state);
  1681. assert(ctx != NULL);
  1682. /* Populate EL3 state so that ERET jumps to the correct entry */
  1683. state = get_el3state_ctx(ctx);
  1684. write_ctx_reg(state, CTX_ELR_EL3, entrypoint);
  1685. write_ctx_reg(state, CTX_SPSR_EL3, spsr);
  1686. }
  1687. /*******************************************************************************
  1688. * This function updates a single bit in the SCR_EL3 member of the 'cpu_context'
  1689. * pertaining to the given security state using the value and bit position
  1690. * specified in the parameters. It preserves all other bits.
  1691. ******************************************************************************/
  1692. void cm_write_scr_el3_bit(uint32_t security_state,
  1693. uint32_t bit_pos,
  1694. uint32_t value)
  1695. {
  1696. cpu_context_t *ctx;
  1697. el3_state_t *state;
  1698. u_register_t scr_el3;
  1699. ctx = cm_get_context(security_state);
  1700. assert(ctx != NULL);
  1701. /* Ensure that the bit position is a valid one */
  1702. assert(((1UL << bit_pos) & SCR_VALID_BIT_MASK) != 0U);
  1703. /* Ensure that the 'value' is only a bit wide */
  1704. assert(value <= 1U);
  1705. /*
  1706. * Get the SCR_EL3 value from the cpu context, clear the desired bit
  1707. * and set it to its new value.
  1708. */
  1709. state = get_el3state_ctx(ctx);
  1710. scr_el3 = read_ctx_reg(state, CTX_SCR_EL3);
  1711. scr_el3 &= ~(1UL << bit_pos);
  1712. scr_el3 |= (u_register_t)value << bit_pos;
  1713. write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
  1714. }
  1715. /*******************************************************************************
  1716. * This function retrieves SCR_EL3 member of 'cpu_context' pertaining to the
  1717. * given security state.
  1718. ******************************************************************************/
  1719. u_register_t cm_get_scr_el3(uint32_t security_state)
  1720. {
  1721. cpu_context_t *ctx;
  1722. el3_state_t *state;
  1723. ctx = cm_get_context(security_state);
  1724. assert(ctx != NULL);
  1725. /* Populate EL3 state so that ERET jumps to the correct entry */
  1726. state = get_el3state_ctx(ctx);
  1727. return read_ctx_reg(state, CTX_SCR_EL3);
  1728. }
  1729. /*******************************************************************************
  1730. * This function is used to program the context that's used for exception
  1731. * return. This initializes the SP_EL3 to a pointer to a 'cpu_context' set for
  1732. * the required security state
  1733. ******************************************************************************/
  1734. void cm_set_next_eret_context(uint32_t security_state)
  1735. {
  1736. cpu_context_t *ctx;
  1737. ctx = cm_get_context(security_state);
  1738. assert(ctx != NULL);
  1739. cm_set_next_context(ctx);
  1740. }