Paul Beesley | fc9ee36 | 2019-03-07 15:47:15 +0000 | [diff] [blame] | 1 | Translation (XLAT) Tables Library |
Antonio Nino Diaz | b5d6809 | 2017-05-23 11:49:22 +0100 | [diff] [blame] | 2 | ================================= |
| 3 | |
Antonio Nino Diaz | b5d6809 | 2017-05-23 11:49:22 +0100 | [diff] [blame] | 4 | This document describes the design of the translation tables library (version 2) |
Dan Handley | 610e7e1 | 2018-03-01 18:44:00 +0000 | [diff] [blame] | 5 | used by Trusted Firmware-A (TF-A). This library provides APIs to create page |
Antonio Nino Diaz | b5d6809 | 2017-05-23 11:49:22 +0100 | [diff] [blame] | 6 | tables based on a description of the memory layout, as well as setting up system |
| 7 | registers related to the Memory Management Unit (MMU) and performing the |
| 8 | required Translation Lookaside Buffer (TLB) maintenance operations. |
| 9 | |
| 10 | More specifically, some use cases that this library aims to support are: |
| 11 | |
| 12 | #. Statically allocate translation tables and populate them (at run-time) based |
| 13 | on a description of the memory layout. The memory layout is typically |
| 14 | provided by the platform port as a list of memory regions; |
| 15 | |
| 16 | #. Support for generating translation tables pertaining to a different |
| 17 | translation regime than the exception level the library code is executing at; |
| 18 | |
| 19 | #. Support for dynamic mapping and unmapping of regions, even while the MMU is |
| 20 | on. This can be used to temporarily map some memory regions and unmap them |
| 21 | later on when no longer needed; |
| 22 | |
| 23 | #. Support for non-identity virtual to physical mappings to compress the virtual |
| 24 | address space; |
| 25 | |
| 26 | #. Support for changing memory attributes of memory regions at run-time. |
| 27 | |
| 28 | |
| 29 | About version 1 and version 2 |
| 30 | ----------------------------- |
| 31 | |
| 32 | This document focuses on version 2 of the library, whose sources are available |
Paul Beesley | f864067 | 2019-04-12 14:19:42 +0100 | [diff] [blame] | 33 | in the ``lib/xlat_tables_v2`` directory. Version 1 of the library can still be |
| 34 | found in ``lib/xlat_tables`` directory but it is less flexible and doesn't |
Antonio Nino Diaz | b5d6809 | 2017-05-23 11:49:22 +0100 | [diff] [blame] | 35 | support dynamic mapping. Although potential bug fixes will be applied to both |
| 36 | versions, future features enhancements will focus on version 2 and might not be |
| 37 | back-ported to version 1. Therefore, it is recommended to use version 2, |
| 38 | especially for new platform ports. |
| 39 | |
| 40 | However, please note that version 2 is still in active development and is not |
| 41 | considered stable yet. Hence, compatibility breaks might be introduced. |
| 42 | |
| 43 | From this point onwards, this document will implicitly refer to version 2 of the |
| 44 | library. |
| 45 | |
| 46 | |
| 47 | Design concepts and interfaces |
| 48 | ------------------------------ |
| 49 | |
| 50 | This section presents some of the key concepts and data structures used in the |
| 51 | translation tables library. |
| 52 | |
| 53 | `mmap` regions |
| 54 | ~~~~~~~~~~~~~~ |
| 55 | |
| 56 | An ``mmap_region`` is an abstract, concise way to represent a memory region to |
| 57 | map. It is one of the key interfaces to the library. It is identified by: |
| 58 | |
| 59 | - its physical base address; |
| 60 | - its virtual base address; |
| 61 | - its size; |
Sandrine Bailleux | 8f23fa8 | 2017-09-28 21:58:12 +0100 | [diff] [blame] | 62 | - its attributes; |
| 63 | - its mapping granularity (optional). |
Antonio Nino Diaz | b5d6809 | 2017-05-23 11:49:22 +0100 | [diff] [blame] | 64 | |
Paul Beesley | f864067 | 2019-04-12 14:19:42 +0100 | [diff] [blame] | 65 | See the ``struct mmap_region`` type in ``xlat_tables_v2.h``. |
Antonio Nino Diaz | b5d6809 | 2017-05-23 11:49:22 +0100 | [diff] [blame] | 66 | |
| 67 | The user usually provides a list of such mmap regions to map and lets the |
| 68 | library transpose that in a set of translation tables. As a result, the library |
| 69 | might create new translation tables, update or split existing ones. |
| 70 | |
| 71 | The region attributes specify the type of memory (for example device or cached |
| 72 | normal memory) as well as the memory access permissions (read-only or |
Antonio Nino Diaz | dcf9d92 | 2017-10-04 16:52:15 +0100 | [diff] [blame] | 73 | read-write, executable or not, secure or non-secure, and so on). In the case of |
| 74 | the EL1&0 translation regime, the attributes also specify whether the region is |
Antonio Nino Diaz | 8643a81 | 2018-06-21 14:39:16 +0100 | [diff] [blame] | 75 | a User region (EL0) or Privileged region (EL1). See the ``MT_xxx`` definitions |
Paul Beesley | f864067 | 2019-04-12 14:19:42 +0100 | [diff] [blame] | 76 | in ``xlat_tables_v2.h``. Note that for the EL1&0 translation regime the Execute |
Antonio Nino Diaz | 8643a81 | 2018-06-21 14:39:16 +0100 | [diff] [blame] | 77 | Never attribute is set simultaneously for both EL1 and EL0. |
Antonio Nino Diaz | b5d6809 | 2017-05-23 11:49:22 +0100 | [diff] [blame] | 78 | |
Sandrine Bailleux | 8f23fa8 | 2017-09-28 21:58:12 +0100 | [diff] [blame] | 79 | The granularity controls the translation table level to go down to when mapping |
| 80 | the region. For example, assuming the MMU has been configured to use a 4KB |
| 81 | granule size, the library might map a 2MB memory region using either of the two |
| 82 | following options: |
| 83 | |
| 84 | - using a single level-2 translation table entry; |
| 85 | - using a level-2 intermediate entry to a level-3 translation table (which |
| 86 | contains 512 entries, each mapping 4KB). |
| 87 | |
| 88 | The first solution potentially requires less translation tables, hence |
| 89 | potentially less memory. However, if part of this 2MB region is later remapped |
| 90 | with different memory attributes, the library might need to split the existing |
| 91 | page tables to refine the mappings. If a single level-2 entry has been used |
| 92 | here, a level-3 table will need to be allocated on the fly and the level-2 |
| 93 | modified to point to this new level-3 table. This has a performance cost at |
| 94 | run-time. |
| 95 | |
| 96 | If the user knows upfront that such a remapping operation is likely to happen |
| 97 | then they might enforce a 4KB mapping granularity for this 2MB region from the |
| 98 | beginning; remapping some of these 4KB pages on the fly then becomes a |
| 99 | lightweight operation. |
| 100 | |
| 101 | The region's granularity is an optional field; if it is not specified the |
| 102 | library will choose the mapping granularity for this region as it sees fit (more |
| 103 | details can be found in `The memory mapping algorithm`_ section below). |
Antonio Nino Diaz | b5d6809 | 2017-05-23 11:49:22 +0100 | [diff] [blame] | 104 | |
| 105 | Translation Context |
| 106 | ~~~~~~~~~~~~~~~~~~~ |
| 107 | |
| 108 | The library can create or modify translation tables pertaining to a different |
| 109 | translation regime than the exception level the library code is executing at. |
| 110 | For example, the library might be used by EL3 software (for instance BL31) to |
| 111 | create translation tables pertaining to the S-EL1&0 translation regime. |
| 112 | |
| 113 | This flexibility comes from the use of *translation contexts*. A *translation |
| 114 | context* constitutes the superset of information used by the library to track |
| 115 | the status of a set of translation tables for a given translation regime. |
| 116 | |
| 117 | The library internally allocates a default translation context, which pertains |
| 118 | to the translation regime of the current exception level. Additional contexts |
| 119 | may be explicitly allocated and initialized using the |
| 120 | ``REGISTER_XLAT_CONTEXT()`` macro. Separate APIs are provided to act either on |
| 121 | the default translation context or on an alternative one. |
| 122 | |
| 123 | To register a translation context, the user must provide the library with the |
| 124 | following information: |
| 125 | |
| 126 | * A name. |
| 127 | |
| 128 | The resulting translation context variable will be called after this name, to |
| 129 | which ``_xlat_ctx`` is appended. For example, if the macro name parameter is |
| 130 | ``foo``, the context variable name will be ``foo_xlat_ctx``. |
| 131 | |
| 132 | * The maximum number of `mmap` regions to map. |
| 133 | |
| 134 | Should account for both static and dynamic regions, if applicable. |
| 135 | |
| 136 | * The number of sub-translation tables to allocate. |
| 137 | |
| 138 | Number of translation tables to statically allocate for this context, |
| 139 | excluding the initial lookup level translation table, which is always |
| 140 | allocated. For example, if the initial lookup level is 1, this parameter would |
| 141 | specify the number of level-2 and level-3 translation tables to pre-allocate |
| 142 | for this context. |
| 143 | |
| 144 | * The size of the virtual address space. |
| 145 | |
| 146 | Size in bytes of the virtual address space to map using this context. This |
| 147 | will incidentally determine the number of entries in the initial lookup level |
| 148 | translation table : the library will allocate as many entries as is required |
| 149 | to map the entire virtual address space. |
| 150 | |
| 151 | * The size of the physical address space. |
| 152 | |
| 153 | Size in bytes of the physical address space to map using this context. |
| 154 | |
| 155 | The default translation context is internally initialized using information |
| 156 | coming (for the most part) from platform-specific defines: |
| 157 | |
| 158 | - name: hard-coded to ``tf`` ; hence the name of the default context variable is |
| 159 | ``tf_xlat_ctx``; |
| 160 | - number of `mmap` regions: ``MAX_MMAP_REGIONS``; |
| 161 | - number of sub-translation tables: ``MAX_XLAT_TABLES``; |
| 162 | - size of the virtual address space: ``PLAT_VIRT_ADDR_SPACE_SIZE``; |
| 163 | - size of the physical address space: ``PLAT_PHY_ADDR_SPACE_SIZE``. |
| 164 | |
Paul Beesley | f864067 | 2019-04-12 14:19:42 +0100 | [diff] [blame] | 165 | Please refer to the :ref:`Porting Guide` for more details about these macros. |
Antonio Nino Diaz | b5d6809 | 2017-05-23 11:49:22 +0100 | [diff] [blame] | 166 | |
| 167 | |
| 168 | Static and dynamic memory regions |
| 169 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 170 | |
| 171 | The library optionally supports dynamic memory mapping. This feature may be |
| 172 | enabled using the ``PLAT_XLAT_TABLES_DYNAMIC`` platform build flag. |
| 173 | |
| 174 | When dynamic memory mapping is enabled, the library categorises mmap regions as |
| 175 | *static* or *dynamic*. |
| 176 | |
| 177 | - *Static regions* are fixed for the lifetime of the system. They can only be |
| 178 | added early on, before the translation tables are created and populated. They |
| 179 | cannot be removed afterwards. |
| 180 | |
| 181 | - *Dynamic regions* can be added or removed any time. |
| 182 | |
| 183 | When the dynamic memory mapping feature is disabled, only static regions exist. |
| 184 | |
| 185 | The dynamic memory mapping feature may be used to map and unmap transient memory |
| 186 | areas. This is useful when the user needs to access some memory for a fixed |
| 187 | period of time, after which the memory may be discarded and reclaimed. For |
| 188 | example, a memory region that is only required at boot time while the system is |
| 189 | initializing, or to temporarily share a memory buffer between the normal world |
| 190 | and trusted world. Note that it is up to the caller to ensure that these regions |
| 191 | are not accessed concurrently while the regions are being added or removed. |
| 192 | |
| 193 | Although this feature provides some level of dynamic memory allocation, this |
| 194 | does not allow dynamically allocating an arbitrary amount of memory at an |
| 195 | arbitrary memory location. The user is still required to declare at compile-time |
| 196 | the limits of these allocations ; the library will deny any mapping request that |
| 197 | does not fit within this pre-allocated pool of memory. |
| 198 | |
| 199 | |
| 200 | Library APIs |
| 201 | ------------ |
| 202 | |
| 203 | The external APIs exposed by this library are declared and documented in the |
Paul Beesley | f864067 | 2019-04-12 14:19:42 +0100 | [diff] [blame] | 204 | ``xlat_tables_v2.h`` header file. This should be the reference point for |
Antonio Nino Diaz | b5d6809 | 2017-05-23 11:49:22 +0100 | [diff] [blame] | 205 | getting information about the usage of the different APIs this library |
| 206 | provides. This section just provides some extra details and clarifications. |
| 207 | |
| 208 | Although the ``mmap_region`` structure is a publicly visible type, it is not |
| 209 | recommended to populate these structures by hand. Instead, wherever APIs expect |
| 210 | function arguments of type ``mmap_region_t``, these should be constructed using |
| 211 | the ``MAP_REGION*()`` family of helper macros. This is to limit the risk of |
| 212 | compatibility breaks, should the ``mmap_region`` structure type evolve in the |
| 213 | future. |
| 214 | |
Sandrine Bailleux | 8f23fa8 | 2017-09-28 21:58:12 +0100 | [diff] [blame] | 215 | The ``MAP_REGION()`` and ``MAP_REGION_FLAT()`` macros do not allow specifying a |
| 216 | mapping granularity, which leaves the library implementation free to choose |
| 217 | it. However, in cases where a specific granularity is required, the |
| 218 | ``MAP_REGION2()`` macro might be used instead. |
| 219 | |
Antonio Nino Diaz | b5d6809 | 2017-05-23 11:49:22 +0100 | [diff] [blame] | 220 | As explained earlier in this document, when the dynamic mapping feature is |
| 221 | disabled, there is no notion of dynamic regions. Conceptually, there are only |
| 222 | static regions. For this reason (and to retain backward compatibility with the |
| 223 | version 1 of the library), the APIs that map static regions do not embed the |
| 224 | word *static* in their functions names (for example ``mmap_add_region()``), in |
| 225 | contrast with the dynamic regions APIs (for example |
| 226 | ``mmap_add_dynamic_region()``). |
| 227 | |
| 228 | Although the definition of static and dynamic regions is not based on the state |
| 229 | of the MMU, the two are still related in some way. Static regions can only be |
| 230 | added before ``init_xlat_tables()`` is called and ``init_xlat_tables()`` must be |
| 231 | called while the MMU is still off. As a result, static regions cannot be added |
| 232 | once the MMU has been enabled. Dynamic regions can be added with the MMU on or |
| 233 | off. In practice, the usual call flow would look like this: |
| 234 | |
| 235 | #. The MMU is initially off. |
| 236 | |
| 237 | #. Add some static regions, add some dynamic regions. |
| 238 | |
| 239 | #. Initialize translation tables based on the list of mmap regions (using one of |
| 240 | the ``init_xlat_tables*()`` APIs). |
| 241 | |
| 242 | #. At this point, it is no longer possible to add static regions. Dynamic |
| 243 | regions can still be added or removed. |
| 244 | |
| 245 | #. Enable the MMU. |
| 246 | |
| 247 | #. Dynamic regions can continue to be added or removed. |
| 248 | |
| 249 | Because static regions are added early on at boot time and are all in the |
| 250 | control of the platform initialization code, the ``mmap_add*()`` family of APIs |
| 251 | are not expected to fail. They do not return any error code. |
| 252 | |
| 253 | Nonetheless, these APIs will check upfront whether the region can be |
| 254 | successfully added before updating the translation context structure. If the |
| 255 | library detects that there is insufficient memory to meet the request, or that |
| 256 | the new region will overlap another one in an invalid way, or if any other |
| 257 | unexpected error is encountered, they will print an error message on the UART. |
| 258 | Additionally, when asserts are enabled (typically in debug builds), an assertion |
| 259 | will be triggered. Otherwise, the function call will just return straight away, |
| 260 | without adding the offending memory region. |
| 261 | |
| 262 | |
| 263 | Library limitations |
| 264 | ------------------- |
| 265 | |
| 266 | Dynamic regions are not allowed to overlap each other. Static regions are |
| 267 | allowed to overlap as long as one of them is fully contained inside the other |
| 268 | one. This is allowed for backwards compatibility with the previous behaviour in |
| 269 | the version 1 of the library. |
| 270 | |
| 271 | |
| 272 | Implementation details |
| 273 | ---------------------- |
| 274 | |
| 275 | Code structure |
| 276 | ~~~~~~~~~~~~~~ |
| 277 | |
Antonio Nino Diaz | 2781808 | 2018-07-03 14:56:31 +0100 | [diff] [blame] | 278 | The library is divided into 4 modules: |
Antonio Nino Diaz | b5d6809 | 2017-05-23 11:49:22 +0100 | [diff] [blame] | 279 | |
Antonio Nino Diaz | 2781808 | 2018-07-03 14:56:31 +0100 | [diff] [blame] | 280 | - **Core module** |
Antonio Nino Diaz | b5d6809 | 2017-05-23 11:49:22 +0100 | [diff] [blame] | 281 | |
Antonio Nino Diaz | 2781808 | 2018-07-03 14:56:31 +0100 | [diff] [blame] | 282 | Provides the main functionality of the library, such as the initialization of |
| 283 | translation tables contexts and mapping/unmapping memory regions. This module |
| 284 | provides functions such as ``mmap_add_region_ctx`` that let the caller specify |
| 285 | the translation tables context affected by them. |
Antonio Nino Diaz | b5d6809 | 2017-05-23 11:49:22 +0100 | [diff] [blame] | 286 | |
Paul Beesley | f864067 | 2019-04-12 14:19:42 +0100 | [diff] [blame] | 287 | See ``xlat_tables_core.c``. |
Antonio Nino Diaz | b5d6809 | 2017-05-23 11:49:22 +0100 | [diff] [blame] | 288 | |
Antonio Nino Diaz | 2781808 | 2018-07-03 14:56:31 +0100 | [diff] [blame] | 289 | - **Active context module** |
Antonio Nino Diaz | b5d6809 | 2017-05-23 11:49:22 +0100 | [diff] [blame] | 290 | |
Antonio Nino Diaz | 2781808 | 2018-07-03 14:56:31 +0100 | [diff] [blame] | 291 | Instantiates the context that is used by the current BL image and provides |
| 292 | helpers to manipulate it, abstracting it from the rest of the code. |
| 293 | This module provides functions such as ``mmap_add_region``, that directly |
| 294 | affect the BL image using them. |
| 295 | |
Paul Beesley | f864067 | 2019-04-12 14:19:42 +0100 | [diff] [blame] | 296 | See ``xlat_tables_context.c``. |
Antonio Nino Diaz | 2781808 | 2018-07-03 14:56:31 +0100 | [diff] [blame] | 297 | |
| 298 | - **Utilities module** |
| 299 | |
| 300 | Provides additional functionality like debug print of the current state of the |
| 301 | translation tables and helpers to query memory attributes and to modify them. |
| 302 | |
Paul Beesley | f864067 | 2019-04-12 14:19:42 +0100 | [diff] [blame] | 303 | See ``xlat_tables_utils.c``. |
Antonio Nino Diaz | 2781808 | 2018-07-03 14:56:31 +0100 | [diff] [blame] | 304 | |
| 305 | - **Architectural module** |
| 306 | |
| 307 | Provides functions that are dependent on the current execution state |
| 308 | (AArch32/AArch64), such as the functions used for TLB invalidation, setup the |
| 309 | MMU, or calculate the Physical Address Space size. They do not need a |
| 310 | translation context to work on. |
| 311 | |
Paul Beesley | f864067 | 2019-04-12 14:19:42 +0100 | [diff] [blame] | 312 | See ``aarch32/xlat_tables_arch.c`` and ``aarch64/xlat_tables_arch.c``. |
Antonio Nino Diaz | b5d6809 | 2017-05-23 11:49:22 +0100 | [diff] [blame] | 313 | |
Sandrine Bailleux | 8f23fa8 | 2017-09-28 21:58:12 +0100 | [diff] [blame] | 314 | From mmap regions to translation tables |
Antonio Nino Diaz | 2781808 | 2018-07-03 14:56:31 +0100 | [diff] [blame] | 315 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
Sandrine Bailleux | 8f23fa8 | 2017-09-28 21:58:12 +0100 | [diff] [blame] | 316 | |
Antonio Nino Diaz | 2781808 | 2018-07-03 14:56:31 +0100 | [diff] [blame] | 317 | A translation context contains a list of ``mmap_region_t``, which holds the |
| 318 | information of all the regions that are mapped at any given time. Whenever there |
| 319 | is a request to map (resp. unmap) a memory region, it is added to (resp. removed |
| 320 | from) the ``mmap_region_t`` list. |
Antonio Nino Diaz | b5d6809 | 2017-05-23 11:49:22 +0100 | [diff] [blame] | 321 | |
| 322 | The mmap regions list is a conceptual way to represent the memory layout. At |
| 323 | some point, the library has to convert this information into actual translation |
| 324 | tables to program into the MMU. |
| 325 | |
| 326 | Before the ``init_xlat_tables()`` API is called, the library only acts on the |
| 327 | mmap regions list. Adding a static or dynamic region at this point through one |
| 328 | of the ``mmap_add*()`` APIs does not affect the translation tables in any way, |
| 329 | they only get registered in the internal mmap region list. It is only when the |
| 330 | user calls the ``init_xlat_tables()`` that the translation tables are populated |
| 331 | in memory based on the list of mmap regions registered so far. This is an |
| 332 | optimization that allows creation of the initial set of translation tables in |
| 333 | one go, rather than having to edit them every time while the MMU is disabled. |
| 334 | |
| 335 | After the ``init_xlat_tables()`` API has been called, only dynamic regions can |
| 336 | be added. Changes to the translation tables (as well as the mmap regions list) |
| 337 | will take effect immediately. |
| 338 | |
Sandrine Bailleux | 8f23fa8 | 2017-09-28 21:58:12 +0100 | [diff] [blame] | 339 | The memory mapping algorithm |
Antonio Nino Diaz | 2781808 | 2018-07-03 14:56:31 +0100 | [diff] [blame] | 340 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
Sandrine Bailleux | 8f23fa8 | 2017-09-28 21:58:12 +0100 | [diff] [blame] | 341 | |
Antonio Nino Diaz | b5d6809 | 2017-05-23 11:49:22 +0100 | [diff] [blame] | 342 | The mapping function is implemented as a recursive algorithm. It is however |
Dan Handley | 610e7e1 | 2018-03-01 18:44:00 +0000 | [diff] [blame] | 343 | bound by the level of depth of the translation tables (the Armv8-A architecture |
Antonio Nino Diaz | b5d6809 | 2017-05-23 11:49:22 +0100 | [diff] [blame] | 344 | allows up to 4 lookup levels). |
| 345 | |
Paul Beesley | f864067 | 2019-04-12 14:19:42 +0100 | [diff] [blame] | 346 | By default [#granularity]_, the algorithm will attempt to minimize the |
Sandrine Bailleux | 8f23fa8 | 2017-09-28 21:58:12 +0100 | [diff] [blame] | 347 | number of translation tables created to satisfy the user's request. It will |
| 348 | favour mapping a region using the biggest possible blocks, only creating a |
| 349 | sub-table if it is strictly necessary. This is to reduce the memory footprint of |
| 350 | the firmware. |
Antonio Nino Diaz | b5d6809 | 2017-05-23 11:49:22 +0100 | [diff] [blame] | 351 | |
| 352 | The most common reason for needing a sub-table is when a specific mapping |
| 353 | requires a finer granularity. Misaligned regions also require a finer |
| 354 | granularity than what the user may had originally expected, using a lot more |
| 355 | memory than expected. The reason is that all levels of translation are |
| 356 | restricted to address translations of the same granularity as the size of the |
| 357 | blocks of that level. For example, for a 4 KiB page size, a level 2 block entry |
| 358 | can only translate up to a granularity of 2 MiB. If the Physical Address is not |
| 359 | aligned to 2 MiB then additional level 3 tables are also needed. |
| 360 | |
| 361 | Note that not every translation level allows any type of descriptor. Depending |
| 362 | on the page size, levels 0 and 1 of translation may only allow table |
| 363 | descriptors. If a block entry could be able to describe a translation, but that |
| 364 | level does not allow block descriptors, a table descriptor will have to be used |
| 365 | instead, as well as additional tables at the next level. |
| 366 | |
| 367 | |Alignment Example| |
| 368 | |
| 369 | The mmap regions are sorted in a way that simplifies the code that maps |
| 370 | them. Even though this ordering is only strictly needed for overlapping static |
| 371 | regions, it must also be applied for dynamic regions to maintain a consistent |
| 372 | order of all regions at all times. As each new region is mapped, existing |
| 373 | entries in the translation tables are checked to ensure consistency. Please |
| 374 | refer to the comments in the source code of the core module for more details |
| 375 | about the sorting algorithm in use. |
| 376 | |
Sandrine Bailleux | 8f23fa8 | 2017-09-28 21:58:12 +0100 | [diff] [blame] | 377 | TLB maintenance operations |
Antonio Nino Diaz | 2781808 | 2018-07-03 14:56:31 +0100 | [diff] [blame] | 378 | ~~~~~~~~~~~~~~~~~~~~~~~~~~ |
Sandrine Bailleux | 8f23fa8 | 2017-09-28 21:58:12 +0100 | [diff] [blame] | 379 | |
Antonio Nino Diaz | b5d6809 | 2017-05-23 11:49:22 +0100 | [diff] [blame] | 380 | The library takes care of performing TLB maintenance operations when required. |
| 381 | For example, when the user requests removing a dynamic region, the library |
| 382 | invalidates all TLB entries associated to that region to ensure that these |
| 383 | changes are visible to subsequent execution, including speculative execution, |
| 384 | that uses the changed translation table entries. |
| 385 | |
| 386 | A counter-example is the initialization of translation tables. In this case, |
Dan Handley | 610e7e1 | 2018-03-01 18:44:00 +0000 | [diff] [blame] | 387 | explicit TLB maintenance is not required. The Armv8-A architecture guarantees |
Antonio Nino Diaz | b5d6809 | 2017-05-23 11:49:22 +0100 | [diff] [blame] | 388 | that all TLBs are disabled from reset and their contents have no effect on |
| 389 | address translation at reset [#tlb-reset-ref]_. Therefore, the TLBs invalidation |
| 390 | is deferred to the ``enable_mmu*()`` family of functions, just before the MMU is |
| 391 | turned on. |
| 392 | |
| 393 | TLB invalidation is not required when adding dynamic regions either. Dynamic |
| 394 | regions are not allowed to overlap existing memory region. Therefore, if the |
| 395 | dynamic mapping request is deemed legitimate, it automatically concerns memory |
| 396 | that was not mapped in this translation regime and the library will have |
| 397 | initialized its corresponding translation table entry to an invalid |
| 398 | descriptor. Given that the TLBs are not architecturally permitted to hold any |
| 399 | invalid translation table entry [#tlb-no-invalid-entry]_, this means that this |
| 400 | mapping cannot be cached in the TLBs. |
| 401 | |
Paul Beesley | f864067 | 2019-04-12 14:19:42 +0100 | [diff] [blame] | 402 | .. rubric:: Footnotes |
| 403 | |
| 404 | .. [#granularity] That is, when mmap regions do not enforce their mapping |
| 405 | granularity. |
| 406 | |
| 407 | .. [#tlb-reset-ref] See section D4.9 ``Translation Lookaside Buffers (TLBs)``, |
| 408 | subsection ``TLB behavior at reset`` in Armv8-A, rev C.a. |
| 409 | |
| 410 | .. [#tlb-no-invalid-entry] See section D4.10.1 ``General TLB maintenance |
| 411 | requirements`` in Armv8-A, rev C.a. |
Antonio Nino Diaz | b5d6809 | 2017-05-23 11:49:22 +0100 | [diff] [blame] | 412 | |
| 413 | -------------- |
| 414 | |
Paul Beesley | f864067 | 2019-04-12 14:19:42 +0100 | [diff] [blame] | 415 | *Copyright (c) 2017-2019, Arm Limited and Contributors. All rights reserved.* |
Antonio Nino Diaz | b5d6809 | 2017-05-23 11:49:22 +0100 | [diff] [blame] | 416 | |
Paul Beesley | 814f8c0 | 2019-03-13 15:49:27 +0000 | [diff] [blame] | 417 | .. |Alignment Example| image:: ../resources/diagrams/xlat_align.png |