sgx_arch.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #ifndef SGX_ARCH_H
  14. #define SGX_ARCH_H
  15. #include "pal_linux_defs.h"
  16. #ifndef __ASSEMBLER__
  17. #include <stdint.h>
  18. typedef uint8_t sgx_arch_key_t [384];
  19. typedef uint8_t sgx_arch_hash_t[32];
  20. typedef uint8_t sgx_arch_mac_t [16];
  21. // This if for passing a mac to hex2str
  22. #define MACBUF_SIZE ((sizeof(sgx_arch_mac_t) * 2) + 1)
  23. typedef struct {
  24. uint64_t flags, xfrm;
  25. } sgx_arch_attributes_t;
  26. #define SGX_FLAGS_INITIALIZED 0x01ULL
  27. #define SGX_FLAGS_DEBUG 0x02ULL
  28. #define SGX_FLAGS_MODE64BIT 0x04ULL
  29. #define SGX_FLAGS_PROVISION_KEY 0x10ULL
  30. #define SGX_FLAGS_LICENSE_KEY 0x20ULL
  31. #define SGX_XFRM_LEGACY 0x03ULL
  32. #define SGX_XFRM_AVX 0x06ULL
  33. #define SGX_XFRM_MPX 0x18ULL
  34. #define SGX_XFRM_AVX512 0xe6ULL
  35. #define SGX_MISCSELECT_EXINFO 0x01UL
  36. typedef struct {
  37. uint64_t size, baseaddr;
  38. uint32_t ssaframesize;
  39. uint32_t miscselect;
  40. uint8_t reserved[24];
  41. sgx_arch_attributes_t attributes;
  42. sgx_arch_hash_t mrenclave;
  43. uint8_t reserved2[32];
  44. sgx_arch_hash_t mrsigner;
  45. uint8_t reserved3[96];
  46. uint16_t isvprodid, isvsvn;
  47. uint8_t reserved4[3836];
  48. } sgx_arch_secs_t;
  49. typedef struct {
  50. uint64_t reserved;
  51. uint64_t flags;
  52. uint64_t ossa;
  53. uint32_t cssa;
  54. uint32_t nssa;
  55. uint64_t oentry;
  56. uint64_t reserved2;
  57. uint64_t ofsbasgx;
  58. uint64_t ogsbasgx;
  59. uint32_t fslimit;
  60. uint32_t gslimit;
  61. uint8_t reserved3[4024];
  62. } sgx_arch_tcs_t;
  63. #define TCS_FLAGS_DBGOPTIN (01ULL)
  64. typedef struct {
  65. uint64_t rax;
  66. uint64_t rcx;
  67. uint64_t rdx;
  68. uint64_t rbx;
  69. uint64_t rsp;
  70. uint64_t rbp;
  71. uint64_t rsi;
  72. uint64_t rdi;
  73. uint64_t r8;
  74. uint64_t r9;
  75. uint64_t r10;
  76. uint64_t r11;
  77. uint64_t r12;
  78. uint64_t r13;
  79. uint64_t r14;
  80. uint64_t r15;
  81. uint64_t rflags;
  82. uint64_t rip;
  83. uint64_t ursp;
  84. uint64_t urbp;
  85. uint32_t exitinfo;
  86. uint32_t reserved;
  87. uint64_t fsbase;
  88. uint64_t gsbase;
  89. } sgx_arch_gpr_t;
  90. typedef struct {
  91. uint64_t rax;
  92. uint64_t rcx;
  93. uint64_t rdx;
  94. uint64_t rbx;
  95. uint64_t rsp;
  96. uint64_t rbp;
  97. uint64_t rsi;
  98. uint64_t rdi;
  99. uint64_t r8;
  100. uint64_t r9;
  101. uint64_t r10;
  102. uint64_t r11;
  103. uint64_t r12;
  104. uint64_t r13;
  105. uint64_t r14;
  106. uint64_t r15;
  107. uint64_t rflags;
  108. uint64_t rip;
  109. } sgx_context_t;
  110. // Required by _restore_sgx_context, see enclave_entry.S.
  111. _Static_assert(offsetof(sgx_context_t, rip) - offsetof(sgx_context_t, rflags) ==
  112. sizeof(((sgx_context_t) {0}).rflags),
  113. "rip must be directly after rflags in sgx_context_t");
  114. _Static_assert(offsetof(sgx_context_t, rflags) - offsetof(sgx_context_t, rdi) <= RED_ZONE_SIZE,
  115. "rdi needs to be within red zone distance from rflags");
  116. typedef struct {
  117. uint32_t vector:8;
  118. uint32_t type:3;
  119. uint32_t reserved:20;
  120. uint32_t valid:1;
  121. } sgx_arch_exitinfo_t;
  122. #define SGX_EXCEPTION_HARDWARE 3UL
  123. #define SGX_EXCEPTION_SOFTWARE 6UL
  124. #define SGX_EXCEPTION_VECTOR_DE 0UL /* DIV and IDIV instructions */
  125. #define SGX_EXCEPTION_VECTOR_DB 1UL /* For Intel use only */
  126. #define SGX_EXCEPTION_VECTOR_BP 3UL /* INT 3 instruction */
  127. #define SGX_EXCEPTION_VECTOR_BR 5UL /* BOUND instruction */
  128. #define SGX_EXCEPTION_VECTOR_UD 6UL /* UD2 instruction or reserved opcodes */
  129. #define SGX_EXCEPTION_VECTOR_MF 16UL /* x87 FPU floating-point or WAIT/FWAIT instruction */
  130. #define SGX_EXCEPTION_VECTOR_AC 17UL /* Any data reference in memory */
  131. #define SGX_EXCEPTION_VECTOR_XM 19UL /* Any SIMD floating-point exceptions */
  132. typedef struct {
  133. uint64_t linaddr;
  134. uint64_t srcpge;
  135. uint64_t secinfo;
  136. uint64_t secs;
  137. } sgx_arch_pageinfo_t;
  138. typedef struct {
  139. uint64_t flags;
  140. uint8_t reserved[56];
  141. } sgx_arch_secinfo_t;
  142. #define SGX_SECINFO_FLAGS_R 0x001
  143. #define SGX_SECINFO_FLAGS_W 0x002
  144. #define SGX_SECINFO_FLAGS_X 0x004
  145. #define SGX_SECINFO_FLAGS_SECS 0x000
  146. #define SGX_SECINFO_FLAGS_TCS 0x100
  147. #define SGX_SECINFO_FLAGS_REG 0x200
  148. typedef struct {
  149. /* header part (signed) */
  150. uint32_t header[4], vendor;
  151. uint32_t date;
  152. uint32_t header2[4];
  153. uint32_t swdefined;
  154. uint8_t reserved1[84];
  155. /* key part (unsigned) */
  156. sgx_arch_key_t modulus;
  157. uint32_t exponent;
  158. sgx_arch_key_t signature;
  159. /* body part (signed) */
  160. uint32_t miscselect, miscselect_mask;
  161. uint8_t reserved2[20];
  162. sgx_arch_attributes_t attributes, attribute_mask;
  163. sgx_arch_hash_t enclave_hash;
  164. uint8_t reserved3[32];
  165. uint16_t isvprodid, isvsvn;
  166. /* tail part (unsigned) */
  167. uint8_t reserved4[12];
  168. sgx_arch_key_t q1, q2;
  169. } __attribute__((packed)) sgx_arch_sigstruct_t;
  170. typedef struct {
  171. uint32_t valid;
  172. uint8_t reserved[44];
  173. sgx_arch_attributes_t attributes;
  174. sgx_arch_hash_t mrenclave;
  175. uint8_t reserved2[32];
  176. sgx_arch_hash_t mrsigner;
  177. uint8_t reserved3[32];
  178. uint64_t cpusvnle[2];
  179. uint16_t isvprodidle, isvsvnle;
  180. uint8_t reserved4[24];
  181. uint32_t miscselect_mask;
  182. sgx_arch_attributes_t attribute_mask;
  183. sgx_arch_hash_t keyid;
  184. sgx_arch_mac_t mac;
  185. } __attribute__((packed)) sgx_arch_token_t;
  186. typedef uint8_t sgx_arch_report_data_t[64];
  187. #define __sgx_mem_aligned __attribute__((aligned(512)))
  188. typedef struct {
  189. uint64_t cpusvn[2];
  190. uint32_t miscselect;
  191. uint8_t reserved[28];
  192. sgx_arch_attributes_t attributes;
  193. sgx_arch_hash_t mrenclave;
  194. uint8_t reserved2[32];
  195. sgx_arch_hash_t mrsigner;
  196. uint8_t reserved3[96];
  197. uint16_t isvprodid, isvsvn;
  198. uint8_t reserved4[60];
  199. sgx_arch_report_data_t report_data;
  200. } __attribute__((packed)) sgx_arch_report_body_t;
  201. typedef struct {
  202. sgx_arch_report_body_t body;
  203. uint8_t keyid[32];
  204. sgx_arch_mac_t mac;
  205. } __attribute__((packed)) sgx_arch_report_t;
  206. #define SGX_REPORT_SIGNED_SIZE 384
  207. #define SGX_REPORT_ACTUAL_SIZE 432
  208. typedef struct {
  209. sgx_arch_hash_t mrenclave;
  210. sgx_arch_attributes_t attributes;
  211. uint8_t reserved[4];
  212. uint32_t miscselect;
  213. uint8_t reserved2[456];
  214. } __attribute__((packed)) sgx_arch_targetinfo_t;
  215. #define SGX_TARGETINFO_FILLED_SIZE (sizeof(sgx_arch_hash_t) + \
  216. sizeof(sgx_arch_attributes_t))
  217. typedef struct {
  218. uint16_t keyname;
  219. uint16_t keypolicy;
  220. uint16_t isvsvn;
  221. uint8_t reserved[2];
  222. uint64_t cpusvn[2];
  223. sgx_arch_attributes_t attributes_mask;
  224. uint8_t keyid[32];
  225. uint32_t miscmask;
  226. uint8_t reserved2[436];
  227. } __attribute__((packed)) sgx_arch_keyrequest_t;
  228. typedef uint8_t sgx_arch_key128_t[16];
  229. #define ENCLU ".byte 0x0f, 0x01, 0xd7"
  230. #else /* !__ASSEMBLER__ */
  231. /* microcode to call ENCLU */
  232. .macro ENCLU
  233. .byte 0x0f, 0x01, 0xd7
  234. .endm
  235. #endif
  236. #define EENTER 2
  237. #define ERESUME 3
  238. #define EDBGRD 4
  239. #define EDBGWR 5
  240. #define EREPORT 0
  241. #define EGETKEY 1
  242. #define EEXIT 4
  243. #define LAUNCH_KEY 0
  244. #define PROVISION_KEY 1
  245. #define PROVISION_SEAL_KEY 2
  246. #define REPORT_KEY 3
  247. #define SEAL_KEY 4
  248. #define KEYPOLICY_MRENCLAVE 1
  249. #define KEYPOLICY_MRSIGNER 2
  250. #define XSAVE_SIZE 512
  251. #define STACK_ALIGN 0xfffffffffffffff0
  252. #define XSAVE_ALIGN 0xffffffffffffffc0
  253. #define XSAVE_NON_FX_MASK 0xfffffffffffffffc
  254. #define RETURN_FROM_OCALL 0xffffffffffffffff
  255. #define RFLAGS_DF (1<<10)
  256. #endif /* SGX_ARCH_H */