1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include <linux/vmw_vmci_defs.h>
17#include <linux/vmw_vmci_api.h>
18#include <linux/moduleparam.h>
19#include <linux/miscdevice.h>
20#include <linux/interrupt.h>
21#include <linux/highmem.h>
22#include <linux/atomic.h>
23#include <linux/kernel.h>
24#include <linux/module.h>
25#include <linux/mutex.h>
26#include <linux/sched.h>
27#include <linux/slab.h>
28#include <linux/file.h>
29#include <linux/init.h>
30#include <linux/poll.h>
31#include <linux/pci.h>
32#include <linux/smp.h>
33#include <linux/fs.h>
34#include <linux/io.h>
35
36#include "vmci_handle_array.h"
37#include "vmci_queue_pair.h"
38#include "vmci_datagram.h"
39#include "vmci_doorbell.h"
40#include "vmci_resource.h"
41#include "vmci_context.h"
42#include "vmci_driver.h"
43#include "vmci_event.h"
44
45#define VMCI_UTIL_NUM_RESOURCES 1
46
47enum {
48 VMCI_NOTIFY_RESOURCE_QUEUE_PAIR = 0,
49 VMCI_NOTIFY_RESOURCE_DOOR_BELL = 1,
50};
51
52enum {
53 VMCI_NOTIFY_RESOURCE_ACTION_NOTIFY = 0,
54 VMCI_NOTIFY_RESOURCE_ACTION_CREATE = 1,
55 VMCI_NOTIFY_RESOURCE_ACTION_DESTROY = 2,
56};
57
58
59
60
61
62struct vmci_init_blk {
63 u32 cid;
64 u32 flags;
65};
66
67
68struct vmci_qp_alloc_info_vmvm {
69 struct vmci_handle handle;
70 u32 peer;
71 u32 flags;
72 u64 produce_size;
73 u64 consume_size;
74 u64 produce_page_file;
75 u64 consume_page_file;
76 u64 produce_page_file_size;
77 u64 consume_page_file_size;
78 s32 result;
79 u32 _pad;
80};
81
82
83struct vmci_set_notify_info {
84 u64 notify_uva;
85 s32 result;
86 u32 _pad;
87};
88
89
90
91
92struct vmci_host_dev {
93 struct vmci_ctx *context;
94 int user_version;
95 enum vmci_obj_type ct_type;
96 struct mutex lock;
97};
98
99static struct vmci_ctx *host_context;
100static bool vmci_host_device_initialized;
101static atomic_t vmci_host_active_users = ATOMIC_INIT(0);
102
103
104
105
106
107
108
109
110
111
112bool vmci_host_code_active(void)
113{
114 return vmci_host_device_initialized &&
115 (!vmci_guest_code_active() ||
116 atomic_read(&vmci_host_active_users) > 0);
117}
118
119
120
121
122static int vmci_host_open(struct inode *inode, struct file *filp)
123{
124 struct vmci_host_dev *vmci_host_dev;
125
126 vmci_host_dev = kzalloc(sizeof(struct vmci_host_dev), GFP_KERNEL);
127 if (vmci_host_dev == NULL)
128 return -ENOMEM;
129
130 vmci_host_dev->ct_type = VMCIOBJ_NOT_SET;
131 mutex_init(&vmci_host_dev->lock);
132 filp->private_data = vmci_host_dev;
133
134 return 0;
135}
136
137
138
139
140
141static int vmci_host_close(struct inode *inode, struct file *filp)
142{
143 struct vmci_host_dev *vmci_host_dev = filp->private_data;
144
145 if (vmci_host_dev->ct_type == VMCIOBJ_CONTEXT) {
146 vmci_ctx_destroy(vmci_host_dev->context);
147 vmci_host_dev->context = NULL;
148
149
150
151
152
153
154
155 atomic_dec(&vmci_host_active_users);
156 }
157 vmci_host_dev->ct_type = VMCIOBJ_NOT_SET;
158
159 kfree(vmci_host_dev);
160 filp->private_data = NULL;
161 return 0;
162}
163
164
165
166
167
168static unsigned int vmci_host_poll(struct file *filp, poll_table *wait)
169{
170 struct vmci_host_dev *vmci_host_dev = filp->private_data;
171 struct vmci_ctx *context = vmci_host_dev->context;
172 unsigned int mask = 0;
173
174 if (vmci_host_dev->ct_type == VMCIOBJ_CONTEXT) {
175
176 if (wait)
177 poll_wait(filp, &context->host_context.wait_queue,
178 wait);
179
180 spin_lock(&context->lock);
181 if (context->pending_datagrams > 0 ||
182 vmci_handle_arr_get_size(
183 context->pending_doorbell_array) > 0) {
184 mask = POLLIN;
185 }
186 spin_unlock(&context->lock);
187 }
188 return mask;
189}
190
191
192
193
194
195
196
197static int drv_cp_harray_to_user(void __user *user_buf_uva,
198 u64 *user_buf_size,
199 struct vmci_handle_arr *handle_array,
200 int *retval)
201{
202 u32 array_size = 0;
203 struct vmci_handle *handles;
204
205 if (handle_array)
206 array_size = vmci_handle_arr_get_size(handle_array);
207
208 if (array_size * sizeof(*handles) > *user_buf_size)
209 return VMCI_ERROR_MORE_DATA;
210
211 *user_buf_size = array_size * sizeof(*handles);
212 if (*user_buf_size)
213 *retval = copy_to_user(user_buf_uva,
214 vmci_handle_arr_get_handles
215 (handle_array), *user_buf_size);
216
217 return VMCI_SUCCESS;
218}
219
220
221
222
223
224static int vmci_host_setup_notify(struct vmci_ctx *context,
225 unsigned long uva)
226{
227 struct page *page;
228 int retval;
229
230 if (context->notify_page) {
231 pr_devel("%s: Notify mechanism is already set up\n", __func__);
232 return VMCI_ERROR_DUPLICATE_ENTRY;
233 }
234
235
236
237
238
239 BUILD_BUG_ON(sizeof(bool) != sizeof(u8));
240 if (!access_ok(VERIFY_WRITE, (void __user *)uva, sizeof(u8)))
241 return VMCI_ERROR_GENERIC;
242
243
244
245
246 retval = get_user_pages_fast(PAGE_ALIGN(uva), 1, 1, &page);
247 if (retval != 1)
248 return VMCI_ERROR_GENERIC;
249
250
251
252
253 context->notify = kmap(page) + (uva & (PAGE_SIZE - 1));
254 vmci_ctx_check_signal_notify(context);
255
256 return VMCI_SUCCESS;
257}
258
259static int vmci_host_get_version(struct vmci_host_dev *vmci_host_dev,
260 unsigned int cmd, void __user *uptr)
261{
262 if (cmd == IOCTL_VMCI_VERSION2) {
263 int __user *vptr = uptr;
264 if (get_user(vmci_host_dev->user_version, vptr))
265 return -EFAULT;
266 }
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282 if (vmci_host_dev->user_version > 0 &&
283 vmci_host_dev->user_version < VMCI_VERSION_HOSTQP) {
284 return vmci_host_dev->user_version;
285 }
286
287 return VMCI_VERSION;
288}
289
290#define vmci_ioctl_err(fmt, ...) \
291 pr_devel("%s: " fmt, ioctl_name, ##__VA_ARGS__)
292
293static int vmci_host_do_init_context(struct vmci_host_dev *vmci_host_dev,
294 const char *ioctl_name,
295 void __user *uptr)
296{
297 struct vmci_init_blk init_block;
298 const struct cred *cred;
299 int retval;
300
301 if (copy_from_user(&init_block, uptr, sizeof(init_block))) {
302 vmci_ioctl_err("error reading init block\n");
303 return -EFAULT;
304 }
305
306 mutex_lock(&vmci_host_dev->lock);
307
308 if (vmci_host_dev->ct_type != VMCIOBJ_NOT_SET) {
309 vmci_ioctl_err("received VMCI init on initialized handle\n");
310 retval = -EINVAL;
311 goto out;
312 }
313
314 if (init_block.flags & ~VMCI_PRIVILEGE_FLAG_RESTRICTED) {
315 vmci_ioctl_err("unsupported VMCI restriction flag\n");
316 retval = -EINVAL;
317 goto out;
318 }
319
320 cred = get_current_cred();
321 vmci_host_dev->context = vmci_ctx_create(init_block.cid,
322 init_block.flags, 0,
323 vmci_host_dev->user_version,
324 cred);
325 put_cred(cred);
326 if (IS_ERR(vmci_host_dev->context)) {
327 retval = PTR_ERR(vmci_host_dev->context);
328 vmci_ioctl_err("error initializing context\n");
329 goto out;
330 }
331
332
333
334
335
336 init_block.cid = vmci_ctx_get_id(vmci_host_dev->context);
337 if (copy_to_user(uptr, &init_block, sizeof(init_block))) {
338 vmci_ctx_destroy(vmci_host_dev->context);
339 vmci_host_dev->context = NULL;
340 vmci_ioctl_err("error writing init block\n");
341 retval = -EFAULT;
342 goto out;
343 }
344
345 vmci_host_dev->ct_type = VMCIOBJ_CONTEXT;
346 atomic_inc(&vmci_host_active_users);
347
348 retval = 0;
349
350out:
351 mutex_unlock(&vmci_host_dev->lock);
352 return retval;
353}
354
355static int vmci_host_do_send_datagram(struct vmci_host_dev *vmci_host_dev,
356 const char *ioctl_name,
357 void __user *uptr)
358{
359 struct vmci_datagram_snd_rcv_info send_info;
360 struct vmci_datagram *dg = NULL;
361 u32 cid;
362
363 if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
364 vmci_ioctl_err("only valid for contexts\n");
365 return -EINVAL;
366 }
367
368 if (copy_from_user(&send_info, uptr, sizeof(send_info)))
369 return -EFAULT;
370
371 if (send_info.len > VMCI_MAX_DG_SIZE) {
372 vmci_ioctl_err("datagram is too big (size=%d)\n",
373 send_info.len);
374 return -EINVAL;
375 }
376
377 if (send_info.len < sizeof(*dg)) {
378 vmci_ioctl_err("datagram is too small (size=%d)\n",
379 send_info.len);
380 return -EINVAL;
381 }
382
383 dg = kmalloc(send_info.len, GFP_KERNEL);
384 if (!dg) {
385 vmci_ioctl_err(
386 "cannot allocate memory to dispatch datagram\n");
387 return -ENOMEM;
388 }
389
390 if (copy_from_user(dg, (void __user *)(uintptr_t)send_info.addr,
391 send_info.len)) {
392 vmci_ioctl_err("error getting datagram\n");
393 kfree(dg);
394 return -EFAULT;
395 }
396
397 pr_devel("Datagram dst (handle=0x%x:0x%x) src (handle=0x%x:0x%x), payload (size=%llu bytes)\n",
398 dg->dst.context, dg->dst.resource,
399 dg->src.context, dg->src.resource,
400 (unsigned long long)dg->payload_size);
401
402
403 cid = vmci_ctx_get_id(vmci_host_dev->context);
404 send_info.result = vmci_datagram_dispatch(cid, dg, true);
405 kfree(dg);
406
407 return copy_to_user(uptr, &send_info, sizeof(send_info)) ? -EFAULT : 0;
408}
409
410static int vmci_host_do_receive_datagram(struct vmci_host_dev *vmci_host_dev,
411 const char *ioctl_name,
412 void __user *uptr)
413{
414 struct vmci_datagram_snd_rcv_info recv_info;
415 struct vmci_datagram *dg = NULL;
416 int retval;
417 size_t size;
418
419 if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
420 vmci_ioctl_err("only valid for contexts\n");
421 return -EINVAL;
422 }
423
424 if (copy_from_user(&recv_info, uptr, sizeof(recv_info)))
425 return -EFAULT;
426
427 size = recv_info.len;
428 recv_info.result = vmci_ctx_dequeue_datagram(vmci_host_dev->context,
429 &size, &dg);
430
431 if (recv_info.result >= VMCI_SUCCESS) {
432 void __user *ubuf = (void __user *)(uintptr_t)recv_info.addr;
433 retval = copy_to_user(ubuf, dg, VMCI_DG_SIZE(dg));
434 kfree(dg);
435 if (retval != 0)
436 return -EFAULT;
437 }
438
439 return copy_to_user(uptr, &recv_info, sizeof(recv_info)) ? -EFAULT : 0;
440}
441
442static int vmci_host_do_alloc_queuepair(struct vmci_host_dev *vmci_host_dev,
443 const char *ioctl_name,
444 void __user *uptr)
445{
446 struct vmci_handle handle;
447 int vmci_status;
448 int __user *retptr;
449 u32 cid;
450
451 if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
452 vmci_ioctl_err("only valid for contexts\n");
453 return -EINVAL;
454 }
455
456 cid = vmci_ctx_get_id(vmci_host_dev->context);
457
458 if (vmci_host_dev->user_version < VMCI_VERSION_NOVMVM) {
459 struct vmci_qp_alloc_info_vmvm alloc_info;
460 struct vmci_qp_alloc_info_vmvm __user *info = uptr;
461
462 if (copy_from_user(&alloc_info, uptr, sizeof(alloc_info)))
463 return -EFAULT;
464
465 handle = alloc_info.handle;
466 retptr = &info->result;
467
468 vmci_status = vmci_qp_broker_alloc(alloc_info.handle,
469 alloc_info.peer,
470 alloc_info.flags,
471 VMCI_NO_PRIVILEGE_FLAGS,
472 alloc_info.produce_size,
473 alloc_info.consume_size,
474 NULL,
475 vmci_host_dev->context);
476
477 if (vmci_status == VMCI_SUCCESS)
478 vmci_status = VMCI_SUCCESS_QUEUEPAIR_CREATE;
479 } else {
480 struct vmci_qp_alloc_info alloc_info;
481 struct vmci_qp_alloc_info __user *info = uptr;
482 struct vmci_qp_page_store page_store;
483
484 if (copy_from_user(&alloc_info, uptr, sizeof(alloc_info)))
485 return -EFAULT;
486
487 handle = alloc_info.handle;
488 retptr = &info->result;
489
490 page_store.pages = alloc_info.ppn_va;
491 page_store.len = alloc_info.num_ppns;
492
493 vmci_status = vmci_qp_broker_alloc(alloc_info.handle,
494 alloc_info.peer,
495 alloc_info.flags,
496 VMCI_NO_PRIVILEGE_FLAGS,
497 alloc_info.produce_size,
498 alloc_info.consume_size,
499 &page_store,
500 vmci_host_dev->context);
501 }
502
503 if (put_user(vmci_status, retptr)) {
504 if (vmci_status >= VMCI_SUCCESS) {
505 vmci_status = vmci_qp_broker_detach(handle,
506 vmci_host_dev->context);
507 }
508 return -EFAULT;
509 }
510
511 return 0;
512}
513
514static int vmci_host_do_queuepair_setva(struct vmci_host_dev *vmci_host_dev,
515 const char *ioctl_name,
516 void __user *uptr)
517{
518 struct vmci_qp_set_va_info set_va_info;
519 struct vmci_qp_set_va_info __user *info = uptr;
520 s32 result;
521
522 if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
523 vmci_ioctl_err("only valid for contexts\n");
524 return -EINVAL;
525 }
526
527 if (vmci_host_dev->user_version < VMCI_VERSION_NOVMVM) {
528 vmci_ioctl_err("is not allowed\n");
529 return -EINVAL;
530 }
531
532 if (copy_from_user(&set_va_info, uptr, sizeof(set_va_info)))
533 return -EFAULT;
534
535 if (set_va_info.va) {
536
537
538
539
540 result = vmci_qp_broker_map(set_va_info.handle,
541 vmci_host_dev->context,
542 set_va_info.va);
543 } else {
544
545
546
547
548 result = vmci_qp_broker_unmap(set_va_info.handle,
549 vmci_host_dev->context, 0);
550 }
551
552 return put_user(result, &info->result) ? -EFAULT : 0;
553}
554
555static int vmci_host_do_queuepair_setpf(struct vmci_host_dev *vmci_host_dev,
556 const char *ioctl_name,
557 void __user *uptr)
558{
559 struct vmci_qp_page_file_info page_file_info;
560 struct vmci_qp_page_file_info __user *info = uptr;
561 s32 result;
562
563 if (vmci_host_dev->user_version < VMCI_VERSION_HOSTQP ||
564 vmci_host_dev->user_version >= VMCI_VERSION_NOVMVM) {
565 vmci_ioctl_err("not supported on this VMX (version=%d)\n",
566 vmci_host_dev->user_version);
567 return -EINVAL;
568 }
569
570 if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
571 vmci_ioctl_err("only valid for contexts\n");
572 return -EINVAL;
573 }
574
575 if (copy_from_user(&page_file_info, uptr, sizeof(*info)))
576 return -EFAULT;
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594 if (put_user(VMCI_SUCCESS, &info->result)) {
595
596
597
598
599
600 return -EFAULT;
601 }
602
603 result = vmci_qp_broker_set_page_store(page_file_info.handle,
604 page_file_info.produce_va,
605 page_file_info.consume_va,
606 vmci_host_dev->context);
607 if (result < VMCI_SUCCESS) {
608 if (put_user(result, &info->result)) {
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626 return -EFAULT;
627 }
628 }
629
630 return 0;
631}
632
633static int vmci_host_do_qp_detach(struct vmci_host_dev *vmci_host_dev,
634 const char *ioctl_name,
635 void __user *uptr)
636{
637 struct vmci_qp_dtch_info detach_info;
638 struct vmci_qp_dtch_info __user *info = uptr;
639 s32 result;
640
641 if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
642 vmci_ioctl_err("only valid for contexts\n");
643 return -EINVAL;
644 }
645
646 if (copy_from_user(&detach_info, uptr, sizeof(detach_info)))
647 return -EFAULT;
648
649 result = vmci_qp_broker_detach(detach_info.handle,
650 vmci_host_dev->context);
651 if (result == VMCI_SUCCESS &&
652 vmci_host_dev->user_version < VMCI_VERSION_NOVMVM) {
653 result = VMCI_SUCCESS_LAST_DETACH;
654 }
655
656 return put_user(result, &info->result) ? -EFAULT : 0;
657}
658
659static int vmci_host_do_ctx_add_notify(struct vmci_host_dev *vmci_host_dev,
660 const char *ioctl_name,
661 void __user *uptr)
662{
663 struct vmci_ctx_info ar_info;
664 struct vmci_ctx_info __user *info = uptr;
665 s32 result;
666 u32 cid;
667
668 if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
669 vmci_ioctl_err("only valid for contexts\n");
670 return -EINVAL;
671 }
672
673 if (copy_from_user(&ar_info, uptr, sizeof(ar_info)))
674 return -EFAULT;
675
676 cid = vmci_ctx_get_id(vmci_host_dev->context);
677 result = vmci_ctx_add_notification(cid, ar_info.remote_cid);
678
679 return put_user(result, &info->result) ? -EFAULT : 0;
680}
681
682static int vmci_host_do_ctx_remove_notify(struct vmci_host_dev *vmci_host_dev,
683 const char *ioctl_name,
684 void __user *uptr)
685{
686 struct vmci_ctx_info ar_info;
687 struct vmci_ctx_info __user *info = uptr;
688 u32 cid;
689 int result;
690
691 if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
692 vmci_ioctl_err("only valid for contexts\n");
693 return -EINVAL;
694 }
695
696 if (copy_from_user(&ar_info, uptr, sizeof(ar_info)))
697 return -EFAULT;
698
699 cid = vmci_ctx_get_id(vmci_host_dev->context);
700 result = vmci_ctx_remove_notification(cid,
701 ar_info.remote_cid);
702
703 return put_user(result, &info->result) ? -EFAULT : 0;
704}
705
706static int vmci_host_do_ctx_get_cpt_state(struct vmci_host_dev *vmci_host_dev,
707 const char *ioctl_name,
708 void __user *uptr)
709{
710 struct vmci_ctx_chkpt_buf_info get_info;
711 u32 cid;
712 void *cpt_buf;
713 int retval;
714
715 if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
716 vmci_ioctl_err("only valid for contexts\n");
717 return -EINVAL;
718 }
719
720 if (copy_from_user(&get_info, uptr, sizeof(get_info)))
721 return -EFAULT;
722
723 cid = vmci_ctx_get_id(vmci_host_dev->context);
724 get_info.result = vmci_ctx_get_chkpt_state(cid, get_info.cpt_type,
725 &get_info.buf_size, &cpt_buf);
726 if (get_info.result == VMCI_SUCCESS && get_info.buf_size) {
727 void __user *ubuf = (void __user *)(uintptr_t)get_info.cpt_buf;
728 retval = copy_to_user(ubuf, cpt_buf, get_info.buf_size);
729 kfree(cpt_buf);
730
731 if (retval)
732 return -EFAULT;
733 }
734
735 return copy_to_user(uptr, &get_info, sizeof(get_info)) ? -EFAULT : 0;
736}
737
738static int vmci_host_do_ctx_set_cpt_state(struct vmci_host_dev *vmci_host_dev,
739 const char *ioctl_name,
740 void __user *uptr)
741{
742 struct vmci_ctx_chkpt_buf_info set_info;
743 u32 cid;
744 void *cpt_buf;
745 int retval;
746
747 if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
748 vmci_ioctl_err("only valid for contexts\n");
749 return -EINVAL;
750 }
751
752 if (copy_from_user(&set_info, uptr, sizeof(set_info)))
753 return -EFAULT;
754
755 cpt_buf = kmalloc(set_info.buf_size, GFP_KERNEL);
756 if (!cpt_buf) {
757 vmci_ioctl_err(
758 "cannot allocate memory to set cpt state (type=%d)\n",
759 set_info.cpt_type);
760 return -ENOMEM;
761 }
762
763 if (copy_from_user(cpt_buf, (void __user *)(uintptr_t)set_info.cpt_buf,
764 set_info.buf_size)) {
765 retval = -EFAULT;
766 goto out;
767 }
768
769 cid = vmci_ctx_get_id(vmci_host_dev->context);
770 set_info.result = vmci_ctx_set_chkpt_state(cid, set_info.cpt_type,
771 set_info.buf_size, cpt_buf);
772
773 retval = copy_to_user(uptr, &set_info, sizeof(set_info)) ? -EFAULT : 0;
774
775out:
776 kfree(cpt_buf);
777 return retval;
778}
779
780static int vmci_host_do_get_context_id(struct vmci_host_dev *vmci_host_dev,
781 const char *ioctl_name,
782 void __user *uptr)
783{
784 u32 __user *u32ptr = uptr;
785
786 return put_user(VMCI_HOST_CONTEXT_ID, u32ptr) ? -EFAULT : 0;
787}
788
789static int vmci_host_do_set_notify(struct vmci_host_dev *vmci_host_dev,
790 const char *ioctl_name,
791 void __user *uptr)
792{
793 struct vmci_set_notify_info notify_info;
794
795 if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
796 vmci_ioctl_err("only valid for contexts\n");
797 return -EINVAL;
798 }
799
800 if (copy_from_user(¬ify_info, uptr, sizeof(notify_info)))
801 return -EFAULT;
802
803 if (notify_info.notify_uva) {
804 notify_info.result =
805 vmci_host_setup_notify(vmci_host_dev->context,
806 notify_info.notify_uva);
807 } else {
808 vmci_ctx_unset_notify(vmci_host_dev->context);
809 notify_info.result = VMCI_SUCCESS;
810 }
811
812 return copy_to_user(uptr, ¬ify_info, sizeof(notify_info)) ?
813 -EFAULT : 0;
814}
815
816static int vmci_host_do_notify_resource(struct vmci_host_dev *vmci_host_dev,
817 const char *ioctl_name,
818 void __user *uptr)
819{
820 struct vmci_dbell_notify_resource_info info;
821 u32 cid;
822
823 if (vmci_host_dev->user_version < VMCI_VERSION_NOTIFY) {
824 vmci_ioctl_err("invalid for current VMX versions\n");
825 return -EINVAL;
826 }
827
828 if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
829 vmci_ioctl_err("only valid for contexts\n");
830 return -EINVAL;
831 }
832
833 if (copy_from_user(&info, uptr, sizeof(info)))
834 return -EFAULT;
835
836 cid = vmci_ctx_get_id(vmci_host_dev->context);
837
838 switch (info.action) {
839 case VMCI_NOTIFY_RESOURCE_ACTION_NOTIFY:
840 if (info.resource == VMCI_NOTIFY_RESOURCE_DOOR_BELL) {
841 u32 flags = VMCI_NO_PRIVILEGE_FLAGS;
842 info.result = vmci_ctx_notify_dbell(cid, info.handle,
843 flags);
844 } else {
845 info.result = VMCI_ERROR_UNAVAILABLE;
846 }
847 break;
848
849 case VMCI_NOTIFY_RESOURCE_ACTION_CREATE:
850 info.result = vmci_ctx_dbell_create(cid, info.handle);
851 break;
852
853 case VMCI_NOTIFY_RESOURCE_ACTION_DESTROY:
854 info.result = vmci_ctx_dbell_destroy(cid, info.handle);
855 break;
856
857 default:
858 vmci_ioctl_err("got unknown action (action=%d)\n",
859 info.action);
860 info.result = VMCI_ERROR_INVALID_ARGS;
861 }
862
863 return copy_to_user(uptr, &info, sizeof(info)) ? -EFAULT : 0;
864}
865
866static int vmci_host_do_recv_notifications(struct vmci_host_dev *vmci_host_dev,
867 const char *ioctl_name,
868 void __user *uptr)
869{
870 struct vmci_ctx_notify_recv_info info;
871 struct vmci_handle_arr *db_handle_array;
872 struct vmci_handle_arr *qp_handle_array;
873 void __user *ubuf;
874 u32 cid;
875 int retval = 0;
876
877 if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
878 vmci_ioctl_err("only valid for contexts\n");
879 return -EINVAL;
880 }
881
882 if (vmci_host_dev->user_version < VMCI_VERSION_NOTIFY) {
883 vmci_ioctl_err("not supported for the current vmx version\n");
884 return -EINVAL;
885 }
886
887 if (copy_from_user(&info, uptr, sizeof(info)))
888 return -EFAULT;
889
890 if ((info.db_handle_buf_size && !info.db_handle_buf_uva) ||
891 (info.qp_handle_buf_size && !info.qp_handle_buf_uva)) {
892 return -EINVAL;
893 }
894
895 cid = vmci_ctx_get_id(vmci_host_dev->context);
896
897 info.result = vmci_ctx_rcv_notifications_get(cid,
898 &db_handle_array, &qp_handle_array);
899 if (info.result != VMCI_SUCCESS)
900 return copy_to_user(uptr, &info, sizeof(info)) ? -EFAULT : 0;
901
902 ubuf = (void __user *)(uintptr_t)info.db_handle_buf_uva;
903 info.result = drv_cp_harray_to_user(ubuf, &info.db_handle_buf_size,
904 db_handle_array, &retval);
905 if (info.result == VMCI_SUCCESS && !retval) {
906 ubuf = (void __user *)(uintptr_t)info.qp_handle_buf_uva;
907 info.result = drv_cp_harray_to_user(ubuf,
908 &info.qp_handle_buf_size,
909 qp_handle_array, &retval);
910 }
911
912 if (!retval && copy_to_user(uptr, &info, sizeof(info)))
913 retval = -EFAULT;
914
915 vmci_ctx_rcv_notifications_release(cid,
916 db_handle_array, qp_handle_array,
917 info.result == VMCI_SUCCESS && !retval);
918
919 return retval;
920}
921
922static long vmci_host_unlocked_ioctl(struct file *filp,
923 unsigned int iocmd, unsigned long ioarg)
924{
925#define VMCI_DO_IOCTL(ioctl_name, ioctl_fn) do { \
926 char *name = __stringify(IOCTL_VMCI_ ## ioctl_name); \
927 return vmci_host_do_ ## ioctl_fn( \
928 vmci_host_dev, name, uptr); \
929 } while (0)
930
931 struct vmci_host_dev *vmci_host_dev = filp->private_data;
932 void __user *uptr = (void __user *)ioarg;
933
934 switch (iocmd) {
935 case IOCTL_VMCI_INIT_CONTEXT:
936 VMCI_DO_IOCTL(INIT_CONTEXT, init_context);
937 case IOCTL_VMCI_DATAGRAM_SEND:
938 VMCI_DO_IOCTL(DATAGRAM_SEND, send_datagram);
939 case IOCTL_VMCI_DATAGRAM_RECEIVE:
940 VMCI_DO_IOCTL(DATAGRAM_RECEIVE, receive_datagram);
941 case IOCTL_VMCI_QUEUEPAIR_ALLOC:
942 VMCI_DO_IOCTL(QUEUEPAIR_ALLOC, alloc_queuepair);
943 case IOCTL_VMCI_QUEUEPAIR_SETVA:
944 VMCI_DO_IOCTL(QUEUEPAIR_SETVA, queuepair_setva);
945 case IOCTL_VMCI_QUEUEPAIR_SETPAGEFILE:
946 VMCI_DO_IOCTL(QUEUEPAIR_SETPAGEFILE, queuepair_setpf);
947 case IOCTL_VMCI_QUEUEPAIR_DETACH:
948 VMCI_DO_IOCTL(QUEUEPAIR_DETACH, qp_detach);
949 case IOCTL_VMCI_CTX_ADD_NOTIFICATION:
950 VMCI_DO_IOCTL(CTX_ADD_NOTIFICATION, ctx_add_notify);
951 case IOCTL_VMCI_CTX_REMOVE_NOTIFICATION:
952 VMCI_DO_IOCTL(CTX_REMOVE_NOTIFICATION, ctx_remove_notify);
953 case IOCTL_VMCI_CTX_GET_CPT_STATE:
954 VMCI_DO_IOCTL(CTX_GET_CPT_STATE, ctx_get_cpt_state);
955 case IOCTL_VMCI_CTX_SET_CPT_STATE:
956 VMCI_DO_IOCTL(CTX_SET_CPT_STATE, ctx_set_cpt_state);
957 case IOCTL_VMCI_GET_CONTEXT_ID:
958 VMCI_DO_IOCTL(GET_CONTEXT_ID, get_context_id);
959 case IOCTL_VMCI_SET_NOTIFY:
960 VMCI_DO_IOCTL(SET_NOTIFY, set_notify);
961 case IOCTL_VMCI_NOTIFY_RESOURCE:
962 VMCI_DO_IOCTL(NOTIFY_RESOURCE, notify_resource);
963 case IOCTL_VMCI_NOTIFICATIONS_RECEIVE:
964 VMCI_DO_IOCTL(NOTIFICATIONS_RECEIVE, recv_notifications);
965
966 case IOCTL_VMCI_VERSION:
967 case IOCTL_VMCI_VERSION2:
968 return vmci_host_get_version(vmci_host_dev, iocmd, uptr);
969
970 default:
971 pr_devel("%s: Unknown ioctl (iocmd=%d)\n", __func__, iocmd);
972 return -EINVAL;
973 }
974
975#undef VMCI_DO_IOCTL
976}
977
978static const struct file_operations vmuser_fops = {
979 .owner = THIS_MODULE,
980 .open = vmci_host_open,
981 .release = vmci_host_close,
982 .poll = vmci_host_poll,
983 .unlocked_ioctl = vmci_host_unlocked_ioctl,
984 .compat_ioctl = vmci_host_unlocked_ioctl,
985};
986
987static struct miscdevice vmci_host_miscdev = {
988 .name = "vmci",
989 .minor = MISC_DYNAMIC_MINOR,
990 .fops = &vmuser_fops,
991};
992
993int __init vmci_host_init(void)
994{
995 int error;
996
997 host_context = vmci_ctx_create(VMCI_HOST_CONTEXT_ID,
998 VMCI_DEFAULT_PROC_PRIVILEGE_FLAGS,
999 -1, VMCI_VERSION, NULL);
1000 if (IS_ERR(host_context)) {
1001 error = PTR_ERR(host_context);
1002 pr_warn("Failed to initialize VMCIContext (error%d)\n",
1003 error);
1004 return error;
1005 }
1006
1007 error = misc_register(&vmci_host_miscdev);
1008 if (error) {
1009 pr_warn("Module registration error (name=%s, major=%d, minor=%d, err=%d)\n",
1010 vmci_host_miscdev.name,
1011 MISC_MAJOR, vmci_host_miscdev.minor,
1012 error);
1013 pr_warn("Unable to initialize host personality\n");
1014 vmci_ctx_destroy(host_context);
1015 return error;
1016 }
1017
1018 pr_info("VMCI host device registered (name=%s, major=%d, minor=%d)\n",
1019 vmci_host_miscdev.name, MISC_MAJOR, vmci_host_miscdev.minor);
1020
1021 vmci_host_device_initialized = true;
1022 return 0;
1023}
1024
1025void __exit vmci_host_exit(void)
1026{
1027 int error;
1028
1029 vmci_host_device_initialized = false;
1030
1031 error = misc_deregister(&vmci_host_miscdev);
1032 if (error)
1033 pr_warn("Error unregistering character device: %d\n", error);
1034
1035 vmci_ctx_destroy(host_context);
1036 vmci_qp_broker_exit();
1037
1038 pr_debug("VMCI host driver module unloaded\n");
1039}
1040