forked from intel/linux-sgx-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisgx_vma.c
390 lines (326 loc) · 9.77 KB
/
isgx_vma.c
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
* (C) Copyright 2015 Intel Corporation
*
* Authors:
*
* Jarkko Sakkinen <[email protected]>
* Suresh Siddha <[email protected]>
* Serge Ayoun <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; version 2
* of the License.
*/
#include "isgx.h"
#include <asm/mman.h>
#include <linux/delay.h>
#include <linux/file.h>
#include <linux/highmem.h>
#include <linux/ratelimit.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/hashtable.h>
#include <linux/shmem_fs.h>
static void isgx_vma_open(struct vm_area_struct *vma)
{
struct isgx_enclave *enclave;
struct isgx_vma *evma;
/* Was vm_private_data nullified as a result of the previous fork? */
enclave = vma->vm_private_data;
if (!enclave)
goto out_fork;
/* Was the process forked? mm_struct changes when the process is
* forked.
*/
mutex_lock(&enclave->lock);
evma = list_first_entry(&enclave->vma_list,
struct isgx_vma, vma_list);
if (evma->vma->vm_mm != vma->vm_mm) {
mutex_unlock(&enclave->lock);
goto out_fork;
}
mutex_unlock(&enclave->lock);
mutex_lock(&enclave->lock);
if (!list_empty(&enclave->vma_list)) {
evma = kzalloc(sizeof(struct isgx_vma), GFP_KERNEL);
if (!evma) {
isgx_invalidate(enclave);
} else {
evma->vma = vma;
list_add_tail(&evma->vma_list, &enclave->vma_list);
}
}
mutex_unlock(&enclave->lock);
kref_get(&enclave->refcount);
return;
out_fork:
zap_vma_ptes(vma, vma->vm_start, vma->vm_end-vma->vm_start);
vma->vm_private_data = NULL;
return;
}
static void isgx_vma_close(struct vm_area_struct *vma)
{
struct isgx_enclave *enclave = vma->vm_private_data;
struct isgx_vma *evma;
/* If process was forked, VMA is still there but
* vm_private_data is set to NULL.
*/
if (!enclave)
return;
mutex_lock(&enclave->lock);
/* On vma_close() we remove the vma from vma_list
* there is a possibility that evma is not found
* in case vma_open() has failed on memory allocation
* and vma list has then been emptied
*/
evma = isgx_find_vma(enclave, vma->vm_start);
if (evma) {
list_del(&evma->vma_list);
kfree(evma);
}
vma->vm_private_data = NULL;
isgx_zap_tcs_ptes(enclave, vma);
zap_vma_ptes(vma, vma->vm_start, vma->vm_end - vma->vm_start);
mutex_unlock(&enclave->lock);
kref_put(&enclave->refcount, isgx_enclave_release);
}
static void do_eldu(struct isgx_enclave *enclave,
struct isgx_enclave_page *enclave_page,
struct isgx_epc_page *epc_page,
struct page *backing_page,
bool is_secs)
{
struct page_info pginfo;
void *secs_ptr = NULL;
void *epc_ptr;
void *va_ptr;
int ret;
pginfo.srcpge = (unsigned long) kmap_atomic(backing_page);
if (!is_secs)
secs_ptr = isgx_get_epc_page(enclave->secs_page.epc_page);
pginfo.secs = (unsigned long) secs_ptr;
epc_ptr = isgx_get_epc_page(epc_page);
va_ptr = isgx_get_epc_page(enclave_page->va_page->epc_page);
pginfo.linaddr = is_secs ? 0 : enclave_page->addr;
pginfo.pcmd = (unsigned long) &enclave_page->pcmd;
ret = __eldu((unsigned long) &pginfo,
(unsigned long) epc_ptr,
(unsigned long) va_ptr +
enclave_page->va_offset);
isgx_put_epc_page(va_ptr);
isgx_put_epc_page(epc_ptr);
if (!is_secs)
isgx_put_epc_page(secs_ptr);
kunmap_atomic((void *) (unsigned long) pginfo.srcpge);
BUG_ON(ret);
}
static struct isgx_enclave_page *isgx_vma_do_fault(struct vm_area_struct *vma,
unsigned long addr,
int reserve)
{
struct isgx_enclave *enclave = vma->vm_private_data;
struct isgx_enclave_page *entry;
struct isgx_epc_page *epc_page;
struct isgx_epc_page *secs_epc_page = NULL;
struct page *backing_page;
/* If process was forked, VMA is still there but vm_private_data is set
* to NULL.
*/
if (!enclave)
return ERR_PTR(-EFAULT);
entry = isgx_enclave_find_page(enclave, addr);
if (!entry)
return ERR_PTR(-EFAULT);
/* We use atomic allocation in the #PF handler in order to avoid ABBA
* deadlock with mmap_sems.
*/
epc_page = isgx_alloc_epc_page(enclave->tgid_ctx, ISGX_ALLOC_ATOMIC);
if (IS_ERR(epc_page))
return (struct isgx_enclave_page *) epc_page;
/* The SECS page is not currently accounted. */
secs_epc_page = isgx_alloc_epc_page(NULL, ISGX_ALLOC_ATOMIC);
if (IS_ERR(secs_epc_page)) {
isgx_free_epc_page(epc_page, enclave, 0);
return (struct isgx_enclave_page *) secs_epc_page;
}
mutex_lock(&enclave->lock);
if (list_empty(&enclave->vma_list)) {
entry = ERR_PTR(-EFAULT);
goto out;
}
if (!(enclave->flags & ISGX_ENCLAVE_INITIALIZED)) {
isgx_dbg(enclave, "cannot fault, unitialized\n");
entry = ERR_PTR(-EFAULT);
goto out;
}
if (reserve && (entry->flags & ISGX_ENCLAVE_PAGE_RESERVED)) {
isgx_dbg(enclave, "cannot fault, 0x%lx is reserved\n",
entry->addr);
entry = ERR_PTR(-EBUSY);
goto out;
}
/* Legal race condition, page is already faulted. */
if (entry->epc_page) {
if (reserve)
entry->flags |= ISGX_ENCLAVE_PAGE_RESERVED;
goto out;
}
/* If SECS is evicted then reload it first */
if (enclave->flags & ISGX_ENCLAVE_SECS_EVICTED) {
backing_page = isgx_get_backing_page(enclave,
&enclave->secs_page,
false);
if (IS_ERR((void *) backing_page)) {
entry = (void *) backing_page;
goto out;
}
do_eldu(enclave, &enclave->secs_page, secs_epc_page,
backing_page, true /* is_secs */);
isgx_put_backing_page(backing_page, 0);
enclave->secs_page.epc_page = secs_epc_page;
enclave->flags &= ~ISGX_ENCLAVE_SECS_EVICTED;
/* Do not free */
secs_epc_page = NULL;
}
backing_page = isgx_get_backing_page(enclave, entry, false);
if (IS_ERR((void *) backing_page)) {
entry = (void *) backing_page;
goto out;
}
do_eldu(enclave, entry, epc_page, backing_page, false /* is_secs */);
isgx_insert_pte(enclave, entry, epc_page, vma);
isgx_put_backing_page(backing_page, 0);
enclave->secs_child_cnt++;
entry->epc_page = epc_page;
if (reserve)
entry->flags |= ISGX_ENCLAVE_PAGE_RESERVED;
/* Do not free */
epc_page = NULL;
list_add_tail(&entry->load_list, &enclave->load_list);
out:
mutex_unlock(&enclave->lock);
if (epc_page)
isgx_free_epc_page(epc_page, enclave, 0);
if (secs_epc_page)
isgx_free_epc_page(secs_epc_page, NULL, 0);
return entry;
}
static int isgx_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
{
unsigned long addr = (unsigned long) vmf->virtual_address;
struct isgx_enclave_page *entry;
entry = isgx_vma_do_fault(vma, addr, 0);
if (!IS_ERR(entry) || PTR_ERR(entry) == -EBUSY)
return VM_FAULT_NOPAGE;
else
return VM_FAULT_SIGBUS;
}
static inline int isgx_vma_access_word(struct isgx_enclave *enclave,
unsigned long addr,
void *buf,
int len,
int write,
struct isgx_enclave_page *enclave_page,
int i)
{
char data[sizeof(unsigned long)];
int align, cnt, offset;
void *vaddr;
int ret;
offset = ((addr + i) & (PAGE_SIZE - 1)) &
~(sizeof(unsigned long) - 1);
align = (addr + i) & (sizeof (unsigned long) - 1);
cnt = sizeof(unsigned long) - align;
cnt = min(cnt, len - i);
if (write) {
if (enclave_page->flags & ISGX_ENCLAVE_PAGE_TCS &&
(offset < 8 || (offset + (len - i)) > 16))
return -ECANCELED;
if (align || (cnt != sizeof (unsigned long))) {
vaddr = isgx_get_epc_page(enclave_page->epc_page);
ret = __edbgrd((void *)((unsigned long) vaddr + offset),
(unsigned long *) data);
isgx_put_epc_page(vaddr);
if (ret) {
isgx_dbg(enclave, "EDBGRD returned %d\n", ret);
return -EFAULT;
}
}
memcpy(data + align, buf + i, cnt);
vaddr = isgx_get_epc_page(enclave_page->epc_page);
ret = __edbgwr((void *)((unsigned long) vaddr + offset),
(unsigned long *) data);
isgx_put_epc_page(vaddr);
if (ret) {
isgx_dbg(enclave, "EDBGWR returned %d\n", ret);
return -EFAULT;
}
} else {
if (enclave_page->flags & ISGX_ENCLAVE_PAGE_TCS &&
(offset + (len - i)) > 72)
return -ECANCELED;
vaddr = isgx_get_epc_page(enclave_page->epc_page);
ret = __edbgrd((void *)((unsigned long) vaddr + offset),
(unsigned long *) data);
isgx_put_epc_page(vaddr);
if (ret) {
isgx_dbg(enclave, "EDBGRD returned %d\n", ret);
return -EFAULT;
}
memcpy(buf + i, data + align, cnt);
}
return cnt;
}
static int isgx_vma_access(struct vm_area_struct *vma, unsigned long addr,
void *buf, int len, int write)
{
struct isgx_enclave *enclave = vma->vm_private_data;
struct isgx_enclave_page *enclave_page = NULL;
const char *op_str = write ? "EDBGWR" : "EDBGRD";
int ret = 0;
int i;
/* If process was forked, VMA is still there but vm_private_data is set
* to NULL.
*/
if (!enclave)
return -EFAULT;
if (!(enclave->flags & ISGX_ENCLAVE_DEBUG) ||
!(enclave->flags & ISGX_ENCLAVE_INITIALIZED) ||
(enclave->flags & ISGX_ENCLAVE_SUSPEND))
return -EFAULT;
isgx_dbg(enclave, "%s addr=0x%lx, len=%d\n", op_str, addr, len);
for (i = 0; i < len; i += ret) {
if (!enclave_page || !((addr + i) & (PAGE_SIZE - 1))) {
if (enclave_page)
enclave_page->flags &= ~ISGX_ENCLAVE_PAGE_RESERVED;
for (enclave_page = ERR_PTR(-EBUSY);
enclave_page == ERR_PTR(-EBUSY);
enclave_page = isgx_vma_do_fault(
vma,
(addr + i) & PAGE_MASK,
1));
if (IS_ERR(enclave_page)) {
ret = PTR_ERR(enclave_page);
enclave_page = NULL;
break;
}
}
/* No locks are needed because used fields are immutable after
* intialization.
*/
ret = isgx_vma_access_word(enclave, addr, buf, len, write,
enclave_page, i);
if (ret < 0)
break;
}
if (enclave_page)
enclave_page->flags &= ~ISGX_ENCLAVE_PAGE_RESERVED;
return (ret < 0 && ret != -ECANCELED) ? ret : i;
}
struct vm_operations_struct isgx_vm_ops = {
.close = isgx_vma_close,
.open = isgx_vma_open,
.fault = isgx_vma_fault,
.access = isgx_vma_access,
};