xref: /freebsd/sys/amd64/sgx/sgx.c (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
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 static void
608 sgx_insert_epc_page_by_index(vm_page_t page, vm_object_t object,
609     vm_pindex_t pidx)
610 {
611 
612 	VM_OBJECT_ASSERT_WLOCKED(object);
613 
614 	page->valid = VM_PAGE_BITS_ALL;
615 	vm_page_insert(page, object, pidx);
616 }
617 
618 static void
619 sgx_insert_epc_page(struct sgx_enclave *enclave,
620     struct epc_page *epc, uint64_t addr)
621 {
622 	vm_pindex_t pidx;
623 	vm_page_t page;
624 
625 	VM_OBJECT_ASSERT_WLOCKED(enclave->object);
626 
627 	pidx = OFF_TO_IDX(addr);
628 	page = PHYS_TO_VM_PAGE(epc->phys);
629 
630 	sgx_insert_epc_page_by_index(page, enclave->object, pidx);
631 }
632 
633 static int
634 sgx_ioctl_create(struct sgx_softc *sc, struct sgx_enclave_create *param)
635 {
636 	struct sgx_vm_handle *vmh;
637 	vm_map_entry_t entry;
638 	vm_page_t p;
639 	struct page_info pginfo;
640 	struct secinfo secinfo;
641 	struct sgx_enclave *enclave;
642 	struct epc_page *epc;
643 	struct secs *secs;
644 	vm_object_t object;
645 	vm_page_t page;
646 	int ret;
647 
648 	epc = NULL;
649 	secs = NULL;
650 	enclave = NULL;
651 	object = NULL;
652 
653 	/* SGX Enclave Control Structure (SECS) */
654 	secs = malloc(PAGE_SIZE, M_SGX, M_WAITOK | M_ZERO);
655 	ret = copyin((void *)param->src, secs, sizeof(struct secs));
656 	if (ret) {
657 		dprintf("%s: Can't copy SECS.\n", __func__);
658 		goto error;
659 	}
660 
661 	ret = sgx_secs_validate(sc, secs);
662 	if (ret) {
663 		dprintf("%s: SECS validation failed.\n", __func__);
664 		goto error;
665 	}
666 
667 	ret = sgx_mem_find(sc, secs->base, &entry, &object);
668 	if (ret) {
669 		dprintf("%s: Can't find vm_map.\n", __func__);
670 		goto error;
671 	}
672 
673 	vmh = object->handle;
674 	if (!vmh) {
675 		dprintf("%s: Can't find vmh.\n", __func__);
676 		ret = ENXIO;
677 		goto error;
678 	}
679 
680 	dprintf("%s: entry start %lx offset %lx\n",
681 	    __func__, entry->start, entry->offset);
682 	vmh->base = (entry->start - entry->offset);
683 
684 	ret = sgx_enclave_alloc(sc, secs, &enclave);
685 	if (ret) {
686 		dprintf("%s: Can't alloc enclave.\n", __func__);
687 		goto error;
688 	}
689 	enclave->object = object;
690 	enclave->vmh = vmh;
691 
692 	memset(&secinfo, 0, sizeof(struct secinfo));
693 	memset(&pginfo, 0, sizeof(struct page_info));
694 	pginfo.linaddr = 0;
695 	pginfo.srcpge = (uint64_t)secs;
696 	pginfo.secinfo = &secinfo;
697 	pginfo.secs = 0;
698 
699 	ret = sgx_get_epc_page(sc, &epc);
700 	if (ret) {
701 		dprintf("%s: Failed to get free epc page.\n", __func__);
702 		goto error;
703 	}
704 	enclave->secs_epc_page = epc;
705 
706 	VM_OBJECT_WLOCK(object);
707 	p = vm_page_lookup(object, SGX_SECS_VM_OBJECT_INDEX);
708 	if (p) {
709 		VM_OBJECT_WUNLOCK(object);
710 		/* SECS page already added. */
711 		ret = ENXIO;
712 		goto error;
713 	}
714 
715 	ret = sgx_va_slot_init_by_index(sc, object,
716 	    - SGX_VA_PAGES_OFFS - SGX_SECS_VM_OBJECT_INDEX);
717 	if (ret) {
718 		VM_OBJECT_WUNLOCK(object);
719 		dprintf("%s: Can't init va slot.\n", __func__);
720 		goto error;
721 	}
722 
723 	mtx_lock(&sc->mtx);
724 	if ((sc->state & SGX_STATE_RUNNING) == 0) {
725 		mtx_unlock(&sc->mtx);
726 		/* Remove VA page that was just created for SECS page. */
727 		p = vm_page_grab(enclave->object,
728 		    - SGX_VA_PAGES_OFFS - SGX_SECS_VM_OBJECT_INDEX,
729 		    VM_ALLOC_NOCREAT);
730 		sgx_page_remove(sc, p);
731 		VM_OBJECT_WUNLOCK(object);
732 		goto error;
733 	}
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_grab(enclave->object,
742 		    - SGX_VA_PAGES_OFFS - SGX_SECS_VM_OBJECT_INDEX,
743 		    VM_ALLOC_NOCREAT);
744 		sgx_page_remove(sc, p);
745 		VM_OBJECT_WUNLOCK(object);
746 		goto error;
747 	}
748 
749 	TAILQ_INSERT_TAIL(&sc->enclaves, enclave, next);
750 	mtx_unlock(&sc->mtx);
751 
752 	vmh->enclave = enclave;
753 
754 	page = PHYS_TO_VM_PAGE(epc->phys);
755 	sgx_insert_epc_page_by_index(page, enclave->object,
756 	    SGX_SECS_VM_OBJECT_INDEX);
757 
758 	VM_OBJECT_WUNLOCK(object);
759 
760 	/* Release the reference. */
761 	vm_object_deallocate(object);
762 
763 	free(secs, M_SGX);
764 
765 	return (0);
766 
767 error:
768 	free(secs, M_SGX);
769 	sgx_put_epc_page(sc, epc);
770 	free(enclave, M_SGX);
771 	vm_object_deallocate(object);
772 
773 	return (ret);
774 }
775 
776 static int
777 sgx_ioctl_add_page(struct sgx_softc *sc,
778     struct sgx_enclave_add_page *addp)
779 {
780 	struct epc_page *secs_epc_page;
781 	struct sgx_enclave *enclave;
782 	struct sgx_vm_handle *vmh;
783 	struct epc_page *epc;
784 	struct page_info pginfo;
785 	struct secinfo secinfo;
786 	vm_object_t object;
787 	void *tmp_vaddr;
788 	uint64_t page_type;
789 	struct tcs *t;
790 	uint64_t addr;
791 	uint64_t pidx;
792 	vm_page_t p;
793 	int ret;
794 
795 	tmp_vaddr = NULL;
796 	epc = NULL;
797 	object = NULL;
798 
799 	/* Find and get reference to VM object. */
800 	ret = sgx_enclave_find(sc, addp->addr, &enclave);
801 	if (ret) {
802 		dprintf("%s: Failed to find enclave.\n", __func__);
803 		goto error;
804 	}
805 
806 	object = enclave->object;
807 	KASSERT(object != NULL, ("vm object is NULL\n"));
808 	vmh = object->handle;
809 
810 	ret = sgx_get_epc_page(sc, &epc);
811 	if (ret) {
812 		dprintf("%s: Failed to get free epc page.\n", __func__);
813 		goto error;
814 	}
815 
816 	memset(&secinfo, 0, sizeof(struct secinfo));
817 	ret = copyin((void *)addp->secinfo, &secinfo,
818 	    sizeof(struct secinfo));
819 	if (ret) {
820 		dprintf("%s: Failed to copy secinfo.\n", __func__);
821 		goto error;
822 	}
823 
824 	tmp_vaddr = malloc(PAGE_SIZE, M_SGX, M_WAITOK | M_ZERO);
825 	ret = copyin((void *)addp->src, tmp_vaddr, PAGE_SIZE);
826 	if (ret) {
827 		dprintf("%s: Failed to copy page.\n", __func__);
828 		goto error;
829 	}
830 
831 	page_type = (secinfo.flags & SECINFO_FLAGS_PT_M) >>
832 	    SECINFO_FLAGS_PT_S;
833 	if (page_type != SGX_PT_TCS && page_type != SGX_PT_REG) {
834 		dprintf("%s: page can't be added.\n", __func__);
835 		goto error;
836 	}
837 	if (page_type == SGX_PT_TCS) {
838 		t = (struct tcs *)tmp_vaddr;
839 		ret = sgx_tcs_validate(t);
840 		if (ret) {
841 			dprintf("%s: TCS page validation failed.\n",
842 			    __func__);
843 			goto error;
844 		}
845 		sgx_tcs_dump(sc, t);
846 	}
847 
848 	addr = (addp->addr - vmh->base);
849 	pidx = OFF_TO_IDX(addr);
850 
851 	VM_OBJECT_WLOCK(object);
852 	p = vm_page_lookup(object, pidx);
853 	if (p) {
854 		VM_OBJECT_WUNLOCK(object);
855 		/* Page already added. */
856 		ret = ENXIO;
857 		goto error;
858 	}
859 
860 	ret = sgx_va_slot_init(sc, enclave, addr);
861 	if (ret) {
862 		VM_OBJECT_WUNLOCK(object);
863 		dprintf("%s: Can't init va slot.\n", __func__);
864 		goto error;
865 	}
866 
867 	secs_epc_page = enclave->secs_epc_page;
868 	memset(&pginfo, 0, sizeof(struct page_info));
869 	pginfo.linaddr = (uint64_t)addp->addr;
870 	pginfo.srcpge = (uint64_t)tmp_vaddr;
871 	pginfo.secinfo = &secinfo;
872 	pginfo.secs = (uint64_t)secs_epc_page->base;
873 
874 	mtx_lock(&sc->mtx_encls);
875 	ret = sgx_eadd(&pginfo, (void *)epc->base);
876 	if (ret == SGX_EFAULT) {
877 		dprintf("%s: gp fault on eadd\n", __func__);
878 		mtx_unlock(&sc->mtx_encls);
879 		VM_OBJECT_WUNLOCK(object);
880 		goto error;
881 	}
882 	mtx_unlock(&sc->mtx_encls);
883 
884 	ret = sgx_measure_page(sc, enclave->secs_epc_page, epc, addp->mrmask);
885 	if (ret == SGX_EFAULT) {
886 		dprintf("%s: gp fault on eextend\n", __func__);
887 		sgx_epc_page_remove(sc, epc);
888 		VM_OBJECT_WUNLOCK(object);
889 		goto error;
890 	}
891 
892 	sgx_insert_epc_page(enclave, epc, addr);
893 
894 	VM_OBJECT_WUNLOCK(object);
895 
896 	/* Release the reference. */
897 	vm_object_deallocate(object);
898 
899 	free(tmp_vaddr, M_SGX);
900 
901 	return (0);
902 
903 error:
904 	free(tmp_vaddr, M_SGX);
905 	sgx_put_epc_page(sc, epc);
906 	vm_object_deallocate(object);
907 
908 	return (ret);
909 }
910 
911 static int
912 sgx_ioctl_init(struct sgx_softc *sc, struct sgx_enclave_init *initp)
913 {
914 	struct epc_page *secs_epc_page;
915 	struct sgx_enclave *enclave;
916 	struct thread *td;
917 	void *tmp_vaddr;
918 	void *einittoken;
919 	void *sigstruct;
920 	vm_object_t object;
921 	int retry;
922 	int ret;
923 
924 	td = curthread;
925 	tmp_vaddr = NULL;
926 	object = NULL;
927 
928 	dprintf("%s: addr %lx, sigstruct %lx, einittoken %lx\n",
929 	    __func__, initp->addr, initp->sigstruct, initp->einittoken);
930 
931 	/* Find and get reference to VM object. */
932 	ret = sgx_enclave_find(sc, initp->addr, &enclave);
933 	if (ret) {
934 		dprintf("%s: Failed to find enclave.\n", __func__);
935 		goto error;
936 	}
937 
938 	object = enclave->object;
939 
940 	tmp_vaddr = malloc(PAGE_SIZE, M_SGX, M_WAITOK | M_ZERO);
941 	sigstruct = tmp_vaddr;
942 	einittoken = (void *)((uint64_t)sigstruct + PAGE_SIZE / 2);
943 
944 	ret = copyin((void *)initp->sigstruct, sigstruct,
945 	    SGX_SIGSTRUCT_SIZE);
946 	if (ret) {
947 		dprintf("%s: Failed to copy SIGSTRUCT page.\n", __func__);
948 		goto error;
949 	}
950 
951 	ret = copyin((void *)initp->einittoken, einittoken,
952 	    SGX_EINITTOKEN_SIZE);
953 	if (ret) {
954 		dprintf("%s: Failed to copy EINITTOKEN page.\n", __func__);
955 		goto error;
956 	}
957 
958 	secs_epc_page = enclave->secs_epc_page;
959 	retry = 16;
960 	do {
961 		mtx_lock(&sc->mtx_encls);
962 		ret = sgx_einit(sigstruct, (void *)secs_epc_page->base,
963 		    einittoken);
964 		mtx_unlock(&sc->mtx_encls);
965 		dprintf("%s: sgx_einit returned %d\n", __func__, ret);
966 	} while (ret == SGX_UNMASKED_EVENT && retry--);
967 
968 	if (ret) {
969 		dprintf("%s: Failed init enclave: %d\n", __func__, ret);
970 		td->td_retval[0] = ret;
971 		ret = 0;
972 	}
973 
974 error:
975 	free(tmp_vaddr, M_SGX);
976 
977 	/* Release the reference. */
978 	vm_object_deallocate(object);
979 
980 	return (ret);
981 }
982 
983 static int
984 sgx_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags,
985     struct thread *td)
986 {
987 	struct sgx_enclave_add_page *addp;
988 	struct sgx_enclave_create *param;
989 	struct sgx_enclave_init *initp;
990 	struct sgx_softc *sc;
991 	int ret;
992 	int len;
993 
994 	sc = &sgx_sc;
995 
996 	len = IOCPARM_LEN(cmd);
997 
998 	dprintf("%s: cmd %lx, addr %lx, len %d\n",
999 	    __func__, cmd, (uint64_t)addr, len);
1000 
1001 	if (len > SGX_IOCTL_MAX_DATA_LEN)
1002 		return (EINVAL);
1003 
1004 	switch (cmd) {
1005 	case SGX_IOC_ENCLAVE_CREATE:
1006 		param = (struct sgx_enclave_create *)addr;
1007 		ret = sgx_ioctl_create(sc, param);
1008 		break;
1009 	case SGX_IOC_ENCLAVE_ADD_PAGE:
1010 		addp = (struct sgx_enclave_add_page *)addr;
1011 		ret = sgx_ioctl_add_page(sc, addp);
1012 		break;
1013 	case SGX_IOC_ENCLAVE_INIT:
1014 		initp = (struct sgx_enclave_init *)addr;
1015 		ret = sgx_ioctl_init(sc, initp);
1016 		break;
1017 	default:
1018 		return (EINVAL);
1019 	}
1020 
1021 	return (ret);
1022 }
1023 
1024 static int
1025 sgx_mmap_single(struct cdev *cdev, vm_ooffset_t *offset,
1026     vm_size_t mapsize, struct vm_object **objp, int nprot)
1027 {
1028 	struct sgx_vm_handle *vmh;
1029 	struct sgx_softc *sc;
1030 
1031 	sc = &sgx_sc;
1032 
1033 	dprintf("%s: mapsize 0x%lx, offset %lx\n",
1034 	    __func__, mapsize, *offset);
1035 
1036 	vmh = malloc(sizeof(struct sgx_vm_handle),
1037 	    M_SGX, M_WAITOK | M_ZERO);
1038 	vmh->sc = sc;
1039 	vmh->size = mapsize;
1040 	vmh->mem = cdev_pager_allocate(vmh, OBJT_MGTDEVICE, &sgx_pg_ops,
1041 	    mapsize, nprot, *offset, NULL);
1042 	if (vmh->mem == NULL) {
1043 		free(vmh, M_SGX);
1044 		return (ENOMEM);
1045 	}
1046 
1047 	VM_OBJECT_WLOCK(vmh->mem);
1048 	vm_object_set_flag(vmh->mem, OBJ_PG_DTOR);
1049 	VM_OBJECT_WUNLOCK(vmh->mem);
1050 
1051 	*objp = vmh->mem;
1052 
1053 	return (0);
1054 }
1055 
1056 static struct cdevsw sgx_cdevsw = {
1057 	.d_version =		D_VERSION,
1058 	.d_ioctl =		sgx_ioctl,
1059 	.d_mmap_single =	sgx_mmap_single,
1060 	.d_name =		"Intel SGX",
1061 };
1062 
1063 static int
1064 sgx_get_epc_area(struct sgx_softc *sc)
1065 {
1066 	vm_offset_t epc_base_vaddr;
1067 	u_int cp[4];
1068 	int error;
1069 	int i;
1070 
1071 	cpuid_count(SGX_CPUID, 0x2, cp);
1072 
1073 	sc->epc_base = ((uint64_t)(cp[1] & 0xfffff) << 32) +
1074 	    (cp[0] & 0xfffff000);
1075 	sc->epc_size = ((uint64_t)(cp[3] & 0xfffff) << 32) +
1076 	    (cp[2] & 0xfffff000);
1077 	sc->npages = sc->epc_size / SGX_PAGE_SIZE;
1078 
1079 	if (sc->epc_size == 0 || sc->epc_base == 0) {
1080 		printf("%s: Incorrect EPC data: EPC base %lx, size %lu\n",
1081 		    __func__, sc->epc_base, sc->epc_size);
1082 		return (EINVAL);
1083 	}
1084 
1085 	if (cp[3] & 0xffff)
1086 		sc->enclave_size_max = (1 << ((cp[3] >> 8) & 0xff));
1087 	else
1088 		sc->enclave_size_max = SGX_ENCL_SIZE_MAX_DEF;
1089 
1090 	epc_base_vaddr = (vm_offset_t)pmap_mapdev_attr(sc->epc_base,
1091 	    sc->epc_size, VM_MEMATTR_DEFAULT);
1092 
1093 	sc->epc_pages = malloc(sizeof(struct epc_page) * sc->npages,
1094 	    M_DEVBUF, M_WAITOK | M_ZERO);
1095 
1096 	for (i = 0; i < sc->npages; i++) {
1097 		sc->epc_pages[i].base = epc_base_vaddr + SGX_PAGE_SIZE * i;
1098 		sc->epc_pages[i].phys = sc->epc_base + SGX_PAGE_SIZE * i;
1099 		sc->epc_pages[i].index = i;
1100 	}
1101 
1102 	sc->vmem_epc = vmem_create("SGX EPC", sc->epc_base, sc->epc_size,
1103 	    PAGE_SIZE, PAGE_SIZE, M_FIRSTFIT | M_WAITOK);
1104 	if (sc->vmem_epc == NULL) {
1105 		printf("%s: Can't create vmem arena.\n", __func__);
1106 		free(sc->epc_pages, M_SGX);
1107 		return (EINVAL);
1108 	}
1109 
1110 	error = vm_phys_fictitious_reg_range(sc->epc_base,
1111 	    sc->epc_base + sc->epc_size, VM_MEMATTR_DEFAULT);
1112 	if (error) {
1113 		printf("%s: Can't register fictitious space.\n", __func__);
1114 		free(sc->epc_pages, M_SGX);
1115 		return (EINVAL);
1116 	}
1117 
1118 	return (0);
1119 }
1120 
1121 static void
1122 sgx_put_epc_area(struct sgx_softc *sc)
1123 {
1124 
1125 	vm_phys_fictitious_unreg_range(sc->epc_base,
1126 	    sc->epc_base + sc->epc_size);
1127 
1128 	free(sc->epc_pages, M_SGX);
1129 }
1130 
1131 static int
1132 sgx_load(void)
1133 {
1134 	struct sgx_softc *sc;
1135 	int error;
1136 
1137 	sc = &sgx_sc;
1138 
1139 	if ((cpu_stdext_feature & CPUID_STDEXT_SGX) == 0)
1140 		return (ENXIO);
1141 
1142 	error = sgx_get_epc_area(sc);
1143 	if (error) {
1144 		printf("%s: Failed to get Processor Reserved Memory area.\n",
1145 		    __func__);
1146 		return (ENXIO);
1147 	}
1148 
1149 	mtx_init(&sc->mtx_encls, "SGX ENCLS", NULL, MTX_DEF);
1150 	mtx_init(&sc->mtx, "SGX driver", NULL, MTX_DEF);
1151 
1152 	TAILQ_INIT(&sc->enclaves);
1153 
1154 	sc->sgx_cdev = make_dev(&sgx_cdevsw, 0, UID_ROOT, GID_WHEEL,
1155 	    0600, "isgx");
1156 
1157 	sc->state |= SGX_STATE_RUNNING;
1158 
1159 	printf("SGX initialized: EPC base 0x%lx size %ld (%d pages)\n",
1160 	    sc->epc_base, sc->epc_size, sc->npages);
1161 
1162 	return (0);
1163 }
1164 
1165 static int
1166 sgx_unload(void)
1167 {
1168 	struct sgx_softc *sc;
1169 
1170 	sc = &sgx_sc;
1171 
1172 	if ((sc->state & SGX_STATE_RUNNING) == 0)
1173 		return (0);
1174 
1175 	mtx_lock(&sc->mtx);
1176 	if (!TAILQ_EMPTY(&sc->enclaves)) {
1177 		mtx_unlock(&sc->mtx);
1178 		return (EBUSY);
1179 	}
1180 	sc->state &= ~SGX_STATE_RUNNING;
1181 	mtx_unlock(&sc->mtx);
1182 
1183 	destroy_dev(sc->sgx_cdev);
1184 
1185 	vmem_destroy(sc->vmem_epc);
1186 	sgx_put_epc_area(sc);
1187 
1188 	mtx_destroy(&sc->mtx_encls);
1189 	mtx_destroy(&sc->mtx);
1190 
1191 	return (0);
1192 }
1193 
1194 static int
1195 sgx_handler(module_t mod, int what, void *arg)
1196 {
1197 	int error;
1198 
1199 	switch (what) {
1200 	case MOD_LOAD:
1201 		error = sgx_load();
1202 		break;
1203 	case MOD_UNLOAD:
1204 		error = sgx_unload();
1205 		break;
1206 	default:
1207 		error = 0;
1208 		break;
1209 	}
1210 
1211 	return (error);
1212 }
1213 
1214 static moduledata_t sgx_kmod = {
1215 	"sgx",
1216 	sgx_handler,
1217 	NULL
1218 };
1219 
1220 DECLARE_MODULE(sgx, sgx_kmod, SI_SUB_LAST, SI_ORDER_ANY);
1221 MODULE_VERSION(sgx, 1);
1222