VirtualBox

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

Last change on this file since 77231 was 77056, checked in by vboxsync, 6 years ago

VMMDev: Simplify VMMDevReq_HGCMCall,VMMDevReq_HGCMCall32, and VMMDevReq_HGCMCall64 definitions. 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 77056 2019-01-30 18:00:43Z vboxsync $ */
2/** @file
3 * VMMDev - Guest <-> VMM/Host communication device.
4 */
5
6/*
7 * Copyright (C) 2006-2019 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_HGCMCall32 and VMMDevReq_HGCMCall64.
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_HGCMCall64:
2745# endif
2746 case VMMDevReq_HGCMCall32:
2747 vmmdevReqHdrSetHgcmAsyncExecute(pThis, GCPhysReqHdr, *ppLock);
2748 pReqHdr->rc = vmmdevReqHandler_HGCMCall(pThis, pReqHdr, GCPhysReqHdr, tsArrival, ppLock);
2749 Assert(pReqHdr->rc == VINF_HGCM_ASYNC_EXECUTE || RT_FAILURE_NP(pReqHdr->rc));
2750 if (RT_SUCCESS(pReqHdr->rc))
2751 *pfPostOptimize |= VMMDEVREQDISP_POST_F_NO_WRITE_OUT;
2752 break;
2753
2754 case VMMDevReq_HGCMCancel:
2755 pReqHdr->rc = vmmdevReqHandler_HGCMCancel(pThis, pReqHdr, GCPhysReqHdr);
2756 break;
2757
2758 case VMMDevReq_HGCMCancel2:
2759 pReqHdr->rc = vmmdevReqHandler_HGCMCancel2(pThis, pReqHdr);
2760 break;
2761#endif /* VBOX_WITH_HGCM */
2762
2763 case VMMDevReq_VideoAccelEnable:
2764 pReqHdr->rc = vmmdevReqHandler_VideoAccelEnable(pThis, pReqHdr);
2765 break;
2766
2767 case VMMDevReq_VideoAccelFlush:
2768 pReqHdr->rc = vmmdevReqHandler_VideoAccelFlush(pThis, pReqHdr);
2769 break;
2770
2771 case VMMDevReq_VideoSetVisibleRegion:
2772 pReqHdr->rc = vmmdevReqHandler_VideoSetVisibleRegion(pThis, pReqHdr);
2773 break;
2774
2775 case VMMDevReq_GetSeamlessChangeRequest:
2776 pReqHdr->rc = vmmdevReqHandler_GetSeamlessChangeRequest(pThis, pReqHdr);
2777 break;
2778
2779 case VMMDevReq_GetVRDPChangeRequest:
2780 pReqHdr->rc = vmmdevReqHandler_GetVRDPChangeRequest(pThis, pReqHdr);
2781 break;
2782
2783 case VMMDevReq_GetMemBalloonChangeRequest:
2784 pReqHdr->rc = vmmdevReqHandler_GetMemBalloonChangeRequest(pThis, pReqHdr);
2785 break;
2786
2787 case VMMDevReq_ChangeMemBalloon:
2788 pReqHdr->rc = vmmdevReqHandler_ChangeMemBalloon(pThis, pReqHdr);
2789 break;
2790
2791 case VMMDevReq_GetStatisticsChangeRequest:
2792 pReqHdr->rc = vmmdevReqHandler_GetStatisticsChangeRequest(pThis, pReqHdr);
2793 break;
2794
2795 case VMMDevReq_ReportGuestStats:
2796 pReqHdr->rc = vmmdevReqHandler_ReportGuestStats(pThis, pReqHdr);
2797 break;
2798
2799 case VMMDevReq_QueryCredentials:
2800 pReqHdr->rc = vmmdevReqHandler_QueryCredentials(pThis, pReqHdr);
2801 break;
2802
2803 case VMMDevReq_ReportCredentialsJudgement:
2804 pReqHdr->rc = vmmdevReqHandler_ReportCredentialsJudgement(pThis, pReqHdr);
2805 break;
2806
2807 case VMMDevReq_GetHostVersion:
2808 pReqHdr->rc = vmmdevReqHandler_GetHostVersion(pReqHdr);
2809 break;
2810
2811 case VMMDevReq_GetCpuHotPlugRequest:
2812 pReqHdr->rc = vmmdevReqHandler_GetCpuHotPlugRequest(pThis, pReqHdr);
2813 break;
2814
2815 case VMMDevReq_SetCpuHotPlugStatus:
2816 pReqHdr->rc = vmmdevReqHandler_SetCpuHotPlugStatus(pThis, pReqHdr);
2817 break;
2818
2819#ifdef VBOX_WITH_PAGE_SHARING
2820 case VMMDevReq_RegisterSharedModule:
2821 pReqHdr->rc = vmmdevReqHandler_RegisterSharedModule(pThis, pReqHdr);
2822 break;
2823
2824 case VMMDevReq_UnregisterSharedModule:
2825 pReqHdr->rc = vmmdevReqHandler_UnregisterSharedModule(pThis, pReqHdr);
2826 break;
2827
2828 case VMMDevReq_CheckSharedModules:
2829 pReqHdr->rc = vmmdevReqHandler_CheckSharedModules(pThis, pReqHdr);
2830 break;
2831
2832 case VMMDevReq_GetPageSharingStatus:
2833 pReqHdr->rc = vmmdevReqHandler_GetPageSharingStatus(pThis, pReqHdr);
2834 break;
2835
2836 case VMMDevReq_DebugIsPageShared:
2837 pReqHdr->rc = vmmdevReqHandler_DebugIsPageShared(pThis, pReqHdr);
2838 break;
2839
2840#endif /* VBOX_WITH_PAGE_SHARING */
2841
2842#ifdef DEBUG
2843 case VMMDevReq_LogString:
2844 pReqHdr->rc = vmmdevReqHandler_LogString(pReqHdr);
2845 break;
2846#endif
2847
2848 case VMMDevReq_GetSessionId:
2849 pReqHdr->rc = vmmdevReqHandler_GetSessionId(pThis, pReqHdr);
2850 break;
2851
2852 /*
2853 * Guest wants to give up a timeslice.
2854 * Note! This was only ever used by experimental GAs!
2855 */
2856 /** @todo maybe we could just remove this? */
2857 case VMMDevReq_Idle:
2858 {
2859 /* just return to EMT telling it that we want to halt */
2860 rcRet = VINF_EM_HALT;
2861 break;
2862 }
2863
2864 case VMMDevReq_GuestHeartbeat:
2865 pReqHdr->rc = vmmDevReqHandler_GuestHeartbeat(pThis);
2866 break;
2867
2868 case VMMDevReq_HeartbeatConfigure:
2869 pReqHdr->rc = vmmDevReqHandler_HeartbeatConfigure(pThis, pReqHdr);
2870 break;
2871
2872 case VMMDevReq_NtBugCheck:
2873 pReqHdr->rc = vmmDevReqHandler_NtBugCheck(pThis, pReqHdr);
2874 break;
2875
2876 default:
2877 {
2878 pReqHdr->rc = VERR_NOT_IMPLEMENTED;
2879 Log(("VMMDev unknown request type %d\n", pReqHdr->requestType));
2880 break;
2881 }
2882 }
2883 return rcRet;
2884}
2885
2886
2887/**
2888 * @callback_method_impl{FNIOMIOPORTOUT,
2889 * Port I/O write andler for the generic request interface.}
2890 */
2891static DECLCALLBACK(int) vmmdevRequestHandler(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
2892{
2893 uint64_t tsArrival;
2894 STAM_GET_TS(tsArrival);
2895
2896 RT_NOREF2(Port, cb);
2897 PVMMDEV pThis = (VMMDevState *)pvUser;
2898
2899 /*
2900 * The caller has passed the guest context physical address of the request
2901 * structure. We'll copy all of it into a heap buffer eventually, but we
2902 * will have to start off with the header.
2903 */
2904 VMMDevRequestHeader requestHeader;
2905 RT_ZERO(requestHeader);
2906 PDMDevHlpPhysRead(pDevIns, (RTGCPHYS)u32, &requestHeader, sizeof(requestHeader));
2907
2908 /* The structure size must be greater or equal to the header size. */
2909 if (requestHeader.size < sizeof(VMMDevRequestHeader))
2910 {
2911 Log(("VMMDev request header size too small! size = %d\n", requestHeader.size));
2912 return VINF_SUCCESS;
2913 }
2914
2915 /* Check the version of the header structure. */
2916 if (requestHeader.version != VMMDEV_REQUEST_HEADER_VERSION)
2917 {
2918 Log(("VMMDev: guest header version (0x%08X) differs from ours (0x%08X)\n", requestHeader.version, VMMDEV_REQUEST_HEADER_VERSION));
2919 return VINF_SUCCESS;
2920 }
2921
2922 Log2(("VMMDev request issued: %d\n", requestHeader.requestType));
2923
2924 int rcRet = VINF_SUCCESS;
2925 /* Check that is doesn't exceed the max packet size. */
2926 if (requestHeader.size <= VMMDEV_MAX_VMMDEVREQ_SIZE)
2927 {
2928 /*
2929 * We require the GAs to report it's information before we let it have
2930 * access to all the functions. The VMMDevReq_ReportGuestInfo request
2931 * is the one which unlocks the access. Newer additions will first
2932 * issue VMMDevReq_ReportGuestInfo2, older ones doesn't know this one.
2933 * Two exceptions: VMMDevReq_GetHostVersion and VMMDevReq_WriteCoreDump.
2934 */
2935 if ( pThis->fu32AdditionsOk
2936 || requestHeader.requestType == VMMDevReq_ReportGuestInfo2
2937 || requestHeader.requestType == VMMDevReq_ReportGuestInfo
2938 || requestHeader.requestType == VMMDevReq_WriteCoreDump
2939 || requestHeader.requestType == VMMDevReq_GetHostVersion
2940 )
2941 {
2942 /*
2943 * The request looks fine. Copy it into a buffer.
2944 *
2945 * The buffer is only used while on this thread, and this thread is one
2946 * of the EMTs, so we keep a 4KB buffer for each EMT around to avoid
2947 * wasting time with the heap. Larger allocations goes to the heap, though.
2948 */
2949 VMCPUID iCpu = PDMDevHlpGetCurrentCpuId(pDevIns);
2950 VMMDevRequestHeader *pRequestHeaderFree = NULL;
2951 VMMDevRequestHeader *pRequestHeader = NULL;
2952 if ( requestHeader.size <= _4K
2953 && iCpu < RT_ELEMENTS(pThis->apReqBufs))
2954 {
2955 pRequestHeader = pThis->apReqBufs[iCpu];
2956 if (pRequestHeader)
2957 { /* likely */ }
2958 else
2959 pThis->apReqBufs[iCpu] = pRequestHeader = (VMMDevRequestHeader *)RTMemPageAlloc(_4K);
2960 }
2961 else
2962 {
2963 Assert(iCpu != NIL_VMCPUID);
2964 STAM_REL_COUNTER_INC(&pThis->StatReqBufAllocs);
2965 pRequestHeaderFree = pRequestHeader = (VMMDevRequestHeader *)RTMemAlloc(RT_MAX(requestHeader.size, 512));
2966 }
2967 if (pRequestHeader)
2968 {
2969 memcpy(pRequestHeader, &requestHeader, sizeof(VMMDevRequestHeader));
2970
2971 /* Try lock the request if it's a HGCM call and not crossing a page boundrary.
2972 Saves on PGM interaction. */
2973 VMMDEVREQLOCK Lock = { NULL, { 0, NULL } };
2974 PVMMDEVREQLOCK pLock = NULL;
2975 size_t cbLeft = requestHeader.size - sizeof(VMMDevRequestHeader);
2976 if (cbLeft)
2977 {
2978 if ( ( requestHeader.requestType == VMMDevReq_HGCMCall32
2979 || requestHeader.requestType == VMMDevReq_HGCMCall64)
2980 && ((u32 + requestHeader.size) >> X86_PAGE_SHIFT) == (u32 >> X86_PAGE_SHIFT)
2981 && RT_SUCCESS(PDMDevHlpPhysGCPhys2CCPtr(pDevIns, u32, 0 /*fFlags*/, &Lock.pvReq, &Lock.Lock)) )
2982 {
2983 memcpy((uint8_t *)pRequestHeader + sizeof(VMMDevRequestHeader),
2984 (uint8_t *)Lock.pvReq + sizeof(VMMDevRequestHeader), cbLeft);
2985 pLock = &Lock;
2986 }
2987 else
2988 PDMDevHlpPhysRead(pDevIns,
2989 (RTGCPHYS)u32 + sizeof(VMMDevRequestHeader),
2990 (uint8_t *)pRequestHeader + sizeof(VMMDevRequestHeader),
2991 cbLeft);
2992 }
2993
2994 /*
2995 * Feed buffered request thru the dispatcher.
2996 */
2997 uint32_t fPostOptimize = 0;
2998 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
2999 rcRet = vmmdevReqDispatcher(pThis, pRequestHeader, u32, tsArrival, &fPostOptimize, &pLock);
3000 PDMCritSectLeave(&pThis->CritSect);
3001
3002 /*
3003 * Write the result back to guest memory (unless it is a locked HGCM call).
3004 */
3005 if (!(fPostOptimize & VMMDEVREQDISP_POST_F_NO_WRITE_OUT))
3006 {
3007 if (pLock)
3008 memcpy(pLock->pvReq, pRequestHeader, pRequestHeader->size);
3009 else
3010 PDMDevHlpPhysWrite(pDevIns, u32, pRequestHeader, pRequestHeader->size);
3011 }
3012
3013 if (!pRequestHeaderFree)
3014 { /* likely */ }
3015 else
3016 RTMemFree(pRequestHeaderFree);
3017 return rcRet;
3018 }
3019
3020 Log(("VMMDev: RTMemAlloc failed!\n"));
3021 requestHeader.rc = VERR_NO_MEMORY;
3022 }
3023 else
3024 {
3025 LogRelMax(10, ("VMMDev: Guest has not yet reported to us -- refusing operation of request #%d\n",
3026 requestHeader.requestType));
3027 requestHeader.rc = VERR_NOT_SUPPORTED;
3028 }
3029 }
3030 else
3031 {
3032 LogRelMax(50, ("VMMDev: Request packet too big (%x), refusing operation\n", requestHeader.size));
3033 requestHeader.rc = VERR_NOT_SUPPORTED;
3034 }
3035
3036 /*
3037 * Write the result back to guest memory.
3038 */
3039 PDMDevHlpPhysWrite(pDevIns, u32, &requestHeader, sizeof(requestHeader));
3040
3041 return rcRet;
3042}
3043
3044#endif /* IN_RING3 */
3045
3046
3047/**
3048 * @callback_method_impl{FNIOMIOPORTOUT, Port I/O write handler for requests
3049 * that can be handled w/o going to ring-3.}
3050 */
3051PDMBOTHCBDECL(int) vmmdevFastRequestHandler(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
3052{
3053#ifndef IN_RING3
3054# if 0 /* This functionality is offered through reading the port (vmmdevFastRequestIrqAck). Leaving it here for later. */
3055 PVMMDEV pThis = (VMMDevState *)pvUser;
3056 Assert(PDMINS_2_DATA(pDevIns, PVMMDEV) == pThis);
3057 RT_NOREF2(Port, cb);
3058
3059 /*
3060 * We only process a limited set of requests here, reflecting the rest down
3061 * to ring-3. So, try read the whole request into a stack buffer and check
3062 * if we can handle it.
3063 */
3064 union
3065 {
3066 VMMDevRequestHeader Hdr;
3067 VMMDevEvents Ack;
3068 } uReq;
3069 RT_ZERO(uReq);
3070
3071 VBOXSTRICTRC rcStrict;
3072 if (pThis->fu32AdditionsOk)
3073 {
3074 /* Read it into memory. */
3075 uint32_t cbToRead = sizeof(uReq); /* (Adjust to stay within a page if we support more than ack requests.) */
3076 rcStrict = PDMDevHlpPhysRead(pDevIns, u32, &uReq, cbToRead);
3077 if (rcStrict == VINF_SUCCESS)
3078 {
3079 /*
3080 * Validate the request and check that we want to handle it here.
3081 */
3082 if ( uReq.Hdr.size >= sizeof(uReq.Hdr)
3083 && uReq.Hdr.version == VMMDEV_REQUEST_HEADER_VERSION
3084 && ( uReq.Hdr.requestType == VMMDevReq_AcknowledgeEvents
3085 && uReq.Hdr.size == sizeof(uReq.Ack)
3086 && cbToRead == sizeof(uReq.Ack)
3087 && pThis->CTX_SUFF(pVMMDevRAM) != NULL)
3088 )
3089 {
3090 RT_UNTRUSTED_VALIDATED_FENCE();
3091
3092 /*
3093 * Try grab the critical section.
3094 */
3095 int rc2 = PDMCritSectEnter(&pThis->CritSect, VINF_IOM_R3_IOPORT_WRITE);
3096 if (rc2 == VINF_SUCCESS)
3097 {
3098 /*
3099 * Handle the request and write back the result to the guest.
3100 */
3101 uReq.Hdr.rc = vmmdevReqHandler_AcknowledgeEvents(pThis, &uReq.Hdr);
3102
3103 rcStrict = PDMDevHlpPhysWrite(pDevIns, u32, &uReq, uReq.Hdr.size);
3104 PDMCritSectLeave(&pThis->CritSect);
3105 if (rcStrict == VINF_SUCCESS)
3106 { /* likely */ }
3107 else
3108 Log(("vmmdevFastRequestHandler: PDMDevHlpPhysWrite(%#RX32+rc,4) -> %Rrc (%RTbool)\n",
3109 u32, VBOXSTRICTRC_VAL(rcStrict), PGM_PHYS_RW_IS_SUCCESS(rcStrict) ));
3110 }
3111 else
3112 {
3113 Log(("vmmdevFastRequestHandler: PDMCritSectEnter -> %Rrc\n", rc2));
3114 rcStrict = rc2;
3115 }
3116 }
3117 else
3118 {
3119 Log(("vmmdevFastRequestHandler: size=%#x version=%#x requestType=%d (pVMMDevRAM=%p) -> R3\n",
3120 uReq.Hdr.size, uReq.Hdr.version, uReq.Hdr.requestType, pThis->CTX_SUFF(pVMMDevRAM) ));
3121 rcStrict = VINF_IOM_R3_IOPORT_WRITE;
3122 }
3123 }
3124 else
3125 Log(("vmmdevFastRequestHandler: PDMDevHlpPhysRead(%#RX32,%#RX32) -> %Rrc\n", u32, cbToRead, VBOXSTRICTRC_VAL(rcStrict)));
3126 }
3127 else
3128 {
3129 Log(("vmmdevFastRequestHandler: additions nok-okay\n"));
3130 rcStrict = VINF_IOM_R3_IOPORT_WRITE;
3131 }
3132
3133 return VBOXSTRICTRC_VAL(rcStrict);
3134# else
3135 RT_NOREF(pDevIns, pvUser, Port, u32, cb);
3136 return VINF_IOM_R3_IOPORT_WRITE;
3137# endif
3138
3139#else /* IN_RING3 */
3140 return vmmdevRequestHandler(pDevIns, pvUser, Port, u32, cb);
3141#endif /* IN_RING3 */
3142}
3143
3144
3145/**
3146 * @callback_method_impl{FNIOMIOPORTIN,
3147 * Port I/O read handler for IRQ acknowledging and getting pending events (same
3148 * as VMMDevReq_AcknowledgeEvents - just faster).}
3149 */
3150PDMBOTHCBDECL(int) vmmdevFastRequestIrqAck(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
3151{
3152 PVMMDEV pThis = (VMMDevState *)pvUser;
3153 Assert(PDMINS_2_DATA(pDevIns, PVMMDEV) == pThis);
3154 RT_NOREF(Port);
3155
3156 /* Only 32-bit accesses. */
3157 ASSERT_GUEST_MSG_RETURN(cb == sizeof(uint32_t), ("cb=%d\n", cb), VERR_IOM_IOPORT_UNUSED);
3158
3159 /* The VMMDev memory mapping might've failed, go to ring-3 in that case. */
3160 VBOXSTRICTRC rcStrict;
3161#ifndef IN_RING3
3162 if (pThis->CTX_SUFF(pVMMDevRAM) != NULL)
3163#endif
3164 {
3165 /* Enter critical section and check that the additions has been properly
3166 initialized and that we're not in legacy v1.3 device mode. */
3167 rcStrict = PDMCritSectEnter(&pThis->CritSect, VINF_IOM_R3_IOPORT_READ);
3168 if (rcStrict == VINF_SUCCESS)
3169 {
3170 if ( pThis->fu32AdditionsOk
3171 && !VMMDEV_INTERFACE_VERSION_IS_1_03(pThis))
3172 {
3173 /*
3174 * Do the job.
3175 *
3176 * Note! This code is duplicated in vmmdevReqHandler_AcknowledgeEvents.
3177 */
3178 STAM_REL_COUNTER_INC(&pThis->CTX_SUFF_Z(StatFastIrqAck));
3179
3180 if (pThis->fNewGuestFilterMask)
3181 {
3182 pThis->fNewGuestFilterMask = false;
3183 pThis->u32GuestFilterMask = pThis->u32NewGuestFilterMask;
3184 }
3185
3186 *pu32 = pThis->u32HostEventFlags & pThis->u32GuestFilterMask;
3187
3188 pThis->u32HostEventFlags &= ~pThis->u32GuestFilterMask;
3189 pThis->CTX_SUFF(pVMMDevRAM)->V.V1_04.fHaveEvents = false;
3190
3191 PDMDevHlpPCISetIrqNoWait(pDevIns, 0, 0);
3192 }
3193 else
3194 {
3195 Log(("vmmdevFastRequestIrqAck: fu32AdditionsOk=%d interfaceVersion=%#x\n", pThis->fu32AdditionsOk,
3196 pThis->guestInfo.interfaceVersion));
3197 *pu32 = UINT32_MAX;
3198 }
3199
3200 PDMCritSectLeave(&pThis->CritSect);
3201 }
3202 }
3203#ifndef IN_RING3
3204 else
3205 rcStrict = VINF_IOM_R3_IOPORT_READ;
3206#endif
3207 return VBOXSTRICTRC_VAL(rcStrict);
3208}
3209
3210
3211
3212#ifdef IN_RING3
3213
3214/* -=-=-=-=-=- PCI Device -=-=-=-=-=- */
3215
3216
3217/**
3218 * @callback_method_impl{FNPCIIOREGIONMAP,MMIO/MMIO2 regions}
3219 */
3220static DECLCALLBACK(int) vmmdevIORAMRegionMap(PPDMDEVINS pDevIns, PPDMPCIDEV pPciDev, uint32_t iRegion,
3221 RTGCPHYS GCPhysAddress, RTGCPHYS cb, PCIADDRESSSPACE enmType)
3222{
3223 RT_NOREF1(cb);
3224 LogFlow(("vmmdevR3IORAMRegionMap: iRegion=%d GCPhysAddress=%RGp cb=%RGp enmType=%d\n", iRegion, GCPhysAddress, cb, enmType));
3225 PVMMDEV pThis = RT_FROM_MEMBER(pPciDev, VMMDEV, PciDev);
3226 int rc;
3227
3228 if (iRegion == 1)
3229 {
3230 AssertReturn(enmType == PCI_ADDRESS_SPACE_MEM, VERR_INTERNAL_ERROR);
3231 Assert(pThis->pVMMDevRAMR3 != NULL);
3232 if (GCPhysAddress != NIL_RTGCPHYS)
3233 {
3234 /*
3235 * Map the MMIO2 memory.
3236 */
3237 pThis->GCPhysVMMDevRAM = GCPhysAddress;
3238 Assert(pThis->GCPhysVMMDevRAM == GCPhysAddress);
3239 rc = PDMDevHlpMMIOExMap(pDevIns, pPciDev, iRegion, GCPhysAddress);
3240 }
3241 else
3242 {
3243 /*
3244 * It is about to be unmapped, just clean up.
3245 */
3246 pThis->GCPhysVMMDevRAM = NIL_RTGCPHYS32;
3247 rc = VINF_SUCCESS;
3248 }
3249 }
3250 else if (iRegion == 2)
3251 {
3252 AssertReturn(enmType == PCI_ADDRESS_SPACE_MEM_PREFETCH, VERR_INTERNAL_ERROR);
3253 Assert(pThis->pVMMDevHeapR3 != NULL);
3254 if (GCPhysAddress != NIL_RTGCPHYS)
3255 {
3256 /*
3257 * Map the MMIO2 memory.
3258 */
3259 pThis->GCPhysVMMDevHeap = GCPhysAddress;
3260 Assert(pThis->GCPhysVMMDevHeap == GCPhysAddress);
3261 rc = PDMDevHlpMMIOExMap(pDevIns, pPciDev, iRegion, GCPhysAddress);
3262 if (RT_SUCCESS(rc))
3263 rc = PDMDevHlpRegisterVMMDevHeap(pDevIns, GCPhysAddress, pThis->pVMMDevHeapR3, VMMDEV_HEAP_SIZE);
3264 }
3265 else
3266 {
3267 /*
3268 * It is about to be unmapped, just clean up.
3269 */
3270 PDMDevHlpRegisterVMMDevHeap(pDevIns, NIL_RTGCPHYS, pThis->pVMMDevHeapR3, VMMDEV_HEAP_SIZE);
3271 pThis->GCPhysVMMDevHeap = NIL_RTGCPHYS32;
3272 rc = VINF_SUCCESS;
3273 }
3274 }
3275 else
3276 {
3277 AssertMsgFailed(("%d\n", iRegion));
3278 rc = VERR_INVALID_PARAMETER;
3279 }
3280
3281 return rc;
3282}
3283
3284
3285/**
3286 * @callback_method_impl{FNPCIIOREGIONMAP,I/O Port Region}
3287 */
3288static DECLCALLBACK(int) vmmdevIOPortRegionMap(PPDMDEVINS pDevIns, PPDMPCIDEV pPciDev, uint32_t iRegion,
3289 RTGCPHYS GCPhysAddress, RTGCPHYS cb, PCIADDRESSSPACE enmType)
3290{
3291 LogFlow(("vmmdevIOPortRegionMap: iRegion=%d GCPhysAddress=%RGp cb=%RGp enmType=%d\n", iRegion, GCPhysAddress, cb, enmType));
3292 RT_NOREF3(iRegion, cb, enmType);
3293 PVMMDEV pThis = RT_FROM_MEMBER(pPciDev, VMMDEV, PciDev);
3294
3295 Assert(enmType == PCI_ADDRESS_SPACE_IO);
3296 Assert(iRegion == 0);
3297 AssertMsg(RT_ALIGN(GCPhysAddress, 8) == GCPhysAddress, ("Expected 8 byte alignment. GCPhysAddress=%#x\n", GCPhysAddress));
3298
3299 /*
3300 * Register our port IO handlers.
3301 */
3302 int rc = PDMDevHlpIOPortRegister(pDevIns, (RTIOPORT)GCPhysAddress + VMMDEV_PORT_OFF_REQUEST, 1,
3303 pThis, vmmdevRequestHandler, NULL, NULL, NULL, "VMMDev Request Handler");
3304 AssertLogRelRCReturn(rc, rc);
3305
3306 /* The fast one: */
3307 rc = PDMDevHlpIOPortRegister(pDevIns, (RTIOPORT)GCPhysAddress + VMMDEV_PORT_OFF_REQUEST_FAST, 1,
3308 pThis, vmmdevFastRequestHandler, vmmdevFastRequestIrqAck, NULL, NULL, "VMMDev Fast R0/RC Requests");
3309 AssertLogRelRCReturn(rc, rc);
3310 if (pThis->fRZEnabled)
3311 {
3312 rc = PDMDevHlpIOPortRegisterR0(pDevIns, (RTIOPORT)GCPhysAddress + VMMDEV_PORT_OFF_REQUEST_FAST, 1,
3313 PDMINS_2_DATA_R0PTR(pDevIns), "vmmdevFastRequestHandler", "vmmdevFastRequestIrqAck",
3314 NULL, NULL, "VMMDev Fast R0/RC Requests");
3315 AssertLogRelRCReturn(rc, rc);
3316 rc = PDMDevHlpIOPortRegisterRC(pDevIns, (RTIOPORT)GCPhysAddress + VMMDEV_PORT_OFF_REQUEST_FAST, 1,
3317 PDMINS_2_DATA_RCPTR(pDevIns), "vmmdevFastRequestHandler", "vmmdevFastRequestIrqAck",
3318 NULL, NULL, "VMMDev Fast R0/RC Requests");
3319 AssertLogRelRCReturn(rc, rc);
3320 }
3321
3322 return rc;
3323}
3324
3325
3326
3327/* -=-=-=-=-=- Backdoor Logging and Time Sync. -=-=-=-=-=- */
3328
3329/**
3330 * @callback_method_impl{FNIOMIOPORTOUT, Backdoor Logging.}
3331 */
3332static DECLCALLBACK(int) vmmdevBackdoorLog(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
3333{
3334 RT_NOREF1(pvUser);
3335 PVMMDEV pThis = PDMINS_2_DATA(pDevIns, VMMDevState *);
3336
3337 if (!pThis->fBackdoorLogDisabled && cb == 1 && Port == RTLOG_DEBUG_PORT)
3338 {
3339
3340 /* The raw version. */
3341 switch (u32)
3342 {
3343 case '\r': LogIt(RTLOGGRPFLAGS_LEVEL_2, LOG_GROUP_DEV_VMM_BACKDOOR, ("vmmdev: <return>\n")); break;
3344 case '\n': LogIt(RTLOGGRPFLAGS_LEVEL_2, LOG_GROUP_DEV_VMM_BACKDOOR, ("vmmdev: <newline>\n")); break;
3345 case '\t': LogIt(RTLOGGRPFLAGS_LEVEL_2, LOG_GROUP_DEV_VMM_BACKDOOR, ("vmmdev: <tab>\n")); break;
3346 default: LogIt(RTLOGGRPFLAGS_LEVEL_2, LOG_GROUP_DEV_VMM_BACKDOOR, ("vmmdev: %c (%02x)\n", u32, u32)); break;
3347 }
3348
3349 /* The readable, buffered version. */
3350 if (u32 == '\n' || u32 == '\r')
3351 {
3352 pThis->szMsg[pThis->iMsg] = '\0';
3353 if (pThis->iMsg)
3354 LogRelIt(RTLOGGRPFLAGS_LEVEL_1, LOG_GROUP_DEV_VMM_BACKDOOR, ("VMMDev: Guest Log: %s\n", pThis->szMsg));
3355 pThis->iMsg = 0;
3356 }
3357 else
3358 {
3359 if (pThis->iMsg >= sizeof(pThis->szMsg)-1)
3360 {
3361 pThis->szMsg[pThis->iMsg] = '\0';
3362 LogRelIt(RTLOGGRPFLAGS_LEVEL_1, LOG_GROUP_DEV_VMM_BACKDOOR, ("VMMDev: Guest Log: %s\n", pThis->szMsg));
3363 pThis->iMsg = 0;
3364 }
3365 pThis->szMsg[pThis->iMsg] = (char )u32;
3366 pThis->szMsg[++pThis->iMsg] = '\0';
3367 }
3368 }
3369 return VINF_SUCCESS;
3370}
3371
3372#ifdef VMMDEV_WITH_ALT_TIMESYNC
3373
3374/**
3375 * @callback_method_impl{FNIOMIOPORTOUT, Alternative time synchronization.}
3376 */
3377static DECLCALLBACK(int) vmmdevAltTimeSyncWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
3378{
3379 RT_NOREF2(pvUser, Port);
3380 PVMMDEV pThis = PDMINS_2_DATA(pDevIns, VMMDevState *);
3381 if (cb == 4)
3382 {
3383 /* Selects high (0) or low (1) DWORD. The high has to be read first. */
3384 switch (u32)
3385 {
3386 case 0:
3387 pThis->fTimesyncBackdoorLo = false;
3388 break;
3389 case 1:
3390 pThis->fTimesyncBackdoorLo = true;
3391 break;
3392 default:
3393 Log(("vmmdevAltTimeSyncWrite: Invalid access cb=%#x u32=%#x\n", cb, u32));
3394 break;
3395 }
3396 }
3397 else
3398 Log(("vmmdevAltTimeSyncWrite: Invalid access cb=%#x u32=%#x\n", cb, u32));
3399 return VINF_SUCCESS;
3400}
3401
3402/**
3403 * @callback_method_impl{FNIOMIOPORTOUT, Alternative time synchronization.}
3404 */
3405static DECLCALLBACK(int) vmmdevAltTimeSyncRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
3406{
3407 RT_NOREF2(pvUser, Port);
3408 PVMMDEV pThis = PDMINS_2_DATA(pDevIns, VMMDevState *);
3409 int rc;
3410 if (cb == 4)
3411 {
3412 if (pThis->fTimesyncBackdoorLo)
3413 *pu32 = (uint32_t)pThis->hostTime;
3414 else
3415 {
3416 /* Reading the high dword gets and saves the current time. */
3417 RTTIMESPEC Now;
3418 pThis->hostTime = RTTimeSpecGetMilli(PDMDevHlpTMUtcNow(pDevIns, &Now));
3419 *pu32 = (uint32_t)(pThis->hostTime >> 32);
3420 }
3421 rc = VINF_SUCCESS;
3422 }
3423 else
3424 {
3425 Log(("vmmdevAltTimeSyncRead: Invalid access cb=%#x\n", cb));
3426 rc = VERR_IOM_IOPORT_UNUSED;
3427 }
3428 return rc;
3429}
3430
3431#endif /* VMMDEV_WITH_ALT_TIMESYNC */
3432
3433
3434/* -=-=-=-=-=- IBase -=-=-=-=-=- */
3435
3436/**
3437 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
3438 */
3439static DECLCALLBACK(void *) vmmdevPortQueryInterface(PPDMIBASE pInterface, const char *pszIID)
3440{
3441 PVMMDEV pThis = RT_FROM_MEMBER(pInterface, VMMDEV, IBase);
3442
3443 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThis->IBase);
3444 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIVMMDEVPORT, &pThis->IPort);
3445#ifdef VBOX_WITH_HGCM
3446 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIHGCMPORT, &pThis->IHGCMPort);
3447#endif
3448 /* Currently only for shared folders. */
3449 PDMIBASE_RETURN_INTERFACE(pszIID, PDMILEDPORTS, &pThis->SharedFolders.ILeds);
3450 return NULL;
3451}
3452
3453
3454/* -=-=-=-=-=- ILeds -=-=-=-=-=- */
3455
3456/**
3457 * Gets the pointer to the status LED of a unit.
3458 *
3459 * @returns VBox status code.
3460 * @param pInterface Pointer to the interface structure containing the called function pointer.
3461 * @param iLUN The unit which status LED we desire.
3462 * @param ppLed Where to store the LED pointer.
3463 */
3464static DECLCALLBACK(int) vmmdevQueryStatusLed(PPDMILEDPORTS pInterface, unsigned iLUN, PPDMLED *ppLed)
3465{
3466 PVMMDEV pThis = RT_FROM_MEMBER(pInterface, VMMDEV, SharedFolders.ILeds);
3467 if (iLUN == 0) /* LUN 0 is shared folders */
3468 {
3469 *ppLed = &pThis->SharedFolders.Led;
3470 return VINF_SUCCESS;
3471 }
3472 return VERR_PDM_LUN_NOT_FOUND;
3473}
3474
3475
3476/* -=-=-=-=-=- PDMIVMMDEVPORT (VMMDEV::IPort) -=-=-=-=-=- */
3477
3478/**
3479 * @interface_method_impl{PDMIVMMDEVPORT,pfnQueryAbsoluteMouse}
3480 */
3481static DECLCALLBACK(int) vmmdevIPort_QueryAbsoluteMouse(PPDMIVMMDEVPORT pInterface, int32_t *pxAbs, int32_t *pyAbs)
3482{
3483 PVMMDEV pThis = RT_FROM_MEMBER(pInterface, VMMDEV, IPort);
3484
3485 /** @todo at the first sign of trouble in this area, just enter the critsect.
3486 * As indicated by the comment below, the atomic reads serves no real purpose
3487 * here since we can assume cache coherency protocoles and int32_t alignment
3488 * rules making sure we won't see a halfwritten value. */
3489 if (pxAbs)
3490 *pxAbs = ASMAtomicReadS32(&pThis->mouseXAbs); /* why the atomic read? */
3491 if (pyAbs)
3492 *pyAbs = ASMAtomicReadS32(&pThis->mouseYAbs);
3493
3494 return VINF_SUCCESS;
3495}
3496
3497/**
3498 * @interface_method_impl{PDMIVMMDEVPORT,pfnSetAbsoluteMouse}
3499 */
3500static DECLCALLBACK(int) vmmdevIPort_SetAbsoluteMouse(PPDMIVMMDEVPORT pInterface, int32_t xAbs, int32_t yAbs)
3501{
3502 PVMMDEV pThis = RT_FROM_MEMBER(pInterface, VMMDEV, IPort);
3503 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
3504
3505 if ( pThis->mouseXAbs != xAbs
3506 || pThis->mouseYAbs != yAbs)
3507 {
3508 Log2(("vmmdevIPort_SetAbsoluteMouse : settings absolute position to x = %d, y = %d\n", xAbs, yAbs));
3509 pThis->mouseXAbs = xAbs;
3510 pThis->mouseYAbs = yAbs;
3511 VMMDevNotifyGuest(pThis, VMMDEV_EVENT_MOUSE_POSITION_CHANGED);
3512 }
3513
3514 PDMCritSectLeave(&pThis->CritSect);
3515 return VINF_SUCCESS;
3516}
3517
3518/**
3519 * @interface_method_impl{PDMIVMMDEVPORT,pfnQueryMouseCapabilities}
3520 */
3521static DECLCALLBACK(int) vmmdevIPort_QueryMouseCapabilities(PPDMIVMMDEVPORT pInterface, uint32_t *pfCapabilities)
3522{
3523 PVMMDEV pThis = RT_FROM_MEMBER(pInterface, VMMDEV, IPort);
3524 AssertPtrReturn(pfCapabilities, VERR_INVALID_PARAMETER);
3525
3526 *pfCapabilities = pThis->mouseCapabilities;
3527 return VINF_SUCCESS;
3528}
3529
3530/**
3531 * @interface_method_impl{PDMIVMMDEVPORT,pfnUpdateMouseCapabilities}
3532 */
3533static DECLCALLBACK(int)
3534vmmdevIPort_UpdateMouseCapabilities(PPDMIVMMDEVPORT pInterface, uint32_t fCapsAdded, uint32_t fCapsRemoved)
3535{
3536 PVMMDEV pThis = RT_FROM_MEMBER(pInterface, VMMDEV, IPort);
3537 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
3538
3539 uint32_t fOldCaps = pThis->mouseCapabilities;
3540 pThis->mouseCapabilities &= ~(fCapsRemoved & VMMDEV_MOUSE_HOST_MASK);
3541 pThis->mouseCapabilities |= (fCapsAdded & VMMDEV_MOUSE_HOST_MASK)
3542 | VMMDEV_MOUSE_HOST_RECHECKS_NEEDS_HOST_CURSOR;
3543 bool fNotify = fOldCaps != pThis->mouseCapabilities;
3544
3545 LogRelFlow(("VMMDev: vmmdevIPort_UpdateMouseCapabilities: fCapsAdded=0x%x, fCapsRemoved=0x%x, fNotify=%RTbool\n", fCapsAdded,
3546 fCapsRemoved, fNotify));
3547
3548 if (fNotify)
3549 VMMDevNotifyGuest(pThis, VMMDEV_EVENT_MOUSE_CAPABILITIES_CHANGED);
3550
3551 PDMCritSectLeave(&pThis->CritSect);
3552 return VINF_SUCCESS;
3553}
3554
3555static bool vmmdevIsMonitorDefEqual(VMMDevDisplayDef const *pNew, VMMDevDisplayDef const *pOld)
3556{
3557 bool fEqual = pNew->idDisplay == pOld->idDisplay;
3558
3559 fEqual = fEqual && ( !RT_BOOL(pNew->fDisplayFlags & VMMDEV_DISPLAY_ORIGIN) /* No change. */
3560 || ( RT_BOOL(pOld->fDisplayFlags & VMMDEV_DISPLAY_ORIGIN) /* Old value exists and */
3561 && pNew->xOrigin == pOld->xOrigin /* the old is equal to the new. */
3562 && pNew->yOrigin == pOld->yOrigin));
3563
3564 fEqual = fEqual && ( !RT_BOOL(pNew->fDisplayFlags & VMMDEV_DISPLAY_CX)
3565 || ( RT_BOOL(pOld->fDisplayFlags & VMMDEV_DISPLAY_CX)
3566 && pNew->cx == pOld->cx));
3567
3568 fEqual = fEqual && ( !RT_BOOL(pNew->fDisplayFlags & VMMDEV_DISPLAY_CY)
3569 || ( RT_BOOL(pOld->fDisplayFlags & VMMDEV_DISPLAY_CY)
3570 && pNew->cy == pOld->cy));
3571
3572 fEqual = fEqual && ( !RT_BOOL(pNew->fDisplayFlags & VMMDEV_DISPLAY_BPP)
3573 || ( RT_BOOL(pOld->fDisplayFlags & VMMDEV_DISPLAY_BPP)
3574 && pNew->cBitsPerPixel == pOld->cBitsPerPixel));
3575
3576 fEqual = fEqual && ( RT_BOOL(pNew->fDisplayFlags & VMMDEV_DISPLAY_DISABLED)
3577 == RT_BOOL(pOld->fDisplayFlags & VMMDEV_DISPLAY_DISABLED));
3578
3579 fEqual = fEqual && ( RT_BOOL(pNew->fDisplayFlags & VMMDEV_DISPLAY_PRIMARY)
3580 == RT_BOOL(pOld->fDisplayFlags & VMMDEV_DISPLAY_PRIMARY));
3581
3582 return fEqual;
3583}
3584
3585/**
3586 * @interface_method_impl{PDMIVMMDEVPORT,pfnRequestDisplayChange}
3587 */
3588static DECLCALLBACK(int)
3589vmmdevIPort_RequestDisplayChange(PPDMIVMMDEVPORT pInterface, uint32_t cDisplays, VMMDevDisplayDef const *paDisplays, bool fForce)
3590{
3591 int rc = VINF_SUCCESS;
3592
3593 PVMMDEV pThis = RT_FROM_MEMBER(pInterface, VMMDEV, IPort);
3594 bool fNotifyGuest = false;
3595
3596 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
3597
3598 uint32_t i;
3599 for (i = 0; i < cDisplays; ++i)
3600 {
3601 VMMDevDisplayDef const *p = &paDisplays[i];
3602
3603 /* Either one display definition is provided or the display id must be equal to the array index. */
3604 AssertBreakStmt(cDisplays == 1 || p->idDisplay == i, rc = VERR_INVALID_PARAMETER);
3605 AssertBreakStmt(p->idDisplay < RT_ELEMENTS(pThis->displayChangeData.aRequests), rc = VERR_INVALID_PARAMETER);
3606
3607 DISPLAYCHANGEREQUEST *pRequest = &pThis->displayChangeData.aRequests[p->idDisplay];
3608
3609 VMMDevDisplayDef const *pLastRead = &pRequest->lastReadDisplayChangeRequest;
3610
3611 /* Verify that the new resolution is different and that guest does not yet know about it. */
3612 bool const fDifferentResolution = fForce || !vmmdevIsMonitorDefEqual(p, pLastRead);
3613
3614 LogFunc(("same=%d. New: %dx%d, cBits=%d, id=%d. Old: %dx%d, cBits=%d, id=%d. @%d,%d, Enabled=%d, ChangeOrigin=%d\n",
3615 !fDifferentResolution, p->cx, p->cy, p->cBitsPerPixel, p->idDisplay,
3616 pLastRead->cx, pLastRead->cy, pLastRead->cBitsPerPixel, pLastRead->idDisplay,
3617 p->xOrigin, p->yOrigin,
3618 !RT_BOOL(p->fDisplayFlags & VMMDEV_DISPLAY_DISABLED),
3619 RT_BOOL(p->fDisplayFlags & VMMDEV_DISPLAY_ORIGIN)));
3620
3621 /* We could validate the information here but hey, the guest can do that as well! */
3622 pRequest->displayChangeRequest = *p;
3623 pRequest->fPending = fDifferentResolution;
3624
3625 fNotifyGuest = fNotifyGuest || fDifferentResolution;
3626 }
3627
3628 if (RT_SUCCESS(rc))
3629 {
3630 if (fNotifyGuest)
3631 {
3632 for (i = 0; i < RT_ELEMENTS(pThis->displayChangeData.aRequests); ++i)
3633 {
3634 DISPLAYCHANGEREQUEST *pRequest = &pThis->displayChangeData.aRequests[i];
3635 if (pRequest->fPending)
3636 {
3637 VMMDevDisplayDef const *p = &pRequest->displayChangeRequest;
3638 LogRel(("VMMDev: SetVideoModeHint: Got a video mode hint (%dx%dx%d)@(%dx%d),(%d;%d) at %d\n",
3639 p->cx, p->cy, p->cBitsPerPixel, p->xOrigin, p->yOrigin,
3640 !RT_BOOL(p->fDisplayFlags & VMMDEV_DISPLAY_DISABLED),
3641 RT_BOOL(p->fDisplayFlags & VMMDEV_DISPLAY_ORIGIN), i));
3642 }
3643 }
3644
3645 /* IRQ so the guest knows what's going on */
3646 VMMDevNotifyGuest(pThis, VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST);
3647 }
3648 }
3649
3650 PDMCritSectLeave(&pThis->CritSect);
3651 return rc;
3652}
3653
3654/**
3655 * @interface_method_impl{PDMIVMMDEVPORT,pfnRequestSeamlessChange}
3656 */
3657static DECLCALLBACK(int) vmmdevIPort_RequestSeamlessChange(PPDMIVMMDEVPORT pInterface, bool fEnabled)
3658{
3659 PVMMDEV pThis = RT_FROM_MEMBER(pInterface, VMMDEV, IPort);
3660 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
3661
3662 /* Verify that the new resolution is different and that guest does not yet know about it. */
3663 bool fSameMode = (pThis->fLastSeamlessEnabled == fEnabled);
3664
3665 Log(("vmmdevIPort_RequestSeamlessChange: same=%d. new=%d\n", fSameMode, fEnabled));
3666
3667 if (!fSameMode)
3668 {
3669 /* we could validate the information here but hey, the guest can do that as well! */
3670 pThis->fSeamlessEnabled = fEnabled;
3671
3672 /* IRQ so the guest knows what's going on */
3673 VMMDevNotifyGuest(pThis, VMMDEV_EVENT_SEAMLESS_MODE_CHANGE_REQUEST);
3674 }
3675
3676 PDMCritSectLeave(&pThis->CritSect);
3677 return VINF_SUCCESS;
3678}
3679
3680/**
3681 * @interface_method_impl{PDMIVMMDEVPORT,pfnSetMemoryBalloon}
3682 */
3683static DECLCALLBACK(int) vmmdevIPort_SetMemoryBalloon(PPDMIVMMDEVPORT pInterface, uint32_t cMbBalloon)
3684{
3685 PVMMDEV pThis = RT_FROM_MEMBER(pInterface, VMMDEV, IPort);
3686 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
3687
3688 /* Verify that the new resolution is different and that guest does not yet know about it. */
3689 Log(("vmmdevIPort_SetMemoryBalloon: old=%u new=%u\n", pThis->cMbMemoryBalloonLast, cMbBalloon));
3690 if (pThis->cMbMemoryBalloonLast != cMbBalloon)
3691 {
3692 /* we could validate the information here but hey, the guest can do that as well! */
3693 pThis->cMbMemoryBalloon = cMbBalloon;
3694
3695 /* IRQ so the guest knows what's going on */
3696 VMMDevNotifyGuest(pThis, VMMDEV_EVENT_BALLOON_CHANGE_REQUEST);
3697 }
3698
3699 PDMCritSectLeave(&pThis->CritSect);
3700 return VINF_SUCCESS;
3701}
3702
3703/**
3704 * @interface_method_impl{PDMIVMMDEVPORT,pfnVRDPChange}
3705 */
3706static DECLCALLBACK(int) vmmdevIPort_VRDPChange(PPDMIVMMDEVPORT pInterface, bool fVRDPEnabled, uint32_t uVRDPExperienceLevel)
3707{
3708 PVMMDEV pThis = RT_FROM_MEMBER(pInterface, VMMDEV, IPort);
3709 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
3710
3711 bool fSame = (pThis->fVRDPEnabled == fVRDPEnabled);
3712
3713 Log(("vmmdevIPort_VRDPChange: old=%d. new=%d\n", pThis->fVRDPEnabled, fVRDPEnabled));
3714
3715 if (!fSame)
3716 {
3717 pThis->fVRDPEnabled = fVRDPEnabled;
3718 pThis->uVRDPExperienceLevel = uVRDPExperienceLevel;
3719
3720 VMMDevNotifyGuest(pThis, VMMDEV_EVENT_VRDP);
3721 }
3722
3723 PDMCritSectLeave(&pThis->CritSect);
3724 return VINF_SUCCESS;
3725}
3726
3727/**
3728 * @interface_method_impl{PDMIVMMDEVPORT,pfnSetStatisticsInterval}
3729 */
3730static DECLCALLBACK(int) vmmdevIPort_SetStatisticsInterval(PPDMIVMMDEVPORT pInterface, uint32_t cSecsStatInterval)
3731{
3732 PVMMDEV pThis = RT_FROM_MEMBER(pInterface, VMMDEV, IPort);
3733 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
3734
3735 /* Verify that the new resolution is different and that guest does not yet know about it. */
3736 bool fSame = (pThis->u32LastStatIntervalSize == cSecsStatInterval);
3737
3738 Log(("vmmdevIPort_SetStatisticsInterval: old=%d. new=%d\n", pThis->u32LastStatIntervalSize, cSecsStatInterval));
3739
3740 if (!fSame)
3741 {
3742 /* we could validate the information here but hey, the guest can do that as well! */
3743 pThis->u32StatIntervalSize = cSecsStatInterval;
3744
3745 /* IRQ so the guest knows what's going on */
3746 VMMDevNotifyGuest(pThis, VMMDEV_EVENT_STATISTICS_INTERVAL_CHANGE_REQUEST);
3747 }
3748
3749 PDMCritSectLeave(&pThis->CritSect);
3750 return VINF_SUCCESS;
3751}
3752
3753/**
3754 * @interface_method_impl{PDMIVMMDEVPORT,pfnSetCredentials}
3755 */
3756static DECLCALLBACK(int) vmmdevIPort_SetCredentials(PPDMIVMMDEVPORT pInterface, const char *pszUsername,
3757 const char *pszPassword, const char *pszDomain, uint32_t fFlags)
3758{
3759 PVMMDEV pThis = RT_FROM_MEMBER(pInterface, VMMDEV, IPort);
3760 AssertReturn(fFlags & (VMMDEV_SETCREDENTIALS_GUESTLOGON | VMMDEV_SETCREDENTIALS_JUDGE), VERR_INVALID_PARAMETER);
3761 size_t const cchUsername = strlen(pszUsername);
3762 AssertReturn(cchUsername < VMMDEV_CREDENTIALS_SZ_SIZE, VERR_BUFFER_OVERFLOW);
3763 size_t const cchPassword = strlen(pszPassword);
3764 AssertReturn(cchPassword < VMMDEV_CREDENTIALS_SZ_SIZE, VERR_BUFFER_OVERFLOW);
3765 size_t const cchDomain = strlen(pszDomain);
3766 AssertReturn(cchDomain < VMMDEV_CREDENTIALS_SZ_SIZE, VERR_BUFFER_OVERFLOW);
3767
3768 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
3769
3770 /*
3771 * Logon mode
3772 */
3773 if (fFlags & VMMDEV_SETCREDENTIALS_GUESTLOGON)
3774 {
3775 /* memorize the data */
3776 memcpy(pThis->pCredentials->Logon.szUserName, pszUsername, cchUsername);
3777 pThis->pCredentials->Logon.szUserName[cchUsername] = '\0';
3778 memcpy(pThis->pCredentials->Logon.szPassword, pszPassword, cchPassword);
3779 pThis->pCredentials->Logon.szPassword[cchPassword] = '\0';
3780 memcpy(pThis->pCredentials->Logon.szDomain, pszDomain, cchDomain);
3781 pThis->pCredentials->Logon.szDomain[cchDomain] = '\0';
3782 pThis->pCredentials->Logon.fAllowInteractiveLogon = !(fFlags & VMMDEV_SETCREDENTIALS_NOLOCALLOGON);
3783 }
3784 /*
3785 * Credentials verification mode?
3786 */
3787 else
3788 {
3789 /* memorize the data */
3790 memcpy(pThis->pCredentials->Judge.szUserName, pszUsername, cchUsername);
3791 pThis->pCredentials->Judge.szUserName[cchUsername] = '\0';
3792 memcpy(pThis->pCredentials->Judge.szPassword, pszPassword, cchPassword);
3793 pThis->pCredentials->Judge.szPassword[cchPassword] = '\0';
3794 memcpy(pThis->pCredentials->Judge.szDomain, pszDomain, cchDomain);
3795 pThis->pCredentials->Judge.szDomain[cchDomain] = '\0';
3796
3797 VMMDevNotifyGuest(pThis, VMMDEV_EVENT_JUDGE_CREDENTIALS);
3798 }
3799
3800 PDMCritSectLeave(&pThis->CritSect);
3801 return VINF_SUCCESS;
3802}
3803
3804/**
3805 * @interface_method_impl{PDMIVMMDEVPORT,pfnVBVAChange}
3806 *
3807 * Notification from the Display. Especially useful when acceleration is
3808 * disabled after a video mode change.
3809 */
3810static DECLCALLBACK(void) vmmdevIPort_VBVAChange(PPDMIVMMDEVPORT pInterface, bool fEnabled)
3811{
3812 PVMMDEV pThis = RT_FROM_MEMBER(pInterface, VMMDEV, IPort);
3813 Log(("vmmdevIPort_VBVAChange: fEnabled = %d\n", fEnabled));
3814
3815 /* Only used by saved state, which I guess is why we don't bother with locking here. */
3816 pThis->u32VideoAccelEnabled = fEnabled;
3817}
3818
3819/**
3820 * @interface_method_impl{PDMIVMMDEVPORT,pfnCpuHotUnplug}
3821 */
3822static DECLCALLBACK(int) vmmdevIPort_CpuHotUnplug(PPDMIVMMDEVPORT pInterface, uint32_t idCpuCore, uint32_t idCpuPackage)
3823{
3824 PVMMDEV pThis = RT_FROM_MEMBER(pInterface, VMMDEV, IPort);
3825 int rc = VINF_SUCCESS;
3826
3827 Log(("vmmdevIPort_CpuHotUnplug: idCpuCore=%u idCpuPackage=%u\n", idCpuCore, idCpuPackage));
3828
3829 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
3830
3831 if (pThis->fCpuHotPlugEventsEnabled)
3832 {
3833 pThis->enmCpuHotPlugEvent = VMMDevCpuEventType_Unplug;
3834 pThis->idCpuCore = idCpuCore;
3835 pThis->idCpuPackage = idCpuPackage;
3836 VMMDevNotifyGuest(pThis, VMMDEV_EVENT_CPU_HOTPLUG);
3837 }
3838 else
3839 rc = VERR_VMMDEV_CPU_HOTPLUG_NOT_MONITORED_BY_GUEST;
3840
3841 PDMCritSectLeave(&pThis->CritSect);
3842 return rc;
3843}
3844
3845/**
3846 * @interface_method_impl{PDMIVMMDEVPORT,pfnCpuHotPlug}
3847 */
3848static DECLCALLBACK(int) vmmdevIPort_CpuHotPlug(PPDMIVMMDEVPORT pInterface, uint32_t idCpuCore, uint32_t idCpuPackage)
3849{
3850 PVMMDEV pThis = RT_FROM_MEMBER(pInterface, VMMDEV, IPort);
3851 int rc = VINF_SUCCESS;
3852
3853 Log(("vmmdevCpuPlug: idCpuCore=%u idCpuPackage=%u\n", idCpuCore, idCpuPackage));
3854
3855 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
3856
3857 if (pThis->fCpuHotPlugEventsEnabled)
3858 {
3859 pThis->enmCpuHotPlugEvent = VMMDevCpuEventType_Plug;
3860 pThis->idCpuCore = idCpuCore;
3861 pThis->idCpuPackage = idCpuPackage;
3862 VMMDevNotifyGuest(pThis, VMMDEV_EVENT_CPU_HOTPLUG);
3863 }
3864 else
3865 rc = VERR_VMMDEV_CPU_HOTPLUG_NOT_MONITORED_BY_GUEST;
3866
3867 PDMCritSectLeave(&pThis->CritSect);
3868 return rc;
3869}
3870
3871
3872/* -=-=-=-=-=- Saved State -=-=-=-=-=- */
3873
3874/**
3875 * @callback_method_impl{FNSSMDEVLIVEEXEC}
3876 */
3877static DECLCALLBACK(int) vmmdevLiveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uPass)
3878{
3879 RT_NOREF1(uPass);
3880 PVMMDEV pThis = PDMINS_2_DATA(pDevIns, PVMMDEV);
3881
3882 SSMR3PutBool(pSSM, pThis->fGetHostTimeDisabled);
3883 SSMR3PutBool(pSSM, pThis->fBackdoorLogDisabled);
3884 SSMR3PutBool(pSSM, pThis->fKeepCredentials);
3885 SSMR3PutBool(pSSM, pThis->fHeapEnabled);
3886
3887 return VINF_SSM_DONT_CALL_AGAIN;
3888}
3889
3890
3891/**
3892 * @callback_method_impl{FNSSMDEVSAVEEXEC}
3893 */
3894static DECLCALLBACK(int) vmmdevSaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
3895{
3896 PVMMDEV pThis = PDMINS_2_DATA(pDevIns, PVMMDEV);
3897 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
3898
3899 vmmdevLiveExec(pDevIns, pSSM, SSM_PASS_FINAL);
3900
3901 SSMR3PutU32(pSSM, pThis->hypervisorSize);
3902 SSMR3PutU32(pSSM, pThis->mouseCapabilities);
3903 SSMR3PutS32(pSSM, pThis->mouseXAbs);
3904 SSMR3PutS32(pSSM, pThis->mouseYAbs);
3905
3906 SSMR3PutBool(pSSM, pThis->fNewGuestFilterMask);
3907 SSMR3PutU32(pSSM, pThis->u32NewGuestFilterMask);
3908 SSMR3PutU32(pSSM, pThis->u32GuestFilterMask);
3909 SSMR3PutU32(pSSM, pThis->u32HostEventFlags);
3910 /* The following is not strictly necessary as PGM restores MMIO2, keeping it for historical reasons. */
3911 SSMR3PutMem(pSSM, &pThis->pVMMDevRAMR3->V, sizeof(pThis->pVMMDevRAMR3->V));
3912
3913 SSMR3PutMem(pSSM, &pThis->guestInfo, sizeof(pThis->guestInfo));
3914 SSMR3PutU32(pSSM, pThis->fu32AdditionsOk);
3915 SSMR3PutU32(pSSM, pThis->u32VideoAccelEnabled);
3916 SSMR3PutBool(pSSM, pThis->displayChangeData.fGuestSentChangeEventAck);
3917
3918 SSMR3PutU32(pSSM, pThis->guestCaps);
3919
3920#ifdef VBOX_WITH_HGCM
3921 vmmdevHGCMSaveState(pThis, pSSM);
3922#endif /* VBOX_WITH_HGCM */
3923
3924 SSMR3PutU32(pSSM, pThis->fHostCursorRequested);
3925
3926 SSMR3PutU32(pSSM, pThis->guestInfo2.uFullVersion);
3927 SSMR3PutU32(pSSM, pThis->guestInfo2.uRevision);
3928 SSMR3PutU32(pSSM, pThis->guestInfo2.fFeatures);
3929 SSMR3PutStrZ(pSSM, pThis->guestInfo2.szName);
3930 SSMR3PutU32(pSSM, pThis->cFacilityStatuses);
3931 for (uint32_t i = 0; i < pThis->cFacilityStatuses; i++)
3932 {
3933 SSMR3PutU32(pSSM, pThis->aFacilityStatuses[i].enmFacility);
3934 SSMR3PutU32(pSSM, pThis->aFacilityStatuses[i].fFlags);
3935 SSMR3PutU16(pSSM, (uint16_t)pThis->aFacilityStatuses[i].enmStatus);
3936 SSMR3PutS64(pSSM, RTTimeSpecGetNano(&pThis->aFacilityStatuses[i].TimeSpecTS));
3937 }
3938
3939 /* Heartbeat: */
3940 SSMR3PutBool(pSSM, pThis->fHeartbeatActive);
3941 SSMR3PutBool(pSSM, pThis->fFlatlined);
3942 SSMR3PutU64(pSSM, pThis->nsLastHeartbeatTS);
3943 TMR3TimerSave(pThis->pFlatlinedTimer, pSSM);
3944
3945 PDMCritSectLeave(&pThis->CritSect);
3946 return VINF_SUCCESS;
3947}
3948
3949/**
3950 * @callback_method_impl{FNSSMDEVLOADEXEC}
3951 */
3952static DECLCALLBACK(int) vmmdevLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
3953{
3954 /** @todo The code load code is assuming we're always loaded into a freshly
3955 * constructed VM. */
3956 PVMMDEV pThis = PDMINS_2_DATA(pDevIns, PVMMDEV);
3957 int rc;
3958
3959 if ( uVersion > VMMDEV_SAVED_STATE_VERSION
3960 || uVersion < 6)
3961 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
3962
3963 /* config */
3964 if (uVersion > VMMDEV_SAVED_STATE_VERSION_VBOX_30)
3965 {
3966 bool f;
3967 rc = SSMR3GetBool(pSSM, &f); AssertRCReturn(rc, rc);
3968 if (pThis->fGetHostTimeDisabled != f)
3969 LogRel(("VMMDev: Config mismatch - fGetHostTimeDisabled: config=%RTbool saved=%RTbool\n", pThis->fGetHostTimeDisabled, f));
3970
3971 rc = SSMR3GetBool(pSSM, &f); AssertRCReturn(rc, rc);
3972 if (pThis->fBackdoorLogDisabled != f)
3973 LogRel(("VMMDev: Config mismatch - fBackdoorLogDisabled: config=%RTbool saved=%RTbool\n", pThis->fBackdoorLogDisabled, f));
3974
3975 rc = SSMR3GetBool(pSSM, &f); AssertRCReturn(rc, rc);
3976 if (pThis->fKeepCredentials != f)
3977 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Config mismatch - fKeepCredentials: config=%RTbool saved=%RTbool"),
3978 pThis->fKeepCredentials, f);
3979 rc = SSMR3GetBool(pSSM, &f); AssertRCReturn(rc, rc);
3980 if (pThis->fHeapEnabled != f)
3981 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Config mismatch - fHeapEnabled: config=%RTbool saved=%RTbool"),
3982 pThis->fHeapEnabled, f);
3983 }
3984
3985 if (uPass != SSM_PASS_FINAL)
3986 return VINF_SUCCESS;
3987
3988 /* state */
3989 SSMR3GetU32(pSSM, &pThis->hypervisorSize);
3990 SSMR3GetU32(pSSM, &pThis->mouseCapabilities);
3991 SSMR3GetS32(pSSM, &pThis->mouseXAbs);
3992 SSMR3GetS32(pSSM, &pThis->mouseYAbs);
3993
3994 SSMR3GetBool(pSSM, &pThis->fNewGuestFilterMask);
3995 SSMR3GetU32(pSSM, &pThis->u32NewGuestFilterMask);
3996 SSMR3GetU32(pSSM, &pThis->u32GuestFilterMask);
3997 SSMR3GetU32(pSSM, &pThis->u32HostEventFlags);
3998
3999 //SSMR3GetBool(pSSM, &pThis->pVMMDevRAMR3->fHaveEvents);
4000 // here be dragons (probably)
4001 SSMR3GetMem(pSSM, &pThis->pVMMDevRAMR3->V, sizeof (pThis->pVMMDevRAMR3->V));
4002
4003 SSMR3GetMem(pSSM, &pThis->guestInfo, sizeof (pThis->guestInfo));
4004 SSMR3GetU32(pSSM, &pThis->fu32AdditionsOk);
4005 SSMR3GetU32(pSSM, &pThis->u32VideoAccelEnabled);
4006 if (uVersion > 10)
4007 SSMR3GetBool(pSSM, &pThis->displayChangeData.fGuestSentChangeEventAck);
4008
4009 rc = SSMR3GetU32(pSSM, &pThis->guestCaps);
4010
4011 /* Attributes which were temporarily introduced in r30072 */
4012 if (uVersion == 7)
4013 {
4014 uint32_t temp;
4015 SSMR3GetU32(pSSM, &temp);
4016 rc = SSMR3GetU32(pSSM, &temp);
4017 }
4018 AssertRCReturn(rc, rc);
4019
4020#ifdef VBOX_WITH_HGCM
4021 rc = vmmdevHGCMLoadState(pThis, pSSM, uVersion);
4022 AssertRCReturn(rc, rc);
4023#endif /* VBOX_WITH_HGCM */
4024
4025 if (uVersion >= 10)
4026 rc = SSMR3GetU32(pSSM, &pThis->fHostCursorRequested);
4027 AssertRCReturn(rc, rc);
4028
4029 if (uVersion > VMMDEV_SAVED_STATE_VERSION_MISSING_GUEST_INFO_2)
4030 {
4031 SSMR3GetU32(pSSM, &pThis->guestInfo2.uFullVersion);
4032 SSMR3GetU32(pSSM, &pThis->guestInfo2.uRevision);
4033 SSMR3GetU32(pSSM, &pThis->guestInfo2.fFeatures);
4034 rc = SSMR3GetStrZ(pSSM, &pThis->guestInfo2.szName[0], sizeof(pThis->guestInfo2.szName));
4035 AssertRCReturn(rc, rc);
4036 }
4037
4038 if (uVersion > VMMDEV_SAVED_STATE_VERSION_MISSING_FACILITY_STATUSES)
4039 {
4040 uint32_t cFacilityStatuses;
4041 rc = SSMR3GetU32(pSSM, &cFacilityStatuses);
4042 AssertRCReturn(rc, rc);
4043
4044 for (uint32_t i = 0; i < cFacilityStatuses; i++)
4045 {
4046 uint32_t uFacility, fFlags;
4047 uint16_t uStatus;
4048 int64_t iTimeStampNano;
4049
4050 SSMR3GetU32(pSSM, &uFacility);
4051 SSMR3GetU32(pSSM, &fFlags);
4052 SSMR3GetU16(pSSM, &uStatus);
4053 rc = SSMR3GetS64(pSSM, &iTimeStampNano);
4054 AssertRCReturn(rc, rc);
4055
4056 PVMMDEVFACILITYSTATUSENTRY pEntry = vmmdevGetFacilityStatusEntry(pThis, (VBoxGuestFacilityType)uFacility);
4057 AssertLogRelMsgReturn(pEntry,
4058 ("VMMDev: Ran out of entries restoring the guest facility statuses. Saved state has %u.\n", cFacilityStatuses),
4059 VERR_OUT_OF_RESOURCES);
4060 pEntry->enmStatus = (VBoxGuestFacilityStatus)uStatus;
4061 pEntry->fFlags = fFlags;
4062 RTTimeSpecSetNano(&pEntry->TimeSpecTS, iTimeStampNano);
4063 }
4064 }
4065
4066 /*
4067 * Heartbeat.
4068 */
4069 if (uVersion >= VMMDEV_SAVED_STATE_VERSION_HEARTBEAT)
4070 {
4071 SSMR3GetBool(pSSM, (bool *)&pThis->fHeartbeatActive);
4072 SSMR3GetBool(pSSM, (bool *)&pThis->fFlatlined);
4073 SSMR3GetU64(pSSM, (uint64_t *)&pThis->nsLastHeartbeatTS);
4074 rc = TMR3TimerLoad(pThis->pFlatlinedTimer, pSSM);
4075 AssertRCReturn(rc, rc);
4076 if (pThis->fFlatlined)
4077 LogRel(("vmmdevLoadState: Guest has flatlined. Last heartbeat %'RU64 ns before state was saved.\n",
4078 TMTimerGetNano(pThis->pFlatlinedTimer) - pThis->nsLastHeartbeatTS));
4079 }
4080
4081 /*
4082 * On a resume, we send the capabilities changed message so
4083 * that listeners can sync their state again
4084 */
4085 Log(("vmmdevLoadState: capabilities changed (%x), informing connector\n", pThis->mouseCapabilities));
4086 if (pThis->pDrv)
4087 {
4088 pThis->pDrv->pfnUpdateMouseCapabilities(pThis->pDrv, pThis->mouseCapabilities);
4089 if (uVersion >= 10)
4090 pThis->pDrv->pfnUpdatePointerShape(pThis->pDrv,
4091 /*fVisible=*/!!pThis->fHostCursorRequested,
4092 /*fAlpha=*/false,
4093 /*xHot=*/0, /*yHot=*/0,
4094 /*cx=*/0, /*cy=*/0,
4095 /*pvShape=*/NULL);
4096 }
4097
4098 if (pThis->fu32AdditionsOk)
4099 {
4100 vmmdevLogGuestOsInfo(&pThis->guestInfo);
4101 if (pThis->pDrv)
4102 {
4103 if (pThis->guestInfo2.uFullVersion && pThis->pDrv->pfnUpdateGuestInfo2)
4104 pThis->pDrv->pfnUpdateGuestInfo2(pThis->pDrv, pThis->guestInfo2.uFullVersion, pThis->guestInfo2.szName,
4105 pThis->guestInfo2.uRevision, pThis->guestInfo2.fFeatures);
4106 if (pThis->pDrv->pfnUpdateGuestInfo)
4107 pThis->pDrv->pfnUpdateGuestInfo(pThis->pDrv, &pThis->guestInfo);
4108
4109 if (pThis->pDrv->pfnUpdateGuestStatus)
4110 {
4111 for (uint32_t i = 0; i < pThis->cFacilityStatuses; i++) /* ascending order! */
4112 if ( pThis->aFacilityStatuses[i].enmStatus != VBoxGuestFacilityStatus_Inactive
4113 || !pThis->aFacilityStatuses[i].fFixed)
4114 pThis->pDrv->pfnUpdateGuestStatus(pThis->pDrv,
4115 pThis->aFacilityStatuses[i].enmFacility,
4116 (uint16_t)pThis->aFacilityStatuses[i].enmStatus,
4117 pThis->aFacilityStatuses[i].fFlags,
4118 &pThis->aFacilityStatuses[i].TimeSpecTS);
4119 }
4120 }
4121 }
4122 if (pThis->pDrv && pThis->pDrv->pfnUpdateGuestCapabilities)
4123 pThis->pDrv->pfnUpdateGuestCapabilities(pThis->pDrv, pThis->guestCaps);
4124
4125 return VINF_SUCCESS;
4126}
4127
4128/**
4129 * Load state done callback. Notify guest of restore event.
4130 *
4131 * @returns VBox status code.
4132 * @param pDevIns The device instance.
4133 * @param pSSM The handle to the saved state.
4134 */
4135static DECLCALLBACK(int) vmmdevLoadStateDone(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
4136{
4137 RT_NOREF1(pSSM);
4138 PVMMDEV pThis = PDMINS_2_DATA(pDevIns, PVMMDEV);
4139
4140#ifdef VBOX_WITH_HGCM
4141 int rc = vmmdevHGCMLoadStateDone(pThis);
4142 AssertLogRelRCReturn(rc, rc);
4143#endif /* VBOX_WITH_HGCM */
4144
4145 /* Reestablish the acceleration status. */
4146 if ( pThis->u32VideoAccelEnabled
4147 && pThis->pDrv)
4148 {
4149 pThis->pDrv->pfnVideoAccelEnable(pThis->pDrv, !!pThis->u32VideoAccelEnabled, &pThis->pVMMDevRAMR3->vbvaMemory);
4150 }
4151
4152 VMMDevNotifyGuest(pThis, VMMDEV_EVENT_RESTORED);
4153
4154 return VINF_SUCCESS;
4155}
4156
4157
4158/* -=-=-=-=- PDMDEVREG -=-=-=-=- */
4159
4160/**
4161 * (Re-)initializes the MMIO2 data.
4162 *
4163 * @param pThis Pointer to the VMMDev instance data.
4164 */
4165static void vmmdevInitRam(PVMMDEV pThis)
4166{
4167 memset(pThis->pVMMDevRAMR3, 0, sizeof(VMMDevMemory));
4168 pThis->pVMMDevRAMR3->u32Size = sizeof(VMMDevMemory);
4169 pThis->pVMMDevRAMR3->u32Version = VMMDEV_MEMORY_VERSION;
4170}
4171
4172
4173/**
4174 * @interface_method_impl{PDMDEVREG,pfnReset}
4175 */
4176static DECLCALLBACK(void) vmmdevReset(PPDMDEVINS pDevIns)
4177{
4178 PVMMDEV pThis = PDMINS_2_DATA(pDevIns, PVMMDEV);
4179 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
4180
4181 /*
4182 * Reset the mouse integration feature bits
4183 */
4184 if (pThis->mouseCapabilities & VMMDEV_MOUSE_GUEST_MASK)
4185 {
4186 pThis->mouseCapabilities &= ~VMMDEV_MOUSE_GUEST_MASK;
4187 /* notify the connector */
4188 Log(("vmmdevReset: capabilities changed (%x), informing connector\n", pThis->mouseCapabilities));
4189 pThis->pDrv->pfnUpdateMouseCapabilities(pThis->pDrv, pThis->mouseCapabilities);
4190 }
4191 pThis->fHostCursorRequested = false;
4192
4193 pThis->hypervisorSize = 0;
4194
4195 /* re-initialize the VMMDev memory */
4196 if (pThis->pVMMDevRAMR3)
4197 vmmdevInitRam(pThis);
4198
4199 /* credentials have to go away (by default) */
4200 if (!pThis->fKeepCredentials)
4201 {
4202 memset(pThis->pCredentials->Logon.szUserName, '\0', VMMDEV_CREDENTIALS_SZ_SIZE);
4203 memset(pThis->pCredentials->Logon.szPassword, '\0', VMMDEV_CREDENTIALS_SZ_SIZE);
4204 memset(pThis->pCredentials->Logon.szDomain, '\0', VMMDEV_CREDENTIALS_SZ_SIZE);
4205 }
4206 memset(pThis->pCredentials->Judge.szUserName, '\0', VMMDEV_CREDENTIALS_SZ_SIZE);
4207 memset(pThis->pCredentials->Judge.szPassword, '\0', VMMDEV_CREDENTIALS_SZ_SIZE);
4208 memset(pThis->pCredentials->Judge.szDomain, '\0', VMMDEV_CREDENTIALS_SZ_SIZE);
4209
4210 /* Reset means that additions will report again. */
4211 const bool fVersionChanged = pThis->fu32AdditionsOk
4212 || pThis->guestInfo.interfaceVersion
4213 || pThis->guestInfo.osType != VBOXOSTYPE_Unknown;
4214 if (fVersionChanged)
4215 Log(("vmmdevReset: fu32AdditionsOk=%d additionsVersion=%x osType=%#x\n",
4216 pThis->fu32AdditionsOk, pThis->guestInfo.interfaceVersion, pThis->guestInfo.osType));
4217 pThis->fu32AdditionsOk = false;
4218 memset (&pThis->guestInfo, 0, sizeof (pThis->guestInfo));
4219 RT_ZERO(pThis->guestInfo2);
4220 const bool fCapsChanged = pThis->guestCaps != 0; /* Report transition to 0. */
4221 pThis->guestCaps = 0;
4222
4223 /* Clear facilities. No need to tell Main as it will get a
4224 pfnUpdateGuestInfo callback. */
4225 RTTIMESPEC TimeStampNow;
4226 RTTimeNow(&TimeStampNow);
4227 uint32_t iFacility = pThis->cFacilityStatuses;
4228 while (iFacility-- > 0)
4229 {
4230 pThis->aFacilityStatuses[iFacility].enmStatus = VBoxGuestFacilityStatus_Inactive;
4231 pThis->aFacilityStatuses[iFacility].TimeSpecTS = TimeStampNow;
4232 }
4233
4234 /* clear pending display change request. */
4235 for (unsigned i = 0; i < RT_ELEMENTS(pThis->displayChangeData.aRequests); i++)
4236 {
4237 DISPLAYCHANGEREQUEST *pRequest = &pThis->displayChangeData.aRequests[i];
4238 memset (&pRequest->lastReadDisplayChangeRequest, 0, sizeof (pRequest->lastReadDisplayChangeRequest));
4239 }
4240 pThis->displayChangeData.iCurrentMonitor = 0;
4241 pThis->displayChangeData.fGuestSentChangeEventAck = false;
4242
4243 /* disable seamless mode */
4244 pThis->fLastSeamlessEnabled = false;
4245
4246 /* disabled memory ballooning */
4247 pThis->cMbMemoryBalloonLast = 0;
4248
4249 /* disabled statistics updating */
4250 pThis->u32LastStatIntervalSize = 0;
4251
4252#ifdef VBOX_WITH_HGCM
4253 /* Clear the "HGCM event enabled" flag so the event can be automatically reenabled. */
4254 pThis->u32HGCMEnabled = 0;
4255#endif
4256
4257 /*
4258 * Deactive heartbeat.
4259 */
4260 if (pThis->fHeartbeatActive)
4261 {
4262 TMTimerStop(pThis->pFlatlinedTimer);
4263 pThis->fFlatlined = false;
4264 pThis->fHeartbeatActive = true;
4265 }
4266
4267 /*
4268 * Clear the event variables.
4269 *
4270 * XXX By design we should NOT clear pThis->u32HostEventFlags because it is designed
4271 * that way so host events do not depend on guest resets. However, the pending
4272 * event flags actually _were_ cleared since ages so we mask out events from
4273 * clearing which we really need to survive the reset. See xtracker 5767.
4274 */
4275 pThis->u32HostEventFlags &= VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST;
4276 pThis->u32GuestFilterMask = 0;
4277 pThis->u32NewGuestFilterMask = 0;
4278 pThis->fNewGuestFilterMask = 0;
4279
4280 /*
4281 * Call the update functions as required.
4282 */
4283 if (fVersionChanged && pThis->pDrv && pThis->pDrv->pfnUpdateGuestInfo)
4284 pThis->pDrv->pfnUpdateGuestInfo(pThis->pDrv, &pThis->guestInfo);
4285 if (fCapsChanged && pThis->pDrv && pThis->pDrv->pfnUpdateGuestCapabilities)
4286 pThis->pDrv->pfnUpdateGuestCapabilities(pThis->pDrv, pThis->guestCaps);
4287
4288 /*
4289 * Generate a unique session id for this VM; it will be changed for each start, reset or restore.
4290 * This can be used for restore detection inside the guest.
4291 */
4292 pThis->idSession = ASMReadTSC();
4293
4294 PDMCritSectLeave(&pThis->CritSect);
4295}
4296
4297
4298/**
4299 * @interface_method_impl{PDMDEVREG,pfnRelocate}
4300 */
4301static DECLCALLBACK(void) vmmdevRelocate(PPDMDEVINS pDevIns, RTGCINTPTR offDelta)
4302{
4303 if (offDelta)
4304 {
4305 PVMMDEV pThis = PDMINS_2_DATA(pDevIns, PVMMDEV);
4306 LogFlow(("vmmdevRelocate: offDelta=%RGv\n", offDelta));
4307
4308 if (pThis->pVMMDevRAMRC)
4309 pThis->pVMMDevRAMRC += offDelta;
4310 pThis->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
4311 }
4312}
4313
4314
4315/**
4316 * @interface_method_impl{PDMDEVREG,pfnDestruct}
4317 */
4318static DECLCALLBACK(int) vmmdevDestruct(PPDMDEVINS pDevIns)
4319{
4320 PVMMDEV pThis = PDMINS_2_DATA(pDevIns, PVMMDEV);
4321 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
4322
4323 /*
4324 * Wipe and free the credentials.
4325 */
4326 if (pThis->pCredentials)
4327 {
4328 RTMemWipeThoroughly(pThis->pCredentials, sizeof(*pThis->pCredentials), 10);
4329 RTMemFree(pThis->pCredentials);
4330 pThis->pCredentials = NULL;
4331 }
4332
4333#ifdef VBOX_WITH_HGCM
4334 /*
4335 * Everything HGCM.
4336 */
4337 vmmdevHGCMDestroy(pThis);
4338#endif
4339
4340 /*
4341 * Free the request buffers.
4342 */
4343 for (uint32_t iCpu = 0; iCpu < RT_ELEMENTS(pThis->apReqBufs); iCpu++)
4344 {
4345 pThis->apReqBufs[iCpu] = NULL;
4346 RTMemPageFree(pThis->apReqBufs[iCpu], _4K);
4347 }
4348
4349#ifndef VBOX_WITHOUT_TESTING_FEATURES
4350 /*
4351 * Clean up the testing device.
4352 */
4353 vmmdevTestingTerminate(pDevIns);
4354#endif
4355
4356 return VINF_SUCCESS;
4357}
4358
4359
4360/**
4361 * @interface_method_impl{PDMDEVREG,pfnConstruct}
4362 */
4363static DECLCALLBACK(int) vmmdevConstruct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
4364{
4365 PVMMDEV pThis = PDMINS_2_DATA(pDevIns, PVMMDEV);
4366 int rc;
4367
4368 Assert(iInstance == 0);
4369 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
4370
4371 /*
4372 * Initialize data (most of it anyway).
4373 */
4374 /* Save PDM device instance data for future reference. */
4375 pThis->pDevInsR3 = pDevIns;
4376 pThis->pDevInsR0 = PDMDEVINS_2_R0PTR(pDevIns);
4377 pThis->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
4378
4379 /* PCI vendor, just a free bogus value */
4380 PCIDevSetVendorId(&pThis->PciDev, 0x80ee);
4381 /* device ID */
4382 PCIDevSetDeviceId(&pThis->PciDev, 0xcafe);
4383 /* class sub code (other type of system peripheral) */
4384 PCIDevSetClassSub(&pThis->PciDev, 0x80);
4385 /* class base code (base system peripheral) */
4386 PCIDevSetClassBase(&pThis->PciDev, 0x08);
4387 /* header type */
4388 PCIDevSetHeaderType(&pThis->PciDev, 0x00);
4389 /* interrupt on pin 0 */
4390 PCIDevSetInterruptPin(&pThis->PciDev, 0x01);
4391
4392 RTTIMESPEC TimeStampNow;
4393 RTTimeNow(&TimeStampNow);
4394 vmmdevAllocFacilityStatusEntry(pThis, VBoxGuestFacilityType_VBoxGuestDriver, true /*fFixed*/, &TimeStampNow);
4395 vmmdevAllocFacilityStatusEntry(pThis, VBoxGuestFacilityType_VBoxService, true /*fFixed*/, &TimeStampNow);
4396 vmmdevAllocFacilityStatusEntry(pThis, VBoxGuestFacilityType_VBoxTrayClient, true /*fFixed*/, &TimeStampNow);
4397 vmmdevAllocFacilityStatusEntry(pThis, VBoxGuestFacilityType_Seamless, true /*fFixed*/, &TimeStampNow);
4398 vmmdevAllocFacilityStatusEntry(pThis, VBoxGuestFacilityType_Graphics, true /*fFixed*/, &TimeStampNow);
4399 Assert(pThis->cFacilityStatuses == 5);
4400
4401 /*
4402 * Interfaces
4403 */
4404 /* IBase */
4405 pThis->IBase.pfnQueryInterface = vmmdevPortQueryInterface;
4406
4407 /* VMMDev port */
4408 pThis->IPort.pfnQueryAbsoluteMouse = vmmdevIPort_QueryAbsoluteMouse;
4409 pThis->IPort.pfnSetAbsoluteMouse = vmmdevIPort_SetAbsoluteMouse ;
4410 pThis->IPort.pfnQueryMouseCapabilities = vmmdevIPort_QueryMouseCapabilities;
4411 pThis->IPort.pfnUpdateMouseCapabilities = vmmdevIPort_UpdateMouseCapabilities;
4412 pThis->IPort.pfnRequestDisplayChange = vmmdevIPort_RequestDisplayChange;
4413 pThis->IPort.pfnSetCredentials = vmmdevIPort_SetCredentials;
4414 pThis->IPort.pfnVBVAChange = vmmdevIPort_VBVAChange;
4415 pThis->IPort.pfnRequestSeamlessChange = vmmdevIPort_RequestSeamlessChange;
4416 pThis->IPort.pfnSetMemoryBalloon = vmmdevIPort_SetMemoryBalloon;
4417 pThis->IPort.pfnSetStatisticsInterval = vmmdevIPort_SetStatisticsInterval;
4418 pThis->IPort.pfnVRDPChange = vmmdevIPort_VRDPChange;
4419 pThis->IPort.pfnCpuHotUnplug = vmmdevIPort_CpuHotUnplug;
4420 pThis->IPort.pfnCpuHotPlug = vmmdevIPort_CpuHotPlug;
4421
4422 /* Shared folder LED */
4423 pThis->SharedFolders.Led.u32Magic = PDMLED_MAGIC;
4424 pThis->SharedFolders.ILeds.pfnQueryStatusLed = vmmdevQueryStatusLed;
4425
4426#ifdef VBOX_WITH_HGCM
4427 /* HGCM port */
4428 pThis->IHGCMPort.pfnCompleted = hgcmCompleted;
4429 pThis->IHGCMPort.pfnIsCmdRestored = hgcmIsCmdRestored;
4430 pThis->IHGCMPort.pfnIsCmdCancelled = hgcmIsCmdCancelled;
4431 pThis->IHGCMPort.pfnGetRequestor = hgcmGetRequestor;
4432 pThis->IHGCMPort.pfnGetVMMDevSessionId = hgcmGetVMMDevSessionId;
4433#endif
4434
4435 pThis->pCredentials = (VMMDEVCREDS *)RTMemAllocZ(sizeof(*pThis->pCredentials));
4436 if (!pThis->pCredentials)
4437 return VERR_NO_MEMORY;
4438
4439
4440 /*
4441 * Validate and read the configuration.
4442 */
4443 PDMDEV_VALIDATE_CONFIG_RETURN(pDevIns,
4444 "GetHostTimeDisabled|"
4445 "BackdoorLogDisabled|"
4446 "KeepCredentials|"
4447 "HeapEnabled|"
4448 "RZEnabled|"
4449 "GuestCoreDumpEnabled|"
4450 "GuestCoreDumpDir|"
4451 "GuestCoreDumpCount|"
4452 "HeartbeatInterval|"
4453 "HeartbeatTimeout|"
4454 "TestingEnabled|"
4455 "TestingMMIO|"
4456 "TestintXmlOutputFile"
4457 ,
4458 "");
4459
4460 rc = CFGMR3QueryBoolDef(pCfg, "GetHostTimeDisabled", &pThis->fGetHostTimeDisabled, false);
4461 if (RT_FAILURE(rc))
4462 return PDMDEV_SET_ERROR(pDevIns, rc,
4463 N_("Configuration error: Failed querying \"GetHostTimeDisabled\" as a boolean"));
4464
4465 rc = CFGMR3QueryBoolDef(pCfg, "BackdoorLogDisabled", &pThis->fBackdoorLogDisabled, false);
4466 if (RT_FAILURE(rc))
4467 return PDMDEV_SET_ERROR(pDevIns, rc,
4468 N_("Configuration error: Failed querying \"BackdoorLogDisabled\" as a boolean"));
4469
4470 rc = CFGMR3QueryBoolDef(pCfg, "KeepCredentials", &pThis->fKeepCredentials, false);
4471 if (RT_FAILURE(rc))
4472 return PDMDEV_SET_ERROR(pDevIns, rc,
4473 N_("Configuration error: Failed querying \"KeepCredentials\" as a boolean"));
4474
4475 rc = CFGMR3QueryBoolDef(pCfg, "HeapEnabled", &pThis->fHeapEnabled, true);
4476 if (RT_FAILURE(rc))
4477 return PDMDEV_SET_ERROR(pDevIns, rc,
4478 N_("Configuration error: Failed querying \"HeapEnabled\" as a boolean"));
4479
4480 rc = CFGMR3QueryBoolDef(pCfg, "RZEnabled", &pThis->fRZEnabled, true);
4481 if (RT_FAILURE(rc))
4482 return PDMDEV_SET_ERROR(pDevIns, rc,
4483 N_("Configuration error: Failed querying \"RZEnabled\" as a boolean"));
4484
4485 rc = CFGMR3QueryBoolDef(pCfg, "GuestCoreDumpEnabled", &pThis->fGuestCoreDumpEnabled, false);
4486 if (RT_FAILURE(rc))
4487 return PDMDEV_SET_ERROR(pDevIns, rc,
4488 N_("Configuration error: Failed querying \"GuestCoreDumpEnabled\" as a boolean"));
4489
4490 char *pszGuestCoreDumpDir = NULL;
4491 rc = CFGMR3QueryStringAllocDef(pCfg, "GuestCoreDumpDir", &pszGuestCoreDumpDir, "");
4492 if (RT_FAILURE(rc))
4493 return PDMDEV_SET_ERROR(pDevIns, rc,
4494 N_("Configuration error: Failed querying \"GuestCoreDumpDir\" as a string"));
4495
4496 RTStrCopy(pThis->szGuestCoreDumpDir, sizeof(pThis->szGuestCoreDumpDir), pszGuestCoreDumpDir);
4497 MMR3HeapFree(pszGuestCoreDumpDir);
4498
4499 rc = CFGMR3QueryU32Def(pCfg, "GuestCoreDumpCount", &pThis->cGuestCoreDumps, 3);
4500 if (RT_FAILURE(rc))
4501 return PDMDEV_SET_ERROR(pDevIns, rc,
4502 N_("Configuration error: Failed querying \"GuestCoreDumpCount\" as a 32-bit unsigned integer"));
4503
4504 rc = CFGMR3QueryU64Def(pCfg, "HeartbeatInterval", &pThis->cNsHeartbeatInterval, VMMDEV_HEARTBEAT_DEFAULT_INTERVAL);
4505 if (RT_FAILURE(rc))
4506 return PDMDEV_SET_ERROR(pDevIns, rc,
4507 N_("Configuration error: Failed querying \"HeartbeatInterval\" as a 64-bit unsigned integer"));
4508 if (pThis->cNsHeartbeatInterval < RT_NS_100MS / 2)
4509 return PDMDEV_SET_ERROR(pDevIns, rc,
4510 N_("Configuration error: Heartbeat interval \"HeartbeatInterval\" too small"));
4511
4512 rc = CFGMR3QueryU64Def(pCfg, "HeartbeatTimeout", &pThis->cNsHeartbeatTimeout, pThis->cNsHeartbeatInterval * 2);
4513 if (RT_FAILURE(rc))
4514 return PDMDEV_SET_ERROR(pDevIns, rc,
4515 N_("Configuration error: Failed querying \"HeartbeatTimeout\" as a 64-bit unsigned integer"));
4516 if (pThis->cNsHeartbeatTimeout < RT_NS_100MS)
4517 return PDMDEV_SET_ERROR(pDevIns, rc,
4518 N_("Configuration error: Heartbeat timeout \"HeartbeatTimeout\" too small"));
4519 if (pThis->cNsHeartbeatTimeout <= pThis->cNsHeartbeatInterval + RT_NS_10MS)
4520 return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
4521 N_("Configuration error: Heartbeat timeout \"HeartbeatTimeout\" value (%'ull ns) is too close to the interval (%'ull ns)"),
4522 pThis->cNsHeartbeatTimeout, pThis->cNsHeartbeatInterval);
4523
4524#ifndef VBOX_WITHOUT_TESTING_FEATURES
4525 rc = CFGMR3QueryBoolDef(pCfg, "TestingEnabled", &pThis->fTestingEnabled, false);
4526 if (RT_FAILURE(rc))
4527 return PDMDEV_SET_ERROR(pDevIns, rc,
4528 N_("Configuration error: Failed querying \"TestingEnabled\" as a boolean"));
4529 rc = CFGMR3QueryBoolDef(pCfg, "TestingMMIO", &pThis->fTestingMMIO, false);
4530 if (RT_FAILURE(rc))
4531 return PDMDEV_SET_ERROR(pDevIns, rc,
4532 N_("Configuration error: Failed querying \"TestingMMIO\" as a boolean"));
4533 rc = CFGMR3QueryStringAllocDef(pCfg, "TestintXmlOutputFile", &pThis->pszTestingXmlOutput, NULL);
4534 if (RT_FAILURE(rc))
4535 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Failed querying \"TestintXmlOutputFile\" as a string"));
4536
4537 /** @todo image-to-load-filename? */
4538#endif
4539
4540 pThis->cbGuestRAM = MMR3PhysGetRamSize(PDMDevHlpGetVM(pDevIns));
4541
4542 /*
4543 * We do our own locking entirely. So, install NOP critsect for the device
4544 * and create our own critsect for use where it really matters (++).
4545 */
4546 rc = PDMDevHlpSetDeviceCritSect(pDevIns, PDMDevHlpCritSectGetNop(pDevIns));
4547 AssertRCReturn(rc, rc);
4548 rc = PDMDevHlpCritSectInit(pDevIns, &pThis->CritSect, RT_SRC_POS, "VMMDev#%u", iInstance);
4549 AssertRCReturn(rc, rc);
4550
4551 /*
4552 * Register the backdoor logging port
4553 */
4554 rc = PDMDevHlpIOPortRegister(pDevIns, RTLOG_DEBUG_PORT, 1, NULL, vmmdevBackdoorLog,
4555 NULL, NULL, NULL, "VMMDev backdoor logging");
4556 AssertRCReturn(rc, rc);
4557
4558#ifdef VMMDEV_WITH_ALT_TIMESYNC
4559 /*
4560 * Alternative timesync source.
4561 *
4562 * This was orignally added for creating a simple time sync service in an
4563 * OpenBSD guest without requiring VBoxGuest and VBoxService to be ported
4564 * first. We keep it in case it comes in handy.
4565 */
4566 rc = PDMDevHlpIOPortRegister(pDevIns, 0x505, 1, NULL,
4567 vmmdevAltTimeSyncWrite, vmmdevAltTimeSyncRead,
4568 NULL, NULL, "VMMDev timesync backdoor");
4569 AssertRCReturn(rc, rc);
4570#endif
4571
4572 /*
4573 * Register the PCI device.
4574 */
4575 rc = PDMDevHlpPCIRegister(pDevIns, &pThis->PciDev);
4576 if (RT_FAILURE(rc))
4577 return rc;
4578 if (pThis->PciDev.uDevFn != 32 || iInstance != 0)
4579 Log(("!!WARNING!!: pThis->PciDev.uDevFn=%d (ignore if testcase or no started by Main)\n", pThis->PciDev.uDevFn));
4580 rc = PDMDevHlpPCIIORegionRegister(pDevIns, 0, 0x20, PCI_ADDRESS_SPACE_IO, vmmdevIOPortRegionMap);
4581 if (RT_FAILURE(rc))
4582 return rc;
4583 rc = PDMDevHlpPCIIORegionRegister(pDevIns, 1, VMMDEV_RAM_SIZE, PCI_ADDRESS_SPACE_MEM, vmmdevIORAMRegionMap);
4584 if (RT_FAILURE(rc))
4585 return rc;
4586 if (pThis->fHeapEnabled)
4587 {
4588 rc = PDMDevHlpPCIIORegionRegister(pDevIns, 2, VMMDEV_HEAP_SIZE, PCI_ADDRESS_SPACE_MEM_PREFETCH, vmmdevIORAMRegionMap);
4589 if (RT_FAILURE(rc))
4590 return rc;
4591 }
4592
4593 /*
4594 * Allocate and initialize the MMIO2 memory.
4595 *
4596 * We map the first page into raw-mode and kernel contexts so we can handle
4597 * interrupt acknowledge requests more timely.
4598 */
4599 rc = PDMDevHlpMMIO2Register(pDevIns, &pThis->PciDev, 1 /*iRegion*/, VMMDEV_RAM_SIZE, 0 /*fFlags*/,
4600 (void **)&pThis->pVMMDevRAMR3, "VMMDev");
4601 if (RT_FAILURE(rc))
4602 return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
4603 N_("Failed to allocate %u bytes of memory for the VMM device"), VMMDEV_RAM_SIZE);
4604 vmmdevInitRam(pThis);
4605 if (pThis->fRZEnabled)
4606 {
4607 rc = PDMDevHlpMMIO2MapKernel(pDevIns, &pThis->PciDev, 1 /*iRegion*/, 0 /*off*/, PAGE_SIZE, "VMMDev", &pThis->pVMMDevRAMR0);
4608 if (RT_FAILURE(rc))
4609 return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
4610 N_("Failed to map first page of the VMMDev ram into kernel space: %Rrc"), rc);
4611
4612#ifdef VBOX_WITH_RAW_MODE
4613 rc = PDMDevHlpMMHyperMapMMIO2(pDevIns, &pThis->PciDev, 1 /*iRegion*/, 0 /*off*/, PAGE_SIZE, "VMMDev", &pThis->pVMMDevRAMRC);
4614 if (RT_FAILURE(rc))
4615 return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
4616 N_("Failed to map first page of the VMMDev ram into raw-mode context: %Rrc"), rc);
4617#endif
4618 }
4619
4620 /*
4621 * Allocate and initialize the MMIO2 heap.
4622 */
4623 if (pThis->fHeapEnabled)
4624 {
4625 rc = PDMDevHlpMMIO2Register(pDevIns, &pThis->PciDev, 2 /*iRegion*/, VMMDEV_HEAP_SIZE, 0 /*fFlags*/,
4626 (void **)&pThis->pVMMDevHeapR3, "VMMDev Heap");
4627 if (RT_FAILURE(rc))
4628 return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
4629 N_("Failed to allocate %u bytes of memory for the VMM device heap"), PAGE_SIZE);
4630
4631 /* Register the memory area with PDM so HM can access it before it's mapped. */
4632 rc = PDMDevHlpRegisterVMMDevHeap(pDevIns, NIL_RTGCPHYS, pThis->pVMMDevHeapR3, VMMDEV_HEAP_SIZE);
4633 AssertLogRelRCReturn(rc, rc);
4634 }
4635
4636#ifndef VBOX_WITHOUT_TESTING_FEATURES
4637 /*
4638 * Initialize testing.
4639 */
4640 rc = vmmdevTestingInitialize(pDevIns);
4641 if (RT_FAILURE(rc))
4642 return rc;
4643#endif
4644
4645 /*
4646 * Get the corresponding connector interface
4647 */
4648 rc = PDMDevHlpDriverAttach(pDevIns, 0, &pThis->IBase, &pThis->pDrvBase, "VMM Driver Port");
4649 if (RT_SUCCESS(rc))
4650 {
4651 pThis->pDrv = PDMIBASE_QUERY_INTERFACE(pThis->pDrvBase, PDMIVMMDEVCONNECTOR);
4652 AssertMsgReturn(pThis->pDrv, ("LUN #0 doesn't have a VMMDev connector interface!\n"), VERR_PDM_MISSING_INTERFACE);
4653#ifdef VBOX_WITH_HGCM
4654 pThis->pHGCMDrv = PDMIBASE_QUERY_INTERFACE(pThis->pDrvBase, PDMIHGCMCONNECTOR);
4655 if (!pThis->pHGCMDrv)
4656 {
4657 Log(("LUN #0 doesn't have a HGCM connector interface, HGCM is not supported. rc=%Rrc\n", rc));
4658 /* this is not actually an error, just means that there is no support for HGCM */
4659 }
4660#endif
4661 /* Query the initial balloon size. */
4662 AssertPtr(pThis->pDrv->pfnQueryBalloonSize);
4663 rc = pThis->pDrv->pfnQueryBalloonSize(pThis->pDrv, &pThis->cMbMemoryBalloon);
4664 AssertRC(rc);
4665
4666 Log(("Initial balloon size %x\n", pThis->cMbMemoryBalloon));
4667 }
4668 else if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
4669 {
4670 Log(("%s/%d: warning: no driver attached to LUN #0!\n", pDevIns->pReg->szName, pDevIns->iInstance));
4671 rc = VINF_SUCCESS;
4672 }
4673 else
4674 AssertMsgFailedReturn(("Failed to attach LUN #0! rc=%Rrc\n", rc), rc);
4675
4676 /*
4677 * Attach status driver for shared folders (optional).
4678 */
4679 PPDMIBASE pBase;
4680 rc = PDMDevHlpDriverAttach(pDevIns, PDM_STATUS_LUN, &pThis->IBase, &pBase, "Status Port");
4681 if (RT_SUCCESS(rc))
4682 pThis->SharedFolders.pLedsConnector = PDMIBASE_QUERY_INTERFACE(pBase, PDMILEDCONNECTORS);
4683 else if (rc != VERR_PDM_NO_ATTACHED_DRIVER)
4684 {
4685 AssertMsgFailed(("Failed to attach to status driver. rc=%Rrc\n", rc));
4686 return rc;
4687 }
4688
4689 /*
4690 * Register saved state and init the HGCM CmdList critsect.
4691 */
4692 rc = PDMDevHlpSSMRegisterEx(pDevIns, VMMDEV_SAVED_STATE_VERSION, sizeof(*pThis), NULL,
4693 NULL, vmmdevLiveExec, NULL,
4694 NULL, vmmdevSaveExec, NULL,
4695 NULL, vmmdevLoadExec, vmmdevLoadStateDone);
4696 AssertRCReturn(rc, rc);
4697
4698 /*
4699 * Create heartbeat checking timer.
4700 */
4701 rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL, vmmDevHeartbeatFlatlinedTimer, pThis,
4702 TMTIMER_FLAGS_NO_CRIT_SECT, "Heartbeat flatlined", &pThis->pFlatlinedTimer);
4703 AssertRCReturn(rc, rc);
4704
4705#ifdef VBOX_WITH_HGCM
4706 rc = vmmdevHGCMInit(pThis);
4707 AssertRCReturn(rc, rc);
4708#endif
4709
4710 /*
4711 * In this version of VirtualBox the GUI checks whether "needs host cursor"
4712 * changes.
4713 */
4714 pThis->mouseCapabilities |= VMMDEV_MOUSE_HOST_RECHECKS_NEEDS_HOST_CURSOR;
4715
4716 /*
4717 * Statistics.
4718 */
4719 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatMemBalloonChunks, STAMTYPE_U32, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT,
4720 "Memory balloon size", "/Devices/VMMDev/BalloonChunks");
4721 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatFastIrqAckR3, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT,
4722 "Fast IRQ acknowledgments handled in ring-3.", "/Devices/VMMDev/FastIrqAckR3");
4723 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatFastIrqAckRZ, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT,
4724 "Fast IRQ acknowledgments handled in ring-0 or raw-mode.", "/Devices/VMMDev/FastIrqAckRZ");
4725 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatSlowIrqAck, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT,
4726 "Slow IRQ acknowledgments (old style).", "/Devices/VMMDev/SlowIrqAck");
4727 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatReqBufAllocs, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT,
4728 "Times a larger request buffer was required.", "/Devices/VMMDev/LargeReqBufAllocs");
4729#ifdef VBOX_WITH_HGCM
4730 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatHgcmCmdArrival, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL,
4731 "Profiling HGCM call arrival processing", "/HGCM/MsgArrival");
4732 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatHgcmCmdCompletion, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL,
4733 "Profiling HGCM call completion processing", "/HGCM/MsgCompletion");
4734 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatHgcmCmdTotal, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL,
4735 "Profiling whole HGCM call.", "/HGCM/MsgTotal");
4736 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatHgcmLargeCmdAllocs,STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT,
4737 "Times the allocation cache could not be used.", "/HGCM/LargeCmdAllocs");
4738#endif
4739
4740 /*
4741 * Generate a unique session id for this VM; it will be changed for each
4742 * start, reset or restore. This can be used for restore detection inside
4743 * the guest.
4744 */
4745 pThis->idSession = ASMReadTSC();
4746 return rc;
4747}
4748
4749/**
4750 * The device registration structure.
4751 */
4752extern "C" const PDMDEVREG g_DeviceVMMDev =
4753{
4754 /* u32Version */
4755 PDM_DEVREG_VERSION,
4756 /* szName */
4757 "VMMDev",
4758 /* szRCMod */
4759 "VBoxDDRC.rc",
4760 /* szR0Mod */
4761 "VBoxDDR0.r0",
4762 /* pszDescription */
4763 "VirtualBox VMM Device\n",
4764 /* fFlags */
4765 PDM_DEVREG_FLAGS_HOST_BITS_DEFAULT | PDM_DEVREG_FLAGS_GUEST_BITS_DEFAULT | PDM_DEVREG_FLAGS_RC | PDM_DEVREG_FLAGS_R0,
4766 /* fClass */
4767 PDM_DEVREG_CLASS_VMM_DEV,
4768 /* cMaxInstances */
4769 1,
4770 /* cbInstance */
4771 sizeof(VMMDevState),
4772 /* pfnConstruct */
4773 vmmdevConstruct,
4774 /* pfnDestruct */
4775 vmmdevDestruct,
4776 /* pfnRelocate */
4777 vmmdevRelocate,
4778 /* pfnMemSetup */
4779 NULL,
4780 /* pfnPowerOn */
4781 NULL,
4782 /* pfnReset */
4783 vmmdevReset,
4784 /* pfnSuspend */
4785 NULL,
4786 /* pfnResume */
4787 NULL,
4788 /* pfnAttach */
4789 NULL,
4790 /* pfnDetach */
4791 NULL,
4792 /* pfnQueryInterface. */
4793 NULL,
4794 /* pfnInitComplete */
4795 NULL,
4796 /* pfnPowerOff */
4797 NULL,
4798 /* pfnSoftReset */
4799 NULL,
4800 /* u32VersionEnd */
4801 PDM_DEVREG_VERSION
4802};
4803#endif /* IN_RING3 */
4804#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