The HCR_EL2 (Hypervisor Configuration Register at EL2) is an ARM special register that plays a role in controlling the behavior of the system, particularly in virtualization environments. It is linked to the Branch Target Identification (BTI) and Branch Target Injection (BTI/TBI) security features, which are designed to mitigate certain types of speculative execution and return-oriented programming (ROP) attacks.
Link between HCR_EL2 and BTI/TBI Support
The HCR_EL2 register contains bits that influence the behavior of the processor, including how it handles branch instructions and speculative execution. Specifically:
BTI (Branch Target Identification):
BTI is a feature introduced in ARMv8.3-A to harden control flow integrity by ensuring that indirect branches only target valid instructions.
The HCR_EL2 register does not directly control BTI, but it influences the execution environment in which BTI operates, particularly in virtualized systems.
TBI (Top Byte Ignore):
TBI is a feature that allows the top byte of virtual addresses to be ignored, which is useful for memory tagging and other security mechanisms.
The HCR_EL2.TGE (Top Byte Ignore Enable) bit (bit 27) controls whether TBI is enabled for stage 2 translations in a virtualized environment.
Disabling BTI/TBI Support
To disable or modify the behavior related to BTI/TBI, you need to consider the following:
Disabling BTI:
BTI is controlled by the Branch Type Enable (bte) bit in the System Instruction Generate Interrupt (SCTLR_ELx) register.
To disable BTI, clear the bte bit in SCTLR_EL1 or SCTLR_EL2, depending on the exception level.
Example (for EL1):
assembly
mrs x0, SCTLR_EL1 // Read SCTLR_EL1
bic x0, x0, #0x400000 // Clear bte bit (bit 22)
msr SCTLR_EL1, x0 // Write back to SCTLR_EL1
Disabling TBI:
TBI is controlled by the HCR_EL2.TGE bit (bit 27) for stage 2 translations in a virtualized environment.
To disable TBI, clear the TGE bit in HCR_EL2.
Example:
assembly
mrs x0, HCR_EL2 // Read HCR_EL2
bic x0, x0, #0x8000000 // Clear TGE bit (bit 27)
msr HCR_EL2, x0 // Write back to HCR_EL2
Considerations
Virtualization: If you are working in a virtualized environment (EL2), changes to HCR_EL2 affect the behavior of the virtual machine. Ensure you have the necessary permissions to modify these registers.
Security Implications: Disabling BTI or TBI can weaken security protections against speculative execution attacks and memory corruption exploits. Only disable these features if absolutely necessary and ensure alternative mitigations are in place.
Summary
HCR_EL2 indirectly influences BTI/TBI by controlling the execution environment, especially in virtualization.
BTI is disabled by clearing the bte bit in SCTLR_ELx.
TBI is disabled by clearing the TGE bit in HCR_EL2 for stage 2 translations.
Always consult the ARM Architecture Reference Manual for your specific processor version to ensure correct register manipulation.