Skip to content

Commit 9af9ad8

Browse files
pa1guptaIngo Molnar
authored andcommitted
x86/speculation: Add a conditional CS prefix to CALL_NOSPEC
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]
1 parent 010c4a4 commit 9af9ad8

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
@@ -198,9 +198,8 @@
198198
.endm
199199

200200
/*
201-
* Equivalent to -mindirect-branch-cs-prefix; emit the 5 byte jmp/call
202-
* to the retpoline thunk with a CS prefix when the register requires
203-
* a RAX prefix byte to encode. Also see apply_retpolines().
201+
* Emits a conditional CS prefix that is compatible with
202+
* -mindirect-branch-cs-prefix.
204203
*/
205204
.macro __CS_PREFIX reg:req
206205
.irp rs,r8,r9,r10,r11,r12,r13,r14,r15
@@ -420,12 +419,24 @@ static inline void call_depth_return_thunk(void) {}
420419

421420
#ifdef CONFIG_X86_64
422421

422+
/*
423+
* Emits a conditional CS prefix that is compatible with
424+
* -mindirect-branch-cs-prefix.
425+
*/
426+
#define __CS_PREFIX(reg) \
427+
".irp rs,r8,r9,r10,r11,r12,r13,r14,r15\n" \
428+
".ifc \\rs," reg "\n" \
429+
".byte 0x2e\n" \
430+
".endif\n" \
431+
".endr\n"
432+
423433
/*
424434
* Inline asm uses the %V modifier which is only in newer GCC
425435
* which is ensured when CONFIG_MITIGATION_RETPOLINE is defined.
426436
*/
427437
#ifdef CONFIG_MITIGATION_RETPOLINE
428-
#define CALL_NOSPEC "call __x86_indirect_thunk_%V[thunk_target]\n"
438+
#define CALL_NOSPEC __CS_PREFIX("%V[thunk_target]") \
439+
"call __x86_indirect_thunk_%V[thunk_target]\n"
429440
#else
430441
#define CALL_NOSPEC "call *%[thunk_target]\n"
431442
#endif

0 commit comments

Comments
 (0)