Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp880935imm; Thu, 31 May 2018 11:01:41 -0700 (PDT) X-Google-Smtp-Source: ADUXVKK0Iq/qdcuj09SsGC8rMuv6mrnN+OEi9+1PQN9ZrflxDd0RpjUkRX49Mg+/iUzY6b2vlrl0 X-Received: by 2002:a62:1bc2:: with SMTP id b185-v6mr4250696pfb.225.1527789701928; Thu, 31 May 2018 11:01:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527789701; cv=none; d=google.com; s=arc-20160816; b=ftf2iW+Tj+eSDsfa6AhFv9gw94AzOKG+65Qm7XDT3fCLHyo6n1QQwDnO7A4ZpIV0vu qNwAFgdmM1usfTu6DrCfDJAtJRZWUgszRvH7icZQ2KQtBN/wEEyBx5M94g7riqbY+byM 4xgcUpU+l77/MZJ+hHN//hnXiZmh2wCaEAmIYtopcVyz9qYSzbMpgroEGfjc8jcHi/bY VH6wOhdIxWXwTIeL00PhRabdfJbMlTMZr1ObnzZqQkPOSC8LBwnLVrKrR6dMzQ3btAr3 69aSuAVv5r+n+RIuNni4hy7Ukeqap+jk1nJge/+9xOPkgKd08FemAsQdW/HuSI28v/9o vmzg== 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=QIcMm/1e7QLeeJGj2Gi7ag8u0Z32OLpzIbz5cZl97+Q=; b=plLip4Ktwu5LuPXOUBTkG2F1ixmdvoLBDiFZaTRCPJcRf+HZ2Oc+knPWKmEVfICPa4 Lg82MpsZM/1gTmzxD/RcbvPM7XEFRQDKZN7E0p8orlCzikuloP+JiEZCp8Uxo7WgYlIt mY3ppY3zAzBV57cO4dY8/jj2CTjKH43W6TO5qY3KWybstyJEtjnSYFmJWI3z66I77wbG BfG/5zoKCiHPXFH+jOXaghugRFbXqb+SIxj+LtUpT2z2DncboX/46HoL/AEsn29avxug ZZdeU7i4kGvI99d+PNcbnttpTbX86ZvZ+C+1x5cZ1bg8aeGoxy2VrzKg8lb1MkQlT5Nm BnhA== 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 s13-v6si36512626plq.464.2018.05.31.11.01.27; Thu, 31 May 2018 11:01:41 -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 S932782AbeEaR7n (ORCPT + 99 others); Thu, 31 May 2018 13:59:43 -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 S1755994AbeEaR7J (ORCPT ); Thu, 31 May 2018 13:59:09 -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:59:00 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.49,463,1520924400"; d="scan'208";a="60725548" Received: from chang-linux-2.sc.intel.com ([10.3.52.139]) by orsmga001.jf.intel.com with ESMTP; 31 May 2018 10:58:59 -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 14/15] x86/elf: Enumerate kernel FSGSBASE capability in AT_HWCAP2 Date: Thu, 31 May 2018 10:58:44 -0700 Message-Id: <1527789525-8857-15-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 The kernel needs to explicitly enable FSGSBASE. So, the application needs to know if it can safely use these instructions. Just looking at the CPUID bit is not enough because it may be running in a kernel that does not enable the instructions. One way for the application would be to just try and catch the SIGILL. But that is difficult to do in libraries which may not want to overwrite the signal handlers of the main application. So we need to provide a way for the application to discover the kernel capability. I used AT_HWCAP2 in the ELF aux vector which is already used by PPC for similar things. We define a new Linux defined bitmap returned in AT_HWCAP. Next to MONITOR/MWAIT, bit 1 is reserved for FSGSBASE capability checks. The application can then access it manually or using the getauxval() function in newer glibc. Signed-off-by: Andi Kleen [chang: Rebase and edit the patch note accordingly] Signed-off-by: Chang S. Bae Cc: Andy Lutomirski Cc: H. Peter Anvin Cc: Dave Hansen Cc: Thomas Gleixner Cc: Ingo Molnar --- arch/x86/include/uapi/asm/hwcap2.h | 3 +++ arch/x86/kernel/cpu/common.c | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/uapi/asm/hwcap2.h b/arch/x86/include/uapi/asm/hwcap2.h index 6ebaae9..c5ce54e 100644 --- a/arch/x86/include/uapi/asm/hwcap2.h +++ b/arch/x86/include/uapi/asm/hwcap2.h @@ -5,4 +5,7 @@ /* MONITOR/MWAIT enabled in Ring 3 */ #define HWCAP2_RING3MWAIT (1 << 0) +/* Kernel allows FSGSBASE instructions available in Ring 3 */ +#define HWCAP2_FSGSBASE BIT(1) + #endif diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c index 0339bb3..5e6b46c 100644 --- a/arch/x86/kernel/cpu/common.c +++ b/arch/x86/kernel/cpu/common.c @@ -1303,8 +1303,10 @@ static void identify_cpu(struct cpuinfo_x86 *c) setup_umip(c); /* Enable FSGSBASE instructions if available. */ - if (cpu_has(c, X86_FEATURE_FSGSBASE)) + if (cpu_has(c, X86_FEATURE_FSGSBASE)) { cr4_set_bits(X86_CR4_FSGSBASE); + elf_hwcap2 |= HWCAP2_FSGSBASE; + } /* * The vendor-specific functions might have changed features. -- 2.7.4