tsp_context.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (c) 2024, Arm Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <arch_features.h>
  7. #include <arch_helpers.h>
  8. #include <bl32/tsp/tsp_el1_context.h>
  9. #include <common/debug.h>
  10. #define DUMMY_CTX_VALUE ULL(0xffffffff)
  11. #define DUMMY_CTX_TCR_VALUE ULL(0xffff0000)
  12. #define DUMMY_CTX_TRF_VALUE ULL(0xf)
  13. #define DUMMY_CTX_GCS_VALUE ULL(0xffff0000)
  14. #define DEFAULT_CTX_VALUE ULL(0x0)
  15. /**
  16. * -------------------------------------------------------
  17. * Private Helper functions required to access and modify
  18. * EL1 context registers at S-EL1.
  19. * -------------------------------------------------------
  20. */
  21. static void modify_el1_common_regs(uint64_t cm_value)
  22. {
  23. /**
  24. * NOTE: Few EL1 registers "SCTLR_EL1, SPSR_EL1, ELR_EL1" are
  25. * left out consciously as those are important registers for
  26. * execution in each world and overwriting them with dummy value
  27. * would cause unintended crash while executing the test.
  28. */
  29. write_tcr_el1(cm_value);
  30. write_cpacr_el1(cm_value);
  31. write_csselr_el1(cm_value);
  32. write_esr_el1(cm_value);
  33. write_ttbr0_el1(cm_value);
  34. write_ttbr1_el1(cm_value);
  35. write_mair_el1(cm_value);
  36. write_amair_el1(cm_value);
  37. write_actlr_el1(cm_value);
  38. write_tpidr_el1(cm_value);
  39. write_tpidr_el0(cm_value);
  40. write_tpidrro_el0(cm_value);
  41. write_par_el1(cm_value);
  42. write_far_el1(cm_value);
  43. write_afsr0_el1(cm_value);
  44. write_afsr1_el1(cm_value);
  45. write_contextidr_el1(cm_value);
  46. write_vbar_el1(cm_value);
  47. write_mdccint_el1(cm_value);
  48. write_mdscr_el1(cm_value);
  49. }
  50. static void modify_el1_mte2_regs(uint64_t mte_value)
  51. {
  52. if (is_feat_mte2_supported()) {
  53. write_tfsre0_el1(mte_value);
  54. write_tfsr_el1(mte_value);
  55. write_rgsr_el1(mte_value);
  56. write_gcr_el1(mte_value);
  57. }
  58. }
  59. static void modify_el1_ras_regs(uint64_t ras_value)
  60. {
  61. if (is_feat_ras_supported()) {
  62. write_disr_el1(ras_value);
  63. }
  64. }
  65. static void modify_el1_s1pie_regs(uint64_t s1pie_value)
  66. {
  67. if (is_feat_s1pie_supported()) {
  68. write_pire0_el1(s1pie_value);
  69. write_pir_el1(s1pie_value);
  70. }
  71. }
  72. static void modify_el1_s1poe_regs(uint64_t s1poe_value)
  73. {
  74. if (is_feat_s1poe_supported()) {
  75. write_por_el1(s1poe_value);
  76. }
  77. }
  78. static void modify_el1_s2poe_regs(uint64_t s2poe_value)
  79. {
  80. if (is_feat_s2poe_supported()) {
  81. write_s2por_el1(s2poe_value);
  82. }
  83. }
  84. static void modify_el1_tcr2_regs(uint64_t tcr_value)
  85. {
  86. if (is_feat_tcr2_supported()) {
  87. write_tcr2_el1(tcr_value & DUMMY_CTX_TCR_VALUE);
  88. }
  89. }
  90. static void modify_el1_trf_regs(uint64_t trf_value)
  91. {
  92. if (is_feat_trf_supported()) {
  93. write_trfcr_el1(trf_value & DUMMY_CTX_TRF_VALUE);
  94. }
  95. }
  96. static void modify_el1_gcs_regs(uint64_t gcs_value)
  97. {
  98. if (is_feat_gcs_supported()) {
  99. write_gcscr_el1(gcs_value & DUMMY_CTX_GCS_VALUE);
  100. write_gcscre0_el1(gcs_value & DUMMY_CTX_GCS_VALUE);
  101. write_gcspr_el1(gcs_value & DUMMY_CTX_GCS_VALUE);
  102. write_gcspr_el0(gcs_value & DUMMY_CTX_GCS_VALUE);
  103. }
  104. }
  105. /**
  106. * -----------------------------------------------------
  107. * Public API, to modify/restore EL1 ctx registers:
  108. * -----------------------------------------------------
  109. */
  110. void modify_el1_ctx_regs(const bool modify_option)
  111. {
  112. uint64_t mask;
  113. if (modify_option == TSP_CORRUPT_EL1_REGS) {
  114. VERBOSE("TSP(S-EL1): Corrupt EL1 Registers with Dummy values\n");
  115. mask = DUMMY_CTX_VALUE;
  116. } else {
  117. VERBOSE("TSP(S-EL1): Restore EL1 Registers with Default values\n");
  118. mask = DEFAULT_CTX_VALUE;
  119. }
  120. modify_el1_common_regs(mask);
  121. modify_el1_mte2_regs(mask);
  122. modify_el1_ras_regs(mask);
  123. modify_el1_s1pie_regs(mask);
  124. modify_el1_s1poe_regs(mask);
  125. modify_el1_s2poe_regs(mask);
  126. modify_el1_tcr2_regs(mask);
  127. modify_el1_trf_regs(mask);
  128. modify_el1_gcs_regs(mask);
  129. }