Skip to content

Commit fb37680

Browse files
pa1guptagregkh
authored andcommitted
x86/speculation: Add a conditional CS prefix to CALL_NOSPEC
commit 052040e34c08428a5a388b85787e8531970c0c67 upstream. Retpoline mitigation for spectre-v2 uses thunks for indirect branches. To support this mitigation compilers add a CS prefix with -mindirect-branch-cs-prefix. For an indirect branch in asm, this needs to be added manually. CS prefix is already being added to indirect branches in asm files, but not in inline asm. Add CS prefix to CALL_NOSPEC for inline asm as well. There is no JMP_NOSPEC for inline asm. Reported-by: Josh Poimboeuf <[email protected]> Signed-off-by: Pawan Gupta <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Cc: Andrew Cooper <[email protected] Cc: Linus Torvalds <[email protected]> Cc: Peter Zijlstra <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 4bc1033 commit fb37680

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,8 @@
119119
.endm
120120

121121
/*
122-
* Equivalent to -mindirect-branch-cs-prefix; emit the 5 byte jmp/call
123-
* to the retpoline thunk with a CS prefix when the register requires
124-
* a RAX prefix byte to encode. Also see apply_retpolines().
122+
* Emits a conditional CS prefix that is compatible with
123+
* -mindirect-branch-cs-prefix.
125124
*/
126125
.macro __CS_PREFIX reg:req
127126
.irp rs,r8,r9,r10,r11,r12,r13,r14,r15
@@ -280,12 +279,24 @@ extern void (*x86_return_thunk)(void);
280279

281280
#ifdef CONFIG_X86_64
282281

282+
/*
283+
* Emits a conditional CS prefix that is compatible with
284+
* -mindirect-branch-cs-prefix.
285+
*/
286+
#define __CS_PREFIX(reg) \
287+
".irp rs,r8,r9,r10,r11,r12,r13,r14,r15\n" \
288+
".ifc \\rs," reg "\n" \
289+
".byte 0x2e\n" \
290+
".endif\n" \
291+
".endr\n"
292+
283293
/*
284294
* Inline asm uses the %V modifier which is only in newer GCC
285295
* which is ensured when CONFIG_RETPOLINE is defined.
286296
*/
287297
#ifdef CONFIG_RETPOLINE
288-
#define CALL_NOSPEC "call __x86_indirect_thunk_%V[thunk_target]\n"
298+
#define CALL_NOSPEC __CS_PREFIX("%V[thunk_target]") \
299+
"call __x86_indirect_thunk_%V[thunk_target]\n"
289300
#else
290301
#define CALL_NOSPEC "call *%[thunk_target]\n"
291302
#endif

0 commit comments

Comments
 (0)