Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 1 | /* |
Manish V Badarkhe | 468c3d4 | 2024-06-24 14:35:46 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2024, Arm Limited and Contributors. All rights reserved. |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 7 | #include <assert.h> |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 8 | #include <errno.h> |
Javier Almansa Sobrino | 2a8af48 | 2021-11-25 09:29:27 +0000 | [diff] [blame] | 9 | #include <inttypes.h> |
Antonio Nino Diaz | 5c97bd1 | 2018-08-02 09:57:29 +0100 | [diff] [blame] | 10 | #include <stdbool.h> |
Antonio Nino Diaz | 4b32e62 | 2018-08-16 16:52:57 +0100 | [diff] [blame] | 11 | #include <stdint.h> |
Antonio Nino Diaz | 00086e3 | 2018-08-16 16:46:06 +0100 | [diff] [blame] | 12 | #include <stdio.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 13 | |
| 14 | #include <platform_def.h> |
| 15 | |
Manish V Badarkhe | 468c3d4 | 2024-06-24 14:35:46 +0100 | [diff] [blame] | 16 | #include <arch_features.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 17 | #include <arch_helpers.h> |
| 18 | #include <common/debug.h> |
| 19 | #include <lib/utils_def.h> |
| 20 | #include <lib/xlat_tables/xlat_tables_defs.h> |
| 21 | #include <lib/xlat_tables/xlat_tables_v2.h> |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 22 | |
| 23 | #include "xlat_tables_private.h" |
| 24 | |
| 25 | #if LOG_LEVEL < LOG_LEVEL_VERBOSE |
| 26 | |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 27 | void xlat_mmap_print(__unused const mmap_region_t *mmap) |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 28 | { |
| 29 | /* Empty */ |
| 30 | } |
| 31 | |
| 32 | void xlat_tables_print(__unused xlat_ctx_t *ctx) |
| 33 | { |
| 34 | /* Empty */ |
| 35 | } |
| 36 | |
| 37 | #else /* if LOG_LEVEL >= LOG_LEVEL_VERBOSE */ |
| 38 | |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 39 | void xlat_mmap_print(const mmap_region_t *mmap) |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 40 | { |
Antonio Nino Diaz | 00086e3 | 2018-08-16 16:46:06 +0100 | [diff] [blame] | 41 | printf("mmap:\n"); |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 42 | const mmap_region_t *mm = mmap; |
| 43 | |
| 44 | while (mm->size != 0U) { |
Antonio Nino Diaz | 00086e3 | 2018-08-16 16:46:06 +0100 | [diff] [blame] | 45 | printf(" VA:0x%lx PA:0x%llx size:0x%zx attr:0x%x granularity:0x%zx\n", |
| 46 | mm->base_va, mm->base_pa, mm->size, mm->attr, |
| 47 | mm->granularity); |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 48 | ++mm; |
| 49 | }; |
Antonio Nino Diaz | 00086e3 | 2018-08-16 16:46:06 +0100 | [diff] [blame] | 50 | printf("\n"); |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | /* Print the attributes of the specified block descriptor. */ |
| 54 | static void xlat_desc_print(const xlat_ctx_t *ctx, uint64_t desc) |
| 55 | { |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 56 | uint64_t mem_type_index = ATTR_INDEX_GET(desc); |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 57 | int xlat_regime = ctx->xlat_regime; |
| 58 | |
| 59 | if (mem_type_index == ATTR_IWBWA_OWBWA_NTR_INDEX) { |
Antonio Nino Diaz | 00086e3 | 2018-08-16 16:46:06 +0100 | [diff] [blame] | 60 | printf("MEM"); |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 61 | } else if (mem_type_index == ATTR_NON_CACHEABLE_INDEX) { |
Antonio Nino Diaz | 00086e3 | 2018-08-16 16:46:06 +0100 | [diff] [blame] | 62 | printf("NC"); |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 63 | } else { |
| 64 | assert(mem_type_index == ATTR_DEVICE_INDEX); |
Antonio Nino Diaz | 00086e3 | 2018-08-16 16:46:06 +0100 | [diff] [blame] | 65 | printf("DEV"); |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 66 | } |
| 67 | |
Antonio Nino Diaz | 128de8d | 2018-08-07 19:59:49 +0100 | [diff] [blame] | 68 | if ((xlat_regime == EL3_REGIME) || (xlat_regime == EL2_REGIME)) { |
| 69 | /* For EL3 and EL2 only check the AP[2] and XN bits. */ |
Antonio Nino Diaz | 00086e3 | 2018-08-16 16:46:06 +0100 | [diff] [blame] | 70 | printf(((desc & LOWER_ATTRS(AP_RO)) != 0ULL) ? "-RO" : "-RW"); |
| 71 | printf(((desc & UPPER_ATTRS(XN)) != 0ULL) ? "-XN" : "-EXEC"); |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 72 | } else { |
Antonio Nino Diaz | 7c1812e | 2018-06-27 14:59:22 +0100 | [diff] [blame] | 73 | assert(xlat_regime == EL1_EL0_REGIME); |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 74 | /* |
Antonio Nino Diaz | 7c1812e | 2018-06-27 14:59:22 +0100 | [diff] [blame] | 75 | * For EL0 and EL1: |
| 76 | * - In AArch64 PXN and UXN can be set independently but in |
| 77 | * AArch32 there is no UXN (XN affects both privilege levels). |
| 78 | * For consistency, we set them simultaneously in both cases. |
| 79 | * - RO and RW permissions must be the same in EL1 and EL0. If |
| 80 | * EL0 can access that memory region, so can EL1, with the |
| 81 | * same permissions. |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 82 | */ |
Antonio Nino Diaz | 7c1812e | 2018-06-27 14:59:22 +0100 | [diff] [blame] | 83 | #if ENABLE_ASSERTIONS |
| 84 | uint64_t xn_mask = xlat_arch_regime_get_xn_desc(EL1_EL0_REGIME); |
| 85 | uint64_t xn_perm = desc & xn_mask; |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 86 | |
Antonio Nino Diaz | 7c1812e | 2018-06-27 14:59:22 +0100 | [diff] [blame] | 87 | assert((xn_perm == xn_mask) || (xn_perm == 0ULL)); |
| 88 | #endif |
Antonio Nino Diaz | 00086e3 | 2018-08-16 16:46:06 +0100 | [diff] [blame] | 89 | printf(((desc & LOWER_ATTRS(AP_RO)) != 0ULL) ? "-RO" : "-RW"); |
Antonio Nino Diaz | 7c1812e | 2018-06-27 14:59:22 +0100 | [diff] [blame] | 90 | /* Only check one of PXN and UXN, the other one is the same. */ |
Antonio Nino Diaz | 00086e3 | 2018-08-16 16:46:06 +0100 | [diff] [blame] | 91 | printf(((desc & UPPER_ATTRS(PXN)) != 0ULL) ? "-XN" : "-EXEC"); |
Antonio Nino Diaz | 7c1812e | 2018-06-27 14:59:22 +0100 | [diff] [blame] | 92 | /* |
| 93 | * Privileged regions can only be accessed from EL1, user |
| 94 | * regions can be accessed from EL1 and EL0. |
| 95 | */ |
Antonio Nino Diaz | 00086e3 | 2018-08-16 16:46:06 +0100 | [diff] [blame] | 96 | printf(((desc & LOWER_ATTRS(AP_ACCESS_UNPRIVILEGED)) != 0ULL) |
Antonio Nino Diaz | 7c1812e | 2018-06-27 14:59:22 +0100 | [diff] [blame] | 97 | ? "-USER" : "-PRIV"); |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 98 | } |
| 99 | |
Zelalem Aweke | 173c6a2 | 2021-07-08 17:23:04 -0500 | [diff] [blame] | 100 | #if ENABLE_RME |
| 101 | switch (desc & LOWER_ATTRS(EL3_S1_NSE | NS)) { |
| 102 | case 0ULL: |
| 103 | printf("-S"); |
| 104 | break; |
| 105 | case LOWER_ATTRS(NS): |
| 106 | printf("-NS"); |
| 107 | break; |
| 108 | case LOWER_ATTRS(EL3_S1_NSE): |
| 109 | printf("-RT"); |
| 110 | break; |
| 111 | default: /* LOWER_ATTRS(EL3_S1_NSE | NS) */ |
| 112 | printf("-RL"); |
| 113 | } |
| 114 | #else |
Antonio Nino Diaz | 00086e3 | 2018-08-16 16:46:06 +0100 | [diff] [blame] | 115 | printf(((LOWER_ATTRS(NS) & desc) != 0ULL) ? "-NS" : "-S"); |
Zelalem Aweke | 173c6a2 | 2021-07-08 17:23:04 -0500 | [diff] [blame] | 116 | #endif |
Alexei Fedorov | 90f2e88 | 2019-05-24 12:17:09 +0100 | [diff] [blame] | 117 | |
Julius Werner | 8e0ef0f | 2019-07-09 14:02:43 -0700 | [diff] [blame] | 118 | #ifdef __aarch64__ |
Alexei Fedorov | 90f2e88 | 2019-05-24 12:17:09 +0100 | [diff] [blame] | 119 | /* Check Guarded Page bit */ |
| 120 | if ((desc & GP) != 0ULL) { |
| 121 | printf("-GP"); |
| 122 | } |
| 123 | #endif |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static const char * const level_spacers[] = { |
| 127 | "[LV0] ", |
| 128 | " [LV1] ", |
| 129 | " [LV2] ", |
| 130 | " [LV3] " |
| 131 | }; |
| 132 | |
| 133 | static const char *invalid_descriptors_ommited = |
| 134 | "%s(%d invalid descriptors omitted)\n"; |
| 135 | |
| 136 | /* |
Antonio Nino Diaz | ff93d44 | 2019-03-19 14:12:09 +0000 | [diff] [blame] | 137 | * Recursive function that reads the translation tables passed as an argument |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 138 | * and prints their status. |
| 139 | */ |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 140 | static void xlat_tables_print_internal(xlat_ctx_t *ctx, uintptr_t table_base_va, |
| 141 | const uint64_t *table_base, unsigned int table_entries, |
| 142 | unsigned int level) |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 143 | { |
| 144 | assert(level <= XLAT_TABLE_LEVEL_MAX); |
| 145 | |
Antonio Nino Diaz | ff93d44 | 2019-03-19 14:12:09 +0000 | [diff] [blame] | 146 | uint64_t desc; |
David Pu | 36e27b8 | 2019-02-25 10:52:41 -0800 | [diff] [blame] | 147 | uintptr_t table_idx_va = table_base_va; |
Antonio Nino Diaz | ff93d44 | 2019-03-19 14:12:09 +0000 | [diff] [blame] | 148 | unsigned int table_idx = 0U; |
| 149 | size_t level_size = XLAT_BLOCK_SIZE(level); |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 150 | |
| 151 | /* |
| 152 | * Keep track of how many invalid descriptors are counted in a row. |
| 153 | * Whenever multiple invalid descriptors are found, only the first one |
| 154 | * is printed, and a line is added to inform about how many descriptors |
| 155 | * have been omitted. |
| 156 | */ |
| 157 | int invalid_row_count = 0; |
| 158 | |
Antonio Nino Diaz | ff93d44 | 2019-03-19 14:12:09 +0000 | [diff] [blame] | 159 | while (table_idx < table_entries) { |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 160 | |
Antonio Nino Diaz | ff93d44 | 2019-03-19 14:12:09 +0000 | [diff] [blame] | 161 | desc = table_base[table_idx]; |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 162 | |
Antonio Nino Diaz | ff93d44 | 2019-03-19 14:12:09 +0000 | [diff] [blame] | 163 | if ((desc & DESC_MASK) == INVALID_DESC) { |
| 164 | |
| 165 | if (invalid_row_count == 0) { |
| 166 | printf("%sVA:0x%lx size:0x%zx\n", |
| 167 | level_spacers[level], |
| 168 | table_idx_va, level_size); |
David Pu | 36e27b8 | 2019-02-25 10:52:41 -0800 | [diff] [blame] | 169 | } |
Antonio Nino Diaz | ff93d44 | 2019-03-19 14:12:09 +0000 | [diff] [blame] | 170 | invalid_row_count++; |
| 171 | |
David Pu | 36e27b8 | 2019-02-25 10:52:41 -0800 | [diff] [blame] | 172 | } else { |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 173 | |
Antonio Nino Diaz | ff93d44 | 2019-03-19 14:12:09 +0000 | [diff] [blame] | 174 | if (invalid_row_count > 1) { |
| 175 | printf(invalid_descriptors_ommited, |
| 176 | level_spacers[level], |
| 177 | invalid_row_count - 1); |
| 178 | } |
| 179 | invalid_row_count = 0; |
| 180 | |
| 181 | /* |
| 182 | * Check if this is a table or a block. Tables are only |
| 183 | * allowed in levels other than 3, but DESC_PAGE has the |
| 184 | * same value as DESC_TABLE, so we need to check. |
| 185 | */ |
| 186 | if (((desc & DESC_MASK) == TABLE_DESC) && |
| 187 | (level < XLAT_TABLE_LEVEL_MAX)) { |
David Pu | 36e27b8 | 2019-02-25 10:52:41 -0800 | [diff] [blame] | 188 | /* |
Antonio Nino Diaz | ff93d44 | 2019-03-19 14:12:09 +0000 | [diff] [blame] | 189 | * Do not print any PA for a table descriptor, |
| 190 | * as it doesn't directly map physical memory |
| 191 | * but instead points to the next translation |
| 192 | * table in the translation table walk. |
David Pu | 36e27b8 | 2019-02-25 10:52:41 -0800 | [diff] [blame] | 193 | */ |
Antonio Nino Diaz | ff93d44 | 2019-03-19 14:12:09 +0000 | [diff] [blame] | 194 | printf("%sVA:0x%lx size:0x%zx\n", |
| 195 | level_spacers[level], |
| 196 | table_idx_va, level_size); |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 197 | |
Antonio Nino Diaz | ff93d44 | 2019-03-19 14:12:09 +0000 | [diff] [blame] | 198 | uintptr_t addr_inner = desc & TABLE_ADDR_MASK; |
David Pu | 36e27b8 | 2019-02-25 10:52:41 -0800 | [diff] [blame] | 199 | |
Antonio Nino Diaz | ff93d44 | 2019-03-19 14:12:09 +0000 | [diff] [blame] | 200 | xlat_tables_print_internal(ctx, table_idx_va, |
| 201 | (uint64_t *)addr_inner, |
| 202 | XLAT_TABLE_ENTRIES, level + 1U); |
| 203 | } else { |
Javier Almansa Sobrino | 2a8af48 | 2021-11-25 09:29:27 +0000 | [diff] [blame] | 204 | printf("%sVA:0x%lx PA:0x%" PRIx64 " size:0x%zx ", |
Antonio Nino Diaz | ff93d44 | 2019-03-19 14:12:09 +0000 | [diff] [blame] | 205 | level_spacers[level], table_idx_va, |
| 206 | (uint64_t)(desc & TABLE_ADDR_MASK), |
| 207 | level_size); |
| 208 | xlat_desc_print(ctx, desc); |
| 209 | printf("\n"); |
David Pu | 36e27b8 | 2019-02-25 10:52:41 -0800 | [diff] [blame] | 210 | } |
| 211 | } |
Antonio Nino Diaz | ff93d44 | 2019-03-19 14:12:09 +0000 | [diff] [blame] | 212 | |
| 213 | table_idx++; |
| 214 | table_idx_va += level_size; |
| 215 | } |
| 216 | |
| 217 | if (invalid_row_count > 1) { |
| 218 | printf(invalid_descriptors_ommited, |
| 219 | level_spacers[level], invalid_row_count - 1); |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | |
| 223 | void xlat_tables_print(xlat_ctx_t *ctx) |
| 224 | { |
| 225 | const char *xlat_regime_str; |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 226 | int used_page_tables; |
| 227 | |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 228 | if (ctx->xlat_regime == EL1_EL0_REGIME) { |
| 229 | xlat_regime_str = "1&0"; |
Antonio Nino Diaz | 128de8d | 2018-08-07 19:59:49 +0100 | [diff] [blame] | 230 | } else if (ctx->xlat_regime == EL2_REGIME) { |
| 231 | xlat_regime_str = "2"; |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 232 | } else { |
| 233 | assert(ctx->xlat_regime == EL3_REGIME); |
| 234 | xlat_regime_str = "3"; |
| 235 | } |
| 236 | VERBOSE("Translation tables state:\n"); |
| 237 | VERBOSE(" Xlat regime: EL%s\n", xlat_regime_str); |
| 238 | VERBOSE(" Max allowed PA: 0x%llx\n", ctx->pa_max_address); |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 239 | VERBOSE(" Max allowed VA: 0x%lx\n", ctx->va_max_address); |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 240 | VERBOSE(" Max mapped PA: 0x%llx\n", ctx->max_pa); |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 241 | VERBOSE(" Max mapped VA: 0x%lx\n", ctx->max_va); |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 242 | |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 243 | VERBOSE(" Initial lookup level: %u\n", ctx->base_level); |
| 244 | VERBOSE(" Entries @initial lookup level: %u\n", |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 245 | ctx->base_table_entries); |
| 246 | |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 247 | #if PLAT_XLAT_TABLES_DYNAMIC |
| 248 | used_page_tables = 0; |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 249 | for (int i = 0; i < ctx->tables_num; ++i) { |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 250 | if (ctx->tables_mapped_regions[i] != 0) |
| 251 | ++used_page_tables; |
| 252 | } |
| 253 | #else |
| 254 | used_page_tables = ctx->next_table; |
| 255 | #endif |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 256 | VERBOSE(" Used %d sub-tables out of %d (spare: %d)\n", |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 257 | used_page_tables, ctx->tables_num, |
| 258 | ctx->tables_num - used_page_tables); |
| 259 | |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 260 | xlat_tables_print_internal(ctx, 0U, ctx->base_table, |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 261 | ctx->base_table_entries, ctx->base_level); |
| 262 | } |
| 263 | |
| 264 | #endif /* LOG_LEVEL >= LOG_LEVEL_VERBOSE */ |
| 265 | |
| 266 | /* |
| 267 | * Do a translation table walk to find the block or page descriptor that maps |
| 268 | * virtual_addr. |
| 269 | * |
| 270 | * On success, return the address of the descriptor within the translation |
| 271 | * table. Its lookup level is stored in '*out_level'. |
| 272 | * On error, return NULL. |
| 273 | * |
| 274 | * xlat_table_base |
| 275 | * Base address for the initial lookup level. |
| 276 | * xlat_table_base_entries |
| 277 | * Number of entries in the translation table for the initial lookup level. |
| 278 | * virt_addr_space_size |
| 279 | * Size in bytes of the virtual address space. |
| 280 | */ |
| 281 | static uint64_t *find_xlat_table_entry(uintptr_t virtual_addr, |
| 282 | void *xlat_table_base, |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 283 | unsigned int xlat_table_base_entries, |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 284 | unsigned long long virt_addr_space_size, |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 285 | unsigned int *out_level) |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 286 | { |
| 287 | unsigned int start_level; |
| 288 | uint64_t *table; |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 289 | unsigned int entries; |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 290 | |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 291 | start_level = GET_XLAT_TABLE_LEVEL_BASE(virt_addr_space_size); |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 292 | |
| 293 | table = xlat_table_base; |
| 294 | entries = xlat_table_base_entries; |
| 295 | |
| 296 | for (unsigned int level = start_level; |
| 297 | level <= XLAT_TABLE_LEVEL_MAX; |
| 298 | ++level) { |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 299 | uint64_t idx, desc, desc_type; |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 300 | |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 301 | idx = XLAT_TABLE_IDX(virtual_addr, level); |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 302 | if (idx >= entries) { |
Antonio Nino Diaz | bd0aff6 | 2018-07-02 09:26:51 +0100 | [diff] [blame] | 303 | WARN("Missing xlat table entry at address 0x%lx\n", |
| 304 | virtual_addr); |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 305 | return NULL; |
| 306 | } |
| 307 | |
| 308 | desc = table[idx]; |
| 309 | desc_type = desc & DESC_MASK; |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 310 | |
| 311 | if (desc_type == INVALID_DESC) { |
| 312 | VERBOSE("Invalid entry (memory not mapped)\n"); |
| 313 | return NULL; |
| 314 | } |
| 315 | |
| 316 | if (level == XLAT_TABLE_LEVEL_MAX) { |
| 317 | /* |
Antonio Nino Diaz | bd0aff6 | 2018-07-02 09:26:51 +0100 | [diff] [blame] | 318 | * Only page descriptors allowed at the final lookup |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 319 | * level. |
| 320 | */ |
| 321 | assert(desc_type == PAGE_DESC); |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 322 | *out_level = level; |
| 323 | return &table[idx]; |
| 324 | } |
| 325 | |
| 326 | if (desc_type == BLOCK_DESC) { |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 327 | *out_level = level; |
| 328 | return &table[idx]; |
| 329 | } |
| 330 | |
| 331 | assert(desc_type == TABLE_DESC); |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 332 | table = (uint64_t *)(uintptr_t)(desc & TABLE_ADDR_MASK); |
| 333 | entries = XLAT_TABLE_ENTRIES; |
| 334 | } |
| 335 | |
| 336 | /* |
| 337 | * This shouldn't be reached, the translation table walk should end at |
| 338 | * most at level XLAT_TABLE_LEVEL_MAX and return from inside the loop. |
| 339 | */ |
Antonio Nino Diaz | 5c97bd1 | 2018-08-02 09:57:29 +0100 | [diff] [blame] | 340 | assert(false); |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 341 | |
| 342 | return NULL; |
| 343 | } |
| 344 | |
| 345 | |
Antonio Nino Diaz | 6c4c9ee | 2018-08-05 15:34:10 +0100 | [diff] [blame] | 346 | static int xlat_get_mem_attributes_internal(const xlat_ctx_t *ctx, |
| 347 | uintptr_t base_va, uint32_t *attributes, uint64_t **table_entry, |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 348 | unsigned long long *addr_pa, unsigned int *table_level) |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 349 | { |
| 350 | uint64_t *entry; |
| 351 | uint64_t desc; |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 352 | unsigned int level; |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 353 | unsigned long long virt_addr_space_size; |
| 354 | |
| 355 | /* |
| 356 | * Sanity-check arguments. |
| 357 | */ |
| 358 | assert(ctx != NULL); |
Antonio Nino Diaz | 5c97bd1 | 2018-08-02 09:57:29 +0100 | [diff] [blame] | 359 | assert(ctx->initialized); |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 360 | assert((ctx->xlat_regime == EL1_EL0_REGIME) || |
Antonio Nino Diaz | 128de8d | 2018-08-07 19:59:49 +0100 | [diff] [blame] | 361 | (ctx->xlat_regime == EL2_REGIME) || |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 362 | (ctx->xlat_regime == EL3_REGIME)); |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 363 | |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 364 | virt_addr_space_size = (unsigned long long)ctx->va_max_address + 1ULL; |
| 365 | assert(virt_addr_space_size > 0U); |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 366 | |
| 367 | entry = find_xlat_table_entry(base_va, |
| 368 | ctx->base_table, |
| 369 | ctx->base_table_entries, |
| 370 | virt_addr_space_size, |
| 371 | &level); |
| 372 | if (entry == NULL) { |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 373 | WARN("Address 0x%lx is not mapped.\n", base_va); |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 374 | return -EINVAL; |
| 375 | } |
| 376 | |
| 377 | if (addr_pa != NULL) { |
| 378 | *addr_pa = *entry & TABLE_ADDR_MASK; |
| 379 | } |
| 380 | |
| 381 | if (table_entry != NULL) { |
| 382 | *table_entry = entry; |
| 383 | } |
| 384 | |
| 385 | if (table_level != NULL) { |
| 386 | *table_level = level; |
| 387 | } |
| 388 | |
| 389 | desc = *entry; |
| 390 | |
| 391 | #if LOG_LEVEL >= LOG_LEVEL_VERBOSE |
| 392 | VERBOSE("Attributes: "); |
| 393 | xlat_desc_print(ctx, desc); |
Antonio Nino Diaz | 00086e3 | 2018-08-16 16:46:06 +0100 | [diff] [blame] | 394 | printf("\n"); |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 395 | #endif /* LOG_LEVEL >= LOG_LEVEL_VERBOSE */ |
| 396 | |
| 397 | assert(attributes != NULL); |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 398 | *attributes = 0U; |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 399 | |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 400 | uint64_t attr_index = (desc >> ATTR_INDEX_SHIFT) & ATTR_INDEX_MASK; |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 401 | |
| 402 | if (attr_index == ATTR_IWBWA_OWBWA_NTR_INDEX) { |
| 403 | *attributes |= MT_MEMORY; |
| 404 | } else if (attr_index == ATTR_NON_CACHEABLE_INDEX) { |
| 405 | *attributes |= MT_NON_CACHEABLE; |
| 406 | } else { |
| 407 | assert(attr_index == ATTR_DEVICE_INDEX); |
| 408 | *attributes |= MT_DEVICE; |
| 409 | } |
| 410 | |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 411 | uint64_t ap2_bit = (desc >> AP2_SHIFT) & 1U; |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 412 | |
| 413 | if (ap2_bit == AP2_RW) |
| 414 | *attributes |= MT_RW; |
| 415 | |
| 416 | if (ctx->xlat_regime == EL1_EL0_REGIME) { |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 417 | uint64_t ap1_bit = (desc >> AP1_SHIFT) & 1U; |
| 418 | |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 419 | if (ap1_bit == AP1_ACCESS_UNPRIVILEGED) |
| 420 | *attributes |= MT_USER; |
| 421 | } |
| 422 | |
Manish V Badarkhe | 468c3d4 | 2024-06-24 14:35:46 +0100 | [diff] [blame] | 423 | uint64_t ns_bit = (desc >> NS_SHIFT) & 1ULL; |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 424 | |
Manish V Badarkhe | 468c3d4 | 2024-06-24 14:35:46 +0100 | [diff] [blame] | 425 | #if ENABLE_RME |
| 426 | uint64_t nse_bit = (desc >> NSE_SHIFT) & 1ULL; |
| 427 | uint32_t sec_state = (uint32_t)(ns_bit | (nse_bit << 1ULL)); |
| 428 | |
| 429 | /* |
| 430 | * ========================================================= |
| 431 | * NSE NS | Output PA space |
| 432 | * ========================================================= |
| 433 | * 0 0 | Secure (if S-EL2 is present, else invalid) |
| 434 | * 0 1 | Non-secure |
| 435 | * 1 0 | Root |
| 436 | * 1 1 | Realm |
| 437 | *========================================================== |
| 438 | */ |
| 439 | switch (sec_state) { |
| 440 | case 0U: |
| 441 | /* |
| 442 | * We expect to get Secure mapping on an RME system only if |
| 443 | * S-EL2 is enabled. |
| 444 | * Hence panic() if we hit the case without EEL2 being enabled. |
| 445 | */ |
| 446 | if ((read_scr_el3() & SCR_EEL2_BIT) == 0ULL) { |
| 447 | ERROR("A secure descriptor is not supported when" |
| 448 | "FEAT_RME is implemented and FEAT_SEL2 is" |
| 449 | "not enabled\n"); |
| 450 | panic(); |
| 451 | } else { |
| 452 | *attributes |= MT_SECURE; |
| 453 | } |
| 454 | break; |
| 455 | case 1U: |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 456 | *attributes |= MT_NS; |
Manish V Badarkhe | 468c3d4 | 2024-06-24 14:35:46 +0100 | [diff] [blame] | 457 | break; |
| 458 | case 2U: |
| 459 | *attributes |= MT_ROOT; |
| 460 | break; |
| 461 | case 3U: |
| 462 | *attributes |= MT_REALM; |
| 463 | break; |
| 464 | default: |
| 465 | /* unreachable code */ |
| 466 | assert(false); |
| 467 | break; |
| 468 | } |
| 469 | #else /* !ENABLE_RME */ |
| 470 | if (ns_bit == 1ULL) { |
| 471 | *attributes |= MT_NS; |
| 472 | } else { |
| 473 | *attributes |= MT_SECURE; |
| 474 | } |
| 475 | #endif /* ENABLE_RME */ |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 476 | |
| 477 | uint64_t xn_mask = xlat_arch_regime_get_xn_desc(ctx->xlat_regime); |
| 478 | |
| 479 | if ((desc & xn_mask) == xn_mask) { |
| 480 | *attributes |= MT_EXECUTE_NEVER; |
| 481 | } else { |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 482 | assert((desc & xn_mask) == 0U); |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | |
Antonio Nino Diaz | 6c4c9ee | 2018-08-05 15:34:10 +0100 | [diff] [blame] | 489 | int xlat_get_mem_attributes_ctx(const xlat_ctx_t *ctx, uintptr_t base_va, |
| 490 | uint32_t *attr) |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 491 | { |
Antonio Nino Diaz | 6c4c9ee | 2018-08-05 15:34:10 +0100 | [diff] [blame] | 492 | return xlat_get_mem_attributes_internal(ctx, base_va, attr, |
| 493 | NULL, NULL, NULL); |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | |
Antonio Nino Diaz | 6c4c9ee | 2018-08-05 15:34:10 +0100 | [diff] [blame] | 497 | int xlat_change_mem_attributes_ctx(const xlat_ctx_t *ctx, uintptr_t base_va, |
| 498 | size_t size, uint32_t attr) |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 499 | { |
| 500 | /* Note: This implementation isn't optimized. */ |
| 501 | |
| 502 | assert(ctx != NULL); |
Antonio Nino Diaz | 5c97bd1 | 2018-08-02 09:57:29 +0100 | [diff] [blame] | 503 | assert(ctx->initialized); |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 504 | |
| 505 | unsigned long long virt_addr_space_size = |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 506 | (unsigned long long)ctx->va_max_address + 1U; |
| 507 | assert(virt_addr_space_size > 0U); |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 508 | |
| 509 | if (!IS_PAGE_ALIGNED(base_va)) { |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 510 | WARN("%s: Address 0x%lx is not aligned on a page boundary.\n", |
| 511 | __func__, base_va); |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 512 | return -EINVAL; |
| 513 | } |
| 514 | |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 515 | if (size == 0U) { |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 516 | WARN("%s: Size is 0.\n", __func__); |
| 517 | return -EINVAL; |
| 518 | } |
| 519 | |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 520 | if ((size % PAGE_SIZE) != 0U) { |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 521 | WARN("%s: Size 0x%zx is not a multiple of a page size.\n", |
| 522 | __func__, size); |
| 523 | return -EINVAL; |
| 524 | } |
| 525 | |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 526 | if (((attr & MT_EXECUTE_NEVER) == 0U) && ((attr & MT_RW) != 0U)) { |
Antonio Nino Diaz | bd0aff6 | 2018-07-02 09:26:51 +0100 | [diff] [blame] | 527 | WARN("%s: Mapping memory as read-write and executable not allowed.\n", |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 528 | __func__); |
| 529 | return -EINVAL; |
| 530 | } |
| 531 | |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 532 | size_t pages_count = size / PAGE_SIZE; |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 533 | |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 534 | VERBOSE("Changing memory attributes of %zu pages starting from address 0x%lx...\n", |
| 535 | pages_count, base_va); |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 536 | |
| 537 | uintptr_t base_va_original = base_va; |
| 538 | |
| 539 | /* |
| 540 | * Sanity checks. |
| 541 | */ |
Jimmy Brisson | ed20207 | 2020-08-04 16:18:52 -0500 | [diff] [blame] | 542 | for (unsigned int i = 0U; i < pages_count; ++i) { |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 543 | const uint64_t *entry; |
| 544 | uint64_t desc, attr_index; |
| 545 | unsigned int level; |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 546 | |
| 547 | entry = find_xlat_table_entry(base_va, |
| 548 | ctx->base_table, |
| 549 | ctx->base_table_entries, |
| 550 | virt_addr_space_size, |
| 551 | &level); |
| 552 | if (entry == NULL) { |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 553 | WARN("Address 0x%lx is not mapped.\n", base_va); |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 554 | return -EINVAL; |
| 555 | } |
| 556 | |
| 557 | desc = *entry; |
| 558 | |
| 559 | /* |
| 560 | * Check that all the required pages are mapped at page |
| 561 | * granularity. |
| 562 | */ |
| 563 | if (((desc & DESC_MASK) != PAGE_DESC) || |
| 564 | (level != XLAT_TABLE_LEVEL_MAX)) { |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 565 | WARN("Address 0x%lx is not mapped at the right granularity.\n", |
| 566 | base_va); |
Jimmy Brisson | ed20207 | 2020-08-04 16:18:52 -0500 | [diff] [blame] | 567 | WARN("Granularity is 0x%lx, should be 0x%lx.\n", |
| 568 | XLAT_BLOCK_SIZE(level), PAGE_SIZE); |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 569 | return -EINVAL; |
| 570 | } |
| 571 | |
| 572 | /* |
| 573 | * If the region type is device, it shouldn't be executable. |
| 574 | */ |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 575 | attr_index = (desc >> ATTR_INDEX_SHIFT) & ATTR_INDEX_MASK; |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 576 | if (attr_index == ATTR_DEVICE_INDEX) { |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 577 | if ((attr & MT_EXECUTE_NEVER) == 0U) { |
| 578 | WARN("Setting device memory as executable at address 0x%lx.", |
| 579 | base_va); |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 580 | return -EINVAL; |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | base_va += PAGE_SIZE; |
| 585 | } |
| 586 | |
| 587 | /* Restore original value. */ |
| 588 | base_va = base_va_original; |
| 589 | |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 590 | for (unsigned int i = 0U; i < pages_count; ++i) { |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 591 | |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 592 | uint32_t old_attr = 0U, new_attr; |
| 593 | uint64_t *entry = NULL; |
| 594 | unsigned int level = 0U; |
| 595 | unsigned long long addr_pa = 0ULL; |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 596 | |
Antonio Nino Diaz | 6c4c9ee | 2018-08-05 15:34:10 +0100 | [diff] [blame] | 597 | (void) xlat_get_mem_attributes_internal(ctx, base_va, &old_attr, |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 598 | &entry, &addr_pa, &level); |
| 599 | |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 600 | /* |
| 601 | * From attr, only MT_RO/MT_RW, MT_EXECUTE/MT_EXECUTE_NEVER and |
| 602 | * MT_USER/MT_PRIVILEGED are taken into account. Any other |
| 603 | * information is ignored. |
| 604 | */ |
| 605 | |
| 606 | /* Clean the old attributes so that they can be rebuilt. */ |
Antonio Nino Diaz | bd0aff6 | 2018-07-02 09:26:51 +0100 | [diff] [blame] | 607 | new_attr = old_attr & ~(MT_RW | MT_EXECUTE_NEVER | MT_USER); |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 608 | |
| 609 | /* |
| 610 | * Update attributes, but filter out the ones this function |
| 611 | * isn't allowed to change. |
| 612 | */ |
Antonio Nino Diaz | bd0aff6 | 2018-07-02 09:26:51 +0100 | [diff] [blame] | 613 | new_attr |= attr & (MT_RW | MT_EXECUTE_NEVER | MT_USER); |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 614 | |
| 615 | /* |
| 616 | * The break-before-make sequence requires writing an invalid |
| 617 | * descriptor and making sure that the system sees the change |
| 618 | * before writing the new descriptor. |
| 619 | */ |
| 620 | *entry = INVALID_DESC; |
Artsem Artsemenka | bce728f | 2019-10-17 13:51:27 +0100 | [diff] [blame] | 621 | #if !HW_ASSISTED_COHERENCY |
Antonio Nino Diaz | 37a5efa | 2018-08-07 12:47:12 +0100 | [diff] [blame] | 622 | dccvac((uintptr_t)entry); |
| 623 | #endif |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 624 | /* Invalidate any cached copy of this mapping in the TLBs. */ |
Antonio Nino Diaz | ad5dc7f | 2018-07-11 09:46:45 +0100 | [diff] [blame] | 625 | xlat_arch_tlbi_va(base_va, ctx->xlat_regime); |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 626 | |
| 627 | /* Ensure completion of the invalidation. */ |
| 628 | xlat_arch_tlbi_va_sync(); |
| 629 | |
| 630 | /* Write new descriptor */ |
| 631 | *entry = xlat_desc(ctx, new_attr, addr_pa, level); |
Artsem Artsemenka | bce728f | 2019-10-17 13:51:27 +0100 | [diff] [blame] | 632 | #if !HW_ASSISTED_COHERENCY |
Antonio Nino Diaz | 37a5efa | 2018-08-07 12:47:12 +0100 | [diff] [blame] | 633 | dccvac((uintptr_t)entry); |
| 634 | #endif |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 635 | base_va += PAGE_SIZE; |
| 636 | } |
| 637 | |
Elyes Haouas | 2be03c0 | 2023-02-13 09:14:48 +0100 | [diff] [blame] | 638 | /* Ensure that the last descriptor written is seen by the system. */ |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 639 | dsbish(); |
| 640 | |
| 641 | return 0; |
| 642 | } |