bl1_context_mgmt.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (c) 2016-2020, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <arch_helpers.h>
  8. #include <context.h>
  9. #include <common/debug.h>
  10. #include <lib/el3_runtime/context_mgmt.h>
  11. #include <plat/common/platform.h>
  12. #include <smccc_helpers.h>
  13. #include "../bl1_private.h"
  14. /*
  15. * Following arrays will be used for context management.
  16. * There are 2 instances, for the Secure and Non-Secure contexts.
  17. */
  18. static cpu_context_t bl1_cpu_context[2];
  19. static smc_ctx_t bl1_smc_context[2];
  20. /* Following contains the next cpu context pointer. */
  21. static void *bl1_next_cpu_context_ptr;
  22. /* Following contains the next smc context pointer. */
  23. static void *bl1_next_smc_context_ptr;
  24. /* Following functions are used for SMC context handling */
  25. void *smc_get_ctx(unsigned int security_state)
  26. {
  27. assert(sec_state_is_valid(security_state));
  28. return &bl1_smc_context[security_state];
  29. }
  30. void smc_set_next_ctx(unsigned int security_state)
  31. {
  32. assert(sec_state_is_valid(security_state));
  33. bl1_next_smc_context_ptr = &bl1_smc_context[security_state];
  34. }
  35. void *smc_get_next_ctx(void)
  36. {
  37. return bl1_next_smc_context_ptr;
  38. }
  39. /* Following functions are used for CPU context handling */
  40. void *cm_get_context(uint32_t security_state)
  41. {
  42. assert(sec_state_is_valid(security_state));
  43. return &bl1_cpu_context[security_state];
  44. }
  45. void cm_set_next_context(void *context)
  46. {
  47. assert(context != NULL);
  48. bl1_next_cpu_context_ptr = context;
  49. }
  50. void *cm_get_next_context(void)
  51. {
  52. return bl1_next_cpu_context_ptr;
  53. }
  54. /*******************************************************************************
  55. * Following function copies GP regs r0-r4, lr and spsr,
  56. * from the CPU context to the SMC context structures.
  57. ******************************************************************************/
  58. static void copy_cpu_ctx_to_smc_ctx(const regs_t *cpu_reg_ctx,
  59. smc_ctx_t *next_smc_ctx)
  60. {
  61. next_smc_ctx->r0 = read_ctx_reg(cpu_reg_ctx, CTX_GPREG_R0);
  62. next_smc_ctx->r1 = read_ctx_reg(cpu_reg_ctx, CTX_GPREG_R1);
  63. next_smc_ctx->r2 = read_ctx_reg(cpu_reg_ctx, CTX_GPREG_R2);
  64. next_smc_ctx->r3 = read_ctx_reg(cpu_reg_ctx, CTX_GPREG_R3);
  65. next_smc_ctx->lr_mon = read_ctx_reg(cpu_reg_ctx, CTX_LR);
  66. next_smc_ctx->spsr_mon = read_ctx_reg(cpu_reg_ctx, CTX_SPSR);
  67. next_smc_ctx->scr = read_ctx_reg(cpu_reg_ctx, CTX_SCR);
  68. }
  69. /*******************************************************************************
  70. * Following function flushes the SMC & CPU context pointer and its data.
  71. ******************************************************************************/
  72. static void flush_smc_and_cpu_ctx(void)
  73. {
  74. flush_dcache_range((uintptr_t)&bl1_next_smc_context_ptr,
  75. sizeof(bl1_next_smc_context_ptr));
  76. flush_dcache_range((uintptr_t)bl1_next_smc_context_ptr,
  77. sizeof(smc_ctx_t));
  78. flush_dcache_range((uintptr_t)&bl1_next_cpu_context_ptr,
  79. sizeof(bl1_next_cpu_context_ptr));
  80. flush_dcache_range((uintptr_t)bl1_next_cpu_context_ptr,
  81. sizeof(cpu_context_t));
  82. }
  83. /*******************************************************************************
  84. * This function prepares the context for Secure/Normal world images.
  85. * Normal world images are transitioned to HYP(if supported) else SVC.
  86. ******************************************************************************/
  87. void bl1_prepare_next_image(unsigned int image_id)
  88. {
  89. unsigned int security_state, mode = MODE32_svc;
  90. image_desc_t *desc;
  91. entry_point_info_t *next_bl_ep;
  92. /* Get the image descriptor. */
  93. desc = bl1_plat_get_image_desc(image_id);
  94. assert(desc != NULL);
  95. /* Get the entry point info. */
  96. next_bl_ep = &desc->ep_info;
  97. /* Get the image security state. */
  98. security_state = GET_SECURITY_STATE(next_bl_ep->h.attr);
  99. /* Prepare the SPSR for the next BL image. */
  100. if ((security_state != SECURE) && (GET_VIRT_EXT(read_id_pfr1()) != 0U)) {
  101. mode = MODE32_hyp;
  102. }
  103. next_bl_ep->spsr = SPSR_MODE32(mode, SPSR_T_ARM,
  104. SPSR_E_LITTLE, DISABLE_ALL_EXCEPTIONS);
  105. /* Allow platform to make change */
  106. bl1_plat_set_ep_info(image_id, next_bl_ep);
  107. /* Prepare the cpu context for the next BL image. */
  108. cm_init_my_context(next_bl_ep);
  109. cm_prepare_el3_exit(security_state);
  110. cm_set_next_context(cm_get_context(security_state));
  111. /* Prepare the smc context for the next BL image. */
  112. smc_set_next_ctx(security_state);
  113. copy_cpu_ctx_to_smc_ctx(get_regs_ctx(cm_get_next_context()),
  114. smc_get_next_ctx());
  115. /*
  116. * If the next image is non-secure, then we need to program the banked
  117. * non secure sctlr. This is not required when the next image is secure
  118. * because in AArch32, we expect the secure world to have the same
  119. * SCTLR settings.
  120. */
  121. if (security_state == NON_SECURE) {
  122. cpu_context_t *ctx = cm_get_context(security_state);
  123. u_register_t ns_sctlr;
  124. /* Temporarily set the NS bit to access NS SCTLR */
  125. write_scr(read_scr() | SCR_NS_BIT);
  126. isb();
  127. ns_sctlr = read_ctx_reg(get_regs_ctx(ctx), CTX_NS_SCTLR);
  128. write_sctlr(ns_sctlr);
  129. isb();
  130. write_scr(read_scr() & ~SCR_NS_BIT);
  131. isb();
  132. }
  133. /*
  134. * Flush the SMC & CPU context and the (next)pointers,
  135. * to access them after caches are disabled.
  136. */
  137. flush_smc_and_cpu_ctx();
  138. /* Indicate that image is in execution state. */
  139. desc->state = IMAGE_STATE_EXECUTED;
  140. print_entry_point_info(next_bl_ep);
  141. }