VMCI for Linux
Subsystems (4)
cover VMCI for Linux
In an effort to improve the out-of-the-box experience with Linux kernels for VMware users, VMware is working on readying the Virtual Machine Communication Interface (vmw_vmci) and VMCI Sockets (vmw_vsock) kernel modules for inclusion in the Linux kernel. The purpose of this post is to acquire feedback on the vmw_vmci kernel module. The vmw_vsock kernel module will be presented in a later post.
VMCI allows virtual machines to communicate with host kernel modules and the VMware hypervisors. User level applications both in a virtual machine and on the host can use vmw_vmci through VMCI Sockets, a socket address family designed to be compatible with UDP and TCP at the interface level. Today, VMCI and VMCI Sockets are used by the VMware shared folders (HGFS) and various VMware Tools components inside the guest for zero-config, network-less access to VMware host services. In addition to this, VMware's users are using VMCI Sockets for various applications, where network access of the virtual machine is restricted or non-existent. Examples of this are VMs communicating with device proxies for proprietary hardware running as host applications and automated testing of applications running within virtual machines.
In a virtual machine, VMCI is exposed as a regular PCI device. The primary communication mechanisms supported are a point-to-point bidirectional transport based on a pair of memory-mapped queues, and asynchronous notifications in the form of datagrams and doorbells. These features are available to kernel level components such as HGFS and VMCI Sockets through the VMCI kernel API. In addition to this, the VMCI kernel API provides support for receiving events related to the state of the VMCI communication channels, and the virtual machine itself.
Outside the virtual machine, the host side support of the VMCI kernel module makes the same VMCI kernel API available to VMCI endpoints on the host. In addition to this, the host side manages each VMCI device in a virtual machine through a context object. This context object serves to identify the virtual machine for communication, and to track the resource consumption of the given VMCI device. Both operations related to communication between the virtual machine and the host kernel, and those related to the management of the VMCI device state in the host kernel, are invoked by the user level component of the hypervisor through a set of ioctls on the VMCI device node. To provide seamless support for nested virtualization, where a virtual machine may use both a VMCI PCI device to talk to its hypervisor, and the VMCI host side support to run nested virtual machines, the VMCI host and virtual machine support are combined in a single kernel module.
For additional information about the use of VMCI and in particular VMCI Sockets, please refer to the VMCI Socket Programming Guide available at https://www.vmware.com/support/developer/vmci-sdk/.
Andrew Stiegmann (stieg) (11):
Apply VMCI context code
Apply VMCI datagram code
Apply VMCI doorbell code
Apply VMCI driver code
Apply VMCI event code
Apply dynamic array code
Apply VMCI hash table
Apply VMCI queue pairs
Apply VMCI resource code
Apply vmci routing code
Apply the header code to make VMCI build
drivers/misc/Kconfig | 1 + drivers/misc/Makefile | 1 + drivers/misc/vmw_vmci/Kconfig | 16 + drivers/misc/vmw_vmci/Makefile | 43 + drivers/misc/vmw_vmci/vmci_common_int.h | 72 + drivers/misc/vmw_vmci/vmci_context.c | 1743 +++++++++++ drivers/misc/vmw_vmci/vmci_context.h | 150 + drivers/misc/vmw_vmci/vmci_datagram.c | 760 +++++ drivers/misc/vmw_vmci/vmci_datagram.h | 57 + drivers/misc/vmw_vmci/vmci_doorbell.c | 989 +++++++ drivers/misc/vmw_vmci/vmci_doorbell.h | 57 + drivers/misc/vmw_vmci/vmci_driver.c | 2875 ++++++++++++++++++ drivers/misc/vmw_vmci/vmci_driver.h | 52 + drivers/misc/vmw_vmci/vmci_event.c | 614 ++++ drivers/misc/vmw_vmci/vmci_event.h | 29 + drivers/misc/vmw_vmci/vmci_handle_array.c | 300 ++ drivers/misc/vmw_vmci/vmci_handle_array.h | 50 + drivers/misc/vmw_vmci/vmci_hash_table.c | 494 ++++ drivers/misc/vmw_vmci/vmci_hash_table.h | 56 + drivers/misc/vmw_vmci/vmci_queue_pair.c | 4517 +++++++++++++++++++++++++++++ drivers/misc/vmw_vmci/vmci_queue_pair.h | 258 ++ drivers/misc/vmw_vmci/vmci_resource.c | 320 ++ drivers/misc/vmw_vmci/vmci_resource.h | 61 + drivers/misc/vmw_vmci/vmci_route.c | 234 ++ drivers/misc/vmw_vmci/vmci_route.h | 34 + include/linux/vmw_vmci_api.h | 103 + include/linux/vmw_vmci_defs.h | 1003 +++++++ 27 files changed, 14889 insertions(+), 0 deletions(-) create mode 100644 drivers/misc/vmw_vmci/Kconfig create mode 100644 drivers/misc/vmw_vmci/Makefile create mode 100644 drivers/misc/vmw_vmci/vmci_common_int.h create mode 100644 drivers/misc/vmw_vmci/vmci_context.c create mode 100644 drivers/misc/vmw_vmci/vmci_context.h create mode 100644 drivers/misc/vmw_vmci/vmci_datagram.c create mode 100644 drivers/misc/vmw_vmci/vmci_datagram.h create mode 100644 drivers/misc/vmw_vmci/vmci_doorbell.c create mode 100644 drivers/misc/vmw_vmci/vmci_doorbell.h create mode 100644 drivers/misc/vmw_vmci/vmci_driver.c create mode 100644 drivers/misc/vmw_vmci/vmci_driver.h create mode 100644 drivers/misc/vmw_vmci/vmci_event.c create mode 100644 drivers/misc/vmw_vmci/vmci_event.h create mode 100644 drivers/misc/vmw_vmci/vmci_handle_array.c create mode 100644 drivers/misc/vmw_vmci/vmci_handle_array.h create mode 100644 drivers/misc/vmw_vmci/vmci_hash_table.c create mode 100644 drivers/misc/vmw_vmci/vmci_hash_table.h create mode 100644 drivers/misc/vmw_vmci/vmci_queue_pair.c create mode 100644 drivers/misc/vmw_vmci/vmci_queue_pair.h create mode 100644 drivers/misc/vmw_vmci/vmci_resource.c create mode 100644 drivers/misc/vmw_vmci/vmci_resource.h create mode 100644 drivers/misc/vmw_vmci/vmci_route.c create mode 100644 drivers/misc/vmw_vmci/vmci_route.h create mode 100644 include/linux/vmw_vmci_api.h create mode 100644 include/linux/vmw_vmci_defs.h
RFC 1/11 Apply VMCI context code
Context code maintains state for vmci and allows the driver to communicate with multiple VMs.
--- drivers/misc/vmw_vmci/vmci_context.c | 1743 ++++++++++++++++++++++++++++++++++ drivers/misc/vmw_vmci/vmci_context.h | 150 +++ 2 files changed, 1893 insertions(+), 0 deletions(-) create mode 100644 drivers/misc/vmw_vmci/vmci_context.c create mode 100644 drivers/misc/vmw_vmci/vmci_context.h
drivers/misc/vmw_vmci/vmci_context.c new file +1743 -0
@@ -0,0 +1,1743 @@ +/* +* VMware VMCI Driver +* +* Copyright (C) 2012 VMware, Inc. All rights reserved. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms of the GNU General Public License as published by the +* Free Software Foundation version 2 and no later version. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +* for more details. +* +* You should have received a copy of the GNU General Public License along +* with this program; if not, write to the Free Software Foundation, Inc., +* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include <linux/highmem.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/sched.h> +#include <linux/vmw_vmci_api.h> +#include <linux/vmw_vmci_defs.h> + +#include "vmci_common_int.h" +#include "vmci_context.h" +#include "vmci_datagram.h" +#include "vmci_doorbell.h" +#include "vmci_driver.h" +#include "vmci_event.h" +#include "vmci_queue_pair.h" + +/* List of current VMCI contexts. */ +static struct { +struct list_head head; +spinlock_t lock; +spinlock_t firingLock; +} ctx_list; + +/* +*---------------------------------------------------------------------- +* +* ctx_signal_notify -- +* +* Sets the notify flag to true. Assumes that the context lock is +* held. +* +* Results: +* None. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +static void ctx_signal_notify (struct vmci_ctx *context) +{ +if (context->notify) + *context->notify = true; +} + +/* +*---------------------------------------------------------------------- +* +* ctx_clear_notify -- +* +* Sets the notify flag to false. Assumes that the context lock is +* held. +* +* Results: +* None. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +static void ctx_clear_notify (struct vmci_ctx *context) +{ +if (context->notify) + *context->notify = false; +} + +/* +*---------------------------------------------------------------------- +* +* ctx_clear_notify_call -- +* +* If nothing requires the attention of the guest, clears both +* notify flag and call. +* +* Results: +* None. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +static void ctx_clear_notify_call (struct vmci_ctx *context) +{ +if (context->pendingDatagrams ==0 && +vmci_handle_arr_get_size (context->pendingDoorbellArray) ==0 ) +ctx_clear_notify (context); +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_check_signal_notify -- +* +* Sets the context's notify flag iff datagrams are pending for this +* context. Called from vmci_setup_notify(). +* +* Results: +* None. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +void vmci_ctx_check_signal_notify (struct vmci_ctx *context) +{ +ASSERT (context); + +spin_lock (&ctx_list.lock); +if (context->pendingDatagrams) +ctx_signal_notify (context); +spin_unlock (&ctx_list.lock); +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_init -- +* +* Initializes the VMCI context module. +* +* Results: +* None. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +int __init vmci_ctx_init (void ) +{ +INIT_LIST_HEAD (&ctx_list.head); + +spin_lock_init (&ctx_list.lock); +spin_lock_init (&ctx_list.firingLock); + +return VMCI_SUCCESS ; +} + +/* +*---------------------------------------------------------------------- +* +* ctx_exists_locked -- +* +* Internal helper to check if a context with the specified context +* ID exists. Assumes the ctx_list.lock is held. +* +* Results: +* true if a context exists with the given cid. +* false otherwise +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +static bool ctx_exists_locked (uint32_t cid)// IN +{ +struct vmci_ctx *context; + +list_for_each_entry (context, &ctx_list.head, listItem) { +if (context->cid == cid) +return true; + } + +return false; +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_init_ctx -- +* +* Allocates and initializes a VMCI context. +* +* Results: +* Returns 0 on success, appropriate error code otherwise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +int vmci_ctx_init_ctx (uint32_t cid, +uint32_t privFlags, +uintptr_t eventHnd, +int userVersion, +uid_t * user,struct vmci_ctx **outContext) +{ +struct vmci_ctx *context; +int result; + +if (privFlags & ~VMCI_PRIVILEGE_ALL_FLAGS ) { +pr_devel ("Invalid flag (flags=0x%x) for VMCI context." , + privFlags); +return VMCI_ERROR_INVALID_ARGS ; + } + +if (userVersion ==0 ) +return VMCI_ERROR_INVALID_ARGS ; + + context =kzalloc (sizeof *context,GFP_KERNEL ); +if (context ==NULL ) { +pr_warn ("Failed to allocate memory for VMCI context." ); +return VMCI_ERROR_NO_MEM ; + } + +INIT_LIST_HEAD (&context->listItem); +INIT_LIST_HEAD (&context->datagramQueue); + + context->userVersion = userVersion; + + context->queuePairArray =vmci_handle_arr_create (0 ); +if (!context->queuePairArray) { + result =VMCI_ERROR_NO_MEM ; + goto error; + } + + context->doorbellArray =vmci_handle_arr_create (0 ); +if (!context->doorbellArray) { + result =VMCI_ERROR_NO_MEM ; + goto error; + } + + context->pendingDoorbellArray =vmci_handle_arr_create (0 ); +if (!context->pendingDoorbellArray) { + result =VMCI_ERROR_NO_MEM ; + goto error; + } + + context->notifierArray =vmci_handle_arr_create (0 ); +if (context->notifierArray ==NULL ) { + result =VMCI_ERROR_NO_MEM ; + goto error; + } + +spin_lock_init (&context->lock); + +atomic_set (&context->refCount,1 ); + +/* Inititialize host-specific VMCI context. */ +init_waitqueue_head (&context->hostContext.waitQueue); + + context->privFlags = privFlags; + +/* +* If we collide with an existing context we generate a new and use it +* instead. The VMX will determine if regeneration is okay. Since there +* isn't 4B - 16 VMs running on a given host, the below loop will terminate. +*/ +spin_lock (&ctx_list.lock); +ASSERT (cid !=VMCI_INVALID_ID ); +while (ctx_exists_locked (cid)) { + +/* +* If the cid is below our limit and we collide we are creating duplicate +* contexts internally so we want to assert fail in that case. +*/ +ASSERT (cid >=VMCI_RESERVED_CID_LIMIT ); + +/* We reserve the lowest 16 ids for fixed contexts. */ + cid =max (cid,VMCI_RESERVED_CID_LIMIT -1 ) +1 ; +if (cid ==VMCI_INVALID_ID ) + cid =VMCI_RESERVED_CID_LIMIT ; + } +ASSERT (!ctx_exists_locked (cid)); + context->cid = cid; + context->validUser = user !=NULL ; +if (context->validUser) + context->user = *user; +list_add (&context->listItem, &ctx_list.head); +spin_unlock (&ctx_list.lock); + + context->notify =NULL ; + context->notifyPage =NULL ; + + *outContext = context; +return VMCI_SUCCESS ; + +error: +if (context->notifierArray) +vmci_handle_arr_destroy (context->notifierArray); +if (context->queuePairArray) +vmci_handle_arr_destroy (context->queuePairArray); +if (context->doorbellArray) +vmci_handle_arr_destroy (context->doorbellArray); +if (context->pendingDoorbellArray) +vmci_handle_arr_destroy (context->pendingDoorbellArray); +kfree (context); +return result; +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_release_ctx -- +* +* Cleans up a VMCI context. +* +* Results: +* None. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +void vmci_ctx_release_ctx (struct vmci_ctx *context)// IN +{ +/* Dequeue VMCI context. */ + +spin_lock (&ctx_list.lock); +list_del (&context->listItem); +spin_unlock (&ctx_list.lock); + +vmci_ctx_release (context); +} + +/* +*---------------------------------------------------------------------- +* +* ctx_fire_notification -- +* +* Fire notification for all contexts interested in given cid. +* +* Results: +* VMCI_SUCCESS on success, error code otherwise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +static int ctx_fire_notification (uint32_t contextID,// IN +uint32_t privFlags)// IN +{ +uint32_t i, arraySize; +struct vmci_ctx *subCtx; +struct vmci_handle_arr *subscriberArray; +struct vmci_handle contextHandle = +vmci_make_handle (contextID,VMCI_EVENT_HANDLER ); + +/* +* We create an array to hold the subscribers we find when scanning through +* all contexts. +*/ + subscriberArray =vmci_handle_arr_create (0 ); +if (subscriberArray ==NULL ) +return VMCI_ERROR_NO_MEM ; + +/* +* Scan all contexts to find who is interested in being notified about +* given contextID. We have a special firingLock that we use to synchronize +* across all notification operations. This avoids us having to take the +* context lock for each HasEntry call and it solves a lock ranking issue. +*/ +spin_lock (&ctx_list.firingLock); +spin_lock (&ctx_list.lock); +list_for_each_entry (subCtx, &ctx_list.head, listItem) { +/* +* We only deliver notifications of the removal of contexts, if +* the two contexts are allowed to interact. +*/ +if (vmci_handle_arr_has_entry + (subCtx->notifierArray, contextHandle) + && !vmci_deny_interaction (privFlags, subCtx->privFlags)) { +vmci_handle_arr_append_entry (&subscriberArray, +vmci_make_handle + (subCtx->cid, +VMCI_EVENT_HANDLER )); + } + } +spin_unlock (&ctx_list.lock); +spin_unlock (&ctx_list.firingLock); + +/* Fire event to all subscribers. */ + arraySize =vmci_handle_arr_get_size (subscriberArray); +for (i =0 ; i < arraySize; i++) { +int result; +struct vmci_event_msg *eMsg; +struct vmci_event_payld_ctx *evPayload; +char buf[sizeof *eMsg +sizeof *evPayload]; + + eMsg = (struct vmci_event_msg *)buf; + +/* Clear out any garbage. */ +memset (eMsg,0 ,sizeof *eMsg +sizeof *evPayload); + eMsg->hdr.dst =vmci_handle_arr_get_entry (subscriberArray, i); + eMsg->hdr.src = +vmci_make_handle (VMCI_HYPERVISOR_CONTEXT_ID , +VMCI_CONTEXT_RESOURCE_ID ); + eMsg->hdr.payloadSize = +sizeof *eMsg +sizeof *evPayload -sizeof eMsg->hdr; + eMsg->eventData.event =VMCI_EVENT_CTX_REMOVED ; + evPayload =vmci_event_data_payload (&eMsg->eventData); + evPayload->contextID = contextID; + + result =vmci_dg_dispatch (VMCI_HYPERVISOR_CONTEXT_ID , + (struct vmci_dg *) + eMsg, false); +if (result <VMCI_SUCCESS ) { +pr_devel ("Failed to enqueue event datagram " +"(type=%d) for context (ID=0x%x)." , + eMsg->eventData.event, eMsg->hdr.dst.context); +/* We continue to enqueue on next subscriber. */ + } + } +vmci_handle_arr_destroy (subscriberArray); + +return VMCI_SUCCESS ; +} + +/* +*----------------------------------------------------------------------------- +* +* ctx_free_ctx -- +* +* Deallocates all parts of a context datastructure. This +* functions doesn't lock the context, because it assumes that +* the caller is holding the last reference to context. +* +* Results: +* None. +* +* Side effects: +* Paged memory is freed. +* +*----------------------------------------------------------------------------- +*/ + +static void ctx_free_ctx (struct vmci_ctx *context)// IN +{ +struct list_head *curr; +struct list_head *next; +struct vmci_dg_queue_entry *dqEntry; +struct vmci_handle tempHandle; + +/* Fire event to all contexts interested in knowing this context is dying. */ +ctx_fire_notification (context->cid, context->privFlags); + +/* +* Cleanup all queue pair resources attached to context. If the VM dies +* without cleaning up, this code will make sure that no resources are +* leaked. +*/ + + tempHandle =vmci_handle_arr_get_entry (context->queuePairArray,0 ); +while (!VMCI_HANDLE_EQUAL (tempHandle,VMCI_INVALID_HANDLE )) { +if (vmci_qp_broker_detach (tempHandle, context) <VMCI_SUCCESS ) { +/* +* When vmci_qp_broker_detach() succeeds it removes the handle from the +* array. If detach fails, we must remove the handle ourselves. +*/ +vmci_handle_arr_remove_entry (context->queuePairArray, + tempHandle); + } + tempHandle = +vmci_handle_arr_get_entry (context->queuePairArray,0 ); + } + +/* +* It is fine to destroy this without locking the callQueue, as +* this is the only thread having a reference to the context. +*/ + +list_for_each_safe (curr, next, &context->datagramQueue) { + dqEntry = + list_entry(curr, struct vmci_dg_queue_entry, listItem); +list_del (curr); +ASSERT (dqEntry && dqEntry->dg); +ASSERT (dqEntry->dgSize ==VMCI_DG_SIZE (dqEntry->dg)); +kfree (dqEntry->dg); +kfree (dqEntry); + } + +vmci_handle_arr_destroy (context->notifierArray); +vmci_handle_arr_destroy (context->queuePairArray); +vmci_handle_arr_destroy (context->doorbellArray); +vmci_handle_arr_destroy (context->pendingDoorbellArray); +vmci_ctx_unset_notify (context); +kfree (context); +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_pending_dgs -- +* +* Returns the current number of pending datagrams. The call may +* also serve as a synchronization point for the datagram queue, +* as no enqueue operations can occur concurrently. +* +* Results: +* Length of datagram queue for the given context. +* +* Side effects: +* Locks datagram queue. +* +*---------------------------------------------------------------------- +*/ + +int vmci_ctx_pending_dgs (uint32_t cid,// IN +uint32_t * pending)// OUT +{ +struct vmci_ctx *context; + + context =vmci_ctx_get (cid); +if (context ==NULL ) +return VMCI_ERROR_INVALID_ARGS ; + +spin_lock (&context->lock); +if (pending) + *pending = context->pendingDatagrams; +spin_unlock (&context->lock); +vmci_ctx_release (context); + +return VMCI_SUCCESS ; +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_enqueue_dg -- +* +* Queues a VMCI datagram for the appropriate target VM +* context. +* +* Results: +* Size of enqueued data on success, appropriate error code otherwise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +int vmci_ctx_enqueue_dg (uint32_t cid,// IN: Target VM +struct vmci_dg *dg)// IN: +{ +struct vmci_dg_queue_entry *dqEntry; +struct vmci_ctx *context; +struct vmci_handle dgSrc; +size_t vmciDgSize; + +ASSERT (dg); + vmciDgSize =VMCI_DG_SIZE (dg); +ASSERT (vmciDgSize <=VMCI_MAX_DG_SIZE ); + +/* Get the target VM's VMCI context. */ + context =vmci_ctx_get (cid); +if (context ==NULL ) { +pr_devel ("Invalid context (ID=0x%x)." , cid); +return VMCI_ERROR_INVALID_ARGS ; + } + +/* Allocate guest call entry and add it to the target VM's queue. */ + dqEntry =kmalloc (sizeof *dqEntry,GFP_KERNEL ); +if (dqEntry ==NULL ) { +pr_warn ("Failed to allocate memory for datagram." ); +vmci_ctx_release (context); +return VMCI_ERROR_NO_MEM ; + } + dqEntry->dg = dg; + dqEntry->dgSize = vmciDgSize; + dgSrc = dg->src; +INIT_LIST_HEAD (&dqEntry->listItem); + +spin_lock (&context->lock); +/* +* We put a higher limit on datagrams from the hypervisor. If the pending +* datagram is not from hypervisor, then we check if enqueueing it would +* exceed the VMCI_MAX_DATAGRAM_QUEUE_SIZE limit on the destination. If the +* pending datagram is from hypervisor, we allow it to be queued at the +* destination side provided we don't reach the +* VMCI_MAX_DATAGRAM_AND_EVENT_QUEUE_SIZE limit. +*/ +if (context->datagramQueueSize + vmciDgSize >= +VMCI_MAX_DATAGRAM_QUEUE_SIZE && + (!VMCI_HANDLE_EQUAL (dgSrc, +vmci_make_handle + (VMCI_HYPERVISOR_CONTEXT_ID , +VMCI_CONTEXT_RESOURCE_ID )) + || context->datagramQueueSize + vmciDgSize >= +VMCI_MAX_DATAGRAM_AND_EVENT_QUEUE_SIZE )) { +spin_unlock (&context->lock); +vmci_ctx_release (context); +kfree (dqEntry); +pr_devel ("Context (ID=0x%x) receive queue is full." , + cid); +return VMCI_ERROR_NO_RESOURCES ; + } + +list_add (&dqEntry->listItem, &context->datagramQueue); + context->pendingDatagrams++; + context->datagramQueueSize += vmciDgSize; +ctx_signal_notify (context); +wake_up (&context->hostContext.waitQueue); +spin_unlock (&context->lock); +vmci_ctx_release (context); + +return vmciDgSize; +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_exists -- +* +* Verifies whether a context with the specified context ID exists. +* +* Results: +* true if a context exists with the given cid. +* false otherwise +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +bool vmci_ctx_exists (uint32_t cid)// IN +{ +bool rv; + +spin_lock (&ctx_list.lock); + rv =ctx_exists_locked (cid); +spin_unlock (&ctx_list.lock); +return rv; +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_get -- +* +* Retrieves VMCI context corresponding to the given cid. +* +* Results: +* VMCI context on success, NULL otherwise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +struct vmci_ctx *vmci_ctx_get (uint32_t cid)// IN +{ +struct vmci_ctx *context =NULL ; + +if (cid ==VMCI_INVALID_ID ) +return NULL ; + +spin_lock (&ctx_list.lock); +list_for_each_entry (context, &ctx_list.head, listItem) { +if (context->cid == cid) { +/* +* At this point, we are sure that the reference count is +* larger already than zero. When starting the destruction of +* a context, we always remove it from the context list +* before decreasing the reference count. As we found the +* context here, it hasn't been destroyed yet. This means +* that we are not about to increase the reference count of +* something that is in the process of being destroyed. +*/ + +atomic_inc (&context->refCount); +break ; + } + } +spin_unlock (&ctx_list.lock); + +return (context && context->cid == cid) ? context :NULL ; +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_release -- +* +* Releases the VMCI context. If this is the last reference to +* the context it will be deallocated. A context is created with +* a reference count of one, and on destroy, it is removed from +* the context list before its reference count is +* decremented. Thus, if we reach zero, we are sure that nobody +* else are about to increment it (they need the entry in the +* context list for that). This function musn't be called with a +* lock held. +* +* Results: +* None. +* +* Side effects: +* Paged memory may be deallocated. +* +*---------------------------------------------------------------------- +*/ + +void vmci_ctx_release (struct vmci_ctx *context)// IN +{ +ASSERT (context); +if (atomic_dec_and_test (&context->refCount)) +ctx_free_ctx (context); +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_dequeue_dg -- +* +* Dequeues the next datagram and returns it to caller. +* The caller passes in a pointer to the max size datagram +* it can handle and the datagram is only unqueued if the +* size is less than maxSize. If larger maxSize is set to +* the size of the datagram to give the caller a chance to +* set up a larger buffer for the guestcall. +* +* Results: +* On success: 0 if no more pending datagrams, otherwise the size of +* the next pending datagram. +* On failure: appropriate error code. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +int vmci_ctx_dequeue_dg (struct vmci_ctx *context,// IN +size_t * maxSize,// IN/OUT: max size of +// datagram caller can handle. +struct vmci_dg **dg)// OUT: +{ +struct vmci_dg_queue_entry *dqEntry; +struct list_head *listItem; +int rv; + +ASSERT (context && dg); + +/* Dequeue the next datagram entry. */ +spin_lock (&context->lock); +if (context->pendingDatagrams ==0 ) { +ctx_clear_notify_call (context); +spin_unlock (&context->lock); +pr_devel ("No datagrams pending." ); +return VMCI_ERROR_NO_MORE_DATAGRAMS ; + } + + listItem = context->datagramQueue.next; +ASSERT (!list_empty (&context->datagramQueue)); + + dqEntry = list_entry(listItem, struct vmci_dg_queue_entry, listItem); +ASSERT (dqEntry->dg); + +/* Check size of caller's buffer. */ +if (*maxSize < dqEntry->dgSize) { + *maxSize = dqEntry->dgSize; +spin_unlock (&context->lock); +pr_devel ("Caller's buffer should be at least " +"(size=%u bytes)." , (uint32_t ) * maxSize); +return VMCI_ERROR_NO_MEM ; + } + +list_del (listItem); + context->pendingDatagrams--; + context->datagramQueueSize -= dqEntry->dgSize; +if (context->pendingDatagrams ==0 ) { +ctx_clear_notify_call (context); + rv =VMCI_SUCCESS ; + }else { +/* +* Return the size of the next datagram. +*/ +struct vmci_dg_queue_entry *nextEntry; + + listItem = context->datagramQueue.next; +ASSERT (!list_empty (&context->datagramQueue)); + nextEntry = + list_entry(listItem, struct vmci_dg_queue_entry, listItem); +ASSERT (nextEntry && nextEntry->dg); +/* +* The following size_t -> int truncation is fine as the maximum size of +* a (routable) datagram is 68KB. +*/ + rv = (int )nextEntry->dgSize; + } +spin_unlock (&context->lock); + +/* Caller must free datagram. */ +ASSERT (dqEntry->dgSize ==VMCI_DG_SIZE (dqEntry->dg)); + *dg = dqEntry->dg; + dqEntry->dg =NULL ; +kfree (dqEntry); + +return rv; +} + +/* +*----------------------------------------------------------------------------- +* +* vmci_ctx_unset_notify -- +* +* Reverts actions set up by vmci_setup_notify(). Unmaps and unlocks the +* page mapped/locked by vmci_setup_notify(). +* +* Results: +* None. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +void vmci_ctx_unset_notify (struct vmci_ctx *context) +{ +struct page *notifyPage = context->notifyPage; + +if (!notifyPage) +return ; + + context->notify =NULL ; + context->notifyPage =NULL ; +kunmap (notifyPage); +put_page (notifyPage); + +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_get_id -- +* +* Retrieves context id of given VMCI context. +* +* Results: +* uint32_t of context on success, VMCI_INVALID_ID otherwise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +uint32_t vmci_ctx_get_id (struct vmci_ctx * context)// IN: +{ +if (!context) +return VMCI_INVALID_ID ; + +ASSERT (context->cid !=VMCI_INVALID_ID ); +return context->cid; +} + +/* +*---------------------------------------------------------------------- +* +* VMCIContext_GetPrivFlags -- +* +* Retrieves the privilege flags of the given VMCI context ID. +* +* Results: +* Context's privilege flags. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +uint32_t VMCIContext_GetPrivFlags (uint32_t contextID)// IN +{ +if (vmci_host_code_active ()) { +uint32_t flags; +struct vmci_ctx *context; + + context =vmci_ctx_get (contextID); +if (!context) +return VMCI_LEAST_PRIVILEGE_FLAGS ; + + flags = context->privFlags; +vmci_ctx_release (context); +return flags; + } +return VMCI_NO_PRIVILEGE_FLAGS ; +} + +EXPORT_SYMBOL (VMCIContext_GetPrivFlags); + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_add_notification -- +* +* Add remoteCID to list of contexts current contexts wants +* notifications from/about. +* +* Results: +* VMCI_SUCCESS on success, error code otherwise. +* +* Side effects: +* As in vmci_handle_arr_append_entry(). +* +*---------------------------------------------------------------------- +*/ + +int vmci_ctx_add_notification (uint32_t contextID,// IN: +uint32_t remoteCID)// IN: +{ +int result =VMCI_ERROR_ALREADY_EXISTS ; +struct vmci_handle notifierHandle; +struct vmci_ctx *context =vmci_ctx_get (contextID); +if (context ==NULL ) +return VMCI_ERROR_NOT_FOUND ; + +if (VMCI_CONTEXT_IS_VM (contextID) &&VMCI_CONTEXT_IS_VM (remoteCID)) { +pr_devel ("Context removed notifications for other VMs not " +"supported (src=0x%x, remote=0x%x)." , + contextID, remoteCID); + result =VMCI_ERROR_DST_UNREACHABLE ; + goto out; + } + +if (context->privFlags &VMCI_PRIVILEGE_FLAG_RESTRICTED ) { + result =VMCI_ERROR_NO_ACCESS ; + goto out; + } + + notifierHandle =vmci_make_handle (remoteCID,VMCI_EVENT_HANDLER ); +spin_lock (&ctx_list.firingLock); +spin_lock (&context->lock); +if (!vmci_handle_arr_has_entry (context->notifierArray, notifierHandle)) { +vmci_handle_arr_append_entry (&context->notifierArray, + notifierHandle); + result =VMCI_SUCCESS ; + } +spin_unlock (&context->lock); +spin_unlock (&ctx_list.firingLock); +out: +vmci_ctx_release (context); +return result; +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_remove_notification -- +* +* Remove remoteCID from current context's list of contexts it is +* interested in getting notifications from/about. +* +* Results: +* VMCI_SUCCESS on success, error code otherwise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +int vmci_ctx_remove_notification (uint32_t contextID,// IN: +uint32_t remoteCID)// IN: +{ +struct vmci_ctx *context =vmci_ctx_get (contextID); +struct vmci_handle tmpHandle; +if (context ==NULL ) +return VMCI_ERROR_NOT_FOUND ; + +spin_lock (&ctx_list.firingLock); +spin_lock (&context->lock); + tmpHandle = +vmci_handle_arr_remove_entry (context->notifierArray, +vmci_make_handle (remoteCID, +VMCI_EVENT_HANDLER )); +spin_unlock (&context->lock); +spin_unlock (&ctx_list.firingLock); +vmci_ctx_release (context); + +if (VMCI_HANDLE_EQUAL (tmpHandle,VMCI_INVALID_HANDLE )) +return VMCI_ERROR_NOT_FOUND ; + +return VMCI_SUCCESS ; +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_get_chkpt_state -- +* +* Get current context's checkpoint state of given type. +* +* Results: +* VMCI_SUCCESS on success, error code otherwise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +int vmci_ctx_get_chkpt_state (uint32_t contextID,// IN: +uint32_t cptType,// IN: +uint32_t * bufSize,// IN/OUT: +char **cptBufPtr)// OUT: +{ +int i, result; +uint32_t arraySize, cptDataSize; +struct vmci_handle_arr *array; +struct vmci_ctx *context; +char *cptBuf; +bool getContextID; + +ASSERT (bufSize && cptBufPtr); + + context =vmci_ctx_get (contextID); +if (context ==NULL ) +return VMCI_ERROR_NOT_FOUND ; + +spin_lock (&context->lock); +if (cptType ==VMCI_NOTIFICATION_CPT_STATE ) { +ASSERT (context->notifierArray); + array = context->notifierArray; + getContextID = true; + }else if (cptType ==VMCI_WELLKNOWN_CPT_STATE ) { +/* +* For compatibility with VMX'en with VM to VM communication, we +* always return zero wellknown handles. +*/ + + *bufSize =0 ; + *cptBufPtr =NULL ; + result =VMCI_SUCCESS ; + goto release; + }else if (cptType ==VMCI_DOORBELL_CPT_STATE ) { +ASSERT (context->doorbellArray); + array = context->doorbellArray; + getContextID = false; + }else { +pr_devel ("Invalid cpt state (type=%d)." , cptType); + result =VMCI_ERROR_INVALID_ARGS ; + goto release; + } + + arraySize =vmci_handle_arr_get_size (array); +if (arraySize >0 ) { +if (cptType ==VMCI_DOORBELL_CPT_STATE ) { + cptDataSize = + arraySize *sizeof (struct dbell_cpt_state ); + }else { + cptDataSize = arraySize *sizeof (uint32_t ); + } + +if (*bufSize < cptDataSize) { + *bufSize = cptDataSize; + result =VMCI_ERROR_MORE_DATA ; + goto release; + } + + cptBuf =kmalloc (cptDataSize,GFP_ATOMIC ); + +if (cptBuf ==NULL ) { + result =VMCI_ERROR_NO_MEM ; + goto release; + } + +for (i =0 ; i < arraySize; i++) { +struct vmci_handle tmpHandle = +vmci_handle_arr_get_entry (array, i); +if (cptType ==VMCI_DOORBELL_CPT_STATE ) { + ((struct dbell_cpt_state *)cptBuf)[i].handle = + tmpHandle; + }else { + ((uint32_t *) cptBuf)[i] = + getContextID ? tmpHandle.context : + tmpHandle.resource; + } + } + *bufSize = cptDataSize; + *cptBufPtr = cptBuf; + }else { + *bufSize =0 ; + *cptBufPtr =NULL ; + } + result =VMCI_SUCCESS ; + +release: +spin_unlock (&context->lock); +vmci_ctx_release (context); + +return result; +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_set_chkpt_state -- +* +* Set current context's checkpoint state of given type. +* +* Results: +* VMCI_SUCCESS on success, error code otherwise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +int vmci_ctx_set_chkpt_state (uint32_t contextID,// IN: +uint32_t cptType,// IN: +uint32_t bufSize,// IN: +char *cptBuf)// IN: +{ +uint32_t i; +uint32_t currentID; +int result =VMCI_SUCCESS ; +uint32_t numIDs = bufSize /sizeof (uint32_t ); +ASSERT (cptBuf); + +if (cptType ==VMCI_WELLKNOWN_CPT_STATE && numIDs >0 ) { +/* +* We would end up here if VMX with VM to VM communication +* attempts to restore a checkpoint with wellknown handles. +*/ + +pr_warn ("Attempt to restore checkpoint with obsolete " +"wellknown handles." ); +return VMCI_ERROR_OBSOLETE ; + } + +if (cptType !=VMCI_NOTIFICATION_CPT_STATE ) { +pr_devel ("Invalid cpt state (type=%d)." , cptType); +return VMCI_ERROR_INVALID_ARGS ; + } + +for (i =0 ; i < numIDs && result ==VMCI_SUCCESS ; i++) { + currentID = ((uint32_t *) cptBuf)[i]; + result =vmci_ctx_add_notification (contextID, currentID); +if (result !=VMCI_SUCCESS ) +break ; + } +if (result !=VMCI_SUCCESS ) +pr_devel ("Failed to set cpt state (type=%d) " +"(error=%d)." , cptType, result); + +return result; +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_rcv_notifications_get -- +* +* Retrieves the specified context's pending notifications in the +* form of a handle array. The handle arrays returned are the +* actual data - not a copy and should not be modified by the +* caller. They must be released using +* vmci_ctx_rcv_notifications_release. +* +* Results: +* VMCI_SUCCESS on success, error code otherwise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +int vmci_ctx_rcv_notifications_get (uint32_t contextID,// IN +struct vmci_handle_arr **dbHandleArray,// OUT +struct vmci_handle_arr **qpHandleArray)// OUT +{ +struct vmci_ctx *context; +int result =VMCI_SUCCESS ; + +ASSERT (dbHandleArray && qpHandleArray); + + context =vmci_ctx_get (contextID); +if (context ==NULL ) +return VMCI_ERROR_NOT_FOUND ; + +spin_lock (&context->lock); + + *dbHandleArray = context->pendingDoorbellArray; + context->pendingDoorbellArray =vmci_handle_arr_create (0 ); +if (!context->pendingDoorbellArray) { + context->pendingDoorbellArray = *dbHandleArray; + *dbHandleArray =NULL ; + result =VMCI_ERROR_NO_MEM ; + } + *qpHandleArray =NULL ; + +spin_unlock (&context->lock); +vmci_ctx_release (context); + +return result; +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_rcv_notifications_release -- +* +* Releases handle arrays with pending notifications previously +* retrieved using vmci_ctx_rcv_notifications_get. If the +* notifications were not successfully handed over to the guest, +* success must be false. +* +* Results: +* None. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +void vmci_ctx_rcv_notifications_release (uint32_t contextID,// IN +struct vmci_handle_arr *dbHandleArray,// IN +struct vmci_handle_arr *qpHandleArray,// IN +bool success)// IN +{ +struct vmci_ctx *context =vmci_ctx_get (contextID); + +if (context) { +spin_lock (&context->lock); +if (!success) { +struct vmci_handle handle; + +/* +* New notifications may have been added while we were not +* holding the context lock, so we transfer any new pending +* doorbell notifications to the old array, and reinstate the +* old array. +*/ + + handle = +vmci_handle_arr_remove_tail + (context->pendingDoorbellArray); +while (!VMCI_HANDLE_INVALID (handle)) { +ASSERT (vmci_handle_arr_has_entry + (context->doorbellArray, handle)); +if (!vmci_handle_arr_has_entry + (dbHandleArray, handle)) { +vmci_handle_arr_append_entry + (&dbHandleArray, handle); + } + handle =vmci_handle_arr_remove_tail ( + context->pendingDoorbellArray); + } +vmci_handle_arr_destroy (context->pendingDoorbellArray); + context->pendingDoorbellArray = dbHandleArray; + dbHandleArray =NULL ; + }else { +ctx_clear_notify_call (context); + } +spin_unlock (&context->lock); +vmci_ctx_release (context); + }else { +/* +* The OS driver part is holding on to the context for the +* duration of the receive notification ioctl, so it should +* still be here. +*/ + +ASSERT (false); + } + +if (dbHandleArray) +vmci_handle_arr_destroy (dbHandleArray); + +if (qpHandleArray) +vmci_handle_arr_destroy (qpHandleArray); +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_dbell_create -- +* +* Registers that a new doorbell handle has been allocated by the +* context. Only doorbell handles registered can be notified. +* +* Results: +* VMCI_SUCCESS on success, appropriate error code otherewise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +int vmci_ctx_dbell_create (uint32_t contextID,// IN +struct vmci_handle handle)// IN +{ +struct vmci_ctx *context; +int result; + +if (contextID ==VMCI_INVALID_ID ||VMCI_HANDLE_INVALID (handle)) +return VMCI_ERROR_INVALID_ARGS ; + + context =vmci_ctx_get (contextID); +if (context ==NULL ) +return VMCI_ERROR_NOT_FOUND ; + +spin_lock (&context->lock); +if (!vmci_handle_arr_has_entry (context->doorbellArray, handle)) { +vmci_handle_arr_append_entry (&context->doorbellArray, handle); + result =VMCI_SUCCESS ; + }else { + result =VMCI_ERROR_DUPLICATE_ENTRY ; + } + +spin_unlock (&context->lock); +vmci_ctx_release (context); + +return result; +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_dbell_destroy -- +* +* Unregisters a doorbell handle that was previously registered +* with vmci_ctx_dbell_create. +* +* Results: +* VMCI_SUCCESS on success, appropriate error code otherewise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +int vmci_ctx_dbell_destroy (uint32_t contextID,// IN +struct vmci_handle handle)// IN +{ +struct vmci_ctx *context; +struct vmci_handle removedHandle; + +if (contextID ==VMCI_INVALID_ID ||VMCI_HANDLE_INVALID (handle)) +return VMCI_ERROR_INVALID_ARGS ; + + context =vmci_ctx_get (contextID); +if (context ==NULL ) +return VMCI_ERROR_NOT_FOUND ; + +spin_lock (&context->lock); + removedHandle = +vmci_handle_arr_remove_entry (context->doorbellArray, handle); +vmci_handle_arr_remove_entry (context->pendingDoorbellArray, handle); +spin_unlock (&context->lock); + +vmci_ctx_release (context); + +return VMCI_HANDLE_INVALID (removedHandle) ? +VMCI_ERROR_NOT_FOUND :VMCI_SUCCESS ; +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_dbell_destroy_all -- +* +* Unregisters all doorbell handles that were previously +* registered with vmci_ctx_dbell_create. +* +* Results: +* VMCI_SUCCESS on success, appropriate error code otherewise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +int vmci_ctx_dbell_destroy_all (uint32_t contextID)// IN +{ +struct vmci_ctx *context; +struct vmci_handle removedHandle; + +if (contextID ==VMCI_INVALID_ID ) +return VMCI_ERROR_INVALID_ARGS ; + + context =vmci_ctx_get (contextID); +if (context ==NULL ) +return VMCI_ERROR_NOT_FOUND ; + +spin_lock (&context->lock); +do { + removedHandle = +vmci_handle_arr_remove_tail (context->doorbellArray); + }while (!VMCI_HANDLE_INVALID (removedHandle)); +do { + removedHandle = +vmci_handle_arr_remove_tail (context->pendingDoorbellArray); + }while (!VMCI_HANDLE_INVALID (removedHandle)); +spin_unlock (&context->lock); + +vmci_ctx_release (context); + +return VMCI_SUCCESS ; +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_notify_dbell -- +* +* Registers a notification of a doorbell handle initiated by the +* specified source context. The notification of doorbells are +* subject to the same isolation rules as datagram delivery. To +* allow host side senders of notifications a finer granularity +* of sender rights than those assigned to the sending context +* itself, the host context is required to specify a different +* set of privilege flags that will override the privileges of +* the source context. +* +* Results: +* VMCI_SUCCESS on success, appropriate error code otherewise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +int vmci_ctx_notify_dbell (uint32_t srcCID,// IN +struct vmci_handle handle,// IN +uint32_t srcPrivFlags)// IN +{ +struct vmci_ctx *dstContext; +int result; + +if (VMCI_HANDLE_INVALID (handle)) +return VMCI_ERROR_INVALID_ARGS ; + +/* Get the target VM's VMCI context. */ + dstContext =vmci_ctx_get (handle.context); +if (dstContext ==NULL ) { +pr_devel ("Invalid context (ID=0x%x)." , handle.context); +return VMCI_ERROR_NOT_FOUND ; + } + +if (srcCID != handle.context) { +uint32_t dstPrivFlags; + +if (VMCI_CONTEXT_IS_VM (srcCID) + &&VMCI_CONTEXT_IS_VM (handle.context)) { +pr_devel ("Doorbell notification from VM to VM not " +"supported (src=0x%x, dst=0x%x)." , srcCID, + handle.context); + result =VMCI_ERROR_DST_UNREACHABLE ; + goto out; + } + + result =vmci_dbell_get_priv_flags (handle, &dstPrivFlags); +if (result <VMCI_SUCCESS ) { +pr_warn ("Failed to get privilege flags for " +"destination (handle=0x%x:0x%x)." , + handle.context, handle.resource); + goto out; + } + +if (srcCID !=VMCI_HOST_CONTEXT_ID || + srcPrivFlags ==VMCI_NO_PRIVILEGE_FLAGS ) { + srcPrivFlags =VMCIContext_GetPrivFlags (srcCID); + } + +if (vmci_deny_interaction (srcPrivFlags, dstPrivFlags)) { + result =VMCI_ERROR_NO_ACCESS ; + goto out; + } + } + +if (handle.context ==VMCI_HOST_CONTEXT_ID ) { + result =vmci_dbell_host_context_notify (srcCID, handle); + }else { +spin_lock (&dstContext->lock); + +if (!vmci_handle_arr_has_entry + (dstContext->doorbellArray, handle)) { + result =VMCI_ERROR_NOT_FOUND ; + }else { +if (!vmci_handle_arr_has_entry + (dstContext->pendingDoorbellArray, handle)) { +vmci_handle_arr_append_entry + (&dstContext->pendingDoorbellArray, handle); + +ctx_signal_notify (dstContext); +wake_up (&dstContext->hostContext.waitQueue); + + } + result =VMCI_SUCCESS ; + } +spin_unlock (&dstContext->lock); + } + +out: +vmci_ctx_release (dstContext); + +return result; +} + +/* +*---------------------------------------------------------------------- +* +* VMCI_ContextID2HostVmID -- +* +* Maps a context ID to the host specific (process/world) ID +* of the VM/VMX. +* +* Results: +* VMCI_SUCCESS on success, error code otherwise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +int VMCI_ContextID2HostVmID (uint32_t contextID,// IN +void *hostVmID,// OUT +size_t hostVmIDLen)// IN +{ +return VMCI_ERROR_UNAVAILABLE ; +} + +EXPORT_SYMBOL (VMCI_ContextID2HostVmID); + +/* +*----------------------------------------------------------------------------- +* +* ctx_compare_user -- +* +* Determines whether the two users are the same. +* +* Results: +* VMCI_SUCCESS if equal, error code otherwise. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +static int ctx_compare_user (uid_t * user1,uid_t * user2) +{ +if (!user1 || !user2) +return VMCI_ERROR_INVALID_ARGS ; + +return (*user1 == *user2) ?VMCI_SUCCESS :VMCI_ERROR_GENERIC ; +} + +/* +*---------------------------------------------------------------------- +* +* VMCI_IsContextOwner -- +* +* Determines whether a given host OS specific representation of +* user is the owner of the VM/VMX. +* +* Results: +* VMCI_SUCCESS if the hostUser is owner, error code otherwise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +int VMCI_IsContextOwner (uint32_t contextID,// IN +void *hostUser)// IN +{ +if (vmci_host_code_active ()) { +struct vmci_ctx *context; +uid_t *user = (uid_t *) hostUser; +int retval; + +if (!hostUser) +return VMCI_ERROR_INVALID_ARGS ; + + context =vmci_ctx_get (contextID); +if (!context) +return VMCI_ERROR_NOT_FOUND ; + +if (context->validUser) + retval =ctx_compare_user (user, &context->user); +else + retval =VMCI_ERROR_UNAVAILABLE ; + +vmci_ctx_release (context); +return retval; + } +return VMCI_ERROR_UNAVAILABLE ; +} + +EXPORT_SYMBOL (VMCI_IsContextOwner); + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_supports_host_qp -- +* +* Can host QPs be connected to this user process. The answer is +* false unless a sufficient version number has previously been set +* by this caller. +* +* Results: +* true if context supports host queue pairs, false otherwise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +bool vmci_ctx_supports_host_qp (struct vmci_ctx * context)// IN: Context structure +{ +return context && context->userVersion >=VMCI_VERSION_HOSTQP ; +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_qp_create -- +* +* Registers that a new queue pair handle has been allocated by +* the context. +* +* Results: +* VMCI_SUCCESS on success, appropriate error code otherewise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +int vmci_ctx_qp_create (struct vmci_ctx *context,// IN: Context structure +struct vmci_handle handle)// IN +{ +int result; + +if (context ==NULL ||VMCI_HANDLE_INVALID (handle)) +return VMCI_ERROR_INVALID_ARGS ; + +if (!vmci_handle_arr_has_entry (context->queuePairArray, handle)) { +vmci_handle_arr_append_entry (&context->queuePairArray, handle); + result =VMCI_SUCCESS ; + }else { + result =VMCI_ERROR_DUPLICATE_ENTRY ; + } + +return result; +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_qp_destroy -- +* +* Unregisters a queue pair handle that was previously registered +* with vmci_ctx_qp_create. +* +* Results: +* VMCI_SUCCESS on success, appropriate error code otherewise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +int vmci_ctx_qp_destroy (struct vmci_ctx *context,// IN: Context structure +struct vmci_handle handle)// IN +{ +struct vmci_handle hndl; + +if (context ==NULL ||VMCI_HANDLE_INVALID (handle)) +return VMCI_ERROR_INVALID_ARGS ; + + hndl =vmci_handle_arr_remove_entry (context->queuePairArray, handle); + +return VMCI_HANDLE_INVALID (hndl) ? +VMCI_ERROR_NOT_FOUND :VMCI_SUCCESS ; +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_qp_exists -- +* +* Determines whether a given queue pair handle is registered +* with the given context. +* +* Results: +* true, if queue pair is registered with context. false, otherwise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +bool vmci_ctx_qp_exists (struct vmci_ctx * context,// IN: Context structure +struct vmci_handle handle)// IN +{ +if (context ==NULL ||VMCI_HANDLE_INVALID (handle)) +return VMCI_ERROR_INVALID_ARGS ; + +return vmci_handle_arr_has_entry (context->queuePairArray, handle); +}
drivers/misc/vmw_vmci/vmci_context.h new file +150 -0
150 lines not on this page · raw message
Signature · 1 line
1.7.0.4
RFC 1/11 Apply VMCI context code
In reply to Andrew Stiegmann (stieg)Context code maintains state for vmci and allows the driver to communicate with multiple VMs.
--- drivers/misc/vmw_vmci/vmci_context.c | 1743 ++++++++++++++++++++++++++++++++++ drivers/misc/vmw_vmci/vmci_context.h | 150 +++ 2 files changed, 1893 insertions(+), 0 deletions(-) create mode 100644 drivers/misc/vmw_vmci/vmci_context.c create mode 100644 drivers/misc/vmw_vmci/vmci_context.h
drivers/misc/vmw_vmci/vmci_context.c new file +1743 -0
@@ -0,0 +1,1743 @@ +/* +* VMware VMCI Driver +* +* Copyright (C) 2012 VMware, Inc. All rights reserved. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms of the GNU General Public License as published by the +* Free Software Foundation version 2 and no later version. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +* for more details. +* +* You should have received a copy of the GNU General Public License along +* with this program; if not, write to the Free Software Foundation, Inc., +* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include <linux/highmem.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/sched.h> +#include <linux/vmw_vmci_api.h> +#include <linux/vmw_vmci_defs.h> + +#include "vmci_common_int.h" +#include "vmci_context.h" +#include "vmci_datagram.h" +#include "vmci_doorbell.h" +#include "vmci_driver.h" +#include "vmci_event.h" +#include "vmci_queue_pair.h" + +/* List of current VMCI contexts. */ +static struct { +struct list_head head; +spinlock_t lock; +spinlock_t firingLock; +} ctx_list; + +/* +*---------------------------------------------------------------------- +* +* ctx_signal_notify -- +* +* Sets the notify flag to true. Assumes that the context lock is +* held. +* +* Results: +* None. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +static void ctx_signal_notify (struct vmci_ctx *context) +{ +if (context->notify) + *context->notify = true; +} + +/* +*---------------------------------------------------------------------- +* +* ctx_clear_notify -- +* +* Sets the notify flag to false. Assumes that the context lock is +* held. +* +* Results: +* None. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +static void ctx_clear_notify (struct vmci_ctx *context) +{ +if (context->notify) + *context->notify = false; +} + +/* +*---------------------------------------------------------------------- +* +* ctx_clear_notify_call -- +* +* If nothing requires the attention of the guest, clears both +* notify flag and call. +* +* Results: +* None. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +static void ctx_clear_notify_call (struct vmci_ctx *context) +{ +if (context->pendingDatagrams ==0 && +vmci_handle_arr_get_size (context->pendingDoorbellArray) ==0 ) +ctx_clear_notify (context); +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_check_signal_notify -- +* +* Sets the context's notify flag iff datagrams are pending for this +* context. Called from vmci_setup_notify(). +* +* Results: +* None. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +void vmci_ctx_check_signal_notify (struct vmci_ctx *context) +{ +ASSERT (context); + +spin_lock (&ctx_list.lock); +if (context->pendingDatagrams) +ctx_signal_notify (context); +spin_unlock (&ctx_list.lock); +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_init -- +* +* Initializes the VMCI context module. +* +* Results: +* None. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +int __init vmci_ctx_init (void ) +{ +INIT_LIST_HEAD (&ctx_list.head); + +spin_lock_init (&ctx_list.lock); +spin_lock_init (&ctx_list.firingLock); + +return VMCI_SUCCESS ; +} + +/* +*---------------------------------------------------------------------- +* +* ctx_exists_locked -- +* +* Internal helper to check if a context with the specified context +* ID exists. Assumes the ctx_list.lock is held. +* +* Results: +* true if a context exists with the given cid. +* false otherwise +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +static bool ctx_exists_locked (uint32_t cid)// IN +{ +struct vmci_ctx *context; + +list_for_each_entry (context, &ctx_list.head, listItem) { +if (context->cid == cid) +return true; + } + +return false; +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_init_ctx -- +* +* Allocates and initializes a VMCI context. +* +* Results: +* Returns 0 on success, appropriate error code otherwise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +int vmci_ctx_init_ctx (uint32_t cid, +uint32_t privFlags, +uintptr_t eventHnd, +int userVersion, +uid_t * user,struct vmci_ctx **outContext) +{ +struct vmci_ctx *context; +int result; + +if (privFlags & ~VMCI_PRIVILEGE_ALL_FLAGS ) { +pr_devel ("Invalid flag (flags=0x%x) for VMCI context." , + privFlags); +return VMCI_ERROR_INVALID_ARGS ; + } + +if (userVersion ==0 ) +return VMCI_ERROR_INVALID_ARGS ; + + context =kzalloc (sizeof *context,GFP_KERNEL ); +if (context ==NULL ) { +pr_warn ("Failed to allocate memory for VMCI context." ); +return VMCI_ERROR_NO_MEM ; + } + +INIT_LIST_HEAD (&context->listItem); +INIT_LIST_HEAD (&context->datagramQueue); + + context->userVersion = userVersion; + + context->queuePairArray =vmci_handle_arr_create (0 ); +if (!context->queuePairArray) { + result =VMCI_ERROR_NO_MEM ; + goto error; + } + + context->doorbellArray =vmci_handle_arr_create (0 ); +if (!context->doorbellArray) { + result =VMCI_ERROR_NO_MEM ; + goto error; + } + + context->pendingDoorbellArray =vmci_handle_arr_create (0 ); +if (!context->pendingDoorbellArray) { + result =VMCI_ERROR_NO_MEM ; + goto error; + } + + context->notifierArray =vmci_handle_arr_create (0 ); +if (context->notifierArray ==NULL ) { + result =VMCI_ERROR_NO_MEM ; + goto error; + } + +spin_lock_init (&context->lock); + +atomic_set (&context->refCount,1 ); + +/* Inititialize host-specific VMCI context. */ +init_waitqueue_head (&context->hostContext.waitQueue); + + context->privFlags = privFlags; + +/* +* If we collide with an existing context we generate a new and use it +* instead. The VMX will determine if regeneration is okay. Since there +* isn't 4B - 16 VMs running on a given host, the below loop will terminate. +*/ +spin_lock (&ctx_list.lock); +ASSERT (cid !=VMCI_INVALID_ID ); +while (ctx_exists_locked (cid)) { + +/* +* If the cid is below our limit and we collide we are creating duplicate +* contexts internally so we want to assert fail in that case. +*/ +ASSERT (cid >=VMCI_RESERVED_CID_LIMIT ); + +/* We reserve the lowest 16 ids for fixed contexts. */ + cid =max (cid,VMCI_RESERVED_CID_LIMIT -1 ) +1 ; +if (cid ==VMCI_INVALID_ID ) + cid =VMCI_RESERVED_CID_LIMIT ; + } +ASSERT (!ctx_exists_locked (cid)); + context->cid = cid; + context->validUser = user !=NULL ; +if (context->validUser) + context->user = *user; +list_add (&context->listItem, &ctx_list.head); +spin_unlock (&ctx_list.lock); + + context->notify =NULL ; + context->notifyPage =NULL ; + + *outContext = context; +return VMCI_SUCCESS ; + +error: +if (context->notifierArray) +vmci_handle_arr_destroy (context->notifierArray); +if (context->queuePairArray) +vmci_handle_arr_destroy (context->queuePairArray); +if (context->doorbellArray) +vmci_handle_arr_destroy (context->doorbellArray); +if (context->pendingDoorbellArray) +vmci_handle_arr_destroy (context->pendingDoorbellArray); +kfree (context); +return result; +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_release_ctx -- +* +* Cleans up a VMCI context. +* +* Results: +* None. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +void vmci_ctx_release_ctx (struct vmci_ctx *context)// IN +{ +/* Dequeue VMCI context. */ + +spin_lock (&ctx_list.lock); +list_del (&context->listItem); +spin_unlock (&ctx_list.lock); + +vmci_ctx_release (context); +} + +/* +*---------------------------------------------------------------------- +* +* ctx_fire_notification -- +* +* Fire notification for all contexts interested in given cid. +* +* Results: +* VMCI_SUCCESS on success, error code otherwise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +static int ctx_fire_notification (uint32_t contextID,// IN +uint32_t privFlags)// IN +{ +uint32_t i, arraySize; +struct vmci_ctx *subCtx; +struct vmci_handle_arr *subscriberArray; +struct vmci_handle contextHandle = +vmci_make_handle (contextID,VMCI_EVENT_HANDLER ); + +/* +* We create an array to hold the subscribers we find when scanning through +* all contexts. +*/ + subscriberArray =vmci_handle_arr_create (0 ); +if (subscriberArray ==NULL ) +return VMCI_ERROR_NO_MEM ; + +/* +* Scan all contexts to find who is interested in being notified about +* given contextID. We have a special firingLock that we use to synchronize +* across all notification operations. This avoids us having to take the +* context lock for each HasEntry call and it solves a lock ranking issue. +*/ +spin_lock (&ctx_list.firingLock); +spin_lock (&ctx_list.lock); +list_for_each_entry (subCtx, &ctx_list.head, listItem) { +/* +* We only deliver notifications of the removal of contexts, if +* the two contexts are allowed to interact. +*/ +if (vmci_handle_arr_has_entry + (subCtx->notifierArray, contextHandle) + && !vmci_deny_interaction (privFlags, subCtx->privFlags)) { +vmci_handle_arr_append_entry (&subscriberArray, +vmci_make_handle + (subCtx->cid, +VMCI_EVENT_HANDLER )); + } + } +spin_unlock (&ctx_list.lock); +spin_unlock (&ctx_list.firingLock); + +/* Fire event to all subscribers. */ + arraySize =vmci_handle_arr_get_size (subscriberArray); +for (i =0 ; i < arraySize; i++) { +int result; +struct vmci_event_msg *eMsg; +struct vmci_event_payld_ctx *evPayload; +char buf[sizeof *eMsg +sizeof *evPayload]; + + eMsg = (struct vmci_event_msg *)buf; + +/* Clear out any garbage. */ +memset (eMsg,0 ,sizeof *eMsg +sizeof *evPayload); + eMsg->hdr.dst =vmci_handle_arr_get_entry (subscriberArray, i); + eMsg->hdr.src = +vmci_make_handle (VMCI_HYPERVISOR_CONTEXT_ID , +VMCI_CONTEXT_RESOURCE_ID ); + eMsg->hdr.payloadSize = +sizeof *eMsg +sizeof *evPayload -sizeof eMsg->hdr; + eMsg->eventData.event =VMCI_EVENT_CTX_REMOVED ; + evPayload =vmci_event_data_payload (&eMsg->eventData); + evPayload->contextID = contextID; + + result =vmci_dg_dispatch (VMCI_HYPERVISOR_CONTEXT_ID , + (struct vmci_dg *) + eMsg, false); +if (result <VMCI_SUCCESS ) { +pr_devel ("Failed to enqueue event datagram " +"(type=%d) for context (ID=0x%x)." , + eMsg->eventData.event, eMsg->hdr.dst.context); +/* We continue to enqueue on next subscriber. */ + } + } +vmci_handle_arr_destroy (subscriberArray); + +return VMCI_SUCCESS ; +} + +/* +*----------------------------------------------------------------------------- +* +* ctx_free_ctx -- +* +* Deallocates all parts of a context datastructure. This +* functions doesn't lock the context, because it assumes that +* the caller is holding the last reference to context. +* +* Results: +* None. +* +* Side effects: +* Paged memory is freed. +* +*----------------------------------------------------------------------------- +*/ + +static void ctx_free_ctx (struct vmci_ctx *context)// IN +{ +struct list_head *curr; +struct list_head *next; +struct vmci_dg_queue_entry *dqEntry; +struct vmci_handle tempHandle; + +/* Fire event to all contexts interested in knowing this context is dying. */ +ctx_fire_notification (context->cid, context->privFlags); + +/* +* Cleanup all queue pair resources attached to context. If the VM dies +* without cleaning up, this code will make sure that no resources are +* leaked. +*/ + + tempHandle =vmci_handle_arr_get_entry (context->queuePairArray,0 ); +while (!VMCI_HANDLE_EQUAL (tempHandle,VMCI_INVALID_HANDLE )) { +if (vmci_qp_broker_detach (tempHandle, context) <VMCI_SUCCESS ) { +/* +* When vmci_qp_broker_detach() succeeds it removes the handle from the +* array. If detach fails, we must remove the handle ourselves. +*/ +vmci_handle_arr_remove_entry (context->queuePairArray, + tempHandle); + } + tempHandle = +vmci_handle_arr_get_entry (context->queuePairArray,0 ); + } + +/* +* It is fine to destroy this without locking the callQueue, as +* this is the only thread having a reference to the context. +*/ + +list_for_each_safe (curr, next, &context->datagramQueue) { + dqEntry = + list_entry(curr, struct vmci_dg_queue_entry, listItem); +list_del (curr); +ASSERT (dqEntry && dqEntry->dg); +ASSERT (dqEntry->dgSize ==VMCI_DG_SIZE (dqEntry->dg)); +kfree (dqEntry->dg); +kfree (dqEntry); + } + +vmci_handle_arr_destroy (context->notifierArray); +vmci_handle_arr_destroy (context->queuePairArray); +vmci_handle_arr_destroy (context->doorbellArray); +vmci_handle_arr_destroy (context->pendingDoorbellArray); +vmci_ctx_unset_notify (context); +kfree (context); +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_pending_dgs -- +* +* Returns the current number of pending datagrams. The call may +* also serve as a synchronization point for the datagram queue, +* as no enqueue operations can occur concurrently. +* +* Results: +* Length of datagram queue for the given context. +* +* Side effects: +* Locks datagram queue. +* +*---------------------------------------------------------------------- +*/ + +int vmci_ctx_pending_dgs (uint32_t cid,// IN +uint32_t * pending)// OUT +{ +struct vmci_ctx *context; + + context =vmci_ctx_get (cid); +if (context ==NULL ) +return VMCI_ERROR_INVALID_ARGS ; + +spin_lock (&context->lock); +if (pending) + *pending = context->pendingDatagrams; +spin_unlock (&context->lock); +vmci_ctx_release (context); + +return VMCI_SUCCESS ; +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_enqueue_dg -- +* +* Queues a VMCI datagram for the appropriate target VM +* context. +* +* Results: +* Size of enqueued data on success, appropriate error code otherwise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +int vmci_ctx_enqueue_dg (uint32_t cid,// IN: Target VM +struct vmci_dg *dg)// IN: +{ +struct vmci_dg_queue_entry *dqEntry; +struct vmci_ctx *context; +struct vmci_handle dgSrc; +size_t vmciDgSize; + +ASSERT (dg); + vmciDgSize =VMCI_DG_SIZE (dg); +ASSERT (vmciDgSize <=VMCI_MAX_DG_SIZE ); + +/* Get the target VM's VMCI context. */ + context =vmci_ctx_get (cid); +if (context ==NULL ) { +pr_devel ("Invalid context (ID=0x%x)." , cid); +return VMCI_ERROR_INVALID_ARGS ; + } + +/* Allocate guest call entry and add it to the target VM's queue. */ + dqEntry =kmalloc (sizeof *dqEntry,GFP_KERNEL ); +if (dqEntry ==NULL ) { +pr_warn ("Failed to allocate memory for datagram." ); +vmci_ctx_release (context); +return VMCI_ERROR_NO_MEM ; + } + dqEntry->dg = dg; + dqEntry->dgSize = vmciDgSize; + dgSrc = dg->src; +INIT_LIST_HEAD (&dqEntry->listItem); + +spin_lock (&context->lock); +/* +* We put a higher limit on datagrams from the hypervisor. If the pending +* datagram is not from hypervisor, then we check if enqueueing it would +* exceed the VMCI_MAX_DATAGRAM_QUEUE_SIZE limit on the destination. If the +* pending datagram is from hypervisor, we allow it to be queued at the +* destination side provided we don't reach the +* VMCI_MAX_DATAGRAM_AND_EVENT_QUEUE_SIZE limit. +*/ +if (context->datagramQueueSize + vmciDgSize >= +VMCI_MAX_DATAGRAM_QUEUE_SIZE && + (!VMCI_HANDLE_EQUAL (dgSrc, +vmci_make_handle + (VMCI_HYPERVISOR_CONTEXT_ID , +VMCI_CONTEXT_RESOURCE_ID )) + || context->datagramQueueSize + vmciDgSize >= +VMCI_MAX_DATAGRAM_AND_EVENT_QUEUE_SIZE )) { +spin_unlock (&context->lock); +vmci_ctx_release (context); +kfree (dqEntry); +pr_devel ("Context (ID=0x%x) receive queue is full." , + cid); +return VMCI_ERROR_NO_RESOURCES ; + } + +list_add (&dqEntry->listItem, &context->datagramQueue); + context->pendingDatagrams++; + context->datagramQueueSize += vmciDgSize; +ctx_signal_notify (context); +wake_up (&context->hostContext.waitQueue); +spin_unlock (&context->lock); +vmci_ctx_release (context); + +return vmciDgSize; +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_exists -- +* +* Verifies whether a context with the specified context ID exists. +* +* Results: +* true if a context exists with the given cid. +* false otherwise +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +bool vmci_ctx_exists (uint32_t cid)// IN +{ +bool rv; + +spin_lock (&ctx_list.lock); + rv =ctx_exists_locked (cid); +spin_unlock (&ctx_list.lock); +return rv; +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_get -- +* +* Retrieves VMCI context corresponding to the given cid. +* +* Results: +* VMCI context on success, NULL otherwise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +struct vmci_ctx *vmci_ctx_get (uint32_t cid)// IN +{ +struct vmci_ctx *context =NULL ; + +if (cid ==VMCI_INVALID_ID ) +return NULL ; + +spin_lock (&ctx_list.lock); +list_for_each_entry (context, &ctx_list.head, listItem) { +if (context->cid == cid) { +/* +* At this point, we are sure that the reference count is +* larger already than zero. When starting the destruction of +* a context, we always remove it from the context list +* before decreasing the reference count. As we found the +* context here, it hasn't been destroyed yet. This means +* that we are not about to increase the reference count of +* something that is in the process of being destroyed. +*/ + +atomic_inc (&context->refCount); +break ; + } + } +spin_unlock (&ctx_list.lock); + +return (context && context->cid == cid) ? context :NULL ; +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_release -- +* +* Releases the VMCI context. If this is the last reference to +* the context it will be deallocated. A context is created with +* a reference count of one, and on destroy, it is removed from +* the context list before its reference count is +* decremented. Thus, if we reach zero, we are sure that nobody +* else are about to increment it (they need the entry in the +* context list for that). This function musn't be called with a +* lock held. +* +* Results: +* None. +* +* Side effects: +* Paged memory may be deallocated. +* +*---------------------------------------------------------------------- +*/ + +void vmci_ctx_release (struct vmci_ctx *context)// IN +{ +ASSERT (context); +if (atomic_dec_and_test (&context->refCount)) +ctx_free_ctx (context); +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_dequeue_dg -- +* +* Dequeues the next datagram and returns it to caller. +* The caller passes in a pointer to the max size datagram +* it can handle and the datagram is only unqueued if the +* size is less than maxSize. If larger maxSize is set to +* the size of the datagram to give the caller a chance to +* set up a larger buffer for the guestcall. +* +* Results: +* On success: 0 if no more pending datagrams, otherwise the size of +* the next pending datagram. +* On failure: appropriate error code. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +int vmci_ctx_dequeue_dg (struct vmci_ctx *context,// IN +size_t * maxSize,// IN/OUT: max size of +// datagram caller can handle. +struct vmci_dg **dg)// OUT: +{ +struct vmci_dg_queue_entry *dqEntry; +struct list_head *listItem; +int rv; + +ASSERT (context && dg); + +/* Dequeue the next datagram entry. */ +spin_lock (&context->lock); +if (context->pendingDatagrams ==0 ) { +ctx_clear_notify_call (context); +spin_unlock (&context->lock); +pr_devel ("No datagrams pending." ); +return VMCI_ERROR_NO_MORE_DATAGRAMS ; + } + + listItem = context->datagramQueue.next; +ASSERT (!list_empty (&context->datagramQueue)); + + dqEntry = list_entry(listItem, struct vmci_dg_queue_entry, listItem); +ASSERT (dqEntry->dg); + +/* Check size of caller's buffer. */ +if (*maxSize < dqEntry->dgSize) { + *maxSize = dqEntry->dgSize; +spin_unlock (&context->lock); +pr_devel ("Caller's buffer should be at least " +"(size=%u bytes)." , (uint32_t ) * maxSize); +return VMCI_ERROR_NO_MEM ; + } + +list_del (listItem); + context->pendingDatagrams--; + context->datagramQueueSize -= dqEntry->dgSize; +if (context->pendingDatagrams ==0 ) { +ctx_clear_notify_call (context); + rv =VMCI_SUCCESS ; + }else { +/* +* Return the size of the next datagram. +*/ +struct vmci_dg_queue_entry *nextEntry; + + listItem = context->datagramQueue.next; +ASSERT (!list_empty (&context->datagramQueue)); + nextEntry = + list_entry(listItem, struct vmci_dg_queue_entry, listItem); +ASSERT (nextEntry && nextEntry->dg); +/* +* The following size_t -> int truncation is fine as the maximum size of +* a (routable) datagram is 68KB. +*/ + rv = (int )nextEntry->dgSize; + } +spin_unlock (&context->lock); + +/* Caller must free datagram. */ +ASSERT (dqEntry->dgSize ==VMCI_DG_SIZE (dqEntry->dg)); + *dg = dqEntry->dg; + dqEntry->dg =NULL ; +kfree (dqEntry); + +return rv; +} + +/* +*----------------------------------------------------------------------------- +* +* vmci_ctx_unset_notify -- +* +* Reverts actions set up by vmci_setup_notify(). Unmaps and unlocks the +* page mapped/locked by vmci_setup_notify(). +* +* Results: +* None. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +void vmci_ctx_unset_notify (struct vmci_ctx *context) +{ +struct page *notifyPage = context->notifyPage; + +if (!notifyPage) +return ; + + context->notify =NULL ; + context->notifyPage =NULL ; +kunmap (notifyPage); +put_page (notifyPage); + +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_get_id -- +* +* Retrieves context id of given VMCI context. +* +* Results: +* uint32_t of context on success, VMCI_INVALID_ID otherwise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +uint32_t vmci_ctx_get_id (struct vmci_ctx * context)// IN: +{ +if (!context) +return VMCI_INVALID_ID ; + +ASSERT (context->cid !=VMCI_INVALID_ID ); +return context->cid; +} + +/* +*---------------------------------------------------------------------- +* +* VMCIContext_GetPrivFlags -- +* +* Retrieves the privilege flags of the given VMCI context ID. +* +* Results: +* Context's privilege flags. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +uint32_t VMCIContext_GetPrivFlags (uint32_t contextID)// IN +{ +if (vmci_host_code_active ()) { +uint32_t flags; +struct vmci_ctx *context; + + context =vmci_ctx_get (contextID); +if (!context) +return VMCI_LEAST_PRIVILEGE_FLAGS ; + + flags = context->privFlags; +vmci_ctx_release (context); +return flags; + } +return VMCI_NO_PRIVILEGE_FLAGS ; +} + +EXPORT_SYMBOL (VMCIContext_GetPrivFlags); + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_add_notification -- +* +* Add remoteCID to list of contexts current contexts wants +* notifications from/about. +* +* Results: +* VMCI_SUCCESS on success, error code otherwise. +* +* Side effects: +* As in vmci_handle_arr_append_entry(). +* +*---------------------------------------------------------------------- +*/ + +int vmci_ctx_add_notification (uint32_t contextID,// IN: +uint32_t remoteCID)// IN: +{ +int result =VMCI_ERROR_ALREADY_EXISTS ; +struct vmci_handle notifierHandle; +struct vmci_ctx *context =vmci_ctx_get (contextID); +if (context ==NULL ) +return VMCI_ERROR_NOT_FOUND ; + +if (VMCI_CONTEXT_IS_VM (contextID) &&VMCI_CONTEXT_IS_VM (remoteCID)) { +pr_devel ("Context removed notifications for other VMs not " +"supported (src=0x%x, remote=0x%x)." , + contextID, remoteCID); + result =VMCI_ERROR_DST_UNREACHABLE ; + goto out; + } + +if (context->privFlags &VMCI_PRIVILEGE_FLAG_RESTRICTED ) { + result =VMCI_ERROR_NO_ACCESS ; + goto out; + } + + notifierHandle =vmci_make_handle (remoteCID,VMCI_EVENT_HANDLER ); +spin_lock (&ctx_list.firingLock); +spin_lock (&context->lock); +if (!vmci_handle_arr_has_entry (context->notifierArray, notifierHandle)) { +vmci_handle_arr_append_entry (&context->notifierArray, + notifierHandle); + result =VMCI_SUCCESS ; + } +spin_unlock (&context->lock); +spin_unlock (&ctx_list.firingLock); +out: +vmci_ctx_release (context); +return result; +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_remove_notification -- +* +* Remove remoteCID from current context's list of contexts it is +* interested in getting notifications from/about. +* +* Results: +* VMCI_SUCCESS on success, error code otherwise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +int vmci_ctx_remove_notification (uint32_t contextID,// IN: +uint32_t remoteCID)// IN: +{ +struct vmci_ctx *context =vmci_ctx_get (contextID); +struct vmci_handle tmpHandle; +if (context ==NULL ) +return VMCI_ERROR_NOT_FOUND ; + +spin_lock (&ctx_list.firingLock); +spin_lock (&context->lock); + tmpHandle = +vmci_handle_arr_remove_entry (context->notifierArray, +vmci_make_handle (remoteCID, +VMCI_EVENT_HANDLER )); +spin_unlock (&context->lock); +spin_unlock (&ctx_list.firingLock); +vmci_ctx_release (context); + +if (VMCI_HANDLE_EQUAL (tmpHandle,VMCI_INVALID_HANDLE )) +return VMCI_ERROR_NOT_FOUND ; + +return VMCI_SUCCESS ; +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_get_chkpt_state -- +* +* Get current context's checkpoint state of given type. +* +* Results: +* VMCI_SUCCESS on success, error code otherwise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +int vmci_ctx_get_chkpt_state (uint32_t contextID,// IN: +uint32_t cptType,// IN: +uint32_t * bufSize,// IN/OUT: +char **cptBufPtr)// OUT: +{ +int i, result; +uint32_t arraySize, cptDataSize; +struct vmci_handle_arr *array; +struct vmci_ctx *context; +char *cptBuf; +bool getContextID; + +ASSERT (bufSize && cptBufPtr); + + context =vmci_ctx_get (contextID); +if (context ==NULL ) +return VMCI_ERROR_NOT_FOUND ; + +spin_lock (&context->lock); +if (cptType ==VMCI_NOTIFICATION_CPT_STATE ) { +ASSERT (context->notifierArray); + array = context->notifierArray; + getContextID = true; + }else if (cptType ==VMCI_WELLKNOWN_CPT_STATE ) { +/* +* For compatibility with VMX'en with VM to VM communication, we +* always return zero wellknown handles. +*/ + + *bufSize =0 ; + *cptBufPtr =NULL ; + result =VMCI_SUCCESS ; + goto release; + }else if (cptType ==VMCI_DOORBELL_CPT_STATE ) { +ASSERT (context->doorbellArray); + array = context->doorbellArray; + getContextID = false; + }else { +pr_devel ("Invalid cpt state (type=%d)." , cptType); + result =VMCI_ERROR_INVALID_ARGS ; + goto release; + } + + arraySize =vmci_handle_arr_get_size (array); +if (arraySize >0 ) { +if (cptType ==VMCI_DOORBELL_CPT_STATE ) { + cptDataSize = + arraySize *sizeof (struct dbell_cpt_state ); + }else { + cptDataSize = arraySize *sizeof (uint32_t ); + } + +if (*bufSize < cptDataSize) { + *bufSize = cptDataSize; + result =VMCI_ERROR_MORE_DATA ; + goto release; + } + + cptBuf =kmalloc (cptDataSize,GFP_ATOMIC ); + +if (cptBuf ==NULL ) { + result =VMCI_ERROR_NO_MEM ; + goto release; + } + +for (i =0 ; i < arraySize; i++) { +struct vmci_handle tmpHandle = +vmci_handle_arr_get_entry (array, i); +if (cptType ==VMCI_DOORBELL_CPT_STATE ) { + ((struct dbell_cpt_state *)cptBuf)[i].handle = + tmpHandle; + }else { + ((uint32_t *) cptBuf)[i] = + getContextID ? tmpHandle.context : + tmpHandle.resource; + } + } + *bufSize = cptDataSize; + *cptBufPtr = cptBuf; + }else { + *bufSize =0 ; + *cptBufPtr =NULL ; + } + result =VMCI_SUCCESS ; + +release: +spin_unlock (&context->lock); +vmci_ctx_release (context); + +return result; +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_set_chkpt_state -- +* +* Set current context's checkpoint state of given type. +* +* Results: +* VMCI_SUCCESS on success, error code otherwise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +int vmci_ctx_set_chkpt_state (uint32_t contextID,// IN: +uint32_t cptType,// IN: +uint32_t bufSize,// IN: +char *cptBuf)// IN: +{ +uint32_t i; +uint32_t currentID; +int result =VMCI_SUCCESS ; +uint32_t numIDs = bufSize /sizeof (uint32_t ); +ASSERT (cptBuf); + +if (cptType ==VMCI_WELLKNOWN_CPT_STATE && numIDs >0 ) { +/* +* We would end up here if VMX with VM to VM communication +* attempts to restore a checkpoint with wellknown handles. +*/ + +pr_warn ("Attempt to restore checkpoint with obsolete " +"wellknown handles." ); +return VMCI_ERROR_OBSOLETE ; + } + +if (cptType !=VMCI_NOTIFICATION_CPT_STATE ) { +pr_devel ("Invalid cpt state (type=%d)." , cptType); +return VMCI_ERROR_INVALID_ARGS ; + } + +for (i =0 ; i < numIDs && result ==VMCI_SUCCESS ; i++) { + currentID = ((uint32_t *) cptBuf)[i]; + result =vmci_ctx_add_notification (contextID, currentID); +if (result !=VMCI_SUCCESS ) +break ; + } +if (result !=VMCI_SUCCESS ) +pr_devel ("Failed to set cpt state (type=%d) " +"(error=%d)." , cptType, result); + +return result; +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_rcv_notifications_get -- +* +* Retrieves the specified context's pending notifications in the +* form of a handle array. The handle arrays returned are the +* actual data - not a copy and should not be modified by the +* caller. They must be released using +* vmci_ctx_rcv_notifications_release. +* +* Results: +* VMCI_SUCCESS on success, error code otherwise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +int vmci_ctx_rcv_notifications_get (uint32_t contextID,// IN +struct vmci_handle_arr **dbHandleArray,// OUT +struct vmci_handle_arr **qpHandleArray)// OUT +{ +struct vmci_ctx *context; +int result =VMCI_SUCCESS ; + +ASSERT (dbHandleArray && qpHandleArray); + + context =vmci_ctx_get (contextID); +if (context ==NULL ) +return VMCI_ERROR_NOT_FOUND ; + +spin_lock (&context->lock); + + *dbHandleArray = context->pendingDoorbellArray; + context->pendingDoorbellArray =vmci_handle_arr_create (0 ); +if (!context->pendingDoorbellArray) { + context->pendingDoorbellArray = *dbHandleArray; + *dbHandleArray =NULL ; + result =VMCI_ERROR_NO_MEM ; + } + *qpHandleArray =NULL ; + +spin_unlock (&context->lock); +vmci_ctx_release (context); + +return result; +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_rcv_notifications_release -- +* +* Releases handle arrays with pending notifications previously +* retrieved using vmci_ctx_rcv_notifications_get. If the +* notifications were not successfully handed over to the guest, +* success must be false. +* +* Results: +* None. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +void vmci_ctx_rcv_notifications_release (uint32_t contextID,// IN +struct vmci_handle_arr *dbHandleArray,// IN +struct vmci_handle_arr *qpHandleArray,// IN +bool success)// IN +{ +struct vmci_ctx *context =vmci_ctx_get (contextID); + +if (context) { +spin_lock (&context->lock); +if (!success) { +struct vmci_handle handle; + +/* +* New notifications may have been added while we were not +* holding the context lock, so we transfer any new pending +* doorbell notifications to the old array, and reinstate the +* old array. +*/ + + handle = +vmci_handle_arr_remove_tail + (context->pendingDoorbellArray); +while (!VMCI_HANDLE_INVALID (handle)) { +ASSERT (vmci_handle_arr_has_entry + (context->doorbellArray, handle)); +if (!vmci_handle_arr_has_entry + (dbHandleArray, handle)) { +vmci_handle_arr_append_entry + (&dbHandleArray, handle); + } + handle =vmci_handle_arr_remove_tail ( + context->pendingDoorbellArray); + } +vmci_handle_arr_destroy (context->pendingDoorbellArray); + context->pendingDoorbellArray = dbHandleArray; + dbHandleArray =NULL ; + }else { +ctx_clear_notify_call (context); + } +spin_unlock (&context->lock); +vmci_ctx_release (context); + }else { +/* +* The OS driver part is holding on to the context for the +* duration of the receive notification ioctl, so it should +* still be here. +*/ + +ASSERT (false); + } + +if (dbHandleArray) +vmci_handle_arr_destroy (dbHandleArray); + +if (qpHandleArray) +vmci_handle_arr_destroy (qpHandleArray); +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_dbell_create -- +* +* Registers that a new doorbell handle has been allocated by the +* context. Only doorbell handles registered can be notified. +* +* Results: +* VMCI_SUCCESS on success, appropriate error code otherewise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +int vmci_ctx_dbell_create (uint32_t contextID,// IN +struct vmci_handle handle)// IN +{ +struct vmci_ctx *context; +int result; + +if (contextID ==VMCI_INVALID_ID ||VMCI_HANDLE_INVALID (handle)) +return VMCI_ERROR_INVALID_ARGS ; + + context =vmci_ctx_get (contextID); +if (context ==NULL ) +return VMCI_ERROR_NOT_FOUND ; + +spin_lock (&context->lock); +if (!vmci_handle_arr_has_entry (context->doorbellArray, handle)) { +vmci_handle_arr_append_entry (&context->doorbellArray, handle); + result =VMCI_SUCCESS ; + }else { + result =VMCI_ERROR_DUPLICATE_ENTRY ; + } + +spin_unlock (&context->lock); +vmci_ctx_release (context); + +return result; +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_dbell_destroy -- +* +* Unregisters a doorbell handle that was previously registered +* with vmci_ctx_dbell_create. +* +* Results: +* VMCI_SUCCESS on success, appropriate error code otherewise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +int vmci_ctx_dbell_destroy (uint32_t contextID,// IN +struct vmci_handle handle)// IN +{ +struct vmci_ctx *context; +struct vmci_handle removedHandle; + +if (contextID ==VMCI_INVALID_ID ||VMCI_HANDLE_INVALID (handle)) +return VMCI_ERROR_INVALID_ARGS ; + + context =vmci_ctx_get (contextID); +if (context ==NULL ) +return VMCI_ERROR_NOT_FOUND ; + +spin_lock (&context->lock); + removedHandle = +vmci_handle_arr_remove_entry (context->doorbellArray, handle); +vmci_handle_arr_remove_entry (context->pendingDoorbellArray, handle); +spin_unlock (&context->lock); + +vmci_ctx_release (context); + +return VMCI_HANDLE_INVALID (removedHandle) ? +VMCI_ERROR_NOT_FOUND :VMCI_SUCCESS ; +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_dbell_destroy_all -- +* +* Unregisters all doorbell handles that were previously +* registered with vmci_ctx_dbell_create. +* +* Results: +* VMCI_SUCCESS on success, appropriate error code otherewise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +int vmci_ctx_dbell_destroy_all (uint32_t contextID)// IN +{ +struct vmci_ctx *context; +struct vmci_handle removedHandle; + +if (contextID ==VMCI_INVALID_ID ) +return VMCI_ERROR_INVALID_ARGS ; + + context =vmci_ctx_get (contextID); +if (context ==NULL ) +return VMCI_ERROR_NOT_FOUND ; + +spin_lock (&context->lock); +do { + removedHandle = +vmci_handle_arr_remove_tail (context->doorbellArray); + }while (!VMCI_HANDLE_INVALID (removedHandle)); +do { + removedHandle = +vmci_handle_arr_remove_tail (context->pendingDoorbellArray); + }while (!VMCI_HANDLE_INVALID (removedHandle)); +spin_unlock (&context->lock); + +vmci_ctx_release (context); + +return VMCI_SUCCESS ; +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_notify_dbell -- +* +* Registers a notification of a doorbell handle initiated by the +* specified source context. The notification of doorbells are +* subject to the same isolation rules as datagram delivery. To +* allow host side senders of notifications a finer granularity +* of sender rights than those assigned to the sending context +* itself, the host context is required to specify a different +* set of privilege flags that will override the privileges of +* the source context. +* +* Results: +* VMCI_SUCCESS on success, appropriate error code otherewise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +int vmci_ctx_notify_dbell (uint32_t srcCID,// IN +struct vmci_handle handle,// IN +uint32_t srcPrivFlags)// IN +{ +struct vmci_ctx *dstContext; +int result; + +if (VMCI_HANDLE_INVALID (handle)) +return VMCI_ERROR_INVALID_ARGS ; + +/* Get the target VM's VMCI context. */ + dstContext =vmci_ctx_get (handle.context); +if (dstContext ==NULL ) { +pr_devel ("Invalid context (ID=0x%x)." , handle.context); +return VMCI_ERROR_NOT_FOUND ; + } + +if (srcCID != handle.context) { +uint32_t dstPrivFlags; + +if (VMCI_CONTEXT_IS_VM (srcCID) + &&VMCI_CONTEXT_IS_VM (handle.context)) { +pr_devel ("Doorbell notification from VM to VM not " +"supported (src=0x%x, dst=0x%x)." , srcCID, + handle.context); + result =VMCI_ERROR_DST_UNREACHABLE ; + goto out; + } + + result =vmci_dbell_get_priv_flags (handle, &dstPrivFlags); +if (result <VMCI_SUCCESS ) { +pr_warn ("Failed to get privilege flags for " +"destination (handle=0x%x:0x%x)." , + handle.context, handle.resource); + goto out; + } + +if (srcCID !=VMCI_HOST_CONTEXT_ID || + srcPrivFlags ==VMCI_NO_PRIVILEGE_FLAGS ) { + srcPrivFlags =VMCIContext_GetPrivFlags (srcCID); + } + +if (vmci_deny_interaction (srcPrivFlags, dstPrivFlags)) { + result =VMCI_ERROR_NO_ACCESS ; + goto out; + } + } + +if (handle.context ==VMCI_HOST_CONTEXT_ID ) { + result =vmci_dbell_host_context_notify (srcCID, handle); + }else { +spin_lock (&dstContext->lock); + +if (!vmci_handle_arr_has_entry + (dstContext->doorbellArray, handle)) { + result =VMCI_ERROR_NOT_FOUND ; + }else { +if (!vmci_handle_arr_has_entry + (dstContext->pendingDoorbellArray, handle)) { +vmci_handle_arr_append_entry + (&dstContext->pendingDoorbellArray, handle); + +ctx_signal_notify (dstContext); +wake_up (&dstContext->hostContext.waitQueue); + + } + result =VMCI_SUCCESS ; + } +spin_unlock (&dstContext->lock); + } + +out: +vmci_ctx_release (dstContext); + +return result; +} + +/* +*---------------------------------------------------------------------- +* +* VMCI_ContextID2HostVmID -- +* +* Maps a context ID to the host specific (process/world) ID +* of the VM/VMX. +* +* Results: +* VMCI_SUCCESS on success, error code otherwise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +int VMCI_ContextID2HostVmID (uint32_t contextID,// IN +void *hostVmID,// OUT +size_t hostVmIDLen)// IN +{ +return VMCI_ERROR_UNAVAILABLE ; +} + +EXPORT_SYMBOL (VMCI_ContextID2HostVmID); + +/* +*----------------------------------------------------------------------------- +* +* ctx_compare_user -- +* +* Determines whether the two users are the same. +* +* Results: +* VMCI_SUCCESS if equal, error code otherwise. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +static int ctx_compare_user (uid_t * user1,uid_t * user2) +{ +if (!user1 || !user2) +return VMCI_ERROR_INVALID_ARGS ; + +return (*user1 == *user2) ?VMCI_SUCCESS :VMCI_ERROR_GENERIC ; +} + +/* +*---------------------------------------------------------------------- +* +* VMCI_IsContextOwner -- +* +* Determines whether a given host OS specific representation of +* user is the owner of the VM/VMX. +* +* Results: +* VMCI_SUCCESS if the hostUser is owner, error code otherwise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +int VMCI_IsContextOwner (uint32_t contextID,// IN +void *hostUser)// IN +{ +if (vmci_host_code_active ()) { +struct vmci_ctx *context; +uid_t *user = (uid_t *) hostUser; +int retval; + +if (!hostUser) +return VMCI_ERROR_INVALID_ARGS ; + + context =vmci_ctx_get (contextID); +if (!context) +return VMCI_ERROR_NOT_FOUND ; + +if (context->validUser) + retval =ctx_compare_user (user, &context->user); +else + retval =VMCI_ERROR_UNAVAILABLE ; + +vmci_ctx_release (context); +return retval; + } +return VMCI_ERROR_UNAVAILABLE ; +} + +EXPORT_SYMBOL (VMCI_IsContextOwner); + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_supports_host_qp -- +* +* Can host QPs be connected to this user process. The answer is +* false unless a sufficient version number has previously been set +* by this caller. +* +* Results: +* true if context supports host queue pairs, false otherwise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +bool vmci_ctx_supports_host_qp (struct vmci_ctx * context)// IN: Context structure +{ +return context && context->userVersion >=VMCI_VERSION_HOSTQP ; +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_qp_create -- +* +* Registers that a new queue pair handle has been allocated by +* the context. +* +* Results: +* VMCI_SUCCESS on success, appropriate error code otherewise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +int vmci_ctx_qp_create (struct vmci_ctx *context,// IN: Context structure +struct vmci_handle handle)// IN +{ +int result; + +if (context ==NULL ||VMCI_HANDLE_INVALID (handle)) +return VMCI_ERROR_INVALID_ARGS ; + +if (!vmci_handle_arr_has_entry (context->queuePairArray, handle)) { +vmci_handle_arr_append_entry (&context->queuePairArray, handle); + result =VMCI_SUCCESS ; + }else { + result =VMCI_ERROR_DUPLICATE_ENTRY ; + } + +return result; +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_qp_destroy -- +* +* Unregisters a queue pair handle that was previously registered +* with vmci_ctx_qp_create. +* +* Results: +* VMCI_SUCCESS on success, appropriate error code otherewise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +int vmci_ctx_qp_destroy (struct vmci_ctx *context,// IN: Context structure +struct vmci_handle handle)// IN +{ +struct vmci_handle hndl; + +if (context ==NULL ||VMCI_HANDLE_INVALID (handle)) +return VMCI_ERROR_INVALID_ARGS ; + + hndl =vmci_handle_arr_remove_entry (context->queuePairArray, handle); + +return VMCI_HANDLE_INVALID (hndl) ? +VMCI_ERROR_NOT_FOUND :VMCI_SUCCESS ; +} + +/* +*---------------------------------------------------------------------- +* +* vmci_ctx_qp_exists -- +* +* Determines whether a given queue pair handle is registered +* with the given context. +* +* Results: +* true, if queue pair is registered with context. false, otherwise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +bool vmci_ctx_qp_exists (struct vmci_ctx * context,// IN: Context structure +struct vmci_handle handle)// IN +{ +if (context ==NULL ||VMCI_HANDLE_INVALID (handle)) +return VMCI_ERROR_INVALID_ARGS ; + +return vmci_handle_arr_has_entry (context->queuePairArray, handle); +}
drivers/misc/vmw_vmci/vmci_context.h new file +150 -0
150 lines not on this page · raw message
Signature · 1 line
1.7.0.4
Greg KH
maintainerRe: [vmw_vmci RFC 01/11] Apply VMCI context code
Quoted 12 lines · Andrew Stiegmann (stieg)
drivers/misc/vmw_vmci/vmci_context.c new file +12
-0+/* +*---------------------------------------------------------------------- +* +* ctx_signal_notify -- +* +* Sets the notify flag to true. Assumes that the context lock is +* held. +* +* Results: +* None. +* +* Side effects:
drivers/misc/vmw_vmci/vmci_context.c line 63
Quoted 10 lines
drivers/misc/vmw_vmci/vmci_context.c new file +10
-0+* None. +* +*---------------------------------------------------------------------- +*/ + +static void ctx_signal_notify (struct vmci_ctx *context) +{ +if (context->notify) + *context->notify = true; +}
What's with the bizarre comment block format here and throughout the code? Care to fix it up (i.e. delete it all from static functions, and use kerneldoc for public functions.)
thanks,
greg k-h
Greg KH
maintainerRe: [vmw_vmci RFC 01/11] Apply VMCI context code
In reply to Andrew Stiegmann (stieg)Quoted 12 lines · Andrew Stiegmann (stieg)
drivers/misc/vmw_vmci/vmci_context.c new file +12
-0+/* +*---------------------------------------------------------------------- +* +* ctx_signal_notify -- +* +* Sets the notify flag to true. Assumes that the context lock is +* held. +* +* Results: +* None. +* +* Side effects:
drivers/misc/vmw_vmci/vmci_context.c line 63
Quoted 10 lines
drivers/misc/vmw_vmci/vmci_context.c new file +10
-0+* None. +* +*---------------------------------------------------------------------- +*/ + +static void ctx_signal_notify (struct vmci_ctx *context) +{ +if (context->notify) + *context->notify = true; +}
What's with the bizarre comment block format here and throughout the code? Care to fix it up (i.e. delete it all from static functions, and use kerneldoc for public functions.)
thanks,
greg k-h
Re: [vmw_vmci RFC 01/11] Apply VMCI context code
In reply to Andrew Stiegmann (stieg)Quoted 4 lines · On Tue, 15 May 2012 08:06:58 -0700 "Andrew Stiegmann (stieg)"…
Context code maintains state for vmci and allows the driver to communicate with multiple VMs.
Signed-off-by Andrew Stiegmann (stieg) <[email protected]>
Running checkpatch reveals the usual noise, and the following that should be addressed.
ERROR: do not use C99 // comments
#272: FILE: drivers/misc/vmw_vmci/vmci_context.c:183:
+static bool ctx_exists_locked(uint32_t cid) // IN
ERROR: "foo * bar" should be "foo *bar"
#304: FILE: drivers/misc/vmw_vmci/vmci_context.c:215:
+ uid_t * user, struct vmci_ctx **outContext)
I don't mind the C99 style comments, but the // IN convention is pretty useless and should be removed.
Re: [vmw_vmci RFC 01/11] Apply VMCI context code
Both of your comments have been added to my "to do" list before the next time I publish. Thanks for the feedback.
----- Original Message -----
Quoted 29 lines
From: "Stephen Hemminger" <[email protected]>
To: "Andrew Stiegmann (stieg)" <[email protected]>
Cc: [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected]
Sent: Wednesday, May 16, 2012 10:01:21 AM
Subject: Re: [vmw_vmciRFC 01/11] Apply VMCI context codeQuoted 4 lines · On Tue, 15 May 2012 08:06:58 -0700 "Andrew Stiegmann (stieg)"…
Context code maintains state for vmci and allows the driver to communicate with multiple VMs.
Signed-off-by Andrew Stiegmann (stieg) <[email protected]>Running checkpatch reveals the usual noise, and the following that should be addressed.
ERROR: do not use C99 // comments
#272: FILE:drivers/misc/vmw_vmci/vmci_context.c:183:
+static boolctx_exists_locked(uint32_t cid)// INERROR: "foo * bar" should be "foo *bar"
#304: FILE:drivers/misc/vmw_vmci/vmci_context.c:215:drivers/misc/vmw_vmci/vmci_context.c new file +1
-0+ uid_t * user, struct vmci_ctx **outContext)I don't mind the C99 style comments, but the // IN convention is pretty useless and should be removed.
Re: [vmw_vmci RFC 01/11] Apply VMCI context code
In reply to Stephen HemmingerBoth of your comments have been added to my "to do" list before the next time I publish. Thanks for the feedback.
----- Original Message -----
Quoted 29 lines
From: "Stephen Hemminger" <[email protected]>
To: "Andrew Stiegmann (stieg)" <[email protected]>
Cc: [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected]
Sent: Wednesday, May 16, 2012 10:01:21 AM
Subject: Re: [vmw_vmciRFC 01/11] Apply VMCI context codeQuoted 4 lines · On Tue, 15 May 2012 08:06:58 -0700 "Andrew Stiegmann (stieg)"…
Context code maintains state for vmci and allows the driver to communicate with multiple VMs.
Signed-off-by Andrew Stiegmann (stieg) <[email protected]>Running checkpatch reveals the usual noise, and the following that should be addressed.
ERROR: do not use C99 // comments
#272: FILE:drivers/misc/vmw_vmci/vmci_context.c:183:
+static boolctx_exists_locked(uint32_t cid)// INERROR: "foo * bar" should be "foo *bar"
#304: FILE:drivers/misc/vmw_vmci/vmci_context.c:215:drivers/misc/vmw_vmci/vmci_context.c new file +1
-0+ uid_t * user, struct vmci_ctx **outContext)I don't mind the C99 style comments, but the // IN convention is pretty useless and should be removed.
RFC 2/11 Apply VMCI datagram code
In reply to Andrew Stiegmann (stieg)Implements datagrams to allow data to be sent between host and guest.
--- drivers/misc/vmw_vmci/vmci_datagram.c | 760 +++++++++++++++++++++++++++++++++ drivers/misc/vmw_vmci/vmci_datagram.h | 57 +++ 2 files changed, 817 insertions(+), 0 deletions(-) create mode 100644 drivers/misc/vmw_vmci/vmci_datagram.c create mode 100644 drivers/misc/vmw_vmci/vmci_datagram.h
drivers/misc/vmw_vmci/vmci_datagram.c new file +760 -0
@@ -0,0 +1,760 @@ +/* +* VMware VMCI Driver +* +* Copyright (C) 2012 VMware, Inc. All rights reserved. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms of the GNU General Public License as published by the +* Free Software Foundation version 2 and no later version. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +* for more details. +* +* You should have received a copy of the GNU General Public License along +* with this program; if not, write to the Free Software Foundation, Inc., +* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include <linux/bug.h> +#include <linux/module.h> +#include <linux/sched.h> +#include <linux/vmw_vmci_api.h> +#include <linux/vmw_vmci_defs.h> + +#include "vmci_common_int.h" +#include "vmci_context.h" +#include "vmci_datagram.h" +#include "vmci_driver.h" +#include "vmci_event.h" +#include "vmci_hash_table.h" +#include "vmci_resource.h" +#include "vmci_route.h" + +/* +* struct datagram_entry describes the datagram entity. It is used for datagram +* entities created only on the host. +*/ +struct datagram_entry { +struct vmci_resource resource; +uint32_t flags; +bool runDelayed; +VMCIDatagramRecvCB recvCB; +void *clientData; +wait_queue_head_t destroyEvent; +uint32_t privFlags; +}; + +struct delayed_datagram_info { +bool inDGHostQueue; +struct datagram_entry *entry; +struct vmci_dg msg; +}; + +static atomic_t delayedDGHostQueueSize; + +/* +*------------------------------------------------------------------------------ +* +* dg_free_cb -- +* Callback to free datagram structure when resource is no longer used, +* ie. the reference count reached 0. +* +* Result: +* None. +* +*------------------------------------------------------------------------------ +*/ + +static void dg_free_cb (void *clientData) +{ +struct datagram_entry *entry = (struct datagram_entry *)clientData; +ASSERT (entry); +/* Entry is freed in VMCIDatagram_DestroyHnd, who waits for the signal */ +wake_up (&entry->destroyEvent); +} + +/* +*------------------------------------------------------------------------------ +* +* dg_release_cb -- +* +* Callback to release the resource reference. It is called by the +* VMCI_WaitOnEvent function before it blocks. +* +* Result: +* None. +* +*------------------------------------------------------------------------------ +*/ + +static int dg_release_cb (void *clientData) +{ +struct datagram_entry *entry = (struct datagram_entry *)clientData; +ASSERT (entry); +vmci_resource_release (&entry->resource); +return 0 ; +} + +/* +*------------------------------------------------------------------------------ +* +* dg_create_handle -- +* +* Internal function to create a datagram entry given a handle. +* +* Results: +* VMCI_SUCCESS if created, negative errno value otherwise. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +static int dg_create_handle (uint32_t resourceID,// IN: +uint32_t flags,// IN: +uint32_t privFlags,// IN: +VMCIDatagramRecvCB recvCB,// IN: +void *clientData,// IN: +struct vmci_handle *outHandle)// OUT: +{ +int result; +uint32_t contextID; +struct vmci_handle handle; +struct datagram_entry *entry; + +ASSERT (recvCB !=NULL ); +ASSERT (outHandle !=NULL ); +ASSERT (!(privFlags & ~VMCI_PRIVILEGE_ALL_FLAGS )); + +if ((flags &VMCI_FLAG_WELLKNOWN_DG_HND ) !=0 ) { +return VMCI_ERROR_INVALID_ARGS ; + }else { +if ((flags &VMCI_FLAG_ANYCID_DG_HND ) !=0 ) { + contextID =VMCI_INVALID_ID ; + }else { + contextID =VMCI_GetContextID (); +if (contextID ==VMCI_INVALID_ID ) +return VMCI_ERROR_NO_RESOURCES ; + } + +if (resourceID ==VMCI_INVALID_ID ) { + resourceID =vmci_resource_get_id (contextID); +if (resourceID ==VMCI_INVALID_ID ) +return VMCI_ERROR_NO_HANDLE ; + } + + handle =vmci_make_handle (contextID, resourceID); + } + + entry =kmalloc (sizeof *entry,GFP_KERNEL ); +if (entry ==NULL ) { +pr_warn ("Failed allocating memory for datagram entry." ); +return VMCI_ERROR_NO_MEM ; + } + + entry->runDelayed = (flags &VMCI_FLAG_DG_DELAYED_CB ) ? true : false; + entry->flags = flags; + entry->recvCB = recvCB; + entry->clientData = clientData; +init_waitqueue_head (&entry->destroyEvent); + entry->privFlags = privFlags; + +/* Make datagram resource live. */ + result = +vmci_resource_add (&entry->resource,VMCI_RESOURCE_TYPE_DATAGRAM , + handle, dg_free_cb, entry); +if (result !=VMCI_SUCCESS ) { +pr_warn ("Failed to add new resource (handle=0x%x:0x%x)." , + handle.context, handle.resource); +kfree (entry); +return result; + } + *outHandle = handle; + +return VMCI_SUCCESS ; +} + +/* +*------------------------------------------------------------------------------ +* +* vmci_dg_init -- +* +* Initialize Datagram API, ie. register the API functions with their +* corresponding vectors. +* +* Result: +* None. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +int __init vmci_dg_init (void ) +{ +atomic_set (&delayedDGHostQueueSize,0 ); +return VMCI_SUCCESS ; +} + +/* +*------------------------------------------------------------------------------ +* +* VMCIDatagram_CreateHnd -- +* +* Creates a host context datagram endpoint and returns a handle to it. +* +* Results: +* VMCI_SUCCESS if created, negative errno value otherwise. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +int VMCIDatagram_CreateHnd (uint32_t resourceID,// IN: Optional, generated +// if VMCI_INVALID_ID +uint32_t flags,// IN: +VMCIDatagramRecvCB recvCB,// IN: +void *clientData,// IN: +struct vmci_handle *outHandle)// OUT: newly created handle +{ +if (outHandle ==NULL ) +return VMCI_ERROR_INVALID_ARGS ; + +if (recvCB ==NULL ) { +pr_devel ("Client callback needed when creating " +"datagram." ); +return VMCI_ERROR_INVALID_ARGS ; + } + +return dg_create_handle (resourceID, flags, +VMCI_DEFAULT_PROC_PRIVILEGE_FLAGS , recvCB, + clientData, outHandle); +} + +EXPORT_SYMBOL (VMCIDatagram_CreateHnd); + +/* +*------------------------------------------------------------------------------ +* +* VMCIDatagram_CreateHndPriv -- +* +* Creates a host context datagram endpoint and returns a handle to it. +* +* Results: +* VMCI_SUCCESS if created, negative errno value otherwise. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +int VMCIDatagram_CreateHndPriv (uint32_t resourceID,// IN: Optional, generated +// if VMCI_INVALID_ID +uint32_t flags,// IN: +uint32_t privFlags,// IN: +VMCIDatagramRecvCB recvCB,// IN: +void *clientData,// IN: +struct vmci_handle *outHandle)// OUT: newly created handle +{ +if (outHandle ==NULL ) +return VMCI_ERROR_INVALID_ARGS ; + +if (recvCB ==NULL ) { +pr_devel ("Client callback needed when creating " +"datagram." ); +return VMCI_ERROR_INVALID_ARGS ; + } + +if (privFlags & ~VMCI_PRIVILEGE_ALL_FLAGS ) +return VMCI_ERROR_INVALID_ARGS ; + +return dg_create_handle (resourceID, flags, privFlags, recvCB, + clientData, outHandle); +} + +EXPORT_SYMBOL (VMCIDatagram_CreateHndPriv); + +/* +*------------------------------------------------------------------------------ +* +* VMCIDatagram_DestroyHnd -- +* +* Destroys a handle. +* +* Results: +* None. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +int VMCIDatagram_DestroyHnd (struct vmci_handle handle)// IN +{ +struct datagram_entry *entry; +struct vmci_resource *resource =vmci_resource_get (handle, +VMCI_RESOURCE_TYPE_DATAGRAM ); +if (resource ==NULL ) { +pr_devel ("Failed to destroy datagram (handle=0x%x:0x%x)" +"." , handle.context, handle.resource); +return VMCI_ERROR_NOT_FOUND ; + } + entry = container_of(resource, struct datagram_entry, resource); + +vmci_resource_remove (handle,VMCI_RESOURCE_TYPE_DATAGRAM ); + +/* +* We now wait on the destroyEvent and release the reference we got +* above. +*/ +vmci_drv_wait_on_event_intr (&entry->destroyEvent, dg_release_cb, + entry); + +/* +* We know that we are now the only reference to the above entry so +* can safely free it. +*/ +kfree (entry); + +return VMCI_SUCCESS ; +} + +EXPORT_SYMBOL (VMCIDatagram_DestroyHnd); + +/* +*------------------------------------------------------------------------------ +* +* vmci_dg_get_priv_flags -- +* +* Internal utilility function with the same purpose as +* vmci_dg_get_priv_flags that also takes a contextID. +* +* Result: +* VMCI_SUCCESS on success, VMCI_ERROR_INVALID_ARGS if handle is invalid. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +static int vmci_dg_get_priv_flags (uint32_t contextID,// IN +struct vmci_handle handle,// IN +uint32_t * privFlags)// OUT +{ +ASSERT (privFlags); +ASSERT (contextID !=VMCI_INVALID_ID ); + +if (contextID ==VMCI_HOST_CONTEXT_ID ) { +struct datagram_entry *srcEntry; +struct vmci_resource *resource; + + resource = +vmci_resource_get (handle,VMCI_RESOURCE_TYPE_DATAGRAM ); +if (resource ==NULL ) +return VMCI_ERROR_INVALID_ARGS ; + + srcEntry = container_of(resource, struct datagram_entry, + resource); + *privFlags = srcEntry->privFlags; +vmci_resource_release (resource); + }else if (contextID ==VMCI_HYPERVISOR_CONTEXT_ID ) { + *privFlags =VMCI_MAX_PRIVILEGE_FLAGS ; + }else { + *privFlags =VMCIContext_GetPrivFlags (contextID); + } + +return VMCI_SUCCESS ; +} + +/* +*----------------------------------------------------------------------------- +* +* dg_delayed_dispatch_cb -- +* +* Calls the specified callback in a delayed context. +* +* Results: +* None. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +static void dg_delayed_dispatch_cb (void *data)// IN +{ +bool inDGHostQueue; +struct delayed_datagram_info *dgInfo = + (struct delayed_datagram_info *)data; + +ASSERT (data); + + dgInfo->entry->recvCB (dgInfo->entry->clientData, &dgInfo->msg); + +vmci_resource_release (&dgInfo->entry->resource); + + inDGHostQueue = dgInfo->inDGHostQueue; +kfree (dgInfo); + +if (inDGHostQueue) +atomic_dec (&delayedDGHostQueueSize); +} + +/* +*------------------------------------------------------------------------------ +* +* dg_dispatch_as_host -- +* +* Dispatch datagram as a host, to the host or other vm context. This +* function cannot dispatch to hypervisor context handlers. This should +* have been handled before we get here by VMCIDatagramDispatch. +* +* Result: +* Number of bytes sent on success, appropriate error code otherwise. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +static int dg_dispatch_as_host (uint32_t contextID,// IN: +struct vmci_dg *dg)// IN: +{ +int retval; +size_t dgSize; +uint32_t srcPrivFlags; + +ASSERT (dg); +ASSERT (vmci_host_code_active ()); + + dgSize =VMCI_DG_SIZE (dg); + +if (contextID ==VMCI_HOST_CONTEXT_ID && + dg->dst.context ==VMCI_HYPERVISOR_CONTEXT_ID ) +return VMCI_ERROR_DST_UNREACHABLE ; + +ASSERT (dg->dst.context !=VMCI_HYPERVISOR_CONTEXT_ID ); + +/* Check that source handle matches sending context. */ +if (dg->src.context != contextID) { +pr_devel ("Sender context (ID=0x%x) is not owner of src " +"datagram entry (handle=0x%x:0x%x)." , + contextID, dg->src.context, dg->src.resource); +return VMCI_ERROR_NO_ACCESS ; + } + +/* Get hold of privileges of sending endpoint. */ + retval =vmci_dg_get_priv_flags (contextID, dg->src, &srcPrivFlags); +if (retval !=VMCI_SUCCESS ) { +pr_warn ("Couldn't get privileges (handle=0x%x:0x%x)." , + dg->src.context, dg->src.resource); +return retval; + } + +/* Determine if we should route to host or guest destination. */ +if (dg->dst.context ==VMCI_HOST_CONTEXT_ID ) { +/* Route to host datagram entry. */ +struct datagram_entry *dstEntry; +struct vmci_resource *resource; + +if (dg->src.context ==VMCI_HYPERVISOR_CONTEXT_ID && + dg->dst.resource ==VMCI_EVENT_HANDLER ) { +return vmci_event_dispatch (dg); + } + + resource = +vmci_resource_get (dg->dst,VMCI_RESOURCE_TYPE_DATAGRAM ); +if (resource ==NULL ) { +pr_devel ("Sending to invalid destination " +"(handle=0x%x:0x%x)." , dg->dst.context, + dg->dst.resource); +return VMCI_ERROR_INVALID_RESOURCE ; + } + dstEntry = + container_of(resource, struct datagram_entry, + resource); +if (vmci_deny_interaction (srcPrivFlags, dstEntry->privFlags)) { +vmci_resource_release (resource); +return VMCI_ERROR_NO_ACCESS ; + } +ASSERT (dstEntry->recvCB); + +/* +* If a VMCI datagram destined for the host is also sent by the +* host, we always run it delayed. This ensures that no locks +* are held when the datagram callback runs. +*/ + +if (dstEntry->runDelayed + || dg->src.context ==VMCI_HOST_CONTEXT_ID ) { +struct delayed_datagram_info *dgInfo; + +if (atomic_add_return (1 , &delayedDGHostQueueSize) + ==VMCI_MAX_DELAYED_DG_HOST_QUEUE_SIZE ) { +atomic_dec (&delayedDGHostQueueSize); +vmci_resource_release (resource); +return VMCI_ERROR_NO_MEM ; + } + + dgInfo = +kmalloc (sizeof *dgInfo + + (size_t ) dg->payloadSize,GFP_ATOMIC ); +if (NULL == dgInfo) { +atomic_dec (&delayedDGHostQueueSize); +vmci_resource_release (resource); +return VMCI_ERROR_NO_MEM ; + } + + dgInfo->inDGHostQueue = true; + dgInfo->entry = dstEntry; +memcpy (&dgInfo->msg, dg, dgSize); + + retval = +vmci_drv_schedule_delayed_work + (dg_delayed_dispatch_cb, dgInfo); +if (retval <VMCI_SUCCESS ) { +pr_warn ("Failed to schedule delayed " +"work for datagram (result=%d)." , + retval); +kfree (dgInfo); +vmci_resource_release (resource); +atomic_dec (&delayedDGHostQueueSize); +return retval; + } + }else { + retval = dstEntry->recvCB (dstEntry->clientData, dg); +vmci_resource_release (resource); +if (retval <VMCI_SUCCESS ) +return retval; + } + }else { +/* Route to destination VM context. */ +struct vmci_dg *newDG; + +if (contextID != dg->dst.context) { +if (vmci_deny_interaction (srcPrivFlags, +VMCIContext_GetPrivFlags + (dg->dst.context))) { +return VMCI_ERROR_NO_ACCESS ; + }else if (VMCI_CONTEXT_IS_VM (contextID)) { +/* If the sending context is a VM, it cannot reach another VM. */ + +pr_devel ("Datagram communication between VMs not" +"supported (src=0x%x, dst=0x%x)." , contextID, + dg->dst.context); +return VMCI_ERROR_DST_UNREACHABLE ; + } + } + +/* We make a copy to enqueue. */ + newDG =kmalloc (dgSize,GFP_KERNEL ); +if (newDG ==NULL ) +return VMCI_ERROR_NO_MEM ; + +memcpy (newDG, dg, dgSize); + retval =vmci_ctx_enqueue_dg (dg->dst.context, newDG); +if (retval <VMCI_SUCCESS ) { +kfree (newDG); +return retval; + } + } + +/* +* We currently truncate the size to signed 32 bits. This doesn't +* matter for this handler as it only support 4Kb messages. +*/ +return (int )dgSize; +} + +/* +*------------------------------------------------------------------------------ +* +* dg_dispatch_as_guest -- +* +* Dispatch datagram as a guest, down through the VMX and potentially to +* the host. +* +* Result: +* Number of bytes sent on success, appropriate error code otherwise. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +static int dg_dispatch_as_guest (struct vmci_dg *dg) +{ +int retval; +struct vmci_resource *resource; + + resource =vmci_resource_get (dg->src,VMCI_RESOURCE_TYPE_DATAGRAM ); +if (NULL == resource) +return VMCI_ERROR_NO_HANDLE ; + + retval =vmci_send_dg (dg); +vmci_resource_release (resource); +return retval; +} + +/* +*------------------------------------------------------------------------------ +* +* vmci_dg_dispatch -- +* +* Dispatch datagram. This will determine the routing for the datagram +* and dispatch it accordingly. +* +* Result: +* Number of bytes sent on success, appropriate error code otherwise. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +int vmci_dg_dispatch (uint32_t contextID, +struct vmci_dg *dg,bool fromGuest) +{ +int retval; +enum vmci_route route; + +ASSERT (dg); +BUILD_BUG_ON (sizeof (struct vmci_dg ) !=24 ); + +if (VMCI_DG_SIZE (dg) >VMCI_MAX_DG_SIZE ) { +pr_devel ("Payload (size=%llu bytes) too big to " +"send." , (unsigned long long ) dg->payloadSize); +return VMCI_ERROR_INVALID_ARGS ; + } + + retval =vmci_route (&dg->src, &dg->dst, fromGuest, &route); +if (retval <VMCI_SUCCESS ) { +pr_devel ("Failed to route datagram (src=0x%x, dst=0x%x, " +"err=%d)." , dg->src.context, dg->dst.context, + retval); +return retval; + } + +if (VMCI_ROUTE_AS_HOST == route) { +if (VMCI_INVALID_ID == contextID) + contextID =VMCI_HOST_CONTEXT_ID ; +return dg_dispatch_as_host (contextID, dg); + } + +if (VMCI_ROUTE_AS_GUEST == route) +return dg_dispatch_as_guest (dg); + +pr_warn ("Unknown route (%d) for datagram." , route); +return VMCI_ERROR_DST_UNREACHABLE ; +} + +/* +*------------------------------------------------------------------------------ +* +* vmci_dg_invoke_guest_handler -- +* +* Invoke the handler for the given datagram. This is intended to be +* called only when acting as a guest and receiving a datagram from the +* virtual device. +* +* Result: +* VMCI_SUCCESS on success, other error values on failure. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +int vmci_dg_invoke_guest_handler (struct vmci_dg *dg)// IN +{ +int retval; +struct vmci_resource *resource; +struct datagram_entry *dstEntry; + +ASSERT (dg); + + resource =vmci_resource_get (dg->dst,VMCI_RESOURCE_TYPE_DATAGRAM ); +if (NULL == resource) { +pr_devel ("destination (handle=0x%x:0x%x) doesn't exist." , + dg->dst.context, dg->dst.resource); +return VMCI_ERROR_NO_HANDLE ; + } + + dstEntry = + container_of(resource, struct datagram_entry, resource); +if (dstEntry->runDelayed) { +struct delayed_datagram_info *dgInfo; + + dgInfo = +kmalloc (sizeof *dgInfo + (size_t ) dg->payloadSize, +GFP_ATOMIC ); +if (NULL == dgInfo) { +vmci_resource_release (resource); + retval =VMCI_ERROR_NO_MEM ; + goto exit; + } + + dgInfo->inDGHostQueue = false; + dgInfo->entry = dstEntry; +memcpy (&dgInfo->msg, dg,VMCI_DG_SIZE (dg)); + + retval = +vmci_drv_schedule_delayed_work (dg_delayed_dispatch_cb, + dgInfo); +if (retval <VMCI_SUCCESS ) { +pr_warn ("Failed to schedule delayed work for " +"datagram (result=%d)." , retval); +kfree (dgInfo); +vmci_resource_release (resource); + dgInfo =NULL ; + goto exit; + } + }else { + dstEntry->recvCB (dstEntry->clientData, dg); +vmci_resource_release (resource); + retval =VMCI_SUCCESS ; + } + +exit: +return retval; +} + +/* +*------------------------------------------------------------------------------ +* +* VMCIDatagram_Send -- +* +* Sends the payload to the destination datagram handle. +* +* Results: +* Returns number of bytes sent if success, or error code if failure. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +int VMCIDatagram_Send (struct vmci_dg *msg)// IN +{ +if (msg ==NULL ) +return VMCI_ERROR_INVALID_ARGS ; + +return vmci_dg_dispatch (VMCI_INVALID_ID , msg, false); +} + +EXPORT_SYMBOL (VMCIDatagram_Send);
drivers/misc/vmw_vmci/vmci_datagram.h new file +57 -0
57 lines not on this page · raw message
Signature · 1 line
1.7.0.4
RFC 2/11 Apply VMCI datagram code
In reply to Andrew Stiegmann (stieg)Implements datagrams to allow data to be sent between host and guest.
--- drivers/misc/vmw_vmci/vmci_datagram.c | 760 +++++++++++++++++++++++++++++++++ drivers/misc/vmw_vmci/vmci_datagram.h | 57 +++ 2 files changed, 817 insertions(+), 0 deletions(-) create mode 100644 drivers/misc/vmw_vmci/vmci_datagram.c create mode 100644 drivers/misc/vmw_vmci/vmci_datagram.h
drivers/misc/vmw_vmci/vmci_datagram.c new file +760 -0
@@ -0,0 +1,760 @@ +/* +* VMware VMCI Driver +* +* Copyright (C) 2012 VMware, Inc. All rights reserved. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms of the GNU General Public License as published by the +* Free Software Foundation version 2 and no later version. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +* for more details. +* +* You should have received a copy of the GNU General Public License along +* with this program; if not, write to the Free Software Foundation, Inc., +* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include <linux/bug.h> +#include <linux/module.h> +#include <linux/sched.h> +#include <linux/vmw_vmci_api.h> +#include <linux/vmw_vmci_defs.h> + +#include "vmci_common_int.h" +#include "vmci_context.h" +#include "vmci_datagram.h" +#include "vmci_driver.h" +#include "vmci_event.h" +#include "vmci_hash_table.h" +#include "vmci_resource.h" +#include "vmci_route.h" + +/* +* struct datagram_entry describes the datagram entity. It is used for datagram +* entities created only on the host. +*/ +struct datagram_entry { +struct vmci_resource resource; +uint32_t flags; +bool runDelayed; +VMCIDatagramRecvCB recvCB; +void *clientData; +wait_queue_head_t destroyEvent; +uint32_t privFlags; +}; + +struct delayed_datagram_info { +bool inDGHostQueue; +struct datagram_entry *entry; +struct vmci_dg msg; +}; + +static atomic_t delayedDGHostQueueSize; + +/* +*------------------------------------------------------------------------------ +* +* dg_free_cb -- +* Callback to free datagram structure when resource is no longer used, +* ie. the reference count reached 0. +* +* Result: +* None. +* +*------------------------------------------------------------------------------ +*/ + +static void dg_free_cb (void *clientData) +{ +struct datagram_entry *entry = (struct datagram_entry *)clientData; +ASSERT (entry); +/* Entry is freed in VMCIDatagram_DestroyHnd, who waits for the signal */ +wake_up (&entry->destroyEvent); +} + +/* +*------------------------------------------------------------------------------ +* +* dg_release_cb -- +* +* Callback to release the resource reference. It is called by the +* VMCI_WaitOnEvent function before it blocks. +* +* Result: +* None. +* +*------------------------------------------------------------------------------ +*/ + +static int dg_release_cb (void *clientData) +{ +struct datagram_entry *entry = (struct datagram_entry *)clientData; +ASSERT (entry); +vmci_resource_release (&entry->resource); +return 0 ; +} + +/* +*------------------------------------------------------------------------------ +* +* dg_create_handle -- +* +* Internal function to create a datagram entry given a handle. +* +* Results: +* VMCI_SUCCESS if created, negative errno value otherwise. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +static int dg_create_handle (uint32_t resourceID,// IN: +uint32_t flags,// IN: +uint32_t privFlags,// IN: +VMCIDatagramRecvCB recvCB,// IN: +void *clientData,// IN: +struct vmci_handle *outHandle)// OUT: +{ +int result; +uint32_t contextID; +struct vmci_handle handle; +struct datagram_entry *entry; + +ASSERT (recvCB !=NULL ); +ASSERT (outHandle !=NULL ); +ASSERT (!(privFlags & ~VMCI_PRIVILEGE_ALL_FLAGS )); + +if ((flags &VMCI_FLAG_WELLKNOWN_DG_HND ) !=0 ) { +return VMCI_ERROR_INVALID_ARGS ; + }else { +if ((flags &VMCI_FLAG_ANYCID_DG_HND ) !=0 ) { + contextID =VMCI_INVALID_ID ; + }else { + contextID =VMCI_GetContextID (); +if (contextID ==VMCI_INVALID_ID ) +return VMCI_ERROR_NO_RESOURCES ; + } + +if (resourceID ==VMCI_INVALID_ID ) { + resourceID =vmci_resource_get_id (contextID); +if (resourceID ==VMCI_INVALID_ID ) +return VMCI_ERROR_NO_HANDLE ; + } + + handle =vmci_make_handle (contextID, resourceID); + } + + entry =kmalloc (sizeof *entry,GFP_KERNEL ); +if (entry ==NULL ) { +pr_warn ("Failed allocating memory for datagram entry." ); +return VMCI_ERROR_NO_MEM ; + } + + entry->runDelayed = (flags &VMCI_FLAG_DG_DELAYED_CB ) ? true : false; + entry->flags = flags; + entry->recvCB = recvCB; + entry->clientData = clientData; +init_waitqueue_head (&entry->destroyEvent); + entry->privFlags = privFlags; + +/* Make datagram resource live. */ + result = +vmci_resource_add (&entry->resource,VMCI_RESOURCE_TYPE_DATAGRAM , + handle, dg_free_cb, entry); +if (result !=VMCI_SUCCESS ) { +pr_warn ("Failed to add new resource (handle=0x%x:0x%x)." , + handle.context, handle.resource); +kfree (entry); +return result; + } + *outHandle = handle; + +return VMCI_SUCCESS ; +} + +/* +*------------------------------------------------------------------------------ +* +* vmci_dg_init -- +* +* Initialize Datagram API, ie. register the API functions with their +* corresponding vectors. +* +* Result: +* None. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +int __init vmci_dg_init (void ) +{ +atomic_set (&delayedDGHostQueueSize,0 ); +return VMCI_SUCCESS ; +} + +/* +*------------------------------------------------------------------------------ +* +* VMCIDatagram_CreateHnd -- +* +* Creates a host context datagram endpoint and returns a handle to it. +* +* Results: +* VMCI_SUCCESS if created, negative errno value otherwise. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +int VMCIDatagram_CreateHnd (uint32_t resourceID,// IN: Optional, generated +// if VMCI_INVALID_ID +uint32_t flags,// IN: +VMCIDatagramRecvCB recvCB,// IN: +void *clientData,// IN: +struct vmci_handle *outHandle)// OUT: newly created handle +{ +if (outHandle ==NULL ) +return VMCI_ERROR_INVALID_ARGS ; + +if (recvCB ==NULL ) { +pr_devel ("Client callback needed when creating " +"datagram." ); +return VMCI_ERROR_INVALID_ARGS ; + } + +return dg_create_handle (resourceID, flags, +VMCI_DEFAULT_PROC_PRIVILEGE_FLAGS , recvCB, + clientData, outHandle); +} + +EXPORT_SYMBOL (VMCIDatagram_CreateHnd); + +/* +*------------------------------------------------------------------------------ +* +* VMCIDatagram_CreateHndPriv -- +* +* Creates a host context datagram endpoint and returns a handle to it. +* +* Results: +* VMCI_SUCCESS if created, negative errno value otherwise. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +int VMCIDatagram_CreateHndPriv (uint32_t resourceID,// IN: Optional, generated +// if VMCI_INVALID_ID +uint32_t flags,// IN: +uint32_t privFlags,// IN: +VMCIDatagramRecvCB recvCB,// IN: +void *clientData,// IN: +struct vmci_handle *outHandle)// OUT: newly created handle +{ +if (outHandle ==NULL ) +return VMCI_ERROR_INVALID_ARGS ; + +if (recvCB ==NULL ) { +pr_devel ("Client callback needed when creating " +"datagram." ); +return VMCI_ERROR_INVALID_ARGS ; + } + +if (privFlags & ~VMCI_PRIVILEGE_ALL_FLAGS ) +return VMCI_ERROR_INVALID_ARGS ; + +return dg_create_handle (resourceID, flags, privFlags, recvCB, + clientData, outHandle); +} + +EXPORT_SYMBOL (VMCIDatagram_CreateHndPriv); + +/* +*------------------------------------------------------------------------------ +* +* VMCIDatagram_DestroyHnd -- +* +* Destroys a handle. +* +* Results: +* None. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +int VMCIDatagram_DestroyHnd (struct vmci_handle handle)// IN +{ +struct datagram_entry *entry; +struct vmci_resource *resource =vmci_resource_get (handle, +VMCI_RESOURCE_TYPE_DATAGRAM ); +if (resource ==NULL ) { +pr_devel ("Failed to destroy datagram (handle=0x%x:0x%x)" +"." , handle.context, handle.resource); +return VMCI_ERROR_NOT_FOUND ; + } + entry = container_of(resource, struct datagram_entry, resource); + +vmci_resource_remove (handle,VMCI_RESOURCE_TYPE_DATAGRAM ); + +/* +* We now wait on the destroyEvent and release the reference we got +* above. +*/ +vmci_drv_wait_on_event_intr (&entry->destroyEvent, dg_release_cb, + entry); + +/* +* We know that we are now the only reference to the above entry so +* can safely free it. +*/ +kfree (entry); + +return VMCI_SUCCESS ; +} + +EXPORT_SYMBOL (VMCIDatagram_DestroyHnd); + +/* +*------------------------------------------------------------------------------ +* +* vmci_dg_get_priv_flags -- +* +* Internal utilility function with the same purpose as +* vmci_dg_get_priv_flags that also takes a contextID. +* +* Result: +* VMCI_SUCCESS on success, VMCI_ERROR_INVALID_ARGS if handle is invalid. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +static int vmci_dg_get_priv_flags (uint32_t contextID,// IN +struct vmci_handle handle,// IN +uint32_t * privFlags)// OUT +{ +ASSERT (privFlags); +ASSERT (contextID !=VMCI_INVALID_ID ); + +if (contextID ==VMCI_HOST_CONTEXT_ID ) { +struct datagram_entry *srcEntry; +struct vmci_resource *resource; + + resource = +vmci_resource_get (handle,VMCI_RESOURCE_TYPE_DATAGRAM ); +if (resource ==NULL ) +return VMCI_ERROR_INVALID_ARGS ; + + srcEntry = container_of(resource, struct datagram_entry, + resource); + *privFlags = srcEntry->privFlags; +vmci_resource_release (resource); + }else if (contextID ==VMCI_HYPERVISOR_CONTEXT_ID ) { + *privFlags =VMCI_MAX_PRIVILEGE_FLAGS ; + }else { + *privFlags =VMCIContext_GetPrivFlags (contextID); + } + +return VMCI_SUCCESS ; +} + +/* +*----------------------------------------------------------------------------- +* +* dg_delayed_dispatch_cb -- +* +* Calls the specified callback in a delayed context. +* +* Results: +* None. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +static void dg_delayed_dispatch_cb (void *data)// IN +{ +bool inDGHostQueue; +struct delayed_datagram_info *dgInfo = + (struct delayed_datagram_info *)data; + +ASSERT (data); + + dgInfo->entry->recvCB (dgInfo->entry->clientData, &dgInfo->msg); + +vmci_resource_release (&dgInfo->entry->resource); + + inDGHostQueue = dgInfo->inDGHostQueue; +kfree (dgInfo); + +if (inDGHostQueue) +atomic_dec (&delayedDGHostQueueSize); +} + +/* +*------------------------------------------------------------------------------ +* +* dg_dispatch_as_host -- +* +* Dispatch datagram as a host, to the host or other vm context. This +* function cannot dispatch to hypervisor context handlers. This should +* have been handled before we get here by VMCIDatagramDispatch. +* +* Result: +* Number of bytes sent on success, appropriate error code otherwise. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +static int dg_dispatch_as_host (uint32_t contextID,// IN: +struct vmci_dg *dg)// IN: +{ +int retval; +size_t dgSize; +uint32_t srcPrivFlags; + +ASSERT (dg); +ASSERT (vmci_host_code_active ()); + + dgSize =VMCI_DG_SIZE (dg); + +if (contextID ==VMCI_HOST_CONTEXT_ID && + dg->dst.context ==VMCI_HYPERVISOR_CONTEXT_ID ) +return VMCI_ERROR_DST_UNREACHABLE ; + +ASSERT (dg->dst.context !=VMCI_HYPERVISOR_CONTEXT_ID ); + +/* Check that source handle matches sending context. */ +if (dg->src.context != contextID) { +pr_devel ("Sender context (ID=0x%x) is not owner of src " +"datagram entry (handle=0x%x:0x%x)." , + contextID, dg->src.context, dg->src.resource); +return VMCI_ERROR_NO_ACCESS ; + } + +/* Get hold of privileges of sending endpoint. */ + retval =vmci_dg_get_priv_flags (contextID, dg->src, &srcPrivFlags); +if (retval !=VMCI_SUCCESS ) { +pr_warn ("Couldn't get privileges (handle=0x%x:0x%x)." , + dg->src.context, dg->src.resource); +return retval; + } + +/* Determine if we should route to host or guest destination. */ +if (dg->dst.context ==VMCI_HOST_CONTEXT_ID ) { +/* Route to host datagram entry. */ +struct datagram_entry *dstEntry; +struct vmci_resource *resource; + +if (dg->src.context ==VMCI_HYPERVISOR_CONTEXT_ID && + dg->dst.resource ==VMCI_EVENT_HANDLER ) { +return vmci_event_dispatch (dg); + } + + resource = +vmci_resource_get (dg->dst,VMCI_RESOURCE_TYPE_DATAGRAM ); +if (resource ==NULL ) { +pr_devel ("Sending to invalid destination " +"(handle=0x%x:0x%x)." , dg->dst.context, + dg->dst.resource); +return VMCI_ERROR_INVALID_RESOURCE ; + } + dstEntry = + container_of(resource, struct datagram_entry, + resource); +if (vmci_deny_interaction (srcPrivFlags, dstEntry->privFlags)) { +vmci_resource_release (resource); +return VMCI_ERROR_NO_ACCESS ; + } +ASSERT (dstEntry->recvCB); + +/* +* If a VMCI datagram destined for the host is also sent by the +* host, we always run it delayed. This ensures that no locks +* are held when the datagram callback runs. +*/ + +if (dstEntry->runDelayed + || dg->src.context ==VMCI_HOST_CONTEXT_ID ) { +struct delayed_datagram_info *dgInfo; + +if (atomic_add_return (1 , &delayedDGHostQueueSize) + ==VMCI_MAX_DELAYED_DG_HOST_QUEUE_SIZE ) { +atomic_dec (&delayedDGHostQueueSize); +vmci_resource_release (resource); +return VMCI_ERROR_NO_MEM ; + } + + dgInfo = +kmalloc (sizeof *dgInfo + + (size_t ) dg->payloadSize,GFP_ATOMIC ); +if (NULL == dgInfo) { +atomic_dec (&delayedDGHostQueueSize); +vmci_resource_release (resource); +return VMCI_ERROR_NO_MEM ; + } + + dgInfo->inDGHostQueue = true; + dgInfo->entry = dstEntry; +memcpy (&dgInfo->msg, dg, dgSize); + + retval = +vmci_drv_schedule_delayed_work + (dg_delayed_dispatch_cb, dgInfo); +if (retval <VMCI_SUCCESS ) { +pr_warn ("Failed to schedule delayed " +"work for datagram (result=%d)." , + retval); +kfree (dgInfo); +vmci_resource_release (resource); +atomic_dec (&delayedDGHostQueueSize); +return retval; + } + }else { + retval = dstEntry->recvCB (dstEntry->clientData, dg); +vmci_resource_release (resource); +if (retval <VMCI_SUCCESS ) +return retval; + } + }else { +/* Route to destination VM context. */ +struct vmci_dg *newDG; + +if (contextID != dg->dst.context) { +if (vmci_deny_interaction (srcPrivFlags, +VMCIContext_GetPrivFlags + (dg->dst.context))) { +return VMCI_ERROR_NO_ACCESS ; + }else if (VMCI_CONTEXT_IS_VM (contextID)) { +/* If the sending context is a VM, it cannot reach another VM. */ + +pr_devel ("Datagram communication between VMs not" +"supported (src=0x%x, dst=0x%x)." , contextID, + dg->dst.context); +return VMCI_ERROR_DST_UNREACHABLE ; + } + } + +/* We make a copy to enqueue. */ + newDG =kmalloc (dgSize,GFP_KERNEL ); +if (newDG ==NULL ) +return VMCI_ERROR_NO_MEM ; + +memcpy (newDG, dg, dgSize); + retval =vmci_ctx_enqueue_dg (dg->dst.context, newDG); +if (retval <VMCI_SUCCESS ) { +kfree (newDG); +return retval; + } + } + +/* +* We currently truncate the size to signed 32 bits. This doesn't +* matter for this handler as it only support 4Kb messages. +*/ +return (int )dgSize; +} + +/* +*------------------------------------------------------------------------------ +* +* dg_dispatch_as_guest -- +* +* Dispatch datagram as a guest, down through the VMX and potentially to +* the host. +* +* Result: +* Number of bytes sent on success, appropriate error code otherwise. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +static int dg_dispatch_as_guest (struct vmci_dg *dg) +{ +int retval; +struct vmci_resource *resource; + + resource =vmci_resource_get (dg->src,VMCI_RESOURCE_TYPE_DATAGRAM ); +if (NULL == resource) +return VMCI_ERROR_NO_HANDLE ; + + retval =vmci_send_dg (dg); +vmci_resource_release (resource); +return retval; +} + +/* +*------------------------------------------------------------------------------ +* +* vmci_dg_dispatch -- +* +* Dispatch datagram. This will determine the routing for the datagram +* and dispatch it accordingly. +* +* Result: +* Number of bytes sent on success, appropriate error code otherwise. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +int vmci_dg_dispatch (uint32_t contextID, +struct vmci_dg *dg,bool fromGuest) +{ +int retval; +enum vmci_route route; + +ASSERT (dg); +BUILD_BUG_ON (sizeof (struct vmci_dg ) !=24 ); + +if (VMCI_DG_SIZE (dg) >VMCI_MAX_DG_SIZE ) { +pr_devel ("Payload (size=%llu bytes) too big to " +"send." , (unsigned long long ) dg->payloadSize); +return VMCI_ERROR_INVALID_ARGS ; + } + + retval =vmci_route (&dg->src, &dg->dst, fromGuest, &route); +if (retval <VMCI_SUCCESS ) { +pr_devel ("Failed to route datagram (src=0x%x, dst=0x%x, " +"err=%d)." , dg->src.context, dg->dst.context, + retval); +return retval; + } + +if (VMCI_ROUTE_AS_HOST == route) { +if (VMCI_INVALID_ID == contextID) + contextID =VMCI_HOST_CONTEXT_ID ; +return dg_dispatch_as_host (contextID, dg); + } + +if (VMCI_ROUTE_AS_GUEST == route) +return dg_dispatch_as_guest (dg); + +pr_warn ("Unknown route (%d) for datagram." , route); +return VMCI_ERROR_DST_UNREACHABLE ; +} + +/* +*------------------------------------------------------------------------------ +* +* vmci_dg_invoke_guest_handler -- +* +* Invoke the handler for the given datagram. This is intended to be +* called only when acting as a guest and receiving a datagram from the +* virtual device. +* +* Result: +* VMCI_SUCCESS on success, other error values on failure. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +int vmci_dg_invoke_guest_handler (struct vmci_dg *dg)// IN +{ +int retval; +struct vmci_resource *resource; +struct datagram_entry *dstEntry; + +ASSERT (dg); + + resource =vmci_resource_get (dg->dst,VMCI_RESOURCE_TYPE_DATAGRAM ); +if (NULL == resource) { +pr_devel ("destination (handle=0x%x:0x%x) doesn't exist." , + dg->dst.context, dg->dst.resource); +return VMCI_ERROR_NO_HANDLE ; + } + + dstEntry = + container_of(resource, struct datagram_entry, resource); +if (dstEntry->runDelayed) { +struct delayed_datagram_info *dgInfo; + + dgInfo = +kmalloc (sizeof *dgInfo + (size_t ) dg->payloadSize, +GFP_ATOMIC ); +if (NULL == dgInfo) { +vmci_resource_release (resource); + retval =VMCI_ERROR_NO_MEM ; + goto exit; + } + + dgInfo->inDGHostQueue = false; + dgInfo->entry = dstEntry; +memcpy (&dgInfo->msg, dg,VMCI_DG_SIZE (dg)); + + retval = +vmci_drv_schedule_delayed_work (dg_delayed_dispatch_cb, + dgInfo); +if (retval <VMCI_SUCCESS ) { +pr_warn ("Failed to schedule delayed work for " +"datagram (result=%d)." , retval); +kfree (dgInfo); +vmci_resource_release (resource); + dgInfo =NULL ; + goto exit; + } + }else { + dstEntry->recvCB (dstEntry->clientData, dg); +vmci_resource_release (resource); + retval =VMCI_SUCCESS ; + } + +exit: +return retval; +} + +/* +*------------------------------------------------------------------------------ +* +* VMCIDatagram_Send -- +* +* Sends the payload to the destination datagram handle. +* +* Results: +* Returns number of bytes sent if success, or error code if failure. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +int VMCIDatagram_Send (struct vmci_dg *msg)// IN +{ +if (msg ==NULL ) +return VMCI_ERROR_INVALID_ARGS ; + +return vmci_dg_dispatch (VMCI_INVALID_ID , msg, false); +} + +EXPORT_SYMBOL (VMCIDatagram_Send);
drivers/misc/vmw_vmci/vmci_datagram.h new file +57 -0
57 lines not on this page · raw message
Signature · 1 line
1.7.0.4
RFC 3/11 Apply VMCI doorbell code
In reply to Andrew Stiegmann (stieg)Doorbell code allows for notifcations between host and guest.
--- drivers/misc/vmw_vmci/vmci_doorbell.c | 989 +++++++++++++++++++++++++++++++++ drivers/misc/vmw_vmci/vmci_doorbell.h | 57 ++ 2 files changed, 1046 insertions(+), 0 deletions(-) create mode 100644 drivers/misc/vmw_vmci/vmci_doorbell.c create mode 100644 drivers/misc/vmw_vmci/vmci_doorbell.h
drivers/misc/vmw_vmci/vmci_doorbell.c new file +989 -0
@@ -0,0 +1,989 @@ +/* +* VMware VMCI Driver +* +* Copyright (C) 2012 VMware, Inc. All rights reserved. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms of the GNU General Public License as published by the +* Free Software Foundation version 2 and no later version. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +* for more details. +* +* You should have received a copy of the GNU General Public License along +* with this program; if not, write to the Free Software Foundation, Inc., +* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/sched.h> +#include <linux/vmw_vmci_api.h> +#include <linux/vmw_vmci_defs.h> + +#include "vmci_common_int.h" +#include "vmci_datagram.h" +#include "vmci_doorbell.h" +#include "vmci_driver.h" +#include "vmci_resource.h" +#include "vmci_route.h" + +#define VMCI_DOORBELL_INDEX_TABLE_SIZE (1 << 6) +#define VMCI_DOORBELL_HASH (_idx) \ + vmci_hash_calc((_idx), VMCI_DOORBELL_INDEX_TABLE_SIZE) + +/* +* DoorbellEntry describes the a doorbell notification handle allocated by the +* host. +*/ +struct dbell_entry { +struct vmci_resource resource; +uint32_t idx; +struct list_head idxListItem; +uint32_t privFlags; +bool runDelayed; +VMCICallback notifyCB; +void *clientData; +wait_queue_head_t destroyEvent; +atomic_t active;/* Only used by guest personality */ +}; + +/* The VMCI index table keeps track of currently registered doorbells. */ +static struct dbell_index_table { +spinlock_t lock; +struct list_head entries[VMCI_DOORBELL_INDEX_TABLE_SIZE ]; +} vmciDoorbellIT; + +/* +* The maxNotifyIdx is one larger than the currently known bitmap index in +* use, and is used to determine how much of the bitmap needs to be scanned. +*/ +static uint32_t maxNotifyIdx; + +/* +* The notifyIdxCount is used for determining whether there are free entries +* within the bitmap (if notifyIdxCount + 1 < maxNotifyIdx). +*/ +static uint32_t notifyIdxCount; + +/* +* The lastNotifyIdxReserved is used to track the last index handed out - in +* the case where multiple handles share a notification index, we hand out +* indexes round robin based on lastNotifyIdxReserved. +*/ +static uint32_t lastNotifyIdxReserved; + +/* This is a one entry cache used to by the index allocation. */ +static uint32_t lastNotifyIdxReleased =PAGE_SIZE ; + +/* +*------------------------------------------------------------------------------ +* +* vmci_dbell_init -- +* +* General init code. +* +* Result: +* VMCI_SUCCESS on success, lock allocation error otherwise. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +int __init vmci_dbell_init (void ) +{ +uint32_t bucket; + +for (bucket =0 ; bucket <ARRAY_SIZE (vmciDoorbellIT.entries); ++bucket) +INIT_LIST_HEAD (&vmciDoorbellIT.entries[bucket]); + +spin_lock_init (&vmciDoorbellIT.lock); +return VMCI_SUCCESS ; +} + +/* +*------------------------------------------------------------------------------ +* +* dbell_free_cb -- +* +* Callback to free doorbell entry structure when resource is no longer used, +* ie. the reference count reached 0. The entry is freed in +* VMCIDoorbell_Destroy(), which is waiting on the signal that gets fired +* here. +* +* Result: +* None. +* +* Side effects: +* Signals VMCI event. +* +*------------------------------------------------------------------------------ +*/ + +static void dbell_free_cb (void *clientData)// IN +{ +struct dbell_entry *entry = (struct dbell_entry *)clientData; +ASSERT (entry); +wake_up (&entry->destroyEvent); +} + +/* +*------------------------------------------------------------------------------ +* +* dbell_release_cb -- +* +* Callback to release the resource reference. It is called by the +* VMCI_WaitOnEvent function before it blocks. +* +* Result: +* Always 0. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +static int dbell_release_cb (void *clientData)// IN: doorbell entry +{ +struct dbell_entry *entry = (struct dbell_entry *)clientData; +ASSERT (entry); +vmci_resource_release (&entry->resource); +return 0 ; +} + +/* +*------------------------------------------------------------------------------ +* +* vmci_dbell_get_priv_flags -- +* +* Utility function that retrieves the privilege flags associated +* with a given doorbell handle. For guest endpoints, the +* privileges are determined by the context ID, but for host +* endpoints privileges are associated with the complete +* handle. Hypervisor endpoints are not yet supported. +* +* Result: +* VMCI_SUCCESS on success, +* VMCI_ERROR_NOT_FOUND if handle isn't found, +* VMCI_ERROR_INVALID_ARGS if handle is invalid. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +int vmci_dbell_get_priv_flags (struct vmci_handle handle,// IN +uint32_t * privFlags)// OUT +{ +if (privFlags ==NULL || handle.context ==VMCI_INVALID_ID ) +return VMCI_ERROR_INVALID_ARGS ; + +if (handle.context ==VMCI_HOST_CONTEXT_ID ) { +struct dbell_entry *entry; +struct vmci_resource *resource; + + resource = +vmci_resource_get (handle,VMCI_RESOURCE_TYPE_DOORBELL ); +if (resource ==NULL ) +return VMCI_ERROR_NOT_FOUND ; + + entry = + container_of(resource, struct dbell_entry, + resource); + *privFlags = entry->privFlags; +vmci_resource_release (resource); + }else if (handle.context ==VMCI_HYPERVISOR_CONTEXT_ID ) { +/* Hypervisor endpoints for notifications are not supported (yet). */ +return VMCI_ERROR_INVALID_ARGS ; + }else { + *privFlags =VMCIContext_GetPrivFlags (handle.context); + } + +return VMCI_SUCCESS ; +} + +/* +*----------------------------------------------------------------------------- +* +* dbell_index_table_find -- +* +* Find doorbell entry by bitmap index. +* +* Results: +* Entry if found, NULL if not. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +static struct dbell_entry *dbell_index_table_find (uint32_t idx)// IN +{ +uint32_t bucket =VMCI_DOORBELL_HASH (idx); +struct dbell_entry *cur; + +ASSERT (vmci_guest_code_active ()); + +list_for_each_entry (cur, &vmciDoorbellIT.entries[bucket], idxListItem) { +if (idx == cur->idx) +return cur; + } + +return NULL ; +} + +/* +*------------------------------------------------------------------------------ +* +* dbell_index_table_add -- +* +* Add the given entry to the index table. This will hold() the entry's +* resource so that the entry is not deleted before it is removed from the +* table. +* +* Results: +* None. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +static void dbell_index_table_add (struct dbell_entry *entry)// IN/OUT +{ +uint32_t bucket; +uint32_t newNotifyIdx; + +ASSERT (entry); +ASSERT (vmci_guest_code_active ()); + +vmci_resource_hold (&entry->resource); + +spin_lock_bh (&vmciDoorbellIT.lock); + +/* +* Below we try to allocate an index in the notification bitmap with "not +* too much" sharing between resources. If we use less that the full bitmap, +* we either add to the end if there are no unused flags within the +* currently used area, or we search for unused ones. If we use the full +* bitmap, we allocate the index round robin. +*/ + +if (maxNotifyIdx <PAGE_SIZE || notifyIdxCount <PAGE_SIZE ) { +if (lastNotifyIdxReleased < maxNotifyIdx && + !dbell_index_table_find (lastNotifyIdxReleased)) { + newNotifyIdx = lastNotifyIdxReleased; + lastNotifyIdxReleased =PAGE_SIZE ; + }else { +bool reused = false; + newNotifyIdx = lastNotifyIdxReserved; +if (notifyIdxCount +1 < maxNotifyIdx) { +do { +if (!dbell_index_table_find + (newNotifyIdx)) { + reused = true; +break ; + } + newNotifyIdx = + (newNotifyIdx +1 ) % maxNotifyIdx; + }while (newNotifyIdx != lastNotifyIdxReleased); + } +if (!reused) { + newNotifyIdx = maxNotifyIdx; + maxNotifyIdx++; + } + } + }else { + newNotifyIdx = (lastNotifyIdxReserved +1 ) %PAGE_SIZE ; + } + + lastNotifyIdxReserved = newNotifyIdx; + notifyIdxCount++; + + entry->idx = newNotifyIdx; + bucket =VMCI_DOORBELL_HASH (entry->idx); +list_add (&entry->idxListItem, &vmciDoorbellIT.entries[bucket]); + +spin_unlock_bh (&vmciDoorbellIT.lock); +} + +/* +*------------------------------------------------------------------------------ +* +* dbell_index_table_remove -- +* +* Remove the given entry from the index table. This will release() the +* entry's resource. +* +* Results: +* None. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +static void dbell_index_table_remove (struct dbell_entry *entry)// IN/OUT +{ +ASSERT (entry); +ASSERT (vmci_guest_code_active ()); + +spin_lock_bh (&vmciDoorbellIT.lock); + +list_del (&entry->idxListItem); + + notifyIdxCount--; +if (entry->idx == maxNotifyIdx -1 ) { +/* +* If we delete an entry with the maximum known notification index, we +* take the opportunity to prune the current max. As there might be other +* unused indices immediately below, we lower the maximum until we hit an +* index in use. +*/ + +while (maxNotifyIdx >0 && + !dbell_index_table_find (maxNotifyIdx -1 )) { + maxNotifyIdx--; + } + } + + lastNotifyIdxReleased = entry->idx; + +spin_unlock_bh (&vmciDoorbellIT.lock); + +vmci_resource_release (&entry->resource); +} + +/* +*------------------------------------------------------------------------------ +* +* dbell_link -- +* +* Creates a link between the given doorbell handle and the given +* index in the bitmap in the device backend. +* +* Results: +* VMCI_SUCCESS if success, appropriate error code otherwise. +* +* Side effects: +* Notification state is created in hypervisor. +* +*------------------------------------------------------------------------------ +*/ + +static int dbell_link (struct vmci_handle handle,// IN +uint32_t notifyIdx)// IN +{ +struct vmci_doorbell_link_msg linkMsg; + +ASSERT (!VMCI_HANDLE_INVALID (handle)); +ASSERT (vmci_guest_code_active ()); + + linkMsg.hdr.dst = +vmci_make_handle (VMCI_HYPERVISOR_CONTEXT_ID ,VMCI_DOORBELL_LINK ); + linkMsg.hdr.src =VMCI_ANON_SRC_HANDLE ; + linkMsg.hdr.payloadSize =sizeof linkMsg -VMCI_DG_HEADERSIZE ; + linkMsg.handle = handle; + linkMsg.notifyIdx = notifyIdx; + +return vmci_send_dg ((struct vmci_dg *)&linkMsg); +} + +/* +*------------------------------------------------------------------------------ +* +* dbell_unlink -- +* +* Unlinks the given doorbell handle from an index in the bitmap in +* the device backend. +* +* Results: +* VMCI_SUCCESS if success, appropriate error code otherwise. +* +* Side effects: +* Notification state is destroyed in hypervisor. +* +*------------------------------------------------------------------------------ +*/ + +static int dbell_unlink (struct vmci_handle handle) +{ +struct vmci_doorbell_unlink_msg unlinkMsg; + +ASSERT (!VMCI_HANDLE_INVALID (handle)); +ASSERT (vmci_guest_code_active ()); + + unlinkMsg.hdr.dst = +vmci_make_handle (VMCI_HYPERVISOR_CONTEXT_ID ,VMCI_DOORBELL_UNLINK ); + unlinkMsg.hdr.src =VMCI_ANON_SRC_HANDLE ; + unlinkMsg.hdr.payloadSize =sizeof unlinkMsg -VMCI_DG_HEADERSIZE ; + unlinkMsg.handle = handle; + +return vmci_send_dg ((struct vmci_dg *)&unlinkMsg); +} + +/* +*------------------------------------------------------------------------------ +* +* VMCIDoorbell_Create -- +* +* Creates a doorbell with the given callback. If the handle is +* VMCI_INVALID_HANDLE, a free handle will be assigned, if +* possible. The callback can be run immediately (potentially with +* locks held - the default) or delayed (in a kernel thread) by +* specifying the flag VMCI_FLAG_DELAYED_CB. If delayed execution +* is selected, a given callback may not be run if the kernel is +* unable to allocate memory for the delayed execution (highly +* unlikely). +* +* Results: +* VMCI_SUCCESS on success, appropriate error code otherwise. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +int VMCIDoorbell_Create (struct vmci_handle *handle,// IN/OUT +uint32_t flags,// IN +uint32_t privFlags,// IN +VMCICallback notifyCB,// IN +void *clientData)// IN +{ +struct dbell_entry *entry; +struct vmci_handle newHandle; +int result; + +if (!handle || !notifyCB || flags & ~VMCI_FLAG_DELAYED_CB || + privFlags & ~VMCI_PRIVILEGE_ALL_FLAGS ) +return VMCI_ERROR_INVALID_ARGS ; + + entry =kmalloc (sizeof *entry,GFP_KERNEL ); +if (entry ==NULL ) { +pr_warn ("Failed allocating memory for datagram entry." ); +return VMCI_ERROR_NO_MEM ; + } + +if (VMCI_HANDLE_INVALID (*handle)) { +uint32_t contextID =VMCI_GetContextID (); +uint32_t resourceID =vmci_resource_get_id (contextID); +if (resourceID ==VMCI_INVALID_ID ) { + result =VMCI_ERROR_NO_HANDLE ; + goto freeMem; + } + newHandle =vmci_make_handle (contextID, resourceID); + }else { +bool validContext = false; + +/* +* Validate the handle. We must do both of the checks below +* because we can be acting as both a host and a guest at the +* same time. We always allow the host context ID, since the +* host functionality is in practice always there with the +* unified driver. +*/ + +if (VMCI_HOST_CONTEXT_ID == handle->context || + (vmci_guest_code_active () && +VMCI_GetContextID () == handle->context)) + validContext = true; + +if (!validContext ||VMCI_INVALID_ID == handle->resource) { +pr_devel ("Invalid argument (handle=0x%x:0x%x)." , + handle->context, handle->resource); + result =VMCI_ERROR_INVALID_ARGS ; + goto freeMem; + } + + newHandle = *handle; + } + + entry->idx =0 ; +INIT_LIST_HEAD (&entry->idxListItem); + entry->privFlags = privFlags; + entry->runDelayed = (flags &VMCI_FLAG_DELAYED_CB ) ? true : false; + entry->notifyCB = notifyCB; + entry->clientData = clientData; +atomic_set (&entry->active,0 ); +init_waitqueue_head (&entry->destroyEvent); + + result = +vmci_resource_add (&entry->resource,VMCI_RESOURCE_TYPE_DOORBELL , + newHandle, dbell_free_cb, entry); +if (result !=VMCI_SUCCESS ) { +pr_warn ("Failed to add new resource (handle=0x%x:0x%x)." , + newHandle.context, newHandle.resource); +if (result ==VMCI_ERROR_DUPLICATE_ENTRY ) + result =VMCI_ERROR_ALREADY_EXISTS ; + + goto freeMem; + } + +if (vmci_guest_code_active ()) { +dbell_index_table_add (entry); + result =dbell_link (newHandle, entry->idx); +if (VMCI_SUCCESS != result) + goto destroyResource; + +atomic_set (&entry->active,1 ); + } + +if (VMCI_HANDLE_INVALID (*handle)) + *handle = newHandle; + +return result; + +destroyResource: +dbell_index_table_remove (entry); +vmci_resource_remove (newHandle,VMCI_RESOURCE_TYPE_DOORBELL ); +freeMem: +kfree (entry); +return result; +} + +EXPORT_SYMBOL (VMCIDoorbell_Create); + +/* +*------------------------------------------------------------------------------ +* +* VMCIDoorbell_Destroy -- +* +* Destroys a doorbell previously created with +* VMCIDoorbell_Create. This operation may block waiting for a +* callback to finish. +* +* Results: +* VMCI_SUCCESS on success, appropriate error code otherwise. +* +* Side effects: +* May block. +* +*------------------------------------------------------------------------------ +*/ + +int VMCIDoorbell_Destroy (struct vmci_handle handle)// IN +{ +struct dbell_entry *entry; +struct vmci_resource *resource; + +if (VMCI_HANDLE_INVALID (handle)) +return VMCI_ERROR_INVALID_ARGS ; + + resource =vmci_resource_get (handle,VMCI_RESOURCE_TYPE_DOORBELL ); +if (resource ==NULL ) { +pr_devel ("Failed to destroy doorbell " +"(handle=0x%x:0x%x)." , handle.context, + handle.resource); +return VMCI_ERROR_NOT_FOUND ; + } + + entry = container_of(resource, struct dbell_entry, resource); + +if (vmci_guest_code_active ()) { +int result; + +dbell_index_table_remove (entry); + + result =dbell_unlink (handle); +if (VMCI_SUCCESS != result) { + +/* +* The only reason this should fail would be an inconsistency between +* guest and hypervisor state, where the guest believes it has an +* active registration whereas the hypervisor doesn't. One case where +* this may happen is if a doorbell is unregistered following a +* hibernation at a time where the doorbell state hasn't been restored +* on the hypervisor side yet. Since the handle has now been removed +* in the guest, we just print a warning and return success. +*/ + +pr_devel ("Unlink of doorbell (handle=0x%x:0x%x) " +"unknown by hypervisor (error=%d)." , + handle.context, handle.resource, + result); + } + } + +/* +* Now remove the resource from the table. It might still be in use +* after this, in a callback or still on the delayed work queue. +*/ +vmci_resource_remove (handle,VMCI_RESOURCE_TYPE_DOORBELL ); + +/* +* We now wait on the destroyEvent and release the reference we got +* above. +*/ +vmci_drv_wait_on_event_intr (&entry->destroyEvent, + dbell_release_cb, entry); + +/* +* We know that we are now the only reference to the above entry so +* can safely free it. +*/ +kfree (entry); + +return VMCI_SUCCESS ; +} + +EXPORT_SYMBOL (VMCIDoorbell_Destroy); + +/* +*------------------------------------------------------------------------------ +* +* dbell_notify_as_guest -- +* +* Notify another guest or the host. We send a datagram down to the +* host via the hypervisor with the notification info. +* +* Results: +* VMCI_SUCCESS on success, appropriate error code otherwise. +* +* Side effects: +* May do a hypercall. +* +*------------------------------------------------------------------------------ +*/ + +static int dbell_notify_as_guest (struct vmci_handle handle,// IN +uint32_t privFlags)// IN +{ +struct vmci_doorbell_ntfy_msg notifyMsg; + +ASSERT (vmci_guest_code_active ()); + + notifyMsg.hdr.dst =vmci_make_handle (VMCI_HYPERVISOR_CONTEXT_ID , +VMCI_DOORBELL_NOTIFY ); + notifyMsg.hdr.src =VMCI_ANON_SRC_HANDLE ; + notifyMsg.hdr.payloadSize =sizeof notifyMsg -VMCI_DG_HEADERSIZE ; + notifyMsg.handle = handle; + +return vmci_send_dg ((struct vmci_dg *)¬ifyMsg); +} + +/* +*------------------------------------------------------------------------------ +* +* VMCIDoorbell_Notify -- +* +* Generates a notification on the doorbell identified by the +* handle. For host side generation of notifications, the caller +* can specify what the privilege of the calling side is. +* +* Results: +* VMCI_SUCCESS on success, appropriate error code otherwise. +* +* Side effects: +* May do a hypercall. +* +*------------------------------------------------------------------------------ +*/ + +int VMCIDoorbell_Notify (struct vmci_handle dst,// IN +uint32_t privFlags)// IN +{ +int retval; +enum vmci_route route; +struct vmci_handle src; + +if (VMCI_HANDLE_INVALID (dst) + || (privFlags & ~VMCI_PRIVILEGE_ALL_FLAGS )) +return VMCI_ERROR_INVALID_ARGS ; + + src =VMCI_INVALID_HANDLE ; + retval =vmci_route (&src, &dst, false, &route); +if (retval <VMCI_SUCCESS ) +return retval; + +if (VMCI_ROUTE_AS_HOST == route) +return vmci_ctx_notify_dbell (VMCI_HOST_CONTEXT_ID , + dst, privFlags); + +if (VMCI_ROUTE_AS_GUEST == route) +return dbell_notify_as_guest (dst, privFlags); + +pr_warn ("Unknown route (%d) for doorbell." , route); +return VMCI_ERROR_DST_UNREACHABLE ; +} + +EXPORT_SYMBOL (VMCIDoorbell_Notify); + +/* +*------------------------------------------------------------------------------ +* +* dbell_delayed_dispatch_cb -- +* +* Calls the specified callback in a delayed context. +* +* Results: +* None. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +static void dbell_delayed_dispatch_cb (void *data)// IN +{ +struct dbell_entry *entry = (struct dbell_entry *)data; + +ASSERT (data); + + entry->notifyCB (entry->clientData); + +vmci_resource_release (&entry->resource); +} + +/* +*------------------------------------------------------------------------------ +* +* vmci_dbell_host_context_notify -- +* +* Dispatches a doorbell notification to the host context. +* +* Results: +* VMCI_SUCCESS on success. Appropriate error code otherwise. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +int vmci_dbell_host_context_notify (uint32_t srcCID,// IN +struct vmci_handle handle)// IN +{ +struct dbell_entry *entry; +struct vmci_resource *resource; +int result; + +ASSERT (vmci_host_code_active ()); + +if (VMCI_HANDLE_INVALID (handle)) { +pr_devel ("Notifying an invalid doorbell " +"(handle=0x%x:0x%x)." , handle.context, + handle.resource); +return VMCI_ERROR_INVALID_ARGS ; + } + + resource =vmci_resource_get (handle,VMCI_RESOURCE_TYPE_DOORBELL ); +if (resource ==NULL ) { +pr_devel ("Notifying an unknown doorbell " +"(handle=0x%x:0x%x)." , handle.context, + handle.resource); +return VMCI_ERROR_NOT_FOUND ; + } + entry = container_of(resource, struct dbell_entry, resource); + +if (entry->runDelayed) { + result = +vmci_drv_schedule_delayed_work (dbell_delayed_dispatch_cb, + entry); +if (result <VMCI_SUCCESS ) { +/* +* If we failed to schedule the delayed work, we need to +* release the resource immediately. Otherwise, the resource +* will be released once the delayed callback has been +* completed. +*/ + +pr_devel ("Failed to schedule delayed doorbell " +"notification (result=%d)." , result); +vmci_resource_release (resource); + } + }else { + entry->notifyCB (entry->clientData); +vmci_resource_release (resource); + result =VMCI_SUCCESS ; + } +return result; +} + +/* +*------------------------------------------------------------------------------ +* +* vmci_dbell_hibernate -- +* +* When a guest leaves hibernation, the device driver state is out of sync +* with the device state, since the driver state has doorbells registered +* that aren't known to the device. This function takes care of +* reregistering any doorbells. In case an error occurs during +* reregistration (this is highly unlikely since 1) it succeeded the first +* time 2) the device driver is the only source of doorbell registrations), +* we simply log the error. The doorbell can still be destroyed using +* VMCIDoorbell_Destroy. +* +* Results: +* None. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +void vmci_dbell_hibernate (bool enterHibernate) +{ +uint32_t bucket; +struct dbell_entry *cur; + +if (!vmci_guest_code_active () || enterHibernate) +return ; + +spin_lock_bh (&vmciDoorbellIT.lock); + +for (bucket =0 ; bucket <ARRAY_SIZE (vmciDoorbellIT.entries); bucket++) { +list_for_each_entry (cur, &vmciDoorbellIT.entries[bucket], + idxListItem) { +int result; +struct vmci_handle h; + + h =vmci_resource_handle (&cur->resource); + result =dbell_link (h, cur->idx); +if (result !=VMCI_SUCCESS + && result !=VMCI_ERROR_DUPLICATE_ENTRY ) { +pr_warn ("Failed to reregister doorbell " +"(handle=0x%x:0x%x) to index (error=%d)." , + h.context, h.resource, result); + } + } + } + +spin_unlock_bh (&vmciDoorbellIT.lock); +} + +/* +*------------------------------------------------------------------------------ +* +* vmci_dbell_register_notification_bitmap -- +* +* Register the notification bitmap with the host. +* +* Results: +* true if the bitmap is registered successfully with the device, false +* otherwise. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +bool vmci_dbell_register_notification_bitmap (uint32_t bitmapPPN) +{ +int result; +struct vmci_ntfy_bm_set_msg bitmapSetMsg; + +/* +* Do not ASSERT() on the guest device here. This function can get called +* during device initialization, so the ASSERT() will fail even though +* the device is (almost) up. +*/ + + bitmapSetMsg.hdr.dst =vmci_make_handle (VMCI_HYPERVISOR_CONTEXT_ID , +VMCI_SET_NOTIFY_BITMAP ); + bitmapSetMsg.hdr.src =VMCI_ANON_SRC_HANDLE ; + bitmapSetMsg.hdr.payloadSize =sizeof bitmapSetMsg -VMCI_DG_HEADERSIZE ; + bitmapSetMsg.bitmapPPN = bitmapPPN; + + result =vmci_send_dg ((struct vmci_dg *)&bitmapSetMsg); +if (result !=VMCI_SUCCESS ) { +pr_devel ("Failed to register (PPN=%u) as " +"notification bitmap (error=%d)." , + bitmapPPN, result); +return false; + } +return true; +} + +/* +*------------------------------------------------------------------------- +* +* dbell_fire_entries -- +* +* Executes or schedules the handlers for a given notify index. +* +* Result: +* Notification hash entry if found. NULL otherwise. +* +* Side effects: +* Whatever the side effects of the handlers are. +* +*------------------------------------------------------------------------- +*/ + +static void dbell_fire_entries (uint32_t notifyIdx)// IN +{ +uint32_t bucket =VMCI_DOORBELL_HASH (notifyIdx); +struct dbell_entry *cur; + +ASSERT (vmci_guest_code_active ()); + +spin_lock_bh (&vmciDoorbellIT.lock); + +list_for_each_entry (cur, &vmciDoorbellIT.entries[bucket], idxListItem) { +if (cur->idx == notifyIdx &&atomic_read (&cur->active) ==1 ) { +ASSERT (cur->notifyCB); +if (cur->runDelayed) { +int err; + +vmci_resource_hold (&cur->resource); + err = +vmci_drv_schedule_delayed_work + (dbell_delayed_dispatch_cb, cur); +if (err !=VMCI_SUCCESS ) { +vmci_resource_release (&cur->resource); + goto out; + } + }else { + cur->notifyCB (cur->clientData); + } + } + } + +out: +spin_unlock_bh (&vmciDoorbellIT.lock); +} + +/* +*------------------------------------------------------------------------------ +* +* vmci_dbell_scan_notification_entries -- +* +* Scans the notification bitmap, collects pending notifications, +* resets the bitmap and invokes appropriate callbacks. +* +* Results: +* None. +* +* Side effects: +* May schedule tasks, allocate memory and run callbacks. +* +*------------------------------------------------------------------------------ +*/ + +void vmci_dbell_scan_notification_entries (uint8_t * bitmap) +{ +uint32_t idx; + +ASSERT (bitmap); +ASSERT (vmci_guest_code_active ()); + +for (idx =0 ; idx < maxNotifyIdx; idx++) { +if (bitmap[idx] &0x1 ) { + bitmap[idx] &= ~1 ; +dbell_fire_entries (idx); + } + } +}
drivers/misc/vmw_vmci/vmci_doorbell.h new file +57 -0
57 lines not on this page · raw message
Signature · 1 line
1.7.0.4
RFC 3/11 Apply VMCI doorbell code
In reply to Andrew Stiegmann (stieg)Doorbell code allows for notifcations between host and guest.
--- drivers/misc/vmw_vmci/vmci_doorbell.c | 989 +++++++++++++++++++++++++++++++++ drivers/misc/vmw_vmci/vmci_doorbell.h | 57 ++ 2 files changed, 1046 insertions(+), 0 deletions(-) create mode 100644 drivers/misc/vmw_vmci/vmci_doorbell.c create mode 100644 drivers/misc/vmw_vmci/vmci_doorbell.h
drivers/misc/vmw_vmci/vmci_doorbell.c new file +989 -0
@@ -0,0 +1,989 @@ +/* +* VMware VMCI Driver +* +* Copyright (C) 2012 VMware, Inc. All rights reserved. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms of the GNU General Public License as published by the +* Free Software Foundation version 2 and no later version. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +* for more details. +* +* You should have received a copy of the GNU General Public License along +* with this program; if not, write to the Free Software Foundation, Inc., +* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/sched.h> +#include <linux/vmw_vmci_api.h> +#include <linux/vmw_vmci_defs.h> + +#include "vmci_common_int.h" +#include "vmci_datagram.h" +#include "vmci_doorbell.h" +#include "vmci_driver.h" +#include "vmci_resource.h" +#include "vmci_route.h" + +#define VMCI_DOORBELL_INDEX_TABLE_SIZE (1 << 6) +#define VMCI_DOORBELL_HASH (_idx) \ + vmci_hash_calc((_idx), VMCI_DOORBELL_INDEX_TABLE_SIZE) + +/* +* DoorbellEntry describes the a doorbell notification handle allocated by the +* host. +*/ +struct dbell_entry { +struct vmci_resource resource; +uint32_t idx; +struct list_head idxListItem; +uint32_t privFlags; +bool runDelayed; +VMCICallback notifyCB; +void *clientData; +wait_queue_head_t destroyEvent; +atomic_t active;/* Only used by guest personality */ +}; + +/* The VMCI index table keeps track of currently registered doorbells. */ +static struct dbell_index_table { +spinlock_t lock; +struct list_head entries[VMCI_DOORBELL_INDEX_TABLE_SIZE ]; +} vmciDoorbellIT; + +/* +* The maxNotifyIdx is one larger than the currently known bitmap index in +* use, and is used to determine how much of the bitmap needs to be scanned. +*/ +static uint32_t maxNotifyIdx; + +/* +* The notifyIdxCount is used for determining whether there are free entries +* within the bitmap (if notifyIdxCount + 1 < maxNotifyIdx). +*/ +static uint32_t notifyIdxCount; + +/* +* The lastNotifyIdxReserved is used to track the last index handed out - in +* the case where multiple handles share a notification index, we hand out +* indexes round robin based on lastNotifyIdxReserved. +*/ +static uint32_t lastNotifyIdxReserved; + +/* This is a one entry cache used to by the index allocation. */ +static uint32_t lastNotifyIdxReleased =PAGE_SIZE ; + +/* +*------------------------------------------------------------------------------ +* +* vmci_dbell_init -- +* +* General init code. +* +* Result: +* VMCI_SUCCESS on success, lock allocation error otherwise. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +int __init vmci_dbell_init (void ) +{ +uint32_t bucket; + +for (bucket =0 ; bucket <ARRAY_SIZE (vmciDoorbellIT.entries); ++bucket) +INIT_LIST_HEAD (&vmciDoorbellIT.entries[bucket]); + +spin_lock_init (&vmciDoorbellIT.lock); +return VMCI_SUCCESS ; +} + +/* +*------------------------------------------------------------------------------ +* +* dbell_free_cb -- +* +* Callback to free doorbell entry structure when resource is no longer used, +* ie. the reference count reached 0. The entry is freed in +* VMCIDoorbell_Destroy(), which is waiting on the signal that gets fired +* here. +* +* Result: +* None. +* +* Side effects: +* Signals VMCI event. +* +*------------------------------------------------------------------------------ +*/ + +static void dbell_free_cb (void *clientData)// IN +{ +struct dbell_entry *entry = (struct dbell_entry *)clientData; +ASSERT (entry); +wake_up (&entry->destroyEvent); +} + +/* +*------------------------------------------------------------------------------ +* +* dbell_release_cb -- +* +* Callback to release the resource reference. It is called by the +* VMCI_WaitOnEvent function before it blocks. +* +* Result: +* Always 0. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +static int dbell_release_cb (void *clientData)// IN: doorbell entry +{ +struct dbell_entry *entry = (struct dbell_entry *)clientData; +ASSERT (entry); +vmci_resource_release (&entry->resource); +return 0 ; +} + +/* +*------------------------------------------------------------------------------ +* +* vmci_dbell_get_priv_flags -- +* +* Utility function that retrieves the privilege flags associated +* with a given doorbell handle. For guest endpoints, the +* privileges are determined by the context ID, but for host +* endpoints privileges are associated with the complete +* handle. Hypervisor endpoints are not yet supported. +* +* Result: +* VMCI_SUCCESS on success, +* VMCI_ERROR_NOT_FOUND if handle isn't found, +* VMCI_ERROR_INVALID_ARGS if handle is invalid. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +int vmci_dbell_get_priv_flags (struct vmci_handle handle,// IN +uint32_t * privFlags)// OUT +{ +if (privFlags ==NULL || handle.context ==VMCI_INVALID_ID ) +return VMCI_ERROR_INVALID_ARGS ; + +if (handle.context ==VMCI_HOST_CONTEXT_ID ) { +struct dbell_entry *entry; +struct vmci_resource *resource; + + resource = +vmci_resource_get (handle,VMCI_RESOURCE_TYPE_DOORBELL ); +if (resource ==NULL ) +return VMCI_ERROR_NOT_FOUND ; + + entry = + container_of(resource, struct dbell_entry, + resource); + *privFlags = entry->privFlags; +vmci_resource_release (resource); + }else if (handle.context ==VMCI_HYPERVISOR_CONTEXT_ID ) { +/* Hypervisor endpoints for notifications are not supported (yet). */ +return VMCI_ERROR_INVALID_ARGS ; + }else { + *privFlags =VMCIContext_GetPrivFlags (handle.context); + } + +return VMCI_SUCCESS ; +} + +/* +*----------------------------------------------------------------------------- +* +* dbell_index_table_find -- +* +* Find doorbell entry by bitmap index. +* +* Results: +* Entry if found, NULL if not. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +static struct dbell_entry *dbell_index_table_find (uint32_t idx)// IN +{ +uint32_t bucket =VMCI_DOORBELL_HASH (idx); +struct dbell_entry *cur; + +ASSERT (vmci_guest_code_active ()); + +list_for_each_entry (cur, &vmciDoorbellIT.entries[bucket], idxListItem) { +if (idx == cur->idx) +return cur; + } + +return NULL ; +} + +/* +*------------------------------------------------------------------------------ +* +* dbell_index_table_add -- +* +* Add the given entry to the index table. This will hold() the entry's +* resource so that the entry is not deleted before it is removed from the +* table. +* +* Results: +* None. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +static void dbell_index_table_add (struct dbell_entry *entry)// IN/OUT +{ +uint32_t bucket; +uint32_t newNotifyIdx; + +ASSERT (entry); +ASSERT (vmci_guest_code_active ()); + +vmci_resource_hold (&entry->resource); + +spin_lock_bh (&vmciDoorbellIT.lock); + +/* +* Below we try to allocate an index in the notification bitmap with "not +* too much" sharing between resources. If we use less that the full bitmap, +* we either add to the end if there are no unused flags within the +* currently used area, or we search for unused ones. If we use the full +* bitmap, we allocate the index round robin. +*/ + +if (maxNotifyIdx <PAGE_SIZE || notifyIdxCount <PAGE_SIZE ) { +if (lastNotifyIdxReleased < maxNotifyIdx && + !dbell_index_table_find (lastNotifyIdxReleased)) { + newNotifyIdx = lastNotifyIdxReleased; + lastNotifyIdxReleased =PAGE_SIZE ; + }else { +bool reused = false; + newNotifyIdx = lastNotifyIdxReserved; +if (notifyIdxCount +1 < maxNotifyIdx) { +do { +if (!dbell_index_table_find + (newNotifyIdx)) { + reused = true; +break ; + } + newNotifyIdx = + (newNotifyIdx +1 ) % maxNotifyIdx; + }while (newNotifyIdx != lastNotifyIdxReleased); + } +if (!reused) { + newNotifyIdx = maxNotifyIdx; + maxNotifyIdx++; + } + } + }else { + newNotifyIdx = (lastNotifyIdxReserved +1 ) %PAGE_SIZE ; + } + + lastNotifyIdxReserved = newNotifyIdx; + notifyIdxCount++; + + entry->idx = newNotifyIdx; + bucket =VMCI_DOORBELL_HASH (entry->idx); +list_add (&entry->idxListItem, &vmciDoorbellIT.entries[bucket]); + +spin_unlock_bh (&vmciDoorbellIT.lock); +} + +/* +*------------------------------------------------------------------------------ +* +* dbell_index_table_remove -- +* +* Remove the given entry from the index table. This will release() the +* entry's resource. +* +* Results: +* None. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +static void dbell_index_table_remove (struct dbell_entry *entry)// IN/OUT +{ +ASSERT (entry); +ASSERT (vmci_guest_code_active ()); + +spin_lock_bh (&vmciDoorbellIT.lock); + +list_del (&entry->idxListItem); + + notifyIdxCount--; +if (entry->idx == maxNotifyIdx -1 ) { +/* +* If we delete an entry with the maximum known notification index, we +* take the opportunity to prune the current max. As there might be other +* unused indices immediately below, we lower the maximum until we hit an +* index in use. +*/ + +while (maxNotifyIdx >0 && + !dbell_index_table_find (maxNotifyIdx -1 )) { + maxNotifyIdx--; + } + } + + lastNotifyIdxReleased = entry->idx; + +spin_unlock_bh (&vmciDoorbellIT.lock); + +vmci_resource_release (&entry->resource); +} + +/* +*------------------------------------------------------------------------------ +* +* dbell_link -- +* +* Creates a link between the given doorbell handle and the given +* index in the bitmap in the device backend. +* +* Results: +* VMCI_SUCCESS if success, appropriate error code otherwise. +* +* Side effects: +* Notification state is created in hypervisor. +* +*------------------------------------------------------------------------------ +*/ + +static int dbell_link (struct vmci_handle handle,// IN +uint32_t notifyIdx)// IN +{ +struct vmci_doorbell_link_msg linkMsg; + +ASSERT (!VMCI_HANDLE_INVALID (handle)); +ASSERT (vmci_guest_code_active ()); + + linkMsg.hdr.dst = +vmci_make_handle (VMCI_HYPERVISOR_CONTEXT_ID ,VMCI_DOORBELL_LINK ); + linkMsg.hdr.src =VMCI_ANON_SRC_HANDLE ; + linkMsg.hdr.payloadSize =sizeof linkMsg -VMCI_DG_HEADERSIZE ; + linkMsg.handle = handle; + linkMsg.notifyIdx = notifyIdx; + +return vmci_send_dg ((struct vmci_dg *)&linkMsg); +} + +/* +*------------------------------------------------------------------------------ +* +* dbell_unlink -- +* +* Unlinks the given doorbell handle from an index in the bitmap in +* the device backend. +* +* Results: +* VMCI_SUCCESS if success, appropriate error code otherwise. +* +* Side effects: +* Notification state is destroyed in hypervisor. +* +*------------------------------------------------------------------------------ +*/ + +static int dbell_unlink (struct vmci_handle handle) +{ +struct vmci_doorbell_unlink_msg unlinkMsg; + +ASSERT (!VMCI_HANDLE_INVALID (handle)); +ASSERT (vmci_guest_code_active ()); + + unlinkMsg.hdr.dst = +vmci_make_handle (VMCI_HYPERVISOR_CONTEXT_ID ,VMCI_DOORBELL_UNLINK ); + unlinkMsg.hdr.src =VMCI_ANON_SRC_HANDLE ; + unlinkMsg.hdr.payloadSize =sizeof unlinkMsg -VMCI_DG_HEADERSIZE ; + unlinkMsg.handle = handle; + +return vmci_send_dg ((struct vmci_dg *)&unlinkMsg); +} + +/* +*------------------------------------------------------------------------------ +* +* VMCIDoorbell_Create -- +* +* Creates a doorbell with the given callback. If the handle is +* VMCI_INVALID_HANDLE, a free handle will be assigned, if +* possible. The callback can be run immediately (potentially with +* locks held - the default) or delayed (in a kernel thread) by +* specifying the flag VMCI_FLAG_DELAYED_CB. If delayed execution +* is selected, a given callback may not be run if the kernel is +* unable to allocate memory for the delayed execution (highly +* unlikely). +* +* Results: +* VMCI_SUCCESS on success, appropriate error code otherwise. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +int VMCIDoorbell_Create (struct vmci_handle *handle,// IN/OUT +uint32_t flags,// IN +uint32_t privFlags,// IN +VMCICallback notifyCB,// IN +void *clientData)// IN +{ +struct dbell_entry *entry; +struct vmci_handle newHandle; +int result; + +if (!handle || !notifyCB || flags & ~VMCI_FLAG_DELAYED_CB || + privFlags & ~VMCI_PRIVILEGE_ALL_FLAGS ) +return VMCI_ERROR_INVALID_ARGS ; + + entry =kmalloc (sizeof *entry,GFP_KERNEL ); +if (entry ==NULL ) { +pr_warn ("Failed allocating memory for datagram entry." ); +return VMCI_ERROR_NO_MEM ; + } + +if (VMCI_HANDLE_INVALID (*handle)) { +uint32_t contextID =VMCI_GetContextID (); +uint32_t resourceID =vmci_resource_get_id (contextID); +if (resourceID ==VMCI_INVALID_ID ) { + result =VMCI_ERROR_NO_HANDLE ; + goto freeMem; + } + newHandle =vmci_make_handle (contextID, resourceID); + }else { +bool validContext = false; + +/* +* Validate the handle. We must do both of the checks below +* because we can be acting as both a host and a guest at the +* same time. We always allow the host context ID, since the +* host functionality is in practice always there with the +* unified driver. +*/ + +if (VMCI_HOST_CONTEXT_ID == handle->context || + (vmci_guest_code_active () && +VMCI_GetContextID () == handle->context)) + validContext = true; + +if (!validContext ||VMCI_INVALID_ID == handle->resource) { +pr_devel ("Invalid argument (handle=0x%x:0x%x)." , + handle->context, handle->resource); + result =VMCI_ERROR_INVALID_ARGS ; + goto freeMem; + } + + newHandle = *handle; + } + + entry->idx =0 ; +INIT_LIST_HEAD (&entry->idxListItem); + entry->privFlags = privFlags; + entry->runDelayed = (flags &VMCI_FLAG_DELAYED_CB ) ? true : false; + entry->notifyCB = notifyCB; + entry->clientData = clientData; +atomic_set (&entry->active,0 ); +init_waitqueue_head (&entry->destroyEvent); + + result = +vmci_resource_add (&entry->resource,VMCI_RESOURCE_TYPE_DOORBELL , + newHandle, dbell_free_cb, entry); +if (result !=VMCI_SUCCESS ) { +pr_warn ("Failed to add new resource (handle=0x%x:0x%x)." , + newHandle.context, newHandle.resource); +if (result ==VMCI_ERROR_DUPLICATE_ENTRY ) + result =VMCI_ERROR_ALREADY_EXISTS ; + + goto freeMem; + } + +if (vmci_guest_code_active ()) { +dbell_index_table_add (entry); + result =dbell_link (newHandle, entry->idx); +if (VMCI_SUCCESS != result) + goto destroyResource; + +atomic_set (&entry->active,1 ); + } + +if (VMCI_HANDLE_INVALID (*handle)) + *handle = newHandle; + +return result; + +destroyResource: +dbell_index_table_remove (entry); +vmci_resource_remove (newHandle,VMCI_RESOURCE_TYPE_DOORBELL ); +freeMem: +kfree (entry); +return result; +} + +EXPORT_SYMBOL (VMCIDoorbell_Create); + +/* +*------------------------------------------------------------------------------ +* +* VMCIDoorbell_Destroy -- +* +* Destroys a doorbell previously created with +* VMCIDoorbell_Create. This operation may block waiting for a +* callback to finish. +* +* Results: +* VMCI_SUCCESS on success, appropriate error code otherwise. +* +* Side effects: +* May block. +* +*------------------------------------------------------------------------------ +*/ + +int VMCIDoorbell_Destroy (struct vmci_handle handle)// IN +{ +struct dbell_entry *entry; +struct vmci_resource *resource; + +if (VMCI_HANDLE_INVALID (handle)) +return VMCI_ERROR_INVALID_ARGS ; + + resource =vmci_resource_get (handle,VMCI_RESOURCE_TYPE_DOORBELL ); +if (resource ==NULL ) { +pr_devel ("Failed to destroy doorbell " +"(handle=0x%x:0x%x)." , handle.context, + handle.resource); +return VMCI_ERROR_NOT_FOUND ; + } + + entry = container_of(resource, struct dbell_entry, resource); + +if (vmci_guest_code_active ()) { +int result; + +dbell_index_table_remove (entry); + + result =dbell_unlink (handle); +if (VMCI_SUCCESS != result) { + +/* +* The only reason this should fail would be an inconsistency between +* guest and hypervisor state, where the guest believes it has an +* active registration whereas the hypervisor doesn't. One case where +* this may happen is if a doorbell is unregistered following a +* hibernation at a time where the doorbell state hasn't been restored +* on the hypervisor side yet. Since the handle has now been removed +* in the guest, we just print a warning and return success. +*/ + +pr_devel ("Unlink of doorbell (handle=0x%x:0x%x) " +"unknown by hypervisor (error=%d)." , + handle.context, handle.resource, + result); + } + } + +/* +* Now remove the resource from the table. It might still be in use +* after this, in a callback or still on the delayed work queue. +*/ +vmci_resource_remove (handle,VMCI_RESOURCE_TYPE_DOORBELL ); + +/* +* We now wait on the destroyEvent and release the reference we got +* above. +*/ +vmci_drv_wait_on_event_intr (&entry->destroyEvent, + dbell_release_cb, entry); + +/* +* We know that we are now the only reference to the above entry so +* can safely free it. +*/ +kfree (entry); + +return VMCI_SUCCESS ; +} + +EXPORT_SYMBOL (VMCIDoorbell_Destroy); + +/* +*------------------------------------------------------------------------------ +* +* dbell_notify_as_guest -- +* +* Notify another guest or the host. We send a datagram down to the +* host via the hypervisor with the notification info. +* +* Results: +* VMCI_SUCCESS on success, appropriate error code otherwise. +* +* Side effects: +* May do a hypercall. +* +*------------------------------------------------------------------------------ +*/ + +static int dbell_notify_as_guest (struct vmci_handle handle,// IN +uint32_t privFlags)// IN +{ +struct vmci_doorbell_ntfy_msg notifyMsg; + +ASSERT (vmci_guest_code_active ()); + + notifyMsg.hdr.dst =vmci_make_handle (VMCI_HYPERVISOR_CONTEXT_ID , +VMCI_DOORBELL_NOTIFY ); + notifyMsg.hdr.src =VMCI_ANON_SRC_HANDLE ; + notifyMsg.hdr.payloadSize =sizeof notifyMsg -VMCI_DG_HEADERSIZE ; + notifyMsg.handle = handle; + +return vmci_send_dg ((struct vmci_dg *)¬ifyMsg); +} + +/* +*------------------------------------------------------------------------------ +* +* VMCIDoorbell_Notify -- +* +* Generates a notification on the doorbell identified by the +* handle. For host side generation of notifications, the caller +* can specify what the privilege of the calling side is. +* +* Results: +* VMCI_SUCCESS on success, appropriate error code otherwise. +* +* Side effects: +* May do a hypercall. +* +*------------------------------------------------------------------------------ +*/ + +int VMCIDoorbell_Notify (struct vmci_handle dst,// IN +uint32_t privFlags)// IN +{ +int retval; +enum vmci_route route; +struct vmci_handle src; + +if (VMCI_HANDLE_INVALID (dst) + || (privFlags & ~VMCI_PRIVILEGE_ALL_FLAGS )) +return VMCI_ERROR_INVALID_ARGS ; + + src =VMCI_INVALID_HANDLE ; + retval =vmci_route (&src, &dst, false, &route); +if (retval <VMCI_SUCCESS ) +return retval; + +if (VMCI_ROUTE_AS_HOST == route) +return vmci_ctx_notify_dbell (VMCI_HOST_CONTEXT_ID , + dst, privFlags); + +if (VMCI_ROUTE_AS_GUEST == route) +return dbell_notify_as_guest (dst, privFlags); + +pr_warn ("Unknown route (%d) for doorbell." , route); +return VMCI_ERROR_DST_UNREACHABLE ; +} + +EXPORT_SYMBOL (VMCIDoorbell_Notify); + +/* +*------------------------------------------------------------------------------ +* +* dbell_delayed_dispatch_cb -- +* +* Calls the specified callback in a delayed context. +* +* Results: +* None. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +static void dbell_delayed_dispatch_cb (void *data)// IN +{ +struct dbell_entry *entry = (struct dbell_entry *)data; + +ASSERT (data); + + entry->notifyCB (entry->clientData); + +vmci_resource_release (&entry->resource); +} + +/* +*------------------------------------------------------------------------------ +* +* vmci_dbell_host_context_notify -- +* +* Dispatches a doorbell notification to the host context. +* +* Results: +* VMCI_SUCCESS on success. Appropriate error code otherwise. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +int vmci_dbell_host_context_notify (uint32_t srcCID,// IN +struct vmci_handle handle)// IN +{ +struct dbell_entry *entry; +struct vmci_resource *resource; +int result; + +ASSERT (vmci_host_code_active ()); + +if (VMCI_HANDLE_INVALID (handle)) { +pr_devel ("Notifying an invalid doorbell " +"(handle=0x%x:0x%x)." , handle.context, + handle.resource); +return VMCI_ERROR_INVALID_ARGS ; + } + + resource =vmci_resource_get (handle,VMCI_RESOURCE_TYPE_DOORBELL ); +if (resource ==NULL ) { +pr_devel ("Notifying an unknown doorbell " +"(handle=0x%x:0x%x)." , handle.context, + handle.resource); +return VMCI_ERROR_NOT_FOUND ; + } + entry = container_of(resource, struct dbell_entry, resource); + +if (entry->runDelayed) { + result = +vmci_drv_schedule_delayed_work (dbell_delayed_dispatch_cb, + entry); +if (result <VMCI_SUCCESS ) { +/* +* If we failed to schedule the delayed work, we need to +* release the resource immediately. Otherwise, the resource +* will be released once the delayed callback has been +* completed. +*/ + +pr_devel ("Failed to schedule delayed doorbell " +"notification (result=%d)." , result); +vmci_resource_release (resource); + } + }else { + entry->notifyCB (entry->clientData); +vmci_resource_release (resource); + result =VMCI_SUCCESS ; + } +return result; +} + +/* +*------------------------------------------------------------------------------ +* +* vmci_dbell_hibernate -- +* +* When a guest leaves hibernation, the device driver state is out of sync +* with the device state, since the driver state has doorbells registered +* that aren't known to the device. This function takes care of +* reregistering any doorbells. In case an error occurs during +* reregistration (this is highly unlikely since 1) it succeeded the first +* time 2) the device driver is the only source of doorbell registrations), +* we simply log the error. The doorbell can still be destroyed using +* VMCIDoorbell_Destroy. +* +* Results: +* None. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +void vmci_dbell_hibernate (bool enterHibernate) +{ +uint32_t bucket; +struct dbell_entry *cur; + +if (!vmci_guest_code_active () || enterHibernate) +return ; + +spin_lock_bh (&vmciDoorbellIT.lock); + +for (bucket =0 ; bucket <ARRAY_SIZE (vmciDoorbellIT.entries); bucket++) { +list_for_each_entry (cur, &vmciDoorbellIT.entries[bucket], + idxListItem) { +int result; +struct vmci_handle h; + + h =vmci_resource_handle (&cur->resource); + result =dbell_link (h, cur->idx); +if (result !=VMCI_SUCCESS + && result !=VMCI_ERROR_DUPLICATE_ENTRY ) { +pr_warn ("Failed to reregister doorbell " +"(handle=0x%x:0x%x) to index (error=%d)." , + h.context, h.resource, result); + } + } + } + +spin_unlock_bh (&vmciDoorbellIT.lock); +} + +/* +*------------------------------------------------------------------------------ +* +* vmci_dbell_register_notification_bitmap -- +* +* Register the notification bitmap with the host. +* +* Results: +* true if the bitmap is registered successfully with the device, false +* otherwise. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +bool vmci_dbell_register_notification_bitmap (uint32_t bitmapPPN) +{ +int result; +struct vmci_ntfy_bm_set_msg bitmapSetMsg; + +/* +* Do not ASSERT() on the guest device here. This function can get called +* during device initialization, so the ASSERT() will fail even though +* the device is (almost) up. +*/ + + bitmapSetMsg.hdr.dst =vmci_make_handle (VMCI_HYPERVISOR_CONTEXT_ID , +VMCI_SET_NOTIFY_BITMAP ); + bitmapSetMsg.hdr.src =VMCI_ANON_SRC_HANDLE ; + bitmapSetMsg.hdr.payloadSize =sizeof bitmapSetMsg -VMCI_DG_HEADERSIZE ; + bitmapSetMsg.bitmapPPN = bitmapPPN; + + result =vmci_send_dg ((struct vmci_dg *)&bitmapSetMsg); +if (result !=VMCI_SUCCESS ) { +pr_devel ("Failed to register (PPN=%u) as " +"notification bitmap (error=%d)." , + bitmapPPN, result); +return false; + } +return true; +} + +/* +*------------------------------------------------------------------------- +* +* dbell_fire_entries -- +* +* Executes or schedules the handlers for a given notify index. +* +* Result: +* Notification hash entry if found. NULL otherwise. +* +* Side effects: +* Whatever the side effects of the handlers are. +* +*------------------------------------------------------------------------- +*/ + +static void dbell_fire_entries (uint32_t notifyIdx)// IN +{ +uint32_t bucket =VMCI_DOORBELL_HASH (notifyIdx); +struct dbell_entry *cur; + +ASSERT (vmci_guest_code_active ()); + +spin_lock_bh (&vmciDoorbellIT.lock); + +list_for_each_entry (cur, &vmciDoorbellIT.entries[bucket], idxListItem) { +if (cur->idx == notifyIdx &&atomic_read (&cur->active) ==1 ) { +ASSERT (cur->notifyCB); +if (cur->runDelayed) { +int err; + +vmci_resource_hold (&cur->resource); + err = +vmci_drv_schedule_delayed_work + (dbell_delayed_dispatch_cb, cur); +if (err !=VMCI_SUCCESS ) { +vmci_resource_release (&cur->resource); + goto out; + } + }else { + cur->notifyCB (cur->clientData); + } + } + } + +out: +spin_unlock_bh (&vmciDoorbellIT.lock); +} + +/* +*------------------------------------------------------------------------------ +* +* vmci_dbell_scan_notification_entries -- +* +* Scans the notification bitmap, collects pending notifications, +* resets the bitmap and invokes appropriate callbacks. +* +* Results: +* None. +* +* Side effects: +* May schedule tasks, allocate memory and run callbacks. +* +*------------------------------------------------------------------------------ +*/ + +void vmci_dbell_scan_notification_entries (uint8_t * bitmap) +{ +uint32_t idx; + +ASSERT (bitmap); +ASSERT (vmci_guest_code_active ()); + +for (idx =0 ; idx < maxNotifyIdx; idx++) { +if (bitmap[idx] &0x1 ) { + bitmap[idx] &= ~1 ; +dbell_fire_entries (idx); + } + } +}
drivers/misc/vmw_vmci/vmci_doorbell.h new file +57 -0
57 lines not on this page · raw message
Signature · 1 line
1.7.0.4
RFC 4/11 Apply VMCI driver code
In reply to Andrew Stiegmann (stieg)This code implementes both the host and guest personalities of the VMCI driver.
--- drivers/misc/vmw_vmci/vmci_driver.c | 2875 +++++++++++++++++++++++++++++++++++ drivers/misc/vmw_vmci/vmci_driver.h | 52 + 2 files changed, 2927 insertions(+), 0 deletions(-) create mode 100644 drivers/misc/vmw_vmci/vmci_driver.c create mode 100644 drivers/misc/vmw_vmci/vmci_driver.h
drivers/misc/vmw_vmci/vmci_driver.c new file +2875 -0
@@ -0,0 +1,2875 @@ +/* +* VMware VMCI Driver +* +* Copyright (C) 2012 VMware, Inc. All rights reserved. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms of the GNU General Public License as published by the +* Free Software Foundation version 2 and no later version. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +* for more details. +* +* You should have received a copy of the GNU General Public License along +* with this program; if not, write to the Free Software Foundation, Inc., +* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include <asm/atomic.h> +#include <asm/io.h> +#include <linux/file.h> +#include <linux/fs.h> +#include <linux/highmem.h> +#include <linux/init.h> +#include <linux/interrupt.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/miscdevice.h> +#include <linux/moduleparam.h> +#include <linux/mutex.h> +#include <linux/pci.h> +#include <linux/poll.h> +#include <linux/sched.h> +#include <linux/smp.h> +#include <linux/version.h> +#include <linux/vmw_vmci_api.h> +#include <linux/vmw_vmci_defs.h> + +#include "vmci_handle_array.h" +#include "vmci_common_int.h" +#include "vmci_context.h" +#include "vmci_datagram.h" +#include "vmci_doorbell.h" +#include "vmci_driver.h" +#include "vmci_event.h" +#include "vmci_hash_table.h" +#include "vmci_queue_pair.h" +#include "vmci_resource.h" + +#define VMCI_UTIL_NUM_RESOURCES 1 + +enum { +VMCI_NOTIFY_RESOURCE_QUEUE_PAIR =0 , +VMCI_NOTIFY_RESOURCE_DOOR_BELL =1 , +}; + +enum { +VMCI_NOTIFY_RESOURCE_ACTION_NOTIFY =0 , +VMCI_NOTIFY_RESOURCE_ACTION_CREATE =1 , +VMCI_NOTIFY_RESOURCE_ACTION_DESTROY =2 , +}; + +static uint32_t ctxUpdateSubID =VMCI_INVALID_ID ; +static struct vmci_ctx *hostContext; +static atomic_t vmContextID = {VMCI_INVALID_ID }; + +struct vmci_delayed_work_info { +struct work_struct work; +VMCIWorkFn *workFn; +void *data; +}; + +/* +*---------------------------------------------------------------------- +* +* PCI Device interface -- +* +* Declarations of types and functions related to the VMCI PCI +* device personality. +* +* +*---------------------------------------------------------------------- +*/ + +/* +* VMCI driver initialization. This block can also be used to +* pass initial group membership etc. +*/ +struct vmci_init_blk { +uint32_t cid; +uint32_t flags; +}; + +/* VMCIQueuePairAllocInfo_VMToVM */ +struct vmci_qp_alloc_info_vmvm { +struct vmci_handle handle; +uint32_t peer; +uint32_t flags; +uint64_t produceSize; +uint64_t consumeSize; +uint64_t producePageFile;/* User VA. */ +uint64_t consumePageFile;/* User VA. */ +uint64_t producePageFileSize;/* Size of the file name array. */ +uint64_t consumePageFileSize;/* Size of the file name array. */ +int32_t result; +uint32_t _pad; +}; + +/* VMCISetNotifyInfo: Used to pass notify flag's address to the host driver. */ +struct vmci_set_notify_info { +uint64_t notifyUVA; +int32_t result; +uint32_t _pad; +}; + +struct vmci_device { +struct mutex lock; + +unsigned int ioaddr; +unsigned int ioaddr_size; +unsigned int irq; +unsigned int intr_type; +bool exclusive_vectors; +struct msix_entry msix_entries[VMCI_MAX_INTRS ]; + +bool enabled; +spinlock_t dev_spinlock; +atomic_t datagrams_allowed; +}; + +static const struct pci_device_id vmci_ids[] = { + {PCI_DEVICE (PCI_VENDOR_ID_VMWARE ,PCI_DEVICE_ID_VMWARE_VMCI ),}, + {0 }, +}; + + +static struct vmci_device vmci_dev; +static bool vmci_disable_host = false; +static bool vmci_disable_guest = false; +static bool vmci_disable_msi = false; +static bool vmci_disable_msix = false; + +/* +* Allocate a buffer for incoming datagrams globally to avoid repeated +* allocation in the interrupt handler's atomic context. +*/ + +static uint8_t *data_buffer =NULL ; +static uint32_t data_buffer_size =VMCI_MAX_DG_SIZE ; + +/* +* If the VMCI hardware supports the notification bitmap, we allocate +* and register a page with the device. +*/ +static uint8_t *notification_bitmap =NULL ; + +/* +*---------------------------------------------------------------------- +* +* Host device node interface -- +* +* Implements VMCI by implementing open/close/ioctl functions +* +* +*---------------------------------------------------------------------- +*/ + +/* +* Per-instance host state +*/ +struct vmci_linux { +struct vmci_ctx *context; +int userVersion; +enum vmci_obj_type ctType; +struct mutex lock; +}; + +/* +* Static driver state. +*/ +struct vmci_linux_state { +struct miscdevice misc; +char buf[1024 ]; +atomic_t activeContexts; +}; + +/* +*---------------------------------------------------------------------- +* +* Shared VMCI device definitions -- +* +* Types and variables shared by both host and guest personality +* +* +*---------------------------------------------------------------------- +*/ + +static bool guestDeviceInit; +static atomic_t guestDeviceActive; +static bool hostDeviceInit; + + + +/* +*---------------------------------------------------------------------------- +* +* drv_delayed_work_cb +* +* Called in a worker thread context. +* +* Results: +* None. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------------- +*/ + +static void drv_delayed_work_cb (struct work_struct *work)// IN +{ +struct vmci_delayed_work_info *delayedWorkInfo; + + delayedWorkInfo = container_of(work, struct vmci_delayed_work_info, work); +ASSERT (delayedWorkInfo); +ASSERT (delayedWorkInfo->workFn); + + delayedWorkInfo->workFn (delayedWorkInfo->data); + +kfree (delayedWorkInfo); +} + +/* +*---------------------------------------------------------------------------- +* +* vmci_drv_schedule_delayed_work -- +* +* Schedule the specified callback. +* +* Results: +* Zero on success, error code otherwise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------------- +*/ + +int vmci_drv_schedule_delayed_work (VMCIWorkFn * workFn,// IN +void *data)// IN +{ +struct vmci_delayed_work_info *delayedWorkInfo; + +ASSERT (workFn); + + delayedWorkInfo =kmalloc (sizeof *delayedWorkInfo,GFP_ATOMIC ); +if (!delayedWorkInfo) +return VMCI_ERROR_NO_MEM ; + + delayedWorkInfo->workFn = workFn; + delayedWorkInfo->data = data; + +INIT_WORK (&delayedWorkInfo->work, drv_delayed_work_cb); + +schedule_work (&delayedWorkInfo->work); + +return VMCI_SUCCESS ; +} + +/* +*----------------------------------------------------------------------------- +* +* vmci_drv_wait_on_event_intr -- +* +* Results: +* True if the wait was interrupted by a signal, false otherwise. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +bool vmci_drv_wait_on_event_intr (wait_queue_head_t * event,// IN: +VMCIEventReleaseCB releaseCB,// IN: +void *clientData)// IN: +{ +DECLARE_WAITQUEUE (wait, current); + +if (event ==NULL || releaseCB ==NULL ) +return false; + +add_wait_queue (event, &wait); + current->state =TASK_INTERRUPTIBLE ; + +/* +* Release the lock or other primitive that makes it possible for us to +* put the current thread on the wait queue without missing the signal. +* Ie. on Linux we need to put ourselves on the wait queue and set our +* stateto TASK_INTERRUPTIBLE without another thread signalling us. +* The releaseCB is used to synchronize this. +*/ +releaseCB (clientData); + +schedule (); + current->state =TASK_RUNNING ; +remove_wait_queue (event, &wait); + +return signal_pending (current); +} + +/* +*---------------------------------------------------------------------- +* +* drv_host_cleanup -- +* +* Cleans up the host specific components of the VMCI module. +* +* Results: +* None. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +static void drv_host_cleanup (void ) +{ +vmci_ctx_release_ctx (hostContext); +vmci_qp_broker_exit (); +} + +/* +*----------------------------------------------------------------------------- +* +* drv_device_enabled -- +* +* Checks whether the VMCI device is enabled. +* +* Results: +* true if device is enabled, false otherwise. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +static bool drv_device_enabled (void ) +{ +return vmci_guest_code_active () + ||vmci_host_code_active (); +} + +/* +*---------------------------------------------------------------------- +* +* VMCI_DeviceGet -- +* +* Verifies that a valid VMCI device is present, and indicates +* the callers intention to use the device until it calls +* VMCI_DeviceRelease(). +* +* Results: +* true if a valid VMCI device is present, false otherwise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +bool VMCI_DeviceGet (uint32_t * apiVersion,// IN/OUT +VMCI_DeviceShutdownFn * deviceShutdownCB,// UNUSED +void *userData,// UNUSED +void **deviceRegistration)// OUT +{ +if (NULL != deviceRegistration) { + *deviceRegistration =NULL ; + } + +if (*apiVersion >VMCI_KERNEL_API_VERSION ) { + *apiVersion =VMCI_KERNEL_API_VERSION ; +return false; + } + +if (!drv_device_enabled ()) { +return false; + } + +return true; +} + +EXPORT_SYMBOL (VMCI_DeviceGet); + +/* +*---------------------------------------------------------------------- +* +* VMCI_DeviceRelease -- +* +* Indicates that the caller is done using the VMCI device. +* +* Results: +* None. +* +* Side effects: +* Useless. +* +*---------------------------------------------------------------------- +*/ + +void VMCI_DeviceRelease (void *deviceRegistration)// UNUSED +{ +} + +EXPORT_SYMBOL (VMCI_DeviceRelease); + +/* +*----------------------------------------------------------------------------- +* +* drv_util_cid_update -- +* +* Gets called with the new context id if updated or resumed. +* +* Results: +* Context id. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +static void drv_util_cid_update (uint32_t subID,// IN: +struct vmci_event_data *eventData,// IN: +void *clientData)// IN: +{ +struct vmci_event_payld_ctx *evPayload = +vmci_event_data_payload (eventData); + +if (subID != ctxUpdateSubID) { +pr_devel ("Invalid subscriber (ID=0x%x)." , subID); +return ; + } + +if (eventData ==NULL || evPayload->contextID ==VMCI_INVALID_ID ) { +pr_devel ("Invalid event data." ); +return ; + } + +pr_devel ("Updating context from (ID=0x%x) to (ID=0x%x) on event " +"(type=%d)." ,atomic_read (&vmContextID), evPayload->contextID, + eventData->event); + +atomic_set (&vmContextID, evPayload->contextID); +} + +/* +*----------------------------------------------------------------------------- +* +* drv_util_init -- +* +* Subscribe to context id update event. +* +* Results: +* None. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +static void __devinit drv_util_init (void ) +{ +/* +* We subscribe to the VMCI_EVENT_CTX_ID_UPDATE here so we can update the +* internal context id when needed. +*/ +if (VMCIEvent_Subscribe + (VMCI_EVENT_CTX_ID_UPDATE ,VMCI_FLAG_EVENT_NONE , + drv_util_cid_update,NULL , &ctxUpdateSubID) <VMCI_SUCCESS ) { +pr_warn ("Failed to subscribe to event (type=%d)." , +VMCI_EVENT_CTX_ID_UPDATE ); + } +} + +/* +*----------------------------------------------------------------------------- +* +* vmci_util_exit -- +* +* Cleanup +* +* Results: +* None. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +static void vmci_util_exit (void ) +{ +if (VMCIEvent_Unsubscribe (ctxUpdateSubID) <VMCI_SUCCESS ) { +pr_warn ("Failed to unsubscribe to event (type=%d) with " +"subscriber (ID=0x%x)." ,VMCI_EVENT_CTX_ID_UPDATE , + ctxUpdateSubID); + } +} + +/* +*----------------------------------------------------------------------------- +* +* drv_check_host_caps -- +* +* Verify that the host supports the hypercalls we need. If it does not, +* try to find fallback hypercalls and use those instead. +* +* Results: +* true if required hypercalls (or fallback hypercalls) are +* supported by the host, false otherwise. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +static bool drv_check_host_caps (void ) +{ +bool result; +struct vmci_rscs_query_msg *msg; +uint32_t msgSize =sizeof (struct vmci_resource_query_hdr ) + +VMCI_UTIL_NUM_RESOURCES *sizeof (uint32_t ); +struct vmci_dg *checkMsg =kmalloc (msgSize,GFP_KERNEL ); + +if (checkMsg ==NULL ) { +pr_warn ("Check host: Insufficient memory." ); +return false; + } + + checkMsg->dst =vmci_make_handle (VMCI_HYPERVISOR_CONTEXT_ID , +VMCI_RESOURCES_QUERY ); + checkMsg->src =VMCI_ANON_SRC_HANDLE ; + checkMsg->payloadSize = msgSize -VMCI_DG_HEADERSIZE ; + msg = (struct vmci_rscs_query_msg *)VMCI_DG_PAYLOAD (checkMsg); + + msg->numResources =VMCI_UTIL_NUM_RESOURCES ; + msg->resources[0 ] =VMCI_GET_CONTEXT_ID ; + +/* Checks that hyper calls are supported */ + result = (0x1 ==vmci_send_dg (checkMsg)); +kfree (checkMsg); + +pr_info ("Host capability check: %s." , + result ?"PASSED" :"FAILED" ); + +/* We need the vector. There are no fallbacks. */ +return result; +} + +/* +*---------------------------------------------------------------------- +* +* drv_read_dgs_from_port -- +* +* Reads datagrams from the data in port and dispatches them. We +* always start reading datagrams into only the first page of the +* datagram buffer. If the datagrams don't fit into one page, we +* use the maximum datagram buffer size for the remainder of the +* invocation. This is a simple heuristic for not penalizing +* small datagrams. +* +* This function assumes that it has exclusive access to the data +* in port for the duration of the call. +* +* Results: +* No result. +* +* Side effects: +* Datagram handlers may be invoked. +* +*---------------------------------------------------------------------- +*/ + +static void drv_read_dgs_from_port (int ioHandle,// IN +unsigned short int dgInPort,// IN +uint8_t * dgInBuffer,// IN +size_t dgInBufferSize)// IN +{ +struct vmci_dg *dg; +size_t currentDgInBufferSize =PAGE_SIZE ; +size_t remainingBytes; + +ASSERT (dgInBufferSize >=PAGE_SIZE ); + +insb (dgInPort, dgInBuffer, currentDgInBufferSize); + dg = (struct vmci_dg *)dgInBuffer; + remainingBytes = currentDgInBufferSize; + +while (dg->dst.resource !=VMCI_INVALID_ID + || remainingBytes >PAGE_SIZE ) { +unsigned dgInSize; + +/* +* When the input buffer spans multiple pages, a datagram can +* start on any page boundary in the buffer. +*/ + +if (dg->dst.resource ==VMCI_INVALID_ID ) { +ASSERT (remainingBytes >PAGE_SIZE ); + dg = (struct vmci_dg *)roundup ((uintptr_t ) + dg +1 ,PAGE_SIZE ); +ASSERT ((uint8_t *) dg < + dgInBuffer + currentDgInBufferSize); + remainingBytes = + (size_t ) (dgInBuffer + currentDgInBufferSize - + (uint8_t *) dg); +continue ; + } + + dgInSize =VMCI_DG_SIZE_ALIGNED (dg); + +if (dgInSize <= dgInBufferSize) { +int result; + +/* +* If the remaining bytes in the datagram buffer doesn't +* contain the complete datagram, we first make sure we have +* enough room for it and then we read the reminder of the +* datagram and possibly any following datagrams. +*/ + +if (dgInSize > remainingBytes) { +if (remainingBytes != currentDgInBufferSize) { + +/* +* We move the partial datagram to the front and read +* the reminder of the datagram and possibly following +* calls into the following bytes. +*/ + +memmove (dgInBuffer, dgInBuffer + + currentDgInBufferSize - + remainingBytes, remainingBytes); + dg = (struct vmci_dg *) + dgInBuffer; + } + +if (currentDgInBufferSize != dgInBufferSize) + currentDgInBufferSize = dgInBufferSize; + +insb (dgInPort, dgInBuffer + remainingBytes, + currentDgInBufferSize - remainingBytes); + } + +/* We special case event datagrams from the hypervisor. */ +if (dg->src.context ==VMCI_HYPERVISOR_CONTEXT_ID + && dg->dst.resource ==VMCI_EVENT_HANDLER ) { + result =vmci_event_dispatch (dg); + }else { + result =vmci_dg_invoke_guest_handler (dg); + } +if (result <VMCI_SUCCESS ) { +pr_devel ("Datagram with resource " +"(ID=0x%x) failed (err=%d)." , + dg->dst.resource, result); + } + +/* On to the next datagram. */ + dg = (struct vmci_dg *)((uint8_t *) dg + + dgInSize); + }else { +size_t bytesToSkip; + +/* Datagram doesn't fit in datagram buffer of maximal size. We drop it. */ +pr_devel ("Failed to receive datagram (size=%u bytes)." , + dgInSize); + + bytesToSkip = dgInSize - remainingBytes; +if (currentDgInBufferSize != dgInBufferSize) + currentDgInBufferSize = dgInBufferSize; + +for (;;) { +insb (dgInPort, dgInBuffer, + currentDgInBufferSize); +if (bytesToSkip <= currentDgInBufferSize) +break ; + + bytesToSkip -= currentDgInBufferSize; + } + dg = (struct vmci_dg *)(dgInBuffer + bytesToSkip); + } + + remainingBytes = + (size_t ) (dgInBuffer + currentDgInBufferSize - + (uint8_t *) dg); + +if (remainingBytes <VMCI_DG_HEADERSIZE ) { +/* Get the next batch of datagrams. */ + +insb (dgInPort, dgInBuffer, currentDgInBufferSize); + dg = (struct vmci_dg *)dgInBuffer; + remainingBytes = currentDgInBufferSize; + } + } +} + +/* +*---------------------------------------------------------------------------- +* +* VMCI_GetContextID -- +* +* Returns the current context ID. Note that since this is accessed only +* from code running in the host, this always returns the host context ID. +* +* Results: +* Context ID. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------------- +*/ + +uint32_t VMCI_GetContextID (void ) +{ +if (vmci_guest_code_active ()) { +if (atomic_read (&vmContextID) ==VMCI_INVALID_ID ) { +uint32_t result; +struct vmci_dg getCidMsg; + getCidMsg.dst = +vmci_make_handle (VMCI_HYPERVISOR_CONTEXT_ID , +VMCI_GET_CONTEXT_ID ); + getCidMsg.src =VMCI_ANON_SRC_HANDLE ; + getCidMsg.payloadSize =0 ; + result =vmci_send_dg (&getCidMsg); +atomic_set (&vmContextID, result); + } +return atomic_read (&vmContextID); + }else if (vmci_host_code_active ()) { +return VMCI_HOST_CONTEXT_ID ; + } +return VMCI_INVALID_ID ; +} + +EXPORT_SYMBOL (VMCI_GetContextID); + +/* +*---------------------------------------------------------------------- +* +* VMCI_Version -- +* +* Returns the version of the VMCI driver. +* +* Results: +* Returns a version number. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +uint32_t VMCI_Version () +{ +return VMCI_VERSION ; +} + +EXPORT_SYMBOL (VMCI_Version); + +/* +*---------------------------------------------------------------------- +* +* drv_shared_init -- +* +* Initializes VMCI components shared between guest and host +* driver. This registers core hypercalls. +* +* Results: +* VMCI_SUCCESS if successful, appropriate error code otherwise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +static int __init drv_shared_init (void ) +{ +int result; + + result =vmci_resource_init (); +if (result <VMCI_SUCCESS ) { +pr_warn ("Failed to initialize VMCIResource (result=%d)." , + result); + goto errorExit; + } + + result =vmci_ctx_init (); +if (result <VMCI_SUCCESS ) { +pr_warn ("Failed to initialize VMCIContext (result=%d)." , + result); + goto resourceExit; + } + + result =vmci_dg_init (); +if (result <VMCI_SUCCESS ) { +pr_warn ("Failed to initialize VMCIDatagram (result=%d)." , + result); + goto resourceExit; + } + + result =vmci_event_init (); +if (result <VMCI_SUCCESS ) { +pr_warn ("Failed to initialize VMCIEvent (result=%d)." , + result); + goto resourceExit; + } + + result =vmci_dbell_init (); +if (result <VMCI_SUCCESS ) { +pr_warn ("Failed to initialize VMCIDoorbell (result=%d)." , + result); + goto eventExit; + } + +pr_notice ("shared components initialized." ); +return VMCI_SUCCESS ; + +eventExit: +vmci_event_exit (); +resourceExit: +vmci_resource_exit (); +errorExit: +return result; +} + +/* +*---------------------------------------------------------------------- +* +* drv_shared_cleanup -- +* +* Cleans up VMCI components shared between guest and host +* driver. +* +* Results: +* None. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +static void drv_shared_cleanup (void ) +{ +vmci_event_exit (); +vmci_resource_exit (); +} + +static const struct file_operations vmuser_fops; +static struct vmci_linux_state linuxState = { + .misc = { + .name =MODULE_NAME , + .minor =MISC_DYNAMIC_MINOR , + .fops = &vmuser_fops, + }, + .activeContexts =ATOMIC_INIT (0 ), +}; + +/* +*---------------------------------------------------------------------- +* +* drv_driver_open -- +* +* Called on open of /dev/vmci. +* +* Side effects: +* Increment use count used to determine eventual deallocation of +* the module +* +*---------------------------------------------------------------------- +*/ + +static int drv_driver_open (struct inode *inode,// IN +struct file *filp)// IN +{ +struct vmci_linux *vmciLinux; + + vmciLinux =kzalloc (sizeof (struct vmci_linux ),GFP_KERNEL ); +if (vmciLinux ==NULL ) +return -ENOMEM ; + + vmciLinux->ctType =VMCIOBJ_NOT_SET ; +mutex_init (&vmciLinux->lock); + filp->private_data = vmciLinux; + +return 0 ; +} + +/* +*---------------------------------------------------------------------- +* +* drv_driver_close -- +* +* Called on close of /dev/vmci, most often when the process +* exits. +* +*---------------------------------------------------------------------- +*/ + +static int drv_driver_close (struct inode *inode,// IN +struct file *filp)// IN +{ +struct vmci_linux *vmciLinux; + + vmciLinux = (struct vmci_linux *)filp->private_data; +ASSERT (vmciLinux); + +if (vmciLinux->ctType ==VMCIOBJ_CONTEXT ) { +ASSERT (vmciLinux->context); + +vmci_ctx_release_ctx (vmciLinux->context); + vmciLinux->context =NULL ; + +/* +* The number of active contexts is used to track whether any +* VMX'en are using the host personality. It is incremented when +* a context is created through the IOCTL_VMCI_INIT_CONTEXT +* ioctl. +*/ + +atomic_dec (&linuxState.activeContexts); + } + vmciLinux->ctType =VMCIOBJ_NOT_SET ; + +kfree (vmciLinux); + filp->private_data =NULL ; +return 0 ; +} + +/* +*---------------------------------------------------------------------- +* +* drv_driver_poll -- +* +* This is used to wake up the VMX when a VMCI call arrives, or +* to wake up select() or poll() at the next clock tick. +* +*---------------------------------------------------------------------- +*/ + +static unsigned int drv_driver_poll (struct file *filp,poll_table * wait) +{ +struct vmci_linux *vmciLinux = (struct vmci_linux *)filp->private_data; +unsigned int mask =0 ; + +if (vmciLinux->ctType ==VMCIOBJ_CONTEXT ) { +ASSERT (vmciLinux->context !=NULL ); +/* +* Check for VMCI calls to this VM context. +*/ + +if (wait !=NULL ) { +poll_wait (filp, + &vmciLinux->context->hostContext.waitQueue, + wait); + } + +spin_lock (&vmciLinux->context->lock); +if (vmciLinux->context->pendingDatagrams >0 || +vmci_handle_arr_get_size (vmciLinux->context-> + pendingDoorbellArray) >0 ) { + mask =POLLIN ; + } +spin_unlock (&vmciLinux->context->lock); + } +return mask; +} + + +/* +*----------------------------------------------------------------------------- +* +* drv_host_init -- +* +* Initializes the VMCI host device driver. +* +* Results: +* 0 on success, other error codes on failure. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +static int __init drv_host_init (void ) +{ +int error; +int result; + + + result =vmci_ctx_init_ctx (VMCI_HOST_CONTEXT_ID , +VMCI_DEFAULT_PROC_PRIVILEGE_FLAGS , +-1 ,VMCI_VERSION ,NULL , &hostContext); +if (result <VMCI_SUCCESS ) { +pr_warn ("Failed to initialize VMCIContext (result=%d)." , + result); +return -ENOMEM ; + } + + result =vmci_qp_broker_init (); +if (result <VMCI_SUCCESS ) { +pr_warn ("Failed to initialize broker (result=%d)." , + result); +vmci_ctx_release_ctx (hostContext); +return -ENOMEM ; + } + + error =misc_register (&linuxState.misc); +if (error) { +pr_warn ("Module registration error " +"(name=%s, major=%d, minor=%d, err=%d)." , + linuxState.misc.name,MISC_MAJOR , linuxState.misc.minor, + error); +drv_host_cleanup (); +return error; + } + +pr_notice ("Module registered (name=%s, major=%d, minor=%d)." , + linuxState.misc.name,MISC_MAJOR , linuxState.misc.minor); + +return 0 ; +} + +/* +*---------------------------------------------------------------------- +* +* drv_cp_harray_to_user -- +* +* Copies the handles of a handle array into a user buffer, and +* returns the new length in userBufferSize. If the copy to the +* user buffer fails, the functions still returns VMCI_SUCCESS, +* but retval != 0. +* +*---------------------------------------------------------------------- +*/ + +static int drv_cp_harray_to_user (void *userBufUVA,// IN +uint64_t * userBufSize,// IN/OUT +struct vmci_handle_arr *handleArray,// IN +int *retval)// IN +{ +uint32_t arraySize =0 ; +struct vmci_handle *handles; + +if (handleArray) + arraySize =vmci_handle_arr_get_size (handleArray); + +if (arraySize *sizeof *handles > *userBufSize) +return VMCI_ERROR_MORE_DATA ; + + *userBufSize = arraySize *sizeof *handles; +if (*userBufSize) + *retval =copy_to_user (userBufUVA, +vmci_handle_arr_get_handles + (handleArray), *userBufSize); + +return VMCI_SUCCESS ; +} + +/* +*----------------------------------------------------------------------------- +* +* drv_qp_broker_alloc -- +* +* Helper function for creating queue pair and copying the result +* to user memory. +* +* Results: +* 0 if result value was copied to user memory, -EFAULT otherwise. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +static int drv_qp_broker_alloc (struct vmci_handle handle, +uint32_t peer, +uint32_t flags, +uint64_t produceSize, +uint64_t consumeSize, +QueuePairPageStore * pageStore, +struct vmci_ctx *context, +bool vmToVm, +void *resultUVA) +{ +uint32_t cid; +int result; +int retval; + + cid =vmci_ctx_get_id (context); + + result = +vmci_qp_broker_alloc (handle, peer, flags, +VMCI_NO_PRIVILEGE_FLAGS , produceSize, + consumeSize, pageStore, context); +if (result ==VMCI_SUCCESS && vmToVm) + result =VMCI_SUCCESS_QUEUEPAIR_CREATE ; + + retval =copy_to_user (resultUVA, &result,sizeof result); +if (retval) { + retval = -EFAULT ; +if (result >=VMCI_SUCCESS ) { + result =vmci_qp_broker_detach (handle, context); +ASSERT (result >=VMCI_SUCCESS ); + } + } + +return retval; +} + +/* +*----------------------------------------------------------------------------- +* +* drv_user_va_lock_page -- +* +* Lock physical page backing a given user VA. Copied from +* bora/modules/vmnet/linux/userif.c:UserIfLockPage(). TODO libify the +* common code. +* +* Results: +* Pointer to struct page on success, NULL otherwise. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +static struct page *drv_user_va_lock_page (uintptr_t addr)// IN: +{ +struct page *page =NULL ; +int retval; + +down_read (¤t->mm->mmap_sem); + retval =get_user_pages (current, current->mm, addr, +1 ,1 ,0 , &page,NULL ); +up_read (¤t->mm->mmap_sem); + +if (retval !=1 ) +return NULL ; + +return page; +} + +/* +*----------------------------------------------------------------------------- +* +* drv_map_bool_ptr -- +* +* Lock physical page backing a given user VA and maps it to kernel +* address space. The range of the mapped memory should be within a +* single page otherwise an error is returned. Copied from +* bora/modules/vmnet/linux/userif.c:VNetUserIfMapUint32Ptr(). TODO +* libify the common code. +* +* Results: +* 0 on success, negative error code otherwise. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +static int drv_map_bool_ptr (uintptr_t notifyUVA,// IN: +struct page **p,// OUT: +bool ** notifyPtr)// OUT: +{ +if (!access_ok (VERIFY_WRITE , notifyUVA,sizeof **notifyPtr) || + (((notifyUVA +sizeof **notifyPtr -1 ) & ~(PAGE_SIZE -1 )) != + (notifyUVA & ~(PAGE_SIZE -1 )))) { +return -EINVAL ; + } + + *p =drv_user_va_lock_page (notifyUVA); +if (*p ==NULL ) +return -EAGAIN ; + + *notifyPtr = + (bool *) ((uint8_t *)kmap (*p) + (notifyUVA & (PAGE_SIZE -1 ))); +return 0 ; +} + +/* +*----------------------------------------------------------------------------- +* +* drv_setup_notify -- +* +* Sets up a given context for notify to work. Calls drv_map_bool_ptr() +* which maps the notify boolean in user VA in kernel space. +* +* Results: +* VMCI_SUCCESS on success, error code otherwise. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +static int drv_setup_notify (struct vmci_ctx *context,// IN: +uintptr_t notifyUVA)// IN: +{ +int retval; + +if (context->notify) { +pr_warn ("Notify mechanism is already set up." ); +return VMCI_ERROR_DUPLICATE_ENTRY ; + } + + retval = +drv_map_bool_ptr (notifyUVA, &context->notifyPage, + &context->notify) == +0 ?VMCI_SUCCESS :VMCI_ERROR_GENERIC ; +if (retval ==VMCI_SUCCESS ) +vmci_ctx_check_signal_notify (context); + +return retval; +} + +/* +*----------------------------------------------------------------------------- +* +* drv_driver_unlocked_ioctl -- +* +* Main path for UserRPC +* +* Results: +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +static long drv_driver_unlocked_ioctl (struct file *filp, +u_int iocmd, +unsigned long ioarg) +{ +struct vmci_linux *vmciLinux = (struct vmci_linux *)filp->private_data; +int retval =0 ; + +switch (iocmd) { +case IOCTL_VMCI_VERSION2 :{ +int verFromUser; + +if (copy_from_user + (&verFromUser, (void *)ioarg,sizeof verFromUser)) { + retval = -EFAULT ; +break ; + } + + vmciLinux->userVersion = verFromUser; + } +/* Fall through. */ +case IOCTL_VMCI_VERSION : +/* +* The basic logic here is: +* +* If the user sends in a version of 0 tell it our version. +* If the user didn't send in a version, tell it our version. +* If the user sent in an old version, tell it -its- version. +* If the user sent in an newer version, tell it our version. +* +* The rationale behind telling the caller its version is that +* Workstation 6.5 required that VMX and VMCI kernel module were +* version sync'd. All new VMX users will be programmed to +* handle the VMCI kernel module version. +*/ + +if (vmciLinux->userVersion >0 && + vmciLinux->userVersion <VMCI_VERSION_HOSTQP ) { + retval = vmciLinux->userVersion; + }else { + retval =VMCI_VERSION ; + } +break ; + +case IOCTL_VMCI_INIT_CONTEXT :{ +struct vmci_init_blk initBlock; +uid_t user; + + retval = +copy_from_user (&initBlock, (void *)ioarg, +sizeof initBlock); +if (retval !=0 ) { +pr_info ("Error reading init block." ); + retval = -EFAULT ; +break ; + } + +mutex_lock (&vmciLinux->lock); +if (vmciLinux->ctType !=VMCIOBJ_NOT_SET ) { +pr_info ("Received VMCI init on initialized handle." ); + retval = -EINVAL ; + goto init_release; + } + +if (initBlock.flags & ~VMCI_PRIVILEGE_FLAG_RESTRICTED ) { +pr_info ("Unsupported VMCI restriction flag." ); + retval = -EINVAL ; + goto init_release; + } + + user =current_uid (); + retval = +vmci_ctx_init_ctx (initBlock.cid, + initBlock.flags, +0 /* Unused */ , + vmciLinux->userVersion, + &user, &vmciLinux->context); +if (retval <VMCI_SUCCESS ) { +pr_info ("Error initializing context." ); + retval = + retval == +VMCI_ERROR_DUPLICATE_ENTRY ? -EEXIST : + -EINVAL ; + goto init_release; + } + +/* +* Copy cid to userlevel, we do this to allow the VMX to enforce its +* policy on cid generation. +*/ + initBlock.cid =vmci_ctx_get_id (vmciLinux->context); + retval = +copy_to_user ((void *)ioarg, &initBlock, +sizeof initBlock); +if (retval !=0 ) { +vmci_ctx_release_ctx (vmciLinux->context); + vmciLinux->context =NULL ; +pr_info ("Error writing init block." ); + retval = -EFAULT ; + goto init_release; + } +ASSERT (initBlock.cid !=VMCI_INVALID_ID ); + + vmciLinux->ctType =VMCIOBJ_CONTEXT ; + +atomic_inc (&linuxState.activeContexts); + + init_release: +mutex_unlock (&vmciLinux->lock); +break ; + } + +case IOCTL_VMCI_DATAGRAM_SEND :{ +struct vmci_dg_snd_rcv_info sendInfo; +struct vmci_dg *dg =NULL ; +uint32_t cid; + +if (vmciLinux->ctType !=VMCIOBJ_CONTEXT ) { +pr_warn ("Ioctl only valid for context handle (iocmd=%d)." , + iocmd); + retval = -EINVAL ; +break ; + } + + retval = +copy_from_user (&sendInfo, (void *)ioarg, +sizeof sendInfo); +if (retval) { +pr_warn ("copy_from_user failed." ); + retval = -EFAULT ; +break ; + } + +if (sendInfo.len >VMCI_MAX_DG_SIZE ) { +pr_warn ("Datagram too big (size=%d)." , + sendInfo.len); + retval = -EINVAL ; +break ; + } + +if (sendInfo.len <sizeof *dg) { +pr_warn ("Datagram too small (size=%d)." , + sendInfo.len); + retval = -EINVAL ; +break ; + } + + dg =kmalloc (sendInfo.len,GFP_KERNEL ); +if (dg ==NULL ) { +pr_info ("Cannot allocate memory to dispatch datagram." ); + retval = -ENOMEM ; +break ; + } + + retval = +copy_from_user (dg, + (char *)(uintptr_t ) sendInfo.addr, + sendInfo.len); +if (retval !=0 ) { +pr_info ("Error getting datagram (err=%d)." , + retval); +kfree (dg); + retval = -EFAULT ; +break ; + } + +pr_devel ("Datagram dst (handle=0x%x:0x%x) src " +"(handle=0x%x:0x%x), payload " +"(size=%llu bytes)." , + dg->dst.context, dg->dst.resource, + dg->src.context, dg->src.resource, + (unsigned long long ) dg->payloadSize); + +/* Get source context id. */ +ASSERT (vmciLinux->context); + cid =vmci_ctx_get_id (vmciLinux->context); +ASSERT (cid !=VMCI_INVALID_ID ); + sendInfo.result =vmci_dg_dispatch (cid, dg, true); +kfree (dg); + retval = +copy_to_user ((void *)ioarg, &sendInfo, +sizeof sendInfo); +break ; + } + +case IOCTL_VMCI_DATAGRAM_RECEIVE :{ +struct vmci_dg_snd_rcv_info recvInfo; +struct vmci_dg *dg =NULL ; +size_t size; + +if (vmciLinux->ctType !=VMCIOBJ_CONTEXT ) { +pr_warn ("Ioctl only valid for context handle (iocmd=%d)." , + iocmd); + retval = -EINVAL ; +break ; + } + + retval = +copy_from_user (&recvInfo, (void *)ioarg, +sizeof recvInfo); +if (retval) { +pr_warn ("copy_from_user failed." ); + retval = -EFAULT ; +break ; + } + +ASSERT (vmciLinux->ctType ==VMCIOBJ_CONTEXT ); + + size = recvInfo.len; +ASSERT (vmciLinux->context); + recvInfo.result = +vmci_ctx_dequeue_dg (vmciLinux->context, + &size, &dg); + +if (recvInfo.result >=VMCI_SUCCESS ) { +ASSERT (dg); + retval =copy_to_user ((void *)((uintptr_t ) + recvInfo.addr), + dg,VMCI_DG_SIZE (dg)); +kfree (dg); +if (retval !=0 ) +break ; + } + retval = +copy_to_user ((void *)ioarg, &recvInfo, +sizeof recvInfo); +break ; + } + +case IOCTL_VMCI_QUEUEPAIR_ALLOC :{ +if (vmciLinux->ctType !=VMCIOBJ_CONTEXT ) { +pr_info ("IOCTL_VMCI_QUEUEPAIR_ALLOC only valid for contexts." ); + retval = -EINVAL ; +break ; + } + +if (vmciLinux->userVersion <VMCI_VERSION_NOVMVM ) { +struct vmci_qp_alloc_info_vmvm queuePairAllocInfo; +struct vmci_qp_alloc_info_vmvm *info = + (struct vmci_qp_alloc_info_vmvm *)ioarg; + + retval = +copy_from_user (&queuePairAllocInfo, + (void *)ioarg, +sizeof queuePairAllocInfo); +if (retval) { + retval = -EFAULT ; +break ; + } + + retval =drv_qp_broker_alloc (queuePairAllocInfo.handle, queuePairAllocInfo.peer, queuePairAllocInfo.flags, queuePairAllocInfo.produceSize, queuePairAllocInfo.consumeSize,NULL , vmciLinux->context, true,// VM to VM style create + &info->result); + }else { +struct vmci_qp_alloc_info + queuePairAllocInfo; +struct vmci_qp_alloc_info *info = + (struct vmci_qp_alloc_info *)ioarg; +QueuePairPageStore pageStore; + + retval = +copy_from_user (&queuePairAllocInfo, + (void *)ioarg, +sizeof queuePairAllocInfo); +if (retval) { + retval = -EFAULT ; +break ; + } + + pageStore.pages = queuePairAllocInfo.ppnVA; + pageStore.len = queuePairAllocInfo.numPPNs; + + retval =drv_qp_broker_alloc ( + queuePairAllocInfo.handle, + queuePairAllocInfo.peer, + queuePairAllocInfo.flags, + queuePairAllocInfo.produceSize, + queuePairAllocInfo.consumeSize, + &pageStore, vmciLinux->context, + false, &info->result); + } +break ; + } + +case IOCTL_VMCI_QUEUEPAIR_SETVA :{ +struct vmci_qp_set_va_info setVAInfo; +struct vmci_qp_set_va_info *info = + (struct vmci_qp_set_va_info *)ioarg; +int32_t result; + +if (vmciLinux->ctType !=VMCIOBJ_CONTEXT ) { +pr_info ("IOCTL_VMCI_QUEUEPAIR_SETVA only valid for contexts." ); + retval = -EINVAL ; +break ; + } + +if (vmciLinux->userVersion <VMCI_VERSION_NOVMVM ) { +pr_info ("IOCTL_VMCI_QUEUEPAIR_SETVA not supported for this VMX version." ); + retval = -EINVAL ; +break ; + } + + retval = +copy_from_user (&setVAInfo, (void *)ioarg, +sizeof setVAInfo); +if (retval) { + retval = -EFAULT ; +break ; + } + +if (setVAInfo.va) { +/* +* VMX is passing down a new VA for the queue pair mapping. +*/ + + result =vmci_qp_broker_map (setVAInfo.handle, + vmciLinux->context, + setVAInfo.va); + }else { +/* +* The queue pair is about to be unmapped by the VMX. +*/ + + result =vmci_qp_broker_unmap (setVAInfo.handle, + vmciLinux->context,0 ); + } + + retval =copy_to_user (&info->result, &result,sizeof result); +if (retval) + retval = -EFAULT ; + +break ; + } + +case IOCTL_VMCI_QUEUEPAIR_SETPAGEFILE :{ +struct vmci_qp_page_file_info pageFileInfo; +struct vmci_qp_page_file_info *info = + (struct vmci_qp_page_file_info *)ioarg; +int32_t result; + +if (vmciLinux->userVersion <VMCI_VERSION_HOSTQP || + vmciLinux->userVersion >=VMCI_VERSION_NOVMVM ) { +pr_info ("IOCTL_VMCI_QUEUEPAIR_SETPAGEFILE not supported this VMX " +"(version=%d)." , vmciLinux->userVersion); + retval = -EINVAL ; +break ; + } + +if (vmciLinux->ctType !=VMCIOBJ_CONTEXT ) { +pr_info ("IOCTL_VMCI_QUEUEPAIR_SETPAGEFILE only valid for contexts." ); + retval = -EINVAL ; +break ; + } + + retval = +copy_from_user (&pageFileInfo, (void *)ioarg, +sizeof *info); +if (retval) { + retval = -EFAULT ; +break ; + } + +/* +* Communicate success pre-emptively to the caller. Note that +* the basic premise is that it is incumbent upon the caller not +* to look at the info.result field until after the ioctl() +* returns. And then, only if the ioctl() result indicates no +* error. We send up the SUCCESS status before calling +* SetPageStore() store because failing to copy up the result +* code means unwinding the SetPageStore(). +* +* It turns out the logic to unwind a SetPageStore() opens a can +* of worms. For example, if a host had created the QueuePair +* and a guest attaches and SetPageStore() is successful but +* writing success fails, then ... the host has to be stopped +* from writing (anymore) data into the QueuePair. That means +* an additional test in the VMCI_Enqueue() code path. Ugh. +*/ + + result =VMCI_SUCCESS ; + retval = +copy_to_user (&info->result, &result,sizeof result); +if (retval ==0 ) { + result = +vmci_qp_broker_set_page_store + (pageFileInfo.handle, + pageFileInfo.produceVA, + pageFileInfo.consumeVA, + vmciLinux->context); +if (result <VMCI_SUCCESS ) { + + retval = +copy_to_user (&info->result, + &result, +sizeof result); +if (retval !=0 ) { +/* +* Note that in this case the SetPageStore() call +* failed but we were unable to communicate that to the +* caller (because the copy_to_user() call failed). +* So, if we simply return an error (in this case +* -EFAULT) then the caller will know that the +* SetPageStore failed even though we couldn't put the +* result code in the result field and indicate exactly +* why it failed. +* +* That says nothing about the issue where we were once +* able to write to the caller's info memory and now +* can't. Something more serious is probably going on +* than the fact that SetPageStore() didn't work. +*/ + retval = -EFAULT ; + } + } + + }else { +/* +* In this case, we can't write a result field of the +* caller's info block. So, we don't even try to +* SetPageStore(). +*/ + retval = -EFAULT ; + } + +break ; + } + +case IOCTL_VMCI_QUEUEPAIR_DETACH :{ +struct vmci_qp_dtch_info detachInfo; +struct vmci_qp_dtch_info *info = + (struct vmci_qp_dtch_info *)ioarg; +int32_t result; + +if (vmciLinux->ctType !=VMCIOBJ_CONTEXT ) { +pr_info ("IOCTL_VMCI_QUEUEPAIR_DETACH only valid for contexts." ); + retval = -EINVAL ; +break ; + } + + retval = +copy_from_user (&detachInfo, (void *)ioarg, +sizeof detachInfo); +if (retval) { + retval = -EFAULT ; +break ; + } + + result = +vmci_qp_broker_detach (detachInfo.handle, + vmciLinux->context); +if (result ==VMCI_SUCCESS + && vmciLinux->userVersion <VMCI_VERSION_NOVMVM ) { + result =VMCI_SUCCESS_LAST_DETACH ; + } + + retval = +copy_to_user (&info->result, &result,sizeof result); +if (retval) { + retval = -EFAULT ; + } + +break ; + } + +case IOCTL_VMCI_CTX_ADD_NOTIFICATION :{ +struct vmci_ctx_info arInfo; +struct vmci_ctx_info *info = + (struct vmci_ctx_info *)ioarg; +int32_t result; +uint32_t cid; + +if (vmciLinux->ctType !=VMCIOBJ_CONTEXT ) { +pr_info ( +"IOCTL_VMCI_CTX_ADD_NOTIFICATION only valid for contexts." ); + retval = -EINVAL ; +break ; + } + + retval = +copy_from_user (&arInfo, (void *)ioarg, +sizeof arInfo); +if (retval) { + retval = -EFAULT ; +break ; + } + + cid =vmci_ctx_get_id (vmciLinux->context); + result = +vmci_ctx_add_notification (cid, arInfo.remoteCID); + retval = +copy_to_user (&info->result, &result,sizeof result); +if (retval) { + retval = -EFAULT ; +break ; + } +break ; + } + +case IOCTL_VMCI_CTX_REMOVE_NOTIFICATION :{ +struct vmci_ctx_info arInfo; +struct vmci_ctx_info *info = + (struct vmci_ctx_info *)ioarg; +int32_t result; +uint32_t cid; + +if (vmciLinux->ctType !=VMCIOBJ_CONTEXT ) { +pr_info ("IOCTL_VMCI_CTX_REMOVE_NOTIFICATION only valid for " +"contexts." ); + retval = -EINVAL ; +break ; + } + + retval = +copy_from_user (&arInfo, (void *)ioarg, +sizeof arInfo); +if (retval) { + retval = -EFAULT ; +break ; + } + + cid =vmci_ctx_get_id (vmciLinux->context); + result = +vmci_ctx_remove_notification (cid, + arInfo.remoteCID); + retval = +copy_to_user (&info->result, &result,sizeof result); +if (retval) { + retval = -EFAULT ; +break ; + } +break ; + } + +case IOCTL_VMCI_CTX_GET_CPT_STATE :{ +struct vmci_ctx_chkpt_buf_info getInfo; +uint32_t cid; +char *cptBuf; + +if (vmciLinux->ctType !=VMCIOBJ_CONTEXT ) { +pr_info ("IOCTL_VMCI_CTX_GET_CPT_STATE only valid for contexts." ); + retval = -EINVAL ; +break ; + } + + retval = +copy_from_user (&getInfo, (void *)ioarg, +sizeof getInfo); +if (retval) { + retval = -EFAULT ; +break ; + } + + cid =vmci_ctx_get_id (vmciLinux->context); + getInfo.result = +vmci_ctx_get_chkpt_state (cid, + getInfo.cptType, + &getInfo.bufSize, + &cptBuf); +if (getInfo.result ==VMCI_SUCCESS && getInfo.bufSize) { + retval =copy_to_user ((void *)(uintptr_t ) + getInfo.cptBuf, cptBuf, + getInfo.bufSize); +kfree (cptBuf); +if (retval) { + retval = -EFAULT ; +break ; + } + } + retval = +copy_to_user ((void *)ioarg, &getInfo, +sizeof getInfo); +if (retval) { + retval = -EFAULT ; +break ; + } +break ; + } + +case IOCTL_VMCI_CTX_SET_CPT_STATE :{ +struct vmci_ctx_chkpt_buf_info setInfo; +uint32_t cid; +char *cptBuf; + +if (vmciLinux->ctType !=VMCIOBJ_CONTEXT ) { +pr_info ("IOCTL_VMCI_CTX_SET_CPT_STATE only valid for contexts." ); + retval = -EINVAL ; +break ; + } + + retval = +copy_from_user (&setInfo, (void *)ioarg, +sizeof setInfo); +if (retval) { + retval = -EFAULT ; +break ; + } + + cptBuf =kmalloc (setInfo.bufSize,GFP_KERNEL ); +if (cptBuf ==NULL ) { +pr_info ("Cannot allocate memory to set cpt state (type=%d)." , + setInfo.cptType); + retval = -ENOMEM ; +break ; + } + retval = +copy_from_user (cptBuf, + (void *)(uintptr_t ) setInfo.cptBuf, + setInfo.bufSize); +if (retval) { +kfree (cptBuf); + retval = -EFAULT ; +break ; + } + + cid =vmci_ctx_get_id (vmciLinux->context); + setInfo.result = +vmci_ctx_set_chkpt_state (cid, + setInfo.cptType, + setInfo.bufSize, + cptBuf); +kfree (cptBuf); + retval = +copy_to_user ((void *)ioarg, &setInfo, +sizeof setInfo); +if (retval) { + retval = -EFAULT ; +break ; + } +break ; + } + +case IOCTL_VMCI_GET_CONTEXT_ID :{ +uint32_t cid =VMCI_HOST_CONTEXT_ID ; + + retval =copy_to_user ((void *)ioarg, &cid,sizeof cid); +break ; + } + +case IOCTL_VMCI_SET_NOTIFY :{ +struct vmci_set_notify_info notifyInfo; + +if (vmciLinux->ctType !=VMCIOBJ_CONTEXT ) { +pr_info ("IOCTL_VMCI_SET_NOTIFY only valid for contexts." ); + retval = -EINVAL ; +break ; + } + + retval = +copy_from_user (¬ifyInfo, (void *)ioarg, +sizeof notifyInfo); +if (retval) { + retval = -EFAULT ; +break ; + } + +if ((uintptr_t ) notifyInfo.notifyUVA != + (uintptr_t )NULL ) { + notifyInfo.result = +drv_setup_notify (vmciLinux->context, + (uintptr_t ) + notifyInfo.notifyUVA); + }else { +spin_lock (&vmciLinux->context->lock); +vmci_ctx_unset_notify (vmciLinux->context); +spin_unlock (&vmciLinux->context->lock); + notifyInfo.result =VMCI_SUCCESS ; + } + + retval = +copy_to_user ((void *)ioarg, ¬ifyInfo, +sizeof notifyInfo); +if (retval) { + retval = -EFAULT ; +break ; + } + +break ; + } + +case IOCTL_VMCI_NOTIFY_RESOURCE :{ +struct vmci_dbell_notify_resource_info info; +uint32_t cid; + +if (vmciLinux->userVersion <VMCI_VERSION_NOTIFY ) { +pr_info ("IOCTL_VMCI_NOTIFY_RESOURCE is invalid for current" +" VMX versions." ); + retval = -EINVAL ; +break ; + } + +if (vmciLinux->ctType !=VMCIOBJ_CONTEXT ) { +pr_info ("IOCTL_VMCI_NOTIFY_RESOURCE is only valid for contexts." ); + retval = -EINVAL ; +break ; + } + + retval = +copy_from_user (&info, (void *)ioarg,sizeof info); +if (retval) { + retval = -EFAULT ; +break ; + } + + cid =vmci_ctx_get_id (vmciLinux->context); +switch (info.action) { +case VMCI_NOTIFY_RESOURCE_ACTION_NOTIFY : +if (info.resource == +VMCI_NOTIFY_RESOURCE_DOOR_BELL ) { + info.result = +vmci_ctx_notify_dbell (cid, + info. + handle, +VMCI_NO_PRIVILEGE_FLAGS ); + }else { + info.result =VMCI_ERROR_UNAVAILABLE ; + } +break ; +case VMCI_NOTIFY_RESOURCE_ACTION_CREATE : + info.result = +vmci_ctx_dbell_create (cid, + info.handle); +break ; +case VMCI_NOTIFY_RESOURCE_ACTION_DESTROY : + info.result = +vmci_ctx_dbell_destroy (cid, + info.handle); +break ; +default : +pr_info ("IOCTL_VMCI_NOTIFY_RESOURCE got unknown action (action=%d)." , + info.action); + info.result =VMCI_ERROR_INVALID_ARGS ; + } + retval =copy_to_user ((void *)ioarg, &info, +sizeof info); +if (retval) { + retval = -EFAULT ; +break ; + } + +break ; + } + +case IOCTL_VMCI_NOTIFICATIONS_RECEIVE :{ +struct vmci_ctx_notify_recv_info info; +struct vmci_handle_arr *dbHandleArray; +struct vmci_handle_arr *qpHandleArray; +uint32_t cid; + +if (vmciLinux->ctType !=VMCIOBJ_CONTEXT ) { +pr_info ("IOCTL_VMCI_NOTIFICATIONS_RECEIVE is only valid for contexts." ); + retval = -EINVAL ; +break ; + } + +if (vmciLinux->userVersion <VMCI_VERSION_NOTIFY ) { +pr_info ("IOCTL_VMCI_NOTIFICATIONS_RECEIVE is not supported for the " +"current vmx version." ); + retval = -EINVAL ; +break ; + } + + retval = +copy_from_user (&info, (void *)ioarg,sizeof info); +if (retval) { + retval = -EFAULT ; +break ; + } + +if ((info.dbHandleBufSize && !info.dbHandleBufUVA) + || (info.qpHandleBufSize && !info.qpHandleBufUVA)) { + retval = -EINVAL ; +break ; + } + + cid =vmci_ctx_get_id (vmciLinux->context); + info.result = +vmci_ctx_rcv_notifications_get (cid, + &dbHandleArray, + &qpHandleArray); +if (info.result ==VMCI_SUCCESS ) { + info.result =drv_cp_harray_to_user ((void *) + (uintptr_t ) + info. + dbHandleBufUVA, + &info. + dbHandleBufSize, + dbHandleArray, + &retval); +if (info.result ==VMCI_SUCCESS && !retval) { + info.result = +drv_cp_harray_to_user ((void *) + (uintptr_t ) + info. + qpHandleBufUVA, + &info. + qpHandleBufSize, + qpHandleArray, + &retval); + } +if (!retval) { + retval = +copy_to_user ((void *)ioarg, + &info,sizeof info); + } +vmci_ctx_rcv_notifications_release + (cid, dbHandleArray, qpHandleArray, + info.result ==VMCI_SUCCESS && !retval); + }else { + retval = +copy_to_user ((void *)ioarg, &info, +sizeof info); + } +break ; + } + +default : +pr_warn ("Unknown ioctl (iocmd=%d)." , iocmd); + retval = -EINVAL ; + } + +return retval; +} + +/* +*----------------------------------------------------------------------------- +* +* PCI device support -- +* +* The following functions implement the support for the VMCI +* guest device. This includes initializing the device and +* interrupt handling. +* +*----------------------------------------------------------------------------- +*/ + +/* +*----------------------------------------------------------------------------- +* +* drv_dispatch_dgs -- +* +* Reads and dispatches incoming datagrams. +* +* Results: +* None. +* +* Side effects: +* Reads data from the device. +* +*----------------------------------------------------------------------------- +*/ + +static void drv_dispatch_dgs (unsigned long data) +{ +struct vmci_device *dev = (struct vmci_device *)data; + +if (dev ==NULL ) { +pr_devel ("No virtual device present in %s." , __func__); +return ; + } + +if (data_buffer ==NULL ) { +pr_devel ("No buffer present in %s." , __func__); +return ; + } + +drv_read_dgs_from_port ((int )0 , + dev->ioaddr +VMCI_DATA_IN_ADDR , + data_buffer, data_buffer_size); +} + +DECLARE_TASKLET (vmci_dg_tasklet, drv_dispatch_dgs, (unsigned long )&vmci_dev); + +/* +*----------------------------------------------------------------------------- +* +* drv_process_bitmap -- +* +* Scans the notification bitmap for raised flags, clears them +* and handles the notifications. +* +* Results: +* None. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +static void drv_process_bitmap (unsigned long data) +{ +struct vmci_device *dev = (struct vmci_device *)data; + +if (dev ==NULL ) { +pr_devel ("No virtual device present in %s." , __func__); +return ; + } + +if (notification_bitmap ==NULL ) { +pr_devel ("No bitmap present in %s." , __func__); +return ; + } + +vmci_dbell_scan_notification_entries (notification_bitmap); +} + +DECLARE_TASKLET (vmci_bm_tasklet, drv_process_bitmap, (unsigned long )&vmci_dev); + +/* +*----------------------------------------------------------------------------- +* +* drv_enable_msix -- +* +* Enable MSI-X. Try exclusive vectors first, then shared vectors. +* +* Results: +* 0 on success, other error codes on failure. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +static int drv_enable_msix (struct pci_dev *pdev)// IN +{ +int i; +int result; + +for (i =0 ; i <VMCI_MAX_INTRS ; ++i) { + vmci_dev.msix_entries[i].entry = i; + vmci_dev.msix_entries[i].vector = i; + } + + result =pci_enable_msix (pdev, vmci_dev.msix_entries,VMCI_MAX_INTRS ); +if (result ==0 ) + vmci_dev.exclusive_vectors = true; +else if (result >0 ) + result =pci_enable_msix (pdev, vmci_dev.msix_entries,1 ); + +return result; +} + +/* +*----------------------------------------------------------------------------- +* +* drv_interrupt -- +* +* Interrupt handler for legacy or MSI interrupt, or for first MSI-X +* interrupt (vector VMCI_INTR_DATAGRAM). +* +* Results: +* COMPAT_IRQ_HANDLED if the interrupt is handled, COMPAT_IRQ_NONE if +* not an interrupt. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +static irqreturn_t drv_interrupt (int irq,// IN +void *clientdata)// IN +{ +struct vmci_device *dev = clientdata; + +if (dev ==NULL ) { +pr_devel ("Irq %d for unknown device in %s." , irq, __func__); +return IRQ_NONE ; + } + +/* +* If we are using MSI-X with exclusive vectors then we simply schedule +* the datagram tasklet, since we know the interrupt was meant for us. +* Otherwise we must read the ICR to determine what to do. +*/ + +if (dev->intr_type ==VMCI_INTR_TYPE_MSIX && dev->exclusive_vectors) { +tasklet_schedule (&vmci_dg_tasklet); + }else { +unsigned int icr; + +ASSERT (dev->intr_type ==VMCI_INTR_TYPE_INTX || + dev->intr_type ==VMCI_INTR_TYPE_MSI ); + +/* Acknowledge interrupt and determine what needs doing. */ + icr =inl (dev->ioaddr +VMCI_ICR_ADDR ); +if (icr ==0 || icr == ~0 ) +return IRQ_NONE ; + +if (icr &VMCI_ICR_DATAGRAM ) { +tasklet_schedule (&vmci_dg_tasklet); + icr &= ~VMCI_ICR_DATAGRAM ; + } + +if (icr &VMCI_ICR_NOTIFICATION ) { +tasklet_schedule (&vmci_bm_tasklet); + icr &= ~VMCI_ICR_NOTIFICATION ; + } + +if (icr !=0 ) +pr_info ("Ignoring unknown interrupt cause (%d)." , icr); + } + +return IRQ_HANDLED ; +} + +/* +*----------------------------------------------------------------------------- +* +* drv_interrupt_bm -- +* +* Interrupt handler for MSI-X interrupt vector VMCI_INTR_NOTIFICATION, +* which is for the notification bitmap. Will only get called if we are +* using MSI-X with exclusive vectors. +* +* Results: +* COMPAT_IRQ_HANDLED if the interrupt is handled, COMPAT_IRQ_NONE if +* not an interrupt. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +static irqreturn_t drv_interrupt_bm (int irq,// IN +void *clientdata)// IN +{ +struct vmci_device *dev = clientdata; + +if (dev ==NULL ) { +pr_devel ("Irq %d for unknown device in %s." , irq, __func__); +return IRQ_NONE ; + } + +/* For MSI-X we can just assume it was meant for us. */ +ASSERT (dev->intr_type ==VMCI_INTR_TYPE_MSIX && dev->exclusive_vectors); +tasklet_schedule (&vmci_bm_tasklet); + +return IRQ_HANDLED ; +} + +/* +*----------------------------------------------------------------------------- +* +* drv_probe_device -- +* +* Most of the initialization at module load time is done here. +* +* Results: +* Returns 0 for success, an error otherwise. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +static int __devinit drv_probe_device (struct pci_dev *pdev,// IN: vmci PCI device +const struct pci_device_id *id)// IN: matching device ID +{ +unsigned int ioaddr; +unsigned int ioaddr_size; +unsigned int capabilities; +int result; + +pr_info ("Probing for vmci/PCI." ); + + result =pci_enable_device (pdev); +if (result) { +printk (KERN_ERR "Cannot enable VMCI device %s: error %d" , +pci_name (pdev), result); +return result; + } +pci_set_master (pdev);/* To enable QueuePair functionality. */ + ioaddr =pci_resource_start (pdev,0 ); + ioaddr_size =pci_resource_len (pdev,0 ); + +/* +* Request I/O region with adjusted base address and size. The adjusted +* values are needed and used if we release the region in case of failure. +*/ + +if (!request_region (ioaddr, ioaddr_size,MODULE_NAME )) { +pr_info (MODULE_NAME ": Another driver already loaded " +"for device in slot %s." ,pci_name (pdev)); + goto pci_disable; + } + +pr_info ("Found VMCI PCI device at %#x, irq %u." , ioaddr, pdev->irq); + +/* +* Verify that the VMCI Device supports the capabilities that +* we need. If the device is missing capabilities that we would +* like to use, check for fallback capabilities and use those +* instead (so we can run a new VM on old hosts). Fail the load if +* a required capability is missing and there is no fallback. +* +* Right now, we need datagrams. There are no fallbacks. +*/ + capabilities =inl (ioaddr +VMCI_CAPS_ADDR ); + +if ((capabilities &VMCI_CAPS_DATAGRAM ) ==0 ) { +pr_err ("Device does not support datagrams." ); + goto release; + } + +/* +* If the hardware supports notifications, we will use that as +* well. +*/ +if (capabilities &VMCI_CAPS_NOTIFICATIONS ) { + capabilities =VMCI_CAPS_DATAGRAM ; + notification_bitmap =vmalloc (PAGE_SIZE ); +if (notification_bitmap ==NULL ) { +pr_err ("Device unable to allocate notification bitmap." ); + }else { +memset (notification_bitmap,0 ,PAGE_SIZE ); + capabilities |=VMCI_CAPS_NOTIFICATIONS ; + } + }else { + capabilities =VMCI_CAPS_DATAGRAM ; + } +pr_info ("Using capabilities 0x%x." , capabilities); + +/* Let the host know which capabilities we intend to use. */ +outl (capabilities, ioaddr +VMCI_CAPS_ADDR ); + +/* Device struct initialization. */ +mutex_lock (&vmci_dev.lock); +if (vmci_dev.enabled) { +pr_err ("Device already enabled." ); + goto unlock; + } + + vmci_dev.ioaddr = ioaddr; + vmci_dev.ioaddr_size = ioaddr_size; +atomic_set (&vmci_dev.datagrams_allowed,1 ); + +/* +* Register notification bitmap with device if that capability is +* used +*/ +if (capabilities &VMCI_CAPS_NOTIFICATIONS ) { +unsigned long bitmapPPN; + bitmapPPN =page_to_pfn (vmalloc_to_page (notification_bitmap)); +if (!vmci_dbell_register_notification_bitmap (bitmapPPN)) { +pr_err ("VMCI device unable to register notification bitmap " +"with PPN 0x%x." , (uint32_t ) bitmapPPN); + goto datagram_disallow; + } + } + +/* Check host capabilities. */ +if (!drv_check_host_caps ()) { + goto remove_bitmap; + } + +/* Enable device. */ + vmci_dev.enabled = true; +pci_set_drvdata (pdev, &vmci_dev); + +/* +* We do global initialization here because we need datagrams +* during drv_util_init, since it registers for VMCI events. If we +* ever support more than one VMCI device we will have to create +* seperate LateInit/EarlyExit functions that can be used to do +* initialization/cleanup that depends on the device being +* accessible. We need to initialize VMCI components before +* requesting an irq - the VMCI interrupt handler uses these +* components, and it may be invoked once request_irq() has +* registered the handler (as the irq line may be shared). +*/ +drv_util_init (); + +if (vmci_qp_guest_endpoints_init () <VMCI_SUCCESS ) { + goto util_exit; + } + +/* +* Enable interrupts. Try MSI-X first, then MSI, and then fallback on +* legacy interrupts. +*/ +if (!vmci_disable_msix && !drv_enable_msix (pdev)) { + vmci_dev.intr_type =VMCI_INTR_TYPE_MSIX ; + vmci_dev.irq = vmci_dev.msix_entries[0 ].vector; + }else if (!vmci_disable_msi && !pci_enable_msi (pdev)) { + vmci_dev.intr_type =VMCI_INTR_TYPE_MSI ; + vmci_dev.irq = pdev->irq; + }else { + vmci_dev.intr_type =VMCI_INTR_TYPE_INTX ; + vmci_dev.irq = pdev->irq; + } + +/* Request IRQ for legacy or MSI interrupts, or for first MSI-X vector. */ + result =request_irq (vmci_dev.irq, drv_interrupt,IRQF_SHARED , +MODULE_NAME , &vmci_dev); +if (result) { +pr_err ("Irq %u in use: %d" , vmci_dev.irq, result); + goto components_exit; + } + +/* +* For MSI-X with exclusive vectors we need to request an interrupt for each +* vector so that we get a separate interrupt handler routine. This allows +* us to distinguish between the vectors. +*/ + +if (vmci_dev.exclusive_vectors) { +ASSERT (vmci_dev.intr_type ==VMCI_INTR_TYPE_MSIX ); + result =request_irq (vmci_dev.msix_entries[1 ].vector, + drv_interrupt_bm,0 ,MODULE_NAME , + &vmci_dev); +if (result) { +pr_err ("Irq %u in use: %d" , + vmci_dev.msix_entries[1 ].vector, result); +free_irq (vmci_dev.irq, &vmci_dev); + goto components_exit; + } + } + +pr_info ("Registered device." ); +atomic_inc (&guestDeviceActive); +mutex_unlock (&vmci_dev.lock); + +/* Enable specific interrupt bits. */ +if (capabilities &VMCI_CAPS_NOTIFICATIONS ) { +outl (VMCI_IMR_DATAGRAM |VMCI_IMR_NOTIFICATION , + vmci_dev.ioaddr +VMCI_IMR_ADDR ); + }else { +outl (VMCI_IMR_DATAGRAM , vmci_dev.ioaddr +VMCI_IMR_ADDR ); + } + +/* Enable interrupts. */ +outl (VMCI_CONTROL_INT_ENABLE , vmci_dev.ioaddr +VMCI_CONTROL_ADDR ); + +return 0 ; + +components_exit: +vmci_qp_guest_endpoints_exit (); +util_exit: +vmci_util_exit (); + vmci_dev.enabled = false; +if (vmci_dev.intr_type ==VMCI_INTR_TYPE_MSIX ) +pci_disable_msix (pdev); +else if (vmci_dev.intr_type ==VMCI_INTR_TYPE_MSI ) +pci_disable_msi (pdev); + +remove_bitmap: +if (notification_bitmap) +outl (VMCI_CONTROL_RESET , vmci_dev.ioaddr +VMCI_CONTROL_ADDR ); + +datagram_disallow: +atomic_set (&vmci_dev.datagrams_allowed,0 ); +unlock: +mutex_unlock (&vmci_dev.lock); +release: +if (notification_bitmap) { +vfree (notification_bitmap); + notification_bitmap =NULL ; + } +release_region (ioaddr, ioaddr_size); +pci_disable: +pci_disable_device (pdev); +return -EBUSY ; +} + +/* +*----------------------------------------------------------------------------- +* +* drv_remove_device -- +* +* Cleanup, called for each device on unload. +* +* Results: +* None. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +static void __devexit drv_remove_device(struct pci_dev *pdev) +{ +struct vmci_device *dev =pci_get_drvdata (pdev); + +pr_info ("Removing device" ); +atomic_dec (&guestDeviceActive); +vmci_qp_guest_endpoints_exit (); +vmci_util_exit (); +mutex_lock (&dev->lock); +atomic_set (&vmci_dev.datagrams_allowed,0 ); +pr_info ("Resetting vmci device" ); +outl (VMCI_CONTROL_RESET , vmci_dev.ioaddr +VMCI_CONTROL_ADDR ); + +/* +* Free IRQ and then disable MSI/MSI-X as appropriate. For MSI-X, we might +* have multiple vectors, each with their own IRQ, which we must free too. +*/ +free_irq (dev->irq, dev); +if (dev->intr_type ==VMCI_INTR_TYPE_MSIX ) { +if (dev->exclusive_vectors) +free_irq (dev->msix_entries[1 ].vector, dev); + +pci_disable_msix (pdev); + }else if (dev->intr_type ==VMCI_INTR_TYPE_MSI ) { +pci_disable_msi (pdev); + } + dev->exclusive_vectors = false; + dev->intr_type =VMCI_INTR_TYPE_INTX ; + +release_region (dev->ioaddr, dev->ioaddr_size); + dev->enabled = false; +if (notification_bitmap) { +/* +* The device reset above cleared the bitmap state of the +* device, so we can safely free it here. +*/ + +vfree (notification_bitmap); + notification_bitmap =NULL ; + } + +pr_info ("Unregistered device." ); +mutex_unlock (&dev->lock); + +pci_disable_device (pdev); +} + +static struct pci_driver vmci_driver = { + .name =MODULE_NAME , + .id_table = vmci_ids, + .probe = drv_probe_device, + .remove =__devexit_p (drv_remove_device), +}; + +/* +*----------------------------------------------------------------------------- +* +* dev_guest_init -- +* +* Initializes the VMCI PCI device. The initialization might fail +* if there is no VMCI PCI device. +* +* Results: +* 0 on success, other error codes on failure. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +static int __init dev_guest_init (void ) +{ +int retval; + +/* Initialize guest device data. */ +mutex_init (&vmci_dev.lock); + vmci_dev.intr_type =VMCI_INTR_TYPE_INTX ; + vmci_dev.exclusive_vectors = false; +spin_lock_init (&vmci_dev.dev_spinlock); + vmci_dev.enabled = false; +atomic_set (&vmci_dev.datagrams_allowed,0 ); +atomic_set (&guestDeviceActive,0 ); + + data_buffer =vmalloc (data_buffer_size); +if (!data_buffer) +return -ENOMEM ; + +/* This should be last to make sure we are done initializing. */ + retval =pci_register_driver (&vmci_driver); +if (retval <0 ) { +vfree (data_buffer); + data_buffer =NULL ; +return retval; + } + +return 0 ; +} + +static const struct file_operations vmuser_fops = { + .owner =THIS_MODULE , + .open = drv_driver_open, + .release = drv_driver_close, + .poll = drv_driver_poll, + .unlocked_ioctl = drv_driver_unlocked_ioctl, + .compat_ioctl = drv_driver_unlocked_ioctl, +}; + + +/* +*----------------------------------------------------------------------------- +* +* vmci_send_dg -- +* +* VM to hypervisor call mechanism. We use the standard VMware naming +* convention since shared code is calling this function as well. +* +* Results: +* The result of the hypercall. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +int vmci_send_dg (struct vmci_dg *dg) +{ +unsigned long flags; +int result; + +/* Check args. */ +if (dg ==NULL ) +return VMCI_ERROR_INVALID_ARGS ; + +if (atomic_read (&vmci_dev.datagrams_allowed) ==0 ) +return VMCI_ERROR_UNAVAILABLE ; + +/* +* Need to acquire spinlock on the device because +* the datagram data may be spread over multiple pages and the monitor may +* interleave device user rpc calls from multiple VCPUs. Acquiring the +* spinlock precludes that possibility. Disabling interrupts to avoid +* incoming datagrams during a "rep out" and possibly landing up in this +* function. +*/ +spin_lock_irqsave (&vmci_dev.dev_spinlock, flags); + +/* +* Send the datagram and retrieve the return value from the result register. +*/ + __asm__ __volatile__("cld\n\t" "rep outsb\n\t" :/* No output. */ + :"d" (vmci_dev.ioaddr +VMCI_DATA_OUT_ADDR ), +"c" (VMCI_DG_SIZE (dg)),"S" (dg) + ); + +/* +* XXX Should read result high port as well when updating handlers to +* return 64bit. +*/ + result =inl (vmci_dev.ioaddr +VMCI_RESULT_LOW_ADDR ); +spin_unlock_irqrestore (&vmci_dev.dev_spinlock, flags); + +return result; +} + +/* +*----------------------------------------------------------------------------- +* +* vmci_guest_code_active -- +* +* Determines whether the VMCI PCI device has been successfully +* initialized. +* +* Results: +* true, if VMCI guest device is operational, false otherwise. +* +* Side effects: +* Reads data from the device. +* +*----------------------------------------------------------------------------- +*/ + +bool vmci_guest_code_active (void ) +{ +return guestDeviceInit &&atomic_read (&guestDeviceActive) >0 ; +} + +/* +*----------------------------------------------------------------------------- +* +* vmci_host_code_active -- +* +* Determines whether the VMCI host personality is +* available. Since the core functionality of the host driver is +* always present, all guests could possibly use the host +* personality. However, to minimize the deviation from the +* pre-unified driver state of affairs, we only consider the host +* device active, if there is no active guest device, or if there +* are VMX'en with active VMCI contexts using the host device. +* +* Results: +* true, if VMCI host driver is operational, false otherwise. +* +* Side effects: +* Reads data from the device. +* +*----------------------------------------------------------------------------- +*/ + +bool vmci_host_code_active (void ) +{ +return hostDeviceInit && + (!vmci_guest_code_active () || +atomic_read (&linuxState.activeContexts) >0 ); +} + +/* +*---------------------------------------------------------------------- +* +* drv_init -- +* +* linux module entry point. Called by /sbin/insmod command +* +* Results: +* registers a device driver for a major # that depends +* on the uid. Add yourself to that list. List is now in +* private/driver-private.c. +* +*---------------------------------------------------------------------- +*/ + +static int __init drv_init (void ) +{ +int retval; + + retval =drv_shared_init (); +if (retval !=VMCI_SUCCESS ) { +pr_warn ("Failed to initialize common " +"components (err=%d)." , retval); +return -ENOMEM ; + } + +if (!vmci_disable_guest) { + retval =dev_guest_init (); +if (retval !=0 ) { +pr_warn ("Failed to initialize guest " +"personality (err=%d)." , retval); + }else { +const char *state =vmci_guest_code_active ()? +"active" :"inactive" ; + guestDeviceInit = true; +pr_info ("Guest personality initialized and is " +"%s." , state); + } + } + +if (!vmci_disable_host) { + retval =drv_host_init (); +if (retval !=0 ) { +pr_warn ("Unable to initialize host " +"personality (err=%d)." , retval); + }else { + hostDeviceInit = true; +pr_info ("Initialized host personality" ); + } + } + +if (!guestDeviceInit && !hostDeviceInit) { +drv_shared_cleanup (); +return -ENODEV ; + } + +pr_info ("Module is initialized" ); +return 0 ; +} + +/* +*---------------------------------------------------------------------- +* +* drv_exit -- +* +* Called by /sbin/rmmod +* +* +*---------------------------------------------------------------------- +*/ + +static void __exit drv_exit(void) +{ +if (guestDeviceInit) { +pci_unregister_driver (&vmci_driver); +vfree (data_buffer); + guestDeviceInit = false; + } + +if (hostDeviceInit) { +drv_host_cleanup (); + +if (misc_deregister (&linuxState.misc)) +pr_warn ("Error unregistering" ); +else +pr_info ("Module unloaded" ); + + hostDeviceInit = false; + } + +drv_shared_cleanup (); +} + +module_init (drv_init); +module_exit (drv_exit); +MODULE_DEVICE_TABLE (pci, vmci_ids); + +module_param_named (disable_host, vmci_disable_host, bool,0 ); +MODULE_PARM_DESC (disable_host,"Disable driver host personality - (default=0)" ); + +module_param_named (disable_guest, vmci_disable_guest, bool,0 ); +MODULE_PARM_DESC (disable_guest, +"Disable driver guest personality - (default=0)" ); + +module_param_named (disable_msi, vmci_disable_msi, bool,0 ); +MODULE_PARM_DESC (disable_msi,"Disable MSI use in driver - (default=0)" ); + +module_param_named (disable_msix, vmci_disable_msix, bool,0 ); +MODULE_PARM_DESC (disable_msix,"Disable MSI-X use in driver - (default=0)" ); + +MODULE_AUTHOR ("VMware, Inc." ); +MODULE_DESCRIPTION ("VMware Virtual Machine Communication Interface." ); +MODULE_VERSION (VMCI_DRIVER_VERSION_STRING ); +MODULE_LICENSE ("GPL v2" ); + +/* +* Starting with SLE10sp2, Novell requires that IHVs sign a support agreement +* with them and mark their kernel modules as externally supported via a +* change to the module header. If this isn't done, the module will not load +* by default (i.e., neither mkinitrd nor modprobe will accept it). +*/ +MODULE_INFO (supported,"external" );
drivers/misc/vmw_vmci/vmci_driver.h new file +52 -0
52 lines not on this page · raw message
Signature · 1 line
1.7.0.4
RFC 4/11 Apply VMCI driver code
In reply to Andrew Stiegmann (stieg)This code implementes both the host and guest personalities of the VMCI driver.
--- drivers/misc/vmw_vmci/vmci_driver.c | 2875 +++++++++++++++++++++++++++++++++++ drivers/misc/vmw_vmci/vmci_driver.h | 52 + 2 files changed, 2927 insertions(+), 0 deletions(-) create mode 100644 drivers/misc/vmw_vmci/vmci_driver.c create mode 100644 drivers/misc/vmw_vmci/vmci_driver.h
drivers/misc/vmw_vmci/vmci_driver.c new file +2875 -0
@@ -0,0 +1,2875 @@ +/* +* VMware VMCI Driver +* +* Copyright (C) 2012 VMware, Inc. All rights reserved. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms of the GNU General Public License as published by the +* Free Software Foundation version 2 and no later version. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +* for more details. +* +* You should have received a copy of the GNU General Public License along +* with this program; if not, write to the Free Software Foundation, Inc., +* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include <asm/atomic.h> +#include <asm/io.h> +#include <linux/file.h> +#include <linux/fs.h> +#include <linux/highmem.h> +#include <linux/init.h> +#include <linux/interrupt.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/miscdevice.h> +#include <linux/moduleparam.h> +#include <linux/mutex.h> +#include <linux/pci.h> +#include <linux/poll.h> +#include <linux/sched.h> +#include <linux/smp.h> +#include <linux/version.h> +#include <linux/vmw_vmci_api.h> +#include <linux/vmw_vmci_defs.h> + +#include "vmci_handle_array.h" +#include "vmci_common_int.h" +#include "vmci_context.h" +#include "vmci_datagram.h" +#include "vmci_doorbell.h" +#include "vmci_driver.h" +#include "vmci_event.h" +#include "vmci_hash_table.h" +#include "vmci_queue_pair.h" +#include "vmci_resource.h" + +#define VMCI_UTIL_NUM_RESOURCES 1 + +enum { +VMCI_NOTIFY_RESOURCE_QUEUE_PAIR =0 , +VMCI_NOTIFY_RESOURCE_DOOR_BELL =1 , +}; + +enum { +VMCI_NOTIFY_RESOURCE_ACTION_NOTIFY =0 , +VMCI_NOTIFY_RESOURCE_ACTION_CREATE =1 , +VMCI_NOTIFY_RESOURCE_ACTION_DESTROY =2 , +}; + +static uint32_t ctxUpdateSubID =VMCI_INVALID_ID ; +static struct vmci_ctx *hostContext; +static atomic_t vmContextID = {VMCI_INVALID_ID }; + +struct vmci_delayed_work_info { +struct work_struct work; +VMCIWorkFn *workFn; +void *data; +}; + +/* +*---------------------------------------------------------------------- +* +* PCI Device interface -- +* +* Declarations of types and functions related to the VMCI PCI +* device personality. +* +* +*---------------------------------------------------------------------- +*/ + +/* +* VMCI driver initialization. This block can also be used to +* pass initial group membership etc. +*/ +struct vmci_init_blk { +uint32_t cid; +uint32_t flags; +}; + +/* VMCIQueuePairAllocInfo_VMToVM */ +struct vmci_qp_alloc_info_vmvm { +struct vmci_handle handle; +uint32_t peer; +uint32_t flags; +uint64_t produceSize; +uint64_t consumeSize; +uint64_t producePageFile;/* User VA. */ +uint64_t consumePageFile;/* User VA. */ +uint64_t producePageFileSize;/* Size of the file name array. */ +uint64_t consumePageFileSize;/* Size of the file name array. */ +int32_t result; +uint32_t _pad; +}; + +/* VMCISetNotifyInfo: Used to pass notify flag's address to the host driver. */ +struct vmci_set_notify_info { +uint64_t notifyUVA; +int32_t result; +uint32_t _pad; +}; + +struct vmci_device { +struct mutex lock; + +unsigned int ioaddr; +unsigned int ioaddr_size; +unsigned int irq; +unsigned int intr_type; +bool exclusive_vectors; +struct msix_entry msix_entries[VMCI_MAX_INTRS ]; + +bool enabled; +spinlock_t dev_spinlock; +atomic_t datagrams_allowed; +}; + +static const struct pci_device_id vmci_ids[] = { + {PCI_DEVICE (PCI_VENDOR_ID_VMWARE ,PCI_DEVICE_ID_VMWARE_VMCI ),}, + {0 }, +}; + + +static struct vmci_device vmci_dev; +static bool vmci_disable_host = false; +static bool vmci_disable_guest = false; +static bool vmci_disable_msi = false; +static bool vmci_disable_msix = false; + +/* +* Allocate a buffer for incoming datagrams globally to avoid repeated +* allocation in the interrupt handler's atomic context. +*/ + +static uint8_t *data_buffer =NULL ; +static uint32_t data_buffer_size =VMCI_MAX_DG_SIZE ; + +/* +* If the VMCI hardware supports the notification bitmap, we allocate +* and register a page with the device. +*/ +static uint8_t *notification_bitmap =NULL ; + +/* +*---------------------------------------------------------------------- +* +* Host device node interface -- +* +* Implements VMCI by implementing open/close/ioctl functions +* +* +*---------------------------------------------------------------------- +*/ + +/* +* Per-instance host state +*/ +struct vmci_linux { +struct vmci_ctx *context; +int userVersion; +enum vmci_obj_type ctType; +struct mutex lock; +}; + +/* +* Static driver state. +*/ +struct vmci_linux_state { +struct miscdevice misc; +char buf[1024 ]; +atomic_t activeContexts; +}; + +/* +*---------------------------------------------------------------------- +* +* Shared VMCI device definitions -- +* +* Types and variables shared by both host and guest personality +* +* +*---------------------------------------------------------------------- +*/ + +static bool guestDeviceInit; +static atomic_t guestDeviceActive; +static bool hostDeviceInit; + + + +/* +*---------------------------------------------------------------------------- +* +* drv_delayed_work_cb +* +* Called in a worker thread context. +* +* Results: +* None. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------------- +*/ + +static void drv_delayed_work_cb (struct work_struct *work)// IN +{ +struct vmci_delayed_work_info *delayedWorkInfo; + + delayedWorkInfo = container_of(work, struct vmci_delayed_work_info, work); +ASSERT (delayedWorkInfo); +ASSERT (delayedWorkInfo->workFn); + + delayedWorkInfo->workFn (delayedWorkInfo->data); + +kfree (delayedWorkInfo); +} + +/* +*---------------------------------------------------------------------------- +* +* vmci_drv_schedule_delayed_work -- +* +* Schedule the specified callback. +* +* Results: +* Zero on success, error code otherwise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------------- +*/ + +int vmci_drv_schedule_delayed_work (VMCIWorkFn * workFn,// IN +void *data)// IN +{ +struct vmci_delayed_work_info *delayedWorkInfo; + +ASSERT (workFn); + + delayedWorkInfo =kmalloc (sizeof *delayedWorkInfo,GFP_ATOMIC ); +if (!delayedWorkInfo) +return VMCI_ERROR_NO_MEM ; + + delayedWorkInfo->workFn = workFn; + delayedWorkInfo->data = data; + +INIT_WORK (&delayedWorkInfo->work, drv_delayed_work_cb); + +schedule_work (&delayedWorkInfo->work); + +return VMCI_SUCCESS ; +} + +/* +*----------------------------------------------------------------------------- +* +* vmci_drv_wait_on_event_intr -- +* +* Results: +* True if the wait was interrupted by a signal, false otherwise. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +bool vmci_drv_wait_on_event_intr (wait_queue_head_t * event,// IN: +VMCIEventReleaseCB releaseCB,// IN: +void *clientData)// IN: +{ +DECLARE_WAITQUEUE (wait, current); + +if (event ==NULL || releaseCB ==NULL ) +return false; + +add_wait_queue (event, &wait); + current->state =TASK_INTERRUPTIBLE ; + +/* +* Release the lock or other primitive that makes it possible for us to +* put the current thread on the wait queue without missing the signal. +* Ie. on Linux we need to put ourselves on the wait queue and set our +* stateto TASK_INTERRUPTIBLE without another thread signalling us. +* The releaseCB is used to synchronize this. +*/ +releaseCB (clientData); + +schedule (); + current->state =TASK_RUNNING ; +remove_wait_queue (event, &wait); + +return signal_pending (current); +} + +/* +*---------------------------------------------------------------------- +* +* drv_host_cleanup -- +* +* Cleans up the host specific components of the VMCI module. +* +* Results: +* None. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +static void drv_host_cleanup (void ) +{ +vmci_ctx_release_ctx (hostContext); +vmci_qp_broker_exit (); +} + +/* +*----------------------------------------------------------------------------- +* +* drv_device_enabled -- +* +* Checks whether the VMCI device is enabled. +* +* Results: +* true if device is enabled, false otherwise. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +static bool drv_device_enabled (void ) +{ +return vmci_guest_code_active () + ||vmci_host_code_active (); +} + +/* +*---------------------------------------------------------------------- +* +* VMCI_DeviceGet -- +* +* Verifies that a valid VMCI device is present, and indicates +* the callers intention to use the device until it calls +* VMCI_DeviceRelease(). +* +* Results: +* true if a valid VMCI device is present, false otherwise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +bool VMCI_DeviceGet (uint32_t * apiVersion,// IN/OUT +VMCI_DeviceShutdownFn * deviceShutdownCB,// UNUSED +void *userData,// UNUSED +void **deviceRegistration)// OUT +{ +if (NULL != deviceRegistration) { + *deviceRegistration =NULL ; + } + +if (*apiVersion >VMCI_KERNEL_API_VERSION ) { + *apiVersion =VMCI_KERNEL_API_VERSION ; +return false; + } + +if (!drv_device_enabled ()) { +return false; + } + +return true; +} + +EXPORT_SYMBOL (VMCI_DeviceGet); + +/* +*---------------------------------------------------------------------- +* +* VMCI_DeviceRelease -- +* +* Indicates that the caller is done using the VMCI device. +* +* Results: +* None. +* +* Side effects: +* Useless. +* +*---------------------------------------------------------------------- +*/ + +void VMCI_DeviceRelease (void *deviceRegistration)// UNUSED +{ +} + +EXPORT_SYMBOL (VMCI_DeviceRelease); + +/* +*----------------------------------------------------------------------------- +* +* drv_util_cid_update -- +* +* Gets called with the new context id if updated or resumed. +* +* Results: +* Context id. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +static void drv_util_cid_update (uint32_t subID,// IN: +struct vmci_event_data *eventData,// IN: +void *clientData)// IN: +{ +struct vmci_event_payld_ctx *evPayload = +vmci_event_data_payload (eventData); + +if (subID != ctxUpdateSubID) { +pr_devel ("Invalid subscriber (ID=0x%x)." , subID); +return ; + } + +if (eventData ==NULL || evPayload->contextID ==VMCI_INVALID_ID ) { +pr_devel ("Invalid event data." ); +return ; + } + +pr_devel ("Updating context from (ID=0x%x) to (ID=0x%x) on event " +"(type=%d)." ,atomic_read (&vmContextID), evPayload->contextID, + eventData->event); + +atomic_set (&vmContextID, evPayload->contextID); +} + +/* +*----------------------------------------------------------------------------- +* +* drv_util_init -- +* +* Subscribe to context id update event. +* +* Results: +* None. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +static void __devinit drv_util_init (void ) +{ +/* +* We subscribe to the VMCI_EVENT_CTX_ID_UPDATE here so we can update the +* internal context id when needed. +*/ +if (VMCIEvent_Subscribe + (VMCI_EVENT_CTX_ID_UPDATE ,VMCI_FLAG_EVENT_NONE , + drv_util_cid_update,NULL , &ctxUpdateSubID) <VMCI_SUCCESS ) { +pr_warn ("Failed to subscribe to event (type=%d)." , +VMCI_EVENT_CTX_ID_UPDATE ); + } +} + +/* +*----------------------------------------------------------------------------- +* +* vmci_util_exit -- +* +* Cleanup +* +* Results: +* None. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +static void vmci_util_exit (void ) +{ +if (VMCIEvent_Unsubscribe (ctxUpdateSubID) <VMCI_SUCCESS ) { +pr_warn ("Failed to unsubscribe to event (type=%d) with " +"subscriber (ID=0x%x)." ,VMCI_EVENT_CTX_ID_UPDATE , + ctxUpdateSubID); + } +} + +/* +*----------------------------------------------------------------------------- +* +* drv_check_host_caps -- +* +* Verify that the host supports the hypercalls we need. If it does not, +* try to find fallback hypercalls and use those instead. +* +* Results: +* true if required hypercalls (or fallback hypercalls) are +* supported by the host, false otherwise. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +static bool drv_check_host_caps (void ) +{ +bool result; +struct vmci_rscs_query_msg *msg; +uint32_t msgSize =sizeof (struct vmci_resource_query_hdr ) + +VMCI_UTIL_NUM_RESOURCES *sizeof (uint32_t ); +struct vmci_dg *checkMsg =kmalloc (msgSize,GFP_KERNEL ); + +if (checkMsg ==NULL ) { +pr_warn ("Check host: Insufficient memory." ); +return false; + } + + checkMsg->dst =vmci_make_handle (VMCI_HYPERVISOR_CONTEXT_ID , +VMCI_RESOURCES_QUERY ); + checkMsg->src =VMCI_ANON_SRC_HANDLE ; + checkMsg->payloadSize = msgSize -VMCI_DG_HEADERSIZE ; + msg = (struct vmci_rscs_query_msg *)VMCI_DG_PAYLOAD (checkMsg); + + msg->numResources =VMCI_UTIL_NUM_RESOURCES ; + msg->resources[0 ] =VMCI_GET_CONTEXT_ID ; + +/* Checks that hyper calls are supported */ + result = (0x1 ==vmci_send_dg (checkMsg)); +kfree (checkMsg); + +pr_info ("Host capability check: %s." , + result ?"PASSED" :"FAILED" ); + +/* We need the vector. There are no fallbacks. */ +return result; +} + +/* +*---------------------------------------------------------------------- +* +* drv_read_dgs_from_port -- +* +* Reads datagrams from the data in port and dispatches them. We +* always start reading datagrams into only the first page of the +* datagram buffer. If the datagrams don't fit into one page, we +* use the maximum datagram buffer size for the remainder of the +* invocation. This is a simple heuristic for not penalizing +* small datagrams. +* +* This function assumes that it has exclusive access to the data +* in port for the duration of the call. +* +* Results: +* No result. +* +* Side effects: +* Datagram handlers may be invoked. +* +*---------------------------------------------------------------------- +*/ + +static void drv_read_dgs_from_port (int ioHandle,// IN +unsigned short int dgInPort,// IN +uint8_t * dgInBuffer,// IN +size_t dgInBufferSize)// IN +{ +struct vmci_dg *dg; +size_t currentDgInBufferSize =PAGE_SIZE ; +size_t remainingBytes; + +ASSERT (dgInBufferSize >=PAGE_SIZE ); + +insb (dgInPort, dgInBuffer, currentDgInBufferSize); + dg = (struct vmci_dg *)dgInBuffer; + remainingBytes = currentDgInBufferSize; + +while (dg->dst.resource !=VMCI_INVALID_ID + || remainingBytes >PAGE_SIZE ) { +unsigned dgInSize; + +/* +* When the input buffer spans multiple pages, a datagram can +* start on any page boundary in the buffer. +*/ + +if (dg->dst.resource ==VMCI_INVALID_ID ) { +ASSERT (remainingBytes >PAGE_SIZE ); + dg = (struct vmci_dg *)roundup ((uintptr_t ) + dg +1 ,PAGE_SIZE ); +ASSERT ((uint8_t *) dg < + dgInBuffer + currentDgInBufferSize); + remainingBytes = + (size_t ) (dgInBuffer + currentDgInBufferSize - + (uint8_t *) dg); +continue ; + } + + dgInSize =VMCI_DG_SIZE_ALIGNED (dg); + +if (dgInSize <= dgInBufferSize) { +int result; + +/* +* If the remaining bytes in the datagram buffer doesn't +* contain the complete datagram, we first make sure we have +* enough room for it and then we read the reminder of the +* datagram and possibly any following datagrams. +*/ + +if (dgInSize > remainingBytes) { +if (remainingBytes != currentDgInBufferSize) { + +/* +* We move the partial datagram to the front and read +* the reminder of the datagram and possibly following +* calls into the following bytes. +*/ + +memmove (dgInBuffer, dgInBuffer + + currentDgInBufferSize - + remainingBytes, remainingBytes); + dg = (struct vmci_dg *) + dgInBuffer; + } + +if (currentDgInBufferSize != dgInBufferSize) + currentDgInBufferSize = dgInBufferSize; + +insb (dgInPort, dgInBuffer + remainingBytes, + currentDgInBufferSize - remainingBytes); + } + +/* We special case event datagrams from the hypervisor. */ +if (dg->src.context ==VMCI_HYPERVISOR_CONTEXT_ID + && dg->dst.resource ==VMCI_EVENT_HANDLER ) { + result =vmci_event_dispatch (dg); + }else { + result =vmci_dg_invoke_guest_handler (dg); + } +if (result <VMCI_SUCCESS ) { +pr_devel ("Datagram with resource " +"(ID=0x%x) failed (err=%d)." , + dg->dst.resource, result); + } + +/* On to the next datagram. */ + dg = (struct vmci_dg *)((uint8_t *) dg + + dgInSize); + }else { +size_t bytesToSkip; + +/* Datagram doesn't fit in datagram buffer of maximal size. We drop it. */ +pr_devel ("Failed to receive datagram (size=%u bytes)." , + dgInSize); + + bytesToSkip = dgInSize - remainingBytes; +if (currentDgInBufferSize != dgInBufferSize) + currentDgInBufferSize = dgInBufferSize; + +for (;;) { +insb (dgInPort, dgInBuffer, + currentDgInBufferSize); +if (bytesToSkip <= currentDgInBufferSize) +break ; + + bytesToSkip -= currentDgInBufferSize; + } + dg = (struct vmci_dg *)(dgInBuffer + bytesToSkip); + } + + remainingBytes = + (size_t ) (dgInBuffer + currentDgInBufferSize - + (uint8_t *) dg); + +if (remainingBytes <VMCI_DG_HEADERSIZE ) { +/* Get the next batch of datagrams. */ + +insb (dgInPort, dgInBuffer, currentDgInBufferSize); + dg = (struct vmci_dg *)dgInBuffer; + remainingBytes = currentDgInBufferSize; + } + } +} + +/* +*---------------------------------------------------------------------------- +* +* VMCI_GetContextID -- +* +* Returns the current context ID. Note that since this is accessed only +* from code running in the host, this always returns the host context ID. +* +* Results: +* Context ID. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------------- +*/ + +uint32_t VMCI_GetContextID (void ) +{ +if (vmci_guest_code_active ()) { +if (atomic_read (&vmContextID) ==VMCI_INVALID_ID ) { +uint32_t result; +struct vmci_dg getCidMsg; + getCidMsg.dst = +vmci_make_handle (VMCI_HYPERVISOR_CONTEXT_ID , +VMCI_GET_CONTEXT_ID ); + getCidMsg.src =VMCI_ANON_SRC_HANDLE ; + getCidMsg.payloadSize =0 ; + result =vmci_send_dg (&getCidMsg); +atomic_set (&vmContextID, result); + } +return atomic_read (&vmContextID); + }else if (vmci_host_code_active ()) { +return VMCI_HOST_CONTEXT_ID ; + } +return VMCI_INVALID_ID ; +} + +EXPORT_SYMBOL (VMCI_GetContextID); + +/* +*---------------------------------------------------------------------- +* +* VMCI_Version -- +* +* Returns the version of the VMCI driver. +* +* Results: +* Returns a version number. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +uint32_t VMCI_Version () +{ +return VMCI_VERSION ; +} + +EXPORT_SYMBOL (VMCI_Version); + +/* +*---------------------------------------------------------------------- +* +* drv_shared_init -- +* +* Initializes VMCI components shared between guest and host +* driver. This registers core hypercalls. +* +* Results: +* VMCI_SUCCESS if successful, appropriate error code otherwise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +static int __init drv_shared_init (void ) +{ +int result; + + result =vmci_resource_init (); +if (result <VMCI_SUCCESS ) { +pr_warn ("Failed to initialize VMCIResource (result=%d)." , + result); + goto errorExit; + } + + result =vmci_ctx_init (); +if (result <VMCI_SUCCESS ) { +pr_warn ("Failed to initialize VMCIContext (result=%d)." , + result); + goto resourceExit; + } + + result =vmci_dg_init (); +if (result <VMCI_SUCCESS ) { +pr_warn ("Failed to initialize VMCIDatagram (result=%d)." , + result); + goto resourceExit; + } + + result =vmci_event_init (); +if (result <VMCI_SUCCESS ) { +pr_warn ("Failed to initialize VMCIEvent (result=%d)." , + result); + goto resourceExit; + } + + result =vmci_dbell_init (); +if (result <VMCI_SUCCESS ) { +pr_warn ("Failed to initialize VMCIDoorbell (result=%d)." , + result); + goto eventExit; + } + +pr_notice ("shared components initialized." ); +return VMCI_SUCCESS ; + +eventExit: +vmci_event_exit (); +resourceExit: +vmci_resource_exit (); +errorExit: +return result; +} + +/* +*---------------------------------------------------------------------- +* +* drv_shared_cleanup -- +* +* Cleans up VMCI components shared between guest and host +* driver. +* +* Results: +* None. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +static void drv_shared_cleanup (void ) +{ +vmci_event_exit (); +vmci_resource_exit (); +} + +static const struct file_operations vmuser_fops; +static struct vmci_linux_state linuxState = { + .misc = { + .name =MODULE_NAME , + .minor =MISC_DYNAMIC_MINOR , + .fops = &vmuser_fops, + }, + .activeContexts =ATOMIC_INIT (0 ), +}; + +/* +*---------------------------------------------------------------------- +* +* drv_driver_open -- +* +* Called on open of /dev/vmci. +* +* Side effects: +* Increment use count used to determine eventual deallocation of +* the module +* +*---------------------------------------------------------------------- +*/ + +static int drv_driver_open (struct inode *inode,// IN +struct file *filp)// IN +{ +struct vmci_linux *vmciLinux; + + vmciLinux =kzalloc (sizeof (struct vmci_linux ),GFP_KERNEL ); +if (vmciLinux ==NULL ) +return -ENOMEM ; + + vmciLinux->ctType =VMCIOBJ_NOT_SET ; +mutex_init (&vmciLinux->lock); + filp->private_data = vmciLinux; + +return 0 ; +} + +/* +*---------------------------------------------------------------------- +* +* drv_driver_close -- +* +* Called on close of /dev/vmci, most often when the process +* exits. +* +*---------------------------------------------------------------------- +*/ + +static int drv_driver_close (struct inode *inode,// IN +struct file *filp)// IN +{ +struct vmci_linux *vmciLinux; + + vmciLinux = (struct vmci_linux *)filp->private_data; +ASSERT (vmciLinux); + +if (vmciLinux->ctType ==VMCIOBJ_CONTEXT ) { +ASSERT (vmciLinux->context); + +vmci_ctx_release_ctx (vmciLinux->context); + vmciLinux->context =NULL ; + +/* +* The number of active contexts is used to track whether any +* VMX'en are using the host personality. It is incremented when +* a context is created through the IOCTL_VMCI_INIT_CONTEXT +* ioctl. +*/ + +atomic_dec (&linuxState.activeContexts); + } + vmciLinux->ctType =VMCIOBJ_NOT_SET ; + +kfree (vmciLinux); + filp->private_data =NULL ; +return 0 ; +} + +/* +*---------------------------------------------------------------------- +* +* drv_driver_poll -- +* +* This is used to wake up the VMX when a VMCI call arrives, or +* to wake up select() or poll() at the next clock tick. +* +*---------------------------------------------------------------------- +*/ + +static unsigned int drv_driver_poll (struct file *filp,poll_table * wait) +{ +struct vmci_linux *vmciLinux = (struct vmci_linux *)filp->private_data; +unsigned int mask =0 ; + +if (vmciLinux->ctType ==VMCIOBJ_CONTEXT ) { +ASSERT (vmciLinux->context !=NULL ); +/* +* Check for VMCI calls to this VM context. +*/ + +if (wait !=NULL ) { +poll_wait (filp, + &vmciLinux->context->hostContext.waitQueue, + wait); + } + +spin_lock (&vmciLinux->context->lock); +if (vmciLinux->context->pendingDatagrams >0 || +vmci_handle_arr_get_size (vmciLinux->context-> + pendingDoorbellArray) >0 ) { + mask =POLLIN ; + } +spin_unlock (&vmciLinux->context->lock); + } +return mask; +} + + +/* +*----------------------------------------------------------------------------- +* +* drv_host_init -- +* +* Initializes the VMCI host device driver. +* +* Results: +* 0 on success, other error codes on failure. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +static int __init drv_host_init (void ) +{ +int error; +int result; + + + result =vmci_ctx_init_ctx (VMCI_HOST_CONTEXT_ID , +VMCI_DEFAULT_PROC_PRIVILEGE_FLAGS , +-1 ,VMCI_VERSION ,NULL , &hostContext); +if (result <VMCI_SUCCESS ) { +pr_warn ("Failed to initialize VMCIContext (result=%d)." , + result); +return -ENOMEM ; + } + + result =vmci_qp_broker_init (); +if (result <VMCI_SUCCESS ) { +pr_warn ("Failed to initialize broker (result=%d)." , + result); +vmci_ctx_release_ctx (hostContext); +return -ENOMEM ; + } + + error =misc_register (&linuxState.misc); +if (error) { +pr_warn ("Module registration error " +"(name=%s, major=%d, minor=%d, err=%d)." , + linuxState.misc.name,MISC_MAJOR , linuxState.misc.minor, + error); +drv_host_cleanup (); +return error; + } + +pr_notice ("Module registered (name=%s, major=%d, minor=%d)." , + linuxState.misc.name,MISC_MAJOR , linuxState.misc.minor); + +return 0 ; +} + +/* +*---------------------------------------------------------------------- +* +* drv_cp_harray_to_user -- +* +* Copies the handles of a handle array into a user buffer, and +* returns the new length in userBufferSize. If the copy to the +* user buffer fails, the functions still returns VMCI_SUCCESS, +* but retval != 0. +* +*---------------------------------------------------------------------- +*/ + +static int drv_cp_harray_to_user (void *userBufUVA,// IN +uint64_t * userBufSize,// IN/OUT +struct vmci_handle_arr *handleArray,// IN +int *retval)// IN +{ +uint32_t arraySize =0 ; +struct vmci_handle *handles; + +if (handleArray) + arraySize =vmci_handle_arr_get_size (handleArray); + +if (arraySize *sizeof *handles > *userBufSize) +return VMCI_ERROR_MORE_DATA ; + + *userBufSize = arraySize *sizeof *handles; +if (*userBufSize) + *retval =copy_to_user (userBufUVA, +vmci_handle_arr_get_handles + (handleArray), *userBufSize); + +return VMCI_SUCCESS ; +} + +/* +*----------------------------------------------------------------------------- +* +* drv_qp_broker_alloc -- +* +* Helper function for creating queue pair and copying the result +* to user memory. +* +* Results: +* 0 if result value was copied to user memory, -EFAULT otherwise. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +static int drv_qp_broker_alloc (struct vmci_handle handle, +uint32_t peer, +uint32_t flags, +uint64_t produceSize, +uint64_t consumeSize, +QueuePairPageStore * pageStore, +struct vmci_ctx *context, +bool vmToVm, +void *resultUVA) +{ +uint32_t cid; +int result; +int retval; + + cid =vmci_ctx_get_id (context); + + result = +vmci_qp_broker_alloc (handle, peer, flags, +VMCI_NO_PRIVILEGE_FLAGS , produceSize, + consumeSize, pageStore, context); +if (result ==VMCI_SUCCESS && vmToVm) + result =VMCI_SUCCESS_QUEUEPAIR_CREATE ; + + retval =copy_to_user (resultUVA, &result,sizeof result); +if (retval) { + retval = -EFAULT ; +if (result >=VMCI_SUCCESS ) { + result =vmci_qp_broker_detach (handle, context); +ASSERT (result >=VMCI_SUCCESS ); + } + } + +return retval; +} + +/* +*----------------------------------------------------------------------------- +* +* drv_user_va_lock_page -- +* +* Lock physical page backing a given user VA. Copied from +* bora/modules/vmnet/linux/userif.c:UserIfLockPage(). TODO libify the +* common code. +* +* Results: +* Pointer to struct page on success, NULL otherwise. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +static struct page *drv_user_va_lock_page (uintptr_t addr)// IN: +{ +struct page *page =NULL ; +int retval; + +down_read (¤t->mm->mmap_sem); + retval =get_user_pages (current, current->mm, addr, +1 ,1 ,0 , &page,NULL ); +up_read (¤t->mm->mmap_sem); + +if (retval !=1 ) +return NULL ; + +return page; +} + +/* +*----------------------------------------------------------------------------- +* +* drv_map_bool_ptr -- +* +* Lock physical page backing a given user VA and maps it to kernel +* address space. The range of the mapped memory should be within a +* single page otherwise an error is returned. Copied from +* bora/modules/vmnet/linux/userif.c:VNetUserIfMapUint32Ptr(). TODO +* libify the common code. +* +* Results: +* 0 on success, negative error code otherwise. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +static int drv_map_bool_ptr (uintptr_t notifyUVA,// IN: +struct page **p,// OUT: +bool ** notifyPtr)// OUT: +{ +if (!access_ok (VERIFY_WRITE , notifyUVA,sizeof **notifyPtr) || + (((notifyUVA +sizeof **notifyPtr -1 ) & ~(PAGE_SIZE -1 )) != + (notifyUVA & ~(PAGE_SIZE -1 )))) { +return -EINVAL ; + } + + *p =drv_user_va_lock_page (notifyUVA); +if (*p ==NULL ) +return -EAGAIN ; + + *notifyPtr = + (bool *) ((uint8_t *)kmap (*p) + (notifyUVA & (PAGE_SIZE -1 ))); +return 0 ; +} + +/* +*----------------------------------------------------------------------------- +* +* drv_setup_notify -- +* +* Sets up a given context for notify to work. Calls drv_map_bool_ptr() +* which maps the notify boolean in user VA in kernel space. +* +* Results: +* VMCI_SUCCESS on success, error code otherwise. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +static int drv_setup_notify (struct vmci_ctx *context,// IN: +uintptr_t notifyUVA)// IN: +{ +int retval; + +if (context->notify) { +pr_warn ("Notify mechanism is already set up." ); +return VMCI_ERROR_DUPLICATE_ENTRY ; + } + + retval = +drv_map_bool_ptr (notifyUVA, &context->notifyPage, + &context->notify) == +0 ?VMCI_SUCCESS :VMCI_ERROR_GENERIC ; +if (retval ==VMCI_SUCCESS ) +vmci_ctx_check_signal_notify (context); + +return retval; +} + +/* +*----------------------------------------------------------------------------- +* +* drv_driver_unlocked_ioctl -- +* +* Main path for UserRPC +* +* Results: +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +static long drv_driver_unlocked_ioctl (struct file *filp, +u_int iocmd, +unsigned long ioarg) +{ +struct vmci_linux *vmciLinux = (struct vmci_linux *)filp->private_data; +int retval =0 ; + +switch (iocmd) { +case IOCTL_VMCI_VERSION2 :{ +int verFromUser; + +if (copy_from_user + (&verFromUser, (void *)ioarg,sizeof verFromUser)) { + retval = -EFAULT ; +break ; + } + + vmciLinux->userVersion = verFromUser; + } +/* Fall through. */ +case IOCTL_VMCI_VERSION : +/* +* The basic logic here is: +* +* If the user sends in a version of 0 tell it our version. +* If the user didn't send in a version, tell it our version. +* If the user sent in an old version, tell it -its- version. +* If the user sent in an newer version, tell it our version. +* +* The rationale behind telling the caller its version is that +* Workstation 6.5 required that VMX and VMCI kernel module were +* version sync'd. All new VMX users will be programmed to +* handle the VMCI kernel module version. +*/ + +if (vmciLinux->userVersion >0 && + vmciLinux->userVersion <VMCI_VERSION_HOSTQP ) { + retval = vmciLinux->userVersion; + }else { + retval =VMCI_VERSION ; + } +break ; + +case IOCTL_VMCI_INIT_CONTEXT :{ +struct vmci_init_blk initBlock; +uid_t user; + + retval = +copy_from_user (&initBlock, (void *)ioarg, +sizeof initBlock); +if (retval !=0 ) { +pr_info ("Error reading init block." ); + retval = -EFAULT ; +break ; + } + +mutex_lock (&vmciLinux->lock); +if (vmciLinux->ctType !=VMCIOBJ_NOT_SET ) { +pr_info ("Received VMCI init on initialized handle." ); + retval = -EINVAL ; + goto init_release; + } + +if (initBlock.flags & ~VMCI_PRIVILEGE_FLAG_RESTRICTED ) { +pr_info ("Unsupported VMCI restriction flag." ); + retval = -EINVAL ; + goto init_release; + } + + user =current_uid (); + retval = +vmci_ctx_init_ctx (initBlock.cid, + initBlock.flags, +0 /* Unused */ , + vmciLinux->userVersion, + &user, &vmciLinux->context); +if (retval <VMCI_SUCCESS ) { +pr_info ("Error initializing context." ); + retval = + retval == +VMCI_ERROR_DUPLICATE_ENTRY ? -EEXIST : + -EINVAL ; + goto init_release; + } + +/* +* Copy cid to userlevel, we do this to allow the VMX to enforce its +* policy on cid generation. +*/ + initBlock.cid =vmci_ctx_get_id (vmciLinux->context); + retval = +copy_to_user ((void *)ioarg, &initBlock, +sizeof initBlock); +if (retval !=0 ) { +vmci_ctx_release_ctx (vmciLinux->context); + vmciLinux->context =NULL ; +pr_info ("Error writing init block." ); + retval = -EFAULT ; + goto init_release; + } +ASSERT (initBlock.cid !=VMCI_INVALID_ID ); + + vmciLinux->ctType =VMCIOBJ_CONTEXT ; + +atomic_inc (&linuxState.activeContexts); + + init_release: +mutex_unlock (&vmciLinux->lock); +break ; + } + +case IOCTL_VMCI_DATAGRAM_SEND :{ +struct vmci_dg_snd_rcv_info sendInfo; +struct vmci_dg *dg =NULL ; +uint32_t cid; + +if (vmciLinux->ctType !=VMCIOBJ_CONTEXT ) { +pr_warn ("Ioctl only valid for context handle (iocmd=%d)." , + iocmd); + retval = -EINVAL ; +break ; + } + + retval = +copy_from_user (&sendInfo, (void *)ioarg, +sizeof sendInfo); +if (retval) { +pr_warn ("copy_from_user failed." ); + retval = -EFAULT ; +break ; + } + +if (sendInfo.len >VMCI_MAX_DG_SIZE ) { +pr_warn ("Datagram too big (size=%d)." , + sendInfo.len); + retval = -EINVAL ; +break ; + } + +if (sendInfo.len <sizeof *dg) { +pr_warn ("Datagram too small (size=%d)." , + sendInfo.len); + retval = -EINVAL ; +break ; + } + + dg =kmalloc (sendInfo.len,GFP_KERNEL ); +if (dg ==NULL ) { +pr_info ("Cannot allocate memory to dispatch datagram." ); + retval = -ENOMEM ; +break ; + } + + retval = +copy_from_user (dg, + (char *)(uintptr_t ) sendInfo.addr, + sendInfo.len); +if (retval !=0 ) { +pr_info ("Error getting datagram (err=%d)." , + retval); +kfree (dg); + retval = -EFAULT ; +break ; + } + +pr_devel ("Datagram dst (handle=0x%x:0x%x) src " +"(handle=0x%x:0x%x), payload " +"(size=%llu bytes)." , + dg->dst.context, dg->dst.resource, + dg->src.context, dg->src.resource, + (unsigned long long ) dg->payloadSize); + +/* Get source context id. */ +ASSERT (vmciLinux->context); + cid =vmci_ctx_get_id (vmciLinux->context); +ASSERT (cid !=VMCI_INVALID_ID ); + sendInfo.result =vmci_dg_dispatch (cid, dg, true); +kfree (dg); + retval = +copy_to_user ((void *)ioarg, &sendInfo, +sizeof sendInfo); +break ; + } + +case IOCTL_VMCI_DATAGRAM_RECEIVE :{ +struct vmci_dg_snd_rcv_info recvInfo; +struct vmci_dg *dg =NULL ; +size_t size; + +if (vmciLinux->ctType !=VMCIOBJ_CONTEXT ) { +pr_warn ("Ioctl only valid for context handle (iocmd=%d)." , + iocmd); + retval = -EINVAL ; +break ; + } + + retval = +copy_from_user (&recvInfo, (void *)ioarg, +sizeof recvInfo); +if (retval) { +pr_warn ("copy_from_user failed." ); + retval = -EFAULT ; +break ; + } + +ASSERT (vmciLinux->ctType ==VMCIOBJ_CONTEXT ); + + size = recvInfo.len; +ASSERT (vmciLinux->context); + recvInfo.result = +vmci_ctx_dequeue_dg (vmciLinux->context, + &size, &dg); + +if (recvInfo.result >=VMCI_SUCCESS ) { +ASSERT (dg); + retval =copy_to_user ((void *)((uintptr_t ) + recvInfo.addr), + dg,VMCI_DG_SIZE (dg)); +kfree (dg); +if (retval !=0 ) +break ; + } + retval = +copy_to_user ((void *)ioarg, &recvInfo, +sizeof recvInfo); +break ; + } + +case IOCTL_VMCI_QUEUEPAIR_ALLOC :{ +if (vmciLinux->ctType !=VMCIOBJ_CONTEXT ) { +pr_info ("IOCTL_VMCI_QUEUEPAIR_ALLOC only valid for contexts." ); + retval = -EINVAL ; +break ; + } + +if (vmciLinux->userVersion <VMCI_VERSION_NOVMVM ) { +struct vmci_qp_alloc_info_vmvm queuePairAllocInfo; +struct vmci_qp_alloc_info_vmvm *info = + (struct vmci_qp_alloc_info_vmvm *)ioarg; + + retval = +copy_from_user (&queuePairAllocInfo, + (void *)ioarg, +sizeof queuePairAllocInfo); +if (retval) { + retval = -EFAULT ; +break ; + } + + retval =drv_qp_broker_alloc (queuePairAllocInfo.handle, queuePairAllocInfo.peer, queuePairAllocInfo.flags, queuePairAllocInfo.produceSize, queuePairAllocInfo.consumeSize,NULL , vmciLinux->context, true,// VM to VM style create + &info->result); + }else { +struct vmci_qp_alloc_info + queuePairAllocInfo; +struct vmci_qp_alloc_info *info = + (struct vmci_qp_alloc_info *)ioarg; +QueuePairPageStore pageStore; + + retval = +copy_from_user (&queuePairAllocInfo, + (void *)ioarg, +sizeof queuePairAllocInfo); +if (retval) { + retval = -EFAULT ; +break ; + } + + pageStore.pages = queuePairAllocInfo.ppnVA; + pageStore.len = queuePairAllocInfo.numPPNs; + + retval =drv_qp_broker_alloc ( + queuePairAllocInfo.handle, + queuePairAllocInfo.peer, + queuePairAllocInfo.flags, + queuePairAllocInfo.produceSize, + queuePairAllocInfo.consumeSize, + &pageStore, vmciLinux->context, + false, &info->result); + } +break ; + } + +case IOCTL_VMCI_QUEUEPAIR_SETVA :{ +struct vmci_qp_set_va_info setVAInfo; +struct vmci_qp_set_va_info *info = + (struct vmci_qp_set_va_info *)ioarg; +int32_t result; + +if (vmciLinux->ctType !=VMCIOBJ_CONTEXT ) { +pr_info ("IOCTL_VMCI_QUEUEPAIR_SETVA only valid for contexts." ); + retval = -EINVAL ; +break ; + } + +if (vmciLinux->userVersion <VMCI_VERSION_NOVMVM ) { +pr_info ("IOCTL_VMCI_QUEUEPAIR_SETVA not supported for this VMX version." ); + retval = -EINVAL ; +break ; + } + + retval = +copy_from_user (&setVAInfo, (void *)ioarg, +sizeof setVAInfo); +if (retval) { + retval = -EFAULT ; +break ; + } + +if (setVAInfo.va) { +/* +* VMX is passing down a new VA for the queue pair mapping. +*/ + + result =vmci_qp_broker_map (setVAInfo.handle, + vmciLinux->context, + setVAInfo.va); + }else { +/* +* The queue pair is about to be unmapped by the VMX. +*/ + + result =vmci_qp_broker_unmap (setVAInfo.handle, + vmciLinux->context,0 ); + } + + retval =copy_to_user (&info->result, &result,sizeof result); +if (retval) + retval = -EFAULT ; + +break ; + } + +case IOCTL_VMCI_QUEUEPAIR_SETPAGEFILE :{ +struct vmci_qp_page_file_info pageFileInfo; +struct vmci_qp_page_file_info *info = + (struct vmci_qp_page_file_info *)ioarg; +int32_t result; + +if (vmciLinux->userVersion <VMCI_VERSION_HOSTQP || + vmciLinux->userVersion >=VMCI_VERSION_NOVMVM ) { +pr_info ("IOCTL_VMCI_QUEUEPAIR_SETPAGEFILE not supported this VMX " +"(version=%d)." , vmciLinux->userVersion); + retval = -EINVAL ; +break ; + } + +if (vmciLinux->ctType !=VMCIOBJ_CONTEXT ) { +pr_info ("IOCTL_VMCI_QUEUEPAIR_SETPAGEFILE only valid for contexts." ); + retval = -EINVAL ; +break ; + } + + retval = +copy_from_user (&pageFileInfo, (void *)ioarg, +sizeof *info); +if (retval) { + retval = -EFAULT ; +break ; + } + +/* +* Communicate success pre-emptively to the caller. Note that +* the basic premise is that it is incumbent upon the caller not +* to look at the info.result field until after the ioctl() +* returns. And then, only if the ioctl() result indicates no +* error. We send up the SUCCESS status before calling +* SetPageStore() store because failing to copy up the result +* code means unwinding the SetPageStore(). +* +* It turns out the logic to unwind a SetPageStore() opens a can +* of worms. For example, if a host had created the QueuePair +* and a guest attaches and SetPageStore() is successful but +* writing success fails, then ... the host has to be stopped +* from writing (anymore) data into the QueuePair. That means +* an additional test in the VMCI_Enqueue() code path. Ugh. +*/ + + result =VMCI_SUCCESS ; + retval = +copy_to_user (&info->result, &result,sizeof result); +if (retval ==0 ) { + result = +vmci_qp_broker_set_page_store + (pageFileInfo.handle, + pageFileInfo.produceVA, + pageFileInfo.consumeVA, + vmciLinux->context); +if (result <VMCI_SUCCESS ) { + + retval = +copy_to_user (&info->result, + &result, +sizeof result); +if (retval !=0 ) { +/* +* Note that in this case the SetPageStore() call +* failed but we were unable to communicate that to the +* caller (because the copy_to_user() call failed). +* So, if we simply return an error (in this case +* -EFAULT) then the caller will know that the +* SetPageStore failed even though we couldn't put the +* result code in the result field and indicate exactly +* why it failed. +* +* That says nothing about the issue where we were once +* able to write to the caller's info memory and now +* can't. Something more serious is probably going on +* than the fact that SetPageStore() didn't work. +*/ + retval = -EFAULT ; + } + } + + }else { +/* +* In this case, we can't write a result field of the +* caller's info block. So, we don't even try to +* SetPageStore(). +*/ + retval = -EFAULT ; + } + +break ; + } + +case IOCTL_VMCI_QUEUEPAIR_DETACH :{ +struct vmci_qp_dtch_info detachInfo; +struct vmci_qp_dtch_info *info = + (struct vmci_qp_dtch_info *)ioarg; +int32_t result; + +if (vmciLinux->ctType !=VMCIOBJ_CONTEXT ) { +pr_info ("IOCTL_VMCI_QUEUEPAIR_DETACH only valid for contexts." ); + retval = -EINVAL ; +break ; + } + + retval = +copy_from_user (&detachInfo, (void *)ioarg, +sizeof detachInfo); +if (retval) { + retval = -EFAULT ; +break ; + } + + result = +vmci_qp_broker_detach (detachInfo.handle, + vmciLinux->context); +if (result ==VMCI_SUCCESS + && vmciLinux->userVersion <VMCI_VERSION_NOVMVM ) { + result =VMCI_SUCCESS_LAST_DETACH ; + } + + retval = +copy_to_user (&info->result, &result,sizeof result); +if (retval) { + retval = -EFAULT ; + } + +break ; + } + +case IOCTL_VMCI_CTX_ADD_NOTIFICATION :{ +struct vmci_ctx_info arInfo; +struct vmci_ctx_info *info = + (struct vmci_ctx_info *)ioarg; +int32_t result; +uint32_t cid; + +if (vmciLinux->ctType !=VMCIOBJ_CONTEXT ) { +pr_info ( +"IOCTL_VMCI_CTX_ADD_NOTIFICATION only valid for contexts." ); + retval = -EINVAL ; +break ; + } + + retval = +copy_from_user (&arInfo, (void *)ioarg, +sizeof arInfo); +if (retval) { + retval = -EFAULT ; +break ; + } + + cid =vmci_ctx_get_id (vmciLinux->context); + result = +vmci_ctx_add_notification (cid, arInfo.remoteCID); + retval = +copy_to_user (&info->result, &result,sizeof result); +if (retval) { + retval = -EFAULT ; +break ; + } +break ; + } + +case IOCTL_VMCI_CTX_REMOVE_NOTIFICATION :{ +struct vmci_ctx_info arInfo; +struct vmci_ctx_info *info = + (struct vmci_ctx_info *)ioarg; +int32_t result; +uint32_t cid; + +if (vmciLinux->ctType !=VMCIOBJ_CONTEXT ) { +pr_info ("IOCTL_VMCI_CTX_REMOVE_NOTIFICATION only valid for " +"contexts." ); + retval = -EINVAL ; +break ; + } + + retval = +copy_from_user (&arInfo, (void *)ioarg, +sizeof arInfo); +if (retval) { + retval = -EFAULT ; +break ; + } + + cid =vmci_ctx_get_id (vmciLinux->context); + result = +vmci_ctx_remove_notification (cid, + arInfo.remoteCID); + retval = +copy_to_user (&info->result, &result,sizeof result); +if (retval) { + retval = -EFAULT ; +break ; + } +break ; + } + +case IOCTL_VMCI_CTX_GET_CPT_STATE :{ +struct vmci_ctx_chkpt_buf_info getInfo; +uint32_t cid; +char *cptBuf; + +if (vmciLinux->ctType !=VMCIOBJ_CONTEXT ) { +pr_info ("IOCTL_VMCI_CTX_GET_CPT_STATE only valid for contexts." ); + retval = -EINVAL ; +break ; + } + + retval = +copy_from_user (&getInfo, (void *)ioarg, +sizeof getInfo); +if (retval) { + retval = -EFAULT ; +break ; + } + + cid =vmci_ctx_get_id (vmciLinux->context); + getInfo.result = +vmci_ctx_get_chkpt_state (cid, + getInfo.cptType, + &getInfo.bufSize, + &cptBuf); +if (getInfo.result ==VMCI_SUCCESS && getInfo.bufSize) { + retval =copy_to_user ((void *)(uintptr_t ) + getInfo.cptBuf, cptBuf, + getInfo.bufSize); +kfree (cptBuf); +if (retval) { + retval = -EFAULT ; +break ; + } + } + retval = +copy_to_user ((void *)ioarg, &getInfo, +sizeof getInfo); +if (retval) { + retval = -EFAULT ; +break ; + } +break ; + } + +case IOCTL_VMCI_CTX_SET_CPT_STATE :{ +struct vmci_ctx_chkpt_buf_info setInfo; +uint32_t cid; +char *cptBuf; + +if (vmciLinux->ctType !=VMCIOBJ_CONTEXT ) { +pr_info ("IOCTL_VMCI_CTX_SET_CPT_STATE only valid for contexts." ); + retval = -EINVAL ; +break ; + } + + retval = +copy_from_user (&setInfo, (void *)ioarg, +sizeof setInfo); +if (retval) { + retval = -EFAULT ; +break ; + } + + cptBuf =kmalloc (setInfo.bufSize,GFP_KERNEL ); +if (cptBuf ==NULL ) { +pr_info ("Cannot allocate memory to set cpt state (type=%d)." , + setInfo.cptType); + retval = -ENOMEM ; +break ; + } + retval = +copy_from_user (cptBuf, + (void *)(uintptr_t ) setInfo.cptBuf, + setInfo.bufSize); +if (retval) { +kfree (cptBuf); + retval = -EFAULT ; +break ; + } + + cid =vmci_ctx_get_id (vmciLinux->context); + setInfo.result = +vmci_ctx_set_chkpt_state (cid, + setInfo.cptType, + setInfo.bufSize, + cptBuf); +kfree (cptBuf); + retval = +copy_to_user ((void *)ioarg, &setInfo, +sizeof setInfo); +if (retval) { + retval = -EFAULT ; +break ; + } +break ; + } + +case IOCTL_VMCI_GET_CONTEXT_ID :{ +uint32_t cid =VMCI_HOST_CONTEXT_ID ; + + retval =copy_to_user ((void *)ioarg, &cid,sizeof cid); +break ; + } + +case IOCTL_VMCI_SET_NOTIFY :{ +struct vmci_set_notify_info notifyInfo; + +if (vmciLinux->ctType !=VMCIOBJ_CONTEXT ) { +pr_info ("IOCTL_VMCI_SET_NOTIFY only valid for contexts." ); + retval = -EINVAL ; +break ; + } + + retval = +copy_from_user (¬ifyInfo, (void *)ioarg, +sizeof notifyInfo); +if (retval) { + retval = -EFAULT ; +break ; + } + +if ((uintptr_t ) notifyInfo.notifyUVA != + (uintptr_t )NULL ) { + notifyInfo.result = +drv_setup_notify (vmciLinux->context, + (uintptr_t ) + notifyInfo.notifyUVA); + }else { +spin_lock (&vmciLinux->context->lock); +vmci_ctx_unset_notify (vmciLinux->context); +spin_unlock (&vmciLinux->context->lock); + notifyInfo.result =VMCI_SUCCESS ; + } + + retval = +copy_to_user ((void *)ioarg, ¬ifyInfo, +sizeof notifyInfo); +if (retval) { + retval = -EFAULT ; +break ; + } + +break ; + } + +case IOCTL_VMCI_NOTIFY_RESOURCE :{ +struct vmci_dbell_notify_resource_info info; +uint32_t cid; + +if (vmciLinux->userVersion <VMCI_VERSION_NOTIFY ) { +pr_info ("IOCTL_VMCI_NOTIFY_RESOURCE is invalid for current" +" VMX versions." ); + retval = -EINVAL ; +break ; + } + +if (vmciLinux->ctType !=VMCIOBJ_CONTEXT ) { +pr_info ("IOCTL_VMCI_NOTIFY_RESOURCE is only valid for contexts." ); + retval = -EINVAL ; +break ; + } + + retval = +copy_from_user (&info, (void *)ioarg,sizeof info); +if (retval) { + retval = -EFAULT ; +break ; + } + + cid =vmci_ctx_get_id (vmciLinux->context); +switch (info.action) { +case VMCI_NOTIFY_RESOURCE_ACTION_NOTIFY : +if (info.resource == +VMCI_NOTIFY_RESOURCE_DOOR_BELL ) { + info.result = +vmci_ctx_notify_dbell (cid, + info. + handle, +VMCI_NO_PRIVILEGE_FLAGS ); + }else { + info.result =VMCI_ERROR_UNAVAILABLE ; + } +break ; +case VMCI_NOTIFY_RESOURCE_ACTION_CREATE : + info.result = +vmci_ctx_dbell_create (cid, + info.handle); +break ; +case VMCI_NOTIFY_RESOURCE_ACTION_DESTROY : + info.result = +vmci_ctx_dbell_destroy (cid, + info.handle); +break ; +default : +pr_info ("IOCTL_VMCI_NOTIFY_RESOURCE got unknown action (action=%d)." , + info.action); + info.result =VMCI_ERROR_INVALID_ARGS ; + } + retval =copy_to_user ((void *)ioarg, &info, +sizeof info); +if (retval) { + retval = -EFAULT ; +break ; + } + +break ; + } + +case IOCTL_VMCI_NOTIFICATIONS_RECEIVE :{ +struct vmci_ctx_notify_recv_info info; +struct vmci_handle_arr *dbHandleArray; +struct vmci_handle_arr *qpHandleArray; +uint32_t cid; + +if (vmciLinux->ctType !=VMCIOBJ_CONTEXT ) { +pr_info ("IOCTL_VMCI_NOTIFICATIONS_RECEIVE is only valid for contexts." ); + retval = -EINVAL ; +break ; + } + +if (vmciLinux->userVersion <VMCI_VERSION_NOTIFY ) { +pr_info ("IOCTL_VMCI_NOTIFICATIONS_RECEIVE is not supported for the " +"current vmx version." ); + retval = -EINVAL ; +break ; + } + + retval = +copy_from_user (&info, (void *)ioarg,sizeof info); +if (retval) { + retval = -EFAULT ; +break ; + } + +if ((info.dbHandleBufSize && !info.dbHandleBufUVA) + || (info.qpHandleBufSize && !info.qpHandleBufUVA)) { + retval = -EINVAL ; +break ; + } + + cid =vmci_ctx_get_id (vmciLinux->context); + info.result = +vmci_ctx_rcv_notifications_get (cid, + &dbHandleArray, + &qpHandleArray); +if (info.result ==VMCI_SUCCESS ) { + info.result =drv_cp_harray_to_user ((void *) + (uintptr_t ) + info. + dbHandleBufUVA, + &info. + dbHandleBufSize, + dbHandleArray, + &retval); +if (info.result ==VMCI_SUCCESS && !retval) { + info.result = +drv_cp_harray_to_user ((void *) + (uintptr_t ) + info. + qpHandleBufUVA, + &info. + qpHandleBufSize, + qpHandleArray, + &retval); + } +if (!retval) { + retval = +copy_to_user ((void *)ioarg, + &info,sizeof info); + } +vmci_ctx_rcv_notifications_release + (cid, dbHandleArray, qpHandleArray, + info.result ==VMCI_SUCCESS && !retval); + }else { + retval = +copy_to_user ((void *)ioarg, &info, +sizeof info); + } +break ; + } + +default : +pr_warn ("Unknown ioctl (iocmd=%d)." , iocmd); + retval = -EINVAL ; + } + +return retval; +} + +/* +*----------------------------------------------------------------------------- +* +* PCI device support -- +* +* The following functions implement the support for the VMCI +* guest device. This includes initializing the device and +* interrupt handling. +* +*----------------------------------------------------------------------------- +*/ + +/* +*----------------------------------------------------------------------------- +* +* drv_dispatch_dgs -- +* +* Reads and dispatches incoming datagrams. +* +* Results: +* None. +* +* Side effects: +* Reads data from the device. +* +*----------------------------------------------------------------------------- +*/ + +static void drv_dispatch_dgs (unsigned long data) +{ +struct vmci_device *dev = (struct vmci_device *)data; + +if (dev ==NULL ) { +pr_devel ("No virtual device present in %s." , __func__); +return ; + } + +if (data_buffer ==NULL ) { +pr_devel ("No buffer present in %s." , __func__); +return ; + } + +drv_read_dgs_from_port ((int )0 , + dev->ioaddr +VMCI_DATA_IN_ADDR , + data_buffer, data_buffer_size); +} + +DECLARE_TASKLET (vmci_dg_tasklet, drv_dispatch_dgs, (unsigned long )&vmci_dev); + +/* +*----------------------------------------------------------------------------- +* +* drv_process_bitmap -- +* +* Scans the notification bitmap for raised flags, clears them +* and handles the notifications. +* +* Results: +* None. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +static void drv_process_bitmap (unsigned long data) +{ +struct vmci_device *dev = (struct vmci_device *)data; + +if (dev ==NULL ) { +pr_devel ("No virtual device present in %s." , __func__); +return ; + } + +if (notification_bitmap ==NULL ) { +pr_devel ("No bitmap present in %s." , __func__); +return ; + } + +vmci_dbell_scan_notification_entries (notification_bitmap); +} + +DECLARE_TASKLET (vmci_bm_tasklet, drv_process_bitmap, (unsigned long )&vmci_dev); + +/* +*----------------------------------------------------------------------------- +* +* drv_enable_msix -- +* +* Enable MSI-X. Try exclusive vectors first, then shared vectors. +* +* Results: +* 0 on success, other error codes on failure. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +static int drv_enable_msix (struct pci_dev *pdev)// IN +{ +int i; +int result; + +for (i =0 ; i <VMCI_MAX_INTRS ; ++i) { + vmci_dev.msix_entries[i].entry = i; + vmci_dev.msix_entries[i].vector = i; + } + + result =pci_enable_msix (pdev, vmci_dev.msix_entries,VMCI_MAX_INTRS ); +if (result ==0 ) + vmci_dev.exclusive_vectors = true; +else if (result >0 ) + result =pci_enable_msix (pdev, vmci_dev.msix_entries,1 ); + +return result; +} + +/* +*----------------------------------------------------------------------------- +* +* drv_interrupt -- +* +* Interrupt handler for legacy or MSI interrupt, or for first MSI-X +* interrupt (vector VMCI_INTR_DATAGRAM). +* +* Results: +* COMPAT_IRQ_HANDLED if the interrupt is handled, COMPAT_IRQ_NONE if +* not an interrupt. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +static irqreturn_t drv_interrupt (int irq,// IN +void *clientdata)// IN +{ +struct vmci_device *dev = clientdata; + +if (dev ==NULL ) { +pr_devel ("Irq %d for unknown device in %s." , irq, __func__); +return IRQ_NONE ; + } + +/* +* If we are using MSI-X with exclusive vectors then we simply schedule +* the datagram tasklet, since we know the interrupt was meant for us. +* Otherwise we must read the ICR to determine what to do. +*/ + +if (dev->intr_type ==VMCI_INTR_TYPE_MSIX && dev->exclusive_vectors) { +tasklet_schedule (&vmci_dg_tasklet); + }else { +unsigned int icr; + +ASSERT (dev->intr_type ==VMCI_INTR_TYPE_INTX || + dev->intr_type ==VMCI_INTR_TYPE_MSI ); + +/* Acknowledge interrupt and determine what needs doing. */ + icr =inl (dev->ioaddr +VMCI_ICR_ADDR ); +if (icr ==0 || icr == ~0 ) +return IRQ_NONE ; + +if (icr &VMCI_ICR_DATAGRAM ) { +tasklet_schedule (&vmci_dg_tasklet); + icr &= ~VMCI_ICR_DATAGRAM ; + } + +if (icr &VMCI_ICR_NOTIFICATION ) { +tasklet_schedule (&vmci_bm_tasklet); + icr &= ~VMCI_ICR_NOTIFICATION ; + } + +if (icr !=0 ) +pr_info ("Ignoring unknown interrupt cause (%d)." , icr); + } + +return IRQ_HANDLED ; +} + +/* +*----------------------------------------------------------------------------- +* +* drv_interrupt_bm -- +* +* Interrupt handler for MSI-X interrupt vector VMCI_INTR_NOTIFICATION, +* which is for the notification bitmap. Will only get called if we are +* using MSI-X with exclusive vectors. +* +* Results: +* COMPAT_IRQ_HANDLED if the interrupt is handled, COMPAT_IRQ_NONE if +* not an interrupt. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +static irqreturn_t drv_interrupt_bm (int irq,// IN +void *clientdata)// IN +{ +struct vmci_device *dev = clientdata; + +if (dev ==NULL ) { +pr_devel ("Irq %d for unknown device in %s." , irq, __func__); +return IRQ_NONE ; + } + +/* For MSI-X we can just assume it was meant for us. */ +ASSERT (dev->intr_type ==VMCI_INTR_TYPE_MSIX && dev->exclusive_vectors); +tasklet_schedule (&vmci_bm_tasklet); + +return IRQ_HANDLED ; +} + +/* +*----------------------------------------------------------------------------- +* +* drv_probe_device -- +* +* Most of the initialization at module load time is done here. +* +* Results: +* Returns 0 for success, an error otherwise. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +static int __devinit drv_probe_device (struct pci_dev *pdev,// IN: vmci PCI device +const struct pci_device_id *id)// IN: matching device ID +{ +unsigned int ioaddr; +unsigned int ioaddr_size; +unsigned int capabilities; +int result; + +pr_info ("Probing for vmci/PCI." ); + + result =pci_enable_device (pdev); +if (result) { +printk (KERN_ERR "Cannot enable VMCI device %s: error %d" , +pci_name (pdev), result); +return result; + } +pci_set_master (pdev);/* To enable QueuePair functionality. */ + ioaddr =pci_resource_start (pdev,0 ); + ioaddr_size =pci_resource_len (pdev,0 ); + +/* +* Request I/O region with adjusted base address and size. The adjusted +* values are needed and used if we release the region in case of failure. +*/ + +if (!request_region (ioaddr, ioaddr_size,MODULE_NAME )) { +pr_info (MODULE_NAME ": Another driver already loaded " +"for device in slot %s." ,pci_name (pdev)); + goto pci_disable; + } + +pr_info ("Found VMCI PCI device at %#x, irq %u." , ioaddr, pdev->irq); + +/* +* Verify that the VMCI Device supports the capabilities that +* we need. If the device is missing capabilities that we would +* like to use, check for fallback capabilities and use those +* instead (so we can run a new VM on old hosts). Fail the load if +* a required capability is missing and there is no fallback. +* +* Right now, we need datagrams. There are no fallbacks. +*/ + capabilities =inl (ioaddr +VMCI_CAPS_ADDR ); + +if ((capabilities &VMCI_CAPS_DATAGRAM ) ==0 ) { +pr_err ("Device does not support datagrams." ); + goto release; + } + +/* +* If the hardware supports notifications, we will use that as +* well. +*/ +if (capabilities &VMCI_CAPS_NOTIFICATIONS ) { + capabilities =VMCI_CAPS_DATAGRAM ; + notification_bitmap =vmalloc (PAGE_SIZE ); +if (notification_bitmap ==NULL ) { +pr_err ("Device unable to allocate notification bitmap." ); + }else { +memset (notification_bitmap,0 ,PAGE_SIZE ); + capabilities |=VMCI_CAPS_NOTIFICATIONS ; + } + }else { + capabilities =VMCI_CAPS_DATAGRAM ; + } +pr_info ("Using capabilities 0x%x." , capabilities); + +/* Let the host know which capabilities we intend to use. */ +outl (capabilities, ioaddr +VMCI_CAPS_ADDR ); + +/* Device struct initialization. */ +mutex_lock (&vmci_dev.lock); +if (vmci_dev.enabled) { +pr_err ("Device already enabled." ); + goto unlock; + } + + vmci_dev.ioaddr = ioaddr; + vmci_dev.ioaddr_size = ioaddr_size; +atomic_set (&vmci_dev.datagrams_allowed,1 ); + +/* +* Register notification bitmap with device if that capability is +* used +*/ +if (capabilities &VMCI_CAPS_NOTIFICATIONS ) { +unsigned long bitmapPPN; + bitmapPPN =page_to_pfn (vmalloc_to_page (notification_bitmap)); +if (!vmci_dbell_register_notification_bitmap (bitmapPPN)) { +pr_err ("VMCI device unable to register notification bitmap " +"with PPN 0x%x." , (uint32_t ) bitmapPPN); + goto datagram_disallow; + } + } + +/* Check host capabilities. */ +if (!drv_check_host_caps ()) { + goto remove_bitmap; + } + +/* Enable device. */ + vmci_dev.enabled = true; +pci_set_drvdata (pdev, &vmci_dev); + +/* +* We do global initialization here because we need datagrams +* during drv_util_init, since it registers for VMCI events. If we +* ever support more than one VMCI device we will have to create +* seperate LateInit/EarlyExit functions that can be used to do +* initialization/cleanup that depends on the device being +* accessible. We need to initialize VMCI components before +* requesting an irq - the VMCI interrupt handler uses these +* components, and it may be invoked once request_irq() has +* registered the handler (as the irq line may be shared). +*/ +drv_util_init (); + +if (vmci_qp_guest_endpoints_init () <VMCI_SUCCESS ) { + goto util_exit; + } + +/* +* Enable interrupts. Try MSI-X first, then MSI, and then fallback on +* legacy interrupts. +*/ +if (!vmci_disable_msix && !drv_enable_msix (pdev)) { + vmci_dev.intr_type =VMCI_INTR_TYPE_MSIX ; + vmci_dev.irq = vmci_dev.msix_entries[0 ].vector; + }else if (!vmci_disable_msi && !pci_enable_msi (pdev)) { + vmci_dev.intr_type =VMCI_INTR_TYPE_MSI ; + vmci_dev.irq = pdev->irq; + }else { + vmci_dev.intr_type =VMCI_INTR_TYPE_INTX ; + vmci_dev.irq = pdev->irq; + } + +/* Request IRQ for legacy or MSI interrupts, or for first MSI-X vector. */ + result =request_irq (vmci_dev.irq, drv_interrupt,IRQF_SHARED , +MODULE_NAME , &vmci_dev); +if (result) { +pr_err ("Irq %u in use: %d" , vmci_dev.irq, result); + goto components_exit; + } + +/* +* For MSI-X with exclusive vectors we need to request an interrupt for each +* vector so that we get a separate interrupt handler routine. This allows +* us to distinguish between the vectors. +*/ + +if (vmci_dev.exclusive_vectors) { +ASSERT (vmci_dev.intr_type ==VMCI_INTR_TYPE_MSIX ); + result =request_irq (vmci_dev.msix_entries[1 ].vector, + drv_interrupt_bm,0 ,MODULE_NAME , + &vmci_dev); +if (result) { +pr_err ("Irq %u in use: %d" , + vmci_dev.msix_entries[1 ].vector, result); +free_irq (vmci_dev.irq, &vmci_dev); + goto components_exit; + } + } + +pr_info ("Registered device." ); +atomic_inc (&guestDeviceActive); +mutex_unlock (&vmci_dev.lock); + +/* Enable specific interrupt bits. */ +if (capabilities &VMCI_CAPS_NOTIFICATIONS ) { +outl (VMCI_IMR_DATAGRAM |VMCI_IMR_NOTIFICATION , + vmci_dev.ioaddr +VMCI_IMR_ADDR ); + }else { +outl (VMCI_IMR_DATAGRAM , vmci_dev.ioaddr +VMCI_IMR_ADDR ); + } + +/* Enable interrupts. */ +outl (VMCI_CONTROL_INT_ENABLE , vmci_dev.ioaddr +VMCI_CONTROL_ADDR ); + +return 0 ; + +components_exit: +vmci_qp_guest_endpoints_exit (); +util_exit: +vmci_util_exit (); + vmci_dev.enabled = false; +if (vmci_dev.intr_type ==VMCI_INTR_TYPE_MSIX ) +pci_disable_msix (pdev); +else if (vmci_dev.intr_type ==VMCI_INTR_TYPE_MSI ) +pci_disable_msi (pdev); + +remove_bitmap: +if (notification_bitmap) +outl (VMCI_CONTROL_RESET , vmci_dev.ioaddr +VMCI_CONTROL_ADDR ); + +datagram_disallow: +atomic_set (&vmci_dev.datagrams_allowed,0 ); +unlock: +mutex_unlock (&vmci_dev.lock); +release: +if (notification_bitmap) { +vfree (notification_bitmap); + notification_bitmap =NULL ; + } +release_region (ioaddr, ioaddr_size); +pci_disable: +pci_disable_device (pdev); +return -EBUSY ; +} + +/* +*----------------------------------------------------------------------------- +* +* drv_remove_device -- +* +* Cleanup, called for each device on unload. +* +* Results: +* None. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +static void __devexit drv_remove_device(struct pci_dev *pdev) +{ +struct vmci_device *dev =pci_get_drvdata (pdev); + +pr_info ("Removing device" ); +atomic_dec (&guestDeviceActive); +vmci_qp_guest_endpoints_exit (); +vmci_util_exit (); +mutex_lock (&dev->lock); +atomic_set (&vmci_dev.datagrams_allowed,0 ); +pr_info ("Resetting vmci device" ); +outl (VMCI_CONTROL_RESET , vmci_dev.ioaddr +VMCI_CONTROL_ADDR ); + +/* +* Free IRQ and then disable MSI/MSI-X as appropriate. For MSI-X, we might +* have multiple vectors, each with their own IRQ, which we must free too. +*/ +free_irq (dev->irq, dev); +if (dev->intr_type ==VMCI_INTR_TYPE_MSIX ) { +if (dev->exclusive_vectors) +free_irq (dev->msix_entries[1 ].vector, dev); + +pci_disable_msix (pdev); + }else if (dev->intr_type ==VMCI_INTR_TYPE_MSI ) { +pci_disable_msi (pdev); + } + dev->exclusive_vectors = false; + dev->intr_type =VMCI_INTR_TYPE_INTX ; + +release_region (dev->ioaddr, dev->ioaddr_size); + dev->enabled = false; +if (notification_bitmap) { +/* +* The device reset above cleared the bitmap state of the +* device, so we can safely free it here. +*/ + +vfree (notification_bitmap); + notification_bitmap =NULL ; + } + +pr_info ("Unregistered device." ); +mutex_unlock (&dev->lock); + +pci_disable_device (pdev); +} + +static struct pci_driver vmci_driver = { + .name =MODULE_NAME , + .id_table = vmci_ids, + .probe = drv_probe_device, + .remove =__devexit_p (drv_remove_device), +}; + +/* +*----------------------------------------------------------------------------- +* +* dev_guest_init -- +* +* Initializes the VMCI PCI device. The initialization might fail +* if there is no VMCI PCI device. +* +* Results: +* 0 on success, other error codes on failure. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +static int __init dev_guest_init (void ) +{ +int retval; + +/* Initialize guest device data. */ +mutex_init (&vmci_dev.lock); + vmci_dev.intr_type =VMCI_INTR_TYPE_INTX ; + vmci_dev.exclusive_vectors = false; +spin_lock_init (&vmci_dev.dev_spinlock); + vmci_dev.enabled = false; +atomic_set (&vmci_dev.datagrams_allowed,0 ); +atomic_set (&guestDeviceActive,0 ); + + data_buffer =vmalloc (data_buffer_size); +if (!data_buffer) +return -ENOMEM ; + +/* This should be last to make sure we are done initializing. */ + retval =pci_register_driver (&vmci_driver); +if (retval <0 ) { +vfree (data_buffer); + data_buffer =NULL ; +return retval; + } + +return 0 ; +} + +static const struct file_operations vmuser_fops = { + .owner =THIS_MODULE , + .open = drv_driver_open, + .release = drv_driver_close, + .poll = drv_driver_poll, + .unlocked_ioctl = drv_driver_unlocked_ioctl, + .compat_ioctl = drv_driver_unlocked_ioctl, +}; + + +/* +*----------------------------------------------------------------------------- +* +* vmci_send_dg -- +* +* VM to hypervisor call mechanism. We use the standard VMware naming +* convention since shared code is calling this function as well. +* +* Results: +* The result of the hypercall. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +int vmci_send_dg (struct vmci_dg *dg) +{ +unsigned long flags; +int result; + +/* Check args. */ +if (dg ==NULL ) +return VMCI_ERROR_INVALID_ARGS ; + +if (atomic_read (&vmci_dev.datagrams_allowed) ==0 ) +return VMCI_ERROR_UNAVAILABLE ; + +/* +* Need to acquire spinlock on the device because +* the datagram data may be spread over multiple pages and the monitor may +* interleave device user rpc calls from multiple VCPUs. Acquiring the +* spinlock precludes that possibility. Disabling interrupts to avoid +* incoming datagrams during a "rep out" and possibly landing up in this +* function. +*/ +spin_lock_irqsave (&vmci_dev.dev_spinlock, flags); + +/* +* Send the datagram and retrieve the return value from the result register. +*/ + __asm__ __volatile__("cld\n\t" "rep outsb\n\t" :/* No output. */ + :"d" (vmci_dev.ioaddr +VMCI_DATA_OUT_ADDR ), +"c" (VMCI_DG_SIZE (dg)),"S" (dg) + ); + +/* +* XXX Should read result high port as well when updating handlers to +* return 64bit. +*/ + result =inl (vmci_dev.ioaddr +VMCI_RESULT_LOW_ADDR ); +spin_unlock_irqrestore (&vmci_dev.dev_spinlock, flags); + +return result; +} + +/* +*----------------------------------------------------------------------------- +* +* vmci_guest_code_active -- +* +* Determines whether the VMCI PCI device has been successfully +* initialized. +* +* Results: +* true, if VMCI guest device is operational, false otherwise. +* +* Side effects: +* Reads data from the device. +* +*----------------------------------------------------------------------------- +*/ + +bool vmci_guest_code_active (void ) +{ +return guestDeviceInit &&atomic_read (&guestDeviceActive) >0 ; +} + +/* +*----------------------------------------------------------------------------- +* +* vmci_host_code_active -- +* +* Determines whether the VMCI host personality is +* available. Since the core functionality of the host driver is +* always present, all guests could possibly use the host +* personality. However, to minimize the deviation from the +* pre-unified driver state of affairs, we only consider the host +* device active, if there is no active guest device, or if there +* are VMX'en with active VMCI contexts using the host device. +* +* Results: +* true, if VMCI host driver is operational, false otherwise. +* +* Side effects: +* Reads data from the device. +* +*----------------------------------------------------------------------------- +*/ + +bool vmci_host_code_active (void ) +{ +return hostDeviceInit && + (!vmci_guest_code_active () || +atomic_read (&linuxState.activeContexts) >0 ); +} + +/* +*---------------------------------------------------------------------- +* +* drv_init -- +* +* linux module entry point. Called by /sbin/insmod command +* +* Results: +* registers a device driver for a major # that depends +* on the uid. Add yourself to that list. List is now in +* private/driver-private.c. +* +*---------------------------------------------------------------------- +*/ + +static int __init drv_init (void ) +{ +int retval; + + retval =drv_shared_init (); +if (retval !=VMCI_SUCCESS ) { +pr_warn ("Failed to initialize common " +"components (err=%d)." , retval); +return -ENOMEM ; + } + +if (!vmci_disable_guest) { + retval =dev_guest_init (); +if (retval !=0 ) { +pr_warn ("Failed to initialize guest " +"personality (err=%d)." , retval); + }else { +const char *state =vmci_guest_code_active ()? +"active" :"inactive" ; + guestDeviceInit = true; +pr_info ("Guest personality initialized and is " +"%s." , state); + } + } + +if (!vmci_disable_host) { + retval =drv_host_init (); +if (retval !=0 ) { +pr_warn ("Unable to initialize host " +"personality (err=%d)." , retval); + }else { + hostDeviceInit = true; +pr_info ("Initialized host personality" ); + } + } + +if (!guestDeviceInit && !hostDeviceInit) { +drv_shared_cleanup (); +return -ENODEV ; + } + +pr_info ("Module is initialized" ); +return 0 ; +} + +/* +*---------------------------------------------------------------------- +* +* drv_exit -- +* +* Called by /sbin/rmmod +* +* +*---------------------------------------------------------------------- +*/ + +static void __exit drv_exit(void) +{ +if (guestDeviceInit) { +pci_unregister_driver (&vmci_driver); +vfree (data_buffer); + guestDeviceInit = false; + } + +if (hostDeviceInit) { +drv_host_cleanup (); + +if (misc_deregister (&linuxState.misc)) +pr_warn ("Error unregistering" ); +else +pr_info ("Module unloaded" ); + + hostDeviceInit = false; + } + +drv_shared_cleanup (); +} + +module_init (drv_init); +module_exit (drv_exit); +MODULE_DEVICE_TABLE (pci, vmci_ids); + +module_param_named (disable_host, vmci_disable_host, bool,0 ); +MODULE_PARM_DESC (disable_host,"Disable driver host personality - (default=0)" ); + +module_param_named (disable_guest, vmci_disable_guest, bool,0 ); +MODULE_PARM_DESC (disable_guest, +"Disable driver guest personality - (default=0)" ); + +module_param_named (disable_msi, vmci_disable_msi, bool,0 ); +MODULE_PARM_DESC (disable_msi,"Disable MSI use in driver - (default=0)" ); + +module_param_named (disable_msix, vmci_disable_msix, bool,0 ); +MODULE_PARM_DESC (disable_msix,"Disable MSI-X use in driver - (default=0)" ); + +MODULE_AUTHOR ("VMware, Inc." ); +MODULE_DESCRIPTION ("VMware Virtual Machine Communication Interface." ); +MODULE_VERSION (VMCI_DRIVER_VERSION_STRING ); +MODULE_LICENSE ("GPL v2" ); + +/* +* Starting with SLE10sp2, Novell requires that IHVs sign a support agreement +* with them and mark their kernel modules as externally supported via a +* change to the module header. If this isn't done, the module will not load +* by default (i.e., neither mkinitrd nor modprobe will accept it). +*/ +MODULE_INFO (supported,"external" );
drivers/misc/vmw_vmci/vmci_driver.h new file +52 -0
52 lines not on this page · raw message
Signature · 1 line
1.7.0.4
RFC 5/11 Apply VMCI event code
In reply to Andrew Stiegmann (stieg)Code that manages event handlers and handles callbacks when specific events fire.
--- drivers/misc/vmw_vmci/vmci_event.c | 614 ++++++++++++++++++++++++++++++++++++ drivers/misc/vmw_vmci/vmci_event.h | 29 ++ 2 files changed, 643 insertions(+), 0 deletions(-) create mode 100644 drivers/misc/vmw_vmci/vmci_event.c create mode 100644 drivers/misc/vmw_vmci/vmci_event.h
drivers/misc/vmw_vmci/vmci_event.c new file +614 -0
@@ -0,0 +1,614 @@ +/* +* VMware VMCI Driver +* +* Copyright (C) 2012 VMware, Inc. All rights reserved. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms of the GNU General Public License as published by the +* Free Software Foundation version 2 and no later version. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +* for more details. +* +* You should have received a copy of the GNU General Public License along +* with this program; if not, write to the Free Software Foundation, Inc., +* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include <linux/module.h> +#include <linux/list.h> +#include <linux/sched.h> +#include <linux/vmw_vmci_api.h> +#include <linux/vmw_vmci_defs.h> + +#include "vmci_event.h" +#include "vmci_driver.h" + +#define EVENT_MAGIC 0xEABE0000 +#define VMCI_EVENT_MAX_ATTEMPTS 10 + +struct vmci_subscription { +uint32_t id; +int refCount; +bool runDelayed; +wait_queue_head_t destroyEvent; +uint32_t event; +VMCI_EventCB callback; +void *callbackData; +struct list_head subscriberListItem; +}; + +static struct list_head subscriberArray[VMCI_EVENT_MAX ]; +static spinlock_t subscriberLock; + +struct delayed_event_info { +struct vmci_subscription *sub; +uint8_t eventPayload[sizeof (struct vmci_event_data_max )]; +}; + +struct event_ref { +struct vmci_subscription *sub; +struct list_head listItem; +}; + +/* +*---------------------------------------------------------------------- +* +* vmci_event_init -- +* +* General init code. +* +* Results: +* VMCI_SUCCESS on success, appropriate error code otherwise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +int __init vmci_event_init (void ) +{ +int i; + +for (i =0 ; i <VMCI_EVENT_MAX ; i++) +INIT_LIST_HEAD (&subscriberArray[i]); + +spin_lock_init (&subscriberLock); +return VMCI_SUCCESS ; +} + +/* +*---------------------------------------------------------------------- +* +* vmci_event_exit -- +* +* General exit code. +* +* Results: +* None. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +void vmci_event_exit (void ) +{ +int e; + +/* We free all memory at exit. */ +for (e =0 ; e <VMCI_EVENT_MAX ; e++) { +struct vmci_subscription *cur, *p2; +list_for_each_entry_safe (cur, p2, &subscriberArray[e], + subscriberListItem) { + +/* +* We should never get here because all events should have been +* unregistered before we try to unload the driver module. +* Also, delayed callbacks could still be firing so this cleanup +* would not be safe. +* Still it is better to free the memory than not ... so we +* leave this code in just in case.... +*/ +pr_warn ("Unexpected free events occuring." ); +kfree (cur); + } + } + +} + +/* +*----------------------------------------------------------------------------- +* +* event_get -- +* +* Gets a reference to the given VMCISubscription. +* +* Results: +* None. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +static void event_get (struct vmci_subscription *entry)// IN +{ +ASSERT (entry); + + entry->refCount++; +} + +/* +*----------------------------------------------------------------------------- +* +* event_release -- +* +* Releases the given VMCISubscription. +* +* Results: +* None. +* +* Side effects: +* Fires the destroy event if the reference count has gone to zero. +* +*----------------------------------------------------------------------------- +*/ + +static void event_release (struct vmci_subscription *entry)// IN +{ +ASSERT (entry); +ASSERT (entry->refCount >0 ); + + entry->refCount--; +if (entry->refCount ==0 ) +wake_up (&entry->destroyEvent); +} + +/* +*------------------------------------------------------------------------------ +* +* event_release_cb -- +* +* Callback to release the event entry reference. It is called by the +* VMCI_WaitOnEvent function before it blocks. +* +* Result: +* None. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +static int event_release_cb (void *clientData)// IN +{ +struct vmci_subscription *sub = (struct vmci_subscription *)clientData; + +ASSERT (sub); + +spin_lock_bh (&subscriberLock); +event_release (sub); +spin_unlock_bh (&subscriberLock); + +return 0 ; +} + +/* +*----------------------------------------------------------------------------- +* +* event_find -- +* +* Find entry. Assumes lock is held. +* +* Results: +* Entry if found, NULL if not. +* +* Side effects: +* Increments the VMCISubscription refcount if an entry is found. +* +*----------------------------------------------------------------------------- +*/ + +static struct vmci_subscription *event_find (uint32_t subID)// IN +{ +int e; + +for (e =0 ; e <VMCI_EVENT_MAX ; e++) { +struct vmci_subscription *cur; +list_for_each_entry (cur, &subscriberArray[e], + subscriberListItem) { +if (cur->id == subID) { +event_get (cur); +return cur; + } + } + } +return NULL ; +} + +/* +*---------------------------------------------------------------------- +* +* event_delayed_dispatch_cb -- +* +* Calls the specified callback in a delayed context. +* +* Results: +* None. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +static void event_delayed_dispatch_cb (void *data)// IN +{ +struct delayed_event_info *eventInfo; +struct vmci_subscription *sub; +struct vmci_event_data *ed; + + eventInfo = data; + +ASSERT (eventInfo); +ASSERT (eventInfo->sub); + + sub = eventInfo->sub; + ed = (struct vmci_event_data *)eventInfo->eventPayload; + + sub->callback (sub->id, ed, sub->callbackData); + +spin_lock_bh (&subscriberLock); +event_release (sub); +spin_unlock_bh (&subscriberLock); + +kfree (eventInfo); +} + +/* +*---------------------------------------------------------------------------- +* +* event_deliver -- +* +* Actually delivers the events to the subscribers. +* +* Results: +* None. +* +* Side effects: +* The callback function for each subscriber is invoked. +* +*---------------------------------------------------------------------------- +*/ + +static int event_deliver (struct vmci_event_msg *eventMsg)// IN +{ +int err =VMCI_SUCCESS ; +struct vmci_subscription *cur; +struct list_head noDelayList; +struct vmci_event_data *ed; +struct event_ref *eventRef, *p2; + +ASSERT (eventMsg); + +INIT_LIST_HEAD (&noDelayList); + +spin_lock_bh (&subscriberLock); +list_for_each_entry (cur, &subscriberArray[eventMsg->eventData.event], + subscriberListItem) { +ASSERT (cur && cur->event == eventMsg->eventData.event); + +if (cur->runDelayed) { +struct delayed_event_info *eventInfo; + eventInfo =kzalloc (sizeof *eventInfo,GFP_ATOMIC ); +if (!eventInfo) { + err =VMCI_ERROR_NO_MEM ; + goto out; + } + +event_get (cur); +memcpy (eventInfo->eventPayload, +VMCI_DG_PAYLOAD (eventMsg), + (size_t ) eventMsg->hdr.payloadSize); + eventInfo->sub = cur; + err = +vmci_drv_schedule_delayed_work (event_delayed_dispatch_cb, + eventInfo); +if (err !=VMCI_SUCCESS ) { +event_release (cur); +kfree (eventInfo); + goto out; + } + + }else { +struct event_ref *eventRef; + +/* +* To avoid possible lock rank voilation when holding +* subscriberLock, we construct a local list of +* subscribers and release subscriberLock before +* invokes the callbacks. This is similar to delayed +* callbacks, but callbacks is invoked right away here. +*/ +if ((eventRef = +kzalloc (sizeof *eventRef, +GFP_ATOMIC )) ==NULL ) { + err =VMCI_ERROR_NO_MEM ; + goto out; + } + +event_get (cur); + eventRef->sub = cur; +INIT_LIST_HEAD (&eventRef->listItem); +list_add (&eventRef->listItem, &noDelayList); + } + } + +out: +spin_unlock_bh (&subscriberLock); + +list_for_each_entry_safe (eventRef, p2, &noDelayList, listItem) { +struct vmci_subscription *cur = eventRef->sub; +uint8_t eventPayload[sizeof (struct vmci_event_data_max )] = + {0 }; + +/* We set event data before each callback to ensure isolation. */ +memcpy (eventPayload,VMCI_DG_PAYLOAD (eventMsg), + (size_t ) eventMsg->hdr.payloadSize); + ed = (struct vmci_event_data *)eventPayload; + cur->callback (cur->id, ed, cur->callbackData); + +spin_lock_bh (&subscriberLock); +event_release (cur); +spin_unlock_bh (&subscriberLock); +kfree (eventRef); + } + +return err; +} + +/* +*---------------------------------------------------------------------- +* +* vmci_event_dispatch -- +* +* Dispatcher for the VMCI_EVENT_RECEIVE datagrams. Calls all +* subscribers for given event. +* +* Results: +* VMCI_SUCCESS on success, error code otherwise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +int vmci_event_dispatch (struct vmci_dg *msg)// IN +{ +struct vmci_event_msg *eventMsg = (struct vmci_event_msg *)msg; + +ASSERT (msg && + msg->src.context ==VMCI_HYPERVISOR_CONTEXT_ID && + msg->dst.resource ==VMCI_EVENT_HANDLER ); + +if (msg->payloadSize <sizeof (uint32_t ) || + msg->payloadSize >sizeof (struct vmci_event_data_max )) +return VMCI_ERROR_INVALID_ARGS ; + +if (!VMCI_EVENT_VALID (eventMsg->eventData.event)) +return VMCI_ERROR_EVENT_UNKNOWN ; + +event_deliver (eventMsg); +return VMCI_SUCCESS ; +} + +/* +*---------------------------------------------------------------------- +* +* event_register_subscription -- +* +* Initialize and add subscription to subscriber list. +* +* Results: +* VMCI_SUCCESS on success, error code otherwise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +static int event_register_subscription (struct vmci_subscription *sub,// IN +uint32_t event,// IN +uint32_t flags,// IN +VMCI_EventCB callback,// IN +void *callbackData)// IN +{ +static uint32_t subscriptionID =0 ; +uint32_t attempts =0 ; +int result; +bool success; + +ASSERT (sub); + +if (!VMCI_EVENT_VALID (event) || callback ==NULL ) { +pr_devel ("Failed to subscribe to event (type=%d) " +"(callback=%p) (data=%p)." , event, + callback, callbackData); +return VMCI_ERROR_INVALID_ARGS ; + } + + sub->runDelayed = (flags &VMCI_FLAG_EVENT_DELAYED_CB ) ? true : false; + sub->refCount =1 ; + sub->event = event; + sub->callback = callback; + sub->callbackData = callbackData; +INIT_LIST_HEAD (&sub->subscriberListItem); + +spin_lock_bh (&subscriberLock); + +/* Creation of a new event is always allowed. */ +for (success = false, attempts =0 ; + success == false && attempts <VMCI_EVENT_MAX_ATTEMPTS ; + attempts++) { +struct vmci_subscription *existingSub =NULL ; + +/* +* We try to get an id a couple of time before claiming we are out of +* resources. +*/ + sub->id = ++subscriptionID; + +/* Test for duplicate id. */ + existingSub =event_find (sub->id); +if (existingSub ==NULL ) +/* We succeeded if we didn't find a duplicate. */ + success = true; +else +event_release (existingSub); + } + +if (success) { +init_waitqueue_head (&sub->destroyEvent); +list_add (&sub->subscriberListItem, &subscriberArray[event]); + result =VMCI_SUCCESS ; + }else { + result =VMCI_ERROR_NO_RESOURCES ; + } + +spin_unlock_bh (&subscriberLock); +return result; +} + +/* +*---------------------------------------------------------------------- +* +* event_unregister_subscription -- +* +* Remove subscription from subscriber list. +* +* Results: +* VMCISubscription when found, NULL otherwise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +static struct vmci_subscription *event_unregister_subscription (uint32_t subID)// IN +{ +struct vmci_subscription *s; + +spin_lock_bh (&subscriberLock); + s =event_find (subID); +if (s !=NULL ) { +event_release (s); +list_del (&s->subscriberListItem); + } +spin_unlock_bh (&subscriberLock); + +if (s !=NULL ) +vmci_drv_wait_on_event_intr (&s->destroyEvent, + event_release_cb, s); + +return s; +} + +/* +*---------------------------------------------------------------------- +* +* VMCIEvent_Subscribe -- +* +* Subscribe to given event. The callback specified can be fired +* in different contexts depending on what flag is specified while +* registering. If flags contains VMCI_FLAG_EVENT_NONE then the +* callback is fired with the subscriber lock held (and BH context +* on the guest). If flags contain VMCI_FLAG_EVENT_DELAYED_CB then +* the callback is fired with no locks held in thread context. +* This is useful because other VMCIEvent functions can be called, +* but it also increases the chances that an event will be dropped. +* +* Results: +* VMCI_SUCCESS on success, error code otherwise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +int VMCIEvent_Subscribe (uint32_t event,// IN +uint32_t flags,// IN +VMCI_EventCB callback,// IN +void *callbackData,// IN +uint32_t * subscriptionID)// OUT +{ +int retval; +struct vmci_subscription *s =NULL ; + +if (subscriptionID ==NULL ) { +pr_devel ("Invalid subscription (NULL)." ); +return VMCI_ERROR_INVALID_ARGS ; + } + + s =kmalloc (sizeof *s,GFP_KERNEL ); +if (s ==NULL ) +return VMCI_ERROR_NO_MEM ; + + retval =event_register_subscription (s, event, flags, + callback, callbackData); +if (retval <VMCI_SUCCESS ) { +kfree (s); +return retval; + } + + *subscriptionID = s->id; +return retval; +} + +EXPORT_SYMBOL (VMCIEvent_Subscribe); + +/* +*---------------------------------------------------------------------- +* +* VMCIEvent_Unsubscribe -- +* +* Unsubscribe to given event. Removes it from list and frees it. +* Will return callbackData if requested by caller. +* +* Results: +* VMCI_SUCCESS on success, error code otherwise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +int VMCIEvent_Unsubscribe (uint32_t subID)// IN +{ +struct vmci_subscription *s; + +/* +* Return subscription. At this point we know noone else is accessing +* the subscription so we can free it. +*/ + s =event_unregister_subscription (subID); +if (s ==NULL ) +return VMCI_ERROR_NOT_FOUND ; + +kfree (s); + +return VMCI_SUCCESS ; +} + +EXPORT_SYMBOL (VMCIEvent_Unsubscribe);
drivers/misc/vmw_vmci/vmci_event.h new file +29 -0
@@ -0,0 +1,29 @@ +/* +* VMware VMCI Driver +* +* Copyright (C) 2012 VMware, Inc. All rights reserved. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms of the GNU General Public License as published by the +* Free Software Foundation version 2 and no later version. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +* for more details. +* +* You should have received a copy of the GNU General Public License along +* with this program; if not, write to the Free Software Foundation, Inc., +* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef __VMCI_EVENT_H__ +#define __VMCI_EVENT_H__ + +#include <linux/vmw_vmci_api.h> + +int vmci_event_init (void ); +void vmci_event_exit (void ); +int vmci_event_dispatch (struct vmci_dg *msg); + +#endif //__VMCI_EVENT_H__
Signature · 1 line
1.7.0.4
RFC 5/11 Apply VMCI event code
In reply to Andrew Stiegmann (stieg)Code that manages event handlers and handles callbacks when specific events fire.
--- drivers/misc/vmw_vmci/vmci_event.c | 614 ++++++++++++++++++++++++++++++++++++ drivers/misc/vmw_vmci/vmci_event.h | 29 ++ 2 files changed, 643 insertions(+), 0 deletions(-) create mode 100644 drivers/misc/vmw_vmci/vmci_event.c create mode 100644 drivers/misc/vmw_vmci/vmci_event.h
drivers/misc/vmw_vmci/vmci_event.c new file +614 -0
@@ -0,0 +1,614 @@ +/* +* VMware VMCI Driver +* +* Copyright (C) 2012 VMware, Inc. All rights reserved. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms of the GNU General Public License as published by the +* Free Software Foundation version 2 and no later version. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +* for more details. +* +* You should have received a copy of the GNU General Public License along +* with this program; if not, write to the Free Software Foundation, Inc., +* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include <linux/module.h> +#include <linux/list.h> +#include <linux/sched.h> +#include <linux/vmw_vmci_api.h> +#include <linux/vmw_vmci_defs.h> + +#include "vmci_event.h" +#include "vmci_driver.h" + +#define EVENT_MAGIC 0xEABE0000 +#define VMCI_EVENT_MAX_ATTEMPTS 10 + +struct vmci_subscription { +uint32_t id; +int refCount; +bool runDelayed; +wait_queue_head_t destroyEvent; +uint32_t event; +VMCI_EventCB callback; +void *callbackData; +struct list_head subscriberListItem; +}; + +static struct list_head subscriberArray[VMCI_EVENT_MAX ]; +static spinlock_t subscriberLock; + +struct delayed_event_info { +struct vmci_subscription *sub; +uint8_t eventPayload[sizeof (struct vmci_event_data_max )]; +}; + +struct event_ref { +struct vmci_subscription *sub; +struct list_head listItem; +}; + +/* +*---------------------------------------------------------------------- +* +* vmci_event_init -- +* +* General init code. +* +* Results: +* VMCI_SUCCESS on success, appropriate error code otherwise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +int __init vmci_event_init (void ) +{ +int i; + +for (i =0 ; i <VMCI_EVENT_MAX ; i++) +INIT_LIST_HEAD (&subscriberArray[i]); + +spin_lock_init (&subscriberLock); +return VMCI_SUCCESS ; +} + +/* +*---------------------------------------------------------------------- +* +* vmci_event_exit -- +* +* General exit code. +* +* Results: +* None. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +void vmci_event_exit (void ) +{ +int e; + +/* We free all memory at exit. */ +for (e =0 ; e <VMCI_EVENT_MAX ; e++) { +struct vmci_subscription *cur, *p2; +list_for_each_entry_safe (cur, p2, &subscriberArray[e], + subscriberListItem) { + +/* +* We should never get here because all events should have been +* unregistered before we try to unload the driver module. +* Also, delayed callbacks could still be firing so this cleanup +* would not be safe. +* Still it is better to free the memory than not ... so we +* leave this code in just in case.... +*/ +pr_warn ("Unexpected free events occuring." ); +kfree (cur); + } + } + +} + +/* +*----------------------------------------------------------------------------- +* +* event_get -- +* +* Gets a reference to the given VMCISubscription. +* +* Results: +* None. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------- +*/ + +static void event_get (struct vmci_subscription *entry)// IN +{ +ASSERT (entry); + + entry->refCount++; +} + +/* +*----------------------------------------------------------------------------- +* +* event_release -- +* +* Releases the given VMCISubscription. +* +* Results: +* None. +* +* Side effects: +* Fires the destroy event if the reference count has gone to zero. +* +*----------------------------------------------------------------------------- +*/ + +static void event_release (struct vmci_subscription *entry)// IN +{ +ASSERT (entry); +ASSERT (entry->refCount >0 ); + + entry->refCount--; +if (entry->refCount ==0 ) +wake_up (&entry->destroyEvent); +} + +/* +*------------------------------------------------------------------------------ +* +* event_release_cb -- +* +* Callback to release the event entry reference. It is called by the +* VMCI_WaitOnEvent function before it blocks. +* +* Result: +* None. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +static int event_release_cb (void *clientData)// IN +{ +struct vmci_subscription *sub = (struct vmci_subscription *)clientData; + +ASSERT (sub); + +spin_lock_bh (&subscriberLock); +event_release (sub); +spin_unlock_bh (&subscriberLock); + +return 0 ; +} + +/* +*----------------------------------------------------------------------------- +* +* event_find -- +* +* Find entry. Assumes lock is held. +* +* Results: +* Entry if found, NULL if not. +* +* Side effects: +* Increments the VMCISubscription refcount if an entry is found. +* +*----------------------------------------------------------------------------- +*/ + +static struct vmci_subscription *event_find (uint32_t subID)// IN +{ +int e; + +for (e =0 ; e <VMCI_EVENT_MAX ; e++) { +struct vmci_subscription *cur; +list_for_each_entry (cur, &subscriberArray[e], + subscriberListItem) { +if (cur->id == subID) { +event_get (cur); +return cur; + } + } + } +return NULL ; +} + +/* +*---------------------------------------------------------------------- +* +* event_delayed_dispatch_cb -- +* +* Calls the specified callback in a delayed context. +* +* Results: +* None. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +static void event_delayed_dispatch_cb (void *data)// IN +{ +struct delayed_event_info *eventInfo; +struct vmci_subscription *sub; +struct vmci_event_data *ed; + + eventInfo = data; + +ASSERT (eventInfo); +ASSERT (eventInfo->sub); + + sub = eventInfo->sub; + ed = (struct vmci_event_data *)eventInfo->eventPayload; + + sub->callback (sub->id, ed, sub->callbackData); + +spin_lock_bh (&subscriberLock); +event_release (sub); +spin_unlock_bh (&subscriberLock); + +kfree (eventInfo); +} + +/* +*---------------------------------------------------------------------------- +* +* event_deliver -- +* +* Actually delivers the events to the subscribers. +* +* Results: +* None. +* +* Side effects: +* The callback function for each subscriber is invoked. +* +*---------------------------------------------------------------------------- +*/ + +static int event_deliver (struct vmci_event_msg *eventMsg)// IN +{ +int err =VMCI_SUCCESS ; +struct vmci_subscription *cur; +struct list_head noDelayList; +struct vmci_event_data *ed; +struct event_ref *eventRef, *p2; + +ASSERT (eventMsg); + +INIT_LIST_HEAD (&noDelayList); + +spin_lock_bh (&subscriberLock); +list_for_each_entry (cur, &subscriberArray[eventMsg->eventData.event], + subscriberListItem) { +ASSERT (cur && cur->event == eventMsg->eventData.event); + +if (cur->runDelayed) { +struct delayed_event_info *eventInfo; + eventInfo =kzalloc (sizeof *eventInfo,GFP_ATOMIC ); +if (!eventInfo) { + err =VMCI_ERROR_NO_MEM ; + goto out; + } + +event_get (cur); +memcpy (eventInfo->eventPayload, +VMCI_DG_PAYLOAD (eventMsg), + (size_t ) eventMsg->hdr.payloadSize); + eventInfo->sub = cur; + err = +vmci_drv_schedule_delayed_work (event_delayed_dispatch_cb, + eventInfo); +if (err !=VMCI_SUCCESS ) { +event_release (cur); +kfree (eventInfo); + goto out; + } + + }else { +struct event_ref *eventRef; + +/* +* To avoid possible lock rank voilation when holding +* subscriberLock, we construct a local list of +* subscribers and release subscriberLock before +* invokes the callbacks. This is similar to delayed +* callbacks, but callbacks is invoked right away here. +*/ +if ((eventRef = +kzalloc (sizeof *eventRef, +GFP_ATOMIC )) ==NULL ) { + err =VMCI_ERROR_NO_MEM ; + goto out; + } + +event_get (cur); + eventRef->sub = cur; +INIT_LIST_HEAD (&eventRef->listItem); +list_add (&eventRef->listItem, &noDelayList); + } + } + +out: +spin_unlock_bh (&subscriberLock); + +list_for_each_entry_safe (eventRef, p2, &noDelayList, listItem) { +struct vmci_subscription *cur = eventRef->sub; +uint8_t eventPayload[sizeof (struct vmci_event_data_max )] = + {0 }; + +/* We set event data before each callback to ensure isolation. */ +memcpy (eventPayload,VMCI_DG_PAYLOAD (eventMsg), + (size_t ) eventMsg->hdr.payloadSize); + ed = (struct vmci_event_data *)eventPayload; + cur->callback (cur->id, ed, cur->callbackData); + +spin_lock_bh (&subscriberLock); +event_release (cur); +spin_unlock_bh (&subscriberLock); +kfree (eventRef); + } + +return err; +} + +/* +*---------------------------------------------------------------------- +* +* vmci_event_dispatch -- +* +* Dispatcher for the VMCI_EVENT_RECEIVE datagrams. Calls all +* subscribers for given event. +* +* Results: +* VMCI_SUCCESS on success, error code otherwise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +int vmci_event_dispatch (struct vmci_dg *msg)// IN +{ +struct vmci_event_msg *eventMsg = (struct vmci_event_msg *)msg; + +ASSERT (msg && + msg->src.context ==VMCI_HYPERVISOR_CONTEXT_ID && + msg->dst.resource ==VMCI_EVENT_HANDLER ); + +if (msg->payloadSize <sizeof (uint32_t ) || + msg->payloadSize >sizeof (struct vmci_event_data_max )) +return VMCI_ERROR_INVALID_ARGS ; + +if (!VMCI_EVENT_VALID (eventMsg->eventData.event)) +return VMCI_ERROR_EVENT_UNKNOWN ; + +event_deliver (eventMsg); +return VMCI_SUCCESS ; +} + +/* +*---------------------------------------------------------------------- +* +* event_register_subscription -- +* +* Initialize and add subscription to subscriber list. +* +* Results: +* VMCI_SUCCESS on success, error code otherwise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +static int event_register_subscription (struct vmci_subscription *sub,// IN +uint32_t event,// IN +uint32_t flags,// IN +VMCI_EventCB callback,// IN +void *callbackData)// IN +{ +static uint32_t subscriptionID =0 ; +uint32_t attempts =0 ; +int result; +bool success; + +ASSERT (sub); + +if (!VMCI_EVENT_VALID (event) || callback ==NULL ) { +pr_devel ("Failed to subscribe to event (type=%d) " +"(callback=%p) (data=%p)." , event, + callback, callbackData); +return VMCI_ERROR_INVALID_ARGS ; + } + + sub->runDelayed = (flags &VMCI_FLAG_EVENT_DELAYED_CB ) ? true : false; + sub->refCount =1 ; + sub->event = event; + sub->callback = callback; + sub->callbackData = callbackData; +INIT_LIST_HEAD (&sub->subscriberListItem); + +spin_lock_bh (&subscriberLock); + +/* Creation of a new event is always allowed. */ +for (success = false, attempts =0 ; + success == false && attempts <VMCI_EVENT_MAX_ATTEMPTS ; + attempts++) { +struct vmci_subscription *existingSub =NULL ; + +/* +* We try to get an id a couple of time before claiming we are out of +* resources. +*/ + sub->id = ++subscriptionID; + +/* Test for duplicate id. */ + existingSub =event_find (sub->id); +if (existingSub ==NULL ) +/* We succeeded if we didn't find a duplicate. */ + success = true; +else +event_release (existingSub); + } + +if (success) { +init_waitqueue_head (&sub->destroyEvent); +list_add (&sub->subscriberListItem, &subscriberArray[event]); + result =VMCI_SUCCESS ; + }else { + result =VMCI_ERROR_NO_RESOURCES ; + } + +spin_unlock_bh (&subscriberLock); +return result; +} + +/* +*---------------------------------------------------------------------- +* +* event_unregister_subscription -- +* +* Remove subscription from subscriber list. +* +* Results: +* VMCISubscription when found, NULL otherwise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +static struct vmci_subscription *event_unregister_subscription (uint32_t subID)// IN +{ +struct vmci_subscription *s; + +spin_lock_bh (&subscriberLock); + s =event_find (subID); +if (s !=NULL ) { +event_release (s); +list_del (&s->subscriberListItem); + } +spin_unlock_bh (&subscriberLock); + +if (s !=NULL ) +vmci_drv_wait_on_event_intr (&s->destroyEvent, + event_release_cb, s); + +return s; +} + +/* +*---------------------------------------------------------------------- +* +* VMCIEvent_Subscribe -- +* +* Subscribe to given event. The callback specified can be fired +* in different contexts depending on what flag is specified while +* registering. If flags contains VMCI_FLAG_EVENT_NONE then the +* callback is fired with the subscriber lock held (and BH context +* on the guest). If flags contain VMCI_FLAG_EVENT_DELAYED_CB then +* the callback is fired with no locks held in thread context. +* This is useful because other VMCIEvent functions can be called, +* but it also increases the chances that an event will be dropped. +* +* Results: +* VMCI_SUCCESS on success, error code otherwise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +int VMCIEvent_Subscribe (uint32_t event,// IN +uint32_t flags,// IN +VMCI_EventCB callback,// IN +void *callbackData,// IN +uint32_t * subscriptionID)// OUT +{ +int retval; +struct vmci_subscription *s =NULL ; + +if (subscriptionID ==NULL ) { +pr_devel ("Invalid subscription (NULL)." ); +return VMCI_ERROR_INVALID_ARGS ; + } + + s =kmalloc (sizeof *s,GFP_KERNEL ); +if (s ==NULL ) +return VMCI_ERROR_NO_MEM ; + + retval =event_register_subscription (s, event, flags, + callback, callbackData); +if (retval <VMCI_SUCCESS ) { +kfree (s); +return retval; + } + + *subscriptionID = s->id; +return retval; +} + +EXPORT_SYMBOL (VMCIEvent_Subscribe); + +/* +*---------------------------------------------------------------------- +* +* VMCIEvent_Unsubscribe -- +* +* Unsubscribe to given event. Removes it from list and frees it. +* Will return callbackData if requested by caller. +* +* Results: +* VMCI_SUCCESS on success, error code otherwise. +* +* Side effects: +* None. +* +*---------------------------------------------------------------------- +*/ + +int VMCIEvent_Unsubscribe (uint32_t subID)// IN +{ +struct vmci_subscription *s; + +/* +* Return subscription. At this point we know noone else is accessing +* the subscription so we can free it. +*/ + s =event_unregister_subscription (subID); +if (s ==NULL ) +return VMCI_ERROR_NOT_FOUND ; + +kfree (s); + +return VMCI_SUCCESS ; +} + +EXPORT_SYMBOL (VMCIEvent_Unsubscribe);
drivers/misc/vmw_vmci/vmci_event.h new file +29 -0
@@ -0,0 +1,29 @@ +/* +* VMware VMCI Driver +* +* Copyright (C) 2012 VMware, Inc. All rights reserved. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms of the GNU General Public License as published by the +* Free Software Foundation version 2 and no later version. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +* for more details. +* +* You should have received a copy of the GNU General Public License along +* with this program; if not, write to the Free Software Foundation, Inc., +* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef __VMCI_EVENT_H__ +#define __VMCI_EVENT_H__ + +#include <linux/vmw_vmci_api.h> + +int vmci_event_init (void ); +void vmci_event_exit (void ); +int vmci_event_dispatch (struct vmci_dg *msg); + +#endif //__VMCI_EVENT_H__
Signature · 1 line
1.7.0.4
RFC 6/11 Apply dynamic array code
In reply to Andrew Stiegmann (stieg)This code adds support for dynamic arrays that will grow if they need to.
--- drivers/misc/vmw_vmci/vmci_handle_array.c | 300 +++++++++++++++++++++++++++++ drivers/misc/vmw_vmci/vmci_handle_array.h | 50 +++++ 2 files changed, 350 insertions(+), 0 deletions(-) create mode 100644 drivers/misc/vmw_vmci/vmci_handle_array.c create mode 100644 drivers/misc/vmw_vmci/vmci_handle_array.h
drivers/misc/vmw_vmci/vmci_handle_array.c new file +300 -0
@@ -0,0 +1,300 @@ +/* +* VMware VMCI Driver +* +* Copyright (C) 2012 VMware, Inc. All rights reserved. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms of the GNU General Public License as published by the +* Free Software Foundation version 2 and no later version. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +* for more details. +* +* You should have received a copy of the GNU General Public License along +* with this program; if not, write to the Free Software Foundation, Inc., +* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include <linux/slab.h> + +#include "vmci_common_int.h" +#include "vmci_handle_array.h" + +/* +*----------------------------------------------------------------------------------- +* +* handle_arr_calc_size -- +* +* Results: +* Size in bytes of the handle array. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------------- +*/ + +static unsigned handle_arr_calc_size (uint32_t c) { +/* Decrement c b/c vmci_handle_arr includes one vmci_handle entry */ +return sizeof (struct vmci_handle_arr ) + + --c *sizeof (struct vmci_handle ); +} + +/* +*----------------------------------------------------------------------------------- +* +* vmci_handle_arr_create -- +* +* Results: +* Array if successful, NULL if not. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------------- +*/ + +struct vmci_handle_arr *vmci_handle_arr_create (uint32_t capacity) +{ +struct vmci_handle_arr *array; +uint32_t arr_size; + +if (capacity ==0 ) + capacity =VMCI_HANDLE_ARRAY_DEFAULT_SIZE ; + + arr_size =handle_arr_calc_size (capacity); + array =kmalloc (arr_size,GFP_ATOMIC ); +if (!array) +return NULL ; + + array->capacity = capacity; + array->size =0 ; + +return array; +} + +/* +*----------------------------------------------------------------------------------- +* +* vmci_handle_arr_destroy -- +* +* Results: +* None. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------------- +*/ + +void vmci_handle_arr_destroy (struct vmci_handle_arr *array) +{ +kfree (array); +} + + +/* +*----------------------------------------------------------------------------------- +* +* vmci_handle_arr_append_entry -- +* +* Results: +* None. +* +* Side effects: +* Array may be reallocated. +* +*----------------------------------------------------------------------------------- +*/ + +void vmci_handle_arr_append_entry (struct vmci_handle_arr **arrayPtr, +struct vmci_handle handle) +{ +struct vmci_handle_arr *array; + +ASSERT (arrayPtr && *arrayPtr); + array = *arrayPtr; + +if (unlikely (array->size >= array->capacity)) { +/* reallocate. */ +struct vmci_handle_arr *newArray; +const uint32_t arraySize = +handle_arr_calc_size (array->capacity * +VMCI_ARR_CAP_MULT ); + + newArray =kmalloc (arraySize,GFP_ATOMIC ); +if (!newArray) +return ; + +memcpy (newArray, array, arraySize); + newArray->capacity *=VMCI_ARR_CAP_MULT ; +kfree (array); + *arrayPtr = newArray; + array = newArray; + } + + array->entries[array->size] = handle; + array->size++; +} + +/* +*----------------------------------------------------------------------------------- +* +* vmci_handle_arr_remove_entry -- +* +* Results: +* Handle that was removed, VMCI_INVALID_HANDLE if entry not found. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------------- +*/ + +struct vmci_handle vmci_handle_arr_remove_entry (struct vmci_handle_arr *array, +struct vmci_handle entryHandle) +{ +uint32_t i; +struct vmci_handle handle =VMCI_INVALID_HANDLE ; + +ASSERT (array); +for (i =0 ; i < array->size; i++) { +if (VMCI_HANDLE_EQUAL (array->entries[i], entryHandle)) { + handle = array->entries[i]; + array->size--; + array->entries[i] = array->entries[array->size]; + array->entries[array->size] =VMCI_INVALID_HANDLE ; +break ; + } + } + +return handle; +} + +/* +*----------------------------------------------------------------------------------- +* +* vmci_handle_arr_remove_tail -- +* +* Results: +* Handle that was removed, VMCI_INVALID_HANDLE if array was empty. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------------- +*/ + +struct vmci_handle vmci_handle_arr_remove_tail (struct vmci_handle_arr *array) +{ +struct vmci_handle handle =VMCI_INVALID_HANDLE ; + +if (array->size) { + array->size--; + handle = array->entries[array->size]; + array->entries[array->size] =VMCI_INVALID_HANDLE ; + } + +return handle; +} + +/* +*----------------------------------------------------------------------------------- +* +* vmci_handle_arr_get_entry -- +* +* Results: +* Handle at given index, VMCI_INVALID_HANDLE if invalid index. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------------- +*/ + +struct vmci_handle +vmci_handle_arr_get_entry (const struct vmci_handle_arr *array,uint32_t index) +{ +ASSERT (array); + +if (unlikely (index >= array->size)) +return VMCI_INVALID_HANDLE ; + +return array->entries[index]; +} + +/* +*----------------------------------------------------------------------------------- +* +* vmci_handle_arr_get_size -- +* +* Results: +* Number of entries in array. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------------- +*/ + +uint32_t vmci_handle_arr_get_size (const struct vmci_handle_arr *array) +{ +ASSERT (array); +return array->size; +} + +/* +*----------------------------------------------------------------------------------- +* +* vmci_handle_arr_has_entry -- +* +* Results: +* true is entry exists in array, false if not. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------------- +*/ + +bool +vmci_handle_arr_has_entry (const struct vmci_handle_arr *array, +struct vmci_handle entryHandle) +{ +uint32_t i; + +ASSERT (array); +for (i =0 ; i < array->size; i++) +if (VMCI_HANDLE_EQUAL (array->entries[i], entryHandle)) +return true; + +return false; +} + +/* +*----------------------------------------------------------------------------------- +* +* vmci_handle_arr_get_handles -- +* +* Results: +* NULL if the array is empty. Otherwise, a pointer to the array +* of VMCI handles in the handle array. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------------- +*/ + +struct vmci_handle *vmci_handle_arr_get_handles (struct vmci_handle_arr *array)// IN +{ +ASSERT (array); + +if (array->size) +return array->entries; + +return NULL ; +} +
drivers/misc/vmw_vmci/vmci_handle_array.h new file +50 -0
50 lines not on this page · raw message
Signature · 1 line
1.7.0.4
RFC 6/11 Apply dynamic array code
In reply to Andrew Stiegmann (stieg)This code adds support for dynamic arrays that will grow if they need to.
--- drivers/misc/vmw_vmci/vmci_handle_array.c | 300 +++++++++++++++++++++++++++++ drivers/misc/vmw_vmci/vmci_handle_array.h | 50 +++++ 2 files changed, 350 insertions(+), 0 deletions(-) create mode 100644 drivers/misc/vmw_vmci/vmci_handle_array.c create mode 100644 drivers/misc/vmw_vmci/vmci_handle_array.h
drivers/misc/vmw_vmci/vmci_handle_array.c new file +300 -0
@@ -0,0 +1,300 @@ +/* +* VMware VMCI Driver +* +* Copyright (C) 2012 VMware, Inc. All rights reserved. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms of the GNU General Public License as published by the +* Free Software Foundation version 2 and no later version. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +* for more details. +* +* You should have received a copy of the GNU General Public License along +* with this program; if not, write to the Free Software Foundation, Inc., +* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include <linux/slab.h> + +#include "vmci_common_int.h" +#include "vmci_handle_array.h" + +/* +*----------------------------------------------------------------------------------- +* +* handle_arr_calc_size -- +* +* Results: +* Size in bytes of the handle array. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------------- +*/ + +static unsigned handle_arr_calc_size (uint32_t c) { +/* Decrement c b/c vmci_handle_arr includes one vmci_handle entry */ +return sizeof (struct vmci_handle_arr ) + + --c *sizeof (struct vmci_handle ); +} + +/* +*----------------------------------------------------------------------------------- +* +* vmci_handle_arr_create -- +* +* Results: +* Array if successful, NULL if not. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------------- +*/ + +struct vmci_handle_arr *vmci_handle_arr_create (uint32_t capacity) +{ +struct vmci_handle_arr *array; +uint32_t arr_size; + +if (capacity ==0 ) + capacity =VMCI_HANDLE_ARRAY_DEFAULT_SIZE ; + + arr_size =handle_arr_calc_size (capacity); + array =kmalloc (arr_size,GFP_ATOMIC ); +if (!array) +return NULL ; + + array->capacity = capacity; + array->size =0 ; + +return array; +} + +/* +*----------------------------------------------------------------------------------- +* +* vmci_handle_arr_destroy -- +* +* Results: +* None. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------------- +*/ + +void vmci_handle_arr_destroy (struct vmci_handle_arr *array) +{ +kfree (array); +} + + +/* +*----------------------------------------------------------------------------------- +* +* vmci_handle_arr_append_entry -- +* +* Results: +* None. +* +* Side effects: +* Array may be reallocated. +* +*----------------------------------------------------------------------------------- +*/ + +void vmci_handle_arr_append_entry (struct vmci_handle_arr **arrayPtr, +struct vmci_handle handle) +{ +struct vmci_handle_arr *array; + +ASSERT (arrayPtr && *arrayPtr); + array = *arrayPtr; + +if (unlikely (array->size >= array->capacity)) { +/* reallocate. */ +struct vmci_handle_arr *newArray; +const uint32_t arraySize = +handle_arr_calc_size (array->capacity * +VMCI_ARR_CAP_MULT ); + + newArray =kmalloc (arraySize,GFP_ATOMIC ); +if (!newArray) +return ; + +memcpy (newArray, array, arraySize); + newArray->capacity *=VMCI_ARR_CAP_MULT ; +kfree (array); + *arrayPtr = newArray; + array = newArray; + } + + array->entries[array->size] = handle; + array->size++; +} + +/* +*----------------------------------------------------------------------------------- +* +* vmci_handle_arr_remove_entry -- +* +* Results: +* Handle that was removed, VMCI_INVALID_HANDLE if entry not found. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------------- +*/ + +struct vmci_handle vmci_handle_arr_remove_entry (struct vmci_handle_arr *array, +struct vmci_handle entryHandle) +{ +uint32_t i; +struct vmci_handle handle =VMCI_INVALID_HANDLE ; + +ASSERT (array); +for (i =0 ; i < array->size; i++) { +if (VMCI_HANDLE_EQUAL (array->entries[i], entryHandle)) { + handle = array->entries[i]; + array->size--; + array->entries[i] = array->entries[array->size]; + array->entries[array->size] =VMCI_INVALID_HANDLE ; +break ; + } + } + +return handle; +} + +/* +*----------------------------------------------------------------------------------- +* +* vmci_handle_arr_remove_tail -- +* +* Results: +* Handle that was removed, VMCI_INVALID_HANDLE if array was empty. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------------- +*/ + +struct vmci_handle vmci_handle_arr_remove_tail (struct vmci_handle_arr *array) +{ +struct vmci_handle handle =VMCI_INVALID_HANDLE ; + +if (array->size) { + array->size--; + handle = array->entries[array->size]; + array->entries[array->size] =VMCI_INVALID_HANDLE ; + } + +return handle; +} + +/* +*----------------------------------------------------------------------------------- +* +* vmci_handle_arr_get_entry -- +* +* Results: +* Handle at given index, VMCI_INVALID_HANDLE if invalid index. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------------- +*/ + +struct vmci_handle +vmci_handle_arr_get_entry (const struct vmci_handle_arr *array,uint32_t index) +{ +ASSERT (array); + +if (unlikely (index >= array->size)) +return VMCI_INVALID_HANDLE ; + +return array->entries[index]; +} + +/* +*----------------------------------------------------------------------------------- +* +* vmci_handle_arr_get_size -- +* +* Results: +* Number of entries in array. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------------- +*/ + +uint32_t vmci_handle_arr_get_size (const struct vmci_handle_arr *array) +{ +ASSERT (array); +return array->size; +} + +/* +*----------------------------------------------------------------------------------- +* +* vmci_handle_arr_has_entry -- +* +* Results: +* true is entry exists in array, false if not. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------------- +*/ + +bool +vmci_handle_arr_has_entry (const struct vmci_handle_arr *array, +struct vmci_handle entryHandle) +{ +uint32_t i; + +ASSERT (array); +for (i =0 ; i < array->size; i++) +if (VMCI_HANDLE_EQUAL (array->entries[i], entryHandle)) +return true; + +return false; +} + +/* +*----------------------------------------------------------------------------------- +* +* vmci_handle_arr_get_handles -- +* +* Results: +* NULL if the array is empty. Otherwise, a pointer to the array +* of VMCI handles in the handle array. +* +* Side effects: +* None. +* +*----------------------------------------------------------------------------------- +*/ + +struct vmci_handle *vmci_handle_arr_get_handles (struct vmci_handle_arr *array)// IN +{ +ASSERT (array); + +if (array->size) +return array->entries; + +return NULL ; +} +
drivers/misc/vmw_vmci/vmci_handle_array.h new file +50 -0
50 lines not on this page · raw message
Signature · 1 line
1.7.0.4
RFC 7/11 Apply VMCI hash table
In reply to Andrew Stiegmann (stieg)Implements a hash table for VMCI's use.
--- drivers/misc/vmw_vmci/vmci_hash_table.c | 494 +++++++++++++++++++++++++++++++ drivers/misc/vmw_vmci/vmci_hash_table.h | 56 ++++ 2 files changed, 550 insertions(+), 0 deletions(-) create mode 100644 drivers/misc/vmw_vmci/vmci_hash_table.c create mode 100644 drivers/misc/vmw_vmci/vmci_hash_table.h
drivers/misc/vmw_vmci/vmci_hash_table.c new file +494 -0
@@ -0,0 +1,494 @@ +/* +* VMware VMCI Driver +* +* Copyright (C) 2012 VMware, Inc. All rights reserved. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms of the GNU General Public License as published by the +* Free Software Foundation version 2 and no later version. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +* for more details. +* +* You should have received a copy of the GNU General Public License along +* with this program; if not, write to the Free Software Foundation, Inc., +* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include <linux/vmw_vmci_defs.h> + +#include "vmci_context.h" +#include "vmci_common_int.h" +#include "vmci_driver.h" +#include "vmci_hash_table.h" + +#define VMCI_HANDLE_TO_CONTEXT_ID (_handle) ((_handle).context) +#define VMCI_HANDLE_TO_RESOURCE_ID (_handle) ((_handle).resource) +#define VMCI_HASHTABLE_HASH (_h, _sz) \ + vmci_hash_calc(VMCI_HANDLE_TO_RESOURCE_ID(_h), (_sz)) + +/* +*------------------------------------------------------------------------------ +* +* vmci_hash_create -- +* +* Result: +* None. +* +*------------------------------------------------------------------------------ +*/ + +struct vmci_hash_table *vmci_hash_create (int size) +{ +struct vmci_hash_table *table; + + table =kmalloc (sizeof *table,GFP_KERNEL ); +if (table ==NULL ) +return NULL ; + + table->entries =kcalloc (size,sizeof *table->entries,GFP_KERNEL ); +if (table->entries ==NULL ) { +kfree (table); +return NULL ; + } + + table->size = size; +spin_lock_init (&table->lock); + +return table; +} + +/* +*------------------------------------------------------------------------------ +* +* vmci_hash_destroy -- +* This function should be called at module exit time. +* We rely on the module ref count to insure that no one is accessing any +* hash table entries at this point in time. Hence we should be able to just +* remove all entries from the hash table. +* +* Result: +* None. +* +*------------------------------------------------------------------------------ +*/ + +void vmci_hash_destroy (struct vmci_hash_table *table) +{ +ASSERT (table); + +spin_lock_bh (&table->lock); +kfree (table->entries); + table->entries =NULL ; +spin_unlock_bh (&table->lock); +kfree (table); +} + +/* +*------------------------------------------------------------------------------ +* +* vmci_hash_init_entry -- +* Initializes a hash entry; +* +* Result: +* None. +* +*------------------------------------------------------------------------------ +*/ +void vmci_hash_init_entry (struct vmci_hash_entry *entry,// IN +struct vmci_handle handle)// IN +{ +ASSERT (entry); + entry->handle = handle; + entry->refCount =0 ; +} + +/* +*------------------------------------------------------------------------------ +* +* hash_exists_locked -- +* +* Unlocked version of vmci_hash_exists. +* +* Result: +* true if handle already in hashtable. false otherwise. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +static bool hash_exists_locked (struct vmci_hash_table *table,// IN +struct vmci_handle handle)// IN +{ +struct vmci_hash_entry *entry; +int idx; + +ASSERT (table); + + idx =VMCI_HASHTABLE_HASH (handle, table->size); + +for (entry = table->entries[idx]; entry; entry = entry->next) { +if (VMCI_HANDLE_TO_RESOURCE_ID (entry->handle) == +VMCI_HANDLE_TO_RESOURCE_ID (handle) && + ((VMCI_HANDLE_TO_CONTEXT_ID (entry->handle) == +VMCI_HANDLE_TO_CONTEXT_ID (handle)) || + (VMCI_INVALID_ID ==VMCI_HANDLE_TO_CONTEXT_ID (handle)) + || (VMCI_INVALID_ID == +VMCI_HANDLE_TO_CONTEXT_ID (entry->handle)))) { +return true; + } + } + +return false; +} + +/* +*------------------------------------------------------------------------------ +* +* hash_unlink -- +* Assumes caller holds table lock. +* +* Result: +* None. +* +*------------------------------------------------------------------------------ +*/ + +static int hash_unlink (struct vmci_hash_table *table,// IN +struct vmci_hash_entry *entry)// IN +{ +int result; +struct vmci_hash_entry *prev, *cur; +const int idx =VMCI_HASHTABLE_HASH (entry->handle, table->size); + + prev =NULL ; + cur = table->entries[idx]; +while (true) { +if (cur ==NULL ) { + result =VMCI_ERROR_NOT_FOUND ; +break ; + } +if (VMCI_HANDLE_EQUAL (cur->handle, entry->handle)) { +ASSERT (cur == entry); + +/* Remove entry and break. */ +if (prev) + prev->next = cur->next; +else + table->entries[idx] = cur->next; + + cur->next =NULL ; + result =VMCI_SUCCESS ; +break ; + } + prev = cur; + cur = cur->next; + } + +return result; +} + +/* +*------------------------------------------------------------------------------ +* +* vmci_hash_add -- +* +* Result: +* None. +* +*------------------------------------------------------------------------------ +*/ + +int vmci_hash_add (struct vmci_hash_table *table,// IN +struct vmci_hash_entry *entry)// IN +{ +int idx; + +ASSERT (entry); +ASSERT (table); + +spin_lock_bh (&table->lock); + +/* Creation of a new hashtable entry is always allowed. */ +if (hash_exists_locked (table, entry->handle)) { +pr_devel ("Entry (handle=0x%x:0x%x) already exists." , + entry->handle.context, entry->handle.resource); +spin_unlock_bh (&table->lock); +return VMCI_ERROR_DUPLICATE_ENTRY ; + } + + idx =VMCI_HASHTABLE_HASH (entry->handle, table->size); +ASSERT (idx < table->size); + +/* New entry is added to top/front of hash bucket. */ + entry->refCount++; + entry->next = table->entries[idx]; + table->entries[idx] = entry; +spin_unlock_bh (&table->lock); + +return VMCI_SUCCESS ; +} + +/* +*------------------------------------------------------------------------------ +* +* vmci_hash_remove -- +* +* Result: +* None. +* +*------------------------------------------------------------------------------ +*/ + +int vmci_hash_remove (struct vmci_hash_table *table,// IN +struct vmci_hash_entry *entry)// IN +{ +int result; + +ASSERT (table); +ASSERT (entry); + +spin_lock_bh (&table->lock); + +/* First unlink the entry. */ + result =hash_unlink (table, entry); +if (result ==VMCI_SUCCESS ) { +/* Decrement refcount and check if this is last reference. */ + entry->refCount--; +if (entry->refCount ==0 ) + result =VMCI_SUCCESS_ENTRY_DEAD ; + } + +spin_unlock_bh (&table->lock); + +return result; +} + +/* +*------------------------------------------------------------------------------ +* +* hash_get_locked -- +* +* Looks up an entry in the hash table, that is already locked. +* +* Result: +* If the element is found, a pointer to the element is returned. +* Otherwise NULL is returned. +* +* Side effects: +* The reference count of the returned element is increased. +* +*------------------------------------------------------------------------------ +*/ + +static struct vmci_hash_entry *hash_get_locked (struct vmci_hash_table *table,// IN +struct vmci_handle handle)// IN +{ +struct vmci_hash_entry *cur =NULL ; +int idx; + +ASSERT (!VMCI_HANDLE_EQUAL (handle,VMCI_INVALID_HANDLE )); +ASSERT (table); + + idx =VMCI_HASHTABLE_HASH (handle, table->size); + +for (cur = table->entries[idx]; cur !=NULL ; cur = cur->next) { +if (VMCI_HANDLE_TO_RESOURCE_ID (cur->handle) == +VMCI_HANDLE_TO_RESOURCE_ID (handle) && + ((VMCI_HANDLE_TO_CONTEXT_ID (cur->handle) == +VMCI_HANDLE_TO_CONTEXT_ID (handle)) || + (VMCI_INVALID_ID == +VMCI_HANDLE_TO_CONTEXT_ID (cur->handle)))) { + cur->refCount++; +break ; + } + } + +return cur; +} + +/* +*------------------------------------------------------------------------------ +* +* vmci_hash_get -- +* +* Result: +* None. +* +*------------------------------------------------------------------------------ +*/ + +struct vmci_hash_entry *vmci_hash_get (struct vmci_hash_table *table,// IN +struct vmci_handle handle)// IN +{ +struct vmci_hash_entry *entry; + +if (VMCI_HANDLE_EQUAL (handle,VMCI_INVALID_HANDLE )) +return NULL ; + +ASSERT (table); + +spin_lock_bh (&table->lock); + entry =hash_get_locked (table, handle); +spin_unlock_bh (&table->lock); + +return entry; +} + +/* +*------------------------------------------------------------------------------ +* +* vmci_hash_hold -- +* +* Hold the given entry. This will increment the entry's reference count. +* This is like a GetEntry() but without having to lookup the entry by +* handle. +* +* Result: +* None. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +void vmci_hash_hold (struct vmci_hash_table *table,// IN +struct vmci_hash_entry *entry)// IN/OUT +{ +ASSERT (table); +ASSERT (entry); + +spin_lock_bh (&table->lock); + entry->refCount++; +spin_unlock_bh (&table->lock); +} + +/* +*------------------------------------------------------------------------------ +* +* hash_release_locked -- +* +* Releases an element previously obtained with +* hash_get_locked. +* +* Result: +* If the entry is removed from the hash table, VMCI_SUCCESS_ENTRY_DEAD +* is returned. Otherwise, VMCI_SUCCESS is returned. +* +* Side effects: +* The reference count of the entry is decreased and the entry is removed +* from the hash table on 0. +* +*------------------------------------------------------------------------------ +*/ + +static int hash_release_locked (struct vmci_hash_table *table,// IN +struct vmci_hash_entry *entry)// IN +{ +int result =VMCI_SUCCESS ; + +ASSERT (table); +ASSERT (entry); + + entry->refCount--; +/* Check if this is last reference and report if so. */ +if (entry->refCount ==0 ) { + +/* +* Remove entry from hash table if not already removed. This could have +* happened already because vmci_hash_remove was called to unlink +* it. We ignore if it is not found. Datagram handles will often have +* RemoveEntry called, whereas SharedMemory regions rely on ReleaseEntry +* to unlink the entry, since the creator does not call RemoveEntry when +* it detaches. +*/ + +hash_unlink (table, entry); + result =VMCI_SUCCESS_ENTRY_DEAD ; + } + +return result; +} + +/* +*------------------------------------------------------------------------------ +* +* vmci_hash_release -- +* +* Result: +* None. +* +*------------------------------------------------------------------------------ +*/ + +int vmci_hash_release (struct vmci_hash_table *table,// IN +struct vmci_hash_entry *entry)// IN +{ +int result; + +spin_lock_bh (&table->lock); + result =hash_release_locked (table, entry); +spin_unlock_bh (&table->lock); + +return result; +} + +/* +*------------------------------------------------------------------------------ +* +* vmci_hash_exists -- +* +* Result: +* true if handle already in hashtable. false otherwise. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +bool vmci_hash_exists (struct vmci_hash_table * table,// IN +struct vmci_handle handle)// IN +{ +bool exists; + +spin_lock_bh (&table->lock); + exists =hash_exists_locked (table, handle); +spin_unlock_bh (&table->lock); + +return exists; +} + +/* +*------------------------------------------------------------------------- +* +* vmci_hash_calc -- +* +* Hash function used by the Simple Datagram API. Hashes only a VMCI id +* (not the full VMCI handle) Based on the djb2 +* hash function by Dan Bernstein. +* +* Result: +* Returns guest call size. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------- +*/ + +int vmci_hash_calc (uint32_t id,unsigned size) +{ +unsigned i; +int hash =5381 ; + +for (i =0 ; i <sizeof id; i++) + hash = ((hash <<5 ) + hash) + (uint8_t ) (id >> (i *8 )); + +return hash & (size -1 ); +}
drivers/misc/vmw_vmci/vmci_hash_table.h new file +56 -0
56 lines not on this page · raw message
Signature · 1 line
1.7.0.4
RFC 7/11 Apply VMCI hash table
In reply to Andrew Stiegmann (stieg)Implements a hash table for VMCI's use.
--- drivers/misc/vmw_vmci/vmci_hash_table.c | 494 +++++++++++++++++++++++++++++++ drivers/misc/vmw_vmci/vmci_hash_table.h | 56 ++++ 2 files changed, 550 insertions(+), 0 deletions(-) create mode 100644 drivers/misc/vmw_vmci/vmci_hash_table.c create mode 100644 drivers/misc/vmw_vmci/vmci_hash_table.h
drivers/misc/vmw_vmci/vmci_hash_table.c new file +494 -0
@@ -0,0 +1,494 @@ +/* +* VMware VMCI Driver +* +* Copyright (C) 2012 VMware, Inc. All rights reserved. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms of the GNU General Public License as published by the +* Free Software Foundation version 2 and no later version. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +* for more details. +* +* You should have received a copy of the GNU General Public License along +* with this program; if not, write to the Free Software Foundation, Inc., +* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include <linux/vmw_vmci_defs.h> + +#include "vmci_context.h" +#include "vmci_common_int.h" +#include "vmci_driver.h" +#include "vmci_hash_table.h" + +#define VMCI_HANDLE_TO_CONTEXT_ID (_handle) ((_handle).context) +#define VMCI_HANDLE_TO_RESOURCE_ID (_handle) ((_handle).resource) +#define VMCI_HASHTABLE_HASH (_h, _sz) \ + vmci_hash_calc(VMCI_HANDLE_TO_RESOURCE_ID(_h), (_sz)) + +/* +*------------------------------------------------------------------------------ +* +* vmci_hash_create -- +* +* Result: +* None. +* +*------------------------------------------------------------------------------ +*/ + +struct vmci_hash_table *vmci_hash_create (int size) +{ +struct vmci_hash_table *table; + + table =kmalloc (sizeof *table,GFP_KERNEL ); +if (table ==NULL ) +return NULL ; + + table->entries =kcalloc (size,sizeof *table->entries,GFP_KERNEL ); +if (table->entries ==NULL ) { +kfree (table); +return NULL ; + } + + table->size = size; +spin_lock_init (&table->lock); + +return table; +} + +/* +*------------------------------------------------------------------------------ +* +* vmci_hash_destroy -- +* This function should be called at module exit time. +* We rely on the module ref count to insure that no one is accessing any +* hash table entries at this point in time. Hence we should be able to just +* remove all entries from the hash table. +* +* Result: +* None. +* +*------------------------------------------------------------------------------ +*/ + +void vmci_hash_destroy (struct vmci_hash_table *table) +{ +ASSERT (table); + +spin_lock_bh (&table->lock); +kfree (table->entries); + table->entries =NULL ; +spin_unlock_bh (&table->lock); +kfree (table); +} + +/* +*------------------------------------------------------------------------------ +* +* vmci_hash_init_entry -- +* Initializes a hash entry; +* +* Result: +* None. +* +*------------------------------------------------------------------------------ +*/ +void vmci_hash_init_entry (struct vmci_hash_entry *entry,// IN +struct vmci_handle handle)// IN +{ +ASSERT (entry); + entry->handle = handle; + entry->refCount =0 ; +} + +/* +*------------------------------------------------------------------------------ +* +* hash_exists_locked -- +* +* Unlocked version of vmci_hash_exists. +* +* Result: +* true if handle already in hashtable. false otherwise. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +static bool hash_exists_locked (struct vmci_hash_table *table,// IN +struct vmci_handle handle)// IN +{ +struct vmci_hash_entry *entry; +int idx; + +ASSERT (table); + + idx =VMCI_HASHTABLE_HASH (handle, table->size); + +for (entry = table->entries[idx]; entry; entry = entry->next) { +if (VMCI_HANDLE_TO_RESOURCE_ID (entry->handle) == +VMCI_HANDLE_TO_RESOURCE_ID (handle) && + ((VMCI_HANDLE_TO_CONTEXT_ID (entry->handle) == +VMCI_HANDLE_TO_CONTEXT_ID (handle)) || + (VMCI_INVALID_ID ==VMCI_HANDLE_TO_CONTEXT_ID (handle)) + || (VMCI_INVALID_ID == +VMCI_HANDLE_TO_CONTEXT_ID (entry->handle)))) { +return true; + } + } + +return false; +} + +/* +*------------------------------------------------------------------------------ +* +* hash_unlink -- +* Assumes caller holds table lock. +* +* Result: +* None. +* +*------------------------------------------------------------------------------ +*/ + +static int hash_unlink (struct vmci_hash_table *table,// IN +struct vmci_hash_entry *entry)// IN +{ +int result; +struct vmci_hash_entry *prev, *cur; +const int idx =VMCI_HASHTABLE_HASH (entry->handle, table->size); + + prev =NULL ; + cur = table->entries[idx]; +while (true) { +if (cur ==NULL ) { + result =VMCI_ERROR_NOT_FOUND ; +break ; + } +if (VMCI_HANDLE_EQUAL (cur->handle, entry->handle)) { +ASSERT (cur == entry); + +/* Remove entry and break. */ +if (prev) + prev->next = cur->next; +else + table->entries[idx] = cur->next; + + cur->next =NULL ; + result =VMCI_SUCCESS ; +break ; + } + prev = cur; + cur = cur->next; + } + +return result; +} + +/* +*------------------------------------------------------------------------------ +* +* vmci_hash_add -- +* +* Result: +* None. +* +*------------------------------------------------------------------------------ +*/ + +int vmci_hash_add (struct vmci_hash_table *table,// IN +struct vmci_hash_entry *entry)// IN +{ +int idx; + +ASSERT (entry); +ASSERT (table); + +spin_lock_bh (&table->lock); + +/* Creation of a new hashtable entry is always allowed. */ +if (hash_exists_locked (table, entry->handle)) { +pr_devel ("Entry (handle=0x%x:0x%x) already exists." , + entry->handle.context, entry->handle.resource); +spin_unlock_bh (&table->lock); +return VMCI_ERROR_DUPLICATE_ENTRY ; + } + + idx =VMCI_HASHTABLE_HASH (entry->handle, table->size); +ASSERT (idx < table->size); + +/* New entry is added to top/front of hash bucket. */ + entry->refCount++; + entry->next = table->entries[idx]; + table->entries[idx] = entry; +spin_unlock_bh (&table->lock); + +return VMCI_SUCCESS ; +} + +/* +*------------------------------------------------------------------------------ +* +* vmci_hash_remove -- +* +* Result: +* None. +* +*------------------------------------------------------------------------------ +*/ + +int vmci_hash_remove (struct vmci_hash_table *table,// IN +struct vmci_hash_entry *entry)// IN +{ +int result; + +ASSERT (table); +ASSERT (entry); + +spin_lock_bh (&table->lock); + +/* First unlink the entry. */ + result =hash_unlink (table, entry); +if (result ==VMCI_SUCCESS ) { +/* Decrement refcount and check if this is last reference. */ + entry->refCount--; +if (entry->refCount ==0 ) + result =VMCI_SUCCESS_ENTRY_DEAD ; + } + +spin_unlock_bh (&table->lock); + +return result; +} + +/* +*------------------------------------------------------------------------------ +* +* hash_get_locked -- +* +* Looks up an entry in the hash table, that is already locked. +* +* Result: +* If the element is found, a pointer to the element is returned. +* Otherwise NULL is returned. +* +* Side effects: +* The reference count of the returned element is increased. +* +*------------------------------------------------------------------------------ +*/ + +static struct vmci_hash_entry *hash_get_locked (struct vmci_hash_table *table,// IN +struct vmci_handle handle)// IN +{ +struct vmci_hash_entry *cur =NULL ; +int idx; + +ASSERT (!VMCI_HANDLE_EQUAL (handle,VMCI_INVALID_HANDLE )); +ASSERT (table); + + idx =VMCI_HASHTABLE_HASH (handle, table->size); + +for (cur = table->entries[idx]; cur !=NULL ; cur = cur->next) { +if (VMCI_HANDLE_TO_RESOURCE_ID (cur->handle) == +VMCI_HANDLE_TO_RESOURCE_ID (handle) && + ((VMCI_HANDLE_TO_CONTEXT_ID (cur->handle) == +VMCI_HANDLE_TO_CONTEXT_ID (handle)) || + (VMCI_INVALID_ID == +VMCI_HANDLE_TO_CONTEXT_ID (cur->handle)))) { + cur->refCount++; +break ; + } + } + +return cur; +} + +/* +*------------------------------------------------------------------------------ +* +* vmci_hash_get -- +* +* Result: +* None. +* +*------------------------------------------------------------------------------ +*/ + +struct vmci_hash_entry *vmci_hash_get (struct vmci_hash_table *table,// IN +struct vmci_handle handle)// IN +{ +struct vmci_hash_entry *entry; + +if (VMCI_HANDLE_EQUAL (handle,VMCI_INVALID_HANDLE )) +return NULL ; + +ASSERT (table); + +spin_lock_bh (&table->lock); + entry =hash_get_locked (table, handle); +spin_unlock_bh (&table->lock); + +return entry; +} + +/* +*------------------------------------------------------------------------------ +* +* vmci_hash_hold -- +* +* Hold the given entry. This will increment the entry's reference count. +* This is like a GetEntry() but without having to lookup the entry by +* handle. +* +* Result: +* None. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +void vmci_hash_hold (struct vmci_hash_table *table,// IN +struct vmci_hash_entry *entry)// IN/OUT +{ +ASSERT (table); +ASSERT (entry); + +spin_lock_bh (&table->lock); + entry->refCount++; +spin_unlock_bh (&table->lock); +} + +/* +*------------------------------------------------------------------------------ +* +* hash_release_locked -- +* +* Releases an element previously obtained with +* hash_get_locked. +* +* Result: +* If the entry is removed from the hash table, VMCI_SUCCESS_ENTRY_DEAD +* is returned. Otherwise, VMCI_SUCCESS is returned. +* +* Side effects: +* The reference count of the entry is decreased and the entry is removed +* from the hash table on 0. +* +*------------------------------------------------------------------------------ +*/ + +static int hash_release_locked (struct vmci_hash_table *table,// IN +struct vmci_hash_entry *entry)// IN +{ +int result =VMCI_SUCCESS ; + +ASSERT (table); +ASSERT (entry); + + entry->refCount--; +/* Check if this is last reference and report if so. */ +if (entry->refCount ==0 ) { + +/* +* Remove entry from hash table if not already removed. This could have +* happened already because vmci_hash_remove was called to unlink +* it. We ignore if it is not found. Datagram handles will often have +* RemoveEntry called, whereas SharedMemory regions rely on ReleaseEntry +* to unlink the entry, since the creator does not call RemoveEntry when +* it detaches. +*/ + +hash_unlink (table, entry); + result =VMCI_SUCCESS_ENTRY_DEAD ; + } + +return result; +} + +/* +*------------------------------------------------------------------------------ +* +* vmci_hash_release -- +* +* Result: +* None. +* +*------------------------------------------------------------------------------ +*/ + +int vmci_hash_release (struct vmci_hash_table *table,// IN +struct vmci_hash_entry *entry)// IN +{ +int result; + +spin_lock_bh (&table->lock); + result =hash_release_locked (table, entry); +spin_unlock_bh (&table->lock); + +return result; +} + +/* +*------------------------------------------------------------------------------ +* +* vmci_hash_exists -- +* +* Result: +* true if handle already in hashtable. false otherwise. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------------ +*/ + +bool vmci_hash_exists (struct vmci_hash_table * table,// IN +struct vmci_handle handle)// IN +{ +bool exists; + +spin_lock_bh (&table->lock); + exists =hash_exists_locked (table, handle); +spin_unlock_bh (&table->lock); + +return exists; +} + +/* +*------------------------------------------------------------------------- +* +* vmci_hash_calc -- +* +* Hash function used by the Simple Datagram API. Hashes only a VMCI id +* (not the full VMCI handle) Based on the djb2 +* hash function by Dan Bernstein. +* +* Result: +* Returns guest call size. +* +* Side effects: +* None. +* +*------------------------------------------------------------------------- +*/ + +int vmci_hash_calc (uint32_t id,unsigned size) +{ +unsigned i; +int hash =5381 ; + +for (i =0 ; i <sizeof id; i++) + hash = ((hash <<5 ) + hash) + (uint8_t ) (id >> (i *8 )); + +return hash & (size -1 ); +}
drivers/misc/vmw_vmci/vmci_hash_table.h new file +56 -0
56 lines not on this page · raw message
Signature · 1 line
1.7.0.4