VirtualBox

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

Last change on this file since 2511 was 1711, checked in by vboxsync, 18 years ago

HGCM cleanup.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.5 KB
Line 
1/** @file
2 *
3 * VirtualBox Driver Interface to VMM device
4 */
5
6/*
7 * Copyright (C) 2006 InnoTek Systemberatung 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 Assert(framebuffer);
272 framebuffer->VideoModeSupported(width, height, bpp, (BOOL*)fSupported);
273 return VINF_SUCCESS;
274}
275
276DECLCALLBACK(int) vmmdevGetHeightReduction(PPDMIVMMDEVCONNECTOR pInterface, uint32_t *heightReduction)
277{
278 PDRVMAINVMMDEV pDrv = PDMIVMMDEVCONNECTOR_2_MAINVMMDEV(pInterface);
279
280 if (!heightReduction)
281 return VERR_INVALID_PARAMETER;
282 IFramebuffer *framebuffer = pDrv->pVMMDev->getParent()->getDisplay()->getFramebuffer();
283 Assert(framebuffer);
284 framebuffer->COMGETTER(HeightReduction)((ULONG*)heightReduction);
285 return VINF_SUCCESS;
286}
287
288DECLCALLBACK(int) vmmdevSetCredentialsJudgementResult(PPDMIVMMDEVCONNECTOR pInterface, uint32_t u32Flags)
289{
290 PDRVMAINVMMDEV pDrv = PDMIVMMDEVCONNECTOR_2_MAINVMMDEV(pInterface);
291
292 int rc = pDrv->pVMMDev->SetCredentialsJudgementResult (u32Flags);
293
294 return rc;
295}
296
297#ifdef VBOX_HGCM
298
299/* HGCM connector interface */
300
301static DECLCALLBACK(int) iface_hgcmConnect (PPDMIHGCMCONNECTOR pInterface, PVBOXHGCMCMD pCmd, PHGCMSERVICELOCATION pServiceLocation, uint32_t *pu32ClientID)
302{
303 LogFlowFunc(("Enter\n"));
304
305 PDRVMAINVMMDEV pDrv = PDMIHGCMCONNECTOR_2_MAINVMMDEV(pInterface);
306
307 if ( !pServiceLocation
308 || ( pServiceLocation->type != VMMDevHGCMLoc_LocalHost
309 && pServiceLocation->type != VMMDevHGCMLoc_LocalHost_Existing))
310 {
311 return VERR_INVALID_PARAMETER;
312 }
313
314 return HGCMGuestConnect (pDrv->pHGCMPort, pCmd, pServiceLocation->u.host.achName, pu32ClientID);
315}
316
317static DECLCALLBACK(int) iface_hgcmDisconnect (PPDMIHGCMCONNECTOR pInterface, PVBOXHGCMCMD pCmd, uint32_t u32ClientID)
318{
319 LogFlowFunc(("Enter\n"));
320
321 PDRVMAINVMMDEV pDrv = PDMIHGCMCONNECTOR_2_MAINVMMDEV(pInterface);
322
323 return HGCMGuestDisconnect (pDrv->pHGCMPort, pCmd, u32ClientID);
324}
325
326static DECLCALLBACK(int) iface_hgcmCall (PPDMIHGCMCONNECTOR pInterface, PVBOXHGCMCMD pCmd, uint32_t u32ClientID, uint32_t u32Function,
327 uint32_t cParms, PVBOXHGCMSVCPARM paParms)
328{
329 LogFlowFunc(("Enter\n"));
330
331 PDRVMAINVMMDEV pDrv = PDMIHGCMCONNECTOR_2_MAINVMMDEV(pInterface);
332
333 return HGCMGuestCall (pDrv->pHGCMPort, pCmd, u32ClientID, u32Function, cParms, paParms);
334}
335
336/**
337 * Execute state save operation.
338 *
339 * @returns VBox status code.
340 * @param pDrvIns Driver instance of the driver which registered the data unit.
341 * @param pSSM SSM operation handle.
342 */
343static DECLCALLBACK(int) iface_hgcmSave(PPDMDRVINS pDrvIns, PSSMHANDLE pSSM)
344{
345 LogFlowFunc(("Enter\n"));
346 return HGCMHostSaveState (pSSM);
347}
348
349
350/**
351 * Execute state load operation.
352 *
353 * @returns VBox status code.
354 * @param pDrvIns Driver instance of the driver which registered the data unit.
355 * @param pSSM SSM operation handle.
356 * @param u32Version Data layout version.
357 */
358static DECLCALLBACK(int) iface_hgcmLoad(PPDMDRVINS pDrvIns, PSSMHANDLE pSSM, uint32_t u32Version)
359{
360 LogFlowFunc(("Enter\n"));
361
362 if (u32Version != HGCM_SSM_VERSION)
363 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
364
365 return HGCMHostLoadState (pSSM);
366}
367
368int VMMDev::hgcmLoadService (const char *pszServiceName, const char *pszServiceLibrary)
369{
370 return HGCMHostLoad (pszServiceName, pszServiceLibrary);
371}
372
373int VMMDev::hgcmHostCall (const char *pszServiceName, uint32_t u32Function,
374 uint32_t cParms, PVBOXHGCMSVCPARM paParms)
375{
376 return HGCMHostCall (pszServiceName, u32Function, cParms, paParms);
377}
378#endif /* HGCM */
379
380
381/**
382 * Queries an interface to the driver.
383 *
384 * @returns Pointer to interface.
385 * @returns NULL if the interface was not supported by the driver.
386 * @param pInterface Pointer to this interface structure.
387 * @param enmInterface The requested interface identification.
388 */
389DECLCALLBACK(void *) VMMDev::drvQueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface)
390{
391 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
392 PDRVMAINVMMDEV pDrv = PDMINS2DATA(pDrvIns, PDRVMAINVMMDEV);
393 switch (enmInterface)
394 {
395 case PDMINTERFACE_BASE:
396 return &pDrvIns->IBase;
397 case PDMINTERFACE_VMMDEV_CONNECTOR:
398 return &pDrv->Connector;
399#ifdef VBOX_HGCM
400 case PDMINTERFACE_HGCM_CONNECTOR:
401 return &pDrv->HGCMConnector;
402#endif
403 default:
404 return NULL;
405 }
406}
407
408
409/**
410 * Destruct a VMMDev driver instance.
411 *
412 * @returns VBox status.
413 * @param pDrvIns The driver instance data.
414 */
415DECLCALLBACK(void) VMMDev::drvDestruct(PPDMDRVINS pDrvIns)
416{
417 PDRVMAINVMMDEV pData = PDMINS2DATA(pDrvIns, PDRVMAINVMMDEV);
418 LogFlow(("VMMDev::drvDestruct: iInstance=%d\n", pDrvIns->iInstance));
419#ifdef VBOX_HGCM
420 /* HGCM is shut down on the VMMDev destructor. */
421#endif /* VBOX_HGCM */
422 if (pData->pVMMDev)
423 {
424 pData->pVMMDev->mpDrv = NULL;
425 }
426}
427
428/**
429 * Reset notification.
430 *
431 * @returns VBox status.
432 * @param pDrvIns The driver instance data.
433 */
434DECLCALLBACK(void) VMMDev::drvReset(PPDMDRVINS pDrvIns)
435{
436 LogFlow(("VMMDev::drvReset: iInstance=%d\n", pDrvIns->iInstance));
437#ifdef VBOX_HGCM
438 HGCMHostReset ();
439#endif /* VBOX_HGCM */
440}
441
442/**
443 * Construct a VMMDev driver instance.
444 *
445 * @returns VBox status.
446 * @param pDrvIns The driver instance data.
447 * If the registration structure is needed, pDrvIns->pDrvReg points to it.
448 * @param pCfgHandle Configuration node handle for the driver. Use this to obtain the configuration
449 * of the driver instance. It's also found in pDrvIns->pCfgHandle, but like
450 * iInstance it's expected to be used a bit in this function.
451 */
452DECLCALLBACK(int) VMMDev::drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle)
453{
454 PDRVMAINVMMDEV pData = PDMINS2DATA(pDrvIns, PDRVMAINVMMDEV);
455 LogFlow(("Keyboard::drvConstruct: iInstance=%d\n", pDrvIns->iInstance));
456
457 /*
458 * Validate configuration.
459 */
460 if (!CFGMR3AreValuesValid(pCfgHandle, "Object\0"))
461 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
462 PPDMIBASE pBaseIgnore;
463 int rc = pDrvIns->pDrvHlp->pfnAttach(pDrvIns, &pBaseIgnore);
464 if (rc != VERR_PDM_NO_ATTACHED_DRIVER)
465 {
466 AssertMsgFailed(("Configuration error: Not possible to attach anything to this driver!\n"));
467 return VERR_PDM_DRVINS_NO_ATTACH;
468 }
469
470 /*
471 * IBase.
472 */
473 pDrvIns->IBase.pfnQueryInterface = VMMDev::drvQueryInterface;
474
475 pData->Connector.pfnUpdateGuestVersion = vmmdevUpdateGuestVersion;
476 pData->Connector.pfnUpdateMouseCapabilities = vmmdevUpdateMouseCapabilities;
477 pData->Connector.pfnUpdatePointerShape = vmmdevUpdatePointerShape;
478 pData->Connector.pfnVideoAccelEnable = iface_VideoAccelEnable;
479 pData->Connector.pfnVideoAccelFlush = iface_VideoAccelFlush;
480 pData->Connector.pfnVideoModeSupported = vmmdevVideoModeSupported;
481 pData->Connector.pfnGetHeightReduction = vmmdevGetHeightReduction;
482 pData->Connector.pfnSetCredentialsJudgementResult = vmmdevSetCredentialsJudgementResult;
483
484#ifdef VBOX_HGCM
485 pData->HGCMConnector.pfnConnect = iface_hgcmConnect;
486 pData->HGCMConnector.pfnDisconnect = iface_hgcmDisconnect;
487 pData->HGCMConnector.pfnCall = iface_hgcmCall;
488#endif
489
490 /*
491 * Get the IVMMDevPort interface of the above driver/device.
492 */
493 pData->pUpPort = (PPDMIVMMDEVPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_VMMDEV_PORT);
494 if (!pData->pUpPort)
495 {
496 AssertMsgFailed(("Configuration error: No VMMDev port interface above!\n"));
497 return VERR_PDM_MISSING_INTERFACE_ABOVE;
498 }
499
500#ifdef VBOX_HGCM
501 pData->pHGCMPort = (PPDMIHGCMPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_HGCM_PORT);
502 if (!pData->pHGCMPort)
503 {
504 AssertMsgFailed(("Configuration error: No HGCM port interface above!\n"));
505 return VERR_PDM_MISSING_INTERFACE_ABOVE;
506 }
507#endif
508
509 /*
510 * Get the Console object pointer and update the mpDrv member.
511 */
512 void *pv;
513 rc = CFGMR3QueryPtr(pCfgHandle, "Object", &pv);
514 if (VBOX_FAILURE(rc))
515 {
516 AssertMsgFailed(("Configuration error: No/bad \"Object\" value! rc=%Vrc\n", rc));
517 return rc;
518 }
519
520 pData->pVMMDev = (VMMDev*)pv; /** @todo Check this cast! */
521 pData->pVMMDev->mpDrv = pData;
522
523#ifdef VBOX_HGCM
524 rc = pData->pVMMDev->hgcmLoadService ("VBoxSharedFolders", "VBoxSharedFolders");
525 pData->pVMMDev->fSharedFolderActive = VBOX_SUCCESS(rc);
526 if (VBOX_SUCCESS(rc))
527 {
528 LogRel(("Shared Folders service loaded.\n"));
529 }
530 else
531 {
532 LogRel(("Failed to load Shared Folders service %Vrc\n", rc));
533 }
534 pDrvIns->pDrvHlp->pfnSSMRegister(pDrvIns, "HGCM", 0, HGCM_SSM_VERSION, 4096/* bad guess */, NULL, iface_hgcmSave, NULL, NULL, iface_hgcmLoad, NULL);
535#endif /* VBOX_HGCM */
536
537 return VINF_SUCCESS;
538}
539
540
541/**
542 * VMMDevice driver registration record.
543 */
544const PDMDRVREG VMMDev::DrvReg =
545{
546 /* u32Version */
547 PDM_DRVREG_VERSION,
548 /* szDriverName */
549 "MainVMMDev",
550 /* pszDescription */
551 "Main VMMDev driver (Main as in the API).",
552 /* fFlags */
553 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
554 /* fClass. */
555 PDM_DRVREG_CLASS_VMMDEV,
556 /* cMaxInstances */
557 ~0,
558 /* cbInstance */
559 sizeof(DRVMAINVMMDEV),
560 /* pfnConstruct */
561 VMMDev::drvConstruct,
562 /* pfnDestruct */
563 VMMDev::drvDestruct,
564 /* pfnIOCtl */
565 NULL,
566 /* pfnPowerOn */
567 NULL,
568 /* pfnReset */
569 VMMDev::drvReset,
570 /* pfnSuspend */
571 NULL,
572 /* pfnResume */
573 NULL,
574 /* pfnDetach */
575 NULL
576};
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