vboxguest_linux.c 13 KB

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