Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 1 | /* |
Antonio Nino Diaz | 4907449 | 2018-04-26 12:59:08 +0100 | [diff] [blame] | 2 | * Copyright (c) 2016-2018, ARM Limited and Contributors. All rights reserved. |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <arch.h> |
| 8 | #include <arch_helpers.h> |
| 9 | #include <assert.h> |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 10 | #include <cassert.h> |
Antonio Nino Diaz | d1beee2 | 2016-12-13 15:28:54 +0000 | [diff] [blame] | 11 | #include <common_def.h> |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 12 | #include <debug.h> |
| 13 | #include <platform_def.h> |
| 14 | #include <string.h> |
Soby Mathew | a0fedc4 | 2016-06-16 14:52:04 +0100 | [diff] [blame] | 15 | #include <types.h> |
Sandrine Bailleux | 7659a26 | 2016-07-05 09:55:03 +0100 | [diff] [blame] | 16 | #include <utils.h> |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 17 | #include <xlat_tables.h> |
Antonio Nino Diaz | d1beee2 | 2016-12-13 15:28:54 +0000 | [diff] [blame] | 18 | #include "xlat_tables_private.h" |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 19 | |
| 20 | #if LOG_LEVEL >= LOG_LEVEL_VERBOSE |
| 21 | #define LVL0_SPACER "" |
| 22 | #define LVL1_SPACER " " |
| 23 | #define LVL2_SPACER " " |
| 24 | #define LVL3_SPACER " " |
| 25 | #define get_level_spacer(level) \ |
David Cunado | 8a354f1 | 2017-06-21 16:52:45 +0100 | [diff] [blame] | 26 | (((level) == U(0)) ? LVL0_SPACER : \ |
| 27 | (((level) == U(1)) ? LVL1_SPACER : \ |
| 28 | (((level) == U(2)) ? LVL2_SPACER : LVL3_SPACER))) |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 29 | #define debug_print(...) tf_printf(__VA_ARGS__) |
| 30 | #else |
| 31 | #define debug_print(...) ((void)0) |
| 32 | #endif |
| 33 | |
David Cunado | c150312 | 2018-02-16 21:12:58 +0000 | [diff] [blame] | 34 | #define UNSET_DESC ~0ULL |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 35 | |
| 36 | static uint64_t xlat_tables[MAX_XLAT_TABLES][XLAT_TABLE_ENTRIES] |
| 37 | __aligned(XLAT_TABLE_SIZE) __section("xlat_table"); |
| 38 | |
David Cunado | 8a354f1 | 2017-06-21 16:52:45 +0100 | [diff] [blame] | 39 | static unsigned int next_xlat; |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 40 | static unsigned long long xlat_max_pa; |
| 41 | static uintptr_t xlat_max_va; |
| 42 | |
Antonio Nino Diaz | efabaa9 | 2017-04-27 13:30:22 +0100 | [diff] [blame] | 43 | static uint64_t execute_never_mask; |
Antonio Nino Diaz | 4907449 | 2018-04-26 12:59:08 +0100 | [diff] [blame] | 44 | static uint64_t ap1_mask; |
Antonio Nino Diaz | efabaa9 | 2017-04-27 13:30:22 +0100 | [diff] [blame] | 45 | |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 46 | /* |
| 47 | * Array of all memory regions stored in order of ascending base address. |
| 48 | * The list is terminated by the first entry with size == 0. |
| 49 | */ |
| 50 | static mmap_region_t mmap[MAX_MMAP_REGIONS + 1]; |
| 51 | |
| 52 | |
| 53 | void print_mmap(void) |
| 54 | { |
| 55 | #if LOG_LEVEL >= LOG_LEVEL_VERBOSE |
| 56 | debug_print("mmap:\n"); |
| 57 | mmap_region_t *mm = mmap; |
| 58 | while (mm->size) { |
| 59 | debug_print(" VA:%p PA:0x%llx size:0x%zx attr:0x%x\n", |
| 60 | (void *)mm->base_va, mm->base_pa, |
| 61 | mm->size, mm->attr); |
| 62 | ++mm; |
| 63 | }; |
| 64 | debug_print("\n"); |
| 65 | #endif |
| 66 | } |
| 67 | |
| 68 | void mmap_add_region(unsigned long long base_pa, uintptr_t base_va, |
Sandrine Bailleux | 04980a3 | 2017-04-19 14:02:23 +0100 | [diff] [blame] | 69 | size_t size, mmap_attr_t attr) |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 70 | { |
| 71 | mmap_region_t *mm = mmap; |
| 72 | mmap_region_t *mm_last = mm + ARRAY_SIZE(mmap) - 1; |
Antonio Nino Diaz | b5e7f77 | 2016-03-30 15:45:57 +0100 | [diff] [blame] | 73 | unsigned long long end_pa = base_pa + size - 1; |
| 74 | uintptr_t end_va = base_va + size - 1; |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 75 | |
| 76 | assert(IS_PAGE_ALIGNED(base_pa)); |
| 77 | assert(IS_PAGE_ALIGNED(base_va)); |
| 78 | assert(IS_PAGE_ALIGNED(size)); |
| 79 | |
| 80 | if (!size) |
| 81 | return; |
| 82 | |
Antonio Nino Diaz | b5e7f77 | 2016-03-30 15:45:57 +0100 | [diff] [blame] | 83 | assert(base_pa < end_pa); /* Check for overflows */ |
| 84 | assert(base_va < end_va); |
| 85 | |
Antonio Nino Diaz | d1beee2 | 2016-12-13 15:28:54 +0000 | [diff] [blame] | 86 | assert((base_va + (uintptr_t)size - (uintptr_t)1) <= |
| 87 | (PLAT_VIRT_ADDR_SPACE_SIZE - 1)); |
| 88 | assert((base_pa + (unsigned long long)size - 1ULL) <= |
| 89 | (PLAT_PHY_ADDR_SPACE_SIZE - 1)); |
| 90 | |
Antonio Nino Diaz | 3759e3f | 2017-03-22 15:48:51 +0000 | [diff] [blame] | 91 | #if ENABLE_ASSERTIONS |
Antonio Nino Diaz | b5e7f77 | 2016-03-30 15:45:57 +0100 | [diff] [blame] | 92 | |
| 93 | /* Check for PAs and VAs overlaps with all other regions */ |
| 94 | for (mm = mmap; mm->size; ++mm) { |
| 95 | |
| 96 | uintptr_t mm_end_va = mm->base_va + mm->size - 1; |
| 97 | |
| 98 | /* |
| 99 | * Check if one of the regions is completely inside the other |
| 100 | * one. |
| 101 | */ |
| 102 | int fully_overlapped_va = |
| 103 | ((base_va >= mm->base_va) && (end_va <= mm_end_va)) || |
| 104 | ((mm->base_va >= base_va) && (mm_end_va <= end_va)); |
| 105 | |
| 106 | /* |
| 107 | * Full VA overlaps are only allowed if both regions are |
| 108 | * identity mapped (zero offset) or have the same VA to PA |
| 109 | * offset. Also, make sure that it's not the exact same area. |
| 110 | */ |
| 111 | if (fully_overlapped_va) { |
| 112 | assert((mm->base_va - mm->base_pa) == |
| 113 | (base_va - base_pa)); |
| 114 | assert((base_va != mm->base_va) || (size != mm->size)); |
| 115 | } else { |
| 116 | /* |
| 117 | * If the regions do not have fully overlapping VAs, |
| 118 | * then they must have fully separated VAs and PAs. |
| 119 | * Partial overlaps are not allowed |
| 120 | */ |
| 121 | |
| 122 | unsigned long long mm_end_pa = |
| 123 | mm->base_pa + mm->size - 1; |
| 124 | |
| 125 | int separated_pa = |
| 126 | (end_pa < mm->base_pa) || (base_pa > mm_end_pa); |
| 127 | int separated_va = |
| 128 | (end_va < mm->base_va) || (base_va > mm_end_va); |
| 129 | |
| 130 | assert(separated_va && separated_pa); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | mm = mmap; /* Restore pointer to the start of the array */ |
| 135 | |
Antonio Nino Diaz | 3759e3f | 2017-03-22 15:48:51 +0000 | [diff] [blame] | 136 | #endif /* ENABLE_ASSERTIONS */ |
Antonio Nino Diaz | b5e7f77 | 2016-03-30 15:45:57 +0100 | [diff] [blame] | 137 | |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 138 | /* Find correct place in mmap to insert new region */ |
| 139 | while (mm->base_va < base_va && mm->size) |
| 140 | ++mm; |
| 141 | |
Antonio Nino Diaz | b5e7f77 | 2016-03-30 15:45:57 +0100 | [diff] [blame] | 142 | /* |
| 143 | * If a section is contained inside another one with the same base |
| 144 | * address, it must be placed after the one it is contained in: |
| 145 | * |
| 146 | * 1st |-----------------------| |
| 147 | * 2nd |------------| |
| 148 | * 3rd |------| |
| 149 | * |
| 150 | * This is required for mmap_region_attr() to get the attributes of the |
| 151 | * small region correctly. |
| 152 | */ |
| 153 | while ((mm->base_va == base_va) && (mm->size > size)) |
| 154 | ++mm; |
| 155 | |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 156 | /* Make room for new region by moving other regions up by one place */ |
| 157 | memmove(mm + 1, mm, (uintptr_t)mm_last - (uintptr_t)mm); |
| 158 | |
| 159 | /* Check we haven't lost the empty sentinal from the end of the array */ |
| 160 | assert(mm_last->size == 0); |
| 161 | |
| 162 | mm->base_pa = base_pa; |
| 163 | mm->base_va = base_va; |
| 164 | mm->size = size; |
| 165 | mm->attr = attr; |
| 166 | |
Antonio Nino Diaz | b5e7f77 | 2016-03-30 15:45:57 +0100 | [diff] [blame] | 167 | if (end_pa > xlat_max_pa) |
| 168 | xlat_max_pa = end_pa; |
| 169 | if (end_va > xlat_max_va) |
| 170 | xlat_max_va = end_va; |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | void mmap_add(const mmap_region_t *mm) |
| 174 | { |
| 175 | while (mm->size) { |
| 176 | mmap_add_region(mm->base_pa, mm->base_va, mm->size, mm->attr); |
| 177 | ++mm; |
| 178 | } |
| 179 | } |
| 180 | |
Sandrine Bailleux | 04980a3 | 2017-04-19 14:02:23 +0100 | [diff] [blame] | 181 | static uint64_t mmap_desc(mmap_attr_t attr, unsigned long long addr_pa, |
David Cunado | 8a354f1 | 2017-06-21 16:52:45 +0100 | [diff] [blame] | 182 | unsigned int level) |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 183 | { |
Sandrine Bailleux | 7528b68 | 2016-06-14 16:29:04 +0100 | [diff] [blame] | 184 | uint64_t desc; |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 185 | int mem_type; |
| 186 | |
Antonio Nino Diaz | 46a33a5 | 2016-12-08 16:03:46 +0000 | [diff] [blame] | 187 | /* Make sure that the granularity is fine enough to map this address. */ |
| 188 | assert((addr_pa & XLAT_BLOCK_MASK(level)) == 0); |
| 189 | |
Sandrine Bailleux | 7528b68 | 2016-06-14 16:29:04 +0100 | [diff] [blame] | 190 | desc = addr_pa; |
Antonio Nino Diaz | d48ae61 | 2016-08-02 09:21:41 +0100 | [diff] [blame] | 191 | /* |
| 192 | * There are different translation table descriptors for level 3 and the |
| 193 | * rest. |
| 194 | */ |
| 195 | desc |= (level == XLAT_TABLE_LEVEL_MAX) ? PAGE_DESC : BLOCK_DESC; |
Sandrine Bailleux | 7528b68 | 2016-06-14 16:29:04 +0100 | [diff] [blame] | 196 | desc |= (attr & MT_NS) ? LOWER_ATTRS(NS) : 0; |
| 197 | desc |= (attr & MT_RW) ? LOWER_ATTRS(AP_RW) : LOWER_ATTRS(AP_RO); |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 198 | desc |= LOWER_ATTRS(ACCESS_FLAG); |
Antonio Nino Diaz | 4907449 | 2018-04-26 12:59:08 +0100 | [diff] [blame] | 199 | desc |= ap1_mask; |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 200 | |
Sandrine Bailleux | 7528b68 | 2016-06-14 16:29:04 +0100 | [diff] [blame] | 201 | /* |
| 202 | * Deduce shareability domain and executability of the memory region |
| 203 | * from the memory type. |
| 204 | * |
| 205 | * Data accesses to device memory and non-cacheable normal memory are |
| 206 | * coherent for all observers in the system, and correspondingly are |
| 207 | * always treated as being Outer Shareable. Therefore, for these 2 types |
| 208 | * of memory, it is not strictly needed to set the shareability field |
| 209 | * in the translation tables. |
| 210 | */ |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 211 | mem_type = MT_TYPE(attr); |
Sandrine Bailleux | 7528b68 | 2016-06-14 16:29:04 +0100 | [diff] [blame] | 212 | if (mem_type == MT_DEVICE) { |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 213 | desc |= LOWER_ATTRS(ATTR_DEVICE_INDEX | OSH); |
Sandrine Bailleux | 7528b68 | 2016-06-14 16:29:04 +0100 | [diff] [blame] | 214 | /* |
| 215 | * Always map device memory as execute-never. |
| 216 | * This is to avoid the possibility of a speculative instruction |
| 217 | * fetch, which could be an issue if this memory region |
| 218 | * corresponds to a read-sensitive peripheral. |
| 219 | */ |
Antonio Nino Diaz | efabaa9 | 2017-04-27 13:30:22 +0100 | [diff] [blame] | 220 | desc |= execute_never_mask; |
| 221 | |
Sandrine Bailleux | 7528b68 | 2016-06-14 16:29:04 +0100 | [diff] [blame] | 222 | } else { /* Normal memory */ |
| 223 | /* |
| 224 | * Always map read-write normal memory as execute-never. |
| 225 | * (Trusted Firmware doesn't self-modify its code, therefore |
| 226 | * R/W memory is reserved for data storage, which must not be |
| 227 | * executable.) |
| 228 | * Note that setting the XN bit here is for consistency only. |
Antonio Nino Diaz | efabaa9 | 2017-04-27 13:30:22 +0100 | [diff] [blame] | 229 | * The function that enables the MMU sets the SCTLR_ELx.WXN bit, |
Sandrine Bailleux | 7528b68 | 2016-06-14 16:29:04 +0100 | [diff] [blame] | 230 | * which makes any writable memory region to be treated as |
| 231 | * execute-never, regardless of the value of the XN bit in the |
| 232 | * translation table. |
Sandrine Bailleux | ac3aa68 | 2016-06-14 16:31:09 +0100 | [diff] [blame] | 233 | * |
| 234 | * For read-only memory, rely on the MT_EXECUTE/MT_EXECUTE_NEVER |
| 235 | * attribute to figure out the value of the XN bit. |
Sandrine Bailleux | 7528b68 | 2016-06-14 16:29:04 +0100 | [diff] [blame] | 236 | */ |
Antonio Nino Diaz | efabaa9 | 2017-04-27 13:30:22 +0100 | [diff] [blame] | 237 | if ((attr & MT_RW) || (attr & MT_EXECUTE_NEVER)) { |
| 238 | desc |= execute_never_mask; |
| 239 | } |
Sandrine Bailleux | 7528b68 | 2016-06-14 16:29:04 +0100 | [diff] [blame] | 240 | |
| 241 | if (mem_type == MT_MEMORY) { |
| 242 | desc |= LOWER_ATTRS(ATTR_IWBWA_OWBWA_NTR_INDEX | ISH); |
| 243 | } else { |
| 244 | assert(mem_type == MT_NON_CACHEABLE); |
| 245 | desc |= LOWER_ATTRS(ATTR_NON_CACHEABLE_INDEX | OSH); |
| 246 | } |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | debug_print((mem_type == MT_MEMORY) ? "MEM" : |
| 250 | ((mem_type == MT_NON_CACHEABLE) ? "NC" : "DEV")); |
| 251 | debug_print(attr & MT_RW ? "-RW" : "-RO"); |
| 252 | debug_print(attr & MT_NS ? "-NS" : "-S"); |
Sandrine Bailleux | ac3aa68 | 2016-06-14 16:31:09 +0100 | [diff] [blame] | 253 | debug_print(attr & MT_EXECUTE_NEVER ? "-XN" : "-EXEC"); |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 254 | return desc; |
| 255 | } |
| 256 | |
Antonio Nino Diaz | b5e7f77 | 2016-03-30 15:45:57 +0100 | [diff] [blame] | 257 | /* |
Nishanth Menon | 6f24346 | 2017-05-01 12:26:34 -0500 | [diff] [blame] | 258 | * Look for the innermost region that contains the area at `base_va` with size |
| 259 | * `size`. Populate *attr with the attributes of this region. |
| 260 | * |
| 261 | * On success, this function returns 0. |
| 262 | * If there are partial overlaps (meaning that a smaller size is needed) or if |
| 263 | * the region can't be found in the given area, it returns -1. In this case the |
| 264 | * value pointed by attr should be ignored by the caller. |
Antonio Nino Diaz | b5e7f77 | 2016-03-30 15:45:57 +0100 | [diff] [blame] | 265 | */ |
Nishanth Menon | 6f24346 | 2017-05-01 12:26:34 -0500 | [diff] [blame] | 266 | static int mmap_region_attr(mmap_region_t *mm, uintptr_t base_va, |
| 267 | size_t size, mmap_attr_t *attr) |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 268 | { |
Antonio Nino Diaz | b5e7f77 | 2016-03-30 15:45:57 +0100 | [diff] [blame] | 269 | /* Don't assume that the area is contained in the first region */ |
Nishanth Menon | 6f24346 | 2017-05-01 12:26:34 -0500 | [diff] [blame] | 270 | int ret = -1; |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 271 | |
Antonio Nino Diaz | b5e7f77 | 2016-03-30 15:45:57 +0100 | [diff] [blame] | 272 | /* |
| 273 | * Get attributes from last (innermost) region that contains the |
| 274 | * requested area. Don't stop as soon as one region doesn't contain it |
| 275 | * because there may be other internal regions that contain this area: |
| 276 | * |
| 277 | * |-----------------------------1-----------------------------| |
| 278 | * |----2----| |-------3-------| |----5----| |
| 279 | * |--4--| |
| 280 | * |
| 281 | * |---| <- Area we want the attributes of. |
| 282 | * |
| 283 | * In this example, the area is contained in regions 1, 3 and 4 but not |
| 284 | * in region 2. The loop shouldn't stop at region 2 as inner regions |
| 285 | * have priority over outer regions, it should stop at region 5. |
| 286 | */ |
| 287 | for (;; ++mm) { |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 288 | |
| 289 | if (!mm->size) |
Nishanth Menon | 6f24346 | 2017-05-01 12:26:34 -0500 | [diff] [blame] | 290 | return ret; /* Reached end of list */ |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 291 | |
Soby Mathew | fea233c | 2016-07-13 15:45:15 +0100 | [diff] [blame] | 292 | if (mm->base_va > base_va + size - 1) |
Nishanth Menon | 6f24346 | 2017-05-01 12:26:34 -0500 | [diff] [blame] | 293 | return ret; /* Next region is after area so end */ |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 294 | |
Soby Mathew | fea233c | 2016-07-13 15:45:15 +0100 | [diff] [blame] | 295 | if (mm->base_va + mm->size - 1 < base_va) |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 296 | continue; /* Next region has already been overtaken */ |
| 297 | |
Nishanth Menon | 6f24346 | 2017-05-01 12:26:34 -0500 | [diff] [blame] | 298 | if (!ret && mm->attr == *attr) |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 299 | continue; /* Region doesn't override attribs so skip */ |
| 300 | |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 301 | if (mm->base_va > base_va || |
Soby Mathew | fea233c | 2016-07-13 15:45:15 +0100 | [diff] [blame] | 302 | mm->base_va + mm->size - 1 < base_va + size - 1) |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 303 | return -1; /* Region doesn't fully cover our area */ |
Antonio Nino Diaz | b5e7f77 | 2016-03-30 15:45:57 +0100 | [diff] [blame] | 304 | |
Nishanth Menon | 6f24346 | 2017-05-01 12:26:34 -0500 | [diff] [blame] | 305 | *attr = mm->attr; |
| 306 | ret = 0; |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 307 | } |
Nishanth Menon | 6f24346 | 2017-05-01 12:26:34 -0500 | [diff] [blame] | 308 | return ret; |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | static mmap_region_t *init_xlation_table_inner(mmap_region_t *mm, |
| 312 | uintptr_t base_va, |
| 313 | uint64_t *table, |
David Cunado | 8a354f1 | 2017-06-21 16:52:45 +0100 | [diff] [blame] | 314 | unsigned int level) |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 315 | { |
Antonio Nino Diaz | d48ae61 | 2016-08-02 09:21:41 +0100 | [diff] [blame] | 316 | assert(level >= XLAT_TABLE_LEVEL_MIN && level <= XLAT_TABLE_LEVEL_MAX); |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 317 | |
Antonio Nino Diaz | d48ae61 | 2016-08-02 09:21:41 +0100 | [diff] [blame] | 318 | unsigned int level_size_shift = |
| 319 | L0_XLAT_ADDRESS_SHIFT - level * XLAT_TABLE_ENTRIES_SHIFT; |
| 320 | u_register_t level_size = (u_register_t)1 << level_size_shift; |
| 321 | u_register_t level_index_mask = |
| 322 | ((u_register_t)XLAT_TABLE_ENTRIES_MASK) << level_size_shift; |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 323 | |
| 324 | debug_print("New xlat table:\n"); |
| 325 | |
| 326 | do { |
| 327 | uint64_t desc = UNSET_DESC; |
| 328 | |
| 329 | if (!mm->size) { |
| 330 | /* Done mapping regions; finish zeroing the table */ |
| 331 | desc = INVALID_DESC; |
Soby Mathew | fea233c | 2016-07-13 15:45:15 +0100 | [diff] [blame] | 332 | } else if (mm->base_va + mm->size - 1 < base_va) { |
Antonio Nino Diaz | b5e7f77 | 2016-03-30 15:45:57 +0100 | [diff] [blame] | 333 | /* This area is after the region so get next region */ |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 334 | ++mm; |
| 335 | continue; |
| 336 | } |
| 337 | |
Antonio Nino Diaz | d48ae61 | 2016-08-02 09:21:41 +0100 | [diff] [blame] | 338 | debug_print("%s VA:%p size:0x%llx ", get_level_spacer(level), |
| 339 | (void *)base_va, (unsigned long long)level_size); |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 340 | |
Soby Mathew | fea233c | 2016-07-13 15:45:15 +0100 | [diff] [blame] | 341 | if (mm->base_va > base_va + level_size - 1) { |
Antonio Nino Diaz | b5e7f77 | 2016-03-30 15:45:57 +0100 | [diff] [blame] | 342 | /* Next region is after this area. Nothing to map yet */ |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 343 | desc = INVALID_DESC; |
Antonio Nino Diaz | 010888d | 2016-12-13 15:02:31 +0000 | [diff] [blame] | 344 | /* Make sure that the current level allows block descriptors */ |
| 345 | } else if (level >= XLAT_BLOCK_LEVEL_MIN) { |
Antonio Nino Diaz | b5e7f77 | 2016-03-30 15:45:57 +0100 | [diff] [blame] | 346 | /* |
| 347 | * Try to get attributes of this area. It will fail if |
| 348 | * there are partially overlapping regions. On success, |
| 349 | * it will return the innermost region's attributes. |
| 350 | */ |
Nishanth Menon | 6f24346 | 2017-05-01 12:26:34 -0500 | [diff] [blame] | 351 | mmap_attr_t attr; |
| 352 | int r = mmap_region_attr(mm, base_va, level_size, &attr); |
| 353 | |
| 354 | if (!r) { |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 355 | desc = mmap_desc(attr, |
| 356 | base_va - mm->base_va + mm->base_pa, |
| 357 | level); |
Antonio Nino Diaz | b5e7f77 | 2016-03-30 15:45:57 +0100 | [diff] [blame] | 358 | } |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 359 | } |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 360 | |
| 361 | if (desc == UNSET_DESC) { |
| 362 | /* Area not covered by a region so need finer table */ |
| 363 | uint64_t *new_table = xlat_tables[next_xlat++]; |
| 364 | assert(next_xlat <= MAX_XLAT_TABLES); |
Soby Mathew | a0fedc4 | 2016-06-16 14:52:04 +0100 | [diff] [blame] | 365 | desc = TABLE_DESC | (uintptr_t)new_table; |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 366 | |
| 367 | /* Recurse to fill in new table */ |
| 368 | mm = init_xlation_table_inner(mm, base_va, |
| 369 | new_table, level+1); |
| 370 | } |
| 371 | |
| 372 | debug_print("\n"); |
| 373 | |
| 374 | *table++ = desc; |
| 375 | base_va += level_size; |
Antonio Nino Diaz | d1beee2 | 2016-12-13 15:28:54 +0000 | [diff] [blame] | 376 | } while ((base_va & level_index_mask) && |
| 377 | (base_va - 1 < PLAT_VIRT_ADDR_SPACE_SIZE - 1)); |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 378 | |
| 379 | return mm; |
| 380 | } |
| 381 | |
| 382 | void init_xlation_table(uintptr_t base_va, uint64_t *table, |
David Cunado | 8a354f1 | 2017-06-21 16:52:45 +0100 | [diff] [blame] | 383 | unsigned int level, uintptr_t *max_va, |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 384 | unsigned long long *max_pa) |
| 385 | { |
Antonio Nino Diaz | 4907449 | 2018-04-26 12:59:08 +0100 | [diff] [blame] | 386 | int el = xlat_arch_current_el(); |
| 387 | |
| 388 | execute_never_mask = xlat_arch_get_xn_desc(el); |
| 389 | |
| 390 | if (el == 3) { |
| 391 | ap1_mask = LOWER_ATTRS(AP_ONE_VA_RANGE_RES1); |
| 392 | } else { |
| 393 | assert(el == 1); |
| 394 | ap1_mask = 0; |
| 395 | } |
| 396 | |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 397 | init_xlation_table_inner(mmap, base_va, table, level); |
| 398 | *max_va = xlat_max_va; |
| 399 | *max_pa = xlat_max_pa; |
| 400 | } |