x86/spectre_v2: Read SPEC_CTRL MSR during boot and re-use reserved bits.
Subsystems (2)
PATCH 3/7 x86/spectre_v2: Read SPEC_CTRL MSR during boot and re-use reserved bits.
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.
XXX-Suggested-by Jon Masters <[email protected]>
---
v2: New patch
v2.1: Simplify SPEC_CTRL handling and create guest accessor functions
- Borislav suggested a few changes implemented by Jon
--- arch/x86/include/asm/nospec-branch.h | 31 +++++++++++++++++++++++++++---- arch/x86/kernel/cpu/bugs.c | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 4 deletions(-)
arch/x86/include/asm/nospec-branch.h +27 -4
@@ -217,6 +217,26 @@ enum spectre_v2_mitigation { SPECTRE_V2_IBRS, }; +/* +* The Intel specification for the SPEC_CTRL MSR requires that we +* preserve any already set reserved bits at boot time (e.g. for +* future additions that this kernel is not currently aware of). +* We then set any additional mitigation bits that we want +* ourselves and always use this as the base for SPEC_CTRL. +* We also use this when handling guest entry/exit as below. +*/ +extern u64 x86_spec_ctrl_base; + +/* +* On VMENTER we must preserve whatever view of the SPEC_CTRL MSR +* the guest has, while on VMEXIT we restore the kernel view. This +* would be easier if SPEC_CTRL were architecturally maskable or +* shadowable for guests but this is not (currently) the case. +* Takes the guest view of SPEC_CTRL MSR as a parameter. +*/ +extern void x86_set_guest_spec_ctrl (u64 ); +extern void x86_restore_kernel_spec_ctrl (u64 ); +extern char __indirect_thunk_start[];extern char __indirect_thunk_end[];
@@ -248,12 +268,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 +286,16 @@ static inline void indirect_branch_prediction_barrier(void)*/ #define firmware_restrict_branch_speculation_start () \ do { \ + u64 val = x86_spec_ctrl_base | 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, \ + alternative_msr_write(MSR_IA32_SPEC_CTRL, \ + x86_spec_ctrl_base, \ X86_FEATURE_USE_IBRS_FW); \ preempt_enable(); \ } while (0)
arch/x86/kernel/cpu/bugs.c +33 -0
@@ -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 @@ enum spectre_v2_mitigation_cmd {static enum spectre_v2_mitigation spectre_v2_enabled =SPECTRE_V2_NONE ; +u64 x86_spec_ctrl_base; + +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); +} + +void x86_set_guest_spec_ctrl (u64 guest_spec_ctrl) +{ +/* TODO: Limit guest to only known and available SPEC_CTRL bits */ + +if (x86_spec_ctrl_base == guest_spec_ctrl) +return ; +else +native_wrmsrl (MSR_IA32_SPEC_CTRL , guest_spec_ctrl); +} + +void x86_restore_kernel_spec_ctrl (u64 guest_spec_ctrl) +{ +if (x86_spec_ctrl_base == guest_spec_ctrl) +return ; +else +native_wrmsrl (MSR_IA32_SPEC_CTRL , x86_spec_ctrl_base); +} +#ifdef RETPOLINE static bool spectre_v2_bad_module;
Signature · 1 line
1.8.3.1