Skip to content

Commit 20ffa1c

Browse files
dwmw2KAGA-KOKO
authored andcommitted
x86/speculation: Add basic IBPB (Indirect Branch Prediction Barrier) support
Expose indirect_branch_prediction_barrier() for use in subsequent patches. [ tglx: Add IBPB status to spectre_v2 sysfs file ] Co-developed-by: KarimAllah Ahmed <[email protected]> Signed-off-by: KarimAllah Ahmed <[email protected]> Signed-off-by: David Woodhouse <[email protected]> Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Link: https://lkml.kernel.org/r/[email protected]
1 parent a5b2966 commit 20ffa1c

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

arch/x86/include/asm/cpufeatures.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@
210210
#define X86_FEATURE_MBA ( 7*32+18) /* Memory Bandwidth Allocation */
211211
#define X86_FEATURE_RSB_CTXSW ( 7*32+19) /* Fill RSB on context switches */
212212

213+
#define X86_FEATURE_IBPB ( 7*32+21) /* Indirect Branch Prediction Barrier enabled*/
214+
213215
/* Virtualization flags: Linux defined, word 8 */
214216
#define X86_FEATURE_TPR_SHADOW ( 8*32+ 0) /* Intel TPR Shadow */
215217
#define X86_FEATURE_VNMI ( 8*32+ 1) /* Intel Virtual NMI */

arch/x86/include/asm/nospec-branch.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,5 +218,18 @@ static inline void vmexit_fill_RSB(void)
218218
#endif
219219
}
220220

221+
static inline void indirect_branch_prediction_barrier(void)
222+
{
223+
asm volatile(ALTERNATIVE("",
224+
"movl %[msr], %%ecx\n\t"
225+
"movl %[val], %%eax\n\t"
226+
"movl $0, %%edx\n\t"
227+
"wrmsr",
228+
X86_FEATURE_IBPB)
229+
: : [msr] "i" (MSR_IA32_PRED_CMD),
230+
[val] "i" (PRED_CMD_IBPB)
231+
: "eax", "ecx", "edx", "memory");
232+
}
233+
221234
#endif /* __ASSEMBLY__ */
222235
#endif /* __NOSPEC_BRANCH_H__ */

arch/x86/kernel/cpu/bugs.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,13 @@ static void __init spectre_v2_select_mitigation(void)
263263
setup_force_cpu_cap(X86_FEATURE_RSB_CTXSW);
264264
pr_info("Filling RSB on context switch\n");
265265
}
266+
267+
/* Initialize Indirect Branch Prediction Barrier if supported */
268+
if (boot_cpu_has(X86_FEATURE_SPEC_CTRL) ||
269+
boot_cpu_has(X86_FEATURE_AMD_PRED_CMD)) {
270+
setup_force_cpu_cap(X86_FEATURE_IBPB);
271+
pr_info("Enabling Indirect Branch Prediction Barrier\n");
272+
}
266273
}
267274

268275
#undef pr_fmt
@@ -292,7 +299,8 @@ ssize_t cpu_show_spectre_v2(struct device *dev,
292299
if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2))
293300
return sprintf(buf, "Not affected\n");
294301

295-
return sprintf(buf, "%s%s\n", spectre_v2_strings[spectre_v2_enabled],
302+
return sprintf(buf, "%s%s%s\n", spectre_v2_strings[spectre_v2_enabled],
303+
boot_cpu_has(X86_FEATURE_IBPB) ? ", IPBP" : "",
296304
spectre_v2_bad_module ? " - vulnerable module loaded" : "");
297305
}
298306
#endif

0 commit comments

Comments
 (0)