Skip to content

Commit cc1ac9c

Browse files
author
Peter Zijlstra
committed
x86/retpoline: Fix retpoline unwind
Currently objtool cannot understand retpolines, and thus cannot generate ORC unwind information for them. This means that we cannot unwind from the middle of a retpoline. The recent ANNOTATE_INTRA_FUNCTION_CALL and UNWIND_HINT_RET_OFFSET support in objtool enables it to understand the basic retpoline construct. A further problem is that the ORC unwind information is alternative invariant; IOW. every alternative should have the same ORC, retpolines obviously violate this. This means we need to out-of-line them. Since all GCC generated code already uses out-of-line retpolines, this should not affect performance much, if anything. This will enable objtool to generate valid ORC data for the out-of-line copies, which means we can correctly and reliably unwind through a retpoline. Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Acked-by: Josh Poimboeuf <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 34fdce6 commit cc1ac9c

File tree

3 files changed

+38
-51
lines changed

3 files changed

+38
-51
lines changed

arch/x86/include/asm/asm-prototypes.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,15 @@ extern void cmpxchg8b_emu(void);
2121
#define DECL_INDIRECT_THUNK(reg) \
2222
extern asmlinkage void __x86_indirect_thunk_ ## reg (void);
2323

24+
#define DECL_RETPOLINE(reg) \
25+
extern asmlinkage void __x86_retpoline_ ## reg (void);
26+
2427
#undef GEN
2528
#define GEN(reg) DECL_INDIRECT_THUNK(reg)
2629
#include <asm/GEN-for-each-reg.h>
2730

31+
#undef GEN
32+
#define GEN(reg) DECL_RETPOLINE(reg)
33+
#include <asm/GEN-for-each-reg.h>
34+
2835
#endif /* CONFIG_RETPOLINE */

arch/x86/include/asm/nospec-branch.h

Lines changed: 8 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,6 @@
1212
#include <asm/msr-index.h>
1313
#include <asm/unwind_hints.h>
1414

15-
/*
16-
* This should be used immediately before a retpoline alternative. It tells
17-
* objtool where the retpolines are so that it can make sense of the control
18-
* flow by just reading the original instruction(s) and ignoring the
19-
* alternatives.
20-
*/
21-
#define ANNOTATE_NOSPEC_ALTERNATIVE \
22-
ANNOTATE_IGNORE_ALTERNATIVE
23-
2415
/*
2516
* Fill the CPU return stack buffer.
2617
*
@@ -82,56 +73,26 @@
8273
.popsection
8374
.endm
8475

85-
/*
86-
* These are the bare retpoline primitives for indirect jmp and call.
87-
* Do not use these directly; they only exist to make the ALTERNATIVE
88-
* invocation below less ugly.
89-
*/
90-
.macro RETPOLINE_JMP reg:req
91-
call .Ldo_rop_\@
92-
.Lspec_trap_\@:
93-
pause
94-
lfence
95-
jmp .Lspec_trap_\@
96-
.Ldo_rop_\@:
97-
mov \reg, (%_ASM_SP)
98-
ret
99-
.endm
100-
101-
/*
102-
* This is a wrapper around RETPOLINE_JMP so the called function in reg
103-
* returns to the instruction after the macro.
104-
*/
105-
.macro RETPOLINE_CALL reg:req
106-
jmp .Ldo_call_\@
107-
.Ldo_retpoline_jmp_\@:
108-
RETPOLINE_JMP \reg
109-
.Ldo_call_\@:
110-
call .Ldo_retpoline_jmp_\@
111-
.endm
112-
11376
/*
11477
* JMP_NOSPEC and CALL_NOSPEC macros can be used instead of a simple
11578
* indirect jmp/call which may be susceptible to the Spectre variant 2
11679
* attack.
11780
*/
11881
.macro JMP_NOSPEC reg:req
11982
#ifdef CONFIG_RETPOLINE
120-
ANNOTATE_NOSPEC_ALTERNATIVE
121-
ALTERNATIVE_2 __stringify(ANNOTATE_RETPOLINE_SAFE; jmp *%\reg), \
122-
__stringify(RETPOLINE_JMP %\reg), X86_FEATURE_RETPOLINE,\
123-
__stringify(lfence; ANNOTATE_RETPOLINE_SAFE; jmp *%\reg), X86_FEATURE_RETPOLINE_AMD
83+
ALTERNATIVE_2 __stringify(ANNOTATE_RETPOLINE_SAFE; jmp *%\reg), \
84+
__stringify(jmp __x86_retpoline_\reg), X86_FEATURE_RETPOLINE, \
85+
__stringify(lfence; ANNOTATE_RETPOLINE_SAFE; jmp *%\reg), X86_FEATURE_RETPOLINE_AMD
12486
#else
12587
jmp *%\reg
12688
#endif
12789
.endm
12890

12991
.macro CALL_NOSPEC reg:req
13092
#ifdef CONFIG_RETPOLINE
131-
ANNOTATE_NOSPEC_ALTERNATIVE
132-
ALTERNATIVE_2 __stringify(ANNOTATE_RETPOLINE_SAFE; call *%\reg),\
133-
__stringify(RETPOLINE_CALL %\reg), X86_FEATURE_RETPOLINE,\
134-
__stringify(lfence; ANNOTATE_RETPOLINE_SAFE; call *%\reg), X86_FEATURE_RETPOLINE_AMD
93+
ALTERNATIVE_2 __stringify(ANNOTATE_RETPOLINE_SAFE; call *%\reg), \
94+
__stringify(call __x86_retpoline_\reg), X86_FEATURE_RETPOLINE, \
95+
__stringify(lfence; ANNOTATE_RETPOLINE_SAFE; call *%\reg), X86_FEATURE_RETPOLINE_AMD
13596
#else
13697
call *%\reg
13798
#endif
@@ -165,16 +126,16 @@
165126
* which is ensured when CONFIG_RETPOLINE is defined.
166127
*/
167128
# define CALL_NOSPEC \
168-
ANNOTATE_NOSPEC_ALTERNATIVE \
169129
ALTERNATIVE_2( \
170130
ANNOTATE_RETPOLINE_SAFE \
171131
"call *%[thunk_target]\n", \
172-
"call __x86_indirect_thunk_%V[thunk_target]\n", \
132+
"call __x86_retpoline_%V[thunk_target]\n", \
173133
X86_FEATURE_RETPOLINE, \
174134
"lfence;\n" \
175135
ANNOTATE_RETPOLINE_SAFE \
176136
"call *%[thunk_target]\n", \
177137
X86_FEATURE_RETPOLINE_AMD)
138+
178139
# define THUNK_TARGET(addr) [thunk_target] "r" (addr)
179140

180141
#else /* CONFIG_X86_32 */
@@ -184,7 +145,6 @@
184145
* here, anyway.
185146
*/
186147
# define CALL_NOSPEC \
187-
ANNOTATE_NOSPEC_ALTERNATIVE \
188148
ALTERNATIVE_2( \
189149
ANNOTATE_RETPOLINE_SAFE \
190150
"call *%[thunk_target]\n", \

arch/x86/lib/retpoline.S

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,31 @@
77
#include <asm/alternative-asm.h>
88
#include <asm/export.h>
99
#include <asm/nospec-branch.h>
10+
#include <asm/unwind_hints.h>
11+
#include <asm/frame.h>
1012

1113
.macro THUNK reg
1214
.section .text.__x86.indirect_thunk
1315

16+
.align 32
1417
SYM_FUNC_START(__x86_indirect_thunk_\reg)
15-
CFI_STARTPROC
16-
JMP_NOSPEC %\reg
17-
CFI_ENDPROC
18+
JMP_NOSPEC \reg
1819
SYM_FUNC_END(__x86_indirect_thunk_\reg)
20+
21+
SYM_FUNC_START_NOALIGN(__x86_retpoline_\reg)
22+
ANNOTATE_INTRA_FUNCTION_CALL
23+
call .Ldo_rop_\@
24+
.Lspec_trap_\@:
25+
UNWIND_HINT_EMPTY
26+
pause
27+
lfence
28+
jmp .Lspec_trap_\@
29+
.Ldo_rop_\@:
30+
mov %\reg, (%_ASM_SP)
31+
UNWIND_HINT_RET_OFFSET
32+
ret
33+
SYM_FUNC_END(__x86_retpoline_\reg)
34+
1935
.endm
2036

2137
/*
@@ -32,6 +48,7 @@ SYM_FUNC_END(__x86_indirect_thunk_\reg)
3248

3349
#define __EXPORT_THUNK(sym) _ASM_NOKPROBE(sym); EXPORT_SYMBOL(sym)
3450
#define EXPORT_THUNK(reg) __EXPORT_THUNK(__x86_indirect_thunk_ ## reg)
51+
#define EXPORT_RETPOLINE(reg) __EXPORT_THUNK(__x86_retpoline_ ## reg)
3552

3653
#undef GEN
3754
#define GEN(reg) THUNK reg
@@ -41,3 +58,6 @@ SYM_FUNC_END(__x86_indirect_thunk_\reg)
4158
#define GEN(reg) EXPORT_THUNK(reg)
4259
#include <asm/GEN-for-each-reg.h>
4360

61+
#undef GEN
62+
#define GEN(reg) EXPORT_RETPOLINE(reg)
63+
#include <asm/GEN-for-each-reg.h>

0 commit comments

Comments
 (0)