vmci_host.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  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. Maps the notify
  194. * boolean in user VA into kernel space.
  195. */
  196. static int vmci_host_setup_notify(struct vmci_ctx *context,
  197. unsigned long uva)
  198. {
  199. int retval;
  200. if (context->notify_page) {
  201. pr_devel("%s: Notify mechanism is already set up\n", __func__);
  202. return VMCI_ERROR_DUPLICATE_ENTRY;
  203. }
  204. /*
  205. * We are using 'bool' internally, but let's make sure we explicit
  206. * about the size.
  207. */
  208. BUILD_BUG_ON(sizeof(bool) != sizeof(u8));
  209. if (!access_ok(VERIFY_WRITE, (void __user *)uva, sizeof(u8)))
  210. return VMCI_ERROR_GENERIC;
  211. /*
  212. * Lock physical page backing a given user VA.
  213. */
  214. retval = get_user_pages_fast(uva, 1, 1, &context->notify_page);
  215. if (retval != 1) {
  216. context->notify_page = NULL;
  217. return VMCI_ERROR_GENERIC;
  218. }
  219. /*
  220. * Map the locked page and set up notify pointer.
  221. */
  222. context->notify = kmap(context->notify_page) + (uva & (PAGE_SIZE - 1));
  223. vmci_ctx_check_signal_notify(context);
  224. return VMCI_SUCCESS;
  225. }
  226. static int vmci_host_get_version(struct vmci_host_dev *vmci_host_dev,
  227. unsigned int cmd, void __user *uptr)
  228. {
  229. if (cmd == IOCTL_VMCI_VERSION2) {
  230. int __user *vptr = uptr;
  231. if (get_user(vmci_host_dev->user_version, vptr))
  232. return -EFAULT;
  233. }
  234. /*
  235. * The basic logic here is:
  236. *
  237. * If the user sends in a version of 0 tell it our version.
  238. * If the user didn't send in a version, tell it our version.
  239. * If the user sent in an old version, tell it -its- version.
  240. * If the user sent in an newer version, tell it our version.
  241. *
  242. * The rationale behind telling the caller its version is that
  243. * Workstation 6.5 required that VMX and VMCI kernel module were
  244. * version sync'd. All new VMX users will be programmed to
  245. * handle the VMCI kernel module version.
  246. */
  247. if (vmci_host_dev->user_version > 0 &&
  248. vmci_host_dev->user_version < VMCI_VERSION_HOSTQP) {
  249. return vmci_host_dev->user_version;
  250. }
  251. return VMCI_VERSION;
  252. }
  253. #define vmci_ioctl_err(fmt, ...) \
  254. pr_devel("%s: " fmt, ioctl_name, ##__VA_ARGS__)
  255. static int vmci_host_do_init_context(struct vmci_host_dev *vmci_host_dev,
  256. const char *ioctl_name,
  257. void __user *uptr)
  258. {
  259. struct vmci_init_blk init_block;
  260. const struct cred *cred;
  261. int retval;
  262. if (copy_from_user(&init_block, uptr, sizeof(init_block))) {
  263. vmci_ioctl_err("error reading init block\n");
  264. return -EFAULT;
  265. }
  266. mutex_lock(&vmci_host_dev->lock);
  267. if (vmci_host_dev->ct_type != VMCIOBJ_NOT_SET) {
  268. vmci_ioctl_err("received VMCI init on initialized handle\n");
  269. retval = -EINVAL;
  270. goto out;
  271. }
  272. if (init_block.flags & ~VMCI_PRIVILEGE_FLAG_RESTRICTED) {
  273. vmci_ioctl_err("unsupported VMCI restriction flag\n");
  274. retval = -EINVAL;
  275. goto out;
  276. }
  277. cred = get_current_cred();
  278. vmci_host_dev->context = vmci_ctx_create(init_block.cid,
  279. init_block.flags, 0,
  280. vmci_host_dev->user_version,
  281. cred);
  282. put_cred(cred);
  283. if (IS_ERR(vmci_host_dev->context)) {
  284. retval = PTR_ERR(vmci_host_dev->context);
  285. vmci_ioctl_err("error initializing context\n");
  286. goto out;
  287. }
  288. /*
  289. * Copy cid to userlevel, we do this to allow the VMX
  290. * to enforce its policy on cid generation.
  291. */
  292. init_block.cid = vmci_ctx_get_id(vmci_host_dev->context);
  293. if (copy_to_user(uptr, &init_block, sizeof(init_block))) {
  294. vmci_ctx_destroy(vmci_host_dev->context);
  295. vmci_host_dev->context = NULL;
  296. vmci_ioctl_err("error writing init block\n");
  297. retval = -EFAULT;
  298. goto out;
  299. }
  300. vmci_host_dev->ct_type = VMCIOBJ_CONTEXT;
  301. atomic_inc(&vmci_host_active_users);
  302. retval = 0;
  303. out:
  304. mutex_unlock(&vmci_host_dev->lock);
  305. return retval;
  306. }
  307. static int vmci_host_do_send_datagram(struct vmci_host_dev *vmci_host_dev,
  308. const char *ioctl_name,
  309. void __user *uptr)
  310. {
  311. struct vmci_datagram_snd_rcv_info send_info;
  312. struct vmci_datagram *dg = NULL;
  313. u32 cid;
  314. if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
  315. vmci_ioctl_err("only valid for contexts\n");
  316. return -EINVAL;
  317. }
  318. if (copy_from_user(&send_info, uptr, sizeof(send_info)))
  319. return -EFAULT;
  320. if (send_info.len > VMCI_MAX_DG_SIZE) {
  321. vmci_ioctl_err("datagram is too big (size=%d)\n",
  322. send_info.len);
  323. return -EINVAL;
  324. }
  325. if (send_info.len < sizeof(*dg)) {
  326. vmci_ioctl_err("datagram is too small (size=%d)\n",
  327. send_info.len);
  328. return -EINVAL;
  329. }
  330. dg = kmalloc(send_info.len, GFP_KERNEL);
  331. if (!dg) {
  332. vmci_ioctl_err(
  333. "cannot allocate memory to dispatch datagram\n");
  334. return -ENOMEM;
  335. }
  336. if (copy_from_user(dg, (void __user *)(uintptr_t)send_info.addr,
  337. send_info.len)) {
  338. vmci_ioctl_err("error getting datagram\n");
  339. kfree(dg);
  340. return -EFAULT;
  341. }
  342. if (VMCI_DG_SIZE(dg) != send_info.len) {
  343. vmci_ioctl_err("datagram size mismatch\n");
  344. kfree(dg);
  345. return -EINVAL;
  346. }
  347. pr_devel("Datagram dst (handle=0x%x:0x%x) src (handle=0x%x:0x%x), payload (size=%llu bytes)\n",
  348. dg->dst.context, dg->dst.resource,
  349. dg->src.context, dg->src.resource,
  350. (unsigned long long)dg->payload_size);
  351. /* Get source context id. */
  352. cid = vmci_ctx_get_id(vmci_host_dev->context);
  353. send_info.result = vmci_datagram_dispatch(cid, dg, true);
  354. kfree(dg);
  355. return copy_to_user(uptr, &send_info, sizeof(send_info)) ? -EFAULT : 0;
  356. }
  357. static int vmci_host_do_receive_datagram(struct vmci_host_dev *vmci_host_dev,
  358. const char *ioctl_name,
  359. void __user *uptr)
  360. {
  361. struct vmci_datagram_snd_rcv_info recv_info;
  362. struct vmci_datagram *dg = NULL;
  363. int retval;
  364. size_t size;
  365. if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
  366. vmci_ioctl_err("only valid for contexts\n");
  367. return -EINVAL;
  368. }
  369. if (copy_from_user(&recv_info, uptr, sizeof(recv_info)))
  370. return -EFAULT;
  371. size = recv_info.len;
  372. recv_info.result = vmci_ctx_dequeue_datagram(vmci_host_dev->context,
  373. &size, &dg);
  374. if (recv_info.result >= VMCI_SUCCESS) {
  375. void __user *ubuf = (void __user *)(uintptr_t)recv_info.addr;
  376. retval = copy_to_user(ubuf, dg, VMCI_DG_SIZE(dg));
  377. kfree(dg);
  378. if (retval != 0)
  379. return -EFAULT;
  380. }
  381. return copy_to_user(uptr, &recv_info, sizeof(recv_info)) ? -EFAULT : 0;
  382. }
  383. static int vmci_host_do_alloc_queuepair(struct vmci_host_dev *vmci_host_dev,
  384. const char *ioctl_name,
  385. void __user *uptr)
  386. {
  387. struct vmci_handle handle;
  388. int vmci_status;
  389. int __user *retptr;
  390. u32 cid;
  391. if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
  392. vmci_ioctl_err("only valid for contexts\n");
  393. return -EINVAL;
  394. }
  395. cid = vmci_ctx_get_id(vmci_host_dev->context);
  396. if (vmci_host_dev->user_version < VMCI_VERSION_NOVMVM) {
  397. struct vmci_qp_alloc_info_vmvm alloc_info;
  398. struct vmci_qp_alloc_info_vmvm __user *info = uptr;
  399. if (copy_from_user(&alloc_info, uptr, sizeof(alloc_info)))
  400. return -EFAULT;
  401. handle = alloc_info.handle;
  402. retptr = &info->result;
  403. vmci_status = vmci_qp_broker_alloc(alloc_info.handle,
  404. alloc_info.peer,
  405. alloc_info.flags,
  406. VMCI_NO_PRIVILEGE_FLAGS,
  407. alloc_info.produce_size,
  408. alloc_info.consume_size,
  409. NULL,
  410. vmci_host_dev->context);
  411. if (vmci_status == VMCI_SUCCESS)
  412. vmci_status = VMCI_SUCCESS_QUEUEPAIR_CREATE;
  413. } else {
  414. struct vmci_qp_alloc_info alloc_info;
  415. struct vmci_qp_alloc_info __user *info = uptr;
  416. struct vmci_qp_page_store page_store;
  417. if (copy_from_user(&alloc_info, uptr, sizeof(alloc_info)))
  418. return -EFAULT;
  419. handle = alloc_info.handle;
  420. retptr = &info->result;
  421. page_store.pages = alloc_info.ppn_va;
  422. page_store.len = alloc_info.num_ppns;
  423. vmci_status = vmci_qp_broker_alloc(alloc_info.handle,
  424. alloc_info.peer,
  425. alloc_info.flags,
  426. VMCI_NO_PRIVILEGE_FLAGS,
  427. alloc_info.produce_size,
  428. alloc_info.consume_size,
  429. &page_store,
  430. vmci_host_dev->context);
  431. }
  432. if (put_user(vmci_status, retptr)) {
  433. if (vmci_status >= VMCI_SUCCESS) {
  434. vmci_status = vmci_qp_broker_detach(handle,
  435. vmci_host_dev->context);
  436. }
  437. return -EFAULT;
  438. }
  439. return 0;
  440. }
  441. static int vmci_host_do_queuepair_setva(struct vmci_host_dev *vmci_host_dev,
  442. const char *ioctl_name,
  443. void __user *uptr)
  444. {
  445. struct vmci_qp_set_va_info set_va_info;
  446. struct vmci_qp_set_va_info __user *info = uptr;
  447. s32 result;
  448. if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
  449. vmci_ioctl_err("only valid for contexts\n");
  450. return -EINVAL;
  451. }
  452. if (vmci_host_dev->user_version < VMCI_VERSION_NOVMVM) {
  453. vmci_ioctl_err("is not allowed\n");
  454. return -EINVAL;
  455. }
  456. if (copy_from_user(&set_va_info, uptr, sizeof(set_va_info)))
  457. return -EFAULT;
  458. if (set_va_info.va) {
  459. /*
  460. * VMX is passing down a new VA for the queue
  461. * pair mapping.
  462. */
  463. result = vmci_qp_broker_map(set_va_info.handle,
  464. vmci_host_dev->context,
  465. set_va_info.va);
  466. } else {
  467. /*
  468. * The queue pair is about to be unmapped by
  469. * the VMX.
  470. */
  471. result = vmci_qp_broker_unmap(set_va_info.handle,
  472. vmci_host_dev->context, 0);
  473. }
  474. return put_user(result, &info->result) ? -EFAULT : 0;
  475. }
  476. static int vmci_host_do_queuepair_setpf(struct vmci_host_dev *vmci_host_dev,
  477. const char *ioctl_name,
  478. void __user *uptr)
  479. {
  480. struct vmci_qp_page_file_info page_file_info;
  481. struct vmci_qp_page_file_info __user *info = uptr;
  482. s32 result;
  483. if (vmci_host_dev->user_version < VMCI_VERSION_HOSTQP ||
  484. vmci_host_dev->user_version >= VMCI_VERSION_NOVMVM) {
  485. vmci_ioctl_err("not supported on this VMX (version=%d)\n",
  486. vmci_host_dev->user_version);
  487. return -EINVAL;
  488. }
  489. if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
  490. vmci_ioctl_err("only valid for contexts\n");
  491. return -EINVAL;
  492. }
  493. if (copy_from_user(&page_file_info, uptr, sizeof(*info)))
  494. return -EFAULT;
  495. /*
  496. * Communicate success pre-emptively to the caller. Note that the
  497. * basic premise is that it is incumbent upon the caller not to look at
  498. * the info.result field until after the ioctl() returns. And then,
  499. * only if the ioctl() result indicates no error. We send up the
  500. * SUCCESS status before calling SetPageStore() store because failing
  501. * to copy up the result code means unwinding the SetPageStore().
  502. *
  503. * It turns out the logic to unwind a SetPageStore() opens a can of
  504. * worms. For example, if a host had created the queue_pair and a
  505. * guest attaches and SetPageStore() is successful but writing success
  506. * fails, then ... the host has to be stopped from writing (anymore)
  507. * data into the queue_pair. That means an additional test in the
  508. * VMCI_Enqueue() code path. Ugh.
  509. */
  510. if (put_user(VMCI_SUCCESS, &info->result)) {
  511. /*
  512. * In this case, we can't write a result field of the
  513. * caller's info block. So, we don't even try to
  514. * SetPageStore().
  515. */
  516. return -EFAULT;
  517. }
  518. result = vmci_qp_broker_set_page_store(page_file_info.handle,
  519. page_file_info.produce_va,
  520. page_file_info.consume_va,
  521. vmci_host_dev->context);
  522. if (result < VMCI_SUCCESS) {
  523. if (put_user(result, &info->result)) {
  524. /*
  525. * Note that in this case the SetPageStore()
  526. * call failed but we were unable to
  527. * communicate that to the caller (because the
  528. * copy_to_user() call failed). So, if we
  529. * simply return an error (in this case
  530. * -EFAULT) then the caller will know that the
  531. * SetPageStore failed even though we couldn't
  532. * put the result code in the result field and
  533. * indicate exactly why it failed.
  534. *
  535. * That says nothing about the issue where we
  536. * were once able to write to the caller's info
  537. * memory and now can't. Something more
  538. * serious is probably going on than the fact
  539. * that SetPageStore() didn't work.
  540. */
  541. return -EFAULT;
  542. }
  543. }
  544. return 0;
  545. }
  546. static int vmci_host_do_qp_detach(struct vmci_host_dev *vmci_host_dev,
  547. const char *ioctl_name,
  548. void __user *uptr)
  549. {
  550. struct vmci_qp_dtch_info detach_info;
  551. struct vmci_qp_dtch_info __user *info = uptr;
  552. s32 result;
  553. if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
  554. vmci_ioctl_err("only valid for contexts\n");
  555. return -EINVAL;
  556. }
  557. if (copy_from_user(&detach_info, uptr, sizeof(detach_info)))
  558. return -EFAULT;
  559. result = vmci_qp_broker_detach(detach_info.handle,
  560. vmci_host_dev->context);
  561. if (result == VMCI_SUCCESS &&
  562. vmci_host_dev->user_version < VMCI_VERSION_NOVMVM) {
  563. result = VMCI_SUCCESS_LAST_DETACH;
  564. }
  565. return put_user(result, &info->result) ? -EFAULT : 0;
  566. }
  567. static int vmci_host_do_ctx_add_notify(struct vmci_host_dev *vmci_host_dev,
  568. const char *ioctl_name,
  569. void __user *uptr)
  570. {
  571. struct vmci_ctx_info ar_info;
  572. struct vmci_ctx_info __user *info = uptr;
  573. s32 result;
  574. u32 cid;
  575. if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
  576. vmci_ioctl_err("only valid for contexts\n");
  577. return -EINVAL;
  578. }
  579. if (copy_from_user(&ar_info, uptr, sizeof(ar_info)))
  580. return -EFAULT;
  581. cid = vmci_ctx_get_id(vmci_host_dev->context);
  582. result = vmci_ctx_add_notification(cid, ar_info.remote_cid);
  583. return put_user(result, &info->result) ? -EFAULT : 0;
  584. }
  585. static int vmci_host_do_ctx_remove_notify(struct vmci_host_dev *vmci_host_dev,
  586. const char *ioctl_name,
  587. void __user *uptr)
  588. {
  589. struct vmci_ctx_info ar_info;
  590. struct vmci_ctx_info __user *info = uptr;
  591. u32 cid;
  592. int result;
  593. if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
  594. vmci_ioctl_err("only valid for contexts\n");
  595. return -EINVAL;
  596. }
  597. if (copy_from_user(&ar_info, uptr, sizeof(ar_info)))
  598. return -EFAULT;
  599. cid = vmci_ctx_get_id(vmci_host_dev->context);
  600. result = vmci_ctx_remove_notification(cid,
  601. ar_info.remote_cid);
  602. return put_user(result, &info->result) ? -EFAULT : 0;
  603. }
  604. static int vmci_host_do_ctx_get_cpt_state(struct vmci_host_dev *vmci_host_dev,
  605. const char *ioctl_name,
  606. void __user *uptr)
  607. {
  608. struct vmci_ctx_chkpt_buf_info get_info;
  609. u32 cid;
  610. void *cpt_buf;
  611. int retval;
  612. if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
  613. vmci_ioctl_err("only valid for contexts\n");
  614. return -EINVAL;
  615. }
  616. if (copy_from_user(&get_info, uptr, sizeof(get_info)))
  617. return -EFAULT;
  618. cid = vmci_ctx_get_id(vmci_host_dev->context);
  619. get_info.result = vmci_ctx_get_chkpt_state(cid, get_info.cpt_type,
  620. &get_info.buf_size, &cpt_buf);
  621. if (get_info.result == VMCI_SUCCESS && get_info.buf_size) {
  622. void __user *ubuf = (void __user *)(uintptr_t)get_info.cpt_buf;
  623. retval = copy_to_user(ubuf, cpt_buf, get_info.buf_size);
  624. kfree(cpt_buf);
  625. if (retval)
  626. return -EFAULT;
  627. }
  628. return copy_to_user(uptr, &get_info, sizeof(get_info)) ? -EFAULT : 0;
  629. }
  630. static int vmci_host_do_ctx_set_cpt_state(struct vmci_host_dev *vmci_host_dev,
  631. const char *ioctl_name,
  632. void __user *uptr)
  633. {
  634. struct vmci_ctx_chkpt_buf_info set_info;
  635. u32 cid;
  636. void *cpt_buf;
  637. int retval;
  638. if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
  639. vmci_ioctl_err("only valid for contexts\n");
  640. return -EINVAL;
  641. }
  642. if (copy_from_user(&set_info, uptr, sizeof(set_info)))
  643. return -EFAULT;
  644. cpt_buf = kmalloc(set_info.buf_size, GFP_KERNEL);
  645. if (!cpt_buf) {
  646. vmci_ioctl_err(
  647. "cannot allocate memory to set cpt state (type=%d)\n",
  648. set_info.cpt_type);
  649. return -ENOMEM;
  650. }
  651. if (copy_from_user(cpt_buf, (void __user *)(uintptr_t)set_info.cpt_buf,
  652. set_info.buf_size)) {
  653. retval = -EFAULT;
  654. goto out;
  655. }
  656. cid = vmci_ctx_get_id(vmci_host_dev->context);
  657. set_info.result = vmci_ctx_set_chkpt_state(cid, set_info.cpt_type,
  658. set_info.buf_size, cpt_buf);
  659. retval = copy_to_user(uptr, &set_info, sizeof(set_info)) ? -EFAULT : 0;
  660. out:
  661. kfree(cpt_buf);
  662. return retval;
  663. }
  664. static int vmci_host_do_get_context_id(struct vmci_host_dev *vmci_host_dev,
  665. const char *ioctl_name,
  666. void __user *uptr)
  667. {
  668. u32 __user *u32ptr = uptr;
  669. return put_user(VMCI_HOST_CONTEXT_ID, u32ptr) ? -EFAULT : 0;
  670. }
  671. static int vmci_host_do_set_notify(struct vmci_host_dev *vmci_host_dev,
  672. const char *ioctl_name,
  673. void __user *uptr)
  674. {
  675. struct vmci_set_notify_info notify_info;
  676. if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
  677. vmci_ioctl_err("only valid for contexts\n");
  678. return -EINVAL;
  679. }
  680. if (copy_from_user(&notify_info, uptr, sizeof(notify_info)))
  681. return -EFAULT;
  682. if (notify_info.notify_uva) {
  683. notify_info.result =
  684. vmci_host_setup_notify(vmci_host_dev->context,
  685. notify_info.notify_uva);
  686. } else {
  687. vmci_ctx_unset_notify(vmci_host_dev->context);
  688. notify_info.result = VMCI_SUCCESS;
  689. }
  690. return copy_to_user(uptr, &notify_info, sizeof(notify_info)) ?
  691. -EFAULT : 0;
  692. }
  693. static int vmci_host_do_notify_resource(struct vmci_host_dev *vmci_host_dev,
  694. const char *ioctl_name,
  695. void __user *uptr)
  696. {
  697. struct vmci_dbell_notify_resource_info info;
  698. u32 cid;
  699. if (vmci_host_dev->user_version < VMCI_VERSION_NOTIFY) {
  700. vmci_ioctl_err("invalid for current VMX versions\n");
  701. return -EINVAL;
  702. }
  703. if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
  704. vmci_ioctl_err("only valid for contexts\n");
  705. return -EINVAL;
  706. }
  707. if (copy_from_user(&info, uptr, sizeof(info)))
  708. return -EFAULT;
  709. cid = vmci_ctx_get_id(vmci_host_dev->context);
  710. switch (info.action) {
  711. case VMCI_NOTIFY_RESOURCE_ACTION_NOTIFY:
  712. if (info.resource == VMCI_NOTIFY_RESOURCE_DOOR_BELL) {
  713. u32 flags = VMCI_NO_PRIVILEGE_FLAGS;
  714. info.result = vmci_ctx_notify_dbell(cid, info.handle,
  715. flags);
  716. } else {
  717. info.result = VMCI_ERROR_UNAVAILABLE;
  718. }
  719. break;
  720. case VMCI_NOTIFY_RESOURCE_ACTION_CREATE:
  721. info.result = vmci_ctx_dbell_create(cid, info.handle);
  722. break;
  723. case VMCI_NOTIFY_RESOURCE_ACTION_DESTROY:
  724. info.result = vmci_ctx_dbell_destroy(cid, info.handle);
  725. break;
  726. default:
  727. vmci_ioctl_err("got unknown action (action=%d)\n",
  728. info.action);
  729. info.result = VMCI_ERROR_INVALID_ARGS;
  730. }
  731. return copy_to_user(uptr, &info, sizeof(info)) ? -EFAULT : 0;
  732. }
  733. static int vmci_host_do_recv_notifications(struct vmci_host_dev *vmci_host_dev,
  734. const char *ioctl_name,
  735. void __user *uptr)
  736. {
  737. struct vmci_ctx_notify_recv_info info;
  738. struct vmci_handle_arr *db_handle_array;
  739. struct vmci_handle_arr *qp_handle_array;
  740. void __user *ubuf;
  741. u32 cid;
  742. int retval = 0;
  743. if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
  744. vmci_ioctl_err("only valid for contexts\n");
  745. return -EINVAL;
  746. }
  747. if (vmci_host_dev->user_version < VMCI_VERSION_NOTIFY) {
  748. vmci_ioctl_err("not supported for the current vmx version\n");
  749. return -EINVAL;
  750. }
  751. if (copy_from_user(&info, uptr, sizeof(info)))
  752. return -EFAULT;
  753. if ((info.db_handle_buf_size && !info.db_handle_buf_uva) ||
  754. (info.qp_handle_buf_size && !info.qp_handle_buf_uva)) {
  755. return -EINVAL;
  756. }
  757. cid = vmci_ctx_get_id(vmci_host_dev->context);
  758. info.result = vmci_ctx_rcv_notifications_get(cid,
  759. &db_handle_array, &qp_handle_array);
  760. if (info.result != VMCI_SUCCESS)
  761. return copy_to_user(uptr, &info, sizeof(info)) ? -EFAULT : 0;
  762. ubuf = (void __user *)(uintptr_t)info.db_handle_buf_uva;
  763. info.result = drv_cp_harray_to_user(ubuf, &info.db_handle_buf_size,
  764. db_handle_array, &retval);
  765. if (info.result == VMCI_SUCCESS && !retval) {
  766. ubuf = (void __user *)(uintptr_t)info.qp_handle_buf_uva;
  767. info.result = drv_cp_harray_to_user(ubuf,
  768. &info.qp_handle_buf_size,
  769. qp_handle_array, &retval);
  770. }
  771. if (!retval && copy_to_user(uptr, &info, sizeof(info)))
  772. retval = -EFAULT;
  773. vmci_ctx_rcv_notifications_release(cid,
  774. db_handle_array, qp_handle_array,
  775. info.result == VMCI_SUCCESS && !retval);
  776. return retval;
  777. }
  778. static long vmci_host_unlocked_ioctl(struct file *filp,
  779. unsigned int iocmd, unsigned long ioarg)
  780. {
  781. #define VMCI_DO_IOCTL(ioctl_name, ioctl_fn) do { \
  782. char *name = __stringify(IOCTL_VMCI_ ## ioctl_name); \
  783. return vmci_host_do_ ## ioctl_fn( \
  784. vmci_host_dev, name, uptr); \
  785. } while (0)
  786. struct vmci_host_dev *vmci_host_dev = filp->private_data;
  787. void __user *uptr = (void __user *)ioarg;
  788. switch (iocmd) {
  789. case IOCTL_VMCI_INIT_CONTEXT:
  790. VMCI_DO_IOCTL(INIT_CONTEXT, init_context);
  791. case IOCTL_VMCI_DATAGRAM_SEND:
  792. VMCI_DO_IOCTL(DATAGRAM_SEND, send_datagram);
  793. case IOCTL_VMCI_DATAGRAM_RECEIVE:
  794. VMCI_DO_IOCTL(DATAGRAM_RECEIVE, receive_datagram);
  795. case IOCTL_VMCI_QUEUEPAIR_ALLOC:
  796. VMCI_DO_IOCTL(QUEUEPAIR_ALLOC, alloc_queuepair);
  797. case IOCTL_VMCI_QUEUEPAIR_SETVA:
  798. VMCI_DO_IOCTL(QUEUEPAIR_SETVA, queuepair_setva);
  799. case IOCTL_VMCI_QUEUEPAIR_SETPAGEFILE:
  800. VMCI_DO_IOCTL(QUEUEPAIR_SETPAGEFILE, queuepair_setpf);
  801. case IOCTL_VMCI_QUEUEPAIR_DETACH:
  802. VMCI_DO_IOCTL(QUEUEPAIR_DETACH, qp_detach);
  803. case IOCTL_VMCI_CTX_ADD_NOTIFICATION:
  804. VMCI_DO_IOCTL(CTX_ADD_NOTIFICATION, ctx_add_notify);
  805. case IOCTL_VMCI_CTX_REMOVE_NOTIFICATION:
  806. VMCI_DO_IOCTL(CTX_REMOVE_NOTIFICATION, ctx_remove_notify);
  807. case IOCTL_VMCI_CTX_GET_CPT_STATE:
  808. VMCI_DO_IOCTL(CTX_GET_CPT_STATE, ctx_get_cpt_state);
  809. case IOCTL_VMCI_CTX_SET_CPT_STATE:
  810. VMCI_DO_IOCTL(CTX_SET_CPT_STATE, ctx_set_cpt_state);
  811. case IOCTL_VMCI_GET_CONTEXT_ID:
  812. VMCI_DO_IOCTL(GET_CONTEXT_ID, get_context_id);
  813. case IOCTL_VMCI_SET_NOTIFY:
  814. VMCI_DO_IOCTL(SET_NOTIFY, set_notify);
  815. case IOCTL_VMCI_NOTIFY_RESOURCE:
  816. VMCI_DO_IOCTL(NOTIFY_RESOURCE, notify_resource);
  817. case IOCTL_VMCI_NOTIFICATIONS_RECEIVE:
  818. VMCI_DO_IOCTL(NOTIFICATIONS_RECEIVE, recv_notifications);
  819. case IOCTL_VMCI_VERSION:
  820. case IOCTL_VMCI_VERSION2:
  821. return vmci_host_get_version(vmci_host_dev, iocmd, uptr);
  822. default:
  823. pr_devel("%s: Unknown ioctl (iocmd=%d)\n", __func__, iocmd);
  824. return -EINVAL;
  825. }
  826. #undef VMCI_DO_IOCTL
  827. }
  828. static const struct file_operations vmuser_fops = {
  829. .owner = THIS_MODULE,
  830. .open = vmci_host_open,
  831. .release = vmci_host_close,
  832. .poll = vmci_host_poll,
  833. .unlocked_ioctl = vmci_host_unlocked_ioctl,
  834. .compat_ioctl = vmci_host_unlocked_ioctl,
  835. };
  836. static struct miscdevice vmci_host_miscdev = {
  837. .name = "vmci",
  838. .minor = MISC_DYNAMIC_MINOR,
  839. .fops = &vmuser_fops,
  840. };
  841. int __init vmci_host_init(void)
  842. {
  843. int error;
  844. host_context = vmci_ctx_create(VMCI_HOST_CONTEXT_ID,
  845. VMCI_DEFAULT_PROC_PRIVILEGE_FLAGS,
  846. -1, VMCI_VERSION, NULL);
  847. if (IS_ERR(host_context)) {
  848. error = PTR_ERR(host_context);
  849. pr_warn("Failed to initialize VMCIContext (error%d)\n",
  850. error);
  851. return error;
  852. }
  853. error = misc_register(&vmci_host_miscdev);
  854. if (error) {
  855. pr_warn("Module registration error (name=%s, major=%d, minor=%d, err=%d)\n",
  856. vmci_host_miscdev.name,
  857. MISC_MAJOR, vmci_host_miscdev.minor,
  858. error);
  859. pr_warn("Unable to initialize host personality\n");
  860. vmci_ctx_destroy(host_context);
  861. return error;
  862. }
  863. pr_info("VMCI host device registered (name=%s, major=%d, minor=%d)\n",
  864. vmci_host_miscdev.name, MISC_MAJOR, vmci_host_miscdev.minor);
  865. vmci_host_device_initialized = true;
  866. return 0;
  867. }
  868. void __exit vmci_host_exit(void)
  869. {
  870. vmci_host_device_initialized = false;
  871. misc_deregister(&vmci_host_miscdev);
  872. vmci_ctx_destroy(host_context);
  873. vmci_qp_broker_exit();
  874. pr_debug("VMCI host driver module unloaded\n");
  875. }