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