vmci_host.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  1. /*
  2. * VMware VMCI Driver
  3. *
  4. * Copyright (C) 2012 VMware, Inc. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation version 2 and no later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * for more details.
  14. */
  15. #include <linux/vmw_vmci_defs.h>
  16. #include <linux/vmw_vmci_api.h>
  17. #include <linux/moduleparam.h>
  18. #include <linux/miscdevice.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/highmem.h>
  21. #include <linux/atomic.h>
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/mutex.h>
  25. #include <linux/sched.h>
  26. #include <linux/slab.h>
  27. #include <linux/file.h>
  28. #include <linux/init.h>
  29. #include <linux/poll.h>
  30. #include <linux/pci.h>
  31. #include <linux/smp.h>
  32. #include <linux/fs.h>
  33. #include <linux/io.h>
  34. #include "vmci_handle_array.h"
  35. #include "vmci_queue_pair.h"
  36. #include "vmci_datagram.h"
  37. #include "vmci_doorbell.h"
  38. #include "vmci_resource.h"
  39. #include "vmci_context.h"
  40. #include "vmci_driver.h"
  41. #include "vmci_event.h"
  42. #define VMCI_UTIL_NUM_RESOURCES 1
  43. enum {
  44. VMCI_NOTIFY_RESOURCE_QUEUE_PAIR = 0,
  45. VMCI_NOTIFY_RESOURCE_DOOR_BELL = 1,
  46. };
  47. enum {
  48. VMCI_NOTIFY_RESOURCE_ACTION_NOTIFY = 0,
  49. VMCI_NOTIFY_RESOURCE_ACTION_CREATE = 1,
  50. VMCI_NOTIFY_RESOURCE_ACTION_DESTROY = 2,
  51. };
  52. /*
  53. * VMCI driver initialization. This block can also be used to
  54. * pass initial group membership etc.
  55. */
  56. struct vmci_init_blk {
  57. u32 cid;
  58. u32 flags;
  59. };
  60. /* VMCIqueue_pairAllocInfo_VMToVM */
  61. struct vmci_qp_alloc_info_vmvm {
  62. struct vmci_handle handle;
  63. u32 peer;
  64. u32 flags;
  65. u64 produce_size;
  66. u64 consume_size;
  67. u64 produce_page_file; /* User VA. */
  68. u64 consume_page_file; /* User VA. */
  69. u64 produce_page_file_size; /* Size of the file name array. */
  70. u64 consume_page_file_size; /* Size of the file name array. */
  71. s32 result;
  72. u32 _pad;
  73. };
  74. /* VMCISetNotifyInfo: Used to pass notify flag's address to the host driver. */
  75. struct vmci_set_notify_info {
  76. u64 notify_uva;
  77. s32 result;
  78. u32 _pad;
  79. };
  80. /*
  81. * Per-instance host state
  82. */
  83. struct vmci_host_dev {
  84. struct vmci_ctx *context;
  85. int user_version;
  86. enum vmci_obj_type ct_type;
  87. struct mutex lock; /* Mutex lock for vmci context access */
  88. };
  89. static struct vmci_ctx *host_context;
  90. static bool vmci_host_device_initialized;
  91. static atomic_t vmci_host_active_users = ATOMIC_INIT(0);
  92. /*
  93. * Determines whether the VMCI host personality is
  94. * available. Since the core functionality of the host driver is
  95. * always present, all guests could possibly use the host
  96. * personality. However, to minimize the deviation from the
  97. * pre-unified driver state of affairs, we only consider the host
  98. * device active if there is no active guest device or if there
  99. * are VMX'en with active VMCI contexts using the host device.
  100. */
  101. bool vmci_host_code_active(void)
  102. {
  103. return vmci_host_device_initialized &&
  104. (!vmci_guest_code_active() ||
  105. atomic_read(&vmci_host_active_users) > 0);
  106. }
  107. /*
  108. * Called on open of /dev/vmci.
  109. */
  110. static int vmci_host_open(struct inode *inode, struct file *filp)
  111. {
  112. struct vmci_host_dev *vmci_host_dev;
  113. vmci_host_dev = kzalloc(sizeof(struct vmci_host_dev), GFP_KERNEL);
  114. if (vmci_host_dev == NULL)
  115. return -ENOMEM;
  116. vmci_host_dev->ct_type = VMCIOBJ_NOT_SET;
  117. mutex_init(&vmci_host_dev->lock);
  118. filp->private_data = vmci_host_dev;
  119. return 0;
  120. }
  121. /*
  122. * Called on close of /dev/vmci, most often when the process
  123. * exits.
  124. */
  125. static int vmci_host_close(struct inode *inode, struct file *filp)
  126. {
  127. struct vmci_host_dev *vmci_host_dev = filp->private_data;
  128. if (vmci_host_dev->ct_type == VMCIOBJ_CONTEXT) {
  129. vmci_ctx_destroy(vmci_host_dev->context);
  130. vmci_host_dev->context = NULL;
  131. /*
  132. * The number of active contexts is used to track whether any
  133. * VMX'en are using the host personality. It is incremented when
  134. * a context is created through the IOCTL_VMCI_INIT_CONTEXT
  135. * ioctl.
  136. */
  137. atomic_dec(&vmci_host_active_users);
  138. }
  139. vmci_host_dev->ct_type = VMCIOBJ_NOT_SET;
  140. kfree(vmci_host_dev);
  141. filp->private_data = NULL;
  142. return 0;
  143. }
  144. /*
  145. * This is used to wake up the VMX when a VMCI call arrives, or
  146. * to wake up select() or poll() at the next clock tick.
  147. */
  148. static unsigned int vmci_host_poll(struct file *filp, poll_table *wait)
  149. {
  150. struct vmci_host_dev *vmci_host_dev = filp->private_data;
  151. struct vmci_ctx *context = vmci_host_dev->context;
  152. unsigned int mask = 0;
  153. if (vmci_host_dev->ct_type == VMCIOBJ_CONTEXT) {
  154. /* Check for VMCI calls to this VM context. */
  155. if (wait)
  156. poll_wait(filp, &context->host_context.wait_queue,
  157. wait);
  158. spin_lock(&context->lock);
  159. if (context->pending_datagrams > 0 ||
  160. vmci_handle_arr_get_size(
  161. context->pending_doorbell_array) > 0) {
  162. mask = POLLIN;
  163. }
  164. spin_unlock(&context->lock);
  165. }
  166. return mask;
  167. }
  168. /*
  169. * Copies the handles of a handle array into a user buffer, and
  170. * returns the new length in userBufferSize. If the copy to the
  171. * user buffer fails, the functions still returns VMCI_SUCCESS,
  172. * but retval != 0.
  173. */
  174. static int drv_cp_harray_to_user(void __user *user_buf_uva,
  175. u64 *user_buf_size,
  176. struct vmci_handle_arr *handle_array,
  177. int *retval)
  178. {
  179. u32 array_size = 0;
  180. struct vmci_handle *handles;
  181. if (handle_array)
  182. array_size = vmci_handle_arr_get_size(handle_array);
  183. if (array_size * sizeof(*handles) > *user_buf_size)
  184. return VMCI_ERROR_MORE_DATA;
  185. *user_buf_size = array_size * sizeof(*handles);
  186. if (*user_buf_size)
  187. *retval = copy_to_user(user_buf_uva,
  188. vmci_handle_arr_get_handles
  189. (handle_array), *user_buf_size);
  190. return VMCI_SUCCESS;
  191. }
  192. /*
  193. * Sets up a given context for notify to work. Calls drv_map_bool_ptr()
  194. * which maps the notify boolean in user VA in kernel space.
  195. */
  196. static int vmci_host_setup_notify(struct vmci_ctx *context,
  197. unsigned long uva)
  198. {
  199. struct page *page;
  200. int retval;
  201. if (context->notify_page) {
  202. pr_devel("%s: Notify mechanism is already set up\n", __func__);
  203. return VMCI_ERROR_DUPLICATE_ENTRY;
  204. }
  205. /*
  206. * We are using 'bool' internally, but let's make sure we explicit
  207. * about the size.
  208. */
  209. BUILD_BUG_ON(sizeof(bool) != sizeof(u8));
  210. if (!access_ok(VERIFY_WRITE, (void __user *)uva, sizeof(u8)))
  211. return VMCI_ERROR_GENERIC;
  212. /*
  213. * Lock physical page backing a given user VA.
  214. */
  215. retval = get_user_pages_fast(PAGE_ALIGN(uva), 1, 1, &page);
  216. if (retval != 1)
  217. return VMCI_ERROR_GENERIC;
  218. /*
  219. * Map the locked page and set up notify pointer.
  220. */
  221. context->notify = kmap(page) + (uva & (PAGE_SIZE - 1));
  222. vmci_ctx_check_signal_notify(context);
  223. return VMCI_SUCCESS;
  224. }
  225. static int vmci_host_get_version(struct vmci_host_dev *vmci_host_dev,
  226. unsigned int cmd, void __user *uptr)
  227. {
  228. if (cmd == IOCTL_VMCI_VERSION2) {
  229. int __user *vptr = uptr;
  230. if (get_user(vmci_host_dev->user_version, vptr))
  231. return -EFAULT;
  232. }
  233. /*
  234. * The basic logic here is:
  235. *
  236. * If the user sends in a version of 0 tell it our version.
  237. * If the user didn't send in a version, tell it our version.
  238. * If the user sent in an old version, tell it -its- version.
  239. * If the user sent in an newer version, tell it our version.
  240. *
  241. * The rationale behind telling the caller its version is that
  242. * Workstation 6.5 required that VMX and VMCI kernel module were
  243. * version sync'd. All new VMX users will be programmed to
  244. * handle the VMCI kernel module version.
  245. */
  246. if (vmci_host_dev->user_version > 0 &&
  247. vmci_host_dev->user_version < VMCI_VERSION_HOSTQP) {
  248. return vmci_host_dev->user_version;
  249. }
  250. return VMCI_VERSION;
  251. }
  252. #define vmci_ioctl_err(fmt, ...) \
  253. pr_devel("%s: " fmt, ioctl_name, ##__VA_ARGS__)
  254. static int vmci_host_do_init_context(struct vmci_host_dev *vmci_host_dev,
  255. const char *ioctl_name,
  256. void __user *uptr)
  257. {
  258. struct vmci_init_blk init_block;
  259. const struct cred *cred;
  260. int retval;
  261. if (copy_from_user(&init_block, uptr, sizeof(init_block))) {
  262. vmci_ioctl_err("error reading init block\n");
  263. return -EFAULT;
  264. }
  265. mutex_lock(&vmci_host_dev->lock);
  266. if (vmci_host_dev->ct_type != VMCIOBJ_NOT_SET) {
  267. vmci_ioctl_err("received VMCI init on initialized handle\n");
  268. retval = -EINVAL;
  269. goto out;
  270. }
  271. if (init_block.flags & ~VMCI_PRIVILEGE_FLAG_RESTRICTED) {
  272. vmci_ioctl_err("unsupported VMCI restriction flag\n");
  273. retval = -EINVAL;
  274. goto out;
  275. }
  276. cred = get_current_cred();
  277. vmci_host_dev->context = vmci_ctx_create(init_block.cid,
  278. init_block.flags, 0,
  279. vmci_host_dev->user_version,
  280. cred);
  281. put_cred(cred);
  282. if (IS_ERR(vmci_host_dev->context)) {
  283. retval = PTR_ERR(vmci_host_dev->context);
  284. vmci_ioctl_err("error initializing context\n");
  285. goto out;
  286. }
  287. /*
  288. * Copy cid to userlevel, we do this to allow the VMX
  289. * to enforce its policy on cid generation.
  290. */
  291. init_block.cid = vmci_ctx_get_id(vmci_host_dev->context);
  292. if (copy_to_user(uptr, &init_block, sizeof(init_block))) {
  293. vmci_ctx_destroy(vmci_host_dev->context);
  294. vmci_host_dev->context = NULL;
  295. vmci_ioctl_err("error writing init block\n");
  296. retval = -EFAULT;
  297. goto out;
  298. }
  299. vmci_host_dev->ct_type = VMCIOBJ_CONTEXT;
  300. atomic_inc(&vmci_host_active_users);
  301. retval = 0;
  302. out:
  303. mutex_unlock(&vmci_host_dev->lock);
  304. return retval;
  305. }
  306. static int vmci_host_do_send_datagram(struct vmci_host_dev *vmci_host_dev,
  307. const char *ioctl_name,
  308. void __user *uptr)
  309. {
  310. struct vmci_datagram_snd_rcv_info send_info;
  311. struct vmci_datagram *dg = NULL;
  312. u32 cid;
  313. if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
  314. vmci_ioctl_err("only valid for contexts\n");
  315. return -EINVAL;
  316. }
  317. if (copy_from_user(&send_info, uptr, sizeof(send_info)))
  318. return -EFAULT;
  319. if (send_info.len > VMCI_MAX_DG_SIZE) {
  320. vmci_ioctl_err("datagram is too big (size=%d)\n",
  321. send_info.len);
  322. return -EINVAL;
  323. }
  324. if (send_info.len < sizeof(*dg)) {
  325. vmci_ioctl_err("datagram is too small (size=%d)\n",
  326. send_info.len);
  327. return -EINVAL;
  328. }
  329. dg = kmalloc(send_info.len, GFP_KERNEL);
  330. if (!dg) {
  331. vmci_ioctl_err(
  332. "cannot allocate memory to dispatch datagram\n");
  333. return -ENOMEM;
  334. }
  335. if (copy_from_user(dg, (void __user *)(uintptr_t)send_info.addr,
  336. send_info.len)) {
  337. vmci_ioctl_err("error getting datagram\n");
  338. kfree(dg);
  339. return -EFAULT;
  340. }
  341. pr_devel("Datagram dst (handle=0x%x:0x%x) src (handle=0x%x:0x%x), payload (size=%llu bytes)\n",
  342. dg->dst.context, dg->dst.resource,
  343. dg->src.context, dg->src.resource,
  344. (unsigned long long)dg->payload_size);
  345. /* Get source context id. */
  346. cid = vmci_ctx_get_id(vmci_host_dev->context);
  347. send_info.result = vmci_datagram_dispatch(cid, dg, true);
  348. kfree(dg);
  349. return copy_to_user(uptr, &send_info, sizeof(send_info)) ? -EFAULT : 0;
  350. }
  351. static int vmci_host_do_receive_datagram(struct vmci_host_dev *vmci_host_dev,
  352. const char *ioctl_name,
  353. void __user *uptr)
  354. {
  355. struct vmci_datagram_snd_rcv_info recv_info;
  356. struct vmci_datagram *dg = NULL;
  357. int retval;
  358. size_t size;
  359. if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
  360. vmci_ioctl_err("only valid for contexts\n");
  361. return -EINVAL;
  362. }
  363. if (copy_from_user(&recv_info, uptr, sizeof(recv_info)))
  364. return -EFAULT;
  365. size = recv_info.len;
  366. recv_info.result = vmci_ctx_dequeue_datagram(vmci_host_dev->context,
  367. &size, &dg);
  368. if (recv_info.result >= VMCI_SUCCESS) {
  369. void __user *ubuf = (void __user *)(uintptr_t)recv_info.addr;
  370. retval = copy_to_user(ubuf, dg, VMCI_DG_SIZE(dg));
  371. kfree(dg);
  372. if (retval != 0)
  373. return -EFAULT;
  374. }
  375. return copy_to_user(uptr, &recv_info, sizeof(recv_info)) ? -EFAULT : 0;
  376. }
  377. static int vmci_host_do_alloc_queuepair(struct vmci_host_dev *vmci_host_dev,
  378. const char *ioctl_name,
  379. void __user *uptr)
  380. {
  381. struct vmci_handle handle;
  382. int vmci_status;
  383. int __user *retptr;
  384. u32 cid;
  385. if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
  386. vmci_ioctl_err("only valid for contexts\n");
  387. return -EINVAL;
  388. }
  389. cid = vmci_ctx_get_id(vmci_host_dev->context);
  390. if (vmci_host_dev->user_version < VMCI_VERSION_NOVMVM) {
  391. struct vmci_qp_alloc_info_vmvm alloc_info;
  392. struct vmci_qp_alloc_info_vmvm __user *info = uptr;
  393. if (copy_from_user(&alloc_info, uptr, sizeof(alloc_info)))
  394. return -EFAULT;
  395. handle = alloc_info.handle;
  396. retptr = &info->result;
  397. vmci_status = vmci_qp_broker_alloc(alloc_info.handle,
  398. alloc_info.peer,
  399. alloc_info.flags,
  400. VMCI_NO_PRIVILEGE_FLAGS,
  401. alloc_info.produce_size,
  402. alloc_info.consume_size,
  403. NULL,
  404. vmci_host_dev->context);
  405. if (vmci_status == VMCI_SUCCESS)
  406. vmci_status = VMCI_SUCCESS_QUEUEPAIR_CREATE;
  407. } else {
  408. struct vmci_qp_alloc_info alloc_info;
  409. struct vmci_qp_alloc_info __user *info = uptr;
  410. struct vmci_qp_page_store page_store;
  411. if (copy_from_user(&alloc_info, uptr, sizeof(alloc_info)))
  412. return -EFAULT;
  413. handle = alloc_info.handle;
  414. retptr = &info->result;
  415. page_store.pages = alloc_info.ppn_va;
  416. page_store.len = alloc_info.num_ppns;
  417. vmci_status = vmci_qp_broker_alloc(alloc_info.handle,
  418. alloc_info.peer,
  419. alloc_info.flags,
  420. VMCI_NO_PRIVILEGE_FLAGS,
  421. alloc_info.produce_size,
  422. alloc_info.consume_size,
  423. &page_store,
  424. vmci_host_dev->context);
  425. }
  426. if (put_user(vmci_status, retptr)) {
  427. if (vmci_status >= VMCI_SUCCESS) {
  428. vmci_status = vmci_qp_broker_detach(handle,
  429. vmci_host_dev->context);
  430. }
  431. return -EFAULT;
  432. }
  433. return 0;
  434. }
  435. static int vmci_host_do_queuepair_setva(struct vmci_host_dev *vmci_host_dev,
  436. const char *ioctl_name,
  437. void __user *uptr)
  438. {
  439. struct vmci_qp_set_va_info set_va_info;
  440. struct vmci_qp_set_va_info __user *info = uptr;
  441. s32 result;
  442. if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
  443. vmci_ioctl_err("only valid for contexts\n");
  444. return -EINVAL;
  445. }
  446. if (vmci_host_dev->user_version < VMCI_VERSION_NOVMVM) {
  447. vmci_ioctl_err("is not allowed\n");
  448. return -EINVAL;
  449. }
  450. if (copy_from_user(&set_va_info, uptr, sizeof(set_va_info)))
  451. return -EFAULT;
  452. if (set_va_info.va) {
  453. /*
  454. * VMX is passing down a new VA for the queue
  455. * pair mapping.
  456. */
  457. result = vmci_qp_broker_map(set_va_info.handle,
  458. vmci_host_dev->context,
  459. set_va_info.va);
  460. } else {
  461. /*
  462. * The queue pair is about to be unmapped by
  463. * the VMX.
  464. */
  465. result = vmci_qp_broker_unmap(set_va_info.handle,
  466. vmci_host_dev->context, 0);
  467. }
  468. return put_user(result, &info->result) ? -EFAULT : 0;
  469. }
  470. static int vmci_host_do_queuepair_setpf(struct vmci_host_dev *vmci_host_dev,
  471. const char *ioctl_name,
  472. void __user *uptr)
  473. {
  474. struct vmci_qp_page_file_info page_file_info;
  475. struct vmci_qp_page_file_info __user *info = uptr;
  476. s32 result;
  477. if (vmci_host_dev->user_version < VMCI_VERSION_HOSTQP ||
  478. vmci_host_dev->user_version >= VMCI_VERSION_NOVMVM) {
  479. vmci_ioctl_err("not supported on this VMX (version=%d)\n",
  480. vmci_host_dev->user_version);
  481. return -EINVAL;
  482. }
  483. if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
  484. vmci_ioctl_err("only valid for contexts\n");
  485. return -EINVAL;
  486. }
  487. if (copy_from_user(&page_file_info, uptr, sizeof(*info)))
  488. return -EFAULT;
  489. /*
  490. * Communicate success pre-emptively to the caller. Note that the
  491. * basic premise is that it is incumbent upon the caller not to look at
  492. * the info.result field until after the ioctl() returns. And then,
  493. * only if the ioctl() result indicates no error. We send up the
  494. * SUCCESS status before calling SetPageStore() store because failing
  495. * to copy up the result code means unwinding the SetPageStore().
  496. *
  497. * It turns out the logic to unwind a SetPageStore() opens a can of
  498. * worms. For example, if a host had created the queue_pair and a
  499. * guest attaches and SetPageStore() is successful but writing success
  500. * fails, then ... the host has to be stopped from writing (anymore)
  501. * data into the queue_pair. That means an additional test in the
  502. * VMCI_Enqueue() code path. Ugh.
  503. */
  504. if (put_user(VMCI_SUCCESS, &info->result)) {
  505. /*
  506. * In this case, we can't write a result field of the
  507. * caller's info block. So, we don't even try to
  508. * SetPageStore().
  509. */
  510. return -EFAULT;
  511. }
  512. result = vmci_qp_broker_set_page_store(page_file_info.handle,
  513. page_file_info.produce_va,
  514. page_file_info.consume_va,
  515. vmci_host_dev->context);
  516. if (result < VMCI_SUCCESS) {
  517. if (put_user(result, &info->result)) {
  518. /*
  519. * Note that in this case the SetPageStore()
  520. * call failed but we were unable to
  521. * communicate that to the caller (because the
  522. * copy_to_user() call failed). So, if we
  523. * simply return an error (in this case
  524. * -EFAULT) then the caller will know that the
  525. * SetPageStore failed even though we couldn't
  526. * put the result code in the result field and
  527. * indicate exactly why it failed.
  528. *
  529. * That says nothing about the issue where we
  530. * were once able to write to the caller's info
  531. * memory and now can't. Something more
  532. * serious is probably going on than the fact
  533. * that SetPageStore() didn't work.
  534. */
  535. return -EFAULT;
  536. }
  537. }
  538. return 0;
  539. }
  540. static int vmci_host_do_qp_detach(struct vmci_host_dev *vmci_host_dev,
  541. const char *ioctl_name,
  542. void __user *uptr)
  543. {
  544. struct vmci_qp_dtch_info detach_info;
  545. struct vmci_qp_dtch_info __user *info = uptr;
  546. s32 result;
  547. if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
  548. vmci_ioctl_err("only valid for contexts\n");
  549. return -EINVAL;
  550. }
  551. if (copy_from_user(&detach_info, uptr, sizeof(detach_info)))
  552. return -EFAULT;
  553. result = vmci_qp_broker_detach(detach_info.handle,
  554. vmci_host_dev->context);
  555. if (result == VMCI_SUCCESS &&
  556. vmci_host_dev->user_version < VMCI_VERSION_NOVMVM) {
  557. result = VMCI_SUCCESS_LAST_DETACH;
  558. }
  559. return put_user(result, &info->result) ? -EFAULT : 0;
  560. }
  561. static int vmci_host_do_ctx_add_notify(struct vmci_host_dev *vmci_host_dev,
  562. const char *ioctl_name,
  563. void __user *uptr)
  564. {
  565. struct vmci_ctx_info ar_info;
  566. struct vmci_ctx_info __user *info = uptr;
  567. s32 result;
  568. u32 cid;
  569. if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
  570. vmci_ioctl_err("only valid for contexts\n");
  571. return -EINVAL;
  572. }
  573. if (copy_from_user(&ar_info, uptr, sizeof(ar_info)))
  574. return -EFAULT;
  575. cid = vmci_ctx_get_id(vmci_host_dev->context);
  576. result = vmci_ctx_add_notification(cid, ar_info.remote_cid);
  577. return put_user(result, &info->result) ? -EFAULT : 0;
  578. }
  579. static int vmci_host_do_ctx_remove_notify(struct vmci_host_dev *vmci_host_dev,
  580. const char *ioctl_name,
  581. void __user *uptr)
  582. {
  583. struct vmci_ctx_info ar_info;
  584. struct vmci_ctx_info __user *info = uptr;
  585. u32 cid;
  586. int result;
  587. if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
  588. vmci_ioctl_err("only valid for contexts\n");
  589. return -EINVAL;
  590. }
  591. if (copy_from_user(&ar_info, uptr, sizeof(ar_info)))
  592. return -EFAULT;
  593. cid = vmci_ctx_get_id(vmci_host_dev->context);
  594. result = vmci_ctx_remove_notification(cid,
  595. ar_info.remote_cid);
  596. return put_user(result, &info->result) ? -EFAULT : 0;
  597. }
  598. static int vmci_host_do_ctx_get_cpt_state(struct vmci_host_dev *vmci_host_dev,
  599. const char *ioctl_name,
  600. void __user *uptr)
  601. {
  602. struct vmci_ctx_chkpt_buf_info get_info;
  603. u32 cid;
  604. void *cpt_buf;
  605. int retval;
  606. if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
  607. vmci_ioctl_err("only valid for contexts\n");
  608. return -EINVAL;
  609. }
  610. if (copy_from_user(&get_info, uptr, sizeof(get_info)))
  611. return -EFAULT;
  612. cid = vmci_ctx_get_id(vmci_host_dev->context);
  613. get_info.result = vmci_ctx_get_chkpt_state(cid, get_info.cpt_type,
  614. &get_info.buf_size, &cpt_buf);
  615. if (get_info.result == VMCI_SUCCESS && get_info.buf_size) {
  616. void __user *ubuf = (void __user *)(uintptr_t)get_info.cpt_buf;
  617. retval = copy_to_user(ubuf, cpt_buf, get_info.buf_size);
  618. kfree(cpt_buf);
  619. if (retval)
  620. return -EFAULT;
  621. }
  622. return copy_to_user(uptr, &get_info, sizeof(get_info)) ? -EFAULT : 0;
  623. }
  624. static int vmci_host_do_ctx_set_cpt_state(struct vmci_host_dev *vmci_host_dev,
  625. const char *ioctl_name,
  626. void __user *uptr)
  627. {
  628. struct vmci_ctx_chkpt_buf_info set_info;
  629. u32 cid;
  630. void *cpt_buf;
  631. int retval;
  632. if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
  633. vmci_ioctl_err("only valid for contexts\n");
  634. return -EINVAL;
  635. }
  636. if (copy_from_user(&set_info, uptr, sizeof(set_info)))
  637. return -EFAULT;
  638. cpt_buf = kmalloc(set_info.buf_size, GFP_KERNEL);
  639. if (!cpt_buf) {
  640. vmci_ioctl_err(
  641. "cannot allocate memory to set cpt state (type=%d)\n",
  642. set_info.cpt_type);
  643. return -ENOMEM;
  644. }
  645. if (copy_from_user(cpt_buf, (void __user *)(uintptr_t)set_info.cpt_buf,
  646. set_info.buf_size)) {
  647. retval = -EFAULT;
  648. goto out;
  649. }
  650. cid = vmci_ctx_get_id(vmci_host_dev->context);
  651. set_info.result = vmci_ctx_set_chkpt_state(cid, set_info.cpt_type,
  652. set_info.buf_size, cpt_buf);
  653. retval = copy_to_user(uptr, &set_info, sizeof(set_info)) ? -EFAULT : 0;
  654. out:
  655. kfree(cpt_buf);
  656. return retval;
  657. }
  658. static int vmci_host_do_get_context_id(struct vmci_host_dev *vmci_host_dev,
  659. const char *ioctl_name,
  660. void __user *uptr)
  661. {
  662. u32 __user *u32ptr = uptr;
  663. return put_user(VMCI_HOST_CONTEXT_ID, u32ptr) ? -EFAULT : 0;
  664. }
  665. static int vmci_host_do_set_notify(struct vmci_host_dev *vmci_host_dev,
  666. const char *ioctl_name,
  667. void __user *uptr)
  668. {
  669. struct vmci_set_notify_info notify_info;
  670. if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
  671. vmci_ioctl_err("only valid for contexts\n");
  672. return -EINVAL;
  673. }
  674. if (copy_from_user(&notify_info, uptr, sizeof(notify_info)))
  675. return -EFAULT;
  676. if (notify_info.notify_uva) {
  677. notify_info.result =
  678. vmci_host_setup_notify(vmci_host_dev->context,
  679. notify_info.notify_uva);
  680. } else {
  681. vmci_ctx_unset_notify(vmci_host_dev->context);
  682. notify_info.result = VMCI_SUCCESS;
  683. }
  684. return copy_to_user(uptr, &notify_info, sizeof(notify_info)) ?
  685. -EFAULT : 0;
  686. }
  687. static int vmci_host_do_notify_resource(struct vmci_host_dev *vmci_host_dev,
  688. const char *ioctl_name,
  689. void __user *uptr)
  690. {
  691. struct vmci_dbell_notify_resource_info info;
  692. u32 cid;
  693. if (vmci_host_dev->user_version < VMCI_VERSION_NOTIFY) {
  694. vmci_ioctl_err("invalid for current VMX versions\n");
  695. return -EINVAL;
  696. }
  697. if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
  698. vmci_ioctl_err("only valid for contexts\n");
  699. return -EINVAL;
  700. }
  701. if (copy_from_user(&info, uptr, sizeof(info)))
  702. return -EFAULT;
  703. cid = vmci_ctx_get_id(vmci_host_dev->context);
  704. switch (info.action) {
  705. case VMCI_NOTIFY_RESOURCE_ACTION_NOTIFY:
  706. if (info.resource == VMCI_NOTIFY_RESOURCE_DOOR_BELL) {
  707. u32 flags = VMCI_NO_PRIVILEGE_FLAGS;
  708. info.result = vmci_ctx_notify_dbell(cid, info.handle,
  709. flags);
  710. } else {
  711. info.result = VMCI_ERROR_UNAVAILABLE;
  712. }
  713. break;
  714. case VMCI_NOTIFY_RESOURCE_ACTION_CREATE:
  715. info.result = vmci_ctx_dbell_create(cid, info.handle);
  716. break;
  717. case VMCI_NOTIFY_RESOURCE_ACTION_DESTROY:
  718. info.result = vmci_ctx_dbell_destroy(cid, info.handle);
  719. break;
  720. default:
  721. vmci_ioctl_err("got unknown action (action=%d)\n",
  722. info.action);
  723. info.result = VMCI_ERROR_INVALID_ARGS;
  724. }
  725. return copy_to_user(uptr, &info, sizeof(info)) ? -EFAULT : 0;
  726. }
  727. static int vmci_host_do_recv_notifications(struct vmci_host_dev *vmci_host_dev,
  728. const char *ioctl_name,
  729. void __user *uptr)
  730. {
  731. struct vmci_ctx_notify_recv_info info;
  732. struct vmci_handle_arr *db_handle_array;
  733. struct vmci_handle_arr *qp_handle_array;
  734. void __user *ubuf;
  735. u32 cid;
  736. int retval = 0;
  737. if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
  738. vmci_ioctl_err("only valid for contexts\n");
  739. return -EINVAL;
  740. }
  741. if (vmci_host_dev->user_version < VMCI_VERSION_NOTIFY) {
  742. vmci_ioctl_err("not supported for the current vmx version\n");
  743. return -EINVAL;
  744. }
  745. if (copy_from_user(&info, uptr, sizeof(info)))
  746. return -EFAULT;
  747. if ((info.db_handle_buf_size && !info.db_handle_buf_uva) ||
  748. (info.qp_handle_buf_size && !info.qp_handle_buf_uva)) {
  749. return -EINVAL;
  750. }
  751. cid = vmci_ctx_get_id(vmci_host_dev->context);
  752. info.result = vmci_ctx_rcv_notifications_get(cid,
  753. &db_handle_array, &qp_handle_array);
  754. if (info.result != VMCI_SUCCESS)
  755. return copy_to_user(uptr, &info, sizeof(info)) ? -EFAULT : 0;
  756. ubuf = (void __user *)(uintptr_t)info.db_handle_buf_uva;
  757. info.result = drv_cp_harray_to_user(ubuf, &info.db_handle_buf_size,
  758. db_handle_array, &retval);
  759. if (info.result == VMCI_SUCCESS && !retval) {
  760. ubuf = (void __user *)(uintptr_t)info.qp_handle_buf_uva;
  761. info.result = drv_cp_harray_to_user(ubuf,
  762. &info.qp_handle_buf_size,
  763. qp_handle_array, &retval);
  764. }
  765. if (!retval && copy_to_user(uptr, &info, sizeof(info)))
  766. retval = -EFAULT;
  767. vmci_ctx_rcv_notifications_release(cid,
  768. db_handle_array, qp_handle_array,
  769. info.result == VMCI_SUCCESS && !retval);
  770. return retval;
  771. }
  772. static long vmci_host_unlocked_ioctl(struct file *filp,
  773. unsigned int iocmd, unsigned long ioarg)
  774. {
  775. #define VMCI_DO_IOCTL(ioctl_name, ioctl_fn) do { \
  776. char *name = __stringify(IOCTL_VMCI_ ## ioctl_name); \
  777. return vmci_host_do_ ## ioctl_fn( \
  778. vmci_host_dev, name, uptr); \
  779. } while (0)
  780. struct vmci_host_dev *vmci_host_dev = filp->private_data;
  781. void __user *uptr = (void __user *)ioarg;
  782. switch (iocmd) {
  783. case IOCTL_VMCI_INIT_CONTEXT:
  784. VMCI_DO_IOCTL(INIT_CONTEXT, init_context);
  785. case IOCTL_VMCI_DATAGRAM_SEND:
  786. VMCI_DO_IOCTL(DATAGRAM_SEND, send_datagram);
  787. case IOCTL_VMCI_DATAGRAM_RECEIVE:
  788. VMCI_DO_IOCTL(DATAGRAM_RECEIVE, receive_datagram);
  789. case IOCTL_VMCI_QUEUEPAIR_ALLOC:
  790. VMCI_DO_IOCTL(QUEUEPAIR_ALLOC, alloc_queuepair);
  791. case IOCTL_VMCI_QUEUEPAIR_SETVA:
  792. VMCI_DO_IOCTL(QUEUEPAIR_SETVA, queuepair_setva);
  793. case IOCTL_VMCI_QUEUEPAIR_SETPAGEFILE:
  794. VMCI_DO_IOCTL(QUEUEPAIR_SETPAGEFILE, queuepair_setpf);
  795. case IOCTL_VMCI_QUEUEPAIR_DETACH:
  796. VMCI_DO_IOCTL(QUEUEPAIR_DETACH, qp_detach);
  797. case IOCTL_VMCI_CTX_ADD_NOTIFICATION:
  798. VMCI_DO_IOCTL(CTX_ADD_NOTIFICATION, ctx_add_notify);
  799. case IOCTL_VMCI_CTX_REMOVE_NOTIFICATION:
  800. VMCI_DO_IOCTL(CTX_REMOVE_NOTIFICATION, ctx_remove_notify);
  801. case IOCTL_VMCI_CTX_GET_CPT_STATE:
  802. VMCI_DO_IOCTL(CTX_GET_CPT_STATE, ctx_get_cpt_state);
  803. case IOCTL_VMCI_CTX_SET_CPT_STATE:
  804. VMCI_DO_IOCTL(CTX_SET_CPT_STATE, ctx_set_cpt_state);
  805. case IOCTL_VMCI_GET_CONTEXT_ID:
  806. VMCI_DO_IOCTL(GET_CONTEXT_ID, get_context_id);
  807. case IOCTL_VMCI_SET_NOTIFY:
  808. VMCI_DO_IOCTL(SET_NOTIFY, set_notify);
  809. case IOCTL_VMCI_NOTIFY_RESOURCE:
  810. VMCI_DO_IOCTL(NOTIFY_RESOURCE, notify_resource);
  811. case IOCTL_VMCI_NOTIFICATIONS_RECEIVE:
  812. VMCI_DO_IOCTL(NOTIFICATIONS_RECEIVE, recv_notifications);
  813. case IOCTL_VMCI_VERSION:
  814. case IOCTL_VMCI_VERSION2:
  815. return vmci_host_get_version(vmci_host_dev, iocmd, uptr);
  816. default:
  817. pr_devel("%s: Unknown ioctl (iocmd=%d)\n", __func__, iocmd);
  818. return -EINVAL;
  819. }
  820. #undef VMCI_DO_IOCTL
  821. }
  822. static const struct file_operations vmuser_fops = {
  823. .owner = THIS_MODULE,
  824. .open = vmci_host_open,
  825. .release = vmci_host_close,
  826. .poll = vmci_host_poll,
  827. .unlocked_ioctl = vmci_host_unlocked_ioctl,
  828. .compat_ioctl = vmci_host_unlocked_ioctl,
  829. };
  830. static struct miscdevice vmci_host_miscdev = {
  831. .name = "vmci",
  832. .minor = MISC_DYNAMIC_MINOR,
  833. .fops = &vmuser_fops,
  834. };
  835. int __init vmci_host_init(void)
  836. {
  837. int error;
  838. host_context = vmci_ctx_create(VMCI_HOST_CONTEXT_ID,
  839. VMCI_DEFAULT_PROC_PRIVILEGE_FLAGS,
  840. -1, VMCI_VERSION, NULL);
  841. if (IS_ERR(host_context)) {
  842. error = PTR_ERR(host_context);
  843. pr_warn("Failed to initialize VMCIContext (error%d)\n",
  844. error);
  845. return error;
  846. }
  847. error = misc_register(&vmci_host_miscdev);
  848. if (error) {
  849. pr_warn("Module registration error (name=%s, major=%d, minor=%d, err=%d)\n",
  850. vmci_host_miscdev.name,
  851. MISC_MAJOR, vmci_host_miscdev.minor,
  852. error);
  853. pr_warn("Unable to initialize host personality\n");
  854. vmci_ctx_destroy(host_context);
  855. return error;
  856. }
  857. pr_info("VMCI host device registered (name=%s, major=%d, minor=%d)\n",
  858. vmci_host_miscdev.name, MISC_MAJOR, vmci_host_miscdev.minor);
  859. vmci_host_device_initialized = true;
  860. return 0;
  861. }
  862. void __exit vmci_host_exit(void)
  863. {
  864. int error;
  865. vmci_host_device_initialized = false;
  866. error = misc_deregister(&vmci_host_miscdev);
  867. if (error)
  868. pr_warn("Error unregistering character device: %d\n", error);
  869. vmci_ctx_destroy(host_context);
  870. vmci_qp_broker_exit();
  871. pr_debug("VMCI host driver module unloaded\n");
  872. }