1*2164af29SRuslan Bukin /*- 2*2164af29SRuslan Bukin * Copyright (c) 2017 Ruslan Bukin <br@bsdpad.com> 3*2164af29SRuslan Bukin * All rights reserved. 4*2164af29SRuslan Bukin * 5*2164af29SRuslan Bukin * This software was developed by BAE Systems, the University of Cambridge 6*2164af29SRuslan Bukin * Computer Laboratory, and Memorial University under DARPA/AFRL contract 7*2164af29SRuslan Bukin * FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent Computing 8*2164af29SRuslan Bukin * (TC) research program. 9*2164af29SRuslan Bukin * 10*2164af29SRuslan Bukin * Redistribution and use in source and binary forms, with or without 11*2164af29SRuslan Bukin * modification, are permitted provided that the following conditions 12*2164af29SRuslan Bukin * are met: 13*2164af29SRuslan Bukin * 1. Redistributions of source code must retain the above copyright 14*2164af29SRuslan Bukin * notice, this list of conditions and the following disclaimer. 15*2164af29SRuslan Bukin * 2. Redistributions in binary form must reproduce the above copyright 16*2164af29SRuslan Bukin * notice, this list of conditions and the following disclaimer in the 17*2164af29SRuslan Bukin * documentation and/or other materials provided with the distribution. 18*2164af29SRuslan Bukin * 19*2164af29SRuslan Bukin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20*2164af29SRuslan Bukin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21*2164af29SRuslan Bukin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22*2164af29SRuslan Bukin * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23*2164af29SRuslan Bukin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24*2164af29SRuslan Bukin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25*2164af29SRuslan Bukin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26*2164af29SRuslan Bukin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27*2164af29SRuslan Bukin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28*2164af29SRuslan Bukin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29*2164af29SRuslan Bukin * SUCH DAMAGE. 30*2164af29SRuslan Bukin */ 31*2164af29SRuslan Bukin 32*2164af29SRuslan Bukin /* Machine-defined variables. */ 33*2164af29SRuslan Bukin 34*2164af29SRuslan Bukin #ifndef _MACHINE_SGXREG_H_ 35*2164af29SRuslan Bukin #define _MACHINE_SGXREG_H_ 36*2164af29SRuslan Bukin 37*2164af29SRuslan Bukin /* Error codes. */ 38*2164af29SRuslan Bukin #define SGX_SUCCESS 0 39*2164af29SRuslan Bukin #define SGX_INVALID_SIG_STRUCT 1 /* EINIT */ 40*2164af29SRuslan Bukin #define SGX_INVALID_ATTRIBUTE 2 /* EINIT, EGETKEY */ 41*2164af29SRuslan Bukin #define SGX_BLSTATE 3 /* EBLOCK */ 42*2164af29SRuslan Bukin #define SGX_INVALID_MEASUREMENT 4 /* EINIT */ 43*2164af29SRuslan Bukin #define SGX_NOTBLOCKABLE 5 /* EBLOCK */ 44*2164af29SRuslan Bukin #define SGX_PG_INVLD 6 /* EBLOCK */ 45*2164af29SRuslan Bukin #define SGX_LOCKFAIL 7 /* EBLOCK, EMODPR, EMODT */ 46*2164af29SRuslan Bukin #define SGX_INVALID_SIGNATURE 8 /* EINIT */ 47*2164af29SRuslan Bukin #define SGX_MAC_COMPARE_FAIL 9 /* ELDB, ELDU */ 48*2164af29SRuslan Bukin #define SGX_PAGE_NOT_BLOCKED 10 /* EWB */ 49*2164af29SRuslan Bukin #define SGX_NOT_TRACKED 11 /* EWB, EACCEPT */ 50*2164af29SRuslan Bukin #define SGX_VA_SLOT_OCCUPIED 12 /* EWB */ 51*2164af29SRuslan Bukin #define SGX_CHILD_PRESENT 13 /* EWB, EREMOVE */ 52*2164af29SRuslan Bukin #define SGX_ENCLAVE_ACT 14 /* EREMOVE */ 53*2164af29SRuslan Bukin #define SGX_ENTRYEPOCH_LOCKED 15 /* EBLOCK */ 54*2164af29SRuslan Bukin #define SGX_INVALID_EINIT_TOKEN 16 /* EINIT */ 55*2164af29SRuslan Bukin #define SGX_PREV_TRK_INCMPL 17 /* ETRACK */ 56*2164af29SRuslan Bukin #define SGX_PG_IS_SECS 18 /* EBLOCK */ 57*2164af29SRuslan Bukin #define SGX_PAGE_ATTRIBUTES_MISMATCH 19 /* EACCEPT, EACCEPTCOPY */ 58*2164af29SRuslan Bukin #define SGX_PAGE_NOT_MODIFIABLE 20 /* EMODPR, EMODT */ 59*2164af29SRuslan Bukin #define SGX_INVALID_CPUSVN 32 /* EINIT, EGETKEY */ 60*2164af29SRuslan Bukin #define SGX_INVALID_ISVSVN 64 /* EGETKEY */ 61*2164af29SRuslan Bukin #define SGX_UNMASKED_EVENT 128 /* EINIT */ 62*2164af29SRuslan Bukin #define SGX_INVALID_KEYNAME 256 /* EGETKEY */ 63*2164af29SRuslan Bukin 64*2164af29SRuslan Bukin /* 65*2164af29SRuslan Bukin * 2.10 Page Information (PAGEINFO) 66*2164af29SRuslan Bukin * PAGEINFO is an architectural data structure that is used as a parameter 67*2164af29SRuslan Bukin * to the EPC-management instructions. It requires 32-Byte alignment. 68*2164af29SRuslan Bukin */ 69*2164af29SRuslan Bukin struct page_info { 70*2164af29SRuslan Bukin uint64_t linaddr; 71*2164af29SRuslan Bukin uint64_t srcpge; 72*2164af29SRuslan Bukin union { 73*2164af29SRuslan Bukin struct secinfo *secinfo; 74*2164af29SRuslan Bukin uint64_t pcmd; 75*2164af29SRuslan Bukin }; 76*2164af29SRuslan Bukin uint64_t secs; 77*2164af29SRuslan Bukin } __aligned(32); 78*2164af29SRuslan Bukin 79*2164af29SRuslan Bukin /* 80*2164af29SRuslan Bukin * 2.11 Security Information (SECINFO) 81*2164af29SRuslan Bukin * The SECINFO data structure holds meta-data about an enclave page. 82*2164af29SRuslan Bukin */ 83*2164af29SRuslan Bukin struct secinfo { 84*2164af29SRuslan Bukin uint64_t flags; 85*2164af29SRuslan Bukin #define SECINFO_FLAGS_PT_S 8 /* Page type shift */ 86*2164af29SRuslan Bukin #define SECINFO_FLAGS_PT_M (0xff << SECINFO_FLAGS_PT_S) 87*2164af29SRuslan Bukin uint64_t reserved[7]; 88*2164af29SRuslan Bukin } __aligned(64); 89*2164af29SRuslan Bukin 90*2164af29SRuslan Bukin /* 91*2164af29SRuslan Bukin * 2.7.1 ATTRIBUTES 92*2164af29SRuslan Bukin * The ATTRIBUTES data structure is comprised of bit-granular fields that 93*2164af29SRuslan Bukin * are used in the SECS, CPUID enumeration, the REPORT and the KEYREQUEST 94*2164af29SRuslan Bukin * structures. 95*2164af29SRuslan Bukin */ 96*2164af29SRuslan Bukin struct secs_attr { 97*2164af29SRuslan Bukin uint8_t reserved1: 1; 98*2164af29SRuslan Bukin uint8_t debug: 1; 99*2164af29SRuslan Bukin uint8_t mode64bit: 1; 100*2164af29SRuslan Bukin uint8_t reserved2: 1; 101*2164af29SRuslan Bukin uint8_t provisionkey: 1; 102*2164af29SRuslan Bukin uint8_t einittokenkey: 1; 103*2164af29SRuslan Bukin uint8_t reserved3: 2; 104*2164af29SRuslan Bukin #define SECS_ATTR_RSV4_SIZE 7 105*2164af29SRuslan Bukin uint8_t reserved4[SECS_ATTR_RSV4_SIZE]; 106*2164af29SRuslan Bukin uint64_t xfrm; /* X-Feature Request Mask */ 107*2164af29SRuslan Bukin }; 108*2164af29SRuslan Bukin 109*2164af29SRuslan Bukin /* 110*2164af29SRuslan Bukin * 2.7 SGX Enclave Control Structure (SECS) 111*2164af29SRuslan Bukin * The SECS data structure requires 4K-Bytes alignment. 112*2164af29SRuslan Bukin */ 113*2164af29SRuslan Bukin struct secs { 114*2164af29SRuslan Bukin uint64_t size; 115*2164af29SRuslan Bukin uint64_t base; 116*2164af29SRuslan Bukin uint32_t ssa_frame_size; 117*2164af29SRuslan Bukin uint32_t misc_select; 118*2164af29SRuslan Bukin #define SECS_RSV1_SIZE 24 119*2164af29SRuslan Bukin uint8_t reserved1[SECS_RSV1_SIZE]; 120*2164af29SRuslan Bukin struct secs_attr attributes; 121*2164af29SRuslan Bukin uint8_t mr_enclave[32]; 122*2164af29SRuslan Bukin #define SECS_RSV2_SIZE 32 123*2164af29SRuslan Bukin uint8_t reserved2[SECS_RSV2_SIZE]; 124*2164af29SRuslan Bukin uint8_t mr_signer[32]; 125*2164af29SRuslan Bukin #define SECS_RSV3_SIZE 96 126*2164af29SRuslan Bukin uint8_t reserved3[SECS_RSV3_SIZE]; 127*2164af29SRuslan Bukin uint16_t isv_prod_id; 128*2164af29SRuslan Bukin uint16_t isv_svn; 129*2164af29SRuslan Bukin #define SECS_RSV4_SIZE 3836 130*2164af29SRuslan Bukin uint8_t reserved4[SECS_RSV4_SIZE]; 131*2164af29SRuslan Bukin }; 132*2164af29SRuslan Bukin 133*2164af29SRuslan Bukin /* 134*2164af29SRuslan Bukin * 2.8 Thread Control Structure (TCS) 135*2164af29SRuslan Bukin * Each executing thread in the enclave is associated with a 136*2164af29SRuslan Bukin * Thread Control Structure. It requires 4K-Bytes alignment. 137*2164af29SRuslan Bukin */ 138*2164af29SRuslan Bukin struct tcs { 139*2164af29SRuslan Bukin uint64_t reserved1; 140*2164af29SRuslan Bukin uint64_t flags; 141*2164af29SRuslan Bukin uint64_t ossa; 142*2164af29SRuslan Bukin uint32_t cssa; 143*2164af29SRuslan Bukin uint32_t nssa; 144*2164af29SRuslan Bukin uint64_t oentry; 145*2164af29SRuslan Bukin uint64_t reserved2; 146*2164af29SRuslan Bukin uint64_t ofsbasgx; 147*2164af29SRuslan Bukin uint64_t ogsbasgx; 148*2164af29SRuslan Bukin uint32_t fslimit; 149*2164af29SRuslan Bukin uint32_t gslimit; 150*2164af29SRuslan Bukin uint64_t reserved3[503]; 151*2164af29SRuslan Bukin }; 152*2164af29SRuslan Bukin 153*2164af29SRuslan Bukin #endif /* !_MACHINE_SGXREG_H_ */ 154