From: madvenka@linux.microsoft.com
To: jpoimboe@redhat.com, peterz@infradead.org,
chenzhongjin@huawei.com, mark.rutland@arm.com,
broonie@kernel.org, nobuta.keiya@fujitsu.com,
sjitindarsingh@gmail.com, catalin.marinas@arm.com,
will@kernel.org, jamorris@linux.microsoft.com,
linux-arm-kernel@lists.infradead.org,
live-patching@vger.kernel.org, linux-kernel@vger.kernel.org,
madvenka@linux.microsoft.com
Subject: [RFC PATCH v2 11/20] objtool: arm64: Walk instructions and compute CFI for each instruction
Date: Mon, 23 May 2022 19:16:28 -0500 [thread overview]
Message-ID: <20220524001637.1707472-12-madvenka@linux.microsoft.com> (raw)
In-Reply-To: <20220524001637.1707472-1-madvenka@linux.microsoft.com>
From: "Madhavan T. Venkataraman" <madvenka@linux.microsoft.com>
Implement arch_initial_func_cfi_state() to initialize the CFI for a
function.
Add code to fpv_decode() to walk the instructions in every function and
compute the CFI information for each instruction.
Implement special handling for cases like jump tables.
Signed-off-by: Madhavan T. Venkataraman <madvenka@linux.microsoft.com>
---
tools/objtool/arch/arm64/decode.c | 15 +++
tools/objtool/fpv.c | 204 ++++++++++++++++++++++++++++++
2 files changed, 219 insertions(+)
diff --git a/tools/objtool/arch/arm64/decode.c b/tools/objtool/arch/arm64/decode.c
index f9df8b321659..93ef7c0811f1 100644
--- a/tools/objtool/arch/arm64/decode.c
+++ b/tools/objtool/arch/arm64/decode.c
@@ -35,6 +35,21 @@ struct decode {
/* --------------------- arch support functions ------------------------- */
+void arch_initial_func_cfi_state(struct cfi_init_state *state)
+{
+ int i;
+
+ for (i = 0; i < CFI_NUM_REGS; i++) {
+ state->regs[i].base = CFI_UNDEFINED;
+ state->regs[i].offset = 0;
+ }
+ state->regs[CFI_FP].base = CFI_CFA;
+
+ /* initial CFA (call frame address) */
+ state->cfa.base = CFI_SP;
+ state->cfa.offset = 0;
+}
+
unsigned long arch_dest_reloc_offset(int addend)
{
return addend;
diff --git a/tools/objtool/fpv.c b/tools/objtool/fpv.c
index 92ad0d0aac8e..52f613ae998f 100644
--- a/tools/objtool/fpv.c
+++ b/tools/objtool/fpv.c
@@ -13,6 +13,8 @@
#include <objtool/insn.h>
#include <objtool/warn.h>
+static bool fill;
+
/*
* Find the destination instructions for all jumps.
*/
@@ -50,15 +52,217 @@ static void add_jump_destinations(struct objtool_file *file)
}
}
+static void update_cfi_state(struct cfi_state *cfi, struct stack_op *op)
+{
+ struct cfi_reg *cfa = &cfi->cfa;
+ struct cfi_reg *regs = cfi->regs;
+
+ if (op->src.reg == CFI_SP) {
+ if (op->dest.reg == CFI_SP)
+ cfa->offset -= op->src.offset;
+ else
+ regs[CFI_FP].offset = -cfa->offset + op->src.offset;
+ } else {
+ if (op->dest.reg == CFI_SP)
+ cfa->offset = -(regs[CFI_FP].offset + op->src.offset);
+ else
+ regs[CFI_FP].offset += op->src.offset;
+ }
+
+ if (cfa->offset < -regs[CFI_FP].offset)
+ regs[CFI_FP].offset = 0;
+}
+
+static void do_stack_ops(struct instruction *insn, struct insn_state *state)
+{
+ struct stack_op *op;
+
+ list_for_each_entry(op, &insn->stack_ops, list) {
+ update_cfi_state(&state->cfi, op);
+ }
+}
+
+static void walk_code(struct objtool_file *file, struct section *sec,
+ struct symbol *func,
+ struct instruction *insn, struct insn_state *state)
+{
+ struct symbol *insn_func = insn->func;
+ struct instruction *dest;
+ struct cfi_state save_cfi;
+ unsigned long start, end;
+
+ for (; insn; insn = next_insn_same_sec(file, insn)) {
+
+ if (insn->func != insn_func)
+ return;
+
+ if (insn->cfi) {
+ if (fill) {
+ /* CFI is present. Nothing to fill. */
+ return;
+ }
+ if (insn->cfi->regs[CFI_FP].offset ||
+ !state->cfi.regs[CFI_FP].offset) {
+ return;
+ }
+ /*
+ * The new CFI contains a valid frame and the existing
+ * CFI doesn't. Replace the existing CFI with the new
+ * one.
+ */
+ }
+ insn->cfi = cfi_hash_find_or_add(&state->cfi);
+ dest = insn->jump_dest;
+
+ do_stack_ops(insn, state);
+
+ switch (insn->type) {
+ case INSN_BUG:
+ case INSN_RETURN:
+ case INSN_UNRELIABLE:
+ return;
+
+ case INSN_CALL:
+ case INSN_CALL_DYNAMIC:
+ start = func->offset;
+ end = start + func->len;
+ /*
+ * Treat intra-function calls as jumps and fall
+ * through.
+ */
+ if (!dest || dest->sec != sec ||
+ dest->offset <= start || dest->offset >= end) {
+ break;
+ }
+ /* fallthrough */
+
+ case INSN_JUMP_UNCONDITIONAL:
+ case INSN_JUMP_CONDITIONAL:
+ case INSN_JUMP_DYNAMIC:
+ if (dest) {
+ save_cfi = state->cfi;
+ walk_code(file, sec, func, dest, state);
+ state->cfi = save_cfi;
+ }
+ if (insn->type == INSN_JUMP_UNCONDITIONAL ||
+ insn->type == INSN_JUMP_DYNAMIC) {
+ return;
+ }
+ break;
+
+ default:
+ break;
+ }
+ }
+}
+
+static void walk_function(struct objtool_file *file, struct section *sec,
+ struct symbol *func)
+{
+ struct instruction *insn = find_insn(file, sec, func->offset);
+ struct insn_state state;
+
+ init_insn_state(&state, sec);
+ set_func_state(&state.cfi);
+
+ walk_code(file, sec, func, insn, &state);
+}
+
+/*
+ * This function addresses cases like jump tables where there is an array
+ * of unconditional branches. The normal walk would not have visited these
+ * instructions and established CFIs for them. Find those instructions. For
+ * each such instruction, copy the CFI from the branch instruction and
+ * propagate it down.
+ */
+static void fill_function(struct objtool_file *file, struct section *sec,
+ struct symbol *func)
+{
+ struct instruction *insn, *prev;
+ struct insn_state state;
+
+ func_for_each_insn(file, func, insn) {
+
+ if (insn->cfi) {
+ /* Instruction already has a CFI. */
+ continue;
+ }
+
+ prev = list_prev_entry(insn, list);
+ if (!prev || !prev->cfi) {
+ /*
+ * Previous instruction does not have a CFI that can
+ * be used for this instruction.
+ */
+ continue;
+ }
+
+ if (prev->type != INSN_JUMP_UNCONDITIONAL &&
+ prev->type != INSN_JUMP_DYNAMIC) {
+ /* Only copy CFI from unconditional branches. */
+ continue;
+ }
+
+ /*
+ * Propagate the CFI to all the instructions that can be
+ * visited from the current instruction that don't already
+ * have a CFI.
+ */
+ state.cfi = *prev->cfi;
+ walk_code(file, insn->sec, insn->func, insn, &state);
+ }
+}
+
+static void walk_section(struct objtool_file *file, struct section *sec)
+{
+ struct symbol *func;
+
+ list_for_each_entry(func, &sec->symbol_list, list) {
+
+ if (func->type != STT_FUNC || !func->len ||
+ func->pfunc != func || func->alias != func) {
+ /* No CFI generated for this function. */
+ continue;
+ }
+
+ if (!fill)
+ walk_function(file, sec, func);
+ else
+ fill_function(file, sec, func);
+ }
+}
+
+static void walk_sections(struct objtool_file *file)
+{
+ struct section *sec;
+
+ for_each_sec(file, sec) {
+ if (sec->sh.sh_flags & SHF_EXECINSTR)
+ walk_section(file, sec);
+ }
+}
+
int fpv_decode(struct objtool_file *file)
{
int ret;
+ arch_initial_func_cfi_state(&initial_func_cfi);
+
+ if (!cfi_hash_alloc(1UL << (file->elf->symbol_bits - 3)))
+ return -1;
+
ret = decode_instructions(file);
if (ret)
return ret;
add_jump_destinations(file);
+ if (!list_empty(&file->insn_list)) {
+ fill = false;
+ walk_sections(file);
+ fill = true;
+ walk_sections(file);
+ }
+
return 0;
}
--
2.25.1
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
next prev parent reply other threads:[~2022-05-24 0:20 UTC|newest]
Thread overview: 40+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <e81e773678f88f7c2ff7480e2eb096973ec198db>
2022-05-24 0:16 ` [RFC PATCH v2 00/20] arm64: livepatch: Use ORC for dynamic frame pointer validation madvenka
2022-05-24 0:16 ` [RFC PATCH v2 01/20] objtool: Reorganize CFI code madvenka
2022-05-24 0:16 ` [RFC PATCH v2 02/20] objtool: Reorganize instruction-related code madvenka
2022-05-24 0:16 ` [RFC PATCH v2 03/20] objtool: Move decode_instructions() to a separate file madvenka
2022-05-24 0:16 ` [RFC PATCH v2 04/20] objtool: Reorganize Unwind hint code madvenka
2022-05-24 0:16 ` [RFC PATCH v2 05/20] objtool: Reorganize ORC types madvenka
2022-05-24 14:27 ` Chen Zhongjin
2022-05-29 15:36 ` Madhavan T. Venkataraman
2022-05-24 0:16 ` [RFC PATCH v2 06/20] objtool: Reorganize ORC code madvenka
2022-05-24 0:16 ` [RFC PATCH v2 07/20] objtool: Reorganize ORC kernel code madvenka
2022-05-24 0:16 ` [RFC PATCH v2 08/20] objtool: arm64: Implement decoder for FP validation madvenka
2022-05-24 0:16 ` [RFC PATCH v2 09/20] objtool: arm64: Implement command to invoke the decoder madvenka
2022-05-24 14:09 ` Mark Brown
2022-05-29 14:49 ` Madhavan T. Venkataraman
2022-05-30 7:51 ` Peter Zijlstra
2022-06-01 22:45 ` Madhavan T. Venkataraman
2022-06-07 18:13 ` Madhavan T. Venkataraman
2022-05-24 0:16 ` [RFC PATCH v2 10/20] objtool: arm64: Compute destinations for call and jump instructions madvenka
2022-05-24 0:16 ` madvenka [this message]
2022-05-24 13:45 ` [RFC PATCH v2 11/20] objtool: arm64: Walk instructions and compute CFI for each instruction Chen Zhongjin
2022-05-29 15:18 ` Madhavan T. Venkataraman
2022-05-30 1:44 ` Chen Zhongjin
2022-05-24 0:16 ` [RFC PATCH v2 12/20] objtool: arm64: Generate ORC data from CFI for object files madvenka
2022-05-24 0:16 ` [RFC PATCH v2 13/20] objtool: arm64: Dump ORC data present in " madvenka
2022-05-24 0:16 ` [RFC PATCH v2 14/20] objtool: arm64: Add unwind hint support madvenka
2022-05-24 0:16 ` [RFC PATCH v2 15/20] arm64: Add unwind hints to specific points in code madvenka
2022-05-24 0:16 ` [RFC PATCH v2 16/20] arm64: Add kernel and module support for ORC madvenka
2022-05-24 0:16 ` [RFC PATCH v2 17/20] arm64: Build the kernel with ORC information madvenka
2022-05-24 0:16 ` [RFC PATCH v2 18/20] arm64: unwinder: Add a reliability check in the unwinder based on ORC madvenka
2022-05-24 0:16 ` [RFC PATCH v2 19/20] arm64: Miscellaneous changes required for enabling livepatch madvenka
2022-07-01 14:16 ` Miroslav Benes
2022-07-01 19:53 ` Madhavan T. Venkataraman
2022-05-24 0:16 ` [RFC PATCH v2 20/20] arm64: Enable livepatch for ARM64 madvenka
2022-05-24 14:24 ` [RFC PATCH v2 00/20] arm64: livepatch: Use ORC for dynamic frame pointer validation Chen Zhongjin
2022-05-29 15:30 ` Madhavan T. Venkataraman
2022-06-15 12:18 ` Ivan T. Ivanov
2022-06-15 13:37 ` Mark Rutland
2022-06-15 14:18 ` Ivan T. Ivanov
2022-06-15 20:50 ` Madhavan T. Venkataraman
2022-06-15 20:47 ` Madhavan T. Venkataraman
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220524001637.1707472-12-madvenka@linux.microsoft.com \
--to=madvenka@linux.microsoft.com \
--cc=broonie@kernel.org \
--cc=catalin.marinas@arm.com \
--cc=chenzhongjin@huawei.com \
--cc=jamorris@linux.microsoft.com \
--cc=jpoimboe@redhat.com \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=live-patching@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=nobuta.keiya@fujitsu.com \
--cc=peterz@infradead.org \
--cc=sjitindarsingh@gmail.com \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).