1685a6bf8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
28bf50399SGeorge Zhang /*
38bf50399SGeorge Zhang  * VMware VMCI Driver
48bf50399SGeorge Zhang  *
58bf50399SGeorge Zhang  * Copyright (C) 2012 VMware, Inc. All rights reserved.
68bf50399SGeorge Zhang  */
78bf50399SGeorge Zhang 
88bf50399SGeorge Zhang #include <linux/vmw_vmci_defs.h>
98bf50399SGeorge Zhang #include <linux/vmw_vmci_api.h>
108bf50399SGeorge Zhang #include <linux/miscdevice.h>
118bf50399SGeorge Zhang #include <linux/interrupt.h>
128bf50399SGeorge Zhang #include <linux/highmem.h>
138bf50399SGeorge Zhang #include <linux/atomic.h>
148bf50399SGeorge Zhang #include <linux/kernel.h>
158bf50399SGeorge Zhang #include <linux/module.h>
168bf50399SGeorge Zhang #include <linux/mutex.h>
178bf50399SGeorge Zhang #include <linux/sched.h>
185b825c3aSIngo Molnar #include <linux/cred.h>
19ea8a83a4SDmitry Torokhov #include <linux/slab.h>
208bf50399SGeorge Zhang #include <linux/file.h>
218bf50399SGeorge Zhang #include <linux/init.h>
228bf50399SGeorge Zhang #include <linux/poll.h>
238bf50399SGeorge Zhang #include <linux/pci.h>
248bf50399SGeorge Zhang #include <linux/smp.h>
258bf50399SGeorge Zhang #include <linux/fs.h>
268bf50399SGeorge Zhang #include <linux/io.h>
278bf50399SGeorge Zhang 
288bf50399SGeorge Zhang #include "vmci_handle_array.h"
298bf50399SGeorge Zhang #include "vmci_queue_pair.h"
308bf50399SGeorge Zhang #include "vmci_datagram.h"
318bf50399SGeorge Zhang #include "vmci_doorbell.h"
328bf50399SGeorge Zhang #include "vmci_resource.h"
338bf50399SGeorge Zhang #include "vmci_context.h"
348bf50399SGeorge Zhang #include "vmci_driver.h"
358bf50399SGeorge Zhang #include "vmci_event.h"
368bf50399SGeorge Zhang 
378bf50399SGeorge Zhang #define VMCI_UTIL_NUM_RESOURCES 1
388bf50399SGeorge Zhang 
398bf50399SGeorge Zhang enum {
408bf50399SGeorge Zhang 	VMCI_NOTIFY_RESOURCE_QUEUE_PAIR = 0,
418bf50399SGeorge Zhang 	VMCI_NOTIFY_RESOURCE_DOOR_BELL = 1,
428bf50399SGeorge Zhang };
438bf50399SGeorge Zhang 
448bf50399SGeorge Zhang enum {
458bf50399SGeorge Zhang 	VMCI_NOTIFY_RESOURCE_ACTION_NOTIFY = 0,
468bf50399SGeorge Zhang 	VMCI_NOTIFY_RESOURCE_ACTION_CREATE = 1,
478bf50399SGeorge Zhang 	VMCI_NOTIFY_RESOURCE_ACTION_DESTROY = 2,
488bf50399SGeorge Zhang };
498bf50399SGeorge Zhang 
508bf50399SGeorge Zhang /*
518bf50399SGeorge Zhang  * VMCI driver initialization. This block can also be used to
528bf50399SGeorge Zhang  * pass initial group membership etc.
538bf50399SGeorge Zhang  */
548bf50399SGeorge Zhang struct vmci_init_blk {
558bf50399SGeorge Zhang 	u32 cid;
568bf50399SGeorge Zhang 	u32 flags;
578bf50399SGeorge Zhang };
588bf50399SGeorge Zhang 
598bf50399SGeorge Zhang /* VMCIqueue_pairAllocInfo_VMToVM */
608bf50399SGeorge Zhang struct vmci_qp_alloc_info_vmvm {
618bf50399SGeorge Zhang 	struct vmci_handle handle;
628bf50399SGeorge Zhang 	u32 peer;
638bf50399SGeorge Zhang 	u32 flags;
648bf50399SGeorge Zhang 	u64 produce_size;
658bf50399SGeorge Zhang 	u64 consume_size;
668bf50399SGeorge Zhang 	u64 produce_page_file;	  /* User VA. */
678bf50399SGeorge Zhang 	u64 consume_page_file;	  /* User VA. */
688bf50399SGeorge Zhang 	u64 produce_page_file_size;  /* Size of the file name array. */
698bf50399SGeorge Zhang 	u64 consume_page_file_size;  /* Size of the file name array. */
708bf50399SGeorge Zhang 	s32 result;
718bf50399SGeorge Zhang 	u32 _pad;
728bf50399SGeorge Zhang };
738bf50399SGeorge Zhang 
748bf50399SGeorge Zhang /* VMCISetNotifyInfo: Used to pass notify flag's address to the host driver. */
758bf50399SGeorge Zhang struct vmci_set_notify_info {
768bf50399SGeorge Zhang 	u64 notify_uva;
778bf50399SGeorge Zhang 	s32 result;
788bf50399SGeorge Zhang 	u32 _pad;
798bf50399SGeorge Zhang };
808bf50399SGeorge Zhang 
818bf50399SGeorge Zhang /*
828bf50399SGeorge Zhang  * Per-instance host state
838bf50399SGeorge Zhang  */
848bf50399SGeorge Zhang struct vmci_host_dev {
858bf50399SGeorge Zhang 	struct vmci_ctx *context;
868bf50399SGeorge Zhang 	int user_version;
878bf50399SGeorge Zhang 	enum vmci_obj_type ct_type;
888bf50399SGeorge Zhang 	struct mutex lock;  /* Mutex lock for vmci context access */
898bf50399SGeorge Zhang };
908bf50399SGeorge Zhang 
918bf50399SGeorge Zhang static struct vmci_ctx *host_context;
928bf50399SGeorge Zhang static bool vmci_host_device_initialized;
938bf50399SGeorge Zhang static atomic_t vmci_host_active_users = ATOMIC_INIT(0);
948bf50399SGeorge Zhang 
958bf50399SGeorge Zhang /*
968bf50399SGeorge Zhang  * Determines whether the VMCI host personality is
978bf50399SGeorge Zhang  * available. Since the core functionality of the host driver is
988bf50399SGeorge Zhang  * always present, all guests could possibly use the host
998bf50399SGeorge Zhang  * personality. However, to minimize the deviation from the
1008bf50399SGeorge Zhang  * pre-unified driver state of affairs, we only consider the host
1018bf50399SGeorge Zhang  * device active if there is no active guest device or if there
1028bf50399SGeorge Zhang  * are VMX'en with active VMCI contexts using the host device.
1038bf50399SGeorge Zhang  */
vmci_host_code_active(void)1048bf50399SGeorge Zhang bool vmci_host_code_active(void)
1058bf50399SGeorge Zhang {
1068bf50399SGeorge Zhang 	return vmci_host_device_initialized &&
1078bf50399SGeorge Zhang 	    (!vmci_guest_code_active() ||
1088bf50399SGeorge Zhang 	     atomic_read(&vmci_host_active_users) > 0);
1098bf50399SGeorge Zhang }
1108bf50399SGeorge Zhang 
vmci_host_users(void)111b1bba80aSStefano Garzarella int vmci_host_users(void)
112b1bba80aSStefano Garzarella {
113b1bba80aSStefano Garzarella 	return atomic_read(&vmci_host_active_users);
114b1bba80aSStefano Garzarella }
115b1bba80aSStefano Garzarella 
1168bf50399SGeorge Zhang /*
1178bf50399SGeorge Zhang  * Called on open of /dev/vmci.
1188bf50399SGeorge Zhang  */
vmci_host_open(struct inode * inode,struct file * filp)1198bf50399SGeorge Zhang static int vmci_host_open(struct inode *inode, struct file *filp)
1208bf50399SGeorge Zhang {
1218bf50399SGeorge Zhang 	struct vmci_host_dev *vmci_host_dev;
1228bf50399SGeorge Zhang 
1238bf50399SGeorge Zhang 	vmci_host_dev = kzalloc(sizeof(struct vmci_host_dev), GFP_KERNEL);
1248bf50399SGeorge Zhang 	if (vmci_host_dev == NULL)
1258bf50399SGeorge Zhang 		return -ENOMEM;
1268bf50399SGeorge Zhang 
1278bf50399SGeorge Zhang 	vmci_host_dev->ct_type = VMCIOBJ_NOT_SET;
1288bf50399SGeorge Zhang 	mutex_init(&vmci_host_dev->lock);
1298bf50399SGeorge Zhang 	filp->private_data = vmci_host_dev;
1308bf50399SGeorge Zhang 
1318bf50399SGeorge Zhang 	return 0;
1328bf50399SGeorge Zhang }
1338bf50399SGeorge Zhang 
1348bf50399SGeorge Zhang /*
1358bf50399SGeorge Zhang  * Called on close of /dev/vmci, most often when the process
1368bf50399SGeorge Zhang  * exits.
1378bf50399SGeorge Zhang  */
vmci_host_close(struct inode * inode,struct file * filp)1388bf50399SGeorge Zhang static int vmci_host_close(struct inode *inode, struct file *filp)
1398bf50399SGeorge Zhang {
1408bf50399SGeorge Zhang 	struct vmci_host_dev *vmci_host_dev = filp->private_data;
1418bf50399SGeorge Zhang 
1428bf50399SGeorge Zhang 	if (vmci_host_dev->ct_type == VMCIOBJ_CONTEXT) {
1438bf50399SGeorge Zhang 		vmci_ctx_destroy(vmci_host_dev->context);
1448bf50399SGeorge Zhang 		vmci_host_dev->context = NULL;
1458bf50399SGeorge Zhang 
1468bf50399SGeorge Zhang 		/*
1478bf50399SGeorge Zhang 		 * The number of active contexts is used to track whether any
1488bf50399SGeorge Zhang 		 * VMX'en are using the host personality. It is incremented when
1498bf50399SGeorge Zhang 		 * a context is created through the IOCTL_VMCI_INIT_CONTEXT
1508bf50399SGeorge Zhang 		 * ioctl.
1518bf50399SGeorge Zhang 		 */
1528bf50399SGeorge Zhang 		atomic_dec(&vmci_host_active_users);
1538bf50399SGeorge Zhang 	}
1548bf50399SGeorge Zhang 	vmci_host_dev->ct_type = VMCIOBJ_NOT_SET;
1558bf50399SGeorge Zhang 
1568bf50399SGeorge Zhang 	kfree(vmci_host_dev);
1578bf50399SGeorge Zhang 	filp->private_data = NULL;
1588bf50399SGeorge Zhang 	return 0;
1598bf50399SGeorge Zhang }
1608bf50399SGeorge Zhang 
1618bf50399SGeorge Zhang /*
1628bf50399SGeorge Zhang  * This is used to wake up the VMX when a VMCI call arrives, or
1638bf50399SGeorge Zhang  * to wake up select() or poll() at the next clock tick.
1648bf50399SGeorge Zhang  */
vmci_host_poll(struct file * filp,poll_table * wait)165afc9a42bSAl Viro static __poll_t vmci_host_poll(struct file *filp, poll_table *wait)
1668bf50399SGeorge Zhang {
1678bf50399SGeorge Zhang 	struct vmci_host_dev *vmci_host_dev = filp->private_data;
168*ae13381dSDae R. Jeong 	struct vmci_ctx *context;
169afc9a42bSAl Viro 	__poll_t mask = 0;
1708bf50399SGeorge Zhang 
1718bf50399SGeorge Zhang 	if (vmci_host_dev->ct_type == VMCIOBJ_CONTEXT) {
172*ae13381dSDae R. Jeong 		/*
173*ae13381dSDae R. Jeong 		 * Read context only if ct_type == VMCIOBJ_CONTEXT to make
174*ae13381dSDae R. Jeong 		 * sure that context is initialized
175*ae13381dSDae R. Jeong 		 */
176*ae13381dSDae R. Jeong 		context = vmci_host_dev->context;
177*ae13381dSDae R. Jeong 
1788bf50399SGeorge Zhang 		/* Check for VMCI calls to this VM context. */
1798bf50399SGeorge Zhang 		if (wait)
1808bf50399SGeorge Zhang 			poll_wait(filp, &context->host_context.wait_queue,
1818bf50399SGeorge Zhang 				  wait);
1828bf50399SGeorge Zhang 
1838bf50399SGeorge Zhang 		spin_lock(&context->lock);
1848bf50399SGeorge Zhang 		if (context->pending_datagrams > 0 ||
1858bf50399SGeorge Zhang 		    vmci_handle_arr_get_size(
1868bf50399SGeorge Zhang 				context->pending_doorbell_array) > 0) {
187a9a08845SLinus Torvalds 			mask = EPOLLIN;
1888bf50399SGeorge Zhang 		}
1898bf50399SGeorge Zhang 		spin_unlock(&context->lock);
1908bf50399SGeorge Zhang 	}
1918bf50399SGeorge Zhang 	return mask;
1928bf50399SGeorge Zhang }
1938bf50399SGeorge Zhang 
1948bf50399SGeorge Zhang /*
1958bf50399SGeorge Zhang  * Copies the handles of a handle array into a user buffer, and
1968bf50399SGeorge Zhang  * returns the new length in userBufferSize. If the copy to the
1978bf50399SGeorge Zhang  * user buffer fails, the functions still returns VMCI_SUCCESS,
1988bf50399SGeorge Zhang  * but retval != 0.
1998bf50399SGeorge Zhang  */
drv_cp_harray_to_user(void __user * user_buf_uva,u64 * user_buf_size,struct vmci_handle_arr * handle_array,int * retval)2008bf50399SGeorge Zhang static int drv_cp_harray_to_user(void __user *user_buf_uva,
2018bf50399SGeorge Zhang 				 u64 *user_buf_size,
2028bf50399SGeorge Zhang 				 struct vmci_handle_arr *handle_array,
2038bf50399SGeorge Zhang 				 int *retval)
2048bf50399SGeorge Zhang {
2058bf50399SGeorge Zhang 	u32 array_size = 0;
2068bf50399SGeorge Zhang 	struct vmci_handle *handles;
2078bf50399SGeorge Zhang 
2088bf50399SGeorge Zhang 	if (handle_array)
2098bf50399SGeorge Zhang 		array_size = vmci_handle_arr_get_size(handle_array);
2108bf50399SGeorge Zhang 
2118bf50399SGeorge Zhang 	if (array_size * sizeof(*handles) > *user_buf_size)
2128bf50399SGeorge Zhang 		return VMCI_ERROR_MORE_DATA;
2138bf50399SGeorge Zhang 
2148bf50399SGeorge Zhang 	*user_buf_size = array_size * sizeof(*handles);
2158bf50399SGeorge Zhang 	if (*user_buf_size)
2168bf50399SGeorge Zhang 		*retval = copy_to_user(user_buf_uva,
2178bf50399SGeorge Zhang 				       vmci_handle_arr_get_handles
2188bf50399SGeorge Zhang 				       (handle_array), *user_buf_size);
2198bf50399SGeorge Zhang 
2208bf50399SGeorge Zhang 	return VMCI_SUCCESS;
2218bf50399SGeorge Zhang }
2228bf50399SGeorge Zhang 
2238bf50399SGeorge Zhang /*
224a1d88436SJorgen Hansen  * Sets up a given context for notify to work. Maps the notify
225a1d88436SJorgen Hansen  * boolean in user VA into kernel space.
2268bf50399SGeorge Zhang  */
vmci_host_setup_notify(struct vmci_ctx * context,unsigned long uva)2278bf50399SGeorge Zhang static int vmci_host_setup_notify(struct vmci_ctx *context,
2288bf50399SGeorge Zhang 				  unsigned long uva)
2298bf50399SGeorge Zhang {
2308bf50399SGeorge Zhang 	int retval;
2318bf50399SGeorge Zhang 
2328bf50399SGeorge Zhang 	if (context->notify_page) {
2338bf50399SGeorge Zhang 		pr_devel("%s: Notify mechanism is already set up\n", __func__);
2348bf50399SGeorge Zhang 		return VMCI_ERROR_DUPLICATE_ENTRY;
2358bf50399SGeorge Zhang 	}
2368bf50399SGeorge Zhang 
2378bf50399SGeorge Zhang 	/*
2388bf50399SGeorge Zhang 	 * We are using 'bool' internally, but let's make sure we explicit
2398bf50399SGeorge Zhang 	 * about the size.
2408bf50399SGeorge Zhang 	 */
2418bf50399SGeorge Zhang 	BUILD_BUG_ON(sizeof(bool) != sizeof(u8));
2428bf50399SGeorge Zhang 
2438bf50399SGeorge Zhang 	/*
2448bf50399SGeorge Zhang 	 * Lock physical page backing a given user VA.
2458bf50399SGeorge Zhang 	 */
24673b0140bSIra Weiny 	retval = get_user_pages_fast(uva, 1, FOLL_WRITE, &context->notify_page);
247a1d88436SJorgen Hansen 	if (retval != 1) {
248a1d88436SJorgen Hansen 		context->notify_page = NULL;
2498bf50399SGeorge Zhang 		return VMCI_ERROR_GENERIC;
250a1d88436SJorgen Hansen 	}
2511a726cb4SGeorge Kennedy 	if (context->notify_page == NULL)
2521a726cb4SGeorge Kennedy 		return VMCI_ERROR_UNAVAILABLE;
2538bf50399SGeorge Zhang 
2548bf50399SGeorge Zhang 	/*
2558bf50399SGeorge Zhang 	 * Map the locked page and set up notify pointer.
2568bf50399SGeorge Zhang 	 */
257a1d88436SJorgen Hansen 	context->notify = kmap(context->notify_page) + (uva & (PAGE_SIZE - 1));
2588bf50399SGeorge Zhang 	vmci_ctx_check_signal_notify(context);
2598bf50399SGeorge Zhang 
2608bf50399SGeorge Zhang 	return VMCI_SUCCESS;
2618bf50399SGeorge Zhang }
2628bf50399SGeorge Zhang 
vmci_host_get_version(struct vmci_host_dev * vmci_host_dev,unsigned int cmd,void __user * uptr)2638bf50399SGeorge Zhang static int vmci_host_get_version(struct vmci_host_dev *vmci_host_dev,
2648bf50399SGeorge Zhang 				 unsigned int cmd, void __user *uptr)
2658bf50399SGeorge Zhang {
2668bf50399SGeorge Zhang 	if (cmd == IOCTL_VMCI_VERSION2) {
2678bf50399SGeorge Zhang 		int __user *vptr = uptr;
2688bf50399SGeorge Zhang 		if (get_user(vmci_host_dev->user_version, vptr))
2698bf50399SGeorge Zhang 			return -EFAULT;
2708bf50399SGeorge Zhang 	}
2718bf50399SGeorge Zhang 
2728bf50399SGeorge Zhang 	/*
2738bf50399SGeorge Zhang 	 * The basic logic here is:
2748bf50399SGeorge Zhang 	 *
2758bf50399SGeorge Zhang 	 * If the user sends in a version of 0 tell it our version.
2768bf50399SGeorge Zhang 	 * If the user didn't send in a version, tell it our version.
2778bf50399SGeorge Zhang 	 * If the user sent in an old version, tell it -its- version.
2788bf50399SGeorge Zhang 	 * If the user sent in an newer version, tell it our version.
2798bf50399SGeorge Zhang 	 *
2808bf50399SGeorge Zhang 	 * The rationale behind telling the caller its version is that
2818bf50399SGeorge Zhang 	 * Workstation 6.5 required that VMX and VMCI kernel module were
2828bf50399SGeorge Zhang 	 * version sync'd.  All new VMX users will be programmed to
2838bf50399SGeorge Zhang 	 * handle the VMCI kernel module version.
2848bf50399SGeorge Zhang 	 */
2858bf50399SGeorge Zhang 
2868bf50399SGeorge Zhang 	if (vmci_host_dev->user_version > 0 &&
2878bf50399SGeorge Zhang 	    vmci_host_dev->user_version < VMCI_VERSION_HOSTQP) {
2888bf50399SGeorge Zhang 		return vmci_host_dev->user_version;
2898bf50399SGeorge Zhang 	}
2908bf50399SGeorge Zhang 
2918bf50399SGeorge Zhang 	return VMCI_VERSION;
2928bf50399SGeorge Zhang }
2938bf50399SGeorge Zhang 
2948bf50399SGeorge Zhang #define vmci_ioctl_err(fmt, ...)	\
2958bf50399SGeorge Zhang 	pr_devel("%s: " fmt, ioctl_name, ##__VA_ARGS__)
2968bf50399SGeorge Zhang 
vmci_host_do_init_context(struct vmci_host_dev * vmci_host_dev,const char * ioctl_name,void __user * uptr)2978bf50399SGeorge Zhang static int vmci_host_do_init_context(struct vmci_host_dev *vmci_host_dev,
2988bf50399SGeorge Zhang 				     const char *ioctl_name,
2998bf50399SGeorge Zhang 				     void __user *uptr)
3008bf50399SGeorge Zhang {
3018bf50399SGeorge Zhang 	struct vmci_init_blk init_block;
3028bf50399SGeorge Zhang 	const struct cred *cred;
3038bf50399SGeorge Zhang 	int retval;
3048bf50399SGeorge Zhang 
3058bf50399SGeorge Zhang 	if (copy_from_user(&init_block, uptr, sizeof(init_block))) {
3068bf50399SGeorge Zhang 		vmci_ioctl_err("error reading init block\n");
3078bf50399SGeorge Zhang 		return -EFAULT;
3088bf50399SGeorge Zhang 	}
3098bf50399SGeorge Zhang 
3108bf50399SGeorge Zhang 	mutex_lock(&vmci_host_dev->lock);
3118bf50399SGeorge Zhang 
3128bf50399SGeorge Zhang 	if (vmci_host_dev->ct_type != VMCIOBJ_NOT_SET) {
3138bf50399SGeorge Zhang 		vmci_ioctl_err("received VMCI init on initialized handle\n");
3148bf50399SGeorge Zhang 		retval = -EINVAL;
3158bf50399SGeorge Zhang 		goto out;
3168bf50399SGeorge Zhang 	}
3178bf50399SGeorge Zhang 
3188bf50399SGeorge Zhang 	if (init_block.flags & ~VMCI_PRIVILEGE_FLAG_RESTRICTED) {
3198bf50399SGeorge Zhang 		vmci_ioctl_err("unsupported VMCI restriction flag\n");
3208bf50399SGeorge Zhang 		retval = -EINVAL;
3218bf50399SGeorge Zhang 		goto out;
3228bf50399SGeorge Zhang 	}
3238bf50399SGeorge Zhang 
3248bf50399SGeorge Zhang 	cred = get_current_cred();
3258bf50399SGeorge Zhang 	vmci_host_dev->context = vmci_ctx_create(init_block.cid,
3268bf50399SGeorge Zhang 						 init_block.flags, 0,
3278bf50399SGeorge Zhang 						 vmci_host_dev->user_version,
3288bf50399SGeorge Zhang 						 cred);
3298bf50399SGeorge Zhang 	put_cred(cred);
3308bf50399SGeorge Zhang 	if (IS_ERR(vmci_host_dev->context)) {
3318bf50399SGeorge Zhang 		retval = PTR_ERR(vmci_host_dev->context);
3328bf50399SGeorge Zhang 		vmci_ioctl_err("error initializing context\n");
3338bf50399SGeorge Zhang 		goto out;
3348bf50399SGeorge Zhang 	}
3358bf50399SGeorge Zhang 
3368bf50399SGeorge Zhang 	/*
3378bf50399SGeorge Zhang 	 * Copy cid to userlevel, we do this to allow the VMX
3388bf50399SGeorge Zhang 	 * to enforce its policy on cid generation.
3398bf50399SGeorge Zhang 	 */
3408bf50399SGeorge Zhang 	init_block.cid = vmci_ctx_get_id(vmci_host_dev->context);
3418bf50399SGeorge Zhang 	if (copy_to_user(uptr, &init_block, sizeof(init_block))) {
3428bf50399SGeorge Zhang 		vmci_ctx_destroy(vmci_host_dev->context);
3438bf50399SGeorge Zhang 		vmci_host_dev->context = NULL;
3448bf50399SGeorge Zhang 		vmci_ioctl_err("error writing init block\n");
3458bf50399SGeorge Zhang 		retval = -EFAULT;
3468bf50399SGeorge Zhang 		goto out;
3478bf50399SGeorge Zhang 	}
3488bf50399SGeorge Zhang 
3498bf50399SGeorge Zhang 	vmci_host_dev->ct_type = VMCIOBJ_CONTEXT;
3508bf50399SGeorge Zhang 	atomic_inc(&vmci_host_active_users);
3518bf50399SGeorge Zhang 
352b1bba80aSStefano Garzarella 	vmci_call_vsock_callback(true);
353b1bba80aSStefano Garzarella 
3548bf50399SGeorge Zhang 	retval = 0;
3558bf50399SGeorge Zhang 
3568bf50399SGeorge Zhang out:
3578bf50399SGeorge Zhang 	mutex_unlock(&vmci_host_dev->lock);
3588bf50399SGeorge Zhang 	return retval;
3598bf50399SGeorge Zhang }
3608bf50399SGeorge Zhang 
vmci_host_do_send_datagram(struct vmci_host_dev * vmci_host_dev,const char * ioctl_name,void __user * uptr)3618bf50399SGeorge Zhang static int vmci_host_do_send_datagram(struct vmci_host_dev *vmci_host_dev,
3628bf50399SGeorge Zhang 				      const char *ioctl_name,
3638bf50399SGeorge Zhang 				      void __user *uptr)
3648bf50399SGeorge Zhang {
3658bf50399SGeorge Zhang 	struct vmci_datagram_snd_rcv_info send_info;
3668bf50399SGeorge Zhang 	struct vmci_datagram *dg = NULL;
3678bf50399SGeorge Zhang 	u32 cid;
3688bf50399SGeorge Zhang 
3698bf50399SGeorge Zhang 	if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
3708bf50399SGeorge Zhang 		vmci_ioctl_err("only valid for contexts\n");
3718bf50399SGeorge Zhang 		return -EINVAL;
3728bf50399SGeorge Zhang 	}
3738bf50399SGeorge Zhang 
3748bf50399SGeorge Zhang 	if (copy_from_user(&send_info, uptr, sizeof(send_info)))
3758bf50399SGeorge Zhang 		return -EFAULT;
3768bf50399SGeorge Zhang 
3778bf50399SGeorge Zhang 	if (send_info.len > VMCI_MAX_DG_SIZE) {
3788bf50399SGeorge Zhang 		vmci_ioctl_err("datagram is too big (size=%d)\n",
3798bf50399SGeorge Zhang 			       send_info.len);
3808bf50399SGeorge Zhang 		return -EINVAL;
3818bf50399SGeorge Zhang 	}
3828bf50399SGeorge Zhang 
3838bf50399SGeorge Zhang 	if (send_info.len < sizeof(*dg)) {
3848bf50399SGeorge Zhang 		vmci_ioctl_err("datagram is too small (size=%d)\n",
3858bf50399SGeorge Zhang 			       send_info.len);
3868bf50399SGeorge Zhang 		return -EINVAL;
3878bf50399SGeorge Zhang 	}
3888bf50399SGeorge Zhang 
389655745b0SMuhammad Falak R Wani 	dg = memdup_user((void __user *)(uintptr_t)send_info.addr,
390655745b0SMuhammad Falak R Wani 			 send_info.len);
391655745b0SMuhammad Falak R Wani 	if (IS_ERR(dg)) {
3928bf50399SGeorge Zhang 		vmci_ioctl_err(
3938bf50399SGeorge Zhang 			"cannot allocate memory to dispatch datagram\n");
394655745b0SMuhammad Falak R Wani 		return PTR_ERR(dg);
3958bf50399SGeorge Zhang 	}
3968bf50399SGeorge Zhang 
39774b5c297SAndy King 	if (VMCI_DG_SIZE(dg) != send_info.len) {
39874b5c297SAndy King 		vmci_ioctl_err("datagram size mismatch\n");
39974b5c297SAndy King 		kfree(dg);
40074b5c297SAndy King 		return -EINVAL;
40174b5c297SAndy King 	}
40274b5c297SAndy King 
4038bf50399SGeorge Zhang 	pr_devel("Datagram dst (handle=0x%x:0x%x) src (handle=0x%x:0x%x), payload (size=%llu bytes)\n",
4048bf50399SGeorge Zhang 		 dg->dst.context, dg->dst.resource,
4058bf50399SGeorge Zhang 		 dg->src.context, dg->src.resource,
4068bf50399SGeorge Zhang 		 (unsigned long long)dg->payload_size);
4078bf50399SGeorge Zhang 
4088bf50399SGeorge Zhang 	/* Get source context id. */
4098bf50399SGeorge Zhang 	cid = vmci_ctx_get_id(vmci_host_dev->context);
4108bf50399SGeorge Zhang 	send_info.result = vmci_datagram_dispatch(cid, dg, true);
4118bf50399SGeorge Zhang 	kfree(dg);
4128bf50399SGeorge Zhang 
4138bf50399SGeorge Zhang 	return copy_to_user(uptr, &send_info, sizeof(send_info)) ? -EFAULT : 0;
4148bf50399SGeorge Zhang }
4158bf50399SGeorge Zhang 
vmci_host_do_receive_datagram(struct vmci_host_dev * vmci_host_dev,const char * ioctl_name,void __user * uptr)4168bf50399SGeorge Zhang static int vmci_host_do_receive_datagram(struct vmci_host_dev *vmci_host_dev,
4178bf50399SGeorge Zhang 					 const char *ioctl_name,
4188bf50399SGeorge Zhang 					 void __user *uptr)
4198bf50399SGeorge Zhang {
4208bf50399SGeorge Zhang 	struct vmci_datagram_snd_rcv_info recv_info;
4218bf50399SGeorge Zhang 	struct vmci_datagram *dg = NULL;
4228bf50399SGeorge Zhang 	int retval;
4238bf50399SGeorge Zhang 	size_t size;
4248bf50399SGeorge Zhang 
4258bf50399SGeorge Zhang 	if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
4268bf50399SGeorge Zhang 		vmci_ioctl_err("only valid for contexts\n");
4278bf50399SGeorge Zhang 		return -EINVAL;
4288bf50399SGeorge Zhang 	}
4298bf50399SGeorge Zhang 
4308bf50399SGeorge Zhang 	if (copy_from_user(&recv_info, uptr, sizeof(recv_info)))
4318bf50399SGeorge Zhang 		return -EFAULT;
4328bf50399SGeorge Zhang 
4338bf50399SGeorge Zhang 	size = recv_info.len;
4348bf50399SGeorge Zhang 	recv_info.result = vmci_ctx_dequeue_datagram(vmci_host_dev->context,
4358bf50399SGeorge Zhang 						     &size, &dg);
4368bf50399SGeorge Zhang 
4378bf50399SGeorge Zhang 	if (recv_info.result >= VMCI_SUCCESS) {
4388bf50399SGeorge Zhang 		void __user *ubuf = (void __user *)(uintptr_t)recv_info.addr;
4398bf50399SGeorge Zhang 		retval = copy_to_user(ubuf, dg, VMCI_DG_SIZE(dg));
4408bf50399SGeorge Zhang 		kfree(dg);
4418bf50399SGeorge Zhang 		if (retval != 0)
4428bf50399SGeorge Zhang 			return -EFAULT;
4438bf50399SGeorge Zhang 	}
4448bf50399SGeorge Zhang 
4458bf50399SGeorge Zhang 	return copy_to_user(uptr, &recv_info, sizeof(recv_info)) ? -EFAULT : 0;
4468bf50399SGeorge Zhang }
4478bf50399SGeorge Zhang 
vmci_host_do_alloc_queuepair(struct vmci_host_dev * vmci_host_dev,const char * ioctl_name,void __user * uptr)4488bf50399SGeorge Zhang static int vmci_host_do_alloc_queuepair(struct vmci_host_dev *vmci_host_dev,
4498bf50399SGeorge Zhang 					const char *ioctl_name,
4508bf50399SGeorge Zhang 					void __user *uptr)
4518bf50399SGeorge Zhang {
4528bf50399SGeorge Zhang 	struct vmci_handle handle;
4538bf50399SGeorge Zhang 	int vmci_status;
4548bf50399SGeorge Zhang 	int __user *retptr;
4558bf50399SGeorge Zhang 
4568bf50399SGeorge Zhang 	if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
4578bf50399SGeorge Zhang 		vmci_ioctl_err("only valid for contexts\n");
4588bf50399SGeorge Zhang 		return -EINVAL;
4598bf50399SGeorge Zhang 	}
4608bf50399SGeorge Zhang 
4618bf50399SGeorge Zhang 	if (vmci_host_dev->user_version < VMCI_VERSION_NOVMVM) {
4628bf50399SGeorge Zhang 		struct vmci_qp_alloc_info_vmvm alloc_info;
4638bf50399SGeorge Zhang 		struct vmci_qp_alloc_info_vmvm __user *info = uptr;
4648bf50399SGeorge Zhang 
4658bf50399SGeorge Zhang 		if (copy_from_user(&alloc_info, uptr, sizeof(alloc_info)))
4668bf50399SGeorge Zhang 			return -EFAULT;
4678bf50399SGeorge Zhang 
4688bf50399SGeorge Zhang 		handle = alloc_info.handle;
4698bf50399SGeorge Zhang 		retptr = &info->result;
4708bf50399SGeorge Zhang 
4718bf50399SGeorge Zhang 		vmci_status = vmci_qp_broker_alloc(alloc_info.handle,
4728bf50399SGeorge Zhang 						alloc_info.peer,
4738bf50399SGeorge Zhang 						alloc_info.flags,
4748bf50399SGeorge Zhang 						VMCI_NO_PRIVILEGE_FLAGS,
4758bf50399SGeorge Zhang 						alloc_info.produce_size,
4768bf50399SGeorge Zhang 						alloc_info.consume_size,
4778bf50399SGeorge Zhang 						NULL,
4788bf50399SGeorge Zhang 						vmci_host_dev->context);
4798bf50399SGeorge Zhang 
4808bf50399SGeorge Zhang 		if (vmci_status == VMCI_SUCCESS)
4818bf50399SGeorge Zhang 			vmci_status = VMCI_SUCCESS_QUEUEPAIR_CREATE;
4828bf50399SGeorge Zhang 	} else {
4838bf50399SGeorge Zhang 		struct vmci_qp_alloc_info alloc_info;
4848bf50399SGeorge Zhang 		struct vmci_qp_alloc_info __user *info = uptr;
4858bf50399SGeorge Zhang 		struct vmci_qp_page_store page_store;
4868bf50399SGeorge Zhang 
4878bf50399SGeorge Zhang 		if (copy_from_user(&alloc_info, uptr, sizeof(alloc_info)))
4888bf50399SGeorge Zhang 			return -EFAULT;
4898bf50399SGeorge Zhang 
4908bf50399SGeorge Zhang 		handle = alloc_info.handle;
4918bf50399SGeorge Zhang 		retptr = &info->result;
4928bf50399SGeorge Zhang 
4938bf50399SGeorge Zhang 		page_store.pages = alloc_info.ppn_va;
4948bf50399SGeorge Zhang 		page_store.len = alloc_info.num_ppns;
4958bf50399SGeorge Zhang 
4968bf50399SGeorge Zhang 		vmci_status = vmci_qp_broker_alloc(alloc_info.handle,
4978bf50399SGeorge Zhang 						alloc_info.peer,
4988bf50399SGeorge Zhang 						alloc_info.flags,
4998bf50399SGeorge Zhang 						VMCI_NO_PRIVILEGE_FLAGS,
5008bf50399SGeorge Zhang 						alloc_info.produce_size,
5018bf50399SGeorge Zhang 						alloc_info.consume_size,
5028bf50399SGeorge Zhang 						&page_store,
5038bf50399SGeorge Zhang 						vmci_host_dev->context);
5048bf50399SGeorge Zhang 	}
5058bf50399SGeorge Zhang 
5068bf50399SGeorge Zhang 	if (put_user(vmci_status, retptr)) {
5078bf50399SGeorge Zhang 		if (vmci_status >= VMCI_SUCCESS) {
5088bf50399SGeorge Zhang 			vmci_status = vmci_qp_broker_detach(handle,
5098bf50399SGeorge Zhang 							vmci_host_dev->context);
5108bf50399SGeorge Zhang 		}
5118bf50399SGeorge Zhang 		return -EFAULT;
5128bf50399SGeorge Zhang 	}
5138bf50399SGeorge Zhang 
5148bf50399SGeorge Zhang 	return 0;
5158bf50399SGeorge Zhang }
5168bf50399SGeorge Zhang 
vmci_host_do_queuepair_setva(struct vmci_host_dev * vmci_host_dev,const char * ioctl_name,void __user * uptr)5178bf50399SGeorge Zhang static int vmci_host_do_queuepair_setva(struct vmci_host_dev *vmci_host_dev,
5188bf50399SGeorge Zhang 					const char *ioctl_name,
5198bf50399SGeorge Zhang 					void __user *uptr)
5208bf50399SGeorge Zhang {
5218bf50399SGeorge Zhang 	struct vmci_qp_set_va_info set_va_info;
5228bf50399SGeorge Zhang 	struct vmci_qp_set_va_info __user *info = uptr;
5238bf50399SGeorge Zhang 	s32 result;
5248bf50399SGeorge Zhang 
5258bf50399SGeorge Zhang 	if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
5268bf50399SGeorge Zhang 		vmci_ioctl_err("only valid for contexts\n");
5278bf50399SGeorge Zhang 		return -EINVAL;
5288bf50399SGeorge Zhang 	}
5298bf50399SGeorge Zhang 
5308bf50399SGeorge Zhang 	if (vmci_host_dev->user_version < VMCI_VERSION_NOVMVM) {
5318bf50399SGeorge Zhang 		vmci_ioctl_err("is not allowed\n");
5328bf50399SGeorge Zhang 		return -EINVAL;
5338bf50399SGeorge Zhang 	}
5348bf50399SGeorge Zhang 
5358bf50399SGeorge Zhang 	if (copy_from_user(&set_va_info, uptr, sizeof(set_va_info)))
5368bf50399SGeorge Zhang 		return -EFAULT;
5378bf50399SGeorge Zhang 
5388bf50399SGeorge Zhang 	if (set_va_info.va) {
5398bf50399SGeorge Zhang 		/*
5408bf50399SGeorge Zhang 		 * VMX is passing down a new VA for the queue
5418bf50399SGeorge Zhang 		 * pair mapping.
5428bf50399SGeorge Zhang 		 */
5438bf50399SGeorge Zhang 		result = vmci_qp_broker_map(set_va_info.handle,
5448bf50399SGeorge Zhang 					    vmci_host_dev->context,
5458bf50399SGeorge Zhang 					    set_va_info.va);
5468bf50399SGeorge Zhang 	} else {
5478bf50399SGeorge Zhang 		/*
5488bf50399SGeorge Zhang 		 * The queue pair is about to be unmapped by
5498bf50399SGeorge Zhang 		 * the VMX.
5508bf50399SGeorge Zhang 		 */
5518bf50399SGeorge Zhang 		result = vmci_qp_broker_unmap(set_va_info.handle,
5528bf50399SGeorge Zhang 					 vmci_host_dev->context, 0);
5538bf50399SGeorge Zhang 	}
5548bf50399SGeorge Zhang 
5558bf50399SGeorge Zhang 	return put_user(result, &info->result) ? -EFAULT : 0;
5568bf50399SGeorge Zhang }
5578bf50399SGeorge Zhang 
vmci_host_do_queuepair_setpf(struct vmci_host_dev * vmci_host_dev,const char * ioctl_name,void __user * uptr)5588bf50399SGeorge Zhang static int vmci_host_do_queuepair_setpf(struct vmci_host_dev *vmci_host_dev,
5598bf50399SGeorge Zhang 					const char *ioctl_name,
5608bf50399SGeorge Zhang 					void __user *uptr)
5618bf50399SGeorge Zhang {
5628bf50399SGeorge Zhang 	struct vmci_qp_page_file_info page_file_info;
5638bf50399SGeorge Zhang 	struct vmci_qp_page_file_info __user *info = uptr;
5648bf50399SGeorge Zhang 	s32 result;
5658bf50399SGeorge Zhang 
5668bf50399SGeorge Zhang 	if (vmci_host_dev->user_version < VMCI_VERSION_HOSTQP ||
5678bf50399SGeorge Zhang 	    vmci_host_dev->user_version >= VMCI_VERSION_NOVMVM) {
5688bf50399SGeorge Zhang 		vmci_ioctl_err("not supported on this VMX (version=%d)\n",
5698bf50399SGeorge Zhang 			       vmci_host_dev->user_version);
5708bf50399SGeorge Zhang 		return -EINVAL;
5718bf50399SGeorge Zhang 	}
5728bf50399SGeorge Zhang 
5738bf50399SGeorge Zhang 	if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
5748bf50399SGeorge Zhang 		vmci_ioctl_err("only valid for contexts\n");
5758bf50399SGeorge Zhang 		return -EINVAL;
5768bf50399SGeorge Zhang 	}
5778bf50399SGeorge Zhang 
5788bf50399SGeorge Zhang 	if (copy_from_user(&page_file_info, uptr, sizeof(*info)))
5798bf50399SGeorge Zhang 		return -EFAULT;
5808bf50399SGeorge Zhang 
5818bf50399SGeorge Zhang 	/*
5828bf50399SGeorge Zhang 	 * Communicate success pre-emptively to the caller.  Note that the
5838bf50399SGeorge Zhang 	 * basic premise is that it is incumbent upon the caller not to look at
5848bf50399SGeorge Zhang 	 * the info.result field until after the ioctl() returns.  And then,
5858bf50399SGeorge Zhang 	 * only if the ioctl() result indicates no error.  We send up the
5868bf50399SGeorge Zhang 	 * SUCCESS status before calling SetPageStore() store because failing
5878bf50399SGeorge Zhang 	 * to copy up the result code means unwinding the SetPageStore().
5888bf50399SGeorge Zhang 	 *
5898bf50399SGeorge Zhang 	 * It turns out the logic to unwind a SetPageStore() opens a can of
5908bf50399SGeorge Zhang 	 * worms.  For example, if a host had created the queue_pair and a
5918bf50399SGeorge Zhang 	 * guest attaches and SetPageStore() is successful but writing success
5928bf50399SGeorge Zhang 	 * fails, then ... the host has to be stopped from writing (anymore)
5938bf50399SGeorge Zhang 	 * data into the queue_pair.  That means an additional test in the
5948bf50399SGeorge Zhang 	 * VMCI_Enqueue() code path.  Ugh.
5958bf50399SGeorge Zhang 	 */
5968bf50399SGeorge Zhang 
5978bf50399SGeorge Zhang 	if (put_user(VMCI_SUCCESS, &info->result)) {
5988bf50399SGeorge Zhang 		/*
5998bf50399SGeorge Zhang 		 * In this case, we can't write a result field of the
6008bf50399SGeorge Zhang 		 * caller's info block.  So, we don't even try to
6018bf50399SGeorge Zhang 		 * SetPageStore().
6028bf50399SGeorge Zhang 		 */
6038bf50399SGeorge Zhang 		return -EFAULT;
6048bf50399SGeorge Zhang 	}
6058bf50399SGeorge Zhang 
6068bf50399SGeorge Zhang 	result = vmci_qp_broker_set_page_store(page_file_info.handle,
6078bf50399SGeorge Zhang 						page_file_info.produce_va,
6088bf50399SGeorge Zhang 						page_file_info.consume_va,
6098bf50399SGeorge Zhang 						vmci_host_dev->context);
6108bf50399SGeorge Zhang 	if (result < VMCI_SUCCESS) {
6118bf50399SGeorge Zhang 		if (put_user(result, &info->result)) {
6128bf50399SGeorge Zhang 			/*
6138bf50399SGeorge Zhang 			 * Note that in this case the SetPageStore()
6148bf50399SGeorge Zhang 			 * call failed but we were unable to
6158bf50399SGeorge Zhang 			 * communicate that to the caller (because the
6168bf50399SGeorge Zhang 			 * copy_to_user() call failed).  So, if we
6178bf50399SGeorge Zhang 			 * simply return an error (in this case
6188bf50399SGeorge Zhang 			 * -EFAULT) then the caller will know that the
6198bf50399SGeorge Zhang 			 *  SetPageStore failed even though we couldn't
6208bf50399SGeorge Zhang 			 *  put the result code in the result field and
6218bf50399SGeorge Zhang 			 *  indicate exactly why it failed.
6228bf50399SGeorge Zhang 			 *
6238bf50399SGeorge Zhang 			 * That says nothing about the issue where we
6248bf50399SGeorge Zhang 			 * were once able to write to the caller's info
6258bf50399SGeorge Zhang 			 * memory and now can't.  Something more
6268bf50399SGeorge Zhang 			 * serious is probably going on than the fact
6278bf50399SGeorge Zhang 			 * that SetPageStore() didn't work.
6288bf50399SGeorge Zhang 			 */
6298bf50399SGeorge Zhang 			return -EFAULT;
6308bf50399SGeorge Zhang 		}
6318bf50399SGeorge Zhang 	}
6328bf50399SGeorge Zhang 
6338bf50399SGeorge Zhang 	return 0;
6348bf50399SGeorge Zhang }
6358bf50399SGeorge Zhang 
vmci_host_do_qp_detach(struct vmci_host_dev * vmci_host_dev,const char * ioctl_name,void __user * uptr)6368bf50399SGeorge Zhang static int vmci_host_do_qp_detach(struct vmci_host_dev *vmci_host_dev,
6378bf50399SGeorge Zhang 				  const char *ioctl_name,
6388bf50399SGeorge Zhang 				  void __user *uptr)
6398bf50399SGeorge Zhang {
6408bf50399SGeorge Zhang 	struct vmci_qp_dtch_info detach_info;
6418bf50399SGeorge Zhang 	struct vmci_qp_dtch_info __user *info = uptr;
6428bf50399SGeorge Zhang 	s32 result;
6438bf50399SGeorge Zhang 
6448bf50399SGeorge Zhang 	if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
6458bf50399SGeorge Zhang 		vmci_ioctl_err("only valid for contexts\n");
6468bf50399SGeorge Zhang 		return -EINVAL;
6478bf50399SGeorge Zhang 	}
6488bf50399SGeorge Zhang 
6498bf50399SGeorge Zhang 	if (copy_from_user(&detach_info, uptr, sizeof(detach_info)))
6508bf50399SGeorge Zhang 		return -EFAULT;
6518bf50399SGeorge Zhang 
6528bf50399SGeorge Zhang 	result = vmci_qp_broker_detach(detach_info.handle,
6538bf50399SGeorge Zhang 				       vmci_host_dev->context);
6548bf50399SGeorge Zhang 	if (result == VMCI_SUCCESS &&
6558bf50399SGeorge Zhang 	    vmci_host_dev->user_version < VMCI_VERSION_NOVMVM) {
6568bf50399SGeorge Zhang 		result = VMCI_SUCCESS_LAST_DETACH;
6578bf50399SGeorge Zhang 	}
6588bf50399SGeorge Zhang 
6598bf50399SGeorge Zhang 	return put_user(result, &info->result) ? -EFAULT : 0;
6608bf50399SGeorge Zhang }
6618bf50399SGeorge Zhang 
vmci_host_do_ctx_add_notify(struct vmci_host_dev * vmci_host_dev,const char * ioctl_name,void __user * uptr)6628bf50399SGeorge Zhang static int vmci_host_do_ctx_add_notify(struct vmci_host_dev *vmci_host_dev,
6638bf50399SGeorge Zhang 				       const char *ioctl_name,
6648bf50399SGeorge Zhang 				       void __user *uptr)
6658bf50399SGeorge Zhang {
6668bf50399SGeorge Zhang 	struct vmci_ctx_info ar_info;
6678bf50399SGeorge Zhang 	struct vmci_ctx_info __user *info = uptr;
6688bf50399SGeorge Zhang 	s32 result;
6698bf50399SGeorge Zhang 	u32 cid;
6708bf50399SGeorge Zhang 
6718bf50399SGeorge Zhang 	if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
6728bf50399SGeorge Zhang 		vmci_ioctl_err("only valid for contexts\n");
6738bf50399SGeorge Zhang 		return -EINVAL;
6748bf50399SGeorge Zhang 	}
6758bf50399SGeorge Zhang 
6768bf50399SGeorge Zhang 	if (copy_from_user(&ar_info, uptr, sizeof(ar_info)))
6778bf50399SGeorge Zhang 		return -EFAULT;
6788bf50399SGeorge Zhang 
6798bf50399SGeorge Zhang 	cid = vmci_ctx_get_id(vmci_host_dev->context);
6808bf50399SGeorge Zhang 	result = vmci_ctx_add_notification(cid, ar_info.remote_cid);
6818bf50399SGeorge Zhang 
6828bf50399SGeorge Zhang 	return put_user(result, &info->result) ? -EFAULT : 0;
6838bf50399SGeorge Zhang }
6848bf50399SGeorge Zhang 
vmci_host_do_ctx_remove_notify(struct vmci_host_dev * vmci_host_dev,const char * ioctl_name,void __user * uptr)6858bf50399SGeorge Zhang static int vmci_host_do_ctx_remove_notify(struct vmci_host_dev *vmci_host_dev,
6868bf50399SGeorge Zhang 					  const char *ioctl_name,
6878bf50399SGeorge Zhang 					  void __user *uptr)
6888bf50399SGeorge Zhang {
6898bf50399SGeorge Zhang 	struct vmci_ctx_info ar_info;
6908bf50399SGeorge Zhang 	struct vmci_ctx_info __user *info = uptr;
6918bf50399SGeorge Zhang 	u32 cid;
6928bf50399SGeorge Zhang 	int result;
6938bf50399SGeorge Zhang 
6948bf50399SGeorge Zhang 	if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
6958bf50399SGeorge Zhang 		vmci_ioctl_err("only valid for contexts\n");
6968bf50399SGeorge Zhang 		return -EINVAL;
6978bf50399SGeorge Zhang 	}
6988bf50399SGeorge Zhang 
6998bf50399SGeorge Zhang 	if (copy_from_user(&ar_info, uptr, sizeof(ar_info)))
7008bf50399SGeorge Zhang 		return -EFAULT;
7018bf50399SGeorge Zhang 
7028bf50399SGeorge Zhang 	cid = vmci_ctx_get_id(vmci_host_dev->context);
7038bf50399SGeorge Zhang 	result = vmci_ctx_remove_notification(cid,
7048bf50399SGeorge Zhang 					      ar_info.remote_cid);
7058bf50399SGeorge Zhang 
7068bf50399SGeorge Zhang 	return put_user(result, &info->result) ? -EFAULT : 0;
7078bf50399SGeorge Zhang }
7088bf50399SGeorge Zhang 
vmci_host_do_ctx_get_cpt_state(struct vmci_host_dev * vmci_host_dev,const char * ioctl_name,void __user * uptr)7098bf50399SGeorge Zhang static int vmci_host_do_ctx_get_cpt_state(struct vmci_host_dev *vmci_host_dev,
7108bf50399SGeorge Zhang 					  const char *ioctl_name,
7118bf50399SGeorge Zhang 					  void __user *uptr)
7128bf50399SGeorge Zhang {
7138bf50399SGeorge Zhang 	struct vmci_ctx_chkpt_buf_info get_info;
7148bf50399SGeorge Zhang 	u32 cid;
7158bf50399SGeorge Zhang 	void *cpt_buf;
7168bf50399SGeorge Zhang 	int retval;
7178bf50399SGeorge Zhang 
7188bf50399SGeorge Zhang 	if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
7198bf50399SGeorge Zhang 		vmci_ioctl_err("only valid for contexts\n");
7208bf50399SGeorge Zhang 		return -EINVAL;
7218bf50399SGeorge Zhang 	}
7228bf50399SGeorge Zhang 
7238bf50399SGeorge Zhang 	if (copy_from_user(&get_info, uptr, sizeof(get_info)))
7248bf50399SGeorge Zhang 		return -EFAULT;
7258bf50399SGeorge Zhang 
7268bf50399SGeorge Zhang 	cid = vmci_ctx_get_id(vmci_host_dev->context);
7278bf50399SGeorge Zhang 	get_info.result = vmci_ctx_get_chkpt_state(cid, get_info.cpt_type,
7288bf50399SGeorge Zhang 						&get_info.buf_size, &cpt_buf);
7298bf50399SGeorge Zhang 	if (get_info.result == VMCI_SUCCESS && get_info.buf_size) {
7308bf50399SGeorge Zhang 		void __user *ubuf = (void __user *)(uintptr_t)get_info.cpt_buf;
7318bf50399SGeorge Zhang 		retval = copy_to_user(ubuf, cpt_buf, get_info.buf_size);
7328bf50399SGeorge Zhang 		kfree(cpt_buf);
7338bf50399SGeorge Zhang 
7348bf50399SGeorge Zhang 		if (retval)
7358bf50399SGeorge Zhang 			return -EFAULT;
7368bf50399SGeorge Zhang 	}
7378bf50399SGeorge Zhang 
7388bf50399SGeorge Zhang 	return copy_to_user(uptr, &get_info, sizeof(get_info)) ? -EFAULT : 0;
7398bf50399SGeorge Zhang }
7408bf50399SGeorge Zhang 
vmci_host_do_ctx_set_cpt_state(struct vmci_host_dev * vmci_host_dev,const char * ioctl_name,void __user * uptr)7418bf50399SGeorge Zhang static int vmci_host_do_ctx_set_cpt_state(struct vmci_host_dev *vmci_host_dev,
7428bf50399SGeorge Zhang 					  const char *ioctl_name,
7438bf50399SGeorge Zhang 					  void __user *uptr)
7448bf50399SGeorge Zhang {
7458bf50399SGeorge Zhang 	struct vmci_ctx_chkpt_buf_info set_info;
7468bf50399SGeorge Zhang 	u32 cid;
7478bf50399SGeorge Zhang 	void *cpt_buf;
7488bf50399SGeorge Zhang 	int retval;
7498bf50399SGeorge Zhang 
7508bf50399SGeorge Zhang 	if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
7518bf50399SGeorge Zhang 		vmci_ioctl_err("only valid for contexts\n");
7528bf50399SGeorge Zhang 		return -EINVAL;
7538bf50399SGeorge Zhang 	}
7548bf50399SGeorge Zhang 
7558bf50399SGeorge Zhang 	if (copy_from_user(&set_info, uptr, sizeof(set_info)))
7568bf50399SGeorge Zhang 		return -EFAULT;
7578bf50399SGeorge Zhang 
7588995fa1eSWen Yang 	cpt_buf = memdup_user((void __user *)(uintptr_t)set_info.cpt_buf,
7598995fa1eSWen Yang 				set_info.buf_size);
7608995fa1eSWen Yang 	if (IS_ERR(cpt_buf))
7618995fa1eSWen Yang 		return PTR_ERR(cpt_buf);
7628bf50399SGeorge Zhang 
7638bf50399SGeorge Zhang 	cid = vmci_ctx_get_id(vmci_host_dev->context);
7648bf50399SGeorge Zhang 	set_info.result = vmci_ctx_set_chkpt_state(cid, set_info.cpt_type,
7658bf50399SGeorge Zhang 						   set_info.buf_size, cpt_buf);
7668bf50399SGeorge Zhang 
7678bf50399SGeorge Zhang 	retval = copy_to_user(uptr, &set_info, sizeof(set_info)) ? -EFAULT : 0;
7688bf50399SGeorge Zhang 
7698bf50399SGeorge Zhang 	kfree(cpt_buf);
7708bf50399SGeorge Zhang 	return retval;
7718bf50399SGeorge Zhang }
7728bf50399SGeorge Zhang 
vmci_host_do_get_context_id(struct vmci_host_dev * vmci_host_dev,const char * ioctl_name,void __user * uptr)7738bf50399SGeorge Zhang static int vmci_host_do_get_context_id(struct vmci_host_dev *vmci_host_dev,
7748bf50399SGeorge Zhang 				       const char *ioctl_name,
7758bf50399SGeorge Zhang 				       void __user *uptr)
7768bf50399SGeorge Zhang {
7778bf50399SGeorge Zhang 	u32 __user *u32ptr = uptr;
7788bf50399SGeorge Zhang 
7798bf50399SGeorge Zhang 	return put_user(VMCI_HOST_CONTEXT_ID, u32ptr) ? -EFAULT : 0;
7808bf50399SGeorge Zhang }
7818bf50399SGeorge Zhang 
vmci_host_do_set_notify(struct vmci_host_dev * vmci_host_dev,const char * ioctl_name,void __user * uptr)7828bf50399SGeorge Zhang static int vmci_host_do_set_notify(struct vmci_host_dev *vmci_host_dev,
7838bf50399SGeorge Zhang 				   const char *ioctl_name,
7848bf50399SGeorge Zhang 				   void __user *uptr)
7858bf50399SGeorge Zhang {
7868bf50399SGeorge Zhang 	struct vmci_set_notify_info notify_info;
7878bf50399SGeorge Zhang 
7888bf50399SGeorge Zhang 	if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
7898bf50399SGeorge Zhang 		vmci_ioctl_err("only valid for contexts\n");
7908bf50399SGeorge Zhang 		return -EINVAL;
7918bf50399SGeorge Zhang 	}
7928bf50399SGeorge Zhang 
7938bf50399SGeorge Zhang 	if (copy_from_user(&notify_info, uptr, sizeof(notify_info)))
7948bf50399SGeorge Zhang 		return -EFAULT;
7958bf50399SGeorge Zhang 
7968bf50399SGeorge Zhang 	if (notify_info.notify_uva) {
7978bf50399SGeorge Zhang 		notify_info.result =
7988bf50399SGeorge Zhang 			vmci_host_setup_notify(vmci_host_dev->context,
7998bf50399SGeorge Zhang 					       notify_info.notify_uva);
8008bf50399SGeorge Zhang 	} else {
8018bf50399SGeorge Zhang 		vmci_ctx_unset_notify(vmci_host_dev->context);
8028bf50399SGeorge Zhang 		notify_info.result = VMCI_SUCCESS;
8038bf50399SGeorge Zhang 	}
8048bf50399SGeorge Zhang 
8058bf50399SGeorge Zhang 	return copy_to_user(uptr, &notify_info, sizeof(notify_info)) ?
8068bf50399SGeorge Zhang 		-EFAULT : 0;
8078bf50399SGeorge Zhang }
8088bf50399SGeorge Zhang 
vmci_host_do_notify_resource(struct vmci_host_dev * vmci_host_dev,const char * ioctl_name,void __user * uptr)8098bf50399SGeorge Zhang static int vmci_host_do_notify_resource(struct vmci_host_dev *vmci_host_dev,
8108bf50399SGeorge Zhang 					const char *ioctl_name,
8118bf50399SGeorge Zhang 					void __user *uptr)
8128bf50399SGeorge Zhang {
8138bf50399SGeorge Zhang 	struct vmci_dbell_notify_resource_info info;
8148bf50399SGeorge Zhang 	u32 cid;
8158bf50399SGeorge Zhang 
8168bf50399SGeorge Zhang 	if (vmci_host_dev->user_version < VMCI_VERSION_NOTIFY) {
8178bf50399SGeorge Zhang 		vmci_ioctl_err("invalid for current VMX versions\n");
8188bf50399SGeorge Zhang 		return -EINVAL;
8198bf50399SGeorge Zhang 	}
8208bf50399SGeorge Zhang 
8218bf50399SGeorge Zhang 	if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
8228bf50399SGeorge Zhang 		vmci_ioctl_err("only valid for contexts\n");
8238bf50399SGeorge Zhang 		return -EINVAL;
8248bf50399SGeorge Zhang 	}
8258bf50399SGeorge Zhang 
8268bf50399SGeorge Zhang 	if (copy_from_user(&info, uptr, sizeof(info)))
8278bf50399SGeorge Zhang 		return -EFAULT;
8288bf50399SGeorge Zhang 
8298bf50399SGeorge Zhang 	cid = vmci_ctx_get_id(vmci_host_dev->context);
8308bf50399SGeorge Zhang 
8318bf50399SGeorge Zhang 	switch (info.action) {
8328bf50399SGeorge Zhang 	case VMCI_NOTIFY_RESOURCE_ACTION_NOTIFY:
8338bf50399SGeorge Zhang 		if (info.resource == VMCI_NOTIFY_RESOURCE_DOOR_BELL) {
8348bf50399SGeorge Zhang 			u32 flags = VMCI_NO_PRIVILEGE_FLAGS;
8358bf50399SGeorge Zhang 			info.result = vmci_ctx_notify_dbell(cid, info.handle,
8368bf50399SGeorge Zhang 							    flags);
8378bf50399SGeorge Zhang 		} else {
8388bf50399SGeorge Zhang 			info.result = VMCI_ERROR_UNAVAILABLE;
8398bf50399SGeorge Zhang 		}
8408bf50399SGeorge Zhang 		break;
8418bf50399SGeorge Zhang 
8428bf50399SGeorge Zhang 	case VMCI_NOTIFY_RESOURCE_ACTION_CREATE:
8438bf50399SGeorge Zhang 		info.result = vmci_ctx_dbell_create(cid, info.handle);
8448bf50399SGeorge Zhang 		break;
8458bf50399SGeorge Zhang 
8468bf50399SGeorge Zhang 	case VMCI_NOTIFY_RESOURCE_ACTION_DESTROY:
8478bf50399SGeorge Zhang 		info.result = vmci_ctx_dbell_destroy(cid, info.handle);
8488bf50399SGeorge Zhang 		break;
8498bf50399SGeorge Zhang 
8508bf50399SGeorge Zhang 	default:
8518bf50399SGeorge Zhang 		vmci_ioctl_err("got unknown action (action=%d)\n",
8528bf50399SGeorge Zhang 			       info.action);
8538bf50399SGeorge Zhang 		info.result = VMCI_ERROR_INVALID_ARGS;
8548bf50399SGeorge Zhang 	}
8558bf50399SGeorge Zhang 
8568bf50399SGeorge Zhang 	return copy_to_user(uptr, &info, sizeof(info)) ? -EFAULT : 0;
8578bf50399SGeorge Zhang }
8588bf50399SGeorge Zhang 
vmci_host_do_recv_notifications(struct vmci_host_dev * vmci_host_dev,const char * ioctl_name,void __user * uptr)8598bf50399SGeorge Zhang static int vmci_host_do_recv_notifications(struct vmci_host_dev *vmci_host_dev,
8608bf50399SGeorge Zhang 					   const char *ioctl_name,
8618bf50399SGeorge Zhang 					   void __user *uptr)
8628bf50399SGeorge Zhang {
8638bf50399SGeorge Zhang 	struct vmci_ctx_notify_recv_info info;
8648bf50399SGeorge Zhang 	struct vmci_handle_arr *db_handle_array;
8658bf50399SGeorge Zhang 	struct vmci_handle_arr *qp_handle_array;
8668bf50399SGeorge Zhang 	void __user *ubuf;
8678bf50399SGeorge Zhang 	u32 cid;
8688bf50399SGeorge Zhang 	int retval = 0;
8698bf50399SGeorge Zhang 
8708bf50399SGeorge Zhang 	if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
8718bf50399SGeorge Zhang 		vmci_ioctl_err("only valid for contexts\n");
8728bf50399SGeorge Zhang 		return -EINVAL;
8738bf50399SGeorge Zhang 	}
8748bf50399SGeorge Zhang 
8758bf50399SGeorge Zhang 	if (vmci_host_dev->user_version < VMCI_VERSION_NOTIFY) {
8768bf50399SGeorge Zhang 		vmci_ioctl_err("not supported for the current vmx version\n");
8778bf50399SGeorge Zhang 		return -EINVAL;
8788bf50399SGeorge Zhang 	}
8798bf50399SGeorge Zhang 
8808bf50399SGeorge Zhang 	if (copy_from_user(&info, uptr, sizeof(info)))
8818bf50399SGeorge Zhang 		return -EFAULT;
8828bf50399SGeorge Zhang 
8838bf50399SGeorge Zhang 	if ((info.db_handle_buf_size && !info.db_handle_buf_uva) ||
8848bf50399SGeorge Zhang 	    (info.qp_handle_buf_size && !info.qp_handle_buf_uva)) {
8858bf50399SGeorge Zhang 		return -EINVAL;
8868bf50399SGeorge Zhang 	}
8878bf50399SGeorge Zhang 
8888bf50399SGeorge Zhang 	cid = vmci_ctx_get_id(vmci_host_dev->context);
8898bf50399SGeorge Zhang 
8908bf50399SGeorge Zhang 	info.result = vmci_ctx_rcv_notifications_get(cid,
8918bf50399SGeorge Zhang 				&db_handle_array, &qp_handle_array);
8928bf50399SGeorge Zhang 	if (info.result != VMCI_SUCCESS)
8938bf50399SGeorge Zhang 		return copy_to_user(uptr, &info, sizeof(info)) ? -EFAULT : 0;
8948bf50399SGeorge Zhang 
8958bf50399SGeorge Zhang 	ubuf = (void __user *)(uintptr_t)info.db_handle_buf_uva;
8968bf50399SGeorge Zhang 	info.result = drv_cp_harray_to_user(ubuf, &info.db_handle_buf_size,
8978bf50399SGeorge Zhang 					    db_handle_array, &retval);
8988bf50399SGeorge Zhang 	if (info.result == VMCI_SUCCESS && !retval) {
8998bf50399SGeorge Zhang 		ubuf = (void __user *)(uintptr_t)info.qp_handle_buf_uva;
9008bf50399SGeorge Zhang 		info.result = drv_cp_harray_to_user(ubuf,
9018bf50399SGeorge Zhang 						    &info.qp_handle_buf_size,
9028bf50399SGeorge Zhang 						    qp_handle_array, &retval);
9038bf50399SGeorge Zhang 	}
9048bf50399SGeorge Zhang 
9058bf50399SGeorge Zhang 	if (!retval && copy_to_user(uptr, &info, sizeof(info)))
9068bf50399SGeorge Zhang 		retval = -EFAULT;
9078bf50399SGeorge Zhang 
9088bf50399SGeorge Zhang 	vmci_ctx_rcv_notifications_release(cid,
9098bf50399SGeorge Zhang 				db_handle_array, qp_handle_array,
9108bf50399SGeorge Zhang 				info.result == VMCI_SUCCESS && !retval);
9118bf50399SGeorge Zhang 
9128bf50399SGeorge Zhang 	return retval;
9138bf50399SGeorge Zhang }
9148bf50399SGeorge Zhang 
vmci_host_unlocked_ioctl(struct file * filp,unsigned int iocmd,unsigned long ioarg)9158bf50399SGeorge Zhang static long vmci_host_unlocked_ioctl(struct file *filp,
9168bf50399SGeorge Zhang 				     unsigned int iocmd, unsigned long ioarg)
9178bf50399SGeorge Zhang {
9188bf50399SGeorge Zhang #define VMCI_DO_IOCTL(ioctl_name, ioctl_fn) do {			\
9197b7d2261SGleb Fotengauer-Malinovskiy 		char *name = "IOCTL_VMCI_" # ioctl_name;		\
9208bf50399SGeorge Zhang 		return vmci_host_do_ ## ioctl_fn(			\
9218bf50399SGeorge Zhang 			vmci_host_dev, name, uptr);			\
9228bf50399SGeorge Zhang 	} while (0)
9238bf50399SGeorge Zhang 
9248bf50399SGeorge Zhang 	struct vmci_host_dev *vmci_host_dev = filp->private_data;
9258bf50399SGeorge Zhang 	void __user *uptr = (void __user *)ioarg;
9268bf50399SGeorge Zhang 
9278bf50399SGeorge Zhang 	switch (iocmd) {
9288bf50399SGeorge Zhang 	case IOCTL_VMCI_INIT_CONTEXT:
9298bf50399SGeorge Zhang 		VMCI_DO_IOCTL(INIT_CONTEXT, init_context);
9308bf50399SGeorge Zhang 	case IOCTL_VMCI_DATAGRAM_SEND:
9318bf50399SGeorge Zhang 		VMCI_DO_IOCTL(DATAGRAM_SEND, send_datagram);
9328bf50399SGeorge Zhang 	case IOCTL_VMCI_DATAGRAM_RECEIVE:
9338bf50399SGeorge Zhang 		VMCI_DO_IOCTL(DATAGRAM_RECEIVE, receive_datagram);
9348bf50399SGeorge Zhang 	case IOCTL_VMCI_QUEUEPAIR_ALLOC:
9358bf50399SGeorge Zhang 		VMCI_DO_IOCTL(QUEUEPAIR_ALLOC, alloc_queuepair);
9368bf50399SGeorge Zhang 	case IOCTL_VMCI_QUEUEPAIR_SETVA:
9378bf50399SGeorge Zhang 		VMCI_DO_IOCTL(QUEUEPAIR_SETVA, queuepair_setva);
9388bf50399SGeorge Zhang 	case IOCTL_VMCI_QUEUEPAIR_SETPAGEFILE:
9398bf50399SGeorge Zhang 		VMCI_DO_IOCTL(QUEUEPAIR_SETPAGEFILE, queuepair_setpf);
9408bf50399SGeorge Zhang 	case IOCTL_VMCI_QUEUEPAIR_DETACH:
9418bf50399SGeorge Zhang 		VMCI_DO_IOCTL(QUEUEPAIR_DETACH, qp_detach);
9428bf50399SGeorge Zhang 	case IOCTL_VMCI_CTX_ADD_NOTIFICATION:
9438bf50399SGeorge Zhang 		VMCI_DO_IOCTL(CTX_ADD_NOTIFICATION, ctx_add_notify);
9448bf50399SGeorge Zhang 	case IOCTL_VMCI_CTX_REMOVE_NOTIFICATION:
9458bf50399SGeorge Zhang 		VMCI_DO_IOCTL(CTX_REMOVE_NOTIFICATION, ctx_remove_notify);
9468bf50399SGeorge Zhang 	case IOCTL_VMCI_CTX_GET_CPT_STATE:
9478bf50399SGeorge Zhang 		VMCI_DO_IOCTL(CTX_GET_CPT_STATE, ctx_get_cpt_state);
9488bf50399SGeorge Zhang 	case IOCTL_VMCI_CTX_SET_CPT_STATE:
9498bf50399SGeorge Zhang 		VMCI_DO_IOCTL(CTX_SET_CPT_STATE, ctx_set_cpt_state);
9508bf50399SGeorge Zhang 	case IOCTL_VMCI_GET_CONTEXT_ID:
9518bf50399SGeorge Zhang 		VMCI_DO_IOCTL(GET_CONTEXT_ID, get_context_id);
9528bf50399SGeorge Zhang 	case IOCTL_VMCI_SET_NOTIFY:
9538bf50399SGeorge Zhang 		VMCI_DO_IOCTL(SET_NOTIFY, set_notify);
9548bf50399SGeorge Zhang 	case IOCTL_VMCI_NOTIFY_RESOURCE:
9558bf50399SGeorge Zhang 		VMCI_DO_IOCTL(NOTIFY_RESOURCE, notify_resource);
9568bf50399SGeorge Zhang 	case IOCTL_VMCI_NOTIFICATIONS_RECEIVE:
9578bf50399SGeorge Zhang 		VMCI_DO_IOCTL(NOTIFICATIONS_RECEIVE, recv_notifications);
9588bf50399SGeorge Zhang 
9598bf50399SGeorge Zhang 	case IOCTL_VMCI_VERSION:
9608bf50399SGeorge Zhang 	case IOCTL_VMCI_VERSION2:
9618bf50399SGeorge Zhang 		return vmci_host_get_version(vmci_host_dev, iocmd, uptr);
9628bf50399SGeorge Zhang 
9638bf50399SGeorge Zhang 	default:
9648bf50399SGeorge Zhang 		pr_devel("%s: Unknown ioctl (iocmd=%d)\n", __func__, iocmd);
9658bf50399SGeorge Zhang 		return -EINVAL;
9668bf50399SGeorge Zhang 	}
9678bf50399SGeorge Zhang 
9688bf50399SGeorge Zhang #undef VMCI_DO_IOCTL
9698bf50399SGeorge Zhang }
9708bf50399SGeorge Zhang 
9718bf50399SGeorge Zhang static const struct file_operations vmuser_fops = {
9728bf50399SGeorge Zhang 	.owner		= THIS_MODULE,
9738bf50399SGeorge Zhang 	.open		= vmci_host_open,
9748bf50399SGeorge Zhang 	.release	= vmci_host_close,
9758bf50399SGeorge Zhang 	.poll		= vmci_host_poll,
9768bf50399SGeorge Zhang 	.unlocked_ioctl	= vmci_host_unlocked_ioctl,
9771832f2d8SArnd Bergmann 	.compat_ioctl	= compat_ptr_ioctl,
9788bf50399SGeorge Zhang };
9798bf50399SGeorge Zhang 
9808bf50399SGeorge Zhang static struct miscdevice vmci_host_miscdev = {
9818bf50399SGeorge Zhang 	 .name = "vmci",
9828bf50399SGeorge Zhang 	 .minor = MISC_DYNAMIC_MINOR,
9838bf50399SGeorge Zhang 	 .fops = &vmuser_fops,
9848bf50399SGeorge Zhang };
9858bf50399SGeorge Zhang 
vmci_host_init(void)9868bf50399SGeorge Zhang int __init vmci_host_init(void)
9878bf50399SGeorge Zhang {
9888bf50399SGeorge Zhang 	int error;
9898bf50399SGeorge Zhang 
9908bf50399SGeorge Zhang 	host_context = vmci_ctx_create(VMCI_HOST_CONTEXT_ID,
9918bf50399SGeorge Zhang 					VMCI_DEFAULT_PROC_PRIVILEGE_FLAGS,
9928bf50399SGeorge Zhang 					-1, VMCI_VERSION, NULL);
9938bf50399SGeorge Zhang 	if (IS_ERR(host_context)) {
9948bf50399SGeorge Zhang 		error = PTR_ERR(host_context);
9958bf50399SGeorge Zhang 		pr_warn("Failed to initialize VMCIContext (error%d)\n",
9968bf50399SGeorge Zhang 			error);
9978bf50399SGeorge Zhang 		return error;
9988bf50399SGeorge Zhang 	}
9998bf50399SGeorge Zhang 
10008bf50399SGeorge Zhang 	error = misc_register(&vmci_host_miscdev);
10018bf50399SGeorge Zhang 	if (error) {
10028bf50399SGeorge Zhang 		pr_warn("Module registration error (name=%s, major=%d, minor=%d, err=%d)\n",
10038bf50399SGeorge Zhang 			vmci_host_miscdev.name,
10048bf50399SGeorge Zhang 			MISC_MAJOR, vmci_host_miscdev.minor,
10058bf50399SGeorge Zhang 			error);
10068bf50399SGeorge Zhang 		pr_warn("Unable to initialize host personality\n");
10078bf50399SGeorge Zhang 		vmci_ctx_destroy(host_context);
10088bf50399SGeorge Zhang 		return error;
10098bf50399SGeorge Zhang 	}
10108bf50399SGeorge Zhang 
10118bf50399SGeorge Zhang 	pr_info("VMCI host device registered (name=%s, major=%d, minor=%d)\n",
10128bf50399SGeorge Zhang 		vmci_host_miscdev.name, MISC_MAJOR, vmci_host_miscdev.minor);
10138bf50399SGeorge Zhang 
10148bf50399SGeorge Zhang 	vmci_host_device_initialized = true;
10158bf50399SGeorge Zhang 	return 0;
10168bf50399SGeorge Zhang }
10178bf50399SGeorge Zhang 
vmci_host_exit(void)10188bf50399SGeorge Zhang void __exit vmci_host_exit(void)
10198bf50399SGeorge Zhang {
10208bf50399SGeorge Zhang 	vmci_host_device_initialized = false;
10218bf50399SGeorge Zhang 
1022f368ed60SGreg Kroah-Hartman 	misc_deregister(&vmci_host_miscdev);
10238bf50399SGeorge Zhang 	vmci_ctx_destroy(host_context);
10248bf50399SGeorge Zhang 	vmci_qp_broker_exit();
10258bf50399SGeorge Zhang 
10268bf50399SGeorge Zhang 	pr_debug("VMCI host driver module unloaded\n");
10278bf50399SGeorge Zhang }
1028