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