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