Skip to content

Commit 0b53c37

Browse files
Peter Zijlstrasuryasaimadhu
authored andcommitted
x86/retpoline: Use -mfunction-return
Utilize -mfunction-return=thunk-extern when available to have the compiler replace RET instructions with direct JMPs to the symbol __x86_return_thunk. This does not affect assembler (.S) sources, only C sources. -mfunction-return=thunk-extern has been available since gcc 7.3 and clang 15. Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Signed-off-by: Borislav Petkov <[email protected]> Reviewed-by: Nick Desaulniers <[email protected]> Reviewed-by: Josh Poimboeuf <[email protected]> Tested-by: Nick Desaulniers <[email protected]> Signed-off-by: Borislav Petkov <[email protected]>
1 parent 00e1533 commit 0b53c37

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

arch/x86/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ endif
1515
ifdef CONFIG_CC_IS_GCC
1616
RETPOLINE_CFLAGS := $(call cc-option,-mindirect-branch=thunk-extern -mindirect-branch-register)
1717
RETPOLINE_CFLAGS += $(call cc-option,-mindirect-branch-cs-prefix)
18+
RETPOLINE_CFLAGS += $(call cc-option,-mfunction-return=thunk-extern)
1819
RETPOLINE_VDSO_CFLAGS := $(call cc-option,-mindirect-branch=thunk-inline -mindirect-branch-register)
1920
endif
2021
ifdef CONFIG_CC_IS_CLANG
2122
RETPOLINE_CFLAGS := -mretpoline-external-thunk
2223
RETPOLINE_VDSO_CFLAGS := -mretpoline
24+
RETPOLINE_CFLAGS += $(call cc-option,-mfunction-return=thunk-extern)
2325
endif
2426
export RETPOLINE_CFLAGS
2527
export RETPOLINE_VDSO_CFLAGS

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@
123123
typedef u8 retpoline_thunk_t[RETPOLINE_THUNK_SIZE];
124124
extern retpoline_thunk_t __x86_indirect_thunk_array[];
125125

126+
extern void __x86_return_thunk(void);
127+
126128
#ifdef CONFIG_RETPOLINE
127129

128130
#define GEN(reg) \

arch/x86/lib/retpoline.S

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,16 @@ SYM_CODE_END(__x86_indirect_thunk_array)
6767
#define GEN(reg) EXPORT_THUNK(reg)
6868
#include <asm/GEN-for-each-reg.h>
6969
#undef GEN
70+
71+
/*
72+
* This function name is magical and is used by -mfunction-return=thunk-extern
73+
* for the compiler to generate JMPs to it.
74+
*/
75+
SYM_CODE_START(__x86_return_thunk)
76+
UNWIND_HINT_EMPTY
77+
ANNOTATE_NOENDBR
78+
ret
79+
int3
80+
SYM_CODE_END(__x86_return_thunk)
81+
82+
__EXPORT_THUNK(__x86_return_thunk)

0 commit comments

Comments
 (0)