opteed_common.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (c) 2013-2023, 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 <lib/utils.h>
  12. #include "opteed_private.h"
  13. /*******************************************************************************
  14. * Given a OPTEE entrypoint info pointer, entry point PC, register width,
  15. * cpu id & pointer to a context data structure, this function will
  16. * initialize OPTEE context and entry point info for OPTEE.
  17. ******************************************************************************/
  18. void opteed_init_optee_ep_state(struct entry_point_info *optee_entry_point,
  19. uint32_t rw, uint64_t pc, uint64_t arg0,
  20. uint64_t arg1, uint64_t arg2, uint64_t arg3,
  21. optee_context_t *optee_ctx)
  22. {
  23. uint32_t ep_attr;
  24. /* Passing a NULL context is a critical programming error */
  25. assert(optee_ctx);
  26. assert(optee_entry_point);
  27. assert(pc);
  28. /* Associate this context with the cpu specified */
  29. optee_ctx->mpidr = read_mpidr_el1();
  30. optee_ctx->state = 0;
  31. set_optee_pstate(optee_ctx->state, OPTEE_PSTATE_OFF);
  32. cm_set_context(&optee_ctx->cpu_ctx, SECURE);
  33. /* initialise an entrypoint to set up the CPU context */
  34. ep_attr = SECURE | EP_ST_ENABLE;
  35. if (read_sctlr_el3() & SCTLR_EE_BIT)
  36. ep_attr |= EP_EE_BIG;
  37. SET_PARAM_HEAD(optee_entry_point, PARAM_EP, VERSION_1, ep_attr);
  38. optee_entry_point->pc = pc;
  39. if (rw == OPTEE_AARCH64)
  40. optee_entry_point->spsr = SPSR_64(MODE_EL1, MODE_SP_ELX,
  41. DISABLE_ALL_EXCEPTIONS);
  42. else
  43. optee_entry_point->spsr = SPSR_MODE32(MODE32_svc, SPSR_T_ARM,
  44. SPSR_E_LITTLE,
  45. DAIF_FIQ_BIT |
  46. DAIF_IRQ_BIT |
  47. DAIF_ABT_BIT);
  48. zeromem(&optee_entry_point->args, sizeof(optee_entry_point->args));
  49. optee_entry_point->args.arg0 = arg0;
  50. optee_entry_point->args.arg1 = arg1;
  51. optee_entry_point->args.arg2 = arg2;
  52. optee_entry_point->args.arg3 = arg3;
  53. }
  54. /*******************************************************************************
  55. * This function takes an OPTEE context pointer and:
  56. * 1. Applies the S-EL1 system register context from optee_ctx->cpu_ctx.
  57. * 2. Saves the current C runtime state (callee saved registers) on the stack
  58. * frame and saves a reference to this state.
  59. * 3. Calls el3_exit() so that the EL3 system and general purpose registers
  60. * from the optee_ctx->cpu_ctx are used to enter the OPTEE image.
  61. ******************************************************************************/
  62. uint64_t opteed_synchronous_sp_entry(optee_context_t *optee_ctx)
  63. {
  64. uint64_t rc;
  65. assert(optee_ctx != NULL);
  66. assert(optee_ctx->c_rt_ctx == 0);
  67. /* Apply the Secure EL1 system register context and switch to it */
  68. assert(cm_get_context(SECURE) == &optee_ctx->cpu_ctx);
  69. cm_el1_sysregs_context_restore(SECURE);
  70. cm_set_next_eret_context(SECURE);
  71. rc = opteed_enter_sp(&optee_ctx->c_rt_ctx);
  72. #if ENABLE_ASSERTIONS
  73. optee_ctx->c_rt_ctx = 0;
  74. #endif
  75. return rc;
  76. }
  77. /*******************************************************************************
  78. * This function takes an OPTEE context pointer and:
  79. * 1. Saves the S-EL1 system register context tp optee_ctx->cpu_ctx.
  80. * 2. Restores the current C runtime state (callee saved registers) from the
  81. * stack frame using the reference to this state saved in opteed_enter_sp().
  82. * 3. It does not need to save any general purpose or EL3 system register state
  83. * as the generic smc entry routine should have saved those.
  84. ******************************************************************************/
  85. void opteed_synchronous_sp_exit(optee_context_t *optee_ctx, uint64_t ret)
  86. {
  87. assert(optee_ctx != NULL);
  88. /* Save the Secure EL1 system register context */
  89. assert(cm_get_context(SECURE) == &optee_ctx->cpu_ctx);
  90. cm_el1_sysregs_context_save(SECURE);
  91. assert(optee_ctx->c_rt_ctx != 0);
  92. opteed_exit_sp(optee_ctx->c_rt_ctx, ret);
  93. /* Should never reach here */
  94. assert(0);
  95. }