context_mgmt.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713
  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/el3_runtime/context_mgmt.h>
  20. #include <lib/el3_runtime/cpu_data.h>
  21. #include <lib/el3_runtime/pubsub_events.h>
  22. #include <lib/extensions/amu.h>
  23. #include <lib/extensions/brbe.h>
  24. #include <lib/extensions/mpam.h>
  25. #include <lib/extensions/pmuv3.h>
  26. #include <lib/extensions/sme.h>
  27. #include <lib/extensions/spe.h>
  28. #include <lib/extensions/sve.h>
  29. #include <lib/extensions/sys_reg_trace.h>
  30. #include <lib/extensions/trbe.h>
  31. #include <lib/extensions/trf.h>
  32. #include <lib/utils.h>
  33. #if ENABLE_FEAT_TWED
  34. /* Make sure delay value fits within the range(0-15) */
  35. CASSERT(((TWED_DELAY & ~SCR_TWEDEL_MASK) == 0U), assert_twed_delay_value_check);
  36. #endif /* ENABLE_FEAT_TWED */
  37. per_world_context_t per_world_context[CPU_DATA_CONTEXT_NUM];
  38. static bool has_secure_perworld_init;
  39. static void manage_extensions_nonsecure(cpu_context_t *ctx);
  40. static void manage_extensions_secure(cpu_context_t *ctx);
  41. static void manage_extensions_secure_per_world(void);
  42. static void setup_el1_context(cpu_context_t *ctx, const struct entry_point_info *ep)
  43. {
  44. u_register_t sctlr_elx, actlr_elx;
  45. /*
  46. * Initialise SCTLR_EL1 to the reset value corresponding to the target
  47. * execution state setting all fields rather than relying on the hw.
  48. * Some fields have architecturally UNKNOWN reset values and these are
  49. * set to zero.
  50. *
  51. * SCTLR.EE: Endianness is taken from the entrypoint attributes.
  52. *
  53. * SCTLR.M, SCTLR.C and SCTLR.I: These fields must be zero (as
  54. * required by PSCI specification)
  55. */
  56. sctlr_elx = (EP_GET_EE(ep->h.attr) != 0U) ? SCTLR_EE_BIT : 0UL;
  57. if (GET_RW(ep->spsr) == MODE_RW_64) {
  58. sctlr_elx |= SCTLR_EL1_RES1;
  59. } else {
  60. /*
  61. * If the target execution state is AArch32 then the following
  62. * fields need to be set.
  63. *
  64. * SCTRL_EL1.nTWE: Set to one so that EL0 execution of WFE
  65. * instructions are not trapped to EL1.
  66. *
  67. * SCTLR_EL1.nTWI: Set to one so that EL0 execution of WFI
  68. * instructions are not trapped to EL1.
  69. *
  70. * SCTLR_EL1.CP15BEN: Set to one to enable EL0 execution of the
  71. * CP15DMB, CP15DSB, and CP15ISB instructions.
  72. */
  73. sctlr_elx |= SCTLR_AARCH32_EL1_RES1 | SCTLR_CP15BEN_BIT
  74. | SCTLR_NTWI_BIT | SCTLR_NTWE_BIT;
  75. }
  76. #if ERRATA_A75_764081
  77. /*
  78. * If workaround of errata 764081 for Cortex-A75 is used then set
  79. * SCTLR_EL1.IESB to enable Implicit Error Synchronization Barrier.
  80. */
  81. sctlr_elx |= SCTLR_IESB_BIT;
  82. #endif
  83. /* Store the initialised SCTLR_EL1 value in the cpu_context */
  84. write_ctx_reg(get_el1_sysregs_ctx(ctx), CTX_SCTLR_EL1, sctlr_elx);
  85. /*
  86. * Base the context ACTLR_EL1 on the current value, as it is
  87. * implementation defined. The context restore process will write
  88. * the value from the context to the actual register and can cause
  89. * problems for processor cores that don't expect certain bits to
  90. * be zero.
  91. */
  92. actlr_elx = read_actlr_el1();
  93. write_ctx_reg((get_el1_sysregs_ctx(ctx)), (CTX_ACTLR_EL1), (actlr_elx));
  94. }
  95. /******************************************************************************
  96. * This function performs initializations that are specific to SECURE state
  97. * and updates the cpu context specified by 'ctx'.
  98. *****************************************************************************/
  99. static void setup_secure_context(cpu_context_t *ctx, const struct entry_point_info *ep)
  100. {
  101. u_register_t scr_el3;
  102. el3_state_t *state;
  103. state = get_el3state_ctx(ctx);
  104. scr_el3 = read_ctx_reg(state, CTX_SCR_EL3);
  105. #if defined(IMAGE_BL31) && !defined(SPD_spmd)
  106. /*
  107. * SCR_EL3.IRQ, SCR_EL3.FIQ: Enable the physical FIQ and IRQ routing as
  108. * indicated by the interrupt routing model for BL31.
  109. */
  110. scr_el3 |= get_scr_el3_from_routing_model(SECURE);
  111. #endif
  112. /* Allow access to Allocation Tags when FEAT_MTE2 is implemented and enabled. */
  113. if (is_feat_mte2_supported()) {
  114. scr_el3 |= SCR_ATA_BIT;
  115. }
  116. write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
  117. /*
  118. * Initialize EL1 context registers unless SPMC is running
  119. * at S-EL2.
  120. */
  121. #if !SPMD_SPM_AT_SEL2
  122. setup_el1_context(ctx, ep);
  123. #endif
  124. manage_extensions_secure(ctx);
  125. /**
  126. * manage_extensions_secure_per_world api has to be executed once,
  127. * as the registers getting initialised, maintain constant value across
  128. * all the cpus for the secure world.
  129. * Henceforth, this check ensures that the registers are initialised once
  130. * and avoids re-initialization from multiple cores.
  131. */
  132. if (!has_secure_perworld_init) {
  133. manage_extensions_secure_per_world();
  134. }
  135. }
  136. #if ENABLE_RME
  137. /******************************************************************************
  138. * This function performs initializations that are specific to REALM state
  139. * and updates the cpu context specified by 'ctx'.
  140. *****************************************************************************/
  141. static void setup_realm_context(cpu_context_t *ctx, const struct entry_point_info *ep)
  142. {
  143. u_register_t scr_el3;
  144. el3_state_t *state;
  145. state = get_el3state_ctx(ctx);
  146. scr_el3 = read_ctx_reg(state, CTX_SCR_EL3);
  147. scr_el3 |= SCR_NS_BIT | SCR_NSE_BIT;
  148. /* CSV2 version 2 and above */
  149. if (is_feat_csv2_2_supported()) {
  150. /* Enable access to the SCXTNUM_ELx registers. */
  151. scr_el3 |= SCR_EnSCXT_BIT;
  152. }
  153. write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
  154. }
  155. #endif /* ENABLE_RME */
  156. /******************************************************************************
  157. * This function performs initializations that are specific to NON-SECURE state
  158. * and updates the cpu context specified by 'ctx'.
  159. *****************************************************************************/
  160. static void setup_ns_context(cpu_context_t *ctx, const struct entry_point_info *ep)
  161. {
  162. u_register_t scr_el3;
  163. el3_state_t *state;
  164. state = get_el3state_ctx(ctx);
  165. scr_el3 = read_ctx_reg(state, CTX_SCR_EL3);
  166. /* SCR_NS: Set the NS bit */
  167. scr_el3 |= SCR_NS_BIT;
  168. /* Allow access to Allocation Tags when FEAT_MTE2 is implemented and enabled. */
  169. if (is_feat_mte2_supported()) {
  170. scr_el3 |= SCR_ATA_BIT;
  171. }
  172. #if !CTX_INCLUDE_PAUTH_REGS
  173. /*
  174. * Pointer Authentication feature, if present, is always enabled by default
  175. * for Non secure lower exception levels. We do not have an explicit
  176. * flag to set it.
  177. * CTX_INCLUDE_PAUTH_REGS flag, is explicitly used to enable for lower
  178. * exception levels of secure and realm worlds.
  179. *
  180. * To prevent the leakage between the worlds during world switch,
  181. * we enable it only for the non-secure world.
  182. *
  183. * If the Secure/realm world wants to use pointer authentication,
  184. * CTX_INCLUDE_PAUTH_REGS must be explicitly set to 1, in which case
  185. * it will be enabled globally for all the contexts.
  186. *
  187. * SCR_EL3.API: Set to one to not trap any PAuth instructions at ELs
  188. * other than EL3
  189. *
  190. * SCR_EL3.APK: Set to one to not trap any PAuth key values at ELs other
  191. * than EL3
  192. */
  193. scr_el3 |= SCR_API_BIT | SCR_APK_BIT;
  194. #endif /* CTX_INCLUDE_PAUTH_REGS */
  195. #if HANDLE_EA_EL3_FIRST_NS
  196. /* SCR_EL3.EA: Route External Abort and SError Interrupt to EL3. */
  197. scr_el3 |= SCR_EA_BIT;
  198. #endif
  199. #if RAS_TRAP_NS_ERR_REC_ACCESS
  200. /*
  201. * SCR_EL3.TERR: Trap Error record accesses. Accesses to the RAS ERR
  202. * and RAS ERX registers from EL1 and EL2(from any security state)
  203. * are trapped to EL3.
  204. * Set here to trap only for NS EL1/EL2
  205. *
  206. */
  207. scr_el3 |= SCR_TERR_BIT;
  208. #endif
  209. /* CSV2 version 2 and above */
  210. if (is_feat_csv2_2_supported()) {
  211. /* Enable access to the SCXTNUM_ELx registers. */
  212. scr_el3 |= SCR_EnSCXT_BIT;
  213. }
  214. #ifdef IMAGE_BL31
  215. /*
  216. * SCR_EL3.IRQ, SCR_EL3.FIQ: Enable the physical FIQ and IRQ routing as
  217. * indicated by the interrupt routing model for BL31.
  218. */
  219. scr_el3 |= get_scr_el3_from_routing_model(NON_SECURE);
  220. #endif
  221. write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
  222. /* Initialize EL1 context registers */
  223. setup_el1_context(ctx, ep);
  224. /* Initialize EL2 context registers */
  225. #if CTX_INCLUDE_EL2_REGS
  226. /*
  227. * Initialize SCTLR_EL2 context register using Endianness value
  228. * taken from the entrypoint attribute.
  229. */
  230. u_register_t sctlr_el2_val = (EP_GET_EE(ep->h.attr) != 0U) ? SCTLR_EE_BIT : 0UL;
  231. sctlr_el2_val |= SCTLR_EL2_RES1;
  232. write_el2_ctx_common(get_el2_sysregs_ctx(ctx), sctlr_el2, sctlr_el2_val);
  233. if (is_feat_hcx_supported()) {
  234. /*
  235. * Initialize register HCRX_EL2 with its init value.
  236. * As the value of HCRX_EL2 is UNKNOWN on reset, there is a
  237. * chance that this can lead to unexpected behavior in lower
  238. * ELs that have not been updated since the introduction of
  239. * this feature if not properly initialized, especially when
  240. * it comes to those bits that enable/disable traps.
  241. */
  242. write_el2_ctx_hcx(get_el2_sysregs_ctx(ctx), hcrx_el2,
  243. HCRX_EL2_INIT_VAL);
  244. }
  245. if (is_feat_fgt_supported()) {
  246. /*
  247. * Initialize HFG*_EL2 registers with a default value so legacy
  248. * systems unaware of FEAT_FGT do not get trapped due to their lack
  249. * of initialization for this feature.
  250. */
  251. write_el2_ctx_fgt(get_el2_sysregs_ctx(ctx), hfgitr_el2,
  252. HFGITR_EL2_INIT_VAL);
  253. write_el2_ctx_fgt(get_el2_sysregs_ctx(ctx), hfgrtr_el2,
  254. HFGRTR_EL2_INIT_VAL);
  255. write_el2_ctx_fgt(get_el2_sysregs_ctx(ctx), hfgwtr_el2,
  256. HFGWTR_EL2_INIT_VAL);
  257. }
  258. #endif /* CTX_INCLUDE_EL2_REGS */
  259. manage_extensions_nonsecure(ctx);
  260. }
  261. /*******************************************************************************
  262. * The following function performs initialization of the cpu_context 'ctx'
  263. * for first use that is common to all security states, and sets the
  264. * initial entrypoint state as specified by the entry_point_info structure.
  265. *
  266. * The EE and ST attributes are used to configure the endianness and secure
  267. * timer availability for the new execution context.
  268. ******************************************************************************/
  269. static void setup_context_common(cpu_context_t *ctx, const entry_point_info_t *ep)
  270. {
  271. u_register_t scr_el3;
  272. el3_state_t *state;
  273. gp_regs_t *gp_regs;
  274. state = get_el3state_ctx(ctx);
  275. /* Clear any residual register values from the context */
  276. zeromem(ctx, sizeof(*ctx));
  277. /*
  278. * The lower-EL context is zeroed so that no stale values leak to a world.
  279. * It is assumed that an all-zero lower-EL context is good enough for it
  280. * to boot correctly. However, there are very few registers where this
  281. * is not true and some values need to be recreated.
  282. */
  283. #if CTX_INCLUDE_EL2_REGS
  284. el2_sysregs_t *el2_ctx = get_el2_sysregs_ctx(ctx);
  285. /*
  286. * These bits are set in the gicv3 driver. Losing them (especially the
  287. * SRE bit) is problematic for all worlds. Henceforth recreate them.
  288. */
  289. u_register_t icc_sre_el2_val = ICC_SRE_DIB_BIT | ICC_SRE_DFB_BIT |
  290. ICC_SRE_EN_BIT | ICC_SRE_SRE_BIT;
  291. write_el2_ctx_common(el2_ctx, icc_sre_el2, icc_sre_el2_val);
  292. #endif /* CTX_INCLUDE_EL2_REGS */
  293. /* Start with a clean SCR_EL3 copy as all relevant values are set */
  294. scr_el3 = SCR_RESET_VAL;
  295. /*
  296. * SCR_EL3.TWE: Set to zero so that execution of WFE instructions at
  297. * EL2, EL1 and EL0 are not trapped to EL3.
  298. *
  299. * SCR_EL3.TWI: Set to zero so that execution of WFI instructions at
  300. * EL2, EL1 and EL0 are not trapped to EL3.
  301. *
  302. * SCR_EL3.SMD: Set to zero to enable SMC calls at EL1 and above, from
  303. * both Security states and both Execution states.
  304. *
  305. * SCR_EL3.SIF: Set to one to disable secure instruction execution from
  306. * Non-secure memory.
  307. */
  308. scr_el3 &= ~(SCR_TWE_BIT | SCR_TWI_BIT | SCR_SMD_BIT);
  309. scr_el3 |= SCR_SIF_BIT;
  310. /*
  311. * SCR_EL3.RW: Set the execution state, AArch32 or AArch64, for next
  312. * Exception level as specified by SPSR.
  313. */
  314. if (GET_RW(ep->spsr) == MODE_RW_64) {
  315. scr_el3 |= SCR_RW_BIT;
  316. }
  317. /*
  318. * SCR_EL3.ST: Traps Secure EL1 accesses to the Counter-timer Physical
  319. * Secure timer registers to EL3, from AArch64 state only, if specified
  320. * by the entrypoint attributes. If SEL2 is present and enabled, the ST
  321. * bit always behaves as 1 (i.e. secure physical timer register access
  322. * is not trapped)
  323. */
  324. if (EP_GET_ST(ep->h.attr) != 0U) {
  325. scr_el3 |= SCR_ST_BIT;
  326. }
  327. /*
  328. * If FEAT_HCX is enabled, enable access to HCRX_EL2 by setting
  329. * SCR_EL3.HXEn.
  330. */
  331. if (is_feat_hcx_supported()) {
  332. scr_el3 |= SCR_HXEn_BIT;
  333. }
  334. /*
  335. * If FEAT_RNG_TRAP is enabled, all reads of the RNDR and RNDRRS
  336. * registers are trapped to EL3.
  337. */
  338. #if ENABLE_FEAT_RNG_TRAP
  339. scr_el3 |= SCR_TRNDR_BIT;
  340. #endif
  341. #if FAULT_INJECTION_SUPPORT
  342. /* Enable fault injection from lower ELs */
  343. scr_el3 |= SCR_FIEN_BIT;
  344. #endif
  345. #if CTX_INCLUDE_PAUTH_REGS
  346. /*
  347. * Enable Pointer Authentication globally for all the worlds.
  348. *
  349. * SCR_EL3.API: Set to one to not trap any PAuth instructions at ELs
  350. * other than EL3
  351. *
  352. * SCR_EL3.APK: Set to one to not trap any PAuth key values at ELs other
  353. * than EL3
  354. */
  355. scr_el3 |= SCR_API_BIT | SCR_APK_BIT;
  356. #endif /* CTX_INCLUDE_PAUTH_REGS */
  357. /*
  358. * SCR_EL3.TCR2EN: Enable access to TCR2_ELx for AArch64 if present.
  359. */
  360. if (is_feat_tcr2_supported() && (GET_RW(ep->spsr) == MODE_RW_64)) {
  361. scr_el3 |= SCR_TCR2EN_BIT;
  362. }
  363. /*
  364. * SCR_EL3.PIEN: Enable permission indirection and overlay
  365. * registers for AArch64 if present.
  366. */
  367. if (is_feat_sxpie_supported() || is_feat_sxpoe_supported()) {
  368. scr_el3 |= SCR_PIEN_BIT;
  369. }
  370. /*
  371. * SCR_EL3.GCSEn: Enable GCS registers for AArch64 if present.
  372. */
  373. if ((is_feat_gcs_supported()) && (GET_RW(ep->spsr) == MODE_RW_64)) {
  374. scr_el3 |= SCR_GCSEn_BIT;
  375. }
  376. /*
  377. * SCR_EL3.HCE: Enable HVC instructions if next execution state is
  378. * AArch64 and next EL is EL2, or if next execution state is AArch32 and
  379. * next mode is Hyp.
  380. * SCR_EL3.FGTEn: Enable Fine Grained Virtualization Traps under the
  381. * same conditions as HVC instructions and when the processor supports
  382. * ARMv8.6-FGT.
  383. * SCR_EL3.ECVEn: Enable Enhanced Counter Virtualization (ECV)
  384. * CNTPOFF_EL2 register under the same conditions as HVC instructions
  385. * and when the processor supports ECV.
  386. */
  387. if (((GET_RW(ep->spsr) == MODE_RW_64) && (GET_EL(ep->spsr) == MODE_EL2))
  388. || ((GET_RW(ep->spsr) != MODE_RW_64)
  389. && (GET_M32(ep->spsr) == MODE32_hyp))) {
  390. scr_el3 |= SCR_HCE_BIT;
  391. if (is_feat_fgt_supported()) {
  392. scr_el3 |= SCR_FGTEN_BIT;
  393. }
  394. if (is_feat_ecv_supported()) {
  395. scr_el3 |= SCR_ECVEN_BIT;
  396. }
  397. }
  398. /* Enable WFE trap delay in SCR_EL3 if supported and configured */
  399. if (is_feat_twed_supported()) {
  400. /* Set delay in SCR_EL3 */
  401. scr_el3 &= ~(SCR_TWEDEL_MASK << SCR_TWEDEL_SHIFT);
  402. scr_el3 |= ((TWED_DELAY & SCR_TWEDEL_MASK)
  403. << SCR_TWEDEL_SHIFT);
  404. /* Enable WFE delay */
  405. scr_el3 |= SCR_TWEDEn_BIT;
  406. }
  407. #if IMAGE_BL31 && defined(SPD_spmd) && SPMD_SPM_AT_SEL2
  408. /* Enable S-EL2 if FEAT_SEL2 is implemented for all the contexts. */
  409. if (is_feat_sel2_supported()) {
  410. scr_el3 |= SCR_EEL2_BIT;
  411. }
  412. #endif /* (IMAGE_BL31 && defined(SPD_spmd) && SPMD_SPM_AT_SEL2) */
  413. /*
  414. * Populate EL3 state so that we've the right context
  415. * before doing ERET
  416. */
  417. write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
  418. write_ctx_reg(state, CTX_ELR_EL3, ep->pc);
  419. write_ctx_reg(state, CTX_SPSR_EL3, ep->spsr);
  420. /*
  421. * Store the X0-X7 value from the entrypoint into the context
  422. * Use memcpy as we are in control of the layout of the structures
  423. */
  424. gp_regs = get_gpregs_ctx(ctx);
  425. memcpy(gp_regs, (void *)&ep->args, sizeof(aapcs64_params_t));
  426. }
  427. /*******************************************************************************
  428. * Context management library initialization routine. This library is used by
  429. * runtime services to share pointers to 'cpu_context' structures for secure
  430. * non-secure and realm states. Management of the structures and their associated
  431. * memory is not done by the context management library e.g. the PSCI service
  432. * manages the cpu context used for entry from and exit to the non-secure state.
  433. * The Secure payload dispatcher service manages the context(s) corresponding to
  434. * the secure state. It also uses this library to get access to the non-secure
  435. * state cpu context pointers.
  436. * Lastly, this library provides the API to make SP_EL3 point to the cpu context
  437. * which will be used for programming an entry into a lower EL. The same context
  438. * will be used to save state upon exception entry from that EL.
  439. ******************************************************************************/
  440. void __init cm_init(void)
  441. {
  442. /*
  443. * The context management library has only global data to initialize, but
  444. * that will be done when the BSS is zeroed out.
  445. */
  446. }
  447. /*******************************************************************************
  448. * This is the high-level function used to initialize the cpu_context 'ctx' for
  449. * first use. It performs initializations that are common to all security states
  450. * and initializations specific to the security state specified in 'ep'
  451. ******************************************************************************/
  452. void cm_setup_context(cpu_context_t *ctx, const entry_point_info_t *ep)
  453. {
  454. unsigned int security_state;
  455. assert(ctx != NULL);
  456. /*
  457. * Perform initializations that are common
  458. * to all security states
  459. */
  460. setup_context_common(ctx, ep);
  461. security_state = GET_SECURITY_STATE(ep->h.attr);
  462. /* Perform security state specific initializations */
  463. switch (security_state) {
  464. case SECURE:
  465. setup_secure_context(ctx, ep);
  466. break;
  467. #if ENABLE_RME
  468. case REALM:
  469. setup_realm_context(ctx, ep);
  470. break;
  471. #endif
  472. case NON_SECURE:
  473. setup_ns_context(ctx, ep);
  474. break;
  475. default:
  476. ERROR("Invalid security state\n");
  477. panic();
  478. break;
  479. }
  480. }
  481. /*******************************************************************************
  482. * Enable architecture extensions for EL3 execution. This function only updates
  483. * registers in-place which are expected to either never change or be
  484. * overwritten by el3_exit.
  485. ******************************************************************************/
  486. #if IMAGE_BL31
  487. void cm_manage_extensions_el3(void)
  488. {
  489. if (is_feat_spe_supported()) {
  490. spe_init_el3();
  491. }
  492. if (is_feat_amu_supported()) {
  493. amu_init_el3();
  494. }
  495. if (is_feat_sme_supported()) {
  496. sme_init_el3();
  497. }
  498. if (is_feat_trbe_supported()) {
  499. trbe_init_el3();
  500. }
  501. if (is_feat_brbe_supported()) {
  502. brbe_init_el3();
  503. }
  504. if (is_feat_trf_supported()) {
  505. trf_init_el3();
  506. }
  507. pmuv3_init_el3();
  508. }
  509. #endif /* IMAGE_BL31 */
  510. /******************************************************************************
  511. * Function to initialise the registers with the RESET values in the context
  512. * memory, which are maintained per world.
  513. ******************************************************************************/
  514. #if IMAGE_BL31
  515. void cm_el3_arch_init_per_world(per_world_context_t *per_world_ctx)
  516. {
  517. /*
  518. * Initialise CPTR_EL3, setting all fields rather than relying on hw.
  519. *
  520. * CPTR_EL3.TFP: Set to zero so that accesses to the V- or Z- registers
  521. * by Advanced SIMD, floating-point or SVE instructions (if
  522. * implemented) do not trap to EL3.
  523. *
  524. * CPTR_EL3.TCPAC: Set to zero so that accesses to CPACR_EL1,
  525. * CPTR_EL2,CPACR, or HCPTR do not trap to EL3.
  526. */
  527. uint64_t cptr_el3 = CPTR_EL3_RESET_VAL & ~(TCPAC_BIT | TFP_BIT);
  528. per_world_ctx->ctx_cptr_el3 = cptr_el3;
  529. /*
  530. * Initialize MPAM3_EL3 to its default reset value
  531. *
  532. * MPAM3_EL3_RESET_VAL sets the MPAM3_EL3.TRAPLOWER bit that forces
  533. * all lower ELn MPAM3_EL3 register access to, trap to EL3
  534. */
  535. per_world_ctx->ctx_mpam3_el3 = MPAM3_EL3_RESET_VAL;
  536. }
  537. #endif /* IMAGE_BL31 */
  538. /*******************************************************************************
  539. * Initialise per_world_context for Non-Secure world.
  540. * This function enables the architecture extensions, which have same value
  541. * across the cores for the non-secure world.
  542. ******************************************************************************/
  543. #if IMAGE_BL31
  544. void manage_extensions_nonsecure_per_world(void)
  545. {
  546. cm_el3_arch_init_per_world(&per_world_context[CPU_CONTEXT_NS]);
  547. if (is_feat_sme_supported()) {
  548. sme_enable_per_world(&per_world_context[CPU_CONTEXT_NS]);
  549. }
  550. if (is_feat_sve_supported()) {
  551. sve_enable_per_world(&per_world_context[CPU_CONTEXT_NS]);
  552. }
  553. if (is_feat_amu_supported()) {
  554. amu_enable_per_world(&per_world_context[CPU_CONTEXT_NS]);
  555. }
  556. if (is_feat_sys_reg_trace_supported()) {
  557. sys_reg_trace_enable_per_world(&per_world_context[CPU_CONTEXT_NS]);
  558. }
  559. if (is_feat_mpam_supported()) {
  560. mpam_enable_per_world(&per_world_context[CPU_CONTEXT_NS]);
  561. }
  562. }
  563. #endif /* IMAGE_BL31 */
  564. /*******************************************************************************
  565. * Initialise per_world_context for Secure world.
  566. * This function enables the architecture extensions, which have same value
  567. * across the cores for the secure world.
  568. ******************************************************************************/
  569. static void manage_extensions_secure_per_world(void)
  570. {
  571. #if IMAGE_BL31
  572. cm_el3_arch_init_per_world(&per_world_context[CPU_CONTEXT_SECURE]);
  573. if (is_feat_sme_supported()) {
  574. if (ENABLE_SME_FOR_SWD) {
  575. /*
  576. * Enable SME, SVE, FPU/SIMD in secure context, SPM must ensure
  577. * SME, SVE, and FPU/SIMD context properly managed.
  578. */
  579. sme_enable_per_world(&per_world_context[CPU_CONTEXT_SECURE]);
  580. } else {
  581. /*
  582. * Disable SME, SVE, FPU/SIMD in secure context so non-secure
  583. * world can safely use the associated registers.
  584. */
  585. sme_disable_per_world(&per_world_context[CPU_CONTEXT_SECURE]);
  586. }
  587. }
  588. if (is_feat_sve_supported()) {
  589. if (ENABLE_SVE_FOR_SWD) {
  590. /*
  591. * Enable SVE and FPU in secure context, SPM must ensure
  592. * that the SVE and FPU register contexts are properly managed.
  593. */
  594. sve_enable_per_world(&per_world_context[CPU_CONTEXT_SECURE]);
  595. } else {
  596. /*
  597. * Disable SVE and FPU in secure context so non-secure world
  598. * can safely use them.
  599. */
  600. sve_disable_per_world(&per_world_context[CPU_CONTEXT_SECURE]);
  601. }
  602. }
  603. /* NS can access this but Secure shouldn't */
  604. if (is_feat_sys_reg_trace_supported()) {
  605. sys_reg_trace_disable_per_world(&per_world_context[CPU_CONTEXT_SECURE]);
  606. }
  607. has_secure_perworld_init = true;
  608. #endif /* IMAGE_BL31 */
  609. }
  610. /*******************************************************************************
  611. * Enable architecture extensions on first entry to Non-secure world.
  612. ******************************************************************************/
  613. static void manage_extensions_nonsecure(cpu_context_t *ctx)
  614. {
  615. #if IMAGE_BL31
  616. if (is_feat_amu_supported()) {
  617. amu_enable(ctx);
  618. }
  619. if (is_feat_sme_supported()) {
  620. sme_enable(ctx);
  621. }
  622. pmuv3_enable(ctx);
  623. #endif /* IMAGE_BL31 */
  624. }
  625. /* TODO: move to lib/extensions/pauth when it has been ported to FEAT_STATE */
  626. static __unused void enable_pauth_el2(void)
  627. {
  628. u_register_t hcr_el2 = read_hcr_el2();
  629. /*
  630. * For Armv8.3 pointer authentication feature, disable traps to EL2 when
  631. * accessing key registers or using pointer authentication instructions
  632. * from lower ELs.
  633. */
  634. hcr_el2 |= (HCR_API_BIT | HCR_APK_BIT);
  635. write_hcr_el2(hcr_el2);
  636. }
  637. #if INIT_UNUSED_NS_EL2
  638. /*******************************************************************************
  639. * Enable architecture extensions in-place at EL2 on first entry to Non-secure
  640. * world when EL2 is empty and unused.
  641. ******************************************************************************/
  642. static void manage_extensions_nonsecure_el2_unused(void)
  643. {
  644. #if IMAGE_BL31
  645. if (is_feat_spe_supported()) {
  646. spe_init_el2_unused();
  647. }
  648. if (is_feat_amu_supported()) {
  649. amu_init_el2_unused();
  650. }
  651. if (is_feat_mpam_supported()) {
  652. mpam_init_el2_unused();
  653. }
  654. if (is_feat_trbe_supported()) {
  655. trbe_init_el2_unused();
  656. }
  657. if (is_feat_sys_reg_trace_supported()) {
  658. sys_reg_trace_init_el2_unused();
  659. }
  660. if (is_feat_trf_supported()) {
  661. trf_init_el2_unused();
  662. }
  663. pmuv3_init_el2_unused();
  664. if (is_feat_sve_supported()) {
  665. sve_init_el2_unused();
  666. }
  667. if (is_feat_sme_supported()) {
  668. sme_init_el2_unused();
  669. }
  670. #if ENABLE_PAUTH
  671. enable_pauth_el2();
  672. #endif /* ENABLE_PAUTH */
  673. #endif /* IMAGE_BL31 */
  674. }
  675. #endif /* INIT_UNUSED_NS_EL2 */
  676. /*******************************************************************************
  677. * Enable architecture extensions on first entry to Secure world.
  678. ******************************************************************************/
  679. static void manage_extensions_secure(cpu_context_t *ctx)
  680. {
  681. #if IMAGE_BL31
  682. if (is_feat_sme_supported()) {
  683. if (ENABLE_SME_FOR_SWD) {
  684. /*
  685. * Enable SME, SVE, FPU/SIMD in secure context, secure manager
  686. * must ensure SME, SVE, and FPU/SIMD context properly managed.
  687. */
  688. sme_init_el3();
  689. sme_enable(ctx);
  690. } else {
  691. /*
  692. * Disable SME, SVE, FPU/SIMD in secure context so non-secure
  693. * world can safely use the associated registers.
  694. */
  695. sme_disable(ctx);
  696. }
  697. }
  698. #endif /* IMAGE_BL31 */
  699. }
  700. /*******************************************************************************
  701. * The following function initializes the cpu_context for a CPU specified by
  702. * its `cpu_idx` for first use, and sets the initial entrypoint state as
  703. * specified by the entry_point_info structure.
  704. ******************************************************************************/
  705. void cm_init_context_by_index(unsigned int cpu_idx,
  706. const entry_point_info_t *ep)
  707. {
  708. cpu_context_t *ctx;
  709. ctx = cm_get_context_by_index(cpu_idx, GET_SECURITY_STATE(ep->h.attr));
  710. cm_setup_context(ctx, ep);
  711. }
  712. /*******************************************************************************
  713. * The following function initializes the cpu_context for the current CPU
  714. * for first use, and sets the initial entrypoint state as specified by the
  715. * entry_point_info structure.
  716. ******************************************************************************/
  717. void cm_init_my_context(const entry_point_info_t *ep)
  718. {
  719. cpu_context_t *ctx;
  720. ctx = cm_get_context(GET_SECURITY_STATE(ep->h.attr));
  721. cm_setup_context(ctx, ep);
  722. }
  723. /* EL2 present but unused, need to disable safely. SCTLR_EL2 can be ignored */
  724. static void init_nonsecure_el2_unused(cpu_context_t *ctx)
  725. {
  726. #if INIT_UNUSED_NS_EL2
  727. u_register_t hcr_el2 = HCR_RESET_VAL;
  728. u_register_t mdcr_el2;
  729. u_register_t scr_el3;
  730. scr_el3 = read_ctx_reg(get_el3state_ctx(ctx), CTX_SCR_EL3);
  731. /* Set EL2 register width: Set HCR_EL2.RW to match SCR_EL3.RW */
  732. if ((scr_el3 & SCR_RW_BIT) != 0U) {
  733. hcr_el2 |= HCR_RW_BIT;
  734. }
  735. write_hcr_el2(hcr_el2);
  736. /*
  737. * Initialise CPTR_EL2 setting all fields rather than relying on the hw.
  738. * All fields have architecturally UNKNOWN reset values.
  739. */
  740. write_cptr_el2(CPTR_EL2_RESET_VAL);
  741. /*
  742. * Initialise CNTHCTL_EL2. All fields are architecturally UNKNOWN on
  743. * reset and are set to zero except for field(s) listed below.
  744. *
  745. * CNTHCTL_EL2.EL1PTEN: Set to one to disable traps to Hyp mode of
  746. * Non-secure EL0 and EL1 accesses to the physical timer registers.
  747. *
  748. * CNTHCTL_EL2.EL1PCTEN: Set to one to disable traps to Hyp mode of
  749. * Non-secure EL0 and EL1 accesses to the physical counter registers.
  750. */
  751. write_cnthctl_el2(CNTHCTL_RESET_VAL | EL1PCEN_BIT | EL1PCTEN_BIT);
  752. /*
  753. * Initialise CNTVOFF_EL2 to zero as it resets to an architecturally
  754. * UNKNOWN value.
  755. */
  756. write_cntvoff_el2(0);
  757. /*
  758. * Set VPIDR_EL2 and VMPIDR_EL2 to match MIDR_EL1 and MPIDR_EL1
  759. * respectively.
  760. */
  761. write_vpidr_el2(read_midr_el1());
  762. write_vmpidr_el2(read_mpidr_el1());
  763. /*
  764. * Initialise VTTBR_EL2. All fields are architecturally UNKNOWN on reset.
  765. *
  766. * VTTBR_EL2.VMID: Set to zero. Even though EL1&0 stage 2 address
  767. * translation is disabled, cache maintenance operations depend on the
  768. * VMID.
  769. *
  770. * VTTBR_EL2.BADDR: Set to zero as EL1&0 stage 2 address translation is
  771. * disabled.
  772. */
  773. write_vttbr_el2(VTTBR_RESET_VAL &
  774. ~((VTTBR_VMID_MASK << VTTBR_VMID_SHIFT) |
  775. (VTTBR_BADDR_MASK << VTTBR_BADDR_SHIFT)));
  776. /*
  777. * Initialise MDCR_EL2, setting all fields rather than relying on hw.
  778. * Some fields are architecturally UNKNOWN on reset.
  779. *
  780. * MDCR_EL2.TDRA: Set to zero so that Non-secure EL0 and EL1 System
  781. * register accesses to the Debug ROM registers are not trapped to EL2.
  782. *
  783. * MDCR_EL2.TDOSA: Set to zero so that Non-secure EL1 System register
  784. * accesses to the powerdown debug registers are not trapped to EL2.
  785. *
  786. * MDCR_EL2.TDA: Set to zero so that System register accesses to the
  787. * debug registers do not trap to EL2.
  788. *
  789. * MDCR_EL2.TDE: Set to zero so that debug exceptions are not routed to
  790. * EL2.
  791. */
  792. mdcr_el2 = MDCR_EL2_RESET_VAL &
  793. ~(MDCR_EL2_TDRA_BIT | MDCR_EL2_TDOSA_BIT | MDCR_EL2_TDA_BIT |
  794. MDCR_EL2_TDE_BIT);
  795. write_mdcr_el2(mdcr_el2);
  796. /*
  797. * Initialise HSTR_EL2. All fields are architecturally UNKNOWN on reset.
  798. *
  799. * HSTR_EL2.T<n>: Set all these fields to zero so that Non-secure EL0 or
  800. * EL1 accesses to System registers do not trap to EL2.
  801. */
  802. write_hstr_el2(HSTR_EL2_RESET_VAL & ~(HSTR_EL2_T_MASK));
  803. /*
  804. * Initialise CNTHP_CTL_EL2. All fields are architecturally UNKNOWN on
  805. * reset.
  806. *
  807. * CNTHP_CTL_EL2:ENABLE: Set to zero to disable the EL2 physical timer
  808. * and prevent timer interrupts.
  809. */
  810. write_cnthp_ctl_el2(CNTHP_CTL_RESET_VAL & ~(CNTHP_CTL_ENABLE_BIT));
  811. manage_extensions_nonsecure_el2_unused();
  812. #endif /* INIT_UNUSED_NS_EL2 */
  813. }
  814. /*******************************************************************************
  815. * Prepare the CPU system registers for first entry into realm, secure, or
  816. * normal world.
  817. *
  818. * If execution is requested to EL2 or hyp mode, SCTLR_EL2 is initialized
  819. * If execution is requested to non-secure EL1 or svc mode, and the CPU supports
  820. * EL2 then EL2 is disabled by configuring all necessary EL2 registers.
  821. * For all entries, the EL1 registers are initialized from the cpu_context
  822. ******************************************************************************/
  823. void cm_prepare_el3_exit(uint32_t security_state)
  824. {
  825. u_register_t sctlr_elx, scr_el3;
  826. cpu_context_t *ctx = cm_get_context(security_state);
  827. assert(ctx != NULL);
  828. if (security_state == NON_SECURE) {
  829. uint64_t el2_implemented = el_implemented(2);
  830. scr_el3 = read_ctx_reg(get_el3state_ctx(ctx),
  831. CTX_SCR_EL3);
  832. if (el2_implemented != EL_IMPL_NONE) {
  833. /*
  834. * If context is not being used for EL2, initialize
  835. * HCRX_EL2 with its init value here.
  836. */
  837. if (is_feat_hcx_supported()) {
  838. write_hcrx_el2(HCRX_EL2_INIT_VAL);
  839. }
  840. /*
  841. * Initialize Fine-grained trap registers introduced
  842. * by FEAT_FGT so all traps are initially disabled when
  843. * switching to EL2 or a lower EL, preventing undesired
  844. * behavior.
  845. */
  846. if (is_feat_fgt_supported()) {
  847. /*
  848. * Initialize HFG*_EL2 registers with a default
  849. * value so legacy systems unaware of FEAT_FGT
  850. * do not get trapped due to their lack of
  851. * initialization for this feature.
  852. */
  853. write_hfgitr_el2(HFGITR_EL2_INIT_VAL);
  854. write_hfgrtr_el2(HFGRTR_EL2_INIT_VAL);
  855. write_hfgwtr_el2(HFGWTR_EL2_INIT_VAL);
  856. }
  857. /* Condition to ensure EL2 is being used. */
  858. if ((scr_el3 & SCR_HCE_BIT) != 0U) {
  859. /* Use SCTLR_EL1.EE value to initialise sctlr_el2 */
  860. sctlr_elx = read_ctx_reg(get_el1_sysregs_ctx(ctx),
  861. CTX_SCTLR_EL1);
  862. sctlr_elx &= SCTLR_EE_BIT;
  863. sctlr_elx |= SCTLR_EL2_RES1;
  864. #if ERRATA_A75_764081
  865. /*
  866. * If workaround of errata 764081 for Cortex-A75
  867. * is used then set SCTLR_EL2.IESB to enable
  868. * Implicit Error Synchronization Barrier.
  869. */
  870. sctlr_elx |= SCTLR_IESB_BIT;
  871. #endif /* ERRATA_A75_764081 */
  872. write_sctlr_el2(sctlr_elx);
  873. } else {
  874. /*
  875. * (scr_el3 & SCR_HCE_BIT==0)
  876. * EL2 implemented but unused.
  877. */
  878. init_nonsecure_el2_unused(ctx);
  879. }
  880. }
  881. }
  882. cm_el1_sysregs_context_restore(security_state);
  883. cm_set_next_eret_context(security_state);
  884. }
  885. #if CTX_INCLUDE_EL2_REGS
  886. static void el2_sysregs_context_save_fgt(el2_sysregs_t *ctx)
  887. {
  888. write_el2_ctx_fgt(ctx, hdfgrtr_el2, read_hdfgrtr_el2());
  889. if (is_feat_amu_supported()) {
  890. write_el2_ctx_fgt(ctx, hafgrtr_el2, read_hafgrtr_el2());
  891. }
  892. write_el2_ctx_fgt(ctx, hdfgwtr_el2, read_hdfgwtr_el2());
  893. write_el2_ctx_fgt(ctx, hfgitr_el2, read_hfgitr_el2());
  894. write_el2_ctx_fgt(ctx, hfgrtr_el2, read_hfgrtr_el2());
  895. write_el2_ctx_fgt(ctx, hfgwtr_el2, read_hfgwtr_el2());
  896. }
  897. static void el2_sysregs_context_restore_fgt(el2_sysregs_t *ctx)
  898. {
  899. write_hdfgrtr_el2(read_el2_ctx_fgt(ctx, hdfgrtr_el2));
  900. if (is_feat_amu_supported()) {
  901. write_hafgrtr_el2(read_el2_ctx_fgt(ctx, hafgrtr_el2));
  902. }
  903. write_hdfgwtr_el2(read_el2_ctx_fgt(ctx, hdfgwtr_el2));
  904. write_hfgitr_el2(read_el2_ctx_fgt(ctx, hfgitr_el2));
  905. write_hfgrtr_el2(read_el2_ctx_fgt(ctx, hfgrtr_el2));
  906. write_hfgwtr_el2(read_el2_ctx_fgt(ctx, hfgwtr_el2));
  907. }
  908. #if CTX_INCLUDE_MPAM_REGS
  909. static void el2_sysregs_context_save_mpam(mpam_t *ctx)
  910. {
  911. u_register_t mpam_idr = read_mpamidr_el1();
  912. write_ctx_reg(ctx, CTX_MPAM2_EL2, read_mpam2_el2());
  913. /*
  914. * The context registers that we intend to save would be part of the
  915. * PE's system register frame only if MPAMIDR_EL1.HAS_HCR == 1.
  916. */
  917. if ((mpam_idr & MPAMIDR_HAS_HCR_BIT) == 0U) {
  918. return;
  919. }
  920. /*
  921. * MPAMHCR_EL2, MPAMVPMV_EL2 and MPAMVPM0_EL2 are always present if
  922. * MPAMIDR_HAS_HCR_BIT == 1.
  923. */
  924. write_ctx_reg(ctx, CTX_MPAMHCR_EL2, read_mpamhcr_el2());
  925. write_ctx_reg(ctx, CTX_MPAMVPM0_EL2, read_mpamvpm0_el2());
  926. write_ctx_reg(ctx, CTX_MPAMVPMV_EL2, read_mpamvpmv_el2());
  927. /*
  928. * The number of MPAMVPM registers is implementation defined, their
  929. * number is stored in the MPAMIDR_EL1 register.
  930. */
  931. switch ((mpam_idr >> MPAMIDR_EL1_VPMR_MAX_SHIFT) & MPAMIDR_EL1_VPMR_MAX_MASK) {
  932. case 7:
  933. write_ctx_reg(ctx, CTX_MPAMVPM7_EL2, read_mpamvpm7_el2());
  934. __fallthrough;
  935. case 6:
  936. write_ctx_reg(ctx, CTX_MPAMVPM6_EL2, read_mpamvpm6_el2());
  937. __fallthrough;
  938. case 5:
  939. write_ctx_reg(ctx, CTX_MPAMVPM5_EL2, read_mpamvpm5_el2());
  940. __fallthrough;
  941. case 4:
  942. write_ctx_reg(ctx, CTX_MPAMVPM4_EL2, read_mpamvpm4_el2());
  943. __fallthrough;
  944. case 3:
  945. write_ctx_reg(ctx, CTX_MPAMVPM3_EL2, read_mpamvpm3_el2());
  946. __fallthrough;
  947. case 2:
  948. write_ctx_reg(ctx, CTX_MPAMVPM2_EL2, read_mpamvpm2_el2());
  949. __fallthrough;
  950. case 1:
  951. write_ctx_reg(ctx, CTX_MPAMVPM1_EL2, read_mpamvpm1_el2());
  952. break;
  953. }
  954. }
  955. #endif /* CTX_INCLUDE_MPAM_REGS */
  956. #if CTX_INCLUDE_MPAM_REGS
  957. static void el2_sysregs_context_restore_mpam(mpam_t *ctx)
  958. {
  959. u_register_t mpam_idr = read_mpamidr_el1();
  960. write_mpam2_el2(read_ctx_reg(ctx, CTX_MPAM2_EL2));
  961. if ((mpam_idr & MPAMIDR_HAS_HCR_BIT) == 0U) {
  962. return;
  963. }
  964. write_mpamhcr_el2(read_ctx_reg(ctx, CTX_MPAMHCR_EL2));
  965. write_mpamvpm0_el2(read_ctx_reg(ctx, CTX_MPAMVPM0_EL2));
  966. write_mpamvpmv_el2(read_ctx_reg(ctx, CTX_MPAMVPMV_EL2));
  967. switch ((mpam_idr >> MPAMIDR_EL1_VPMR_MAX_SHIFT) & MPAMIDR_EL1_VPMR_MAX_MASK) {
  968. case 7:
  969. write_mpamvpm7_el2(read_ctx_reg(ctx, CTX_MPAMVPM7_EL2));
  970. __fallthrough;
  971. case 6:
  972. write_mpamvpm6_el2(read_ctx_reg(ctx, CTX_MPAMVPM6_EL2));
  973. __fallthrough;
  974. case 5:
  975. write_mpamvpm5_el2(read_ctx_reg(ctx, CTX_MPAMVPM5_EL2));
  976. __fallthrough;
  977. case 4:
  978. write_mpamvpm4_el2(read_ctx_reg(ctx, CTX_MPAMVPM4_EL2));
  979. __fallthrough;
  980. case 3:
  981. write_mpamvpm3_el2(read_ctx_reg(ctx, CTX_MPAMVPM3_EL2));
  982. __fallthrough;
  983. case 2:
  984. write_mpamvpm2_el2(read_ctx_reg(ctx, CTX_MPAMVPM2_EL2));
  985. __fallthrough;
  986. case 1:
  987. write_mpamvpm1_el2(read_ctx_reg(ctx, CTX_MPAMVPM1_EL2));
  988. break;
  989. }
  990. }
  991. #endif /* CTX_INCLUDE_MPAM_REGS */
  992. /* ---------------------------------------------------------------------------
  993. * The following registers are not added:
  994. * ICH_AP0R<n>_EL2
  995. * ICH_AP1R<n>_EL2
  996. * ICH_LR<n>_EL2
  997. *
  998. * NOTE: For a system with S-EL2 present but not enabled, accessing
  999. * ICC_SRE_EL2 is undefined from EL3. To workaround this change the
  1000. * SCR_EL3.NS = 1 before accessing this register.
  1001. * ---------------------------------------------------------------------------
  1002. */
  1003. static void el2_sysregs_context_save_gic(el2_sysregs_t *ctx)
  1004. {
  1005. #if defined(SPD_spmd) && SPMD_SPM_AT_SEL2
  1006. write_el2_ctx_common(ctx, icc_sre_el2, read_icc_sre_el2());
  1007. #else
  1008. u_register_t scr_el3 = read_scr_el3();
  1009. write_scr_el3(scr_el3 | SCR_NS_BIT);
  1010. isb();
  1011. write_el2_ctx_common(ctx, icc_sre_el2, read_icc_sre_el2());
  1012. write_scr_el3(scr_el3);
  1013. isb();
  1014. #endif
  1015. write_el2_ctx_common(ctx, ich_hcr_el2, read_ich_hcr_el2());
  1016. write_el2_ctx_common(ctx, ich_vmcr_el2, read_ich_vmcr_el2());
  1017. }
  1018. static void el2_sysregs_context_restore_gic(el2_sysregs_t *ctx)
  1019. {
  1020. #if defined(SPD_spmd) && SPMD_SPM_AT_SEL2
  1021. write_icc_sre_el2(read_el2_ctx_common(ctx, icc_sre_el2));
  1022. #else
  1023. u_register_t scr_el3 = read_scr_el3();
  1024. write_scr_el3(scr_el3 | SCR_NS_BIT);
  1025. isb();
  1026. write_icc_sre_el2(read_el2_ctx_common(ctx, icc_sre_el2));
  1027. write_scr_el3(scr_el3);
  1028. isb();
  1029. #endif
  1030. write_ich_hcr_el2(read_el2_ctx_common(ctx, ich_hcr_el2));
  1031. write_ich_vmcr_el2(read_el2_ctx_common(ctx, ich_vmcr_el2));
  1032. }
  1033. /* -----------------------------------------------------
  1034. * The following registers are not added:
  1035. * AMEVCNTVOFF0<n>_EL2
  1036. * AMEVCNTVOFF1<n>_EL2
  1037. * -----------------------------------------------------
  1038. */
  1039. static void el2_sysregs_context_save_common(el2_sysregs_t *ctx)
  1040. {
  1041. write_el2_ctx_common(ctx, actlr_el2, read_actlr_el2());
  1042. write_el2_ctx_common(ctx, afsr0_el2, read_afsr0_el2());
  1043. write_el2_ctx_common(ctx, afsr1_el2, read_afsr1_el2());
  1044. write_el2_ctx_common(ctx, amair_el2, read_amair_el2());
  1045. write_el2_ctx_common(ctx, cnthctl_el2, read_cnthctl_el2());
  1046. write_el2_ctx_common(ctx, cntvoff_el2, read_cntvoff_el2());
  1047. write_el2_ctx_common(ctx, cptr_el2, read_cptr_el2());
  1048. if (CTX_INCLUDE_AARCH32_REGS) {
  1049. write_el2_ctx_common(ctx, dbgvcr32_el2, read_dbgvcr32_el2());
  1050. }
  1051. write_el2_ctx_common(ctx, elr_el2, read_elr_el2());
  1052. write_el2_ctx_common(ctx, esr_el2, read_esr_el2());
  1053. write_el2_ctx_common(ctx, far_el2, read_far_el2());
  1054. write_el2_ctx_common(ctx, hacr_el2, read_hacr_el2());
  1055. write_el2_ctx_common(ctx, hcr_el2, read_hcr_el2());
  1056. write_el2_ctx_common(ctx, hpfar_el2, read_hpfar_el2());
  1057. write_el2_ctx_common(ctx, hstr_el2, read_hstr_el2());
  1058. write_el2_ctx_common(ctx, mair_el2, read_mair_el2());
  1059. write_el2_ctx_common(ctx, mdcr_el2, read_mdcr_el2());
  1060. write_el2_ctx_common(ctx, sctlr_el2, read_sctlr_el2());
  1061. write_el2_ctx_common(ctx, spsr_el2, read_spsr_el2());
  1062. write_el2_ctx_common(ctx, sp_el2, read_sp_el2());
  1063. write_el2_ctx_common(ctx, tcr_el2, read_tcr_el2());
  1064. write_el2_ctx_common(ctx, tpidr_el2, read_tpidr_el2());
  1065. write_el2_ctx_common(ctx, ttbr0_el2, read_ttbr0_el2());
  1066. write_el2_ctx_common(ctx, vbar_el2, read_vbar_el2());
  1067. write_el2_ctx_common(ctx, vmpidr_el2, read_vmpidr_el2());
  1068. write_el2_ctx_common(ctx, vpidr_el2, read_vpidr_el2());
  1069. write_el2_ctx_common(ctx, vtcr_el2, read_vtcr_el2());
  1070. write_el2_ctx_common(ctx, vttbr_el2, read_vttbr_el2());
  1071. }
  1072. static void el2_sysregs_context_restore_common(el2_sysregs_t *ctx)
  1073. {
  1074. write_actlr_el2(read_el2_ctx_common(ctx, actlr_el2));
  1075. write_afsr0_el2(read_el2_ctx_common(ctx, afsr0_el2));
  1076. write_afsr1_el2(read_el2_ctx_common(ctx, afsr1_el2));
  1077. write_amair_el2(read_el2_ctx_common(ctx, amair_el2));
  1078. write_cnthctl_el2(read_el2_ctx_common(ctx, cnthctl_el2));
  1079. write_cntvoff_el2(read_el2_ctx_common(ctx, cntvoff_el2));
  1080. write_cptr_el2(read_el2_ctx_common(ctx, cptr_el2));
  1081. if (CTX_INCLUDE_AARCH32_REGS) {
  1082. write_dbgvcr32_el2(read_el2_ctx_common(ctx, dbgvcr32_el2));
  1083. }
  1084. write_elr_el2(read_el2_ctx_common(ctx, elr_el2));
  1085. write_esr_el2(read_el2_ctx_common(ctx, esr_el2));
  1086. write_far_el2(read_el2_ctx_common(ctx, far_el2));
  1087. write_hacr_el2(read_el2_ctx_common(ctx, hacr_el2));
  1088. write_hcr_el2(read_el2_ctx_common(ctx, hcr_el2));
  1089. write_hpfar_el2(read_el2_ctx_common(ctx, hpfar_el2));
  1090. write_hstr_el2(read_el2_ctx_common(ctx, hstr_el2));
  1091. write_mair_el2(read_el2_ctx_common(ctx, mair_el2));
  1092. write_mdcr_el2(read_el2_ctx_common(ctx, mdcr_el2));
  1093. write_sctlr_el2(read_el2_ctx_common(ctx, sctlr_el2));
  1094. write_spsr_el2(read_el2_ctx_common(ctx, spsr_el2));
  1095. write_sp_el2(read_el2_ctx_common(ctx, sp_el2));
  1096. write_tcr_el2(read_el2_ctx_common(ctx, tcr_el2));
  1097. write_tpidr_el2(read_el2_ctx_common(ctx, tpidr_el2));
  1098. write_ttbr0_el2(read_el2_ctx_common(ctx, ttbr0_el2));
  1099. write_vbar_el2(read_el2_ctx_common(ctx, vbar_el2));
  1100. write_vmpidr_el2(read_el2_ctx_common(ctx, vmpidr_el2));
  1101. write_vpidr_el2(read_el2_ctx_common(ctx, vpidr_el2));
  1102. write_vtcr_el2(read_el2_ctx_common(ctx, vtcr_el2));
  1103. write_vttbr_el2(read_el2_ctx_common(ctx, vttbr_el2));
  1104. }
  1105. /*******************************************************************************
  1106. * Save EL2 sysreg context
  1107. ******************************************************************************/
  1108. void cm_el2_sysregs_context_save(uint32_t security_state)
  1109. {
  1110. cpu_context_t *ctx;
  1111. el2_sysregs_t *el2_sysregs_ctx;
  1112. ctx = cm_get_context(security_state);
  1113. assert(ctx != NULL);
  1114. el2_sysregs_ctx = get_el2_sysregs_ctx(ctx);
  1115. el2_sysregs_context_save_common(el2_sysregs_ctx);
  1116. el2_sysregs_context_save_gic(el2_sysregs_ctx);
  1117. if (is_feat_mte2_supported()) {
  1118. write_el2_ctx_mte(el2_sysregs_ctx, tfsr_el2, read_tfsr_el2());
  1119. }
  1120. #if CTX_INCLUDE_MPAM_REGS
  1121. if (is_feat_mpam_supported()) {
  1122. mpam_t *mpam_ctx = get_mpam_ctx(ctx);
  1123. el2_sysregs_context_save_mpam(mpam_ctx);
  1124. }
  1125. #endif
  1126. if (is_feat_fgt_supported()) {
  1127. el2_sysregs_context_save_fgt(el2_sysregs_ctx);
  1128. }
  1129. if (is_feat_ecv_v2_supported()) {
  1130. write_el2_ctx_ecv(el2_sysregs_ctx, cntpoff_el2, read_cntpoff_el2());
  1131. }
  1132. if (is_feat_vhe_supported()) {
  1133. write_el2_ctx_vhe(el2_sysregs_ctx, contextidr_el2,
  1134. read_contextidr_el2());
  1135. write_el2_ctx_vhe(el2_sysregs_ctx, ttbr1_el2, read_ttbr1_el2());
  1136. }
  1137. if (is_feat_ras_supported()) {
  1138. write_el2_ctx_ras(el2_sysregs_ctx, vdisr_el2, read_vdisr_el2());
  1139. write_el2_ctx_ras(el2_sysregs_ctx, vsesr_el2, read_vsesr_el2());
  1140. }
  1141. if (is_feat_nv2_supported()) {
  1142. write_el2_ctx_neve(el2_sysregs_ctx, vncr_el2, read_vncr_el2());
  1143. }
  1144. if (is_feat_trf_supported()) {
  1145. write_el2_ctx_trf(el2_sysregs_ctx, trfcr_el2, read_trfcr_el2());
  1146. }
  1147. if (is_feat_csv2_2_supported()) {
  1148. write_el2_ctx_csv2_2(el2_sysregs_ctx, scxtnum_el2,
  1149. read_scxtnum_el2());
  1150. }
  1151. if (is_feat_hcx_supported()) {
  1152. write_el2_ctx_hcx(el2_sysregs_ctx, hcrx_el2, read_hcrx_el2());
  1153. }
  1154. if (is_feat_tcr2_supported()) {
  1155. write_el2_ctx_tcr2(el2_sysregs_ctx, tcr2_el2, read_tcr2_el2());
  1156. }
  1157. if (is_feat_sxpie_supported()) {
  1158. write_el2_ctx_sxpie(el2_sysregs_ctx, pire0_el2, read_pire0_el2());
  1159. write_el2_ctx_sxpie(el2_sysregs_ctx, pir_el2, read_pir_el2());
  1160. }
  1161. if (is_feat_sxpoe_supported()) {
  1162. write_el2_ctx_sxpoe(el2_sysregs_ctx, por_el2, read_por_el2());
  1163. }
  1164. if (is_feat_s2pie_supported()) {
  1165. write_el2_ctx_s2pie(el2_sysregs_ctx, s2pir_el2, read_s2pir_el2());
  1166. }
  1167. if (is_feat_gcs_supported()) {
  1168. write_el2_ctx_gcs(el2_sysregs_ctx, gcscr_el2, read_gcspr_el2());
  1169. write_el2_ctx_gcs(el2_sysregs_ctx, gcspr_el2, read_gcscr_el2());
  1170. }
  1171. }
  1172. /*******************************************************************************
  1173. * Restore EL2 sysreg context
  1174. ******************************************************************************/
  1175. void cm_el2_sysregs_context_restore(uint32_t security_state)
  1176. {
  1177. cpu_context_t *ctx;
  1178. el2_sysregs_t *el2_sysregs_ctx;
  1179. ctx = cm_get_context(security_state);
  1180. assert(ctx != NULL);
  1181. el2_sysregs_ctx = get_el2_sysregs_ctx(ctx);
  1182. el2_sysregs_context_restore_common(el2_sysregs_ctx);
  1183. el2_sysregs_context_restore_gic(el2_sysregs_ctx);
  1184. if (is_feat_mte2_supported()) {
  1185. write_tfsr_el2(read_el2_ctx_mte(el2_sysregs_ctx, tfsr_el2));
  1186. }
  1187. #if CTX_INCLUDE_MPAM_REGS
  1188. if (is_feat_mpam_supported()) {
  1189. mpam_t *mpam_ctx = get_mpam_ctx(ctx);
  1190. el2_sysregs_context_restore_mpam(mpam_ctx);
  1191. }
  1192. #endif
  1193. if (is_feat_fgt_supported()) {
  1194. el2_sysregs_context_restore_fgt(el2_sysregs_ctx);
  1195. }
  1196. if (is_feat_ecv_v2_supported()) {
  1197. write_cntpoff_el2(read_el2_ctx_ecv(el2_sysregs_ctx, cntpoff_el2));
  1198. }
  1199. if (is_feat_vhe_supported()) {
  1200. write_contextidr_el2(read_el2_ctx_vhe(el2_sysregs_ctx,
  1201. contextidr_el2));
  1202. write_ttbr1_el2(read_el2_ctx_vhe(el2_sysregs_ctx, ttbr1_el2));
  1203. }
  1204. if (is_feat_ras_supported()) {
  1205. write_vdisr_el2(read_el2_ctx_ras(el2_sysregs_ctx, vdisr_el2));
  1206. write_vsesr_el2(read_el2_ctx_ras(el2_sysregs_ctx, vsesr_el2));
  1207. }
  1208. if (is_feat_nv2_supported()) {
  1209. write_vncr_el2(read_el2_ctx_neve(el2_sysregs_ctx, vncr_el2));
  1210. }
  1211. if (is_feat_trf_supported()) {
  1212. write_trfcr_el2(read_el2_ctx_trf(el2_sysregs_ctx, trfcr_el2));
  1213. }
  1214. if (is_feat_csv2_2_supported()) {
  1215. write_scxtnum_el2(read_el2_ctx_csv2_2(el2_sysregs_ctx,
  1216. scxtnum_el2));
  1217. }
  1218. if (is_feat_hcx_supported()) {
  1219. write_hcrx_el2(read_el2_ctx_hcx(el2_sysregs_ctx, hcrx_el2));
  1220. }
  1221. if (is_feat_tcr2_supported()) {
  1222. write_tcr2_el2(read_el2_ctx_tcr2(el2_sysregs_ctx, tcr2_el2));
  1223. }
  1224. if (is_feat_sxpie_supported()) {
  1225. write_pire0_el2(read_el2_ctx_sxpie(el2_sysregs_ctx, pire0_el2));
  1226. write_pir_el2(read_el2_ctx_sxpie(el2_sysregs_ctx, pir_el2));
  1227. }
  1228. if (is_feat_sxpoe_supported()) {
  1229. write_por_el2(read_el2_ctx_sxpoe(el2_sysregs_ctx, por_el2));
  1230. }
  1231. if (is_feat_s2pie_supported()) {
  1232. write_s2pir_el2(read_el2_ctx_s2pie(el2_sysregs_ctx, s2pir_el2));
  1233. }
  1234. if (is_feat_gcs_supported()) {
  1235. write_gcscr_el2(read_el2_ctx_gcs(el2_sysregs_ctx, gcscr_el2));
  1236. write_gcspr_el2(read_el2_ctx_gcs(el2_sysregs_ctx, gcspr_el2));
  1237. }
  1238. }
  1239. #endif /* CTX_INCLUDE_EL2_REGS */
  1240. /*******************************************************************************
  1241. * This function is used to exit to Non-secure world. If CTX_INCLUDE_EL2_REGS
  1242. * is enabled, it restores EL1 and EL2 sysreg contexts instead of directly
  1243. * updating EL1 and EL2 registers. Otherwise, it calls the generic
  1244. * cm_prepare_el3_exit function.
  1245. ******************************************************************************/
  1246. void cm_prepare_el3_exit_ns(void)
  1247. {
  1248. #if CTX_INCLUDE_EL2_REGS
  1249. #if ENABLE_ASSERTIONS
  1250. cpu_context_t *ctx = cm_get_context(NON_SECURE);
  1251. assert(ctx != NULL);
  1252. /* Assert that EL2 is used. */
  1253. u_register_t scr_el3 = read_ctx_reg(get_el3state_ctx(ctx), CTX_SCR_EL3);
  1254. assert(((scr_el3 & SCR_HCE_BIT) != 0UL) &&
  1255. (el_implemented(2U) != EL_IMPL_NONE));
  1256. #endif /* ENABLE_ASSERTIONS */
  1257. /* Restore EL2 and EL1 sysreg contexts */
  1258. cm_el2_sysregs_context_restore(NON_SECURE);
  1259. cm_el1_sysregs_context_restore(NON_SECURE);
  1260. cm_set_next_eret_context(NON_SECURE);
  1261. #else
  1262. cm_prepare_el3_exit(NON_SECURE);
  1263. #endif /* CTX_INCLUDE_EL2_REGS */
  1264. }
  1265. static void el1_sysregs_context_save(el1_sysregs_t *ctx)
  1266. {
  1267. write_ctx_reg(ctx, CTX_SPSR_EL1, read_spsr_el1());
  1268. write_ctx_reg(ctx, CTX_ELR_EL1, read_elr_el1());
  1269. #if !ERRATA_SPECULATIVE_AT
  1270. write_ctx_reg(ctx, CTX_SCTLR_EL1, read_sctlr_el1());
  1271. write_ctx_reg(ctx, CTX_TCR_EL1, read_tcr_el1());
  1272. #endif /* (!ERRATA_SPECULATIVE_AT) */
  1273. write_ctx_reg(ctx, CTX_CPACR_EL1, read_cpacr_el1());
  1274. write_ctx_reg(ctx, CTX_CSSELR_EL1, read_csselr_el1());
  1275. write_ctx_reg(ctx, CTX_SP_EL1, read_sp_el1());
  1276. write_ctx_reg(ctx, CTX_ESR_EL1, read_esr_el1());
  1277. write_ctx_reg(ctx, CTX_TTBR0_EL1, read_ttbr0_el1());
  1278. write_ctx_reg(ctx, CTX_TTBR1_EL1, read_ttbr1_el1());
  1279. write_ctx_reg(ctx, CTX_MAIR_EL1, read_mair_el1());
  1280. write_ctx_reg(ctx, CTX_AMAIR_EL1, read_amair_el1());
  1281. write_ctx_reg(ctx, CTX_ACTLR_EL1, read_actlr_el1());
  1282. write_ctx_reg(ctx, CTX_TPIDR_EL1, read_tpidr_el1());
  1283. write_ctx_reg(ctx, CTX_TPIDR_EL0, read_tpidr_el0());
  1284. write_ctx_reg(ctx, CTX_TPIDRRO_EL0, read_tpidrro_el0());
  1285. write_ctx_reg(ctx, CTX_PAR_EL1, read_par_el1());
  1286. write_ctx_reg(ctx, CTX_FAR_EL1, read_far_el1());
  1287. write_ctx_reg(ctx, CTX_AFSR0_EL1, read_afsr0_el1());
  1288. write_ctx_reg(ctx, CTX_AFSR1_EL1, read_afsr1_el1());
  1289. write_ctx_reg(ctx, CTX_CONTEXTIDR_EL1, read_contextidr_el1());
  1290. write_ctx_reg(ctx, CTX_VBAR_EL1, read_vbar_el1());
  1291. #if CTX_INCLUDE_AARCH32_REGS
  1292. write_ctx_reg(ctx, CTX_SPSR_ABT, read_spsr_abt());
  1293. write_ctx_reg(ctx, CTX_SPSR_UND, read_spsr_und());
  1294. write_ctx_reg(ctx, CTX_SPSR_IRQ, read_spsr_irq());
  1295. write_ctx_reg(ctx, CTX_SPSR_FIQ, read_spsr_fiq());
  1296. write_ctx_reg(ctx, CTX_DACR32_EL2, read_dacr32_el2());
  1297. write_ctx_reg(ctx, CTX_IFSR32_EL2, read_ifsr32_el2());
  1298. #endif /* CTX_INCLUDE_AARCH32_REGS */
  1299. #if NS_TIMER_SWITCH
  1300. write_ctx_reg(ctx, CTX_CNTP_CTL_EL0, read_cntp_ctl_el0());
  1301. write_ctx_reg(ctx, CTX_CNTP_CVAL_EL0, read_cntp_cval_el0());
  1302. write_ctx_reg(ctx, CTX_CNTV_CTL_EL0, read_cntv_ctl_el0());
  1303. write_ctx_reg(ctx, CTX_CNTV_CVAL_EL0, read_cntv_cval_el0());
  1304. write_ctx_reg(ctx, CTX_CNTKCTL_EL1, read_cntkctl_el1());
  1305. #endif /* NS_TIMER_SWITCH */
  1306. #if ENABLE_FEAT_MTE2
  1307. write_ctx_reg(ctx, CTX_TFSRE0_EL1, read_tfsre0_el1());
  1308. write_ctx_reg(ctx, CTX_TFSR_EL1, read_tfsr_el1());
  1309. write_ctx_reg(ctx, CTX_RGSR_EL1, read_rgsr_el1());
  1310. write_ctx_reg(ctx, CTX_GCR_EL1, read_gcr_el1());
  1311. #endif /* ENABLE_FEAT_MTE2 */
  1312. }
  1313. static void el1_sysregs_context_restore(el1_sysregs_t *ctx)
  1314. {
  1315. write_spsr_el1(read_ctx_reg(ctx, CTX_SPSR_EL1));
  1316. write_elr_el1(read_ctx_reg(ctx, CTX_ELR_EL1));
  1317. #if !ERRATA_SPECULATIVE_AT
  1318. write_sctlr_el1(read_ctx_reg(ctx, CTX_SCTLR_EL1));
  1319. write_tcr_el1(read_ctx_reg(ctx, CTX_TCR_EL1));
  1320. #endif /* (!ERRATA_SPECULATIVE_AT) */
  1321. write_cpacr_el1(read_ctx_reg(ctx, CTX_CPACR_EL1));
  1322. write_csselr_el1(read_ctx_reg(ctx, CTX_CSSELR_EL1));
  1323. write_sp_el1(read_ctx_reg(ctx, CTX_SP_EL1));
  1324. write_esr_el1(read_ctx_reg(ctx, CTX_ESR_EL1));
  1325. write_ttbr0_el1(read_ctx_reg(ctx, CTX_TTBR0_EL1));
  1326. write_ttbr1_el1(read_ctx_reg(ctx, CTX_TTBR1_EL1));
  1327. write_mair_el1(read_ctx_reg(ctx, CTX_MAIR_EL1));
  1328. write_amair_el1(read_ctx_reg(ctx, CTX_AMAIR_EL1));
  1329. write_actlr_el1(read_ctx_reg(ctx, CTX_ACTLR_EL1));
  1330. write_tpidr_el1(read_ctx_reg(ctx, CTX_TPIDR_EL1));
  1331. write_tpidr_el0(read_ctx_reg(ctx, CTX_TPIDR_EL0));
  1332. write_tpidrro_el0(read_ctx_reg(ctx, CTX_TPIDRRO_EL0));
  1333. write_par_el1(read_ctx_reg(ctx, CTX_PAR_EL1));
  1334. write_far_el1(read_ctx_reg(ctx, CTX_FAR_EL1));
  1335. write_afsr0_el1(read_ctx_reg(ctx, CTX_AFSR0_EL1));
  1336. write_afsr1_el1(read_ctx_reg(ctx, CTX_AFSR1_EL1));
  1337. write_contextidr_el1(read_ctx_reg(ctx, CTX_CONTEXTIDR_EL1));
  1338. write_vbar_el1(read_ctx_reg(ctx, CTX_VBAR_EL1));
  1339. #if CTX_INCLUDE_AARCH32_REGS
  1340. write_spsr_abt(read_ctx_reg(ctx, CTX_SPSR_ABT));
  1341. write_spsr_und(read_ctx_reg(ctx, CTX_SPSR_UND));
  1342. write_spsr_irq(read_ctx_reg(ctx, CTX_SPSR_IRQ));
  1343. write_spsr_fiq(read_ctx_reg(ctx, CTX_SPSR_FIQ));
  1344. write_dacr32_el2(read_ctx_reg(ctx, CTX_DACR32_EL2));
  1345. write_ifsr32_el2(read_ctx_reg(ctx, CTX_IFSR32_EL2));
  1346. #endif /* CTX_INCLUDE_AARCH32_REGS */
  1347. #if NS_TIMER_SWITCH
  1348. write_cntp_ctl_el0(read_ctx_reg(ctx, CTX_CNTP_CTL_EL0));
  1349. write_cntp_cval_el0(read_ctx_reg(ctx, CTX_CNTP_CVAL_EL0));
  1350. write_cntv_ctl_el0(read_ctx_reg(ctx, CTX_CNTV_CTL_EL0));
  1351. write_cntv_cval_el0(read_ctx_reg(ctx, CTX_CNTV_CVAL_EL0));
  1352. write_cntkctl_el1(read_ctx_reg(ctx, CTX_CNTKCTL_EL1));
  1353. #endif /* NS_TIMER_SWITCH */
  1354. #if ENABLE_FEAT_MTE2
  1355. write_tfsre0_el1(read_ctx_reg(ctx, CTX_TFSRE0_EL1));
  1356. write_tfsr_el1(read_ctx_reg(ctx, CTX_TFSR_EL1));
  1357. write_rgsr_el1(read_ctx_reg(ctx, CTX_RGSR_EL1));
  1358. write_gcr_el1(read_ctx_reg(ctx, CTX_GCR_EL1));
  1359. #endif /* ENABLE_FEAT_MTE2 */
  1360. }
  1361. /*******************************************************************************
  1362. * The next four functions are used by runtime services to save and restore
  1363. * EL1 context on the 'cpu_context' structure for the specified security
  1364. * state.
  1365. ******************************************************************************/
  1366. void cm_el1_sysregs_context_save(uint32_t security_state)
  1367. {
  1368. cpu_context_t *ctx;
  1369. ctx = cm_get_context(security_state);
  1370. assert(ctx != NULL);
  1371. el1_sysregs_context_save(get_el1_sysregs_ctx(ctx));
  1372. #if IMAGE_BL31
  1373. if (security_state == SECURE)
  1374. PUBLISH_EVENT(cm_exited_secure_world);
  1375. else
  1376. PUBLISH_EVENT(cm_exited_normal_world);
  1377. #endif
  1378. }
  1379. void cm_el1_sysregs_context_restore(uint32_t security_state)
  1380. {
  1381. cpu_context_t *ctx;
  1382. ctx = cm_get_context(security_state);
  1383. assert(ctx != NULL);
  1384. el1_sysregs_context_restore(get_el1_sysregs_ctx(ctx));
  1385. #if IMAGE_BL31
  1386. if (security_state == SECURE)
  1387. PUBLISH_EVENT(cm_entering_secure_world);
  1388. else
  1389. PUBLISH_EVENT(cm_entering_normal_world);
  1390. #endif
  1391. }
  1392. /*******************************************************************************
  1393. * This function populates ELR_EL3 member of 'cpu_context' pertaining to the
  1394. * given security state with the given entrypoint
  1395. ******************************************************************************/
  1396. void cm_set_elr_el3(uint32_t security_state, uintptr_t entrypoint)
  1397. {
  1398. cpu_context_t *ctx;
  1399. el3_state_t *state;
  1400. ctx = cm_get_context(security_state);
  1401. assert(ctx != NULL);
  1402. /* Populate EL3 state so that ERET jumps to the correct entry */
  1403. state = get_el3state_ctx(ctx);
  1404. write_ctx_reg(state, CTX_ELR_EL3, entrypoint);
  1405. }
  1406. /*******************************************************************************
  1407. * This function populates ELR_EL3 and SPSR_EL3 members of 'cpu_context'
  1408. * pertaining to the given security state
  1409. ******************************************************************************/
  1410. void cm_set_elr_spsr_el3(uint32_t security_state,
  1411. uintptr_t entrypoint, uint32_t spsr)
  1412. {
  1413. cpu_context_t *ctx;
  1414. el3_state_t *state;
  1415. ctx = cm_get_context(security_state);
  1416. assert(ctx != NULL);
  1417. /* Populate EL3 state so that ERET jumps to the correct entry */
  1418. state = get_el3state_ctx(ctx);
  1419. write_ctx_reg(state, CTX_ELR_EL3, entrypoint);
  1420. write_ctx_reg(state, CTX_SPSR_EL3, spsr);
  1421. }
  1422. /*******************************************************************************
  1423. * This function updates a single bit in the SCR_EL3 member of the 'cpu_context'
  1424. * pertaining to the given security state using the value and bit position
  1425. * specified in the parameters. It preserves all other bits.
  1426. ******************************************************************************/
  1427. void cm_write_scr_el3_bit(uint32_t security_state,
  1428. uint32_t bit_pos,
  1429. uint32_t value)
  1430. {
  1431. cpu_context_t *ctx;
  1432. el3_state_t *state;
  1433. u_register_t scr_el3;
  1434. ctx = cm_get_context(security_state);
  1435. assert(ctx != NULL);
  1436. /* Ensure that the bit position is a valid one */
  1437. assert(((1UL << bit_pos) & SCR_VALID_BIT_MASK) != 0U);
  1438. /* Ensure that the 'value' is only a bit wide */
  1439. assert(value <= 1U);
  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);
  1445. scr_el3 = read_ctx_reg(state, CTX_SCR_EL3);
  1446. scr_el3 &= ~(1UL << bit_pos);
  1447. scr_el3 |= (u_register_t)value << bit_pos;
  1448. write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
  1449. }
  1450. /*******************************************************************************
  1451. * This function retrieves SCR_EL3 member of 'cpu_context' pertaining to the
  1452. * given security state.
  1453. ******************************************************************************/
  1454. u_register_t cm_get_scr_el3(uint32_t security_state)
  1455. {
  1456. cpu_context_t *ctx;
  1457. el3_state_t *state;
  1458. ctx = cm_get_context(security_state);
  1459. assert(ctx != NULL);
  1460. /* Populate EL3 state so that ERET jumps to the correct entry */
  1461. state = get_el3state_ctx(ctx);
  1462. return read_ctx_reg(state, CTX_SCR_EL3);
  1463. }
  1464. /*******************************************************************************
  1465. * This function is used to program the context that's used for exception
  1466. * return. This initializes the SP_EL3 to a pointer to a 'cpu_context' set for
  1467. * the required security state
  1468. ******************************************************************************/
  1469. void cm_set_next_eret_context(uint32_t security_state)
  1470. {
  1471. cpu_context_t *ctx;
  1472. ctx = cm_get_context(security_state);
  1473. assert(ctx != NULL);
  1474. cm_set_next_context(ctx);
  1475. }