Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp1175390rwb; Thu, 6 Oct 2022 09:20:33 -0700 (PDT) X-Google-Smtp-Source: AMsMyM701OsKG3hBf6UFSyqtRtPxAzdHzSVcXekXEtBaWRj77/y8r+xV9b1u++HRSm99lIKoOc73 X-Received: by 2002:a63:1210:0:b0:435:60d2:b953 with SMTP id h16-20020a631210000000b0043560d2b953mr527683pgl.459.1665073232811; Thu, 06 Oct 2022 09:20:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665073232; cv=none; d=google.com; s=arc-20160816; b=ETi7Fa/hfMZyHwfcPlr7yiwoUOqGcMMXR20KkijBiOOT6KW3E6jNfUYrtXLA9+6NkG nLL2Ja5B3f0KMDaUq2I7AlfzMMpIt9V/LcQYorFV9Q/wXn50TMe+8IUWpAqyUw8NxUPL yraonxgC49PH9mAmM986MIEaI7PUc/VpketLX+WLco+TcJlHUFr1HZ5q11ZVVVKPBmqP Vs06+Mganp2mu2Mel8cmfDe0ujcsYIV49Z6kQfEvNalBh0xUFHHGr7ZGQRj7KJiAkM7q QllMrHf8U+tU7eDE6hNVwcr3aP1TP0oXYZFbmPeobM8IbGSF7xWitZxFTLULGsgKN6hg fhjQ== 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=vAu65On7AowZ13W5EJ54rOjbfxIfTNoVqR/E6MFKUgQ=; b=P2fdMb5dORoOY2U+V4sRi7B4zKjvPP7LvZ0QbNnOfF2+g/iqwxkvqxOrpl4aQWyUb3 NNyv2bTCEcWzeoaLsUcRDVgc52dEH0XIoGSxrIdCRHIRyBsj+Z7P6rcjBmKJmV76Isg1 7hNTKcTPLNcLQrb/sPUZb/qhq9h8H1GlXMtIUW/iHtHP6g5nfDmjp1Q5ZebKMMszCKbX 7QKu+hVDTQPWa5oZe0Dk4AzBQxpEfMXNn7A0UjIWgYb6RoZhpQlvDcpM0l1I+k65Vx+i W9b7u6q4ls3tWK1EAgEVn5FYxsHei96SEPsWiGxQlFHvmldKhL2sPm3iMSAy1wQgtqtf R37g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=g3QiBSh2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w18-20020a170902a71200b0017e0ca906e0si13511202plq.562.2022.10.06.09.20.16; Thu, 06 Oct 2022 09:20:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=g3QiBSh2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231971AbiJFQDM (ORCPT + 99 others); Thu, 6 Oct 2022 12:03:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42806 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231968AbiJFQDI (ORCPT ); Thu, 6 Oct 2022 12:03:08 -0400 Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9AFDFAE228 for ; Thu, 6 Oct 2022 09:03:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1665072187; x=1696608187; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=TYDLP/WLagDk6RRKpxF1aIZ7QTqIfIktvLR1UyU4GrE=; b=g3QiBSh2sWgslY+YgKSUpLOo+GKlO1AsiQRinu6nZGFUpr0jMJHgtmvX uVk2hAPAuGTSEkzmlfMrKd3TV8S1IjRzr3Rd5Ntt6t345UBgFGSr+n9pb E50aY82ocCXPlWcc7Vz2u2vE9B2ezTCM6qbVgnxyMPYMGQ4Ng27T4+Bg8 lXt/naDrQuKBziNjFNAwfn02/5jD+LKUbYY40BU6MSC/WhqcOrivNApg/ YhXWkX69oCZcHr4m2BOU82O3ESiRKVtUq3XObxrnAIFivXxr8Msuco6zQ Nvnagk82J1gg3LWrVbX0DV1OPor8k6PNayyCA3H1n2Tn8OlmINSppxjnh w==; X-IronPort-AV: E=McAfee;i="6500,9779,10491"; a="286709428" X-IronPort-AV: E=Sophos;i="5.95,164,1661842800"; d="scan'208";a="286709428" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Oct 2022 09:02:37 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10491"; a="687486237" X-IronPort-AV: E=Sophos;i="5.95,164,1661842800"; d="scan'208";a="687486237" Received: from unknown (HELO fred..) ([172.25.112.68]) by fmsmga008.fm.intel.com with ESMTP; 06 Oct 2022 09:02:36 -0700 From: Xin Li To: linux-kernel@vger.kernel.org, x86@kernel.org Cc: tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, hpa@zytor.com Subject: [PATCH 6/6] x86/gsseg: use the LKGS instruction if available for load_gs_index() Date: Thu, 6 Oct 2022 08:40:41 -0700 Message-Id: <20221006154041.13001-7-xin3.li@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221006154041.13001-1-xin3.li@intel.com> References: <20221006154041.13001-1-xin3.li@intel.com> MIME-Version: 1.0 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,SPF_HELO_NONE, SPF_NONE 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 From: "H. Peter Anvin (Intel)" The LKGS instruction atomically loads a segment descriptor into the %gs descriptor registers, *except* that %gs.base is unchanged, and the base is instead loaded into MSR_IA32_KERNEL_GS_BASE, which is exactly what we want this function to do. Signed-off-by: H. Peter Anvin (Intel) Signed-off-by: Xin Li --- arch/x86/include/asm/gsseg.h | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/gsseg.h b/arch/x86/include/asm/gsseg.h index 5e3b56a17098..b8a6a98d88b8 100644 --- a/arch/x86/include/asm/gsseg.h +++ b/arch/x86/include/asm/gsseg.h @@ -3,15 +3,41 @@ #define _ASM_X86_GSSEG_H #include + +#include +#include +#include #include +#include #ifdef CONFIG_X86_64 extern asmlinkage void asm_load_gs_index(u16 selector); +#define LKGS_DI _ASM_BYTES(0xf2,0x0f,0x00,0xf7) + static inline void native_load_gs_index(unsigned int selector) { - asm_load_gs_index(selector); + u16 sel = selector; + + /* + * Note: the fixup is used for the LKGS instruction, but + * it needs to be attached to the primary instruction sequence + * as it isn't something that gets patched. + * + * %rax is provided to the assembly routine as a scratch + * register. + */ + alternative_io("1: call asm_load_gs_index\n" + ".pushsection \".fixup\",\"ax\"\n" + "2: xorl %k[sel], %k[sel]\n" + " jmp 1b\n" + ".popsection\n" + _ASM_EXTABLE(1b, 2b), + _ASM_BYTES(0x3e) LKGS_DI, + X86_FEATURE_LKGS, + ASM_OUTPUT2([sel] "+D" (sel), ASM_CALL_CONSTRAINT), + ASM_NO_INPUT_CLOBBER(_ASM_AX)); } #endif /* CONFIG_X86_64 */ -- 2.34.1