VirtualBox

source: vbox/trunk/src/VBox/Devices/VMMDev/VMMDev.cpp@ 73641

Last change on this file since 73641 was 73641, checked in by vboxsync, 7 years ago

VMMDev: fGuestSentChangeEventAck flag in multimonitor resize request should be true if event is acknowledged, bugref:8444

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 168.2 KB
Line 
1/* $Id: VMMDev.cpp 73641 2018-08-13 19:13:59Z vboxsync $ */
2/** @file
3 * VMMDev - Guest <-> VMM/Host communication device.
4 */
5
6/*
7 * Copyright (C) 2006-2017 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18/** @page pg_vmmdev The VMM Device.
19 *
20 * The VMM device is a custom hardware device emulation for communicating with
21 * the guest additions.
22 *
23 * Whenever host wants to inform guest about something an IRQ notification will
24 * be raised.
25 *
26 * VMMDev PDM interface will contain the guest notification method.
27 *
28 * There is a 32 bit event mask which will be read by guest on an interrupt. A
29 * non zero bit in the mask means that the specific event occurred and requires
30 * processing on guest side.
31 *
32 * After reading the event mask guest must issue a generic request
33 * AcknowlegdeEvents.
34 *
35 * IRQ line is set to 1 (request) if there are unprocessed events, that is the
36 * event mask is not zero.
37 *
38 * After receiving an interrupt and checking event mask, the guest must process
39 * events using the event specific mechanism.
40 *
41 * That is if mouse capabilities were changed, guest will use
42 * VMMDev_GetMouseStatus generic request.
43 *
44 * Event mask is only a set of flags indicating that guest must proceed with a
45 * procedure.
46 *
47 * Unsupported events are therefore ignored. The guest additions must inform
48 * host which events they want to receive, to avoid unnecessary IRQ processing.
49 * By default no events are signalled to guest.
50 *
51 * This seems to be fast method. It requires only one context switch for an
52 * event processing.
53 *
54 *
55 * @section sec_vmmdev_heartbeat Heartbeat
56 *
57 * The heartbeat is a feature to monitor whether the guest OS is hung or not.
58 *
59 * The main kernel component of the guest additions, VBoxGuest, sets up a timer
60 * at a frequency returned by VMMDevReq_HeartbeatConfigure
61 * (VMMDevReqHeartbeat::cNsInterval, VMMDEV::cNsHeartbeatInterval) and performs
62 * a VMMDevReq_GuestHeartbeat request every time the timer ticks.
63 *
64 * The host side (VMMDev) arms a timer with a more distant deadline
65 * (VMMDEV::cNsHeartbeatTimeout), twice cNsHeartbeatInterval by default. Each
66 * time a VMMDevReq_GuestHeartbeat request comes in, the timer is rearmed with
67 * the same relative deadline. So, as long as VMMDevReq_GuestHeartbeat comes
68 * when they should, the host timer will never fire.
69 *
70 * When the timer fires, we consider the guest as hung / flatlined / dead.
71 * Currently we only LogRel that, but it's easy to extend this with an event in
72 * Main API.
73 *
74 * Should the guest reawaken at some later point, we LogRel that event and
75 * continue as normal. Again something which would merit an API event.
76 *
77 */
78
79
80/*********************************************************************************************************************************
81* Header Files *
82*********************************************************************************************************************************/
83/* Enable dev_vmm Log3 statements to get IRQ-related logging. */
84#define LOG_GROUP LOG_GROUP_DEV_VMM
85#include <VBox/AssertGuest.h>
86#include <VBox/VMMDev.h>
87#include <VBox/vmm/dbgf.h>
88#include <VBox/vmm/mm.h>
89#include <VBox/log.h>
90#include <VBox/param.h>
91#include <iprt/path.h>
92#include <iprt/dir.h>
93#include <iprt/file.h>
94#include <VBox/vmm/pgm.h>
95#include <VBox/err.h>
96#include <VBox/vmm/vm.h> /* for VM_IS_EMT */
97#include <VBox/dbg.h>
98#include <VBox/version.h>
99
100#include <iprt/asm.h>
101#include <iprt/asm-amd64-x86.h>
102#include <iprt/assert.h>
103#include <iprt/buildconfig.h>
104#include <iprt/string.h>
105#include <iprt/time.h>
106#ifndef IN_RC
107# include <iprt/mem.h>
108#endif
109#ifdef IN_RING3
110# include <iprt/uuid.h>
111#endif
112
113#include "VMMDevState.h"
114#ifdef VBOX_WITH_HGCM
115# include "VMMDevHGCM.h"
116#endif
117#ifndef VBOX_WITHOUT_TESTING_FEATURES
118# include "VMMDevTesting.h"
119#endif
120
121
122/*********************************************************************************************************************************
123* Defined Constants And Macros *
124*********************************************************************************************************************************/
125#define VMMDEV_INTERFACE_VERSION_IS_1_03(s) \
126 ( RT_HIWORD((s)->guestInfo.interfaceVersion) == 1 \
127 && RT_LOWORD((s)->guestInfo.interfaceVersion) == 3 )
128
129#define VMMDEV_INTERFACE_VERSION_IS_OK(additionsVersion) \
130 ( RT_HIWORD(additionsVersion) == RT_HIWORD(VMMDEV_VERSION) \
131 && RT_LOWORD(additionsVersion) <= RT_LOWORD(VMMDEV_VERSION) )
132
133#define VMMDEV_INTERFACE_VERSION_IS_OLD(additionsVersion) \
134 ( (RT_HIWORD(additionsVersion) < RT_HIWORD(VMMDEV_VERSION) \
135 || ( RT_HIWORD(additionsVersion) == RT_HIWORD(VMMDEV_VERSION) \
136 && RT_LOWORD(additionsVersion) <= RT_LOWORD(VMMDEV_VERSION) ) )
137
138#define VMMDEV_INTERFACE_VERSION_IS_TOO_OLD(additionsVersion) \
139 ( RT_HIWORD(additionsVersion) < RT_HIWORD(VMMDEV_VERSION) )
140
141#define VMMDEV_INTERFACE_VERSION_IS_NEW(additionsVersion) \
142 ( RT_HIWORD(additionsVersion) > RT_HIWORD(VMMDEV_VERSION) \
143 || ( RT_HIWORD(additionsVersion) == RT_HIWORD(VMMDEV_VERSION) \
144 && RT_LOWORD(additionsVersion) > RT_LOWORD(VMMDEV_VERSION) ) )
145
146/** Default interval in nanoseconds between guest heartbeats.
147 * Used when no HeartbeatInterval is set in CFGM and for setting
148 * HB check timer if the guest's heartbeat frequency is less than 1Hz. */
149#define VMMDEV_HEARTBEAT_DEFAULT_INTERVAL (2U*RT_NS_1SEC_64)
150
151
152#ifndef VBOX_DEVICE_STRUCT_TESTCASE
153
154/* -=-=-=-=- Misc Helpers -=-=-=-=- */
155
156/**
157 * Log information about the Guest Additions.
158 *
159 * @param pGuestInfo The information we've got from the Guest Additions driver.
160 */
161static void vmmdevLogGuestOsInfo(VBoxGuestInfo *pGuestInfo)
162{
163 const char *pszOs;
164 switch (pGuestInfo->osType & ~VBOXOSTYPE_x64)
165 {
166 case VBOXOSTYPE_DOS: pszOs = "DOS"; break;
167 case VBOXOSTYPE_Win31: pszOs = "Windows 3.1"; break;
168 case VBOXOSTYPE_Win9x: pszOs = "Windows 9x"; break;
169 case VBOXOSTYPE_Win95: pszOs = "Windows 95"; break;
170 case VBOXOSTYPE_Win98: pszOs = "Windows 98"; break;
171 case VBOXOSTYPE_WinMe: pszOs = "Windows Me"; break;
172 case VBOXOSTYPE_WinNT: pszOs = "Windows NT"; break;
173 case VBOXOSTYPE_WinNT3x: pszOs = "Windows NT 3.x"; break;
174 case VBOXOSTYPE_WinNT4: pszOs = "Windows NT4"; break;
175 case VBOXOSTYPE_Win2k: pszOs = "Windows 2k"; break;
176 case VBOXOSTYPE_WinXP: pszOs = "Windows XP"; break;
177 case VBOXOSTYPE_Win2k3: pszOs = "Windows 2k3"; break;
178 case VBOXOSTYPE_WinVista: pszOs = "Windows Vista"; break;
179 case VBOXOSTYPE_Win2k8: pszOs = "Windows 2k8"; break;
180 case VBOXOSTYPE_Win7: pszOs = "Windows 7"; break;
181 case VBOXOSTYPE_Win8: pszOs = "Windows 8"; break;
182 case VBOXOSTYPE_Win2k12_x64 & ~VBOXOSTYPE_x64: pszOs = "Windows 2k12"; break;
183 case VBOXOSTYPE_Win81: pszOs = "Windows 8.1"; break;
184 case VBOXOSTYPE_Win10: pszOs = "Windows 10"; break;
185 case VBOXOSTYPE_Win2k16_x64 & ~VBOXOSTYPE_x64: pszOs = "Windows 2k16"; break;
186 case VBOXOSTYPE_OS2: pszOs = "OS/2"; break;
187 case VBOXOSTYPE_OS2Warp3: pszOs = "OS/2 Warp 3"; break;
188 case VBOXOSTYPE_OS2Warp4: pszOs = "OS/2 Warp 4"; break;
189 case VBOXOSTYPE_OS2Warp45: pszOs = "OS/2 Warp 4.5"; break;
190 case VBOXOSTYPE_ECS: pszOs = "OS/2 ECS"; break;
191 case VBOXOSTYPE_OS21x: pszOs = "OS/2 2.1x"; break;
192 case VBOXOSTYPE_Linux: pszOs = "Linux"; break;
193 case VBOXOSTYPE_Linux22: pszOs = "Linux 2.2"; break;
194 case VBOXOSTYPE_Linux24: pszOs = "Linux 2.4"; break;
195 case VBOXOSTYPE_Linux26: pszOs = "Linux >= 2.6"; break;
196 case VBOXOSTYPE_ArchLinux: pszOs = "ArchLinux"; break;
197 case VBOXOSTYPE_Debian: pszOs = "Debian"; break;
198 case VBOXOSTYPE_OpenSUSE: pszOs = "openSUSE"; break;
199 case VBOXOSTYPE_FedoraCore: pszOs = "Fedora"; break;
200 case VBOXOSTYPE_Gentoo: pszOs = "Gentoo"; break;
201 case VBOXOSTYPE_Mandriva: pszOs = "Mandriva"; break;
202 case VBOXOSTYPE_RedHat: pszOs = "RedHat"; break;
203 case VBOXOSTYPE_Turbolinux: pszOs = "TurboLinux"; break;
204 case VBOXOSTYPE_Ubuntu: pszOs = "Ubuntu"; break;
205 case VBOXOSTYPE_Xandros: pszOs = "Xandros"; break;
206 case VBOXOSTYPE_Oracle: pszOs = "Oracle Linux"; break;
207 case VBOXOSTYPE_FreeBSD: pszOs = "FreeBSD"; break;
208 case VBOXOSTYPE_OpenBSD: pszOs = "OpenBSD"; break;
209 case VBOXOSTYPE_NetBSD: pszOs = "NetBSD"; break;
210 case VBOXOSTYPE_Netware: pszOs = "Netware"; break;
211 case VBOXOSTYPE_Solaris: pszOs = "Solaris"; break;
212 case VBOXOSTYPE_OpenSolaris: pszOs = "OpenSolaris"; break;
213 case VBOXOSTYPE_Solaris11_x64 & ~VBOXOSTYPE_x64: pszOs = "Solaris 11"; break;
214 case VBOXOSTYPE_MacOS: pszOs = "Mac OS X"; break;
215 case VBOXOSTYPE_MacOS106: pszOs = "Mac OS X 10.6"; break;
216 case VBOXOSTYPE_MacOS107_x64 & ~VBOXOSTYPE_x64: pszOs = "Mac OS X 10.7"; break;
217 case VBOXOSTYPE_MacOS108_x64 & ~VBOXOSTYPE_x64: pszOs = "Mac OS X 10.8"; break;
218 case VBOXOSTYPE_MacOS109_x64 & ~VBOXOSTYPE_x64: pszOs = "Mac OS X 10.9"; break;
219 case VBOXOSTYPE_MacOS1010_x64 & ~VBOXOSTYPE_x64: pszOs = "Mac OS X 10.10"; break;
220 case VBOXOSTYPE_MacOS1011_x64 & ~VBOXOSTYPE_x64: pszOs = "Mac OS X 10.11"; break;
221 case VBOXOSTYPE_MacOS1012_x64 & ~VBOXOSTYPE_x64: pszOs = "macOS 10.12"; break;
222 case VBOXOSTYPE_MacOS1013_x64 & ~VBOXOSTYPE_x64: pszOs = "macOS 10.13"; break;
223 case VBOXOSTYPE_Haiku: pszOs = "Haiku"; break;
224 default: pszOs = "unknown"; break;
225 }
226 LogRel(("VMMDev: Guest Additions information report: Interface = 0x%08X osType = 0x%08X (%s, %u-bit)\n",
227 pGuestInfo->interfaceVersion, pGuestInfo->osType, pszOs,
228 pGuestInfo->osType & VBOXOSTYPE_x64 ? 64 : 32));
229}
230
231/**
232 * Sets the IRQ (raise it or lower it) for 1.03 additions.
233 *
234 * @param pThis The VMMDev state.
235 * @thread Any.
236 * @remarks Must be called owning the critical section.
237 */
238static void vmmdevSetIRQ_Legacy(PVMMDEV pThis)
239{
240 if (pThis->fu32AdditionsOk)
241 {
242 /* Filter unsupported events */
243 uint32_t fEvents = pThis->u32HostEventFlags & pThis->pVMMDevRAMR3->V.V1_03.u32GuestEventMask;
244
245 Log(("vmmdevSetIRQ: fEvents=%#010x, u32HostEventFlags=%#010x, u32GuestEventMask=%#010x.\n",
246 fEvents, pThis->u32HostEventFlags, pThis->pVMMDevRAMR3->V.V1_03.u32GuestEventMask));
247
248 /* Move event flags to VMMDev RAM */
249 pThis->pVMMDevRAMR3->V.V1_03.u32HostEvents = fEvents;
250
251 uint32_t uIRQLevel = 0;
252 if (fEvents)
253 {
254 /* Clear host flags which will be delivered to guest. */
255 pThis->u32HostEventFlags &= ~fEvents;
256 Log(("vmmdevSetIRQ: u32HostEventFlags=%#010x\n", pThis->u32HostEventFlags));
257 uIRQLevel = 1;
258 }
259
260 /* Set IRQ level for pin 0 (see NoWait comment in vmmdevMaybeSetIRQ). */
261 /** @todo make IRQ pin configurable, at least a symbolic constant */
262 PDMDevHlpPCISetIrqNoWait(pThis->pDevIns, 0, uIRQLevel);
263 Log(("vmmdevSetIRQ: IRQ set %d\n", uIRQLevel));
264 }
265 else
266 Log(("vmmdevSetIRQ: IRQ is not generated, guest has not yet reported to us.\n"));
267}
268
269/**
270 * Sets the IRQ if there are events to be delivered.
271 *
272 * @param pThis The VMMDev state.
273 * @thread Any.
274 * @remarks Must be called owning the critical section.
275 */
276static void vmmdevMaybeSetIRQ(PVMMDEV pThis)
277{
278 Log3(("vmmdevMaybeSetIRQ: u32HostEventFlags=%#010x, u32GuestFilterMask=%#010x.\n",
279 pThis->u32HostEventFlags, pThis->u32GuestFilterMask));
280
281 if (pThis->u32HostEventFlags & pThis->u32GuestFilterMask)
282 {
283 /*
284 * Note! No need to wait for the IRQs to be set (if we're not luck
285 * with the locks, etc). It is a notification about something,
286 * which has already happened.
287 */
288 pThis->pVMMDevRAMR3->V.V1_04.fHaveEvents = true;
289 PDMDevHlpPCISetIrqNoWait(pThis->pDevIns, 0, 1);
290 Log3(("vmmdevMaybeSetIRQ: IRQ set.\n"));
291 }
292}
293
294/**
295 * Notifies the guest about new events (@a fAddEvents).
296 *
297 * @param pThis The VMMDev state.
298 * @param fAddEvents New events to add.
299 * @thread Any.
300 * @remarks Must be called owning the critical section.
301 */
302static void vmmdevNotifyGuestWorker(PVMMDEV pThis, uint32_t fAddEvents)
303{
304 Log3(("vmmdevNotifyGuestWorker: fAddEvents=%#010x.\n", fAddEvents));
305 Assert(PDMCritSectIsOwner(&pThis->CritSect));
306
307 if (!VMMDEV_INTERFACE_VERSION_IS_1_03(pThis))
308 {
309 Log3(("vmmdevNotifyGuestWorker: New additions detected.\n"));
310
311 if (pThis->fu32AdditionsOk)
312 {
313 const bool fHadEvents = (pThis->u32HostEventFlags & pThis->u32GuestFilterMask) != 0;
314
315 Log3(("vmmdevNotifyGuestWorker: fHadEvents=%d, u32HostEventFlags=%#010x, u32GuestFilterMask=%#010x.\n",
316 fHadEvents, pThis->u32HostEventFlags, pThis->u32GuestFilterMask));
317
318 pThis->u32HostEventFlags |= fAddEvents;
319
320 if (!fHadEvents)
321 vmmdevMaybeSetIRQ(pThis);
322 }
323 else
324 {
325 pThis->u32HostEventFlags |= fAddEvents;
326 Log(("vmmdevNotifyGuestWorker: IRQ is not generated, guest has not yet reported to us.\n"));
327 }
328 }
329 else
330 {
331 Log3(("vmmdevNotifyGuestWorker: Old additions detected.\n"));
332
333 pThis->u32HostEventFlags |= fAddEvents;
334 vmmdevSetIRQ_Legacy(pThis);
335 }
336}
337
338
339
340/* -=-=-=-=- Interfaces shared with VMMDevHGCM.cpp -=-=-=-=- */
341
342/**
343 * Notifies the guest about new events (@a fAddEvents).
344 *
345 * This is used by VMMDev.cpp as well as VMMDevHGCM.cpp.
346 *
347 * @param pThis The VMMDev state.
348 * @param fAddEvents New events to add.
349 * @thread Any.
350 */
351void VMMDevNotifyGuest(PVMMDEV pThis, uint32_t fAddEvents)
352{
353 Log3(("VMMDevNotifyGuest: fAddEvents=%#010x\n", fAddEvents));
354
355 /*
356 * Only notify the VM when it's running.
357 */
358 VMSTATE enmVMState = PDMDevHlpVMState(pThis->pDevIns);
359/** @todo r=bird: Shouldn't there be more states here? Wouldn't we drop
360 * notifications now when we're in the process of suspending or
361 * similar? */
362 if ( enmVMState == VMSTATE_RUNNING
363 || enmVMState == VMSTATE_RUNNING_LS)
364 {
365 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
366 vmmdevNotifyGuestWorker(pThis, fAddEvents);
367 PDMCritSectLeave(&pThis->CritSect);
368 }
369}
370
371/**
372 * Code shared by VMMDevReq_CtlGuestFilterMask and HGCM for controlling the
373 * events the guest are interested in.
374 *
375 * @param pThis The VMMDev state.
376 * @param fOrMask Events to add (VMMDEV_EVENT_XXX). Pass 0 for no
377 * change.
378 * @param fNotMask Events to remove (VMMDEV_EVENT_XXX). Pass 0 for no
379 * change.
380 *
381 * @remarks When HGCM will automatically enable VMMDEV_EVENT_HGCM when the guest
382 * starts submitting HGCM requests. Otherwise, the events are
383 * controlled by the guest.
384 */
385void VMMDevCtlSetGuestFilterMask(PVMMDEV pThis, uint32_t fOrMask, uint32_t fNotMask)
386{
387 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
388
389 const bool fHadEvents = (pThis->u32HostEventFlags & pThis->u32GuestFilterMask) != 0;
390
391 Log(("VMMDevCtlSetGuestFilterMask: fOrMask=%#010x, u32NotMask=%#010x, fHadEvents=%d.\n", fOrMask, fNotMask, fHadEvents));
392 if (fHadEvents)
393 {
394 if (!pThis->fNewGuestFilterMask)
395 pThis->u32NewGuestFilterMask = pThis->u32GuestFilterMask;
396
397 pThis->u32NewGuestFilterMask |= fOrMask;
398 pThis->u32NewGuestFilterMask &= ~fNotMask;
399 pThis->fNewGuestFilterMask = true;
400 }
401 else
402 {
403 pThis->u32GuestFilterMask |= fOrMask;
404 pThis->u32GuestFilterMask &= ~fNotMask;
405 vmmdevMaybeSetIRQ(pThis);
406 }
407
408 PDMCritSectLeave(&pThis->CritSect);
409}
410
411
412
413/* -=-=-=-=- Request processing functions. -=-=-=-=- */
414
415/**
416 * Handles VMMDevReq_ReportGuestInfo.
417 *
418 * @returns VBox status code that the guest should see.
419 * @param pThis The VMMDev instance data.
420 * @param pRequestHeader The header of the request to handle.
421 */
422static int vmmdevReqHandler_ReportGuestInfo(PVMMDEV pThis, VMMDevRequestHeader *pRequestHeader)
423{
424 AssertMsgReturn(pRequestHeader->size == sizeof(VMMDevReportGuestInfo), ("%u\n", pRequestHeader->size), VERR_INVALID_PARAMETER);
425 VBoxGuestInfo const *pInfo = &((VMMDevReportGuestInfo *)pRequestHeader)->guestInfo;
426
427 if (memcmp(&pThis->guestInfo, pInfo, sizeof(*pInfo)) != 0)
428 {
429 /* Make a copy of supplied information. */
430 pThis->guestInfo = *pInfo;
431
432 /* Check additions interface version. */
433 pThis->fu32AdditionsOk = VMMDEV_INTERFACE_VERSION_IS_OK(pThis->guestInfo.interfaceVersion);
434
435 vmmdevLogGuestOsInfo(&pThis->guestInfo);
436
437 if (pThis->pDrv && pThis->pDrv->pfnUpdateGuestInfo)
438 pThis->pDrv->pfnUpdateGuestInfo(pThis->pDrv, &pThis->guestInfo);
439 }
440
441 if (!pThis->fu32AdditionsOk)
442 return VERR_VERSION_MISMATCH;
443
444 /* Clear our IRQ in case it was high for whatever reason. */
445 PDMDevHlpPCISetIrqNoWait(pThis->pDevIns, 0, 0);
446
447 return VINF_SUCCESS;
448}
449
450
451/**
452 * Handles VMMDevReq_GuestHeartbeat.
453 *
454 * @returns VBox status code that the guest should see.
455 * @param pThis The VMMDev instance data.
456 */
457static int vmmDevReqHandler_GuestHeartbeat(PVMMDEV pThis)
458{
459 int rc;
460 if (pThis->fHeartbeatActive)
461 {
462 uint64_t const nsNowTS = TMTimerGetNano(pThis->pFlatlinedTimer);
463 if (!pThis->fFlatlined)
464 { /* likely */ }
465 else
466 {
467 LogRel(("VMMDev: GuestHeartBeat: Guest is alive (gone %'llu ns)\n", nsNowTS - pThis->nsLastHeartbeatTS));
468 ASMAtomicWriteBool(&pThis->fFlatlined, false);
469 }
470 ASMAtomicWriteU64(&pThis->nsLastHeartbeatTS, nsNowTS);
471
472 /* Postpone (or restart if we missed a beat) the timeout timer. */
473 rc = TMTimerSetNano(pThis->pFlatlinedTimer, pThis->cNsHeartbeatTimeout);
474 }
475 else
476 rc = VINF_SUCCESS;
477 return rc;
478}
479
480
481/**
482 * Timer that fires when where have been no heartbeats for a given time.
483 *
484 * @remarks Does not take the VMMDev critsect.
485 */
486static DECLCALLBACK(void) vmmDevHeartbeatFlatlinedTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
487{
488 RT_NOREF1(pDevIns);
489 PVMMDEV pThis = (PVMMDEV)pvUser;
490 if (pThis->fHeartbeatActive)
491 {
492 uint64_t cNsElapsed = TMTimerGetNano(pTimer) - pThis->nsLastHeartbeatTS;
493 if ( !pThis->fFlatlined
494 && cNsElapsed >= pThis->cNsHeartbeatInterval)
495 {
496 LogRel(("VMMDev: vmmDevHeartbeatFlatlinedTimer: Guest seems to be unresponsive. Last heartbeat received %RU64 seconds ago\n",
497 cNsElapsed / RT_NS_1SEC));
498 ASMAtomicWriteBool(&pThis->fFlatlined, true);
499 }
500 }
501}
502
503
504/**
505 * Handles VMMDevReq_HeartbeatConfigure.
506 *
507 * @returns VBox status code that the guest should see.
508 * @param pThis The VMMDev instance data.
509 * @param pReqHdr The header of the request to handle.
510 */
511static int vmmDevReqHandler_HeartbeatConfigure(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
512{
513 AssertMsgReturn(pReqHdr->size == sizeof(VMMDevReqHeartbeat), ("%u\n", pReqHdr->size), VERR_INVALID_PARAMETER);
514 VMMDevReqHeartbeat *pReq = (VMMDevReqHeartbeat *)pReqHdr;
515 int rc;
516
517 pReq->cNsInterval = pThis->cNsHeartbeatInterval;
518
519 if (pReq->fEnabled != pThis->fHeartbeatActive)
520 {
521 ASMAtomicWriteBool(&pThis->fHeartbeatActive, pReq->fEnabled);
522 if (pReq->fEnabled)
523 {
524 /*
525 * Activate the heartbeat monitor.
526 */
527 pThis->nsLastHeartbeatTS = TMTimerGetNano(pThis->pFlatlinedTimer);
528 rc = TMTimerSetNano(pThis->pFlatlinedTimer, pThis->cNsHeartbeatTimeout);
529 if (RT_SUCCESS(rc))
530 LogRel(("VMMDev: Heartbeat flatline timer set to trigger after %'RU64 ns\n", pThis->cNsHeartbeatTimeout));
531 else
532 LogRel(("VMMDev: Error starting flatline timer (heartbeat): %Rrc\n", rc));
533 }
534 else
535 {
536 /*
537 * Deactivate the heartbeat monitor.
538 */
539 rc = TMTimerStop(pThis->pFlatlinedTimer);
540 LogRel(("VMMDev: Heartbeat checking timer has been stopped (rc=%Rrc)\n", rc));
541 }
542 }
543 else
544 {
545 LogRel(("VMMDev: vmmDevReqHandler_HeartbeatConfigure: No change (fHeartbeatActive=%RTbool)\n", pThis->fHeartbeatActive));
546 rc = VINF_SUCCESS;
547 }
548
549 return rc;
550}
551
552
553/**
554 * Handles VMMDevReq_NtBugCheck.
555 *
556 * @returns VBox status code that the guest should see.
557 * @param pThis The VMMDev instance data.
558 * @param pReqHdr The header of the request to handle.
559 */
560static int vmmDevReqHandler_NtBugCheck(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
561{
562 if (pReqHdr->size == sizeof(VMMDevReqNtBugCheck))
563 {
564 VMMDevReqNtBugCheck const *pReq = (VMMDevReqNtBugCheck const *)pReqHdr;
565 DBGFR3ReportBugCheck(PDMDevHlpGetVM(pThis->pDevIns), PDMDevHlpGetVMCPU(pThis->pDevIns), DBGFEVENT_BSOD_VMMDEV,
566 pReq->uBugCheck, pReq->auParameters[0], pReq->auParameters[1],
567 pReq->auParameters[2], pReq->auParameters[3]);
568 }
569 else if (pReqHdr->size == sizeof(VMMDevRequestHeader))
570 {
571 LogRel(("VMMDev: NT BugCheck w/o data.\n"));
572 DBGFR3ReportBugCheck(PDMDevHlpGetVM(pThis->pDevIns), PDMDevHlpGetVMCPU(pThis->pDevIns), DBGFEVENT_BSOD_VMMDEV,
573 0, 0, 0, 0, 0);
574 }
575 else
576 return VERR_INVALID_PARAMETER;
577 return VINF_SUCCESS;
578}
579
580
581/**
582 * Validates a publisher tag.
583 *
584 * @returns true / false.
585 * @param pszTag Tag to validate.
586 */
587static bool vmmdevReqIsValidPublisherTag(const char *pszTag)
588{
589 /* Note! This character set is also found in Config.kmk. */
590 static char const s_szValidChars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz()[]{}+-.,";
591
592 while (*pszTag != '\0')
593 {
594 if (!strchr(s_szValidChars, *pszTag))
595 return false;
596 pszTag++;
597 }
598 return true;
599}
600
601
602/**
603 * Validates a build tag.
604 *
605 * @returns true / false.
606 * @param pszTag Tag to validate.
607 */
608static bool vmmdevReqIsValidBuildTag(const char *pszTag)
609{
610 int cchPrefix;
611 if (!strncmp(pszTag, "RC", 2))
612 cchPrefix = 2;
613 else if (!strncmp(pszTag, "BETA", 4))
614 cchPrefix = 4;
615 else if (!strncmp(pszTag, "ALPHA", 5))
616 cchPrefix = 5;
617 else
618 return false;
619
620 if (pszTag[cchPrefix] == '\0')
621 return true;
622
623 uint8_t u8;
624 int rc = RTStrToUInt8Full(&pszTag[cchPrefix], 10, &u8);
625 return rc == VINF_SUCCESS;
626}
627
628
629/**
630 * Handles VMMDevReq_ReportGuestInfo2.
631 *
632 * @returns VBox status code that the guest should see.
633 * @param pThis The VMMDev instance data.
634 * @param pReqHdr The header of the request to handle.
635 */
636static int vmmdevReqHandler_ReportGuestInfo2(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
637{
638 AssertMsgReturn(pReqHdr->size == sizeof(VMMDevReportGuestInfo2), ("%u\n", pReqHdr->size), VERR_INVALID_PARAMETER);
639 VBoxGuestInfo2 const *pInfo2 = &((VMMDevReportGuestInfo2 *)pReqHdr)->guestInfo;
640
641 LogRel(("VMMDev: Guest Additions information report: Version %d.%d.%d r%d '%.*s'\n",
642 pInfo2->additionsMajor, pInfo2->additionsMinor, pInfo2->additionsBuild,
643 pInfo2->additionsRevision, sizeof(pInfo2->szName), pInfo2->szName));
644
645 /* The interface was introduced in 3.2 and will definitely not be
646 backported beyond 3.0 (bird). */
647 AssertMsgReturn(pInfo2->additionsMajor >= 3,
648 ("%u.%u.%u\n", pInfo2->additionsMajor, pInfo2->additionsMinor, pInfo2->additionsBuild),
649 VERR_INVALID_PARAMETER);
650
651 /* The version must fit in a full version compression. */
652 uint32_t uFullVersion = VBOX_FULL_VERSION_MAKE(pInfo2->additionsMajor, pInfo2->additionsMinor, pInfo2->additionsBuild);
653 AssertMsgReturn( VBOX_FULL_VERSION_GET_MAJOR(uFullVersion) == pInfo2->additionsMajor
654 && VBOX_FULL_VERSION_GET_MINOR(uFullVersion) == pInfo2->additionsMinor
655 && VBOX_FULL_VERSION_GET_BUILD(uFullVersion) == pInfo2->additionsBuild,
656 ("%u.%u.%u\n", pInfo2->additionsMajor, pInfo2->additionsMinor, pInfo2->additionsBuild),
657 VERR_OUT_OF_RANGE);
658
659 /*
660 * Validate the name.
661 * Be less strict towards older additions (< v4.1.50).
662 */
663 AssertCompile(sizeof(pThis->guestInfo2.szName) == sizeof(pInfo2->szName));
664 AssertReturn(RTStrEnd(pInfo2->szName, sizeof(pInfo2->szName)) != NULL, VERR_INVALID_PARAMETER);
665 const char *pszName = pInfo2->szName;
666
667 /* The version number which shouldn't be there. */
668 char szTmp[sizeof(pInfo2->szName)];
669 size_t cchStart = RTStrPrintf(szTmp, sizeof(szTmp), "%u.%u.%u", pInfo2->additionsMajor, pInfo2->additionsMinor, pInfo2->additionsBuild);
670 AssertMsgReturn(!strncmp(pszName, szTmp, cchStart), ("%s != %s\n", pszName, szTmp), VERR_INVALID_PARAMETER);
671 pszName += cchStart;
672
673 /* Now we can either have nothing or a build tag or/and a publisher tag. */
674 if (*pszName != '\0')
675 {
676 const char *pszRelaxedName = "";
677 bool const fStrict = pInfo2->additionsMajor > 4
678 || (pInfo2->additionsMajor == 4 && pInfo2->additionsMinor > 1)
679 || (pInfo2->additionsMajor == 4 && pInfo2->additionsMinor == 1 && pInfo2->additionsBuild >= 50);
680 bool fOk = false;
681 if (*pszName == '_')
682 {
683 pszName++;
684 strcpy(szTmp, pszName);
685 char *pszTag2 = strchr(szTmp, '_');
686 if (!pszTag2)
687 {
688 fOk = vmmdevReqIsValidBuildTag(szTmp)
689 || vmmdevReqIsValidPublisherTag(szTmp);
690 }
691 else
692 {
693 *pszTag2++ = '\0';
694 fOk = vmmdevReqIsValidBuildTag(szTmp);
695 if (fOk)
696 {
697 fOk = vmmdevReqIsValidPublisherTag(pszTag2);
698 if (!fOk)
699 pszRelaxedName = szTmp;
700 }
701 }
702 }
703
704 if (!fOk)
705 {
706 AssertLogRelMsgReturn(!fStrict, ("%s", pszName), VERR_INVALID_PARAMETER);
707
708 /* non-strict mode, just zap the extra stuff. */
709 LogRel(("VMMDev: ReportGuestInfo2: Ignoring unparsable version name bits: '%s' -> '%s'.\n", pszName, pszRelaxedName));
710 pszName = pszRelaxedName;
711 }
712 }
713
714 /*
715 * Save the info and tell Main or whoever is listening.
716 */
717 pThis->guestInfo2.uFullVersion = uFullVersion;
718 pThis->guestInfo2.uRevision = pInfo2->additionsRevision;
719 pThis->guestInfo2.fFeatures = pInfo2->additionsFeatures;
720 strcpy(pThis->guestInfo2.szName, pszName);
721
722 if (pThis->pDrv && pThis->pDrv->pfnUpdateGuestInfo2)
723 pThis->pDrv->pfnUpdateGuestInfo2(pThis->pDrv, uFullVersion, pszName, pInfo2->additionsRevision, pInfo2->additionsFeatures);
724
725 /* Clear our IRQ in case it was high for whatever reason. */
726 PDMDevHlpPCISetIrqNoWait(pThis->pDevIns, 0, 0);
727
728 return VINF_SUCCESS;
729}
730
731
732/**
733 * Allocates a new facility status entry, initializing it to inactive.
734 *
735 * @returns Pointer to a facility status entry on success, NULL on failure
736 * (table full).
737 * @param pThis The VMMDev instance data.
738 * @param enmFacility The facility type code.
739 * @param fFixed This is set when allocating the standard entries
740 * from the constructor.
741 * @param pTimeSpecNow Optionally giving the entry timestamp to use (ctor).
742 */
743static PVMMDEVFACILITYSTATUSENTRY
744vmmdevAllocFacilityStatusEntry(PVMMDEV pThis, VBoxGuestFacilityType enmFacility, bool fFixed, PCRTTIMESPEC pTimeSpecNow)
745{
746 /* If full, expunge one inactive entry. */
747 if (pThis->cFacilityStatuses == RT_ELEMENTS(pThis->aFacilityStatuses))
748 {
749 uint32_t i = pThis->cFacilityStatuses;
750 while (i-- > 0)
751 {
752 if ( pThis->aFacilityStatuses[i].enmStatus == VBoxGuestFacilityStatus_Inactive
753 && !pThis->aFacilityStatuses[i].fFixed)
754 {
755 pThis->cFacilityStatuses--;
756 int cToMove = pThis->cFacilityStatuses - i;
757 if (cToMove)
758 memmove(&pThis->aFacilityStatuses[i], &pThis->aFacilityStatuses[i + 1],
759 cToMove * sizeof(pThis->aFacilityStatuses[i]));
760 RT_ZERO(pThis->aFacilityStatuses[pThis->cFacilityStatuses]);
761 break;
762 }
763 }
764
765 if (pThis->cFacilityStatuses == RT_ELEMENTS(pThis->aFacilityStatuses))
766 return NULL;
767 }
768
769 /* Find location in array (it's sorted). */
770 uint32_t i = pThis->cFacilityStatuses;
771 while (i-- > 0)
772 if ((uint32_t)pThis->aFacilityStatuses[i].enmFacility < (uint32_t)enmFacility)
773 break;
774 i++;
775
776 /* Move. */
777 int cToMove = pThis->cFacilityStatuses - i;
778 if (cToMove > 0)
779 memmove(&pThis->aFacilityStatuses[i + 1], &pThis->aFacilityStatuses[i],
780 cToMove * sizeof(pThis->aFacilityStatuses[i]));
781 pThis->cFacilityStatuses++;
782
783 /* Initialize. */
784 pThis->aFacilityStatuses[i].enmFacility = enmFacility;
785 pThis->aFacilityStatuses[i].enmStatus = VBoxGuestFacilityStatus_Inactive;
786 pThis->aFacilityStatuses[i].fFixed = fFixed;
787 pThis->aFacilityStatuses[i].afPadding[0] = 0;
788 pThis->aFacilityStatuses[i].afPadding[1] = 0;
789 pThis->aFacilityStatuses[i].afPadding[2] = 0;
790 pThis->aFacilityStatuses[i].fFlags = 0;
791 if (pTimeSpecNow)
792 pThis->aFacilityStatuses[i].TimeSpecTS = *pTimeSpecNow;
793 else
794 RTTimeSpecSetNano(&pThis->aFacilityStatuses[i].TimeSpecTS, 0);
795
796 return &pThis->aFacilityStatuses[i];
797}
798
799
800/**
801 * Gets a facility status entry, allocating a new one if not already present.
802 *
803 * @returns Pointer to a facility status entry on success, NULL on failure
804 * (table full).
805 * @param pThis The VMMDev instance data.
806 * @param enmFacility The facility type code.
807 */
808static PVMMDEVFACILITYSTATUSENTRY vmmdevGetFacilityStatusEntry(PVMMDEV pThis, VBoxGuestFacilityType enmFacility)
809{
810 /** @todo change to binary search. */
811 uint32_t i = pThis->cFacilityStatuses;
812 while (i-- > 0)
813 {
814 if (pThis->aFacilityStatuses[i].enmFacility == enmFacility)
815 return &pThis->aFacilityStatuses[i];
816 if ((uint32_t)pThis->aFacilityStatuses[i].enmFacility < (uint32_t)enmFacility)
817 break;
818 }
819 return vmmdevAllocFacilityStatusEntry(pThis, enmFacility, false /*fFixed*/, NULL);
820}
821
822
823/**
824 * Handles VMMDevReq_ReportGuestStatus.
825 *
826 * @returns VBox status code that the guest should see.
827 * @param pThis The VMMDev instance data.
828 * @param pReqHdr The header of the request to handle.
829 */
830static int vmmdevReqHandler_ReportGuestStatus(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
831{
832 /*
833 * Validate input.
834 */
835 AssertMsgReturn(pReqHdr->size == sizeof(VMMDevReportGuestStatus), ("%u\n", pReqHdr->size), VERR_INVALID_PARAMETER);
836 VBoxGuestStatus *pStatus = &((VMMDevReportGuestStatus *)pReqHdr)->guestStatus;
837 AssertMsgReturn( pStatus->facility > VBoxGuestFacilityType_Unknown
838 && pStatus->facility <= VBoxGuestFacilityType_All,
839 ("%d\n", pStatus->facility),
840 VERR_INVALID_PARAMETER);
841 AssertMsgReturn(pStatus->status == (VBoxGuestFacilityStatus)(uint16_t)pStatus->status,
842 ("%#x (%u)\n", pStatus->status, pStatus->status),
843 VERR_OUT_OF_RANGE);
844
845 /*
846 * Do the update.
847 */
848 RTTIMESPEC Now;
849 RTTimeNow(&Now);
850 if (pStatus->facility == VBoxGuestFacilityType_All)
851 {
852 uint32_t i = pThis->cFacilityStatuses;
853 while (i-- > 0)
854 {
855 pThis->aFacilityStatuses[i].TimeSpecTS = Now;
856 pThis->aFacilityStatuses[i].enmStatus = pStatus->status;
857 pThis->aFacilityStatuses[i].fFlags = pStatus->flags;
858 }
859 }
860 else
861 {
862 PVMMDEVFACILITYSTATUSENTRY pEntry = vmmdevGetFacilityStatusEntry(pThis, pStatus->facility);
863 if (!pEntry)
864 {
865 LogRelMax(10, ("VMMDev: Facility table is full - facility=%u status=%u\n", pStatus->facility, pStatus->status));
866 return VERR_OUT_OF_RESOURCES;
867 }
868
869 pEntry->TimeSpecTS = Now;
870 pEntry->enmStatus = pStatus->status;
871 pEntry->fFlags = pStatus->flags;
872 }
873
874 if (pThis->pDrv && pThis->pDrv->pfnUpdateGuestStatus)
875 pThis->pDrv->pfnUpdateGuestStatus(pThis->pDrv, pStatus->facility, pStatus->status, pStatus->flags, &Now);
876
877 return VINF_SUCCESS;
878}
879
880
881/**
882 * Handles VMMDevReq_ReportGuestUserState.
883 *
884 * @returns VBox status code that the guest should see.
885 * @param pThis The VMMDev instance data.
886 * @param pReqHdr The header of the request to handle.
887 */
888static int vmmdevReqHandler_ReportGuestUserState(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
889{
890 /*
891 * Validate input.
892 */
893 VMMDevReportGuestUserState *pReq = (VMMDevReportGuestUserState *)pReqHdr;
894 AssertMsgReturn(pReq->header.size >= sizeof(*pReq), ("%u\n", pReqHdr->size), VERR_INVALID_PARAMETER);
895
896 if ( pThis->pDrv
897 && pThis->pDrv->pfnUpdateGuestUserState)
898 {
899 /* Play safe. */
900 AssertReturn(pReq->header.size <= _2K, VERR_TOO_MUCH_DATA);
901 AssertReturn(pReq->status.cbUser <= 256, VERR_TOO_MUCH_DATA);
902 AssertReturn(pReq->status.cbDomain <= 256, VERR_TOO_MUCH_DATA);
903 AssertReturn(pReq->status.cbDetails <= _1K, VERR_TOO_MUCH_DATA);
904
905 /* pbDynamic marks the beginning of the struct's dynamically
906 * allocated data area. */
907 uint8_t *pbDynamic = (uint8_t *)&pReq->status.szUser;
908 uint32_t cbLeft = pReqHdr->size - RT_UOFFSETOF(VMMDevReportGuestUserState, status.szUser);
909
910 /* The user. */
911 AssertReturn(pReq->status.cbUser > 0, VERR_INVALID_PARAMETER); /* User name is required. */
912 AssertReturn(pReq->status.cbUser <= cbLeft, VERR_INVALID_PARAMETER);
913 const char *pszUser = (const char *)pbDynamic;
914 AssertReturn(RTStrEnd(pszUser, pReq->status.cbUser), VERR_INVALID_PARAMETER);
915 int rc = RTStrValidateEncoding(pszUser);
916 AssertRCReturn(rc, rc);
917
918 /* Advance to the next field. */
919 pbDynamic += pReq->status.cbUser;
920 cbLeft -= pReq->status.cbUser;
921
922 /* pszDomain can be NULL. */
923 AssertReturn(pReq->status.cbDomain <= cbLeft, VERR_INVALID_PARAMETER);
924 const char *pszDomain = NULL;
925 if (pReq->status.cbDomain)
926 {
927 pszDomain = (const char *)pbDynamic;
928 AssertReturn(RTStrEnd(pszDomain, pReq->status.cbDomain), VERR_INVALID_PARAMETER);
929 rc = RTStrValidateEncoding(pszDomain);
930 AssertRCReturn(rc, rc);
931
932 /* Advance to the next field. */
933 pbDynamic += pReq->status.cbDomain;
934 cbLeft -= pReq->status.cbDomain;
935 }
936
937 /* pbDetails can be NULL. */
938 const uint8_t *pbDetails = NULL;
939 AssertReturn(pReq->status.cbDetails <= cbLeft, VERR_INVALID_PARAMETER);
940 if (pReq->status.cbDetails > 0)
941 pbDetails = pbDynamic;
942
943 pThis->pDrv->pfnUpdateGuestUserState(pThis->pDrv, pszUser, pszDomain, (uint32_t)pReq->status.state,
944 pbDetails, pReq->status.cbDetails);
945 }
946
947 return VINF_SUCCESS;
948}
949
950
951/**
952 * Handles VMMDevReq_ReportGuestCapabilities.
953 *
954 * @returns VBox status code that the guest should see.
955 * @param pThis The VMMDev instance data.
956 * @param pReqHdr The header of the request to handle.
957 */
958static int vmmdevReqHandler_ReportGuestCapabilities(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
959{
960 VMMDevReqGuestCapabilities *pReq = (VMMDevReqGuestCapabilities *)pReqHdr;
961 AssertMsgReturn(pReq->header.size == sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
962
963 /* Enable VMMDEV_GUEST_SUPPORTS_GRAPHICS automatically for guests using the old
964 * request to report their capabilities.
965 */
966 const uint32_t fu32Caps = pReq->caps | VMMDEV_GUEST_SUPPORTS_GRAPHICS;
967
968 if (pThis->guestCaps != fu32Caps)
969 {
970 /* make a copy of supplied information */
971 pThis->guestCaps = fu32Caps;
972
973 LogRel(("VMMDev: Guest Additions capability report (legacy): (0x%x) seamless: %s, hostWindowMapping: %s, graphics: yes\n",
974 fu32Caps,
975 fu32Caps & VMMDEV_GUEST_SUPPORTS_SEAMLESS ? "yes" : "no",
976 fu32Caps & VMMDEV_GUEST_SUPPORTS_GUEST_HOST_WINDOW_MAPPING ? "yes" : "no"));
977
978 if (pThis->pDrv && pThis->pDrv->pfnUpdateGuestCapabilities)
979 pThis->pDrv->pfnUpdateGuestCapabilities(pThis->pDrv, fu32Caps);
980 }
981 return VINF_SUCCESS;
982}
983
984
985/**
986 * Handles VMMDevReq_SetGuestCapabilities.
987 *
988 * @returns VBox status code that the guest should see.
989 * @param pThis The VMMDev instance data.
990 * @param pReqHdr The header of the request to handle.
991 */
992static int vmmdevReqHandler_SetGuestCapabilities(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
993{
994 VMMDevReqGuestCapabilities2 *pReq = (VMMDevReqGuestCapabilities2 *)pReqHdr;
995 AssertMsgReturn(pReq->header.size == sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
996
997 uint32_t fu32Caps = pThis->guestCaps;
998 fu32Caps |= pReq->u32OrMask;
999 fu32Caps &= ~pReq->u32NotMask;
1000
1001 LogRel(("VMMDev: Guest Additions capability report: (%#x -> %#x) seamless: %s, hostWindowMapping: %s, graphics: %s\n",
1002 pThis->guestCaps, fu32Caps,
1003 fu32Caps & VMMDEV_GUEST_SUPPORTS_SEAMLESS ? "yes" : "no",
1004 fu32Caps & VMMDEV_GUEST_SUPPORTS_GUEST_HOST_WINDOW_MAPPING ? "yes" : "no",
1005 fu32Caps & VMMDEV_GUEST_SUPPORTS_GRAPHICS ? "yes" : "no"));
1006
1007 pThis->guestCaps = fu32Caps;
1008
1009 if (pThis->pDrv && pThis->pDrv->pfnUpdateGuestCapabilities)
1010 pThis->pDrv->pfnUpdateGuestCapabilities(pThis->pDrv, fu32Caps);
1011
1012 return VINF_SUCCESS;
1013}
1014
1015
1016/**
1017 * Handles VMMDevReq_GetMouseStatus.
1018 *
1019 * @returns VBox status code that the guest should see.
1020 * @param pThis The VMMDev instance data.
1021 * @param pReqHdr The header of the request to handle.
1022 */
1023static int vmmdevReqHandler_GetMouseStatus(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
1024{
1025 VMMDevReqMouseStatus *pReq = (VMMDevReqMouseStatus *)pReqHdr;
1026 AssertMsgReturn(pReq->header.size == sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
1027
1028 pReq->mouseFeatures = pThis->mouseCapabilities
1029 & VMMDEV_MOUSE_MASK;
1030 pReq->pointerXPos = pThis->mouseXAbs;
1031 pReq->pointerYPos = pThis->mouseYAbs;
1032 LogRel2(("VMMDev: vmmdevReqHandler_GetMouseStatus: mouseFeatures=%#x, xAbs=%d, yAbs=%d\n",
1033 pReq->mouseFeatures, pReq->pointerXPos, pReq->pointerYPos));
1034 return VINF_SUCCESS;
1035}
1036
1037
1038/**
1039 * Handles VMMDevReq_SetMouseStatus.
1040 *
1041 * @returns VBox status code that the guest should see.
1042 * @param pThis The VMMDev instance data.
1043 * @param pReqHdr The header of the request to handle.
1044 */
1045static int vmmdevReqHandler_SetMouseStatus(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
1046{
1047 VMMDevReqMouseStatus *pReq = (VMMDevReqMouseStatus *)pReqHdr;
1048 AssertMsgReturn(pReq->header.size == sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
1049
1050 LogRelFlow(("VMMDev: vmmdevReqHandler_SetMouseStatus: mouseFeatures=%#x\n", pReq->mouseFeatures));
1051
1052 bool fNotify = false;
1053 if ( (pReq->mouseFeatures & VMMDEV_MOUSE_NOTIFY_HOST_MASK)
1054 != ( pThis->mouseCapabilities
1055 & VMMDEV_MOUSE_NOTIFY_HOST_MASK))
1056 fNotify = true;
1057
1058 pThis->mouseCapabilities &= ~VMMDEV_MOUSE_GUEST_MASK;
1059 pThis->mouseCapabilities |= (pReq->mouseFeatures & VMMDEV_MOUSE_GUEST_MASK);
1060
1061 LogRelFlow(("VMMDev: vmmdevReqHandler_SetMouseStatus: New host capabilities: %#x\n", pThis->mouseCapabilities));
1062
1063 /*
1064 * Notify connector if something changed.
1065 */
1066 if (fNotify)
1067 {
1068 LogRelFlow(("VMMDev: vmmdevReqHandler_SetMouseStatus: Notifying connector\n"));
1069 pThis->pDrv->pfnUpdateMouseCapabilities(pThis->pDrv, pThis->mouseCapabilities);
1070 }
1071
1072 return VINF_SUCCESS;
1073}
1074
1075static int vmmdevVerifyPointerShape(VMMDevReqMousePointer *pReq)
1076{
1077 /* Should be enough for most mouse pointers. */
1078 if (pReq->width > 8192 || pReq->height > 8192)
1079 return VERR_INVALID_PARAMETER;
1080
1081 uint32_t cbShape = (pReq->width + 7) / 8 * pReq->height; /* size of the AND mask */
1082 cbShape = ((cbShape + 3) & ~3) + pReq->width * 4 * pReq->height; /* + gap + size of the XOR mask */
1083 if (RT_UOFFSETOF(VMMDevReqMousePointer, pointerData) + cbShape > pReq->header.size)
1084 return VERR_INVALID_PARAMETER;
1085
1086 return VINF_SUCCESS;
1087}
1088
1089/**
1090 * Handles VMMDevReq_SetPointerShape.
1091 *
1092 * @returns VBox status code that the guest should see.
1093 * @param pThis The VMMDev instance data.
1094 * @param pReqHdr The header of the request to handle.
1095 */
1096static int vmmdevReqHandler_SetPointerShape(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
1097{
1098 VMMDevReqMousePointer *pReq = (VMMDevReqMousePointer *)pReqHdr;
1099 if (pReq->header.size < sizeof(*pReq))
1100 {
1101 AssertMsg(pReq->header.size == 0x10028 && pReq->header.version == 10000, /* don't complain about legacy!!! */
1102 ("VMMDev mouse shape structure has invalid size %d (%#x) version=%d!\n",
1103 pReq->header.size, pReq->header.size, pReq->header.version));
1104 return VERR_INVALID_PARAMETER;
1105 }
1106
1107 bool fVisible = RT_BOOL(pReq->fFlags & VBOX_MOUSE_POINTER_VISIBLE);
1108 bool fAlpha = RT_BOOL(pReq->fFlags & VBOX_MOUSE_POINTER_ALPHA);
1109 bool fShape = RT_BOOL(pReq->fFlags & VBOX_MOUSE_POINTER_SHAPE);
1110
1111 Log(("VMMDevReq_SetPointerShape: visible: %d, alpha: %d, shape = %d, width: %d, height: %d\n",
1112 fVisible, fAlpha, fShape, pReq->width, pReq->height));
1113
1114 if (pReq->header.size == sizeof(VMMDevReqMousePointer))
1115 {
1116 /* The guest did not provide the shape actually. */
1117 fShape = false;
1118 }
1119
1120 /* forward call to driver */
1121 if (fShape)
1122 {
1123 int rc = vmmdevVerifyPointerShape(pReq);
1124 if (RT_FAILURE(rc))
1125 return rc;
1126
1127 pThis->pDrv->pfnUpdatePointerShape(pThis->pDrv,
1128 fVisible,
1129 fAlpha,
1130 pReq->xHot, pReq->yHot,
1131 pReq->width, pReq->height,
1132 pReq->pointerData);
1133 }
1134 else
1135 {
1136 pThis->pDrv->pfnUpdatePointerShape(pThis->pDrv,
1137 fVisible,
1138 0,
1139 0, 0,
1140 0, 0,
1141 NULL);
1142 }
1143
1144 pThis->fHostCursorRequested = fVisible;
1145 return VINF_SUCCESS;
1146}
1147
1148
1149/**
1150 * Handles VMMDevReq_GetHostTime.
1151 *
1152 * @returns VBox status code that the guest should see.
1153 * @param pThis The VMMDev instance data.
1154 * @param pReqHdr The header of the request to handle.
1155 */
1156static int vmmdevReqHandler_GetHostTime(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
1157{
1158 VMMDevReqHostTime *pReq = (VMMDevReqHostTime *)pReqHdr;
1159 AssertMsgReturn(pReq->header.size == sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
1160
1161 if (RT_LIKELY(!pThis->fGetHostTimeDisabled))
1162 {
1163 RTTIMESPEC now;
1164 pReq->time = RTTimeSpecGetMilli(PDMDevHlpTMUtcNow(pThis->pDevIns, &now));
1165 return VINF_SUCCESS;
1166 }
1167 return VERR_NOT_SUPPORTED;
1168}
1169
1170
1171/**
1172 * Handles VMMDevReq_GetHypervisorInfo.
1173 *
1174 * @returns VBox status code that the guest should see.
1175 * @param pThis The VMMDev instance data.
1176 * @param pReqHdr The header of the request to handle.
1177 */
1178static int vmmdevReqHandler_GetHypervisorInfo(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
1179{
1180 VMMDevReqHypervisorInfo *pReq = (VMMDevReqHypervisorInfo *)pReqHdr;
1181 AssertMsgReturn(pReq->header.size == sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
1182
1183 return PGMR3MappingsSize(PDMDevHlpGetVM(pThis->pDevIns), &pReq->hypervisorSize);
1184}
1185
1186
1187/**
1188 * Handles VMMDevReq_SetHypervisorInfo.
1189 *
1190 * @returns VBox status code that the guest should see.
1191 * @param pThis The VMMDev instance data.
1192 * @param pReqHdr The header of the request to handle.
1193 */
1194static int vmmdevReqHandler_SetHypervisorInfo(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
1195{
1196 VMMDevReqHypervisorInfo *pReq = (VMMDevReqHypervisorInfo *)pReqHdr;
1197 AssertMsgReturn(pReq->header.size == sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
1198
1199 int rc;
1200 PVM pVM = PDMDevHlpGetVM(pThis->pDevIns);
1201 if (pReq->hypervisorStart == 0)
1202 rc = PGMR3MappingsUnfix(pVM);
1203 else
1204 {
1205 /* only if the client has queried the size before! */
1206 uint32_t cbMappings;
1207 rc = PGMR3MappingsSize(pVM, &cbMappings);
1208 if (RT_SUCCESS(rc) && pReq->hypervisorSize == cbMappings)
1209 {
1210 /* new reservation */
1211 rc = PGMR3MappingsFix(pVM, pReq->hypervisorStart, pReq->hypervisorSize);
1212 LogRel(("VMMDev: Guest reported fixed hypervisor window at 0%010x LB %#x (rc=%Rrc)\n",
1213 pReq->hypervisorStart, pReq->hypervisorSize, rc));
1214 }
1215 else if (RT_FAILURE(rc))
1216 rc = VERR_TRY_AGAIN;
1217 }
1218 return rc;
1219}
1220
1221
1222/**
1223 * Handles VMMDevReq_RegisterPatchMemory.
1224 *
1225 * @returns VBox status code that the guest should see.
1226 * @param pThis The VMMDev instance data.
1227 * @param pReqHdr The header of the request to handle.
1228 */
1229static int vmmdevReqHandler_RegisterPatchMemory(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
1230{
1231 VMMDevReqPatchMemory *pReq = (VMMDevReqPatchMemory *)pReqHdr;
1232 AssertMsgReturn(pReq->header.size == sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
1233
1234 return VMMR3RegisterPatchMemory(PDMDevHlpGetVM(pThis->pDevIns), pReq->pPatchMem, pReq->cbPatchMem);
1235}
1236
1237
1238/**
1239 * Handles VMMDevReq_DeregisterPatchMemory.
1240 *
1241 * @returns VBox status code that the guest should see.
1242 * @param pThis The VMMDev instance data.
1243 * @param pReqHdr The header of the request to handle.
1244 */
1245static int vmmdevReqHandler_DeregisterPatchMemory(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
1246{
1247 VMMDevReqPatchMemory *pReq = (VMMDevReqPatchMemory *)pReqHdr;
1248 AssertMsgReturn(pReq->header.size == sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
1249
1250 return VMMR3DeregisterPatchMemory(PDMDevHlpGetVM(pThis->pDevIns), pReq->pPatchMem, pReq->cbPatchMem);
1251}
1252
1253
1254/**
1255 * Handles VMMDevReq_SetPowerStatus.
1256 *
1257 * @returns VBox status code that the guest should see.
1258 * @param pThis The VMMDev instance data.
1259 * @param pReqHdr The header of the request to handle.
1260 */
1261static int vmmdevReqHandler_SetPowerStatus(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
1262{
1263 VMMDevPowerStateRequest *pReq = (VMMDevPowerStateRequest *)pReqHdr;
1264 AssertMsgReturn(pReq->header.size == sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
1265
1266 switch (pReq->powerState)
1267 {
1268 case VMMDevPowerState_Pause:
1269 {
1270 LogRel(("VMMDev: Guest requests the VM to be suspended (paused)\n"));
1271 return PDMDevHlpVMSuspend(pThis->pDevIns);
1272 }
1273
1274 case VMMDevPowerState_PowerOff:
1275 {
1276 LogRel(("VMMDev: Guest requests the VM to be turned off\n"));
1277 return PDMDevHlpVMPowerOff(pThis->pDevIns);
1278 }
1279
1280 case VMMDevPowerState_SaveState:
1281 {
1282 if (true /*pThis->fAllowGuestToSaveState*/)
1283 {
1284 LogRel(("VMMDev: Guest requests the VM to be saved and powered off\n"));
1285 return PDMDevHlpVMSuspendSaveAndPowerOff(pThis->pDevIns);
1286 }
1287 LogRel(("VMMDev: Guest requests the VM to be saved and powered off, declined\n"));
1288 return VERR_ACCESS_DENIED;
1289 }
1290
1291 default:
1292 AssertMsgFailed(("VMMDev: Invalid power state request: %d\n", pReq->powerState));
1293 return VERR_INVALID_PARAMETER;
1294 }
1295}
1296
1297
1298/**
1299 * Handles VMMDevReq_GetDisplayChangeRequest
1300 *
1301 * @returns VBox status code that the guest should see.
1302 * @param pThis The VMMDev instance data.
1303 * @param pReqHdr The header of the request to handle.
1304 * @remarks Deprecated.
1305 */
1306static int vmmdevReqHandler_GetDisplayChangeRequest(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
1307{
1308 VMMDevDisplayChangeRequest *pReq = (VMMDevDisplayChangeRequest *)pReqHdr;
1309 AssertMsgReturn(pReq->header.size == sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
1310
1311/**
1312 * @todo It looks like a multi-monitor guest which only uses
1313 * @c VMMDevReq_GetDisplayChangeRequest (not the *2 version) will get
1314 * into a @c VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST event loop if it tries
1315 * to acknowlege host requests for additional monitors. Should the loop
1316 * which checks for those requests be removed?
1317 */
1318
1319 DISPLAYCHANGEREQUEST *pDispRequest = &pThis->displayChangeData.aRequests[0];
1320
1321 if (pReq->eventAck == VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST)
1322 {
1323 /* Current request has been read at least once. */
1324 pDispRequest->fPending = false;
1325
1326 /* Check if there are more pending requests. */
1327 for (unsigned i = 1; i < RT_ELEMENTS(pThis->displayChangeData.aRequests); i++)
1328 {
1329 if (pThis->displayChangeData.aRequests[i].fPending)
1330 {
1331 VMMDevNotifyGuest(pThis, VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST);
1332 break;
1333 }
1334 }
1335
1336 /* Remember which resolution the client has queried, subsequent reads
1337 * will return the same values. */
1338 pDispRequest->lastReadDisplayChangeRequest = pDispRequest->displayChangeRequest;
1339 pThis->displayChangeData.fGuestSentChangeEventAck = true;
1340 }
1341
1342 /* If not a response to a VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST, just
1343 * read the last valid video mode hint. This happens when the guest X server
1344 * determines the initial mode. */
1345 VMMDevDisplayDef const *pDisplayDef = pThis->displayChangeData.fGuestSentChangeEventAck ?
1346 &pDispRequest->lastReadDisplayChangeRequest :
1347 &pDispRequest->displayChangeRequest;
1348 pReq->xres = RT_BOOL(pDisplayDef->fDisplayFlags & VMMDEV_DISPLAY_CX) ? pDisplayDef->cx : 0;
1349 pReq->yres = RT_BOOL(pDisplayDef->fDisplayFlags & VMMDEV_DISPLAY_CY) ? pDisplayDef->cy : 0;
1350 pReq->bpp = RT_BOOL(pDisplayDef->fDisplayFlags & VMMDEV_DISPLAY_BPP) ? pDisplayDef->cBitsPerPixel : 0;
1351
1352 Log(("VMMDev: returning display change request xres = %d, yres = %d, bpp = %d\n", pReq->xres, pReq->yres, pReq->bpp));
1353
1354 return VINF_SUCCESS;
1355}
1356
1357
1358/**
1359 * Handles VMMDevReq_GetDisplayChangeRequest2.
1360 *
1361 * @returns VBox status code that the guest should see.
1362 * @param pThis The VMMDev instance data.
1363 * @param pReqHdr The header of the request to handle.
1364 */
1365static int vmmdevReqHandler_GetDisplayChangeRequest2(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
1366{
1367 VMMDevDisplayChangeRequest2 *pReq = (VMMDevDisplayChangeRequest2 *)pReqHdr;
1368 AssertMsgReturn(pReq->header.size == sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
1369
1370 DISPLAYCHANGEREQUEST *pDispRequest = NULL;
1371
1372 if (pReq->eventAck == VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST)
1373 {
1374 /* Select a pending request to report. */
1375 unsigned i;
1376 for (i = 0; i < RT_ELEMENTS(pThis->displayChangeData.aRequests); i++)
1377 {
1378 if (pThis->displayChangeData.aRequests[i].fPending)
1379 {
1380 pDispRequest = &pThis->displayChangeData.aRequests[i];
1381 /* Remember which request should be reported. */
1382 pThis->displayChangeData.iCurrentMonitor = i;
1383 Log3(("VMMDev: will report pending request for %u\n", i));
1384 break;
1385 }
1386 }
1387
1388 /* Check if there are more pending requests. */
1389 i++;
1390 for (; i < RT_ELEMENTS(pThis->displayChangeData.aRequests); i++)
1391 {
1392 if (pThis->displayChangeData.aRequests[i].fPending)
1393 {
1394 VMMDevNotifyGuest(pThis, VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST);
1395 Log3(("VMMDev: another pending at %u\n", i));
1396 break;
1397 }
1398 }
1399
1400 if (pDispRequest)
1401 {
1402 /* Current request has been read at least once. */
1403 pDispRequest->fPending = false;
1404
1405 /* Remember which resolution the client has queried, subsequent reads
1406 * will return the same values. */
1407 pDispRequest->lastReadDisplayChangeRequest = pDispRequest->displayChangeRequest;
1408 pThis->displayChangeData.fGuestSentChangeEventAck = true;
1409 }
1410 else
1411 {
1412 Log3(("VMMDev: no pending request!!!\n"));
1413 }
1414 }
1415
1416 if (!pDispRequest)
1417 {
1418 Log3(("VMMDev: default to %d\n", pThis->displayChangeData.iCurrentMonitor));
1419 pDispRequest = &pThis->displayChangeData.aRequests[pThis->displayChangeData.iCurrentMonitor];
1420 }
1421
1422 /* If not a response to a VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST, just
1423 * read the last valid video mode hint. This happens when the guest X server
1424 * determines the initial mode. */
1425 VMMDevDisplayDef const *pDisplayDef = pThis->displayChangeData.fGuestSentChangeEventAck ?
1426 &pDispRequest->lastReadDisplayChangeRequest :
1427 &pDispRequest->displayChangeRequest;
1428 pReq->xres = RT_BOOL(pDisplayDef->fDisplayFlags & VMMDEV_DISPLAY_CX) ? pDisplayDef->cx : 0;
1429 pReq->yres = RT_BOOL(pDisplayDef->fDisplayFlags & VMMDEV_DISPLAY_CY) ? pDisplayDef->cy : 0;
1430 pReq->bpp = RT_BOOL(pDisplayDef->fDisplayFlags & VMMDEV_DISPLAY_BPP) ? pDisplayDef->cBitsPerPixel : 0;
1431 pReq->display = pDisplayDef->idDisplay;
1432
1433 Log(("VMMDev: returning display change request xres = %d, yres = %d, bpp = %d at %d\n",
1434 pReq->xres, pReq->yres, pReq->bpp, pReq->display));
1435
1436 return VINF_SUCCESS;
1437}
1438
1439
1440/**
1441 * Handles VMMDevReq_GetDisplayChangeRequestEx.
1442 *
1443 * @returns VBox status code that the guest should see.
1444 * @param pThis The VMMDev instance data.
1445 * @param pReqHdr The header of the request to handle.
1446 */
1447static int vmmdevReqHandler_GetDisplayChangeRequestEx(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
1448{
1449 VMMDevDisplayChangeRequestEx *pReq = (VMMDevDisplayChangeRequestEx *)pReqHdr;
1450 AssertMsgReturn(pReq->header.size == sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
1451
1452 DISPLAYCHANGEREQUEST *pDispRequest = NULL;
1453
1454 if (pReq->eventAck == VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST)
1455 {
1456 /* Select a pending request to report. */
1457 unsigned i;
1458 for (i = 0; i < RT_ELEMENTS(pThis->displayChangeData.aRequests); i++)
1459 {
1460 if (pThis->displayChangeData.aRequests[i].fPending)
1461 {
1462 pDispRequest = &pThis->displayChangeData.aRequests[i];
1463 /* Remember which request should be reported. */
1464 pThis->displayChangeData.iCurrentMonitor = i;
1465 Log3(("VMMDev: will report pending request for %d\n",
1466 i));
1467 break;
1468 }
1469 }
1470
1471 /* Check if there are more pending requests. */
1472 i++;
1473 for (; i < RT_ELEMENTS(pThis->displayChangeData.aRequests); i++)
1474 {
1475 if (pThis->displayChangeData.aRequests[i].fPending)
1476 {
1477 VMMDevNotifyGuest(pThis, VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST);
1478 Log3(("VMMDev: another pending at %d\n",
1479 i));
1480 break;
1481 }
1482 }
1483
1484 if (pDispRequest)
1485 {
1486 /* Current request has been read at least once. */
1487 pDispRequest->fPending = false;
1488
1489 /* Remember which resolution the client has queried, subsequent reads
1490 * will return the same values. */
1491 pDispRequest->lastReadDisplayChangeRequest = pDispRequest->displayChangeRequest;
1492 pThis->displayChangeData.fGuestSentChangeEventAck = true;
1493 }
1494 else
1495 {
1496 Log3(("VMMDev: no pending request!!!\n"));
1497 }
1498 }
1499
1500 if (!pDispRequest)
1501 {
1502 Log3(("VMMDev: default to %d\n",
1503 pThis->displayChangeData.iCurrentMonitor));
1504 pDispRequest = &pThis->displayChangeData.aRequests[pThis->displayChangeData.iCurrentMonitor];
1505 }
1506
1507 /* If not a response to a VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST, just
1508 * read the last valid video mode hint. This happens when the guest X server
1509 * determines the initial mode. */
1510 VMMDevDisplayDef const *pDisplayDef = pThis->displayChangeData.fGuestSentChangeEventAck ?
1511 &pDispRequest->lastReadDisplayChangeRequest :
1512 &pDispRequest->displayChangeRequest;
1513 pReq->xres = RT_BOOL(pDisplayDef->fDisplayFlags & VMMDEV_DISPLAY_CX) ? pDisplayDef->cx : 0;
1514 pReq->yres = RT_BOOL(pDisplayDef->fDisplayFlags & VMMDEV_DISPLAY_CY) ? pDisplayDef->cy : 0;
1515 pReq->bpp = RT_BOOL(pDisplayDef->fDisplayFlags & VMMDEV_DISPLAY_BPP) ? pDisplayDef->cBitsPerPixel : 0;
1516 pReq->display = pDisplayDef->idDisplay;
1517 pReq->cxOrigin = pDisplayDef->xOrigin;
1518 pReq->cyOrigin = pDisplayDef->yOrigin;
1519 pReq->fEnabled = !RT_BOOL(pDisplayDef->fDisplayFlags & VMMDEV_DISPLAY_DISABLED);
1520 pReq->fChangeOrigin = RT_BOOL(pDisplayDef->fDisplayFlags & VMMDEV_DISPLAY_ORIGIN);
1521
1522 Log(("VMMDevEx: returning display change request xres = %d, yres = %d, bpp = %d id %d xPos = %d, yPos = %d & Enabled=%d\n",
1523 pReq->xres, pReq->yres, pReq->bpp, pReq->display, pReq->cxOrigin, pReq->cyOrigin, pReq->fEnabled));
1524
1525 return VINF_SUCCESS;
1526}
1527
1528
1529/**
1530 * Handles VMMDevReq_GetDisplayChangeRequestMulti.
1531 *
1532 * @returns VBox status code that the guest should see.
1533 * @param pThis The VMMDev instance data.
1534 * @param pReqHdr The header of the request to handle.
1535 */
1536static int vmmdevReqHandler_GetDisplayChangeRequestMulti(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
1537{
1538 VMMDevDisplayChangeRequestMulti *pReq = (VMMDevDisplayChangeRequestMulti *)pReqHdr;
1539 unsigned i;
1540
1541 ASSERT_GUEST_MSG_RETURN(pReq->header.size >= sizeof(*pReq),
1542 ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
1543 RT_UNTRUSTED_VALIDATED_FENCE();
1544
1545 uint32_t const cDisplays = pReq->cDisplays;
1546 ASSERT_GUEST_MSG_RETURN(cDisplays > 0 && cDisplays <= RT_ELEMENTS(pThis->displayChangeData.aRequests),
1547 ("cDisplays %u\n", cDisplays), VERR_INVALID_PARAMETER);
1548 RT_UNTRUSTED_VALIDATED_FENCE();
1549
1550 ASSERT_GUEST_MSG_RETURN(pReq->header.size >= sizeof(*pReq) + (cDisplays - 1) * sizeof(VMMDevDisplayDef),
1551 ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
1552 RT_UNTRUSTED_VALIDATED_FENCE();
1553
1554 if (pReq->eventAck == VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST)
1555 {
1556 /* Remember which resolution the client has queried, subsequent reads
1557 * will return the same values. */
1558 for (i = 0; i < RT_ELEMENTS(pThis->displayChangeData.aRequests); ++i)
1559 {
1560 DISPLAYCHANGEREQUEST *pDCR = &pThis->displayChangeData.aRequests[i];
1561
1562 pDCR->lastReadDisplayChangeRequest = pDCR->displayChangeRequest;
1563 pDCR->fPending = false;
1564 }
1565
1566 pThis->displayChangeData.fGuestSentChangeEventAck = true;
1567 }
1568
1569 /* Fill the guest request with monitor layout data. */
1570 for (i = 0; i < cDisplays; ++i)
1571 {
1572 /* If not a response to a VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST, just
1573 * read the last valid video mode hint. This happens when the guest X server
1574 * determines the initial mode. */
1575 DISPLAYCHANGEREQUEST const *pDCR = &pThis->displayChangeData.aRequests[i];
1576 VMMDevDisplayDef const *pDisplayDef = pThis->displayChangeData.fGuestSentChangeEventAck ?
1577 &pDCR->lastReadDisplayChangeRequest :
1578 &pDCR->displayChangeRequest;
1579 pReq->aDisplays[i] = *pDisplayDef;
1580 }
1581
1582 Log(("VMMDev: returning multimonitor display change request cDisplays %d\n", cDisplays));
1583
1584 return VINF_SUCCESS;
1585}
1586
1587
1588/**
1589 * Handles VMMDevReq_VideoModeSupported.
1590 *
1591 * Query whether the given video mode is supported.
1592 *
1593 * @returns VBox status code that the guest should see.
1594 * @param pThis The VMMDev instance data.
1595 * @param pReqHdr The header of the request to handle.
1596 */
1597static int vmmdevReqHandler_VideoModeSupported(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
1598{
1599 VMMDevVideoModeSupportedRequest *pReq = (VMMDevVideoModeSupportedRequest *)pReqHdr;
1600 AssertMsgReturn(pReq->header.size == sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
1601
1602 /* forward the call */
1603 return pThis->pDrv->pfnVideoModeSupported(pThis->pDrv,
1604 0, /* primary screen. */
1605 pReq->width,
1606 pReq->height,
1607 pReq->bpp,
1608 &pReq->fSupported);
1609}
1610
1611
1612/**
1613 * Handles VMMDevReq_VideoModeSupported2.
1614 *
1615 * Query whether the given video mode is supported for a specific display
1616 *
1617 * @returns VBox status code that the guest should see.
1618 * @param pThis The VMMDev instance data.
1619 * @param pReqHdr The header of the request to handle.
1620 */
1621static int vmmdevReqHandler_VideoModeSupported2(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
1622{
1623 VMMDevVideoModeSupportedRequest2 *pReq = (VMMDevVideoModeSupportedRequest2 *)pReqHdr;
1624 AssertMsgReturn(pReq->header.size == sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
1625
1626 /* forward the call */
1627 return pThis->pDrv->pfnVideoModeSupported(pThis->pDrv,
1628 pReq->display,
1629 pReq->width,
1630 pReq->height,
1631 pReq->bpp,
1632 &pReq->fSupported);
1633}
1634
1635
1636
1637/**
1638 * Handles VMMDevReq_GetHeightReduction.
1639 *
1640 * @returns VBox status code that the guest should see.
1641 * @param pThis The VMMDev instance data.
1642 * @param pReqHdr The header of the request to handle.
1643 */
1644static int vmmdevReqHandler_GetHeightReduction(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
1645{
1646 VMMDevGetHeightReductionRequest *pReq = (VMMDevGetHeightReductionRequest *)pReqHdr;
1647 AssertMsgReturn(pReq->header.size == sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
1648
1649 /* forward the call */
1650 return pThis->pDrv->pfnGetHeightReduction(pThis->pDrv, &pReq->heightReduction);
1651}
1652
1653
1654/**
1655 * Handles VMMDevReq_AcknowledgeEvents.
1656 *
1657 * @returns VBox status code that the guest should see.
1658 * @param pThis The VMMDev instance data.
1659 * @param pReqHdr The header of the request to handle.
1660 */
1661static int vmmdevReqHandler_AcknowledgeEvents(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
1662{
1663 VMMDevEvents *pReq = (VMMDevEvents *)pReqHdr;
1664 AssertMsgReturn(pReq->header.size == sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
1665
1666 if (!VMMDEV_INTERFACE_VERSION_IS_1_03(pThis))
1667 {
1668 if (pThis->fNewGuestFilterMask)
1669 {
1670 pThis->fNewGuestFilterMask = false;
1671 pThis->u32GuestFilterMask = pThis->u32NewGuestFilterMask;
1672 }
1673
1674 pReq->events = pThis->u32HostEventFlags & pThis->u32GuestFilterMask;
1675
1676 pThis->u32HostEventFlags &= ~pThis->u32GuestFilterMask;
1677 pThis->pVMMDevRAMR3->V.V1_04.fHaveEvents = false;
1678 PDMDevHlpPCISetIrqNoWait(pThis->pDevIns, 0, 0);
1679 }
1680 else
1681 vmmdevSetIRQ_Legacy(pThis);
1682 return VINF_SUCCESS;
1683}
1684
1685
1686/**
1687 * Handles VMMDevReq_CtlGuestFilterMask.
1688 *
1689 * @returns VBox status code that the guest should see.
1690 * @param pThis The VMMDev instance data.
1691 * @param pReqHdr The header of the request to handle.
1692 */
1693static int vmmdevReqHandler_CtlGuestFilterMask(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
1694{
1695 VMMDevCtlGuestFilterMask *pReq = (VMMDevCtlGuestFilterMask *)pReqHdr;
1696 AssertMsgReturn(pReq->header.size == sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
1697
1698 LogRelFlow(("VMMDev: vmmdevReqHandler_CtlGuestFilterMask: OR mask: %#x, NOT mask: %#x\n", pReq->u32OrMask, pReq->u32NotMask));
1699
1700 /* HGCM event notification is enabled by the VMMDev device
1701 * automatically when any HGCM command is issued. The guest
1702 * cannot disable these notifications. */
1703 VMMDevCtlSetGuestFilterMask(pThis, pReq->u32OrMask, pReq->u32NotMask & ~VMMDEV_EVENT_HGCM);
1704 return VINF_SUCCESS;
1705}
1706
1707#ifdef VBOX_WITH_HGCM
1708
1709/**
1710 * Handles VMMDevReq_HGCMConnect.
1711 *
1712 * @returns VBox status code that the guest should see.
1713 * @param pThis The VMMDev instance data.
1714 * @param pReqHdr The header of the request to handle.
1715 * @param GCPhysReqHdr The guest physical address of the request header.
1716 */
1717static int vmmdevReqHandler_HGCMConnect(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr, RTGCPHYS GCPhysReqHdr)
1718{
1719 VMMDevHGCMConnect *pReq = (VMMDevHGCMConnect *)pReqHdr;
1720 AssertMsgReturn(pReq->header.header.size >= sizeof(*pReq), ("%u\n", pReq->header.header.size), VERR_INVALID_PARAMETER); /** @todo Not sure why this is >= ... */
1721
1722 if (pThis->pHGCMDrv)
1723 {
1724 Log(("VMMDevReq_HGCMConnect\n"));
1725 return vmmdevHGCMConnect(pThis, pReq, GCPhysReqHdr);
1726 }
1727
1728 Log(("VMMDevReq_HGCMConnect: HGCM Connector is NULL!\n"));
1729 return VERR_NOT_SUPPORTED;
1730}
1731
1732
1733/**
1734 * Handles VMMDevReq_HGCMDisconnect.
1735 *
1736 * @returns VBox status code that the guest should see.
1737 * @param pThis The VMMDev instance data.
1738 * @param pReqHdr The header of the request to handle.
1739 * @param GCPhysReqHdr The guest physical address of the request header.
1740 */
1741static int vmmdevReqHandler_HGCMDisconnect(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr, RTGCPHYS GCPhysReqHdr)
1742{
1743 VMMDevHGCMDisconnect *pReq = (VMMDevHGCMDisconnect *)pReqHdr;
1744 AssertMsgReturn(pReq->header.header.size >= sizeof(*pReq), ("%u\n", pReq->header.header.size), VERR_INVALID_PARAMETER); /** @todo Not sure why this >= ... */
1745
1746 if (pThis->pHGCMDrv)
1747 {
1748 Log(("VMMDevReq_VMMDevHGCMDisconnect\n"));
1749 return vmmdevHGCMDisconnect(pThis, pReq, GCPhysReqHdr);
1750 }
1751
1752 Log(("VMMDevReq_VMMDevHGCMDisconnect: HGCM Connector is NULL!\n"));
1753 return VERR_NOT_SUPPORTED;
1754}
1755
1756
1757/**
1758 * Handles VMMDevReq_HGCMCall.
1759 *
1760 * @returns VBox status code that the guest should see.
1761 * @param pThis The VMMDev instance data.
1762 * @param pReqHdr The header of the request to handle.
1763 * @param GCPhysReqHdr The guest physical address of the request header.
1764 */
1765static int vmmdevReqHandler_HGCMCall(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr, RTGCPHYS GCPhysReqHdr)
1766{
1767 VMMDevHGCMCall *pReq = (VMMDevHGCMCall *)pReqHdr;
1768 AssertMsgReturn(pReq->header.header.size >= sizeof(*pReq), ("%u\n", pReq->header.header.size), VERR_INVALID_PARAMETER);
1769
1770 if (pThis->pHGCMDrv)
1771 {
1772 Log2(("VMMDevReq_HGCMCall: sizeof(VMMDevHGCMRequest) = %04X\n", sizeof(VMMDevHGCMCall)));
1773 Log2(("%.*Rhxd\n", pReq->header.header.size, pReq));
1774
1775 return vmmdevHGCMCall(pThis, pReq, pReq->header.header.size, GCPhysReqHdr, pReq->header.header.requestType);
1776 }
1777
1778 Log(("VMMDevReq_HGCMCall: HGCM Connector is NULL!\n"));
1779 return VERR_NOT_SUPPORTED;
1780}
1781
1782/**
1783 * Handles VMMDevReq_HGCMCancel.
1784 *
1785 * @returns VBox status code that the guest should see.
1786 * @param pThis The VMMDev instance data.
1787 * @param pReqHdr The header of the request to handle.
1788 * @param GCPhysReqHdr The guest physical address of the request header.
1789 */
1790static int vmmdevReqHandler_HGCMCancel(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr, RTGCPHYS GCPhysReqHdr)
1791{
1792 VMMDevHGCMCancel *pReq = (VMMDevHGCMCancel *)pReqHdr;
1793 AssertMsgReturn(pReq->header.header.size >= sizeof(*pReq), ("%u\n", pReq->header.header.size), VERR_INVALID_PARAMETER); /** @todo Not sure why this >= ... */
1794
1795 if (pThis->pHGCMDrv)
1796 {
1797 Log(("VMMDevReq_VMMDevHGCMCancel\n"));
1798 return vmmdevHGCMCancel(pThis, pReq, GCPhysReqHdr);
1799 }
1800
1801 Log(("VMMDevReq_VMMDevHGCMCancel: HGCM Connector is NULL!\n"));
1802 return VERR_NOT_SUPPORTED;
1803}
1804
1805
1806/**
1807 * Handles VMMDevReq_HGCMCancel2.
1808 *
1809 * @returns VBox status code that the guest should see.
1810 * @param pThis The VMMDev instance data.
1811 * @param pReqHdr The header of the request to handle.
1812 */
1813static int vmmdevReqHandler_HGCMCancel2(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
1814{
1815 VMMDevHGCMCancel2 *pReq = (VMMDevHGCMCancel2 *)pReqHdr;
1816 AssertMsgReturn(pReq->header.size >= sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER); /** @todo Not sure why this >= ... */
1817
1818 if (pThis->pHGCMDrv)
1819 {
1820 Log(("VMMDevReq_HGCMCancel2\n"));
1821 return vmmdevHGCMCancel2(pThis, pReq->physReqToCancel);
1822 }
1823
1824 Log(("VMMDevReq_HGCMConnect2: HGCM Connector is NULL!\n"));
1825 return VERR_NOT_SUPPORTED;
1826}
1827
1828#endif /* VBOX_WITH_HGCM */
1829
1830
1831/**
1832 * Handles VMMDevReq_VideoAccelEnable.
1833 *
1834 * @returns VBox status code that the guest should see.
1835 * @param pThis The VMMDev instance data.
1836 * @param pReqHdr The header of the request to handle.
1837 */
1838static int vmmdevReqHandler_VideoAccelEnable(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
1839{
1840 VMMDevVideoAccelEnable *pReq = (VMMDevVideoAccelEnable *)pReqHdr;
1841 AssertMsgReturn(pReq->header.size >= sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER); /** @todo Not sure why this >= ... */
1842
1843 if (!pThis->pDrv)
1844 {
1845 Log(("VMMDevReq_VideoAccelEnable Connector is NULL!!\n"));
1846 return VERR_NOT_SUPPORTED;
1847 }
1848
1849 if (pReq->cbRingBuffer != VMMDEV_VBVA_RING_BUFFER_SIZE)
1850 {
1851 /* The guest driver seems compiled with different headers. */
1852 LogRelMax(16,("VMMDevReq_VideoAccelEnable guest ring buffer size %#x, should be %#x!!\n", pReq->cbRingBuffer, VMMDEV_VBVA_RING_BUFFER_SIZE));
1853 return VERR_INVALID_PARAMETER;
1854 }
1855
1856 /* The request is correct. */
1857 pReq->fu32Status |= VBVA_F_STATUS_ACCEPTED;
1858
1859 LogFlow(("VMMDevReq_VideoAccelEnable pReq->u32Enable = %d\n", pReq->u32Enable));
1860
1861 int rc = pReq->u32Enable
1862 ? pThis->pDrv->pfnVideoAccelEnable(pThis->pDrv, true, &pThis->pVMMDevRAMR3->vbvaMemory)
1863 : pThis->pDrv->pfnVideoAccelEnable(pThis->pDrv, false, NULL);
1864
1865 if ( pReq->u32Enable
1866 && RT_SUCCESS(rc))
1867 {
1868 pReq->fu32Status |= VBVA_F_STATUS_ENABLED;
1869
1870 /* Remember that guest successfully enabled acceleration.
1871 * We need to reestablish it on restoring the VM from saved state.
1872 */
1873 pThis->u32VideoAccelEnabled = 1;
1874 }
1875 else
1876 {
1877 /* The acceleration was not enabled. Remember that. */
1878 pThis->u32VideoAccelEnabled = 0;
1879 }
1880 return VINF_SUCCESS;
1881}
1882
1883
1884/**
1885 * Handles VMMDevReq_VideoAccelFlush.
1886 *
1887 * @returns VBox status code that the guest should see.
1888 * @param pThis The VMMDev instance data.
1889 * @param pReqHdr The header of the request to handle.
1890 */
1891static int vmmdevReqHandler_VideoAccelFlush(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
1892{
1893 VMMDevVideoAccelFlush *pReq = (VMMDevVideoAccelFlush *)pReqHdr;
1894 AssertMsgReturn(pReq->header.size >= sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER); /** @todo Not sure why this >= ... */
1895
1896 if (!pThis->pDrv)
1897 {
1898 Log(("VMMDevReq_VideoAccelFlush: Connector is NULL!!!\n"));
1899 return VERR_NOT_SUPPORTED;
1900 }
1901
1902 pThis->pDrv->pfnVideoAccelFlush(pThis->pDrv);
1903 return VINF_SUCCESS;
1904}
1905
1906
1907/**
1908 * Handles VMMDevReq_VideoSetVisibleRegion.
1909 *
1910 * @returns VBox status code that the guest should see.
1911 * @param pThis The VMMDev instance data.
1912 * @param pReqHdr The header of the request to handle.
1913 */
1914static int vmmdevReqHandler_VideoSetVisibleRegion(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
1915{
1916 VMMDevVideoSetVisibleRegion *pReq = (VMMDevVideoSetVisibleRegion *)pReqHdr;
1917 AssertMsgReturn(pReq->header.size + sizeof(RTRECT) >= sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
1918
1919 if (!pThis->pDrv)
1920 {
1921 Log(("VMMDevReq_VideoSetVisibleRegion: Connector is NULL!!!\n"));
1922 return VERR_NOT_SUPPORTED;
1923 }
1924
1925 if ( pReq->cRect > _1M /* restrict to sane range */
1926 || pReq->header.size != sizeof(VMMDevVideoSetVisibleRegion) + pReq->cRect * sizeof(RTRECT) - sizeof(RTRECT))
1927 {
1928 Log(("VMMDevReq_VideoSetVisibleRegion: cRects=%#x doesn't match size=%#x or is out of bounds\n",
1929 pReq->cRect, pReq->header.size));
1930 return VERR_INVALID_PARAMETER;
1931 }
1932
1933 Log(("VMMDevReq_VideoSetVisibleRegion %d rectangles\n", pReq->cRect));
1934 /* forward the call */
1935 return pThis->pDrv->pfnSetVisibleRegion(pThis->pDrv, pReq->cRect, &pReq->Rect);
1936}
1937
1938
1939/**
1940 * Handles VMMDevReq_GetSeamlessChangeRequest.
1941 *
1942 * @returns VBox status code that the guest should see.
1943 * @param pThis The VMMDev instance data.
1944 * @param pReqHdr The header of the request to handle.
1945 */
1946static int vmmdevReqHandler_GetSeamlessChangeRequest(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
1947{
1948 VMMDevSeamlessChangeRequest *pReq = (VMMDevSeamlessChangeRequest *)pReqHdr;
1949 AssertMsgReturn(pReq->header.size == sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
1950
1951 /* just pass on the information */
1952 Log(("VMMDev: returning seamless change request mode=%d\n", pThis->fSeamlessEnabled));
1953 if (pThis->fSeamlessEnabled)
1954 pReq->mode = VMMDev_Seamless_Visible_Region;
1955 else
1956 pReq->mode = VMMDev_Seamless_Disabled;
1957
1958 if (pReq->eventAck == VMMDEV_EVENT_SEAMLESS_MODE_CHANGE_REQUEST)
1959 {
1960 /* Remember which mode the client has queried. */
1961 pThis->fLastSeamlessEnabled = pThis->fSeamlessEnabled;
1962 }
1963
1964 return VINF_SUCCESS;
1965}
1966
1967
1968/**
1969 * Handles VMMDevReq_GetVRDPChangeRequest.
1970 *
1971 * @returns VBox status code that the guest should see.
1972 * @param pThis The VMMDev instance data.
1973 * @param pReqHdr The header of the request to handle.
1974 */
1975static int vmmdevReqHandler_GetVRDPChangeRequest(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
1976{
1977 VMMDevVRDPChangeRequest *pReq = (VMMDevVRDPChangeRequest *)pReqHdr;
1978 AssertMsgReturn(pReq->header.size == sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
1979
1980 /* just pass on the information */
1981 Log(("VMMDev: returning VRDP status %d level %d\n", pThis->fVRDPEnabled, pThis->uVRDPExperienceLevel));
1982
1983 pReq->u8VRDPActive = pThis->fVRDPEnabled;
1984 pReq->u32VRDPExperienceLevel = pThis->uVRDPExperienceLevel;
1985
1986 return VINF_SUCCESS;
1987}
1988
1989
1990/**
1991 * Handles VMMDevReq_GetMemBalloonChangeRequest.
1992 *
1993 * @returns VBox status code that the guest should see.
1994 * @param pThis The VMMDev instance data.
1995 * @param pReqHdr The header of the request to handle.
1996 */
1997static int vmmdevReqHandler_GetMemBalloonChangeRequest(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
1998{
1999 VMMDevGetMemBalloonChangeRequest *pReq = (VMMDevGetMemBalloonChangeRequest *)pReqHdr;
2000 AssertMsgReturn(pReq->header.size == sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
2001
2002 /* just pass on the information */
2003 Log(("VMMDev: returning memory balloon size =%d\n", pThis->cMbMemoryBalloon));
2004 pReq->cBalloonChunks = pThis->cMbMemoryBalloon;
2005 pReq->cPhysMemChunks = pThis->cbGuestRAM / (uint64_t)_1M;
2006
2007 if (pReq->eventAck == VMMDEV_EVENT_BALLOON_CHANGE_REQUEST)
2008 {
2009 /* Remember which mode the client has queried. */
2010 pThis->cMbMemoryBalloonLast = pThis->cMbMemoryBalloon;
2011 }
2012
2013 return VINF_SUCCESS;
2014}
2015
2016
2017/**
2018 * Handles VMMDevReq_ChangeMemBalloon.
2019 *
2020 * @returns VBox status code that the guest should see.
2021 * @param pThis The VMMDev instance data.
2022 * @param pReqHdr The header of the request to handle.
2023 */
2024static int vmmdevReqHandler_ChangeMemBalloon(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
2025{
2026 VMMDevChangeMemBalloon *pReq = (VMMDevChangeMemBalloon *)pReqHdr;
2027 AssertMsgReturn(pReq->header.size >= sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
2028 AssertMsgReturn(pReq->cPages == VMMDEV_MEMORY_BALLOON_CHUNK_PAGES, ("%u\n", pReq->cPages), VERR_INVALID_PARAMETER);
2029 AssertMsgReturn(pReq->header.size == (uint32_t)RT_UOFFSETOF_DYN(VMMDevChangeMemBalloon, aPhysPage[pReq->cPages]),
2030 ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
2031
2032 Log(("VMMDevReq_ChangeMemBalloon\n"));
2033 int rc = PGMR3PhysChangeMemBalloon(PDMDevHlpGetVM(pThis->pDevIns), !!pReq->fInflate, pReq->cPages, pReq->aPhysPage);
2034 if (pReq->fInflate)
2035 STAM_REL_U32_INC(&pThis->StatMemBalloonChunks);
2036 else
2037 STAM_REL_U32_DEC(&pThis->StatMemBalloonChunks);
2038 return rc;
2039}
2040
2041
2042/**
2043 * Handles VMMDevReq_GetStatisticsChangeRequest.
2044 *
2045 * @returns VBox status code that the guest should see.
2046 * @param pThis The VMMDev instance data.
2047 * @param pReqHdr The header of the request to handle.
2048 */
2049static int vmmdevReqHandler_GetStatisticsChangeRequest(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
2050{
2051 VMMDevGetStatisticsChangeRequest *pReq = (VMMDevGetStatisticsChangeRequest *)pReqHdr;
2052 AssertMsgReturn(pReq->header.size == sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
2053
2054 Log(("VMMDevReq_GetStatisticsChangeRequest\n"));
2055 /* just pass on the information */
2056 Log(("VMMDev: returning statistics interval %d seconds\n", pThis->u32StatIntervalSize));
2057 pReq->u32StatInterval = pThis->u32StatIntervalSize;
2058
2059 if (pReq->eventAck == VMMDEV_EVENT_STATISTICS_INTERVAL_CHANGE_REQUEST)
2060 {
2061 /* Remember which mode the client has queried. */
2062 pThis->u32LastStatIntervalSize= pThis->u32StatIntervalSize;
2063 }
2064
2065 return VINF_SUCCESS;
2066}
2067
2068
2069/**
2070 * Handles VMMDevReq_ReportGuestStats.
2071 *
2072 * @returns VBox status code that the guest should see.
2073 * @param pThis The VMMDev instance data.
2074 * @param pReqHdr The header of the request to handle.
2075 */
2076static int vmmdevReqHandler_ReportGuestStats(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
2077{
2078 VMMDevReportGuestStats *pReq = (VMMDevReportGuestStats *)pReqHdr;
2079 AssertMsgReturn(pReq->header.size == sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
2080
2081 Log(("VMMDevReq_ReportGuestStats\n"));
2082#ifdef LOG_ENABLED
2083 VBoxGuestStatistics *pGuestStats = &pReq->guestStats;
2084
2085 Log(("Current statistics:\n"));
2086 if (pGuestStats->u32StatCaps & VBOX_GUEST_STAT_CPU_LOAD_IDLE)
2087 Log(("CPU%u: CPU Load Idle %-3d%%\n", pGuestStats->u32CpuId, pGuestStats->u32CpuLoad_Idle));
2088
2089 if (pGuestStats->u32StatCaps & VBOX_GUEST_STAT_CPU_LOAD_KERNEL)
2090 Log(("CPU%u: CPU Load Kernel %-3d%%\n", pGuestStats->u32CpuId, pGuestStats->u32CpuLoad_Kernel));
2091
2092 if (pGuestStats->u32StatCaps & VBOX_GUEST_STAT_CPU_LOAD_USER)
2093 Log(("CPU%u: CPU Load User %-3d%%\n", pGuestStats->u32CpuId, pGuestStats->u32CpuLoad_User));
2094
2095 if (pGuestStats->u32StatCaps & VBOX_GUEST_STAT_THREADS)
2096 Log(("CPU%u: Thread %d\n", pGuestStats->u32CpuId, pGuestStats->u32Threads));
2097
2098 if (pGuestStats->u32StatCaps & VBOX_GUEST_STAT_PROCESSES)
2099 Log(("CPU%u: Processes %d\n", pGuestStats->u32CpuId, pGuestStats->u32Processes));
2100
2101 if (pGuestStats->u32StatCaps & VBOX_GUEST_STAT_HANDLES)
2102 Log(("CPU%u: Handles %d\n", pGuestStats->u32CpuId, pGuestStats->u32Handles));
2103
2104 if (pGuestStats->u32StatCaps & VBOX_GUEST_STAT_MEMORY_LOAD)
2105 Log(("CPU%u: Memory Load %d%%\n", pGuestStats->u32CpuId, pGuestStats->u32MemoryLoad));
2106
2107 /* Note that reported values are in pages; upper layers expect them in megabytes */
2108 Log(("CPU%u: Page size %-4d bytes\n", pGuestStats->u32CpuId, pGuestStats->u32PageSize));
2109 Assert(pGuestStats->u32PageSize == 4096);
2110
2111 if (pGuestStats->u32StatCaps & VBOX_GUEST_STAT_PHYS_MEM_TOTAL)
2112 Log(("CPU%u: Total physical memory %-4d MB\n", pGuestStats->u32CpuId, (pGuestStats->u32PhysMemTotal + (_1M/_4K)-1) / (_1M/_4K)));
2113
2114 if (pGuestStats->u32StatCaps & VBOX_GUEST_STAT_PHYS_MEM_AVAIL)
2115 Log(("CPU%u: Free physical memory %-4d MB\n", pGuestStats->u32CpuId, pGuestStats->u32PhysMemAvail / (_1M/_4K)));
2116
2117 if (pGuestStats->u32StatCaps & VBOX_GUEST_STAT_PHYS_MEM_BALLOON)
2118 Log(("CPU%u: Memory balloon size %-4d MB\n", pGuestStats->u32CpuId, pGuestStats->u32PhysMemBalloon / (_1M/_4K)));
2119
2120 if (pGuestStats->u32StatCaps & VBOX_GUEST_STAT_MEM_COMMIT_TOTAL)
2121 Log(("CPU%u: Committed memory %-4d MB\n", pGuestStats->u32CpuId, pGuestStats->u32MemCommitTotal / (_1M/_4K)));
2122
2123 if (pGuestStats->u32StatCaps & VBOX_GUEST_STAT_MEM_KERNEL_TOTAL)
2124 Log(("CPU%u: Total kernel memory %-4d MB\n", pGuestStats->u32CpuId, pGuestStats->u32MemKernelTotal / (_1M/_4K)));
2125
2126 if (pGuestStats->u32StatCaps & VBOX_GUEST_STAT_MEM_KERNEL_PAGED)
2127 Log(("CPU%u: Paged kernel memory %-4d MB\n", pGuestStats->u32CpuId, pGuestStats->u32MemKernelPaged / (_1M/_4K)));
2128
2129 if (pGuestStats->u32StatCaps & VBOX_GUEST_STAT_MEM_KERNEL_NONPAGED)
2130 Log(("CPU%u: Nonpaged kernel memory %-4d MB\n", pGuestStats->u32CpuId, pGuestStats->u32MemKernelNonPaged / (_1M/_4K)));
2131
2132 if (pGuestStats->u32StatCaps & VBOX_GUEST_STAT_MEM_SYSTEM_CACHE)
2133 Log(("CPU%u: System cache size %-4d MB\n", pGuestStats->u32CpuId, pGuestStats->u32MemSystemCache / (_1M/_4K)));
2134
2135 if (pGuestStats->u32StatCaps & VBOX_GUEST_STAT_PAGE_FILE_SIZE)
2136 Log(("CPU%u: Page file size %-4d MB\n", pGuestStats->u32CpuId, pGuestStats->u32PageFileSize / (_1M/_4K)));
2137 Log(("Statistics end *******************\n"));
2138#endif /* LOG_ENABLED */
2139
2140 /* forward the call */
2141 return pThis->pDrv->pfnReportStatistics(pThis->pDrv, &pReq->guestStats);
2142}
2143
2144
2145/**
2146 * Handles VMMDevReq_QueryCredentials.
2147 *
2148 * @returns VBox status code that the guest should see.
2149 * @param pThis The VMMDev instance data.
2150 * @param pReqHdr The header of the request to handle.
2151 */
2152static int vmmdevReqHandler_QueryCredentials(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
2153{
2154 VMMDevCredentials *pReq = (VMMDevCredentials *)pReqHdr;
2155 AssertMsgReturn(pReq->header.size == sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
2156
2157 /* let's start by nulling out the data */
2158 memset(pReq->szUserName, '\0', VMMDEV_CREDENTIALS_SZ_SIZE);
2159 memset(pReq->szPassword, '\0', VMMDEV_CREDENTIALS_SZ_SIZE);
2160 memset(pReq->szDomain, '\0', VMMDEV_CREDENTIALS_SZ_SIZE);
2161
2162 /* should we return whether we got credentials for a logon? */
2163 if (pReq->u32Flags & VMMDEV_CREDENTIALS_QUERYPRESENCE)
2164 {
2165 if ( pThis->pCredentials->Logon.szUserName[0]
2166 || pThis->pCredentials->Logon.szPassword[0]
2167 || pThis->pCredentials->Logon.szDomain[0])
2168 pReq->u32Flags |= VMMDEV_CREDENTIALS_PRESENT;
2169 else
2170 pReq->u32Flags &= ~VMMDEV_CREDENTIALS_PRESENT;
2171 }
2172
2173 /* does the guest want to read logon credentials? */
2174 if (pReq->u32Flags & VMMDEV_CREDENTIALS_READ)
2175 {
2176 if (pThis->pCredentials->Logon.szUserName[0])
2177 strcpy(pReq->szUserName, pThis->pCredentials->Logon.szUserName);
2178 if (pThis->pCredentials->Logon.szPassword[0])
2179 strcpy(pReq->szPassword, pThis->pCredentials->Logon.szPassword);
2180 if (pThis->pCredentials->Logon.szDomain[0])
2181 strcpy(pReq->szDomain, pThis->pCredentials->Logon.szDomain);
2182 if (!pThis->pCredentials->Logon.fAllowInteractiveLogon)
2183 pReq->u32Flags |= VMMDEV_CREDENTIALS_NOLOCALLOGON;
2184 else
2185 pReq->u32Flags &= ~VMMDEV_CREDENTIALS_NOLOCALLOGON;
2186 }
2187
2188 if (!pThis->fKeepCredentials)
2189 {
2190 /* does the caller want us to destroy the logon credentials? */
2191 if (pReq->u32Flags & VMMDEV_CREDENTIALS_CLEAR)
2192 {
2193 memset(pThis->pCredentials->Logon.szUserName, '\0', VMMDEV_CREDENTIALS_SZ_SIZE);
2194 memset(pThis->pCredentials->Logon.szPassword, '\0', VMMDEV_CREDENTIALS_SZ_SIZE);
2195 memset(pThis->pCredentials->Logon.szDomain, '\0', VMMDEV_CREDENTIALS_SZ_SIZE);
2196 }
2197 }
2198
2199 /* does the guest want to read credentials for verification? */
2200 if (pReq->u32Flags & VMMDEV_CREDENTIALS_READJUDGE)
2201 {
2202 if (pThis->pCredentials->Judge.szUserName[0])
2203 strcpy(pReq->szUserName, pThis->pCredentials->Judge.szUserName);
2204 if (pThis->pCredentials->Judge.szPassword[0])
2205 strcpy(pReq->szPassword, pThis->pCredentials->Judge.szPassword);
2206 if (pThis->pCredentials->Judge.szDomain[0])
2207 strcpy(pReq->szDomain, pThis->pCredentials->Judge.szDomain);
2208 }
2209
2210 /* does the caller want us to destroy the judgement credentials? */
2211 if (pReq->u32Flags & VMMDEV_CREDENTIALS_CLEARJUDGE)
2212 {
2213 memset(pThis->pCredentials->Judge.szUserName, '\0', VMMDEV_CREDENTIALS_SZ_SIZE);
2214 memset(pThis->pCredentials->Judge.szPassword, '\0', VMMDEV_CREDENTIALS_SZ_SIZE);
2215 memset(pThis->pCredentials->Judge.szDomain, '\0', VMMDEV_CREDENTIALS_SZ_SIZE);
2216 }
2217
2218 return VINF_SUCCESS;
2219}
2220
2221
2222/**
2223 * Handles VMMDevReq_ReportCredentialsJudgement.
2224 *
2225 * @returns VBox status code that the guest should see.
2226 * @param pThis The VMMDev instance data.
2227 * @param pReqHdr The header of the request to handle.
2228 */
2229static int vmmdevReqHandler_ReportCredentialsJudgement(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
2230{
2231 VMMDevCredentials *pReq = (VMMDevCredentials *)pReqHdr;
2232 AssertMsgReturn(pReq->header.size == sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
2233
2234 /* what does the guest think about the credentials? (note: the order is important here!) */
2235 if (pReq->u32Flags & VMMDEV_CREDENTIALS_JUDGE_DENY)
2236 pThis->pDrv->pfnSetCredentialsJudgementResult(pThis->pDrv, VMMDEV_CREDENTIALS_JUDGE_DENY);
2237 else if (pReq->u32Flags & VMMDEV_CREDENTIALS_JUDGE_NOJUDGEMENT)
2238 pThis->pDrv->pfnSetCredentialsJudgementResult(pThis->pDrv, VMMDEV_CREDENTIALS_JUDGE_NOJUDGEMENT);
2239 else if (pReq->u32Flags & VMMDEV_CREDENTIALS_JUDGE_OK)
2240 pThis->pDrv->pfnSetCredentialsJudgementResult(pThis->pDrv, VMMDEV_CREDENTIALS_JUDGE_OK);
2241 else
2242 {
2243 Log(("VMMDevReq_ReportCredentialsJudgement: invalid flags: %d!!!\n", pReq->u32Flags));
2244 /** @todo why don't we return VERR_INVALID_PARAMETER to the guest? */
2245 }
2246
2247 return VINF_SUCCESS;
2248}
2249
2250
2251/**
2252 * Handles VMMDevReq_GetHostVersion.
2253 *
2254 * @returns VBox status code that the guest should see.
2255 * @param pReqHdr The header of the request to handle.
2256 * @since 3.1.0
2257 * @note The ring-0 VBoxGuestLib uses this to check whether
2258 * VMMDevHGCMParmType_PageList is supported.
2259 */
2260static int vmmdevReqHandler_GetHostVersion(VMMDevRequestHeader *pReqHdr)
2261{
2262 VMMDevReqHostVersion *pReq = (VMMDevReqHostVersion *)pReqHdr;
2263 AssertMsgReturn(pReq->header.size == sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
2264
2265 pReq->major = RTBldCfgVersionMajor();
2266 pReq->minor = RTBldCfgVersionMinor();
2267 pReq->build = RTBldCfgVersionBuild();
2268 pReq->revision = RTBldCfgRevision();
2269 pReq->features = VMMDEV_HVF_HGCM_PHYS_PAGE_LIST;
2270 return VINF_SUCCESS;
2271}
2272
2273
2274/**
2275 * Handles VMMDevReq_GetCpuHotPlugRequest.
2276 *
2277 * @returns VBox status code that the guest should see.
2278 * @param pThis The VMMDev instance data.
2279 * @param pReqHdr The header of the request to handle.
2280 */
2281static int vmmdevReqHandler_GetCpuHotPlugRequest(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
2282{
2283 VMMDevGetCpuHotPlugRequest *pReq = (VMMDevGetCpuHotPlugRequest *)pReqHdr;
2284 AssertMsgReturn(pReq->header.size == sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
2285
2286 pReq->enmEventType = pThis->enmCpuHotPlugEvent;
2287 pReq->idCpuCore = pThis->idCpuCore;
2288 pReq->idCpuPackage = pThis->idCpuPackage;
2289
2290 /* Clear the event */
2291 pThis->enmCpuHotPlugEvent = VMMDevCpuEventType_None;
2292 pThis->idCpuCore = UINT32_MAX;
2293 pThis->idCpuPackage = UINT32_MAX;
2294
2295 return VINF_SUCCESS;
2296}
2297
2298
2299/**
2300 * Handles VMMDevReq_SetCpuHotPlugStatus.
2301 *
2302 * @returns VBox status code that the guest should see.
2303 * @param pThis The VMMDev instance data.
2304 * @param pReqHdr The header of the request to handle.
2305 */
2306static int vmmdevReqHandler_SetCpuHotPlugStatus(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
2307{
2308 VMMDevCpuHotPlugStatusRequest *pReq = (VMMDevCpuHotPlugStatusRequest *)pReqHdr;
2309 AssertMsgReturn(pReq->header.size == sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
2310
2311 if (pReq->enmStatusType == VMMDevCpuStatusType_Disable)
2312 pThis->fCpuHotPlugEventsEnabled = false;
2313 else if (pReq->enmStatusType == VMMDevCpuStatusType_Enable)
2314 pThis->fCpuHotPlugEventsEnabled = true;
2315 else
2316 return VERR_INVALID_PARAMETER;
2317 return VINF_SUCCESS;
2318}
2319
2320
2321#ifdef DEBUG
2322/**
2323 * Handles VMMDevReq_LogString.
2324 *
2325 * @returns VBox status code that the guest should see.
2326 * @param pReqHdr The header of the request to handle.
2327 */
2328static int vmmdevReqHandler_LogString(VMMDevRequestHeader *pReqHdr)
2329{
2330 VMMDevReqLogString *pReq = (VMMDevReqLogString *)pReqHdr;
2331 AssertMsgReturn(pReq->header.size >= sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
2332 AssertMsgReturn(pReq->szString[pReq->header.size - RT_UOFFSETOF(VMMDevReqLogString, szString) - 1] == '\0',
2333 ("not null terminated\n"), VERR_INVALID_PARAMETER);
2334
2335 LogIt(RTLOGGRPFLAGS_LEVEL_1, LOG_GROUP_DEV_VMM_BACKDOOR, ("DEBUG LOG: %s", pReq->szString));
2336 return VINF_SUCCESS;
2337}
2338#endif /* DEBUG */
2339
2340/**
2341 * Handles VMMDevReq_GetSessionId.
2342 *
2343 * Get a unique "session" ID for this VM, where the ID will be different after each
2344 * start, reset or restore of the VM. This can be used for restore detection
2345 * inside the guest.
2346 *
2347 * @returns VBox status code that the guest should see.
2348 * @param pThis The VMMDev instance data.
2349 * @param pReqHdr The header of the request to handle.
2350 */
2351static int vmmdevReqHandler_GetSessionId(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
2352{
2353 VMMDevReqSessionId *pReq = (VMMDevReqSessionId *)pReqHdr;
2354 AssertMsgReturn(pReq->header.size == sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
2355
2356 pReq->idSession = pThis->idSession;
2357 return VINF_SUCCESS;
2358}
2359
2360
2361#ifdef VBOX_WITH_PAGE_SHARING
2362
2363/**
2364 * Handles VMMDevReq_RegisterSharedModule.
2365 *
2366 * @returns VBox status code that the guest should see.
2367 * @param pThis The VMMDev instance data.
2368 * @param pReqHdr The header of the request to handle.
2369 */
2370static int vmmdevReqHandler_RegisterSharedModule(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
2371{
2372 /*
2373 * Basic input validation (more done by GMM).
2374 */
2375 VMMDevSharedModuleRegistrationRequest *pReq = (VMMDevSharedModuleRegistrationRequest *)pReqHdr;
2376 AssertMsgReturn(pReq->header.size >= sizeof(VMMDevSharedModuleRegistrationRequest),
2377 ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
2378 AssertMsgReturn(pReq->header.size == RT_UOFFSETOF_DYN(VMMDevSharedModuleRegistrationRequest, aRegions[pReq->cRegions]),
2379 ("%u cRegions=%u\n", pReq->header.size, pReq->cRegions), VERR_INVALID_PARAMETER);
2380
2381 AssertReturn(RTStrEnd(pReq->szName, sizeof(pReq->szName)), VERR_INVALID_PARAMETER);
2382 AssertReturn(RTStrEnd(pReq->szVersion, sizeof(pReq->szVersion)), VERR_INVALID_PARAMETER);
2383 int rc = RTStrValidateEncoding(pReq->szName);
2384 AssertRCReturn(rc, rc);
2385 rc = RTStrValidateEncoding(pReq->szVersion);
2386 AssertRCReturn(rc, rc);
2387
2388 /*
2389 * Forward the request to the VMM.
2390 */
2391 return PGMR3SharedModuleRegister(PDMDevHlpGetVM(pThis->pDevIns), pReq->enmGuestOS, pReq->szName, pReq->szVersion,
2392 pReq->GCBaseAddr, pReq->cbModule, pReq->cRegions, pReq->aRegions);
2393}
2394
2395/**
2396 * Handles VMMDevReq_UnregisterSharedModule.
2397 *
2398 * @returns VBox status code that the guest should see.
2399 * @param pThis The VMMDev instance data.
2400 * @param pReqHdr The header of the request to handle.
2401 */
2402static int vmmdevReqHandler_UnregisterSharedModule(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
2403{
2404 /*
2405 * Basic input validation.
2406 */
2407 VMMDevSharedModuleUnregistrationRequest *pReq = (VMMDevSharedModuleUnregistrationRequest *)pReqHdr;
2408 AssertMsgReturn(pReq->header.size == sizeof(VMMDevSharedModuleUnregistrationRequest),
2409 ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
2410
2411 AssertReturn(RTStrEnd(pReq->szName, sizeof(pReq->szName)), VERR_INVALID_PARAMETER);
2412 AssertReturn(RTStrEnd(pReq->szVersion, sizeof(pReq->szVersion)), VERR_INVALID_PARAMETER);
2413 int rc = RTStrValidateEncoding(pReq->szName);
2414 AssertRCReturn(rc, rc);
2415 rc = RTStrValidateEncoding(pReq->szVersion);
2416 AssertRCReturn(rc, rc);
2417
2418 /*
2419 * Forward the request to the VMM.
2420 */
2421 return PGMR3SharedModuleUnregister(PDMDevHlpGetVM(pThis->pDevIns), pReq->szName, pReq->szVersion,
2422 pReq->GCBaseAddr, pReq->cbModule);
2423}
2424
2425/**
2426 * Handles VMMDevReq_CheckSharedModules.
2427 *
2428 * @returns VBox status code that the guest should see.
2429 * @param pThis The VMMDev instance data.
2430 * @param pReqHdr The header of the request to handle.
2431 */
2432static int vmmdevReqHandler_CheckSharedModules(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
2433{
2434 VMMDevSharedModuleCheckRequest *pReq = (VMMDevSharedModuleCheckRequest *)pReqHdr;
2435 AssertMsgReturn(pReq->header.size == sizeof(VMMDevSharedModuleCheckRequest),
2436 ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
2437 return PGMR3SharedModuleCheckAll(PDMDevHlpGetVM(pThis->pDevIns));
2438}
2439
2440/**
2441 * Handles VMMDevReq_GetPageSharingStatus.
2442 *
2443 * @returns VBox status code that the guest should see.
2444 * @param pThis The VMMDev instance data.
2445 * @param pReqHdr The header of the request to handle.
2446 */
2447static int vmmdevReqHandler_GetPageSharingStatus(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
2448{
2449 VMMDevPageSharingStatusRequest *pReq = (VMMDevPageSharingStatusRequest *)pReqHdr;
2450 AssertMsgReturn(pReq->header.size == sizeof(VMMDevPageSharingStatusRequest),
2451 ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
2452
2453 pReq->fEnabled = false;
2454 int rc = pThis->pDrv->pfnIsPageFusionEnabled(pThis->pDrv, &pReq->fEnabled);
2455 if (RT_FAILURE(rc))
2456 pReq->fEnabled = false;
2457 return VINF_SUCCESS;
2458}
2459
2460
2461/**
2462 * Handles VMMDevReq_DebugIsPageShared.
2463 *
2464 * @returns VBox status code that the guest should see.
2465 * @param pThis The VMMDev instance data.
2466 * @param pReqHdr The header of the request to handle.
2467 */
2468static int vmmdevReqHandler_DebugIsPageShared(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
2469{
2470 VMMDevPageIsSharedRequest *pReq = (VMMDevPageIsSharedRequest *)pReqHdr;
2471 AssertMsgReturn(pReq->header.size == sizeof(VMMDevPageIsSharedRequest),
2472 ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
2473
2474# ifdef DEBUG
2475 return PGMR3SharedModuleGetPageState(PDMDevHlpGetVM(pThis->pDevIns), pReq->GCPtrPage, &pReq->fShared, &pReq->uPageFlags);
2476# else
2477 RT_NOREF1(pThis);
2478 return VERR_NOT_IMPLEMENTED;
2479# endif
2480}
2481
2482#endif /* VBOX_WITH_PAGE_SHARING */
2483
2484
2485/**
2486 * Handles VMMDevReq_WriteCoreDumpe
2487 *
2488 * @returns VBox status code that the guest should see.
2489 * @param pThis The VMMDev instance data.
2490 * @param pReqHdr Pointer to the request header.
2491 */
2492static int vmmdevReqHandler_WriteCoreDump(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
2493{
2494 VMMDevReqWriteCoreDump *pReq = (VMMDevReqWriteCoreDump *)pReqHdr;
2495 AssertMsgReturn(pReq->header.size == sizeof(VMMDevReqWriteCoreDump), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
2496
2497 /*
2498 * Only available if explicitly enabled by the user.
2499 */
2500 if (!pThis->fGuestCoreDumpEnabled)
2501 return VERR_ACCESS_DENIED;
2502
2503 /*
2504 * User makes sure the directory exists before composing the path.
2505 */
2506 if (!RTDirExists(pThis->szGuestCoreDumpDir))
2507 return VERR_PATH_NOT_FOUND;
2508
2509 char szCorePath[RTPATH_MAX];
2510 RTStrCopy(szCorePath, sizeof(szCorePath), pThis->szGuestCoreDumpDir);
2511 RTPathAppend(szCorePath, sizeof(szCorePath), "VBox.core");
2512
2513 /*
2514 * Rotate existing cores based on number of additional cores to keep around.
2515 */
2516 if (pThis->cGuestCoreDumps > 0)
2517 for (int64_t i = pThis->cGuestCoreDumps - 1; i >= 0; i--)
2518 {
2519 char szFilePathOld[RTPATH_MAX];
2520 if (i == 0)
2521 RTStrCopy(szFilePathOld, sizeof(szFilePathOld), szCorePath);
2522 else
2523 RTStrPrintf(szFilePathOld, sizeof(szFilePathOld), "%s.%lld", szCorePath, i);
2524
2525 char szFilePathNew[RTPATH_MAX];
2526 RTStrPrintf(szFilePathNew, sizeof(szFilePathNew), "%s.%lld", szCorePath, i + 1);
2527 int vrc = RTFileMove(szFilePathOld, szFilePathNew, RTFILEMOVE_FLAGS_REPLACE);
2528 if (vrc == VERR_FILE_NOT_FOUND)
2529 RTFileDelete(szFilePathNew);
2530 }
2531
2532 /*
2533 * Write the core file.
2534 */
2535 PUVM pUVM = PDMDevHlpGetUVM(pThis->pDevIns);
2536 return DBGFR3CoreWrite(pUVM, szCorePath, true /*fReplaceFile*/);
2537}
2538
2539
2540/**
2541 * Dispatch the request to the appropriate handler function.
2542 *
2543 * @returns Port I/O handler exit code.
2544 * @param pThis The VMM device instance data.
2545 * @param pReqHdr The request header (cached in host memory).
2546 * @param GCPhysReqHdr The guest physical address of the request (for
2547 * HGCM).
2548 * @param pfDelayedUnlock Where to indicate whether the critical section exit
2549 * needs to be delayed till after the request has been
2550 * written back. This is a HGCM kludge, see critsect
2551 * work in hgcmCompletedWorker for more details.
2552 */
2553static int vmmdevReqDispatcher(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr, RTGCPHYS GCPhysReqHdr, bool *pfDelayedUnlock)
2554{
2555 int rcRet = VINF_SUCCESS;
2556 *pfDelayedUnlock = false;
2557
2558 switch (pReqHdr->requestType)
2559 {
2560 case VMMDevReq_ReportGuestInfo:
2561 pReqHdr->rc = vmmdevReqHandler_ReportGuestInfo(pThis, pReqHdr);
2562 break;
2563
2564 case VMMDevReq_ReportGuestInfo2:
2565 pReqHdr->rc = vmmdevReqHandler_ReportGuestInfo2(pThis, pReqHdr);
2566 break;
2567
2568 case VMMDevReq_ReportGuestStatus:
2569 pReqHdr->rc = vmmdevReqHandler_ReportGuestStatus(pThis, pReqHdr);
2570 break;
2571
2572 case VMMDevReq_ReportGuestUserState:
2573 pReqHdr->rc = vmmdevReqHandler_ReportGuestUserState(pThis, pReqHdr);
2574 break;
2575
2576 case VMMDevReq_ReportGuestCapabilities:
2577 pReqHdr->rc = vmmdevReqHandler_ReportGuestCapabilities(pThis, pReqHdr);
2578 break;
2579
2580 case VMMDevReq_SetGuestCapabilities:
2581 pReqHdr->rc = vmmdevReqHandler_SetGuestCapabilities(pThis, pReqHdr);
2582 break;
2583
2584 case VMMDevReq_WriteCoreDump:
2585 pReqHdr->rc = vmmdevReqHandler_WriteCoreDump(pThis, pReqHdr);
2586 break;
2587
2588 case VMMDevReq_GetMouseStatus:
2589 pReqHdr->rc = vmmdevReqHandler_GetMouseStatus(pThis, pReqHdr);
2590 break;
2591
2592 case VMMDevReq_SetMouseStatus:
2593 pReqHdr->rc = vmmdevReqHandler_SetMouseStatus(pThis, pReqHdr);
2594 break;
2595
2596 case VMMDevReq_SetPointerShape:
2597 pReqHdr->rc = vmmdevReqHandler_SetPointerShape(pThis, pReqHdr);
2598 break;
2599
2600 case VMMDevReq_GetHostTime:
2601 pReqHdr->rc = vmmdevReqHandler_GetHostTime(pThis, pReqHdr);
2602 break;
2603
2604 case VMMDevReq_GetHypervisorInfo:
2605 pReqHdr->rc = vmmdevReqHandler_GetHypervisorInfo(pThis, pReqHdr);
2606 break;
2607
2608 case VMMDevReq_SetHypervisorInfo:
2609 pReqHdr->rc = vmmdevReqHandler_SetHypervisorInfo(pThis, pReqHdr);
2610 break;
2611
2612 case VMMDevReq_RegisterPatchMemory:
2613 pReqHdr->rc = vmmdevReqHandler_RegisterPatchMemory(pThis, pReqHdr);
2614 break;
2615
2616 case VMMDevReq_DeregisterPatchMemory:
2617 pReqHdr->rc = vmmdevReqHandler_DeregisterPatchMemory(pThis, pReqHdr);
2618 break;
2619
2620 case VMMDevReq_SetPowerStatus:
2621 {
2622 int rc = pReqHdr->rc = vmmdevReqHandler_SetPowerStatus(pThis, pReqHdr);
2623 if (rc != VINF_SUCCESS && RT_SUCCESS(rc))
2624 rcRet = rc;
2625 break;
2626 }
2627
2628 case VMMDevReq_GetDisplayChangeRequest:
2629 pReqHdr->rc = vmmdevReqHandler_GetDisplayChangeRequest(pThis, pReqHdr);
2630 break;
2631
2632 case VMMDevReq_GetDisplayChangeRequest2:
2633 pReqHdr->rc = vmmdevReqHandler_GetDisplayChangeRequest2(pThis, pReqHdr);
2634 break;
2635
2636 case VMMDevReq_GetDisplayChangeRequestEx:
2637 pReqHdr->rc = vmmdevReqHandler_GetDisplayChangeRequestEx(pThis, pReqHdr);
2638 break;
2639
2640 case VMMDevReq_GetDisplayChangeRequestMulti:
2641 pReqHdr->rc = vmmdevReqHandler_GetDisplayChangeRequestMulti(pThis, pReqHdr);
2642 break;
2643
2644 case VMMDevReq_VideoModeSupported:
2645 pReqHdr->rc = vmmdevReqHandler_VideoModeSupported(pThis, pReqHdr);
2646 break;
2647
2648 case VMMDevReq_VideoModeSupported2:
2649 pReqHdr->rc = vmmdevReqHandler_VideoModeSupported2(pThis, pReqHdr);
2650 break;
2651
2652 case VMMDevReq_GetHeightReduction:
2653 pReqHdr->rc = vmmdevReqHandler_GetHeightReduction(pThis, pReqHdr);
2654 break;
2655
2656 case VMMDevReq_AcknowledgeEvents:
2657 pReqHdr->rc = vmmdevReqHandler_AcknowledgeEvents(pThis, pReqHdr);
2658 break;
2659
2660 case VMMDevReq_CtlGuestFilterMask:
2661 pReqHdr->rc = vmmdevReqHandler_CtlGuestFilterMask(pThis, pReqHdr);
2662 break;
2663
2664#ifdef VBOX_WITH_HGCM
2665 case VMMDevReq_HGCMConnect:
2666 pReqHdr->rc = vmmdevReqHandler_HGCMConnect(pThis, pReqHdr, GCPhysReqHdr);
2667 *pfDelayedUnlock = true;
2668 break;
2669
2670 case VMMDevReq_HGCMDisconnect:
2671 pReqHdr->rc = vmmdevReqHandler_HGCMDisconnect(pThis, pReqHdr, GCPhysReqHdr);
2672 *pfDelayedUnlock = true;
2673 break;
2674
2675# ifdef VBOX_WITH_64_BITS_GUESTS
2676 case VMMDevReq_HGCMCall32:
2677 case VMMDevReq_HGCMCall64:
2678# else
2679 case VMMDevReq_HGCMCall:
2680# endif /* VBOX_WITH_64_BITS_GUESTS */
2681 pReqHdr->rc = vmmdevReqHandler_HGCMCall(pThis, pReqHdr, GCPhysReqHdr);
2682 *pfDelayedUnlock = true;
2683 break;
2684
2685 case VMMDevReq_HGCMCancel:
2686 pReqHdr->rc = vmmdevReqHandler_HGCMCancel(pThis, pReqHdr, GCPhysReqHdr);
2687 *pfDelayedUnlock = true;
2688 break;
2689
2690 case VMMDevReq_HGCMCancel2:
2691 pReqHdr->rc = vmmdevReqHandler_HGCMCancel2(pThis, pReqHdr);
2692 break;
2693#endif /* VBOX_WITH_HGCM */
2694
2695 case VMMDevReq_VideoAccelEnable:
2696 pReqHdr->rc = vmmdevReqHandler_VideoAccelEnable(pThis, pReqHdr);
2697 break;
2698
2699 case VMMDevReq_VideoAccelFlush:
2700 pReqHdr->rc = vmmdevReqHandler_VideoAccelFlush(pThis, pReqHdr);
2701 break;
2702
2703 case VMMDevReq_VideoSetVisibleRegion:
2704 pReqHdr->rc = vmmdevReqHandler_VideoSetVisibleRegion(pThis, pReqHdr);
2705 break;
2706
2707 case VMMDevReq_GetSeamlessChangeRequest:
2708 pReqHdr->rc = vmmdevReqHandler_GetSeamlessChangeRequest(pThis, pReqHdr);
2709 break;
2710
2711 case VMMDevReq_GetVRDPChangeRequest:
2712 pReqHdr->rc = vmmdevReqHandler_GetVRDPChangeRequest(pThis, pReqHdr);
2713 break;
2714
2715 case VMMDevReq_GetMemBalloonChangeRequest:
2716 pReqHdr->rc = vmmdevReqHandler_GetMemBalloonChangeRequest(pThis, pReqHdr);
2717 break;
2718
2719 case VMMDevReq_ChangeMemBalloon:
2720 pReqHdr->rc = vmmdevReqHandler_ChangeMemBalloon(pThis, pReqHdr);
2721 break;
2722
2723 case VMMDevReq_GetStatisticsChangeRequest:
2724 pReqHdr->rc = vmmdevReqHandler_GetStatisticsChangeRequest(pThis, pReqHdr);
2725 break;
2726
2727 case VMMDevReq_ReportGuestStats:
2728 pReqHdr->rc = vmmdevReqHandler_ReportGuestStats(pThis, pReqHdr);
2729 break;
2730
2731 case VMMDevReq_QueryCredentials:
2732 pReqHdr->rc = vmmdevReqHandler_QueryCredentials(pThis, pReqHdr);
2733 break;
2734
2735 case VMMDevReq_ReportCredentialsJudgement:
2736 pReqHdr->rc = vmmdevReqHandler_ReportCredentialsJudgement(pThis, pReqHdr);
2737 break;
2738
2739 case VMMDevReq_GetHostVersion:
2740 pReqHdr->rc = vmmdevReqHandler_GetHostVersion(pReqHdr);
2741 break;
2742
2743 case VMMDevReq_GetCpuHotPlugRequest:
2744 pReqHdr->rc = vmmdevReqHandler_GetCpuHotPlugRequest(pThis, pReqHdr);
2745 break;
2746
2747 case VMMDevReq_SetCpuHotPlugStatus:
2748 pReqHdr->rc = vmmdevReqHandler_SetCpuHotPlugStatus(pThis, pReqHdr);
2749 break;
2750
2751#ifdef VBOX_WITH_PAGE_SHARING
2752 case VMMDevReq_RegisterSharedModule:
2753 pReqHdr->rc = vmmdevReqHandler_RegisterSharedModule(pThis, pReqHdr);
2754 break;
2755
2756 case VMMDevReq_UnregisterSharedModule:
2757 pReqHdr->rc = vmmdevReqHandler_UnregisterSharedModule(pThis, pReqHdr);
2758 break;
2759
2760 case VMMDevReq_CheckSharedModules:
2761 pReqHdr->rc = vmmdevReqHandler_CheckSharedModules(pThis, pReqHdr);
2762 break;
2763
2764 case VMMDevReq_GetPageSharingStatus:
2765 pReqHdr->rc = vmmdevReqHandler_GetPageSharingStatus(pThis, pReqHdr);
2766 break;
2767
2768 case VMMDevReq_DebugIsPageShared:
2769 pReqHdr->rc = vmmdevReqHandler_DebugIsPageShared(pThis, pReqHdr);
2770 break;
2771
2772#endif /* VBOX_WITH_PAGE_SHARING */
2773
2774#ifdef DEBUG
2775 case VMMDevReq_LogString:
2776 pReqHdr->rc = vmmdevReqHandler_LogString(pReqHdr);
2777 break;
2778#endif
2779
2780 case VMMDevReq_GetSessionId:
2781 pReqHdr->rc = vmmdevReqHandler_GetSessionId(pThis, pReqHdr);
2782 break;
2783
2784 /*
2785 * Guest wants to give up a timeslice.
2786 * Note! This was only ever used by experimental GAs!
2787 */
2788 /** @todo maybe we could just remove this? */
2789 case VMMDevReq_Idle:
2790 {
2791 /* just return to EMT telling it that we want to halt */
2792 rcRet = VINF_EM_HALT;
2793 break;
2794 }
2795
2796 case VMMDevReq_GuestHeartbeat:
2797 pReqHdr->rc = vmmDevReqHandler_GuestHeartbeat(pThis);
2798 break;
2799
2800 case VMMDevReq_HeartbeatConfigure:
2801 pReqHdr->rc = vmmDevReqHandler_HeartbeatConfigure(pThis, pReqHdr);
2802 break;
2803
2804 case VMMDevReq_NtBugCheck:
2805 pReqHdr->rc = vmmDevReqHandler_NtBugCheck(pThis, pReqHdr);
2806 break;
2807
2808 default:
2809 {
2810 pReqHdr->rc = VERR_NOT_IMPLEMENTED;
2811 Log(("VMMDev unknown request type %d\n", pReqHdr->requestType));
2812 break;
2813 }
2814 }
2815 return rcRet;
2816}
2817
2818
2819/**
2820 * @callback_method_impl{FNIOMIOPORTOUT, Port I/O Handler for the generic
2821 * request interface.}
2822 */
2823static DECLCALLBACK(int) vmmdevRequestHandler(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
2824{
2825 RT_NOREF2(Port, cb);
2826 PVMMDEV pThis = (VMMDevState*)pvUser;
2827
2828 /*
2829 * The caller has passed the guest context physical address of the request
2830 * structure. We'll copy all of it into a heap buffer eventually, but we
2831 * will have to start off with the header.
2832 */
2833 VMMDevRequestHeader requestHeader;
2834 RT_ZERO(requestHeader);
2835 PDMDevHlpPhysRead(pDevIns, (RTGCPHYS)u32, &requestHeader, sizeof(requestHeader));
2836
2837 /* The structure size must be greater or equal to the header size. */
2838 if (requestHeader.size < sizeof(VMMDevRequestHeader))
2839 {
2840 Log(("VMMDev request header size too small! size = %d\n", requestHeader.size));
2841 return VINF_SUCCESS;
2842 }
2843
2844 /* Check the version of the header structure. */
2845 if (requestHeader.version != VMMDEV_REQUEST_HEADER_VERSION)
2846 {
2847 Log(("VMMDev: guest header version (0x%08X) differs from ours (0x%08X)\n", requestHeader.version, VMMDEV_REQUEST_HEADER_VERSION));
2848 return VINF_SUCCESS;
2849 }
2850
2851 Log2(("VMMDev request issued: %d\n", requestHeader.requestType));
2852
2853 int rcRet = VINF_SUCCESS;
2854 bool fDelayedUnlock = false;
2855 VMMDevRequestHeader *pRequestHeader = NULL;
2856
2857 /* Check that is doesn't exceed the max packet size. */
2858 if (requestHeader.size <= VMMDEV_MAX_VMMDEVREQ_SIZE)
2859 {
2860 /*
2861 * We require the GAs to report it's information before we let it have
2862 * access to all the functions. The VMMDevReq_ReportGuestInfo request
2863 * is the one which unlocks the access. Newer additions will first
2864 * issue VMMDevReq_ReportGuestInfo2, older ones doesn't know this one.
2865 * Two exceptions: VMMDevReq_GetHostVersion and VMMDevReq_WriteCoreDump.
2866 */
2867 if ( pThis->fu32AdditionsOk
2868 || requestHeader.requestType == VMMDevReq_ReportGuestInfo2
2869 || requestHeader.requestType == VMMDevReq_ReportGuestInfo
2870 || requestHeader.requestType == VMMDevReq_WriteCoreDump
2871 || requestHeader.requestType == VMMDevReq_GetHostVersion
2872 )
2873 {
2874 /*
2875 * The request looks fine. Allocate a heap block for it, read the
2876 * entire package from guest memory and feed it to the dispatcher.
2877 */
2878 pRequestHeader = (VMMDevRequestHeader *)RTMemAlloc(requestHeader.size);
2879 if (pRequestHeader)
2880 {
2881 memcpy(pRequestHeader, &requestHeader, sizeof(VMMDevRequestHeader));
2882 size_t cbLeft = requestHeader.size - sizeof(VMMDevRequestHeader);
2883 if (cbLeft)
2884 PDMDevHlpPhysRead(pDevIns,
2885 (RTGCPHYS)u32 + sizeof(VMMDevRequestHeader),
2886 (uint8_t *)pRequestHeader + sizeof(VMMDevRequestHeader),
2887 cbLeft);
2888
2889 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
2890 rcRet = vmmdevReqDispatcher(pThis, pRequestHeader, u32, &fDelayedUnlock);
2891 if (!fDelayedUnlock)
2892 PDMCritSectLeave(&pThis->CritSect);
2893 }
2894 else
2895 {
2896 Log(("VMMDev: RTMemAlloc failed!\n"));
2897 requestHeader.rc = VERR_NO_MEMORY;
2898 }
2899 }
2900 else
2901 {
2902 LogRelMax(10, ("VMMDev: Guest has not yet reported to us -- refusing operation of request #%d\n",
2903 requestHeader.requestType));
2904 requestHeader.rc = VERR_NOT_SUPPORTED;
2905 }
2906 }
2907 else
2908 {
2909 LogRelMax(50, ("VMMDev: Request packet too big (%x), refusing operation\n", requestHeader.size));
2910 requestHeader.rc = VERR_NOT_SUPPORTED;
2911 }
2912
2913 /*
2914 * Write the result back to guest memory
2915 */
2916 if (pRequestHeader)
2917 {
2918 PDMDevHlpPhysWrite(pDevIns, u32, pRequestHeader, pRequestHeader->size);
2919 if (fDelayedUnlock)
2920 PDMCritSectLeave(&pThis->CritSect);
2921 RTMemFree(pRequestHeader);
2922 }
2923 else
2924 {
2925 /* early error case; write back header only */
2926 PDMDevHlpPhysWrite(pDevIns, u32, &requestHeader, sizeof(requestHeader));
2927 Assert(!fDelayedUnlock);
2928 }
2929
2930 return rcRet;
2931}
2932
2933
2934/* -=-=-=-=-=- PCI Device -=-=-=-=-=- */
2935
2936
2937/**
2938 * @callback_method_impl{FNPCIIOREGIONMAP,MMIO/MMIO2 regions}
2939 */
2940static DECLCALLBACK(int) vmmdevIORAMRegionMap(PPDMDEVINS pDevIns, PPDMPCIDEV pPciDev, uint32_t iRegion,
2941 RTGCPHYS GCPhysAddress, RTGCPHYS cb, PCIADDRESSSPACE enmType)
2942{
2943 RT_NOREF1(cb);
2944 LogFlow(("vmmdevR3IORAMRegionMap: iRegion=%d GCPhysAddress=%RGp cb=%RGp enmType=%d\n", iRegion, GCPhysAddress, cb, enmType));
2945 PVMMDEV pThis = RT_FROM_MEMBER(pPciDev, VMMDEV, PciDev);
2946 int rc;
2947
2948 if (iRegion == 1)
2949 {
2950 AssertReturn(enmType == PCI_ADDRESS_SPACE_MEM, VERR_INTERNAL_ERROR);
2951 Assert(pThis->pVMMDevRAMR3 != NULL);
2952 if (GCPhysAddress != NIL_RTGCPHYS)
2953 {
2954 /*
2955 * Map the MMIO2 memory.
2956 */
2957 pThis->GCPhysVMMDevRAM = GCPhysAddress;
2958 Assert(pThis->GCPhysVMMDevRAM == GCPhysAddress);
2959 rc = PDMDevHlpMMIOExMap(pDevIns, pPciDev, iRegion, GCPhysAddress);
2960 }
2961 else
2962 {
2963 /*
2964 * It is about to be unmapped, just clean up.
2965 */
2966 pThis->GCPhysVMMDevRAM = NIL_RTGCPHYS32;
2967 rc = VINF_SUCCESS;
2968 }
2969 }
2970 else if (iRegion == 2)
2971 {
2972 AssertReturn(enmType == PCI_ADDRESS_SPACE_MEM_PREFETCH, VERR_INTERNAL_ERROR);
2973 Assert(pThis->pVMMDevHeapR3 != NULL);
2974 if (GCPhysAddress != NIL_RTGCPHYS)
2975 {
2976 /*
2977 * Map the MMIO2 memory.
2978 */
2979 pThis->GCPhysVMMDevHeap = GCPhysAddress;
2980 Assert(pThis->GCPhysVMMDevHeap == GCPhysAddress);
2981 rc = PDMDevHlpMMIOExMap(pDevIns, pPciDev, iRegion, GCPhysAddress);
2982 if (RT_SUCCESS(rc))
2983 rc = PDMDevHlpRegisterVMMDevHeap(pDevIns, GCPhysAddress, pThis->pVMMDevHeapR3, VMMDEV_HEAP_SIZE);
2984 }
2985 else
2986 {
2987 /*
2988 * It is about to be unmapped, just clean up.
2989 */
2990 PDMDevHlpRegisterVMMDevHeap(pDevIns, NIL_RTGCPHYS, pThis->pVMMDevHeapR3, VMMDEV_HEAP_SIZE);
2991 pThis->GCPhysVMMDevHeap = NIL_RTGCPHYS32;
2992 rc = VINF_SUCCESS;
2993 }
2994 }
2995 else
2996 {
2997 AssertMsgFailed(("%d\n", iRegion));
2998 rc = VERR_INVALID_PARAMETER;
2999 }
3000
3001 return rc;
3002}
3003
3004
3005/**
3006 * @callback_method_impl{FNPCIIOREGIONMAP,I/O Port Region}
3007 */
3008static DECLCALLBACK(int) vmmdevIOPortRegionMap(PPDMDEVINS pDevIns, PPDMPCIDEV pPciDev, uint32_t iRegion,
3009 RTGCPHYS GCPhysAddress, RTGCPHYS cb, PCIADDRESSSPACE enmType)
3010{
3011 LogFlow(("vmmdevIOPortRegionMap: iRegion=%d GCPhysAddress=%RGp cb=%RGp enmType=%d\n", iRegion, GCPhysAddress, cb, enmType));
3012 RT_NOREF3(iRegion, cb, enmType);
3013 PVMMDEV pThis = RT_FROM_MEMBER(pPciDev, VMMDEV, PciDev);
3014
3015 Assert(enmType == PCI_ADDRESS_SPACE_IO);
3016 Assert(iRegion == 0);
3017 AssertMsg(RT_ALIGN(GCPhysAddress, 8) == GCPhysAddress, ("Expected 8 byte alignment. GCPhysAddress=%#x\n", GCPhysAddress));
3018
3019 /*
3020 * Register our port IO handlers.
3021 */
3022 int rc = PDMDevHlpIOPortRegister(pDevIns, (RTIOPORT)GCPhysAddress + VMMDEV_PORT_OFF_REQUEST, 1,
3023 pThis, vmmdevRequestHandler, NULL, NULL, NULL, "VMMDev Request Handler");
3024 AssertRC(rc);
3025 return rc;
3026}
3027
3028
3029
3030/* -=-=-=-=-=- Backdoor Logging and Time Sync. -=-=-=-=-=- */
3031
3032/**
3033 * @callback_method_impl{FNIOMIOPORTOUT, Backdoor Logging.}
3034 */
3035static DECLCALLBACK(int) vmmdevBackdoorLog(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
3036{
3037 RT_NOREF1(pvUser);
3038 PVMMDEV pThis = PDMINS_2_DATA(pDevIns, VMMDevState *);
3039
3040 if (!pThis->fBackdoorLogDisabled && cb == 1 && Port == RTLOG_DEBUG_PORT)
3041 {
3042
3043 /* The raw version. */
3044 switch (u32)
3045 {
3046 case '\r': LogIt(RTLOGGRPFLAGS_LEVEL_2, LOG_GROUP_DEV_VMM_BACKDOOR, ("vmmdev: <return>\n")); break;
3047 case '\n': LogIt(RTLOGGRPFLAGS_LEVEL_2, LOG_GROUP_DEV_VMM_BACKDOOR, ("vmmdev: <newline>\n")); break;
3048 case '\t': LogIt(RTLOGGRPFLAGS_LEVEL_2, LOG_GROUP_DEV_VMM_BACKDOOR, ("vmmdev: <tab>\n")); break;
3049 default: LogIt(RTLOGGRPFLAGS_LEVEL_2, LOG_GROUP_DEV_VMM_BACKDOOR, ("vmmdev: %c (%02x)\n", u32, u32)); break;
3050 }
3051
3052 /* The readable, buffered version. */
3053 if (u32 == '\n' || u32 == '\r')
3054 {
3055 pThis->szMsg[pThis->iMsg] = '\0';
3056 if (pThis->iMsg)
3057 LogRelIt(RTLOGGRPFLAGS_LEVEL_1, LOG_GROUP_DEV_VMM_BACKDOOR, ("VMMDev: Guest Log: %s\n", pThis->szMsg));
3058 pThis->iMsg = 0;
3059 }
3060 else
3061 {
3062 if (pThis->iMsg >= sizeof(pThis->szMsg)-1)
3063 {
3064 pThis->szMsg[pThis->iMsg] = '\0';
3065 LogRelIt(RTLOGGRPFLAGS_LEVEL_1, LOG_GROUP_DEV_VMM_BACKDOOR, ("VMMDev: Guest Log: %s\n", pThis->szMsg));
3066 pThis->iMsg = 0;
3067 }
3068 pThis->szMsg[pThis->iMsg] = (char )u32;
3069 pThis->szMsg[++pThis->iMsg] = '\0';
3070 }
3071 }
3072 return VINF_SUCCESS;
3073}
3074
3075#ifdef VMMDEV_WITH_ALT_TIMESYNC
3076
3077/**
3078 * @callback_method_impl{FNIOMIOPORTOUT, Alternative time synchronization.}
3079 */
3080static DECLCALLBACK(int) vmmdevAltTimeSyncWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
3081{
3082 RT_NOREF2(pvUser, Port);
3083 PVMMDEV pThis = PDMINS_2_DATA(pDevIns, VMMDevState *);
3084 if (cb == 4)
3085 {
3086 /* Selects high (0) or low (1) DWORD. The high has to be read first. */
3087 switch (u32)
3088 {
3089 case 0:
3090 pThis->fTimesyncBackdoorLo = false;
3091 break;
3092 case 1:
3093 pThis->fTimesyncBackdoorLo = true;
3094 break;
3095 default:
3096 Log(("vmmdevAltTimeSyncWrite: Invalid access cb=%#x u32=%#x\n", cb, u32));
3097 break;
3098 }
3099 }
3100 else
3101 Log(("vmmdevAltTimeSyncWrite: Invalid access cb=%#x u32=%#x\n", cb, u32));
3102 return VINF_SUCCESS;
3103}
3104
3105/**
3106 * @callback_method_impl{FNIOMIOPORTOUT, Alternative time synchronization.}
3107 */
3108static DECLCALLBACK(int) vmmdevAltTimeSyncRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
3109{
3110 RT_NOREF2(pvUser, Port);
3111 PVMMDEV pThis = PDMINS_2_DATA(pDevIns, VMMDevState *);
3112 int rc;
3113 if (cb == 4)
3114 {
3115 if (pThis->fTimesyncBackdoorLo)
3116 *pu32 = (uint32_t)pThis->hostTime;
3117 else
3118 {
3119 /* Reading the high dword gets and saves the current time. */
3120 RTTIMESPEC Now;
3121 pThis->hostTime = RTTimeSpecGetMilli(PDMDevHlpTMUtcNow(pDevIns, &Now));
3122 *pu32 = (uint32_t)(pThis->hostTime >> 32);
3123 }
3124 rc = VINF_SUCCESS;
3125 }
3126 else
3127 {
3128 Log(("vmmdevAltTimeSyncRead: Invalid access cb=%#x\n", cb));
3129 rc = VERR_IOM_IOPORT_UNUSED;
3130 }
3131 return rc;
3132}
3133
3134#endif /* VMMDEV_WITH_ALT_TIMESYNC */
3135
3136
3137/* -=-=-=-=-=- IBase -=-=-=-=-=- */
3138
3139/**
3140 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
3141 */
3142static DECLCALLBACK(void *) vmmdevPortQueryInterface(PPDMIBASE pInterface, const char *pszIID)
3143{
3144 PVMMDEV pThis = RT_FROM_MEMBER(pInterface, VMMDEV, IBase);
3145
3146 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThis->IBase);
3147 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIVMMDEVPORT, &pThis->IPort);
3148#ifdef VBOX_WITH_HGCM
3149 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIHGCMPORT, &pThis->IHGCMPort);
3150#endif
3151 /* Currently only for shared folders. */
3152 PDMIBASE_RETURN_INTERFACE(pszIID, PDMILEDPORTS, &pThis->SharedFolders.ILeds);
3153 return NULL;
3154}
3155
3156
3157/* -=-=-=-=-=- ILeds -=-=-=-=-=- */
3158
3159/**
3160 * Gets the pointer to the status LED of a unit.
3161 *
3162 * @returns VBox status code.
3163 * @param pInterface Pointer to the interface structure containing the called function pointer.
3164 * @param iLUN The unit which status LED we desire.
3165 * @param ppLed Where to store the LED pointer.
3166 */
3167static DECLCALLBACK(int) vmmdevQueryStatusLed(PPDMILEDPORTS pInterface, unsigned iLUN, PPDMLED *ppLed)
3168{
3169 PVMMDEV pThis = RT_FROM_MEMBER(pInterface, VMMDEV, SharedFolders.ILeds);
3170 if (iLUN == 0) /* LUN 0 is shared folders */
3171 {
3172 *ppLed = &pThis->SharedFolders.Led;
3173 return VINF_SUCCESS;
3174 }
3175 return VERR_PDM_LUN_NOT_FOUND;
3176}
3177
3178
3179/* -=-=-=-=-=- PDMIVMMDEVPORT (VMMDEV::IPort) -=-=-=-=-=- */
3180
3181/**
3182 * @interface_method_impl{PDMIVMMDEVPORT,pfnQueryAbsoluteMouse}
3183 */
3184static DECLCALLBACK(int) vmmdevIPort_QueryAbsoluteMouse(PPDMIVMMDEVPORT pInterface, int32_t *pxAbs, int32_t *pyAbs)
3185{
3186 PVMMDEV pThis = RT_FROM_MEMBER(pInterface, VMMDEV, IPort);
3187
3188 /** @todo at the first sign of trouble in this area, just enter the critsect.
3189 * As indicated by the comment below, the atomic reads serves no real purpose
3190 * here since we can assume cache coherency protocoles and int32_t alignment
3191 * rules making sure we won't see a halfwritten value. */
3192 if (pxAbs)
3193 *pxAbs = ASMAtomicReadS32(&pThis->mouseXAbs); /* why the atomic read? */
3194 if (pyAbs)
3195 *pyAbs = ASMAtomicReadS32(&pThis->mouseYAbs);
3196
3197 return VINF_SUCCESS;
3198}
3199
3200/**
3201 * @interface_method_impl{PDMIVMMDEVPORT,pfnSetAbsoluteMouse}
3202 */
3203static DECLCALLBACK(int) vmmdevIPort_SetAbsoluteMouse(PPDMIVMMDEVPORT pInterface, int32_t xAbs, int32_t yAbs)
3204{
3205 PVMMDEV pThis = RT_FROM_MEMBER(pInterface, VMMDEV, IPort);
3206 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
3207
3208 if ( pThis->mouseXAbs != xAbs
3209 || pThis->mouseYAbs != yAbs)
3210 {
3211 Log2(("vmmdevIPort_SetAbsoluteMouse : settings absolute position to x = %d, y = %d\n", xAbs, yAbs));
3212 pThis->mouseXAbs = xAbs;
3213 pThis->mouseYAbs = yAbs;
3214 VMMDevNotifyGuest(pThis, VMMDEV_EVENT_MOUSE_POSITION_CHANGED);
3215 }
3216
3217 PDMCritSectLeave(&pThis->CritSect);
3218 return VINF_SUCCESS;
3219}
3220
3221/**
3222 * @interface_method_impl{PDMIVMMDEVPORT,pfnQueryMouseCapabilities}
3223 */
3224static DECLCALLBACK(int) vmmdevIPort_QueryMouseCapabilities(PPDMIVMMDEVPORT pInterface, uint32_t *pfCapabilities)
3225{
3226 PVMMDEV pThis = RT_FROM_MEMBER(pInterface, VMMDEV, IPort);
3227 AssertPtrReturn(pfCapabilities, VERR_INVALID_PARAMETER);
3228
3229 *pfCapabilities = pThis->mouseCapabilities;
3230 return VINF_SUCCESS;
3231}
3232
3233/**
3234 * @interface_method_impl{PDMIVMMDEVPORT,pfnUpdateMouseCapabilities}
3235 */
3236static DECLCALLBACK(int)
3237vmmdevIPort_UpdateMouseCapabilities(PPDMIVMMDEVPORT pInterface, uint32_t fCapsAdded, uint32_t fCapsRemoved)
3238{
3239 PVMMDEV pThis = RT_FROM_MEMBER(pInterface, VMMDEV, IPort);
3240 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
3241
3242 uint32_t fOldCaps = pThis->mouseCapabilities;
3243 pThis->mouseCapabilities &= ~(fCapsRemoved & VMMDEV_MOUSE_HOST_MASK);
3244 pThis->mouseCapabilities |= (fCapsAdded & VMMDEV_MOUSE_HOST_MASK)
3245 | VMMDEV_MOUSE_HOST_RECHECKS_NEEDS_HOST_CURSOR;
3246 bool fNotify = fOldCaps != pThis->mouseCapabilities;
3247
3248 LogRelFlow(("VMMDev: vmmdevIPort_UpdateMouseCapabilities: fCapsAdded=0x%x, fCapsRemoved=0x%x, fNotify=%RTbool\n", fCapsAdded,
3249 fCapsRemoved, fNotify));
3250
3251 if (fNotify)
3252 VMMDevNotifyGuest(pThis, VMMDEV_EVENT_MOUSE_CAPABILITIES_CHANGED);
3253
3254 PDMCritSectLeave(&pThis->CritSect);
3255 return VINF_SUCCESS;
3256}
3257
3258static bool vmmdevIsMonitorDefEqual(VMMDevDisplayDef const *pNew, VMMDevDisplayDef const *pOld)
3259{
3260 bool fEqual = pNew->idDisplay == pOld->idDisplay;
3261
3262 fEqual = fEqual && ( !RT_BOOL(pNew->fDisplayFlags & VMMDEV_DISPLAY_ORIGIN) /* No change. */
3263 || ( RT_BOOL(pOld->fDisplayFlags & VMMDEV_DISPLAY_ORIGIN) /* Old value exists and */
3264 && pNew->xOrigin == pOld->xOrigin /* the old is equal to the new. */
3265 && pNew->yOrigin == pOld->yOrigin));
3266
3267 fEqual = fEqual && ( !RT_BOOL(pNew->fDisplayFlags & VMMDEV_DISPLAY_CX)
3268 || ( RT_BOOL(pOld->fDisplayFlags & VMMDEV_DISPLAY_CX)
3269 && pNew->cx == pOld->cx));
3270
3271 fEqual = fEqual && ( !RT_BOOL(pNew->fDisplayFlags & VMMDEV_DISPLAY_CY)
3272 || ( RT_BOOL(pOld->fDisplayFlags & VMMDEV_DISPLAY_CY)
3273 && pNew->cy == pOld->cy));
3274
3275 fEqual = fEqual && ( !RT_BOOL(pNew->fDisplayFlags & VMMDEV_DISPLAY_BPP)
3276 || ( RT_BOOL(pOld->fDisplayFlags & VMMDEV_DISPLAY_BPP)
3277 && pNew->cBitsPerPixel == pOld->cBitsPerPixel));
3278
3279 fEqual = fEqual && ( RT_BOOL(pNew->fDisplayFlags & VMMDEV_DISPLAY_DISABLED)
3280 == RT_BOOL(pOld->fDisplayFlags & VMMDEV_DISPLAY_DISABLED));
3281
3282 fEqual = fEqual && ( RT_BOOL(pNew->fDisplayFlags & VMMDEV_DISPLAY_PRIMARY)
3283 == RT_BOOL(pOld->fDisplayFlags & VMMDEV_DISPLAY_PRIMARY));
3284
3285 return fEqual;
3286}
3287
3288/**
3289 * @interface_method_impl{PDMIVMMDEVPORT,pfnRequestDisplayChange}
3290 */
3291static DECLCALLBACK(int)
3292vmmdevIPort_RequestDisplayChange(PPDMIVMMDEVPORT pInterface, uint32_t cDisplays, VMMDevDisplayDef const *paDisplays, bool fForce)
3293{
3294 int rc = VINF_SUCCESS;
3295
3296 PVMMDEV pThis = RT_FROM_MEMBER(pInterface, VMMDEV, IPort);
3297 bool fNotifyGuest = false;
3298
3299 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
3300
3301 uint32_t i;
3302 for (i = 0; i < cDisplays; ++i)
3303 {
3304 VMMDevDisplayDef const *p = &paDisplays[i];
3305
3306 /* Either one display definition is provided or the display id must be equal to the array index. */
3307 AssertBreakStmt(cDisplays == 1 || p->idDisplay == i, rc = VERR_INVALID_PARAMETER);
3308 AssertBreakStmt(p->idDisplay < RT_ELEMENTS(pThis->displayChangeData.aRequests), rc = VERR_INVALID_PARAMETER);
3309
3310 DISPLAYCHANGEREQUEST *pRequest = &pThis->displayChangeData.aRequests[p->idDisplay];
3311
3312 VMMDevDisplayDef const *pLastRead = &pRequest->lastReadDisplayChangeRequest;
3313
3314 /* Verify that the new resolution is different and that guest does not yet know about it. */
3315 bool const fDifferentResolution = fForce || !vmmdevIsMonitorDefEqual(p, pLastRead);
3316
3317 LogFunc(("same=%d. New: %dx%d, cBits=%d, id=%d. Old: %dx%d, cBits=%d, id=%d. @%d,%d, Enabled=%d, ChangeOrigin=%d\n",
3318 !fDifferentResolution, p->cx, p->cy, p->cBitsPerPixel, p->idDisplay,
3319 pLastRead->cx, pLastRead->cy, pLastRead->cBitsPerPixel, pLastRead->idDisplay,
3320 p->xOrigin, p->yOrigin,
3321 !RT_BOOL(p->fDisplayFlags & VMMDEV_DISPLAY_DISABLED),
3322 RT_BOOL(p->fDisplayFlags & VMMDEV_DISPLAY_ORIGIN)));
3323
3324 /* We could validate the information here but hey, the guest can do that as well! */
3325 pRequest->displayChangeRequest = *p;
3326 pRequest->fPending = fDifferentResolution;
3327
3328 fNotifyGuest = fNotifyGuest || fDifferentResolution;
3329 }
3330
3331 if (RT_SUCCESS(rc))
3332 {
3333 if (fNotifyGuest)
3334 {
3335 for (i = 0; i < RT_ELEMENTS(pThis->displayChangeData.aRequests); ++i)
3336 {
3337 DISPLAYCHANGEREQUEST *pRequest = &pThis->displayChangeData.aRequests[i];
3338 if (pRequest->fPending)
3339 {
3340 VMMDevDisplayDef const *p = &pRequest->displayChangeRequest;
3341 LogRel(("VMMDev: SetVideoModeHint: Got a video mode hint (%dx%dx%d)@(%dx%d),(%d;%d) at %d\n",
3342 p->cx, p->cy, p->cBitsPerPixel, p->xOrigin, p->yOrigin,
3343 !RT_BOOL(p->fDisplayFlags & VMMDEV_DISPLAY_DISABLED),
3344 RT_BOOL(p->fDisplayFlags & VMMDEV_DISPLAY_ORIGIN), i));
3345 }
3346 }
3347
3348 /* IRQ so the guest knows what's going on */
3349 VMMDevNotifyGuest(pThis, VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST);
3350 }
3351 }
3352
3353 PDMCritSectLeave(&pThis->CritSect);
3354 return rc;
3355}
3356
3357/**
3358 * @interface_method_impl{PDMIVMMDEVPORT,pfnRequestSeamlessChange}
3359 */
3360static DECLCALLBACK(int) vmmdevIPort_RequestSeamlessChange(PPDMIVMMDEVPORT pInterface, bool fEnabled)
3361{
3362 PVMMDEV pThis = RT_FROM_MEMBER(pInterface, VMMDEV, IPort);
3363 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
3364
3365 /* Verify that the new resolution is different and that guest does not yet know about it. */
3366 bool fSameMode = (pThis->fLastSeamlessEnabled == fEnabled);
3367
3368 Log(("vmmdevIPort_RequestSeamlessChange: same=%d. new=%d\n", fSameMode, fEnabled));
3369
3370 if (!fSameMode)
3371 {
3372 /* we could validate the information here but hey, the guest can do that as well! */
3373 pThis->fSeamlessEnabled = fEnabled;
3374
3375 /* IRQ so the guest knows what's going on */
3376 VMMDevNotifyGuest(pThis, VMMDEV_EVENT_SEAMLESS_MODE_CHANGE_REQUEST);
3377 }
3378
3379 PDMCritSectLeave(&pThis->CritSect);
3380 return VINF_SUCCESS;
3381}
3382
3383/**
3384 * @interface_method_impl{PDMIVMMDEVPORT,pfnSetMemoryBalloon}
3385 */
3386static DECLCALLBACK(int) vmmdevIPort_SetMemoryBalloon(PPDMIVMMDEVPORT pInterface, uint32_t cMbBalloon)
3387{
3388 PVMMDEV pThis = RT_FROM_MEMBER(pInterface, VMMDEV, IPort);
3389 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
3390
3391 /* Verify that the new resolution is different and that guest does not yet know about it. */
3392 Log(("vmmdevIPort_SetMemoryBalloon: old=%u new=%u\n", pThis->cMbMemoryBalloonLast, cMbBalloon));
3393 if (pThis->cMbMemoryBalloonLast != cMbBalloon)
3394 {
3395 /* we could validate the information here but hey, the guest can do that as well! */
3396 pThis->cMbMemoryBalloon = cMbBalloon;
3397
3398 /* IRQ so the guest knows what's going on */
3399 VMMDevNotifyGuest(pThis, VMMDEV_EVENT_BALLOON_CHANGE_REQUEST);
3400 }
3401
3402 PDMCritSectLeave(&pThis->CritSect);
3403 return VINF_SUCCESS;
3404}
3405
3406/**
3407 * @interface_method_impl{PDMIVMMDEVPORT,pfnVRDPChange}
3408 */
3409static DECLCALLBACK(int) vmmdevIPort_VRDPChange(PPDMIVMMDEVPORT pInterface, bool fVRDPEnabled, uint32_t uVRDPExperienceLevel)
3410{
3411 PVMMDEV pThis = RT_FROM_MEMBER(pInterface, VMMDEV, IPort);
3412 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
3413
3414 bool fSame = (pThis->fVRDPEnabled == fVRDPEnabled);
3415
3416 Log(("vmmdevIPort_VRDPChange: old=%d. new=%d\n", pThis->fVRDPEnabled, fVRDPEnabled));
3417
3418 if (!fSame)
3419 {
3420 pThis->fVRDPEnabled = fVRDPEnabled;
3421 pThis->uVRDPExperienceLevel = uVRDPExperienceLevel;
3422
3423 VMMDevNotifyGuest(pThis, VMMDEV_EVENT_VRDP);
3424 }
3425
3426 PDMCritSectLeave(&pThis->CritSect);
3427 return VINF_SUCCESS;
3428}
3429
3430/**
3431 * @interface_method_impl{PDMIVMMDEVPORT,pfnSetStatisticsInterval}
3432 */
3433static DECLCALLBACK(int) vmmdevIPort_SetStatisticsInterval(PPDMIVMMDEVPORT pInterface, uint32_t cSecsStatInterval)
3434{
3435 PVMMDEV pThis = RT_FROM_MEMBER(pInterface, VMMDEV, IPort);
3436 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
3437
3438 /* Verify that the new resolution is different and that guest does not yet know about it. */
3439 bool fSame = (pThis->u32LastStatIntervalSize == cSecsStatInterval);
3440
3441 Log(("vmmdevIPort_SetStatisticsInterval: old=%d. new=%d\n", pThis->u32LastStatIntervalSize, cSecsStatInterval));
3442
3443 if (!fSame)
3444 {
3445 /* we could validate the information here but hey, the guest can do that as well! */
3446 pThis->u32StatIntervalSize = cSecsStatInterval;
3447
3448 /* IRQ so the guest knows what's going on */
3449 VMMDevNotifyGuest(pThis, VMMDEV_EVENT_STATISTICS_INTERVAL_CHANGE_REQUEST);
3450 }
3451
3452 PDMCritSectLeave(&pThis->CritSect);
3453 return VINF_SUCCESS;
3454}
3455
3456/**
3457 * @interface_method_impl{PDMIVMMDEVPORT,pfnSetCredentials}
3458 */
3459static DECLCALLBACK(int) vmmdevIPort_SetCredentials(PPDMIVMMDEVPORT pInterface, const char *pszUsername,
3460 const char *pszPassword, const char *pszDomain, uint32_t fFlags)
3461{
3462 PVMMDEV pThis = RT_FROM_MEMBER(pInterface, VMMDEV, IPort);
3463 AssertReturn(fFlags & (VMMDEV_SETCREDENTIALS_GUESTLOGON | VMMDEV_SETCREDENTIALS_JUDGE), VERR_INVALID_PARAMETER);
3464 size_t const cchUsername = strlen(pszUsername);
3465 AssertReturn(cchUsername < VMMDEV_CREDENTIALS_SZ_SIZE, VERR_BUFFER_OVERFLOW);
3466 size_t const cchPassword = strlen(pszPassword);
3467 AssertReturn(cchPassword < VMMDEV_CREDENTIALS_SZ_SIZE, VERR_BUFFER_OVERFLOW);
3468 size_t const cchDomain = strlen(pszDomain);
3469 AssertReturn(cchDomain < VMMDEV_CREDENTIALS_SZ_SIZE, VERR_BUFFER_OVERFLOW);
3470
3471 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
3472
3473 /*
3474 * Logon mode
3475 */
3476 if (fFlags & VMMDEV_SETCREDENTIALS_GUESTLOGON)
3477 {
3478 /* memorize the data */
3479 memcpy(pThis->pCredentials->Logon.szUserName, pszUsername, cchUsername);
3480 pThis->pCredentials->Logon.szUserName[cchUsername] = '\0';
3481 memcpy(pThis->pCredentials->Logon.szPassword, pszPassword, cchPassword);
3482 pThis->pCredentials->Logon.szPassword[cchPassword] = '\0';
3483 memcpy(pThis->pCredentials->Logon.szDomain, pszDomain, cchDomain);
3484 pThis->pCredentials->Logon.szDomain[cchDomain] = '\0';
3485 pThis->pCredentials->Logon.fAllowInteractiveLogon = !(fFlags & VMMDEV_SETCREDENTIALS_NOLOCALLOGON);
3486 }
3487 /*
3488 * Credentials verification mode?
3489 */
3490 else
3491 {
3492 /* memorize the data */
3493 memcpy(pThis->pCredentials->Judge.szUserName, pszUsername, cchUsername);
3494 pThis->pCredentials->Judge.szUserName[cchUsername] = '\0';
3495 memcpy(pThis->pCredentials->Judge.szPassword, pszPassword, cchPassword);
3496 pThis->pCredentials->Judge.szPassword[cchPassword] = '\0';
3497 memcpy(pThis->pCredentials->Judge.szDomain, pszDomain, cchDomain);
3498 pThis->pCredentials->Judge.szDomain[cchDomain] = '\0';
3499
3500 VMMDevNotifyGuest(pThis, VMMDEV_EVENT_JUDGE_CREDENTIALS);
3501 }
3502
3503 PDMCritSectLeave(&pThis->CritSect);
3504 return VINF_SUCCESS;
3505}
3506
3507/**
3508 * @interface_method_impl{PDMIVMMDEVPORT,pfnVBVAChange}
3509 *
3510 * Notification from the Display. Especially useful when acceleration is
3511 * disabled after a video mode change.
3512 */
3513static DECLCALLBACK(void) vmmdevIPort_VBVAChange(PPDMIVMMDEVPORT pInterface, bool fEnabled)
3514{
3515 PVMMDEV pThis = RT_FROM_MEMBER(pInterface, VMMDEV, IPort);
3516 Log(("vmmdevIPort_VBVAChange: fEnabled = %d\n", fEnabled));
3517
3518 /* Only used by saved state, which I guess is why we don't bother with locking here. */
3519 pThis->u32VideoAccelEnabled = fEnabled;
3520}
3521
3522/**
3523 * @interface_method_impl{PDMIVMMDEVPORT,pfnCpuHotUnplug}
3524 */
3525static DECLCALLBACK(int) vmmdevIPort_CpuHotUnplug(PPDMIVMMDEVPORT pInterface, uint32_t idCpuCore, uint32_t idCpuPackage)
3526{
3527 PVMMDEV pThis = RT_FROM_MEMBER(pInterface, VMMDEV, IPort);
3528 int rc = VINF_SUCCESS;
3529
3530 Log(("vmmdevIPort_CpuHotUnplug: idCpuCore=%u idCpuPackage=%u\n", idCpuCore, idCpuPackage));
3531
3532 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
3533
3534 if (pThis->fCpuHotPlugEventsEnabled)
3535 {
3536 pThis->enmCpuHotPlugEvent = VMMDevCpuEventType_Unplug;
3537 pThis->idCpuCore = idCpuCore;
3538 pThis->idCpuPackage = idCpuPackage;
3539 VMMDevNotifyGuest(pThis, VMMDEV_EVENT_CPU_HOTPLUG);
3540 }
3541 else
3542 rc = VERR_VMMDEV_CPU_HOTPLUG_NOT_MONITORED_BY_GUEST;
3543
3544 PDMCritSectLeave(&pThis->CritSect);
3545 return rc;
3546}
3547
3548/**
3549 * @interface_method_impl{PDMIVMMDEVPORT,pfnCpuHotPlug}
3550 */
3551static DECLCALLBACK(int) vmmdevIPort_CpuHotPlug(PPDMIVMMDEVPORT pInterface, uint32_t idCpuCore, uint32_t idCpuPackage)
3552{
3553 PVMMDEV pThis = RT_FROM_MEMBER(pInterface, VMMDEV, IPort);
3554 int rc = VINF_SUCCESS;
3555
3556 Log(("vmmdevCpuPlug: idCpuCore=%u idCpuPackage=%u\n", idCpuCore, idCpuPackage));
3557
3558 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
3559
3560 if (pThis->fCpuHotPlugEventsEnabled)
3561 {
3562 pThis->enmCpuHotPlugEvent = VMMDevCpuEventType_Plug;
3563 pThis->idCpuCore = idCpuCore;
3564 pThis->idCpuPackage = idCpuPackage;
3565 VMMDevNotifyGuest(pThis, VMMDEV_EVENT_CPU_HOTPLUG);
3566 }
3567 else
3568 rc = VERR_VMMDEV_CPU_HOTPLUG_NOT_MONITORED_BY_GUEST;
3569
3570 PDMCritSectLeave(&pThis->CritSect);
3571 return rc;
3572}
3573
3574
3575/* -=-=-=-=-=- Saved State -=-=-=-=-=- */
3576
3577/**
3578 * @callback_method_impl{FNSSMDEVLIVEEXEC}
3579 */
3580static DECLCALLBACK(int) vmmdevLiveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uPass)
3581{
3582 RT_NOREF1(uPass);
3583 PVMMDEV pThis = PDMINS_2_DATA(pDevIns, PVMMDEV);
3584
3585 SSMR3PutBool(pSSM, pThis->fGetHostTimeDisabled);
3586 SSMR3PutBool(pSSM, pThis->fBackdoorLogDisabled);
3587 SSMR3PutBool(pSSM, pThis->fKeepCredentials);
3588 SSMR3PutBool(pSSM, pThis->fHeapEnabled);
3589
3590 return VINF_SSM_DONT_CALL_AGAIN;
3591}
3592
3593
3594/**
3595 * @callback_method_impl{FNSSMDEVSAVEEXEC}
3596 */
3597static DECLCALLBACK(int) vmmdevSaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
3598{
3599 PVMMDEV pThis = PDMINS_2_DATA(pDevIns, PVMMDEV);
3600 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
3601
3602 vmmdevLiveExec(pDevIns, pSSM, SSM_PASS_FINAL);
3603
3604 SSMR3PutU32(pSSM, pThis->hypervisorSize);
3605 SSMR3PutU32(pSSM, pThis->mouseCapabilities);
3606 SSMR3PutS32(pSSM, pThis->mouseXAbs);
3607 SSMR3PutS32(pSSM, pThis->mouseYAbs);
3608
3609 SSMR3PutBool(pSSM, pThis->fNewGuestFilterMask);
3610 SSMR3PutU32(pSSM, pThis->u32NewGuestFilterMask);
3611 SSMR3PutU32(pSSM, pThis->u32GuestFilterMask);
3612 SSMR3PutU32(pSSM, pThis->u32HostEventFlags);
3613 /* The following is not strictly necessary as PGM restores MMIO2, keeping it for historical reasons. */
3614 SSMR3PutMem(pSSM, &pThis->pVMMDevRAMR3->V, sizeof(pThis->pVMMDevRAMR3->V));
3615
3616 SSMR3PutMem(pSSM, &pThis->guestInfo, sizeof(pThis->guestInfo));
3617 SSMR3PutU32(pSSM, pThis->fu32AdditionsOk);
3618 SSMR3PutU32(pSSM, pThis->u32VideoAccelEnabled);
3619 SSMR3PutBool(pSSM, pThis->displayChangeData.fGuestSentChangeEventAck);
3620
3621 SSMR3PutU32(pSSM, pThis->guestCaps);
3622
3623#ifdef VBOX_WITH_HGCM
3624 vmmdevHGCMSaveState(pThis, pSSM);
3625#endif /* VBOX_WITH_HGCM */
3626
3627 SSMR3PutU32(pSSM, pThis->fHostCursorRequested);
3628
3629 SSMR3PutU32(pSSM, pThis->guestInfo2.uFullVersion);
3630 SSMR3PutU32(pSSM, pThis->guestInfo2.uRevision);
3631 SSMR3PutU32(pSSM, pThis->guestInfo2.fFeatures);
3632 SSMR3PutStrZ(pSSM, pThis->guestInfo2.szName);
3633 SSMR3PutU32(pSSM, pThis->cFacilityStatuses);
3634 for (uint32_t i = 0; i < pThis->cFacilityStatuses; i++)
3635 {
3636 SSMR3PutU32(pSSM, pThis->aFacilityStatuses[i].enmFacility);
3637 SSMR3PutU32(pSSM, pThis->aFacilityStatuses[i].fFlags);
3638 SSMR3PutU16(pSSM, (uint16_t)pThis->aFacilityStatuses[i].enmStatus);
3639 SSMR3PutS64(pSSM, RTTimeSpecGetNano(&pThis->aFacilityStatuses[i].TimeSpecTS));
3640 }
3641
3642 /* Heartbeat: */
3643 SSMR3PutBool(pSSM, pThis->fHeartbeatActive);
3644 SSMR3PutBool(pSSM, pThis->fFlatlined);
3645 SSMR3PutU64(pSSM, pThis->nsLastHeartbeatTS);
3646 TMR3TimerSave(pThis->pFlatlinedTimer, pSSM);
3647
3648 PDMCritSectLeave(&pThis->CritSect);
3649 return VINF_SUCCESS;
3650}
3651
3652/**
3653 * @callback_method_impl{FNSSMDEVLOADEXEC}
3654 */
3655static DECLCALLBACK(int) vmmdevLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
3656{
3657 /** @todo The code load code is assuming we're always loaded into a freshly
3658 * constructed VM. */
3659 PVMMDEV pThis = PDMINS_2_DATA(pDevIns, PVMMDEV);
3660 int rc;
3661
3662 if ( uVersion > VMMDEV_SAVED_STATE_VERSION
3663 || uVersion < 6)
3664 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
3665
3666 /* config */
3667 if (uVersion > VMMDEV_SAVED_STATE_VERSION_VBOX_30)
3668 {
3669 bool f;
3670 rc = SSMR3GetBool(pSSM, &f); AssertRCReturn(rc, rc);
3671 if (pThis->fGetHostTimeDisabled != f)
3672 LogRel(("VMMDev: Config mismatch - fGetHostTimeDisabled: config=%RTbool saved=%RTbool\n", pThis->fGetHostTimeDisabled, f));
3673
3674 rc = SSMR3GetBool(pSSM, &f); AssertRCReturn(rc, rc);
3675 if (pThis->fBackdoorLogDisabled != f)
3676 LogRel(("VMMDev: Config mismatch - fBackdoorLogDisabled: config=%RTbool saved=%RTbool\n", pThis->fBackdoorLogDisabled, f));
3677
3678 rc = SSMR3GetBool(pSSM, &f); AssertRCReturn(rc, rc);
3679 if (pThis->fKeepCredentials != f)
3680 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Config mismatch - fKeepCredentials: config=%RTbool saved=%RTbool"),
3681 pThis->fKeepCredentials, f);
3682 rc = SSMR3GetBool(pSSM, &f); AssertRCReturn(rc, rc);
3683 if (pThis->fHeapEnabled != f)
3684 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Config mismatch - fHeapEnabled: config=%RTbool saved=%RTbool"),
3685 pThis->fHeapEnabled, f);
3686 }
3687
3688 if (uPass != SSM_PASS_FINAL)
3689 return VINF_SUCCESS;
3690
3691 /* state */
3692 SSMR3GetU32(pSSM, &pThis->hypervisorSize);
3693 SSMR3GetU32(pSSM, &pThis->mouseCapabilities);
3694 SSMR3GetS32(pSSM, &pThis->mouseXAbs);
3695 SSMR3GetS32(pSSM, &pThis->mouseYAbs);
3696
3697 SSMR3GetBool(pSSM, &pThis->fNewGuestFilterMask);
3698 SSMR3GetU32(pSSM, &pThis->u32NewGuestFilterMask);
3699 SSMR3GetU32(pSSM, &pThis->u32GuestFilterMask);
3700 SSMR3GetU32(pSSM, &pThis->u32HostEventFlags);
3701
3702 //SSMR3GetBool(pSSM, &pThis->pVMMDevRAMR3->fHaveEvents);
3703 // here be dragons (probably)
3704 SSMR3GetMem(pSSM, &pThis->pVMMDevRAMR3->V, sizeof (pThis->pVMMDevRAMR3->V));
3705
3706 SSMR3GetMem(pSSM, &pThis->guestInfo, sizeof (pThis->guestInfo));
3707 SSMR3GetU32(pSSM, &pThis->fu32AdditionsOk);
3708 SSMR3GetU32(pSSM, &pThis->u32VideoAccelEnabled);
3709 if (uVersion > 10)
3710 SSMR3GetBool(pSSM, &pThis->displayChangeData.fGuestSentChangeEventAck);
3711
3712 rc = SSMR3GetU32(pSSM, &pThis->guestCaps);
3713
3714 /* Attributes which were temporarily introduced in r30072 */
3715 if (uVersion == 7)
3716 {
3717 uint32_t temp;
3718 SSMR3GetU32(pSSM, &temp);
3719 rc = SSMR3GetU32(pSSM, &temp);
3720 }
3721 AssertRCReturn(rc, rc);
3722
3723#ifdef VBOX_WITH_HGCM
3724 rc = vmmdevHGCMLoadState(pThis, pSSM, uVersion);
3725 AssertRCReturn(rc, rc);
3726#endif /* VBOX_WITH_HGCM */
3727
3728 if (uVersion >= 10)
3729 rc = SSMR3GetU32(pSSM, &pThis->fHostCursorRequested);
3730 AssertRCReturn(rc, rc);
3731
3732 if (uVersion > VMMDEV_SAVED_STATE_VERSION_MISSING_GUEST_INFO_2)
3733 {
3734 SSMR3GetU32(pSSM, &pThis->guestInfo2.uFullVersion);
3735 SSMR3GetU32(pSSM, &pThis->guestInfo2.uRevision);
3736 SSMR3GetU32(pSSM, &pThis->guestInfo2.fFeatures);
3737 rc = SSMR3GetStrZ(pSSM, &pThis->guestInfo2.szName[0], sizeof(pThis->guestInfo2.szName));
3738 AssertRCReturn(rc, rc);
3739 }
3740
3741 if (uVersion > VMMDEV_SAVED_STATE_VERSION_MISSING_FACILITY_STATUSES)
3742 {
3743 uint32_t cFacilityStatuses;
3744 rc = SSMR3GetU32(pSSM, &cFacilityStatuses);
3745 AssertRCReturn(rc, rc);
3746
3747 for (uint32_t i = 0; i < cFacilityStatuses; i++)
3748 {
3749 uint32_t uFacility, fFlags;
3750 uint16_t uStatus;
3751 int64_t iTimeStampNano;
3752
3753 SSMR3GetU32(pSSM, &uFacility);
3754 SSMR3GetU32(pSSM, &fFlags);
3755 SSMR3GetU16(pSSM, &uStatus);
3756 rc = SSMR3GetS64(pSSM, &iTimeStampNano);
3757 AssertRCReturn(rc, rc);
3758
3759 PVMMDEVFACILITYSTATUSENTRY pEntry = vmmdevGetFacilityStatusEntry(pThis, (VBoxGuestFacilityType)uFacility);
3760 AssertLogRelMsgReturn(pEntry,
3761 ("VMMDev: Ran out of entries restoring the guest facility statuses. Saved state has %u.\n", cFacilityStatuses),
3762 VERR_OUT_OF_RESOURCES);
3763 pEntry->enmStatus = (VBoxGuestFacilityStatus)uStatus;
3764 pEntry->fFlags = fFlags;
3765 RTTimeSpecSetNano(&pEntry->TimeSpecTS, iTimeStampNano);
3766 }
3767 }
3768
3769 /*
3770 * Heartbeat.
3771 */
3772 if (uVersion >= VMMDEV_SAVED_STATE_VERSION_HEARTBEAT)
3773 {
3774 SSMR3GetBool(pSSM, (bool *)&pThis->fHeartbeatActive);
3775 SSMR3GetBool(pSSM, (bool *)&pThis->fFlatlined);
3776 SSMR3GetU64(pSSM, (uint64_t *)&pThis->nsLastHeartbeatTS);
3777 rc = TMR3TimerLoad(pThis->pFlatlinedTimer, pSSM);
3778 AssertRCReturn(rc, rc);
3779 if (pThis->fFlatlined)
3780 LogRel(("vmmdevLoadState: Guest has flatlined. Last heartbeat %'RU64 ns before state was saved.\n",
3781 TMTimerGetNano(pThis->pFlatlinedTimer) - pThis->nsLastHeartbeatTS));
3782 }
3783
3784 /*
3785 * On a resume, we send the capabilities changed message so
3786 * that listeners can sync their state again
3787 */
3788 Log(("vmmdevLoadState: capabilities changed (%x), informing connector\n", pThis->mouseCapabilities));
3789 if (pThis->pDrv)
3790 {
3791 pThis->pDrv->pfnUpdateMouseCapabilities(pThis->pDrv, pThis->mouseCapabilities);
3792 if (uVersion >= 10)
3793 pThis->pDrv->pfnUpdatePointerShape(pThis->pDrv,
3794 /*fVisible=*/!!pThis->fHostCursorRequested,
3795 /*fAlpha=*/false,
3796 /*xHot=*/0, /*yHot=*/0,
3797 /*cx=*/0, /*cy=*/0,
3798 /*pvShape=*/NULL);
3799 }
3800
3801 if (pThis->fu32AdditionsOk)
3802 {
3803 vmmdevLogGuestOsInfo(&pThis->guestInfo);
3804 if (pThis->pDrv)
3805 {
3806 if (pThis->guestInfo2.uFullVersion && pThis->pDrv->pfnUpdateGuestInfo2)
3807 pThis->pDrv->pfnUpdateGuestInfo2(pThis->pDrv, pThis->guestInfo2.uFullVersion, pThis->guestInfo2.szName,
3808 pThis->guestInfo2.uRevision, pThis->guestInfo2.fFeatures);
3809 if (pThis->pDrv->pfnUpdateGuestInfo)
3810 pThis->pDrv->pfnUpdateGuestInfo(pThis->pDrv, &pThis->guestInfo);
3811
3812 if (pThis->pDrv->pfnUpdateGuestStatus)
3813 {
3814 for (uint32_t i = 0; i < pThis->cFacilityStatuses; i++) /* ascending order! */
3815 if ( pThis->aFacilityStatuses[i].enmStatus != VBoxGuestFacilityStatus_Inactive
3816 || !pThis->aFacilityStatuses[i].fFixed)
3817 pThis->pDrv->pfnUpdateGuestStatus(pThis->pDrv,
3818 pThis->aFacilityStatuses[i].enmFacility,
3819 (uint16_t)pThis->aFacilityStatuses[i].enmStatus,
3820 pThis->aFacilityStatuses[i].fFlags,
3821 &pThis->aFacilityStatuses[i].TimeSpecTS);
3822 }
3823 }
3824 }
3825 if (pThis->pDrv && pThis->pDrv->pfnUpdateGuestCapabilities)
3826 pThis->pDrv->pfnUpdateGuestCapabilities(pThis->pDrv, pThis->guestCaps);
3827
3828 return VINF_SUCCESS;
3829}
3830
3831/**
3832 * Load state done callback. Notify guest of restore event.
3833 *
3834 * @returns VBox status code.
3835 * @param pDevIns The device instance.
3836 * @param pSSM The handle to the saved state.
3837 */
3838static DECLCALLBACK(int) vmmdevLoadStateDone(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
3839{
3840 RT_NOREF1(pSSM);
3841 PVMMDEV pThis = PDMINS_2_DATA(pDevIns, PVMMDEV);
3842
3843#ifdef VBOX_WITH_HGCM
3844 int rc = vmmdevHGCMLoadStateDone(pThis);
3845 AssertLogRelRCReturn(rc, rc);
3846#endif /* VBOX_WITH_HGCM */
3847
3848 /* Reestablish the acceleration status. */
3849 if ( pThis->u32VideoAccelEnabled
3850 && pThis->pDrv)
3851 {
3852 pThis->pDrv->pfnVideoAccelEnable(pThis->pDrv, !!pThis->u32VideoAccelEnabled, &pThis->pVMMDevRAMR3->vbvaMemory);
3853 }
3854
3855 VMMDevNotifyGuest(pThis, VMMDEV_EVENT_RESTORED);
3856
3857 return VINF_SUCCESS;
3858}
3859
3860
3861/* -=-=-=-=- PDMDEVREG -=-=-=-=- */
3862
3863/**
3864 * (Re-)initializes the MMIO2 data.
3865 *
3866 * @param pThis Pointer to the VMMDev instance data.
3867 */
3868static void vmmdevInitRam(PVMMDEV pThis)
3869{
3870 memset(pThis->pVMMDevRAMR3, 0, sizeof(VMMDevMemory));
3871 pThis->pVMMDevRAMR3->u32Size = sizeof(VMMDevMemory);
3872 pThis->pVMMDevRAMR3->u32Version = VMMDEV_MEMORY_VERSION;
3873}
3874
3875
3876/**
3877 * @interface_method_impl{PDMDEVREG,pfnReset}
3878 */
3879static DECLCALLBACK(void) vmmdevReset(PPDMDEVINS pDevIns)
3880{
3881 PVMMDEV pThis = PDMINS_2_DATA(pDevIns, PVMMDEV);
3882 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
3883
3884 /*
3885 * Reset the mouse integration feature bits
3886 */
3887 if (pThis->mouseCapabilities & VMMDEV_MOUSE_GUEST_MASK)
3888 {
3889 pThis->mouseCapabilities &= ~VMMDEV_MOUSE_GUEST_MASK;
3890 /* notify the connector */
3891 Log(("vmmdevReset: capabilities changed (%x), informing connector\n", pThis->mouseCapabilities));
3892 pThis->pDrv->pfnUpdateMouseCapabilities(pThis->pDrv, pThis->mouseCapabilities);
3893 }
3894 pThis->fHostCursorRequested = false;
3895
3896 pThis->hypervisorSize = 0;
3897
3898 /* re-initialize the VMMDev memory */
3899 if (pThis->pVMMDevRAMR3)
3900 vmmdevInitRam(pThis);
3901
3902 /* credentials have to go away (by default) */
3903 if (!pThis->fKeepCredentials)
3904 {
3905 memset(pThis->pCredentials->Logon.szUserName, '\0', VMMDEV_CREDENTIALS_SZ_SIZE);
3906 memset(pThis->pCredentials->Logon.szPassword, '\0', VMMDEV_CREDENTIALS_SZ_SIZE);
3907 memset(pThis->pCredentials->Logon.szDomain, '\0', VMMDEV_CREDENTIALS_SZ_SIZE);
3908 }
3909 memset(pThis->pCredentials->Judge.szUserName, '\0', VMMDEV_CREDENTIALS_SZ_SIZE);
3910 memset(pThis->pCredentials->Judge.szPassword, '\0', VMMDEV_CREDENTIALS_SZ_SIZE);
3911 memset(pThis->pCredentials->Judge.szDomain, '\0', VMMDEV_CREDENTIALS_SZ_SIZE);
3912
3913 /* Reset means that additions will report again. */
3914 const bool fVersionChanged = pThis->fu32AdditionsOk
3915 || pThis->guestInfo.interfaceVersion
3916 || pThis->guestInfo.osType != VBOXOSTYPE_Unknown;
3917 if (fVersionChanged)
3918 Log(("vmmdevReset: fu32AdditionsOk=%d additionsVersion=%x osType=%#x\n",
3919 pThis->fu32AdditionsOk, pThis->guestInfo.interfaceVersion, pThis->guestInfo.osType));
3920 pThis->fu32AdditionsOk = false;
3921 memset (&pThis->guestInfo, 0, sizeof (pThis->guestInfo));
3922 RT_ZERO(pThis->guestInfo2);
3923 const bool fCapsChanged = pThis->guestCaps != 0; /* Report transition to 0. */
3924 pThis->guestCaps = 0;
3925
3926 /* Clear facilities. No need to tell Main as it will get a
3927 pfnUpdateGuestInfo callback. */
3928 RTTIMESPEC TimeStampNow;
3929 RTTimeNow(&TimeStampNow);
3930 uint32_t iFacility = pThis->cFacilityStatuses;
3931 while (iFacility-- > 0)
3932 {
3933 pThis->aFacilityStatuses[iFacility].enmStatus = VBoxGuestFacilityStatus_Inactive;
3934 pThis->aFacilityStatuses[iFacility].TimeSpecTS = TimeStampNow;
3935 }
3936
3937 /* clear pending display change request. */
3938 for (unsigned i = 0; i < RT_ELEMENTS(pThis->displayChangeData.aRequests); i++)
3939 {
3940 DISPLAYCHANGEREQUEST *pRequest = &pThis->displayChangeData.aRequests[i];
3941 memset (&pRequest->lastReadDisplayChangeRequest, 0, sizeof (pRequest->lastReadDisplayChangeRequest));
3942 }
3943 pThis->displayChangeData.iCurrentMonitor = 0;
3944 pThis->displayChangeData.fGuestSentChangeEventAck = false;
3945
3946 /* disable seamless mode */
3947 pThis->fLastSeamlessEnabled = false;
3948
3949 /* disabled memory ballooning */
3950 pThis->cMbMemoryBalloonLast = 0;
3951
3952 /* disabled statistics updating */
3953 pThis->u32LastStatIntervalSize = 0;
3954
3955#ifdef VBOX_WITH_HGCM
3956 /* Clear the "HGCM event enabled" flag so the event can be automatically reenabled. */
3957 pThis->u32HGCMEnabled = 0;
3958#endif
3959
3960 /*
3961 * Deactive heartbeat.
3962 */
3963 if (pThis->fHeartbeatActive)
3964 {
3965 TMTimerStop(pThis->pFlatlinedTimer);
3966 pThis->fFlatlined = false;
3967 pThis->fHeartbeatActive = true;
3968 }
3969
3970 /*
3971 * Clear the event variables.
3972 *
3973 * XXX By design we should NOT clear pThis->u32HostEventFlags because it is designed
3974 * that way so host events do not depend on guest resets. However, the pending
3975 * event flags actually _were_ cleared since ages so we mask out events from
3976 * clearing which we really need to survive the reset. See xtracker 5767.
3977 */
3978 pThis->u32HostEventFlags &= VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST;
3979 pThis->u32GuestFilterMask = 0;
3980 pThis->u32NewGuestFilterMask = 0;
3981 pThis->fNewGuestFilterMask = 0;
3982
3983 /*
3984 * Call the update functions as required.
3985 */
3986 if (fVersionChanged && pThis->pDrv && pThis->pDrv->pfnUpdateGuestInfo)
3987 pThis->pDrv->pfnUpdateGuestInfo(pThis->pDrv, &pThis->guestInfo);
3988 if (fCapsChanged && pThis->pDrv && pThis->pDrv->pfnUpdateGuestCapabilities)
3989 pThis->pDrv->pfnUpdateGuestCapabilities(pThis->pDrv, pThis->guestCaps);
3990
3991 /*
3992 * Generate a unique session id for this VM; it will be changed for each start, reset or restore.
3993 * This can be used for restore detection inside the guest.
3994 */
3995 pThis->idSession = ASMReadTSC();
3996
3997 PDMCritSectLeave(&pThis->CritSect);
3998}
3999
4000
4001/**
4002 * @interface_method_impl{PDMDEVREG,pfnRelocate}
4003 */
4004static DECLCALLBACK(void) vmmdevRelocate(PPDMDEVINS pDevIns, RTGCINTPTR offDelta)
4005{
4006 NOREF(pDevIns);
4007 NOREF(offDelta);
4008}
4009
4010
4011/**
4012 * @interface_method_impl{PDMDEVREG,pfnDestruct}
4013 */
4014static DECLCALLBACK(int) vmmdevDestruct(PPDMDEVINS pDevIns)
4015{
4016 PVMMDEV pThis = PDMINS_2_DATA(pDevIns, PVMMDEV);
4017 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
4018
4019 /*
4020 * Wipe and free the credentials.
4021 */
4022 if (pThis->pCredentials)
4023 {
4024 RTMemWipeThoroughly(pThis->pCredentials, sizeof(*pThis->pCredentials), 10);
4025 RTMemFree(pThis->pCredentials);
4026 pThis->pCredentials = NULL;
4027 }
4028
4029#ifdef VBOX_WITH_HGCM
4030 vmmdevHGCMDestroy(pThis);
4031 RTCritSectDelete(&pThis->critsectHGCMCmdList);
4032#endif
4033
4034#ifndef VBOX_WITHOUT_TESTING_FEATURES
4035 /*
4036 * Clean up the testing device.
4037 */
4038 vmmdevTestingTerminate(pDevIns);
4039#endif
4040
4041 return VINF_SUCCESS;
4042}
4043
4044
4045/**
4046 * @interface_method_impl{PDMDEVREG,pfnConstruct}
4047 */
4048static DECLCALLBACK(int) vmmdevConstruct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
4049{
4050 PVMMDEV pThis = PDMINS_2_DATA(pDevIns, PVMMDEV);
4051 int rc;
4052
4053 Assert(iInstance == 0);
4054 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
4055
4056 /*
4057 * Initialize data (most of it anyway).
4058 */
4059 /* Save PDM device instance data for future reference. */
4060 pThis->pDevIns = pDevIns;
4061
4062 /* PCI vendor, just a free bogus value */
4063 PCIDevSetVendorId(&pThis->PciDev, 0x80ee);
4064 /* device ID */
4065 PCIDevSetDeviceId(&pThis->PciDev, 0xcafe);
4066 /* class sub code (other type of system peripheral) */
4067 PCIDevSetClassSub(&pThis->PciDev, 0x80);
4068 /* class base code (base system peripheral) */
4069 PCIDevSetClassBase(&pThis->PciDev, 0x08);
4070 /* header type */
4071 PCIDevSetHeaderType(&pThis->PciDev, 0x00);
4072 /* interrupt on pin 0 */
4073 PCIDevSetInterruptPin(&pThis->PciDev, 0x01);
4074
4075 RTTIMESPEC TimeStampNow;
4076 RTTimeNow(&TimeStampNow);
4077 vmmdevAllocFacilityStatusEntry(pThis, VBoxGuestFacilityType_VBoxGuestDriver, true /*fFixed*/, &TimeStampNow);
4078 vmmdevAllocFacilityStatusEntry(pThis, VBoxGuestFacilityType_VBoxService, true /*fFixed*/, &TimeStampNow);
4079 vmmdevAllocFacilityStatusEntry(pThis, VBoxGuestFacilityType_VBoxTrayClient, true /*fFixed*/, &TimeStampNow);
4080 vmmdevAllocFacilityStatusEntry(pThis, VBoxGuestFacilityType_Seamless, true /*fFixed*/, &TimeStampNow);
4081 vmmdevAllocFacilityStatusEntry(pThis, VBoxGuestFacilityType_Graphics, true /*fFixed*/, &TimeStampNow);
4082 Assert(pThis->cFacilityStatuses == 5);
4083
4084 /*
4085 * Interfaces
4086 */
4087 /* IBase */
4088 pThis->IBase.pfnQueryInterface = vmmdevPortQueryInterface;
4089
4090 /* VMMDev port */
4091 pThis->IPort.pfnQueryAbsoluteMouse = vmmdevIPort_QueryAbsoluteMouse;
4092 pThis->IPort.pfnSetAbsoluteMouse = vmmdevIPort_SetAbsoluteMouse ;
4093 pThis->IPort.pfnQueryMouseCapabilities = vmmdevIPort_QueryMouseCapabilities;
4094 pThis->IPort.pfnUpdateMouseCapabilities = vmmdevIPort_UpdateMouseCapabilities;
4095 pThis->IPort.pfnRequestDisplayChange = vmmdevIPort_RequestDisplayChange;
4096 pThis->IPort.pfnSetCredentials = vmmdevIPort_SetCredentials;
4097 pThis->IPort.pfnVBVAChange = vmmdevIPort_VBVAChange;
4098 pThis->IPort.pfnRequestSeamlessChange = vmmdevIPort_RequestSeamlessChange;
4099 pThis->IPort.pfnSetMemoryBalloon = vmmdevIPort_SetMemoryBalloon;
4100 pThis->IPort.pfnSetStatisticsInterval = vmmdevIPort_SetStatisticsInterval;
4101 pThis->IPort.pfnVRDPChange = vmmdevIPort_VRDPChange;
4102 pThis->IPort.pfnCpuHotUnplug = vmmdevIPort_CpuHotUnplug;
4103 pThis->IPort.pfnCpuHotPlug = vmmdevIPort_CpuHotPlug;
4104
4105 /* Shared folder LED */
4106 pThis->SharedFolders.Led.u32Magic = PDMLED_MAGIC;
4107 pThis->SharedFolders.ILeds.pfnQueryStatusLed = vmmdevQueryStatusLed;
4108
4109#ifdef VBOX_WITH_HGCM
4110 /* HGCM port */
4111 pThis->IHGCMPort.pfnCompleted = hgcmCompleted;
4112#endif
4113
4114 pThis->pCredentials = (VMMDEVCREDS *)RTMemAllocZ(sizeof(*pThis->pCredentials));
4115 if (!pThis->pCredentials)
4116 return VERR_NO_MEMORY;
4117
4118
4119 /*
4120 * Validate and read the configuration.
4121 */
4122 PDMDEV_VALIDATE_CONFIG_RETURN(pDevIns,
4123 "GetHostTimeDisabled|"
4124 "BackdoorLogDisabled|"
4125 "KeepCredentials|"
4126 "HeapEnabled|"
4127 "RZEnabled|"
4128 "GuestCoreDumpEnabled|"
4129 "GuestCoreDumpDir|"
4130 "GuestCoreDumpCount|"
4131 "HeartbeatInterval|"
4132 "HeartbeatTimeout|"
4133 "TestingEnabled|"
4134 "TestingMMIO|"
4135 "TestintXmlOutputFile"
4136 ,
4137 "");
4138
4139 rc = CFGMR3QueryBoolDef(pCfg, "GetHostTimeDisabled", &pThis->fGetHostTimeDisabled, false);
4140 if (RT_FAILURE(rc))
4141 return PDMDEV_SET_ERROR(pDevIns, rc,
4142 N_("Configuration error: Failed querying \"GetHostTimeDisabled\" as a boolean"));
4143
4144 rc = CFGMR3QueryBoolDef(pCfg, "BackdoorLogDisabled", &pThis->fBackdoorLogDisabled, false);
4145 if (RT_FAILURE(rc))
4146 return PDMDEV_SET_ERROR(pDevIns, rc,
4147 N_("Configuration error: Failed querying \"BackdoorLogDisabled\" as a boolean"));
4148
4149 rc = CFGMR3QueryBoolDef(pCfg, "KeepCredentials", &pThis->fKeepCredentials, false);
4150 if (RT_FAILURE(rc))
4151 return PDMDEV_SET_ERROR(pDevIns, rc,
4152 N_("Configuration error: Failed querying \"KeepCredentials\" as a boolean"));
4153
4154 rc = CFGMR3QueryBoolDef(pCfg, "HeapEnabled", &pThis->fHeapEnabled, true);
4155 if (RT_FAILURE(rc))
4156 return PDMDEV_SET_ERROR(pDevIns, rc,
4157 N_("Configuration error: Failed querying \"HeapEnabled\" as a boolean"));
4158
4159 rc = CFGMR3QueryBoolDef(pCfg, "RZEnabled", &pThis->fRZEnabled, true);
4160 if (RT_FAILURE(rc))
4161 return PDMDEV_SET_ERROR(pDevIns, rc,
4162 N_("Configuration error: Failed querying \"RZEnabled\" as a boolean"));
4163
4164 rc = CFGMR3QueryBoolDef(pCfg, "GuestCoreDumpEnabled", &pThis->fGuestCoreDumpEnabled, false);
4165 if (RT_FAILURE(rc))
4166 return PDMDEV_SET_ERROR(pDevIns, rc,
4167 N_("Configuration error: Failed querying \"GuestCoreDumpEnabled\" as a boolean"));
4168
4169 char *pszGuestCoreDumpDir = NULL;
4170 rc = CFGMR3QueryStringAllocDef(pCfg, "GuestCoreDumpDir", &pszGuestCoreDumpDir, "");
4171 if (RT_FAILURE(rc))
4172 return PDMDEV_SET_ERROR(pDevIns, rc,
4173 N_("Configuration error: Failed querying \"GuestCoreDumpDir\" as a string"));
4174
4175 RTStrCopy(pThis->szGuestCoreDumpDir, sizeof(pThis->szGuestCoreDumpDir), pszGuestCoreDumpDir);
4176 MMR3HeapFree(pszGuestCoreDumpDir);
4177
4178 rc = CFGMR3QueryU32Def(pCfg, "GuestCoreDumpCount", &pThis->cGuestCoreDumps, 3);
4179 if (RT_FAILURE(rc))
4180 return PDMDEV_SET_ERROR(pDevIns, rc,
4181 N_("Configuration error: Failed querying \"GuestCoreDumpCount\" as a 32-bit unsigned integer"));
4182
4183 rc = CFGMR3QueryU64Def(pCfg, "HeartbeatInterval", &pThis->cNsHeartbeatInterval, VMMDEV_HEARTBEAT_DEFAULT_INTERVAL);
4184 if (RT_FAILURE(rc))
4185 return PDMDEV_SET_ERROR(pDevIns, rc,
4186 N_("Configuration error: Failed querying \"HeartbeatInterval\" as a 64-bit unsigned integer"));
4187 if (pThis->cNsHeartbeatInterval < RT_NS_100MS / 2)
4188 return PDMDEV_SET_ERROR(pDevIns, rc,
4189 N_("Configuration error: Heartbeat interval \"HeartbeatInterval\" too small"));
4190
4191 rc = CFGMR3QueryU64Def(pCfg, "HeartbeatTimeout", &pThis->cNsHeartbeatTimeout, pThis->cNsHeartbeatInterval * 2);
4192 if (RT_FAILURE(rc))
4193 return PDMDEV_SET_ERROR(pDevIns, rc,
4194 N_("Configuration error: Failed querying \"HeartbeatTimeout\" as a 64-bit unsigned integer"));
4195 if (pThis->cNsHeartbeatTimeout < RT_NS_100MS)
4196 return PDMDEV_SET_ERROR(pDevIns, rc,
4197 N_("Configuration error: Heartbeat timeout \"HeartbeatTimeout\" too small"));
4198 if (pThis->cNsHeartbeatTimeout <= pThis->cNsHeartbeatInterval + RT_NS_10MS)
4199 return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
4200 N_("Configuration error: Heartbeat timeout \"HeartbeatTimeout\" value (%'ull ns) is too close to the interval (%'ull ns)"),
4201 pThis->cNsHeartbeatTimeout, pThis->cNsHeartbeatInterval);
4202
4203#ifndef VBOX_WITHOUT_TESTING_FEATURES
4204 rc = CFGMR3QueryBoolDef(pCfg, "TestingEnabled", &pThis->fTestingEnabled, false);
4205 if (RT_FAILURE(rc))
4206 return PDMDEV_SET_ERROR(pDevIns, rc,
4207 N_("Configuration error: Failed querying \"TestingEnabled\" as a boolean"));
4208 rc = CFGMR3QueryBoolDef(pCfg, "TestingMMIO", &pThis->fTestingMMIO, false);
4209 if (RT_FAILURE(rc))
4210 return PDMDEV_SET_ERROR(pDevIns, rc,
4211 N_("Configuration error: Failed querying \"TestingMMIO\" as a boolean"));
4212 rc = CFGMR3QueryStringAllocDef(pCfg, "TestintXmlOutputFile", &pThis->pszTestingXmlOutput, NULL);
4213 if (RT_FAILURE(rc))
4214 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Failed querying \"TestintXmlOutputFile\" as a string"));
4215
4216 /** @todo image-to-load-filename? */
4217#endif
4218
4219 pThis->cbGuestRAM = MMR3PhysGetRamSize(PDMDevHlpGetVM(pDevIns));
4220
4221 /*
4222 * We do our own locking entirely. So, install NOP critsect for the device
4223 * and create our own critsect for use where it really matters (++).
4224 */
4225 rc = PDMDevHlpSetDeviceCritSect(pDevIns, PDMDevHlpCritSectGetNop(pDevIns));
4226 AssertRCReturn(rc, rc);
4227 rc = PDMDevHlpCritSectInit(pDevIns, &pThis->CritSect, RT_SRC_POS, "VMMDev#%u", iInstance);
4228 AssertRCReturn(rc, rc);
4229
4230 /*
4231 * Register the backdoor logging port
4232 */
4233 rc = PDMDevHlpIOPortRegister(pDevIns, RTLOG_DEBUG_PORT, 1, NULL, vmmdevBackdoorLog,
4234 NULL, NULL, NULL, "VMMDev backdoor logging");
4235 AssertRCReturn(rc, rc);
4236
4237#ifdef VMMDEV_WITH_ALT_TIMESYNC
4238 /*
4239 * Alternative timesync source.
4240 *
4241 * This was orignally added for creating a simple time sync service in an
4242 * OpenBSD guest without requiring VBoxGuest and VBoxService to be ported
4243 * first. We keep it in case it comes in handy.
4244 */
4245 rc = PDMDevHlpIOPortRegister(pDevIns, 0x505, 1, NULL,
4246 vmmdevAltTimeSyncWrite, vmmdevAltTimeSyncRead,
4247 NULL, NULL, "VMMDev timesync backdoor");
4248 AssertRCReturn(rc, rc);
4249#endif
4250
4251 /*
4252 * Register the PCI device.
4253 */
4254 rc = PDMDevHlpPCIRegister(pDevIns, &pThis->PciDev);
4255 if (RT_FAILURE(rc))
4256 return rc;
4257 if (pThis->PciDev.uDevFn != 32 || iInstance != 0)
4258 Log(("!!WARNING!!: pThis->PciDev.uDevFn=%d (ignore if testcase or no started by Main)\n", pThis->PciDev.uDevFn));
4259 rc = PDMDevHlpPCIIORegionRegister(pDevIns, 0, 0x20, PCI_ADDRESS_SPACE_IO, vmmdevIOPortRegionMap);
4260 if (RT_FAILURE(rc))
4261 return rc;
4262 rc = PDMDevHlpPCIIORegionRegister(pDevIns, 1, VMMDEV_RAM_SIZE, PCI_ADDRESS_SPACE_MEM, vmmdevIORAMRegionMap);
4263 if (RT_FAILURE(rc))
4264 return rc;
4265 if (pThis->fHeapEnabled)
4266 {
4267 rc = PDMDevHlpPCIIORegionRegister(pDevIns, 2, VMMDEV_HEAP_SIZE, PCI_ADDRESS_SPACE_MEM_PREFETCH, vmmdevIORAMRegionMap);
4268 if (RT_FAILURE(rc))
4269 return rc;
4270 }
4271
4272 /*
4273 * Allocate and initialize the MMIO2 memory.
4274 */
4275 rc = PDMDevHlpMMIO2Register(pDevIns, &pThis->PciDev, 1 /*iRegion*/, VMMDEV_RAM_SIZE, 0 /*fFlags*/,
4276 (void **)&pThis->pVMMDevRAMR3, "VMMDev");
4277 if (RT_FAILURE(rc))
4278 return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
4279 N_("Failed to allocate %u bytes of memory for the VMM device"), VMMDEV_RAM_SIZE);
4280 vmmdevInitRam(pThis);
4281
4282 if (pThis->fHeapEnabled)
4283 {
4284 rc = PDMDevHlpMMIO2Register(pDevIns, &pThis->PciDev, 2 /*iRegion*/, VMMDEV_HEAP_SIZE, 0 /*fFlags*/,
4285 (void **)&pThis->pVMMDevHeapR3, "VMMDev Heap");
4286 if (RT_FAILURE(rc))
4287 return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
4288 N_("Failed to allocate %u bytes of memory for the VMM device heap"), PAGE_SIZE);
4289
4290 /* Register the memory area with PDM so HM can access it before it's mapped. */
4291 rc = PDMDevHlpRegisterVMMDevHeap(pDevIns, NIL_RTGCPHYS, pThis->pVMMDevHeapR3, VMMDEV_HEAP_SIZE);
4292 AssertLogRelRCReturn(rc, rc);
4293 }
4294
4295#ifndef VBOX_WITHOUT_TESTING_FEATURES
4296 /*
4297 * Initialize testing.
4298 */
4299 rc = vmmdevTestingInitialize(pDevIns);
4300 if (RT_FAILURE(rc))
4301 return rc;
4302#endif
4303
4304 /*
4305 * Get the corresponding connector interface
4306 */
4307 rc = PDMDevHlpDriverAttach(pDevIns, 0, &pThis->IBase, &pThis->pDrvBase, "VMM Driver Port");
4308 if (RT_SUCCESS(rc))
4309 {
4310 pThis->pDrv = PDMIBASE_QUERY_INTERFACE(pThis->pDrvBase, PDMIVMMDEVCONNECTOR);
4311 AssertMsgReturn(pThis->pDrv, ("LUN #0 doesn't have a VMMDev connector interface!\n"), VERR_PDM_MISSING_INTERFACE);
4312#ifdef VBOX_WITH_HGCM
4313 pThis->pHGCMDrv = PDMIBASE_QUERY_INTERFACE(pThis->pDrvBase, PDMIHGCMCONNECTOR);
4314 if (!pThis->pHGCMDrv)
4315 {
4316 Log(("LUN #0 doesn't have a HGCM connector interface, HGCM is not supported. rc=%Rrc\n", rc));
4317 /* this is not actually an error, just means that there is no support for HGCM */
4318 }
4319#endif
4320 /* Query the initial balloon size. */
4321 AssertPtr(pThis->pDrv->pfnQueryBalloonSize);
4322 rc = pThis->pDrv->pfnQueryBalloonSize(pThis->pDrv, &pThis->cMbMemoryBalloon);
4323 AssertRC(rc);
4324
4325 Log(("Initial balloon size %x\n", pThis->cMbMemoryBalloon));
4326 }
4327 else if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
4328 {
4329 Log(("%s/%d: warning: no driver attached to LUN #0!\n", pDevIns->pReg->szName, pDevIns->iInstance));
4330 rc = VINF_SUCCESS;
4331 }
4332 else
4333 AssertMsgFailedReturn(("Failed to attach LUN #0! rc=%Rrc\n", rc), rc);
4334
4335 /*
4336 * Attach status driver for shared folders (optional).
4337 */
4338 PPDMIBASE pBase;
4339 rc = PDMDevHlpDriverAttach(pDevIns, PDM_STATUS_LUN, &pThis->IBase, &pBase, "Status Port");
4340 if (RT_SUCCESS(rc))
4341 pThis->SharedFolders.pLedsConnector = PDMIBASE_QUERY_INTERFACE(pBase, PDMILEDCONNECTORS);
4342 else if (rc != VERR_PDM_NO_ATTACHED_DRIVER)
4343 {
4344 AssertMsgFailed(("Failed to attach to status driver. rc=%Rrc\n", rc));
4345 return rc;
4346 }
4347
4348 /*
4349 * Register saved state and init the HGCM CmdList critsect.
4350 */
4351 rc = PDMDevHlpSSMRegisterEx(pDevIns, VMMDEV_SAVED_STATE_VERSION, sizeof(*pThis), NULL,
4352 NULL, vmmdevLiveExec, NULL,
4353 NULL, vmmdevSaveExec, NULL,
4354 NULL, vmmdevLoadExec, vmmdevLoadStateDone);
4355 AssertRCReturn(rc, rc);
4356
4357 /*
4358 * Create heartbeat checking timer.
4359 */
4360 rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL, vmmDevHeartbeatFlatlinedTimer, pThis,
4361 TMTIMER_FLAGS_NO_CRIT_SECT, "Heartbeat flatlined", &pThis->pFlatlinedTimer);
4362 AssertRCReturn(rc, rc);
4363
4364#ifdef VBOX_WITH_HGCM
4365 RTListInit(&pThis->listHGCMCmd);
4366 rc = RTCritSectInit(&pThis->critsectHGCMCmdList);
4367 AssertRCReturn(rc, rc);
4368 pThis->u32HGCMEnabled = 0;
4369#endif /* VBOX_WITH_HGCM */
4370
4371 /*
4372 * In this version of VirtualBox the GUI checks whether "needs host cursor"
4373 * changes.
4374 */
4375 pThis->mouseCapabilities |= VMMDEV_MOUSE_HOST_RECHECKS_NEEDS_HOST_CURSOR;
4376
4377 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatMemBalloonChunks, STAMTYPE_U32, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "Memory balloon size", "/Devices/VMMDev/BalloonChunks");
4378
4379 /*
4380 * Generate a unique session id for this VM; it will be changed for each
4381 * start, reset or restore. This can be used for restore detection inside
4382 * the guest.
4383 */
4384 pThis->idSession = ASMReadTSC();
4385 return rc;
4386}
4387
4388/**
4389 * The device registration structure.
4390 */
4391extern "C" const PDMDEVREG g_DeviceVMMDev =
4392{
4393 /* u32Version */
4394 PDM_DEVREG_VERSION,
4395 /* szName */
4396 "VMMDev",
4397 /* szRCMod */
4398 "VBoxDDRC.rc",
4399 /* szR0Mod */
4400 "VBoxDDR0.r0",
4401 /* pszDescription */
4402 "VirtualBox VMM Device\n",
4403 /* fFlags */
4404 PDM_DEVREG_FLAGS_HOST_BITS_DEFAULT | PDM_DEVREG_FLAGS_GUEST_BITS_DEFAULT | PDM_DEVREG_FLAGS_RC | PDM_DEVREG_FLAGS_R0,
4405 /* fClass */
4406 PDM_DEVREG_CLASS_VMM_DEV,
4407 /* cMaxInstances */
4408 1,
4409 /* cbInstance */
4410 sizeof(VMMDevState),
4411 /* pfnConstruct */
4412 vmmdevConstruct,
4413 /* pfnDestruct */
4414 vmmdevDestruct,
4415 /* pfnRelocate */
4416 vmmdevRelocate,
4417 /* pfnMemSetup */
4418 NULL,
4419 /* pfnPowerOn */
4420 NULL,
4421 /* pfnReset */
4422 vmmdevReset,
4423 /* pfnSuspend */
4424 NULL,
4425 /* pfnResume */
4426 NULL,
4427 /* pfnAttach */
4428 NULL,
4429 /* pfnDetach */
4430 NULL,
4431 /* pfnQueryInterface. */
4432 NULL,
4433 /* pfnInitComplete */
4434 NULL,
4435 /* pfnPowerOff */
4436 NULL,
4437 /* pfnSoftReset */
4438 NULL,
4439 /* u32VersionEnd */
4440 PDM_DEVREG_VERSION
4441};
4442#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
4443
Note: See TracBrowser for help on using the repository browser.

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