1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
7 * DOC: Enclave lifetime management driver for Nitro Enclaves (NE).
8 * Nitro is a hypervisor that has been developed by Amazon.
11 #include <linux/anon_inodes.h>
12 #include <linux/capability.h>
13 #include <linux/cpu.h>
14 #include <linux/device.h>
15 #include <linux/file.h>
16 #include <linux/hugetlb.h>
17 #include <linux/limits.h>
18 #include <linux/list.h>
19 #include <linux/miscdevice.h>
21 #include <linux/mman.h>
22 #include <linux/module.h>
23 #include <linux/mutex.h>
24 #include <linux/nitro_enclaves.h>
25 #include <linux/pci.h>
26 #include <linux/poll.h>
27 #include <linux/slab.h>
28 #include <linux/types.h>
29 #include <uapi/linux/vm_sockets.h>
31 #include "ne_misc_dev.h"
32 #include "ne_pci_dev.h"
35 * NE_CPUS_SIZE - Size for max 128 CPUs, for now, in a cpu-list string, comma
36 * separated. The NE CPU pool includes CPUs from a single NUMA
39 #define NE_CPUS_SIZE (512)
42 * NE_EIF_LOAD_OFFSET - The offset where to copy the Enclave Image Format (EIF)
43 * image in enclave memory.
45 #define NE_EIF_LOAD_OFFSET (8 * 1024UL * 1024UL)
48 * NE_MIN_ENCLAVE_MEM_SIZE - The minimum memory size an enclave can be launched
51 #define NE_MIN_ENCLAVE_MEM_SIZE (64 * 1024UL * 1024UL)
54 * NE_MIN_MEM_REGION_SIZE - The minimum size of an enclave memory region.
56 #define NE_MIN_MEM_REGION_SIZE (2 * 1024UL * 1024UL)
59 * NE_PARENT_VM_CID - The CID for the vsock device of the primary / parent VM.
61 #define NE_PARENT_VM_CID (3)
63 static long ne_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
65 static const struct file_operations ne_fops = {
67 .llseek = noop_llseek,
68 .unlocked_ioctl = ne_ioctl,
71 static struct miscdevice ne_misc_dev = {
72 .minor = MISC_DYNAMIC_MINOR,
73 .name = "nitro_enclaves",
78 struct ne_devs ne_devs = {
79 .ne_misc_dev = &ne_misc_dev,
83 * TODO: Update logic to create new sysfs entries instead of using
84 * a kernel parameter e.g. if multiple sysfs files needed.
86 static int ne_set_kernel_param(const char *val, const struct kernel_param *kp);
88 static const struct kernel_param_ops ne_cpu_pool_ops = {
89 .get = param_get_string,
90 .set = ne_set_kernel_param,
93 static char ne_cpus[NE_CPUS_SIZE];
94 static struct kparam_string ne_cpus_arg = {
95 .maxlen = sizeof(ne_cpus),
99 module_param_cb(ne_cpus, &ne_cpu_pool_ops, &ne_cpus_arg, 0644);
100 /* https://www.kernel.org/doc/html/latest/admin-guide/kernel-parameters.html#cpu-lists */
101 MODULE_PARM_DESC(ne_cpus, "<cpu-list> - CPU pool used for Nitro Enclaves");
104 * struct ne_cpu_pool - CPU pool used for Nitro Enclaves.
105 * @avail_threads_per_core: Available full CPU cores to be dedicated to
106 * enclave(s). The cpumasks from the array, indexed
107 * by core id, contain all the threads from the
108 * available cores, that are not set for created
109 * enclave(s). The full CPU cores are part of the
111 * @mutex: Mutex for the access to the NE CPU pool.
112 * @nr_parent_vm_cores : The size of the available threads per core array.
113 * The total number of CPU cores available on the
114 * primary / parent VM.
115 * @nr_threads_per_core: The number of threads that a full CPU core has.
116 * @numa_node: NUMA node of the CPUs in the pool.
119 cpumask_var_t *avail_threads_per_core;
121 unsigned int nr_parent_vm_cores;
122 unsigned int nr_threads_per_core;
126 static struct ne_cpu_pool ne_cpu_pool;
129 * ne_check_enclaves_created() - Verify if at least one enclave has been created.
130 * @void: No parameters provided.
132 * Context: Process context.
134 * * True if at least one enclave is created.
137 static bool ne_check_enclaves_created(void)
139 struct ne_pci_dev *ne_pci_dev = ne_devs.ne_pci_dev;
145 mutex_lock(&ne_pci_dev->enclaves_list_mutex);
147 if (!list_empty(&ne_pci_dev->enclaves_list))
150 mutex_unlock(&ne_pci_dev->enclaves_list_mutex);
156 * ne_setup_cpu_pool() - Set the NE CPU pool after handling sanity checks such
157 * as not sharing CPU cores with the primary / parent VM
158 * or not using CPU 0, which should remain available for
159 * the primary / parent VM. Offline the CPUs from the
160 * pool after the checks passed.
161 * @ne_cpu_list: The CPU list used for setting NE CPU pool.
163 * Context: Process context.
166 * * Negative return value on failure.
168 static int ne_setup_cpu_pool(const char *ne_cpu_list)
171 unsigned int cpu = 0;
172 cpumask_var_t cpu_pool;
173 unsigned int cpu_sibling = 0;
178 if (!zalloc_cpumask_var(&cpu_pool, GFP_KERNEL))
181 mutex_lock(&ne_cpu_pool.mutex);
183 rc = cpulist_parse(ne_cpu_list, cpu_pool);
185 pr_err("%s: Error in cpulist parse [rc=%d]\n", ne_misc_dev.name, rc);
187 goto free_pool_cpumask;
190 cpu = cpumask_any(cpu_pool);
191 if (cpu >= nr_cpu_ids) {
192 pr_err("%s: No CPUs available in CPU pool\n", ne_misc_dev.name);
196 goto free_pool_cpumask;
200 * Check if the CPUs are online, to further get info about them
201 * e.g. numa node, core id, siblings.
203 for_each_cpu(cpu, cpu_pool)
204 if (cpu_is_offline(cpu)) {
205 pr_err("%s: CPU %d is offline, has to be online to get its metadata\n",
206 ne_misc_dev.name, cpu);
210 goto free_pool_cpumask;
214 * Check if the CPUs from the NE CPU pool are from the same NUMA node.
216 for_each_cpu(cpu, cpu_pool)
218 numa_node = cpu_to_node(cpu);
220 pr_err("%s: Invalid NUMA node %d\n",
221 ne_misc_dev.name, numa_node);
225 goto free_pool_cpumask;
228 if (numa_node != cpu_to_node(cpu)) {
229 pr_err("%s: CPUs with different NUMA nodes\n",
234 goto free_pool_cpumask;
239 * Check if CPU 0 and its siblings are included in the provided CPU pool
240 * They should remain available for the primary / parent VM.
242 if (cpumask_test_cpu(0, cpu_pool)) {
243 pr_err("%s: CPU 0 has to remain available\n", ne_misc_dev.name);
247 goto free_pool_cpumask;
250 for_each_cpu(cpu_sibling, topology_sibling_cpumask(0)) {
251 if (cpumask_test_cpu(cpu_sibling, cpu_pool)) {
252 pr_err("%s: CPU sibling %d for CPU 0 is in CPU pool\n",
253 ne_misc_dev.name, cpu_sibling);
257 goto free_pool_cpumask;
262 * Check if CPU siblings are included in the provided CPU pool. The
263 * expectation is that full CPU cores are made available in the CPU pool
266 for_each_cpu(cpu, cpu_pool) {
267 for_each_cpu(cpu_sibling, topology_sibling_cpumask(cpu)) {
268 if (!cpumask_test_cpu(cpu_sibling, cpu_pool)) {
269 pr_err("%s: CPU %d is not in CPU pool\n",
270 ne_misc_dev.name, cpu_sibling);
274 goto free_pool_cpumask;
279 /* Calculate the number of threads from a full CPU core. */
280 cpu = cpumask_any(cpu_pool);
281 for_each_cpu(cpu_sibling, topology_sibling_cpumask(cpu))
282 ne_cpu_pool.nr_threads_per_core++;
284 ne_cpu_pool.nr_parent_vm_cores = nr_cpu_ids / ne_cpu_pool.nr_threads_per_core;
286 ne_cpu_pool.avail_threads_per_core = kcalloc(ne_cpu_pool.nr_parent_vm_cores,
287 sizeof(*ne_cpu_pool.avail_threads_per_core),
289 if (!ne_cpu_pool.avail_threads_per_core) {
292 goto free_pool_cpumask;
295 for (i = 0; i < ne_cpu_pool.nr_parent_vm_cores; i++)
296 if (!zalloc_cpumask_var(&ne_cpu_pool.avail_threads_per_core[i], GFP_KERNEL)) {
299 goto free_cores_cpumask;
303 * Split the NE CPU pool in threads per core to keep the CPU topology
304 * after offlining the CPUs.
306 for_each_cpu(cpu, cpu_pool) {
307 core_id = topology_core_id(cpu);
308 if (core_id < 0 || core_id >= ne_cpu_pool.nr_parent_vm_cores) {
309 pr_err("%s: Invalid core id %d for CPU %d\n",
310 ne_misc_dev.name, core_id, cpu);
317 cpumask_set_cpu(cpu, ne_cpu_pool.avail_threads_per_core[core_id]);
321 * CPUs that are given to enclave(s) should not be considered online
322 * by Linux anymore, as the hypervisor will degrade them to floating.
323 * The physical CPUs (full cores) are carved out of the primary / parent
324 * VM and given to the enclave VM. The same number of vCPUs would run
325 * on less pCPUs for the primary / parent VM.
327 * We offline them here, to not degrade performance and expose correct
328 * topology to Linux and user space.
330 for_each_cpu(cpu, cpu_pool) {
331 rc = remove_cpu(cpu);
333 pr_err("%s: CPU %d is not offlined [rc=%d]\n",
334 ne_misc_dev.name, cpu, rc);
340 free_cpumask_var(cpu_pool);
342 ne_cpu_pool.numa_node = numa_node;
344 mutex_unlock(&ne_cpu_pool.mutex);
349 for_each_cpu(cpu, cpu_pool)
352 for (i = 0; i < ne_cpu_pool.nr_parent_vm_cores; i++)
353 cpumask_clear(ne_cpu_pool.avail_threads_per_core[i]);
355 for (i = 0; i < ne_cpu_pool.nr_parent_vm_cores; i++)
356 free_cpumask_var(ne_cpu_pool.avail_threads_per_core[i]);
357 kfree(ne_cpu_pool.avail_threads_per_core);
359 free_cpumask_var(cpu_pool);
360 ne_cpu_pool.nr_parent_vm_cores = 0;
361 ne_cpu_pool.nr_threads_per_core = 0;
362 ne_cpu_pool.numa_node = -1;
363 mutex_unlock(&ne_cpu_pool.mutex);
369 * ne_teardown_cpu_pool() - Online the CPUs from the NE CPU pool and cleanup the
371 * @void: No parameters provided.
373 * Context: Process context.
375 static void ne_teardown_cpu_pool(void)
377 unsigned int cpu = 0;
381 mutex_lock(&ne_cpu_pool.mutex);
383 if (!ne_cpu_pool.nr_parent_vm_cores) {
384 mutex_unlock(&ne_cpu_pool.mutex);
389 for (i = 0; i < ne_cpu_pool.nr_parent_vm_cores; i++) {
390 for_each_cpu(cpu, ne_cpu_pool.avail_threads_per_core[i]) {
393 pr_err("%s: CPU %d is not onlined [rc=%d]\n",
394 ne_misc_dev.name, cpu, rc);
397 cpumask_clear(ne_cpu_pool.avail_threads_per_core[i]);
399 free_cpumask_var(ne_cpu_pool.avail_threads_per_core[i]);
402 kfree(ne_cpu_pool.avail_threads_per_core);
403 ne_cpu_pool.nr_parent_vm_cores = 0;
404 ne_cpu_pool.nr_threads_per_core = 0;
405 ne_cpu_pool.numa_node = -1;
407 mutex_unlock(&ne_cpu_pool.mutex);
411 * ne_set_kernel_param() - Set the NE CPU pool value via the NE kernel parameter.
412 * @val: NE CPU pool string value.
413 * @kp : NE kernel parameter associated with the NE CPU pool.
415 * Context: Process context.
418 * * Negative return value on failure.
420 static int ne_set_kernel_param(const char *val, const struct kernel_param *kp)
422 char error_val[] = "";
425 if (!capable(CAP_SYS_ADMIN))
428 if (ne_check_enclaves_created()) {
429 pr_err("%s: The CPU pool is used by enclave(s)\n", ne_misc_dev.name);
434 ne_teardown_cpu_pool();
436 rc = ne_setup_cpu_pool(val);
438 pr_err("%s: Error in setup CPU pool [rc=%d]\n", ne_misc_dev.name, rc);
440 param_set_copystring(error_val, kp);
445 rc = param_set_copystring(val, kp);
447 pr_err("%s: Error in param set copystring [rc=%d]\n", ne_misc_dev.name, rc);
449 ne_teardown_cpu_pool();
451 param_set_copystring(error_val, kp);
460 * ne_donated_cpu() - Check if the provided CPU is already used by the enclave.
461 * @ne_enclave : Private data associated with the current enclave.
462 * @cpu: CPU to check if already used.
464 * Context: Process context. This function is called with the ne_enclave mutex held.
466 * * True if the provided CPU is already used by the enclave.
469 static bool ne_donated_cpu(struct ne_enclave *ne_enclave, unsigned int cpu)
471 if (cpumask_test_cpu(cpu, ne_enclave->vcpu_ids))
478 * ne_get_unused_core_from_cpu_pool() - Get the id of a full core from the
480 * @void: No parameters provided.
482 * Context: Process context. This function is called with the ne_enclave and
483 * ne_cpu_pool mutexes held.
486 * * -1 if no CPU core available in the pool.
488 static int ne_get_unused_core_from_cpu_pool(void)
493 for (i = 0; i < ne_cpu_pool.nr_parent_vm_cores; i++)
494 if (!cpumask_empty(ne_cpu_pool.avail_threads_per_core[i])) {
504 * ne_set_enclave_threads_per_core() - Set the threads of the provided core in
505 * the enclave data structure.
506 * @ne_enclave : Private data associated with the current enclave.
507 * @core_id: Core id to get its threads from the NE CPU pool.
508 * @vcpu_id: vCPU id part of the provided core.
510 * Context: Process context. This function is called with the ne_enclave and
511 * ne_cpu_pool mutexes held.
514 * * Negative return value on failure.
516 static int ne_set_enclave_threads_per_core(struct ne_enclave *ne_enclave,
517 int core_id, u32 vcpu_id)
519 unsigned int cpu = 0;
521 if (core_id < 0 && vcpu_id == 0) {
522 dev_err_ratelimited(ne_misc_dev.this_device,
523 "No CPUs available in NE CPU pool\n");
525 return -NE_ERR_NO_CPUS_AVAIL_IN_POOL;
529 dev_err_ratelimited(ne_misc_dev.this_device,
530 "CPU %d is not in NE CPU pool\n", vcpu_id);
532 return -NE_ERR_VCPU_NOT_IN_CPU_POOL;
535 if (core_id >= ne_enclave->nr_parent_vm_cores) {
536 dev_err_ratelimited(ne_misc_dev.this_device,
537 "Invalid core id %d - ne_enclave\n", core_id);
539 return -NE_ERR_VCPU_INVALID_CPU_CORE;
542 for_each_cpu(cpu, ne_cpu_pool.avail_threads_per_core[core_id])
543 cpumask_set_cpu(cpu, ne_enclave->threads_per_core[core_id]);
545 cpumask_clear(ne_cpu_pool.avail_threads_per_core[core_id]);
551 * ne_get_cpu_from_cpu_pool() - Get a CPU from the NE CPU pool, either from the
552 * remaining sibling(s) of a CPU core or the first
553 * sibling of a new CPU core.
554 * @ne_enclave : Private data associated with the current enclave.
555 * @vcpu_id: vCPU to get from the NE CPU pool.
557 * Context: Process context. This function is called with the ne_enclave mutex held.
560 * * Negative return value on failure.
562 static int ne_get_cpu_from_cpu_pool(struct ne_enclave *ne_enclave, u32 *vcpu_id)
565 unsigned int cpu = 0;
570 * If previously allocated a thread of a core to this enclave, first
571 * check remaining sibling(s) for new CPU allocations, so that full
572 * CPU cores are used for the enclave.
574 for (i = 0; i < ne_enclave->nr_parent_vm_cores; i++)
575 for_each_cpu(cpu, ne_enclave->threads_per_core[i])
576 if (!ne_donated_cpu(ne_enclave, cpu)) {
582 mutex_lock(&ne_cpu_pool.mutex);
585 * If no remaining siblings, get a core from the NE CPU pool and keep
586 * track of all the threads in the enclave threads per core data structure.
588 core_id = ne_get_unused_core_from_cpu_pool();
590 rc = ne_set_enclave_threads_per_core(ne_enclave, core_id, *vcpu_id);
594 *vcpu_id = cpumask_any(ne_enclave->threads_per_core[core_id]);
599 mutex_unlock(&ne_cpu_pool.mutex);
605 * ne_get_vcpu_core_from_cpu_pool() - Get from the NE CPU pool the id of the
606 * core associated with the provided vCPU.
607 * @vcpu_id: Provided vCPU id to get its associated core id.
609 * Context: Process context. This function is called with the ne_enclave and
610 * ne_cpu_pool mutexes held.
613 * * -1 if the provided vCPU is not in the pool.
615 static int ne_get_vcpu_core_from_cpu_pool(u32 vcpu_id)
620 for (i = 0; i < ne_cpu_pool.nr_parent_vm_cores; i++)
621 if (cpumask_test_cpu(vcpu_id, ne_cpu_pool.avail_threads_per_core[i])) {
631 * ne_check_cpu_in_cpu_pool() - Check if the given vCPU is in the available CPUs
633 * @ne_enclave : Private data associated with the current enclave.
634 * @vcpu_id: ID of the vCPU to check if available in the NE CPU pool.
636 * Context: Process context. This function is called with the ne_enclave mutex held.
639 * * Negative return value on failure.
641 static int ne_check_cpu_in_cpu_pool(struct ne_enclave *ne_enclave, u32 vcpu_id)
647 if (ne_donated_cpu(ne_enclave, vcpu_id)) {
648 dev_err_ratelimited(ne_misc_dev.this_device,
649 "CPU %d already used\n", vcpu_id);
651 return -NE_ERR_VCPU_ALREADY_USED;
655 * If previously allocated a thread of a core to this enclave, but not
656 * the full core, first check remaining sibling(s).
658 for (i = 0; i < ne_enclave->nr_parent_vm_cores; i++)
659 if (cpumask_test_cpu(vcpu_id, ne_enclave->threads_per_core[i]))
662 mutex_lock(&ne_cpu_pool.mutex);
665 * If no remaining siblings, get from the NE CPU pool the core
666 * associated with the vCPU and keep track of all the threads in the
667 * enclave threads per core data structure.
669 core_id = ne_get_vcpu_core_from_cpu_pool(vcpu_id);
671 rc = ne_set_enclave_threads_per_core(ne_enclave, core_id, vcpu_id);
678 mutex_unlock(&ne_cpu_pool.mutex);
684 * ne_add_vcpu_ioctl() - Add a vCPU to the slot associated with the current
686 * @ne_enclave : Private data associated with the current enclave.
687 * @vcpu_id: ID of the CPU to be associated with the given slot,
690 * Context: Process context. This function is called with the ne_enclave mutex held.
693 * * Negative return value on failure.
695 static int ne_add_vcpu_ioctl(struct ne_enclave *ne_enclave, u32 vcpu_id)
697 struct ne_pci_dev_cmd_reply cmd_reply = {};
698 struct pci_dev *pdev = ne_devs.ne_pci_dev->pdev;
700 struct slot_add_vcpu_req slot_add_vcpu_req = {};
702 if (ne_enclave->mm != current->mm)
705 slot_add_vcpu_req.slot_uid = ne_enclave->slot_uid;
706 slot_add_vcpu_req.vcpu_id = vcpu_id;
708 rc = ne_do_request(pdev, SLOT_ADD_VCPU,
709 &slot_add_vcpu_req, sizeof(slot_add_vcpu_req),
710 &cmd_reply, sizeof(cmd_reply));
712 dev_err_ratelimited(ne_misc_dev.this_device,
713 "Error in slot add vCPU [rc=%d]\n", rc);
718 cpumask_set_cpu(vcpu_id, ne_enclave->vcpu_ids);
720 ne_enclave->nr_vcpus++;
726 * ne_sanity_check_user_mem_region() - Sanity check the user space memory
727 * region received during the set user
728 * memory region ioctl call.
729 * @ne_enclave : Private data associated with the current enclave.
730 * @mem_region : User space memory region to be sanity checked.
732 * Context: Process context. This function is called with the ne_enclave mutex held.
735 * * Negative return value on failure.
737 static int ne_sanity_check_user_mem_region(struct ne_enclave *ne_enclave,
738 struct ne_user_memory_region mem_region)
740 struct ne_mem_region *ne_mem_region = NULL;
742 if (ne_enclave->mm != current->mm)
745 if (mem_region.memory_size & (NE_MIN_MEM_REGION_SIZE - 1)) {
746 dev_err_ratelimited(ne_misc_dev.this_device,
747 "User space memory size is not multiple of 2 MiB\n");
749 return -NE_ERR_INVALID_MEM_REGION_SIZE;
752 if (!IS_ALIGNED(mem_region.userspace_addr, NE_MIN_MEM_REGION_SIZE)) {
753 dev_err_ratelimited(ne_misc_dev.this_device,
754 "User space address is not 2 MiB aligned\n");
756 return -NE_ERR_UNALIGNED_MEM_REGION_ADDR;
759 if ((mem_region.userspace_addr & (NE_MIN_MEM_REGION_SIZE - 1)) ||
760 !access_ok((void __user *)(unsigned long)mem_region.userspace_addr,
761 mem_region.memory_size)) {
762 dev_err_ratelimited(ne_misc_dev.this_device,
763 "Invalid user space address range\n");
765 return -NE_ERR_INVALID_MEM_REGION_ADDR;
768 list_for_each_entry(ne_mem_region, &ne_enclave->mem_regions_list,
769 mem_region_list_entry) {
770 u64 memory_size = ne_mem_region->memory_size;
771 u64 userspace_addr = ne_mem_region->userspace_addr;
773 if ((userspace_addr <= mem_region.userspace_addr &&
774 mem_region.userspace_addr < (userspace_addr + memory_size)) ||
775 (mem_region.userspace_addr <= userspace_addr &&
776 (mem_region.userspace_addr + mem_region.memory_size) > userspace_addr)) {
777 dev_err_ratelimited(ne_misc_dev.this_device,
778 "User space memory region already used\n");
780 return -NE_ERR_MEM_REGION_ALREADY_USED;
788 * ne_sanity_check_user_mem_region_page() - Sanity check a page from the user space
789 * memory region received during the set
790 * user memory region ioctl call.
791 * @ne_enclave : Private data associated with the current enclave.
792 * @mem_region_page: Page from the user space memory region to be sanity checked.
794 * Context: Process context. This function is called with the ne_enclave mutex held.
797 * * Negative return value on failure.
799 static int ne_sanity_check_user_mem_region_page(struct ne_enclave *ne_enclave,
800 struct page *mem_region_page)
802 if (!PageHuge(mem_region_page)) {
803 dev_err_ratelimited(ne_misc_dev.this_device,
804 "Not a hugetlbfs page\n");
806 return -NE_ERR_MEM_NOT_HUGE_PAGE;
809 if (page_size(mem_region_page) & (NE_MIN_MEM_REGION_SIZE - 1)) {
810 dev_err_ratelimited(ne_misc_dev.this_device,
811 "Page size not multiple of 2 MiB\n");
813 return -NE_ERR_INVALID_PAGE_SIZE;
816 if (ne_enclave->numa_node != page_to_nid(mem_region_page)) {
817 dev_err_ratelimited(ne_misc_dev.this_device,
818 "Page is not from NUMA node %d\n",
819 ne_enclave->numa_node);
821 return -NE_ERR_MEM_DIFFERENT_NUMA_NODE;
828 * ne_set_user_memory_region_ioctl() - Add user space memory region to the slot
829 * associated with the current enclave.
830 * @ne_enclave : Private data associated with the current enclave.
831 * @mem_region : User space memory region to be associated with the given slot.
833 * Context: Process context. This function is called with the ne_enclave mutex held.
836 * * Negative return value on failure.
838 static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave,
839 struct ne_user_memory_region mem_region)
843 unsigned long max_nr_pages = 0;
844 unsigned long memory_size = 0;
845 struct ne_mem_region *ne_mem_region = NULL;
846 unsigned long nr_phys_contig_mem_regions = 0;
847 struct pci_dev *pdev = ne_devs.ne_pci_dev->pdev;
848 struct page **phys_contig_mem_regions = NULL;
851 rc = ne_sanity_check_user_mem_region(ne_enclave, mem_region);
855 ne_mem_region = kzalloc(sizeof(*ne_mem_region), GFP_KERNEL);
859 max_nr_pages = mem_region.memory_size / NE_MIN_MEM_REGION_SIZE;
861 ne_mem_region->pages = kcalloc(max_nr_pages, sizeof(*ne_mem_region->pages),
863 if (!ne_mem_region->pages) {
866 goto free_mem_region;
869 phys_contig_mem_regions = kcalloc(max_nr_pages, sizeof(*phys_contig_mem_regions),
871 if (!phys_contig_mem_regions) {
874 goto free_mem_region;
878 i = ne_mem_region->nr_pages;
880 if (i == max_nr_pages) {
881 dev_err_ratelimited(ne_misc_dev.this_device,
882 "Reached max nr of pages in the pages data struct\n");
889 gup_rc = get_user_pages_unlocked(mem_region.userspace_addr + memory_size, 1,
890 ne_mem_region->pages + i, FOLL_GET);
895 dev_err_ratelimited(ne_misc_dev.this_device,
896 "Error in get user pages [rc=%d]\n", rc);
901 rc = ne_sanity_check_user_mem_region_page(ne_enclave, ne_mem_region->pages[i]);
906 * TODO: Update once handled non-contiguous memory regions
907 * received from user space or contiguous physical memory regions
908 * larger than 2 MiB e.g. 8 MiB.
910 phys_contig_mem_regions[i] = ne_mem_region->pages[i];
912 memory_size += page_size(ne_mem_region->pages[i]);
914 ne_mem_region->nr_pages++;
915 } while (memory_size < mem_region.memory_size);
918 * TODO: Update once handled non-contiguous memory regions received
919 * from user space or contiguous physical memory regions larger than
922 nr_phys_contig_mem_regions = ne_mem_region->nr_pages;
924 if ((ne_enclave->nr_mem_regions + nr_phys_contig_mem_regions) >
925 ne_enclave->max_mem_regions) {
926 dev_err_ratelimited(ne_misc_dev.this_device,
927 "Reached max memory regions %lld\n",
928 ne_enclave->max_mem_regions);
930 rc = -NE_ERR_MEM_MAX_REGIONS;
935 for (i = 0; i < nr_phys_contig_mem_regions; i++) {
936 u64 phys_region_addr = page_to_phys(phys_contig_mem_regions[i]);
937 u64 phys_region_size = page_size(phys_contig_mem_regions[i]);
939 if (phys_region_size & (NE_MIN_MEM_REGION_SIZE - 1)) {
940 dev_err_ratelimited(ne_misc_dev.this_device,
941 "Physical mem region size is not multiple of 2 MiB\n");
948 if (!IS_ALIGNED(phys_region_addr, NE_MIN_MEM_REGION_SIZE)) {
949 dev_err_ratelimited(ne_misc_dev.this_device,
950 "Physical mem region address is not 2 MiB aligned\n");
958 ne_mem_region->memory_size = mem_region.memory_size;
959 ne_mem_region->userspace_addr = mem_region.userspace_addr;
961 list_add(&ne_mem_region->mem_region_list_entry, &ne_enclave->mem_regions_list);
963 for (i = 0; i < nr_phys_contig_mem_regions; i++) {
964 struct ne_pci_dev_cmd_reply cmd_reply = {};
965 struct slot_add_mem_req slot_add_mem_req = {};
967 slot_add_mem_req.slot_uid = ne_enclave->slot_uid;
968 slot_add_mem_req.paddr = page_to_phys(phys_contig_mem_regions[i]);
969 slot_add_mem_req.size = page_size(phys_contig_mem_regions[i]);
971 rc = ne_do_request(pdev, SLOT_ADD_MEM,
972 &slot_add_mem_req, sizeof(slot_add_mem_req),
973 &cmd_reply, sizeof(cmd_reply));
975 dev_err_ratelimited(ne_misc_dev.this_device,
976 "Error in slot add mem [rc=%d]\n", rc);
978 kfree(phys_contig_mem_regions);
981 * Exit here without put pages as memory regions may
982 * already been added.
987 ne_enclave->mem_size += slot_add_mem_req.size;
988 ne_enclave->nr_mem_regions++;
991 kfree(phys_contig_mem_regions);
996 for (i = 0; i < ne_mem_region->nr_pages; i++)
997 put_page(ne_mem_region->pages[i]);
999 kfree(phys_contig_mem_regions);
1000 kfree(ne_mem_region->pages);
1001 kfree(ne_mem_region);
1007 * ne_start_enclave_ioctl() - Trigger enclave start after the enclave resources,
1008 * such as memory and CPU, have been set.
1009 * @ne_enclave : Private data associated with the current enclave.
1010 * @enclave_start_info : Enclave info that includes enclave cid and flags.
1012 * Context: Process context. This function is called with the ne_enclave mutex held.
1015 * * Negative return value on failure.
1017 static int ne_start_enclave_ioctl(struct ne_enclave *ne_enclave,
1018 struct ne_enclave_start_info *enclave_start_info)
1020 struct ne_pci_dev_cmd_reply cmd_reply = {};
1021 unsigned int cpu = 0;
1022 struct enclave_start_req enclave_start_req = {};
1024 struct pci_dev *pdev = ne_devs.ne_pci_dev->pdev;
1027 if (!ne_enclave->nr_mem_regions) {
1028 dev_err_ratelimited(ne_misc_dev.this_device,
1029 "Enclave has no mem regions\n");
1031 return -NE_ERR_NO_MEM_REGIONS_ADDED;
1034 if (ne_enclave->mem_size < NE_MIN_ENCLAVE_MEM_SIZE) {
1035 dev_err_ratelimited(ne_misc_dev.this_device,
1036 "Enclave memory is less than %ld\n",
1037 NE_MIN_ENCLAVE_MEM_SIZE);
1039 return -NE_ERR_ENCLAVE_MEM_MIN_SIZE;
1042 if (!ne_enclave->nr_vcpus) {
1043 dev_err_ratelimited(ne_misc_dev.this_device,
1044 "Enclave has no vCPUs\n");
1046 return -NE_ERR_NO_VCPUS_ADDED;
1049 for (i = 0; i < ne_enclave->nr_parent_vm_cores; i++)
1050 for_each_cpu(cpu, ne_enclave->threads_per_core[i])
1051 if (!cpumask_test_cpu(cpu, ne_enclave->vcpu_ids)) {
1052 dev_err_ratelimited(ne_misc_dev.this_device,
1053 "Full CPU cores not used\n");
1055 return -NE_ERR_FULL_CORES_NOT_USED;
1058 enclave_start_req.enclave_cid = enclave_start_info->enclave_cid;
1059 enclave_start_req.flags = enclave_start_info->flags;
1060 enclave_start_req.slot_uid = ne_enclave->slot_uid;
1062 rc = ne_do_request(pdev, ENCLAVE_START,
1063 &enclave_start_req, sizeof(enclave_start_req),
1064 &cmd_reply, sizeof(cmd_reply));
1066 dev_err_ratelimited(ne_misc_dev.this_device,
1067 "Error in enclave start [rc=%d]\n", rc);
1072 ne_enclave->state = NE_STATE_RUNNING;
1074 enclave_start_info->enclave_cid = cmd_reply.enclave_cid;
1080 * ne_enclave_ioctl() - Ioctl function provided by the enclave file.
1081 * @file: File associated with this ioctl function.
1082 * @cmd: The command that is set for the ioctl call.
1083 * @arg: The argument that is provided for the ioctl call.
1085 * Context: Process context.
1088 * * Negative return value on failure.
1090 static long ne_enclave_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1092 struct ne_enclave *ne_enclave = file->private_data;
1099 if (copy_from_user(&vcpu_id, (void __user *)arg, sizeof(vcpu_id)))
1102 mutex_lock(&ne_enclave->enclave_info_mutex);
1104 if (ne_enclave->state != NE_STATE_INIT) {
1105 dev_err_ratelimited(ne_misc_dev.this_device,
1106 "Enclave is not in init state\n");
1108 mutex_unlock(&ne_enclave->enclave_info_mutex);
1110 return -NE_ERR_NOT_IN_INIT_STATE;
1113 if (vcpu_id >= (ne_enclave->nr_parent_vm_cores *
1114 ne_enclave->nr_threads_per_core)) {
1115 dev_err_ratelimited(ne_misc_dev.this_device,
1116 "vCPU id higher than max CPU id\n");
1118 mutex_unlock(&ne_enclave->enclave_info_mutex);
1120 return -NE_ERR_INVALID_VCPU;
1124 /* Use the CPU pool for choosing a CPU for the enclave. */
1125 rc = ne_get_cpu_from_cpu_pool(ne_enclave, &vcpu_id);
1127 dev_err_ratelimited(ne_misc_dev.this_device,
1128 "Error in get CPU from pool [rc=%d]\n",
1131 mutex_unlock(&ne_enclave->enclave_info_mutex);
1136 /* Check if the provided vCPU is available in the NE CPU pool. */
1137 rc = ne_check_cpu_in_cpu_pool(ne_enclave, vcpu_id);
1139 dev_err_ratelimited(ne_misc_dev.this_device,
1140 "Error in check CPU %d in pool [rc=%d]\n",
1143 mutex_unlock(&ne_enclave->enclave_info_mutex);
1149 rc = ne_add_vcpu_ioctl(ne_enclave, vcpu_id);
1151 mutex_unlock(&ne_enclave->enclave_info_mutex);
1156 mutex_unlock(&ne_enclave->enclave_info_mutex);
1158 if (copy_to_user((void __user *)arg, &vcpu_id, sizeof(vcpu_id)))
1164 case NE_GET_IMAGE_LOAD_INFO: {
1165 struct ne_image_load_info image_load_info = {};
1167 if (copy_from_user(&image_load_info, (void __user *)arg, sizeof(image_load_info)))
1170 mutex_lock(&ne_enclave->enclave_info_mutex);
1172 if (ne_enclave->state != NE_STATE_INIT) {
1173 dev_err_ratelimited(ne_misc_dev.this_device,
1174 "Enclave is not in init state\n");
1176 mutex_unlock(&ne_enclave->enclave_info_mutex);
1178 return -NE_ERR_NOT_IN_INIT_STATE;
1181 mutex_unlock(&ne_enclave->enclave_info_mutex);
1183 if (!image_load_info.flags ||
1184 image_load_info.flags >= NE_IMAGE_LOAD_MAX_FLAG_VAL) {
1185 dev_err_ratelimited(ne_misc_dev.this_device,
1186 "Incorrect flag in enclave image load info\n");
1188 return -NE_ERR_INVALID_FLAG_VALUE;
1191 if (image_load_info.flags == NE_EIF_IMAGE)
1192 image_load_info.memory_offset = NE_EIF_LOAD_OFFSET;
1194 if (copy_to_user((void __user *)arg, &image_load_info, sizeof(image_load_info)))
1200 case NE_SET_USER_MEMORY_REGION: {
1201 struct ne_user_memory_region mem_region = {};
1204 if (copy_from_user(&mem_region, (void __user *)arg, sizeof(mem_region)))
1207 if (mem_region.flags >= NE_MEMORY_REGION_MAX_FLAG_VAL) {
1208 dev_err_ratelimited(ne_misc_dev.this_device,
1209 "Incorrect flag for user memory region\n");
1211 return -NE_ERR_INVALID_FLAG_VALUE;
1214 mutex_lock(&ne_enclave->enclave_info_mutex);
1216 if (ne_enclave->state != NE_STATE_INIT) {
1217 dev_err_ratelimited(ne_misc_dev.this_device,
1218 "Enclave is not in init state\n");
1220 mutex_unlock(&ne_enclave->enclave_info_mutex);
1222 return -NE_ERR_NOT_IN_INIT_STATE;
1225 rc = ne_set_user_memory_region_ioctl(ne_enclave, mem_region);
1227 mutex_unlock(&ne_enclave->enclave_info_mutex);
1232 mutex_unlock(&ne_enclave->enclave_info_mutex);
1237 case NE_START_ENCLAVE: {
1238 struct ne_enclave_start_info enclave_start_info = {};
1241 if (copy_from_user(&enclave_start_info, (void __user *)arg,
1242 sizeof(enclave_start_info)))
1245 if (enclave_start_info.flags >= NE_ENCLAVE_START_MAX_FLAG_VAL) {
1246 dev_err_ratelimited(ne_misc_dev.this_device,
1247 "Incorrect flag in enclave start info\n");
1249 return -NE_ERR_INVALID_FLAG_VALUE;
1253 * Do not use well-known CIDs - 0, 1, 2 - for enclaves.
1254 * VMADDR_CID_ANY = -1U
1255 * VMADDR_CID_HYPERVISOR = 0
1256 * VMADDR_CID_LOCAL = 1
1257 * VMADDR_CID_HOST = 2
1258 * Note: 0 is used as a placeholder to auto-generate an enclave CID.
1259 * http://man7.org/linux/man-pages/man7/vsock.7.html
1261 if (enclave_start_info.enclave_cid > 0 &&
1262 enclave_start_info.enclave_cid <= VMADDR_CID_HOST) {
1263 dev_err_ratelimited(ne_misc_dev.this_device,
1264 "Well-known CID value, not to be used for enclaves\n");
1266 return -NE_ERR_INVALID_ENCLAVE_CID;
1269 if (enclave_start_info.enclave_cid == U32_MAX) {
1270 dev_err_ratelimited(ne_misc_dev.this_device,
1271 "Well-known CID value, not to be used for enclaves\n");
1273 return -NE_ERR_INVALID_ENCLAVE_CID;
1277 * Do not use the CID of the primary / parent VM for enclaves.
1279 if (enclave_start_info.enclave_cid == NE_PARENT_VM_CID) {
1280 dev_err_ratelimited(ne_misc_dev.this_device,
1281 "CID of the parent VM, not to be used for enclaves\n");
1283 return -NE_ERR_INVALID_ENCLAVE_CID;
1286 /* 64-bit CIDs are not yet supported for the vsock device. */
1287 if (enclave_start_info.enclave_cid > U32_MAX) {
1288 dev_err_ratelimited(ne_misc_dev.this_device,
1289 "64-bit CIDs not yet supported for the vsock device\n");
1291 return -NE_ERR_INVALID_ENCLAVE_CID;
1294 mutex_lock(&ne_enclave->enclave_info_mutex);
1296 if (ne_enclave->state != NE_STATE_INIT) {
1297 dev_err_ratelimited(ne_misc_dev.this_device,
1298 "Enclave is not in init state\n");
1300 mutex_unlock(&ne_enclave->enclave_info_mutex);
1302 return -NE_ERR_NOT_IN_INIT_STATE;
1305 rc = ne_start_enclave_ioctl(ne_enclave, &enclave_start_info);
1307 mutex_unlock(&ne_enclave->enclave_info_mutex);
1312 mutex_unlock(&ne_enclave->enclave_info_mutex);
1314 if (copy_to_user((void __user *)arg, &enclave_start_info,
1315 sizeof(enclave_start_info)))
1329 * ne_enclave_remove_all_mem_region_entries() - Remove all memory region entries
1330 * from the enclave data structure.
1331 * @ne_enclave : Private data associated with the current enclave.
1333 * Context: Process context. This function is called with the ne_enclave mutex held.
1335 static void ne_enclave_remove_all_mem_region_entries(struct ne_enclave *ne_enclave)
1337 unsigned long i = 0;
1338 struct ne_mem_region *ne_mem_region = NULL;
1339 struct ne_mem_region *ne_mem_region_tmp = NULL;
1341 list_for_each_entry_safe(ne_mem_region, ne_mem_region_tmp,
1342 &ne_enclave->mem_regions_list,
1343 mem_region_list_entry) {
1344 list_del(&ne_mem_region->mem_region_list_entry);
1346 for (i = 0; i < ne_mem_region->nr_pages; i++)
1347 put_page(ne_mem_region->pages[i]);
1349 kfree(ne_mem_region->pages);
1351 kfree(ne_mem_region);
1356 * ne_enclave_remove_all_vcpu_id_entries() - Remove all vCPU id entries from
1357 * the enclave data structure.
1358 * @ne_enclave : Private data associated with the current enclave.
1360 * Context: Process context. This function is called with the ne_enclave mutex held.
1362 static void ne_enclave_remove_all_vcpu_id_entries(struct ne_enclave *ne_enclave)
1364 unsigned int cpu = 0;
1367 mutex_lock(&ne_cpu_pool.mutex);
1369 for (i = 0; i < ne_enclave->nr_parent_vm_cores; i++) {
1370 for_each_cpu(cpu, ne_enclave->threads_per_core[i])
1371 /* Update the available NE CPU pool. */
1372 cpumask_set_cpu(cpu, ne_cpu_pool.avail_threads_per_core[i]);
1374 free_cpumask_var(ne_enclave->threads_per_core[i]);
1377 mutex_unlock(&ne_cpu_pool.mutex);
1379 kfree(ne_enclave->threads_per_core);
1381 free_cpumask_var(ne_enclave->vcpu_ids);
1385 * ne_pci_dev_remove_enclave_entry() - Remove the enclave entry from the data
1386 * structure that is part of the NE PCI
1387 * device private data.
1388 * @ne_enclave : Private data associated with the current enclave.
1389 * @ne_pci_dev : Private data associated with the PCI device.
1391 * Context: Process context. This function is called with the ne_pci_dev enclave
1394 static void ne_pci_dev_remove_enclave_entry(struct ne_enclave *ne_enclave,
1395 struct ne_pci_dev *ne_pci_dev)
1397 struct ne_enclave *ne_enclave_entry = NULL;
1398 struct ne_enclave *ne_enclave_entry_tmp = NULL;
1400 list_for_each_entry_safe(ne_enclave_entry, ne_enclave_entry_tmp,
1401 &ne_pci_dev->enclaves_list, enclave_list_entry) {
1402 if (ne_enclave_entry->slot_uid == ne_enclave->slot_uid) {
1403 list_del(&ne_enclave_entry->enclave_list_entry);
1411 * ne_enclave_release() - Release function provided by the enclave file.
1412 * @inode: Inode associated with this file release function.
1413 * @file: File associated with this release function.
1415 * Context: Process context.
1418 * * Negative return value on failure.
1420 static int ne_enclave_release(struct inode *inode, struct file *file)
1422 struct ne_pci_dev_cmd_reply cmd_reply = {};
1423 struct enclave_stop_req enclave_stop_request = {};
1424 struct ne_enclave *ne_enclave = file->private_data;
1425 struct ne_pci_dev *ne_pci_dev = ne_devs.ne_pci_dev;
1426 struct pci_dev *pdev = ne_pci_dev->pdev;
1428 struct slot_free_req slot_free_req = {};
1434 * Early exit in case there is an error in the enclave creation logic
1435 * and fput() is called on the cleanup path.
1437 if (!ne_enclave->slot_uid)
1441 * Acquire the enclave list mutex before the enclave mutex
1442 * in order to avoid deadlocks with @ref ne_event_work_handler.
1444 mutex_lock(&ne_pci_dev->enclaves_list_mutex);
1445 mutex_lock(&ne_enclave->enclave_info_mutex);
1447 if (ne_enclave->state != NE_STATE_INIT && ne_enclave->state != NE_STATE_STOPPED) {
1448 enclave_stop_request.slot_uid = ne_enclave->slot_uid;
1450 rc = ne_do_request(pdev, ENCLAVE_STOP,
1451 &enclave_stop_request, sizeof(enclave_stop_request),
1452 &cmd_reply, sizeof(cmd_reply));
1454 dev_err_ratelimited(ne_misc_dev.this_device,
1455 "Error in enclave stop [rc=%d]\n", rc);
1460 memset(&cmd_reply, 0, sizeof(cmd_reply));
1463 slot_free_req.slot_uid = ne_enclave->slot_uid;
1465 rc = ne_do_request(pdev, SLOT_FREE,
1466 &slot_free_req, sizeof(slot_free_req),
1467 &cmd_reply, sizeof(cmd_reply));
1469 dev_err_ratelimited(ne_misc_dev.this_device,
1470 "Error in slot free [rc=%d]\n", rc);
1475 ne_pci_dev_remove_enclave_entry(ne_enclave, ne_pci_dev);
1476 ne_enclave_remove_all_mem_region_entries(ne_enclave);
1477 ne_enclave_remove_all_vcpu_id_entries(ne_enclave);
1479 mutex_unlock(&ne_enclave->enclave_info_mutex);
1480 mutex_unlock(&ne_pci_dev->enclaves_list_mutex);
1487 mutex_unlock(&ne_enclave->enclave_info_mutex);
1488 mutex_unlock(&ne_pci_dev->enclaves_list_mutex);
1494 * ne_enclave_poll() - Poll functionality used for enclave out-of-band events.
1495 * @file: File associated with this poll function.
1496 * @wait: Poll table data structure.
1498 * Context: Process context.
1502 static __poll_t ne_enclave_poll(struct file *file, poll_table *wait)
1505 struct ne_enclave *ne_enclave = file->private_data;
1507 poll_wait(file, &ne_enclave->eventq, wait);
1509 if (ne_enclave->has_event)
1515 static const struct file_operations ne_enclave_fops = {
1516 .owner = THIS_MODULE,
1517 .llseek = noop_llseek,
1518 .poll = ne_enclave_poll,
1519 .unlocked_ioctl = ne_enclave_ioctl,
1520 .release = ne_enclave_release,
1524 * ne_create_vm_ioctl() - Alloc slot to be associated with an enclave. Create
1525 * enclave file descriptor to be further used for enclave
1526 * resources handling e.g. memory regions and CPUs.
1527 * @ne_pci_dev : Private data associated with the PCI device.
1528 * @slot_uid: User pointer to store the generated unique slot id
1529 * associated with an enclave to.
1531 * Context: Process context. This function is called with the ne_pci_dev enclave
1534 * * Enclave fd on success.
1535 * * Negative return value on failure.
1537 static int ne_create_vm_ioctl(struct ne_pci_dev *ne_pci_dev, u64 __user *slot_uid)
1539 struct ne_pci_dev_cmd_reply cmd_reply = {};
1540 int enclave_fd = -1;
1541 struct file *enclave_file = NULL;
1543 struct ne_enclave *ne_enclave = NULL;
1544 struct pci_dev *pdev = ne_pci_dev->pdev;
1546 struct slot_alloc_req slot_alloc_req = {};
1548 mutex_lock(&ne_cpu_pool.mutex);
1550 for (i = 0; i < ne_cpu_pool.nr_parent_vm_cores; i++)
1551 if (!cpumask_empty(ne_cpu_pool.avail_threads_per_core[i]))
1554 if (i == ne_cpu_pool.nr_parent_vm_cores) {
1555 dev_err_ratelimited(ne_misc_dev.this_device,
1556 "No CPUs available in CPU pool\n");
1558 mutex_unlock(&ne_cpu_pool.mutex);
1560 return -NE_ERR_NO_CPUS_AVAIL_IN_POOL;
1563 mutex_unlock(&ne_cpu_pool.mutex);
1565 ne_enclave = kzalloc(sizeof(*ne_enclave), GFP_KERNEL);
1569 mutex_lock(&ne_cpu_pool.mutex);
1571 ne_enclave->nr_parent_vm_cores = ne_cpu_pool.nr_parent_vm_cores;
1572 ne_enclave->nr_threads_per_core = ne_cpu_pool.nr_threads_per_core;
1573 ne_enclave->numa_node = ne_cpu_pool.numa_node;
1575 mutex_unlock(&ne_cpu_pool.mutex);
1577 ne_enclave->threads_per_core = kcalloc(ne_enclave->nr_parent_vm_cores,
1578 sizeof(*ne_enclave->threads_per_core), GFP_KERNEL);
1579 if (!ne_enclave->threads_per_core) {
1582 goto free_ne_enclave;
1585 for (i = 0; i < ne_enclave->nr_parent_vm_cores; i++)
1586 if (!zalloc_cpumask_var(&ne_enclave->threads_per_core[i], GFP_KERNEL)) {
1592 if (!zalloc_cpumask_var(&ne_enclave->vcpu_ids, GFP_KERNEL)) {
1598 enclave_fd = get_unused_fd_flags(O_CLOEXEC);
1599 if (enclave_fd < 0) {
1602 dev_err_ratelimited(ne_misc_dev.this_device,
1603 "Error in getting unused fd [rc=%d]\n", rc);
1608 enclave_file = anon_inode_getfile("ne-vm", &ne_enclave_fops, ne_enclave, O_RDWR);
1609 if (IS_ERR(enclave_file)) {
1610 rc = PTR_ERR(enclave_file);
1612 dev_err_ratelimited(ne_misc_dev.this_device,
1613 "Error in anon inode get file [rc=%d]\n", rc);
1618 rc = ne_do_request(pdev, SLOT_ALLOC,
1619 &slot_alloc_req, sizeof(slot_alloc_req),
1620 &cmd_reply, sizeof(cmd_reply));
1622 dev_err_ratelimited(ne_misc_dev.this_device,
1623 "Error in slot alloc [rc=%d]\n", rc);
1628 init_waitqueue_head(&ne_enclave->eventq);
1629 ne_enclave->has_event = false;
1630 mutex_init(&ne_enclave->enclave_info_mutex);
1631 ne_enclave->max_mem_regions = cmd_reply.mem_regions;
1632 INIT_LIST_HEAD(&ne_enclave->mem_regions_list);
1633 ne_enclave->mm = current->mm;
1634 ne_enclave->slot_uid = cmd_reply.slot_uid;
1635 ne_enclave->state = NE_STATE_INIT;
1637 list_add(&ne_enclave->enclave_list_entry, &ne_pci_dev->enclaves_list);
1639 if (copy_to_user(slot_uid, &ne_enclave->slot_uid, sizeof(ne_enclave->slot_uid))) {
1641 * As we're holding the only reference to 'enclave_file', fput()
1642 * will call ne_enclave_release() which will do a proper cleanup
1643 * of all so far allocated resources, leaving only the unused fd
1647 put_unused_fd(enclave_fd);
1652 fd_install(enclave_fd, enclave_file);
1659 put_unused_fd(enclave_fd);
1661 free_cpumask_var(ne_enclave->vcpu_ids);
1662 for (i = 0; i < ne_enclave->nr_parent_vm_cores; i++)
1663 free_cpumask_var(ne_enclave->threads_per_core[i]);
1664 kfree(ne_enclave->threads_per_core);
1672 * ne_ioctl() - Ioctl function provided by the NE misc device.
1673 * @file: File associated with this ioctl function.
1674 * @cmd: The command that is set for the ioctl call.
1675 * @arg: The argument that is provided for the ioctl call.
1677 * Context: Process context.
1679 * * Ioctl result (e.g. enclave file descriptor) on success.
1680 * * Negative return value on failure.
1682 static long ne_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1685 case NE_CREATE_VM: {
1686 int enclave_fd = -1;
1687 struct ne_pci_dev *ne_pci_dev = ne_devs.ne_pci_dev;
1688 u64 __user *slot_uid = (void __user *)arg;
1690 mutex_lock(&ne_pci_dev->enclaves_list_mutex);
1691 enclave_fd = ne_create_vm_ioctl(ne_pci_dev, slot_uid);
1692 mutex_unlock(&ne_pci_dev->enclaves_list_mutex);
1704 static int __init ne_init(void)
1706 mutex_init(&ne_cpu_pool.mutex);
1708 return pci_register_driver(&ne_pci_driver);
1711 static void __exit ne_exit(void)
1713 pci_unregister_driver(&ne_pci_driver);
1715 ne_teardown_cpu_pool();
1718 module_init(ne_init);
1719 module_exit(ne_exit);
1721 MODULE_AUTHOR("Amazon.com, Inc. or its affiliates");
1722 MODULE_DESCRIPTION("Nitro Enclaves Driver");
1723 MODULE_LICENSE("GPL v2");