xlat_tables_context.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Copyright (c) 2017-2020, Arm Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <arch_helpers.h>
  7. #include <assert.h>
  8. #include <platform_def.h>
  9. #include <common/debug.h>
  10. #include <lib/xlat_tables/xlat_tables_defs.h>
  11. #include <lib/xlat_tables/xlat_tables_v2.h>
  12. #include "xlat_tables_private.h"
  13. /*
  14. * MMU configuration register values for the active translation context. Used
  15. * from the MMU assembly helpers.
  16. */
  17. uint64_t mmu_cfg_params[MMU_CFG_PARAM_MAX];
  18. /*
  19. * Allocate and initialise the default translation context for the BL image
  20. * currently executing.
  21. */
  22. REGISTER_XLAT_CONTEXT(tf, MAX_MMAP_REGIONS, MAX_XLAT_TABLES,
  23. PLAT_VIRT_ADDR_SPACE_SIZE, PLAT_PHY_ADDR_SPACE_SIZE);
  24. void mmap_add_region(unsigned long long base_pa, uintptr_t base_va, size_t size,
  25. unsigned int attr)
  26. {
  27. mmap_region_t mm = MAP_REGION(base_pa, base_va, size, attr);
  28. mmap_add_region_ctx(&tf_xlat_ctx, &mm);
  29. }
  30. void mmap_add(const mmap_region_t *mm)
  31. {
  32. mmap_add_ctx(&tf_xlat_ctx, mm);
  33. }
  34. void mmap_add_region_alloc_va(unsigned long long base_pa, uintptr_t *base_va,
  35. size_t size, unsigned int attr)
  36. {
  37. mmap_region_t mm = MAP_REGION_ALLOC_VA(base_pa, size, attr);
  38. mmap_add_region_alloc_va_ctx(&tf_xlat_ctx, &mm);
  39. *base_va = mm.base_va;
  40. }
  41. void mmap_add_alloc_va(mmap_region_t *mm)
  42. {
  43. while (mm->granularity != 0U) {
  44. assert(mm->base_va == 0U);
  45. mmap_add_region_alloc_va_ctx(&tf_xlat_ctx, mm);
  46. mm++;
  47. }
  48. }
  49. #if PLAT_XLAT_TABLES_DYNAMIC
  50. int mmap_add_dynamic_region(unsigned long long base_pa, uintptr_t base_va,
  51. size_t size, unsigned int attr)
  52. {
  53. mmap_region_t mm = MAP_REGION(base_pa, base_va, size, attr);
  54. return mmap_add_dynamic_region_ctx(&tf_xlat_ctx, &mm);
  55. }
  56. int mmap_add_dynamic_region_alloc_va(unsigned long long base_pa,
  57. uintptr_t *base_va, size_t size,
  58. unsigned int attr)
  59. {
  60. mmap_region_t mm = MAP_REGION_ALLOC_VA(base_pa, size, attr);
  61. int rc = mmap_add_dynamic_region_alloc_va_ctx(&tf_xlat_ctx, &mm);
  62. *base_va = mm.base_va;
  63. return rc;
  64. }
  65. int mmap_remove_dynamic_region(uintptr_t base_va, size_t size)
  66. {
  67. return mmap_remove_dynamic_region_ctx(&tf_xlat_ctx,
  68. base_va, size);
  69. }
  70. #endif /* PLAT_XLAT_TABLES_DYNAMIC */
  71. void __init init_xlat_tables(void)
  72. {
  73. assert(tf_xlat_ctx.xlat_regime == EL_REGIME_INVALID);
  74. unsigned int current_el = xlat_arch_current_el();
  75. if (current_el == 1U) {
  76. tf_xlat_ctx.xlat_regime = EL1_EL0_REGIME;
  77. } else if (current_el == 2U) {
  78. tf_xlat_ctx.xlat_regime = EL2_REGIME;
  79. } else {
  80. assert(current_el == 3U);
  81. tf_xlat_ctx.xlat_regime = EL3_REGIME;
  82. }
  83. init_xlat_tables_ctx(&tf_xlat_ctx);
  84. }
  85. int xlat_get_mem_attributes(uintptr_t base_va, uint32_t *attr)
  86. {
  87. return xlat_get_mem_attributes_ctx(&tf_xlat_ctx, base_va, attr);
  88. }
  89. int xlat_change_mem_attributes(uintptr_t base_va, size_t size, uint32_t attr)
  90. {
  91. return xlat_change_mem_attributes_ctx(&tf_xlat_ctx, base_va, size, attr);
  92. }
  93. #if PLAT_RO_XLAT_TABLES
  94. /* Change the memory attributes of the descriptors which resolve the address
  95. * range that belongs to the translation tables themselves, which are by default
  96. * mapped as part of read-write data in the BL image's memory.
  97. *
  98. * Since the translation tables map themselves via these level 3 (page)
  99. * descriptors, any change applied to them with the MMU on would introduce a
  100. * chicken and egg problem because of the break-before-make sequence.
  101. * Eventually, it would reach the descriptor that resolves the very table it
  102. * belongs to and the invalidation (break step) would cause the subsequent write
  103. * (make step) to it to generate an MMU fault. Therefore, the MMU is disabled
  104. * before making the change.
  105. *
  106. * No assumption is made about what data this function needs, therefore all the
  107. * caches are flushed in order to ensure coherency. A future optimization would
  108. * be to only flush the required data to main memory.
  109. */
  110. int xlat_make_tables_readonly(void)
  111. {
  112. assert(tf_xlat_ctx.initialized == true);
  113. #ifdef __aarch64__
  114. if (tf_xlat_ctx.xlat_regime == EL1_EL0_REGIME) {
  115. disable_mmu_el1();
  116. } else if (tf_xlat_ctx.xlat_regime == EL3_REGIME) {
  117. disable_mmu_el3();
  118. } else {
  119. assert(tf_xlat_ctx.xlat_regime == EL2_REGIME);
  120. return -1;
  121. }
  122. /* Flush all caches. */
  123. dcsw_op_all(DCCISW);
  124. #else /* !__aarch64__ */
  125. assert(tf_xlat_ctx.xlat_regime == EL1_EL0_REGIME);
  126. /* On AArch32, we flush the caches before disabling the MMU. The reason
  127. * for this is that the dcsw_op_all AArch32 function pushes some
  128. * registers onto the stack under the assumption that it is writing to
  129. * cache, which is not true with the MMU off. This would result in the
  130. * stack becoming corrupted and a wrong/junk value for the LR being
  131. * restored at the end of the routine.
  132. */
  133. dcsw_op_all(DC_OP_CISW);
  134. disable_mmu_secure();
  135. #endif
  136. int rc = xlat_change_mem_attributes_ctx(&tf_xlat_ctx,
  137. (uintptr_t)tf_xlat_ctx.tables,
  138. tf_xlat_ctx.tables_num * XLAT_TABLE_SIZE,
  139. MT_RO_DATA | MT_SECURE);
  140. #ifdef __aarch64__
  141. if (tf_xlat_ctx.xlat_regime == EL1_EL0_REGIME) {
  142. enable_mmu_el1(0U);
  143. } else {
  144. assert(tf_xlat_ctx.xlat_regime == EL3_REGIME);
  145. enable_mmu_el3(0U);
  146. }
  147. #else /* !__aarch64__ */
  148. enable_mmu_svc_mon(0U);
  149. #endif
  150. if (rc == 0) {
  151. tf_xlat_ctx.readonly_tables = true;
  152. }
  153. return rc;
  154. }
  155. #endif /* PLAT_RO_XLAT_TABLES */
  156. /*
  157. * If dynamic allocation of new regions is disabled then by the time we call the
  158. * function enabling the MMU, we'll have registered all the memory regions to
  159. * map for the system's lifetime. Therefore, at this point we know the maximum
  160. * physical address that will ever be mapped.
  161. *
  162. * If dynamic allocation is enabled then we can't make any such assumption
  163. * because the maximum physical address could get pushed while adding a new
  164. * region. Therefore, in this case we have to assume that the whole address
  165. * space size might be mapped.
  166. */
  167. #if PLAT_XLAT_TABLES_DYNAMIC
  168. #define MAX_PHYS_ADDR tf_xlat_ctx.pa_max_address
  169. #else
  170. #define MAX_PHYS_ADDR tf_xlat_ctx.max_pa
  171. #endif
  172. #ifdef __aarch64__
  173. void enable_mmu_el1(unsigned int flags)
  174. {
  175. setup_mmu_cfg((uint64_t *)&mmu_cfg_params, flags,
  176. tf_xlat_ctx.base_table, MAX_PHYS_ADDR,
  177. tf_xlat_ctx.va_max_address, EL1_EL0_REGIME);
  178. enable_mmu_direct_el1(flags);
  179. }
  180. void enable_mmu_el2(unsigned int flags)
  181. {
  182. setup_mmu_cfg((uint64_t *)&mmu_cfg_params, flags,
  183. tf_xlat_ctx.base_table, MAX_PHYS_ADDR,
  184. tf_xlat_ctx.va_max_address, EL2_REGIME);
  185. enable_mmu_direct_el2(flags);
  186. }
  187. void enable_mmu_el3(unsigned int flags)
  188. {
  189. setup_mmu_cfg((uint64_t *)&mmu_cfg_params, flags,
  190. tf_xlat_ctx.base_table, MAX_PHYS_ADDR,
  191. tf_xlat_ctx.va_max_address, EL3_REGIME);
  192. enable_mmu_direct_el3(flags);
  193. }
  194. void enable_mmu(unsigned int flags)
  195. {
  196. switch (get_current_el_maybe_constant()) {
  197. case 1:
  198. enable_mmu_el1(flags);
  199. break;
  200. case 2:
  201. enable_mmu_el2(flags);
  202. break;
  203. case 3:
  204. enable_mmu_el3(flags);
  205. break;
  206. default:
  207. panic();
  208. }
  209. }
  210. #else /* !__aarch64__ */
  211. void enable_mmu_svc_mon(unsigned int flags)
  212. {
  213. setup_mmu_cfg((uint64_t *)&mmu_cfg_params, flags,
  214. tf_xlat_ctx.base_table, MAX_PHYS_ADDR,
  215. tf_xlat_ctx.va_max_address, EL1_EL0_REGIME);
  216. enable_mmu_direct_svc_mon(flags);
  217. }
  218. void enable_mmu_hyp(unsigned int flags)
  219. {
  220. setup_mmu_cfg((uint64_t *)&mmu_cfg_params, flags,
  221. tf_xlat_ctx.base_table, MAX_PHYS_ADDR,
  222. tf_xlat_ctx.va_max_address, EL2_REGIME);
  223. enable_mmu_direct_hyp(flags);
  224. }
  225. #endif /* __aarch64__ */