1*91f16700SchasingluluEnhance Context Management library for EL3 firmware 2*91f16700Schasinglulu=================================================== 3*91f16700Schasinglulu 4*91f16700Schasinglulu:Authors: Soby Mathew & Zelalem Aweke 5*91f16700Schasinglulu:Organization: Arm Limited 6*91f16700Schasinglulu:Contact: Soby Mathew <soby.mathew@arm.com> & Zelalem Aweke <zelalem.aweke@arm.com> 7*91f16700Schasinglulu:Status: RFC 8*91f16700Schasinglulu 9*91f16700Schasinglulu.. contents:: Table of Contents 10*91f16700Schasinglulu 11*91f16700SchasingluluIntroduction 12*91f16700Schasinglulu------------ 13*91f16700SchasingluluThe context management library in TF-A provides the basic CPU context 14*91f16700Schasingluluinitialization and management routines for use by different components 15*91f16700Schasingluluin EL3 firmware. The original design of the library was done keeping in 16*91f16700Schasinglulumind the 2 world switch and hence this design pattern has been extended to 17*91f16700Schasinglulukeep up with growing requirements of EL3 firmware. With the introduction 18*91f16700Schasingluluof a new Realm world and a separate Root world for EL3 firmware, it is clear 19*91f16700Schasingluluthat this library needs to be refactored to cater for future enhancements and 20*91f16700Schasinglulureduce chances of introducing error in code. This also aligns with the overall 21*91f16700Schasinglulugoal of reducing EL3 firmware complexity and footprint. 22*91f16700Schasinglulu 23*91f16700SchasingluluIt is expected that the suggestions below could have legacy implications and 24*91f16700Schasingluluhence we are mainly targeting SPM/RMM based systems. It is expected that these 25*91f16700Schasinglululegacy issues will need to be sorted out as part of implementation on a case 26*91f16700Schasingluluby case basis. 27*91f16700Schasinglulu 28*91f16700SchasingluluDesign Principles 29*91f16700Schasinglulu----------------- 30*91f16700SchasingluluThe below section lays down the design principles for re-factoring the context 31*91f16700Schasinglulumanagement library : 32*91f16700Schasinglulu 33*91f16700Schasinglulu(1) **Decentralized model for context mgmt** 34*91f16700Schasinglulu 35*91f16700Schasinglulu Both the Secure and Realm worlds have associated dispatcher component in 36*91f16700Schasinglulu EL3 firmware to allow management of their respective worlds. Allowing the 37*91f16700Schasinglulu dispatcher to own the context for their respective world and moving away 38*91f16700Schasinglulu from a centralized policy management by context management library will 39*91f16700Schasinglulu remove the world differentiation code in the library. This also means that 40*91f16700Schasinglulu the library will not be responsible for CPU feature enablement for 41*91f16700Schasinglulu Secure and Realm worlds. See point 3 and 4 for more details. 42*91f16700Schasinglulu 43*91f16700Schasinglulu The Non Secure world does not have a dispatcher component and hence EL3 44*91f16700Schasinglulu firmware (BL31)/context management library needs to have routines to help 45*91f16700Schasinglulu initialize the Non Secure world context. 46*91f16700Schasinglulu 47*91f16700Schasinglulu(2) **EL3 should only initialize immediate used lower EL** 48*91f16700Schasinglulu 49*91f16700Schasinglulu Due to the way TF-A evolved, from EL3 interacting with an S-EL1 payload to 50*91f16700Schasinglulu SPM in S-EL2, there is some code initializing S-EL1 registers which is 51*91f16700Schasinglulu probably redundant when SPM is present in S-EL2. As a principle, EL3 52*91f16700Schasinglulu firmware should only initialize the next immediate lower EL in use. 53*91f16700Schasinglulu If EL2 needs to be skipped and is not to be used at runtime, then 54*91f16700Schasinglulu EL3 can do the bare minimal EL2 init and init EL1 to prepare for EL3 exit. 55*91f16700Schasinglulu It is expected that this skip EL2 configuration is only needed for NS 56*91f16700Schasinglulu world to support legacy Android deployments. It is worth removing this 57*91f16700Schasinglulu `skip EL2 for Non Secure` config support if this is no longer used. 58*91f16700Schasinglulu 59*91f16700Schasinglulu(3) **Maintain EL3 sysregs which affect lower EL within CPU context** 60*91f16700Schasinglulu 61*91f16700Schasinglulu The CPU context contains some EL3 sysregs and gets applied on a per-world 62*91f16700Schasinglulu basis (eg: cptr_el3, scr_el3, zcr_el3 is part of the context 63*91f16700Schasinglulu because different settings need to be applied between each world). 64*91f16700Schasinglulu But this design pattern is not enforced in TF-A. It is possible to directly 65*91f16700Schasinglulu modify EL3 sysreg dynamically during the transition between NS and Secure 66*91f16700Schasinglulu worlds. Having multiple ways of manipulating EL3 sysregs for different 67*91f16700Schasinglulu values between the worlds is flaky and error prone. The proposal is to 68*91f16700Schasinglulu enforce the rule that any EL3 sysreg which can be different between worlds 69*91f16700Schasinglulu is maintained in the CPU Context. Once the context is initialized the 70*91f16700Schasinglulu EL3 sysreg values corresponding to the world being entered will be restored. 71*91f16700Schasinglulu 72*91f16700Schasinglulu(4) **Allow more flexibility for Dispatchers to select feature set to save and restore** 73*91f16700Schasinglulu 74*91f16700Schasinglulu The current functions for EL2 CPU context save and restore is a single 75*91f16700Schasinglulu function which takes care of saving and restoring all the registers for 76*91f16700Schasinglulu EL2. This method is inflexible and it does not allow to dynamically detect 77*91f16700Schasinglulu CPU features to select registers to save and restore. It also assumes that 78*91f16700Schasinglulu both Realm and Secure world will have the same feature set enabled from 79*91f16700Schasinglulu EL3 at runtime and makes it hard to enable different features for each 80*91f16700Schasinglulu world. The framework should cater for selective save and restore of CPU 81*91f16700Schasinglulu registers which can be controlled by the dispatcher. 82*91f16700Schasinglulu 83*91f16700Schasinglulu For the implementation, this could mean that there is a separate assembly 84*91f16700Schasinglulu save and restore routine corresponding to Arch feature. The memory allocation 85*91f16700Schasinglulu within the CPU Context for each set of registers will be controlled by a 86*91f16700Schasinglulu FEAT_xxx build option. It is a valid configuration to have 87*91f16700Schasinglulu context memory allocated but not used at runtime based on feature detection 88*91f16700Schasinglulu at runtime or the platform owner has decided not to enable the feature 89*91f16700Schasinglulu for the particular world. 90*91f16700Schasinglulu 91*91f16700SchasingluluContext Allocation and Initialization 92*91f16700Schasinglulu------------------------------------- 93*91f16700Schasinglulu 94*91f16700Schasinglulu|context_mgmt_abs| 95*91f16700Schasinglulu 96*91f16700Schasinglulu.. |context_mgmt_abs| image:: 97*91f16700Schasinglulu ../resources/diagrams/context_management_abs.png 98*91f16700Schasinglulu 99*91f16700SchasingluluThe above figure shows how the CPU context is allocated within TF-A. The 100*91f16700Schasingluluallocation for Secure and Realm world is by the respective dispatcher. In the case 101*91f16700Schasingluluof NS world, the context is allocated by the PSCI lib. This scheme allows TF-A 102*91f16700Schasingluluto be built in various configurations (with or without Secure/Realm worlds) and 103*91f16700Schasingluluwill result in optimal memory footprint. The Secure and Realm world contexts are 104*91f16700Schasingluluinitialized by invoking context management library APIs which then initialize 105*91f16700Schasinglulueach world based on conditional evaluation of the security state of the 106*91f16700Schasinglulucontext. The proposal here is to move the conditional initialization 107*91f16700Schasingluluof context for Secure and Realm worlds to their respective dispatchers and 108*91f16700Schasingluluhave the library do only the common init needed. The library can export 109*91f16700Schasingluluhelpers to initialize registers corresponding to certain features but 110*91f16700Schasinglulushould not try to do different initialization between the worlds. The library 111*91f16700Schasinglulucan also export helpers for initialization of NS CPU Context since there is no 112*91f16700Schasingluludispatcher for that world. 113*91f16700Schasinglulu 114*91f16700SchasingluluThis implies that any world specific code in context mgmt lib should now be 115*91f16700Schasinglulumigrated to the respective "owners". To maintain compatibility with legacy, the 116*91f16700Schasinglulucurrent functions can be retained in the lib and perhaps define new ones for 117*91f16700Schasingluluuse by SPMD and RMMD. The details of this can be worked out during 118*91f16700Schasingluluimplementation. 119*91f16700Schasinglulu 120*91f16700SchasingluluIntroducing Root Context 121*91f16700Schasinglulu------------------------ 122*91f16700SchasingluluTill now, we have been ignoring the fact that Root world (or EL3) itself could 123*91f16700Schasingluluhave some settings which are distinct from NS/S/Realm worlds. In this case, 124*91f16700SchasingluluRoot world itself would need to maintain some sysregs settings for its own 125*91f16700Schasingluluexecution and would need to use sysregs of lower EL (eg: PAuth, pmcr) to enable 126*91f16700Schasinglulusome functionalities in EL3. The current sequence for context save and restore 127*91f16700Schasingluluin TF-A is as given below: 128*91f16700Schasinglulu 129*91f16700Schasinglulu|context_mgmt_existing| 130*91f16700Schasinglulu 131*91f16700Schasinglulu.. |context_mgmt_existing| image:: 132*91f16700Schasinglulu ../resources/diagrams/context_mgmt_existing.png 133*91f16700Schasinglulu 134*91f16700SchasingluluNote1: The EL3 CPU context is not a homogenous collection of EL3 sysregs but 135*91f16700Schasinglulua collection of EL3 and some other lower EL registers. The save and restore 136*91f16700Schasingluluis also not done homogenously but based on the objective of using the 137*91f16700Schasingluluparticular register. 138*91f16700Schasinglulu 139*91f16700SchasingluluNote2: The EL1 context save and restore can possibly be removed when switching 140*91f16700Schasingluluto S-EL2 as SPM can take care of saving the incoming NS EL1 context. 141*91f16700Schasinglulu 142*91f16700SchasingluluIt can be seen that the EL3 sysreg values applied while the execution is in Root 143*91f16700Schasingluluworld corresponds to the world it came from (eg: if entering EL3 from NS world, 144*91f16700Schasingluluthe sysregs correspond to the values in NS context). There is a case that EL3 145*91f16700Schasingluluitself may have some settings to apply for various reasons. A good example for 146*91f16700Schasingluluthis is the cptr_el3 regsiter. Although FPU traps need to be disabled for 147*91f16700SchasingluluNon Secure, Secure and Realm worlds, the EL3 execution itself may keep the trap 148*91f16700Schasingluluenabled for the sake of robustness. Another example is, if the MTE feature 149*91f16700Schasingluluis enabled for a particular world, this feature will be enabled for Root world 150*91f16700Schasingluluas well when entering EL3 from that world. The firmware at EL3 may not 151*91f16700Schasinglulube expecting this feature to be enabled and may cause unwanted side-effects 152*91f16700Schasingluluwhich could be problematic. Thus it would be more robust if Root world is not 153*91f16700Schasinglulusubject to EL3 sysreg values from other worlds but maintains its own values 154*91f16700Schasingluluwhich is stable and predictable throughout root world execution. 155*91f16700Schasinglulu 156*91f16700SchasingluluThere is also the case that when EL3 would like to make use of some 157*91f16700SchasingluluArchitectural feature(s) or do some security hardening, it might need 158*91f16700Schasingluluprogramming of some lower EL sysregs. For example, if EL3 needs to make 159*91f16700Schasingluluuse of Pointer Authentication (PAuth) feature, it needs to program 160*91f16700Schasingluluits own PAuth Keys during execution at EL3. Hence EL3 needs its 161*91f16700Schasingluluown copy of PAuth registers which needs to be restored on every 162*91f16700Schasingluluentry to EL3. A similar case can be made for DIT bit in PSTATE, 163*91f16700Schasingluluor use of SP_EL0 for C Runtime Stack at EL3. 164*91f16700Schasinglulu 165*91f16700SchasingluluThe proposal here is to maintain a separate root world CPU context 166*91f16700Schasingluluwhich gets applied for Root world execution. This is not the full 167*91f16700SchasingluluCPU_Context, but subset of EL3 sysregs (`el3_sysreg`) and lower EL 168*91f16700Schasinglulusysregs (`root_exc_context`) used by EL3. The save and restore 169*91f16700Schasinglulusequence for this Root context would need to be done in 170*91f16700Schasingluluan optimal way. The `el3_sysreg` does not need to be saved 171*91f16700Schasingluluon EL3 Exit and possibly only some registers in `root_exc_context` 172*91f16700Schasingluluof Root world context would need to be saved on EL3 exit (eg: SP_EL0). 173*91f16700Schasinglulu 174*91f16700SchasingluluThe new sequence for world switch including Root world context would 175*91f16700Schasinglulube as given below : 176*91f16700Schasinglulu 177*91f16700Schasinglulu|context_mgmt_proposed| 178*91f16700Schasinglulu 179*91f16700Schasinglulu.. |context_mgmt_proposed| image:: 180*91f16700Schasinglulu ../resources/diagrams/context_mgmt_proposed.png 181*91f16700Schasinglulu 182*91f16700SchasingluluHaving this framework in place will allow Root world to make use of lower EL 183*91f16700Schasingluluregisters easily for its own purposes and also have a fixed EL3 sysreg setting 184*91f16700Schasingluluwhich is not affected by the settings of other worlds. This will unify the 185*91f16700SchasingluluRoot world register usage pattern for its own execution and remove some 186*91f16700Schasingluluof the adhoc usages in code. 187*91f16700Schasinglulu 188*91f16700SchasingluluConclusion 189*91f16700Schasinglulu---------- 190*91f16700SchasingluluOf all the proposals, the introduction of Root world context would likely need 191*91f16700Schasinglulufurther prototyping to confirm the design and we will need to measure the 192*91f16700Schasingluluperformance and memory impact of this change. Other changes are incremental 193*91f16700Schasingluluimprovements which are thought to have negligible impact on EL3 performance. 194*91f16700Schasinglulu 195*91f16700Schasinglulu-------------- 196*91f16700Schasinglulu 197*91f16700Schasinglulu*Copyright (c) 2022, Arm Limited and Contributors. All rights reserved.* 198