xref: /linux/arch/x86/lib/retpoline.S (revision dec1c62e91ba268ab2a6e339d4d7a59287d5eba1)
1/* SPDX-License-Identifier: GPL-2.0 */
2
3#include <linux/stringify.h>
4#include <linux/linkage.h>
5#include <asm/dwarf2.h>
6#include <asm/cpufeatures.h>
7#include <asm/alternative.h>
8#include <asm/export.h>
9#include <asm/nospec-branch.h>
10#include <asm/unwind_hints.h>
11#include <asm/frame.h>
12
13	.section .text.__x86.indirect_thunk
14
15.macro RETPOLINE reg
16	ANNOTATE_INTRA_FUNCTION_CALL
17	call    .Ldo_rop_\@
18.Lspec_trap_\@:
19	UNWIND_HINT_EMPTY
20	pause
21	lfence
22	jmp .Lspec_trap_\@
23.Ldo_rop_\@:
24	mov     %\reg, (%_ASM_SP)
25	UNWIND_HINT_FUNC
26	RET
27.endm
28
29.macro THUNK reg
30
31	.align RETPOLINE_THUNK_SIZE
32SYM_INNER_LABEL(__x86_indirect_thunk_\reg, SYM_L_GLOBAL)
33	UNWIND_HINT_EMPTY
34	ANNOTATE_NOENDBR
35
36	ALTERNATIVE_2 __stringify(ANNOTATE_RETPOLINE_SAFE; jmp *%\reg), \
37		      __stringify(RETPOLINE \reg), X86_FEATURE_RETPOLINE, \
38		      __stringify(lfence; ANNOTATE_RETPOLINE_SAFE; jmp *%\reg; int3), X86_FEATURE_RETPOLINE_LFENCE
39
40.endm
41
42/*
43 * Despite being an assembler file we can't just use .irp here
44 * because __KSYM_DEPS__ only uses the C preprocessor and would
45 * only see one instance of "__x86_indirect_thunk_\reg" rather
46 * than one per register with the correct names. So we do it
47 * the simple and nasty way...
48 *
49 * Worse, you can only have a single EXPORT_SYMBOL per line,
50 * and CPP can't insert newlines, so we have to repeat everything
51 * at least twice.
52 */
53
54#define __EXPORT_THUNK(sym)	_ASM_NOKPROBE(sym); EXPORT_SYMBOL(sym)
55#define EXPORT_THUNK(reg)	__EXPORT_THUNK(__x86_indirect_thunk_ ## reg)
56
57	.align RETPOLINE_THUNK_SIZE
58SYM_CODE_START(__x86_indirect_thunk_array)
59
60#define GEN(reg) THUNK reg
61#include <asm/GEN-for-each-reg.h>
62#undef GEN
63
64	.align RETPOLINE_THUNK_SIZE
65SYM_CODE_END(__x86_indirect_thunk_array)
66
67#define GEN(reg) EXPORT_THUNK(reg)
68#include <asm/GEN-for-each-reg.h>
69#undef GEN
70