VirtualBox

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

Last change on this file since 63009 was 62891, checked in by vboxsync, 9 years ago

Devices: warnings

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

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