blob: 170c89ed22fcd3a27106d166a9e7f5a5d1fadf80 [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 Gleixnerfa1202ef2018-11-25 19:33:45 +01006#include <linux/static_key.h>
Julien Thierry00089c02020-09-04 16:30:25 +01007#include <linux/objtool.h>
Peter Zijlstra6fda8a32021-10-26 14:01:40 +02008#include <linux/linkage.h>
Thomas Gleixnerfa1202ef2018-11-25 19:33:45 +01009
David Woodhouse76b04382018-01-11 21:46:25 +000010#include <asm/alternative.h>
David Woodhouse76b04382018-01-11 21:46:25 +000011#include <asm/cpufeatures.h>
Peter Zijlstraea00f302018-02-13 14:28:19 +010012#include <asm/msr-index.h>
Peter Zijlstra089dd8e2020-04-14 12:36:16 +020013#include <asm/unwind_hints.h>
Nathan Chancellordb886972022-07-13 08:24:37 -070014#include <asm/percpu.h>
Thomas Gleixner5d821382022-09-15 13:11:27 +020015#include <asm/current.h>
David Woodhouse76b04382018-01-11 21:46:25 +000016
Thomas Gleixner5d821382022-09-15 13:11:27 +020017/*
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
Bjorn Helgaas54aa6992024-01-02 18:40:11 -060052 * extremely difficult.
Thomas Gleixner5d821382022-09-15 13:11:27 +020053 */
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
Thomas Gleixnerf5c1bb22022-09-15 13:11:30 +020060#ifdef CONFIG_CALL_THUNKS_DEBUG
61# define CALL_THUNKS_DEBUG_INC_CALLS \
Uros Bizjak2adeed92023-11-05 22:34:37 +010062 incq PER_CPU_VAR(__x86_call_count);
Thomas Gleixnerf5c1bb22022-09-15 13:11:30 +020063# define CALL_THUNKS_DEBUG_INC_RETS \
Uros Bizjak2adeed92023-11-05 22:34:37 +010064 incq PER_CPU_VAR(__x86_ret_count);
Thomas Gleixnerf5c1bb22022-09-15 13:11:30 +020065# define CALL_THUNKS_DEBUG_INC_STUFFS \
Uros Bizjak2adeed92023-11-05 22:34:37 +010066 incq PER_CPU_VAR(__x86_stuffs_count);
Thomas Gleixnerf5c1bb22022-09-15 13:11:30 +020067# define CALL_THUNKS_DEBUG_INC_CTXSW \
Uros Bizjak2adeed92023-11-05 22:34:37 +010068 incq PER_CPU_VAR(__x86_ctxsw_count);
Thomas Gleixnerf5c1bb22022-09-15 13:11:30 +020069#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
Breno Leitao5fa31af2023-11-21 08:07:30 -080076#if defined(CONFIG_MITIGATION_CALL_DEPTH_TRACKING) && !defined(COMPILE_OFFSETS)
Thomas Gleixner5d821382022-09-15 13:11:27 +020077
78#include <asm/asm-offsets.h>
79
80#define CREDIT_CALL_DEPTH \
81 movq $-1, PER_CPU_VAR(pcpu_hot + X86_call_depth);
82
Thomas Gleixner5d821382022-09-15 13:11:27 +020083#define RESET_CALL_DEPTH \
Peter Zijlstra3496d1c2023-02-10 10:10:57 +000084 xor %eax, %eax; \
85 bts $63, %rax; \
Thomas Gleixner5d821382022-09-15 13:11:27 +020086 movq %rax, PER_CPU_VAR(pcpu_hot + X86_call_depth);
87
88#define RESET_CALL_DEPTH_FROM_CALL \
Peter Zijlstra3496d1c2023-02-10 10:10:57 +000089 movb $0xfc, %al; \
Thomas Gleixner5d821382022-09-15 13:11:27 +020090 shl $56, %rax; \
Thomas Gleixnerf5c1bb22022-09-15 13:11:30 +020091 movq %rax, PER_CPU_VAR(pcpu_hot + X86_call_depth); \
92 CALL_THUNKS_DEBUG_INC_CALLS
Thomas Gleixner5d821382022-09-15 13:11:27 +020093
94#define INCREMENT_CALL_DEPTH \
Thomas Gleixnerf5c1bb22022-09-15 13:11:30 +020095 sarq $5, PER_CPU_VAR(pcpu_hot + X86_call_depth); \
96 CALL_THUNKS_DEBUG_INC_CALLS
Thomas Gleixner5d821382022-09-15 13:11:27 +020097
98#else
99#define CREDIT_CALL_DEPTH
100#define RESET_CALL_DEPTH
Thomas Gleixner5d821382022-09-15 13:11:27 +0200101#define RESET_CALL_DEPTH_FROM_CALL
Uros Bizjak2adeed92023-11-05 22:34:37 +0100102#define INCREMENT_CALL_DEPTH
Thomas Gleixner5d821382022-09-15 13:11:27 +0200103#endif
Peter Zijlstra1a6f7442021-10-26 14:01:41 +0200104
David Woodhoused1c99102018-02-19 10:50:56 +0000105/*
106 * Fill the CPU return stack buffer.
107 *
108 * Each entry in the RSB, if used for a speculative 'ret', contains an
109 * infinite 'pause; lfence; jmp' loop to capture speculative execution.
110 *
111 * This is required in various cases for retpoline and IBRS-based
112 * mitigations for the Spectre variant 2 vulnerability. Sometimes to
113 * eliminate potentially bogus entries from the RSB, and sometimes
114 * purely to ensure that it doesn't get empty, which on some CPUs would
115 * allow predictions from other (unwanted!) sources to be used.
116 *
117 * We define a CPP macro such that it can be used from both .S files and
118 * inline assembly. It's possible to do a .macro and then include that
119 * from C via asm(".include <asm/nospec-branch.h>") but let's not go there.
120 */
121
Thomas Gleixner5d821382022-09-15 13:11:27 +0200122#define RETPOLINE_THUNK_SIZE 32
David Woodhoused1c99102018-02-19 10:50:56 +0000123#define RSB_CLEAR_LOOPS 32 /* To forcibly overwrite all entries */
David Woodhoused1c99102018-02-19 10:50:56 +0000124
125/*
Peter Zijlstra4e3aa922022-08-16 14:28:36 +0200126 * Common helper for __FILL_RETURN_BUFFER and __FILL_ONE_RETURN.
127 */
128#define __FILL_RETURN_SLOT \
129 ANNOTATE_INTRA_FUNCTION_CALL; \
130 call 772f; \
131 int3; \
132772:
133
134/*
135 * Stuff the entire RSB.
136 *
David Woodhoused1c99102018-02-19 10:50:56 +0000137 * Google experimented with loop-unrolling and this turned out to be
Ingo Molnarc681df82021-03-21 23:32:33 +0100138 * the optimal version - two calls, each with their own speculation
David Woodhoused1c99102018-02-19 10:50:56 +0000139 * trap should their return address end up getting used, in a loop.
140 */
Peter Zijlstra33292492022-08-19 13:01:35 +0200141#ifdef CONFIG_X86_64
Peter Zijlstra4e3aa922022-08-16 14:28:36 +0200142#define __FILL_RETURN_BUFFER(reg, nr) \
143 mov $(nr/2), reg; \
144771: \
145 __FILL_RETURN_SLOT \
146 __FILL_RETURN_SLOT \
147 add $(BITS_PER_LONG/8) * 2, %_ASM_SP; \
148 dec reg; \
149 jnz 771b; \
150 /* barrier for jnz misprediction */ \
Thomas Gleixner5d821382022-09-15 13:11:27 +0200151 lfence; \
Uros Bizjak2adeed92023-11-05 22:34:37 +0100152 CREDIT_CALL_DEPTH \
Thomas Gleixnerf5c1bb22022-09-15 13:11:30 +0200153 CALL_THUNKS_DEBUG_INC_CTXSW
Peter Zijlstra33292492022-08-19 13:01:35 +0200154#else
155/*
156 * i386 doesn't unconditionally have LFENCE, as such it can't
157 * do a loop.
158 */
159#define __FILL_RETURN_BUFFER(reg, nr) \
160 .rept nr; \
161 __FILL_RETURN_SLOT; \
162 .endr; \
163 add $(BITS_PER_LONG/8) * nr, %_ASM_SP;
164#endif
Peter Zijlstra4e3aa922022-08-16 14:28:36 +0200165
166/*
167 * Stuff a single RSB slot.
168 *
169 * To mitigate Post-Barrier RSB speculation, one CALL instruction must be
170 * forced to retire before letting a RET instruction execute.
171 *
172 * On PBRSB-vulnerable CPUs, it is not safe for a RET to be executed
173 * before this point.
174 */
175#define __FILL_ONE_RETURN \
176 __FILL_RETURN_SLOT \
177 add $(BITS_PER_LONG/8), %_ASM_SP; \
Pawan Guptaba6e31a2022-08-02 15:47:02 -0700178 lfence;
David Woodhoused1c99102018-02-19 10:50:56 +0000179
David Woodhouse76b04382018-01-11 21:46:25 +0000180#ifdef __ASSEMBLY__
181
182/*
Peter Zijlstra9e0e3c52018-01-17 22:34:34 +0100183 * This should be used immediately before an indirect jump/call. It tells
184 * objtool the subsequent indirect jump/call is vouched safe for retpoline
185 * builds.
186 */
187.macro ANNOTATE_RETPOLINE_SAFE
Josh Poimboeuf1c0c1fa2023-03-01 07:13:08 -0800188.Lhere_\@:
Peter Zijlstra9e0e3c52018-01-17 22:34:34 +0100189 .pushsection .discard.retpoline_safe
Fangrui Songb8ec60e2023-09-19 17:17:28 -0700190 .long .Lhere_\@
Peter Zijlstra9e0e3c52018-01-17 22:34:34 +0100191 .popsection
192.endm
193
194/*
Peter Zijlstra9bb2ec62022-06-14 23:15:59 +0200195 * (ab)use RETPOLINE_SAFE on RET to annotate away 'bare' RET instructions
196 * vs RETBleed validation.
197 */
198#define ANNOTATE_UNRET_SAFE ANNOTATE_RETPOLINE_SAFE
199
200/*
Peter Zijlstraa09a6e22022-06-14 23:16:03 +0200201 * Abuse ANNOTATE_RETPOLINE_SAFE on a NOP to indicate UNRET_END, should
Bjorn Helgaas54aa6992024-01-02 18:40:11 -0600202 * eventually turn into its own annotation.
Peter Zijlstraa09a6e22022-06-14 23:16:03 +0200203 */
Josh Poimboeuf4708ea12023-03-01 07:13:11 -0800204.macro VALIDATE_UNRET_END
Borislav Petkov (AMD)fb3bd912023-06-28 11:02:39 +0200205#if defined(CONFIG_NOINSTR_VALIDATION) && \
Breno Leitaoa033eec2023-11-21 08:07:36 -0800206 (defined(CONFIG_MITIGATION_UNRET_ENTRY) || defined(CONFIG_MITIGATION_SRSO))
Peter Zijlstraa09a6e22022-06-14 23:16:03 +0200207 ANNOTATE_RETPOLINE_SAFE
208 nop
209#endif
210.endm
211
212/*
Peter Zijlstra09d09532022-07-20 12:04:21 +0200213 * Equivalent to -mindirect-branch-cs-prefix; emit the 5 byte jmp/call
214 * to the retpoline thunk with a CS prefix when the register requires
215 * a RAX prefix byte to encode. Also see apply_retpolines().
216 */
217.macro __CS_PREFIX reg:req
218 .irp rs,r8,r9,r10,r11,r12,r13,r14,r15
219 .ifc \reg,\rs
220 .byte 0x2e
221 .endif
222 .endr
223.endm
224
225/*
David Woodhouse76b04382018-01-11 21:46:25 +0000226 * JMP_NOSPEC and CALL_NOSPEC macros can be used instead of a simple
227 * indirect jmp/call which may be susceptible to the Spectre variant 2
228 * attack.
Peter Zijlstra0479a422023-06-22 16:27:13 +0200229 *
230 * NOTE: these do not take kCFI into account and are thus not comparable to C
231 * indirect calls, take care when using. The target of these should be an ENDBR
232 * instruction irrespective of kCFI.
David Woodhouse76b04382018-01-11 21:46:25 +0000233 */
234.macro JMP_NOSPEC reg:req
Breno Leitaoaefb2f22023-11-21 08:07:32 -0800235#ifdef CONFIG_MITIGATION_RETPOLINE
Peter Zijlstra09d09532022-07-20 12:04:21 +0200236 __CS_PREFIX \reg
237 jmp __x86_indirect_thunk_\reg
David Woodhouse76b04382018-01-11 21:46:25 +0000238#else
Peter Zijlstra34fdce62020-04-22 17:16:40 +0200239 jmp *%\reg
Peter Zijlstra09d09532022-07-20 12:04:21 +0200240 int3
David Woodhouse76b04382018-01-11 21:46:25 +0000241#endif
242.endm
243
244.macro CALL_NOSPEC reg:req
Breno Leitaoaefb2f22023-11-21 08:07:32 -0800245#ifdef CONFIG_MITIGATION_RETPOLINE
Peter Zijlstra09d09532022-07-20 12:04:21 +0200246 __CS_PREFIX \reg
247 call __x86_indirect_thunk_\reg
David Woodhouse76b04382018-01-11 21:46:25 +0000248#else
Peter Zijlstra34fdce62020-04-22 17:16:40 +0200249 call *%\reg
David Woodhouse76b04382018-01-11 21:46:25 +0000250#endif
251.endm
252
David Woodhoused1c99102018-02-19 10:50:56 +0000253 /*
254 * A simpler FILL_RETURN_BUFFER macro. Don't make people use the CPP
255 * monstrosity above, manually.
256 */
Peter Zijlstra4e3aa922022-08-16 14:28:36 +0200257.macro FILL_RETURN_BUFFER reg:req nr:req ftr:req ftr2=ALT_NOT(X86_FEATURE_ALWAYS)
258 ALTERNATIVE_2 "jmp .Lskip_rsb_\@", \
259 __stringify(__FILL_RETURN_BUFFER(\reg,\nr)), \ftr, \
Peter Zijlstra6ea17e82023-02-08 18:18:04 +0100260 __stringify(nop;nop;__FILL_ONE_RETURN), \ftr2
Peter Zijlstra4e3aa922022-08-16 14:28:36 +0200261
David Woodhoused1c99102018-02-19 10:50:56 +0000262.Lskip_rsb_\@:
David Woodhouse117cc7a2018-01-12 11:11:27 +0000263.endm
264
Borislav Petkov (AMD)4535e1a2024-03-28 13:59:05 +0100265/*
266 * The CALL to srso_alias_untrain_ret() must be patched in directly at
267 * the spot where untraining must be done, ie., srso_alias_untrain_ret()
268 * must be the target of a CALL instruction instead of indirectly
269 * jumping to a wrapper which then calls it. Therefore, this macro is
270 * called outside of __UNTRAIN_RET below, for the time being, before the
271 * kernel can support nested alternatives with arbitrary nesting.
272 */
273.macro CALL_UNTRAIN_RET
Breno Leitaoa033eec2023-11-21 08:07:36 -0800274#if defined(CONFIG_MITIGATION_UNRET_ENTRY) || defined(CONFIG_MITIGATION_SRSO)
Borislav Petkov (AMD)4535e1a2024-03-28 13:59:05 +0100275 ALTERNATIVE_2 "", "call entry_untrain_ret", X86_FEATURE_UNRET, \
276 "call srso_alias_untrain_ret", X86_FEATURE_SRSO_ALIAS
Peter Zijlstraf43b98762022-06-27 22:21:17 +0000277#endif
Borislav Petkov (AMD)4535e1a2024-03-28 13:59:05 +0100278.endm
Peter Zijlstraf43b98762022-06-27 22:21:17 +0000279
Peter Zijlstraa1491802022-06-14 23:15:48 +0200280/*
281 * Mitigate RETBleed for AMD/Hygon Zen uarch. Requires KERNEL CR3 because the
282 * return thunk isn't mapped into the userspace tables (then again, AMD
283 * typically has NO_MELTDOWN).
284 *
Peter Zijlstrad025b7b2023-08-14 13:44:32 +0200285 * While retbleed_untrain_ret() doesn't clobber anything but requires stack,
Peter Zijlstra3ebc1702022-06-14 23:16:02 +0200286 * entry_ibpb() will clobber AX, CX, DX.
Peter Zijlstraa1491802022-06-14 23:15:48 +0200287 *
288 * As such, this must be placed after every *SWITCH_TO_KERNEL_CR3 at a point
289 * where we have a stack but before any RET instruction.
290 */
Josh Poimboeufe8efc082023-09-04 22:05:03 -0700291.macro __UNTRAIN_RET ibpb_feature, call_depth_insns
Breno Leitao0911b8c2023-11-21 08:07:37 -0800292#if defined(CONFIG_MITIGATION_RETHUNK) || defined(CONFIG_MITIGATION_IBPB_ENTRY)
Josh Poimboeuf4708ea12023-03-01 07:13:11 -0800293 VALIDATE_UNRET_END
Borislav Petkov (AMD)4535e1a2024-03-28 13:59:05 +0100294 CALL_UNTRAIN_RET
295 ALTERNATIVE_2 "", \
Josh Poimboeufe8efc082023-09-04 22:05:03 -0700296 "call entry_ibpb", \ibpb_feature, \
297 __stringify(\call_depth_insns), X86_FEATURE_CALL_DEPTH
Thomas Gleixner5d821382022-09-15 13:11:27 +0200298#endif
299.endm
300
Josh Poimboeufe8efc082023-09-04 22:05:03 -0700301#define UNTRAIN_RET \
302 __UNTRAIN_RET X86_FEATURE_ENTRY_IBPB, __stringify(RESET_CALL_DEPTH)
Peter Zijlstra864bcaa2023-08-14 13:44:35 +0200303
Josh Poimboeufe8efc082023-09-04 22:05:03 -0700304#define UNTRAIN_RET_VM \
305 __UNTRAIN_RET X86_FEATURE_IBPB_ON_VMEXIT, __stringify(RESET_CALL_DEPTH)
306
307#define UNTRAIN_RET_FROM_CALL \
308 __UNTRAIN_RET X86_FEATURE_ENTRY_IBPB, __stringify(RESET_CALL_DEPTH_FROM_CALL)
Thomas Gleixner5d821382022-09-15 13:11:27 +0200309
310
311.macro CALL_DEPTH_ACCOUNT
Breno Leitao5fa31af2023-11-21 08:07:30 -0800312#ifdef CONFIG_MITIGATION_CALL_DEPTH_TRACKING
Thomas Gleixner5d821382022-09-15 13:11:27 +0200313 ALTERNATIVE "", \
Uros Bizjak2adeed92023-11-05 22:34:37 +0100314 __stringify(INCREMENT_CALL_DEPTH), X86_FEATURE_CALL_DEPTH
Peter Zijlstraa1491802022-06-14 23:15:48 +0200315#endif
316.endm
317
Pawan Guptabaf83612024-02-13 18:21:35 -0800318/*
319 * Macro to execute VERW instruction that mitigate transient data sampling
320 * attacks such as MDS. On affected systems a microcode update overloaded VERW
321 * instruction to also clear the CPU buffers. VERW clobbers CFLAGS.ZF.
322 *
323 * Note: Only the memory operand variant of VERW clears the CPU buffers.
324 */
325.macro CLEAR_CPU_BUFFERS
Dave Hansen532a0c52024-03-12 07:27:57 -0700326 ALTERNATIVE "", __stringify(verw _ASM_RIP(mds_verw_sel)), X86_FEATURE_CLEAR_CPU_BUF
Pawan Guptabaf83612024-02-13 18:21:35 -0800327.endm
328
David Woodhouse76b04382018-01-11 21:46:25 +0000329#else /* __ASSEMBLY__ */
330
Peter Zijlstra9e0e3c52018-01-17 22:34:34 +0100331#define ANNOTATE_RETPOLINE_SAFE \
332 "999:\n\t" \
333 ".pushsection .discard.retpoline_safe\n\t" \
Fangrui Songb8ec60e2023-09-19 17:17:28 -0700334 ".long 999b\n\t" \
Peter Zijlstra9e0e3c52018-01-17 22:34:34 +0100335 ".popsection\n\t"
336
Peter Zijlstra1a6f7442021-10-26 14:01:41 +0200337typedef u8 retpoline_thunk_t[RETPOLINE_THUNK_SIZE];
Peter Zijlstra369ae6f2022-06-14 23:15:34 +0200338extern retpoline_thunk_t __x86_indirect_thunk_array[];
Peter Zijlstra3b6c1742022-09-15 13:11:28 +0200339extern retpoline_thunk_t __x86_indirect_call_thunk_array[];
340extern retpoline_thunk_t __x86_indirect_jump_thunk_array[];
Peter Zijlstra369ae6f2022-06-14 23:15:34 +0200341
Breno Leitao0911b8c2023-11-21 08:07:37 -0800342#ifdef CONFIG_MITIGATION_RETHUNK
Peter Zijlstra0b53c372022-06-14 23:15:36 +0200343extern void __x86_return_thunk(void);
Peter Zijlstra095b8302023-08-14 13:44:30 +0200344#else
345static inline void __x86_return_thunk(void) {}
346#endif
347
Breno Leitaoac61d432023-11-21 08:07:34 -0800348#ifdef CONFIG_MITIGATION_UNRET_ENTRY
Josh Poimboeuf34a3cae2023-09-04 22:05:00 -0700349extern void retbleed_return_thunk(void);
350#else
351static inline void retbleed_return_thunk(void) {}
352#endif
353
Borislav Petkov (AMD)4535e1a2024-03-28 13:59:05 +0100354extern void srso_alias_untrain_ret(void);
355
Breno Leitaoa033eec2023-11-21 08:07:36 -0800356#ifdef CONFIG_MITIGATION_SRSO
Josh Poimboeuf34a3cae2023-09-04 22:05:00 -0700357extern void srso_return_thunk(void);
358extern void srso_alias_return_thunk(void);
359#else
360static inline void srso_return_thunk(void) {}
361static inline void srso_alias_return_thunk(void) {}
362#endif
363
Peter Zijlstrad025b7b2023-08-14 13:44:32 +0200364extern void retbleed_return_thunk(void);
Peter Zijlstrad43490d2023-08-14 13:44:31 +0200365extern void srso_return_thunk(void);
366extern void srso_alias_return_thunk(void);
367
Peter Zijlstrae7c25c42023-08-14 13:44:34 +0200368extern void entry_untrain_ret(void);
Peter Zijlstra3ebc1702022-06-14 23:16:02 +0200369extern void entry_ibpb(void);
Peter Zijlstra0b53c372022-06-14 23:15:36 +0200370
Peter Zijlstra770ae1b2022-09-15 13:11:25 +0200371extern void (*x86_return_thunk)(void);
Peter Zijlstra770ae1b2022-09-15 13:11:25 +0200372
Josh Poimboeuf4461438a2024-01-03 19:36:26 +0100373extern void __warn_thunk(void);
374
Breno Leitao5fa31af2023-11-21 08:07:30 -0800375#ifdef CONFIG_MITIGATION_CALL_DEPTH_TRACKING
Josh Poimboeuf99ee56c2023-09-04 22:05:04 -0700376extern void call_depth_return_thunk(void);
Peter Zijlstraee3e2462022-09-15 13:11:37 +0200377
378#define CALL_DEPTH_ACCOUNT \
379 ALTERNATIVE("", \
380 __stringify(INCREMENT_CALL_DEPTH), \
381 X86_FEATURE_CALL_DEPTH)
382
Thomas Gleixnerf5c1bb22022-09-15 13:11:30 +0200383#ifdef CONFIG_CALL_THUNKS_DEBUG
384DECLARE_PER_CPU(u64, __x86_call_count);
385DECLARE_PER_CPU(u64, __x86_ret_count);
386DECLARE_PER_CPU(u64, __x86_stuffs_count);
387DECLARE_PER_CPU(u64, __x86_ctxsw_count);
388#endif
Breno Leitao5fa31af2023-11-21 08:07:30 -0800389#else /* !CONFIG_MITIGATION_CALL_DEPTH_TRACKING */
Peter Zijlstraee3e2462022-09-15 13:11:37 +0200390
Josh Poimboeuf99ee56c2023-09-04 22:05:04 -0700391static inline void call_depth_return_thunk(void) {}
Peter Zijlstraee3e2462022-09-15 13:11:37 +0200392#define CALL_DEPTH_ACCOUNT ""
393
Breno Leitao5fa31af2023-11-21 08:07:30 -0800394#endif /* CONFIG_MITIGATION_CALL_DEPTH_TRACKING */
Thomas Gleixner5d821382022-09-15 13:11:27 +0200395
Breno Leitaoaefb2f22023-11-21 08:07:32 -0800396#ifdef CONFIG_MITIGATION_RETPOLINE
Peter Zijlstra1a6f7442021-10-26 14:01:41 +0200397
Peter Zijlstra6fda8a32021-10-26 14:01:40 +0200398#define GEN(reg) \
Peter Zijlstra1a6f7442021-10-26 14:01:41 +0200399 extern retpoline_thunk_t __x86_indirect_thunk_ ## reg;
Peter Zijlstra6fda8a32021-10-26 14:01:40 +0200400#include <asm/GEN-for-each-reg.h>
401#undef GEN
402
Peter Zijlstra3b6c1742022-09-15 13:11:28 +0200403#define GEN(reg) \
404 extern retpoline_thunk_t __x86_indirect_call_thunk_ ## reg;
405#include <asm/GEN-for-each-reg.h>
406#undef GEN
407
408#define GEN(reg) \
409 extern retpoline_thunk_t __x86_indirect_jump_thunk_ ## reg;
410#include <asm/GEN-for-each-reg.h>
411#undef GEN
412
Zhenzhong Duan4cd24de2018-11-02 01:45:41 -0700413#ifdef CONFIG_X86_64
David Woodhouse76b04382018-01-11 21:46:25 +0000414
415/*
Zhenzhong Duan4cd24de2018-11-02 01:45:41 -0700416 * Inline asm uses the %V modifier which is only in newer GCC
Breno Leitaoaefb2f22023-11-21 08:07:32 -0800417 * which is ensured when CONFIG_MITIGATION_RETPOLINE is defined.
David Woodhouse76b04382018-01-11 21:46:25 +0000418 */
419# define CALL_NOSPEC \
Zhenzhong Duan0cbb76d2018-09-18 07:45:00 -0700420 ALTERNATIVE_2( \
Peter Zijlstra9e0e3c52018-01-17 22:34:34 +0100421 ANNOTATE_RETPOLINE_SAFE \
David Woodhouse76b04382018-01-11 21:46:25 +0000422 "call *%[thunk_target]\n", \
Peter Zijlstra119251852021-03-26 16:12:02 +0100423 "call __x86_indirect_thunk_%V[thunk_target]\n", \
Zhenzhong Duan0cbb76d2018-09-18 07:45:00 -0700424 X86_FEATURE_RETPOLINE, \
425 "lfence;\n" \
426 ANNOTATE_RETPOLINE_SAFE \
427 "call *%[thunk_target]\n", \
Peter Zijlstra (Intel)d45476d2022-02-16 20:57:00 +0100428 X86_FEATURE_RETPOLINE_LFENCE)
Peter Zijlstracc1ac9c2020-04-16 14:34:26 +0200429
David Woodhouse76b04382018-01-11 21:46:25 +0000430# define THUNK_TARGET(addr) [thunk_target] "r" (addr)
431
Zhenzhong Duan4cd24de2018-11-02 01:45:41 -0700432#else /* CONFIG_X86_32 */
David Woodhouse76b04382018-01-11 21:46:25 +0000433/*
434 * For i386 we use the original ret-equivalent retpoline, because
435 * otherwise we'll run out of registers. We don't care about CET
436 * here, anyway.
437 */
Andy Whitcrofta14bff12018-03-14 11:24:27 +0000438# define CALL_NOSPEC \
Zhenzhong Duan0cbb76d2018-09-18 07:45:00 -0700439 ALTERNATIVE_2( \
Andy Whitcrofta14bff12018-03-14 11:24:27 +0000440 ANNOTATE_RETPOLINE_SAFE \
441 "call *%[thunk_target]\n", \
David Woodhouse76b04382018-01-11 21:46:25 +0000442 " jmp 904f;\n" \
443 " .align 16\n" \
444 "901: call 903f;\n" \
445 "902: pause;\n" \
Tom Lendacky28d437d2018-01-13 17:27:30 -0600446 " lfence;\n" \
David Woodhouse76b04382018-01-11 21:46:25 +0000447 " jmp 902b;\n" \
448 " .align 16\n" \
Sean Christophersonb63f20a2019-08-22 14:11:22 -0700449 "903: lea 4(%%esp), %%esp;\n" \
David Woodhouse76b04382018-01-11 21:46:25 +0000450 " pushl %[thunk_target];\n" \
451 " ret;\n" \
452 " .align 16\n" \
453 "904: call 901b;\n", \
Zhenzhong Duan0cbb76d2018-09-18 07:45:00 -0700454 X86_FEATURE_RETPOLINE, \
455 "lfence;\n" \
456 ANNOTATE_RETPOLINE_SAFE \
457 "call *%[thunk_target]\n", \
Peter Zijlstra (Intel)d45476d2022-02-16 20:57:00 +0100458 X86_FEATURE_RETPOLINE_LFENCE)
David Woodhouse76b04382018-01-11 21:46:25 +0000459
460# define THUNK_TARGET(addr) [thunk_target] "rm" (addr)
Zhenzhong Duan4cd24de2018-11-02 01:45:41 -0700461#endif
David Woodhouse117cc7a2018-01-12 11:11:27 +0000462#else /* No retpoline for C / inline asm */
David Woodhouse76b04382018-01-11 21:46:25 +0000463# define CALL_NOSPEC "call *%[thunk_target]\n"
464# define THUNK_TARGET(addr) [thunk_target] "rm" (addr)
465#endif
466
David Woodhouseda285122018-01-11 21:46:26 +0000467/* The Spectre V2 mitigation variants */
468enum spectre_v2_mitigation {
469 SPECTRE_V2_NONE,
Peter Zijlstra (Intel)d45476d2022-02-16 20:57:00 +0100470 SPECTRE_V2_RETPOLINE,
471 SPECTRE_V2_LFENCE,
Peter Zijlstra1e19da82022-02-16 20:57:01 +0100472 SPECTRE_V2_EIBRS,
473 SPECTRE_V2_EIBRS_RETPOLINE,
474 SPECTRE_V2_EIBRS_LFENCE,
Pawan Gupta7c693f52022-06-14 23:15:55 +0200475 SPECTRE_V2_IBRS,
David Woodhouseda285122018-01-11 21:46:26 +0000476};
477
Thomas Gleixnerfa1202ef2018-11-25 19:33:45 +0100478/* The indirect branch speculation control variants */
479enum spectre_v2_user_mitigation {
480 SPECTRE_V2_USER_NONE,
481 SPECTRE_V2_USER_STRICT,
Thomas Lendacky20c3a2c2018-12-13 23:03:54 +0000482 SPECTRE_V2_USER_STRICT_PREFERRED,
Thomas Gleixner9137bb22018-11-25 19:33:53 +0100483 SPECTRE_V2_USER_PRCTL,
Thomas Gleixner6b3e64c2018-11-25 19:33:55 +0100484 SPECTRE_V2_USER_SECCOMP,
Thomas Gleixnerfa1202ef2018-11-25 19:33:45 +0100485};
486
Konrad Rzeszutek Wilk24f7fc82018-04-25 22:04:21 -0400487/* The Speculative Store Bypass disable variants */
488enum ssb_mitigation {
489 SPEC_STORE_BYPASS_NONE,
490 SPEC_STORE_BYPASS_DISABLE,
Thomas Gleixnera73ec772018-04-29 15:26:40 +0200491 SPEC_STORE_BYPASS_PRCTL,
Kees Cookf21b53b2018-05-03 14:37:54 -0700492 SPEC_STORE_BYPASS_SECCOMP,
Konrad Rzeszutek Wilk24f7fc82018-04-25 22:04:21 -0400493};
494
Linus Torvalds1aa7a572018-05-01 15:55:51 +0200495static __always_inline
496void alternative_msr_write(unsigned int msr, u64 val, unsigned int feature)
497{
498 asm volatile(ALTERNATIVE("", "wrmsr", %c[feature])
499 : : "c" (msr),
Jim Mattson5f2b7452018-05-13 17:33:57 -0400500 "a" ((u32)val),
501 "d" ((u32)(val >> 32)),
Linus Torvalds1aa7a572018-05-01 15:55:51 +0200502 [feature] "i" (feature)
503 : "memory");
504}
David Woodhousedd844412018-02-19 10:50:54 +0000505
Borislav Petkov (AMD)1b5277c2023-06-29 17:43:40 +0200506extern u64 x86_pred_cmd;
507
David Woodhouse20ffa1c2018-01-25 16:14:15 +0000508static inline void indirect_branch_prediction_barrier(void)
509{
Borislav Petkov (AMD)1b5277c2023-06-29 17:43:40 +0200510 alternative_msr_write(MSR_IA32_PRED_CMD, x86_pred_cmd, X86_FEATURE_USE_IBPB);
David Woodhouse20ffa1c2018-01-25 16:14:15 +0000511}
512
Thomas Gleixnerfa8ac492018-05-12 20:49:16 +0200513/* The Intel SPEC CTRL MSR base value cache */
514extern u64 x86_spec_ctrl_base;
Nathan Chancellordb886972022-07-13 08:24:37 -0700515DECLARE_PER_CPU(u64, x86_spec_ctrl_current);
Pawan Gupta66065152022-11-30 07:25:51 -0800516extern void update_spec_ctrl_cond(u64 val);
Peter Zijlstrabf5835b2022-06-14 23:15:58 +0200517extern u64 spec_ctrl_current(void);
Thomas Gleixnerfa8ac492018-05-12 20:49:16 +0200518
David Woodhousedd844412018-02-19 10:50:54 +0000519/*
520 * With retpoline, we must use IBRS to restrict branch prediction
521 * before calling into firmware.
Ingo Molnard72f4e22018-02-21 09:20:37 +0100522 *
523 * (Implemented as CPP macros due to header hell.)
David Woodhousedd844412018-02-19 10:50:54 +0000524 */
Ingo Molnard72f4e22018-02-21 09:20:37 +0100525#define firmware_restrict_branch_speculation_start() \
526do { \
527 preempt_disable(); \
Josh Poimboeufe6aa1362022-06-14 23:16:06 +0200528 alternative_msr_write(MSR_IA32_SPEC_CTRL, \
529 spec_ctrl_current() | SPEC_CTRL_IBRS, \
Ingo Molnard72f4e22018-02-21 09:20:37 +0100530 X86_FEATURE_USE_IBRS_FW); \
Peter Zijlstra28a99e92022-07-18 13:41:37 +0200531 alternative_msr_write(MSR_IA32_PRED_CMD, PRED_CMD_IBPB, \
532 X86_FEATURE_USE_IBPB_FW); \
Ingo Molnard72f4e22018-02-21 09:20:37 +0100533} while (0)
David Woodhousedd844412018-02-19 10:50:54 +0000534
Ingo Molnard72f4e22018-02-21 09:20:37 +0100535#define firmware_restrict_branch_speculation_end() \
536do { \
Josh Poimboeufe6aa1362022-06-14 23:16:06 +0200537 alternative_msr_write(MSR_IA32_SPEC_CTRL, \
538 spec_ctrl_current(), \
Ingo Molnard72f4e22018-02-21 09:20:37 +0100539 X86_FEATURE_USE_IBRS_FW); \
540 preempt_enable(); \
541} while (0)
David Woodhouse76b04382018-01-11 21:46:25 +0000542
Thomas Gleixnerfa1202ef2018-11-25 19:33:45 +0100543DECLARE_STATIC_KEY_FALSE(switch_to_cond_stibp);
Thomas Gleixner4c71a2b62018-11-25 19:33:49 +0100544DECLARE_STATIC_KEY_FALSE(switch_mm_cond_ibpb);
545DECLARE_STATIC_KEY_FALSE(switch_mm_always_ibpb);
Thomas Gleixnerfa1202ef2018-11-25 19:33:45 +0100546
Thomas Gleixner07f07f52019-02-18 23:04:01 +0100547DECLARE_STATIC_KEY_FALSE(mds_idle_clear);
Thomas Gleixner04dcbdb2019-02-18 23:42:51 +0100548
Balbir Singhb5f06f62021-04-26 21:42:30 +0200549DECLARE_STATIC_KEY_FALSE(switch_mm_cond_l1d_flush);
550
Pawan Gupta8cb861e2022-05-19 20:29:11 -0700551DECLARE_STATIC_KEY_FALSE(mmio_stale_data_clear);
552
Pawan Guptabaf83612024-02-13 18:21:35 -0800553extern u16 mds_verw_sel;
554
Thomas Gleixner6a9e5292019-02-18 23:13:06 +0100555#include <asm/segment.h>
556
557/**
Pawan Gupta1b42f012019-10-23 11:30:45 +0200558 * mds_clear_cpu_buffers - Mitigation for MDS and TAA vulnerability
Thomas Gleixner6a9e5292019-02-18 23:13:06 +0100559 *
560 * This uses the otherwise unused and obsolete VERW instruction in
561 * combination with microcode which triggers a CPU buffer flush when the
562 * instruction is executed.
563 */
Thomas Gleixnera7ef9ba2020-03-04 12:49:18 +0100564static __always_inline void mds_clear_cpu_buffers(void)
Thomas Gleixner6a9e5292019-02-18 23:13:06 +0100565{
566 static const u16 ds = __KERNEL_DS;
567
568 /*
569 * Has to be the memory-operand variant because only that
570 * guarantees the CPU buffer flush functionality according to
571 * documentation. The register-operand variant does not.
572 * Works with any segment selector, but a valid writable
573 * data segment is the fastest variant.
574 *
575 * "cc" clobber is required because VERW modifies ZF.
576 */
577 asm volatile("verw %[ds]" : : [ds] "m" (ds) : "cc");
578}
579
Thomas Gleixner04dcbdb2019-02-18 23:42:51 +0100580/**
Thomas Gleixner07f07f52019-02-18 23:04:01 +0100581 * mds_idle_clear_cpu_buffers - Mitigation for MDS vulnerability
582 *
583 * Clear CPU buffers if the corresponding static key is enabled
584 */
Peter Zijlstra10fdb382023-01-12 20:43:45 +0100585static __always_inline void mds_idle_clear_cpu_buffers(void)
Thomas Gleixner07f07f52019-02-18 23:04:01 +0100586{
587 if (static_branch_likely(&mds_idle_clear))
588 mds_clear_cpu_buffers();
589}
590
David Woodhouse76b04382018-01-11 21:46:25 +0000591#endif /* __ASSEMBLY__ */
Daniel Borkmanna493a872018-02-22 15:12:53 +0100592
Borislav Petkov7a32fc52018-01-26 13:11:37 +0100593#endif /* _ASM_X86_NOSPEC_BRANCH_H_ */