Received: by 2002:ac0:aa62:0:0:0:0:0 with SMTP id w31-v6csp3815567ima; Tue, 23 Oct 2018 11:44:17 -0700 (PDT) X-Google-Smtp-Source: AJdET5cIuvEya+RLIMJp8WU2ux5138zf90WD35nsjy+6i9XRzKhpld1Ef88MNhWSKKNguhIEpLP8 X-Received: by 2002:a63:f811:: with SMTP id n17mr4978683pgh.23.1540320257886; Tue, 23 Oct 2018 11:44:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1540320257; cv=none; d=google.com; s=arc-20160816; b=g2KHDh/dLB3DtuBtc5/EL4slKffkoQMycubkFE4D2eHj0IhKTnACFHgHwV3Xui6PxH 5BbF35+94CbCoI9KZz0mGQSeais1jHn5UzzF/NfR273Ok8QcfLEREAh1zC3K/qpJhI03 osx4mZDs1olYXND+KfZa7G8OCv+6rJYYp0c/qec5lspoT9eIi9kPt+shMokLNqWMEQYQ tvQRgcoXKtDuTPGJEZeb3e8UrPeFtnj8JebImLZnProIWn8Qm4ppM5PZyhXoa5JmyBRh Y+4mPUX2eCC7WCYB50QM5qfEngAwvRNWSJCBI4xZC5opUQbvQUXupvNVo2sPkeC3eLah 99zg== 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=FlZ6rp5FvuWRuOlc6x0PlKBchBNSe2nQsNQtFetvcFQ=; b=f/p7kzVqXzshX0F1jSbZEVXaGfcmdKzDpDWohwphYcLXw5j0M/3f7S8Sc3ZtNV9bnZ LqRTgYEyfbTL+3Z0B6gL6EPMu/2+aUeCyGzw1WpB9ZI0t/U8TBa57vQl7f/iLvKIq4IP R+8xU33KkZhD2yD5rUHv1kIv6Edpi5jch8hVdAvgk7bzjlVkbZhFNDQutOs323qhHFqF PKR+a153pOi0jiuVlHItLHcYgsyGrzHqozbZFcWiM/loe9q4XUmfTKIES2I3BQaiQdPi sGE+6kCcrfjLd/XBMCiHmnMZJbFAOEb4QV9uXiOVGaGKXspEqXRdoKMgmCcWhnNCGMoB NPYA== 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 b12-v6si1960773pls.367.2018.10.23.11.44.02; Tue, 23 Oct 2018 11:44:17 -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 S1728947AbeJXDIE (ORCPT + 99 others); Tue, 23 Oct 2018 23:08:04 -0400 Received: from mga05.intel.com ([192.55.52.43]:55096 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727758AbeJXDIE (ORCPT ); Tue, 23 Oct 2018 23:08:04 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga105.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 23 Oct 2018 11:43:30 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.54,417,1534834800"; d="scan'208";a="243726602" Received: from chang-linux-2.sc.intel.com ([10.3.52.139]) by orsmga004.jf.intel.com with ESMTP; 23 Oct 2018 11:43:29 -0700 From: "Chang S. Bae" To: Ingo Molnar , Thomas Gleixner , Andy Lutomirski , "H . Peter Anvin" Cc: Andi Kleen , Dave Hansen , Markus T Metzger , Ravi Shankar , "Chang S . Bae" , LKML Subject: [v3 11/12] x86/elf: Enumerate kernel FSGSBASE capability in AT_HWCAP2 Date: Tue, 23 Oct 2018 11:42:33 -0700 Message-Id: <20181023184234.14025-12-chang.seok.bae@intel.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181023184234.14025-1-chang.seok.bae@intel.com> References: <20181023184234.14025-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 patch note 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 Cc: Dave Hansen --- 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 f20edc754532..6964dd24082d 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