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 | |
Antonio Nino Diaz | c003328 | 2018-11-20 16:03:11 +0000 | [diff] [blame] | 814 | /* |
| 815 | * Determine the table level closest to the initial lookup level that |
| 816 | * can describe this translation. Then, align base VA to the next block |
| 817 | * at the determined level. |
| 818 | */ |
| 819 | static void mmap_alloc_va_align_ctx(xlat_ctx_t *ctx, mmap_region_t *mm) |
| 820 | { |
| 821 | /* |
| 822 | * By or'ing the size and base PA the alignment will be the one |
| 823 | * corresponding to the smallest boundary of the two of them. |
| 824 | * |
| 825 | * There are three different cases. For example (for 4 KiB page size): |
| 826 | * |
| 827 | * +--------------+------------------++--------------+ |
| 828 | * | PA alignment | Size multiple of || VA alignment | |
| 829 | * +--------------+------------------++--------------+ |
| 830 | * | 2 MiB | 2 MiB || 2 MiB | (1) |
| 831 | * | 2 MiB | 4 KiB || 4 KiB | (2) |
| 832 | * | 4 KiB | 2 MiB || 4 KiB | (3) |
| 833 | * +--------------+------------------++--------------+ |
| 834 | * |
| 835 | * - In (1), it is possible to take advantage of the alignment of the PA |
| 836 | * and the size of the region to use a level 2 translation table |
| 837 | * instead of a level 3 one. |
| 838 | * |
| 839 | * - In (2), the size is smaller than a block entry of level 2, so it is |
| 840 | * needed to use a level 3 table to describe the region or the library |
| 841 | * will map more memory than the desired one. |
| 842 | * |
| 843 | * - In (3), even though the region has the size of one level 2 block |
| 844 | * entry, it isn't possible to describe the translation with a level 2 |
| 845 | * block entry because of the alignment of the base PA. |
| 846 | * |
| 847 | * Only bits 47:21 of a level 2 block descriptor are used by the MMU, |
| 848 | * bits 20:0 of the resulting address are 0 in this case. Because of |
| 849 | * this, the PA generated as result of this translation is aligned to |
| 850 | * 2 MiB. The PA that was requested to be mapped is aligned to 4 KiB, |
| 851 | * though, which means that the resulting translation is incorrect. |
| 852 | * The only way to prevent this is by using a finer granularity. |
| 853 | */ |
| 854 | unsigned long long align_check; |
| 855 | |
| 856 | align_check = mm->base_pa | (unsigned long long)mm->size; |
| 857 | |
| 858 | /* |
| 859 | * Assume it is always aligned to level 3. There's no need to check that |
| 860 | * level because its block size is PAGE_SIZE. The checks to verify that |
| 861 | * the addresses and size are aligned to PAGE_SIZE are inside |
| 862 | * mmap_add_region. |
| 863 | */ |
| 864 | for (unsigned int level = ctx->base_level; level <= 2U; ++level) { |
| 865 | |
| 866 | if ((align_check & XLAT_BLOCK_MASK(level)) != 0U) |
| 867 | continue; |
| 868 | |
| 869 | mm->base_va = round_up(mm->base_va, XLAT_BLOCK_SIZE(level)); |
| 870 | return; |
| 871 | } |
| 872 | } |
| 873 | |
| 874 | void mmap_add_region_alloc_va_ctx(xlat_ctx_t *ctx, mmap_region_t *mm) |
| 875 | { |
| 876 | mm->base_va = ctx->max_va + 1UL; |
| 877 | |
| 878 | assert(mm->size > 0U); |
| 879 | |
| 880 | mmap_alloc_va_align_ctx(ctx, mm); |
| 881 | |
| 882 | /* Detect overflows. More checks are done in mmap_add_region_check(). */ |
| 883 | assert(mm->base_va > ctx->max_va); |
| 884 | |
| 885 | mmap_add_region_ctx(ctx, mm); |
| 886 | } |
| 887 | |
Sandrine Bailleux | 6634293 | 2017-07-18 13:26:36 +0100 | [diff] [blame] | 888 | void mmap_add_ctx(xlat_ctx_t *ctx, const mmap_region_t *mm) |
| 889 | { |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 890 | const mmap_region_t *mm_cursor = mm; |
| 891 | |
Antonio Nino Diaz | 2cb864c | 2018-10-08 16:11:11 +0100 | [diff] [blame] | 892 | while (mm_cursor->granularity != 0U) { |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 893 | mmap_add_region_ctx(ctx, mm_cursor); |
| 894 | mm_cursor++; |
Sandrine Bailleux | 6634293 | 2017-07-18 13:26:36 +0100 | [diff] [blame] | 895 | } |
| 896 | } |
| 897 | |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 898 | #if PLAT_XLAT_TABLES_DYNAMIC |
| 899 | |
| 900 | int mmap_add_dynamic_region_ctx(xlat_ctx_t *ctx, mmap_region_t *mm) |
| 901 | { |
| 902 | mmap_region_t *mm_cursor = ctx->mmap; |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 903 | const mmap_region_t *mm_last = mm_cursor + ctx->mmap_num; |
| 904 | unsigned long long end_pa = mm->base_pa + mm->size - 1U; |
| 905 | uintptr_t end_va = mm->base_va + mm->size - 1U; |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 906 | int ret; |
| 907 | |
| 908 | /* Nothing to do */ |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 909 | if (mm->size == 0U) |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 910 | return 0; |
| 911 | |
Douglas Raillard | 6a5f8f1 | 2017-09-21 08:42:21 +0100 | [diff] [blame] | 912 | /* Now this region is a dynamic one */ |
| 913 | mm->attr |= MT_DYNAMIC; |
| 914 | |
| 915 | ret = mmap_add_region_check(ctx, mm); |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 916 | if (ret != 0) |
| 917 | return ret; |
| 918 | |
| 919 | /* |
| 920 | * Find the adequate entry in the mmap array in the same way done for |
| 921 | * static regions in mmap_add_region_ctx(). |
| 922 | */ |
| 923 | |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 924 | while (((mm_cursor->base_va + mm_cursor->size - 1U) < end_va) |
| 925 | && (mm_cursor->size != 0U)) { |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 926 | ++mm_cursor; |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 927 | } |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 928 | |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 929 | while (((mm_cursor->base_va + mm_cursor->size - 1U) == end_va) && |
| 930 | (mm_cursor->size != 0U) && (mm_cursor->size < mm->size)) { |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 931 | ++mm_cursor; |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 932 | } |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 933 | |
| 934 | /* 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] | 935 | (void)memmove(mm_cursor + 1U, mm_cursor, |
Douglas Raillard | 6a5f8f1 | 2017-09-21 08:42:21 +0100 | [diff] [blame] | 936 | (uintptr_t)mm_last - (uintptr_t)mm_cursor); |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 937 | |
| 938 | /* |
| 939 | * Check we haven't lost the empty sentinal from the end of the array. |
| 940 | * This shouldn't happen as we have checked in mmap_add_region_check |
| 941 | * that there is free space. |
| 942 | */ |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 943 | assert(mm_last->size == 0U); |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 944 | |
Douglas Raillard | f68d2ed | 2017-09-12 10:31:49 +0100 | [diff] [blame] | 945 | *mm_cursor = *mm; |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 946 | |
| 947 | /* |
| 948 | * Update the translation tables if the xlat tables are initialized. If |
| 949 | * not, this region will be mapped when they are initialized. |
| 950 | */ |
Antonio Nino Diaz | 5c97bd1 | 2018-08-02 09:57:29 +0100 | [diff] [blame] | 951 | if (ctx->initialized) { |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 952 | end_va = xlat_tables_map_region(ctx, mm_cursor, |
| 953 | 0U, ctx->base_table, ctx->base_table_entries, |
Douglas Raillard | 6a5f8f1 | 2017-09-21 08:42:21 +0100 | [diff] [blame] | 954 | ctx->base_level); |
Antonio Nino Diaz | 37a5efa | 2018-08-07 12:47:12 +0100 | [diff] [blame] | 955 | #if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY) |
| 956 | xlat_clean_dcache_range((uintptr_t)ctx->base_table, |
| 957 | ctx->base_table_entries * sizeof(uint64_t)); |
| 958 | #endif |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 959 | /* Failed to map, remove mmap entry, unmap and return error. */ |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 960 | if (end_va != (mm_cursor->base_va + mm_cursor->size - 1U)) { |
| 961 | (void)memmove(mm_cursor, mm_cursor + 1U, |
Douglas Raillard | 6a5f8f1 | 2017-09-21 08:42:21 +0100 | [diff] [blame] | 962 | (uintptr_t)mm_last - (uintptr_t)mm_cursor); |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 963 | |
| 964 | /* |
| 965 | * Check if the mapping function actually managed to map |
| 966 | * anything. If not, just return now. |
| 967 | */ |
Antonio Nino Diaz | 3f51892 | 2018-01-05 11:30:36 +0000 | [diff] [blame] | 968 | if (mm->base_va >= end_va) |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 969 | return -ENOMEM; |
| 970 | |
| 971 | /* |
Douglas Raillard | 6a5f8f1 | 2017-09-21 08:42:21 +0100 | [diff] [blame] | 972 | * Something went wrong after mapping some table |
| 973 | * entries, undo every change done up to this point. |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 974 | */ |
| 975 | mmap_region_t unmap_mm = { |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 976 | .base_pa = 0U, |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 977 | .base_va = mm->base_va, |
| 978 | .size = end_va - mm->base_va, |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 979 | .attr = 0U |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 980 | }; |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 981 | xlat_tables_unmap_region(ctx, &unmap_mm, 0U, |
| 982 | ctx->base_table, ctx->base_table_entries, |
| 983 | ctx->base_level); |
Antonio Nino Diaz | 37a5efa | 2018-08-07 12:47:12 +0100 | [diff] [blame] | 984 | #if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY) |
| 985 | xlat_clean_dcache_range((uintptr_t)ctx->base_table, |
| 986 | ctx->base_table_entries * sizeof(uint64_t)); |
| 987 | #endif |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 988 | return -ENOMEM; |
| 989 | } |
| 990 | |
| 991 | /* |
| 992 | * Make sure that all entries are written to the memory. There |
| 993 | * is no need to invalidate entries when mapping dynamic regions |
| 994 | * because new table/block/page descriptors only replace old |
| 995 | * invalid descriptors, that aren't TLB cached. |
| 996 | */ |
| 997 | dsbishst(); |
| 998 | } |
| 999 | |
| 1000 | if (end_pa > ctx->max_pa) |
| 1001 | ctx->max_pa = end_pa; |
| 1002 | if (end_va > ctx->max_va) |
| 1003 | ctx->max_va = end_va; |
| 1004 | |
| 1005 | return 0; |
| 1006 | } |
| 1007 | |
Antonio Nino Diaz | c003328 | 2018-11-20 16:03:11 +0000 | [diff] [blame] | 1008 | int mmap_add_dynamic_region_alloc_va_ctx(xlat_ctx_t *ctx, mmap_region_t *mm) |
| 1009 | { |
| 1010 | mm->base_va = ctx->max_va + 1UL; |
| 1011 | |
| 1012 | if (mm->size == 0U) |
| 1013 | return 0; |
| 1014 | |
| 1015 | mmap_alloc_va_align_ctx(ctx, mm); |
| 1016 | |
| 1017 | /* Detect overflows. More checks are done in mmap_add_region_check(). */ |
| 1018 | if (mm->base_va < ctx->max_va) { |
| 1019 | return -ENOMEM; |
| 1020 | } |
| 1021 | |
| 1022 | return mmap_add_dynamic_region_ctx(ctx, mm); |
| 1023 | } |
| 1024 | |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 1025 | /* |
| 1026 | * Removes the region with given base Virtual Address and size from the given |
| 1027 | * context. |
| 1028 | * |
| 1029 | * Returns: |
| 1030 | * 0: Success. |
| 1031 | * EINVAL: Invalid values were used as arguments (region not found). |
| 1032 | * EPERM: Tried to remove a static region. |
| 1033 | */ |
| 1034 | int mmap_remove_dynamic_region_ctx(xlat_ctx_t *ctx, uintptr_t base_va, |
| 1035 | size_t size) |
| 1036 | { |
| 1037 | mmap_region_t *mm = ctx->mmap; |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 1038 | const mmap_region_t *mm_last = mm + ctx->mmap_num; |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 1039 | int update_max_va_needed = 0; |
| 1040 | int update_max_pa_needed = 0; |
| 1041 | |
| 1042 | /* Check sanity of mmap array. */ |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 1043 | assert(mm[ctx->mmap_num].size == 0U); |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 1044 | |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 1045 | while (mm->size != 0U) { |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 1046 | if ((mm->base_va == base_va) && (mm->size == size)) |
| 1047 | break; |
| 1048 | ++mm; |
| 1049 | } |
| 1050 | |
| 1051 | /* Check that the region was found */ |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 1052 | if (mm->size == 0U) |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 1053 | return -EINVAL; |
| 1054 | |
| 1055 | /* If the region is static it can't be removed */ |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 1056 | if ((mm->attr & MT_DYNAMIC) == 0U) |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 1057 | return -EPERM; |
| 1058 | |
| 1059 | /* Check if this region is using the top VAs or PAs. */ |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 1060 | if ((mm->base_va + mm->size - 1U) == ctx->max_va) |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 1061 | update_max_va_needed = 1; |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 1062 | if ((mm->base_pa + mm->size - 1U) == ctx->max_pa) |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 1063 | update_max_pa_needed = 1; |
| 1064 | |
| 1065 | /* Update the translation tables if needed */ |
Antonio Nino Diaz | 5c97bd1 | 2018-08-02 09:57:29 +0100 | [diff] [blame] | 1066 | if (ctx->initialized) { |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 1067 | xlat_tables_unmap_region(ctx, mm, 0U, ctx->base_table, |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 1068 | ctx->base_table_entries, |
| 1069 | ctx->base_level); |
Antonio Nino Diaz | 37a5efa | 2018-08-07 12:47:12 +0100 | [diff] [blame] | 1070 | #if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY) |
| 1071 | xlat_clean_dcache_range((uintptr_t)ctx->base_table, |
| 1072 | ctx->base_table_entries * sizeof(uint64_t)); |
| 1073 | #endif |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 1074 | xlat_arch_tlbi_va_sync(); |
| 1075 | } |
| 1076 | |
| 1077 | /* Remove this region by moving the rest down by one place. */ |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 1078 | (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] | 1079 | |
| 1080 | /* Check if we need to update the max VAs and PAs */ |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 1081 | if (update_max_va_needed == 1) { |
| 1082 | ctx->max_va = 0U; |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 1083 | mm = ctx->mmap; |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 1084 | while (mm->size != 0U) { |
| 1085 | if ((mm->base_va + mm->size - 1U) > ctx->max_va) |
| 1086 | ctx->max_va = mm->base_va + mm->size - 1U; |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 1087 | ++mm; |
| 1088 | } |
| 1089 | } |
| 1090 | |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 1091 | if (update_max_pa_needed == 1) { |
| 1092 | ctx->max_pa = 0U; |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 1093 | mm = ctx->mmap; |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 1094 | while (mm->size != 0U) { |
| 1095 | if ((mm->base_pa + mm->size - 1U) > ctx->max_pa) |
| 1096 | ctx->max_pa = mm->base_pa + mm->size - 1U; |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 1097 | ++mm; |
| 1098 | } |
| 1099 | } |
| 1100 | |
| 1101 | return 0; |
| 1102 | } |
| 1103 | |
Antonio Nino Diaz | 675d155 | 2018-10-30 11:36:47 +0000 | [diff] [blame] | 1104 | void xlat_setup_dynamic_ctx(xlat_ctx_t *ctx, unsigned long long pa_max, |
| 1105 | uintptr_t va_max, struct mmap_region *mmap, |
| 1106 | unsigned int mmap_num, uint64_t **tables, |
| 1107 | unsigned int tables_num, uint64_t *base_table, |
| 1108 | int xlat_regime, int *mapped_regions) |
| 1109 | { |
| 1110 | ctx->xlat_regime = xlat_regime; |
| 1111 | |
| 1112 | ctx->pa_max_address = pa_max; |
| 1113 | ctx->va_max_address = va_max; |
| 1114 | |
| 1115 | ctx->mmap = mmap; |
| 1116 | ctx->mmap_num = mmap_num; |
| 1117 | memset(ctx->mmap, 0, sizeof(struct mmap_region) * mmap_num); |
| 1118 | |
| 1119 | ctx->tables = (void *) tables; |
| 1120 | ctx->tables_num = tables_num; |
| 1121 | |
| 1122 | uintptr_t va_space_size = va_max + 1; |
| 1123 | ctx->base_level = GET_XLAT_TABLE_LEVEL_BASE(va_space_size); |
| 1124 | ctx->base_table = base_table; |
| 1125 | ctx->base_table_entries = GET_NUM_BASE_LEVEL_ENTRIES(va_space_size); |
| 1126 | |
| 1127 | ctx->tables_mapped_regions = mapped_regions; |
| 1128 | |
| 1129 | ctx->max_pa = 0; |
| 1130 | ctx->max_va = 0; |
| 1131 | ctx->initialized = 0; |
| 1132 | } |
| 1133 | |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 1134 | #endif /* PLAT_XLAT_TABLES_DYNAMIC */ |
| 1135 | |
Daniel Boulby | 5a03a25 | 2018-08-30 16:48:56 +0100 | [diff] [blame] | 1136 | void __init init_xlat_tables_ctx(xlat_ctx_t *ctx) |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 1137 | { |
Antonio Nino Diaz | dcf9d92 | 2017-10-04 16:52:15 +0100 | [diff] [blame] | 1138 | assert(ctx != NULL); |
Antonio Nino Diaz | 5c97bd1 | 2018-08-02 09:57:29 +0100 | [diff] [blame] | 1139 | assert(!ctx->initialized); |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 1140 | assert((ctx->xlat_regime == EL3_REGIME) || |
Antonio Nino Diaz | 128de8d | 2018-08-07 19:59:49 +0100 | [diff] [blame] | 1141 | (ctx->xlat_regime == EL2_REGIME) || |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 1142 | (ctx->xlat_regime == EL1_EL0_REGIME)); |
Antonio Nino Diaz | 5c97bd1 | 2018-08-02 09:57:29 +0100 | [diff] [blame] | 1143 | assert(!is_mmu_enabled_ctx(ctx)); |
Sandrine Bailleux | 6634293 | 2017-07-18 13:26:36 +0100 | [diff] [blame] | 1144 | |
Antonio Nino Diaz | dcf9d92 | 2017-10-04 16:52:15 +0100 | [diff] [blame] | 1145 | mmap_region_t *mm = ctx->mmap; |
Sandrine Bailleux | 6634293 | 2017-07-18 13:26:36 +0100 | [diff] [blame] | 1146 | |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 1147 | xlat_mmap_print(mm); |
Sandrine Bailleux | 6634293 | 2017-07-18 13:26:36 +0100 | [diff] [blame] | 1148 | |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 1149 | /* All tables must be zeroed before mapping any region. */ |
| 1150 | |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 1151 | for (unsigned int i = 0U; i < ctx->base_table_entries; i++) |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 1152 | ctx->base_table[i] = INVALID_DESC; |
| 1153 | |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 1154 | for (int j = 0; j < ctx->tables_num; j++) { |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 1155 | #if PLAT_XLAT_TABLES_DYNAMIC |
| 1156 | ctx->tables_mapped_regions[j] = 0; |
| 1157 | #endif |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 1158 | for (unsigned int i = 0U; i < XLAT_TABLE_ENTRIES; i++) |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 1159 | ctx->tables[j][i] = INVALID_DESC; |
| 1160 | } |
| 1161 | |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 1162 | while (mm->size != 0U) { |
| 1163 | uintptr_t end_va = xlat_tables_map_region(ctx, mm, 0U, |
| 1164 | ctx->base_table, ctx->base_table_entries, |
| 1165 | ctx->base_level); |
Antonio Nino Diaz | 37a5efa | 2018-08-07 12:47:12 +0100 | [diff] [blame] | 1166 | #if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY) |
| 1167 | xlat_clean_dcache_range((uintptr_t)ctx->base_table, |
| 1168 | ctx->base_table_entries * sizeof(uint64_t)); |
| 1169 | #endif |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 1170 | if (end_va != (mm->base_va + mm->size - 1U)) { |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 1171 | ERROR("Not enough memory to map region:\n" |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 1172 | " VA:0x%lx PA:0x%llx size:0x%zx attr:0x%x\n", |
| 1173 | mm->base_va, mm->base_pa, mm->size, mm->attr); |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 1174 | panic(); |
| 1175 | } |
| 1176 | |
| 1177 | mm++; |
| 1178 | } |
| 1179 | |
Sandrine Bailleux | 46c53a2 | 2017-07-11 15:11:10 +0100 | [diff] [blame] | 1180 | assert(ctx->pa_max_address <= xlat_arch_get_max_supported_pa()); |
Sandrine Bailleux | 6634293 | 2017-07-18 13:26:36 +0100 | [diff] [blame] | 1181 | assert(ctx->max_va <= ctx->va_max_address); |
| 1182 | assert(ctx->max_pa <= ctx->pa_max_address); |
| 1183 | |
Antonio Nino Diaz | 5c97bd1 | 2018-08-02 09:57:29 +0100 | [diff] [blame] | 1184 | ctx->initialized = true; |
Sandrine Bailleux | c5b6377 | 2017-05-31 13:31:48 +0100 | [diff] [blame] | 1185 | |
| 1186 | xlat_tables_print(ctx); |
Sandrine Bailleux | 6634293 | 2017-07-18 13:26:36 +0100 | [diff] [blame] | 1187 | } |