vmci_host.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  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/miscdevice.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/highmem.h>
  20. #include <linux/atomic.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/mutex.h>
  24. #include <linux/sched.h>
  25. #include <linux/cred.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 __poll_t 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. __poll_t 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 = EPOLLIN;
  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 = 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 = kmalloc(set_info.buf_size, GFP_KERNEL);
  638. if (!cpt_buf) {
  639. vmci_ioctl_err(
  640. "cannot allocate memory to set cpt state (type=%d)\n",
  641. set_info.cpt_type);
  642. return -ENOMEM;
  643. }
  644. if (copy_from_user(cpt_buf, (void __user *)(uintptr_t)set_info.cpt_buf,
  645. set_info.buf_size)) {
  646. retval = -EFAULT;
  647. goto out;
  648. }
  649. cid = vmci_ctx_get_id(vmci_host_dev->context);
  650. set_info.result = vmci_ctx_set_chkpt_state(cid, set_info.cpt_type,
  651. set_info.buf_size, cpt_buf);
  652. retval = copy_to_user(uptr, &set_info, sizeof(set_info)) ? -EFAULT : 0;
  653. out:
  654. kfree(cpt_buf);
  655. return retval;
  656. }
  657. static int vmci_host_do_get_context_id(struct vmci_host_dev *vmci_host_dev,
  658. const char *ioctl_name,
  659. void __user *uptr)
  660. {
  661. u32 __user *u32ptr = uptr;
  662. return put_user(VMCI_HOST_CONTEXT_ID, u32ptr) ? -EFAULT : 0;
  663. }
  664. static int vmci_host_do_set_notify(struct vmci_host_dev *vmci_host_dev,
  665. const char *ioctl_name,
  666. void __user *uptr)
  667. {
  668. struct vmci_set_notify_info notify_info;
  669. if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
  670. vmci_ioctl_err("only valid for contexts\n");
  671. return -EINVAL;
  672. }
  673. if (copy_from_user(&notify_info, uptr, sizeof(notify_info)))
  674. return -EFAULT;
  675. if (notify_info.notify_uva) {
  676. notify_info.result =
  677. vmci_host_setup_notify(vmci_host_dev->context,
  678. notify_info.notify_uva);
  679. } else {
  680. vmci_ctx_unset_notify(vmci_host_dev->context);
  681. notify_info.result = VMCI_SUCCESS;
  682. }
  683. return copy_to_user(uptr, &notify_info, sizeof(notify_info)) ?
  684. -EFAULT : 0;
  685. }
  686. static int vmci_host_do_notify_resource(struct vmci_host_dev *vmci_host_dev,
  687. const char *ioctl_name,
  688. void __user *uptr)
  689. {
  690. struct vmci_dbell_notify_resource_info info;
  691. u32 cid;
  692. if (vmci_host_dev->user_version < VMCI_VERSION_NOTIFY) {
  693. vmci_ioctl_err("invalid for current VMX versions\n");
  694. return -EINVAL;
  695. }
  696. if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
  697. vmci_ioctl_err("only valid for contexts\n");
  698. return -EINVAL;
  699. }
  700. if (copy_from_user(&info, uptr, sizeof(info)))
  701. return -EFAULT;
  702. cid = vmci_ctx_get_id(vmci_host_dev->context);
  703. switch (info.action) {
  704. case VMCI_NOTIFY_RESOURCE_ACTION_NOTIFY:
  705. if (info.resource == VMCI_NOTIFY_RESOURCE_DOOR_BELL) {
  706. u32 flags = VMCI_NO_PRIVILEGE_FLAGS;
  707. info.result = vmci_ctx_notify_dbell(cid, info.handle,
  708. flags);
  709. } else {
  710. info.result = VMCI_ERROR_UNAVAILABLE;
  711. }
  712. break;
  713. case VMCI_NOTIFY_RESOURCE_ACTION_CREATE:
  714. info.result = vmci_ctx_dbell_create(cid, info.handle);
  715. break;
  716. case VMCI_NOTIFY_RESOURCE_ACTION_DESTROY:
  717. info.result = vmci_ctx_dbell_destroy(cid, info.handle);
  718. break;
  719. default:
  720. vmci_ioctl_err("got unknown action (action=%d)\n",
  721. info.action);
  722. info.result = VMCI_ERROR_INVALID_ARGS;
  723. }
  724. return copy_to_user(uptr, &info, sizeof(info)) ? -EFAULT : 0;
  725. }
  726. static int vmci_host_do_recv_notifications(struct vmci_host_dev *vmci_host_dev,
  727. const char *ioctl_name,
  728. void __user *uptr)
  729. {
  730. struct vmci_ctx_notify_recv_info info;
  731. struct vmci_handle_arr *db_handle_array;
  732. struct vmci_handle_arr *qp_handle_array;
  733. void __user *ubuf;
  734. u32 cid;
  735. int retval = 0;
  736. if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
  737. vmci_ioctl_err("only valid for contexts\n");
  738. return -EINVAL;
  739. }
  740. if (vmci_host_dev->user_version < VMCI_VERSION_NOTIFY) {
  741. vmci_ioctl_err("not supported for the current vmx version\n");
  742. return -EINVAL;
  743. }
  744. if (copy_from_user(&info, uptr, sizeof(info)))
  745. return -EFAULT;
  746. if ((info.db_handle_buf_size && !info.db_handle_buf_uva) ||
  747. (info.qp_handle_buf_size && !info.qp_handle_buf_uva)) {
  748. return -EINVAL;
  749. }
  750. cid = vmci_ctx_get_id(vmci_host_dev->context);
  751. info.result = vmci_ctx_rcv_notifications_get(cid,
  752. &db_handle_array, &qp_handle_array);
  753. if (info.result != VMCI_SUCCESS)
  754. return copy_to_user(uptr, &info, sizeof(info)) ? -EFAULT : 0;
  755. ubuf = (void __user *)(uintptr_t)info.db_handle_buf_uva;
  756. info.result = drv_cp_harray_to_user(ubuf, &info.db_handle_buf_size,
  757. db_handle_array, &retval);
  758. if (info.result == VMCI_SUCCESS && !retval) {
  759. ubuf = (void __user *)(uintptr_t)info.qp_handle_buf_uva;
  760. info.result = drv_cp_harray_to_user(ubuf,
  761. &info.qp_handle_buf_size,
  762. qp_handle_array, &retval);
  763. }
  764. if (!retval && copy_to_user(uptr, &info, sizeof(info)))
  765. retval = -EFAULT;
  766. vmci_ctx_rcv_notifications_release(cid,
  767. db_handle_array, qp_handle_array,
  768. info.result == VMCI_SUCCESS && !retval);
  769. return retval;
  770. }
  771. static long vmci_host_unlocked_ioctl(struct file *filp,
  772. unsigned int iocmd, unsigned long ioarg)
  773. {
  774. #define VMCI_DO_IOCTL(ioctl_name, ioctl_fn) do { \
  775. char *name = __stringify(IOCTL_VMCI_ ## ioctl_name); \
  776. return vmci_host_do_ ## ioctl_fn( \
  777. vmci_host_dev, name, uptr); \
  778. } while (0)
  779. struct vmci_host_dev *vmci_host_dev = filp->private_data;
  780. void __user *uptr = (void __user *)ioarg;
  781. switch (iocmd) {
  782. case IOCTL_VMCI_INIT_CONTEXT:
  783. VMCI_DO_IOCTL(INIT_CONTEXT, init_context);
  784. case IOCTL_VMCI_DATAGRAM_SEND:
  785. VMCI_DO_IOCTL(DATAGRAM_SEND, send_datagram);
  786. case IOCTL_VMCI_DATAGRAM_RECEIVE:
  787. VMCI_DO_IOCTL(DATAGRAM_RECEIVE, receive_datagram);
  788. case IOCTL_VMCI_QUEUEPAIR_ALLOC:
  789. VMCI_DO_IOCTL(QUEUEPAIR_ALLOC, alloc_queuepair);
  790. case IOCTL_VMCI_QUEUEPAIR_SETVA:
  791. VMCI_DO_IOCTL(QUEUEPAIR_SETVA, queuepair_setva);
  792. case IOCTL_VMCI_QUEUEPAIR_SETPAGEFILE:
  793. VMCI_DO_IOCTL(QUEUEPAIR_SETPAGEFILE, queuepair_setpf);
  794. case IOCTL_VMCI_QUEUEPAIR_DETACH:
  795. VMCI_DO_IOCTL(QUEUEPAIR_DETACH, qp_detach);
  796. case IOCTL_VMCI_CTX_ADD_NOTIFICATION:
  797. VMCI_DO_IOCTL(CTX_ADD_NOTIFICATION, ctx_add_notify);
  798. case IOCTL_VMCI_CTX_REMOVE_NOTIFICATION:
  799. VMCI_DO_IOCTL(CTX_REMOVE_NOTIFICATION, ctx_remove_notify);
  800. case IOCTL_VMCI_CTX_GET_CPT_STATE:
  801. VMCI_DO_IOCTL(CTX_GET_CPT_STATE, ctx_get_cpt_state);
  802. case IOCTL_VMCI_CTX_SET_CPT_STATE:
  803. VMCI_DO_IOCTL(CTX_SET_CPT_STATE, ctx_set_cpt_state);
  804. case IOCTL_VMCI_GET_CONTEXT_ID:
  805. VMCI_DO_IOCTL(GET_CONTEXT_ID, get_context_id);
  806. case IOCTL_VMCI_SET_NOTIFY:
  807. VMCI_DO_IOCTL(SET_NOTIFY, set_notify);
  808. case IOCTL_VMCI_NOTIFY_RESOURCE:
  809. VMCI_DO_IOCTL(NOTIFY_RESOURCE, notify_resource);
  810. case IOCTL_VMCI_NOTIFICATIONS_RECEIVE:
  811. VMCI_DO_IOCTL(NOTIFICATIONS_RECEIVE, recv_notifications);
  812. case IOCTL_VMCI_VERSION:
  813. case IOCTL_VMCI_VERSION2:
  814. return vmci_host_get_version(vmci_host_dev, iocmd, uptr);
  815. default:
  816. pr_devel("%s: Unknown ioctl (iocmd=%d)\n", __func__, iocmd);
  817. return -EINVAL;
  818. }
  819. #undef VMCI_DO_IOCTL
  820. }
  821. static const struct file_operations vmuser_fops = {
  822. .owner = THIS_MODULE,
  823. .open = vmci_host_open,
  824. .release = vmci_host_close,
  825. .poll = vmci_host_poll,
  826. .unlocked_ioctl = vmci_host_unlocked_ioctl,
  827. .compat_ioctl = vmci_host_unlocked_ioctl,
  828. };
  829. static struct miscdevice vmci_host_miscdev = {
  830. .name = "vmci",
  831. .minor = MISC_DYNAMIC_MINOR,
  832. .fops = &vmuser_fops,
  833. };
  834. int __init vmci_host_init(void)
  835. {
  836. int error;
  837. host_context = vmci_ctx_create(VMCI_HOST_CONTEXT_ID,
  838. VMCI_DEFAULT_PROC_PRIVILEGE_FLAGS,
  839. -1, VMCI_VERSION, NULL);
  840. if (IS_ERR(host_context)) {
  841. error = PTR_ERR(host_context);
  842. pr_warn("Failed to initialize VMCIContext (error%d)\n",
  843. error);
  844. return error;
  845. }
  846. error = misc_register(&vmci_host_miscdev);
  847. if (error) {
  848. pr_warn("Module registration error (name=%s, major=%d, minor=%d, err=%d)\n",
  849. vmci_host_miscdev.name,
  850. MISC_MAJOR, vmci_host_miscdev.minor,
  851. error);
  852. pr_warn("Unable to initialize host personality\n");
  853. vmci_ctx_destroy(host_context);
  854. return error;
  855. }
  856. pr_info("VMCI host device registered (name=%s, major=%d, minor=%d)\n",
  857. vmci_host_miscdev.name, MISC_MAJOR, vmci_host_miscdev.minor);
  858. vmci_host_device_initialized = true;
  859. return 0;
  860. }
  861. void __exit vmci_host_exit(void)
  862. {
  863. vmci_host_device_initialized = false;
  864. misc_deregister(&vmci_host_miscdev);
  865. vmci_ctx_destroy(host_context);
  866. vmci_qp_broker_exit();
  867. pr_debug("VMCI host driver module unloaded\n");
  868. }