VirtualBox

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

最後變更 在這個檔案從81625是 81625,由 vboxsync 提交於 5 年 前

VMMDev: Converted to the new PDM device style. bugref:9218

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

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