4 * Copyright (C) 2012 VMware, Inc. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation version 2 and no later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
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>
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"
45 #define VMCI_UTIL_NUM_RESOURCES 1
48 VMCI_NOTIFY_RESOURCE_QUEUE_PAIR
= 0,
49 VMCI_NOTIFY_RESOURCE_DOOR_BELL
= 1,
53 VMCI_NOTIFY_RESOURCE_ACTION_NOTIFY
= 0,
54 VMCI_NOTIFY_RESOURCE_ACTION_CREATE
= 1,
55 VMCI_NOTIFY_RESOURCE_ACTION_DESTROY
= 2,
59 * VMCI driver initialization. This block can also be used to
60 * pass initial group membership etc.
62 struct vmci_init_blk
{
67 /* VMCIqueue_pairAllocInfo_VMToVM */
68 struct vmci_qp_alloc_info_vmvm
{
69 struct vmci_handle handle
;
74 u64 produce_page_file
; /* User VA. */
75 u64 consume_page_file
; /* User VA. */
76 u64 produce_page_file_size
; /* Size of the file name array. */
77 u64 consume_page_file_size
; /* Size of the file name array. */
82 /* VMCISetNotifyInfo: Used to pass notify flag's address to the host driver. */
83 struct vmci_set_notify_info
{
90 * Per-instance host state
92 struct vmci_host_dev
{
93 struct vmci_ctx
*context
;
95 enum vmci_obj_type ct_type
;
96 struct mutex lock
; /* Mutex lock for vmci context access */
99 static struct vmci_ctx
*host_context
;
100 static bool vmci_host_device_initialized
;
101 static atomic_t vmci_host_active_users
= ATOMIC_INIT(0);
104 * Determines whether the VMCI host personality is
105 * available. Since the core functionality of the host driver is
106 * always present, all guests could possibly use the host
107 * personality. However, to minimize the deviation from the
108 * pre-unified driver state of affairs, we only consider the host
109 * device active if there is no active guest device or if there
110 * are VMX'en with active VMCI contexts using the host device.
112 bool vmci_host_code_active(void)
114 return vmci_host_device_initialized
&&
115 (!vmci_guest_code_active() ||
116 atomic_read(&vmci_host_active_users
) > 0);
120 * Called on open of /dev/vmci.
122 static int vmci_host_open(struct inode
*inode
, struct file
*filp
)
124 struct vmci_host_dev
*vmci_host_dev
;
126 vmci_host_dev
= kzalloc(sizeof(struct vmci_host_dev
), GFP_KERNEL
);
127 if (vmci_host_dev
== NULL
)
130 vmci_host_dev
->ct_type
= VMCIOBJ_NOT_SET
;
131 mutex_init(&vmci_host_dev
->lock
);
132 filp
->private_data
= vmci_host_dev
;
138 * Called on close of /dev/vmci, most often when the process
141 static int vmci_host_close(struct inode
*inode
, struct file
*filp
)
143 struct vmci_host_dev
*vmci_host_dev
= filp
->private_data
;
145 if (vmci_host_dev
->ct_type
== VMCIOBJ_CONTEXT
) {
146 vmci_ctx_destroy(vmci_host_dev
->context
);
147 vmci_host_dev
->context
= NULL
;
150 * The number of active contexts is used to track whether any
151 * VMX'en are using the host personality. It is incremented when
152 * a context is created through the IOCTL_VMCI_INIT_CONTEXT
155 atomic_dec(&vmci_host_active_users
);
157 vmci_host_dev
->ct_type
= VMCIOBJ_NOT_SET
;
159 kfree(vmci_host_dev
);
160 filp
->private_data
= NULL
;
165 * This is used to wake up the VMX when a VMCI call arrives, or
166 * to wake up select() or poll() at the next clock tick.
168 static unsigned int vmci_host_poll(struct file
*filp
, poll_table
*wait
)
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;
174 if (vmci_host_dev
->ct_type
== VMCIOBJ_CONTEXT
) {
175 /* Check for VMCI calls to this VM context. */
177 poll_wait(filp
, &context
->host_context
.wait_queue
,
180 spin_lock(&context
->lock
);
181 if (context
->pending_datagrams
> 0 ||
182 vmci_handle_arr_get_size(
183 context
->pending_doorbell_array
) > 0) {
186 spin_unlock(&context
->lock
);
192 * Copies the handles of a handle array into a user buffer, and
193 * returns the new length in userBufferSize. If the copy to the
194 * user buffer fails, the functions still returns VMCI_SUCCESS,
197 static int drv_cp_harray_to_user(void __user
*user_buf_uva
,
199 struct vmci_handle_arr
*handle_array
,
203 struct vmci_handle
*handles
;
206 array_size
= vmci_handle_arr_get_size(handle_array
);
208 if (array_size
* sizeof(*handles
) > *user_buf_size
)
209 return VMCI_ERROR_MORE_DATA
;
211 *user_buf_size
= array_size
* sizeof(*handles
);
213 *retval
= copy_to_user(user_buf_uva
,
214 vmci_handle_arr_get_handles
215 (handle_array
), *user_buf_size
);
221 * Sets up a given context for notify to work. Maps the notify
222 * boolean in user VA into kernel space.
224 static int vmci_host_setup_notify(struct vmci_ctx
*context
,
229 if (context
->notify_page
) {
230 pr_devel("%s: Notify mechanism is already set up\n", __func__
);
231 return VMCI_ERROR_DUPLICATE_ENTRY
;
235 * We are using 'bool' internally, but let's make sure we explicit
238 BUILD_BUG_ON(sizeof(bool) != sizeof(u8
));
239 if (!access_ok(VERIFY_WRITE
, (void __user
*)uva
, sizeof(u8
)))
240 return VMCI_ERROR_GENERIC
;
243 * Lock physical page backing a given user VA.
245 retval
= get_user_pages_fast(uva
, 1, 1, &context
->notify_page
);
247 context
->notify_page
= NULL
;
248 return VMCI_ERROR_GENERIC
;
252 * Map the locked page and set up notify pointer.
254 context
->notify
= kmap(context
->notify_page
) + (uva
& (PAGE_SIZE
- 1));
255 vmci_ctx_check_signal_notify(context
);
260 static int vmci_host_get_version(struct vmci_host_dev
*vmci_host_dev
,
261 unsigned int cmd
, void __user
*uptr
)
263 if (cmd
== IOCTL_VMCI_VERSION2
) {
264 int __user
*vptr
= uptr
;
265 if (get_user(vmci_host_dev
->user_version
, vptr
))
270 * The basic logic here is:
272 * If the user sends in a version of 0 tell it our version.
273 * If the user didn't send in a version, tell it our version.
274 * If the user sent in an old version, tell it -its- version.
275 * If the user sent in an newer version, tell it our version.
277 * The rationale behind telling the caller its version is that
278 * Workstation 6.5 required that VMX and VMCI kernel module were
279 * version sync'd. All new VMX users will be programmed to
280 * handle the VMCI kernel module version.
283 if (vmci_host_dev
->user_version
> 0 &&
284 vmci_host_dev
->user_version
< VMCI_VERSION_HOSTQP
) {
285 return vmci_host_dev
->user_version
;
291 #define vmci_ioctl_err(fmt, ...) \
292 pr_devel("%s: " fmt, ioctl_name, ##__VA_ARGS__)
294 static int vmci_host_do_init_context(struct vmci_host_dev
*vmci_host_dev
,
295 const char *ioctl_name
,
298 struct vmci_init_blk init_block
;
299 const struct cred
*cred
;
302 if (copy_from_user(&init_block
, uptr
, sizeof(init_block
))) {
303 vmci_ioctl_err("error reading init block\n");
307 mutex_lock(&vmci_host_dev
->lock
);
309 if (vmci_host_dev
->ct_type
!= VMCIOBJ_NOT_SET
) {
310 vmci_ioctl_err("received VMCI init on initialized handle\n");
315 if (init_block
.flags
& ~VMCI_PRIVILEGE_FLAG_RESTRICTED
) {
316 vmci_ioctl_err("unsupported VMCI restriction flag\n");
321 cred
= get_current_cred();
322 vmci_host_dev
->context
= vmci_ctx_create(init_block
.cid
,
324 vmci_host_dev
->user_version
,
327 if (IS_ERR(vmci_host_dev
->context
)) {
328 retval
= PTR_ERR(vmci_host_dev
->context
);
329 vmci_ioctl_err("error initializing context\n");
334 * Copy cid to userlevel, we do this to allow the VMX
335 * to enforce its policy on cid generation.
337 init_block
.cid
= vmci_ctx_get_id(vmci_host_dev
->context
);
338 if (copy_to_user(uptr
, &init_block
, sizeof(init_block
))) {
339 vmci_ctx_destroy(vmci_host_dev
->context
);
340 vmci_host_dev
->context
= NULL
;
341 vmci_ioctl_err("error writing init block\n");
346 vmci_host_dev
->ct_type
= VMCIOBJ_CONTEXT
;
347 atomic_inc(&vmci_host_active_users
);
352 mutex_unlock(&vmci_host_dev
->lock
);
356 static int vmci_host_do_send_datagram(struct vmci_host_dev
*vmci_host_dev
,
357 const char *ioctl_name
,
360 struct vmci_datagram_snd_rcv_info send_info
;
361 struct vmci_datagram
*dg
= NULL
;
364 if (vmci_host_dev
->ct_type
!= VMCIOBJ_CONTEXT
) {
365 vmci_ioctl_err("only valid for contexts\n");
369 if (copy_from_user(&send_info
, uptr
, sizeof(send_info
)))
372 if (send_info
.len
> VMCI_MAX_DG_SIZE
) {
373 vmci_ioctl_err("datagram is too big (size=%d)\n",
378 if (send_info
.len
< sizeof(*dg
)) {
379 vmci_ioctl_err("datagram is too small (size=%d)\n",
384 dg
= memdup_user((void __user
*)(uintptr_t)send_info
.addr
,
388 "cannot allocate memory to dispatch datagram\n");
392 if (VMCI_DG_SIZE(dg
) != send_info
.len
) {
393 vmci_ioctl_err("datagram size mismatch\n");
398 pr_devel("Datagram dst (handle=0x%x:0x%x) src (handle=0x%x:0x%x), payload (size=%llu bytes)\n",
399 dg
->dst
.context
, dg
->dst
.resource
,
400 dg
->src
.context
, dg
->src
.resource
,
401 (unsigned long long)dg
->payload_size
);
403 /* Get source context id. */
404 cid
= vmci_ctx_get_id(vmci_host_dev
->context
);
405 send_info
.result
= vmci_datagram_dispatch(cid
, dg
, true);
408 return copy_to_user(uptr
, &send_info
, sizeof(send_info
)) ? -EFAULT
: 0;
411 static int vmci_host_do_receive_datagram(struct vmci_host_dev
*vmci_host_dev
,
412 const char *ioctl_name
,
415 struct vmci_datagram_snd_rcv_info recv_info
;
416 struct vmci_datagram
*dg
= NULL
;
420 if (vmci_host_dev
->ct_type
!= VMCIOBJ_CONTEXT
) {
421 vmci_ioctl_err("only valid for contexts\n");
425 if (copy_from_user(&recv_info
, uptr
, sizeof(recv_info
)))
428 size
= recv_info
.len
;
429 recv_info
.result
= vmci_ctx_dequeue_datagram(vmci_host_dev
->context
,
432 if (recv_info
.result
>= VMCI_SUCCESS
) {
433 void __user
*ubuf
= (void __user
*)(uintptr_t)recv_info
.addr
;
434 retval
= copy_to_user(ubuf
, dg
, VMCI_DG_SIZE(dg
));
440 return copy_to_user(uptr
, &recv_info
, sizeof(recv_info
)) ? -EFAULT
: 0;
443 static int vmci_host_do_alloc_queuepair(struct vmci_host_dev
*vmci_host_dev
,
444 const char *ioctl_name
,
447 struct vmci_handle handle
;
452 if (vmci_host_dev
->ct_type
!= VMCIOBJ_CONTEXT
) {
453 vmci_ioctl_err("only valid for contexts\n");
457 cid
= vmci_ctx_get_id(vmci_host_dev
->context
);
459 if (vmci_host_dev
->user_version
< VMCI_VERSION_NOVMVM
) {
460 struct vmci_qp_alloc_info_vmvm alloc_info
;
461 struct vmci_qp_alloc_info_vmvm __user
*info
= uptr
;
463 if (copy_from_user(&alloc_info
, uptr
, sizeof(alloc_info
)))
466 handle
= alloc_info
.handle
;
467 retptr
= &info
->result
;
469 vmci_status
= vmci_qp_broker_alloc(alloc_info
.handle
,
472 VMCI_NO_PRIVILEGE_FLAGS
,
473 alloc_info
.produce_size
,
474 alloc_info
.consume_size
,
476 vmci_host_dev
->context
);
478 if (vmci_status
== VMCI_SUCCESS
)
479 vmci_status
= VMCI_SUCCESS_QUEUEPAIR_CREATE
;
481 struct vmci_qp_alloc_info alloc_info
;
482 struct vmci_qp_alloc_info __user
*info
= uptr
;
483 struct vmci_qp_page_store page_store
;
485 if (copy_from_user(&alloc_info
, uptr
, sizeof(alloc_info
)))
488 handle
= alloc_info
.handle
;
489 retptr
= &info
->result
;
491 page_store
.pages
= alloc_info
.ppn_va
;
492 page_store
.len
= alloc_info
.num_ppns
;
494 vmci_status
= vmci_qp_broker_alloc(alloc_info
.handle
,
497 VMCI_NO_PRIVILEGE_FLAGS
,
498 alloc_info
.produce_size
,
499 alloc_info
.consume_size
,
501 vmci_host_dev
->context
);
504 if (put_user(vmci_status
, retptr
)) {
505 if (vmci_status
>= VMCI_SUCCESS
) {
506 vmci_status
= vmci_qp_broker_detach(handle
,
507 vmci_host_dev
->context
);
515 static int vmci_host_do_queuepair_setva(struct vmci_host_dev
*vmci_host_dev
,
516 const char *ioctl_name
,
519 struct vmci_qp_set_va_info set_va_info
;
520 struct vmci_qp_set_va_info __user
*info
= uptr
;
523 if (vmci_host_dev
->ct_type
!= VMCIOBJ_CONTEXT
) {
524 vmci_ioctl_err("only valid for contexts\n");
528 if (vmci_host_dev
->user_version
< VMCI_VERSION_NOVMVM
) {
529 vmci_ioctl_err("is not allowed\n");
533 if (copy_from_user(&set_va_info
, uptr
, sizeof(set_va_info
)))
536 if (set_va_info
.va
) {
538 * VMX is passing down a new VA for the queue
541 result
= vmci_qp_broker_map(set_va_info
.handle
,
542 vmci_host_dev
->context
,
546 * The queue pair is about to be unmapped by
549 result
= vmci_qp_broker_unmap(set_va_info
.handle
,
550 vmci_host_dev
->context
, 0);
553 return put_user(result
, &info
->result
) ? -EFAULT
: 0;
556 static int vmci_host_do_queuepair_setpf(struct vmci_host_dev
*vmci_host_dev
,
557 const char *ioctl_name
,
560 struct vmci_qp_page_file_info page_file_info
;
561 struct vmci_qp_page_file_info __user
*info
= uptr
;
564 if (vmci_host_dev
->user_version
< VMCI_VERSION_HOSTQP
||
565 vmci_host_dev
->user_version
>= VMCI_VERSION_NOVMVM
) {
566 vmci_ioctl_err("not supported on this VMX (version=%d)\n",
567 vmci_host_dev
->user_version
);
571 if (vmci_host_dev
->ct_type
!= VMCIOBJ_CONTEXT
) {
572 vmci_ioctl_err("only valid for contexts\n");
576 if (copy_from_user(&page_file_info
, uptr
, sizeof(*info
)))
580 * Communicate success pre-emptively to the caller. Note that the
581 * basic premise is that it is incumbent upon the caller not to look at
582 * the info.result field until after the ioctl() returns. And then,
583 * only if the ioctl() result indicates no error. We send up the
584 * SUCCESS status before calling SetPageStore() store because failing
585 * to copy up the result code means unwinding the SetPageStore().
587 * It turns out the logic to unwind a SetPageStore() opens a can of
588 * worms. For example, if a host had created the queue_pair and a
589 * guest attaches and SetPageStore() is successful but writing success
590 * fails, then ... the host has to be stopped from writing (anymore)
591 * data into the queue_pair. That means an additional test in the
592 * VMCI_Enqueue() code path. Ugh.
595 if (put_user(VMCI_SUCCESS
, &info
->result
)) {
597 * In this case, we can't write a result field of the
598 * caller's info block. So, we don't even try to
604 result
= vmci_qp_broker_set_page_store(page_file_info
.handle
,
605 page_file_info
.produce_va
,
606 page_file_info
.consume_va
,
607 vmci_host_dev
->context
);
608 if (result
< VMCI_SUCCESS
) {
609 if (put_user(result
, &info
->result
)) {
611 * Note that in this case the SetPageStore()
612 * call failed but we were unable to
613 * communicate that to the caller (because the
614 * copy_to_user() call failed). So, if we
615 * simply return an error (in this case
616 * -EFAULT) then the caller will know that the
617 * SetPageStore failed even though we couldn't
618 * put the result code in the result field and
619 * indicate exactly why it failed.
621 * That says nothing about the issue where we
622 * were once able to write to the caller's info
623 * memory and now can't. Something more
624 * serious is probably going on than the fact
625 * that SetPageStore() didn't work.
634 static int vmci_host_do_qp_detach(struct vmci_host_dev
*vmci_host_dev
,
635 const char *ioctl_name
,
638 struct vmci_qp_dtch_info detach_info
;
639 struct vmci_qp_dtch_info __user
*info
= uptr
;
642 if (vmci_host_dev
->ct_type
!= VMCIOBJ_CONTEXT
) {
643 vmci_ioctl_err("only valid for contexts\n");
647 if (copy_from_user(&detach_info
, uptr
, sizeof(detach_info
)))
650 result
= vmci_qp_broker_detach(detach_info
.handle
,
651 vmci_host_dev
->context
);
652 if (result
== VMCI_SUCCESS
&&
653 vmci_host_dev
->user_version
< VMCI_VERSION_NOVMVM
) {
654 result
= VMCI_SUCCESS_LAST_DETACH
;
657 return put_user(result
, &info
->result
) ? -EFAULT
: 0;
660 static int vmci_host_do_ctx_add_notify(struct vmci_host_dev
*vmci_host_dev
,
661 const char *ioctl_name
,
664 struct vmci_ctx_info ar_info
;
665 struct vmci_ctx_info __user
*info
= uptr
;
669 if (vmci_host_dev
->ct_type
!= VMCIOBJ_CONTEXT
) {
670 vmci_ioctl_err("only valid for contexts\n");
674 if (copy_from_user(&ar_info
, uptr
, sizeof(ar_info
)))
677 cid
= vmci_ctx_get_id(vmci_host_dev
->context
);
678 result
= vmci_ctx_add_notification(cid
, ar_info
.remote_cid
);
680 return put_user(result
, &info
->result
) ? -EFAULT
: 0;
683 static int vmci_host_do_ctx_remove_notify(struct vmci_host_dev
*vmci_host_dev
,
684 const char *ioctl_name
,
687 struct vmci_ctx_info ar_info
;
688 struct vmci_ctx_info __user
*info
= uptr
;
692 if (vmci_host_dev
->ct_type
!= VMCIOBJ_CONTEXT
) {
693 vmci_ioctl_err("only valid for contexts\n");
697 if (copy_from_user(&ar_info
, uptr
, sizeof(ar_info
)))
700 cid
= vmci_ctx_get_id(vmci_host_dev
->context
);
701 result
= vmci_ctx_remove_notification(cid
,
704 return put_user(result
, &info
->result
) ? -EFAULT
: 0;
707 static int vmci_host_do_ctx_get_cpt_state(struct vmci_host_dev
*vmci_host_dev
,
708 const char *ioctl_name
,
711 struct vmci_ctx_chkpt_buf_info get_info
;
716 if (vmci_host_dev
->ct_type
!= VMCIOBJ_CONTEXT
) {
717 vmci_ioctl_err("only valid for contexts\n");
721 if (copy_from_user(&get_info
, uptr
, sizeof(get_info
)))
724 cid
= vmci_ctx_get_id(vmci_host_dev
->context
);
725 get_info
.result
= vmci_ctx_get_chkpt_state(cid
, get_info
.cpt_type
,
726 &get_info
.buf_size
, &cpt_buf
);
727 if (get_info
.result
== VMCI_SUCCESS
&& get_info
.buf_size
) {
728 void __user
*ubuf
= (void __user
*)(uintptr_t)get_info
.cpt_buf
;
729 retval
= copy_to_user(ubuf
, cpt_buf
, get_info
.buf_size
);
736 return copy_to_user(uptr
, &get_info
, sizeof(get_info
)) ? -EFAULT
: 0;
739 static int vmci_host_do_ctx_set_cpt_state(struct vmci_host_dev
*vmci_host_dev
,
740 const char *ioctl_name
,
743 struct vmci_ctx_chkpt_buf_info set_info
;
748 if (vmci_host_dev
->ct_type
!= VMCIOBJ_CONTEXT
) {
749 vmci_ioctl_err("only valid for contexts\n");
753 if (copy_from_user(&set_info
, uptr
, sizeof(set_info
)))
756 cpt_buf
= kmalloc(set_info
.buf_size
, GFP_KERNEL
);
759 "cannot allocate memory to set cpt state (type=%d)\n",
764 if (copy_from_user(cpt_buf
, (void __user
*)(uintptr_t)set_info
.cpt_buf
,
765 set_info
.buf_size
)) {
770 cid
= vmci_ctx_get_id(vmci_host_dev
->context
);
771 set_info
.result
= vmci_ctx_set_chkpt_state(cid
, set_info
.cpt_type
,
772 set_info
.buf_size
, cpt_buf
);
774 retval
= copy_to_user(uptr
, &set_info
, sizeof(set_info
)) ? -EFAULT
: 0;
781 static int vmci_host_do_get_context_id(struct vmci_host_dev
*vmci_host_dev
,
782 const char *ioctl_name
,
785 u32 __user
*u32ptr
= uptr
;
787 return put_user(VMCI_HOST_CONTEXT_ID
, u32ptr
) ? -EFAULT
: 0;
790 static int vmci_host_do_set_notify(struct vmci_host_dev
*vmci_host_dev
,
791 const char *ioctl_name
,
794 struct vmci_set_notify_info notify_info
;
796 if (vmci_host_dev
->ct_type
!= VMCIOBJ_CONTEXT
) {
797 vmci_ioctl_err("only valid for contexts\n");
801 if (copy_from_user(¬ify_info
, uptr
, sizeof(notify_info
)))
804 if (notify_info
.notify_uva
) {
806 vmci_host_setup_notify(vmci_host_dev
->context
,
807 notify_info
.notify_uva
);
809 vmci_ctx_unset_notify(vmci_host_dev
->context
);
810 notify_info
.result
= VMCI_SUCCESS
;
813 return copy_to_user(uptr
, ¬ify_info
, sizeof(notify_info
)) ?
817 static int vmci_host_do_notify_resource(struct vmci_host_dev
*vmci_host_dev
,
818 const char *ioctl_name
,
821 struct vmci_dbell_notify_resource_info info
;
824 if (vmci_host_dev
->user_version
< VMCI_VERSION_NOTIFY
) {
825 vmci_ioctl_err("invalid for current VMX versions\n");
829 if (vmci_host_dev
->ct_type
!= VMCIOBJ_CONTEXT
) {
830 vmci_ioctl_err("only valid for contexts\n");
834 if (copy_from_user(&info
, uptr
, sizeof(info
)))
837 cid
= vmci_ctx_get_id(vmci_host_dev
->context
);
839 switch (info
.action
) {
840 case VMCI_NOTIFY_RESOURCE_ACTION_NOTIFY
:
841 if (info
.resource
== VMCI_NOTIFY_RESOURCE_DOOR_BELL
) {
842 u32 flags
= VMCI_NO_PRIVILEGE_FLAGS
;
843 info
.result
= vmci_ctx_notify_dbell(cid
, info
.handle
,
846 info
.result
= VMCI_ERROR_UNAVAILABLE
;
850 case VMCI_NOTIFY_RESOURCE_ACTION_CREATE
:
851 info
.result
= vmci_ctx_dbell_create(cid
, info
.handle
);
854 case VMCI_NOTIFY_RESOURCE_ACTION_DESTROY
:
855 info
.result
= vmci_ctx_dbell_destroy(cid
, info
.handle
);
859 vmci_ioctl_err("got unknown action (action=%d)\n",
861 info
.result
= VMCI_ERROR_INVALID_ARGS
;
864 return copy_to_user(uptr
, &info
, sizeof(info
)) ? -EFAULT
: 0;
867 static int vmci_host_do_recv_notifications(struct vmci_host_dev
*vmci_host_dev
,
868 const char *ioctl_name
,
871 struct vmci_ctx_notify_recv_info info
;
872 struct vmci_handle_arr
*db_handle_array
;
873 struct vmci_handle_arr
*qp_handle_array
;
878 if (vmci_host_dev
->ct_type
!= VMCIOBJ_CONTEXT
) {
879 vmci_ioctl_err("only valid for contexts\n");
883 if (vmci_host_dev
->user_version
< VMCI_VERSION_NOTIFY
) {
884 vmci_ioctl_err("not supported for the current vmx version\n");
888 if (copy_from_user(&info
, uptr
, sizeof(info
)))
891 if ((info
.db_handle_buf_size
&& !info
.db_handle_buf_uva
) ||
892 (info
.qp_handle_buf_size
&& !info
.qp_handle_buf_uva
)) {
896 cid
= vmci_ctx_get_id(vmci_host_dev
->context
);
898 info
.result
= vmci_ctx_rcv_notifications_get(cid
,
899 &db_handle_array
, &qp_handle_array
);
900 if (info
.result
!= VMCI_SUCCESS
)
901 return copy_to_user(uptr
, &info
, sizeof(info
)) ? -EFAULT
: 0;
903 ubuf
= (void __user
*)(uintptr_t)info
.db_handle_buf_uva
;
904 info
.result
= drv_cp_harray_to_user(ubuf
, &info
.db_handle_buf_size
,
905 db_handle_array
, &retval
);
906 if (info
.result
== VMCI_SUCCESS
&& !retval
) {
907 ubuf
= (void __user
*)(uintptr_t)info
.qp_handle_buf_uva
;
908 info
.result
= drv_cp_harray_to_user(ubuf
,
909 &info
.qp_handle_buf_size
,
910 qp_handle_array
, &retval
);
913 if (!retval
&& copy_to_user(uptr
, &info
, sizeof(info
)))
916 vmci_ctx_rcv_notifications_release(cid
,
917 db_handle_array
, qp_handle_array
,
918 info
.result
== VMCI_SUCCESS
&& !retval
);
923 static long vmci_host_unlocked_ioctl(struct file
*filp
,
924 unsigned int iocmd
, unsigned long ioarg
)
926 #define VMCI_DO_IOCTL(ioctl_name, ioctl_fn) do { \
927 char *name = __stringify(IOCTL_VMCI_ ## ioctl_name); \
928 return vmci_host_do_ ## ioctl_fn( \
929 vmci_host_dev, name, uptr); \
932 struct vmci_host_dev
*vmci_host_dev
= filp
->private_data
;
933 void __user
*uptr
= (void __user
*)ioarg
;
936 case IOCTL_VMCI_INIT_CONTEXT
:
937 VMCI_DO_IOCTL(INIT_CONTEXT
, init_context
);
938 case IOCTL_VMCI_DATAGRAM_SEND
:
939 VMCI_DO_IOCTL(DATAGRAM_SEND
, send_datagram
);
940 case IOCTL_VMCI_DATAGRAM_RECEIVE
:
941 VMCI_DO_IOCTL(DATAGRAM_RECEIVE
, receive_datagram
);
942 case IOCTL_VMCI_QUEUEPAIR_ALLOC
:
943 VMCI_DO_IOCTL(QUEUEPAIR_ALLOC
, alloc_queuepair
);
944 case IOCTL_VMCI_QUEUEPAIR_SETVA
:
945 VMCI_DO_IOCTL(QUEUEPAIR_SETVA
, queuepair_setva
);
946 case IOCTL_VMCI_QUEUEPAIR_SETPAGEFILE
:
947 VMCI_DO_IOCTL(QUEUEPAIR_SETPAGEFILE
, queuepair_setpf
);
948 case IOCTL_VMCI_QUEUEPAIR_DETACH
:
949 VMCI_DO_IOCTL(QUEUEPAIR_DETACH
, qp_detach
);
950 case IOCTL_VMCI_CTX_ADD_NOTIFICATION
:
951 VMCI_DO_IOCTL(CTX_ADD_NOTIFICATION
, ctx_add_notify
);
952 case IOCTL_VMCI_CTX_REMOVE_NOTIFICATION
:
953 VMCI_DO_IOCTL(CTX_REMOVE_NOTIFICATION
, ctx_remove_notify
);
954 case IOCTL_VMCI_CTX_GET_CPT_STATE
:
955 VMCI_DO_IOCTL(CTX_GET_CPT_STATE
, ctx_get_cpt_state
);
956 case IOCTL_VMCI_CTX_SET_CPT_STATE
:
957 VMCI_DO_IOCTL(CTX_SET_CPT_STATE
, ctx_set_cpt_state
);
958 case IOCTL_VMCI_GET_CONTEXT_ID
:
959 VMCI_DO_IOCTL(GET_CONTEXT_ID
, get_context_id
);
960 case IOCTL_VMCI_SET_NOTIFY
:
961 VMCI_DO_IOCTL(SET_NOTIFY
, set_notify
);
962 case IOCTL_VMCI_NOTIFY_RESOURCE
:
963 VMCI_DO_IOCTL(NOTIFY_RESOURCE
, notify_resource
);
964 case IOCTL_VMCI_NOTIFICATIONS_RECEIVE
:
965 VMCI_DO_IOCTL(NOTIFICATIONS_RECEIVE
, recv_notifications
);
967 case IOCTL_VMCI_VERSION
:
968 case IOCTL_VMCI_VERSION2
:
969 return vmci_host_get_version(vmci_host_dev
, iocmd
, uptr
);
972 pr_devel("%s: Unknown ioctl (iocmd=%d)\n", __func__
, iocmd
);
979 static const struct file_operations vmuser_fops
= {
980 .owner
= THIS_MODULE
,
981 .open
= vmci_host_open
,
982 .release
= vmci_host_close
,
983 .poll
= vmci_host_poll
,
984 .unlocked_ioctl
= vmci_host_unlocked_ioctl
,
985 .compat_ioctl
= vmci_host_unlocked_ioctl
,
988 static struct miscdevice vmci_host_miscdev
= {
990 .minor
= MISC_DYNAMIC_MINOR
,
991 .fops
= &vmuser_fops
,
994 int __init
vmci_host_init(void)
998 host_context
= vmci_ctx_create(VMCI_HOST_CONTEXT_ID
,
999 VMCI_DEFAULT_PROC_PRIVILEGE_FLAGS
,
1000 -1, VMCI_VERSION
, NULL
);
1001 if (IS_ERR(host_context
)) {
1002 error
= PTR_ERR(host_context
);
1003 pr_warn("Failed to initialize VMCIContext (error%d)\n",
1008 error
= misc_register(&vmci_host_miscdev
);
1010 pr_warn("Module registration error (name=%s, major=%d, minor=%d, err=%d)\n",
1011 vmci_host_miscdev
.name
,
1012 MISC_MAJOR
, vmci_host_miscdev
.minor
,
1014 pr_warn("Unable to initialize host personality\n");
1015 vmci_ctx_destroy(host_context
);
1019 pr_info("VMCI host device registered (name=%s, major=%d, minor=%d)\n",
1020 vmci_host_miscdev
.name
, MISC_MAJOR
, vmci_host_miscdev
.minor
);
1022 vmci_host_device_initialized
= true;
1026 void __exit
vmci_host_exit(void)
1028 vmci_host_device_initialized
= false;
1030 misc_deregister(&vmci_host_miscdev
);
1031 vmci_ctx_destroy(host_context
);
1032 vmci_qp_broker_exit();
1034 pr_debug("VMCI host driver module unloaded\n");