Return-path: Received: from edison.jonmasters.org ([173.255.233.168]) by Galois.linutronix.de with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1fACx7-00050u-Rj for speck@linutronix.de; Sun, 22 Apr 2018 13:14:42 +0200 Received: from jcm by edison.jonmasters.org with local (Exim 4.71) (envelope-from ) id 1fABiO-0003g4-HN for speck@linutronix.de; Sun, 22 Apr 2018 09:55:24 +0000 Message-Id: <20180422094128.049000558@washington.bos.jonmasters.org> Date: Sun, 22 Apr 2018 05:35:26 -0400 From: jcm@redhat.com Subject: [MODERATED] [patch 3/7] [PATCH v2.1-jcm 3/7] x86/spectre_v2: Read SPEC_CTRL MSR during boot and re-use reserved bits. MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit To: speck@linutronix.de List-ID: 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 --- 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(-) diff --git a/arch/x86/include/asm/nospec-branch.h b/arch/x86/include/asm/nospec-branch.h index f928ad9..45ea869 100644 --- a/arch/x86/include/asm/nospec-branch.h +++ b/arch/x86/include/asm/nospec-branch.h @@ -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) diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c index ad613f7..463a412 100644 --- a/arch/x86/kernel/cpu/bugs.c +++ b/arch/x86/kernel/cpu/bugs.c @@ -27,11 +27,14 @@ #include 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; -- 1.8.3.1