bl1_context_mgmt.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (c) 2015-2021, 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 "../bl1_private.h"
  13. /* Following contains the cpu context pointers. */
  14. static void *bl1_cpu_context_ptr[2];
  15. entry_point_info_t *bl2_ep_info;
  16. void *cm_get_context(uint32_t security_state)
  17. {
  18. assert(sec_state_is_valid(security_state));
  19. return bl1_cpu_context_ptr[security_state];
  20. }
  21. void cm_set_context(void *context, uint32_t security_state)
  22. {
  23. assert(sec_state_is_valid(security_state));
  24. bl1_cpu_context_ptr[security_state] = context;
  25. }
  26. #if ENABLE_RME
  27. /*******************************************************************************
  28. * This function prepares the entry point information to run BL2 in Root world,
  29. * i.e. EL3, for the case when FEAT_RME is enabled.
  30. ******************************************************************************/
  31. void bl1_prepare_next_image(unsigned int image_id)
  32. {
  33. image_desc_t *bl2_desc;
  34. assert(image_id == BL2_IMAGE_ID);
  35. /* Get the image descriptor. */
  36. bl2_desc = bl1_plat_get_image_desc(BL2_IMAGE_ID);
  37. assert(bl2_desc != NULL);
  38. /* Get the entry point info. */
  39. bl2_ep_info = &bl2_desc->ep_info;
  40. bl2_ep_info->spsr = (uint32_t)SPSR_64(MODE_EL3, MODE_SP_ELX,
  41. DISABLE_ALL_EXCEPTIONS);
  42. /*
  43. * Flush cache since bl2_ep_info is accessed after MMU is disabled
  44. * before jumping to BL2.
  45. */
  46. flush_dcache_range((uintptr_t)bl2_ep_info, sizeof(entry_point_info_t));
  47. /* Indicate that image is in execution state. */
  48. bl2_desc->state = IMAGE_STATE_EXECUTED;
  49. /* Print debug info and flush the console before running BL2. */
  50. print_entry_point_info(bl2_ep_info);
  51. }
  52. #else
  53. /*******************************************************************************
  54. * This function prepares the context for Secure/Normal world images.
  55. * Normal world images are transitioned to EL2(if supported) else EL1.
  56. ******************************************************************************/
  57. void bl1_prepare_next_image(unsigned int image_id)
  58. {
  59. /*
  60. * Following array will be used for context management.
  61. * There are 2 instances, for the Secure and Non-Secure contexts.
  62. */
  63. static cpu_context_t bl1_cpu_context[2];
  64. unsigned int security_state, mode = MODE_EL1;
  65. image_desc_t *desc;
  66. entry_point_info_t *next_bl_ep;
  67. #if CTX_INCLUDE_AARCH32_REGS
  68. /*
  69. * Ensure that the build flag to save AArch32 system registers in CPU
  70. * context is not set for AArch64-only platforms.
  71. */
  72. if (el_implemented(1) == EL_IMPL_A64ONLY) {
  73. ERROR("EL1 supports AArch64-only. Please set build flag "
  74. "CTX_INCLUDE_AARCH32_REGS = 0\n");
  75. panic();
  76. }
  77. #endif
  78. /* Get the image descriptor. */
  79. desc = bl1_plat_get_image_desc(image_id);
  80. assert(desc != NULL);
  81. /* Get the entry point info. */
  82. next_bl_ep = &desc->ep_info;
  83. /* Get the image security state. */
  84. security_state = GET_SECURITY_STATE(next_bl_ep->h.attr);
  85. /* Setup the Secure/Non-Secure context if not done already. */
  86. if (cm_get_context(security_state) == NULL)
  87. cm_set_context(&bl1_cpu_context[security_state], security_state);
  88. /* Prepare the SPSR for the next BL image. */
  89. if ((security_state != SECURE) && (el_implemented(2) != EL_IMPL_NONE)) {
  90. mode = MODE_EL2;
  91. }
  92. next_bl_ep->spsr = (uint32_t)SPSR_64((uint64_t) mode,
  93. (uint64_t)MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
  94. /* Allow platform to make change */
  95. bl1_plat_set_ep_info(image_id, next_bl_ep);
  96. /* Prepare the context for the next BL image. */
  97. cm_init_my_context(next_bl_ep);
  98. cm_prepare_el3_exit(security_state);
  99. /* Indicate that image is in execution state. */
  100. desc->state = IMAGE_STATE_EXECUTED;
  101. print_entry_point_info(next_bl_ep);
  102. }
  103. #endif /* ENABLE_RME */