xlat_tables_utils.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /*
  2. * Copyright (c) 2017-2021, Arm Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <inttypes.h>
  9. #include <stdbool.h>
  10. #include <stdint.h>
  11. #include <stdio.h>
  12. #include <platform_def.h>
  13. #include <arch_helpers.h>
  14. #include <common/debug.h>
  15. #include <lib/utils_def.h>
  16. #include <lib/xlat_tables/xlat_tables_defs.h>
  17. #include <lib/xlat_tables/xlat_tables_v2.h>
  18. #include "xlat_tables_private.h"
  19. #if LOG_LEVEL < LOG_LEVEL_VERBOSE
  20. void xlat_mmap_print(__unused const mmap_region_t *mmap)
  21. {
  22. /* Empty */
  23. }
  24. void xlat_tables_print(__unused xlat_ctx_t *ctx)
  25. {
  26. /* Empty */
  27. }
  28. #else /* if LOG_LEVEL >= LOG_LEVEL_VERBOSE */
  29. void xlat_mmap_print(const mmap_region_t *mmap)
  30. {
  31. printf("mmap:\n");
  32. const mmap_region_t *mm = mmap;
  33. while (mm->size != 0U) {
  34. printf(" VA:0x%lx PA:0x%llx size:0x%zx attr:0x%x granularity:0x%zx\n",
  35. mm->base_va, mm->base_pa, mm->size, mm->attr,
  36. mm->granularity);
  37. ++mm;
  38. };
  39. printf("\n");
  40. }
  41. /* Print the attributes of the specified block descriptor. */
  42. static void xlat_desc_print(const xlat_ctx_t *ctx, uint64_t desc)
  43. {
  44. uint64_t mem_type_index = ATTR_INDEX_GET(desc);
  45. int xlat_regime = ctx->xlat_regime;
  46. if (mem_type_index == ATTR_IWBWA_OWBWA_NTR_INDEX) {
  47. printf("MEM");
  48. } else if (mem_type_index == ATTR_NON_CACHEABLE_INDEX) {
  49. printf("NC");
  50. } else {
  51. assert(mem_type_index == ATTR_DEVICE_INDEX);
  52. printf("DEV");
  53. }
  54. if ((xlat_regime == EL3_REGIME) || (xlat_regime == EL2_REGIME)) {
  55. /* For EL3 and EL2 only check the AP[2] and XN bits. */
  56. printf(((desc & LOWER_ATTRS(AP_RO)) != 0ULL) ? "-RO" : "-RW");
  57. printf(((desc & UPPER_ATTRS(XN)) != 0ULL) ? "-XN" : "-EXEC");
  58. } else {
  59. assert(xlat_regime == EL1_EL0_REGIME);
  60. /*
  61. * For EL0 and EL1:
  62. * - In AArch64 PXN and UXN can be set independently but in
  63. * AArch32 there is no UXN (XN affects both privilege levels).
  64. * For consistency, we set them simultaneously in both cases.
  65. * - RO and RW permissions must be the same in EL1 and EL0. If
  66. * EL0 can access that memory region, so can EL1, with the
  67. * same permissions.
  68. */
  69. #if ENABLE_ASSERTIONS
  70. uint64_t xn_mask = xlat_arch_regime_get_xn_desc(EL1_EL0_REGIME);
  71. uint64_t xn_perm = desc & xn_mask;
  72. assert((xn_perm == xn_mask) || (xn_perm == 0ULL));
  73. #endif
  74. printf(((desc & LOWER_ATTRS(AP_RO)) != 0ULL) ? "-RO" : "-RW");
  75. /* Only check one of PXN and UXN, the other one is the same. */
  76. printf(((desc & UPPER_ATTRS(PXN)) != 0ULL) ? "-XN" : "-EXEC");
  77. /*
  78. * Privileged regions can only be accessed from EL1, user
  79. * regions can be accessed from EL1 and EL0.
  80. */
  81. printf(((desc & LOWER_ATTRS(AP_ACCESS_UNPRIVILEGED)) != 0ULL)
  82. ? "-USER" : "-PRIV");
  83. }
  84. #if ENABLE_RME
  85. switch (desc & LOWER_ATTRS(EL3_S1_NSE | NS)) {
  86. case 0ULL:
  87. printf("-S");
  88. break;
  89. case LOWER_ATTRS(NS):
  90. printf("-NS");
  91. break;
  92. case LOWER_ATTRS(EL3_S1_NSE):
  93. printf("-RT");
  94. break;
  95. default: /* LOWER_ATTRS(EL3_S1_NSE | NS) */
  96. printf("-RL");
  97. }
  98. #else
  99. printf(((LOWER_ATTRS(NS) & desc) != 0ULL) ? "-NS" : "-S");
  100. #endif
  101. #ifdef __aarch64__
  102. /* Check Guarded Page bit */
  103. if ((desc & GP) != 0ULL) {
  104. printf("-GP");
  105. }
  106. #endif
  107. }
  108. static const char * const level_spacers[] = {
  109. "[LV0] ",
  110. " [LV1] ",
  111. " [LV2] ",
  112. " [LV3] "
  113. };
  114. static const char *invalid_descriptors_ommited =
  115. "%s(%d invalid descriptors omitted)\n";
  116. /*
  117. * Recursive function that reads the translation tables passed as an argument
  118. * and prints their status.
  119. */
  120. static void xlat_tables_print_internal(xlat_ctx_t *ctx, uintptr_t table_base_va,
  121. const uint64_t *table_base, unsigned int table_entries,
  122. unsigned int level)
  123. {
  124. assert(level <= XLAT_TABLE_LEVEL_MAX);
  125. uint64_t desc;
  126. uintptr_t table_idx_va = table_base_va;
  127. unsigned int table_idx = 0U;
  128. size_t level_size = XLAT_BLOCK_SIZE(level);
  129. /*
  130. * Keep track of how many invalid descriptors are counted in a row.
  131. * Whenever multiple invalid descriptors are found, only the first one
  132. * is printed, and a line is added to inform about how many descriptors
  133. * have been omitted.
  134. */
  135. int invalid_row_count = 0;
  136. while (table_idx < table_entries) {
  137. desc = table_base[table_idx];
  138. if ((desc & DESC_MASK) == INVALID_DESC) {
  139. if (invalid_row_count == 0) {
  140. printf("%sVA:0x%lx size:0x%zx\n",
  141. level_spacers[level],
  142. table_idx_va, level_size);
  143. }
  144. invalid_row_count++;
  145. } else {
  146. if (invalid_row_count > 1) {
  147. printf(invalid_descriptors_ommited,
  148. level_spacers[level],
  149. invalid_row_count - 1);
  150. }
  151. invalid_row_count = 0;
  152. /*
  153. * Check if this is a table or a block. Tables are only
  154. * allowed in levels other than 3, but DESC_PAGE has the
  155. * same value as DESC_TABLE, so we need to check.
  156. */
  157. if (((desc & DESC_MASK) == TABLE_DESC) &&
  158. (level < XLAT_TABLE_LEVEL_MAX)) {
  159. /*
  160. * Do not print any PA for a table descriptor,
  161. * as it doesn't directly map physical memory
  162. * but instead points to the next translation
  163. * table in the translation table walk.
  164. */
  165. printf("%sVA:0x%lx size:0x%zx\n",
  166. level_spacers[level],
  167. table_idx_va, level_size);
  168. uintptr_t addr_inner = desc & TABLE_ADDR_MASK;
  169. xlat_tables_print_internal(ctx, table_idx_va,
  170. (uint64_t *)addr_inner,
  171. XLAT_TABLE_ENTRIES, level + 1U);
  172. } else {
  173. printf("%sVA:0x%lx PA:0x%" PRIx64 " size:0x%zx ",
  174. level_spacers[level], table_idx_va,
  175. (uint64_t)(desc & TABLE_ADDR_MASK),
  176. level_size);
  177. xlat_desc_print(ctx, desc);
  178. printf("\n");
  179. }
  180. }
  181. table_idx++;
  182. table_idx_va += level_size;
  183. }
  184. if (invalid_row_count > 1) {
  185. printf(invalid_descriptors_ommited,
  186. level_spacers[level], invalid_row_count - 1);
  187. }
  188. }
  189. void xlat_tables_print(xlat_ctx_t *ctx)
  190. {
  191. const char *xlat_regime_str;
  192. int used_page_tables;
  193. if (ctx->xlat_regime == EL1_EL0_REGIME) {
  194. xlat_regime_str = "1&0";
  195. } else if (ctx->xlat_regime == EL2_REGIME) {
  196. xlat_regime_str = "2";
  197. } else {
  198. assert(ctx->xlat_regime == EL3_REGIME);
  199. xlat_regime_str = "3";
  200. }
  201. VERBOSE("Translation tables state:\n");
  202. VERBOSE(" Xlat regime: EL%s\n", xlat_regime_str);
  203. VERBOSE(" Max allowed PA: 0x%llx\n", ctx->pa_max_address);
  204. VERBOSE(" Max allowed VA: 0x%lx\n", ctx->va_max_address);
  205. VERBOSE(" Max mapped PA: 0x%llx\n", ctx->max_pa);
  206. VERBOSE(" Max mapped VA: 0x%lx\n", ctx->max_va);
  207. VERBOSE(" Initial lookup level: %u\n", ctx->base_level);
  208. VERBOSE(" Entries @initial lookup level: %u\n",
  209. ctx->base_table_entries);
  210. #if PLAT_XLAT_TABLES_DYNAMIC
  211. used_page_tables = 0;
  212. for (int i = 0; i < ctx->tables_num; ++i) {
  213. if (ctx->tables_mapped_regions[i] != 0)
  214. ++used_page_tables;
  215. }
  216. #else
  217. used_page_tables = ctx->next_table;
  218. #endif
  219. VERBOSE(" Used %d sub-tables out of %d (spare: %d)\n",
  220. used_page_tables, ctx->tables_num,
  221. ctx->tables_num - used_page_tables);
  222. xlat_tables_print_internal(ctx, 0U, ctx->base_table,
  223. ctx->base_table_entries, ctx->base_level);
  224. }
  225. #endif /* LOG_LEVEL >= LOG_LEVEL_VERBOSE */
  226. /*
  227. * Do a translation table walk to find the block or page descriptor that maps
  228. * virtual_addr.
  229. *
  230. * On success, return the address of the descriptor within the translation
  231. * table. Its lookup level is stored in '*out_level'.
  232. * On error, return NULL.
  233. *
  234. * xlat_table_base
  235. * Base address for the initial lookup level.
  236. * xlat_table_base_entries
  237. * Number of entries in the translation table for the initial lookup level.
  238. * virt_addr_space_size
  239. * Size in bytes of the virtual address space.
  240. */
  241. static uint64_t *find_xlat_table_entry(uintptr_t virtual_addr,
  242. void *xlat_table_base,
  243. unsigned int xlat_table_base_entries,
  244. unsigned long long virt_addr_space_size,
  245. unsigned int *out_level)
  246. {
  247. unsigned int start_level;
  248. uint64_t *table;
  249. unsigned int entries;
  250. start_level = GET_XLAT_TABLE_LEVEL_BASE(virt_addr_space_size);
  251. table = xlat_table_base;
  252. entries = xlat_table_base_entries;
  253. for (unsigned int level = start_level;
  254. level <= XLAT_TABLE_LEVEL_MAX;
  255. ++level) {
  256. uint64_t idx, desc, desc_type;
  257. idx = XLAT_TABLE_IDX(virtual_addr, level);
  258. if (idx >= entries) {
  259. WARN("Missing xlat table entry at address 0x%lx\n",
  260. virtual_addr);
  261. return NULL;
  262. }
  263. desc = table[idx];
  264. desc_type = desc & DESC_MASK;
  265. if (desc_type == INVALID_DESC) {
  266. VERBOSE("Invalid entry (memory not mapped)\n");
  267. return NULL;
  268. }
  269. if (level == XLAT_TABLE_LEVEL_MAX) {
  270. /*
  271. * Only page descriptors allowed at the final lookup
  272. * level.
  273. */
  274. assert(desc_type == PAGE_DESC);
  275. *out_level = level;
  276. return &table[idx];
  277. }
  278. if (desc_type == BLOCK_DESC) {
  279. *out_level = level;
  280. return &table[idx];
  281. }
  282. assert(desc_type == TABLE_DESC);
  283. table = (uint64_t *)(uintptr_t)(desc & TABLE_ADDR_MASK);
  284. entries = XLAT_TABLE_ENTRIES;
  285. }
  286. /*
  287. * This shouldn't be reached, the translation table walk should end at
  288. * most at level XLAT_TABLE_LEVEL_MAX and return from inside the loop.
  289. */
  290. assert(false);
  291. return NULL;
  292. }
  293. static int xlat_get_mem_attributes_internal(const xlat_ctx_t *ctx,
  294. uintptr_t base_va, uint32_t *attributes, uint64_t **table_entry,
  295. unsigned long long *addr_pa, unsigned int *table_level)
  296. {
  297. uint64_t *entry;
  298. uint64_t desc;
  299. unsigned int level;
  300. unsigned long long virt_addr_space_size;
  301. /*
  302. * Sanity-check arguments.
  303. */
  304. assert(ctx != NULL);
  305. assert(ctx->initialized);
  306. assert((ctx->xlat_regime == EL1_EL0_REGIME) ||
  307. (ctx->xlat_regime == EL2_REGIME) ||
  308. (ctx->xlat_regime == EL3_REGIME));
  309. virt_addr_space_size = (unsigned long long)ctx->va_max_address + 1ULL;
  310. assert(virt_addr_space_size > 0U);
  311. entry = find_xlat_table_entry(base_va,
  312. ctx->base_table,
  313. ctx->base_table_entries,
  314. virt_addr_space_size,
  315. &level);
  316. if (entry == NULL) {
  317. WARN("Address 0x%lx is not mapped.\n", base_va);
  318. return -EINVAL;
  319. }
  320. if (addr_pa != NULL) {
  321. *addr_pa = *entry & TABLE_ADDR_MASK;
  322. }
  323. if (table_entry != NULL) {
  324. *table_entry = entry;
  325. }
  326. if (table_level != NULL) {
  327. *table_level = level;
  328. }
  329. desc = *entry;
  330. #if LOG_LEVEL >= LOG_LEVEL_VERBOSE
  331. VERBOSE("Attributes: ");
  332. xlat_desc_print(ctx, desc);
  333. printf("\n");
  334. #endif /* LOG_LEVEL >= LOG_LEVEL_VERBOSE */
  335. assert(attributes != NULL);
  336. *attributes = 0U;
  337. uint64_t attr_index = (desc >> ATTR_INDEX_SHIFT) & ATTR_INDEX_MASK;
  338. if (attr_index == ATTR_IWBWA_OWBWA_NTR_INDEX) {
  339. *attributes |= MT_MEMORY;
  340. } else if (attr_index == ATTR_NON_CACHEABLE_INDEX) {
  341. *attributes |= MT_NON_CACHEABLE;
  342. } else {
  343. assert(attr_index == ATTR_DEVICE_INDEX);
  344. *attributes |= MT_DEVICE;
  345. }
  346. uint64_t ap2_bit = (desc >> AP2_SHIFT) & 1U;
  347. if (ap2_bit == AP2_RW)
  348. *attributes |= MT_RW;
  349. if (ctx->xlat_regime == EL1_EL0_REGIME) {
  350. uint64_t ap1_bit = (desc >> AP1_SHIFT) & 1U;
  351. if (ap1_bit == AP1_ACCESS_UNPRIVILEGED)
  352. *attributes |= MT_USER;
  353. }
  354. uint64_t ns_bit = (desc >> NS_SHIFT) & 1U;
  355. if (ns_bit == 1U)
  356. *attributes |= MT_NS;
  357. uint64_t xn_mask = xlat_arch_regime_get_xn_desc(ctx->xlat_regime);
  358. if ((desc & xn_mask) == xn_mask) {
  359. *attributes |= MT_EXECUTE_NEVER;
  360. } else {
  361. assert((desc & xn_mask) == 0U);
  362. }
  363. return 0;
  364. }
  365. int xlat_get_mem_attributes_ctx(const xlat_ctx_t *ctx, uintptr_t base_va,
  366. uint32_t *attr)
  367. {
  368. return xlat_get_mem_attributes_internal(ctx, base_va, attr,
  369. NULL, NULL, NULL);
  370. }
  371. int xlat_change_mem_attributes_ctx(const xlat_ctx_t *ctx, uintptr_t base_va,
  372. size_t size, uint32_t attr)
  373. {
  374. /* Note: This implementation isn't optimized. */
  375. assert(ctx != NULL);
  376. assert(ctx->initialized);
  377. unsigned long long virt_addr_space_size =
  378. (unsigned long long)ctx->va_max_address + 1U;
  379. assert(virt_addr_space_size > 0U);
  380. if (!IS_PAGE_ALIGNED(base_va)) {
  381. WARN("%s: Address 0x%lx is not aligned on a page boundary.\n",
  382. __func__, base_va);
  383. return -EINVAL;
  384. }
  385. if (size == 0U) {
  386. WARN("%s: Size is 0.\n", __func__);
  387. return -EINVAL;
  388. }
  389. if ((size % PAGE_SIZE) != 0U) {
  390. WARN("%s: Size 0x%zx is not a multiple of a page size.\n",
  391. __func__, size);
  392. return -EINVAL;
  393. }
  394. if (((attr & MT_EXECUTE_NEVER) == 0U) && ((attr & MT_RW) != 0U)) {
  395. WARN("%s: Mapping memory as read-write and executable not allowed.\n",
  396. __func__);
  397. return -EINVAL;
  398. }
  399. size_t pages_count = size / PAGE_SIZE;
  400. VERBOSE("Changing memory attributes of %zu pages starting from address 0x%lx...\n",
  401. pages_count, base_va);
  402. uintptr_t base_va_original = base_va;
  403. /*
  404. * Sanity checks.
  405. */
  406. for (unsigned int i = 0U; i < pages_count; ++i) {
  407. const uint64_t *entry;
  408. uint64_t desc, attr_index;
  409. unsigned int level;
  410. entry = find_xlat_table_entry(base_va,
  411. ctx->base_table,
  412. ctx->base_table_entries,
  413. virt_addr_space_size,
  414. &level);
  415. if (entry == NULL) {
  416. WARN("Address 0x%lx is not mapped.\n", base_va);
  417. return -EINVAL;
  418. }
  419. desc = *entry;
  420. /*
  421. * Check that all the required pages are mapped at page
  422. * granularity.
  423. */
  424. if (((desc & DESC_MASK) != PAGE_DESC) ||
  425. (level != XLAT_TABLE_LEVEL_MAX)) {
  426. WARN("Address 0x%lx is not mapped at the right granularity.\n",
  427. base_va);
  428. WARN("Granularity is 0x%lx, should be 0x%lx.\n",
  429. XLAT_BLOCK_SIZE(level), PAGE_SIZE);
  430. return -EINVAL;
  431. }
  432. /*
  433. * If the region type is device, it shouldn't be executable.
  434. */
  435. attr_index = (desc >> ATTR_INDEX_SHIFT) & ATTR_INDEX_MASK;
  436. if (attr_index == ATTR_DEVICE_INDEX) {
  437. if ((attr & MT_EXECUTE_NEVER) == 0U) {
  438. WARN("Setting device memory as executable at address 0x%lx.",
  439. base_va);
  440. return -EINVAL;
  441. }
  442. }
  443. base_va += PAGE_SIZE;
  444. }
  445. /* Restore original value. */
  446. base_va = base_va_original;
  447. for (unsigned int i = 0U; i < pages_count; ++i) {
  448. uint32_t old_attr = 0U, new_attr;
  449. uint64_t *entry = NULL;
  450. unsigned int level = 0U;
  451. unsigned long long addr_pa = 0ULL;
  452. (void) xlat_get_mem_attributes_internal(ctx, base_va, &old_attr,
  453. &entry, &addr_pa, &level);
  454. /*
  455. * From attr, only MT_RO/MT_RW, MT_EXECUTE/MT_EXECUTE_NEVER and
  456. * MT_USER/MT_PRIVILEGED are taken into account. Any other
  457. * information is ignored.
  458. */
  459. /* Clean the old attributes so that they can be rebuilt. */
  460. new_attr = old_attr & ~(MT_RW | MT_EXECUTE_NEVER | MT_USER);
  461. /*
  462. * Update attributes, but filter out the ones this function
  463. * isn't allowed to change.
  464. */
  465. new_attr |= attr & (MT_RW | MT_EXECUTE_NEVER | MT_USER);
  466. /*
  467. * The break-before-make sequence requires writing an invalid
  468. * descriptor and making sure that the system sees the change
  469. * before writing the new descriptor.
  470. */
  471. *entry = INVALID_DESC;
  472. #if !HW_ASSISTED_COHERENCY
  473. dccvac((uintptr_t)entry);
  474. #endif
  475. /* Invalidate any cached copy of this mapping in the TLBs. */
  476. xlat_arch_tlbi_va(base_va, ctx->xlat_regime);
  477. /* Ensure completion of the invalidation. */
  478. xlat_arch_tlbi_va_sync();
  479. /* Write new descriptor */
  480. *entry = xlat_desc(ctx, new_attr, addr_pa, level);
  481. #if !HW_ASSISTED_COHERENCY
  482. dccvac((uintptr_t)entry);
  483. #endif
  484. base_va += PAGE_SIZE;
  485. }
  486. /* Ensure that the last descriptor written is seen by the system. */
  487. dsbish();
  488. return 0;
  489. }