Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp883195imm; Thu, 31 May 2018 11:03:37 -0700 (PDT) X-Google-Smtp-Source: ADUXVKLZNElcLJUeJXvPONc+qic3WEhrgiMC2xkDWaoDEJPYHN+UNLEVVm9257bayhfLW9PrUv3o X-Received: by 2002:a62:a6dd:: with SMTP id r90-v6mr2721215pfl.60.1527789817070; Thu, 31 May 2018 11:03:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527789817; cv=none; d=google.com; s=arc-20160816; b=TCzWZJvtCHMtJGU03SUminWI5I0NciTIAF25rG7YV/AJ3BUejcVoe+92szjSGz3S+I BPaYw7ZMAY+rItaBxX+koOoQaFTxmUCx+MA0V+sBWP1IM8v7Mocfy6kaKB0W8xyClx+L lQmzWD6ueITAzYGkKOjq2Ou2ehvFwd0ozZlOnwO3A5XzlFodvO1UXLOYiUotcVBq/dlk pFNIV6chIFnoZAOcd3TXsGpc3l6LH7hOJpn33VldeyvABdeIacMCyhliQlZKArFDBe1w wQ/0E1w4OvIWn5p6Wdv2UxYlhyJSW4o1905b4FIdlvlspOB54yBLEdypsk5wiVazdsBC HRLA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=Fl3yhCdiDQfVesTGupGJ56j6P6WJ6jaCFinIy+d0l44=; b=k/Ewj0E3L+6OKOC/d3yoUE/yr60sXTXjO8np/Dk3lsjPc8MqdkBZqQ5+2eojR58Xak XuDweC5QqS3GAxl96Fx7fHnYmB11YBJMDd4ix6/dyXpRGQintYeqeZe4yCJqz7gSD13m DbPjvLI/5iPhQskjg68JzfxnLEE7jsTvLSrY78kk0V1jqvhw6rOzwqeUJejlFKoLbDIo YqUS1RmuTqI7lsR5kkWIjf4eYRGBeeMy+v9uN8r+3BKkLzUfEw8HjQqVspkL7pgPCMo8 RZo+lSp1lgJ179soalYUUnT2qW0Qv2T5W4+jMaKwNxObMpzpSdlmJDBuJmb3r7huIewk Ma/Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n61-v6si38340458plb.256.2018.05.31.11.03.22; Thu, 31 May 2018 11:03:37 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756027AbeEaSBO (ORCPT + 99 others); Thu, 31 May 2018 14:01:14 -0400 Received: from mga01.intel.com ([192.55.52.88]:18555 "EHLO mga01.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755948AbeEaR7B (ORCPT ); Thu, 31 May 2018 13:59:01 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga101.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 31 May 2018 10:58:57 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.49,463,1520924400"; d="scan'208";a="60725523" Received: from chang-linux-2.sc.intel.com ([10.3.52.139]) by orsmga001.jf.intel.com with ESMTP; 31 May 2018 10:58:57 -0700 From: "Chang S. Bae" To: Andy Lutomirski , "H . Peter Anvin" , Thomas Gleixner , Ingo Molnar Cc: Andi Kleen , Dave Hansen , Markus T Metzger , "Ravi V . Shankar" , "Chang S . Bae" , linux-kernel@vger.kernel.org Subject: [PATCH V2 08/15] x86/fsgsbase/64: Add intrinsics/macros for FSGSBASE instructions Date: Thu, 31 May 2018 10:58:38 -0700 Message-Id: <1527789525-8857-9-git-send-email-chang.seok.bae@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1527789525-8857-1-git-send-email-chang.seok.bae@intel.com> References: <1527789525-8857-1-git-send-email-chang.seok.bae@intel.com> 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 RD/WR FS/GS BASE instructions. Very straight forward. Used in followon patch. [luto: rename the variables from fs and gs to fsbase and gsbase and make fsgs.h safe to include on 32-bit kernels.] v2: Use __always_inline Signed-off-by: Andi Kleen Signed-off-by: Andy Lutomirski [chang: Replace new instruction macros with GAS-compatible and renaming. Note: if GCC supports it, we can add -mfsgsbase to CFLAGS and use the builtins here for extra performance.] Signed-off-by: Chang S. Bae Cc: H. Peter Anvin Cc: Dave Hansen Cc: Thomas Gleixner Cc: Ingo Molnar --- arch/x86/include/asm/fsgsbase.h | 70 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/arch/x86/include/asm/fsgsbase.h b/arch/x86/include/asm/fsgsbase.h index 0d4fbef..ed42015 100644 --- a/arch/x86/include/asm/fsgsbase.h +++ b/arch/x86/include/asm/fsgsbase.h @@ -18,6 +18,42 @@ unsigned long read_task_gsbase(struct task_struct *task); int write_task_fsbase(struct task_struct *task, unsigned long fsbase); int write_task_gsbase(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(".byte 0xf3, 0x48, 0x0f, 0xae, 0xc0 # rdfsbaseq %%rax" + : "=a" (fsbase) + :: "memory"); + return fsbase; +} + +static __always_inline unsigned long rdgsbase(void) +{ + unsigned long gsbase; + + asm volatile(".byte 0xf3, 0x48, 0x0f, 0xae, 0xc8 # rdgsbaseq %%rax;" + : "=a" (gsbase) + :: "memory"); + return gsbase; +} + +static __always_inline void wrfsbase(unsigned long fsbase) +{ + asm volatile(".byte 0xf3, 0x48, 0x0f, 0xae, 0xd0 # wrfsbaseq %%rax" + :: "a" (fsbase) + : "memory"); +} + +static __always_inline void wrgsbase(unsigned long gsbase) +{ + asm volatile(".byte 0xf3, 0x48, 0x0f, 0xae, 0xd8 # wrgsbaseq %%rax;" + :: "a" (gsbase) + : "memory"); +} + /* Helper functions for reading/writing FS/GS base */ static inline unsigned long read_fsbase(void) @@ -42,6 +78,40 @@ void write_inactive_gsbase(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.7.4