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