xref: /freebsd/sys/amd64/sgx/sgx.c (revision e17f5b1d)
1 /*-
2  * Copyright (c) 2017 Ruslan Bukin <br@bsdpad.com>
3  * All rights reserved.
4  *
5  * This software was developed by BAE Systems, the University of Cambridge
6  * Computer Laboratory, and Memorial University under DARPA/AFRL contract
7  * FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent Computing
8  * (TC) research program.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 /*
33  * Design overview.
34  *
35  * The driver provides character device for mmap(2) and ioctl(2) system calls
36  * allowing user to manage isolated compartments ("enclaves") in user VA space.
37  *
38  * The driver duties is EPC pages management, enclave management, user data
39  * validation.
40  *
41  * This driver requires Intel SGX support from hardware.
42  *
43  * /dev/sgx:
44  *    .mmap:
45  *        sgx_mmap_single() allocates VM object with following pager
46  *        operations:
47  *              a) sgx_pg_ctor():
48  *                  VM object constructor does nothing
49  *              b) sgx_pg_dtor():
50  *                  VM object destructor destroys the SGX enclave associated
51  *                  with the object: it frees all the EPC pages allocated for
52  *                  enclave and removes the enclave.
53  *              c) sgx_pg_fault():
54  *                  VM object fault handler does nothing
55  *
56  *    .ioctl:
57  *        sgx_ioctl():
58  *               a) SGX_IOC_ENCLAVE_CREATE
59  *                   Adds Enclave SECS page: initial step of enclave creation.
60  *               b) SGX_IOC_ENCLAVE_ADD_PAGE
61  *                   Adds TCS, REG pages to the enclave.
62  *               c) SGX_IOC_ENCLAVE_INIT
63  *                   Finalizes enclave creation.
64  *
65  * Enclave lifecycle:
66  *          .-- ECREATE  -- Add SECS page
67  *   Kernel |   EADD     -- Add TCS, REG pages
68  *    space |   EEXTEND  -- Measure the page (take unique hash)
69  *    ENCLS |   EPA      -- Allocate version array page
70  *          '-- EINIT    -- Finalize enclave creation
71  *   User   .-- EENTER   -- Go to entry point of enclave
72  *    space |   EEXIT    -- Exit back to main application
73  *    ENCLU '-- ERESUME  -- Resume enclave execution (e.g. after exception)
74  *
75  * Enclave lifecycle from driver point of view:
76  *  1) User calls mmap() on /dev/sgx: we allocate a VM object
77  *  2) User calls ioctl SGX_IOC_ENCLAVE_CREATE: we look for the VM object
78  *     associated with user process created on step 1, create SECS physical
79  *     page and store it in enclave's VM object queue by special index
80  *     SGX_SECS_VM_OBJECT_INDEX.
81  *  3) User calls ioctl SGX_IOC_ENCLAVE_ADD_PAGE: we look for enclave created
82  *     on step 2, create TCS or REG physical page and map it to specified by
83  *     user address of enclave VM object.
84  *  4) User finalizes enclave creation with ioctl SGX_IOC_ENCLAVE_INIT call.
85  *  5) User can freely enter to and exit from enclave using ENCLU instructions
86  *     from userspace: the driver does nothing here.
87  *  6) User proceed munmap(2) system call (or the process with enclave dies):
88  *     we destroy the enclave associated with the object.
89  *
90  * EPC page types and their indexes in VM object queue:
91  *   - PT_SECS index is special and equals SGX_SECS_VM_OBJECT_INDEX (-1);
92  *   - PT_TCS and PT_REG indexes are specified by user in addr field of ioctl
93  *     request data and determined as follows:
94  *       pidx = OFF_TO_IDX(addp->addr - vmh->base);
95  *   - PT_VA index is special, created for PT_REG, PT_TCS and PT_SECS pages
96  *     and determined by formula:
97  *       va_page_idx = - SGX_VA_PAGES_OFFS - (page_idx / SGX_VA_PAGE_SLOTS);
98  *     PT_VA page can hold versions of up to 512 pages, and slot for each
99  *     page in PT_VA page is determined as follows:
100  *       va_slot_idx = page_idx % SGX_VA_PAGE_SLOTS;
101  *   - PT_TRIM is unused.
102  *
103  * Locking:
104  *    SGX ENCLS set of instructions have limitations on concurrency:
105  *    some instructions can't be executed same time on different CPUs.
106  *    We use sc->mtx_encls lock around them to prevent concurrent execution.
107  *    sc->mtx lock is used to manage list of created enclaves and the state of
108  *    SGX driver.
109  *
110  * Eviction of EPC pages:
111  *    Eviction support is not implemented in this driver, however the driver
112  *    manages VA (version array) pages: it allocates a VA slot for each EPC
113  *    page. This will be required for eviction support in future.
114  *    VA pages and slots are currently unused.
115  *
116  * Intel® 64 and IA-32 Architectures Software Developer's Manual
117  * https://software.intel.com/en-us/articles/intel-sdm
118  */
119 
120 #include <sys/cdefs.h>
121 __FBSDID("$FreeBSD$");
122 
123 #include <sys/param.h>
124 #include <sys/systm.h>
125 #include <sys/ioccom.h>
126 #include <sys/malloc.h>
127 #include <sys/kernel.h>
128 #include <sys/lock.h>
129 #include <sys/mutex.h>
130 #include <sys/rwlock.h>
131 #include <sys/conf.h>
132 #include <sys/module.h>
133 #include <sys/proc.h>
134 #include <sys/vmem.h>
135 #include <sys/vmmeter.h>
136 
137 #include <vm/vm.h>
138 #include <vm/vm_param.h>
139 #include <vm/vm_extern.h>
140 #include <vm/vm_kern.h>
141 #include <vm/vm_page.h>
142 #include <vm/vm_map.h>
143 #include <vm/vm_object.h>
144 #include <vm/vm_pager.h>
145 #include <vm/vm_phys.h>
146 #include <vm/vm_radix.h>
147 #include <vm/pmap.h>
148 
149 #include <machine/md_var.h>
150 #include <machine/specialreg.h>
151 #include <machine/cpufunc.h>
152 #include <machine/sgx.h>
153 #include <machine/sgxreg.h>
154 
155 #include <amd64/sgx/sgxvar.h>
156 
157 #define	SGX_DEBUG
158 #undef	SGX_DEBUG
159 
160 #ifdef	SGX_DEBUG
161 #define	dprintf(fmt, ...)	printf(fmt, ##__VA_ARGS__)
162 #else
163 #define	dprintf(fmt, ...)
164 #endif
165 
166 static struct cdev_pager_ops sgx_pg_ops;
167 struct sgx_softc sgx_sc;
168 
169 static int
170 sgx_get_epc_page(struct sgx_softc *sc, struct epc_page **epc)
171 {
172 	vmem_addr_t addr;
173 	int i;
174 
175 	if (vmem_alloc(sc->vmem_epc, PAGE_SIZE, M_FIRSTFIT | M_NOWAIT,
176 	    &addr) == 0) {
177 		i = (addr - sc->epc_base) / PAGE_SIZE;
178 		*epc = &sc->epc_pages[i];
179 		return (0);
180 	}
181 
182 	return (ENOMEM);
183 }
184 
185 static void
186 sgx_put_epc_page(struct sgx_softc *sc, struct epc_page *epc)
187 {
188 	vmem_addr_t addr;
189 
190 	if (epc == NULL)
191 		return;
192 
193 	addr = (epc->index * PAGE_SIZE) + sc->epc_base;
194 	vmem_free(sc->vmem_epc, addr, PAGE_SIZE);
195 }
196 
197 static int
198 sgx_va_slot_init_by_index(struct sgx_softc *sc, vm_object_t object,
199     uint64_t idx)
200 {
201 	struct epc_page *epc;
202 	vm_page_t page;
203 	vm_page_t p;
204 	int ret;
205 
206 	VM_OBJECT_ASSERT_WLOCKED(object);
207 
208 	p = vm_page_lookup(object, idx);
209 	if (p == NULL) {
210 		ret = sgx_get_epc_page(sc, &epc);
211 		if (ret) {
212 			dprintf("%s: No free EPC pages available.\n",
213 			    __func__);
214 			return (ret);
215 		}
216 
217 		mtx_lock(&sc->mtx_encls);
218 		sgx_epa((void *)epc->base);
219 		mtx_unlock(&sc->mtx_encls);
220 
221 		page = PHYS_TO_VM_PAGE(epc->phys);
222 
223 		page->valid = VM_PAGE_BITS_ALL;
224 		vm_page_insert(page, object, idx);
225 	}
226 
227 	return (0);
228 }
229 
230 static int
231 sgx_va_slot_init(struct sgx_softc *sc,
232     struct sgx_enclave *enclave,
233     uint64_t addr)
234 {
235 	vm_pindex_t pidx;
236 	uint64_t va_page_idx;
237 	uint64_t idx;
238 	vm_object_t object;
239 	int va_slot;
240 	int ret;
241 
242 	object = enclave->object;
243 
244 	VM_OBJECT_ASSERT_WLOCKED(object);
245 
246 	pidx = OFF_TO_IDX(addr);
247 
248 	va_slot = pidx % SGX_VA_PAGE_SLOTS;
249 	va_page_idx = pidx / SGX_VA_PAGE_SLOTS;
250 	idx = - SGX_VA_PAGES_OFFS - va_page_idx;
251 
252 	ret = sgx_va_slot_init_by_index(sc, object, idx);
253 
254 	return (ret);
255 }
256 
257 static int
258 sgx_mem_find(struct sgx_softc *sc, uint64_t addr,
259     vm_map_entry_t *entry0, vm_object_t *object0)
260 {
261 	vm_map_t map;
262 	vm_map_entry_t entry;
263 	vm_object_t object;
264 
265 	map = &curproc->p_vmspace->vm_map;
266 
267 	vm_map_lock_read(map);
268 	if (!vm_map_lookup_entry(map, addr, &entry)) {
269 		vm_map_unlock_read(map);
270 		dprintf("%s: Can't find enclave.\n", __func__);
271 		return (EINVAL);
272 	}
273 
274 	object = entry->object.vm_object;
275 	if (object == NULL || object->handle == NULL) {
276 		vm_map_unlock_read(map);
277 		return (EINVAL);
278 	}
279 
280 	if (object->type != OBJT_MGTDEVICE ||
281 	    object->un_pager.devp.ops != &sgx_pg_ops) {
282 		vm_map_unlock_read(map);
283 		return (EINVAL);
284 	}
285 
286 	vm_object_reference(object);
287 
288 	*object0 = object;
289 	*entry0 = entry;
290 	vm_map_unlock_read(map);
291 
292 	return (0);
293 }
294 
295 static int
296 sgx_enclave_find(struct sgx_softc *sc, uint64_t addr,
297     struct sgx_enclave **encl)
298 {
299 	struct sgx_vm_handle *vmh;
300 	struct sgx_enclave *enclave;
301 	vm_map_entry_t entry;
302 	vm_object_t object;
303 	int ret;
304 
305 	ret = sgx_mem_find(sc, addr, &entry, &object);
306 	if (ret)
307 		return (ret);
308 
309 	vmh = object->handle;
310 	if (vmh == NULL) {
311 		vm_object_deallocate(object);
312 		return (EINVAL);
313 	}
314 
315 	enclave = vmh->enclave;
316 	if (enclave == NULL || enclave->object == NULL) {
317 		vm_object_deallocate(object);
318 		return (EINVAL);
319 	}
320 
321 	*encl = enclave;
322 
323 	return (0);
324 }
325 
326 static int
327 sgx_enclave_alloc(struct sgx_softc *sc, struct secs *secs,
328     struct sgx_enclave **enclave0)
329 {
330 	struct sgx_enclave *enclave;
331 
332 	enclave = malloc(sizeof(struct sgx_enclave),
333 	    M_SGX, M_WAITOK | M_ZERO);
334 
335 	enclave->base = secs->base;
336 	enclave->size = secs->size;
337 
338 	*enclave0 = enclave;
339 
340 	return (0);
341 }
342 
343 static void
344 sgx_epc_page_remove(struct sgx_softc *sc,
345     struct epc_page *epc)
346 {
347 
348 	mtx_lock(&sc->mtx_encls);
349 	sgx_eremove((void *)epc->base);
350 	mtx_unlock(&sc->mtx_encls);
351 }
352 
353 static void
354 sgx_page_remove(struct sgx_softc *sc, vm_page_t p)
355 {
356 	struct epc_page *epc;
357 	vm_paddr_t pa;
358 	uint64_t offs;
359 
360 	(void)vm_page_remove(p);
361 
362 	dprintf("%s: p->pidx %ld\n", __func__, p->pindex);
363 
364 	pa = VM_PAGE_TO_PHYS(p);
365 	epc = &sc->epc_pages[0];
366 	offs = (pa - epc->phys) / PAGE_SIZE;
367 	epc = &sc->epc_pages[offs];
368 
369 	sgx_epc_page_remove(sc, epc);
370 	sgx_put_epc_page(sc, epc);
371 }
372 
373 static void
374 sgx_enclave_remove(struct sgx_softc *sc,
375     struct sgx_enclave *enclave)
376 {
377 	vm_object_t object;
378 	vm_page_t p, p_secs, p_next;
379 
380 	mtx_lock(&sc->mtx);
381 	TAILQ_REMOVE(&sc->enclaves, enclave, next);
382 	mtx_unlock(&sc->mtx);
383 
384 	object = enclave->object;
385 
386 	VM_OBJECT_WLOCK(object);
387 
388 	/*
389 	 * First remove all the pages except SECS,
390 	 * then remove SECS page.
391 	 */
392 restart:
393 	TAILQ_FOREACH_SAFE(p, &object->memq, listq, p_next) {
394 		if (p->pindex == SGX_SECS_VM_OBJECT_INDEX)
395 			continue;
396 		if (vm_page_busy_acquire(p, VM_ALLOC_WAITFAIL) == 0)
397 			goto restart;
398 		sgx_page_remove(sc, p);
399 	}
400 	p_secs = vm_page_grab(object, SGX_SECS_VM_OBJECT_INDEX,
401 	    VM_ALLOC_NOCREAT);
402 	/* Now remove SECS page */
403 	if (p_secs != NULL)
404 		sgx_page_remove(sc, p_secs);
405 
406 	KASSERT(TAILQ_EMPTY(&object->memq) == 1, ("not empty"));
407 	KASSERT(object->resident_page_count == 0, ("count"));
408 
409 	VM_OBJECT_WUNLOCK(object);
410 }
411 
412 static int
413 sgx_measure_page(struct sgx_softc *sc, struct epc_page *secs,
414     struct epc_page *epc, uint16_t mrmask)
415 {
416 	int i, j;
417 	int ret;
418 
419 	mtx_lock(&sc->mtx_encls);
420 
421 	for (i = 0, j = 1; i < PAGE_SIZE; i += 0x100, j <<= 1) {
422 		if (!(j & mrmask))
423 			continue;
424 
425 		ret = sgx_eextend((void *)secs->base,
426 		    (void *)(epc->base + i));
427 		if (ret == SGX_EFAULT) {
428 			mtx_unlock(&sc->mtx_encls);
429 			return (ret);
430 		}
431 	}
432 
433 	mtx_unlock(&sc->mtx_encls);
434 
435 	return (0);
436 }
437 
438 static int
439 sgx_secs_validate(struct sgx_softc *sc, struct secs *secs)
440 {
441 	struct secs_attr *attr;
442 	int i;
443 
444 	if (secs->size == 0)
445 		return (EINVAL);
446 
447 	/* BASEADDR must be naturally aligned on an SECS.SIZE boundary. */
448 	if (secs->base & (secs->size - 1))
449 		return (EINVAL);
450 
451 	/* SECS.SIZE must be at least 2 pages. */
452 	if (secs->size < 2 * PAGE_SIZE)
453 		return (EINVAL);
454 
455 	if ((secs->size & (secs->size - 1)) != 0)
456 		return (EINVAL);
457 
458 	attr = &secs->attributes;
459 
460 	if (attr->reserved1 != 0 ||
461 	    attr->reserved2 != 0 ||
462 	    attr->reserved3 != 0)
463 		return (EINVAL);
464 
465 	for (i = 0; i < SECS_ATTR_RSV4_SIZE; i++)
466 		if (attr->reserved4[i])
467 			return (EINVAL);
468 
469 	/*
470 	 * Intel® Software Guard Extensions Programming Reference
471 	 * 6.7.2 Relevant Fields in Various Data Structures
472 	 * 6.7.2.1 SECS.ATTRIBUTES.XFRM
473 	 * XFRM[1:0] must be set to 0x3.
474 	 */
475 	if ((attr->xfrm & 0x3) != 0x3)
476 		return (EINVAL);
477 
478 	if (!attr->mode64bit)
479 		return (EINVAL);
480 
481 	if (secs->size > sc->enclave_size_max)
482 		return (EINVAL);
483 
484 	for (i = 0; i < SECS_RSV1_SIZE; i++)
485 		if (secs->reserved1[i])
486 			return (EINVAL);
487 
488 	for (i = 0; i < SECS_RSV2_SIZE; i++)
489 		if (secs->reserved2[i])
490 			return (EINVAL);
491 
492 	for (i = 0; i < SECS_RSV3_SIZE; i++)
493 		if (secs->reserved3[i])
494 			return (EINVAL);
495 
496 	for (i = 0; i < SECS_RSV4_SIZE; i++)
497 		if (secs->reserved4[i])
498 			return (EINVAL);
499 
500 	return (0);
501 }
502 
503 static int
504 sgx_tcs_validate(struct tcs *tcs)
505 {
506 	int i;
507 
508 	if ((tcs->flags) ||
509 	    (tcs->ossa & (PAGE_SIZE - 1)) ||
510 	    (tcs->ofsbasgx & (PAGE_SIZE - 1)) ||
511 	    (tcs->ogsbasgx & (PAGE_SIZE - 1)) ||
512 	    ((tcs->fslimit & 0xfff) != 0xfff) ||
513 	    ((tcs->gslimit & 0xfff) != 0xfff))
514 		return (EINVAL);
515 
516 	for (i = 0; i < nitems(tcs->reserved3); i++)
517 		if (tcs->reserved3[i])
518 			return (EINVAL);
519 
520 	return (0);
521 }
522 
523 static void
524 sgx_tcs_dump(struct sgx_softc *sc, struct tcs *t)
525 {
526 
527 	dprintf("t->flags %lx\n", t->flags);
528 	dprintf("t->ossa %lx\n", t->ossa);
529 	dprintf("t->cssa %x\n", t->cssa);
530 	dprintf("t->nssa %x\n", t->nssa);
531 	dprintf("t->oentry %lx\n", t->oentry);
532 	dprintf("t->ofsbasgx %lx\n", t->ofsbasgx);
533 	dprintf("t->ogsbasgx %lx\n", t->ogsbasgx);
534 	dprintf("t->fslimit %x\n", t->fslimit);
535 	dprintf("t->gslimit %x\n", t->gslimit);
536 }
537 
538 static int
539 sgx_pg_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
540     vm_ooffset_t foff, struct ucred *cred, u_short *color)
541 {
542 	struct sgx_vm_handle *vmh;
543 
544 	vmh = handle;
545 	if (vmh == NULL) {
546 		dprintf("%s: vmh not found.\n", __func__);
547 		return (0);
548 	}
549 
550 	dprintf("%s: vmh->base %lx foff 0x%lx size 0x%lx\n",
551 	    __func__, vmh->base, foff, size);
552 
553 	return (0);
554 }
555 
556 static void
557 sgx_pg_dtor(void *handle)
558 {
559 	struct sgx_vm_handle *vmh;
560 	struct sgx_softc *sc;
561 
562 	vmh = handle;
563 	if (vmh == NULL) {
564 		dprintf("%s: vmh not found.\n", __func__);
565 		return;
566 	}
567 
568 	sc = vmh->sc;
569 	if (sc == NULL) {
570 		dprintf("%s: sc is NULL\n", __func__);
571 		return;
572 	}
573 
574 	if (vmh->enclave == NULL) {
575 		dprintf("%s: Enclave not found.\n", __func__);
576 		return;
577 	}
578 
579 	sgx_enclave_remove(sc, vmh->enclave);
580 
581 	free(vmh->enclave, M_SGX);
582 	free(vmh, M_SGX);
583 }
584 
585 static int
586 sgx_pg_fault(vm_object_t object, vm_ooffset_t offset,
587     int prot, vm_page_t *mres)
588 {
589 
590 	/*
591 	 * The purpose of this trivial handler is to handle the race
592 	 * when user tries to access mmaped region before or during
593 	 * enclave creation ioctl calls.
594 	 */
595 
596 	dprintf("%s: offset 0x%lx\n", __func__, offset);
597 
598 	return (VM_PAGER_FAIL);
599 }
600 
601 static struct cdev_pager_ops sgx_pg_ops = {
602 	.cdev_pg_ctor = sgx_pg_ctor,
603 	.cdev_pg_dtor = sgx_pg_dtor,
604 	.cdev_pg_fault = sgx_pg_fault,
605 };
606 
607 
608 static void
609 sgx_insert_epc_page_by_index(vm_page_t page, vm_object_t object,
610     vm_pindex_t pidx)
611 {
612 
613 	VM_OBJECT_ASSERT_WLOCKED(object);
614 
615 	page->valid = VM_PAGE_BITS_ALL;
616 	vm_page_insert(page, object, pidx);
617 }
618 
619 static void
620 sgx_insert_epc_page(struct sgx_enclave *enclave,
621     struct epc_page *epc, uint64_t addr)
622 {
623 	vm_pindex_t pidx;
624 	vm_page_t page;
625 
626 	VM_OBJECT_ASSERT_WLOCKED(enclave->object);
627 
628 	pidx = OFF_TO_IDX(addr);
629 	page = PHYS_TO_VM_PAGE(epc->phys);
630 
631 	sgx_insert_epc_page_by_index(page, enclave->object, pidx);
632 }
633 
634 static int
635 sgx_ioctl_create(struct sgx_softc *sc, struct sgx_enclave_create *param)
636 {
637 	struct sgx_vm_handle *vmh;
638 	vm_map_entry_t entry;
639 	vm_page_t p;
640 	struct page_info pginfo;
641 	struct secinfo secinfo;
642 	struct sgx_enclave *enclave;
643 	struct epc_page *epc;
644 	struct secs *secs;
645 	vm_object_t object;
646 	vm_page_t page;
647 	int ret;
648 
649 	epc = NULL;
650 	secs = NULL;
651 	enclave = NULL;
652 	object = NULL;
653 
654 	/* SGX Enclave Control Structure (SECS) */
655 	secs = malloc(PAGE_SIZE, M_SGX, M_WAITOK | M_ZERO);
656 	ret = copyin((void *)param->src, secs, sizeof(struct secs));
657 	if (ret) {
658 		dprintf("%s: Can't copy SECS.\n", __func__);
659 		goto error;
660 	}
661 
662 	ret = sgx_secs_validate(sc, secs);
663 	if (ret) {
664 		dprintf("%s: SECS validation failed.\n", __func__);
665 		goto error;
666 	}
667 
668 	ret = sgx_mem_find(sc, secs->base, &entry, &object);
669 	if (ret) {
670 		dprintf("%s: Can't find vm_map.\n", __func__);
671 		goto error;
672 	}
673 
674 	vmh = object->handle;
675 	if (!vmh) {
676 		dprintf("%s: Can't find vmh.\n", __func__);
677 		ret = ENXIO;
678 		goto error;
679 	}
680 
681 	dprintf("%s: entry start %lx offset %lx\n",
682 	    __func__, entry->start, entry->offset);
683 	vmh->base = (entry->start - entry->offset);
684 
685 	ret = sgx_enclave_alloc(sc, secs, &enclave);
686 	if (ret) {
687 		dprintf("%s: Can't alloc enclave.\n", __func__);
688 		goto error;
689 	}
690 	enclave->object = object;
691 	enclave->vmh = vmh;
692 
693 	memset(&secinfo, 0, sizeof(struct secinfo));
694 	memset(&pginfo, 0, sizeof(struct page_info));
695 	pginfo.linaddr = 0;
696 	pginfo.srcpge = (uint64_t)secs;
697 	pginfo.secinfo = &secinfo;
698 	pginfo.secs = 0;
699 
700 	ret = sgx_get_epc_page(sc, &epc);
701 	if (ret) {
702 		dprintf("%s: Failed to get free epc page.\n", __func__);
703 		goto error;
704 	}
705 	enclave->secs_epc_page = epc;
706 
707 	VM_OBJECT_WLOCK(object);
708 	p = vm_page_lookup(object, SGX_SECS_VM_OBJECT_INDEX);
709 	if (p) {
710 		VM_OBJECT_WUNLOCK(object);
711 		/* SECS page already added. */
712 		ret = ENXIO;
713 		goto error;
714 	}
715 
716 	ret = sgx_va_slot_init_by_index(sc, object,
717 	    - SGX_VA_PAGES_OFFS - SGX_SECS_VM_OBJECT_INDEX);
718 	if (ret) {
719 		VM_OBJECT_WUNLOCK(object);
720 		dprintf("%s: Can't init va slot.\n", __func__);
721 		goto error;
722 	}
723 
724 	mtx_lock(&sc->mtx);
725 	if ((sc->state & SGX_STATE_RUNNING) == 0) {
726 		mtx_unlock(&sc->mtx);
727 		/* Remove VA page that was just created for SECS page. */
728 		p = vm_page_grab(enclave->object,
729 		    - SGX_VA_PAGES_OFFS - SGX_SECS_VM_OBJECT_INDEX,
730 		    VM_ALLOC_NOCREAT);
731 		sgx_page_remove(sc, p);
732 		VM_OBJECT_WUNLOCK(object);
733 		goto error;
734 	}
735 	mtx_lock(&sc->mtx_encls);
736 	ret = sgx_ecreate(&pginfo, (void *)epc->base);
737 	mtx_unlock(&sc->mtx_encls);
738 	if (ret == SGX_EFAULT) {
739 		dprintf("%s: gp fault\n", __func__);
740 		mtx_unlock(&sc->mtx);
741 		/* Remove VA page that was just created for SECS page. */
742 		p = vm_page_grab(enclave->object,
743 		    - SGX_VA_PAGES_OFFS - SGX_SECS_VM_OBJECT_INDEX,
744 		    VM_ALLOC_NOCREAT);
745 		sgx_page_remove(sc, p);
746 		VM_OBJECT_WUNLOCK(object);
747 		goto error;
748 	}
749 
750 	TAILQ_INSERT_TAIL(&sc->enclaves, enclave, next);
751 	mtx_unlock(&sc->mtx);
752 
753 	vmh->enclave = enclave;
754 
755 	page = PHYS_TO_VM_PAGE(epc->phys);
756 	sgx_insert_epc_page_by_index(page, enclave->object,
757 	    SGX_SECS_VM_OBJECT_INDEX);
758 
759 	VM_OBJECT_WUNLOCK(object);
760 
761 	/* Release the reference. */
762 	vm_object_deallocate(object);
763 
764 	free(secs, M_SGX);
765 
766 	return (0);
767 
768 error:
769 	free(secs, M_SGX);
770 	sgx_put_epc_page(sc, epc);
771 	free(enclave, M_SGX);
772 	vm_object_deallocate(object);
773 
774 	return (ret);
775 }
776 
777 static int
778 sgx_ioctl_add_page(struct sgx_softc *sc,
779     struct sgx_enclave_add_page *addp)
780 {
781 	struct epc_page *secs_epc_page;
782 	struct sgx_enclave *enclave;
783 	struct sgx_vm_handle *vmh;
784 	struct epc_page *epc;
785 	struct page_info pginfo;
786 	struct secinfo secinfo;
787 	vm_object_t object;
788 	void *tmp_vaddr;
789 	uint64_t page_type;
790 	struct tcs *t;
791 	uint64_t addr;
792 	uint64_t pidx;
793 	vm_page_t p;
794 	int ret;
795 
796 	tmp_vaddr = NULL;
797 	epc = NULL;
798 	object = NULL;
799 
800 	/* Find and get reference to VM object. */
801 	ret = sgx_enclave_find(sc, addp->addr, &enclave);
802 	if (ret) {
803 		dprintf("%s: Failed to find enclave.\n", __func__);
804 		goto error;
805 	}
806 
807 	object = enclave->object;
808 	KASSERT(object != NULL, ("vm object is NULL\n"));
809 	vmh = object->handle;
810 
811 	ret = sgx_get_epc_page(sc, &epc);
812 	if (ret) {
813 		dprintf("%s: Failed to get free epc page.\n", __func__);
814 		goto error;
815 	}
816 
817 	memset(&secinfo, 0, sizeof(struct secinfo));
818 	ret = copyin((void *)addp->secinfo, &secinfo,
819 	    sizeof(struct secinfo));
820 	if (ret) {
821 		dprintf("%s: Failed to copy secinfo.\n", __func__);
822 		goto error;
823 	}
824 
825 	tmp_vaddr = malloc(PAGE_SIZE, M_SGX, M_WAITOK | M_ZERO);
826 	ret = copyin((void *)addp->src, tmp_vaddr, PAGE_SIZE);
827 	if (ret) {
828 		dprintf("%s: Failed to copy page.\n", __func__);
829 		goto error;
830 	}
831 
832 	page_type = (secinfo.flags & SECINFO_FLAGS_PT_M) >>
833 	    SECINFO_FLAGS_PT_S;
834 	if (page_type != SGX_PT_TCS && page_type != SGX_PT_REG) {
835 		dprintf("%s: page can't be added.\n", __func__);
836 		goto error;
837 	}
838 	if (page_type == SGX_PT_TCS) {
839 		t = (struct tcs *)tmp_vaddr;
840 		ret = sgx_tcs_validate(t);
841 		if (ret) {
842 			dprintf("%s: TCS page validation failed.\n",
843 			    __func__);
844 			goto error;
845 		}
846 		sgx_tcs_dump(sc, t);
847 	}
848 
849 	addr = (addp->addr - vmh->base);
850 	pidx = OFF_TO_IDX(addr);
851 
852 	VM_OBJECT_WLOCK(object);
853 	p = vm_page_lookup(object, pidx);
854 	if (p) {
855 		VM_OBJECT_WUNLOCK(object);
856 		/* Page already added. */
857 		ret = ENXIO;
858 		goto error;
859 	}
860 
861 	ret = sgx_va_slot_init(sc, enclave, addr);
862 	if (ret) {
863 		VM_OBJECT_WUNLOCK(object);
864 		dprintf("%s: Can't init va slot.\n", __func__);
865 		goto error;
866 	}
867 
868 	secs_epc_page = enclave->secs_epc_page;
869 	memset(&pginfo, 0, sizeof(struct page_info));
870 	pginfo.linaddr = (uint64_t)addp->addr;
871 	pginfo.srcpge = (uint64_t)tmp_vaddr;
872 	pginfo.secinfo = &secinfo;
873 	pginfo.secs = (uint64_t)secs_epc_page->base;
874 
875 	mtx_lock(&sc->mtx_encls);
876 	ret = sgx_eadd(&pginfo, (void *)epc->base);
877 	if (ret == SGX_EFAULT) {
878 		dprintf("%s: gp fault on eadd\n", __func__);
879 		mtx_unlock(&sc->mtx_encls);
880 		VM_OBJECT_WUNLOCK(object);
881 		goto error;
882 	}
883 	mtx_unlock(&sc->mtx_encls);
884 
885 	ret = sgx_measure_page(sc, enclave->secs_epc_page, epc, addp->mrmask);
886 	if (ret == SGX_EFAULT) {
887 		dprintf("%s: gp fault on eextend\n", __func__);
888 		sgx_epc_page_remove(sc, epc);
889 		VM_OBJECT_WUNLOCK(object);
890 		goto error;
891 	}
892 
893 	sgx_insert_epc_page(enclave, epc, addr);
894 
895 	VM_OBJECT_WUNLOCK(object);
896 
897 	/* Release the reference. */
898 	vm_object_deallocate(object);
899 
900 	free(tmp_vaddr, M_SGX);
901 
902 	return (0);
903 
904 error:
905 	free(tmp_vaddr, M_SGX);
906 	sgx_put_epc_page(sc, epc);
907 	vm_object_deallocate(object);
908 
909 	return (ret);
910 }
911 
912 static int
913 sgx_ioctl_init(struct sgx_softc *sc, struct sgx_enclave_init *initp)
914 {
915 	struct epc_page *secs_epc_page;
916 	struct sgx_enclave *enclave;
917 	struct thread *td;
918 	void *tmp_vaddr;
919 	void *einittoken;
920 	void *sigstruct;
921 	vm_object_t object;
922 	int retry;
923 	int ret;
924 
925 	td = curthread;
926 	tmp_vaddr = NULL;
927 	object = NULL;
928 
929 	dprintf("%s: addr %lx, sigstruct %lx, einittoken %lx\n",
930 	    __func__, initp->addr, initp->sigstruct, initp->einittoken);
931 
932 	/* Find and get reference to VM object. */
933 	ret = sgx_enclave_find(sc, initp->addr, &enclave);
934 	if (ret) {
935 		dprintf("%s: Failed to find enclave.\n", __func__);
936 		goto error;
937 	}
938 
939 	object = enclave->object;
940 
941 	tmp_vaddr = malloc(PAGE_SIZE, M_SGX, M_WAITOK | M_ZERO);
942 	sigstruct = tmp_vaddr;
943 	einittoken = (void *)((uint64_t)sigstruct + PAGE_SIZE / 2);
944 
945 	ret = copyin((void *)initp->sigstruct, sigstruct,
946 	    SGX_SIGSTRUCT_SIZE);
947 	if (ret) {
948 		dprintf("%s: Failed to copy SIGSTRUCT page.\n", __func__);
949 		goto error;
950 	}
951 
952 	ret = copyin((void *)initp->einittoken, einittoken,
953 	    SGX_EINITTOKEN_SIZE);
954 	if (ret) {
955 		dprintf("%s: Failed to copy EINITTOKEN page.\n", __func__);
956 		goto error;
957 	}
958 
959 	secs_epc_page = enclave->secs_epc_page;
960 	retry = 16;
961 	do {
962 		mtx_lock(&sc->mtx_encls);
963 		ret = sgx_einit(sigstruct, (void *)secs_epc_page->base,
964 		    einittoken);
965 		mtx_unlock(&sc->mtx_encls);
966 		dprintf("%s: sgx_einit returned %d\n", __func__, ret);
967 	} while (ret == SGX_UNMASKED_EVENT && retry--);
968 
969 	if (ret) {
970 		dprintf("%s: Failed init enclave: %d\n", __func__, ret);
971 		td->td_retval[0] = ret;
972 		ret = 0;
973 	}
974 
975 error:
976 	free(tmp_vaddr, M_SGX);
977 
978 	/* Release the reference. */
979 	vm_object_deallocate(object);
980 
981 	return (ret);
982 }
983 
984 static int
985 sgx_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags,
986     struct thread *td)
987 {
988 	struct sgx_enclave_add_page *addp;
989 	struct sgx_enclave_create *param;
990 	struct sgx_enclave_init *initp;
991 	struct sgx_softc *sc;
992 	int ret;
993 	int len;
994 
995 	sc = &sgx_sc;
996 
997 	len = IOCPARM_LEN(cmd);
998 
999 	dprintf("%s: cmd %lx, addr %lx, len %d\n",
1000 	    __func__, cmd, (uint64_t)addr, len);
1001 
1002 	if (len > SGX_IOCTL_MAX_DATA_LEN)
1003 		return (EINVAL);
1004 
1005 	switch (cmd) {
1006 	case SGX_IOC_ENCLAVE_CREATE:
1007 		param = (struct sgx_enclave_create *)addr;
1008 		ret = sgx_ioctl_create(sc, param);
1009 		break;
1010 	case SGX_IOC_ENCLAVE_ADD_PAGE:
1011 		addp = (struct sgx_enclave_add_page *)addr;
1012 		ret = sgx_ioctl_add_page(sc, addp);
1013 		break;
1014 	case SGX_IOC_ENCLAVE_INIT:
1015 		initp = (struct sgx_enclave_init *)addr;
1016 		ret = sgx_ioctl_init(sc, initp);
1017 		break;
1018 	default:
1019 		return (EINVAL);
1020 	}
1021 
1022 	return (ret);
1023 }
1024 
1025 static int
1026 sgx_mmap_single(struct cdev *cdev, vm_ooffset_t *offset,
1027     vm_size_t mapsize, struct vm_object **objp, int nprot)
1028 {
1029 	struct sgx_vm_handle *vmh;
1030 	struct sgx_softc *sc;
1031 
1032 	sc = &sgx_sc;
1033 
1034 	dprintf("%s: mapsize 0x%lx, offset %lx\n",
1035 	    __func__, mapsize, *offset);
1036 
1037 	vmh = malloc(sizeof(struct sgx_vm_handle),
1038 	    M_SGX, M_WAITOK | M_ZERO);
1039 	vmh->sc = sc;
1040 	vmh->size = mapsize;
1041 	vmh->mem = cdev_pager_allocate(vmh, OBJT_MGTDEVICE, &sgx_pg_ops,
1042 	    mapsize, nprot, *offset, NULL);
1043 	if (vmh->mem == NULL) {
1044 		free(vmh, M_SGX);
1045 		return (ENOMEM);
1046 	}
1047 
1048 	VM_OBJECT_WLOCK(vmh->mem);
1049 	vm_object_set_flag(vmh->mem, OBJ_PG_DTOR);
1050 	VM_OBJECT_WUNLOCK(vmh->mem);
1051 
1052 	*objp = vmh->mem;
1053 
1054 	return (0);
1055 }
1056 
1057 static struct cdevsw sgx_cdevsw = {
1058 	.d_version =		D_VERSION,
1059 	.d_ioctl =		sgx_ioctl,
1060 	.d_mmap_single =	sgx_mmap_single,
1061 	.d_name =		"Intel SGX",
1062 };
1063 
1064 static int
1065 sgx_get_epc_area(struct sgx_softc *sc)
1066 {
1067 	vm_offset_t epc_base_vaddr;
1068 	u_int cp[4];
1069 	int error;
1070 	int i;
1071 
1072 	cpuid_count(SGX_CPUID, 0x2, cp);
1073 
1074 	sc->epc_base = ((uint64_t)(cp[1] & 0xfffff) << 32) +
1075 	    (cp[0] & 0xfffff000);
1076 	sc->epc_size = ((uint64_t)(cp[3] & 0xfffff) << 32) +
1077 	    (cp[2] & 0xfffff000);
1078 	sc->npages = sc->epc_size / SGX_PAGE_SIZE;
1079 
1080 	if (sc->epc_size == 0 || sc->epc_base == 0) {
1081 		printf("%s: Incorrect EPC data: EPC base %lx, size %lu\n",
1082 		    __func__, sc->epc_base, sc->epc_size);
1083 		return (EINVAL);
1084 	}
1085 
1086 	if (cp[3] & 0xffff)
1087 		sc->enclave_size_max = (1 << ((cp[3] >> 8) & 0xff));
1088 	else
1089 		sc->enclave_size_max = SGX_ENCL_SIZE_MAX_DEF;
1090 
1091 	epc_base_vaddr = (vm_offset_t)pmap_mapdev_attr(sc->epc_base,
1092 	    sc->epc_size, VM_MEMATTR_DEFAULT);
1093 
1094 	sc->epc_pages = malloc(sizeof(struct epc_page) * sc->npages,
1095 	    M_DEVBUF, M_WAITOK | M_ZERO);
1096 
1097 	for (i = 0; i < sc->npages; i++) {
1098 		sc->epc_pages[i].base = epc_base_vaddr + SGX_PAGE_SIZE * i;
1099 		sc->epc_pages[i].phys = sc->epc_base + SGX_PAGE_SIZE * i;
1100 		sc->epc_pages[i].index = i;
1101 	}
1102 
1103 	sc->vmem_epc = vmem_create("SGX EPC", sc->epc_base, sc->epc_size,
1104 	    PAGE_SIZE, PAGE_SIZE, M_FIRSTFIT | M_WAITOK);
1105 	if (sc->vmem_epc == NULL) {
1106 		printf("%s: Can't create vmem arena.\n", __func__);
1107 		free(sc->epc_pages, M_SGX);
1108 		return (EINVAL);
1109 	}
1110 
1111 	error = vm_phys_fictitious_reg_range(sc->epc_base,
1112 	    sc->epc_base + sc->epc_size, VM_MEMATTR_DEFAULT);
1113 	if (error) {
1114 		printf("%s: Can't register fictitious space.\n", __func__);
1115 		free(sc->epc_pages, M_SGX);
1116 		return (EINVAL);
1117 	}
1118 
1119 	return (0);
1120 }
1121 
1122 static void
1123 sgx_put_epc_area(struct sgx_softc *sc)
1124 {
1125 
1126 	vm_phys_fictitious_unreg_range(sc->epc_base,
1127 	    sc->epc_base + sc->epc_size);
1128 
1129 	free(sc->epc_pages, M_SGX);
1130 }
1131 
1132 static int
1133 sgx_load(void)
1134 {
1135 	struct sgx_softc *sc;
1136 	int error;
1137 
1138 	sc = &sgx_sc;
1139 
1140 	if ((cpu_stdext_feature & CPUID_STDEXT_SGX) == 0)
1141 		return (ENXIO);
1142 
1143 	error = sgx_get_epc_area(sc);
1144 	if (error) {
1145 		printf("%s: Failed to get Processor Reserved Memory area.\n",
1146 		    __func__);
1147 		return (ENXIO);
1148 	}
1149 
1150 	mtx_init(&sc->mtx_encls, "SGX ENCLS", NULL, MTX_DEF);
1151 	mtx_init(&sc->mtx, "SGX driver", NULL, MTX_DEF);
1152 
1153 	TAILQ_INIT(&sc->enclaves);
1154 
1155 	sc->sgx_cdev = make_dev(&sgx_cdevsw, 0, UID_ROOT, GID_WHEEL,
1156 	    0600, "isgx");
1157 
1158 	sc->state |= SGX_STATE_RUNNING;
1159 
1160 	printf("SGX initialized: EPC base 0x%lx size %ld (%d pages)\n",
1161 	    sc->epc_base, sc->epc_size, sc->npages);
1162 
1163 	return (0);
1164 }
1165 
1166 static int
1167 sgx_unload(void)
1168 {
1169 	struct sgx_softc *sc;
1170 
1171 	sc = &sgx_sc;
1172 
1173 	if ((sc->state & SGX_STATE_RUNNING) == 0)
1174 		return (0);
1175 
1176 	mtx_lock(&sc->mtx);
1177 	if (!TAILQ_EMPTY(&sc->enclaves)) {
1178 		mtx_unlock(&sc->mtx);
1179 		return (EBUSY);
1180 	}
1181 	sc->state &= ~SGX_STATE_RUNNING;
1182 	mtx_unlock(&sc->mtx);
1183 
1184 	destroy_dev(sc->sgx_cdev);
1185 
1186 	vmem_destroy(sc->vmem_epc);
1187 	sgx_put_epc_area(sc);
1188 
1189 	mtx_destroy(&sc->mtx_encls);
1190 	mtx_destroy(&sc->mtx);
1191 
1192 	return (0);
1193 }
1194 
1195 static int
1196 sgx_handler(module_t mod, int what, void *arg)
1197 {
1198 	int error;
1199 
1200 	switch (what) {
1201 	case MOD_LOAD:
1202 		error = sgx_load();
1203 		break;
1204 	case MOD_UNLOAD:
1205 		error = sgx_unload();
1206 		break;
1207 	default:
1208 		error = 0;
1209 		break;
1210 	}
1211 
1212 	return (error);
1213 }
1214 
1215 static moduledata_t sgx_kmod = {
1216 	"sgx",
1217 	sgx_handler,
1218 	NULL
1219 };
1220 
1221 DECLARE_MODULE(sgx, sgx_kmod, SI_SUB_LAST, SI_ORDER_ANY);
1222 MODULE_VERSION(sgx, 1);
1223