el3-spmc.rst 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. EL3 Secure Partition Manager
  2. ****************************
  3. .. contents::
  4. Foreword
  5. ========
  6. This document describes the design of the EL3 SPMC based on the FF-A specification.
  7. EL3 SPMC provides reference FF-A compliant implementation without S-EL2 virtualization support,
  8. to help adopt and migrate to FF-A early.
  9. EL3 SPMC implementation in TF-A:
  10. - Manages a single S-EL1 Secure Partition
  11. - Provides a standard protocol for communication and memory sharing between FF-A endpoints.
  12. - Provides support for EL3 Logical Partitions to support easy migration from EL3 to S-EL1.
  13. Sample reference stack
  14. ======================
  15. The following diagram illustrates a possible configuration when the
  16. FEAT_SEL2 architecture extension is not implemented, showing the SPMD
  17. and SPMC at EL3, one S-EL1 secure partition, with an optional
  18. Hypervisor:
  19. .. image:: ../resources/diagrams/ff-a-spm-at-el3.png
  20. TF-A build options
  21. ==================
  22. This section explains the TF-A build options involved in building
  23. an FF-A based SPM where the SPMD and SPMC are located at EL3:
  24. - **SPD=spmd**: this option selects the SPMD component to relay the FF-A
  25. protocol from NWd to SWd back and forth. It is not possible to
  26. enable another Secure Payload Dispatcher when this option is chosen.
  27. - **SPMC_AT_EL3**: this option adjusts the SPMC exception level to being
  28. at EL3.
  29. - **ARM_SPMC_MANIFEST_DTS**: this option specifies a manifest file
  30. providing SP description. It is required when
  31. ``SPMC_AT_EL3`` is enabled, the secure partitions are loaded
  32. by BL2 on behalf of the SPMC.
  33. Notes:
  34. - BL32 option is re-purposed to specify the S-EL1 TEE or SP image.
  35. BL32 option can be omitted if using TF-A Test Secure Payload as SP.
  36. - BL33 option can specify the TFTF binary or a normal world loader
  37. such as U-Boot or the UEFI framework payload.
  38. Sample TF-A build command line when the SPMC is located at EL3:
  39. .. code:: shell
  40. make \
  41. CROSS_COMPILE=aarch64-none-elf- \
  42. SPD=spmd \
  43. SPMD_SPM_AT_SEL2=0 \
  44. SPMC_AT_EL3=1 \
  45. BL32=<path-to-tee-binary> (opt for TSP) \
  46. BL33=<path-to-bl33-binary> \
  47. PLAT=fvp \
  48. all fip
  49. FVP model invocation
  50. ====================
  51. Sample FVP command line invocation:
  52. .. code:: shell
  53. <path-to-fvp-model>/FVP_Base_RevC-2xAEMvA -C pctl.startup=0.0.0.0 \
  54. -C cluster0.NUM_CORES=4 -C cluster1.NUM_CORES=4 -C bp.secure_memory=1 \
  55. -C bp.secureflashloader.fname=trusted-firmware-a/build/fvp/debug/bl1.bin \
  56. -C bp.flashloader0.fname=trusted-firmware-a/build/fvp/debug/fip.bin \
  57. -C bp.pl011_uart0.out_file=fvp-uart0.log -C bp.pl011_uart1.out_file=fvp-uart1.log \
  58. -C bp.pl011_uart2.out_file=fvp-uart2.log -C bp.vis.disable_visualisation=1
  59. Platform Guide
  60. ==============
  61. - Platform Hooks See - `[4]`_
  62. - plat_spmc_shmem_begin
  63. - plat_spmc_shmem_reclaim
  64. SPMC provides platform hooks related to memory management interfaces.
  65. These hooks can be used for platform specific implementations like
  66. for managing access control, programming TZ Controller or MPUs.
  67. These hooks are called by SPMC before the initial share request completes,
  68. and after the final reclaim has been completed.
  69. - Datastore
  70. - plat_spmc_shmem_datastore_get
  71. EL3 SPMC uses datastore for tracking memory transaction descriptors.
  72. On FVP platform datastore is allocated from TZC DRAM section.
  73. Other platforms need to allocate a similar secure memory region
  74. to be used as shared memory datastore.
  75. The accessor function is used during SPMC initialization to obtain
  76. address and size of the datastore.
  77. SPMC will also zero out the provided memory region.
  78. - Platform Defines See - `[5]`_
  79. - SECURE_PARTITION_COUNT
  80. Number of Secure Partitions supported: must be 1.
  81. - NS_PARTITION_COUNT
  82. Number of NWd Partitions supported.
  83. - MAX_EL3_LP_DESCS_COUNT
  84. Number of Logical Partitions supported.
  85. Logical Secure Partition (LSP)
  86. ==============================
  87. - The SPMC provides support for statically allocated EL3 Logical Secure Partitions
  88. as per FF-A v1.1 specification.
  89. - The DECLARE_LOGICAL_PARTITION macro can be used to add a LSP.
  90. - For reference implementation See - `[2]`_
  91. .. image:: ../resources/diagrams/ff-a-lsp-at-el3.png
  92. SPMC boot
  93. =========
  94. The SPMD and SPMC are built into the BL31 image along with TF-A's runtime components.
  95. BL2 loads the BL31 image as a part of (secure) boot process.
  96. The SPMC manifest is loaded by BL2 as the ``TOS_FW_CONFIG`` image `[9]`_.
  97. BL2 passes the SPMC manifest address to BL31 through a register.
  98. At boot time, the SPMD in BL31 runs from the primary core, initializes the core
  99. contexts and launches the SPMC passing the following information through
  100. registers:
  101. - X0 holds the SPMC manifest blob address.
  102. - X4 holds the currently running core linear id.
  103. Parsing SP partition manifests
  104. ------------------------------
  105. SPMC consumes the SP manifest, as defined in `[7]`_.
  106. SP manifest fields align with Hafnium SP manifest for easy porting.
  107. .. code:: shell
  108. compatible = "arm,ffa-manifest-1.0";
  109. ffa-version = <0x00010001>; /* 31:16 - Major, 15:0 - Minor */
  110. id = <0x8001>;
  111. uuid = <0x6b43b460 0x74a24b78 0xade24502 0x40682886>;
  112. messaging-method = <0x3>; /* Direct Messaging Only */
  113. exception-level = <0x2>; /* S-EL1 */
  114. execution-state = <0>;
  115. execution-ctx-count = <8>;
  116. gp-register-num = <0>;
  117. power-management-messages = <0x7>;
  118. Passing boot data to the SP
  119. ---------------------------
  120. In `[1]`_ , the section "Boot information protocol" defines a method for passing
  121. data to the SPs at boot time. It specifies the format for the boot information
  122. descriptor and boot information header structures, which describe the data to be
  123. exchanged between SPMC and SP.
  124. The specification also defines the types of data that can be passed.
  125. The aggregate of both the boot info structures and the data itself is designated
  126. the boot information blob, and is passed to a Partition as a contiguous memory
  127. region.
  128. Currently, the SPM implementation supports the FDT type which is used to pass the
  129. partition's DTB manifest.
  130. The region for the boot information blob is statically allocated (4K) by SPMC.
  131. BLOB contains Boot Info Header, followed by SP Manifest contents.
  132. The configuration of the boot protocol is done in the SP manifest. As defined by
  133. the specification, the manifest field 'gp-register-num' configures the GP register
  134. which shall be used to pass the address to the partitions boot information blob when
  135. booting the partition.
  136. Supported interfaces
  137. ====================
  138. The following interfaces are exposed to SPs only:
  139. - ``FFA_MSG_WAIT``
  140. - ``FFA_MEM_RETRIEVE_REQ``
  141. - ``FFA_MEM_RETRIEVE_RESP``
  142. - ``FFA_MEM_RELINQUISH``
  143. - ``FFA_SECONDARY_EP_REGISTER``
  144. The following interfaces are exposed to both NS Client and SPs:
  145. - ``FFA_VERSION``
  146. - ``FFA_FEATURES``
  147. - ``FFA_RX_RELEASE``
  148. - ``FFA_RXTX_MAP``
  149. - ``FFA_RXTX_UNMAP``
  150. - ``FFA_PARTITION_INFO_GET``
  151. - ``FFA_ID_GET``
  152. - ``FFA_MSG_SEND_DIRECT_REQ``
  153. - ``FFA_MSG_SEND_DIRECT_RESP``
  154. - ``FFA_MEM_FRAG_TX``
  155. - ``FFA_SPM_ID_GET``
  156. The following additional interfaces are forwarded from SPMD to support NS Client:
  157. - ``FFA_RUN``
  158. - ``FFA_MEM_LEND``
  159. - ``FFA_MEM_SHARE``
  160. - ``FFA_MEM_FRAG_RX``
  161. - ``FFA_MEM_RECLAIM``
  162. FFA_VERSION
  163. -----------
  164. ``FFA_VERSION`` requires a *requested_version* parameter from the caller.
  165. SPMD forwards call to SPMC, the SPMC returns its own implemented version.
  166. SPMC asserts SP and SPMC are at same FF-A Version.
  167. FFA_FEATURES
  168. ------------
  169. FF-A features supported by the SPMC may be discovered by secure partitions at
  170. boot (that is prior to NWd is booted) or run-time.
  171. The SPMC calling FFA_FEATURES at secure physical FF-A instance always get
  172. FFA_SUCCESS from the SPMD.
  173. The request made by an Hypervisor or OS kernel is forwarded to the SPMC and
  174. the response relayed back to the NWd.
  175. FFA_RXTX_MAP
  176. ------------
  177. FFA_RXTX_UNMAP
  178. --------------
  179. When invoked from a secure partition FFA_RXTX_MAP maps the provided send and
  180. receive buffers described by their PAs to the EL3 translation regime
  181. as secure buffers in the MMU descriptors.
  182. When invoked from the Hypervisor or OS kernel, the buffers are mapped into the
  183. SPMC EL3 translation regime and marked as NS buffers in the MMU
  184. descriptors.
  185. The FFA_RXTX_UNMAP unmaps the RX/TX pair from the translation regime of the
  186. caller, either it being the Hypervisor or OS kernel, as well as a secure
  187. partition.
  188. FFA_PARTITION_INFO_GET
  189. ----------------------
  190. Partition info get call can originate:
  191. - from SP to SPMC
  192. - from Hypervisor or OS kernel to SPMC. The request is relayed by the SPMD.
  193. The format (v1.0 or v1.1) of the populated data structure returned is based upon the
  194. FFA version of the calling entity.
  195. EL3 SPMC also supports returning only the count of partitions deployed.
  196. All LSPs and SP are discoverable from FFA_PARTITION_INFO_GET call made by
  197. either SP or NWd entities.
  198. FFA_ID_GET
  199. ----------
  200. The FF-A ID space is split into a non-secure space and secure space:
  201. - FF-A ID with bit 15 clear relates to VMs.
  202. - FF-A ID with bit 15 set related to SPs or LSPs.
  203. - FF-A IDs 0, 0xffff, 0x8000 are assigned respectively to the Hypervisor
  204. (or OS Kernel if Hyp is absent), SPMD and SPMC.
  205. This convention helps the SPM to determine the origin and destination worlds in
  206. an FF-A ABI invocation. In particular the SPM shall filter unauthorized
  207. transactions in its world switch routine. It must not be permitted for a VM to
  208. use a secure FF-A ID as origin world by spoofing:
  209. - A VM-to-SP direct request/response shall set the origin world to be non-secure
  210. (FF-A ID bit 15 clear) and destination world to be secure (FF-A ID bit 15
  211. set).
  212. - Similarly, an SP-to-LSP direct request/response shall set the FF-A ID bit 15
  213. for both origin and destination IDs.
  214. An incoming direct message request arriving at SPMD from NWd is forwarded to
  215. SPMC without a specific check. The SPMC is resumed through eret and "knows" the
  216. message is coming from normal world in this specific code path. Thus the origin
  217. endpoint ID must be checked by SPMC for being a normal world ID.
  218. An SP sending a direct message request must have bit 15 set in its origin
  219. endpoint ID and this can be checked by the SPMC when the SP invokes the ABI.
  220. The SPMC shall reject the direct message if the claimed world in origin endpoint
  221. ID is not consistent:
  222. - It is either forwarded by SPMD and thus origin endpoint ID must be a "normal
  223. world ID",
  224. - or initiated by an SP and thus origin endpoint ID must be a "secure world ID".
  225. FFA_MSG_SEND_DIRECT_REQ
  226. -----------------------
  227. FFA_MSG_SEND_DIRECT_RESP
  228. ------------------------
  229. This is a mandatory interface for secure partitions participating in direct request
  230. and responses with the following rules:
  231. - An SP can send a direct request to LSP.
  232. - An LSP can send a direct response to SP.
  233. - An SP cannot send a direct request to an Hypervisor or OS kernel.
  234. - An Hypervisor or OS kernel can send a direct request to an SP or LSP.
  235. - An SP and LSP can send a direct response to an Hypervisor or OS kernel.
  236. - SPMD can send direct request to SPMC.
  237. FFA_SPM_ID_GET
  238. --------------
  239. Returns the FF-A ID allocated to an SPM component which can be one of SPMD
  240. or SPMC.
  241. At initialization, the SPMC queries the SPMD for the SPMC ID, using the
  242. FFA_ID_GET interface, and records it. The SPMC can also query the SPMD ID using
  243. the FFA_SPM_ID_GET interface at the secure physical FF-A instance.
  244. Secure partitions call this interface at the virtual FF-A instance, to which
  245. the SPMC returns the SPMC ID.
  246. The Hypervisor or OS kernel can issue the FFA_SPM_ID_GET call handled by the
  247. SPMD, which returns the SPMC ID.
  248. FFA_ID_GET
  249. ----------
  250. Returns the FF-A ID of the calling endpoint.
  251. FFA_MEM_SHARE
  252. -------------
  253. FFA_MEM_LEND
  254. ------------
  255. - If SP is borrower in the memory transaction, these calls are forwarded to SPMC.
  256. SPMC performs Relayer responsibilities, caches the memory descriptors in the datastore,
  257. and allocates FF-A memory handle.
  258. - If format of descriptor was v1.0, SPMC converts the descriptor to v1.1 before caching.
  259. In case of fragmented sharing, conversion of memory descriptors happens after last
  260. fragment has been received.
  261. - Multiple borrowers (including NWd endpoint) and fragmented memory sharing are supported.
  262. FFA_MEM_RETRIEVE_REQ
  263. --------------------
  264. FFA_MEM_RETRIEVE_RESP
  265. ---------------------
  266. - Memory retrieve is supported only from SP.
  267. - SPMC fetches the cached memory descriptor from the datastore,
  268. - Performs Relayer responsiilities and sends FFA_MEM_RETRIEVE_RESP back to SP.
  269. - If descriptor size is more than RX buffer size, SPMC will send the descriptor in fragments.
  270. - SPMC will set NS Bit to 1 in memory descriptor response.
  271. FFA_MEM_FRAG_RX
  272. ---------------
  273. FFA_MEM_FRAG_TX
  274. ---------------
  275. FFA_MEM_FRAG_RX is to be used by:
  276. - SP if FFA_MEM_RETRIEVE_RESP returned descriptor with fragment length less than total length.
  277. - or by SPMC if FFA_MEM_SHARE/FFA_MEM_LEND is called with fragment length less than total length.
  278. SPMC validates handle and Endpoint ID and returns response with FFA_MEM_FRAG_TX.
  279. FFA_SECONDARY_EP_REGISTER
  280. -------------------------
  281. When the SPMC boots, secure partition is initialized on its primary
  282. Execution Context.
  283. The FFA_SECONDARY_EP_REGISTER interface is to be used by a secure partition
  284. from its first execution context, to provide the entry point address for
  285. secondary execution contexts.
  286. A secondary EC is first resumed either upon invocation of PSCI_CPU_ON from
  287. the NWd or by invocation of FFA_RUN.
  288. Power management
  289. ================
  290. In platforms with or without secure virtualization:
  291. - The NWd owns the platform PM policy.
  292. - The Hypervisor or OS kernel is the component initiating PSCI service calls.
  293. - The EL3 PSCI library is in charge of the PM coordination and control
  294. (eventually writing to platform registers).
  295. - While coordinating PM events, the PSCI library calls backs into the Secure
  296. Payload Dispatcher for events the latter has statically registered to.
  297. When using the SPMD as a Secure Payload Dispatcher:
  298. - A power management event is relayed through the SPD hook to the SPMC.
  299. - In the current implementation CPU_ON (svc_on_finish), CPU_OFF
  300. (svc_off), CPU_SUSPEND (svc_suspend) and CPU_SUSPEND_RESUME (svc_suspend_finish)
  301. hooks are registered.
  302. Secure partitions scheduling
  303. ============================
  304. The FF-A specification `[1]`_ provides two ways to relinquinsh CPU time to
  305. secure partitions. For this a VM (Hypervisor or OS kernel), or SP invokes one of:
  306. - the FFA_MSG_SEND_DIRECT_REQ interface.
  307. - the FFA_RUN interface.
  308. Additionally a secure interrupt can pre-empt the normal world execution and give
  309. CPU cycles by transitioning to EL3.
  310. Partition Runtime State and Model
  311. =================================
  312. EL3 SPMC implements Partition runtime states are described in v1.1 FF-A specification `[1]`_
  313. An SP can be in one of the following state:
  314. - RT_STATE_WAITING
  315. - RT_STATE_RUNNING
  316. - RT_STATE_PREEMPTED
  317. - RT_STATE_BLOCKED
  318. An SP will transition to one of the following runtime model when not in waiting state:
  319. - RT_MODEL_DIR_REQ
  320. - RT_MODEL_RUN
  321. - RT_MODEL_INIT
  322. - RT_MODEL_INTR
  323. Platform topology
  324. =================
  325. SPMC only supports a single Pinned MP S-EL1 SP. The *execution-ctx-count*
  326. SP manifest field should match the number of physical PE.
  327. Interrupt handling
  328. ==================
  329. Secure Interrupt handling
  330. -------------------------
  331. - SPMC is capable of forwarding Secure interrupt to S-EL1 SP
  332. which has preempted the normal world.
  333. - Interrupt is forwarded to SP using FFA_INTERRUPT interface.
  334. - Interrupt Number is not passed, S-EL1 SP can access the GIC registers directly.
  335. - Upon completion of Interrupt handling SP is expected to return to
  336. SPMC using FFA_MSG_WAIT interface.
  337. - SPMC returns to normal world after interrupt handling is completed.
  338. In the scenario when secure interrupt occurs while the secure partition is running,
  339. the SPMC is not involved and the handling is implementation defined in the TOS.
  340. Non-Secure Interrupt handling
  341. -----------------------------
  342. The 'managed exit' scenario is the responsibility of the TOS and the SPMC is not involved.
  343. Test Secure Payload (TSP)
  344. =========================
  345. - TSP provides reference implementation of FF-A programming model.
  346. - TSP has the following support:
  347. - SP initialization on all CPUs.
  348. - Consuming Power Messages including CPU_ON, CPU_OFF, CPU_SUSPEND, CPU_SUSPEND_RESUME.
  349. - Event Loop to receive Direct Requests.
  350. - Sending Direct Response.
  351. - Memory Sharing helper library.
  352. - Ability to handle secure interrupt (timer).
  353. TSP Tests in CI
  354. ---------------
  355. - TSP Tests are exercised in the TF-A CI using prebuilt FF-A Linux Test driver in NWd.
  356. - Expected output:
  357. .. code:: shell
  358. #ioctl 255
  359. Test: Echo Message to SP.
  360. Status: Completed Test Case: 1
  361. Test Executed Successfully
  362. Test: Message Relay vis SP to EL3 LSP.
  363. Status: Completed Test Case: 2
  364. Test Executed Successfully
  365. Test: Memory Send.
  366. Verified 1 constituents successfully
  367. Status: Completed Test Case: 3
  368. Test Executed Successfully
  369. Test: Memory Send in Fragments.
  370. Verified 256 constituents successfully
  371. Status: Completed Test Case: 4
  372. Test Executed Successfully
  373. Test: Memory Lend.
  374. Verified 1 constituents successfully
  375. Status: Completed Test Case: 5
  376. Test Executed Successfully
  377. Test: Memory Lend in Fragments.
  378. Verified 256 constituents successfully
  379. Status: Completed Test Case: 6
  380. Test Executed Successfully
  381. Test: Memory Send with Multiple Endpoints.
  382. random: fast init done
  383. Verified 256 constituents successfully
  384. Status: Completed Test Case: 7
  385. Test Executed Successfully
  386. Test: Memory Lend with Multiple Endpoints.
  387. Verified 256 constituents successfully
  388. Status: Completed Test Case: 8
  389. Test Executed Successfully
  390. Test: Ensure Duplicate Memory Send Requests are Rejected.
  391. Status: Completed Test Case: 9
  392. Test Executed Successfully
  393. Test: Ensure Duplicate Memory Lend Requests are Rejected.
  394. Status: Completed Test Case: 10
  395. Test Executed Successfully
  396. 0 Tests Failed
  397. Exiting Test Application - Total Failures: 0
  398. References
  399. ==========
  400. .. _[1]:
  401. [1] `Arm Firmware Framework for Arm A-profile <https://developer.arm.com/docs/den0077/latest>`__
  402. .. _[2]:
  403. [2] https://git.trustedfirmware.org/TF-A/trusted-firmware-a.git/tree/plat/arm/board/fvp/fvp_el3_spmc_logical_sp.c
  404. .. _[3]:
  405. [3] `Trusted Boot Board Requirements
  406. Client <https://developer.arm.com/documentation/den0006/d/>`__
  407. .. _[4]:
  408. [4] https://git.trustedfirmware.org/TF-A/trusted-firmware-a.git/tree/plat/arm/board/fvp/fvp_el3_spmc.c
  409. .. _[5]:
  410. [5] https://git.trustedfirmware.org/TF-A/trusted-firmware-a.git/tree/plat/arm/board/fvp/include/platform_def.h
  411. .. _[6]:
  412. [6] https://trustedfirmware-a.readthedocs.io/en/latest/components/ffa-manifest-binding.html
  413. .. _[7]:
  414. [7] https://git.trustedfirmware.org/TF-A/trusted-firmware-a.git/tree/plat/arm/board/fvp/fdts/fvp_tsp_sp_manifest.dts
  415. .. _[8]:
  416. [8] https://lists.trustedfirmware.org/archives/list/tf-a@lists.trustedfirmware.org/thread/CFQFGU6H2D5GZYMUYGTGUSXIU3OYZP6U/
  417. .. _[9]:
  418. [9] https://trustedfirmware-a.readthedocs.io/en/latest/design/firmware-design.html#dynamic-configuration-during-cold-boot
  419. --------------
  420. *Copyright (c) 2020-2022, Arm Limited and Contributors. All rights reserved.*