1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright(c) 2016-20 Intel Corporation. */ 3 4 #include <stddef.h> 5 #include "defines.h" 6 7 /* 8 * Data buffer spanning two pages that will be placed first in .data 9 * segment. Even if not used internally the second page is needed by 10 * external test manipulating page permissions. 11 */ 12 static uint8_t encl_buffer[8192] = { 1 }; 13 14 enum sgx_enclu_function { 15 EACCEPT = 0x5, 16 EMODPE = 0x6, 17 }; 18 19 static void do_encl_emodpe(void *_op) 20 { 21 struct sgx_secinfo secinfo __aligned(sizeof(struct sgx_secinfo)) = {0}; 22 struct encl_op_emodpe *op = _op; 23 24 secinfo.flags = op->flags; 25 26 asm volatile(".byte 0x0f, 0x01, 0xd7" 27 : 28 : "a" (EMODPE), 29 "b" (&secinfo), 30 "c" (op->epc_addr)); 31 } 32 33 static void do_encl_eaccept(void *_op) 34 { 35 struct sgx_secinfo secinfo __aligned(sizeof(struct sgx_secinfo)) = {0}; 36 struct encl_op_eaccept *op = _op; 37 int rax; 38 39 secinfo.flags = op->flags; 40 41 asm volatile(".byte 0x0f, 0x01, 0xd7" 42 : "=a" (rax) 43 : "a" (EACCEPT), 44 "b" (&secinfo), 45 "c" (op->epc_addr)); 46 47 op->ret = rax; 48 } 49 50 static void *memcpy(void *dest, const void *src, size_t n) 51 { 52 size_t i; 53 54 for (i = 0; i < n; i++) 55 ((char *)dest)[i] = ((char *)src)[i]; 56 57 return dest; 58 } 59 60 static void *memset(void *dest, int c, size_t n) 61 { 62 size_t i; 63 64 for (i = 0; i < n; i++) 65 ((char *)dest)[i] = c; 66 67 return dest; 68 } 69 70 static void do_encl_init_tcs_page(void *_op) 71 { 72 struct encl_op_init_tcs_page *op = _op; 73 void *tcs = (void *)op->tcs_page; 74 uint32_t val_32; 75 76 memset(tcs, 0, 16); /* STATE and FLAGS */ 77 memcpy(tcs + 16, &op->ssa, 8); /* OSSA */ 78 memset(tcs + 24, 0, 4); /* CSSA */ 79 val_32 = 1; 80 memcpy(tcs + 28, &val_32, 4); /* NSSA */ 81 memcpy(tcs + 32, &op->entry, 8); /* OENTRY */ 82 memset(tcs + 40, 0, 24); /* AEP, OFSBASE, OGSBASE */ 83 val_32 = 0xFFFFFFFF; 84 memcpy(tcs + 64, &val_32, 4); /* FSLIMIT */ 85 memcpy(tcs + 68, &val_32, 4); /* GSLIMIT */ 86 memset(tcs + 72, 0, 4024); /* Reserved */ 87 } 88 89 static void do_encl_op_put_to_buf(void *op) 90 { 91 struct encl_op_put_to_buf *op2 = op; 92 93 memcpy(&encl_buffer[0], &op2->value, 8); 94 } 95 96 static void do_encl_op_get_from_buf(void *op) 97 { 98 struct encl_op_get_from_buf *op2 = op; 99 100 memcpy(&op2->value, &encl_buffer[0], 8); 101 } 102 103 static void do_encl_op_put_to_addr(void *_op) 104 { 105 struct encl_op_put_to_addr *op = _op; 106 107 memcpy((void *)op->addr, &op->value, 8); 108 } 109 110 static void do_encl_op_get_from_addr(void *_op) 111 { 112 struct encl_op_get_from_addr *op = _op; 113 114 memcpy(&op->value, (void *)op->addr, 8); 115 } 116 117 static void do_encl_op_nop(void *_op) 118 { 119 120 } 121 122 void encl_body(void *rdi, void *rsi) 123 { 124 const void (*encl_op_array[ENCL_OP_MAX])(void *) = { 125 do_encl_op_put_to_buf, 126 do_encl_op_get_from_buf, 127 do_encl_op_put_to_addr, 128 do_encl_op_get_from_addr, 129 do_encl_op_nop, 130 do_encl_eaccept, 131 do_encl_emodpe, 132 do_encl_init_tcs_page, 133 }; 134 135 struct encl_op_header *op = (struct encl_op_header *)rdi; 136 137 if (op->type < ENCL_OP_MAX) 138 (*encl_op_array[op->type])(op); 139 } 140