xlat_tables_core.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244
  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 <stdbool.h>
  9. #include <stdint.h>
  10. #include <string.h>
  11. #include <platform_def.h>
  12. #include <arch_features.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. /* Helper function that cleans the data cache only if it is enabled. */
  20. static inline __attribute__((unused)) void xlat_clean_dcache_range(uintptr_t addr, size_t size)
  21. {
  22. if (is_dcache_enabled())
  23. clean_dcache_range(addr, size);
  24. }
  25. #if PLAT_XLAT_TABLES_DYNAMIC
  26. /*
  27. * The following functions assume that they will be called using subtables only.
  28. * The base table can't be unmapped, so it is not needed to do any special
  29. * handling for it.
  30. */
  31. /*
  32. * Returns the index of the array corresponding to the specified translation
  33. * table.
  34. */
  35. static int xlat_table_get_index(const xlat_ctx_t *ctx, const uint64_t *table)
  36. {
  37. for (int i = 0; i < ctx->tables_num; i++)
  38. if (ctx->tables[i] == table)
  39. return i;
  40. /*
  41. * Maybe we were asked to get the index of the base level table, which
  42. * should never happen.
  43. */
  44. assert(false);
  45. return -1;
  46. }
  47. /* Returns a pointer to an empty translation table. */
  48. static uint64_t *xlat_table_get_empty(const xlat_ctx_t *ctx)
  49. {
  50. for (int i = 0; i < ctx->tables_num; i++)
  51. if (ctx->tables_mapped_regions[i] == 0)
  52. return ctx->tables[i];
  53. return NULL;
  54. }
  55. /* Increments region count for a given table. */
  56. static void xlat_table_inc_regions_count(const xlat_ctx_t *ctx,
  57. const uint64_t *table)
  58. {
  59. int idx = xlat_table_get_index(ctx, table);
  60. ctx->tables_mapped_regions[idx]++;
  61. }
  62. /* Decrements region count for a given table. */
  63. static void xlat_table_dec_regions_count(const xlat_ctx_t *ctx,
  64. const uint64_t *table)
  65. {
  66. int idx = xlat_table_get_index(ctx, table);
  67. ctx->tables_mapped_regions[idx]--;
  68. }
  69. /* Returns 0 if the specified table isn't empty, otherwise 1. */
  70. static bool xlat_table_is_empty(const xlat_ctx_t *ctx, const uint64_t *table)
  71. {
  72. return ctx->tables_mapped_regions[xlat_table_get_index(ctx, table)] == 0;
  73. }
  74. #else /* PLAT_XLAT_TABLES_DYNAMIC */
  75. /* Returns a pointer to the first empty translation table. */
  76. static uint64_t *xlat_table_get_empty(xlat_ctx_t *ctx)
  77. {
  78. assert(ctx->next_table < ctx->tables_num);
  79. return ctx->tables[ctx->next_table++];
  80. }
  81. #endif /* PLAT_XLAT_TABLES_DYNAMIC */
  82. /*
  83. * Returns a block/page table descriptor for the given level and attributes.
  84. */
  85. uint64_t xlat_desc(const xlat_ctx_t *ctx, uint32_t attr,
  86. unsigned long long addr_pa, unsigned int level)
  87. {
  88. uint64_t desc;
  89. uint32_t mem_type;
  90. uint32_t shareability_type;
  91. /* Make sure that the granularity is fine enough to map this address. */
  92. assert((addr_pa & XLAT_BLOCK_MASK(level)) == 0U);
  93. desc = addr_pa;
  94. /*
  95. * There are different translation table descriptors for level 3 and the
  96. * rest.
  97. */
  98. desc |= (level == XLAT_TABLE_LEVEL_MAX) ? PAGE_DESC : BLOCK_DESC;
  99. /*
  100. * Always set the access flag, as this library assumes access flag
  101. * faults aren't managed.
  102. */
  103. desc |= LOWER_ATTRS(ACCESS_FLAG);
  104. /* Determine the physical address space this region belongs to. */
  105. desc |= xlat_arch_get_pas(attr);
  106. /*
  107. * Deduce other fields of the descriptor based on the MT_RW memory
  108. * region attributes.
  109. */
  110. desc |= ((attr & MT_RW) != 0U) ? LOWER_ATTRS(AP_RW) : LOWER_ATTRS(AP_RO);
  111. /*
  112. * Do not allow unprivileged access when the mapping is for a privileged
  113. * EL. For translation regimes that do not have mappings for access for
  114. * lower exception levels, set AP[2] to AP_NO_ACCESS_UNPRIVILEGED.
  115. */
  116. if (ctx->xlat_regime == EL1_EL0_REGIME) {
  117. if ((attr & MT_USER) != 0U) {
  118. /* EL0 mapping requested, so we give User access */
  119. desc |= LOWER_ATTRS(AP_ACCESS_UNPRIVILEGED);
  120. } else {
  121. /* EL1 mapping requested, no User access granted */
  122. desc |= LOWER_ATTRS(AP_NO_ACCESS_UNPRIVILEGED);
  123. }
  124. } else {
  125. assert((ctx->xlat_regime == EL2_REGIME) ||
  126. (ctx->xlat_regime == EL3_REGIME));
  127. desc |= LOWER_ATTRS(AP_ONE_VA_RANGE_RES1);
  128. }
  129. /*
  130. * Deduce shareability domain and executability of the memory region
  131. * from the memory type of the attributes (MT_TYPE).
  132. *
  133. * Data accesses to device memory and non-cacheable normal memory are
  134. * coherent for all observers in the system, and correspondingly are
  135. * always treated as being Outer Shareable. Therefore, for these 2 types
  136. * of memory, it is not strictly needed to set the shareability field
  137. * in the translation tables.
  138. */
  139. mem_type = MT_TYPE(attr);
  140. if (mem_type == MT_DEVICE) {
  141. desc |= LOWER_ATTRS(ATTR_DEVICE_INDEX | OSH);
  142. /*
  143. * Always map device memory as execute-never.
  144. * This is to avoid the possibility of a speculative instruction
  145. * fetch, which could be an issue if this memory region
  146. * corresponds to a read-sensitive peripheral.
  147. */
  148. desc |= xlat_arch_regime_get_xn_desc(ctx->xlat_regime);
  149. } else { /* Normal memory */
  150. /*
  151. * Always map read-write normal memory as execute-never.
  152. * This library assumes that it is used by software that does
  153. * not self-modify its code, therefore R/W memory is reserved
  154. * for data storage, which must not be executable.
  155. *
  156. * Note that setting the XN bit here is for consistency only.
  157. * The function that enables the MMU sets the SCTLR_ELx.WXN bit,
  158. * which makes any writable memory region to be treated as
  159. * execute-never, regardless of the value of the XN bit in the
  160. * translation table.
  161. *
  162. * For read-only memory, rely on the MT_EXECUTE/MT_EXECUTE_NEVER
  163. * attribute to figure out the value of the XN bit. The actual
  164. * XN bit(s) to set in the descriptor depends on the context's
  165. * translation regime and the policy applied in
  166. * xlat_arch_regime_get_xn_desc().
  167. */
  168. if (((attr & MT_RW) != 0U) || ((attr & MT_EXECUTE_NEVER) != 0U)) {
  169. desc |= xlat_arch_regime_get_xn_desc(ctx->xlat_regime);
  170. }
  171. shareability_type = MT_SHAREABILITY(attr);
  172. if (mem_type == MT_MEMORY) {
  173. desc |= LOWER_ATTRS(ATTR_IWBWA_OWBWA_NTR_INDEX);
  174. if (shareability_type == MT_SHAREABILITY_NSH) {
  175. desc |= LOWER_ATTRS(NSH);
  176. } else if (shareability_type == MT_SHAREABILITY_OSH) {
  177. desc |= LOWER_ATTRS(OSH);
  178. } else {
  179. desc |= LOWER_ATTRS(ISH);
  180. }
  181. /* Check if Branch Target Identification is enabled */
  182. #if ENABLE_BTI
  183. /* Set GP bit for block and page code entries
  184. * if BTI mechanism is implemented.
  185. */
  186. if (is_feat_bti_present() &&
  187. ((attr & (MT_TYPE_MASK | MT_RW |
  188. MT_EXECUTE_NEVER)) == MT_CODE)) {
  189. desc |= GP;
  190. }
  191. #endif
  192. } else {
  193. assert(mem_type == MT_NON_CACHEABLE);
  194. desc |= LOWER_ATTRS(ATTR_NON_CACHEABLE_INDEX | OSH);
  195. }
  196. }
  197. return desc;
  198. }
  199. /*
  200. * Enumeration of actions that can be made when mapping table entries depending
  201. * on the previous value in that entry and information about the region being
  202. * mapped.
  203. */
  204. typedef enum {
  205. /* Do nothing */
  206. ACTION_NONE,
  207. /* Write a block (or page, if in level 3) entry. */
  208. ACTION_WRITE_BLOCK_ENTRY,
  209. /*
  210. * Create a new table and write a table entry pointing to it. Recurse
  211. * into it for further processing.
  212. */
  213. ACTION_CREATE_NEW_TABLE,
  214. /*
  215. * There is a table descriptor in this entry, read it and recurse into
  216. * that table for further processing.
  217. */
  218. ACTION_RECURSE_INTO_TABLE,
  219. } action_t;
  220. /*
  221. * Function that returns the first VA of the table affected by the specified
  222. * mmap region.
  223. */
  224. static uintptr_t xlat_tables_find_start_va(mmap_region_t *mm,
  225. const uintptr_t table_base_va,
  226. const unsigned int level)
  227. {
  228. uintptr_t table_idx_va;
  229. if (mm->base_va > table_base_va) {
  230. /* Find the first index of the table affected by the region. */
  231. table_idx_va = mm->base_va & ~XLAT_BLOCK_MASK(level);
  232. } else {
  233. /* Start from the beginning of the table. */
  234. table_idx_va = table_base_va;
  235. }
  236. return table_idx_va;
  237. }
  238. /*
  239. * Function that returns table index for the given VA and level arguments.
  240. */
  241. static inline unsigned int xlat_tables_va_to_index(const uintptr_t table_base_va,
  242. const uintptr_t va,
  243. const unsigned int level)
  244. {
  245. return (unsigned int)((va - table_base_va) >> XLAT_ADDR_SHIFT(level));
  246. }
  247. #if PLAT_XLAT_TABLES_DYNAMIC
  248. /*
  249. * From the given arguments, it decides which action to take when unmapping the
  250. * specified region.
  251. */
  252. static action_t xlat_tables_unmap_region_action(const mmap_region_t *mm,
  253. const uintptr_t table_idx_va, const uintptr_t table_idx_end_va,
  254. const unsigned int level, const uint64_t desc_type)
  255. {
  256. action_t action;
  257. uintptr_t region_end_va = mm->base_va + mm->size - 1U;
  258. if ((mm->base_va <= table_idx_va) &&
  259. (region_end_va >= table_idx_end_va)) {
  260. /* Region covers all block */
  261. if (level == 3U) {
  262. /*
  263. * Last level, only page descriptors allowed,
  264. * erase it.
  265. */
  266. assert(desc_type == PAGE_DESC);
  267. action = ACTION_WRITE_BLOCK_ENTRY;
  268. } else {
  269. /*
  270. * Other levels can have table descriptors. If
  271. * so, recurse into it and erase descriptors
  272. * inside it as needed. If there is a block
  273. * descriptor, just erase it. If an invalid
  274. * descriptor is found, this table isn't
  275. * actually mapped, which shouldn't happen.
  276. */
  277. if (desc_type == TABLE_DESC) {
  278. action = ACTION_RECURSE_INTO_TABLE;
  279. } else {
  280. assert(desc_type == BLOCK_DESC);
  281. action = ACTION_WRITE_BLOCK_ENTRY;
  282. }
  283. }
  284. } else if ((mm->base_va <= table_idx_end_va) ||
  285. (region_end_va >= table_idx_va)) {
  286. /*
  287. * Region partially covers block.
  288. *
  289. * It can't happen in level 3.
  290. *
  291. * There must be a table descriptor here, if not there
  292. * was a problem when mapping the region.
  293. */
  294. assert(level < 3U);
  295. assert(desc_type == TABLE_DESC);
  296. action = ACTION_RECURSE_INTO_TABLE;
  297. } else {
  298. /* The region doesn't cover the block at all */
  299. action = ACTION_NONE;
  300. }
  301. return action;
  302. }
  303. /*
  304. * Recursive function that writes to the translation tables and unmaps the
  305. * specified region.
  306. */
  307. static void xlat_tables_unmap_region(xlat_ctx_t *ctx, mmap_region_t *mm,
  308. const uintptr_t table_base_va,
  309. uint64_t *const table_base,
  310. const unsigned int table_entries,
  311. const unsigned int level)
  312. {
  313. assert((level >= ctx->base_level) && (level <= XLAT_TABLE_LEVEL_MAX));
  314. uint64_t *subtable;
  315. uint64_t desc;
  316. uintptr_t table_idx_va;
  317. uintptr_t table_idx_end_va; /* End VA of this entry */
  318. uintptr_t region_end_va = mm->base_va + mm->size - 1U;
  319. unsigned int table_idx;
  320. table_idx_va = xlat_tables_find_start_va(mm, table_base_va, level);
  321. table_idx = xlat_tables_va_to_index(table_base_va, table_idx_va, level);
  322. while (table_idx < table_entries) {
  323. table_idx_end_va = table_idx_va + XLAT_BLOCK_SIZE(level) - 1U;
  324. desc = table_base[table_idx];
  325. uint64_t desc_type = desc & DESC_MASK;
  326. action_t action = xlat_tables_unmap_region_action(mm,
  327. table_idx_va, table_idx_end_va, level,
  328. desc_type);
  329. if (action == ACTION_WRITE_BLOCK_ENTRY) {
  330. table_base[table_idx] = INVALID_DESC;
  331. xlat_arch_tlbi_va(table_idx_va, ctx->xlat_regime);
  332. } else if (action == ACTION_RECURSE_INTO_TABLE) {
  333. subtable = (uint64_t *)(uintptr_t)(desc & TABLE_ADDR_MASK);
  334. /* Recurse to write into subtable */
  335. xlat_tables_unmap_region(ctx, mm, table_idx_va,
  336. subtable, XLAT_TABLE_ENTRIES,
  337. level + 1U);
  338. #if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY)
  339. xlat_clean_dcache_range((uintptr_t)subtable,
  340. XLAT_TABLE_ENTRIES * sizeof(uint64_t));
  341. #endif
  342. /*
  343. * If the subtable is now empty, remove its reference.
  344. */
  345. if (xlat_table_is_empty(ctx, subtable)) {
  346. table_base[table_idx] = INVALID_DESC;
  347. xlat_arch_tlbi_va(table_idx_va,
  348. ctx->xlat_regime);
  349. }
  350. } else {
  351. assert(action == ACTION_NONE);
  352. }
  353. table_idx++;
  354. table_idx_va += XLAT_BLOCK_SIZE(level);
  355. /* If reached the end of the region, exit */
  356. if (region_end_va <= table_idx_va)
  357. break;
  358. }
  359. if (level > ctx->base_level)
  360. xlat_table_dec_regions_count(ctx, table_base);
  361. }
  362. #endif /* PLAT_XLAT_TABLES_DYNAMIC */
  363. /*
  364. * From the given arguments, it decides which action to take when mapping the
  365. * specified region.
  366. */
  367. static action_t xlat_tables_map_region_action(const mmap_region_t *mm,
  368. unsigned int desc_type, unsigned long long dest_pa,
  369. uintptr_t table_entry_base_va, unsigned int level)
  370. {
  371. uintptr_t mm_end_va = mm->base_va + mm->size - 1U;
  372. uintptr_t table_entry_end_va =
  373. table_entry_base_va + XLAT_BLOCK_SIZE(level) - 1U;
  374. /*
  375. * The descriptor types allowed depend on the current table level.
  376. */
  377. if ((mm->base_va <= table_entry_base_va) &&
  378. (mm_end_va >= table_entry_end_va)) {
  379. /*
  380. * Table entry is covered by region
  381. * --------------------------------
  382. *
  383. * This means that this table entry can describe the whole
  384. * translation with this granularity in principle.
  385. */
  386. if (level == 3U) {
  387. /*
  388. * Last level, only page descriptors are allowed.
  389. */
  390. if (desc_type == PAGE_DESC) {
  391. /*
  392. * There's another region mapped here, don't
  393. * overwrite.
  394. */
  395. return ACTION_NONE;
  396. } else {
  397. assert(desc_type == INVALID_DESC);
  398. return ACTION_WRITE_BLOCK_ENTRY;
  399. }
  400. } else {
  401. /*
  402. * Other levels. Table descriptors are allowed. Block
  403. * descriptors too, but they have some limitations.
  404. */
  405. if (desc_type == TABLE_DESC) {
  406. /* There's already a table, recurse into it. */
  407. return ACTION_RECURSE_INTO_TABLE;
  408. } else if (desc_type == INVALID_DESC) {
  409. /*
  410. * There's nothing mapped here, create a new
  411. * entry.
  412. *
  413. * Check if the destination granularity allows
  414. * us to use a block descriptor or we need a
  415. * finer table for it.
  416. *
  417. * Also, check if the current level allows block
  418. * descriptors. If not, create a table instead.
  419. */
  420. if (((dest_pa & XLAT_BLOCK_MASK(level)) != 0U)
  421. || (level < MIN_LVL_BLOCK_DESC) ||
  422. (mm->granularity < XLAT_BLOCK_SIZE(level)))
  423. return ACTION_CREATE_NEW_TABLE;
  424. else
  425. return ACTION_WRITE_BLOCK_ENTRY;
  426. } else {
  427. /*
  428. * There's another region mapped here, don't
  429. * overwrite.
  430. */
  431. assert(desc_type == BLOCK_DESC);
  432. return ACTION_NONE;
  433. }
  434. }
  435. } else if ((mm->base_va <= table_entry_end_va) ||
  436. (mm_end_va >= table_entry_base_va)) {
  437. /*
  438. * Region partially covers table entry
  439. * -----------------------------------
  440. *
  441. * This means that this table entry can't describe the whole
  442. * translation, a finer table is needed.
  443. * There cannot be partial block overlaps in level 3. If that
  444. * happens, some of the preliminary checks when adding the
  445. * mmap region failed to detect that PA and VA must at least be
  446. * aligned to PAGE_SIZE.
  447. */
  448. assert(level < 3U);
  449. if (desc_type == INVALID_DESC) {
  450. /*
  451. * The block is not fully covered by the region. Create
  452. * a new table, recurse into it and try to map the
  453. * region with finer granularity.
  454. */
  455. return ACTION_CREATE_NEW_TABLE;
  456. } else {
  457. assert(desc_type == TABLE_DESC);
  458. /*
  459. * The block is not fully covered by the region, but
  460. * there is already a table here. Recurse into it and
  461. * try to map with finer granularity.
  462. *
  463. * PAGE_DESC for level 3 has the same value as
  464. * TABLE_DESC, but this code can't run on a level 3
  465. * table because there can't be overlaps in level 3.
  466. */
  467. return ACTION_RECURSE_INTO_TABLE;
  468. }
  469. } else {
  470. /*
  471. * This table entry is outside of the region specified in the
  472. * arguments, don't write anything to it.
  473. */
  474. return ACTION_NONE;
  475. }
  476. }
  477. /*
  478. * Recursive function that writes to the translation tables and maps the
  479. * specified region. On success, it returns the VA of the last byte that was
  480. * successfully mapped. On error, it returns the VA of the next entry that
  481. * should have been mapped.
  482. */
  483. static uintptr_t xlat_tables_map_region(xlat_ctx_t *ctx, mmap_region_t *mm,
  484. uintptr_t table_base_va,
  485. uint64_t *const table_base,
  486. unsigned int table_entries,
  487. unsigned int level)
  488. {
  489. assert((level >= ctx->base_level) && (level <= XLAT_TABLE_LEVEL_MAX));
  490. uintptr_t mm_end_va = mm->base_va + mm->size - 1U;
  491. uintptr_t table_idx_va;
  492. unsigned long long table_idx_pa;
  493. uint64_t *subtable;
  494. uint64_t desc;
  495. unsigned int table_idx;
  496. table_idx_va = xlat_tables_find_start_va(mm, table_base_va, level);
  497. table_idx = xlat_tables_va_to_index(table_base_va, table_idx_va, level);
  498. #if PLAT_XLAT_TABLES_DYNAMIC
  499. if (level > ctx->base_level)
  500. xlat_table_inc_regions_count(ctx, table_base);
  501. #endif
  502. while (table_idx < table_entries) {
  503. desc = table_base[table_idx];
  504. table_idx_pa = mm->base_pa + table_idx_va - mm->base_va;
  505. action_t action = xlat_tables_map_region_action(mm,
  506. (uint32_t)(desc & DESC_MASK), table_idx_pa,
  507. table_idx_va, level);
  508. if (action == ACTION_WRITE_BLOCK_ENTRY) {
  509. table_base[table_idx] =
  510. xlat_desc(ctx, (uint32_t)mm->attr, table_idx_pa,
  511. level);
  512. } else if (action == ACTION_CREATE_NEW_TABLE) {
  513. uintptr_t end_va;
  514. subtable = xlat_table_get_empty(ctx);
  515. if (subtable == NULL) {
  516. /* Not enough free tables to map this region */
  517. return table_idx_va;
  518. }
  519. /* Point to new subtable from this one. */
  520. table_base[table_idx] =
  521. TABLE_DESC | (uintptr_t)subtable;
  522. /* Recurse to write into subtable */
  523. end_va = xlat_tables_map_region(ctx, mm, table_idx_va,
  524. subtable, XLAT_TABLE_ENTRIES,
  525. level + 1U);
  526. #if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY)
  527. xlat_clean_dcache_range((uintptr_t)subtable,
  528. XLAT_TABLE_ENTRIES * sizeof(uint64_t));
  529. #endif
  530. if (end_va !=
  531. (table_idx_va + XLAT_BLOCK_SIZE(level) - 1U))
  532. return end_va;
  533. } else if (action == ACTION_RECURSE_INTO_TABLE) {
  534. uintptr_t end_va;
  535. subtable = (uint64_t *)(uintptr_t)(desc & TABLE_ADDR_MASK);
  536. /* Recurse to write into subtable */
  537. end_va = xlat_tables_map_region(ctx, mm, table_idx_va,
  538. subtable, XLAT_TABLE_ENTRIES,
  539. level + 1U);
  540. #if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY)
  541. xlat_clean_dcache_range((uintptr_t)subtable,
  542. XLAT_TABLE_ENTRIES * sizeof(uint64_t));
  543. #endif
  544. if (end_va !=
  545. (table_idx_va + XLAT_BLOCK_SIZE(level) - 1U))
  546. return end_va;
  547. } else {
  548. assert(action == ACTION_NONE);
  549. }
  550. table_idx++;
  551. table_idx_va += XLAT_BLOCK_SIZE(level);
  552. /* If reached the end of the region, exit */
  553. if (mm_end_va <= table_idx_va)
  554. break;
  555. }
  556. return table_idx_va - 1U;
  557. }
  558. /*
  559. * Function that verifies that a region can be mapped.
  560. * Returns:
  561. * 0: Success, the mapping is allowed.
  562. * EINVAL: Invalid values were used as arguments.
  563. * ERANGE: The memory limits were surpassed.
  564. * ENOMEM: There is not enough memory in the mmap array.
  565. * EPERM: Region overlaps another one in an invalid way.
  566. */
  567. static int mmap_add_region_check(const xlat_ctx_t *ctx, const mmap_region_t *mm)
  568. {
  569. unsigned long long base_pa = mm->base_pa;
  570. uintptr_t base_va = mm->base_va;
  571. size_t size = mm->size;
  572. size_t granularity = mm->granularity;
  573. unsigned long long end_pa = base_pa + size - 1U;
  574. uintptr_t end_va = base_va + size - 1U;
  575. if (!IS_PAGE_ALIGNED(base_pa) || !IS_PAGE_ALIGNED(base_va) ||
  576. !IS_PAGE_ALIGNED(size))
  577. return -EINVAL;
  578. if ((granularity != XLAT_BLOCK_SIZE(1U)) &&
  579. (granularity != XLAT_BLOCK_SIZE(2U)) &&
  580. (granularity != XLAT_BLOCK_SIZE(3U))) {
  581. return -EINVAL;
  582. }
  583. /* Check for overflows */
  584. if ((base_pa > end_pa) || (base_va > end_va))
  585. return -ERANGE;
  586. if (end_va > ctx->va_max_address)
  587. return -ERANGE;
  588. if (end_pa > ctx->pa_max_address)
  589. return -ERANGE;
  590. /* Check that there is space in the ctx->mmap array */
  591. if (ctx->mmap[ctx->mmap_num - 1].size != 0U)
  592. return -ENOMEM;
  593. /* Check for PAs and VAs overlaps with all other regions */
  594. for (const mmap_region_t *mm_cursor = ctx->mmap;
  595. mm_cursor->size != 0U; ++mm_cursor) {
  596. uintptr_t mm_cursor_end_va = mm_cursor->base_va
  597. + mm_cursor->size - 1U;
  598. /*
  599. * Check if one of the regions is completely inside the other
  600. * one.
  601. */
  602. bool fully_overlapped_va =
  603. ((base_va >= mm_cursor->base_va) &&
  604. (end_va <= mm_cursor_end_va)) ||
  605. ((mm_cursor->base_va >= base_va) &&
  606. (mm_cursor_end_va <= end_va));
  607. /*
  608. * Full VA overlaps are only allowed if both regions are
  609. * identity mapped (zero offset) or have the same VA to PA
  610. * offset. Also, make sure that it's not the exact same area.
  611. * This can only be done with static regions.
  612. */
  613. if (fully_overlapped_va) {
  614. #if PLAT_XLAT_TABLES_DYNAMIC
  615. if (((mm->attr & MT_DYNAMIC) != 0U) ||
  616. ((mm_cursor->attr & MT_DYNAMIC) != 0U))
  617. return -EPERM;
  618. #endif /* PLAT_XLAT_TABLES_DYNAMIC */
  619. if ((mm_cursor->base_va - mm_cursor->base_pa) !=
  620. (base_va - base_pa))
  621. return -EPERM;
  622. if ((base_va == mm_cursor->base_va) &&
  623. (size == mm_cursor->size))
  624. return -EPERM;
  625. } else {
  626. /*
  627. * If the regions do not have fully overlapping VAs,
  628. * then they must have fully separated VAs and PAs.
  629. * Partial overlaps are not allowed
  630. */
  631. unsigned long long mm_cursor_end_pa =
  632. mm_cursor->base_pa + mm_cursor->size - 1U;
  633. bool separated_pa = (end_pa < mm_cursor->base_pa) ||
  634. (base_pa > mm_cursor_end_pa);
  635. bool separated_va = (end_va < mm_cursor->base_va) ||
  636. (base_va > mm_cursor_end_va);
  637. if (!separated_va || !separated_pa)
  638. return -EPERM;
  639. }
  640. }
  641. return 0;
  642. }
  643. void mmap_add_region_ctx(xlat_ctx_t *ctx, const mmap_region_t *mm)
  644. {
  645. mmap_region_t *mm_cursor = ctx->mmap, *mm_destination;
  646. const mmap_region_t *mm_end = ctx->mmap + ctx->mmap_num;
  647. const mmap_region_t *mm_last;
  648. unsigned long long end_pa = mm->base_pa + mm->size - 1U;
  649. uintptr_t end_va = mm->base_va + mm->size - 1U;
  650. int ret;
  651. /* Ignore empty regions */
  652. if (mm->size == 0U)
  653. return;
  654. /* Static regions must be added before initializing the xlat tables. */
  655. assert(!ctx->initialized);
  656. ret = mmap_add_region_check(ctx, mm);
  657. if (ret != 0) {
  658. ERROR("mmap_add_region_check() failed. error %d\n", ret);
  659. assert(false);
  660. return;
  661. }
  662. /*
  663. * Find correct place in mmap to insert new region.
  664. *
  665. * 1 - Lower region VA end first.
  666. * 2 - Smaller region size first.
  667. *
  668. * VA 0 0xFF
  669. *
  670. * 1st |------|
  671. * 2nd |------------|
  672. * 3rd |------|
  673. * 4th |---|
  674. * 5th |---|
  675. * 6th |----------|
  676. * 7th |-------------------------------------|
  677. *
  678. * This is required for overlapping regions only. It simplifies adding
  679. * regions with the loop in xlat_tables_init_internal because the outer
  680. * ones won't overwrite block or page descriptors of regions added
  681. * previously.
  682. *
  683. * Overlapping is only allowed for static regions.
  684. */
  685. while (((mm_cursor->base_va + mm_cursor->size - 1U) < end_va)
  686. && (mm_cursor->size != 0U)) {
  687. ++mm_cursor;
  688. }
  689. while (((mm_cursor->base_va + mm_cursor->size - 1U) == end_va) &&
  690. (mm_cursor->size != 0U) && (mm_cursor->size < mm->size)) {
  691. ++mm_cursor;
  692. }
  693. /*
  694. * Find the last entry marker in the mmap
  695. */
  696. mm_last = ctx->mmap;
  697. while ((mm_last->size != 0U) && (mm_last < mm_end)) {
  698. ++mm_last;
  699. }
  700. /*
  701. * Check if we have enough space in the memory mapping table.
  702. * This shouldn't happen as we have checked in mmap_add_region_check
  703. * that there is free space.
  704. */
  705. assert(mm_last->size == 0U);
  706. /* Make room for new region by moving other regions up by one place */
  707. mm_destination = mm_cursor + 1;
  708. (void)memmove(mm_destination, mm_cursor,
  709. (uintptr_t)mm_last - (uintptr_t)mm_cursor);
  710. /*
  711. * Check we haven't lost the empty sentinel from the end of the array.
  712. * This shouldn't happen as we have checked in mmap_add_region_check
  713. * that there is free space.
  714. */
  715. assert(mm_end->size == 0U);
  716. *mm_cursor = *mm;
  717. if (end_pa > ctx->max_pa)
  718. ctx->max_pa = end_pa;
  719. if (end_va > ctx->max_va)
  720. ctx->max_va = end_va;
  721. }
  722. /*
  723. * Determine the table level closest to the initial lookup level that
  724. * can describe this translation. Then, align base VA to the next block
  725. * at the determined level.
  726. */
  727. static void mmap_alloc_va_align_ctx(xlat_ctx_t *ctx, mmap_region_t *mm)
  728. {
  729. /*
  730. * By or'ing the size and base PA the alignment will be the one
  731. * corresponding to the smallest boundary of the two of them.
  732. *
  733. * There are three different cases. For example (for 4 KiB page size):
  734. *
  735. * +--------------+------------------++--------------+
  736. * | PA alignment | Size multiple of || VA alignment |
  737. * +--------------+------------------++--------------+
  738. * | 2 MiB | 2 MiB || 2 MiB | (1)
  739. * | 2 MiB | 4 KiB || 4 KiB | (2)
  740. * | 4 KiB | 2 MiB || 4 KiB | (3)
  741. * +--------------+------------------++--------------+
  742. *
  743. * - In (1), it is possible to take advantage of the alignment of the PA
  744. * and the size of the region to use a level 2 translation table
  745. * instead of a level 3 one.
  746. *
  747. * - In (2), the size is smaller than a block entry of level 2, so it is
  748. * needed to use a level 3 table to describe the region or the library
  749. * will map more memory than the desired one.
  750. *
  751. * - In (3), even though the region has the size of one level 2 block
  752. * entry, it isn't possible to describe the translation with a level 2
  753. * block entry because of the alignment of the base PA.
  754. *
  755. * Only bits 47:21 of a level 2 block descriptor are used by the MMU,
  756. * bits 20:0 of the resulting address are 0 in this case. Because of
  757. * this, the PA generated as result of this translation is aligned to
  758. * 2 MiB. The PA that was requested to be mapped is aligned to 4 KiB,
  759. * though, which means that the resulting translation is incorrect.
  760. * The only way to prevent this is by using a finer granularity.
  761. */
  762. unsigned long long align_check;
  763. align_check = mm->base_pa | (unsigned long long)mm->size;
  764. /*
  765. * Assume it is always aligned to level 3. There's no need to check that
  766. * level because its block size is PAGE_SIZE. The checks to verify that
  767. * the addresses and size are aligned to PAGE_SIZE are inside
  768. * mmap_add_region.
  769. */
  770. for (unsigned int level = ctx->base_level; level <= 2U; ++level) {
  771. if ((align_check & XLAT_BLOCK_MASK(level)) != 0U)
  772. continue;
  773. mm->base_va = round_up(mm->base_va, XLAT_BLOCK_SIZE(level));
  774. return;
  775. }
  776. }
  777. void mmap_add_region_alloc_va_ctx(xlat_ctx_t *ctx, mmap_region_t *mm)
  778. {
  779. mm->base_va = ctx->max_va + 1UL;
  780. assert(mm->size > 0U);
  781. mmap_alloc_va_align_ctx(ctx, mm);
  782. /* Detect overflows. More checks are done in mmap_add_region_check(). */
  783. assert(mm->base_va > ctx->max_va);
  784. mmap_add_region_ctx(ctx, mm);
  785. }
  786. void mmap_add_ctx(xlat_ctx_t *ctx, const mmap_region_t *mm)
  787. {
  788. const mmap_region_t *mm_cursor = mm;
  789. while (mm_cursor->granularity != 0U) {
  790. mmap_add_region_ctx(ctx, mm_cursor);
  791. mm_cursor++;
  792. }
  793. }
  794. #if PLAT_XLAT_TABLES_DYNAMIC
  795. int mmap_add_dynamic_region_ctx(xlat_ctx_t *ctx, mmap_region_t *mm)
  796. {
  797. mmap_region_t *mm_cursor = ctx->mmap;
  798. const mmap_region_t *mm_last = mm_cursor + ctx->mmap_num;
  799. unsigned long long end_pa = mm->base_pa + mm->size - 1U;
  800. uintptr_t end_va = mm->base_va + mm->size - 1U;
  801. int ret;
  802. /* Nothing to do */
  803. if (mm->size == 0U)
  804. return 0;
  805. /* Now this region is a dynamic one */
  806. mm->attr |= MT_DYNAMIC;
  807. ret = mmap_add_region_check(ctx, mm);
  808. if (ret != 0)
  809. return ret;
  810. /*
  811. * Find the adequate entry in the mmap array in the same way done for
  812. * static regions in mmap_add_region_ctx().
  813. */
  814. while (((mm_cursor->base_va + mm_cursor->size - 1U) < end_va)
  815. && (mm_cursor->size != 0U)) {
  816. ++mm_cursor;
  817. }
  818. while (((mm_cursor->base_va + mm_cursor->size - 1U) == end_va) &&
  819. (mm_cursor->size != 0U) && (mm_cursor->size < mm->size)) {
  820. ++mm_cursor;
  821. }
  822. /* Make room for new region by moving other regions up by one place */
  823. (void)memmove(mm_cursor + 1U, mm_cursor,
  824. (uintptr_t)mm_last - (uintptr_t)mm_cursor);
  825. /*
  826. * Check we haven't lost the empty sentinel from the end of the array.
  827. * This shouldn't happen as we have checked in mmap_add_region_check
  828. * that there is free space.
  829. */
  830. assert(mm_last->size == 0U);
  831. *mm_cursor = *mm;
  832. /*
  833. * Update the translation tables if the xlat tables are initialized. If
  834. * not, this region will be mapped when they are initialized.
  835. */
  836. if (ctx->initialized) {
  837. end_va = xlat_tables_map_region(ctx, mm_cursor,
  838. 0U, ctx->base_table, ctx->base_table_entries,
  839. ctx->base_level);
  840. #if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY)
  841. xlat_clean_dcache_range((uintptr_t)ctx->base_table,
  842. ctx->base_table_entries * sizeof(uint64_t));
  843. #endif
  844. /* Failed to map, remove mmap entry, unmap and return error. */
  845. if (end_va != (mm_cursor->base_va + mm_cursor->size - 1U)) {
  846. (void)memmove(mm_cursor, mm_cursor + 1U,
  847. (uintptr_t)mm_last - (uintptr_t)mm_cursor);
  848. /*
  849. * Check if the mapping function actually managed to map
  850. * anything. If not, just return now.
  851. */
  852. if (mm->base_va >= end_va)
  853. return -ENOMEM;
  854. /*
  855. * Something went wrong after mapping some table
  856. * entries, undo every change done up to this point.
  857. */
  858. mmap_region_t unmap_mm = {
  859. .base_pa = 0U,
  860. .base_va = mm->base_va,
  861. .size = end_va - mm->base_va,
  862. .attr = 0U
  863. };
  864. xlat_tables_unmap_region(ctx, &unmap_mm, 0U,
  865. ctx->base_table, ctx->base_table_entries,
  866. ctx->base_level);
  867. #if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY)
  868. xlat_clean_dcache_range((uintptr_t)ctx->base_table,
  869. ctx->base_table_entries * sizeof(uint64_t));
  870. #endif
  871. return -ENOMEM;
  872. }
  873. /*
  874. * Make sure that all entries are written to the memory. There
  875. * is no need to invalidate entries when mapping dynamic regions
  876. * because new table/block/page descriptors only replace old
  877. * invalid descriptors, that aren't TLB cached.
  878. */
  879. dsbishst();
  880. }
  881. if (end_pa > ctx->max_pa)
  882. ctx->max_pa = end_pa;
  883. if (end_va > ctx->max_va)
  884. ctx->max_va = end_va;
  885. return 0;
  886. }
  887. int mmap_add_dynamic_region_alloc_va_ctx(xlat_ctx_t *ctx, mmap_region_t *mm)
  888. {
  889. mm->base_va = ctx->max_va + 1UL;
  890. if (mm->size == 0U)
  891. return 0;
  892. mmap_alloc_va_align_ctx(ctx, mm);
  893. /* Detect overflows. More checks are done in mmap_add_region_check(). */
  894. if (mm->base_va < ctx->max_va) {
  895. return -ENOMEM;
  896. }
  897. return mmap_add_dynamic_region_ctx(ctx, mm);
  898. }
  899. /*
  900. * Removes the region with given base Virtual Address and size from the given
  901. * context.
  902. *
  903. * Returns:
  904. * 0: Success.
  905. * EINVAL: Invalid values were used as arguments (region not found).
  906. * EPERM: Tried to remove a static region.
  907. */
  908. int mmap_remove_dynamic_region_ctx(xlat_ctx_t *ctx, uintptr_t base_va,
  909. size_t size)
  910. {
  911. mmap_region_t *mm = ctx->mmap;
  912. const mmap_region_t *mm_last = mm + ctx->mmap_num;
  913. int update_max_va_needed = 0;
  914. int update_max_pa_needed = 0;
  915. /* Check sanity of mmap array. */
  916. assert(mm[ctx->mmap_num].size == 0U);
  917. while (mm->size != 0U) {
  918. if ((mm->base_va == base_va) && (mm->size == size))
  919. break;
  920. ++mm;
  921. }
  922. /* Check that the region was found */
  923. if (mm->size == 0U)
  924. return -EINVAL;
  925. /* If the region is static it can't be removed */
  926. if ((mm->attr & MT_DYNAMIC) == 0U)
  927. return -EPERM;
  928. /* Check if this region is using the top VAs or PAs. */
  929. if ((mm->base_va + mm->size - 1U) == ctx->max_va)
  930. update_max_va_needed = 1;
  931. if ((mm->base_pa + mm->size - 1U) == ctx->max_pa)
  932. update_max_pa_needed = 1;
  933. /* Update the translation tables if needed */
  934. if (ctx->initialized) {
  935. xlat_tables_unmap_region(ctx, mm, 0U, ctx->base_table,
  936. ctx->base_table_entries,
  937. ctx->base_level);
  938. #if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY)
  939. xlat_clean_dcache_range((uintptr_t)ctx->base_table,
  940. ctx->base_table_entries * sizeof(uint64_t));
  941. #endif
  942. xlat_arch_tlbi_va_sync();
  943. }
  944. /* Remove this region by moving the rest down by one place. */
  945. (void)memmove(mm, mm + 1U, (uintptr_t)mm_last - (uintptr_t)mm);
  946. /* Check if we need to update the max VAs and PAs */
  947. if (update_max_va_needed == 1) {
  948. ctx->max_va = 0U;
  949. mm = ctx->mmap;
  950. while (mm->size != 0U) {
  951. if ((mm->base_va + mm->size - 1U) > ctx->max_va)
  952. ctx->max_va = mm->base_va + mm->size - 1U;
  953. ++mm;
  954. }
  955. }
  956. if (update_max_pa_needed == 1) {
  957. ctx->max_pa = 0U;
  958. mm = ctx->mmap;
  959. while (mm->size != 0U) {
  960. if ((mm->base_pa + mm->size - 1U) > ctx->max_pa)
  961. ctx->max_pa = mm->base_pa + mm->size - 1U;
  962. ++mm;
  963. }
  964. }
  965. return 0;
  966. }
  967. void xlat_setup_dynamic_ctx(xlat_ctx_t *ctx, unsigned long long pa_max,
  968. uintptr_t va_max, struct mmap_region *mmap,
  969. unsigned int mmap_num, uint64_t **tables,
  970. unsigned int tables_num, uint64_t *base_table,
  971. int xlat_regime, int *mapped_regions)
  972. {
  973. ctx->xlat_regime = xlat_regime;
  974. ctx->pa_max_address = pa_max;
  975. ctx->va_max_address = va_max;
  976. ctx->mmap = mmap;
  977. ctx->mmap_num = mmap_num;
  978. memset(ctx->mmap, 0, sizeof(struct mmap_region) * mmap_num);
  979. ctx->tables = (void *) tables;
  980. ctx->tables_num = tables_num;
  981. uintptr_t va_space_size = va_max + 1;
  982. ctx->base_level = GET_XLAT_TABLE_LEVEL_BASE(va_space_size);
  983. ctx->base_table = base_table;
  984. ctx->base_table_entries = GET_NUM_BASE_LEVEL_ENTRIES(va_space_size);
  985. ctx->tables_mapped_regions = mapped_regions;
  986. ctx->max_pa = 0;
  987. ctx->max_va = 0;
  988. ctx->initialized = 0;
  989. }
  990. #endif /* PLAT_XLAT_TABLES_DYNAMIC */
  991. void __init init_xlat_tables_ctx(xlat_ctx_t *ctx)
  992. {
  993. assert(ctx != NULL);
  994. assert(!ctx->initialized);
  995. assert((ctx->xlat_regime == EL3_REGIME) ||
  996. (ctx->xlat_regime == EL2_REGIME) ||
  997. (ctx->xlat_regime == EL1_EL0_REGIME));
  998. assert(!is_mmu_enabled_ctx(ctx));
  999. mmap_region_t *mm = ctx->mmap;
  1000. assert(ctx->va_max_address >=
  1001. (xlat_get_min_virt_addr_space_size() - 1U));
  1002. assert(ctx->va_max_address <= (MAX_VIRT_ADDR_SPACE_SIZE - 1U));
  1003. assert(IS_POWER_OF_TWO(ctx->va_max_address + 1U));
  1004. xlat_mmap_print(mm);
  1005. /* All tables must be zeroed before mapping any region. */
  1006. for (unsigned int i = 0U; i < ctx->base_table_entries; i++)
  1007. ctx->base_table[i] = INVALID_DESC;
  1008. for (int j = 0; j < ctx->tables_num; j++) {
  1009. #if PLAT_XLAT_TABLES_DYNAMIC
  1010. ctx->tables_mapped_regions[j] = 0;
  1011. #endif
  1012. for (unsigned int i = 0U; i < XLAT_TABLE_ENTRIES; i++)
  1013. ctx->tables[j][i] = INVALID_DESC;
  1014. }
  1015. while (mm->size != 0U) {
  1016. uintptr_t end_va = xlat_tables_map_region(ctx, mm, 0U,
  1017. ctx->base_table, ctx->base_table_entries,
  1018. ctx->base_level);
  1019. #if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY)
  1020. xlat_clean_dcache_range((uintptr_t)ctx->base_table,
  1021. ctx->base_table_entries * sizeof(uint64_t));
  1022. #endif
  1023. if (end_va != (mm->base_va + mm->size - 1U)) {
  1024. ERROR("Not enough memory to map region:\n"
  1025. " VA:0x%lx PA:0x%llx size:0x%zx attr:0x%x\n",
  1026. mm->base_va, mm->base_pa, mm->size, mm->attr);
  1027. panic();
  1028. }
  1029. mm++;
  1030. }
  1031. assert(ctx->pa_max_address <= xlat_arch_get_max_supported_pa());
  1032. assert(ctx->max_va <= ctx->va_max_address);
  1033. assert(ctx->max_pa <= ctx->pa_max_address);
  1034. ctx->initialized = true;
  1035. xlat_tables_print(ctx);
  1036. }