nitro_enclaves.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
  2. /*
  3. * Copyright 2020-2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. */
  5. #ifndef _LINUX_NITRO_ENCLAVES_H_
  6. #define _LINUX_NITRO_ENCLAVES_H_
  7. #include <linux/types.h>
  8. /**
  9. * DOC: Nitro Enclaves (NE) Kernel Driver Interface
  10. */
  11. /**
  12. * NE_CREATE_VM - The command is used to create a slot that is associated with
  13. * an enclave VM.
  14. * The generated unique slot id is an output parameter.
  15. * The ioctl can be invoked on the /dev/nitro_enclaves fd, before
  16. * setting any resources, such as memory and vCPUs, for an
  17. * enclave. Memory and vCPUs are set for the slot mapped to an enclave.
  18. * A NE CPU pool has to be set before calling this function. The
  19. * pool can be set after the NE driver load, using
  20. * /sys/module/nitro_enclaves/parameters/ne_cpus.
  21. * Its format is the detailed in the cpu-lists section:
  22. * https://www.kernel.org/doc/html/latest/admin-guide/kernel-parameters.html
  23. * CPU 0 and its siblings have to remain available for the
  24. * primary / parent VM, so they cannot be set for enclaves. Full
  25. * CPU core(s), from the same NUMA node, need(s) to be included
  26. * in the CPU pool.
  27. *
  28. * Context: Process context.
  29. * Return:
  30. * * Enclave file descriptor - Enclave file descriptor used with
  31. * ioctl calls to set vCPUs and memory
  32. * regions, then start the enclave.
  33. * * -1 - There was a failure in the ioctl logic.
  34. * On failure, errno is set to:
  35. * * EFAULT - copy_to_user() failure.
  36. * * ENOMEM - Memory allocation failure for internal
  37. * bookkeeping variables.
  38. * * NE_ERR_NO_CPUS_AVAIL_IN_POOL - No NE CPU pool set / no CPUs available
  39. * in the pool.
  40. * * Error codes from get_unused_fd_flags() and anon_inode_getfile().
  41. * * Error codes from the NE PCI device request.
  42. */
  43. #define NE_CREATE_VM _IOR(0xAE, 0x20, __u64)
  44. /**
  45. * NE_ADD_VCPU - The command is used to set a vCPU for an enclave. The vCPU can
  46. * be auto-chosen from the NE CPU pool or it can be set by the
  47. * caller, with the note that it needs to be available in the NE
  48. * CPU pool. Full CPU core(s), from the same NUMA node, need(s) to
  49. * be associated with an enclave.
  50. * The vCPU id is an input / output parameter. If its value is 0,
  51. * then a CPU is chosen from the enclave CPU pool and returned via
  52. * this parameter.
  53. * The ioctl can be invoked on the enclave fd, before an enclave
  54. * is started.
  55. *
  56. * Context: Process context.
  57. * Return:
  58. * * 0 - Logic successfully completed.
  59. * * -1 - There was a failure in the ioctl logic.
  60. * On failure, errno is set to:
  61. * * EFAULT - copy_from_user() / copy_to_user() failure.
  62. * * ENOMEM - Memory allocation failure for internal
  63. * bookkeeping variables.
  64. * * EIO - Current task mm is not the same as the one
  65. * that created the enclave.
  66. * * NE_ERR_NO_CPUS_AVAIL_IN_POOL - No CPUs available in the NE CPU pool.
  67. * * NE_ERR_VCPU_ALREADY_USED - The provided vCPU is already used.
  68. * * NE_ERR_VCPU_NOT_IN_CPU_POOL - The provided vCPU is not available in the
  69. * NE CPU pool.
  70. * * NE_ERR_VCPU_INVALID_CPU_CORE - The core id of the provided vCPU is invalid
  71. * or out of range.
  72. * * NE_ERR_NOT_IN_INIT_STATE - The enclave is not in init state
  73. * (init = before being started).
  74. * * NE_ERR_INVALID_VCPU - The provided vCPU is not in the available
  75. * CPUs range.
  76. * * Error codes from the NE PCI device request.
  77. */
  78. #define NE_ADD_VCPU _IOWR(0xAE, 0x21, __u32)
  79. /**
  80. * NE_GET_IMAGE_LOAD_INFO - The command is used to get information needed for
  81. * in-memory enclave image loading e.g. offset in
  82. * enclave memory to start placing the enclave image.
  83. * The image load info is an input / output parameter.
  84. * It includes info provided by the caller - flags -
  85. * and returns the offset in enclave memory where to
  86. * start placing the enclave image.
  87. * The ioctl can be invoked on the enclave fd, before
  88. * an enclave is started.
  89. *
  90. * Context: Process context.
  91. * Return:
  92. * * 0 - Logic successfully completed.
  93. * * -1 - There was a failure in the ioctl logic.
  94. * On failure, errno is set to:
  95. * * EFAULT - copy_from_user() / copy_to_user() failure.
  96. * * NE_ERR_NOT_IN_INIT_STATE - The enclave is not in init state (init =
  97. * before being started).
  98. * * NE_ERR_INVALID_FLAG_VALUE - The value of the provided flag is invalid.
  99. */
  100. #define NE_GET_IMAGE_LOAD_INFO _IOWR(0xAE, 0x22, struct ne_image_load_info)
  101. /**
  102. * NE_SET_USER_MEMORY_REGION - The command is used to set a memory region for an
  103. * enclave, given the allocated memory from the
  104. * userspace. Enclave memory needs to be from the
  105. * same NUMA node as the enclave CPUs.
  106. * The user memory region is an input parameter. It
  107. * includes info provided by the caller - flags,
  108. * memory size and userspace address.
  109. * The ioctl can be invoked on the enclave fd,
  110. * before an enclave is started.
  111. *
  112. * Context: Process context.
  113. * Return:
  114. * * 0 - Logic successfully completed.
  115. * * -1 - There was a failure in the ioctl logic.
  116. * On failure, errno is set to:
  117. * * EFAULT - copy_from_user() failure.
  118. * * EINVAL - Invalid physical memory region(s) e.g.
  119. * unaligned address.
  120. * * EIO - Current task mm is not the same as
  121. * the one that created the enclave.
  122. * * ENOMEM - Memory allocation failure for internal
  123. * bookkeeping variables.
  124. * * NE_ERR_NOT_IN_INIT_STATE - The enclave is not in init state
  125. * (init = before being started).
  126. * * NE_ERR_INVALID_MEM_REGION_SIZE - The memory size of the region is not
  127. * multiple of 2 MiB.
  128. * * NE_ERR_INVALID_MEM_REGION_ADDR - Invalid user space address given.
  129. * * NE_ERR_UNALIGNED_MEM_REGION_ADDR - Unaligned user space address given.
  130. * * NE_ERR_MEM_REGION_ALREADY_USED - The memory region is already used.
  131. * * NE_ERR_MEM_NOT_HUGE_PAGE - The memory region is not backed by
  132. * huge pages.
  133. * * NE_ERR_MEM_DIFFERENT_NUMA_NODE - The memory region is not from the same
  134. * NUMA node as the CPUs.
  135. * * NE_ERR_MEM_MAX_REGIONS - The number of memory regions set for
  136. * the enclave reached maximum.
  137. * * NE_ERR_INVALID_PAGE_SIZE - The memory region is not backed by
  138. * pages multiple of 2 MiB.
  139. * * NE_ERR_INVALID_FLAG_VALUE - The value of the provided flag is invalid.
  140. * * Error codes from get_user_pages().
  141. * * Error codes from the NE PCI device request.
  142. */
  143. #define NE_SET_USER_MEMORY_REGION _IOW(0xAE, 0x23, struct ne_user_memory_region)
  144. /**
  145. * NE_START_ENCLAVE - The command is used to trigger enclave start after the
  146. * enclave resources, such as memory and CPU, have been set.
  147. * The enclave start info is an input / output parameter. It
  148. * includes info provided by the caller - enclave cid and
  149. * flags - and returns the cid (if input cid is 0).
  150. * The ioctl can be invoked on the enclave fd, after an
  151. * enclave slot is created and resources, such as memory and
  152. * vCPUs are set for an enclave.
  153. *
  154. * Context: Process context.
  155. * Return:
  156. * * 0 - Logic successfully completed.
  157. * * -1 - There was a failure in the ioctl logic.
  158. * On failure, errno is set to:
  159. * * EFAULT - copy_from_user() / copy_to_user() failure.
  160. * * NE_ERR_NOT_IN_INIT_STATE - The enclave is not in init state
  161. * (init = before being started).
  162. * * NE_ERR_NO_MEM_REGIONS_ADDED - No memory regions are set.
  163. * * NE_ERR_NO_VCPUS_ADDED - No vCPUs are set.
  164. * * NE_ERR_FULL_CORES_NOT_USED - Full core(s) not set for the enclave.
  165. * * NE_ERR_ENCLAVE_MEM_MIN_SIZE - Enclave memory is less than minimum
  166. * memory size (64 MiB).
  167. * * NE_ERR_INVALID_FLAG_VALUE - The value of the provided flag is invalid.
  168. * * NE_ERR_INVALID_ENCLAVE_CID - The provided enclave CID is invalid.
  169. * * Error codes from the NE PCI device request.
  170. */
  171. #define NE_START_ENCLAVE _IOWR(0xAE, 0x24, struct ne_enclave_start_info)
  172. /**
  173. * DOC: NE specific error codes
  174. */
  175. /**
  176. * NE_ERR_VCPU_ALREADY_USED - The provided vCPU is already used.
  177. */
  178. #define NE_ERR_VCPU_ALREADY_USED (256)
  179. /**
  180. * NE_ERR_VCPU_NOT_IN_CPU_POOL - The provided vCPU is not available in the
  181. * NE CPU pool.
  182. */
  183. #define NE_ERR_VCPU_NOT_IN_CPU_POOL (257)
  184. /**
  185. * NE_ERR_VCPU_INVALID_CPU_CORE - The core id of the provided vCPU is invalid
  186. * or out of range of the NE CPU pool.
  187. */
  188. #define NE_ERR_VCPU_INVALID_CPU_CORE (258)
  189. /**
  190. * NE_ERR_INVALID_MEM_REGION_SIZE - The user space memory region size is not
  191. * multiple of 2 MiB.
  192. */
  193. #define NE_ERR_INVALID_MEM_REGION_SIZE (259)
  194. /**
  195. * NE_ERR_INVALID_MEM_REGION_ADDR - The user space memory region address range
  196. * is invalid.
  197. */
  198. #define NE_ERR_INVALID_MEM_REGION_ADDR (260)
  199. /**
  200. * NE_ERR_UNALIGNED_MEM_REGION_ADDR - The user space memory region address is
  201. * not aligned.
  202. */
  203. #define NE_ERR_UNALIGNED_MEM_REGION_ADDR (261)
  204. /**
  205. * NE_ERR_MEM_REGION_ALREADY_USED - The user space memory region is already used.
  206. */
  207. #define NE_ERR_MEM_REGION_ALREADY_USED (262)
  208. /**
  209. * NE_ERR_MEM_NOT_HUGE_PAGE - The user space memory region is not backed by
  210. * contiguous physical huge page(s).
  211. */
  212. #define NE_ERR_MEM_NOT_HUGE_PAGE (263)
  213. /**
  214. * NE_ERR_MEM_DIFFERENT_NUMA_NODE - The user space memory region is backed by
  215. * pages from different NUMA nodes than the CPUs.
  216. */
  217. #define NE_ERR_MEM_DIFFERENT_NUMA_NODE (264)
  218. /**
  219. * NE_ERR_MEM_MAX_REGIONS - The supported max memory regions per enclaves has
  220. * been reached.
  221. */
  222. #define NE_ERR_MEM_MAX_REGIONS (265)
  223. /**
  224. * NE_ERR_NO_MEM_REGIONS_ADDED - The command to start an enclave is triggered
  225. * and no memory regions are added.
  226. */
  227. #define NE_ERR_NO_MEM_REGIONS_ADDED (266)
  228. /**
  229. * NE_ERR_NO_VCPUS_ADDED - The command to start an enclave is triggered and no
  230. * vCPUs are added.
  231. */
  232. #define NE_ERR_NO_VCPUS_ADDED (267)
  233. /**
  234. * NE_ERR_ENCLAVE_MEM_MIN_SIZE - The enclave memory size is lower than the
  235. * minimum supported.
  236. */
  237. #define NE_ERR_ENCLAVE_MEM_MIN_SIZE (268)
  238. /**
  239. * NE_ERR_FULL_CORES_NOT_USED - The command to start an enclave is triggered and
  240. * full CPU cores are not set.
  241. */
  242. #define NE_ERR_FULL_CORES_NOT_USED (269)
  243. /**
  244. * NE_ERR_NOT_IN_INIT_STATE - The enclave is not in init state when setting
  245. * resources or triggering start.
  246. */
  247. #define NE_ERR_NOT_IN_INIT_STATE (270)
  248. /**
  249. * NE_ERR_INVALID_VCPU - The provided vCPU is out of range of the available CPUs.
  250. */
  251. #define NE_ERR_INVALID_VCPU (271)
  252. /**
  253. * NE_ERR_NO_CPUS_AVAIL_IN_POOL - The command to create an enclave is triggered
  254. * and no CPUs are available in the pool.
  255. */
  256. #define NE_ERR_NO_CPUS_AVAIL_IN_POOL (272)
  257. /**
  258. * NE_ERR_INVALID_PAGE_SIZE - The user space memory region is not backed by pages
  259. * multiple of 2 MiB.
  260. */
  261. #define NE_ERR_INVALID_PAGE_SIZE (273)
  262. /**
  263. * NE_ERR_INVALID_FLAG_VALUE - The provided flag value is invalid.
  264. */
  265. #define NE_ERR_INVALID_FLAG_VALUE (274)
  266. /**
  267. * NE_ERR_INVALID_ENCLAVE_CID - The provided enclave CID is invalid, either
  268. * being a well-known value or the CID of the
  269. * parent / primary VM.
  270. */
  271. #define NE_ERR_INVALID_ENCLAVE_CID (275)
  272. /**
  273. * DOC: Image load info flags
  274. */
  275. /**
  276. * NE_EIF_IMAGE - Enclave Image Format (EIF)
  277. */
  278. #define NE_EIF_IMAGE (0x01)
  279. #define NE_IMAGE_LOAD_MAX_FLAG_VAL (0x02)
  280. /**
  281. * struct ne_image_load_info - Info necessary for in-memory enclave image
  282. * loading (in / out).
  283. * @flags: Flags to determine the enclave image type
  284. * (e.g. Enclave Image Format - EIF) (in).
  285. * @memory_offset: Offset in enclave memory where to start placing the
  286. * enclave image (out).
  287. */
  288. struct ne_image_load_info {
  289. __u64 flags;
  290. __u64 memory_offset;
  291. };
  292. /**
  293. * DOC: User memory region flags
  294. */
  295. /**
  296. * NE_DEFAULT_MEMORY_REGION - Memory region for enclave general usage.
  297. */
  298. #define NE_DEFAULT_MEMORY_REGION (0x00)
  299. #define NE_MEMORY_REGION_MAX_FLAG_VAL (0x01)
  300. /**
  301. * struct ne_user_memory_region - Memory region to be set for an enclave (in).
  302. * @flags: Flags to determine the usage for the memory region (in).
  303. * @memory_size: The size, in bytes, of the memory region to be set for
  304. * an enclave (in).
  305. * @userspace_addr: The start address of the userspace allocated memory of
  306. * the memory region to set for an enclave (in).
  307. */
  308. struct ne_user_memory_region {
  309. __u64 flags;
  310. __u64 memory_size;
  311. __u64 userspace_addr;
  312. };
  313. /**
  314. * DOC: Enclave start info flags
  315. */
  316. /**
  317. * NE_ENCLAVE_PRODUCTION_MODE - Start enclave in production mode.
  318. */
  319. #define NE_ENCLAVE_PRODUCTION_MODE (0x00)
  320. /**
  321. * NE_ENCLAVE_DEBUG_MODE - Start enclave in debug mode.
  322. */
  323. #define NE_ENCLAVE_DEBUG_MODE (0x01)
  324. #define NE_ENCLAVE_START_MAX_FLAG_VAL (0x02)
  325. /**
  326. * struct ne_enclave_start_info - Setup info necessary for enclave start (in / out).
  327. * @flags: Flags for the enclave to start with (e.g. debug mode) (in).
  328. * @enclave_cid: Context ID (CID) for the enclave vsock device. If 0 as
  329. * input, the CID is autogenerated by the hypervisor and
  330. * returned back as output by the driver (in / out).
  331. */
  332. struct ne_enclave_start_info {
  333. __u64 flags;
  334. __u64 enclave_cid;
  335. };
  336. #endif /* _LINUX_NITRO_ENCLAVES_H_ */