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