context_mgmt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * Copyright (c) 2016-2024, Arm Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <stdbool.h>
  8. #include <string.h>
  9. #include <platform_def.h>
  10. #include <arch.h>
  11. #include <arch_features.h>
  12. #include <arch_helpers.h>
  13. #include <common/bl_common.h>
  14. #include <context.h>
  15. #include <lib/el3_runtime/context_mgmt.h>
  16. #include <lib/extensions/amu.h>
  17. #include <lib/extensions/pmuv3.h>
  18. #include <lib/extensions/sys_reg_trace.h>
  19. #include <lib/extensions/trf.h>
  20. #include <lib/utils.h>
  21. /*******************************************************************************
  22. * Context management library initialisation routine. This library is used by
  23. * runtime services to share pointers to 'cpu_context' structures for the secure
  24. * and non-secure states. Management of the structures and their associated
  25. * memory is not done by the context management library e.g. the PSCI service
  26. * manages the cpu context used for entry from and exit to the non-secure state.
  27. * The Secure payload manages the context(s) corresponding to the secure state.
  28. * It also uses this library to get access to the non-secure
  29. * state cpu context pointers.
  30. ******************************************************************************/
  31. void cm_init(void)
  32. {
  33. /*
  34. * The context management library has only global data to initialize, but
  35. * that will be done when the BSS is zeroed out
  36. */
  37. }
  38. /*******************************************************************************
  39. * The following function initializes the cpu_context 'ctx' for
  40. * first use, and sets the initial entrypoint state as specified by the
  41. * entry_point_info structure.
  42. *
  43. * The security state to initialize is determined by the SECURE attribute
  44. * of the entry_point_info.
  45. *
  46. * The EE and ST attributes are used to configure the endianness and secure
  47. * timer availability for the new execution context.
  48. *
  49. * To prepare the register state for entry call cm_prepare_el3_exit() and
  50. * el3_exit(). For Secure-EL1 cm_prepare_el3_exit() is equivalent to
  51. * cm_el1_sysregs_context_restore().
  52. ******************************************************************************/
  53. void cm_setup_context(cpu_context_t *ctx, const entry_point_info_t *ep)
  54. {
  55. unsigned int security_state;
  56. uint32_t scr, sctlr;
  57. regs_t *reg_ctx;
  58. assert(ctx != NULL);
  59. security_state = GET_SECURITY_STATE(ep->h.attr);
  60. /* Clear any residual register values from the context */
  61. zeromem(ctx, sizeof(*ctx));
  62. reg_ctx = get_regs_ctx(ctx);
  63. /*
  64. * Base the context SCR on the current value, adjust for entry point
  65. * specific requirements
  66. */
  67. scr = read_scr();
  68. scr &= ~(SCR_NS_BIT | SCR_HCE_BIT);
  69. if (security_state != SECURE)
  70. scr |= SCR_NS_BIT;
  71. if (security_state != SECURE) {
  72. /*
  73. * Set up SCTLR for the Non-secure context.
  74. *
  75. * SCTLR.EE: Endianness is taken from the entrypoint attributes.
  76. *
  77. * SCTLR.M, SCTLR.C and SCTLR.I: These fields must be zero (as
  78. * required by PSCI specification)
  79. *
  80. * Set remaining SCTLR fields to their architecturally defined
  81. * values. Some fields reset to an IMPLEMENTATION DEFINED value:
  82. *
  83. * SCTLR.TE: Set to zero so that exceptions to an Exception
  84. * Level executing at PL1 are taken to A32 state.
  85. *
  86. * SCTLR.V: Set to zero to select the normal exception vectors
  87. * with base address held in VBAR.
  88. */
  89. assert(((ep->spsr >> SPSR_E_SHIFT) & SPSR_E_MASK) ==
  90. (EP_GET_EE(ep->h.attr) >> EP_EE_SHIFT));
  91. sctlr = (EP_GET_EE(ep->h.attr) != 0U) ? SCTLR_EE_BIT : 0U;
  92. sctlr |= (SCTLR_RESET_VAL & ~(SCTLR_TE_BIT | SCTLR_V_BIT));
  93. write_ctx_reg(reg_ctx, CTX_NS_SCTLR, sctlr);
  94. }
  95. /*
  96. * The target exception level is based on the spsr mode requested. If
  97. * execution is requested to hyp mode, HVC is enabled via SCR.HCE.
  98. */
  99. if (GET_M32(ep->spsr) == MODE32_hyp)
  100. scr |= SCR_HCE_BIT;
  101. /*
  102. * Store the initialised values for SCTLR and SCR in the cpu_context.
  103. * The Hyp mode registers are not part of the saved context and are
  104. * set-up in cm_prepare_el3_exit().
  105. */
  106. write_ctx_reg(reg_ctx, CTX_SCR, scr);
  107. write_ctx_reg(reg_ctx, CTX_LR, ep->pc);
  108. write_ctx_reg(reg_ctx, CTX_SPSR, ep->spsr);
  109. /*
  110. * Store the r0-r3 value from the entrypoint into the context
  111. * Use memcpy as we are in control of the layout of the structures
  112. */
  113. memcpy((void *)reg_ctx, (void *)&ep->args, sizeof(aapcs32_params_t));
  114. }
  115. /*******************************************************************************
  116. * Enable architecture extensions on first entry to Non-secure world.
  117. * When EL2 is implemented but unused `el2_unused` is non-zero, otherwise
  118. * it is zero.
  119. ******************************************************************************/
  120. static void enable_extensions_nonsecure(bool el2_unused)
  121. {
  122. #if IMAGE_BL32
  123. if (is_feat_amu_supported()) {
  124. amu_enable(el2_unused);
  125. }
  126. if (is_feat_sys_reg_trace_supported()) {
  127. sys_reg_trace_init_el3();
  128. }
  129. if (is_feat_trf_supported()) {
  130. trf_init_el3();
  131. }
  132. if (is_feat_pmuv3_present()) {
  133. pmuv3_init_el3();
  134. }
  135. #endif /* IMAGE_BL32 */
  136. }
  137. #if !IMAGE_BL1
  138. /*******************************************************************************
  139. * The following function initializes the cpu_context for a CPU specified by
  140. * its `cpu_idx` for first use, and sets the initial entrypoint state as
  141. * specified by the entry_point_info structure.
  142. ******************************************************************************/
  143. void cm_init_context_by_index(unsigned int cpu_idx,
  144. const entry_point_info_t *ep)
  145. {
  146. cpu_context_t *ctx;
  147. ctx = cm_get_context_by_index(cpu_idx, GET_SECURITY_STATE(ep->h.attr));
  148. cm_setup_context(ctx, ep);
  149. }
  150. #endif /* !IMAGE_BL1 */
  151. /*******************************************************************************
  152. * The following function initializes the cpu_context for the current CPU
  153. * for first use, and sets the initial entrypoint state as specified by the
  154. * entry_point_info structure.
  155. ******************************************************************************/
  156. void cm_init_my_context(const entry_point_info_t *ep)
  157. {
  158. cpu_context_t *ctx;
  159. ctx = cm_get_context(GET_SECURITY_STATE(ep->h.attr));
  160. cm_setup_context(ctx, ep);
  161. }
  162. /*******************************************************************************
  163. * Prepare the CPU system registers for first entry into secure or normal world
  164. *
  165. * If execution is requested to hyp mode, HSCTLR is initialized
  166. * If execution is requested to non-secure PL1, and the CPU supports
  167. * HYP mode then HYP mode is disabled by configuring all necessary HYP mode
  168. * registers.
  169. ******************************************************************************/
  170. void cm_prepare_el3_exit(uint32_t security_state)
  171. {
  172. uint32_t hsctlr, scr;
  173. cpu_context_t *ctx = cm_get_context(security_state);
  174. bool el2_unused = false;
  175. assert(ctx != NULL);
  176. if (security_state == NON_SECURE) {
  177. scr = read_ctx_reg(get_regs_ctx(ctx), CTX_SCR);
  178. if ((scr & SCR_HCE_BIT) != 0U) {
  179. /* Use SCTLR value to initialize HSCTLR */
  180. hsctlr = read_ctx_reg(get_regs_ctx(ctx),
  181. CTX_NS_SCTLR);
  182. hsctlr |= HSCTLR_RES1;
  183. /* Temporarily set the NS bit to access HSCTLR */
  184. write_scr(read_scr() | SCR_NS_BIT);
  185. /*
  186. * Make sure the write to SCR is complete so that
  187. * we can access HSCTLR
  188. */
  189. isb();
  190. write_hsctlr(hsctlr);
  191. isb();
  192. write_scr(read_scr() & ~SCR_NS_BIT);
  193. isb();
  194. } else if ((read_id_pfr1() &
  195. (ID_PFR1_VIRTEXT_MASK << ID_PFR1_VIRTEXT_SHIFT)) != 0U) {
  196. el2_unused = true;
  197. /*
  198. * Set the NS bit to access NS copies of certain banked
  199. * registers
  200. */
  201. write_scr(read_scr() | SCR_NS_BIT);
  202. isb();
  203. /*
  204. * Hyp / PL2 present but unused, need to disable safely.
  205. * HSCTLR can be ignored in this case.
  206. *
  207. * Set HCR to its architectural reset value so that
  208. * Non-secure operations do not trap to Hyp mode.
  209. */
  210. write_hcr(HCR_RESET_VAL);
  211. /*
  212. * Set HCPTR to its architectural reset value so that
  213. * Non-secure access from EL1 or EL0 to trace and to
  214. * Advanced SIMD and floating point functionality does
  215. * not trap to Hyp mode.
  216. */
  217. write_hcptr(HCPTR_RESET_VAL);
  218. /*
  219. * Initialise CNTHCTL. All fields are architecturally
  220. * UNKNOWN on reset and are set to zero except for
  221. * field(s) listed below.
  222. *
  223. * CNTHCTL.PL1PCEN: Disable traps to Hyp mode of
  224. * Non-secure EL0 and EL1 accessed to the physical
  225. * timer registers.
  226. *
  227. * CNTHCTL.PL1PCTEN: Disable traps to Hyp mode of
  228. * Non-secure EL0 and EL1 accessed to the physical
  229. * counter registers.
  230. */
  231. write_cnthctl(CNTHCTL_RESET_VAL |
  232. PL1PCEN_BIT | PL1PCTEN_BIT);
  233. /*
  234. * Initialise CNTVOFF to zero as it resets to an
  235. * IMPLEMENTATION DEFINED value.
  236. */
  237. write64_cntvoff(0);
  238. /*
  239. * Set VPIDR and VMPIDR to match MIDR_EL1 and MPIDR
  240. * respectively.
  241. */
  242. write_vpidr(read_midr());
  243. write_vmpidr(read_mpidr());
  244. /*
  245. * Initialise VTTBR, setting all fields rather than
  246. * relying on the hw. Some fields are architecturally
  247. * UNKNOWN at reset.
  248. *
  249. * VTTBR.VMID: Set to zero which is the architecturally
  250. * defined reset value. Even though EL1&0 stage 2
  251. * address translation is disabled, cache maintenance
  252. * operations depend on the VMID.
  253. *
  254. * VTTBR.BADDR: Set to zero as EL1&0 stage 2 address
  255. * translation is disabled.
  256. */
  257. write64_vttbr(VTTBR_RESET_VAL &
  258. ~((VTTBR_VMID_MASK << VTTBR_VMID_SHIFT)
  259. | (VTTBR_BADDR_MASK << VTTBR_BADDR_SHIFT)));
  260. /*
  261. * Initialise HDCR, setting all the fields rather than
  262. * relying on hw.
  263. *
  264. * HDCR.HPMN: Set to value of PMCR.N which is the
  265. * architecturally-defined reset value.
  266. *
  267. * HDCR.HLP: Set to one so that event counter
  268. * overflow, that is recorded in PMOVSCLR[0-30],
  269. * occurs on the increment that changes
  270. * PMEVCNTR<n>[63] from 1 to 0, when ARMv8.5-PMU is
  271. * implemented. This bit is RES0 in versions of the
  272. * architecture earlier than ARMv8.5, setting it to 1
  273. * doesn't have any effect on them.
  274. * This bit is Reserved, UNK/SBZP in ARMv7.
  275. *
  276. * HDCR.HPME: Set to zero to disable EL2 Event
  277. * counters.
  278. */
  279. #if (ARM_ARCH_MAJOR > 7)
  280. write_hdcr((HDCR_RESET_VAL | HDCR_HLP_BIT |
  281. ((read_pmcr() & PMCR_N_BITS) >>
  282. PMCR_N_SHIFT)) & ~HDCR_HPME_BIT);
  283. #else
  284. write_hdcr((HDCR_RESET_VAL |
  285. ((read_pmcr() & PMCR_N_BITS) >>
  286. PMCR_N_SHIFT)) & ~HDCR_HPME_BIT);
  287. #endif
  288. /*
  289. * Set HSTR to its architectural reset value so that
  290. * access to system registers in the cproc=1111
  291. * encoding space do not trap to Hyp mode.
  292. */
  293. write_hstr(HSTR_RESET_VAL);
  294. /*
  295. * Set CNTHP_CTL to its architectural reset value to
  296. * disable the EL2 physical timer and prevent timer
  297. * interrupts. Some fields are architecturally UNKNOWN
  298. * on reset and are set to zero.
  299. */
  300. write_cnthp_ctl(CNTHP_CTL_RESET_VAL);
  301. isb();
  302. write_scr(read_scr() & ~SCR_NS_BIT);
  303. isb();
  304. }
  305. enable_extensions_nonsecure(el2_unused);
  306. }
  307. }
  308. /*******************************************************************************
  309. * This function is used to exit to Non-secure world. It simply calls the
  310. * cm_prepare_el3_exit function for AArch32.
  311. ******************************************************************************/
  312. void cm_prepare_el3_exit_ns(void)
  313. {
  314. cm_prepare_el3_exit(NON_SECURE);
  315. }