sgxintrin.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /* Copyright (C) 2017-2019 Free Software Foundation, Inc.
  2. This file is part of GCC.
  3. GCC is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3, or (at your option)
  6. any later version.
  7. GCC 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 General Public License for more details.
  11. Under Section 7 of GPL version 3, you are granted additional
  12. permissions described in the GCC Runtime Library Exception, version
  13. 3.1, as published by the Free Software Foundation.
  14. You should have received a copy of the GNU General Public License and
  15. a copy of the GCC Runtime Library Exception along with this program;
  16. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  17. <http://www.gnu.org/licenses/>. */
  18. #ifndef _SGXINTRIN_H_INCLUDED
  19. #define _SGXINTRIN_H_INCLUDED
  20. #ifndef __SGX__
  21. #pragma GCC push_options
  22. #pragma GCC target("sgx")
  23. #define __DISABLE_SGX__
  24. #endif /* __SGX__ */
  25. #define __encls_bc(leaf, b, c, retval) \
  26. __asm__ __volatile__ ("encls\n\t" \
  27. : "=a" (retval) \
  28. : "a" (leaf), "b" (b), "c" (c) \
  29. : "cc")
  30. #define __encls_bcd(leaf, b, c, d, retval) \
  31. __asm__ __volatile__("encls\n\t" \
  32. : "=a" (retval) \
  33. : "a" (leaf), "b" (b), "c" (c), "d" (d) \
  34. : "cc")
  35. #define __encls_c(leaf, c, retval) \
  36. __asm__ __volatile__("encls\n\t" \
  37. : "=a" (retval) \
  38. : "a" (leaf), "c" (c) \
  39. : "cc")
  40. #define __encls_edbgrd(leaf, b, c, retval) \
  41. __asm__ __volatile__("encls\n\t" \
  42. : "=a" (retval), "=b" (b) \
  43. : "a" (leaf), "c" (c))
  44. #define __encls_generic(leaf, b, c, d, retval) \
  45. __asm__ __volatile__("encls\n\t" \
  46. : "=a" (retval), "=b" (b), "=c" (c), "=d" (d)\
  47. : "a" (leaf), "b" (b), "c" (c), "d" (d) \
  48. : "cc")
  49. #define __enclu_bc(leaf, b, c, retval) \
  50. __asm__ __volatile__("enclu\n\t" \
  51. : "=a" (retval) \
  52. : "a" (leaf), "b" (b), "c" (c) \
  53. : "cc")
  54. #define __enclu_bcd(leaf, b, c, d, retval) \
  55. __asm__ __volatile__("enclu\n\t" \
  56. : "=a" (retval) \
  57. : "a" (leaf), "b" (b), "c" (c), "d" (d) \
  58. : "cc")
  59. #define __enclu_eenter(leaf, b, c, retval) \
  60. __asm__ __volatile__("enclu\n\t" \
  61. : "=a" (retval), "=c" (c) \
  62. : "a" (leaf), "b" (b), "c" (c) \
  63. : "cc")
  64. #define __enclu_eexit(leaf, b, c, retval) \
  65. __asm__ __volatile__("enclu\n\t" \
  66. : "=a" (retval), "=c" (c) \
  67. : "a" (leaf), "b" (b) \
  68. : "cc")
  69. #define __enclu_generic(leaf, b, c, d, retval) \
  70. __asm__ __volatile__("enclu\n\t" \
  71. : "=a" (retval), "=b" (b), "=c" (c), "=d" (d)\
  72. : "a" (leaf), "b" (b), "c" (c), "d" (d) \
  73. : "cc")
  74. #define __enclv_bc(leaf, b, c, retval) \
  75. __asm__ __volatile__("enclv\n\t" \
  76. : "=a" (retval) \
  77. : "a" (leaf), "b" (b), "c" (c) \
  78. : "cc")
  79. #define __enclv_cd(leaf, c, d, retval) \
  80. __asm__ __volatile__("enclv\n\t" \
  81. : "=a" (retval) \
  82. : "a" (leaf), "c" (c), "d" (d) \
  83. : "cc")
  84. #define __enclv_generic(leaf, b, c, d, retval) \
  85. __asm__ __volatile__("enclv\n\t" \
  86. : "=a" (retval), "=b" (b), "=c" (b), "=d" (d)\
  87. : "a" (leaf), "b" (b), "c" (c), "d" (d) \
  88. : "cc")
  89. extern __inline unsigned int
  90. __attribute__((__gnu_inline__, __always_inline__, __artificial__))
  91. _encls_u32 (const unsigned int __L, size_t __D[])
  92. {
  93. enum __encls_type
  94. {
  95. __SGX_ECREATE = 0x00,
  96. __SGX_EADD = 0x01,
  97. __SGX_EINIT = 0x02,
  98. __SGX_EREMOVE = 0x03,
  99. __SGX_EDBGRD = 0x04,
  100. __SGX_EDBGWR = 0x05,
  101. __SGX_EEXTEND = 0x06,
  102. __SGX_ELDB = 0x07,
  103. __SGX_ELDU = 0x08,
  104. __SGX_EBLOCK = 0x09,
  105. __SGX_EPA = 0x0A,
  106. __SGX_EWB = 0x0B,
  107. __SGX_ETRACK = 0x0C,
  108. __SGX_EAUG = 0x0D,
  109. __SGX_EMODPR = 0x0E,
  110. __SGX_EMODT = 0x0F,
  111. __SGX_ERDINFO = 0x10,
  112. __SGX_ETRACKC = 0x11,
  113. __SGX_ELDBC = 0x12,
  114. __SGX_ELDUC = 0x13
  115. };
  116. enum __encls_type __T = (enum __encls_type)__L;
  117. unsigned int __R = 0;
  118. if (!__builtin_constant_p (__T))
  119. __encls_generic (__L, __D[0], __D[1], __D[2], __R);
  120. else switch (__T)
  121. {
  122. case __SGX_ECREATE:
  123. case __SGX_EADD:
  124. case __SGX_EDBGWR:
  125. case __SGX_EEXTEND:
  126. case __SGX_EPA:
  127. case __SGX_EMODPR:
  128. case __SGX_EMODT:
  129. case __SGX_EAUG:
  130. case __SGX_ERDINFO:
  131. __encls_bc (__L, __D[0], __D[1], __R);
  132. break;
  133. case __SGX_EINIT:
  134. case __SGX_ELDB:
  135. case __SGX_ELDU:
  136. case __SGX_EWB:
  137. case __SGX_ELDBC:
  138. case __SGX_ELDUC:
  139. __encls_bcd (__L, __D[0], __D[1], __D[2], __R);
  140. break;
  141. case __SGX_EREMOVE:
  142. case __SGX_EBLOCK:
  143. case __SGX_ETRACK:
  144. case __SGX_ETRACKC:
  145. __encls_c (__L, __D[1], __R);
  146. break;
  147. case __SGX_EDBGRD:
  148. __encls_edbgrd (__L, __D[0], __D[1], __R);
  149. break;
  150. default:
  151. __encls_generic (__L, __D[0], __D[1], __D[2], __R);
  152. }
  153. return __R;
  154. }
  155. extern __inline unsigned int
  156. __attribute__((__gnu_inline__, __always_inline__, __artificial__))
  157. _enclu_u32 (const unsigned int __L, size_t __D[])
  158. {
  159. enum __enclu_type
  160. {
  161. __SGX_EREPORT = 0x00,
  162. __SGX_EGETKEY = 0x01,
  163. __SGX_EENTER = 0x02,
  164. __SGX_ERESUME = 0x03,
  165. __SGX_EEXIT = 0x04,
  166. __SGX_EACCEPT = 0x05,
  167. __SGX_EMODPE = 0x06,
  168. __SGX_EACCEPTCOPY = 0x07
  169. };
  170. enum __enclu_type __T = (enum __enclu_type) __L;
  171. unsigned int __R = 0;
  172. if (!__builtin_constant_p (__T))
  173. __enclu_generic (__L, __D[0], __D[1], __D[2], __R);
  174. else switch (__T)
  175. {
  176. case __SGX_EREPORT:
  177. case __SGX_EACCEPTCOPY:
  178. __enclu_bcd (__L, __D[0], __D[1], __D[2], __R);
  179. break;
  180. case __SGX_EGETKEY:
  181. case __SGX_ERESUME:
  182. case __SGX_EACCEPT:
  183. case __SGX_EMODPE:
  184. __enclu_bc (__L, __D[0], __D[1], __R);
  185. break;
  186. case __SGX_EENTER:
  187. __enclu_eenter (__L, __D[0], __D[1], __R);
  188. break;
  189. case __SGX_EEXIT:
  190. __enclu_eexit (__L, __D[0], __D[1], __R);
  191. break;
  192. default:
  193. __enclu_generic (__L, __D[0], __D[1], __D[2], __R);
  194. }
  195. return __R;
  196. }
  197. extern __inline unsigned int
  198. __attribute__((__gnu_inline__, __always_inline__, __artificial__))
  199. _enclv_u32 (const unsigned int __L, size_t __D[])
  200. {
  201. enum __enclv_type
  202. {
  203. __SGX_EDECVIRTCHILD = 0x00,
  204. __SGX_EINCVIRTCHILD = 0x01,
  205. __SGX_ESETCONTEXT = 0x02
  206. };
  207. unsigned int __R = 0;
  208. if (!__builtin_constant_p (__L))
  209. __enclv_generic (__L, __D[0], __D[1], __D[2], __R);
  210. else switch (__L)
  211. {
  212. case __SGX_EDECVIRTCHILD:
  213. case __SGX_EINCVIRTCHILD:
  214. __enclv_bc (__L, __D[0], __D[1], __R);
  215. break;
  216. case __SGX_ESETCONTEXT:
  217. __enclv_cd (__L, __D[1], __D[2], __R);
  218. break;
  219. default:
  220. __enclv_generic (__L, __D[0], __D[1], __D[2], __R);
  221. }
  222. return __R;
  223. }
  224. #ifdef __DISABLE_SGX__
  225. #undef __DISABLE_SGX__
  226. #pragma GCC pop_options
  227. #endif /* __DISABLE_SGX__ */
  228. #endif /* _SGXINTRIN_H_INCLUDED */