VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxGuest/VBoxGuest-linux.c@ 52700

Last change on this file since 52700 was 52700, checked in by vboxsync, 10 years ago

Additions/VBoxGuest: add a flag in the session structure to differentiate whether a process opened the user or the main device node (Linux guests only).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 32.6 KB
Line 
1/* $Rev: 52700 $ */
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-2013 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
21/*******************************************************************************
22* Header Files *
23*******************************************************************************/
24#define LOG_GROUP LOG_GROUP_SUP_DRV
25
26#include "the-linux-kernel.h"
27
28#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 15)
29# define VBOXGUEST_WITH_INPUT_DRIVER
30#endif
31
32#include "VBoxGuestInternal.h"
33#ifdef VBOXGUEST_WITH_INPUT_DRIVER
34# include <linux/input.h>
35#endif
36#include <linux/miscdevice.h>
37#include <linux/poll.h>
38#include <VBox/version.h>
39
40#include <iprt/assert.h>
41#include <iprt/asm.h>
42#include <iprt/err.h>
43#include <iprt/initterm.h>
44#include <iprt/mem.h>
45#include <iprt/mp.h>
46#include <iprt/process.h>
47#include <iprt/spinlock.h>
48#include <iprt/semaphore.h>
49#include <VBox/log.h>
50
51
52/*******************************************************************************
53* Defined Constants And Macros *
54*******************************************************************************/
55/** The device name. */
56#define DEVICE_NAME "vboxguest"
57/** The device name for the device node open to everyone.. */
58#define DEVICE_NAME_USER "vboxuser"
59/** The name of the PCI driver */
60#define DRIVER_NAME DEVICE_NAME
61
62
63/* 2.4.x compatibility macros that may or may not be defined. */
64#ifndef IRQ_RETVAL
65# define irqreturn_t void
66# define IRQ_RETVAL(n)
67#endif
68
69
70/*******************************************************************************
71* Internal Functions *
72*******************************************************************************/
73static void vboxguestLinuxTermPci(struct pci_dev *pPciDev);
74static int vboxguestLinuxModInit(void);
75static void vboxguestLinuxModExit(void);
76static int vboxguestLinuxOpen(struct inode *pInode, struct file *pFilp);
77static int vboxguestLinuxRelease(struct inode *pInode, struct file *pFilp);
78#ifdef HAVE_UNLOCKED_IOCTL
79static long vboxguestLinuxIOCtl(struct file *pFilp, unsigned int uCmd, unsigned long ulArg);
80#else
81static int vboxguestLinuxIOCtl(struct inode *pInode, struct file *pFilp, unsigned int uCmd, unsigned long ulArg);
82#endif
83static int vboxguestFAsync(int fd, struct file *pFile, int fOn);
84static unsigned int vboxguestPoll(struct file *pFile, poll_table *pPt);
85static ssize_t vboxguestRead(struct file *pFile, char *pbBuf, size_t cbRead, loff_t *poff);
86
87
88/*******************************************************************************
89* Global Variables *
90*******************************************************************************/
91/**
92 * Device extention & session data association structure.
93 */
94static VBOXGUESTDEVEXT g_DevExt;
95/** The PCI device. */
96static struct pci_dev *g_pPciDev = NULL;
97/** The base of the I/O port range. */
98static RTIOPORT g_IOPortBase;
99/** The base of the MMIO range. */
100static RTHCPHYS g_MMIOPhysAddr = NIL_RTHCPHYS;
101/** The size of the MMIO range as seen by PCI. */
102static uint32_t g_cbMMIO;
103/** The pointer to the mapping of the MMIO range. */
104static void *g_pvMMIOBase;
105/** Wait queue used by polling. */
106static wait_queue_head_t g_PollEventQueue;
107/** Asynchronous notification stuff. */
108static struct fasync_struct *g_pFAsyncQueue;
109#ifdef VBOXGUEST_WITH_INPUT_DRIVER
110/** Pre-allocated mouse status VMMDev request for use in the IRQ
111 * handler. */
112static VMMDevReqMouseStatus *g_pMouseStatusReq;
113#endif
114#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
115/** Whether we've create the logger or not. */
116static volatile bool g_fLoggerCreated;
117/** Release logger group settings. */
118static char g_szLogGrp[128];
119/** Release logger flags settings. */
120static char g_szLogFlags[128];
121/** Release logger destination settings. */
122static char g_szLogDst[128];
123# if 0
124/** Debug logger group settings. */
125static char g_szDbgLogGrp[128];
126/** Debug logger flags settings. */
127static char g_szDbgLogFlags[128];
128/** Debug logger destination settings. */
129static char g_szDbgLogDst[128];
130# endif
131#endif
132
133/** The input device handle */
134#ifdef VBOXGUEST_WITH_INPUT_DRIVER
135static struct input_dev *g_pInputDevice = NULL;
136#endif
137
138/** The file_operations structure. */
139static struct file_operations g_FileOps =
140{
141 owner: THIS_MODULE,
142 open: vboxguestLinuxOpen,
143 release: vboxguestLinuxRelease,
144#ifdef HAVE_UNLOCKED_IOCTL
145 unlocked_ioctl: vboxguestLinuxIOCtl,
146#else
147 ioctl: vboxguestLinuxIOCtl,
148#endif
149 fasync: vboxguestFAsync,
150 read: vboxguestRead,
151 poll: vboxguestPoll,
152 llseek: no_llseek,
153};
154
155/** The miscdevice structure. */
156static struct miscdevice g_MiscDevice =
157{
158 minor: MISC_DYNAMIC_MINOR,
159 name: DEVICE_NAME,
160 fops: &g_FileOps,
161};
162
163/** The file_operations structure for the user device.
164 * @remarks For the time being we'll be using the same implementation as
165 * /dev/vboxguest here. */
166static struct file_operations g_FileOpsUser =
167{
168 owner: THIS_MODULE,
169 open: vboxguestLinuxOpen,
170 release: vboxguestLinuxRelease,
171#ifdef HAVE_UNLOCKED_IOCTL
172 unlocked_ioctl: vboxguestLinuxIOCtl,
173#else
174 ioctl: vboxguestLinuxIOCtl,
175#endif
176};
177
178/** The miscdevice structure for the user device. */
179static struct miscdevice g_MiscDeviceUser =
180{
181 minor: MISC_DYNAMIC_MINOR,
182 name: DEVICE_NAME_USER,
183 fops: &g_FileOpsUser,
184};
185
186
187/** PCI hotplug structure. */
188static const struct pci_device_id
189#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 8, 0)
190__devinitdata
191#endif
192g_VBoxGuestPciId[] =
193{
194 {
195 vendor: VMMDEV_VENDORID,
196 device: VMMDEV_DEVICEID
197 },
198 {
199 /* empty entry */
200 }
201};
202MODULE_DEVICE_TABLE(pci, g_VBoxGuestPciId);
203
204static PVBOXGUESTSESSION g_pKernelSession = NULL;
205
206/**
207 * Converts a VBox status code to a linux error code.
208 *
209 * @returns corresponding negative linux error code.
210 * @param rc supdrv error code (SUPDRV_ERR_* defines).
211 */
212static int vboxguestLinuxConvertToNegErrno(int rc)
213{
214 if ( rc > -1000
215 && rc < 1000)
216 return -RTErrConvertToErrno(rc);
217 switch (rc)
218 {
219 case VERR_HGCM_SERVICE_NOT_FOUND: return -ESRCH;
220 case VINF_HGCM_CLIENT_REJECTED: return 0;
221 case VERR_HGCM_INVALID_CMD_ADDRESS: return -EFAULT;
222 case VINF_HGCM_ASYNC_EXECUTE: return 0;
223 case VERR_HGCM_INTERNAL: return -EPROTO;
224 case VERR_HGCM_INVALID_CLIENT_ID: return -EINVAL;
225 case VINF_HGCM_SAVE_STATE: return 0;
226 /* No reason to return this to a guest */
227 // case VERR_HGCM_SERVICE_EXISTS: return -EEXIST;
228 default:
229 AssertMsgFailed(("Unhandled error code %Rrc\n", rc));
230 return -EPROTO;
231 }
232}
233
234
235
236/**
237 * Does the PCI detection and init of the device.
238 *
239 * @returns 0 on success, negated errno on failure.
240 */
241static int vboxguestLinuxProbePci(struct pci_dev *pPciDev,
242 const struct pci_device_id *id)
243{
244 int rc;
245
246 NOREF(id);
247 AssertReturn(!g_pPciDev, -EINVAL);
248 rc = pci_enable_device(pPciDev);
249 if (rc >= 0)
250 {
251 /* I/O Ports are mandatory, the MMIO bit is not. */
252 g_IOPortBase = pci_resource_start(pPciDev, 0);
253 if (g_IOPortBase != 0)
254 {
255 /*
256 * Map the register address space.
257 */
258 g_MMIOPhysAddr = pci_resource_start(pPciDev, 1);
259 g_cbMMIO = pci_resource_len(pPciDev, 1);
260 if (request_mem_region(g_MMIOPhysAddr, g_cbMMIO, DEVICE_NAME) != NULL)
261 {
262 g_pvMMIOBase = ioremap(g_MMIOPhysAddr, g_cbMMIO);
263 if (g_pvMMIOBase)
264 {
265 /** @todo why aren't we requesting ownership of the I/O ports as well? */
266 g_pPciDev = pPciDev;
267 return 0;
268 }
269
270 /* failure cleanup path */
271 LogRel((DEVICE_NAME ": ioremap failed; MMIO Addr=%RHp cb=%#x\n", g_MMIOPhysAddr, g_cbMMIO));
272 rc = -ENOMEM;
273 release_mem_region(g_MMIOPhysAddr, g_cbMMIO);
274 }
275 else
276 {
277 LogRel((DEVICE_NAME ": failed to obtain adapter memory\n"));
278 rc = -EBUSY;
279 }
280 g_MMIOPhysAddr = NIL_RTHCPHYS;
281 g_cbMMIO = 0;
282 g_IOPortBase = 0;
283 }
284 else
285 {
286 LogRel((DEVICE_NAME ": did not find expected hardware resources\n"));
287 rc = -ENXIO;
288 }
289 pci_disable_device(pPciDev);
290 }
291 else
292 LogRel((DEVICE_NAME ": could not enable device: %d\n", rc));
293 return rc;
294}
295
296
297/**
298 * Clean up the usage of the PCI device.
299 */
300static void vboxguestLinuxTermPci(struct pci_dev *pPciDev)
301{
302 g_pPciDev = NULL;
303 if (pPciDev)
304 {
305 iounmap(g_pvMMIOBase);
306 g_pvMMIOBase = NULL;
307
308 release_mem_region(g_MMIOPhysAddr, g_cbMMIO);
309 g_MMIOPhysAddr = NIL_RTHCPHYS;
310 g_cbMMIO = 0;
311
312 pci_disable_device(pPciDev);
313 }
314}
315
316
317/** Structure for registering the PCI driver. */
318static struct pci_driver g_PciDriver =
319{
320 name: DRIVER_NAME,
321 id_table: g_VBoxGuestPciId,
322 probe: vboxguestLinuxProbePci,
323 remove: vboxguestLinuxTermPci
324};
325
326
327/**
328 * Interrupt service routine.
329 *
330 * @returns In 2.4 it returns void.
331 * In 2.6 we indicate whether we've handled the IRQ or not.
332 *
333 * @param iIrq The IRQ number.
334 * @param pvDevId The device ID, a pointer to g_DevExt.
335 * @param pvRegs Register set. Removed in 2.6.19.
336 */
337#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19)
338static irqreturn_t vboxguestLinuxISR(int iIrrq, void *pvDevId)
339#else
340static irqreturn_t vboxguestLinuxISR(int iIrrq, void *pvDevId, struct pt_regs *pRegs)
341#endif
342{
343 bool fTaken = VBoxGuestCommonISR(&g_DevExt);
344 return IRQ_RETVAL(fTaken);
345}
346
347
348/**
349 * Registers the ISR and initializes the poll wait queue.
350 */
351static int __init vboxguestLinuxInitISR(void)
352{
353 int rc;
354
355 init_waitqueue_head(&g_PollEventQueue);
356 rc = request_irq(g_pPciDev->irq,
357 vboxguestLinuxISR,
358#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20)
359 IRQF_SHARED,
360#else
361 SA_SHIRQ,
362#endif
363 DEVICE_NAME,
364 &g_DevExt);
365 if (rc)
366 {
367 LogRel((DEVICE_NAME ": could not request IRQ %d: err=%d\n", g_pPciDev->irq, rc));
368 return rc;
369 }
370 return 0;
371}
372
373
374/**
375 * Deregisters the ISR.
376 */
377static void vboxguestLinuxTermISR(void)
378{
379 free_irq(g_pPciDev->irq, &g_DevExt);
380}
381
382
383#ifdef VBOXGUEST_WITH_INPUT_DRIVER
384/** Calls the kernel IOCtl to report mouse status to the host on behalf of
385 * our kernel session. */
386static int vboxguestLinuxSetMouseStatus(uint32_t fStatus)
387{
388 return VBoxGuestCommonIOCtl(VBOXGUEST_IOCTL_SET_MOUSE_STATUS, &g_DevExt,
389 g_pKernelSession, &fStatus, sizeof(fStatus),
390 NULL);
391}
392
393
394/** Called when the input device is first opened. Sets up absolute reporting.
395 */
396static int vboxguestOpenInputDevice(struct input_dev *pDev)
397{
398 NOREF(pDev);
399 if (RT_FAILURE(vboxguestLinuxSetMouseStatus
400 ( VMMDEV_MOUSE_GUEST_CAN_ABSOLUTE
401 | VMMDEV_MOUSE_NEW_PROTOCOL)))
402 return ENODEV;
403 return 0;
404}
405
406
407/** Called if all open handles to the device are closed, disables absolute
408 * reporting. */
409static void vboxguestCloseInputDevice(struct input_dev *pDev)
410{
411 NOREF(pDev);
412 vboxguestLinuxSetMouseStatus(0);
413}
414
415
416/**
417 * Creates the kernel input device.
418 */
419static int __init vboxguestLinuxCreateInputDevice(void)
420{
421 int rc;
422
423 rc = VbglGRAlloc((VMMDevRequestHeader **)&g_pMouseStatusReq,
424 sizeof(*g_pMouseStatusReq),
425 VMMDevReq_GetMouseStatus);
426 if (RT_FAILURE(rc))
427 return -ENOMEM;
428 g_pInputDevice = input_allocate_device();
429 if (!g_pInputDevice)
430 {
431 VbglGRFree(&g_pMouseStatusReq->header);
432 return -ENOMEM;
433 }
434 g_pInputDevice->id.bustype = BUS_PCI;
435 g_pInputDevice->id.vendor = VMMDEV_VENDORID;
436 g_pInputDevice->id.product = VMMDEV_DEVICEID;
437 g_pInputDevice->id.version = VBOX_SHORT_VERSION;
438 g_pInputDevice->open = vboxguestOpenInputDevice;
439 g_pInputDevice->close = vboxguestCloseInputDevice;
440# if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 22)
441 g_pInputDevice->cdev.dev = &g_pPciDev->dev;
442# else
443 g_pInputDevice->dev.parent = &g_pPciDev->dev;
444# endif
445 {
446 int rc = input_register_device(g_pInputDevice);
447 if (rc)
448 {
449 VbglGRFree(&g_pMouseStatusReq->header);
450 input_free_device(g_pInputDevice);
451 return rc;
452 }
453 }
454 /* Do what one of our competitors apparently does as that works. */
455 ASMBitSet(g_pInputDevice->evbit, EV_ABS);
456 ASMBitSet(g_pInputDevice->evbit, EV_KEY);
457# ifdef EV_SYN
458 ASMBitSet(g_pInputDevice->evbit, EV_SYN);
459# endif
460 input_set_abs_params(g_pInputDevice, ABS_X, VMMDEV_MOUSE_RANGE_MIN,
461 VMMDEV_MOUSE_RANGE_MAX, 0, 0);
462 input_set_abs_params(g_pInputDevice, ABS_Y, VMMDEV_MOUSE_RANGE_MIN,
463 VMMDEV_MOUSE_RANGE_MAX, 0, 0);
464 ASMBitSet(g_pInputDevice->keybit, BTN_MOUSE);
465 /** @todo this string should be in a header file somewhere. */
466 g_pInputDevice->name = "VirtualBox mouse integration";
467 return 0;
468}
469
470
471/**
472 * Terminates the kernel input device.
473 */
474static void vboxguestLinuxTermInputDevice(void)
475{
476 VbglGRFree(&g_pMouseStatusReq->header);
477 input_unregister_device(g_pInputDevice);
478 input_free_device(g_pInputDevice);
479}
480#endif
481
482
483/**
484 * Creates the device nodes.
485 *
486 * @returns 0 on success, negated errno on failure.
487 */
488static int __init vboxguestLinuxInitDeviceNodes(void)
489{
490 int rc;
491
492 /*
493 * The full feature device node.
494 */
495 rc = misc_register(&g_MiscDevice);
496 if (rc)
497 {
498 LogRel((DEVICE_NAME ": misc_register failed for %s (rc=%d)\n", DEVICE_NAME, rc));
499 return rc;
500 }
501
502 /*
503 * The device node intended to be accessible by all users.
504 */
505 rc = misc_register(&g_MiscDeviceUser);
506 if (rc)
507 {
508 LogRel((DEVICE_NAME ": misc_register failed for %s (rc=%d)\n", DEVICE_NAME_USER, rc));
509 misc_deregister(&g_MiscDevice);
510 return rc;
511 }
512
513 return 0;
514}
515
516
517/**
518 * Deregisters the device nodes.
519 */
520static void vboxguestLinuxTermDeviceNodes(void)
521{
522 misc_deregister(&g_MiscDevice);
523 misc_deregister(&g_MiscDeviceUser);
524}
525
526
527/**
528 * Initialize module.
529 *
530 * @returns appropriate status code.
531 */
532static int __init vboxguestLinuxModInit(void)
533{
534 static const char * const s_apszGroups[] = VBOX_LOGGROUP_NAMES;
535 PRTLOGGER pRelLogger;
536 int rc;
537
538 /*
539 * Initialize IPRT first.
540 */
541 rc = RTR0Init(0);
542 if (RT_FAILURE(rc))
543 {
544 printk(KERN_ERR DEVICE_NAME ": RTR0Init failed, rc=%d.\n", rc);
545 return -EINVAL;
546 }
547
548 /*
549 * Create the release log.
550 * (We do that here instead of common code because we want to log
551 * early failures using the LogRel macro.)
552 */
553 rc = RTLogCreate(&pRelLogger, 0 /* fFlags */, "all",
554 "VBOX_RELEASE_LOG", RT_ELEMENTS(s_apszGroups), s_apszGroups,
555 RTLOGDEST_STDOUT | RTLOGDEST_DEBUGGER | RTLOGDEST_USER, NULL);
556 if (RT_SUCCESS(rc))
557 {
558#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
559 RTLogGroupSettings(pRelLogger, g_szLogGrp);
560 RTLogFlags(pRelLogger, g_szLogFlags);
561 RTLogDestinations(pRelLogger, g_szLogDst);
562#endif
563 RTLogRelSetDefaultInstance(pRelLogger);
564 }
565#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
566 g_fLoggerCreated = true;
567#endif
568
569 /*
570 * Locate and initialize the PCI device.
571 */
572 rc = pci_register_driver(&g_PciDriver);
573 if (rc >= 0 && g_pPciDev)
574 {
575 /*
576 * Register the interrupt service routine for it.
577 */
578 rc = vboxguestLinuxInitISR();
579 if (rc >= 0)
580 {
581 /*
582 * Call the common device extension initializer.
583 */
584#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) && defined(RT_ARCH_X86)
585 VBOXOSTYPE enmOSType = VBOXOSTYPE_Linux26;
586#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) && defined(RT_ARCH_AMD64)
587 VBOXOSTYPE enmOSType = VBOXOSTYPE_Linux26_x64;
588#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 0) && defined(RT_ARCH_X86)
589 VBOXOSTYPE enmOSType = VBOXOSTYPE_Linux24;
590#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 0) && defined(RT_ARCH_AMD64)
591 VBOXOSTYPE enmOSType = VBOXOSTYPE_Linux24_x64;
592#else
593# warning "huh? which arch + version is this?"
594 VBOXOSTYPE enmOsType = VBOXOSTYPE_Linux;
595#endif
596 rc = VBoxGuestInitDevExt(&g_DevExt,
597 g_IOPortBase,
598 g_pvMMIOBase,
599 g_cbMMIO,
600 enmOSType,
601 VMMDEV_EVENT_MOUSE_POSITION_CHANGED);
602 if (RT_SUCCESS(rc))
603 {
604 /*
605 * Create the kernel session for this driver.
606 */
607 rc = VBoxGuestCreateKernelSession(&g_DevExt,
608 &g_pKernelSession);
609 if (RT_SUCCESS(rc))
610 {
611 /*
612 * Create the kernel input device.
613 */
614#ifdef VBOXGUEST_WITH_INPUT_DRIVER
615 rc = vboxguestLinuxCreateInputDevice();
616 if (rc >= 0)
617 {
618#endif
619 /*
620 * Finally, create the device nodes.
621 */
622 rc = vboxguestLinuxInitDeviceNodes();
623 if (rc >= 0)
624 {
625 /* some useful information for the user but don't show this on the console */
626 LogRel((DEVICE_NAME ": misc device minor %d, IRQ %d, I/O port %RTiop, MMIO at %RHp (size 0x%x)\n",
627 g_MiscDevice.minor, g_pPciDev->irq, g_IOPortBase, g_MMIOPhysAddr, g_cbMMIO));
628 printk(KERN_DEBUG DEVICE_NAME ": Successfully loaded version "
629 VBOX_VERSION_STRING " (interface " RT_XSTR(VMMDEV_VERSION) ")\n");
630 return rc;
631 }
632
633 /* bail out */
634#ifdef VBOXGUEST_WITH_INPUT_DRIVER
635 vboxguestLinuxTermInputDevice();
636 }
637 else
638 {
639 LogRel((DEVICE_NAME ": vboxguestCreateInputDevice failed with rc=%Rrc\n", rc));
640 rc = RTErrConvertFromErrno(rc);
641 }
642#endif
643 VBoxGuestCloseSession(&g_DevExt, g_pKernelSession);
644 }
645 VBoxGuestDeleteDevExt(&g_DevExt);
646 }
647 else
648 {
649 LogRel((DEVICE_NAME ": VBoxGuestInitDevExt failed with rc=%Rrc\n", rc));
650 rc = RTErrConvertFromErrno(rc);
651 }
652 vboxguestLinuxTermISR();
653 }
654 }
655 else
656 {
657 LogRel((DEVICE_NAME ": PCI device not found, probably running on physical hardware.\n"));
658 rc = -ENODEV;
659 }
660 pci_unregister_driver(&g_PciDriver);
661 RTLogDestroy(RTLogRelSetDefaultInstance(NULL));
662 RTLogDestroy(RTLogSetDefaultInstance(NULL));
663 RTR0Term();
664 return rc;
665}
666
667
668/**
669 * Unload the module.
670 */
671static void __exit vboxguestLinuxModExit(void)
672{
673 /*
674 * Inverse order of init.
675 */
676 vboxguestLinuxTermDeviceNodes();
677#ifdef VBOXGUEST_WITH_INPUT_DRIVER
678 vboxguestLinuxTermInputDevice();
679#endif
680 VBoxGuestCloseSession(&g_DevExt, g_pKernelSession);
681 VBoxGuestDeleteDevExt(&g_DevExt);
682 vboxguestLinuxTermISR();
683 pci_unregister_driver(&g_PciDriver);
684 RTLogDestroy(RTLogRelSetDefaultInstance(NULL));
685 RTLogDestroy(RTLogSetDefaultInstance(NULL));
686 RTR0Term();
687}
688
689
690/**
691 * Device open. Called on open /dev/vboxdrv
692 *
693 * @param pInode Pointer to inode info structure.
694 * @param pFilp Associated file pointer.
695 */
696static int vboxguestLinuxOpen(struct inode *pInode, struct file *pFilp)
697{
698 int rc;
699 PVBOXGUESTSESSION pSession;
700 Log((DEVICE_NAME ": pFilp=%p pid=%d/%d %s\n", pFilp, RTProcSelf(), current->pid, current->comm));
701
702 /*
703 * Call common code to create the user session. Associate it with
704 * the file so we can access it in the other methods.
705 */
706 rc = VBoxGuestCreateUserSession(&g_DevExt, &pSession);
707 if (RT_SUCCESS(rc))
708 {
709 pFilp->private_data = pSession;
710 if (MINOR(pInode->i_rdev) == g_MiscDeviceUser.minor)
711 pSession->fUserSession = true;
712 }
713
714 Log(("vboxguestLinuxOpen: g_DevExt=%p pSession=%p rc=%d/%d (pid=%d/%d %s)\n",
715 &g_DevExt, pSession, rc, vboxguestLinuxConvertToNegErrno(rc),
716 RTProcSelf(), current->pid, current->comm));
717 return vboxguestLinuxConvertToNegErrno(rc);
718}
719
720
721/**
722 * Close device.
723 *
724 * @param pInode Pointer to inode info structure.
725 * @param pFilp Associated file pointer.
726 */
727static int vboxguestLinuxRelease(struct inode *pInode, struct file *pFilp)
728{
729 Log(("vboxguestLinuxRelease: pFilp=%p pSession=%p pid=%d/%d %s\n",
730 pFilp, pFilp->private_data, RTProcSelf(), current->pid, current->comm));
731
732#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 28)
733 /* This housekeeping was needed in older kernel versions to ensure that
734 * the file pointer didn't get left on the polling queue. */
735 vboxguestFAsync(-1, pFilp, 0);
736#endif
737 VBoxGuestCloseSession(&g_DevExt, (PVBOXGUESTSESSION)pFilp->private_data);
738 pFilp->private_data = NULL;
739 return 0;
740}
741
742
743/**
744 * Device I/O Control entry point.
745 *
746 * @param pFilp Associated file pointer.
747 * @param uCmd The function specified to ioctl().
748 * @param ulArg The argument specified to ioctl().
749 */
750#ifdef HAVE_UNLOCKED_IOCTL
751static long vboxguestLinuxIOCtl(struct file *pFilp, unsigned int uCmd, unsigned long ulArg)
752#else
753static int vboxguestLinuxIOCtl(struct inode *pInode, struct file *pFilp, unsigned int uCmd, unsigned long ulArg)
754#endif
755{
756 PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)pFilp->private_data;
757 uint32_t cbData = _IOC_SIZE(uCmd);
758 void *pvBufFree;
759 void *pvBuf;
760 int rc;
761 uint64_t au64Buf[32/sizeof(uint64_t)];
762
763 Log6(("vboxguestLinuxIOCtl: pFilp=%p uCmd=%#x ulArg=%p pid=%d/%d\n", pFilp, uCmd, (void *)ulArg, RTProcSelf(), current->pid));
764
765 /*
766 * Buffer the request.
767 */
768 if (cbData <= sizeof(au64Buf))
769 {
770 pvBufFree = NULL;
771 pvBuf = &au64Buf[0];
772 }
773 else
774 {
775 pvBufFree = pvBuf = RTMemTmpAlloc(cbData);
776 if (RT_UNLIKELY(!pvBuf))
777 {
778 LogRel((DEVICE_NAME "::IOCtl: RTMemTmpAlloc failed to alloc %u bytes.\n", cbData));
779 return -ENOMEM;
780 }
781 }
782 if (RT_LIKELY(copy_from_user(pvBuf, (void *)ulArg, cbData) == 0))
783 {
784 /*
785 * Process the IOCtl.
786 */
787 size_t cbDataReturned;
788 rc = VBoxGuestCommonIOCtl(uCmd, &g_DevExt, pSession, pvBuf, cbData, &cbDataReturned);
789
790 /*
791 * Copy ioctl data and output buffer back to user space.
792 */
793 if (RT_SUCCESS(rc))
794 {
795 rc = 0;
796 if (RT_UNLIKELY(cbDataReturned > cbData))
797 {
798 LogRel((DEVICE_NAME "::IOCtl: too much output data %u expected %u\n", cbDataReturned, cbData));
799 cbDataReturned = cbData;
800 }
801 if (cbDataReturned > 0)
802 {
803 if (RT_UNLIKELY(copy_to_user((void *)ulArg, pvBuf, cbDataReturned) != 0))
804 {
805 LogRel((DEVICE_NAME "::IOCtl: copy_to_user failed; pvBuf=%p ulArg=%p cbDataReturned=%u uCmd=%d\n",
806 pvBuf, (void *)ulArg, cbDataReturned, uCmd, rc));
807 rc = -EFAULT;
808 }
809 }
810 }
811 else
812 {
813 Log(("vboxguestLinuxIOCtl: pFilp=%p uCmd=%#x ulArg=%p failed, rc=%d\n", pFilp, uCmd, (void *)ulArg, rc));
814 rc = -rc; Assert(rc > 0); /* Positive returns == negated VBox error status codes. */
815 }
816 }
817 else
818 {
819 Log((DEVICE_NAME "::IOCtl: copy_from_user(,%#lx, %#x) failed; uCmd=%#x.\n", ulArg, cbData, uCmd));
820 rc = -EFAULT;
821 }
822 if (pvBufFree)
823 RTMemFree(pvBufFree);
824
825 Log6(("vboxguestLinuxIOCtl: returns %d (pid=%d/%d)\n", rc, RTProcSelf(), current->pid));
826 return rc;
827}
828
829
830/**
831 * Asynchronous notification activation method.
832 *
833 * @returns 0 on success, negative errno on failure.
834 *
835 * @param fd The file descriptor.
836 * @param pFile The file structure.
837 * @param fOn On/off indicator.
838 */
839static int vboxguestFAsync(int fd, struct file *pFile, int fOn)
840{
841 return fasync_helper(fd, pFile, fOn, &g_pFAsyncQueue);
842}
843
844
845/**
846 * Poll function.
847 *
848 * This returns ready to read if the mouse pointer mode or the pointer position
849 * has changed since last call to read.
850 *
851 * @returns 0 if no changes, POLLIN | POLLRDNORM if there are unseen changes.
852 *
853 * @param pFile The file structure.
854 * @param pPt The poll table.
855 *
856 * @remarks This is probably not really used, X11 is said to use the fasync
857 * interface instead.
858 */
859static unsigned int vboxguestPoll(struct file *pFile, poll_table *pPt)
860{
861 PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)pFile->private_data;
862 uint32_t u32CurSeq = ASMAtomicUoReadU32(&g_DevExt.u32MousePosChangedSeq);
863 unsigned int fMask = pSession->u32MousePosChangedSeq != u32CurSeq
864 ? POLLIN | POLLRDNORM
865 : 0;
866 poll_wait(pFile, &g_PollEventQueue, pPt);
867 return fMask;
868}
869
870
871/**
872 * Read to go with our poll/fasync response.
873 *
874 * @returns 1 or -EINVAL.
875 *
876 * @param pFile The file structure.
877 * @param pbBuf The buffer to read into.
878 * @param cbRead The max number of bytes to read.
879 * @param poff The current file position.
880 *
881 * @remarks This is probably not really used as X11 lets the driver do its own
882 * event reading. The poll condition is therefore also cleared when we
883 * see VMMDevReq_GetMouseStatus in VBoxGuestCommonIOCtl_VMMRequest.
884 */
885static ssize_t vboxguestRead(struct file *pFile, char *pbBuf, size_t cbRead, loff_t *poff)
886{
887 PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)pFile->private_data;
888 uint32_t u32CurSeq = ASMAtomicUoReadU32(&g_DevExt.u32MousePosChangedSeq);
889
890 if (*poff != 0)
891 return -EINVAL;
892
893 /*
894 * Fake a single byte read if we're not up to date with the current mouse position.
895 */
896 if ( pSession->u32MousePosChangedSeq != u32CurSeq
897 && cbRead > 0)
898 {
899 pSession->u32MousePosChangedSeq = u32CurSeq;
900 pbBuf[0] = 0;
901 return 1;
902 }
903 return 0;
904}
905
906
907void VBoxGuestNativeISRMousePollEvent(PVBOXGUESTDEVEXT pDevExt)
908{
909#ifdef VBOXGUEST_WITH_INPUT_DRIVER
910 int rc;
911#endif
912 NOREF(pDevExt);
913
914 /*
915 * Wake up everyone that's in a poll() and post anyone that has
916 * subscribed to async notifications.
917 */
918 Log3(("VBoxGuestNativeISRMousePollEvent: wake_up_all\n"));
919 wake_up_all(&g_PollEventQueue);
920 Log3(("VBoxGuestNativeISRMousePollEvent: kill_fasync\n"));
921 kill_fasync(&g_pFAsyncQueue, SIGIO, POLL_IN);
922#ifdef VBOXGUEST_WITH_INPUT_DRIVER
923 /* Report events to the kernel input device */
924 g_pMouseStatusReq->mouseFeatures = 0;
925 g_pMouseStatusReq->pointerXPos = 0;
926 g_pMouseStatusReq->pointerYPos = 0;
927 rc = VbglGRPerform(&g_pMouseStatusReq->header);
928 if (RT_SUCCESS(rc))
929 {
930 input_report_abs(g_pInputDevice, ABS_X,
931 g_pMouseStatusReq->pointerXPos);
932 input_report_abs(g_pInputDevice, ABS_Y,
933 g_pMouseStatusReq->pointerYPos);
934# ifdef EV_SYN
935 input_sync(g_pInputDevice);
936# endif
937 }
938#endif
939 Log3(("VBoxGuestNativeISRMousePollEvent: done\n"));
940}
941
942
943/* Common code that depend on g_DevExt. */
944#include "VBoxGuestIDC-unix.c.h"
945
946EXPORT_SYMBOL(VBoxGuestIDCOpen);
947EXPORT_SYMBOL(VBoxGuestIDCClose);
948EXPORT_SYMBOL(VBoxGuestIDCCall);
949
950
951#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
952
953/** log and dbg_log parameter setter. */
954static int vboxguestLinuxParamLogGrpSet(const char *pszValue, struct kernel_param *pParam)
955{
956 if (g_fLoggerCreated)
957 {
958 PRTLOGGER pLogger = pParam->name[0] == 'd' ? RTLogDefaultInstance() : RTLogRelDefaultInstance();
959 if (pLogger)
960 RTLogGroupSettings(pLogger, pszValue);
961 }
962 else if (pParam->name[0] != 'd')
963 strlcpy(&g_szLogGrp[0], pszValue, sizeof(g_szLogGrp));
964
965 return 0;
966}
967
968
969/** log and dbg_log parameter getter. */
970static int vboxguestLinuxParamLogGrpGet(char *pszBuf, struct kernel_param *pParam)
971{
972 PRTLOGGER pLogger = pParam->name[0] == 'd' ? RTLogDefaultInstance() : RTLogRelDefaultInstance();
973 *pszBuf = '\0';
974 if (pLogger)
975 RTLogGetGroupSettings(pLogger, pszBuf, _4K);
976 return strlen(pszBuf);
977}
978
979
980/** log and dbg_log_flags parameter setter. */
981static int vboxguestLinuxParamLogFlagsSet(const char *pszValue, struct kernel_param *pParam)
982{
983 if (g_fLoggerCreated)
984 {
985 PRTLOGGER pLogger = pParam->name[0] == 'd' ? RTLogDefaultInstance() : RTLogRelDefaultInstance();
986 if (pLogger)
987 RTLogFlags(pLogger, pszValue);
988 }
989 else if (pParam->name[0] != 'd')
990 strlcpy(&g_szLogFlags[0], pszValue, sizeof(g_szLogFlags));
991 return 0;
992}
993
994
995/** log and dbg_log_flags parameter getter. */
996static int vboxguestLinuxParamLogFlagsGet(char *pszBuf, struct kernel_param *pParam)
997{
998 PRTLOGGER pLogger = pParam->name[0] == 'd' ? RTLogDefaultInstance() : RTLogRelDefaultInstance();
999 *pszBuf = '\0';
1000 if (pLogger)
1001 RTLogGetFlags(pLogger, pszBuf, _4K);
1002 return strlen(pszBuf);
1003}
1004
1005
1006/** log and dbg_log_dest parameter setter. */
1007static int vboxguestLinuxParamLogDstSet(const char *pszValue, struct kernel_param *pParam)
1008{
1009 if (g_fLoggerCreated)
1010 {
1011 PRTLOGGER pLogger = pParam->name[0] == 'd' ? RTLogDefaultInstance() : RTLogRelDefaultInstance();
1012 if (pLogger)
1013 RTLogDestinations(pLogger, pszValue);
1014 }
1015 else if (pParam->name[0] != 'd')
1016 strlcpy(&g_szLogDst[0], pszValue, sizeof(g_szLogDst));
1017 return 0;
1018}
1019
1020
1021/** log and dbg_log_dest parameter getter. */
1022static int vboxguestLinuxParamLogDstGet(char *pszBuf, struct kernel_param *pParam)
1023{
1024 PRTLOGGER pLogger = pParam->name[0] == 'd' ? RTLogDefaultInstance() : RTLogRelDefaultInstance();
1025 *pszBuf = '\0';
1026 if (pLogger)
1027 RTLogGetDestinations(pLogger, pszBuf, _4K);
1028 return strlen(pszBuf);
1029}
1030
1031/*
1032 * Define module parameters.
1033 */
1034module_param_call(log, vboxguestLinuxParamLogGrpSet, vboxguestLinuxParamLogGrpGet, NULL, 0664);
1035module_param_call(log_flags, vboxguestLinuxParamLogFlagsSet, vboxguestLinuxParamLogFlagsGet, NULL, 0664);
1036module_param_call(log_dest, vboxguestLinuxParamLogDstSet, vboxguestLinuxParamLogDstGet, NULL, 0664);
1037# ifdef LOG_ENABLED
1038module_param_call(dbg_log, vboxguestLinuxParamLogGrpSet, vboxguestLinuxParamLogGrpGet, NULL, 0664);
1039module_param_call(dbg_log_flags, vboxguestLinuxParamLogFlagsSet, vboxguestLinuxParamLogFlagsGet, NULL, 0664);
1040module_param_call(dbg_log_dest, vboxguestLinuxParamLogDstSet, vboxguestLinuxParamLogDstGet, NULL, 0664);
1041# endif
1042
1043#endif /* 2.6.0 and later */
1044
1045
1046module_init(vboxguestLinuxModInit);
1047module_exit(vboxguestLinuxModExit);
1048
1049MODULE_AUTHOR(VBOX_VENDOR);
1050MODULE_DESCRIPTION(VBOX_PRODUCT " Guest Additions for Linux Module");
1051MODULE_LICENSE("GPL");
1052#ifdef MODULE_VERSION
1053MODULE_VERSION(VBOX_VERSION_STRING);
1054#endif
1055
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette