Return-path: Received: from userp2130.oracle.com ([156.151.31.86]) by Galois.linutronix.de with esmtps (TLS1.2:RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1f9Lll-0004A3-AC for speck@linutronix.de; Fri, 20 Apr 2018 04:27:25 +0200 Received: from pps.filterd (userp2130.oracle.com [127.0.0.1]) by userp2130.oracle.com (8.16.0.22/8.16.0.22) with SMTP id w3K2QJMp068509 for ; Fri, 20 Apr 2018 02:27:18 GMT Received: from userv0021.oracle.com (userv0021.oracle.com [156.151.31.71]) by userp2130.oracle.com with ESMTP id 2hdrxp2kax-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Fri, 20 Apr 2018 02:27:18 +0000 Received: from userv0122.oracle.com (userv0122.oracle.com [156.151.31.75]) by userv0021.oracle.com (8.14.4/8.14.4) with ESMTP id w3K2RISk003847 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Fri, 20 Apr 2018 02:27:18 GMT Received: from abhmp0013.oracle.com (abhmp0013.oracle.com [141.146.116.19]) by userv0122.oracle.com (8.14.4/8.14.4) with ESMTP id w3K2RIme026701 for ; Fri, 20 Apr 2018 02:27:18 GMT Message-Id: <20180420022612.989986681@localhost.localdomain> Date: Thu, 19 Apr 2018 22:25:43 -0400 From: konrad.wilk@oracle.com Subject: [MODERATED] [patch 03/11] [PATCH v2 03/10] Linux Patch #3 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 Signed-off-by: Konrad Rzeszutek Wilk --- 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(-) diff --git a/arch/x86/include/asm/nospec-branch.h b/arch/x86/include/asm/nospec-branch.h index f928ad9b143f..f8cc8e0c0c9c 100644 --- a/arch/x86/include/asm/nospec-branch.h +++ b/arch/x86/include/asm/nospec-branch.h @@ -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) diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c index ad613f73d071..3c28473784ad 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 @@ 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; -- 2.14.3