vmci_host.c 28 KB

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