vboxguest_linux.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * vboxguest linux pci driver, char-dev and input-device code,
  4. *
  5. * Copyright (C) 2006-2016 Oracle Corporation
  6. */
  7. #include <linux/cred.h>
  8. #include <linux/input.h>
  9. #include <linux/kernel.h>
  10. #include <linux/miscdevice.h>
  11. #include <linux/module.h>
  12. #include <linux/pci.h>
  13. #include <linux/poll.h>
  14. #include <linux/vbox_utils.h>
  15. #include "vboxguest_core.h"
  16. /** The device name. */
  17. #define DEVICE_NAME "vboxguest"
  18. /** The device name for the device node open to everyone. */
  19. #define DEVICE_NAME_USER "vboxuser"
  20. /** VirtualBox PCI vendor ID. */
  21. #define VBOX_VENDORID 0x80ee
  22. /** VMMDev PCI card product ID. */
  23. #define VMMDEV_DEVICEID 0xcafe
  24. /** Mutex protecting the global vbg_gdev pointer used by vbg_get/put_gdev. */
  25. static DEFINE_MUTEX(vbg_gdev_mutex);
  26. /** Global vbg_gdev pointer used by vbg_get/put_gdev. */
  27. static struct vbg_dev *vbg_gdev;
  28. static u32 vbg_misc_device_requestor(struct inode *inode)
  29. {
  30. u32 requestor = VMMDEV_REQUESTOR_USERMODE |
  31. VMMDEV_REQUESTOR_CON_DONT_KNOW |
  32. VMMDEV_REQUESTOR_TRUST_NOT_GIVEN;
  33. if (from_kuid(current_user_ns(), current_uid()) == 0)
  34. requestor |= VMMDEV_REQUESTOR_USR_ROOT;
  35. else
  36. requestor |= VMMDEV_REQUESTOR_USR_USER;
  37. if (in_egroup_p(inode->i_gid))
  38. requestor |= VMMDEV_REQUESTOR_GRP_VBOX;
  39. return requestor;
  40. }
  41. static int vbg_misc_device_open(struct inode *inode, struct file *filp)
  42. {
  43. struct vbg_session *session;
  44. struct vbg_dev *gdev;
  45. /* misc_open sets filp->private_data to our misc device */
  46. gdev = container_of(filp->private_data, struct vbg_dev, misc_device);
  47. session = vbg_core_open_session(gdev, vbg_misc_device_requestor(inode));
  48. if (IS_ERR(session))
  49. return PTR_ERR(session);
  50. filp->private_data = session;
  51. return 0;
  52. }
  53. static int vbg_misc_device_user_open(struct inode *inode, struct file *filp)
  54. {
  55. struct vbg_session *session;
  56. struct vbg_dev *gdev;
  57. /* misc_open sets filp->private_data to our misc device */
  58. gdev = container_of(filp->private_data, struct vbg_dev,
  59. misc_device_user);
  60. session = vbg_core_open_session(gdev, vbg_misc_device_requestor(inode) |
  61. VMMDEV_REQUESTOR_USER_DEVICE);
  62. if (IS_ERR(session))
  63. return PTR_ERR(session);
  64. filp->private_data = session;
  65. return 0;
  66. }
  67. /**
  68. * vbg_misc_device_close - Close device.
  69. * @inode: Pointer to inode info structure.
  70. * @filp: Associated file pointer.
  71. *
  72. * Return: %0 on success, negated errno on failure.
  73. */
  74. static int vbg_misc_device_close(struct inode *inode, struct file *filp)
  75. {
  76. vbg_core_close_session(filp->private_data);
  77. filp->private_data = NULL;
  78. return 0;
  79. }
  80. /**
  81. * vbg_misc_device_ioctl - Device I/O Control entry point.
  82. * @filp: Associated file pointer.
  83. * @req: The request specified to ioctl().
  84. * @arg: The argument specified to ioctl().
  85. *
  86. * Return: %0 on success, negated errno on failure.
  87. */
  88. static long vbg_misc_device_ioctl(struct file *filp, unsigned int req,
  89. unsigned long arg)
  90. {
  91. struct vbg_session *session = filp->private_data;
  92. size_t returned_size, size;
  93. struct vbg_ioctl_hdr hdr;
  94. bool is_vmmdev_req;
  95. int ret = 0;
  96. void *buf;
  97. if (copy_from_user(&hdr, (void *)arg, sizeof(hdr)))
  98. return -EFAULT;
  99. if (hdr.version != VBG_IOCTL_HDR_VERSION)
  100. return -EINVAL;
  101. if (hdr.size_in < sizeof(hdr) ||
  102. (hdr.size_out && hdr.size_out < sizeof(hdr)))
  103. return -EINVAL;
  104. size = max(hdr.size_in, hdr.size_out);
  105. if (_IOC_SIZE(req) && _IOC_SIZE(req) != size)
  106. return -EINVAL;
  107. if (size > SZ_16M)
  108. return -E2BIG;
  109. /*
  110. * IOCTL_VMMDEV_REQUEST needs the buffer to be below 4G to avoid
  111. * the need for a bounce-buffer and another copy later on.
  112. */
  113. is_vmmdev_req = (req & ~IOCSIZE_MASK) == VBG_IOCTL_VMMDEV_REQUEST(0) ||
  114. req == VBG_IOCTL_VMMDEV_REQUEST_BIG ||
  115. req == VBG_IOCTL_VMMDEV_REQUEST_BIG_ALT;
  116. if (is_vmmdev_req)
  117. buf = vbg_req_alloc(size, VBG_IOCTL_HDR_TYPE_DEFAULT,
  118. session->requestor);
  119. else
  120. buf = kmalloc(size, GFP_KERNEL);
  121. if (!buf)
  122. return -ENOMEM;
  123. *((struct vbg_ioctl_hdr *)buf) = hdr;
  124. if (copy_from_user(buf + sizeof(hdr), (void *)arg + sizeof(hdr),
  125. hdr.size_in - sizeof(hdr))) {
  126. ret = -EFAULT;
  127. goto out;
  128. }
  129. if (hdr.size_in < size)
  130. memset(buf + hdr.size_in, 0, size - hdr.size_in);
  131. ret = vbg_core_ioctl(session, req, buf);
  132. if (ret)
  133. goto out;
  134. returned_size = ((struct vbg_ioctl_hdr *)buf)->size_out;
  135. if (returned_size > size) {
  136. vbg_debug("%s: too much output data %zu > %zu\n",
  137. __func__, returned_size, size);
  138. returned_size = size;
  139. }
  140. if (copy_to_user((void *)arg, buf, returned_size) != 0)
  141. ret = -EFAULT;
  142. out:
  143. if (is_vmmdev_req)
  144. vbg_req_free(buf, size);
  145. else
  146. kfree(buf);
  147. return ret;
  148. }
  149. /* The file_operations structures. */
  150. static const struct file_operations vbg_misc_device_fops = {
  151. .owner = THIS_MODULE,
  152. .open = vbg_misc_device_open,
  153. .release = vbg_misc_device_close,
  154. .unlocked_ioctl = vbg_misc_device_ioctl,
  155. #ifdef CONFIG_COMPAT
  156. .compat_ioctl = vbg_misc_device_ioctl,
  157. #endif
  158. };
  159. static const struct file_operations vbg_misc_device_user_fops = {
  160. .owner = THIS_MODULE,
  161. .open = vbg_misc_device_user_open,
  162. .release = vbg_misc_device_close,
  163. .unlocked_ioctl = vbg_misc_device_ioctl,
  164. #ifdef CONFIG_COMPAT
  165. .compat_ioctl = vbg_misc_device_ioctl,
  166. #endif
  167. };
  168. /*
  169. * Called when the input device is first opened.
  170. *
  171. * Sets up absolute mouse reporting.
  172. */
  173. static int vbg_input_open(struct input_dev *input)
  174. {
  175. struct vbg_dev *gdev = input_get_drvdata(input);
  176. u32 feat = VMMDEV_MOUSE_GUEST_CAN_ABSOLUTE | VMMDEV_MOUSE_NEW_PROTOCOL;
  177. return vbg_core_set_mouse_status(gdev, feat);
  178. }
  179. /*
  180. * Called if all open handles to the input device are closed.
  181. *
  182. * Disables absolute reporting.
  183. */
  184. static void vbg_input_close(struct input_dev *input)
  185. {
  186. struct vbg_dev *gdev = input_get_drvdata(input);
  187. vbg_core_set_mouse_status(gdev, 0);
  188. }
  189. /*
  190. * Creates the kernel input device.
  191. *
  192. * Return: 0 on success, negated errno on failure.
  193. */
  194. static int vbg_create_input_device(struct vbg_dev *gdev)
  195. {
  196. struct input_dev *input;
  197. input = devm_input_allocate_device(gdev->dev);
  198. if (!input)
  199. return -ENOMEM;
  200. input->id.bustype = BUS_PCI;
  201. input->id.vendor = VBOX_VENDORID;
  202. input->id.product = VMMDEV_DEVICEID;
  203. input->open = vbg_input_open;
  204. input->close = vbg_input_close;
  205. input->dev.parent = gdev->dev;
  206. input->name = "VirtualBox mouse integration";
  207. input_set_abs_params(input, ABS_X, VMMDEV_MOUSE_RANGE_MIN,
  208. VMMDEV_MOUSE_RANGE_MAX, 0, 0);
  209. input_set_abs_params(input, ABS_Y, VMMDEV_MOUSE_RANGE_MIN,
  210. VMMDEV_MOUSE_RANGE_MAX, 0, 0);
  211. input_set_capability(input, EV_KEY, BTN_MOUSE);
  212. input_set_drvdata(input, gdev);
  213. gdev->input = input;
  214. return input_register_device(gdev->input);
  215. }
  216. static ssize_t host_version_show(struct device *dev,
  217. struct device_attribute *attr, char *buf)
  218. {
  219. struct vbg_dev *gdev = dev_get_drvdata(dev);
  220. return sprintf(buf, "%s\n", gdev->host_version);
  221. }
  222. static ssize_t host_features_show(struct device *dev,
  223. struct device_attribute *attr, char *buf)
  224. {
  225. struct vbg_dev *gdev = dev_get_drvdata(dev);
  226. return sprintf(buf, "%#x\n", gdev->host_features);
  227. }
  228. static DEVICE_ATTR_RO(host_version);
  229. static DEVICE_ATTR_RO(host_features);
  230. static struct attribute *vbg_pci_attrs[] = {
  231. &dev_attr_host_version.attr,
  232. &dev_attr_host_features.attr,
  233. NULL,
  234. };
  235. ATTRIBUTE_GROUPS(vbg_pci);
  236. /*
  237. * Does the PCI detection and init of the device.
  238. *
  239. * Return: 0 on success, negated errno on failure.
  240. */
  241. static int vbg_pci_probe(struct pci_dev *pci, const struct pci_device_id *id)
  242. {
  243. struct device *dev = &pci->dev;
  244. resource_size_t io, io_len, mmio, mmio_len;
  245. struct vmmdev_memory *vmmdev;
  246. struct vbg_dev *gdev;
  247. int ret;
  248. gdev = devm_kzalloc(dev, sizeof(*gdev), GFP_KERNEL);
  249. if (!gdev)
  250. return -ENOMEM;
  251. ret = pci_enable_device(pci);
  252. if (ret != 0) {
  253. vbg_err("vboxguest: Error enabling device: %d\n", ret);
  254. return ret;
  255. }
  256. ret = -ENODEV;
  257. io = pci_resource_start(pci, 0);
  258. io_len = pci_resource_len(pci, 0);
  259. if (!io || !io_len) {
  260. vbg_err("vboxguest: Error IO-port resource (0) is missing\n");
  261. goto err_disable_pcidev;
  262. }
  263. if (devm_request_region(dev, io, io_len, DEVICE_NAME) == NULL) {
  264. vbg_err("vboxguest: Error could not claim IO resource\n");
  265. ret = -EBUSY;
  266. goto err_disable_pcidev;
  267. }
  268. mmio = pci_resource_start(pci, 1);
  269. mmio_len = pci_resource_len(pci, 1);
  270. if (!mmio || !mmio_len) {
  271. vbg_err("vboxguest: Error MMIO resource (1) is missing\n");
  272. goto err_disable_pcidev;
  273. }
  274. if (devm_request_mem_region(dev, mmio, mmio_len, DEVICE_NAME) == NULL) {
  275. vbg_err("vboxguest: Error could not claim MMIO resource\n");
  276. ret = -EBUSY;
  277. goto err_disable_pcidev;
  278. }
  279. vmmdev = devm_ioremap(dev, mmio, mmio_len);
  280. if (!vmmdev) {
  281. vbg_err("vboxguest: Error ioremap failed; MMIO addr=%pap size=%pap\n",
  282. &mmio, &mmio_len);
  283. goto err_disable_pcidev;
  284. }
  285. /* Validate MMIO region version and size. */
  286. if (vmmdev->version != VMMDEV_MEMORY_VERSION ||
  287. vmmdev->size < 32 || vmmdev->size > mmio_len) {
  288. vbg_err("vboxguest: Bogus VMMDev memory; version=%08x (expected %08x) size=%d (expected <= %d)\n",
  289. vmmdev->version, VMMDEV_MEMORY_VERSION,
  290. vmmdev->size, (int)mmio_len);
  291. goto err_disable_pcidev;
  292. }
  293. gdev->io_port = io;
  294. gdev->mmio = vmmdev;
  295. gdev->dev = dev;
  296. gdev->misc_device.minor = MISC_DYNAMIC_MINOR;
  297. gdev->misc_device.name = DEVICE_NAME;
  298. gdev->misc_device.fops = &vbg_misc_device_fops;
  299. gdev->misc_device_user.minor = MISC_DYNAMIC_MINOR;
  300. gdev->misc_device_user.name = DEVICE_NAME_USER;
  301. gdev->misc_device_user.fops = &vbg_misc_device_user_fops;
  302. ret = vbg_core_init(gdev, VMMDEV_EVENT_MOUSE_POSITION_CHANGED);
  303. if (ret)
  304. goto err_disable_pcidev;
  305. ret = vbg_create_input_device(gdev);
  306. if (ret) {
  307. vbg_err("vboxguest: Error creating input device: %d\n", ret);
  308. goto err_vbg_core_exit;
  309. }
  310. ret = request_irq(pci->irq, vbg_core_isr, IRQF_SHARED, DEVICE_NAME,
  311. gdev);
  312. if (ret) {
  313. vbg_err("vboxguest: Error requesting irq: %d\n", ret);
  314. goto err_vbg_core_exit;
  315. }
  316. ret = misc_register(&gdev->misc_device);
  317. if (ret) {
  318. vbg_err("vboxguest: Error misc_register %s failed: %d\n",
  319. DEVICE_NAME, ret);
  320. goto err_free_irq;
  321. }
  322. ret = misc_register(&gdev->misc_device_user);
  323. if (ret) {
  324. vbg_err("vboxguest: Error misc_register %s failed: %d\n",
  325. DEVICE_NAME_USER, ret);
  326. goto err_unregister_misc_device;
  327. }
  328. mutex_lock(&vbg_gdev_mutex);
  329. if (!vbg_gdev)
  330. vbg_gdev = gdev;
  331. else
  332. ret = -EBUSY;
  333. mutex_unlock(&vbg_gdev_mutex);
  334. if (ret) {
  335. vbg_err("vboxguest: Error more then 1 vbox guest pci device\n");
  336. goto err_unregister_misc_device_user;
  337. }
  338. pci_set_drvdata(pci, gdev);
  339. return 0;
  340. err_unregister_misc_device_user:
  341. misc_deregister(&gdev->misc_device_user);
  342. err_unregister_misc_device:
  343. misc_deregister(&gdev->misc_device);
  344. err_free_irq:
  345. free_irq(pci->irq, gdev);
  346. err_vbg_core_exit:
  347. vbg_core_exit(gdev);
  348. err_disable_pcidev:
  349. pci_disable_device(pci);
  350. return ret;
  351. }
  352. static void vbg_pci_remove(struct pci_dev *pci)
  353. {
  354. struct vbg_dev *gdev = pci_get_drvdata(pci);
  355. mutex_lock(&vbg_gdev_mutex);
  356. vbg_gdev = NULL;
  357. mutex_unlock(&vbg_gdev_mutex);
  358. free_irq(pci->irq, gdev);
  359. misc_deregister(&gdev->misc_device_user);
  360. misc_deregister(&gdev->misc_device);
  361. vbg_core_exit(gdev);
  362. pci_disable_device(pci);
  363. }
  364. struct vbg_dev *vbg_get_gdev(void)
  365. {
  366. mutex_lock(&vbg_gdev_mutex);
  367. /*
  368. * Note on success we keep the mutex locked until vbg_put_gdev(),
  369. * this stops vbg_pci_remove from removing the device from underneath
  370. * vboxsf. vboxsf will only hold a reference for a short while.
  371. */
  372. if (vbg_gdev)
  373. return vbg_gdev;
  374. mutex_unlock(&vbg_gdev_mutex);
  375. return ERR_PTR(-ENODEV);
  376. }
  377. EXPORT_SYMBOL(vbg_get_gdev);
  378. void vbg_put_gdev(struct vbg_dev *gdev)
  379. {
  380. WARN_ON(gdev != vbg_gdev);
  381. mutex_unlock(&vbg_gdev_mutex);
  382. }
  383. EXPORT_SYMBOL(vbg_put_gdev);
  384. /*
  385. * Callback for mouse events.
  386. *
  387. * This is called at the end of the ISR, after leaving the event spinlock, if
  388. * VMMDEV_EVENT_MOUSE_POSITION_CHANGED was raised by the host.
  389. *
  390. * @gdev: The device extension.
  391. */
  392. void vbg_linux_mouse_event(struct vbg_dev *gdev)
  393. {
  394. int rc;
  395. /* Report events to the kernel input device */
  396. gdev->mouse_status_req->mouse_features = 0;
  397. gdev->mouse_status_req->pointer_pos_x = 0;
  398. gdev->mouse_status_req->pointer_pos_y = 0;
  399. rc = vbg_req_perform(gdev, gdev->mouse_status_req);
  400. if (rc >= 0) {
  401. input_report_abs(gdev->input, ABS_X,
  402. gdev->mouse_status_req->pointer_pos_x);
  403. input_report_abs(gdev->input, ABS_Y,
  404. gdev->mouse_status_req->pointer_pos_y);
  405. input_sync(gdev->input);
  406. }
  407. }
  408. static const struct pci_device_id vbg_pci_ids[] = {
  409. { .vendor = VBOX_VENDORID, .device = VMMDEV_DEVICEID },
  410. {}
  411. };
  412. MODULE_DEVICE_TABLE(pci, vbg_pci_ids);
  413. static struct pci_driver vbg_pci_driver = {
  414. .name = DEVICE_NAME,
  415. .dev_groups = vbg_pci_groups,
  416. .id_table = vbg_pci_ids,
  417. .probe = vbg_pci_probe,
  418. .remove = vbg_pci_remove,
  419. };
  420. module_pci_driver(vbg_pci_driver);
  421. MODULE_AUTHOR("Oracle Corporation");
  422. MODULE_DESCRIPTION("Oracle VM VirtualBox Guest Additions for Linux Module");
  423. MODULE_LICENSE("GPL");