Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp549572ybk; Sat, 9 May 2020 10:39:27 -0700 (PDT) X-Google-Smtp-Source: APiQypKFNq5zNu7Nt/4rWIFNoAK/lt3dspghBe2KoWhNihglJsviq6JVrj9Op58y3oO6mY65Z76s X-Received: by 2002:aa7:d78a:: with SMTP id s10mr7238204edq.319.1589045967030; Sat, 09 May 2020 10:39:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589045967; cv=none; d=google.com; s=arc-20160816; b=LdLvqj3RobS3wp7B4TyWV/A9H7N4RVvkx+x06sDzCmlcW+6IjVHi5jWvMCgo6ktXd0 VXyZoNTNTBmrzftrGIno/gFxDi6sGh0IaI4yDeA/PqbAa3HM1toB/zwKL+yyHpAPU3hQ yWxZ7zOSFv5hlVVnpX8xEsCQq4iVFi+g+OlV0MmGqUNqQEwR6n1VPaNtHRIQvRRhTg9a 4iR9PiUUlOTjXTmA3QUOVvbJO/WaM7J+bjK6UzCgLk31dSbYNsPyZCaaurMboy+FeTCx qmaOCF5FygbHc4x2wIgCb0zh97MNqeXMwI5a9ozcKV069jFdAXoH4G0jVDr1IRA8M40w cMiQ== 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 :dkim-signature; bh=7slurwkWKop/eRO35/8+Oxb0QPhPEJnq7MA2ReJ9TaI=; b=d+0IQNeGV736eppIZtOAxZYpIyaCmguF2hmDQBF+Jk9AKc9TQyKV25izJv1x0E39vl gWUe3vhxKytMrfMg50eMi6Rdx/hUv3diK0VgF2+P4v6jZxZwykFseoCkGDLDrxarHF61 mLS9gNx1x47CyKm+59NRmUC7kWBCSkWAUWhVbRC7yGJ1QuDZtLaftq3OKnC7BNwyLr9y +OCLKeXbl36CwdsxESv55E2CnAxXOiSfiT4aw2zSe3SpQaz9OXIiAVIodeFwZED0UbAA gKR2GEuSNIh3oQkqMjAMiqYgV3NzXki5Si56Ju6LbMuPqCx4Si8vosSsgnsn6Jmn1PbW Uosw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=YQQQS8zv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d8si2842878edp.582.2020.05.09.10.39.04; Sat, 09 May 2020 10:39:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=YQQQS8zv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728700AbgEIRh3 (ORCPT + 99 others); Sat, 9 May 2020 13:37:29 -0400 Received: from mail.kernel.org ([198.145.29.99]:54752 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728653AbgEIRhY (ORCPT ); Sat, 9 May 2020 13:37:24 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 75C0624962; Sat, 9 May 2020 17:37:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1589045843; bh=Uf/dQt3pOb42ZMkyeOF+KZniQCmaCDN+4LcrOHEj2qc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YQQQS8zvJAf3c/LtIeM/oIA22GOlVtL47sfxIKrm7drpkzqapU4BX9vxsaFoxmcoy RC5Z3+xvQPvHNm2AppQ4T39RKp+36IF9UjXhMLRpOXrfRn0t3hBPQOVszqPx4NBDgP KunP9Ujbm0pPUQjv2HiP+RZYV0rn+RziyKFlNsqQ= From: Sasha Levin To: linux-kernel@vger.kernel.org, tglx@linutronix.de, bp@alien8.de, luto@kernel.org Cc: hpa@zytor.com, dave.hansen@intel.com, tony.luck@intel.com, ak@linux.intel.com, ravi.v.shankar@intel.com, chang.seok.bae@intel.com, Sasha Levin Subject: [PATCH v11 17/18] x86/elf: Enumerate kernel FSGSBASE capability in AT_HWCAP2 Date: Sat, 9 May 2020 13:36:54 -0400 Message-Id: <20200509173655.13977-18-sashal@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200509173655.13977-1-sashal@kernel.org> References: <20200509173655.13977-1-sashal@kernel.org> 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. Enumerate the enabled FSGSBASE capability in bit 1 of AT_HWCAP2 in the ELF aux vector. AT_HWCAP2 is already used by PPC for similar purposes. The application can access it open coded or by using the getauxval() function in newer versions of glibc. Signed-off-by: Andi Kleen Signed-off-by: Chang S. Bae Signed-off-by: Sasha Levin Reviewed-by: Tony Luck Cc: Thomas Gleixner Cc: Borislav Petkov Cc: Andy Lutomirski Cc: H. Peter Anvin Cc: Dave Hansen Cc: Tony Luck Cc: Andi Kleen --- 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 8b2effe6efb82..5fdfcb47000f9 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 0d480cbadc7dc..b5a086ea34258 100644 --- a/arch/x86/kernel/cpu/common.c +++ b/arch/x86/kernel/cpu/common.c @@ -1495,8 +1495,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.20.1