xref: /rk3399_ARM-atf/docs/components/xlat-tables-lib-v2-design.rst (revision 8aa050554b996406231a66a048b56fa03ba220c8)
1*8aa05055SPaul BeesleyTranslation (XLAT) Tables Library
240d553cfSPaul Beesley=================================
340d553cfSPaul Beesley
440d553cfSPaul Beesley.. contents::
540d553cfSPaul Beesley
640d553cfSPaul Beesley
740d553cfSPaul BeesleyThis document describes the design of the translation tables library (version 2)
840d553cfSPaul Beesleyused by Trusted Firmware-A (TF-A). This library provides APIs to create page
940d553cfSPaul Beesleytables based on a description of the memory layout, as well as setting up system
1040d553cfSPaul Beesleyregisters related to the Memory Management Unit (MMU) and performing the
1140d553cfSPaul Beesleyrequired Translation Lookaside Buffer (TLB) maintenance operations.
1240d553cfSPaul Beesley
1340d553cfSPaul BeesleyMore specifically, some use cases that this library aims to support are:
1440d553cfSPaul Beesley
1540d553cfSPaul Beesley#. Statically allocate translation tables and populate them (at run-time) based
1640d553cfSPaul Beesley   on a description of the memory layout. The memory layout is typically
1740d553cfSPaul Beesley   provided by the platform port as a list of memory regions;
1840d553cfSPaul Beesley
1940d553cfSPaul Beesley#. Support for generating translation tables pertaining to a different
2040d553cfSPaul Beesley   translation regime than the exception level the library code is executing at;
2140d553cfSPaul Beesley
2240d553cfSPaul Beesley#. Support for dynamic mapping and unmapping of regions, even while the MMU is
2340d553cfSPaul Beesley   on. This can be used to temporarily map some memory regions and unmap them
2440d553cfSPaul Beesley   later on when no longer needed;
2540d553cfSPaul Beesley
2640d553cfSPaul Beesley#. Support for non-identity virtual to physical mappings to compress the virtual
2740d553cfSPaul Beesley   address space;
2840d553cfSPaul Beesley
2940d553cfSPaul Beesley#. Support for changing memory attributes of memory regions at run-time.
3040d553cfSPaul Beesley
3140d553cfSPaul Beesley
3240d553cfSPaul BeesleyAbout version 1 and version 2
3340d553cfSPaul Beesley-----------------------------
3440d553cfSPaul Beesley
3540d553cfSPaul BeesleyThis document focuses on version 2 of the library, whose sources are available
3640d553cfSPaul Beesleyin the `lib/xlat_tables_v2`_ directory. Version 1 of the library can still be
3740d553cfSPaul Beesleyfound in `lib/xlat_tables`_ directory but it is less flexible and doesn't
3840d553cfSPaul Beesleysupport dynamic mapping. Although potential bug fixes will be applied to both
3940d553cfSPaul Beesleyversions, future features enhancements will focus on version 2 and might not be
4040d553cfSPaul Beesleyback-ported to version 1. Therefore, it is recommended to use version 2,
4140d553cfSPaul Beesleyespecially for new platform ports.
4240d553cfSPaul Beesley
4340d553cfSPaul BeesleyHowever, please note that version 2 is still in active development and is not
4440d553cfSPaul Beesleyconsidered stable yet. Hence, compatibility breaks might be introduced.
4540d553cfSPaul Beesley
4640d553cfSPaul BeesleyFrom this point onwards, this document will implicitly refer to version 2 of the
4740d553cfSPaul Beesleylibrary.
4840d553cfSPaul Beesley
4940d553cfSPaul Beesley
5040d553cfSPaul BeesleyDesign concepts and interfaces
5140d553cfSPaul Beesley------------------------------
5240d553cfSPaul Beesley
5340d553cfSPaul BeesleyThis section presents some of the key concepts and data structures used in the
5440d553cfSPaul Beesleytranslation tables library.
5540d553cfSPaul Beesley
5640d553cfSPaul Beesley`mmap` regions
5740d553cfSPaul Beesley~~~~~~~~~~~~~~
5840d553cfSPaul Beesley
5940d553cfSPaul BeesleyAn ``mmap_region`` is an abstract, concise way to represent a memory region to
6040d553cfSPaul Beesleymap. It is one of the key interfaces to the library. It is identified by:
6140d553cfSPaul Beesley
6240d553cfSPaul Beesley- its physical base address;
6340d553cfSPaul Beesley- its virtual base address;
6440d553cfSPaul Beesley- its size;
6540d553cfSPaul Beesley- its attributes;
6640d553cfSPaul Beesley- its mapping granularity (optional).
6740d553cfSPaul Beesley
6840d553cfSPaul BeesleySee the ``struct mmap_region`` type in `xlat_tables_v2.h`_.
6940d553cfSPaul Beesley
7040d553cfSPaul BeesleyThe user usually provides a list of such mmap regions to map and lets the
7140d553cfSPaul Beesleylibrary transpose that in a set of translation tables. As a result, the library
7240d553cfSPaul Beesleymight create new translation tables, update or split existing ones.
7340d553cfSPaul Beesley
7440d553cfSPaul BeesleyThe region attributes specify the type of memory (for example device or cached
7540d553cfSPaul Beesleynormal memory) as well as the memory access permissions (read-only or
7640d553cfSPaul Beesleyread-write, executable or not, secure or non-secure, and so on). In the case of
7740d553cfSPaul Beesleythe EL1&0 translation regime, the attributes also specify whether the region is
7840d553cfSPaul Beesleya User region (EL0) or Privileged region (EL1). See the ``MT_xxx`` definitions
7940d553cfSPaul Beesleyin `xlat_tables_v2.h`_. Note that for the EL1&0 translation regime the Execute
8040d553cfSPaul BeesleyNever attribute is set simultaneously for both EL1 and EL0.
8140d553cfSPaul Beesley
8240d553cfSPaul BeesleyThe granularity controls the translation table level to go down to when mapping
8340d553cfSPaul Beesleythe region. For example, assuming the MMU has been configured to use a 4KB
8440d553cfSPaul Beesleygranule size, the library might map a 2MB memory region using either of the two
8540d553cfSPaul Beesleyfollowing options:
8640d553cfSPaul Beesley
8740d553cfSPaul Beesley- using a single level-2 translation table entry;
8840d553cfSPaul Beesley- using a level-2 intermediate entry to a level-3 translation table (which
8940d553cfSPaul Beesley  contains 512 entries, each mapping 4KB).
9040d553cfSPaul Beesley
9140d553cfSPaul BeesleyThe first solution potentially requires less translation tables, hence
9240d553cfSPaul Beesleypotentially less memory.  However, if part of this 2MB region is later remapped
9340d553cfSPaul Beesleywith different memory attributes, the library might need to split the existing
9440d553cfSPaul Beesleypage tables to refine the mappings. If a single level-2 entry has been used
9540d553cfSPaul Beesleyhere, a level-3 table will need to be allocated on the fly and the level-2
9640d553cfSPaul Beesleymodified to point to this new level-3 table. This has a performance cost at
9740d553cfSPaul Beesleyrun-time.
9840d553cfSPaul Beesley
9940d553cfSPaul BeesleyIf the user knows upfront that such a remapping operation is likely to happen
10040d553cfSPaul Beesleythen they might enforce a 4KB mapping granularity for this 2MB region from the
10140d553cfSPaul Beesleybeginning; remapping some of these 4KB pages on the fly then becomes a
10240d553cfSPaul Beesleylightweight operation.
10340d553cfSPaul Beesley
10440d553cfSPaul BeesleyThe region's granularity is an optional field; if it is not specified the
10540d553cfSPaul Beesleylibrary will choose the mapping granularity for this region as it sees fit (more
10640d553cfSPaul Beesleydetails can be found in `The memory mapping algorithm`_ section below).
10740d553cfSPaul Beesley
10840d553cfSPaul BeesleyTranslation Context
10940d553cfSPaul Beesley~~~~~~~~~~~~~~~~~~~
11040d553cfSPaul Beesley
11140d553cfSPaul BeesleyThe library can create or modify translation tables pertaining to a different
11240d553cfSPaul Beesleytranslation regime than the exception level the library code is executing at.
11340d553cfSPaul BeesleyFor example, the library might be used by EL3 software (for instance BL31) to
11440d553cfSPaul Beesleycreate translation tables pertaining to the S-EL1&0 translation regime.
11540d553cfSPaul Beesley
11640d553cfSPaul BeesleyThis flexibility comes from the use of *translation contexts*. A *translation
11740d553cfSPaul Beesleycontext* constitutes the superset of information used by the library to track
11840d553cfSPaul Beesleythe status of a set of translation tables for a given translation regime.
11940d553cfSPaul Beesley
12040d553cfSPaul BeesleyThe library internally allocates a default translation context, which pertains
12140d553cfSPaul Beesleyto the translation regime of the current exception level. Additional contexts
12240d553cfSPaul Beesleymay be explicitly allocated and initialized using the
12340d553cfSPaul Beesley``REGISTER_XLAT_CONTEXT()`` macro. Separate APIs are provided to act either on
12440d553cfSPaul Beesleythe default translation context or on an alternative one.
12540d553cfSPaul Beesley
12640d553cfSPaul BeesleyTo register a translation context, the user must provide the library with the
12740d553cfSPaul Beesleyfollowing information:
12840d553cfSPaul Beesley
12940d553cfSPaul Beesley* A name.
13040d553cfSPaul Beesley
13140d553cfSPaul Beesley  The resulting translation context variable will be called after this name, to
13240d553cfSPaul Beesley  which ``_xlat_ctx`` is appended. For example, if the macro name parameter is
13340d553cfSPaul Beesley  ``foo``, the context variable name will be ``foo_xlat_ctx``.
13440d553cfSPaul Beesley
13540d553cfSPaul Beesley* The maximum number of `mmap` regions to map.
13640d553cfSPaul Beesley
13740d553cfSPaul Beesley  Should account for both static and dynamic regions, if applicable.
13840d553cfSPaul Beesley
13940d553cfSPaul Beesley* The number of sub-translation tables to allocate.
14040d553cfSPaul Beesley
14140d553cfSPaul Beesley  Number of translation tables to statically allocate for this context,
14240d553cfSPaul Beesley  excluding the initial lookup level translation table, which is always
14340d553cfSPaul Beesley  allocated. For example, if the initial lookup level is 1, this parameter would
14440d553cfSPaul Beesley  specify the number of level-2 and level-3 translation tables to pre-allocate
14540d553cfSPaul Beesley  for this context.
14640d553cfSPaul Beesley
14740d553cfSPaul Beesley* The size of the virtual address space.
14840d553cfSPaul Beesley
14940d553cfSPaul Beesley  Size in bytes of the virtual address space to map using this context. This
15040d553cfSPaul Beesley  will incidentally determine the number of entries in the initial lookup level
15140d553cfSPaul Beesley  translation table : the library will allocate as many entries as is required
15240d553cfSPaul Beesley  to map the entire virtual address space.
15340d553cfSPaul Beesley
15440d553cfSPaul Beesley* The size of the physical address space.
15540d553cfSPaul Beesley
15640d553cfSPaul Beesley  Size in bytes of the physical address space to map using this context.
15740d553cfSPaul Beesley
15840d553cfSPaul BeesleyThe default translation context is internally initialized using information
15940d553cfSPaul Beesleycoming (for the most part) from platform-specific defines:
16040d553cfSPaul Beesley
16140d553cfSPaul Beesley- name: hard-coded to ``tf`` ; hence the name of the default context variable is
16240d553cfSPaul Beesley  ``tf_xlat_ctx``;
16340d553cfSPaul Beesley- number of `mmap` regions: ``MAX_MMAP_REGIONS``;
16440d553cfSPaul Beesley- number of sub-translation tables: ``MAX_XLAT_TABLES``;
16540d553cfSPaul Beesley- size of the virtual address space: ``PLAT_VIRT_ADDR_SPACE_SIZE``;
16640d553cfSPaul Beesley- size of the physical address space: ``PLAT_PHY_ADDR_SPACE_SIZE``.
16740d553cfSPaul Beesley
16840d553cfSPaul BeesleyPlease refer to the `Porting Guide`_ for more details about these macros.
16940d553cfSPaul Beesley
17040d553cfSPaul Beesley
17140d553cfSPaul BeesleyStatic and dynamic memory regions
17240d553cfSPaul Beesley~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17340d553cfSPaul Beesley
17440d553cfSPaul BeesleyThe library optionally supports dynamic memory mapping. This feature may be
17540d553cfSPaul Beesleyenabled using the ``PLAT_XLAT_TABLES_DYNAMIC`` platform build flag.
17640d553cfSPaul Beesley
17740d553cfSPaul BeesleyWhen dynamic memory mapping is enabled, the library categorises mmap regions as
17840d553cfSPaul Beesley*static* or *dynamic*.
17940d553cfSPaul Beesley
18040d553cfSPaul Beesley- *Static regions* are fixed for the lifetime of the system. They can only be
18140d553cfSPaul Beesley  added early on, before the translation tables are created and populated. They
18240d553cfSPaul Beesley  cannot be removed afterwards.
18340d553cfSPaul Beesley
18440d553cfSPaul Beesley- *Dynamic regions* can be added or removed any time.
18540d553cfSPaul Beesley
18640d553cfSPaul BeesleyWhen the dynamic memory mapping feature is disabled, only static regions exist.
18740d553cfSPaul Beesley
18840d553cfSPaul BeesleyThe dynamic memory mapping feature may be used to map and unmap transient memory
18940d553cfSPaul Beesleyareas. This is useful when the user needs to access some memory for a fixed
19040d553cfSPaul Beesleyperiod of time, after which the memory may be discarded and reclaimed. For
19140d553cfSPaul Beesleyexample, a memory region that is only required at boot time while the system is
19240d553cfSPaul Beesleyinitializing, or to temporarily share a memory buffer between the normal world
19340d553cfSPaul Beesleyand trusted world. Note that it is up to the caller to ensure that these regions
19440d553cfSPaul Beesleyare not accessed concurrently while the regions are being added or removed.
19540d553cfSPaul Beesley
19640d553cfSPaul BeesleyAlthough this feature provides some level of dynamic memory allocation, this
19740d553cfSPaul Beesleydoes not allow dynamically allocating an arbitrary amount of memory at an
19840d553cfSPaul Beesleyarbitrary memory location. The user is still required to declare at compile-time
19940d553cfSPaul Beesleythe limits of these allocations ; the library will deny any mapping request that
20040d553cfSPaul Beesleydoes not fit within this pre-allocated pool of memory.
20140d553cfSPaul Beesley
20240d553cfSPaul Beesley
20340d553cfSPaul BeesleyLibrary APIs
20440d553cfSPaul Beesley------------
20540d553cfSPaul Beesley
20640d553cfSPaul BeesleyThe external APIs exposed by this library are declared and documented in the
20740d553cfSPaul Beesley`xlat_tables_v2.h`_ header file. This should be the reference point for
20840d553cfSPaul Beesleygetting information about the usage of the different APIs this library
20940d553cfSPaul Beesleyprovides. This section just provides some extra details and clarifications.
21040d553cfSPaul Beesley
21140d553cfSPaul BeesleyAlthough the ``mmap_region`` structure is a publicly visible type, it is not
21240d553cfSPaul Beesleyrecommended to populate these structures by hand. Instead, wherever APIs expect
21340d553cfSPaul Beesleyfunction arguments of type ``mmap_region_t``, these should be constructed using
21440d553cfSPaul Beesleythe ``MAP_REGION*()`` family of helper macros. This is to limit the risk of
21540d553cfSPaul Beesleycompatibility breaks, should the ``mmap_region`` structure type evolve in the
21640d553cfSPaul Beesleyfuture.
21740d553cfSPaul Beesley
21840d553cfSPaul BeesleyThe ``MAP_REGION()`` and ``MAP_REGION_FLAT()`` macros do not allow specifying a
21940d553cfSPaul Beesleymapping granularity, which leaves the library implementation free to choose
22040d553cfSPaul Beesleyit. However, in cases where a specific granularity is required, the
22140d553cfSPaul Beesley``MAP_REGION2()`` macro might be used instead.
22240d553cfSPaul Beesley
22340d553cfSPaul BeesleyAs explained earlier in this document, when the dynamic mapping feature is
22440d553cfSPaul Beesleydisabled, there is no notion of dynamic regions. Conceptually, there are only
22540d553cfSPaul Beesleystatic regions. For this reason (and to retain backward compatibility with the
22640d553cfSPaul Beesleyversion 1 of the library), the APIs that map static regions do not embed the
22740d553cfSPaul Beesleyword *static* in their functions names (for example ``mmap_add_region()``), in
22840d553cfSPaul Beesleycontrast with the dynamic regions APIs (for example
22940d553cfSPaul Beesley``mmap_add_dynamic_region()``).
23040d553cfSPaul Beesley
23140d553cfSPaul BeesleyAlthough the definition of static and dynamic regions is not based on the state
23240d553cfSPaul Beesleyof the MMU, the two are still related in some way. Static regions can only be
23340d553cfSPaul Beesleyadded before ``init_xlat_tables()`` is called and ``init_xlat_tables()`` must be
23440d553cfSPaul Beesleycalled while the MMU is still off. As a result, static regions cannot be added
23540d553cfSPaul Beesleyonce the MMU has been enabled. Dynamic regions can be added with the MMU on or
23640d553cfSPaul Beesleyoff. In practice, the usual call flow would look like this:
23740d553cfSPaul Beesley
23840d553cfSPaul Beesley#. The MMU is initially off.
23940d553cfSPaul Beesley
24040d553cfSPaul Beesley#. Add some static regions, add some dynamic regions.
24140d553cfSPaul Beesley
24240d553cfSPaul Beesley#. Initialize translation tables based on the list of mmap regions (using one of
24340d553cfSPaul Beesley   the ``init_xlat_tables*()`` APIs).
24440d553cfSPaul Beesley
24540d553cfSPaul Beesley#. At this point, it is no longer possible to add static regions. Dynamic
24640d553cfSPaul Beesley   regions can still be added or removed.
24740d553cfSPaul Beesley
24840d553cfSPaul Beesley#. Enable the MMU.
24940d553cfSPaul Beesley
25040d553cfSPaul Beesley#. Dynamic regions can continue to be added or removed.
25140d553cfSPaul Beesley
25240d553cfSPaul BeesleyBecause static regions are added early on at boot time and are all in the
25340d553cfSPaul Beesleycontrol of the platform initialization code, the ``mmap_add*()`` family of APIs
25440d553cfSPaul Beesleyare not expected to fail. They do not return any error code.
25540d553cfSPaul Beesley
25640d553cfSPaul BeesleyNonetheless, these APIs will check upfront whether the region can be
25740d553cfSPaul Beesleysuccessfully added before updating the translation context structure. If the
25840d553cfSPaul Beesleylibrary detects that there is insufficient memory to meet the request, or that
25940d553cfSPaul Beesleythe new region will overlap another one in an invalid way, or if any other
26040d553cfSPaul Beesleyunexpected error is encountered, they will print an error message on the UART.
26140d553cfSPaul BeesleyAdditionally, when asserts are enabled (typically in debug builds), an assertion
26240d553cfSPaul Beesleywill be triggered. Otherwise, the function call will just return straight away,
26340d553cfSPaul Beesleywithout adding the offending memory region.
26440d553cfSPaul Beesley
26540d553cfSPaul Beesley
26640d553cfSPaul BeesleyLibrary limitations
26740d553cfSPaul Beesley-------------------
26840d553cfSPaul Beesley
26940d553cfSPaul BeesleyDynamic regions are not allowed to overlap each other. Static regions are
27040d553cfSPaul Beesleyallowed to overlap as long as one of them is fully contained inside the other
27140d553cfSPaul Beesleyone. This is allowed for backwards compatibility with the previous behaviour in
27240d553cfSPaul Beesleythe version 1 of the library.
27340d553cfSPaul Beesley
27440d553cfSPaul Beesley
27540d553cfSPaul BeesleyImplementation details
27640d553cfSPaul Beesley----------------------
27740d553cfSPaul Beesley
27840d553cfSPaul BeesleyCode structure
27940d553cfSPaul Beesley~~~~~~~~~~~~~~
28040d553cfSPaul Beesley
28140d553cfSPaul BeesleyThe library is divided into 4 modules:
28240d553cfSPaul Beesley
28340d553cfSPaul Beesley- **Core module**
28440d553cfSPaul Beesley
28540d553cfSPaul Beesley  Provides the main functionality of the library, such as the initialization of
28640d553cfSPaul Beesley  translation tables contexts and mapping/unmapping memory regions. This module
28740d553cfSPaul Beesley  provides functions such as ``mmap_add_region_ctx`` that let the caller specify
28840d553cfSPaul Beesley  the translation tables context affected by them.
28940d553cfSPaul Beesley
29040d553cfSPaul Beesley  See `xlat_tables_core.c`_.
29140d553cfSPaul Beesley
29240d553cfSPaul Beesley- **Active context module**
29340d553cfSPaul Beesley
29440d553cfSPaul Beesley  Instantiates the context that is used by the current BL image and provides
29540d553cfSPaul Beesley  helpers to manipulate it, abstracting it from the rest of the code.
29640d553cfSPaul Beesley  This module provides functions such as ``mmap_add_region``, that directly
29740d553cfSPaul Beesley  affect the BL image using them.
29840d553cfSPaul Beesley
29940d553cfSPaul Beesley  See `xlat_tables_context.c`_.
30040d553cfSPaul Beesley
30140d553cfSPaul Beesley- **Utilities module**
30240d553cfSPaul Beesley
30340d553cfSPaul Beesley  Provides additional functionality like debug print of the current state of the
30440d553cfSPaul Beesley  translation tables and helpers to query memory attributes and to modify them.
30540d553cfSPaul Beesley
30640d553cfSPaul Beesley  See `xlat_tables_utils.c`_.
30740d553cfSPaul Beesley
30840d553cfSPaul Beesley- **Architectural module**
30940d553cfSPaul Beesley
31040d553cfSPaul Beesley  Provides functions that are dependent on the current execution state
31140d553cfSPaul Beesley  (AArch32/AArch64), such as the functions used for TLB invalidation, setup the
31240d553cfSPaul Beesley  MMU, or calculate the Physical Address Space size. They do not need a
31340d553cfSPaul Beesley  translation context to work on.
31440d553cfSPaul Beesley
31540d553cfSPaul Beesley  See `aarch32/xlat_tables_arch.c`_ and `aarch64/xlat_tables_arch.c`_.
31640d553cfSPaul Beesley
31740d553cfSPaul BeesleyFrom mmap regions to translation tables
31840d553cfSPaul Beesley~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
31940d553cfSPaul Beesley
32040d553cfSPaul BeesleyA translation context contains a list of ``mmap_region_t``, which holds the
32140d553cfSPaul Beesleyinformation of all the regions that are mapped at any given time. Whenever there
32240d553cfSPaul Beesleyis a request to map (resp. unmap) a memory region, it is added to (resp. removed
32340d553cfSPaul Beesleyfrom) the ``mmap_region_t`` list.
32440d553cfSPaul Beesley
32540d553cfSPaul BeesleyThe mmap regions list is a conceptual way to represent the memory layout. At
32640d553cfSPaul Beesleysome point, the library has to convert this information into actual translation
32740d553cfSPaul Beesleytables to program into the MMU.
32840d553cfSPaul Beesley
32940d553cfSPaul BeesleyBefore the ``init_xlat_tables()`` API is called, the library only acts on the
33040d553cfSPaul Beesleymmap regions list. Adding a static or dynamic region at this point through one
33140d553cfSPaul Beesleyof the ``mmap_add*()`` APIs does not affect the translation tables in any way,
33240d553cfSPaul Beesleythey only get registered in the internal mmap region list. It is only when the
33340d553cfSPaul Beesleyuser calls the ``init_xlat_tables()`` that the translation tables are populated
33440d553cfSPaul Beesleyin memory based on the list of mmap regions registered so far. This is an
33540d553cfSPaul Beesleyoptimization that allows creation of the initial set of translation tables in
33640d553cfSPaul Beesleyone go, rather than having to edit them every time while the MMU is disabled.
33740d553cfSPaul Beesley
33840d553cfSPaul BeesleyAfter the ``init_xlat_tables()`` API has been called, only dynamic regions can
33940d553cfSPaul Beesleybe added. Changes to the translation tables (as well as the mmap regions list)
34040d553cfSPaul Beesleywill take effect immediately.
34140d553cfSPaul Beesley
34240d553cfSPaul BeesleyThe memory mapping algorithm
34340d553cfSPaul Beesley~~~~~~~~~~~~~~~~~~~~~~~~~~~~
34440d553cfSPaul Beesley
34540d553cfSPaul BeesleyThe mapping function is implemented as a recursive algorithm. It is however
34640d553cfSPaul Beesleybound by the level of depth of the translation tables (the Armv8-A architecture
34740d553cfSPaul Beesleyallows up to 4 lookup levels).
34840d553cfSPaul Beesley
34940d553cfSPaul BeesleyBy default [#granularity-ref]_, the algorithm will attempt to minimize the
35040d553cfSPaul Beesleynumber of translation tables created to satisfy the user's request. It will
35140d553cfSPaul Beesleyfavour mapping a region using the biggest possible blocks, only creating a
35240d553cfSPaul Beesleysub-table if it is strictly necessary. This is to reduce the memory footprint of
35340d553cfSPaul Beesleythe firmware.
35440d553cfSPaul Beesley
35540d553cfSPaul BeesleyThe most common reason for needing a sub-table is when a specific mapping
35640d553cfSPaul Beesleyrequires a finer granularity. Misaligned regions also require a finer
35740d553cfSPaul Beesleygranularity than what the user may had originally expected, using a lot more
35840d553cfSPaul Beesleymemory than expected. The reason is that all levels of translation are
35940d553cfSPaul Beesleyrestricted to address translations of the same granularity as the size of the
36040d553cfSPaul Beesleyblocks of that level.  For example, for a 4 KiB page size, a level 2 block entry
36140d553cfSPaul Beesleycan only translate up to a granularity of 2 MiB. If the Physical Address is not
36240d553cfSPaul Beesleyaligned to 2 MiB then additional level 3 tables are also needed.
36340d553cfSPaul Beesley
36440d553cfSPaul BeesleyNote that not every translation level allows any type of descriptor. Depending
36540d553cfSPaul Beesleyon the page size, levels 0 and 1 of translation may only allow table
36640d553cfSPaul Beesleydescriptors. If a block entry could be able to describe a translation, but that
36740d553cfSPaul Beesleylevel does not allow block descriptors, a table descriptor will have to be used
36840d553cfSPaul Beesleyinstead, as well as additional tables at the next level.
36940d553cfSPaul Beesley
37040d553cfSPaul Beesley|Alignment Example|
37140d553cfSPaul Beesley
37240d553cfSPaul BeesleyThe mmap regions are sorted in a way that simplifies the code that maps
37340d553cfSPaul Beesleythem. Even though this ordering is only strictly needed for overlapping static
37440d553cfSPaul Beesleyregions, it must also be applied for dynamic regions to maintain a consistent
37540d553cfSPaul Beesleyorder of all regions at all times. As each new region is mapped, existing
37640d553cfSPaul Beesleyentries in the translation tables are checked to ensure consistency. Please
37740d553cfSPaul Beesleyrefer to the comments in the source code of the core module for more details
37840d553cfSPaul Beesleyabout the sorting algorithm in use.
37940d553cfSPaul Beesley
38040d553cfSPaul Beesley.. [#granularity-ref] That is, when mmap regions do not enforce their mapping
38140d553cfSPaul Beesley                      granularity.
38240d553cfSPaul Beesley
38340d553cfSPaul BeesleyTLB maintenance operations
38440d553cfSPaul Beesley~~~~~~~~~~~~~~~~~~~~~~~~~~
38540d553cfSPaul Beesley
38640d553cfSPaul BeesleyThe library takes care of performing TLB maintenance operations when required.
38740d553cfSPaul BeesleyFor example, when the user requests removing a dynamic region, the library
38840d553cfSPaul Beesleyinvalidates all TLB entries associated to that region to ensure that these
38940d553cfSPaul Beesleychanges are visible to subsequent execution, including speculative execution,
39040d553cfSPaul Beesleythat uses the changed translation table entries.
39140d553cfSPaul Beesley
39240d553cfSPaul BeesleyA counter-example is the initialization of translation tables. In this case,
39340d553cfSPaul Beesleyexplicit TLB maintenance is not required. The Armv8-A architecture guarantees
39440d553cfSPaul Beesleythat all TLBs are disabled from reset and their contents have no effect on
39540d553cfSPaul Beesleyaddress translation at reset [#tlb-reset-ref]_. Therefore, the TLBs invalidation
39640d553cfSPaul Beesleyis deferred to the ``enable_mmu*()`` family of functions, just before the MMU is
39740d553cfSPaul Beesleyturned on.
39840d553cfSPaul Beesley
39940d553cfSPaul BeesleyTLB invalidation is not required when adding dynamic regions either. Dynamic
40040d553cfSPaul Beesleyregions are not allowed to overlap existing memory region. Therefore, if the
40140d553cfSPaul Beesleydynamic mapping request is deemed legitimate, it automatically concerns memory
40240d553cfSPaul Beesleythat was not mapped in this translation regime and the library will have
40340d553cfSPaul Beesleyinitialized its corresponding translation table entry to an invalid
40440d553cfSPaul Beesleydescriptor. Given that the TLBs are not architecturally permitted to hold any
40540d553cfSPaul Beesleyinvalid translation table entry [#tlb-no-invalid-entry]_, this means that this
40640d553cfSPaul Beesleymapping cannot be cached in the TLBs.
40740d553cfSPaul Beesley
40840d553cfSPaul Beesley.. [#tlb-reset-ref] See section D4.9 `Translation Lookaside Buffers (TLBs)`, subsection `TLB behavior at reset` in Armv8-A, rev C.a.
40940d553cfSPaul Beesley.. [#tlb-no-invalid-entry] See section D4.10.1 `General TLB maintenance requirements` in Armv8-A, rev C.a.
41040d553cfSPaul Beesley
41140d553cfSPaul Beesley--------------
41240d553cfSPaul Beesley
41340d553cfSPaul Beesley*Copyright (c) 2017-2018, Arm Limited and Contributors. All rights reserved.*
41440d553cfSPaul Beesley
41540d553cfSPaul Beesley.. _lib/xlat_tables_v2: ../../lib/xlat_tables_v2
41640d553cfSPaul Beesley.. _lib/xlat_tables: ../../lib/xlat_tables
41740d553cfSPaul Beesley.. _xlat_tables_v2.h: ../../include/lib/xlat_tables/xlat_tables_v2.h
41840d553cfSPaul Beesley.. _xlat_tables_context.c: ../../lib/xlat_tables_v2/xlat_tables_context.c
41940d553cfSPaul Beesley.. _xlat_tables_core.c: ../../lib/xlat_tables_v2/xlat_tables_core.c
42040d553cfSPaul Beesley.. _xlat_tables_utils.c: ../../lib/xlat_tables_v2/xlat_tables_utils.c
42140d553cfSPaul Beesley.. _aarch32/xlat_tables_arch.c: ../../lib/xlat_tables_v2/aarch32/xlat_tables_arch.c
42240d553cfSPaul Beesley.. _aarch64/xlat_tables_arch.c: ../../lib/xlat_tables_v2/aarch64/xlat_tables_arch.c
42340d553cfSPaul Beesley.. _Porting Guide: ../getting_started/porting-guide.rst
42440d553cfSPaul Beesley.. |Alignment Example| image:: ../diagrams/xlat_align.png?raw=true
425