xref: /openbmc/linux/arch/x86/include/asm/nospec-branch.h (revision 4ebdac060e5e24a89a7b3ec33ec46a41621e57fe)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 
3 #ifndef _ASM_X86_NOSPEC_BRANCH_H_
4 #define _ASM_X86_NOSPEC_BRANCH_H_
5 
6 #include <linux/static_key.h>
7 #include <linux/objtool.h>
8 #include <linux/linkage.h>
9 
10 #include <asm/alternative.h>
11 #include <asm/cpufeatures.h>
12 #include <asm/msr-index.h>
13 #include <asm/unwind_hints.h>
14 #include <asm/percpu.h>
15 #include <asm/current.h>
16 
17 /*
18  * Call depth tracking for Intel SKL CPUs to address the RSB underflow
19  * issue in software.
20  *
21  * The tracking does not use a counter. It uses uses arithmetic shift
22  * right on call entry and logical shift left on return.
23  *
24  * The depth tracking variable is initialized to 0x8000.... when the call
25  * depth is zero. The arithmetic shift right sign extends the MSB and
26  * saturates after the 12th call. The shift count is 5 for both directions
27  * so the tracking covers 12 nested calls.
28  *
29  *  Call
30  *  0: 0x8000000000000000	0x0000000000000000
31  *  1: 0xfc00000000000000	0xf000000000000000
32  * ...
33  * 11: 0xfffffffffffffff8	0xfffffffffffffc00
34  * 12: 0xffffffffffffffff	0xffffffffffffffe0
35  *
36  * After a return buffer fill the depth is credited 12 calls before the
37  * next stuffing has to take place.
38  *
39  * There is a inaccuracy for situations like this:
40  *
41  *  10 calls
42  *   5 returns
43  *   3 calls
44  *   4 returns
45  *   3 calls
46  *   ....
47  *
48  * The shift count might cause this to be off by one in either direction,
49  * but there is still a cushion vs. the RSB depth. The algorithm does not
50  * claim to be perfect and it can be speculated around by the CPU, but it
51  * is considered that it obfuscates the problem enough to make exploitation
52  * extremly difficult.
53  */
54 #define RET_DEPTH_SHIFT			5
55 #define RSB_RET_STUFF_LOOPS		16
56 #define RET_DEPTH_INIT			0x8000000000000000ULL
57 #define RET_DEPTH_INIT_FROM_CALL	0xfc00000000000000ULL
58 #define RET_DEPTH_CREDIT		0xffffffffffffffffULL
59 
60 #ifdef CONFIG_CALL_THUNKS_DEBUG
61 # define CALL_THUNKS_DEBUG_INC_CALLS				\
62 	incq	%gs:__x86_call_count;
63 # define CALL_THUNKS_DEBUG_INC_RETS				\
64 	incq	%gs:__x86_ret_count;
65 # define CALL_THUNKS_DEBUG_INC_STUFFS				\
66 	incq	%gs:__x86_stuffs_count;
67 # define CALL_THUNKS_DEBUG_INC_CTXSW				\
68 	incq	%gs:__x86_ctxsw_count;
69 #else
70 # define CALL_THUNKS_DEBUG_INC_CALLS
71 # define CALL_THUNKS_DEBUG_INC_RETS
72 # define CALL_THUNKS_DEBUG_INC_STUFFS
73 # define CALL_THUNKS_DEBUG_INC_CTXSW
74 #endif
75 
76 #if defined(CONFIG_CALL_DEPTH_TRACKING) && !defined(COMPILE_OFFSETS)
77 
78 #include <asm/asm-offsets.h>
79 
80 #define CREDIT_CALL_DEPTH					\
81 	movq	$-1, PER_CPU_VAR(pcpu_hot + X86_call_depth);
82 
83 #define ASM_CREDIT_CALL_DEPTH					\
84 	movq	$-1, PER_CPU_VAR(pcpu_hot + X86_call_depth);
85 
86 #define RESET_CALL_DEPTH					\
87 	xor	%eax, %eax;					\
88 	bts	$63, %rax;					\
89 	movq	%rax, PER_CPU_VAR(pcpu_hot + X86_call_depth);
90 
91 #define RESET_CALL_DEPTH_FROM_CALL				\
92 	movb	$0xfc, %al;					\
93 	shl	$56, %rax;					\
94 	movq	%rax, PER_CPU_VAR(pcpu_hot + X86_call_depth);	\
95 	CALL_THUNKS_DEBUG_INC_CALLS
96 
97 #define INCREMENT_CALL_DEPTH					\
98 	sarq	$5, %gs:pcpu_hot + X86_call_depth;		\
99 	CALL_THUNKS_DEBUG_INC_CALLS
100 
101 #define ASM_INCREMENT_CALL_DEPTH				\
102 	sarq	$5, PER_CPU_VAR(pcpu_hot + X86_call_depth);	\
103 	CALL_THUNKS_DEBUG_INC_CALLS
104 
105 #else
106 #define CREDIT_CALL_DEPTH
107 #define ASM_CREDIT_CALL_DEPTH
108 #define RESET_CALL_DEPTH
109 #define INCREMENT_CALL_DEPTH
110 #define ASM_INCREMENT_CALL_DEPTH
111 #define RESET_CALL_DEPTH_FROM_CALL
112 #endif
113 
114 /*
115  * Fill the CPU return stack buffer.
116  *
117  * Each entry in the RSB, if used for a speculative 'ret', contains an
118  * infinite 'pause; lfence; jmp' loop to capture speculative execution.
119  *
120  * This is required in various cases for retpoline and IBRS-based
121  * mitigations for the Spectre variant 2 vulnerability. Sometimes to
122  * eliminate potentially bogus entries from the RSB, and sometimes
123  * purely to ensure that it doesn't get empty, which on some CPUs would
124  * allow predictions from other (unwanted!) sources to be used.
125  *
126  * We define a CPP macro such that it can be used from both .S files and
127  * inline assembly. It's possible to do a .macro and then include that
128  * from C via asm(".include <asm/nospec-branch.h>") but let's not go there.
129  */
130 
131 #define RETPOLINE_THUNK_SIZE	32
132 #define RSB_CLEAR_LOOPS		32	/* To forcibly overwrite all entries */
133 
134 /*
135  * Common helper for __FILL_RETURN_BUFFER and __FILL_ONE_RETURN.
136  */
137 #define __FILL_RETURN_SLOT			\
138 	ANNOTATE_INTRA_FUNCTION_CALL;		\
139 	call	772f;				\
140 	int3;					\
141 772:
142 
143 /*
144  * Stuff the entire RSB.
145  *
146  * Google experimented with loop-unrolling and this turned out to be
147  * the optimal version - two calls, each with their own speculation
148  * trap should their return address end up getting used, in a loop.
149  */
150 #ifdef CONFIG_X86_64
151 #define __FILL_RETURN_BUFFER(reg, nr)			\
152 	mov	$(nr/2), reg;				\
153 771:							\
154 	__FILL_RETURN_SLOT				\
155 	__FILL_RETURN_SLOT				\
156 	add	$(BITS_PER_LONG/8) * 2, %_ASM_SP;	\
157 	dec	reg;					\
158 	jnz	771b;					\
159 	/* barrier for jnz misprediction */		\
160 	lfence;						\
161 	ASM_CREDIT_CALL_DEPTH				\
162 	CALL_THUNKS_DEBUG_INC_CTXSW
163 #else
164 /*
165  * i386 doesn't unconditionally have LFENCE, as such it can't
166  * do a loop.
167  */
168 #define __FILL_RETURN_BUFFER(reg, nr)			\
169 	.rept nr;					\
170 	__FILL_RETURN_SLOT;				\
171 	.endr;						\
172 	add	$(BITS_PER_LONG/8) * nr, %_ASM_SP;
173 #endif
174 
175 /*
176  * Stuff a single RSB slot.
177  *
178  * To mitigate Post-Barrier RSB speculation, one CALL instruction must be
179  * forced to retire before letting a RET instruction execute.
180  *
181  * On PBRSB-vulnerable CPUs, it is not safe for a RET to be executed
182  * before this point.
183  */
184 #define __FILL_ONE_RETURN				\
185 	__FILL_RETURN_SLOT				\
186 	add	$(BITS_PER_LONG/8), %_ASM_SP;		\
187 	lfence;
188 
189 #ifdef __ASSEMBLY__
190 
191 /*
192  * This should be used immediately before an indirect jump/call. It tells
193  * objtool the subsequent indirect jump/call is vouched safe for retpoline
194  * builds.
195  */
196 .macro ANNOTATE_RETPOLINE_SAFE
197 .Lhere_\@:
198 	.pushsection .discard.retpoline_safe
199 	.long .Lhere_\@
200 	.popsection
201 .endm
202 
203 /*
204  * (ab)use RETPOLINE_SAFE on RET to annotate away 'bare' RET instructions
205  * vs RETBleed validation.
206  */
207 #define ANNOTATE_UNRET_SAFE ANNOTATE_RETPOLINE_SAFE
208 
209 /*
210  * Abuse ANNOTATE_RETPOLINE_SAFE on a NOP to indicate UNRET_END, should
211  * eventually turn into it's own annotation.
212  */
213 .macro VALIDATE_UNRET_END
214 #if defined(CONFIG_NOINSTR_VALIDATION) && \
215 	(defined(CONFIG_CPU_UNRET_ENTRY) || defined(CONFIG_CPU_SRSO))
216 	ANNOTATE_RETPOLINE_SAFE
217 	nop
218 #endif
219 .endm
220 
221 /*
222  * Emits a conditional CS prefix that is compatible with
223  * -mindirect-branch-cs-prefix.
224  */
225 .macro __CS_PREFIX reg:req
226 	.irp rs,r8,r9,r10,r11,r12,r13,r14,r15
227 	.ifc \reg,\rs
228 	.byte 0x2e
229 	.endif
230 	.endr
231 .endm
232 
233 /*
234  * JMP_NOSPEC and CALL_NOSPEC macros can be used instead of a simple
235  * indirect jmp/call which may be susceptible to the Spectre variant 2
236  * attack.
237  *
238  * NOTE: these do not take kCFI into account and are thus not comparable to C
239  * indirect calls, take care when using. The target of these should be an ENDBR
240  * instruction irrespective of kCFI.
241  */
242 .macro JMP_NOSPEC reg:req
243 #ifdef CONFIG_RETPOLINE
244 	__CS_PREFIX \reg
245 	jmp	__x86_indirect_thunk_\reg
246 #else
247 	jmp	*%\reg
248 	int3
249 #endif
250 .endm
251 
252 .macro CALL_NOSPEC reg:req
253 #ifdef CONFIG_RETPOLINE
254 	__CS_PREFIX \reg
255 	call	__x86_indirect_thunk_\reg
256 #else
257 	call	*%\reg
258 #endif
259 .endm
260 
261  /*
262   * A simpler FILL_RETURN_BUFFER macro. Don't make people use the CPP
263   * monstrosity above, manually.
264   */
265 .macro FILL_RETURN_BUFFER reg:req nr:req ftr:req ftr2=ALT_NOT(X86_FEATURE_ALWAYS)
266 	ALTERNATIVE_2 "jmp .Lskip_rsb_\@", \
267 		__stringify(__FILL_RETURN_BUFFER(\reg,\nr)), \ftr, \
268 		__stringify(nop;nop;__FILL_ONE_RETURN), \ftr2
269 
270 .Lskip_rsb_\@:
271 .endm
272 
273 /*
274  * The CALL to srso_alias_untrain_ret() must be patched in directly at
275  * the spot where untraining must be done, ie., srso_alias_untrain_ret()
276  * must be the target of a CALL instruction instead of indirectly
277  * jumping to a wrapper which then calls it. Therefore, this macro is
278  * called outside of __UNTRAIN_RET below, for the time being, before the
279  * kernel can support nested alternatives with arbitrary nesting.
280  */
281 .macro CALL_UNTRAIN_RET
282 #if defined(CONFIG_CPU_UNRET_ENTRY) || defined(CONFIG_CPU_SRSO)
283 	ALTERNATIVE_2 "", "call entry_untrain_ret", X86_FEATURE_UNRET, \
284 		          "call srso_alias_untrain_ret", X86_FEATURE_SRSO_ALIAS
285 #endif
286 .endm
287 
288 /*
289  * Mitigate RETBleed for AMD/Hygon Zen uarch. Requires KERNEL CR3 because the
290  * return thunk isn't mapped into the userspace tables (then again, AMD
291  * typically has NO_MELTDOWN).
292  *
293  * While retbleed_untrain_ret() doesn't clobber anything but requires stack,
294  * entry_ibpb() will clobber AX, CX, DX.
295  *
296  * As such, this must be placed after every *SWITCH_TO_KERNEL_CR3 at a point
297  * where we have a stack but before any RET instruction.
298  */
299 .macro __UNTRAIN_RET ibpb_feature, call_depth_insns
300 #if defined(CONFIG_RETHUNK) || defined(CONFIG_CPU_IBPB_ENTRY)
301 	VALIDATE_UNRET_END
302 	CALL_UNTRAIN_RET
303 	ALTERNATIVE_2 "",						\
304 		      "call entry_ibpb", \ibpb_feature,			\
305 		     __stringify(\call_depth_insns), X86_FEATURE_CALL_DEPTH
306 #endif
307 .endm
308 
309 #define UNTRAIN_RET \
310 	__UNTRAIN_RET X86_FEATURE_ENTRY_IBPB, __stringify(RESET_CALL_DEPTH)
311 
312 #define UNTRAIN_RET_VM \
313 	__UNTRAIN_RET X86_FEATURE_IBPB_ON_VMEXIT, __stringify(RESET_CALL_DEPTH)
314 
315 #define UNTRAIN_RET_FROM_CALL \
316 	__UNTRAIN_RET X86_FEATURE_ENTRY_IBPB, __stringify(RESET_CALL_DEPTH_FROM_CALL)
317 
318 
319 .macro CALL_DEPTH_ACCOUNT
320 #ifdef CONFIG_CALL_DEPTH_TRACKING
321 	ALTERNATIVE "",							\
322 		    __stringify(ASM_INCREMENT_CALL_DEPTH), X86_FEATURE_CALL_DEPTH
323 #endif
324 .endm
325 
326 /*
327  * Macro to execute VERW insns that mitigate transient data sampling
328  * attacks such as MDS or TSA. On affected systems a microcode update
329  * overloaded VERW insns to also clear the CPU buffers. VERW clobbers
330  * CFLAGS.ZF.
331  * Note: Only the memory operand variant of VERW clears the CPU buffers.
332  */
333 .macro __CLEAR_CPU_BUFFERS feature
334 #ifdef CONFIG_X86_64
335 	ALTERNATIVE "", "verw x86_verw_sel(%rip)", \feature
336 #else
337 	/*
338 	 * In 32bit mode, the memory operand must be a %cs reference. The data
339 	 * segments may not be usable (vm86 mode), and the stack segment may not
340 	 * be flat (ESPFIX32).
341 	 */
342 	ALTERNATIVE "", "verw %cs:x86_verw_sel", \feature
343 #endif
344 .endm
345 
346 #define CLEAR_CPU_BUFFERS \
347 	__CLEAR_CPU_BUFFERS X86_FEATURE_CLEAR_CPU_BUF
348 
349 #define VM_CLEAR_CPU_BUFFERS \
350 	__CLEAR_CPU_BUFFERS X86_FEATURE_CLEAR_CPU_BUF_VM
351 
352 #ifdef CONFIG_X86_64
353 .macro CLEAR_BRANCH_HISTORY
354 	ALTERNATIVE "", "call clear_bhb_loop", X86_FEATURE_CLEAR_BHB_LOOP
355 .endm
356 
357 .macro CLEAR_BRANCH_HISTORY_VMEXIT
358 	ALTERNATIVE "", "call clear_bhb_loop", X86_FEATURE_CLEAR_BHB_LOOP_ON_VMEXIT
359 .endm
360 #else
361 #define CLEAR_BRANCH_HISTORY
362 #define CLEAR_BRANCH_HISTORY_VMEXIT
363 #endif
364 
365 #else /* __ASSEMBLY__ */
366 
367 #define ANNOTATE_RETPOLINE_SAFE					\
368 	"999:\n\t"						\
369 	".pushsection .discard.retpoline_safe\n\t"		\
370 	".long 999b\n\t"					\
371 	".popsection\n\t"
372 
373 #define ITS_THUNK_SIZE	64
374 
375 typedef u8 retpoline_thunk_t[RETPOLINE_THUNK_SIZE];
376 typedef u8 its_thunk_t[ITS_THUNK_SIZE];
377 extern retpoline_thunk_t __x86_indirect_thunk_array[];
378 extern retpoline_thunk_t __x86_indirect_call_thunk_array[];
379 extern retpoline_thunk_t __x86_indirect_jump_thunk_array[];
380 extern its_thunk_t	 __x86_indirect_its_thunk_array[];
381 
382 #ifdef CONFIG_RETHUNK
383 extern void __x86_return_thunk(void);
384 #else
__x86_return_thunk(void)385 static inline void __x86_return_thunk(void) {}
386 #endif
387 
388 #ifdef CONFIG_CPU_UNRET_ENTRY
389 extern void retbleed_return_thunk(void);
390 #else
retbleed_return_thunk(void)391 static inline void retbleed_return_thunk(void) {}
392 #endif
393 
394 extern void srso_alias_untrain_ret(void);
395 
396 #ifdef CONFIG_CPU_SRSO
397 extern void srso_return_thunk(void);
398 extern void srso_alias_return_thunk(void);
399 #else
srso_return_thunk(void)400 static inline void srso_return_thunk(void) {}
srso_alias_return_thunk(void)401 static inline void srso_alias_return_thunk(void) {}
402 #endif
403 
404 #ifdef CONFIG_MITIGATION_ITS
405 extern void its_return_thunk(void);
406 #else
its_return_thunk(void)407 static inline void its_return_thunk(void) {}
408 #endif
409 
410 extern void retbleed_return_thunk(void);
411 extern void srso_return_thunk(void);
412 extern void srso_alias_return_thunk(void);
413 
414 extern void retbleed_untrain_ret(void);
415 extern void srso_untrain_ret(void);
416 extern void srso_alias_untrain_ret(void);
417 
418 extern void entry_untrain_ret(void);
419 extern void entry_ibpb(void);
420 
421 #ifdef CONFIG_X86_64
422 extern void clear_bhb_loop(void);
423 #endif
424 
425 extern void (*x86_return_thunk)(void);
426 
427 #ifdef CONFIG_CALL_DEPTH_TRACKING
428 extern void __x86_return_skl(void);
429 
430 #define CALL_DEPTH_ACCOUNT					\
431 	ALTERNATIVE("",						\
432 		    __stringify(INCREMENT_CALL_DEPTH),		\
433 		    X86_FEATURE_CALL_DEPTH)
434 
435 #ifdef CONFIG_CALL_THUNKS_DEBUG
436 DECLARE_PER_CPU(u64, __x86_call_count);
437 DECLARE_PER_CPU(u64, __x86_ret_count);
438 DECLARE_PER_CPU(u64, __x86_stuffs_count);
439 DECLARE_PER_CPU(u64, __x86_ctxsw_count);
440 #endif
441 #else
442 
443 #define CALL_DEPTH_ACCOUNT ""
444 
445 #endif
446 
447 #ifdef CONFIG_RETPOLINE
448 
449 #define GEN(reg) \
450 	extern retpoline_thunk_t __x86_indirect_thunk_ ## reg;
451 #include <asm/GEN-for-each-reg.h>
452 #undef GEN
453 
454 #define GEN(reg)						\
455 	extern retpoline_thunk_t __x86_indirect_call_thunk_ ## reg;
456 #include <asm/GEN-for-each-reg.h>
457 #undef GEN
458 
459 #define GEN(reg)						\
460 	extern retpoline_thunk_t __x86_indirect_jump_thunk_ ## reg;
461 #include <asm/GEN-for-each-reg.h>
462 #undef GEN
463 
464 #ifdef CONFIG_X86_64
465 
466 /*
467  * Emits a conditional CS prefix that is compatible with
468  * -mindirect-branch-cs-prefix.
469  */
470 #define __CS_PREFIX(reg)				\
471 	".irp rs,r8,r9,r10,r11,r12,r13,r14,r15\n"	\
472 	".ifc \\rs," reg "\n"				\
473 	".byte 0x2e\n"					\
474 	".endif\n"					\
475 	".endr\n"
476 
477 /*
478  * Inline asm uses the %V modifier which is only in newer GCC
479  * which is ensured when CONFIG_RETPOLINE is defined.
480  */
481 #define CALL_NOSPEC	__CS_PREFIX("%V[thunk_target]")	\
482 			"call __x86_indirect_thunk_%V[thunk_target]\n"
483 
484 # define THUNK_TARGET(addr) [thunk_target] "r" (addr)
485 
486 #else /* CONFIG_X86_32 */
487 /*
488  * For i386 we use the original ret-equivalent retpoline, because
489  * otherwise we'll run out of registers. We don't care about CET
490  * here, anyway.
491  */
492 # define CALL_NOSPEC						\
493 	ALTERNATIVE_2(						\
494 	ANNOTATE_RETPOLINE_SAFE					\
495 	"call *%[thunk_target]\n",				\
496 	"       jmp    904f;\n"					\
497 	"       .align 16\n"					\
498 	"901:	call   903f;\n"					\
499 	"902:	pause;\n"					\
500 	"    	lfence;\n"					\
501 	"       jmp    902b;\n"					\
502 	"       .align 16\n"					\
503 	"903:	lea    4(%%esp), %%esp;\n"			\
504 	"       pushl  %[thunk_target];\n"			\
505 	"       ret;\n"						\
506 	"       .align 16\n"					\
507 	"904:	call   901b;\n",				\
508 	X86_FEATURE_RETPOLINE,					\
509 	"lfence;\n"						\
510 	ANNOTATE_RETPOLINE_SAFE					\
511 	"call *%[thunk_target]\n",				\
512 	X86_FEATURE_RETPOLINE_LFENCE)
513 
514 # define THUNK_TARGET(addr) [thunk_target] "rm" (addr)
515 #endif
516 #else /* No retpoline for C / inline asm */
517 # define CALL_NOSPEC "call *%[thunk_target]\n"
518 # define THUNK_TARGET(addr) [thunk_target] "rm" (addr)
519 #endif
520 
521 /* The Spectre V2 mitigation variants */
522 enum spectre_v2_mitigation {
523 	SPECTRE_V2_NONE,
524 	SPECTRE_V2_RETPOLINE,
525 	SPECTRE_V2_LFENCE,
526 	SPECTRE_V2_EIBRS,
527 	SPECTRE_V2_EIBRS_RETPOLINE,
528 	SPECTRE_V2_EIBRS_LFENCE,
529 	SPECTRE_V2_IBRS,
530 };
531 
532 /* The indirect branch speculation control variants */
533 enum spectre_v2_user_mitigation {
534 	SPECTRE_V2_USER_NONE,
535 	SPECTRE_V2_USER_STRICT,
536 	SPECTRE_V2_USER_STRICT_PREFERRED,
537 	SPECTRE_V2_USER_PRCTL,
538 	SPECTRE_V2_USER_SECCOMP,
539 };
540 
541 /* The Speculative Store Bypass disable variants */
542 enum ssb_mitigation {
543 	SPEC_STORE_BYPASS_NONE,
544 	SPEC_STORE_BYPASS_DISABLE,
545 	SPEC_STORE_BYPASS_PRCTL,
546 	SPEC_STORE_BYPASS_SECCOMP,
547 };
548 
549 static __always_inline
alternative_msr_write(unsigned int msr,u64 val,unsigned int feature)550 void alternative_msr_write(unsigned int msr, u64 val, unsigned int feature)
551 {
552 	asm volatile(ALTERNATIVE("", "wrmsr", %c[feature])
553 		: : "c" (msr),
554 		    "a" ((u32)val),
555 		    "d" ((u32)(val >> 32)),
556 		    [feature] "i" (feature)
557 		: "memory");
558 }
559 
560 extern u64 x86_pred_cmd;
561 
indirect_branch_prediction_barrier(void)562 static inline void indirect_branch_prediction_barrier(void)
563 {
564 	alternative_msr_write(MSR_IA32_PRED_CMD, x86_pred_cmd, X86_FEATURE_USE_IBPB);
565 }
566 
567 /* The Intel SPEC CTRL MSR base value cache */
568 extern u64 x86_spec_ctrl_base;
569 DECLARE_PER_CPU(u64, x86_spec_ctrl_current);
570 extern void update_spec_ctrl_cond(u64 val);
571 extern u64 spec_ctrl_current(void);
572 
573 /*
574  * With retpoline, we must use IBRS to restrict branch prediction
575  * before calling into firmware.
576  *
577  * (Implemented as CPP macros due to header hell.)
578  */
579 #define firmware_restrict_branch_speculation_start()			\
580 do {									\
581 	preempt_disable();						\
582 	alternative_msr_write(MSR_IA32_SPEC_CTRL,			\
583 			      spec_ctrl_current() | SPEC_CTRL_IBRS,	\
584 			      X86_FEATURE_USE_IBRS_FW);			\
585 	alternative_msr_write(MSR_IA32_PRED_CMD, PRED_CMD_IBPB,		\
586 			      X86_FEATURE_USE_IBPB_FW);			\
587 } while (0)
588 
589 #define firmware_restrict_branch_speculation_end()			\
590 do {									\
591 	alternative_msr_write(MSR_IA32_SPEC_CTRL,			\
592 			      spec_ctrl_current(),			\
593 			      X86_FEATURE_USE_IBRS_FW);			\
594 	preempt_enable();						\
595 } while (0)
596 
597 DECLARE_STATIC_KEY_FALSE(switch_to_cond_stibp);
598 DECLARE_STATIC_KEY_FALSE(switch_mm_cond_ibpb);
599 DECLARE_STATIC_KEY_FALSE(switch_mm_always_ibpb);
600 
601 DECLARE_STATIC_KEY_FALSE(cpu_buf_idle_clear);
602 
603 DECLARE_STATIC_KEY_FALSE(switch_mm_cond_l1d_flush);
604 
605 DECLARE_STATIC_KEY_FALSE(mmio_stale_data_clear);
606 
607 extern u16 x86_verw_sel;
608 
609 #include <asm/segment.h>
610 
611 /**
612  * x86_clear_cpu_buffers - Buffer clearing support for different x86 CPU vulns
613  *
614  * This uses the otherwise unused and obsolete VERW instruction in
615  * combination with microcode which triggers a CPU buffer flush when the
616  * instruction is executed.
617  */
x86_clear_cpu_buffers(void)618 static __always_inline void x86_clear_cpu_buffers(void)
619 {
620 	static const u16 ds = __KERNEL_DS;
621 
622 	/*
623 	 * Has to be the memory-operand variant because only that
624 	 * guarantees the CPU buffer flush functionality according to
625 	 * documentation. The register-operand variant does not.
626 	 * Works with any segment selector, but a valid writable
627 	 * data segment is the fastest variant.
628 	 *
629 	 * "cc" clobber is required because VERW modifies ZF.
630 	 */
631 	asm volatile("verw %[ds]" : : [ds] "m" (ds) : "cc");
632 }
633 
634 /**
635  * x86_idle_clear_cpu_buffers - Buffer clearing support in idle for the MDS
636  * and TSA vulnerabilities.
637  *
638  * Clear CPU buffers if the corresponding static key is enabled
639  */
x86_idle_clear_cpu_buffers(void)640 static __always_inline void x86_idle_clear_cpu_buffers(void)
641 {
642 	if (static_branch_likely(&cpu_buf_idle_clear))
643 		x86_clear_cpu_buffers();
644 }
645 
646 #endif /* __ASSEMBLY__ */
647 
648 #endif /* _ASM_X86_NOSPEC_BRANCH_H_ */
649