forked from intel/linux-sgx-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsgx.h
306 lines (278 loc) · 10.9 KB
/
sgx.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
* This file is provided under a dual BSD/GPLv2 license. When using or
* redistributing this file, you may do so under either license.
*
* GPL LICENSE SUMMARY
*
* Copyright(c) 2016 Intel Corporation.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that 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.
*
* Contact Information:
* Jarkko Sakkinen <[email protected]>
* Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo
*
* BSD LICENSE
*
* Copyright(c) 2016 Intel Corporation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Intel Corporation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Authors:
*
* Jarkko Sakkinen <[email protected]>
* Suresh Siddha <[email protected]>
* Serge Ayoun <[email protected]>
* Shay Katz-zamir <[email protected]>
*/
#ifndef __ARCH_INTEL_SGX_H__
#define __ARCH_INTEL_SGX_H__
#include "sgx_user.h"
#include <linux/kref.h>
#include <linux/version.h>
#include <linux/rbtree.h>
#include <linux/rwsem.h>
#include <linux/sched.h>
#include <linux/workqueue.h>
#include <linux/mmu_notifier.h>
#include <linux/radix-tree.h>
#include "sgx_arch.h"
#define SGX_EINIT_SPIN_COUNT 20
#define SGX_EINIT_SLEEP_COUNT 50
#define SGX_EINIT_SLEEP_TIME 20
/* # slots in one VA page (4KB page size / 8B nonce-slot) */
#define SGX_VA_SLOT_COUNT 512
/**
* struct sgx_va_page - Metadata for VA page (special type of EPC page)
*
* @epc_page: corresponding EPC page
* @slots: bitvector indicating occupied/free VA slots
* @list: item in list of enclave's VA pages
*/
struct sgx_va_page {
struct sgx_epc_page *epc_page;
DECLARE_BITMAP(slots, SGX_VA_SLOT_COUNT);
struct list_head list;
};
/* Find first free VA slot, update VA page metadata, and return slot offset */
static inline unsigned int sgx_alloc_va_slot(struct sgx_va_page *page)
{
int slot = find_first_zero_bit(page->slots, SGX_VA_SLOT_COUNT);
if (slot < SGX_VA_SLOT_COUNT)
set_bit(slot, page->slots);
return slot << 3; /* each slot is 8 bytes in size */
}
/* Update VA page metadata (slots) to indicate a freed slot */
static inline void sgx_free_va_slot(struct sgx_va_page *page,
unsigned int offset)
{
clear_bit(offset >> 3, page->slots);
}
/* Metadata flags for enclave pages: is it TCS page? is it reserved? */
enum sgx_encl_page_flags {
SGX_ENCL_PAGE_TCS = BIT(0),
SGX_ENCL_PAGE_RESERVED = BIT(1), /* used for debug */
};
/**
* struct sgx_encl_page - Metadata for enclave page
*
* @addr: address in the ELRANGE
* @flags: tcs page, reserved
* @epc_page: corresponding EPC page
* @load_list: item in list of loaded (present) enclave pages
* @va_page: corresponding VA page storing slot with nonce
* @va_offset: corresponding slot with nonce in VA page
*/
struct sgx_encl_page {
unsigned long addr;
unsigned int flags;
struct sgx_epc_page *epc_page;
struct list_head load_list;
struct sgx_va_page *va_page;
unsigned int va_offset;
};
/**
* struct sgx_tgid_ctx - Thread Group ID (TGID) context: driver keeps track
* of many threads in many enclaves (several enclaves
* are grouped in one TGID)
* @tgid: corresponding kernel PID
* @refcount: kref to correctly release unused TGID
* @encl_list: head of list of enclaves inside this TGID (see sgx_encl)
* @list: item in list of TGID contexts
*/
struct sgx_tgid_ctx {
struct pid *tgid;
struct kref refcount;
struct list_head encl_list;
struct list_head list;
};
/* Metadata flags for enclave: is it initialized? is it in debug mode? etc. */
enum sgx_encl_flags {
SGX_ENCL_INITIALIZED = BIT(0),
SGX_ENCL_DEBUG = BIT(1),
SGX_ENCL_SECS_EVICTED = BIT(2),
SGX_ENCL_SUSPEND = BIT(3),
SGX_ENCL_DEAD = BIT(4),
};
/**
* struct sgx_encl - Metadata for enclave
*
* @flags: initialized, debug, dead, etc.
* @secs_child_cnt: # loaded (present) EPC pages, used to evict SECS page when secs_child_cnt=0
* @lock: global enclave lock
* @mm: memory descriptor: VMAs, amount of physical/virtual memory, etc.
* @backing: normal-RAM pages backing EPC pages, used for page-add and eviction (implemented as shmem)
* @pcmd: Paging Crypto MetaData for evicted pages (implemented as shmem)
* @load_list: head of list of loaded enclave pages (see sgx_encl_page)
* @refcount: kref to correctly release unused enclave
* @base: base of enclave's ELRANGE
* @size: size of enclave's ELRANGE
* @va_pages: head of list of enclave's VA pages (see sgx_va_page)
* @page_tree: tree of all enclave pages (except VAs/SECS), with key = addr>>PAGE_SHIFT and value = &sgx_encl_page
* @add_page_reqs: head of list of add-page requests asynchronously processed by page worker (see sgx_add_page_req)
* @add_page_work: work of add_page_reqs deferred to sgx_add_page_worker()
* @secs_page: SECS enclave page
* @tgid_ctx: enclosing TGID context
* @encl_list: item in list of enclaves with same TGID
* @mmu_notifier: if host notifies about release, then declare enclave SGX_ENCL_DEAD
*/
struct sgx_encl {
unsigned int flags;
unsigned int secs_child_cnt;
struct mutex lock;
struct mm_struct *mm;
struct file *backing;
struct file *pcmd;
struct list_head load_list;
struct kref refcount;
unsigned long base;
unsigned long size;
struct list_head va_pages;
struct radix_tree_root page_tree;
struct list_head add_page_reqs;
struct work_struct add_page_work;
struct sgx_encl_page secs_page;
struct sgx_tgid_ctx *tgid_ctx;
struct list_head encl_list;
struct mmu_notifier mmu_notifier;
};
/**
* struct sgx_epc_bank - server can be equipped with several EPC banks, so
* SGX page cache searches for enclave pages in them
* @mem: remapped device memory (only for x64)
* @start: device memory start
* @end: device memory end
*/
struct sgx_epc_bank {
#ifdef CONFIG_X86_64
void *mem;
#endif
unsigned long start;
unsigned long end;
};
extern struct workqueue_struct *sgx_add_page_wq;
extern struct sgx_epc_bank sgx_epc_banks[];
extern int sgx_nr_epc_banks;
extern u64 sgx_encl_size_max_32;
extern u64 sgx_encl_size_max_64;
extern u64 sgx_xfrm_mask;
extern u32 sgx_ssaframesize_tbl[64];
extern bool sgx_has_sgx2;
extern const struct vm_operations_struct sgx_vm_ops;
extern atomic_t sgx_nr_pids;
#define sgx_pr_ratelimited(level, encl, fmt, ...) \
pr_ ## level ## _ratelimited("intel_sgx: [%d:0x%p] " fmt, \
pid_nr((encl)->tgid_ctx->tgid), \
(void *)(encl)->base, ##__VA_ARGS__)
#define sgx_dbg(encl, fmt, ...) sgx_pr_ratelimited(debug, encl, fmt, ##__VA_ARGS__)
#define sgx_info(encl, fmt, ...) sgx_pr_ratelimited(info, encl, fmt, ##__VA_ARGS__)
#define sgx_warn(encl, fmt, ...) sgx_pr_ratelimited(warn, encl, fmt, ##__VA_ARGS__)
#define sgx_err(encl, fmt, ...) sgx_pr_ratelimited(err, encl, fmt, ##__VA_ARGS__)
#define sgx_crit(encl, fmt, ...) sgx_pr_ratelimited(crit, encl, fmt, ##__VA_ARGS__)
long sgx_ioctl(struct file *filep, unsigned int cmd, unsigned long arg);
#ifdef CONFIG_COMPAT
long sgx_compat_ioctl(struct file *filep, unsigned int cmd, unsigned long arg);
#endif
/* Utility functions */
int sgx_test_and_clear_young(struct sgx_encl_page *page, struct sgx_encl *encl);
struct page *sgx_get_backing(struct sgx_encl *encl,
struct sgx_encl_page *entry,
bool pcmd);
void sgx_put_backing(struct page *backing, bool write);
void sgx_insert_pte(struct sgx_encl *encl,
struct sgx_encl_page *encl_page,
struct sgx_epc_page *epc_page,
struct vm_area_struct *vma);
int sgx_eremove(struct sgx_epc_page *epc_page);
struct vm_area_struct *sgx_find_vma(struct sgx_encl *encl, unsigned long addr);
void sgx_zap_tcs_ptes(struct sgx_encl *encl,
struct vm_area_struct *vma);
void sgx_invalidate(struct sgx_encl *encl, bool flush_cpus);
void sgx_flush_cpus(struct sgx_encl *encl);
int sgx_find_encl(struct mm_struct *mm, unsigned long addr,
struct vm_area_struct **vma);
/**
* SGX_FAULT_RESERVE is used for debug (vm_operations_struct.access):
* debug write/read access may require several EPC page faults
* to bring those pages which contain the requested data in EPC
*/
enum sgx_fault_flags {
SGX_FAULT_RESERVE = BIT(0),
};
struct sgx_encl_page *sgx_fault_page(struct vm_area_struct *vma,
unsigned long addr,
unsigned int flags);
void sgx_encl_release(struct kref *ref);
void sgx_tgid_ctx_release(struct kref *ref);
extern struct mutex sgx_tgid_ctx_mutex;
extern struct list_head sgx_tgid_ctx_list;
extern struct task_struct *ksgxswapd_tsk;
/**
* SGX_ALLOC_ATOMIC is set for major page faults and unset for
* init/addition of SECS/VA/normal pages; SGX_ALLOC_ATOMIC allows
* to ignore page faults if ksgxswapd daemon is overloaded and
* cannot swap-out pages to provide free page for this page fault
* (in this case, fault happens again and again till a page is freed)
*/
enum sgx_alloc_flags {
SGX_ALLOC_ATOMIC = BIT(0),
};
int ksgxswapd(void *p);
int sgx_page_cache_init(resource_size_t start, unsigned long size);
void sgx_page_cache_teardown(void);
struct sgx_epc_page *sgx_alloc_page(unsigned int flags);
int sgx_free_page(struct sgx_epc_page *entry, struct sgx_encl *encl);
void *sgx_get_page(struct sgx_epc_page *entry);
void sgx_put_page(void *epc_page_vaddr);
#endif /* __ARCH_X86_INTEL_SGX_H__ */