From patchwork Thu Oct 13 20:01:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Li, Xin3" X-Patchwork-Id: 2359 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp465631wrs; Thu, 13 Oct 2022 13:25:57 -0700 (PDT) X-Google-Smtp-Source: AMsMyM713F1DnoJYo5dRSj75yHGjmRhpzqKF+OKhut4YRZkB4vElexZUG1oIdmgOsq9V0ikKKPjm X-Received: by 2002:a62:d448:0:b0:565:ef61:ee0d with SMTP id u8-20020a62d448000000b00565ef61ee0dmr1257355pfl.73.1665692757099; Thu, 13 Oct 2022 13:25:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665692757; cv=none; d=google.com; s=arc-20160816; b=KwiTAT98KLSdMatlIjt2oScooe1czVkoR4l1NxtrOB/Uzpvkwp1JTRmn0411NdqZjS wst282Zhw5S9phBMFg3rplJuRMy5XrQJJsJmmhkwRd0BiF5QB14gL0wWsD/w9qT030S6 XicUGmKKhGh/Lf50kqz/P9cikAU0ltAS+0w0/ECyw18AtdGKY4w/Pa9eahUiESGZ5yQm HvoHtS+0+iC+WN8rAIi63j0iH3VJ+jGwi7IJsgvWMfzZTCEwf2uFK8wa+b3k28aKhOar kfa6mKV1dVMX2jHbP36htSXMPTqirlcDWgFswTiHKy0fYHnJ381tdzPA8p0bfJ3ndwZQ Pq+g== 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=q/J32esKxNLhWVIe4rlNmfTqIFhmRrAPGF03f3PFekQ=; b=wE8Fizqw47h5IIJbDShfebmzpmSQb4rkYpbX8tw/VLAxQ2SAKZXWM1mUr8srqT6yt4 +im9vyqX7/ktVD6jqZoH0kIVocNIqjMlfVlvQq/UpBDgpRsgW8caMY4nNA+JlcMhKKvy 4636VpFR5eVgE+HBOJK7tf8vJ0HNbkZrzZBif6LSUTSBo+gUYTZdMoYYY1GVETw8C967 iJCxs8jwo8O7ouzCb+tP6LMPpQTIiEozh9yPDUHzzMIkiTZ3Dp3x7Ji1B6m1jcqeN+1n XA9YeQMF4QTLnwH8CC3ygykg5C08YHJKvmU7gVrYBgej1MXWF+WfK+k3UQ8zwIO0QP6z M51g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=c8gqwewC; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b10-20020a170902d50a00b00179c4bebb6bsi853599plg.409.2022.10.13.13.25.45; Thu, 13 Oct 2022 13:25:57 -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=c8gqwewC; 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 S229949AbiJMUYC (ORCPT + 99 others); Thu, 13 Oct 2022 16:24:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55250 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229846AbiJMUXc (ORCPT ); Thu, 13 Oct 2022 16:23:32 -0400 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9A8C5159D58 for ; Thu, 13 Oct 2022 13:23:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1665692611; x=1697228611; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=WGOoMAJR5hBAKRpXEvmTbPxECjoZohq6t2fBwrzA0Pw=; b=c8gqwewCizdqpOO43cn1Vo6lTXO68K7gzlcG7pw7BSVEabvn3VHNYMSk fI2NSKKEivg7z1ZNItiT0QtsFnTY6/VzHHLmWxAj3Zcdi4fVjfQ/tGu16 WarjQuCRbMM2koMzkDouyYhIk9DQ37weDcvrTI30QfnogAgJMzriQqxWo ozwpiFqXFSGnVph8+c19LMu10yd9Mm/A4urMXnenyRFaGCIMAw/3DG3pU RZsXMFGETzgmLN7xe9AxNhQGiH2qRB7O5K79plkn24y/GBjJ5BFAhRoGq XoWo4sS6ckjw/cRi0hnJ45oTh3CFEhPHV0kH7rvaiTADuqIMKWUoWYLJ0 g==; X-IronPort-AV: E=McAfee;i="6500,9779,10499"; a="302808970" X-IronPort-AV: E=Sophos;i="5.95,182,1661842800"; d="scan'208";a="302808970" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Oct 2022 13:23:30 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10499"; a="690271053" X-IronPort-AV: E=Sophos;i="5.95,182,1661842800"; d="scan'208";a="690271053" Received: from unknown (HELO fred..) ([172.25.112.68]) by fmsmga008.fm.intel.com with ESMTP; 13 Oct 2022 13:23:28 -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, peterz@infradead.org, brgerst@gmail.com, chang.seok.bae@intel.com Subject: [PATCH v3 6/6] x86/gsseg: use the LKGS instruction if available for load_gs_index() Date: Thu, 13 Oct 2022 13:01:34 -0700 Message-Id: <20221013200134.1487-7-xin3.li@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221013200134.1487-1-xin3.li@intel.com> References: <20221013200134.1487-1-xin3.li@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1746605448753364896?= X-GMAIL-MSGID: =?utf-8?q?1746605448753364896?= 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: Peter Zijlstra (Intel) Signed-off-by: Brian Gerst Signed-off-by: Xin Li link: https://lkml.org/lkml/2022/10/7/352 link: https://lkml.org/lkml/2022/10/7/373 link: https://lkml.org/lkml/2022/10/10/1286 --- arch/x86/include/asm/gsseg.h | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/gsseg.h b/arch/x86/include/asm/gsseg.h index 5e3b56a17098..7463ac65ef56 100644 --- a/arch/x86/include/asm/gsseg.h +++ b/arch/x86/include/asm/gsseg.h @@ -3,15 +3,39 @@ #define _ASM_X86_GSSEG_H #include + +#include +#include +#include #include +#include #ifdef CONFIG_X86_64 extern asmlinkage void asm_load_gs_index(u16 selector); +/* Replace with "lkgs %di" once binutils support LKGS instruction */ +#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. + */ + asm_inline volatile("1:\n" + ALTERNATIVE("call asm_load_gs_index\n", + _ASM_BYTES(0x3e) LKGS_DI, + X86_FEATURE_LKGS) + _ASM_EXTABLE_TYPE_REG(1b, 1b, EX_TYPE_ZERO_REG, %k[sel]) + : ASM_OUTPUT2([sel] "+D" (sel), ASM_CALL_CONSTRAINT) + : : _ASM_AX); } #endif /* CONFIG_X86_64 */