blob: 104303a48533bb748ce75292a1d1e04c2f35a3aa [file] [log] [blame]
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +00001/*
Antonio Nino Diaz3f518922018-01-05 11:30:36 +00002 * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved.
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +00003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +00005 */
6
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +00007#include <arch_helpers.h>
8#include <assert.h>
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +00009#include <debug.h>
10#include <errno.h>
11#include <platform_def.h>
12#include <string.h>
13#include <types.h>
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +010014#include <utils_def.h>
Sandrine Bailleux090c8492017-05-19 09:59:37 +010015#include <xlat_tables_defs.h>
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +000016#include <xlat_tables_v2.h>
Sandrine Bailleux090c8492017-05-19 09:59:37 +010017
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +000018#include "xlat_tables_private.h"
19
Antonio Nino Diazac998032017-02-27 17:23:54 +000020#if PLAT_XLAT_TABLES_DYNAMIC
21
22/*
23 * The following functions assume that they will be called using subtables only.
24 * The base table can't be unmapped, so it is not needed to do any special
25 * handling for it.
26 */
27
28/*
29 * Returns the index of the array corresponding to the specified translation
30 * table.
31 */
32static int xlat_table_get_index(xlat_ctx_t *ctx, const uint64_t *table)
33{
Varun Wadekar66231d12017-06-07 09:57:42 -070034 for (unsigned int i = 0; i < ctx->tables_num; i++)
Antonio Nino Diazac998032017-02-27 17:23:54 +000035 if (ctx->tables[i] == table)
36 return i;
37
38 /*
39 * Maybe we were asked to get the index of the base level table, which
40 * should never happen.
41 */
42 assert(0);
43
44 return -1;
45}
46
47/* Returns a pointer to an empty translation table. */
48static uint64_t *xlat_table_get_empty(xlat_ctx_t *ctx)
49{
Varun Wadekar66231d12017-06-07 09:57:42 -070050 for (unsigned int i = 0; i < ctx->tables_num; i++)
Antonio Nino Diazac998032017-02-27 17:23:54 +000051 if (ctx->tables_mapped_regions[i] == 0)
52 return ctx->tables[i];
53
54 return NULL;
55}
56
57/* Increments region count for a given table. */
58static void xlat_table_inc_regions_count(xlat_ctx_t *ctx, const uint64_t *table)
59{
60 ctx->tables_mapped_regions[xlat_table_get_index(ctx, table)]++;
61}
62
63/* Decrements region count for a given table. */
64static void xlat_table_dec_regions_count(xlat_ctx_t *ctx, const uint64_t *table)
65{
66 ctx->tables_mapped_regions[xlat_table_get_index(ctx, table)]--;
67}
68
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +010069/* Returns 0 if the specified table isn't empty, otherwise 1. */
Antonio Nino Diazac998032017-02-27 17:23:54 +000070static int xlat_table_is_empty(xlat_ctx_t *ctx, const uint64_t *table)
71{
72 return !ctx->tables_mapped_regions[xlat_table_get_index(ctx, table)];
73}
74
75#else /* PLAT_XLAT_TABLES_DYNAMIC */
76
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +000077/* Returns a pointer to the first empty translation table. */
78static uint64_t *xlat_table_get_empty(xlat_ctx_t *ctx)
79{
80 assert(ctx->next_table < ctx->tables_num);
81
82 return ctx->tables[ctx->next_table++];
83}
84
Antonio Nino Diazac998032017-02-27 17:23:54 +000085#endif /* PLAT_XLAT_TABLES_DYNAMIC */
86
Antonio Nino Diazdcf9d922017-10-04 16:52:15 +010087/*
88 * Returns a block/page table descriptor for the given level and attributes.
89 */
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +010090uint64_t xlat_desc(const xlat_ctx_t *ctx, uint32_t attr,
91 unsigned long long addr_pa, int level)
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +000092{
93 uint64_t desc;
94 int mem_type;
95
96 /* Make sure that the granularity is fine enough to map this address. */
97 assert((addr_pa & XLAT_BLOCK_MASK(level)) == 0);
98
99 desc = addr_pa;
100 /*
101 * There are different translation table descriptors for level 3 and the
102 * rest.
103 */
104 desc |= (level == XLAT_TABLE_LEVEL_MAX) ? PAGE_DESC : BLOCK_DESC;
105 /*
106 * Always set the access flag, as TF doesn't manage access flag faults.
107 * Deduce other fields of the descriptor based on the MT_NS and MT_RW
108 * memory region attributes.
109 */
Antonio Nino Diazdcf9d922017-10-04 16:52:15 +0100110 desc |= LOWER_ATTRS(ACCESS_FLAG);
111
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000112 desc |= (attr & MT_NS) ? LOWER_ATTRS(NS) : 0;
113 desc |= (attr & MT_RW) ? LOWER_ATTRS(AP_RW) : LOWER_ATTRS(AP_RO);
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000114
115 /*
Antonio Nino Diazdcf9d922017-10-04 16:52:15 +0100116 * Do not allow unprivileged access when the mapping is for a privileged
117 * EL. For translation regimes that do not have mappings for access for
118 * lower exception levels, set AP[2] to AP_NO_ACCESS_UNPRIVILEGED.
119 */
120 if (ctx->xlat_regime == EL1_EL0_REGIME) {
121 if (attr & MT_USER) {
122 /* EL0 mapping requested, so we give User access */
123 desc |= LOWER_ATTRS(AP_ACCESS_UNPRIVILEGED);
124 } else {
125 /* EL1 mapping requested, no User access granted */
126 desc |= LOWER_ATTRS(AP_NO_ACCESS_UNPRIVILEGED);
127 }
128 } else {
129 assert(ctx->xlat_regime == EL3_REGIME);
Antonio Nino Diaz49074492018-04-26 12:59:08 +0100130 desc |= LOWER_ATTRS(AP_ONE_VA_RANGE_RES1);
Antonio Nino Diazdcf9d922017-10-04 16:52:15 +0100131 }
132
133 /*
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000134 * Deduce shareability domain and executability of the memory region
135 * from the memory type of the attributes (MT_TYPE).
136 *
137 * Data accesses to device memory and non-cacheable normal memory are
138 * coherent for all observers in the system, and correspondingly are
139 * always treated as being Outer Shareable. Therefore, for these 2 types
140 * of memory, it is not strictly needed to set the shareability field
141 * in the translation tables.
142 */
143 mem_type = MT_TYPE(attr);
144 if (mem_type == MT_DEVICE) {
145 desc |= LOWER_ATTRS(ATTR_DEVICE_INDEX | OSH);
146 /*
147 * Always map device memory as execute-never.
148 * This is to avoid the possibility of a speculative instruction
149 * fetch, which could be an issue if this memory region
150 * corresponds to a read-sensitive peripheral.
151 */
Antonio Nino Diazdcf9d922017-10-04 16:52:15 +0100152 desc |= xlat_arch_regime_get_xn_desc(ctx->xlat_regime);
Antonio Nino Diazefabaa92017-04-27 13:30:22 +0100153
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000154 } else { /* Normal memory */
155 /*
156 * Always map read-write normal memory as execute-never.
157 * (Trusted Firmware doesn't self-modify its code, therefore
158 * R/W memory is reserved for data storage, which must not be
159 * executable.)
160 * Note that setting the XN bit here is for consistency only.
Antonio Nino Diazefabaa92017-04-27 13:30:22 +0100161 * The function that enables the MMU sets the SCTLR_ELx.WXN bit,
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000162 * which makes any writable memory region to be treated as
163 * execute-never, regardless of the value of the XN bit in the
164 * translation table.
165 *
166 * For read-only memory, rely on the MT_EXECUTE/MT_EXECUTE_NEVER
Antonio Nino Diazdcf9d922017-10-04 16:52:15 +0100167 * attribute to figure out the value of the XN bit. The actual
168 * XN bit(s) to set in the descriptor depends on the context's
169 * translation regime and the policy applied in
170 * xlat_arch_regime_get_xn_desc().
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000171 */
Antonio Nino Diazefabaa92017-04-27 13:30:22 +0100172 if ((attr & MT_RW) || (attr & MT_EXECUTE_NEVER)) {
Antonio Nino Diazdcf9d922017-10-04 16:52:15 +0100173 desc |= xlat_arch_regime_get_xn_desc(ctx->xlat_regime);
Antonio Nino Diazefabaa92017-04-27 13:30:22 +0100174 }
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000175
176 if (mem_type == MT_MEMORY) {
177 desc |= LOWER_ATTRS(ATTR_IWBWA_OWBWA_NTR_INDEX | ISH);
178 } else {
179 assert(mem_type == MT_NON_CACHEABLE);
180 desc |= LOWER_ATTRS(ATTR_NON_CACHEABLE_INDEX | OSH);
181 }
182 }
183
184 return desc;
185}
186
187/*
188 * Enumeration of actions that can be made when mapping table entries depending
189 * on the previous value in that entry and information about the region being
190 * mapped.
191 */
192typedef enum {
193
194 /* Do nothing */
195 ACTION_NONE,
196
197 /* Write a block (or page, if in level 3) entry. */
198 ACTION_WRITE_BLOCK_ENTRY,
199
200 /*
201 * Create a new table and write a table entry pointing to it. Recurse
202 * into it for further processing.
203 */
204 ACTION_CREATE_NEW_TABLE,
205
206 /*
207 * There is a table descriptor in this entry, read it and recurse into
208 * that table for further processing.
209 */
210 ACTION_RECURSE_INTO_TABLE,
211
212} action_t;
213
Antonio Nino Diazac998032017-02-27 17:23:54 +0000214#if PLAT_XLAT_TABLES_DYNAMIC
215
216/*
217 * Recursive function that writes to the translation tables and unmaps the
218 * specified region.
219 */
220static void xlat_tables_unmap_region(xlat_ctx_t *ctx, mmap_region_t *mm,
221 const uintptr_t table_base_va,
222 uint64_t *const table_base,
223 const int table_entries,
Varun Wadekar66231d12017-06-07 09:57:42 -0700224 const unsigned int level)
Antonio Nino Diazac998032017-02-27 17:23:54 +0000225{
226 assert(level >= ctx->base_level && level <= XLAT_TABLE_LEVEL_MAX);
227
228 uint64_t *subtable;
229 uint64_t desc;
230
231 uintptr_t table_idx_va;
232 uintptr_t table_idx_end_va; /* End VA of this entry */
233
234 uintptr_t region_end_va = mm->base_va + mm->size - 1;
235
236 int table_idx;
237
238 if (mm->base_va > table_base_va) {
239 /* Find the first index of the table affected by the region. */
240 table_idx_va = mm->base_va & ~XLAT_BLOCK_MASK(level);
241
242 table_idx = (table_idx_va - table_base_va) >>
243 XLAT_ADDR_SHIFT(level);
244
245 assert(table_idx < table_entries);
246 } else {
247 /* Start from the beginning of the table. */
248 table_idx_va = table_base_va;
249 table_idx = 0;
250 }
251
252 while (table_idx < table_entries) {
253
254 table_idx_end_va = table_idx_va + XLAT_BLOCK_SIZE(level) - 1;
255
256 desc = table_base[table_idx];
257 uint64_t desc_type = desc & DESC_MASK;
258
259 action_t action = ACTION_NONE;
260
261 if ((mm->base_va <= table_idx_va) &&
262 (region_end_va >= table_idx_end_va)) {
263
264 /* Region covers all block */
265
266 if (level == 3) {
267 /*
268 * Last level, only page descriptors allowed,
269 * erase it.
270 */
271 assert(desc_type == PAGE_DESC);
272
273 action = ACTION_WRITE_BLOCK_ENTRY;
274 } else {
275 /*
276 * Other levels can have table descriptors. If
277 * so, recurse into it and erase descriptors
278 * inside it as needed. If there is a block
279 * descriptor, just erase it. If an invalid
280 * descriptor is found, this table isn't
281 * actually mapped, which shouldn't happen.
282 */
283 if (desc_type == TABLE_DESC) {
284 action = ACTION_RECURSE_INTO_TABLE;
285 } else {
286 assert(desc_type == BLOCK_DESC);
287 action = ACTION_WRITE_BLOCK_ENTRY;
288 }
289 }
290
291 } else if ((mm->base_va <= table_idx_end_va) ||
292 (region_end_va >= table_idx_va)) {
293
294 /*
295 * Region partially covers block.
296 *
297 * It can't happen in level 3.
298 *
299 * There must be a table descriptor here, if not there
300 * was a problem when mapping the region.
301 */
302
303 assert(level < 3);
304
305 assert(desc_type == TABLE_DESC);
306
307 action = ACTION_RECURSE_INTO_TABLE;
308 }
309
310 if (action == ACTION_WRITE_BLOCK_ENTRY) {
311
312 table_base[table_idx] = INVALID_DESC;
Antonio Nino Diazad5dc7f2018-07-11 09:46:45 +0100313 xlat_arch_tlbi_va(table_idx_va, ctx->xlat_regime);
Antonio Nino Diazac998032017-02-27 17:23:54 +0000314
315 } else if (action == ACTION_RECURSE_INTO_TABLE) {
316
317 subtable = (uint64_t *)(uintptr_t)(desc & TABLE_ADDR_MASK);
318
319 /* Recurse to write into subtable */
320 xlat_tables_unmap_region(ctx, mm, table_idx_va,
321 subtable, XLAT_TABLE_ENTRIES,
322 level + 1);
323
324 /*
325 * If the subtable is now empty, remove its reference.
326 */
327 if (xlat_table_is_empty(ctx, subtable)) {
328 table_base[table_idx] = INVALID_DESC;
Antonio Nino Diazad5dc7f2018-07-11 09:46:45 +0100329 xlat_arch_tlbi_va(table_idx_va,
330 ctx->xlat_regime);
Antonio Nino Diazac998032017-02-27 17:23:54 +0000331 }
332
333 } else {
334 assert(action == ACTION_NONE);
335 }
336
337 table_idx++;
338 table_idx_va += XLAT_BLOCK_SIZE(level);
339
340 /* If reached the end of the region, exit */
341 if (region_end_va <= table_idx_va)
342 break;
343 }
344
345 if (level > ctx->base_level)
346 xlat_table_dec_regions_count(ctx, table_base);
347}
348
349#endif /* PLAT_XLAT_TABLES_DYNAMIC */
350
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000351/*
352 * From the given arguments, it decides which action to take when mapping the
353 * specified region.
354 */
355static action_t xlat_tables_map_region_action(const mmap_region_t *mm,
356 const int desc_type, const unsigned long long dest_pa,
Sandrine Bailleux12e86442017-07-19 10:11:13 +0100357 const uintptr_t table_entry_base_va, const unsigned int level)
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000358{
359 uintptr_t mm_end_va = mm->base_va + mm->size - 1;
360 uintptr_t table_entry_end_va =
361 table_entry_base_va + XLAT_BLOCK_SIZE(level) - 1;
362
363 /*
364 * The descriptor types allowed depend on the current table level.
365 */
366
367 if ((mm->base_va <= table_entry_base_va) &&
368 (mm_end_va >= table_entry_end_va)) {
369
370 /*
371 * Table entry is covered by region
372 * --------------------------------
373 *
374 * This means that this table entry can describe the whole
375 * translation with this granularity in principle.
376 */
377
378 if (level == 3) {
379 /*
380 * Last level, only page descriptors are allowed.
381 */
382 if (desc_type == PAGE_DESC) {
383 /*
384 * There's another region mapped here, don't
385 * overwrite.
386 */
387 return ACTION_NONE;
388 } else {
389 assert(desc_type == INVALID_DESC);
390 return ACTION_WRITE_BLOCK_ENTRY;
391 }
392
393 } else {
394
395 /*
396 * Other levels. Table descriptors are allowed. Block
397 * descriptors too, but they have some limitations.
398 */
399
400 if (desc_type == TABLE_DESC) {
401 /* There's already a table, recurse into it. */
402 return ACTION_RECURSE_INTO_TABLE;
403
404 } else if (desc_type == INVALID_DESC) {
405 /*
406 * There's nothing mapped here, create a new
407 * entry.
408 *
409 * Check if the destination granularity allows
410 * us to use a block descriptor or we need a
411 * finer table for it.
412 *
413 * Also, check if the current level allows block
414 * descriptors. If not, create a table instead.
415 */
416 if ((dest_pa & XLAT_BLOCK_MASK(level)) ||
Sandrine Bailleux8f23fa82017-09-28 21:58:12 +0100417 (level < MIN_LVL_BLOCK_DESC) ||
418 (mm->granularity < XLAT_BLOCK_SIZE(level)))
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000419 return ACTION_CREATE_NEW_TABLE;
420 else
421 return ACTION_WRITE_BLOCK_ENTRY;
422
423 } else {
424 /*
425 * There's another region mapped here, don't
426 * overwrite.
427 */
428 assert(desc_type == BLOCK_DESC);
429
430 return ACTION_NONE;
431 }
432 }
433
434 } else if ((mm->base_va <= table_entry_end_va) ||
435 (mm_end_va >= table_entry_base_va)) {
436
437 /*
438 * Region partially covers table entry
439 * -----------------------------------
440 *
441 * This means that this table entry can't describe the whole
442 * translation, a finer table is needed.
443
444 * There cannot be partial block overlaps in level 3. If that
445 * happens, some of the preliminary checks when adding the
446 * mmap region failed to detect that PA and VA must at least be
447 * aligned to PAGE_SIZE.
448 */
449 assert(level < 3);
450
451 if (desc_type == INVALID_DESC) {
452 /*
453 * The block is not fully covered by the region. Create
454 * a new table, recurse into it and try to map the
455 * region with finer granularity.
456 */
457 return ACTION_CREATE_NEW_TABLE;
458
459 } else {
460 assert(desc_type == TABLE_DESC);
461 /*
462 * The block is not fully covered by the region, but
463 * there is already a table here. Recurse into it and
464 * try to map with finer granularity.
465 *
466 * PAGE_DESC for level 3 has the same value as
467 * TABLE_DESC, but this code can't run on a level 3
468 * table because there can't be overlaps in level 3.
469 */
470 return ACTION_RECURSE_INTO_TABLE;
471 }
472 }
473
474 /*
475 * This table entry is outside of the region specified in the arguments,
476 * don't write anything to it.
477 */
478 return ACTION_NONE;
479}
480
481/*
482 * Recursive function that writes to the translation tables and maps the
Antonio Nino Diazac998032017-02-27 17:23:54 +0000483 * specified region. On success, it returns the VA of the last byte that was
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100484 * successfully mapped. On error, it returns the VA of the next entry that
Antonio Nino Diazac998032017-02-27 17:23:54 +0000485 * should have been mapped.
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000486 */
Antonio Nino Diazac998032017-02-27 17:23:54 +0000487static uintptr_t xlat_tables_map_region(xlat_ctx_t *ctx, mmap_region_t *mm,
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000488 const uintptr_t table_base_va,
489 uint64_t *const table_base,
490 const int table_entries,
Varun Wadekar66231d12017-06-07 09:57:42 -0700491 const unsigned int level)
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000492{
493 assert(level >= ctx->base_level && level <= XLAT_TABLE_LEVEL_MAX);
494
495 uintptr_t mm_end_va = mm->base_va + mm->size - 1;
496
497 uintptr_t table_idx_va;
498 unsigned long long table_idx_pa;
499
500 uint64_t *subtable;
501 uint64_t desc;
502
503 int table_idx;
504
505 if (mm->base_va > table_base_va) {
506 /* Find the first index of the table affected by the region. */
507 table_idx_va = mm->base_va & ~XLAT_BLOCK_MASK(level);
508
509 table_idx = (table_idx_va - table_base_va) >>
510 XLAT_ADDR_SHIFT(level);
511
512 assert(table_idx < table_entries);
513 } else {
514 /* Start from the beginning of the table. */
515 table_idx_va = table_base_va;
516 table_idx = 0;
517 }
518
Antonio Nino Diazac998032017-02-27 17:23:54 +0000519#if PLAT_XLAT_TABLES_DYNAMIC
520 if (level > ctx->base_level)
521 xlat_table_inc_regions_count(ctx, table_base);
522#endif
523
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000524 while (table_idx < table_entries) {
525
526 desc = table_base[table_idx];
527
528 table_idx_pa = mm->base_pa + table_idx_va - mm->base_va;
529
530 action_t action = xlat_tables_map_region_action(mm,
531 desc & DESC_MASK, table_idx_pa, table_idx_va, level);
532
533 if (action == ACTION_WRITE_BLOCK_ENTRY) {
534
535 table_base[table_idx] =
Antonio Nino Diaze8811472018-04-17 15:10:18 +0100536 xlat_desc(ctx, (uint32_t)mm->attr, table_idx_pa,
537 level);
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000538
539 } else if (action == ACTION_CREATE_NEW_TABLE) {
540
541 subtable = xlat_table_get_empty(ctx);
Antonio Nino Diazac998032017-02-27 17:23:54 +0000542 if (subtable == NULL) {
543 /* Not enough free tables to map this region */
544 return table_idx_va;
545 }
546
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000547 /* Point to new subtable from this one. */
Antonio Nino Diazac998032017-02-27 17:23:54 +0000548 table_base[table_idx] = TABLE_DESC | (unsigned long)subtable;
549
550 /* Recurse to write into subtable */
551 uintptr_t end_va = xlat_tables_map_region(ctx, mm, table_idx_va,
552 subtable, XLAT_TABLE_ENTRIES,
553 level + 1);
554 if (end_va != table_idx_va + XLAT_BLOCK_SIZE(level) - 1)
555 return end_va;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000556
557 } else if (action == ACTION_RECURSE_INTO_TABLE) {
558
559 subtable = (uint64_t *)(uintptr_t)(desc & TABLE_ADDR_MASK);
560 /* Recurse to write into subtable */
Antonio Nino Diazac998032017-02-27 17:23:54 +0000561 uintptr_t end_va = xlat_tables_map_region(ctx, mm, table_idx_va,
562 subtable, XLAT_TABLE_ENTRIES,
563 level + 1);
564 if (end_va != table_idx_va + XLAT_BLOCK_SIZE(level) - 1)
565 return end_va;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000566
567 } else {
568
569 assert(action == ACTION_NONE);
570
571 }
572
573 table_idx++;
574 table_idx_va += XLAT_BLOCK_SIZE(level);
575
576 /* If reached the end of the region, exit */
577 if (mm_end_va <= table_idx_va)
578 break;
579 }
Antonio Nino Diazac998032017-02-27 17:23:54 +0000580
581 return table_idx_va - 1;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000582}
583
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000584/*
585 * Function that verifies that a region can be mapped.
586 * Returns:
587 * 0: Success, the mapping is allowed.
588 * EINVAL: Invalid values were used as arguments.
589 * ERANGE: The memory limits were surpassed.
590 * ENOMEM: There is not enough memory in the mmap array.
591 * EPERM: Region overlaps another one in an invalid way.
592 */
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100593static int mmap_add_region_check(xlat_ctx_t *ctx, const mmap_region_t *mm)
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000594{
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100595 unsigned long long base_pa = mm->base_pa;
596 uintptr_t base_va = mm->base_va;
597 size_t size = mm->size;
Sandrine Bailleux8f23fa82017-09-28 21:58:12 +0100598 size_t granularity = mm->granularity;
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100599
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000600 unsigned long long end_pa = base_pa + size - 1;
601 uintptr_t end_va = base_va + size - 1;
602
603 if (!IS_PAGE_ALIGNED(base_pa) || !IS_PAGE_ALIGNED(base_va) ||
604 !IS_PAGE_ALIGNED(size))
605 return -EINVAL;
606
Sandrine Bailleux8f23fa82017-09-28 21:58:12 +0100607 if ((granularity != XLAT_BLOCK_SIZE(1)) &&
608 (granularity != XLAT_BLOCK_SIZE(2)) &&
609 (granularity != XLAT_BLOCK_SIZE(3))) {
610 return -EINVAL;
611 }
612
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000613 /* Check for overflows */
614 if ((base_pa > end_pa) || (base_va > end_va))
615 return -ERANGE;
616
617 if ((base_va + (uintptr_t)size - (uintptr_t)1) > ctx->va_max_address)
618 return -ERANGE;
619
620 if ((base_pa + (unsigned long long)size - 1ULL) > ctx->pa_max_address)
621 return -ERANGE;
622
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100623 /* Check that there is space in the ctx->mmap array */
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000624 if (ctx->mmap[ctx->mmap_num - 1].size != 0)
625 return -ENOMEM;
626
627 /* Check for PAs and VAs overlaps with all other regions */
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100628 for (mmap_region_t *mm_cursor = ctx->mmap;
629 mm_cursor->size; ++mm_cursor) {
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000630
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100631 uintptr_t mm_cursor_end_va = mm_cursor->base_va
632 + mm_cursor->size - 1;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000633
634 /*
635 * Check if one of the regions is completely inside the other
636 * one.
637 */
638 int fully_overlapped_va =
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100639 ((base_va >= mm_cursor->base_va) &&
640 (end_va <= mm_cursor_end_va)) ||
641
642 ((mm_cursor->base_va >= base_va) &&
643 (mm_cursor_end_va <= end_va));
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000644
645 /*
646 * Full VA overlaps are only allowed if both regions are
647 * identity mapped (zero offset) or have the same VA to PA
648 * offset. Also, make sure that it's not the exact same area.
Antonio Nino Diazac998032017-02-27 17:23:54 +0000649 * This can only be done with static regions.
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000650 */
651 if (fully_overlapped_va) {
652
Antonio Nino Diazac998032017-02-27 17:23:54 +0000653#if PLAT_XLAT_TABLES_DYNAMIC
Sandrine Bailleux8f23fa82017-09-28 21:58:12 +0100654 if ((mm->attr & MT_DYNAMIC) ||
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100655 (mm_cursor->attr & MT_DYNAMIC))
Antonio Nino Diazac998032017-02-27 17:23:54 +0000656 return -EPERM;
657#endif /* PLAT_XLAT_TABLES_DYNAMIC */
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100658 if ((mm_cursor->base_va - mm_cursor->base_pa) !=
659 (base_va - base_pa))
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000660 return -EPERM;
661
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100662 if ((base_va == mm_cursor->base_va) &&
663 (size == mm_cursor->size))
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000664 return -EPERM;
665
666 } else {
667 /*
668 * If the regions do not have fully overlapping VAs,
669 * then they must have fully separated VAs and PAs.
670 * Partial overlaps are not allowed
671 */
672
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100673 unsigned long long mm_cursor_end_pa =
674 mm_cursor->base_pa + mm_cursor->size - 1;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000675
676 int separated_pa =
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100677 (end_pa < mm_cursor->base_pa) ||
678 (base_pa > mm_cursor_end_pa);
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000679 int separated_va =
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100680 (end_va < mm_cursor->base_va) ||
681 (base_va > mm_cursor_end_va);
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000682
683 if (!(separated_va && separated_pa))
684 return -EPERM;
685 }
686 }
687
688 return 0;
689}
690
Sandrine Bailleux66342932017-07-18 13:26:36 +0100691void mmap_add_region_ctx(xlat_ctx_t *ctx, const mmap_region_t *mm)
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000692{
John Tsichritzisfdd92482018-05-25 09:12:48 +0100693 mmap_region_t *mm_cursor = ctx->mmap, *mm_destination;
Varun Wadekarccbd2e32018-04-03 10:44:41 -0700694 const mmap_region_t *mm_end = ctx->mmap + ctx->mmap_num;
695 mmap_region_t *mm_last;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000696 unsigned long long end_pa = mm->base_pa + mm->size - 1;
697 uintptr_t end_va = mm->base_va + mm->size - 1;
698 int ret;
699
700 /* Ignore empty regions */
701 if (!mm->size)
702 return;
703
Antonio Nino Diazac998032017-02-27 17:23:54 +0000704 /* Static regions must be added before initializing the xlat tables. */
705 assert(!ctx->initialized);
706
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100707 ret = mmap_add_region_check(ctx, mm);
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000708 if (ret != 0) {
709 ERROR("mmap_add_region_check() failed. error %d\n", ret);
710 assert(0);
711 return;
712 }
713
714 /*
715 * Find correct place in mmap to insert new region.
716 *
717 * 1 - Lower region VA end first.
718 * 2 - Smaller region size first.
719 *
720 * VA 0 0xFF
721 *
722 * 1st |------|
723 * 2nd |------------|
724 * 3rd |------|
725 * 4th |---|
726 * 5th |---|
727 * 6th |----------|
728 * 7th |-------------------------------------|
729 *
730 * This is required for overlapping regions only. It simplifies adding
731 * regions with the loop in xlat_tables_init_internal because the outer
732 * ones won't overwrite block or page descriptors of regions added
733 * previously.
Antonio Nino Diazac998032017-02-27 17:23:54 +0000734 *
735 * Overlapping is only allowed for static regions.
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000736 */
737
738 while ((mm_cursor->base_va + mm_cursor->size - 1) < end_va
739 && mm_cursor->size)
740 ++mm_cursor;
741
Yann Gautiereabaea52018-06-14 14:36:20 +0200742 while ((mm_cursor->base_va + mm_cursor->size - 1 == end_va) &&
743 (mm_cursor->size != 0U) && (mm_cursor->size < mm->size))
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000744 ++mm_cursor;
745
Varun Wadekarccbd2e32018-04-03 10:44:41 -0700746 /*
747 * Find the last entry marker in the mmap
748 */
749 mm_last = ctx->mmap;
750 while ((mm_last->size != 0U) && (mm_last < mm_end)) {
751 ++mm_last;
752 }
753
754 /*
755 * Check if we have enough space in the memory mapping table.
756 * This shouldn't happen as we have checked in mmap_add_region_check
757 * that there is free space.
758 */
759 assert(mm_last->size == 0U);
Jeenu Viswambharan58e81482018-04-27 15:06:57 +0100760
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000761 /* Make room for new region by moving other regions up by one place */
John Tsichritzisfdd92482018-05-25 09:12:48 +0100762 mm_destination = mm_cursor + 1;
763 memmove(mm_destination, mm_cursor,
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000764 (uintptr_t)mm_last - (uintptr_t)mm_cursor);
765
766 /*
767 * Check we haven't lost the empty sentinel from the end of the array.
768 * This shouldn't happen as we have checked in mmap_add_region_check
769 * that there is free space.
770 */
Varun Wadekarccbd2e32018-04-03 10:44:41 -0700771 assert(mm_end->size == 0U);
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000772
Douglas Raillardf68d2ed2017-09-12 10:31:49 +0100773 *mm_cursor = *mm;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000774
775 if (end_pa > ctx->max_pa)
776 ctx->max_pa = end_pa;
777 if (end_va > ctx->max_va)
778 ctx->max_va = end_va;
779}
780
Sandrine Bailleux66342932017-07-18 13:26:36 +0100781void mmap_add_ctx(xlat_ctx_t *ctx, const mmap_region_t *mm)
782{
783 while (mm->size) {
784 mmap_add_region_ctx(ctx, mm);
785 mm++;
786 }
787}
788
Antonio Nino Diazac998032017-02-27 17:23:54 +0000789#if PLAT_XLAT_TABLES_DYNAMIC
790
791int mmap_add_dynamic_region_ctx(xlat_ctx_t *ctx, mmap_region_t *mm)
792{
793 mmap_region_t *mm_cursor = ctx->mmap;
794 mmap_region_t *mm_last = mm_cursor + ctx->mmap_num;
795 unsigned long long end_pa = mm->base_pa + mm->size - 1;
796 uintptr_t end_va = mm->base_va + mm->size - 1;
797 int ret;
798
799 /* Nothing to do */
800 if (!mm->size)
801 return 0;
802
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100803 /* Now this region is a dynamic one */
804 mm->attr |= MT_DYNAMIC;
805
806 ret = mmap_add_region_check(ctx, mm);
Antonio Nino Diazac998032017-02-27 17:23:54 +0000807 if (ret != 0)
808 return ret;
809
810 /*
811 * Find the adequate entry in the mmap array in the same way done for
812 * static regions in mmap_add_region_ctx().
813 */
814
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100815 while ((mm_cursor->base_va + mm_cursor->size - 1)
816 < end_va && mm_cursor->size)
Antonio Nino Diazac998032017-02-27 17:23:54 +0000817 ++mm_cursor;
818
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100819 while ((mm_cursor->base_va + mm_cursor->size - 1 == end_va)
820 && (mm_cursor->size < mm->size))
Antonio Nino Diazac998032017-02-27 17:23:54 +0000821 ++mm_cursor;
822
823 /* Make room for new region by moving other regions up by one place */
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100824 memmove(mm_cursor + 1, mm_cursor,
825 (uintptr_t)mm_last - (uintptr_t)mm_cursor);
Antonio Nino Diazac998032017-02-27 17:23:54 +0000826
827 /*
828 * Check we haven't lost the empty sentinal from the end of the array.
829 * This shouldn't happen as we have checked in mmap_add_region_check
830 * that there is free space.
831 */
832 assert(mm_last->size == 0);
833
Douglas Raillardf68d2ed2017-09-12 10:31:49 +0100834 *mm_cursor = *mm;
Antonio Nino Diazac998032017-02-27 17:23:54 +0000835
836 /*
837 * Update the translation tables if the xlat tables are initialized. If
838 * not, this region will be mapped when they are initialized.
839 */
840 if (ctx->initialized) {
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100841 uintptr_t end_va = xlat_tables_map_region(ctx, mm_cursor,
842 0, ctx->base_table, ctx->base_table_entries,
843 ctx->base_level);
Antonio Nino Diazac998032017-02-27 17:23:54 +0000844
845 /* Failed to map, remove mmap entry, unmap and return error. */
846 if (end_va != mm_cursor->base_va + mm_cursor->size - 1) {
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100847 memmove(mm_cursor, mm_cursor + 1,
848 (uintptr_t)mm_last - (uintptr_t)mm_cursor);
Antonio Nino Diazac998032017-02-27 17:23:54 +0000849
850 /*
851 * Check if the mapping function actually managed to map
852 * anything. If not, just return now.
853 */
Antonio Nino Diaz3f518922018-01-05 11:30:36 +0000854 if (mm->base_va >= end_va)
Antonio Nino Diazac998032017-02-27 17:23:54 +0000855 return -ENOMEM;
856
857 /*
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100858 * Something went wrong after mapping some table
859 * entries, undo every change done up to this point.
Antonio Nino Diazac998032017-02-27 17:23:54 +0000860 */
861 mmap_region_t unmap_mm = {
862 .base_pa = 0,
863 .base_va = mm->base_va,
864 .size = end_va - mm->base_va,
865 .attr = 0
866 };
Antonio Nino Diazdcf9d922017-10-04 16:52:15 +0100867 xlat_tables_unmap_region(ctx, &unmap_mm, 0, ctx->base_table,
868 ctx->base_table_entries, ctx->base_level);
Antonio Nino Diazac998032017-02-27 17:23:54 +0000869
870 return -ENOMEM;
871 }
872
873 /*
874 * Make sure that all entries are written to the memory. There
875 * is no need to invalidate entries when mapping dynamic regions
876 * because new table/block/page descriptors only replace old
877 * invalid descriptors, that aren't TLB cached.
878 */
879 dsbishst();
880 }
881
882 if (end_pa > ctx->max_pa)
883 ctx->max_pa = end_pa;
884 if (end_va > ctx->max_va)
885 ctx->max_va = end_va;
886
887 return 0;
888}
889
890/*
891 * Removes the region with given base Virtual Address and size from the given
892 * context.
893 *
894 * Returns:
895 * 0: Success.
896 * EINVAL: Invalid values were used as arguments (region not found).
897 * EPERM: Tried to remove a static region.
898 */
899int mmap_remove_dynamic_region_ctx(xlat_ctx_t *ctx, uintptr_t base_va,
900 size_t size)
901{
902 mmap_region_t *mm = ctx->mmap;
903 mmap_region_t *mm_last = mm + ctx->mmap_num;
904 int update_max_va_needed = 0;
905 int update_max_pa_needed = 0;
906
907 /* Check sanity of mmap array. */
908 assert(mm[ctx->mmap_num].size == 0);
909
910 while (mm->size) {
911 if ((mm->base_va == base_va) && (mm->size == size))
912 break;
913 ++mm;
914 }
915
916 /* Check that the region was found */
917 if (mm->size == 0)
918 return -EINVAL;
919
920 /* If the region is static it can't be removed */
921 if (!(mm->attr & MT_DYNAMIC))
922 return -EPERM;
923
924 /* Check if this region is using the top VAs or PAs. */
925 if ((mm->base_va + mm->size - 1) == ctx->max_va)
926 update_max_va_needed = 1;
927 if ((mm->base_pa + mm->size - 1) == ctx->max_pa)
928 update_max_pa_needed = 1;
929
930 /* Update the translation tables if needed */
931 if (ctx->initialized) {
932 xlat_tables_unmap_region(ctx, mm, 0, ctx->base_table,
933 ctx->base_table_entries,
934 ctx->base_level);
935 xlat_arch_tlbi_va_sync();
936 }
937
938 /* Remove this region by moving the rest down by one place. */
939 memmove(mm, mm + 1, (uintptr_t)mm_last - (uintptr_t)mm);
940
941 /* Check if we need to update the max VAs and PAs */
942 if (update_max_va_needed) {
943 ctx->max_va = 0;
944 mm = ctx->mmap;
945 while (mm->size) {
946 if ((mm->base_va + mm->size - 1) > ctx->max_va)
947 ctx->max_va = mm->base_va + mm->size - 1;
948 ++mm;
949 }
950 }
951
952 if (update_max_pa_needed) {
953 ctx->max_pa = 0;
954 mm = ctx->mmap;
955 while (mm->size) {
956 if ((mm->base_pa + mm->size - 1) > ctx->max_pa)
957 ctx->max_pa = mm->base_pa + mm->size - 1;
958 ++mm;
959 }
960 }
961
962 return 0;
963}
964
965#endif /* PLAT_XLAT_TABLES_DYNAMIC */
966
Sandrine Bailleux66342932017-07-18 13:26:36 +0100967void init_xlat_tables_ctx(xlat_ctx_t *ctx)
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000968{
Antonio Nino Diazdcf9d922017-10-04 16:52:15 +0100969 assert(ctx != NULL);
Sandrine Bailleux66342932017-07-18 13:26:36 +0100970 assert(!ctx->initialized);
Antonio Nino Diazdcf9d922017-10-04 16:52:15 +0100971 assert(ctx->xlat_regime == EL3_REGIME || ctx->xlat_regime == EL1_EL0_REGIME);
972 assert(!is_mmu_enabled_ctx(ctx));
Sandrine Bailleux66342932017-07-18 13:26:36 +0100973
Antonio Nino Diazdcf9d922017-10-04 16:52:15 +0100974 mmap_region_t *mm = ctx->mmap;
Sandrine Bailleux66342932017-07-18 13:26:36 +0100975
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100976 xlat_mmap_print(mm);
Sandrine Bailleux66342932017-07-18 13:26:36 +0100977
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000978 /* All tables must be zeroed before mapping any region. */
979
Varun Wadekar66231d12017-06-07 09:57:42 -0700980 for (unsigned int i = 0; i < ctx->base_table_entries; i++)
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000981 ctx->base_table[i] = INVALID_DESC;
982
Varun Wadekar66231d12017-06-07 09:57:42 -0700983 for (unsigned int j = 0; j < ctx->tables_num; j++) {
Antonio Nino Diazac998032017-02-27 17:23:54 +0000984#if PLAT_XLAT_TABLES_DYNAMIC
985 ctx->tables_mapped_regions[j] = 0;
986#endif
Varun Wadekar66231d12017-06-07 09:57:42 -0700987 for (unsigned int i = 0; i < XLAT_TABLE_ENTRIES; i++)
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000988 ctx->tables[j][i] = INVALID_DESC;
989 }
990
Antonio Nino Diazac998032017-02-27 17:23:54 +0000991 while (mm->size) {
992 uintptr_t end_va = xlat_tables_map_region(ctx, mm, 0, ctx->base_table,
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000993 ctx->base_table_entries, ctx->base_level);
994
Antonio Nino Diazac998032017-02-27 17:23:54 +0000995 if (end_va != mm->base_va + mm->size - 1) {
996 ERROR("Not enough memory to map region:\n"
997 " VA:%p PA:0x%llx size:0x%zx attr:0x%x\n",
998 (void *)mm->base_va, mm->base_pa, mm->size, mm->attr);
999 panic();
1000 }
1001
1002 mm++;
1003 }
1004
Sandrine Bailleux46c53a22017-07-11 15:11:10 +01001005 assert(ctx->pa_max_address <= xlat_arch_get_max_supported_pa());
Sandrine Bailleux66342932017-07-18 13:26:36 +01001006 assert(ctx->max_va <= ctx->va_max_address);
1007 assert(ctx->max_pa <= ctx->pa_max_address);
1008
Sandrine Bailleuxc5b63772017-05-31 13:31:48 +01001009 ctx->initialized = 1;
1010
1011 xlat_tables_print(ctx);
Sandrine Bailleux66342932017-07-18 13:26:36 +01001012}