xlat_tables_common.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. * Copyright (c) 2016-2018, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <stdbool.h>
  8. #include <stdint.h>
  9. #include <string.h>
  10. #include <platform_def.h>
  11. #include <arch.h>
  12. #include <arch_helpers.h>
  13. #include <common/debug.h>
  14. #include <lib/cassert.h>
  15. #include <lib/utils.h>
  16. #include <lib/xlat_tables/xlat_tables.h>
  17. #include <plat/common/common_def.h>
  18. #include "xlat_tables_private.h"
  19. #if LOG_LEVEL >= LOG_LEVEL_VERBOSE
  20. #define LVL0_SPACER ""
  21. #define LVL1_SPACER " "
  22. #define LVL2_SPACER " "
  23. #define LVL3_SPACER " "
  24. #define get_level_spacer(level) \
  25. (((level) == U(0)) ? LVL0_SPACER : \
  26. (((level) == U(1)) ? LVL1_SPACER : \
  27. (((level) == U(2)) ? LVL2_SPACER : LVL3_SPACER)))
  28. #define debug_print(...) printf(__VA_ARGS__)
  29. #else
  30. #define debug_print(...) ((void)0)
  31. #endif
  32. #define UNSET_DESC ~0ULL
  33. #define MT_UNKNOWN ~0U
  34. static uint64_t xlat_tables[MAX_XLAT_TABLES][XLAT_TABLE_ENTRIES]
  35. __aligned(XLAT_TABLE_SIZE) __section("xlat_table");
  36. static unsigned int next_xlat;
  37. static unsigned long long xlat_max_pa;
  38. static uintptr_t xlat_max_va;
  39. static uint64_t execute_never_mask;
  40. static uint64_t ap1_mask;
  41. /*
  42. * Array of all memory regions stored in order of ascending base address.
  43. * The list is terminated by the first entry with size == 0.
  44. */
  45. static mmap_region_t mmap[MAX_MMAP_REGIONS + 1];
  46. void print_mmap(void)
  47. {
  48. #if LOG_LEVEL >= LOG_LEVEL_VERBOSE
  49. debug_print("mmap:\n");
  50. mmap_region_t *mm = mmap;
  51. while (mm->size != 0U) {
  52. debug_print(" VA:%p PA:0x%llx size:0x%zx attr:0x%x\n",
  53. (void *)mm->base_va, mm->base_pa,
  54. mm->size, mm->attr);
  55. ++mm;
  56. };
  57. debug_print("\n");
  58. #endif
  59. }
  60. void mmap_add_region(unsigned long long base_pa, uintptr_t base_va,
  61. size_t size, unsigned int attr)
  62. {
  63. mmap_region_t *mm = mmap;
  64. const mmap_region_t *mm_last = mm + ARRAY_SIZE(mmap) - 1U;
  65. unsigned long long end_pa = base_pa + size - 1U;
  66. uintptr_t end_va = base_va + size - 1U;
  67. assert(IS_PAGE_ALIGNED(base_pa));
  68. assert(IS_PAGE_ALIGNED(base_va));
  69. assert(IS_PAGE_ALIGNED(size));
  70. if (size == 0U)
  71. return;
  72. assert(base_pa < end_pa); /* Check for overflows */
  73. assert(base_va < end_va);
  74. assert((base_va + (uintptr_t)size - (uintptr_t)1) <=
  75. (PLAT_VIRT_ADDR_SPACE_SIZE - 1U));
  76. assert((base_pa + (unsigned long long)size - 1ULL) <=
  77. (PLAT_PHY_ADDR_SPACE_SIZE - 1U));
  78. #if ENABLE_ASSERTIONS
  79. /* Check for PAs and VAs overlaps with all other regions */
  80. for (mm = mmap; mm->size; ++mm) {
  81. uintptr_t mm_end_va = mm->base_va + mm->size - 1U;
  82. /*
  83. * Check if one of the regions is completely inside the other
  84. * one.
  85. */
  86. bool fully_overlapped_va =
  87. ((base_va >= mm->base_va) && (end_va <= mm_end_va)) ||
  88. ((mm->base_va >= base_va) && (mm_end_va <= end_va));
  89. /*
  90. * Full VA overlaps are only allowed if both regions are
  91. * identity mapped (zero offset) or have the same VA to PA
  92. * offset. Also, make sure that it's not the exact same area.
  93. */
  94. if (fully_overlapped_va) {
  95. assert((mm->base_va - mm->base_pa) ==
  96. (base_va - base_pa));
  97. assert((base_va != mm->base_va) || (size != mm->size));
  98. } else {
  99. /*
  100. * If the regions do not have fully overlapping VAs,
  101. * then they must have fully separated VAs and PAs.
  102. * Partial overlaps are not allowed
  103. */
  104. unsigned long long mm_end_pa =
  105. mm->base_pa + mm->size - 1;
  106. bool separated_pa = (end_pa < mm->base_pa) ||
  107. (base_pa > mm_end_pa);
  108. bool separated_va = (end_va < mm->base_va) ||
  109. (base_va > mm_end_va);
  110. assert(separated_va && separated_pa);
  111. }
  112. }
  113. mm = mmap; /* Restore pointer to the start of the array */
  114. #endif /* ENABLE_ASSERTIONS */
  115. /* Find correct place in mmap to insert new region */
  116. while ((mm->base_va < base_va) && (mm->size != 0U))
  117. ++mm;
  118. /*
  119. * If a section is contained inside another one with the same base
  120. * address, it must be placed after the one it is contained in:
  121. *
  122. * 1st |-----------------------|
  123. * 2nd |------------|
  124. * 3rd |------|
  125. *
  126. * This is required for mmap_region_attr() to get the attributes of the
  127. * small region correctly.
  128. */
  129. while ((mm->base_va == base_va) && (mm->size > size))
  130. ++mm;
  131. /* Make room for new region by moving other regions up by one place */
  132. (void)memmove(mm + 1, mm, (uintptr_t)mm_last - (uintptr_t)mm);
  133. /* Check we haven't lost the empty sentinal from the end of the array */
  134. assert(mm_last->size == 0U);
  135. mm->base_pa = base_pa;
  136. mm->base_va = base_va;
  137. mm->size = size;
  138. mm->attr = attr;
  139. if (end_pa > xlat_max_pa)
  140. xlat_max_pa = end_pa;
  141. if (end_va > xlat_max_va)
  142. xlat_max_va = end_va;
  143. }
  144. void mmap_add(const mmap_region_t *mm)
  145. {
  146. const mmap_region_t *mm_cursor = mm;
  147. while ((mm_cursor->size != 0U) || (mm_cursor->attr != 0U)) {
  148. mmap_add_region(mm_cursor->base_pa, mm_cursor->base_va,
  149. mm_cursor->size, mm_cursor->attr);
  150. mm_cursor++;
  151. }
  152. }
  153. static uint64_t mmap_desc(unsigned int attr, unsigned long long addr_pa,
  154. unsigned int level)
  155. {
  156. uint64_t desc;
  157. int mem_type;
  158. /* Make sure that the granularity is fine enough to map this address. */
  159. assert((addr_pa & XLAT_BLOCK_MASK(level)) == 0U);
  160. desc = addr_pa;
  161. /*
  162. * There are different translation table descriptors for level 3 and the
  163. * rest.
  164. */
  165. desc |= (level == XLAT_TABLE_LEVEL_MAX) ? PAGE_DESC : BLOCK_DESC;
  166. desc |= ((attr & MT_NS) != 0U) ? LOWER_ATTRS(NS) : 0U;
  167. desc |= ((attr & MT_RW) != 0U) ? LOWER_ATTRS(AP_RW) : LOWER_ATTRS(AP_RO);
  168. /*
  169. * Always set the access flag, as this library assumes access flag
  170. * faults aren't managed.
  171. */
  172. desc |= LOWER_ATTRS(ACCESS_FLAG);
  173. desc |= ap1_mask;
  174. /*
  175. * Deduce shareability domain and executability of the memory region
  176. * from the memory type.
  177. *
  178. * Data accesses to device memory and non-cacheable normal memory are
  179. * coherent for all observers in the system, and correspondingly are
  180. * always treated as being Outer Shareable. Therefore, for these 2 types
  181. * of memory, it is not strictly needed to set the shareability field
  182. * in the translation tables.
  183. */
  184. mem_type = MT_TYPE(attr);
  185. if (mem_type == MT_DEVICE) {
  186. desc |= LOWER_ATTRS(ATTR_DEVICE_INDEX | OSH);
  187. /*
  188. * Always map device memory as execute-never.
  189. * This is to avoid the possibility of a speculative instruction
  190. * fetch, which could be an issue if this memory region
  191. * corresponds to a read-sensitive peripheral.
  192. */
  193. desc |= execute_never_mask;
  194. } else { /* Normal memory */
  195. /*
  196. * Always map read-write normal memory as execute-never.
  197. * This library assumes that it is used by software that does
  198. * not self-modify its code, therefore R/W memory is reserved
  199. * for data storage, which must not be executable.
  200. *
  201. * Note that setting the XN bit here is for consistency only.
  202. * The function that enables the MMU sets the SCTLR_ELx.WXN bit,
  203. * which makes any writable memory region to be treated as
  204. * execute-never, regardless of the value of the XN bit in the
  205. * translation table.
  206. *
  207. * For read-only memory, rely on the MT_EXECUTE/MT_EXECUTE_NEVER
  208. * attribute to figure out the value of the XN bit.
  209. */
  210. if (((attr & MT_RW) != 0U) || ((attr & MT_EXECUTE_NEVER) != 0U)) {
  211. desc |= execute_never_mask;
  212. }
  213. if (mem_type == MT_MEMORY) {
  214. desc |= LOWER_ATTRS(ATTR_IWBWA_OWBWA_NTR_INDEX | ISH);
  215. } else {
  216. assert(mem_type == MT_NON_CACHEABLE);
  217. desc |= LOWER_ATTRS(ATTR_NON_CACHEABLE_INDEX | OSH);
  218. }
  219. }
  220. debug_print((mem_type == MT_MEMORY) ? "MEM" :
  221. ((mem_type == MT_NON_CACHEABLE) ? "NC" : "DEV"));
  222. debug_print(((attr & MT_RW) != 0U) ? "-RW" : "-RO");
  223. debug_print(((attr & MT_NS) != 0U) ? "-NS" : "-S");
  224. debug_print(((attr & MT_EXECUTE_NEVER) != 0U) ? "-XN" : "-EXEC");
  225. return desc;
  226. }
  227. /*
  228. * Look for the innermost region that contains the area at `base_va` with size
  229. * `size`. Populate *attr with the attributes of this region.
  230. *
  231. * On success, this function returns 0.
  232. * If there are partial overlaps (meaning that a smaller size is needed) or if
  233. * the region can't be found in the given area, it returns MT_UNKNOWN. In this
  234. * case the value pointed by attr should be ignored by the caller.
  235. */
  236. static unsigned int mmap_region_attr(const mmap_region_t *mm, uintptr_t base_va,
  237. size_t size, unsigned int *attr)
  238. {
  239. /* Don't assume that the area is contained in the first region */
  240. unsigned int ret = MT_UNKNOWN;
  241. /*
  242. * Get attributes from last (innermost) region that contains the
  243. * requested area. Don't stop as soon as one region doesn't contain it
  244. * because there may be other internal regions that contain this area:
  245. *
  246. * |-----------------------------1-----------------------------|
  247. * |----2----| |-------3-------| |----5----|
  248. * |--4--|
  249. *
  250. * |---| <- Area we want the attributes of.
  251. *
  252. * In this example, the area is contained in regions 1, 3 and 4 but not
  253. * in region 2. The loop shouldn't stop at region 2 as inner regions
  254. * have priority over outer regions, it should stop at region 5.
  255. */
  256. for ( ; ; ++mm) {
  257. if (mm->size == 0U)
  258. return ret; /* Reached end of list */
  259. if (mm->base_va > (base_va + size - 1U))
  260. return ret; /* Next region is after area so end */
  261. if ((mm->base_va + mm->size - 1U) < base_va)
  262. continue; /* Next region has already been overtaken */
  263. if ((ret == 0U) && (mm->attr == *attr))
  264. continue; /* Region doesn't override attribs so skip */
  265. if ((mm->base_va > base_va) ||
  266. ((mm->base_va + mm->size - 1U) < (base_va + size - 1U)))
  267. return MT_UNKNOWN; /* Region doesn't fully cover area */
  268. *attr = mm->attr;
  269. ret = 0U;
  270. }
  271. return ret;
  272. }
  273. static mmap_region_t *init_xlation_table_inner(mmap_region_t *mm,
  274. uintptr_t base_va,
  275. uint64_t *table,
  276. unsigned int level)
  277. {
  278. assert((level >= XLAT_TABLE_LEVEL_MIN) &&
  279. (level <= XLAT_TABLE_LEVEL_MAX));
  280. unsigned int level_size_shift =
  281. L0_XLAT_ADDRESS_SHIFT - level * XLAT_TABLE_ENTRIES_SHIFT;
  282. u_register_t level_size = (u_register_t)1 << level_size_shift;
  283. u_register_t level_index_mask =
  284. ((u_register_t)XLAT_TABLE_ENTRIES_MASK) << level_size_shift;
  285. debug_print("New xlat table:\n");
  286. do {
  287. uint64_t desc = UNSET_DESC;
  288. if (mm->size == 0U) {
  289. /* Done mapping regions; finish zeroing the table */
  290. desc = INVALID_DESC;
  291. } else if ((mm->base_va + mm->size - 1U) < base_va) {
  292. /* This area is after the region so get next region */
  293. ++mm;
  294. continue;
  295. }
  296. debug_print("%s VA:%p size:0x%llx ", get_level_spacer(level),
  297. (void *)base_va, (unsigned long long)level_size);
  298. if (mm->base_va > (base_va + level_size - 1U)) {
  299. /* Next region is after this area. Nothing to map yet */
  300. desc = INVALID_DESC;
  301. /* Make sure that the current level allows block descriptors */
  302. } else if (level >= XLAT_BLOCK_LEVEL_MIN) {
  303. /*
  304. * Try to get attributes of this area. It will fail if
  305. * there are partially overlapping regions. On success,
  306. * it will return the innermost region's attributes.
  307. */
  308. unsigned int attr;
  309. unsigned int r = mmap_region_attr(mm, base_va,
  310. level_size, &attr);
  311. if (r == 0U) {
  312. desc = mmap_desc(attr,
  313. base_va - mm->base_va + mm->base_pa,
  314. level);
  315. }
  316. }
  317. if (desc == UNSET_DESC) {
  318. /* Area not covered by a region so need finer table */
  319. uint64_t *new_table = xlat_tables[next_xlat];
  320. next_xlat++;
  321. assert(next_xlat <= MAX_XLAT_TABLES);
  322. desc = TABLE_DESC | (uintptr_t)new_table;
  323. /* Recurse to fill in new table */
  324. mm = init_xlation_table_inner(mm, base_va,
  325. new_table, level + 1U);
  326. }
  327. debug_print("\n");
  328. *table++ = desc;
  329. base_va += level_size;
  330. } while ((base_va & level_index_mask) &&
  331. ((base_va - 1U) < (PLAT_VIRT_ADDR_SPACE_SIZE - 1U)));
  332. return mm;
  333. }
  334. void init_xlation_table(uintptr_t base_va, uint64_t *table,
  335. unsigned int level, uintptr_t *max_va,
  336. unsigned long long *max_pa)
  337. {
  338. unsigned int el = xlat_arch_current_el();
  339. execute_never_mask = xlat_arch_get_xn_desc(el);
  340. if (el == 3U) {
  341. ap1_mask = LOWER_ATTRS(AP_ONE_VA_RANGE_RES1);
  342. } else {
  343. assert(el == 1U);
  344. ap1_mask = 0ULL;
  345. }
  346. init_xlation_table_inner(mmap, base_va, table, level);
  347. *max_va = xlat_max_va;
  348. *max_pa = xlat_max_pa;
  349. }