vmci_host.c 27 KB

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