1 /* 2 * Copyright (c) 2016, ARM Limited and Contributors. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are met: 6 * 7 * Redistributions of source code must retain the above copyright notice, this 8 * list of conditions and the following disclaimer. 9 * 10 * Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 14 * Neither the name of ARM nor the names of its contributors may be used 15 * to endorse or promote products derived from this software without specific 16 * prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include <arch.h> 32 #include <arch_helpers.h> 33 #include <assert.h> 34 #include <bl_common.h> 35 #include <cassert.h> 36 #include <debug.h> 37 #include <platform_def.h> 38 #include <string.h> 39 #include <xlat_tables.h> 40 41 #if LOG_LEVEL >= LOG_LEVEL_VERBOSE 42 #define LVL0_SPACER "" 43 #define LVL1_SPACER " " 44 #define LVL2_SPACER " " 45 #define LVL3_SPACER " " 46 #define get_level_spacer(level) \ 47 (((level) == 0) ? LVL0_SPACER : \ 48 (((level) == 1) ? LVL1_SPACER : \ 49 (((level) == 2) ? LVL2_SPACER : LVL3_SPACER))) 50 #define debug_print(...) tf_printf(__VA_ARGS__) 51 #else 52 #define debug_print(...) ((void)0) 53 #endif 54 55 #define UNSET_DESC ~0ul 56 57 static uint64_t xlat_tables[MAX_XLAT_TABLES][XLAT_TABLE_ENTRIES] 58 __aligned(XLAT_TABLE_SIZE) __section("xlat_table"); 59 60 static unsigned next_xlat; 61 static unsigned long long xlat_max_pa; 62 static uintptr_t xlat_max_va; 63 64 /* 65 * Array of all memory regions stored in order of ascending base address. 66 * The list is terminated by the first entry with size == 0. 67 */ 68 static mmap_region_t mmap[MAX_MMAP_REGIONS + 1]; 69 70 71 void print_mmap(void) 72 { 73 #if LOG_LEVEL >= LOG_LEVEL_VERBOSE 74 debug_print("mmap:\n"); 75 mmap_region_t *mm = mmap; 76 while (mm->size) { 77 debug_print(" VA:%p PA:0x%llx size:0x%zx attr:0x%x\n", 78 (void *)mm->base_va, mm->base_pa, 79 mm->size, mm->attr); 80 ++mm; 81 }; 82 debug_print("\n"); 83 #endif 84 } 85 86 void mmap_add_region(unsigned long long base_pa, uintptr_t base_va, 87 size_t size, unsigned int attr) 88 { 89 mmap_region_t *mm = mmap; 90 mmap_region_t *mm_last = mm + ARRAY_SIZE(mmap) - 1; 91 unsigned long long end_pa = base_pa + size - 1; 92 uintptr_t end_va = base_va + size - 1; 93 94 assert(IS_PAGE_ALIGNED(base_pa)); 95 assert(IS_PAGE_ALIGNED(base_va)); 96 assert(IS_PAGE_ALIGNED(size)); 97 98 if (!size) 99 return; 100 101 assert(base_pa < end_pa); /* Check for overflows */ 102 assert(base_va < end_va); 103 104 #if DEBUG 105 106 /* Check for PAs and VAs overlaps with all other regions */ 107 for (mm = mmap; mm->size; ++mm) { 108 109 uintptr_t mm_end_va = mm->base_va + mm->size - 1; 110 111 /* 112 * Check if one of the regions is completely inside the other 113 * one. 114 */ 115 int fully_overlapped_va = 116 ((base_va >= mm->base_va) && (end_va <= mm_end_va)) || 117 ((mm->base_va >= base_va) && (mm_end_va <= end_va)); 118 119 /* 120 * Full VA overlaps are only allowed if both regions are 121 * identity mapped (zero offset) or have the same VA to PA 122 * offset. Also, make sure that it's not the exact same area. 123 */ 124 if (fully_overlapped_va) { 125 assert((mm->base_va - mm->base_pa) == 126 (base_va - base_pa)); 127 assert((base_va != mm->base_va) || (size != mm->size)); 128 } else { 129 /* 130 * If the regions do not have fully overlapping VAs, 131 * then they must have fully separated VAs and PAs. 132 * Partial overlaps are not allowed 133 */ 134 135 unsigned long long mm_end_pa = 136 mm->base_pa + mm->size - 1; 137 138 int separated_pa = 139 (end_pa < mm->base_pa) || (base_pa > mm_end_pa); 140 int separated_va = 141 (end_va < mm->base_va) || (base_va > mm_end_va); 142 143 assert(separated_va && separated_pa); 144 } 145 } 146 147 mm = mmap; /* Restore pointer to the start of the array */ 148 149 #endif /* DEBUG */ 150 151 /* Find correct place in mmap to insert new region */ 152 while (mm->base_va < base_va && mm->size) 153 ++mm; 154 155 /* 156 * If a section is contained inside another one with the same base 157 * address, it must be placed after the one it is contained in: 158 * 159 * 1st |-----------------------| 160 * 2nd |------------| 161 * 3rd |------| 162 * 163 * This is required for mmap_region_attr() to get the attributes of the 164 * small region correctly. 165 */ 166 while ((mm->base_va == base_va) && (mm->size > size)) 167 ++mm; 168 169 /* Make room for new region by moving other regions up by one place */ 170 memmove(mm + 1, mm, (uintptr_t)mm_last - (uintptr_t)mm); 171 172 /* Check we haven't lost the empty sentinal from the end of the array */ 173 assert(mm_last->size == 0); 174 175 mm->base_pa = base_pa; 176 mm->base_va = base_va; 177 mm->size = size; 178 mm->attr = attr; 179 180 if (end_pa > xlat_max_pa) 181 xlat_max_pa = end_pa; 182 if (end_va > xlat_max_va) 183 xlat_max_va = end_va; 184 } 185 186 void mmap_add(const mmap_region_t *mm) 187 { 188 while (mm->size) { 189 mmap_add_region(mm->base_pa, mm->base_va, mm->size, mm->attr); 190 ++mm; 191 } 192 } 193 194 static uint64_t mmap_desc(unsigned attr, unsigned long long addr_pa, 195 int level) 196 { 197 uint64_t desc; 198 int mem_type; 199 200 desc = addr_pa; 201 desc |= (level == 3) ? TABLE_DESC : BLOCK_DESC; 202 desc |= (attr & MT_NS) ? LOWER_ATTRS(NS) : 0; 203 desc |= (attr & MT_RW) ? LOWER_ATTRS(AP_RW) : LOWER_ATTRS(AP_RO); 204 desc |= LOWER_ATTRS(ACCESS_FLAG); 205 206 /* 207 * Deduce shareability domain and executability of the memory region 208 * from the memory type. 209 * 210 * Data accesses to device memory and non-cacheable normal memory are 211 * coherent for all observers in the system, and correspondingly are 212 * always treated as being Outer Shareable. Therefore, for these 2 types 213 * of memory, it is not strictly needed to set the shareability field 214 * in the translation tables. 215 */ 216 mem_type = MT_TYPE(attr); 217 if (mem_type == MT_DEVICE) { 218 desc |= LOWER_ATTRS(ATTR_DEVICE_INDEX | OSH); 219 /* 220 * Always map device memory as execute-never. 221 * This is to avoid the possibility of a speculative instruction 222 * fetch, which could be an issue if this memory region 223 * corresponds to a read-sensitive peripheral. 224 */ 225 desc |= UPPER_ATTRS(XN); 226 } else { /* Normal memory */ 227 /* 228 * Always map read-write normal memory as execute-never. 229 * (Trusted Firmware doesn't self-modify its code, therefore 230 * R/W memory is reserved for data storage, which must not be 231 * executable.) 232 * Note that setting the XN bit here is for consistency only. 233 * The enable_mmu_elx() function sets the SCTLR_EL3.WXN bit, 234 * which makes any writable memory region to be treated as 235 * execute-never, regardless of the value of the XN bit in the 236 * translation table. 237 * 238 * For read-only memory, rely on the MT_EXECUTE/MT_EXECUTE_NEVER 239 * attribute to figure out the value of the XN bit. 240 */ 241 if ((attr & MT_RW) || (attr & MT_EXECUTE_NEVER)) 242 desc |= UPPER_ATTRS(XN); 243 244 if (mem_type == MT_MEMORY) { 245 desc |= LOWER_ATTRS(ATTR_IWBWA_OWBWA_NTR_INDEX | ISH); 246 } else { 247 assert(mem_type == MT_NON_CACHEABLE); 248 desc |= LOWER_ATTRS(ATTR_NON_CACHEABLE_INDEX | OSH); 249 } 250 } 251 252 debug_print((mem_type == MT_MEMORY) ? "MEM" : 253 ((mem_type == MT_NON_CACHEABLE) ? "NC" : "DEV")); 254 debug_print(attr & MT_RW ? "-RW" : "-RO"); 255 debug_print(attr & MT_NS ? "-NS" : "-S"); 256 debug_print(attr & MT_EXECUTE_NEVER ? "-XN" : "-EXEC"); 257 return desc; 258 } 259 260 /* 261 * Returns attributes of area at `base_va` with size `size`. It returns the 262 * attributes of the innermost region that contains it. If there are partial 263 * overlaps, it returns -1, as a smaller size is needed. 264 */ 265 static int mmap_region_attr(mmap_region_t *mm, uintptr_t base_va, 266 size_t size) 267 { 268 /* Don't assume that the area is contained in the first region */ 269 int attr = -1; 270 271 /* 272 * Get attributes from last (innermost) region that contains the 273 * requested area. Don't stop as soon as one region doesn't contain it 274 * because there may be other internal regions that contain this area: 275 * 276 * |-----------------------------1-----------------------------| 277 * |----2----| |-------3-------| |----5----| 278 * |--4--| 279 * 280 * |---| <- Area we want the attributes of. 281 * 282 * In this example, the area is contained in regions 1, 3 and 4 but not 283 * in region 2. The loop shouldn't stop at region 2 as inner regions 284 * have priority over outer regions, it should stop at region 5. 285 */ 286 for (;; ++mm) { 287 288 if (!mm->size) 289 return attr; /* Reached end of list */ 290 291 if (mm->base_va >= base_va + size) 292 return attr; /* Next region is after area so end */ 293 294 if (mm->base_va + mm->size <= base_va) 295 continue; /* Next region has already been overtaken */ 296 297 if (mm->attr == attr) 298 continue; /* Region doesn't override attribs so skip */ 299 300 if (mm->base_va > base_va || 301 mm->base_va + mm->size < base_va + size) 302 return -1; /* Region doesn't fully cover our area */ 303 304 attr = mm->attr; 305 } 306 } 307 308 static mmap_region_t *init_xlation_table_inner(mmap_region_t *mm, 309 uintptr_t base_va, 310 uint64_t *table, 311 int level) 312 { 313 unsigned level_size_shift = L1_XLAT_ADDRESS_SHIFT - (level - 1) * 314 XLAT_TABLE_ENTRIES_SHIFT; 315 unsigned level_size = 1 << level_size_shift; 316 unsigned long long level_index_mask = 317 ((unsigned long long) XLAT_TABLE_ENTRIES_MASK) 318 << level_size_shift; 319 320 assert(level > 0 && level <= 3); 321 322 debug_print("New xlat table:\n"); 323 324 do { 325 uint64_t desc = UNSET_DESC; 326 327 if (!mm->size) { 328 /* Done mapping regions; finish zeroing the table */ 329 desc = INVALID_DESC; 330 } else if (mm->base_va + mm->size <= base_va) { 331 /* This area is after the region so get next region */ 332 ++mm; 333 continue; 334 } 335 336 debug_print("%s VA:%p size:0x%x ", get_level_spacer(level), 337 (void *)base_va, level_size); 338 339 if (mm->base_va >= base_va + level_size) { 340 /* Next region is after this area. Nothing to map yet */ 341 desc = INVALID_DESC; 342 } else { 343 /* 344 * Try to get attributes of this area. It will fail if 345 * there are partially overlapping regions. On success, 346 * it will return the innermost region's attributes. 347 */ 348 int attr = mmap_region_attr(mm, base_va, level_size); 349 if (attr >= 0) { 350 desc = mmap_desc(attr, 351 base_va - mm->base_va + mm->base_pa, 352 level); 353 } 354 } 355 356 if (desc == UNSET_DESC) { 357 /* Area not covered by a region so need finer table */ 358 uint64_t *new_table = xlat_tables[next_xlat++]; 359 assert(next_xlat <= MAX_XLAT_TABLES); 360 desc = TABLE_DESC | (uint64_t)new_table; 361 362 /* Recurse to fill in new table */ 363 mm = init_xlation_table_inner(mm, base_va, 364 new_table, level+1); 365 } 366 367 debug_print("\n"); 368 369 *table++ = desc; 370 base_va += level_size; 371 } while ((base_va & level_index_mask) && (base_va < ADDR_SPACE_SIZE)); 372 373 return mm; 374 } 375 376 void init_xlation_table(uintptr_t base_va, uint64_t *table, 377 int level, uintptr_t *max_va, 378 unsigned long long *max_pa) 379 { 380 381 init_xlation_table_inner(mmap, base_va, table, level); 382 *max_va = xlat_max_va; 383 *max_pa = xlat_max_pa; 384 } 385