sgx.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
  2. /*
  3. * Copyright(c) 2016-20 Intel Corporation.
  4. */
  5. #ifndef _ASM_X86_SGX_H
  6. #define _ASM_X86_SGX_H
  7. #include <linux/types.h>
  8. #include <linux/ioctl.h>
  9. /**
  10. * enum sgx_page_flags - page control flags
  11. * @SGX_PAGE_MEASURE: Measure the page contents with a sequence of
  12. * ENCLS[EEXTEND] operations.
  13. */
  14. enum sgx_page_flags {
  15. SGX_PAGE_MEASURE = 0x01,
  16. };
  17. #define SGX_MAGIC 0xA4
  18. #define SGX_IOC_ENCLAVE_CREATE \
  19. _IOW(SGX_MAGIC, 0x00, struct sgx_enclave_create)
  20. #define SGX_IOC_ENCLAVE_ADD_PAGES \
  21. _IOWR(SGX_MAGIC, 0x01, struct sgx_enclave_add_pages)
  22. #define SGX_IOC_ENCLAVE_INIT \
  23. _IOW(SGX_MAGIC, 0x02, struct sgx_enclave_init)
  24. #define SGX_IOC_ENCLAVE_PROVISION \
  25. _IOW(SGX_MAGIC, 0x03, struct sgx_enclave_provision)
  26. #define SGX_IOC_VEPC_REMOVE_ALL \
  27. _IO(SGX_MAGIC, 0x04)
  28. #define SGX_IOC_ENCLAVE_RESTRICT_PERMISSIONS \
  29. _IOWR(SGX_MAGIC, 0x05, struct sgx_enclave_restrict_permissions)
  30. #define SGX_IOC_ENCLAVE_MODIFY_TYPES \
  31. _IOWR(SGX_MAGIC, 0x06, struct sgx_enclave_modify_types)
  32. #define SGX_IOC_ENCLAVE_REMOVE_PAGES \
  33. _IOWR(SGX_MAGIC, 0x07, struct sgx_enclave_remove_pages)
  34. /**
  35. * struct sgx_enclave_create - parameter structure for the
  36. * %SGX_IOC_ENCLAVE_CREATE ioctl
  37. * @src: address for the SECS page data
  38. */
  39. struct sgx_enclave_create {
  40. __u64 src;
  41. };
  42. /**
  43. * struct sgx_enclave_add_pages - parameter structure for the
  44. * %SGX_IOC_ENCLAVE_ADD_PAGE ioctl
  45. * @src: start address for the page data
  46. * @offset: starting page offset
  47. * @length: length of the data (multiple of the page size)
  48. * @secinfo: address for the SECINFO data
  49. * @flags: page control flags
  50. * @count: number of bytes added (multiple of the page size)
  51. */
  52. struct sgx_enclave_add_pages {
  53. __u64 src;
  54. __u64 offset;
  55. __u64 length;
  56. __u64 secinfo;
  57. __u64 flags;
  58. __u64 count;
  59. };
  60. /**
  61. * struct sgx_enclave_init - parameter structure for the
  62. * %SGX_IOC_ENCLAVE_INIT ioctl
  63. * @sigstruct: address for the SIGSTRUCT data
  64. */
  65. struct sgx_enclave_init {
  66. __u64 sigstruct;
  67. };
  68. /**
  69. * struct sgx_enclave_provision - parameter structure for the
  70. * %SGX_IOC_ENCLAVE_PROVISION ioctl
  71. * @fd: file handle of /dev/sgx_provision
  72. */
  73. struct sgx_enclave_provision {
  74. __u64 fd;
  75. };
  76. /**
  77. * struct sgx_enclave_restrict_permissions - parameters for ioctl
  78. * %SGX_IOC_ENCLAVE_RESTRICT_PERMISSIONS
  79. * @offset: starting page offset (page aligned relative to enclave base
  80. * address defined in SECS)
  81. * @length: length of memory (multiple of the page size)
  82. * @permissions:new permission bits for pages in range described by @offset
  83. * and @length
  84. * @result: (output) SGX result code of ENCLS[EMODPR] function
  85. * @count: (output) bytes successfully changed (multiple of page size)
  86. */
  87. struct sgx_enclave_restrict_permissions {
  88. __u64 offset;
  89. __u64 length;
  90. __u64 permissions;
  91. __u64 result;
  92. __u64 count;
  93. };
  94. /**
  95. * struct sgx_enclave_modify_types - parameters for ioctl
  96. * %SGX_IOC_ENCLAVE_MODIFY_TYPES
  97. * @offset: starting page offset (page aligned relative to enclave base
  98. * address defined in SECS)
  99. * @length: length of memory (multiple of the page size)
  100. * @page_type: new type for pages in range described by @offset and @length
  101. * @result: (output) SGX result code of ENCLS[EMODT] function
  102. * @count: (output) bytes successfully changed (multiple of page size)
  103. */
  104. struct sgx_enclave_modify_types {
  105. __u64 offset;
  106. __u64 length;
  107. __u64 page_type;
  108. __u64 result;
  109. __u64 count;
  110. };
  111. /**
  112. * struct sgx_enclave_remove_pages - %SGX_IOC_ENCLAVE_REMOVE_PAGES parameters
  113. * @offset: starting page offset (page aligned relative to enclave base
  114. * address defined in SECS)
  115. * @length: length of memory (multiple of the page size)
  116. * @count: (output) bytes successfully changed (multiple of page size)
  117. *
  118. * Regular (PT_REG) or TCS (PT_TCS) can be removed from an initialized
  119. * enclave if the system supports SGX2. First, the %SGX_IOC_ENCLAVE_MODIFY_TYPES
  120. * ioctl() should be used to change the page type to PT_TRIM. After that
  121. * succeeds ENCLU[EACCEPT] should be run from within the enclave and then
  122. * %SGX_IOC_ENCLAVE_REMOVE_PAGES can be used to complete the page removal.
  123. */
  124. struct sgx_enclave_remove_pages {
  125. __u64 offset;
  126. __u64 length;
  127. __u64 count;
  128. };
  129. struct sgx_enclave_run;
  130. /**
  131. * typedef sgx_enclave_user_handler_t - Exit handler function accepted by
  132. * __vdso_sgx_enter_enclave()
  133. * @rdi: RDI at the time of EEXIT, undefined on AEX
  134. * @rsi: RSI at the time of EEXIT, undefined on AEX
  135. * @rdx: RDX at the time of EEXIT, undefined on AEX
  136. * @rsp: RSP (untrusted) at the time of EEXIT or AEX
  137. * @r8: R8 at the time of EEXIT, undefined on AEX
  138. * @r9: R9 at the time of EEXIT, undefined on AEX
  139. * @run: The run instance given by the caller
  140. *
  141. * The register parameters contain the snapshot of their values at enclave
  142. * exit. An invalid ENCLU function number will cause -EINVAL to be returned
  143. * to the caller.
  144. *
  145. * Return:
  146. * - <= 0: The given value is returned back to the caller.
  147. * - > 0: ENCLU function to invoke, either EENTER or ERESUME.
  148. */
  149. typedef int (*sgx_enclave_user_handler_t)(long rdi, long rsi, long rdx,
  150. long rsp, long r8, long r9,
  151. struct sgx_enclave_run *run);
  152. /**
  153. * struct sgx_enclave_run - the execution context of __vdso_sgx_enter_enclave()
  154. * @tcs: TCS used to enter the enclave
  155. * @function: The last seen ENCLU function (EENTER, ERESUME or EEXIT)
  156. * @exception_vector: The interrupt vector of the exception
  157. * @exception_error_code: The exception error code pulled out of the stack
  158. * @exception_addr: The address that triggered the exception
  159. * @user_handler: User provided callback run on exception
  160. * @user_data: Data passed to the user handler
  161. * @reserved: Reserved for future extensions
  162. *
  163. * If @user_handler is provided, the handler will be invoked on all return paths
  164. * of the normal flow. The user handler may transfer control, e.g. via a
  165. * longjmp() call or a C++ exception, without returning to
  166. * __vdso_sgx_enter_enclave().
  167. */
  168. struct sgx_enclave_run {
  169. __u64 tcs;
  170. __u32 function;
  171. __u16 exception_vector;
  172. __u16 exception_error_code;
  173. __u64 exception_addr;
  174. __u64 user_handler;
  175. __u64 user_data;
  176. __u8 reserved[216];
  177. };
  178. /**
  179. * typedef vdso_sgx_enter_enclave_t - Prototype for __vdso_sgx_enter_enclave(),
  180. * a vDSO function to enter an SGX enclave.
  181. * @rdi: Pass-through value for RDI
  182. * @rsi: Pass-through value for RSI
  183. * @rdx: Pass-through value for RDX
  184. * @function: ENCLU function, must be EENTER or ERESUME
  185. * @r8: Pass-through value for R8
  186. * @r9: Pass-through value for R9
  187. * @run: struct sgx_enclave_run, must be non-NULL
  188. *
  189. * NOTE: __vdso_sgx_enter_enclave() does not ensure full compliance with the
  190. * x86-64 ABI, e.g. doesn't handle XSAVE state. Except for non-volatile
  191. * general purpose registers, EFLAGS.DF, and RSP alignment, preserving/setting
  192. * state in accordance with the x86-64 ABI is the responsibility of the enclave
  193. * and its runtime, i.e. __vdso_sgx_enter_enclave() cannot be called from C
  194. * code without careful consideration by both the enclave and its runtime.
  195. *
  196. * All general purpose registers except RAX, RBX and RCX are passed as-is to the
  197. * enclave. RAX, RBX and RCX are consumed by EENTER and ERESUME and are loaded
  198. * with @function, asynchronous exit pointer, and @run.tcs respectively.
  199. *
  200. * RBP and the stack are used to anchor __vdso_sgx_enter_enclave() to the
  201. * pre-enclave state, e.g. to retrieve @run.exception and @run.user_handler
  202. * after an enclave exit. All other registers are available for use by the
  203. * enclave and its runtime, e.g. an enclave can push additional data onto the
  204. * stack (and modify RSP) to pass information to the optional user handler (see
  205. * below).
  206. *
  207. * Most exceptions reported on ENCLU, including those that occur within the
  208. * enclave, are fixed up and reported synchronously instead of being delivered
  209. * via a standard signal. Debug Exceptions (#DB) and Breakpoints (#BP) are
  210. * never fixed up and are always delivered via standard signals. On synchronously
  211. * reported exceptions, -EFAULT is returned and details about the exception are
  212. * recorded in @run.exception, the optional sgx_enclave_exception struct.
  213. *
  214. * Return:
  215. * - 0: ENCLU function was successfully executed.
  216. * - -EINVAL: Invalid ENCL number (neither EENTER nor ERESUME).
  217. */
  218. typedef int (*vdso_sgx_enter_enclave_t)(unsigned long rdi, unsigned long rsi,
  219. unsigned long rdx, unsigned int function,
  220. unsigned long r8, unsigned long r9,
  221. struct sgx_enclave_run *run);
  222. #endif /* _ASM_X86_SGX_H */