blob: 109f974f983532041ab48e020e1fda9d5539f8f5 [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
Thomas Gleixnerfa1202e2018-11-25 19:33:45 +01006#include <linux/static_key.h>
7
David Woodhouse76b04382018-01-11 21:46:25 +00008#include <asm/alternative.h>
9#include <asm/alternative-asm.h>
10#include <asm/cpufeatures.h>
Peter Zijlstraea00f302018-02-13 14:28:19 +010011#include <asm/msr-index.h>
David Woodhouse76b04382018-01-11 21:46:25 +000012
David Woodhoused1c99102018-02-19 10:50:56 +000013/*
Peter Zijlstraff05ab22019-03-18 14:33:07 +010014 * 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#define ANNOTATE_NOSPEC_ALTERNATIVE \
20 ANNOTATE_IGNORE_ALTERNATIVE
21
22/*
David Woodhoused1c99102018-02-19 10:50:56 +000023 * Fill the CPU return stack buffer.
24 *
25 * Each entry in the RSB, if used for a speculative 'ret', contains an
26 * infinite 'pause; lfence; jmp' loop to capture speculative execution.
27 *
28 * This is required in various cases for retpoline and IBRS-based
29 * mitigations for the Spectre variant 2 vulnerability. Sometimes to
30 * eliminate potentially bogus entries from the RSB, and sometimes
31 * purely to ensure that it doesn't get empty, which on some CPUs would
32 * allow predictions from other (unwanted!) sources to be used.
33 *
34 * We define a CPP macro such that it can be used from both .S files and
35 * inline assembly. It's possible to do a .macro and then include that
36 * from C via asm(".include <asm/nospec-branch.h>") but let's not go there.
37 */
38
39#define RSB_CLEAR_LOOPS 32 /* To forcibly overwrite all entries */
40#define RSB_FILL_LOOPS 16 /* To avoid underflow */
41
42/*
43 * Google experimented with loop-unrolling and this turned out to be
44 * the optimal version — two calls, each with their own speculation
45 * trap should their return address end up getting used, in a loop.
46 */
47#define __FILL_RETURN_BUFFER(reg, nr, sp) \
48 mov $(nr/2), reg; \
49771: \
50 call 772f; \
51773: /* speculation trap */ \
52 pause; \
53 lfence; \
54 jmp 773b; \
55772: \
56 call 774f; \
57775: /* speculation trap */ \
58 pause; \
59 lfence; \
60 jmp 775b; \
61774: \
62 dec reg; \
63 jnz 771b; \
64 add $(BITS_PER_LONG/8) * nr, sp;
65
David Woodhouse76b04382018-01-11 21:46:25 +000066#ifdef __ASSEMBLY__
67
68/*
Peter Zijlstra9e0e3c52018-01-17 22:34:34 +010069 * This should be used immediately before an indirect jump/call. It tells
70 * objtool the subsequent indirect jump/call is vouched safe for retpoline
71 * builds.
72 */
73.macro ANNOTATE_RETPOLINE_SAFE
74 .Lannotate_\@:
75 .pushsection .discard.retpoline_safe
76 _ASM_PTR .Lannotate_\@
77 .popsection
78.endm
79
80/*
David Woodhouse76b04382018-01-11 21:46:25 +000081 * These are the bare retpoline primitives for indirect jmp and call.
82 * Do not use these directly; they only exist to make the ALTERNATIVE
83 * invocation below less ugly.
84 */
85.macro RETPOLINE_JMP reg:req
86 call .Ldo_rop_\@
87.Lspec_trap_\@:
88 pause
Tom Lendacky28d437d2018-01-13 17:27:30 -060089 lfence
David Woodhouse76b04382018-01-11 21:46:25 +000090 jmp .Lspec_trap_\@
91.Ldo_rop_\@:
92 mov \reg, (%_ASM_SP)
93 ret
94.endm
95
96/*
97 * This is a wrapper around RETPOLINE_JMP so the called function in reg
98 * returns to the instruction after the macro.
99 */
100.macro RETPOLINE_CALL reg:req
101 jmp .Ldo_call_\@
102.Ldo_retpoline_jmp_\@:
103 RETPOLINE_JMP \reg
104.Ldo_call_\@:
105 call .Ldo_retpoline_jmp_\@
106.endm
107
108/*
109 * JMP_NOSPEC and CALL_NOSPEC macros can be used instead of a simple
110 * indirect jmp/call which may be susceptible to the Spectre variant 2
111 * attack.
112 */
113.macro JMP_NOSPEC reg:req
114#ifdef CONFIG_RETPOLINE
115 ANNOTATE_NOSPEC_ALTERNATIVE
Peter Zijlstra9e0e3c52018-01-17 22:34:34 +0100116 ALTERNATIVE_2 __stringify(ANNOTATE_RETPOLINE_SAFE; jmp *\reg), \
David Woodhouse76b04382018-01-11 21:46:25 +0000117 __stringify(RETPOLINE_JMP \reg), X86_FEATURE_RETPOLINE, \
Peter Zijlstra9e0e3c52018-01-17 22:34:34 +0100118 __stringify(lfence; ANNOTATE_RETPOLINE_SAFE; jmp *\reg), X86_FEATURE_RETPOLINE_AMD
David Woodhouse76b04382018-01-11 21:46:25 +0000119#else
120 jmp *\reg
121#endif
122.endm
123
124.macro CALL_NOSPEC reg:req
125#ifdef CONFIG_RETPOLINE
126 ANNOTATE_NOSPEC_ALTERNATIVE
Peter Zijlstra9e0e3c52018-01-17 22:34:34 +0100127 ALTERNATIVE_2 __stringify(ANNOTATE_RETPOLINE_SAFE; call *\reg), \
David Woodhouse76b04382018-01-11 21:46:25 +0000128 __stringify(RETPOLINE_CALL \reg), X86_FEATURE_RETPOLINE,\
Peter Zijlstra9e0e3c52018-01-17 22:34:34 +0100129 __stringify(lfence; ANNOTATE_RETPOLINE_SAFE; call *\reg), X86_FEATURE_RETPOLINE_AMD
David Woodhouse76b04382018-01-11 21:46:25 +0000130#else
131 call *\reg
132#endif
133.endm
134
David Woodhoused1c99102018-02-19 10:50:56 +0000135 /*
136 * A simpler FILL_RETURN_BUFFER macro. Don't make people use the CPP
137 * monstrosity above, manually.
138 */
139.macro FILL_RETURN_BUFFER reg:req nr:req ftr:req
David Woodhouse117cc7a2018-01-12 11:11:27 +0000140#ifdef CONFIG_RETPOLINE
David Woodhoused1c99102018-02-19 10:50:56 +0000141 ANNOTATE_NOSPEC_ALTERNATIVE
142 ALTERNATIVE "jmp .Lskip_rsb_\@", \
143 __stringify(__FILL_RETURN_BUFFER(\reg,\nr,%_ASM_SP)) \
144 \ftr
145.Lskip_rsb_\@:
David Woodhouse117cc7a2018-01-12 11:11:27 +0000146#endif
147.endm
148
David Woodhouse76b04382018-01-11 21:46:25 +0000149#else /* __ASSEMBLY__ */
150
Peter Zijlstra9e0e3c52018-01-17 22:34:34 +0100151#define ANNOTATE_RETPOLINE_SAFE \
152 "999:\n\t" \
153 ".pushsection .discard.retpoline_safe\n\t" \
154 _ASM_PTR " 999b\n\t" \
155 ".popsection\n\t"
156
Zhenzhong Duan4cd24de2018-11-02 01:45:41 -0700157#ifdef CONFIG_RETPOLINE
158#ifdef CONFIG_X86_64
David Woodhouse76b04382018-01-11 21:46:25 +0000159
160/*
Zhenzhong Duan4cd24de2018-11-02 01:45:41 -0700161 * Inline asm uses the %V modifier which is only in newer GCC
162 * which is ensured when CONFIG_RETPOLINE is defined.
David Woodhouse76b04382018-01-11 21:46:25 +0000163 */
164# define CALL_NOSPEC \
165 ANNOTATE_NOSPEC_ALTERNATIVE \
Zhenzhong Duan0cbb76d2018-09-18 07:45:00 -0700166 ALTERNATIVE_2( \
Peter Zijlstra9e0e3c52018-01-17 22:34:34 +0100167 ANNOTATE_RETPOLINE_SAFE \
David Woodhouse76b04382018-01-11 21:46:25 +0000168 "call *%[thunk_target]\n", \
169 "call __x86_indirect_thunk_%V[thunk_target]\n", \
Zhenzhong Duan0cbb76d2018-09-18 07:45:00 -0700170 X86_FEATURE_RETPOLINE, \
171 "lfence;\n" \
172 ANNOTATE_RETPOLINE_SAFE \
173 "call *%[thunk_target]\n", \
174 X86_FEATURE_RETPOLINE_AMD)
David Woodhouse76b04382018-01-11 21:46:25 +0000175# define THUNK_TARGET(addr) [thunk_target] "r" (addr)
176
Zhenzhong Duan4cd24de2018-11-02 01:45:41 -0700177#else /* CONFIG_X86_32 */
David Woodhouse76b04382018-01-11 21:46:25 +0000178/*
179 * For i386 we use the original ret-equivalent retpoline, because
180 * otherwise we'll run out of registers. We don't care about CET
181 * here, anyway.
182 */
Andy Whitcrofta14bff12018-03-14 11:24:27 +0000183# define CALL_NOSPEC \
Zhenzhong Duan0cbb76d2018-09-18 07:45:00 -0700184 ANNOTATE_NOSPEC_ALTERNATIVE \
185 ALTERNATIVE_2( \
Andy Whitcrofta14bff12018-03-14 11:24:27 +0000186 ANNOTATE_RETPOLINE_SAFE \
187 "call *%[thunk_target]\n", \
David Woodhouse76b04382018-01-11 21:46:25 +0000188 " jmp 904f;\n" \
189 " .align 16\n" \
190 "901: call 903f;\n" \
191 "902: pause;\n" \
Tom Lendacky28d437d2018-01-13 17:27:30 -0600192 " lfence;\n" \
David Woodhouse76b04382018-01-11 21:46:25 +0000193 " jmp 902b;\n" \
194 " .align 16\n" \
195 "903: addl $4, %%esp;\n" \
196 " pushl %[thunk_target];\n" \
197 " ret;\n" \
198 " .align 16\n" \
199 "904: call 901b;\n", \
Zhenzhong Duan0cbb76d2018-09-18 07:45:00 -0700200 X86_FEATURE_RETPOLINE, \
201 "lfence;\n" \
202 ANNOTATE_RETPOLINE_SAFE \
203 "call *%[thunk_target]\n", \
204 X86_FEATURE_RETPOLINE_AMD)
David Woodhouse76b04382018-01-11 21:46:25 +0000205
206# define THUNK_TARGET(addr) [thunk_target] "rm" (addr)
Zhenzhong Duan4cd24de2018-11-02 01:45:41 -0700207#endif
David Woodhouse117cc7a2018-01-12 11:11:27 +0000208#else /* No retpoline for C / inline asm */
David Woodhouse76b04382018-01-11 21:46:25 +0000209# define CALL_NOSPEC "call *%[thunk_target]\n"
210# define THUNK_TARGET(addr) [thunk_target] "rm" (addr)
211#endif
212
David Woodhouseda285122018-01-11 21:46:26 +0000213/* The Spectre V2 mitigation variants */
214enum spectre_v2_mitigation {
215 SPECTRE_V2_NONE,
David Woodhouseda285122018-01-11 21:46:26 +0000216 SPECTRE_V2_RETPOLINE_GENERIC,
217 SPECTRE_V2_RETPOLINE_AMD,
Sai Praneeth706d5162018-08-01 11:42:25 -0700218 SPECTRE_V2_IBRS_ENHANCED,
David Woodhouseda285122018-01-11 21:46:26 +0000219};
220
Thomas Gleixnerfa1202e2018-11-25 19:33:45 +0100221/* The indirect branch speculation control variants */
222enum spectre_v2_user_mitigation {
223 SPECTRE_V2_USER_NONE,
224 SPECTRE_V2_USER_STRICT,
Thomas Lendacky20c3a2c2018-12-13 23:03:54 +0000225 SPECTRE_V2_USER_STRICT_PREFERRED,
Thomas Gleixner9137bb22018-11-25 19:33:53 +0100226 SPECTRE_V2_USER_PRCTL,
Thomas Gleixner6b3e64c2018-11-25 19:33:55 +0100227 SPECTRE_V2_USER_SECCOMP,
Thomas Gleixnerfa1202e2018-11-25 19:33:45 +0100228};
229
Konrad Rzeszutek Wilk24f7fc82018-04-25 22:04:21 -0400230/* The Speculative Store Bypass disable variants */
231enum ssb_mitigation {
232 SPEC_STORE_BYPASS_NONE,
233 SPEC_STORE_BYPASS_DISABLE,
Thomas Gleixnera73ec772018-04-29 15:26:40 +0200234 SPEC_STORE_BYPASS_PRCTL,
Kees Cookf21b53b2018-05-03 14:37:54 -0700235 SPEC_STORE_BYPASS_SECCOMP,
Konrad Rzeszutek Wilk24f7fc82018-04-25 22:04:21 -0400236};
237
Masami Hiramatsu736e80a2018-01-19 01:14:21 +0900238extern char __indirect_thunk_start[];
239extern char __indirect_thunk_end[];
240
David Woodhouse117cc7a2018-01-12 11:11:27 +0000241/*
242 * On VMEXIT we must ensure that no RSB predictions learned in the guest
243 * can be followed in the host, by overwriting the RSB completely. Both
244 * retpoline and IBRS mitigations for Spectre v2 need this; only on future
Darren Kennyaf189c92018-02-02 19:12:20 +0000245 * CPUs with IBRS_ALL *might* it be avoided.
David Woodhouse117cc7a2018-01-12 11:11:27 +0000246 */
247static inline void vmexit_fill_RSB(void)
248{
249#ifdef CONFIG_RETPOLINE
David Woodhoused1c99102018-02-19 10:50:56 +0000250 unsigned long loops;
251
252 asm volatile (ANNOTATE_NOSPEC_ALTERNATIVE
253 ALTERNATIVE("jmp 910f",
254 __stringify(__FILL_RETURN_BUFFER(%0, RSB_CLEAR_LOOPS, %1)),
255 X86_FEATURE_RETPOLINE)
256 "910:"
257 : "=r" (loops), ASM_CALL_CONSTRAINT
258 : : "memory" );
David Woodhouse117cc7a2018-01-12 11:11:27 +0000259#endif
260}
Andi Kleen3f7d8752018-01-17 14:53:28 -0800261
Linus Torvalds1aa7a572018-05-01 15:55:51 +0200262static __always_inline
263void alternative_msr_write(unsigned int msr, u64 val, unsigned int feature)
264{
265 asm volatile(ALTERNATIVE("", "wrmsr", %c[feature])
266 : : "c" (msr),
Jim Mattson5f2b7452018-05-13 17:33:57 -0400267 "a" ((u32)val),
268 "d" ((u32)(val >> 32)),
Linus Torvalds1aa7a572018-05-01 15:55:51 +0200269 [feature] "i" (feature)
270 : "memory");
271}
David Woodhousedd844412018-02-19 10:50:54 +0000272
David Woodhouse20ffa1c2018-01-25 16:14:15 +0000273static inline void indirect_branch_prediction_barrier(void)
274{
Konrad Rzeszutek Wilk1b868832018-04-25 22:04:18 -0400275 u64 val = PRED_CMD_IBPB;
276
277 alternative_msr_write(MSR_IA32_PRED_CMD, val, X86_FEATURE_USE_IBPB);
David Woodhouse20ffa1c2018-01-25 16:14:15 +0000278}
279
Thomas Gleixnerfa8ac492018-05-12 20:49:16 +0200280/* The Intel SPEC CTRL MSR base value cache */
281extern u64 x86_spec_ctrl_base;
282
David Woodhousedd844412018-02-19 10:50:54 +0000283/*
284 * With retpoline, we must use IBRS to restrict branch prediction
285 * before calling into firmware.
Ingo Molnard72f4e22018-02-21 09:20:37 +0100286 *
287 * (Implemented as CPP macros due to header hell.)
David Woodhousedd844412018-02-19 10:50:54 +0000288 */
Ingo Molnard72f4e22018-02-21 09:20:37 +0100289#define firmware_restrict_branch_speculation_start() \
290do { \
Thomas Gleixnerfa8ac492018-05-12 20:49:16 +0200291 u64 val = x86_spec_ctrl_base | SPEC_CTRL_IBRS; \
Konrad Rzeszutek Wilk1b868832018-04-25 22:04:18 -0400292 \
Ingo Molnard72f4e22018-02-21 09:20:37 +0100293 preempt_disable(); \
Konrad Rzeszutek Wilk1b868832018-04-25 22:04:18 -0400294 alternative_msr_write(MSR_IA32_SPEC_CTRL, val, \
Ingo Molnard72f4e22018-02-21 09:20:37 +0100295 X86_FEATURE_USE_IBRS_FW); \
296} while (0)
David Woodhousedd844412018-02-19 10:50:54 +0000297
Ingo Molnard72f4e22018-02-21 09:20:37 +0100298#define firmware_restrict_branch_speculation_end() \
299do { \
Thomas Gleixnerfa8ac492018-05-12 20:49:16 +0200300 u64 val = x86_spec_ctrl_base; \
Konrad Rzeszutek Wilk1b868832018-04-25 22:04:18 -0400301 \
302 alternative_msr_write(MSR_IA32_SPEC_CTRL, val, \
Ingo Molnard72f4e22018-02-21 09:20:37 +0100303 X86_FEATURE_USE_IBRS_FW); \
304 preempt_enable(); \
305} while (0)
David Woodhouse76b04382018-01-11 21:46:25 +0000306
Thomas Gleixnerfa1202e2018-11-25 19:33:45 +0100307DECLARE_STATIC_KEY_FALSE(switch_to_cond_stibp);
Thomas Gleixner4c71a2b62018-11-25 19:33:49 +0100308DECLARE_STATIC_KEY_FALSE(switch_mm_cond_ibpb);
309DECLARE_STATIC_KEY_FALSE(switch_mm_always_ibpb);
Thomas Gleixnerfa1202e2018-11-25 19:33:45 +0100310
Thomas Gleixner04dcbdb2019-02-18 23:42:51 +0100311DECLARE_STATIC_KEY_FALSE(mds_user_clear);
Thomas Gleixner07f07f52019-02-18 23:04:01 +0100312DECLARE_STATIC_KEY_FALSE(mds_idle_clear);
Thomas Gleixner04dcbdb2019-02-18 23:42:51 +0100313
Thomas Gleixner6a9e5292019-02-18 23:13:06 +0100314#include <asm/segment.h>
315
316/**
317 * mds_clear_cpu_buffers - Mitigation for MDS vulnerability
318 *
319 * This uses the otherwise unused and obsolete VERW instruction in
320 * combination with microcode which triggers a CPU buffer flush when the
321 * instruction is executed.
322 */
323static inline void mds_clear_cpu_buffers(void)
324{
325 static const u16 ds = __KERNEL_DS;
326
327 /*
328 * Has to be the memory-operand variant because only that
329 * guarantees the CPU buffer flush functionality according to
330 * documentation. The register-operand variant does not.
331 * Works with any segment selector, but a valid writable
332 * data segment is the fastest variant.
333 *
334 * "cc" clobber is required because VERW modifies ZF.
335 */
336 asm volatile("verw %[ds]" : : [ds] "m" (ds) : "cc");
337}
338
Thomas Gleixner04dcbdb2019-02-18 23:42:51 +0100339/**
340 * mds_user_clear_cpu_buffers - Mitigation for MDS vulnerability
341 *
342 * Clear CPU buffers if the corresponding static key is enabled
343 */
344static inline void mds_user_clear_cpu_buffers(void)
345{
346 if (static_branch_likely(&mds_user_clear))
347 mds_clear_cpu_buffers();
348}
349
Thomas Gleixner07f07f52019-02-18 23:04:01 +0100350/**
351 * mds_idle_clear_cpu_buffers - Mitigation for MDS vulnerability
352 *
353 * Clear CPU buffers if the corresponding static key is enabled
354 */
355static inline void mds_idle_clear_cpu_buffers(void)
356{
357 if (static_branch_likely(&mds_idle_clear))
358 mds_clear_cpu_buffers();
359}
360
David Woodhouse76b04382018-01-11 21:46:25 +0000361#endif /* __ASSEMBLY__ */
Daniel Borkmanna493a872018-02-22 15:12:53 +0100362
363/*
364 * Below is used in the eBPF JIT compiler and emits the byte sequence
365 * for the following assembly:
366 *
367 * With retpolines configured:
368 *
369 * callq do_rop
370 * spec_trap:
371 * pause
372 * lfence
373 * jmp spec_trap
374 * do_rop:
Wang YanQing03f57812018-05-03 14:10:43 +0800375 * mov %rax,(%rsp) for x86_64
376 * mov %edx,(%esp) for x86_32
Daniel Borkmanna493a872018-02-22 15:12:53 +0100377 * retq
378 *
379 * Without retpolines configured:
380 *
Wang YanQing03f57812018-05-03 14:10:43 +0800381 * jmp *%rax for x86_64
382 * jmp *%edx for x86_32
Daniel Borkmanna493a872018-02-22 15:12:53 +0100383 */
384#ifdef CONFIG_RETPOLINE
Daniel Borkmann36256002018-05-14 23:22:29 +0200385# ifdef CONFIG_X86_64
386# define RETPOLINE_RAX_BPF_JIT_SIZE 17
387# define RETPOLINE_RAX_BPF_JIT() \
Wang YanQing03f57812018-05-03 14:10:43 +0800388do { \
Daniel Borkmanna493a872018-02-22 15:12:53 +0100389 EMIT1_off32(0xE8, 7); /* callq do_rop */ \
390 /* spec_trap: */ \
391 EMIT2(0xF3, 0x90); /* pause */ \
392 EMIT3(0x0F, 0xAE, 0xE8); /* lfence */ \
393 EMIT2(0xEB, 0xF9); /* jmp spec_trap */ \
394 /* do_rop: */ \
395 EMIT4(0x48, 0x89, 0x04, 0x24); /* mov %rax,(%rsp) */ \
Wang YanQing03f57812018-05-03 14:10:43 +0800396 EMIT1(0xC3); /* retq */ \
397} while (0)
Daniel Borkmann36256002018-05-14 23:22:29 +0200398# else /* !CONFIG_X86_64 */
399# define RETPOLINE_EDX_BPF_JIT() \
Wang YanQing03f57812018-05-03 14:10:43 +0800400do { \
401 EMIT1_off32(0xE8, 7); /* call do_rop */ \
402 /* spec_trap: */ \
403 EMIT2(0xF3, 0x90); /* pause */ \
404 EMIT3(0x0F, 0xAE, 0xE8); /* lfence */ \
405 EMIT2(0xEB, 0xF9); /* jmp spec_trap */ \
406 /* do_rop: */ \
407 EMIT3(0x89, 0x14, 0x24); /* mov %edx,(%esp) */ \
408 EMIT1(0xC3); /* ret */ \
409} while (0)
Daniel Borkmann36256002018-05-14 23:22:29 +0200410# endif
Wang YanQing03f57812018-05-03 14:10:43 +0800411#else /* !CONFIG_RETPOLINE */
Daniel Borkmann36256002018-05-14 23:22:29 +0200412# ifdef CONFIG_X86_64
413# define RETPOLINE_RAX_BPF_JIT_SIZE 2
414# define RETPOLINE_RAX_BPF_JIT() \
415 EMIT2(0xFF, 0xE0); /* jmp *%rax */
416# else /* !CONFIG_X86_64 */
417# define RETPOLINE_EDX_BPF_JIT() \
418 EMIT2(0xFF, 0xE2) /* jmp *%edx */
419# endif
Daniel Borkmanna493a872018-02-22 15:12:53 +0100420#endif
421
Borislav Petkov7a32fc52018-01-26 13:11:37 +0100422#endif /* _ASM_X86_NOSPEC_BRANCH_H_ */