blob: f555524a958b27c82f784217efc0cae54d7cac5b [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>
Antonio Nino Diazdcf9d922017-10-04 16:52:15 +010015#include <xlat_tables_arch_private.h>
Sandrine Bailleux090c8492017-05-19 09:59:37 +010016#include <xlat_tables_defs.h>
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +000017#include <xlat_tables_v2.h>
Sandrine Bailleux090c8492017-05-19 09:59:37 +010018
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +000019#include "xlat_tables_private.h"
20
Antonio Nino Diazac998032017-02-27 17:23:54 +000021#if PLAT_XLAT_TABLES_DYNAMIC
22
23/*
24 * The following functions assume that they will be called using subtables only.
25 * The base table can't be unmapped, so it is not needed to do any special
26 * handling for it.
27 */
28
29/*
30 * Returns the index of the array corresponding to the specified translation
31 * table.
32 */
33static int xlat_table_get_index(xlat_ctx_t *ctx, const uint64_t *table)
34{
Varun Wadekar66231d12017-06-07 09:57:42 -070035 for (unsigned int i = 0; i < ctx->tables_num; i++)
Antonio Nino Diazac998032017-02-27 17:23:54 +000036 if (ctx->tables[i] == table)
37 return i;
38
39 /*
40 * Maybe we were asked to get the index of the base level table, which
41 * should never happen.
42 */
43 assert(0);
44
45 return -1;
46}
47
48/* Returns a pointer to an empty translation table. */
49static uint64_t *xlat_table_get_empty(xlat_ctx_t *ctx)
50{
Varun Wadekar66231d12017-06-07 09:57:42 -070051 for (unsigned int i = 0; i < ctx->tables_num; i++)
Antonio Nino Diazac998032017-02-27 17:23:54 +000052 if (ctx->tables_mapped_regions[i] == 0)
53 return ctx->tables[i];
54
55 return NULL;
56}
57
58/* Increments region count for a given table. */
59static void xlat_table_inc_regions_count(xlat_ctx_t *ctx, const uint64_t *table)
60{
61 ctx->tables_mapped_regions[xlat_table_get_index(ctx, table)]++;
62}
63
64/* Decrements region count for a given table. */
65static void xlat_table_dec_regions_count(xlat_ctx_t *ctx, const uint64_t *table)
66{
67 ctx->tables_mapped_regions[xlat_table_get_index(ctx, table)]--;
68}
69
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +010070/* Returns 0 if the specified table isn't empty, otherwise 1. */
Antonio Nino Diazac998032017-02-27 17:23:54 +000071static int xlat_table_is_empty(xlat_ctx_t *ctx, const uint64_t *table)
72{
73 return !ctx->tables_mapped_regions[xlat_table_get_index(ctx, table)];
74}
75
76#else /* PLAT_XLAT_TABLES_DYNAMIC */
77
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +000078/* Returns a pointer to the first empty translation table. */
79static uint64_t *xlat_table_get_empty(xlat_ctx_t *ctx)
80{
81 assert(ctx->next_table < ctx->tables_num);
82
83 return ctx->tables[ctx->next_table++];
84}
85
Antonio Nino Diazac998032017-02-27 17:23:54 +000086#endif /* PLAT_XLAT_TABLES_DYNAMIC */
87
Antonio Nino Diazdcf9d922017-10-04 16:52:15 +010088/*
89 * Returns a block/page table descriptor for the given level and attributes.
90 */
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +010091uint64_t xlat_desc(const xlat_ctx_t *ctx, uint32_t attr,
92 unsigned long long addr_pa, int level)
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +000093{
94 uint64_t desc;
95 int mem_type;
96
97 /* Make sure that the granularity is fine enough to map this address. */
98 assert((addr_pa & XLAT_BLOCK_MASK(level)) == 0);
99
100 desc = addr_pa;
101 /*
102 * There are different translation table descriptors for level 3 and the
103 * rest.
104 */
105 desc |= (level == XLAT_TABLE_LEVEL_MAX) ? PAGE_DESC : BLOCK_DESC;
106 /*
107 * Always set the access flag, as TF doesn't manage access flag faults.
108 * Deduce other fields of the descriptor based on the MT_NS and MT_RW
109 * memory region attributes.
110 */
Antonio Nino Diazdcf9d922017-10-04 16:52:15 +0100111 desc |= LOWER_ATTRS(ACCESS_FLAG);
112
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000113 desc |= (attr & MT_NS) ? LOWER_ATTRS(NS) : 0;
114 desc |= (attr & MT_RW) ? LOWER_ATTRS(AP_RW) : LOWER_ATTRS(AP_RO);
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000115
116 /*
Antonio Nino Diazdcf9d922017-10-04 16:52:15 +0100117 * Do not allow unprivileged access when the mapping is for a privileged
118 * EL. For translation regimes that do not have mappings for access for
119 * lower exception levels, set AP[2] to AP_NO_ACCESS_UNPRIVILEGED.
120 */
121 if (ctx->xlat_regime == EL1_EL0_REGIME) {
122 if (attr & MT_USER) {
123 /* EL0 mapping requested, so we give User access */
124 desc |= LOWER_ATTRS(AP_ACCESS_UNPRIVILEGED);
125 } else {
126 /* EL1 mapping requested, no User access granted */
127 desc |= LOWER_ATTRS(AP_NO_ACCESS_UNPRIVILEGED);
128 }
129 } else {
130 assert(ctx->xlat_regime == EL3_REGIME);
Antonio Nino Diaz49074492018-04-26 12:59:08 +0100131 desc |= LOWER_ATTRS(AP_ONE_VA_RANGE_RES1);
Antonio Nino Diazdcf9d922017-10-04 16:52:15 +0100132 }
133
134 /*
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000135 * Deduce shareability domain and executability of the memory region
136 * from the memory type of the attributes (MT_TYPE).
137 *
138 * Data accesses to device memory and non-cacheable normal memory are
139 * coherent for all observers in the system, and correspondingly are
140 * always treated as being Outer Shareable. Therefore, for these 2 types
141 * of memory, it is not strictly needed to set the shareability field
142 * in the translation tables.
143 */
144 mem_type = MT_TYPE(attr);
145 if (mem_type == MT_DEVICE) {
146 desc |= LOWER_ATTRS(ATTR_DEVICE_INDEX | OSH);
147 /*
148 * Always map device memory as execute-never.
149 * This is to avoid the possibility of a speculative instruction
150 * fetch, which could be an issue if this memory region
151 * corresponds to a read-sensitive peripheral.
152 */
Antonio Nino Diazdcf9d922017-10-04 16:52:15 +0100153 desc |= xlat_arch_regime_get_xn_desc(ctx->xlat_regime);
Antonio Nino Diazefabaa92017-04-27 13:30:22 +0100154
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000155 } else { /* Normal memory */
156 /*
157 * Always map read-write normal memory as execute-never.
158 * (Trusted Firmware doesn't self-modify its code, therefore
159 * R/W memory is reserved for data storage, which must not be
160 * executable.)
161 * Note that setting the XN bit here is for consistency only.
Antonio Nino Diazefabaa92017-04-27 13:30:22 +0100162 * The function that enables the MMU sets the SCTLR_ELx.WXN bit,
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000163 * which makes any writable memory region to be treated as
164 * execute-never, regardless of the value of the XN bit in the
165 * translation table.
166 *
167 * For read-only memory, rely on the MT_EXECUTE/MT_EXECUTE_NEVER
Antonio Nino Diazdcf9d922017-10-04 16:52:15 +0100168 * attribute to figure out the value of the XN bit. The actual
169 * XN bit(s) to set in the descriptor depends on the context's
170 * translation regime and the policy applied in
171 * xlat_arch_regime_get_xn_desc().
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000172 */
Antonio Nino Diazefabaa92017-04-27 13:30:22 +0100173 if ((attr & MT_RW) || (attr & MT_EXECUTE_NEVER)) {
Antonio Nino Diazdcf9d922017-10-04 16:52:15 +0100174 desc |= xlat_arch_regime_get_xn_desc(ctx->xlat_regime);
Antonio Nino Diazefabaa92017-04-27 13:30:22 +0100175 }
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000176
177 if (mem_type == MT_MEMORY) {
178 desc |= LOWER_ATTRS(ATTR_IWBWA_OWBWA_NTR_INDEX | ISH);
179 } else {
180 assert(mem_type == MT_NON_CACHEABLE);
181 desc |= LOWER_ATTRS(ATTR_NON_CACHEABLE_INDEX | OSH);
182 }
183 }
184
185 return desc;
186}
187
188/*
189 * Enumeration of actions that can be made when mapping table entries depending
190 * on the previous value in that entry and information about the region being
191 * mapped.
192 */
193typedef enum {
194
195 /* Do nothing */
196 ACTION_NONE,
197
198 /* Write a block (or page, if in level 3) entry. */
199 ACTION_WRITE_BLOCK_ENTRY,
200
201 /*
202 * Create a new table and write a table entry pointing to it. Recurse
203 * into it for further processing.
204 */
205 ACTION_CREATE_NEW_TABLE,
206
207 /*
208 * There is a table descriptor in this entry, read it and recurse into
209 * that table for further processing.
210 */
211 ACTION_RECURSE_INTO_TABLE,
212
213} action_t;
214
Antonio Nino Diazac998032017-02-27 17:23:54 +0000215#if PLAT_XLAT_TABLES_DYNAMIC
216
217/*
218 * Recursive function that writes to the translation tables and unmaps the
219 * specified region.
220 */
221static void xlat_tables_unmap_region(xlat_ctx_t *ctx, mmap_region_t *mm,
222 const uintptr_t table_base_va,
223 uint64_t *const table_base,
224 const int table_entries,
Varun Wadekar66231d12017-06-07 09:57:42 -0700225 const unsigned int level)
Antonio Nino Diazac998032017-02-27 17:23:54 +0000226{
227 assert(level >= ctx->base_level && level <= XLAT_TABLE_LEVEL_MAX);
228
229 uint64_t *subtable;
230 uint64_t desc;
231
232 uintptr_t table_idx_va;
233 uintptr_t table_idx_end_va; /* End VA of this entry */
234
235 uintptr_t region_end_va = mm->base_va + mm->size - 1;
236
237 int table_idx;
238
239 if (mm->base_va > table_base_va) {
240 /* Find the first index of the table affected by the region. */
241 table_idx_va = mm->base_va & ~XLAT_BLOCK_MASK(level);
242
243 table_idx = (table_idx_va - table_base_va) >>
244 XLAT_ADDR_SHIFT(level);
245
246 assert(table_idx < table_entries);
247 } else {
248 /* Start from the beginning of the table. */
249 table_idx_va = table_base_va;
250 table_idx = 0;
251 }
252
253 while (table_idx < table_entries) {
254
255 table_idx_end_va = table_idx_va + XLAT_BLOCK_SIZE(level) - 1;
256
257 desc = table_base[table_idx];
258 uint64_t desc_type = desc & DESC_MASK;
259
260 action_t action = ACTION_NONE;
261
262 if ((mm->base_va <= table_idx_va) &&
263 (region_end_va >= table_idx_end_va)) {
264
265 /* Region covers all block */
266
267 if (level == 3) {
268 /*
269 * Last level, only page descriptors allowed,
270 * erase it.
271 */
272 assert(desc_type == PAGE_DESC);
273
274 action = ACTION_WRITE_BLOCK_ENTRY;
275 } else {
276 /*
277 * Other levels can have table descriptors. If
278 * so, recurse into it and erase descriptors
279 * inside it as needed. If there is a block
280 * descriptor, just erase it. If an invalid
281 * descriptor is found, this table isn't
282 * actually mapped, which shouldn't happen.
283 */
284 if (desc_type == TABLE_DESC) {
285 action = ACTION_RECURSE_INTO_TABLE;
286 } else {
287 assert(desc_type == BLOCK_DESC);
288 action = ACTION_WRITE_BLOCK_ENTRY;
289 }
290 }
291
292 } else if ((mm->base_va <= table_idx_end_va) ||
293 (region_end_va >= table_idx_va)) {
294
295 /*
296 * Region partially covers block.
297 *
298 * It can't happen in level 3.
299 *
300 * There must be a table descriptor here, if not there
301 * was a problem when mapping the region.
302 */
303
304 assert(level < 3);
305
306 assert(desc_type == TABLE_DESC);
307
308 action = ACTION_RECURSE_INTO_TABLE;
309 }
310
311 if (action == ACTION_WRITE_BLOCK_ENTRY) {
312
313 table_base[table_idx] = INVALID_DESC;
Antonio Nino Diazdcf9d922017-10-04 16:52:15 +0100314 xlat_arch_tlbi_va_regime(table_idx_va, ctx->xlat_regime);
Antonio Nino Diazac998032017-02-27 17:23:54 +0000315
316 } else if (action == ACTION_RECURSE_INTO_TABLE) {
317
318 subtable = (uint64_t *)(uintptr_t)(desc & TABLE_ADDR_MASK);
319
320 /* Recurse to write into subtable */
321 xlat_tables_unmap_region(ctx, mm, table_idx_va,
322 subtable, XLAT_TABLE_ENTRIES,
323 level + 1);
324
325 /*
326 * If the subtable is now empty, remove its reference.
327 */
328 if (xlat_table_is_empty(ctx, subtable)) {
329 table_base[table_idx] = INVALID_DESC;
Antonio Nino Diazdcf9d922017-10-04 16:52:15 +0100330 xlat_arch_tlbi_va_regime(table_idx_va,
331 ctx->xlat_regime);
Antonio Nino Diazac998032017-02-27 17:23:54 +0000332 }
333
334 } else {
335 assert(action == ACTION_NONE);
336 }
337
338 table_idx++;
339 table_idx_va += XLAT_BLOCK_SIZE(level);
340
341 /* If reached the end of the region, exit */
342 if (region_end_va <= table_idx_va)
343 break;
344 }
345
346 if (level > ctx->base_level)
347 xlat_table_dec_regions_count(ctx, table_base);
348}
349
350#endif /* PLAT_XLAT_TABLES_DYNAMIC */
351
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000352/*
353 * From the given arguments, it decides which action to take when mapping the
354 * specified region.
355 */
356static action_t xlat_tables_map_region_action(const mmap_region_t *mm,
357 const int desc_type, const unsigned long long dest_pa,
Sandrine Bailleux12e86442017-07-19 10:11:13 +0100358 const uintptr_t table_entry_base_va, const unsigned int level)
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000359{
360 uintptr_t mm_end_va = mm->base_va + mm->size - 1;
361 uintptr_t table_entry_end_va =
362 table_entry_base_va + XLAT_BLOCK_SIZE(level) - 1;
363
364 /*
365 * The descriptor types allowed depend on the current table level.
366 */
367
368 if ((mm->base_va <= table_entry_base_va) &&
369 (mm_end_va >= table_entry_end_va)) {
370
371 /*
372 * Table entry is covered by region
373 * --------------------------------
374 *
375 * This means that this table entry can describe the whole
376 * translation with this granularity in principle.
377 */
378
379 if (level == 3) {
380 /*
381 * Last level, only page descriptors are allowed.
382 */
383 if (desc_type == PAGE_DESC) {
384 /*
385 * There's another region mapped here, don't
386 * overwrite.
387 */
388 return ACTION_NONE;
389 } else {
390 assert(desc_type == INVALID_DESC);
391 return ACTION_WRITE_BLOCK_ENTRY;
392 }
393
394 } else {
395
396 /*
397 * Other levels. Table descriptors are allowed. Block
398 * descriptors too, but they have some limitations.
399 */
400
401 if (desc_type == TABLE_DESC) {
402 /* There's already a table, recurse into it. */
403 return ACTION_RECURSE_INTO_TABLE;
404
405 } else if (desc_type == INVALID_DESC) {
406 /*
407 * There's nothing mapped here, create a new
408 * entry.
409 *
410 * Check if the destination granularity allows
411 * us to use a block descriptor or we need a
412 * finer table for it.
413 *
414 * Also, check if the current level allows block
415 * descriptors. If not, create a table instead.
416 */
417 if ((dest_pa & XLAT_BLOCK_MASK(level)) ||
Sandrine Bailleux8f23fa82017-09-28 21:58:12 +0100418 (level < MIN_LVL_BLOCK_DESC) ||
419 (mm->granularity < XLAT_BLOCK_SIZE(level)))
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000420 return ACTION_CREATE_NEW_TABLE;
421 else
422 return ACTION_WRITE_BLOCK_ENTRY;
423
424 } else {
425 /*
426 * There's another region mapped here, don't
427 * overwrite.
428 */
429 assert(desc_type == BLOCK_DESC);
430
431 return ACTION_NONE;
432 }
433 }
434
435 } else if ((mm->base_va <= table_entry_end_va) ||
436 (mm_end_va >= table_entry_base_va)) {
437
438 /*
439 * Region partially covers table entry
440 * -----------------------------------
441 *
442 * This means that this table entry can't describe the whole
443 * translation, a finer table is needed.
444
445 * There cannot be partial block overlaps in level 3. If that
446 * happens, some of the preliminary checks when adding the
447 * mmap region failed to detect that PA and VA must at least be
448 * aligned to PAGE_SIZE.
449 */
450 assert(level < 3);
451
452 if (desc_type == INVALID_DESC) {
453 /*
454 * The block is not fully covered by the region. Create
455 * a new table, recurse into it and try to map the
456 * region with finer granularity.
457 */
458 return ACTION_CREATE_NEW_TABLE;
459
460 } else {
461 assert(desc_type == TABLE_DESC);
462 /*
463 * The block is not fully covered by the region, but
464 * there is already a table here. Recurse into it and
465 * try to map with finer granularity.
466 *
467 * PAGE_DESC for level 3 has the same value as
468 * TABLE_DESC, but this code can't run on a level 3
469 * table because there can't be overlaps in level 3.
470 */
471 return ACTION_RECURSE_INTO_TABLE;
472 }
473 }
474
475 /*
476 * This table entry is outside of the region specified in the arguments,
477 * don't write anything to it.
478 */
479 return ACTION_NONE;
480}
481
482/*
483 * Recursive function that writes to the translation tables and maps the
Antonio Nino Diazac998032017-02-27 17:23:54 +0000484 * specified region. On success, it returns the VA of the last byte that was
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100485 * successfully mapped. On error, it returns the VA of the next entry that
Antonio Nino Diazac998032017-02-27 17:23:54 +0000486 * should have been mapped.
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000487 */
Antonio Nino Diazac998032017-02-27 17:23:54 +0000488static uintptr_t xlat_tables_map_region(xlat_ctx_t *ctx, mmap_region_t *mm,
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000489 const uintptr_t table_base_va,
490 uint64_t *const table_base,
491 const int table_entries,
Varun Wadekar66231d12017-06-07 09:57:42 -0700492 const unsigned int level)
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000493{
494 assert(level >= ctx->base_level && level <= XLAT_TABLE_LEVEL_MAX);
495
496 uintptr_t mm_end_va = mm->base_va + mm->size - 1;
497
498 uintptr_t table_idx_va;
499 unsigned long long table_idx_pa;
500
501 uint64_t *subtable;
502 uint64_t desc;
503
504 int table_idx;
505
506 if (mm->base_va > table_base_va) {
507 /* Find the first index of the table affected by the region. */
508 table_idx_va = mm->base_va & ~XLAT_BLOCK_MASK(level);
509
510 table_idx = (table_idx_va - table_base_va) >>
511 XLAT_ADDR_SHIFT(level);
512
513 assert(table_idx < table_entries);
514 } else {
515 /* Start from the beginning of the table. */
516 table_idx_va = table_base_va;
517 table_idx = 0;
518 }
519
Antonio Nino Diazac998032017-02-27 17:23:54 +0000520#if PLAT_XLAT_TABLES_DYNAMIC
521 if (level > ctx->base_level)
522 xlat_table_inc_regions_count(ctx, table_base);
523#endif
524
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000525 while (table_idx < table_entries) {
526
527 desc = table_base[table_idx];
528
529 table_idx_pa = mm->base_pa + table_idx_va - mm->base_va;
530
531 action_t action = xlat_tables_map_region_action(mm,
532 desc & DESC_MASK, table_idx_pa, table_idx_va, level);
533
534 if (action == ACTION_WRITE_BLOCK_ENTRY) {
535
536 table_base[table_idx] =
Antonio Nino Diaze8811472018-04-17 15:10:18 +0100537 xlat_desc(ctx, (uint32_t)mm->attr, table_idx_pa,
538 level);
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000539
540 } else if (action == ACTION_CREATE_NEW_TABLE) {
541
542 subtable = xlat_table_get_empty(ctx);
Antonio Nino Diazac998032017-02-27 17:23:54 +0000543 if (subtable == NULL) {
544 /* Not enough free tables to map this region */
545 return table_idx_va;
546 }
547
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000548 /* Point to new subtable from this one. */
Antonio Nino Diazac998032017-02-27 17:23:54 +0000549 table_base[table_idx] = TABLE_DESC | (unsigned long)subtable;
550
551 /* Recurse to write into subtable */
552 uintptr_t end_va = xlat_tables_map_region(ctx, mm, table_idx_va,
553 subtable, XLAT_TABLE_ENTRIES,
554 level + 1);
555 if (end_va != table_idx_va + XLAT_BLOCK_SIZE(level) - 1)
556 return end_va;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000557
558 } else if (action == ACTION_RECURSE_INTO_TABLE) {
559
560 subtable = (uint64_t *)(uintptr_t)(desc & TABLE_ADDR_MASK);
561 /* Recurse to write into subtable */
Antonio Nino Diazac998032017-02-27 17:23:54 +0000562 uintptr_t end_va = xlat_tables_map_region(ctx, mm, table_idx_va,
563 subtable, XLAT_TABLE_ENTRIES,
564 level + 1);
565 if (end_va != table_idx_va + XLAT_BLOCK_SIZE(level) - 1)
566 return end_va;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000567
568 } else {
569
570 assert(action == ACTION_NONE);
571
572 }
573
574 table_idx++;
575 table_idx_va += XLAT_BLOCK_SIZE(level);
576
577 /* If reached the end of the region, exit */
578 if (mm_end_va <= table_idx_va)
579 break;
580 }
Antonio Nino Diazac998032017-02-27 17:23:54 +0000581
582 return table_idx_va - 1;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000583}
584
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000585/*
586 * Function that verifies that a region can be mapped.
587 * Returns:
588 * 0: Success, the mapping is allowed.
589 * EINVAL: Invalid values were used as arguments.
590 * ERANGE: The memory limits were surpassed.
591 * ENOMEM: There is not enough memory in the mmap array.
592 * EPERM: Region overlaps another one in an invalid way.
593 */
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100594static int mmap_add_region_check(xlat_ctx_t *ctx, const mmap_region_t *mm)
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000595{
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100596 unsigned long long base_pa = mm->base_pa;
597 uintptr_t base_va = mm->base_va;
598 size_t size = mm->size;
Sandrine Bailleux8f23fa82017-09-28 21:58:12 +0100599 size_t granularity = mm->granularity;
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100600
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000601 unsigned long long end_pa = base_pa + size - 1;
602 uintptr_t end_va = base_va + size - 1;
603
604 if (!IS_PAGE_ALIGNED(base_pa) || !IS_PAGE_ALIGNED(base_va) ||
605 !IS_PAGE_ALIGNED(size))
606 return -EINVAL;
607
Sandrine Bailleux8f23fa82017-09-28 21:58:12 +0100608 if ((granularity != XLAT_BLOCK_SIZE(1)) &&
609 (granularity != XLAT_BLOCK_SIZE(2)) &&
610 (granularity != XLAT_BLOCK_SIZE(3))) {
611 return -EINVAL;
612 }
613
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000614 /* Check for overflows */
615 if ((base_pa > end_pa) || (base_va > end_va))
616 return -ERANGE;
617
618 if ((base_va + (uintptr_t)size - (uintptr_t)1) > ctx->va_max_address)
619 return -ERANGE;
620
621 if ((base_pa + (unsigned long long)size - 1ULL) > ctx->pa_max_address)
622 return -ERANGE;
623
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100624 /* Check that there is space in the ctx->mmap array */
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000625 if (ctx->mmap[ctx->mmap_num - 1].size != 0)
626 return -ENOMEM;
627
628 /* Check for PAs and VAs overlaps with all other regions */
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100629 for (mmap_region_t *mm_cursor = ctx->mmap;
630 mm_cursor->size; ++mm_cursor) {
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000631
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100632 uintptr_t mm_cursor_end_va = mm_cursor->base_va
633 + mm_cursor->size - 1;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000634
635 /*
636 * Check if one of the regions is completely inside the other
637 * one.
638 */
639 int fully_overlapped_va =
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100640 ((base_va >= mm_cursor->base_va) &&
641 (end_va <= mm_cursor_end_va)) ||
642
643 ((mm_cursor->base_va >= base_va) &&
644 (mm_cursor_end_va <= end_va));
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000645
646 /*
647 * Full VA overlaps are only allowed if both regions are
648 * identity mapped (zero offset) or have the same VA to PA
649 * offset. Also, make sure that it's not the exact same area.
Antonio Nino Diazac998032017-02-27 17:23:54 +0000650 * This can only be done with static regions.
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000651 */
652 if (fully_overlapped_va) {
653
Antonio Nino Diazac998032017-02-27 17:23:54 +0000654#if PLAT_XLAT_TABLES_DYNAMIC
Sandrine Bailleux8f23fa82017-09-28 21:58:12 +0100655 if ((mm->attr & MT_DYNAMIC) ||
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100656 (mm_cursor->attr & MT_DYNAMIC))
Antonio Nino Diazac998032017-02-27 17:23:54 +0000657 return -EPERM;
658#endif /* PLAT_XLAT_TABLES_DYNAMIC */
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100659 if ((mm_cursor->base_va - mm_cursor->base_pa) !=
660 (base_va - base_pa))
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000661 return -EPERM;
662
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100663 if ((base_va == mm_cursor->base_va) &&
664 (size == mm_cursor->size))
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000665 return -EPERM;
666
667 } else {
668 /*
669 * If the regions do not have fully overlapping VAs,
670 * then they must have fully separated VAs and PAs.
671 * Partial overlaps are not allowed
672 */
673
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100674 unsigned long long mm_cursor_end_pa =
675 mm_cursor->base_pa + mm_cursor->size - 1;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000676
677 int separated_pa =
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100678 (end_pa < mm_cursor->base_pa) ||
679 (base_pa > mm_cursor_end_pa);
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000680 int separated_va =
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100681 (end_va < mm_cursor->base_va) ||
682 (base_va > mm_cursor_end_va);
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000683
684 if (!(separated_va && separated_pa))
685 return -EPERM;
686 }
687 }
688
689 return 0;
690}
691
Sandrine Bailleux66342932017-07-18 13:26:36 +0100692void mmap_add_region_ctx(xlat_ctx_t *ctx, const mmap_region_t *mm)
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000693{
John Tsichritzisfdd92482018-05-25 09:12:48 +0100694 mmap_region_t *mm_cursor = ctx->mmap, *mm_destination;
Varun Wadekarccbd2e32018-04-03 10:44:41 -0700695 const mmap_region_t *mm_end = ctx->mmap + ctx->mmap_num;
696 mmap_region_t *mm_last;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000697 unsigned long long end_pa = mm->base_pa + mm->size - 1;
698 uintptr_t end_va = mm->base_va + mm->size - 1;
699 int ret;
700
701 /* Ignore empty regions */
702 if (!mm->size)
703 return;
704
Antonio Nino Diazac998032017-02-27 17:23:54 +0000705 /* Static regions must be added before initializing the xlat tables. */
706 assert(!ctx->initialized);
707
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100708 ret = mmap_add_region_check(ctx, mm);
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000709 if (ret != 0) {
710 ERROR("mmap_add_region_check() failed. error %d\n", ret);
711 assert(0);
712 return;
713 }
714
715 /*
716 * Find correct place in mmap to insert new region.
717 *
718 * 1 - Lower region VA end first.
719 * 2 - Smaller region size first.
720 *
721 * VA 0 0xFF
722 *
723 * 1st |------|
724 * 2nd |------------|
725 * 3rd |------|
726 * 4th |---|
727 * 5th |---|
728 * 6th |----------|
729 * 7th |-------------------------------------|
730 *
731 * This is required for overlapping regions only. It simplifies adding
732 * regions with the loop in xlat_tables_init_internal because the outer
733 * ones won't overwrite block or page descriptors of regions added
734 * previously.
Antonio Nino Diazac998032017-02-27 17:23:54 +0000735 *
736 * Overlapping is only allowed for static regions.
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000737 */
738
739 while ((mm_cursor->base_va + mm_cursor->size - 1) < end_va
740 && mm_cursor->size)
741 ++mm_cursor;
742
Yann Gautiereabaea52018-06-14 14:36:20 +0200743 while ((mm_cursor->base_va + mm_cursor->size - 1 == end_va) &&
744 (mm_cursor->size != 0U) && (mm_cursor->size < mm->size))
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000745 ++mm_cursor;
746
Varun Wadekarccbd2e32018-04-03 10:44:41 -0700747 /*
748 * Find the last entry marker in the mmap
749 */
750 mm_last = ctx->mmap;
751 while ((mm_last->size != 0U) && (mm_last < mm_end)) {
752 ++mm_last;
753 }
754
755 /*
756 * Check if we have enough space in the memory mapping table.
757 * This shouldn't happen as we have checked in mmap_add_region_check
758 * that there is free space.
759 */
760 assert(mm_last->size == 0U);
Jeenu Viswambharan58e81482018-04-27 15:06:57 +0100761
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000762 /* Make room for new region by moving other regions up by one place */
John Tsichritzisfdd92482018-05-25 09:12:48 +0100763 mm_destination = mm_cursor + 1;
764 memmove(mm_destination, mm_cursor,
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000765 (uintptr_t)mm_last - (uintptr_t)mm_cursor);
766
767 /*
768 * Check we haven't lost the empty sentinel from the end of the array.
769 * This shouldn't happen as we have checked in mmap_add_region_check
770 * that there is free space.
771 */
Varun Wadekarccbd2e32018-04-03 10:44:41 -0700772 assert(mm_end->size == 0U);
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000773
Douglas Raillardf68d2ed2017-09-12 10:31:49 +0100774 *mm_cursor = *mm;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000775
776 if (end_pa > ctx->max_pa)
777 ctx->max_pa = end_pa;
778 if (end_va > ctx->max_va)
779 ctx->max_va = end_va;
780}
781
Sandrine Bailleux66342932017-07-18 13:26:36 +0100782void mmap_add_ctx(xlat_ctx_t *ctx, const mmap_region_t *mm)
783{
784 while (mm->size) {
785 mmap_add_region_ctx(ctx, mm);
786 mm++;
787 }
788}
789
Antonio Nino Diazac998032017-02-27 17:23:54 +0000790#if PLAT_XLAT_TABLES_DYNAMIC
791
792int mmap_add_dynamic_region_ctx(xlat_ctx_t *ctx, mmap_region_t *mm)
793{
794 mmap_region_t *mm_cursor = ctx->mmap;
795 mmap_region_t *mm_last = mm_cursor + ctx->mmap_num;
796 unsigned long long end_pa = mm->base_pa + mm->size - 1;
797 uintptr_t end_va = mm->base_va + mm->size - 1;
798 int ret;
799
800 /* Nothing to do */
801 if (!mm->size)
802 return 0;
803
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100804 /* Now this region is a dynamic one */
805 mm->attr |= MT_DYNAMIC;
806
807 ret = mmap_add_region_check(ctx, mm);
Antonio Nino Diazac998032017-02-27 17:23:54 +0000808 if (ret != 0)
809 return ret;
810
811 /*
812 * Find the adequate entry in the mmap array in the same way done for
813 * static regions in mmap_add_region_ctx().
814 */
815
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100816 while ((mm_cursor->base_va + mm_cursor->size - 1)
817 < end_va && mm_cursor->size)
Antonio Nino Diazac998032017-02-27 17:23:54 +0000818 ++mm_cursor;
819
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100820 while ((mm_cursor->base_va + mm_cursor->size - 1 == end_va)
821 && (mm_cursor->size < mm->size))
Antonio Nino Diazac998032017-02-27 17:23:54 +0000822 ++mm_cursor;
823
824 /* Make room for new region by moving other regions up by one place */
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100825 memmove(mm_cursor + 1, mm_cursor,
826 (uintptr_t)mm_last - (uintptr_t)mm_cursor);
Antonio Nino Diazac998032017-02-27 17:23:54 +0000827
828 /*
829 * Check we haven't lost the empty sentinal from the end of the array.
830 * This shouldn't happen as we have checked in mmap_add_region_check
831 * that there is free space.
832 */
833 assert(mm_last->size == 0);
834
Douglas Raillardf68d2ed2017-09-12 10:31:49 +0100835 *mm_cursor = *mm;
Antonio Nino Diazac998032017-02-27 17:23:54 +0000836
837 /*
838 * Update the translation tables if the xlat tables are initialized. If
839 * not, this region will be mapped when they are initialized.
840 */
841 if (ctx->initialized) {
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100842 uintptr_t end_va = xlat_tables_map_region(ctx, mm_cursor,
843 0, ctx->base_table, ctx->base_table_entries,
844 ctx->base_level);
Antonio Nino Diazac998032017-02-27 17:23:54 +0000845
846 /* Failed to map, remove mmap entry, unmap and return error. */
847 if (end_va != mm_cursor->base_va + mm_cursor->size - 1) {
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100848 memmove(mm_cursor, mm_cursor + 1,
849 (uintptr_t)mm_last - (uintptr_t)mm_cursor);
Antonio Nino Diazac998032017-02-27 17:23:54 +0000850
851 /*
852 * Check if the mapping function actually managed to map
853 * anything. If not, just return now.
854 */
Antonio Nino Diaz3f518922018-01-05 11:30:36 +0000855 if (mm->base_va >= end_va)
Antonio Nino Diazac998032017-02-27 17:23:54 +0000856 return -ENOMEM;
857
858 /*
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100859 * Something went wrong after mapping some table
860 * entries, undo every change done up to this point.
Antonio Nino Diazac998032017-02-27 17:23:54 +0000861 */
862 mmap_region_t unmap_mm = {
863 .base_pa = 0,
864 .base_va = mm->base_va,
865 .size = end_va - mm->base_va,
866 .attr = 0
867 };
Antonio Nino Diazdcf9d922017-10-04 16:52:15 +0100868 xlat_tables_unmap_region(ctx, &unmap_mm, 0, ctx->base_table,
869 ctx->base_table_entries, ctx->base_level);
Antonio Nino Diazac998032017-02-27 17:23:54 +0000870
871 return -ENOMEM;
872 }
873
874 /*
875 * Make sure that all entries are written to the memory. There
876 * is no need to invalidate entries when mapping dynamic regions
877 * because new table/block/page descriptors only replace old
878 * invalid descriptors, that aren't TLB cached.
879 */
880 dsbishst();
881 }
882
883 if (end_pa > ctx->max_pa)
884 ctx->max_pa = end_pa;
885 if (end_va > ctx->max_va)
886 ctx->max_va = end_va;
887
888 return 0;
889}
890
891/*
892 * Removes the region with given base Virtual Address and size from the given
893 * context.
894 *
895 * Returns:
896 * 0: Success.
897 * EINVAL: Invalid values were used as arguments (region not found).
898 * EPERM: Tried to remove a static region.
899 */
900int mmap_remove_dynamic_region_ctx(xlat_ctx_t *ctx, uintptr_t base_va,
901 size_t size)
902{
903 mmap_region_t *mm = ctx->mmap;
904 mmap_region_t *mm_last = mm + ctx->mmap_num;
905 int update_max_va_needed = 0;
906 int update_max_pa_needed = 0;
907
908 /* Check sanity of mmap array. */
909 assert(mm[ctx->mmap_num].size == 0);
910
911 while (mm->size) {
912 if ((mm->base_va == base_va) && (mm->size == size))
913 break;
914 ++mm;
915 }
916
917 /* Check that the region was found */
918 if (mm->size == 0)
919 return -EINVAL;
920
921 /* If the region is static it can't be removed */
922 if (!(mm->attr & MT_DYNAMIC))
923 return -EPERM;
924
925 /* Check if this region is using the top VAs or PAs. */
926 if ((mm->base_va + mm->size - 1) == ctx->max_va)
927 update_max_va_needed = 1;
928 if ((mm->base_pa + mm->size - 1) == ctx->max_pa)
929 update_max_pa_needed = 1;
930
931 /* Update the translation tables if needed */
932 if (ctx->initialized) {
933 xlat_tables_unmap_region(ctx, mm, 0, ctx->base_table,
934 ctx->base_table_entries,
935 ctx->base_level);
936 xlat_arch_tlbi_va_sync();
937 }
938
939 /* Remove this region by moving the rest down by one place. */
940 memmove(mm, mm + 1, (uintptr_t)mm_last - (uintptr_t)mm);
941
942 /* Check if we need to update the max VAs and PAs */
943 if (update_max_va_needed) {
944 ctx->max_va = 0;
945 mm = ctx->mmap;
946 while (mm->size) {
947 if ((mm->base_va + mm->size - 1) > ctx->max_va)
948 ctx->max_va = mm->base_va + mm->size - 1;
949 ++mm;
950 }
951 }
952
953 if (update_max_pa_needed) {
954 ctx->max_pa = 0;
955 mm = ctx->mmap;
956 while (mm->size) {
957 if ((mm->base_pa + mm->size - 1) > ctx->max_pa)
958 ctx->max_pa = mm->base_pa + mm->size - 1;
959 ++mm;
960 }
961 }
962
963 return 0;
964}
965
966#endif /* PLAT_XLAT_TABLES_DYNAMIC */
967
Sandrine Bailleux66342932017-07-18 13:26:36 +0100968void init_xlat_tables_ctx(xlat_ctx_t *ctx)
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000969{
Antonio Nino Diazdcf9d922017-10-04 16:52:15 +0100970 assert(ctx != NULL);
Sandrine Bailleux66342932017-07-18 13:26:36 +0100971 assert(!ctx->initialized);
Antonio Nino Diazdcf9d922017-10-04 16:52:15 +0100972 assert(ctx->xlat_regime == EL3_REGIME || ctx->xlat_regime == EL1_EL0_REGIME);
973 assert(!is_mmu_enabled_ctx(ctx));
Sandrine Bailleux66342932017-07-18 13:26:36 +0100974
Antonio Nino Diazdcf9d922017-10-04 16:52:15 +0100975 mmap_region_t *mm = ctx->mmap;
Sandrine Bailleux66342932017-07-18 13:26:36 +0100976
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100977 xlat_mmap_print(mm);
Sandrine Bailleux66342932017-07-18 13:26:36 +0100978
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000979 /* All tables must be zeroed before mapping any region. */
980
Varun Wadekar66231d12017-06-07 09:57:42 -0700981 for (unsigned int i = 0; i < ctx->base_table_entries; i++)
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000982 ctx->base_table[i] = INVALID_DESC;
983
Varun Wadekar66231d12017-06-07 09:57:42 -0700984 for (unsigned int j = 0; j < ctx->tables_num; j++) {
Antonio Nino Diazac998032017-02-27 17:23:54 +0000985#if PLAT_XLAT_TABLES_DYNAMIC
986 ctx->tables_mapped_regions[j] = 0;
987#endif
Varun Wadekar66231d12017-06-07 09:57:42 -0700988 for (unsigned int i = 0; i < XLAT_TABLE_ENTRIES; i++)
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000989 ctx->tables[j][i] = INVALID_DESC;
990 }
991
Antonio Nino Diazac998032017-02-27 17:23:54 +0000992 while (mm->size) {
993 uintptr_t end_va = xlat_tables_map_region(ctx, mm, 0, ctx->base_table,
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000994 ctx->base_table_entries, ctx->base_level);
995
Antonio Nino Diazac998032017-02-27 17:23:54 +0000996 if (end_va != mm->base_va + mm->size - 1) {
997 ERROR("Not enough memory to map region:\n"
998 " VA:%p PA:0x%llx size:0x%zx attr:0x%x\n",
999 (void *)mm->base_va, mm->base_pa, mm->size, mm->attr);
1000 panic();
1001 }
1002
1003 mm++;
1004 }
1005
Sandrine Bailleux46c53a22017-07-11 15:11:10 +01001006 assert(ctx->pa_max_address <= xlat_arch_get_max_supported_pa());
Sandrine Bailleux66342932017-07-18 13:26:36 +01001007 assert(ctx->max_va <= ctx->va_max_address);
1008 assert(ctx->max_pa <= ctx->pa_max_address);
1009
Sandrine Bailleuxc5b63772017-05-31 13:31:48 +01001010 ctx->initialized = 1;
1011
1012 xlat_tables_print(ctx);
Sandrine Bailleux66342932017-07-18 13:26:36 +01001013}