spm_mm_xlat.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (c) 2018-2022, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <arch.h>
  7. #include <arch_helpers.h>
  8. #include <assert.h>
  9. #include <errno.h>
  10. #include <lib/xlat_tables/xlat_tables_v2.h>
  11. #include <platform_def.h>
  12. #include <plat/common/platform.h>
  13. #include <services/spm_mm_partition.h>
  14. #include <services/spm_mm_svc.h>
  15. #include "spm_mm_private.h"
  16. #include "spm_mm_shim_private.h"
  17. /* Place translation tables by default along with the ones used by BL31. */
  18. #ifndef PLAT_SP_IMAGE_XLAT_SECTION_NAME
  19. #define PLAT_SP_IMAGE_XLAT_SECTION_NAME ".xlat_table"
  20. #endif
  21. #ifndef PLAT_SP_IMAGE_BASE_XLAT_SECTION_NAME
  22. #define PLAT_SP_IMAGE_BASE_XLAT_SECTION_NAME ".bss"
  23. #endif
  24. /* Allocate and initialise the translation context for the secure partitions. */
  25. REGISTER_XLAT_CONTEXT2(sp,
  26. PLAT_SP_IMAGE_MMAP_REGIONS,
  27. PLAT_SP_IMAGE_MAX_XLAT_TABLES,
  28. PLAT_VIRT_ADDR_SPACE_SIZE, PLAT_PHY_ADDR_SPACE_SIZE,
  29. EL1_EL0_REGIME, PLAT_SP_IMAGE_XLAT_SECTION_NAME,
  30. PLAT_SP_IMAGE_BASE_XLAT_SECTION_NAME);
  31. /* Lock used for SP_MEMORY_ATTRIBUTES_GET and SP_MEMORY_ATTRIBUTES_SET */
  32. static spinlock_t mem_attr_smc_lock;
  33. /* Get handle of Secure Partition translation context */
  34. xlat_ctx_t *spm_get_sp_xlat_context(void)
  35. {
  36. return &sp_xlat_ctx;
  37. };
  38. /*
  39. * Attributes are encoded using a different format in the SMC interface than in
  40. * the Trusted Firmware, where the mmap_attr_t enum type is used. This function
  41. * converts an attributes value from the SMC format to the mmap_attr_t format by
  42. * setting MT_RW/MT_RO, MT_USER/MT_PRIVILEGED and MT_EXECUTE/MT_EXECUTE_NEVER.
  43. * The other fields are left as 0 because they are ignored by the function
  44. * xlat_change_mem_attributes_ctx().
  45. */
  46. static unsigned int smc_attr_to_mmap_attr(unsigned int attributes)
  47. {
  48. unsigned int tf_attr = 0U;
  49. unsigned int access = (attributes & MM_SP_MEMORY_ATTRIBUTES_ACCESS_MASK)
  50. >> MM_SP_MEMORY_ATTRIBUTES_ACCESS_SHIFT;
  51. if (access == MM_SP_MEMORY_ATTRIBUTES_ACCESS_RW) {
  52. tf_attr |= MT_RW | MT_USER;
  53. } else if (access == MM_SP_MEMORY_ATTRIBUTES_ACCESS_RO) {
  54. tf_attr |= MT_RO | MT_USER;
  55. } else {
  56. /* Other values are reserved. */
  57. assert(access == MM_SP_MEMORY_ATTRIBUTES_ACCESS_NOACCESS);
  58. /* The only requirement is that there's no access from EL0 */
  59. tf_attr |= MT_RO | MT_PRIVILEGED;
  60. }
  61. if ((attributes & MM_SP_MEMORY_ATTRIBUTES_NON_EXEC) == 0) {
  62. tf_attr |= MT_EXECUTE;
  63. } else {
  64. tf_attr |= MT_EXECUTE_NEVER;
  65. }
  66. return tf_attr;
  67. }
  68. /*
  69. * This function converts attributes from the Trusted Firmware format into the
  70. * SMC interface format.
  71. */
  72. static unsigned int smc_mmap_to_smc_attr(unsigned int attr)
  73. {
  74. unsigned int smc_attr = 0U;
  75. unsigned int data_access;
  76. if ((attr & MT_USER) == 0) {
  77. /* No access from EL0. */
  78. data_access = MM_SP_MEMORY_ATTRIBUTES_ACCESS_NOACCESS;
  79. } else {
  80. if ((attr & MT_RW) != 0) {
  81. assert(MT_TYPE(attr) != MT_DEVICE);
  82. data_access = MM_SP_MEMORY_ATTRIBUTES_ACCESS_RW;
  83. } else {
  84. data_access = MM_SP_MEMORY_ATTRIBUTES_ACCESS_RO;
  85. }
  86. }
  87. smc_attr |= (data_access & MM_SP_MEMORY_ATTRIBUTES_ACCESS_MASK)
  88. << MM_SP_MEMORY_ATTRIBUTES_ACCESS_SHIFT;
  89. if ((attr & MT_EXECUTE_NEVER) != 0U) {
  90. smc_attr |= MM_SP_MEMORY_ATTRIBUTES_NON_EXEC;
  91. }
  92. return smc_attr;
  93. }
  94. int32_t spm_memory_attributes_get_smc_handler(sp_context_t *sp_ctx,
  95. uintptr_t base_va)
  96. {
  97. uint32_t attributes;
  98. spin_lock(&mem_attr_smc_lock);
  99. int rc = xlat_get_mem_attributes_ctx(sp_ctx->xlat_ctx_handle,
  100. base_va, &attributes);
  101. spin_unlock(&mem_attr_smc_lock);
  102. /* Convert error codes of xlat_get_mem_attributes_ctx() into SPM. */
  103. assert((rc == 0) || (rc == -EINVAL));
  104. if (rc == 0) {
  105. return (int32_t) smc_mmap_to_smc_attr(attributes);
  106. } else {
  107. return SPM_MM_INVALID_PARAMETER;
  108. }
  109. }
  110. int spm_memory_attributes_set_smc_handler(sp_context_t *sp_ctx,
  111. u_register_t page_address,
  112. u_register_t pages_count,
  113. u_register_t smc_attributes)
  114. {
  115. uintptr_t base_va = (uintptr_t) page_address;
  116. size_t size = (size_t) (pages_count * PAGE_SIZE);
  117. uint32_t attributes = (uint32_t) smc_attributes;
  118. INFO(" Start address : 0x%lx\n", base_va);
  119. INFO(" Number of pages: %i (%zi bytes)\n", (int) pages_count, size);
  120. INFO(" Attributes : 0x%x\n", attributes);
  121. spin_lock(&mem_attr_smc_lock);
  122. int ret = xlat_change_mem_attributes_ctx(sp_ctx->xlat_ctx_handle,
  123. base_va, size,
  124. smc_attr_to_mmap_attr(attributes));
  125. spin_unlock(&mem_attr_smc_lock);
  126. /* Convert error codes of xlat_change_mem_attributes_ctx() into SPM. */
  127. assert((ret == 0) || (ret == -EINVAL));
  128. return (ret == 0) ? SPM_MM_SUCCESS : SPM_MM_INVALID_PARAMETER;
  129. }