blob: e3306e6df78fff4ad1aa8242f21641e72ced12d9 [file] [log] [blame]
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +00001/*
Antonio Nino Diaz96abc222018-01-05 11:30:36 +00002 * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved.
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +00003 *
dp-arm82cb2c12017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +00005 */
6
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +00007#include <arch_helpers.h>
8#include <assert.h>
Antonio Nino Diaz7bb01fb2017-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 Diazfd2299e2018-07-03 11:58:49 +010014#include <utils_def.h>
Sandrine Bailleux8933c342017-05-19 09:59:37 +010015#include <xlat_tables_defs.h>
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +000016#include <xlat_tables_v2.h>
Sandrine Bailleux8933c342017-05-19 09:59:37 +010017
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +000018#include "xlat_tables_private.h"
19
Antonio Nino Diaz0b64f4e2017-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 Wadekar6311f632017-06-07 09:57:42 -070034 for (unsigned int i = 0; i < ctx->tables_num; i++)
Antonio Nino Diaz0b64f4e2017-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 Wadekar6311f632017-06-07 09:57:42 -070050 for (unsigned int i = 0; i < ctx->tables_num; i++)
Antonio Nino Diaz0b64f4e2017-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 Diazfd2299e2018-07-03 11:58:49 +010069/* Returns 0 if the specified table isn't empty, otherwise 1. */
Antonio Nino Diaz0b64f4e2017-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 Diaz7bb01fb2017-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 Diaz0b64f4e2017-02-27 17:23:54 +000085#endif /* PLAT_XLAT_TABLES_DYNAMIC */
86
Antonio Nino Diaz609c9192017-10-04 16:52:15 +010087/*
88 * Returns a block/page table descriptor for the given level and attributes.
89 */
Antonio Nino Diazfd2299e2018-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 Diaz7bb01fb2017-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 /*
Antonio Nino Diaz1dd6c052018-07-12 15:54:10 +0100106 * Always set the access flag, as this library assumes access flag
107 * faults aren't managed.
108 */
109 desc |= LOWER_ATTRS(ACCESS_FLAG);
110 /*
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000111 * Deduce other fields of the descriptor based on the MT_NS and MT_RW
112 * memory region attributes.
113 */
114 desc |= (attr & MT_NS) ? LOWER_ATTRS(NS) : 0;
115 desc |= (attr & MT_RW) ? LOWER_ATTRS(AP_RW) : LOWER_ATTRS(AP_RO);
Antonio Nino Diaz609c9192017-10-04 16:52:15 +0100116
117 /*
118 * Do not allow unprivileged access when the mapping is for a privileged
119 * EL. For translation regimes that do not have mappings for access for
120 * lower exception levels, set AP[2] to AP_NO_ACCESS_UNPRIVILEGED.
121 */
122 if (ctx->xlat_regime == EL1_EL0_REGIME) {
123 if (attr & MT_USER) {
124 /* EL0 mapping requested, so we give User access */
125 desc |= LOWER_ATTRS(AP_ACCESS_UNPRIVILEGED);
126 } else {
127 /* EL1 mapping requested, no User access granted */
128 desc |= LOWER_ATTRS(AP_NO_ACCESS_UNPRIVILEGED);
129 }
130 } else {
131 assert(ctx->xlat_regime == EL3_REGIME);
Antonio Nino Diaz01c0a382018-04-26 12:59:08 +0100132 desc |= LOWER_ATTRS(AP_ONE_VA_RANGE_RES1);
Antonio Nino Diaz609c9192017-10-04 16:52:15 +0100133 }
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000134
135 /*
136 * Deduce shareability domain and executability of the memory region
137 * from the memory type of the attributes (MT_TYPE).
138 *
139 * Data accesses to device memory and non-cacheable normal memory are
140 * coherent for all observers in the system, and correspondingly are
141 * always treated as being Outer Shareable. Therefore, for these 2 types
142 * of memory, it is not strictly needed to set the shareability field
143 * in the translation tables.
144 */
145 mem_type = MT_TYPE(attr);
146 if (mem_type == MT_DEVICE) {
147 desc |= LOWER_ATTRS(ATTR_DEVICE_INDEX | OSH);
148 /*
149 * Always map device memory as execute-never.
150 * This is to avoid the possibility of a speculative instruction
151 * fetch, which could be an issue if this memory region
152 * corresponds to a read-sensitive peripheral.
153 */
Antonio Nino Diaz609c9192017-10-04 16:52:15 +0100154 desc |= xlat_arch_regime_get_xn_desc(ctx->xlat_regime);
Antonio Nino Diaza5640252017-04-27 13:30:22 +0100155
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000156 } else { /* Normal memory */
157 /*
158 * Always map read-write normal memory as execute-never.
Antonio Nino Diaz1dd6c052018-07-12 15:54:10 +0100159 * This library assumes that it is used by software that does
160 * not self-modify its code, therefore R/W memory is reserved
161 * for data storage, which must not be executable.
162 *
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000163 * Note that setting the XN bit here is for consistency only.
Antonio Nino Diaza5640252017-04-27 13:30:22 +0100164 * The function that enables the MMU sets the SCTLR_ELx.WXN bit,
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000165 * which makes any writable memory region to be treated as
166 * execute-never, regardless of the value of the XN bit in the
167 * translation table.
168 *
169 * For read-only memory, rely on the MT_EXECUTE/MT_EXECUTE_NEVER
Antonio Nino Diaz609c9192017-10-04 16:52:15 +0100170 * attribute to figure out the value of the XN bit. The actual
171 * XN bit(s) to set in the descriptor depends on the context's
172 * translation regime and the policy applied in
173 * xlat_arch_regime_get_xn_desc().
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000174 */
Antonio Nino Diaza5640252017-04-27 13:30:22 +0100175 if ((attr & MT_RW) || (attr & MT_EXECUTE_NEVER)) {
Antonio Nino Diaz609c9192017-10-04 16:52:15 +0100176 desc |= xlat_arch_regime_get_xn_desc(ctx->xlat_regime);
Antonio Nino Diaza5640252017-04-27 13:30:22 +0100177 }
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000178
179 if (mem_type == MT_MEMORY) {
180 desc |= LOWER_ATTRS(ATTR_IWBWA_OWBWA_NTR_INDEX | ISH);
181 } else {
182 assert(mem_type == MT_NON_CACHEABLE);
183 desc |= LOWER_ATTRS(ATTR_NON_CACHEABLE_INDEX | OSH);
184 }
185 }
186
187 return desc;
188}
189
190/*
191 * Enumeration of actions that can be made when mapping table entries depending
192 * on the previous value in that entry and information about the region being
193 * mapped.
194 */
195typedef enum {
196
197 /* Do nothing */
198 ACTION_NONE,
199
200 /* Write a block (or page, if in level 3) entry. */
201 ACTION_WRITE_BLOCK_ENTRY,
202
203 /*
204 * Create a new table and write a table entry pointing to it. Recurse
205 * into it for further processing.
206 */
207 ACTION_CREATE_NEW_TABLE,
208
209 /*
210 * There is a table descriptor in this entry, read it and recurse into
211 * that table for further processing.
212 */
213 ACTION_RECURSE_INTO_TABLE,
214
215} action_t;
216
Antonio Nino Diaz0b64f4e2017-02-27 17:23:54 +0000217#if PLAT_XLAT_TABLES_DYNAMIC
218
219/*
220 * Recursive function that writes to the translation tables and unmaps the
221 * specified region.
222 */
223static void xlat_tables_unmap_region(xlat_ctx_t *ctx, mmap_region_t *mm,
224 const uintptr_t table_base_va,
225 uint64_t *const table_base,
226 const int table_entries,
Varun Wadekar6311f632017-06-07 09:57:42 -0700227 const unsigned int level)
Antonio Nino Diaz0b64f4e2017-02-27 17:23:54 +0000228{
229 assert(level >= ctx->base_level && level <= XLAT_TABLE_LEVEL_MAX);
230
231 uint64_t *subtable;
232 uint64_t desc;
233
234 uintptr_t table_idx_va;
235 uintptr_t table_idx_end_va; /* End VA of this entry */
236
237 uintptr_t region_end_va = mm->base_va + mm->size - 1;
238
239 int table_idx;
240
241 if (mm->base_va > table_base_va) {
242 /* Find the first index of the table affected by the region. */
243 table_idx_va = mm->base_va & ~XLAT_BLOCK_MASK(level);
244
245 table_idx = (table_idx_va - table_base_va) >>
246 XLAT_ADDR_SHIFT(level);
247
248 assert(table_idx < table_entries);
249 } else {
250 /* Start from the beginning of the table. */
251 table_idx_va = table_base_va;
252 table_idx = 0;
253 }
254
255 while (table_idx < table_entries) {
256
257 table_idx_end_va = table_idx_va + XLAT_BLOCK_SIZE(level) - 1;
258
259 desc = table_base[table_idx];
260 uint64_t desc_type = desc & DESC_MASK;
261
262 action_t action = ACTION_NONE;
263
264 if ((mm->base_va <= table_idx_va) &&
265 (region_end_va >= table_idx_end_va)) {
266
267 /* Region covers all block */
268
269 if (level == 3) {
270 /*
271 * Last level, only page descriptors allowed,
272 * erase it.
273 */
274 assert(desc_type == PAGE_DESC);
275
276 action = ACTION_WRITE_BLOCK_ENTRY;
277 } else {
278 /*
279 * Other levels can have table descriptors. If
280 * so, recurse into it and erase descriptors
281 * inside it as needed. If there is a block
282 * descriptor, just erase it. If an invalid
283 * descriptor is found, this table isn't
284 * actually mapped, which shouldn't happen.
285 */
286 if (desc_type == TABLE_DESC) {
287 action = ACTION_RECURSE_INTO_TABLE;
288 } else {
289 assert(desc_type == BLOCK_DESC);
290 action = ACTION_WRITE_BLOCK_ENTRY;
291 }
292 }
293
294 } else if ((mm->base_va <= table_idx_end_va) ||
295 (region_end_va >= table_idx_va)) {
296
297 /*
298 * Region partially covers block.
299 *
300 * It can't happen in level 3.
301 *
302 * There must be a table descriptor here, if not there
303 * was a problem when mapping the region.
304 */
305
306 assert(level < 3);
307
308 assert(desc_type == TABLE_DESC);
309
310 action = ACTION_RECURSE_INTO_TABLE;
311 }
312
313 if (action == ACTION_WRITE_BLOCK_ENTRY) {
314
315 table_base[table_idx] = INVALID_DESC;
Antonio Nino Diaz8d164bc2018-07-11 09:46:45 +0100316 xlat_arch_tlbi_va(table_idx_va, ctx->xlat_regime);
Antonio Nino Diaz0b64f4e2017-02-27 17:23:54 +0000317
318 } else if (action == ACTION_RECURSE_INTO_TABLE) {
319
320 subtable = (uint64_t *)(uintptr_t)(desc & TABLE_ADDR_MASK);
321
322 /* Recurse to write into subtable */
323 xlat_tables_unmap_region(ctx, mm, table_idx_va,
324 subtable, XLAT_TABLE_ENTRIES,
325 level + 1);
326
327 /*
328 * If the subtable is now empty, remove its reference.
329 */
330 if (xlat_table_is_empty(ctx, subtable)) {
331 table_base[table_idx] = INVALID_DESC;
Antonio Nino Diaz8d164bc2018-07-11 09:46:45 +0100332 xlat_arch_tlbi_va(table_idx_va,
333 ctx->xlat_regime);
Antonio Nino Diaz0b64f4e2017-02-27 17:23:54 +0000334 }
335
336 } else {
337 assert(action == ACTION_NONE);
338 }
339
340 table_idx++;
341 table_idx_va += XLAT_BLOCK_SIZE(level);
342
343 /* If reached the end of the region, exit */
344 if (region_end_va <= table_idx_va)
345 break;
346 }
347
348 if (level > ctx->base_level)
349 xlat_table_dec_regions_count(ctx, table_base);
350}
351
352#endif /* PLAT_XLAT_TABLES_DYNAMIC */
353
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000354/*
355 * From the given arguments, it decides which action to take when mapping the
356 * specified region.
357 */
358static action_t xlat_tables_map_region_action(const mmap_region_t *mm,
359 const int desc_type, const unsigned long long dest_pa,
Sandrine Bailleux00442312017-07-19 10:11:13 +0100360 const uintptr_t table_entry_base_va, const unsigned int level)
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000361{
362 uintptr_t mm_end_va = mm->base_va + mm->size - 1;
363 uintptr_t table_entry_end_va =
364 table_entry_base_va + XLAT_BLOCK_SIZE(level) - 1;
365
366 /*
367 * The descriptor types allowed depend on the current table level.
368 */
369
370 if ((mm->base_va <= table_entry_base_va) &&
371 (mm_end_va >= table_entry_end_va)) {
372
373 /*
374 * Table entry is covered by region
375 * --------------------------------
376 *
377 * This means that this table entry can describe the whole
378 * translation with this granularity in principle.
379 */
380
381 if (level == 3) {
382 /*
383 * Last level, only page descriptors are allowed.
384 */
385 if (desc_type == PAGE_DESC) {
386 /*
387 * There's another region mapped here, don't
388 * overwrite.
389 */
390 return ACTION_NONE;
391 } else {
392 assert(desc_type == INVALID_DESC);
393 return ACTION_WRITE_BLOCK_ENTRY;
394 }
395
396 } else {
397
398 /*
399 * Other levels. Table descriptors are allowed. Block
400 * descriptors too, but they have some limitations.
401 */
402
403 if (desc_type == TABLE_DESC) {
404 /* There's already a table, recurse into it. */
405 return ACTION_RECURSE_INTO_TABLE;
406
407 } else if (desc_type == INVALID_DESC) {
408 /*
409 * There's nothing mapped here, create a new
410 * entry.
411 *
412 * Check if the destination granularity allows
413 * us to use a block descriptor or we need a
414 * finer table for it.
415 *
416 * Also, check if the current level allows block
417 * descriptors. If not, create a table instead.
418 */
419 if ((dest_pa & XLAT_BLOCK_MASK(level)) ||
Sandrine Bailleuxfdb19642017-09-28 21:58:12 +0100420 (level < MIN_LVL_BLOCK_DESC) ||
421 (mm->granularity < XLAT_BLOCK_SIZE(level)))
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000422 return ACTION_CREATE_NEW_TABLE;
423 else
424 return ACTION_WRITE_BLOCK_ENTRY;
425
426 } else {
427 /*
428 * There's another region mapped here, don't
429 * overwrite.
430 */
431 assert(desc_type == BLOCK_DESC);
432
433 return ACTION_NONE;
434 }
435 }
436
437 } else if ((mm->base_va <= table_entry_end_va) ||
438 (mm_end_va >= table_entry_base_va)) {
439
440 /*
441 * Region partially covers table entry
442 * -----------------------------------
443 *
444 * This means that this table entry can't describe the whole
445 * translation, a finer table is needed.
446
447 * There cannot be partial block overlaps in level 3. If that
448 * happens, some of the preliminary checks when adding the
449 * mmap region failed to detect that PA and VA must at least be
450 * aligned to PAGE_SIZE.
451 */
452 assert(level < 3);
453
454 if (desc_type == INVALID_DESC) {
455 /*
456 * The block is not fully covered by the region. Create
457 * a new table, recurse into it and try to map the
458 * region with finer granularity.
459 */
460 return ACTION_CREATE_NEW_TABLE;
461
462 } else {
463 assert(desc_type == TABLE_DESC);
464 /*
465 * The block is not fully covered by the region, but
466 * there is already a table here. Recurse into it and
467 * try to map with finer granularity.
468 *
469 * PAGE_DESC for level 3 has the same value as
470 * TABLE_DESC, but this code can't run on a level 3
471 * table because there can't be overlaps in level 3.
472 */
473 return ACTION_RECURSE_INTO_TABLE;
474 }
475 }
476
477 /*
478 * This table entry is outside of the region specified in the arguments,
479 * don't write anything to it.
480 */
481 return ACTION_NONE;
482}
483
484/*
485 * Recursive function that writes to the translation tables and maps the
Antonio Nino Diaz0b64f4e2017-02-27 17:23:54 +0000486 * specified region. On success, it returns the VA of the last byte that was
Antonio Nino Diazfd2299e2018-07-03 11:58:49 +0100487 * successfully mapped. On error, it returns the VA of the next entry that
Antonio Nino Diaz0b64f4e2017-02-27 17:23:54 +0000488 * should have been mapped.
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000489 */
Antonio Nino Diaz0b64f4e2017-02-27 17:23:54 +0000490static uintptr_t xlat_tables_map_region(xlat_ctx_t *ctx, mmap_region_t *mm,
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000491 const uintptr_t table_base_va,
492 uint64_t *const table_base,
493 const int table_entries,
Varun Wadekar6311f632017-06-07 09:57:42 -0700494 const unsigned int level)
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000495{
496 assert(level >= ctx->base_level && level <= XLAT_TABLE_LEVEL_MAX);
497
498 uintptr_t mm_end_va = mm->base_va + mm->size - 1;
499
500 uintptr_t table_idx_va;
501 unsigned long long table_idx_pa;
502
503 uint64_t *subtable;
504 uint64_t desc;
505
506 int table_idx;
507
508 if (mm->base_va > table_base_va) {
509 /* Find the first index of the table affected by the region. */
510 table_idx_va = mm->base_va & ~XLAT_BLOCK_MASK(level);
511
512 table_idx = (table_idx_va - table_base_va) >>
513 XLAT_ADDR_SHIFT(level);
514
515 assert(table_idx < table_entries);
516 } else {
517 /* Start from the beginning of the table. */
518 table_idx_va = table_base_va;
519 table_idx = 0;
520 }
521
Antonio Nino Diaz0b64f4e2017-02-27 17:23:54 +0000522#if PLAT_XLAT_TABLES_DYNAMIC
523 if (level > ctx->base_level)
524 xlat_table_inc_regions_count(ctx, table_base);
525#endif
526
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000527 while (table_idx < table_entries) {
528
529 desc = table_base[table_idx];
530
531 table_idx_pa = mm->base_pa + table_idx_va - mm->base_va;
532
533 action_t action = xlat_tables_map_region_action(mm,
534 desc & DESC_MASK, table_idx_pa, table_idx_va, level);
535
536 if (action == ACTION_WRITE_BLOCK_ENTRY) {
537
538 table_base[table_idx] =
Antonio Nino Diazb3323cd2018-04-17 15:10:18 +0100539 xlat_desc(ctx, (uint32_t)mm->attr, table_idx_pa,
540 level);
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000541
542 } else if (action == ACTION_CREATE_NEW_TABLE) {
543
544 subtable = xlat_table_get_empty(ctx);
Antonio Nino Diaz0b64f4e2017-02-27 17:23:54 +0000545 if (subtable == NULL) {
546 /* Not enough free tables to map this region */
547 return table_idx_va;
548 }
549
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000550 /* Point to new subtable from this one. */
Antonio Nino Diaz0b64f4e2017-02-27 17:23:54 +0000551 table_base[table_idx] = TABLE_DESC | (unsigned long)subtable;
552
553 /* Recurse to write into subtable */
554 uintptr_t end_va = xlat_tables_map_region(ctx, mm, table_idx_va,
555 subtable, XLAT_TABLE_ENTRIES,
556 level + 1);
557 if (end_va != table_idx_va + XLAT_BLOCK_SIZE(level) - 1)
558 return end_va;
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000559
560 } else if (action == ACTION_RECURSE_INTO_TABLE) {
561
562 subtable = (uint64_t *)(uintptr_t)(desc & TABLE_ADDR_MASK);
563 /* Recurse to write into subtable */
Antonio Nino Diaz0b64f4e2017-02-27 17:23:54 +0000564 uintptr_t end_va = xlat_tables_map_region(ctx, mm, table_idx_va,
565 subtable, XLAT_TABLE_ENTRIES,
566 level + 1);
567 if (end_va != table_idx_va + XLAT_BLOCK_SIZE(level) - 1)
568 return end_va;
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000569
570 } else {
571
572 assert(action == ACTION_NONE);
573
574 }
575
576 table_idx++;
577 table_idx_va += XLAT_BLOCK_SIZE(level);
578
579 /* If reached the end of the region, exit */
580 if (mm_end_va <= table_idx_va)
581 break;
582 }
Antonio Nino Diaz0b64f4e2017-02-27 17:23:54 +0000583
584 return table_idx_va - 1;
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000585}
586
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000587/*
588 * Function that verifies that a region can be mapped.
589 * Returns:
590 * 0: Success, the mapping is allowed.
591 * EINVAL: Invalid values were used as arguments.
592 * ERANGE: The memory limits were surpassed.
593 * ENOMEM: There is not enough memory in the mmap array.
594 * EPERM: Region overlaps another one in an invalid way.
595 */
Douglas Raillarddf312c52017-09-21 08:42:21 +0100596static int mmap_add_region_check(xlat_ctx_t *ctx, const mmap_region_t *mm)
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000597{
Douglas Raillarddf312c52017-09-21 08:42:21 +0100598 unsigned long long base_pa = mm->base_pa;
599 uintptr_t base_va = mm->base_va;
600 size_t size = mm->size;
Sandrine Bailleuxfdb19642017-09-28 21:58:12 +0100601 size_t granularity = mm->granularity;
Douglas Raillarddf312c52017-09-21 08:42:21 +0100602
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000603 unsigned long long end_pa = base_pa + size - 1;
604 uintptr_t end_va = base_va + size - 1;
605
606 if (!IS_PAGE_ALIGNED(base_pa) || !IS_PAGE_ALIGNED(base_va) ||
607 !IS_PAGE_ALIGNED(size))
608 return -EINVAL;
609
Sandrine Bailleuxfdb19642017-09-28 21:58:12 +0100610 if ((granularity != XLAT_BLOCK_SIZE(1)) &&
611 (granularity != XLAT_BLOCK_SIZE(2)) &&
612 (granularity != XLAT_BLOCK_SIZE(3))) {
613 return -EINVAL;
614 }
615
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000616 /* Check for overflows */
617 if ((base_pa > end_pa) || (base_va > end_va))
618 return -ERANGE;
619
620 if ((base_va + (uintptr_t)size - (uintptr_t)1) > ctx->va_max_address)
621 return -ERANGE;
622
623 if ((base_pa + (unsigned long long)size - 1ULL) > ctx->pa_max_address)
624 return -ERANGE;
625
Douglas Raillarddf312c52017-09-21 08:42:21 +0100626 /* Check that there is space in the ctx->mmap array */
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000627 if (ctx->mmap[ctx->mmap_num - 1].size != 0)
628 return -ENOMEM;
629
630 /* Check for PAs and VAs overlaps with all other regions */
Douglas Raillarddf312c52017-09-21 08:42:21 +0100631 for (mmap_region_t *mm_cursor = ctx->mmap;
632 mm_cursor->size; ++mm_cursor) {
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000633
Douglas Raillarddf312c52017-09-21 08:42:21 +0100634 uintptr_t mm_cursor_end_va = mm_cursor->base_va
635 + mm_cursor->size - 1;
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000636
637 /*
638 * Check if one of the regions is completely inside the other
639 * one.
640 */
641 int fully_overlapped_va =
Douglas Raillarddf312c52017-09-21 08:42:21 +0100642 ((base_va >= mm_cursor->base_va) &&
643 (end_va <= mm_cursor_end_va)) ||
644
645 ((mm_cursor->base_va >= base_va) &&
646 (mm_cursor_end_va <= end_va));
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000647
648 /*
649 * Full VA overlaps are only allowed if both regions are
650 * identity mapped (zero offset) or have the same VA to PA
651 * offset. Also, make sure that it's not the exact same area.
Antonio Nino Diaz0b64f4e2017-02-27 17:23:54 +0000652 * This can only be done with static regions.
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000653 */
654 if (fully_overlapped_va) {
655
Antonio Nino Diaz0b64f4e2017-02-27 17:23:54 +0000656#if PLAT_XLAT_TABLES_DYNAMIC
Sandrine Bailleuxfdb19642017-09-28 21:58:12 +0100657 if ((mm->attr & MT_DYNAMIC) ||
Douglas Raillarddf312c52017-09-21 08:42:21 +0100658 (mm_cursor->attr & MT_DYNAMIC))
Antonio Nino Diaz0b64f4e2017-02-27 17:23:54 +0000659 return -EPERM;
660#endif /* PLAT_XLAT_TABLES_DYNAMIC */
Douglas Raillarddf312c52017-09-21 08:42:21 +0100661 if ((mm_cursor->base_va - mm_cursor->base_pa) !=
662 (base_va - base_pa))
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000663 return -EPERM;
664
Douglas Raillarddf312c52017-09-21 08:42:21 +0100665 if ((base_va == mm_cursor->base_va) &&
666 (size == mm_cursor->size))
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000667 return -EPERM;
668
669 } else {
670 /*
671 * If the regions do not have fully overlapping VAs,
672 * then they must have fully separated VAs and PAs.
673 * Partial overlaps are not allowed
674 */
675
Douglas Raillarddf312c52017-09-21 08:42:21 +0100676 unsigned long long mm_cursor_end_pa =
677 mm_cursor->base_pa + mm_cursor->size - 1;
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000678
679 int separated_pa =
Douglas Raillarddf312c52017-09-21 08:42:21 +0100680 (end_pa < mm_cursor->base_pa) ||
681 (base_pa > mm_cursor_end_pa);
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000682 int separated_va =
Douglas Raillarddf312c52017-09-21 08:42:21 +0100683 (end_va < mm_cursor->base_va) ||
684 (base_va > mm_cursor_end_va);
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000685
686 if (!(separated_va && separated_pa))
687 return -EPERM;
688 }
689 }
690
691 return 0;
692}
693
Sandrine Bailleuxa9ad8482017-07-18 13:26:36 +0100694void mmap_add_region_ctx(xlat_ctx_t *ctx, const mmap_region_t *mm)
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000695{
John Tsichritzisbdcd33a2018-05-25 09:12:48 +0100696 mmap_region_t *mm_cursor = ctx->mmap, *mm_destination;
Varun Wadekar0ed32232018-04-03 10:44:41 -0700697 const mmap_region_t *mm_end = ctx->mmap + ctx->mmap_num;
698 mmap_region_t *mm_last;
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000699 unsigned long long end_pa = mm->base_pa + mm->size - 1;
700 uintptr_t end_va = mm->base_va + mm->size - 1;
701 int ret;
702
703 /* Ignore empty regions */
704 if (!mm->size)
705 return;
706
Antonio Nino Diaz0b64f4e2017-02-27 17:23:54 +0000707 /* Static regions must be added before initializing the xlat tables. */
708 assert(!ctx->initialized);
709
Douglas Raillarddf312c52017-09-21 08:42:21 +0100710 ret = mmap_add_region_check(ctx, mm);
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000711 if (ret != 0) {
712 ERROR("mmap_add_region_check() failed. error %d\n", ret);
713 assert(0);
714 return;
715 }
716
717 /*
718 * Find correct place in mmap to insert new region.
719 *
720 * 1 - Lower region VA end first.
721 * 2 - Smaller region size first.
722 *
723 * VA 0 0xFF
724 *
725 * 1st |------|
726 * 2nd |------------|
727 * 3rd |------|
728 * 4th |---|
729 * 5th |---|
730 * 6th |----------|
731 * 7th |-------------------------------------|
732 *
733 * This is required for overlapping regions only. It simplifies adding
734 * regions with the loop in xlat_tables_init_internal because the outer
735 * ones won't overwrite block or page descriptors of regions added
736 * previously.
Antonio Nino Diaz0b64f4e2017-02-27 17:23:54 +0000737 *
738 * Overlapping is only allowed for static regions.
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000739 */
740
741 while ((mm_cursor->base_va + mm_cursor->size - 1) < end_va
742 && mm_cursor->size)
743 ++mm_cursor;
744
Yann Gautier75df6262018-06-14 14:36:20 +0200745 while ((mm_cursor->base_va + mm_cursor->size - 1 == end_va) &&
746 (mm_cursor->size != 0U) && (mm_cursor->size < mm->size))
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000747 ++mm_cursor;
748
Varun Wadekar0ed32232018-04-03 10:44:41 -0700749 /*
750 * Find the last entry marker in the mmap
751 */
752 mm_last = ctx->mmap;
753 while ((mm_last->size != 0U) && (mm_last < mm_end)) {
754 ++mm_last;
755 }
756
757 /*
758 * Check if we have enough space in the memory mapping table.
759 * This shouldn't happen as we have checked in mmap_add_region_check
760 * that there is free space.
761 */
762 assert(mm_last->size == 0U);
Jeenu Viswambharan0cc7aa82018-04-27 15:06:57 +0100763
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000764 /* Make room for new region by moving other regions up by one place */
John Tsichritzisbdcd33a2018-05-25 09:12:48 +0100765 mm_destination = mm_cursor + 1;
766 memmove(mm_destination, mm_cursor,
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000767 (uintptr_t)mm_last - (uintptr_t)mm_cursor);
768
769 /*
770 * Check we haven't lost the empty sentinel from the end of the array.
771 * This shouldn't happen as we have checked in mmap_add_region_check
772 * that there is free space.
773 */
Varun Wadekar0ed32232018-04-03 10:44:41 -0700774 assert(mm_end->size == 0U);
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000775
Douglas Raillard73addb72017-09-12 10:31:49 +0100776 *mm_cursor = *mm;
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000777
778 if (end_pa > ctx->max_pa)
779 ctx->max_pa = end_pa;
780 if (end_va > ctx->max_va)
781 ctx->max_va = end_va;
782}
783
Sandrine Bailleuxa9ad8482017-07-18 13:26:36 +0100784void mmap_add_ctx(xlat_ctx_t *ctx, const mmap_region_t *mm)
785{
786 while (mm->size) {
787 mmap_add_region_ctx(ctx, mm);
788 mm++;
789 }
790}
791
Antonio Nino Diaz0b64f4e2017-02-27 17:23:54 +0000792#if PLAT_XLAT_TABLES_DYNAMIC
793
794int mmap_add_dynamic_region_ctx(xlat_ctx_t *ctx, mmap_region_t *mm)
795{
796 mmap_region_t *mm_cursor = ctx->mmap;
797 mmap_region_t *mm_last = mm_cursor + ctx->mmap_num;
798 unsigned long long end_pa = mm->base_pa + mm->size - 1;
799 uintptr_t end_va = mm->base_va + mm->size - 1;
800 int ret;
801
802 /* Nothing to do */
803 if (!mm->size)
804 return 0;
805
Douglas Raillarddf312c52017-09-21 08:42:21 +0100806 /* Now this region is a dynamic one */
807 mm->attr |= MT_DYNAMIC;
808
809 ret = mmap_add_region_check(ctx, mm);
Antonio Nino Diaz0b64f4e2017-02-27 17:23:54 +0000810 if (ret != 0)
811 return ret;
812
813 /*
814 * Find the adequate entry in the mmap array in the same way done for
815 * static regions in mmap_add_region_ctx().
816 */
817
Douglas Raillarddf312c52017-09-21 08:42:21 +0100818 while ((mm_cursor->base_va + mm_cursor->size - 1)
819 < end_va && mm_cursor->size)
Antonio Nino Diaz0b64f4e2017-02-27 17:23:54 +0000820 ++mm_cursor;
821
Douglas Raillarddf312c52017-09-21 08:42:21 +0100822 while ((mm_cursor->base_va + mm_cursor->size - 1 == end_va)
823 && (mm_cursor->size < mm->size))
Antonio Nino Diaz0b64f4e2017-02-27 17:23:54 +0000824 ++mm_cursor;
825
826 /* Make room for new region by moving other regions up by one place */
Douglas Raillarddf312c52017-09-21 08:42:21 +0100827 memmove(mm_cursor + 1, mm_cursor,
828 (uintptr_t)mm_last - (uintptr_t)mm_cursor);
Antonio Nino Diaz0b64f4e2017-02-27 17:23:54 +0000829
830 /*
831 * Check we haven't lost the empty sentinal from the end of the array.
832 * This shouldn't happen as we have checked in mmap_add_region_check
833 * that there is free space.
834 */
835 assert(mm_last->size == 0);
836
Douglas Raillard73addb72017-09-12 10:31:49 +0100837 *mm_cursor = *mm;
Antonio Nino Diaz0b64f4e2017-02-27 17:23:54 +0000838
839 /*
840 * Update the translation tables if the xlat tables are initialized. If
841 * not, this region will be mapped when they are initialized.
842 */
843 if (ctx->initialized) {
Douglas Raillarddf312c52017-09-21 08:42:21 +0100844 uintptr_t end_va = xlat_tables_map_region(ctx, mm_cursor,
845 0, ctx->base_table, ctx->base_table_entries,
846 ctx->base_level);
Antonio Nino Diaz0b64f4e2017-02-27 17:23:54 +0000847
848 /* Failed to map, remove mmap entry, unmap and return error. */
849 if (end_va != mm_cursor->base_va + mm_cursor->size - 1) {
Douglas Raillarddf312c52017-09-21 08:42:21 +0100850 memmove(mm_cursor, mm_cursor + 1,
851 (uintptr_t)mm_last - (uintptr_t)mm_cursor);
Antonio Nino Diaz0b64f4e2017-02-27 17:23:54 +0000852
853 /*
854 * Check if the mapping function actually managed to map
855 * anything. If not, just return now.
856 */
Antonio Nino Diaz96abc222018-01-05 11:30:36 +0000857 if (mm->base_va >= end_va)
Antonio Nino Diaz0b64f4e2017-02-27 17:23:54 +0000858 return -ENOMEM;
859
860 /*
Douglas Raillarddf312c52017-09-21 08:42:21 +0100861 * Something went wrong after mapping some table
862 * entries, undo every change done up to this point.
Antonio Nino Diaz0b64f4e2017-02-27 17:23:54 +0000863 */
864 mmap_region_t unmap_mm = {
865 .base_pa = 0,
866 .base_va = mm->base_va,
867 .size = end_va - mm->base_va,
868 .attr = 0
869 };
Antonio Nino Diaz609c9192017-10-04 16:52:15 +0100870 xlat_tables_unmap_region(ctx, &unmap_mm, 0, ctx->base_table,
871 ctx->base_table_entries, ctx->base_level);
Antonio Nino Diaz0b64f4e2017-02-27 17:23:54 +0000872
873 return -ENOMEM;
874 }
875
876 /*
877 * Make sure that all entries are written to the memory. There
878 * is no need to invalidate entries when mapping dynamic regions
879 * because new table/block/page descriptors only replace old
880 * invalid descriptors, that aren't TLB cached.
881 */
882 dsbishst();
883 }
884
885 if (end_pa > ctx->max_pa)
886 ctx->max_pa = end_pa;
887 if (end_va > ctx->max_va)
888 ctx->max_va = end_va;
889
890 return 0;
891}
892
893/*
894 * Removes the region with given base Virtual Address and size from the given
895 * context.
896 *
897 * Returns:
898 * 0: Success.
899 * EINVAL: Invalid values were used as arguments (region not found).
900 * EPERM: Tried to remove a static region.
901 */
902int mmap_remove_dynamic_region_ctx(xlat_ctx_t *ctx, uintptr_t base_va,
903 size_t size)
904{
905 mmap_region_t *mm = ctx->mmap;
906 mmap_region_t *mm_last = mm + ctx->mmap_num;
907 int update_max_va_needed = 0;
908 int update_max_pa_needed = 0;
909
910 /* Check sanity of mmap array. */
911 assert(mm[ctx->mmap_num].size == 0);
912
913 while (mm->size) {
914 if ((mm->base_va == base_va) && (mm->size == size))
915 break;
916 ++mm;
917 }
918
919 /* Check that the region was found */
920 if (mm->size == 0)
921 return -EINVAL;
922
923 /* If the region is static it can't be removed */
924 if (!(mm->attr & MT_DYNAMIC))
925 return -EPERM;
926
927 /* Check if this region is using the top VAs or PAs. */
928 if ((mm->base_va + mm->size - 1) == ctx->max_va)
929 update_max_va_needed = 1;
930 if ((mm->base_pa + mm->size - 1) == ctx->max_pa)
931 update_max_pa_needed = 1;
932
933 /* Update the translation tables if needed */
934 if (ctx->initialized) {
935 xlat_tables_unmap_region(ctx, mm, 0, ctx->base_table,
936 ctx->base_table_entries,
937 ctx->base_level);
938 xlat_arch_tlbi_va_sync();
939 }
940
941 /* Remove this region by moving the rest down by one place. */
942 memmove(mm, mm + 1, (uintptr_t)mm_last - (uintptr_t)mm);
943
944 /* Check if we need to update the max VAs and PAs */
945 if (update_max_va_needed) {
946 ctx->max_va = 0;
947 mm = ctx->mmap;
948 while (mm->size) {
949 if ((mm->base_va + mm->size - 1) > ctx->max_va)
950 ctx->max_va = mm->base_va + mm->size - 1;
951 ++mm;
952 }
953 }
954
955 if (update_max_pa_needed) {
956 ctx->max_pa = 0;
957 mm = ctx->mmap;
958 while (mm->size) {
959 if ((mm->base_pa + mm->size - 1) > ctx->max_pa)
960 ctx->max_pa = mm->base_pa + mm->size - 1;
961 ++mm;
962 }
963 }
964
965 return 0;
966}
967
968#endif /* PLAT_XLAT_TABLES_DYNAMIC */
969
Sandrine Bailleuxa9ad8482017-07-18 13:26:36 +0100970void init_xlat_tables_ctx(xlat_ctx_t *ctx)
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000971{
Antonio Nino Diaz609c9192017-10-04 16:52:15 +0100972 assert(ctx != NULL);
973 assert(!ctx->initialized);
974 assert(ctx->xlat_regime == EL3_REGIME || ctx->xlat_regime == EL1_EL0_REGIME);
975 assert(!is_mmu_enabled_ctx(ctx));
976
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000977 mmap_region_t *mm = ctx->mmap;
978
Antonio Nino Diazfd2299e2018-07-03 11:58:49 +0100979 xlat_mmap_print(mm);
Sandrine Bailleuxa9ad8482017-07-18 13:26:36 +0100980
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000981 /* All tables must be zeroed before mapping any region. */
982
Varun Wadekar6311f632017-06-07 09:57:42 -0700983 for (unsigned int i = 0; i < ctx->base_table_entries; i++)
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000984 ctx->base_table[i] = INVALID_DESC;
985
Varun Wadekar6311f632017-06-07 09:57:42 -0700986 for (unsigned int j = 0; j < ctx->tables_num; j++) {
Antonio Nino Diaz0b64f4e2017-02-27 17:23:54 +0000987#if PLAT_XLAT_TABLES_DYNAMIC
988 ctx->tables_mapped_regions[j] = 0;
989#endif
Varun Wadekar6311f632017-06-07 09:57:42 -0700990 for (unsigned int i = 0; i < XLAT_TABLE_ENTRIES; i++)
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000991 ctx->tables[j][i] = INVALID_DESC;
992 }
993
Antonio Nino Diaz0b64f4e2017-02-27 17:23:54 +0000994 while (mm->size) {
995 uintptr_t end_va = xlat_tables_map_region(ctx, mm, 0, ctx->base_table,
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +0000996 ctx->base_table_entries, ctx->base_level);
997
Antonio Nino Diaz0b64f4e2017-02-27 17:23:54 +0000998 if (end_va != mm->base_va + mm->size - 1) {
999 ERROR("Not enough memory to map region:\n"
1000 " VA:%p PA:0x%llx size:0x%zx attr:0x%x\n",
1001 (void *)mm->base_va, mm->base_pa, mm->size, mm->attr);
1002 panic();
1003 }
1004
1005 mm++;
1006 }
1007
Sandrine Bailleux347621b2017-07-11 15:11:10 +01001008 assert(ctx->pa_max_address <= xlat_arch_get_max_supported_pa());
Sandrine Bailleuxa9ad8482017-07-18 13:26:36 +01001009 assert(ctx->max_va <= ctx->va_max_address);
1010 assert(ctx->max_pa <= ctx->pa_max_address);
1011
Sandrine Bailleux99f60792017-05-31 13:31:48 +01001012 ctx->initialized = 1;
1013
1014 xlat_tables_print(ctx);
Antonio Nino Diaz7bb01fb2017-03-08 14:40:23 +00001015}