rt-svc-writers-guide.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. EL3 Runtime Service Writer's Guide
  2. =====================================================
  3. Introduction
  4. ------------
  5. This document describes how to add a runtime service to the EL3 Runtime
  6. Firmware component of Trusted Firmware-A (TF-A), BL31.
  7. Software executing in the normal world and in the trusted world at exception
  8. levels lower than EL3 will request runtime services using the Secure Monitor
  9. Call (SMC) instruction. These requests will follow the convention described in
  10. the SMC Calling Convention PDD (`SMCCC`_). The `SMCCC`_ assigns function
  11. identifiers to each SMC request and describes how arguments are passed and
  12. results are returned.
  13. SMC Functions are grouped together based on the implementor of the service, for
  14. example a subset of the Function IDs are designated as "OEM Calls" (see `SMCCC`_
  15. for full details). The EL3 runtime services framework in BL31 enables the
  16. independent implementation of services for each group, which are then compiled
  17. into the BL31 image. This simplifies the integration of common software from
  18. Arm to support `PSCI`_, Secure Monitor for a Trusted OS and SoC specific
  19. software. The common runtime services framework ensures that SMC Functions are
  20. dispatched to their respective service implementation - the
  21. :ref:`Firmware Design` document provides details of how this is achieved.
  22. The interface and operation of the runtime services depends heavily on the
  23. concepts and definitions described in the `SMCCC`_, in particular SMC Function
  24. IDs, Owning Entity Numbers (OEN), Fast and Standard calls, and the SMC32 and
  25. SMC64 calling conventions. Please refer to that document for a full explanation
  26. of these terms.
  27. Owning Entities, Call Types and Function IDs
  28. --------------------------------------------
  29. The SMC Function Identifier includes a OEN field. These values and their
  30. meaning are described in `SMCCC`_ and summarized in table 1 below. Some entities
  31. are allocated a range of of OENs. The OEN must be interpreted in conjunction
  32. with the SMC call type, which is either *Fast* or *Yielding*. Fast calls are
  33. uninterruptible whereas Yielding calls can be pre-empted. The majority of
  34. Owning Entities only have allocated ranges for Fast calls: Yielding calls are
  35. reserved exclusively for Trusted OS providers or for interoperability with
  36. legacy 32-bit software that predates the `SMCCC`_.
  37. ::
  38. Type OEN Service
  39. Fast 0 Arm Architecture calls
  40. Fast 1 CPU Service calls
  41. Fast 2 SiP Service calls
  42. Fast 3 OEM Service calls
  43. Fast 4 Standard Secure Service calls
  44. Fast 5 Standard Hypervisor Service Calls
  45. Fast 6 Vendor Specific Hypervisor Service Calls
  46. Fast 7 Vendor Specific EL3 Monitor Calls
  47. Fast 8-47 Reserved for future use
  48. Fast 48-49 Trusted Application calls
  49. Fast 50-63 Trusted OS calls
  50. Yielding 0- 1 Reserved for existing Armv7-A calls
  51. Yielding 2-63 Trusted OS Standard Calls
  52. *Table 1: Service types and their corresponding Owning Entity Numbers*
  53. Each individual entity can allocate the valid identifiers within the entity
  54. range as they need - it is not necessary to coordinate with other entities of
  55. the same type. For example, two SoC providers can use the same Function ID
  56. within the SiP Service calls OEN range to mean different things - as these
  57. calls should be specific to the SoC. The Standard Runtime Calls OEN is used for
  58. services defined by Arm standards, such as `PSCI`_.
  59. The SMC Function ID also indicates whether the call has followed the SMC32
  60. calling convention, where all parameters are 32-bit, or the SMC64 calling
  61. convention, where the parameters are 64-bit. The framework identifies and
  62. rejects invalid calls that use the SMC64 calling convention but that originate
  63. from an AArch32 caller.
  64. The EL3 runtime services framework uses the call type and OEN to identify a
  65. specific handler for each SMC call, but it is expected that an individual
  66. handler will be responsible for all SMC Functions within a given service type.
  67. Getting started
  68. ---------------
  69. TF-A has a ``services`` directory in the source tree under which
  70. each owning entity can place the implementation of its runtime service. The
  71. `PSCI`_ implementation is located here in the ``lib/psci`` directory.
  72. Runtime service sources will need to include the ``runtime_svc.h`` header file.
  73. Registering a runtime service
  74. -----------------------------
  75. A runtime service is registered using the ``DECLARE_RT_SVC()`` macro, specifying
  76. the name of the service, the range of OENs covered, the type of service and
  77. initialization and call handler functions.
  78. .. code:: c
  79. #define DECLARE_RT_SVC(_name, _start, _end, _type, _setup, _smch)
  80. - ``_name`` is used to identify the data structure declared by this macro, and
  81. is also used for diagnostic purposes
  82. - ``_start`` and ``_end`` values must be based on the ``OEN_*`` values defined in
  83. ``smccc.h``
  84. - ``_type`` must be one of ``SMC_TYPE_FAST`` or ``SMC_TYPE_YIELD``
  85. - ``_setup`` is the initialization function with the ``rt_svc_init`` signature:
  86. .. code:: c
  87. typedef int32_t (*rt_svc_init)(void);
  88. - ``_smch`` is the SMC handler function with the ``rt_svc_handle`` signature:
  89. .. code:: c
  90. typedef uintptr_t (*rt_svc_handle_t)(uint32_t smc_fid,
  91. u_register_t x1, u_register_t x2,
  92. u_register_t x3, u_register_t x4,
  93. void *cookie,
  94. void *handle,
  95. u_register_t flags);
  96. Details of the requirements and behavior of the two callbacks is provided in
  97. the following sections.
  98. During initialization the services framework validates each declared service
  99. to ensure that the following conditions are met:
  100. #. The ``_start`` OEN is not greater than the ``_end`` OEN
  101. #. The ``_end`` OEN does not exceed the maximum OEN value (63)
  102. #. The ``_type`` is one of ``SMC_TYPE_FAST`` or ``SMC_TYPE_YIELD``
  103. #. ``_setup`` and ``_smch`` routines have been specified
  104. ``std_svc_setup.c`` provides an example of registering a runtime service:
  105. .. code:: c
  106. /* Register Standard Service Calls as runtime service */
  107. DECLARE_RT_SVC(
  108. std_svc,
  109. OEN_STD_START,
  110. OEN_STD_END,
  111. SMC_TYPE_FAST,
  112. std_svc_setup,
  113. std_svc_smc_handler
  114. );
  115. Initializing a runtime service
  116. ------------------------------
  117. Runtime services are initialized once, during cold boot, by the primary CPU
  118. after platform and architectural initialization is complete. The framework
  119. performs basic validation of the declared service before calling
  120. the service initialization function (``_setup`` in the declaration). This
  121. function must carry out any essential EL3 initialization prior to receiving a
  122. SMC Function call via the handler function.
  123. On success, the initialization function must return ``0``. Any other return value
  124. will cause the framework to issue a diagnostic:
  125. ::
  126. Error initializing runtime service <name of the service>
  127. and then ignore the service - the system will continue to boot but SMC calls
  128. will not be passed to the service handler and instead return the *Unknown SMC
  129. Function ID* result ``0xFFFFFFFF``.
  130. If the system must not be allowed to proceed without the service, the
  131. initialization function must itself cause the firmware boot to be halted.
  132. If the service uses per-CPU data this must either be initialized for all CPUs
  133. during this call, or be done lazily when a CPU first issues an SMC call to that
  134. service.
  135. Handling runtime service requests
  136. ---------------------------------
  137. SMC calls for a service are forwarded by the framework to the service's SMC
  138. handler function (``_smch`` in the service declaration). This function must have
  139. the following signature:
  140. .. code:: c
  141. typedef uintptr_t (*rt_svc_handle_t)(uint32_t smc_fid,
  142. u_register_t x1, u_register_t x2,
  143. u_register_t x3, u_register_t x4,
  144. void *cookie,
  145. void *handle,
  146. u_register_t flags);
  147. The handler is responsible for:
  148. #. Determining that ``smc_fid`` is a valid and supported SMC Function ID,
  149. otherwise completing the request with the *Unknown SMC Function ID*:
  150. .. code:: c
  151. SMC_RET1(handle, SMC_UNK);
  152. #. Determining if the requested function is valid for the calling security
  153. state. SMC Calls can be made from Non-secure, Secure or Realm worlds and
  154. the framework will forward all calls to the service handler.
  155. The ``flags`` parameter to this function indicates the caller security state
  156. in bits 0 and 5. The ``is_caller_secure(flags)``, ``is_caller_non_secure(flags)``
  157. and ``is_caller_realm(flags)`` helper functions can be used to determine whether
  158. the caller's security state is Secure, Non-secure or Realm respectively.
  159. If invalid, the request should be completed with:
  160. .. code:: c
  161. SMC_RET1(handle, SMC_UNK);
  162. #. Truncating parameters for calls made using the SMC32 calling convention.
  163. Such calls can be determined by checking the CC field in bit[30] of the
  164. ``smc_fid`` parameter, for example by using:
  165. ::
  166. if (GET_SMC_CC(smc_fid) == SMC_32) ...
  167. For such calls, the upper bits of the parameters x1-x4 and the saved
  168. parameters X5-X7 are UNDEFINED and must be explicitly ignored by the
  169. handler. This can be done by truncating the values to a suitable 32-bit
  170. integer type before use, for example by ensuring that functions defined
  171. to handle individual SMC Functions use appropriate 32-bit parameters.
  172. #. Providing the service requested by the SMC Function, utilizing the
  173. immediate parameters x1-x4 and/or the additional saved parameters X5-X7.
  174. The latter can be retrieved using the ``SMC_GET_GP(handle, ref)`` function,
  175. supplying the appropriate ``CTX_GPREG_Xn`` reference, e.g.
  176. .. code:: c
  177. uint64_t x6 = SMC_GET_GP(handle, CTX_GPREG_X6);
  178. #. Implementing the standard SMC32 Functions that provide information about
  179. the implementation of the service. These are the Call Count, Implementor
  180. UID and Revision Details for each service documented in section 6 of the
  181. `SMCCC`_.
  182. TF-A expects owning entities to follow this recommendation.
  183. #. Returning the result to the caller. Based on `SMCCC`_ spec, results are
  184. returned in W0-W7(X0-X7) registers for SMC32(SMC64) calls from AArch64
  185. state. Results are returned in R0-R7 registers for SMC32 calls from AArch32
  186. state. The framework provides a family of macros to set the multi-register
  187. return value and complete the handler:
  188. .. code:: c
  189. AArch64 state:
  190. SMC_RET1(handle, x0);
  191. SMC_RET2(handle, x0, x1);
  192. SMC_RET3(handle, x0, x1, x2);
  193. SMC_RET4(handle, x0, x1, x2, x3);
  194. SMC_RET5(handle, x0, x1, x2, x3, x4);
  195. SMC_RET6(handle, x0, x1, x2, x3, x4, x5);
  196. SMC_RET7(handle, x0, x1, x2, x3, x4, x5, x6);
  197. SMC_RET8(handle, x0, x1, x2, x3, x4, x5, x6, x7);
  198. AArch32 state:
  199. SMC_RET1(handle, r0);
  200. SMC_RET2(handle, r0, r1);
  201. SMC_RET3(handle, r0, r1, r2);
  202. SMC_RET4(handle, r0, r1, r2, r3);
  203. SMC_RET5(handle, r0, r1, r2, r3, r4);
  204. SMC_RET6(handle, r0, r1, r2, r3, r4, r5);
  205. SMC_RET7(handle, r0, r1, r2, r3, r4, r5, r6);
  206. SMC_RET8(handle, r0, r1, r2, r3, r4, r5, r6, r7);
  207. The ``cookie`` parameter to the handler is reserved for future use and can be
  208. ignored. The ``handle`` is returned by the SMC handler - completion of the
  209. handler function must always be via one of the ``SMC_RETn()`` macros.
  210. .. note::
  211. The PSCI and Test Secure-EL1 Payload Dispatcher services do not follow
  212. all of the above requirements yet.
  213. Services that contain multiple sub-services
  214. -------------------------------------------
  215. It is possible that a single owning entity implements multiple sub-services. For
  216. example, the Standard calls service handles ``0x84000000``-``0x8400FFFF`` and
  217. ``0xC4000000``-``0xC400FFFF`` functions. Within that range, the `PSCI`_ service
  218. handles the ``0x84000000``-``0x8400001F`` and ``0xC4000000``-``0xC400001F`` functions.
  219. In that respect, `PSCI`_ is a 'sub-service' of the Standard calls service. In
  220. future, there could be additional such sub-services in the Standard calls
  221. service which perform independent functions.
  222. In this situation it may be valuable to introduce a second level framework to
  223. enable independent implementation of sub-services. Such a framework might look
  224. very similar to the current runtime services framework, but using a different
  225. part of the SMC Function ID to identify the sub-service. TF-A does not provide
  226. such a framework at present.
  227. Secure-EL1 Payload Dispatcher service (SPD)
  228. -------------------------------------------
  229. Services that handle SMC Functions targeting a Trusted OS, Trusted Application,
  230. or other Secure-EL1 Payload are special. These services need to manage the
  231. Secure-EL1 context, provide the *Secure Monitor* functionality of switching
  232. between the normal and secure worlds, deliver SMC Calls through to Secure-EL1
  233. and generally manage the Secure-EL1 Payload through CPU power-state transitions.
  234. TODO: Provide details of the additional work required to implement a SPD and
  235. the BL31 support for these services. Or a reference to the document that will
  236. provide this information....
  237. Additional References:
  238. ----------------------
  239. #. :ref:`ARM SiP Services <arm sip services>`
  240. #. :ref:`Vendor Specific EL3 Monitor Service Calls`
  241. --------------
  242. *Copyright (c) 2014-2024, Arm Limited and Contributors. All rights reserved.*
  243. .. _SMCCC: https://developer.arm.com/docs/den0028/latest
  244. .. _PSCI: https://developer.arm.com/documentation/den0022/latest/
  245. .. _ARM SiP Services: arm-sip-service.rst
  246. .. _Vendor Specific EL3 Monitor Service Calls: ven-el3-service.rst