sgx_x86.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. * @run: The run instance given by the caller
  134. *
  135. * The register parameters contain the snapshot of their values at enclave
  136. * exit. An invalid ENCLU function number will cause -EINVAL to be returned
  137. * to the caller.
  138. *
  139. * Return:
  140. * - <= 0: The given value is returned back to the caller.
  141. * - > 0: ENCLU function to invoke, either EENTER or ERESUME.
  142. */
  143. typedef int (*sgx_enclave_user_handler_t)(long rdi, long rsi, long rdx,
  144. long rsp, long r8, long r9,
  145. struct sgx_enclave_run *run);
  146. /**
  147. * struct sgx_enclave_run - the execution context of __vdso_sgx_enter_enclave()
  148. * @tcs: TCS used to enter the enclave
  149. * @function: The last seen ENCLU function (EENTER, ERESUME or EEXIT)
  150. * @exception_vector: The interrupt vector of the exception
  151. * @exception_error_code: The exception error code pulled out of the stack
  152. * @exception_addr: The address that triggered the exception
  153. * @user_handler: User provided callback run on exception
  154. * @user_data: Data passed to the user handler
  155. * @reserved Reserved for future extensions
  156. *
  157. * If @user_handler is provided, the handler will be invoked on all return paths
  158. * of the normal flow. The user handler may transfer control, e.g. via a
  159. * longjmp() call or a C++ exception, without returning to
  160. * __vdso_sgx_enter_enclave().
  161. */
  162. struct sgx_enclave_run {
  163. __u64 tcs;
  164. __u32 function;
  165. __u16 exception_vector;
  166. __u16 exception_error_code;
  167. __u64 exception_addr;
  168. __u64 user_handler;
  169. __u64 user_data;
  170. __u8 reserved[216];
  171. };
  172. /**
  173. * typedef vdso_sgx_enter_enclave_t - Prototype for __vdso_sgx_enter_enclave(),
  174. * a vDSO function to enter an SGX enclave.
  175. * @rdi: Pass-through value for RDI
  176. * @rsi: Pass-through value for RSI
  177. * @rdx: Pass-through value for RDX
  178. * @function: ENCLU function, must be EENTER or ERESUME
  179. * @r8: Pass-through value for R8
  180. * @r9: Pass-through value for R9
  181. * @run: struct sgx_enclave_run, must be non-NULL
  182. *
  183. * NOTE: __vdso_sgx_enter_enclave() does not ensure full compliance with the
  184. * x86-64 ABI, e.g. doesn't handle XSAVE state. Except for non-volatile
  185. * general purpose registers, EFLAGS.DF, and RSP alignment, preserving/setting
  186. * state in accordance with the x86-64 ABI is the responsibility of the enclave
  187. * and its runtime, i.e. __vdso_sgx_enter_enclave() cannot be called from C
  188. * code without careful consideration by both the enclave and its runtime.
  189. *
  190. * All general purpose registers except RAX, RBX and RCX are passed as-is to the
  191. * enclave. RAX, RBX and RCX are consumed by EENTER and ERESUME and are loaded
  192. * with @function, asynchronous exit pointer, and @run.tcs respectively.
  193. *
  194. * RBP and the stack are used to anchor __vdso_sgx_enter_enclave() to the
  195. * pre-enclave state, e.g. to retrieve @run.exception and @run.user_handler
  196. * after an enclave exit. All other registers are available for use by the
  197. * enclave and its runtime, e.g. an enclave can push additional data onto the
  198. * stack (and modify RSP) to pass information to the optional user handler (see
  199. * below).
  200. *
  201. * Most exceptions reported on ENCLU, including those that occur within the
  202. * enclave, are fixed up and reported synchronously instead of being delivered
  203. * via a standard signal. Debug Exceptions (#DB) and Breakpoints (#BP) are
  204. * never fixed up and are always delivered via standard signals. On synchronously
  205. * reported exceptions, -EFAULT is returned and details about the exception are
  206. * recorded in @run.exception, the optional sgx_enclave_exception struct.
  207. *
  208. * Return:
  209. * - 0: ENCLU function was successfully executed.
  210. * - -EINVAL: Invalid ENCL number (neither EENTER nor ERESUME).
  211. */
  212. typedef int (*vdso_sgx_enter_enclave_t)(unsigned long rdi, unsigned long rsi,
  213. unsigned long rdx, unsigned int function,
  214. unsigned long r8, unsigned long r9,
  215. struct sgx_enclave_run *run);
  216. #endif /* _ASM_X86_SGX_H */