Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp719728img; Wed, 20 Mar 2019 09:25:35 -0700 (PDT) X-Google-Smtp-Source: APXvYqyqBxJODhX2o88E7NNcjmDXMpY1k1v+rm3Brba41bdxqHvEj6TghZBCHh80tGkf9KEzp95b X-Received: by 2002:a17:902:b413:: with SMTP id x19mr8946273plr.256.1553099135301; Wed, 20 Mar 2019 09:25:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553099135; cv=none; d=google.com; s=arc-20160816; b=1KDqxx2KwumqbR4p9exWjffBD8JAZVD+1EIQ+4NvQK3D6wD2UBcBGCL7V4NKZNpV7T pKDcF+sFJ5E/7Q6ssqvEEQY1V8U3BHcZPgOcr32IgEER2yzXXRCusA9Fhu+o+K/T7xUJ MJWX6bQrRZvmIx0bFjoakOdsOvUr2fhiPUZu2QYruhohDeUeGfvQzEFlxYqG4/NfNRX8 /+aAv1kTqxjTQiYV+QqSeTnO5WcadFqoO+aiE1EicktAn96eNZo5HU2PHWftOgHpBjS/ /98i42MIKHOFG/r0UrGFliNNDVVfiBTbE/R8XQcXUdyt3h4RCAwWDxm5NJgWZpkkL4nS DC7Q== 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=9WQCHrDN41mx36gxIHtZks63fQxFvPWFd0248t64goo=; b=M8Ekb3EByG/TAHiYqhh8TzDwdDdujdzmSwgb8ZLTfaxF+8MejwksRBYu+H3uo0TTEb /hecPYMOlCJm1vULS2g2PrQ1+gDygSZr0Q1wJDUEgnsjP/M+6s3HVZPx+oC9eh6oKRfR deyabIymZnjVNl6esEVFYfVopSM870dkXHZ6pfjjJl32ysFy/7w7JpJfZI2cYiHPahI8 ze0QFiyGhFwVzJuwepb9F540MX4A5+h6NI5hyW8KvwMSS1qgWKGKmnUOccGhmjqdyH6A uiC+TLoORufnLLyLNPEjWHY48MOIO9Zkn+fkHKSSAVRCqHfo5wkEYR+Dka5tSFYWBPLk O1ow== 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 p24si782149pfd.288.2019.03.20.09.25.20; Wed, 20 Mar 2019 09:25:35 -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 S1727660AbfCTQXA (ORCPT + 99 others); Wed, 20 Mar 2019 12:23:00 -0400 Received: from mga06.intel.com ([134.134.136.31]:62517 "EHLO mga06.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726295AbfCTQXA (ORCPT ); Wed, 20 Mar 2019 12:23:00 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga003.jf.intel.com ([10.7.209.27]) by orsmga104.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 20 Mar 2019 09:22:59 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.60,249,1549958400"; d="scan'208";a="135714849" Received: from sorenthe-mobl1.ger.corp.intel.com (HELO localhost) ([10.249.254.203]) by orsmga003.jf.intel.com with ESMTP; 20 Mar 2019 09:22:50 -0700 From: Jarkko Sakkinen To: linux-kernel@vger.kernel.org, x86@kernel.org, linux-sgx@vger.kernel.org Cc: akpm@linux-foundation.org, dave.hansen@intel.com, sean.j.christopherson@intel.com, nhorman@redhat.com, npmccallum@redhat.com, serge.ayoun@intel.com, shay.katz-zamir@intel.com, haitao.huang@intel.com, andriy.shevchenko@linux.intel.com, tglx@linutronix.de, kai.svahn@intel.com, bp@alien8.de, josh@joshtriplett.org, luto@kernel.org, kai.huang@intel.com, rientjes@google.com, Jarkko Sakkinen Subject: [PATCH v19,RESEND 08/27] x86/cpu/intel: Detect SGX support and update caps appropriately Date: Wed, 20 Mar 2019 18:21:00 +0200 Message-Id: <20190320162119.4469-9-jarkko.sakkinen@linux.intel.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190320162119.4469-1-jarkko.sakkinen@linux.intel.com> References: <20190320162119.4469-1-jarkko.sakkinen@linux.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: Sean Christopherson Similar to other large Intel features such as VMX and TXT, SGX must be explicitly enabled in IA32_FEATURE_CONTROL MSR to be truly usable. Clear all SGX related capabilities if SGX is not fully enabled in IA32_FEATURE_CONTROL or if the SGX1 instruction set isn't supported (impossible on bare metal, theoretically possible in a VM if the VMM is doing something weird). Like SGX itself, SGX Launch Control must be explicitly enabled via a flag in IA32_FEATURE_CONTROL. Clear the SGX_LC capability if Launch Control is not fully enabled (or obviously if SGX itself is disabled). Note that clearing X86_FEATURE_SGX_LC creates a bit of a conundrum regarding the SGXLEPUBKEYHASH MSRs, as it may be desirable to read the MSRs even if they are not writable, e.g. to query the configured key, but clearing the capability leaves no breadcrum for discerning whether or not the MSRs exist. But, such usage will be rare (KVM is the only known case at this time) and not performance critical, so it's not unreasonable to require the use of rdmsr_safe(). Clearing the cap bit eliminates the need for an additional flag to track whether or not Launch Control is truly enabled, which is what we care about the vast majority of the time. Signed-off-by: Sean Christopherson Co-developed-by: Jarkko Sakkinen Signed-off-by: Jarkko Sakkinen --- arch/x86/kernel/cpu/intel.c | 39 +++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/arch/x86/kernel/cpu/intel.c b/arch/x86/kernel/cpu/intel.c index fc3c07fe7df5..702497f34a96 100644 --- a/arch/x86/kernel/cpu/intel.c +++ b/arch/x86/kernel/cpu/intel.c @@ -596,6 +596,42 @@ static void detect_tme(struct cpuinfo_x86 *c) c->x86_phys_bits -= keyid_bits; } +static void __maybe_unused detect_sgx(struct cpuinfo_x86 *c) +{ + unsigned long long fc; + + rdmsrl(MSR_IA32_FEATURE_CONTROL, fc); + if (!(fc & FEATURE_CONTROL_LOCKED)) { + pr_err_once("sgx: The feature control MSR is not locked\n"); + goto err_unsupported; + } + + if (!(fc & FEATURE_CONTROL_SGX_ENABLE)) { + pr_err_once("sgx: SGX is not enabled in IA32_FEATURE_CONTROL MSR\n"); + goto err_unsupported; + } + + if (!cpu_has(c, X86_FEATURE_SGX1)) { + pr_err_once("sgx: SGX1 instruction set is not supported\n"); + goto err_unsupported; + } + + if (!(fc & FEATURE_CONTROL_SGX_LE_WR)) { + pr_info_once("sgx: The launch control MSRs are not writable\n"); + goto err_msrs_rdonly; + } + + return; + +err_unsupported: + setup_clear_cpu_cap(X86_FEATURE_SGX); + setup_clear_cpu_cap(X86_FEATURE_SGX1); + setup_clear_cpu_cap(X86_FEATURE_SGX2); + +err_msrs_rdonly: + setup_clear_cpu_cap(X86_FEATURE_SGX_LC); +} + static void init_intel_energy_perf(struct cpuinfo_x86 *c) { u64 epb; @@ -763,6 +799,9 @@ static void init_intel(struct cpuinfo_x86 *c) if (cpu_has(c, X86_FEATURE_TME)) detect_tme(c); + if (IS_ENABLED(CONFIG_INTEL_SGX) && cpu_has(c, X86_FEATURE_SGX)) + detect_sgx(c); + init_intel_energy_perf(c); init_intel_misc_features(c); -- 2.19.1