Received: by 2002:ac0:8c9a:0:0:0:0:0 with SMTP id r26csp910533ima; Fri, 1 Feb 2019 12:59:39 -0800 (PST) X-Google-Smtp-Source: AHgI3IbFQBV3dAOq+C3mRPLXkIkVgSfFCsgNqthbfUem2KqMFfLnUUwdgujKgTla88IbyJTJTDBP X-Received: by 2002:a63:3e05:: with SMTP id l5mr3477614pga.96.1549054779557; Fri, 01 Feb 2019 12:59:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1549054779; cv=none; d=google.com; s=arc-20160816; b=JP3eWuZYyTnaSEbTLYQfnHC5LtpnaqCewI1IZ1WeGiNjeSwtAIYoUewM17I++HlQuE ealtJgaSjhpYQav7nLahDSGkvOYMSq2f0+wmZ59j6XQoZdMpDlzrsh0zHtN047r6TM/w Bp5DXBq9kaiOiQNJfc1MMArMR7EbBK4hXJp0uXHILtDcu6PK2FxSyDtGm24UWTB+4GrB Ceba0lSpDVNKUi/rqLyKZXKlW4NMRRlUc4O/C/vB26cEaKyV0Aef/UlOMbG/oHV10LQg ACB1+x9fMjKW8NvIVcVMVr3Qh91Vl0hjWm2pwweweLB18gUdh3wAGSB7DPqNlNiX2iKg VMZw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=VzXKYHoJCkmfCjgPnoSKDvCImn1DhUTmRCLvh+elqBU=; b=kyOnVdzG1dGoBUtjekd7yOpkQEBPo/le5j80tZIZQQQtLhLGAj79oOZEXfGzvyY4k6 qU7KaSioqsadxh+y4yRPMoROrZDHFn4bRIPjkuO/c8VGe6hlUIbiBpLCdKqVuKW9oFBM ruOaXTnEqr4XtJ7O0X7iGjRfNZtD2RoH8AZfFskhIN7JePZSrpABsX8wXFXb8Pp171ht 1nj8mqG0Mh49dlOZ+rM8Sa5pI/gcmuugdeERkA/FKoxbXlJv0DDeasHFK7KLdtMOhBwz DF9KIOi0ENPkcmx3WQwNy8MwZUqqIcA56oKYN0XYmxvhr5E8T3fg1fIR7Dy3oTFCnYxR 96GA== 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 o11si8428846pll.160.2019.02.01.12.59.24; Fri, 01 Feb 2019 12:59:39 -0800 (PST) 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 S1730733AbfBAUzH (ORCPT + 99 others); Fri, 1 Feb 2019 15:55:07 -0500 Received: from mga07.intel.com ([134.134.136.100]:49754 "EHLO mga07.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726925AbfBAUzF (ORCPT ); Fri, 1 Feb 2019 15:55:05 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga105.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 01 Feb 2019 12:55:05 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.56,549,1539673200"; d="scan'208";a="140866400" Received: from chang-linux-2.sc.intel.com ([10.3.52.165]) by fmsmga004.fm.intel.com with ESMTP; 01 Feb 2019 12:55:05 -0800 From: "Chang S. Bae" To: Andy Lutomirski , Thomas Gleixner , Ingo Molnar , "H . Peter Anvin" , Andi Kleen Cc: Markus T Metzger , Ravi Shankar , "Chang S . Bae" , LKML Subject: [PATCH v5 12/13] x86/elf: Enumerate kernel FSGSBASE capability in AT_HWCAP2 Date: Fri, 1 Feb 2019 12:53:18 -0800 Message-Id: <20190201205319.15995-14-chang.seok.bae@intel.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190201205319.15995-1-chang.seok.bae@intel.com> References: <20190201205319.15995-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 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. [ chang: Rebase and edit the changelog accordingly. ] Signed-off-by: Andi Kleen Signed-off-by: Chang S. Bae Cc: Andy Lutomirski Cc: H. Peter Anvin 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 6ebaae90e207..c5ce54e749f6 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 3d7d4ca1a29e..3bdac91316c9 100644 --- a/arch/x86/kernel/cpu/common.c +++ b/arch/x86/kernel/cpu/common.c @@ -1369,8 +1369,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.19.1