objtool: Add support for arm64
Subsystems (6)
v3 cover objtool: Add support for arm64
As of now, objtool only supports the x86_64 architecture but the groundwork has already been done in order to add support for other architectures without too much effort.
This series of patches adds support for the arm64 architecture based on the Armv8.5 Architecture Reference Manual.
Objtool will be a valuable tool to progress and provide more guarentees on live patching which is a work in progress for arm64.
Once we have the base of objtool working the next steps will be to port Peter Z's uaccess validation for arm64.
Changes since previous version:
* Rebased on tip/master: Note that I had to re-expose the struct alternative using check.h because it is now used outside of check.c.
* Reorder commits for a more coherent progression
* Introduce GCC plugin to help detect switch-tables for arm64 This plugins could be improve: It plugs in after the RTL control flow graph passes but only extract information about the switch tables. I originally intended for it to introduce new code_label/note within the RTL representation in order to reference them and thus get the address of the branch instruction. However I did not manage to do it properly using gen_rtx_CODE_LABEL/emit_label_before/after. If anyone has some experience with RTL plugins I am all ears for advices.
Raphael Gault (18):
objtool: Add abstraction for computation of symbols offsets
objtool: orc: Refactor ORC API for other architectures to implement.
objtool: Move registers and control flow to arch-dependent code
objtool: arm64: Add required implementation for supporting the aarch64
architecture in objtool.
objtool: special: Adapt special section handling
objtool: arm64: Adapt the stack frame checks for arm architecture
objtool: Introduce INSN_UNKNOWN type
objtool: Refactor switch-tables code to support other architectures
gcc-plugins: objtool: Add plugin to detect switch table on arm64
objtool: arm64: Implement functions to add switch tables alternatives
arm64: alternative: Mark .altinstr_replacement as containing
executable instructions
arm64: assembler: Add macro to annotate asm function having non
standard stack-frame.
arm64: sleep: Prevent stack frame warnings from objtool
arm64: kvm: Annotate non-standard stack frame functions
arm64: kernel: Add exception on kuser32 to prevent stack analysis
arm64: crypto: Add exceptions for crypto object to prevent stack
analysis
arm64: kernel: Annotate non-standard stack frame functions
objtool: arm64: Enable stack validation for arm64
arch/arm64/Kconfig | 1 +
arch/arm64/crypto/Makefile | 3 +
arch/arm64/include/asm/alternative.h | 2 +-
arch/arm64/include/asm/assembler.h | 13 +
arch/arm64/kernel/Makefile | 3 +
arch/arm64/kernel/hyp-stub.S | 2 +
arch/arm64/kernel/sleep.S | 4 +
arch/arm64/kvm/hyp-init.S | 2 +
arch/arm64/kvm/hyp/entry.S | 2 +
scripts/Makefile.gcc-plugins | 2 +
scripts/gcc-plugins/Kconfig | 9 +
.../arm64_switch_table_detection_plugin.c | 58 +
tools/objtool/Build | 2 -
tools/objtool/arch.h | 21 +-
tools/objtool/arch/arm64/Build | 8 +
tools/objtool/arch/arm64/arch_special.c | 173 +
tools/objtool/arch/arm64/bit_operations.c | 67 +
tools/objtool/arch/arm64/decode.c | 2809 +++++++++++++++++
.../objtool/arch/arm64/include/arch_special.h | 52 +
.../arch/arm64/include/asm/orc_types.h | 96 +
.../arch/arm64/include/bit_operations.h | 24 +
tools/objtool/arch/arm64/include/cfi.h | 74 +
.../objtool/arch/arm64/include/insn_decode.h | 210 ++
tools/objtool/arch/arm64/orc_dump.c | 26 +
tools/objtool/arch/arm64/orc_gen.c | 40 +
tools/objtool/arch/x86/Build | 3 +
tools/objtool/arch/x86/arch_special.c | 101 +
tools/objtool/arch/x86/decode.c | 16 +
tools/objtool/arch/x86/include/arch_special.h | 45 +
tools/objtool/{ => arch/x86/include}/cfi.h | 0
tools/objtool/{ => arch/x86}/orc_dump.c | 4 +-
tools/objtool/{ => arch/x86}/orc_gen.c | 104 +-
tools/objtool/check.c | 309 +-
tools/objtool/check.h | 10 +
tools/objtool/elf.c | 3 +-
tools/objtool/orc.h | 4 +-
tools/objtool/special.c | 28 +-
tools/objtool/special.h | 13 +-
38 files changed, 4119 insertions(+), 224 deletions(-)
create mode 100644 scripts/gcc-plugins/arm64_switch_table_detection_plugin.c
create mode 100644 tools/objtool/arch/arm64/Build
create mode 100644 tools/objtool/arch/arm64/arch_special.c
create mode 100644 tools/objtool/arch/arm64/bit_operations.c
create mode 100644 tools/objtool/arch/arm64/decode.c
create mode 100644 tools/objtool/arch/arm64/include/arch_special.h
create mode 100644 tools/objtool/arch/arm64/include/asm/orc_types.h
create mode 100644 tools/objtool/arch/arm64/include/bit_operations.h
create mode 100644 tools/objtool/arch/arm64/include/cfi.h
create mode 100644 tools/objtool/arch/arm64/include/insn_decode.h
create mode 100644 tools/objtool/arch/arm64/orc_dump.c
create mode 100644 tools/objtool/arch/arm64/orc_gen.c
create mode 100644 tools/objtool/arch/x86/arch_special.c
create mode 100644 tools/objtool/arch/x86/include/arch_special.h
rename tools/objtool/{ => arch/x86/include}/cfi.h (100%)
rename tools/objtool/{ => arch/x86}/orc_dump.c (98%)
rename tools/objtool/{ => arch/x86}/orc_gen.c (66%)Signature · 1 line
2.17.1
RFC v3 1/18 objtool: Add abstraction for computation of symbols offsets
The jump destination and relocation offset used previously are only reliable on x86_64 architecture. We abstract these computations by calling arch-dependent implementations.
--- tools/objtool/arch.h | 6 ++++++ tools/objtool/arch/x86/decode.c | 11 +++++++++++ tools/objtool/check.c | 15 ++++++++++----- 3 files changed, 27 insertions(+), 5 deletions(-)
tools/objtool/arch.h +6 -0
@@ -64,6 +64,8 @@ struct stack_op {struct op_src src; }; +struct instruction ; +void arch_initial_func_cfi_state (struct cfi_state *state); int arch_decode_instruction(struct elf *elf, struct section *sec,
@@ -73,4 +75,8 @@ int arch_decode_instruction(struct elf *elf, struct section *sec,bool arch_callee_saved_reg (unsigned char reg); +unsigned long arch_jump_destination (struct instruction *insn); + +unsigned long arch_dest_rela_offset (int addend); +#endif /* _ARCH_H */
tools/objtool/arch/x86/decode.c +11 -0
@@ -11,6 +11,7 @@#include "lib/inat.c" #include "lib/insn.c" +#include "../../check.h" #include "../../elf.h" #include "../../arch.h" #include "../../warn.h"
@@ -66,6 +67,11 @@ bool arch_callee_saved_reg(unsigned char reg) } } +unsigned long arch_dest_rela_offset (int addend) +{ +return addend +4 ; +} + int arch_decode_instruction(struct elf *elf, struct section *sec, unsigned long offset, unsigned int maxlen, unsigned int *len, unsigned char *type,
@@ -497,3 +503,8 @@ void arch_initial_func_cfi_state(struct cfi_state *state) state->regs[16 ].base =CFI_CFA ; state->regs[16 ].offset =-8 ; } + +unsigned long arch_jump_destination (struct instruction *insn) +{ +return insn->offset + insn->len + insn->immediate; +}
tools/objtool/check.c +10 -5
@@ -565,7 +565,7 @@ static int add_jump_destinations(struct objtool_file *file) insn->len);if (!rela) { dest_sec = insn->sec; - dest_off = insn->offset + insn->len + insn->immediate; + dest_off =arch_jump_destination (insn); }else if (rela->sym->type ==STT_SECTION ) { dest_sec = rela->sym->sec; dest_off = rela->addend +4 ;
@@ -659,7 +659,7 @@ static int add_call_destinations(struct objtool_file *file) rela =find_rela_by_dest_range (insn->sec, insn->offset, insn->len);if (!rela) { - dest_off = insn->offset + insn->len + insn->immediate; + dest_off =arch_jump_destination (insn); insn->call_dest =find_symbol_by_offset (insn->sec, dest_off);
@@ -672,14 +672,19 @@ static int add_call_destinations(struct objtool_file *file) } }else if (rela->sym->type ==STT_SECTION ) { +/* +* the original x86_64 code adds 4 to the rela->addend +* which is not needed on arm64 architecture. +*/ + dest_off =arch_dest_rela_offset (rela->addend); insn->call_dest =find_symbol_by_offset (rela->sym->sec, - rela->addend+4 ); + dest_off);if (!insn->call_dest || insn->call_dest->type !=STT_FUNC ) { -WARN_FUNC ("can't find call dest symbol at %s+0x%x" , +WARN_FUNC ("can't find call dest symbol at %s+0x%lx" , insn->sec, insn->offset, rela->sym->sec->name, - rela->addend +4 ); + dest_off);return -1 ; } }else
Signature · 1 line
2.17.1
RFC v3 2/18 objtool: orc: Refactor ORC API for other architectures to implement.
In reply to Raphael GaultThe ORC unwinder is only supported on x86 at the moment and should thus be in the x86 architecture code. In order not to break the whole structure in case another architecture decides to support the ORC unwinder via objtool we choose to let the implementation be done in the architecture dependent code.
---
tools/objtool/Build | 2 -
tools/objtool/arch.h | 3 +
tools/objtool/arch/x86/Build | 2 +
tools/objtool/{ => arch/x86}/orc_dump.c | 4 +-
tools/objtool/{ => arch/x86}/orc_gen.c | 104 ++++++++++++++++++++++--
tools/objtool/check.c | 99 +---------------------
tools/objtool/orc.h | 4 +-
7 files changed, 111 insertions(+), 107 deletions(-)
rename tools/objtool/{ => arch/x86}/orc_dump.c (98%)
rename tools/objtool/{ => arch/x86}/orc_gen.c (66%)tools/objtool/Build +0 -2
@@ -2,8 +2,6 @@ objtool-y += arch/$(SRCARCH)/ objtool-y += builtin-check.o objtool-y += builtin-orc.o objtool-y += check.o -objtool-y += orc_gen.o -objtool-y += orc_dump.o objtool-y += elf.o objtool-y += special.o objtool-y += objtool.o
tools/objtool/arch.h +3 -0
@@ -10,6 +10,7 @@#include <linux/list.h> #include "elf.h" #include "cfi.h" +#include "orc.h" #define INSN_JUMP_CONDITIONAL 1#define INSN_JUMP_UNCONDITIONAL 2
@@ -75,6 +76,8 @@ int arch_decode_instruction(struct elf *elf, struct section *sec,bool arch_callee_saved_reg (unsigned char reg); +int arch_orc_read_unwind_hints (struct objtool_file *file); +unsigned long arch_jump_destination (struct instruction *insn);unsigned long arch_dest_rela_offset (int addend);
tools/objtool/arch/x86/Build +2 -0
@@ -1,4 +1,6 @@ objtool-y += decode.o +objtool-y += orc_dump.o +objtool-y += orc_gen.o inat_tables_script = arch/x86/tools/gen-insn-attr-x86.awk inat_tables_maps = arch/x86/lib/x86-opcode-map.txt
tools/objtool/arch/x86/orc_dump.c renamed from tools/objtool/orc_dump.c +2 -2
@@ -4,8 +4,8 @@*/ #include <unistd.h> -#include "orc.h" -#include "warn.h" +#include "../../orc.h" +#include "../../warn.h" static const char *reg_name (unsigned int reg) {
tools/objtool/arch/x86/orc_gen.c renamed from tools/objtool/orc_gen.c +99 -5
120 lines not on this page · raw message
tools/objtool/check.c +3 -96
120 lines not on this page · raw message
tools/objtool/orc.h +2 -2
@@ -10,8 +10,8 @@struct objtool_file ; -int create_orc (struct objtool_file *file); -int create_orc_sections (struct objtool_file *file); +int arch_create_orc (struct objtool_file *file); +int arch_create_orc_sections (struct objtool_file *file);int orc_dump (const char *objname);
Signature · 7 lines
2.17.1 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
RFC v3 3/18 objtool: Move registers and control flow to arch-dependent code
In reply to Raphael GaultThe control flow information and register macro definitions were based on the x86_64 architecture but should be abstract so that each architecture can define the correct values for the registers, especially the registers related to the stack frame (Frame Pointer, Stack Pointer and possibly Return Address).
---
tools/objtool/arch/x86/include/arch_special.h | 36 +++++++++++++++++++
tools/objtool/{ => arch/x86/include}/cfi.h | 0
tools/objtool/check.h | 1 +
tools/objtool/special.c | 19 +---------
4 files changed, 38 insertions(+), 18 deletions(-)
create mode 100644 tools/objtool/arch/x86/include/arch_special.h
rename tools/objtool/{ => arch/x86/include}/cfi.h (100%)tools/objtool/arch/x86/include/arch_special.h new file +36 -0
@@ -0,0 +1,36 @@ +/* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, see <http://www.gnu.org/licenses/>. +*/ +#ifndef _X86_ARCH_SPECIAL_H +#define _X86_ARCH_SPECIAL_H + +#define EX_ENTRY_SIZE 12 +#define EX_ORIG_OFFSET 0 +#define EX_NEW_OFFSET 4 + +#define JUMP_ENTRY_SIZE 16 +#define JUMP_ORIG_OFFSET 0 +#define JUMP_NEW_OFFSET 4 + +#define ALT_ENTRY_SIZE 13 +#define ALT_ORIG_OFFSET 0 +#define ALT_NEW_OFFSET 4 +#define ALT_FEATURE_OFFSET 8 +#define ALT_ORIG_LEN_OFFSET 10 +#define ALT_NEW_LEN_OFFSET 11 + +#define X86_FEATURE_POPCNT (4 * 32 + 23) +#define X86_FEATURE_SMAP (9 * 32 + 20) + +#endif /* _X86_ARCH_SPECIAL_H */
tools/objtool/arch/x86/include/cfi.h renamed from tools/objtool/cfi.h +0 -0
tools/objtool/check.h +1 -0
@@ -11,6 +11,7 @@#include "cfi.h" #include "arch.h" #include "orc.h" +#include "arch_special.h" #include <linux/hashtable.h> struct insn_state {
tools/objtool/special.c +1 -18
@@ -14,24 +14,7 @@#include "builtin.h" #include "special.h" #include "warn.h" - -#define EX_ENTRY_SIZE 12 -#define EX_ORIG_OFFSET 0 -#define EX_NEW_OFFSET 4 - -#define JUMP_ENTRY_SIZE 16 -#define JUMP_ORIG_OFFSET 0 -#define JUMP_NEW_OFFSET 4 - -#define ALT_ENTRY_SIZE 13 -#define ALT_ORIG_OFFSET 0 -#define ALT_NEW_OFFSET 4 -#define ALT_FEATURE_OFFSET 8 -#define ALT_ORIG_LEN_OFFSET 10 -#define ALT_NEW_LEN_OFFSET 11 - -#define X86_FEATURE_POPCNT (4*32+23) -#define X86_FEATURE_SMAP (9*32+20) +#include "arch_special.h" struct special_entry {const char *sec;
Signature · 1 line
2.17.1
RFC v3 4/18 objtool: arm64: Add required implementation for supporting the aarch64 architecture in objtool.
In reply to Raphael GaultProvide implementation for the arch-dependent functions that are called by the main check function of objtool. The ORC unwinder is not yet supported by the arm64 architecture so we only provide a dummy interface for now. The decoding of the instruction is split into classes and subclasses as described into the Instruction Encoding in the ArmV8.5 Architecture Reference Manual.
In order to handle the load/store instructions for a pair of registers we add an extra field to the stack_op structure.
We consider that the hypervisor/secure-monitor is behaving correctly. This enables us to handle hvc/smc/svc context switching instructions as nop since we consider that the context is restored correctly.
--- tools/objtool/arch.h | 7 + tools/objtool/arch/arm64/Build | 7 + tools/objtool/arch/arm64/bit_operations.c | 67 + tools/objtool/arch/arm64/decode.c | 2781 +++++++++++++++++ .../objtool/arch/arm64/include/arch_special.h | 36 + .../arch/arm64/include/asm/orc_types.h | 96 + .../arch/arm64/include/bit_operations.h | 24 + tools/objtool/arch/arm64/include/cfi.h | 74 + .../objtool/arch/arm64/include/insn_decode.h | 211 ++ tools/objtool/arch/arm64/orc_dump.c | 26 + tools/objtool/arch/arm64/orc_gen.c | 40 + 11 files changed, 3369 insertions(+) create mode 100644 tools/objtool/arch/arm64/Build create mode 100644 tools/objtool/arch/arm64/bit_operations.c create mode 100644 tools/objtool/arch/arm64/decode.c create mode 100644 tools/objtool/arch/arm64/include/arch_special.h create mode 100644 tools/objtool/arch/arm64/include/asm/orc_types.h create mode 100644 tools/objtool/arch/arm64/include/bit_operations.h create mode 100644 tools/objtool/arch/arm64/include/cfi.h create mode 100644 tools/objtool/arch/arm64/include/insn_decode.h create mode 100644 tools/objtool/arch/arm64/orc_dump.c create mode 100644 tools/objtool/arch/arm64/orc_gen.c
tools/objtool/arch.h +7 -0
@@ -60,9 +60,16 @@ struct op_src {int offset; }; +struct op_extra { +unsigned char used; +unsigned char reg; +int offset; +}; +struct stack_op {struct op_dest dest;struct op_src src; +struct op_extra extra; };struct instruction ;
tools/objtool/arch/arm64/Build new file +7 -0
@@ -0,0 +1,7 @@ +objtool-y += decode.o +objtool-y += orc_dump.o +objtool-y += orc_gen.o +objtool-y += bit_operations.o + + +CFLAGS_decode.o += -I$(OUTPUT)arch/arm64/lib
tools/objtool/arch/arm64/bit_operations.c new file +67 -0
67 lines not on this page · raw message
tools/objtool/arch/arm64/decode.c new file +2781 -0
2,781 lines not on this page · raw message
tools/objtool/arch/arm64/include/arch_special.h new file +36 -0
@@ -0,0 +1,36 @@ +/* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, see <http://www.gnu.org/licenses/>. +*/ +#ifndef _ARM64_ARCH_SPECIAL_H +#define _ARM64_ARCH_SPECIAL_H + +#define EX_ENTRY_SIZE 8 +#define EX_ORIG_OFFSET 0 +#define EX_NEW_OFFSET 4 + +#define JUMP_ENTRY_SIZE 16 +#define JUMP_ORIG_OFFSET 0 +#define JUMP_NEW_OFFSET 4 + +#define ALT_ENTRY_SIZE 12 +#define ALT_ORIG_OFFSET 0 +#define ALT_NEW_OFFSET 4 +#define ALT_FEATURE_OFFSET 8 +#define ALT_ORIG_LEN_OFFSET 10 +#define ALT_NEW_LEN_OFFSET 11 + +#define X86_FEATURE_POPCNT (4 * 32 + 23) +#define X86_FEATURE_SMAP (9 * 32 + 20) + +#endif /* _ARM64_ARCH_SPECIAL_H */
tools/objtool/arch/arm64/include/asm/orc_types.h new file +96 -0
93 lines not on this page · raw message
tools/objtool/arch/arm64/include/bit_operations.h new file +24 -0
Lines of this file are not shown. View the raw message.
tools/objtool/arch/arm64/include/cfi.h new file +74 -0
74 lines not on this page · raw message
tools/objtool/arch/arm64/include/insn_decode.h new file +211 -0
211 lines not on this page · raw message
tools/objtool/arch/arm64/orc_dump.c new file +26 -0
Lines of this file are not shown. View the raw message.
tools/objtool/arch/arm64/orc_gen.c new file +40 -0
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.17.1
Part of the diff is not shown here. View the raw message.
RFC v3 5/18 objtool: special: Adapt special section handling
In reply to Raphael GaultThis patch abstracts the few architecture dependent tests that are perform when handling special section and switch tables. It enables any architecture to ignore a particular CPU feature or not to handle switch tables.
--- tools/objtool/arch/arm64/Build | 1 + tools/objtool/arch/arm64/arch_special.c | 22 +++++++++++++++ .../objtool/arch/arm64/include/arch_special.h | 10 +++++-- tools/objtool/arch/x86/Build | 1 + tools/objtool/arch/x86/arch_special.c | 28 +++++++++++++++++++ tools/objtool/arch/x86/include/arch_special.h | 9 ++++++ tools/objtool/check.c | 15 ++++++++-- tools/objtool/special.c | 9 ++---- tools/objtool/special.h | 3 ++ 9 files changed, 87 insertions(+), 11 deletions(-) create mode 100644 tools/objtool/arch/arm64/arch_special.c create mode 100644 tools/objtool/arch/x86/arch_special.c
tools/objtool/arch/arm64/Build +1 -0
@@ -1,3 +1,4 @@ +objtool-y += arch_special.o objtool-y += decode.o objtool-y += orc_dump.o objtool-y += orc_gen.o
tools/objtool/arch/arm64/arch_special.c new file +22 -0
@@ -0,0 +1,22 @@ +/* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, see <http://www.gnu.org/licenses/>. +*/ +#include "../../special.h" +#include "arch_special.h" + +void arch_force_alt_path (unsigned short feature, +bool uaccess, +struct special_alt *alt) +{ +}
tools/objtool/arch/arm64/include/arch_special.h +8 -2
@@ -30,7 +30,13 @@#define ALT_ORIG_LEN_OFFSET 10#define ALT_NEW_LEN_OFFSET 11 -#define X86_FEATURE_POPCNT (4 * 32 + 23) -#define X86_FEATURE_SMAP (9 * 32 + 20) +static inline bool arch_should_ignore_feature (unsigned short feature) +{ +return false; +} +static inline bool arch_support_switch_table (void ) +{ +return false; +}#endif /* _ARM64_ARCH_SPECIAL_H */
tools/objtool/arch/x86/Build +1 -0
@@ -1,3 +1,4 @@ +objtool-y += arch_special.o objtool-y += decode.o objtool-y += orc_dump.o objtool-y += orc_gen.o
tools/objtool/arch/x86/arch_special.c new file +28 -0
@@ -0,0 +1,28 @@ +/* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, see <http://www.gnu.org/licenses/>. +*/ +#include "../../special.h" +#include "arch_special.h" + +void arch_force_alt_path (unsigned short feature, +bool uaccess, +struct special_alt *alt) +{ +if (feature ==X86_FEATURE_SMAP ) { +if (uaccess) + alt->skip_orig = true; +else + alt->skip_alt = true; + } +}
tools/objtool/arch/x86/include/arch_special.h +9 -0
@@ -33,4 +33,13 @@#define X86_FEATURE_POPCNT (4 * 32 + 23)#define X86_FEATURE_SMAP (9 * 32 + 20) +static inline bool arch_should_ignore_feature (unsigned short feature) +{ +return feature ==X86_FEATURE_POPCNT ; +} + +static inline bool arch_support_switch_table (void ) +{ +return true; +}#endif /* _X86_ARCH_SPECIAL_H */
tools/objtool/check.c +13 -2
@@ -729,7 +729,7 @@ static int handle_group_alt(struct objtool_file *file, last_orig_insn = insn; } -if (next_insn_same_sec (file, last_orig_insn)) { +if (last_orig_insn &&next_insn_same_sec (file, last_orig_insn)) { fake_jump =malloc (sizeof (*fake_jump));if (!fake_jump) {WARN ("malloc failed" );
@@ -1045,6 +1045,17 @@ static struct rela *find_switch_table(struct objtool_file *file,if (find_symbol_containing (rodata_sec, table_offset))continue ; +/* +* If we are on arm64 architecture, we now that we +* are in presence of a switch table thanks to +* the `br <Xn>` insn. but we can't retrieve it yet. +* So we just ignore unreachable for this file. +*/ +if (!arch_support_switch_table ()) { + file->ignore_unreachables = true; +return NULL ; + } + rodata_rela =find_rela_by_dest (rodata_sec, table_offset);if (rodata_rela) {/*
@@ -1844,7 +1855,7 @@ static int validate_branch(struct objtool_file *file, struct instruction *first, insn = first; sec = insn->sec; -if (insn->alt_group &&list_empty (&insn->alts)) { +if (!insn->visited && insn->alt_group &&list_empty (&insn->alts)) {WARN_FUNC ("don't know how to handle branch to middle of alternative instruction group" , sec, insn->offset);return 1 ;
tools/objtool/special.c +2 -7
@@ -81,7 +81,7 @@ static int get_alt_entry(struct elf *elf, struct special_entry *entry,* feature path which is a "very very small percentage of * machines". */ -if (feature ==X86_FEATURE_POPCNT ) +if (arch_should_ignore_feature (feature)) alt->skip_orig = true;/*
@@ -93,12 +93,7 @@ static int get_alt_entry(struct elf *elf, struct special_entry *entry,* find paths that see the STAC but take the NOP instead of * CLAC and the other way around. */ -if (feature ==X86_FEATURE_SMAP ) { -if (uaccess) - alt->skip_orig = true; -else - alt->skip_alt = true; - } +arch_force_alt_path (feature, uaccess, alt); } orig_rela =find_rela_by_dest (sec, offset + entry->orig);
tools/objtool/special.h +3 -0
@@ -27,5 +27,8 @@ struct special_alt { };int special_get_alts (struct elf *elf,struct list_head *alts); +void arch_force_alt_path (unsigned short feature, +bool uaccess, +struct special_alt *alt);#endif /* _SPECIAL_H */
Signature · 1 line
2.17.1
RFC v3 6/18 objtool: arm64: Adapt the stack frame checks for arm architecture
In reply to Raphael GaultSince the way the initial stack frame when entering a function is different that what is done in the x86_64 architecture, we need to add some more check to support the different cases. As opposed as for x86_64, the return address is not stored by the call instruction but is instead loaded in a register. The initial stack frame is thus empty when entering a function and 2 push operations are needed to set it up correctly. All the different combinations need to be taken into account.
--- tools/objtool/arch.h | 2 + tools/objtool/arch/arm64/decode.c | 28 +++++++++ tools/objtool/arch/x86/decode.c | 5 ++ tools/objtool/check.c | 100 ++++++++++++++++++++++++++++-- tools/objtool/elf.c | 3 +- 5 files changed, 131 insertions(+), 7 deletions(-)
tools/objtool/arch.h +2 -0
@@ -89,4 +89,6 @@ unsigned long arch_jump_destination(struct instruction *insn);unsigned long arch_dest_rela_offset (int addend); +bool arch_is_insn_sibling_call (struct instruction *insn); +#endif /* _ARCH_H */
tools/objtool/arch/arm64/decode.c +28 -0
@@ -106,6 +106,34 @@ unsigned long arch_dest_rela_offset(int addend)return addend; } +/* +* In order to know if we are in presence of a sibling +* call and not in presence of a switch table we look +* back at the previous instructions and see if we are +* jumping inside the same function that we are already +* in. +*/ +bool arch_is_insn_sibling_call (struct instruction *insn) +{ +struct instruction *prev; +struct list_head *l; +struct symbol *sym; +list_for_each_prev (l, &insn->list) { + prev = list_entry(l, struct instruction, list); +if (!prev->func || + prev->func->pfunc != insn->func->pfunc) +return false; +if (prev->stack_op.src.reg !=ADR_SOURCE ) +continue ; + sym =find_symbol_containing (insn->sec, insn->immediate); +if (!sym || sym->type !=STT_FUNC ) +return false; +else if (sym->type ==STT_FUNC ) +return true; +break ; + } +return false; +}static int is_arm64 (struct elf *elf) {switch (elf->ehdr.e_machine) {
tools/objtool/arch/x86/decode.c +5 -0
@@ -72,6 +72,11 @@ unsigned long arch_dest_rela_offset(int addend)return addend +4 ; } +bool arch_is_insn_sibling_call (struct instruction *insn) +{ +return true; +} + int arch_decode_instruction(struct elf *elf, struct section *sec, unsigned long offset, unsigned int maxlen, unsigned int *len, unsigned char *type,
tools/objtool/check.c +94 -6
182 lines not on this page · raw message
tools/objtool/elf.c +2 -1
@@ -62,7 +62,8 @@ struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset)struct symbol *sym;list_for_each_entry (sym, &sec->symbol_list, list) -if (sym->type !=STT_SECTION && +if (sym->type !=STT_NOTYPE && + sym->type !=STT_SECTION && sym->offset == offset)return sym;
Signature · 1 line
2.17.1
RFC v3 7/18 objtool: Introduce INSN_UNKNOWN type
In reply to Raphael GaultOn arm64 some object files contain data stored in the .text section. This data is interpreted by objtool as instruction but can't be identified as a valid one. In order to keep analysing those files we introduce INSN_UNKNOWN type. The "unknown instruction" warning will thus only be raised if such instructions are uncountered while validating an execution branch.
This change doesn't impact the x86 decoding logic since 0 is still used as a way to specify an unknown type, raising the "unknown instruction" warning during the decoding phase still.
--- tools/objtool/arch.h | 3 ++- tools/objtool/arch/arm64/decode.c | 8 ++++---- tools/objtool/arch/arm64/include/insn_decode.h | 4 ++-- tools/objtool/check.c | 10 +++++++++- 4 files changed, 17 insertions(+), 8 deletions(-)
tools/objtool/arch.h +2 -1
@@ -26,7 +26,8 @@#define INSN_CLAC 12#define INSN_STD 13#define INSN_CLD 14 -#define INSN_OTHER 15 +#define INSN_UNKNOWN 15 +#define INSN_OTHER 16#define INSN_LAST INSN_OTHERenum op_dest_type {
tools/objtool/arch/arm64/decode.c +4 -4
@@ -37,9 +37,9 @@*/ static arm_decode_class aarch64_insn_class_decode_table[] = { [INSN_RESERVED ] = arm_decode_reserved, - [INSN_UNKNOWN ] = arm_decode_unknown, + [INSN_UNALLOC_1 ] = arm_decode_unknown, [INSN_SVE_ENC ] = arm_decode_sve_encoding, - [INSN_UNALLOC ] = arm_decode_unknown, + [INSN_UNALLOC_2 ] = arm_decode_unknown, [INSN_LD_ST_4 ] = arm_decode_ld_st, [INSN_DP_REG_5 ] = arm_decode_dp_reg, [INSN_LD_ST_6 ] = arm_decode_ld_st,
@@ -191,7 +191,7 @@ int arch_decode_instruction(struct elf *elf, struct section *sec,int arm_decode_unknown (u32 instr,unsigned char *type,unsigned long *immediate,struct stack_op *op) { - *type =0 ; + *type =INSN_UNKNOWN ;return 0 ; }
@@ -206,7 +206,7 @@ int arm_decode_reserved(u32 instr, unsigned char *type, unsigned long *immediate, struct stack_op *op) { *immediate = instr &ONES (16 ); - *type =INSN_BUG ; + *type =INSN_UNKNOWN ;return 0 ; }
tools/objtool/arch/arm64/include/insn_decode.h +2 -2
@@ -20,9 +20,9 @@#include "../../../arch.h" #define INSN_RESERVED 0b0000 -#define INSN_UNKNOWN 0b0001 +#define INSN_UNALLOC_1 0b0001#define INSN_SVE_ENC 0b0010 -#define INSN_UNALLOC 0b0011 +#define INSN_UNALLOC_2 0b0011#define INSN_DP_IMM 0b1001 //0x100x#define INSN_BRANCH 0b1011 //0x101x#define INSN_LD_ST_4 0b0100 //0bx1x0
tools/objtool/check.c +9 -1
@@ -1952,6 +1952,13 @@ static int validate_branch(struct objtool_file *file, struct instruction *first,while (1 ) { next_insn =next_insn_same_sec (file, insn); +if (insn->type ==INSN_UNKNOWN ) { +WARN ("%s+0x%lx unknown instruction type, should never be reached" , + insn->sec->name, + insn->offset); +return 1 ; + } +if (file->c_file && func && insn->func && func != insn->func->pfunc) {WARN ("%s() falls through to next function %s()" , func->name, insn->func->name);
@@ -2383,7 +2390,8 @@ static int validate_reachable_instructions(struct objtool_file *file)return 0 ;for_each_insn (file, insn) { -if (insn->visited ||ignore_unreachable_insn (insn)) +if (insn->visited ||ignore_unreachable_insn (insn) || + insn->type ==INSN_UNKNOWN )continue ;WARN_FUNC ("unreachable instruction" , insn->sec, insn->offset);
Signature · 1 line
2.17.1
RFC v3 8/18 objtool: Refactor switch-tables code to support other architectures
In reply to Raphael GaultThe way to identify switch-tables and retrieves all the data necessary to handle the different execution branches is not the same on all architecture. In order to be able to add other architecture support, this patch defines arch-dependent functions to process jump-tables.
--- tools/objtool/arch/arm64/arch_special.c | 15 +++++ tools/objtool/arch/x86/arch_special.c | 73 +++++++++++++++++++++ tools/objtool/check.c | 84 +------------------------ tools/objtool/check.h | 7 +++ tools/objtool/special.h | 10 ++- 5 files changed, 107 insertions(+), 82 deletions(-)
tools/objtool/arch/arm64/arch_special.c +15 -0
@@ -20,3 +20,18 @@ void arch_force_alt_path(unsigned short feature, struct special_alt *alt) { } + +int arch_add_switch_table (struct objtool_file *file,struct instruction *insn, +struct rela *table,struct rela *next_table) +{ +return 0 ; +} + +struct rela *arch_find_switch_table (struct objtool_file *file, +struct rela *text_rela, +struct section *rodata_sec, +unsigned long table_offset) +{ + file->ignore_unreachables = true; +return NULL ; +}
tools/objtool/arch/x86/arch_special.c +73 -0
76 lines not on this page · raw message
tools/objtool/check.c +3 -81
114 lines not on this page · raw message
tools/objtool/check.h +7 -0
@@ -13,6 +13,7 @@#include "orc.h" #include "arch_special.h" #include <linux/hashtable.h> +;struct insn_state {struct cfi_reg cfa;
@@ -46,6 +47,12 @@ struct instruction {struct orc_entry orc; }; +struct alternative { +struct list_head list; +struct instruction *insn; +bool skip_orig; +}; +struct objtool_file {struct elf *elf;struct list_head insn_list;
tools/objtool/special.h +9 -1
@@ -7,7 +7,10 @@#define _SPECIAL_H#include <stdbool.h> +#include <stdlib.h> +#include "check.h" #include "elf.h" +#include "warn.h" struct special_alt {struct list_head list;
@@ -30,5 +33,10 @@ int special_get_alts(struct elf *elf, struct list_head *alts);void arch_force_alt_path (unsigned short feature,bool uaccess,struct special_alt *alt); - +int arch_add_switch_table (struct objtool_file *file,struct instruction *insn, +struct rela *table,struct rela *next_table); +struct rela *arch_find_switch_table (struct objtool_file *file, +struct rela *text_rela, +struct section *rodata_sec, +unsigned long table_offset);#endif /* _SPECIAL_H */
Signature · 1 line
2.17.1
RFC v3 9/18 gcc-plugins: objtool: Add plugin to detect switch table on arm64
In reply to Raphael GaultThis plugins comes into play before the final 2 RTL passes of GCC and detects switch-tables that are to be outputed in the ELF and writes information in an "objtool_data" section which will be used by objtool.
--- scripts/Makefile.gcc-plugins | 2 + scripts/gcc-plugins/Kconfig | 9 +++ .../arm64_switch_table_detection_plugin.c | 58 +++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 scripts/gcc-plugins/arm64_switch_table_detection_plugin.c
scripts/Makefile.gcc-plugins +2 -0
@@ -44,6 +44,8 @@ ifdef CONFIG_GCC_PLUGIN_ARM_SSP_PER_TASK endif export DISABLE_ARM_SSP_PER_TASK_PLUGIN +gcc-plugin-$(CONFIG_GCC_PLUGIN_SWITCH_TABLES) += arm64_switch_table_detection_plugin.so + # All the plugin CFLAGS are collected here in case a build target needs to # filter them out of the KBUILD_CFLAGS. GCC_PLUGINS_CFLAGS := $(strip $(addprefix -fplugin=$(objtree)/scripts/gcc-plugins/, $(gcc-plugin-y)) $(gcc-plugin-cflags-y))
scripts/gcc-plugins/Kconfig +9 -0
@@ -113,4 +113,13 @@ config GCC_PLUGIN_ARM_SSP_PER_TASK bool depends on GCC_PLUGINS && ARM +config GCC_PLUGIN_SWITCH_TABLES + bool "GCC Plugin: Identify switch tables at compile time" + default y + depends on STACK_VALIDATION && ARM64 + help + Plugin to identify switch tables generated at compile time and store + them in a .objtool_data section. Objtool will then use that section + to analyse the different execution path of the switch table. + endmenu
scripts/gcc-plugins/arm64_switch_table_detection_plugin.c new file +58 -0
58 lines not on this page · raw message
Signature · 1 line
2.17.1
RFC v3 10/18 objtool: arm64: Implement functions to add switch tables alternatives
In reply to Raphael GaultThis patch implements the functions required to identify and add as alternatives all the possible destinations of the switch table. This implementation relies on the new plugin introduced previously which records information about the switch-table in a .objtool_data section.
--- tools/objtool/arch/arm64/arch_special.c | 142 +++++++++++++++++- tools/objtool/arch/arm64/decode.c | 2 +- .../objtool/arch/arm64/include/arch_special.h | 10 ++ .../objtool/arch/arm64/include/insn_decode.h | 3 +- tools/objtool/check.c | 8 +- tools/objtool/check.h | 2 + 6 files changed, 157 insertions(+), 10 deletions(-)
tools/objtool/arch/arm64/arch_special.c +139 -3
@@ -12,8 +12,13 @@* You should have received a copy of the GNU General Public License * along with this program; if not, see <http://www.gnu.org/licenses/>. */ + +#include <stdlib.h> +#include <string.h> +#include "../../special.h" #include "arch_special.h" +#include "bit_operations.h" void arch_force_alt_path(unsigned short feature, bool uaccess,
@@ -21,9 +26,141 @@ void arch_force_alt_path(unsigned short feature, { } +static u32 next_offset (u8 *table,u8 entry_size) +{ +switch (entry_size) { +case 1 : +return table[0 ]; +case 2 : +return *(u16 *)(table); +default : +return *(u32 *)(table); + } +} + +static u32 get_table_entry_size (u32 insn) +{ +unsigned char size = (insn >>30 ) &ONES (2 ); +switch (size) { +case 0 : +return 1 ; +case 1 : +return 2 ; +default : +return 4 ; + } +} + +static int add_possible_branch (struct objtool_file *file, +struct instruction *insn, +u32 base,u32 offset) +{ +struct instruction *new_insn; +struct alternative *alt; + offset = base +4 * offset; + new_insn =calloc (1 ,sizeof (*new_insn)); + +if (new_insn ==NULL ) { +WARN ("allocation failure, can't add jump alternative" ); +return -1 ; + } + +memcpy (new_insn, insn,sizeof (*insn)); + alt =calloc (1 ,sizeof (*alt)); + +if (new_insn ==NULL ) { +WARN ("allocation failure, can't add jump alternative" ); +return -1 ; + } + + new_insn->type =INSN_JUMP_UNCONDITIONAL ; + new_insn->immediate = offset; +INIT_LIST_HEAD (&new_insn->alts); + new_insn->jump_dest =find_insn (file, insn->sec, offset); + alt->insn = new_insn; + alt->skip_orig = true; +list_add_tail (&alt->list, &insn->alts); +list_add_tail (&new_insn->list, &file->insn_list); +return 0 ; +} +int arch_add_switch_table (struct objtool_file *file,struct instruction *insn, -struct rela *table,struct rela *next_table) +struct rela *table,struct rela *next_table) { +struct rela *objtool_data_rela =NULL ; +struct switch_table_info *swt_info =NULL ; +struct section *objtool_data =find_section_by_name (file->elf,".objtool_data" ); +struct section *rodata_sec =find_section_by_name (file->elf,".rodata" ); +struct section *branch_sec =NULL ; +u8 *switch_table =NULL ; +u64 base_offset =0 ; +struct instruction *pre_jump_insn; +u32 sec_size =0 ; +u32 entry_size =0 ; +u32 offset =0 ; +u32 i, j; + +if (objtool_data ==NULL ) +return 0 ; + +/* +* 1. Using rela, Identify entry for the switch table +* 2. Retrieve base offset +* 3. Retrieve branch instruction +* 3. For all entries in switch table: +* 3.1. Compute new offset +* 3.2. Create alternative instruction +* 3.3. Add alt_instr to insn->alts list +*/ + sec_size = objtool_data->sh.sh_size; +for (i =0 , swt_info = (void *)objtool_data->data->d_buf; + i < sec_size /sizeof (struct switch_table_info ); + i++, swt_info++) { + offset = i *sizeof (struct switch_table_info ); + objtool_data_rela =find_rela_by_dest_range (objtool_data, offset, +sizeof (u64)); +/* retrieving the objtool data of the switch table we need */ +if (objtool_data_rela ==NULL || + table->sym->sec != objtool_data_rela->sym->sec || + table->addend != objtool_data_rela->addend) +continue ; + +/* retrieving switch table content */ + switch_table = (u8 *)(rodata_sec->data->d_buf + table->addend); + +/* retrieving pre jump instruction (ldr) */ + branch_sec = insn->sec; + pre_jump_insn =find_insn (file, branch_sec, + insn->offset -3 *sizeof (u32)); + entry_size =get_table_entry_size (*(u32 *)(branch_sec->data->d_buf + pre_jump_insn->offset)); + +/* +* iterating over the pre-jumps instruction in order to +* retrieve switch base offset. +*/ +while (pre_jump_insn !=NULL && + pre_jump_insn->offset <= insn->offset) { +if (pre_jump_insn->stack_op.src.reg ==ADR_SOURCE ) { + base_offset = pre_jump_insn->offset + + pre_jump_insn->immediate; +/* +* Once we have the switch table entry size +* we add every possible destination using +* alternatives of the original branch +* instruction +*/ +for (j =0 ; j < swt_info->nb_entries; j++) { +if (add_possible_branch (file, insn, + base_offset, +next_offset (switch_table, entry_size))) { +return -1 ; + } + switch_table += entry_size; + } + } + pre_jump_insn =next_insn_same_sec (file, pre_jump_insn); + } + }return 0 ; }
@@ -32,6 +169,5 @@ struct rela *arch_find_switch_table(struct objtool_file *file,struct section *rodata_sec, unsigned long table_offset) { - file->ignore_unreachables = true; -return NULL ; + return text_rela; }
tools/objtool/arch/arm64/decode.c +1 -1
@@ -613,7 +613,7 @@ int arm_decode_except_gen(u32 instr, unsigned char *type,return 0 ;case INSN_BRK :if (imm16 ==0x800 ) - *type =INSN_BUG ; + *type =INSN_NOP ;else if (imm16 ==0x100 || imm16 >=0x900 ) *type =INSN_CONTEXT_SWITCH ;else
tools/objtool/arch/arm64/include/arch_special.h +10 -0
@@ -15,6 +15,8 @@#ifndef _ARM64_ARCH_SPECIAL_H#define _ARM64_ARCH_SPECIAL_H +#include <linux/types.h> +#define EX_ENTRY_SIZE 8#define EX_ORIG_OFFSET 0#define EX_NEW_OFFSET 4
@@ -30,6 +32,14 @@#define ALT_ORIG_LEN_OFFSET 10#define ALT_NEW_LEN_OFFSET 11 +#define ADR_SOURCE 255 + +struct switch_table_info { +u64 switch_table_label; +u64 nb_entries; +u64 offset_unsigned; +} __attribute__((__packed__ )); +static inline bool arch_should_ignore_feature (unsigned short feature) {return false;
tools/objtool/arch/arm64/include/insn_decode.h +1 -2
@@ -18,6 +18,7 @@#define _ARM_INSN_DECODE_H#include "../../../arch.h" +#include "arch_special.h" #define INSN_RESERVED 0b0000#define INSN_UNALLOC_1 0b0001
@@ -58,8 +59,6 @@#define COMPOSED_INSN_REGS_NUM 2#define INSN_COMPOSED 1 -#define ADR_SOURCE -1 -typedef int (*arm_decode_class )(u32 instr,unsigned char *type,unsigned long *immediate,struct stack_op *op);
tools/objtool/check.c +4 -4
@@ -33,8 +33,8 @@ struct instruction *find_insn(struct objtool_file *file,return NULL ; } -static struct instruction *next_insn_same_sec (struct objtool_file *file, -struct instruction *insn) +struct instruction *next_insn_same_sec (struct objtool_file *file, +struct instruction *insn) {struct instruction *next =list_next_entry (insn, list);
@@ -942,7 +942,7 @@ static struct rela *find_switch_table(struct objtool_file *file,struct symbol *func, struct instruction *insn) { - struct rela *text_rela, *rodata_rela; + struct rela *text_rela;struct instruction *orig_insn = insn;struct section *rodata_sec;unsigned long table_offset;
@@ -1840,7 +1840,7 @@ static int validate_sibling_call(struct instruction *insn, struct insn_state *st {if (arch_is_insn_sibling_call (insn) &&has_modified_stack_frame (state)) {WARN_FUNC ("sibling call from callable instruction with modified stack frame" , - insn->sec, insn->offset); + insn->sec, insn->offset);return 1 ; }
tools/objtool/check.h +2 -0
@@ -64,6 +64,8 @@ int check(const char *objname, bool orc);struct instruction *find_insn (struct objtool_file *file,struct section *sec,unsigned long offset); +struct instruction *next_insn_same_sec (struct objtool_file *file, +struct instruction *insn);#define for_each_insn (file, insn) \ list_for_each_entry(insn, &file->insn_list, list)
Signature · 1 line
2.17.1
RFC v3 11/18 arm64: alternative: Mark .altinstr_replacement as containing executable instructions
In reply to Raphael GaultUntil now, the section .altinstr_replacement wasn't marked as containing executable instructions on arm64. This patch changes that so that it is coherent with what is done on x86.
--- arch/arm64/include/asm/alternative.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/include/asm/alternative.h +1 -1
@@ -71,7 +71,7 @@ static inline void apply_alternatives_module(void *start, size_t length) { } ALTINSTR_ENTRY(feature,cb) \".popsection\n" \ " .if " __stringify(cb) " == 0\n" \ -".pushsection .altinstr_replacement, \"a\"\n" \ +".pushsection .altinstr_replacement, \"ax\"\n" \"663:\n\t" \ newinstr"\n" \"664:\n\t" \
Signature · 1 line
2.17.1
Catalin Marinas
maintainerRe: [RFC V3 11/18] arm64: alternative: Mark .altinstr_replacement as containing executable instructions
Quoted 14 lines · Raphael Gault
Until now, the section .altinstr_replacement wasn't marked as containing executable instructions on arm64. This patch changes that so that it is coherent with what is done on x86.
Signed-off-by Raphael Gault <[email protected]>--- arch/arm64/include/asm/alternative.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/arm64/include/asm/alternative.h +0
-0
arch/arm64/include/asm/alternative.h static inline void apply_alternatives_module(void *start, size_t length) { } · line 77
Quoted 8 lines
arch/arm64/include/asm/alternative.h +1
-1@@ -71,7 +71,7 @@ static inline void apply_alternatives_module(void *start, size_t length) { } ALTINSTR_ENTRY(feature,cb) \".popsection\n" \ " .if " __stringify(cb) " == 0\n" \ -".pushsection .altinstr_replacement, \"a\"\n" \ +".pushsection .altinstr_replacement, \"ax\"\n" \"663:\n\t" \ newinstr"\n" \"664:\n\t" \
I guess that's an inconsistency we missed since the asm macro has "ax".
Signature · 1 line
Catalin
RFC v3 12/18 arm64: assembler: Add macro to annotate asm function having non standard stack-frame.
In reply to Raphael GaultSome functions don't have standard stack-frames but are intended this way. In order for objtool to ignore those particular cases we add a macro that enables us to annotate the cases we chose to mark as particular.
--- arch/arm64/include/asm/assembler.h | 13 +++++++++++++ 1 file changed, 13 insertions(+)
arch/arm64/include/asm/assembler.h +13 -0
@@ -752,4 +752,17 @@ USER(\label, ic ivau, \tmp2) // invalidate I line PoU .Lyield_out_\@ : .endm + /* + * This macro is the arm64 assembler equivalent of the + * macro STACK_FRAME_NON_STANDARD define at + * ~/include/linux/frame.h + */ + .macro asm_stack_frame_non_standard func +#ifdef CONFIG_STACK_VALIDATION + .pushsection ".discard.func_stack_frame_non_standard" + .8byte \func + .popsection +#endif + .endm +#endif /* __ASM_ASSEMBLER_H */
Signature · 1 line
2.17.1
Catalin Marinas
maintainerRe: [RFC V3 12/18] arm64: assembler: Add macro to annotate asm function having non standard stack-frame.
Quoted 5 lines · Raphael Gault
arch/arm64/include/asm/assembler.h +0
-0@@ -752,4 +752,17 @@ USER(\label, ic ivau, \tmp2) // invalidate I line PoU .Lyield_out_\@ : .endm
arch/arm64/include/asm/assembler.h USER(\label, ic ivau, \tmp2) // invalidate I line PoU · line 763
Quoted 10 lines
arch/arm64/include/asm/assembler.h +9
-0+/* +* This macro is the arm64 assembler equivalent of the +* macro STACK_FRAME_NON_STANDARD define at +* ~/include/linux/frame.h +*/ + .macro asm_stack_frame_non_standard func +#ifdef CONFIG_STACK_VALIDATION + .pushsection ".discard.func_stack_frame_non_standard" + .8byte \func
Nitpicks:
Does .quad vs .8byte make any difference?
Could we place this in include/linux/frame.h directly with a generic name (and some __ASSEMBLY__ guards)? It doesn't look to be arm specific.
Signature · 1 line
Catalin
Re: [RFC V3 12/18] arm64: assembler: Add macro to annotate asm function having non standard stack-frame.
Hi,
Quoted 19 lines · Catalin Marinas
Quoted 5 lines · Raphael Gault
arch/arm64/include/asm/assembler.h +0
-0@@ -752,4 +752,17 @@ USER(\label, ic ivau, \tmp2) // invalidate I line PoU .Lyield_out_\@ : .endmarch/arm64/include/asm/assembler.h USER(\label, ic ivau, \tmp2) // invalidate I line PoU · line 763
Quoted 10 lines
arch/arm64/include/asm/assembler.h +9
-0+/* +* This macro is the arm64 assembler equivalent of the +* macro STACK_FRAME_NON_STANDARD define at +* ~/include/linux/frame.h +*/ + .macro asm_stack_frame_non_standard func +#ifdef CONFIG_STACK_VALIDATION + .pushsection ".discard.func_stack_frame_non_standard" + .8byte \funcNitpicks:
Quoted 2 lines
Does .quad vs .8byte make any difference?
No it doesn't, I'll use .quad then.
Quoted 3 lines
Could we place this in
include/linux/frame.hdirectly with a generic name (and some__ASSEMBLY__guards)? It doesn't look to be arm specific.
It might be more consistent indeed, I'll do that.
Thanks,
Signature · 1 line
Raphael Gault
RFC v3 13/18 arm64: sleep: Prevent stack frame warnings from objtool
In reply to Raphael GaultThis code doesn't respect the Arm PCS but it is intended this way. Adapting it to respect the PCS would result in altering the behaviour.
In order to suppress objtool's warnings, we setup a stack frame for __cpu_suspend_enter and annotate cpu_resume and _cpu_resume as having non-standard stack frames.
--- arch/arm64/kernel/sleep.S | 4 ++++ 1 file changed, 4 insertions(+)
arch/arm64/kernel/sleep.S +4 -0
@@ -90,6 +90,7 @@ ENTRY(__cpu_suspend_enter) str x0, [x1] add x0, x0, #SLEEP_STACK_DATA_SYSTEM_REGS stp x29, lr, [sp, #-16]! + mov x29, sp bl cpu_do_suspend ldp x29, lr, [sp], #16 mov x0, #1
@@ -146,3 +147,6 @@ ENTRY(_cpu_resume) mov x0, #0 ret ENDPROC(_cpu_resume) + + asm_stack_frame_non_standard cpu_resume + asm_stack_frame_non_standard _cpu_resume
Signature · 1 line
2.17.1
RFC v3 14/18 arm64: kvm: Annotate non-standard stack frame functions
In reply to Raphael GaultBoth __guest_entry and __guest_exit functions do not setup a correct stack frame. Because they can be considered as callable functions, even if they are particular cases, we chose to silence the warnings given by objtool by annotating them as non-standard.
--- arch/arm64/kvm/hyp/entry.S | 2 ++ 1 file changed, 2 insertions(+)
arch/arm64/kvm/hyp/entry.S +2 -0
@@ -82,6 +82,7 @@ ENTRY(__guest_enter) eret sb ENDPROC(__guest_enter) +asm_stack_frame_non_standard __guest_enter ENTRY(__guest_exit) // x0: return code
@@ -171,3 +172,4 @@ abort_guest_exit_end: orr x0, x0, x5 1: ret ENDPROC(__guest_exit) +asm_stack_frame_non_standard __guest_exit
Signature · 1 line
2.17.1
RFC v3 15/18 arm64: kernel: Add exception on kuser32 to prevent stack analysis
In reply to Raphael Gaultkuser32 being used for compatibility, it contains a32 instructions which are not recognised by objtool when trying to analyse arm64 object files. Thus, we add an exception to skip validation on this particular file.
--- arch/arm64/kernel/Makefile | 3 +++ 1 file changed, 3 insertions(+)
arch/arm64/kernel/Makefile +3 -0
@@ -33,6 +33,9 @@ ifneq ($(CONFIG_COMPAT_VDSO), y) obj-$(CONFIG_COMPAT) += sigreturn32.o endif obj-$(CONFIG_KUSER_HELPERS) += kuser32.o + +OBJECT_FILES_NON_STANDARD_kuser32.o := y + obj-$(CONFIG_FUNCTION_TRACER) += ftrace.o entry-ftrace.o obj-$(CONFIG_MODULES) += module.o obj-$(CONFIG_ARM64_MODULE_PLTS) += module-plts.o
Signature · 1 line
2.17.1
RFC v3 16/18 arm64: crypto: Add exceptions for crypto object to prevent stack analysis
In reply to Raphael GaultSome crypto modules contain .word of data in the .text section. Since objtool can't make the distinction between data and incorrect instruction, it gives a warning about the instruction beeing unknown and stops the analysis of the object file.
The exception can be removed if the data are moved to another section or if objtool is tweaked to handle this particular case.
--- arch/arm64/crypto/Makefile | 3 +++ 1 file changed, 3 insertions(+)
arch/arm64/crypto/Makefile +3 -0
@@ -43,9 +43,11 @@ aes-neon-blk-y := aes-glue-neon.o aes-neon.o obj-$(CONFIG_CRYPTO_SHA256_ARM64) += sha256-arm64.o sha256-arm64-y := sha256-glue.o sha256-core.o +OBJECT_FILES_NON_STANDARD_sha256-core.o := y obj-$(CONFIG_CRYPTO_SHA512_ARM64) += sha512-arm64.o sha512-arm64-y := sha512-glue.o sha512-core.o +OBJECT_FILES_NON_STANDARD_sha512-core.o := y obj-$(CONFIG_CRYPTO_CHACHA20_NEON) += chacha-neon.o chacha-neon-y := chacha-neon-core.o chacha-neon-glue.o
@@ -58,6 +60,7 @@ aes-arm64-y := aes-cipher-core.o aes-cipher-glue.o obj-$(CONFIG_CRYPTO_AES_ARM64_BS) += aes-neon-bs.o aes-neon-bs-y := aes-neonbs-core.o aes-neonbs-glue.o +OBJECT_FILES_NON_STANDARD_aes-neonbs-core.o := y CFLAGS_aes-glue-ce.o := -DUSE_V8_CRYPTO_EXTENSIONS
Signature · 1 line
2.17.1
RFC v3 17/18 arm64: kernel: Annotate non-standard stack frame functions
In reply to Raphael GaultAnnotate assembler functions which are callable but do not setup a correct stack frame.
--- arch/arm64/kernel/hyp-stub.S | 2 ++ arch/arm64/kvm/hyp-init.S | 2 ++ 2 files changed, 4 insertions(+)
arch/arm64/kernel/hyp-stub.S +2 -0
@@ -42,6 +42,7 @@ ENTRY(__hyp_stub_vectors) ventry el1_fiq_invalid // FIQ 32-bit EL1 ventry el1_error_invalid // Error 32-bit EL1 ENDPROC(__hyp_stub_vectors) +asm_stack_frame_non_standard __hyp_stub_vectors .align 11
@@ -69,6 +70,7 @@ el1_sync: 9: mov x0, xzr eret ENDPROC(el1_sync) +asm_stack_frame_non_standard el1_sync .macro invalid_vector label \label:
arch/arm64/kvm/hyp-init.S +2 -0
@@ -118,6 +118,7 @@ CPU_BE( orr x4, x4, #SCTLR_ELx_EE) /* Hello, World! */ eret ENDPROC(__kvm_hyp_init) +asm_stack_frame_non_standard __kvm_hyp_init ENTRY(__kvm_handle_stub_hvc) cmp x0, #HVC_SOFT_RESTART
@@ -159,6 +160,7 @@ reset: eret ENDPROC(__kvm_handle_stub_hvc) +asm_stack_frame_non_standard __kvm_handle_stub_hvc .ltorg
Signature · 1 line
2.17.1
RFC v3 18/18 objtool: arm64: Enable stack validation for arm64
In reply to Raphael Gault--- arch/arm64/Kconfig | 1 + 1 file changed, 1 insertion(+)
arch/arm64/Kconfig +1 -0
@@ -159,6 +159,7 @@ config ARM64 select HAVE_RCU_TABLE_FREE select HAVE_RSEQ select HAVE_STACKPROTECTOR + select HAVE_STACK_VALIDATION select HAVE_SYSCALL_TRACEPOINTS select HAVE_KPROBES select HAVE_KRETPROBES
Signature · 1 line
2.17.1
Hi all,
Just a gentle ping to see if anyone has comments to make about this version :)
Quoted 99 lines · Raphael Gault
As of now, objtool only supports the
x86_64architecture but the groundwork has already been done in order to add support for other architectures without too much effort.This series of patches adds support for the arm64 architecture based on the Armv8.5 Architecture Reference Manual.
Objtool will be a valuable tool to progress and provide more guarentees on live patching which is a work in progress for arm64.
Once we have the base of objtool working the next steps will be to port Peter Z's uaccess validation for arm64.
Changes since previous version:
* Rebased on tip/master: Note that I had to re-expose thestruct alternativeusingcheck.hbecause it is now used outside ofcheck.c.
* Reorder commits for a more coherent progression
* Introduce GCC plugin to help detect switch-tables for arm64 This plugins could be improve: It plugs in after the RTL control flow graph passes but only extract information about the switch tables. I originally intended for it to introduce new code_label/note within the RTL representation in order to reference them and thus get the address of the branch instruction. However I did not manage to do it properly using gen_rtx_CODE_LABEL/emit_label_before/after. If anyone has some experience with RTL plugins I am all ears for advices.Raphael Gault (18):
objtool: Add abstraction for computation of symbols offsets
objtool: orc: Refactor ORC API for other architectures to implement.
objtool: Move registers and control flow to arch-dependent code
objtool: arm64: Add required implementation for supporting the aarch64
architecture in objtool.
objtool: special: Adapt special section handling
objtool: arm64: Adapt the stack frame checks for arm architecture
objtool: IntroduceINSN_UNKNOWNtype
objtool: Refactor switch-tables code to support other architectures
gcc-plugins: objtool: Add plugin to detect switch table on arm64
objtool: arm64: Implement functions to add switch tables alternatives
arm64: alternative: Mark .altinstr_replacement as containing
executable instructions
arm64: assembler: Add macro to annotate asm function having non
standard stack-frame.
arm64: sleep: Prevent stack frame warnings from objtool
arm64: kvm: Annotate non-standard stack frame functions
arm64: kernel: Add exception on kuser32 to prevent stack analysis
arm64: crypto: Add exceptions for crypto object to prevent stack
analysis
arm64: kernel: Annotate non-standard stack frame functions
objtool: arm64: Enable stack validation for arm64arch/arm64/Kconfig | 1 + arch/arm64/crypto/Makefile | 3 + arch/arm64/include/asm/alternative.h | 2 +- arch/arm64/include/asm/assembler.h | 13 + arch/arm64/kernel/Makefile | 3 + arch/arm64/kernel/hyp-stub.S | 2 + arch/arm64/kernel/sleep.S | 4 + arch/arm64/kvm/hyp-init.S | 2 + arch/arm64/kvm/hyp/entry.S | 2 + scripts/Makefile.gcc-plugins | 2 + scripts/gcc-plugins/Kconfig | 9 + .../arm64_switch_table_detection_plugin.c | 58 + tools/objtool/Build | 2 - tools/objtool/arch.h | 21 +- tools/objtool/arch/arm64/Build | 8 + tools/objtool/arch/arm64/arch_special.c | 173 + tools/objtool/arch/arm64/bit_operations.c | 67 + tools/objtool/arch/arm64/decode.c | 2809 +++++++++++++++++ .../objtool/arch/arm64/include/arch_special.h | 52 + .../arch/arm64/include/asm/orc_types.h | 96 + .../arch/arm64/include/bit_operations.h | 24 + tools/objtool/arch/arm64/include/cfi.h | 74 + .../objtool/arch/arm64/include/insn_decode.h | 210 ++ tools/objtool/arch/arm64/orc_dump.c | 26 + tools/objtool/arch/arm64/orc_gen.c | 40 + tools/objtool/arch/x86/Build | 3 + tools/objtool/arch/x86/arch_special.c | 101 + tools/objtool/arch/x86/decode.c | 16 + tools/objtool/arch/x86/include/arch_special.h | 45 + tools/objtool/{ => arch/x86/include}/cfi.h | 0 tools/objtool/{ => arch/x86}/orc_dump.c | 4 +- tools/objtool/{ => arch/x86}/orc_gen.c | 104 +- tools/objtool/check.c | 309 +- tools/objtool/check.h | 10 + tools/objtool/elf.c | 3 +- tools/objtool/orc.h | 4 +- tools/objtool/special.c | 28 +- tools/objtool/special.h | 13 +- 38 files changed, 4119 insertions(+), 224 deletions(-)create mode 100644
scripts/gcc-plugins/arm64_switch_table_detection_plugin.ccreate mode 100644tools/objtool/arch/arm64/Build
create mode 100644tools/objtool/arch/arm64/arch_special.ccreate mode 100644tools/objtool/arch/arm64/bit_operations.ccreate mode 100644tools/objtool/arch/arm64/decode.c
create mode 100644tools/objtool/arch/arm64/include/arch_special.hcreate mode 100644tools/objtool/arch/arm64/include/asm/orc_types.hcreate mode 100644tools/objtool/arch/arm64/include/bit_operations.hcreate mode 100644tools/objtool/arch/arm64/include/cfi.h
Quoted 9 lines
create mode 100644
tools/objtool/arch/arm64/include/insn_decode.hcreate mode 100644tools/objtool/arch/arm64/orc_dump.ccreate mode 100644tools/objtool/arch/arm64/orc_gen.ccreate mode 100644tools/objtool/arch/x86/arch_special.ccreate mode 100644tools/objtool/arch/x86/include/arch_special.hrenametools/objtool/{=>arch/x86/include}/cfi.h(100%) renametools/objtool/{=>arch/x86}/orc_dump.c(98%) renametools/objtool/{=>arch/x86}/orc_gen.c(66%)
Cheers,
Signature · 1 line
Raphael Gault
Quoted 4 lines · Raphael Gault
Hi all,
Just a gentle ping to see if anyone has comments to make about this version :)
Hi Raphael,
Sorry for the delay. I haven't forgotten about these patches. I hope to do a proper review in the next week or so.
Signature · 1 line
Josh