1 | /* $Id: VBoxGuest-linux.c 90692 2021-08-16 09:20:36Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxGuest - Linux specifics.
|
---|
4 | *
|
---|
5 | * Note. Unfortunately, the difference between this and SUPDrv-linux.c is
|
---|
6 | * a little bit too big to be helpful.
|
---|
7 | */
|
---|
8 |
|
---|
9 | /*
|
---|
10 | * Copyright (C) 2006-2020 Oracle Corporation
|
---|
11 | *
|
---|
12 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
13 | * available from http://www.virtualbox.org. This file is free software;
|
---|
14 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
15 | * General Public License (GPL) as published by the Free Software
|
---|
16 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
17 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
18 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
19 | *
|
---|
20 | * The contents of this file may alternatively be used under the terms
|
---|
21 | * of the Common Development and Distribution License Version 1.0
|
---|
22 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
23 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
24 | * CDDL are applicable instead of those of the GPL.
|
---|
25 | *
|
---|
26 | * You may elect to license modified versions of this file under the
|
---|
27 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
28 | */
|
---|
29 |
|
---|
30 |
|
---|
31 | /*********************************************************************************************************************************
|
---|
32 | * Header Files *
|
---|
33 | *********************************************************************************************************************************/
|
---|
34 | #define LOG_GROUP LOG_GROUP_SUP_DRV
|
---|
35 |
|
---|
36 | #include "the-linux-kernel.h"
|
---|
37 |
|
---|
38 | #if RTLNX_VER_MIN(2,6,15)
|
---|
39 | # define VBOXGUEST_WITH_INPUT_DRIVER
|
---|
40 | #endif
|
---|
41 |
|
---|
42 | #if RTLNX_VER_MIN(4,15,0)
|
---|
43 | # define CONST_4_15 const
|
---|
44 | #else
|
---|
45 | # define CONST_4_15
|
---|
46 | #endif
|
---|
47 |
|
---|
48 | #include "VBoxGuestInternal.h"
|
---|
49 | #ifdef VBOXGUEST_WITH_INPUT_DRIVER
|
---|
50 | # include <linux/input.h>
|
---|
51 | #endif
|
---|
52 | #include <linux/miscdevice.h>
|
---|
53 | #include <linux/poll.h>
|
---|
54 | #if RTLNX_VER_MIN(2,6,28)
|
---|
55 | # include <linux/tty.h>
|
---|
56 | #endif
|
---|
57 | #include <VBox/version.h>
|
---|
58 | #include "revision-generated.h"
|
---|
59 |
|
---|
60 | #include <iprt/assert.h>
|
---|
61 | #include <iprt/asm.h>
|
---|
62 | #include <iprt/ctype.h>
|
---|
63 | #include <iprt/initterm.h>
|
---|
64 | #include <iprt/mem.h>
|
---|
65 | #include <iprt/mp.h>
|
---|
66 | #include <iprt/process.h>
|
---|
67 | #include <iprt/spinlock.h>
|
---|
68 | #include <iprt/semaphore.h>
|
---|
69 | #include <iprt/string.h>
|
---|
70 | #include <VBox/err.h>
|
---|
71 | #include <VBox/log.h>
|
---|
72 |
|
---|
73 |
|
---|
74 | /*********************************************************************************************************************************
|
---|
75 | * Defined Constants And Macros *
|
---|
76 | *********************************************************************************************************************************/
|
---|
77 | /** The device name. */
|
---|
78 | #define DEVICE_NAME "vboxguest"
|
---|
79 | /** The device name for the device node open to everyone. */
|
---|
80 | #define DEVICE_NAME_USER "vboxuser"
|
---|
81 | /** The name of the PCI driver */
|
---|
82 | #define DRIVER_NAME DEVICE_NAME
|
---|
83 |
|
---|
84 |
|
---|
85 | /* 2.4.x compatibility macros that may or may not be defined. */
|
---|
86 | #ifndef IRQ_RETVAL
|
---|
87 | # define irqreturn_t void
|
---|
88 | # define IRQ_RETVAL(n)
|
---|
89 | #endif
|
---|
90 |
|
---|
91 | /* uidgid.h was introduced in 3.5.0. */
|
---|
92 | #if RTLNX_VER_MAX(3,5,0)
|
---|
93 | # define kgid_t gid_t
|
---|
94 | # define kuid_t uid_t
|
---|
95 | #endif
|
---|
96 |
|
---|
97 |
|
---|
98 | /*********************************************************************************************************************************
|
---|
99 | * Internal Functions *
|
---|
100 | *********************************************************************************************************************************/
|
---|
101 | static void vgdrvLinuxTermPci(struct pci_dev *pPciDev);
|
---|
102 | static int vgdrvLinuxProbePci(struct pci_dev *pPciDev, const struct pci_device_id *id);
|
---|
103 | static int __init vgdrvLinuxModInit(void);
|
---|
104 | static void __exit vgdrvLinuxModExit(void);
|
---|
105 | static int vgdrvLinuxOpen(struct inode *pInode, struct file *pFilp);
|
---|
106 | static int vgdrvLinuxRelease(struct inode *pInode, struct file *pFilp);
|
---|
107 | #ifdef HAVE_UNLOCKED_IOCTL
|
---|
108 | static long vgdrvLinuxIOCtl(struct file *pFilp, unsigned int uCmd, unsigned long ulArg);
|
---|
109 | #else
|
---|
110 | static int vgdrvLinuxIOCtl(struct inode *pInode, struct file *pFilp, unsigned int uCmd, unsigned long ulArg);
|
---|
111 | #endif
|
---|
112 | static int vgdrvLinuxIOCtlSlow(struct file *pFilp, unsigned int uCmd, unsigned long ulArg, PVBOXGUESTSESSION pSession);
|
---|
113 | static int vgdrvLinuxFAsync(int fd, struct file *pFile, int fOn);
|
---|
114 | static unsigned int vgdrvLinuxPoll(struct file *pFile, poll_table *pPt);
|
---|
115 | static ssize_t vgdrvLinuxRead(struct file *pFile, char *pbBuf, size_t cbRead, loff_t *poff);
|
---|
116 |
|
---|
117 |
|
---|
118 | /*********************************************************************************************************************************
|
---|
119 | * Global Variables *
|
---|
120 | *********************************************************************************************************************************/
|
---|
121 | /**
|
---|
122 | * Device extention & session data association structure.
|
---|
123 | */
|
---|
124 | static VBOXGUESTDEVEXT g_DevExt;
|
---|
125 | /** The PCI device. */
|
---|
126 | static struct pci_dev *g_pPciDev = NULL;
|
---|
127 | /** The base of the I/O port range. */
|
---|
128 | static RTIOPORT g_IOPortBase;
|
---|
129 | /** The base of the MMIO range. */
|
---|
130 | static RTHCPHYS g_MMIOPhysAddr = NIL_RTHCPHYS;
|
---|
131 | /** The size of the MMIO range as seen by PCI. */
|
---|
132 | static uint32_t g_cbMMIO;
|
---|
133 | /** The pointer to the mapping of the MMIO range. */
|
---|
134 | static void *g_pvMMIOBase;
|
---|
135 | /** Wait queue used by polling. */
|
---|
136 | static wait_queue_head_t g_PollEventQueue;
|
---|
137 | /** Asynchronous notification stuff. */
|
---|
138 | static struct fasync_struct *g_pFAsyncQueue;
|
---|
139 | #ifdef VBOXGUEST_WITH_INPUT_DRIVER
|
---|
140 | /** Pre-allocated mouse status VMMDev request for use in the IRQ
|
---|
141 | * handler. */
|
---|
142 | static VMMDevReqMouseStatus *g_pMouseStatusReq;
|
---|
143 | #endif
|
---|
144 | #if RTLNX_VER_MIN(2,6,0)
|
---|
145 | /** Whether we've create the logger or not. */
|
---|
146 | static volatile bool g_fLoggerCreated;
|
---|
147 | /** Release logger group settings. */
|
---|
148 | static char g_szLogGrp[128];
|
---|
149 | /** Release logger flags settings. */
|
---|
150 | static char g_szLogFlags[128];
|
---|
151 | /** Release logger destination settings. */
|
---|
152 | static char g_szLogDst[128];
|
---|
153 | # if 0
|
---|
154 | /** Debug logger group settings. */
|
---|
155 | static char g_szDbgLogGrp[128];
|
---|
156 | /** Debug logger flags settings. */
|
---|
157 | static char g_szDbgLogFlags[128];
|
---|
158 | /** Debug logger destination settings. */
|
---|
159 | static char g_szDbgLogDst[128];
|
---|
160 | # endif
|
---|
161 | #endif
|
---|
162 |
|
---|
163 | /** The input device handle */
|
---|
164 | #ifdef VBOXGUEST_WITH_INPUT_DRIVER
|
---|
165 | static struct input_dev *g_pInputDevice = NULL;
|
---|
166 | #endif
|
---|
167 |
|
---|
168 | /** The file_operations structure. */
|
---|
169 | static struct file_operations g_FileOps =
|
---|
170 | {
|
---|
171 | owner: THIS_MODULE,
|
---|
172 | open: vgdrvLinuxOpen,
|
---|
173 | release: vgdrvLinuxRelease,
|
---|
174 | #ifdef HAVE_UNLOCKED_IOCTL
|
---|
175 | unlocked_ioctl: vgdrvLinuxIOCtl,
|
---|
176 | #else
|
---|
177 | ioctl: vgdrvLinuxIOCtl,
|
---|
178 | #endif
|
---|
179 | fasync: vgdrvLinuxFAsync,
|
---|
180 | read: vgdrvLinuxRead,
|
---|
181 | poll: vgdrvLinuxPoll,
|
---|
182 | llseek: no_llseek,
|
---|
183 | };
|
---|
184 |
|
---|
185 | /** The miscdevice structure. */
|
---|
186 | static struct miscdevice g_MiscDevice =
|
---|
187 | {
|
---|
188 | minor: MISC_DYNAMIC_MINOR,
|
---|
189 | name: DEVICE_NAME,
|
---|
190 | fops: &g_FileOps,
|
---|
191 | };
|
---|
192 |
|
---|
193 | /** The file_operations structure for the user device.
|
---|
194 | * @remarks For the time being we'll be using the same implementation as
|
---|
195 | * /dev/vboxguest here. */
|
---|
196 | static struct file_operations g_FileOpsUser =
|
---|
197 | {
|
---|
198 | owner: THIS_MODULE,
|
---|
199 | open: vgdrvLinuxOpen,
|
---|
200 | release: vgdrvLinuxRelease,
|
---|
201 | #ifdef HAVE_UNLOCKED_IOCTL
|
---|
202 | unlocked_ioctl: vgdrvLinuxIOCtl,
|
---|
203 | #else
|
---|
204 | ioctl: vgdrvLinuxIOCtl,
|
---|
205 | #endif
|
---|
206 | };
|
---|
207 |
|
---|
208 | /** The miscdevice structure for the user device. */
|
---|
209 | static struct miscdevice g_MiscDeviceUser =
|
---|
210 | {
|
---|
211 | minor: MISC_DYNAMIC_MINOR,
|
---|
212 | name: DEVICE_NAME_USER,
|
---|
213 | fops: &g_FileOpsUser,
|
---|
214 | };
|
---|
215 |
|
---|
216 |
|
---|
217 | /** PCI hotplug structure. */
|
---|
218 | static const struct pci_device_id g_VBoxGuestPciId[] =
|
---|
219 | {
|
---|
220 | {
|
---|
221 | vendor: VMMDEV_VENDORID,
|
---|
222 | device: VMMDEV_DEVICEID
|
---|
223 | },
|
---|
224 | {
|
---|
225 | /* empty entry */
|
---|
226 | }
|
---|
227 | };
|
---|
228 |
|
---|
229 | MODULE_DEVICE_TABLE(pci, g_VBoxGuestPciId);
|
---|
230 |
|
---|
231 | /** Structure for registering the PCI driver. */
|
---|
232 | static struct pci_driver g_PciDriver =
|
---|
233 | {
|
---|
234 | name: DRIVER_NAME,
|
---|
235 | id_table: g_VBoxGuestPciId,
|
---|
236 | probe: vgdrvLinuxProbePci,
|
---|
237 | remove: vgdrvLinuxTermPci
|
---|
238 | };
|
---|
239 |
|
---|
240 | #ifdef VBOXGUEST_WITH_INPUT_DRIVER
|
---|
241 | /** Kernel IDC session to ourselves for use with the mouse events. */
|
---|
242 | static PVBOXGUESTSESSION g_pKernelSession = NULL;
|
---|
243 | #endif
|
---|
244 |
|
---|
245 |
|
---|
246 |
|
---|
247 | /**
|
---|
248 | * Converts a VBox status code to a linux error code.
|
---|
249 | *
|
---|
250 | * @returns corresponding negative linux error code.
|
---|
251 | * @param rc supdrv error code (SUPDRV_ERR_* defines).
|
---|
252 | */
|
---|
253 | static int vgdrvLinuxConvertToNegErrno(int rc)
|
---|
254 | {
|
---|
255 | if ( rc > -1000
|
---|
256 | && rc < 1000)
|
---|
257 | return -RTErrConvertToErrno(rc);
|
---|
258 | switch (rc)
|
---|
259 | {
|
---|
260 | case VERR_HGCM_SERVICE_NOT_FOUND: return -ESRCH;
|
---|
261 | case VINF_HGCM_CLIENT_REJECTED: return 0;
|
---|
262 | case VERR_HGCM_INVALID_CMD_ADDRESS: return -EFAULT;
|
---|
263 | case VINF_HGCM_ASYNC_EXECUTE: return 0;
|
---|
264 | case VERR_HGCM_INTERNAL: return -EPROTO;
|
---|
265 | case VERR_HGCM_INVALID_CLIENT_ID: return -EINVAL;
|
---|
266 | case VINF_HGCM_SAVE_STATE: return 0;
|
---|
267 | /* No reason to return this to a guest */
|
---|
268 | // case VERR_HGCM_SERVICE_EXISTS: return -EEXIST;
|
---|
269 | default:
|
---|
270 | AssertMsgFailed(("Unhandled error code %Rrc\n", rc));
|
---|
271 | return -EPROTO;
|
---|
272 | }
|
---|
273 | }
|
---|
274 |
|
---|
275 |
|
---|
276 | /**
|
---|
277 | * Does the PCI detection and init of the device.
|
---|
278 | *
|
---|
279 | * @returns 0 on success, negated errno on failure.
|
---|
280 | */
|
---|
281 | static int vgdrvLinuxProbePci(struct pci_dev *pPciDev, const struct pci_device_id *id)
|
---|
282 | {
|
---|
283 | int rc;
|
---|
284 |
|
---|
285 | NOREF(id);
|
---|
286 | AssertReturn(!g_pPciDev, -EINVAL);
|
---|
287 | rc = pci_enable_device(pPciDev);
|
---|
288 | if (rc >= 0)
|
---|
289 | {
|
---|
290 | /* I/O Ports are mandatory, the MMIO bit is not. */
|
---|
291 | g_IOPortBase = pci_resource_start(pPciDev, 0);
|
---|
292 | if (g_IOPortBase != 0)
|
---|
293 | {
|
---|
294 | /*
|
---|
295 | * Map the register address space.
|
---|
296 | */
|
---|
297 | g_MMIOPhysAddr = pci_resource_start(pPciDev, 1);
|
---|
298 | g_cbMMIO = pci_resource_len(pPciDev, 1);
|
---|
299 | if (request_mem_region(g_MMIOPhysAddr, g_cbMMIO, DEVICE_NAME) != NULL)
|
---|
300 | {
|
---|
301 | g_pvMMIOBase = ioremap(g_MMIOPhysAddr, g_cbMMIO);
|
---|
302 | if (g_pvMMIOBase)
|
---|
303 | {
|
---|
304 | /** @todo why aren't we requesting ownership of the I/O ports as well? */
|
---|
305 | g_pPciDev = pPciDev;
|
---|
306 | return 0;
|
---|
307 | }
|
---|
308 |
|
---|
309 | /* failure cleanup path */
|
---|
310 | LogRel((DEVICE_NAME ": ioremap failed; MMIO Addr=%RHp cb=%#x\n", g_MMIOPhysAddr, g_cbMMIO));
|
---|
311 | rc = -ENOMEM;
|
---|
312 | release_mem_region(g_MMIOPhysAddr, g_cbMMIO);
|
---|
313 | }
|
---|
314 | else
|
---|
315 | {
|
---|
316 | LogRel((DEVICE_NAME ": failed to obtain adapter memory\n"));
|
---|
317 | rc = -EBUSY;
|
---|
318 | }
|
---|
319 | g_MMIOPhysAddr = NIL_RTHCPHYS;
|
---|
320 | g_cbMMIO = 0;
|
---|
321 | g_IOPortBase = 0;
|
---|
322 | }
|
---|
323 | else
|
---|
324 | {
|
---|
325 | LogRel((DEVICE_NAME ": did not find expected hardware resources\n"));
|
---|
326 | rc = -ENXIO;
|
---|
327 | }
|
---|
328 | pci_disable_device(pPciDev);
|
---|
329 | }
|
---|
330 | else
|
---|
331 | LogRel((DEVICE_NAME ": could not enable device: %d\n", rc));
|
---|
332 | return rc;
|
---|
333 | }
|
---|
334 |
|
---|
335 |
|
---|
336 | /**
|
---|
337 | * Clean up the usage of the PCI device.
|
---|
338 | */
|
---|
339 | static void vgdrvLinuxTermPci(struct pci_dev *pPciDev)
|
---|
340 | {
|
---|
341 | g_pPciDev = NULL;
|
---|
342 | if (pPciDev)
|
---|
343 | {
|
---|
344 | iounmap(g_pvMMIOBase);
|
---|
345 | g_pvMMIOBase = NULL;
|
---|
346 |
|
---|
347 | release_mem_region(g_MMIOPhysAddr, g_cbMMIO);
|
---|
348 | g_MMIOPhysAddr = NIL_RTHCPHYS;
|
---|
349 | g_cbMMIO = 0;
|
---|
350 |
|
---|
351 | pci_disable_device(pPciDev);
|
---|
352 | }
|
---|
353 | }
|
---|
354 |
|
---|
355 |
|
---|
356 | /**
|
---|
357 | * Interrupt service routine.
|
---|
358 | *
|
---|
359 | * @returns In 2.4 it returns void.
|
---|
360 | * In 2.6 we indicate whether we've handled the IRQ or not.
|
---|
361 | *
|
---|
362 | * @param iIrq The IRQ number.
|
---|
363 | * @param pvDevId The device ID, a pointer to g_DevExt.
|
---|
364 | * @param pRegs Register set. Removed in 2.6.19.
|
---|
365 | */
|
---|
366 | #if RTLNX_VER_MIN(2,6,19) && !defined(DOXYGEN_RUNNING)
|
---|
367 | static irqreturn_t vgdrvLinuxISR(int iIrq, void *pvDevId)
|
---|
368 | #else
|
---|
369 | static irqreturn_t vgdrvLinuxISR(int iIrq, void *pvDevId, struct pt_regs *pRegs)
|
---|
370 | #endif
|
---|
371 | {
|
---|
372 | bool fTaken = VGDrvCommonISR(&g_DevExt);
|
---|
373 | return IRQ_RETVAL(fTaken);
|
---|
374 | }
|
---|
375 |
|
---|
376 |
|
---|
377 | /**
|
---|
378 | * Registers the ISR and initializes the poll wait queue.
|
---|
379 | */
|
---|
380 | static int __init vgdrvLinuxInitISR(void)
|
---|
381 | {
|
---|
382 | int rc;
|
---|
383 |
|
---|
384 | init_waitqueue_head(&g_PollEventQueue);
|
---|
385 | rc = request_irq(g_pPciDev->irq,
|
---|
386 | vgdrvLinuxISR,
|
---|
387 | #if RTLNX_VER_MIN(2,6,20)
|
---|
388 | IRQF_SHARED,
|
---|
389 | #else
|
---|
390 | SA_SHIRQ,
|
---|
391 | #endif
|
---|
392 | DEVICE_NAME,
|
---|
393 | &g_DevExt);
|
---|
394 | if (rc)
|
---|
395 | {
|
---|
396 | LogRel((DEVICE_NAME ": could not request IRQ %d: err=%d\n", g_pPciDev->irq, rc));
|
---|
397 | return rc;
|
---|
398 | }
|
---|
399 | return 0;
|
---|
400 | }
|
---|
401 |
|
---|
402 |
|
---|
403 | /**
|
---|
404 | * Deregisters the ISR.
|
---|
405 | */
|
---|
406 | static void vgdrvLinuxTermISR(void)
|
---|
407 | {
|
---|
408 | free_irq(g_pPciDev->irq, &g_DevExt);
|
---|
409 | }
|
---|
410 |
|
---|
411 |
|
---|
412 | #ifdef VBOXGUEST_WITH_INPUT_DRIVER
|
---|
413 |
|
---|
414 | /**
|
---|
415 | * Reports the mouse integration status to the host.
|
---|
416 | *
|
---|
417 | * Calls the kernel IOCtl to report mouse status to the host on behalf of
|
---|
418 | * our kernel session.
|
---|
419 | *
|
---|
420 | * @param fStatus The mouse status to report.
|
---|
421 | */
|
---|
422 | static int vgdrvLinuxSetMouseStatus(uint32_t fStatus)
|
---|
423 | {
|
---|
424 | int rc;
|
---|
425 | VBGLIOCSETMOUSESTATUS Req;
|
---|
426 | VBGLREQHDR_INIT(&Req.Hdr, SET_MOUSE_STATUS);
|
---|
427 | Req.u.In.fStatus = fStatus;
|
---|
428 | rc = VGDrvCommonIoCtl(VBGL_IOCTL_SET_MOUSE_STATUS, &g_DevExt, g_pKernelSession, &Req.Hdr, sizeof(Req));
|
---|
429 | if (RT_SUCCESS(rc))
|
---|
430 | rc = Req.Hdr.rc;
|
---|
431 | return rc;
|
---|
432 | }
|
---|
433 |
|
---|
434 |
|
---|
435 | /**
|
---|
436 | * Called when the input device is first opened.
|
---|
437 | *
|
---|
438 | * Sets up absolute mouse reporting.
|
---|
439 | */
|
---|
440 | static int vboxguestOpenInputDevice(struct input_dev *pDev)
|
---|
441 | {
|
---|
442 | int rc = vgdrvLinuxSetMouseStatus(VMMDEV_MOUSE_GUEST_CAN_ABSOLUTE | VMMDEV_MOUSE_NEW_PROTOCOL);
|
---|
443 | if (RT_FAILURE(rc))
|
---|
444 | return ENODEV;
|
---|
445 | NOREF(pDev);
|
---|
446 | return 0;
|
---|
447 | }
|
---|
448 |
|
---|
449 |
|
---|
450 | /**
|
---|
451 | * Called if all open handles to the input device are closed.
|
---|
452 | *
|
---|
453 | * Disables absolute reporting.
|
---|
454 | */
|
---|
455 | static void vboxguestCloseInputDevice(struct input_dev *pDev)
|
---|
456 | {
|
---|
457 | NOREF(pDev);
|
---|
458 | vgdrvLinuxSetMouseStatus(0);
|
---|
459 | }
|
---|
460 |
|
---|
461 |
|
---|
462 | /**
|
---|
463 | * Creates the kernel input device.
|
---|
464 | */
|
---|
465 | static int __init vgdrvLinuxCreateInputDevice(void)
|
---|
466 | {
|
---|
467 | int rc = VbglR0GRAlloc((VMMDevRequestHeader **)&g_pMouseStatusReq, sizeof(*g_pMouseStatusReq), VMMDevReq_GetMouseStatus);
|
---|
468 | if (RT_SUCCESS(rc))
|
---|
469 | {
|
---|
470 | g_pInputDevice = input_allocate_device();
|
---|
471 | if (g_pInputDevice)
|
---|
472 | {
|
---|
473 | g_pInputDevice->id.bustype = BUS_PCI;
|
---|
474 | g_pInputDevice->id.vendor = VMMDEV_VENDORID;
|
---|
475 | g_pInputDevice->id.product = VMMDEV_DEVICEID;
|
---|
476 | g_pInputDevice->id.version = VBOX_SHORT_VERSION;
|
---|
477 | g_pInputDevice->open = vboxguestOpenInputDevice;
|
---|
478 | g_pInputDevice->close = vboxguestCloseInputDevice;
|
---|
479 | # if RTLNX_VER_MAX(2,6,22)
|
---|
480 | g_pInputDevice->cdev.dev = &g_pPciDev->dev;
|
---|
481 | # else
|
---|
482 | g_pInputDevice->dev.parent = &g_pPciDev->dev;
|
---|
483 | # endif
|
---|
484 | rc = input_register_device(g_pInputDevice);
|
---|
485 | if (rc == 0)
|
---|
486 | {
|
---|
487 | /* Do what one of our competitors apparently does as that works. */
|
---|
488 | ASMBitSet(g_pInputDevice->evbit, EV_ABS);
|
---|
489 | ASMBitSet(g_pInputDevice->evbit, EV_KEY);
|
---|
490 | # ifdef EV_SYN
|
---|
491 | ASMBitSet(g_pInputDevice->evbit, EV_SYN);
|
---|
492 | # endif
|
---|
493 | input_set_abs_params(g_pInputDevice, ABS_X, VMMDEV_MOUSE_RANGE_MIN, VMMDEV_MOUSE_RANGE_MAX, 0, 0);
|
---|
494 | input_set_abs_params(g_pInputDevice, ABS_Y, VMMDEV_MOUSE_RANGE_MIN, VMMDEV_MOUSE_RANGE_MAX, 0, 0);
|
---|
495 | ASMBitSet(g_pInputDevice->keybit, BTN_MOUSE);
|
---|
496 | /** @todo this string should be in a header file somewhere. */
|
---|
497 | g_pInputDevice->name = "VirtualBox mouse integration";
|
---|
498 | return 0;
|
---|
499 | }
|
---|
500 |
|
---|
501 | input_free_device(g_pInputDevice);
|
---|
502 | }
|
---|
503 | else
|
---|
504 | rc = -ENOMEM;
|
---|
505 | VbglR0GRFree(&g_pMouseStatusReq->header);
|
---|
506 | g_pMouseStatusReq = NULL;
|
---|
507 | }
|
---|
508 | else
|
---|
509 | rc = -ENOMEM;
|
---|
510 | return rc;
|
---|
511 | }
|
---|
512 |
|
---|
513 |
|
---|
514 | /**
|
---|
515 | * Terminates the kernel input device.
|
---|
516 | */
|
---|
517 | static void vgdrvLinuxTermInputDevice(void)
|
---|
518 | {
|
---|
519 | VbglR0GRFree(&g_pMouseStatusReq->header);
|
---|
520 | g_pMouseStatusReq = NULL;
|
---|
521 |
|
---|
522 | /* See documentation of input_register_device(): input_free_device()
|
---|
523 | * should not be called after a device has been registered. */
|
---|
524 | input_unregister_device(g_pInputDevice);
|
---|
525 | }
|
---|
526 |
|
---|
527 | #endif /* VBOXGUEST_WITH_INPUT_DRIVER */
|
---|
528 |
|
---|
529 | /**
|
---|
530 | * Creates the device nodes.
|
---|
531 | *
|
---|
532 | * @returns 0 on success, negated errno on failure.
|
---|
533 | */
|
---|
534 | static int __init vgdrvLinuxInitDeviceNodes(void)
|
---|
535 | {
|
---|
536 | /*
|
---|
537 | * The full feature device node.
|
---|
538 | */
|
---|
539 | int rc = misc_register(&g_MiscDevice);
|
---|
540 | if (!rc)
|
---|
541 | {
|
---|
542 | /*
|
---|
543 | * The device node intended to be accessible by all users.
|
---|
544 | */
|
---|
545 | rc = misc_register(&g_MiscDeviceUser);
|
---|
546 | if (!rc)
|
---|
547 | return 0;
|
---|
548 | LogRel((DEVICE_NAME ": misc_register failed for %s (rc=%d)\n", DEVICE_NAME_USER, rc));
|
---|
549 | misc_deregister(&g_MiscDevice);
|
---|
550 | }
|
---|
551 | else
|
---|
552 | LogRel((DEVICE_NAME ": misc_register failed for %s (rc=%d)\n", DEVICE_NAME, rc));
|
---|
553 | return rc;
|
---|
554 | }
|
---|
555 |
|
---|
556 |
|
---|
557 | /**
|
---|
558 | * Deregisters the device nodes.
|
---|
559 | */
|
---|
560 | static void vgdrvLinuxTermDeviceNodes(void)
|
---|
561 | {
|
---|
562 | misc_deregister(&g_MiscDevice);
|
---|
563 | misc_deregister(&g_MiscDeviceUser);
|
---|
564 | }
|
---|
565 |
|
---|
566 |
|
---|
567 | /**
|
---|
568 | * Initialize module.
|
---|
569 | *
|
---|
570 | * @returns appropriate status code.
|
---|
571 | */
|
---|
572 | static int __init vgdrvLinuxModInit(void)
|
---|
573 | {
|
---|
574 | static const char * const s_apszGroups[] = VBOX_LOGGROUP_NAMES;
|
---|
575 | PRTLOGGER pRelLogger;
|
---|
576 | int rc;
|
---|
577 |
|
---|
578 | /*
|
---|
579 | * Initialize IPRT first.
|
---|
580 | */
|
---|
581 | rc = RTR0Init(0);
|
---|
582 | if (RT_FAILURE(rc))
|
---|
583 | {
|
---|
584 | printk(KERN_ERR DEVICE_NAME ": RTR0Init failed, rc=%d.\n", rc);
|
---|
585 | return -EINVAL;
|
---|
586 | }
|
---|
587 |
|
---|
588 | /*
|
---|
589 | * Create the release log.
|
---|
590 | * (We do that here instead of common code because we want to log
|
---|
591 | * early failures using the LogRel macro.)
|
---|
592 | */
|
---|
593 | rc = RTLogCreate(&pRelLogger, 0 /* fFlags */, "all",
|
---|
594 | "VBOX_RELEASE_LOG", RT_ELEMENTS(s_apszGroups), s_apszGroups,
|
---|
595 | RTLOGDEST_STDOUT | RTLOGDEST_DEBUGGER | RTLOGDEST_USER, NULL);
|
---|
596 | if (RT_SUCCESS(rc))
|
---|
597 | {
|
---|
598 | #if RTLNX_VER_MIN(2,6,0)
|
---|
599 | RTLogGroupSettings(pRelLogger, g_szLogGrp);
|
---|
600 | RTLogFlags(pRelLogger, g_szLogFlags);
|
---|
601 | RTLogDestinations(pRelLogger, g_szLogDst);
|
---|
602 | #endif
|
---|
603 | RTLogRelSetDefaultInstance(pRelLogger);
|
---|
604 | }
|
---|
605 | #if RTLNX_VER_MIN(2,6,0)
|
---|
606 | g_fLoggerCreated = true;
|
---|
607 | #endif
|
---|
608 |
|
---|
609 | /*
|
---|
610 | * Locate and initialize the PCI device.
|
---|
611 | */
|
---|
612 | rc = pci_register_driver(&g_PciDriver);
|
---|
613 | if (rc >= 0 && g_pPciDev)
|
---|
614 | {
|
---|
615 | /*
|
---|
616 | * Call the common device extension initializer.
|
---|
617 | */
|
---|
618 | #if RTLNX_VER_MIN(2,6,0) && defined(RT_ARCH_X86)
|
---|
619 | VBOXOSTYPE enmOSType = VBOXOSTYPE_Linux26;
|
---|
620 | #elif RTLNX_VER_MIN(2,6,0) && defined(RT_ARCH_AMD64)
|
---|
621 | VBOXOSTYPE enmOSType = VBOXOSTYPE_Linux26_x64;
|
---|
622 | #elif RTLNX_VER_MIN(2,4,0) && defined(RT_ARCH_X86)
|
---|
623 | VBOXOSTYPE enmOSType = VBOXOSTYPE_Linux24;
|
---|
624 | #elif RTLNX_VER_MIN(2,4,0) && defined(RT_ARCH_AMD64)
|
---|
625 | VBOXOSTYPE enmOSType = VBOXOSTYPE_Linux24_x64;
|
---|
626 | #else
|
---|
627 | # warning "huh? which arch + version is this?"
|
---|
628 | VBOXOSTYPE enmOsType = VBOXOSTYPE_Linux;
|
---|
629 | #endif
|
---|
630 | rc = VGDrvCommonInitDevExt(&g_DevExt,
|
---|
631 | g_IOPortBase,
|
---|
632 | g_pvMMIOBase,
|
---|
633 | g_cbMMIO,
|
---|
634 | enmOSType,
|
---|
635 | VMMDEV_EVENT_MOUSE_POSITION_CHANGED);
|
---|
636 | if (RT_SUCCESS(rc))
|
---|
637 | {
|
---|
638 | /*
|
---|
639 | * Register the interrupt service routine for it now that g_DevExt can handle IRQs.
|
---|
640 | */
|
---|
641 | rc = vgdrvLinuxInitISR();
|
---|
642 | if (rc >= 0) /** @todo r=bird: status check differs from that inside vgdrvLinuxInitISR. */
|
---|
643 | {
|
---|
644 | #ifdef VBOXGUEST_WITH_INPUT_DRIVER
|
---|
645 | /*
|
---|
646 | * Create the kernel session for this driver.
|
---|
647 | */
|
---|
648 | rc = VGDrvCommonCreateKernelSession(&g_DevExt, &g_pKernelSession);
|
---|
649 | if (RT_SUCCESS(rc))
|
---|
650 | {
|
---|
651 | /*
|
---|
652 | * Create the kernel input device.
|
---|
653 | */
|
---|
654 | rc = vgdrvLinuxCreateInputDevice();
|
---|
655 | if (rc >= 0)
|
---|
656 | {
|
---|
657 | #endif
|
---|
658 | /*
|
---|
659 | * Read host configuration.
|
---|
660 | */
|
---|
661 | VGDrvCommonProcessOptionsFromHost(&g_DevExt);
|
---|
662 |
|
---|
663 | /*
|
---|
664 | * Finally, create the device nodes.
|
---|
665 | */
|
---|
666 | rc = vgdrvLinuxInitDeviceNodes();
|
---|
667 | if (rc >= 0)
|
---|
668 | {
|
---|
669 | /* some useful information for the user but don't show this on the console */
|
---|
670 | LogRel((DEVICE_NAME ": Successfully loaded version " VBOX_VERSION_STRING " r" __stringify(VBOX_SVN_REV) "\n"));
|
---|
671 | LogRel((DEVICE_NAME ": misc device minor %d, IRQ %d, I/O port %RTiop, MMIO at %RHp (size 0x%x)\n",
|
---|
672 | g_MiscDevice.minor, g_pPciDev->irq, g_IOPortBase, g_MMIOPhysAddr, g_cbMMIO));
|
---|
673 | printk(KERN_DEBUG DEVICE_NAME ": Successfully loaded version "
|
---|
674 | VBOX_VERSION_STRING " r" __stringify(VBOX_SVN_REV) " (interface " RT_XSTR(VMMDEV_VERSION) ")\n");
|
---|
675 | return rc;
|
---|
676 | }
|
---|
677 |
|
---|
678 | /* bail out */
|
---|
679 | #ifdef VBOXGUEST_WITH_INPUT_DRIVER
|
---|
680 | vgdrvLinuxTermInputDevice();
|
---|
681 | }
|
---|
682 | else
|
---|
683 | {
|
---|
684 | LogRel((DEVICE_NAME ": vboxguestCreateInputDevice failed with rc=%Rrc\n", rc));
|
---|
685 | rc = RTErrConvertFromErrno(rc);
|
---|
686 | }
|
---|
687 | VGDrvCommonCloseSession(&g_DevExt, g_pKernelSession);
|
---|
688 | }
|
---|
689 | #endif
|
---|
690 | vgdrvLinuxTermISR();
|
---|
691 | }
|
---|
692 | VGDrvCommonDeleteDevExt(&g_DevExt);
|
---|
693 | }
|
---|
694 | else
|
---|
695 | {
|
---|
696 | LogRel((DEVICE_NAME ": VGDrvCommonInitDevExt failed with rc=%Rrc\n", rc));
|
---|
697 | rc = RTErrConvertFromErrno(rc);
|
---|
698 | }
|
---|
699 | }
|
---|
700 | else
|
---|
701 | {
|
---|
702 | LogRel((DEVICE_NAME ": PCI device not found, probably running on physical hardware.\n"));
|
---|
703 | rc = -ENODEV;
|
---|
704 | }
|
---|
705 | pci_unregister_driver(&g_PciDriver);
|
---|
706 | RTLogDestroy(RTLogRelSetDefaultInstance(NULL));
|
---|
707 | RTLogDestroy(RTLogSetDefaultInstance(NULL));
|
---|
708 | RTR0Term();
|
---|
709 | return rc;
|
---|
710 | }
|
---|
711 |
|
---|
712 |
|
---|
713 | /**
|
---|
714 | * Unload the module.
|
---|
715 | */
|
---|
716 | static void __exit vgdrvLinuxModExit(void)
|
---|
717 | {
|
---|
718 | /*
|
---|
719 | * Inverse order of init.
|
---|
720 | */
|
---|
721 | vgdrvLinuxTermDeviceNodes();
|
---|
722 | #ifdef VBOXGUEST_WITH_INPUT_DRIVER
|
---|
723 | vgdrvLinuxTermInputDevice();
|
---|
724 | VGDrvCommonCloseSession(&g_DevExt, g_pKernelSession);
|
---|
725 | #endif
|
---|
726 | vgdrvLinuxTermISR();
|
---|
727 | VGDrvCommonDeleteDevExt(&g_DevExt);
|
---|
728 | pci_unregister_driver(&g_PciDriver);
|
---|
729 | RTLogDestroy(RTLogRelSetDefaultInstance(NULL));
|
---|
730 | RTLogDestroy(RTLogSetDefaultInstance(NULL));
|
---|
731 | RTR0Term();
|
---|
732 | }
|
---|
733 |
|
---|
734 |
|
---|
735 | /**
|
---|
736 | * Get the process user ID.
|
---|
737 | *
|
---|
738 | * @returns UID.
|
---|
739 | */
|
---|
740 | DECLINLINE(RTUID) vgdrvLinuxGetUid(void)
|
---|
741 | {
|
---|
742 | #if RTLNX_VER_MIN(2,6,29)
|
---|
743 | # if RTLNX_VER_MIN(3,5,0)
|
---|
744 | return from_kuid(current_user_ns(), current->cred->uid);
|
---|
745 | # else
|
---|
746 | return current->cred->uid;
|
---|
747 | # endif
|
---|
748 | #else
|
---|
749 | return current->uid;
|
---|
750 | #endif
|
---|
751 | }
|
---|
752 |
|
---|
753 |
|
---|
754 | /**
|
---|
755 | * Checks if the given group number is zero or not.
|
---|
756 | *
|
---|
757 | * @returns true / false.
|
---|
758 | * @param gid The group to check for.
|
---|
759 | */
|
---|
760 | DECLINLINE(bool) vgdrvLinuxIsGroupZero(kgid_t gid)
|
---|
761 | {
|
---|
762 | #if RTLNX_VER_MIN(3,5,0)
|
---|
763 | return from_kgid(current_user_ns(), gid);
|
---|
764 | #else
|
---|
765 | return gid == 0;
|
---|
766 | #endif
|
---|
767 | }
|
---|
768 |
|
---|
769 |
|
---|
770 | /**
|
---|
771 | * Searches the effective group and supplementary groups for @a gid.
|
---|
772 | *
|
---|
773 | * @returns true if member, false if not.
|
---|
774 | * @param gid The group to check for.
|
---|
775 | */
|
---|
776 | DECLINLINE(RTGID) vgdrvLinuxIsInGroupEff(kgid_t gid)
|
---|
777 | {
|
---|
778 | return in_egroup_p(gid) != 0;
|
---|
779 | }
|
---|
780 |
|
---|
781 |
|
---|
782 | /**
|
---|
783 | * Check if we can positively or negatively determine that the process is
|
---|
784 | * running under a login on the physical machine console.
|
---|
785 | *
|
---|
786 | * Havne't found a good way to figure this out for graphical sessions, so this
|
---|
787 | * is mostly pointless. But let us try do what we can do.
|
---|
788 | *
|
---|
789 | * @returns VMMDEV_REQUESTOR_CON_XXX.
|
---|
790 | */
|
---|
791 | static uint32_t vgdrvLinuxRequestorOnConsole(void)
|
---|
792 | {
|
---|
793 | uint32_t fRet = VMMDEV_REQUESTOR_CON_DONT_KNOW;
|
---|
794 |
|
---|
795 | #if RTLNX_VER_MIN(2,6,28) /* First with tty_kref_put(). */
|
---|
796 | /*
|
---|
797 | * Check for tty0..63, ASSUMING that these are only used for the physical console.
|
---|
798 | */
|
---|
799 | struct tty_struct *pTty = get_current_tty();
|
---|
800 | if (pTty)
|
---|
801 | {
|
---|
802 | # if RTLNX_VER_MIN(4,2,0)
|
---|
803 | const char *pszName = tty_name(pTty);
|
---|
804 | # else
|
---|
805 | char szBuf[64];
|
---|
806 | const char *pszName = tty_name(pTty, szBuf);
|
---|
807 | # endif
|
---|
808 | if ( pszName
|
---|
809 | && pszName[0] == 't'
|
---|
810 | && pszName[1] == 't'
|
---|
811 | && pszName[2] == 'y'
|
---|
812 | && RT_C_IS_DIGIT(pszName[3])
|
---|
813 | && ( pszName[4] == '\0'
|
---|
814 | || ( RT_C_IS_DIGIT(pszName[4])
|
---|
815 | && pszName[5] == '\0'
|
---|
816 | && (pszName[3] - '0') * 10 + (pszName[4] - '0') <= 63)) )
|
---|
817 | fRet = VMMDEV_REQUESTOR_CON_YES;
|
---|
818 | tty_kref_put(pTty);
|
---|
819 | }
|
---|
820 | #endif
|
---|
821 |
|
---|
822 | return fRet;
|
---|
823 | }
|
---|
824 |
|
---|
825 |
|
---|
826 | /**
|
---|
827 | * Device open. Called on open /dev/vboxdrv
|
---|
828 | *
|
---|
829 | * @param pInode Pointer to inode info structure.
|
---|
830 | * @param pFilp Associated file pointer.
|
---|
831 | */
|
---|
832 | static int vgdrvLinuxOpen(struct inode *pInode, struct file *pFilp)
|
---|
833 | {
|
---|
834 | int rc;
|
---|
835 | PVBOXGUESTSESSION pSession;
|
---|
836 | uint32_t fRequestor;
|
---|
837 | Log((DEVICE_NAME ": pFilp=%p pid=%d/%d %s\n", pFilp, RTProcSelf(), current->pid, current->comm));
|
---|
838 |
|
---|
839 | /*
|
---|
840 | * Figure out the requestor flags.
|
---|
841 | * ASSUMES that the gid of /dev/vboxuser is what we should consider the special vbox group.
|
---|
842 | */
|
---|
843 | fRequestor = VMMDEV_REQUESTOR_USERMODE | VMMDEV_REQUESTOR_TRUST_NOT_GIVEN;
|
---|
844 | if (vgdrvLinuxGetUid() == 0)
|
---|
845 | fRequestor |= VMMDEV_REQUESTOR_USR_ROOT;
|
---|
846 | else
|
---|
847 | fRequestor |= VMMDEV_REQUESTOR_USR_USER;
|
---|
848 | if (MINOR(pInode->i_rdev) == g_MiscDeviceUser.minor)
|
---|
849 | {
|
---|
850 | fRequestor |= VMMDEV_REQUESTOR_USER_DEVICE;
|
---|
851 | if (!vgdrvLinuxIsGroupZero(pInode->i_gid) && vgdrvLinuxIsInGroupEff(pInode->i_gid))
|
---|
852 | fRequestor |= VMMDEV_REQUESTOR_GRP_VBOX;
|
---|
853 | }
|
---|
854 | fRequestor |= vgdrvLinuxRequestorOnConsole();
|
---|
855 |
|
---|
856 | /*
|
---|
857 | * Call common code to create the user session. Associate it with
|
---|
858 | * the file so we can access it in the other methods.
|
---|
859 | */
|
---|
860 | rc = VGDrvCommonCreateUserSession(&g_DevExt, fRequestor, &pSession);
|
---|
861 | if (RT_SUCCESS(rc))
|
---|
862 | pFilp->private_data = pSession;
|
---|
863 |
|
---|
864 | Log(("vgdrvLinuxOpen: g_DevExt=%p pSession=%p rc=%d/%d (pid=%d/%d %s)\n",
|
---|
865 | &g_DevExt, pSession, rc, vgdrvLinuxConvertToNegErrno(rc), RTProcSelf(), current->pid, current->comm));
|
---|
866 | return vgdrvLinuxConvertToNegErrno(rc);
|
---|
867 | }
|
---|
868 |
|
---|
869 |
|
---|
870 | /**
|
---|
871 | * Close device.
|
---|
872 | *
|
---|
873 | * @param pInode Pointer to inode info structure.
|
---|
874 | * @param pFilp Associated file pointer.
|
---|
875 | */
|
---|
876 | static int vgdrvLinuxRelease(struct inode *pInode, struct file *pFilp)
|
---|
877 | {
|
---|
878 | Log(("vgdrvLinuxRelease: pFilp=%p pSession=%p pid=%d/%d %s\n",
|
---|
879 | pFilp, pFilp->private_data, RTProcSelf(), current->pid, current->comm));
|
---|
880 |
|
---|
881 | #if RTLNX_VER_MAX(2,6,28)
|
---|
882 | /* This housekeeping was needed in older kernel versions to ensure that
|
---|
883 | * the file pointer didn't get left on the polling queue. */
|
---|
884 | vgdrvLinuxFAsync(-1, pFilp, 0);
|
---|
885 | #endif
|
---|
886 | VGDrvCommonCloseSession(&g_DevExt, (PVBOXGUESTSESSION)pFilp->private_data);
|
---|
887 | pFilp->private_data = NULL;
|
---|
888 | return 0;
|
---|
889 | }
|
---|
890 |
|
---|
891 |
|
---|
892 | /**
|
---|
893 | * Device I/O Control entry point.
|
---|
894 | *
|
---|
895 | * @param pFilp Associated file pointer.
|
---|
896 | * @param uCmd The function specified to ioctl().
|
---|
897 | * @param ulArg The argument specified to ioctl().
|
---|
898 | */
|
---|
899 | #if defined(HAVE_UNLOCKED_IOCTL) || defined(DOXYGEN_RUNNING)
|
---|
900 | static long vgdrvLinuxIOCtl(struct file *pFilp, unsigned int uCmd, unsigned long ulArg)
|
---|
901 | #else
|
---|
902 | static int vgdrvLinuxIOCtl(struct inode *pInode, struct file *pFilp, unsigned int uCmd, unsigned long ulArg)
|
---|
903 | #endif
|
---|
904 | {
|
---|
905 | PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)pFilp->private_data;
|
---|
906 | int rc;
|
---|
907 | #ifndef HAVE_UNLOCKED_IOCTL
|
---|
908 | unlock_kernel();
|
---|
909 | #endif
|
---|
910 |
|
---|
911 | #if 0 /* no fast I/O controls defined atm. */
|
---|
912 | if (RT_LIKELY( ( uCmd == SUP_IOCTL_FAST_DO_RAW_RUN
|
---|
913 | || uCmd == SUP_IOCTL_FAST_DO_HM_RUN
|
---|
914 | || uCmd == SUP_IOCTL_FAST_DO_NOP)
|
---|
915 | && pSession->fUnrestricted == true))
|
---|
916 | rc = VGDrvCommonIoCtlFast(uCmd, ulArg, &g_DevExt, pSession);
|
---|
917 | else
|
---|
918 | #endif
|
---|
919 | rc = vgdrvLinuxIOCtlSlow(pFilp, uCmd, ulArg, pSession);
|
---|
920 |
|
---|
921 | #ifndef HAVE_UNLOCKED_IOCTL
|
---|
922 | lock_kernel();
|
---|
923 | #endif
|
---|
924 | return rc;
|
---|
925 | }
|
---|
926 |
|
---|
927 |
|
---|
928 | /**
|
---|
929 | * Device I/O Control entry point, slow variant.
|
---|
930 | *
|
---|
931 | * @param pFilp Associated file pointer.
|
---|
932 | * @param uCmd The function specified to ioctl().
|
---|
933 | * @param ulArg The argument specified to ioctl().
|
---|
934 | * @param pSession The session instance.
|
---|
935 | */
|
---|
936 | static int vgdrvLinuxIOCtlSlow(struct file *pFilp, unsigned int uCmd, unsigned long ulArg, PVBOXGUESTSESSION pSession)
|
---|
937 | {
|
---|
938 | int rc;
|
---|
939 | VBGLREQHDR Hdr;
|
---|
940 | PVBGLREQHDR pHdr;
|
---|
941 | uint32_t cbBuf;
|
---|
942 |
|
---|
943 | Log6(("vgdrvLinuxIOCtlSlow: pFilp=%p uCmd=%#x ulArg=%p pid=%d/%d\n", pFilp, uCmd, (void *)ulArg, RTProcSelf(), current->pid));
|
---|
944 |
|
---|
945 | /*
|
---|
946 | * Read the header.
|
---|
947 | */
|
---|
948 | if (RT_FAILURE(RTR0MemUserCopyFrom(&Hdr, ulArg, sizeof(Hdr))))
|
---|
949 | {
|
---|
950 | Log(("vgdrvLinuxIOCtlSlow: copy_from_user(,%#lx,) failed; uCmd=%#x\n", ulArg, uCmd));
|
---|
951 | return -EFAULT;
|
---|
952 | }
|
---|
953 | if (RT_UNLIKELY(Hdr.uVersion != VBGLREQHDR_VERSION))
|
---|
954 | {
|
---|
955 | Log(("vgdrvLinuxIOCtlSlow: bad header version %#x; uCmd=%#x\n", Hdr.uVersion, uCmd));
|
---|
956 | return -EINVAL;
|
---|
957 | }
|
---|
958 |
|
---|
959 | /*
|
---|
960 | * Buffer the request.
|
---|
961 | * Note! The header is revalidated by the common code.
|
---|
962 | */
|
---|
963 | cbBuf = RT_MAX(Hdr.cbIn, Hdr.cbOut);
|
---|
964 | if (RT_UNLIKELY(cbBuf > _1M*16))
|
---|
965 | {
|
---|
966 | Log(("vgdrvLinuxIOCtlSlow: too big cbBuf=%#x; uCmd=%#x\n", cbBuf, uCmd));
|
---|
967 | return -E2BIG;
|
---|
968 | }
|
---|
969 | if (RT_UNLIKELY( Hdr.cbIn < sizeof(Hdr)
|
---|
970 | || (cbBuf != _IOC_SIZE(uCmd) && _IOC_SIZE(uCmd) != 0)))
|
---|
971 | {
|
---|
972 | Log(("vgdrvLinuxIOCtlSlow: bad ioctl cbBuf=%#x _IOC_SIZE=%#x; uCmd=%#x\n", cbBuf, _IOC_SIZE(uCmd), uCmd));
|
---|
973 | return -EINVAL;
|
---|
974 | }
|
---|
975 | pHdr = RTMemAlloc(cbBuf);
|
---|
976 | if (RT_UNLIKELY(!pHdr))
|
---|
977 | {
|
---|
978 | LogRel(("vgdrvLinuxIOCtlSlow: failed to allocate buffer of %d bytes for uCmd=%#x\n", cbBuf, uCmd));
|
---|
979 | return -ENOMEM;
|
---|
980 | }
|
---|
981 | if (RT_FAILURE(RTR0MemUserCopyFrom(pHdr, ulArg, Hdr.cbIn)))
|
---|
982 | {
|
---|
983 | Log(("vgdrvLinuxIOCtlSlow: copy_from_user(,%#lx, %#x) failed; uCmd=%#x\n", ulArg, Hdr.cbIn, uCmd));
|
---|
984 | RTMemFree(pHdr);
|
---|
985 | return -EFAULT;
|
---|
986 | }
|
---|
987 | if (Hdr.cbIn < cbBuf)
|
---|
988 | RT_BZERO((uint8_t *)pHdr + Hdr.cbIn, cbBuf - Hdr.cbIn);
|
---|
989 |
|
---|
990 | /*
|
---|
991 | * Process the IOCtl.
|
---|
992 | */
|
---|
993 | rc = VGDrvCommonIoCtl(uCmd, &g_DevExt, pSession, pHdr, cbBuf);
|
---|
994 |
|
---|
995 | /*
|
---|
996 | * Copy ioctl data and output buffer back to user space.
|
---|
997 | */
|
---|
998 | if (RT_SUCCESS(rc))
|
---|
999 | {
|
---|
1000 | uint32_t cbOut = pHdr->cbOut;
|
---|
1001 | if (RT_UNLIKELY(cbOut > cbBuf))
|
---|
1002 | {
|
---|
1003 | LogRel(("vgdrvLinuxIOCtlSlow: too much output! %#x > %#x; uCmd=%#x!\n", cbOut, cbBuf, uCmd));
|
---|
1004 | cbOut = cbBuf;
|
---|
1005 | }
|
---|
1006 | if (RT_FAILURE(RTR0MemUserCopyTo(ulArg, pHdr, cbOut)))
|
---|
1007 | {
|
---|
1008 | /* this is really bad! */
|
---|
1009 | LogRel(("vgdrvLinuxIOCtlSlow: copy_to_user(%#lx,,%#x); uCmd=%#x!\n", ulArg, cbOut, uCmd));
|
---|
1010 | rc = -EFAULT;
|
---|
1011 | }
|
---|
1012 | }
|
---|
1013 | else
|
---|
1014 | {
|
---|
1015 | Log(("vgdrvLinuxIOCtlSlow: pFilp=%p uCmd=%#x ulArg=%p failed, rc=%d\n", pFilp, uCmd, (void *)ulArg, rc));
|
---|
1016 | rc = -EINVAL;
|
---|
1017 | }
|
---|
1018 | RTMemFree(pHdr);
|
---|
1019 |
|
---|
1020 | Log6(("vgdrvLinuxIOCtlSlow: returns %d (pid=%d/%d)\n", rc, RTProcSelf(), current->pid));
|
---|
1021 | return rc;
|
---|
1022 | }
|
---|
1023 |
|
---|
1024 |
|
---|
1025 | /**
|
---|
1026 | * @note This code is duplicated on other platforms with variations, so please
|
---|
1027 | * keep them all up to date when making changes!
|
---|
1028 | */
|
---|
1029 | int VBOXCALL VBoxGuestIDC(void *pvSession, uintptr_t uReq, PVBGLREQHDR pReqHdr, size_t cbReq)
|
---|
1030 | {
|
---|
1031 | /*
|
---|
1032 | * Simple request validation (common code does the rest).
|
---|
1033 | */
|
---|
1034 | int rc;
|
---|
1035 | if ( RT_VALID_PTR(pReqHdr)
|
---|
1036 | && cbReq >= sizeof(*pReqHdr))
|
---|
1037 | {
|
---|
1038 | /*
|
---|
1039 | * All requests except the connect one requires a valid session.
|
---|
1040 | */
|
---|
1041 | PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)pvSession;
|
---|
1042 | if (pSession)
|
---|
1043 | {
|
---|
1044 | if ( RT_VALID_PTR(pSession)
|
---|
1045 | && pSession->pDevExt == &g_DevExt)
|
---|
1046 | rc = VGDrvCommonIoCtl(uReq, &g_DevExt, pSession, pReqHdr, cbReq);
|
---|
1047 | else
|
---|
1048 | rc = VERR_INVALID_HANDLE;
|
---|
1049 | }
|
---|
1050 | else if (uReq == VBGL_IOCTL_IDC_CONNECT)
|
---|
1051 | {
|
---|
1052 | rc = VGDrvCommonCreateKernelSession(&g_DevExt, &pSession);
|
---|
1053 | if (RT_SUCCESS(rc))
|
---|
1054 | {
|
---|
1055 | rc = VGDrvCommonIoCtl(uReq, &g_DevExt, pSession, pReqHdr, cbReq);
|
---|
1056 | if (RT_FAILURE(rc))
|
---|
1057 | VGDrvCommonCloseSession(&g_DevExt, pSession);
|
---|
1058 | }
|
---|
1059 | }
|
---|
1060 | else
|
---|
1061 | rc = VERR_INVALID_HANDLE;
|
---|
1062 | }
|
---|
1063 | else
|
---|
1064 | rc = VERR_INVALID_POINTER;
|
---|
1065 | return rc;
|
---|
1066 | }
|
---|
1067 | EXPORT_SYMBOL(VBoxGuestIDC);
|
---|
1068 |
|
---|
1069 |
|
---|
1070 | /**
|
---|
1071 | * Asynchronous notification activation method.
|
---|
1072 | *
|
---|
1073 | * @returns 0 on success, negative errno on failure.
|
---|
1074 | *
|
---|
1075 | * @param fd The file descriptor.
|
---|
1076 | * @param pFile The file structure.
|
---|
1077 | * @param fOn On/off indicator.
|
---|
1078 | */
|
---|
1079 | static int vgdrvLinuxFAsync(int fd, struct file *pFile, int fOn)
|
---|
1080 | {
|
---|
1081 | return fasync_helper(fd, pFile, fOn, &g_pFAsyncQueue);
|
---|
1082 | }
|
---|
1083 |
|
---|
1084 |
|
---|
1085 | /**
|
---|
1086 | * Poll function.
|
---|
1087 | *
|
---|
1088 | * This returns ready to read if the mouse pointer mode or the pointer position
|
---|
1089 | * has changed since last call to read.
|
---|
1090 | *
|
---|
1091 | * @returns 0 if no changes, POLLIN | POLLRDNORM if there are unseen changes.
|
---|
1092 | *
|
---|
1093 | * @param pFile The file structure.
|
---|
1094 | * @param pPt The poll table.
|
---|
1095 | *
|
---|
1096 | * @remarks This is probably not really used, X11 is said to use the fasync
|
---|
1097 | * interface instead.
|
---|
1098 | */
|
---|
1099 | static unsigned int vgdrvLinuxPoll(struct file *pFile, poll_table *pPt)
|
---|
1100 | {
|
---|
1101 | PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)pFile->private_data;
|
---|
1102 | uint32_t u32CurSeq = ASMAtomicUoReadU32(&g_DevExt.u32MousePosChangedSeq);
|
---|
1103 | unsigned int fMask = pSession->u32MousePosChangedSeq != u32CurSeq
|
---|
1104 | ? POLLIN | POLLRDNORM
|
---|
1105 | : 0;
|
---|
1106 | poll_wait(pFile, &g_PollEventQueue, pPt);
|
---|
1107 | return fMask;
|
---|
1108 | }
|
---|
1109 |
|
---|
1110 |
|
---|
1111 | /**
|
---|
1112 | * Read to go with our poll/fasync response.
|
---|
1113 | *
|
---|
1114 | * @returns 1 or -EINVAL.
|
---|
1115 | *
|
---|
1116 | * @param pFile The file structure.
|
---|
1117 | * @param pbBuf The buffer to read into.
|
---|
1118 | * @param cbRead The max number of bytes to read.
|
---|
1119 | * @param poff The current file position.
|
---|
1120 | *
|
---|
1121 | * @remarks This is probably not really used as X11 lets the driver do its own
|
---|
1122 | * event reading. The poll condition is therefore also cleared when we
|
---|
1123 | * see VMMDevReq_GetMouseStatus in vgdrvIoCtl_VMMRequest.
|
---|
1124 | */
|
---|
1125 | static ssize_t vgdrvLinuxRead(struct file *pFile, char *pbBuf, size_t cbRead, loff_t *poff)
|
---|
1126 | {
|
---|
1127 | PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)pFile->private_data;
|
---|
1128 | uint32_t u32CurSeq = ASMAtomicUoReadU32(&g_DevExt.u32MousePosChangedSeq);
|
---|
1129 |
|
---|
1130 | if (*poff != 0)
|
---|
1131 | return -EINVAL;
|
---|
1132 |
|
---|
1133 | /*
|
---|
1134 | * Fake a single byte read if we're not up to date with the current mouse position.
|
---|
1135 | */
|
---|
1136 | if ( pSession->u32MousePosChangedSeq != u32CurSeq
|
---|
1137 | && cbRead > 0)
|
---|
1138 | {
|
---|
1139 | pSession->u32MousePosChangedSeq = u32CurSeq;
|
---|
1140 | pbBuf[0] = 0;
|
---|
1141 | return 1;
|
---|
1142 | }
|
---|
1143 | return 0;
|
---|
1144 | }
|
---|
1145 |
|
---|
1146 |
|
---|
1147 | void VGDrvNativeISRMousePollEvent(PVBOXGUESTDEVEXT pDevExt)
|
---|
1148 | {
|
---|
1149 | #ifdef VBOXGUEST_WITH_INPUT_DRIVER
|
---|
1150 | int rc;
|
---|
1151 | #endif
|
---|
1152 | NOREF(pDevExt);
|
---|
1153 |
|
---|
1154 | /*
|
---|
1155 | * Wake up everyone that's in a poll() and post anyone that has
|
---|
1156 | * subscribed to async notifications.
|
---|
1157 | */
|
---|
1158 | Log3(("VGDrvNativeISRMousePollEvent: wake_up_all\n"));
|
---|
1159 | wake_up_all(&g_PollEventQueue);
|
---|
1160 | Log3(("VGDrvNativeISRMousePollEvent: kill_fasync\n"));
|
---|
1161 | kill_fasync(&g_pFAsyncQueue, SIGIO, POLL_IN);
|
---|
1162 | #ifdef VBOXGUEST_WITH_INPUT_DRIVER
|
---|
1163 | /* Report events to the kernel input device */
|
---|
1164 | g_pMouseStatusReq->mouseFeatures = 0;
|
---|
1165 | g_pMouseStatusReq->pointerXPos = 0;
|
---|
1166 | g_pMouseStatusReq->pointerYPos = 0;
|
---|
1167 | rc = VbglR0GRPerform(&g_pMouseStatusReq->header);
|
---|
1168 | if (RT_SUCCESS(rc))
|
---|
1169 | {
|
---|
1170 | input_report_abs(g_pInputDevice, ABS_X,
|
---|
1171 | g_pMouseStatusReq->pointerXPos);
|
---|
1172 | input_report_abs(g_pInputDevice, ABS_Y,
|
---|
1173 | g_pMouseStatusReq->pointerYPos);
|
---|
1174 | # ifdef EV_SYN
|
---|
1175 | input_sync(g_pInputDevice);
|
---|
1176 | # endif
|
---|
1177 | }
|
---|
1178 | #endif
|
---|
1179 | Log3(("VGDrvNativeISRMousePollEvent: done\n"));
|
---|
1180 | }
|
---|
1181 |
|
---|
1182 |
|
---|
1183 | bool VGDrvNativeProcessOption(PVBOXGUESTDEVEXT pDevExt, const char *pszName, const char *pszValue)
|
---|
1184 | {
|
---|
1185 | RT_NOREF(pDevExt); RT_NOREF(pszName); RT_NOREF(pszValue);
|
---|
1186 | return false;
|
---|
1187 | }
|
---|
1188 |
|
---|
1189 |
|
---|
1190 | #if RTLNX_VER_MIN(2,6,0)
|
---|
1191 |
|
---|
1192 | /** log and dbg_log parameter setter. */
|
---|
1193 | static int vgdrvLinuxParamLogGrpSet(const char *pszValue, CONST_4_15 struct kernel_param *pParam)
|
---|
1194 | {
|
---|
1195 | if (g_fLoggerCreated)
|
---|
1196 | {
|
---|
1197 | PRTLOGGER pLogger = pParam->name[0] == 'd' ? RTLogDefaultInstance() : RTLogRelGetDefaultInstance();
|
---|
1198 | if (pLogger)
|
---|
1199 | RTLogGroupSettings(pLogger, pszValue);
|
---|
1200 | }
|
---|
1201 | else if (pParam->name[0] != 'd')
|
---|
1202 | strlcpy(&g_szLogGrp[0], pszValue, sizeof(g_szLogGrp));
|
---|
1203 |
|
---|
1204 | return 0;
|
---|
1205 | }
|
---|
1206 |
|
---|
1207 | /** log and dbg_log parameter getter. */
|
---|
1208 | static int vgdrvLinuxParamLogGrpGet(char *pszBuf, CONST_4_15 struct kernel_param *pParam)
|
---|
1209 | {
|
---|
1210 | PRTLOGGER pLogger = pParam->name[0] == 'd' ? RTLogDefaultInstance() : RTLogRelGetDefaultInstance();
|
---|
1211 | *pszBuf = '\0';
|
---|
1212 | if (pLogger)
|
---|
1213 | RTLogQueryGroupSettings(pLogger, pszBuf, _4K);
|
---|
1214 | return strlen(pszBuf);
|
---|
1215 | }
|
---|
1216 |
|
---|
1217 |
|
---|
1218 | /** log and dbg_log_flags parameter setter. */
|
---|
1219 | static int vgdrvLinuxParamLogFlagsSet(const char *pszValue, CONST_4_15 struct kernel_param *pParam)
|
---|
1220 | {
|
---|
1221 | if (g_fLoggerCreated)
|
---|
1222 | {
|
---|
1223 | PRTLOGGER pLogger = pParam->name[0] == 'd' ? RTLogDefaultInstance() : RTLogRelGetDefaultInstance();
|
---|
1224 | if (pLogger)
|
---|
1225 | RTLogFlags(pLogger, pszValue);
|
---|
1226 | }
|
---|
1227 | else if (pParam->name[0] != 'd')
|
---|
1228 | strlcpy(&g_szLogFlags[0], pszValue, sizeof(g_szLogFlags));
|
---|
1229 | return 0;
|
---|
1230 | }
|
---|
1231 |
|
---|
1232 | /** log and dbg_log_flags parameter getter. */
|
---|
1233 | static int vgdrvLinuxParamLogFlagsGet(char *pszBuf, CONST_4_15 struct kernel_param *pParam)
|
---|
1234 | {
|
---|
1235 | PRTLOGGER pLogger = pParam->name[0] == 'd' ? RTLogDefaultInstance() : RTLogRelGetDefaultInstance();
|
---|
1236 | *pszBuf = '\0';
|
---|
1237 | if (pLogger)
|
---|
1238 | RTLogQueryFlags(pLogger, pszBuf, _4K);
|
---|
1239 | return strlen(pszBuf);
|
---|
1240 | }
|
---|
1241 |
|
---|
1242 |
|
---|
1243 | /** log and dbg_log_dest parameter setter. */
|
---|
1244 | static int vgdrvLinuxParamLogDstSet(const char *pszValue, CONST_4_15 struct kernel_param *pParam)
|
---|
1245 | {
|
---|
1246 | if (g_fLoggerCreated)
|
---|
1247 | {
|
---|
1248 | PRTLOGGER pLogger = pParam->name[0] == 'd' ? RTLogDefaultInstance() : RTLogRelGetDefaultInstance();
|
---|
1249 | if (pLogger)
|
---|
1250 | RTLogDestinations(pLogger, pszValue);
|
---|
1251 | }
|
---|
1252 | else if (pParam->name[0] != 'd')
|
---|
1253 | strlcpy(&g_szLogDst[0], pszValue, sizeof(g_szLogDst));
|
---|
1254 | return 0;
|
---|
1255 | }
|
---|
1256 |
|
---|
1257 | /** log and dbg_log_dest parameter getter. */
|
---|
1258 | static int vgdrvLinuxParamLogDstGet(char *pszBuf, CONST_4_15 struct kernel_param *pParam)
|
---|
1259 | {
|
---|
1260 | PRTLOGGER pLogger = pParam->name[0] == 'd' ? RTLogDefaultInstance() : RTLogRelGetDefaultInstance();
|
---|
1261 | *pszBuf = '\0';
|
---|
1262 | if (pLogger)
|
---|
1263 | RTLogQueryDestinations(pLogger, pszBuf, _4K);
|
---|
1264 | return strlen(pszBuf);
|
---|
1265 | }
|
---|
1266 |
|
---|
1267 |
|
---|
1268 | /** r3_log_to_host parameter setter. */
|
---|
1269 | static int vgdrvLinuxParamR3LogToHostSet(const char *pszValue, CONST_4_15 struct kernel_param *pParam)
|
---|
1270 | {
|
---|
1271 | g_DevExt.fLoggingEnabled = VBDrvCommonIsOptionValueTrue(pszValue);
|
---|
1272 | return 0;
|
---|
1273 | }
|
---|
1274 |
|
---|
1275 | /** r3_log_to_host parameter getter. */
|
---|
1276 | static int vgdrvLinuxParamR3LogToHostGet(char *pszBuf, CONST_4_15 struct kernel_param *pParam)
|
---|
1277 | {
|
---|
1278 | strcpy(pszBuf, g_DevExt.fLoggingEnabled ? "enabled" : "disabled");
|
---|
1279 | return strlen(pszBuf);
|
---|
1280 | }
|
---|
1281 |
|
---|
1282 |
|
---|
1283 | /*
|
---|
1284 | * Define module parameters.
|
---|
1285 | */
|
---|
1286 | module_param_call(log, vgdrvLinuxParamLogGrpSet, vgdrvLinuxParamLogGrpGet, NULL, 0664);
|
---|
1287 | module_param_call(log_flags, vgdrvLinuxParamLogFlagsSet, vgdrvLinuxParamLogFlagsGet, NULL, 0664);
|
---|
1288 | module_param_call(log_dest, vgdrvLinuxParamLogDstSet, vgdrvLinuxParamLogDstGet, NULL, 0664);
|
---|
1289 | # ifdef LOG_ENABLED
|
---|
1290 | module_param_call(dbg_log, vgdrvLinuxParamLogGrpSet, vgdrvLinuxParamLogGrpGet, NULL, 0664);
|
---|
1291 | module_param_call(dbg_log_flags, vgdrvLinuxParamLogFlagsSet, vgdrvLinuxParamLogFlagsGet, NULL, 0664);
|
---|
1292 | module_param_call(dbg_log_dest, vgdrvLinuxParamLogDstSet, vgdrvLinuxParamLogDstGet, NULL, 0664);
|
---|
1293 | # endif
|
---|
1294 | module_param_call(r3_log_to_host, vgdrvLinuxParamR3LogToHostSet, vgdrvLinuxParamR3LogToHostGet, NULL, 0664);
|
---|
1295 |
|
---|
1296 | #endif /* 2.6.0 and later */
|
---|
1297 |
|
---|
1298 |
|
---|
1299 | module_init(vgdrvLinuxModInit);
|
---|
1300 | module_exit(vgdrvLinuxModExit);
|
---|
1301 |
|
---|
1302 | MODULE_AUTHOR(VBOX_VENDOR);
|
---|
1303 | MODULE_DESCRIPTION(VBOX_PRODUCT " Guest Additions for Linux Module");
|
---|
1304 | MODULE_LICENSE("GPL");
|
---|
1305 | #ifdef MODULE_VERSION
|
---|
1306 | MODULE_VERSION(VBOX_VERSION_STRING " r" RT_XSTR(VBOX_SVN_REV));
|
---|
1307 | #endif
|
---|
1308 |
|
---|