tlkd_common.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <string.h>
  8. #include <arch_helpers.h>
  9. #include <common/bl_common.h>
  10. #include <lib/el3_runtime/context_mgmt.h>
  11. #include "tlkd_private.h"
  12. #define AT_MASK 3
  13. /*******************************************************************************
  14. * This function helps the SP to translate NS/S virtual addresses.
  15. ******************************************************************************/
  16. uint64_t tlkd_va_translate(uintptr_t va, int type)
  17. {
  18. uint64_t pa;
  19. if (type & TLK_TRANSLATE_NS_VADDR) {
  20. /* save secure context */
  21. cm_el1_sysregs_context_save(SECURE);
  22. /* restore non-secure context */
  23. cm_el1_sysregs_context_restore(NON_SECURE);
  24. /* switch NS bit to start using 64-bit, non-secure mappings */
  25. write_scr(cm_get_scr_el3(NON_SECURE));
  26. isb();
  27. }
  28. int at = type & AT_MASK;
  29. switch (at) {
  30. case 0:
  31. AT(ats12e1r, va);
  32. break;
  33. case 1:
  34. AT(ats12e1w, va);
  35. break;
  36. case 2:
  37. AT(ats12e0r, va);
  38. break;
  39. case 3:
  40. AT(ats12e0w, va);
  41. break;
  42. default:
  43. assert(0); /* Unreachable */
  44. break;
  45. }
  46. /* get the (NS/S) physical address */
  47. isb();
  48. pa = read_par_el1();
  49. /* Restore secure state */
  50. if (type & TLK_TRANSLATE_NS_VADDR) {
  51. /* restore secure context */
  52. cm_el1_sysregs_context_restore(SECURE);
  53. /* switch NS bit to start using 32-bit, secure mappings */
  54. write_scr(cm_get_scr_el3(SECURE));
  55. isb();
  56. }
  57. return pa;
  58. }
  59. /*******************************************************************************
  60. * Given a secure payload entrypoint, register width, cpu id & pointer to a
  61. * context data structure, this function will create a secure context ready for
  62. * programming an entry into the secure payload.
  63. ******************************************************************************/
  64. void tlkd_init_tlk_ep_state(struct entry_point_info *tlk_entry_point,
  65. uint32_t rw,
  66. uint64_t pc,
  67. tlk_context_t *tlk_ctx)
  68. {
  69. uint32_t ep_attr, spsr;
  70. /* Passing a NULL context is a critical programming error */
  71. assert(tlk_ctx);
  72. assert(tlk_entry_point);
  73. assert(pc);
  74. /* Associate this context with the cpu specified */
  75. tlk_ctx->mpidr = read_mpidr_el1();
  76. clr_yield_smc_active_flag(tlk_ctx->state);
  77. cm_set_context(&tlk_ctx->cpu_ctx, SECURE);
  78. if (rw == SP_AARCH64)
  79. spsr = SPSR_64(MODE_EL1, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
  80. else
  81. spsr = SPSR_MODE32(MODE32_svc,
  82. SPSR_T_ARM,
  83. read_sctlr_el3() & SCTLR_EE_BIT,
  84. DISABLE_ALL_EXCEPTIONS);
  85. /* initialise an entrypoint to set up the CPU context */
  86. ep_attr = SECURE | EP_ST_ENABLE;
  87. if (read_sctlr_el3() & SCTLR_EE_BIT)
  88. ep_attr |= EP_EE_BIG;
  89. SET_PARAM_HEAD(tlk_entry_point, PARAM_EP, VERSION_1, ep_attr);
  90. tlk_entry_point->pc = pc;
  91. tlk_entry_point->spsr = spsr;
  92. }
  93. /*******************************************************************************
  94. * This function takes a TLK context pointer and:
  95. * 1. Applies the S-EL1 system register context from tlk_ctx->cpu_ctx.
  96. * 2. Saves the current C runtime state (callee saved registers) on the stack
  97. * frame and saves a reference to this state.
  98. * 3. Calls el3_exit() so that the EL3 system and general purpose registers
  99. * from the tlk_ctx->cpu_ctx are used to enter the secure payload image.
  100. ******************************************************************************/
  101. uint64_t tlkd_synchronous_sp_entry(tlk_context_t *tlk_ctx)
  102. {
  103. uint64_t rc;
  104. /* Passing a NULL context is a critical programming error */
  105. assert(tlk_ctx);
  106. /* Apply the Secure EL1 system register context and switch to it */
  107. assert(cm_get_context(SECURE) == &tlk_ctx->cpu_ctx);
  108. cm_el1_sysregs_context_restore(SECURE);
  109. cm_set_next_eret_context(SECURE);
  110. rc = tlkd_enter_sp(&tlk_ctx->c_rt_ctx);
  111. #if ENABLE_ASSERTIONS
  112. tlk_ctx->c_rt_ctx = 0;
  113. #endif
  114. return rc;
  115. }
  116. /*******************************************************************************
  117. * This function takes a TLK context pointer and:
  118. * 1. Saves the S-EL1 system register context to tlk_ctx->cpu_ctx.
  119. * 2. Restores the current C runtime state (callee saved registers) from the
  120. * stack frame using reference to this state saved in tlkd_enter_sp().
  121. * 3. It does not need to save any general purpose or EL3 system register state
  122. * as the generic smc entry routine should have saved those.
  123. ******************************************************************************/
  124. void tlkd_synchronous_sp_exit(tlk_context_t *tlk_ctx, uint64_t ret)
  125. {
  126. /* Passing a NULL context is a critical programming error */
  127. assert(tlk_ctx);
  128. /* Save the Secure EL1 system register context */
  129. assert(cm_get_context(SECURE) == &tlk_ctx->cpu_ctx);
  130. cm_el1_sysregs_context_save(SECURE);
  131. assert(tlk_ctx->c_rt_ctx != 0);
  132. tlkd_exit_sp(tlk_ctx->c_rt_ctx, ret);
  133. /* Should never reach here */
  134. assert(0);
  135. }