spm_mm_xlat.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (c) 2018-2023, 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_shim_private.h"
  17. /* Lock used for SP_MEMORY_ATTRIBUTES_GET and SP_MEMORY_ATTRIBUTES_SET */
  18. static spinlock_t mem_attr_smc_lock;
  19. /*
  20. * Attributes are encoded using a different format in the SMC interface than in
  21. * the Trusted Firmware, where the mmap_attr_t enum type is used. This function
  22. * converts an attributes value from the SMC format to the mmap_attr_t format by
  23. * setting MT_RW/MT_RO, MT_USER/MT_PRIVILEGED and MT_EXECUTE/MT_EXECUTE_NEVER.
  24. * The other fields are left as 0 because they are ignored by the function
  25. * xlat_change_mem_attributes_ctx().
  26. */
  27. static unsigned int smc_attr_to_mmap_attr(unsigned int attributes)
  28. {
  29. unsigned int tf_attr = 0U;
  30. unsigned int access = (attributes & MM_SP_MEMORY_ATTRIBUTES_ACCESS_MASK)
  31. >> MM_SP_MEMORY_ATTRIBUTES_ACCESS_SHIFT;
  32. if (access == MM_SP_MEMORY_ATTRIBUTES_ACCESS_RW) {
  33. tf_attr |= MT_RW | MT_USER;
  34. } else if (access == MM_SP_MEMORY_ATTRIBUTES_ACCESS_RO) {
  35. tf_attr |= MT_RO | MT_USER;
  36. } else {
  37. /* Other values are reserved. */
  38. assert(access == MM_SP_MEMORY_ATTRIBUTES_ACCESS_NOACCESS);
  39. /* The only requirement is that there's no access from EL0 */
  40. tf_attr |= MT_RO | MT_PRIVILEGED;
  41. }
  42. if ((attributes & MM_SP_MEMORY_ATTRIBUTES_NON_EXEC) == 0) {
  43. tf_attr |= MT_EXECUTE;
  44. } else {
  45. tf_attr |= MT_EXECUTE_NEVER;
  46. }
  47. return tf_attr;
  48. }
  49. /*
  50. * This function converts attributes from the Trusted Firmware format into the
  51. * SMC interface format.
  52. */
  53. static unsigned int smc_mmap_to_smc_attr(unsigned int attr)
  54. {
  55. unsigned int smc_attr = 0U;
  56. unsigned int data_access;
  57. if ((attr & MT_USER) == 0) {
  58. /* No access from EL0. */
  59. data_access = MM_SP_MEMORY_ATTRIBUTES_ACCESS_NOACCESS;
  60. } else {
  61. if ((attr & MT_RW) != 0) {
  62. assert(MT_TYPE(attr) != MT_DEVICE);
  63. data_access = MM_SP_MEMORY_ATTRIBUTES_ACCESS_RW;
  64. } else {
  65. data_access = MM_SP_MEMORY_ATTRIBUTES_ACCESS_RO;
  66. }
  67. }
  68. smc_attr |= (data_access & MM_SP_MEMORY_ATTRIBUTES_ACCESS_MASK)
  69. << MM_SP_MEMORY_ATTRIBUTES_ACCESS_SHIFT;
  70. if ((attr & MT_EXECUTE_NEVER) != 0U) {
  71. smc_attr |= MM_SP_MEMORY_ATTRIBUTES_NON_EXEC;
  72. }
  73. return smc_attr;
  74. }
  75. int32_t spm_memory_attributes_get_smc_handler(sp_context_t *sp_ctx,
  76. uintptr_t base_va)
  77. {
  78. uint32_t attributes;
  79. spin_lock(&mem_attr_smc_lock);
  80. int rc = xlat_get_mem_attributes_ctx(sp_ctx->xlat_ctx_handle,
  81. base_va, &attributes);
  82. spin_unlock(&mem_attr_smc_lock);
  83. /* Convert error codes of xlat_get_mem_attributes_ctx() into SPM. */
  84. assert((rc == 0) || (rc == -EINVAL));
  85. if (rc == 0) {
  86. return (int32_t) smc_mmap_to_smc_attr(attributes);
  87. } else {
  88. return SPM_MM_INVALID_PARAMETER;
  89. }
  90. }
  91. int spm_memory_attributes_set_smc_handler(sp_context_t *sp_ctx,
  92. u_register_t page_address,
  93. u_register_t pages_count,
  94. u_register_t smc_attributes)
  95. {
  96. uintptr_t base_va = (uintptr_t) page_address;
  97. size_t size = (size_t) (pages_count * PAGE_SIZE);
  98. uint32_t attributes = (uint32_t) smc_attributes;
  99. INFO(" Start address : 0x%lx\n", base_va);
  100. INFO(" Number of pages: %i (%zi bytes)\n", (int) pages_count, size);
  101. INFO(" Attributes : 0x%x\n", attributes);
  102. spin_lock(&mem_attr_smc_lock);
  103. int ret = xlat_change_mem_attributes_ctx(sp_ctx->xlat_ctx_handle,
  104. base_va, size,
  105. smc_attr_to_mmap_attr(attributes));
  106. spin_unlock(&mem_attr_smc_lock);
  107. /* Convert error codes of xlat_change_mem_attributes_ctx() into SPM. */
  108. assert((ret == 0) || (ret == -EINVAL));
  109. return (ret == 0) ? SPM_MM_SUCCESS : SPM_MM_INVALID_PARAMETER;
  110. }