tspd_common.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (c) 2013-2017, 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 <bl32/tsp/tsp.h>
  10. #include <common/bl_common.h>
  11. #include <common/debug.h>
  12. #include <lib/el3_runtime/context_mgmt.h>
  13. #include <lib/utils.h>
  14. #include "tspd_private.h"
  15. /*******************************************************************************
  16. * Given a secure payload entrypoint info pointer, entry point PC, register
  17. * width, cpu id & pointer to a context data structure, this function will
  18. * initialize tsp context and entry point info for the secure payload
  19. ******************************************************************************/
  20. void tspd_init_tsp_ep_state(struct entry_point_info *tsp_entry_point,
  21. uint32_t rw,
  22. uint64_t pc,
  23. tsp_context_t *tsp_ctx)
  24. {
  25. uint32_t ep_attr;
  26. /* Passing a NULL context is a critical programming error */
  27. assert(tsp_ctx);
  28. assert(tsp_entry_point);
  29. assert(pc);
  30. /*
  31. * We support AArch64 TSP for now.
  32. * TODO: Add support for AArch32 TSP
  33. */
  34. assert(rw == TSP_AARCH64);
  35. /* Associate this context with the cpu specified */
  36. tsp_ctx->mpidr = read_mpidr_el1();
  37. tsp_ctx->state = 0;
  38. set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_OFF);
  39. clr_yield_smc_active_flag(tsp_ctx->state);
  40. cm_set_context(&tsp_ctx->cpu_ctx, SECURE);
  41. /* initialise an entrypoint to set up the CPU context */
  42. ep_attr = SECURE | EP_ST_ENABLE;
  43. if (read_sctlr_el3() & SCTLR_EE_BIT)
  44. ep_attr |= EP_EE_BIG;
  45. SET_PARAM_HEAD(tsp_entry_point, PARAM_EP, VERSION_1, ep_attr);
  46. tsp_entry_point->pc = pc;
  47. tsp_entry_point->spsr = SPSR_64(MODE_EL1,
  48. MODE_SP_ELX,
  49. DISABLE_ALL_EXCEPTIONS);
  50. zeromem(&tsp_entry_point->args, sizeof(tsp_entry_point->args));
  51. }
  52. /*******************************************************************************
  53. * This function takes an SP context pointer and:
  54. * 1. Applies the S-EL1 system register context from tsp_ctx->cpu_ctx.
  55. * 2. Saves the current C runtime state (callee saved registers) on the stack
  56. * frame and saves a reference to this state.
  57. * 3. Calls el3_exit() so that the EL3 system and general purpose registers
  58. * from the tsp_ctx->cpu_ctx are used to enter the secure payload image.
  59. ******************************************************************************/
  60. uint64_t tspd_synchronous_sp_entry(tsp_context_t *tsp_ctx)
  61. {
  62. uint64_t rc;
  63. assert(tsp_ctx != NULL);
  64. assert(tsp_ctx->c_rt_ctx == 0);
  65. /* Apply the Secure EL1 system register context and switch to it */
  66. assert(cm_get_context(SECURE) == &tsp_ctx->cpu_ctx);
  67. cm_el1_sysregs_context_restore(SECURE);
  68. cm_set_next_eret_context(SECURE);
  69. rc = tspd_enter_sp(&tsp_ctx->c_rt_ctx);
  70. #if ENABLE_ASSERTIONS
  71. tsp_ctx->c_rt_ctx = 0;
  72. #endif
  73. return rc;
  74. }
  75. /*******************************************************************************
  76. * This function takes an SP context pointer and:
  77. * 1. Saves the S-EL1 system register context tp tsp_ctx->cpu_ctx.
  78. * 2. Restores the current C runtime state (callee saved registers) from the
  79. * stack frame using the reference to this state saved in tspd_enter_sp().
  80. * 3. It does not need to save any general purpose or EL3 system register state
  81. * as the generic smc entry routine should have saved those.
  82. ******************************************************************************/
  83. void tspd_synchronous_sp_exit(tsp_context_t *tsp_ctx, uint64_t ret)
  84. {
  85. assert(tsp_ctx != NULL);
  86. /* Save the Secure EL1 system register context */
  87. assert(cm_get_context(SECURE) == &tsp_ctx->cpu_ctx);
  88. cm_el1_sysregs_context_save(SECURE);
  89. assert(tsp_ctx->c_rt_ctx != 0);
  90. tspd_exit_sp(tsp_ctx->c_rt_ctx, ret);
  91. /* Should never reach here */
  92. assert(0);
  93. }
  94. /*******************************************************************************
  95. * This function takes an SP context pointer and abort any preempted SMC
  96. * request.
  97. * Return 1 if there was a preempted SMC request, 0 otherwise.
  98. ******************************************************************************/
  99. int tspd_abort_preempted_smc(tsp_context_t *tsp_ctx)
  100. {
  101. if (!get_yield_smc_active_flag(tsp_ctx->state))
  102. return 0;
  103. /* Abort any preempted SMC request */
  104. clr_yield_smc_active_flag(tsp_ctx->state);
  105. /*
  106. * Arrange for an entry into the test secure payload. It will
  107. * be returned via TSP_ABORT_DONE case in tspd_smc_handler.
  108. */
  109. cm_set_elr_el3(SECURE,
  110. (uint64_t) &tsp_vectors->abort_yield_smc_entry);
  111. uint64_t rc = tspd_synchronous_sp_entry(tsp_ctx);
  112. if (rc != 0)
  113. panic();
  114. return 1;
  115. }