From mboxrd@z Thu Jan 1 00:00:00 1970 From: Kai Huang Subject: [PATCH 03/10] kvm: vmx: detect presence of host SGX driver Date: Mon, 8 May 2017 17:24:26 +1200 Message-ID: <20170508052434.3627-4-kai.huang@linux.intel.com> References: <20170508052434.3627-1-kai.huang@linux.intel.com> To: pbonzini@redhat.com, rkrcmar@redhat.com, kvm@vger.kernel.org Return-path: Received: from mail-pg0-f65.google.com ([74.125.83.65]:36111 "EHLO mail-pg0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751179AbdEHFY5 (ORCPT ); Mon, 8 May 2017 01:24:57 -0400 Received: by mail-pg0-f65.google.com with SMTP id 64so1420170pgb.3 for ; Sun, 07 May 2017 22:24:57 -0700 (PDT) In-Reply-To: <20170508052434.3627-1-kai.huang@linux.intel.com> Sender: kvm-owner@vger.kernel.org List-ID: At host side there's SGX driver which serves host SGX applications. It detects SGX feature and manages all EPC pages. KVM needs to co-work with SGX driver in terms of EPC management, because they are both EPC consumers. We should go for 'unified model', in which SGX driver manages all EPC pages and KVM simply calls driver's APIs to allocate/free EPC page, etc. However KVM cannot call driver's APIs directly, as on machines without SGX feature, SGX driver won't be loaded, and calling driver's APIs directly will make KVM unable to be loaded either. Instead, KVM uses symbol_get to get driver's APIs at runtime thus avoids this issue. This patch adds new functions to initialize and destroy KVM SGX support, where currently KVM simply calls symbol_get{put} for all necessary driver's APIs. The symbols will only be released when KVM exits to prevent SGX driver being unloaded during KVM's lifetime. Note KVM compeletely trusts SGX driver in SGX feature and EPC resource detection and won't detect SGX by itself. Two new files arch/x86/kvm/sgx.c{h} are added for holding bulk of KVM SGX code. Signed-off-by: Kai Huang --- arch/x86/kvm/Makefile | 2 +- arch/x86/kvm/sgx.c | 163 ++++++++++++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/sgx.h | 34 +++++++++++ arch/x86/kvm/vmx.c | 10 ++++ 4 files changed, 208 insertions(+), 1 deletion(-) create mode 100644 arch/x86/kvm/sgx.c create mode 100644 arch/x86/kvm/sgx.h diff --git a/arch/x86/kvm/Makefile b/arch/x86/kvm/Makefile index 3bff20710471..015712e666fd 100644 --- a/arch/x86/kvm/Makefile +++ b/arch/x86/kvm/Makefile @@ -17,7 +17,7 @@ kvm-y += x86.o mmu.o emulate.o i8259.o irq.o lapic.o \ kvm-$(CONFIG_KVM_DEVICE_ASSIGNMENT) += assigned-dev.o iommu.o -kvm-intel-y += vmx.o pmu_intel.o +kvm-intel-y += vmx.o pmu_intel.o sgx.o kvm-amd-y += svm.o pmu_amd.o obj-$(CONFIG_KVM) += kvm.o diff --git a/arch/x86/kvm/sgx.c b/arch/x86/kvm/sgx.c new file mode 100644 index 000000000000..4b65b1bb1f30 --- /dev/null +++ b/arch/x86/kvm/sgx.c @@ -0,0 +1,163 @@ +/* + * KVM SGX Virtualization support. + * + * Copyright (c) 2015, Intel Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place - Suite 330, Boston, MA 02111-1307 USA. + * + * Author: Kai Huang + */ + +#include +#include /* boot_cpu_has */ +#include /* cpuid */ +#include +#include +#include "sgx.h" + +/* Debug helpers... */ +#define sgx_debug(fmt, ...) \ + printk(KERN_DEBUG "KVM: SGX: %s: "fmt, __func__, ## __VA_ARGS__) +#define sgx_info(fmt, ...) \ + printk(KERN_INFO "KVM: SGX: "fmt, ## __VA_ARGS__) +#define sgx_err(fmt, ...) \ + printk(KERN_ERR "KVM: SGX: "fmt, ## __VA_ARGS__) + +/* + * EPC pages are managed by SGX driver. KVM needs to call SGX driver's APIs + * to allocate/free EPC page, etc. + * + * However KVM cannot call SGX driver's APIs directly. As on machine without + * SGX support, SGX driver cannot be loaded, therefore if KVM calls driver's + * APIs directly, KVM won't be able to be loaded either, which is not + * acceptable. Instead, KVM uses symbol_get{put} pair to get driver's APIs + * at runtime and simply disable SGX if those symbols cannot be found. + */ +struct required_sgx_driver_symbols { + struct sgx_epc_page *(*alloc_epc_page)(unsigned int flags); + /* + * Currently SGX driver's sgx_free_page has 'struct sgx_encl *encl' + * as parameter. We need to honor that. + */ + int (*free_epc_page)(struct sgx_epc_page *epg, struct sgx_encl *encl); + /* + * get/put (map/unmap) kernel virtual address of given EPC page. + * The namings are aligned to SGX driver's APIs. + */ + void *(*get_epc_page)(struct sgx_epc_page *epg); + void (*put_epc_page)(void *epc_page_vaddr); +}; + +static struct required_sgx_driver_symbols sgx_driver_symbols = { + .alloc_epc_page = NULL, + .free_epc_page = NULL, + .get_epc_page = NULL, + .put_epc_page = NULL, +}; + +static inline struct sgx_epc_page *sgx_alloc_epc_page(unsigned int flags) +{ + struct sgx_epc_page *epg; + + BUG_ON(!sgx_driver_symbols.alloc_epc_page); + + epg = sgx_driver_symbols.alloc_epc_page(flags); + + /* sgx_alloc_page returns ERR_PTR(error_code) instead of NULL */ + return IS_ERR_OR_NULL(epg) ? NULL : epg; +} + +static inline void sgx_free_epc_page(struct sgx_epc_page *epg) +{ + BUG_ON(!sgx_driver_symbols.free_epc_page); + + sgx_driver_symbols.free_epc_page(epg, NULL); +} + +static inline void *sgx_kmap_epc_page(struct sgx_epc_page *epg) +{ + BUG_ON(!sgx_driver_symbols.get_epc_page); + + return sgx_driver_symbols.get_epc_page(epg); +} + +static inline void sgx_kunmap_epc_page(void *addr) +{ + BUG_ON(!sgx_driver_symbols.put_epc_page); + + sgx_driver_symbols.put_epc_page(addr); +} + +static inline u64 sgx_epc_page_to_pfn(struct sgx_epc_page *epg) +{ + return (u64)(epg->pa >> PAGE_SHIFT); +} + +static void put_sgx_driver_symbols(void); + +static int get_sgx_driver_symbols(void) +{ + sgx_driver_symbols.alloc_epc_page = symbol_get(sgx_alloc_page); + if (!sgx_driver_symbols.alloc_epc_page) + goto error; + sgx_driver_symbols.free_epc_page = symbol_get(sgx_free_page); + if (!sgx_driver_symbols.free_epc_page) + goto error; + sgx_driver_symbols.get_epc_page = symbol_get(sgx_get_page); + if (!sgx_driver_symbols.get_epc_page) + goto error; + sgx_driver_symbols.put_epc_page = symbol_get(sgx_put_page); + if (!sgx_driver_symbols.put_epc_page) + goto error; + + return 0; + +error: + put_sgx_driver_symbols(); + return -EFAULT; +} + +static void put_sgx_driver_symbols(void) +{ + if (sgx_driver_symbols.alloc_epc_page) + symbol_put(sgx_alloc_page); + if (sgx_driver_symbols.free_epc_page) + symbol_put(sgx_free_page); + if (sgx_driver_symbols.get_epc_page) + symbol_put(sgx_get_page); + if (sgx_driver_symbols.put_epc_page) + symbol_put(sgx_put_page); + + memset(&sgx_driver_symbols, 0, sizeof (sgx_driver_symbols)); +} + +int sgx_init(void) +{ + int r; + + r = get_sgx_driver_symbols(); + if (r) { + sgx_err("SGX driver is not loaded.\n"); + return r; + } + + sgx_info("SGX virtualization supported.\n"); + + return 0; +} + +void sgx_destroy(void) +{ + put_sgx_driver_symbols(); +} diff --git a/arch/x86/kvm/sgx.h b/arch/x86/kvm/sgx.h new file mode 100644 index 000000000000..ff2766eeae33 --- /dev/null +++ b/arch/x86/kvm/sgx.h @@ -0,0 +1,34 @@ +/* + * KVM SGX Virtualization support. + * + * Copyright (c) 2015, Intel Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place - Suite 330, Boston, MA 02111-1307 USA. + * + * Author: Kai Huang + */ + +#ifndef ARCH_X86_KVM_SGX_H +#define ARCH_X86_KVM_SGX_H + +#include +#include +#include +#include +#include + +int sgx_init(void); +void sgx_destroy(void); + +#endif diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c index 050a143414e1..4b368a0af9bd 100644 --- a/arch/x86/kvm/vmx.c +++ b/arch/x86/kvm/vmx.c @@ -52,6 +52,8 @@ #include "trace.h" #include "pmu.h" +#include "sgx.h" + #define __ex(x) __kvm_handle_fault_on_reboot(x) #define __ex_clear(x, reg) \ ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg) @@ -11657,6 +11659,11 @@ static int __init vmx_init(void) if (r) return r; + if (enable_sgx) { + if (sgx_init()) + enable_sgx = 0; + } + #ifdef CONFIG_KEXEC_CORE rcu_assign_pointer(crash_vmclear_loaded_vmcss, crash_vmclear_local_loaded_vmcss); @@ -11672,6 +11679,9 @@ static void __exit vmx_exit(void) synchronize_rcu(); #endif + if (enable_sgx) + sgx_destroy(); + kvm_exit(); } -- 2.11.0