VirtualBox

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

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

VMMDev,HGCM: Added cancellation notification and query helper.

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