0001
0002
0003
0004
0005
0006
0007 #ifndef _X86_ENCL_H
0008 #define _X86_ENCL_H
0009
0010 #include <linux/cpumask.h>
0011 #include <linux/kref.h>
0012 #include <linux/list.h>
0013 #include <linux/mm_types.h>
0014 #include <linux/mmu_notifier.h>
0015 #include <linux/mutex.h>
0016 #include <linux/notifier.h>
0017 #include <linux/srcu.h>
0018 #include <linux/workqueue.h>
0019 #include <linux/xarray.h>
0020 #include "sgx.h"
0021
0022
0023 #define SGX_ENCL_PAGE_VA_OFFSET_MASK GENMASK_ULL(11, 3)
0024
0025
0026 #define SGX_ENCL_PAGE_BEING_RECLAIMED BIT(3)
0027
0028 struct sgx_encl_page {
0029 unsigned long desc;
0030 unsigned long vm_max_prot_bits:8;
0031 enum sgx_page_type type:16;
0032 struct sgx_epc_page *epc_page;
0033 struct sgx_encl *encl;
0034 struct sgx_va_page *va_page;
0035 };
0036
0037 enum sgx_encl_flags {
0038 SGX_ENCL_IOCTL = BIT(0),
0039 SGX_ENCL_DEBUG = BIT(1),
0040 SGX_ENCL_CREATED = BIT(2),
0041 SGX_ENCL_INITIALIZED = BIT(3),
0042 };
0043
0044 struct sgx_encl_mm {
0045 struct sgx_encl *encl;
0046 struct mm_struct *mm;
0047 struct list_head list;
0048 struct mmu_notifier mmu_notifier;
0049 };
0050
0051 struct sgx_encl {
0052 unsigned long base;
0053 unsigned long size;
0054 unsigned long flags;
0055 unsigned int page_cnt;
0056 unsigned int secs_child_cnt;
0057 struct mutex lock;
0058 struct xarray page_array;
0059 struct sgx_encl_page secs;
0060 unsigned long attributes;
0061 unsigned long attributes_mask;
0062
0063 cpumask_t cpumask;
0064 struct file *backing;
0065 struct kref refcount;
0066 struct list_head va_pages;
0067 unsigned long mm_list_version;
0068 struct list_head mm_list;
0069 spinlock_t mm_lock;
0070 struct srcu_struct srcu;
0071 };
0072
0073 #define SGX_VA_SLOT_COUNT 512
0074
0075 struct sgx_va_page {
0076 struct sgx_epc_page *epc_page;
0077 DECLARE_BITMAP(slots, SGX_VA_SLOT_COUNT);
0078 struct list_head list;
0079 };
0080
0081 struct sgx_backing {
0082 struct page *contents;
0083 struct page *pcmd;
0084 unsigned long pcmd_offset;
0085 };
0086
0087 extern const struct vm_operations_struct sgx_vm_ops;
0088
0089 static inline int sgx_encl_find(struct mm_struct *mm, unsigned long addr,
0090 struct vm_area_struct **vma)
0091 {
0092 struct vm_area_struct *result;
0093
0094 result = vma_lookup(mm, addr);
0095 if (!result || result->vm_ops != &sgx_vm_ops)
0096 return -EINVAL;
0097
0098 *vma = result;
0099
0100 return 0;
0101 }
0102
0103 int sgx_encl_may_map(struct sgx_encl *encl, unsigned long start,
0104 unsigned long end, unsigned long vm_flags);
0105
0106 bool current_is_ksgxd(void);
0107 void sgx_encl_release(struct kref *ref);
0108 int sgx_encl_mm_add(struct sgx_encl *encl, struct mm_struct *mm);
0109 const cpumask_t *sgx_encl_cpumask(struct sgx_encl *encl);
0110 int sgx_encl_lookup_backing(struct sgx_encl *encl, unsigned long page_index,
0111 struct sgx_backing *backing);
0112 int sgx_encl_alloc_backing(struct sgx_encl *encl, unsigned long page_index,
0113 struct sgx_backing *backing);
0114 void sgx_encl_put_backing(struct sgx_backing *backing);
0115 int sgx_encl_test_and_clear_young(struct mm_struct *mm,
0116 struct sgx_encl_page *page);
0117 struct sgx_encl_page *sgx_encl_page_alloc(struct sgx_encl *encl,
0118 unsigned long offset,
0119 u64 secinfo_flags);
0120 void sgx_zap_enclave_ptes(struct sgx_encl *encl, unsigned long addr);
0121 struct sgx_epc_page *sgx_alloc_va_page(bool reclaim);
0122 unsigned int sgx_alloc_va_slot(struct sgx_va_page *va_page);
0123 void sgx_free_va_slot(struct sgx_va_page *va_page, unsigned int offset);
0124 bool sgx_va_page_full(struct sgx_va_page *va_page);
0125 void sgx_encl_free_epc_page(struct sgx_epc_page *page);
0126 struct sgx_encl_page *sgx_encl_load_page(struct sgx_encl *encl,
0127 unsigned long addr);
0128 struct sgx_va_page *sgx_encl_grow(struct sgx_encl *encl, bool reclaim);
0129 void sgx_encl_shrink(struct sgx_encl *encl, struct sgx_va_page *va_page);
0130
0131 #endif