From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.0 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 09A5AC43387 for ; Wed, 16 Jan 2019 22:49:35 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id D1C70206C2 for ; Wed, 16 Jan 2019 22:49:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387633AbfAPWtO (ORCPT ); Wed, 16 Jan 2019 17:49:14 -0500 Received: from mga02.intel.com ([134.134.136.20]:57469 "EHLO mga02.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387570AbfAPWtJ (ORCPT ); Wed, 16 Jan 2019 17:49:09 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 16 Jan 2019 14:49:08 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.56,487,1539673200"; d="scan'208";a="117288384" Received: from chang-linux-2.sc.intel.com ([10.3.52.165]) by fmsmga008.fm.intel.com with ESMTP; 16 Jan 2019 14:49:08 -0800 From: "Chang S. Bae" To: Andy Lutomirski , Thomas Gleixner , Ingo Molnar , "H . Peter Anvin" , Andi Kleen Cc: Dave Hansen , Markus T Metzger , Ravi Shankar , "Chang S . Bae" , LKML Subject: [PATCH v4 04/13] x86/fsgsbase/64: Add intrinsics/macros for FSGSBASE instructions Date: Wed, 16 Jan 2019 14:48:40 -0800 Message-Id: <20190116224849.8617-5-chang.seok.bae@intel.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190116224849.8617-1-chang.seok.bae@intel.com> References: <20190116224849.8617-1-chang.seok.bae@intel.com> 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: Andi Kleen Add C intrinsics and assembler macros for the new FSBASE and GSBASE instructions. Very straight forward. Used in followon patches. [ luto: Rename the variables from FS and GS to FSBASE and GSBASE and make safe to include on 32-bit kernels. ] v2: Use __always_inline [ chang: Revise the changelog. Place them in . Replace the macros with GAS-compatible ones. ] If GCC supports it, we can add -mfsgsbase to CFLAGS and use the builtins here for extra performance. [ chang: Use FSGSBASE instructions directly instead of .byte ] Signed-off-by: Andi Kleen Signed-off-by: Andy Lutomirski Signed-off-by: Chang S. Bae Reviewed-by: Andy Lutomirski # C parts only Cc: H. Peter Anvin Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Dave Hansen --- arch/x86/include/asm/fsgsbase.h | 64 +++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/arch/x86/include/asm/fsgsbase.h b/arch/x86/include/asm/fsgsbase.h index bca4c743de77..09b34ee0cfd4 100644 --- a/arch/x86/include/asm/fsgsbase.h +++ b/arch/x86/include/asm/fsgsbase.h @@ -19,6 +19,36 @@ extern unsigned long x86_gsbase_read_task(struct task_struct *task); extern void x86_fsbase_write_task(struct task_struct *task, unsigned long fsbase); extern void x86_gsbase_write_task(struct task_struct *task, unsigned long gsbase); +/* Must be protected by X86_FEATURE_FSGSBASE check. */ + +static __always_inline unsigned long rdfsbase(void) +{ + unsigned long fsbase; + + asm volatile("rdfsbase %0" : "=r" (fsbase) :: "memory"); + + return fsbase; +} + +static __always_inline unsigned long rdgsbase(void) +{ + unsigned long gsbase; + + asm volatile("rdgsbase %0" : "=r" (gsbase) :: "memory"); + + return gsbase; +} + +static __always_inline void wrfsbase(unsigned long fsbase) +{ + asm volatile("wrfsbase %0" :: "r" (fsbase) : "memory"); +} + +static __always_inline void wrgsbase(unsigned long gsbase) +{ + asm volatile("wrgsbase %0" :: "r" (gsbase) : "memory"); +} + /* Helper functions for reading/writing FS/GS base */ static inline unsigned long x86_fsbase_read_cpu(void) @@ -51,6 +81,40 @@ static inline void x86_gsbase_write_cpu_inactive(unsigned long gsbase) #endif /* CONFIG_X86_64 */ +#else /* __ASSEMBLY__ */ + +#ifdef CONFIG_X86_64 + +#include + +.macro RDGSBASE opd + REG_TYPE rdgsbase_opd_type \opd + .if rdgsbase_opd_type == REG_TYPE_R64 + R64_NUM rdgsbase_opd \opd + .byte 0xf3 + PFX_REX rdgsbase_opd 0 W = 1 + .else + .error "RDGSBASE: only for 64-bit value" + .endif + .byte 0xf, 0xae + MODRM 0xc0 rdgsbase_opd 1 +.endm + +.macro WRGSBASE opd + REG_TYPE wrgsbase_opd_type \opd + .if wrgsbase_opd_type == REG_TYPE_R64 + R64_NUM wrgsbase_opd \opd + .byte 0xf3 + PFX_REX wrgsbase_opd 0 W = 1 + .else + .error "WRGSBASE: only for 64-bit value" + .endif + .byte 0xf, 0xae + MODRM 0xd0 wrgsbase_opd 1 +.endm + +#endif /* CONFIG_X86_64 */ + #endif /* __ASSEMBLY__ */ #endif /* _ASM_FSGSBASE_H */ -- 2.19.1