On Fri, May 15, 2020 at 03:43:57AM +0300, Jarkko Sakkinen wrote: > +config INTEL_SGX > + bool "Intel SGX" > + depends on X86_64 && CPU_SUP_INTEL > + select SRCU > + select MMU_NOTIFIER > + help > + Intel(R) SGX is a set of CPU instructions that can be used by > + applications to set aside private regions of code and data, referred > + to as enclaves. An enclave's private memory can only be accessed by > + code running within the enclave. Accesses from outside the enclave, > + including other enclaves, are disallowed by hardware. > + > + If unsure, say N. > + Enabling this gives: In file included from arch/x86/kernel/cpu/sgx/main.c:11: arch/x86/kernel/cpu/sgx/encls.h:189:51: warning: ‘struct sgx_einittoken’ declared inside parameter list will not be visible outside of this definition or declaration 189 | static inline int __einit(void *sigstruct, struct sgx_einittoken *einittoken, | ^~~~~~~~~~~~~~ In file included from arch/x86/kernel/cpu/sgx/reclaim.c:12: arch/x86/kernel/cpu/sgx/encls.h:189:51: warning: ‘struct sgx_einittoken’ declared inside parameter list will not be visible outside of this definition or declaration 189 | static inline int __einit(void *sigstruct, struct sgx_einittoken *einittoken, | You need a forward declaration somewhere. > config EFI > bool "EFI runtime service support" > depends on ACPI > diff --git a/arch/x86/kernel/cpu/Makefile b/arch/x86/kernel/cpu/Makefile > index 7dc4ad68eb41..45534fb81007 100644 > --- a/arch/x86/kernel/cpu/Makefile > +++ b/arch/x86/kernel/cpu/Makefile > @@ -46,6 +46,7 @@ obj-$(CONFIG_X86_MCE) += mce/ > obj-$(CONFIG_MTRR) += mtrr/ > obj-$(CONFIG_MICROCODE) += microcode/ > obj-$(CONFIG_X86_CPU_RESCTRL) += resctrl/ > +obj-$(CONFIG_INTEL_SGX) += sgx/ > > obj-$(CONFIG_X86_LOCAL_APIC) += perfctr-watchdog.o > > diff --git a/arch/x86/kernel/cpu/sgx/Makefile b/arch/x86/kernel/cpu/sgx/Makefile > new file mode 100644 > index 000000000000..2dec75916a5e > --- /dev/null > +++ b/arch/x86/kernel/cpu/sgx/Makefile > @@ -0,0 +1,3 @@ > +obj-y += \ > + main.o \ > + reclaim.o > diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c > new file mode 100644 > index 000000000000..38424c1e8341 > --- /dev/null > +++ b/arch/x86/kernel/cpu/sgx/main.c > @@ -0,0 +1,151 @@ > +// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) > +// Copyright(c) 2016-17 Intel Corporation. > + > +#include <linux/freezer.h> > +#include <linux/highmem.h> > +#include <linux/kthread.h> > +#include <linux/pagemap.h> > +#include <linux/ratelimit.h> > +#include <linux/sched/signal.h> > +#include <linux/slab.h> > +#include "encls.h" > + > +struct sgx_epc_section sgx_epc_sections[SGX_MAX_EPC_SECTIONS]; > +int sgx_nr_epc_sections; We have become very averse against global stuff. What is going to use those, only sgx code I assume...? > +static bool __init sgx_page_cache_init(void) > +{ > + u32 eax, ebx, ecx, edx, type; > + u64 pa, size; > + int i; > + > + for (i = 0; i <= ARRAY_SIZE(sgx_epc_sections); i++) { > + cpuid_count(SGX_CPUID, i + SGX_CPUID_FIRST_VARIABLE_SUB_LEAF, > + &eax, &ebx, &ecx, &edx); > + > + type = eax & SGX_CPUID_SUB_LEAF_TYPE_MASK; > + if (type == SGX_CPUID_SUB_LEAF_INVALID) > + break; > + > + if (type != SGX_CPUID_SUB_LEAF_EPC_SECTION) { > + pr_err_once("Unknown EPC section type: %u\n", type); > + break; > + } > + > + if (i == ARRAY_SIZE(sgx_epc_sections)) { > + pr_warn("No free slot for an EPC section\n"); > + break; > + } This is also the loop termination: do we really need this warn or can the loop simply do "i < ARRAY_SIZE" ? If the warn is needed, it can be after the loop too. > + > + pa = sgx_calc_section_metric(eax, ebx); > + size = sgx_calc_section_metric(ecx, edx); > + > + pr_info("EPC section 0x%llx-0x%llx\n", pa, pa + size - 1); I'm assuming that's useful information to issue in dmesg? > diff --git a/arch/x86/kernel/cpu/sgx/reclaim.c b/arch/x86/kernel/cpu/sgx/reclaim.c > new file mode 100644 > index 000000000000..215371588a25 > --- /dev/null > +++ b/arch/x86/kernel/cpu/sgx/reclaim.c > @@ -0,0 +1,82 @@ > +// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) > +// Copyright(c) 2016-19 Intel Corporation. > + > +#include <linux/freezer.h> > +#include <linux/highmem.h> > +#include <linux/kthread.h> > +#include <linux/pagemap.h> > +#include <linux/ratelimit.h> > +#include <linux/slab.h> > +#include <linux/sched/mm.h> > +#include <linux/sched/signal.h> > +#include "encls.h" > + > +struct task_struct *ksgxswapd_tsk; Same for this one: also shared only among sgx code? > +/** > + * enum sgx_epc_page_desc - bits and masks for an EPC page's descriptor > + * %SGX_EPC_SECTION_MASK: SGX allows to have multiple EPC sections in the > + * physical memory. The existing and near-future > + * hardware defines at most eight sections, hence > + * three bits to hold a section. > + */ > +enum sgx_epc_page_desc { > + SGX_EPC_SECTION_MASK = GENMASK_ULL(3, 0), If that should be three bits, then it should be (2, 0). Because now you have 4 bits: # arch/x86/kernel/cpu/sgx/sgx.h:56: return &sgx_epc_sections[page->desc & SGX_EPC_SECTION_MASK]; andl $15, %edx #, _22 ^^^ > + /* bits 12-63 are reserved for the physical page address of the page */ > +}; > + > +#define SGX_MAX_EPC_SECTIONS (SGX_EPC_SECTION_MASK + 1) > + > +extern struct sgx_epc_section sgx_epc_sections[SGX_MAX_EPC_SECTIONS]; > + > +static inline struct sgx_epc_section *sgx_epc_section(struct sgx_epc_page *page) This function name needs a verb. "get" sounds fitting. :) > +{ > + return &sgx_epc_sections[page->desc & SGX_EPC_SECTION_MASK]; > +} > + > +static inline void *sgx_epc_addr(struct sgx_epc_page *page) Ditto. -- Regards/Gruss, Boris. https://people.kernel.org/tglx/notes-about-netiquette