xref: /aosp_15_r20/external/arm-trusted-firmware/docs/components/xlat-tables-lib-v2-design.rst (revision 54fd6939e177f8ff529b10183254802c76df6d08)
1*54fd6939SJiyong ParkTranslation (XLAT) Tables Library
2*54fd6939SJiyong Park=================================
3*54fd6939SJiyong Park
4*54fd6939SJiyong ParkThis document describes the design of the translation tables library (version 2)
5*54fd6939SJiyong Parkused by Trusted Firmware-A (TF-A). This library provides APIs to create page
6*54fd6939SJiyong Parktables based on a description of the memory layout, as well as setting up system
7*54fd6939SJiyong Parkregisters related to the Memory Management Unit (MMU) and performing the
8*54fd6939SJiyong Parkrequired Translation Lookaside Buffer (TLB) maintenance operations.
9*54fd6939SJiyong Park
10*54fd6939SJiyong ParkMore specifically, some use cases that this library aims to support are:
11*54fd6939SJiyong Park
12*54fd6939SJiyong Park#. Statically allocate translation tables and populate them (at run-time) based
13*54fd6939SJiyong Park   upon a description of the memory layout. The memory layout is typically
14*54fd6939SJiyong Park   provided by the platform port as a list of memory regions;
15*54fd6939SJiyong Park
16*54fd6939SJiyong Park#. Support for generating translation tables pertaining to a different
17*54fd6939SJiyong Park   translation regime than the exception level the library code is executing at;
18*54fd6939SJiyong Park
19*54fd6939SJiyong Park#. Support for dynamic mapping and unmapping of regions, even while the MMU is
20*54fd6939SJiyong Park   on. This can be used to temporarily map some memory regions and unmap them
21*54fd6939SJiyong Park   later on when no longer needed;
22*54fd6939SJiyong Park
23*54fd6939SJiyong Park#. Support for non-identity virtual to physical mappings to compress the virtual
24*54fd6939SJiyong Park   address space;
25*54fd6939SJiyong Park
26*54fd6939SJiyong Park#. Support for changing memory attributes of memory regions at run-time.
27*54fd6939SJiyong Park
28*54fd6939SJiyong Park
29*54fd6939SJiyong ParkAbout version 1, version 2 and MPU libraries
30*54fd6939SJiyong Park--------------------------------------------
31*54fd6939SJiyong Park
32*54fd6939SJiyong ParkThis document focuses on version 2 of the library, whose sources are available
33*54fd6939SJiyong Parkin the ``lib/xlat_tables_v2`` directory. Version 1 of the library can still be
34*54fd6939SJiyong Parkfound in ``lib/xlat_tables`` directory but it is less flexible and doesn't
35*54fd6939SJiyong Parksupport dynamic mapping. ``lib/xlat_mpu``, which configures Arm's MPU
36*54fd6939SJiyong Parkequivalently, is also addressed here. The ``lib/xlat_mpu`` is experimental,
37*54fd6939SJiyong Parkmeaning that its API may change. It currently strives for consistency and
38*54fd6939SJiyong Parkcode-reuse with xlat_tables_v2.  Future versions may be more MPU-specific (e.g.,
39*54fd6939SJiyong Parkremoving all mentions of virtual addresses). Although potential bug fixes will
40*54fd6939SJiyong Parkbe applied to all versions of the xlat_* libs, future feature enhancements will
41*54fd6939SJiyong Parkfocus on version 2 and might not be back-ported to version 1 and MPU versions.
42*54fd6939SJiyong ParkTherefore, it is recommended to use version 2, especially for new platform
43*54fd6939SJiyong Parkports (unless the platform uses an MPU).
44*54fd6939SJiyong Park
45*54fd6939SJiyong ParkHowever, please note that version 2 and the MPU version are still in active
46*54fd6939SJiyong Parkdevelopment and is not considered stable yet. Hence, compatibility breaks might
47*54fd6939SJiyong Parkbe introduced.
48*54fd6939SJiyong Park
49*54fd6939SJiyong ParkFrom this point onwards, this document will implicitly refer to version 2 of the
50*54fd6939SJiyong Parklibrary, unless stated otherwise.
51*54fd6939SJiyong Park
52*54fd6939SJiyong Park
53*54fd6939SJiyong ParkDesign concepts and interfaces
54*54fd6939SJiyong Park------------------------------
55*54fd6939SJiyong Park
56*54fd6939SJiyong ParkThis section presents some of the key concepts and data structures used in the
57*54fd6939SJiyong Parktranslation tables library.
58*54fd6939SJiyong Park
59*54fd6939SJiyong Park`mmap` regions
60*54fd6939SJiyong Park~~~~~~~~~~~~~~
61*54fd6939SJiyong Park
62*54fd6939SJiyong ParkAn ``mmap_region`` is an abstract, concise way to represent a memory region to
63*54fd6939SJiyong Parkmap. It is one of the key interfaces to the library. It is identified by:
64*54fd6939SJiyong Park
65*54fd6939SJiyong Park- its physical base address;
66*54fd6939SJiyong Park- its virtual base address;
67*54fd6939SJiyong Park- its size;
68*54fd6939SJiyong Park- its attributes;
69*54fd6939SJiyong Park- its mapping granularity (optional).
70*54fd6939SJiyong Park
71*54fd6939SJiyong ParkSee the ``struct mmap_region`` type in ``xlat_tables_v2.h``.
72*54fd6939SJiyong Park
73*54fd6939SJiyong ParkThe user usually provides a list of such mmap regions to map and lets the
74*54fd6939SJiyong Parklibrary transpose that in a set of translation tables. As a result, the library
75*54fd6939SJiyong Parkmight create new translation tables, update or split existing ones.
76*54fd6939SJiyong Park
77*54fd6939SJiyong ParkThe region attributes specify the type of memory (for example device or cached
78*54fd6939SJiyong Parknormal memory) as well as the memory access permissions (read-only or
79*54fd6939SJiyong Parkread-write, executable or not, secure or non-secure, and so on). In the case of
80*54fd6939SJiyong Parkthe EL1&0 translation regime, the attributes also specify whether the region is
81*54fd6939SJiyong Parka User region (EL0) or Privileged region (EL1). See the ``MT_xxx`` definitions
82*54fd6939SJiyong Parkin ``xlat_tables_v2.h``. Note that for the EL1&0 translation regime the Execute
83*54fd6939SJiyong ParkNever attribute is set simultaneously for both EL1 and EL0.
84*54fd6939SJiyong Park
85*54fd6939SJiyong ParkThe granularity controls the translation table level to go down to when mapping
86*54fd6939SJiyong Parkthe region. For example, assuming the MMU has been configured to use a 4KB
87*54fd6939SJiyong Parkgranule size, the library might map a 2MB memory region using either of the two
88*54fd6939SJiyong Parkfollowing options:
89*54fd6939SJiyong Park
90*54fd6939SJiyong Park- using a single level-2 translation table entry;
91*54fd6939SJiyong Park- using a level-2 intermediate entry to a level-3 translation table (which
92*54fd6939SJiyong Park  contains 512 entries, each mapping 4KB).
93*54fd6939SJiyong Park
94*54fd6939SJiyong ParkThe first solution potentially requires less translation tables, hence
95*54fd6939SJiyong Parkpotentially less memory.  However, if part of this 2MB region is later remapped
96*54fd6939SJiyong Parkwith different memory attributes, the library might need to split the existing
97*54fd6939SJiyong Parkpage tables to refine the mappings. If a single level-2 entry has been used
98*54fd6939SJiyong Parkhere, a level-3 table will need to be allocated on the fly and the level-2
99*54fd6939SJiyong Parkmodified to point to this new level-3 table. This has a performance cost at
100*54fd6939SJiyong Parkrun-time.
101*54fd6939SJiyong Park
102*54fd6939SJiyong ParkIf the user knows upfront that such a remapping operation is likely to happen
103*54fd6939SJiyong Parkthen they might enforce a 4KB mapping granularity for this 2MB region from the
104*54fd6939SJiyong Parkbeginning; remapping some of these 4KB pages on the fly then becomes a
105*54fd6939SJiyong Parklightweight operation.
106*54fd6939SJiyong Park
107*54fd6939SJiyong ParkThe region's granularity is an optional field; if it is not specified the
108*54fd6939SJiyong Parklibrary will choose the mapping granularity for this region as it sees fit (more
109*54fd6939SJiyong Parkdetails can be found in `The memory mapping algorithm`_ section below).
110*54fd6939SJiyong Park
111*54fd6939SJiyong ParkThe MPU library also uses ``struct mmap_region`` to specify translations, but
112*54fd6939SJiyong Parkthe MPU's translations are limited to specification of valid addresses and
113*54fd6939SJiyong Parkaccess permissions.  If the requested virtual and physical addresses mismatch
114*54fd6939SJiyong Parkthe system will panic. Being register-based for deterministic memory-reference
115*54fd6939SJiyong Parktiming, the MPU hardware does not involve memory-resident translation tables.
116*54fd6939SJiyong Park
117*54fd6939SJiyong ParkCurrently, the MPU library is also limited to MPU translation at EL2 with no
118*54fd6939SJiyong ParkMMU translation at other ELs.  These limitations, however, are expected to be
119*54fd6939SJiyong Parkovercome in future library versions.
120*54fd6939SJiyong Park
121*54fd6939SJiyong ParkTranslation Context
122*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~
123*54fd6939SJiyong Park
124*54fd6939SJiyong ParkThe library can create or modify translation tables pertaining to a different
125*54fd6939SJiyong Parktranslation regime than the exception level the library code is executing at.
126*54fd6939SJiyong ParkFor example, the library might be used by EL3 software (for instance BL31) to
127*54fd6939SJiyong Parkcreate translation tables pertaining to the S-EL1&0 translation regime.
128*54fd6939SJiyong Park
129*54fd6939SJiyong ParkThis flexibility comes from the use of *translation contexts*. A *translation
130*54fd6939SJiyong Parkcontext* constitutes the superset of information used by the library to track
131*54fd6939SJiyong Parkthe status of a set of translation tables for a given translation regime.
132*54fd6939SJiyong Park
133*54fd6939SJiyong ParkThe library internally allocates a default translation context, which pertains
134*54fd6939SJiyong Parkto the translation regime of the current exception level. Additional contexts
135*54fd6939SJiyong Parkmay be explicitly allocated and initialized using the
136*54fd6939SJiyong Park``REGISTER_XLAT_CONTEXT()`` macro. Separate APIs are provided to act either on
137*54fd6939SJiyong Parkthe default translation context or on an alternative one.
138*54fd6939SJiyong Park
139*54fd6939SJiyong ParkTo register a translation context, the user must provide the library with the
140*54fd6939SJiyong Parkfollowing information:
141*54fd6939SJiyong Park
142*54fd6939SJiyong Park* A name.
143*54fd6939SJiyong Park
144*54fd6939SJiyong Park  The resulting translation context variable will be called after this name, to
145*54fd6939SJiyong Park  which ``_xlat_ctx`` is appended. For example, if the macro name parameter is
146*54fd6939SJiyong Park  ``foo``, the context variable name will be ``foo_xlat_ctx``.
147*54fd6939SJiyong Park
148*54fd6939SJiyong Park* The maximum number of `mmap` regions to map.
149*54fd6939SJiyong Park
150*54fd6939SJiyong Park  Should account for both static and dynamic regions, if applicable.
151*54fd6939SJiyong Park
152*54fd6939SJiyong Park* The number of sub-translation tables to allocate.
153*54fd6939SJiyong Park
154*54fd6939SJiyong Park  Number of translation tables to statically allocate for this context,
155*54fd6939SJiyong Park  excluding the initial lookup level translation table, which is always
156*54fd6939SJiyong Park  allocated. For example, if the initial lookup level is 1, this parameter would
157*54fd6939SJiyong Park  specify the number of level-2 and level-3 translation tables to pre-allocate
158*54fd6939SJiyong Park  for this context.
159*54fd6939SJiyong Park
160*54fd6939SJiyong Park* The size of the virtual address space.
161*54fd6939SJiyong Park
162*54fd6939SJiyong Park  Size in bytes of the virtual address space to map using this context. This
163*54fd6939SJiyong Park  will incidentally determine the number of entries in the initial lookup level
164*54fd6939SJiyong Park  translation table : the library will allocate as many entries as is required
165*54fd6939SJiyong Park  to map the entire virtual address space.
166*54fd6939SJiyong Park
167*54fd6939SJiyong Park* The size of the physical address space.
168*54fd6939SJiyong Park
169*54fd6939SJiyong Park  Size in bytes of the physical address space to map using this context.
170*54fd6939SJiyong Park
171*54fd6939SJiyong ParkThe default translation context is internally initialized using information
172*54fd6939SJiyong Parkcoming (for the most part) from platform-specific defines:
173*54fd6939SJiyong Park
174*54fd6939SJiyong Park- name: hard-coded to ``tf`` ; hence the name of the default context variable is
175*54fd6939SJiyong Park  ``tf_xlat_ctx``;
176*54fd6939SJiyong Park- number of `mmap` regions: ``MAX_MMAP_REGIONS``;
177*54fd6939SJiyong Park- number of sub-translation tables: ``MAX_XLAT_TABLES``;
178*54fd6939SJiyong Park- size of the virtual address space: ``PLAT_VIRT_ADDR_SPACE_SIZE``;
179*54fd6939SJiyong Park- size of the physical address space: ``PLAT_PHY_ADDR_SPACE_SIZE``.
180*54fd6939SJiyong Park
181*54fd6939SJiyong ParkPlease refer to the :ref:`Porting Guide` for more details about these macros.
182*54fd6939SJiyong Park
183*54fd6939SJiyong Park
184*54fd6939SJiyong ParkStatic and dynamic memory regions
185*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
186*54fd6939SJiyong Park
187*54fd6939SJiyong ParkThe library optionally supports dynamic memory mapping. This feature may be
188*54fd6939SJiyong Parkenabled using the ``PLAT_XLAT_TABLES_DYNAMIC`` platform build flag.
189*54fd6939SJiyong Park
190*54fd6939SJiyong ParkWhen dynamic memory mapping is enabled, the library categorises mmap regions as
191*54fd6939SJiyong Park*static* or *dynamic*.
192*54fd6939SJiyong Park
193*54fd6939SJiyong Park- *Static regions* are fixed for the lifetime of the system. They can only be
194*54fd6939SJiyong Park  added early on, before the translation tables are created and populated. They
195*54fd6939SJiyong Park  cannot be removed afterwards.
196*54fd6939SJiyong Park
197*54fd6939SJiyong Park- *Dynamic regions* can be added or removed any time.
198*54fd6939SJiyong Park
199*54fd6939SJiyong ParkWhen the dynamic memory mapping feature is disabled, only static regions exist.
200*54fd6939SJiyong Park
201*54fd6939SJiyong ParkThe dynamic memory mapping feature may be used to map and unmap transient memory
202*54fd6939SJiyong Parkareas. This is useful when the user needs to access some memory for a fixed
203*54fd6939SJiyong Parkperiod of time, after which the memory may be discarded and reclaimed. For
204*54fd6939SJiyong Parkexample, a memory region that is only required at boot time while the system is
205*54fd6939SJiyong Parkinitializing, or to temporarily share a memory buffer between the normal world
206*54fd6939SJiyong Parkand trusted world. Note that it is up to the caller to ensure that these regions
207*54fd6939SJiyong Parkare not accessed concurrently while the regions are being added or removed.
208*54fd6939SJiyong Park
209*54fd6939SJiyong ParkAlthough this feature provides some level of dynamic memory allocation, this
210*54fd6939SJiyong Parkdoes not allow dynamically allocating an arbitrary amount of memory at an
211*54fd6939SJiyong Parkarbitrary memory location. The user is still required to declare at compile-time
212*54fd6939SJiyong Parkthe limits of these allocations ; the library will deny any mapping request that
213*54fd6939SJiyong Parkdoes not fit within this pre-allocated pool of memory.
214*54fd6939SJiyong Park
215*54fd6939SJiyong Park
216*54fd6939SJiyong ParkLibrary APIs
217*54fd6939SJiyong Park------------
218*54fd6939SJiyong Park
219*54fd6939SJiyong ParkThe external APIs exposed by this library are declared and documented in the
220*54fd6939SJiyong Park``xlat_tables_v2.h`` header file. This should be the reference point for
221*54fd6939SJiyong Parkgetting information about the usage of the different APIs this library
222*54fd6939SJiyong Parkprovides. This section just provides some extra details and clarifications.
223*54fd6939SJiyong Park
224*54fd6939SJiyong ParkAlthough the ``mmap_region`` structure is a publicly visible type, it is not
225*54fd6939SJiyong Parkrecommended to populate these structures by hand. Instead, wherever APIs expect
226*54fd6939SJiyong Parkfunction arguments of type ``mmap_region_t``, these should be constructed using
227*54fd6939SJiyong Parkthe ``MAP_REGION*()`` family of helper macros. This is to limit the risk of
228*54fd6939SJiyong Parkcompatibility breaks, should the ``mmap_region`` structure type evolve in the
229*54fd6939SJiyong Parkfuture.
230*54fd6939SJiyong Park
231*54fd6939SJiyong ParkThe ``MAP_REGION()`` and ``MAP_REGION_FLAT()`` macros do not allow specifying a
232*54fd6939SJiyong Parkmapping granularity, which leaves the library implementation free to choose
233*54fd6939SJiyong Parkit. However, in cases where a specific granularity is required, the
234*54fd6939SJiyong Park``MAP_REGION2()`` macro might be used instead. Using ``MAP_REGION_FLAT()`` only
235*54fd6939SJiyong Parkto define regions for the MPU library is strongly recommended.
236*54fd6939SJiyong Park
237*54fd6939SJiyong ParkAs explained earlier in this document, when the dynamic mapping feature is
238*54fd6939SJiyong Parkdisabled, there is no notion of dynamic regions. Conceptually, there are only
239*54fd6939SJiyong Parkstatic regions. For this reason (and to retain backward compatibility with the
240*54fd6939SJiyong Parkversion 1 of the library), the APIs that map static regions do not embed the
241*54fd6939SJiyong Parkword *static* in their functions names (for example ``mmap_add_region()``), in
242*54fd6939SJiyong Parkcontrast with the dynamic regions APIs (for example
243*54fd6939SJiyong Park``mmap_add_dynamic_region()``).
244*54fd6939SJiyong Park
245*54fd6939SJiyong ParkAlthough the definition of static and dynamic regions is not based on the state
246*54fd6939SJiyong Parkof the MMU, the two are still related in some way. Static regions can only be
247*54fd6939SJiyong Parkadded before ``init_xlat_tables()`` is called and ``init_xlat_tables()`` must be
248*54fd6939SJiyong Parkcalled while the MMU is still off. As a result, static regions cannot be added
249*54fd6939SJiyong Parkonce the MMU has been enabled. Dynamic regions can be added with the MMU on or
250*54fd6939SJiyong Parkoff. In practice, the usual call flow would look like this:
251*54fd6939SJiyong Park
252*54fd6939SJiyong Park#. The MMU is initially off.
253*54fd6939SJiyong Park
254*54fd6939SJiyong Park#. Add some static regions, add some dynamic regions.
255*54fd6939SJiyong Park
256*54fd6939SJiyong Park#. Initialize translation tables based on the list of mmap regions (using one of
257*54fd6939SJiyong Park   the ``init_xlat_tables*()`` APIs).
258*54fd6939SJiyong Park
259*54fd6939SJiyong Park#. At this point, it is no longer possible to add static regions. Dynamic
260*54fd6939SJiyong Park   regions can still be added or removed.
261*54fd6939SJiyong Park
262*54fd6939SJiyong Park#. Enable the MMU.
263*54fd6939SJiyong Park
264*54fd6939SJiyong Park#. Dynamic regions can continue to be added or removed.
265*54fd6939SJiyong Park
266*54fd6939SJiyong ParkBecause static regions are added early on at boot time and are all in the
267*54fd6939SJiyong Parkcontrol of the platform initialization code, the ``mmap_add*()`` family of APIs
268*54fd6939SJiyong Parkare not expected to fail. They do not return any error code.
269*54fd6939SJiyong Park
270*54fd6939SJiyong ParkNonetheless, these APIs will check upfront whether the region can be
271*54fd6939SJiyong Parksuccessfully added before updating the translation context structure. If the
272*54fd6939SJiyong Parklibrary detects that there is insufficient memory to meet the request, or that
273*54fd6939SJiyong Parkthe new region will overlap another one in an invalid way, or if any other
274*54fd6939SJiyong Parkunexpected error is encountered, they will print an error message on the UART.
275*54fd6939SJiyong ParkAdditionally, when asserts are enabled (typically in debug builds), an assertion
276*54fd6939SJiyong Parkwill be triggered. Otherwise, the function call will just return straight away,
277*54fd6939SJiyong Parkwithout adding the offending memory region.
278*54fd6939SJiyong Park
279*54fd6939SJiyong Park
280*54fd6939SJiyong ParkLibrary limitations
281*54fd6939SJiyong Park-------------------
282*54fd6939SJiyong Park
283*54fd6939SJiyong ParkDynamic regions are not allowed to overlap each other. Static regions are
284*54fd6939SJiyong Parkallowed to overlap as long as one of them is fully contained inside the other
285*54fd6939SJiyong Parkone. This is allowed for backwards compatibility with the previous behaviour in
286*54fd6939SJiyong Parkthe version 1 of the library.
287*54fd6939SJiyong Park
288*54fd6939SJiyong Park
289*54fd6939SJiyong ParkImplementation details
290*54fd6939SJiyong Park----------------------
291*54fd6939SJiyong Park
292*54fd6939SJiyong ParkCode structure
293*54fd6939SJiyong Park~~~~~~~~~~~~~~
294*54fd6939SJiyong Park
295*54fd6939SJiyong ParkThe library is divided into 4 modules:
296*54fd6939SJiyong Park
297*54fd6939SJiyong Park- **Core module**
298*54fd6939SJiyong Park
299*54fd6939SJiyong Park  Provides the main functionality of the library, such as the initialization of
300*54fd6939SJiyong Park  translation tables contexts and mapping/unmapping memory regions. This module
301*54fd6939SJiyong Park  provides functions such as ``mmap_add_region_ctx`` that let the caller specify
302*54fd6939SJiyong Park  the translation tables context affected by them.
303*54fd6939SJiyong Park
304*54fd6939SJiyong Park  See ``xlat_tables_core.c``.
305*54fd6939SJiyong Park
306*54fd6939SJiyong Park- **Active context module**
307*54fd6939SJiyong Park
308*54fd6939SJiyong Park  Instantiates the context that is used by the current BL image and provides
309*54fd6939SJiyong Park  helpers to manipulate it, abstracting it from the rest of the code.
310*54fd6939SJiyong Park  This module provides functions such as ``mmap_add_region``, that directly
311*54fd6939SJiyong Park  affect the BL image using them.
312*54fd6939SJiyong Park
313*54fd6939SJiyong Park  See ``xlat_tables_context.c``.
314*54fd6939SJiyong Park
315*54fd6939SJiyong Park- **Utilities module**
316*54fd6939SJiyong Park
317*54fd6939SJiyong Park  Provides additional functionality like debug print of the current state of the
318*54fd6939SJiyong Park  translation tables and helpers to query memory attributes and to modify them.
319*54fd6939SJiyong Park
320*54fd6939SJiyong Park  See ``xlat_tables_utils.c``.
321*54fd6939SJiyong Park
322*54fd6939SJiyong Park- **Architectural module**
323*54fd6939SJiyong Park
324*54fd6939SJiyong Park  Provides functions that are dependent on the current execution state
325*54fd6939SJiyong Park  (AArch32/AArch64), such as the functions used for TLB invalidation, setup the
326*54fd6939SJiyong Park  MMU, or calculate the Physical Address Space size. They do not need a
327*54fd6939SJiyong Park  translation context to work on.
328*54fd6939SJiyong Park
329*54fd6939SJiyong Park  See ``aarch32/xlat_tables_arch.c`` and ``aarch64/xlat_tables_arch.c``.
330*54fd6939SJiyong Park
331*54fd6939SJiyong ParkFrom mmap regions to translation tables
332*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
333*54fd6939SJiyong Park
334*54fd6939SJiyong ParkA translation context contains a list of ``mmap_region_t``, which holds the
335*54fd6939SJiyong Parkinformation of all the regions that are mapped at any given time. Whenever there
336*54fd6939SJiyong Parkis a request to map (resp. unmap) a memory region, it is added to (resp. removed
337*54fd6939SJiyong Parkfrom) the ``mmap_region_t`` list.
338*54fd6939SJiyong Park
339*54fd6939SJiyong ParkThe mmap regions list is a conceptual way to represent the memory layout. At
340*54fd6939SJiyong Parksome point, the library has to convert this information into actual translation
341*54fd6939SJiyong Parktables to program into the MMU.
342*54fd6939SJiyong Park
343*54fd6939SJiyong ParkBefore the ``init_xlat_tables()`` API is called, the library only acts on the
344*54fd6939SJiyong Parkmmap regions list. Adding a static or dynamic region at this point through one
345*54fd6939SJiyong Parkof the ``mmap_add*()`` APIs does not affect the translation tables in any way,
346*54fd6939SJiyong Parkthey only get registered in the internal mmap region list. It is only when the
347*54fd6939SJiyong Parkuser calls the ``init_xlat_tables()`` that the translation tables are populated
348*54fd6939SJiyong Parkin memory based on the list of mmap regions registered so far. This is an
349*54fd6939SJiyong Parkoptimization that allows creation of the initial set of translation tables in
350*54fd6939SJiyong Parkone go, rather than having to edit them every time while the MMU is disabled.
351*54fd6939SJiyong Park
352*54fd6939SJiyong ParkAfter the ``init_xlat_tables()`` API has been called, only dynamic regions can
353*54fd6939SJiyong Parkbe added. Changes to the translation tables (as well as the mmap regions list)
354*54fd6939SJiyong Parkwill take effect immediately.
355*54fd6939SJiyong Park
356*54fd6939SJiyong ParkThe memory mapping algorithm
357*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~~~~~~~~~
358*54fd6939SJiyong Park
359*54fd6939SJiyong ParkThe mapping function is implemented as a recursive algorithm. It is however
360*54fd6939SJiyong Parkbound by the level of depth of the translation tables (the Armv8-A architecture
361*54fd6939SJiyong Parkallows up to 4 lookup levels).
362*54fd6939SJiyong Park
363*54fd6939SJiyong ParkBy default [#granularity]_, the algorithm will attempt to minimize the
364*54fd6939SJiyong Parknumber of translation tables created to satisfy the user's request. It will
365*54fd6939SJiyong Parkfavour mapping a region using the biggest possible blocks, only creating a
366*54fd6939SJiyong Parksub-table if it is strictly necessary. This is to reduce the memory footprint of
367*54fd6939SJiyong Parkthe firmware.
368*54fd6939SJiyong Park
369*54fd6939SJiyong ParkThe most common reason for needing a sub-table is when a specific mapping
370*54fd6939SJiyong Parkrequires a finer granularity. Misaligned regions also require a finer
371*54fd6939SJiyong Parkgranularity than what the user may had originally expected, using a lot more
372*54fd6939SJiyong Parkmemory than expected. The reason is that all levels of translation are
373*54fd6939SJiyong Parkrestricted to address translations of the same granularity as the size of the
374*54fd6939SJiyong Parkblocks of that level.  For example, for a 4 KiB page size, a level 2 block entry
375*54fd6939SJiyong Parkcan only translate up to a granularity of 2 MiB. If the Physical Address is not
376*54fd6939SJiyong Parkaligned to 2 MiB then additional level 3 tables are also needed.
377*54fd6939SJiyong Park
378*54fd6939SJiyong ParkNote that not every translation level allows any type of descriptor. Depending
379*54fd6939SJiyong Parkon the page size, levels 0 and 1 of translation may only allow table
380*54fd6939SJiyong Parkdescriptors. If a block entry could be able to describe a translation, but that
381*54fd6939SJiyong Parklevel does not allow block descriptors, a table descriptor will have to be used
382*54fd6939SJiyong Parkinstead, as well as additional tables at the next level.
383*54fd6939SJiyong Park
384*54fd6939SJiyong Park|Alignment Example|
385*54fd6939SJiyong Park
386*54fd6939SJiyong ParkThe mmap regions are sorted in a way that simplifies the code that maps
387*54fd6939SJiyong Parkthem. Even though this ordering is only strictly needed for overlapping static
388*54fd6939SJiyong Parkregions, it must also be applied for dynamic regions to maintain a consistent
389*54fd6939SJiyong Parkorder of all regions at all times. As each new region is mapped, existing
390*54fd6939SJiyong Parkentries in the translation tables are checked to ensure consistency. Please
391*54fd6939SJiyong Parkrefer to the comments in the source code of the core module for more details
392*54fd6939SJiyong Parkabout the sorting algorithm in use.
393*54fd6939SJiyong Park
394*54fd6939SJiyong ParkThis mapping algorithm does not apply to the MPU library, since the MPU hardware
395*54fd6939SJiyong Parkdirectly maps regions by "base" and "limit" (bottom and top) addresses.
396*54fd6939SJiyong Park
397*54fd6939SJiyong ParkTLB maintenance operations
398*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~~~~~~~
399*54fd6939SJiyong Park
400*54fd6939SJiyong ParkThe library takes care of performing TLB maintenance operations when required.
401*54fd6939SJiyong ParkFor example, when the user requests removing a dynamic region, the library
402*54fd6939SJiyong Parkinvalidates all TLB entries associated to that region to ensure that these
403*54fd6939SJiyong Parkchanges are visible to subsequent execution, including speculative execution,
404*54fd6939SJiyong Parkthat uses the changed translation table entries.
405*54fd6939SJiyong Park
406*54fd6939SJiyong ParkA counter-example is the initialization of translation tables. In this case,
407*54fd6939SJiyong Parkexplicit TLB maintenance is not required. The Armv8-A architecture guarantees
408*54fd6939SJiyong Parkthat all TLBs are disabled from reset and their contents have no effect on
409*54fd6939SJiyong Parkaddress translation at reset [#tlb-reset-ref]_. Therefore, the TLBs invalidation
410*54fd6939SJiyong Parkis deferred to the ``enable_mmu*()`` family of functions, just before the MMU is
411*54fd6939SJiyong Parkturned on.
412*54fd6939SJiyong Park
413*54fd6939SJiyong ParkRegarding enabling and disabling memory management, for the MPU library, to
414*54fd6939SJiyong Parkreduce confusion, calls to enable or disable the MPU use ``mpu`` in their names
415*54fd6939SJiyong Parkin place of ``mmu``. For example, the ``enable_mmu_el2()`` call is changed to
416*54fd6939SJiyong Park``enable_mpu_el2()``.
417*54fd6939SJiyong Park
418*54fd6939SJiyong ParkTLB invalidation is not required when adding dynamic regions either. Dynamic
419*54fd6939SJiyong Parkregions are not allowed to overlap existing memory region. Therefore, if the
420*54fd6939SJiyong Parkdynamic mapping request is deemed legitimate, it automatically concerns memory
421*54fd6939SJiyong Parkthat was not mapped in this translation regime and the library will have
422*54fd6939SJiyong Parkinitialized its corresponding translation table entry to an invalid
423*54fd6939SJiyong Parkdescriptor. Given that the TLBs are not architecturally permitted to hold any
424*54fd6939SJiyong Parkinvalid translation table entry [#tlb-no-invalid-entry]_, this means that this
425*54fd6939SJiyong Parkmapping cannot be cached in the TLBs.
426*54fd6939SJiyong Park
427*54fd6939SJiyong Park.. rubric:: Footnotes
428*54fd6939SJiyong Park
429*54fd6939SJiyong Park.. [#granularity] That is, when mmap regions do not enforce their mapping
430*54fd6939SJiyong Park                  granularity.
431*54fd6939SJiyong Park
432*54fd6939SJiyong Park.. [#tlb-reset-ref] See section D4.9 ``Translation Lookaside Buffers (TLBs)``,
433*54fd6939SJiyong Park                    subsection ``TLB behavior at reset`` in Armv8-A, rev C.a.
434*54fd6939SJiyong Park
435*54fd6939SJiyong Park.. [#tlb-no-invalid-entry] See section D4.10.1 ``General TLB maintenance
436*54fd6939SJiyong Park                           requirements`` in Armv8-A, rev C.a.
437*54fd6939SJiyong Park
438*54fd6939SJiyong Park--------------
439*54fd6939SJiyong Park
440*54fd6939SJiyong Park*Copyright (c) 2017-2021, Arm Limited and Contributors. All rights reserved.*
441*54fd6939SJiyong Park
442*54fd6939SJiyong Park.. |Alignment Example| image:: ../resources/diagrams/xlat_align.png
443