Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp1575105ybk; Sun, 10 May 2020 21:55:45 -0700 (PDT) X-Google-Smtp-Source: APiQypKwFsnA3zZdxH9YunLvg2HQxINyY0tjWwmOaujlFyT6kdFQSzVjCVCx3uA6+RKebw10pWDX X-Received: by 2002:a17:906:340a:: with SMTP id c10mr12043212ejb.218.1589172945481; Sun, 10 May 2020 21:55:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589172945; cv=none; d=google.com; s=arc-20160816; b=DxQ/dE5b2XIxFI++jvJ9stTkk7N3ZsWyfwQzrn2aTEt1qVYzVImgOfiJYa5j8rWjcu na474i+S/BipUZ5AH/VCxrOOfRlVBxxUQ1yl8UH4dWjCyvaFC9tuNN3Ns5LILRkCJ9E5 VMK5QE3XDKYK5XrllK29OYKZbxZ2BTFJXZeG1yUo2JQD8j07bjDzamCW/W5I0pCQwHjd bpX+E5meJvSJsmloHKbQZNNePQIHgEKhYe74XlnmX7LRTLvBeufSzAXDY5vagcA7GHl2 9JLx+I0qdJdGsLT/dSUs74VWoO6YV/dtczYLxfTNnVg84cyYDmUS7Y88nCEi5fe0b2/B zBpg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=WJuck5GuQHbVACMXrxJdeAUkPa6rIfPV4YoLZT+NN5Y=; b=uyt8H+nAeDLrqSDsi5AJrCWLqXGAmNvoubK74SFQDZYO1We5Rc1yB6OP+bCVOikyHV VFIVvKpR+UnAAGFvc0Y8mU0RP3Lf8JQVbyDadxPk20+uQQ2wY5EAQht29CNSkP15PSBw 04zEoSVjQhw+EU77o5vXmA81rVmOr6Vf8a3+fkMp2oEBiTvSMYVMsSboQJBjrfEIWrRo kwwalEyPhgw3d6wVD/Sbphze6RwQANAGusWmgQYHcPm95Ii3HSoYjzUQnGjHGeTOpsQy 7tYIcaRFvB7mB64UwPlFwsN0TbRxG3rOeEQkIuMgktyau4rcOPGF352+ouxojoWXaI1d Q4hw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="HtzEeJ/s"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id gj20si5074908ejb.48.2020.05.10.21.55.16; Sun, 10 May 2020 21:55:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="HtzEeJ/s"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728471AbgEKExh (ORCPT + 99 others); Mon, 11 May 2020 00:53:37 -0400 Received: from mail.kernel.org ([198.145.29.99]:37360 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728402AbgEKExe (ORCPT ); Mon, 11 May 2020 00:53:34 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 97AFD208CA; Mon, 11 May 2020 04:53:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1589172813; bh=3HxCBk+i1/xuyraquCNdCuSz2+rSr/9JEZRSASx8d8g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HtzEeJ/sLW7i7ykoJ5YdZfZX6ZUKvdyUdqWrzAu+6aP/E3sX9iNpLwKK1USP7pmPZ ckEigyHhNLGGZ0NeWbigP07G1cOntkoOklzmgJatM9bB1p6IhF8avMAaPyKIAGF0KZ M8rOGIw05V65fD9JZ0Rt0SnQI01G+zj3l1NA5Ra0= From: Sasha Levin 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, Sasha Levin , Vegard Nossum Subject: [PATCH v12 05/18] x86/entry/64: Switch CR3 before SWAPGS in paranoid entry Date: Mon, 11 May 2020 00:52:58 -0400 Message-Id: <20200511045311.4785-6-sashal@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200511045311.4785-1-sashal@kernel.org> References: <20200511045311.4785-1-sashal@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: "Chang S. Bae" 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 Signed-off-by: Sasha Levin 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 --- 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 0da56e6791b73..3ac1313724eaa 100644 --- a/arch/x86/entry/entry_64.S +++ b/arch/x86/entry/entry_64.S @@ -1220,15 +1220,7 @@ SYM_CODE_START_LOCAL(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 @@ -1238,16 +1230,31 @@ SYM_CODE_START_LOCAL(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 SYM_CODE_END(paranoid_entry) -- 2.20.1