ne_ioctl_sample.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. */
  5. /**
  6. * DOC: Sample flow of using the ioctl interface provided by the Nitro Enclaves (NE)
  7. * kernel driver.
  8. *
  9. * Usage
  10. * -----
  11. *
  12. * Load the nitro_enclaves module, setting also the enclave CPU pool. The
  13. * enclave CPUs need to be full cores from the same NUMA node. CPU 0 and its
  14. * siblings have to remain available for the primary / parent VM, so they
  15. * cannot be included in the enclave CPU pool.
  16. *
  17. * See the cpu list section from the kernel documentation.
  18. * https://www.kernel.org/doc/html/latest/admin-guide/kernel-parameters.html#cpu-lists
  19. *
  20. * insmod drivers/virt/nitro_enclaves/nitro_enclaves.ko
  21. * lsmod
  22. *
  23. * The CPU pool can be set at runtime, after the kernel module is loaded.
  24. *
  25. * echo <cpu-list> > /sys/module/nitro_enclaves/parameters/ne_cpus
  26. *
  27. * NUMA and CPU siblings information can be found using:
  28. *
  29. * lscpu
  30. * /proc/cpuinfo
  31. *
  32. * Check the online / offline CPU list. The CPUs from the pool should be
  33. * offlined.
  34. *
  35. * lscpu
  36. *
  37. * Check dmesg for any warnings / errors through the NE driver lifetime / usage.
  38. * The NE logs contain the "nitro_enclaves" or "pci 0000:00:02.0" pattern.
  39. *
  40. * dmesg
  41. *
  42. * Setup hugetlbfs huge pages. The memory needs to be from the same NUMA node as
  43. * the enclave CPUs.
  44. *
  45. * https://www.kernel.org/doc/html/latest/admin-guide/mm/hugetlbpage.html
  46. *
  47. * By default, the allocation of hugetlb pages are distributed on all possible
  48. * NUMA nodes. Use the following configuration files to set the number of huge
  49. * pages from a NUMA node:
  50. *
  51. * /sys/devices/system/node/node<X>/hugepages/hugepages-2048kB/nr_hugepages
  52. * /sys/devices/system/node/node<X>/hugepages/hugepages-1048576kB/nr_hugepages
  53. *
  54. * or, if not on a system with multiple NUMA nodes, can also set the number
  55. * of 2 MiB / 1 GiB huge pages using
  56. *
  57. * /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages
  58. * /sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepages
  59. *
  60. * In this example 256 hugepages of 2 MiB are used.
  61. *
  62. * Build and run the NE sample.
  63. *
  64. * make -C samples/nitro_enclaves clean
  65. * make -C samples/nitro_enclaves
  66. * ./samples/nitro_enclaves/ne_ioctl_sample <path_to_enclave_image>
  67. *
  68. * Unload the nitro_enclaves module.
  69. *
  70. * rmmod nitro_enclaves
  71. * lsmod
  72. */
  73. #include <stdio.h>
  74. #include <stdlib.h>
  75. #include <errno.h>
  76. #include <fcntl.h>
  77. #include <limits.h>
  78. #include <poll.h>
  79. #include <pthread.h>
  80. #include <string.h>
  81. #include <sys/eventfd.h>
  82. #include <sys/ioctl.h>
  83. #include <sys/mman.h>
  84. #include <sys/socket.h>
  85. #include <sys/stat.h>
  86. #include <sys/types.h>
  87. #include <unistd.h>
  88. #include <linux/mman.h>
  89. #include <linux/nitro_enclaves.h>
  90. #include <linux/vm_sockets.h>
  91. /**
  92. * NE_DEV_NAME - Nitro Enclaves (NE) misc device that provides the ioctl interface.
  93. */
  94. #define NE_DEV_NAME "/dev/nitro_enclaves"
  95. /**
  96. * NE_POLL_WAIT_TIME - Timeout in seconds for each poll event.
  97. */
  98. #define NE_POLL_WAIT_TIME (60)
  99. /**
  100. * NE_POLL_WAIT_TIME_MS - Timeout in milliseconds for each poll event.
  101. */
  102. #define NE_POLL_WAIT_TIME_MS (NE_POLL_WAIT_TIME * 1000)
  103. /**
  104. * NE_SLEEP_TIME - Amount of time in seconds for the process to keep the enclave alive.
  105. */
  106. #define NE_SLEEP_TIME (300)
  107. /**
  108. * NE_DEFAULT_NR_VCPUS - Default number of vCPUs set for an enclave.
  109. */
  110. #define NE_DEFAULT_NR_VCPUS (2)
  111. /**
  112. * NE_MIN_MEM_REGION_SIZE - Minimum size of a memory region - 2 MiB.
  113. */
  114. #define NE_MIN_MEM_REGION_SIZE (2 * 1024 * 1024)
  115. /**
  116. * NE_DEFAULT_NR_MEM_REGIONS - Default number of memory regions of 2 MiB set for
  117. * an enclave.
  118. */
  119. #define NE_DEFAULT_NR_MEM_REGIONS (256)
  120. /**
  121. * NE_IMAGE_LOAD_HEARTBEAT_CID - Vsock CID for enclave image loading heartbeat logic.
  122. */
  123. #define NE_IMAGE_LOAD_HEARTBEAT_CID (3)
  124. /**
  125. * NE_IMAGE_LOAD_HEARTBEAT_PORT - Vsock port for enclave image loading heartbeat logic.
  126. */
  127. #define NE_IMAGE_LOAD_HEARTBEAT_PORT (9000)
  128. /**
  129. * NE_IMAGE_LOAD_HEARTBEAT_VALUE - Heartbeat value for enclave image loading.
  130. */
  131. #define NE_IMAGE_LOAD_HEARTBEAT_VALUE (0xb7)
  132. /**
  133. * struct ne_user_mem_region - User space memory region set for an enclave.
  134. * @userspace_addr: Address of the user space memory region.
  135. * @memory_size: Size of the user space memory region.
  136. */
  137. struct ne_user_mem_region {
  138. void *userspace_addr;
  139. size_t memory_size;
  140. };
  141. /**
  142. * ne_create_vm() - Create a slot for the enclave VM.
  143. * @ne_dev_fd: The file descriptor of the NE misc device.
  144. * @slot_uid: The generated slot uid for the enclave.
  145. * @enclave_fd : The generated file descriptor for the enclave.
  146. *
  147. * Context: Process context.
  148. * Return:
  149. * * 0 on success.
  150. * * Negative return value on failure.
  151. */
  152. static int ne_create_vm(int ne_dev_fd, unsigned long *slot_uid, int *enclave_fd)
  153. {
  154. int rc = -EINVAL;
  155. *enclave_fd = ioctl(ne_dev_fd, NE_CREATE_VM, slot_uid);
  156. if (*enclave_fd < 0) {
  157. rc = *enclave_fd;
  158. switch (errno) {
  159. case NE_ERR_NO_CPUS_AVAIL_IN_POOL: {
  160. printf("Error in create VM, no CPUs available in the NE CPU pool\n");
  161. break;
  162. }
  163. default:
  164. printf("Error in create VM [%m]\n");
  165. }
  166. return rc;
  167. }
  168. return 0;
  169. }
  170. /**
  171. * ne_poll_enclave_fd() - Thread function for polling the enclave fd.
  172. * @data: Argument provided for the polling function.
  173. *
  174. * Context: Process context.
  175. * Return:
  176. * * NULL on success / failure.
  177. */
  178. void *ne_poll_enclave_fd(void *data)
  179. {
  180. int enclave_fd = *(int *)data;
  181. struct pollfd fds[1] = {};
  182. int i = 0;
  183. int rc = -EINVAL;
  184. printf("Running from poll thread, enclave fd %d\n", enclave_fd);
  185. fds[0].fd = enclave_fd;
  186. fds[0].events = POLLIN | POLLERR | POLLHUP;
  187. /* Keep on polling until the current process is terminated. */
  188. while (1) {
  189. printf("[iter %d] Polling ...\n", i);
  190. rc = poll(fds, 1, NE_POLL_WAIT_TIME_MS);
  191. if (rc < 0) {
  192. printf("Error in poll [%m]\n");
  193. return NULL;
  194. }
  195. i++;
  196. if (!rc) {
  197. printf("Poll: %d seconds elapsed\n",
  198. i * NE_POLL_WAIT_TIME);
  199. continue;
  200. }
  201. printf("Poll received value 0x%x\n", fds[0].revents);
  202. if (fds[0].revents & POLLHUP) {
  203. printf("Received POLLHUP\n");
  204. return NULL;
  205. }
  206. if (fds[0].revents & POLLNVAL) {
  207. printf("Received POLLNVAL\n");
  208. return NULL;
  209. }
  210. }
  211. return NULL;
  212. }
  213. /**
  214. * ne_alloc_user_mem_region() - Allocate a user space memory region for an enclave.
  215. * @ne_user_mem_region: User space memory region allocated using hugetlbfs.
  216. *
  217. * Context: Process context.
  218. * Return:
  219. * * 0 on success.
  220. * * Negative return value on failure.
  221. */
  222. static int ne_alloc_user_mem_region(struct ne_user_mem_region *ne_user_mem_region)
  223. {
  224. /**
  225. * Check available hugetlb encodings for different huge page sizes in
  226. * include/uapi/linux/mman.h.
  227. */
  228. ne_user_mem_region->userspace_addr = mmap(NULL, ne_user_mem_region->memory_size,
  229. PROT_READ | PROT_WRITE,
  230. MAP_PRIVATE | MAP_ANONYMOUS |
  231. MAP_HUGETLB | MAP_HUGE_2MB, -1, 0);
  232. if (ne_user_mem_region->userspace_addr == MAP_FAILED) {
  233. printf("Error in mmap memory [%m]\n");
  234. return -1;
  235. }
  236. return 0;
  237. }
  238. /**
  239. * ne_load_enclave_image() - Place the enclave image in the enclave memory.
  240. * @enclave_fd : The file descriptor associated with the enclave.
  241. * @ne_user_mem_regions: User space memory regions allocated for the enclave.
  242. * @enclave_image_path : The file path of the enclave image.
  243. *
  244. * Context: Process context.
  245. * Return:
  246. * * 0 on success.
  247. * * Negative return value on failure.
  248. */
  249. static int ne_load_enclave_image(int enclave_fd, struct ne_user_mem_region ne_user_mem_regions[],
  250. char *enclave_image_path)
  251. {
  252. unsigned char *enclave_image = NULL;
  253. int enclave_image_fd = -1;
  254. size_t enclave_image_size = 0;
  255. size_t enclave_memory_size = 0;
  256. unsigned long i = 0;
  257. size_t image_written_bytes = 0;
  258. struct ne_image_load_info image_load_info = {
  259. .flags = NE_EIF_IMAGE,
  260. };
  261. struct stat image_stat_buf = {};
  262. int rc = -EINVAL;
  263. size_t temp_image_offset = 0;
  264. for (i = 0; i < NE_DEFAULT_NR_MEM_REGIONS; i++)
  265. enclave_memory_size += ne_user_mem_regions[i].memory_size;
  266. rc = stat(enclave_image_path, &image_stat_buf);
  267. if (rc < 0) {
  268. printf("Error in get image stat info [%m]\n");
  269. return rc;
  270. }
  271. enclave_image_size = image_stat_buf.st_size;
  272. if (enclave_memory_size < enclave_image_size) {
  273. printf("The enclave memory is smaller than the enclave image size\n");
  274. return -ENOMEM;
  275. }
  276. rc = ioctl(enclave_fd, NE_GET_IMAGE_LOAD_INFO, &image_load_info);
  277. if (rc < 0) {
  278. switch (errno) {
  279. case NE_ERR_NOT_IN_INIT_STATE: {
  280. printf("Error in get image load info, enclave not in init state\n");
  281. break;
  282. }
  283. case NE_ERR_INVALID_FLAG_VALUE: {
  284. printf("Error in get image load info, provided invalid flag\n");
  285. break;
  286. }
  287. default:
  288. printf("Error in get image load info [%m]\n");
  289. }
  290. return rc;
  291. }
  292. printf("Enclave image offset in enclave memory is %lld\n",
  293. image_load_info.memory_offset);
  294. enclave_image_fd = open(enclave_image_path, O_RDONLY);
  295. if (enclave_image_fd < 0) {
  296. printf("Error in open enclave image file [%m]\n");
  297. return enclave_image_fd;
  298. }
  299. enclave_image = mmap(NULL, enclave_image_size, PROT_READ,
  300. MAP_PRIVATE, enclave_image_fd, 0);
  301. if (enclave_image == MAP_FAILED) {
  302. printf("Error in mmap enclave image [%m]\n");
  303. return -1;
  304. }
  305. temp_image_offset = image_load_info.memory_offset;
  306. for (i = 0; i < NE_DEFAULT_NR_MEM_REGIONS; i++) {
  307. size_t bytes_to_write = 0;
  308. size_t memory_offset = 0;
  309. size_t memory_size = ne_user_mem_regions[i].memory_size;
  310. size_t remaining_bytes = 0;
  311. void *userspace_addr = ne_user_mem_regions[i].userspace_addr;
  312. if (temp_image_offset >= memory_size) {
  313. temp_image_offset -= memory_size;
  314. continue;
  315. } else if (temp_image_offset != 0) {
  316. memory_offset = temp_image_offset;
  317. memory_size -= temp_image_offset;
  318. temp_image_offset = 0;
  319. }
  320. remaining_bytes = enclave_image_size - image_written_bytes;
  321. bytes_to_write = memory_size < remaining_bytes ?
  322. memory_size : remaining_bytes;
  323. memcpy(userspace_addr + memory_offset,
  324. enclave_image + image_written_bytes, bytes_to_write);
  325. image_written_bytes += bytes_to_write;
  326. if (image_written_bytes == enclave_image_size)
  327. break;
  328. }
  329. munmap(enclave_image, enclave_image_size);
  330. close(enclave_image_fd);
  331. return 0;
  332. }
  333. /**
  334. * ne_set_user_mem_region() - Set a user space memory region for the given enclave.
  335. * @enclave_fd : The file descriptor associated with the enclave.
  336. * @ne_user_mem_region : User space memory region to be set for the enclave.
  337. *
  338. * Context: Process context.
  339. * Return:
  340. * * 0 on success.
  341. * * Negative return value on failure.
  342. */
  343. static int ne_set_user_mem_region(int enclave_fd, struct ne_user_mem_region ne_user_mem_region)
  344. {
  345. struct ne_user_memory_region mem_region = {
  346. .flags = NE_DEFAULT_MEMORY_REGION,
  347. .memory_size = ne_user_mem_region.memory_size,
  348. .userspace_addr = (__u64)ne_user_mem_region.userspace_addr,
  349. };
  350. int rc = -EINVAL;
  351. rc = ioctl(enclave_fd, NE_SET_USER_MEMORY_REGION, &mem_region);
  352. if (rc < 0) {
  353. switch (errno) {
  354. case NE_ERR_NOT_IN_INIT_STATE: {
  355. printf("Error in set user memory region, enclave not in init state\n");
  356. break;
  357. }
  358. case NE_ERR_INVALID_MEM_REGION_SIZE: {
  359. printf("Error in set user memory region, mem size not multiple of 2 MiB\n");
  360. break;
  361. }
  362. case NE_ERR_INVALID_MEM_REGION_ADDR: {
  363. printf("Error in set user memory region, invalid user space address\n");
  364. break;
  365. }
  366. case NE_ERR_UNALIGNED_MEM_REGION_ADDR: {
  367. printf("Error in set user memory region, unaligned user space address\n");
  368. break;
  369. }
  370. case NE_ERR_MEM_REGION_ALREADY_USED: {
  371. printf("Error in set user memory region, memory region already used\n");
  372. break;
  373. }
  374. case NE_ERR_MEM_NOT_HUGE_PAGE: {
  375. printf("Error in set user memory region, not backed by huge pages\n");
  376. break;
  377. }
  378. case NE_ERR_MEM_DIFFERENT_NUMA_NODE: {
  379. printf("Error in set user memory region, different NUMA node than CPUs\n");
  380. break;
  381. }
  382. case NE_ERR_MEM_MAX_REGIONS: {
  383. printf("Error in set user memory region, max memory regions reached\n");
  384. break;
  385. }
  386. case NE_ERR_INVALID_PAGE_SIZE: {
  387. printf("Error in set user memory region, has page not multiple of 2 MiB\n");
  388. break;
  389. }
  390. case NE_ERR_INVALID_FLAG_VALUE: {
  391. printf("Error in set user memory region, provided invalid flag\n");
  392. break;
  393. }
  394. default:
  395. printf("Error in set user memory region [%m]\n");
  396. }
  397. return rc;
  398. }
  399. return 0;
  400. }
  401. /**
  402. * ne_free_mem_regions() - Unmap all the user space memory regions that were set
  403. * aside for the enclave.
  404. * @ne_user_mem_regions: The user space memory regions associated with an enclave.
  405. *
  406. * Context: Process context.
  407. */
  408. static void ne_free_mem_regions(struct ne_user_mem_region ne_user_mem_regions[])
  409. {
  410. unsigned int i = 0;
  411. for (i = 0; i < NE_DEFAULT_NR_MEM_REGIONS; i++)
  412. munmap(ne_user_mem_regions[i].userspace_addr,
  413. ne_user_mem_regions[i].memory_size);
  414. }
  415. /**
  416. * ne_add_vcpu() - Add a vCPU to the given enclave.
  417. * @enclave_fd : The file descriptor associated with the enclave.
  418. * @vcpu_id: vCPU id to be set for the enclave, either provided or
  419. * auto-generated (if provided vCPU id is 0).
  420. *
  421. * Context: Process context.
  422. * Return:
  423. * * 0 on success.
  424. * * Negative return value on failure.
  425. */
  426. static int ne_add_vcpu(int enclave_fd, unsigned int *vcpu_id)
  427. {
  428. int rc = -EINVAL;
  429. rc = ioctl(enclave_fd, NE_ADD_VCPU, vcpu_id);
  430. if (rc < 0) {
  431. switch (errno) {
  432. case NE_ERR_NO_CPUS_AVAIL_IN_POOL: {
  433. printf("Error in add vcpu, no CPUs available in the NE CPU pool\n");
  434. break;
  435. }
  436. case NE_ERR_VCPU_ALREADY_USED: {
  437. printf("Error in add vcpu, the provided vCPU is already used\n");
  438. break;
  439. }
  440. case NE_ERR_VCPU_NOT_IN_CPU_POOL: {
  441. printf("Error in add vcpu, the provided vCPU is not in the NE CPU pool\n");
  442. break;
  443. }
  444. case NE_ERR_VCPU_INVALID_CPU_CORE: {
  445. printf("Error in add vcpu, the core id of the provided vCPU is invalid\n");
  446. break;
  447. }
  448. case NE_ERR_NOT_IN_INIT_STATE: {
  449. printf("Error in add vcpu, enclave not in init state\n");
  450. break;
  451. }
  452. case NE_ERR_INVALID_VCPU: {
  453. printf("Error in add vcpu, the provided vCPU is out of avail CPUs range\n");
  454. break;
  455. }
  456. default:
  457. printf("Error in add vcpu [%m]\n");
  458. }
  459. return rc;
  460. }
  461. return 0;
  462. }
  463. /**
  464. * ne_start_enclave() - Start the given enclave.
  465. * @enclave_fd : The file descriptor associated with the enclave.
  466. * @enclave_start_info : Enclave metadata used for starting e.g. vsock CID.
  467. *
  468. * Context: Process context.
  469. * Return:
  470. * * 0 on success.
  471. * * Negative return value on failure.
  472. */
  473. static int ne_start_enclave(int enclave_fd, struct ne_enclave_start_info *enclave_start_info)
  474. {
  475. int rc = -EINVAL;
  476. rc = ioctl(enclave_fd, NE_START_ENCLAVE, enclave_start_info);
  477. if (rc < 0) {
  478. switch (errno) {
  479. case NE_ERR_NOT_IN_INIT_STATE: {
  480. printf("Error in start enclave, enclave not in init state\n");
  481. break;
  482. }
  483. case NE_ERR_NO_MEM_REGIONS_ADDED: {
  484. printf("Error in start enclave, no memory regions have been added\n");
  485. break;
  486. }
  487. case NE_ERR_NO_VCPUS_ADDED: {
  488. printf("Error in start enclave, no vCPUs have been added\n");
  489. break;
  490. }
  491. case NE_ERR_FULL_CORES_NOT_USED: {
  492. printf("Error in start enclave, enclave has no full cores set\n");
  493. break;
  494. }
  495. case NE_ERR_ENCLAVE_MEM_MIN_SIZE: {
  496. printf("Error in start enclave, enclave memory is less than min size\n");
  497. break;
  498. }
  499. case NE_ERR_INVALID_FLAG_VALUE: {
  500. printf("Error in start enclave, provided invalid flag\n");
  501. break;
  502. }
  503. case NE_ERR_INVALID_ENCLAVE_CID: {
  504. printf("Error in start enclave, provided invalid enclave CID\n");
  505. break;
  506. }
  507. default:
  508. printf("Error in start enclave [%m]\n");
  509. }
  510. return rc;
  511. }
  512. return 0;
  513. }
  514. /**
  515. * ne_start_enclave_check_booted() - Start the enclave and wait for a hearbeat
  516. * from it, on a newly created vsock channel,
  517. * to check it has booted.
  518. * @enclave_fd : The file descriptor associated with the enclave.
  519. *
  520. * Context: Process context.
  521. * Return:
  522. * * 0 on success.
  523. * * Negative return value on failure.
  524. */
  525. static int ne_start_enclave_check_booted(int enclave_fd)
  526. {
  527. struct sockaddr_vm client_vsock_addr = {};
  528. int client_vsock_fd = -1;
  529. socklen_t client_vsock_len = sizeof(client_vsock_addr);
  530. struct ne_enclave_start_info enclave_start_info = {};
  531. struct pollfd fds[1] = {};
  532. int rc = -EINVAL;
  533. unsigned char recv_buf = 0;
  534. struct sockaddr_vm server_vsock_addr = {
  535. .svm_family = AF_VSOCK,
  536. .svm_cid = NE_IMAGE_LOAD_HEARTBEAT_CID,
  537. .svm_port = NE_IMAGE_LOAD_HEARTBEAT_PORT,
  538. };
  539. int server_vsock_fd = -1;
  540. server_vsock_fd = socket(AF_VSOCK, SOCK_STREAM, 0);
  541. if (server_vsock_fd < 0) {
  542. rc = server_vsock_fd;
  543. printf("Error in socket [%m]\n");
  544. return rc;
  545. }
  546. rc = bind(server_vsock_fd, (struct sockaddr *)&server_vsock_addr,
  547. sizeof(server_vsock_addr));
  548. if (rc < 0) {
  549. printf("Error in bind [%m]\n");
  550. goto out;
  551. }
  552. rc = listen(server_vsock_fd, 1);
  553. if (rc < 0) {
  554. printf("Error in listen [%m]\n");
  555. goto out;
  556. }
  557. rc = ne_start_enclave(enclave_fd, &enclave_start_info);
  558. if (rc < 0)
  559. goto out;
  560. printf("Enclave started, CID %llu\n", enclave_start_info.enclave_cid);
  561. fds[0].fd = server_vsock_fd;
  562. fds[0].events = POLLIN;
  563. rc = poll(fds, 1, NE_POLL_WAIT_TIME_MS);
  564. if (rc < 0) {
  565. printf("Error in poll [%m]\n");
  566. goto out;
  567. }
  568. if (!rc) {
  569. printf("Poll timeout, %d seconds elapsed\n", NE_POLL_WAIT_TIME);
  570. rc = -ETIMEDOUT;
  571. goto out;
  572. }
  573. if ((fds[0].revents & POLLIN) == 0) {
  574. printf("Poll received value %d\n", fds[0].revents);
  575. rc = -EINVAL;
  576. goto out;
  577. }
  578. rc = accept(server_vsock_fd, (struct sockaddr *)&client_vsock_addr,
  579. &client_vsock_len);
  580. if (rc < 0) {
  581. printf("Error in accept [%m]\n");
  582. goto out;
  583. }
  584. client_vsock_fd = rc;
  585. /*
  586. * Read the heartbeat value that the init process in the enclave sends
  587. * after vsock connect.
  588. */
  589. rc = read(client_vsock_fd, &recv_buf, sizeof(recv_buf));
  590. if (rc < 0) {
  591. printf("Error in read [%m]\n");
  592. goto out;
  593. }
  594. if (rc != sizeof(recv_buf) || recv_buf != NE_IMAGE_LOAD_HEARTBEAT_VALUE) {
  595. printf("Read %d instead of %d\n", recv_buf,
  596. NE_IMAGE_LOAD_HEARTBEAT_VALUE);
  597. goto out;
  598. }
  599. /* Write the heartbeat value back. */
  600. rc = write(client_vsock_fd, &recv_buf, sizeof(recv_buf));
  601. if (rc < 0) {
  602. printf("Error in write [%m]\n");
  603. goto out;
  604. }
  605. rc = 0;
  606. out:
  607. close(server_vsock_fd);
  608. return rc;
  609. }
  610. int main(int argc, char *argv[])
  611. {
  612. int enclave_fd = -1;
  613. unsigned int i = 0;
  614. int ne_dev_fd = -1;
  615. struct ne_user_mem_region ne_user_mem_regions[NE_DEFAULT_NR_MEM_REGIONS] = {};
  616. unsigned int ne_vcpus[NE_DEFAULT_NR_VCPUS] = {};
  617. int rc = -EINVAL;
  618. pthread_t thread_id = 0;
  619. unsigned long slot_uid = 0;
  620. if (argc != 2) {
  621. printf("Usage: %s <path_to_enclave_image>\n", argv[0]);
  622. exit(EXIT_FAILURE);
  623. }
  624. if (strlen(argv[1]) >= PATH_MAX) {
  625. printf("The size of the path to enclave image is higher than max path\n");
  626. exit(EXIT_FAILURE);
  627. }
  628. ne_dev_fd = open(NE_DEV_NAME, O_RDWR | O_CLOEXEC);
  629. if (ne_dev_fd < 0) {
  630. printf("Error in open NE device [%m]\n");
  631. exit(EXIT_FAILURE);
  632. }
  633. printf("Creating enclave slot ...\n");
  634. rc = ne_create_vm(ne_dev_fd, &slot_uid, &enclave_fd);
  635. close(ne_dev_fd);
  636. if (rc < 0)
  637. exit(EXIT_FAILURE);
  638. printf("Enclave fd %d\n", enclave_fd);
  639. rc = pthread_create(&thread_id, NULL, ne_poll_enclave_fd, (void *)&enclave_fd);
  640. if (rc < 0) {
  641. printf("Error in thread create [%m]\n");
  642. close(enclave_fd);
  643. exit(EXIT_FAILURE);
  644. }
  645. for (i = 0; i < NE_DEFAULT_NR_MEM_REGIONS; i++) {
  646. ne_user_mem_regions[i].memory_size = NE_MIN_MEM_REGION_SIZE;
  647. rc = ne_alloc_user_mem_region(&ne_user_mem_regions[i]);
  648. if (rc < 0) {
  649. printf("Error in alloc userspace memory region, iter %d\n", i);
  650. goto release_enclave_fd;
  651. }
  652. }
  653. rc = ne_load_enclave_image(enclave_fd, ne_user_mem_regions, argv[1]);
  654. if (rc < 0)
  655. goto release_enclave_fd;
  656. for (i = 0; i < NE_DEFAULT_NR_MEM_REGIONS; i++) {
  657. rc = ne_set_user_mem_region(enclave_fd, ne_user_mem_regions[i]);
  658. if (rc < 0) {
  659. printf("Error in set memory region, iter %d\n", i);
  660. goto release_enclave_fd;
  661. }
  662. }
  663. printf("Enclave memory regions were added\n");
  664. for (i = 0; i < NE_DEFAULT_NR_VCPUS; i++) {
  665. /*
  666. * The vCPU is chosen from the enclave vCPU pool, if the value
  667. * of the vcpu_id is 0.
  668. */
  669. ne_vcpus[i] = 0;
  670. rc = ne_add_vcpu(enclave_fd, &ne_vcpus[i]);
  671. if (rc < 0) {
  672. printf("Error in add vcpu, iter %d\n", i);
  673. goto release_enclave_fd;
  674. }
  675. printf("Added vCPU %d to the enclave\n", ne_vcpus[i]);
  676. }
  677. printf("Enclave vCPUs were added\n");
  678. rc = ne_start_enclave_check_booted(enclave_fd);
  679. if (rc < 0) {
  680. printf("Error in the enclave start / image loading heartbeat logic [rc=%d]\n", rc);
  681. goto release_enclave_fd;
  682. }
  683. printf("Entering sleep for %d seconds ...\n", NE_SLEEP_TIME);
  684. sleep(NE_SLEEP_TIME);
  685. close(enclave_fd);
  686. ne_free_mem_regions(ne_user_mem_regions);
  687. exit(EXIT_SUCCESS);
  688. release_enclave_fd:
  689. close(enclave_fd);
  690. ne_free_mem_regions(ne_user_mem_regions);
  691. exit(EXIT_FAILURE);
  692. }