|
60 | 60 | */
|
61 | 61 | #define NE_PARENT_VM_CID (3)
|
62 | 62 |
|
| 63 | +static long ne_ioctl(struct file *file, unsigned int cmd, unsigned long arg); |
| 64 | + |
63 | 65 | static const struct file_operations ne_fops = {
|
64 | 66 | .owner = THIS_MODULE,
|
65 | 67 | .llseek = noop_llseek,
|
| 68 | + .unlocked_ioctl = ne_ioctl, |
66 | 69 | };
|
67 | 70 |
|
68 | 71 | static struct miscdevice ne_misc_dev = {
|
@@ -119,6 +122,226 @@ struct ne_cpu_pool {
|
119 | 122 |
|
120 | 123 | static struct ne_cpu_pool ne_cpu_pool;
|
121 | 124 |
|
| 125 | +/** |
| 126 | + * ne_enclave_poll() - Poll functionality used for enclave out-of-band events. |
| 127 | + * @file: File associated with this poll function. |
| 128 | + * @wait: Poll table data structure. |
| 129 | + * |
| 130 | + * Context: Process context. |
| 131 | + * Return: |
| 132 | + * * Poll mask. |
| 133 | + */ |
| 134 | +static __poll_t ne_enclave_poll(struct file *file, poll_table *wait) |
| 135 | +{ |
| 136 | + __poll_t mask = 0; |
| 137 | + struct ne_enclave *ne_enclave = file->private_data; |
| 138 | + |
| 139 | + poll_wait(file, &ne_enclave->eventq, wait); |
| 140 | + |
| 141 | + if (!ne_enclave->has_event) |
| 142 | + return mask; |
| 143 | + |
| 144 | + mask = POLLHUP; |
| 145 | + |
| 146 | + return mask; |
| 147 | +} |
| 148 | + |
| 149 | +static const struct file_operations ne_enclave_fops = { |
| 150 | + .owner = THIS_MODULE, |
| 151 | + .llseek = noop_llseek, |
| 152 | + .poll = ne_enclave_poll, |
| 153 | +}; |
| 154 | + |
| 155 | +/** |
| 156 | + * ne_create_vm_ioctl() - Alloc slot to be associated with an enclave. Create |
| 157 | + * enclave file descriptor to be further used for enclave |
| 158 | + * resources handling e.g. memory regions and CPUs. |
| 159 | + * @ne_pci_dev : Private data associated with the PCI device. |
| 160 | + * @slot_uid: Generated unique slot id associated with an enclave. |
| 161 | + * |
| 162 | + * Context: Process context. This function is called with the ne_pci_dev enclave |
| 163 | + * mutex held. |
| 164 | + * Return: |
| 165 | + * * Enclave fd on success. |
| 166 | + * * Negative return value on failure. |
| 167 | + */ |
| 168 | +static int ne_create_vm_ioctl(struct ne_pci_dev *ne_pci_dev, u64 *slot_uid) |
| 169 | +{ |
| 170 | + struct ne_pci_dev_cmd_reply cmd_reply = {}; |
| 171 | + int enclave_fd = -1; |
| 172 | + struct file *enclave_file = NULL; |
| 173 | + unsigned int i = 0; |
| 174 | + struct ne_enclave *ne_enclave = NULL; |
| 175 | + struct pci_dev *pdev = ne_pci_dev->pdev; |
| 176 | + int rc = -EINVAL; |
| 177 | + struct slot_alloc_req slot_alloc_req = {}; |
| 178 | + |
| 179 | + mutex_lock(&ne_cpu_pool.mutex); |
| 180 | + |
| 181 | + for (i = 0; i < ne_cpu_pool.nr_parent_vm_cores; i++) |
| 182 | + if (!cpumask_empty(ne_cpu_pool.avail_threads_per_core[i])) |
| 183 | + break; |
| 184 | + |
| 185 | + if (i == ne_cpu_pool.nr_parent_vm_cores) { |
| 186 | + dev_err_ratelimited(ne_misc_dev.this_device, |
| 187 | + "No CPUs available in CPU pool\n"); |
| 188 | + |
| 189 | + mutex_unlock(&ne_cpu_pool.mutex); |
| 190 | + |
| 191 | + return -NE_ERR_NO_CPUS_AVAIL_IN_POOL; |
| 192 | + } |
| 193 | + |
| 194 | + mutex_unlock(&ne_cpu_pool.mutex); |
| 195 | + |
| 196 | + ne_enclave = kzalloc(sizeof(*ne_enclave), GFP_KERNEL); |
| 197 | + if (!ne_enclave) |
| 198 | + return -ENOMEM; |
| 199 | + |
| 200 | + mutex_lock(&ne_cpu_pool.mutex); |
| 201 | + |
| 202 | + ne_enclave->nr_parent_vm_cores = ne_cpu_pool.nr_parent_vm_cores; |
| 203 | + ne_enclave->nr_threads_per_core = ne_cpu_pool.nr_threads_per_core; |
| 204 | + ne_enclave->numa_node = ne_cpu_pool.numa_node; |
| 205 | + |
| 206 | + mutex_unlock(&ne_cpu_pool.mutex); |
| 207 | + |
| 208 | + ne_enclave->threads_per_core = kcalloc(ne_enclave->nr_parent_vm_cores, |
| 209 | + sizeof(*ne_enclave->threads_per_core), GFP_KERNEL); |
| 210 | + if (!ne_enclave->threads_per_core) { |
| 211 | + rc = -ENOMEM; |
| 212 | + |
| 213 | + goto free_ne_enclave; |
| 214 | + } |
| 215 | + |
| 216 | + for (i = 0; i < ne_enclave->nr_parent_vm_cores; i++) |
| 217 | + if (!zalloc_cpumask_var(&ne_enclave->threads_per_core[i], GFP_KERNEL)) { |
| 218 | + rc = -ENOMEM; |
| 219 | + |
| 220 | + goto free_cpumask; |
| 221 | + } |
| 222 | + |
| 223 | + if (!zalloc_cpumask_var(&ne_enclave->vcpu_ids, GFP_KERNEL)) { |
| 224 | + rc = -ENOMEM; |
| 225 | + |
| 226 | + goto free_cpumask; |
| 227 | + } |
| 228 | + |
| 229 | + enclave_fd = get_unused_fd_flags(O_CLOEXEC); |
| 230 | + if (enclave_fd < 0) { |
| 231 | + rc = enclave_fd; |
| 232 | + |
| 233 | + dev_err_ratelimited(ne_misc_dev.this_device, |
| 234 | + "Error in getting unused fd [rc=%d]\n", rc); |
| 235 | + |
| 236 | + goto free_cpumask; |
| 237 | + } |
| 238 | + |
| 239 | + enclave_file = anon_inode_getfile("ne-vm", &ne_enclave_fops, ne_enclave, O_RDWR); |
| 240 | + if (IS_ERR(enclave_file)) { |
| 241 | + rc = PTR_ERR(enclave_file); |
| 242 | + |
| 243 | + dev_err_ratelimited(ne_misc_dev.this_device, |
| 244 | + "Error in anon inode get file [rc=%d]\n", rc); |
| 245 | + |
| 246 | + goto put_fd; |
| 247 | + } |
| 248 | + |
| 249 | + rc = ne_do_request(pdev, SLOT_ALLOC, |
| 250 | + &slot_alloc_req, sizeof(slot_alloc_req), |
| 251 | + &cmd_reply, sizeof(cmd_reply)); |
| 252 | + if (rc < 0) { |
| 253 | + dev_err_ratelimited(ne_misc_dev.this_device, |
| 254 | + "Error in slot alloc [rc=%d]\n", rc); |
| 255 | + |
| 256 | + goto put_file; |
| 257 | + } |
| 258 | + |
| 259 | + init_waitqueue_head(&ne_enclave->eventq); |
| 260 | + ne_enclave->has_event = false; |
| 261 | + mutex_init(&ne_enclave->enclave_info_mutex); |
| 262 | + ne_enclave->max_mem_regions = cmd_reply.mem_regions; |
| 263 | + INIT_LIST_HEAD(&ne_enclave->mem_regions_list); |
| 264 | + ne_enclave->mm = current->mm; |
| 265 | + ne_enclave->slot_uid = cmd_reply.slot_uid; |
| 266 | + ne_enclave->state = NE_STATE_INIT; |
| 267 | + |
| 268 | + list_add(&ne_enclave->enclave_list_entry, &ne_pci_dev->enclaves_list); |
| 269 | + |
| 270 | + *slot_uid = ne_enclave->slot_uid; |
| 271 | + |
| 272 | + fd_install(enclave_fd, enclave_file); |
| 273 | + |
| 274 | + return enclave_fd; |
| 275 | + |
| 276 | +put_file: |
| 277 | + fput(enclave_file); |
| 278 | +put_fd: |
| 279 | + put_unused_fd(enclave_fd); |
| 280 | +free_cpumask: |
| 281 | + free_cpumask_var(ne_enclave->vcpu_ids); |
| 282 | + for (i = 0; i < ne_enclave->nr_parent_vm_cores; i++) |
| 283 | + free_cpumask_var(ne_enclave->threads_per_core[i]); |
| 284 | + kfree(ne_enclave->threads_per_core); |
| 285 | +free_ne_enclave: |
| 286 | + kfree(ne_enclave); |
| 287 | + |
| 288 | + return rc; |
| 289 | +} |
| 290 | + |
| 291 | +/** |
| 292 | + * ne_ioctl() - Ioctl function provided by the NE misc device. |
| 293 | + * @file: File associated with this ioctl function. |
| 294 | + * @cmd: The command that is set for the ioctl call. |
| 295 | + * @arg: The argument that is provided for the ioctl call. |
| 296 | + * |
| 297 | + * Context: Process context. |
| 298 | + * Return: |
| 299 | + * * Ioctl result (e.g. enclave file descriptor) on success. |
| 300 | + * * Negative return value on failure. |
| 301 | + */ |
| 302 | +static long ne_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 303 | +{ |
| 304 | + switch (cmd) { |
| 305 | + case NE_CREATE_VM: { |
| 306 | + int enclave_fd = -1; |
| 307 | + struct file *enclave_file = NULL; |
| 308 | + struct ne_pci_dev *ne_pci_dev = ne_devs.ne_pci_dev; |
| 309 | + int rc = -EINVAL; |
| 310 | + u64 slot_uid = 0; |
| 311 | + |
| 312 | + mutex_lock(&ne_pci_dev->enclaves_list_mutex); |
| 313 | + |
| 314 | + enclave_fd = ne_create_vm_ioctl(ne_pci_dev, &slot_uid); |
| 315 | + if (enclave_fd < 0) { |
| 316 | + rc = enclave_fd; |
| 317 | + |
| 318 | + mutex_unlock(&ne_pci_dev->enclaves_list_mutex); |
| 319 | + |
| 320 | + return rc; |
| 321 | + } |
| 322 | + |
| 323 | + mutex_unlock(&ne_pci_dev->enclaves_list_mutex); |
| 324 | + |
| 325 | + if (copy_to_user((void __user *)arg, &slot_uid, sizeof(slot_uid))) { |
| 326 | + enclave_file = fget(enclave_fd); |
| 327 | + /* Decrement file refs to have release() called. */ |
| 328 | + fput(enclave_file); |
| 329 | + fput(enclave_file); |
| 330 | + put_unused_fd(enclave_fd); |
| 331 | + |
| 332 | + return -EFAULT; |
| 333 | + } |
| 334 | + |
| 335 | + return enclave_fd; |
| 336 | + } |
| 337 | + |
| 338 | + default: |
| 339 | + return -ENOTTY; |
| 340 | + } |
| 341 | + |
| 342 | + return 0; |
| 343 | +} |
| 344 | + |
122 | 345 | static int __init ne_init(void)
|
123 | 346 | {
|
124 | 347 | mutex_init(&ne_cpu_pool.mutex);
|
|
0 commit comments