VirtualBox

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

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

VBoxGuest,VMMDev,DBGF,VM: Added bug check report to VBoxGuest/VMMDev and hooked it up to DBGF. Made DBGF remember the last reported bug check, adding an info handler for displaying it. Added VM reset counters w/ getters for use in bug check reporting.

  • 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 73351 2018-07-25 13:02:11Z 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.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (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
1567 /* Fill the guest request with monitor layout data. */
1568 for (i = 0; i < cDisplays; ++i)
1569 {
1570 /* If not a response to a VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST, just
1571 * read the last valid video mode hint. This happens when the guest X server
1572 * determines the initial mode. */
1573 DISPLAYCHANGEREQUEST const *pDCR = &pThis->displayChangeData.aRequests[i];
1574 VMMDevDisplayDef const *pDisplayDef = pThis->displayChangeData.fGuestSentChangeEventAck ?
1575 &pDCR->lastReadDisplayChangeRequest :
1576 &pDCR->displayChangeRequest;
1577 pReq->aDisplays[i] = *pDisplayDef;
1578 }
1579
1580 Log(("VMMDev: returning multimonitor display change request cDisplays %d\n", cDisplays));
1581
1582 return VINF_SUCCESS;
1583}
1584
1585
1586/**
1587 * Handles VMMDevReq_VideoModeSupported.
1588 *
1589 * Query whether the given video mode is supported.
1590 *
1591 * @returns VBox status code that the guest should see.
1592 * @param pThis The VMMDev instance data.
1593 * @param pReqHdr The header of the request to handle.
1594 */
1595static int vmmdevReqHandler_VideoModeSupported(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
1596{
1597 VMMDevVideoModeSupportedRequest *pReq = (VMMDevVideoModeSupportedRequest *)pReqHdr;
1598 AssertMsgReturn(pReq->header.size == sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
1599
1600 /* forward the call */
1601 return pThis->pDrv->pfnVideoModeSupported(pThis->pDrv,
1602 0, /* primary screen. */
1603 pReq->width,
1604 pReq->height,
1605 pReq->bpp,
1606 &pReq->fSupported);
1607}
1608
1609
1610/**
1611 * Handles VMMDevReq_VideoModeSupported2.
1612 *
1613 * Query whether the given video mode is supported for a specific display
1614 *
1615 * @returns VBox status code that the guest should see.
1616 * @param pThis The VMMDev instance data.
1617 * @param pReqHdr The header of the request to handle.
1618 */
1619static int vmmdevReqHandler_VideoModeSupported2(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
1620{
1621 VMMDevVideoModeSupportedRequest2 *pReq = (VMMDevVideoModeSupportedRequest2 *)pReqHdr;
1622 AssertMsgReturn(pReq->header.size == sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
1623
1624 /* forward the call */
1625 return pThis->pDrv->pfnVideoModeSupported(pThis->pDrv,
1626 pReq->display,
1627 pReq->width,
1628 pReq->height,
1629 pReq->bpp,
1630 &pReq->fSupported);
1631}
1632
1633
1634
1635/**
1636 * Handles VMMDevReq_GetHeightReduction.
1637 *
1638 * @returns VBox status code that the guest should see.
1639 * @param pThis The VMMDev instance data.
1640 * @param pReqHdr The header of the request to handle.
1641 */
1642static int vmmdevReqHandler_GetHeightReduction(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
1643{
1644 VMMDevGetHeightReductionRequest *pReq = (VMMDevGetHeightReductionRequest *)pReqHdr;
1645 AssertMsgReturn(pReq->header.size == sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
1646
1647 /* forward the call */
1648 return pThis->pDrv->pfnGetHeightReduction(pThis->pDrv, &pReq->heightReduction);
1649}
1650
1651
1652/**
1653 * Handles VMMDevReq_AcknowledgeEvents.
1654 *
1655 * @returns VBox status code that the guest should see.
1656 * @param pThis The VMMDev instance data.
1657 * @param pReqHdr The header of the request to handle.
1658 */
1659static int vmmdevReqHandler_AcknowledgeEvents(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
1660{
1661 VMMDevEvents *pReq = (VMMDevEvents *)pReqHdr;
1662 AssertMsgReturn(pReq->header.size == sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
1663
1664 if (!VMMDEV_INTERFACE_VERSION_IS_1_03(pThis))
1665 {
1666 if (pThis->fNewGuestFilterMask)
1667 {
1668 pThis->fNewGuestFilterMask = false;
1669 pThis->u32GuestFilterMask = pThis->u32NewGuestFilterMask;
1670 }
1671
1672 pReq->events = pThis->u32HostEventFlags & pThis->u32GuestFilterMask;
1673
1674 pThis->u32HostEventFlags &= ~pThis->u32GuestFilterMask;
1675 pThis->pVMMDevRAMR3->V.V1_04.fHaveEvents = false;
1676 PDMDevHlpPCISetIrqNoWait(pThis->pDevIns, 0, 0);
1677 }
1678 else
1679 vmmdevSetIRQ_Legacy(pThis);
1680 return VINF_SUCCESS;
1681}
1682
1683
1684/**
1685 * Handles VMMDevReq_CtlGuestFilterMask.
1686 *
1687 * @returns VBox status code that the guest should see.
1688 * @param pThis The VMMDev instance data.
1689 * @param pReqHdr The header of the request to handle.
1690 */
1691static int vmmdevReqHandler_CtlGuestFilterMask(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
1692{
1693 VMMDevCtlGuestFilterMask *pReq = (VMMDevCtlGuestFilterMask *)pReqHdr;
1694 AssertMsgReturn(pReq->header.size == sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
1695
1696 LogRelFlow(("VMMDev: vmmdevReqHandler_CtlGuestFilterMask: OR mask: %#x, NOT mask: %#x\n", pReq->u32OrMask, pReq->u32NotMask));
1697
1698 /* HGCM event notification is enabled by the VMMDev device
1699 * automatically when any HGCM command is issued. The guest
1700 * cannot disable these notifications. */
1701 VMMDevCtlSetGuestFilterMask(pThis, pReq->u32OrMask, pReq->u32NotMask & ~VMMDEV_EVENT_HGCM);
1702 return VINF_SUCCESS;
1703}
1704
1705#ifdef VBOX_WITH_HGCM
1706
1707/**
1708 * Handles VMMDevReq_HGCMConnect.
1709 *
1710 * @returns VBox status code that the guest should see.
1711 * @param pThis The VMMDev instance data.
1712 * @param pReqHdr The header of the request to handle.
1713 * @param GCPhysReqHdr The guest physical address of the request header.
1714 */
1715static int vmmdevReqHandler_HGCMConnect(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr, RTGCPHYS GCPhysReqHdr)
1716{
1717 VMMDevHGCMConnect *pReq = (VMMDevHGCMConnect *)pReqHdr;
1718 AssertMsgReturn(pReq->header.header.size >= sizeof(*pReq), ("%u\n", pReq->header.header.size), VERR_INVALID_PARAMETER); /** @todo Not sure why this is >= ... */
1719
1720 if (pThis->pHGCMDrv)
1721 {
1722 Log(("VMMDevReq_HGCMConnect\n"));
1723 return vmmdevHGCMConnect(pThis, pReq, GCPhysReqHdr);
1724 }
1725
1726 Log(("VMMDevReq_HGCMConnect: HGCM Connector is NULL!\n"));
1727 return VERR_NOT_SUPPORTED;
1728}
1729
1730
1731/**
1732 * Handles VMMDevReq_HGCMDisconnect.
1733 *
1734 * @returns VBox status code that the guest should see.
1735 * @param pThis The VMMDev instance data.
1736 * @param pReqHdr The header of the request to handle.
1737 * @param GCPhysReqHdr The guest physical address of the request header.
1738 */
1739static int vmmdevReqHandler_HGCMDisconnect(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr, RTGCPHYS GCPhysReqHdr)
1740{
1741 VMMDevHGCMDisconnect *pReq = (VMMDevHGCMDisconnect *)pReqHdr;
1742 AssertMsgReturn(pReq->header.header.size >= sizeof(*pReq), ("%u\n", pReq->header.header.size), VERR_INVALID_PARAMETER); /** @todo Not sure why this >= ... */
1743
1744 if (pThis->pHGCMDrv)
1745 {
1746 Log(("VMMDevReq_VMMDevHGCMDisconnect\n"));
1747 return vmmdevHGCMDisconnect(pThis, pReq, GCPhysReqHdr);
1748 }
1749
1750 Log(("VMMDevReq_VMMDevHGCMDisconnect: HGCM Connector is NULL!\n"));
1751 return VERR_NOT_SUPPORTED;
1752}
1753
1754
1755/**
1756 * Handles VMMDevReq_HGCMCall.
1757 *
1758 * @returns VBox status code that the guest should see.
1759 * @param pThis The VMMDev instance data.
1760 * @param pReqHdr The header of the request to handle.
1761 * @param GCPhysReqHdr The guest physical address of the request header.
1762 */
1763static int vmmdevReqHandler_HGCMCall(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr, RTGCPHYS GCPhysReqHdr)
1764{
1765 VMMDevHGCMCall *pReq = (VMMDevHGCMCall *)pReqHdr;
1766 AssertMsgReturn(pReq->header.header.size >= sizeof(*pReq), ("%u\n", pReq->header.header.size), VERR_INVALID_PARAMETER);
1767
1768 if (pThis->pHGCMDrv)
1769 {
1770 Log2(("VMMDevReq_HGCMCall: sizeof(VMMDevHGCMRequest) = %04X\n", sizeof(VMMDevHGCMCall)));
1771 Log2(("%.*Rhxd\n", pReq->header.header.size, pReq));
1772
1773 return vmmdevHGCMCall(pThis, pReq, pReq->header.header.size, GCPhysReqHdr, pReq->header.header.requestType);
1774 }
1775
1776 Log(("VMMDevReq_HGCMCall: HGCM Connector is NULL!\n"));
1777 return VERR_NOT_SUPPORTED;
1778}
1779
1780/**
1781 * Handles VMMDevReq_HGCMCancel.
1782 *
1783 * @returns VBox status code that the guest should see.
1784 * @param pThis The VMMDev instance data.
1785 * @param pReqHdr The header of the request to handle.
1786 * @param GCPhysReqHdr The guest physical address of the request header.
1787 */
1788static int vmmdevReqHandler_HGCMCancel(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr, RTGCPHYS GCPhysReqHdr)
1789{
1790 VMMDevHGCMCancel *pReq = (VMMDevHGCMCancel *)pReqHdr;
1791 AssertMsgReturn(pReq->header.header.size >= sizeof(*pReq), ("%u\n", pReq->header.header.size), VERR_INVALID_PARAMETER); /** @todo Not sure why this >= ... */
1792
1793 if (pThis->pHGCMDrv)
1794 {
1795 Log(("VMMDevReq_VMMDevHGCMCancel\n"));
1796 return vmmdevHGCMCancel(pThis, pReq, GCPhysReqHdr);
1797 }
1798
1799 Log(("VMMDevReq_VMMDevHGCMCancel: HGCM Connector is NULL!\n"));
1800 return VERR_NOT_SUPPORTED;
1801}
1802
1803
1804/**
1805 * Handles VMMDevReq_HGCMCancel2.
1806 *
1807 * @returns VBox status code that the guest should see.
1808 * @param pThis The VMMDev instance data.
1809 * @param pReqHdr The header of the request to handle.
1810 */
1811static int vmmdevReqHandler_HGCMCancel2(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
1812{
1813 VMMDevHGCMCancel2 *pReq = (VMMDevHGCMCancel2 *)pReqHdr;
1814 AssertMsgReturn(pReq->header.size >= sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER); /** @todo Not sure why this >= ... */
1815
1816 if (pThis->pHGCMDrv)
1817 {
1818 Log(("VMMDevReq_HGCMCancel2\n"));
1819 return vmmdevHGCMCancel2(pThis, pReq->physReqToCancel);
1820 }
1821
1822 Log(("VMMDevReq_HGCMConnect2: HGCM Connector is NULL!\n"));
1823 return VERR_NOT_SUPPORTED;
1824}
1825
1826#endif /* VBOX_WITH_HGCM */
1827
1828
1829/**
1830 * Handles VMMDevReq_VideoAccelEnable.
1831 *
1832 * @returns VBox status code that the guest should see.
1833 * @param pThis The VMMDev instance data.
1834 * @param pReqHdr The header of the request to handle.
1835 */
1836static int vmmdevReqHandler_VideoAccelEnable(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
1837{
1838 VMMDevVideoAccelEnable *pReq = (VMMDevVideoAccelEnable *)pReqHdr;
1839 AssertMsgReturn(pReq->header.size >= sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER); /** @todo Not sure why this >= ... */
1840
1841 if (!pThis->pDrv)
1842 {
1843 Log(("VMMDevReq_VideoAccelEnable Connector is NULL!!\n"));
1844 return VERR_NOT_SUPPORTED;
1845 }
1846
1847 if (pReq->cbRingBuffer != VMMDEV_VBVA_RING_BUFFER_SIZE)
1848 {
1849 /* The guest driver seems compiled with different headers. */
1850 LogRelMax(16,("VMMDevReq_VideoAccelEnable guest ring buffer size %#x, should be %#x!!\n", pReq->cbRingBuffer, VMMDEV_VBVA_RING_BUFFER_SIZE));
1851 return VERR_INVALID_PARAMETER;
1852 }
1853
1854 /* The request is correct. */
1855 pReq->fu32Status |= VBVA_F_STATUS_ACCEPTED;
1856
1857 LogFlow(("VMMDevReq_VideoAccelEnable pReq->u32Enable = %d\n", pReq->u32Enable));
1858
1859 int rc = pReq->u32Enable
1860 ? pThis->pDrv->pfnVideoAccelEnable(pThis->pDrv, true, &pThis->pVMMDevRAMR3->vbvaMemory)
1861 : pThis->pDrv->pfnVideoAccelEnable(pThis->pDrv, false, NULL);
1862
1863 if ( pReq->u32Enable
1864 && RT_SUCCESS(rc))
1865 {
1866 pReq->fu32Status |= VBVA_F_STATUS_ENABLED;
1867
1868 /* Remember that guest successfully enabled acceleration.
1869 * We need to reestablish it on restoring the VM from saved state.
1870 */
1871 pThis->u32VideoAccelEnabled = 1;
1872 }
1873 else
1874 {
1875 /* The acceleration was not enabled. Remember that. */
1876 pThis->u32VideoAccelEnabled = 0;
1877 }
1878 return VINF_SUCCESS;
1879}
1880
1881
1882/**
1883 * Handles VMMDevReq_VideoAccelFlush.
1884 *
1885 * @returns VBox status code that the guest should see.
1886 * @param pThis The VMMDev instance data.
1887 * @param pReqHdr The header of the request to handle.
1888 */
1889static int vmmdevReqHandler_VideoAccelFlush(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
1890{
1891 VMMDevVideoAccelFlush *pReq = (VMMDevVideoAccelFlush *)pReqHdr;
1892 AssertMsgReturn(pReq->header.size >= sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER); /** @todo Not sure why this >= ... */
1893
1894 if (!pThis->pDrv)
1895 {
1896 Log(("VMMDevReq_VideoAccelFlush: Connector is NULL!!!\n"));
1897 return VERR_NOT_SUPPORTED;
1898 }
1899
1900 pThis->pDrv->pfnVideoAccelFlush(pThis->pDrv);
1901 return VINF_SUCCESS;
1902}
1903
1904
1905/**
1906 * Handles VMMDevReq_VideoSetVisibleRegion.
1907 *
1908 * @returns VBox status code that the guest should see.
1909 * @param pThis The VMMDev instance data.
1910 * @param pReqHdr The header of the request to handle.
1911 */
1912static int vmmdevReqHandler_VideoSetVisibleRegion(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
1913{
1914 VMMDevVideoSetVisibleRegion *pReq = (VMMDevVideoSetVisibleRegion *)pReqHdr;
1915 AssertMsgReturn(pReq->header.size + sizeof(RTRECT) >= sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
1916
1917 if (!pThis->pDrv)
1918 {
1919 Log(("VMMDevReq_VideoSetVisibleRegion: Connector is NULL!!!\n"));
1920 return VERR_NOT_SUPPORTED;
1921 }
1922
1923 if ( pReq->cRect > _1M /* restrict to sane range */
1924 || pReq->header.size != sizeof(VMMDevVideoSetVisibleRegion) + pReq->cRect * sizeof(RTRECT) - sizeof(RTRECT))
1925 {
1926 Log(("VMMDevReq_VideoSetVisibleRegion: cRects=%#x doesn't match size=%#x or is out of bounds\n",
1927 pReq->cRect, pReq->header.size));
1928 return VERR_INVALID_PARAMETER;
1929 }
1930
1931 Log(("VMMDevReq_VideoSetVisibleRegion %d rectangles\n", pReq->cRect));
1932 /* forward the call */
1933 return pThis->pDrv->pfnSetVisibleRegion(pThis->pDrv, pReq->cRect, &pReq->Rect);
1934}
1935
1936
1937/**
1938 * Handles VMMDevReq_GetSeamlessChangeRequest.
1939 *
1940 * @returns VBox status code that the guest should see.
1941 * @param pThis The VMMDev instance data.
1942 * @param pReqHdr The header of the request to handle.
1943 */
1944static int vmmdevReqHandler_GetSeamlessChangeRequest(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
1945{
1946 VMMDevSeamlessChangeRequest *pReq = (VMMDevSeamlessChangeRequest *)pReqHdr;
1947 AssertMsgReturn(pReq->header.size == sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
1948
1949 /* just pass on the information */
1950 Log(("VMMDev: returning seamless change request mode=%d\n", pThis->fSeamlessEnabled));
1951 if (pThis->fSeamlessEnabled)
1952 pReq->mode = VMMDev_Seamless_Visible_Region;
1953 else
1954 pReq->mode = VMMDev_Seamless_Disabled;
1955
1956 if (pReq->eventAck == VMMDEV_EVENT_SEAMLESS_MODE_CHANGE_REQUEST)
1957 {
1958 /* Remember which mode the client has queried. */
1959 pThis->fLastSeamlessEnabled = pThis->fSeamlessEnabled;
1960 }
1961
1962 return VINF_SUCCESS;
1963}
1964
1965
1966/**
1967 * Handles VMMDevReq_GetVRDPChangeRequest.
1968 *
1969 * @returns VBox status code that the guest should see.
1970 * @param pThis The VMMDev instance data.
1971 * @param pReqHdr The header of the request to handle.
1972 */
1973static int vmmdevReqHandler_GetVRDPChangeRequest(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
1974{
1975 VMMDevVRDPChangeRequest *pReq = (VMMDevVRDPChangeRequest *)pReqHdr;
1976 AssertMsgReturn(pReq->header.size == sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
1977
1978 /* just pass on the information */
1979 Log(("VMMDev: returning VRDP status %d level %d\n", pThis->fVRDPEnabled, pThis->uVRDPExperienceLevel));
1980
1981 pReq->u8VRDPActive = pThis->fVRDPEnabled;
1982 pReq->u32VRDPExperienceLevel = pThis->uVRDPExperienceLevel;
1983
1984 return VINF_SUCCESS;
1985}
1986
1987
1988/**
1989 * Handles VMMDevReq_GetMemBalloonChangeRequest.
1990 *
1991 * @returns VBox status code that the guest should see.
1992 * @param pThis The VMMDev instance data.
1993 * @param pReqHdr The header of the request to handle.
1994 */
1995static int vmmdevReqHandler_GetMemBalloonChangeRequest(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
1996{
1997 VMMDevGetMemBalloonChangeRequest *pReq = (VMMDevGetMemBalloonChangeRequest *)pReqHdr;
1998 AssertMsgReturn(pReq->header.size == sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
1999
2000 /* just pass on the information */
2001 Log(("VMMDev: returning memory balloon size =%d\n", pThis->cMbMemoryBalloon));
2002 pReq->cBalloonChunks = pThis->cMbMemoryBalloon;
2003 pReq->cPhysMemChunks = pThis->cbGuestRAM / (uint64_t)_1M;
2004
2005 if (pReq->eventAck == VMMDEV_EVENT_BALLOON_CHANGE_REQUEST)
2006 {
2007 /* Remember which mode the client has queried. */
2008 pThis->cMbMemoryBalloonLast = pThis->cMbMemoryBalloon;
2009 }
2010
2011 return VINF_SUCCESS;
2012}
2013
2014
2015/**
2016 * Handles VMMDevReq_ChangeMemBalloon.
2017 *
2018 * @returns VBox status code that the guest should see.
2019 * @param pThis The VMMDev instance data.
2020 * @param pReqHdr The header of the request to handle.
2021 */
2022static int vmmdevReqHandler_ChangeMemBalloon(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
2023{
2024 VMMDevChangeMemBalloon *pReq = (VMMDevChangeMemBalloon *)pReqHdr;
2025 AssertMsgReturn(pReq->header.size >= sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
2026 AssertMsgReturn(pReq->cPages == VMMDEV_MEMORY_BALLOON_CHUNK_PAGES, ("%u\n", pReq->cPages), VERR_INVALID_PARAMETER);
2027 AssertMsgReturn(pReq->header.size == (uint32_t)RT_UOFFSETOF_DYN(VMMDevChangeMemBalloon, aPhysPage[pReq->cPages]),
2028 ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
2029
2030 Log(("VMMDevReq_ChangeMemBalloon\n"));
2031 int rc = PGMR3PhysChangeMemBalloon(PDMDevHlpGetVM(pThis->pDevIns), !!pReq->fInflate, pReq->cPages, pReq->aPhysPage);
2032 if (pReq->fInflate)
2033 STAM_REL_U32_INC(&pThis->StatMemBalloonChunks);
2034 else
2035 STAM_REL_U32_DEC(&pThis->StatMemBalloonChunks);
2036 return rc;
2037}
2038
2039
2040/**
2041 * Handles VMMDevReq_GetStatisticsChangeRequest.
2042 *
2043 * @returns VBox status code that the guest should see.
2044 * @param pThis The VMMDev instance data.
2045 * @param pReqHdr The header of the request to handle.
2046 */
2047static int vmmdevReqHandler_GetStatisticsChangeRequest(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
2048{
2049 VMMDevGetStatisticsChangeRequest *pReq = (VMMDevGetStatisticsChangeRequest *)pReqHdr;
2050 AssertMsgReturn(pReq->header.size == sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
2051
2052 Log(("VMMDevReq_GetStatisticsChangeRequest\n"));
2053 /* just pass on the information */
2054 Log(("VMMDev: returning statistics interval %d seconds\n", pThis->u32StatIntervalSize));
2055 pReq->u32StatInterval = pThis->u32StatIntervalSize;
2056
2057 if (pReq->eventAck == VMMDEV_EVENT_STATISTICS_INTERVAL_CHANGE_REQUEST)
2058 {
2059 /* Remember which mode the client has queried. */
2060 pThis->u32LastStatIntervalSize= pThis->u32StatIntervalSize;
2061 }
2062
2063 return VINF_SUCCESS;
2064}
2065
2066
2067/**
2068 * Handles VMMDevReq_ReportGuestStats.
2069 *
2070 * @returns VBox status code that the guest should see.
2071 * @param pThis The VMMDev instance data.
2072 * @param pReqHdr The header of the request to handle.
2073 */
2074static int vmmdevReqHandler_ReportGuestStats(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
2075{
2076 VMMDevReportGuestStats *pReq = (VMMDevReportGuestStats *)pReqHdr;
2077 AssertMsgReturn(pReq->header.size == sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
2078
2079 Log(("VMMDevReq_ReportGuestStats\n"));
2080#ifdef LOG_ENABLED
2081 VBoxGuestStatistics *pGuestStats = &pReq->guestStats;
2082
2083 Log(("Current statistics:\n"));
2084 if (pGuestStats->u32StatCaps & VBOX_GUEST_STAT_CPU_LOAD_IDLE)
2085 Log(("CPU%u: CPU Load Idle %-3d%%\n", pGuestStats->u32CpuId, pGuestStats->u32CpuLoad_Idle));
2086
2087 if (pGuestStats->u32StatCaps & VBOX_GUEST_STAT_CPU_LOAD_KERNEL)
2088 Log(("CPU%u: CPU Load Kernel %-3d%%\n", pGuestStats->u32CpuId, pGuestStats->u32CpuLoad_Kernel));
2089
2090 if (pGuestStats->u32StatCaps & VBOX_GUEST_STAT_CPU_LOAD_USER)
2091 Log(("CPU%u: CPU Load User %-3d%%\n", pGuestStats->u32CpuId, pGuestStats->u32CpuLoad_User));
2092
2093 if (pGuestStats->u32StatCaps & VBOX_GUEST_STAT_THREADS)
2094 Log(("CPU%u: Thread %d\n", pGuestStats->u32CpuId, pGuestStats->u32Threads));
2095
2096 if (pGuestStats->u32StatCaps & VBOX_GUEST_STAT_PROCESSES)
2097 Log(("CPU%u: Processes %d\n", pGuestStats->u32CpuId, pGuestStats->u32Processes));
2098
2099 if (pGuestStats->u32StatCaps & VBOX_GUEST_STAT_HANDLES)
2100 Log(("CPU%u: Handles %d\n", pGuestStats->u32CpuId, pGuestStats->u32Handles));
2101
2102 if (pGuestStats->u32StatCaps & VBOX_GUEST_STAT_MEMORY_LOAD)
2103 Log(("CPU%u: Memory Load %d%%\n", pGuestStats->u32CpuId, pGuestStats->u32MemoryLoad));
2104
2105 /* Note that reported values are in pages; upper layers expect them in megabytes */
2106 Log(("CPU%u: Page size %-4d bytes\n", pGuestStats->u32CpuId, pGuestStats->u32PageSize));
2107 Assert(pGuestStats->u32PageSize == 4096);
2108
2109 if (pGuestStats->u32StatCaps & VBOX_GUEST_STAT_PHYS_MEM_TOTAL)
2110 Log(("CPU%u: Total physical memory %-4d MB\n", pGuestStats->u32CpuId, (pGuestStats->u32PhysMemTotal + (_1M/_4K)-1) / (_1M/_4K)));
2111
2112 if (pGuestStats->u32StatCaps & VBOX_GUEST_STAT_PHYS_MEM_AVAIL)
2113 Log(("CPU%u: Free physical memory %-4d MB\n", pGuestStats->u32CpuId, pGuestStats->u32PhysMemAvail / (_1M/_4K)));
2114
2115 if (pGuestStats->u32StatCaps & VBOX_GUEST_STAT_PHYS_MEM_BALLOON)
2116 Log(("CPU%u: Memory balloon size %-4d MB\n", pGuestStats->u32CpuId, pGuestStats->u32PhysMemBalloon / (_1M/_4K)));
2117
2118 if (pGuestStats->u32StatCaps & VBOX_GUEST_STAT_MEM_COMMIT_TOTAL)
2119 Log(("CPU%u: Committed memory %-4d MB\n", pGuestStats->u32CpuId, pGuestStats->u32MemCommitTotal / (_1M/_4K)));
2120
2121 if (pGuestStats->u32StatCaps & VBOX_GUEST_STAT_MEM_KERNEL_TOTAL)
2122 Log(("CPU%u: Total kernel memory %-4d MB\n", pGuestStats->u32CpuId, pGuestStats->u32MemKernelTotal / (_1M/_4K)));
2123
2124 if (pGuestStats->u32StatCaps & VBOX_GUEST_STAT_MEM_KERNEL_PAGED)
2125 Log(("CPU%u: Paged kernel memory %-4d MB\n", pGuestStats->u32CpuId, pGuestStats->u32MemKernelPaged / (_1M/_4K)));
2126
2127 if (pGuestStats->u32StatCaps & VBOX_GUEST_STAT_MEM_KERNEL_NONPAGED)
2128 Log(("CPU%u: Nonpaged kernel memory %-4d MB\n", pGuestStats->u32CpuId, pGuestStats->u32MemKernelNonPaged / (_1M/_4K)));
2129
2130 if (pGuestStats->u32StatCaps & VBOX_GUEST_STAT_MEM_SYSTEM_CACHE)
2131 Log(("CPU%u: System cache size %-4d MB\n", pGuestStats->u32CpuId, pGuestStats->u32MemSystemCache / (_1M/_4K)));
2132
2133 if (pGuestStats->u32StatCaps & VBOX_GUEST_STAT_PAGE_FILE_SIZE)
2134 Log(("CPU%u: Page file size %-4d MB\n", pGuestStats->u32CpuId, pGuestStats->u32PageFileSize / (_1M/_4K)));
2135 Log(("Statistics end *******************\n"));
2136#endif /* LOG_ENABLED */
2137
2138 /* forward the call */
2139 return pThis->pDrv->pfnReportStatistics(pThis->pDrv, &pReq->guestStats);
2140}
2141
2142
2143/**
2144 * Handles VMMDevReq_QueryCredentials.
2145 *
2146 * @returns VBox status code that the guest should see.
2147 * @param pThis The VMMDev instance data.
2148 * @param pReqHdr The header of the request to handle.
2149 */
2150static int vmmdevReqHandler_QueryCredentials(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
2151{
2152 VMMDevCredentials *pReq = (VMMDevCredentials *)pReqHdr;
2153 AssertMsgReturn(pReq->header.size == sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
2154
2155 /* let's start by nulling out the data */
2156 memset(pReq->szUserName, '\0', VMMDEV_CREDENTIALS_SZ_SIZE);
2157 memset(pReq->szPassword, '\0', VMMDEV_CREDENTIALS_SZ_SIZE);
2158 memset(pReq->szDomain, '\0', VMMDEV_CREDENTIALS_SZ_SIZE);
2159
2160 /* should we return whether we got credentials for a logon? */
2161 if (pReq->u32Flags & VMMDEV_CREDENTIALS_QUERYPRESENCE)
2162 {
2163 if ( pThis->pCredentials->Logon.szUserName[0]
2164 || pThis->pCredentials->Logon.szPassword[0]
2165 || pThis->pCredentials->Logon.szDomain[0])
2166 pReq->u32Flags |= VMMDEV_CREDENTIALS_PRESENT;
2167 else
2168 pReq->u32Flags &= ~VMMDEV_CREDENTIALS_PRESENT;
2169 }
2170
2171 /* does the guest want to read logon credentials? */
2172 if (pReq->u32Flags & VMMDEV_CREDENTIALS_READ)
2173 {
2174 if (pThis->pCredentials->Logon.szUserName[0])
2175 strcpy(pReq->szUserName, pThis->pCredentials->Logon.szUserName);
2176 if (pThis->pCredentials->Logon.szPassword[0])
2177 strcpy(pReq->szPassword, pThis->pCredentials->Logon.szPassword);
2178 if (pThis->pCredentials->Logon.szDomain[0])
2179 strcpy(pReq->szDomain, pThis->pCredentials->Logon.szDomain);
2180 if (!pThis->pCredentials->Logon.fAllowInteractiveLogon)
2181 pReq->u32Flags |= VMMDEV_CREDENTIALS_NOLOCALLOGON;
2182 else
2183 pReq->u32Flags &= ~VMMDEV_CREDENTIALS_NOLOCALLOGON;
2184 }
2185
2186 if (!pThis->fKeepCredentials)
2187 {
2188 /* does the caller want us to destroy the logon credentials? */
2189 if (pReq->u32Flags & VMMDEV_CREDENTIALS_CLEAR)
2190 {
2191 memset(pThis->pCredentials->Logon.szUserName, '\0', VMMDEV_CREDENTIALS_SZ_SIZE);
2192 memset(pThis->pCredentials->Logon.szPassword, '\0', VMMDEV_CREDENTIALS_SZ_SIZE);
2193 memset(pThis->pCredentials->Logon.szDomain, '\0', VMMDEV_CREDENTIALS_SZ_SIZE);
2194 }
2195 }
2196
2197 /* does the guest want to read credentials for verification? */
2198 if (pReq->u32Flags & VMMDEV_CREDENTIALS_READJUDGE)
2199 {
2200 if (pThis->pCredentials->Judge.szUserName[0])
2201 strcpy(pReq->szUserName, pThis->pCredentials->Judge.szUserName);
2202 if (pThis->pCredentials->Judge.szPassword[0])
2203 strcpy(pReq->szPassword, pThis->pCredentials->Judge.szPassword);
2204 if (pThis->pCredentials->Judge.szDomain[0])
2205 strcpy(pReq->szDomain, pThis->pCredentials->Judge.szDomain);
2206 }
2207
2208 /* does the caller want us to destroy the judgement credentials? */
2209 if (pReq->u32Flags & VMMDEV_CREDENTIALS_CLEARJUDGE)
2210 {
2211 memset(pThis->pCredentials->Judge.szUserName, '\0', VMMDEV_CREDENTIALS_SZ_SIZE);
2212 memset(pThis->pCredentials->Judge.szPassword, '\0', VMMDEV_CREDENTIALS_SZ_SIZE);
2213 memset(pThis->pCredentials->Judge.szDomain, '\0', VMMDEV_CREDENTIALS_SZ_SIZE);
2214 }
2215
2216 return VINF_SUCCESS;
2217}
2218
2219
2220/**
2221 * Handles VMMDevReq_ReportCredentialsJudgement.
2222 *
2223 * @returns VBox status code that the guest should see.
2224 * @param pThis The VMMDev instance data.
2225 * @param pReqHdr The header of the request to handle.
2226 */
2227static int vmmdevReqHandler_ReportCredentialsJudgement(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
2228{
2229 VMMDevCredentials *pReq = (VMMDevCredentials *)pReqHdr;
2230 AssertMsgReturn(pReq->header.size == sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
2231
2232 /* what does the guest think about the credentials? (note: the order is important here!) */
2233 if (pReq->u32Flags & VMMDEV_CREDENTIALS_JUDGE_DENY)
2234 pThis->pDrv->pfnSetCredentialsJudgementResult(pThis->pDrv, VMMDEV_CREDENTIALS_JUDGE_DENY);
2235 else if (pReq->u32Flags & VMMDEV_CREDENTIALS_JUDGE_NOJUDGEMENT)
2236 pThis->pDrv->pfnSetCredentialsJudgementResult(pThis->pDrv, VMMDEV_CREDENTIALS_JUDGE_NOJUDGEMENT);
2237 else if (pReq->u32Flags & VMMDEV_CREDENTIALS_JUDGE_OK)
2238 pThis->pDrv->pfnSetCredentialsJudgementResult(pThis->pDrv, VMMDEV_CREDENTIALS_JUDGE_OK);
2239 else
2240 {
2241 Log(("VMMDevReq_ReportCredentialsJudgement: invalid flags: %d!!!\n", pReq->u32Flags));
2242 /** @todo why don't we return VERR_INVALID_PARAMETER to the guest? */
2243 }
2244
2245 return VINF_SUCCESS;
2246}
2247
2248
2249/**
2250 * Handles VMMDevReq_GetHostVersion.
2251 *
2252 * @returns VBox status code that the guest should see.
2253 * @param pReqHdr The header of the request to handle.
2254 * @since 3.1.0
2255 * @note The ring-0 VBoxGuestLib uses this to check whether
2256 * VMMDevHGCMParmType_PageList is supported.
2257 */
2258static int vmmdevReqHandler_GetHostVersion(VMMDevRequestHeader *pReqHdr)
2259{
2260 VMMDevReqHostVersion *pReq = (VMMDevReqHostVersion *)pReqHdr;
2261 AssertMsgReturn(pReq->header.size == sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
2262
2263 pReq->major = RTBldCfgVersionMajor();
2264 pReq->minor = RTBldCfgVersionMinor();
2265 pReq->build = RTBldCfgVersionBuild();
2266 pReq->revision = RTBldCfgRevision();
2267 pReq->features = VMMDEV_HVF_HGCM_PHYS_PAGE_LIST;
2268 return VINF_SUCCESS;
2269}
2270
2271
2272/**
2273 * Handles VMMDevReq_GetCpuHotPlugRequest.
2274 *
2275 * @returns VBox status code that the guest should see.
2276 * @param pThis The VMMDev instance data.
2277 * @param pReqHdr The header of the request to handle.
2278 */
2279static int vmmdevReqHandler_GetCpuHotPlugRequest(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
2280{
2281 VMMDevGetCpuHotPlugRequest *pReq = (VMMDevGetCpuHotPlugRequest *)pReqHdr;
2282 AssertMsgReturn(pReq->header.size == sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
2283
2284 pReq->enmEventType = pThis->enmCpuHotPlugEvent;
2285 pReq->idCpuCore = pThis->idCpuCore;
2286 pReq->idCpuPackage = pThis->idCpuPackage;
2287
2288 /* Clear the event */
2289 pThis->enmCpuHotPlugEvent = VMMDevCpuEventType_None;
2290 pThis->idCpuCore = UINT32_MAX;
2291 pThis->idCpuPackage = UINT32_MAX;
2292
2293 return VINF_SUCCESS;
2294}
2295
2296
2297/**
2298 * Handles VMMDevReq_SetCpuHotPlugStatus.
2299 *
2300 * @returns VBox status code that the guest should see.
2301 * @param pThis The VMMDev instance data.
2302 * @param pReqHdr The header of the request to handle.
2303 */
2304static int vmmdevReqHandler_SetCpuHotPlugStatus(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
2305{
2306 VMMDevCpuHotPlugStatusRequest *pReq = (VMMDevCpuHotPlugStatusRequest *)pReqHdr;
2307 AssertMsgReturn(pReq->header.size == sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
2308
2309 if (pReq->enmStatusType == VMMDevCpuStatusType_Disable)
2310 pThis->fCpuHotPlugEventsEnabled = false;
2311 else if (pReq->enmStatusType == VMMDevCpuStatusType_Enable)
2312 pThis->fCpuHotPlugEventsEnabled = true;
2313 else
2314 return VERR_INVALID_PARAMETER;
2315 return VINF_SUCCESS;
2316}
2317
2318
2319#ifdef DEBUG
2320/**
2321 * Handles VMMDevReq_LogString.
2322 *
2323 * @returns VBox status code that the guest should see.
2324 * @param pReqHdr The header of the request to handle.
2325 */
2326static int vmmdevReqHandler_LogString(VMMDevRequestHeader *pReqHdr)
2327{
2328 VMMDevReqLogString *pReq = (VMMDevReqLogString *)pReqHdr;
2329 AssertMsgReturn(pReq->header.size >= sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
2330 AssertMsgReturn(pReq->szString[pReq->header.size - RT_UOFFSETOF(VMMDevReqLogString, szString) - 1] == '\0',
2331 ("not null terminated\n"), VERR_INVALID_PARAMETER);
2332
2333 LogIt(RTLOGGRPFLAGS_LEVEL_1, LOG_GROUP_DEV_VMM_BACKDOOR, ("DEBUG LOG: %s", pReq->szString));
2334 return VINF_SUCCESS;
2335}
2336#endif /* DEBUG */
2337
2338/**
2339 * Handles VMMDevReq_GetSessionId.
2340 *
2341 * Get a unique "session" ID for this VM, where the ID will be different after each
2342 * start, reset or restore of the VM. This can be used for restore detection
2343 * inside the guest.
2344 *
2345 * @returns VBox status code that the guest should see.
2346 * @param pThis The VMMDev instance data.
2347 * @param pReqHdr The header of the request to handle.
2348 */
2349static int vmmdevReqHandler_GetSessionId(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
2350{
2351 VMMDevReqSessionId *pReq = (VMMDevReqSessionId *)pReqHdr;
2352 AssertMsgReturn(pReq->header.size == sizeof(*pReq), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
2353
2354 pReq->idSession = pThis->idSession;
2355 return VINF_SUCCESS;
2356}
2357
2358
2359#ifdef VBOX_WITH_PAGE_SHARING
2360
2361/**
2362 * Handles VMMDevReq_RegisterSharedModule.
2363 *
2364 * @returns VBox status code that the guest should see.
2365 * @param pThis The VMMDev instance data.
2366 * @param pReqHdr The header of the request to handle.
2367 */
2368static int vmmdevReqHandler_RegisterSharedModule(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
2369{
2370 /*
2371 * Basic input validation (more done by GMM).
2372 */
2373 VMMDevSharedModuleRegistrationRequest *pReq = (VMMDevSharedModuleRegistrationRequest *)pReqHdr;
2374 AssertMsgReturn(pReq->header.size >= sizeof(VMMDevSharedModuleRegistrationRequest),
2375 ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
2376 AssertMsgReturn(pReq->header.size == RT_UOFFSETOF_DYN(VMMDevSharedModuleRegistrationRequest, aRegions[pReq->cRegions]),
2377 ("%u cRegions=%u\n", pReq->header.size, pReq->cRegions), VERR_INVALID_PARAMETER);
2378
2379 AssertReturn(RTStrEnd(pReq->szName, sizeof(pReq->szName)), VERR_INVALID_PARAMETER);
2380 AssertReturn(RTStrEnd(pReq->szVersion, sizeof(pReq->szVersion)), VERR_INVALID_PARAMETER);
2381 int rc = RTStrValidateEncoding(pReq->szName);
2382 AssertRCReturn(rc, rc);
2383 rc = RTStrValidateEncoding(pReq->szVersion);
2384 AssertRCReturn(rc, rc);
2385
2386 /*
2387 * Forward the request to the VMM.
2388 */
2389 return PGMR3SharedModuleRegister(PDMDevHlpGetVM(pThis->pDevIns), pReq->enmGuestOS, pReq->szName, pReq->szVersion,
2390 pReq->GCBaseAddr, pReq->cbModule, pReq->cRegions, pReq->aRegions);
2391}
2392
2393/**
2394 * Handles VMMDevReq_UnregisterSharedModule.
2395 *
2396 * @returns VBox status code that the guest should see.
2397 * @param pThis The VMMDev instance data.
2398 * @param pReqHdr The header of the request to handle.
2399 */
2400static int vmmdevReqHandler_UnregisterSharedModule(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
2401{
2402 /*
2403 * Basic input validation.
2404 */
2405 VMMDevSharedModuleUnregistrationRequest *pReq = (VMMDevSharedModuleUnregistrationRequest *)pReqHdr;
2406 AssertMsgReturn(pReq->header.size == sizeof(VMMDevSharedModuleUnregistrationRequest),
2407 ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
2408
2409 AssertReturn(RTStrEnd(pReq->szName, sizeof(pReq->szName)), VERR_INVALID_PARAMETER);
2410 AssertReturn(RTStrEnd(pReq->szVersion, sizeof(pReq->szVersion)), VERR_INVALID_PARAMETER);
2411 int rc = RTStrValidateEncoding(pReq->szName);
2412 AssertRCReturn(rc, rc);
2413 rc = RTStrValidateEncoding(pReq->szVersion);
2414 AssertRCReturn(rc, rc);
2415
2416 /*
2417 * Forward the request to the VMM.
2418 */
2419 return PGMR3SharedModuleUnregister(PDMDevHlpGetVM(pThis->pDevIns), pReq->szName, pReq->szVersion,
2420 pReq->GCBaseAddr, pReq->cbModule);
2421}
2422
2423/**
2424 * Handles VMMDevReq_CheckSharedModules.
2425 *
2426 * @returns VBox status code that the guest should see.
2427 * @param pThis The VMMDev instance data.
2428 * @param pReqHdr The header of the request to handle.
2429 */
2430static int vmmdevReqHandler_CheckSharedModules(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
2431{
2432 VMMDevSharedModuleCheckRequest *pReq = (VMMDevSharedModuleCheckRequest *)pReqHdr;
2433 AssertMsgReturn(pReq->header.size == sizeof(VMMDevSharedModuleCheckRequest),
2434 ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
2435 return PGMR3SharedModuleCheckAll(PDMDevHlpGetVM(pThis->pDevIns));
2436}
2437
2438/**
2439 * Handles VMMDevReq_GetPageSharingStatus.
2440 *
2441 * @returns VBox status code that the guest should see.
2442 * @param pThis The VMMDev instance data.
2443 * @param pReqHdr The header of the request to handle.
2444 */
2445static int vmmdevReqHandler_GetPageSharingStatus(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
2446{
2447 VMMDevPageSharingStatusRequest *pReq = (VMMDevPageSharingStatusRequest *)pReqHdr;
2448 AssertMsgReturn(pReq->header.size == sizeof(VMMDevPageSharingStatusRequest),
2449 ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
2450
2451 pReq->fEnabled = false;
2452 int rc = pThis->pDrv->pfnIsPageFusionEnabled(pThis->pDrv, &pReq->fEnabled);
2453 if (RT_FAILURE(rc))
2454 pReq->fEnabled = false;
2455 return VINF_SUCCESS;
2456}
2457
2458
2459/**
2460 * Handles VMMDevReq_DebugIsPageShared.
2461 *
2462 * @returns VBox status code that the guest should see.
2463 * @param pThis The VMMDev instance data.
2464 * @param pReqHdr The header of the request to handle.
2465 */
2466static int vmmdevReqHandler_DebugIsPageShared(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
2467{
2468 VMMDevPageIsSharedRequest *pReq = (VMMDevPageIsSharedRequest *)pReqHdr;
2469 AssertMsgReturn(pReq->header.size == sizeof(VMMDevPageIsSharedRequest),
2470 ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
2471
2472# ifdef DEBUG
2473 return PGMR3SharedModuleGetPageState(PDMDevHlpGetVM(pThis->pDevIns), pReq->GCPtrPage, &pReq->fShared, &pReq->uPageFlags);
2474# else
2475 RT_NOREF1(pThis);
2476 return VERR_NOT_IMPLEMENTED;
2477# endif
2478}
2479
2480#endif /* VBOX_WITH_PAGE_SHARING */
2481
2482
2483/**
2484 * Handles VMMDevReq_WriteCoreDumpe
2485 *
2486 * @returns VBox status code that the guest should see.
2487 * @param pThis The VMMDev instance data.
2488 * @param pReqHdr Pointer to the request header.
2489 */
2490static int vmmdevReqHandler_WriteCoreDump(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr)
2491{
2492 VMMDevReqWriteCoreDump *pReq = (VMMDevReqWriteCoreDump *)pReqHdr;
2493 AssertMsgReturn(pReq->header.size == sizeof(VMMDevReqWriteCoreDump), ("%u\n", pReq->header.size), VERR_INVALID_PARAMETER);
2494
2495 /*
2496 * Only available if explicitly enabled by the user.
2497 */
2498 if (!pThis->fGuestCoreDumpEnabled)
2499 return VERR_ACCESS_DENIED;
2500
2501 /*
2502 * User makes sure the directory exists before composing the path.
2503 */
2504 if (!RTDirExists(pThis->szGuestCoreDumpDir))
2505 return VERR_PATH_NOT_FOUND;
2506
2507 char szCorePath[RTPATH_MAX];
2508 RTStrCopy(szCorePath, sizeof(szCorePath), pThis->szGuestCoreDumpDir);
2509 RTPathAppend(szCorePath, sizeof(szCorePath), "VBox.core");
2510
2511 /*
2512 * Rotate existing cores based on number of additional cores to keep around.
2513 */
2514 if (pThis->cGuestCoreDumps > 0)
2515 for (int64_t i = pThis->cGuestCoreDumps - 1; i >= 0; i--)
2516 {
2517 char szFilePathOld[RTPATH_MAX];
2518 if (i == 0)
2519 RTStrCopy(szFilePathOld, sizeof(szFilePathOld), szCorePath);
2520 else
2521 RTStrPrintf(szFilePathOld, sizeof(szFilePathOld), "%s.%lld", szCorePath, i);
2522
2523 char szFilePathNew[RTPATH_MAX];
2524 RTStrPrintf(szFilePathNew, sizeof(szFilePathNew), "%s.%lld", szCorePath, i + 1);
2525 int vrc = RTFileMove(szFilePathOld, szFilePathNew, RTFILEMOVE_FLAGS_REPLACE);
2526 if (vrc == VERR_FILE_NOT_FOUND)
2527 RTFileDelete(szFilePathNew);
2528 }
2529
2530 /*
2531 * Write the core file.
2532 */
2533 PUVM pUVM = PDMDevHlpGetUVM(pThis->pDevIns);
2534 return DBGFR3CoreWrite(pUVM, szCorePath, true /*fReplaceFile*/);
2535}
2536
2537
2538/**
2539 * Dispatch the request to the appropriate handler function.
2540 *
2541 * @returns Port I/O handler exit code.
2542 * @param pThis The VMM device instance data.
2543 * @param pReqHdr The request header (cached in host memory).
2544 * @param GCPhysReqHdr The guest physical address of the request (for
2545 * HGCM).
2546 * @param pfDelayedUnlock Where to indicate whether the critical section exit
2547 * needs to be delayed till after the request has been
2548 * written back. This is a HGCM kludge, see critsect
2549 * work in hgcmCompletedWorker for more details.
2550 */
2551static int vmmdevReqDispatcher(PVMMDEV pThis, VMMDevRequestHeader *pReqHdr, RTGCPHYS GCPhysReqHdr, bool *pfDelayedUnlock)
2552{
2553 int rcRet = VINF_SUCCESS;
2554 *pfDelayedUnlock = false;
2555
2556 switch (pReqHdr->requestType)
2557 {
2558 case VMMDevReq_ReportGuestInfo:
2559 pReqHdr->rc = vmmdevReqHandler_ReportGuestInfo(pThis, pReqHdr);
2560 break;
2561
2562 case VMMDevReq_ReportGuestInfo2:
2563 pReqHdr->rc = vmmdevReqHandler_ReportGuestInfo2(pThis, pReqHdr);
2564 break;
2565
2566 case VMMDevReq_ReportGuestStatus:
2567 pReqHdr->rc = vmmdevReqHandler_ReportGuestStatus(pThis, pReqHdr);
2568 break;
2569
2570 case VMMDevReq_ReportGuestUserState:
2571 pReqHdr->rc = vmmdevReqHandler_ReportGuestUserState(pThis, pReqHdr);
2572 break;
2573
2574 case VMMDevReq_ReportGuestCapabilities:
2575 pReqHdr->rc = vmmdevReqHandler_ReportGuestCapabilities(pThis, pReqHdr);
2576 break;
2577
2578 case VMMDevReq_SetGuestCapabilities:
2579 pReqHdr->rc = vmmdevReqHandler_SetGuestCapabilities(pThis, pReqHdr);
2580 break;
2581
2582 case VMMDevReq_WriteCoreDump:
2583 pReqHdr->rc = vmmdevReqHandler_WriteCoreDump(pThis, pReqHdr);
2584 break;
2585
2586 case VMMDevReq_GetMouseStatus:
2587 pReqHdr->rc = vmmdevReqHandler_GetMouseStatus(pThis, pReqHdr);
2588 break;
2589
2590 case VMMDevReq_SetMouseStatus:
2591 pReqHdr->rc = vmmdevReqHandler_SetMouseStatus(pThis, pReqHdr);
2592 break;
2593
2594 case VMMDevReq_SetPointerShape:
2595 pReqHdr->rc = vmmdevReqHandler_SetPointerShape(pThis, pReqHdr);
2596 break;
2597
2598 case VMMDevReq_GetHostTime:
2599 pReqHdr->rc = vmmdevReqHandler_GetHostTime(pThis, pReqHdr);
2600 break;
2601
2602 case VMMDevReq_GetHypervisorInfo:
2603 pReqHdr->rc = vmmdevReqHandler_GetHypervisorInfo(pThis, pReqHdr);
2604 break;
2605
2606 case VMMDevReq_SetHypervisorInfo:
2607 pReqHdr->rc = vmmdevReqHandler_SetHypervisorInfo(pThis, pReqHdr);
2608 break;
2609
2610 case VMMDevReq_RegisterPatchMemory:
2611 pReqHdr->rc = vmmdevReqHandler_RegisterPatchMemory(pThis, pReqHdr);
2612 break;
2613
2614 case VMMDevReq_DeregisterPatchMemory:
2615 pReqHdr->rc = vmmdevReqHandler_DeregisterPatchMemory(pThis, pReqHdr);
2616 break;
2617
2618 case VMMDevReq_SetPowerStatus:
2619 {
2620 int rc = pReqHdr->rc = vmmdevReqHandler_SetPowerStatus(pThis, pReqHdr);
2621 if (rc != VINF_SUCCESS && RT_SUCCESS(rc))
2622 rcRet = rc;
2623 break;
2624 }
2625
2626 case VMMDevReq_GetDisplayChangeRequest:
2627 pReqHdr->rc = vmmdevReqHandler_GetDisplayChangeRequest(pThis, pReqHdr);
2628 break;
2629
2630 case VMMDevReq_GetDisplayChangeRequest2:
2631 pReqHdr->rc = vmmdevReqHandler_GetDisplayChangeRequest2(pThis, pReqHdr);
2632 break;
2633
2634 case VMMDevReq_GetDisplayChangeRequestEx:
2635 pReqHdr->rc = vmmdevReqHandler_GetDisplayChangeRequestEx(pThis, pReqHdr);
2636 break;
2637
2638 case VMMDevReq_GetDisplayChangeRequestMulti:
2639 pReqHdr->rc = vmmdevReqHandler_GetDisplayChangeRequestMulti(pThis, pReqHdr);
2640 break;
2641
2642 case VMMDevReq_VideoModeSupported:
2643 pReqHdr->rc = vmmdevReqHandler_VideoModeSupported(pThis, pReqHdr);
2644 break;
2645
2646 case VMMDevReq_VideoModeSupported2:
2647 pReqHdr->rc = vmmdevReqHandler_VideoModeSupported2(pThis, pReqHdr);
2648 break;
2649
2650 case VMMDevReq_GetHeightReduction:
2651 pReqHdr->rc = vmmdevReqHandler_GetHeightReduction(pThis, pReqHdr);
2652 break;
2653
2654 case VMMDevReq_AcknowledgeEvents:
2655 pReqHdr->rc = vmmdevReqHandler_AcknowledgeEvents(pThis, pReqHdr);
2656 break;
2657
2658 case VMMDevReq_CtlGuestFilterMask:
2659 pReqHdr->rc = vmmdevReqHandler_CtlGuestFilterMask(pThis, pReqHdr);
2660 break;
2661
2662#ifdef VBOX_WITH_HGCM
2663 case VMMDevReq_HGCMConnect:
2664 pReqHdr->rc = vmmdevReqHandler_HGCMConnect(pThis, pReqHdr, GCPhysReqHdr);
2665 *pfDelayedUnlock = true;
2666 break;
2667
2668 case VMMDevReq_HGCMDisconnect:
2669 pReqHdr->rc = vmmdevReqHandler_HGCMDisconnect(pThis, pReqHdr, GCPhysReqHdr);
2670 *pfDelayedUnlock = true;
2671 break;
2672
2673# ifdef VBOX_WITH_64_BITS_GUESTS
2674 case VMMDevReq_HGCMCall32:
2675 case VMMDevReq_HGCMCall64:
2676# else
2677 case VMMDevReq_HGCMCall:
2678# endif /* VBOX_WITH_64_BITS_GUESTS */
2679 pReqHdr->rc = vmmdevReqHandler_HGCMCall(pThis, pReqHdr, GCPhysReqHdr);
2680 *pfDelayedUnlock = true;
2681 break;
2682
2683 case VMMDevReq_HGCMCancel:
2684 pReqHdr->rc = vmmdevReqHandler_HGCMCancel(pThis, pReqHdr, GCPhysReqHdr);
2685 *pfDelayedUnlock = true;
2686 break;
2687
2688 case VMMDevReq_HGCMCancel2:
2689 pReqHdr->rc = vmmdevReqHandler_HGCMCancel2(pThis, pReqHdr);
2690 break;
2691#endif /* VBOX_WITH_HGCM */
2692
2693 case VMMDevReq_VideoAccelEnable:
2694 pReqHdr->rc = vmmdevReqHandler_VideoAccelEnable(pThis, pReqHdr);
2695 break;
2696
2697 case VMMDevReq_VideoAccelFlush:
2698 pReqHdr->rc = vmmdevReqHandler_VideoAccelFlush(pThis, pReqHdr);
2699 break;
2700
2701 case VMMDevReq_VideoSetVisibleRegion:
2702 pReqHdr->rc = vmmdevReqHandler_VideoSetVisibleRegion(pThis, pReqHdr);
2703 break;
2704
2705 case VMMDevReq_GetSeamlessChangeRequest:
2706 pReqHdr->rc = vmmdevReqHandler_GetSeamlessChangeRequest(pThis, pReqHdr);
2707 break;
2708
2709 case VMMDevReq_GetVRDPChangeRequest:
2710 pReqHdr->rc = vmmdevReqHandler_GetVRDPChangeRequest(pThis, pReqHdr);
2711 break;
2712
2713 case VMMDevReq_GetMemBalloonChangeRequest:
2714 pReqHdr->rc = vmmdevReqHandler_GetMemBalloonChangeRequest(pThis, pReqHdr);
2715 break;
2716
2717 case VMMDevReq_ChangeMemBalloon:
2718 pReqHdr->rc = vmmdevReqHandler_ChangeMemBalloon(pThis, pReqHdr);
2719 break;
2720
2721 case VMMDevReq_GetStatisticsChangeRequest:
2722 pReqHdr->rc = vmmdevReqHandler_GetStatisticsChangeRequest(pThis, pReqHdr);
2723 break;
2724
2725 case VMMDevReq_ReportGuestStats:
2726 pReqHdr->rc = vmmdevReqHandler_ReportGuestStats(pThis, pReqHdr);
2727 break;
2728
2729 case VMMDevReq_QueryCredentials:
2730 pReqHdr->rc = vmmdevReqHandler_QueryCredentials(pThis, pReqHdr);
2731 break;
2732
2733 case VMMDevReq_ReportCredentialsJudgement:
2734 pReqHdr->rc = vmmdevReqHandler_ReportCredentialsJudgement(pThis, pReqHdr);
2735 break;
2736
2737 case VMMDevReq_GetHostVersion:
2738 pReqHdr->rc = vmmdevReqHandler_GetHostVersion(pReqHdr);
2739 break;
2740
2741 case VMMDevReq_GetCpuHotPlugRequest:
2742 pReqHdr->rc = vmmdevReqHandler_GetCpuHotPlugRequest(pThis, pReqHdr);
2743 break;
2744
2745 case VMMDevReq_SetCpuHotPlugStatus:
2746 pReqHdr->rc = vmmdevReqHandler_SetCpuHotPlugStatus(pThis, pReqHdr);
2747 break;
2748
2749#ifdef VBOX_WITH_PAGE_SHARING
2750 case VMMDevReq_RegisterSharedModule:
2751 pReqHdr->rc = vmmdevReqHandler_RegisterSharedModule(pThis, pReqHdr);
2752 break;
2753
2754 case VMMDevReq_UnregisterSharedModule:
2755 pReqHdr->rc = vmmdevReqHandler_UnregisterSharedModule(pThis, pReqHdr);
2756 break;
2757
2758 case VMMDevReq_CheckSharedModules:
2759 pReqHdr->rc = vmmdevReqHandler_CheckSharedModules(pThis, pReqHdr);
2760 break;
2761
2762 case VMMDevReq_GetPageSharingStatus:
2763 pReqHdr->rc = vmmdevReqHandler_GetPageSharingStatus(pThis, pReqHdr);
2764 break;
2765
2766 case VMMDevReq_DebugIsPageShared:
2767 pReqHdr->rc = vmmdevReqHandler_DebugIsPageShared(pThis, pReqHdr);
2768 break;
2769
2770#endif /* VBOX_WITH_PAGE_SHARING */
2771
2772#ifdef DEBUG
2773 case VMMDevReq_LogString:
2774 pReqHdr->rc = vmmdevReqHandler_LogString(pReqHdr);
2775 break;
2776#endif
2777
2778 case VMMDevReq_GetSessionId:
2779 pReqHdr->rc = vmmdevReqHandler_GetSessionId(pThis, pReqHdr);
2780 break;
2781
2782 /*
2783 * Guest wants to give up a timeslice.
2784 * Note! This was only ever used by experimental GAs!
2785 */
2786 /** @todo maybe we could just remove this? */
2787 case VMMDevReq_Idle:
2788 {
2789 /* just return to EMT telling it that we want to halt */
2790 rcRet = VINF_EM_HALT;
2791 break;
2792 }
2793
2794 case VMMDevReq_GuestHeartbeat:
2795 pReqHdr->rc = vmmDevReqHandler_GuestHeartbeat(pThis);
2796 break;
2797
2798 case VMMDevReq_HeartbeatConfigure:
2799 pReqHdr->rc = vmmDevReqHandler_HeartbeatConfigure(pThis, pReqHdr);
2800 break;
2801
2802 case VMMDevReq_NtBugCheck:
2803 pReqHdr->rc = vmmDevReqHandler_NtBugCheck(pThis, pReqHdr);
2804 break;
2805
2806 default:
2807 {
2808 pReqHdr->rc = VERR_NOT_IMPLEMENTED;
2809 Log(("VMMDev unknown request type %d\n", pReqHdr->requestType));
2810 break;
2811 }
2812 }
2813 return rcRet;
2814}
2815
2816
2817/**
2818 * @callback_method_impl{FNIOMIOPORTOUT, Port I/O Handler for the generic
2819 * request interface.}
2820 */
2821static DECLCALLBACK(int) vmmdevRequestHandler(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
2822{
2823 RT_NOREF2(Port, cb);
2824 PVMMDEV pThis = (VMMDevState*)pvUser;
2825
2826 /*
2827 * The caller has passed the guest context physical address of the request
2828 * structure. We'll copy all of it into a heap buffer eventually, but we
2829 * will have to start off with the header.
2830 */
2831 VMMDevRequestHeader requestHeader;
2832 RT_ZERO(requestHeader);
2833 PDMDevHlpPhysRead(pDevIns, (RTGCPHYS)u32, &requestHeader, sizeof(requestHeader));
2834
2835 /* The structure size must be greater or equal to the header size. */
2836 if (requestHeader.size < sizeof(VMMDevRequestHeader))
2837 {
2838 Log(("VMMDev request header size too small! size = %d\n", requestHeader.size));
2839 return VINF_SUCCESS;
2840 }
2841
2842 /* Check the version of the header structure. */
2843 if (requestHeader.version != VMMDEV_REQUEST_HEADER_VERSION)
2844 {
2845 Log(("VMMDev: guest header version (0x%08X) differs from ours (0x%08X)\n", requestHeader.version, VMMDEV_REQUEST_HEADER_VERSION));
2846 return VINF_SUCCESS;
2847 }
2848
2849 Log2(("VMMDev request issued: %d\n", requestHeader.requestType));
2850
2851 int rcRet = VINF_SUCCESS;
2852 bool fDelayedUnlock = false;
2853 VMMDevRequestHeader *pRequestHeader = NULL;
2854
2855 /* Check that is doesn't exceed the max packet size. */
2856 if (requestHeader.size <= VMMDEV_MAX_VMMDEVREQ_SIZE)
2857 {
2858 /*
2859 * We require the GAs to report it's information before we let it have
2860 * access to all the functions. The VMMDevReq_ReportGuestInfo request
2861 * is the one which unlocks the access. Newer additions will first
2862 * issue VMMDevReq_ReportGuestInfo2, older ones doesn't know this one.
2863 * Two exceptions: VMMDevReq_GetHostVersion and VMMDevReq_WriteCoreDump.
2864 */
2865 if ( pThis->fu32AdditionsOk
2866 || requestHeader.requestType == VMMDevReq_ReportGuestInfo2
2867 || requestHeader.requestType == VMMDevReq_ReportGuestInfo
2868 || requestHeader.requestType == VMMDevReq_WriteCoreDump
2869 || requestHeader.requestType == VMMDevReq_GetHostVersion
2870 )
2871 {
2872 /*
2873 * The request looks fine. Allocate a heap block for it, read the
2874 * entire package from guest memory and feed it to the dispatcher.
2875 */
2876 pRequestHeader = (VMMDevRequestHeader *)RTMemAlloc(requestHeader.size);
2877 if (pRequestHeader)
2878 {
2879 memcpy(pRequestHeader, &requestHeader, sizeof(VMMDevRequestHeader));
2880 size_t cbLeft = requestHeader.size - sizeof(VMMDevRequestHeader);
2881 if (cbLeft)
2882 PDMDevHlpPhysRead(pDevIns,
2883 (RTGCPHYS)u32 + sizeof(VMMDevRequestHeader),
2884 (uint8_t *)pRequestHeader + sizeof(VMMDevRequestHeader),
2885 cbLeft);
2886
2887 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
2888 rcRet = vmmdevReqDispatcher(pThis, pRequestHeader, u32, &fDelayedUnlock);
2889 if (!fDelayedUnlock)
2890 PDMCritSectLeave(&pThis->CritSect);
2891 }
2892 else
2893 {
2894 Log(("VMMDev: RTMemAlloc failed!\n"));
2895 requestHeader.rc = VERR_NO_MEMORY;
2896 }
2897 }
2898 else
2899 {
2900 LogRelMax(10, ("VMMDev: Guest has not yet reported to us -- refusing operation of request #%d\n",
2901 requestHeader.requestType));
2902 requestHeader.rc = VERR_NOT_SUPPORTED;
2903 }
2904 }
2905 else
2906 {
2907 LogRelMax(50, ("VMMDev: Request packet too big (%x), refusing operation\n", requestHeader.size));
2908 requestHeader.rc = VERR_NOT_SUPPORTED;
2909 }
2910
2911 /*
2912 * Write the result back to guest memory
2913 */
2914 if (pRequestHeader)
2915 {
2916 PDMDevHlpPhysWrite(pDevIns, u32, pRequestHeader, pRequestHeader->size);
2917 if (fDelayedUnlock)
2918 PDMCritSectLeave(&pThis->CritSect);
2919 RTMemFree(pRequestHeader);
2920 }
2921 else
2922 {
2923 /* early error case; write back header only */
2924 PDMDevHlpPhysWrite(pDevIns, u32, &requestHeader, sizeof(requestHeader));
2925 Assert(!fDelayedUnlock);
2926 }
2927
2928 return rcRet;
2929}
2930
2931
2932/* -=-=-=-=-=- PCI Device -=-=-=-=-=- */
2933
2934
2935/**
2936 * @callback_method_impl{FNPCIIOREGIONMAP,MMIO/MMIO2 regions}
2937 */
2938static DECLCALLBACK(int) vmmdevIORAMRegionMap(PPDMDEVINS pDevIns, PPDMPCIDEV pPciDev, uint32_t iRegion,
2939 RTGCPHYS GCPhysAddress, RTGCPHYS cb, PCIADDRESSSPACE enmType)
2940{
2941 RT_NOREF1(cb);
2942 LogFlow(("vmmdevR3IORAMRegionMap: iRegion=%d GCPhysAddress=%RGp cb=%RGp enmType=%d\n", iRegion, GCPhysAddress, cb, enmType));
2943 PVMMDEV pThis = RT_FROM_MEMBER(pPciDev, VMMDEV, PciDev);
2944 int rc;
2945
2946 if (iRegion == 1)
2947 {
2948 AssertReturn(enmType == PCI_ADDRESS_SPACE_MEM, VERR_INTERNAL_ERROR);
2949 Assert(pThis->pVMMDevRAMR3 != NULL);
2950 if (GCPhysAddress != NIL_RTGCPHYS)
2951 {
2952 /*
2953 * Map the MMIO2 memory.
2954 */
2955 pThis->GCPhysVMMDevRAM = GCPhysAddress;
2956 Assert(pThis->GCPhysVMMDevRAM == GCPhysAddress);
2957 rc = PDMDevHlpMMIOExMap(pDevIns, pPciDev, iRegion, GCPhysAddress);
2958 }
2959 else
2960 {
2961 /*
2962 * It is about to be unmapped, just clean up.
2963 */
2964 pThis->GCPhysVMMDevRAM = NIL_RTGCPHYS32;
2965 rc = VINF_SUCCESS;
2966 }
2967 }
2968 else if (iRegion == 2)
2969 {
2970 AssertReturn(enmType == PCI_ADDRESS_SPACE_MEM_PREFETCH, VERR_INTERNAL_ERROR);
2971 Assert(pThis->pVMMDevHeapR3 != NULL);
2972 if (GCPhysAddress != NIL_RTGCPHYS)
2973 {
2974 /*
2975 * Map the MMIO2 memory.
2976 */
2977 pThis->GCPhysVMMDevHeap = GCPhysAddress;
2978 Assert(pThis->GCPhysVMMDevHeap == GCPhysAddress);
2979 rc = PDMDevHlpMMIOExMap(pDevIns, pPciDev, iRegion, GCPhysAddress);
2980 if (RT_SUCCESS(rc))
2981 rc = PDMDevHlpRegisterVMMDevHeap(pDevIns, GCPhysAddress, pThis->pVMMDevHeapR3, VMMDEV_HEAP_SIZE);
2982 }
2983 else
2984 {
2985 /*
2986 * It is about to be unmapped, just clean up.
2987 */
2988 PDMDevHlpRegisterVMMDevHeap(pDevIns, NIL_RTGCPHYS, pThis->pVMMDevHeapR3, VMMDEV_HEAP_SIZE);
2989 pThis->GCPhysVMMDevHeap = NIL_RTGCPHYS32;
2990 rc = VINF_SUCCESS;
2991 }
2992 }
2993 else
2994 {
2995 AssertMsgFailed(("%d\n", iRegion));
2996 rc = VERR_INVALID_PARAMETER;
2997 }
2998
2999 return rc;
3000}
3001
3002
3003/**
3004 * @callback_method_impl{FNPCIIOREGIONMAP,I/O Port Region}
3005 */
3006static DECLCALLBACK(int) vmmdevIOPortRegionMap(PPDMDEVINS pDevIns, PPDMPCIDEV pPciDev, uint32_t iRegion,
3007 RTGCPHYS GCPhysAddress, RTGCPHYS cb, PCIADDRESSSPACE enmType)
3008{
3009 LogFlow(("vmmdevIOPortRegionMap: iRegion=%d GCPhysAddress=%RGp cb=%RGp enmType=%d\n", iRegion, GCPhysAddress, cb, enmType));
3010 RT_NOREF3(iRegion, cb, enmType);
3011 PVMMDEV pThis = RT_FROM_MEMBER(pPciDev, VMMDEV, PciDev);
3012
3013 Assert(enmType == PCI_ADDRESS_SPACE_IO);
3014 Assert(iRegion == 0);
3015 AssertMsg(RT_ALIGN(GCPhysAddress, 8) == GCPhysAddress, ("Expected 8 byte alignment. GCPhysAddress=%#x\n", GCPhysAddress));
3016
3017 /*
3018 * Register our port IO handlers.
3019 */
3020 int rc = PDMDevHlpIOPortRegister(pDevIns, (RTIOPORT)GCPhysAddress + VMMDEV_PORT_OFF_REQUEST, 1,
3021 pThis, vmmdevRequestHandler, NULL, NULL, NULL, "VMMDev Request Handler");
3022 AssertRC(rc);
3023 return rc;
3024}
3025
3026
3027
3028/* -=-=-=-=-=- Backdoor Logging and Time Sync. -=-=-=-=-=- */
3029
3030/**
3031 * @callback_method_impl{FNIOMIOPORTOUT, Backdoor Logging.}
3032 */
3033static DECLCALLBACK(int) vmmdevBackdoorLog(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
3034{
3035 RT_NOREF1(pvUser);
3036 PVMMDEV pThis = PDMINS_2_DATA(pDevIns, VMMDevState *);
3037
3038 if (!pThis->fBackdoorLogDisabled && cb == 1 && Port == RTLOG_DEBUG_PORT)
3039 {
3040
3041 /* The raw version. */
3042 switch (u32)
3043 {
3044 case '\r': LogIt(RTLOGGRPFLAGS_LEVEL_2, LOG_GROUP_DEV_VMM_BACKDOOR, ("vmmdev: <return>\n")); break;
3045 case '\n': LogIt(RTLOGGRPFLAGS_LEVEL_2, LOG_GROUP_DEV_VMM_BACKDOOR, ("vmmdev: <newline>\n")); break;
3046 case '\t': LogIt(RTLOGGRPFLAGS_LEVEL_2, LOG_GROUP_DEV_VMM_BACKDOOR, ("vmmdev: <tab>\n")); break;
3047 default: LogIt(RTLOGGRPFLAGS_LEVEL_2, LOG_GROUP_DEV_VMM_BACKDOOR, ("vmmdev: %c (%02x)\n", u32, u32)); break;
3048 }
3049
3050 /* The readable, buffered version. */
3051 if (u32 == '\n' || u32 == '\r')
3052 {
3053 pThis->szMsg[pThis->iMsg] = '\0';
3054 if (pThis->iMsg)
3055 LogRelIt(RTLOGGRPFLAGS_LEVEL_1, LOG_GROUP_DEV_VMM_BACKDOOR, ("VMMDev: Guest Log: %s\n", pThis->szMsg));
3056 pThis->iMsg = 0;
3057 }
3058 else
3059 {
3060 if (pThis->iMsg >= sizeof(pThis->szMsg)-1)
3061 {
3062 pThis->szMsg[pThis->iMsg] = '\0';
3063 LogRelIt(RTLOGGRPFLAGS_LEVEL_1, LOG_GROUP_DEV_VMM_BACKDOOR, ("VMMDev: Guest Log: %s\n", pThis->szMsg));
3064 pThis->iMsg = 0;
3065 }
3066 pThis->szMsg[pThis->iMsg] = (char )u32;
3067 pThis->szMsg[++pThis->iMsg] = '\0';
3068 }
3069 }
3070 return VINF_SUCCESS;
3071}
3072
3073#ifdef VMMDEV_WITH_ALT_TIMESYNC
3074
3075/**
3076 * @callback_method_impl{FNIOMIOPORTOUT, Alternative time synchronization.}
3077 */
3078static DECLCALLBACK(int) vmmdevAltTimeSyncWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
3079{
3080 RT_NOREF2(pvUser, Port);
3081 PVMMDEV pThis = PDMINS_2_DATA(pDevIns, VMMDevState *);
3082 if (cb == 4)
3083 {
3084 /* Selects high (0) or low (1) DWORD. The high has to be read first. */
3085 switch (u32)
3086 {
3087 case 0:
3088 pThis->fTimesyncBackdoorLo = false;
3089 break;
3090 case 1:
3091 pThis->fTimesyncBackdoorLo = true;
3092 break;
3093 default:
3094 Log(("vmmdevAltTimeSyncWrite: Invalid access cb=%#x u32=%#x\n", cb, u32));
3095 break;
3096 }
3097 }
3098 else
3099 Log(("vmmdevAltTimeSyncWrite: Invalid access cb=%#x u32=%#x\n", cb, u32));
3100 return VINF_SUCCESS;
3101}
3102
3103/**
3104 * @callback_method_impl{FNIOMIOPORTOUT, Alternative time synchronization.}
3105 */
3106static DECLCALLBACK(int) vmmdevAltTimeSyncRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
3107{
3108 RT_NOREF2(pvUser, Port);
3109 PVMMDEV pThis = PDMINS_2_DATA(pDevIns, VMMDevState *);
3110 int rc;
3111 if (cb == 4)
3112 {
3113 if (pThis->fTimesyncBackdoorLo)
3114 *pu32 = (uint32_t)pThis->hostTime;
3115 else
3116 {
3117 /* Reading the high dword gets and saves the current time. */
3118 RTTIMESPEC Now;
3119 pThis->hostTime = RTTimeSpecGetMilli(PDMDevHlpTMUtcNow(pDevIns, &Now));
3120 *pu32 = (uint32_t)(pThis->hostTime >> 32);
3121 }
3122 rc = VINF_SUCCESS;
3123 }
3124 else
3125 {
3126 Log(("vmmdevAltTimeSyncRead: Invalid access cb=%#x\n", cb));
3127 rc = VERR_IOM_IOPORT_UNUSED;
3128 }
3129 return rc;
3130}
3131
3132#endif /* VMMDEV_WITH_ALT_TIMESYNC */
3133
3134
3135/* -=-=-=-=-=- IBase -=-=-=-=-=- */
3136
3137/**
3138 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
3139 */
3140static DECLCALLBACK(void *) vmmdevPortQueryInterface(PPDMIBASE pInterface, const char *pszIID)
3141{
3142 PVMMDEV pThis = RT_FROM_MEMBER(pInterface, VMMDEV, IBase);
3143
3144 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThis->IBase);
3145 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIVMMDEVPORT, &pThis->IPort);
3146#ifdef VBOX_WITH_HGCM
3147 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIHGCMPORT, &pThis->IHGCMPort);
3148#endif
3149 /* Currently only for shared folders. */
3150 PDMIBASE_RETURN_INTERFACE(pszIID, PDMILEDPORTS, &pThis->SharedFolders.ILeds);
3151 return NULL;
3152}
3153
3154
3155/* -=-=-=-=-=- ILeds -=-=-=-=-=- */
3156
3157/**
3158 * Gets the pointer to the status LED of a unit.
3159 *
3160 * @returns VBox status code.
3161 * @param pInterface Pointer to the interface structure containing the called function pointer.
3162 * @param iLUN The unit which status LED we desire.
3163 * @param ppLed Where to store the LED pointer.
3164 */
3165static DECLCALLBACK(int) vmmdevQueryStatusLed(PPDMILEDPORTS pInterface, unsigned iLUN, PPDMLED *ppLed)
3166{
3167 PVMMDEV pThis = RT_FROM_MEMBER(pInterface, VMMDEV, SharedFolders.ILeds);
3168 if (iLUN == 0) /* LUN 0 is shared folders */
3169 {
3170 *ppLed = &pThis->SharedFolders.Led;
3171 return VINF_SUCCESS;
3172 }
3173 return VERR_PDM_LUN_NOT_FOUND;
3174}
3175
3176
3177/* -=-=-=-=-=- PDMIVMMDEVPORT (VMMDEV::IPort) -=-=-=-=-=- */
3178
3179/**
3180 * @interface_method_impl{PDMIVMMDEVPORT,pfnQueryAbsoluteMouse}
3181 */
3182static DECLCALLBACK(int) vmmdevIPort_QueryAbsoluteMouse(PPDMIVMMDEVPORT pInterface, int32_t *pxAbs, int32_t *pyAbs)
3183{
3184 PVMMDEV pThis = RT_FROM_MEMBER(pInterface, VMMDEV, IPort);
3185
3186 /** @todo at the first sign of trouble in this area, just enter the critsect.
3187 * As indicated by the comment below, the atomic reads serves no real purpose
3188 * here since we can assume cache coherency protocoles and int32_t alignment
3189 * rules making sure we won't see a halfwritten value. */
3190 if (pxAbs)
3191 *pxAbs = ASMAtomicReadS32(&pThis->mouseXAbs); /* why the atomic read? */
3192 if (pyAbs)
3193 *pyAbs = ASMAtomicReadS32(&pThis->mouseYAbs);
3194
3195 return VINF_SUCCESS;
3196}
3197
3198/**
3199 * @interface_method_impl{PDMIVMMDEVPORT,pfnSetAbsoluteMouse}
3200 */
3201static DECLCALLBACK(int) vmmdevIPort_SetAbsoluteMouse(PPDMIVMMDEVPORT pInterface, int32_t xAbs, int32_t yAbs)
3202{
3203 PVMMDEV pThis = RT_FROM_MEMBER(pInterface, VMMDEV, IPort);
3204 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
3205
3206 if ( pThis->mouseXAbs != xAbs
3207 || pThis->mouseYAbs != yAbs)
3208 {
3209 Log2(("vmmdevIPort_SetAbsoluteMouse : settings absolute position to x = %d, y = %d\n", xAbs, yAbs));
3210 pThis->mouseXAbs = xAbs;
3211 pThis->mouseYAbs = yAbs;
3212 VMMDevNotifyGuest(pThis, VMMDEV_EVENT_MOUSE_POSITION_CHANGED);
3213 }
3214
3215 PDMCritSectLeave(&pThis->CritSect);
3216 return VINF_SUCCESS;
3217}
3218
3219/**
3220 * @interface_method_impl{PDMIVMMDEVPORT,pfnQueryMouseCapabilities}
3221 */
3222static DECLCALLBACK(int) vmmdevIPort_QueryMouseCapabilities(PPDMIVMMDEVPORT pInterface, uint32_t *pfCapabilities)
3223{
3224 PVMMDEV pThis = RT_FROM_MEMBER(pInterface, VMMDEV, IPort);
3225 AssertPtrReturn(pfCapabilities, VERR_INVALID_PARAMETER);
3226
3227 *pfCapabilities = pThis->mouseCapabilities;
3228 return VINF_SUCCESS;
3229}
3230
3231/**
3232 * @interface_method_impl{PDMIVMMDEVPORT,pfnUpdateMouseCapabilities}
3233 */
3234static DECLCALLBACK(int)
3235vmmdevIPort_UpdateMouseCapabilities(PPDMIVMMDEVPORT pInterface, uint32_t fCapsAdded, uint32_t fCapsRemoved)
3236{
3237 PVMMDEV pThis = RT_FROM_MEMBER(pInterface, VMMDEV, IPort);
3238 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
3239
3240 uint32_t fOldCaps = pThis->mouseCapabilities;
3241 pThis->mouseCapabilities &= ~(fCapsRemoved & VMMDEV_MOUSE_HOST_MASK);
3242 pThis->mouseCapabilities |= (fCapsAdded & VMMDEV_MOUSE_HOST_MASK)
3243 | VMMDEV_MOUSE_HOST_RECHECKS_NEEDS_HOST_CURSOR;
3244 bool fNotify = fOldCaps != pThis->mouseCapabilities;
3245
3246 LogRelFlow(("VMMDev: vmmdevIPort_UpdateMouseCapabilities: fCapsAdded=0x%x, fCapsRemoved=0x%x, fNotify=%RTbool\n", fCapsAdded,
3247 fCapsRemoved, fNotify));
3248
3249 if (fNotify)
3250 VMMDevNotifyGuest(pThis, VMMDEV_EVENT_MOUSE_CAPABILITIES_CHANGED);
3251
3252 PDMCritSectLeave(&pThis->CritSect);
3253 return VINF_SUCCESS;
3254}
3255
3256static bool vmmdevIsMonitorDefEqual(VMMDevDisplayDef const *pNew, VMMDevDisplayDef const *pOld)
3257{
3258 bool fEqual = pNew->idDisplay == pOld->idDisplay;
3259
3260 fEqual = fEqual && ( !RT_BOOL(pNew->fDisplayFlags & VMMDEV_DISPLAY_ORIGIN) /* No change. */
3261 || ( RT_BOOL(pOld->fDisplayFlags & VMMDEV_DISPLAY_ORIGIN) /* Old value exists and */
3262 && pNew->xOrigin == pOld->xOrigin /* the old is equal to the new. */
3263 && pNew->yOrigin == pOld->yOrigin));
3264
3265 fEqual = fEqual && ( !RT_BOOL(pNew->fDisplayFlags & VMMDEV_DISPLAY_CX)
3266 || ( RT_BOOL(pOld->fDisplayFlags & VMMDEV_DISPLAY_CX)
3267 && pNew->cx == pOld->cx));
3268
3269 fEqual = fEqual && ( !RT_BOOL(pNew->fDisplayFlags & VMMDEV_DISPLAY_CY)
3270 || ( RT_BOOL(pOld->fDisplayFlags & VMMDEV_DISPLAY_CY)
3271 && pNew->cy == pOld->cy));
3272
3273 fEqual = fEqual && ( !RT_BOOL(pNew->fDisplayFlags & VMMDEV_DISPLAY_BPP)
3274 || ( RT_BOOL(pOld->fDisplayFlags & VMMDEV_DISPLAY_BPP)
3275 && pNew->cBitsPerPixel == pOld->cBitsPerPixel));
3276
3277 fEqual = fEqual && ( RT_BOOL(pNew->fDisplayFlags & VMMDEV_DISPLAY_DISABLED)
3278 == RT_BOOL(pOld->fDisplayFlags & VMMDEV_DISPLAY_DISABLED));
3279
3280 fEqual = fEqual && ( RT_BOOL(pNew->fDisplayFlags & VMMDEV_DISPLAY_PRIMARY)
3281 == RT_BOOL(pOld->fDisplayFlags & VMMDEV_DISPLAY_PRIMARY));
3282
3283 return fEqual;
3284}
3285
3286/**
3287 * @interface_method_impl{PDMIVMMDEVPORT,pfnRequestDisplayChange}
3288 */
3289static DECLCALLBACK(int)
3290vmmdevIPort_RequestDisplayChange(PPDMIVMMDEVPORT pInterface, uint32_t cDisplays, VMMDevDisplayDef const *paDisplays, bool fForce)
3291{
3292 int rc = VINF_SUCCESS;
3293
3294 PVMMDEV pThis = RT_FROM_MEMBER(pInterface, VMMDEV, IPort);
3295 bool fNotifyGuest = false;
3296
3297 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
3298
3299 uint32_t i;
3300 for (i = 0; i < cDisplays; ++i)
3301 {
3302 VMMDevDisplayDef const *p = &paDisplays[i];
3303
3304 /* Either one display definition is provided or the display id must be equal to the array index. */
3305 AssertBreakStmt(cDisplays == 1 || p->idDisplay == i, rc = VERR_INVALID_PARAMETER);
3306 AssertBreakStmt(p->idDisplay < RT_ELEMENTS(pThis->displayChangeData.aRequests), rc = VERR_INVALID_PARAMETER);
3307
3308 DISPLAYCHANGEREQUEST *pRequest = &pThis->displayChangeData.aRequests[p->idDisplay];
3309
3310 VMMDevDisplayDef const *pLastRead = &pRequest->lastReadDisplayChangeRequest;
3311
3312 /* Verify that the new resolution is different and that guest does not yet know about it. */
3313 bool const fDifferentResolution = fForce || !vmmdevIsMonitorDefEqual(p, pLastRead);
3314
3315 LogFunc(("same=%d. New: %dx%d, cBits=%d, id=%d. Old: %dx%d, cBits=%d, id=%d. @%d,%d, Enabled=%d, ChangeOrigin=%d\n",
3316 !fDifferentResolution, p->cx, p->cy, p->cBitsPerPixel, p->idDisplay,
3317 pLastRead->cx, pLastRead->cy, pLastRead->cBitsPerPixel, pLastRead->idDisplay,
3318 p->xOrigin, p->yOrigin,
3319 !RT_BOOL(p->fDisplayFlags & VMMDEV_DISPLAY_DISABLED),
3320 RT_BOOL(p->fDisplayFlags & VMMDEV_DISPLAY_ORIGIN)));
3321
3322 /* We could validate the information here but hey, the guest can do that as well! */
3323 pRequest->displayChangeRequest = *p;
3324 pRequest->fPending = fDifferentResolution;
3325
3326 fNotifyGuest = fNotifyGuest || fDifferentResolution;
3327 }
3328
3329 if (RT_SUCCESS(rc))
3330 {
3331 if (fNotifyGuest)
3332 {
3333 for (i = 0; i < RT_ELEMENTS(pThis->displayChangeData.aRequests); ++i)
3334 {
3335 DISPLAYCHANGEREQUEST *pRequest = &pThis->displayChangeData.aRequests[i];
3336 if (pRequest->fPending)
3337 {
3338 VMMDevDisplayDef const *p = &pRequest->displayChangeRequest;
3339 LogRel(("VMMDev: SetVideoModeHint: Got a video mode hint (%dx%dx%d)@(%dx%d),(%d;%d) at %d\n",
3340 p->cx, p->cy, p->cBitsPerPixel, p->xOrigin, p->yOrigin,
3341 !RT_BOOL(p->fDisplayFlags & VMMDEV_DISPLAY_DISABLED),
3342 RT_BOOL(p->fDisplayFlags & VMMDEV_DISPLAY_ORIGIN), i));
3343 }
3344 }
3345
3346 /* IRQ so the guest knows what's going on */
3347 VMMDevNotifyGuest(pThis, VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST);
3348 }
3349 }
3350
3351 PDMCritSectLeave(&pThis->CritSect);
3352 return rc;
3353}
3354
3355/**
3356 * @interface_method_impl{PDMIVMMDEVPORT,pfnRequestSeamlessChange}
3357 */
3358static DECLCALLBACK(int) vmmdevIPort_RequestSeamlessChange(PPDMIVMMDEVPORT pInterface, bool fEnabled)
3359{
3360 PVMMDEV pThis = RT_FROM_MEMBER(pInterface, VMMDEV, IPort);
3361 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
3362
3363 /* Verify that the new resolution is different and that guest does not yet know about it. */
3364 bool fSameMode = (pThis->fLastSeamlessEnabled == fEnabled);
3365
3366 Log(("vmmdevIPort_RequestSeamlessChange: same=%d. new=%d\n", fSameMode, fEnabled));
3367
3368 if (!fSameMode)
3369 {
3370 /* we could validate the information here but hey, the guest can do that as well! */
3371 pThis->fSeamlessEnabled = fEnabled;
3372
3373 /* IRQ so the guest knows what's going on */
3374 VMMDevNotifyGuest(pThis, VMMDEV_EVENT_SEAMLESS_MODE_CHANGE_REQUEST);
3375 }
3376
3377 PDMCritSectLeave(&pThis->CritSect);
3378 return VINF_SUCCESS;
3379}
3380
3381/**
3382 * @interface_method_impl{PDMIVMMDEVPORT,pfnSetMemoryBalloon}
3383 */
3384static DECLCALLBACK(int) vmmdevIPort_SetMemoryBalloon(PPDMIVMMDEVPORT pInterface, uint32_t cMbBalloon)
3385{
3386 PVMMDEV pThis = RT_FROM_MEMBER(pInterface, VMMDEV, IPort);
3387 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
3388
3389 /* Verify that the new resolution is different and that guest does not yet know about it. */
3390 Log(("vmmdevIPort_SetMemoryBalloon: old=%u new=%u\n", pThis->cMbMemoryBalloonLast, cMbBalloon));
3391 if (pThis->cMbMemoryBalloonLast != cMbBalloon)
3392 {
3393 /* we could validate the information here but hey, the guest can do that as well! */
3394 pThis->cMbMemoryBalloon = cMbBalloon;
3395
3396 /* IRQ so the guest knows what's going on */
3397 VMMDevNotifyGuest(pThis, VMMDEV_EVENT_BALLOON_CHANGE_REQUEST);
3398 }
3399
3400 PDMCritSectLeave(&pThis->CritSect);
3401 return VINF_SUCCESS;
3402}
3403
3404/**
3405 * @interface_method_impl{PDMIVMMDEVPORT,pfnVRDPChange}
3406 */
3407static DECLCALLBACK(int) vmmdevIPort_VRDPChange(PPDMIVMMDEVPORT pInterface, bool fVRDPEnabled, uint32_t uVRDPExperienceLevel)
3408{
3409 PVMMDEV pThis = RT_FROM_MEMBER(pInterface, VMMDEV, IPort);
3410 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
3411
3412 bool fSame = (pThis->fVRDPEnabled == fVRDPEnabled);
3413
3414 Log(("vmmdevIPort_VRDPChange: old=%d. new=%d\n", pThis->fVRDPEnabled, fVRDPEnabled));
3415
3416 if (!fSame)
3417 {
3418 pThis->fVRDPEnabled = fVRDPEnabled;
3419 pThis->uVRDPExperienceLevel = uVRDPExperienceLevel;
3420
3421 VMMDevNotifyGuest(pThis, VMMDEV_EVENT_VRDP);
3422 }
3423
3424 PDMCritSectLeave(&pThis->CritSect);
3425 return VINF_SUCCESS;
3426}
3427
3428/**
3429 * @interface_method_impl{PDMIVMMDEVPORT,pfnSetStatisticsInterval}
3430 */
3431static DECLCALLBACK(int) vmmdevIPort_SetStatisticsInterval(PPDMIVMMDEVPORT pInterface, uint32_t cSecsStatInterval)
3432{
3433 PVMMDEV pThis = RT_FROM_MEMBER(pInterface, VMMDEV, IPort);
3434 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
3435
3436 /* Verify that the new resolution is different and that guest does not yet know about it. */
3437 bool fSame = (pThis->u32LastStatIntervalSize == cSecsStatInterval);
3438
3439 Log(("vmmdevIPort_SetStatisticsInterval: old=%d. new=%d\n", pThis->u32LastStatIntervalSize, cSecsStatInterval));
3440
3441 if (!fSame)
3442 {
3443 /* we could validate the information here but hey, the guest can do that as well! */
3444 pThis->u32StatIntervalSize = cSecsStatInterval;
3445
3446 /* IRQ so the guest knows what's going on */
3447 VMMDevNotifyGuest(pThis, VMMDEV_EVENT_STATISTICS_INTERVAL_CHANGE_REQUEST);
3448 }
3449
3450 PDMCritSectLeave(&pThis->CritSect);
3451 return VINF_SUCCESS;
3452}
3453
3454/**
3455 * @interface_method_impl{PDMIVMMDEVPORT,pfnSetCredentials}
3456 */
3457static DECLCALLBACK(int) vmmdevIPort_SetCredentials(PPDMIVMMDEVPORT pInterface, const char *pszUsername,
3458 const char *pszPassword, const char *pszDomain, uint32_t fFlags)
3459{
3460 PVMMDEV pThis = RT_FROM_MEMBER(pInterface, VMMDEV, IPort);
3461 AssertReturn(fFlags & (VMMDEV_SETCREDENTIALS_GUESTLOGON | VMMDEV_SETCREDENTIALS_JUDGE), VERR_INVALID_PARAMETER);
3462 size_t const cchUsername = strlen(pszUsername);
3463 AssertReturn(cchUsername < VMMDEV_CREDENTIALS_SZ_SIZE, VERR_BUFFER_OVERFLOW);
3464 size_t const cchPassword = strlen(pszPassword);
3465 AssertReturn(cchPassword < VMMDEV_CREDENTIALS_SZ_SIZE, VERR_BUFFER_OVERFLOW);
3466 size_t const cchDomain = strlen(pszDomain);
3467 AssertReturn(cchDomain < VMMDEV_CREDENTIALS_SZ_SIZE, VERR_BUFFER_OVERFLOW);
3468
3469 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
3470
3471 /*
3472 * Logon mode
3473 */
3474 if (fFlags & VMMDEV_SETCREDENTIALS_GUESTLOGON)
3475 {
3476 /* memorize the data */
3477 memcpy(pThis->pCredentials->Logon.szUserName, pszUsername, cchUsername);
3478 pThis->pCredentials->Logon.szUserName[cchUsername] = '\0';
3479 memcpy(pThis->pCredentials->Logon.szPassword, pszPassword, cchPassword);
3480 pThis->pCredentials->Logon.szPassword[cchPassword] = '\0';
3481 memcpy(pThis->pCredentials->Logon.szDomain, pszDomain, cchDomain);
3482 pThis->pCredentials->Logon.szDomain[cchDomain] = '\0';
3483 pThis->pCredentials->Logon.fAllowInteractiveLogon = !(fFlags & VMMDEV_SETCREDENTIALS_NOLOCALLOGON);
3484 }
3485 /*
3486 * Credentials verification mode?
3487 */
3488 else
3489 {
3490 /* memorize the data */
3491 memcpy(pThis->pCredentials->Judge.szUserName, pszUsername, cchUsername);
3492 pThis->pCredentials->Judge.szUserName[cchUsername] = '\0';
3493 memcpy(pThis->pCredentials->Judge.szPassword, pszPassword, cchPassword);
3494 pThis->pCredentials->Judge.szPassword[cchPassword] = '\0';
3495 memcpy(pThis->pCredentials->Judge.szDomain, pszDomain, cchDomain);
3496 pThis->pCredentials->Judge.szDomain[cchDomain] = '\0';
3497
3498 VMMDevNotifyGuest(pThis, VMMDEV_EVENT_JUDGE_CREDENTIALS);
3499 }
3500
3501 PDMCritSectLeave(&pThis->CritSect);
3502 return VINF_SUCCESS;
3503}
3504
3505/**
3506 * @interface_method_impl{PDMIVMMDEVPORT,pfnVBVAChange}
3507 *
3508 * Notification from the Display. Especially useful when acceleration is
3509 * disabled after a video mode change.
3510 */
3511static DECLCALLBACK(void) vmmdevIPort_VBVAChange(PPDMIVMMDEVPORT pInterface, bool fEnabled)
3512{
3513 PVMMDEV pThis = RT_FROM_MEMBER(pInterface, VMMDEV, IPort);
3514 Log(("vmmdevIPort_VBVAChange: fEnabled = %d\n", fEnabled));
3515
3516 /* Only used by saved state, which I guess is why we don't bother with locking here. */
3517 pThis->u32VideoAccelEnabled = fEnabled;
3518}
3519
3520/**
3521 * @interface_method_impl{PDMIVMMDEVPORT,pfnCpuHotUnplug}
3522 */
3523static DECLCALLBACK(int) vmmdevIPort_CpuHotUnplug(PPDMIVMMDEVPORT pInterface, uint32_t idCpuCore, uint32_t idCpuPackage)
3524{
3525 PVMMDEV pThis = RT_FROM_MEMBER(pInterface, VMMDEV, IPort);
3526 int rc = VINF_SUCCESS;
3527
3528 Log(("vmmdevIPort_CpuHotUnplug: idCpuCore=%u idCpuPackage=%u\n", idCpuCore, idCpuPackage));
3529
3530 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
3531
3532 if (pThis->fCpuHotPlugEventsEnabled)
3533 {
3534 pThis->enmCpuHotPlugEvent = VMMDevCpuEventType_Unplug;
3535 pThis->idCpuCore = idCpuCore;
3536 pThis->idCpuPackage = idCpuPackage;
3537 VMMDevNotifyGuest(pThis, VMMDEV_EVENT_CPU_HOTPLUG);
3538 }
3539 else
3540 rc = VERR_VMMDEV_CPU_HOTPLUG_NOT_MONITORED_BY_GUEST;
3541
3542 PDMCritSectLeave(&pThis->CritSect);
3543 return rc;
3544}
3545
3546/**
3547 * @interface_method_impl{PDMIVMMDEVPORT,pfnCpuHotPlug}
3548 */
3549static DECLCALLBACK(int) vmmdevIPort_CpuHotPlug(PPDMIVMMDEVPORT pInterface, uint32_t idCpuCore, uint32_t idCpuPackage)
3550{
3551 PVMMDEV pThis = RT_FROM_MEMBER(pInterface, VMMDEV, IPort);
3552 int rc = VINF_SUCCESS;
3553
3554 Log(("vmmdevCpuPlug: idCpuCore=%u idCpuPackage=%u\n", idCpuCore, idCpuPackage));
3555
3556 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
3557
3558 if (pThis->fCpuHotPlugEventsEnabled)
3559 {
3560 pThis->enmCpuHotPlugEvent = VMMDevCpuEventType_Plug;
3561 pThis->idCpuCore = idCpuCore;
3562 pThis->idCpuPackage = idCpuPackage;
3563 VMMDevNotifyGuest(pThis, VMMDEV_EVENT_CPU_HOTPLUG);
3564 }
3565 else
3566 rc = VERR_VMMDEV_CPU_HOTPLUG_NOT_MONITORED_BY_GUEST;
3567
3568 PDMCritSectLeave(&pThis->CritSect);
3569 return rc;
3570}
3571
3572
3573/* -=-=-=-=-=- Saved State -=-=-=-=-=- */
3574
3575/**
3576 * @callback_method_impl{FNSSMDEVLIVEEXEC}
3577 */
3578static DECLCALLBACK(int) vmmdevLiveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uPass)
3579{
3580 RT_NOREF1(uPass);
3581 PVMMDEV pThis = PDMINS_2_DATA(pDevIns, PVMMDEV);
3582
3583 SSMR3PutBool(pSSM, pThis->fGetHostTimeDisabled);
3584 SSMR3PutBool(pSSM, pThis->fBackdoorLogDisabled);
3585 SSMR3PutBool(pSSM, pThis->fKeepCredentials);
3586 SSMR3PutBool(pSSM, pThis->fHeapEnabled);
3587
3588 return VINF_SSM_DONT_CALL_AGAIN;
3589}
3590
3591
3592/**
3593 * @callback_method_impl{FNSSMDEVSAVEEXEC}
3594 */
3595static DECLCALLBACK(int) vmmdevSaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
3596{
3597 PVMMDEV pThis = PDMINS_2_DATA(pDevIns, PVMMDEV);
3598 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
3599
3600 vmmdevLiveExec(pDevIns, pSSM, SSM_PASS_FINAL);
3601
3602 SSMR3PutU32(pSSM, pThis->hypervisorSize);
3603 SSMR3PutU32(pSSM, pThis->mouseCapabilities);
3604 SSMR3PutS32(pSSM, pThis->mouseXAbs);
3605 SSMR3PutS32(pSSM, pThis->mouseYAbs);
3606
3607 SSMR3PutBool(pSSM, pThis->fNewGuestFilterMask);
3608 SSMR3PutU32(pSSM, pThis->u32NewGuestFilterMask);
3609 SSMR3PutU32(pSSM, pThis->u32GuestFilterMask);
3610 SSMR3PutU32(pSSM, pThis->u32HostEventFlags);
3611 /* The following is not strictly necessary as PGM restores MMIO2, keeping it for historical reasons. */
3612 SSMR3PutMem(pSSM, &pThis->pVMMDevRAMR3->V, sizeof(pThis->pVMMDevRAMR3->V));
3613
3614 SSMR3PutMem(pSSM, &pThis->guestInfo, sizeof(pThis->guestInfo));
3615 SSMR3PutU32(pSSM, pThis->fu32AdditionsOk);
3616 SSMR3PutU32(pSSM, pThis->u32VideoAccelEnabled);
3617 SSMR3PutBool(pSSM, pThis->displayChangeData.fGuestSentChangeEventAck);
3618
3619 SSMR3PutU32(pSSM, pThis->guestCaps);
3620
3621#ifdef VBOX_WITH_HGCM
3622 vmmdevHGCMSaveState(pThis, pSSM);
3623#endif /* VBOX_WITH_HGCM */
3624
3625 SSMR3PutU32(pSSM, pThis->fHostCursorRequested);
3626
3627 SSMR3PutU32(pSSM, pThis->guestInfo2.uFullVersion);
3628 SSMR3PutU32(pSSM, pThis->guestInfo2.uRevision);
3629 SSMR3PutU32(pSSM, pThis->guestInfo2.fFeatures);
3630 SSMR3PutStrZ(pSSM, pThis->guestInfo2.szName);
3631 SSMR3PutU32(pSSM, pThis->cFacilityStatuses);
3632 for (uint32_t i = 0; i < pThis->cFacilityStatuses; i++)
3633 {
3634 SSMR3PutU32(pSSM, pThis->aFacilityStatuses[i].enmFacility);
3635 SSMR3PutU32(pSSM, pThis->aFacilityStatuses[i].fFlags);
3636 SSMR3PutU16(pSSM, (uint16_t)pThis->aFacilityStatuses[i].enmStatus);
3637 SSMR3PutS64(pSSM, RTTimeSpecGetNano(&pThis->aFacilityStatuses[i].TimeSpecTS));
3638 }
3639
3640 /* Heartbeat: */
3641 SSMR3PutBool(pSSM, pThis->fHeartbeatActive);
3642 SSMR3PutBool(pSSM, pThis->fFlatlined);
3643 SSMR3PutU64(pSSM, pThis->nsLastHeartbeatTS);
3644 TMR3TimerSave(pThis->pFlatlinedTimer, pSSM);
3645
3646 PDMCritSectLeave(&pThis->CritSect);
3647 return VINF_SUCCESS;
3648}
3649
3650/**
3651 * @callback_method_impl{FNSSMDEVLOADEXEC}
3652 */
3653static DECLCALLBACK(int) vmmdevLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
3654{
3655 /** @todo The code load code is assuming we're always loaded into a freshly
3656 * constructed VM. */
3657 PVMMDEV pThis = PDMINS_2_DATA(pDevIns, PVMMDEV);
3658 int rc;
3659
3660 if ( uVersion > VMMDEV_SAVED_STATE_VERSION
3661 || uVersion < 6)
3662 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
3663
3664 /* config */
3665 if (uVersion > VMMDEV_SAVED_STATE_VERSION_VBOX_30)
3666 {
3667 bool f;
3668 rc = SSMR3GetBool(pSSM, &f); AssertRCReturn(rc, rc);
3669 if (pThis->fGetHostTimeDisabled != f)
3670 LogRel(("VMMDev: Config mismatch - fGetHostTimeDisabled: config=%RTbool saved=%RTbool\n", pThis->fGetHostTimeDisabled, f));
3671
3672 rc = SSMR3GetBool(pSSM, &f); AssertRCReturn(rc, rc);
3673 if (pThis->fBackdoorLogDisabled != f)
3674 LogRel(("VMMDev: Config mismatch - fBackdoorLogDisabled: config=%RTbool saved=%RTbool\n", pThis->fBackdoorLogDisabled, f));
3675
3676 rc = SSMR3GetBool(pSSM, &f); AssertRCReturn(rc, rc);
3677 if (pThis->fKeepCredentials != f)
3678 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Config mismatch - fKeepCredentials: config=%RTbool saved=%RTbool"),
3679 pThis->fKeepCredentials, f);
3680 rc = SSMR3GetBool(pSSM, &f); AssertRCReturn(rc, rc);
3681 if (pThis->fHeapEnabled != f)
3682 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Config mismatch - fHeapEnabled: config=%RTbool saved=%RTbool"),
3683 pThis->fHeapEnabled, f);
3684 }
3685
3686 if (uPass != SSM_PASS_FINAL)
3687 return VINF_SUCCESS;
3688
3689 /* state */
3690 SSMR3GetU32(pSSM, &pThis->hypervisorSize);
3691 SSMR3GetU32(pSSM, &pThis->mouseCapabilities);
3692 SSMR3GetS32(pSSM, &pThis->mouseXAbs);
3693 SSMR3GetS32(pSSM, &pThis->mouseYAbs);
3694
3695 SSMR3GetBool(pSSM, &pThis->fNewGuestFilterMask);
3696 SSMR3GetU32(pSSM, &pThis->u32NewGuestFilterMask);
3697 SSMR3GetU32(pSSM, &pThis->u32GuestFilterMask);
3698 SSMR3GetU32(pSSM, &pThis->u32HostEventFlags);
3699
3700 //SSMR3GetBool(pSSM, &pThis->pVMMDevRAMR3->fHaveEvents);
3701 // here be dragons (probably)
3702 SSMR3GetMem(pSSM, &pThis->pVMMDevRAMR3->V, sizeof (pThis->pVMMDevRAMR3->V));
3703
3704 SSMR3GetMem(pSSM, &pThis->guestInfo, sizeof (pThis->guestInfo));
3705 SSMR3GetU32(pSSM, &pThis->fu32AdditionsOk);
3706 SSMR3GetU32(pSSM, &pThis->u32VideoAccelEnabled);
3707 if (uVersion > 10)
3708 SSMR3GetBool(pSSM, &pThis->displayChangeData.fGuestSentChangeEventAck);
3709
3710 rc = SSMR3GetU32(pSSM, &pThis->guestCaps);
3711
3712 /* Attributes which were temporarily introduced in r30072 */
3713 if (uVersion == 7)
3714 {
3715 uint32_t temp;
3716 SSMR3GetU32(pSSM, &temp);
3717 rc = SSMR3GetU32(pSSM, &temp);
3718 }
3719 AssertRCReturn(rc, rc);
3720
3721#ifdef VBOX_WITH_HGCM
3722 rc = vmmdevHGCMLoadState(pThis, pSSM, uVersion);
3723 AssertRCReturn(rc, rc);
3724#endif /* VBOX_WITH_HGCM */
3725
3726 if (uVersion >= 10)
3727 rc = SSMR3GetU32(pSSM, &pThis->fHostCursorRequested);
3728 AssertRCReturn(rc, rc);
3729
3730 if (uVersion > VMMDEV_SAVED_STATE_VERSION_MISSING_GUEST_INFO_2)
3731 {
3732 SSMR3GetU32(pSSM, &pThis->guestInfo2.uFullVersion);
3733 SSMR3GetU32(pSSM, &pThis->guestInfo2.uRevision);
3734 SSMR3GetU32(pSSM, &pThis->guestInfo2.fFeatures);
3735 rc = SSMR3GetStrZ(pSSM, &pThis->guestInfo2.szName[0], sizeof(pThis->guestInfo2.szName));
3736 AssertRCReturn(rc, rc);
3737 }
3738
3739 if (uVersion > VMMDEV_SAVED_STATE_VERSION_MISSING_FACILITY_STATUSES)
3740 {
3741 uint32_t cFacilityStatuses;
3742 rc = SSMR3GetU32(pSSM, &cFacilityStatuses);
3743 AssertRCReturn(rc, rc);
3744
3745 for (uint32_t i = 0; i < cFacilityStatuses; i++)
3746 {
3747 uint32_t uFacility, fFlags;
3748 uint16_t uStatus;
3749 int64_t iTimeStampNano;
3750
3751 SSMR3GetU32(pSSM, &uFacility);
3752 SSMR3GetU32(pSSM, &fFlags);
3753 SSMR3GetU16(pSSM, &uStatus);
3754 rc = SSMR3GetS64(pSSM, &iTimeStampNano);
3755 AssertRCReturn(rc, rc);
3756
3757 PVMMDEVFACILITYSTATUSENTRY pEntry = vmmdevGetFacilityStatusEntry(pThis, (VBoxGuestFacilityType)uFacility);
3758 AssertLogRelMsgReturn(pEntry,
3759 ("VMMDev: Ran out of entries restoring the guest facility statuses. Saved state has %u.\n", cFacilityStatuses),
3760 VERR_OUT_OF_RESOURCES);
3761 pEntry->enmStatus = (VBoxGuestFacilityStatus)uStatus;
3762 pEntry->fFlags = fFlags;
3763 RTTimeSpecSetNano(&pEntry->TimeSpecTS, iTimeStampNano);
3764 }
3765 }
3766
3767 /*
3768 * Heartbeat.
3769 */
3770 if (uVersion >= VMMDEV_SAVED_STATE_VERSION_HEARTBEAT)
3771 {
3772 SSMR3GetBool(pSSM, (bool *)&pThis->fHeartbeatActive);
3773 SSMR3GetBool(pSSM, (bool *)&pThis->fFlatlined);
3774 SSMR3GetU64(pSSM, (uint64_t *)&pThis->nsLastHeartbeatTS);
3775 rc = TMR3TimerLoad(pThis->pFlatlinedTimer, pSSM);
3776 AssertRCReturn(rc, rc);
3777 if (pThis->fFlatlined)
3778 LogRel(("vmmdevLoadState: Guest has flatlined. Last heartbeat %'RU64 ns before state was saved.\n",
3779 TMTimerGetNano(pThis->pFlatlinedTimer) - pThis->nsLastHeartbeatTS));
3780 }
3781
3782 /*
3783 * On a resume, we send the capabilities changed message so
3784 * that listeners can sync their state again
3785 */
3786 Log(("vmmdevLoadState: capabilities changed (%x), informing connector\n", pThis->mouseCapabilities));
3787 if (pThis->pDrv)
3788 {
3789 pThis->pDrv->pfnUpdateMouseCapabilities(pThis->pDrv, pThis->mouseCapabilities);
3790 if (uVersion >= 10)
3791 pThis->pDrv->pfnUpdatePointerShape(pThis->pDrv,
3792 /*fVisible=*/!!pThis->fHostCursorRequested,
3793 /*fAlpha=*/false,
3794 /*xHot=*/0, /*yHot=*/0,
3795 /*cx=*/0, /*cy=*/0,
3796 /*pvShape=*/NULL);
3797 }
3798
3799 if (pThis->fu32AdditionsOk)
3800 {
3801 vmmdevLogGuestOsInfo(&pThis->guestInfo);
3802 if (pThis->pDrv)
3803 {
3804 if (pThis->guestInfo2.uFullVersion && pThis->pDrv->pfnUpdateGuestInfo2)
3805 pThis->pDrv->pfnUpdateGuestInfo2(pThis->pDrv, pThis->guestInfo2.uFullVersion, pThis->guestInfo2.szName,
3806 pThis->guestInfo2.uRevision, pThis->guestInfo2.fFeatures);
3807 if (pThis->pDrv->pfnUpdateGuestInfo)
3808 pThis->pDrv->pfnUpdateGuestInfo(pThis->pDrv, &pThis->guestInfo);
3809
3810 if (pThis->pDrv->pfnUpdateGuestStatus)
3811 {
3812 for (uint32_t i = 0; i < pThis->cFacilityStatuses; i++) /* ascending order! */
3813 if ( pThis->aFacilityStatuses[i].enmStatus != VBoxGuestFacilityStatus_Inactive
3814 || !pThis->aFacilityStatuses[i].fFixed)
3815 pThis->pDrv->pfnUpdateGuestStatus(pThis->pDrv,
3816 pThis->aFacilityStatuses[i].enmFacility,
3817 (uint16_t)pThis->aFacilityStatuses[i].enmStatus,
3818 pThis->aFacilityStatuses[i].fFlags,
3819 &pThis->aFacilityStatuses[i].TimeSpecTS);
3820 }
3821 }
3822 }
3823 if (pThis->pDrv && pThis->pDrv->pfnUpdateGuestCapabilities)
3824 pThis->pDrv->pfnUpdateGuestCapabilities(pThis->pDrv, pThis->guestCaps);
3825
3826 return VINF_SUCCESS;
3827}
3828
3829/**
3830 * Load state done callback. Notify guest of restore event.
3831 *
3832 * @returns VBox status code.
3833 * @param pDevIns The device instance.
3834 * @param pSSM The handle to the saved state.
3835 */
3836static DECLCALLBACK(int) vmmdevLoadStateDone(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
3837{
3838 RT_NOREF1(pSSM);
3839 PVMMDEV pThis = PDMINS_2_DATA(pDevIns, PVMMDEV);
3840
3841#ifdef VBOX_WITH_HGCM
3842 int rc = vmmdevHGCMLoadStateDone(pThis);
3843 AssertLogRelRCReturn(rc, rc);
3844#endif /* VBOX_WITH_HGCM */
3845
3846 /* Reestablish the acceleration status. */
3847 if ( pThis->u32VideoAccelEnabled
3848 && pThis->pDrv)
3849 {
3850 pThis->pDrv->pfnVideoAccelEnable(pThis->pDrv, !!pThis->u32VideoAccelEnabled, &pThis->pVMMDevRAMR3->vbvaMemory);
3851 }
3852
3853 VMMDevNotifyGuest(pThis, VMMDEV_EVENT_RESTORED);
3854
3855 return VINF_SUCCESS;
3856}
3857
3858
3859/* -=-=-=-=- PDMDEVREG -=-=-=-=- */
3860
3861/**
3862 * (Re-)initializes the MMIO2 data.
3863 *
3864 * @param pThis Pointer to the VMMDev instance data.
3865 */
3866static void vmmdevInitRam(PVMMDEV pThis)
3867{
3868 memset(pThis->pVMMDevRAMR3, 0, sizeof(VMMDevMemory));
3869 pThis->pVMMDevRAMR3->u32Size = sizeof(VMMDevMemory);
3870 pThis->pVMMDevRAMR3->u32Version = VMMDEV_MEMORY_VERSION;
3871}
3872
3873
3874/**
3875 * @interface_method_impl{PDMDEVREG,pfnReset}
3876 */
3877static DECLCALLBACK(void) vmmdevReset(PPDMDEVINS pDevIns)
3878{
3879 PVMMDEV pThis = PDMINS_2_DATA(pDevIns, PVMMDEV);
3880 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
3881
3882 /*
3883 * Reset the mouse integration feature bits
3884 */
3885 if (pThis->mouseCapabilities & VMMDEV_MOUSE_GUEST_MASK)
3886 {
3887 pThis->mouseCapabilities &= ~VMMDEV_MOUSE_GUEST_MASK;
3888 /* notify the connector */
3889 Log(("vmmdevReset: capabilities changed (%x), informing connector\n", pThis->mouseCapabilities));
3890 pThis->pDrv->pfnUpdateMouseCapabilities(pThis->pDrv, pThis->mouseCapabilities);
3891 }
3892 pThis->fHostCursorRequested = false;
3893
3894 pThis->hypervisorSize = 0;
3895
3896 /* re-initialize the VMMDev memory */
3897 if (pThis->pVMMDevRAMR3)
3898 vmmdevInitRam(pThis);
3899
3900 /* credentials have to go away (by default) */
3901 if (!pThis->fKeepCredentials)
3902 {
3903 memset(pThis->pCredentials->Logon.szUserName, '\0', VMMDEV_CREDENTIALS_SZ_SIZE);
3904 memset(pThis->pCredentials->Logon.szPassword, '\0', VMMDEV_CREDENTIALS_SZ_SIZE);
3905 memset(pThis->pCredentials->Logon.szDomain, '\0', VMMDEV_CREDENTIALS_SZ_SIZE);
3906 }
3907 memset(pThis->pCredentials->Judge.szUserName, '\0', VMMDEV_CREDENTIALS_SZ_SIZE);
3908 memset(pThis->pCredentials->Judge.szPassword, '\0', VMMDEV_CREDENTIALS_SZ_SIZE);
3909 memset(pThis->pCredentials->Judge.szDomain, '\0', VMMDEV_CREDENTIALS_SZ_SIZE);
3910
3911 /* Reset means that additions will report again. */
3912 const bool fVersionChanged = pThis->fu32AdditionsOk
3913 || pThis->guestInfo.interfaceVersion
3914 || pThis->guestInfo.osType != VBOXOSTYPE_Unknown;
3915 if (fVersionChanged)
3916 Log(("vmmdevReset: fu32AdditionsOk=%d additionsVersion=%x osType=%#x\n",
3917 pThis->fu32AdditionsOk, pThis->guestInfo.interfaceVersion, pThis->guestInfo.osType));
3918 pThis->fu32AdditionsOk = false;
3919 memset (&pThis->guestInfo, 0, sizeof (pThis->guestInfo));
3920 RT_ZERO(pThis->guestInfo2);
3921 const bool fCapsChanged = pThis->guestCaps != 0; /* Report transition to 0. */
3922 pThis->guestCaps = 0;
3923
3924 /* Clear facilities. No need to tell Main as it will get a
3925 pfnUpdateGuestInfo callback. */
3926 RTTIMESPEC TimeStampNow;
3927 RTTimeNow(&TimeStampNow);
3928 uint32_t iFacility = pThis->cFacilityStatuses;
3929 while (iFacility-- > 0)
3930 {
3931 pThis->aFacilityStatuses[iFacility].enmStatus = VBoxGuestFacilityStatus_Inactive;
3932 pThis->aFacilityStatuses[iFacility].TimeSpecTS = TimeStampNow;
3933 }
3934
3935 /* clear pending display change request. */
3936 for (unsigned i = 0; i < RT_ELEMENTS(pThis->displayChangeData.aRequests); i++)
3937 {
3938 DISPLAYCHANGEREQUEST *pRequest = &pThis->displayChangeData.aRequests[i];
3939 memset (&pRequest->lastReadDisplayChangeRequest, 0, sizeof (pRequest->lastReadDisplayChangeRequest));
3940 }
3941 pThis->displayChangeData.iCurrentMonitor = 0;
3942 pThis->displayChangeData.fGuestSentChangeEventAck = false;
3943
3944 /* disable seamless mode */
3945 pThis->fLastSeamlessEnabled = false;
3946
3947 /* disabled memory ballooning */
3948 pThis->cMbMemoryBalloonLast = 0;
3949
3950 /* disabled statistics updating */
3951 pThis->u32LastStatIntervalSize = 0;
3952
3953#ifdef VBOX_WITH_HGCM
3954 /* Clear the "HGCM event enabled" flag so the event can be automatically reenabled. */
3955 pThis->u32HGCMEnabled = 0;
3956#endif
3957
3958 /*
3959 * Deactive heartbeat.
3960 */
3961 if (pThis->fHeartbeatActive)
3962 {
3963 TMTimerStop(pThis->pFlatlinedTimer);
3964 pThis->fFlatlined = false;
3965 pThis->fHeartbeatActive = true;
3966 }
3967
3968 /*
3969 * Clear the event variables.
3970 *
3971 * XXX By design we should NOT clear pThis->u32HostEventFlags because it is designed
3972 * that way so host events do not depend on guest resets. However, the pending
3973 * event flags actually _were_ cleared since ages so we mask out events from
3974 * clearing which we really need to survive the reset. See xtracker 5767.
3975 */
3976 pThis->u32HostEventFlags &= VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST;
3977 pThis->u32GuestFilterMask = 0;
3978 pThis->u32NewGuestFilterMask = 0;
3979 pThis->fNewGuestFilterMask = 0;
3980
3981 /*
3982 * Call the update functions as required.
3983 */
3984 if (fVersionChanged && pThis->pDrv && pThis->pDrv->pfnUpdateGuestInfo)
3985 pThis->pDrv->pfnUpdateGuestInfo(pThis->pDrv, &pThis->guestInfo);
3986 if (fCapsChanged && pThis->pDrv && pThis->pDrv->pfnUpdateGuestCapabilities)
3987 pThis->pDrv->pfnUpdateGuestCapabilities(pThis->pDrv, pThis->guestCaps);
3988
3989 /*
3990 * Generate a unique session id for this VM; it will be changed for each start, reset or restore.
3991 * This can be used for restore detection inside the guest.
3992 */
3993 pThis->idSession = ASMReadTSC();
3994
3995 PDMCritSectLeave(&pThis->CritSect);
3996}
3997
3998
3999/**
4000 * @interface_method_impl{PDMDEVREG,pfnRelocate}
4001 */
4002static DECLCALLBACK(void) vmmdevRelocate(PPDMDEVINS pDevIns, RTGCINTPTR offDelta)
4003{
4004 NOREF(pDevIns);
4005 NOREF(offDelta);
4006}
4007
4008
4009/**
4010 * @interface_method_impl{PDMDEVREG,pfnDestruct}
4011 */
4012static DECLCALLBACK(int) vmmdevDestruct(PPDMDEVINS pDevIns)
4013{
4014 PVMMDEV pThis = PDMINS_2_DATA(pDevIns, PVMMDEV);
4015 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
4016
4017 /*
4018 * Wipe and free the credentials.
4019 */
4020 if (pThis->pCredentials)
4021 {
4022 RTMemWipeThoroughly(pThis->pCredentials, sizeof(*pThis->pCredentials), 10);
4023 RTMemFree(pThis->pCredentials);
4024 pThis->pCredentials = NULL;
4025 }
4026
4027#ifdef VBOX_WITH_HGCM
4028 vmmdevHGCMDestroy(pThis);
4029 RTCritSectDelete(&pThis->critsectHGCMCmdList);
4030#endif
4031
4032#ifndef VBOX_WITHOUT_TESTING_FEATURES
4033 /*
4034 * Clean up the testing device.
4035 */
4036 vmmdevTestingTerminate(pDevIns);
4037#endif
4038
4039 return VINF_SUCCESS;
4040}
4041
4042
4043/**
4044 * @interface_method_impl{PDMDEVREG,pfnConstruct}
4045 */
4046static DECLCALLBACK(int) vmmdevConstruct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
4047{
4048 PVMMDEV pThis = PDMINS_2_DATA(pDevIns, PVMMDEV);
4049 int rc;
4050
4051 Assert(iInstance == 0);
4052 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
4053
4054 /*
4055 * Initialize data (most of it anyway).
4056 */
4057 /* Save PDM device instance data for future reference. */
4058 pThis->pDevIns = pDevIns;
4059
4060 /* PCI vendor, just a free bogus value */
4061 PCIDevSetVendorId(&pThis->PciDev, 0x80ee);
4062 /* device ID */
4063 PCIDevSetDeviceId(&pThis->PciDev, 0xcafe);
4064 /* class sub code (other type of system peripheral) */
4065 PCIDevSetClassSub(&pThis->PciDev, 0x80);
4066 /* class base code (base system peripheral) */
4067 PCIDevSetClassBase(&pThis->PciDev, 0x08);
4068 /* header type */
4069 PCIDevSetHeaderType(&pThis->PciDev, 0x00);
4070 /* interrupt on pin 0 */
4071 PCIDevSetInterruptPin(&pThis->PciDev, 0x01);
4072
4073 RTTIMESPEC TimeStampNow;
4074 RTTimeNow(&TimeStampNow);
4075 vmmdevAllocFacilityStatusEntry(pThis, VBoxGuestFacilityType_VBoxGuestDriver, true /*fFixed*/, &TimeStampNow);
4076 vmmdevAllocFacilityStatusEntry(pThis, VBoxGuestFacilityType_VBoxService, true /*fFixed*/, &TimeStampNow);
4077 vmmdevAllocFacilityStatusEntry(pThis, VBoxGuestFacilityType_VBoxTrayClient, true /*fFixed*/, &TimeStampNow);
4078 vmmdevAllocFacilityStatusEntry(pThis, VBoxGuestFacilityType_Seamless, true /*fFixed*/, &TimeStampNow);
4079 vmmdevAllocFacilityStatusEntry(pThis, VBoxGuestFacilityType_Graphics, true /*fFixed*/, &TimeStampNow);
4080 Assert(pThis->cFacilityStatuses == 5);
4081
4082 /*
4083 * Interfaces
4084 */
4085 /* IBase */
4086 pThis->IBase.pfnQueryInterface = vmmdevPortQueryInterface;
4087
4088 /* VMMDev port */
4089 pThis->IPort.pfnQueryAbsoluteMouse = vmmdevIPort_QueryAbsoluteMouse;
4090 pThis->IPort.pfnSetAbsoluteMouse = vmmdevIPort_SetAbsoluteMouse ;
4091 pThis->IPort.pfnQueryMouseCapabilities = vmmdevIPort_QueryMouseCapabilities;
4092 pThis->IPort.pfnUpdateMouseCapabilities = vmmdevIPort_UpdateMouseCapabilities;
4093 pThis->IPort.pfnRequestDisplayChange = vmmdevIPort_RequestDisplayChange;
4094 pThis->IPort.pfnSetCredentials = vmmdevIPort_SetCredentials;
4095 pThis->IPort.pfnVBVAChange = vmmdevIPort_VBVAChange;
4096 pThis->IPort.pfnRequestSeamlessChange = vmmdevIPort_RequestSeamlessChange;
4097 pThis->IPort.pfnSetMemoryBalloon = vmmdevIPort_SetMemoryBalloon;
4098 pThis->IPort.pfnSetStatisticsInterval = vmmdevIPort_SetStatisticsInterval;
4099 pThis->IPort.pfnVRDPChange = vmmdevIPort_VRDPChange;
4100 pThis->IPort.pfnCpuHotUnplug = vmmdevIPort_CpuHotUnplug;
4101 pThis->IPort.pfnCpuHotPlug = vmmdevIPort_CpuHotPlug;
4102
4103 /* Shared folder LED */
4104 pThis->SharedFolders.Led.u32Magic = PDMLED_MAGIC;
4105 pThis->SharedFolders.ILeds.pfnQueryStatusLed = vmmdevQueryStatusLed;
4106
4107#ifdef VBOX_WITH_HGCM
4108 /* HGCM port */
4109 pThis->IHGCMPort.pfnCompleted = hgcmCompleted;
4110#endif
4111
4112 pThis->pCredentials = (VMMDEVCREDS *)RTMemAllocZ(sizeof(*pThis->pCredentials));
4113 if (!pThis->pCredentials)
4114 return VERR_NO_MEMORY;
4115
4116
4117 /*
4118 * Validate and read the configuration.
4119 */
4120 PDMDEV_VALIDATE_CONFIG_RETURN(pDevIns,
4121 "GetHostTimeDisabled|"
4122 "BackdoorLogDisabled|"
4123 "KeepCredentials|"
4124 "HeapEnabled|"
4125 "RZEnabled|"
4126 "GuestCoreDumpEnabled|"
4127 "GuestCoreDumpDir|"
4128 "GuestCoreDumpCount|"
4129 "HeartbeatInterval|"
4130 "HeartbeatTimeout|"
4131 "TestingEnabled|"
4132 "TestingMMIO|"
4133 "TestintXmlOutputFile"
4134 ,
4135 "");
4136
4137 rc = CFGMR3QueryBoolDef(pCfg, "GetHostTimeDisabled", &pThis->fGetHostTimeDisabled, false);
4138 if (RT_FAILURE(rc))
4139 return PDMDEV_SET_ERROR(pDevIns, rc,
4140 N_("Configuration error: Failed querying \"GetHostTimeDisabled\" as a boolean"));
4141
4142 rc = CFGMR3QueryBoolDef(pCfg, "BackdoorLogDisabled", &pThis->fBackdoorLogDisabled, false);
4143 if (RT_FAILURE(rc))
4144 return PDMDEV_SET_ERROR(pDevIns, rc,
4145 N_("Configuration error: Failed querying \"BackdoorLogDisabled\" as a boolean"));
4146
4147 rc = CFGMR3QueryBoolDef(pCfg, "KeepCredentials", &pThis->fKeepCredentials, false);
4148 if (RT_FAILURE(rc))
4149 return PDMDEV_SET_ERROR(pDevIns, rc,
4150 N_("Configuration error: Failed querying \"KeepCredentials\" as a boolean"));
4151
4152 rc = CFGMR3QueryBoolDef(pCfg, "HeapEnabled", &pThis->fHeapEnabled, true);
4153 if (RT_FAILURE(rc))
4154 return PDMDEV_SET_ERROR(pDevIns, rc,
4155 N_("Configuration error: Failed querying \"HeapEnabled\" as a boolean"));
4156
4157 rc = CFGMR3QueryBoolDef(pCfg, "RZEnabled", &pThis->fRZEnabled, true);
4158 if (RT_FAILURE(rc))
4159 return PDMDEV_SET_ERROR(pDevIns, rc,
4160 N_("Configuration error: Failed querying \"RZEnabled\" as a boolean"));
4161
4162 rc = CFGMR3QueryBoolDef(pCfg, "GuestCoreDumpEnabled", &pThis->fGuestCoreDumpEnabled, false);
4163 if (RT_FAILURE(rc))
4164 return PDMDEV_SET_ERROR(pDevIns, rc,
4165 N_("Configuration error: Failed querying \"GuestCoreDumpEnabled\" as a boolean"));
4166
4167 char *pszGuestCoreDumpDir = NULL;
4168 rc = CFGMR3QueryStringAllocDef(pCfg, "GuestCoreDumpDir", &pszGuestCoreDumpDir, "");
4169 if (RT_FAILURE(rc))
4170 return PDMDEV_SET_ERROR(pDevIns, rc,
4171 N_("Configuration error: Failed querying \"GuestCoreDumpDir\" as a string"));
4172
4173 RTStrCopy(pThis->szGuestCoreDumpDir, sizeof(pThis->szGuestCoreDumpDir), pszGuestCoreDumpDir);
4174 MMR3HeapFree(pszGuestCoreDumpDir);
4175
4176 rc = CFGMR3QueryU32Def(pCfg, "GuestCoreDumpCount", &pThis->cGuestCoreDumps, 3);
4177 if (RT_FAILURE(rc))
4178 return PDMDEV_SET_ERROR(pDevIns, rc,
4179 N_("Configuration error: Failed querying \"GuestCoreDumpCount\" as a 32-bit unsigned integer"));
4180
4181 rc = CFGMR3QueryU64Def(pCfg, "HeartbeatInterval", &pThis->cNsHeartbeatInterval, VMMDEV_HEARTBEAT_DEFAULT_INTERVAL);
4182 if (RT_FAILURE(rc))
4183 return PDMDEV_SET_ERROR(pDevIns, rc,
4184 N_("Configuration error: Failed querying \"HeartbeatInterval\" as a 64-bit unsigned integer"));
4185 if (pThis->cNsHeartbeatInterval < RT_NS_100MS / 2)
4186 return PDMDEV_SET_ERROR(pDevIns, rc,
4187 N_("Configuration error: Heartbeat interval \"HeartbeatInterval\" too small"));
4188
4189 rc = CFGMR3QueryU64Def(pCfg, "HeartbeatTimeout", &pThis->cNsHeartbeatTimeout, pThis->cNsHeartbeatInterval * 2);
4190 if (RT_FAILURE(rc))
4191 return PDMDEV_SET_ERROR(pDevIns, rc,
4192 N_("Configuration error: Failed querying \"HeartbeatTimeout\" as a 64-bit unsigned integer"));
4193 if (pThis->cNsHeartbeatTimeout < RT_NS_100MS)
4194 return PDMDEV_SET_ERROR(pDevIns, rc,
4195 N_("Configuration error: Heartbeat timeout \"HeartbeatTimeout\" too small"));
4196 if (pThis->cNsHeartbeatTimeout <= pThis->cNsHeartbeatInterval + RT_NS_10MS)
4197 return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
4198 N_("Configuration error: Heartbeat timeout \"HeartbeatTimeout\" value (%'ull ns) is too close to the interval (%'ull ns)"),
4199 pThis->cNsHeartbeatTimeout, pThis->cNsHeartbeatInterval);
4200
4201#ifndef VBOX_WITHOUT_TESTING_FEATURES
4202 rc = CFGMR3QueryBoolDef(pCfg, "TestingEnabled", &pThis->fTestingEnabled, false);
4203 if (RT_FAILURE(rc))
4204 return PDMDEV_SET_ERROR(pDevIns, rc,
4205 N_("Configuration error: Failed querying \"TestingEnabled\" as a boolean"));
4206 rc = CFGMR3QueryBoolDef(pCfg, "TestingMMIO", &pThis->fTestingMMIO, false);
4207 if (RT_FAILURE(rc))
4208 return PDMDEV_SET_ERROR(pDevIns, rc,
4209 N_("Configuration error: Failed querying \"TestingMMIO\" as a boolean"));
4210 rc = CFGMR3QueryStringAllocDef(pCfg, "TestintXmlOutputFile", &pThis->pszTestingXmlOutput, NULL);
4211 if (RT_FAILURE(rc))
4212 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Failed querying \"TestintXmlOutputFile\" as a string"));
4213
4214 /** @todo image-to-load-filename? */
4215#endif
4216
4217 pThis->cbGuestRAM = MMR3PhysGetRamSize(PDMDevHlpGetVM(pDevIns));
4218
4219 /*
4220 * We do our own locking entirely. So, install NOP critsect for the device
4221 * and create our own critsect for use where it really matters (++).
4222 */
4223 rc = PDMDevHlpSetDeviceCritSect(pDevIns, PDMDevHlpCritSectGetNop(pDevIns));
4224 AssertRCReturn(rc, rc);
4225 rc = PDMDevHlpCritSectInit(pDevIns, &pThis->CritSect, RT_SRC_POS, "VMMDev#%u", iInstance);
4226 AssertRCReturn(rc, rc);
4227
4228 /*
4229 * Register the backdoor logging port
4230 */
4231 rc = PDMDevHlpIOPortRegister(pDevIns, RTLOG_DEBUG_PORT, 1, NULL, vmmdevBackdoorLog,
4232 NULL, NULL, NULL, "VMMDev backdoor logging");
4233 AssertRCReturn(rc, rc);
4234
4235#ifdef VMMDEV_WITH_ALT_TIMESYNC
4236 /*
4237 * Alternative timesync source.
4238 *
4239 * This was orignally added for creating a simple time sync service in an
4240 * OpenBSD guest without requiring VBoxGuest and VBoxService to be ported
4241 * first. We keep it in case it comes in handy.
4242 */
4243 rc = PDMDevHlpIOPortRegister(pDevIns, 0x505, 1, NULL,
4244 vmmdevAltTimeSyncWrite, vmmdevAltTimeSyncRead,
4245 NULL, NULL, "VMMDev timesync backdoor");
4246 AssertRCReturn(rc, rc);
4247#endif
4248
4249 /*
4250 * Register the PCI device.
4251 */
4252 rc = PDMDevHlpPCIRegister(pDevIns, &pThis->PciDev);
4253 if (RT_FAILURE(rc))
4254 return rc;
4255 if (pThis->PciDev.uDevFn != 32 || iInstance != 0)
4256 Log(("!!WARNING!!: pThis->PciDev.uDevFn=%d (ignore if testcase or no started by Main)\n", pThis->PciDev.uDevFn));
4257 rc = PDMDevHlpPCIIORegionRegister(pDevIns, 0, 0x20, PCI_ADDRESS_SPACE_IO, vmmdevIOPortRegionMap);
4258 if (RT_FAILURE(rc))
4259 return rc;
4260 rc = PDMDevHlpPCIIORegionRegister(pDevIns, 1, VMMDEV_RAM_SIZE, PCI_ADDRESS_SPACE_MEM, vmmdevIORAMRegionMap);
4261 if (RT_FAILURE(rc))
4262 return rc;
4263 if (pThis->fHeapEnabled)
4264 {
4265 rc = PDMDevHlpPCIIORegionRegister(pDevIns, 2, VMMDEV_HEAP_SIZE, PCI_ADDRESS_SPACE_MEM_PREFETCH, vmmdevIORAMRegionMap);
4266 if (RT_FAILURE(rc))
4267 return rc;
4268 }
4269
4270 /*
4271 * Allocate and initialize the MMIO2 memory.
4272 */
4273 rc = PDMDevHlpMMIO2Register(pDevIns, &pThis->PciDev, 1 /*iRegion*/, VMMDEV_RAM_SIZE, 0 /*fFlags*/,
4274 (void **)&pThis->pVMMDevRAMR3, "VMMDev");
4275 if (RT_FAILURE(rc))
4276 return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
4277 N_("Failed to allocate %u bytes of memory for the VMM device"), VMMDEV_RAM_SIZE);
4278 vmmdevInitRam(pThis);
4279
4280 if (pThis->fHeapEnabled)
4281 {
4282 rc = PDMDevHlpMMIO2Register(pDevIns, &pThis->PciDev, 2 /*iRegion*/, VMMDEV_HEAP_SIZE, 0 /*fFlags*/,
4283 (void **)&pThis->pVMMDevHeapR3, "VMMDev Heap");
4284 if (RT_FAILURE(rc))
4285 return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
4286 N_("Failed to allocate %u bytes of memory for the VMM device heap"), PAGE_SIZE);
4287
4288 /* Register the memory area with PDM so HM can access it before it's mapped. */
4289 rc = PDMDevHlpRegisterVMMDevHeap(pDevIns, NIL_RTGCPHYS, pThis->pVMMDevHeapR3, VMMDEV_HEAP_SIZE);
4290 AssertLogRelRCReturn(rc, rc);
4291 }
4292
4293#ifndef VBOX_WITHOUT_TESTING_FEATURES
4294 /*
4295 * Initialize testing.
4296 */
4297 rc = vmmdevTestingInitialize(pDevIns);
4298 if (RT_FAILURE(rc))
4299 return rc;
4300#endif
4301
4302 /*
4303 * Get the corresponding connector interface
4304 */
4305 rc = PDMDevHlpDriverAttach(pDevIns, 0, &pThis->IBase, &pThis->pDrvBase, "VMM Driver Port");
4306 if (RT_SUCCESS(rc))
4307 {
4308 pThis->pDrv = PDMIBASE_QUERY_INTERFACE(pThis->pDrvBase, PDMIVMMDEVCONNECTOR);
4309 AssertMsgReturn(pThis->pDrv, ("LUN #0 doesn't have a VMMDev connector interface!\n"), VERR_PDM_MISSING_INTERFACE);
4310#ifdef VBOX_WITH_HGCM
4311 pThis->pHGCMDrv = PDMIBASE_QUERY_INTERFACE(pThis->pDrvBase, PDMIHGCMCONNECTOR);
4312 if (!pThis->pHGCMDrv)
4313 {
4314 Log(("LUN #0 doesn't have a HGCM connector interface, HGCM is not supported. rc=%Rrc\n", rc));
4315 /* this is not actually an error, just means that there is no support for HGCM */
4316 }
4317#endif
4318 /* Query the initial balloon size. */
4319 AssertPtr(pThis->pDrv->pfnQueryBalloonSize);
4320 rc = pThis->pDrv->pfnQueryBalloonSize(pThis->pDrv, &pThis->cMbMemoryBalloon);
4321 AssertRC(rc);
4322
4323 Log(("Initial balloon size %x\n", pThis->cMbMemoryBalloon));
4324 }
4325 else if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
4326 {
4327 Log(("%s/%d: warning: no driver attached to LUN #0!\n", pDevIns->pReg->szName, pDevIns->iInstance));
4328 rc = VINF_SUCCESS;
4329 }
4330 else
4331 AssertMsgFailedReturn(("Failed to attach LUN #0! rc=%Rrc\n", rc), rc);
4332
4333 /*
4334 * Attach status driver for shared folders (optional).
4335 */
4336 PPDMIBASE pBase;
4337 rc = PDMDevHlpDriverAttach(pDevIns, PDM_STATUS_LUN, &pThis->IBase, &pBase, "Status Port");
4338 if (RT_SUCCESS(rc))
4339 pThis->SharedFolders.pLedsConnector = PDMIBASE_QUERY_INTERFACE(pBase, PDMILEDCONNECTORS);
4340 else if (rc != VERR_PDM_NO_ATTACHED_DRIVER)
4341 {
4342 AssertMsgFailed(("Failed to attach to status driver. rc=%Rrc\n", rc));
4343 return rc;
4344 }
4345
4346 /*
4347 * Register saved state and init the HGCM CmdList critsect.
4348 */
4349 rc = PDMDevHlpSSMRegisterEx(pDevIns, VMMDEV_SAVED_STATE_VERSION, sizeof(*pThis), NULL,
4350 NULL, vmmdevLiveExec, NULL,
4351 NULL, vmmdevSaveExec, NULL,
4352 NULL, vmmdevLoadExec, vmmdevLoadStateDone);
4353 AssertRCReturn(rc, rc);
4354
4355 /*
4356 * Create heartbeat checking timer.
4357 */
4358 rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL, vmmDevHeartbeatFlatlinedTimer, pThis,
4359 TMTIMER_FLAGS_NO_CRIT_SECT, "Heartbeat flatlined", &pThis->pFlatlinedTimer);
4360 AssertRCReturn(rc, rc);
4361
4362#ifdef VBOX_WITH_HGCM
4363 RTListInit(&pThis->listHGCMCmd);
4364 rc = RTCritSectInit(&pThis->critsectHGCMCmdList);
4365 AssertRCReturn(rc, rc);
4366 pThis->u32HGCMEnabled = 0;
4367#endif /* VBOX_WITH_HGCM */
4368
4369 /*
4370 * In this version of VirtualBox the GUI checks whether "needs host cursor"
4371 * changes.
4372 */
4373 pThis->mouseCapabilities |= VMMDEV_MOUSE_HOST_RECHECKS_NEEDS_HOST_CURSOR;
4374
4375 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatMemBalloonChunks, STAMTYPE_U32, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "Memory balloon size", "/Devices/VMMDev/BalloonChunks");
4376
4377 /*
4378 * Generate a unique session id for this VM; it will be changed for each
4379 * start, reset or restore. This can be used for restore detection inside
4380 * the guest.
4381 */
4382 pThis->idSession = ASMReadTSC();
4383 return rc;
4384}
4385
4386/**
4387 * The device registration structure.
4388 */
4389extern "C" const PDMDEVREG g_DeviceVMMDev =
4390{
4391 /* u32Version */
4392 PDM_DEVREG_VERSION,
4393 /* szName */
4394 "VMMDev",
4395 /* szRCMod */
4396 "VBoxDDRC.rc",
4397 /* szR0Mod */
4398 "VBoxDDR0.r0",
4399 /* pszDescription */
4400 "VirtualBox VMM Device\n",
4401 /* fFlags */
4402 PDM_DEVREG_FLAGS_HOST_BITS_DEFAULT | PDM_DEVREG_FLAGS_GUEST_BITS_DEFAULT | PDM_DEVREG_FLAGS_RC | PDM_DEVREG_FLAGS_R0,
4403 /* fClass */
4404 PDM_DEVREG_CLASS_VMM_DEV,
4405 /* cMaxInstances */
4406 1,
4407 /* cbInstance */
4408 sizeof(VMMDevState),
4409 /* pfnConstruct */
4410 vmmdevConstruct,
4411 /* pfnDestruct */
4412 vmmdevDestruct,
4413 /* pfnRelocate */
4414 vmmdevRelocate,
4415 /* pfnMemSetup */
4416 NULL,
4417 /* pfnPowerOn */
4418 NULL,
4419 /* pfnReset */
4420 vmmdevReset,
4421 /* pfnSuspend */
4422 NULL,
4423 /* pfnResume */
4424 NULL,
4425 /* pfnAttach */
4426 NULL,
4427 /* pfnDetach */
4428 NULL,
4429 /* pfnQueryInterface. */
4430 NULL,
4431 /* pfnInitComplete */
4432 NULL,
4433 /* pfnPowerOff */
4434 NULL,
4435 /* pfnSoftReset */
4436 NULL,
4437 /* u32VersionEnd */
4438 PDM_DEVREG_VERSION
4439};
4440#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
4441
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