blob: fec7c00c71a0199da80afa862c1b611141a74287 [file] [log] [blame]
Antonio Nino Diaz352c8522017-12-15 11:41:17 +00001*******************************
2Secure Partition Manager Design
3*******************************
4
5.. section-numbering::
6 :suffix: .
7
8.. contents::
9
10Background
11==========
12
13In some market segments that primarily deal with client-side devices like mobile
14phones, tablets, STBs and embedded devices, a Trusted OS instantiates trusted
15applications to provide security services like DRM, secure payment and
16authentication. The Global Platform TEE Client API specification defines the API
17used by Non-secure world applications to access these services. A Trusted OS
18fulfils the requirements of a security service as described above.
19
20Management services are typically implemented at the highest level of privilege
Dan Handley610e7e12018-03-01 18:44:00 +000021in the system, i.e. EL3 in Trusted Firmware-A (TF-A). The service requirements are
22fulfilled by the execution environment provided by TF-A.
Antonio Nino Diaz352c8522017-12-15 11:41:17 +000023
24The following diagram illustrates the corresponding software stack:
25
26|Image 1|
27
28In other market segments that primarily deal with server-side devices (e.g. data
29centres and enterprise servers) the secure software stack typically does not
30include a Global Platform Trusted OS. Security functions are accessed through
31other interfaces (e.g. ACPI TCG TPM interface, UEFI runtime variable service).
32
33Placement of management and security functions with diverse requirements in a
34privileged Exception Level (i.e. EL3 or S-EL1) makes security auditing of
35firmware more difficult and does not allow isolation of unrelated services from
36each other either.
37
38Introduction
39============
40
41A **Secure Partition** is a software execution environment instantiated in
42S-EL0 that can be used to implement simple management and security services.
43Since S-EL0 is an unprivileged Exception Level, a Secure Partition relies on
Dan Handley610e7e12018-03-01 18:44:00 +000044privileged firmware (i.e. TF-A) to be granted access to system and processor
45resources. Essentially, it is a software sandbox in the Secure world that runs
46under the control of privileged software, provides one or more services and
47accesses the following system resources:
Antonio Nino Diaz352c8522017-12-15 11:41:17 +000048
49- Memory and device regions in the system address map.
50
51- PE system registers.
52
53- A range of synchronous exceptions (e.g. SMC function identifiers).
54
Dan Handley610e7e12018-03-01 18:44:00 +000055Note that currently TF-A only supports handling one Secure Partition.
Antonio Nino Diaz352c8522017-12-15 11:41:17 +000056
Dan Handley610e7e12018-03-01 18:44:00 +000057A Secure Partition enables TF-A to implement only the essential secure
58services in EL3 and instantiate the rest in a partition in S-EL0.
Antonio Nino Diaz352c8522017-12-15 11:41:17 +000059Furthermore, multiple Secure Partitions can be used to isolate unrelated
60services from each other.
61
62The following diagram illustrates the place of a Secure Partition in a typical
Dan Handley610e7e12018-03-01 18:44:00 +000063Armv8-A software stack. A single or multiple Secure Partitions provide secure
Antonio Nino Diaz352c8522017-12-15 11:41:17 +000064services to software components in the Non-secure world and other Secure
65Partitions.
66
67|Image 2|
68
Dan Handley610e7e12018-03-01 18:44:00 +000069The TF-A build system is responsible for including the Secure Partition image
70in the FIP. During boot, BL2 includes support to authenticate and load the
71Secure Partition image. A BL31 component called **Secure Partition Manager
72(SPM)** is responsible for managing the partition. This is semantically
Antonio Nino Diaz352c8522017-12-15 11:41:17 +000073similar to a hypervisor managing a virtual machine.
74
75The SPM is responsible for the following actions during boot:
76
77- Allocate resources requested by the Secure Partition.
78
79- Perform architectural and system setup required by the Secure Partition to
80 fulfil a service request.
81
82- Implement a standard interface that is used for initialising a Secure
83 Partition.
84
85The SPM is responsible for the following actions during runtime:
86
87- Implement a standard interface that is used by a Secure Partition to fulfil
88 service requests.
89
90- Implement a standard interface that is used by the Non-secure world for
91 accessing the services exported by a Secure Partition. A service can be
92 invoked through a SMC.
93
94Alternatively, a partition can be viewed as a thread of execution running under
95the control of the SPM. Hence common programming concepts described below are
96applicable to a partition.
97
98Description
99===========
100
101The previous section introduced some general aspects of the software
102architecture of a Secure Partition. This section describes the specific choices
103made in the current implementation of this software architecture. Subsequent
104revisions of the implementation will include a richer set of features that
105enable a more flexible architecture.
106
Dan Handley610e7e12018-03-01 18:44:00 +0000107Building TF-A with Secure Partition support
108-------------------------------------------
Antonio Nino Diaz352c8522017-12-15 11:41:17 +0000109
110SPM is supported on the Arm FVP exclusively at the moment. The current
111implementation supports inclusion of only a single Secure Partition in which a
112service always runs to completion (e.g. the requested services cannot be
113preempted to give control back to the Normal world).
114
115It is not currently possible for BL31 to integrate SPM support and a Secure
116Payload Dispatcher (SPD) at the same time; they are mutually exclusive. In the
117SPM bootflow, a Secure Partition image executing at S-EL0 replaces the Secure
118Payload image executing at S-EL1 (e.g. a Trusted OS). Both are referred to as
119BL32.
120
121A working prototype of a SP has been implemented by re-purposing the EDK2 code
122and tools, leveraging the concept of the *Standalone Management Mode (MM)* in
123the UEFI specification (see the PI v1.6 Volume 4: Management Mode Core
124Interface). This will be referred to as the *Standalone MM Secure Partition* in
125the rest of this document.
126
Dan Handley610e7e12018-03-01 18:44:00 +0000127To enable SPM support in TF-A, the source code must be compiled with the build
Antonio Nino Diaz352c8522017-12-15 11:41:17 +0000128flag ``ENABLE_SPM=1``. On Arm platforms the build option ``ARM_BL31_IN_DRAM``
Antonio Nino Diaz31c397f2018-05-24 09:14:58 +0100129must be set to 1. Also, the location of the binary that contains the BL32 image
Antonio Nino Diaz352c8522017-12-15 11:41:17 +0000130(``BL32=path/to/image.bin``) must be specified.
131
132First, build the Standalone MM Secure Partition. To build it, refer to the
133`instructions in the EDK2 repository`_.
134
Dan Handley610e7e12018-03-01 18:44:00 +0000135Then build TF-A with SPM support and include the Standalone MM Secure Partition
Antonio Nino Diaz352c8522017-12-15 11:41:17 +0000136image in the FIP:
137
138::
139
140 BL32=path/to/standalone/mm/sp BL33=path/to/bl33.bin \
Antonio Nino Diaz31c397f2018-05-24 09:14:58 +0100141 make PLAT=fvp ENABLE_SPM=1 ARM_BL31_IN_DRAM=1 fip all
Antonio Nino Diaz352c8522017-12-15 11:41:17 +0000142
143Describing Secure Partition resources
144-------------------------------------
145
Dan Handley610e7e12018-03-01 18:44:00 +0000146TF-A exports a porting interface that enables a platform to specify the system
147resources required by the Secure Partition. Some instructions are given below.
148However, this interface is under development and it may change as new features
149are implemented.
Antonio Nino Diaz352c8522017-12-15 11:41:17 +0000150
151- A Secure Partition is considered a BL32 image, so the same defines that apply
152 to BL32 images apply to a Secure Partition: ``BL32_BASE`` and ``BL32_LIMIT``.
153
154- The following defines are needed to allocate space for the translation tables
155 used by the Secure Partition: ``PLAT_SP_IMAGE_MMAP_REGIONS`` and
156 ``PLAT_SP_IMAGE_MAX_XLAT_TABLES``.
157
158- The functions ``plat_get_secure_partition_mmap()`` and
159 ``plat_get_secure_partition_boot_info()`` have to be implemented. The file
160 ``plat/arm/board/fvp/fvp_common.c`` can be used as an example. It uses the
161 defines in ``include/plat/arm/common/arm_spm_def.h``.
162
163 - ``plat_get_secure_partition_mmap()`` returns an array of mmap regions that
164 describe the memory regions that the SPM needs to allocate for a Secure
165 Partition.
166
167 - ``plat_get_secure_partition_boot_info()`` returns a
168 ``secure_partition_boot_info_t`` struct that is populated by the platform
169 with information about the memory map of the Secure Partition.
170
171For an example of all the changes in context, you may refer to commit
172``e29efeb1b4``, in which the port for FVP was introduced.
173
174Accessing Secure Partition services
175-----------------------------------
176
Dan Handley610e7e12018-03-01 18:44:00 +0000177The `SMC Calling Convention`_ (*Arm DEN 0028B*) describes SMCs as a conduit for
Antonio Nino Diaz352c8522017-12-15 11:41:17 +0000178accessing services implemented in the Secure world. The ``MM_COMMUNICATE``
Dan Handley610e7e12018-03-01 18:44:00 +0000179interface defined in the `Management Mode Interface Specification`_ (*Arm DEN
Antonio Nino Diaz352c8522017-12-15 11:41:17 +00001800060A*) is used to invoke a Secure Partition service as a Fast Call.
181
182The mechanism used to identify a service within the partition depends on the
183service implementation. It is assumed that the caller of the service will be
184able to discover this mechanism through standard platform discovery mechanisms
185like ACPI and Device Trees. For example, *Volume 4: Platform Initialisation
186Specification v1.6. Management Mode Core Interface* specifies that a GUID is
187used to identify a management mode service. A client populates the GUID in the
188``EFI_MM_COMMUNICATE_HEADER``. The header is populated in the communication
189buffer shared with the Secure Partition.
190
191A Fast Call appears to be atomic from the perspective of the caller and returns
192when the requested operation has completed. A service invoked through the
193``MM_COMMUNICATE`` SMC will run to completion in the partition on a given CPU.
194The SPM is responsible for guaranteeing this behaviour. This means that there
195can only be a single outstanding Fast Call in a partition on a given CPU.
196
197Exchanging data with the Secure Partition
198-----------------------------------------
199
200The exchange of data between the Non-secure world and the partition takes place
201through a shared memory region. The location of data in the shared memory area
202is passed as a parameter to the ``MM_COMMUNICATE`` SMC. The shared memory area
203is statically allocated by the SPM and is expected to be either implicitly known
204to the Non-secure world or discovered through a platform discovery mechanism
205e.g. ACPI table or device tree. It is possible for the Non-secure world to
206exchange data with a partition only if it has been populated in this shared
207memory area. The shared memory area is implemented as per the guidelines
208specified in Section 3.2.3 of the `Management Mode Interface Specification`_
Dan Handley610e7e12018-03-01 18:44:00 +0000209(*Arm DEN 0060A*).
Antonio Nino Diaz352c8522017-12-15 11:41:17 +0000210
211The format of data structures used to encapsulate data in the shared memory is
212agreed between the Non-secure world and the Secure Partition. For example, in
Dan Handley610e7e12018-03-01 18:44:00 +0000213the `Management Mode Interface specification`_ (*Arm DEN 0060A*), Section 4
Antonio Nino Diaz352c8522017-12-15 11:41:17 +0000214describes that the communication buffer shared between the Non-secure world and
215the Management Mode (MM) in the Secure world must be of the type
216``EFI_MM_COMMUNICATE_HEADER``. This data structure is defined in *Volume 4:
217Platform Initialisation Specification v1.6. Management Mode Core Interface*.
218Any caller of a MM service will have to use the ``EFI_MM_COMMUNICATE_HEADER``
219data structure.
220
221Runtime model of the Secure Partition
222=====================================
223
224This section describes how the Secure Partition interfaces with the SPM.
225
226Interface with SPM
227------------------
228
229In order to instantiate one or more secure services in the Secure Partition in
230S-EL0, the SPM should define the following types of interfaces:
231
232- Interfaces that enable access to privileged operations from S-EL0. These
233 operations typically require access to system resources that are either shared
234 amongst multiple software components in the Secure world or cannot be directly
235 accessed from an unprivileged Exception Level.
236
237- Interfaces that establish the control path between the SPM and the Secure
238 Partition.
239
240This section describes the APIs currently exported by the SPM that enable a
241Secure Partition to initialise itself and export its services in S-EL0. These
242interfaces are not accessible from the Non-secure world.
243
244Conduit
245^^^^^^^
246
Dan Handley610e7e12018-03-01 18:44:00 +0000247The `SMC Calling Convention`_ (*Arm DEN 0028B*) specification describes the SMC
Antonio Nino Diaz352c8522017-12-15 11:41:17 +0000248and HVC conduits for accessing firmware services and their availability
249depending on the implemented Exception levels. In S-EL0, the Supervisor Call
250exception (SVC) is the only architectural mechanism available for unprivileged
251software to make a request for an operation implemented in privileged software.
252Hence, the SVC conduit must be used by the Secure Partition to access interfaces
253implemented by the SPM.
254
Dan Handley610e7e12018-03-01 18:44:00 +0000255A SVC causes an exception to be taken to S-EL1. TF-A assumes ownership of S-EL1
256and installs a simple exception vector table in S-EL1 that relays a SVC request
257from a Secure Partition as a SMC request to the SPM in EL3. Upon servicing the
258SMC request, Arm Trusted Firmware returns control directly to S-EL0 through an
259ERET instruction.
Antonio Nino Diaz352c8522017-12-15 11:41:17 +0000260
261Calling conventions
262^^^^^^^^^^^^^^^^^^^
263
Dan Handley610e7e12018-03-01 18:44:00 +0000264The `SMC Calling Convention`_ (*Arm DEN 0028B*) specification describes the
Antonio Nino Diaz352c8522017-12-15 11:41:17 +000026532-bit and 64-bit calling conventions for the SMC and HVC conduits. The SVC
266conduit introduces the concept of SVC32 and SVC64 calling conventions. The SVC32
267and SVC64 calling conventions are equivalent to the 32-bit (SMC32) and the
26864-bit (SMC64) calling conventions respectively.
269
270Communication initiated by SPM
271^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
272
273A service request is initiated from the SPM through an exception return
274instruction (ERET) to S-EL0. Later, the Secure Partition issues an SVC
275instruction to signal completion of the request. Some example use cases are
276given below:
277
278- A request to initialise the Secure Partition during system boot.
279
280- A request to handle a runtime service request.
281
282Communication initiated by Secure Partition
283^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
284
285A request is initiated from the Secure Partition by executing a SVC instruction.
Dan Handley610e7e12018-03-01 18:44:00 +0000286An ERET instruction is used by TF-A to return to S-EL0 with the result of the
287request.
Antonio Nino Diaz352c8522017-12-15 11:41:17 +0000288
289For instance, a request to perform privileged operations on behalf of a
290partition (e.g. management of memory attributes in the translation tables for
291the Secure EL1&0 translation regime).
292
293Interfaces
294^^^^^^^^^^
295
296The current implementation reserves function IDs for Fast Calls in the Standard
Dan Handley610e7e12018-03-01 18:44:00 +0000297Secure Service calls range (see `SMC Calling Convention`_ (*Arm DEN 0028B*)
Antonio Nino Diaz352c8522017-12-15 11:41:17 +0000298specification) for each API exported by the SPM. This section defines the
299function prototypes for each function ID. The function IDs specify whether one
300or both of the SVC32 and SVC64 calling conventions can be used to invoke the
301corresponding interface.
302
303Secure Partition Event Management
304^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
305
306The Secure Partition provides an Event Management interface that is used by the
307SPM to delegate service requests to the Secure Partition. The interface also
308allows the Secure Partition to:
309
310- Register with the SPM a service that it provides.
311- Indicate completion of a service request delagated by the SPM
312
313Miscellaneous interfaces
314------------------------
315
316``SPM_VERSION_AARCH32``
317^^^^^^^^^^^^^^^^^^^^^^^
318
319- Description
320
321 Returns the version of the interface exported by SPM.
322
323- Parameters
324
325 - **uint32** - Function ID
326
327 - SVC32 Version: **0x84000060**
328
329- Return parameters
330
331 - **int32** - Status
332
333 On success, the format of the value is as follows:
334
335 - Bit [31]: Must be 0
336 - Bits [30:16]: Major Version. Must be 0 for this revision of the SPM
337 interface.
338 - Bits [15:0]: Minor Version. Must be 1 for this revision of the SPM
339 interface.
340
341 On error, the format of the value is as follows:
342
343 - ``NOT_SUPPORTED``: SPM interface is not supported or not available for the
344 client.
345
346- Usage
347
348 This function returns the version of the Secure Partition Manager
349 implementation. The major version is 0 and the minor version is 1. The version
350 number is a 31-bit unsigned integer, with the upper 15 bits denoting the major
351 revision, and the lower 16 bits denoting the minor revision. The following
352 rules apply to the version numbering:
353
354 - Different major revision values indicate possibly incompatible functions.
355
356 - For two revisions, A and B, for which the major revision values are
357 identical, if the minor revision value of revision B is greater than the
358 minor revision value of revision A, then every function in revision A must
359 work in a compatible way with revision B. However, it is possible for
360 revision B to have a higher function count than revision A.
361
362- Implementation responsibilities
363
364 If this function returns a valid version number, all the functions that are
365 described subsequently must be implemented, unless it is explicitly stated
366 that a function is optional.
367
368See `Error Codes`_ for integer values that are associated with each return
369code.
370
371Secure Partition Initialisation
372-------------------------------
373
374The SPM is responsible for initialising the architectural execution context to
375enable initialisation of a service in S-EL0. The responsibilities of the SPM are
376listed below. At the end of initialisation, the partition issues a
377``SP_EVENT_COMPLETE_AARCH64`` call (described later) to signal readiness for
378handling requests for services implemented by the Secure Partition. The
379initialisation event is executed as a Fast Call.
380
381Entry point invocation
382^^^^^^^^^^^^^^^^^^^^^^
383
384The entry point for service requests that should be handled as Fast Calls is
385used as the target of the ERET instruction to start initialisation of the Secure
386Partition.
387
388Architectural Setup
389^^^^^^^^^^^^^^^^^^^
390
391At cold boot, system registers accessible from S-EL0 will be in their reset
392state unless otherwise specified. The SPM will perform the following
393architectural setup to enable execution in S-EL0
394
395MMU setup
396^^^^^^^^^
397
398The platform port of a Secure Partition specifies to the SPM a list of regions
399that it needs access to and their attributes. The SPM validates this resource
400description and initialises the Secure EL1&0 translation regime as follows.
401
4021. Device regions are mapped with nGnRE attributes and Execute Never
403 instruction access permissions.
404
4052. Code memory regions are mapped with RO data and Executable instruction access
406 permissions.
407
4083. Read Only data memory regions are mapped with RO data and Execute Never
409 instruction access permissions.
410
4114. Read Write data memory regions are mapped with RW data and Execute Never
412 instruction access permissions.
413
4145. If the resource description does not explicitly describe the type of memory
415 regions then all memory regions will be marked with Code memory region
416 attributes.
417
4186. The ``UXN`` and ``PXN`` bits are set for regions that are not executable by
419 S-EL0 or S-EL1.
420
421System Register Setup
422^^^^^^^^^^^^^^^^^^^^^
423
424System registers that influence software execution in S-EL0 are setup by the SPM
425as follows:
426
4271. ``SCTLR_EL1``
428
429 - ``UCI=1``
430 - ``EOE=0``
431 - ``WXN=1``
432 - ``nTWE=1``
433 - ``nTWI=1``
434 - ``UCT=1``
435 - ``DZE=1``
436 - ``I=1``
437 - ``UMA=0``
438 - ``SA0=1``
439 - ``C=1``
440 - ``A=1``
441 - ``M=1``
442
4432. ``CPACR_EL1``
444
445 - ``FPEN=b'11``
446
4473. ``PSTATE``
448
449 - ``D,A,I,F=1``
450 - ``CurrentEL=0`` (EL0)
451 - ``SpSel=0`` (Thread mode)
452 - ``NRW=0`` (AArch64)
453
454General Purpose Register Setup
455^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
456
457SPM will invoke the entry point of a service by executing an ERET instruction.
458This transition into S-EL0 is special since it is not in response to a previous
459request through a SVC instruction. This is the first entry into S-EL0. The
460general purpose register usage at the time of entry will be as specified in the
461"Return State" column of Table 3-1 in Section 3.1 "Register use in AArch64 SMC
Dan Handley610e7e12018-03-01 18:44:00 +0000462calls" of the `SMC Calling Convention`_ (*Arm DEN 0028B*) specification. In
Antonio Nino Diaz352c8522017-12-15 11:41:17 +0000463addition, certain other restrictions will be applied as described below.
464
4651. ``SP_EL0``
466
467 A non-zero value will indicate that the SPM has initialised the stack pointer
468 for the current CPU.
469
470 The value will be 0 otherwise.
471
4722. ``X4-X30``
473
474 The values of these registers will be 0.
475
4763. ``X0-X3``
477
478 Parameters passed by the SPM.
479
480 - ``X0``: Virtual address of a buffer shared between EL3 and S-EL0. The
481 buffer will be mapped in the Secure EL1&0 translation regime with read-only
482 memory attributes described earlier.
483
484 - ``X1``: Size of the buffer in bytes.
485
486 - ``X2``: Cookie value (*IMPLEMENTATION DEFINED*).
487
488 - ``X3``: Cookie value (*IMPLEMENTATION DEFINED*).
489
490Runtime Event Delegation
491------------------------
492
493The SPM receives requests for Secure Partition services through a synchronous
494invocation (i.e. a SMC from the Non-secure world). These requests are delegated
495to the partition by programming a return from the last
496``SP_EVENT_COMPLETE_AARCH64`` call received from the partition. The last call
497was made to signal either completion of Secure Partition initialisation or
498completion of a partition service request.
499
500``SP_EVENT_COMPLETE_AARCH64``
501^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
502
503- Description
504
505 Signal completion of the last SP service request.
506
507- Parameters
508
509 - **uint32** - Function ID
510
511 - SVC64 Version: **0xC4000061**
512
513 - **int32** - Event Status Code
514
515 Zero or a positive value indicates that the event was handled successfully.
516 The values depend upon the original event that was delegated to the Secure
517 partition. They are described as follows.
518
519 - ``SUCCESS`` : Used to indicate that the Secure Partition was initialised
520 or a runtime request was handled successfully.
521
522 - Any other value greater than 0 is used to pass a specific Event Status
523 code in response to a runtime event.
524
525 A negative value indicates an error. The values of Event Status code depend
526 on the original event.
527
528- Return parameters
529
530 - **int32** - Event ID/Return Code
531
532 Zero or a positive value specifies the unique ID of the event being
533 delegated to the partition by the SPM.
534
535 In the current implementation, this parameter contains the function ID of
536 the ``MM_COMMUNICATE`` SMC. This value indicates to the partition that an
537 event has been delegated to it in response to an ``MM_COMMUNICATE`` request
538 from the Non-secure world.
539
540 A negative value indicates an error. The format of the value is as follows:
541
542 - ``NOT_SUPPORTED``: Function was called from the Non-secure world.
543
544 See `Error Codes`_ for integer values that are associated with each return
545 code.
546
547 - **uint32** - Event Context Address
548
549 Address of a buffer shared between the SPM and Secure Partition to pass
550 event specific information. The format of the data populated in the buffer
551 is implementation defined.
552
553 The buffer is mapped in the Secure EL1&0 translation regime with read-only
554 memory attributes described earlier.
555
556 For the SVC64 version, this parameter is a 64-bit Virtual Address (VA).
557
558 For the SVC32 version, this parameter is a 32-bit Virtual Address (VA).
559
560 - **uint32** - Event context size
561
562 Size of the memory starting at Event Address.
563
564 - **uint32/uint64** - Event Cookie
565
566 This is an optional parameter. If unused its value is SBZ.
567
568- Usage
569
570 This function signals to the SPM that the handling of the last event delegated
571 to a partition has completed. The partition is ready to handle its next event.
572 A return from this function is in response to the next event that will be
573 delegated to the partition. The return parameters describe the next event.
574
575- Caller responsibilities
576
577 A Secure Partition must only call ``SP_EVENT_COMPLETE_AARCH64`` to signal
578 completion of a request that was delegated to it by the SPM.
579
580- Callee responsibilities
581
582 When the SPM receives this call from a Secure Partition, the corresponding
583 syndrome information can be used to return control through an ERET
584 instruction, to the instruction immediately after the call in the Secure
585 Partition context. This syndrome information comprises of general purpose and
586 system register values when the call was made.
587
588 The SPM must save this syndrome information and use it to delegate the next
589 event to the Secure Partition. The return parameters of this interface must
590 specify the properties of the event and be populated in ``X0-X3/W0-W3``
591 registers.
592
593Secure Partition Memory Management
594----------------------------------
595
596A Secure Partition executes at S-EL0, which is an unprivileged Exception Level.
597The SPM is responsible for enabling access to regions of memory in the system
598address map from a Secure Partition. This is done by mapping these regions in
599the Secure EL1&0 Translation regime with appropriate memory attributes.
600Attributes refer to memory type, permission, cacheability and shareability
601attributes used in the Translation tables. The definitions of these attributes
Dan Handley610e7e12018-03-01 18:44:00 +0000602and their usage can be found in the `Armv8-A ARM`_ (*Arm DDI 0487*).
Antonio Nino Diaz352c8522017-12-15 11:41:17 +0000603
604All memory required by the Secure Partition is allocated upfront in the SPM,
605even before handing over to the Secure Partition for the first time. The initial
606access permissions of the memory regions are statically provided by the platform
607port and should allow the Secure Partition to run its initialisation code.
608
609However, they might not suit the final needs of the Secure Partition because its
610final memory layout might not be known until the Secure Partition initialises
611itself. As the Secure Partition initialises its runtime environment it might,
612for example, load dynamically some modules. For instance, a Secure Partition
613could implement a loader for a standard executable file format (e.g. an PE-COFF
614loader for loading executable files at runtime). These executable files will be
615a part of the Secure Partition image. The location of various sections in an
616executable file and their permission attributes (e.g. read-write data, read-only
617data and code) will be known only when the file is loaded into memory.
618
619In this case, the Secure Partition needs a way to change the access permissions
620of its memory regions. The SPM provides this feature through the
621``SP_MEMORY_ATTRIBUTES_SET_AARCH64`` SVC interface. This interface is available
622to the Secure Partition during a specific time window: from the first entry into
623the Secure Partition up to the first ``SP_EVENT_COMPLETE`` call that signals the
624Secure Partition has finished its initialisation. Once the initialisation is
625complete, the SPM does not allow changes to the memory attributes.
626
627This section describes the standard SVC interface that is implemented by the SPM
628to determine and change permission attributes of memory regions that belong to a
629Secure Partition.
630
631``SP_MEMORY_ATTRIBUTES_GET_AARCH64``
632^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
633
634- Description
635
636 Request the permission attributes of a memory region from S-EL0.
637
638- Parameters
639
640 - **uint32** Function ID
641
642 - SVC64 Version: **0xC4000064**
643
644 - **uint64** Base Address
645
646 This parameter is a 64-bit Virtual Address (VA).
647
648 There are no alignment restrictions on the Base Address. The permission
649 attributes of the translation granule it lies in are returned.
650
651- Return parameters
652
653 - **int32** - Memory Attributes/Return Code
654
655 On success the format of the Return Code is as follows:
656
657 - Bits[1:0] : Data access permission
658
659 - b'00 : No access
660 - b'01 : Read-Write access
661 - b'10 : Reserved
662 - b'11 : Read-only access
663
664 - Bit[2]: Instruction access permission
665
666 - b'0 : Executable
667 - b'1 : Non-executable
668
669 - Bit[30:3] : Reserved. SBZ.
670
671 - Bit[31] : Must be 0
672
673 On failure the following error codes are returned:
674
675 - ``INVALID_PARAMETERS``: The Secure Partition is not allowed to access the
676 memory region the Base Address lies in.
677
678 - ``NOT_SUPPORTED`` : The SPM does not support retrieval of attributes of
679 any memory page that is accessible by the Secure Partition, or the
680 function was called from the Non-secure world. Also returned if it is
681 used after ``SP_EVENT_COMPLETE_AARCH64``.
682
683 See `Error Codes`_ for integer values that are associated with each return
684 code.
685
686- Usage
687
688 This function is used to request the permission attributes for S-EL0 on a
689 memory region accessible from a Secure Partition. The size of the memory
690 region is equal to the Translation Granule size used in the Secure EL1&0
691 translation regime. Requests to retrieve other memory region attributes are
692 not currently supported.
693
694- Caller responsibilities
695
696 The caller must obtain the Translation Granule Size of the Secure EL1&0
697 translation regime from the SPM through an implementation defined method.
698
699- Callee responsibilities
700
701 The SPM must not return the memory access controls for a page of memory that
702 is not accessible from a Secure Partition.
703
704``SP_MEMORY_ATTRIBUTES_SET_AARCH64``
705^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
706
707- Description
708
709 Set the permission attributes of a memory region from S-EL0.
710
711- Parameters
712
713 - **uint32** - Function ID
714
715 - SVC64 Version: **0xC4000065**
716
717 - **uint64** - Base Address
718
719 This parameter is a 64-bit Virtual Address (VA).
720
721 The alignment of the Base Address must be greater than or equal to the size
722 of the Translation Granule Size used in the Secure EL1&0 translation
723 regime.
724
725 - **uint32** - Page count
726
727 Number of pages starting from the Base Address whose memory attributes
728 should be changed. The page size is equal to the Translation Granule Size.
729
730 - **uint32** - Memory Access Controls
731
732 - Bits[1:0] : Data access permission
733
734 - b'00 : No access
735 - b'01 : Read-Write access
736 - b'10 : Reserved
737 - b'11 : Read-only access
738
739 - Bit[2] : Instruction access permission
740
741 - b'0 : Executable
742 - b'1 : Non-executable
743
744 - Bits[31:3] : Reserved. SBZ.
745
746 A combination of attributes that mark the region with RW and Executable
747 permissions is prohibited. A request to mark a device memory region with
748 Executable permissions is prohibited.
749
750- Return parameters
751
752 - **int32** - Return Code
753
754 - ``SUCCESS``: The Memory Access Controls were changed successfully.
755
756 - ``DENIED``: The SPM is servicing a request to change the attributes of a
757 memory region that overlaps with the region specified in this request.
758
759 - ``INVALID_PARAMETER``: An invalid combination of Memory Access Controls
760 has been specified. The Base Address is not correctly aligned. The Secure
761 Partition is not allowed to access part or all of the memory region
762 specified in the call.
763
764 - ``NO_MEMORY``: The SPM does not have memory resources to change the
765 attributes of the memory region in the translation tables.
766
767 - ``NOT_SUPPORTED``: The SPM does not permit change of attributes of any
768 memory region that is accessible by the Secure Partition. Function was
769 called from the Non-secure world. Also returned if it is used after
770 ``SP_EVENT_COMPLETE_AARCH64``.
771
772 See `Error Codes`_ for integer values that are associated with each return
773 code.
774
775- Usage
776
777 This function is used to change the permission attributes for S-EL0 on a
778 memory region accessible from a Secure Partition. The size of the memory
779 region is equal to the Translation Granule size used in the Secure EL1&0
780 translation regime. Requests to change other memory region attributes are not
781 currently supported.
782
783 This function is only available at boot time. This interface is revoked after
784 the Secure Partition sends the first ``SP_EVENT_COMPLETE_AARCH64`` to signal
785 that it is initialised and ready to receive run-time requests.
786
787- Caller responsibilities
788
789 The caller must obtain the Translation Granule Size of the Secure EL1&0
790 translation regime from the SPM through an implementation defined method.
791
792- Callee responsibilities
793
794 The SPM must preserve the original memory access controls of the region of
795 memory in case of an unsuccessful call.  The SPM must preserve the consistency
796 of the S-EL1 translation regime if this function is called on different PEs
797 concurrently and the memory regions specified overlap.
798
799Error Codes
800-----------
801
802.. csv-table::
803 :header: "Name", "Value"
804
805 ``SUCCESS``,0
806 ``NOT_SUPPORTED``,-1
807 ``INVALID_PARAMETER``,-2
808 ``DENIED``,-3
809 ``NO_MEMORY``,-5
810 ``NOT_PRESENT``,-7
811
812--------------
813
Dan Handley610e7e12018-03-01 18:44:00 +0000814*Copyright (c) 2017-2018, Arm Limited and Contributors. All rights reserved.*
Antonio Nino Diaz352c8522017-12-15 11:41:17 +0000815
Dan Handley610e7e12018-03-01 18:44:00 +0000816.. _Armv8-A ARM: https://developer.arm.com/docs/ddi0487/latest/arm-architecture-reference-manual-armv8-for-armv8-a-architecture-profile
Antonio Nino Diaz352c8522017-12-15 11:41:17 +0000817.. _instructions in the EDK2 repository: https://github.com/tianocore/edk2-staging/blob/AArch64StandaloneMm/HowtoBuild.MD
818.. _Management Mode Interface Specification: http://infocenter.arm.com/help/topic/com.arm.doc.den0060a/DEN0060A_ARM_MM_Interface_Specification.pdf
819.. _SDEI Specification: http://infocenter.arm.com/help/topic/com.arm.doc.den0054a/ARM_DEN0054A_Software_Delegated_Exception_Interface.pdf
820.. _SMC Calling Convention: http://infocenter.arm.com/help/topic/com.arm.doc.den0028b/ARM_DEN0028B_SMC_Calling_Convention.pdf
821
822.. |Image 1| image:: diagrams/secure_sw_stack_tos.png
823.. |Image 2| image:: diagrams/secure_sw_stack_sp.png