pncd_common.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright (c) 2021-2022, ProvenRun S.A.S. 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 <common/debug.h>
  11. #include <lib/el3_runtime/context_mgmt.h>
  12. #include <lib/utils.h>
  13. #include <plat/common/platform.h>
  14. #include "pncd_private.h"
  15. /*******************************************************************************
  16. * Given a secure payload entrypoint info pointer, entry point PC & pointer to a
  17. * context data structure, this function will initialize pnc context and entry
  18. * point info for the secure payload
  19. ******************************************************************************/
  20. void pncd_init_pnc_ep_state(struct entry_point_info *pnc_entry_point,
  21. uint64_t pc,
  22. pnc_context_t *pnc_ctx)
  23. {
  24. uint32_t ep_attr;
  25. /* Passing a NULL context is a critical programming error */
  26. assert(pnc_ctx);
  27. assert(pnc_entry_point);
  28. assert(pc);
  29. /* Associate this context with the current cpu */
  30. pnc_ctx->mpidr = read_mpidr();
  31. cm_set_context(&pnc_ctx->cpu_ctx, SECURE);
  32. /* initialise an entrypoint to set up the CPU context */
  33. ep_attr = SECURE | EP_ST_ENABLE;
  34. if (read_sctlr_el3() & SCTLR_EE_BIT) {
  35. ep_attr |= EP_EE_BIG;
  36. }
  37. SET_PARAM_HEAD(pnc_entry_point, PARAM_EP, VERSION_1, ep_attr);
  38. pnc_entry_point->pc = pc;
  39. pnc_entry_point->spsr = SPSR_64(MODE_EL1,
  40. MODE_SP_ELX,
  41. DISABLE_ALL_EXCEPTIONS);
  42. memset(&pnc_entry_point->args, 0, sizeof(pnc_entry_point->args));
  43. }
  44. /*******************************************************************************
  45. * This function takes an SP context pointer and:
  46. * 1. Applies the S-EL1 system register context from pnc_ctx->cpu_ctx.
  47. * 2. Saves the current C runtime state (callee saved registers) on the stack
  48. * frame and saves a reference to this state.
  49. * 3. Calls el3_exit() so that the EL3 system and general purpose registers
  50. * from the pnc_ctx->cpu_ctx are used to enter the secure payload image.
  51. ******************************************************************************/
  52. uint64_t pncd_synchronous_sp_entry(pnc_context_t *pnc_ctx)
  53. {
  54. assert(pnc_ctx != NULL);
  55. assert(pnc_ctx->c_rt_ctx == 0U);
  56. /* Apply the Secure EL1 system register context and switch to it */
  57. assert(cm_get_context(SECURE) == &pnc_ctx->cpu_ctx);
  58. cm_el1_sysregs_context_restore(SECURE);
  59. #if CTX_INCLUDE_FPREGS
  60. simd_ctx_restore(SECURE);
  61. #endif
  62. cm_set_next_eret_context(SECURE);
  63. return pncd_enter_sp(&pnc_ctx->c_rt_ctx);
  64. }
  65. /*******************************************************************************
  66. * This function takes an SP context pointer and:
  67. * 1. Saves the S-EL1 system register context tp pnc_ctx->cpu_ctx.
  68. * 2. Restores the current C runtime state (callee saved registers) from the
  69. * stack frame using the reference to this state saved in pncd_enter_sp().
  70. * 3. It does not need to save any general purpose or EL3 system register state
  71. * as the generic smc entry routine should have saved those.
  72. ******************************************************************************/
  73. void pncd_synchronous_sp_exit(pnc_context_t *pnc_ctx, uint64_t ret)
  74. {
  75. assert(pnc_ctx != NULL);
  76. /* Save the Secure EL1 system register context */
  77. assert(cm_get_context(SECURE) == &pnc_ctx->cpu_ctx);
  78. cm_el1_sysregs_context_save(SECURE);
  79. #if CTX_INCLUDE_FPREGS
  80. simd_ctx_save(SECURE, false);
  81. #endif
  82. assert(pnc_ctx->c_rt_ctx != 0);
  83. pncd_exit_sp(pnc_ctx->c_rt_ctx, ret);
  84. /* Should never reach here */
  85. panic();
  86. }