Skip to content

Commit 627fce1

Browse files
jpoimboeIngo Molnar
authored andcommitted
objtool: Add ORC unwind table generation
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>
1 parent 5a3cf86 commit 627fce1

File tree

14 files changed

+916
-60
lines changed

14 files changed

+916
-60
lines changed

tools/objtool/Build

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
objtool-y += arch/$(SRCARCH)/
22
objtool-y += builtin-check.o
3+
objtool-y += builtin-orc.o
34
objtool-y += check.o
5+
objtool-y += orc_gen.o
6+
objtool-y += orc_dump.o
47
objtool-y += elf.o
58
objtool-y += special.o
69
objtool-y += objtool.o

tools/objtool/Documentation/stack-validation.txt

Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ analyzes every .o file and ensures the validity of its stack metadata.
1111
It enforces a set of rules on asm code and C inline assembly code so
1212
that stack traces can be reliable.
1313

14-
Currently it only checks frame pointer usage, but there are plans to add
15-
CFI validation for C files and CFI generation for asm files.
16-
1714
For each function, it recursively follows all possible code paths and
1815
validates the correct frame pointer state at each instruction.
1916

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

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

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

96-
b) 100% reliable stack traces for DWARF enabled kernels
97-
98-
(NOTE: This is not yet implemented)
99-
100-
As an alternative to frame pointers, DWARF Call Frame Information
101-
(CFI) metadata can be used to walk the stack. Unlike frame pointers,
102-
CFI metadata is out of band. So it doesn't affect runtime
103-
performance and it can be reliable even when interrupts or exceptions
104-
are involved.
105-
106-
For C code, gcc automatically generates DWARF CFI metadata. But for
107-
asm code, generating CFI is a tedious manual approach which requires
108-
manually placed .cfi assembler macros to be scattered throughout the
109-
code. It's clumsy and very easy to get wrong, and it makes the real
110-
code harder to read.
111-
112-
Stacktool will improve this situation in several ways. For code
113-
which already has CFI annotations, it will validate them. For code
114-
which doesn't have CFI annotations, it will generate them. So an
115-
architecture can opt to strip out all the manual .cfi annotations
116-
from their asm code and have objtool generate them instead.
97+
b) ORC (Oops Rewind Capability) unwind table generation
11798

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

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

128106
c) Higher live patching compatibility rate
129107

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

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

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

263241

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

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

277255

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

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

294272

295273
8. file.o: warning: objtool: funcA() falls through to next function funcB()

tools/objtool/builtin-check.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,5 @@ int cmd_check(int argc, const char **argv)
5252

5353
objname = argv[0];
5454

55-
return check(objname, nofp);
55+
return check(objname, nofp, false);
5656
}

tools/objtool/builtin-orc.c

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (C) 2017 Josh Poimboeuf <jpoimboe@redhat.com>
3+
*
4+
* This program is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU General Public License
6+
* as published by the Free Software Foundation; either version 2
7+
* of the License, or (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
/*
19+
* objtool orc:
20+
*
21+
* This command analyzes a .o file and adds .orc_unwind and .orc_unwind_ip
22+
* sections to it, which is used by the in-kernel ORC unwinder.
23+
*
24+
* This command is a superset of "objtool check".
25+
*/
26+
27+
#include <string.h>
28+
#include <subcmd/parse-options.h>
29+
#include "builtin.h"
30+
#include "check.h"
31+
32+
33+
static const char *orc_usage[] = {
34+
"objtool orc generate [<options>] file.o",
35+
"objtool orc dump file.o",
36+
NULL,
37+
};
38+
39+
extern const struct option check_options[];
40+
extern bool nofp;
41+
42+
int cmd_orc(int argc, const char **argv)
43+
{
44+
const char *objname;
45+
46+
argc--; argv++;
47+
if (!strncmp(argv[0], "gen", 3)) {
48+
argc = parse_options(argc, argv, check_options, orc_usage, 0);
49+
if (argc != 1)
50+
usage_with_options(orc_usage, check_options);
51+
52+
objname = argv[0];
53+
54+
return check(objname, nofp, true);
55+
56+
}
57+
58+
if (!strcmp(argv[0], "dump")) {
59+
if (argc != 2)
60+
usage_with_options(orc_usage, check_options);
61+
62+
objname = argv[1];
63+
64+
return orc_dump(objname);
65+
}
66+
67+
usage_with_options(orc_usage, check_options);
68+
69+
return 0;
70+
}

tools/objtool/builtin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@
1818
#define _BUILTIN_H
1919

2020
extern int cmd_check(int argc, const char **argv);
21+
extern int cmd_orc(int argc, const char **argv);
2122

2223
#endif /* _BUILTIN_H */

tools/objtool/check.c

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ const char *objname;
3636
static bool nofp;
3737
struct cfi_state initial_func_cfi;
3838

39-
static struct instruction *find_insn(struct objtool_file *file,
40-
struct section *sec, unsigned long offset)
39+
struct instruction *find_insn(struct objtool_file *file,
40+
struct section *sec, unsigned long offset)
4141
{
4242
struct instruction *insn;
4343

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

262+
if (strcmp(sec->name, ".altinstr_replacement") &&
263+
strcmp(sec->name, ".altinstr_aux") &&
264+
strncmp(sec->name, ".discard.", 9))
265+
sec->text = true;
266+
262267
for (offset = 0; offset < sec->len; offset += insn->len) {
263268
insn = malloc(sizeof(*insn));
264269
if (!insn) {
@@ -947,6 +952,30 @@ static bool has_valid_stack_frame(struct insn_state *state)
947952
return false;
948953
}
949954

955+
static int update_insn_state_regs(struct instruction *insn, struct insn_state *state)
956+
{
957+
struct cfi_reg *cfa = &state->cfa;
958+
struct stack_op *op = &insn->stack_op;
959+
960+
if (cfa->base != CFI_SP)
961+
return 0;
962+
963+
/* push */
964+
if (op->dest.type == OP_DEST_PUSH)
965+
cfa->offset += 8;
966+
967+
/* pop */
968+
if (op->src.type == OP_SRC_POP)
969+
cfa->offset -= 8;
970+
971+
/* add immediate to sp */
972+
if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
973+
op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
974+
cfa->offset -= op->src.offset;
975+
976+
return 0;
977+
}
978+
950979
static void save_reg(struct insn_state *state, unsigned char reg, int base,
951980
int offset)
952981
{
@@ -1032,6 +1061,9 @@ static int update_insn_state(struct instruction *insn, struct insn_state *state)
10321061
return 0;
10331062
}
10341063

1064+
if (state->type == ORC_TYPE_REGS || state->type == ORC_TYPE_REGS_IRET)
1065+
return update_insn_state_regs(insn, state);
1066+
10351067
switch (op->dest.type) {
10361068

10371069
case OP_DEST_REG:
@@ -1323,6 +1355,10 @@ static bool insn_state_match(struct instruction *insn, struct insn_state *state)
13231355
break;
13241356
}
13251357

1358+
} else if (state1->type != state2->type) {
1359+
WARN_FUNC("stack state mismatch: type1=%d type2=%d",
1360+
insn->sec, insn->offset, state1->type, state2->type);
1361+
13261362
} else if (state1->drap != state2->drap ||
13271363
(state1->drap && state1->drap_reg != state2->drap_reg)) {
13281364
WARN_FUNC("stack state mismatch: drap1=%d(%d) drap2=%d(%d)",
@@ -1613,15 +1649,15 @@ static void cleanup(struct objtool_file *file)
16131649
elf_close(file->elf);
16141650
}
16151651

1616-
int check(const char *_objname, bool _nofp)
1652+
int check(const char *_objname, bool _nofp, bool orc)
16171653
{
16181654
struct objtool_file file;
16191655
int ret, warnings = 0;
16201656

16211657
objname = _objname;
16221658
nofp = _nofp;
16231659

1624-
file.elf = elf_open(objname);
1660+
file.elf = elf_open(objname, orc ? O_RDWR : O_RDONLY);
16251661
if (!file.elf)
16261662
return 1;
16271663

@@ -1654,6 +1690,20 @@ int check(const char *_objname, bool _nofp)
16541690
warnings += ret;
16551691
}
16561692

1693+
if (orc) {
1694+
ret = create_orc(&file);
1695+
if (ret < 0)
1696+
goto out;
1697+
1698+
ret = create_orc_sections(&file);
1699+
if (ret < 0)
1700+
goto out;
1701+
1702+
ret = elf_write(file.elf);
1703+
if (ret < 0)
1704+
goto out;
1705+
}
1706+
16571707
out:
16581708
cleanup(&file);
16591709

tools/objtool/check.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@
2222
#include "elf.h"
2323
#include "cfi.h"
2424
#include "arch.h"
25+
#include "orc.h"
2526
#include <linux/hashtable.h>
2627

2728
struct insn_state {
2829
struct cfi_reg cfa;
2930
struct cfi_reg regs[CFI_NUM_REGS];
3031
int stack_size;
32+
unsigned char type;
3133
bool bp_scratch;
3234
bool drap;
3335
int drap_reg;
@@ -48,6 +50,7 @@ struct instruction {
4850
struct symbol *func;
4951
struct stack_op stack_op;
5052
struct insn_state state;
53+
struct orc_entry orc;
5154
};
5255

5356
struct objtool_file {
@@ -58,9 +61,19 @@ struct objtool_file {
5861
bool ignore_unreachables, c_file;
5962
};
6063

61-
int check(const char *objname, bool nofp);
64+
int check(const char *objname, bool nofp, bool orc);
65+
66+
struct instruction *find_insn(struct objtool_file *file,
67+
struct section *sec, unsigned long offset);
6268

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

72+
#define sec_for_each_insn(file, sec, insn) \
73+
for (insn = find_insn(file, sec, 0); \
74+
insn && &insn->list != &file->insn_list && \
75+
insn->sec == sec; \
76+
insn = list_next_entry(insn, list))
77+
78+
6679
#endif /* _CHECK_H */

0 commit comments

Comments
 (0)