1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2020-2021 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/range.h>
28 #include <linux/slab.h>
29 #include <linux/types.h>
30 #include <uapi/linux/vm_sockets.h>
32 #include "ne_misc_dev.h"
33 #include "ne_pci_dev.h"
36 * NE_CPUS_SIZE - Size for max 128 CPUs, for now, in a cpu-list string, comma
37 * separated. The NE CPU pool includes CPUs from a single NUMA
40 #define NE_CPUS_SIZE (512)
43 * NE_EIF_LOAD_OFFSET - The offset where to copy the Enclave Image Format (EIF)
44 * image in enclave memory.
46 #define NE_EIF_LOAD_OFFSET (8 * 1024UL * 1024UL)
49 * NE_MIN_ENCLAVE_MEM_SIZE - The minimum memory size an enclave can be launched
52 #define NE_MIN_ENCLAVE_MEM_SIZE (64 * 1024UL * 1024UL)
55 * NE_MIN_MEM_REGION_SIZE - The minimum size of an enclave memory region.
57 #define NE_MIN_MEM_REGION_SIZE (2 * 1024UL * 1024UL)
60 * NE_PARENT_VM_CID - The CID for the vsock device of the primary / parent VM.
62 #define NE_PARENT_VM_CID (3)
64 static long ne_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
);
66 static const struct file_operations ne_fops
= {
68 .llseek
= noop_llseek
,
69 .unlocked_ioctl
= ne_ioctl
,
72 static struct miscdevice ne_misc_dev
= {
73 .minor
= MISC_DYNAMIC_MINOR
,
74 .name
= "nitro_enclaves",
79 struct ne_devs ne_devs
= {
80 .ne_misc_dev
= &ne_misc_dev
,
84 * TODO: Update logic to create new sysfs entries instead of using
85 * a kernel parameter e.g. if multiple sysfs files needed.
87 static int ne_set_kernel_param(const char *val
, const struct kernel_param
*kp
);
89 static const struct kernel_param_ops ne_cpu_pool_ops
= {
90 .get
= param_get_string
,
91 .set
= ne_set_kernel_param
,
94 static char ne_cpus
[NE_CPUS_SIZE
];
95 static struct kparam_string ne_cpus_arg
= {
96 .maxlen
= sizeof(ne_cpus
),
100 module_param_cb(ne_cpus
, &ne_cpu_pool_ops
, &ne_cpus_arg
, 0644);
101 /* https://www.kernel.org/doc/html/latest/admin-guide/kernel-parameters.html#cpu-lists */
102 MODULE_PARM_DESC(ne_cpus
, "<cpu-list> - CPU pool used for Nitro Enclaves");
105 * struct ne_cpu_pool - CPU pool used for Nitro Enclaves.
106 * @avail_threads_per_core: Available full CPU cores to be dedicated to
107 * enclave(s). The cpumasks from the array, indexed
108 * by core id, contain all the threads from the
109 * available cores, that are not set for created
110 * enclave(s). The full CPU cores are part of the
112 * @mutex: Mutex for the access to the NE CPU pool.
113 * @nr_parent_vm_cores : The size of the available threads per core array.
114 * The total number of CPU cores available on the
115 * primary / parent VM.
116 * @nr_threads_per_core: The number of threads that a full CPU core has.
117 * @numa_node: NUMA node of the CPUs in the pool.
120 cpumask_var_t
*avail_threads_per_core
;
122 unsigned int nr_parent_vm_cores
;
123 unsigned int nr_threads_per_core
;
127 static struct ne_cpu_pool ne_cpu_pool
;
130 * struct ne_phys_contig_mem_regions - Contiguous physical memory regions.
131 * @num: The number of regions that currently has.
132 * @regions: The array of physical memory regions.
134 struct ne_phys_contig_mem_regions
{
136 struct range
*regions
;
140 * ne_check_enclaves_created() - Verify if at least one enclave has been created.
141 * @void: No parameters provided.
143 * Context: Process context.
145 * * True if at least one enclave is created.
148 static bool ne_check_enclaves_created(void)
150 struct ne_pci_dev
*ne_pci_dev
= ne_devs
.ne_pci_dev
;
156 mutex_lock(&ne_pci_dev
->enclaves_list_mutex
);
158 if (!list_empty(&ne_pci_dev
->enclaves_list
))
161 mutex_unlock(&ne_pci_dev
->enclaves_list_mutex
);
167 * ne_setup_cpu_pool() - Set the NE CPU pool after handling sanity checks such
168 * as not sharing CPU cores with the primary / parent VM
169 * or not using CPU 0, which should remain available for
170 * the primary / parent VM. Offline the CPUs from the
171 * pool after the checks passed.
172 * @ne_cpu_list: The CPU list used for setting NE CPU pool.
174 * Context: Process context.
177 * * Negative return value on failure.
179 static int ne_setup_cpu_pool(const char *ne_cpu_list
)
182 unsigned int cpu
= 0;
183 cpumask_var_t cpu_pool
;
184 unsigned int cpu_sibling
= 0;
189 if (!zalloc_cpumask_var(&cpu_pool
, GFP_KERNEL
))
192 mutex_lock(&ne_cpu_pool
.mutex
);
194 rc
= cpulist_parse(ne_cpu_list
, cpu_pool
);
196 pr_err("%s: Error in cpulist parse [rc=%d]\n", ne_misc_dev
.name
, rc
);
198 goto free_pool_cpumask
;
201 cpu
= cpumask_any(cpu_pool
);
202 if (cpu
>= nr_cpu_ids
) {
203 pr_err("%s: No CPUs available in CPU pool\n", ne_misc_dev
.name
);
207 goto free_pool_cpumask
;
211 * Check if the CPUs are online, to further get info about them
212 * e.g. numa node, core id, siblings.
214 for_each_cpu(cpu
, cpu_pool
)
215 if (cpu_is_offline(cpu
)) {
216 pr_err("%s: CPU %d is offline, has to be online to get its metadata\n",
217 ne_misc_dev
.name
, cpu
);
221 goto free_pool_cpumask
;
225 * Check if the CPUs from the NE CPU pool are from the same NUMA node.
227 for_each_cpu(cpu
, cpu_pool
)
229 numa_node
= cpu_to_node(cpu
);
231 pr_err("%s: Invalid NUMA node %d\n",
232 ne_misc_dev
.name
, numa_node
);
236 goto free_pool_cpumask
;
239 if (numa_node
!= cpu_to_node(cpu
)) {
240 pr_err("%s: CPUs with different NUMA nodes\n",
245 goto free_pool_cpumask
;
250 * Check if CPU 0 and its siblings are included in the provided CPU pool
251 * They should remain available for the primary / parent VM.
253 if (cpumask_test_cpu(0, cpu_pool
)) {
254 pr_err("%s: CPU 0 has to remain available\n", ne_misc_dev
.name
);
258 goto free_pool_cpumask
;
261 for_each_cpu(cpu_sibling
, topology_sibling_cpumask(0)) {
262 if (cpumask_test_cpu(cpu_sibling
, cpu_pool
)) {
263 pr_err("%s: CPU sibling %d for CPU 0 is in CPU pool\n",
264 ne_misc_dev
.name
, cpu_sibling
);
268 goto free_pool_cpumask
;
273 * Check if CPU siblings are included in the provided CPU pool. The
274 * expectation is that full CPU cores are made available in the CPU pool
277 for_each_cpu(cpu
, cpu_pool
) {
278 for_each_cpu(cpu_sibling
, topology_sibling_cpumask(cpu
)) {
279 if (!cpumask_test_cpu(cpu_sibling
, cpu_pool
)) {
280 pr_err("%s: CPU %d is not in CPU pool\n",
281 ne_misc_dev
.name
, cpu_sibling
);
285 goto free_pool_cpumask
;
290 /* Calculate the number of threads from a full CPU core. */
291 cpu
= cpumask_any(cpu_pool
);
292 for_each_cpu(cpu_sibling
, topology_sibling_cpumask(cpu
))
293 ne_cpu_pool
.nr_threads_per_core
++;
295 ne_cpu_pool
.nr_parent_vm_cores
= nr_cpu_ids
/ ne_cpu_pool
.nr_threads_per_core
;
297 ne_cpu_pool
.avail_threads_per_core
= kcalloc(ne_cpu_pool
.nr_parent_vm_cores
,
298 sizeof(*ne_cpu_pool
.avail_threads_per_core
),
300 if (!ne_cpu_pool
.avail_threads_per_core
) {
303 goto free_pool_cpumask
;
306 for (i
= 0; i
< ne_cpu_pool
.nr_parent_vm_cores
; i
++)
307 if (!zalloc_cpumask_var(&ne_cpu_pool
.avail_threads_per_core
[i
], GFP_KERNEL
)) {
310 goto free_cores_cpumask
;
314 * Split the NE CPU pool in threads per core to keep the CPU topology
315 * after offlining the CPUs.
317 for_each_cpu(cpu
, cpu_pool
) {
318 core_id
= topology_core_id(cpu
);
319 if (core_id
< 0 || core_id
>= ne_cpu_pool
.nr_parent_vm_cores
) {
320 pr_err("%s: Invalid core id %d for CPU %d\n",
321 ne_misc_dev
.name
, core_id
, cpu
);
328 cpumask_set_cpu(cpu
, ne_cpu_pool
.avail_threads_per_core
[core_id
]);
332 * CPUs that are given to enclave(s) should not be considered online
333 * by Linux anymore, as the hypervisor will degrade them to floating.
334 * The physical CPUs (full cores) are carved out of the primary / parent
335 * VM and given to the enclave VM. The same number of vCPUs would run
336 * on less pCPUs for the primary / parent VM.
338 * We offline them here, to not degrade performance and expose correct
339 * topology to Linux and user space.
341 for_each_cpu(cpu
, cpu_pool
) {
342 rc
= remove_cpu(cpu
);
344 pr_err("%s: CPU %d is not offlined [rc=%d]\n",
345 ne_misc_dev
.name
, cpu
, rc
);
351 free_cpumask_var(cpu_pool
);
353 ne_cpu_pool
.numa_node
= numa_node
;
355 mutex_unlock(&ne_cpu_pool
.mutex
);
360 for_each_cpu(cpu
, cpu_pool
)
363 for (i
= 0; i
< ne_cpu_pool
.nr_parent_vm_cores
; i
++)
364 cpumask_clear(ne_cpu_pool
.avail_threads_per_core
[i
]);
366 for (i
= 0; i
< ne_cpu_pool
.nr_parent_vm_cores
; i
++)
367 free_cpumask_var(ne_cpu_pool
.avail_threads_per_core
[i
]);
368 kfree(ne_cpu_pool
.avail_threads_per_core
);
370 free_cpumask_var(cpu_pool
);
371 ne_cpu_pool
.nr_parent_vm_cores
= 0;
372 ne_cpu_pool
.nr_threads_per_core
= 0;
373 ne_cpu_pool
.numa_node
= -1;
374 mutex_unlock(&ne_cpu_pool
.mutex
);
380 * ne_teardown_cpu_pool() - Online the CPUs from the NE CPU pool and cleanup the
382 * @void: No parameters provided.
384 * Context: Process context.
386 static void ne_teardown_cpu_pool(void)
388 unsigned int cpu
= 0;
392 mutex_lock(&ne_cpu_pool
.mutex
);
394 if (!ne_cpu_pool
.nr_parent_vm_cores
) {
395 mutex_unlock(&ne_cpu_pool
.mutex
);
400 for (i
= 0; i
< ne_cpu_pool
.nr_parent_vm_cores
; i
++) {
401 for_each_cpu(cpu
, ne_cpu_pool
.avail_threads_per_core
[i
]) {
404 pr_err("%s: CPU %d is not onlined [rc=%d]\n",
405 ne_misc_dev
.name
, cpu
, rc
);
408 cpumask_clear(ne_cpu_pool
.avail_threads_per_core
[i
]);
410 free_cpumask_var(ne_cpu_pool
.avail_threads_per_core
[i
]);
413 kfree(ne_cpu_pool
.avail_threads_per_core
);
414 ne_cpu_pool
.nr_parent_vm_cores
= 0;
415 ne_cpu_pool
.nr_threads_per_core
= 0;
416 ne_cpu_pool
.numa_node
= -1;
418 mutex_unlock(&ne_cpu_pool
.mutex
);
422 * ne_set_kernel_param() - Set the NE CPU pool value via the NE kernel parameter.
423 * @val: NE CPU pool string value.
424 * @kp : NE kernel parameter associated with the NE CPU pool.
426 * Context: Process context.
429 * * Negative return value on failure.
431 static int ne_set_kernel_param(const char *val
, const struct kernel_param
*kp
)
433 char error_val
[] = "";
436 if (!capable(CAP_SYS_ADMIN
))
439 if (ne_check_enclaves_created()) {
440 pr_err("%s: The CPU pool is used by enclave(s)\n", ne_misc_dev
.name
);
445 ne_teardown_cpu_pool();
447 rc
= ne_setup_cpu_pool(val
);
449 pr_err("%s: Error in setup CPU pool [rc=%d]\n", ne_misc_dev
.name
, rc
);
451 param_set_copystring(error_val
, kp
);
456 rc
= param_set_copystring(val
, kp
);
458 pr_err("%s: Error in param set copystring [rc=%d]\n", ne_misc_dev
.name
, rc
);
460 ne_teardown_cpu_pool();
462 param_set_copystring(error_val
, kp
);
471 * ne_donated_cpu() - Check if the provided CPU is already used by the enclave.
472 * @ne_enclave : Private data associated with the current enclave.
473 * @cpu: CPU to check if already used.
475 * Context: Process context. This function is called with the ne_enclave mutex held.
477 * * True if the provided CPU is already used by the enclave.
480 static bool ne_donated_cpu(struct ne_enclave
*ne_enclave
, unsigned int cpu
)
482 if (cpumask_test_cpu(cpu
, ne_enclave
->vcpu_ids
))
489 * ne_get_unused_core_from_cpu_pool() - Get the id of a full core from the
491 * @void: No parameters provided.
493 * Context: Process context. This function is called with the ne_enclave and
494 * ne_cpu_pool mutexes held.
497 * * -1 if no CPU core available in the pool.
499 static int ne_get_unused_core_from_cpu_pool(void)
504 for (i
= 0; i
< ne_cpu_pool
.nr_parent_vm_cores
; i
++)
505 if (!cpumask_empty(ne_cpu_pool
.avail_threads_per_core
[i
])) {
515 * ne_set_enclave_threads_per_core() - Set the threads of the provided core in
516 * the enclave data structure.
517 * @ne_enclave : Private data associated with the current enclave.
518 * @core_id: Core id to get its threads from the NE CPU pool.
519 * @vcpu_id: vCPU id part of the provided core.
521 * Context: Process context. This function is called with the ne_enclave and
522 * ne_cpu_pool mutexes held.
525 * * Negative return value on failure.
527 static int ne_set_enclave_threads_per_core(struct ne_enclave
*ne_enclave
,
528 int core_id
, u32 vcpu_id
)
530 unsigned int cpu
= 0;
532 if (core_id
< 0 && vcpu_id
== 0) {
533 dev_err_ratelimited(ne_misc_dev
.this_device
,
534 "No CPUs available in NE CPU pool\n");
536 return -NE_ERR_NO_CPUS_AVAIL_IN_POOL
;
540 dev_err_ratelimited(ne_misc_dev
.this_device
,
541 "CPU %d is not in NE CPU pool\n", vcpu_id
);
543 return -NE_ERR_VCPU_NOT_IN_CPU_POOL
;
546 if (core_id
>= ne_enclave
->nr_parent_vm_cores
) {
547 dev_err_ratelimited(ne_misc_dev
.this_device
,
548 "Invalid core id %d - ne_enclave\n", core_id
);
550 return -NE_ERR_VCPU_INVALID_CPU_CORE
;
553 for_each_cpu(cpu
, ne_cpu_pool
.avail_threads_per_core
[core_id
])
554 cpumask_set_cpu(cpu
, ne_enclave
->threads_per_core
[core_id
]);
556 cpumask_clear(ne_cpu_pool
.avail_threads_per_core
[core_id
]);
562 * ne_get_cpu_from_cpu_pool() - Get a CPU from the NE CPU pool, either from the
563 * remaining sibling(s) of a CPU core or the first
564 * sibling of a new CPU core.
565 * @ne_enclave : Private data associated with the current enclave.
566 * @vcpu_id: vCPU to get from the NE CPU pool.
568 * Context: Process context. This function is called with the ne_enclave mutex held.
571 * * Negative return value on failure.
573 static int ne_get_cpu_from_cpu_pool(struct ne_enclave
*ne_enclave
, u32
*vcpu_id
)
576 unsigned int cpu
= 0;
581 * If previously allocated a thread of a core to this enclave, first
582 * check remaining sibling(s) for new CPU allocations, so that full
583 * CPU cores are used for the enclave.
585 for (i
= 0; i
< ne_enclave
->nr_parent_vm_cores
; i
++)
586 for_each_cpu(cpu
, ne_enclave
->threads_per_core
[i
])
587 if (!ne_donated_cpu(ne_enclave
, cpu
)) {
593 mutex_lock(&ne_cpu_pool
.mutex
);
596 * If no remaining siblings, get a core from the NE CPU pool and keep
597 * track of all the threads in the enclave threads per core data structure.
599 core_id
= ne_get_unused_core_from_cpu_pool();
601 rc
= ne_set_enclave_threads_per_core(ne_enclave
, core_id
, *vcpu_id
);
605 *vcpu_id
= cpumask_any(ne_enclave
->threads_per_core
[core_id
]);
610 mutex_unlock(&ne_cpu_pool
.mutex
);
616 * ne_get_vcpu_core_from_cpu_pool() - Get from the NE CPU pool the id of the
617 * core associated with the provided vCPU.
618 * @vcpu_id: Provided vCPU id to get its associated core id.
620 * Context: Process context. This function is called with the ne_enclave and
621 * ne_cpu_pool mutexes held.
624 * * -1 if the provided vCPU is not in the pool.
626 static int ne_get_vcpu_core_from_cpu_pool(u32 vcpu_id
)
631 for (i
= 0; i
< ne_cpu_pool
.nr_parent_vm_cores
; i
++)
632 if (cpumask_test_cpu(vcpu_id
, ne_cpu_pool
.avail_threads_per_core
[i
])) {
642 * ne_check_cpu_in_cpu_pool() - Check if the given vCPU is in the available CPUs
644 * @ne_enclave : Private data associated with the current enclave.
645 * @vcpu_id: ID of the vCPU to check if available in the NE CPU pool.
647 * Context: Process context. This function is called with the ne_enclave mutex held.
650 * * Negative return value on failure.
652 static int ne_check_cpu_in_cpu_pool(struct ne_enclave
*ne_enclave
, u32 vcpu_id
)
658 if (ne_donated_cpu(ne_enclave
, vcpu_id
)) {
659 dev_err_ratelimited(ne_misc_dev
.this_device
,
660 "CPU %d already used\n", vcpu_id
);
662 return -NE_ERR_VCPU_ALREADY_USED
;
666 * If previously allocated a thread of a core to this enclave, but not
667 * the full core, first check remaining sibling(s).
669 for (i
= 0; i
< ne_enclave
->nr_parent_vm_cores
; i
++)
670 if (cpumask_test_cpu(vcpu_id
, ne_enclave
->threads_per_core
[i
]))
673 mutex_lock(&ne_cpu_pool
.mutex
);
676 * If no remaining siblings, get from the NE CPU pool the core
677 * associated with the vCPU and keep track of all the threads in the
678 * enclave threads per core data structure.
680 core_id
= ne_get_vcpu_core_from_cpu_pool(vcpu_id
);
682 rc
= ne_set_enclave_threads_per_core(ne_enclave
, core_id
, vcpu_id
);
689 mutex_unlock(&ne_cpu_pool
.mutex
);
695 * ne_add_vcpu_ioctl() - Add a vCPU to the slot associated with the current
697 * @ne_enclave : Private data associated with the current enclave.
698 * @vcpu_id: ID of the CPU to be associated with the given slot,
701 * Context: Process context. This function is called with the ne_enclave mutex held.
704 * * Negative return value on failure.
706 static int ne_add_vcpu_ioctl(struct ne_enclave
*ne_enclave
, u32 vcpu_id
)
708 struct ne_pci_dev_cmd_reply cmd_reply
= {};
709 struct pci_dev
*pdev
= ne_devs
.ne_pci_dev
->pdev
;
711 struct slot_add_vcpu_req slot_add_vcpu_req
= {};
713 if (ne_enclave
->mm
!= current
->mm
)
716 slot_add_vcpu_req
.slot_uid
= ne_enclave
->slot_uid
;
717 slot_add_vcpu_req
.vcpu_id
= vcpu_id
;
719 rc
= ne_do_request(pdev
, SLOT_ADD_VCPU
,
720 &slot_add_vcpu_req
, sizeof(slot_add_vcpu_req
),
721 &cmd_reply
, sizeof(cmd_reply
));
723 dev_err_ratelimited(ne_misc_dev
.this_device
,
724 "Error in slot add vCPU [rc=%d]\n", rc
);
729 cpumask_set_cpu(vcpu_id
, ne_enclave
->vcpu_ids
);
731 ne_enclave
->nr_vcpus
++;
737 * ne_sanity_check_user_mem_region() - Sanity check the user space memory
738 * region received during the set user
739 * memory region ioctl call.
740 * @ne_enclave : Private data associated with the current enclave.
741 * @mem_region : User space memory region to be sanity checked.
743 * Context: Process context. This function is called with the ne_enclave mutex held.
746 * * Negative return value on failure.
748 static int ne_sanity_check_user_mem_region(struct ne_enclave
*ne_enclave
,
749 struct ne_user_memory_region mem_region
)
751 struct ne_mem_region
*ne_mem_region
= NULL
;
753 if (ne_enclave
->mm
!= current
->mm
)
756 if (mem_region
.memory_size
& (NE_MIN_MEM_REGION_SIZE
- 1)) {
757 dev_err_ratelimited(ne_misc_dev
.this_device
,
758 "User space memory size is not multiple of 2 MiB\n");
760 return -NE_ERR_INVALID_MEM_REGION_SIZE
;
763 if (!IS_ALIGNED(mem_region
.userspace_addr
, NE_MIN_MEM_REGION_SIZE
)) {
764 dev_err_ratelimited(ne_misc_dev
.this_device
,
765 "User space address is not 2 MiB aligned\n");
767 return -NE_ERR_UNALIGNED_MEM_REGION_ADDR
;
770 if ((mem_region
.userspace_addr
& (NE_MIN_MEM_REGION_SIZE
- 1)) ||
771 !access_ok((void __user
*)(unsigned long)mem_region
.userspace_addr
,
772 mem_region
.memory_size
)) {
773 dev_err_ratelimited(ne_misc_dev
.this_device
,
774 "Invalid user space address range\n");
776 return -NE_ERR_INVALID_MEM_REGION_ADDR
;
779 list_for_each_entry(ne_mem_region
, &ne_enclave
->mem_regions_list
,
780 mem_region_list_entry
) {
781 u64 memory_size
= ne_mem_region
->memory_size
;
782 u64 userspace_addr
= ne_mem_region
->userspace_addr
;
784 if ((userspace_addr
<= mem_region
.userspace_addr
&&
785 mem_region
.userspace_addr
< (userspace_addr
+ memory_size
)) ||
786 (mem_region
.userspace_addr
<= userspace_addr
&&
787 (mem_region
.userspace_addr
+ mem_region
.memory_size
) > userspace_addr
)) {
788 dev_err_ratelimited(ne_misc_dev
.this_device
,
789 "User space memory region already used\n");
791 return -NE_ERR_MEM_REGION_ALREADY_USED
;
799 * ne_sanity_check_user_mem_region_page() - Sanity check a page from the user space
800 * memory region received during the set
801 * user memory region ioctl call.
802 * @ne_enclave : Private data associated with the current enclave.
803 * @mem_region_page: Page from the user space memory region to be sanity checked.
805 * Context: Process context. This function is called with the ne_enclave mutex held.
808 * * Negative return value on failure.
810 static int ne_sanity_check_user_mem_region_page(struct ne_enclave
*ne_enclave
,
811 struct page
*mem_region_page
)
813 if (!PageHuge(mem_region_page
)) {
814 dev_err_ratelimited(ne_misc_dev
.this_device
,
815 "Not a hugetlbfs page\n");
817 return -NE_ERR_MEM_NOT_HUGE_PAGE
;
820 if (page_size(mem_region_page
) & (NE_MIN_MEM_REGION_SIZE
- 1)) {
821 dev_err_ratelimited(ne_misc_dev
.this_device
,
822 "Page size not multiple of 2 MiB\n");
824 return -NE_ERR_INVALID_PAGE_SIZE
;
827 if (ne_enclave
->numa_node
!= page_to_nid(mem_region_page
)) {
828 dev_err_ratelimited(ne_misc_dev
.this_device
,
829 "Page is not from NUMA node %d\n",
830 ne_enclave
->numa_node
);
832 return -NE_ERR_MEM_DIFFERENT_NUMA_NODE
;
839 * ne_sanity_check_phys_mem_region() - Sanity check the start address and the size
840 * of a physical memory region.
841 * @phys_mem_region_paddr : Physical start address of the region to be sanity checked.
842 * @phys_mem_region_size : Length of the region to be sanity checked.
844 * Context: Process context. This function is called with the ne_enclave mutex held.
847 * * Negative return value on failure.
849 static int ne_sanity_check_phys_mem_region(u64 phys_mem_region_paddr
,
850 u64 phys_mem_region_size
)
852 if (phys_mem_region_size
& (NE_MIN_MEM_REGION_SIZE
- 1)) {
853 dev_err_ratelimited(ne_misc_dev
.this_device
,
854 "Physical mem region size is not multiple of 2 MiB\n");
859 if (!IS_ALIGNED(phys_mem_region_paddr
, NE_MIN_MEM_REGION_SIZE
)) {
860 dev_err_ratelimited(ne_misc_dev
.this_device
,
861 "Physical mem region address is not 2 MiB aligned\n");
870 * ne_merge_phys_contig_memory_regions() - Add a memory region and merge the adjacent
871 * regions if they are physically contiguous.
872 * @phys_contig_regions : Private data associated with the contiguous physical memory regions.
873 * @page_paddr : Physical start address of the region to be added.
874 * @page_size : Length of the region to be added.
876 * Context: Process context. This function is called with the ne_enclave mutex held.
879 * * Negative return value on failure.
882 ne_merge_phys_contig_memory_regions(struct ne_phys_contig_mem_regions
*phys_contig_regions
,
883 u64 page_paddr
, u64 page_size
)
885 unsigned long num
= phys_contig_regions
->num
;
888 rc
= ne_sanity_check_phys_mem_region(page_paddr
, page_size
);
892 /* Physically contiguous, just merge */
893 if (num
&& (phys_contig_regions
->regions
[num
- 1].end
+ 1) == page_paddr
) {
894 phys_contig_regions
->regions
[num
- 1].end
+= page_size
;
896 phys_contig_regions
->regions
[num
].start
= page_paddr
;
897 phys_contig_regions
->regions
[num
].end
= page_paddr
+ page_size
- 1;
898 phys_contig_regions
->num
++;
905 * ne_set_user_memory_region_ioctl() - Add user space memory region to the slot
906 * associated with the current enclave.
907 * @ne_enclave : Private data associated with the current enclave.
908 * @mem_region : User space memory region to be associated with the given slot.
910 * Context: Process context. This function is called with the ne_enclave mutex held.
913 * * Negative return value on failure.
915 static int ne_set_user_memory_region_ioctl(struct ne_enclave
*ne_enclave
,
916 struct ne_user_memory_region mem_region
)
920 unsigned long max_nr_pages
= 0;
921 unsigned long memory_size
= 0;
922 struct ne_mem_region
*ne_mem_region
= NULL
;
923 struct pci_dev
*pdev
= ne_devs
.ne_pci_dev
->pdev
;
924 struct ne_phys_contig_mem_regions phys_contig_mem_regions
= {};
927 rc
= ne_sanity_check_user_mem_region(ne_enclave
, mem_region
);
931 ne_mem_region
= kzalloc(sizeof(*ne_mem_region
), GFP_KERNEL
);
935 max_nr_pages
= mem_region
.memory_size
/ NE_MIN_MEM_REGION_SIZE
;
937 ne_mem_region
->pages
= kcalloc(max_nr_pages
, sizeof(*ne_mem_region
->pages
),
939 if (!ne_mem_region
->pages
) {
942 goto free_mem_region
;
945 phys_contig_mem_regions
.regions
= kcalloc(max_nr_pages
,
946 sizeof(*phys_contig_mem_regions
.regions
),
948 if (!phys_contig_mem_regions
.regions
) {
951 goto free_mem_region
;
955 i
= ne_mem_region
->nr_pages
;
957 if (i
== max_nr_pages
) {
958 dev_err_ratelimited(ne_misc_dev
.this_device
,
959 "Reached max nr of pages in the pages data struct\n");
966 gup_rc
= get_user_pages_unlocked(mem_region
.userspace_addr
+ memory_size
, 1,
967 ne_mem_region
->pages
+ i
, FOLL_GET
);
972 dev_err_ratelimited(ne_misc_dev
.this_device
,
973 "Error in get user pages [rc=%d]\n", rc
);
978 rc
= ne_sanity_check_user_mem_region_page(ne_enclave
, ne_mem_region
->pages
[i
]);
982 rc
= ne_merge_phys_contig_memory_regions(&phys_contig_mem_regions
,
983 page_to_phys(ne_mem_region
->pages
[i
]),
984 page_size(ne_mem_region
->pages
[i
]));
988 memory_size
+= page_size(ne_mem_region
->pages
[i
]);
990 ne_mem_region
->nr_pages
++;
991 } while (memory_size
< mem_region
.memory_size
);
993 if ((ne_enclave
->nr_mem_regions
+ phys_contig_mem_regions
.num
) >
994 ne_enclave
->max_mem_regions
) {
995 dev_err_ratelimited(ne_misc_dev
.this_device
,
996 "Reached max memory regions %lld\n",
997 ne_enclave
->max_mem_regions
);
999 rc
= -NE_ERR_MEM_MAX_REGIONS
;
1004 for (i
= 0; i
< phys_contig_mem_regions
.num
; i
++) {
1005 u64 phys_region_addr
= phys_contig_mem_regions
.regions
[i
].start
;
1006 u64 phys_region_size
= range_len(&phys_contig_mem_regions
.regions
[i
]);
1008 rc
= ne_sanity_check_phys_mem_region(phys_region_addr
, phys_region_size
);
1013 ne_mem_region
->memory_size
= mem_region
.memory_size
;
1014 ne_mem_region
->userspace_addr
= mem_region
.userspace_addr
;
1016 list_add(&ne_mem_region
->mem_region_list_entry
, &ne_enclave
->mem_regions_list
);
1018 for (i
= 0; i
< phys_contig_mem_regions
.num
; i
++) {
1019 struct ne_pci_dev_cmd_reply cmd_reply
= {};
1020 struct slot_add_mem_req slot_add_mem_req
= {};
1022 slot_add_mem_req
.slot_uid
= ne_enclave
->slot_uid
;
1023 slot_add_mem_req
.paddr
= phys_contig_mem_regions
.regions
[i
].start
;
1024 slot_add_mem_req
.size
= range_len(&phys_contig_mem_regions
.regions
[i
]);
1026 rc
= ne_do_request(pdev
, SLOT_ADD_MEM
,
1027 &slot_add_mem_req
, sizeof(slot_add_mem_req
),
1028 &cmd_reply
, sizeof(cmd_reply
));
1030 dev_err_ratelimited(ne_misc_dev
.this_device
,
1031 "Error in slot add mem [rc=%d]\n", rc
);
1033 kfree(phys_contig_mem_regions
.regions
);
1036 * Exit here without put pages as memory regions may
1037 * already been added.
1042 ne_enclave
->mem_size
+= slot_add_mem_req
.size
;
1043 ne_enclave
->nr_mem_regions
++;
1046 kfree(phys_contig_mem_regions
.regions
);
1051 for (i
= 0; i
< ne_mem_region
->nr_pages
; i
++)
1052 put_page(ne_mem_region
->pages
[i
]);
1054 kfree(phys_contig_mem_regions
.regions
);
1055 kfree(ne_mem_region
->pages
);
1056 kfree(ne_mem_region
);
1062 * ne_start_enclave_ioctl() - Trigger enclave start after the enclave resources,
1063 * such as memory and CPU, have been set.
1064 * @ne_enclave : Private data associated with the current enclave.
1065 * @enclave_start_info : Enclave info that includes enclave cid and flags.
1067 * Context: Process context. This function is called with the ne_enclave mutex held.
1070 * * Negative return value on failure.
1072 static int ne_start_enclave_ioctl(struct ne_enclave
*ne_enclave
,
1073 struct ne_enclave_start_info
*enclave_start_info
)
1075 struct ne_pci_dev_cmd_reply cmd_reply
= {};
1076 unsigned int cpu
= 0;
1077 struct enclave_start_req enclave_start_req
= {};
1079 struct pci_dev
*pdev
= ne_devs
.ne_pci_dev
->pdev
;
1082 if (!ne_enclave
->nr_mem_regions
) {
1083 dev_err_ratelimited(ne_misc_dev
.this_device
,
1084 "Enclave has no mem regions\n");
1086 return -NE_ERR_NO_MEM_REGIONS_ADDED
;
1089 if (ne_enclave
->mem_size
< NE_MIN_ENCLAVE_MEM_SIZE
) {
1090 dev_err_ratelimited(ne_misc_dev
.this_device
,
1091 "Enclave memory is less than %ld\n",
1092 NE_MIN_ENCLAVE_MEM_SIZE
);
1094 return -NE_ERR_ENCLAVE_MEM_MIN_SIZE
;
1097 if (!ne_enclave
->nr_vcpus
) {
1098 dev_err_ratelimited(ne_misc_dev
.this_device
,
1099 "Enclave has no vCPUs\n");
1101 return -NE_ERR_NO_VCPUS_ADDED
;
1104 for (i
= 0; i
< ne_enclave
->nr_parent_vm_cores
; i
++)
1105 for_each_cpu(cpu
, ne_enclave
->threads_per_core
[i
])
1106 if (!cpumask_test_cpu(cpu
, ne_enclave
->vcpu_ids
)) {
1107 dev_err_ratelimited(ne_misc_dev
.this_device
,
1108 "Full CPU cores not used\n");
1110 return -NE_ERR_FULL_CORES_NOT_USED
;
1113 enclave_start_req
.enclave_cid
= enclave_start_info
->enclave_cid
;
1114 enclave_start_req
.flags
= enclave_start_info
->flags
;
1115 enclave_start_req
.slot_uid
= ne_enclave
->slot_uid
;
1117 rc
= ne_do_request(pdev
, ENCLAVE_START
,
1118 &enclave_start_req
, sizeof(enclave_start_req
),
1119 &cmd_reply
, sizeof(cmd_reply
));
1121 dev_err_ratelimited(ne_misc_dev
.this_device
,
1122 "Error in enclave start [rc=%d]\n", rc
);
1127 ne_enclave
->state
= NE_STATE_RUNNING
;
1129 enclave_start_info
->enclave_cid
= cmd_reply
.enclave_cid
;
1135 * ne_enclave_ioctl() - Ioctl function provided by the enclave file.
1136 * @file: File associated with this ioctl function.
1137 * @cmd: The command that is set for the ioctl call.
1138 * @arg: The argument that is provided for the ioctl call.
1140 * Context: Process context.
1143 * * Negative return value on failure.
1145 static long ne_enclave_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1147 struct ne_enclave
*ne_enclave
= file
->private_data
;
1154 if (copy_from_user(&vcpu_id
, (void __user
*)arg
, sizeof(vcpu_id
)))
1157 mutex_lock(&ne_enclave
->enclave_info_mutex
);
1159 if (ne_enclave
->state
!= NE_STATE_INIT
) {
1160 dev_err_ratelimited(ne_misc_dev
.this_device
,
1161 "Enclave is not in init state\n");
1163 mutex_unlock(&ne_enclave
->enclave_info_mutex
);
1165 return -NE_ERR_NOT_IN_INIT_STATE
;
1168 if (vcpu_id
>= (ne_enclave
->nr_parent_vm_cores
*
1169 ne_enclave
->nr_threads_per_core
)) {
1170 dev_err_ratelimited(ne_misc_dev
.this_device
,
1171 "vCPU id higher than max CPU id\n");
1173 mutex_unlock(&ne_enclave
->enclave_info_mutex
);
1175 return -NE_ERR_INVALID_VCPU
;
1179 /* Use the CPU pool for choosing a CPU for the enclave. */
1180 rc
= ne_get_cpu_from_cpu_pool(ne_enclave
, &vcpu_id
);
1182 dev_err_ratelimited(ne_misc_dev
.this_device
,
1183 "Error in get CPU from pool [rc=%d]\n",
1186 mutex_unlock(&ne_enclave
->enclave_info_mutex
);
1191 /* Check if the provided vCPU is available in the NE CPU pool. */
1192 rc
= ne_check_cpu_in_cpu_pool(ne_enclave
, vcpu_id
);
1194 dev_err_ratelimited(ne_misc_dev
.this_device
,
1195 "Error in check CPU %d in pool [rc=%d]\n",
1198 mutex_unlock(&ne_enclave
->enclave_info_mutex
);
1204 rc
= ne_add_vcpu_ioctl(ne_enclave
, vcpu_id
);
1206 mutex_unlock(&ne_enclave
->enclave_info_mutex
);
1211 mutex_unlock(&ne_enclave
->enclave_info_mutex
);
1213 if (copy_to_user((void __user
*)arg
, &vcpu_id
, sizeof(vcpu_id
)))
1219 case NE_GET_IMAGE_LOAD_INFO
: {
1220 struct ne_image_load_info image_load_info
= {};
1222 if (copy_from_user(&image_load_info
, (void __user
*)arg
, sizeof(image_load_info
)))
1225 mutex_lock(&ne_enclave
->enclave_info_mutex
);
1227 if (ne_enclave
->state
!= NE_STATE_INIT
) {
1228 dev_err_ratelimited(ne_misc_dev
.this_device
,
1229 "Enclave is not in init state\n");
1231 mutex_unlock(&ne_enclave
->enclave_info_mutex
);
1233 return -NE_ERR_NOT_IN_INIT_STATE
;
1236 mutex_unlock(&ne_enclave
->enclave_info_mutex
);
1238 if (!image_load_info
.flags
||
1239 image_load_info
.flags
>= NE_IMAGE_LOAD_MAX_FLAG_VAL
) {
1240 dev_err_ratelimited(ne_misc_dev
.this_device
,
1241 "Incorrect flag in enclave image load info\n");
1243 return -NE_ERR_INVALID_FLAG_VALUE
;
1246 if (image_load_info
.flags
== NE_EIF_IMAGE
)
1247 image_load_info
.memory_offset
= NE_EIF_LOAD_OFFSET
;
1249 if (copy_to_user((void __user
*)arg
, &image_load_info
, sizeof(image_load_info
)))
1255 case NE_SET_USER_MEMORY_REGION
: {
1256 struct ne_user_memory_region mem_region
= {};
1259 if (copy_from_user(&mem_region
, (void __user
*)arg
, sizeof(mem_region
)))
1262 if (mem_region
.flags
>= NE_MEMORY_REGION_MAX_FLAG_VAL
) {
1263 dev_err_ratelimited(ne_misc_dev
.this_device
,
1264 "Incorrect flag for user memory region\n");
1266 return -NE_ERR_INVALID_FLAG_VALUE
;
1269 mutex_lock(&ne_enclave
->enclave_info_mutex
);
1271 if (ne_enclave
->state
!= NE_STATE_INIT
) {
1272 dev_err_ratelimited(ne_misc_dev
.this_device
,
1273 "Enclave is not in init state\n");
1275 mutex_unlock(&ne_enclave
->enclave_info_mutex
);
1277 return -NE_ERR_NOT_IN_INIT_STATE
;
1280 rc
= ne_set_user_memory_region_ioctl(ne_enclave
, mem_region
);
1282 mutex_unlock(&ne_enclave
->enclave_info_mutex
);
1287 mutex_unlock(&ne_enclave
->enclave_info_mutex
);
1292 case NE_START_ENCLAVE
: {
1293 struct ne_enclave_start_info enclave_start_info
= {};
1296 if (copy_from_user(&enclave_start_info
, (void __user
*)arg
,
1297 sizeof(enclave_start_info
)))
1300 if (enclave_start_info
.flags
>= NE_ENCLAVE_START_MAX_FLAG_VAL
) {
1301 dev_err_ratelimited(ne_misc_dev
.this_device
,
1302 "Incorrect flag in enclave start info\n");
1304 return -NE_ERR_INVALID_FLAG_VALUE
;
1308 * Do not use well-known CIDs - 0, 1, 2 - for enclaves.
1309 * VMADDR_CID_ANY = -1U
1310 * VMADDR_CID_HYPERVISOR = 0
1311 * VMADDR_CID_LOCAL = 1
1312 * VMADDR_CID_HOST = 2
1313 * Note: 0 is used as a placeholder to auto-generate an enclave CID.
1314 * http://man7.org/linux/man-pages/man7/vsock.7.html
1316 if (enclave_start_info
.enclave_cid
> 0 &&
1317 enclave_start_info
.enclave_cid
<= VMADDR_CID_HOST
) {
1318 dev_err_ratelimited(ne_misc_dev
.this_device
,
1319 "Well-known CID value, not to be used for enclaves\n");
1321 return -NE_ERR_INVALID_ENCLAVE_CID
;
1324 if (enclave_start_info
.enclave_cid
== U32_MAX
) {
1325 dev_err_ratelimited(ne_misc_dev
.this_device
,
1326 "Well-known CID value, not to be used for enclaves\n");
1328 return -NE_ERR_INVALID_ENCLAVE_CID
;
1332 * Do not use the CID of the primary / parent VM for enclaves.
1334 if (enclave_start_info
.enclave_cid
== NE_PARENT_VM_CID
) {
1335 dev_err_ratelimited(ne_misc_dev
.this_device
,
1336 "CID of the parent VM, not to be used for enclaves\n");
1338 return -NE_ERR_INVALID_ENCLAVE_CID
;
1341 /* 64-bit CIDs are not yet supported for the vsock device. */
1342 if (enclave_start_info
.enclave_cid
> U32_MAX
) {
1343 dev_err_ratelimited(ne_misc_dev
.this_device
,
1344 "64-bit CIDs not yet supported for the vsock device\n");
1346 return -NE_ERR_INVALID_ENCLAVE_CID
;
1349 mutex_lock(&ne_enclave
->enclave_info_mutex
);
1351 if (ne_enclave
->state
!= NE_STATE_INIT
) {
1352 dev_err_ratelimited(ne_misc_dev
.this_device
,
1353 "Enclave is not in init state\n");
1355 mutex_unlock(&ne_enclave
->enclave_info_mutex
);
1357 return -NE_ERR_NOT_IN_INIT_STATE
;
1360 rc
= ne_start_enclave_ioctl(ne_enclave
, &enclave_start_info
);
1362 mutex_unlock(&ne_enclave
->enclave_info_mutex
);
1367 mutex_unlock(&ne_enclave
->enclave_info_mutex
);
1369 if (copy_to_user((void __user
*)arg
, &enclave_start_info
,
1370 sizeof(enclave_start_info
)))
1384 * ne_enclave_remove_all_mem_region_entries() - Remove all memory region entries
1385 * from the enclave data structure.
1386 * @ne_enclave : Private data associated with the current enclave.
1388 * Context: Process context. This function is called with the ne_enclave mutex held.
1390 static void ne_enclave_remove_all_mem_region_entries(struct ne_enclave
*ne_enclave
)
1392 unsigned long i
= 0;
1393 struct ne_mem_region
*ne_mem_region
= NULL
;
1394 struct ne_mem_region
*ne_mem_region_tmp
= NULL
;
1396 list_for_each_entry_safe(ne_mem_region
, ne_mem_region_tmp
,
1397 &ne_enclave
->mem_regions_list
,
1398 mem_region_list_entry
) {
1399 list_del(&ne_mem_region
->mem_region_list_entry
);
1401 for (i
= 0; i
< ne_mem_region
->nr_pages
; i
++)
1402 put_page(ne_mem_region
->pages
[i
]);
1404 kfree(ne_mem_region
->pages
);
1406 kfree(ne_mem_region
);
1411 * ne_enclave_remove_all_vcpu_id_entries() - Remove all vCPU id entries from
1412 * the enclave data structure.
1413 * @ne_enclave : Private data associated with the current enclave.
1415 * Context: Process context. This function is called with the ne_enclave mutex held.
1417 static void ne_enclave_remove_all_vcpu_id_entries(struct ne_enclave
*ne_enclave
)
1419 unsigned int cpu
= 0;
1422 mutex_lock(&ne_cpu_pool
.mutex
);
1424 for (i
= 0; i
< ne_enclave
->nr_parent_vm_cores
; i
++) {
1425 for_each_cpu(cpu
, ne_enclave
->threads_per_core
[i
])
1426 /* Update the available NE CPU pool. */
1427 cpumask_set_cpu(cpu
, ne_cpu_pool
.avail_threads_per_core
[i
]);
1429 free_cpumask_var(ne_enclave
->threads_per_core
[i
]);
1432 mutex_unlock(&ne_cpu_pool
.mutex
);
1434 kfree(ne_enclave
->threads_per_core
);
1436 free_cpumask_var(ne_enclave
->vcpu_ids
);
1440 * ne_pci_dev_remove_enclave_entry() - Remove the enclave entry from the data
1441 * structure that is part of the NE PCI
1442 * device private data.
1443 * @ne_enclave : Private data associated with the current enclave.
1444 * @ne_pci_dev : Private data associated with the PCI device.
1446 * Context: Process context. This function is called with the ne_pci_dev enclave
1449 static void ne_pci_dev_remove_enclave_entry(struct ne_enclave
*ne_enclave
,
1450 struct ne_pci_dev
*ne_pci_dev
)
1452 struct ne_enclave
*ne_enclave_entry
= NULL
;
1453 struct ne_enclave
*ne_enclave_entry_tmp
= NULL
;
1455 list_for_each_entry_safe(ne_enclave_entry
, ne_enclave_entry_tmp
,
1456 &ne_pci_dev
->enclaves_list
, enclave_list_entry
) {
1457 if (ne_enclave_entry
->slot_uid
== ne_enclave
->slot_uid
) {
1458 list_del(&ne_enclave_entry
->enclave_list_entry
);
1466 * ne_enclave_release() - Release function provided by the enclave file.
1467 * @inode: Inode associated with this file release function.
1468 * @file: File associated with this release function.
1470 * Context: Process context.
1473 * * Negative return value on failure.
1475 static int ne_enclave_release(struct inode
*inode
, struct file
*file
)
1477 struct ne_pci_dev_cmd_reply cmd_reply
= {};
1478 struct enclave_stop_req enclave_stop_request
= {};
1479 struct ne_enclave
*ne_enclave
= file
->private_data
;
1480 struct ne_pci_dev
*ne_pci_dev
= ne_devs
.ne_pci_dev
;
1481 struct pci_dev
*pdev
= ne_pci_dev
->pdev
;
1483 struct slot_free_req slot_free_req
= {};
1489 * Early exit in case there is an error in the enclave creation logic
1490 * and fput() is called on the cleanup path.
1492 if (!ne_enclave
->slot_uid
)
1496 * Acquire the enclave list mutex before the enclave mutex
1497 * in order to avoid deadlocks with @ref ne_event_work_handler.
1499 mutex_lock(&ne_pci_dev
->enclaves_list_mutex
);
1500 mutex_lock(&ne_enclave
->enclave_info_mutex
);
1502 if (ne_enclave
->state
!= NE_STATE_INIT
&& ne_enclave
->state
!= NE_STATE_STOPPED
) {
1503 enclave_stop_request
.slot_uid
= ne_enclave
->slot_uid
;
1505 rc
= ne_do_request(pdev
, ENCLAVE_STOP
,
1506 &enclave_stop_request
, sizeof(enclave_stop_request
),
1507 &cmd_reply
, sizeof(cmd_reply
));
1509 dev_err_ratelimited(ne_misc_dev
.this_device
,
1510 "Error in enclave stop [rc=%d]\n", rc
);
1515 memset(&cmd_reply
, 0, sizeof(cmd_reply
));
1518 slot_free_req
.slot_uid
= ne_enclave
->slot_uid
;
1520 rc
= ne_do_request(pdev
, SLOT_FREE
,
1521 &slot_free_req
, sizeof(slot_free_req
),
1522 &cmd_reply
, sizeof(cmd_reply
));
1524 dev_err_ratelimited(ne_misc_dev
.this_device
,
1525 "Error in slot free [rc=%d]\n", rc
);
1530 ne_pci_dev_remove_enclave_entry(ne_enclave
, ne_pci_dev
);
1531 ne_enclave_remove_all_mem_region_entries(ne_enclave
);
1532 ne_enclave_remove_all_vcpu_id_entries(ne_enclave
);
1534 mutex_unlock(&ne_enclave
->enclave_info_mutex
);
1535 mutex_unlock(&ne_pci_dev
->enclaves_list_mutex
);
1542 mutex_unlock(&ne_enclave
->enclave_info_mutex
);
1543 mutex_unlock(&ne_pci_dev
->enclaves_list_mutex
);
1549 * ne_enclave_poll() - Poll functionality used for enclave out-of-band events.
1550 * @file: File associated with this poll function.
1551 * @wait: Poll table data structure.
1553 * Context: Process context.
1557 static __poll_t
ne_enclave_poll(struct file
*file
, poll_table
*wait
)
1560 struct ne_enclave
*ne_enclave
= file
->private_data
;
1562 poll_wait(file
, &ne_enclave
->eventq
, wait
);
1564 if (ne_enclave
->has_event
)
1570 static const struct file_operations ne_enclave_fops
= {
1571 .owner
= THIS_MODULE
,
1572 .llseek
= noop_llseek
,
1573 .poll
= ne_enclave_poll
,
1574 .unlocked_ioctl
= ne_enclave_ioctl
,
1575 .release
= ne_enclave_release
,
1579 * ne_create_vm_ioctl() - Alloc slot to be associated with an enclave. Create
1580 * enclave file descriptor to be further used for enclave
1581 * resources handling e.g. memory regions and CPUs.
1582 * @ne_pci_dev : Private data associated with the PCI device.
1583 * @slot_uid: User pointer to store the generated unique slot id
1584 * associated with an enclave to.
1586 * Context: Process context. This function is called with the ne_pci_dev enclave
1589 * * Enclave fd on success.
1590 * * Negative return value on failure.
1592 static int ne_create_vm_ioctl(struct ne_pci_dev
*ne_pci_dev
, u64 __user
*slot_uid
)
1594 struct ne_pci_dev_cmd_reply cmd_reply
= {};
1595 int enclave_fd
= -1;
1596 struct file
*enclave_file
= NULL
;
1598 struct ne_enclave
*ne_enclave
= NULL
;
1599 struct pci_dev
*pdev
= ne_pci_dev
->pdev
;
1601 struct slot_alloc_req slot_alloc_req
= {};
1603 mutex_lock(&ne_cpu_pool
.mutex
);
1605 for (i
= 0; i
< ne_cpu_pool
.nr_parent_vm_cores
; i
++)
1606 if (!cpumask_empty(ne_cpu_pool
.avail_threads_per_core
[i
]))
1609 if (i
== ne_cpu_pool
.nr_parent_vm_cores
) {
1610 dev_err_ratelimited(ne_misc_dev
.this_device
,
1611 "No CPUs available in CPU pool\n");
1613 mutex_unlock(&ne_cpu_pool
.mutex
);
1615 return -NE_ERR_NO_CPUS_AVAIL_IN_POOL
;
1618 mutex_unlock(&ne_cpu_pool
.mutex
);
1620 ne_enclave
= kzalloc(sizeof(*ne_enclave
), GFP_KERNEL
);
1624 mutex_lock(&ne_cpu_pool
.mutex
);
1626 ne_enclave
->nr_parent_vm_cores
= ne_cpu_pool
.nr_parent_vm_cores
;
1627 ne_enclave
->nr_threads_per_core
= ne_cpu_pool
.nr_threads_per_core
;
1628 ne_enclave
->numa_node
= ne_cpu_pool
.numa_node
;
1630 mutex_unlock(&ne_cpu_pool
.mutex
);
1632 ne_enclave
->threads_per_core
= kcalloc(ne_enclave
->nr_parent_vm_cores
,
1633 sizeof(*ne_enclave
->threads_per_core
),
1635 if (!ne_enclave
->threads_per_core
) {
1638 goto free_ne_enclave
;
1641 for (i
= 0; i
< ne_enclave
->nr_parent_vm_cores
; i
++)
1642 if (!zalloc_cpumask_var(&ne_enclave
->threads_per_core
[i
], GFP_KERNEL
)) {
1648 if (!zalloc_cpumask_var(&ne_enclave
->vcpu_ids
, GFP_KERNEL
)) {
1654 enclave_fd
= get_unused_fd_flags(O_CLOEXEC
);
1655 if (enclave_fd
< 0) {
1658 dev_err_ratelimited(ne_misc_dev
.this_device
,
1659 "Error in getting unused fd [rc=%d]\n", rc
);
1664 enclave_file
= anon_inode_getfile("ne-vm", &ne_enclave_fops
, ne_enclave
, O_RDWR
);
1665 if (IS_ERR(enclave_file
)) {
1666 rc
= PTR_ERR(enclave_file
);
1668 dev_err_ratelimited(ne_misc_dev
.this_device
,
1669 "Error in anon inode get file [rc=%d]\n", rc
);
1674 rc
= ne_do_request(pdev
, SLOT_ALLOC
,
1675 &slot_alloc_req
, sizeof(slot_alloc_req
),
1676 &cmd_reply
, sizeof(cmd_reply
));
1678 dev_err_ratelimited(ne_misc_dev
.this_device
,
1679 "Error in slot alloc [rc=%d]\n", rc
);
1684 init_waitqueue_head(&ne_enclave
->eventq
);
1685 ne_enclave
->has_event
= false;
1686 mutex_init(&ne_enclave
->enclave_info_mutex
);
1687 ne_enclave
->max_mem_regions
= cmd_reply
.mem_regions
;
1688 INIT_LIST_HEAD(&ne_enclave
->mem_regions_list
);
1689 ne_enclave
->mm
= current
->mm
;
1690 ne_enclave
->slot_uid
= cmd_reply
.slot_uid
;
1691 ne_enclave
->state
= NE_STATE_INIT
;
1693 list_add(&ne_enclave
->enclave_list_entry
, &ne_pci_dev
->enclaves_list
);
1695 if (copy_to_user(slot_uid
, &ne_enclave
->slot_uid
, sizeof(ne_enclave
->slot_uid
))) {
1697 * As we're holding the only reference to 'enclave_file', fput()
1698 * will call ne_enclave_release() which will do a proper cleanup
1699 * of all so far allocated resources, leaving only the unused fd
1703 put_unused_fd(enclave_fd
);
1708 fd_install(enclave_fd
, enclave_file
);
1715 put_unused_fd(enclave_fd
);
1717 free_cpumask_var(ne_enclave
->vcpu_ids
);
1718 for (i
= 0; i
< ne_enclave
->nr_parent_vm_cores
; i
++)
1719 free_cpumask_var(ne_enclave
->threads_per_core
[i
]);
1720 kfree(ne_enclave
->threads_per_core
);
1728 * ne_ioctl() - Ioctl function provided by the NE misc device.
1729 * @file: File associated with this ioctl function.
1730 * @cmd: The command that is set for the ioctl call.
1731 * @arg: The argument that is provided for the ioctl call.
1733 * Context: Process context.
1735 * * Ioctl result (e.g. enclave file descriptor) on success.
1736 * * Negative return value on failure.
1738 static long ne_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1741 case NE_CREATE_VM
: {
1742 int enclave_fd
= -1;
1743 struct ne_pci_dev
*ne_pci_dev
= ne_devs
.ne_pci_dev
;
1744 u64 __user
*slot_uid
= (void __user
*)arg
;
1746 mutex_lock(&ne_pci_dev
->enclaves_list_mutex
);
1747 enclave_fd
= ne_create_vm_ioctl(ne_pci_dev
, slot_uid
);
1748 mutex_unlock(&ne_pci_dev
->enclaves_list_mutex
);
1760 #if defined(CONFIG_NITRO_ENCLAVES_MISC_DEV_TEST)
1761 #include "ne_misc_dev_test.c"
1764 static int __init
ne_init(void)
1766 mutex_init(&ne_cpu_pool
.mutex
);
1768 return pci_register_driver(&ne_pci_driver
);
1771 static void __exit
ne_exit(void)
1773 pci_unregister_driver(&ne_pci_driver
);
1775 ne_teardown_cpu_pool();
1778 module_init(ne_init
);
1779 module_exit(ne_exit
);
1781 MODULE_AUTHOR("Amazon.com, Inc. or its affiliates");
1782 MODULE_DESCRIPTION("Nitro Enclaves Driver");
1783 MODULE_LICENSE("GPL v2");