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