1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright(c) 2016-20 Intel Corporation.
5 * Contains the software defined data structures for enclaves.
10 #include <linux/cpumask.h>
11 #include <linux/kref.h>
12 #include <linux/list.h>
13 #include <linux/mm_types.h>
14 #include <linux/mmu_notifier.h>
15 #include <linux/mutex.h>
16 #include <linux/notifier.h>
17 #include <linux/srcu.h>
18 #include <linux/workqueue.h>
19 #include <linux/xarray.h>
22 /* 'desc' bits holding the offset in the VA (version array) page. */
23 #define SGX_ENCL_PAGE_VA_OFFSET_MASK GENMASK_ULL(11, 3)
25 /* 'desc' bit marking that the page is being reclaimed. */
26 #define SGX_ENCL_PAGE_BEING_RECLAIMED BIT(3)
28 struct sgx_encl_page {
30 unsigned long vm_max_prot_bits;
31 struct sgx_epc_page *epc_page;
32 struct sgx_encl *encl;
33 struct sgx_va_page *va_page;
37 SGX_ENCL_IOCTL = BIT(0),
38 SGX_ENCL_DEBUG = BIT(1),
39 SGX_ENCL_CREATED = BIT(2),
40 SGX_ENCL_INITIALIZED = BIT(3),
44 struct sgx_encl *encl;
46 struct list_head list;
47 struct mmu_notifier mmu_notifier;
54 unsigned int page_cnt;
55 unsigned int secs_child_cnt;
57 struct xarray page_array;
58 struct sgx_encl_page secs;
59 unsigned long attributes;
60 unsigned long attributes_mask;
65 struct list_head va_pages;
66 unsigned long mm_list_version;
67 struct list_head mm_list;
69 struct srcu_struct srcu;
72 #define SGX_VA_SLOT_COUNT 512
75 struct sgx_epc_page *epc_page;
76 DECLARE_BITMAP(slots, SGX_VA_SLOT_COUNT);
77 struct list_head list;
82 struct page *contents;
84 unsigned long pcmd_offset;
87 extern const struct vm_operations_struct sgx_vm_ops;
89 static inline int sgx_encl_find(struct mm_struct *mm, unsigned long addr,
90 struct vm_area_struct **vma)
92 struct vm_area_struct *result;
94 result = vma_lookup(mm, addr);
95 if (!result || result->vm_ops != &sgx_vm_ops)
103 int sgx_encl_may_map(struct sgx_encl *encl, unsigned long start,
104 unsigned long end, unsigned long vm_flags);
106 bool current_is_ksgxd(void);
107 void sgx_encl_release(struct kref *ref);
108 int sgx_encl_mm_add(struct sgx_encl *encl, struct mm_struct *mm);
109 int sgx_encl_lookup_backing(struct sgx_encl *encl, unsigned long page_index,
110 struct sgx_backing *backing);
111 int sgx_encl_alloc_backing(struct sgx_encl *encl, unsigned long page_index,
112 struct sgx_backing *backing);
113 void sgx_encl_put_backing(struct sgx_backing *backing);
114 int sgx_encl_test_and_clear_young(struct mm_struct *mm,
115 struct sgx_encl_page *page);
117 struct sgx_epc_page *sgx_alloc_va_page(void);
118 unsigned int sgx_alloc_va_slot(struct sgx_va_page *va_page);
119 void sgx_free_va_slot(struct sgx_va_page *va_page, unsigned int offset);
120 bool sgx_va_page_full(struct sgx_va_page *va_page);
121 void sgx_encl_free_epc_page(struct sgx_epc_page *page);
123 #endif /* _X86_ENCL_H */