1 /* SPDX-License-Identifier: GPL-2.0 */
3 #include <linux/stringify.h>
4 #include <linux/linkage.h>
5 #include <asm/dwarf2.h>
6 #include <asm/cpufeatures.h>
7 #include <asm/alternative-asm.h>
8 #include <asm/export.h>
9 #include <asm/nospec-branch.h>
10 #include <asm/bitsperlong.h>
13 .section .text.__x86.indirect_thunk
15 ENTRY(__x86_indirect_thunk_\reg)
19 ENDPROC(__x86_indirect_thunk_\reg)
23 * Despite being an assembler file we can't just use .irp here
24 * because __KSYM_DEPS__ only uses the C preprocessor and would
25 * only see one instance of "__x86_indirect_thunk_\reg" rather
26 * than one per register with the correct names. So we do it
27 * the simple and nasty way...
29 #define __EXPORT_THUNK(sym) _ASM_NOKPROBE(sym); EXPORT_SYMBOL(sym)
30 #define EXPORT_THUNK(reg) __EXPORT_THUNK(__x86_indirect_thunk_ ## reg)
31 #define GENERATE_THUNK(reg) THUNK reg ; EXPORT_THUNK(reg)
33 GENERATE_THUNK(_ASM_AX)
34 GENERATE_THUNK(_ASM_BX)
35 GENERATE_THUNK(_ASM_CX)
36 GENERATE_THUNK(_ASM_DX)
37 GENERATE_THUNK(_ASM_SI)
38 GENERATE_THUNK(_ASM_DI)
39 GENERATE_THUNK(_ASM_BP)
52 * Fill the CPU return stack buffer.
54 * Each entry in the RSB, if used for a speculative 'ret', contains an
55 * infinite 'pause; lfence; jmp' loop to capture speculative execution.
57 * This is required in various cases for retpoline and IBRS-based
58 * mitigations for the Spectre variant 2 vulnerability. Sometimes to
59 * eliminate potentially bogus entries from the RSB, and sometimes
60 * purely to ensure that it doesn't get empty, which on some CPUs would
61 * allow predictions from other (unwanted!) sources to be used.
63 * Google experimented with loop-unrolling and this turned out to be
64 * the optimal version - two calls, each with their own speculation
65 * trap should their return address end up getting used, in a loop.
67 .macro STUFF_RSB nr:req sp:req
68 mov $(\nr / 2), %_ASM_BX
72 773: /* speculation trap */
79 775: /* speculation trap */
87 add $((BITS_PER_LONG/8) * \nr), \sp
90 #define RSB_FILL_LOOPS 16 /* To avoid underflow */
93 STUFF_RSB RSB_FILL_LOOPS, %_ASM_SP
96 EXPORT_SYMBOL_GPL(__fill_rsb)
98 #define RSB_CLEAR_LOOPS 32 /* To forcibly overwrite all entries */
101 STUFF_RSB RSB_CLEAR_LOOPS, %_ASM_SP
104 EXPORT_SYMBOL_GPL(__clear_rsb)