2 * Copyright (c) 2017 Ruslan Bukin <br@bsdpad.com>
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.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
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.
38 * The driver duties is EPC pages management, enclave management, user data
41 * This driver requires Intel SGX support from hardware.
45 * sgx_mmap_single() allocates VM object with following pager
48 * VM object constructor does nothing
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.
54 * VM object fault handler does nothing
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.
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)
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.
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.
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
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.
116 * IntelĀ® 64 and IA-32 Architectures Software Developer's Manual
117 * https://software.intel.com/en-us/articles/intel-sdm
120 #include <sys/cdefs.h>
121 __FBSDID("$FreeBSD$");
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>
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>
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>
155 #include <amd64/sgx/sgxvar.h>
161 #define dprintf(fmt, ...) printf(fmt, ##__VA_ARGS__)
163 #define dprintf(fmt, ...)
166 static struct cdev_pager_ops sgx_pg_ops;
167 struct sgx_softc sgx_sc;
170 sgx_get_epc_page(struct sgx_softc *sc, struct epc_page **epc)
175 if (vmem_alloc(sc->vmem_epc, PAGE_SIZE, M_FIRSTFIT | M_NOWAIT,
177 i = (addr - sc->epc_base) / PAGE_SIZE;
178 *epc = &sc->epc_pages[i];
186 sgx_put_epc_page(struct sgx_softc *sc, struct epc_page *epc)
193 addr = (epc->index * PAGE_SIZE) + sc->epc_base;
194 vmem_free(sc->vmem_epc, addr, PAGE_SIZE);
198 sgx_va_slot_init_by_index(struct sgx_softc *sc, vm_object_t object,
201 struct epc_page *epc;
206 VM_OBJECT_ASSERT_WLOCKED(object);
208 p = vm_page_lookup(object, idx);
210 ret = sgx_get_epc_page(sc, &epc);
212 dprintf("%s: No free EPC pages available.\n",
217 mtx_lock(&sc->mtx_encls);
218 sgx_epa((void *)epc->base);
219 mtx_unlock(&sc->mtx_encls);
221 page = PHYS_TO_VM_PAGE(epc->phys);
223 vm_page_insert(page, object, idx);
224 page->valid = VM_PAGE_BITS_ALL;
231 sgx_va_slot_init(struct sgx_softc *sc,
232 struct sgx_enclave *enclave,
236 uint64_t va_page_idx;
242 object = enclave->object;
244 VM_OBJECT_ASSERT_WLOCKED(object);
246 pidx = OFF_TO_IDX(addr);
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;
252 ret = sgx_va_slot_init_by_index(sc, object, idx);
258 sgx_mem_find(struct sgx_softc *sc, uint64_t addr,
259 vm_map_entry_t *entry0, vm_object_t *object0)
262 vm_map_entry_t entry;
265 map = &curproc->p_vmspace->vm_map;
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__);
274 object = entry->object.vm_object;
275 if (object == NULL || object->handle == NULL) {
276 vm_map_unlock_read(map);
280 if (object->type != OBJT_MGTDEVICE ||
281 object->un_pager.devp.ops != &sgx_pg_ops) {
282 vm_map_unlock_read(map);
286 vm_object_reference(object);
290 vm_map_unlock_read(map);
296 sgx_enclave_find(struct sgx_softc *sc, uint64_t addr,
297 struct sgx_enclave **encl)
299 struct sgx_vm_handle *vmh;
300 struct sgx_enclave *enclave;
301 vm_map_entry_t entry;
305 ret = sgx_mem_find(sc, addr, &entry, &object);
309 vmh = object->handle;
311 vm_object_deallocate(object);
315 enclave = vmh->enclave;
316 if (enclave == NULL || enclave->object == NULL) {
317 vm_object_deallocate(object);
327 sgx_enclave_alloc(struct sgx_softc *sc, struct secs *secs,
328 struct sgx_enclave **enclave0)
330 struct sgx_enclave *enclave;
332 enclave = malloc(sizeof(struct sgx_enclave),
333 M_SGX, M_WAITOK | M_ZERO);
335 enclave->base = secs->base;
336 enclave->size = secs->size;
344 sgx_epc_page_remove(struct sgx_softc *sc,
345 struct epc_page *epc)
348 mtx_lock(&sc->mtx_encls);
349 sgx_eremove((void *)epc->base);
350 mtx_unlock(&sc->mtx_encls);
354 sgx_page_remove(struct sgx_softc *sc, vm_page_t p)
356 struct epc_page *epc;
361 (void)vm_page_remove(p);
364 dprintf("%s: p->pidx %ld\n", __func__, p->pindex);
366 pa = VM_PAGE_TO_PHYS(p);
367 epc = &sc->epc_pages[0];
368 offs = (pa - epc->phys) / PAGE_SIZE;
369 epc = &sc->epc_pages[offs];
371 sgx_epc_page_remove(sc, epc);
372 sgx_put_epc_page(sc, epc);
376 sgx_enclave_remove(struct sgx_softc *sc,
377 struct sgx_enclave *enclave)
380 vm_page_t p, p_secs, p_next;
383 TAILQ_REMOVE(&sc->enclaves, enclave, next);
384 mtx_unlock(&sc->mtx);
386 object = enclave->object;
388 VM_OBJECT_WLOCK(object);
391 * First remove all the pages except SECS,
392 * then remove SECS page.
395 TAILQ_FOREACH_SAFE(p, &object->memq, listq, p_next) {
396 if (p->pindex == SGX_SECS_VM_OBJECT_INDEX) {
400 sgx_page_remove(sc, p);
402 /* Now remove SECS page */
404 sgx_page_remove(sc, p_secs);
406 KASSERT(TAILQ_EMPTY(&object->memq) == 1, ("not empty"));
407 KASSERT(object->resident_page_count == 0, ("count"));
409 VM_OBJECT_WUNLOCK(object);
413 sgx_measure_page(struct sgx_softc *sc, struct epc_page *secs,
414 struct epc_page *epc, uint16_t mrmask)
419 mtx_lock(&sc->mtx_encls);
421 for (i = 0, j = 1; i < PAGE_SIZE; i += 0x100, j <<= 1) {
425 ret = sgx_eextend((void *)secs->base,
426 (void *)(epc->base + i));
427 if (ret == SGX_EFAULT) {
428 mtx_unlock(&sc->mtx_encls);
433 mtx_unlock(&sc->mtx_encls);
439 sgx_secs_validate(struct sgx_softc *sc, struct secs *secs)
441 struct secs_attr *attr;
447 /* BASEADDR must be naturally aligned on an SECS.SIZE boundary. */
448 if (secs->base & (secs->size - 1))
451 /* SECS.SIZE must be at least 2 pages. */
452 if (secs->size < 2 * PAGE_SIZE)
455 if ((secs->size & (secs->size - 1)) != 0)
458 attr = &secs->attributes;
460 if (attr->reserved1 != 0 ||
461 attr->reserved2 != 0 ||
462 attr->reserved3 != 0)
465 for (i = 0; i < SECS_ATTR_RSV4_SIZE; i++)
466 if (attr->reserved4[i])
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.
475 if ((attr->xfrm & 0x3) != 0x3)
478 if (!attr->mode64bit)
481 if (secs->size > sc->enclave_size_max)
484 for (i = 0; i < SECS_RSV1_SIZE; i++)
485 if (secs->reserved1[i])
488 for (i = 0; i < SECS_RSV2_SIZE; i++)
489 if (secs->reserved2[i])
492 for (i = 0; i < SECS_RSV3_SIZE; i++)
493 if (secs->reserved3[i])
496 for (i = 0; i < SECS_RSV4_SIZE; i++)
497 if (secs->reserved4[i])
504 sgx_tcs_validate(struct tcs *tcs)
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))
516 for (i = 0; i < nitems(tcs->reserved3); i++)
517 if (tcs->reserved3[i])
524 sgx_tcs_dump(struct sgx_softc *sc, struct tcs *t)
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);
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)
542 struct sgx_vm_handle *vmh;
546 dprintf("%s: vmh not found.\n", __func__);
550 dprintf("%s: vmh->base %lx foff 0x%lx size 0x%lx\n",
551 __func__, vmh->base, foff, size);
557 sgx_pg_dtor(void *handle)
559 struct sgx_vm_handle *vmh;
560 struct sgx_softc *sc;
564 dprintf("%s: vmh not found.\n", __func__);
570 dprintf("%s: sc is NULL\n", __func__);
574 if (vmh->enclave == NULL) {
575 dprintf("%s: Enclave not found.\n", __func__);
579 sgx_enclave_remove(sc, vmh->enclave);
581 free(vmh->enclave, M_SGX);
586 sgx_pg_fault(vm_object_t object, vm_ooffset_t offset,
587 int prot, vm_page_t *mres)
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.
596 dprintf("%s: offset 0x%lx\n", __func__, offset);
598 return (VM_PAGER_FAIL);
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,
609 sgx_insert_epc_page_by_index(vm_page_t page, vm_object_t object,
613 VM_OBJECT_ASSERT_WLOCKED(object);
615 vm_page_insert(page, object, pidx);
616 page->valid = VM_PAGE_BITS_ALL;
620 sgx_insert_epc_page(struct sgx_enclave *enclave,
621 struct epc_page *epc, uint64_t addr)
626 VM_OBJECT_ASSERT_WLOCKED(enclave->object);
628 pidx = OFF_TO_IDX(addr);
629 page = PHYS_TO_VM_PAGE(epc->phys);
631 sgx_insert_epc_page_by_index(page, enclave->object, pidx);
635 sgx_ioctl_create(struct sgx_softc *sc, struct sgx_enclave_create *param)
637 struct sgx_vm_handle *vmh;
638 vm_map_entry_t entry;
640 struct page_info pginfo;
641 struct secinfo secinfo;
642 struct sgx_enclave *enclave;
643 struct epc_page *epc;
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));
658 dprintf("%s: Can't copy SECS.\n", __func__);
662 ret = sgx_secs_validate(sc, secs);
664 dprintf("%s: SECS validation failed.\n", __func__);
668 ret = sgx_mem_find(sc, secs->base, &entry, &object);
670 dprintf("%s: Can't find vm_map.\n", __func__);
674 vmh = object->handle;
676 dprintf("%s: Can't find vmh.\n", __func__);
681 dprintf("%s: entry start %lx offset %lx\n",
682 __func__, entry->start, entry->offset);
683 vmh->base = (entry->start - entry->offset);
685 ret = sgx_enclave_alloc(sc, secs, &enclave);
687 dprintf("%s: Can't alloc enclave.\n", __func__);
690 enclave->object = object;
693 memset(&secinfo, 0, sizeof(struct secinfo));
694 memset(&pginfo, 0, sizeof(struct page_info));
696 pginfo.srcpge = (uint64_t)secs;
697 pginfo.secinfo = &secinfo;
700 ret = sgx_get_epc_page(sc, &epc);
702 dprintf("%s: Failed to get free epc page.\n", __func__);
705 enclave->secs_epc_page = epc;
707 VM_OBJECT_WLOCK(object);
708 p = vm_page_lookup(object, SGX_SECS_VM_OBJECT_INDEX);
710 VM_OBJECT_WUNLOCK(object);
711 /* SECS page already added. */
716 ret = sgx_va_slot_init_by_index(sc, object,
717 - SGX_VA_PAGES_OFFS - SGX_SECS_VM_OBJECT_INDEX);
719 VM_OBJECT_WUNLOCK(object);
720 dprintf("%s: Can't init va slot.\n", __func__);
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_lookup(enclave->object,
729 - SGX_VA_PAGES_OFFS - SGX_SECS_VM_OBJECT_INDEX);
730 sgx_page_remove(sc, p);
731 VM_OBJECT_WUNLOCK(object);
734 mtx_lock(&sc->mtx_encls);
735 ret = sgx_ecreate(&pginfo, (void *)epc->base);
736 mtx_unlock(&sc->mtx_encls);
737 if (ret == SGX_EFAULT) {
738 dprintf("%s: gp fault\n", __func__);
739 mtx_unlock(&sc->mtx);
740 /* Remove VA page that was just created for SECS page. */
741 p = vm_page_lookup(enclave->object,
742 - SGX_VA_PAGES_OFFS - SGX_SECS_VM_OBJECT_INDEX);
743 sgx_page_remove(sc, p);
744 VM_OBJECT_WUNLOCK(object);
748 TAILQ_INSERT_TAIL(&sc->enclaves, enclave, next);
749 mtx_unlock(&sc->mtx);
751 vmh->enclave = enclave;
753 page = PHYS_TO_VM_PAGE(epc->phys);
754 sgx_insert_epc_page_by_index(page, enclave->object,
755 SGX_SECS_VM_OBJECT_INDEX);
757 VM_OBJECT_WUNLOCK(object);
759 /* Release the reference. */
760 vm_object_deallocate(object);
768 sgx_put_epc_page(sc, epc);
769 free(enclave, M_SGX);
770 vm_object_deallocate(object);
776 sgx_ioctl_add_page(struct sgx_softc *sc,
777 struct sgx_enclave_add_page *addp)
779 struct epc_page *secs_epc_page;
780 struct sgx_enclave *enclave;
781 struct sgx_vm_handle *vmh;
782 struct epc_page *epc;
783 struct page_info pginfo;
784 struct secinfo secinfo;
798 /* Find and get reference to VM object. */
799 ret = sgx_enclave_find(sc, addp->addr, &enclave);
801 dprintf("%s: Failed to find enclave.\n", __func__);
805 object = enclave->object;
806 KASSERT(object != NULL, ("vm object is NULL\n"));
807 vmh = object->handle;
809 ret = sgx_get_epc_page(sc, &epc);
811 dprintf("%s: Failed to get free epc page.\n", __func__);
815 memset(&secinfo, 0, sizeof(struct secinfo));
816 ret = copyin((void *)addp->secinfo, &secinfo,
817 sizeof(struct secinfo));
819 dprintf("%s: Failed to copy secinfo.\n", __func__);
823 tmp_vaddr = malloc(PAGE_SIZE, M_SGX, M_WAITOK | M_ZERO);
824 ret = copyin((void *)addp->src, tmp_vaddr, PAGE_SIZE);
826 dprintf("%s: Failed to copy page.\n", __func__);
830 page_type = (secinfo.flags & SECINFO_FLAGS_PT_M) >>
832 if (page_type != SGX_PT_TCS && page_type != SGX_PT_REG) {
833 dprintf("%s: page can't be added.\n", __func__);
836 if (page_type == SGX_PT_TCS) {
837 t = (struct tcs *)tmp_vaddr;
838 ret = sgx_tcs_validate(t);
840 dprintf("%s: TCS page validation failed.\n",
847 addr = (addp->addr - vmh->base);
848 pidx = OFF_TO_IDX(addr);
850 VM_OBJECT_WLOCK(object);
851 p = vm_page_lookup(object, pidx);
853 VM_OBJECT_WUNLOCK(object);
854 /* Page already added. */
859 ret = sgx_va_slot_init(sc, enclave, addr);
861 VM_OBJECT_WUNLOCK(object);
862 dprintf("%s: Can't init va slot.\n", __func__);
866 secs_epc_page = enclave->secs_epc_page;
867 memset(&pginfo, 0, sizeof(struct page_info));
868 pginfo.linaddr = (uint64_t)addp->addr;
869 pginfo.srcpge = (uint64_t)tmp_vaddr;
870 pginfo.secinfo = &secinfo;
871 pginfo.secs = (uint64_t)secs_epc_page->base;
873 mtx_lock(&sc->mtx_encls);
874 ret = sgx_eadd(&pginfo, (void *)epc->base);
875 if (ret == SGX_EFAULT) {
876 dprintf("%s: gp fault on eadd\n", __func__);
877 mtx_unlock(&sc->mtx_encls);
878 VM_OBJECT_WUNLOCK(object);
881 mtx_unlock(&sc->mtx_encls);
883 ret = sgx_measure_page(sc, enclave->secs_epc_page, epc, addp->mrmask);
884 if (ret == SGX_EFAULT) {
885 dprintf("%s: gp fault on eextend\n", __func__);
886 sgx_epc_page_remove(sc, epc);
887 VM_OBJECT_WUNLOCK(object);
891 sgx_insert_epc_page(enclave, epc, addr);
893 VM_OBJECT_WUNLOCK(object);
895 /* Release the reference. */
896 vm_object_deallocate(object);
898 free(tmp_vaddr, M_SGX);
903 free(tmp_vaddr, M_SGX);
904 sgx_put_epc_page(sc, epc);
905 vm_object_deallocate(object);
911 sgx_ioctl_init(struct sgx_softc *sc, struct sgx_enclave_init *initp)
913 struct epc_page *secs_epc_page;
914 struct sgx_enclave *enclave;
927 dprintf("%s: addr %lx, sigstruct %lx, einittoken %lx\n",
928 __func__, initp->addr, initp->sigstruct, initp->einittoken);
930 /* Find and get reference to VM object. */
931 ret = sgx_enclave_find(sc, initp->addr, &enclave);
933 dprintf("%s: Failed to find enclave.\n", __func__);
937 object = enclave->object;
939 tmp_vaddr = malloc(PAGE_SIZE, M_SGX, M_WAITOK | M_ZERO);
940 sigstruct = tmp_vaddr;
941 einittoken = (void *)((uint64_t)sigstruct + PAGE_SIZE / 2);
943 ret = copyin((void *)initp->sigstruct, sigstruct,
946 dprintf("%s: Failed to copy SIGSTRUCT page.\n", __func__);
950 ret = copyin((void *)initp->einittoken, einittoken,
951 SGX_EINITTOKEN_SIZE);
953 dprintf("%s: Failed to copy EINITTOKEN page.\n", __func__);
957 secs_epc_page = enclave->secs_epc_page;
960 mtx_lock(&sc->mtx_encls);
961 ret = sgx_einit(sigstruct, (void *)secs_epc_page->base,
963 mtx_unlock(&sc->mtx_encls);
964 dprintf("%s: sgx_einit returned %d\n", __func__, ret);
965 } while (ret == SGX_UNMASKED_EVENT && retry--);
968 dprintf("%s: Failed init enclave: %d\n", __func__, ret);
969 td->td_retval[0] = ret;
974 free(tmp_vaddr, M_SGX);
976 /* Release the reference. */
977 vm_object_deallocate(object);
983 sgx_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags,
986 struct sgx_enclave_add_page *addp;
987 struct sgx_enclave_create *param;
988 struct sgx_enclave_init *initp;
989 struct sgx_softc *sc;
995 len = IOCPARM_LEN(cmd);
997 dprintf("%s: cmd %lx, addr %lx, len %d\n",
998 __func__, cmd, (uint64_t)addr, len);
1000 if (len > SGX_IOCTL_MAX_DATA_LEN)
1004 case SGX_IOC_ENCLAVE_CREATE:
1005 param = (struct sgx_enclave_create *)addr;
1006 ret = sgx_ioctl_create(sc, param);
1008 case SGX_IOC_ENCLAVE_ADD_PAGE:
1009 addp = (struct sgx_enclave_add_page *)addr;
1010 ret = sgx_ioctl_add_page(sc, addp);
1012 case SGX_IOC_ENCLAVE_INIT:
1013 initp = (struct sgx_enclave_init *)addr;
1014 ret = sgx_ioctl_init(sc, initp);
1024 sgx_mmap_single(struct cdev *cdev, vm_ooffset_t *offset,
1025 vm_size_t mapsize, struct vm_object **objp, int nprot)
1027 struct sgx_vm_handle *vmh;
1028 struct sgx_softc *sc;
1032 dprintf("%s: mapsize 0x%lx, offset %lx\n",
1033 __func__, mapsize, *offset);
1035 vmh = malloc(sizeof(struct sgx_vm_handle),
1036 M_SGX, M_WAITOK | M_ZERO);
1038 vmh->size = mapsize;
1039 vmh->mem = cdev_pager_allocate(vmh, OBJT_MGTDEVICE, &sgx_pg_ops,
1040 mapsize, nprot, *offset, NULL);
1041 if (vmh->mem == NULL) {
1046 VM_OBJECT_WLOCK(vmh->mem);
1047 vm_object_set_flag(vmh->mem, OBJ_PG_DTOR);
1048 VM_OBJECT_WUNLOCK(vmh->mem);
1055 static struct cdevsw sgx_cdevsw = {
1056 .d_version = D_VERSION,
1057 .d_ioctl = sgx_ioctl,
1058 .d_mmap_single = sgx_mmap_single,
1059 .d_name = "Intel SGX",
1063 sgx_get_epc_area(struct sgx_softc *sc)
1065 vm_offset_t epc_base_vaddr;
1070 cpuid_count(SGX_CPUID, 0x2, cp);
1072 sc->epc_base = ((uint64_t)(cp[1] & 0xfffff) << 32) +
1073 (cp[0] & 0xfffff000);
1074 sc->epc_size = ((uint64_t)(cp[3] & 0xfffff) << 32) +
1075 (cp[2] & 0xfffff000);
1076 sc->npages = sc->epc_size / SGX_PAGE_SIZE;
1079 sc->enclave_size_max = (1 << ((cp[3] >> 8) & 0xff));
1081 sc->enclave_size_max = SGX_ENCL_SIZE_MAX_DEF;
1083 epc_base_vaddr = (vm_offset_t)pmap_mapdev_attr(sc->epc_base,
1084 sc->epc_size, VM_MEMATTR_DEFAULT);
1086 sc->epc_pages = malloc(sizeof(struct epc_page) * sc->npages,
1087 M_DEVBUF, M_WAITOK | M_ZERO);
1089 for (i = 0; i < sc->npages; i++) {
1090 sc->epc_pages[i].base = epc_base_vaddr + SGX_PAGE_SIZE * i;
1091 sc->epc_pages[i].phys = sc->epc_base + SGX_PAGE_SIZE * i;
1092 sc->epc_pages[i].index = i;
1095 sc->vmem_epc = vmem_create("SGX EPC", sc->epc_base, sc->epc_size,
1096 PAGE_SIZE, PAGE_SIZE, M_FIRSTFIT | M_WAITOK);
1097 if (sc->vmem_epc == NULL) {
1098 printf("%s: Can't create vmem arena.\n", __func__);
1099 free(sc->epc_pages, M_SGX);
1103 error = vm_phys_fictitious_reg_range(sc->epc_base,
1104 sc->epc_base + sc->epc_size, VM_MEMATTR_DEFAULT);
1106 printf("%s: Can't register fictitious space.\n", __func__);
1107 free(sc->epc_pages, M_SGX);
1115 sgx_put_epc_area(struct sgx_softc *sc)
1118 vm_phys_fictitious_unreg_range(sc->epc_base,
1119 sc->epc_base + sc->epc_size);
1121 free(sc->epc_pages, M_SGX);
1127 struct sgx_softc *sc;
1132 if ((cpu_stdext_feature & CPUID_STDEXT_SGX) == 0)
1135 error = sgx_get_epc_area(sc);
1137 printf("%s: Failed to get Processor Reserved Memory area.\n",
1142 mtx_init(&sc->mtx_encls, "SGX ENCLS", NULL, MTX_DEF);
1143 mtx_init(&sc->mtx, "SGX driver", NULL, MTX_DEF);
1145 TAILQ_INIT(&sc->enclaves);
1147 sc->sgx_cdev = make_dev(&sgx_cdevsw, 0, UID_ROOT, GID_WHEEL,
1150 sc->state |= SGX_STATE_RUNNING;
1152 printf("SGX initialized: EPC base 0x%lx size %ld (%d pages)\n",
1153 sc->epc_base, sc->epc_size, sc->npages);
1161 struct sgx_softc *sc;
1165 if ((sc->state & SGX_STATE_RUNNING) == 0)
1169 if (!TAILQ_EMPTY(&sc->enclaves)) {
1170 mtx_unlock(&sc->mtx);
1173 sc->state &= ~SGX_STATE_RUNNING;
1174 mtx_unlock(&sc->mtx);
1176 destroy_dev(sc->sgx_cdev);
1178 vmem_destroy(sc->vmem_epc);
1179 sgx_put_epc_area(sc);
1181 mtx_destroy(&sc->mtx_encls);
1182 mtx_destroy(&sc->mtx);
1188 sgx_handler(module_t mod, int what, void *arg)
1197 error = sgx_unload();
1207 static moduledata_t sgx_kmod = {
1213 DECLARE_MODULE(sgx, sgx_kmod, SI_SUB_LAST, SI_ORDER_ANY);
1214 MODULE_VERSION(sgx, 1);