xlat_tables_private.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright (c) 2017, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #ifndef __XLAT_TABLES_PRIVATE_H__
  7. #define __XLAT_TABLES_PRIVATE_H__
  8. #include <cassert.h>
  9. #include <platform_def.h>
  10. #include <utils_def.h>
  11. /*
  12. * If the platform hasn't defined a physical and a virtual address space size
  13. * default to ADDR_SPACE_SIZE.
  14. */
  15. #if ERROR_DEPRECATED
  16. # ifdef ADDR_SPACE_SIZE
  17. # error "ADDR_SPACE_SIZE is deprecated. Use PLAT_xxx_ADDR_SPACE_SIZE instead."
  18. # endif
  19. #elif defined(ADDR_SPACE_SIZE)
  20. # ifndef PLAT_PHY_ADDR_SPACE_SIZE
  21. # define PLAT_PHY_ADDR_SPACE_SIZE ADDR_SPACE_SIZE
  22. # endif
  23. # ifndef PLAT_VIRT_ADDR_SPACE_SIZE
  24. # define PLAT_VIRT_ADDR_SPACE_SIZE ADDR_SPACE_SIZE
  25. # endif
  26. #endif
  27. /* The virtual and physical address space sizes must be powers of two. */
  28. CASSERT(IS_POWER_OF_TWO(PLAT_VIRT_ADDR_SPACE_SIZE),
  29. assert_valid_virt_addr_space_size);
  30. CASSERT(IS_POWER_OF_TWO(PLAT_PHY_ADDR_SPACE_SIZE),
  31. assert_valid_phy_addr_space_size);
  32. /* Struct that holds all information about the translation tables. */
  33. typedef struct {
  34. /*
  35. * Max allowed Virtual and Physical Addresses.
  36. */
  37. unsigned long long pa_max_address;
  38. uintptr_t va_max_address;
  39. /*
  40. * Array of all memory regions stored in order of ascending end address
  41. * and ascending size to simplify the code that allows overlapping
  42. * regions. The list is terminated by the first entry with size == 0.
  43. * The max size of the list is stored in `mmap_num`. `mmap` points to an
  44. * array of mmap_num + 1 elements, so that there is space for the final
  45. * null entry.
  46. */
  47. mmap_region_t *mmap;
  48. unsigned int mmap_num;
  49. /*
  50. * Array of finer-grain translation tables.
  51. * For example, if the initial lookup level is 1 then this array would
  52. * contain both level-2 and level-3 entries.
  53. */
  54. uint64_t (*tables)[XLAT_TABLE_ENTRIES];
  55. unsigned int tables_num;
  56. /*
  57. * Keep track of how many regions are mapped in each table. The base
  58. * table can't be unmapped so it isn't needed to keep track of it.
  59. */
  60. #if PLAT_XLAT_TABLES_DYNAMIC
  61. int *tables_mapped_regions;
  62. #endif /* PLAT_XLAT_TABLES_DYNAMIC */
  63. unsigned int next_table;
  64. /*
  65. * Base translation table. It doesn't need to have the same amount of
  66. * entries as the ones used for other levels.
  67. */
  68. uint64_t *base_table;
  69. unsigned int base_table_entries;
  70. /*
  71. * Max Physical and Virtual addresses currently in use by the
  72. * translation tables. These might get updated as we map/unmap memory
  73. * regions but they will never go beyond pa/va_max_address.
  74. */
  75. unsigned long long max_pa;
  76. uintptr_t max_va;
  77. /* Level of the base translation table. */
  78. unsigned int base_level;
  79. /* Set to 1 when the translation tables are initialized. */
  80. unsigned int initialized;
  81. /*
  82. * Bit mask that has to be ORed to the rest of a translation table
  83. * descriptor in order to prohibit execution of code at the exception
  84. * level of this translation context.
  85. */
  86. uint64_t execute_never_mask;
  87. } xlat_ctx_t;
  88. #if PLAT_XLAT_TABLES_DYNAMIC
  89. /*
  90. * Shifts and masks to access fields of an mmap_attr_t
  91. */
  92. /* Dynamic or static */
  93. #define MT_DYN_SHIFT 30 /* 31 would cause undefined behaviours */
  94. /*
  95. * Memory mapping private attributes
  96. *
  97. * Private attributes not exposed in the mmap_attr_t enum.
  98. */
  99. typedef enum {
  100. /*
  101. * Regions mapped before the MMU can't be unmapped dynamically (they are
  102. * static) and regions mapped with MMU enabled can be unmapped. This
  103. * behaviour can't be overridden.
  104. *
  105. * Static regions can overlap each other, dynamic regions can't.
  106. */
  107. MT_STATIC = 0 << MT_DYN_SHIFT,
  108. MT_DYNAMIC = 1 << MT_DYN_SHIFT
  109. } mmap_priv_attr_t;
  110. /*
  111. * Function used to invalidate all levels of the translation walk for a given
  112. * virtual address. It must be called for every translation table entry that is
  113. * modified.
  114. */
  115. void xlat_arch_tlbi_va(uintptr_t va);
  116. /*
  117. * This function has to be called at the end of any code that uses the function
  118. * xlat_arch_tlbi_va().
  119. */
  120. void xlat_arch_tlbi_va_sync(void);
  121. /* Add a dynamic region to the specified context. */
  122. int mmap_add_dynamic_region_ctx(xlat_ctx_t *ctx, mmap_region_t *mm);
  123. /* Remove a dynamic region from the specified context. */
  124. int mmap_remove_dynamic_region_ctx(xlat_ctx_t *ctx, uintptr_t base_va,
  125. size_t size);
  126. #endif /* PLAT_XLAT_TABLES_DYNAMIC */
  127. /* Print VA, PA, size and attributes of all regions in the mmap array. */
  128. void print_mmap(mmap_region_t *const mmap);
  129. /*
  130. * Print the current state of the translation tables by reading them from
  131. * memory.
  132. */
  133. void xlat_tables_print(xlat_ctx_t *ctx);
  134. /*
  135. * Initialize the translation tables by mapping all regions added to the
  136. * specified context.
  137. */
  138. void init_xlation_table(xlat_ctx_t *ctx);
  139. /* Add a static region to the specified context. */
  140. void mmap_add_region_ctx(xlat_ctx_t *ctx, mmap_region_t *mm);
  141. /*
  142. * Architecture-specific initialization code.
  143. */
  144. /* Returns the current Exception Level. The returned EL must be 1 or higher. */
  145. int xlat_arch_current_el(void);
  146. /*
  147. * Returns the bit mask that has to be ORed to the rest of a translation table
  148. * descriptor so that execution of code is prohibited at the given Exception
  149. * Level.
  150. */
  151. uint64_t xlat_arch_get_xn_desc(int el);
  152. /* Execute architecture-specific translation table initialization code. */
  153. void init_xlat_tables_arch(unsigned long long max_pa);
  154. /* Enable MMU and configure it to use the specified translation tables. */
  155. void enable_mmu_arch(unsigned int flags, uint64_t *base_table);
  156. /* Return 1 if the MMU of this Exception Level is enabled, 0 otherwise. */
  157. int is_mmu_enabled(void);
  158. #endif /* __XLAT_TABLES_PRIVATE_H__ */