1bd47c995SAndra Paraschiv // SPDX-License-Identifier: GPL-2.0
2bd47c995SAndra Paraschiv /*
302bba596SAndra Paraschiv * Copyright 2020-2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4bd47c995SAndra Paraschiv */
5bd47c995SAndra Paraschiv
6bd47c995SAndra Paraschiv /**
7bd47c995SAndra Paraschiv * DOC: Enclave lifetime management driver for Nitro Enclaves (NE).
8bd47c995SAndra Paraschiv * Nitro is a hypervisor that has been developed by Amazon.
9bd47c995SAndra Paraschiv */
10bd47c995SAndra Paraschiv
11bd47c995SAndra Paraschiv #include <linux/anon_inodes.h>
12bd47c995SAndra Paraschiv #include <linux/capability.h>
13bd47c995SAndra Paraschiv #include <linux/cpu.h>
14bd47c995SAndra Paraschiv #include <linux/device.h>
15bd47c995SAndra Paraschiv #include <linux/file.h>
16bd47c995SAndra Paraschiv #include <linux/hugetlb.h>
17bd47c995SAndra Paraschiv #include <linux/limits.h>
18bd47c995SAndra Paraschiv #include <linux/list.h>
19bd47c995SAndra Paraschiv #include <linux/miscdevice.h>
20bd47c995SAndra Paraschiv #include <linux/mm.h>
21bd47c995SAndra Paraschiv #include <linux/mman.h>
22bd47c995SAndra Paraschiv #include <linux/module.h>
23bd47c995SAndra Paraschiv #include <linux/mutex.h>
24bd47c995SAndra Paraschiv #include <linux/nitro_enclaves.h>
25bd47c995SAndra Paraschiv #include <linux/pci.h>
26bd47c995SAndra Paraschiv #include <linux/poll.h>
27f6bdc0aaSLongpeng #include <linux/range.h>
28bd47c995SAndra Paraschiv #include <linux/slab.h>
29bd47c995SAndra Paraschiv #include <linux/types.h>
30bd47c995SAndra Paraschiv #include <uapi/linux/vm_sockets.h>
31bd47c995SAndra Paraschiv
32bd47c995SAndra Paraschiv #include "ne_misc_dev.h"
33bd47c995SAndra Paraschiv #include "ne_pci_dev.h"
34bd47c995SAndra Paraschiv
35bd47c995SAndra Paraschiv /**
36bd47c995SAndra Paraschiv * NE_CPUS_SIZE - Size for max 128 CPUs, for now, in a cpu-list string, comma
37bd47c995SAndra Paraschiv * separated. The NE CPU pool includes CPUs from a single NUMA
38bd47c995SAndra Paraschiv * node.
39bd47c995SAndra Paraschiv */
40bd47c995SAndra Paraschiv #define NE_CPUS_SIZE (512)
41bd47c995SAndra Paraschiv
42bd47c995SAndra Paraschiv /**
43bd47c995SAndra Paraschiv * NE_EIF_LOAD_OFFSET - The offset where to copy the Enclave Image Format (EIF)
44bd47c995SAndra Paraschiv * image in enclave memory.
45bd47c995SAndra Paraschiv */
46bd47c995SAndra Paraschiv #define NE_EIF_LOAD_OFFSET (8 * 1024UL * 1024UL)
47bd47c995SAndra Paraschiv
48bd47c995SAndra Paraschiv /**
49bd47c995SAndra Paraschiv * NE_MIN_ENCLAVE_MEM_SIZE - The minimum memory size an enclave can be launched
50bd47c995SAndra Paraschiv * with.
51bd47c995SAndra Paraschiv */
52bd47c995SAndra Paraschiv #define NE_MIN_ENCLAVE_MEM_SIZE (64 * 1024UL * 1024UL)
53bd47c995SAndra Paraschiv
54bd47c995SAndra Paraschiv /**
55bd47c995SAndra Paraschiv * NE_MIN_MEM_REGION_SIZE - The minimum size of an enclave memory region.
56bd47c995SAndra Paraschiv */
57bd47c995SAndra Paraschiv #define NE_MIN_MEM_REGION_SIZE (2 * 1024UL * 1024UL)
58bd47c995SAndra Paraschiv
59bd47c995SAndra Paraschiv /**
60bd47c995SAndra Paraschiv * NE_PARENT_VM_CID - The CID for the vsock device of the primary / parent VM.
61bd47c995SAndra Paraschiv */
62bd47c995SAndra Paraschiv #define NE_PARENT_VM_CID (3)
63bd47c995SAndra Paraschiv
6438907e12SAndra Paraschiv static long ne_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
6538907e12SAndra Paraschiv
66bd47c995SAndra Paraschiv static const struct file_operations ne_fops = {
67bd47c995SAndra Paraschiv .owner = THIS_MODULE,
68bd47c995SAndra Paraschiv .llseek = noop_llseek,
6938907e12SAndra Paraschiv .unlocked_ioctl = ne_ioctl,
70bd47c995SAndra Paraschiv };
71bd47c995SAndra Paraschiv
72bd47c995SAndra Paraschiv static struct miscdevice ne_misc_dev = {
73bd47c995SAndra Paraschiv .minor = MISC_DYNAMIC_MINOR,
74bd47c995SAndra Paraschiv .name = "nitro_enclaves",
75bd47c995SAndra Paraschiv .fops = &ne_fops,
76bd47c995SAndra Paraschiv .mode = 0660,
77bd47c995SAndra Paraschiv };
78bd47c995SAndra Paraschiv
79bd47c995SAndra Paraschiv struct ne_devs ne_devs = {
80bd47c995SAndra Paraschiv .ne_misc_dev = &ne_misc_dev,
81bd47c995SAndra Paraschiv };
82bd47c995SAndra Paraschiv
83bd47c995SAndra Paraschiv /*
84bd47c995SAndra Paraschiv * TODO: Update logic to create new sysfs entries instead of using
85bd47c995SAndra Paraschiv * a kernel parameter e.g. if multiple sysfs files needed.
86bd47c995SAndra Paraschiv */
87ff8a4d3eSAndra Paraschiv static int ne_set_kernel_param(const char *val, const struct kernel_param *kp);
88ff8a4d3eSAndra Paraschiv
89bd47c995SAndra Paraschiv static const struct kernel_param_ops ne_cpu_pool_ops = {
90bd47c995SAndra Paraschiv .get = param_get_string,
91ff8a4d3eSAndra Paraschiv .set = ne_set_kernel_param,
92bd47c995SAndra Paraschiv };
93bd47c995SAndra Paraschiv
94bd47c995SAndra Paraschiv static char ne_cpus[NE_CPUS_SIZE];
95bd47c995SAndra Paraschiv static struct kparam_string ne_cpus_arg = {
96bd47c995SAndra Paraschiv .maxlen = sizeof(ne_cpus),
97bd47c995SAndra Paraschiv .string = ne_cpus,
98bd47c995SAndra Paraschiv };
99bd47c995SAndra Paraschiv
100bd47c995SAndra Paraschiv module_param_cb(ne_cpus, &ne_cpu_pool_ops, &ne_cpus_arg, 0644);
101bd47c995SAndra Paraschiv /* https://www.kernel.org/doc/html/latest/admin-guide/kernel-parameters.html#cpu-lists */
102bd47c995SAndra Paraschiv MODULE_PARM_DESC(ne_cpus, "<cpu-list> - CPU pool used for Nitro Enclaves");
103bd47c995SAndra Paraschiv
104bd47c995SAndra Paraschiv /**
105bd47c995SAndra Paraschiv * struct ne_cpu_pool - CPU pool used for Nitro Enclaves.
106bd47c995SAndra Paraschiv * @avail_threads_per_core: Available full CPU cores to be dedicated to
107bd47c995SAndra Paraschiv * enclave(s). The cpumasks from the array, indexed
108bd47c995SAndra Paraschiv * by core id, contain all the threads from the
109bd47c995SAndra Paraschiv * available cores, that are not set for created
110bd47c995SAndra Paraschiv * enclave(s). The full CPU cores are part of the
111bd47c995SAndra Paraschiv * NE CPU pool.
112bd47c995SAndra Paraschiv * @mutex: Mutex for the access to the NE CPU pool.
113bd47c995SAndra Paraschiv * @nr_parent_vm_cores : The size of the available threads per core array.
114bd47c995SAndra Paraschiv * The total number of CPU cores available on the
115bd47c995SAndra Paraschiv * primary / parent VM.
116bd47c995SAndra Paraschiv * @nr_threads_per_core: The number of threads that a full CPU core has.
117bd47c995SAndra Paraschiv * @numa_node: NUMA node of the CPUs in the pool.
118bd47c995SAndra Paraschiv */
119bd47c995SAndra Paraschiv struct ne_cpu_pool {
120bd47c995SAndra Paraschiv cpumask_var_t *avail_threads_per_core;
121bd47c995SAndra Paraschiv struct mutex mutex;
122bd47c995SAndra Paraschiv unsigned int nr_parent_vm_cores;
123bd47c995SAndra Paraschiv unsigned int nr_threads_per_core;
124bd47c995SAndra Paraschiv int numa_node;
125bd47c995SAndra Paraschiv };
126bd47c995SAndra Paraschiv
127bd47c995SAndra Paraschiv static struct ne_cpu_pool ne_cpu_pool;
128bd47c995SAndra Paraschiv
12938907e12SAndra Paraschiv /**
130f6bdc0aaSLongpeng * struct ne_phys_contig_mem_regions - Contiguous physical memory regions.
131f6bdc0aaSLongpeng * @num: The number of regions that currently has.
132f6bdc0aaSLongpeng * @regions: The array of physical memory regions.
133f6bdc0aaSLongpeng */
134f6bdc0aaSLongpeng struct ne_phys_contig_mem_regions {
135f6bdc0aaSLongpeng unsigned long num;
136f6bdc0aaSLongpeng struct range *regions;
137f6bdc0aaSLongpeng };
138f6bdc0aaSLongpeng
139f6bdc0aaSLongpeng /**
140ff8a4d3eSAndra Paraschiv * ne_check_enclaves_created() - Verify if at least one enclave has been created.
141ff8a4d3eSAndra Paraschiv * @void: No parameters provided.
142ff8a4d3eSAndra Paraschiv *
143ff8a4d3eSAndra Paraschiv * Context: Process context.
144ff8a4d3eSAndra Paraschiv * Return:
145ff8a4d3eSAndra Paraschiv * * True if at least one enclave is created.
146ff8a4d3eSAndra Paraschiv * * False otherwise.
147ff8a4d3eSAndra Paraschiv */
ne_check_enclaves_created(void)148ff8a4d3eSAndra Paraschiv static bool ne_check_enclaves_created(void)
149ff8a4d3eSAndra Paraschiv {
150ff8a4d3eSAndra Paraschiv struct ne_pci_dev *ne_pci_dev = ne_devs.ne_pci_dev;
151ff8a4d3eSAndra Paraschiv bool ret = false;
152ff8a4d3eSAndra Paraschiv
153ff8a4d3eSAndra Paraschiv if (!ne_pci_dev)
154ff8a4d3eSAndra Paraschiv return ret;
155ff8a4d3eSAndra Paraschiv
156ff8a4d3eSAndra Paraschiv mutex_lock(&ne_pci_dev->enclaves_list_mutex);
157ff8a4d3eSAndra Paraschiv
158ff8a4d3eSAndra Paraschiv if (!list_empty(&ne_pci_dev->enclaves_list))
159ff8a4d3eSAndra Paraschiv ret = true;
160ff8a4d3eSAndra Paraschiv
161ff8a4d3eSAndra Paraschiv mutex_unlock(&ne_pci_dev->enclaves_list_mutex);
162ff8a4d3eSAndra Paraschiv
163ff8a4d3eSAndra Paraschiv return ret;
164ff8a4d3eSAndra Paraschiv }
165ff8a4d3eSAndra Paraschiv
166ff8a4d3eSAndra Paraschiv /**
167ff8a4d3eSAndra Paraschiv * ne_setup_cpu_pool() - Set the NE CPU pool after handling sanity checks such
168ff8a4d3eSAndra Paraschiv * as not sharing CPU cores with the primary / parent VM
169ff8a4d3eSAndra Paraschiv * or not using CPU 0, which should remain available for
170ff8a4d3eSAndra Paraschiv * the primary / parent VM. Offline the CPUs from the
171ff8a4d3eSAndra Paraschiv * pool after the checks passed.
172ff8a4d3eSAndra Paraschiv * @ne_cpu_list: The CPU list used for setting NE CPU pool.
173ff8a4d3eSAndra Paraschiv *
174ff8a4d3eSAndra Paraschiv * Context: Process context.
175ff8a4d3eSAndra Paraschiv * Return:
176ff8a4d3eSAndra Paraschiv * * 0 on success.
177ff8a4d3eSAndra Paraschiv * * Negative return value on failure.
178ff8a4d3eSAndra Paraschiv */
ne_setup_cpu_pool(const char * ne_cpu_list)179ff8a4d3eSAndra Paraschiv static int ne_setup_cpu_pool(const char *ne_cpu_list)
180ff8a4d3eSAndra Paraschiv {
181ff8a4d3eSAndra Paraschiv int core_id = -1;
182ff8a4d3eSAndra Paraschiv unsigned int cpu = 0;
183ff8a4d3eSAndra Paraschiv cpumask_var_t cpu_pool;
184ff8a4d3eSAndra Paraschiv unsigned int cpu_sibling = 0;
185ff8a4d3eSAndra Paraschiv unsigned int i = 0;
186ff8a4d3eSAndra Paraschiv int numa_node = -1;
187ff8a4d3eSAndra Paraschiv int rc = -EINVAL;
188ff8a4d3eSAndra Paraschiv
189ff8a4d3eSAndra Paraschiv if (!zalloc_cpumask_var(&cpu_pool, GFP_KERNEL))
190ff8a4d3eSAndra Paraschiv return -ENOMEM;
191ff8a4d3eSAndra Paraschiv
192ff8a4d3eSAndra Paraschiv mutex_lock(&ne_cpu_pool.mutex);
193ff8a4d3eSAndra Paraschiv
194ff8a4d3eSAndra Paraschiv rc = cpulist_parse(ne_cpu_list, cpu_pool);
195ff8a4d3eSAndra Paraschiv if (rc < 0) {
196ff8a4d3eSAndra Paraschiv pr_err("%s: Error in cpulist parse [rc=%d]\n", ne_misc_dev.name, rc);
197ff8a4d3eSAndra Paraschiv
198ff8a4d3eSAndra Paraschiv goto free_pool_cpumask;
199ff8a4d3eSAndra Paraschiv }
200ff8a4d3eSAndra Paraschiv
201ff8a4d3eSAndra Paraschiv cpu = cpumask_any(cpu_pool);
202ff8a4d3eSAndra Paraschiv if (cpu >= nr_cpu_ids) {
203ff8a4d3eSAndra Paraschiv pr_err("%s: No CPUs available in CPU pool\n", ne_misc_dev.name);
204ff8a4d3eSAndra Paraschiv
205ff8a4d3eSAndra Paraschiv rc = -EINVAL;
206ff8a4d3eSAndra Paraschiv
207ff8a4d3eSAndra Paraschiv goto free_pool_cpumask;
208ff8a4d3eSAndra Paraschiv }
209ff8a4d3eSAndra Paraschiv
210ff8a4d3eSAndra Paraschiv /*
211ff8a4d3eSAndra Paraschiv * Check if the CPUs are online, to further get info about them
212ff8a4d3eSAndra Paraschiv * e.g. numa node, core id, siblings.
213ff8a4d3eSAndra Paraschiv */
214ff8a4d3eSAndra Paraschiv for_each_cpu(cpu, cpu_pool)
215ff8a4d3eSAndra Paraschiv if (cpu_is_offline(cpu)) {
216ff8a4d3eSAndra Paraschiv pr_err("%s: CPU %d is offline, has to be online to get its metadata\n",
217ff8a4d3eSAndra Paraschiv ne_misc_dev.name, cpu);
218ff8a4d3eSAndra Paraschiv
219ff8a4d3eSAndra Paraschiv rc = -EINVAL;
220ff8a4d3eSAndra Paraschiv
221ff8a4d3eSAndra Paraschiv goto free_pool_cpumask;
222ff8a4d3eSAndra Paraschiv }
223ff8a4d3eSAndra Paraschiv
224ff8a4d3eSAndra Paraschiv /*
225ff8a4d3eSAndra Paraschiv * Check if the CPUs from the NE CPU pool are from the same NUMA node.
226ff8a4d3eSAndra Paraschiv */
227ff8a4d3eSAndra Paraschiv for_each_cpu(cpu, cpu_pool)
228ff8a4d3eSAndra Paraschiv if (numa_node < 0) {
229ff8a4d3eSAndra Paraschiv numa_node = cpu_to_node(cpu);
230ff8a4d3eSAndra Paraschiv if (numa_node < 0) {
231ff8a4d3eSAndra Paraschiv pr_err("%s: Invalid NUMA node %d\n",
232ff8a4d3eSAndra Paraschiv ne_misc_dev.name, numa_node);
233ff8a4d3eSAndra Paraschiv
234ff8a4d3eSAndra Paraschiv rc = -EINVAL;
235ff8a4d3eSAndra Paraschiv
236ff8a4d3eSAndra Paraschiv goto free_pool_cpumask;
237ff8a4d3eSAndra Paraschiv }
238ff8a4d3eSAndra Paraschiv } else {
239ff8a4d3eSAndra Paraschiv if (numa_node != cpu_to_node(cpu)) {
240ff8a4d3eSAndra Paraschiv pr_err("%s: CPUs with different NUMA nodes\n",
241ff8a4d3eSAndra Paraschiv ne_misc_dev.name);
242ff8a4d3eSAndra Paraschiv
243ff8a4d3eSAndra Paraschiv rc = -EINVAL;
244ff8a4d3eSAndra Paraschiv
245ff8a4d3eSAndra Paraschiv goto free_pool_cpumask;
246ff8a4d3eSAndra Paraschiv }
247ff8a4d3eSAndra Paraschiv }
248ff8a4d3eSAndra Paraschiv
249ff8a4d3eSAndra Paraschiv /*
250ff8a4d3eSAndra Paraschiv * Check if CPU 0 and its siblings are included in the provided CPU pool
251ff8a4d3eSAndra Paraschiv * They should remain available for the primary / parent VM.
252ff8a4d3eSAndra Paraschiv */
253ff8a4d3eSAndra Paraschiv if (cpumask_test_cpu(0, cpu_pool)) {
254ff8a4d3eSAndra Paraschiv pr_err("%s: CPU 0 has to remain available\n", ne_misc_dev.name);
255ff8a4d3eSAndra Paraschiv
256ff8a4d3eSAndra Paraschiv rc = -EINVAL;
257ff8a4d3eSAndra Paraschiv
258ff8a4d3eSAndra Paraschiv goto free_pool_cpumask;
259ff8a4d3eSAndra Paraschiv }
260ff8a4d3eSAndra Paraschiv
261ff8a4d3eSAndra Paraschiv for_each_cpu(cpu_sibling, topology_sibling_cpumask(0)) {
262ff8a4d3eSAndra Paraschiv if (cpumask_test_cpu(cpu_sibling, cpu_pool)) {
263ff8a4d3eSAndra Paraschiv pr_err("%s: CPU sibling %d for CPU 0 is in CPU pool\n",
264ff8a4d3eSAndra Paraschiv ne_misc_dev.name, cpu_sibling);
265ff8a4d3eSAndra Paraschiv
266ff8a4d3eSAndra Paraschiv rc = -EINVAL;
267ff8a4d3eSAndra Paraschiv
268ff8a4d3eSAndra Paraschiv goto free_pool_cpumask;
269ff8a4d3eSAndra Paraschiv }
270ff8a4d3eSAndra Paraschiv }
271ff8a4d3eSAndra Paraschiv
272ff8a4d3eSAndra Paraschiv /*
273ff8a4d3eSAndra Paraschiv * Check if CPU siblings are included in the provided CPU pool. The
274ff8a4d3eSAndra Paraschiv * expectation is that full CPU cores are made available in the CPU pool
275ff8a4d3eSAndra Paraschiv * for enclaves.
276ff8a4d3eSAndra Paraschiv */
277ff8a4d3eSAndra Paraschiv for_each_cpu(cpu, cpu_pool) {
278ff8a4d3eSAndra Paraschiv for_each_cpu(cpu_sibling, topology_sibling_cpumask(cpu)) {
279ff8a4d3eSAndra Paraschiv if (!cpumask_test_cpu(cpu_sibling, cpu_pool)) {
280ff8a4d3eSAndra Paraschiv pr_err("%s: CPU %d is not in CPU pool\n",
281ff8a4d3eSAndra Paraschiv ne_misc_dev.name, cpu_sibling);
282ff8a4d3eSAndra Paraschiv
283ff8a4d3eSAndra Paraschiv rc = -EINVAL;
284ff8a4d3eSAndra Paraschiv
285ff8a4d3eSAndra Paraschiv goto free_pool_cpumask;
286ff8a4d3eSAndra Paraschiv }
287ff8a4d3eSAndra Paraschiv }
288ff8a4d3eSAndra Paraschiv }
289ff8a4d3eSAndra Paraschiv
290ff8a4d3eSAndra Paraschiv /* Calculate the number of threads from a full CPU core. */
291ff8a4d3eSAndra Paraschiv cpu = cpumask_any(cpu_pool);
292ff8a4d3eSAndra Paraschiv for_each_cpu(cpu_sibling, topology_sibling_cpumask(cpu))
293ff8a4d3eSAndra Paraschiv ne_cpu_pool.nr_threads_per_core++;
294ff8a4d3eSAndra Paraschiv
295ff8a4d3eSAndra Paraschiv ne_cpu_pool.nr_parent_vm_cores = nr_cpu_ids / ne_cpu_pool.nr_threads_per_core;
296ff8a4d3eSAndra Paraschiv
297ff8a4d3eSAndra Paraschiv ne_cpu_pool.avail_threads_per_core = kcalloc(ne_cpu_pool.nr_parent_vm_cores,
298ff8a4d3eSAndra Paraschiv sizeof(*ne_cpu_pool.avail_threads_per_core),
299ff8a4d3eSAndra Paraschiv GFP_KERNEL);
300ff8a4d3eSAndra Paraschiv if (!ne_cpu_pool.avail_threads_per_core) {
301ff8a4d3eSAndra Paraschiv rc = -ENOMEM;
302ff8a4d3eSAndra Paraschiv
303ff8a4d3eSAndra Paraschiv goto free_pool_cpumask;
304ff8a4d3eSAndra Paraschiv }
305ff8a4d3eSAndra Paraschiv
306ff8a4d3eSAndra Paraschiv for (i = 0; i < ne_cpu_pool.nr_parent_vm_cores; i++)
307ff8a4d3eSAndra Paraschiv if (!zalloc_cpumask_var(&ne_cpu_pool.avail_threads_per_core[i], GFP_KERNEL)) {
308ff8a4d3eSAndra Paraschiv rc = -ENOMEM;
309ff8a4d3eSAndra Paraschiv
310ff8a4d3eSAndra Paraschiv goto free_cores_cpumask;
311ff8a4d3eSAndra Paraschiv }
312ff8a4d3eSAndra Paraschiv
313ff8a4d3eSAndra Paraschiv /*
314ff8a4d3eSAndra Paraschiv * Split the NE CPU pool in threads per core to keep the CPU topology
315ff8a4d3eSAndra Paraschiv * after offlining the CPUs.
316ff8a4d3eSAndra Paraschiv */
317ff8a4d3eSAndra Paraschiv for_each_cpu(cpu, cpu_pool) {
318ff8a4d3eSAndra Paraschiv core_id = topology_core_id(cpu);
319ff8a4d3eSAndra Paraschiv if (core_id < 0 || core_id >= ne_cpu_pool.nr_parent_vm_cores) {
320ff8a4d3eSAndra Paraschiv pr_err("%s: Invalid core id %d for CPU %d\n",
321ff8a4d3eSAndra Paraschiv ne_misc_dev.name, core_id, cpu);
322ff8a4d3eSAndra Paraschiv
323ff8a4d3eSAndra Paraschiv rc = -EINVAL;
324ff8a4d3eSAndra Paraschiv
325ff8a4d3eSAndra Paraschiv goto clear_cpumask;
326ff8a4d3eSAndra Paraschiv }
327ff8a4d3eSAndra Paraschiv
328ff8a4d3eSAndra Paraschiv cpumask_set_cpu(cpu, ne_cpu_pool.avail_threads_per_core[core_id]);
329ff8a4d3eSAndra Paraschiv }
330ff8a4d3eSAndra Paraschiv
331ff8a4d3eSAndra Paraschiv /*
332ff8a4d3eSAndra Paraschiv * CPUs that are given to enclave(s) should not be considered online
333ff8a4d3eSAndra Paraschiv * by Linux anymore, as the hypervisor will degrade them to floating.
334ff8a4d3eSAndra Paraschiv * The physical CPUs (full cores) are carved out of the primary / parent
335ff8a4d3eSAndra Paraschiv * VM and given to the enclave VM. The same number of vCPUs would run
336ff8a4d3eSAndra Paraschiv * on less pCPUs for the primary / parent VM.
337ff8a4d3eSAndra Paraschiv *
338ff8a4d3eSAndra Paraschiv * We offline them here, to not degrade performance and expose correct
339ff8a4d3eSAndra Paraschiv * topology to Linux and user space.
340ff8a4d3eSAndra Paraschiv */
341ff8a4d3eSAndra Paraschiv for_each_cpu(cpu, cpu_pool) {
342ff8a4d3eSAndra Paraschiv rc = remove_cpu(cpu);
343ff8a4d3eSAndra Paraschiv if (rc != 0) {
344ff8a4d3eSAndra Paraschiv pr_err("%s: CPU %d is not offlined [rc=%d]\n",
345ff8a4d3eSAndra Paraschiv ne_misc_dev.name, cpu, rc);
346ff8a4d3eSAndra Paraschiv
347ff8a4d3eSAndra Paraschiv goto online_cpus;
348ff8a4d3eSAndra Paraschiv }
349ff8a4d3eSAndra Paraschiv }
350ff8a4d3eSAndra Paraschiv
351ff8a4d3eSAndra Paraschiv free_cpumask_var(cpu_pool);
352ff8a4d3eSAndra Paraschiv
353ff8a4d3eSAndra Paraschiv ne_cpu_pool.numa_node = numa_node;
354ff8a4d3eSAndra Paraschiv
355ff8a4d3eSAndra Paraschiv mutex_unlock(&ne_cpu_pool.mutex);
356ff8a4d3eSAndra Paraschiv
357ff8a4d3eSAndra Paraschiv return 0;
358ff8a4d3eSAndra Paraschiv
359ff8a4d3eSAndra Paraschiv online_cpus:
360ff8a4d3eSAndra Paraschiv for_each_cpu(cpu, cpu_pool)
361ff8a4d3eSAndra Paraschiv add_cpu(cpu);
362ff8a4d3eSAndra Paraschiv clear_cpumask:
363ff8a4d3eSAndra Paraschiv for (i = 0; i < ne_cpu_pool.nr_parent_vm_cores; i++)
364ff8a4d3eSAndra Paraschiv cpumask_clear(ne_cpu_pool.avail_threads_per_core[i]);
365ff8a4d3eSAndra Paraschiv free_cores_cpumask:
366ff8a4d3eSAndra Paraschiv for (i = 0; i < ne_cpu_pool.nr_parent_vm_cores; i++)
367ff8a4d3eSAndra Paraschiv free_cpumask_var(ne_cpu_pool.avail_threads_per_core[i]);
368ff8a4d3eSAndra Paraschiv kfree(ne_cpu_pool.avail_threads_per_core);
369ff8a4d3eSAndra Paraschiv free_pool_cpumask:
370ff8a4d3eSAndra Paraschiv free_cpumask_var(cpu_pool);
371ff8a4d3eSAndra Paraschiv ne_cpu_pool.nr_parent_vm_cores = 0;
372ff8a4d3eSAndra Paraschiv ne_cpu_pool.nr_threads_per_core = 0;
373ff8a4d3eSAndra Paraschiv ne_cpu_pool.numa_node = -1;
374ff8a4d3eSAndra Paraschiv mutex_unlock(&ne_cpu_pool.mutex);
375ff8a4d3eSAndra Paraschiv
376ff8a4d3eSAndra Paraschiv return rc;
377ff8a4d3eSAndra Paraschiv }
378ff8a4d3eSAndra Paraschiv
379ff8a4d3eSAndra Paraschiv /**
380ff8a4d3eSAndra Paraschiv * ne_teardown_cpu_pool() - Online the CPUs from the NE CPU pool and cleanup the
381ff8a4d3eSAndra Paraschiv * CPU pool.
382ff8a4d3eSAndra Paraschiv * @void: No parameters provided.
383ff8a4d3eSAndra Paraschiv *
384ff8a4d3eSAndra Paraschiv * Context: Process context.
385ff8a4d3eSAndra Paraschiv */
ne_teardown_cpu_pool(void)386ff8a4d3eSAndra Paraschiv static void ne_teardown_cpu_pool(void)
387ff8a4d3eSAndra Paraschiv {
388ff8a4d3eSAndra Paraschiv unsigned int cpu = 0;
389ff8a4d3eSAndra Paraschiv unsigned int i = 0;
390ff8a4d3eSAndra Paraschiv int rc = -EINVAL;
391ff8a4d3eSAndra Paraschiv
392ff8a4d3eSAndra Paraschiv mutex_lock(&ne_cpu_pool.mutex);
393ff8a4d3eSAndra Paraschiv
394ff8a4d3eSAndra Paraschiv if (!ne_cpu_pool.nr_parent_vm_cores) {
395ff8a4d3eSAndra Paraschiv mutex_unlock(&ne_cpu_pool.mutex);
396ff8a4d3eSAndra Paraschiv
397ff8a4d3eSAndra Paraschiv return;
398ff8a4d3eSAndra Paraschiv }
399ff8a4d3eSAndra Paraschiv
400ff8a4d3eSAndra Paraschiv for (i = 0; i < ne_cpu_pool.nr_parent_vm_cores; i++) {
401ff8a4d3eSAndra Paraschiv for_each_cpu(cpu, ne_cpu_pool.avail_threads_per_core[i]) {
402ff8a4d3eSAndra Paraschiv rc = add_cpu(cpu);
403ff8a4d3eSAndra Paraschiv if (rc != 0)
404ff8a4d3eSAndra Paraschiv pr_err("%s: CPU %d is not onlined [rc=%d]\n",
405ff8a4d3eSAndra Paraschiv ne_misc_dev.name, cpu, rc);
406ff8a4d3eSAndra Paraschiv }
407ff8a4d3eSAndra Paraschiv
408ff8a4d3eSAndra Paraschiv cpumask_clear(ne_cpu_pool.avail_threads_per_core[i]);
409ff8a4d3eSAndra Paraschiv
410ff8a4d3eSAndra Paraschiv free_cpumask_var(ne_cpu_pool.avail_threads_per_core[i]);
411ff8a4d3eSAndra Paraschiv }
412ff8a4d3eSAndra Paraschiv
413ff8a4d3eSAndra Paraschiv kfree(ne_cpu_pool.avail_threads_per_core);
414ff8a4d3eSAndra Paraschiv ne_cpu_pool.nr_parent_vm_cores = 0;
415ff8a4d3eSAndra Paraschiv ne_cpu_pool.nr_threads_per_core = 0;
416ff8a4d3eSAndra Paraschiv ne_cpu_pool.numa_node = -1;
417ff8a4d3eSAndra Paraschiv
418ff8a4d3eSAndra Paraschiv mutex_unlock(&ne_cpu_pool.mutex);
419ff8a4d3eSAndra Paraschiv }
420ff8a4d3eSAndra Paraschiv
421ff8a4d3eSAndra Paraschiv /**
422ff8a4d3eSAndra Paraschiv * ne_set_kernel_param() - Set the NE CPU pool value via the NE kernel parameter.
423ff8a4d3eSAndra Paraschiv * @val: NE CPU pool string value.
424ff8a4d3eSAndra Paraschiv * @kp : NE kernel parameter associated with the NE CPU pool.
425ff8a4d3eSAndra Paraschiv *
426ff8a4d3eSAndra Paraschiv * Context: Process context.
427ff8a4d3eSAndra Paraschiv * Return:
428ff8a4d3eSAndra Paraschiv * * 0 on success.
429ff8a4d3eSAndra Paraschiv * * Negative return value on failure.
430ff8a4d3eSAndra Paraschiv */
ne_set_kernel_param(const char * val,const struct kernel_param * kp)431ff8a4d3eSAndra Paraschiv static int ne_set_kernel_param(const char *val, const struct kernel_param *kp)
432ff8a4d3eSAndra Paraschiv {
433ff8a4d3eSAndra Paraschiv char error_val[] = "";
434ff8a4d3eSAndra Paraschiv int rc = -EINVAL;
435ff8a4d3eSAndra Paraschiv
436ff8a4d3eSAndra Paraschiv if (!capable(CAP_SYS_ADMIN))
437ff8a4d3eSAndra Paraschiv return -EPERM;
438ff8a4d3eSAndra Paraschiv
439ff8a4d3eSAndra Paraschiv if (ne_check_enclaves_created()) {
440ff8a4d3eSAndra Paraschiv pr_err("%s: The CPU pool is used by enclave(s)\n", ne_misc_dev.name);
441ff8a4d3eSAndra Paraschiv
442ff8a4d3eSAndra Paraschiv return -EPERM;
443ff8a4d3eSAndra Paraschiv }
444ff8a4d3eSAndra Paraschiv
445ff8a4d3eSAndra Paraschiv ne_teardown_cpu_pool();
446ff8a4d3eSAndra Paraschiv
447ff8a4d3eSAndra Paraschiv rc = ne_setup_cpu_pool(val);
448ff8a4d3eSAndra Paraschiv if (rc < 0) {
449ff8a4d3eSAndra Paraschiv pr_err("%s: Error in setup CPU pool [rc=%d]\n", ne_misc_dev.name, rc);
450ff8a4d3eSAndra Paraschiv
451ff8a4d3eSAndra Paraschiv param_set_copystring(error_val, kp);
452ff8a4d3eSAndra Paraschiv
453ff8a4d3eSAndra Paraschiv return rc;
454ff8a4d3eSAndra Paraschiv }
455ff8a4d3eSAndra Paraschiv
456ff8a4d3eSAndra Paraschiv rc = param_set_copystring(val, kp);
457ff8a4d3eSAndra Paraschiv if (rc < 0) {
458ff8a4d3eSAndra Paraschiv pr_err("%s: Error in param set copystring [rc=%d]\n", ne_misc_dev.name, rc);
459ff8a4d3eSAndra Paraschiv
460ff8a4d3eSAndra Paraschiv ne_teardown_cpu_pool();
461ff8a4d3eSAndra Paraschiv
462ff8a4d3eSAndra Paraschiv param_set_copystring(error_val, kp);
463ff8a4d3eSAndra Paraschiv
464ff8a4d3eSAndra Paraschiv return rc;
465ff8a4d3eSAndra Paraschiv }
466ff8a4d3eSAndra Paraschiv
467ff8a4d3eSAndra Paraschiv return 0;
468ff8a4d3eSAndra Paraschiv }
469ff8a4d3eSAndra Paraschiv
470ff8a4d3eSAndra Paraschiv /**
471ff8a4d3eSAndra Paraschiv * ne_donated_cpu() - Check if the provided CPU is already used by the enclave.
472ff8a4d3eSAndra Paraschiv * @ne_enclave : Private data associated with the current enclave.
473ff8a4d3eSAndra Paraschiv * @cpu: CPU to check if already used.
474ff8a4d3eSAndra Paraschiv *
475ff8a4d3eSAndra Paraschiv * Context: Process context. This function is called with the ne_enclave mutex held.
476ff8a4d3eSAndra Paraschiv * Return:
477ff8a4d3eSAndra Paraschiv * * True if the provided CPU is already used by the enclave.
478ff8a4d3eSAndra Paraschiv * * False otherwise.
479ff8a4d3eSAndra Paraschiv */
ne_donated_cpu(struct ne_enclave * ne_enclave,unsigned int cpu)480ff8a4d3eSAndra Paraschiv static bool ne_donated_cpu(struct ne_enclave *ne_enclave, unsigned int cpu)
481ff8a4d3eSAndra Paraschiv {
482ff8a4d3eSAndra Paraschiv if (cpumask_test_cpu(cpu, ne_enclave->vcpu_ids))
483ff8a4d3eSAndra Paraschiv return true;
484ff8a4d3eSAndra Paraschiv
485ff8a4d3eSAndra Paraschiv return false;
486ff8a4d3eSAndra Paraschiv }
487ff8a4d3eSAndra Paraschiv
488ff8a4d3eSAndra Paraschiv /**
489ff8a4d3eSAndra Paraschiv * ne_get_unused_core_from_cpu_pool() - Get the id of a full core from the
490ff8a4d3eSAndra Paraschiv * NE CPU pool.
491ff8a4d3eSAndra Paraschiv * @void: No parameters provided.
492ff8a4d3eSAndra Paraschiv *
493ff8a4d3eSAndra Paraschiv * Context: Process context. This function is called with the ne_enclave and
494ff8a4d3eSAndra Paraschiv * ne_cpu_pool mutexes held.
495ff8a4d3eSAndra Paraschiv * Return:
496ff8a4d3eSAndra Paraschiv * * Core id.
497ff8a4d3eSAndra Paraschiv * * -1 if no CPU core available in the pool.
498ff8a4d3eSAndra Paraschiv */
ne_get_unused_core_from_cpu_pool(void)499ff8a4d3eSAndra Paraschiv static int ne_get_unused_core_from_cpu_pool(void)
500ff8a4d3eSAndra Paraschiv {
501ff8a4d3eSAndra Paraschiv int core_id = -1;
502ff8a4d3eSAndra Paraschiv unsigned int i = 0;
503ff8a4d3eSAndra Paraschiv
504ff8a4d3eSAndra Paraschiv for (i = 0; i < ne_cpu_pool.nr_parent_vm_cores; i++)
505ff8a4d3eSAndra Paraschiv if (!cpumask_empty(ne_cpu_pool.avail_threads_per_core[i])) {
506ff8a4d3eSAndra Paraschiv core_id = i;
507ff8a4d3eSAndra Paraschiv
508ff8a4d3eSAndra Paraschiv break;
509ff8a4d3eSAndra Paraschiv }
510ff8a4d3eSAndra Paraschiv
511ff8a4d3eSAndra Paraschiv return core_id;
512ff8a4d3eSAndra Paraschiv }
513ff8a4d3eSAndra Paraschiv
514ff8a4d3eSAndra Paraschiv /**
515ff8a4d3eSAndra Paraschiv * ne_set_enclave_threads_per_core() - Set the threads of the provided core in
516ff8a4d3eSAndra Paraschiv * the enclave data structure.
517ff8a4d3eSAndra Paraschiv * @ne_enclave : Private data associated with the current enclave.
518ff8a4d3eSAndra Paraschiv * @core_id: Core id to get its threads from the NE CPU pool.
519ff8a4d3eSAndra Paraschiv * @vcpu_id: vCPU id part of the provided core.
520ff8a4d3eSAndra Paraschiv *
521ff8a4d3eSAndra Paraschiv * Context: Process context. This function is called with the ne_enclave and
522ff8a4d3eSAndra Paraschiv * ne_cpu_pool mutexes held.
523ff8a4d3eSAndra Paraschiv * Return:
524ff8a4d3eSAndra Paraschiv * * 0 on success.
525ff8a4d3eSAndra Paraschiv * * Negative return value on failure.
526ff8a4d3eSAndra Paraschiv */
ne_set_enclave_threads_per_core(struct ne_enclave * ne_enclave,int core_id,u32 vcpu_id)527ff8a4d3eSAndra Paraschiv static int ne_set_enclave_threads_per_core(struct ne_enclave *ne_enclave,
528ff8a4d3eSAndra Paraschiv int core_id, u32 vcpu_id)
529ff8a4d3eSAndra Paraschiv {
530ff8a4d3eSAndra Paraschiv unsigned int cpu = 0;
531ff8a4d3eSAndra Paraschiv
532ff8a4d3eSAndra Paraschiv if (core_id < 0 && vcpu_id == 0) {
533ff8a4d3eSAndra Paraschiv dev_err_ratelimited(ne_misc_dev.this_device,
534ff8a4d3eSAndra Paraschiv "No CPUs available in NE CPU pool\n");
535ff8a4d3eSAndra Paraschiv
536ff8a4d3eSAndra Paraschiv return -NE_ERR_NO_CPUS_AVAIL_IN_POOL;
537ff8a4d3eSAndra Paraschiv }
538ff8a4d3eSAndra Paraschiv
539ff8a4d3eSAndra Paraschiv if (core_id < 0) {
540ff8a4d3eSAndra Paraschiv dev_err_ratelimited(ne_misc_dev.this_device,
541ff8a4d3eSAndra Paraschiv "CPU %d is not in NE CPU pool\n", vcpu_id);
542ff8a4d3eSAndra Paraschiv
543ff8a4d3eSAndra Paraschiv return -NE_ERR_VCPU_NOT_IN_CPU_POOL;
544ff8a4d3eSAndra Paraschiv }
545ff8a4d3eSAndra Paraschiv
546ff8a4d3eSAndra Paraschiv if (core_id >= ne_enclave->nr_parent_vm_cores) {
547ff8a4d3eSAndra Paraschiv dev_err_ratelimited(ne_misc_dev.this_device,
548ff8a4d3eSAndra Paraschiv "Invalid core id %d - ne_enclave\n", core_id);
549ff8a4d3eSAndra Paraschiv
550ff8a4d3eSAndra Paraschiv return -NE_ERR_VCPU_INVALID_CPU_CORE;
551ff8a4d3eSAndra Paraschiv }
552ff8a4d3eSAndra Paraschiv
553ff8a4d3eSAndra Paraschiv for_each_cpu(cpu, ne_cpu_pool.avail_threads_per_core[core_id])
554ff8a4d3eSAndra Paraschiv cpumask_set_cpu(cpu, ne_enclave->threads_per_core[core_id]);
555ff8a4d3eSAndra Paraschiv
556ff8a4d3eSAndra Paraschiv cpumask_clear(ne_cpu_pool.avail_threads_per_core[core_id]);
557ff8a4d3eSAndra Paraschiv
558ff8a4d3eSAndra Paraschiv return 0;
559ff8a4d3eSAndra Paraschiv }
560ff8a4d3eSAndra Paraschiv
561ff8a4d3eSAndra Paraschiv /**
562ff8a4d3eSAndra Paraschiv * ne_get_cpu_from_cpu_pool() - Get a CPU from the NE CPU pool, either from the
563ff8a4d3eSAndra Paraschiv * remaining sibling(s) of a CPU core or the first
564ff8a4d3eSAndra Paraschiv * sibling of a new CPU core.
565ff8a4d3eSAndra Paraschiv * @ne_enclave : Private data associated with the current enclave.
566ff8a4d3eSAndra Paraschiv * @vcpu_id: vCPU to get from the NE CPU pool.
567ff8a4d3eSAndra Paraschiv *
568ff8a4d3eSAndra Paraschiv * Context: Process context. This function is called with the ne_enclave mutex held.
569ff8a4d3eSAndra Paraschiv * Return:
570ff8a4d3eSAndra Paraschiv * * 0 on success.
571ff8a4d3eSAndra Paraschiv * * Negative return value on failure.
572ff8a4d3eSAndra Paraschiv */
ne_get_cpu_from_cpu_pool(struct ne_enclave * ne_enclave,u32 * vcpu_id)573ff8a4d3eSAndra Paraschiv static int ne_get_cpu_from_cpu_pool(struct ne_enclave *ne_enclave, u32 *vcpu_id)
574ff8a4d3eSAndra Paraschiv {
575ff8a4d3eSAndra Paraschiv int core_id = -1;
576ff8a4d3eSAndra Paraschiv unsigned int cpu = 0;
577ff8a4d3eSAndra Paraschiv unsigned int i = 0;
578ff8a4d3eSAndra Paraschiv int rc = -EINVAL;
579ff8a4d3eSAndra Paraschiv
580ff8a4d3eSAndra Paraschiv /*
581ff8a4d3eSAndra Paraschiv * If previously allocated a thread of a core to this enclave, first
582ff8a4d3eSAndra Paraschiv * check remaining sibling(s) for new CPU allocations, so that full
583ff8a4d3eSAndra Paraschiv * CPU cores are used for the enclave.
584ff8a4d3eSAndra Paraschiv */
585ff8a4d3eSAndra Paraschiv for (i = 0; i < ne_enclave->nr_parent_vm_cores; i++)
586ff8a4d3eSAndra Paraschiv for_each_cpu(cpu, ne_enclave->threads_per_core[i])
587ff8a4d3eSAndra Paraschiv if (!ne_donated_cpu(ne_enclave, cpu)) {
588ff8a4d3eSAndra Paraschiv *vcpu_id = cpu;
589ff8a4d3eSAndra Paraschiv
590ff8a4d3eSAndra Paraschiv return 0;
591ff8a4d3eSAndra Paraschiv }
592ff8a4d3eSAndra Paraschiv
593ff8a4d3eSAndra Paraschiv mutex_lock(&ne_cpu_pool.mutex);
594ff8a4d3eSAndra Paraschiv
595ff8a4d3eSAndra Paraschiv /*
596ff8a4d3eSAndra Paraschiv * If no remaining siblings, get a core from the NE CPU pool and keep
597ff8a4d3eSAndra Paraschiv * track of all the threads in the enclave threads per core data structure.
598ff8a4d3eSAndra Paraschiv */
599ff8a4d3eSAndra Paraschiv core_id = ne_get_unused_core_from_cpu_pool();
600ff8a4d3eSAndra Paraschiv
601ff8a4d3eSAndra Paraschiv rc = ne_set_enclave_threads_per_core(ne_enclave, core_id, *vcpu_id);
602ff8a4d3eSAndra Paraschiv if (rc < 0)
603ff8a4d3eSAndra Paraschiv goto unlock_mutex;
604ff8a4d3eSAndra Paraschiv
605ff8a4d3eSAndra Paraschiv *vcpu_id = cpumask_any(ne_enclave->threads_per_core[core_id]);
606ff8a4d3eSAndra Paraschiv
607ff8a4d3eSAndra Paraschiv rc = 0;
608ff8a4d3eSAndra Paraschiv
609ff8a4d3eSAndra Paraschiv unlock_mutex:
610ff8a4d3eSAndra Paraschiv mutex_unlock(&ne_cpu_pool.mutex);
611ff8a4d3eSAndra Paraschiv
612ff8a4d3eSAndra Paraschiv return rc;
613ff8a4d3eSAndra Paraschiv }
614ff8a4d3eSAndra Paraschiv
615ff8a4d3eSAndra Paraschiv /**
616ff8a4d3eSAndra Paraschiv * ne_get_vcpu_core_from_cpu_pool() - Get from the NE CPU pool the id of the
617ff8a4d3eSAndra Paraschiv * core associated with the provided vCPU.
618ff8a4d3eSAndra Paraschiv * @vcpu_id: Provided vCPU id to get its associated core id.
619ff8a4d3eSAndra Paraschiv *
620ff8a4d3eSAndra Paraschiv * Context: Process context. This function is called with the ne_enclave and
621ff8a4d3eSAndra Paraschiv * ne_cpu_pool mutexes held.
622ff8a4d3eSAndra Paraschiv * Return:
623ff8a4d3eSAndra Paraschiv * * Core id.
624ff8a4d3eSAndra Paraschiv * * -1 if the provided vCPU is not in the pool.
625ff8a4d3eSAndra Paraschiv */
ne_get_vcpu_core_from_cpu_pool(u32 vcpu_id)626ff8a4d3eSAndra Paraschiv static int ne_get_vcpu_core_from_cpu_pool(u32 vcpu_id)
627ff8a4d3eSAndra Paraschiv {
628ff8a4d3eSAndra Paraschiv int core_id = -1;
629ff8a4d3eSAndra Paraschiv unsigned int i = 0;
630ff8a4d3eSAndra Paraschiv
631ff8a4d3eSAndra Paraschiv for (i = 0; i < ne_cpu_pool.nr_parent_vm_cores; i++)
632ff8a4d3eSAndra Paraschiv if (cpumask_test_cpu(vcpu_id, ne_cpu_pool.avail_threads_per_core[i])) {
633ff8a4d3eSAndra Paraschiv core_id = i;
634ff8a4d3eSAndra Paraschiv
635ff8a4d3eSAndra Paraschiv break;
636ff8a4d3eSAndra Paraschiv }
637ff8a4d3eSAndra Paraschiv
638ff8a4d3eSAndra Paraschiv return core_id;
639ff8a4d3eSAndra Paraschiv }
640ff8a4d3eSAndra Paraschiv
641ff8a4d3eSAndra Paraschiv /**
642ff8a4d3eSAndra Paraschiv * ne_check_cpu_in_cpu_pool() - Check if the given vCPU is in the available CPUs
643ff8a4d3eSAndra Paraschiv * from the pool.
644ff8a4d3eSAndra Paraschiv * @ne_enclave : Private data associated with the current enclave.
645ff8a4d3eSAndra Paraschiv * @vcpu_id: ID of the vCPU to check if available in the NE CPU pool.
646ff8a4d3eSAndra Paraschiv *
647ff8a4d3eSAndra Paraschiv * Context: Process context. This function is called with the ne_enclave mutex held.
648ff8a4d3eSAndra Paraschiv * Return:
649ff8a4d3eSAndra Paraschiv * * 0 on success.
650ff8a4d3eSAndra Paraschiv * * Negative return value on failure.
651ff8a4d3eSAndra Paraschiv */
ne_check_cpu_in_cpu_pool(struct ne_enclave * ne_enclave,u32 vcpu_id)652ff8a4d3eSAndra Paraschiv static int ne_check_cpu_in_cpu_pool(struct ne_enclave *ne_enclave, u32 vcpu_id)
653ff8a4d3eSAndra Paraschiv {
654ff8a4d3eSAndra Paraschiv int core_id = -1;
655ff8a4d3eSAndra Paraschiv unsigned int i = 0;
656ff8a4d3eSAndra Paraschiv int rc = -EINVAL;
657ff8a4d3eSAndra Paraschiv
658ff8a4d3eSAndra Paraschiv if (ne_donated_cpu(ne_enclave, vcpu_id)) {
659ff8a4d3eSAndra Paraschiv dev_err_ratelimited(ne_misc_dev.this_device,
660ff8a4d3eSAndra Paraschiv "CPU %d already used\n", vcpu_id);
661ff8a4d3eSAndra Paraschiv
662ff8a4d3eSAndra Paraschiv return -NE_ERR_VCPU_ALREADY_USED;
663ff8a4d3eSAndra Paraschiv }
664ff8a4d3eSAndra Paraschiv
665ff8a4d3eSAndra Paraschiv /*
666ff8a4d3eSAndra Paraschiv * If previously allocated a thread of a core to this enclave, but not
667ff8a4d3eSAndra Paraschiv * the full core, first check remaining sibling(s).
668ff8a4d3eSAndra Paraschiv */
669ff8a4d3eSAndra Paraschiv for (i = 0; i < ne_enclave->nr_parent_vm_cores; i++)
670ff8a4d3eSAndra Paraschiv if (cpumask_test_cpu(vcpu_id, ne_enclave->threads_per_core[i]))
671ff8a4d3eSAndra Paraschiv return 0;
672ff8a4d3eSAndra Paraschiv
673ff8a4d3eSAndra Paraschiv mutex_lock(&ne_cpu_pool.mutex);
674ff8a4d3eSAndra Paraschiv
675ff8a4d3eSAndra Paraschiv /*
676ff8a4d3eSAndra Paraschiv * If no remaining siblings, get from the NE CPU pool the core
677ff8a4d3eSAndra Paraschiv * associated with the vCPU and keep track of all the threads in the
678ff8a4d3eSAndra Paraschiv * enclave threads per core data structure.
679ff8a4d3eSAndra Paraschiv */
680ff8a4d3eSAndra Paraschiv core_id = ne_get_vcpu_core_from_cpu_pool(vcpu_id);
681ff8a4d3eSAndra Paraschiv
682ff8a4d3eSAndra Paraschiv rc = ne_set_enclave_threads_per_core(ne_enclave, core_id, vcpu_id);
683ff8a4d3eSAndra Paraschiv if (rc < 0)
684ff8a4d3eSAndra Paraschiv goto unlock_mutex;
685ff8a4d3eSAndra Paraschiv
686ff8a4d3eSAndra Paraschiv rc = 0;
687ff8a4d3eSAndra Paraschiv
688ff8a4d3eSAndra Paraschiv unlock_mutex:
689ff8a4d3eSAndra Paraschiv mutex_unlock(&ne_cpu_pool.mutex);
690ff8a4d3eSAndra Paraschiv
691ff8a4d3eSAndra Paraschiv return rc;
692ff8a4d3eSAndra Paraschiv }
693ff8a4d3eSAndra Paraschiv
694ff8a4d3eSAndra Paraschiv /**
695ff8a4d3eSAndra Paraschiv * ne_add_vcpu_ioctl() - Add a vCPU to the slot associated with the current
696ff8a4d3eSAndra Paraschiv * enclave.
697ff8a4d3eSAndra Paraschiv * @ne_enclave : Private data associated with the current enclave.
698ff8a4d3eSAndra Paraschiv * @vcpu_id: ID of the CPU to be associated with the given slot,
699ff8a4d3eSAndra Paraschiv * apic id on x86.
700ff8a4d3eSAndra Paraschiv *
701ff8a4d3eSAndra Paraschiv * Context: Process context. This function is called with the ne_enclave mutex held.
702ff8a4d3eSAndra Paraschiv * Return:
703ff8a4d3eSAndra Paraschiv * * 0 on success.
704ff8a4d3eSAndra Paraschiv * * Negative return value on failure.
705ff8a4d3eSAndra Paraschiv */
ne_add_vcpu_ioctl(struct ne_enclave * ne_enclave,u32 vcpu_id)706ff8a4d3eSAndra Paraschiv static int ne_add_vcpu_ioctl(struct ne_enclave *ne_enclave, u32 vcpu_id)
707ff8a4d3eSAndra Paraschiv {
708ff8a4d3eSAndra Paraschiv struct ne_pci_dev_cmd_reply cmd_reply = {};
709ff8a4d3eSAndra Paraschiv struct pci_dev *pdev = ne_devs.ne_pci_dev->pdev;
710ff8a4d3eSAndra Paraschiv int rc = -EINVAL;
711ff8a4d3eSAndra Paraschiv struct slot_add_vcpu_req slot_add_vcpu_req = {};
712ff8a4d3eSAndra Paraschiv
713ff8a4d3eSAndra Paraschiv if (ne_enclave->mm != current->mm)
714ff8a4d3eSAndra Paraschiv return -EIO;
715ff8a4d3eSAndra Paraschiv
716ff8a4d3eSAndra Paraschiv slot_add_vcpu_req.slot_uid = ne_enclave->slot_uid;
717ff8a4d3eSAndra Paraschiv slot_add_vcpu_req.vcpu_id = vcpu_id;
718ff8a4d3eSAndra Paraschiv
719ff8a4d3eSAndra Paraschiv rc = ne_do_request(pdev, SLOT_ADD_VCPU,
720ff8a4d3eSAndra Paraschiv &slot_add_vcpu_req, sizeof(slot_add_vcpu_req),
721ff8a4d3eSAndra Paraschiv &cmd_reply, sizeof(cmd_reply));
722ff8a4d3eSAndra Paraschiv if (rc < 0) {
723ff8a4d3eSAndra Paraschiv dev_err_ratelimited(ne_misc_dev.this_device,
724ff8a4d3eSAndra Paraschiv "Error in slot add vCPU [rc=%d]\n", rc);
725ff8a4d3eSAndra Paraschiv
726ff8a4d3eSAndra Paraschiv return rc;
727ff8a4d3eSAndra Paraschiv }
728ff8a4d3eSAndra Paraschiv
729ff8a4d3eSAndra Paraschiv cpumask_set_cpu(vcpu_id, ne_enclave->vcpu_ids);
730ff8a4d3eSAndra Paraschiv
731ff8a4d3eSAndra Paraschiv ne_enclave->nr_vcpus++;
732ff8a4d3eSAndra Paraschiv
733ff8a4d3eSAndra Paraschiv return 0;
734ff8a4d3eSAndra Paraschiv }
735ff8a4d3eSAndra Paraschiv
736ff8a4d3eSAndra Paraschiv /**
7377dc9d430SAndra Paraschiv * ne_sanity_check_user_mem_region() - Sanity check the user space memory
7387dc9d430SAndra Paraschiv * region received during the set user
7397dc9d430SAndra Paraschiv * memory region ioctl call.
7407dc9d430SAndra Paraschiv * @ne_enclave : Private data associated with the current enclave.
7417dc9d430SAndra Paraschiv * @mem_region : User space memory region to be sanity checked.
7427dc9d430SAndra Paraschiv *
7437dc9d430SAndra Paraschiv * Context: Process context. This function is called with the ne_enclave mutex held.
7447dc9d430SAndra Paraschiv * Return:
7457dc9d430SAndra Paraschiv * * 0 on success.
7467dc9d430SAndra Paraschiv * * Negative return value on failure.
7477dc9d430SAndra Paraschiv */
ne_sanity_check_user_mem_region(struct ne_enclave * ne_enclave,struct ne_user_memory_region mem_region)7487dc9d430SAndra Paraschiv static int ne_sanity_check_user_mem_region(struct ne_enclave *ne_enclave,
7497dc9d430SAndra Paraschiv struct ne_user_memory_region mem_region)
7507dc9d430SAndra Paraschiv {
7517dc9d430SAndra Paraschiv struct ne_mem_region *ne_mem_region = NULL;
7527dc9d430SAndra Paraschiv
7537dc9d430SAndra Paraschiv if (ne_enclave->mm != current->mm)
7547dc9d430SAndra Paraschiv return -EIO;
7557dc9d430SAndra Paraschiv
7567dc9d430SAndra Paraschiv if (mem_region.memory_size & (NE_MIN_MEM_REGION_SIZE - 1)) {
7577dc9d430SAndra Paraschiv dev_err_ratelimited(ne_misc_dev.this_device,
7587dc9d430SAndra Paraschiv "User space memory size is not multiple of 2 MiB\n");
7597dc9d430SAndra Paraschiv
7607dc9d430SAndra Paraschiv return -NE_ERR_INVALID_MEM_REGION_SIZE;
7617dc9d430SAndra Paraschiv }
7627dc9d430SAndra Paraschiv
7637dc9d430SAndra Paraschiv if (!IS_ALIGNED(mem_region.userspace_addr, NE_MIN_MEM_REGION_SIZE)) {
7647dc9d430SAndra Paraschiv dev_err_ratelimited(ne_misc_dev.this_device,
7657dc9d430SAndra Paraschiv "User space address is not 2 MiB aligned\n");
7667dc9d430SAndra Paraschiv
7677dc9d430SAndra Paraschiv return -NE_ERR_UNALIGNED_MEM_REGION_ADDR;
7687dc9d430SAndra Paraschiv }
7697dc9d430SAndra Paraschiv
7707dc9d430SAndra Paraschiv if ((mem_region.userspace_addr & (NE_MIN_MEM_REGION_SIZE - 1)) ||
7717dc9d430SAndra Paraschiv !access_ok((void __user *)(unsigned long)mem_region.userspace_addr,
7727dc9d430SAndra Paraschiv mem_region.memory_size)) {
7737dc9d430SAndra Paraschiv dev_err_ratelimited(ne_misc_dev.this_device,
7747dc9d430SAndra Paraschiv "Invalid user space address range\n");
7757dc9d430SAndra Paraschiv
7767dc9d430SAndra Paraschiv return -NE_ERR_INVALID_MEM_REGION_ADDR;
7777dc9d430SAndra Paraschiv }
7787dc9d430SAndra Paraschiv
7797dc9d430SAndra Paraschiv list_for_each_entry(ne_mem_region, &ne_enclave->mem_regions_list,
7807dc9d430SAndra Paraschiv mem_region_list_entry) {
7817dc9d430SAndra Paraschiv u64 memory_size = ne_mem_region->memory_size;
7827dc9d430SAndra Paraschiv u64 userspace_addr = ne_mem_region->userspace_addr;
7837dc9d430SAndra Paraschiv
7847dc9d430SAndra Paraschiv if ((userspace_addr <= mem_region.userspace_addr &&
7857dc9d430SAndra Paraschiv mem_region.userspace_addr < (userspace_addr + memory_size)) ||
7867dc9d430SAndra Paraschiv (mem_region.userspace_addr <= userspace_addr &&
7877dc9d430SAndra Paraschiv (mem_region.userspace_addr + mem_region.memory_size) > userspace_addr)) {
7887dc9d430SAndra Paraschiv dev_err_ratelimited(ne_misc_dev.this_device,
7897dc9d430SAndra Paraschiv "User space memory region already used\n");
7907dc9d430SAndra Paraschiv
7917dc9d430SAndra Paraschiv return -NE_ERR_MEM_REGION_ALREADY_USED;
7927dc9d430SAndra Paraschiv }
7937dc9d430SAndra Paraschiv }
7947dc9d430SAndra Paraschiv
7957dc9d430SAndra Paraschiv return 0;
7967dc9d430SAndra Paraschiv }
7977dc9d430SAndra Paraschiv
7987dc9d430SAndra Paraschiv /**
7997dc9d430SAndra Paraschiv * ne_sanity_check_user_mem_region_page() - Sanity check a page from the user space
8007dc9d430SAndra Paraschiv * memory region received during the set
8017dc9d430SAndra Paraschiv * user memory region ioctl call.
8027dc9d430SAndra Paraschiv * @ne_enclave : Private data associated with the current enclave.
8037dc9d430SAndra Paraschiv * @mem_region_page: Page from the user space memory region to be sanity checked.
8047dc9d430SAndra Paraschiv *
8057dc9d430SAndra Paraschiv * Context: Process context. This function is called with the ne_enclave mutex held.
8067dc9d430SAndra Paraschiv * Return:
8077dc9d430SAndra Paraschiv * * 0 on success.
8087dc9d430SAndra Paraschiv * * Negative return value on failure.
8097dc9d430SAndra Paraschiv */
ne_sanity_check_user_mem_region_page(struct ne_enclave * ne_enclave,struct page * mem_region_page)8107dc9d430SAndra Paraschiv static int ne_sanity_check_user_mem_region_page(struct ne_enclave *ne_enclave,
8117dc9d430SAndra Paraschiv struct page *mem_region_page)
8127dc9d430SAndra Paraschiv {
8137dc9d430SAndra Paraschiv if (!PageHuge(mem_region_page)) {
8147dc9d430SAndra Paraschiv dev_err_ratelimited(ne_misc_dev.this_device,
8157dc9d430SAndra Paraschiv "Not a hugetlbfs page\n");
8167dc9d430SAndra Paraschiv
8177dc9d430SAndra Paraschiv return -NE_ERR_MEM_NOT_HUGE_PAGE;
8187dc9d430SAndra Paraschiv }
8197dc9d430SAndra Paraschiv
8207dc9d430SAndra Paraschiv if (page_size(mem_region_page) & (NE_MIN_MEM_REGION_SIZE - 1)) {
8217dc9d430SAndra Paraschiv dev_err_ratelimited(ne_misc_dev.this_device,
8227dc9d430SAndra Paraschiv "Page size not multiple of 2 MiB\n");
8237dc9d430SAndra Paraschiv
8247dc9d430SAndra Paraschiv return -NE_ERR_INVALID_PAGE_SIZE;
8257dc9d430SAndra Paraschiv }
8267dc9d430SAndra Paraschiv
8277dc9d430SAndra Paraschiv if (ne_enclave->numa_node != page_to_nid(mem_region_page)) {
8287dc9d430SAndra Paraschiv dev_err_ratelimited(ne_misc_dev.this_device,
8297dc9d430SAndra Paraschiv "Page is not from NUMA node %d\n",
8307dc9d430SAndra Paraschiv ne_enclave->numa_node);
8317dc9d430SAndra Paraschiv
8327dc9d430SAndra Paraschiv return -NE_ERR_MEM_DIFFERENT_NUMA_NODE;
8337dc9d430SAndra Paraschiv }
8347dc9d430SAndra Paraschiv
8357dc9d430SAndra Paraschiv return 0;
8367dc9d430SAndra Paraschiv }
8377dc9d430SAndra Paraschiv
8387dc9d430SAndra Paraschiv /**
839090ce783SLongpeng * ne_sanity_check_phys_mem_region() - Sanity check the start address and the size
840090ce783SLongpeng * of a physical memory region.
841090ce783SLongpeng * @phys_mem_region_paddr : Physical start address of the region to be sanity checked.
842090ce783SLongpeng * @phys_mem_region_size : Length of the region to be sanity checked.
843090ce783SLongpeng *
844090ce783SLongpeng * Context: Process context. This function is called with the ne_enclave mutex held.
845090ce783SLongpeng * Return:
846090ce783SLongpeng * * 0 on success.
847090ce783SLongpeng * * Negative return value on failure.
848090ce783SLongpeng */
ne_sanity_check_phys_mem_region(u64 phys_mem_region_paddr,u64 phys_mem_region_size)849090ce783SLongpeng static int ne_sanity_check_phys_mem_region(u64 phys_mem_region_paddr,
850090ce783SLongpeng u64 phys_mem_region_size)
851090ce783SLongpeng {
852090ce783SLongpeng if (phys_mem_region_size & (NE_MIN_MEM_REGION_SIZE - 1)) {
853090ce783SLongpeng dev_err_ratelimited(ne_misc_dev.this_device,
854090ce783SLongpeng "Physical mem region size is not multiple of 2 MiB\n");
855090ce783SLongpeng
856090ce783SLongpeng return -EINVAL;
857090ce783SLongpeng }
858090ce783SLongpeng
859090ce783SLongpeng if (!IS_ALIGNED(phys_mem_region_paddr, NE_MIN_MEM_REGION_SIZE)) {
860090ce783SLongpeng dev_err_ratelimited(ne_misc_dev.this_device,
861090ce783SLongpeng "Physical mem region address is not 2 MiB aligned\n");
862090ce783SLongpeng
863090ce783SLongpeng return -EINVAL;
864090ce783SLongpeng }
865090ce783SLongpeng
866090ce783SLongpeng return 0;
867090ce783SLongpeng }
868090ce783SLongpeng
869090ce783SLongpeng /**
870f6bdc0aaSLongpeng * ne_merge_phys_contig_memory_regions() - Add a memory region and merge the adjacent
871f6bdc0aaSLongpeng * regions if they are physically contiguous.
872f6bdc0aaSLongpeng * @phys_contig_regions : Private data associated with the contiguous physical memory regions.
873f6bdc0aaSLongpeng * @page_paddr : Physical start address of the region to be added.
874f6bdc0aaSLongpeng * @page_size : Length of the region to be added.
875f6bdc0aaSLongpeng *
876f6bdc0aaSLongpeng * Context: Process context. This function is called with the ne_enclave mutex held.
877090ce783SLongpeng * Return:
878090ce783SLongpeng * * 0 on success.
879090ce783SLongpeng * * Negative return value on failure.
880f6bdc0aaSLongpeng */
881090ce783SLongpeng static int
ne_merge_phys_contig_memory_regions(struct ne_phys_contig_mem_regions * phys_contig_regions,u64 page_paddr,u64 page_size)882f6bdc0aaSLongpeng ne_merge_phys_contig_memory_regions(struct ne_phys_contig_mem_regions *phys_contig_regions,
883f6bdc0aaSLongpeng u64 page_paddr, u64 page_size)
884f6bdc0aaSLongpeng {
885f6bdc0aaSLongpeng unsigned long num = phys_contig_regions->num;
886090ce783SLongpeng int rc = 0;
887090ce783SLongpeng
888090ce783SLongpeng rc = ne_sanity_check_phys_mem_region(page_paddr, page_size);
889090ce783SLongpeng if (rc < 0)
890090ce783SLongpeng return rc;
891f6bdc0aaSLongpeng
892f6bdc0aaSLongpeng /* Physically contiguous, just merge */
893f6bdc0aaSLongpeng if (num && (phys_contig_regions->regions[num - 1].end + 1) == page_paddr) {
894f6bdc0aaSLongpeng phys_contig_regions->regions[num - 1].end += page_size;
895090ce783SLongpeng } else {
896f6bdc0aaSLongpeng phys_contig_regions->regions[num].start = page_paddr;
897f6bdc0aaSLongpeng phys_contig_regions->regions[num].end = page_paddr + page_size - 1;
898f6bdc0aaSLongpeng phys_contig_regions->num++;
899f6bdc0aaSLongpeng }
900f6bdc0aaSLongpeng
901090ce783SLongpeng return 0;
902090ce783SLongpeng }
903090ce783SLongpeng
904f6bdc0aaSLongpeng /**
9057dc9d430SAndra Paraschiv * ne_set_user_memory_region_ioctl() - Add user space memory region to the slot
9067dc9d430SAndra Paraschiv * associated with the current enclave.
9077dc9d430SAndra Paraschiv * @ne_enclave : Private data associated with the current enclave.
9087dc9d430SAndra Paraschiv * @mem_region : User space memory region to be associated with the given slot.
9097dc9d430SAndra Paraschiv *
9107dc9d430SAndra Paraschiv * Context: Process context. This function is called with the ne_enclave mutex held.
9117dc9d430SAndra Paraschiv * Return:
9127dc9d430SAndra Paraschiv * * 0 on success.
9137dc9d430SAndra Paraschiv * * Negative return value on failure.
9147dc9d430SAndra Paraschiv */
ne_set_user_memory_region_ioctl(struct ne_enclave * ne_enclave,struct ne_user_memory_region mem_region)9157dc9d430SAndra Paraschiv static int ne_set_user_memory_region_ioctl(struct ne_enclave *ne_enclave,
9167dc9d430SAndra Paraschiv struct ne_user_memory_region mem_region)
9177dc9d430SAndra Paraschiv {
9187dc9d430SAndra Paraschiv long gup_rc = 0;
9197dc9d430SAndra Paraschiv unsigned long i = 0;
9207dc9d430SAndra Paraschiv unsigned long max_nr_pages = 0;
9217dc9d430SAndra Paraschiv unsigned long memory_size = 0;
9227dc9d430SAndra Paraschiv struct ne_mem_region *ne_mem_region = NULL;
9237dc9d430SAndra Paraschiv struct pci_dev *pdev = ne_devs.ne_pci_dev->pdev;
924f6bdc0aaSLongpeng struct ne_phys_contig_mem_regions phys_contig_mem_regions = {};
9257dc9d430SAndra Paraschiv int rc = -EINVAL;
9267dc9d430SAndra Paraschiv
9277dc9d430SAndra Paraschiv rc = ne_sanity_check_user_mem_region(ne_enclave, mem_region);
9287dc9d430SAndra Paraschiv if (rc < 0)
9297dc9d430SAndra Paraschiv return rc;
9307dc9d430SAndra Paraschiv
9317dc9d430SAndra Paraschiv ne_mem_region = kzalloc(sizeof(*ne_mem_region), GFP_KERNEL);
9327dc9d430SAndra Paraschiv if (!ne_mem_region)
9337dc9d430SAndra Paraschiv return -ENOMEM;
9347dc9d430SAndra Paraschiv
9357dc9d430SAndra Paraschiv max_nr_pages = mem_region.memory_size / NE_MIN_MEM_REGION_SIZE;
9367dc9d430SAndra Paraschiv
9377dc9d430SAndra Paraschiv ne_mem_region->pages = kcalloc(max_nr_pages, sizeof(*ne_mem_region->pages),
9387dc9d430SAndra Paraschiv GFP_KERNEL);
9397dc9d430SAndra Paraschiv if (!ne_mem_region->pages) {
9407dc9d430SAndra Paraschiv rc = -ENOMEM;
9417dc9d430SAndra Paraschiv
9427dc9d430SAndra Paraschiv goto free_mem_region;
9437dc9d430SAndra Paraschiv }
9447dc9d430SAndra Paraschiv
945f6bdc0aaSLongpeng phys_contig_mem_regions.regions = kcalloc(max_nr_pages,
946f6bdc0aaSLongpeng sizeof(*phys_contig_mem_regions.regions),
9477dc9d430SAndra Paraschiv GFP_KERNEL);
948f6bdc0aaSLongpeng if (!phys_contig_mem_regions.regions) {
9497dc9d430SAndra Paraschiv rc = -ENOMEM;
9507dc9d430SAndra Paraschiv
9517dc9d430SAndra Paraschiv goto free_mem_region;
9527dc9d430SAndra Paraschiv }
9537dc9d430SAndra Paraschiv
9547dc9d430SAndra Paraschiv do {
9557dc9d430SAndra Paraschiv i = ne_mem_region->nr_pages;
9567dc9d430SAndra Paraschiv
9577dc9d430SAndra Paraschiv if (i == max_nr_pages) {
9587dc9d430SAndra Paraschiv dev_err_ratelimited(ne_misc_dev.this_device,
9597dc9d430SAndra Paraschiv "Reached max nr of pages in the pages data struct\n");
9607dc9d430SAndra Paraschiv
9617dc9d430SAndra Paraschiv rc = -ENOMEM;
9627dc9d430SAndra Paraschiv
9637dc9d430SAndra Paraschiv goto put_pages;
9647dc9d430SAndra Paraschiv }
9657dc9d430SAndra Paraschiv
966*3a0152b2SAndra Paraschiv gup_rc = get_user_pages_unlocked(mem_region.userspace_addr + memory_size, 1,
967*3a0152b2SAndra Paraschiv ne_mem_region->pages + i, FOLL_GET);
968*3a0152b2SAndra Paraschiv
9697dc9d430SAndra Paraschiv if (gup_rc < 0) {
9707dc9d430SAndra Paraschiv rc = gup_rc;
9717dc9d430SAndra Paraschiv
9727dc9d430SAndra Paraschiv dev_err_ratelimited(ne_misc_dev.this_device,
9737dc9d430SAndra Paraschiv "Error in get user pages [rc=%d]\n", rc);
9747dc9d430SAndra Paraschiv
9757dc9d430SAndra Paraschiv goto put_pages;
9767dc9d430SAndra Paraschiv }
9777dc9d430SAndra Paraschiv
9787dc9d430SAndra Paraschiv rc = ne_sanity_check_user_mem_region_page(ne_enclave, ne_mem_region->pages[i]);
9797dc9d430SAndra Paraschiv if (rc < 0)
9807dc9d430SAndra Paraschiv goto put_pages;
9817dc9d430SAndra Paraschiv
982090ce783SLongpeng rc = ne_merge_phys_contig_memory_regions(&phys_contig_mem_regions,
983f6bdc0aaSLongpeng page_to_phys(ne_mem_region->pages[i]),
984f6bdc0aaSLongpeng page_size(ne_mem_region->pages[i]));
985090ce783SLongpeng if (rc < 0)
986090ce783SLongpeng goto put_pages;
9877dc9d430SAndra Paraschiv
9887dc9d430SAndra Paraschiv memory_size += page_size(ne_mem_region->pages[i]);
9897dc9d430SAndra Paraschiv
9907dc9d430SAndra Paraschiv ne_mem_region->nr_pages++;
9917dc9d430SAndra Paraschiv } while (memory_size < mem_region.memory_size);
9927dc9d430SAndra Paraschiv
993f6bdc0aaSLongpeng if ((ne_enclave->nr_mem_regions + phys_contig_mem_regions.num) >
9947dc9d430SAndra Paraschiv ne_enclave->max_mem_regions) {
9957dc9d430SAndra Paraschiv dev_err_ratelimited(ne_misc_dev.this_device,
9967dc9d430SAndra Paraschiv "Reached max memory regions %lld\n",
9977dc9d430SAndra Paraschiv ne_enclave->max_mem_regions);
9987dc9d430SAndra Paraschiv
9997dc9d430SAndra Paraschiv rc = -NE_ERR_MEM_MAX_REGIONS;
10007dc9d430SAndra Paraschiv
10017dc9d430SAndra Paraschiv goto put_pages;
10027dc9d430SAndra Paraschiv }
10037dc9d430SAndra Paraschiv
1004f6bdc0aaSLongpeng for (i = 0; i < phys_contig_mem_regions.num; i++) {
1005f6bdc0aaSLongpeng u64 phys_region_addr = phys_contig_mem_regions.regions[i].start;
1006f6bdc0aaSLongpeng u64 phys_region_size = range_len(&phys_contig_mem_regions.regions[i]);
10077dc9d430SAndra Paraschiv
1008090ce783SLongpeng rc = ne_sanity_check_phys_mem_region(phys_region_addr, phys_region_size);
1009090ce783SLongpeng if (rc < 0)
10107dc9d430SAndra Paraschiv goto put_pages;
10117dc9d430SAndra Paraschiv }
10127dc9d430SAndra Paraschiv
10137dc9d430SAndra Paraschiv ne_mem_region->memory_size = mem_region.memory_size;
10147dc9d430SAndra Paraschiv ne_mem_region->userspace_addr = mem_region.userspace_addr;
10157dc9d430SAndra Paraschiv
10167dc9d430SAndra Paraschiv list_add(&ne_mem_region->mem_region_list_entry, &ne_enclave->mem_regions_list);
10177dc9d430SAndra Paraschiv
1018f6bdc0aaSLongpeng for (i = 0; i < phys_contig_mem_regions.num; i++) {
10197dc9d430SAndra Paraschiv struct ne_pci_dev_cmd_reply cmd_reply = {};
10207dc9d430SAndra Paraschiv struct slot_add_mem_req slot_add_mem_req = {};
10217dc9d430SAndra Paraschiv
10227dc9d430SAndra Paraschiv slot_add_mem_req.slot_uid = ne_enclave->slot_uid;
1023f6bdc0aaSLongpeng slot_add_mem_req.paddr = phys_contig_mem_regions.regions[i].start;
1024f6bdc0aaSLongpeng slot_add_mem_req.size = range_len(&phys_contig_mem_regions.regions[i]);
10257dc9d430SAndra Paraschiv
10267dc9d430SAndra Paraschiv rc = ne_do_request(pdev, SLOT_ADD_MEM,
10277dc9d430SAndra Paraschiv &slot_add_mem_req, sizeof(slot_add_mem_req),
10287dc9d430SAndra Paraschiv &cmd_reply, sizeof(cmd_reply));
10297dc9d430SAndra Paraschiv if (rc < 0) {
10307dc9d430SAndra Paraschiv dev_err_ratelimited(ne_misc_dev.this_device,
10317dc9d430SAndra Paraschiv "Error in slot add mem [rc=%d]\n", rc);
10327dc9d430SAndra Paraschiv
1033f6bdc0aaSLongpeng kfree(phys_contig_mem_regions.regions);
10347dc9d430SAndra Paraschiv
10357dc9d430SAndra Paraschiv /*
10367dc9d430SAndra Paraschiv * Exit here without put pages as memory regions may
10377dc9d430SAndra Paraschiv * already been added.
10387dc9d430SAndra Paraschiv */
10397dc9d430SAndra Paraschiv return rc;
10407dc9d430SAndra Paraschiv }
10417dc9d430SAndra Paraschiv
10427dc9d430SAndra Paraschiv ne_enclave->mem_size += slot_add_mem_req.size;
10437dc9d430SAndra Paraschiv ne_enclave->nr_mem_regions++;
10447dc9d430SAndra Paraschiv }
10457dc9d430SAndra Paraschiv
1046f6bdc0aaSLongpeng kfree(phys_contig_mem_regions.regions);
10477dc9d430SAndra Paraschiv
10487dc9d430SAndra Paraschiv return 0;
10497dc9d430SAndra Paraschiv
10507dc9d430SAndra Paraschiv put_pages:
10517dc9d430SAndra Paraschiv for (i = 0; i < ne_mem_region->nr_pages; i++)
10527dc9d430SAndra Paraschiv put_page(ne_mem_region->pages[i]);
10537dc9d430SAndra Paraschiv free_mem_region:
1054f6bdc0aaSLongpeng kfree(phys_contig_mem_regions.regions);
10557dc9d430SAndra Paraschiv kfree(ne_mem_region->pages);
10567dc9d430SAndra Paraschiv kfree(ne_mem_region);
10577dc9d430SAndra Paraschiv
10587dc9d430SAndra Paraschiv return rc;
10597dc9d430SAndra Paraschiv }
10607dc9d430SAndra Paraschiv
10617dc9d430SAndra Paraschiv /**
1062111c775aSAndra Paraschiv * ne_start_enclave_ioctl() - Trigger enclave start after the enclave resources,
1063111c775aSAndra Paraschiv * such as memory and CPU, have been set.
1064111c775aSAndra Paraschiv * @ne_enclave : Private data associated with the current enclave.
1065111c775aSAndra Paraschiv * @enclave_start_info : Enclave info that includes enclave cid and flags.
1066111c775aSAndra Paraschiv *
1067111c775aSAndra Paraschiv * Context: Process context. This function is called with the ne_enclave mutex held.
1068111c775aSAndra Paraschiv * Return:
1069111c775aSAndra Paraschiv * * 0 on success.
1070111c775aSAndra Paraschiv * * Negative return value on failure.
1071111c775aSAndra Paraschiv */
ne_start_enclave_ioctl(struct ne_enclave * ne_enclave,struct ne_enclave_start_info * enclave_start_info)1072111c775aSAndra Paraschiv static int ne_start_enclave_ioctl(struct ne_enclave *ne_enclave,
1073111c775aSAndra Paraschiv struct ne_enclave_start_info *enclave_start_info)
1074111c775aSAndra Paraschiv {
1075111c775aSAndra Paraschiv struct ne_pci_dev_cmd_reply cmd_reply = {};
1076111c775aSAndra Paraschiv unsigned int cpu = 0;
1077111c775aSAndra Paraschiv struct enclave_start_req enclave_start_req = {};
1078111c775aSAndra Paraschiv unsigned int i = 0;
1079111c775aSAndra Paraschiv struct pci_dev *pdev = ne_devs.ne_pci_dev->pdev;
1080111c775aSAndra Paraschiv int rc = -EINVAL;
1081111c775aSAndra Paraschiv
1082111c775aSAndra Paraschiv if (!ne_enclave->nr_mem_regions) {
1083111c775aSAndra Paraschiv dev_err_ratelimited(ne_misc_dev.this_device,
1084111c775aSAndra Paraschiv "Enclave has no mem regions\n");
1085111c775aSAndra Paraschiv
1086111c775aSAndra Paraschiv return -NE_ERR_NO_MEM_REGIONS_ADDED;
1087111c775aSAndra Paraschiv }
1088111c775aSAndra Paraschiv
1089111c775aSAndra Paraschiv if (ne_enclave->mem_size < NE_MIN_ENCLAVE_MEM_SIZE) {
1090111c775aSAndra Paraschiv dev_err_ratelimited(ne_misc_dev.this_device,
1091111c775aSAndra Paraschiv "Enclave memory is less than %ld\n",
1092111c775aSAndra Paraschiv NE_MIN_ENCLAVE_MEM_SIZE);
1093111c775aSAndra Paraschiv
1094111c775aSAndra Paraschiv return -NE_ERR_ENCLAVE_MEM_MIN_SIZE;
1095111c775aSAndra Paraschiv }
1096111c775aSAndra Paraschiv
1097111c775aSAndra Paraschiv if (!ne_enclave->nr_vcpus) {
1098111c775aSAndra Paraschiv dev_err_ratelimited(ne_misc_dev.this_device,
1099111c775aSAndra Paraschiv "Enclave has no vCPUs\n");
1100111c775aSAndra Paraschiv
1101111c775aSAndra Paraschiv return -NE_ERR_NO_VCPUS_ADDED;
1102111c775aSAndra Paraschiv }
1103111c775aSAndra Paraschiv
1104111c775aSAndra Paraschiv for (i = 0; i < ne_enclave->nr_parent_vm_cores; i++)
1105111c775aSAndra Paraschiv for_each_cpu(cpu, ne_enclave->threads_per_core[i])
1106111c775aSAndra Paraschiv if (!cpumask_test_cpu(cpu, ne_enclave->vcpu_ids)) {
1107111c775aSAndra Paraschiv dev_err_ratelimited(ne_misc_dev.this_device,
1108111c775aSAndra Paraschiv "Full CPU cores not used\n");
1109111c775aSAndra Paraschiv
1110111c775aSAndra Paraschiv return -NE_ERR_FULL_CORES_NOT_USED;
1111111c775aSAndra Paraschiv }
1112111c775aSAndra Paraschiv
1113111c775aSAndra Paraschiv enclave_start_req.enclave_cid = enclave_start_info->enclave_cid;
1114111c775aSAndra Paraschiv enclave_start_req.flags = enclave_start_info->flags;
1115111c775aSAndra Paraschiv enclave_start_req.slot_uid = ne_enclave->slot_uid;
1116111c775aSAndra Paraschiv
1117111c775aSAndra Paraschiv rc = ne_do_request(pdev, ENCLAVE_START,
1118111c775aSAndra Paraschiv &enclave_start_req, sizeof(enclave_start_req),
1119111c775aSAndra Paraschiv &cmd_reply, sizeof(cmd_reply));
1120111c775aSAndra Paraschiv if (rc < 0) {
1121111c775aSAndra Paraschiv dev_err_ratelimited(ne_misc_dev.this_device,
1122111c775aSAndra Paraschiv "Error in enclave start [rc=%d]\n", rc);
1123111c775aSAndra Paraschiv
1124111c775aSAndra Paraschiv return rc;
1125111c775aSAndra Paraschiv }
1126111c775aSAndra Paraschiv
1127111c775aSAndra Paraschiv ne_enclave->state = NE_STATE_RUNNING;
1128111c775aSAndra Paraschiv
1129111c775aSAndra Paraschiv enclave_start_info->enclave_cid = cmd_reply.enclave_cid;
1130111c775aSAndra Paraschiv
1131111c775aSAndra Paraschiv return 0;
1132111c775aSAndra Paraschiv }
1133111c775aSAndra Paraschiv
1134111c775aSAndra Paraschiv /**
1135ff8a4d3eSAndra Paraschiv * ne_enclave_ioctl() - Ioctl function provided by the enclave file.
1136ff8a4d3eSAndra Paraschiv * @file: File associated with this ioctl function.
1137ff8a4d3eSAndra Paraschiv * @cmd: The command that is set for the ioctl call.
1138ff8a4d3eSAndra Paraschiv * @arg: The argument that is provided for the ioctl call.
1139ff8a4d3eSAndra Paraschiv *
1140ff8a4d3eSAndra Paraschiv * Context: Process context.
1141ff8a4d3eSAndra Paraschiv * Return:
1142ff8a4d3eSAndra Paraschiv * * 0 on success.
1143ff8a4d3eSAndra Paraschiv * * Negative return value on failure.
1144ff8a4d3eSAndra Paraschiv */
ne_enclave_ioctl(struct file * file,unsigned int cmd,unsigned long arg)1145ff8a4d3eSAndra Paraschiv static long ne_enclave_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1146ff8a4d3eSAndra Paraschiv {
1147ff8a4d3eSAndra Paraschiv struct ne_enclave *ne_enclave = file->private_data;
1148ff8a4d3eSAndra Paraschiv
1149ff8a4d3eSAndra Paraschiv switch (cmd) {
1150ff8a4d3eSAndra Paraschiv case NE_ADD_VCPU: {
1151ff8a4d3eSAndra Paraschiv int rc = -EINVAL;
1152ff8a4d3eSAndra Paraschiv u32 vcpu_id = 0;
1153ff8a4d3eSAndra Paraschiv
1154ff8a4d3eSAndra Paraschiv if (copy_from_user(&vcpu_id, (void __user *)arg, sizeof(vcpu_id)))
1155ff8a4d3eSAndra Paraschiv return -EFAULT;
1156ff8a4d3eSAndra Paraschiv
1157ff8a4d3eSAndra Paraschiv mutex_lock(&ne_enclave->enclave_info_mutex);
1158ff8a4d3eSAndra Paraschiv
1159ff8a4d3eSAndra Paraschiv if (ne_enclave->state != NE_STATE_INIT) {
1160ff8a4d3eSAndra Paraschiv dev_err_ratelimited(ne_misc_dev.this_device,
1161ff8a4d3eSAndra Paraschiv "Enclave is not in init state\n");
1162ff8a4d3eSAndra Paraschiv
1163ff8a4d3eSAndra Paraschiv mutex_unlock(&ne_enclave->enclave_info_mutex);
1164ff8a4d3eSAndra Paraschiv
1165ff8a4d3eSAndra Paraschiv return -NE_ERR_NOT_IN_INIT_STATE;
1166ff8a4d3eSAndra Paraschiv }
1167ff8a4d3eSAndra Paraschiv
1168ff8a4d3eSAndra Paraschiv if (vcpu_id >= (ne_enclave->nr_parent_vm_cores *
1169ff8a4d3eSAndra Paraschiv ne_enclave->nr_threads_per_core)) {
1170ff8a4d3eSAndra Paraschiv dev_err_ratelimited(ne_misc_dev.this_device,
1171ff8a4d3eSAndra Paraschiv "vCPU id higher than max CPU id\n");
1172ff8a4d3eSAndra Paraschiv
1173ff8a4d3eSAndra Paraschiv mutex_unlock(&ne_enclave->enclave_info_mutex);
1174ff8a4d3eSAndra Paraschiv
1175ff8a4d3eSAndra Paraschiv return -NE_ERR_INVALID_VCPU;
1176ff8a4d3eSAndra Paraschiv }
1177ff8a4d3eSAndra Paraschiv
1178ff8a4d3eSAndra Paraschiv if (!vcpu_id) {
1179ff8a4d3eSAndra Paraschiv /* Use the CPU pool for choosing a CPU for the enclave. */
1180ff8a4d3eSAndra Paraschiv rc = ne_get_cpu_from_cpu_pool(ne_enclave, &vcpu_id);
1181ff8a4d3eSAndra Paraschiv if (rc < 0) {
1182ff8a4d3eSAndra Paraschiv dev_err_ratelimited(ne_misc_dev.this_device,
1183ff8a4d3eSAndra Paraschiv "Error in get CPU from pool [rc=%d]\n",
1184ff8a4d3eSAndra Paraschiv rc);
1185ff8a4d3eSAndra Paraschiv
1186ff8a4d3eSAndra Paraschiv mutex_unlock(&ne_enclave->enclave_info_mutex);
1187ff8a4d3eSAndra Paraschiv
1188ff8a4d3eSAndra Paraschiv return rc;
1189ff8a4d3eSAndra Paraschiv }
1190ff8a4d3eSAndra Paraschiv } else {
1191ff8a4d3eSAndra Paraschiv /* Check if the provided vCPU is available in the NE CPU pool. */
1192ff8a4d3eSAndra Paraschiv rc = ne_check_cpu_in_cpu_pool(ne_enclave, vcpu_id);
1193ff8a4d3eSAndra Paraschiv if (rc < 0) {
1194ff8a4d3eSAndra Paraschiv dev_err_ratelimited(ne_misc_dev.this_device,
1195ff8a4d3eSAndra Paraschiv "Error in check CPU %d in pool [rc=%d]\n",
1196ff8a4d3eSAndra Paraschiv vcpu_id, rc);
1197ff8a4d3eSAndra Paraschiv
1198ff8a4d3eSAndra Paraschiv mutex_unlock(&ne_enclave->enclave_info_mutex);
1199ff8a4d3eSAndra Paraschiv
1200ff8a4d3eSAndra Paraschiv return rc;
1201ff8a4d3eSAndra Paraschiv }
1202ff8a4d3eSAndra Paraschiv }
1203ff8a4d3eSAndra Paraschiv
1204ff8a4d3eSAndra Paraschiv rc = ne_add_vcpu_ioctl(ne_enclave, vcpu_id);
1205ff8a4d3eSAndra Paraschiv if (rc < 0) {
1206ff8a4d3eSAndra Paraschiv mutex_unlock(&ne_enclave->enclave_info_mutex);
1207ff8a4d3eSAndra Paraschiv
1208ff8a4d3eSAndra Paraschiv return rc;
1209ff8a4d3eSAndra Paraschiv }
1210ff8a4d3eSAndra Paraschiv
1211ff8a4d3eSAndra Paraschiv mutex_unlock(&ne_enclave->enclave_info_mutex);
1212ff8a4d3eSAndra Paraschiv
1213ff8a4d3eSAndra Paraschiv if (copy_to_user((void __user *)arg, &vcpu_id, sizeof(vcpu_id)))
1214ff8a4d3eSAndra Paraschiv return -EFAULT;
1215ff8a4d3eSAndra Paraschiv
1216ff8a4d3eSAndra Paraschiv return 0;
1217ff8a4d3eSAndra Paraschiv }
1218ff8a4d3eSAndra Paraschiv
1219988b7a47SAndra Paraschiv case NE_GET_IMAGE_LOAD_INFO: {
1220988b7a47SAndra Paraschiv struct ne_image_load_info image_load_info = {};
1221988b7a47SAndra Paraschiv
1222988b7a47SAndra Paraschiv if (copy_from_user(&image_load_info, (void __user *)arg, sizeof(image_load_info)))
1223988b7a47SAndra Paraschiv return -EFAULT;
1224988b7a47SAndra Paraschiv
1225988b7a47SAndra Paraschiv mutex_lock(&ne_enclave->enclave_info_mutex);
1226988b7a47SAndra Paraschiv
1227988b7a47SAndra Paraschiv if (ne_enclave->state != NE_STATE_INIT) {
1228988b7a47SAndra Paraschiv dev_err_ratelimited(ne_misc_dev.this_device,
1229988b7a47SAndra Paraschiv "Enclave is not in init state\n");
1230988b7a47SAndra Paraschiv
1231988b7a47SAndra Paraschiv mutex_unlock(&ne_enclave->enclave_info_mutex);
1232988b7a47SAndra Paraschiv
1233988b7a47SAndra Paraschiv return -NE_ERR_NOT_IN_INIT_STATE;
1234988b7a47SAndra Paraschiv }
1235988b7a47SAndra Paraschiv
1236988b7a47SAndra Paraschiv mutex_unlock(&ne_enclave->enclave_info_mutex);
1237988b7a47SAndra Paraschiv
1238988b7a47SAndra Paraschiv if (!image_load_info.flags ||
1239988b7a47SAndra Paraschiv image_load_info.flags >= NE_IMAGE_LOAD_MAX_FLAG_VAL) {
1240988b7a47SAndra Paraschiv dev_err_ratelimited(ne_misc_dev.this_device,
1241988b7a47SAndra Paraschiv "Incorrect flag in enclave image load info\n");
1242988b7a47SAndra Paraschiv
1243988b7a47SAndra Paraschiv return -NE_ERR_INVALID_FLAG_VALUE;
1244988b7a47SAndra Paraschiv }
1245988b7a47SAndra Paraschiv
1246988b7a47SAndra Paraschiv if (image_load_info.flags == NE_EIF_IMAGE)
1247988b7a47SAndra Paraschiv image_load_info.memory_offset = NE_EIF_LOAD_OFFSET;
1248988b7a47SAndra Paraschiv
1249988b7a47SAndra Paraschiv if (copy_to_user((void __user *)arg, &image_load_info, sizeof(image_load_info)))
1250988b7a47SAndra Paraschiv return -EFAULT;
1251988b7a47SAndra Paraschiv
1252988b7a47SAndra Paraschiv return 0;
1253988b7a47SAndra Paraschiv }
1254988b7a47SAndra Paraschiv
12557dc9d430SAndra Paraschiv case NE_SET_USER_MEMORY_REGION: {
12567dc9d430SAndra Paraschiv struct ne_user_memory_region mem_region = {};
12577dc9d430SAndra Paraschiv int rc = -EINVAL;
12587dc9d430SAndra Paraschiv
12597dc9d430SAndra Paraschiv if (copy_from_user(&mem_region, (void __user *)arg, sizeof(mem_region)))
12607dc9d430SAndra Paraschiv return -EFAULT;
12617dc9d430SAndra Paraschiv
12627dc9d430SAndra Paraschiv if (mem_region.flags >= NE_MEMORY_REGION_MAX_FLAG_VAL) {
12637dc9d430SAndra Paraschiv dev_err_ratelimited(ne_misc_dev.this_device,
12647dc9d430SAndra Paraschiv "Incorrect flag for user memory region\n");
12657dc9d430SAndra Paraschiv
12667dc9d430SAndra Paraschiv return -NE_ERR_INVALID_FLAG_VALUE;
12677dc9d430SAndra Paraschiv }
12687dc9d430SAndra Paraschiv
12697dc9d430SAndra Paraschiv mutex_lock(&ne_enclave->enclave_info_mutex);
12707dc9d430SAndra Paraschiv
12717dc9d430SAndra Paraschiv if (ne_enclave->state != NE_STATE_INIT) {
12727dc9d430SAndra Paraschiv dev_err_ratelimited(ne_misc_dev.this_device,
12737dc9d430SAndra Paraschiv "Enclave is not in init state\n");
12747dc9d430SAndra Paraschiv
12757dc9d430SAndra Paraschiv mutex_unlock(&ne_enclave->enclave_info_mutex);
12767dc9d430SAndra Paraschiv
12777dc9d430SAndra Paraschiv return -NE_ERR_NOT_IN_INIT_STATE;
12787dc9d430SAndra Paraschiv }
12797dc9d430SAndra Paraschiv
12807dc9d430SAndra Paraschiv rc = ne_set_user_memory_region_ioctl(ne_enclave, mem_region);
12817dc9d430SAndra Paraschiv if (rc < 0) {
12827dc9d430SAndra Paraschiv mutex_unlock(&ne_enclave->enclave_info_mutex);
12837dc9d430SAndra Paraschiv
12847dc9d430SAndra Paraschiv return rc;
12857dc9d430SAndra Paraschiv }
12867dc9d430SAndra Paraschiv
12877dc9d430SAndra Paraschiv mutex_unlock(&ne_enclave->enclave_info_mutex);
12887dc9d430SAndra Paraschiv
12897dc9d430SAndra Paraschiv return 0;
12907dc9d430SAndra Paraschiv }
12917dc9d430SAndra Paraschiv
1292111c775aSAndra Paraschiv case NE_START_ENCLAVE: {
1293111c775aSAndra Paraschiv struct ne_enclave_start_info enclave_start_info = {};
1294111c775aSAndra Paraschiv int rc = -EINVAL;
1295111c775aSAndra Paraschiv
1296111c775aSAndra Paraschiv if (copy_from_user(&enclave_start_info, (void __user *)arg,
1297111c775aSAndra Paraschiv sizeof(enclave_start_info)))
1298111c775aSAndra Paraschiv return -EFAULT;
1299111c775aSAndra Paraschiv
1300111c775aSAndra Paraschiv if (enclave_start_info.flags >= NE_ENCLAVE_START_MAX_FLAG_VAL) {
1301111c775aSAndra Paraschiv dev_err_ratelimited(ne_misc_dev.this_device,
1302111c775aSAndra Paraschiv "Incorrect flag in enclave start info\n");
1303111c775aSAndra Paraschiv
1304111c775aSAndra Paraschiv return -NE_ERR_INVALID_FLAG_VALUE;
1305111c775aSAndra Paraschiv }
1306111c775aSAndra Paraschiv
1307111c775aSAndra Paraschiv /*
1308111c775aSAndra Paraschiv * Do not use well-known CIDs - 0, 1, 2 - for enclaves.
1309111c775aSAndra Paraschiv * VMADDR_CID_ANY = -1U
1310111c775aSAndra Paraschiv * VMADDR_CID_HYPERVISOR = 0
1311111c775aSAndra Paraschiv * VMADDR_CID_LOCAL = 1
1312111c775aSAndra Paraschiv * VMADDR_CID_HOST = 2
1313111c775aSAndra Paraschiv * Note: 0 is used as a placeholder to auto-generate an enclave CID.
1314111c775aSAndra Paraschiv * http://man7.org/linux/man-pages/man7/vsock.7.html
1315111c775aSAndra Paraschiv */
1316111c775aSAndra Paraschiv if (enclave_start_info.enclave_cid > 0 &&
1317111c775aSAndra Paraschiv enclave_start_info.enclave_cid <= VMADDR_CID_HOST) {
1318111c775aSAndra Paraschiv dev_err_ratelimited(ne_misc_dev.this_device,
1319111c775aSAndra Paraschiv "Well-known CID value, not to be used for enclaves\n");
1320111c775aSAndra Paraschiv
1321111c775aSAndra Paraschiv return -NE_ERR_INVALID_ENCLAVE_CID;
1322111c775aSAndra Paraschiv }
1323111c775aSAndra Paraschiv
1324111c775aSAndra Paraschiv if (enclave_start_info.enclave_cid == U32_MAX) {
1325111c775aSAndra Paraschiv dev_err_ratelimited(ne_misc_dev.this_device,
1326111c775aSAndra Paraschiv "Well-known CID value, not to be used for enclaves\n");
1327111c775aSAndra Paraschiv
1328111c775aSAndra Paraschiv return -NE_ERR_INVALID_ENCLAVE_CID;
1329111c775aSAndra Paraschiv }
1330111c775aSAndra Paraschiv
1331111c775aSAndra Paraschiv /*
1332111c775aSAndra Paraschiv * Do not use the CID of the primary / parent VM for enclaves.
1333111c775aSAndra Paraschiv */
1334111c775aSAndra Paraschiv if (enclave_start_info.enclave_cid == NE_PARENT_VM_CID) {
1335111c775aSAndra Paraschiv dev_err_ratelimited(ne_misc_dev.this_device,
1336111c775aSAndra Paraschiv "CID of the parent VM, not to be used for enclaves\n");
1337111c775aSAndra Paraschiv
1338111c775aSAndra Paraschiv return -NE_ERR_INVALID_ENCLAVE_CID;
1339111c775aSAndra Paraschiv }
1340111c775aSAndra Paraschiv
1341111c775aSAndra Paraschiv /* 64-bit CIDs are not yet supported for the vsock device. */
1342111c775aSAndra Paraschiv if (enclave_start_info.enclave_cid > U32_MAX) {
1343111c775aSAndra Paraschiv dev_err_ratelimited(ne_misc_dev.this_device,
1344111c775aSAndra Paraschiv "64-bit CIDs not yet supported for the vsock device\n");
1345111c775aSAndra Paraschiv
1346111c775aSAndra Paraschiv return -NE_ERR_INVALID_ENCLAVE_CID;
1347111c775aSAndra Paraschiv }
1348111c775aSAndra Paraschiv
1349111c775aSAndra Paraschiv mutex_lock(&ne_enclave->enclave_info_mutex);
1350111c775aSAndra Paraschiv
1351111c775aSAndra Paraschiv if (ne_enclave->state != NE_STATE_INIT) {
1352111c775aSAndra Paraschiv dev_err_ratelimited(ne_misc_dev.this_device,
1353111c775aSAndra Paraschiv "Enclave is not in init state\n");
1354111c775aSAndra Paraschiv
1355111c775aSAndra Paraschiv mutex_unlock(&ne_enclave->enclave_info_mutex);
1356111c775aSAndra Paraschiv
1357111c775aSAndra Paraschiv return -NE_ERR_NOT_IN_INIT_STATE;
1358111c775aSAndra Paraschiv }
1359111c775aSAndra Paraschiv
1360111c775aSAndra Paraschiv rc = ne_start_enclave_ioctl(ne_enclave, &enclave_start_info);
1361111c775aSAndra Paraschiv if (rc < 0) {
1362111c775aSAndra Paraschiv mutex_unlock(&ne_enclave->enclave_info_mutex);
1363111c775aSAndra Paraschiv
1364111c775aSAndra Paraschiv return rc;
1365111c775aSAndra Paraschiv }
1366111c775aSAndra Paraschiv
1367111c775aSAndra Paraschiv mutex_unlock(&ne_enclave->enclave_info_mutex);
1368111c775aSAndra Paraschiv
1369111c775aSAndra Paraschiv if (copy_to_user((void __user *)arg, &enclave_start_info,
1370111c775aSAndra Paraschiv sizeof(enclave_start_info)))
1371111c775aSAndra Paraschiv return -EFAULT;
1372111c775aSAndra Paraschiv
1373111c775aSAndra Paraschiv return 0;
1374111c775aSAndra Paraschiv }
1375111c775aSAndra Paraschiv
1376ff8a4d3eSAndra Paraschiv default:
1377ff8a4d3eSAndra Paraschiv return -ENOTTY;
1378ff8a4d3eSAndra Paraschiv }
1379ff8a4d3eSAndra Paraschiv
1380ff8a4d3eSAndra Paraschiv return 0;
1381ff8a4d3eSAndra Paraschiv }
1382ff8a4d3eSAndra Paraschiv
1383ff8a4d3eSAndra Paraschiv /**
13849c8eb50fSAndra Paraschiv * ne_enclave_remove_all_mem_region_entries() - Remove all memory region entries
13859c8eb50fSAndra Paraschiv * from the enclave data structure.
13869c8eb50fSAndra Paraschiv * @ne_enclave : Private data associated with the current enclave.
13879c8eb50fSAndra Paraschiv *
13889c8eb50fSAndra Paraschiv * Context: Process context. This function is called with the ne_enclave mutex held.
13899c8eb50fSAndra Paraschiv */
ne_enclave_remove_all_mem_region_entries(struct ne_enclave * ne_enclave)13909c8eb50fSAndra Paraschiv static void ne_enclave_remove_all_mem_region_entries(struct ne_enclave *ne_enclave)
13919c8eb50fSAndra Paraschiv {
13929c8eb50fSAndra Paraschiv unsigned long i = 0;
13939c8eb50fSAndra Paraschiv struct ne_mem_region *ne_mem_region = NULL;
13949c8eb50fSAndra Paraschiv struct ne_mem_region *ne_mem_region_tmp = NULL;
13959c8eb50fSAndra Paraschiv
13969c8eb50fSAndra Paraschiv list_for_each_entry_safe(ne_mem_region, ne_mem_region_tmp,
13979c8eb50fSAndra Paraschiv &ne_enclave->mem_regions_list,
13989c8eb50fSAndra Paraschiv mem_region_list_entry) {
13999c8eb50fSAndra Paraschiv list_del(&ne_mem_region->mem_region_list_entry);
14009c8eb50fSAndra Paraschiv
14019c8eb50fSAndra Paraschiv for (i = 0; i < ne_mem_region->nr_pages; i++)
14029c8eb50fSAndra Paraschiv put_page(ne_mem_region->pages[i]);
14039c8eb50fSAndra Paraschiv
14049c8eb50fSAndra Paraschiv kfree(ne_mem_region->pages);
14059c8eb50fSAndra Paraschiv
14069c8eb50fSAndra Paraschiv kfree(ne_mem_region);
14079c8eb50fSAndra Paraschiv }
14089c8eb50fSAndra Paraschiv }
14099c8eb50fSAndra Paraschiv
14109c8eb50fSAndra Paraschiv /**
14119c8eb50fSAndra Paraschiv * ne_enclave_remove_all_vcpu_id_entries() - Remove all vCPU id entries from
14129c8eb50fSAndra Paraschiv * the enclave data structure.
14139c8eb50fSAndra Paraschiv * @ne_enclave : Private data associated with the current enclave.
14149c8eb50fSAndra Paraschiv *
14159c8eb50fSAndra Paraschiv * Context: Process context. This function is called with the ne_enclave mutex held.
14169c8eb50fSAndra Paraschiv */
ne_enclave_remove_all_vcpu_id_entries(struct ne_enclave * ne_enclave)14179c8eb50fSAndra Paraschiv static void ne_enclave_remove_all_vcpu_id_entries(struct ne_enclave *ne_enclave)
14189c8eb50fSAndra Paraschiv {
14199c8eb50fSAndra Paraschiv unsigned int cpu = 0;
14209c8eb50fSAndra Paraschiv unsigned int i = 0;
14219c8eb50fSAndra Paraschiv
14229c8eb50fSAndra Paraschiv mutex_lock(&ne_cpu_pool.mutex);
14239c8eb50fSAndra Paraschiv
14249c8eb50fSAndra Paraschiv for (i = 0; i < ne_enclave->nr_parent_vm_cores; i++) {
14259c8eb50fSAndra Paraschiv for_each_cpu(cpu, ne_enclave->threads_per_core[i])
14269c8eb50fSAndra Paraschiv /* Update the available NE CPU pool. */
14279c8eb50fSAndra Paraschiv cpumask_set_cpu(cpu, ne_cpu_pool.avail_threads_per_core[i]);
14289c8eb50fSAndra Paraschiv
14299c8eb50fSAndra Paraschiv free_cpumask_var(ne_enclave->threads_per_core[i]);
14309c8eb50fSAndra Paraschiv }
14319c8eb50fSAndra Paraschiv
14329c8eb50fSAndra Paraschiv mutex_unlock(&ne_cpu_pool.mutex);
14339c8eb50fSAndra Paraschiv
14349c8eb50fSAndra Paraschiv kfree(ne_enclave->threads_per_core);
14359c8eb50fSAndra Paraschiv
14369c8eb50fSAndra Paraschiv free_cpumask_var(ne_enclave->vcpu_ids);
14379c8eb50fSAndra Paraschiv }
14389c8eb50fSAndra Paraschiv
14399c8eb50fSAndra Paraschiv /**
14409c8eb50fSAndra Paraschiv * ne_pci_dev_remove_enclave_entry() - Remove the enclave entry from the data
14419c8eb50fSAndra Paraschiv * structure that is part of the NE PCI
14429c8eb50fSAndra Paraschiv * device private data.
14439c8eb50fSAndra Paraschiv * @ne_enclave : Private data associated with the current enclave.
14449c8eb50fSAndra Paraschiv * @ne_pci_dev : Private data associated with the PCI device.
14459c8eb50fSAndra Paraschiv *
14469c8eb50fSAndra Paraschiv * Context: Process context. This function is called with the ne_pci_dev enclave
14479c8eb50fSAndra Paraschiv * mutex held.
14489c8eb50fSAndra Paraschiv */
ne_pci_dev_remove_enclave_entry(struct ne_enclave * ne_enclave,struct ne_pci_dev * ne_pci_dev)14499c8eb50fSAndra Paraschiv static void ne_pci_dev_remove_enclave_entry(struct ne_enclave *ne_enclave,
14509c8eb50fSAndra Paraschiv struct ne_pci_dev *ne_pci_dev)
14519c8eb50fSAndra Paraschiv {
14529c8eb50fSAndra Paraschiv struct ne_enclave *ne_enclave_entry = NULL;
14539c8eb50fSAndra Paraschiv struct ne_enclave *ne_enclave_entry_tmp = NULL;
14549c8eb50fSAndra Paraschiv
14559c8eb50fSAndra Paraschiv list_for_each_entry_safe(ne_enclave_entry, ne_enclave_entry_tmp,
14569c8eb50fSAndra Paraschiv &ne_pci_dev->enclaves_list, enclave_list_entry) {
14579c8eb50fSAndra Paraschiv if (ne_enclave_entry->slot_uid == ne_enclave->slot_uid) {
14589c8eb50fSAndra Paraschiv list_del(&ne_enclave_entry->enclave_list_entry);
14599c8eb50fSAndra Paraschiv
14609c8eb50fSAndra Paraschiv break;
14619c8eb50fSAndra Paraschiv }
14629c8eb50fSAndra Paraschiv }
14639c8eb50fSAndra Paraschiv }
14649c8eb50fSAndra Paraschiv
14659c8eb50fSAndra Paraschiv /**
14669c8eb50fSAndra Paraschiv * ne_enclave_release() - Release function provided by the enclave file.
14679c8eb50fSAndra Paraschiv * @inode: Inode associated with this file release function.
14689c8eb50fSAndra Paraschiv * @file: File associated with this release function.
14699c8eb50fSAndra Paraschiv *
14709c8eb50fSAndra Paraschiv * Context: Process context.
14719c8eb50fSAndra Paraschiv * Return:
14729c8eb50fSAndra Paraschiv * * 0 on success.
14739c8eb50fSAndra Paraschiv * * Negative return value on failure.
14749c8eb50fSAndra Paraschiv */
ne_enclave_release(struct inode * inode,struct file * file)14759c8eb50fSAndra Paraschiv static int ne_enclave_release(struct inode *inode, struct file *file)
14769c8eb50fSAndra Paraschiv {
14779c8eb50fSAndra Paraschiv struct ne_pci_dev_cmd_reply cmd_reply = {};
14789c8eb50fSAndra Paraschiv struct enclave_stop_req enclave_stop_request = {};
14799c8eb50fSAndra Paraschiv struct ne_enclave *ne_enclave = file->private_data;
14809c8eb50fSAndra Paraschiv struct ne_pci_dev *ne_pci_dev = ne_devs.ne_pci_dev;
14819c8eb50fSAndra Paraschiv struct pci_dev *pdev = ne_pci_dev->pdev;
14829c8eb50fSAndra Paraschiv int rc = -EINVAL;
14839c8eb50fSAndra Paraschiv struct slot_free_req slot_free_req = {};
14849c8eb50fSAndra Paraschiv
14859c8eb50fSAndra Paraschiv if (!ne_enclave)
14869c8eb50fSAndra Paraschiv return 0;
14879c8eb50fSAndra Paraschiv
14889c8eb50fSAndra Paraschiv /*
14899c8eb50fSAndra Paraschiv * Early exit in case there is an error in the enclave creation logic
14909c8eb50fSAndra Paraschiv * and fput() is called on the cleanup path.
14919c8eb50fSAndra Paraschiv */
14929c8eb50fSAndra Paraschiv if (!ne_enclave->slot_uid)
14939c8eb50fSAndra Paraschiv return 0;
14949c8eb50fSAndra Paraschiv
14959c8eb50fSAndra Paraschiv /*
14969c8eb50fSAndra Paraschiv * Acquire the enclave list mutex before the enclave mutex
14979c8eb50fSAndra Paraschiv * in order to avoid deadlocks with @ref ne_event_work_handler.
14989c8eb50fSAndra Paraschiv */
14999c8eb50fSAndra Paraschiv mutex_lock(&ne_pci_dev->enclaves_list_mutex);
15009c8eb50fSAndra Paraschiv mutex_lock(&ne_enclave->enclave_info_mutex);
15019c8eb50fSAndra Paraschiv
15029c8eb50fSAndra Paraschiv if (ne_enclave->state != NE_STATE_INIT && ne_enclave->state != NE_STATE_STOPPED) {
15039c8eb50fSAndra Paraschiv enclave_stop_request.slot_uid = ne_enclave->slot_uid;
15049c8eb50fSAndra Paraschiv
15059c8eb50fSAndra Paraschiv rc = ne_do_request(pdev, ENCLAVE_STOP,
15069c8eb50fSAndra Paraschiv &enclave_stop_request, sizeof(enclave_stop_request),
15079c8eb50fSAndra Paraschiv &cmd_reply, sizeof(cmd_reply));
15089c8eb50fSAndra Paraschiv if (rc < 0) {
15099c8eb50fSAndra Paraschiv dev_err_ratelimited(ne_misc_dev.this_device,
15109c8eb50fSAndra Paraschiv "Error in enclave stop [rc=%d]\n", rc);
15119c8eb50fSAndra Paraschiv
15129c8eb50fSAndra Paraschiv goto unlock_mutex;
15139c8eb50fSAndra Paraschiv }
15149c8eb50fSAndra Paraschiv
15159c8eb50fSAndra Paraschiv memset(&cmd_reply, 0, sizeof(cmd_reply));
15169c8eb50fSAndra Paraschiv }
15179c8eb50fSAndra Paraschiv
15189c8eb50fSAndra Paraschiv slot_free_req.slot_uid = ne_enclave->slot_uid;
15199c8eb50fSAndra Paraschiv
15209c8eb50fSAndra Paraschiv rc = ne_do_request(pdev, SLOT_FREE,
15219c8eb50fSAndra Paraschiv &slot_free_req, sizeof(slot_free_req),
15229c8eb50fSAndra Paraschiv &cmd_reply, sizeof(cmd_reply));
15239c8eb50fSAndra Paraschiv if (rc < 0) {
15249c8eb50fSAndra Paraschiv dev_err_ratelimited(ne_misc_dev.this_device,
15259c8eb50fSAndra Paraschiv "Error in slot free [rc=%d]\n", rc);
15269c8eb50fSAndra Paraschiv
15279c8eb50fSAndra Paraschiv goto unlock_mutex;
15289c8eb50fSAndra Paraschiv }
15299c8eb50fSAndra Paraschiv
15309c8eb50fSAndra Paraschiv ne_pci_dev_remove_enclave_entry(ne_enclave, ne_pci_dev);
15319c8eb50fSAndra Paraschiv ne_enclave_remove_all_mem_region_entries(ne_enclave);
15329c8eb50fSAndra Paraschiv ne_enclave_remove_all_vcpu_id_entries(ne_enclave);
15339c8eb50fSAndra Paraschiv
15349c8eb50fSAndra Paraschiv mutex_unlock(&ne_enclave->enclave_info_mutex);
15359c8eb50fSAndra Paraschiv mutex_unlock(&ne_pci_dev->enclaves_list_mutex);
15369c8eb50fSAndra Paraschiv
15379c8eb50fSAndra Paraschiv kfree(ne_enclave);
15389c8eb50fSAndra Paraschiv
15399c8eb50fSAndra Paraschiv return 0;
15409c8eb50fSAndra Paraschiv
15419c8eb50fSAndra Paraschiv unlock_mutex:
15429c8eb50fSAndra Paraschiv mutex_unlock(&ne_enclave->enclave_info_mutex);
15439c8eb50fSAndra Paraschiv mutex_unlock(&ne_pci_dev->enclaves_list_mutex);
15449c8eb50fSAndra Paraschiv
15459c8eb50fSAndra Paraschiv return rc;
15469c8eb50fSAndra Paraschiv }
15479c8eb50fSAndra Paraschiv
15489c8eb50fSAndra Paraschiv /**
154938907e12SAndra Paraschiv * ne_enclave_poll() - Poll functionality used for enclave out-of-band events.
155038907e12SAndra Paraschiv * @file: File associated with this poll function.
155138907e12SAndra Paraschiv * @wait: Poll table data structure.
155238907e12SAndra Paraschiv *
155338907e12SAndra Paraschiv * Context: Process context.
155438907e12SAndra Paraschiv * Return:
155538907e12SAndra Paraschiv * * Poll mask.
155638907e12SAndra Paraschiv */
ne_enclave_poll(struct file * file,poll_table * wait)155738907e12SAndra Paraschiv static __poll_t ne_enclave_poll(struct file *file, poll_table *wait)
155838907e12SAndra Paraschiv {
155938907e12SAndra Paraschiv __poll_t mask = 0;
156038907e12SAndra Paraschiv struct ne_enclave *ne_enclave = file->private_data;
156138907e12SAndra Paraschiv
156238907e12SAndra Paraschiv poll_wait(file, &ne_enclave->eventq, wait);
156338907e12SAndra Paraschiv
1564d9109fe0SAndra Paraschiv if (ne_enclave->has_event)
1565d9109fe0SAndra Paraschiv mask |= EPOLLHUP;
156638907e12SAndra Paraschiv
156738907e12SAndra Paraschiv return mask;
156838907e12SAndra Paraschiv }
156938907e12SAndra Paraschiv
157038907e12SAndra Paraschiv static const struct file_operations ne_enclave_fops = {
157138907e12SAndra Paraschiv .owner = THIS_MODULE,
157238907e12SAndra Paraschiv .llseek = noop_llseek,
157338907e12SAndra Paraschiv .poll = ne_enclave_poll,
1574ff8a4d3eSAndra Paraschiv .unlocked_ioctl = ne_enclave_ioctl,
15759c8eb50fSAndra Paraschiv .release = ne_enclave_release,
157638907e12SAndra Paraschiv };
157738907e12SAndra Paraschiv
157838907e12SAndra Paraschiv /**
157938907e12SAndra Paraschiv * ne_create_vm_ioctl() - Alloc slot to be associated with an enclave. Create
158038907e12SAndra Paraschiv * enclave file descriptor to be further used for enclave
158138907e12SAndra Paraschiv * resources handling e.g. memory regions and CPUs.
158238907e12SAndra Paraschiv * @ne_pci_dev : Private data associated with the PCI device.
1583f1ce3986SMathias Krause * @slot_uid: User pointer to store the generated unique slot id
1584f1ce3986SMathias Krause * associated with an enclave to.
158538907e12SAndra Paraschiv *
158638907e12SAndra Paraschiv * Context: Process context. This function is called with the ne_pci_dev enclave
158738907e12SAndra Paraschiv * mutex held.
158838907e12SAndra Paraschiv * Return:
158938907e12SAndra Paraschiv * * Enclave fd on success.
159038907e12SAndra Paraschiv * * Negative return value on failure.
159138907e12SAndra Paraschiv */
ne_create_vm_ioctl(struct ne_pci_dev * ne_pci_dev,u64 __user * slot_uid)1592f1ce3986SMathias Krause static int ne_create_vm_ioctl(struct ne_pci_dev *ne_pci_dev, u64 __user *slot_uid)
159338907e12SAndra Paraschiv {
159438907e12SAndra Paraschiv struct ne_pci_dev_cmd_reply cmd_reply = {};
159538907e12SAndra Paraschiv int enclave_fd = -1;
159638907e12SAndra Paraschiv struct file *enclave_file = NULL;
159738907e12SAndra Paraschiv unsigned int i = 0;
159838907e12SAndra Paraschiv struct ne_enclave *ne_enclave = NULL;
159938907e12SAndra Paraschiv struct pci_dev *pdev = ne_pci_dev->pdev;
160038907e12SAndra Paraschiv int rc = -EINVAL;
160138907e12SAndra Paraschiv struct slot_alloc_req slot_alloc_req = {};
160238907e12SAndra Paraschiv
160338907e12SAndra Paraschiv mutex_lock(&ne_cpu_pool.mutex);
160438907e12SAndra Paraschiv
160538907e12SAndra Paraschiv for (i = 0; i < ne_cpu_pool.nr_parent_vm_cores; i++)
160638907e12SAndra Paraschiv if (!cpumask_empty(ne_cpu_pool.avail_threads_per_core[i]))
160738907e12SAndra Paraschiv break;
160838907e12SAndra Paraschiv
160938907e12SAndra Paraschiv if (i == ne_cpu_pool.nr_parent_vm_cores) {
161038907e12SAndra Paraschiv dev_err_ratelimited(ne_misc_dev.this_device,
161138907e12SAndra Paraschiv "No CPUs available in CPU pool\n");
161238907e12SAndra Paraschiv
161338907e12SAndra Paraschiv mutex_unlock(&ne_cpu_pool.mutex);
161438907e12SAndra Paraschiv
161538907e12SAndra Paraschiv return -NE_ERR_NO_CPUS_AVAIL_IN_POOL;
161638907e12SAndra Paraschiv }
161738907e12SAndra Paraschiv
161838907e12SAndra Paraschiv mutex_unlock(&ne_cpu_pool.mutex);
161938907e12SAndra Paraschiv
162038907e12SAndra Paraschiv ne_enclave = kzalloc(sizeof(*ne_enclave), GFP_KERNEL);
162138907e12SAndra Paraschiv if (!ne_enclave)
162238907e12SAndra Paraschiv return -ENOMEM;
162338907e12SAndra Paraschiv
162438907e12SAndra Paraschiv mutex_lock(&ne_cpu_pool.mutex);
162538907e12SAndra Paraschiv
162638907e12SAndra Paraschiv ne_enclave->nr_parent_vm_cores = ne_cpu_pool.nr_parent_vm_cores;
162738907e12SAndra Paraschiv ne_enclave->nr_threads_per_core = ne_cpu_pool.nr_threads_per_core;
162838907e12SAndra Paraschiv ne_enclave->numa_node = ne_cpu_pool.numa_node;
162938907e12SAndra Paraschiv
163038907e12SAndra Paraschiv mutex_unlock(&ne_cpu_pool.mutex);
163138907e12SAndra Paraschiv
163238907e12SAndra Paraschiv ne_enclave->threads_per_core = kcalloc(ne_enclave->nr_parent_vm_cores,
163302bba596SAndra Paraschiv sizeof(*ne_enclave->threads_per_core),
163402bba596SAndra Paraschiv GFP_KERNEL);
163538907e12SAndra Paraschiv if (!ne_enclave->threads_per_core) {
163638907e12SAndra Paraschiv rc = -ENOMEM;
163738907e12SAndra Paraschiv
163838907e12SAndra Paraschiv goto free_ne_enclave;
163938907e12SAndra Paraschiv }
164038907e12SAndra Paraschiv
164138907e12SAndra Paraschiv for (i = 0; i < ne_enclave->nr_parent_vm_cores; i++)
164238907e12SAndra Paraschiv if (!zalloc_cpumask_var(&ne_enclave->threads_per_core[i], GFP_KERNEL)) {
164338907e12SAndra Paraschiv rc = -ENOMEM;
164438907e12SAndra Paraschiv
164538907e12SAndra Paraschiv goto free_cpumask;
164638907e12SAndra Paraschiv }
164738907e12SAndra Paraschiv
164838907e12SAndra Paraschiv if (!zalloc_cpumask_var(&ne_enclave->vcpu_ids, GFP_KERNEL)) {
164938907e12SAndra Paraschiv rc = -ENOMEM;
165038907e12SAndra Paraschiv
165138907e12SAndra Paraschiv goto free_cpumask;
165238907e12SAndra Paraschiv }
165338907e12SAndra Paraschiv
165438907e12SAndra Paraschiv enclave_fd = get_unused_fd_flags(O_CLOEXEC);
165538907e12SAndra Paraschiv if (enclave_fd < 0) {
165638907e12SAndra Paraschiv rc = enclave_fd;
165738907e12SAndra Paraschiv
165838907e12SAndra Paraschiv dev_err_ratelimited(ne_misc_dev.this_device,
165938907e12SAndra Paraschiv "Error in getting unused fd [rc=%d]\n", rc);
166038907e12SAndra Paraschiv
166138907e12SAndra Paraschiv goto free_cpumask;
166238907e12SAndra Paraschiv }
166338907e12SAndra Paraschiv
166438907e12SAndra Paraschiv enclave_file = anon_inode_getfile("ne-vm", &ne_enclave_fops, ne_enclave, O_RDWR);
166538907e12SAndra Paraschiv if (IS_ERR(enclave_file)) {
166638907e12SAndra Paraschiv rc = PTR_ERR(enclave_file);
166738907e12SAndra Paraschiv
166838907e12SAndra Paraschiv dev_err_ratelimited(ne_misc_dev.this_device,
166938907e12SAndra Paraschiv "Error in anon inode get file [rc=%d]\n", rc);
167038907e12SAndra Paraschiv
167138907e12SAndra Paraschiv goto put_fd;
167238907e12SAndra Paraschiv }
167338907e12SAndra Paraschiv
167438907e12SAndra Paraschiv rc = ne_do_request(pdev, SLOT_ALLOC,
167538907e12SAndra Paraschiv &slot_alloc_req, sizeof(slot_alloc_req),
167638907e12SAndra Paraschiv &cmd_reply, sizeof(cmd_reply));
167738907e12SAndra Paraschiv if (rc < 0) {
167838907e12SAndra Paraschiv dev_err_ratelimited(ne_misc_dev.this_device,
167938907e12SAndra Paraschiv "Error in slot alloc [rc=%d]\n", rc);
168038907e12SAndra Paraschiv
168138907e12SAndra Paraschiv goto put_file;
168238907e12SAndra Paraschiv }
168338907e12SAndra Paraschiv
168438907e12SAndra Paraschiv init_waitqueue_head(&ne_enclave->eventq);
168538907e12SAndra Paraschiv ne_enclave->has_event = false;
168638907e12SAndra Paraschiv mutex_init(&ne_enclave->enclave_info_mutex);
168738907e12SAndra Paraschiv ne_enclave->max_mem_regions = cmd_reply.mem_regions;
168838907e12SAndra Paraschiv INIT_LIST_HEAD(&ne_enclave->mem_regions_list);
168938907e12SAndra Paraschiv ne_enclave->mm = current->mm;
169038907e12SAndra Paraschiv ne_enclave->slot_uid = cmd_reply.slot_uid;
169138907e12SAndra Paraschiv ne_enclave->state = NE_STATE_INIT;
169238907e12SAndra Paraschiv
169338907e12SAndra Paraschiv list_add(&ne_enclave->enclave_list_entry, &ne_pci_dev->enclaves_list);
169438907e12SAndra Paraschiv
1695f1ce3986SMathias Krause if (copy_to_user(slot_uid, &ne_enclave->slot_uid, sizeof(ne_enclave->slot_uid))) {
1696f1ce3986SMathias Krause /*
1697f1ce3986SMathias Krause * As we're holding the only reference to 'enclave_file', fput()
1698f1ce3986SMathias Krause * will call ne_enclave_release() which will do a proper cleanup
1699f1ce3986SMathias Krause * of all so far allocated resources, leaving only the unused fd
1700f1ce3986SMathias Krause * for us to free.
1701f1ce3986SMathias Krause */
1702f1ce3986SMathias Krause fput(enclave_file);
1703f1ce3986SMathias Krause put_unused_fd(enclave_fd);
1704f1ce3986SMathias Krause
1705f1ce3986SMathias Krause return -EFAULT;
1706f1ce3986SMathias Krause }
170738907e12SAndra Paraschiv
170838907e12SAndra Paraschiv fd_install(enclave_fd, enclave_file);
170938907e12SAndra Paraschiv
171038907e12SAndra Paraschiv return enclave_fd;
171138907e12SAndra Paraschiv
171238907e12SAndra Paraschiv put_file:
171338907e12SAndra Paraschiv fput(enclave_file);
171438907e12SAndra Paraschiv put_fd:
171538907e12SAndra Paraschiv put_unused_fd(enclave_fd);
171638907e12SAndra Paraschiv free_cpumask:
171738907e12SAndra Paraschiv free_cpumask_var(ne_enclave->vcpu_ids);
171838907e12SAndra Paraschiv for (i = 0; i < ne_enclave->nr_parent_vm_cores; i++)
171938907e12SAndra Paraschiv free_cpumask_var(ne_enclave->threads_per_core[i]);
172038907e12SAndra Paraschiv kfree(ne_enclave->threads_per_core);
172138907e12SAndra Paraschiv free_ne_enclave:
172238907e12SAndra Paraschiv kfree(ne_enclave);
172338907e12SAndra Paraschiv
172438907e12SAndra Paraschiv return rc;
172538907e12SAndra Paraschiv }
172638907e12SAndra Paraschiv
172738907e12SAndra Paraschiv /**
172838907e12SAndra Paraschiv * ne_ioctl() - Ioctl function provided by the NE misc device.
172938907e12SAndra Paraschiv * @file: File associated with this ioctl function.
173038907e12SAndra Paraschiv * @cmd: The command that is set for the ioctl call.
173138907e12SAndra Paraschiv * @arg: The argument that is provided for the ioctl call.
173238907e12SAndra Paraschiv *
173338907e12SAndra Paraschiv * Context: Process context.
173438907e12SAndra Paraschiv * Return:
173538907e12SAndra Paraschiv * * Ioctl result (e.g. enclave file descriptor) on success.
173638907e12SAndra Paraschiv * * Negative return value on failure.
173738907e12SAndra Paraschiv */
ne_ioctl(struct file * file,unsigned int cmd,unsigned long arg)173838907e12SAndra Paraschiv static long ne_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
173938907e12SAndra Paraschiv {
174038907e12SAndra Paraschiv switch (cmd) {
174138907e12SAndra Paraschiv case NE_CREATE_VM: {
174238907e12SAndra Paraschiv int enclave_fd = -1;
174338907e12SAndra Paraschiv struct ne_pci_dev *ne_pci_dev = ne_devs.ne_pci_dev;
1744f1ce3986SMathias Krause u64 __user *slot_uid = (void __user *)arg;
174538907e12SAndra Paraschiv
174638907e12SAndra Paraschiv mutex_lock(&ne_pci_dev->enclaves_list_mutex);
1747f1ce3986SMathias Krause enclave_fd = ne_create_vm_ioctl(ne_pci_dev, slot_uid);
174838907e12SAndra Paraschiv mutex_unlock(&ne_pci_dev->enclaves_list_mutex);
174938907e12SAndra Paraschiv
175038907e12SAndra Paraschiv return enclave_fd;
175138907e12SAndra Paraschiv }
175238907e12SAndra Paraschiv
175338907e12SAndra Paraschiv default:
175438907e12SAndra Paraschiv return -ENOTTY;
175538907e12SAndra Paraschiv }
175638907e12SAndra Paraschiv
175738907e12SAndra Paraschiv return 0;
175838907e12SAndra Paraschiv }
175938907e12SAndra Paraschiv
176007503b3cSLongpeng #if defined(CONFIG_NITRO_ENCLAVES_MISC_DEV_TEST)
176107503b3cSLongpeng #include "ne_misc_dev_test.c"
176207503b3cSLongpeng #endif
176307503b3cSLongpeng
ne_init(void)1764bd47c995SAndra Paraschiv static int __init ne_init(void)
1765bd47c995SAndra Paraschiv {
1766bd47c995SAndra Paraschiv mutex_init(&ne_cpu_pool.mutex);
1767bd47c995SAndra Paraschiv
1768bd47c995SAndra Paraschiv return pci_register_driver(&ne_pci_driver);
1769bd47c995SAndra Paraschiv }
1770bd47c995SAndra Paraschiv
ne_exit(void)1771bd47c995SAndra Paraschiv static void __exit ne_exit(void)
1772bd47c995SAndra Paraschiv {
1773bd47c995SAndra Paraschiv pci_unregister_driver(&ne_pci_driver);
1774ff8a4d3eSAndra Paraschiv
1775ff8a4d3eSAndra Paraschiv ne_teardown_cpu_pool();
1776bd47c995SAndra Paraschiv }
1777bd47c995SAndra Paraschiv
1778bd47c995SAndra Paraschiv module_init(ne_init);
1779bd47c995SAndra Paraschiv module_exit(ne_exit);
1780bd47c995SAndra Paraschiv
1781bd47c995SAndra Paraschiv MODULE_AUTHOR("Amazon.com, Inc. or its affiliates");
1782bd47c995SAndra Paraschiv MODULE_DESCRIPTION("Nitro Enclaves Driver");
1783bd47c995SAndra Paraschiv MODULE_LICENSE("GPL v2");
1784