blob: 76b058533e473b10d99e7a1ee4b661b5b2b2b14d [file] [log] [blame]
David Woodhouse76b04382018-01-11 21:46:25 +00001/* SPDX-License-Identifier: GPL-2.0 */
2
Borislav Petkov7a32fc52018-01-26 13:11:37 +01003#ifndef _ASM_X86_NOSPEC_BRANCH_H_
4#define _ASM_X86_NOSPEC_BRANCH_H_
David Woodhouse76b04382018-01-11 21:46:25 +00005
6#include <asm/alternative.h>
7#include <asm/alternative-asm.h>
8#include <asm/cpufeatures.h>
Peter Zijlstraea00f302018-02-13 14:28:19 +01009#include <asm/msr-index.h>
David Woodhouse76b04382018-01-11 21:46:25 +000010
11#ifdef __ASSEMBLY__
12
13/*
14 * This should be used immediately before a retpoline alternative. It tells
15 * objtool where the retpolines are so that it can make sense of the control
16 * flow by just reading the original instruction(s) and ignoring the
17 * alternatives.
18 */
19.macro ANNOTATE_NOSPEC_ALTERNATIVE
20 .Lannotate_\@:
21 .pushsection .discard.nospec
22 .long .Lannotate_\@ - .
23 .popsection
24.endm
25
26/*
27 * These are the bare retpoline primitives for indirect jmp and call.
28 * Do not use these directly; they only exist to make the ALTERNATIVE
29 * invocation below less ugly.
30 */
31.macro RETPOLINE_JMP reg:req
32 call .Ldo_rop_\@
33.Lspec_trap_\@:
34 pause
Tom Lendacky28d437d2018-01-13 17:27:30 -060035 lfence
David Woodhouse76b04382018-01-11 21:46:25 +000036 jmp .Lspec_trap_\@
37.Ldo_rop_\@:
38 mov \reg, (%_ASM_SP)
39 ret
40.endm
41
42/*
43 * This is a wrapper around RETPOLINE_JMP so the called function in reg
44 * returns to the instruction after the macro.
45 */
46.macro RETPOLINE_CALL reg:req
47 jmp .Ldo_call_\@
48.Ldo_retpoline_jmp_\@:
49 RETPOLINE_JMP \reg
50.Ldo_call_\@:
51 call .Ldo_retpoline_jmp_\@
52.endm
53
54/*
55 * JMP_NOSPEC and CALL_NOSPEC macros can be used instead of a simple
56 * indirect jmp/call which may be susceptible to the Spectre variant 2
57 * attack.
58 */
59.macro JMP_NOSPEC reg:req
60#ifdef CONFIG_RETPOLINE
61 ANNOTATE_NOSPEC_ALTERNATIVE
62 ALTERNATIVE_2 __stringify(jmp *\reg), \
63 __stringify(RETPOLINE_JMP \reg), X86_FEATURE_RETPOLINE, \
64 __stringify(lfence; jmp *\reg), X86_FEATURE_RETPOLINE_AMD
65#else
66 jmp *\reg
67#endif
68.endm
69
70.macro CALL_NOSPEC reg:req
71#ifdef CONFIG_RETPOLINE
72 ANNOTATE_NOSPEC_ALTERNATIVE
73 ALTERNATIVE_2 __stringify(call *\reg), \
74 __stringify(RETPOLINE_CALL \reg), X86_FEATURE_RETPOLINE,\
75 __stringify(lfence; call *\reg), X86_FEATURE_RETPOLINE_AMD
76#else
77 call *\reg
78#endif
79.endm
80
Borislav Petkov1dde7412018-01-27 16:24:33 +000081/* This clobbers the BX register */
82.macro FILL_RETURN_BUFFER nr:req ftr:req
David Woodhouse117cc7a2018-01-12 11:11:27 +000083#ifdef CONFIG_RETPOLINE
Borislav Petkov1dde7412018-01-27 16:24:33 +000084 ALTERNATIVE "", "call __clear_rsb", \ftr
David Woodhouse117cc7a2018-01-12 11:11:27 +000085#endif
86.endm
87
David Woodhouse76b04382018-01-11 21:46:25 +000088#else /* __ASSEMBLY__ */
89
90#define ANNOTATE_NOSPEC_ALTERNATIVE \
91 "999:\n\t" \
92 ".pushsection .discard.nospec\n\t" \
93 ".long 999b - .\n\t" \
94 ".popsection\n\t"
95
96#if defined(CONFIG_X86_64) && defined(RETPOLINE)
97
98/*
99 * Since the inline asm uses the %V modifier which is only in newer GCC,
100 * the 64-bit one is dependent on RETPOLINE not CONFIG_RETPOLINE.
101 */
102# define CALL_NOSPEC \
103 ANNOTATE_NOSPEC_ALTERNATIVE \
104 ALTERNATIVE( \
105 "call *%[thunk_target]\n", \
106 "call __x86_indirect_thunk_%V[thunk_target]\n", \
107 X86_FEATURE_RETPOLINE)
108# define THUNK_TARGET(addr) [thunk_target] "r" (addr)
109
110#elif defined(CONFIG_X86_32) && defined(CONFIG_RETPOLINE)
111/*
112 * For i386 we use the original ret-equivalent retpoline, because
113 * otherwise we'll run out of registers. We don't care about CET
114 * here, anyway.
115 */
116# define CALL_NOSPEC ALTERNATIVE("call *%[thunk_target]\n", \
117 " jmp 904f;\n" \
118 " .align 16\n" \
119 "901: call 903f;\n" \
120 "902: pause;\n" \
Tom Lendacky28d437d2018-01-13 17:27:30 -0600121 " lfence;\n" \
David Woodhouse76b04382018-01-11 21:46:25 +0000122 " jmp 902b;\n" \
123 " .align 16\n" \
124 "903: addl $4, %%esp;\n" \
125 " pushl %[thunk_target];\n" \
126 " ret;\n" \
127 " .align 16\n" \
128 "904: call 901b;\n", \
129 X86_FEATURE_RETPOLINE)
130
131# define THUNK_TARGET(addr) [thunk_target] "rm" (addr)
David Woodhouse117cc7a2018-01-12 11:11:27 +0000132#else /* No retpoline for C / inline asm */
David Woodhouse76b04382018-01-11 21:46:25 +0000133# define CALL_NOSPEC "call *%[thunk_target]\n"
134# define THUNK_TARGET(addr) [thunk_target] "rm" (addr)
135#endif
136
David Woodhouseda285122018-01-11 21:46:26 +0000137/* The Spectre V2 mitigation variants */
138enum spectre_v2_mitigation {
139 SPECTRE_V2_NONE,
140 SPECTRE_V2_RETPOLINE_MINIMAL,
141 SPECTRE_V2_RETPOLINE_MINIMAL_AMD,
142 SPECTRE_V2_RETPOLINE_GENERIC,
143 SPECTRE_V2_RETPOLINE_AMD,
144 SPECTRE_V2_IBRS,
145};
146
Masami Hiramatsu736e80a2018-01-19 01:14:21 +0900147extern char __indirect_thunk_start[];
148extern char __indirect_thunk_end[];
149
David Woodhouse117cc7a2018-01-12 11:11:27 +0000150/*
151 * On VMEXIT we must ensure that no RSB predictions learned in the guest
152 * can be followed in the host, by overwriting the RSB completely. Both
153 * retpoline and IBRS mitigations for Spectre v2 need this; only on future
Darren Kennyaf189c92018-02-02 19:12:20 +0000154 * CPUs with IBRS_ALL *might* it be avoided.
David Woodhouse117cc7a2018-01-12 11:11:27 +0000155 */
156static inline void vmexit_fill_RSB(void)
157{
158#ifdef CONFIG_RETPOLINE
Borislav Petkov1dde7412018-01-27 16:24:33 +0000159 alternative_input("",
160 "call __fill_rsb",
161 X86_FEATURE_RETPOLINE,
162 ASM_NO_INPUT_CLOBBER(_ASM_BX, "memory"));
David Woodhouse117cc7a2018-01-12 11:11:27 +0000163#endif
164}
Andi Kleen3f7d8752018-01-17 14:53:28 -0800165
David Woodhouse20ffa1c2018-01-25 16:14:15 +0000166static inline void indirect_branch_prediction_barrier(void)
167{
David Woodhousef2088202018-02-10 23:39:23 +0000168 asm volatile(ALTERNATIVE("",
169 "movl %[msr], %%ecx\n\t"
170 "movl %[val], %%eax\n\t"
171 "movl $0, %%edx\n\t"
172 "wrmsr",
173 X86_FEATURE_USE_IBPB)
174 : : [msr] "i" (MSR_IA32_PRED_CMD),
175 [val] "i" (PRED_CMD_IBPB)
176 : "eax", "ecx", "edx", "memory");
David Woodhouse20ffa1c2018-01-25 16:14:15 +0000177}
178
David Woodhouse76b04382018-01-11 21:46:25 +0000179#endif /* __ASSEMBLY__ */
Borislav Petkov7a32fc52018-01-26 13:11:37 +0100180#endif /* _ASM_X86_NOSPEC_BRANCH_H_ */