sgx_arch.h 11 KB

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