nospec-branch.h 10.4 KB
Newer Older
1 2
/* SPDX-License-Identifier: GPL-2.0 */

3 4
#ifndef _ASM_X86_NOSPEC_BRANCH_H_
#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 11
#include <asm/alternative.h>
#include <asm/cpufeatures.h>
12
#include <asm/msr-index.h>
13
#include <asm/unwind_hints.h>
14
#include <asm/percpu.h>
15

16 17
#define RETPOLINE_THUNK_SIZE	32

18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
/*
 * Fill the CPU return stack buffer.
 *
 * Each entry in the RSB, if used for a speculative 'ret', contains an
 * infinite 'pause; lfence; jmp' loop to capture speculative execution.
 *
 * This is required in various cases for retpoline and IBRS-based
 * mitigations for the Spectre variant 2 vulnerability. Sometimes to
 * eliminate potentially bogus entries from the RSB, and sometimes
 * purely to ensure that it doesn't get empty, which on some CPUs would
 * allow predictions from other (unwanted!) sources to be used.
 *
 * We define a CPP macro such that it can be used from both .S files and
 * inline assembly. It's possible to do a .macro and then include that
 * from C via asm(".include <asm/nospec-branch.h>") but let's not go there.
 */

#define RSB_CLEAR_LOOPS		32	/* To forcibly overwrite all entries */

/*
 * Google experimented with loop-unrolling and this turned out to be
39
 * the optimal version - two calls, each with their own speculation
40 41 42 43 44
 * trap should their return address end up getting used, in a loop.
 */
#define __FILL_RETURN_BUFFER(reg, nr, sp)	\
	mov	$(nr/2), reg;			\
771:						\
45
	ANNOTATE_INTRA_FUNCTION_CALL;		\
46 47
	call	772f;				\
773:	/* speculation trap */			\
48
	UNWIND_HINT_EMPTY;			\
49 50 51 52
	pause;					\
	lfence;					\
	jmp	773b;				\
772:						\
53
	ANNOTATE_INTRA_FUNCTION_CALL;		\
54 55
	call	774f;				\
775:	/* speculation trap */			\
56
	UNWIND_HINT_EMPTY;			\
57 58 59 60
	pause;					\
	lfence;					\
	jmp	775b;				\
774:						\
61
	add	$(BITS_PER_LONG/8) * 2, sp;	\
62
	dec	reg;				\
63
	jnz	771b;
64

65 66
#ifdef __ASSEMBLY__

67 68 69 70 71 72 73 74 75 76 77 78
/*
 * This should be used immediately before an indirect jump/call. It tells
 * objtool the subsequent indirect jump/call is vouched safe for retpoline
 * builds.
 */
.macro ANNOTATE_RETPOLINE_SAFE
	.Lannotate_\@:
	.pushsection .discard.retpoline_safe
	_ASM_PTR .Lannotate_\@
	.popsection
.endm

79 80 81 82 83 84
/*
 * (ab)use RETPOLINE_SAFE on RET to annotate away 'bare' RET instructions
 * vs RETBleed validation.
 */
#define ANNOTATE_UNRET_SAFE ANNOTATE_RETPOLINE_SAFE

85 86 87 88 89 90 91 92 93 94 95
/*
 * Abuse ANNOTATE_RETPOLINE_SAFE on a NOP to indicate UNRET_END, should
 * eventually turn into it's own annotation.
 */
.macro ANNOTATE_UNRET_END
#ifdef CONFIG_DEBUG_ENTRY
	ANNOTATE_RETPOLINE_SAFE
	nop
#endif
.endm

96 97 98 99 100 101 102
/*
 * JMP_NOSPEC and CALL_NOSPEC macros can be used instead of a simple
 * indirect jmp/call which may be susceptible to the Spectre variant 2
 * attack.
 */
.macro JMP_NOSPEC reg:req
#ifdef CONFIG_RETPOLINE
103
	ALTERNATIVE_2 __stringify(ANNOTATE_RETPOLINE_SAFE; jmp *%\reg), \
104
		      __stringify(jmp __x86_indirect_thunk_\reg), X86_FEATURE_RETPOLINE, \
105
		      __stringify(lfence; ANNOTATE_RETPOLINE_SAFE; jmp *%\reg), X86_FEATURE_RETPOLINE_LFENCE
106
#else
107
	jmp	*%\reg
108 109 110 111 112
#endif
.endm

.macro CALL_NOSPEC reg:req
#ifdef CONFIG_RETPOLINE
113
	ALTERNATIVE_2 __stringify(ANNOTATE_RETPOLINE_SAFE; call *%\reg), \
114
		      __stringify(call __x86_indirect_thunk_\reg), X86_FEATURE_RETPOLINE, \
115
		      __stringify(lfence; ANNOTATE_RETPOLINE_SAFE; call *%\reg), X86_FEATURE_RETPOLINE_LFENCE
116
#else
117
	call	*%\reg
118
#endif
119 120
.endm

121 122 123 124 125 126 127 128 129
.macro ISSUE_UNBALANCED_RET_GUARD
	ANNOTATE_INTRA_FUNCTION_CALL
	call .Lunbalanced_ret_guard_\@
	int3
.Lunbalanced_ret_guard_\@:
	add $(BITS_PER_LONG/8), %_ASM_SP
	lfence
.endm

130 131 132 133
 /*
  * A simpler FILL_RETURN_BUFFER macro. Don't make people use the CPP
  * monstrosity above, manually.
  */
134 135
.macro FILL_RETURN_BUFFER reg:req nr:req ftr:req ftr2
.ifb \ftr2
136
	ALTERNATIVE "jmp .Lskip_rsb_\@", "", \ftr
137 138 139
.else
	ALTERNATIVE_2 "jmp .Lskip_rsb_\@", "", \ftr, "jmp .Lunbalanced_\@", \ftr2
.endif
140
	__FILL_RETURN_BUFFER(\reg,\nr,%_ASM_SP)
141 142
.Lunbalanced_\@:
	ISSUE_UNBALANCED_RET_GUARD
143
.Lskip_rsb_\@:
144 145
.endm

146 147 148 149 150 151
#ifdef CONFIG_CPU_UNRET_ENTRY
#define CALL_ZEN_UNTRAIN_RET	"call zen_untrain_ret"
#else
#define CALL_ZEN_UNTRAIN_RET	""
#endif

152 153 154 155 156
/*
 * Mitigate RETBleed for AMD/Hygon Zen uarch. Requires KERNEL CR3 because the
 * return thunk isn't mapped into the userspace tables (then again, AMD
 * typically has NO_MELTDOWN).
 *
157 158
 * While zen_untrain_ret() doesn't clobber anything but requires stack,
 * entry_ibpb() will clobber AX, CX, DX.
159 160 161 162 163
 *
 * As such, this must be placed after every *SWITCH_TO_KERNEL_CR3 at a point
 * where we have a stack but before any RET instruction.
 */
.macro UNTRAIN_RET
164
#if defined(CONFIG_CPU_UNRET_ENTRY) || defined(CONFIG_CPU_IBPB_ENTRY)
165
	ANNOTATE_UNRET_END
166
	ALTERNATIVE_2 "",						\
167
	              CALL_ZEN_UNTRAIN_RET, X86_FEATURE_UNRET,		\
168
		      "call entry_ibpb", X86_FEATURE_ENTRY_IBPB
169 170 171
#endif
.endm

172 173
#else /* __ASSEMBLY__ */

174 175 176 177 178 179
#define ANNOTATE_RETPOLINE_SAFE					\
	"999:\n\t"						\
	".pushsection .discard.retpoline_safe\n\t"		\
	_ASM_PTR " 999b\n\t"					\
	".popsection\n\t"

180
typedef u8 retpoline_thunk_t[RETPOLINE_THUNK_SIZE];
181 182
extern retpoline_thunk_t __x86_indirect_thunk_array[];

183
extern void __x86_return_thunk(void);
184
extern void zen_untrain_ret(void);
185
extern void entry_ibpb(void);
186

187
#ifdef CONFIG_RETPOLINE
188

189
#define GEN(reg) \
190
	extern retpoline_thunk_t __x86_indirect_thunk_ ## reg;
191 192 193
#include <asm/GEN-for-each-reg.h>
#undef GEN

194
#ifdef CONFIG_X86_64
195 196

/*
197 198
 * Inline asm uses the %V modifier which is only in newer GCC
 * which is ensured when CONFIG_RETPOLINE is defined.
199 200
 */
# define CALL_NOSPEC						\
201
	ALTERNATIVE_2(						\
202
	ANNOTATE_RETPOLINE_SAFE					\
203
	"call *%[thunk_target]\n",				\
204
	"call __x86_indirect_thunk_%V[thunk_target]\n",		\
205 206 207 208
	X86_FEATURE_RETPOLINE,					\
	"lfence;\n"						\
	ANNOTATE_RETPOLINE_SAFE					\
	"call *%[thunk_target]\n",				\
209
	X86_FEATURE_RETPOLINE_LFENCE)
210

211 212
# define THUNK_TARGET(addr) [thunk_target] "r" (addr)

213
#else /* CONFIG_X86_32 */
214 215 216 217 218
/*
 * For i386 we use the original ret-equivalent retpoline, because
 * otherwise we'll run out of registers. We don't care about CET
 * here, anyway.
 */
219
# define CALL_NOSPEC						\
220
	ALTERNATIVE_2(						\
221 222
	ANNOTATE_RETPOLINE_SAFE					\
	"call *%[thunk_target]\n",				\
223 224 225 226
	"       jmp    904f;\n"					\
	"       .align 16\n"					\
	"901:	call   903f;\n"					\
	"902:	pause;\n"					\
227
	"    	lfence;\n"					\
228 229
	"       jmp    902b;\n"					\
	"       .align 16\n"					\
230
	"903:	lea    4(%%esp), %%esp;\n"			\
231 232 233 234
	"       pushl  %[thunk_target];\n"			\
	"       ret;\n"						\
	"       .align 16\n"					\
	"904:	call   901b;\n",				\
235 236 237 238
	X86_FEATURE_RETPOLINE,					\
	"lfence;\n"						\
	ANNOTATE_RETPOLINE_SAFE					\
	"call *%[thunk_target]\n",				\
239
	X86_FEATURE_RETPOLINE_LFENCE)
240 241

# define THUNK_TARGET(addr) [thunk_target] "rm" (addr)
242
#endif
243
#else /* No retpoline for C / inline asm */
244 245 246 247
# define CALL_NOSPEC "call *%[thunk_target]\n"
# define THUNK_TARGET(addr) [thunk_target] "rm" (addr)
#endif

248 249 250
/* The Spectre V2 mitigation variants */
enum spectre_v2_mitigation {
	SPECTRE_V2_NONE,
251 252
	SPECTRE_V2_RETPOLINE,
	SPECTRE_V2_LFENCE,
253 254 255
	SPECTRE_V2_EIBRS,
	SPECTRE_V2_EIBRS_RETPOLINE,
	SPECTRE_V2_EIBRS_LFENCE,
256
	SPECTRE_V2_IBRS,
257 258
};

259 260 261 262
/* The indirect branch speculation control variants */
enum spectre_v2_user_mitigation {
	SPECTRE_V2_USER_NONE,
	SPECTRE_V2_USER_STRICT,
263
	SPECTRE_V2_USER_STRICT_PREFERRED,
264
	SPECTRE_V2_USER_PRCTL,
265
	SPECTRE_V2_USER_SECCOMP,
266 267
};

268 269 270 271
/* The Speculative Store Bypass disable variants */
enum ssb_mitigation {
	SPEC_STORE_BYPASS_NONE,
	SPEC_STORE_BYPASS_DISABLE,
272
	SPEC_STORE_BYPASS_PRCTL,
273
	SPEC_STORE_BYPASS_SECCOMP,
274 275
};

276 277 278
extern char __indirect_thunk_start[];
extern char __indirect_thunk_end[];

279 280 281 282 283
static __always_inline
void alternative_msr_write(unsigned int msr, u64 val, unsigned int feature)
{
	asm volatile(ALTERNATIVE("", "wrmsr", %c[feature])
		: : "c" (msr),
284 285
		    "a" ((u32)val),
		    "d" ((u32)(val >> 32)),
286 287 288
		    [feature] "i" (feature)
		: "memory");
}
289

290 291
static inline void indirect_branch_prediction_barrier(void)
{
292 293 294
	u64 val = PRED_CMD_IBPB;

	alternative_msr_write(MSR_IA32_PRED_CMD, val, X86_FEATURE_USE_IBPB);
295 296
}

297 298
/* The Intel SPEC CTRL MSR base value cache */
extern u64 x86_spec_ctrl_base;
299
DECLARE_PER_CPU(u64, x86_spec_ctrl_current);
300
extern void write_spec_ctrl_current(u64 val, bool force);
301
extern u64 spec_ctrl_current(void);
302

303 304 305
/*
 * With retpoline, we must use IBRS to restrict branch prediction
 * before calling into firmware.
306 307
 *
 * (Implemented as CPP macros due to header hell.)
308
 */
309 310 311
#define firmware_restrict_branch_speculation_start()			\
do {									\
	preempt_disable();						\
312 313
	alternative_msr_write(MSR_IA32_SPEC_CTRL,			\
			      spec_ctrl_current() | SPEC_CTRL_IBRS,	\
314
			      X86_FEATURE_USE_IBRS_FW);			\
315 316
	alternative_msr_write(MSR_IA32_PRED_CMD, PRED_CMD_IBPB,		\
			      X86_FEATURE_USE_IBPB_FW);			\
317
} while (0)
318

319 320
#define firmware_restrict_branch_speculation_end()			\
do {									\
321 322
	alternative_msr_write(MSR_IA32_SPEC_CTRL,			\
			      spec_ctrl_current(),			\
323 324 325
			      X86_FEATURE_USE_IBRS_FW);			\
	preempt_enable();						\
} while (0)
326

327
DECLARE_STATIC_KEY_FALSE(switch_to_cond_stibp);
328 329
DECLARE_STATIC_KEY_FALSE(switch_mm_cond_ibpb);
DECLARE_STATIC_KEY_FALSE(switch_mm_always_ibpb);
330

331
DECLARE_STATIC_KEY_FALSE(mds_user_clear);
332
DECLARE_STATIC_KEY_FALSE(mds_idle_clear);
333

334 335
DECLARE_STATIC_KEY_FALSE(switch_mm_cond_l1d_flush);

336 337
DECLARE_STATIC_KEY_FALSE(mmio_stale_data_clear);

338 339 340
#include <asm/segment.h>

/**
341
 * mds_clear_cpu_buffers - Mitigation for MDS and TAA vulnerability
342 343 344 345 346
 *
 * This uses the otherwise unused and obsolete VERW instruction in
 * combination with microcode which triggers a CPU buffer flush when the
 * instruction is executed.
 */
347
static __always_inline void mds_clear_cpu_buffers(void)
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
{
	static const u16 ds = __KERNEL_DS;

	/*
	 * Has to be the memory-operand variant because only that
	 * guarantees the CPU buffer flush functionality according to
	 * documentation. The register-operand variant does not.
	 * Works with any segment selector, but a valid writable
	 * data segment is the fastest variant.
	 *
	 * "cc" clobber is required because VERW modifies ZF.
	 */
	asm volatile("verw %[ds]" : : [ds] "m" (ds) : "cc");
}

363
/**
364
 * mds_user_clear_cpu_buffers - Mitigation for MDS and TAA vulnerability
365 366 367
 *
 * Clear CPU buffers if the corresponding static key is enabled
 */
368
static __always_inline void mds_user_clear_cpu_buffers(void)
369 370 371 372 373
{
	if (static_branch_likely(&mds_user_clear))
		mds_clear_cpu_buffers();
}

374 375 376 377 378 379 380 381 382 383 384
/**
 * mds_idle_clear_cpu_buffers - Mitigation for MDS vulnerability
 *
 * Clear CPU buffers if the corresponding static key is enabled
 */
static inline void mds_idle_clear_cpu_buffers(void)
{
	if (static_branch_likely(&mds_idle_clear))
		mds_clear_cpu_buffers();
}

385
#endif /* __ASSEMBLY__ */
386

387
#endif /* _ASM_X86_NOSPEC_BRANCH_H_ */