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 
15 #define RETPOLINE_THUNK_SIZE	32
16 
17 /*
18  * Fill the CPU return stack buffer.
19  *
20  * Each entry in the RSB, if used for a speculative 'ret', contains an
21  * infinite 'pause; lfence; jmp' loop to capture speculative execution.
22  *
23  * This is required in various cases for retpoline and IBRS-based
24  * mitigations for the Spectre variant 2 vulnerability. Sometimes to
25  * eliminate potentially bogus entries from the RSB, and sometimes
26  * purely to ensure that it doesn't get empty, which on some CPUs would
27  * allow predictions from other (unwanted!) sources to be used.
28  *
29  * We define a CPP macro such that it can be used from both .S files and
30  * inline assembly. It's possible to do a .macro and then include that
31  * from C via asm(".include <asm/nospec-branch.h>") but let's not go there.
32  */
33 
34 #define RSB_CLEAR_LOOPS		32	/* To forcibly overwrite all entries */
35 
36 /*
37  * Google experimented with loop-unrolling and this turned out to be
38  * the optimal version - two calls, each with their own speculation
39  * trap should their return address end up getting used, in a loop.
40  */
41 #define __FILL_RETURN_BUFFER(reg, nr, sp)	\
42 	mov	$(nr/2), reg;			\
43 771:						\
44 	ANNOTATE_INTRA_FUNCTION_CALL;		\
45 	call	772f;				\
46 773:	/* speculation trap */			\
47 	UNWIND_HINT_EMPTY;			\
48 	pause;					\
49 	lfence;					\
50 	jmp	773b;				\
51 772:						\
52 	ANNOTATE_INTRA_FUNCTION_CALL;		\
53 	call	774f;				\
54 775:	/* speculation trap */			\
55 	UNWIND_HINT_EMPTY;			\
56 	pause;					\
57 	lfence;					\
58 	jmp	775b;				\
59 774:						\
60 	add	$(BITS_PER_LONG/8) * 2, sp;	\
61 	dec	reg;				\
62 	jnz	771b;
63 
64 #ifdef __ASSEMBLY__
65 
66 /*
67  * This should be used immediately before an indirect jump/call. It tells
68  * objtool the subsequent indirect jump/call is vouched safe for retpoline
69  * builds.
70  */
71 .macro ANNOTATE_RETPOLINE_SAFE
72 	.Lannotate_\@:
73 	.pushsection .discard.retpoline_safe
74 	_ASM_PTR .Lannotate_\@
75 	.popsection
76 .endm
77 
78 /*
79  * JMP_NOSPEC and CALL_NOSPEC macros can be used instead of a simple
80  * indirect jmp/call which may be susceptible to the Spectre variant 2
81  * attack.
82  */
83 .macro JMP_NOSPEC reg:req
84 #ifdef CONFIG_RETPOLINE
85 	ALTERNATIVE_2 __stringify(ANNOTATE_RETPOLINE_SAFE; jmp *%\reg), \
86 		      __stringify(jmp __x86_indirect_thunk_\reg), X86_FEATURE_RETPOLINE, \
87 		      __stringify(lfence; ANNOTATE_RETPOLINE_SAFE; jmp *%\reg), X86_FEATURE_RETPOLINE_AMD
88 #else
89 	jmp	*%\reg
90 #endif
91 .endm
92 
93 .macro CALL_NOSPEC reg:req
94 #ifdef CONFIG_RETPOLINE
95 	ALTERNATIVE_2 __stringify(ANNOTATE_RETPOLINE_SAFE; call *%\reg), \
96 		      __stringify(call __x86_indirect_thunk_\reg), X86_FEATURE_RETPOLINE, \
97 		      __stringify(lfence; ANNOTATE_RETPOLINE_SAFE; call *%\reg), X86_FEATURE_RETPOLINE_AMD
98 #else
99 	call	*%\reg
100 #endif
101 .endm
102 
103  /*
104   * A simpler FILL_RETURN_BUFFER macro. Don't make people use the CPP
105   * monstrosity above, manually.
106   */
107 .macro FILL_RETURN_BUFFER reg:req nr:req ftr:req
108 #ifdef CONFIG_RETPOLINE
109 	ALTERNATIVE "jmp .Lskip_rsb_\@", "", \ftr
110 	__FILL_RETURN_BUFFER(\reg,\nr,%_ASM_SP)
111 .Lskip_rsb_\@:
112 #endif
113 .endm
114 
115 #else /* __ASSEMBLY__ */
116 
117 #define ANNOTATE_RETPOLINE_SAFE					\
118 	"999:\n\t"						\
119 	".pushsection .discard.retpoline_safe\n\t"		\
120 	_ASM_PTR " 999b\n\t"					\
121 	".popsection\n\t"
122 
123 #ifdef CONFIG_RETPOLINE
124 
125 typedef u8 retpoline_thunk_t[RETPOLINE_THUNK_SIZE];
126 
127 #define GEN(reg) \
128 	extern retpoline_thunk_t __x86_indirect_thunk_ ## reg;
129 #include <asm/GEN-for-each-reg.h>
130 #undef GEN
131 
132 extern retpoline_thunk_t __x86_indirect_thunk_array[];
133 
134 #ifdef CONFIG_X86_64
135 
136 /*
137  * Inline asm uses the %V modifier which is only in newer GCC
138  * which is ensured when CONFIG_RETPOLINE is defined.
139  */
140 # define CALL_NOSPEC						\
141 	ALTERNATIVE_2(						\
142 	ANNOTATE_RETPOLINE_SAFE					\
143 	"call *%[thunk_target]\n",				\
144 	"call __x86_indirect_thunk_%V[thunk_target]\n",		\
145 	X86_FEATURE_RETPOLINE,					\
146 	"lfence;\n"						\
147 	ANNOTATE_RETPOLINE_SAFE					\
148 	"call *%[thunk_target]\n",				\
149 	X86_FEATURE_RETPOLINE_AMD)
150 
151 # define THUNK_TARGET(addr) [thunk_target] "r" (addr)
152 
153 #else /* CONFIG_X86_32 */
154 /*
155  * For i386 we use the original ret-equivalent retpoline, because
156  * otherwise we'll run out of registers. We don't care about CET
157  * here, anyway.
158  */
159 # define CALL_NOSPEC						\
160 	ALTERNATIVE_2(						\
161 	ANNOTATE_RETPOLINE_SAFE					\
162 	"call *%[thunk_target]\n",				\
163 	"       jmp    904f;\n"					\
164 	"       .align 16\n"					\
165 	"901:	call   903f;\n"					\
166 	"902:	pause;\n"					\
167 	"    	lfence;\n"					\
168 	"       jmp    902b;\n"					\
169 	"       .align 16\n"					\
170 	"903:	lea    4(%%esp), %%esp;\n"			\
171 	"       pushl  %[thunk_target];\n"			\
172 	"       ret;\n"						\
173 	"       .align 16\n"					\
174 	"904:	call   901b;\n",				\
175 	X86_FEATURE_RETPOLINE,					\
176 	"lfence;\n"						\
177 	ANNOTATE_RETPOLINE_SAFE					\
178 	"call *%[thunk_target]\n",				\
179 	X86_FEATURE_RETPOLINE_AMD)
180 
181 # define THUNK_TARGET(addr) [thunk_target] "rm" (addr)
182 #endif
183 #else /* No retpoline for C / inline asm */
184 # define CALL_NOSPEC "call *%[thunk_target]\n"
185 # define THUNK_TARGET(addr) [thunk_target] "rm" (addr)
186 #endif
187 
188 /* The Spectre V2 mitigation variants */
189 enum spectre_v2_mitigation {
190 	SPECTRE_V2_NONE,
191 	SPECTRE_V2_RETPOLINE_GENERIC,
192 	SPECTRE_V2_RETPOLINE_AMD,
193 	SPECTRE_V2_IBRS_ENHANCED,
194 };
195 
196 /* The indirect branch speculation control variants */
197 enum spectre_v2_user_mitigation {
198 	SPECTRE_V2_USER_NONE,
199 	SPECTRE_V2_USER_STRICT,
200 	SPECTRE_V2_USER_STRICT_PREFERRED,
201 	SPECTRE_V2_USER_PRCTL,
202 	SPECTRE_V2_USER_SECCOMP,
203 };
204 
205 /* The Speculative Store Bypass disable variants */
206 enum ssb_mitigation {
207 	SPEC_STORE_BYPASS_NONE,
208 	SPEC_STORE_BYPASS_DISABLE,
209 	SPEC_STORE_BYPASS_PRCTL,
210 	SPEC_STORE_BYPASS_SECCOMP,
211 };
212 
213 extern char __indirect_thunk_start[];
214 extern char __indirect_thunk_end[];
215 
216 static __always_inline
217 void alternative_msr_write(unsigned int msr, u64 val, unsigned int feature)
218 {
219 	asm volatile(ALTERNATIVE("", "wrmsr", %c[feature])
220 		: : "c" (msr),
221 		    "a" ((u32)val),
222 		    "d" ((u32)(val >> 32)),
223 		    [feature] "i" (feature)
224 		: "memory");
225 }
226 
227 static inline void indirect_branch_prediction_barrier(void)
228 {
229 	u64 val = PRED_CMD_IBPB;
230 
231 	alternative_msr_write(MSR_IA32_PRED_CMD, val, X86_FEATURE_USE_IBPB);
232 }
233 
234 /* The Intel SPEC CTRL MSR base value cache */
235 extern u64 x86_spec_ctrl_base;
236 
237 /*
238  * With retpoline, we must use IBRS to restrict branch prediction
239  * before calling into firmware.
240  *
241  * (Implemented as CPP macros due to header hell.)
242  */
243 #define firmware_restrict_branch_speculation_start()			\
244 do {									\
245 	u64 val = x86_spec_ctrl_base | SPEC_CTRL_IBRS;			\
246 									\
247 	preempt_disable();						\
248 	alternative_msr_write(MSR_IA32_SPEC_CTRL, val,			\
249 			      X86_FEATURE_USE_IBRS_FW);			\
250 } while (0)
251 
252 #define firmware_restrict_branch_speculation_end()			\
253 do {									\
254 	u64 val = x86_spec_ctrl_base;					\
255 									\
256 	alternative_msr_write(MSR_IA32_SPEC_CTRL, val,			\
257 			      X86_FEATURE_USE_IBRS_FW);			\
258 	preempt_enable();						\
259 } while (0)
260 
261 DECLARE_STATIC_KEY_FALSE(switch_to_cond_stibp);
262 DECLARE_STATIC_KEY_FALSE(switch_mm_cond_ibpb);
263 DECLARE_STATIC_KEY_FALSE(switch_mm_always_ibpb);
264 
265 DECLARE_STATIC_KEY_FALSE(mds_user_clear);
266 DECLARE_STATIC_KEY_FALSE(mds_idle_clear);
267 
268 DECLARE_STATIC_KEY_FALSE(switch_mm_cond_l1d_flush);
269 
270 #include <asm/segment.h>
271 
272 /**
273  * mds_clear_cpu_buffers - Mitigation for MDS and TAA vulnerability
274  *
275  * This uses the otherwise unused and obsolete VERW instruction in
276  * combination with microcode which triggers a CPU buffer flush when the
277  * instruction is executed.
278  */
279 static __always_inline void mds_clear_cpu_buffers(void)
280 {
281 	static const u16 ds = __KERNEL_DS;
282 
283 	/*
284 	 * Has to be the memory-operand variant because only that
285 	 * guarantees the CPU buffer flush functionality according to
286 	 * documentation. The register-operand variant does not.
287 	 * Works with any segment selector, but a valid writable
288 	 * data segment is the fastest variant.
289 	 *
290 	 * "cc" clobber is required because VERW modifies ZF.
291 	 */
292 	asm volatile("verw %[ds]" : : [ds] "m" (ds) : "cc");
293 }
294 
295 /**
296  * mds_user_clear_cpu_buffers - Mitigation for MDS and TAA vulnerability
297  *
298  * Clear CPU buffers if the corresponding static key is enabled
299  */
300 static __always_inline void mds_user_clear_cpu_buffers(void)
301 {
302 	if (static_branch_likely(&mds_user_clear))
303 		mds_clear_cpu_buffers();
304 }
305 
306 /**
307  * mds_idle_clear_cpu_buffers - Mitigation for MDS vulnerability
308  *
309  * Clear CPU buffers if the corresponding static key is enabled
310  */
311 static inline void mds_idle_clear_cpu_buffers(void)
312 {
313 	if (static_branch_likely(&mds_idle_clear))
314 		mds_clear_cpu_buffers();
315 }
316 
317 #endif /* __ASSEMBLY__ */
318 
319 #endif /* _ASM_X86_NOSPEC_BRANCH_H_ */
320