sgx_arch.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #ifndef SGX_ARCH_H
  16. #define SGX_ARCH_H
  17. #include "pal_linux_defs.h"
  18. #ifndef __ASSEMBLER__
  19. #include <stdint.h>
  20. typedef uint8_t sgx_arch_key_t [384];
  21. typedef uint8_t sgx_arch_hash_t[32];
  22. typedef uint8_t sgx_arch_mac_t [16];
  23. // This if for passing a mac to hex2str
  24. #define MACBUF_SIZE ((sizeof(sgx_arch_mac_t)*2)+1)
  25. typedef struct {
  26. uint64_t flags, xfrm;
  27. } sgx_arch_attributes_t;
  28. #define SGX_FLAGS_INITIALIZED 0x01ULL
  29. #define SGX_FLAGS_DEBUG 0x02ULL
  30. #define SGX_FLAGS_MODE64BIT 0x04ULL
  31. #define SGX_FLAGS_PROVISION_KEY 0x10ULL
  32. #define SGX_FLAGS_LICENSE_KEY 0x20ULL
  33. #define SGX_XFRM_LEGACY 0x03ULL
  34. #define SGX_XFRM_AVX 0x06ULL
  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. #if SGX_HAS_FSGSBASE == 1
  88. uint64_t fsbase;
  89. uint64_t gsbase;
  90. #endif
  91. } sgx_arch_gpr_t;
  92. typedef struct {
  93. uint64_t rax;
  94. uint64_t rcx;
  95. uint64_t rdx;
  96. uint64_t rbx;
  97. uint64_t rsp;
  98. uint64_t rbp;
  99. uint64_t rsi;
  100. uint64_t rdi;
  101. uint64_t r8;
  102. uint64_t r9;
  103. uint64_t r10;
  104. uint64_t r11;
  105. uint64_t r12;
  106. uint64_t r13;
  107. uint64_t r14;
  108. uint64_t r15;
  109. uint64_t rflags;
  110. uint64_t rip;
  111. } sgx_context_t;
  112. typedef struct {
  113. uint32_t vector:8;
  114. uint32_t type:3;
  115. uint32_t reserved:20;
  116. uint32_t valid:1;
  117. } sgx_arch_exitinfo_t;
  118. #define SGX_EXCEPTION_HARDWARE 3UL
  119. #define SGX_EXCEPTION_SOFTWARE 6UL
  120. #define SGX_EXCEPTION_VECTOR_DE 0UL /* DIV and IDIV instructions */
  121. #define SGX_EXCEPTION_VECTOR_DB 1UL /* For Intel use only */
  122. #define SGX_EXCEPTION_VECTOR_BP 3UL /* INT 3 instruction */
  123. #define SGX_EXCEPTION_VECTOR_BR 5UL /* BOUND instruction */
  124. #define SGX_EXCEPTION_VECTOR_UD 6UL /* UD2 instruction or reserved opcodes */
  125. #define SGX_EXCEPTION_VECTOR_MF 16UL /* x87 FPU floating-point or WAIT/FWAIT instruction */
  126. #define SGX_EXCEPTION_VECTOR_AC 17UL /* Any data reference in memory */
  127. #define SGX_EXCEPTION_VECTOR_XM 19UL /* Any SIMD floating-point exceptions */
  128. typedef struct {
  129. uint64_t linaddr;
  130. uint64_t srcpge;
  131. uint64_t secinfo;
  132. uint64_t secs;
  133. } sgx_arch_pageinfo_t;
  134. typedef struct {
  135. uint64_t flags;
  136. uint8_t reserved[56];
  137. } sgx_arch_secinfo_t;
  138. #define SGX_SECINFO_FLAGS_R 0x001
  139. #define SGX_SECINFO_FLAGS_W 0x002
  140. #define SGX_SECINFO_FLAGS_X 0x004
  141. #define SGX_SECINFO_FLAGS_SECS 0x000
  142. #define SGX_SECINFO_FLAGS_TCS 0x100
  143. #define SGX_SECINFO_FLAGS_REG 0x200
  144. typedef struct {
  145. /* header part (signed) */
  146. uint32_t header[4], vendor;
  147. uint32_t date;
  148. uint32_t header2[4];
  149. uint32_t swdefined;
  150. uint8_t reserved1[84];
  151. /* key part (unsigned) */
  152. sgx_arch_key_t modulus;
  153. uint32_t exponent;
  154. sgx_arch_key_t signature;
  155. /* body part (signed) */
  156. uint32_t miscselect, miscselect_mask;
  157. uint8_t reserved2[20];
  158. sgx_arch_attributes_t attributes, attribute_mask;
  159. sgx_arch_hash_t enclave_hash;
  160. uint8_t reserved3[32];
  161. uint16_t isvprodid, isvsvn;
  162. /* tail part (unsigned) */
  163. uint8_t reserved4[12];
  164. sgx_arch_key_t q1, q2;
  165. } __attribute__((packed)) sgx_arch_sigstruct_t;
  166. typedef struct {
  167. uint32_t valid;
  168. uint8_t reserved[44];
  169. sgx_arch_attributes_t attributes;
  170. sgx_arch_hash_t mrenclave;
  171. uint8_t reserved2[32];
  172. sgx_arch_hash_t mrsigner;
  173. uint8_t reserved3[32];
  174. uint64_t cpusvnle[2];
  175. uint16_t isvprodidle, isvsvnle;
  176. uint8_t reserved4[24];
  177. uint32_t miscselect_mask;
  178. sgx_arch_attributes_t attribute_mask;
  179. sgx_arch_hash_t keyid;
  180. sgx_arch_mac_t mac;
  181. } __attribute__((packed)) sgx_arch_token_t;
  182. typedef struct {
  183. uint64_t cpusvn[2];
  184. uint32_t miscselect;
  185. uint8_t reserved[28];
  186. sgx_arch_attributes_t attributes;
  187. sgx_arch_hash_t mrenclave;
  188. uint8_t reserved2[32];
  189. sgx_arch_hash_t mrsigner;
  190. uint8_t reserved3[96];
  191. uint16_t isvprodid, isvsvn;
  192. uint8_t reserved4[60];
  193. uint8_t report_data[64];
  194. uint8_t keyid[32];
  195. sgx_arch_mac_t mac;
  196. } __attribute__((packed, aligned(512))) sgx_arch_report_t;
  197. #define SGX_REPORT_SIGNED_SIZE 384
  198. typedef struct {
  199. sgx_arch_hash_t mrenclave;
  200. sgx_arch_attributes_t attributes;
  201. uint8_t reserved[464];
  202. } __attribute__((packed, aligned(512))) sgx_arch_targetinfo_t;
  203. typedef struct {
  204. uint16_t keyname;
  205. uint16_t keypolicy;
  206. uint16_t isvsvn;
  207. uint8_t reserved[2];
  208. uint64_t cpusvn[2];
  209. sgx_arch_attributes_t attributes_mask;
  210. uint8_t keyid[32];
  211. uint32_t miscmask;
  212. uint8_t reserved2[436];
  213. } __attribute__((packed, aligned(512))) sgx_arch_keyrequest_t;
  214. typedef uint8_t sgx_arch_key128_t[16] __attribute__((aligned(16)));
  215. #else /* !__ASSEMBLER__ */
  216. /* microcode to call ENCLU */
  217. .macro ENCLU
  218. .byte 0x0f, 0x01, 0xd7
  219. .endm
  220. #endif
  221. #define EENTER 2
  222. #define ERESUME 3
  223. #define EDBGRD 4
  224. #define EDBGWR 5
  225. #define EREPORT 0
  226. #define EGETKEY 1
  227. #define EEXIT 4
  228. #define LAUNCH_KEY 0
  229. #define PROVISION_KEY 1
  230. #define PROVISION_SEAL_KEY 2
  231. #define REPORT_KEY 3
  232. #define SEAL_KEY 4
  233. #define KEYPOLICY_MRENCLAVE 1
  234. #define KEYPOLICY_MRSIGNER 2
  235. #define SGX_GPR_RAX 0x00
  236. #define SGX_GPR_RCX 0x08
  237. #define SGX_GPR_RDX 0x10
  238. #define SGX_GPR_RBX 0x18
  239. #define SGX_GPR_RSP 0x20
  240. #define SGX_GPR_RBP 0x28
  241. #define SGX_GPR_RSI 0x30
  242. #define SGX_GPR_RDI 0x38
  243. #define SGX_GPR_R8 0x40
  244. #define SGX_GPR_R9 0x48
  245. #define SGX_GPR_R10 0x50
  246. #define SGX_GPR_R11 0x58
  247. #define SGX_GPR_R12 0x60
  248. #define SGX_GPR_R13 0x68
  249. #define SGX_GPR_R14 0x70
  250. #define SGX_GPR_R15 0x78
  251. #define SGX_GPR_RFLAGS 0x80
  252. #define SGX_GPR_RIP 0x88
  253. #define SGX_GPR_EXITINFO 0xa0
  254. #define TCS_SIZE 4096
  255. #define TCS_SHIFT 12
  256. #define XSAVE_SIZE 512
  257. #define STACK_ALIGN 0xfffffffffffffff0
  258. #define XSAVE_ALIGN 0xffffffffffffffc0
  259. #define RETURN_FROM_OCALL 0xffffffffffffffff
  260. #endif /* SGX_ARCH_H */