blob: df17386429e941ad515ff621e0c2a98c2a8db8c8 [file] [log] [blame]
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +01001/*
Zelalem Aweke173c6a22021-07-08 17:23:04 -05002 * Copyright (c) 2017-2021, Arm Limited and Contributors. All rights reserved.
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +01007#include <assert.h>
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +01008#include <errno.h>
Antonio Nino Diaz5c97bd12018-08-02 09:57:29 +01009#include <stdbool.h>
Antonio Nino Diaz4b32e622018-08-16 16:52:57 +010010#include <stdint.h>
Antonio Nino Diaz00086e32018-08-16 16:46:06 +010011#include <stdio.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000012
13#include <platform_def.h>
14
15#include <arch_helpers.h>
16#include <common/debug.h>
17#include <lib/utils_def.h>
18#include <lib/xlat_tables/xlat_tables_defs.h>
19#include <lib/xlat_tables/xlat_tables_v2.h>
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +010020
21#include "xlat_tables_private.h"
22
23#if LOG_LEVEL < LOG_LEVEL_VERBOSE
24
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +010025void xlat_mmap_print(__unused const mmap_region_t *mmap)
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +010026{
27 /* Empty */
28}
29
30void xlat_tables_print(__unused xlat_ctx_t *ctx)
31{
32 /* Empty */
33}
34
35#else /* if LOG_LEVEL >= LOG_LEVEL_VERBOSE */
36
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +010037void xlat_mmap_print(const mmap_region_t *mmap)
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +010038{
Antonio Nino Diaz00086e32018-08-16 16:46:06 +010039 printf("mmap:\n");
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +010040 const mmap_region_t *mm = mmap;
41
42 while (mm->size != 0U) {
Antonio Nino Diaz00086e32018-08-16 16:46:06 +010043 printf(" VA:0x%lx PA:0x%llx size:0x%zx attr:0x%x granularity:0x%zx\n",
44 mm->base_va, mm->base_pa, mm->size, mm->attr,
45 mm->granularity);
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +010046 ++mm;
47 };
Antonio Nino Diaz00086e32018-08-16 16:46:06 +010048 printf("\n");
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +010049}
50
51/* Print the attributes of the specified block descriptor. */
52static void xlat_desc_print(const xlat_ctx_t *ctx, uint64_t desc)
53{
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +010054 uint64_t mem_type_index = ATTR_INDEX_GET(desc);
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +010055 int xlat_regime = ctx->xlat_regime;
56
57 if (mem_type_index == ATTR_IWBWA_OWBWA_NTR_INDEX) {
Antonio Nino Diaz00086e32018-08-16 16:46:06 +010058 printf("MEM");
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +010059 } else if (mem_type_index == ATTR_NON_CACHEABLE_INDEX) {
Antonio Nino Diaz00086e32018-08-16 16:46:06 +010060 printf("NC");
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +010061 } else {
62 assert(mem_type_index == ATTR_DEVICE_INDEX);
Antonio Nino Diaz00086e32018-08-16 16:46:06 +010063 printf("DEV");
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +010064 }
65
Antonio Nino Diaz128de8d2018-08-07 19:59:49 +010066 if ((xlat_regime == EL3_REGIME) || (xlat_regime == EL2_REGIME)) {
67 /* For EL3 and EL2 only check the AP[2] and XN bits. */
Antonio Nino Diaz00086e32018-08-16 16:46:06 +010068 printf(((desc & LOWER_ATTRS(AP_RO)) != 0ULL) ? "-RO" : "-RW");
69 printf(((desc & UPPER_ATTRS(XN)) != 0ULL) ? "-XN" : "-EXEC");
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +010070 } else {
Antonio Nino Diaz7c1812e2018-06-27 14:59:22 +010071 assert(xlat_regime == EL1_EL0_REGIME);
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +010072 /*
Antonio Nino Diaz7c1812e2018-06-27 14:59:22 +010073 * For EL0 and EL1:
74 * - In AArch64 PXN and UXN can be set independently but in
75 * AArch32 there is no UXN (XN affects both privilege levels).
76 * For consistency, we set them simultaneously in both cases.
77 * - RO and RW permissions must be the same in EL1 and EL0. If
78 * EL0 can access that memory region, so can EL1, with the
79 * same permissions.
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +010080 */
Antonio Nino Diaz7c1812e2018-06-27 14:59:22 +010081#if ENABLE_ASSERTIONS
82 uint64_t xn_mask = xlat_arch_regime_get_xn_desc(EL1_EL0_REGIME);
83 uint64_t xn_perm = desc & xn_mask;
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +010084
Antonio Nino Diaz7c1812e2018-06-27 14:59:22 +010085 assert((xn_perm == xn_mask) || (xn_perm == 0ULL));
86#endif
Antonio Nino Diaz00086e32018-08-16 16:46:06 +010087 printf(((desc & LOWER_ATTRS(AP_RO)) != 0ULL) ? "-RO" : "-RW");
Antonio Nino Diaz7c1812e2018-06-27 14:59:22 +010088 /* Only check one of PXN and UXN, the other one is the same. */
Antonio Nino Diaz00086e32018-08-16 16:46:06 +010089 printf(((desc & UPPER_ATTRS(PXN)) != 0ULL) ? "-XN" : "-EXEC");
Antonio Nino Diaz7c1812e2018-06-27 14:59:22 +010090 /*
91 * Privileged regions can only be accessed from EL1, user
92 * regions can be accessed from EL1 and EL0.
93 */
Antonio Nino Diaz00086e32018-08-16 16:46:06 +010094 printf(((desc & LOWER_ATTRS(AP_ACCESS_UNPRIVILEGED)) != 0ULL)
Antonio Nino Diaz7c1812e2018-06-27 14:59:22 +010095 ? "-USER" : "-PRIV");
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +010096 }
97
Zelalem Aweke173c6a22021-07-08 17:23:04 -050098#if ENABLE_RME
99 switch (desc & LOWER_ATTRS(EL3_S1_NSE | NS)) {
100 case 0ULL:
101 printf("-S");
102 break;
103 case LOWER_ATTRS(NS):
104 printf("-NS");
105 break;
106 case LOWER_ATTRS(EL3_S1_NSE):
107 printf("-RT");
108 break;
109 default: /* LOWER_ATTRS(EL3_S1_NSE | NS) */
110 printf("-RL");
111 }
112#else
Antonio Nino Diaz00086e32018-08-16 16:46:06 +0100113 printf(((LOWER_ATTRS(NS) & desc) != 0ULL) ? "-NS" : "-S");
Zelalem Aweke173c6a22021-07-08 17:23:04 -0500114#endif
Alexei Fedorov90f2e882019-05-24 12:17:09 +0100115
Julius Werner8e0ef0f2019-07-09 14:02:43 -0700116#ifdef __aarch64__
Alexei Fedorov90f2e882019-05-24 12:17:09 +0100117 /* Check Guarded Page bit */
118 if ((desc & GP) != 0ULL) {
119 printf("-GP");
120 }
121#endif
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100122}
123
124static const char * const level_spacers[] = {
125 "[LV0] ",
126 " [LV1] ",
127 " [LV2] ",
128 " [LV3] "
129};
130
131static const char *invalid_descriptors_ommited =
132 "%s(%d invalid descriptors omitted)\n";
133
134/*
Antonio Nino Diazff93d442019-03-19 14:12:09 +0000135 * Recursive function that reads the translation tables passed as an argument
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100136 * and prints their status.
137 */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100138static void xlat_tables_print_internal(xlat_ctx_t *ctx, uintptr_t table_base_va,
139 const uint64_t *table_base, unsigned int table_entries,
140 unsigned int level)
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100141{
142 assert(level <= XLAT_TABLE_LEVEL_MAX);
143
Antonio Nino Diazff93d442019-03-19 14:12:09 +0000144 uint64_t desc;
David Pu36e27b82019-02-25 10:52:41 -0800145 uintptr_t table_idx_va = table_base_va;
Antonio Nino Diazff93d442019-03-19 14:12:09 +0000146 unsigned int table_idx = 0U;
147 size_t level_size = XLAT_BLOCK_SIZE(level);
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100148
149 /*
150 * Keep track of how many invalid descriptors are counted in a row.
151 * Whenever multiple invalid descriptors are found, only the first one
152 * is printed, and a line is added to inform about how many descriptors
153 * have been omitted.
154 */
155 int invalid_row_count = 0;
156
Antonio Nino Diazff93d442019-03-19 14:12:09 +0000157 while (table_idx < table_entries) {
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100158
Antonio Nino Diazff93d442019-03-19 14:12:09 +0000159 desc = table_base[table_idx];
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100160
Antonio Nino Diazff93d442019-03-19 14:12:09 +0000161 if ((desc & DESC_MASK) == INVALID_DESC) {
162
163 if (invalid_row_count == 0) {
164 printf("%sVA:0x%lx size:0x%zx\n",
165 level_spacers[level],
166 table_idx_va, level_size);
David Pu36e27b82019-02-25 10:52:41 -0800167 }
Antonio Nino Diazff93d442019-03-19 14:12:09 +0000168 invalid_row_count++;
169
David Pu36e27b82019-02-25 10:52:41 -0800170 } else {
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100171
Antonio Nino Diazff93d442019-03-19 14:12:09 +0000172 if (invalid_row_count > 1) {
173 printf(invalid_descriptors_ommited,
174 level_spacers[level],
175 invalid_row_count - 1);
176 }
177 invalid_row_count = 0;
178
179 /*
180 * Check if this is a table or a block. Tables are only
181 * allowed in levels other than 3, but DESC_PAGE has the
182 * same value as DESC_TABLE, so we need to check.
183 */
184 if (((desc & DESC_MASK) == TABLE_DESC) &&
185 (level < XLAT_TABLE_LEVEL_MAX)) {
David Pu36e27b82019-02-25 10:52:41 -0800186 /*
Antonio Nino Diazff93d442019-03-19 14:12:09 +0000187 * Do not print any PA for a table descriptor,
188 * as it doesn't directly map physical memory
189 * but instead points to the next translation
190 * table in the translation table walk.
David Pu36e27b82019-02-25 10:52:41 -0800191 */
Antonio Nino Diazff93d442019-03-19 14:12:09 +0000192 printf("%sVA:0x%lx size:0x%zx\n",
193 level_spacers[level],
194 table_idx_va, level_size);
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100195
Antonio Nino Diazff93d442019-03-19 14:12:09 +0000196 uintptr_t addr_inner = desc & TABLE_ADDR_MASK;
David Pu36e27b82019-02-25 10:52:41 -0800197
Antonio Nino Diazff93d442019-03-19 14:12:09 +0000198 xlat_tables_print_internal(ctx, table_idx_va,
199 (uint64_t *)addr_inner,
200 XLAT_TABLE_ENTRIES, level + 1U);
201 } else {
202 printf("%sVA:0x%lx PA:0x%llx size:0x%zx ",
203 level_spacers[level], table_idx_va,
204 (uint64_t)(desc & TABLE_ADDR_MASK),
205 level_size);
206 xlat_desc_print(ctx, desc);
207 printf("\n");
David Pu36e27b82019-02-25 10:52:41 -0800208 }
209 }
Antonio Nino Diazff93d442019-03-19 14:12:09 +0000210
211 table_idx++;
212 table_idx_va += level_size;
213 }
214
215 if (invalid_row_count > 1) {
216 printf(invalid_descriptors_ommited,
217 level_spacers[level], invalid_row_count - 1);
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100218 }
219}
220
221void xlat_tables_print(xlat_ctx_t *ctx)
222{
223 const char *xlat_regime_str;
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100224 int used_page_tables;
225
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100226 if (ctx->xlat_regime == EL1_EL0_REGIME) {
227 xlat_regime_str = "1&0";
Antonio Nino Diaz128de8d2018-08-07 19:59:49 +0100228 } else if (ctx->xlat_regime == EL2_REGIME) {
229 xlat_regime_str = "2";
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100230 } else {
231 assert(ctx->xlat_regime == EL3_REGIME);
232 xlat_regime_str = "3";
233 }
234 VERBOSE("Translation tables state:\n");
235 VERBOSE(" Xlat regime: EL%s\n", xlat_regime_str);
236 VERBOSE(" Max allowed PA: 0x%llx\n", ctx->pa_max_address);
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100237 VERBOSE(" Max allowed VA: 0x%lx\n", ctx->va_max_address);
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100238 VERBOSE(" Max mapped PA: 0x%llx\n", ctx->max_pa);
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100239 VERBOSE(" Max mapped VA: 0x%lx\n", ctx->max_va);
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100240
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100241 VERBOSE(" Initial lookup level: %u\n", ctx->base_level);
242 VERBOSE(" Entries @initial lookup level: %u\n",
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100243 ctx->base_table_entries);
244
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100245#if PLAT_XLAT_TABLES_DYNAMIC
246 used_page_tables = 0;
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100247 for (int i = 0; i < ctx->tables_num; ++i) {
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100248 if (ctx->tables_mapped_regions[i] != 0)
249 ++used_page_tables;
250 }
251#else
252 used_page_tables = ctx->next_table;
253#endif
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100254 VERBOSE(" Used %d sub-tables out of %d (spare: %d)\n",
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100255 used_page_tables, ctx->tables_num,
256 ctx->tables_num - used_page_tables);
257
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100258 xlat_tables_print_internal(ctx, 0U, ctx->base_table,
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100259 ctx->base_table_entries, ctx->base_level);
260}
261
262#endif /* LOG_LEVEL >= LOG_LEVEL_VERBOSE */
263
264/*
265 * Do a translation table walk to find the block or page descriptor that maps
266 * virtual_addr.
267 *
268 * On success, return the address of the descriptor within the translation
269 * table. Its lookup level is stored in '*out_level'.
270 * On error, return NULL.
271 *
272 * xlat_table_base
273 * Base address for the initial lookup level.
274 * xlat_table_base_entries
275 * Number of entries in the translation table for the initial lookup level.
276 * virt_addr_space_size
277 * Size in bytes of the virtual address space.
278 */
279static uint64_t *find_xlat_table_entry(uintptr_t virtual_addr,
280 void *xlat_table_base,
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100281 unsigned int xlat_table_base_entries,
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100282 unsigned long long virt_addr_space_size,
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100283 unsigned int *out_level)
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100284{
285 unsigned int start_level;
286 uint64_t *table;
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100287 unsigned int entries;
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100288
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100289 start_level = GET_XLAT_TABLE_LEVEL_BASE(virt_addr_space_size);
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100290
291 table = xlat_table_base;
292 entries = xlat_table_base_entries;
293
294 for (unsigned int level = start_level;
295 level <= XLAT_TABLE_LEVEL_MAX;
296 ++level) {
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100297 uint64_t idx, desc, desc_type;
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100298
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100299 idx = XLAT_TABLE_IDX(virtual_addr, level);
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100300 if (idx >= entries) {
Antonio Nino Diazbd0aff62018-07-02 09:26:51 +0100301 WARN("Missing xlat table entry at address 0x%lx\n",
302 virtual_addr);
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100303 return NULL;
304 }
305
306 desc = table[idx];
307 desc_type = desc & DESC_MASK;
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100308
309 if (desc_type == INVALID_DESC) {
310 VERBOSE("Invalid entry (memory not mapped)\n");
311 return NULL;
312 }
313
314 if (level == XLAT_TABLE_LEVEL_MAX) {
315 /*
Antonio Nino Diazbd0aff62018-07-02 09:26:51 +0100316 * Only page descriptors allowed at the final lookup
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100317 * level.
318 */
319 assert(desc_type == PAGE_DESC);
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100320 *out_level = level;
321 return &table[idx];
322 }
323
324 if (desc_type == BLOCK_DESC) {
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100325 *out_level = level;
326 return &table[idx];
327 }
328
329 assert(desc_type == TABLE_DESC);
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100330 table = (uint64_t *)(uintptr_t)(desc & TABLE_ADDR_MASK);
331 entries = XLAT_TABLE_ENTRIES;
332 }
333
334 /*
335 * This shouldn't be reached, the translation table walk should end at
336 * most at level XLAT_TABLE_LEVEL_MAX and return from inside the loop.
337 */
Antonio Nino Diaz5c97bd12018-08-02 09:57:29 +0100338 assert(false);
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100339
340 return NULL;
341}
342
343
Antonio Nino Diaz6c4c9ee2018-08-05 15:34:10 +0100344static int xlat_get_mem_attributes_internal(const xlat_ctx_t *ctx,
345 uintptr_t base_va, uint32_t *attributes, uint64_t **table_entry,
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100346 unsigned long long *addr_pa, unsigned int *table_level)
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100347{
348 uint64_t *entry;
349 uint64_t desc;
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100350 unsigned int level;
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100351 unsigned long long virt_addr_space_size;
352
353 /*
354 * Sanity-check arguments.
355 */
356 assert(ctx != NULL);
Antonio Nino Diaz5c97bd12018-08-02 09:57:29 +0100357 assert(ctx->initialized);
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100358 assert((ctx->xlat_regime == EL1_EL0_REGIME) ||
Antonio Nino Diaz128de8d2018-08-07 19:59:49 +0100359 (ctx->xlat_regime == EL2_REGIME) ||
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100360 (ctx->xlat_regime == EL3_REGIME));
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100361
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100362 virt_addr_space_size = (unsigned long long)ctx->va_max_address + 1ULL;
363 assert(virt_addr_space_size > 0U);
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100364
365 entry = find_xlat_table_entry(base_va,
366 ctx->base_table,
367 ctx->base_table_entries,
368 virt_addr_space_size,
369 &level);
370 if (entry == NULL) {
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100371 WARN("Address 0x%lx is not mapped.\n", base_va);
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100372 return -EINVAL;
373 }
374
375 if (addr_pa != NULL) {
376 *addr_pa = *entry & TABLE_ADDR_MASK;
377 }
378
379 if (table_entry != NULL) {
380 *table_entry = entry;
381 }
382
383 if (table_level != NULL) {
384 *table_level = level;
385 }
386
387 desc = *entry;
388
389#if LOG_LEVEL >= LOG_LEVEL_VERBOSE
390 VERBOSE("Attributes: ");
391 xlat_desc_print(ctx, desc);
Antonio Nino Diaz00086e32018-08-16 16:46:06 +0100392 printf("\n");
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100393#endif /* LOG_LEVEL >= LOG_LEVEL_VERBOSE */
394
395 assert(attributes != NULL);
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100396 *attributes = 0U;
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100397
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100398 uint64_t attr_index = (desc >> ATTR_INDEX_SHIFT) & ATTR_INDEX_MASK;
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100399
400 if (attr_index == ATTR_IWBWA_OWBWA_NTR_INDEX) {
401 *attributes |= MT_MEMORY;
402 } else if (attr_index == ATTR_NON_CACHEABLE_INDEX) {
403 *attributes |= MT_NON_CACHEABLE;
404 } else {
405 assert(attr_index == ATTR_DEVICE_INDEX);
406 *attributes |= MT_DEVICE;
407 }
408
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100409 uint64_t ap2_bit = (desc >> AP2_SHIFT) & 1U;
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100410
411 if (ap2_bit == AP2_RW)
412 *attributes |= MT_RW;
413
414 if (ctx->xlat_regime == EL1_EL0_REGIME) {
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100415 uint64_t ap1_bit = (desc >> AP1_SHIFT) & 1U;
416
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100417 if (ap1_bit == AP1_ACCESS_UNPRIVILEGED)
418 *attributes |= MT_USER;
419 }
420
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100421 uint64_t ns_bit = (desc >> NS_SHIFT) & 1U;
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100422
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100423 if (ns_bit == 1U)
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100424 *attributes |= MT_NS;
425
426 uint64_t xn_mask = xlat_arch_regime_get_xn_desc(ctx->xlat_regime);
427
428 if ((desc & xn_mask) == xn_mask) {
429 *attributes |= MT_EXECUTE_NEVER;
430 } else {
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100431 assert((desc & xn_mask) == 0U);
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100432 }
433
434 return 0;
435}
436
437
Antonio Nino Diaz6c4c9ee2018-08-05 15:34:10 +0100438int xlat_get_mem_attributes_ctx(const xlat_ctx_t *ctx, uintptr_t base_va,
439 uint32_t *attr)
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100440{
Antonio Nino Diaz6c4c9ee2018-08-05 15:34:10 +0100441 return xlat_get_mem_attributes_internal(ctx, base_va, attr,
442 NULL, NULL, NULL);
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100443}
444
445
Antonio Nino Diaz6c4c9ee2018-08-05 15:34:10 +0100446int xlat_change_mem_attributes_ctx(const xlat_ctx_t *ctx, uintptr_t base_va,
447 size_t size, uint32_t attr)
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100448{
449 /* Note: This implementation isn't optimized. */
450
451 assert(ctx != NULL);
Antonio Nino Diaz5c97bd12018-08-02 09:57:29 +0100452 assert(ctx->initialized);
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100453
454 unsigned long long virt_addr_space_size =
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100455 (unsigned long long)ctx->va_max_address + 1U;
456 assert(virt_addr_space_size > 0U);
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100457
458 if (!IS_PAGE_ALIGNED(base_va)) {
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100459 WARN("%s: Address 0x%lx is not aligned on a page boundary.\n",
460 __func__, base_va);
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100461 return -EINVAL;
462 }
463
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100464 if (size == 0U) {
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100465 WARN("%s: Size is 0.\n", __func__);
466 return -EINVAL;
467 }
468
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100469 if ((size % PAGE_SIZE) != 0U) {
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100470 WARN("%s: Size 0x%zx is not a multiple of a page size.\n",
471 __func__, size);
472 return -EINVAL;
473 }
474
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100475 if (((attr & MT_EXECUTE_NEVER) == 0U) && ((attr & MT_RW) != 0U)) {
Antonio Nino Diazbd0aff62018-07-02 09:26:51 +0100476 WARN("%s: Mapping memory as read-write and executable not allowed.\n",
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100477 __func__);
478 return -EINVAL;
479 }
480
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100481 size_t pages_count = size / PAGE_SIZE;
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100482
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100483 VERBOSE("Changing memory attributes of %zu pages starting from address 0x%lx...\n",
484 pages_count, base_va);
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100485
486 uintptr_t base_va_original = base_va;
487
488 /*
489 * Sanity checks.
490 */
Jimmy Brissoned202072020-08-04 16:18:52 -0500491 for (unsigned int i = 0U; i < pages_count; ++i) {
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100492 const uint64_t *entry;
493 uint64_t desc, attr_index;
494 unsigned int level;
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100495
496 entry = find_xlat_table_entry(base_va,
497 ctx->base_table,
498 ctx->base_table_entries,
499 virt_addr_space_size,
500 &level);
501 if (entry == NULL) {
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100502 WARN("Address 0x%lx is not mapped.\n", base_va);
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100503 return -EINVAL;
504 }
505
506 desc = *entry;
507
508 /*
509 * Check that all the required pages are mapped at page
510 * granularity.
511 */
512 if (((desc & DESC_MASK) != PAGE_DESC) ||
513 (level != XLAT_TABLE_LEVEL_MAX)) {
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100514 WARN("Address 0x%lx is not mapped at the right granularity.\n",
515 base_va);
Jimmy Brissoned202072020-08-04 16:18:52 -0500516 WARN("Granularity is 0x%lx, should be 0x%lx.\n",
517 XLAT_BLOCK_SIZE(level), PAGE_SIZE);
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100518 return -EINVAL;
519 }
520
521 /*
522 * If the region type is device, it shouldn't be executable.
523 */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100524 attr_index = (desc >> ATTR_INDEX_SHIFT) & ATTR_INDEX_MASK;
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100525 if (attr_index == ATTR_DEVICE_INDEX) {
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100526 if ((attr & MT_EXECUTE_NEVER) == 0U) {
527 WARN("Setting device memory as executable at address 0x%lx.",
528 base_va);
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100529 return -EINVAL;
530 }
531 }
532
533 base_va += PAGE_SIZE;
534 }
535
536 /* Restore original value. */
537 base_va = base_va_original;
538
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100539 for (unsigned int i = 0U; i < pages_count; ++i) {
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100540
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100541 uint32_t old_attr = 0U, new_attr;
542 uint64_t *entry = NULL;
543 unsigned int level = 0U;
544 unsigned long long addr_pa = 0ULL;
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100545
Antonio Nino Diaz6c4c9ee2018-08-05 15:34:10 +0100546 (void) xlat_get_mem_attributes_internal(ctx, base_va, &old_attr,
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100547 &entry, &addr_pa, &level);
548
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100549 /*
550 * From attr, only MT_RO/MT_RW, MT_EXECUTE/MT_EXECUTE_NEVER and
551 * MT_USER/MT_PRIVILEGED are taken into account. Any other
552 * information is ignored.
553 */
554
555 /* Clean the old attributes so that they can be rebuilt. */
Antonio Nino Diazbd0aff62018-07-02 09:26:51 +0100556 new_attr = old_attr & ~(MT_RW | MT_EXECUTE_NEVER | MT_USER);
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100557
558 /*
559 * Update attributes, but filter out the ones this function
560 * isn't allowed to change.
561 */
Antonio Nino Diazbd0aff62018-07-02 09:26:51 +0100562 new_attr |= attr & (MT_RW | MT_EXECUTE_NEVER | MT_USER);
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100563
564 /*
565 * The break-before-make sequence requires writing an invalid
566 * descriptor and making sure that the system sees the change
567 * before writing the new descriptor.
568 */
569 *entry = INVALID_DESC;
Artsem Artsemenkabce728f2019-10-17 13:51:27 +0100570#if !HW_ASSISTED_COHERENCY
Antonio Nino Diaz37a5efa2018-08-07 12:47:12 +0100571 dccvac((uintptr_t)entry);
572#endif
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100573 /* Invalidate any cached copy of this mapping in the TLBs. */
Antonio Nino Diazad5dc7f2018-07-11 09:46:45 +0100574 xlat_arch_tlbi_va(base_va, ctx->xlat_regime);
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100575
576 /* Ensure completion of the invalidation. */
577 xlat_arch_tlbi_va_sync();
578
579 /* Write new descriptor */
580 *entry = xlat_desc(ctx, new_attr, addr_pa, level);
Artsem Artsemenkabce728f2019-10-17 13:51:27 +0100581#if !HW_ASSISTED_COHERENCY
Antonio Nino Diaz37a5efa2018-08-07 12:47:12 +0100582 dccvac((uintptr_t)entry);
583#endif
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100584 base_va += PAGE_SIZE;
585 }
586
587 /* Ensure that the last descriptor writen is seen by the system. */
588 dsbish();
589
590 return 0;
591}