blob: 324ea641cfc0142b5e43622c4b0d3e9530225b64 [file] [log] [blame]
Jelle Sels3ef999a2021-03-29 09:35:37 +02001====
2SPMC
3====
4
5This document describes the SPMC (S-EL1) implementation for OP-TEE.
6More information on the SPMC can be found in the FF-A specification can be
7found in the
8`FF-A spec <https://developer.arm.com/documentation/den0077/latest>`_.
9
10.. toctree::
11 :numbered:
12
13SPMC Responsibilities
14=====================
15
16The SPMC is a critical component in the FF-A flow. Some of its major
17responsibilities are:
18
19- Initialisation and run-time management of the SPs:
20 The SPMC component is responsible for initialisation of the
21 Secure Partitions (loading the image, setting up the stack, heap, ...).
22- Routing messages between endpoints:
23 The SPMC is responsible for passing FF-A messages from normal world
24 to SPs and back. It also responsible for passing FF-A messages between
25 SPs.
26- Memory management:
27 The SPMC is responsible for the memory management of the SPs. Memory
28 can be shared between SPs and between a SP to the normal world.
29
30This document describes OP-TEE as a S-EL1 SPMC.
31
32Secure Partitions
33=================
34Secure Partitions (SPs) are the endpoints used in the FF-A protocol. When
35OP-TEE is used as a SPMC SPs run primarily inside S-EL0.
36
37OP-TEE will use FF-A for it transport layer when the OP-TEE ``CFG_CORE_FFA=y``
38configuration flag is enabled.
39The SPMC will expose the OP-TEE core, privileged mode, as an secure endpoint
40itself. This is used to handle all GlobalPlaform programming mode operations.
41All GlobalPlatform messages are encapsulated inside FF-A messages.
42The OP-TEE endpoint will unpack the messages and afterwards handle them as
43standard OP-TEE calls. This is needed as TF-A (S-EL3) does only allow
44FF-A messages to be passed to the secure world when the SPMD is enabled.
45
46SPs run from the initial boot of the system until power down and don't have any
47built-in session management compared to GPD TEE TAs. The only means of
48communicating with the outside world is through messages defined in the FF-A
49specification. The context of a SP is saved between executions.
50
51The
52`Trusted Service <https://www.trustedfirmware.org/projects/trusted-services/>`_
53repository includes the libsp libary which export all needed functions to build
54a S-EL0 SP. It also includes many examples of how to create and implement a SP.
55
Imre Kisc2351582022-11-22 17:16:38 +010056Secure Partition formats
57========================
58
59OP-TEE specific ELF format
60--------------------------
61
62OP-TEE uses an ELF format for its :ref:`trusted_applications`. It has an OP-TEE
63specific section which contains a header structure for describing the Trusted
64Application. A very similar format can be used for Secure Partitions. The same
65ELF format allows OP-TEE to use the built-in ELF loader (``ldelf``) with all its
66features like handling relocations or ASLR. In this case a different section is
67used for the header structure to distinguish between Trusted Applications and
68Secure Partitions.
69
70SPMC agnostic flat binary format
71--------------------------------
72
73This simple binary format aims for maximum portability between SPMC
74implementations by removing the dependency on an ELF loader and implementation
75specific metadata in the SP image. The SPMC can simply copy the binary into the
76memory and start running it. The relocations, the stack setup and any further
77initialization steps should be handled by the startup code of the secure
78partition. The access rights for different sections of the binary can be
79configured either by adding load relative memory regions to the SP manifest or
80by using the ``FFA_MEM_PERM_SET`` interface in the startup code.
81
Jelle Sels3ef999a2021-03-29 09:35:37 +020082SPMC Program Flow
83=================
Balint Dobszayfe29e2b2022-09-27 15:41:30 +020084SP images are either embedded into the OP-TEE image or loaded from the FIP by
85BL2. This makes it possible to start SPs during boot, before the rich OS is
Imre Kisc2351582022-11-22 17:16:38 +010086available in the normal world.
Jelle Sels3ef999a2021-03-29 09:35:37 +020087
88Starting SPs
89------------
90SPs are loaded and started as the last step in OP-TEE's initialisation process.
91This is done by adding ``sp_init_all()`` to the ``boot_final`` initcall level.
92
93.. uml::
94 :width: 800
95
96 autoactivate on
97 thread_optee_smc_a64.s -> boot_final
98 boot_final -> secure_partition.c: sp_init_all()
99 loop for each SP
100 secure_partition.c -> secure_partition.c: sp_init_uuid()
101 return
102 end
103 return
104 return
105 autoactivate off
106 thread_optee_smc_a64.s -> thread_optee_smc_a64.s: thread_ffa_msg_wait()
107 thread_optee_smc_a64.s -> thread_optee_smc_a64.s: ffa_msg_loop()
108 autoactivate off
109 thread_optee_smc_a64.s -> SPMD: SMC
110
111:``sp_init_all()``: Initialise all SPs which have been added by the
112 ``SP_PATHS`` compiler option and run them
113:``thread_ffa_msg_wait()``: All SPs are loaded and started. A
114 ``FFA_MSG_WAIT`` message is sent to the Normal
115 World.
116
117
Imre Kisc2351582022-11-22 17:16:38 +0100118Each ELF format SP is loaded into the system using ``ldelf`` and started. This
119is based around the same process as loading the early TAs.
120For each binary format SP a simpler method is used to copy the binary into a
121suitable memory area.
Jelle Sels3ef999a2021-03-29 09:35:37 +0200122All SPs are run after they are loaded and run until a ``FFA_MSG_WAIT`` is sent
123by the SP.
124
125
126.. uml::
127
128 autoactivate on
129 secure_partition.c -> secure_partition.c: sp_init_uuid()
130 secure_partition.c -> secure_partition.c: sp_open_session()
131 secure_partition.c -> secure_partition.c: find_sp()
132 return
133 secure_partition.c -> secure_partition.c: sp_create_session()
134 return
Imre Kisc2351582022-11-22 17:16:38 +0100135 alt OP-TEE specific ELF format
136 secure_partition.c -> secure_partition.c:ldelf_load_ldelf()
137 return
138 secure_partition.c -> secure_partition.c:ldlelf_init_with_ldelf()
139 return
140 else SPMC agnostic flat binary format
141 secure_partition.c -> secure_partition.c:load_binary_sp()
142 return
143 end
Jelle Sels3ef999a2021-03-29 09:35:37 +0200144 secure_partition.c -> secure_partition.c: sp_init_set_registers()
145 return
146 return
147 secure_partition.c -> secure_partition.c: enter_sp()
148 return
149 secure_partition.c -> secure_partition.c: sp_msg_handler()
150 return
151 return
152
Imre Kisc2351582022-11-22 17:16:38 +0100153:``init_with_ldelf()``: Load the OP-TEE specific ELF format SP
154:``load_binary_sp()``: Load the SPMC agnostic flat binary format SP
Jelle Sels3ef999a2021-03-29 09:35:37 +0200155:``sp_init_info()``: Initialise the ``struct ffa_init_info``. The
156 ``struct ffa_init_info`` is passed to the SP
157 during it first run.
158:``sp_init_set_registers()``: Initialise the registers of the SP
159:``sp_msg_handler()``: Handle the SPs FF-A message
160
161Once all SPs are loaded and started we return to the SPMD and the Normal World
162is booted.
163
164
165SP message handling
166-------------------
167
168The SPMC is split into 2 main message handlers:
169
170:``thread_spmc_msg_recv()`` thread_spmc.c: Used to handle message coming
171 from the Normal World.
172:``sp_msg_handler()`` spmc_sp_handler.c: Used to handle message where
173 the source or the destination
174 is a SP.
175
176When a ``FFA_MSG_SEND_DIRECT_REQ`` message is received by the SPMC from the
177Normal World, a new thread is started.
178The FF-A message is passed to the thread and it will call the
179``sp_msg_handler()`` function.
180
181Whenever the SPMC (``sp_msg_handler()``) receives a message not intended for
182one of the SPs, it will exit the thread and return to the Normal World
183passing the FF-A message.
184
185Currently only a ``FFA_MSG_SEND_DIRECT_REQ`` can be passed from the Normal
186World to a SP.
187
188.. uml::
189 :width: 800
190
191 skinparam backgroundcolor transparent
192 participant "None-secure world" as None_secure_world
193
194 box "S-EL3"
195 participant SPMD
196 end box
197
198 box "S-EL1"
199 participant thread_spmc
200 participant spmc_sp_handler
201 end box
202
203
204 box "S-EL0"
205 participant SP
206 end box
207
208 autoactivate on
209 None_secure_world -> SPMD: FFA_MSG_SEND_DIRECT_REQ \n <font color=red>SMC
210 SPMD -> thread_spmc: thread_spmc_msg_recv() \n <font color=red>ERET
211
212 thread_spmc -> spmc_sp_handler : spmc_sp_start_thread()
213 == thread ==
214 spmc_sp_handler -> spmc_sp_handler : sp_msg_handler()
215
216 loop FF-A dst != NSW
217 spmc_sp_handler -> spmc_sp_handler: ffa_handle_sp_direct_req()
218 spmc_sp_handler -> spmc_sp_handler: enter_sp()
219 spmc_sp_handler -> spmc_sp_handler: sp_enter_invoke_cmd()
220 autoactivate off
221 spmc_sp_handler -> SP: __thread_enter_user_mode() \n <font color=red>ERET
222 activate SP
223
224 SP -> spmc_sp_handler : (FF-A message) sp_handle_svc() \n <font color=red>SVC
225 deactivate SP
226 activate spmc_sp_handler
227 spmc_sp_handler -> spmc_sp_handler: Store SP context
228 spmc_sp_handler -> spmc_sp_handler: return to sp_enter_invoke_cmd()
229 deactivate spmc_sp_handler
230 spmc_sp_handler -> spmc_sp_handler: Retrieve FF-A message from SP context
231 return
232 return
233 return
234 end
235 return
236 == End of thread ==
237 return
238 return \n <font color=red>SMC
239 return FFA_MSG_SEND_DIRECT_RESP \n <font color=red>ERET
240
241
242Every message received by the SPMC from the Normal World is handled in the
243``thread_spmc_msg_recv()`` function.
244
245When entering a SP we need to be running in a OP-TEE thread. This is needed to
246be able to push the TS session (We push the TS session to get access to the SP
247memory).
248Currently the only possibility to enter a SP from the Normal world is via a
249``FFA_MSG_SEND_DIRECT_REQ``. Whenever we receive a ``FFA_MSG_SEND_DIRECT_REQ``
250message which doesn't have OP-TEE as the endpoint-id, we start a thread and
251forward the FF-A message to the ``sp_msg_handler()``.
252
253The ``sp_msg_handler()`` is responsible for all messages coming or going
254to/from a SP. It runs in a while loop and will handle every message until it
255comes across a messages which is not intended for the secure world.
256After a message is handled by the SPMC or when it needs to be forwarded to a SP,
257``sp_enter()`` is called.
258``sp_enter()`` will copy the FF-A arguments and resume the SP.
259
260When the SPMC needs to have access to the SPs memory, it will call
261``ts_push_current_session()`` to gain access and ``ts_pop_current_session()``
262to release the access.
263
264Running and exiting SPs
265-----------------------
266
267The SPMC resumes/starts the SP by calling the ``sp_enter()``. This will set up
268the SP context and jump into S-EL0.
269Whenever the SP performs a system call it will end up in ``sp_handle_svc()``.
270``sp_handle_svc()`` stores the current context of the SP and makes sure that we
271don't return to S-EL0 but instead returns to S-EL1 back to ``sp_enter()``.
272``sp_enter()`` will pass the FF-A registers (x0-x7) to
273``spmc_sp_msg_handler()``. This will process the FF-A message.
274
275
276RxTx buffer managment
277---------------------
278RxTx buffers are used by the SPMC to exchange information between an endpoint
279and the SPMC. The rxtx_buf struct is used by the SPMC for abstracting buffer
280management.
281Every SP has a ``struct rxtx_buf`` wich will be passed to every function that
282needs access to the rxtx buffer.
283A separate ``struct rxtx_buf`` is defined for the Normal World, which gives
284access to the Normal World buffers.
285
286
287Configuration
288=============
289
Balint Dobszayfe29e2b2022-09-27 15:41:30 +0200290SPMC config options
291---------------------------
Jelle Sels3ef999a2021-03-29 09:35:37 +0200292
Balint Dobszayfe29e2b2022-09-27 15:41:30 +0200293To configure OP-TEE as a S-EL1 SPMC with Secure Partition support, the following
294flags should be set for optee_os:
Jelle Sels3ef999a2021-03-29 09:35:37 +0200295
Balint Dobszayfe29e2b2022-09-27 15:41:30 +0200296- ``CFG_CORE_SEL1_SPMC=y``
297- ``CFG_SECURE_PARTITION=y``
298- ``CFG_DT=y``
299- ``CFG_MAP_EXT_DT_SECURE=y``
Jelle Sels3ef999a2021-03-29 09:35:37 +0200300
Balint Dobszayfe29e2b2022-09-27 15:41:30 +0200301Furthermore TF-A should be configured as the SPMD, expecting a S-EL1 SPMC:
Jelle Sels3ef999a2021-03-29 09:35:37 +0200302
Balint Dobszayfe29e2b2022-09-27 15:41:30 +0200303- ``SPD=spmd``
304- ``SPMD_SPM_AT_SEL2=0``
305- ``ARM_SPMC_MANIFEST_DTS=<path to SPMC manifest dts>``
306
307SP loading mechanism
308---------------------
309
310OP-TEE SPMC supports two methods for finding and loading the SP executable
311images. Currently only ELF executables are supported. In the build repo the
312loading method can be selected with the SP_PACKAGING_METHOD option.
313
314Embedded SP
315^^^^^^^^^^^
316
317In this case the early TA mechanism of optee_os is reused: the SP ELF files are
318embedded into the main OP-TEE binary. Each ELF should start with a specific
319section (.sp_head) containing a struct which describes the SP (UUID, stack size,
320etc.). The images can be added to optee_os using the ``SP_PATHS`` config option,
321the build repo will set this up automatically when
322``SP_PACKAGING_METHOD=embedded`` is selected. The images passed in ``SP_PATHS``
323are processed by ``ts_bin_to_c.py`` in optee_os and linked into the main binary.
324At runtime the ``for_each_secure_partition()`` macro can iterate through these
Imre Kisc2351582022-11-22 17:16:38 +0100325images, so a particular SP can be found by UUID and then loaded.
Balint Dobszayfe29e2b2022-09-27 15:41:30 +0200326
327The SP manifest file `[1]`_ used by the SPMC to setup SPs is also handled by
328``ts_bin_to_c.py``, it will be concatenated to the end of the SP ELF.
329
330FIP SP
331^^^^^^
332
333In this case the SP ELF files and the corresponding SP manifest DTs are
334encapsulated into SP packages and packed into the FIP. The goal of providing
335this alternative flow is to make updating SPs easier (independent of the main
336OP-TEE binary) and to get aligned with Hafnium (S-EL2 SPMC). For more
337information about the FIP, please refer to the TF-A documentation `[2]`_. The SP
338packaging process and the package format is provided by TF-A, detailed
339description is available at `[3]`_. In the build repo this method can be
340selected by ``SP_PACKAGING_METHOD=fip``, it covers all the necessary setup
341automatically. In case of using another buildsystem, the following steps should
342be implemented:
343
344- TF-A config ``SP_LAYOUT_FILE``: provide a JSON file which describes the SPs
345 (path to SP executable and corresponding DT, example `[4]`_). The TF-A
346 buildsystem will create the SP packages (using sptool) based on this and pack
347 them into the FIP.
348
349- TF-A config ``ARM_BL2_SP_LIST_DTS``: provide a DT snippet which describes the
350 SPs' UUIDs and load addresses (example: `[5]`_). This will be injected into
351 the SP list in ``TB_FW_CONFIG`` DT of TF-A, and BL2 will load the SP packages
352 based on this. Note that BL2 doesn't automatically load all images from the
353 FIP: it's necessary to explicitly define them in ``TB_FW_CONFIG`` (using this
354 injected snippet or manually editing the DT).
355
356- TF-A config ``ARM_SPMC_MANIFEST_DTS``: provide the SPMC manifest (example:
357 `[6]`_). This DT is passed to the SPMC as a boot argument (in the TF-A naming
358 convention this is the ``TOS_FW_CONFIG``). It should contain the list of SP
359 packages and their load addresses in the ``compatible = "arm,sp_pkg"`` node.
360
361At boot optee_os will parse the SP package load addresses from the SPMC manifest
362and find the SP packages already loaded by BL2. Iterating through the SP
363packages, based on the SP package header in each package it will map the SP
364executable image and the corresponding manifest DT and collect these to the
365``fip_sp_list`` list. Later when initialising the SPs, the ``for_each_fip_sp``
Imre Kisc2351582022-11-22 17:16:38 +0100366macro is used to iterate this list and load the executables, just like for the
367embedded SP case.
Balint Dobszayfe29e2b2022-09-27 15:41:30 +0200368
369.. _[1]:
370
371[1] https://trustedfirmware-a.readthedocs.io/en/v2.6/components/ffa-manifest-binding.html
372
373.. _[2]:
374
375[2] https://trustedfirmware-a.readthedocs.io/en/v2.6/design/firmware-design.html#firmware-image-package-fip
376
377.. _[3]:
378
379[3] https://trustedfirmware-a.readthedocs.io/en/v2.6/components/secure-partition-manager.html#secure-partition-packages
380
381.. _[4]:
382
383[4] https://trustedfirmware-a.readthedocs.io/en/v2.6/components/secure-partition-manager.html#describing-secure-partitions
384
385.. _[5]:
386
387[5] https://github.com/OP-TEE/build/blob/master/fvp/bl2_sp_list.dtsi
388
389.. _[6]:
390
391[6] https://github.com/OP-TEE/build/blob/master/fvp/spmc_manifest.dts