vmci_host.c 28 KB

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