sgx_arch.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. #include "assert.h"
  19. #pragma pack(push, 1)
  20. #define SE_KEY_SIZE 384
  21. #define SE_EXPONENT_SIZE 4
  22. #define SGX_HASH_SIZE 32
  23. #define SGX_MAC_SIZE 16
  24. typedef struct _sgx_measurement_t {
  25. uint8_t m[SGX_HASH_SIZE];
  26. } sgx_measurement_t;
  27. typedef uint8_t sgx_mac_t[SGX_MAC_SIZE];
  28. // This if for passing a mac to hex2str
  29. #define MACBUF_SIZE ((sizeof(sgx_mac_t) * 2) + 1)
  30. typedef struct _sgx_attributes_t {
  31. uint64_t flags;
  32. uint64_t xfrm;
  33. } sgx_attributes_t;
  34. #define SGX_CPUSVN_SIZE 16
  35. #define SGX_CONFIGID_SIZE 64
  36. #define SGX_KEYID_SIZE 32
  37. #define SGX_REPORT_DATA_SIZE 64
  38. typedef struct _sgx_cpu_svn_t {
  39. uint8_t svn[SGX_CPUSVN_SIZE];
  40. } sgx_cpu_svn_t;
  41. typedef uint32_t sgx_misc_select_t;
  42. typedef uint16_t sgx_prod_id_t;
  43. typedef uint16_t sgx_isv_svn_t;
  44. typedef uint16_t sgx_config_svn_t;
  45. typedef uint8_t sgx_config_id_t[SGX_CONFIGID_SIZE];
  46. #define SGX_ISVEXT_PROD_ID_SIZE 16
  47. #define SGX_ISV_FAMILY_ID_SIZE 16
  48. typedef uint8_t sgx_isvext_prod_id_t[SGX_ISVEXT_PROD_ID_SIZE];
  49. typedef uint8_t sgx_isvfamily_id_t[SGX_ISV_FAMILY_ID_SIZE];
  50. #define SGX_FLAGS_INITIALIZED 0x01ULL
  51. #define SGX_FLAGS_DEBUG 0x02ULL
  52. #define SGX_FLAGS_MODE64BIT 0x04ULL
  53. #define SGX_FLAGS_PROVISION_KEY 0x10ULL
  54. #define SGX_FLAGS_LICENSE_KEY 0x20ULL
  55. #define SGX_XFRM_LEGACY 0x03ULL
  56. #define SGX_XFRM_AVX 0x06ULL
  57. #define SGX_XFRM_MPX 0x18ULL
  58. #define SGX_XFRM_AVX512 0xe6ULL
  59. #define SGX_MISCSELECT_EXINFO 0x01UL
  60. typedef struct {
  61. uint64_t size;
  62. uint64_t base;
  63. uint32_t ssa_frame_size;
  64. sgx_misc_select_t misc_select;
  65. uint8_t reserved1[24];
  66. sgx_attributes_t attributes;
  67. sgx_measurement_t mr_enclave;
  68. uint8_t reserved2[32];
  69. sgx_measurement_t mr_signer;
  70. uint8_t reserved3[32];
  71. sgx_config_id_t config_id;
  72. sgx_prod_id_t isv_prod_id;
  73. sgx_isv_svn_t isv_svn;
  74. sgx_config_svn_t config_svn;
  75. uint8_t reserved4[3834];
  76. } sgx_arch_secs_t;
  77. typedef struct {
  78. uint64_t reserved0;
  79. uint64_t flags;
  80. uint64_t ossa;
  81. uint32_t cssa;
  82. uint32_t nssa;
  83. uint64_t oentry;
  84. uint64_t reserved1;
  85. uint64_t ofs_base;
  86. uint64_t ogs_base;
  87. uint32_t ofs_limit;
  88. uint32_t ogs_limit;
  89. uint8_t reserved3[4024];
  90. } sgx_arch_tcs_t;
  91. static_assert(sizeof(sgx_arch_tcs_t) == 4096, "incorrect struct size");
  92. #define TCS_FLAGS_DBGOPTIN (01ULL)
  93. typedef struct {
  94. uint64_t rax;
  95. uint64_t rcx;
  96. uint64_t rdx;
  97. uint64_t rbx;
  98. uint64_t rsp;
  99. uint64_t rbp;
  100. uint64_t rsi;
  101. uint64_t rdi;
  102. uint64_t r8;
  103. uint64_t r9;
  104. uint64_t r10;
  105. uint64_t r11;
  106. uint64_t r12;
  107. uint64_t r13;
  108. uint64_t r14;
  109. uint64_t r15;
  110. uint64_t rflags;
  111. uint64_t rip;
  112. uint64_t ursp;
  113. uint64_t urbp;
  114. uint32_t exitinfo;
  115. uint32_t reserved;
  116. uint64_t fsbase;
  117. uint64_t gsbase;
  118. } sgx_pal_gpr_t;
  119. typedef struct {
  120. uint64_t rax;
  121. uint64_t rcx;
  122. uint64_t rdx;
  123. uint64_t rbx;
  124. uint64_t rsp;
  125. uint64_t rbp;
  126. uint64_t rsi;
  127. uint64_t rdi;
  128. uint64_t r8;
  129. uint64_t r9;
  130. uint64_t r10;
  131. uint64_t r11;
  132. uint64_t r12;
  133. uint64_t r13;
  134. uint64_t r14;
  135. uint64_t r15;
  136. uint64_t rflags;
  137. uint64_t rip;
  138. } sgx_cpu_context_t;
  139. // Required by _restore_sgx_context, see enclave_entry.S.
  140. static_assert(offsetof(sgx_cpu_context_t, rip) - offsetof(sgx_cpu_context_t, rflags) ==
  141. sizeof(((sgx_cpu_context_t){0}).rflags),
  142. "rip must be directly after rflags in sgx_cpu_context_t");
  143. static_assert(offsetof(sgx_cpu_context_t, rflags) - offsetof(sgx_cpu_context_t, rdi) <=
  144. RED_ZONE_SIZE,
  145. "rdi needs to be within red zone distance from rflags");
  146. typedef struct {
  147. uint32_t vector : 8;
  148. uint32_t exit_type : 3;
  149. uint32_t reserved : 20;
  150. uint32_t valid : 1;
  151. } sgx_arch_exit_info_t;
  152. #define SGX_EXCEPTION_HARDWARE 3UL
  153. #define SGX_EXCEPTION_SOFTWARE 6UL
  154. #define SGX_EXCEPTION_VECTOR_DE 0UL /* DIV and IDIV instructions */
  155. #define SGX_EXCEPTION_VECTOR_DB 1UL /* For Intel use only */
  156. #define SGX_EXCEPTION_VECTOR_BP 3UL /* INT 3 instruction */
  157. #define SGX_EXCEPTION_VECTOR_BR 5UL /* BOUND instruction */
  158. #define SGX_EXCEPTION_VECTOR_UD 6UL /* UD2 instruction or reserved opcodes */
  159. #define SGX_EXCEPTION_VECTOR_MF 16UL /* x87 FPU floating-point or WAIT/FWAIT instruction */
  160. #define SGX_EXCEPTION_VECTOR_AC 17UL /* Any data reference in memory */
  161. #define SGX_EXCEPTION_VECTOR_XM 19UL /* Any SIMD floating-point exceptions */
  162. typedef struct {
  163. uint64_t lin_addr;
  164. uint64_t src_pge;
  165. uint64_t sec_info;
  166. uint64_t secs;
  167. } sgx_arch_page_info_t;
  168. typedef struct {
  169. uint64_t flags;
  170. uint64_t reserved[7];
  171. } sgx_arch_sec_info_t;
  172. #define SGX_SECINFO_FLAGS_R 0x001
  173. #define SGX_SECINFO_FLAGS_W 0x002
  174. #define SGX_SECINFO_FLAGS_X 0x004
  175. #define SGX_SECINFO_FLAGS_SECS 0x000
  176. #define SGX_SECINFO_FLAGS_TCS 0x100
  177. #define SGX_SECINFO_FLAGS_REG 0x200
  178. typedef struct _css_header_t {
  179. uint8_t header[12];
  180. uint32_t type;
  181. uint32_t module_vendor;
  182. uint32_t date;
  183. uint8_t header2[16];
  184. uint32_t hw_version;
  185. uint8_t reserved[84];
  186. } css_header_t;
  187. static_assert(sizeof(css_header_t) == 128, "incorrect struct size");
  188. typedef struct _css_key_t {
  189. uint8_t modulus[SE_KEY_SIZE];
  190. uint8_t exponent[SE_EXPONENT_SIZE];
  191. uint8_t signature[SE_KEY_SIZE];
  192. } css_key_t;
  193. static_assert(sizeof(css_key_t) == 772, "incorrect struct size");
  194. typedef struct _css_body_t {
  195. sgx_misc_select_t misc_select;
  196. sgx_misc_select_t misc_mask;
  197. uint8_t reserved[4];
  198. sgx_isvfamily_id_t isv_family_id;
  199. sgx_attributes_t attributes;
  200. sgx_attributes_t attribute_mask;
  201. sgx_measurement_t enclave_hash;
  202. uint8_t reserved2[16];
  203. sgx_isvext_prod_id_t isvext_prod_id;
  204. uint16_t isv_prod_id;
  205. uint16_t isv_svn;
  206. } css_body_t;
  207. static_assert(sizeof(css_body_t) == 128, "incorrect struct size");
  208. typedef struct _css_buffer_t {
  209. uint8_t reserved[12];
  210. uint8_t q1[SE_KEY_SIZE];
  211. uint8_t q2[SE_KEY_SIZE];
  212. } css_buffer_t;
  213. static_assert(sizeof(css_buffer_t) == 780, "incorrect struct size");
  214. typedef struct _enclave_css_t {
  215. css_header_t header;
  216. css_key_t key;
  217. css_body_t body;
  218. css_buffer_t buffer;
  219. } sgx_arch_enclave_css_t;
  220. static_assert(sizeof(sgx_arch_enclave_css_t) == 1808, "incorrect struct size");
  221. typedef struct _sgx_key_id_t {
  222. uint8_t id[SGX_KEYID_SIZE];
  223. } sgx_key_id_t;
  224. typedef struct {
  225. uint32_t valid;
  226. uint32_t reserved1[11];
  227. sgx_attributes_t attributes;
  228. sgx_measurement_t mr_enclave;
  229. uint8_t reserved2[32];
  230. sgx_measurement_t mr_signer;
  231. uint8_t reserved3[32];
  232. } launch_body_t;
  233. typedef struct {
  234. launch_body_t body;
  235. sgx_cpu_svn_t cpu_svn_le;
  236. sgx_prod_id_t isv_prod_id_le;
  237. sgx_isv_svn_t isv_svn_le;
  238. uint8_t reserved2[24];
  239. sgx_misc_select_t masked_misc_select_le;
  240. sgx_attributes_t attributes_le;
  241. sgx_key_id_t key_id;
  242. sgx_mac_t mac;
  243. } sgx_arch_token_t;
  244. typedef struct _sgx_report_data_t {
  245. uint8_t d[SGX_REPORT_DATA_SIZE];
  246. } sgx_report_data_t;
  247. #define __sgx_mem_aligned __attribute__((aligned(512)))
  248. typedef struct _report_body_t {
  249. sgx_cpu_svn_t cpu_svn;
  250. sgx_misc_select_t misc_select;
  251. uint8_t reserved1[28];
  252. sgx_attributes_t attributes;
  253. sgx_measurement_t mr_enclave;
  254. uint8_t reserved2[32];
  255. sgx_measurement_t mr_signer;
  256. uint8_t reserved3[96];
  257. sgx_prod_id_t isv_prod_id;
  258. sgx_isv_svn_t isv_svn;
  259. uint8_t reserved4[60];
  260. sgx_report_data_t report_data;
  261. } sgx_report_body_t;
  262. typedef struct _report_t {
  263. sgx_report_body_t body;
  264. sgx_key_id_t key_id;
  265. sgx_mac_t mac;
  266. } sgx_report_t;
  267. #define SGX_REPORT_SIGNED_SIZE 384
  268. #define SGX_REPORT_ACTUAL_SIZE 432
  269. typedef struct _target_info_t {
  270. sgx_measurement_t mr_enclave;
  271. sgx_attributes_t attributes;
  272. uint8_t reserved1[2];
  273. sgx_config_svn_t config_svn;
  274. sgx_misc_select_t misc_select;
  275. uint8_t reserved2[8];
  276. sgx_config_id_t config_id;
  277. uint8_t reserved3[384];
  278. } sgx_target_info_t;
  279. static_assert(sizeof(sgx_target_info_t) == 512, "incorrect struct size");
  280. typedef struct _key_request_t {
  281. uint16_t key_name;
  282. uint16_t key_policy;
  283. sgx_isv_svn_t isv_svn;
  284. uint16_t reserved1;
  285. sgx_cpu_svn_t cpu_svn;
  286. sgx_attributes_t attribute_mask;
  287. sgx_key_id_t key_id;
  288. sgx_misc_select_t misc_mask;
  289. sgx_config_svn_t config_svn;
  290. uint8_t reserved2[434];
  291. // struct is 512-bytes in size, alignment is required for EGETKEY
  292. } sgx_key_request_t;
  293. static_assert(sizeof(sgx_key_request_t) == 512, "incorrect struct size");
  294. #define SGX_TARGETINFO_FILLED_SIZE (sizeof(sgx_measurement_t) + sizeof(sgx_attributes_t))
  295. typedef uint8_t sgx_key_128bit_t[16];
  296. #define ENCLU ".byte 0x0f, 0x01, 0xd7"
  297. #else /* !__ASSEMBLER__ */
  298. /* microcode to call ENCLU */
  299. .macro ENCLU
  300. .byte 0x0f, 0x01, 0xd7
  301. .endm
  302. #endif
  303. #define EENTER 2
  304. #define ERESUME 3
  305. #define EDBGRD 4
  306. #define EDBGWR 5
  307. #define EREPORT 0
  308. #define EGETKEY 1
  309. #define EEXIT 4
  310. #define LAUNCH_KEY 0
  311. #define PROVISION_KEY 1
  312. #define PROVISION_SEAL_KEY 2
  313. #define REPORT_KEY 3
  314. #define SEAL_KEY 4
  315. #define KEYPOLICY_MRENCLAVE 1
  316. #define KEYPOLICY_MRSIGNER 2
  317. #define XSAVE_SIZE 512
  318. #define STACK_ALIGN 0xfffffffffffffff0
  319. #define XSAVE_ALIGN 0xffffffffffffffc0
  320. #define XSAVE_NON_FX_MASK 0xfffffffffffffffc
  321. #define RETURN_FROM_OCALL 0xffffffffffffffff
  322. #define RFLAGS_DF (1 << 10)
  323. #define RFLAGS_AC (1 << 18)
  324. #pragma pack(pop)
  325. #endif /* SGX_ARCH_H */