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