Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp209728rdb; Tue, 5 Dec 2023 03:22:42 -0800 (PST) X-Google-Smtp-Source: AGHT+IHZG3FFoH4qgPgvAgBwwFKpvlShpXJyawUKg9tVzqFSNJyqZVVoQ1YIhfyUPKU/Aa6EaxX8 X-Received: by 2002:aa7:88ce:0:b0:6cd:e35f:997f with SMTP id k14-20020aa788ce000000b006cde35f997fmr1411000pff.6.1701775361696; Tue, 05 Dec 2023 03:22:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701775361; cv=none; d=google.com; s=arc-20160816; b=ojp7CzI4t1cpueKyHAondDPHg1mrGOmCQ+dQtcqPbVFK0HFKHAgIz+i51iEEU5cmv/ dl1Z1xzEaecLdQATmYuuNApKVHUNSt2GQ7FORxvY5OUFI7aa5XrTjVmIrLFUO6cSpoC2 AWztJbAmbcysieRzmwO0J4UUVj1WpU1rMgBZUAPBpbEJUVLHF7N6zibUeGv7y4d8APjE AzOjSwAUcjMa7LMp92LAy4VqlF98IVXbFoZzyj5O8o+tMgkDrOeAgQiJ5OxwokNJahC0 fBr1+RxCyeUy89mXcvZqThe/iIyyIpU/HZo9BnvJQC9ndIq8CD/BUBOsq6+WjRNMa00K 6JoQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Y0OVv8JPSOCPWqZZVeEP7Z/baealtP8vg8jQbAHxLl4=; fh=uCCWEUVpLc/xDP/D3WvGY/BxHU9d0NQYu/T71GEsNmA=; b=mM0FVlszIhjJaFds2LZAJhq9FGCHvpniEeEFjK2mmZ8EqT13Ben2y2SCGVBqNj+qwX XIJhp4U5vRRrCcvT6/VuJC2Z+trv5rux9VH4NfTo0rHmtf6blvEOvz8F33atMvcaHG7O SQ7K3MHo3rSb/aiqnZsl7hCpakurwi7x3sY4IgJOEj53fpe1vEkJ3GTXVOm62aijvwYv fHbEbJRAAM0/ZF3jmQ8THmQY6dZZO0/JrDXPafyqHbJX5KiSrE/TV7ee1GI3n3B/ij3b 6/xOEE7LNe/shx3LCs4JUF6r8O8VVchl9k6fqLxAYbT8rYsbePRXWXBBwpIjffmOSFru Wing== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=iYZFvm2Y; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id q3-20020a056a00088300b006c4a78a81a5si9519865pfj.88.2023.12.05.03.22.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Dec 2023 03:22:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=iYZFvm2Y; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 86A3680C0373; Tue, 5 Dec 2023 03:22:40 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1442176AbjLELWV (ORCPT + 99 others); Tue, 5 Dec 2023 06:22:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34392 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1442114AbjLELV1 (ORCPT ); Tue, 5 Dec 2023 06:21:27 -0500 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1B308189; Tue, 5 Dec 2023 03:21:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1701775290; x=1733311290; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=/uN2WUhxBQNYcu14PCjKEEJ/KO0CK2Le/qFJcLIBoLk=; b=iYZFvm2YxCezV5Zqp5TCWSS+yNJL6QsrFjwAPC4776BUPZNlbyGRkndx KF/Me5CULvbf2CS/8FeTaQ0KCyOtVCOx1DfMmNtePClorbdSUKK4wfHXG JGVztkfm2C8MaKGieW607UrYCgw50KjKm12FFIibr3pZJBgxg5hrOsehM 8b6+w9sM0AEYPxTbKKoGcEjeXEOvE8z72PbYr44jncfq6RwNIic095XWu lRSbCJ+A6Bdz/H6iR+OcsU1hV6wANMOnDHruu1sH07sXsKtbMJrngDPB8 aLnIgFwuA6ekNH3eDgJoEFi+Zd/RpmCmf4H1E9BjA0QbSNZ6sqOaR8aeI A==; X-IronPort-AV: E=McAfee;i="6600,9927,10914"; a="942528" X-IronPort-AV: E=Sophos;i="6.04,252,1695711600"; d="scan'208";a="942528" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Dec 2023 03:21:23 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10914"; a="1018192960" X-IronPort-AV: E=Sophos;i="6.04,252,1695711600"; d="scan'208";a="1018192960" Received: from unknown (HELO fred..) ([172.25.112.68]) by fmsmga006.fm.intel.com with ESMTP; 05 Dec 2023 03:21:21 -0800 From: Xin Li To: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-edac@vger.kernel.org, linux-hyperv@vger.kernel.org, kvm@vger.kernel.org, xen-devel@lists.xenproject.org Cc: tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, luto@kernel.org, pbonzini@redhat.com, seanjc@google.com, peterz@infradead.org, jgross@suse.com, ravi.v.shankar@intel.com, mhiramat@kernel.org, andrew.cooper3@citrix.com, jiangshanlai@gmail.com, nik.borisov@suse.com, shan.kang@intel.com Subject: [PATCH v13 18/35] x86/fred: Disallow the swapgs instruction when FRED is enabled Date: Tue, 5 Dec 2023 02:50:07 -0800 Message-ID: <20231205105030.8698-19-xin3.li@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231205105030.8698-1-xin3.li@intel.com> References: <20231205105030.8698-1-xin3.li@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Tue, 05 Dec 2023 03:22:40 -0800 (PST) From: "H. Peter Anvin (Intel)" SWAPGS is no longer needed thus NOT allowed with FRED because FRED transitions ensure that an operating system can _always_ operate with its own GS base address: - For events that occur in ring 3, FRED event delivery swaps the GS base address with the IA32_KERNEL_GS_BASE MSR. - ERETU (the FRED transition that returns to ring 3) also swaps the GS base address with the IA32_KERNEL_GS_BASE MSR. And the operating system can still setup the GS segment for a user thread without the need of loading a user thread GS with: - Using LKGS, available with FRED, to modify other attributes of the GS segment without compromising its ability always to operate with its own GS base address. - Accessing the GS segment base address for a user thread as before using RDMSR or WRMSR on the IA32_KERNEL_GS_BASE MSR. Note, LKGS loads the GS base address into the IA32_KERNEL_GS_BASE MSR instead of the GS segment's descriptor cache. As such, the operating system never changes its runtime GS base address. Signed-off-by: H. Peter Anvin (Intel) Tested-by: Shan Kang Signed-off-by: Xin Li --- Change since v12: * Use the ASCII char ' (char number 0x27), instead of its unicode char (Borislav Petkov). Change since v8: * Explain why writing directly to the IA32_KERNEL_GS_BASE MSR is doing the right thing (Thomas Gleixner). --- arch/x86/kernel/process_64.c | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c index b924477c5ba8..7f66c0b14de6 100644 --- a/arch/x86/kernel/process_64.c +++ b/arch/x86/kernel/process_64.c @@ -166,7 +166,29 @@ static noinstr unsigned long __rdgsbase_inactive(void) lockdep_assert_irqs_disabled(); - if (!cpu_feature_enabled(X86_FEATURE_XENPV)) { + /* + * SWAPGS is no longer needed thus NOT allowed with FRED because + * FRED transitions ensure that an operating system can _always_ + * operate with its own GS base address: + * - For events that occur in ring 3, FRED event delivery swaps + * the GS base address with the IA32_KERNEL_GS_BASE MSR. + * - ERETU (the FRED transition that returns to ring 3) also swaps + * the GS base address with the IA32_KERNEL_GS_BASE MSR. + * + * And the operating system can still setup the GS segment for a + * user thread without the need of loading a user thread GS with: + * - Using LKGS, available with FRED, to modify other attributes + * of the GS segment without compromising its ability always to + * operate with its own GS base address. + * - Accessing the GS segment base address for a user thread as + * before using RDMSR or WRMSR on the IA32_KERNEL_GS_BASE MSR. + * + * Note, LKGS loads the GS base address into the IA32_KERNEL_GS_BASE + * MSR instead of the GS segment’s descriptor cache. As such, the + * operating system never changes its runtime GS base address. + */ + if (!cpu_feature_enabled(X86_FEATURE_FRED) && + !cpu_feature_enabled(X86_FEATURE_XENPV)) { native_swapgs(); gsbase = rdgsbase(); native_swapgs(); @@ -191,7 +213,8 @@ static noinstr void __wrgsbase_inactive(unsigned long gsbase) { lockdep_assert_irqs_disabled(); - if (!cpu_feature_enabled(X86_FEATURE_XENPV)) { + if (!cpu_feature_enabled(X86_FEATURE_FRED) && + !cpu_feature_enabled(X86_FEATURE_XENPV)) { native_swapgs(); wrgsbase(gsbase); native_swapgs(); -- 2.43.0