1*4efd2193SJayanth Dodderi ChidanandContext Management Library 2*4efd2193SJayanth Dodderi Chidanand************************** 3*4efd2193SJayanth Dodderi Chidanand 4*4efd2193SJayanth Dodderi ChidanandThis document provides an overview of the Context Management library implementation 5*4efd2193SJayanth Dodderi Chidanandin Trusted Firmware-A (TF-A). It enumerates and describes the APIs implemented 6*4efd2193SJayanth Dodderi Chidanandand their accessibility from other components at EL3. 7*4efd2193SJayanth Dodderi Chidanand 8*4efd2193SJayanth Dodderi ChidanandOverview 9*4efd2193SJayanth Dodderi Chidanand======== 10*4efd2193SJayanth Dodderi Chidanand 11*4efd2193SJayanth Dodderi ChidanandArm TrustZone architecture facilitates hardware-enforced isolation between 12*4efd2193SJayanth Dodderi Chidanandsoftware running in various security states (Secure/Non-Secure/Realm). 13*4efd2193SJayanth Dodderi ChidanandThe general-purpose registers, most of the system registers and vector registers 14*4efd2193SJayanth Dodderi Chidanandare not banked per world. When moving between the security states it is the 15*4efd2193SJayanth Dodderi Chidanandresponsibility of the secure monitor software (BL31(AArch64) / BL32(Aarch32)) 16*4efd2193SJayanth Dodderi Chidanandin TF-A, not the hardware, to save and restore register state. 17*4efd2193SJayanth Dodderi ChidanandRefer to `Trustzone for AArch64`_ for more details. 18*4efd2193SJayanth Dodderi Chidanand 19*4efd2193SJayanth Dodderi ChidanandEL3 Runtime Firmware, also termed as secure monitor firmware, is integrated 20*4efd2193SJayanth Dodderi Chidanandwith a context management library to handle the context of the CPU, managing the 21*4efd2193SJayanth Dodderi Chidanandsaving and restoring of register states across the worlds. 22*4efd2193SJayanth Dodderi Chidanand 23*4efd2193SJayanth Dodderi ChidanandTF-A Context 24*4efd2193SJayanth Dodderi Chidanand============ 25*4efd2193SJayanth Dodderi Chidanand 26*4efd2193SJayanth Dodderi ChidanandIn TF-A, the context is represented as a data structure used by the EL3 firmware 27*4efd2193SJayanth Dodderi Chidanandto preserve the state of the CPU at the next lower exception level (EL) in a given 28*4efd2193SJayanth Dodderi Chidanandsecurity state and save enough EL3 metadata to be able to return to that exception 29*4efd2193SJayanth Dodderi Chidanandlevel and security state. The memory for the context data structures are allocated 30*4efd2193SJayanth Dodderi Chidanandin BSS section of EL3 firmware. 31*4efd2193SJayanth Dodderi Chidanand 32*4efd2193SJayanth Dodderi ChidanandIn a trusted system at any instance, a given CPU could be executing in one of the 33*4efd2193SJayanth Dodderi Chidanandsecurity states (Non-Secure, Secure, Realm). Each world must have its 34*4efd2193SJayanth Dodderi Chidanandconfiguration of system registers independent of other security states to access 35*4efd2193SJayanth Dodderi Chidanandand execute any of the architectural features. 36*4efd2193SJayanth Dodderi Chidanand 37*4efd2193SJayanth Dodderi ChidanandIf the CPU switches across security states (for example: from Non-secure to Secure 38*4efd2193SJayanth Dodderi Chidanandor vice versa), the register contents, especially the ones that are not banked 39*4efd2193SJayanth Dodderi Chidanand(EL2/EL1, vector, general-purpose registers), will be overwritten, as the software 40*4efd2193SJayanth Dodderi Chidanandrunning in either state has the privileges to access them. Additionally, some of 41*4efd2193SJayanth Dodderi Chidanandthe architectural features enabled in the former security state will be unconditionally 42*4efd2193SJayanth Dodderi Chidanandaccessible in the latter security state as well. This can be a major concern when 43*4efd2193SJayanth Dodderi Chidananddealing with security-specific bits, as they need to be explicitly enabled or 44*4efd2193SJayanth Dodderi Chidananddisabled in each state to prevent data leakage across the worlds. 45*4efd2193SJayanth Dodderi Chidanand 46*4efd2193SJayanth Dodderi ChidanandIn general, an ideal trusted system should have Secure world-specific configurations 47*4efd2193SJayanth Dodderi Chidanandthat are not influenced by Normal World operations. Therefore, for each CPU, we 48*4efd2193SJayanth Dodderi Chidanandneed to maintain world-specific context to ensure that register entries from one 49*4efd2193SJayanth Dodderi Chidanandworld do not leak or impact the execution of the CPU in other worlds. 50*4efd2193SJayanth Dodderi ChidanandThis will help ensure the integrity and security of the system, preventing any 51*4efd2193SJayanth Dodderi Chidanandunauthorized access or data corruption between the different security states. 52*4efd2193SJayanth Dodderi Chidanand 53*4efd2193SJayanth Dodderi ChidanandDesign 54*4efd2193SJayanth Dodderi Chidanand====== 55*4efd2193SJayanth Dodderi Chidanand 56*4efd2193SJayanth Dodderi ChidanandThe Context Management library in TF-A is designed to cover all the requirements 57*4efd2193SJayanth Dodderi Chidanandfor maintaining world-specific context essential for a trusted system. 58*4efd2193SJayanth Dodderi ChidanandThis includes implementing CPU context initialization and management routines, 59*4efd2193SJayanth Dodderi Chidanandas well as other helper APIs that are required by dispatcher components in EL3 60*4efd2193SJayanth Dodderi Chidanandfirmware, which are collectively referred to as CPU Context Management. 61*4efd2193SJayanth Dodderi ChidanandThe APIs and their usecases are listed in detail under the :ref:`Library APIs` 62*4efd2193SJayanth Dodderi Chidanandsection. 63*4efd2193SJayanth Dodderi Chidanand 64*4efd2193SJayanth Dodderi ChidanandOriginally, the Context Management library in TF-A was designed to cater for a 65*4efd2193SJayanth Dodderi Chidanandtwo-world system, comprising of Non-Secure and Secure Worlds. In this case, the 66*4efd2193SJayanth Dodderi ChidanandEL3 Firmware is assumed to be running in Secure World. 67*4efd2193SJayanth Dodderi ChidanandWith introduction of Realm Management Extension (RME), from Armv9.2 a system 68*4efd2193SJayanth Dodderi Chidanandcan have four distinct worlds (Non-Secure, Secure, Realm, Root). 69*4efd2193SJayanth Dodderi ChidanandRME isolates EL3 from all other Security states and moves it into its own security 70*4efd2193SJayanth Dodderi Chidanandstate called root. EL3 firmware now runs at Root World and thereby is 71*4efd2193SJayanth Dodderi Chidanandtrusted from software in Non-secure, Secure, and Realm states. 72*4efd2193SJayanth Dodderi ChidanandRefer to `Security States with RME`_ for more details. 73*4efd2193SJayanth Dodderi Chidanand 74*4efd2193SJayanth Dodderi ChidanandKey principles followed in designing the context management library : 75*4efd2193SJayanth Dodderi Chidanand 76*4efd2193SJayanth Dodderi Chidanand1. **EL3 should only initialize immediate used lower EL** 77*4efd2193SJayanth Dodderi Chidanand 78*4efd2193SJayanth Dodderi ChidanandContext Management library running at EL3 should only initialize and monitor the 79*4efd2193SJayanth Dodderi Chidanandimmediate used lower EL. This implies that, when S-EL2 is present in the system, 80*4efd2193SJayanth Dodderi ChidanandEL3 should initialise and monitor S-EL2 registers only. S-EL1 registers should 81*4efd2193SJayanth Dodderi Chidanandnot be the concern of EL3 while S-EL2 is in place. In systems where S-EL2 is 82*4efd2193SJayanth Dodderi Chidanandabsent, S-EL1 registers should be initialised from EL3. 83*4efd2193SJayanth Dodderi Chidanand 84*4efd2193SJayanth Dodderi Chidanand2. **Decentralized model for context management** 85*4efd2193SJayanth Dodderi Chidanand 86*4efd2193SJayanth Dodderi ChidanandEach world (Non-Secure, Secure, and Realm) should have their separate component 87*4efd2193SJayanth Dodderi Chidanandin EL3 responsible for their respective world context management. 88*4efd2193SJayanth Dodderi ChidanandBoth the Secure and Realm world have associated dispatcher components in EL3 89*4efd2193SJayanth Dodderi Chidanandfirmware to allow management of the respective worlds. For the Non-Secure world, 90*4efd2193SJayanth Dodderi ChidanandPSCI Library (BL31)/context management library provides routines to help 91*4efd2193SJayanth Dodderi Chidanandinitialize the Non-Secure world context. 92*4efd2193SJayanth Dodderi Chidanand 93*4efd2193SJayanth Dodderi Chidanand3. **Flexibility for Dispatchers to select desired feature set to save and restore** 94*4efd2193SJayanth Dodderi Chidanand 95*4efd2193SJayanth Dodderi ChidanandEach feature is supported with a helper function ``is_feature_supported(void)``, 96*4efd2193SJayanth Dodderi Chidanandto detect its presence at runtime. This helps dispatchers to select the desired 97*4efd2193SJayanth Dodderi Chidanandfeature set, and thereby save and restore the configuration associated with them. 98*4efd2193SJayanth Dodderi Chidanand 99*4efd2193SJayanth Dodderi Chidanand4. **Dynamic discovery of Feature enablement by EL3** 100*4efd2193SJayanth Dodderi Chidanand 101*4efd2193SJayanth Dodderi ChidanandTF-A supports three states for feature enablement at EL3, to make them available 102*4efd2193SJayanth Dodderi Chidanandfor lower exception levels. 103*4efd2193SJayanth Dodderi Chidanand 104*4efd2193SJayanth Dodderi Chidanand.. code:: c 105*4efd2193SJayanth Dodderi Chidanand 106*4efd2193SJayanth Dodderi Chidanand #define FEAT_STATE_DISABLED 0 107*4efd2193SJayanth Dodderi Chidanand #define FEAT_STATE_ENABLED 1 108*4efd2193SJayanth Dodderi Chidanand #define FEAT_STATE_CHECK 2 109*4efd2193SJayanth Dodderi Chidanand 110*4efd2193SJayanth Dodderi ChidanandA pattern is established for feature enablement behavior. 111*4efd2193SJayanth Dodderi ChidanandEach feature must support the 3 possible values with rigid semantics. 112*4efd2193SJayanth Dodderi Chidanand 113*4efd2193SJayanth Dodderi Chidanand- **FEAT_STATE_DISABLED** - all code relating to this feature is always skipped. 114*4efd2193SJayanth Dodderi Chidanand Firmware is unaware of this feature. 115*4efd2193SJayanth Dodderi Chidanand 116*4efd2193SJayanth Dodderi Chidanand- **FEAT_STATE_ALWAYS** - all code relating to this feature is always executed. 117*4efd2193SJayanth Dodderi Chidanand Firmware expects this feature to be present in hardware. 118*4efd2193SJayanth Dodderi Chidanand 119*4efd2193SJayanth Dodderi Chidanand- **FEAT_STATE_CHECK** - same as ``FEAT_STATE_ALWAYS`` except that the feature's 120*4efd2193SJayanth Dodderi Chidanand existence will be checked at runtime. Default on dynamic platforms (example: FVP). 121*4efd2193SJayanth Dodderi Chidanand 122*4efd2193SJayanth Dodderi Chidanand.. note:: 123*4efd2193SJayanth Dodderi Chidanand ``FEAT_RAS`` is an exception here, as it impacts the execution of EL3 and 124*4efd2193SJayanth Dodderi Chidanand it is essential to know its presence at compile time. Refer to ``ENABLE_FEAT`` 125*4efd2193SJayanth Dodderi Chidanand macro under :ref:`Build Options` section for more details. 126*4efd2193SJayanth Dodderi Chidanand 127*4efd2193SJayanth Dodderi ChidanandCode Structure 128*4efd2193SJayanth Dodderi Chidanand============== 129*4efd2193SJayanth Dodderi Chidanand 130*4efd2193SJayanth Dodderi Chidanand`lib/el3_runtime/(aarch32/aarch64)`_ - Context library code directory. 131*4efd2193SJayanth Dodderi Chidanand 132*4efd2193SJayanth Dodderi ChidanandSource Files 133*4efd2193SJayanth Dodderi Chidanand~~~~~~~~~~~~ 134*4efd2193SJayanth Dodderi Chidanand 135*4efd2193SJayanth Dodderi Chidanand#. ``context_mgmt.c`` : consists of core functions that setup, save and restore 136*4efd2193SJayanth Dodderi Chidanand context for different security states alongside high level feature enablement 137*4efd2193SJayanth Dodderi Chidanand APIs for individual worlds. 138*4efd2193SJayanth Dodderi Chidanand 139*4efd2193SJayanth Dodderi Chidanand#. ``cpu_data_array.c`` : contains per_cpu_data structure instantiation. 140*4efd2193SJayanth Dodderi Chidanand 141*4efd2193SJayanth Dodderi Chidanand#. ``context.S`` : consists of functions that save and restore some of the context 142*4efd2193SJayanth Dodderi Chidanand structure members in assembly code. 143*4efd2193SJayanth Dodderi Chidanand 144*4efd2193SJayanth Dodderi Chidanand#. ``cpu_data.S`` : consists of helper functions to initialise per_cpu_data pointers. 145*4efd2193SJayanth Dodderi Chidanand 146*4efd2193SJayanth Dodderi Chidanand#. ``el3_common_macros.S`` : consists of macros to facilitate actions to be performed 147*4efd2193SJayanth Dodderi Chidanand during cold and warmboot and el3 registers initialisation in assembly code. 148*4efd2193SJayanth Dodderi Chidanand 149*4efd2193SJayanth Dodderi ChidanandHeader Files 150*4efd2193SJayanth Dodderi Chidanand~~~~~~~~~~~~ 151*4efd2193SJayanth Dodderi Chidanand 152*4efd2193SJayanth Dodderi Chidanand#. ``context_mgmt.h`` : contains the public interface to Context Management Library. 153*4efd2193SJayanth Dodderi Chidanand 154*4efd2193SJayanth Dodderi Chidanand#. ``context.h`` : contains the helper macros and definitions for context entries. 155*4efd2193SJayanth Dodderi Chidanand 156*4efd2193SJayanth Dodderi Chidanand#. ``cpu_data.h`` : contains the public interface to Per CPU data structure. 157*4efd2193SJayanth Dodderi Chidanand 158*4efd2193SJayanth Dodderi Chidanand#. ``context_debug.h`` : contains public interface to report context memory 159*4efd2193SJayanth Dodderi Chidanand utilisation across the security states. 160*4efd2193SJayanth Dodderi Chidanand 161*4efd2193SJayanth Dodderi Chidanand#. ``context_el2.h`` : internal header consisting of helper macros to access EL2 162*4efd2193SJayanth Dodderi Chidanand context entries. Used by ``context.h``. 163*4efd2193SJayanth Dodderi Chidanand 164*4efd2193SJayanth Dodderi ChidanandApart from these files, we have some context related source files under ``BL1`` 165*4efd2193SJayanth Dodderi Chidanandand ``BL31`` directory. ``bl1_context_mgmt.c`` ``bl31_context_mgmt.c`` 166*4efd2193SJayanth Dodderi Chidanand 167*4efd2193SJayanth Dodderi ChidanandBootloader Images utilizing Context Management Library 168*4efd2193SJayanth Dodderi Chidanand====================================================== 169*4efd2193SJayanth Dodderi Chidanand 170*4efd2193SJayanth Dodderi Chidanand+-------------------------------------------+-----------------------------+ 171*4efd2193SJayanth Dodderi Chidanand| Bootloader | Context Management Library | 172*4efd2193SJayanth Dodderi Chidanand+-------------------------------------------+-----------------------------+ 173*4efd2193SJayanth Dodderi Chidanand| BL1 | Yes | 174*4efd2193SJayanth Dodderi Chidanand+-------------------------------------------+-----------------------------+ 175*4efd2193SJayanth Dodderi Chidanand| BL2 | No | 176*4efd2193SJayanth Dodderi Chidanand+-------------------------------------------+-----------------------------+ 177*4efd2193SJayanth Dodderi Chidanand| BL31 (Aarch64- EL3runtime firmware) | Yes | 178*4efd2193SJayanth Dodderi Chidanand+-------------------------------------------+-----------------------------+ 179*4efd2193SJayanth Dodderi Chidanand| BL32 (Aarch32- EL3runtime firmware) | Yes | 180*4efd2193SJayanth Dodderi Chidanand+-------------------------------------------+-----------------------------+ 181*4efd2193SJayanth Dodderi Chidanand 182*4efd2193SJayanth Dodderi ChidanandCPU Data Structure 183*4efd2193SJayanth Dodderi Chidanand================== 184*4efd2193SJayanth Dodderi ChidanandFor a given system, depending on the CPU count, the platform statically 185*4efd2193SJayanth Dodderi Chidanandallocates memory for the CPU data structure. 186*4efd2193SJayanth Dodderi Chidanand 187*4efd2193SJayanth Dodderi Chidanand.. code:: c 188*4efd2193SJayanth Dodderi Chidanand 189*4efd2193SJayanth Dodderi Chidanand /* The per_cpu_ptr_cache_t space allocation */ 190*4efd2193SJayanth Dodderi Chidanand cpu_data_t percpu_data[PLATFORM_CORE_COUNT]; 191*4efd2193SJayanth Dodderi Chidanand 192*4efd2193SJayanth Dodderi ChidanandThis CPU data structure has a member element with an array of pointers to hold 193*4efd2193SJayanth Dodderi Chidanandthe Non-Secure, Realm and Secure security state context structures as listed below. 194*4efd2193SJayanth Dodderi Chidanand 195*4efd2193SJayanth Dodderi Chidanand.. code:: c 196*4efd2193SJayanth Dodderi Chidanand 197*4efd2193SJayanth Dodderi Chidanand typedef struct cpu_data { 198*4efd2193SJayanth Dodderi Chidanand #ifdef __aarch64__ 199*4efd2193SJayanth Dodderi Chidanand void *cpu_context[CPU_DATA_CONTEXT_NUM]; 200*4efd2193SJayanth Dodderi Chidanand #endif 201*4efd2193SJayanth Dodderi Chidanand 202*4efd2193SJayanth Dodderi Chidanand .... 203*4efd2193SJayanth Dodderi Chidanand .... 204*4efd2193SJayanth Dodderi Chidanand 205*4efd2193SJayanth Dodderi Chidanand }cpu_data_t; 206*4efd2193SJayanth Dodderi Chidanand 207*4efd2193SJayanth Dodderi Chidanand|CPU Data Structure| 208*4efd2193SJayanth Dodderi Chidanand 209*4efd2193SJayanth Dodderi ChidanandAt runtime, ``cpu_context[CPU_DATA_CONTEXT_NUM]`` array will be intitialised with 210*4efd2193SJayanth Dodderi Chidanandthe Secure, Non-Secure and Realm context structure addresses to ensure proper 211*4efd2193SJayanth Dodderi Chidanandhandling of the register state. 212*4efd2193SJayanth Dodderi ChidanandSee :ref:`Library APIs` section for more details. 213*4efd2193SJayanth Dodderi Chidanand 214*4efd2193SJayanth Dodderi ChidanandCPU Context and Memory allocation 215*4efd2193SJayanth Dodderi Chidanand================================= 216*4efd2193SJayanth Dodderi Chidanand 217*4efd2193SJayanth Dodderi ChidanandCPU Context 218*4efd2193SJayanth Dodderi Chidanand~~~~~~~~~~~ 219*4efd2193SJayanth Dodderi ChidanandThe members of the context structure used by the EL3 firmware to preserve the 220*4efd2193SJayanth Dodderi Chidanandstate of CPU across exception levels for a given security state are listed below. 221*4efd2193SJayanth Dodderi Chidanand 222*4efd2193SJayanth Dodderi Chidanand.. code:: c 223*4efd2193SJayanth Dodderi Chidanand 224*4efd2193SJayanth Dodderi Chidanand typedef struct cpu_context { 225*4efd2193SJayanth Dodderi Chidanand gp_regs_t gpregs_ctx; 226*4efd2193SJayanth Dodderi Chidanand el3_state_t el3state_ctx; 227*4efd2193SJayanth Dodderi Chidanand el1_sysregs_t el1_sysregs_ctx; 228*4efd2193SJayanth Dodderi Chidanand 229*4efd2193SJayanth Dodderi Chidanand #if CTX_INCLUDE_EL2_REGS 230*4efd2193SJayanth Dodderi Chidanand el2_sysregs_t el2_sysregs_ctx; 231*4efd2193SJayanth Dodderi Chidanand #endif 232*4efd2193SJayanth Dodderi Chidanand 233*4efd2193SJayanth Dodderi Chidanand #if CTX_INCLUDE_FPREGS 234*4efd2193SJayanth Dodderi Chidanand fp_regs_t fpregs_ctx; 235*4efd2193SJayanth Dodderi Chidanand #endif 236*4efd2193SJayanth Dodderi Chidanand 237*4efd2193SJayanth Dodderi Chidanand cve_2018_3639_t cve_2018_3639_ctx; 238*4efd2193SJayanth Dodderi Chidanand #if CTX_INCLUDE_PAUTH_REGS 239*4efd2193SJayanth Dodderi Chidanand pauth_t pauth_ctx; 240*4efd2193SJayanth Dodderi Chidanand #endif 241*4efd2193SJayanth Dodderi Chidanand 242*4efd2193SJayanth Dodderi Chidanand #if CTX_INCLUDE_MPAM_REGS 243*4efd2193SJayanth Dodderi Chidanand mpam_t mpam_ctx; 244*4efd2193SJayanth Dodderi Chidanand #endif 245*4efd2193SJayanth Dodderi Chidanand 246*4efd2193SJayanth Dodderi Chidanand } cpu_context_t; 247*4efd2193SJayanth Dodderi Chidanand 248*4efd2193SJayanth Dodderi ChidanandContext Memory Allocation 249*4efd2193SJayanth Dodderi Chidanand~~~~~~~~~~~~~~~~~~~~~~~~~ 250*4efd2193SJayanth Dodderi Chidanand 251*4efd2193SJayanth Dodderi ChidanandCPUs maintain their context per world. The individual context memory allocation 252*4efd2193SJayanth Dodderi Chidanandfor each CPU per world is allocated by the world-specific dispatcher components 253*4efd2193SJayanth Dodderi Chidanandat compile time as shown below. 254*4efd2193SJayanth Dodderi Chidanand 255*4efd2193SJayanth Dodderi Chidanand|Context memory allocation| 256*4efd2193SJayanth Dodderi Chidanand 257*4efd2193SJayanth Dodderi ChidanandNS-Context Memory 258*4efd2193SJayanth Dodderi Chidanand~~~~~~~~~~~~~~~~~ 259*4efd2193SJayanth Dodderi ChidanandIt's important to note that the Normal world doesn't possess the dispatcher 260*4efd2193SJayanth Dodderi Chidanandcomponent found in the Secure and Realm worlds. Instead, the PSCI library at EL3 261*4efd2193SJayanth Dodderi Chidanandhandles memory allocation for ``Non-Secure`` world context for all CPUs. 262*4efd2193SJayanth Dodderi Chidanand 263*4efd2193SJayanth Dodderi Chidanand.. code:: c 264*4efd2193SJayanth Dodderi Chidanand 265*4efd2193SJayanth Dodderi Chidanand static cpu_context_t psci_ns_context[PLATFORM_CORE_COUNT]; 266*4efd2193SJayanth Dodderi Chidanand 267*4efd2193SJayanth Dodderi ChidanandSecure-Context Memory 268*4efd2193SJayanth Dodderi Chidanand~~~~~~~~~~~~~~~~~~~~~ 269*4efd2193SJayanth Dodderi ChidanandSecure World dispatcher (such as SPMD) at EL3 allocates the memory for ``Secure`` 270*4efd2193SJayanth Dodderi Chidanandworld context of all CPUs. 271*4efd2193SJayanth Dodderi Chidanand 272*4efd2193SJayanth Dodderi Chidanand.. code:: c 273*4efd2193SJayanth Dodderi Chidanand 274*4efd2193SJayanth Dodderi Chidanand static spmd_spm_core_context_t spm_core_context[PLATFORM_CORE_COUNT]; 275*4efd2193SJayanth Dodderi Chidanand 276*4efd2193SJayanth Dodderi ChidanandRealm-Context Memory 277*4efd2193SJayanth Dodderi Chidanand~~~~~~~~~~~~~~~~~~~~ 278*4efd2193SJayanth Dodderi ChidanandRealm World dispatcher (RMMD) at EL3 allocates the memory for ``Realm`` world 279*4efd2193SJayanth Dodderi Chidanandcontext of all CPUs. 280*4efd2193SJayanth Dodderi Chidanand 281*4efd2193SJayanth Dodderi Chidanand.. code:: c 282*4efd2193SJayanth Dodderi Chidanand 283*4efd2193SJayanth Dodderi Chidanand rmmd_rmm_context_t rmm_context[PLATFORM_CORE_COUNT]; 284*4efd2193SJayanth Dodderi Chidanand 285*4efd2193SJayanth Dodderi ChidanandTo summarize, the world-specific context structures are synchronized with 286*4efd2193SJayanth Dodderi Chidanandper-CPU data structures, which means that each CPU will have an array of pointers 287*4efd2193SJayanth Dodderi Chidanandto individual worlds. The figure below illustrates the same. 288*4efd2193SJayanth Dodderi Chidanand 289*4efd2193SJayanth Dodderi Chidanand|CPU Context Memory Configuration| 290*4efd2193SJayanth Dodderi Chidanand 291*4efd2193SJayanth Dodderi ChidanandContext Setup/Initialization 292*4efd2193SJayanth Dodderi Chidanand============================ 293*4efd2193SJayanth Dodderi Chidanand 294*4efd2193SJayanth Dodderi ChidanandThe CPU has been assigned context structures for every security state, which include 295*4efd2193SJayanth Dodderi ChidanandNon-Secure, Secure and Realm. It is crucial to initialize each of these structures 296*4efd2193SJayanth Dodderi Chidanandduring the bootup of every CPU before they enter any security state for the 297*4efd2193SJayanth Dodderi Chidanandfirst time. This section explains the specifics of how the initialization of 298*4efd2193SJayanth Dodderi Chidanandevery CPU context takes place during both cold and warm boot paths. 299*4efd2193SJayanth Dodderi Chidanand 300*4efd2193SJayanth Dodderi ChidanandContext Setup during Cold boot 301*4efd2193SJayanth Dodderi Chidanand~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 302*4efd2193SJayanth Dodderi ChidanandThe cold boot path is mainly executed by the primary CPU, other than essential 303*4efd2193SJayanth Dodderi ChidanandCPU initialization executed by all CPUs. After executing BL1 and BL2, the Primary 304*4efd2193SJayanth Dodderi ChidanandCPU jumps to the BL31 image for runtime services initialization. 305*4efd2193SJayanth Dodderi ChidanandDuring this process, the per_cpu_data structure gets initialized with statically 306*4efd2193SJayanth Dodderi Chidanandallocated world-specific context memory. 307*4efd2193SJayanth Dodderi Chidanand 308*4efd2193SJayanth Dodderi ChidanandLater in the cold boot sequence, the BL31 image at EL3 checks for the presence 309*4efd2193SJayanth Dodderi Chidanandof a Secure world image at S-EL2. If detected, it invokes the secure context 310*4efd2193SJayanth Dodderi Chidanandinitialization sequence under SPMD. Additionally, based on RME enablement, 311*4efd2193SJayanth Dodderi Chidanandthe Realm context gets initialized from the RMMD at EL3. Finally, before exiting 312*4efd2193SJayanth Dodderi Chidanandto the normal world, the Non-Secure context gets initialized via the context 313*4efd2193SJayanth Dodderi Chidanandmanagement library. At this stage, all Primary CPU contexts are initialized 314*4efd2193SJayanth Dodderi Chidanandand the CPU exits EL3 to enter the Normal world. 315*4efd2193SJayanth Dodderi Chidanand 316*4efd2193SJayanth Dodderi Chidanand|Context Init ColdBoot| 317*4efd2193SJayanth Dodderi Chidanand 318*4efd2193SJayanth Dodderi Chidanand.. note:: 319*4efd2193SJayanth Dodderi Chidanand The figure above illustrates a scenario on FVP for one of the build 320*4efd2193SJayanth Dodderi Chidanand configurations with TFTF component at NS-EL2. 321*4efd2193SJayanth Dodderi Chidanand 322*4efd2193SJayanth Dodderi ChidanandContext Setup during Warmboot 323*4efd2193SJayanth Dodderi Chidanand~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 324*4efd2193SJayanth Dodderi Chidanand 325*4efd2193SJayanth Dodderi ChidanandDuring a warm boot sequence, the primary CPU is responsible for powering on the 326*4efd2193SJayanth Dodderi Chidanandsecondary CPUs. Refer to :ref:`CPU Reset` and :ref:`Firmware Design` sections for 327*4efd2193SJayanth Dodderi Chidanandmore details on the warm boot. 328*4efd2193SJayanth Dodderi Chidanand 329*4efd2193SJayanth Dodderi Chidanand|Context Init WarmBoot| 330*4efd2193SJayanth Dodderi Chidanand 331*4efd2193SJayanth Dodderi ChidanandThe primary CPU initializes the Non-Secure context for the secondary CPU while 332*4efd2193SJayanth Dodderi Chidanandrestoring re-entry information for the Non-Secure world. 333*4efd2193SJayanth Dodderi ChidanandIt initialises via ``cm_init_context_by_index(target_idx, ep )``. 334*4efd2193SJayanth Dodderi Chidanand 335*4efd2193SJayanth Dodderi Chidanand``psci_warmboot_entrypoint()`` is the warm boot entrypoint procedure. 336*4efd2193SJayanth Dodderi ChidanandDuring the warm bootup process, secondary CPUs have their secure context 337*4efd2193SJayanth Dodderi Chidanandinitialized through SPMD at EL3. Upon successful SP initialization, the SPD 338*4efd2193SJayanth Dodderi Chidanandpower management operations become shared with the PSCI library. During this 339*4efd2193SJayanth Dodderi Chidanandprocess, the SPMD duly registers its handlers with the PSCI library. 340*4efd2193SJayanth Dodderi Chidanand 341*4efd2193SJayanth Dodderi Chidanand.. code:: c 342*4efd2193SJayanth Dodderi Chidanand 343*4efd2193SJayanth Dodderi Chidanand file: psci_common.c 344*4efd2193SJayanth Dodderi Chidanand const spd_pm_ops_t *psci_spd_pm; 345*4efd2193SJayanth Dodderi Chidanand 346*4efd2193SJayanth Dodderi Chidanand file: spmd_pm.c 347*4efd2193SJayanth Dodderi Chidanand const spd_pm_ops_t spmd_pm = { 348*4efd2193SJayanth Dodderi Chidanand .svc_on_finish = spmd_cpu_on_finish_handler, 349*4efd2193SJayanth Dodderi Chidanand .svc_off = spmd_cpu_off_handler 350*4efd2193SJayanth Dodderi Chidanand } 351*4efd2193SJayanth Dodderi Chidanand 352*4efd2193SJayanth Dodderi ChidanandSecondary CPUs during their bootup in the ``psci_cpu_on_finish()`` routine get 353*4efd2193SJayanth Dodderi Chidanandtheir secure context initialised via the registered SPMD handler 354*4efd2193SJayanth Dodderi Chidanand``spmd_cpu_on_finish_handler()`` at EL3. 355*4efd2193SJayanth Dodderi ChidanandThe figure above illustrates the same with reference of Primary CPU running at 356*4efd2193SJayanth Dodderi ChidanandNS-EL2. 357*4efd2193SJayanth Dodderi Chidanand 358*4efd2193SJayanth Dodderi Chidanand.. _Library APIs: 359*4efd2193SJayanth Dodderi Chidanand 360*4efd2193SJayanth Dodderi ChidanandLibrary APIs 361*4efd2193SJayanth Dodderi Chidanand============ 362*4efd2193SJayanth Dodderi Chidanand 363*4efd2193SJayanth Dodderi ChidanandThe public APIs and types can be found in ``include/lib/el3_runtime/context_management.h`` 364*4efd2193SJayanth Dodderi Chidanandand this section is intended to provide additional details and clarifications. 365*4efd2193SJayanth Dodderi Chidanand 366*4efd2193SJayanth Dodderi ChidanandContext Initialization for Individual Worlds 367*4efd2193SJayanth Dodderi Chidanand~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 368*4efd2193SJayanth Dodderi ChidanandThe library implements high level APIs for the CPUs in setting up their individual 369*4efd2193SJayanth Dodderi Chidanandcontext for each world (Non-Secure, Secure and Realm). 370*4efd2193SJayanth Dodderi Chidanand 371*4efd2193SJayanth Dodderi Chidanand.. c:function:: static void setup_context_common(cpu_context_t *ctx, const entry_point_info_t *ep); 372*4efd2193SJayanth Dodderi Chidanand 373*4efd2193SJayanth Dodderi ChidanandThis function is responsible for the general context initialization that applies 374*4efd2193SJayanth Dodderi Chidanandto all worlds. It will be invoked first, before calling the individual 375*4efd2193SJayanth Dodderi Chidanandworld-specific context setup APIs. 376*4efd2193SJayanth Dodderi Chidanand 377*4efd2193SJayanth Dodderi Chidanand.. c:function:: static void setup_ns_context(cpu_context_t *ctx, const struct entry_point_info *ep); 378*4efd2193SJayanth Dodderi Chidanand.. c:function:: static void setup_realm_context(cpu_context_t *ctx, const struct entry_point_info *ep); 379*4efd2193SJayanth Dodderi Chidanand.. c:function:: static void setup_secure_context(cpu_context_t *ctx, const struct entry_point_info *ep); 380*4efd2193SJayanth Dodderi Chidanand 381*4efd2193SJayanth Dodderi ChidanandDepending on the security state that the CPU needs to enter, the respective 382*4efd2193SJayanth Dodderi Chidanandworld-specific context setup handlers listed above will be invoked once per-CPU 383*4efd2193SJayanth Dodderi Chidanandto set up the context for their execution. 384*4efd2193SJayanth Dodderi Chidanand 385*4efd2193SJayanth Dodderi Chidanand.. c:function:: void cm_manage_extensions_el3(void) 386*4efd2193SJayanth Dodderi Chidanand 387*4efd2193SJayanth Dodderi ChidanandThis function initializes all EL3 registers whose values do not change during the 388*4efd2193SJayanth Dodderi Chidanandlifetime of EL3 runtime firmware. It is invoked from each CPU via the cold boot 389*4efd2193SJayanth Dodderi Chidanandpath ``bl31_main()`` and in the WarmBoot entry path ``void psci_warmboot_entrypoint()``. 390*4efd2193SJayanth Dodderi Chidanand 391*4efd2193SJayanth Dodderi ChidanandRuntime Save and Restore of Registers 392*4efd2193SJayanth Dodderi Chidanand~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 393*4efd2193SJayanth Dodderi Chidanand 394*4efd2193SJayanth Dodderi ChidanandEL1 Registers 395*4efd2193SJayanth Dodderi Chidanand------------- 396*4efd2193SJayanth Dodderi Chidanand 397*4efd2193SJayanth Dodderi Chidanand.. c:function:: void cm_el1_sysregs_context_save(uint32_t security_state); 398*4efd2193SJayanth Dodderi Chidanand.. c:function:: void cm_el1_sysregs_context_restore(uint32_t security_state); 399*4efd2193SJayanth Dodderi Chidanand 400*4efd2193SJayanth Dodderi ChidanandThese functions are utilized by the world-specific dispatcher components running 401*4efd2193SJayanth Dodderi Chidanandat EL3 to facilitate the saving and restoration of the EL1 system registers 402*4efd2193SJayanth Dodderi Chidanandduring a world switch. 403*4efd2193SJayanth Dodderi Chidanand 404*4efd2193SJayanth Dodderi ChidanandEL2 Registers 405*4efd2193SJayanth Dodderi Chidanand------------- 406*4efd2193SJayanth Dodderi Chidanand 407*4efd2193SJayanth Dodderi Chidanand.. c:function:: void cm_el2_sysregs_context_save(uint32_t security_state); 408*4efd2193SJayanth Dodderi Chidanand.. c:function:: void cm_el2_sysregs_context_restore(uint32_t security_state); 409*4efd2193SJayanth Dodderi Chidanand 410*4efd2193SJayanth Dodderi ChidanandThese functions are utilized by the world-specific dispatcher components running 411*4efd2193SJayanth Dodderi Chidanandat EL3 to facilitate the saving and restoration of the EL2 system registers 412*4efd2193SJayanth Dodderi Chidanandduring a world switch. 413*4efd2193SJayanth Dodderi Chidanand 414*4efd2193SJayanth Dodderi ChidanandPauth Registers 415*4efd2193SJayanth Dodderi Chidanand--------------- 416*4efd2193SJayanth Dodderi Chidanand 417*4efd2193SJayanth Dodderi ChidanandPointer Authentication feature is enabled by default for Non-Secure world and 418*4efd2193SJayanth Dodderi Chidananddisabled for Secure and Realm worlds. In this case, we don't need to explicitly 419*4efd2193SJayanth Dodderi Chidanandsave and restore the Pauth registers during world switch. 420*4efd2193SJayanth Dodderi ChidanandHowever, ``CTX_INCLUDE_PAUTH_REGS`` flag is explicitly used to enable Pauth for 421*4efd2193SJayanth Dodderi Chidanandlower exception levels of Secure and Realm worlds. In this scenario, we save the 422*4efd2193SJayanth Dodderi Chidanandgeneral purpose and Pauth registers while we enter EL3 from lower ELs via 423*4efd2193SJayanth Dodderi Chidanand``prepare_el3_entry`` and restore them back while we exit EL3 to lower ELs 424*4efd2193SJayanth Dodderi Chidanandvia ``el3_exit``. 425*4efd2193SJayanth Dodderi Chidanand 426*4efd2193SJayanth Dodderi Chidanand.. code:: c 427*4efd2193SJayanth Dodderi Chidanand 428*4efd2193SJayanth Dodderi Chidanand .macro save_gp_pmcr_pauth_regs 429*4efd2193SJayanth Dodderi Chidanand func restore_gp_pmcr_pauth_regs 430*4efd2193SJayanth Dodderi Chidanand 431*4efd2193SJayanth Dodderi ChidanandFeature Enablement for Individual Worlds 432*4efd2193SJayanth Dodderi Chidanand~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 433*4efd2193SJayanth Dodderi Chidanand 434*4efd2193SJayanth Dodderi Chidanand.. c:function:: static void manage_extensions_nonsecure(cpu_context_t *ctx); 435*4efd2193SJayanth Dodderi Chidanand.. c:function:: static void manage_extensions_secure(cpu_context_t *ctx); 436*4efd2193SJayanth Dodderi Chidanand.. c:function:: static void manage_extensions_realm(cpu_context_t *ctx) 437*4efd2193SJayanth Dodderi Chidanand 438*4efd2193SJayanth Dodderi ChidanandFunctions that allow the enabling and disabling of architectural features for 439*4efd2193SJayanth Dodderi Chidanandeach security state. These functions are invoked from the top-level setup APIs 440*4efd2193SJayanth Dodderi Chidanandduring context initialization. 441*4efd2193SJayanth Dodderi Chidanand 442*4efd2193SJayanth Dodderi ChidanandFurther, a pattern is established for feature enablement code (AArch64). 443*4efd2193SJayanth Dodderi ChidanandEach feature implements following APIs as applicable: 444*4efd2193SJayanth Dodderi ChidanandNote: (``xxx`` is the name of the feature in the APIs) 445*4efd2193SJayanth Dodderi Chidanand 446*4efd2193SJayanth Dodderi Chidanand- ``is_feat_xxx_supported()`` and ``is_feat_xxx_present()`` - mandatory for all features. 447*4efd2193SJayanth Dodderi Chidanand 448*4efd2193SJayanth Dodderi Chidanand- ``xxx_enable(cpu_context * )`` and ``xxx_disable(cpu_context * )`` - optional 449*4efd2193SJayanth Dodderi Chidanand functions to enable the feature for the passed context only. To be called in 450*4efd2193SJayanth Dodderi Chidanand the respective world's setup_context to select behaviour. 451*4efd2193SJayanth Dodderi Chidanand 452*4efd2193SJayanth Dodderi Chidanand- ``xxx_init_el3()`` - optional function to enable the feature in-place in any EL3 453*4efd2193SJayanth Dodderi Chidanand registers that are never context switched. The values they write must never 454*4efd2193SJayanth Dodderi Chidanand change, otherwise the functions mentioned in previous point should be used. 455*4efd2193SJayanth Dodderi Chidanand Invoked from ``cm_manage_extensions_el3()``. 456*4efd2193SJayanth Dodderi Chidanand 457*4efd2193SJayanth Dodderi Chidanand- ``xxx_init_el2_unused()`` - optional function to enable the feature in-place 458*4efd2193SJayanth Dodderi Chidanand in any EL2 registers that are necessary for execution in EL1 with no EL2 present. 459*4efd2193SJayanth Dodderi Chidanand 460*4efd2193SJayanth Dodderi ChidanandThe above mentioned rules, followed for ``FEAT_SME`` is shown below: 461*4efd2193SJayanth Dodderi Chidanand 462*4efd2193SJayanth Dodderi Chidanand.. code:: c 463*4efd2193SJayanth Dodderi Chidanand 464*4efd2193SJayanth Dodderi Chidanand void sme_enable(cpu_context_t *context); 465*4efd2193SJayanth Dodderi Chidanand void sme_init_el3(void); 466*4efd2193SJayanth Dodderi Chidanand void sme_init_el2_unused(void); 467*4efd2193SJayanth Dodderi Chidanand void sme_disable(cpu_context_t *context); 468*4efd2193SJayanth Dodderi Chidanand 469*4efd2193SJayanth Dodderi ChidanandPer-world Context 470*4efd2193SJayanth Dodderi Chidanand================= 471*4efd2193SJayanth Dodderi Chidanand 472*4efd2193SJayanth Dodderi ChidanandApart from the CPU context structure, we have another structure to manage some 473*4efd2193SJayanth Dodderi Chidanandof the EL3 system registers whose values are identical across all the CPUs 474*4efd2193SJayanth Dodderi Chidanandreferred to as ``per_world_context_t``. 475*4efd2193SJayanth Dodderi ChidanandThe Per-world context structure is intended for managing EL3 system registers with 476*4efd2193SJayanth Dodderi Chidanandidentical values across all CPUs, requiring only a singular context entry for each 477*4efd2193SJayanth Dodderi Chidanandindividual world. This structure operates independently of the CPU context 478*4efd2193SJayanth Dodderi Chidanandstructure and is intended to manage specific EL3 registers. 479*4efd2193SJayanth Dodderi Chidanand 480*4efd2193SJayanth Dodderi Chidanand.. code-block:: c 481*4efd2193SJayanth Dodderi Chidanand 482*4efd2193SJayanth Dodderi Chidanand typedef struct per_world_context { 483*4efd2193SJayanth Dodderi Chidanand uint64_t ctx_cptr_el3; 484*4efd2193SJayanth Dodderi Chidanand uint64_t ctx_zcr_el3; 485*4efd2193SJayanth Dodderi Chidanand uint64_t ctx_mpam3_el3; 486*4efd2193SJayanth Dodderi Chidanand } per_world_context_t; 487*4efd2193SJayanth Dodderi Chidanand 488*4efd2193SJayanth Dodderi ChidanandThese functions facilitate the activation of architectural extensions that possess 489*4efd2193SJayanth Dodderi Chidanandidentical values across all cores for the individual Non-secure, Secure, and 490*4efd2193SJayanth Dodderi ChidanandRealm worlds. 491*4efd2193SJayanth Dodderi Chidanand 492*4efd2193SJayanth Dodderi Chidanand*Copyright (c) 2024, Arm Limited and Contributors. All rights reserved.* 493*4efd2193SJayanth Dodderi Chidanand 494*4efd2193SJayanth Dodderi Chidanand.. |Context Memory Allocation| image:: ../resources/diagrams/context_memory_allocation.png 495*4efd2193SJayanth Dodderi Chidanand.. |CPU Context Memory Configuration| image:: ../resources/diagrams/cpu_data_config_context_memory.png 496*4efd2193SJayanth Dodderi Chidanand.. |CPU Data Structure| image:: ../resources/diagrams/percpu-data-struct.png 497*4efd2193SJayanth Dodderi Chidanand.. |Context Init ColdBoot| image:: ../resources/diagrams/context_init_coldboot.png 498*4efd2193SJayanth Dodderi Chidanand.. |Context Init WarmBoot| image:: ../resources/diagrams/context_init_warmboot.png 499*4efd2193SJayanth Dodderi Chidanand.. _Trustzone for AArch64: https://developer.arm.com/documentation/102418/0101/TrustZone-in-the-processor/Switching-between-Security-states 500*4efd2193SJayanth Dodderi Chidanand.. _Security States with RME: https://developer.arm.com/documentation/den0126/0100/Security-states 501*4efd2193SJayanth Dodderi Chidanand.. _lib/el3_runtime/(aarch32/aarch64): https://git.trustedfirmware.org/TF-A/trusted-firmware-a.git/tree/lib/el3_runtime