Received: by 2002:ac0:a581:0:0:0:0:0 with SMTP id m1-v6csp1454116imm; Tue, 3 Jul 2018 11:24:25 -0700 (PDT) X-Google-Smtp-Source: ADUXVKKYOL/BF8+zwLVi1OLtWbI5z2Q6HuxRluQ/1q3rluaCBbbnC+m6iip1c1lEN4VKlrlmI24S X-Received: by 2002:a17:902:2d24:: with SMTP id o33-v6mr31377821plb.14.1530642265122; Tue, 03 Jul 2018 11:24:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530642265; cv=none; d=google.com; s=arc-20160816; b=z380WyM8vA+aiHBpHcK8ONb3vo1o8+iFenehf68IY5E07PCKmnwbH9F3WqNoce2Gxj +YF1lNu1+TJ5Iz3spTcD32qc8ZCoQ84CpjPzYO/IwscFwPiCVNC+LchJGi1yS8kkS3er L5cCOoQflPmPV/ZmFwLe1lIX7en/sCHROMVvS3VoqaLjaFyveAX+rKp6kelpcEx3y3io 6hF4Mo2pesv/580OcriUW0SGrzgeCAhHzJDLtyJcSmgEdgGdSZCl3OsmZQ958PGoyl+A YrUO34+SLkcOnrt7pY7qbLDpEQjVC6UXFIq4IuXHWvXpnJ9d1iaFED/vcPBXPybdK+4p PukA== 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=5juTdqnI/ICNOTNtb+WXAoGSW+h9NDDnjPkrsKHUrFI=; b=mj1QrO/5532jEDLsb6cwVY7f8l2u9EalmGq3Wy2mLYdB1GHMDeo4ZgGy/EtbUwRlT/ 1aKRoNtrqoAzhwTfjl1scg4MQlelMF+EIyeHbb+mFoSSF6OlnvCMmschAtoFO48LCByS W7hJMkQGO3/dR2/2LB2xM+z6sp4sb0nw+mCjRccExmv4xlCaHFWbyMzazoTFh/wI3TJP kOZ13E4DmEqhfPhof5/vcsQ7Ptd6o8kOCvCuzEAPS9d42mn1ffwPxCJvyIrLk9fVssXv eUIyE/DNGMJLZcdZMNbz/tcMDd2uKwHbQjUs/u3DfTJtIm4/S6odeBHLLL6uzvnq0eGA 6J2Q== 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 r1-v6si1856263plj.474.2018.07.03.11.24.11; Tue, 03 Jul 2018 11:24:25 -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 S934632AbeGCSXD (ORCPT + 99 others); Tue, 3 Jul 2018 14:23:03 -0400 Received: from mga17.intel.com ([192.55.52.151]:2653 "EHLO mga17.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934593AbeGCSXC (ORCPT ); Tue, 3 Jul 2018 14:23:02 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga107.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 03 Jul 2018 11:23:01 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.51,304,1526367600"; d="scan'208";a="242708171" Received: from cdikicix-mobl.ger.corp.intel.com (HELO localhost) ([10.249.254.69]) by fmsmga006.fm.intel.com with ESMTP; 03 Jul 2018 11:22:52 -0700 From: Jarkko Sakkinen To: x86@kernel.org, platform-driver-x86@vger.kernel.org Cc: dave.hansen@intel.com, sean.j.christopherson@intel.com, nhorman@redhat.com, npmccallum@redhat.com, linux-sgx@vger.kernel.org, Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , Jarkko Sakkinen , Vikas Shivappa , "Rafael J. Wysocki" , Andi Kleen , "Kirill A. Shutemov" , Greg Kroah-Hartman , linux-kernel@vger.kernel.org (open list:X86 ARCHITECTURE (32-BIT AND 64-BIT)) Subject: [PATCH v12 06/13] x86/sgx: detect Intel SGX Date: Tue, 3 Jul 2018 21:19:51 +0300 Message-Id: <20180703182118.15024-7-jarkko.sakkinen@linux.intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180703182118.15024-1-jarkko.sakkinen@linux.intel.com> References: <20180703182118.15024-1-jarkko.sakkinen@linux.intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sean Christopherson Intel(R) SGX is a set of CPU instructions that can be used by applications to set aside private regions of code and data. The code outside the enclave is disallowed to access the memory inside the enclave by the CPU access control. This commit adds the check for SGX to arch/x86 and a new config option, INTEL_SGX_CORE. Exposes a boolean variable 'sgx_enabled' to query whether or not the SGX support is available. Signed-off-by: Sean Christopherson Co-developed-by: Jarkko Sakkinen Co-developed-by: Suresh Siddha --- arch/x86/Kconfig | 19 ++++++++++++ arch/x86/include/asm/sgx.h | 12 ++++++++ arch/x86/include/asm/sgx_pr.h | 13 ++++++++ arch/x86/kernel/cpu/Makefile | 1 + arch/x86/kernel/cpu/intel_sgx.c | 54 +++++++++++++++++++++++++++++++++ 5 files changed, 99 insertions(+) create mode 100644 arch/x86/include/asm/sgx.h create mode 100644 arch/x86/include/asm/sgx_pr.h create mode 100644 arch/x86/kernel/cpu/intel_sgx.c diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index c07f492b871a..d2c7e7a2b2ac 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -1925,6 +1925,25 @@ config X86_INTEL_MEMORY_PROTECTION_KEYS If unsure, say y. +config INTEL_SGX_CORE + prompt "Intel SGX core functionality" + def_bool n + depends on X86_64 && CPU_SUP_INTEL + help + Intel Software Guard eXtensions (SGX) is a set of CPU instructions + that allows ring 3 applications to create enclaves, private regions + of memory that are protected, by hardware, from unauthorized access + and/or modification. + + This option enables kernel recognition of SGX, high-level management + of the Enclave Page Cache (EPC), tracking and writing of SGX Launch + Enclave Hash MSRs, and allows for virtualization of SGX via KVM. By + iteslf, this option does not provide SGX support to userspace. + + For details, see Documentation/x86/intel_sgx.rst + + If unsure, say N. + config EFI bool "EFI runtime service support" depends on ACPI diff --git a/arch/x86/include/asm/sgx.h b/arch/x86/include/asm/sgx.h new file mode 100644 index 000000000000..2130e639ab49 --- /dev/null +++ b/arch/x86/include/asm/sgx.h @@ -0,0 +1,12 @@ +// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) +// Copyright(c) 2016-18 Intel Corporation. + +#ifndef _ASM_X86_SGX_H +#define _ASM_X86_SGX_H + +#include + +extern bool sgx_enabled; +extern bool sgx_lc_enabled; + +#endif /* _ASM_X86_SGX_H */ diff --git a/arch/x86/include/asm/sgx_pr.h b/arch/x86/include/asm/sgx_pr.h new file mode 100644 index 000000000000..42a4185d4bc9 --- /dev/null +++ b/arch/x86/include/asm/sgx_pr.h @@ -0,0 +1,13 @@ +// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) +// Copyright(c) 2016-17 Intel Corporation. + +#ifndef _ASM_X86_SGX_PR_H +#define _ASM_X86_SGX_PR_H + +#include +#include + +#undef pr_fmt +#define pr_fmt(fmt) "intel_sgx: " fmt + +#endif /* _ASM_X86_SGX_PR_H */ diff --git a/arch/x86/kernel/cpu/Makefile b/arch/x86/kernel/cpu/Makefile index a66229f51b12..9552ff5b4ec3 100644 --- a/arch/x86/kernel/cpu/Makefile +++ b/arch/x86/kernel/cpu/Makefile @@ -36,6 +36,7 @@ obj-$(CONFIG_CPU_SUP_TRANSMETA_32) += transmeta.o obj-$(CONFIG_CPU_SUP_UMC_32) += umc.o obj-$(CONFIG_INTEL_RDT) += intel_rdt.o intel_rdt_rdtgroup.o intel_rdt_monitor.o intel_rdt_ctrlmondata.o +obj-$(CONFIG_INTEL_SGX_CORE) += intel_sgx.o obj-$(CONFIG_X86_MCE) += mcheck/ obj-$(CONFIG_MTRR) += mtrr/ diff --git a/arch/x86/kernel/cpu/intel_sgx.c b/arch/x86/kernel/cpu/intel_sgx.c new file mode 100644 index 000000000000..77d94115c4cf --- /dev/null +++ b/arch/x86/kernel/cpu/intel_sgx.c @@ -0,0 +1,54 @@ +// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) +// Copyright(c) 2016-17 Intel Corporation. + +#include +#include +#include +#include +#include +#include +#include +#include + +bool sgx_enabled __ro_after_init; +EXPORT_SYMBOL(sgx_enabled); +bool sgx_lc_enabled __ro_after_init; +EXPORT_SYMBOL(sgx_lc_enabled); + +static __init bool sgx_is_enabled(bool *lc_enabled) +{ + unsigned long fc; + + if (!boot_cpu_has(X86_FEATURE_SGX)) + return false; + + if (!boot_cpu_has(X86_FEATURE_SGX1)) + return false; + + rdmsrl(MSR_IA32_FEATURE_CONTROL, fc); + if (!(fc & FEATURE_CONTROL_LOCKED)) { + pr_info("IA32_FEATURE_CONTROL MSR is not locked\n"); + return false; + } + + if (!(fc & FEATURE_CONTROL_SGX_ENABLE)) { + pr_info("disabled by the firmware\n"); + return false; + } + + if (!(fc & FEATURE_CONTROL_SGX_LE_WR)) { + pr_info("IA32_SGXLEPUBKEYHASHn MSRs are not writable\n"); + return false; + } + + *lc_enabled = !!(fc & FEATURE_CONTROL_SGX_LE_WR); + return true; +} + +static __init int sgx_init(void) +{ + sgx_enabled = sgx_is_enabled(&sgx_lc_enabled); + return 0; +} + +arch_initcall(sgx_init); -- 2.17.1