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