1fd2299e6SAntonio Nino Diaz /* 2fd2299e6SAntonio Nino Diaz * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved. 3fd2299e6SAntonio Nino Diaz * 4fd2299e6SAntonio Nino Diaz * SPDX-License-Identifier: BSD-3-Clause 5fd2299e6SAntonio Nino Diaz */ 6fd2299e6SAntonio Nino Diaz 7fd2299e6SAntonio Nino Diaz #include <assert.h> 8fd2299e6SAntonio Nino Diaz #include <errno.h> 95b395e37SAntonio Nino Diaz #include <stdbool.h> 1093c78ed2SAntonio Nino Diaz #include <stdint.h> 1139b6cc66SAntonio Nino Diaz #include <stdio.h> 1209d40e0eSAntonio Nino Diaz 1309d40e0eSAntonio Nino Diaz #include <platform_def.h> 1409d40e0eSAntonio Nino Diaz 1509d40e0eSAntonio Nino Diaz #include <arch_helpers.h> 1609d40e0eSAntonio Nino Diaz #include <common/debug.h> 1709d40e0eSAntonio Nino Diaz #include <lib/utils_def.h> 1809d40e0eSAntonio Nino Diaz #include <lib/xlat_tables/xlat_tables_defs.h> 1909d40e0eSAntonio Nino Diaz #include <lib/xlat_tables/xlat_tables_v2.h> 20fd2299e6SAntonio Nino Diaz 21fd2299e6SAntonio Nino Diaz #include "xlat_tables_private.h" 22fd2299e6SAntonio Nino Diaz 23fd2299e6SAntonio Nino Diaz #if LOG_LEVEL < LOG_LEVEL_VERBOSE 24fd2299e6SAntonio Nino Diaz 25e7b9886cSAntonio Nino Diaz void xlat_mmap_print(__unused const mmap_region_t *mmap) 26fd2299e6SAntonio Nino Diaz { 27fd2299e6SAntonio Nino Diaz /* Empty */ 28fd2299e6SAntonio Nino Diaz } 29fd2299e6SAntonio Nino Diaz 30fd2299e6SAntonio Nino Diaz void xlat_tables_print(__unused xlat_ctx_t *ctx) 31fd2299e6SAntonio Nino Diaz { 32fd2299e6SAntonio Nino Diaz /* Empty */ 33fd2299e6SAntonio Nino Diaz } 34fd2299e6SAntonio Nino Diaz 35fd2299e6SAntonio Nino Diaz #else /* if LOG_LEVEL >= LOG_LEVEL_VERBOSE */ 36fd2299e6SAntonio Nino Diaz 37e7b9886cSAntonio Nino Diaz void xlat_mmap_print(const mmap_region_t *mmap) 38fd2299e6SAntonio Nino Diaz { 3939b6cc66SAntonio Nino Diaz printf("mmap:\n"); 40fd2299e6SAntonio Nino Diaz const mmap_region_t *mm = mmap; 41fd2299e6SAntonio Nino Diaz 42fd2299e6SAntonio Nino Diaz while (mm->size != 0U) { 4339b6cc66SAntonio Nino Diaz printf(" VA:0x%lx PA:0x%llx size:0x%zx attr:0x%x granularity:0x%zx\n", 4439b6cc66SAntonio Nino Diaz mm->base_va, mm->base_pa, mm->size, mm->attr, 4539b6cc66SAntonio Nino Diaz mm->granularity); 46fd2299e6SAntonio Nino Diaz ++mm; 47fd2299e6SAntonio Nino Diaz }; 4839b6cc66SAntonio Nino Diaz printf("\n"); 49fd2299e6SAntonio Nino Diaz } 50fd2299e6SAntonio Nino Diaz 51fd2299e6SAntonio Nino Diaz /* Print the attributes of the specified block descriptor. */ 52fd2299e6SAntonio Nino Diaz static void xlat_desc_print(const xlat_ctx_t *ctx, uint64_t desc) 53fd2299e6SAntonio Nino Diaz { 54e7b9886cSAntonio Nino Diaz uint64_t mem_type_index = ATTR_INDEX_GET(desc); 55fd2299e6SAntonio Nino Diaz int xlat_regime = ctx->xlat_regime; 56fd2299e6SAntonio Nino Diaz 57fd2299e6SAntonio Nino Diaz if (mem_type_index == ATTR_IWBWA_OWBWA_NTR_INDEX) { 5839b6cc66SAntonio Nino Diaz printf("MEM"); 59fd2299e6SAntonio Nino Diaz } else if (mem_type_index == ATTR_NON_CACHEABLE_INDEX) { 6039b6cc66SAntonio Nino Diaz printf("NC"); 61fd2299e6SAntonio Nino Diaz } else { 62fd2299e6SAntonio Nino Diaz assert(mem_type_index == ATTR_DEVICE_INDEX); 6339b6cc66SAntonio Nino Diaz printf("DEV"); 64fd2299e6SAntonio Nino Diaz } 65fd2299e6SAntonio Nino Diaz 661a92a0e0SAntonio Nino Diaz if ((xlat_regime == EL3_REGIME) || (xlat_regime == EL2_REGIME)) { 671a92a0e0SAntonio Nino Diaz /* For EL3 and EL2 only check the AP[2] and XN bits. */ 6839b6cc66SAntonio Nino Diaz printf(((desc & LOWER_ATTRS(AP_RO)) != 0ULL) ? "-RO" : "-RW"); 6939b6cc66SAntonio Nino Diaz printf(((desc & UPPER_ATTRS(XN)) != 0ULL) ? "-XN" : "-EXEC"); 70fd2299e6SAntonio Nino Diaz } else { 71f9d58d17SAntonio Nino Diaz assert(xlat_regime == EL1_EL0_REGIME); 72fd2299e6SAntonio Nino Diaz /* 73f9d58d17SAntonio Nino Diaz * For EL0 and EL1: 74f9d58d17SAntonio Nino Diaz * - In AArch64 PXN and UXN can be set independently but in 75f9d58d17SAntonio Nino Diaz * AArch32 there is no UXN (XN affects both privilege levels). 76f9d58d17SAntonio Nino Diaz * For consistency, we set them simultaneously in both cases. 77f9d58d17SAntonio Nino Diaz * - RO and RW permissions must be the same in EL1 and EL0. If 78f9d58d17SAntonio Nino Diaz * EL0 can access that memory region, so can EL1, with the 79f9d58d17SAntonio Nino Diaz * same permissions. 80fd2299e6SAntonio Nino Diaz */ 81f9d58d17SAntonio Nino Diaz #if ENABLE_ASSERTIONS 82f9d58d17SAntonio Nino Diaz uint64_t xn_mask = xlat_arch_regime_get_xn_desc(EL1_EL0_REGIME); 83f9d58d17SAntonio Nino Diaz uint64_t xn_perm = desc & xn_mask; 84fd2299e6SAntonio Nino Diaz 85f9d58d17SAntonio Nino Diaz assert((xn_perm == xn_mask) || (xn_perm == 0ULL)); 86f9d58d17SAntonio Nino Diaz #endif 8739b6cc66SAntonio Nino Diaz printf(((desc & LOWER_ATTRS(AP_RO)) != 0ULL) ? "-RO" : "-RW"); 88f9d58d17SAntonio Nino Diaz /* Only check one of PXN and UXN, the other one is the same. */ 8939b6cc66SAntonio Nino Diaz printf(((desc & UPPER_ATTRS(PXN)) != 0ULL) ? "-XN" : "-EXEC"); 90f9d58d17SAntonio Nino Diaz /* 91f9d58d17SAntonio Nino Diaz * Privileged regions can only be accessed from EL1, user 92f9d58d17SAntonio Nino Diaz * regions can be accessed from EL1 and EL0. 93f9d58d17SAntonio Nino Diaz */ 9439b6cc66SAntonio Nino Diaz printf(((desc & LOWER_ATTRS(AP_ACCESS_UNPRIVILEGED)) != 0ULL) 95f9d58d17SAntonio Nino Diaz ? "-USER" : "-PRIV"); 96fd2299e6SAntonio Nino Diaz } 97fd2299e6SAntonio Nino Diaz 9839b6cc66SAntonio Nino Diaz printf(((LOWER_ATTRS(NS) & desc) != 0ULL) ? "-NS" : "-S"); 99fd2299e6SAntonio Nino Diaz } 100fd2299e6SAntonio Nino Diaz 101fd2299e6SAntonio Nino Diaz static const char * const level_spacers[] = { 102fd2299e6SAntonio Nino Diaz "[LV0] ", 103fd2299e6SAntonio Nino Diaz " [LV1] ", 104fd2299e6SAntonio Nino Diaz " [LV2] ", 105fd2299e6SAntonio Nino Diaz " [LV3] " 106fd2299e6SAntonio Nino Diaz }; 107fd2299e6SAntonio Nino Diaz 108fd2299e6SAntonio Nino Diaz static const char *invalid_descriptors_ommited = 109fd2299e6SAntonio Nino Diaz "%s(%d invalid descriptors omitted)\n"; 110fd2299e6SAntonio Nino Diaz 111fd2299e6SAntonio Nino Diaz /* 112*c54c7fc3SDavid Pu * Function that reads the translation tables passed as an argument 113fd2299e6SAntonio Nino Diaz * and prints their status. 114fd2299e6SAntonio Nino Diaz */ 115e7b9886cSAntonio Nino Diaz static void xlat_tables_print_internal(xlat_ctx_t *ctx, uintptr_t table_base_va, 116e7b9886cSAntonio Nino Diaz const uint64_t *table_base, unsigned int table_entries, 117e7b9886cSAntonio Nino Diaz unsigned int level) 118fd2299e6SAntonio Nino Diaz { 119fd2299e6SAntonio Nino Diaz assert(level <= XLAT_TABLE_LEVEL_MAX); 120fd2299e6SAntonio Nino Diaz 121*c54c7fc3SDavid Pu /* 122*c54c7fc3SDavid Pu * data structure to track DESC_TABLE entry before iterate into subtable 123*c54c7fc3SDavid Pu * of next translation level. it will be restored after return from 124*c54c7fc3SDavid Pu * subtable iteration. 125*c54c7fc3SDavid Pu */ 126*c54c7fc3SDavid Pu struct desc_table { 127*c54c7fc3SDavid Pu const uint64_t *table_base; 128*c54c7fc3SDavid Pu uintptr_t table_idx_va; 129*c54c7fc3SDavid Pu unsigned int idx; 130*c54c7fc3SDavid Pu } desc_tables[XLAT_TABLE_LEVEL_MAX + 1] = { 131*c54c7fc3SDavid Pu {NULL, 0U, XLAT_TABLE_ENTRIES}, }; 132*c54c7fc3SDavid Pu unsigned int this_level = level; 133*c54c7fc3SDavid Pu const uint64_t *this_base = table_base; 134*c54c7fc3SDavid Pu unsigned int max_entries = table_entries; 135*c54c7fc3SDavid Pu size_t level_size = XLAT_BLOCK_SIZE(this_level); 136e7b9886cSAntonio Nino Diaz unsigned int table_idx = 0U; 137*c54c7fc3SDavid Pu uintptr_t table_idx_va = table_base_va; 138fd2299e6SAntonio Nino Diaz 139fd2299e6SAntonio Nino Diaz /* 140fd2299e6SAntonio Nino Diaz * Keep track of how many invalid descriptors are counted in a row. 141fd2299e6SAntonio Nino Diaz * Whenever multiple invalid descriptors are found, only the first one 142fd2299e6SAntonio Nino Diaz * is printed, and a line is added to inform about how many descriptors 143fd2299e6SAntonio Nino Diaz * have been omitted. 144fd2299e6SAntonio Nino Diaz */ 145fd2299e6SAntonio Nino Diaz int invalid_row_count = 0; 146fd2299e6SAntonio Nino Diaz 147*c54c7fc3SDavid Pu while (this_base != NULL) { 148*c54c7fc3SDavid Pu /* finish current xlat level */ 149*c54c7fc3SDavid Pu if (table_idx >= max_entries) { 150fd2299e6SAntonio Nino Diaz if (invalid_row_count > 1) { 15139b6cc66SAntonio Nino Diaz printf(invalid_descriptors_ommited, 152*c54c7fc3SDavid Pu level_spacers[this_level], 153fd2299e6SAntonio Nino Diaz invalid_row_count - 1); 154fd2299e6SAntonio Nino Diaz } 155fd2299e6SAntonio Nino Diaz invalid_row_count = 0; 156fd2299e6SAntonio Nino Diaz 157*c54c7fc3SDavid Pu /* no parent level to iterate. */ 158*c54c7fc3SDavid Pu if (this_level <= level) { 159*c54c7fc3SDavid Pu this_base = NULL; 160*c54c7fc3SDavid Pu table_idx = max_entries + 1; 161*c54c7fc3SDavid Pu } else { 162*c54c7fc3SDavid Pu /* retore previous DESC_TABLE entry and start 163*c54c7fc3SDavid Pu * to iterate. 164fd2299e6SAntonio Nino Diaz */ 165*c54c7fc3SDavid Pu this_level--; 166*c54c7fc3SDavid Pu level_size = XLAT_BLOCK_SIZE(this_level); 167*c54c7fc3SDavid Pu this_base = desc_tables[this_level].table_base; 168*c54c7fc3SDavid Pu table_idx = desc_tables[this_level].idx; 169*c54c7fc3SDavid Pu table_idx_va = 170*c54c7fc3SDavid Pu desc_tables[this_level].table_idx_va; 171*c54c7fc3SDavid Pu if (this_level == level) { 172*c54c7fc3SDavid Pu max_entries = table_entries; 173*c54c7fc3SDavid Pu } else { 174*c54c7fc3SDavid Pu max_entries = XLAT_TABLE_ENTRIES; 175*c54c7fc3SDavid Pu } 176*c54c7fc3SDavid Pu 177*c54c7fc3SDavid Pu assert(this_base != NULL); 178*c54c7fc3SDavid Pu } 179*c54c7fc3SDavid Pu } else { 180*c54c7fc3SDavid Pu uint64_t desc = this_base[table_idx]; 181*c54c7fc3SDavid Pu 182*c54c7fc3SDavid Pu if ((desc & DESC_MASK) == INVALID_DESC) { 183*c54c7fc3SDavid Pu if (invalid_row_count == 0) { 184*c54c7fc3SDavid Pu printf("%sVA:0x%lx size:0x%zx\n", 185*c54c7fc3SDavid Pu level_spacers[this_level], 186*c54c7fc3SDavid Pu table_idx_va, level_size); 187*c54c7fc3SDavid Pu } 188*c54c7fc3SDavid Pu invalid_row_count++; 189*c54c7fc3SDavid Pu table_idx++; 190*c54c7fc3SDavid Pu table_idx_va += level_size; 191*c54c7fc3SDavid Pu } else { 192*c54c7fc3SDavid Pu if (invalid_row_count > 1) { 193*c54c7fc3SDavid Pu printf(invalid_descriptors_ommited, 194*c54c7fc3SDavid Pu level_spacers[this_level], 195*c54c7fc3SDavid Pu invalid_row_count - 1); 196*c54c7fc3SDavid Pu } 197*c54c7fc3SDavid Pu invalid_row_count = 0; 198fd2299e6SAntonio Nino Diaz /* 199*c54c7fc3SDavid Pu * Check if this is a table or a block. Tables 200*c54c7fc3SDavid Pu * are only allowed in levels other than 3, but 201*c54c7fc3SDavid Pu * DESC_PAGE has the same value as DESC_TABLE, 202*c54c7fc3SDavid Pu * so we need to check. 203*c54c7fc3SDavid Pu */ 204*c54c7fc3SDavid Pu 205*c54c7fc3SDavid Pu if (((desc & DESC_MASK) == TABLE_DESC) && 206*c54c7fc3SDavid Pu (this_level < XLAT_TABLE_LEVEL_MAX)) { 207*c54c7fc3SDavid Pu uintptr_t addr_inner; 208*c54c7fc3SDavid Pu 209*c54c7fc3SDavid Pu /* 210*c54c7fc3SDavid Pu * Do not print any PA for a table 211*c54c7fc3SDavid Pu * descriptor, as it doesn't directly 212*c54c7fc3SDavid Pu * map physical memory but instead 213*c54c7fc3SDavid Pu * points to the next translation 214fd2299e6SAntonio Nino Diaz * table in the translation table walk. 215fd2299e6SAntonio Nino Diaz */ 21639b6cc66SAntonio Nino Diaz printf("%sVA:0x%lx size:0x%zx\n", 217*c54c7fc3SDavid Pu level_spacers[this_level], 218e7b9886cSAntonio Nino Diaz table_idx_va, level_size); 219fd2299e6SAntonio Nino Diaz 220*c54c7fc3SDavid Pu addr_inner = desc & TABLE_ADDR_MASK; 221*c54c7fc3SDavid Pu /* save current xlat level */ 222*c54c7fc3SDavid Pu desc_tables[this_level].table_base = 223*c54c7fc3SDavid Pu this_base; 224*c54c7fc3SDavid Pu desc_tables[this_level].idx = 225*c54c7fc3SDavid Pu table_idx + 1; 226*c54c7fc3SDavid Pu desc_tables[this_level].table_idx_va = 227*c54c7fc3SDavid Pu table_idx_va + level_size; 228fd2299e6SAntonio Nino Diaz 229*c54c7fc3SDavid Pu /* start iterating next level entries */ 230*c54c7fc3SDavid Pu this_base = (uint64_t *)addr_inner; 231*c54c7fc3SDavid Pu max_entries = XLAT_TABLE_ENTRIES; 232*c54c7fc3SDavid Pu this_level++; 233*c54c7fc3SDavid Pu level_size = 234*c54c7fc3SDavid Pu XLAT_BLOCK_SIZE(this_level); 235*c54c7fc3SDavid Pu table_idx = 0U; 236fd2299e6SAntonio Nino Diaz } else { 23739b6cc66SAntonio Nino Diaz printf("%sVA:0x%lx PA:0x%llx size:0x%zx ", 238*c54c7fc3SDavid Pu level_spacers[this_level], 239*c54c7fc3SDavid Pu table_idx_va, 240e7b9886cSAntonio Nino Diaz (uint64_t)(desc & TABLE_ADDR_MASK), 241fd2299e6SAntonio Nino Diaz level_size); 242fd2299e6SAntonio Nino Diaz xlat_desc_print(ctx, desc); 24339b6cc66SAntonio Nino Diaz printf("\n"); 244fd2299e6SAntonio Nino Diaz 245fd2299e6SAntonio Nino Diaz table_idx++; 246fd2299e6SAntonio Nino Diaz table_idx_va += level_size; 247fd2299e6SAntonio Nino Diaz 248*c54c7fc3SDavid Pu } 249*c54c7fc3SDavid Pu } 250*c54c7fc3SDavid Pu } 251fd2299e6SAntonio Nino Diaz } 252fd2299e6SAntonio Nino Diaz } 253fd2299e6SAntonio Nino Diaz 254fd2299e6SAntonio Nino Diaz void xlat_tables_print(xlat_ctx_t *ctx) 255fd2299e6SAntonio Nino Diaz { 256fd2299e6SAntonio Nino Diaz const char *xlat_regime_str; 257e7b9886cSAntonio Nino Diaz int used_page_tables; 258e7b9886cSAntonio Nino Diaz 259fd2299e6SAntonio Nino Diaz if (ctx->xlat_regime == EL1_EL0_REGIME) { 260fd2299e6SAntonio Nino Diaz xlat_regime_str = "1&0"; 2611a92a0e0SAntonio Nino Diaz } else if (ctx->xlat_regime == EL2_REGIME) { 2621a92a0e0SAntonio Nino Diaz xlat_regime_str = "2"; 263fd2299e6SAntonio Nino Diaz } else { 264fd2299e6SAntonio Nino Diaz assert(ctx->xlat_regime == EL3_REGIME); 265fd2299e6SAntonio Nino Diaz xlat_regime_str = "3"; 266fd2299e6SAntonio Nino Diaz } 267fd2299e6SAntonio Nino Diaz VERBOSE("Translation tables state:\n"); 268fd2299e6SAntonio Nino Diaz VERBOSE(" Xlat regime: EL%s\n", xlat_regime_str); 269fd2299e6SAntonio Nino Diaz VERBOSE(" Max allowed PA: 0x%llx\n", ctx->pa_max_address); 270e7b9886cSAntonio Nino Diaz VERBOSE(" Max allowed VA: 0x%lx\n", ctx->va_max_address); 271fd2299e6SAntonio Nino Diaz VERBOSE(" Max mapped PA: 0x%llx\n", ctx->max_pa); 272e7b9886cSAntonio Nino Diaz VERBOSE(" Max mapped VA: 0x%lx\n", ctx->max_va); 273fd2299e6SAntonio Nino Diaz 274e7b9886cSAntonio Nino Diaz VERBOSE(" Initial lookup level: %u\n", ctx->base_level); 275e7b9886cSAntonio Nino Diaz VERBOSE(" Entries @initial lookup level: %u\n", 276fd2299e6SAntonio Nino Diaz ctx->base_table_entries); 277fd2299e6SAntonio Nino Diaz 278fd2299e6SAntonio Nino Diaz #if PLAT_XLAT_TABLES_DYNAMIC 279fd2299e6SAntonio Nino Diaz used_page_tables = 0; 280e7b9886cSAntonio Nino Diaz for (int i = 0; i < ctx->tables_num; ++i) { 281fd2299e6SAntonio Nino Diaz if (ctx->tables_mapped_regions[i] != 0) 282fd2299e6SAntonio Nino Diaz ++used_page_tables; 283fd2299e6SAntonio Nino Diaz } 284fd2299e6SAntonio Nino Diaz #else 285fd2299e6SAntonio Nino Diaz used_page_tables = ctx->next_table; 286fd2299e6SAntonio Nino Diaz #endif 287e7b9886cSAntonio Nino Diaz VERBOSE(" Used %d sub-tables out of %d (spare: %d)\n", 288fd2299e6SAntonio Nino Diaz used_page_tables, ctx->tables_num, 289fd2299e6SAntonio Nino Diaz ctx->tables_num - used_page_tables); 290fd2299e6SAntonio Nino Diaz 291e7b9886cSAntonio Nino Diaz xlat_tables_print_internal(ctx, 0U, ctx->base_table, 292fd2299e6SAntonio Nino Diaz ctx->base_table_entries, ctx->base_level); 293fd2299e6SAntonio Nino Diaz } 294fd2299e6SAntonio Nino Diaz 295fd2299e6SAntonio Nino Diaz #endif /* LOG_LEVEL >= LOG_LEVEL_VERBOSE */ 296fd2299e6SAntonio Nino Diaz 297fd2299e6SAntonio Nino Diaz /* 298fd2299e6SAntonio Nino Diaz * Do a translation table walk to find the block or page descriptor that maps 299fd2299e6SAntonio Nino Diaz * virtual_addr. 300fd2299e6SAntonio Nino Diaz * 301fd2299e6SAntonio Nino Diaz * On success, return the address of the descriptor within the translation 302fd2299e6SAntonio Nino Diaz * table. Its lookup level is stored in '*out_level'. 303fd2299e6SAntonio Nino Diaz * On error, return NULL. 304fd2299e6SAntonio Nino Diaz * 305fd2299e6SAntonio Nino Diaz * xlat_table_base 306fd2299e6SAntonio Nino Diaz * Base address for the initial lookup level. 307fd2299e6SAntonio Nino Diaz * xlat_table_base_entries 308fd2299e6SAntonio Nino Diaz * Number of entries in the translation table for the initial lookup level. 309fd2299e6SAntonio Nino Diaz * virt_addr_space_size 310fd2299e6SAntonio Nino Diaz * Size in bytes of the virtual address space. 311fd2299e6SAntonio Nino Diaz */ 312fd2299e6SAntonio Nino Diaz static uint64_t *find_xlat_table_entry(uintptr_t virtual_addr, 313fd2299e6SAntonio Nino Diaz void *xlat_table_base, 314e7b9886cSAntonio Nino Diaz unsigned int xlat_table_base_entries, 315fd2299e6SAntonio Nino Diaz unsigned long long virt_addr_space_size, 316e7b9886cSAntonio Nino Diaz unsigned int *out_level) 317fd2299e6SAntonio Nino Diaz { 318fd2299e6SAntonio Nino Diaz unsigned int start_level; 319fd2299e6SAntonio Nino Diaz uint64_t *table; 320e7b9886cSAntonio Nino Diaz unsigned int entries; 321fd2299e6SAntonio Nino Diaz 322fd2299e6SAntonio Nino Diaz start_level = GET_XLAT_TABLE_LEVEL_BASE(virt_addr_space_size); 323fd2299e6SAntonio Nino Diaz 324fd2299e6SAntonio Nino Diaz table = xlat_table_base; 325fd2299e6SAntonio Nino Diaz entries = xlat_table_base_entries; 326fd2299e6SAntonio Nino Diaz 327fd2299e6SAntonio Nino Diaz for (unsigned int level = start_level; 328fd2299e6SAntonio Nino Diaz level <= XLAT_TABLE_LEVEL_MAX; 329fd2299e6SAntonio Nino Diaz ++level) { 330e7b9886cSAntonio Nino Diaz uint64_t idx, desc, desc_type; 331fd2299e6SAntonio Nino Diaz 332fd2299e6SAntonio Nino Diaz idx = XLAT_TABLE_IDX(virtual_addr, level); 333fd2299e6SAntonio Nino Diaz if (idx >= entries) { 3346a086061SAntonio Nino Diaz WARN("Missing xlat table entry at address 0x%lx\n", 3356a086061SAntonio Nino Diaz virtual_addr); 336fd2299e6SAntonio Nino Diaz return NULL; 337fd2299e6SAntonio Nino Diaz } 338fd2299e6SAntonio Nino Diaz 339fd2299e6SAntonio Nino Diaz desc = table[idx]; 340fd2299e6SAntonio Nino Diaz desc_type = desc & DESC_MASK; 341fd2299e6SAntonio Nino Diaz 342fd2299e6SAntonio Nino Diaz if (desc_type == INVALID_DESC) { 343fd2299e6SAntonio Nino Diaz VERBOSE("Invalid entry (memory not mapped)\n"); 344fd2299e6SAntonio Nino Diaz return NULL; 345fd2299e6SAntonio Nino Diaz } 346fd2299e6SAntonio Nino Diaz 347fd2299e6SAntonio Nino Diaz if (level == XLAT_TABLE_LEVEL_MAX) { 348fd2299e6SAntonio Nino Diaz /* 3496a086061SAntonio Nino Diaz * Only page descriptors allowed at the final lookup 350fd2299e6SAntonio Nino Diaz * level. 351fd2299e6SAntonio Nino Diaz */ 352fd2299e6SAntonio Nino Diaz assert(desc_type == PAGE_DESC); 353fd2299e6SAntonio Nino Diaz *out_level = level; 354fd2299e6SAntonio Nino Diaz return &table[idx]; 355fd2299e6SAntonio Nino Diaz } 356fd2299e6SAntonio Nino Diaz 357fd2299e6SAntonio Nino Diaz if (desc_type == BLOCK_DESC) { 358fd2299e6SAntonio Nino Diaz *out_level = level; 359fd2299e6SAntonio Nino Diaz return &table[idx]; 360fd2299e6SAntonio Nino Diaz } 361fd2299e6SAntonio Nino Diaz 362fd2299e6SAntonio Nino Diaz assert(desc_type == TABLE_DESC); 363fd2299e6SAntonio Nino Diaz table = (uint64_t *)(uintptr_t)(desc & TABLE_ADDR_MASK); 364fd2299e6SAntonio Nino Diaz entries = XLAT_TABLE_ENTRIES; 365fd2299e6SAntonio Nino Diaz } 366fd2299e6SAntonio Nino Diaz 367fd2299e6SAntonio Nino Diaz /* 368fd2299e6SAntonio Nino Diaz * This shouldn't be reached, the translation table walk should end at 369fd2299e6SAntonio Nino Diaz * most at level XLAT_TABLE_LEVEL_MAX and return from inside the loop. 370fd2299e6SAntonio Nino Diaz */ 3715b395e37SAntonio Nino Diaz assert(false); 372fd2299e6SAntonio Nino Diaz 373fd2299e6SAntonio Nino Diaz return NULL; 374fd2299e6SAntonio Nino Diaz } 375fd2299e6SAntonio Nino Diaz 376fd2299e6SAntonio Nino Diaz 377e5d59519SAntonio Nino Diaz static int xlat_get_mem_attributes_internal(const xlat_ctx_t *ctx, 378e5d59519SAntonio Nino Diaz uintptr_t base_va, uint32_t *attributes, uint64_t **table_entry, 379e7b9886cSAntonio Nino Diaz unsigned long long *addr_pa, unsigned int *table_level) 380fd2299e6SAntonio Nino Diaz { 381fd2299e6SAntonio Nino Diaz uint64_t *entry; 382fd2299e6SAntonio Nino Diaz uint64_t desc; 383e7b9886cSAntonio Nino Diaz unsigned int level; 384fd2299e6SAntonio Nino Diaz unsigned long long virt_addr_space_size; 385fd2299e6SAntonio Nino Diaz 386fd2299e6SAntonio Nino Diaz /* 387fd2299e6SAntonio Nino Diaz * Sanity-check arguments. 388fd2299e6SAntonio Nino Diaz */ 389fd2299e6SAntonio Nino Diaz assert(ctx != NULL); 3905b395e37SAntonio Nino Diaz assert(ctx->initialized); 391e7b9886cSAntonio Nino Diaz assert((ctx->xlat_regime == EL1_EL0_REGIME) || 3921a92a0e0SAntonio Nino Diaz (ctx->xlat_regime == EL2_REGIME) || 393e7b9886cSAntonio Nino Diaz (ctx->xlat_regime == EL3_REGIME)); 394fd2299e6SAntonio Nino Diaz 395e7b9886cSAntonio Nino Diaz virt_addr_space_size = (unsigned long long)ctx->va_max_address + 1ULL; 396e7b9886cSAntonio Nino Diaz assert(virt_addr_space_size > 0U); 397fd2299e6SAntonio Nino Diaz 398fd2299e6SAntonio Nino Diaz entry = find_xlat_table_entry(base_va, 399fd2299e6SAntonio Nino Diaz ctx->base_table, 400fd2299e6SAntonio Nino Diaz ctx->base_table_entries, 401fd2299e6SAntonio Nino Diaz virt_addr_space_size, 402fd2299e6SAntonio Nino Diaz &level); 403fd2299e6SAntonio Nino Diaz if (entry == NULL) { 404e7b9886cSAntonio Nino Diaz WARN("Address 0x%lx is not mapped.\n", base_va); 405fd2299e6SAntonio Nino Diaz return -EINVAL; 406fd2299e6SAntonio Nino Diaz } 407fd2299e6SAntonio Nino Diaz 408fd2299e6SAntonio Nino Diaz if (addr_pa != NULL) { 409fd2299e6SAntonio Nino Diaz *addr_pa = *entry & TABLE_ADDR_MASK; 410fd2299e6SAntonio Nino Diaz } 411fd2299e6SAntonio Nino Diaz 412fd2299e6SAntonio Nino Diaz if (table_entry != NULL) { 413fd2299e6SAntonio Nino Diaz *table_entry = entry; 414fd2299e6SAntonio Nino Diaz } 415fd2299e6SAntonio Nino Diaz 416fd2299e6SAntonio Nino Diaz if (table_level != NULL) { 417fd2299e6SAntonio Nino Diaz *table_level = level; 418fd2299e6SAntonio Nino Diaz } 419fd2299e6SAntonio Nino Diaz 420fd2299e6SAntonio Nino Diaz desc = *entry; 421fd2299e6SAntonio Nino Diaz 422fd2299e6SAntonio Nino Diaz #if LOG_LEVEL >= LOG_LEVEL_VERBOSE 423fd2299e6SAntonio Nino Diaz VERBOSE("Attributes: "); 424fd2299e6SAntonio Nino Diaz xlat_desc_print(ctx, desc); 42539b6cc66SAntonio Nino Diaz printf("\n"); 426fd2299e6SAntonio Nino Diaz #endif /* LOG_LEVEL >= LOG_LEVEL_VERBOSE */ 427fd2299e6SAntonio Nino Diaz 428fd2299e6SAntonio Nino Diaz assert(attributes != NULL); 429e7b9886cSAntonio Nino Diaz *attributes = 0U; 430fd2299e6SAntonio Nino Diaz 431e7b9886cSAntonio Nino Diaz uint64_t attr_index = (desc >> ATTR_INDEX_SHIFT) & ATTR_INDEX_MASK; 432fd2299e6SAntonio Nino Diaz 433fd2299e6SAntonio Nino Diaz if (attr_index == ATTR_IWBWA_OWBWA_NTR_INDEX) { 434fd2299e6SAntonio Nino Diaz *attributes |= MT_MEMORY; 435fd2299e6SAntonio Nino Diaz } else if (attr_index == ATTR_NON_CACHEABLE_INDEX) { 436fd2299e6SAntonio Nino Diaz *attributes |= MT_NON_CACHEABLE; 437fd2299e6SAntonio Nino Diaz } else { 438fd2299e6SAntonio Nino Diaz assert(attr_index == ATTR_DEVICE_INDEX); 439fd2299e6SAntonio Nino Diaz *attributes |= MT_DEVICE; 440fd2299e6SAntonio Nino Diaz } 441fd2299e6SAntonio Nino Diaz 442e7b9886cSAntonio Nino Diaz uint64_t ap2_bit = (desc >> AP2_SHIFT) & 1U; 443fd2299e6SAntonio Nino Diaz 444fd2299e6SAntonio Nino Diaz if (ap2_bit == AP2_RW) 445fd2299e6SAntonio Nino Diaz *attributes |= MT_RW; 446fd2299e6SAntonio Nino Diaz 447fd2299e6SAntonio Nino Diaz if (ctx->xlat_regime == EL1_EL0_REGIME) { 448e7b9886cSAntonio Nino Diaz uint64_t ap1_bit = (desc >> AP1_SHIFT) & 1U; 449e7b9886cSAntonio Nino Diaz 450fd2299e6SAntonio Nino Diaz if (ap1_bit == AP1_ACCESS_UNPRIVILEGED) 451fd2299e6SAntonio Nino Diaz *attributes |= MT_USER; 452fd2299e6SAntonio Nino Diaz } 453fd2299e6SAntonio Nino Diaz 454e7b9886cSAntonio Nino Diaz uint64_t ns_bit = (desc >> NS_SHIFT) & 1U; 455fd2299e6SAntonio Nino Diaz 456e7b9886cSAntonio Nino Diaz if (ns_bit == 1U) 457fd2299e6SAntonio Nino Diaz *attributes |= MT_NS; 458fd2299e6SAntonio Nino Diaz 459fd2299e6SAntonio Nino Diaz uint64_t xn_mask = xlat_arch_regime_get_xn_desc(ctx->xlat_regime); 460fd2299e6SAntonio Nino Diaz 461fd2299e6SAntonio Nino Diaz if ((desc & xn_mask) == xn_mask) { 462fd2299e6SAntonio Nino Diaz *attributes |= MT_EXECUTE_NEVER; 463fd2299e6SAntonio Nino Diaz } else { 464e7b9886cSAntonio Nino Diaz assert((desc & xn_mask) == 0U); 465fd2299e6SAntonio Nino Diaz } 466fd2299e6SAntonio Nino Diaz 467fd2299e6SAntonio Nino Diaz return 0; 468fd2299e6SAntonio Nino Diaz } 469fd2299e6SAntonio Nino Diaz 470fd2299e6SAntonio Nino Diaz 471e5d59519SAntonio Nino Diaz int xlat_get_mem_attributes_ctx(const xlat_ctx_t *ctx, uintptr_t base_va, 472e5d59519SAntonio Nino Diaz uint32_t *attr) 473fd2299e6SAntonio Nino Diaz { 474e5d59519SAntonio Nino Diaz return xlat_get_mem_attributes_internal(ctx, base_va, attr, 475fd2299e6SAntonio Nino Diaz NULL, NULL, NULL); 476fd2299e6SAntonio Nino Diaz } 477fd2299e6SAntonio Nino Diaz 478fd2299e6SAntonio Nino Diaz 479e5d59519SAntonio Nino Diaz int xlat_change_mem_attributes_ctx(const xlat_ctx_t *ctx, uintptr_t base_va, 480e5d59519SAntonio Nino Diaz size_t size, uint32_t attr) 481fd2299e6SAntonio Nino Diaz { 482fd2299e6SAntonio Nino Diaz /* Note: This implementation isn't optimized. */ 483fd2299e6SAntonio Nino Diaz 484fd2299e6SAntonio Nino Diaz assert(ctx != NULL); 4855b395e37SAntonio Nino Diaz assert(ctx->initialized); 486fd2299e6SAntonio Nino Diaz 487fd2299e6SAntonio Nino Diaz unsigned long long virt_addr_space_size = 488e7b9886cSAntonio Nino Diaz (unsigned long long)ctx->va_max_address + 1U; 489e7b9886cSAntonio Nino Diaz assert(virt_addr_space_size > 0U); 490fd2299e6SAntonio Nino Diaz 491fd2299e6SAntonio Nino Diaz if (!IS_PAGE_ALIGNED(base_va)) { 492e7b9886cSAntonio Nino Diaz WARN("%s: Address 0x%lx is not aligned on a page boundary.\n", 493e7b9886cSAntonio Nino Diaz __func__, base_va); 494fd2299e6SAntonio Nino Diaz return -EINVAL; 495fd2299e6SAntonio Nino Diaz } 496fd2299e6SAntonio Nino Diaz 497e7b9886cSAntonio Nino Diaz if (size == 0U) { 498fd2299e6SAntonio Nino Diaz WARN("%s: Size is 0.\n", __func__); 499fd2299e6SAntonio Nino Diaz return -EINVAL; 500fd2299e6SAntonio Nino Diaz } 501fd2299e6SAntonio Nino Diaz 502e7b9886cSAntonio Nino Diaz if ((size % PAGE_SIZE) != 0U) { 503fd2299e6SAntonio Nino Diaz WARN("%s: Size 0x%zx is not a multiple of a page size.\n", 504fd2299e6SAntonio Nino Diaz __func__, size); 505fd2299e6SAntonio Nino Diaz return -EINVAL; 506fd2299e6SAntonio Nino Diaz } 507fd2299e6SAntonio Nino Diaz 508e7b9886cSAntonio Nino Diaz if (((attr & MT_EXECUTE_NEVER) == 0U) && ((attr & MT_RW) != 0U)) { 5096a086061SAntonio Nino Diaz WARN("%s: Mapping memory as read-write and executable not allowed.\n", 510fd2299e6SAntonio Nino Diaz __func__); 511fd2299e6SAntonio Nino Diaz return -EINVAL; 512fd2299e6SAntonio Nino Diaz } 513fd2299e6SAntonio Nino Diaz 514e7b9886cSAntonio Nino Diaz size_t pages_count = size / PAGE_SIZE; 515fd2299e6SAntonio Nino Diaz 516e7b9886cSAntonio Nino Diaz VERBOSE("Changing memory attributes of %zu pages starting from address 0x%lx...\n", 517e7b9886cSAntonio Nino Diaz pages_count, base_va); 518fd2299e6SAntonio Nino Diaz 519fd2299e6SAntonio Nino Diaz uintptr_t base_va_original = base_va; 520fd2299e6SAntonio Nino Diaz 521fd2299e6SAntonio Nino Diaz /* 522fd2299e6SAntonio Nino Diaz * Sanity checks. 523fd2299e6SAntonio Nino Diaz */ 524e7b9886cSAntonio Nino Diaz for (size_t i = 0U; i < pages_count; ++i) { 525e7b9886cSAntonio Nino Diaz const uint64_t *entry; 526e7b9886cSAntonio Nino Diaz uint64_t desc, attr_index; 527e7b9886cSAntonio Nino Diaz unsigned int level; 528fd2299e6SAntonio Nino Diaz 529fd2299e6SAntonio Nino Diaz entry = find_xlat_table_entry(base_va, 530fd2299e6SAntonio Nino Diaz ctx->base_table, 531fd2299e6SAntonio Nino Diaz ctx->base_table_entries, 532fd2299e6SAntonio Nino Diaz virt_addr_space_size, 533fd2299e6SAntonio Nino Diaz &level); 534fd2299e6SAntonio Nino Diaz if (entry == NULL) { 535e7b9886cSAntonio Nino Diaz WARN("Address 0x%lx is not mapped.\n", base_va); 536fd2299e6SAntonio Nino Diaz return -EINVAL; 537fd2299e6SAntonio Nino Diaz } 538fd2299e6SAntonio Nino Diaz 539fd2299e6SAntonio Nino Diaz desc = *entry; 540fd2299e6SAntonio Nino Diaz 541fd2299e6SAntonio Nino Diaz /* 542fd2299e6SAntonio Nino Diaz * Check that all the required pages are mapped at page 543fd2299e6SAntonio Nino Diaz * granularity. 544fd2299e6SAntonio Nino Diaz */ 545fd2299e6SAntonio Nino Diaz if (((desc & DESC_MASK) != PAGE_DESC) || 546fd2299e6SAntonio Nino Diaz (level != XLAT_TABLE_LEVEL_MAX)) { 547e7b9886cSAntonio Nino Diaz WARN("Address 0x%lx is not mapped at the right granularity.\n", 548e7b9886cSAntonio Nino Diaz base_va); 549fd2299e6SAntonio Nino Diaz WARN("Granularity is 0x%llx, should be 0x%x.\n", 550fd2299e6SAntonio Nino Diaz (unsigned long long)XLAT_BLOCK_SIZE(level), PAGE_SIZE); 551fd2299e6SAntonio Nino Diaz return -EINVAL; 552fd2299e6SAntonio Nino Diaz } 553fd2299e6SAntonio Nino Diaz 554fd2299e6SAntonio Nino Diaz /* 555fd2299e6SAntonio Nino Diaz * If the region type is device, it shouldn't be executable. 556fd2299e6SAntonio Nino Diaz */ 557e7b9886cSAntonio Nino Diaz attr_index = (desc >> ATTR_INDEX_SHIFT) & ATTR_INDEX_MASK; 558fd2299e6SAntonio Nino Diaz if (attr_index == ATTR_DEVICE_INDEX) { 559e7b9886cSAntonio Nino Diaz if ((attr & MT_EXECUTE_NEVER) == 0U) { 560e7b9886cSAntonio Nino Diaz WARN("Setting device memory as executable at address 0x%lx.", 561e7b9886cSAntonio Nino Diaz base_va); 562fd2299e6SAntonio Nino Diaz return -EINVAL; 563fd2299e6SAntonio Nino Diaz } 564fd2299e6SAntonio Nino Diaz } 565fd2299e6SAntonio Nino Diaz 566fd2299e6SAntonio Nino Diaz base_va += PAGE_SIZE; 567fd2299e6SAntonio Nino Diaz } 568fd2299e6SAntonio Nino Diaz 569fd2299e6SAntonio Nino Diaz /* Restore original value. */ 570fd2299e6SAntonio Nino Diaz base_va = base_va_original; 571fd2299e6SAntonio Nino Diaz 572e7b9886cSAntonio Nino Diaz for (unsigned int i = 0U; i < pages_count; ++i) { 573fd2299e6SAntonio Nino Diaz 574e7b9886cSAntonio Nino Diaz uint32_t old_attr = 0U, new_attr; 575e7b9886cSAntonio Nino Diaz uint64_t *entry = NULL; 576e7b9886cSAntonio Nino Diaz unsigned int level = 0U; 577e7b9886cSAntonio Nino Diaz unsigned long long addr_pa = 0ULL; 578fd2299e6SAntonio Nino Diaz 579e5d59519SAntonio Nino Diaz (void) xlat_get_mem_attributes_internal(ctx, base_va, &old_attr, 580fd2299e6SAntonio Nino Diaz &entry, &addr_pa, &level); 581fd2299e6SAntonio Nino Diaz 582fd2299e6SAntonio Nino Diaz /* 583fd2299e6SAntonio Nino Diaz * From attr, only MT_RO/MT_RW, MT_EXECUTE/MT_EXECUTE_NEVER and 584fd2299e6SAntonio Nino Diaz * MT_USER/MT_PRIVILEGED are taken into account. Any other 585fd2299e6SAntonio Nino Diaz * information is ignored. 586fd2299e6SAntonio Nino Diaz */ 587fd2299e6SAntonio Nino Diaz 588fd2299e6SAntonio Nino Diaz /* Clean the old attributes so that they can be rebuilt. */ 589fd2299e6SAntonio Nino Diaz new_attr = old_attr & ~(MT_RW | MT_EXECUTE_NEVER | MT_USER); 590fd2299e6SAntonio Nino Diaz 591fd2299e6SAntonio Nino Diaz /* 592fd2299e6SAntonio Nino Diaz * Update attributes, but filter out the ones this function 593fd2299e6SAntonio Nino Diaz * isn't allowed to change. 594fd2299e6SAntonio Nino Diaz */ 595fd2299e6SAntonio Nino Diaz new_attr |= attr & (MT_RW | MT_EXECUTE_NEVER | MT_USER); 596fd2299e6SAntonio Nino Diaz 597fd2299e6SAntonio Nino Diaz /* 598fd2299e6SAntonio Nino Diaz * The break-before-make sequence requires writing an invalid 599fd2299e6SAntonio Nino Diaz * descriptor and making sure that the system sees the change 600fd2299e6SAntonio Nino Diaz * before writing the new descriptor. 601fd2299e6SAntonio Nino Diaz */ 602fd2299e6SAntonio Nino Diaz *entry = INVALID_DESC; 6033e318e40SAntonio Nino Diaz #if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY) 6043e318e40SAntonio Nino Diaz dccvac((uintptr_t)entry); 6053e318e40SAntonio Nino Diaz #endif 606fd2299e6SAntonio Nino Diaz /* Invalidate any cached copy of this mapping in the TLBs. */ 6078d164bc6SAntonio Nino Diaz xlat_arch_tlbi_va(base_va, ctx->xlat_regime); 608fd2299e6SAntonio Nino Diaz 609fd2299e6SAntonio Nino Diaz /* Ensure completion of the invalidation. */ 610fd2299e6SAntonio Nino Diaz xlat_arch_tlbi_va_sync(); 611fd2299e6SAntonio Nino Diaz 612fd2299e6SAntonio Nino Diaz /* Write new descriptor */ 613fd2299e6SAntonio Nino Diaz *entry = xlat_desc(ctx, new_attr, addr_pa, level); 6143e318e40SAntonio Nino Diaz #if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY) 6153e318e40SAntonio Nino Diaz dccvac((uintptr_t)entry); 6163e318e40SAntonio Nino Diaz #endif 617fd2299e6SAntonio Nino Diaz base_va += PAGE_SIZE; 618fd2299e6SAntonio Nino Diaz } 619fd2299e6SAntonio Nino Diaz 620fd2299e6SAntonio Nino Diaz /* Ensure that the last descriptor writen is seen by the system. */ 621fd2299e6SAntonio Nino Diaz dsbish(); 622fd2299e6SAntonio Nino Diaz 623fd2299e6SAntonio Nino Diaz return 0; 624fd2299e6SAntonio Nino Diaz } 625