vmci_host.c 27 KB

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