Skip to contentSkip to conversation

Linux Patch #3

PATCH Superseded [email protected] 1 participant 1 message Apr 20, 2018

Subsystems (2)

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 3/10 Linux Patch #3

The 336996-Speculative-Execution-Side-Channel-Mitigations.pdf refers to all the other bits as reserved. The Intel SDM glossary defines reserved as implementation specific - aka unknown.

As such at bootup we should take it in-to account and apply proper masking for the bits we use.

---
v2: New patch

---
 arch/x86/include/asm/nospec-branch.h | 18 ++++++++++++++----
 arch/x86/kernel/cpu/bugs.c           | 33 +++++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+), 4 deletions(-)
arch/x86/include/asm/nospec-branch.h +14 -4
index f928ad9b143f..f8cc8e0c0c9c 100644
@@ -217,6 +217,13 @@ enum spectre_v2_mitigation {
 	SPECTRE_V2_IBRS,
 };
 
+/*
+ * Our accessory functions for safe SPEC_CTRL MSR values.
+ */
+extern u64 set_spec_ctrl(u64 val);
+extern u64 clear_spec_ctrl(u64 val);
+extern bool need_spec_ctrl_acc(void);
+
 extern char __indirect_thunk_start[];
 extern char __indirect_thunk_end[];
 
@@ -248,12 +255,13 @@ static inline void vmexit_fill_RSB(void)
 				 "movl $0, %%edx\n\t"		\
 				 "wrmsr",			\
 				 _feature)			\
-		     : : [msr] "i" (_msr), [val] "i" (_val)	\
+		     : : [msr] "i" (_msr), [val] "m" (_val)	\
 		     : "eax", "ecx", "edx", "memory")
 
 static inline void indirect_branch_prediction_barrier(void)
 {
-	alternative_msr_write(MSR_IA32_PRED_CMD, PRED_CMD_IBPB,
+	u64 val = PRED_CMD_IBPB;
+	alternative_msr_write(MSR_IA32_PRED_CMD, val,
 			      X86_FEATURE_USE_IBPB);
 }
 
@@ -265,14 +273,16 @@ static inline void indirect_branch_prediction_barrier(void)
  */
 #define firmware_restrict_branch_speculation_start()			\
 do {									\
+	u64 val = set_spec_ctrl(SPEC_CTRL_IBRS);			\
 	preempt_disable();						\
-	alternative_msr_write(MSR_IA32_SPEC_CTRL, SPEC_CTRL_IBRS,	\
+	alternative_msr_write(MSR_IA32_SPEC_CTRL, val,			\
 			      X86_FEATURE_USE_IBRS_FW);			\
 } while (0)
 
 #define firmware_restrict_branch_speculation_end()			\
 do {									\
-	alternative_msr_write(MSR_IA32_SPEC_CTRL, 0,			\
+	u64 val = clear_spec_ctrl(SPEC_CTRL_IBRS);			\
+	alternative_msr_write(MSR_IA32_SPEC_CTRL, val,			\
 			      X86_FEATURE_USE_IBRS_FW);			\
 	preempt_enable();						\
 } while (0)
arch/x86/kernel/cpu/bugs.c +33 -0
index ad613f73d071..3c28473784ad 100644
@@ -27,11 +27,14 @@
 #include <asm/intel-family.h>
 
 static void __init spectre_v2_select_mitigation(void);
+static void __init spec_ctrl_save_msr(void);
 
 void __init check_bugs(void)
 {
 	identify_boot_cpu();
 
+	spec_ctrl_save_msr();
+
 	if (!IS_ENABLED(CONFIG_SMP)) {
 		pr_info("CPU: ");
 		print_cpu_info(&boot_cpu_data);
@@ -95,6 +98,36 @@ static const char *spectre_v2_strings[] = {
 
 static enum spectre_v2_mitigation spectre_v2_enabled = SPECTRE_V2_NONE;
 
+static u64 x86_spec_ctrl_base;
+
+u64 set_spec_ctrl(u64 val)
+{
+	return x86_spec_ctrl_base | val;
+}
+EXPORT_SYMBOL_GPL(set_spec_ctrl);
+
+u64 clear_spec_ctrl(u64 val)
+{
+	return x86_spec_ctrl_base & ~val;
+}
+EXPORT_SYMBOL_GPL(clear_spec_ctrl);
+
+bool need_spec_ctrl_acc(void)
+{
+	return !!x86_spec_ctrl_base;
+}
+EXPORT_SYMBOL_GPL(need_spec_ctrl_acc);
+
+static void __init spec_ctrl_save_msr(void)
+{
+	/*
+	 * Read the SPEC_CTRL MSR to account for reserved bits which may have
+	 * unknown values.
+	 */
+	if (boot_cpu_has(X86_FEATURE_IBPB) || boot_cpu_has(X86_FEATURE_IBRS))
+		rdmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
+}
+
 #ifdef RETPOLINE
 static bool spectre_v2_bad_module;
 
Signature · 1 line
2.14.3