1*40d553cfSPaul BeesleyTrusted Firmware-A EL3 runtime service writer's guide 2*40d553cfSPaul Beesley===================================================== 3*40d553cfSPaul Beesley 4*40d553cfSPaul Beesley 5*40d553cfSPaul Beesley 6*40d553cfSPaul Beesley.. contents:: 7*40d553cfSPaul Beesley 8*40d553cfSPaul BeesleyIntroduction 9*40d553cfSPaul Beesley------------ 10*40d553cfSPaul Beesley 11*40d553cfSPaul BeesleyThis document describes how to add a runtime service to the EL3 Runtime 12*40d553cfSPaul BeesleyFirmware component of Trusted Firmware-A (TF-A), BL31. 13*40d553cfSPaul Beesley 14*40d553cfSPaul BeesleySoftware executing in the normal world and in the trusted world at exception 15*40d553cfSPaul Beesleylevels lower than EL3 will request runtime services using the Secure Monitor 16*40d553cfSPaul BeesleyCall (SMC) instruction. These requests will follow the convention described in 17*40d553cfSPaul Beesleythe SMC Calling Convention PDD (`SMCCC`_). The `SMCCC`_ assigns function 18*40d553cfSPaul Beesleyidentifiers to each SMC request and describes how arguments are passed and 19*40d553cfSPaul Beesleyresults are returned. 20*40d553cfSPaul Beesley 21*40d553cfSPaul BeesleySMC Functions are grouped together based on the implementor of the service, for 22*40d553cfSPaul Beesleyexample a subset of the Function IDs are designated as "OEM Calls" (see `SMCCC`_ 23*40d553cfSPaul Beesleyfor full details). The EL3 runtime services framework in BL31 enables the 24*40d553cfSPaul Beesleyindependent implementation of services for each group, which are then compiled 25*40d553cfSPaul Beesleyinto the BL31 image. This simplifies the integration of common software from 26*40d553cfSPaul BeesleyArm to support `PSCI`_, Secure Monitor for a Trusted OS and SoC specific 27*40d553cfSPaul Beesleysoftware. The common runtime services framework ensures that SMC Functions are 28*40d553cfSPaul Beesleydispatched to their respective service implementation - the `Firmware Design`_ 29*40d553cfSPaul Beesleyprovides details of how this is achieved. 30*40d553cfSPaul Beesley 31*40d553cfSPaul BeesleyThe interface and operation of the runtime services depends heavily on the 32*40d553cfSPaul Beesleyconcepts and definitions described in the `SMCCC`_, in particular SMC Function 33*40d553cfSPaul BeesleyIDs, Owning Entity Numbers (OEN), Fast and Standard calls, and the SMC32 and 34*40d553cfSPaul BeesleySMC64 calling conventions. Please refer to that document for a full explanation 35*40d553cfSPaul Beesleyof these terms. 36*40d553cfSPaul Beesley 37*40d553cfSPaul BeesleyOwning Entities, Call Types and Function IDs 38*40d553cfSPaul Beesley-------------------------------------------- 39*40d553cfSPaul Beesley 40*40d553cfSPaul BeesleyThe SMC Function Identifier includes a OEN field. These values and their 41*40d553cfSPaul Beesleymeaning are described in `SMCCC`_ and summarized in table 1 below. Some entities 42*40d553cfSPaul Beesleyare allocated a range of of OENs. The OEN must be interpreted in conjunction 43*40d553cfSPaul Beesleywith the SMC call type, which is either *Fast* or *Yielding*. Fast calls are 44*40d553cfSPaul Beesleyuninterruptible whereas Yielding calls can be pre-empted. The majority of 45*40d553cfSPaul BeesleyOwning Entities only have allocated ranges for Fast calls: Yielding calls are 46*40d553cfSPaul Beesleyreserved exclusively for Trusted OS providers or for interoperability with 47*40d553cfSPaul Beesleylegacy 32-bit software that predates the `SMCCC`_. 48*40d553cfSPaul Beesley 49*40d553cfSPaul Beesley:: 50*40d553cfSPaul Beesley 51*40d553cfSPaul Beesley Type OEN Service 52*40d553cfSPaul Beesley Fast 0 Arm Architecture calls 53*40d553cfSPaul Beesley Fast 1 CPU Service calls 54*40d553cfSPaul Beesley Fast 2 SiP Service calls 55*40d553cfSPaul Beesley Fast 3 OEM Service calls 56*40d553cfSPaul Beesley Fast 4 Standard Service calls 57*40d553cfSPaul Beesley Fast 5-47 Reserved for future use 58*40d553cfSPaul Beesley Fast 48-49 Trusted Application calls 59*40d553cfSPaul Beesley Fast 50-63 Trusted OS calls 60*40d553cfSPaul Beesley 61*40d553cfSPaul Beesley Yielding 0- 1 Reserved for existing Armv7-A calls 62*40d553cfSPaul Beesley Yielding 2-63 Trusted OS Standard Calls 63*40d553cfSPaul Beesley 64*40d553cfSPaul Beesley*Table 1: Service types and their corresponding Owning Entity Numbers* 65*40d553cfSPaul Beesley 66*40d553cfSPaul BeesleyEach individual entity can allocate the valid identifiers within the entity 67*40d553cfSPaul Beesleyrange as they need - it is not necessary to coordinate with other entities of 68*40d553cfSPaul Beesleythe same type. For example, two SoC providers can use the same Function ID 69*40d553cfSPaul Beesleywithin the SiP Service calls OEN range to mean different things - as these 70*40d553cfSPaul Beesleycalls should be specific to the SoC. The Standard Runtime Calls OEN is used for 71*40d553cfSPaul Beesleyservices defined by Arm standards, such as `PSCI`_. 72*40d553cfSPaul Beesley 73*40d553cfSPaul BeesleyThe SMC Function ID also indicates whether the call has followed the SMC32 74*40d553cfSPaul Beesleycalling convention, where all parameters are 32-bit, or the SMC64 calling 75*40d553cfSPaul Beesleyconvention, where the parameters are 64-bit. The framework identifies and 76*40d553cfSPaul Beesleyrejects invalid calls that use the SMC64 calling convention but that originate 77*40d553cfSPaul Beesleyfrom an AArch32 caller. 78*40d553cfSPaul Beesley 79*40d553cfSPaul BeesleyThe EL3 runtime services framework uses the call type and OEN to identify a 80*40d553cfSPaul Beesleyspecific handler for each SMC call, but it is expected that an individual 81*40d553cfSPaul Beesleyhandler will be responsible for all SMC Functions within a given service type. 82*40d553cfSPaul Beesley 83*40d553cfSPaul BeesleyGetting started 84*40d553cfSPaul Beesley--------------- 85*40d553cfSPaul Beesley 86*40d553cfSPaul BeesleyTF-A has a `services`_ directory in the source tree under which 87*40d553cfSPaul Beesleyeach owning entity can place the implementation of its runtime service. The 88*40d553cfSPaul Beesley`PSCI`_ implementation is located here in the `lib/psci`_ directory. 89*40d553cfSPaul Beesley 90*40d553cfSPaul BeesleyRuntime service sources will need to include the `runtime_svc.h`_ header file. 91*40d553cfSPaul Beesley 92*40d553cfSPaul BeesleyRegistering a runtime service 93*40d553cfSPaul Beesley----------------------------- 94*40d553cfSPaul Beesley 95*40d553cfSPaul BeesleyA runtime service is registered using the ``DECLARE_RT_SVC()`` macro, specifying 96*40d553cfSPaul Beesleythe name of the service, the range of OENs covered, the type of service and 97*40d553cfSPaul Beesleyinitialization and call handler functions. 98*40d553cfSPaul Beesley 99*40d553cfSPaul Beesley:: 100*40d553cfSPaul Beesley 101*40d553cfSPaul Beesley #define DECLARE_RT_SVC(_name, _start, _end, _type, _setup, _smch) 102*40d553cfSPaul Beesley 103*40d553cfSPaul Beesley- ``_name`` is used to identify the data structure declared by this macro, and 104*40d553cfSPaul Beesley is also used for diagnostic purposes 105*40d553cfSPaul Beesley 106*40d553cfSPaul Beesley- ``_start`` and ``_end`` values must be based on the ``OEN_*`` values defined in 107*40d553cfSPaul Beesley `smccc.h`_ 108*40d553cfSPaul Beesley 109*40d553cfSPaul Beesley- ``_type`` must be one of ``SMC_TYPE_FAST`` or ``SMC_TYPE_YIELD`` 110*40d553cfSPaul Beesley 111*40d553cfSPaul Beesley- ``_setup`` is the initialization function with the ``rt_svc_init`` signature: 112*40d553cfSPaul Beesley 113*40d553cfSPaul Beesley .. code:: c 114*40d553cfSPaul Beesley 115*40d553cfSPaul Beesley typedef int32_t (*rt_svc_init)(void); 116*40d553cfSPaul Beesley 117*40d553cfSPaul Beesley- ``_smch`` is the SMC handler function with the ``rt_svc_handle`` signature: 118*40d553cfSPaul Beesley 119*40d553cfSPaul Beesley .. code:: c 120*40d553cfSPaul Beesley 121*40d553cfSPaul Beesley typedef uintptr_t (*rt_svc_handle_t)(uint32_t smc_fid, 122*40d553cfSPaul Beesley u_register_t x1, u_register_t x2, 123*40d553cfSPaul Beesley u_register_t x3, u_register_t x4, 124*40d553cfSPaul Beesley void *cookie, 125*40d553cfSPaul Beesley void *handle, 126*40d553cfSPaul Beesley u_register_t flags); 127*40d553cfSPaul Beesley 128*40d553cfSPaul BeesleyDetails of the requirements and behavior of the two callbacks is provided in 129*40d553cfSPaul Beesleythe following sections. 130*40d553cfSPaul Beesley 131*40d553cfSPaul BeesleyDuring initialization the services framework validates each declared service 132*40d553cfSPaul Beesleyto ensure that the following conditions are met: 133*40d553cfSPaul Beesley 134*40d553cfSPaul Beesley#. The ``_start`` OEN is not greater than the ``_end`` OEN 135*40d553cfSPaul Beesley#. The ``_end`` OEN does not exceed the maximum OEN value (63) 136*40d553cfSPaul Beesley#. The ``_type`` is one of ``SMC_TYPE_FAST`` or ``SMC_TYPE_YIELD`` 137*40d553cfSPaul Beesley#. ``_setup`` and ``_smch`` routines have been specified 138*40d553cfSPaul Beesley 139*40d553cfSPaul Beesley`std_svc_setup.c`_ provides an example of registering a runtime service: 140*40d553cfSPaul Beesley 141*40d553cfSPaul Beesley.. code:: c 142*40d553cfSPaul Beesley 143*40d553cfSPaul Beesley /* Register Standard Service Calls as runtime service */ 144*40d553cfSPaul Beesley DECLARE_RT_SVC( 145*40d553cfSPaul Beesley std_svc, 146*40d553cfSPaul Beesley OEN_STD_START, 147*40d553cfSPaul Beesley OEN_STD_END, 148*40d553cfSPaul Beesley SMC_TYPE_FAST, 149*40d553cfSPaul Beesley std_svc_setup, 150*40d553cfSPaul Beesley std_svc_smc_handler 151*40d553cfSPaul Beesley ); 152*40d553cfSPaul Beesley 153*40d553cfSPaul BeesleyInitializing a runtime service 154*40d553cfSPaul Beesley------------------------------ 155*40d553cfSPaul Beesley 156*40d553cfSPaul BeesleyRuntime services are initialized once, during cold boot, by the primary CPU 157*40d553cfSPaul Beesleyafter platform and architectural initialization is complete. The framework 158*40d553cfSPaul Beesleyperforms basic validation of the declared service before calling 159*40d553cfSPaul Beesleythe service initialization function (``_setup`` in the declaration). This 160*40d553cfSPaul Beesleyfunction must carry out any essential EL3 initialization prior to receiving a 161*40d553cfSPaul BeesleySMC Function call via the handler function. 162*40d553cfSPaul Beesley 163*40d553cfSPaul BeesleyOn success, the initialization function must return ``0``. Any other return value 164*40d553cfSPaul Beesleywill cause the framework to issue a diagnostic: 165*40d553cfSPaul Beesley 166*40d553cfSPaul Beesley:: 167*40d553cfSPaul Beesley 168*40d553cfSPaul Beesley Error initializing runtime service <name of the service> 169*40d553cfSPaul Beesley 170*40d553cfSPaul Beesleyand then ignore the service - the system will continue to boot but SMC calls 171*40d553cfSPaul Beesleywill not be passed to the service handler and instead return the *Unknown SMC 172*40d553cfSPaul BeesleyFunction ID* result ``0xFFFFFFFF``. 173*40d553cfSPaul Beesley 174*40d553cfSPaul BeesleyIf the system must not be allowed to proceed without the service, the 175*40d553cfSPaul Beesleyinitialization function must itself cause the firmware boot to be halted. 176*40d553cfSPaul Beesley 177*40d553cfSPaul BeesleyIf the service uses per-CPU data this must either be initialized for all CPUs 178*40d553cfSPaul Beesleyduring this call, or be done lazily when a CPU first issues an SMC call to that 179*40d553cfSPaul Beesleyservice. 180*40d553cfSPaul Beesley 181*40d553cfSPaul BeesleyHandling runtime service requests 182*40d553cfSPaul Beesley--------------------------------- 183*40d553cfSPaul Beesley 184*40d553cfSPaul BeesleySMC calls for a service are forwarded by the framework to the service's SMC 185*40d553cfSPaul Beesleyhandler function (``_smch`` in the service declaration). This function must have 186*40d553cfSPaul Beesleythe following signature: 187*40d553cfSPaul Beesley 188*40d553cfSPaul Beesley.. code:: c 189*40d553cfSPaul Beesley 190*40d553cfSPaul Beesley typedef uintptr_t (*rt_svc_handle_t)(uint32_t smc_fid, 191*40d553cfSPaul Beesley u_register_t x1, u_register_t x2, 192*40d553cfSPaul Beesley u_register_t x3, u_register_t x4, 193*40d553cfSPaul Beesley void *cookie, 194*40d553cfSPaul Beesley void *handle, 195*40d553cfSPaul Beesley u_register_t flags); 196*40d553cfSPaul Beesley 197*40d553cfSPaul BeesleyThe handler is responsible for: 198*40d553cfSPaul Beesley 199*40d553cfSPaul Beesley#. Determining that ``smc_fid`` is a valid and supported SMC Function ID, 200*40d553cfSPaul Beesley otherwise completing the request with the *Unknown SMC Function ID*: 201*40d553cfSPaul Beesley 202*40d553cfSPaul Beesley .. code:: c 203*40d553cfSPaul Beesley 204*40d553cfSPaul Beesley SMC_RET1(handle, SMC_UNK); 205*40d553cfSPaul Beesley 206*40d553cfSPaul Beesley#. Determining if the requested function is valid for the calling security 207*40d553cfSPaul Beesley state. SMC Calls can be made from both the normal and trusted worlds and 208*40d553cfSPaul Beesley the framework will forward all calls to the service handler. 209*40d553cfSPaul Beesley 210*40d553cfSPaul Beesley The ``flags`` parameter to this function indicates the caller security state 211*40d553cfSPaul Beesley in bit[0], where a value of ``1`` indicates a non-secure caller. The 212*40d553cfSPaul Beesley ``is_caller_secure(flags)`` and ``is_caller_non_secure(flags)`` can be used to 213*40d553cfSPaul Beesley test this condition. 214*40d553cfSPaul Beesley 215*40d553cfSPaul Beesley If invalid, the request should be completed with: 216*40d553cfSPaul Beesley 217*40d553cfSPaul Beesley .. code:: c 218*40d553cfSPaul Beesley 219*40d553cfSPaul Beesley SMC_RET1(handle, SMC_UNK); 220*40d553cfSPaul Beesley 221*40d553cfSPaul Beesley#. Truncating parameters for calls made using the SMC32 calling convention. 222*40d553cfSPaul Beesley Such calls can be determined by checking the CC field in bit[30] of the 223*40d553cfSPaul Beesley ``smc_fid`` parameter, for example by using: 224*40d553cfSPaul Beesley 225*40d553cfSPaul Beesley :: 226*40d553cfSPaul Beesley 227*40d553cfSPaul Beesley if (GET_SMC_CC(smc_fid) == SMC_32) ... 228*40d553cfSPaul Beesley 229*40d553cfSPaul Beesley For such calls, the upper bits of the parameters x1-x4 and the saved 230*40d553cfSPaul Beesley parameters X5-X7 are UNDEFINED and must be explicitly ignored by the 231*40d553cfSPaul Beesley handler. This can be done by truncating the values to a suitable 32-bit 232*40d553cfSPaul Beesley integer type before use, for example by ensuring that functions defined 233*40d553cfSPaul Beesley to handle individual SMC Functions use appropriate 32-bit parameters. 234*40d553cfSPaul Beesley 235*40d553cfSPaul Beesley#. Providing the service requested by the SMC Function, utilizing the 236*40d553cfSPaul Beesley immediate parameters x1-x4 and/or the additional saved parameters X5-X7. 237*40d553cfSPaul Beesley The latter can be retrieved using the ``SMC_GET_GP(handle, ref)`` function, 238*40d553cfSPaul Beesley supplying the appropriate ``CTX_GPREG_Xn`` reference, e.g. 239*40d553cfSPaul Beesley 240*40d553cfSPaul Beesley .. code:: c 241*40d553cfSPaul Beesley 242*40d553cfSPaul Beesley uint64_t x6 = SMC_GET_GP(handle, CTX_GPREG_X6); 243*40d553cfSPaul Beesley 244*40d553cfSPaul Beesley#. Implementing the standard SMC32 Functions that provide information about 245*40d553cfSPaul Beesley the implementation of the service. These are the Call Count, Implementor 246*40d553cfSPaul Beesley UID and Revision Details for each service documented in section 6 of the 247*40d553cfSPaul Beesley `SMCCC`_. 248*40d553cfSPaul Beesley 249*40d553cfSPaul Beesley TF-A expects owning entities to follow this recommendation. 250*40d553cfSPaul Beesley 251*40d553cfSPaul Beesley#. Returning the result to the caller. The `SMCCC`_ allows for up to 256 bits 252*40d553cfSPaul Beesley of return value in SMC64 using X0-X3 and 128 bits in SMC32 using W0-W3. The 253*40d553cfSPaul Beesley framework provides a family of macros to set the multi-register return 254*40d553cfSPaul Beesley value and complete the handler: 255*40d553cfSPaul Beesley 256*40d553cfSPaul Beesley .. code:: c 257*40d553cfSPaul Beesley 258*40d553cfSPaul Beesley SMC_RET1(handle, x0); 259*40d553cfSPaul Beesley SMC_RET2(handle, x0, x1); 260*40d553cfSPaul Beesley SMC_RET3(handle, x0, x1, x2); 261*40d553cfSPaul Beesley SMC_RET4(handle, x0, x1, x2, x3); 262*40d553cfSPaul Beesley 263*40d553cfSPaul BeesleyThe ``cookie`` parameter to the handler is reserved for future use and can be 264*40d553cfSPaul Beesleyignored. The ``handle`` is returned by the SMC handler - completion of the 265*40d553cfSPaul Beesleyhandler function must always be via one of the ``SMC_RETn()`` macros. 266*40d553cfSPaul Beesley 267*40d553cfSPaul BeesleyNOTE: The PSCI and Test Secure-EL1 Payload Dispatcher services do not follow 268*40d553cfSPaul Beesleyall of the above requirements yet. 269*40d553cfSPaul Beesley 270*40d553cfSPaul BeesleyServices that contain multiple sub-services 271*40d553cfSPaul Beesley------------------------------------------- 272*40d553cfSPaul Beesley 273*40d553cfSPaul BeesleyIt is possible that a single owning entity implements multiple sub-services. For 274*40d553cfSPaul Beesleyexample, the Standard calls service handles ``0x84000000``-``0x8400FFFF`` and 275*40d553cfSPaul Beesley``0xC4000000``-``0xC400FFFF`` functions. Within that range, the `PSCI`_ service 276*40d553cfSPaul Beesleyhandles the ``0x84000000``-``0x8400001F`` and ``0xC4000000``-``0xC400001F`` functions. 277*40d553cfSPaul BeesleyIn that respect, `PSCI`_ is a 'sub-service' of the Standard calls service. In 278*40d553cfSPaul Beesleyfuture, there could be additional such sub-services in the Standard calls 279*40d553cfSPaul Beesleyservice which perform independent functions. 280*40d553cfSPaul Beesley 281*40d553cfSPaul BeesleyIn this situation it may be valuable to introduce a second level framework to 282*40d553cfSPaul Beesleyenable independent implementation of sub-services. Such a framework might look 283*40d553cfSPaul Beesleyvery similar to the current runtime services framework, but using a different 284*40d553cfSPaul Beesleypart of the SMC Function ID to identify the sub-service. TF-A does not provide 285*40d553cfSPaul Beesleysuch a framework at present. 286*40d553cfSPaul Beesley 287*40d553cfSPaul BeesleySecure-EL1 Payload Dispatcher service (SPD) 288*40d553cfSPaul Beesley------------------------------------------- 289*40d553cfSPaul Beesley 290*40d553cfSPaul BeesleyServices that handle SMC Functions targeting a Trusted OS, Trusted Application, 291*40d553cfSPaul Beesleyor other Secure-EL1 Payload are special. These services need to manage the 292*40d553cfSPaul BeesleySecure-EL1 context, provide the *Secure Monitor* functionality of switching 293*40d553cfSPaul Beesleybetween the normal and secure worlds, deliver SMC Calls through to Secure-EL1 294*40d553cfSPaul Beesleyand generally manage the Secure-EL1 Payload through CPU power-state transitions. 295*40d553cfSPaul Beesley 296*40d553cfSPaul BeesleyTODO: Provide details of the additional work required to implement a SPD and 297*40d553cfSPaul Beesleythe BL31 support for these services. Or a reference to the document that will 298*40d553cfSPaul Beesleyprovide this information.... 299*40d553cfSPaul Beesley 300*40d553cfSPaul Beesley-------------- 301*40d553cfSPaul Beesley 302*40d553cfSPaul Beesley*Copyright (c) 2014-2018, Arm Limited and Contributors. All rights reserved.* 303*40d553cfSPaul Beesley 304*40d553cfSPaul Beesley.. _SMCCC: http://infocenter.arm.com/help/topic/com.arm.doc.den0028a/index.html 305*40d553cfSPaul Beesley.. _PSCI: http://infocenter.arm.com/help/topic/com.arm.doc.den0022c/DEN0022C_Power_State_Coordination_Interface.pdf 306*40d553cfSPaul Beesley.. _Firmware Design: ../designb_documents/firmware-design.rst 307*40d553cfSPaul Beesley.. _services: ../../services 308*40d553cfSPaul Beesley.. _lib/psci: ../../lib/psci 309*40d553cfSPaul Beesley.. _runtime_svc.h: ../../include/common/runtime_svc.h 310*40d553cfSPaul Beesley.. _smccc.h: ../../include/lib/smccc.h 311*40d553cfSPaul Beesley.. _std_svc_setup.c: ../../services/std_svc/std_svc_setup.c 312