VirtualBox

source: vbox/trunk/src/VBox/Main/VMMDevInterface.cpp@ 3516

Last change on this file since 3516 was 3347, checked in by vboxsync, 18 years ago

Enable shared opengl service

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.8 KB
Line 
1/** @file
2 *
3 * VirtualBox Driver Interface to VMM device
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
20 */
21
22#include "VMMDev.h"
23#include "ConsoleImpl.h"
24#include "DisplayImpl.h"
25#include "GuestImpl.h"
26
27#include "Logging.h"
28
29#include <VBox/pdm.h>
30#include <VBox/VBoxDev.h>
31#include <VBox/VBoxGuest.h>
32#include <VBox/cfgm.h>
33#include <VBox/err.h>
34#include <iprt/asm.h>
35
36#ifdef VBOX_HGCM
37#include "hgcm/HGCM.h"
38#include "hgcm/HGCMObjects.h"
39#endif
40
41//
42// defines
43//
44
45
46//
47// globals
48//
49
50
51/**
52 * VMMDev driver instance data.
53 */
54typedef struct DRVMAINVMMDEV
55{
56 /** Pointer to the VMMDev object. */
57 VMMDev *pVMMDev;
58 /** Pointer to the driver instance structure. */
59 PPDMDRVINS pDrvIns;
60 /** Pointer to the VMMDev port interface of the driver/device above us. */
61 PPDMIVMMDEVPORT pUpPort;
62 /** Our VMM device connector interface. */
63 PDMIVMMDEVCONNECTOR Connector;
64#ifdef VBOX_HGCM
65 /** Pointer to the HGCM port interface of the driver/device above us. */
66 PPDMIHGCMPORT pHGCMPort;
67 /** Our HGCM connector interface. */
68 PDMIHGCMCONNECTOR HGCMConnector;
69#endif
70} DRVMAINVMMDEV, *PDRVMAINVMMDEV;
71
72/** Converts PDMIVMMDEVCONNECTOR pointer to a DRVMAINVMMDEV pointer. */
73#define PDMIVMMDEVCONNECTOR_2_MAINVMMDEV(pInterface) ( (PDRVMAINVMMDEV) ((uintptr_t)pInterface - RT_OFFSETOF(DRVMAINVMMDEV, Connector)) )
74
75#ifdef VBOX_HGCM
76/** Converts PDMIHGCMCONNECTOR pointer to a DRVMAINVMMDEV pointer. */
77#define PDMIHGCMCONNECTOR_2_MAINVMMDEV(pInterface) ( (PDRVMAINVMMDEV) ((uintptr_t)pInterface - RT_OFFSETOF(DRVMAINVMMDEV, HGCMConnector)) )
78#endif
79
80//
81// constructor / destructor
82//
83VMMDev::VMMDev(Console *console) : mpDrv(NULL)
84{
85 mParent = console;
86 int rc = RTSemEventCreate(&mCredentialsEvent);
87 AssertRC(rc);
88#ifdef VBOX_HGCM
89 rc = HGCMHostInit ();
90 AssertRC(rc);
91#endif /* VBOX_HGCM */
92 mu32CredentialsFlags = 0;
93}
94
95VMMDev::~VMMDev()
96{
97#ifdef VBOX_HGCM
98 HGCMHostShutdown ();
99#endif /* VBOX_HGCM */
100 RTSemEventDestroy (mCredentialsEvent);
101 if (mpDrv)
102 mpDrv->pVMMDev = NULL;
103 mpDrv = NULL;
104}
105
106PPDMIVMMDEVPORT VMMDev::getVMMDevPort()
107{
108 Assert(mpDrv);
109 return mpDrv->pUpPort;
110}
111
112
113
114//
115// public methods
116//
117
118/**
119 * Wait on event semaphore for guest credential judgement result.
120 */
121int VMMDev::WaitCredentialsJudgement (uint32_t u32Timeout, uint32_t *pu32CredentialsFlags)
122{
123 if (u32Timeout == 0)
124 {
125 u32Timeout = 5000;
126 }
127
128 int rc = RTSemEventWait (mCredentialsEvent, u32Timeout);
129
130 if (VBOX_SUCCESS (rc))
131 {
132 *pu32CredentialsFlags = mu32CredentialsFlags;
133 }
134
135 return rc;
136}
137
138int VMMDev::SetCredentialsJudgementResult (uint32_t u32Flags)
139{
140 mu32CredentialsFlags = u32Flags;
141
142 int rc = RTSemEventSignal (mCredentialsEvent);
143 AssertRC(rc);
144
145 return rc;
146}
147
148
149/**
150 * Report guest OS version.
151 * Called whenever the Additions issue a guest version report request.
152 *
153 * @param pInterface Pointer to this interface.
154 * @param guestInfo Pointer to guest information structure
155 * @thread The emulation thread.
156 */
157DECLCALLBACK(void) vmmdevUpdateGuestVersion(PPDMIVMMDEVCONNECTOR pInterface, VBoxGuestInfo *guestInfo)
158{
159 PDRVMAINVMMDEV pDrv = PDMIVMMDEVCONNECTOR_2_MAINVMMDEV(pInterface);
160
161 Assert(guestInfo);
162 if (!guestInfo)
163 return;
164
165 /* store that information in IGuest */
166 Guest* guest = pDrv->pVMMDev->getParent()->getGuest();
167 Assert(guest);
168 if (!guest)
169 return;
170
171 char version[20];
172 sprintf(version, "%d", guestInfo->additionsVersion);
173 guest->setAdditionsVersion(Bstr(version));
174
175 /*
176 * Tell the console interface about the event
177 * so that it can notify its consumers.
178 */
179 pDrv->pVMMDev->getParent()->onAdditionsStateChange();
180
181 if (guestInfo->additionsVersion < VMMDEV_VERSION)
182 {
183 pDrv->pVMMDev->getParent()->onAdditionsOutdated();
184 }
185}
186
187/**
188 * Update the mouse capabilities.
189 * This is called when the mouse capabilities change. The new capabilities
190 * are given and the connector should update its internal state.
191 *
192 * @param pInterface Pointer to this interface.
193 * @param newCapabilities New capabilities.
194 * @thread The emulation thread.
195 */
196DECLCALLBACK(void) vmmdevUpdateMouseCapabilities(PPDMIVMMDEVCONNECTOR pInterface, uint32_t newCapabilities)
197{
198 PDRVMAINVMMDEV pDrv = PDMIVMMDEVCONNECTOR_2_MAINVMMDEV(pInterface);
199 /*
200 * Tell the console interface about the event
201 * so that it can notify its consumers.
202 */
203 pDrv->pVMMDev->getParent()->onMouseCapabilityChange(BOOL (newCapabilities & VMMDEV_MOUSEGUESTWANTSABS),
204 BOOL (newCapabilities & VMMDEV_MOUSEGUESTNEEDSHOSTCUR));
205}
206
207
208/**
209 * Update the pointer shape or visibility.
210 *
211 * This is called when the mouse pointer shape changes or pointer is hidden/displaying.
212 * The new shape is passed as a caller allocated buffer that will be freed after returning.
213 *
214 * @param pInterface Pointer to this interface.
215 * @param fVisible Whether the pointer is visible or not.
216 * @param fAlpha Alpha channel information is present.
217 * @param xHot Horizontal coordinate of the pointer hot spot.
218 * @param yHot Vertical coordinate of the pointer hot spot.
219 * @param width Pointer width in pixels.
220 * @param height Pointer height in pixels.
221 * @param pShape The shape buffer. If NULL, then only pointer visibility is being changed.
222 * @thread The emulation thread.
223 */
224DECLCALLBACK(void) vmmdevUpdatePointerShape(PPDMIVMMDEVCONNECTOR pInterface, bool fVisible, bool fAlpha,
225 uint32_t xHot, uint32_t yHot,
226 uint32_t width, uint32_t height,
227 void *pShape)
228{
229 PDRVMAINVMMDEV pDrv = PDMIVMMDEVCONNECTOR_2_MAINVMMDEV(pInterface);
230
231 /* tell the console about it */
232 pDrv->pVMMDev->getParent()->onMousePointerShapeChange(fVisible, fAlpha,
233 xHot, yHot, width, height, pShape);
234}
235
236DECLCALLBACK(int) iface_VideoAccelEnable(PPDMIVMMDEVCONNECTOR pInterface, bool fEnable, VBVAMEMORY *pVbvaMemory)
237{
238 PDRVMAINVMMDEV pDrv = PDMIVMMDEVCONNECTOR_2_MAINVMMDEV(pInterface);
239
240 Display *display = pDrv->pVMMDev->getParent()->getDisplay();
241
242 if (display)
243 {
244 LogFlow(("MAIN::VMMDevInterface::iface_VideoAccelEnable: %d, %p\n", fEnable, pVbvaMemory));
245 return display->VideoAccelEnable (fEnable, pVbvaMemory);
246 }
247
248 return VERR_NOT_SUPPORTED;
249}
250DECLCALLBACK(void) iface_VideoAccelFlush(PPDMIVMMDEVCONNECTOR pInterface)
251{
252 PDRVMAINVMMDEV pDrv = PDMIVMMDEVCONNECTOR_2_MAINVMMDEV(pInterface);
253
254 Display *display = pDrv->pVMMDev->getParent()->getDisplay();
255
256 if (display)
257 {
258 LogFlow(("MAIN::VMMDevInterface::iface_VideoAccelFlush\n"));
259 display->VideoAccelFlush ();
260 }
261}
262
263DECLCALLBACK(int) vmmdevVideoModeSupported(PPDMIVMMDEVCONNECTOR pInterface, uint32_t width, uint32_t height,
264 uint32_t bpp, bool *fSupported)
265{
266 PDRVMAINVMMDEV pDrv = PDMIVMMDEVCONNECTOR_2_MAINVMMDEV(pInterface);
267
268 if (!fSupported)
269 return VERR_INVALID_PARAMETER;
270 IFramebuffer *framebuffer = pDrv->pVMMDev->getParent()->getDisplay()->getFramebuffer();
271 if (framebuffer)
272 framebuffer->VideoModeSupported(width, height, bpp, (BOOL*)fSupported);
273 else
274 *fSupported = true;
275 return VINF_SUCCESS;
276}
277
278DECLCALLBACK(int) vmmdevGetHeightReduction(PPDMIVMMDEVCONNECTOR pInterface, uint32_t *heightReduction)
279{
280 PDRVMAINVMMDEV pDrv = PDMIVMMDEVCONNECTOR_2_MAINVMMDEV(pInterface);
281
282 if (!heightReduction)
283 return VERR_INVALID_PARAMETER;
284 IFramebuffer *framebuffer = pDrv->pVMMDev->getParent()->getDisplay()->getFramebuffer();
285 if (framebuffer)
286 framebuffer->COMGETTER(HeightReduction)((ULONG*)heightReduction);
287 else
288 *heightReduction = 0;
289 return VINF_SUCCESS;
290}
291
292DECLCALLBACK(int) vmmdevSetCredentialsJudgementResult(PPDMIVMMDEVCONNECTOR pInterface, uint32_t u32Flags)
293{
294 PDRVMAINVMMDEV pDrv = PDMIVMMDEVCONNECTOR_2_MAINVMMDEV(pInterface);
295
296 int rc = pDrv->pVMMDev->SetCredentialsJudgementResult (u32Flags);
297
298 return rc;
299}
300
301#ifdef VBOX_HGCM
302
303/* HGCM connector interface */
304
305static DECLCALLBACK(int) iface_hgcmConnect (PPDMIHGCMCONNECTOR pInterface, PVBOXHGCMCMD pCmd, PHGCMSERVICELOCATION pServiceLocation, uint32_t *pu32ClientID)
306{
307 LogFlowFunc(("Enter\n"));
308
309 PDRVMAINVMMDEV pDrv = PDMIHGCMCONNECTOR_2_MAINVMMDEV(pInterface);
310
311 if ( !pServiceLocation
312 || ( pServiceLocation->type != VMMDevHGCMLoc_LocalHost
313 && pServiceLocation->type != VMMDevHGCMLoc_LocalHost_Existing))
314 {
315 return VERR_INVALID_PARAMETER;
316 }
317
318 return HGCMGuestConnect (pDrv->pHGCMPort, pCmd, pServiceLocation->u.host.achName, pu32ClientID);
319}
320
321static DECLCALLBACK(int) iface_hgcmDisconnect (PPDMIHGCMCONNECTOR pInterface, PVBOXHGCMCMD pCmd, uint32_t u32ClientID)
322{
323 LogFlowFunc(("Enter\n"));
324
325 PDRVMAINVMMDEV pDrv = PDMIHGCMCONNECTOR_2_MAINVMMDEV(pInterface);
326
327 return HGCMGuestDisconnect (pDrv->pHGCMPort, pCmd, u32ClientID);
328}
329
330static DECLCALLBACK(int) iface_hgcmCall (PPDMIHGCMCONNECTOR pInterface, PVBOXHGCMCMD pCmd, uint32_t u32ClientID, uint32_t u32Function,
331 uint32_t cParms, PVBOXHGCMSVCPARM paParms)
332{
333 LogFlowFunc(("Enter\n"));
334
335 PDRVMAINVMMDEV pDrv = PDMIHGCMCONNECTOR_2_MAINVMMDEV(pInterface);
336
337 return HGCMGuestCall (pDrv->pHGCMPort, pCmd, u32ClientID, u32Function, cParms, paParms);
338}
339
340/**
341 * Execute state save operation.
342 *
343 * @returns VBox status code.
344 * @param pDrvIns Driver instance of the driver which registered the data unit.
345 * @param pSSM SSM operation handle.
346 */
347static DECLCALLBACK(int) iface_hgcmSave(PPDMDRVINS pDrvIns, PSSMHANDLE pSSM)
348{
349 LogFlowFunc(("Enter\n"));
350 return HGCMHostSaveState (pSSM);
351}
352
353
354/**
355 * Execute state load operation.
356 *
357 * @returns VBox status code.
358 * @param pDrvIns Driver instance of the driver which registered the data unit.
359 * @param pSSM SSM operation handle.
360 * @param u32Version Data layout version.
361 */
362static DECLCALLBACK(int) iface_hgcmLoad(PPDMDRVINS pDrvIns, PSSMHANDLE pSSM, uint32_t u32Version)
363{
364 LogFlowFunc(("Enter\n"));
365
366 if (u32Version != HGCM_SSM_VERSION)
367 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
368
369 return HGCMHostLoadState (pSSM);
370}
371
372int VMMDev::hgcmLoadService (const char *pszServiceName, const char *pszServiceLibrary)
373{
374 return HGCMHostLoad (pszServiceName, pszServiceLibrary);
375}
376
377int VMMDev::hgcmHostCall (const char *pszServiceName, uint32_t u32Function,
378 uint32_t cParms, PVBOXHGCMSVCPARM paParms)
379{
380 return HGCMHostCall (pszServiceName, u32Function, cParms, paParms);
381}
382#endif /* HGCM */
383
384
385/**
386 * Queries an interface to the driver.
387 *
388 * @returns Pointer to interface.
389 * @returns NULL if the interface was not supported by the driver.
390 * @param pInterface Pointer to this interface structure.
391 * @param enmInterface The requested interface identification.
392 */
393DECLCALLBACK(void *) VMMDev::drvQueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface)
394{
395 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
396 PDRVMAINVMMDEV pDrv = PDMINS2DATA(pDrvIns, PDRVMAINVMMDEV);
397 switch (enmInterface)
398 {
399 case PDMINTERFACE_BASE:
400 return &pDrvIns->IBase;
401 case PDMINTERFACE_VMMDEV_CONNECTOR:
402 return &pDrv->Connector;
403#ifdef VBOX_HGCM
404 case PDMINTERFACE_HGCM_CONNECTOR:
405 return &pDrv->HGCMConnector;
406#endif
407 default:
408 return NULL;
409 }
410}
411
412
413/**
414 * Destruct a VMMDev driver instance.
415 *
416 * @returns VBox status.
417 * @param pDrvIns The driver instance data.
418 */
419DECLCALLBACK(void) VMMDev::drvDestruct(PPDMDRVINS pDrvIns)
420{
421 PDRVMAINVMMDEV pData = PDMINS2DATA(pDrvIns, PDRVMAINVMMDEV);
422 LogFlow(("VMMDev::drvDestruct: iInstance=%d\n", pDrvIns->iInstance));
423#ifdef VBOX_HGCM
424 /* HGCM is shut down on the VMMDev destructor. */
425#endif /* VBOX_HGCM */
426 if (pData->pVMMDev)
427 {
428 pData->pVMMDev->mpDrv = NULL;
429 }
430}
431
432/**
433 * Reset notification.
434 *
435 * @returns VBox status.
436 * @param pDrvIns The driver instance data.
437 */
438DECLCALLBACK(void) VMMDev::drvReset(PPDMDRVINS pDrvIns)
439{
440 LogFlow(("VMMDev::drvReset: iInstance=%d\n", pDrvIns->iInstance));
441#ifdef VBOX_HGCM
442 HGCMHostReset ();
443#endif /* VBOX_HGCM */
444}
445
446/**
447 * Construct a VMMDev driver instance.
448 *
449 * @returns VBox status.
450 * @param pDrvIns The driver instance data.
451 * If the registration structure is needed, pDrvIns->pDrvReg points to it.
452 * @param pCfgHandle Configuration node handle for the driver. Use this to obtain the configuration
453 * of the driver instance. It's also found in pDrvIns->pCfgHandle, but like
454 * iInstance it's expected to be used a bit in this function.
455 */
456DECLCALLBACK(int) VMMDev::drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle)
457{
458 PDRVMAINVMMDEV pData = PDMINS2DATA(pDrvIns, PDRVMAINVMMDEV);
459 LogFlow(("Keyboard::drvConstruct: iInstance=%d\n", pDrvIns->iInstance));
460
461 /*
462 * Validate configuration.
463 */
464 if (!CFGMR3AreValuesValid(pCfgHandle, "Object\0"))
465 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
466 PPDMIBASE pBaseIgnore;
467 int rc = pDrvIns->pDrvHlp->pfnAttach(pDrvIns, &pBaseIgnore);
468 if (rc != VERR_PDM_NO_ATTACHED_DRIVER)
469 {
470 AssertMsgFailed(("Configuration error: Not possible to attach anything to this driver!\n"));
471 return VERR_PDM_DRVINS_NO_ATTACH;
472 }
473
474 /*
475 * IBase.
476 */
477 pDrvIns->IBase.pfnQueryInterface = VMMDev::drvQueryInterface;
478
479 pData->Connector.pfnUpdateGuestVersion = vmmdevUpdateGuestVersion;
480 pData->Connector.pfnUpdateMouseCapabilities = vmmdevUpdateMouseCapabilities;
481 pData->Connector.pfnUpdatePointerShape = vmmdevUpdatePointerShape;
482 pData->Connector.pfnVideoAccelEnable = iface_VideoAccelEnable;
483 pData->Connector.pfnVideoAccelFlush = iface_VideoAccelFlush;
484 pData->Connector.pfnVideoModeSupported = vmmdevVideoModeSupported;
485 pData->Connector.pfnGetHeightReduction = vmmdevGetHeightReduction;
486 pData->Connector.pfnSetCredentialsJudgementResult = vmmdevSetCredentialsJudgementResult;
487
488#ifdef VBOX_HGCM
489 pData->HGCMConnector.pfnConnect = iface_hgcmConnect;
490 pData->HGCMConnector.pfnDisconnect = iface_hgcmDisconnect;
491 pData->HGCMConnector.pfnCall = iface_hgcmCall;
492#endif
493
494 /*
495 * Get the IVMMDevPort interface of the above driver/device.
496 */
497 pData->pUpPort = (PPDMIVMMDEVPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_VMMDEV_PORT);
498 if (!pData->pUpPort)
499 {
500 AssertMsgFailed(("Configuration error: No VMMDev port interface above!\n"));
501 return VERR_PDM_MISSING_INTERFACE_ABOVE;
502 }
503
504#ifdef VBOX_HGCM
505 pData->pHGCMPort = (PPDMIHGCMPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_HGCM_PORT);
506 if (!pData->pHGCMPort)
507 {
508 AssertMsgFailed(("Configuration error: No HGCM port interface above!\n"));
509 return VERR_PDM_MISSING_INTERFACE_ABOVE;
510 }
511#endif
512
513 /*
514 * Get the Console object pointer and update the mpDrv member.
515 */
516 void *pv;
517 rc = CFGMR3QueryPtr(pCfgHandle, "Object", &pv);
518 if (VBOX_FAILURE(rc))
519 {
520 AssertMsgFailed(("Configuration error: No/bad \"Object\" value! rc=%Vrc\n", rc));
521 return rc;
522 }
523
524 pData->pVMMDev = (VMMDev*)pv; /** @todo Check this cast! */
525 pData->pVMMDev->mpDrv = pData;
526
527#ifdef VBOX_HGCM
528 rc = pData->pVMMDev->hgcmLoadService ("VBoxSharedFolders", "VBoxSharedFolders");
529 pData->pVMMDev->fSharedFolderActive = VBOX_SUCCESS(rc);
530 if (VBOX_SUCCESS(rc))
531 {
532 LogRel(("Shared Folders service loaded.\n"));
533 }
534 else
535 {
536 LogRel(("Failed to load Shared Folders service %Vrc\n", rc));
537 }
538 rc = pData->pVMMDev->hgcmLoadService ("VBoxSharedOpenGL", "VBoxSharedOpenGL");
539 if (VBOX_SUCCESS(rc))
540 {
541 LogRel(("Shared OpenGL service loaded.\n"));
542 }
543 else
544 {
545 LogRel(("Failed to load Shared OpenGL service %Vrc\n", rc));
546 }
547
548 pDrvIns->pDrvHlp->pfnSSMRegister(pDrvIns, "HGCM", 0, HGCM_SSM_VERSION, 4096/* bad guess */, NULL, iface_hgcmSave, NULL, NULL, iface_hgcmLoad, NULL);
549#endif /* VBOX_HGCM */
550
551 return VINF_SUCCESS;
552}
553
554
555/**
556 * VMMDevice driver registration record.
557 */
558const PDMDRVREG VMMDev::DrvReg =
559{
560 /* u32Version */
561 PDM_DRVREG_VERSION,
562 /* szDriverName */
563 "MainVMMDev",
564 /* pszDescription */
565 "Main VMMDev driver (Main as in the API).",
566 /* fFlags */
567 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
568 /* fClass. */
569 PDM_DRVREG_CLASS_VMMDEV,
570 /* cMaxInstances */
571 ~0,
572 /* cbInstance */
573 sizeof(DRVMAINVMMDEV),
574 /* pfnConstruct */
575 VMMDev::drvConstruct,
576 /* pfnDestruct */
577 VMMDev::drvDestruct,
578 /* pfnIOCtl */
579 NULL,
580 /* pfnPowerOn */
581 NULL,
582 /* pfnReset */
583 VMMDev::drvReset,
584 /* pfnSuspend */
585 NULL,
586 /* pfnResume */
587 NULL,
588 /* pfnDetach */
589 NULL
590};
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