sgx_arch.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. 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 struct {
  187. uint64_t cpusvn[2];
  188. uint32_t miscselect;
  189. uint8_t reserved[28];
  190. sgx_arch_attributes_t attributes;
  191. sgx_arch_hash_t mrenclave;
  192. uint8_t reserved2[32];
  193. sgx_arch_hash_t mrsigner;
  194. uint8_t reserved3[96];
  195. uint16_t isvprodid, isvsvn;
  196. uint8_t reserved4[60];
  197. uint8_t report_data[64];
  198. uint8_t keyid[32];
  199. sgx_arch_mac_t mac;
  200. } __attribute__((packed, aligned(512))) sgx_arch_report_t;
  201. #define SGX_REPORT_SIGNED_SIZE 384
  202. typedef struct {
  203. sgx_arch_hash_t mrenclave;
  204. sgx_arch_attributes_t attributes;
  205. uint8_t reserved[464];
  206. } __attribute__((packed, aligned(512))) sgx_arch_targetinfo_t;
  207. typedef struct {
  208. uint16_t keyname;
  209. uint16_t keypolicy;
  210. uint16_t isvsvn;
  211. uint8_t reserved[2];
  212. uint64_t cpusvn[2];
  213. sgx_arch_attributes_t attributes_mask;
  214. uint8_t keyid[32];
  215. uint32_t miscmask;
  216. uint8_t reserved2[436];
  217. } __attribute__((packed, aligned(512))) sgx_arch_keyrequest_t;
  218. typedef uint8_t sgx_arch_key128_t[16] __attribute__((aligned(16)));
  219. #else /* !__ASSEMBLER__ */
  220. /* microcode to call ENCLU */
  221. .macro ENCLU
  222. .byte 0x0f, 0x01, 0xd7
  223. .endm
  224. #endif
  225. #define EENTER 2
  226. #define ERESUME 3
  227. #define EDBGRD 4
  228. #define EDBGWR 5
  229. #define EREPORT 0
  230. #define EGETKEY 1
  231. #define EEXIT 4
  232. #define LAUNCH_KEY 0
  233. #define PROVISION_KEY 1
  234. #define PROVISION_SEAL_KEY 2
  235. #define REPORT_KEY 3
  236. #define SEAL_KEY 4
  237. #define KEYPOLICY_MRENCLAVE 1
  238. #define KEYPOLICY_MRSIGNER 2
  239. #define XSAVE_SIZE 512
  240. #define STACK_ALIGN 0xfffffffffffffff0
  241. #define XSAVE_ALIGN 0xffffffffffffffc0
  242. #define XSAVE_NON_FX_MASK 0xfffffffffffffffc
  243. #define RETURN_FROM_OCALL 0xffffffffffffffff
  244. #endif /* SGX_ARCH_H */