Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017, ARM Limited and Contributors. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are met: |
| 6 | * |
| 7 | * Redistributions of source code must retain the above copyright notice, this |
| 8 | * list of conditions and the following disclaimer. |
| 9 | * |
| 10 | * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * Neither the name of ARM nor the names of its contributors may be used |
| 15 | * to endorse or promote products derived from this software without specific |
| 16 | * prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | * POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 | |
| 31 | #include <arch.h> |
| 32 | #include <arch_helpers.h> |
| 33 | #include <assert.h> |
| 34 | #include <cassert.h> |
| 35 | #include <common_def.h> |
| 36 | #include <debug.h> |
| 37 | #include <errno.h> |
| 38 | #include <platform_def.h> |
| 39 | #include <string.h> |
| 40 | #include <types.h> |
| 41 | #include <utils.h> |
| 42 | #include <xlat_tables_v2.h> |
| 43 | #ifdef AARCH32 |
| 44 | # include "aarch32/xlat_tables_arch.h" |
| 45 | #else |
| 46 | # include "aarch64/xlat_tables_arch.h" |
| 47 | #endif |
| 48 | #include "xlat_tables_private.h" |
| 49 | |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 50 | #if PLAT_XLAT_TABLES_DYNAMIC |
| 51 | |
| 52 | /* |
| 53 | * The following functions assume that they will be called using subtables only. |
| 54 | * The base table can't be unmapped, so it is not needed to do any special |
| 55 | * handling for it. |
| 56 | */ |
| 57 | |
| 58 | /* |
| 59 | * Returns the index of the array corresponding to the specified translation |
| 60 | * table. |
| 61 | */ |
| 62 | static int xlat_table_get_index(xlat_ctx_t *ctx, const uint64_t *table) |
| 63 | { |
| 64 | for (int i = 0; i < ctx->tables_num; i++) |
| 65 | if (ctx->tables[i] == table) |
| 66 | return i; |
| 67 | |
| 68 | /* |
| 69 | * Maybe we were asked to get the index of the base level table, which |
| 70 | * should never happen. |
| 71 | */ |
| 72 | assert(0); |
| 73 | |
| 74 | return -1; |
| 75 | } |
| 76 | |
| 77 | /* Returns a pointer to an empty translation table. */ |
| 78 | static uint64_t *xlat_table_get_empty(xlat_ctx_t *ctx) |
| 79 | { |
| 80 | for (int i = 0; i < ctx->tables_num; i++) |
| 81 | if (ctx->tables_mapped_regions[i] == 0) |
| 82 | return ctx->tables[i]; |
| 83 | |
| 84 | return NULL; |
| 85 | } |
| 86 | |
| 87 | /* Increments region count for a given table. */ |
| 88 | static void xlat_table_inc_regions_count(xlat_ctx_t *ctx, const uint64_t *table) |
| 89 | { |
| 90 | ctx->tables_mapped_regions[xlat_table_get_index(ctx, table)]++; |
| 91 | } |
| 92 | |
| 93 | /* Decrements region count for a given table. */ |
| 94 | static void xlat_table_dec_regions_count(xlat_ctx_t *ctx, const uint64_t *table) |
| 95 | { |
| 96 | ctx->tables_mapped_regions[xlat_table_get_index(ctx, table)]--; |
| 97 | } |
| 98 | |
| 99 | /* Returns 0 if the speficied table isn't empty, otherwise 1. */ |
| 100 | static int xlat_table_is_empty(xlat_ctx_t *ctx, const uint64_t *table) |
| 101 | { |
| 102 | return !ctx->tables_mapped_regions[xlat_table_get_index(ctx, table)]; |
| 103 | } |
| 104 | |
| 105 | #else /* PLAT_XLAT_TABLES_DYNAMIC */ |
| 106 | |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 107 | /* Returns a pointer to the first empty translation table. */ |
| 108 | static uint64_t *xlat_table_get_empty(xlat_ctx_t *ctx) |
| 109 | { |
| 110 | assert(ctx->next_table < ctx->tables_num); |
| 111 | |
| 112 | return ctx->tables[ctx->next_table++]; |
| 113 | } |
| 114 | |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 115 | #endif /* PLAT_XLAT_TABLES_DYNAMIC */ |
| 116 | |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 117 | /* Returns a block/page table descriptor for the given level and attributes. */ |
Sandrine Bailleux | 04980a3 | 2017-04-19 14:02:23 +0100 | [diff] [blame] | 118 | static uint64_t xlat_desc(mmap_attr_t attr, unsigned long long addr_pa, |
Antonio Nino Diaz | efabaa9 | 2017-04-27 13:30:22 +0100 | [diff] [blame^] | 119 | int level, uint64_t execute_never_mask) |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 120 | { |
| 121 | uint64_t desc; |
| 122 | int mem_type; |
| 123 | |
| 124 | /* Make sure that the granularity is fine enough to map this address. */ |
| 125 | assert((addr_pa & XLAT_BLOCK_MASK(level)) == 0); |
| 126 | |
| 127 | desc = addr_pa; |
| 128 | /* |
| 129 | * There are different translation table descriptors for level 3 and the |
| 130 | * rest. |
| 131 | */ |
| 132 | desc |= (level == XLAT_TABLE_LEVEL_MAX) ? PAGE_DESC : BLOCK_DESC; |
| 133 | /* |
| 134 | * Always set the access flag, as TF doesn't manage access flag faults. |
| 135 | * Deduce other fields of the descriptor based on the MT_NS and MT_RW |
| 136 | * memory region attributes. |
| 137 | */ |
| 138 | desc |= (attr & MT_NS) ? LOWER_ATTRS(NS) : 0; |
| 139 | desc |= (attr & MT_RW) ? LOWER_ATTRS(AP_RW) : LOWER_ATTRS(AP_RO); |
| 140 | desc |= LOWER_ATTRS(ACCESS_FLAG); |
| 141 | |
| 142 | /* |
| 143 | * Deduce shareability domain and executability of the memory region |
| 144 | * from the memory type of the attributes (MT_TYPE). |
| 145 | * |
| 146 | * Data accesses to device memory and non-cacheable normal memory are |
| 147 | * coherent for all observers in the system, and correspondingly are |
| 148 | * always treated as being Outer Shareable. Therefore, for these 2 types |
| 149 | * of memory, it is not strictly needed to set the shareability field |
| 150 | * in the translation tables. |
| 151 | */ |
| 152 | mem_type = MT_TYPE(attr); |
| 153 | if (mem_type == MT_DEVICE) { |
| 154 | desc |= LOWER_ATTRS(ATTR_DEVICE_INDEX | OSH); |
| 155 | /* |
| 156 | * Always map device memory as execute-never. |
| 157 | * This is to avoid the possibility of a speculative instruction |
| 158 | * fetch, which could be an issue if this memory region |
| 159 | * corresponds to a read-sensitive peripheral. |
| 160 | */ |
Antonio Nino Diaz | efabaa9 | 2017-04-27 13:30:22 +0100 | [diff] [blame^] | 161 | desc |= execute_never_mask; |
| 162 | |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 163 | } else { /* Normal memory */ |
| 164 | /* |
| 165 | * Always map read-write normal memory as execute-never. |
| 166 | * (Trusted Firmware doesn't self-modify its code, therefore |
| 167 | * R/W memory is reserved for data storage, which must not be |
| 168 | * executable.) |
| 169 | * Note that setting the XN bit here is for consistency only. |
Antonio Nino Diaz | efabaa9 | 2017-04-27 13:30:22 +0100 | [diff] [blame^] | 170 | * The function that enables the MMU sets the SCTLR_ELx.WXN bit, |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 171 | * which makes any writable memory region to be treated as |
| 172 | * execute-never, regardless of the value of the XN bit in the |
| 173 | * translation table. |
| 174 | * |
| 175 | * For read-only memory, rely on the MT_EXECUTE/MT_EXECUTE_NEVER |
| 176 | * attribute to figure out the value of the XN bit. |
| 177 | */ |
Antonio Nino Diaz | efabaa9 | 2017-04-27 13:30:22 +0100 | [diff] [blame^] | 178 | if ((attr & MT_RW) || (attr & MT_EXECUTE_NEVER)) { |
| 179 | desc |= execute_never_mask; |
| 180 | } |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 181 | |
| 182 | if (mem_type == MT_MEMORY) { |
| 183 | desc |= LOWER_ATTRS(ATTR_IWBWA_OWBWA_NTR_INDEX | ISH); |
| 184 | } else { |
| 185 | assert(mem_type == MT_NON_CACHEABLE); |
| 186 | desc |= LOWER_ATTRS(ATTR_NON_CACHEABLE_INDEX | OSH); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | return desc; |
| 191 | } |
| 192 | |
| 193 | /* |
| 194 | * Enumeration of actions that can be made when mapping table entries depending |
| 195 | * on the previous value in that entry and information about the region being |
| 196 | * mapped. |
| 197 | */ |
| 198 | typedef enum { |
| 199 | |
| 200 | /* Do nothing */ |
| 201 | ACTION_NONE, |
| 202 | |
| 203 | /* Write a block (or page, if in level 3) entry. */ |
| 204 | ACTION_WRITE_BLOCK_ENTRY, |
| 205 | |
| 206 | /* |
| 207 | * Create a new table and write a table entry pointing to it. Recurse |
| 208 | * into it for further processing. |
| 209 | */ |
| 210 | ACTION_CREATE_NEW_TABLE, |
| 211 | |
| 212 | /* |
| 213 | * There is a table descriptor in this entry, read it and recurse into |
| 214 | * that table for further processing. |
| 215 | */ |
| 216 | ACTION_RECURSE_INTO_TABLE, |
| 217 | |
| 218 | } action_t; |
| 219 | |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 220 | #if PLAT_XLAT_TABLES_DYNAMIC |
| 221 | |
| 222 | /* |
| 223 | * Recursive function that writes to the translation tables and unmaps the |
| 224 | * specified region. |
| 225 | */ |
| 226 | static void xlat_tables_unmap_region(xlat_ctx_t *ctx, mmap_region_t *mm, |
| 227 | const uintptr_t table_base_va, |
| 228 | uint64_t *const table_base, |
| 229 | const int table_entries, |
| 230 | const int level) |
| 231 | { |
| 232 | assert(level >= ctx->base_level && level <= XLAT_TABLE_LEVEL_MAX); |
| 233 | |
| 234 | uint64_t *subtable; |
| 235 | uint64_t desc; |
| 236 | |
| 237 | uintptr_t table_idx_va; |
| 238 | uintptr_t table_idx_end_va; /* End VA of this entry */ |
| 239 | |
| 240 | uintptr_t region_end_va = mm->base_va + mm->size - 1; |
| 241 | |
| 242 | int table_idx; |
| 243 | |
| 244 | if (mm->base_va > table_base_va) { |
| 245 | /* Find the first index of the table affected by the region. */ |
| 246 | table_idx_va = mm->base_va & ~XLAT_BLOCK_MASK(level); |
| 247 | |
| 248 | table_idx = (table_idx_va - table_base_va) >> |
| 249 | XLAT_ADDR_SHIFT(level); |
| 250 | |
| 251 | assert(table_idx < table_entries); |
| 252 | } else { |
| 253 | /* Start from the beginning of the table. */ |
| 254 | table_idx_va = table_base_va; |
| 255 | table_idx = 0; |
| 256 | } |
| 257 | |
| 258 | while (table_idx < table_entries) { |
| 259 | |
| 260 | table_idx_end_va = table_idx_va + XLAT_BLOCK_SIZE(level) - 1; |
| 261 | |
| 262 | desc = table_base[table_idx]; |
| 263 | uint64_t desc_type = desc & DESC_MASK; |
| 264 | |
| 265 | action_t action = ACTION_NONE; |
| 266 | |
| 267 | if ((mm->base_va <= table_idx_va) && |
| 268 | (region_end_va >= table_idx_end_va)) { |
| 269 | |
| 270 | /* Region covers all block */ |
| 271 | |
| 272 | if (level == 3) { |
| 273 | /* |
| 274 | * Last level, only page descriptors allowed, |
| 275 | * erase it. |
| 276 | */ |
| 277 | assert(desc_type == PAGE_DESC); |
| 278 | |
| 279 | action = ACTION_WRITE_BLOCK_ENTRY; |
| 280 | } else { |
| 281 | /* |
| 282 | * Other levels can have table descriptors. If |
| 283 | * so, recurse into it and erase descriptors |
| 284 | * inside it as needed. If there is a block |
| 285 | * descriptor, just erase it. If an invalid |
| 286 | * descriptor is found, this table isn't |
| 287 | * actually mapped, which shouldn't happen. |
| 288 | */ |
| 289 | if (desc_type == TABLE_DESC) { |
| 290 | action = ACTION_RECURSE_INTO_TABLE; |
| 291 | } else { |
| 292 | assert(desc_type == BLOCK_DESC); |
| 293 | action = ACTION_WRITE_BLOCK_ENTRY; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | } else if ((mm->base_va <= table_idx_end_va) || |
| 298 | (region_end_va >= table_idx_va)) { |
| 299 | |
| 300 | /* |
| 301 | * Region partially covers block. |
| 302 | * |
| 303 | * It can't happen in level 3. |
| 304 | * |
| 305 | * There must be a table descriptor here, if not there |
| 306 | * was a problem when mapping the region. |
| 307 | */ |
| 308 | |
| 309 | assert(level < 3); |
| 310 | |
| 311 | assert(desc_type == TABLE_DESC); |
| 312 | |
| 313 | action = ACTION_RECURSE_INTO_TABLE; |
| 314 | } |
| 315 | |
| 316 | if (action == ACTION_WRITE_BLOCK_ENTRY) { |
| 317 | |
| 318 | table_base[table_idx] = INVALID_DESC; |
| 319 | xlat_arch_tlbi_va(table_idx_va); |
| 320 | |
| 321 | } else if (action == ACTION_RECURSE_INTO_TABLE) { |
| 322 | |
| 323 | subtable = (uint64_t *)(uintptr_t)(desc & TABLE_ADDR_MASK); |
| 324 | |
| 325 | /* Recurse to write into subtable */ |
| 326 | xlat_tables_unmap_region(ctx, mm, table_idx_va, |
| 327 | subtable, XLAT_TABLE_ENTRIES, |
| 328 | level + 1); |
| 329 | |
| 330 | /* |
| 331 | * If the subtable is now empty, remove its reference. |
| 332 | */ |
| 333 | if (xlat_table_is_empty(ctx, subtable)) { |
| 334 | table_base[table_idx] = INVALID_DESC; |
| 335 | xlat_arch_tlbi_va(table_idx_va); |
| 336 | } |
| 337 | |
| 338 | } else { |
| 339 | assert(action == ACTION_NONE); |
| 340 | } |
| 341 | |
| 342 | table_idx++; |
| 343 | table_idx_va += XLAT_BLOCK_SIZE(level); |
| 344 | |
| 345 | /* If reached the end of the region, exit */ |
| 346 | if (region_end_va <= table_idx_va) |
| 347 | break; |
| 348 | } |
| 349 | |
| 350 | if (level > ctx->base_level) |
| 351 | xlat_table_dec_regions_count(ctx, table_base); |
| 352 | } |
| 353 | |
| 354 | #endif /* PLAT_XLAT_TABLES_DYNAMIC */ |
| 355 | |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 356 | /* |
| 357 | * From the given arguments, it decides which action to take when mapping the |
| 358 | * specified region. |
| 359 | */ |
| 360 | static action_t xlat_tables_map_region_action(const mmap_region_t *mm, |
| 361 | const int desc_type, const unsigned long long dest_pa, |
| 362 | const uintptr_t table_entry_base_va, const int level) |
| 363 | { |
| 364 | uintptr_t mm_end_va = mm->base_va + mm->size - 1; |
| 365 | uintptr_t table_entry_end_va = |
| 366 | table_entry_base_va + XLAT_BLOCK_SIZE(level) - 1; |
| 367 | |
| 368 | /* |
| 369 | * The descriptor types allowed depend on the current table level. |
| 370 | */ |
| 371 | |
| 372 | if ((mm->base_va <= table_entry_base_va) && |
| 373 | (mm_end_va >= table_entry_end_va)) { |
| 374 | |
| 375 | /* |
| 376 | * Table entry is covered by region |
| 377 | * -------------------------------- |
| 378 | * |
| 379 | * This means that this table entry can describe the whole |
| 380 | * translation with this granularity in principle. |
| 381 | */ |
| 382 | |
| 383 | if (level == 3) { |
| 384 | /* |
| 385 | * Last level, only page descriptors are allowed. |
| 386 | */ |
| 387 | if (desc_type == PAGE_DESC) { |
| 388 | /* |
| 389 | * There's another region mapped here, don't |
| 390 | * overwrite. |
| 391 | */ |
| 392 | return ACTION_NONE; |
| 393 | } else { |
| 394 | assert(desc_type == INVALID_DESC); |
| 395 | return ACTION_WRITE_BLOCK_ENTRY; |
| 396 | } |
| 397 | |
| 398 | } else { |
| 399 | |
| 400 | /* |
| 401 | * Other levels. Table descriptors are allowed. Block |
| 402 | * descriptors too, but they have some limitations. |
| 403 | */ |
| 404 | |
| 405 | if (desc_type == TABLE_DESC) { |
| 406 | /* There's already a table, recurse into it. */ |
| 407 | return ACTION_RECURSE_INTO_TABLE; |
| 408 | |
| 409 | } else if (desc_type == INVALID_DESC) { |
| 410 | /* |
| 411 | * There's nothing mapped here, create a new |
| 412 | * entry. |
| 413 | * |
| 414 | * Check if the destination granularity allows |
| 415 | * us to use a block descriptor or we need a |
| 416 | * finer table for it. |
| 417 | * |
| 418 | * Also, check if the current level allows block |
| 419 | * descriptors. If not, create a table instead. |
| 420 | */ |
| 421 | if ((dest_pa & XLAT_BLOCK_MASK(level)) || |
| 422 | (level < MIN_LVL_BLOCK_DESC)) |
| 423 | return ACTION_CREATE_NEW_TABLE; |
| 424 | else |
| 425 | return ACTION_WRITE_BLOCK_ENTRY; |
| 426 | |
| 427 | } else { |
| 428 | /* |
| 429 | * There's another region mapped here, don't |
| 430 | * overwrite. |
| 431 | */ |
| 432 | assert(desc_type == BLOCK_DESC); |
| 433 | |
| 434 | return ACTION_NONE; |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | } else if ((mm->base_va <= table_entry_end_va) || |
| 439 | (mm_end_va >= table_entry_base_va)) { |
| 440 | |
| 441 | /* |
| 442 | * Region partially covers table entry |
| 443 | * ----------------------------------- |
| 444 | * |
| 445 | * This means that this table entry can't describe the whole |
| 446 | * translation, a finer table is needed. |
| 447 | |
| 448 | * There cannot be partial block overlaps in level 3. If that |
| 449 | * happens, some of the preliminary checks when adding the |
| 450 | * mmap region failed to detect that PA and VA must at least be |
| 451 | * aligned to PAGE_SIZE. |
| 452 | */ |
| 453 | assert(level < 3); |
| 454 | |
| 455 | if (desc_type == INVALID_DESC) { |
| 456 | /* |
| 457 | * The block is not fully covered by the region. Create |
| 458 | * a new table, recurse into it and try to map the |
| 459 | * region with finer granularity. |
| 460 | */ |
| 461 | return ACTION_CREATE_NEW_TABLE; |
| 462 | |
| 463 | } else { |
| 464 | assert(desc_type == TABLE_DESC); |
| 465 | /* |
| 466 | * The block is not fully covered by the region, but |
| 467 | * there is already a table here. Recurse into it and |
| 468 | * try to map with finer granularity. |
| 469 | * |
| 470 | * PAGE_DESC for level 3 has the same value as |
| 471 | * TABLE_DESC, but this code can't run on a level 3 |
| 472 | * table because there can't be overlaps in level 3. |
| 473 | */ |
| 474 | return ACTION_RECURSE_INTO_TABLE; |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | /* |
| 479 | * This table entry is outside of the region specified in the arguments, |
| 480 | * don't write anything to it. |
| 481 | */ |
| 482 | return ACTION_NONE; |
| 483 | } |
| 484 | |
| 485 | /* |
| 486 | * Recursive function that writes to the translation tables and maps the |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 487 | * specified region. On success, it returns the VA of the last byte that was |
| 488 | * succesfully mapped. On error, it returns the VA of the next entry that |
| 489 | * should have been mapped. |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 490 | */ |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 491 | static uintptr_t xlat_tables_map_region(xlat_ctx_t *ctx, mmap_region_t *mm, |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 492 | const uintptr_t table_base_va, |
| 493 | uint64_t *const table_base, |
| 494 | const int table_entries, |
| 495 | const int level) |
| 496 | { |
| 497 | assert(level >= ctx->base_level && level <= XLAT_TABLE_LEVEL_MAX); |
| 498 | |
| 499 | uintptr_t mm_end_va = mm->base_va + mm->size - 1; |
| 500 | |
| 501 | uintptr_t table_idx_va; |
| 502 | unsigned long long table_idx_pa; |
| 503 | |
| 504 | uint64_t *subtable; |
| 505 | uint64_t desc; |
| 506 | |
| 507 | int table_idx; |
| 508 | |
| 509 | if (mm->base_va > table_base_va) { |
| 510 | /* Find the first index of the table affected by the region. */ |
| 511 | table_idx_va = mm->base_va & ~XLAT_BLOCK_MASK(level); |
| 512 | |
| 513 | table_idx = (table_idx_va - table_base_va) >> |
| 514 | XLAT_ADDR_SHIFT(level); |
| 515 | |
| 516 | assert(table_idx < table_entries); |
| 517 | } else { |
| 518 | /* Start from the beginning of the table. */ |
| 519 | table_idx_va = table_base_va; |
| 520 | table_idx = 0; |
| 521 | } |
| 522 | |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 523 | #if PLAT_XLAT_TABLES_DYNAMIC |
| 524 | if (level > ctx->base_level) |
| 525 | xlat_table_inc_regions_count(ctx, table_base); |
| 526 | #endif |
| 527 | |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 528 | while (table_idx < table_entries) { |
| 529 | |
| 530 | desc = table_base[table_idx]; |
| 531 | |
| 532 | table_idx_pa = mm->base_pa + table_idx_va - mm->base_va; |
| 533 | |
| 534 | action_t action = xlat_tables_map_region_action(mm, |
| 535 | desc & DESC_MASK, table_idx_pa, table_idx_va, level); |
| 536 | |
| 537 | if (action == ACTION_WRITE_BLOCK_ENTRY) { |
| 538 | |
| 539 | table_base[table_idx] = |
Antonio Nino Diaz | efabaa9 | 2017-04-27 13:30:22 +0100 | [diff] [blame^] | 540 | xlat_desc(mm->attr, table_idx_pa, level, |
| 541 | ctx->execute_never_mask); |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 542 | |
| 543 | } else if (action == ACTION_CREATE_NEW_TABLE) { |
| 544 | |
| 545 | subtable = xlat_table_get_empty(ctx); |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 546 | if (subtable == NULL) { |
| 547 | /* Not enough free tables to map this region */ |
| 548 | return table_idx_va; |
| 549 | } |
| 550 | |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 551 | /* Point to new subtable from this one. */ |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 552 | table_base[table_idx] = TABLE_DESC | (unsigned long)subtable; |
| 553 | |
| 554 | /* Recurse to write into subtable */ |
| 555 | uintptr_t end_va = xlat_tables_map_region(ctx, mm, table_idx_va, |
| 556 | subtable, XLAT_TABLE_ENTRIES, |
| 557 | level + 1); |
| 558 | if (end_va != table_idx_va + XLAT_BLOCK_SIZE(level) - 1) |
| 559 | return end_va; |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 560 | |
| 561 | } else if (action == ACTION_RECURSE_INTO_TABLE) { |
| 562 | |
| 563 | subtable = (uint64_t *)(uintptr_t)(desc & TABLE_ADDR_MASK); |
| 564 | /* Recurse to write into subtable */ |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 565 | uintptr_t end_va = xlat_tables_map_region(ctx, mm, table_idx_va, |
| 566 | subtable, XLAT_TABLE_ENTRIES, |
| 567 | level + 1); |
| 568 | if (end_va != table_idx_va + XLAT_BLOCK_SIZE(level) - 1) |
| 569 | return end_va; |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 570 | |
| 571 | } else { |
| 572 | |
| 573 | assert(action == ACTION_NONE); |
| 574 | |
| 575 | } |
| 576 | |
| 577 | table_idx++; |
| 578 | table_idx_va += XLAT_BLOCK_SIZE(level); |
| 579 | |
| 580 | /* If reached the end of the region, exit */ |
| 581 | if (mm_end_va <= table_idx_va) |
| 582 | break; |
| 583 | } |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 584 | |
| 585 | return table_idx_va - 1; |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | void print_mmap(mmap_region_t *const mmap) |
| 589 | { |
| 590 | #if LOG_LEVEL >= LOG_LEVEL_VERBOSE |
| 591 | tf_printf("mmap:\n"); |
| 592 | mmap_region_t *mm = mmap; |
| 593 | |
| 594 | while (mm->size) { |
| 595 | tf_printf(" VA:%p PA:0x%llx size:0x%zx attr:0x%x\n", |
| 596 | (void *)mm->base_va, mm->base_pa, |
| 597 | mm->size, mm->attr); |
| 598 | ++mm; |
| 599 | }; |
| 600 | tf_printf("\n"); |
| 601 | #endif |
| 602 | } |
| 603 | |
| 604 | /* |
| 605 | * Function that verifies that a region can be mapped. |
| 606 | * Returns: |
| 607 | * 0: Success, the mapping is allowed. |
| 608 | * EINVAL: Invalid values were used as arguments. |
| 609 | * ERANGE: The memory limits were surpassed. |
| 610 | * ENOMEM: There is not enough memory in the mmap array. |
| 611 | * EPERM: Region overlaps another one in an invalid way. |
| 612 | */ |
| 613 | static int mmap_add_region_check(xlat_ctx_t *ctx, unsigned long long base_pa, |
| 614 | uintptr_t base_va, size_t size, |
Sandrine Bailleux | 04980a3 | 2017-04-19 14:02:23 +0100 | [diff] [blame] | 615 | mmap_attr_t attr) |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 616 | { |
| 617 | mmap_region_t *mm = ctx->mmap; |
| 618 | unsigned long long end_pa = base_pa + size - 1; |
| 619 | uintptr_t end_va = base_va + size - 1; |
| 620 | |
| 621 | if (!IS_PAGE_ALIGNED(base_pa) || !IS_PAGE_ALIGNED(base_va) || |
| 622 | !IS_PAGE_ALIGNED(size)) |
| 623 | return -EINVAL; |
| 624 | |
| 625 | /* Check for overflows */ |
| 626 | if ((base_pa > end_pa) || (base_va > end_va)) |
| 627 | return -ERANGE; |
| 628 | |
| 629 | if ((base_va + (uintptr_t)size - (uintptr_t)1) > ctx->va_max_address) |
| 630 | return -ERANGE; |
| 631 | |
| 632 | if ((base_pa + (unsigned long long)size - 1ULL) > ctx->pa_max_address) |
| 633 | return -ERANGE; |
| 634 | |
| 635 | /* Check that there is space in the mmap array */ |
| 636 | if (ctx->mmap[ctx->mmap_num - 1].size != 0) |
| 637 | return -ENOMEM; |
| 638 | |
| 639 | /* Check for PAs and VAs overlaps with all other regions */ |
| 640 | for (mm = ctx->mmap; mm->size; ++mm) { |
| 641 | |
| 642 | uintptr_t mm_end_va = mm->base_va + mm->size - 1; |
| 643 | |
| 644 | /* |
| 645 | * Check if one of the regions is completely inside the other |
| 646 | * one. |
| 647 | */ |
| 648 | int fully_overlapped_va = |
| 649 | ((base_va >= mm->base_va) && (end_va <= mm_end_va)) || |
| 650 | ((mm->base_va >= base_va) && (mm_end_va <= end_va)); |
| 651 | |
| 652 | /* |
| 653 | * Full VA overlaps are only allowed if both regions are |
| 654 | * identity mapped (zero offset) or have the same VA to PA |
| 655 | * offset. Also, make sure that it's not the exact same area. |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 656 | * This can only be done with static regions. |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 657 | */ |
| 658 | if (fully_overlapped_va) { |
| 659 | |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 660 | #if PLAT_XLAT_TABLES_DYNAMIC |
| 661 | if ((attr & MT_DYNAMIC) || (mm->attr & MT_DYNAMIC)) |
| 662 | return -EPERM; |
| 663 | #endif /* PLAT_XLAT_TABLES_DYNAMIC */ |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 664 | if ((mm->base_va - mm->base_pa) != (base_va - base_pa)) |
| 665 | return -EPERM; |
| 666 | |
| 667 | if ((base_va == mm->base_va) && (size == mm->size)) |
| 668 | return -EPERM; |
| 669 | |
| 670 | } else { |
| 671 | /* |
| 672 | * If the regions do not have fully overlapping VAs, |
| 673 | * then they must have fully separated VAs and PAs. |
| 674 | * Partial overlaps are not allowed |
| 675 | */ |
| 676 | |
| 677 | unsigned long long mm_end_pa = |
| 678 | mm->base_pa + mm->size - 1; |
| 679 | |
| 680 | int separated_pa = |
| 681 | (end_pa < mm->base_pa) || (base_pa > mm_end_pa); |
| 682 | int separated_va = |
| 683 | (end_va < mm->base_va) || (base_va > mm_end_va); |
| 684 | |
| 685 | if (!(separated_va && separated_pa)) |
| 686 | return -EPERM; |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | return 0; |
| 691 | } |
| 692 | |
| 693 | void mmap_add_region_ctx(xlat_ctx_t *ctx, mmap_region_t *mm) |
| 694 | { |
| 695 | mmap_region_t *mm_cursor = ctx->mmap; |
| 696 | mmap_region_t *mm_last = mm_cursor + ctx->mmap_num; |
| 697 | unsigned long long end_pa = mm->base_pa + mm->size - 1; |
| 698 | uintptr_t end_va = mm->base_va + mm->size - 1; |
| 699 | int ret; |
| 700 | |
| 701 | /* Ignore empty regions */ |
| 702 | if (!mm->size) |
| 703 | return; |
| 704 | |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 705 | /* Static regions must be added before initializing the xlat tables. */ |
| 706 | assert(!ctx->initialized); |
| 707 | |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 708 | ret = mmap_add_region_check(ctx, mm->base_pa, mm->base_va, mm->size, |
| 709 | mm->attr); |
| 710 | if (ret != 0) { |
| 711 | ERROR("mmap_add_region_check() failed. error %d\n", ret); |
| 712 | assert(0); |
| 713 | return; |
| 714 | } |
| 715 | |
| 716 | /* |
| 717 | * Find correct place in mmap to insert new region. |
| 718 | * |
| 719 | * 1 - Lower region VA end first. |
| 720 | * 2 - Smaller region size first. |
| 721 | * |
| 722 | * VA 0 0xFF |
| 723 | * |
| 724 | * 1st |------| |
| 725 | * 2nd |------------| |
| 726 | * 3rd |------| |
| 727 | * 4th |---| |
| 728 | * 5th |---| |
| 729 | * 6th |----------| |
| 730 | * 7th |-------------------------------------| |
| 731 | * |
| 732 | * This is required for overlapping regions only. It simplifies adding |
| 733 | * regions with the loop in xlat_tables_init_internal because the outer |
| 734 | * ones won't overwrite block or page descriptors of regions added |
| 735 | * previously. |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 736 | * |
| 737 | * Overlapping is only allowed for static regions. |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 738 | */ |
| 739 | |
| 740 | while ((mm_cursor->base_va + mm_cursor->size - 1) < end_va |
| 741 | && mm_cursor->size) |
| 742 | ++mm_cursor; |
| 743 | |
| 744 | while ((mm_cursor->base_va + mm_cursor->size - 1 == end_va) |
| 745 | && (mm_cursor->size < mm->size)) |
| 746 | ++mm_cursor; |
| 747 | |
| 748 | /* Make room for new region by moving other regions up by one place */ |
| 749 | memmove(mm_cursor + 1, mm_cursor, |
| 750 | (uintptr_t)mm_last - (uintptr_t)mm_cursor); |
| 751 | |
| 752 | /* |
| 753 | * Check we haven't lost the empty sentinel from the end of the array. |
| 754 | * This shouldn't happen as we have checked in mmap_add_region_check |
| 755 | * that there is free space. |
| 756 | */ |
| 757 | assert(mm_last->size == 0); |
| 758 | |
| 759 | mm_cursor->base_pa = mm->base_pa; |
| 760 | mm_cursor->base_va = mm->base_va; |
| 761 | mm_cursor->size = mm->size; |
| 762 | mm_cursor->attr = mm->attr; |
| 763 | |
| 764 | if (end_pa > ctx->max_pa) |
| 765 | ctx->max_pa = end_pa; |
| 766 | if (end_va > ctx->max_va) |
| 767 | ctx->max_va = end_va; |
| 768 | } |
| 769 | |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 770 | #if PLAT_XLAT_TABLES_DYNAMIC |
| 771 | |
| 772 | int mmap_add_dynamic_region_ctx(xlat_ctx_t *ctx, mmap_region_t *mm) |
| 773 | { |
| 774 | mmap_region_t *mm_cursor = ctx->mmap; |
| 775 | mmap_region_t *mm_last = mm_cursor + ctx->mmap_num; |
| 776 | unsigned long long end_pa = mm->base_pa + mm->size - 1; |
| 777 | uintptr_t end_va = mm->base_va + mm->size - 1; |
| 778 | int ret; |
| 779 | |
| 780 | /* Nothing to do */ |
| 781 | if (!mm->size) |
| 782 | return 0; |
| 783 | |
| 784 | ret = mmap_add_region_check(ctx, mm->base_pa, mm->base_va, mm->size, mm->attr | MT_DYNAMIC); |
| 785 | if (ret != 0) |
| 786 | return ret; |
| 787 | |
| 788 | /* |
| 789 | * Find the adequate entry in the mmap array in the same way done for |
| 790 | * static regions in mmap_add_region_ctx(). |
| 791 | */ |
| 792 | |
| 793 | while ((mm_cursor->base_va + mm_cursor->size - 1) < end_va && mm_cursor->size) |
| 794 | ++mm_cursor; |
| 795 | |
| 796 | while ((mm_cursor->base_va + mm_cursor->size - 1 == end_va) && (mm_cursor->size < mm->size)) |
| 797 | ++mm_cursor; |
| 798 | |
| 799 | /* Make room for new region by moving other regions up by one place */ |
| 800 | memmove(mm_cursor + 1, mm_cursor, (uintptr_t)mm_last - (uintptr_t)mm_cursor); |
| 801 | |
| 802 | /* |
| 803 | * Check we haven't lost the empty sentinal from the end of the array. |
| 804 | * This shouldn't happen as we have checked in mmap_add_region_check |
| 805 | * that there is free space. |
| 806 | */ |
| 807 | assert(mm_last->size == 0); |
| 808 | |
| 809 | mm_cursor->base_pa = mm->base_pa; |
| 810 | mm_cursor->base_va = mm->base_va; |
| 811 | mm_cursor->size = mm->size; |
| 812 | mm_cursor->attr = mm->attr | MT_DYNAMIC; |
| 813 | |
| 814 | /* |
| 815 | * Update the translation tables if the xlat tables are initialized. If |
| 816 | * not, this region will be mapped when they are initialized. |
| 817 | */ |
| 818 | if (ctx->initialized) { |
| 819 | uintptr_t end_va = xlat_tables_map_region(ctx, mm_cursor, 0, ctx->base_table, |
| 820 | ctx->base_table_entries, ctx->base_level); |
| 821 | |
| 822 | /* Failed to map, remove mmap entry, unmap and return error. */ |
| 823 | if (end_va != mm_cursor->base_va + mm_cursor->size - 1) { |
| 824 | memmove(mm_cursor, mm_cursor + 1, (uintptr_t)mm_last - (uintptr_t)mm_cursor); |
| 825 | |
| 826 | /* |
| 827 | * Check if the mapping function actually managed to map |
| 828 | * anything. If not, just return now. |
| 829 | */ |
| 830 | if (mm_cursor->base_va >= end_va) |
| 831 | return -ENOMEM; |
| 832 | |
| 833 | /* |
| 834 | * Something went wrong after mapping some table entries, |
| 835 | * undo every change done up to this point. |
| 836 | */ |
| 837 | mmap_region_t unmap_mm = { |
| 838 | .base_pa = 0, |
| 839 | .base_va = mm->base_va, |
| 840 | .size = end_va - mm->base_va, |
| 841 | .attr = 0 |
| 842 | }; |
| 843 | xlat_tables_unmap_region(ctx, &unmap_mm, 0, ctx->base_table, |
| 844 | ctx->base_table_entries, ctx->base_level); |
| 845 | |
| 846 | return -ENOMEM; |
| 847 | } |
| 848 | |
| 849 | /* |
| 850 | * Make sure that all entries are written to the memory. There |
| 851 | * is no need to invalidate entries when mapping dynamic regions |
| 852 | * because new table/block/page descriptors only replace old |
| 853 | * invalid descriptors, that aren't TLB cached. |
| 854 | */ |
| 855 | dsbishst(); |
| 856 | } |
| 857 | |
| 858 | if (end_pa > ctx->max_pa) |
| 859 | ctx->max_pa = end_pa; |
| 860 | if (end_va > ctx->max_va) |
| 861 | ctx->max_va = end_va; |
| 862 | |
| 863 | return 0; |
| 864 | } |
| 865 | |
| 866 | /* |
| 867 | * Removes the region with given base Virtual Address and size from the given |
| 868 | * context. |
| 869 | * |
| 870 | * Returns: |
| 871 | * 0: Success. |
| 872 | * EINVAL: Invalid values were used as arguments (region not found). |
| 873 | * EPERM: Tried to remove a static region. |
| 874 | */ |
| 875 | int mmap_remove_dynamic_region_ctx(xlat_ctx_t *ctx, uintptr_t base_va, |
| 876 | size_t size) |
| 877 | { |
| 878 | mmap_region_t *mm = ctx->mmap; |
| 879 | mmap_region_t *mm_last = mm + ctx->mmap_num; |
| 880 | int update_max_va_needed = 0; |
| 881 | int update_max_pa_needed = 0; |
| 882 | |
| 883 | /* Check sanity of mmap array. */ |
| 884 | assert(mm[ctx->mmap_num].size == 0); |
| 885 | |
| 886 | while (mm->size) { |
| 887 | if ((mm->base_va == base_va) && (mm->size == size)) |
| 888 | break; |
| 889 | ++mm; |
| 890 | } |
| 891 | |
| 892 | /* Check that the region was found */ |
| 893 | if (mm->size == 0) |
| 894 | return -EINVAL; |
| 895 | |
| 896 | /* If the region is static it can't be removed */ |
| 897 | if (!(mm->attr & MT_DYNAMIC)) |
| 898 | return -EPERM; |
| 899 | |
| 900 | /* Check if this region is using the top VAs or PAs. */ |
| 901 | if ((mm->base_va + mm->size - 1) == ctx->max_va) |
| 902 | update_max_va_needed = 1; |
| 903 | if ((mm->base_pa + mm->size - 1) == ctx->max_pa) |
| 904 | update_max_pa_needed = 1; |
| 905 | |
| 906 | /* Update the translation tables if needed */ |
| 907 | if (ctx->initialized) { |
| 908 | xlat_tables_unmap_region(ctx, mm, 0, ctx->base_table, |
| 909 | ctx->base_table_entries, |
| 910 | ctx->base_level); |
| 911 | xlat_arch_tlbi_va_sync(); |
| 912 | } |
| 913 | |
| 914 | /* Remove this region by moving the rest down by one place. */ |
| 915 | memmove(mm, mm + 1, (uintptr_t)mm_last - (uintptr_t)mm); |
| 916 | |
| 917 | /* Check if we need to update the max VAs and PAs */ |
| 918 | if (update_max_va_needed) { |
| 919 | ctx->max_va = 0; |
| 920 | mm = ctx->mmap; |
| 921 | while (mm->size) { |
| 922 | if ((mm->base_va + mm->size - 1) > ctx->max_va) |
| 923 | ctx->max_va = mm->base_va + mm->size - 1; |
| 924 | ++mm; |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | if (update_max_pa_needed) { |
| 929 | ctx->max_pa = 0; |
| 930 | mm = ctx->mmap; |
| 931 | while (mm->size) { |
| 932 | if ((mm->base_pa + mm->size - 1) > ctx->max_pa) |
| 933 | ctx->max_pa = mm->base_pa + mm->size - 1; |
| 934 | ++mm; |
| 935 | } |
| 936 | } |
| 937 | |
| 938 | return 0; |
| 939 | } |
| 940 | |
| 941 | #endif /* PLAT_XLAT_TABLES_DYNAMIC */ |
| 942 | |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 943 | #if LOG_LEVEL >= LOG_LEVEL_VERBOSE |
| 944 | |
| 945 | /* Print the attributes of the specified block descriptor. */ |
Antonio Nino Diaz | efabaa9 | 2017-04-27 13:30:22 +0100 | [diff] [blame^] | 946 | static void xlat_desc_print(uint64_t desc, uint64_t execute_never_mask) |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 947 | { |
| 948 | int mem_type_index = ATTR_INDEX_GET(desc); |
| 949 | |
| 950 | if (mem_type_index == ATTR_IWBWA_OWBWA_NTR_INDEX) { |
| 951 | tf_printf("MEM"); |
| 952 | } else if (mem_type_index == ATTR_NON_CACHEABLE_INDEX) { |
| 953 | tf_printf("NC"); |
| 954 | } else { |
| 955 | assert(mem_type_index == ATTR_DEVICE_INDEX); |
| 956 | tf_printf("DEV"); |
| 957 | } |
| 958 | |
| 959 | tf_printf(LOWER_ATTRS(AP_RO) & desc ? "-RO" : "-RW"); |
| 960 | tf_printf(LOWER_ATTRS(NS) & desc ? "-NS" : "-S"); |
Antonio Nino Diaz | efabaa9 | 2017-04-27 13:30:22 +0100 | [diff] [blame^] | 961 | tf_printf(execute_never_mask & desc ? "-XN" : "-EXEC"); |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | static const char * const level_spacers[] = { |
Antonio Nino Diaz | 755e54f | 2017-02-13 11:35:49 +0000 | [diff] [blame] | 965 | "[LV0] ", |
| 966 | " [LV1] ", |
| 967 | " [LV2] ", |
| 968 | " [LV3] " |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 969 | }; |
| 970 | |
Antonio Nino Diaz | 755e54f | 2017-02-13 11:35:49 +0000 | [diff] [blame] | 971 | static const char *invalid_descriptors_ommited = |
| 972 | "%s(%d invalid descriptors omitted)\n"; |
| 973 | |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 974 | /* |
| 975 | * Recursive function that reads the translation tables passed as an argument |
| 976 | * and prints their status. |
| 977 | */ |
| 978 | static void xlat_tables_print_internal(const uintptr_t table_base_va, |
| 979 | uint64_t *const table_base, const int table_entries, |
Antonio Nino Diaz | efabaa9 | 2017-04-27 13:30:22 +0100 | [diff] [blame^] | 980 | const int level, const uint64_t execute_never_mask) |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 981 | { |
| 982 | assert(level <= XLAT_TABLE_LEVEL_MAX); |
| 983 | |
| 984 | uint64_t desc; |
| 985 | uintptr_t table_idx_va = table_base_va; |
| 986 | int table_idx = 0; |
| 987 | |
| 988 | size_t level_size = XLAT_BLOCK_SIZE(level); |
| 989 | |
Antonio Nino Diaz | 755e54f | 2017-02-13 11:35:49 +0000 | [diff] [blame] | 990 | /* |
| 991 | * Keep track of how many invalid descriptors are counted in a row. |
| 992 | * Whenever multiple invalid descriptors are found, only the first one |
| 993 | * is printed, and a line is added to inform about how many descriptors |
| 994 | * have been omitted. |
| 995 | */ |
| 996 | int invalid_row_count = 0; |
| 997 | |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 998 | while (table_idx < table_entries) { |
| 999 | |
| 1000 | desc = table_base[table_idx]; |
| 1001 | |
| 1002 | if ((desc & DESC_MASK) == INVALID_DESC) { |
| 1003 | |
Antonio Nino Diaz | 755e54f | 2017-02-13 11:35:49 +0000 | [diff] [blame] | 1004 | if (invalid_row_count == 0) { |
| 1005 | tf_printf("%sVA:%p size:0x%zx\n", |
| 1006 | level_spacers[level], |
| 1007 | (void *)table_idx_va, level_size); |
| 1008 | } |
| 1009 | invalid_row_count++; |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 1010 | |
| 1011 | } else { |
| 1012 | |
Antonio Nino Diaz | 755e54f | 2017-02-13 11:35:49 +0000 | [diff] [blame] | 1013 | if (invalid_row_count > 1) { |
| 1014 | tf_printf(invalid_descriptors_ommited, |
| 1015 | level_spacers[level], |
| 1016 | invalid_row_count - 1); |
| 1017 | } |
| 1018 | invalid_row_count = 0; |
| 1019 | |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 1020 | /* |
| 1021 | * Check if this is a table or a block. Tables are only |
| 1022 | * allowed in levels other than 3, but DESC_PAGE has the |
| 1023 | * same value as DESC_TABLE, so we need to check. |
| 1024 | */ |
| 1025 | if (((desc & DESC_MASK) == TABLE_DESC) && |
| 1026 | (level < XLAT_TABLE_LEVEL_MAX)) { |
| 1027 | /* |
| 1028 | * Do not print any PA for a table descriptor, |
| 1029 | * as it doesn't directly map physical memory |
| 1030 | * but instead points to the next translation |
| 1031 | * table in the translation table walk. |
| 1032 | */ |
| 1033 | tf_printf("%sVA:%p size:0x%zx\n", |
| 1034 | level_spacers[level], |
| 1035 | (void *)table_idx_va, level_size); |
| 1036 | |
| 1037 | uintptr_t addr_inner = desc & TABLE_ADDR_MASK; |
| 1038 | |
| 1039 | xlat_tables_print_internal(table_idx_va, |
| 1040 | (uint64_t *)addr_inner, |
Antonio Nino Diaz | efabaa9 | 2017-04-27 13:30:22 +0100 | [diff] [blame^] | 1041 | XLAT_TABLE_ENTRIES, level+1, |
| 1042 | execute_never_mask); |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 1043 | } else { |
| 1044 | tf_printf("%sVA:%p PA:0x%llx size:0x%zx ", |
| 1045 | level_spacers[level], |
| 1046 | (void *)table_idx_va, |
| 1047 | (unsigned long long)(desc & TABLE_ADDR_MASK), |
| 1048 | level_size); |
Antonio Nino Diaz | efabaa9 | 2017-04-27 13:30:22 +0100 | [diff] [blame^] | 1049 | xlat_desc_print(desc, execute_never_mask); |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 1050 | tf_printf("\n"); |
| 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | table_idx++; |
| 1055 | table_idx_va += level_size; |
| 1056 | } |
Antonio Nino Diaz | 755e54f | 2017-02-13 11:35:49 +0000 | [diff] [blame] | 1057 | |
| 1058 | if (invalid_row_count > 1) { |
| 1059 | tf_printf(invalid_descriptors_ommited, |
| 1060 | level_spacers[level], invalid_row_count - 1); |
| 1061 | } |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | #endif /* LOG_LEVEL >= LOG_LEVEL_VERBOSE */ |
| 1065 | |
| 1066 | void xlat_tables_print(xlat_ctx_t *ctx) |
| 1067 | { |
| 1068 | #if LOG_LEVEL >= LOG_LEVEL_VERBOSE |
| 1069 | xlat_tables_print_internal(0, ctx->base_table, ctx->base_table_entries, |
Antonio Nino Diaz | efabaa9 | 2017-04-27 13:30:22 +0100 | [diff] [blame^] | 1070 | ctx->base_level, ctx->execute_never_mask); |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 1071 | #endif /* LOG_LEVEL >= LOG_LEVEL_VERBOSE */ |
| 1072 | } |
| 1073 | |
| 1074 | void init_xlation_table(xlat_ctx_t *ctx) |
| 1075 | { |
| 1076 | mmap_region_t *mm = ctx->mmap; |
| 1077 | |
| 1078 | /* All tables must be zeroed before mapping any region. */ |
| 1079 | |
| 1080 | for (int i = 0; i < ctx->base_table_entries; i++) |
| 1081 | ctx->base_table[i] = INVALID_DESC; |
| 1082 | |
| 1083 | for (int j = 0; j < ctx->tables_num; j++) { |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 1084 | #if PLAT_XLAT_TABLES_DYNAMIC |
| 1085 | ctx->tables_mapped_regions[j] = 0; |
| 1086 | #endif |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 1087 | for (int i = 0; i < XLAT_TABLE_ENTRIES; i++) |
| 1088 | ctx->tables[j][i] = INVALID_DESC; |
| 1089 | } |
| 1090 | |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 1091 | while (mm->size) { |
| 1092 | uintptr_t end_va = xlat_tables_map_region(ctx, mm, 0, ctx->base_table, |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 1093 | ctx->base_table_entries, ctx->base_level); |
| 1094 | |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 1095 | if (end_va != mm->base_va + mm->size - 1) { |
| 1096 | ERROR("Not enough memory to map region:\n" |
| 1097 | " VA:%p PA:0x%llx size:0x%zx attr:0x%x\n", |
| 1098 | (void *)mm->base_va, mm->base_pa, mm->size, mm->attr); |
| 1099 | panic(); |
| 1100 | } |
| 1101 | |
| 1102 | mm++; |
| 1103 | } |
| 1104 | |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 1105 | ctx->initialized = 1; |
| 1106 | } |