Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp1228033ybp; Fri, 4 Oct 2019 11:21:31 -0700 (PDT) X-Google-Smtp-Source: APXvYqx2UEGYaNERUBcFXeCfR4ZWMfrMghCfLFXGcMeymL8eUfyHFZWVZdODjSU7fCD/QgtwVG1Q X-Received: by 2002:a50:aa96:: with SMTP id q22mr16681943edc.179.1570213291069; Fri, 04 Oct 2019 11:21:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570213291; cv=none; d=google.com; s=arc-20160816; b=ZRic8Pigu8cgJMjCHMs8d7WRT5O/MumPJYFk7HOZaAfbFUHK3Jd0fNgi1t77aCIOWS WKag7YvxF3uW6odQ1Lvrasi+SRExxms37TjGrPDBaqvxosII79w8c7ZftJ1Xab2fhfqE 8Cn3P7O6g/LMcopgyB4heT9yMNHIfLFE5qdhKRVVIQ5pfwbnJcyuIA6vUCxQLBV8xEjw bG/3tc3+KhUnXmzKd9QMV5x5RbVZuKprOLIjeZLlayedhQDv4FWPVv315i4kO8RuLmnV 6dtErueJ4fK7EXsnVY6Rd/JWt6ZKb6Ybn6qYADPJH5lTNE/pMAJurzuR3ILDmf6iHkr7 i1AQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=xAwq6CRvrEKlOvaU4pSQaNUkTPFPC2OYyABvLtXR4wE=; b=nrx6S/7KhpsiLgHh5xGdgA09CRPhUFIv/t9NaZy7+HcJobU0PHoDyxq4+Q9/dCeZSp PsIsi0+7cKKcKr60uBbCfcCD86XoBwwja5rGe/MD3OO4k21NudaQHt2VPc+Pc3Q+cVfS lAdSJMVulsRuGI9TY085anlU1nq0P4P6jpoPkUjtICCuTcVk7BsSQ65g87ZmhlFjY8Uz QzFKqD+v6D2AfNg404q0CdeSoowIpv/sMSkuQJw0K6Fu9PC/i+2Wrjo/3/vU5L2B76gn nTulOjPEaJz6IRbtx4qInZJKIqMEMjkvnfL50ObImRfDRw2BWiaq3ewqb9KvoYY6/lcJ YU+g== ARC-Authentication-Results: i=1; mx.google.com; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c48si4001590edc.169.2019.10.04.11.21.07; Fri, 04 Oct 2019 11:21:31 -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; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730407AbfJDSRD (ORCPT + 99 others); Fri, 4 Oct 2019 14:17:03 -0400 Received: from mga07.intel.com ([134.134.136.100]:15508 "EHLO mga07.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730532AbfJDSRB (ORCPT ); Fri, 4 Oct 2019 14:17:01 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga105.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 04 Oct 2019 11:17:01 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.67,257,1566889200"; d="scan'208";a="204394766" Received: from chang-linux-3.sc.intel.com ([172.25.66.185]) by orsmga002.jf.intel.com with ESMTP; 04 Oct 2019 11:17:00 -0700 From: "Chang S. Bae" To: linux-kernel@vger.kernel.org, tglx@linutronix.de, bp@alien8.de, luto@kernel.org Cc: hpa@zytor.com, dave.hansen@intel.com, tony.luck@intel.com, ak@linux.intel.com, ravi.v.shankar@intel.com, chang.seok.bae@intel.com, Vegard Nossum Subject: [PATCH v9 05/17] x86/entry/64: Switch CR3 before SWAPGS in paranoid entry Date: Fri, 4 Oct 2019 11:15:57 -0700 Message-Id: <1570212969-21888-6-git-send-email-chang.seok.bae@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1570212969-21888-1-git-send-email-chang.seok.bae@intel.com> References: <1570212969-21888-1-git-send-email-chang.seok.bae@intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When FSGSBASE is enabled, the GS base handling in paranoid entry will need to retrieve the kernel GS base which requires that the kernel page table is active. As the CR3 switch to the kernel page tables (PTI is active) does not depend on kernel GS base, move the CR3 switch in front of the GS base handling. Comment the EBX content while at it. No functional change. Signed-off-by: Chang S. Bae Reviewed-by: Tony Luck Cc: Thomas Gleixner Cc: Borislav Petkov Cc: Andy Lutomirski Cc: H. Peter Anvin Cc: Dave Hansen Cc: Tony Luck Cc: Andi Kleen Cc: Vegard Nossum --- Changes from v8: none Changes from v7: * Rebased onto the LFENCE-based SWAPGS mitigation code * Dropped the READ_MSR_GSBASE macro by Thomas * Rewrote changelog and comments by Thomas * Use 'GS base' consistently, instead of 'GSBASE' --- arch/x86/entry/entry_64.S | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/arch/x86/entry/entry_64.S b/arch/x86/entry/entry_64.S index dd0d62a..edb4160 100644 --- a/arch/x86/entry/entry_64.S +++ b/arch/x86/entry/entry_64.S @@ -1219,15 +1219,7 @@ ENTRY(paranoid_entry) cld PUSH_AND_CLEAR_REGS save_ret=1 ENCODE_FRAME_POINTER 8 - movl $1, %ebx - movl $MSR_GS_BASE, %ecx - rdmsr - testl %edx, %edx - js 1f /* negative -> in kernel */ - SWAPGS - xorl %ebx, %ebx -1: /* * Always stash CR3 in %r14. This value will be restored, * verbatim, at exit. Needed if paranoid_entry interrupted @@ -1237,16 +1229,31 @@ ENTRY(paranoid_entry) * This is also why CS (stashed in the "iret frame" by the * hardware at entry) can not be used: this may be a return * to kernel code, but with a user CR3 value. + * + * Switching CR3 does not depend on kernel GS base so it can + * be done before switching to the kernel GS base. This is + * required for FSGSBASE because the kernel GS base has to + * be retrieved from a kernel internal table. */ SAVE_AND_SWITCH_TO_KERNEL_CR3 scratch_reg=%rax save_reg=%r14 + /* EBX = 1 -> kernel GSBASE active, no restore required */ + movl $1, %ebx /* - * The above SAVE_AND_SWITCH_TO_KERNEL_CR3 macro doesn't do an - * unconditional CR3 write, even in the PTI case. So do an lfence - * to prevent GS speculation, regardless of whether PTI is enabled. + * The kernel-enforced convention is a negative GS base indicates + * a kernel value. No SWAPGS needed on entry and exit. */ - FENCE_SWAPGS_KERNEL_ENTRY + movl $MSR_GS_BASE, %ecx + rdmsr + testl %edx, %edx + jns .Lparanoid_entry_swapgs + ret +.Lparanoid_entry_swapgs: + SWAPGS + FENCE_SWAPGS_KERNEL_ENTRY + /* EBX = 0 -> SWAPGS required on exit */ + xorl %ebx, %ebx ret END(paranoid_entry) -- 2.7.4