xlat_tables_utils.c 16 KB

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