Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp549359ybk; Sat, 9 May 2020 10:38:59 -0700 (PDT) X-Google-Smtp-Source: APiQypKC6HQcxip0gF6/IqGNOUrgeV+p1Q1godu1KePsEo+E8RWKMmoovpgIv7KbgS/AY3DC0BJx X-Received: by 2002:a17:906:390a:: with SMTP id f10mr6770784eje.74.1589045939220; Sat, 09 May 2020 10:38:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589045939; cv=none; d=google.com; s=arc-20160816; b=DaERAQywx4ejnLjhrrcarQe4qpryaOvWjtR0RN6mNxKbcscW3dJxSGx6eOqwyd+wkm 1JqrXQK5ul/osJ5BOOKUnIeUIlsb/oN5Rv5eV6k6EfwQV0fVPDmwzuovmh1oLxjBd4MA 8JMOuQlfbkZSSmhUB2fpzZcNoeSyeCuq2VRrFIJvnNwK9bbprwVMBLPzV1l6PF9VKUPb rQGK11yFfEDw5Mu10mY3vFUofQeWMn/MhNHsIz0/1Sold/ojxDAempKoBLCZceqAsxpP MTInJMoB8d9rOPOoip5Fqv8BiDbZL44mOp//qsT9vPNG8fHi5I290yyo2YFYmnZeTwfs OcxA== 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=IDxLwQG8HvBEEL9BZrhyLJYCac24d/HSQ2vA0QZUl74=; b=bmOd4NYzTwraY7oxwObttDQCqDgggc8OF3z1tj4+iVogxCLYWRuRejIqNNa0OlqkYM vfrjll19sJluHn+1qLY83ylvY6waQgL5EcRuSs3uAPYfXKwWkK3Bksx7eLCLkInSi3zw 5SvqnHbAwmoqYDVGCRLpjaAsmIBi1uSUU313/NOXCawfclCsFduRKUNyHgjRw415B2I6 pydAA9aLN0fuZj3sCADusfzfKIf7niecKsMOjwVb+GODf8FeG4glTf3B25H0PBbmL7P9 16x6wM3viNb6y0YA08B1nBOCw2k54qsyYgm6O+rteX0gByVpeRVcNSMV9PbQhmzQCk2U Zzng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=nIN5qX+T; 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 qx7si1584221ejb.526.2020.05.09.10.38.36; Sat, 09 May 2020 10:38:59 -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=nIN5qX+T; 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 S1728532AbgEIRhM (ORCPT + 99 others); Sat, 9 May 2020 13:37:12 -0400 Received: from mail.kernel.org ([198.145.29.99]:54334 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728285AbgEIRhH (ORCPT ); Sat, 9 May 2020 13:37:07 -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 A3DEA2495E; Sat, 9 May 2020 17:37:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1589045826; bh=r4D+aChn8KLioSykZzXYPZ3ZeQ120BCv3VVd3s3G1UU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nIN5qX+TEV0we8L3IbSQxFwqF3IGLubsfM01/+HJ56wieaFt1ZMDnG8BF4xNQ854+ HIvy8UVLPqzHg1ukB5JDQ0TLhecoT8qt9RPXWSx3I8mr8SHmPGw0fO0pIKxVzWnt/N cvrjl15pkMpcdPPuTDQaoiZ8UIUydyM01KlJyFiY= 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 v11 05/18] x86/entry/64: Switch CR3 before SWAPGS in paranoid entry Date: Sat, 9 May 2020 13:36:42 -0400 Message-Id: <20200509173655.13977-6-sashal@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200509173655.13977-1-sashal@kernel.org> References: <20200509173655.13977-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 3adb3c8e2409b..7f27626f8426f 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