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