Skip to content

Commit

Permalink
objtool: Add ORC unwind table generation
Browse files Browse the repository at this point in the history
Now that objtool knows the states of all registers on the stack for each
instruction, it's straightforward to generate debuginfo for an unwinder
to use.

Instead of generating DWARF, generate a new format called ORC, which is
more suitable for an in-kernel unwinder.  See
Documentation/x86/orc-unwinder.txt for a more detailed description of
this new debuginfo format and why it's preferable to DWARF.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Brian Gerst <brgerst@gmail.com>
Cc: Denys Vlasenko <dvlasenk@redhat.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Jiri Slaby <jslaby@suse.cz>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Mike Galbraith <efault@gmx.de>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: live-patching@vger.kernel.org
Link: http://lkml.kernel.org/r/c9b9f01ba6c5ed2bdc9bb0957b78167fdbf9632e.1499786555.git.jpoimboe@redhat.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>
  • Loading branch information
Josh Poimboeuf authored and Ingo Molnar committed Jul 18, 2017
1 parent 5a3cf86 commit 627fce1
Show file tree
Hide file tree
Showing 14 changed files with 916 additions and 60 deletions.
3 changes: 3 additions & 0 deletions tools/objtool/Build
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
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
Expand Down
56 changes: 17 additions & 39 deletions tools/objtool/Documentation/stack-validation.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ analyzes every .o file and ensures the validity of its stack metadata.
It enforces a set of rules on asm code and C inline assembly code so
that stack traces can be reliable.

Currently it only checks frame pointer usage, but there are plans to add
CFI validation for C files and CFI generation for asm files.

For each function, it recursively follows all possible code paths and
validates the correct frame pointer state at each instruction.

Expand All @@ -23,6 +20,10 @@ alternative execution paths to a given instruction (or set of
instructions). Similarly, it knows how to follow switch statements, for
which gcc sometimes uses jump tables.

(Objtool also has an 'orc generate' subcommand which generates debuginfo
for the ORC unwinder. See Documentation/x86/orc-unwinder.txt in the
kernel tree for more details.)


Why do we need stack metadata validation?
-----------------------------------------
Expand Down Expand Up @@ -93,37 +94,14 @@ a) More reliable stack traces for frame pointer enabled kernels
or at the very end of the function after the stack frame has been
destroyed. This is an inherent limitation of frame pointers.

b) 100% reliable stack traces for DWARF enabled kernels

(NOTE: This is not yet implemented)

As an alternative to frame pointers, DWARF Call Frame Information
(CFI) metadata can be used to walk the stack. Unlike frame pointers,
CFI metadata is out of band. So it doesn't affect runtime
performance and it can be reliable even when interrupts or exceptions
are involved.

For C code, gcc automatically generates DWARF CFI metadata. But for
asm code, generating CFI is a tedious manual approach which requires
manually placed .cfi assembler macros to be scattered throughout the
code. It's clumsy and very easy to get wrong, and it makes the real
code harder to read.

Stacktool will improve this situation in several ways. For code
which already has CFI annotations, it will validate them. For code
which doesn't have CFI annotations, it will generate them. So an
architecture can opt to strip out all the manual .cfi annotations
from their asm code and have objtool generate them instead.
b) ORC (Oops Rewind Capability) unwind table generation

We might also add a runtime stack validation debug option where we
periodically walk the stack from schedule() and/or an NMI to ensure
that the stack metadata is sane and that we reach the bottom of the
stack.
An alternative to frame pointers and DWARF, ORC unwind data can be
used to walk the stack. Unlike frame pointers, ORC data is out of
band. So it doesn't affect runtime performance and it can be
reliable even when interrupts or exceptions are involved.

So the benefit of objtool here will be that external tooling should
always show perfect stack traces. And the same will be true for
kernel warning/oops traces if the architecture has a runtime DWARF
unwinder.
For more details, see Documentation/x86/orc-unwinder.txt.

c) Higher live patching compatibility rate

Expand Down Expand Up @@ -211,7 +189,7 @@ they mean, and suggestions for how to fix them.
function, add proper frame pointer logic using the FRAME_BEGIN and
FRAME_END macros. Otherwise, if it's not a callable function, remove
its ELF function annotation by changing ENDPROC to END, and instead
use the manual CFI hint macros in asm/undwarf.h.
use the manual unwind hint macros in asm/unwind_hints.h.

If it's a GCC-compiled .c file, the error may be because the function
uses an inline asm() statement which has a "call" instruction. An
Expand All @@ -231,8 +209,8 @@ they mean, and suggestions for how to fix them.
If the error is for an asm file, and the instruction is inside (or
reachable from) a callable function, the function should be annotated
with the ENTRY/ENDPROC macros (ENDPROC is the important one).
Otherwise, the code should probably be annotated with the CFI hint
macros in asm/undwarf.h so objtool and the unwinder can know the
Otherwise, the code should probably be annotated with the unwind hint
macros in asm/unwind_hints.h so objtool and the unwinder can know the
stack state associated with the code.

If you're 100% sure the code won't affect stack traces, or if you're
Expand All @@ -258,7 +236,7 @@ they mean, and suggestions for how to fix them.
instructions aren't allowed in a callable function, and are most
likely part of the kernel entry code. They should usually not have
the callable function annotation (ENDPROC) and should always be
annotated with the CFI hint macros in asm/undwarf.h.
annotated with the unwind hint macros in asm/unwind_hints.h.


6. file.o: warning: objtool: func()+0x26: sibling call from callable instruction with modified stack frame
Expand All @@ -272,7 +250,7 @@ they mean, and suggestions for how to fix them.

If the instruction is not actually in a callable function (e.g.
kernel entry code), change ENDPROC to END and annotate manually with
the CFI hint macros in asm/undwarf.h.
the unwind hint macros in asm/unwind_hints.h.


7. file: warning: objtool: func()+0x5c: stack state mismatch
Expand All @@ -288,8 +266,8 @@ they mean, and suggestions for how to fix them.

Another possibility is that the code has some asm or inline asm which
does some unusual things to the stack or the frame pointer. In such
cases it's probably appropriate to use the CFI hint macros in
asm/undwarf.h.
cases it's probably appropriate to use the unwind hint macros in
asm/unwind_hints.h.


8. file.o: warning: objtool: funcA() falls through to next function funcB()
Expand Down
2 changes: 1 addition & 1 deletion tools/objtool/builtin-check.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ int cmd_check(int argc, const char **argv)

objname = argv[0];

return check(objname, nofp);
return check(objname, nofp, false);
}
70 changes: 70 additions & 0 deletions tools/objtool/builtin-orc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (C) 2017 Josh Poimboeuf <jpoimboe@redhat.com>
*
* 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/>.
*/

/*
* objtool orc:
*
* This command analyzes a .o file and adds .orc_unwind and .orc_unwind_ip
* sections to it, which is used by the in-kernel ORC unwinder.
*
* This command is a superset of "objtool check".
*/

#include <string.h>
#include <subcmd/parse-options.h>
#include "builtin.h"
#include "check.h"


static const char *orc_usage[] = {
"objtool orc generate [<options>] file.o",
"objtool orc dump file.o",
NULL,
};

extern const struct option check_options[];
extern bool nofp;

int cmd_orc(int argc, const char **argv)
{
const char *objname;

argc--; argv++;
if (!strncmp(argv[0], "gen", 3)) {
argc = parse_options(argc, argv, check_options, orc_usage, 0);
if (argc != 1)
usage_with_options(orc_usage, check_options);

objname = argv[0];

return check(objname, nofp, true);

}

if (!strcmp(argv[0], "dump")) {
if (argc != 2)
usage_with_options(orc_usage, check_options);

objname = argv[1];

return orc_dump(objname);
}

usage_with_options(orc_usage, check_options);

return 0;
}
1 change: 1 addition & 0 deletions tools/objtool/builtin.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
#define _BUILTIN_H

extern int cmd_check(int argc, const char **argv);
extern int cmd_orc(int argc, const char **argv);

#endif /* _BUILTIN_H */
58 changes: 54 additions & 4 deletions tools/objtool/check.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ const char *objname;
static bool nofp;
struct cfi_state initial_func_cfi;

static struct instruction *find_insn(struct objtool_file *file,
struct section *sec, unsigned long offset)
struct instruction *find_insn(struct objtool_file *file,
struct section *sec, unsigned long offset)
{
struct instruction *insn;

Expand Down Expand Up @@ -259,6 +259,11 @@ static int decode_instructions(struct objtool_file *file)
if (!(sec->sh.sh_flags & SHF_EXECINSTR))
continue;

if (strcmp(sec->name, ".altinstr_replacement") &&
strcmp(sec->name, ".altinstr_aux") &&
strncmp(sec->name, ".discard.", 9))
sec->text = true;

for (offset = 0; offset < sec->len; offset += insn->len) {
insn = malloc(sizeof(*insn));
if (!insn) {
Expand Down Expand Up @@ -947,6 +952,30 @@ static bool has_valid_stack_frame(struct insn_state *state)
return false;
}

static int update_insn_state_regs(struct instruction *insn, struct insn_state *state)
{
struct cfi_reg *cfa = &state->cfa;
struct stack_op *op = &insn->stack_op;

if (cfa->base != CFI_SP)
return 0;

/* push */
if (op->dest.type == OP_DEST_PUSH)
cfa->offset += 8;

/* pop */
if (op->src.type == OP_SRC_POP)
cfa->offset -= 8;

/* add immediate to sp */
if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
cfa->offset -= op->src.offset;

return 0;
}

static void save_reg(struct insn_state *state, unsigned char reg, int base,
int offset)
{
Expand Down Expand Up @@ -1032,6 +1061,9 @@ static int update_insn_state(struct instruction *insn, struct insn_state *state)
return 0;
}

if (state->type == ORC_TYPE_REGS || state->type == ORC_TYPE_REGS_IRET)
return update_insn_state_regs(insn, state);

switch (op->dest.type) {

case OP_DEST_REG:
Expand Down Expand Up @@ -1323,6 +1355,10 @@ static bool insn_state_match(struct instruction *insn, struct insn_state *state)
break;
}

} else if (state1->type != state2->type) {
WARN_FUNC("stack state mismatch: type1=%d type2=%d",
insn->sec, insn->offset, state1->type, state2->type);

} else if (state1->drap != state2->drap ||
(state1->drap && state1->drap_reg != state2->drap_reg)) {
WARN_FUNC("stack state mismatch: drap1=%d(%d) drap2=%d(%d)",
Expand Down Expand Up @@ -1613,15 +1649,15 @@ static void cleanup(struct objtool_file *file)
elf_close(file->elf);
}

int check(const char *_objname, bool _nofp)
int check(const char *_objname, bool _nofp, bool orc)
{
struct objtool_file file;
int ret, warnings = 0;

objname = _objname;
nofp = _nofp;

file.elf = elf_open(objname);
file.elf = elf_open(objname, orc ? O_RDWR : O_RDONLY);
if (!file.elf)
return 1;

Expand Down Expand Up @@ -1654,6 +1690,20 @@ int check(const char *_objname, bool _nofp)
warnings += ret;
}

if (orc) {
ret = create_orc(&file);
if (ret < 0)
goto out;

ret = create_orc_sections(&file);
if (ret < 0)
goto out;

ret = elf_write(file.elf);
if (ret < 0)
goto out;
}

out:
cleanup(&file);

Expand Down
15 changes: 14 additions & 1 deletion tools/objtool/check.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
#include "elf.h"
#include "cfi.h"
#include "arch.h"
#include "orc.h"
#include <linux/hashtable.h>

struct insn_state {
struct cfi_reg cfa;
struct cfi_reg regs[CFI_NUM_REGS];
int stack_size;
unsigned char type;
bool bp_scratch;
bool drap;
int drap_reg;
Expand All @@ -48,6 +50,7 @@ struct instruction {
struct symbol *func;
struct stack_op stack_op;
struct insn_state state;
struct orc_entry orc;
};

struct objtool_file {
Expand All @@ -58,9 +61,19 @@ struct objtool_file {
bool ignore_unreachables, c_file;
};

int check(const char *objname, bool nofp);
int check(const char *objname, bool nofp, bool orc);

struct instruction *find_insn(struct objtool_file *file,
struct section *sec, unsigned long offset);

#define for_each_insn(file, insn) \
list_for_each_entry(insn, &file->insn_list, list)

#define sec_for_each_insn(file, sec, insn) \
for (insn = find_insn(file, sec, 0); \
insn && &insn->list != &file->insn_list && \
insn->sec == sec; \
insn = list_next_entry(insn, list))


#endif /* _CHECK_H */
Loading

0 comments on commit 627fce1

Please sign in to comment.