context_mgmt_rework.rst 10 KB

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