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