aboutsummaryrefslogtreecommitdiff
path: root/gcc/config/aarch64/predicates.md
diff options
context:
space:
mode:
authorMatthew Malcomson <matthew.malcomson@arm.com>2020-07-09 09:11:59 +0100
committerMatthew Malcomson <matthew.malcomson@arm.com>2020-07-09 09:18:47 +0100
commit96b7f495f9269d5448822e4fc28882edb35a58d7 (patch)
tree64c0b9cf11b60e32e6b70c714395b8e16eff1ddf /gcc/config/aarch64/predicates.md
parentbe178ecd5ac1fe1510d960ff95c66d0ff831afe1 (diff)
downloadgcc-96b7f495f9269d5448822e4fc28882edb35a58d7.zip
gcc-96b7f495f9269d5448822e4fc28882edb35a58d7.tar.gz
gcc-96b7f495f9269d5448822e4fc28882edb35a58d7.tar.bz2
aarch64: Mitigate SLS for BLR instruction
This patch introduces the mitigation for Straight Line Speculation past the BLR instruction. This mitigation replaces BLR instructions with a BL to a stub which uses a BR to jump to the original value. These function stubs are then appended with a speculation barrier to ensure no straight line speculation happens after these jumps. When optimising for speed we use a set of stubs for each function since this should help the branch predictor make more accurate predictions about where a stub should branch. When optimising for size we use one set of stubs for all functions. This set of stubs can have human readable names, and we are using `__call_indirect_x<N>` for register x<N>. When BTI branch protection is enabled the BLR instruction can jump to a `BTI c` instruction using any register, while the BR instruction can only jump to a `BTI c` instruction using the x16 or x17 registers. Hence, in order to ensure this transformation is safe we mov the value of the original register into x16 and use x16 for the BR. As an example when optimising for size: a BLR x0 instruction would get transformed to something like BL __call_indirect_x0 where __call_indirect_x0 labels a thunk that contains __call_indirect_x0: MOV X16, X0 BR X16 <speculation barrier> The first version of this patch used local symbols specific to a compilation unit to try and avoid relocations. This was mistaken since functions coming from the same compilation unit can still be in different sections, and the assembler will insert relocations at jumps between sections. On any relocation the linker is permitted to emit a veneer to handle jumps between symbols that are very far apart. The registers x16 and x17 may be clobbered by these veneers. Hence the function stubs cannot rely on the values of x16 and x17 being the same as just before the function stub is called. Similar can be said for the hot/cold partitioning of single functions, so function-local stubs have the same restriction. This updated version of the patch never emits function stubs for x16 and x17, and instead forces other registers to be used. Given the above, there is now no benefit to local symbols (since they are not enough to avoid dealing with linker intricacies). This patch now uses global symbols with hidden visibility each stored in their own COMDAT section. This means stubs can be shared between compilation units while still avoiding the PLT indirection. This patch also removes the `__call_indirect_x30` stub (and function-local equivalent) which would simply jump back to the original location. The function-local stubs are emitted to the assembly output file in one chunk, which means we need not add the speculation barrier directly after each one. This is because we know for certain that the instructions directly after the BR in all but the last function stub will be from another one of these stubs and hence will not contain a speculation gadget. Instead we add a speculation barrier at the end of the sequence of stubs. The global stubs are emitted in COMDAT/.linkonce sections by themselves so that the linker can remove duplicates from multiple object files. This means they are not emitted in one chunk, and each one must include the speculation barrier. Another difference is that since the global stubs are shared across compilation units we do not know that all functions will be targeting an architecture supporting the SB instruction. Rather than provide multiple stubs for each architecture, we provide a stub that will work for all architectures -- using the DSB+ISB barrier. This mitigation does not apply for BLR instructions in the following places: - Some accesses to thread-local variables use a code sequence with a BLR instruction. This code sequence is part of the binary interface between compiler and linker. If this BLR instruction needs to be mitigated, it'd probably be best to do so in the linker. It seems that the code sequence for thread-local variable access is unlikely to lead to a Spectre Revalation Gadget. - PLT stubs are produced by the linker and each contain a BLR instruction. It seems that at most only after the last PLT stub a Spectre Revalation Gadget might appear. Testing: Bootstrap and regtest on AArch64 (with BOOT_CFLAGS="-mharden-sls=retbr,blr") Used a temporary hack(1) in gcc-dg.exp to use these options on every test in the testsuite, a slight modification to emit the speculation barrier after every function stub, and a script to check that the output never emitted a BLR, or unmitigated BR or RET instruction. Similar on an aarch64-none-elf cross-compiler. 1) Temporary hack emitted a speculation barrier at the end of every stub function, and used a script to ensure that: a) Every RET or BR is immediately followed by a speculation barrier. b) No BLR instruction is emitted by compiler. gcc/ChangeLog: * config/aarch64/aarch64-protos.h (aarch64_indirect_call_asm): New declaration. * config/aarch64/aarch64.c (aarch64_regno_regclass): Handle new stub registers class. (aarch64_class_max_nregs): Likewise. (aarch64_register_move_cost): Likewise. (aarch64_sls_shared_thunks): Global array to store stub labels. (aarch64_sls_emit_function_stub): New. (aarch64_create_blr_label): New. (aarch64_sls_emit_blr_function_thunks): New. (aarch64_sls_emit_shared_blr_thunks): New. (aarch64_asm_file_end): New. (aarch64_indirect_call_asm): New. (TARGET_ASM_FILE_END): Use aarch64_asm_file_end. (TARGET_ASM_FUNCTION_EPILOGUE): Use aarch64_sls_emit_blr_function_thunks. * config/aarch64/aarch64.h (STB_REGNUM_P): New. (enum reg_class): Add STUB_REGS class. (machine_function): Introduce `call_via` array for function-local stub labels. * config/aarch64/aarch64.md (*call_insn, *call_value_insn): Use aarch64_indirect_call_asm to emit code when hardening BLR instructions. * config/aarch64/constraints.md (Ucr): New constraint representing registers for indirect calls. Is GENERAL_REGS usually, and STUB_REGS when hardening BLR instruction against SLS. * config/aarch64/predicates.md (aarch64_general_reg): STUB_REGS class is also a general register. gcc/testsuite/ChangeLog: * gcc.target/aarch64/sls-mitigation/sls-miti-blr-bti.c: New test. * gcc.target/aarch64/sls-mitigation/sls-miti-blr.c: New test.
Diffstat (limited to 'gcc/config/aarch64/predicates.md')
-rw-r--r--gcc/config/aarch64/predicates.md3
1 files changed, 2 insertions, 1 deletions
diff --git a/gcc/config/aarch64/predicates.md b/gcc/config/aarch64/predicates.md
index 215fcec..1754b1e 100644
--- a/gcc/config/aarch64/predicates.md
+++ b/gcc/config/aarch64/predicates.md
@@ -32,7 +32,8 @@
(define_predicate "aarch64_general_reg"
(and (match_operand 0 "register_operand")
- (match_test "REGNO_REG_CLASS (REGNO (op)) == GENERAL_REGS")))
+ (match_test "REGNO_REG_CLASS (REGNO (op)) == STUB_REGS
+ || REGNO_REG_CLASS (REGNO (op)) == GENERAL_REGS")))
;; Return true if OP a (const_int 0) operand.
(define_predicate "const0_operand"