VirtualBox

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

Last change on this file since 76408 was 76196, checked in by vboxsync, 6 years ago

VMMDev/HGCM,VBoxGuest: New page list variant for more effectively passing physical contiguous buffers (VbglR0PhysHeapAlloc, kmalloc, ++). bugref:9172

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

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