xref: /rk3399_ARM-atf/docs/components/context-management-library.rst (revision 43d1d951ddb3b725d372884f314babb6594fcd47)
14efd2193SJayanth Dodderi ChidanandContext Management Library
24efd2193SJayanth Dodderi Chidanand**************************
34efd2193SJayanth Dodderi Chidanand
44efd2193SJayanth Dodderi ChidanandThis document provides an overview of the Context Management library implementation
54efd2193SJayanth Dodderi Chidanandin Trusted Firmware-A (TF-A). It enumerates and describes the APIs implemented
64efd2193SJayanth Dodderi Chidanandand their accessibility from other components at EL3.
74efd2193SJayanth Dodderi Chidanand
84efd2193SJayanth Dodderi ChidanandOverview
94efd2193SJayanth Dodderi Chidanand========
104efd2193SJayanth Dodderi Chidanand
114efd2193SJayanth Dodderi ChidanandArm TrustZone architecture facilitates hardware-enforced isolation between
124efd2193SJayanth Dodderi Chidanandsoftware running in various security states (Secure/Non-Secure/Realm).
134efd2193SJayanth Dodderi ChidanandThe general-purpose registers, most of the system registers and vector registers
144efd2193SJayanth Dodderi Chidanandare not banked per world. When moving between the security states it is the
154efd2193SJayanth Dodderi Chidanandresponsibility of the secure monitor software (BL31(AArch64) / BL32(Aarch32))
164efd2193SJayanth Dodderi Chidanandin TF-A, not the hardware, to save and restore register state.
174efd2193SJayanth Dodderi ChidanandRefer to `Trustzone for AArch64`_ for more details.
184efd2193SJayanth Dodderi Chidanand
194efd2193SJayanth Dodderi ChidanandEL3 Runtime Firmware, also termed as secure monitor firmware, is integrated
204efd2193SJayanth Dodderi Chidanandwith a context management library to handle the context of the CPU, managing the
214efd2193SJayanth Dodderi Chidanandsaving and restoring of register states across the worlds.
224efd2193SJayanth Dodderi Chidanand
234efd2193SJayanth Dodderi ChidanandTF-A Context
244efd2193SJayanth Dodderi Chidanand============
254efd2193SJayanth Dodderi Chidanand
264efd2193SJayanth Dodderi ChidanandIn TF-A, the context is represented as a data structure used by the EL3 firmware
274efd2193SJayanth Dodderi Chidanandto preserve the state of the CPU at the next lower exception level (EL) in a given
284efd2193SJayanth Dodderi Chidanandsecurity state and save enough EL3 metadata to be able to return to that exception
294efd2193SJayanth Dodderi Chidanandlevel and security state. The memory for the context data structures are allocated
304efd2193SJayanth Dodderi Chidanandin BSS section of EL3 firmware.
314efd2193SJayanth Dodderi Chidanand
324efd2193SJayanth Dodderi ChidanandIn a trusted system at any instance, a given CPU could be executing in one of the
334efd2193SJayanth Dodderi Chidanandsecurity states (Non-Secure, Secure, Realm). Each world must have its
344efd2193SJayanth Dodderi Chidanandconfiguration of system registers independent of other security states to access
354efd2193SJayanth Dodderi Chidanandand execute any of the architectural features.
364efd2193SJayanth Dodderi Chidanand
374efd2193SJayanth Dodderi ChidanandIf the CPU switches across security states (for example: from Non-secure to Secure
384efd2193SJayanth Dodderi Chidanandor vice versa), the register contents, especially the ones that are not banked
394efd2193SJayanth Dodderi Chidanand(EL2/EL1, vector, general-purpose registers), will be overwritten, as the software
404efd2193SJayanth Dodderi Chidanandrunning in either state has the privileges to access them. Additionally, some of
414efd2193SJayanth Dodderi Chidanandthe architectural features enabled in the former security state will be unconditionally
424efd2193SJayanth Dodderi Chidanandaccessible in the latter security state as well. This can be a major concern when
434efd2193SJayanth Dodderi Chidananddealing with security-specific bits, as they need to be explicitly enabled or
444efd2193SJayanth Dodderi Chidananddisabled in each state to prevent data leakage across the worlds.
454efd2193SJayanth Dodderi Chidanand
464efd2193SJayanth Dodderi ChidanandIn general, an ideal trusted system should have Secure world-specific configurations
474efd2193SJayanth Dodderi Chidanandthat are not influenced by Normal World operations. Therefore, for each CPU, we
484efd2193SJayanth Dodderi Chidanandneed to maintain world-specific context to ensure that register entries from one
494efd2193SJayanth Dodderi Chidanandworld do not leak or impact the execution of the CPU in other worlds.
504efd2193SJayanth Dodderi ChidanandThis will help ensure the integrity and security of the system, preventing any
514efd2193SJayanth Dodderi Chidanandunauthorized access or data corruption between the different security states.
524efd2193SJayanth Dodderi Chidanand
534efd2193SJayanth Dodderi ChidanandDesign
544efd2193SJayanth Dodderi Chidanand======
554efd2193SJayanth Dodderi Chidanand
564efd2193SJayanth Dodderi ChidanandThe Context Management library in TF-A is designed to cover all the requirements
574efd2193SJayanth Dodderi Chidanandfor maintaining world-specific context essential for a trusted system.
584efd2193SJayanth Dodderi ChidanandThis includes implementing CPU context initialization and management routines,
594efd2193SJayanth Dodderi Chidanandas well as other helper APIs that are required by dispatcher components in EL3
604efd2193SJayanth Dodderi Chidanandfirmware, which are collectively referred to as CPU Context Management.
614efd2193SJayanth Dodderi ChidanandThe APIs and their usecases are listed in detail under the :ref:`Library APIs`
624efd2193SJayanth Dodderi Chidanandsection.
634efd2193SJayanth Dodderi Chidanand
644efd2193SJayanth Dodderi ChidanandOriginally, the Context Management library in TF-A was designed to cater for a
654efd2193SJayanth Dodderi Chidanandtwo-world system, comprising of Non-Secure and Secure Worlds. In this case, the
664efd2193SJayanth Dodderi ChidanandEL3 Firmware is assumed to be running in Secure World.
674efd2193SJayanth Dodderi ChidanandWith introduction of Realm Management Extension (RME), from Armv9.2 a system
684efd2193SJayanth Dodderi Chidanandcan have four distinct worlds (Non-Secure, Secure, Realm, Root).
694efd2193SJayanth Dodderi ChidanandRME isolates EL3 from all other Security states and moves it into its own security
704efd2193SJayanth Dodderi Chidanandstate called root. EL3 firmware now runs at Root World and thereby is
714efd2193SJayanth Dodderi Chidanandtrusted from software in Non-secure, Secure, and Realm states.
724efd2193SJayanth Dodderi ChidanandRefer to `Security States with RME`_ for more details.
734efd2193SJayanth Dodderi Chidanand
744efd2193SJayanth Dodderi ChidanandKey principles followed in designing the context management library :
754efd2193SJayanth Dodderi Chidanand
764efd2193SJayanth Dodderi Chidanand1. **EL3 should only initialize immediate used lower EL**
774efd2193SJayanth Dodderi Chidanand
784efd2193SJayanth Dodderi ChidanandContext Management library running at EL3 should only initialize and monitor the
794efd2193SJayanth Dodderi Chidanandimmediate used lower EL. This implies that, when S-EL2 is present in the system,
804efd2193SJayanth Dodderi ChidanandEL3 should initialise and monitor S-EL2 registers only. S-EL1 registers should
814efd2193SJayanth Dodderi Chidanandnot be the concern of EL3 while S-EL2 is in place. In systems where S-EL2 is
824efd2193SJayanth Dodderi Chidanandabsent, S-EL1 registers should be initialised from EL3.
834efd2193SJayanth Dodderi Chidanand
844efd2193SJayanth Dodderi Chidanand2. **Decentralized model for context management**
854efd2193SJayanth Dodderi Chidanand
864efd2193SJayanth Dodderi ChidanandEach world (Non-Secure, Secure, and Realm) should have their separate component
874efd2193SJayanth Dodderi Chidanandin EL3 responsible for their respective world context management.
884efd2193SJayanth Dodderi ChidanandBoth the Secure and Realm world have associated dispatcher components in EL3
894efd2193SJayanth Dodderi Chidanandfirmware to allow management of the respective worlds. For the Non-Secure world,
904efd2193SJayanth Dodderi ChidanandPSCI Library (BL31)/context management library provides routines to help
914efd2193SJayanth Dodderi Chidanandinitialize the Non-Secure world context.
924efd2193SJayanth Dodderi Chidanand
934efd2193SJayanth Dodderi Chidanand3. **Flexibility for Dispatchers to select desired feature set to save and restore**
944efd2193SJayanth Dodderi Chidanand
954efd2193SJayanth Dodderi ChidanandEach feature is supported with a helper function ``is_feature_supported(void)``,
964efd2193SJayanth Dodderi Chidanandto detect its presence at runtime. This helps dispatchers to select the desired
974efd2193SJayanth Dodderi Chidanandfeature set, and thereby save and restore the configuration associated with them.
984efd2193SJayanth Dodderi Chidanand
994efd2193SJayanth Dodderi Chidanand4. **Dynamic discovery of Feature enablement by EL3**
1004efd2193SJayanth Dodderi Chidanand
101*43d1d951SManish PandeyTF-A supports four states for feature enablement at EL3, to make them available
1024efd2193SJayanth Dodderi Chidanandfor lower exception levels.
1034efd2193SJayanth Dodderi Chidanand
1044efd2193SJayanth Dodderi Chidanand.. code:: c
1054efd2193SJayanth Dodderi Chidanand
1064efd2193SJayanth Dodderi Chidanand	#define FEAT_STATE_DISABLED     	0
1074efd2193SJayanth Dodderi Chidanand	#define FEAT_STATE_ENABLED      	1
1084efd2193SJayanth Dodderi Chidanand	#define FEAT_STATE_CHECK        	2
109*43d1d951SManish Pandey	#define FEAT_STATE_CHECK_ASYMMETRIC	3
1104efd2193SJayanth Dodderi Chidanand
1114efd2193SJayanth Dodderi ChidanandA pattern is established for feature enablement behavior.
1124efd2193SJayanth Dodderi ChidanandEach feature must support the 3 possible values with rigid semantics.
1134efd2193SJayanth Dodderi Chidanand
1144efd2193SJayanth Dodderi Chidanand- **FEAT_STATE_DISABLED** - all code relating to this feature is always skipped.
1154efd2193SJayanth Dodderi Chidanand  Firmware is unaware of this feature.
1164efd2193SJayanth Dodderi Chidanand
1174efd2193SJayanth Dodderi Chidanand- **FEAT_STATE_ALWAYS** - all code relating to this feature is always executed.
1184efd2193SJayanth Dodderi Chidanand  Firmware expects this feature to be present in hardware.
1194efd2193SJayanth Dodderi Chidanand
1204efd2193SJayanth Dodderi Chidanand- **FEAT_STATE_CHECK** - same as ``FEAT_STATE_ALWAYS`` except that the feature's
1214efd2193SJayanth Dodderi Chidanand  existence will be checked at runtime. Default on dynamic platforms (example: FVP).
1224efd2193SJayanth Dodderi Chidanand
123*43d1d951SManish Pandey- **FEAT_STATE_CHECK_ASYMMETRIC** - same as ``FEAT_STATE_CHECK`` except that the feature's
124*43d1d951SManish Pandey  existence is asymmetric across cores, which requires the feature existence is checked
125*43d1d951SManish Pandey  during warmboot path also. Note that only limited number of features can be asymmetric.
126*43d1d951SManish Pandey
127*43d1d951SManish Pandey .. note::
128*43d1d951SManish Pandey   Only limited number of features can be ``FEAT_STATE_CHECK_ASYMMETRIC`` this is due to
129*43d1d951SManish Pandey   the fact that Operating systems are designed for SMP systems.
130*43d1d951SManish Pandey   There are no clear guidelines what kind of mismatch is allowed but following pointers
131*43d1d951SManish Pandey   can help making a decision
132*43d1d951SManish Pandey
133*43d1d951SManish Pandey    - All mandatory features must be symmetric.
134*43d1d951SManish Pandey    - Any feature that impacts the generation of page tables must be symmetric.
135*43d1d951SManish Pandey    - Any feature access which does not trap to EL3 should be symmetric.
136*43d1d951SManish Pandey    - Features related with profiling, debug and trace could be asymmetric
137*43d1d951SManish Pandey    - Migration of vCPU/tasks between CPUs should not cause an error
138*43d1d951SManish Pandey
139*43d1d951SManish Pandey    Whenever there is asymmetric feature support is added for a feature TF-A need to add
140*43d1d951SManish Pandey    feature specific code in context management code.
141*43d1d951SManish Pandey
1424efd2193SJayanth Dodderi Chidanand .. note::
1434efd2193SJayanth Dodderi Chidanand   ``FEAT_RAS`` is an exception here, as it impacts the execution of EL3 and
1444efd2193SJayanth Dodderi Chidanand   it is essential to know its presence at compile time. Refer to ``ENABLE_FEAT``
1454efd2193SJayanth Dodderi Chidanand   macro under :ref:`Build Options` section for more details.
1464efd2193SJayanth Dodderi Chidanand
1474efd2193SJayanth Dodderi ChidanandCode Structure
1484efd2193SJayanth Dodderi Chidanand==============
1494efd2193SJayanth Dodderi Chidanand
1504efd2193SJayanth Dodderi Chidanand`lib/el3_runtime/(aarch32/aarch64)`_ - Context library code directory.
1514efd2193SJayanth Dodderi Chidanand
1524efd2193SJayanth Dodderi ChidanandSource Files
1534efd2193SJayanth Dodderi Chidanand~~~~~~~~~~~~
1544efd2193SJayanth Dodderi Chidanand
1554efd2193SJayanth Dodderi Chidanand#. ``context_mgmt.c`` : consists of core functions that setup, save and restore
1564efd2193SJayanth Dodderi Chidanand   context for different security states alongside high level feature enablement
1574efd2193SJayanth Dodderi Chidanand   APIs for individual worlds.
1584efd2193SJayanth Dodderi Chidanand
1594efd2193SJayanth Dodderi Chidanand#. ``cpu_data_array.c`` : contains per_cpu_data structure instantiation.
1604efd2193SJayanth Dodderi Chidanand
1614efd2193SJayanth Dodderi Chidanand#. ``context.S`` : consists of functions that save and restore some of the context
1624efd2193SJayanth Dodderi Chidanand   structure members in assembly code.
1634efd2193SJayanth Dodderi Chidanand
1644efd2193SJayanth Dodderi Chidanand#. ``cpu_data.S`` : consists of helper functions to initialise per_cpu_data pointers.
1654efd2193SJayanth Dodderi Chidanand
1664efd2193SJayanth Dodderi Chidanand#. ``el3_common_macros.S`` : consists of macros to facilitate actions to be performed
1674efd2193SJayanth Dodderi Chidanand   during cold and warmboot and el3 registers initialisation in assembly code.
1684efd2193SJayanth Dodderi Chidanand
1694efd2193SJayanth Dodderi ChidanandHeader Files
1704efd2193SJayanth Dodderi Chidanand~~~~~~~~~~~~
1714efd2193SJayanth Dodderi Chidanand
1724efd2193SJayanth Dodderi Chidanand#. ``context_mgmt.h`` :  contains the public interface to Context Management Library.
1734efd2193SJayanth Dodderi Chidanand
1744efd2193SJayanth Dodderi Chidanand#. ``context.h`` : contains the helper macros and definitions for context entries.
1754efd2193SJayanth Dodderi Chidanand
1764efd2193SJayanth Dodderi Chidanand#. ``cpu_data.h`` : contains the public interface to Per CPU data structure.
1774efd2193SJayanth Dodderi Chidanand
1784efd2193SJayanth Dodderi Chidanand#. ``context_debug.h`` : contains public interface to report context memory
1794efd2193SJayanth Dodderi Chidanand   utilisation across the security states.
1804efd2193SJayanth Dodderi Chidanand
1814efd2193SJayanth Dodderi Chidanand#. ``context_el2.h`` : internal header consisting of helper macros to access EL2
1824efd2193SJayanth Dodderi Chidanand   context entries. Used by ``context.h``.
1834efd2193SJayanth Dodderi Chidanand
1844efd2193SJayanth Dodderi ChidanandApart from these files, we have some context related source files under ``BL1``
1854efd2193SJayanth Dodderi Chidanandand ``BL31`` directory. ``bl1_context_mgmt.c`` ``bl31_context_mgmt.c``
1864efd2193SJayanth Dodderi Chidanand
1874efd2193SJayanth Dodderi ChidanandBootloader Images utilizing Context Management Library
1884efd2193SJayanth Dodderi Chidanand======================================================
1894efd2193SJayanth Dodderi Chidanand
1904efd2193SJayanth Dodderi Chidanand+-------------------------------------------+-----------------------------+
1914efd2193SJayanth Dodderi Chidanand|   Bootloader                              | Context Management Library  |
1924efd2193SJayanth Dodderi Chidanand+-------------------------------------------+-----------------------------+
1934efd2193SJayanth Dodderi Chidanand|   BL1                                     |       Yes                   |
1944efd2193SJayanth Dodderi Chidanand+-------------------------------------------+-----------------------------+
1954efd2193SJayanth Dodderi Chidanand|   BL2                                     |       No                    |
1964efd2193SJayanth Dodderi Chidanand+-------------------------------------------+-----------------------------+
1974efd2193SJayanth Dodderi Chidanand|   BL31 (Aarch64- EL3runtime firmware)     |       Yes                   |
1984efd2193SJayanth Dodderi Chidanand+-------------------------------------------+-----------------------------+
1994efd2193SJayanth Dodderi Chidanand|   BL32 (Aarch32- EL3runtime firmware)     |       Yes                   |
2004efd2193SJayanth Dodderi Chidanand+-------------------------------------------+-----------------------------+
2014efd2193SJayanth Dodderi Chidanand
2024efd2193SJayanth Dodderi ChidanandCPU Data Structure
2034efd2193SJayanth Dodderi Chidanand==================
2044efd2193SJayanth Dodderi ChidanandFor a given system, depending on the CPU count, the platform statically
2054efd2193SJayanth Dodderi Chidanandallocates memory for the CPU data structure.
2064efd2193SJayanth Dodderi Chidanand
2074efd2193SJayanth Dodderi Chidanand.. code:: c
2084efd2193SJayanth Dodderi Chidanand
2094efd2193SJayanth Dodderi Chidanand	/* The per_cpu_ptr_cache_t space allocation */
2104efd2193SJayanth Dodderi Chidanand	cpu_data_t percpu_data[PLATFORM_CORE_COUNT];
2114efd2193SJayanth Dodderi Chidanand
2124efd2193SJayanth Dodderi ChidanandThis CPU data structure has a member element with an array of pointers to hold
2134efd2193SJayanth Dodderi Chidanandthe Non-Secure, Realm and Secure security state context structures as listed below.
2144efd2193SJayanth Dodderi Chidanand
2154efd2193SJayanth Dodderi Chidanand.. code:: c
2164efd2193SJayanth Dodderi Chidanand
2174efd2193SJayanth Dodderi Chidanand	typedef struct cpu_data {
2184efd2193SJayanth Dodderi Chidanand	#ifdef __aarch64__
2194efd2193SJayanth Dodderi Chidanand	void *cpu_context[CPU_DATA_CONTEXT_NUM];
2204efd2193SJayanth Dodderi Chidanand	#endif
2214efd2193SJayanth Dodderi Chidanand
2224efd2193SJayanth Dodderi Chidanand	....
2234efd2193SJayanth Dodderi Chidanand	....
2244efd2193SJayanth Dodderi Chidanand
2254efd2193SJayanth Dodderi Chidanand	}cpu_data_t;
2264efd2193SJayanth Dodderi Chidanand
2274efd2193SJayanth Dodderi Chidanand|CPU Data Structure|
2284efd2193SJayanth Dodderi Chidanand
2294efd2193SJayanth Dodderi ChidanandAt runtime, ``cpu_context[CPU_DATA_CONTEXT_NUM]`` array will be intitialised with
2304efd2193SJayanth Dodderi Chidanandthe Secure, Non-Secure and Realm context structure addresses to ensure proper
2314efd2193SJayanth Dodderi Chidanandhandling of the register state.
2324efd2193SJayanth Dodderi ChidanandSee :ref:`Library APIs` section for more details.
2334efd2193SJayanth Dodderi Chidanand
2344efd2193SJayanth Dodderi ChidanandCPU Context and Memory allocation
2354efd2193SJayanth Dodderi Chidanand=================================
2364efd2193SJayanth Dodderi Chidanand
2374efd2193SJayanth Dodderi ChidanandCPU Context
2384efd2193SJayanth Dodderi Chidanand~~~~~~~~~~~
2394efd2193SJayanth Dodderi ChidanandThe members of the context structure used by the EL3 firmware to preserve the
2404efd2193SJayanth Dodderi Chidanandstate of CPU across exception levels for a given security state are listed below.
2414efd2193SJayanth Dodderi Chidanand
2424efd2193SJayanth Dodderi Chidanand.. code:: c
2434efd2193SJayanth Dodderi Chidanand
2444efd2193SJayanth Dodderi Chidanand	typedef struct cpu_context {
2454efd2193SJayanth Dodderi Chidanand	gp_regs_t gpregs_ctx;
2464efd2193SJayanth Dodderi Chidanand	el3_state_t el3state_ctx;
2474efd2193SJayanth Dodderi Chidanand	el1_sysregs_t el1_sysregs_ctx;
2484efd2193SJayanth Dodderi Chidanand
2494efd2193SJayanth Dodderi Chidanand	#if CTX_INCLUDE_EL2_REGS
2504efd2193SJayanth Dodderi Chidanand	el2_sysregs_t el2_sysregs_ctx;
2514efd2193SJayanth Dodderi Chidanand	#endif
2524efd2193SJayanth Dodderi Chidanand
2534efd2193SJayanth Dodderi Chidanand	#if CTX_INCLUDE_FPREGS
2544efd2193SJayanth Dodderi Chidanand	fp_regs_t fpregs_ctx;
2554efd2193SJayanth Dodderi Chidanand	#endif
2564efd2193SJayanth Dodderi Chidanand
2574efd2193SJayanth Dodderi Chidanand	cve_2018_3639_t cve_2018_3639_ctx;
2584efd2193SJayanth Dodderi Chidanand	#if CTX_INCLUDE_PAUTH_REGS
2594efd2193SJayanth Dodderi Chidanand	pauth_t pauth_ctx;
2604efd2193SJayanth Dodderi Chidanand	#endif
2614efd2193SJayanth Dodderi Chidanand
2624efd2193SJayanth Dodderi Chidanand	#if CTX_INCLUDE_MPAM_REGS
2634efd2193SJayanth Dodderi Chidanand	mpam_t	mpam_ctx;
2644efd2193SJayanth Dodderi Chidanand	#endif
2654efd2193SJayanth Dodderi Chidanand
2664efd2193SJayanth Dodderi Chidanand	} cpu_context_t;
2674efd2193SJayanth Dodderi Chidanand
2684efd2193SJayanth Dodderi ChidanandContext Memory Allocation
2694efd2193SJayanth Dodderi Chidanand~~~~~~~~~~~~~~~~~~~~~~~~~
2704efd2193SJayanth Dodderi Chidanand
2714efd2193SJayanth Dodderi ChidanandCPUs maintain their context per world. The individual context memory allocation
2724efd2193SJayanth Dodderi Chidanandfor each CPU per world is allocated by the world-specific dispatcher components
2734efd2193SJayanth Dodderi Chidanandat compile time as shown below.
2744efd2193SJayanth Dodderi Chidanand
2754efd2193SJayanth Dodderi Chidanand|Context memory allocation|
2764efd2193SJayanth Dodderi Chidanand
2774efd2193SJayanth Dodderi ChidanandNS-Context Memory
2784efd2193SJayanth Dodderi Chidanand~~~~~~~~~~~~~~~~~
2794efd2193SJayanth Dodderi ChidanandIt's important to note that the Normal world doesn't possess the dispatcher
2804efd2193SJayanth Dodderi Chidanandcomponent found in the Secure and Realm worlds. Instead, the PSCI library at EL3
2814efd2193SJayanth Dodderi Chidanandhandles memory allocation for ``Non-Secure`` world context for all CPUs.
2824efd2193SJayanth Dodderi Chidanand
2834efd2193SJayanth Dodderi Chidanand.. code:: c
2844efd2193SJayanth Dodderi Chidanand
2854efd2193SJayanth Dodderi Chidanand	static cpu_context_t psci_ns_context[PLATFORM_CORE_COUNT];
2864efd2193SJayanth Dodderi Chidanand
2874efd2193SJayanth Dodderi ChidanandSecure-Context Memory
2884efd2193SJayanth Dodderi Chidanand~~~~~~~~~~~~~~~~~~~~~
2894efd2193SJayanth Dodderi ChidanandSecure World dispatcher (such as SPMD) at EL3 allocates the memory for ``Secure``
2904efd2193SJayanth Dodderi Chidanandworld context of all CPUs.
2914efd2193SJayanth Dodderi Chidanand
2924efd2193SJayanth Dodderi Chidanand.. code:: c
2934efd2193SJayanth Dodderi Chidanand
2944efd2193SJayanth Dodderi Chidanand	static spmd_spm_core_context_t spm_core_context[PLATFORM_CORE_COUNT];
2954efd2193SJayanth Dodderi Chidanand
2964efd2193SJayanth Dodderi ChidanandRealm-Context Memory
2974efd2193SJayanth Dodderi Chidanand~~~~~~~~~~~~~~~~~~~~
2984efd2193SJayanth Dodderi ChidanandRealm World dispatcher (RMMD) at EL3 allocates the memory for ``Realm`` world
2994efd2193SJayanth Dodderi Chidanandcontext of all CPUs.
3004efd2193SJayanth Dodderi Chidanand
3014efd2193SJayanth Dodderi Chidanand.. code:: c
3024efd2193SJayanth Dodderi Chidanand
3034efd2193SJayanth Dodderi Chidanand	rmmd_rmm_context_t rmm_context[PLATFORM_CORE_COUNT];
3044efd2193SJayanth Dodderi Chidanand
3054efd2193SJayanth Dodderi ChidanandTo summarize, the world-specific context structures are synchronized with
3064efd2193SJayanth Dodderi Chidanandper-CPU data structures, which means that each CPU will have an array of pointers
3074efd2193SJayanth Dodderi Chidanandto individual worlds. The figure below illustrates the same.
3084efd2193SJayanth Dodderi Chidanand
3094efd2193SJayanth Dodderi Chidanand|CPU Context Memory Configuration|
3104efd2193SJayanth Dodderi Chidanand
3114efd2193SJayanth Dodderi ChidanandContext Setup/Initialization
3124efd2193SJayanth Dodderi Chidanand============================
3134efd2193SJayanth Dodderi Chidanand
3144efd2193SJayanth Dodderi ChidanandThe CPU has been assigned context structures for every security state, which include
3154efd2193SJayanth Dodderi ChidanandNon-Secure, Secure and Realm. It is crucial to initialize each of these structures
3164efd2193SJayanth Dodderi Chidanandduring the bootup of every CPU before they enter any security state for the
3174efd2193SJayanth Dodderi Chidanandfirst time. This section explains the specifics of how the initialization of
3184efd2193SJayanth Dodderi Chidanandevery CPU context takes place during both cold and warm boot paths.
3194efd2193SJayanth Dodderi Chidanand
3204efd2193SJayanth Dodderi ChidanandContext Setup during Cold boot
3214efd2193SJayanth Dodderi Chidanand~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3224efd2193SJayanth Dodderi ChidanandThe cold boot path is mainly executed by the primary CPU, other than essential
3234efd2193SJayanth Dodderi ChidanandCPU initialization executed by all CPUs. After executing BL1 and BL2, the Primary
3244efd2193SJayanth Dodderi ChidanandCPU jumps to the BL31 image for runtime services initialization.
3254efd2193SJayanth Dodderi ChidanandDuring this process, the per_cpu_data structure gets initialized with statically
3264efd2193SJayanth Dodderi Chidanandallocated world-specific context memory.
3274efd2193SJayanth Dodderi Chidanand
3284efd2193SJayanth Dodderi ChidanandLater in the cold boot sequence, the BL31 image at EL3 checks for the presence
3294efd2193SJayanth Dodderi Chidanandof a Secure world image at S-EL2. If detected, it invokes the secure context
3304efd2193SJayanth Dodderi Chidanandinitialization sequence under SPMD. Additionally, based on RME enablement,
3314efd2193SJayanth Dodderi Chidanandthe Realm context gets initialized from the RMMD at EL3. Finally, before exiting
3324efd2193SJayanth Dodderi Chidanandto the normal world, the Non-Secure context gets initialized via the context
3334efd2193SJayanth Dodderi Chidanandmanagement library. At this stage, all Primary CPU contexts are initialized
3344efd2193SJayanth Dodderi Chidanandand the CPU exits EL3 to enter the Normal world.
3354efd2193SJayanth Dodderi Chidanand
3364efd2193SJayanth Dodderi Chidanand|Context Init ColdBoot|
3374efd2193SJayanth Dodderi Chidanand
3384efd2193SJayanth Dodderi Chidanand.. note::
3394efd2193SJayanth Dodderi Chidanand   The figure above illustrates a scenario on FVP for one of the build
3404efd2193SJayanth Dodderi Chidanand   configurations with TFTF component at NS-EL2.
3414efd2193SJayanth Dodderi Chidanand
3424efd2193SJayanth Dodderi ChidanandContext Setup during Warmboot
3434efd2193SJayanth Dodderi Chidanand~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3444efd2193SJayanth Dodderi Chidanand
3454efd2193SJayanth Dodderi ChidanandDuring a warm boot sequence, the primary CPU is responsible for powering on the
3464efd2193SJayanth Dodderi Chidanandsecondary CPUs. Refer to :ref:`CPU Reset` and :ref:`Firmware Design` sections for
3474efd2193SJayanth Dodderi Chidanandmore details on the warm boot.
3484efd2193SJayanth Dodderi Chidanand
3494efd2193SJayanth Dodderi Chidanand|Context Init WarmBoot|
3504efd2193SJayanth Dodderi Chidanand
3514efd2193SJayanth Dodderi ChidanandThe primary CPU initializes the Non-Secure context for the secondary CPU while
3524efd2193SJayanth Dodderi Chidanandrestoring re-entry information for the Non-Secure world.
3534efd2193SJayanth Dodderi ChidanandIt initialises via ``cm_init_context_by_index(target_idx, ep )``.
3544efd2193SJayanth Dodderi Chidanand
3554efd2193SJayanth Dodderi Chidanand``psci_warmboot_entrypoint()`` is the warm boot entrypoint procedure.
3564efd2193SJayanth Dodderi ChidanandDuring the warm bootup process, secondary CPUs have their secure context
3574efd2193SJayanth Dodderi Chidanandinitialized through SPMD at EL3. Upon successful SP initialization, the SPD
3584efd2193SJayanth Dodderi Chidanandpower management operations become shared with the PSCI library. During this
3594efd2193SJayanth Dodderi Chidanandprocess, the SPMD duly registers its handlers with the PSCI library.
3604efd2193SJayanth Dodderi Chidanand
3614efd2193SJayanth Dodderi Chidanand.. code:: c
3624efd2193SJayanth Dodderi Chidanand
3634efd2193SJayanth Dodderi Chidanand	file: psci_common.c
3644efd2193SJayanth Dodderi Chidanand	const spd_pm_ops_t *psci_spd_pm;
3654efd2193SJayanth Dodderi Chidanand
3664efd2193SJayanth Dodderi Chidanand	file: spmd_pm.c
3674efd2193SJayanth Dodderi Chidanand	const spd_pm_ops_t spmd_pm = {
3684efd2193SJayanth Dodderi Chidanand	.svc_on_finish = spmd_cpu_on_finish_handler,
3694efd2193SJayanth Dodderi Chidanand	.svc_off = spmd_cpu_off_handler
3704efd2193SJayanth Dodderi Chidanand	}
3714efd2193SJayanth Dodderi Chidanand
3724efd2193SJayanth Dodderi ChidanandSecondary CPUs during their bootup in the ``psci_cpu_on_finish()`` routine get
3734efd2193SJayanth Dodderi Chidanandtheir secure context initialised via the registered SPMD handler
3744efd2193SJayanth Dodderi Chidanand``spmd_cpu_on_finish_handler()`` at EL3.
3754efd2193SJayanth Dodderi ChidanandThe figure above illustrates the same with reference of Primary CPU running at
3764efd2193SJayanth Dodderi ChidanandNS-EL2.
3774efd2193SJayanth Dodderi Chidanand
3784efd2193SJayanth Dodderi Chidanand.. _Library APIs:
3794efd2193SJayanth Dodderi Chidanand
3804efd2193SJayanth Dodderi ChidanandLibrary APIs
3814efd2193SJayanth Dodderi Chidanand============
3824efd2193SJayanth Dodderi Chidanand
3834efd2193SJayanth Dodderi ChidanandThe public APIs and types can be found in ``include/lib/el3_runtime/context_management.h``
3844efd2193SJayanth Dodderi Chidanandand this section is intended to provide additional details and clarifications.
3854efd2193SJayanth Dodderi Chidanand
3864efd2193SJayanth Dodderi ChidanandContext Initialization for Individual Worlds
3874efd2193SJayanth Dodderi Chidanand~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3884efd2193SJayanth Dodderi ChidanandThe library implements high level APIs for the CPUs in setting up their individual
3894efd2193SJayanth Dodderi Chidanandcontext for each world (Non-Secure, Secure and Realm).
3904efd2193SJayanth Dodderi Chidanand
3914efd2193SJayanth Dodderi Chidanand.. c:function::	static void setup_context_common(cpu_context_t *ctx, const entry_point_info_t *ep);
3924efd2193SJayanth Dodderi Chidanand
3934efd2193SJayanth Dodderi ChidanandThis function is responsible for the general context initialization that applies
3944efd2193SJayanth Dodderi Chidanandto all worlds. It will be invoked first, before calling the individual
3954efd2193SJayanth Dodderi Chidanandworld-specific context setup APIs.
3964efd2193SJayanth Dodderi Chidanand
3974efd2193SJayanth Dodderi Chidanand.. c:function::	static void setup_ns_context(cpu_context_t *ctx, const struct entry_point_info *ep);
3984efd2193SJayanth Dodderi Chidanand.. c:function::	static void setup_realm_context(cpu_context_t *ctx, const struct entry_point_info *ep);
3994efd2193SJayanth Dodderi Chidanand.. c:function::	static void setup_secure_context(cpu_context_t *ctx, const struct entry_point_info *ep);
4004efd2193SJayanth Dodderi Chidanand
4014efd2193SJayanth Dodderi ChidanandDepending on the security state that the CPU needs to enter, the respective
4024efd2193SJayanth Dodderi Chidanandworld-specific context setup handlers listed above will be invoked once per-CPU
4034efd2193SJayanth Dodderi Chidanandto set up the context for their execution.
4044efd2193SJayanth Dodderi Chidanand
4054efd2193SJayanth Dodderi Chidanand.. c:function::	void cm_manage_extensions_el3(void)
4064efd2193SJayanth Dodderi Chidanand
4074efd2193SJayanth Dodderi ChidanandThis function initializes all EL3 registers whose values do not change during the
4084efd2193SJayanth Dodderi Chidanandlifetime of EL3 runtime firmware. It is invoked from each CPU via the cold boot
4094efd2193SJayanth Dodderi Chidanandpath ``bl31_main()`` and in the WarmBoot entry path ``void psci_warmboot_entrypoint()``.
4104efd2193SJayanth Dodderi Chidanand
4114efd2193SJayanth Dodderi ChidanandRuntime Save and Restore of Registers
4124efd2193SJayanth Dodderi Chidanand~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4134efd2193SJayanth Dodderi Chidanand
4144efd2193SJayanth Dodderi ChidanandEL1 Registers
4154efd2193SJayanth Dodderi Chidanand-------------
4164efd2193SJayanth Dodderi Chidanand
4174efd2193SJayanth Dodderi Chidanand.. c:function::	void cm_el1_sysregs_context_save(uint32_t security_state);
4184efd2193SJayanth Dodderi Chidanand.. c:function::	void cm_el1_sysregs_context_restore(uint32_t security_state);
4194efd2193SJayanth Dodderi Chidanand
4204efd2193SJayanth Dodderi ChidanandThese functions are utilized by the world-specific dispatcher components running
4214efd2193SJayanth Dodderi Chidanandat EL3 to facilitate the saving and restoration of the EL1 system registers
4224efd2193SJayanth Dodderi Chidanandduring a world switch.
4234efd2193SJayanth Dodderi Chidanand
4244efd2193SJayanth Dodderi ChidanandEL2 Registers
4254efd2193SJayanth Dodderi Chidanand-------------
4264efd2193SJayanth Dodderi Chidanand
4274efd2193SJayanth Dodderi Chidanand.. c:function::	void cm_el2_sysregs_context_save(uint32_t security_state);
4284efd2193SJayanth Dodderi Chidanand.. c:function::	void cm_el2_sysregs_context_restore(uint32_t security_state);
4294efd2193SJayanth Dodderi Chidanand
4304efd2193SJayanth Dodderi ChidanandThese functions are utilized by the world-specific dispatcher components running
4314efd2193SJayanth Dodderi Chidanandat EL3 to facilitate the saving and restoration of the EL2 system registers
4324efd2193SJayanth Dodderi Chidanandduring a world switch.
4334efd2193SJayanth Dodderi Chidanand
4344efd2193SJayanth Dodderi ChidanandPauth Registers
4354efd2193SJayanth Dodderi Chidanand---------------
4364efd2193SJayanth Dodderi Chidanand
4374efd2193SJayanth Dodderi ChidanandPointer Authentication feature is enabled by default for Non-Secure world and
4384efd2193SJayanth Dodderi Chidananddisabled for Secure and Realm worlds. In this case, we don't need to explicitly
4394efd2193SJayanth Dodderi Chidanandsave and restore the Pauth registers during world switch.
4404efd2193SJayanth Dodderi ChidanandHowever, ``CTX_INCLUDE_PAUTH_REGS`` flag is explicitly used to enable Pauth for
4414efd2193SJayanth Dodderi Chidanandlower exception levels of Secure and Realm worlds. In this scenario, we save the
4424efd2193SJayanth Dodderi Chidanandgeneral purpose and Pauth registers while we enter EL3 from lower ELs via
4434efd2193SJayanth Dodderi Chidanand``prepare_el3_entry`` and restore them back while we exit EL3 to lower ELs
4444efd2193SJayanth Dodderi Chidanandvia ``el3_exit``.
4454efd2193SJayanth Dodderi Chidanand
4464efd2193SJayanth Dodderi Chidanand.. code:: c
4474efd2193SJayanth Dodderi Chidanand
4484efd2193SJayanth Dodderi Chidanand	.macro save_gp_pmcr_pauth_regs
4494efd2193SJayanth Dodderi Chidanand	func restore_gp_pmcr_pauth_regs
4504efd2193SJayanth Dodderi Chidanand
4514efd2193SJayanth Dodderi ChidanandFeature Enablement for Individual Worlds
4524efd2193SJayanth Dodderi Chidanand~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4534efd2193SJayanth Dodderi Chidanand
4544efd2193SJayanth Dodderi Chidanand.. c:function::	static void manage_extensions_nonsecure(cpu_context_t *ctx);
4554efd2193SJayanth Dodderi Chidanand.. c:function::	static void manage_extensions_secure(cpu_context_t *ctx);
4564efd2193SJayanth Dodderi Chidanand.. c:function::	static void manage_extensions_realm(cpu_context_t *ctx)
4574efd2193SJayanth Dodderi Chidanand
4584efd2193SJayanth Dodderi ChidanandFunctions that allow the enabling and disabling of architectural features for
4594efd2193SJayanth Dodderi Chidanandeach security state. These functions are invoked from the top-level setup APIs
4604efd2193SJayanth Dodderi Chidanandduring context initialization.
4614efd2193SJayanth Dodderi Chidanand
4624efd2193SJayanth Dodderi ChidanandFurther, a pattern is established for feature enablement code (AArch64).
4634efd2193SJayanth Dodderi ChidanandEach feature implements following APIs as applicable:
4644efd2193SJayanth Dodderi ChidanandNote: (``xxx`` is the name of the feature in the APIs)
4654efd2193SJayanth Dodderi Chidanand
4664efd2193SJayanth Dodderi Chidanand- ``is_feat_xxx_supported()`` and ``is_feat_xxx_present()`` - mandatory for all features.
4674efd2193SJayanth Dodderi Chidanand
4684efd2193SJayanth Dodderi Chidanand- ``xxx_enable(cpu_context * )`` and ``xxx_disable(cpu_context * )`` - optional
4694efd2193SJayanth Dodderi Chidanand  functions to enable the feature for the passed context only. To be called in
4704efd2193SJayanth Dodderi Chidanand  the respective world's setup_context to select behaviour.
4714efd2193SJayanth Dodderi Chidanand
4724efd2193SJayanth Dodderi Chidanand- ``xxx_init_el3()`` - optional function to enable the feature in-place in any EL3
4734efd2193SJayanth Dodderi Chidanand  registers that are never context switched. The values they write must never
4744efd2193SJayanth Dodderi Chidanand  change, otherwise the functions mentioned in previous point should be used.
4754efd2193SJayanth Dodderi Chidanand  Invoked from ``cm_manage_extensions_el3()``.
4764efd2193SJayanth Dodderi Chidanand
4774efd2193SJayanth Dodderi Chidanand- ``xxx_init_el2_unused()`` - optional function to enable the feature in-place
4784efd2193SJayanth Dodderi Chidanand  in any EL2 registers that are necessary for execution in EL1 with no EL2 present.
4794efd2193SJayanth Dodderi Chidanand
4804efd2193SJayanth Dodderi ChidanandThe above mentioned rules, followed for ``FEAT_SME`` is shown below:
4814efd2193SJayanth Dodderi Chidanand
4824efd2193SJayanth Dodderi Chidanand.. code:: c
4834efd2193SJayanth Dodderi Chidanand
4844efd2193SJayanth Dodderi Chidanand	void sme_enable(cpu_context_t *context);
4854efd2193SJayanth Dodderi Chidanand	void sme_init_el3(void);
4864efd2193SJayanth Dodderi Chidanand	void sme_init_el2_unused(void);
4874efd2193SJayanth Dodderi Chidanand	void sme_disable(cpu_context_t *context);
4884efd2193SJayanth Dodderi Chidanand
4894efd2193SJayanth Dodderi ChidanandPer-world Context
4904efd2193SJayanth Dodderi Chidanand=================
4914efd2193SJayanth Dodderi Chidanand
4924efd2193SJayanth Dodderi ChidanandApart from the CPU context structure, we have another structure to manage some
4934efd2193SJayanth Dodderi Chidanandof the EL3 system registers whose values are identical across all the CPUs
4944efd2193SJayanth Dodderi Chidanandreferred to as ``per_world_context_t``.
4954efd2193SJayanth Dodderi ChidanandThe Per-world context structure is intended for managing EL3 system registers with
4964efd2193SJayanth Dodderi Chidanandidentical values across all CPUs, requiring only a singular context entry for each
4974efd2193SJayanth Dodderi Chidanandindividual world. This structure operates independently of the CPU context
4984efd2193SJayanth Dodderi Chidanandstructure and is intended to manage specific EL3 registers.
4994efd2193SJayanth Dodderi Chidanand
5004efd2193SJayanth Dodderi Chidanand.. code-block:: c
5014efd2193SJayanth Dodderi Chidanand
5024efd2193SJayanth Dodderi Chidanand	typedef struct per_world_context {
5034efd2193SJayanth Dodderi Chidanand		uint64_t ctx_cptr_el3;
5044efd2193SJayanth Dodderi Chidanand		uint64_t ctx_zcr_el3;
5054efd2193SJayanth Dodderi Chidanand		uint64_t ctx_mpam3_el3;
5064efd2193SJayanth Dodderi Chidanand	} per_world_context_t;
5074efd2193SJayanth Dodderi Chidanand
5084efd2193SJayanth Dodderi ChidanandThese functions facilitate the activation of architectural extensions that possess
5094efd2193SJayanth Dodderi Chidanandidentical values across all cores for the individual Non-secure, Secure, and
5104efd2193SJayanth Dodderi ChidanandRealm worlds.
5114efd2193SJayanth Dodderi Chidanand
5124efd2193SJayanth Dodderi Chidanand*Copyright (c) 2024, Arm Limited and Contributors. All rights reserved.*
5134efd2193SJayanth Dodderi Chidanand
5144efd2193SJayanth Dodderi Chidanand.. |Context Memory Allocation| image:: ../resources/diagrams/context_memory_allocation.png
5154efd2193SJayanth Dodderi Chidanand.. |CPU Context Memory Configuration| image:: ../resources/diagrams/cpu_data_config_context_memory.png
5164efd2193SJayanth Dodderi Chidanand.. |CPU Data Structure| image:: ../resources/diagrams/percpu-data-struct.png
5174efd2193SJayanth Dodderi Chidanand.. |Context Init ColdBoot| image:: ../resources/diagrams/context_init_coldboot.png
5184efd2193SJayanth Dodderi Chidanand.. |Context Init WarmBoot| image:: ../resources/diagrams/context_init_warmboot.png
5194efd2193SJayanth Dodderi Chidanand.. _Trustzone for AArch64: https://developer.arm.com/documentation/102418/0101/TrustZone-in-the-processor/Switching-between-Security-states
5204efd2193SJayanth Dodderi Chidanand.. _Security States with RME: https://developer.arm.com/documentation/den0126/0100/Security-states
5214efd2193SJayanth Dodderi Chidanand.. _lib/el3_runtime/(aarch32/aarch64): https://git.trustedfirmware.org/TF-A/trusted-firmware-a.git/tree/lib/el3_runtime
522