Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp787390imm; Mon, 21 May 2018 14:27:15 -0700 (PDT) X-Google-Smtp-Source: AB8JxZqkyyeiC8U54g6pCLBO3fvkedwhGZsos1FrHY7RDCMyEU7CC3OgfCrT62YVtJL7YEGqVu57 X-Received: by 2002:a17:902:a711:: with SMTP id w17-v6mr22042979plq.292.1526938035532; Mon, 21 May 2018 14:27:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526938035; cv=none; d=google.com; s=arc-20160816; b=WoaezUONjLE4cUUCtSR/aZpAGZqVIS1c2ISi/y4f3GYX6Gd8ekRmiptS+yh7iI5Qtn XRpna/U3qqwkn8iiExAL1EkKa5VTCPvjDrJ4AAIgK0ISt+8C4YgU8vfyA5NjVMOg72ky wlX7sqC28nXydCZOgAoo0+dXEDIpan12XafRdttvIwTYkQ0TJwAdYcKajowsbVE++P2m TwiE5TCi05eptABdDlcn/C8FkeIMjQwWn17XFFcmS9jilTA7u3q3LlJGTIFLkZA3M5fc eeMxryiVmUecIHtrbz4iT7yEuZM2I3hS5auaTmdpLuuQGvILfYkjnlGebdsSGRWiD0t+ LlrA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=Fi5lUB0l+MDX86nEJjpbvLtYLJWAojQWJN9lreA5pAs=; b=XlTd0lCgBMN0JZnUsshugc42QQp5cuXP1oG0CwM+aYv0t+2x/+LVcngJL7DfU1MNvc oTOOitJti9h4qDxmX6M4QoMpB8uFbgqBTaOnKM5htNDVOm9bBRfGaKTK7WWJ+qScIVVn FXdJnmUDTuXJQVNKN7WJWtRtwyX7zXUke70XCZn6+zmXj6JfA6dT95m0sgMpkYWLRlGb eWFagZJJGAhattrhLRJGM3M61sJjirVjQ2b+jWHvt9ryf9BTbh5hLI5qHymDD3MAInvd mzdOSrsXcCVyqeJyjD6Bqow/fvuZLwgs5XRyRlA0dx0CnzNy+2Atu6k3EKJMYzYPYIal emOQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=0lf5w6kP; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id s6-v6si12061769pgr.369.2018.05.21.14.27.00; Mon, 21 May 2018 14:27:15 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=0lf5w6kP; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932494AbeEUVZy (ORCPT + 99 others); Mon, 21 May 2018 17:25:54 -0400 Received: from mail.kernel.org ([198.145.29.99]:40554 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754407AbeEUVZn (ORCPT ); Mon, 21 May 2018 17:25:43 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 2F9F320853; Mon, 21 May 2018 21:25:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1526937942; bh=XGRNPMGujTFMxlrO3xlUqjAVcn9IE0Fnnib1kxX8NTQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=0lf5w6kPqoKLvrB7CoGpZrZxN+f/lH0Zjl7WoXDfFJc4OyYZaM4aKgcXl8Fj6k4iv qapS6EQRX8brFdY7OaBISSR1rHpmGQN6xahH3C48vr6BzrcEwkPd0hgwuJeQTdH69/ EbfUfFVf50HyH5fUHUVnqTmn3BBBSGTeWKiGryYo= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jon Masters , Konrad Rzeszutek Wilk , Thomas Gleixner , Borislav Petkov , Ingo Molnar Subject: [PATCH 4.16 065/110] x86/bugs: Read SPEC_CTRL MSR during boot and re-use reserved bits Date: Mon, 21 May 2018 23:12:02 +0200 Message-Id: <20180521210511.969678559@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180521210503.823249477@linuxfoundation.org> References: <20180521210503.823249477@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.16-stable review patch. If anyone has any objections, please let me know. ------------------ From: Konrad Rzeszutek Wilk commit 1b86883ccb8d5d9506529d42dbe1a5257cb30b18 upstream 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 this must be taken it into account and proper masking for the bits in use applied. A copy of this document is available at https://bugzilla.kernel.org/show_bug.cgi?id=199511 [ tglx: Made x86_spec_ctrl_base __ro_after_init ] Suggested-by: Jon Masters Signed-off-by: Konrad Rzeszutek Wilk Signed-off-by: Thomas Gleixner Reviewed-by: Borislav Petkov Reviewed-by: Ingo Molnar Signed-off-by: Greg Kroah-Hartman --- arch/x86/include/asm/nospec-branch.h | 24 ++++++++++++++++++++---- arch/x86/kernel/cpu/bugs.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 4 deletions(-) --- a/arch/x86/include/asm/nospec-branch.h +++ b/arch/x86/include/asm/nospec-branch.h @@ -217,6 +217,17 @@ 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 void x86_spec_ctrl_set(u64); +extern u64 x86_spec_ctrl_get_default(void); + extern char __indirect_thunk_start[]; extern char __indirect_thunk_end[]; @@ -254,8 +265,9 @@ void alternative_msr_write(unsigned int static inline void indirect_branch_prediction_barrier(void) { - alternative_msr_write(MSR_IA32_PRED_CMD, PRED_CMD_IBPB, - X86_FEATURE_USE_IBPB); + u64 val = PRED_CMD_IBPB; + + alternative_msr_write(MSR_IA32_PRED_CMD, val, X86_FEATURE_USE_IBPB); } /* @@ -266,14 +278,18 @@ static inline void indirect_branch_predi */ #define firmware_restrict_branch_speculation_start() \ do { \ + u64 val = x86_spec_ctrl_get_default() | 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 = x86_spec_ctrl_get_default(); \ + \ + alternative_msr_write(MSR_IA32_SPEC_CTRL, val, \ X86_FEATURE_USE_IBRS_FW); \ preempt_enable(); \ } while (0) --- a/arch/x86/kernel/cpu/bugs.c +++ b/arch/x86/kernel/cpu/bugs.c @@ -28,6 +28,12 @@ static void __init spectre_v2_select_mitigation(void); +/* + * Our boot-time value of the SPEC_CTRL MSR. We read it once so that any + * writes to SPEC_CTRL contain whatever reserved bits have been set. + */ +static u64 __ro_after_init x86_spec_ctrl_base; + void __init check_bugs(void) { identify_boot_cpu(); @@ -37,6 +43,13 @@ void __init check_bugs(void) print_cpu_info(&boot_cpu_data); } + /* + * Read the SPEC_CTRL MSR to account for reserved bits which may + * have unknown values. + */ + if (boot_cpu_has(X86_FEATURE_IBRS)) + rdmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base); + /* Select the proper spectre mitigation before patching alternatives */ spectre_v2_select_mitigation(); @@ -95,6 +108,21 @@ static const char *spectre_v2_strings[] static enum spectre_v2_mitigation spectre_v2_enabled = SPECTRE_V2_NONE; +void x86_spec_ctrl_set(u64 val) +{ + if (val & ~SPEC_CTRL_IBRS) + WARN_ONCE(1, "SPEC_CTRL MSR value 0x%16llx is unknown.\n", val); + else + wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base | val); +} +EXPORT_SYMBOL_GPL(x86_spec_ctrl_set); + +u64 x86_spec_ctrl_get_default(void) +{ + return x86_spec_ctrl_base; +} +EXPORT_SYMBOL_GPL(x86_spec_ctrl_get_default); + #ifdef RETPOLINE static bool spectre_v2_bad_module;