VirtualBox

source: vbox/trunk/src/VBox/Devices/Graphics/DevVGA_VBVA.cpp@ 95288

Last change on this file since 95288 was 93115, checked in by vboxsync, 3 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 105.8 KB
Line 
1/* $Id: DevVGA_VBVA.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
2/** @file
3 * VirtualBox Video Acceleration (VBVA).
4 */
5
6/*
7 * Copyright (C) 2006-2022 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
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_DEV_VGA
23#include <VBox/vmm/pdmifs.h>
24#include <VBox/vmm/pdmdev.h>
25#include <VBox/vmm/pgm.h>
26#include <VBox/vmm/ssm.h>
27#include <VBox/VMMDev.h>
28#include <VBox/AssertGuest.h>
29#include <VBoxVideo.h>
30#include <iprt/alloc.h>
31#include <iprt/assert.h>
32#include <iprt/asm.h>
33#include <iprt/string.h>
34#include <iprt/param.h>
35#ifdef VBOX_WITH_VIDEOHWACCEL
36#include <iprt/semaphore.h>
37#endif
38
39#include "DevVGA.h"
40
41/* A very detailed logging. */
42#if 0 // def DEBUG_sunlover
43#define LOGVBVABUFFER(a) LogFlow(a)
44#else
45#define LOGVBVABUFFER(a) do {} while (0)
46#endif
47
48
49/*********************************************************************************************************************************
50* Structures and Typedefs *
51*********************************************************************************************************************************/
52typedef struct VBVAPARTIALRECORD
53{
54 uint8_t *pu8;
55 uint32_t cb;
56} VBVAPARTIALRECORD;
57
58typedef struct VBVADATA
59{
60 struct
61 {
62 VBVABUFFER RT_UNTRUSTED_VOLATILE_GUEST *pVBVA; /**< Pointer to the guest memory with the VBVABUFFER. */
63 uint8_t RT_UNTRUSTED_VOLATILE_GUEST *pu8Data; /**< For convenience, pointer to the guest ring buffer (VBVABUFFER::au8Data). */
64 } guest;
65 uint32_t u32VBVAOffset; /**< VBVABUFFER offset in the guest VRAM. */
66 VBVAPARTIALRECORD partialRecord; /**< Partial record temporary storage. */
67 uint32_t off32Data; /**< The offset where the data starts in the VBVABUFFER.
68 * The host code uses it instead of VBVABUFFER::off32Data. */
69 uint32_t indexRecordFirst; /**< Index of the first filled record in VBVABUFFER::aRecords. */
70 uint32_t cbPartialWriteThreshold; /**< Copy of VBVABUFFER::cbPartialWriteThreshold used by host code. */
71 uint32_t cbData; /**< Copy of VBVABUFFER::cbData used by host code. */
72} VBVADATA;
73
74typedef struct VBVAVIEW
75{
76 VBVAINFOVIEW view;
77 VBVAINFOSCREEN screen;
78 VBVADATA vbva;
79} VBVAVIEW;
80
81typedef struct VBVAMOUSESHAPEINFO
82{
83 bool fSet;
84 bool fVisible;
85 bool fAlpha;
86 uint32_t u32HotX;
87 uint32_t u32HotY;
88 uint32_t u32Width;
89 uint32_t u32Height;
90 uint32_t cbShape;
91 uint32_t cbAllocated;
92 uint8_t *pu8Shape;
93} VBVAMOUSESHAPEINFO;
94
95/** @todo saved state: save and restore VBVACONTEXT */
96typedef struct VBVACONTEXT
97{
98 uint32_t cViews;
99 VBVAVIEW aViews[VBOX_VIDEO_MAX_SCREENS];
100 VBVAMOUSESHAPEINFO mouseShapeInfo;
101 bool fPaused;
102 VBVAMODEHINT aModeHints[VBOX_VIDEO_MAX_SCREENS];
103} VBVACONTEXT;
104
105
106static void vbvaDataCleanup(VBVADATA *pVBVAData)
107{
108 if (pVBVAData->guest.pVBVA)
109 {
110 pVBVAData->guest.pVBVA->hostFlags.u32HostEvents = 0;
111 pVBVAData->guest.pVBVA->hostFlags.u32SupportedOrders = 0;
112 }
113
114 RTMemFreeZ(pVBVAData->partialRecord.pu8, pVBVAData->partialRecord.cb);
115
116 RT_ZERO(*pVBVAData);
117 pVBVAData->u32VBVAOffset = HGSMIOFFSET_VOID;
118}
119
120/** Copies @a cb bytes from the VBVA ring buffer to the @a pbDst.
121 * Used for partial records or for records which cross the ring boundary.
122 */
123static bool vbvaFetchBytes(VBVADATA *pVBVAData, uint8_t *pbDst, uint32_t cb)
124{
125 if (cb >= pVBVAData->cbData)
126 {
127 AssertMsgFailed(("cb = 0x%08X, ring buffer size 0x%08X", cb, pVBVAData->cbData));
128 return false;
129 }
130
131 const uint8_t RT_UNTRUSTED_VOLATILE_GUEST *pbSrc = &pVBVAData->guest.pu8Data[pVBVAData->off32Data];
132 const uint32_t u32BytesTillBoundary = pVBVAData->cbData - pVBVAData->off32Data;
133 const int32_t i32Diff = cb - u32BytesTillBoundary;
134
135 if (i32Diff <= 0)
136 {
137 /* Chunk will not cross buffer boundary. */
138 RT_BCOPY_VOLATILE(pbDst, pbSrc, cb);
139 }
140 else
141 {
142 /* Chunk crosses buffer boundary. */
143 RT_BCOPY_VOLATILE(pbDst, pbSrc, u32BytesTillBoundary);
144 RT_BCOPY_VOLATILE(pbDst + u32BytesTillBoundary, &pVBVAData->guest.pu8Data[0], i32Diff);
145 }
146
147 /* Advance data offset and sync with guest. */
148 pVBVAData->off32Data = (pVBVAData->off32Data + cb) % pVBVAData->cbData;
149 pVBVAData->guest.pVBVA->off32Data = pVBVAData->off32Data;
150 return true;
151}
152
153
154static bool vbvaPartialRead(uint32_t cbRecord, VBVADATA *pVBVAData)
155{
156 VBVAPARTIALRECORD *pPartialRecord = &pVBVAData->partialRecord;
157 uint8_t *pu8New;
158
159 LOGVBVABUFFER(("vbvaPartialRead: p = %p, cb = %d, cbRecord 0x%08X\n",
160 pPartialRecord->pu8, pPartialRecord->cb, cbRecord));
161
162 Assert(cbRecord > pPartialRecord->cb); /* Caller ensures this. */
163
164 const uint32_t cbChunk = cbRecord - pPartialRecord->cb;
165 if (cbChunk >= pVBVAData->cbData)
166 {
167 return false;
168 }
169
170 if (pPartialRecord->pu8)
171 {
172 Assert(pPartialRecord->cb);
173 pu8New = (uint8_t *)RTMemRealloc(pPartialRecord->pu8, cbRecord);
174 }
175 else
176 {
177 Assert(!pPartialRecord->cb);
178 pu8New = (uint8_t *)RTMemAlloc(cbRecord);
179 }
180
181 if (!pu8New)
182 {
183 /* Memory allocation failed, fail the function. */
184 Log(("vbvaPartialRead: failed to (re)alocate memory for partial record!!! cbRecord 0x%08X\n",
185 cbRecord));
186
187 return false;
188 }
189
190 /* Fetch data from the ring buffer. */
191 if (!vbvaFetchBytes(pVBVAData, pu8New + pPartialRecord->cb, cbChunk))
192 {
193 return false;
194 }
195
196 pPartialRecord->pu8 = pu8New;
197 pPartialRecord->cb = cbRecord;
198
199 return true;
200}
201
202/**
203 * For contiguous chunks just return the address in the buffer. For crossing
204 * boundary - allocate a buffer from heap.
205 */
206static bool vbvaFetchCmd(VBVADATA *pVBVAData, VBVACMDHDR RT_UNTRUSTED_VOLATILE_GUEST **ppHdr, uint32_t *pcbCmd)
207{
208 VBVAPARTIALRECORD *pPartialRecord = &pVBVAData->partialRecord;
209 uint32_t indexRecordFirst = pVBVAData->indexRecordFirst;
210 const uint32_t indexRecordFree = ASMAtomicReadU32(&pVBVAData->guest.pVBVA->indexRecordFree);
211
212 LOGVBVABUFFER(("first = %d, free = %d\n",
213 indexRecordFirst, indexRecordFree));
214
215 if (indexRecordFree >= RT_ELEMENTS(pVBVAData->guest.pVBVA->aRecords))
216 {
217 return false;
218 }
219
220 if (indexRecordFirst == indexRecordFree)
221 {
222 /* No records to process. Return without assigning output variables. */
223 return true;
224 }
225
226 uint32_t cbRecordCurrent = ASMAtomicReadU32(&pVBVAData->guest.pVBVA->aRecords[indexRecordFirst].cbRecord);
227
228 LOGVBVABUFFER(("cbRecord = 0x%08X, pPartialRecord->cb = 0x%08X\n", cbRecordCurrent, pPartialRecord->cb));
229
230 uint32_t cbRecord = cbRecordCurrent & ~VBVA_F_RECORD_PARTIAL;
231
232 if (cbRecord > VBVA_MAX_RECORD_SIZE)
233 {
234 return false;
235 }
236
237 if (pPartialRecord->cb)
238 {
239 /* There is a partial read in process. Continue with it. */
240 Assert (pPartialRecord->pu8);
241
242 LOGVBVABUFFER(("continue partial record cb = %d cbRecord 0x%08X, first = %d, free = %d\n",
243 pPartialRecord->cb, cbRecordCurrent, indexRecordFirst, indexRecordFree));
244
245 if (cbRecord > pPartialRecord->cb)
246 {
247 /* New data has been added to the record. */
248 if (!vbvaPartialRead(cbRecord, pVBVAData))
249 {
250 return false;
251 }
252 }
253
254 if (!(cbRecordCurrent & VBVA_F_RECORD_PARTIAL))
255 {
256 /* The record is completed by guest. Return it to the caller. */
257 *ppHdr = (VBVACMDHDR *)pPartialRecord->pu8;
258 *pcbCmd = pPartialRecord->cb;
259
260 pPartialRecord->pu8 = NULL;
261 pPartialRecord->cb = 0;
262
263 /* Advance the record index and sync with guest. */
264 pVBVAData->indexRecordFirst = (indexRecordFirst + 1) % RT_ELEMENTS(pVBVAData->guest.pVBVA->aRecords);
265 pVBVAData->guest.pVBVA->indexRecordFirst = pVBVAData->indexRecordFirst;
266
267 LOGVBVABUFFER(("partial done ok, data = %d, free = %d\n",
268 pVBVAData->off32Data, pVBVAData->guest.pVBVA->off32Free));
269 }
270
271 return true;
272 }
273
274 /* A new record need to be processed. */
275 if (cbRecordCurrent & VBVA_F_RECORD_PARTIAL)
276 {
277 /* Current record is being written by guest. '=' is important here,
278 * because the guest will do a FLUSH at this condition.
279 * This partial record is too large for the ring buffer and must
280 * be accumulated in an allocated buffer.
281 */
282 if (cbRecord >= pVBVAData->cbData - pVBVAData->cbPartialWriteThreshold)
283 {
284 /* Partial read must be started. */
285 if (!vbvaPartialRead(cbRecord, pVBVAData))
286 {
287 return false;
288 }
289
290 LOGVBVABUFFER(("started partial record cb = 0x%08X cbRecord 0x%08X, first = %d, free = %d\n",
291 pPartialRecord->cb, cbRecordCurrent, indexRecordFirst, indexRecordFree));
292 }
293
294 return true;
295 }
296
297 /* Current record is complete. If it is not empty, process it. */
298 if (cbRecord >= pVBVAData->cbData)
299 {
300 return false;
301 }
302
303 if (cbRecord)
304 {
305 /* The size of largest contiguous chunk in the ring buffer. */
306 uint32_t u32BytesTillBoundary = pVBVAData->cbData - pVBVAData->off32Data;
307
308 /* The pointer to data in the ring buffer. */
309 uint8_t RT_UNTRUSTED_VOLATILE_GUEST *pbSrc = &pVBVAData->guest.pu8Data[pVBVAData->off32Data];
310
311 /* Fetch or point the data. */
312 if (u32BytesTillBoundary >= cbRecord)
313 {
314 /* The command does not cross buffer boundary. Return address in the buffer. */
315 *ppHdr = (VBVACMDHDR RT_UNTRUSTED_VOLATILE_GUEST *)pbSrc;
316
317 /* The data offset will be updated in vbvaReleaseCmd. */
318 }
319 else
320 {
321 /* The command crosses buffer boundary. Rare case, so not optimized. */
322 uint8_t *pbDst = (uint8_t *)RTMemAlloc(cbRecord);
323 if (!pbDst)
324 {
325 LogFlowFunc (("could not allocate %d bytes from heap!!!\n", cbRecord));
326 return false;
327 }
328
329 vbvaFetchBytes(pVBVAData, pbDst, cbRecord);
330
331 *ppHdr = (VBVACMDHDR *)pbDst;
332
333 LOGVBVABUFFER(("Allocated from heap %p\n", pbDst));
334 }
335 }
336
337 *pcbCmd = cbRecord;
338
339 /* Advance the record index and sync with guest. */
340 pVBVAData->indexRecordFirst = (indexRecordFirst + 1) % RT_ELEMENTS(pVBVAData->guest.pVBVA->aRecords);
341 pVBVAData->guest.pVBVA->indexRecordFirst = pVBVAData->indexRecordFirst;
342
343 LOGVBVABUFFER(("done ok, data = %d, free = %d\n",
344 pVBVAData->off32Data, pVBVAData->guest.pVBVA->off32Free));
345
346 return true;
347}
348
349static void vbvaReleaseCmd(VBVADATA *pVBVAData, VBVACMDHDR RT_UNTRUSTED_VOLATILE_GUEST *pHdr, uint32_t cbCmd)
350{
351 VBVAPARTIALRECORD *pPartialRecord = &pVBVAData->partialRecord;
352 const uint8_t RT_UNTRUSTED_VOLATILE_GUEST *pbRingBuffer = pVBVAData->guest.pu8Data;
353
354 if ( (uintptr_t)pHdr >= (uintptr_t)pbRingBuffer
355 && (uintptr_t)pHdr < (uintptr_t)&pbRingBuffer[pVBVAData->cbData])
356 {
357 /* The pointer is inside ring buffer. Must be continuous chunk. */
358 Assert(pVBVAData->cbData - (uint32_t)((uint8_t *)pHdr - pbRingBuffer) >= cbCmd);
359
360 /* Advance data offset and sync with guest. */
361 pVBVAData->off32Data = (pVBVAData->off32Data + cbCmd) % pVBVAData->cbData;
362 pVBVAData->guest.pVBVA->off32Data = pVBVAData->off32Data;
363
364 Assert(!pPartialRecord->pu8 && pPartialRecord->cb == 0);
365 }
366 else
367 {
368 /* The pointer is outside. It is then an allocated copy. */
369 LOGVBVABUFFER(("Free heap %p\n", pHdr));
370
371 if ((uint8_t *)pHdr == pPartialRecord->pu8)
372 {
373 pPartialRecord->pu8 = NULL;
374 pPartialRecord->cb = 0;
375 }
376 else
377 {
378 Assert(!pPartialRecord->pu8 && pPartialRecord->cb == 0);
379 }
380
381 RTMemFree((void *)pHdr);
382 }
383}
384
385static int vbvaFlushProcess(PVGASTATECC pThisCC, VBVADATA *pVBVAData, unsigned uScreenId)
386{
387 LOGVBVABUFFER(("uScreenId %d, indexRecordFirst = %d, indexRecordFree = %d, off32Data = %d, off32Free = %d\n",
388 uScreenId, pVBVAData->indexRecordFirst, pVBVAData->guest.pVBVA->indexRecordFree,
389 pVBVAData->off32Data, pVBVAData->guest.pVBVA->off32Free));
390 struct
391 {
392 /* The rectangle that includes all dirty rectangles. */
393 int32_t xLeft;
394 int32_t xRight;
395 int32_t yTop;
396 int32_t yBottom;
397 } dirtyRect;
398 RT_ZERO(dirtyRect);
399
400 bool fUpdate = false; /* Whether there were any updates. */
401 bool fDirtyEmpty = true;
402
403 for (;;)
404 {
405 /* Fetch the command data. */
406 VBVACMDHDR RT_UNTRUSTED_VOLATILE_GUEST *pHdr = NULL;
407 uint32_t cbCmd = UINT32_MAX;
408 if (!vbvaFetchCmd(pVBVAData, &pHdr, &cbCmd))
409 {
410 LogFunc(("unable to fetch command. off32Data = %d, off32Free = %d!!!\n",
411 pVBVAData->off32Data, pVBVAData->guest.pVBVA->off32Free));
412 return VERR_NOT_SUPPORTED;
413 }
414
415 if (cbCmd == UINT32_MAX)
416 {
417 /* No more commands yet in the queue. */
418 break;
419 }
420
421 if (cbCmd < sizeof(VBVACMDHDR))
422 {
423 LogFunc(("short command. off32Data = %d, off32Free = %d, cbCmd %d!!!\n",
424 pVBVAData->off32Data, pVBVAData->guest.pVBVA->off32Free, cbCmd));
425
426 return VERR_NOT_SUPPORTED;
427 }
428
429 if (cbCmd != 0)
430 {
431 if (!fUpdate)
432 {
433 pThisCC->pDrv->pfnVBVAUpdateBegin(pThisCC->pDrv, uScreenId);
434 fUpdate = true;
435 }
436
437 /* Updates the rectangle and sends the command to the VRDP server. */
438 pThisCC->pDrv->pfnVBVAUpdateProcess(pThisCC->pDrv, uScreenId, pHdr, cbCmd);
439
440 int32_t xRight = pHdr->x + pHdr->w;
441 int32_t yBottom = pHdr->y + pHdr->h;
442
443 /* These are global coords, relative to the primary screen. */
444
445 LOGVBVABUFFER(("cbCmd = %d, x=%d, y=%d, w=%d, h=%d\n", cbCmd, pHdr->x, pHdr->y, pHdr->w, pHdr->h));
446 LogRel3(("%s: update command cbCmd = %d, x=%d, y=%d, w=%d, h=%d\n",
447 __FUNCTION__, cbCmd, pHdr->x, pHdr->y, pHdr->w, pHdr->h));
448
449 /* Collect all rects into one. */
450 if (fDirtyEmpty)
451 {
452 /* This is the first rectangle to be added. */
453 dirtyRect.xLeft = pHdr->x;
454 dirtyRect.yTop = pHdr->y;
455 dirtyRect.xRight = xRight;
456 dirtyRect.yBottom = yBottom;
457 fDirtyEmpty = false;
458 }
459 else
460 {
461 /* Adjust region coordinates. */
462 if (dirtyRect.xLeft > pHdr->x)
463 {
464 dirtyRect.xLeft = pHdr->x;
465 }
466
467 if (dirtyRect.yTop > pHdr->y)
468 {
469 dirtyRect.yTop = pHdr->y;
470 }
471
472 if (dirtyRect.xRight < xRight)
473 {
474 dirtyRect.xRight = xRight;
475 }
476
477 if (dirtyRect.yBottom < yBottom)
478 {
479 dirtyRect.yBottom = yBottom;
480 }
481 }
482 }
483
484 vbvaReleaseCmd(pVBVAData, pHdr, cbCmd);
485 }
486
487 if (fUpdate)
488 {
489 if (dirtyRect.xRight - dirtyRect.xLeft)
490 {
491 LogRel3(("%s: sending update screen=%d, x=%d, y=%d, w=%d, h=%d\n",
492 __FUNCTION__, uScreenId, dirtyRect.xLeft,
493 dirtyRect.yTop, dirtyRect.xRight - dirtyRect.xLeft,
494 dirtyRect.yBottom - dirtyRect.yTop));
495 pThisCC->pDrv->pfnVBVAUpdateEnd(pThisCC->pDrv, uScreenId, dirtyRect.xLeft, dirtyRect.yTop,
496 dirtyRect.xRight - dirtyRect.xLeft, dirtyRect.yBottom - dirtyRect.yTop);
497 }
498 else
499 {
500 pThisCC->pDrv->pfnVBVAUpdateEnd(pThisCC->pDrv, uScreenId, 0, 0, 0, 0);
501 }
502 }
503
504 return VINF_SUCCESS;
505}
506
507static int vbvaFlush(PVGASTATE pThis, PVGASTATECC pThisCC, VBVACONTEXT *pCtx)
508{
509 int rc = VINF_SUCCESS;
510
511 unsigned uScreenId;
512 for (uScreenId = 0; uScreenId < pCtx->cViews; uScreenId++)
513 {
514 VBVADATA *pVBVAData = &pCtx->aViews[uScreenId].vbva;
515 if (pVBVAData->guest.pVBVA)
516 {
517 rc = vbvaFlushProcess(pThisCC, pVBVAData, uScreenId);
518 if (RT_FAILURE(rc))
519 break;
520 }
521 }
522
523 if (RT_FAILURE(rc))
524 {
525 /* Turn off VBVA processing. */
526 LogRel(("VBVA: Disabling (%Rrc)\n", rc));
527 pThis->fGuestCaps = 0;
528 pThisCC->pDrv->pfnVBVAGuestCapabilityUpdate(pThisCC->pDrv, pThis->fGuestCaps);
529 for (uScreenId = 0; uScreenId < pCtx->cViews; uScreenId++)
530 {
531 VBVADATA *pVBVAData = &pCtx->aViews[uScreenId].vbva;
532 if (pVBVAData->guest.pVBVA)
533 {
534 vbvaDataCleanup(pVBVAData);
535 pThisCC->pDrv->pfnVBVADisable(pThisCC->pDrv, uScreenId);
536 }
537 }
538 }
539
540 return rc;
541}
542
543static int vbvaResize(PVGASTATECC pThisCC, VBVAVIEW *pView, const VBVAINFOSCREEN *pNewScreen, bool fResetInputMapping)
544{
545 /* Callers ensure that pNewScreen contains valid data. */
546
547 /* Apply these changes. */
548 pView->screen = *pNewScreen;
549
550 uint8_t *pbVRam = pThisCC->pbVRam + pView->view.u32ViewOffset;
551 return pThisCC->pDrv->pfnVBVAResize(pThisCC->pDrv, &pView->view, &pView->screen, pbVRam, fResetInputMapping);
552}
553
554static int vbvaEnable(PVGASTATE pThis, PVGASTATECC pThisCC, VBVACONTEXT *pCtx, unsigned uScreenId,
555 VBVABUFFER RT_UNTRUSTED_VOLATILE_GUEST *pVBVA, uint32_t u32Offset, bool fRestored)
556{
557 /*
558 * Copy into non-volatile memory and validate its content.
559 */
560 VBVABUFFER VbgaSafe;
561 RT_COPY_VOLATILE(VbgaSafe, *pVBVA);
562 RT_UNTRUSTED_NONVOLATILE_COPY_FENCE();
563
564 uint32_t const cbVBVABuffer = RT_UOFFSETOF(VBVABUFFER, au8Data) + VbgaSafe.cbData;
565 ASSERT_GUEST_RETURN( VbgaSafe.cbData <= UINT32_MAX - RT_UOFFSETOF(VBVABUFFER, au8Data)
566 && cbVBVABuffer <= pThis->vram_size
567 && u32Offset <= pThis->vram_size - cbVBVABuffer,
568 VERR_INVALID_PARAMETER);
569 if (!fRestored)
570 {
571 ASSERT_GUEST_RETURN(VbgaSafe.off32Data == 0, VERR_INVALID_PARAMETER);
572 ASSERT_GUEST_RETURN(VbgaSafe.off32Free == 0, VERR_INVALID_PARAMETER);
573 ASSERT_GUEST_RETURN(VbgaSafe.indexRecordFirst == 0, VERR_INVALID_PARAMETER);
574 ASSERT_GUEST_RETURN(VbgaSafe.indexRecordFree == 0, VERR_INVALID_PARAMETER);
575 }
576 ASSERT_GUEST_RETURN( VbgaSafe.cbPartialWriteThreshold < VbgaSafe.cbData
577 && VbgaSafe.cbPartialWriteThreshold != 0,
578 VERR_INVALID_PARAMETER);
579 RT_UNTRUSTED_VALIDATED_FENCE();
580
581 /*
582 * Okay, try do the job.
583 */
584 int rc;
585 if (pThisCC->pDrv->pfnVBVAEnable)
586 {
587 pVBVA->hostFlags.u32HostEvents = 0;
588 pVBVA->hostFlags.u32SupportedOrders = 0;
589 rc = pThisCC->pDrv->pfnVBVAEnable(pThisCC->pDrv, uScreenId, &pVBVA->hostFlags);
590 if (RT_SUCCESS(rc))
591 {
592 /* pVBVA->hostFlags has been set up by pfnVBVAEnable. */
593 LogFlowFunc(("u32HostEvents=0x%08x u32SupportedOrders=0x%08x\n",
594 pVBVA->hostFlags.u32HostEvents, pVBVA->hostFlags.u32SupportedOrders));
595
596 VBVADATA *pVBVAData = &pCtx->aViews[uScreenId].vbva;
597 pVBVAData->guest.pVBVA = pVBVA;
598 pVBVAData->guest.pu8Data = &pVBVA->au8Data[0];
599 pVBVAData->u32VBVAOffset = u32Offset;
600 pVBVAData->off32Data = VbgaSafe.off32Data;
601 pVBVAData->indexRecordFirst = VbgaSafe.indexRecordFirst;
602 pVBVAData->cbPartialWriteThreshold = VbgaSafe.cbPartialWriteThreshold;
603 pVBVAData->cbData = VbgaSafe.cbData;
604
605 if (!fRestored)
606 {
607 /** @todo Actually this function must not touch the partialRecord structure at all,
608 * because initially it is a zero and when VBVA is disabled this should be set to zero.
609 * But I'm not sure that no code depends on zeroing partialRecord here.
610 * So for now (a quick fix for 4.1) just do not do this if the VM was restored,
611 * when partialRecord might be loaded already from the saved state.
612 */
613 pVBVAData->partialRecord.pu8 = NULL;
614 pVBVAData->partialRecord.cb = 0;
615 }
616
617 /* VBVA is working so disable the pause. */
618 pCtx->fPaused = false;
619 }
620 }
621 else
622 rc = VERR_NOT_SUPPORTED;
623 return rc;
624}
625
626static int vbvaDisable(PVGASTATE pThis, PVGASTATECC pThisCC, VBVACONTEXT *pCtx, unsigned idScreen)
627{
628 /* Process any pending orders and empty the VBVA ring buffer. */
629 vbvaFlush(pThis, pThisCC, pCtx);
630
631 AssertReturn(idScreen < RT_ELEMENTS(pCtx->aViews), VERR_OUT_OF_RANGE);
632 VBVADATA *pVBVAData = &pCtx->aViews[idScreen].vbva;
633 vbvaDataCleanup(pVBVAData);
634
635 if (idScreen == 0)
636 {
637 pThis->fGuestCaps = 0;
638 pThisCC->pDrv->pfnVBVAGuestCapabilityUpdate(pThisCC->pDrv, pThis->fGuestCaps);
639 }
640 pThisCC->pDrv->pfnVBVADisable(pThisCC->pDrv, idScreen);
641 return VINF_SUCCESS;
642}
643
644#ifdef UNUSED_FUNCTION
645bool VBVAIsEnabled(PVGASTATECC pThisCC)
646{
647 PHGSMIINSTANCE pHGSMI = pThisCC->pHGSMI;
648 if (pHGSMI)
649 {
650 VBVACONTEXT *pCtx = (VBVACONTEXT *)HGSMIContext(pHGSMI);
651 if (pCtx)
652 {
653 if (pCtx->cViews)
654 {
655 VBVAVIEW * pView = &pCtx->aViews[0];
656 if (pView->vbva.guest.pVBVA)
657 return true;
658 }
659 }
660 }
661 return false;
662}
663#endif
664
665#ifdef DEBUG_sunlover
666void dumpMouseShapeInfo(const VBVAMOUSESHAPEINFO *pMouseShapeInfo)
667{
668 LogFlow(("fSet = %d, fVisible %d, fAlpha %d, @%d,%d %dx%d (%p, %d/%d)\n",
669 pMouseShapeInfo->fSet,
670 pMouseShapeInfo->fVisible,
671 pMouseShapeInfo->fAlpha,
672 pMouseShapeInfo->u32HotX,
673 pMouseShapeInfo->u32HotY,
674 pMouseShapeInfo->u32Width,
675 pMouseShapeInfo->u32Height,
676 pMouseShapeInfo->pu8Shape,
677 pMouseShapeInfo->cbShape,
678 pMouseShapeInfo->cbAllocated
679 ));
680}
681#endif
682
683static int vbvaUpdateMousePointerShape(PVGASTATECC pThisCC, VBVAMOUSESHAPEINFO *pMouseShapeInfo, bool fShape)
684{
685 LogFlowFunc(("pThisCC %p, pMouseShapeInfo %p, fShape %d\n", pThisCC, pMouseShapeInfo, fShape));
686#ifdef DEBUG_sunlover
687 dumpMouseShapeInfo(pMouseShapeInfo);
688#endif
689
690 if (pThisCC->pDrv->pfnVBVAMousePointerShape == NULL)
691 return VERR_NOT_SUPPORTED;
692
693 int rc;
694 if (fShape && pMouseShapeInfo->pu8Shape != NULL)
695 rc = pThisCC->pDrv->pfnVBVAMousePointerShape(pThisCC->pDrv,
696 pMouseShapeInfo->fVisible,
697 pMouseShapeInfo->fAlpha,
698 pMouseShapeInfo->u32HotX,
699 pMouseShapeInfo->u32HotY,
700 pMouseShapeInfo->u32Width,
701 pMouseShapeInfo->u32Height,
702 pMouseShapeInfo->pu8Shape);
703 else
704 rc = pThisCC->pDrv->pfnVBVAMousePointerShape(pThisCC->pDrv,
705 pMouseShapeInfo->fVisible,
706 false,
707 0, 0,
708 0, 0,
709 NULL);
710 return rc;
711}
712
713static int vbvaMousePointerShape(PVGASTATECC pThisCC, VBVACONTEXT *pCtx,
714 const VBVAMOUSEPOINTERSHAPE RT_UNTRUSTED_VOLATILE_GUEST *pShape, HGSMISIZE cbShape)
715{
716 /*
717 * Make non-volatile copy of the shape header and validate it.
718 */
719 VBVAMOUSEPOINTERSHAPE SafeShape;
720 RT_COPY_VOLATILE(SafeShape, *pShape);
721 RT_UNTRUSTED_NONVOLATILE_COPY_FENCE();
722
723 LogFlowFunc(("VBVA_MOUSE_POINTER_SHAPE: i32Result 0x%x, fu32Flags 0x%x, hot spot %d,%d, size %dx%d\n",
724 SafeShape.i32Result, SafeShape.fu32Flags, SafeShape.u32HotX, SafeShape.u32HotY, SafeShape.u32Width, SafeShape.u32Height));
725
726 const bool fVisible = RT_BOOL(SafeShape.fu32Flags & VBOX_MOUSE_POINTER_VISIBLE);
727 const bool fAlpha = RT_BOOL(SafeShape.fu32Flags & VBOX_MOUSE_POINTER_ALPHA);
728 const bool fShape = RT_BOOL(SafeShape.fu32Flags & VBOX_MOUSE_POINTER_SHAPE);
729
730 HGSMISIZE cbPointerData = 0;
731 if (fShape)
732 {
733 static const uint32_t s_cxMax = 2048; //used to be: 8192;
734 static const uint32_t s_cyMax = 2048; //used to be: 8192;
735 ASSERT_GUEST_MSG_RETURN( SafeShape.u32Width <= s_cxMax
736 || SafeShape.u32Height <= s_cyMax,
737 ("Too large: %ux%u, max %ux%x\n", SafeShape.u32Width, SafeShape.u32Height, s_cxMax, s_cyMax),
738 VERR_INVALID_PARAMETER);
739
740 cbPointerData = ((((SafeShape.u32Width + 7) / 8) * SafeShape.u32Height + 3) & ~3)
741 + SafeShape.u32Width * 4 * SafeShape.u32Height;
742
743 ASSERT_GUEST_MSG_RETURN(cbPointerData <= cbShape - RT_UOFFSETOF(VBVAMOUSEPOINTERSHAPE, au8Data),
744 ("Insufficent pointer data: Expected %#x, got %#x\n",
745 cbPointerData, cbShape - RT_UOFFSETOF(VBVAMOUSEPOINTERSHAPE, au8Data) ),
746 VERR_INVALID_PARAMETER);
747 }
748 RT_UNTRUSTED_VALIDATED_FENCE();
749
750 /*
751 * Do the job.
752 */
753 /* Save mouse info it will be used to restore mouse pointer after restoring saved state. */
754 pCtx->mouseShapeInfo.fSet = true;
755 pCtx->mouseShapeInfo.fVisible = fVisible;
756 if (fShape)
757 {
758 /* Data related to shape. */
759 pCtx->mouseShapeInfo.u32HotX = SafeShape.u32HotX;
760 pCtx->mouseShapeInfo.u32HotY = SafeShape.u32HotY;
761 pCtx->mouseShapeInfo.u32Width = SafeShape.u32Width;
762 pCtx->mouseShapeInfo.u32Height = SafeShape.u32Height;
763 pCtx->mouseShapeInfo.fAlpha = fAlpha;
764
765 /* Reallocate memory buffer if necessary. */
766 if (cbPointerData > pCtx->mouseShapeInfo.cbAllocated)
767 {
768 RTMemFreeZ(pCtx->mouseShapeInfo.pu8Shape, pCtx->mouseShapeInfo.cbAllocated);
769 pCtx->mouseShapeInfo.pu8Shape = NULL;
770 pCtx->mouseShapeInfo.cbShape = 0;
771
772 uint8_t *pu8Shape = (uint8_t *)RTMemAlloc(cbPointerData);
773 if (pu8Shape)
774 {
775 pCtx->mouseShapeInfo.pu8Shape = pu8Shape;
776 pCtx->mouseShapeInfo.cbAllocated = cbPointerData;
777 }
778 }
779
780 /* Copy shape bitmaps. */
781 if (pCtx->mouseShapeInfo.pu8Shape)
782 {
783 RT_BCOPY_VOLATILE(pCtx->mouseShapeInfo.pu8Shape, &pShape->au8Data[0], cbPointerData);
784 pCtx->mouseShapeInfo.cbShape = cbPointerData;
785 }
786 }
787
788 return vbvaUpdateMousePointerShape(pThisCC, &pCtx->mouseShapeInfo, fShape);
789}
790
791static uint32_t vbvaViewFromBufferPtr(PHGSMIINSTANCE pIns, const VBVACONTEXT *pCtx,
792 const void RT_UNTRUSTED_VOLATILE_GUEST *pvBuffer)
793{
794 /* Check which view contains the buffer. */
795 HGSMIOFFSET offBuffer = HGSMIPointerToOffsetHost(pIns, pvBuffer);
796 if (offBuffer != HGSMIOFFSET_VOID)
797 {
798 unsigned uScreenId;
799 for (uScreenId = 0; uScreenId < pCtx->cViews; uScreenId++)
800 {
801 const VBVAINFOVIEW *pView = &pCtx->aViews[uScreenId].view;
802 if ((uint32_t)(offBuffer - pView->u32ViewOffset) < pView->u32ViewSize)
803 return pView->u32ViewIndex;
804 }
805 }
806 return UINT32_MAX;
807}
808
809#ifdef DEBUG_sunlover
810static void dumpctx(const VBVACONTEXT *pCtx)
811{
812 Log(("VBVACONTEXT dump: cViews %d\n", pCtx->cViews));
813
814 uint32_t iView;
815 for (iView = 0; iView < pCtx->cViews; iView++)
816 {
817 const VBVAVIEW *pView = &pCtx->aViews[iView];
818
819 Log((" view %d o 0x%x s 0x%x m 0x%x\n",
820 pView->view.u32ViewIndex,
821 pView->view.u32ViewOffset,
822 pView->view.u32ViewSize,
823 pView->view.u32MaxScreenSize));
824
825 Log((" screen %d @%d,%d s 0x%x l 0x%x %dx%d bpp %d f 0x%x\n",
826 pView->screen.u32ViewIndex,
827 pView->screen.i32OriginX,
828 pView->screen.i32OriginY,
829 pView->screen.u32StartOffset,
830 pView->screen.u32LineSize,
831 pView->screen.u32Width,
832 pView->screen.u32Height,
833 pView->screen.u16BitsPerPixel,
834 pView->screen.u16Flags));
835
836 Log((" VBVA o 0x%x p %p\n",
837 pView->vbva.u32VBVAOffset,
838 pView->vbva.guest.pVBVA));
839
840 Log((" PR cb 0x%x p %p\n",
841 pView->vbva.partialRecord.cb,
842 pView->vbva.partialRecord.pu8));
843 }
844
845 dumpMouseShapeInfo(&pCtx->mouseShapeInfo);
846}
847#endif /* DEBUG_sunlover */
848
849#define VBOXVBVASAVEDSTATE_VHWAAVAILABLE_MAGIC 0x12345678
850#define VBOXVBVASAVEDSTATE_VHWAUNAVAILABLE_MAGIC 0x9abcdef0
851
852#ifdef VBOX_WITH_VIDEOHWACCEL
853
854static void vbvaVHWAHHCommandReinit(VBOXVHWACMD* pHdr, VBOXVHWACMD_TYPE enmCmd, int32_t iDisplay)
855{
856 memset(pHdr, 0, VBOXVHWACMD_HEADSIZE());
857 pHdr->cRefs = 1;
858 pHdr->iDisplay = iDisplay;
859 pHdr->rc = VERR_NOT_IMPLEMENTED;
860 pHdr->enmCmd = enmCmd;
861 pHdr->Flags = VBOXVHWACMD_FLAG_HH_CMD;
862}
863
864static VBOXVHWACMD *vbvaVHWAHHCommandCreate(VBOXVHWACMD_TYPE enmCmd, int32_t iDisplay, VBOXVHWACMD_LENGTH cbCmd)
865{
866 VBOXVHWACMD *pHdr = (VBOXVHWACMD *)RTMemAllocZ(cbCmd + VBOXVHWACMD_HEADSIZE());
867 Assert(pHdr);
868 if (pHdr)
869 vbvaVHWAHHCommandReinit(pHdr, enmCmd, iDisplay);
870
871 return pHdr;
872}
873
874DECLINLINE(void) vbvaVHWAHHCommandRelease(VBOXVHWACMD *pCmd)
875{
876 uint32_t cRefs = ASMAtomicDecU32(&pCmd->cRefs);
877 if (!cRefs)
878 RTMemFree(pCmd);
879}
880
881DECLINLINE(void) vbvaVHWAHHCommandRetain(VBOXVHWACMD *pCmd)
882{
883 ASMAtomicIncU32(&pCmd->cRefs);
884}
885
886static void vbvaVHWACommandComplete(PVGASTATECC pThisCC, VBOXVHWACMD RT_UNTRUSTED_VOLATILE_GUEST *pCommand, bool fAsyncCommand)
887{
888 if (fAsyncCommand)
889 {
890 Assert(pCommand->Flags & VBOXVHWACMD_FLAG_HG_ASYNCH);
891 vbvaR3VHWACommandCompleteAsync(&pThisCC->IVBVACallbacks, pCommand);
892 }
893 else
894 {
895 Log(("VGA Command <<< Sync rc %d %#p, %d\n", pCommand->rc, pCommand, pCommand->enmCmd));
896 pCommand->Flags &= ~VBOXVHWACMD_FLAG_HG_ASYNCH;
897 }
898
899}
900
901static void vbvaVHWACommandCompleteAllPending(PPDMDEVINS pDevIns, PVGASTATE pThis, PVGASTATECC pThisCC, int rc)
902{
903 if (!ASMAtomicUoReadU32(&pThis->pendingVhwaCommands.cPending))
904 return;
905
906 int const rcLock = PDMDevHlpCritSectEnter(pDevIns, &pThis->CritSect, VERR_SEM_BUSY);
907 PDM_CRITSECT_RELEASE_ASSERT_RC_DEV(pDevIns, &pThis->CritSect, rcLock);
908
909 VBOX_VHWA_PENDINGCMD *pIter, *pNext;
910 RTListForEachSafe(&pThis->pendingVhwaCommands.PendingList, pIter, pNext, VBOX_VHWA_PENDINGCMD, Node)
911 {
912 pIter->pCommand->rc = rc;
913 vbvaVHWACommandComplete(pThisCC, pIter->pCommand, true);
914
915 /* the command is submitted/processed, remove from the pend list */
916 RTListNodeRemove(&pIter->Node);
917 ASMAtomicDecU32(&pThis->pendingVhwaCommands.cPending);
918 RTMemFree(pIter);
919 }
920
921 PDMDevHlpCritSectLeave(pDevIns, &pThis->CritSect);
922}
923
924static void vbvaVHWACommandClearAllPending(PPDMDEVINS pDevIns, PVGASTATE pThis)
925{
926 if (!ASMAtomicUoReadU32(&pThis->pendingVhwaCommands.cPending))
927 return;
928
929 int const rcLock = PDMDevHlpCritSectEnter(pDevIns, &pThis->CritSect, VERR_SEM_BUSY);
930 PDM_CRITSECT_RELEASE_ASSERT_RC_DEV(pDevIns, &pThis->CritSect, rcLock);
931
932 VBOX_VHWA_PENDINGCMD *pIter, *pNext;
933 RTListForEachSafe(&pThis->pendingVhwaCommands.PendingList, pIter, pNext, VBOX_VHWA_PENDINGCMD, Node)
934 {
935 RTListNodeRemove(&pIter->Node);
936 ASMAtomicDecU32(&pThis->pendingVhwaCommands.cPending);
937 RTMemFree(pIter);
938 }
939
940 PDMDevHlpCritSectLeave(pDevIns, &pThis->CritSect);
941}
942
943static void vbvaVHWACommandPend(PPDMDEVINS pDevIns, PVGASTATE pThis, PVGASTATECC pThisCC,
944 VBOXVHWACMD RT_UNTRUSTED_VOLATILE_GUEST *pCommand)
945{
946 int rc = VERR_BUFFER_OVERFLOW;
947
948 if (ASMAtomicUoReadU32(&pThis->pendingVhwaCommands.cPending) < VBOX_VHWA_MAX_PENDING_COMMANDS)
949 {
950 VBOX_VHWA_PENDINGCMD *pPend = (VBOX_VHWA_PENDINGCMD *)RTMemAlloc(sizeof(*pPend));
951 if (pPend)
952 {
953 pCommand->Flags |= VBOXVHWACMD_FLAG_HG_ASYNCH;
954 pPend->pCommand = pCommand;
955
956 int const rcLock = PDMDevHlpCritSectEnter(pDevIns, &pThis->CritSect, VERR_SEM_BUSY);
957 PDM_CRITSECT_RELEASE_ASSERT_RC_DEV(pDevIns, &pThis->CritSect, rcLock);
958
959 if (ASMAtomicUoReadU32(&pThis->pendingVhwaCommands.cPending) < VBOX_VHWA_MAX_PENDING_COMMANDS)
960 {
961 RTListAppend(&pThis->pendingVhwaCommands.PendingList, &pPend->Node);
962 ASMAtomicIncU32(&pThis->pendingVhwaCommands.cPending);
963 PDMDevHlpCritSectLeave(pDevIns, &pThis->CritSect);
964 return;
965 }
966 PDMDevHlpCritSectLeave(pDevIns, &pThis->CritSect);
967 LogRel(("VBVA: Pending command count has reached its threshold.. completing them all.."));
968 RTMemFree(pPend);
969 }
970 else
971 rc = VERR_NO_MEMORY;
972 }
973 else
974 LogRel(("VBVA: Pending command count has reached its threshold, completing them all.."));
975
976 vbvaVHWACommandCompleteAllPending(pDevIns, pThis, pThisCC, rc);
977
978 pCommand->rc = rc;
979
980 vbvaVHWACommandComplete(pThisCC, pCommand, false);
981}
982
983static bool vbvaVHWACommandCanPend(VBOXVHWACMD_TYPE enmCmd)
984{
985 switch (enmCmd)
986 {
987 case VBOXVHWACMD_TYPE_HH_CONSTRUCT:
988 case VBOXVHWACMD_TYPE_HH_SAVESTATE_SAVEBEGIN:
989 case VBOXVHWACMD_TYPE_HH_SAVESTATE_SAVEEND:
990 case VBOXVHWACMD_TYPE_HH_SAVESTATE_SAVEPERFORM:
991 case VBOXVHWACMD_TYPE_HH_SAVESTATE_LOADPERFORM:
992 return false;
993 default:
994 return true;
995 }
996}
997
998static int vbvaVHWACommandSavePending(PCPDMDEVHLPR3 pHlp, PVGASTATE pThis, PVGASTATECC pThisCC, PSSMHANDLE pSSM)
999{
1000 int rc = pHlp->pfnSSMPutU32(pSSM, pThis->pendingVhwaCommands.cPending);
1001 AssertRCReturn(rc, rc);
1002
1003 VBOX_VHWA_PENDINGCMD *pIter;
1004 RTListForEach(&pThis->pendingVhwaCommands.PendingList, pIter, VBOX_VHWA_PENDINGCMD, Node)
1005 {
1006 AssertContinue((uintptr_t)pIter->pCommand - (uintptr_t)pThisCC->pbVRam < pThis->vram_size);
1007 rc = pHlp->pfnSSMPutU32(pSSM, (uint32_t)(((uint8_t *)pIter->pCommand) - ((uint8_t *)pThisCC->pbVRam)));
1008 AssertRCReturn(rc, rc);
1009 }
1010 return rc;
1011}
1012
1013static int vbvaVHWACommandLoadPending(PPDMDEVINS pDevIns, PCPDMDEVHLPR3 pHlp, PVGASTATE pThis, PVGASTATECC pThisCC,
1014 PSSMHANDLE pSSM, uint32_t u32Version)
1015{
1016 if (u32Version < VGA_SAVEDSTATE_VERSION_WITH_PENDVHWA)
1017 return VINF_SUCCESS;
1018
1019 uint32_t u32;
1020 int rc = pHlp->pfnSSMGetU32(pSSM, &u32);
1021 AssertRCReturn(rc, rc);
1022 for (uint32_t i = 0; i < u32; ++i)
1023 {
1024 uint32_t off32;
1025 rc = pHlp->pfnSSMGetU32(pSSM, &off32);
1026 AssertRCReturn(rc, rc);
1027 VBOXVHWACMD RT_UNTRUSTED_VOLATILE_GUEST *pCommand
1028 = (VBOXVHWACMD RT_UNTRUSTED_VOLATILE_GUEST *)((uint8_t volatile *)pThisCC->pbVRam + off32);
1029 vbvaVHWACommandPend(pDevIns, pThis, pThisCC, pCommand);
1030 }
1031 return rc;
1032}
1033
1034
1035/** Worker for vbvaVHWACommandSubmit. */
1036static bool vbvaVHWACommandSubmitInner(PVGASTATE pThis, PVGASTATECC pThisCC,
1037 VBOXVHWACMD RT_UNTRUSTED_VOLATILE_GUEST *pCommand, bool *pfPending)
1038{
1039 *pfPending = false;
1040
1041 /*
1042 * Read the command type and validate it and our driver state.
1043 */
1044 VBOXVHWACMD_TYPE enmCmd = pCommand->enmCmd;
1045 RT_UNTRUSTED_NONVOLATILE_COPY_FENCE();
1046
1047 bool fGuestCmd = (uintptr_t)pCommand - (uintptr_t)pThisCC->pbVRam < pThis->vram_size;
1048 ASSERT_GUEST_LOGREL_MSG_STMT_RETURN( !fGuestCmd
1049 || ( enmCmd != VBOXVHWACMD_TYPE_HH_CONSTRUCT
1050 && enmCmd != VBOXVHWACMD_TYPE_HH_RESET
1051 && enmCmd != VBOXVHWACMD_TYPE_HH_DISABLE
1052 && enmCmd != VBOXVHWACMD_TYPE_HH_ENABLE
1053 && enmCmd != VBOXVHWACMD_TYPE_HH_SAVESTATE_SAVEBEGIN
1054 && enmCmd != VBOXVHWACMD_TYPE_HH_SAVESTATE_SAVEEND
1055 && enmCmd != VBOXVHWACMD_TYPE_HH_SAVESTATE_SAVEPERFORM
1056 && enmCmd != VBOXVHWACMD_TYPE_HH_SAVESTATE_LOADPERFORM),
1057 ("enmCmd=%d\n", enmCmd),
1058 pCommand->rc = VERR_INVALID_PARAMETER,
1059 true);
1060 ASSERT_GUEST_STMT_RETURN(pThisCC->pDrv->pfnVHWACommandProcess, pCommand->rc = VERR_INVALID_STATE, true);
1061 RT_UNTRUSTED_VALIDATED_FENCE();
1062
1063 /*
1064 * Call the driver to process the command.
1065 */
1066 Log(("VGA Command >>> %#p, %d\n", pCommand, enmCmd));
1067 int rc = pThisCC->pDrv->pfnVHWACommandProcess(pThisCC->pDrv, enmCmd, fGuestCmd, pCommand);
1068 if (rc == VINF_CALLBACK_RETURN)
1069 {
1070 Log(("VGA Command --- Going Async %#p, %d\n", pCommand, enmCmd));
1071 *pfPending = true;
1072 return true; /* Command will be completed asynchronously by the driver and need not be put in the pending list. */
1073 }
1074
1075 if (rc == VERR_INVALID_STATE)
1076 {
1077 Log(("VGA Command --- Trying Pend %#p, %d\n", pCommand, enmCmd));
1078 if (vbvaVHWACommandCanPend(enmCmd))
1079 {
1080 Log(("VGA Command --- Can Pend %#p, %d\n", pCommand, enmCmd));
1081 *pfPending = true;
1082 return false; /* put on pending list so it can be retried?? */
1083 }
1084
1085 Log(("VGA Command --- Can NOT Pend %#p, %d\n", pCommand, enmCmd));
1086 }
1087 else
1088 Log(("VGA Command --- Going Complete Sync rc %d %#p, %d\n", rc, pCommand, enmCmd));
1089
1090 /* the command was completed, take a special care about it (see caller) */
1091 pCommand->rc = rc;
1092 return true;
1093}
1094
1095
1096static bool vbvaVHWACommandSubmit(PVGASTATE pThis, PVGASTATECC pThisCC,
1097 VBOXVHWACMD RT_UNTRUSTED_VOLATILE_GUEST *pCommand, bool fAsyncCommand)
1098{
1099 bool fPending = false;
1100 bool fRet = vbvaVHWACommandSubmitInner(pThis, pThisCC, pCommand, &fPending);
1101 if (!fPending)
1102 vbvaVHWACommandComplete(pThisCC, pCommand, fAsyncCommand);
1103 return fRet;
1104}
1105
1106
1107/**
1108 * @returns false if commands are pending, otherwise true.
1109 */
1110static bool vbvaVHWACheckPendingCommands(PPDMDEVINS pDevIns, PVGASTATE pThis, PVGASTATECC pThisCC)
1111{
1112 if (!ASMAtomicUoReadU32(&pThis->pendingVhwaCommands.cPending))
1113 return true;
1114
1115 int const rcLock = PDMDevHlpCritSectEnter(pDevIns, &pThis->CritSect, VERR_SEM_BUSY);
1116 PDM_CRITSECT_RELEASE_ASSERT_RC_DEV(pDevIns, &pThis->CritSect, rcLock);
1117
1118 VBOX_VHWA_PENDINGCMD *pIter, *pNext;
1119 RTListForEachSafe(&pThis->pendingVhwaCommands.PendingList, pIter, pNext, VBOX_VHWA_PENDINGCMD, Node)
1120 {
1121 if (!vbvaVHWACommandSubmit(pThis, pThisCC, pIter->pCommand, true))
1122 {
1123 PDMDevHlpCritSectLeave(pDevIns, &pThis->CritSect);
1124 return false; /* the command should be still pending */
1125 }
1126
1127 /* the command is submitted/processed, remove from the pend list */
1128 RTListNodeRemove(&pIter->Node);
1129 ASMAtomicDecU32(&pThis->pendingVhwaCommands.cPending);
1130 RTMemFree(pIter);
1131 }
1132
1133 PDMDevHlpCritSectLeave(pDevIns, &pThis->CritSect);
1134
1135 return true;
1136}
1137
1138void vbvaTimerCb(PPDMDEVINS pDevIns, PVGASTATE pThis, PVGASTATECC pThisCC)
1139{
1140 vbvaVHWACheckPendingCommands(pDevIns, pThis, pThisCC);
1141}
1142
1143static void vbvaVHWAHandleCommand(PPDMDEVINS pDevIns, PVGASTATE pThis, PVGASTATECC pThisCC,
1144 VBOXVHWACMD RT_UNTRUSTED_VOLATILE_GUEST *pCmd)
1145{
1146 if (vbvaVHWACheckPendingCommands(pDevIns, pThis, pThisCC))
1147 {
1148 if (vbvaVHWACommandSubmit(pThis, pThisCC, pCmd, false))
1149 return;
1150 }
1151
1152 vbvaVHWACommandPend(pDevIns, pThis, pThisCC, pCmd);
1153}
1154
1155static DECLCALLBACK(void) vbvaVHWAHHCommandSetEventCallback(void * pContext)
1156{
1157 RTSemEventSignal((RTSEMEVENT)pContext);
1158}
1159
1160static int vbvaVHWAHHCommandPost(PPDMDEVINS pDevIns, PVGASTATE pThis, PVGASTATECC pThisCC, VBOXVHWACMD *pCmd)
1161{
1162 RTSEMEVENT hComplEvent;
1163 int rc = RTSemEventCreate(&hComplEvent);
1164 AssertRC(rc);
1165 if (RT_SUCCESS(rc))
1166 {
1167 /* ensure the cmd is not deleted until we process it */
1168 vbvaVHWAHHCommandRetain(pCmd);
1169
1170 VBOXVHWA_HH_CALLBACK_SET(pCmd, vbvaVHWAHHCommandSetEventCallback, (void *)hComplEvent);
1171 vbvaVHWAHandleCommand(pDevIns, pThis, pThisCC, pCmd);
1172
1173 if ((ASMAtomicReadU32((volatile uint32_t *)&pCmd->Flags) & VBOXVHWACMD_FLAG_HG_ASYNCH) != 0)
1174 rc = RTSemEventWaitNoResume(hComplEvent, RT_INDEFINITE_WAIT); /** @todo Why the NoResume and event leaking here? */
1175 /* else: the command is completed */
1176
1177 AssertRC(rc);
1178 if (RT_SUCCESS(rc))
1179 RTSemEventDestroy(hComplEvent);
1180
1181 vbvaVHWAHHCommandRelease(pCmd);
1182 }
1183 return rc;
1184}
1185
1186int vbvaVHWAConstruct(PPDMDEVINS pDevIns, PVGASTATE pThis, PVGASTATECC pThisCC)
1187{
1188 pThis->pendingVhwaCommands.cPending = 0;
1189 RTListInit(&pThis->pendingVhwaCommands.PendingList);
1190
1191 VBOXVHWACMD *pCmd = vbvaVHWAHHCommandCreate(VBOXVHWACMD_TYPE_HH_CONSTRUCT, 0, sizeof(VBOXVHWACMD_HH_CONSTRUCT));
1192 Assert(pCmd);
1193 if(pCmd)
1194 {
1195 uint32_t iDisplay = 0;
1196 int rc = VINF_SUCCESS;
1197 VBOXVHWACMD_HH_CONSTRUCT *pBody = VBOXVHWACMD_BODY_HOST_HEAP(pCmd, VBOXVHWACMD_HH_CONSTRUCT);
1198
1199 for (;;)
1200 {
1201 memset(pBody, 0, sizeof(VBOXVHWACMD_HH_CONSTRUCT));
1202
1203 PVM pVM = PDMDevHlpGetVM(pDevIns);
1204
1205 pBody->pVM = pVM;
1206 pBody->pvVRAM = pThisCC->pbVRam;
1207 pBody->cbVRAM = pThis->vram_size;
1208
1209 rc = vbvaVHWAHHCommandPost(pDevIns, pThis, pThisCC, pCmd);
1210 ASMCompilerBarrier();
1211
1212 AssertRC(rc);
1213 if (RT_SUCCESS(rc))
1214 {
1215 rc = pCmd->rc;
1216 AssertMsg(RT_SUCCESS(rc) || rc == VERR_NOT_IMPLEMENTED, ("%Rrc\n", rc));
1217 if(rc == VERR_NOT_IMPLEMENTED)
1218 {
1219 /** @todo set some flag in pThis indicating VHWA is not supported */
1220 /* VERR_NOT_IMPLEMENTED is not a failure, we just do not support it */
1221 rc = VINF_SUCCESS;
1222 }
1223
1224 if (!RT_SUCCESS(rc))
1225 break;
1226 }
1227 else
1228 break;
1229
1230 ++iDisplay;
1231 if (iDisplay >= pThis->cMonitors)
1232 break;
1233 vbvaVHWAHHCommandReinit(pCmd, VBOXVHWACMD_TYPE_HH_CONSTRUCT, (int32_t)iDisplay);
1234 }
1235
1236 vbvaVHWAHHCommandRelease(pCmd);
1237
1238 return rc;
1239 }
1240 return VERR_OUT_OF_RESOURCES;
1241}
1242
1243static int vbvaVHWAReset(PPDMDEVINS pDevIns, PVGASTATE pThis, PVGASTATECC pThisCC)
1244{
1245 vbvaVHWACommandClearAllPending(pDevIns, pThis);
1246
1247 /* ensure we have all pending cmds processed and h->g cmds disabled */
1248 VBOXVHWACMD *pCmd = vbvaVHWAHHCommandCreate(VBOXVHWACMD_TYPE_HH_RESET, 0, 0);
1249 Assert(pCmd);
1250 if (pCmd)
1251 {
1252 int rc = VINF_SUCCESS;
1253 uint32_t iDisplay = 0;
1254
1255 do
1256 {
1257 rc = vbvaVHWAHHCommandPost(pDevIns, pThis, pThisCC, pCmd);
1258 AssertRC(rc);
1259 if(RT_SUCCESS(rc))
1260 {
1261 rc = pCmd->rc;
1262 AssertMsg(RT_SUCCESS(rc) || rc == VERR_NOT_IMPLEMENTED, ("%Rrc\n", rc));
1263 if (rc == VERR_NOT_IMPLEMENTED)
1264 rc = VINF_SUCCESS;
1265 }
1266
1267 if (!RT_SUCCESS(rc))
1268 break;
1269
1270 ++iDisplay;
1271 if (iDisplay >= pThis->cMonitors)
1272 break;
1273 vbvaVHWAHHCommandReinit(pCmd, VBOXVHWACMD_TYPE_HH_RESET, (int32_t)iDisplay);
1274
1275 } while (true);
1276
1277 vbvaVHWAHHCommandRelease(pCmd);
1278
1279 return rc;
1280 }
1281 return VERR_OUT_OF_RESOURCES;
1282}
1283
1284typedef DECLCALLBACKTYPE(bool, FNVBOXVHWAHHCMDPRECB,(PPDMDEVINS pDevIns, PVGASTATE pThis, PVGASTATECC pThisCC, VBOXVHWACMD *pCmd,
1285 uint32_t iDisplay, void *pvContext));
1286typedef FNVBOXVHWAHHCMDPRECB *PFNVBOXVHWAHHCMDPRECB;
1287
1288typedef DECLCALLBACKTYPE(bool, FNVBOXVHWAHHCMDPOSTCB,(PPDMDEVINS pDevIns, PVGASTATE pThis, PVGASTATECC pThisCC, VBOXVHWACMD *pCmd,
1289 uint32_t iDisplay, int rc, void *pvContext));
1290typedef FNVBOXVHWAHHCMDPOSTCB *PFNVBOXVHWAHHCMDPOSTCB;
1291
1292static int vbvaVHWAHHPost(PPDMDEVINS pDevIns, PVGASTATE pThis, PVGASTATECC pThisCC, VBOXVHWACMD *pCmd,
1293 PFNVBOXVHWAHHCMDPRECB pfnPre, PFNVBOXVHWAHHCMDPOSTCB pfnPost, void *pvContext)
1294{
1295 const VBOXVHWACMD_TYPE enmType = pCmd->enmCmd;
1296 int rc = VINF_SUCCESS;
1297 uint32_t iDisplay = 0;
1298
1299 do
1300 {
1301 if (!pfnPre || pfnPre(pDevIns, pThis, pThisCC, pCmd, iDisplay, pvContext))
1302 {
1303 rc = vbvaVHWAHHCommandPost(pDevIns, pThis, pThisCC, pCmd);
1304 AssertRC(rc);
1305 if (pfnPost)
1306 {
1307 if (!pfnPost(pDevIns, pThis, pThisCC, pCmd, iDisplay, rc, pvContext))
1308 {
1309 rc = VINF_SUCCESS;
1310 break;
1311 }
1312 rc = VINF_SUCCESS;
1313 }
1314 else if(RT_SUCCESS(rc))
1315 {
1316 rc = pCmd->rc;
1317 AssertMsg(RT_SUCCESS(rc) || rc == VERR_NOT_IMPLEMENTED, ("%Rrc\n", rc));
1318 if(rc == VERR_NOT_IMPLEMENTED)
1319 {
1320 rc = VINF_SUCCESS;
1321 }
1322 }
1323
1324 if (!RT_SUCCESS(rc))
1325 break;
1326 }
1327
1328 ++iDisplay;
1329 if (iDisplay >= pThis->cMonitors)
1330 break;
1331 vbvaVHWAHHCommandReinit(pCmd, enmType, (int32_t)iDisplay);
1332 } while (true);
1333
1334 return rc;
1335}
1336
1337/** @todo call this also on reset? */
1338static int vbvaVHWAEnable(PPDMDEVINS pDevIns, PVGASTATE pThis, PVGASTATECC pThisCC, bool bEnable)
1339{
1340 const VBOXVHWACMD_TYPE enmType = bEnable ? VBOXVHWACMD_TYPE_HH_ENABLE : VBOXVHWACMD_TYPE_HH_DISABLE;
1341 VBOXVHWACMD *pCmd = vbvaVHWAHHCommandCreate(enmType, 0, 0);
1342 Assert(pCmd);
1343 if(pCmd)
1344 {
1345 int rc = vbvaVHWAHHPost(pDevIns, pThis, pThisCC, pCmd, NULL, NULL, NULL);
1346 vbvaVHWAHHCommandRelease(pCmd);
1347 return rc;
1348 }
1349 return VERR_OUT_OF_RESOURCES;
1350}
1351
1352int vboxVBVASaveStatePrep(PPDMDEVINS pDevIns)
1353{
1354 /* ensure we have no pending commands */
1355 return vbvaVHWAEnable(pDevIns, PDMDEVINS_2_DATA(pDevIns, PVGASTATE), PDMDEVINS_2_DATA_CC(pDevIns, PVGASTATECC), false);
1356}
1357
1358int vboxVBVASaveStateDone(PPDMDEVINS pDevIns)
1359{
1360 /* ensure we have no pending commands */
1361 return vbvaVHWAEnable(pDevIns, PDMDEVINS_2_DATA(pDevIns, PVGASTATE), PDMDEVINS_2_DATA_CC(pDevIns, PVGASTATECC), true);
1362}
1363
1364
1365/**
1366 * @interface_method_impl{PDMIDISPLAYVBVACALLBACKS,pfnVHWACommandCompleteAsync}
1367 */
1368DECLCALLBACK(int) vbvaR3VHWACommandCompleteAsync(PPDMIDISPLAYVBVACALLBACKS pInterface,
1369 VBOXVHWACMD RT_UNTRUSTED_VOLATILE_GUEST *pCmd)
1370{
1371 PVGASTATECC pThisCC = RT_FROM_MEMBER(pInterface, VGASTATECC, IVBVACallbacks);
1372 PPDMDEVINS pDevIns = pThisCC->pDevIns;
1373 PVGASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVGASTATE);
1374 int rc;
1375
1376 Log(("VGA Command <<< Async rc %d %#p, %d\n", pCmd->rc, pCmd, pCmd->enmCmd));
1377
1378 if ((uintptr_t)pCmd - (uintptr_t)pThisCC->pbVRam < pThis->vram_size)
1379 {
1380 PHGSMIINSTANCE pIns = pThisCC->pHGSMI;
1381 Assert(!(pCmd->Flags & VBOXVHWACMD_FLAG_HH_CMD));
1382 Assert(pCmd->Flags & VBOXVHWACMD_FLAG_HG_ASYNCH);
1383#ifdef VBOX_WITH_WDDM
1384 if (pThis->fGuestCaps & VBVACAPS_COMPLETEGCMD_BY_IOREAD)
1385 {
1386 rc = HGSMICompleteGuestCommand(pIns, pCmd, !!(pCmd->Flags & VBOXVHWACMD_FLAG_GH_ASYNCH_IRQ));
1387 AssertRC(rc);
1388 }
1389 else
1390#endif
1391 {
1392 VBVAHOSTCMD RT_UNTRUSTED_VOLATILE_GUEST *pHostCmd = NULL; /* Shut up MSC. */
1393 if (pCmd->Flags & VBOXVHWACMD_FLAG_GH_ASYNCH_EVENT)
1394 {
1395 rc = HGSMIHostCommandAlloc(pIns,
1396 (void RT_UNTRUSTED_VOLATILE_GUEST **)&pHostCmd,
1397 VBVAHOSTCMD_SIZE(sizeof(VBVAHOSTCMDEVENT)),
1398 HGSMI_CH_VBVA,
1399 VBVAHG_EVENT);
1400 AssertRC(rc);
1401 if (RT_SUCCESS(rc))
1402 {
1403 memset((void *)pHostCmd, 0 , VBVAHOSTCMD_SIZE(sizeof(VBVAHOSTCMDEVENT)));
1404 pHostCmd->iDstID = pCmd->iDisplay;
1405 pHostCmd->customOpCode = 0;
1406 VBVAHOSTCMDEVENT RT_UNTRUSTED_VOLATILE_GUEST *pBody = VBVAHOSTCMD_BODY(pHostCmd, VBVAHOSTCMDEVENT);
1407 pBody->pEvent = pCmd->GuestVBVAReserved1;
1408 }
1409 }
1410 else
1411 {
1412 HGSMIOFFSET offCmd = HGSMIPointerToOffsetHost(pIns, pCmd);
1413 Assert(offCmd != HGSMIOFFSET_VOID);
1414 if (offCmd != HGSMIOFFSET_VOID)
1415 {
1416 rc = HGSMIHostCommandAlloc(pIns,
1417 (void RT_UNTRUSTED_VOLATILE_GUEST **)&pHostCmd,
1418 VBVAHOSTCMD_SIZE(sizeof(VBVAHOSTCMDVHWACMDCOMPLETE)),
1419 HGSMI_CH_VBVA,
1420 VBVAHG_DISPLAY_CUSTOM);
1421 AssertRC(rc);
1422 if (RT_SUCCESS(rc))
1423 {
1424 memset((void *)pHostCmd, 0 , VBVAHOSTCMD_SIZE(sizeof(VBVAHOSTCMDVHWACMDCOMPLETE)));
1425 pHostCmd->iDstID = pCmd->iDisplay;
1426 pHostCmd->customOpCode = VBVAHG_DCUSTOM_VHWA_CMDCOMPLETE;
1427 VBVAHOSTCMDVHWACMDCOMPLETE RT_UNTRUSTED_VOLATILE_GUEST *pBody
1428 = VBVAHOSTCMD_BODY(pHostCmd, VBVAHOSTCMDVHWACMDCOMPLETE);
1429 pBody->offCmd = offCmd;
1430 }
1431 }
1432 else
1433 rc = VERR_INVALID_PARAMETER;
1434 }
1435
1436 if (RT_SUCCESS(rc))
1437 {
1438 rc = HGSMIHostCommandSubmitAndFreeAsynch(pIns, pHostCmd, RT_BOOL(pCmd->Flags & VBOXVHWACMD_FLAG_GH_ASYNCH_IRQ));
1439 AssertRC(rc);
1440 if (RT_SUCCESS(rc))
1441 return rc;
1442
1443 HGSMIHostCommandFree (pIns, pHostCmd);
1444 }
1445 }
1446 }
1447 else
1448 {
1449 Assert(pCmd->Flags & VBOXVHWACMD_FLAG_HH_CMD);
1450 PFNVBOXVHWA_HH_CALLBACK pfn = VBOXVHWA_HH_CALLBACK_GET(pCmd);
1451 if (pfn)
1452 pfn(VBOXVHWA_HH_CALLBACK_GET_ARG(pCmd));
1453 rc = VINF_SUCCESS;
1454 }
1455 return rc;
1456}
1457
1458typedef struct VBOXVBVASAVEDSTATECBDATA
1459{
1460 PSSMHANDLE pSSM;
1461 int rc;
1462 bool ab2DOn[VBOX_VIDEO_MAX_SCREENS];
1463} VBOXVBVASAVEDSTATECBDATA, *PVBOXVBVASAVEDSTATECBDATA;
1464
1465/**
1466 * @callback_method_impl{FNVBOXVHWAHHCMDPOSTCB}
1467 */
1468static DECLCALLBACK(bool) vboxVBVASaveStateBeginPostCb(PPDMDEVINS pDevIns, PVGASTATE pThis, PVGASTATECC pThisCC,
1469 VBOXVHWACMD *pCmd, uint32_t iDisplay, int rc, void *pvContext)
1470{
1471 RT_NOREF(pDevIns, pThis, pThisCC, pCmd);
1472 PVBOXVBVASAVEDSTATECBDATA pData = (PVBOXVBVASAVEDSTATECBDATA)pvContext;
1473 if (RT_FAILURE(pData->rc))
1474 return false;
1475 if (RT_FAILURE(rc))
1476 {
1477 pData->rc = rc;
1478 return false;
1479 }
1480
1481 Assert(iDisplay < RT_ELEMENTS(pData->ab2DOn));
1482 if (iDisplay >= RT_ELEMENTS(pData->ab2DOn))
1483 {
1484 pData->rc = VERR_INVALID_PARAMETER;
1485 return false;
1486 }
1487
1488 Assert(RT_SUCCESS(pCmd->rc) || pCmd->rc == VERR_NOT_IMPLEMENTED);
1489 if (RT_SUCCESS(pCmd->rc))
1490 {
1491 pData->ab2DOn[iDisplay] = true;
1492 }
1493 else if (pCmd->rc != VERR_NOT_IMPLEMENTED)
1494 {
1495 pData->rc = pCmd->rc;
1496 return false;
1497 }
1498
1499 return true;
1500}
1501
1502/**
1503 * @callback_method_impl{FNVBOXVHWAHHCMDPRECB}
1504 */
1505static DECLCALLBACK(bool) vboxVBVASaveStatePerformPreCb(PPDMDEVINS pDevIns, PVGASTATE pThis, PVGASTATECC pThisCC,
1506 VBOXVHWACMD *pCmd, uint32_t iDisplay, void *pvContext)
1507{
1508 RT_NOREF(pThis, pThisCC, pCmd);
1509 PVBOXVBVASAVEDSTATECBDATA pData = (PVBOXVBVASAVEDSTATECBDATA)pvContext;
1510 if (RT_FAILURE(pData->rc))
1511 return false;
1512
1513 Assert(iDisplay < RT_ELEMENTS(pData->ab2DOn));
1514 if (iDisplay >= RT_ELEMENTS(pData->ab2DOn))
1515 {
1516 pData->rc = VERR_INVALID_PARAMETER;
1517 return false;
1518 }
1519
1520 int rc;
1521 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
1522
1523 if (pData->ab2DOn[iDisplay])
1524 {
1525 rc = pHlp->pfnSSMPutU32(pData->pSSM, VBOXVBVASAVEDSTATE_VHWAAVAILABLE_MAGIC); AssertRC(rc);
1526 if (RT_FAILURE(rc))
1527 {
1528 pData->rc = rc;
1529 return false;
1530 }
1531 return true;
1532 }
1533
1534 rc = pHlp->pfnSSMPutU32(pData->pSSM, VBOXVBVASAVEDSTATE_VHWAUNAVAILABLE_MAGIC); AssertRC(rc);
1535 if (RT_FAILURE(rc))
1536 {
1537 pData->rc = rc;
1538 return false;
1539 }
1540
1541 return false;
1542}
1543
1544/**
1545 * @callback_method_impl{FNVBOXVHWAHHCMDPOSTCB}
1546 */
1547static DECLCALLBACK(bool) vboxVBVASaveStateEndPreCb(PPDMDEVINS pDevIns, PVGASTATE pThis, PVGASTATECC pThisCC,
1548 VBOXVHWACMD *pCmd, uint32_t iDisplay, void *pvContext)
1549{
1550 RT_NOREF(pDevIns, pThis, pThisCC, pCmd);
1551 PVBOXVBVASAVEDSTATECBDATA pData = (PVBOXVBVASAVEDSTATECBDATA)pvContext;
1552 Assert(iDisplay < RT_ELEMENTS(pData->ab2DOn));
1553 if (pData->ab2DOn[iDisplay])
1554 return true;
1555 return false;
1556}
1557
1558/**
1559 * @callback_method_impl{FNVBOXVHWAHHCMDPOSTCB}
1560 */
1561static DECLCALLBACK(bool) vboxVBVALoadStatePerformPostCb(PPDMDEVINS pDevIns, PVGASTATE pThis, PVGASTATECC pThisCC,
1562 VBOXVHWACMD *pCmd, uint32_t iDisplay, int rc, void *pvContext)
1563{
1564 RT_NOREF(pThis, pThisCC, pCmd);
1565 PVBOXVBVASAVEDSTATECBDATA pData = (PVBOXVBVASAVEDSTATECBDATA)pvContext;
1566 if (RT_FAILURE(pData->rc))
1567 return false;
1568 if (RT_FAILURE(rc))
1569 {
1570 pData->rc = rc;
1571 return false;
1572 }
1573
1574 Assert(iDisplay < RT_ELEMENTS(pData->ab2DOn));
1575 if (iDisplay >= RT_ELEMENTS(pData->ab2DOn))
1576 {
1577 pData->rc = VERR_INVALID_PARAMETER;
1578 return false;
1579 }
1580
1581 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
1582 Assert(RT_SUCCESS(pCmd->rc) || pCmd->rc == VERR_NOT_IMPLEMENTED);
1583 if (pCmd->rc == VERR_NOT_IMPLEMENTED)
1584 {
1585 pData->rc = pHlp->pfnSSMSkipToEndOfUnit(pData->pSSM);
1586 AssertRC(pData->rc);
1587 return false;
1588 }
1589 if (RT_FAILURE(pCmd->rc))
1590 {
1591 pData->rc = pCmd->rc;
1592 return false;
1593 }
1594
1595 return true;
1596}
1597
1598/**
1599 * @callback_method_impl{FNVBOXVHWAHHCMDPOSTCB}
1600 */
1601static DECLCALLBACK(bool) vboxVBVALoadStatePerformPreCb(PPDMDEVINS pDevIns, PVGASTATE pThis, PVGASTATECC pThisCC,
1602 VBOXVHWACMD *pCmd, uint32_t iDisplay, void *pvContext)
1603{
1604 RT_NOREF(pThis, pThisCC, pCmd);
1605 PVBOXVBVASAVEDSTATECBDATA pData = (PVBOXVBVASAVEDSTATECBDATA)pvContext;
1606 if (RT_FAILURE(pData->rc))
1607 return false;
1608
1609 Assert(iDisplay < RT_ELEMENTS(pData->ab2DOn));
1610 if (iDisplay >= RT_ELEMENTS(pData->ab2DOn))
1611 {
1612 pData->rc = VERR_INVALID_PARAMETER;
1613 return false;
1614 }
1615
1616 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
1617 int rc;
1618 uint32_t u32;
1619 rc = pHlp->pfnSSMGetU32(pData->pSSM, &u32); AssertRC(rc);
1620 if (RT_FAILURE(rc))
1621 {
1622 pData->rc = rc;
1623 return false;
1624 }
1625
1626 switch (u32)
1627 {
1628 case VBOXVBVASAVEDSTATE_VHWAAVAILABLE_MAGIC:
1629 pData->ab2DOn[iDisplay] = true;
1630 return true;
1631 case VBOXVBVASAVEDSTATE_VHWAUNAVAILABLE_MAGIC:
1632 pData->ab2DOn[iDisplay] = false;
1633 return false;
1634 default:
1635 pData->rc = VERR_INVALID_STATE;
1636 return false;
1637 }
1638}
1639
1640#endif /* VBOX_WITH_VIDEOHWACCEL */
1641
1642static int vboxVBVASaveDevStateExec(PCPDMDEVHLPR3 pHlp, PVGASTATE pThis, PVGASTATECC pThisCC, PSSMHANDLE pSSM)
1643{
1644 PHGSMIINSTANCE pIns = pThisCC->pHGSMI;
1645 int rc = HGSMIHostSaveStateExec(pHlp, pIns, pSSM);
1646 if (RT_SUCCESS(rc))
1647 {
1648 VGA_SAVED_STATE_PUT_MARKER(pSSM, 2);
1649
1650 /* Save VBVACONTEXT. */
1651 VBVACONTEXT *pCtx = (VBVACONTEXT *)HGSMIContext (pIns);
1652
1653 if (!pCtx)
1654 {
1655 AssertFailed();
1656
1657 /* Still write a valid value to the SSM. */
1658 rc = pHlp->pfnSSMPutU32 (pSSM, 0);
1659 AssertRCReturn(rc, rc);
1660 }
1661 else
1662 {
1663#ifdef DEBUG_sunlover
1664 dumpctx(pCtx);
1665#endif
1666
1667 rc = pHlp->pfnSSMPutU32 (pSSM, pCtx->cViews);
1668 AssertRCReturn(rc, rc);
1669
1670 uint32_t iView;
1671 for (iView = 0; iView < pCtx->cViews; iView++)
1672 {
1673 VBVAVIEW *pView = &pCtx->aViews[iView];
1674
1675 rc = pHlp->pfnSSMPutU32(pSSM, pView->view.u32ViewIndex);
1676 AssertRCReturn(rc, rc);
1677 rc = pHlp->pfnSSMPutU32(pSSM, pView->view.u32ViewOffset);
1678 AssertRCReturn(rc, rc);
1679 rc = pHlp->pfnSSMPutU32(pSSM, pView->view.u32ViewSize);
1680 AssertRCReturn(rc, rc);
1681 rc = pHlp->pfnSSMPutU32(pSSM, pView->view.u32MaxScreenSize);
1682 AssertRCReturn(rc, rc);
1683
1684 rc = pHlp->pfnSSMPutU32(pSSM, pView->screen.u32ViewIndex);
1685 AssertRCReturn(rc, rc);
1686 rc = pHlp->pfnSSMPutS32(pSSM, pView->screen.i32OriginX);
1687 AssertRCReturn(rc, rc);
1688 rc = pHlp->pfnSSMPutS32(pSSM, pView->screen.i32OriginY);
1689 AssertRCReturn(rc, rc);
1690 rc = pHlp->pfnSSMPutU32(pSSM, pView->screen.u32StartOffset);
1691 AssertRCReturn(rc, rc);
1692 rc = pHlp->pfnSSMPutU32(pSSM, pView->screen.u32LineSize);
1693 AssertRCReturn(rc, rc);
1694 rc = pHlp->pfnSSMPutU32(pSSM, pView->screen.u32Width);
1695 AssertRCReturn(rc, rc);
1696 rc = pHlp->pfnSSMPutU32(pSSM, pView->screen.u32Height);
1697 AssertRCReturn(rc, rc);
1698 rc = pHlp->pfnSSMPutU16(pSSM, pView->screen.u16BitsPerPixel);
1699 AssertRCReturn(rc, rc);
1700 rc = pHlp->pfnSSMPutU16(pSSM, pView->screen.u16Flags);
1701 AssertRCReturn(rc, rc);
1702
1703 rc = pHlp->pfnSSMPutU32(pSSM, pView->vbva.guest.pVBVA? pView->vbva.u32VBVAOffset: HGSMIOFFSET_VOID);
1704 AssertRCReturn(rc, rc);
1705
1706 rc = pHlp->pfnSSMPutU32(pSSM, pView->vbva.partialRecord.cb);
1707 AssertRCReturn(rc, rc);
1708
1709 if (pView->vbva.partialRecord.cb > 0)
1710 {
1711 rc = pHlp->pfnSSMPutMem(pSSM, pView->vbva.partialRecord.pu8, pView->vbva.partialRecord.cb);
1712 AssertRCReturn(rc, rc);
1713 }
1714 }
1715
1716 /* Save mouse pointer shape information. */
1717 rc = pHlp->pfnSSMPutBool(pSSM, pCtx->mouseShapeInfo.fSet);
1718 AssertRCReturn(rc, rc);
1719 rc = pHlp->pfnSSMPutBool(pSSM, pCtx->mouseShapeInfo.fVisible);
1720 AssertRCReturn(rc, rc);
1721 rc = pHlp->pfnSSMPutBool(pSSM, pCtx->mouseShapeInfo.fAlpha);
1722 AssertRCReturn(rc, rc);
1723 rc = pHlp->pfnSSMPutU32(pSSM, pCtx->mouseShapeInfo.u32HotX);
1724 AssertRCReturn(rc, rc);
1725 rc = pHlp->pfnSSMPutU32(pSSM, pCtx->mouseShapeInfo.u32HotY);
1726 AssertRCReturn(rc, rc);
1727 rc = pHlp->pfnSSMPutU32(pSSM, pCtx->mouseShapeInfo.u32Width);
1728 AssertRCReturn(rc, rc);
1729 rc = pHlp->pfnSSMPutU32(pSSM, pCtx->mouseShapeInfo.u32Height);
1730 AssertRCReturn(rc, rc);
1731 rc = pHlp->pfnSSMPutU32(pSSM, pCtx->mouseShapeInfo.cbShape);
1732 AssertRCReturn(rc, rc);
1733 if (pCtx->mouseShapeInfo.cbShape)
1734 {
1735 rc = pHlp->pfnSSMPutMem(pSSM, pCtx->mouseShapeInfo.pu8Shape, pCtx->mouseShapeInfo.cbShape);
1736 AssertRCReturn(rc, rc);
1737 }
1738
1739#ifdef VBOX_WITH_WDDM
1740 /* Size of some additional data. For future extensions. */
1741 rc = pHlp->pfnSSMPutU32(pSSM, 4);
1742 AssertRCReturn(rc, rc);
1743 rc = pHlp->pfnSSMPutU32(pSSM, pThis->fGuestCaps);
1744 AssertRCReturn(rc, rc);
1745#else
1746 /* Size of some additional data. For future extensions. */
1747 rc = pHlp->pfnSSMPutU32(pSSM, 0);
1748 AssertRCReturn(rc, rc);
1749#endif
1750 rc = pHlp->pfnSSMPutU32(pSSM, RT_ELEMENTS(pCtx->aModeHints));
1751 AssertRCReturn(rc, rc);
1752 rc = pHlp->pfnSSMPutU32(pSSM, sizeof(VBVAMODEHINT));
1753 AssertRCReturn(rc, rc);
1754 for (unsigned i = 0; i < RT_ELEMENTS(pCtx->aModeHints); ++i)
1755 {
1756 rc = pHlp->pfnSSMPutMem(pSSM, &pCtx->aModeHints[i], sizeof(VBVAMODEHINT));
1757 AssertRCReturn(rc, rc);
1758 }
1759 }
1760 }
1761
1762 return rc;
1763}
1764
1765int vboxVBVASaveStateExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
1766{
1767 PVGASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVGASTATE);
1768 PVGASTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVGASTATECC);
1769 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
1770 int rc;
1771#ifdef VBOX_WITH_VIDEOHWACCEL
1772 VBOXVBVASAVEDSTATECBDATA VhwaData = {0};
1773 VhwaData.pSSM = pSSM;
1774 uint32_t cbCmd = sizeof (VBOXVHWACMD_HH_SAVESTATE_SAVEPERFORM); /* maximum cmd size */
1775 VBOXVHWACMD *pCmd = vbvaVHWAHHCommandCreate(VBOXVHWACMD_TYPE_HH_SAVESTATE_SAVEBEGIN, 0, cbCmd);
1776 Assert(pCmd);
1777 if (pCmd)
1778 {
1779 vbvaVHWAHHPost(pDevIns, pThis, pThisCC, pCmd, NULL, vboxVBVASaveStateBeginPostCb, &VhwaData);
1780 rc = VhwaData.rc;
1781 AssertRC(rc);
1782 if (RT_SUCCESS(rc))
1783 {
1784#endif
1785 rc = vboxVBVASaveDevStateExec(pHlp, pThis, pThisCC, pSSM);
1786 AssertRC(rc);
1787#ifdef VBOX_WITH_VIDEOHWACCEL
1788 if (RT_SUCCESS(rc))
1789 {
1790 vbvaVHWAHHCommandReinit(pCmd, VBOXVHWACMD_TYPE_HH_SAVESTATE_SAVEPERFORM, 0);
1791 VBOXVHWACMD_HH_SAVESTATE_SAVEPERFORM *pSave = VBOXVHWACMD_BODY_HOST_HEAP(pCmd, VBOXVHWACMD_HH_SAVESTATE_SAVEPERFORM);
1792 pSave->pSSM = pSSM;
1793 vbvaVHWAHHPost(pDevIns, pThis, pThisCC, pCmd, vboxVBVASaveStatePerformPreCb, NULL, &VhwaData);
1794 rc = VhwaData.rc;
1795 AssertRC(rc);
1796 if (RT_SUCCESS(rc))
1797 {
1798 rc = vbvaVHWACommandSavePending(pHlp, pThis, pThisCC, pSSM);
1799 AssertRCReturn(rc, rc);
1800
1801 vbvaVHWAHHCommandReinit(pCmd, VBOXVHWACMD_TYPE_HH_SAVESTATE_SAVEEND, 0);
1802 vbvaVHWAHHPost(pDevIns, pThis, pThisCC, pCmd, vboxVBVASaveStateEndPreCb, NULL, &VhwaData);
1803 rc = VhwaData.rc;
1804 AssertRC(rc);
1805 }
1806 }
1807 }
1808
1809 vbvaVHWAHHCommandRelease(pCmd);
1810 }
1811 else
1812 rc = VERR_OUT_OF_RESOURCES;
1813#else
1814 if (RT_SUCCESS(rc))
1815 {
1816 for (uint32_t i = 0; i < pThis->cMonitors; ++i)
1817 {
1818 rc = pHlp->pfnSSMPutU32(pSSM, VBOXVBVASAVEDSTATE_VHWAUNAVAILABLE_MAGIC);
1819 AssertRCReturn(rc, rc);
1820 }
1821 }
1822
1823 /* no pending commands */
1824 pHlp->pfnSSMPutU32(pSSM, 0);
1825#endif
1826 return rc;
1827}
1828
1829int vboxVBVALoadStateExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion)
1830{
1831 if (uVersion < VGA_SAVEDSTATE_VERSION_HGSMI)
1832 {
1833 /* Nothing was saved. */
1834 return VINF_SUCCESS;
1835 }
1836
1837 PVGASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVGASTATE);
1838 PVGASTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVGASTATECC);
1839 PHGSMIINSTANCE pIns = pThisCC->pHGSMI;
1840 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
1841 int rc = HGSMIHostLoadStateExec(pHlp, pIns, pSSM, uVersion);
1842 if (RT_SUCCESS(rc))
1843 {
1844 VGA_SAVED_STATE_GET_MARKER_RETURN_ON_MISMATCH(pSSM, uVersion, 2);
1845
1846 /* Load VBVACONTEXT. */
1847 VBVACONTEXT *pCtx = (VBVACONTEXT *)HGSMIContext (pIns);
1848
1849 if (!pCtx)
1850 {
1851 /* This should not happen. */
1852 AssertFailed();
1853 rc = VERR_INVALID_PARAMETER;
1854 }
1855 else
1856 {
1857 uint32_t cViews = 0;
1858 rc = pHlp->pfnSSMGetU32 (pSSM, &cViews);
1859 AssertRCReturn(rc, rc);
1860
1861 uint32_t iView;
1862 for (iView = 0; iView < cViews; iView++)
1863 {
1864 VBVAVIEW *pView = &pCtx->aViews[iView];
1865
1866 rc = pHlp->pfnSSMGetU32 (pSSM, &pView->view.u32ViewIndex);
1867 AssertRCReturn(rc, rc);
1868 rc = pHlp->pfnSSMGetU32 (pSSM, &pView->view.u32ViewOffset);
1869 AssertRCReturn(rc, rc);
1870 rc = pHlp->pfnSSMGetU32 (pSSM, &pView->view.u32ViewSize);
1871 AssertRCReturn(rc, rc);
1872 rc = pHlp->pfnSSMGetU32 (pSSM, &pView->view.u32MaxScreenSize);
1873 AssertRCReturn(rc, rc);
1874
1875 rc = pHlp->pfnSSMGetU32 (pSSM, &pView->screen.u32ViewIndex);
1876 AssertRCReturn(rc, rc);
1877 rc = pHlp->pfnSSMGetS32 (pSSM, &pView->screen.i32OriginX);
1878 AssertRCReturn(rc, rc);
1879 rc = pHlp->pfnSSMGetS32 (pSSM, &pView->screen.i32OriginY);
1880 AssertRCReturn(rc, rc);
1881 rc = pHlp->pfnSSMGetU32 (pSSM, &pView->screen.u32StartOffset);
1882 AssertRCReturn(rc, rc);
1883 rc = pHlp->pfnSSMGetU32 (pSSM, &pView->screen.u32LineSize);
1884 AssertRCReturn(rc, rc);
1885 rc = pHlp->pfnSSMGetU32 (pSSM, &pView->screen.u32Width);
1886 AssertRCReturn(rc, rc);
1887 rc = pHlp->pfnSSMGetU32 (pSSM, &pView->screen.u32Height);
1888 AssertRCReturn(rc, rc);
1889 rc = pHlp->pfnSSMGetU16 (pSSM, &pView->screen.u16BitsPerPixel);
1890 AssertRCReturn(rc, rc);
1891 rc = pHlp->pfnSSMGetU16 (pSSM, &pView->screen.u16Flags);
1892 AssertRCReturn(rc, rc);
1893
1894 rc = pHlp->pfnSSMGetU32 (pSSM, &pView->vbva.u32VBVAOffset);
1895 AssertRCReturn(rc, rc);
1896
1897 rc = pHlp->pfnSSMGetU32 (pSSM, &pView->vbva.partialRecord.cb);
1898 AssertRCReturn(rc, rc);
1899
1900 if (pView->vbva.partialRecord.cb == 0)
1901 {
1902 pView->vbva.partialRecord.pu8 = NULL;
1903 }
1904 else
1905 {
1906 Assert(pView->vbva.partialRecord.pu8 == NULL); /* Should be it. */
1907
1908 uint8_t *pu8 = (uint8_t *)RTMemAlloc(pView->vbva.partialRecord.cb);
1909
1910 if (!pu8)
1911 {
1912 return VERR_NO_MEMORY;
1913 }
1914
1915 pView->vbva.partialRecord.pu8 = pu8;
1916
1917 rc = pHlp->pfnSSMGetMem (pSSM, pView->vbva.partialRecord.pu8, pView->vbva.partialRecord.cb);
1918 AssertRCReturn(rc, rc);
1919 }
1920
1921 if (pView->vbva.u32VBVAOffset == HGSMIOFFSET_VOID)
1922 {
1923 pView->vbva.guest.pVBVA = NULL;
1924 }
1925 else
1926 {
1927 pView->vbva.guest.pVBVA = (VBVABUFFER *)HGSMIOffsetToPointerHost(pIns, pView->vbva.u32VBVAOffset);
1928 }
1929 }
1930
1931 if (uVersion > VGA_SAVEDSTATE_VERSION_WITH_CONFIG)
1932 {
1933 /* Read mouse pointer shape information. */
1934 rc = pHlp->pfnSSMGetBool (pSSM, &pCtx->mouseShapeInfo.fSet);
1935 AssertRCReturn(rc, rc);
1936 rc = pHlp->pfnSSMGetBool (pSSM, &pCtx->mouseShapeInfo.fVisible);
1937 AssertRCReturn(rc, rc);
1938 rc = pHlp->pfnSSMGetBool (pSSM, &pCtx->mouseShapeInfo.fAlpha);
1939 AssertRCReturn(rc, rc);
1940 rc = pHlp->pfnSSMGetU32 (pSSM, &pCtx->mouseShapeInfo.u32HotX);
1941 AssertRCReturn(rc, rc);
1942 rc = pHlp->pfnSSMGetU32 (pSSM, &pCtx->mouseShapeInfo.u32HotY);
1943 AssertRCReturn(rc, rc);
1944 rc = pHlp->pfnSSMGetU32 (pSSM, &pCtx->mouseShapeInfo.u32Width);
1945 AssertRCReturn(rc, rc);
1946 rc = pHlp->pfnSSMGetU32 (pSSM, &pCtx->mouseShapeInfo.u32Height);
1947 AssertRCReturn(rc, rc);
1948 rc = pHlp->pfnSSMGetU32 (pSSM, &pCtx->mouseShapeInfo.cbShape);
1949 AssertRCReturn(rc, rc);
1950 if (pCtx->mouseShapeInfo.cbShape)
1951 {
1952 pCtx->mouseShapeInfo.pu8Shape = (uint8_t *)RTMemAlloc(pCtx->mouseShapeInfo.cbShape);
1953 if (pCtx->mouseShapeInfo.pu8Shape == NULL)
1954 {
1955 return VERR_NO_MEMORY;
1956 }
1957 pCtx->mouseShapeInfo.cbAllocated = pCtx->mouseShapeInfo.cbShape;
1958 rc = pHlp->pfnSSMGetMem (pSSM, pCtx->mouseShapeInfo.pu8Shape, pCtx->mouseShapeInfo.cbShape);
1959 AssertRCReturn(rc, rc);
1960 }
1961 else
1962 {
1963 pCtx->mouseShapeInfo.pu8Shape = NULL;
1964 }
1965
1966 /* Size of some additional data. For future extensions. */
1967 uint32_t cbExtra = 0;
1968 rc = pHlp->pfnSSMGetU32 (pSSM, &cbExtra);
1969 AssertRCReturn(rc, rc);
1970#ifdef VBOX_WITH_WDDM
1971 if (cbExtra >= 4)
1972 {
1973 rc = pHlp->pfnSSMGetU32 (pSSM, &pThis->fGuestCaps);
1974 AssertRCReturn(rc, rc);
1975 pThisCC->pDrv->pfnVBVAGuestCapabilityUpdate(pThisCC->pDrv, pThis->fGuestCaps);
1976 cbExtra -= 4;
1977 }
1978#endif
1979 if (cbExtra > 0)
1980 {
1981 rc = pHlp->pfnSSMSkip(pSSM, cbExtra);
1982 AssertRCReturn(rc, rc);
1983 }
1984
1985 if (uVersion >= VGA_SAVEDSTATE_VERSION_MODE_HINTS)
1986 {
1987 uint32_t cModeHints, cbModeHints;
1988 rc = pHlp->pfnSSMGetU32 (pSSM, &cModeHints);
1989 AssertRCReturn(rc, rc);
1990 rc = pHlp->pfnSSMGetU32 (pSSM, &cbModeHints);
1991 AssertRCReturn(rc, rc);
1992 memset(&pCtx->aModeHints, ~0, sizeof(pCtx->aModeHints));
1993 unsigned iHint;
1994 for (iHint = 0; iHint < cModeHints; ++iHint)
1995 {
1996 if ( cbModeHints <= sizeof(VBVAMODEHINT)
1997 && iHint < RT_ELEMENTS(pCtx->aModeHints))
1998 rc = pHlp->pfnSSMGetMem(pSSM, &pCtx->aModeHints[iHint],
1999 cbModeHints);
2000 else
2001 rc = pHlp->pfnSSMSkip(pSSM, cbModeHints);
2002 AssertRCReturn(rc, rc);
2003 }
2004 }
2005 }
2006
2007 pCtx->cViews = iView;
2008 LogFlowFunc(("%d views loaded\n", pCtx->cViews));
2009
2010 if (uVersion > VGA_SAVEDSTATE_VERSION_WDDM)
2011 {
2012 bool fLoadCommands;
2013
2014 if (uVersion < VGA_SAVEDSTATE_VERSION_FIXED_PENDVHWA)
2015 {
2016 const char *pcszOsArch = pHlp->pfnSSMHandleHostOSAndArch(pSSM);
2017 Assert(pcszOsArch);
2018 fLoadCommands = !pcszOsArch || RTStrNCmp(pcszOsArch, RT_STR_TUPLE("solaris"));
2019 }
2020 else
2021 fLoadCommands = true;
2022
2023#ifdef VBOX_WITH_VIDEOHWACCEL
2024 uint32_t cbCmd = sizeof (VBOXVHWACMD_HH_SAVESTATE_LOADPERFORM); /* maximum cmd size */
2025 VBOXVHWACMD *pCmd = vbvaVHWAHHCommandCreate(VBOXVHWACMD_TYPE_HH_SAVESTATE_LOADPERFORM, 0, cbCmd);
2026 Assert(pCmd);
2027 if(pCmd)
2028 {
2029 VBOXVBVASAVEDSTATECBDATA VhwaData = {0};
2030 VhwaData.pSSM = pSSM;
2031 VBOXVHWACMD_HH_SAVESTATE_LOADPERFORM *pLoad = VBOXVHWACMD_BODY_HOST_HEAP(pCmd, VBOXVHWACMD_HH_SAVESTATE_LOADPERFORM);
2032 pLoad->pSSM = pSSM;
2033 vbvaVHWAHHPost(pDevIns, pThis, pThisCC, pCmd, vboxVBVALoadStatePerformPreCb,
2034 vboxVBVALoadStatePerformPostCb, &VhwaData);
2035 rc = VhwaData.rc;
2036 vbvaVHWAHHCommandRelease(pCmd);
2037 AssertRCReturn(rc, rc);
2038
2039 if (fLoadCommands)
2040 {
2041 rc = vbvaVHWACommandLoadPending(pDevIns, pHlp, pThis, pThisCC, pSSM, uVersion);
2042 AssertRCReturn(rc, rc);
2043 }
2044 }
2045 else
2046 {
2047 rc = VERR_OUT_OF_RESOURCES;
2048 }
2049#else
2050 uint32_t u32;
2051
2052 for (uint32_t i = 0; i < pThis->cMonitors; ++i)
2053 {
2054 rc = pHlp->pfnSSMGetU32(pSSM, &u32);
2055 AssertRCReturn(rc, rc);
2056
2057 if (u32 != VBOXVBVASAVEDSTATE_VHWAUNAVAILABLE_MAGIC)
2058 {
2059 LogRel(("VBVA: 2D data while 2D is not supported\n"));
2060 return VERR_NOT_SUPPORTED;
2061 }
2062 }
2063
2064 if (fLoadCommands)
2065 {
2066 rc = pHlp->pfnSSMGetU32(pSSM, &u32);
2067 AssertRCReturn(rc, rc);
2068
2069 if (u32)
2070 {
2071 LogRel(("VBVA: 2D pending command while 2D is not supported\n"));
2072 return VERR_NOT_SUPPORTED;
2073 }
2074 }
2075#endif
2076 }
2077
2078#ifdef DEBUG_sunlover
2079 dumpctx(pCtx);
2080#endif
2081 }
2082 }
2083
2084 return rc;
2085}
2086
2087int vboxVBVALoadStateDone(PPDMDEVINS pDevIns)
2088{
2089 PVGASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVGASTATE);
2090 PVGASTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVGASTATECC);
2091 VBVACONTEXT *pCtx = (VBVACONTEXT *)HGSMIContext(pThisCC->pHGSMI);
2092 if (pCtx)
2093 {
2094 uint32_t iView;
2095 for (iView = 0; iView < pCtx->cViews; iView++)
2096 {
2097 VBVAVIEW *pView = &pCtx->aViews[iView];
2098 if (pView->vbva.guest.pVBVA)
2099 {
2100 int rc = vbvaEnable(pThis, pThisCC, pCtx, iView, pView->vbva.guest.pVBVA,
2101 pView->vbva.u32VBVAOffset, true /* fRestored */);
2102 if (RT_SUCCESS(rc))
2103 vbvaResize(pThisCC, pView, &pView->screen, false);
2104 else
2105 LogRel(("VBVA: can not restore: %Rrc\n", rc));
2106 }
2107 }
2108
2109 if (pCtx->mouseShapeInfo.fSet)
2110 vbvaUpdateMousePointerShape(pThisCC, &pCtx->mouseShapeInfo, true);
2111 }
2112
2113 return VINF_SUCCESS;
2114}
2115
2116void VBVARaiseIrq(PPDMDEVINS pDevIns, PVGASTATE pThis, PVGASTATECC pThisCC, uint32_t fFlags)
2117{
2118 int const rcLock = PDMDevHlpCritSectEnter(pDevIns, &pThis->CritSectIRQ, VERR_SEM_BUSY);
2119 PDM_CRITSECT_RELEASE_ASSERT_RC_DEV(pDevIns, &pThis->CritSectIRQ, rcLock);
2120
2121 const uint32_t fu32CurrentGuestFlags = HGSMIGetHostGuestFlags(pThisCC->pHGSMI);
2122 if ((fu32CurrentGuestFlags & HGSMIHOSTFLAGS_IRQ) == 0)
2123 {
2124 /* No IRQ set yet. */
2125 Assert(pThis->fu32PendingGuestFlags == 0);
2126
2127 HGSMISetHostGuestFlags(pThisCC->pHGSMI, HGSMIHOSTFLAGS_IRQ | fFlags);
2128
2129 /* If VM is not running, the IRQ will be set in VBVAOnResume. */
2130 const VMSTATE enmVMState = PDMDevHlpVMState(pDevIns);
2131 if ( enmVMState == VMSTATE_RUNNING
2132 || enmVMState == VMSTATE_RUNNING_LS)
2133 PDMDevHlpPCISetIrqNoWait(pDevIns, 0, PDM_IRQ_LEVEL_HIGH);
2134 }
2135 else
2136 {
2137 /* IRQ already set, remember the new flags. */
2138 pThis->fu32PendingGuestFlags |= HGSMIHOSTFLAGS_IRQ | fFlags;
2139 }
2140
2141 PDMDevHlpCritSectLeave(pDevIns, &pThis->CritSectIRQ);
2142}
2143
2144void VBVAOnResume(PPDMDEVINS pDevIns, PVGASTATE pThis, PVGASTATECC pThisCC)
2145{
2146 int const rcLock = PDMDevHlpCritSectEnter(pDevIns, &pThis->CritSectIRQ, VERR_SEM_BUSY);
2147 PDM_CRITSECT_RELEASE_ASSERT_RC_DEV(pDevIns, &pThis->CritSectIRQ, rcLock);
2148
2149 if (HGSMIGetHostGuestFlags(pThisCC->pHGSMI) & HGSMIHOSTFLAGS_IRQ)
2150 PDMDevHlpPCISetIrqNoWait(pDevIns, 0, PDM_IRQ_LEVEL_HIGH);
2151
2152 PDMDevHlpCritSectLeave(pDevIns, &pThis->CritSectIRQ);
2153}
2154
2155static int vbvaHandleQueryConf32(PVGASTATECC pThisCC, VBVACONF32 RT_UNTRUSTED_VOLATILE_GUEST *pConf32)
2156{
2157 uint32_t const idxQuery = pConf32->u32Index;
2158 RT_UNTRUSTED_NONVOLATILE_COPY_FENCE();
2159 LogFlowFunc(("VBVA_QUERY_CONF32: u32Index %d, u32Value 0x%x\n", idxQuery, pConf32->u32Value));
2160
2161 VBVACONTEXT *pCtx = (VBVACONTEXT *)HGSMIContext(pThisCC->pHGSMI);
2162 uint32_t uValue;
2163 if (idxQuery == VBOX_VBVA_CONF32_MONITOR_COUNT)
2164 uValue = pCtx->cViews;
2165 else if (idxQuery == VBOX_VBVA_CONF32_HOST_HEAP_SIZE)
2166 uValue = _64K; /** @todo a value calculated from the vram size */
2167 else if ( idxQuery == VBOX_VBVA_CONF32_MODE_HINT_REPORTING
2168 || idxQuery == VBOX_VBVA_CONF32_GUEST_CURSOR_REPORTING)
2169 uValue = VINF_SUCCESS;
2170 else if (idxQuery == VBOX_VBVA_CONF32_CURSOR_CAPABILITIES)
2171 uValue = VBOX_VBVA_CURSOR_CAPABILITY_HARDWARE;
2172 else if (idxQuery == VBOX_VBVA_CONF32_SCREEN_FLAGS)
2173 uValue = VBVA_SCREEN_F_ACTIVE
2174 | VBVA_SCREEN_F_DISABLED
2175 | VBVA_SCREEN_F_BLANK
2176 | VBVA_SCREEN_F_BLANK2;
2177 else if (idxQuery == VBOX_VBVA_CONF32_MAX_RECORD_SIZE)
2178 uValue = VBVA_MAX_RECORD_SIZE;
2179 else if (idxQuery == UINT32_MAX)
2180 uValue = UINT32_MAX; /* Older GA uses this for sanity checking. See testQueryConf in HGSMIBase.cpp on branches. */
2181 else
2182 ASSERT_GUEST_MSG_FAILED_RETURN(("Invalid index %#x\n", idxQuery), VERR_INVALID_PARAMETER);
2183
2184 pConf32->u32Value = uValue;
2185 return VINF_SUCCESS;
2186}
2187
2188static int vbvaHandleSetConf32(VBVACONF32 RT_UNTRUSTED_VOLATILE_GUEST *pConf32)
2189{
2190 uint32_t const idxQuery = pConf32->u32Index;
2191 uint32_t const uValue = pConf32->u32Value;
2192 RT_UNTRUSTED_NONVOLATILE_COPY_FENCE();
2193 LogFlowFunc(("VBVA_SET_CONF32: u32Index %d, u32Value 0x%x\n", idxQuery, uValue));
2194
2195 if (idxQuery == VBOX_VBVA_CONF32_MONITOR_COUNT)
2196 { /* do nothing. this is a const. */ }
2197 else if (idxQuery == VBOX_VBVA_CONF32_HOST_HEAP_SIZE)
2198 { /* do nothing. this is a const. */ }
2199 else
2200 ASSERT_GUEST_MSG_FAILED_RETURN(("Invalid index %#x (value=%u)\n", idxQuery, uValue), VERR_INVALID_PARAMETER);
2201
2202 RT_NOREF_PV(uValue);
2203 return VINF_SUCCESS;
2204}
2205
2206static int vbvaHandleInfoHeap(PVGASTATECC pThisCC, const VBVAINFOHEAP RT_UNTRUSTED_VOLATILE_GUEST *pInfoHeap)
2207{
2208 uint32_t const offHeap = pInfoHeap->u32HeapOffset;
2209 uint32_t const cbHeap = pInfoHeap->u32HeapSize;
2210 RT_UNTRUSTED_NONVOLATILE_COPY_FENCE();
2211 LogFlowFunc(("VBVA_INFO_HEAP: offset 0x%x, size 0x%x\n", offHeap, cbHeap));
2212
2213 return HGSMIHostHeapSetup(pThisCC->pHGSMI, offHeap, cbHeap);
2214}
2215
2216static int vbvaInfoView(PVGASTATE pThis, PVGASTATER3 pThisCC, const VBVAINFOVIEW RT_UNTRUSTED_VOLATILE_GUEST *pView)
2217{
2218 VBVAINFOVIEW view;
2219 RT_COPY_VOLATILE(view, *pView);
2220 RT_UNTRUSTED_NONVOLATILE_COPY_FENCE();
2221
2222 LogFlowFunc(("VBVA_INFO_VIEW: u32ViewIndex %d, u32ViewOffset 0x%x, u32ViewSize 0x%x, u32MaxScreenSize 0x%x\n",
2223 view.u32ViewIndex, view.u32ViewOffset, view.u32ViewSize, view.u32MaxScreenSize));
2224
2225 VBVACONTEXT *pCtx = (VBVACONTEXT *)HGSMIContext(pThisCC->pHGSMI);
2226 ASSERT_GUEST_LOGREL_MSG_RETURN( view.u32ViewIndex < pCtx->cViews
2227 && view.u32ViewOffset <= pThis->vram_size
2228 && view.u32ViewSize <= pThis->vram_size
2229 && view.u32ViewOffset <= pThis->vram_size - view.u32ViewSize
2230 && view.u32MaxScreenSize <= view.u32ViewSize,
2231 ("index %d(%d), offset 0x%x, size 0x%x, max 0x%x, vram size 0x%x\n",
2232 view.u32ViewIndex, pCtx->cViews, view.u32ViewOffset, view.u32ViewSize,
2233 view.u32MaxScreenSize, pThis->vram_size),
2234 VERR_INVALID_PARAMETER);
2235 RT_UNTRUSTED_VALIDATED_FENCE();
2236
2237 pCtx->aViews[view.u32ViewIndex].view = view;
2238 return VINF_SUCCESS;
2239}
2240
2241static int vbvaInfoScreen(PVGASTATECC pThisCC, const VBVAINFOSCREEN RT_UNTRUSTED_VOLATILE_GUEST *pScreen)
2242{
2243 /*
2244 * Copy input into non-volatile buffer.
2245 */
2246 VBVAINFOSCREEN screen;
2247 RT_COPY_VOLATILE(screen, *pScreen);
2248 RT_UNTRUSTED_NONVOLATILE_COPY_FENCE();
2249 LogRel2(("VBVA: InfoScreen: [%d] @%d,%d %dx%d, line 0x%x, BPP %d, flags 0x%x\n",
2250 screen.u32ViewIndex, screen.i32OriginX, screen.i32OriginY,
2251 screen.u32Width, screen.u32Height,
2252 screen.u32LineSize, screen.u16BitsPerPixel, screen.u16Flags));
2253
2254 /*
2255 * Validate input.
2256 */
2257 /* Allow screen.u16BitsPerPixel == 0 because legacy guest code used it for screen blanking. */
2258 VBVACONTEXT *pCtx = (VBVACONTEXT *)HGSMIContext(pThisCC->pHGSMI);
2259 ASSERT_GUEST_LOGREL_MSG_RETURN(screen.u32ViewIndex < pCtx->cViews,
2260 ("Screen index %#x is out of bound (cViews=%#x)\n", screen.u32ViewIndex, pCtx->cViews),
2261 VERR_INVALID_PARAMETER);
2262 ASSERT_GUEST_LOGREL_MSG_RETURN( screen.u16BitsPerPixel <= 32
2263 && screen.u32Width <= UINT16_MAX
2264 && screen.u32Height <= UINT16_MAX
2265 && screen.u32LineSize <= UINT16_MAX * UINT32_C(4),
2266 ("One or more values out of range: u16BitsPerPixel=%#x u32Width=%#x u32Height=%#x u32LineSize=%#x\n",
2267 screen.u16BitsPerPixel, screen.u32Width, screen.u32Height, screen.u32LineSize),
2268 VERR_INVALID_PARAMETER);
2269 RT_UNTRUSTED_VALIDATED_FENCE();
2270
2271 const VBVAINFOVIEW *pView = &pCtx->aViews[screen.u32ViewIndex].view;
2272 const uint32_t cbPerPixel = (screen.u16BitsPerPixel + 7) / 8;
2273 ASSERT_GUEST_LOGREL_MSG_RETURN(screen.u32Width <= screen.u32LineSize / (cbPerPixel ? cbPerPixel : 1),
2274 ("u32Width=%#x u32LineSize=%3x cbPerPixel=%#x\n",
2275 screen.u32Width, screen.u32LineSize, cbPerPixel),
2276 VERR_INVALID_PARAMETER);
2277
2278 const uint64_t u64ScreenSize = (uint64_t)screen.u32LineSize * screen.u32Height;
2279
2280 ASSERT_GUEST_LOGREL_MSG_RETURN( screen.u32StartOffset <= pView->u32ViewSize
2281 && u64ScreenSize <= pView->u32MaxScreenSize
2282 && screen.u32StartOffset <= pView->u32ViewSize - (uint32_t)u64ScreenSize,
2283 ("u32StartOffset=%#x u32ViewSize=%#x u64ScreenSize=%#RX64 u32MaxScreenSize=%#x\n",
2284 screen.u32StartOffset, pView->u32ViewSize, u64ScreenSize, pView->u32MaxScreenSize),
2285 VERR_INVALID_PARAMETER);
2286 RT_UNTRUSTED_VALIDATED_FENCE();
2287
2288 /*
2289 * Do the job.
2290 */
2291 vbvaResize(pThisCC, &pCtx->aViews[screen.u32ViewIndex], &screen, true);
2292 return VINF_SUCCESS;
2293}
2294
2295#ifdef UNUSED_FUNCTION
2296int VBVAGetInfoViewAndScreen(PVGASTATE pThis, PVGASTATECC pThisCC, uint32_t u32ViewIndex, VBVAINFOVIEW *pView, VBVAINFOSCREEN *pScreen)
2297{
2298 if (u32ViewIndex >= pThis->cMonitors)
2299 return VERR_INVALID_PARAMETER;
2300
2301 PHGSMIINSTANCE pIns = pThisCC->pHGSMI;
2302 VBVACONTEXT *pCtx = (VBVACONTEXT *)HGSMIContext (pIns);
2303
2304 if (pView)
2305 *pView = pCtx->aViews[u32ViewIndex].view;
2306
2307 if (pScreen)
2308 *pScreen = pCtx->aViews[u32ViewIndex].screen;
2309
2310 return VINF_SUCCESS;
2311}
2312#endif
2313
2314static int vbvaHandleEnable(PVGASTATE pThis, PVGASTATER3 pThisCC, uint32_t fEnableFlags, uint32_t offEnable, uint32_t idScreen)
2315{
2316 LogFlowFunc(("VBVA_ENABLE[%u]: fEnableFlags=0x%x offEnable=%#x\n", idScreen, fEnableFlags, offEnable));
2317 PHGSMIINSTANCE pIns = pThisCC->pHGSMI;
2318 VBVACONTEXT *pCtx = (VBVACONTEXT *)HGSMIContext(pIns);
2319
2320 /*
2321 * Validate input.
2322 */
2323 ASSERT_GUEST_LOGREL_MSG_RETURN(idScreen < pCtx->cViews, ("idScreen=%#x cViews=%#x\n", idScreen, pCtx->cViews), VERR_INVALID_PARAMETER);
2324 ASSERT_GUEST_LOGREL_MSG_RETURN( (fEnableFlags & (VBVA_F_ENABLE | VBVA_F_DISABLE)) == VBVA_F_ENABLE
2325 || (fEnableFlags & (VBVA_F_ENABLE | VBVA_F_DISABLE)) == VBVA_F_DISABLE,
2326 ("fEnableFlags=%#x\n", fEnableFlags),
2327 VERR_INVALID_PARAMETER);
2328 if (fEnableFlags & VBVA_F_ENABLE)
2329 {
2330 ASSERT_GUEST_LOGREL_MSG_RETURN(offEnable < pThis->vram_size,
2331 ("offEnable=%#x vram_size=%#x\n", offEnable, pThis->vram_size),
2332 VERR_INVALID_PARAMETER);
2333 if (fEnableFlags & VBVA_F_ABSOFFSET)
2334 /* Offset from VRAM start. */
2335 ASSERT_GUEST_LOGREL_MSG_RETURN( pThis->vram_size >= RT_UOFFSETOF(VBVABUFFER, au8Data)
2336 && offEnable <= pThis->vram_size - RT_UOFFSETOF(VBVABUFFER, au8Data),
2337 ("offEnable=%#x vram_size=%#x\n", offEnable, pThis->vram_size),
2338 VERR_INVALID_PARAMETER);
2339 else
2340 {
2341 /* Offset from the view start. We'd be using idScreen here to fence required. */
2342 RT_UNTRUSTED_VALIDATED_FENCE();
2343 const VBVAINFOVIEW *pView = &pCtx->aViews[idScreen].view;
2344 ASSERT_GUEST_LOGREL_MSG_RETURN( pThis->vram_size - offEnable >= pView->u32ViewOffset
2345 && pView->u32ViewSize >= RT_UOFFSETOF(VBVABUFFER, au8Data)
2346 && offEnable <= pView->u32ViewSize - RT_UOFFSETOF(VBVABUFFER, au8Data),
2347 ("offEnable=%#x vram_size=%#x view: %#x LB %#x\n",
2348 offEnable, pThis->vram_size, pView->u32ViewOffset, pView->u32ViewSize),
2349 VERR_INVALID_PARAMETER);
2350 offEnable += pView->u32ViewOffset;
2351 }
2352 ASSERT_GUEST_LOGREL_MSG_RETURN(HGSMIIsOffsetValid(pIns, offEnable),
2353 ("offEnable=%#x area %#x LB %#x\n",
2354 offEnable, HGSMIGetAreaOffset(pIns), HGSMIGetAreaSize(pIns)),
2355 VERR_INVALID_PARAMETER);
2356 }
2357 RT_UNTRUSTED_VALIDATED_FENCE();
2358
2359 /*
2360 * Execute.
2361 */
2362 int rc = VINF_SUCCESS;
2363 if (fEnableFlags & VBVA_F_ENABLE)
2364 {
2365 VBVABUFFER RT_UNTRUSTED_VOLATILE_GUEST *pVBVA
2366 = (VBVABUFFER RT_UNTRUSTED_VOLATILE_GUEST *)HGSMIOffsetToPointerHost(pIns, offEnable);
2367 ASSERT_GUEST_LOGREL_RETURN(pVBVA, VERR_INVALID_PARAMETER); /* already check above, but let's be careful. */
2368
2369 /* Process any pending orders and empty the VBVA ring buffer. */
2370 vbvaFlush(pThis, pThisCC, pCtx);
2371
2372 rc = vbvaEnable(pThis, pThisCC, pCtx, idScreen, pVBVA, offEnable, false /* fRestored */);
2373 if (RT_FAILURE(rc))
2374 LogRelMax(8, ("VBVA: can not enable: %Rrc\n", rc));
2375 }
2376 else
2377 rc = vbvaDisable(pThis, pThisCC, pCtx, idScreen);
2378 return rc;
2379}
2380
2381static int vbvaHandleQueryModeHints(PVGASTATECC pThisCC, VBVAQUERYMODEHINTS volatile *pQueryModeHints, HGSMISIZE cbBuffer)
2382{
2383 PHGSMIINSTANCE pIns = pThisCC->pHGSMI;
2384 VBVACONTEXT *pCtx = (VBVACONTEXT *)HGSMIContext(pIns);
2385
2386 /*
2387 * Copy and validate the request.
2388 */
2389 uint16_t const cHintsQueried = pQueryModeHints->cHintsQueried;
2390 uint16_t const cbHintStructureGuest = pQueryModeHints->cbHintStructureGuest;
2391 RT_UNTRUSTED_NONVOLATILE_COPY_FENCE();
2392
2393 LogRelFlowFunc(("VBVA: HandleQueryModeHints: cHintsQueried=%RU16, cbHintStructureGuest=%RU16\n",
2394 cHintsQueried, cbHintStructureGuest));
2395 ASSERT_GUEST_RETURN(cbBuffer >= sizeof(VBVAQUERYMODEHINTS) + (uint32_t)cHintsQueried * cbHintStructureGuest,
2396 VERR_INVALID_PARAMETER);
2397 RT_UNTRUSTED_VALIDATED_FENCE();
2398
2399 /*
2400 * Produce the requested data.
2401 */
2402 uint8_t *pbHint = (uint8_t *)(pQueryModeHints + 1);
2403 memset(pbHint, ~0, cbBuffer - sizeof(VBVAQUERYMODEHINTS));
2404
2405 for (unsigned iHint = 0; iHint < cHintsQueried && iHint < VBOX_VIDEO_MAX_SCREENS; ++iHint)
2406 {
2407 memcpy(pbHint, &pCtx->aModeHints[iHint], RT_MIN(cbHintStructureGuest, sizeof(VBVAMODEHINT)));
2408 pbHint += cbHintStructureGuest;
2409 Assert((uintptr_t)(pbHint - (uint8_t *)pQueryModeHints) <= cbBuffer);
2410 }
2411
2412 return VINF_SUCCESS;
2413}
2414
2415/*
2416 *
2417 * New VBVA uses a new interface id: #define VBE_DISPI_ID_VBOX_VIDEO 0xBE01
2418 *
2419 * VBVA uses two 32 bits IO ports to write VRAM offsets of shared memory blocks for commands.
2420 * Read Write
2421 * Host port 0x3b0 to process completed
2422 * Guest port 0x3d0 control value? to process
2423 *
2424 */
2425
2426static DECLCALLBACK(void) vbvaNotifyGuest(void *pvCallback)
2427{
2428#if defined(VBOX_WITH_HGSMI) && (defined(VBOX_WITH_VIDEOHWACCEL) || defined(VBOX_WITH_VDMA) || defined(VBOX_WITH_WDDM))
2429 PPDMDEVINS pDevIns = (PPDMDEVINS)pvCallback;
2430 PVGASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVGASTATE);
2431 PVGASTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVGASTATECC);
2432 VBVARaiseIrq(pDevIns, pThis, pThisCC, 0);
2433#else
2434 NOREF(pvCallback);
2435 /* Do nothing. Later the VMMDev/VGA IRQ can be used for the notification. */
2436#endif
2437}
2438
2439/**
2440 * The guest submitted a command buffer (hit VGA_PORT_HGSMI_GUEST).
2441 *
2442 * Verify the buffer size and invoke corresponding handler.
2443 *
2444 * @return VBox status code.
2445 * @param pvHandler The VBVA channel context.
2446 * @param u16ChannelInfo Command code.
2447 * @param pvBuffer HGSMI buffer with command data. Considered volatile!
2448 * @param cbBuffer Size of command data.
2449 *
2450 * @thread EMT
2451 */
2452static DECLCALLBACK(int) vbvaChannelHandler(void *pvHandler, uint16_t u16ChannelInfo,
2453 void RT_UNTRUSTED_VOLATILE_GUEST *pvBuffer, HGSMISIZE cbBuffer)
2454{
2455 int rc = VINF_SUCCESS;
2456
2457 LogFlowFunc(("pvHandler %p, u16ChannelInfo %d, pvBuffer %p, cbBuffer %u\n", pvHandler, u16ChannelInfo, pvBuffer, cbBuffer));
2458
2459 PPDMDEVINS pDevIns = (PPDMDEVINS)pvHandler;
2460 PVGASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVGASTATE);
2461 PVGASTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVGASTATECC);
2462 PHGSMIINSTANCE pIns = pThisCC->pHGSMI;
2463 VBVACONTEXT *pCtx = (VBVACONTEXT *)HGSMIContext(pIns);
2464
2465 switch (u16ChannelInfo)
2466 {
2467#ifdef VBOX_WITH_VDMA
2468 case VBVA_VDMA_CMD:
2469 if (cbBuffer >= VBoxSHGSMIBufferHeaderSize() + sizeof(VBOXVDMACBUF_DR))
2470 {
2471 VBOXVDMACBUF_DR RT_UNTRUSTED_VOLATILE_GUEST *pCmd
2472 = (VBOXVDMACBUF_DR RT_UNTRUSTED_VOLATILE_GUEST *)VBoxSHGSMIBufferData((VBOXSHGSMIHEADER RT_UNTRUSTED_VOLATILE_GUEST *)pvBuffer);
2473 vboxVDMACommand(pThisCC->pVdma, pCmd, cbBuffer - VBoxSHGSMIBufferHeaderSize());
2474 rc = VINF_SUCCESS;
2475 }
2476 else
2477 rc = VERR_INVALID_PARAMETER;
2478 break;
2479
2480 case VBVA_VDMA_CTL:
2481 if (cbBuffer >= VBoxSHGSMIBufferHeaderSize() + sizeof(VBOXVDMA_CTL))
2482 {
2483 VBOXVDMA_CTL RT_UNTRUSTED_VOLATILE_GUEST *pCmd
2484 = (VBOXVDMA_CTL RT_UNTRUSTED_VOLATILE_GUEST *)VBoxSHGSMIBufferData((VBOXSHGSMIHEADER RT_UNTRUSTED_VOLATILE_GUEST *)pvBuffer);
2485 vboxVDMAControl(pThisCC->pVdma, pCmd, cbBuffer - VBoxSHGSMIBufferHeaderSize());
2486 }
2487 else
2488 rc = VERR_INVALID_PARAMETER;
2489 break;
2490#endif /* VBOX_WITH_VDMA */
2491
2492 case VBVA_QUERY_CONF32:
2493 if (cbBuffer >= sizeof(VBVACONF32))
2494 rc = vbvaHandleQueryConf32(pThisCC, (VBVACONF32 RT_UNTRUSTED_VOLATILE_GUEST *)pvBuffer);
2495 else
2496 rc = VERR_INVALID_PARAMETER;
2497 break;
2498
2499 case VBVA_SET_CONF32:
2500 if (cbBuffer >= sizeof(VBVACONF32))
2501 rc = vbvaHandleSetConf32((VBVACONF32 RT_UNTRUSTED_VOLATILE_GUEST *)pvBuffer);
2502 else
2503 rc = VERR_INVALID_PARAMETER;
2504 break;
2505
2506 case VBVA_INFO_VIEW:
2507 /* Expect at least one VBVAINFOVIEW structure. */
2508 rc = VERR_INVALID_PARAMETER;
2509 if (cbBuffer >= sizeof(VBVAINFOVIEW))
2510 {
2511 /* Guest submits an array of VBVAINFOVIEW structures. */
2512 const VBVAINFOVIEW RT_UNTRUSTED_VOLATILE_GUEST *pView = (VBVAINFOVIEW RT_UNTRUSTED_VOLATILE_GUEST *)pvBuffer;
2513 for (;
2514 cbBuffer >= sizeof(VBVAINFOVIEW);
2515 ++pView, cbBuffer -= sizeof(VBVAINFOVIEW))
2516 {
2517 rc = vbvaInfoView(pThis, pThisCC, pView);
2518 if (RT_FAILURE(rc))
2519 break;
2520 }
2521 }
2522 break;
2523
2524 case VBVA_INFO_HEAP:
2525 if (cbBuffer >= sizeof(VBVAINFOHEAP))
2526 rc = vbvaHandleInfoHeap(pThisCC, (VBVAINFOHEAP RT_UNTRUSTED_VOLATILE_GUEST *)pvBuffer);
2527 else
2528 rc = VERR_INVALID_PARAMETER;
2529 break;
2530
2531 case VBVA_FLUSH:
2532 if (cbBuffer >= sizeof(VBVAFLUSH))
2533 rc = vbvaFlush(pThis, pThisCC, pCtx);
2534 else
2535 rc = VERR_INVALID_PARAMETER;
2536 break;
2537
2538 case VBVA_INFO_SCREEN:
2539 rc = VERR_INVALID_PARAMETER;
2540 if (cbBuffer >= sizeof(VBVAINFOSCREEN))
2541 rc = vbvaInfoScreen(pThisCC, (VBVAINFOSCREEN RT_UNTRUSTED_VOLATILE_GUEST *)pvBuffer);
2542 break;
2543
2544 case VBVA_ENABLE:
2545 rc = VERR_INVALID_PARAMETER;
2546 if (cbBuffer >= sizeof(VBVAENABLE))
2547 {
2548 VBVAENABLE RT_UNTRUSTED_VOLATILE_GUEST *pVbvaEnable = (VBVAENABLE RT_UNTRUSTED_VOLATILE_GUEST *)pvBuffer;
2549 uint32_t const fEnableFlags = pVbvaEnable->u32Flags;
2550 uint32_t const offEnable = pVbvaEnable->u32Offset;
2551 RT_UNTRUSTED_NONVOLATILE_COPY_FENCE();
2552
2553 uint32_t idScreen;
2554 if (fEnableFlags & VBVA_F_EXTENDED)
2555 {
2556 ASSERT_GUEST_STMT_BREAK(cbBuffer >= sizeof(VBVAENABLE_EX), rc = VERR_INVALID_PARAMETER);
2557 idScreen = ((VBVAENABLE_EX RT_UNTRUSTED_VOLATILE_GUEST *)pvBuffer)->u32ScreenId;
2558 RT_UNTRUSTED_NONVOLATILE_COPY_FENCE();
2559 }
2560 else
2561 idScreen = vbvaViewFromBufferPtr(pIns, pCtx, pvBuffer);
2562
2563 rc = vbvaHandleEnable(pThis, pThisCC, fEnableFlags, offEnable, idScreen);
2564 pVbvaEnable->i32Result = rc;
2565 }
2566 break;
2567
2568 case VBVA_MOUSE_POINTER_SHAPE:
2569 if (cbBuffer >= sizeof(VBVAMOUSEPOINTERSHAPE))
2570 {
2571 VBVAMOUSEPOINTERSHAPE RT_UNTRUSTED_VOLATILE_GUEST *pShape
2572 = (VBVAMOUSEPOINTERSHAPE RT_UNTRUSTED_VOLATILE_GUEST *)pvBuffer;
2573 rc = vbvaMousePointerShape(pThisCC, pCtx, pShape, cbBuffer);
2574 pShape->i32Result = rc;
2575 }
2576 else
2577 rc = VERR_INVALID_PARAMETER;
2578 break;
2579
2580
2581#ifdef VBOX_WITH_VIDEOHWACCEL
2582 case VBVA_VHWA_CMD:
2583 if (cbBuffer >= VBOXVHWACMD_HEADSIZE())
2584 {
2585 vbvaVHWAHandleCommand(pDevIns, pThis, pThisCC, (VBOXVHWACMD RT_UNTRUSTED_VOLATILE_GUEST *)pvBuffer);
2586 rc = VINF_SUCCESS;
2587 }
2588 else
2589 rc = VERR_INVALID_PARAMETER;
2590 break;
2591#endif
2592
2593#ifdef VBOX_WITH_WDDM
2594 case VBVA_INFO_CAPS:
2595 if (cbBuffer >= sizeof(VBVACAPS))
2596 {
2597 VBVACAPS RT_UNTRUSTED_VOLATILE_GUEST *pCaps = (VBVACAPS RT_UNTRUSTED_VOLATILE_GUEST *)pvBuffer;
2598 pThis->fGuestCaps = pCaps->fCaps;
2599 RT_UNTRUSTED_NONVOLATILE_COPY_FENCE();
2600
2601 pThisCC->pDrv->pfnVBVAGuestCapabilityUpdate(pThisCC->pDrv, pThis->fGuestCaps);
2602 pCaps->rc = rc = VINF_SUCCESS;
2603 }
2604 else
2605 rc = VERR_INVALID_PARAMETER;
2606 break;
2607#endif
2608
2609 case VBVA_SCANLINE_CFG:
2610 if (cbBuffer >= sizeof(VBVASCANLINECFG))
2611 {
2612 VBVASCANLINECFG RT_UNTRUSTED_VOLATILE_GUEST *pCfg = (VBVASCANLINECFG RT_UNTRUSTED_VOLATILE_GUEST *)pvBuffer;
2613 pThis->fScanLineCfg = pCfg->fFlags;
2614 RT_UNTRUSTED_NONVOLATILE_COPY_FENCE();
2615
2616 pCfg->rc = rc = VINF_SUCCESS;
2617 }
2618 else
2619 rc = VERR_INVALID_PARAMETER;
2620 break;
2621
2622 case VBVA_QUERY_MODE_HINTS:
2623 if (cbBuffer >= sizeof(VBVAQUERYMODEHINTS))
2624 {
2625 VBVAQUERYMODEHINTS RT_UNTRUSTED_VOLATILE_GUEST *pQueryModeHints
2626 = (VBVAQUERYMODEHINTS RT_UNTRUSTED_VOLATILE_GUEST *)pvBuffer;
2627 rc = vbvaHandleQueryModeHints(pThisCC, pQueryModeHints, cbBuffer);
2628 pQueryModeHints->rc = rc;
2629 }
2630 else
2631 rc = VERR_INVALID_PARAMETER;
2632 break;
2633
2634 case VBVA_REPORT_INPUT_MAPPING:
2635 if (cbBuffer >= sizeof(VBVAREPORTINPUTMAPPING))
2636 {
2637 VBVAREPORTINPUTMAPPING inputMapping;
2638 {
2639 VBVAREPORTINPUTMAPPING RT_UNTRUSTED_VOLATILE_GUEST *pInputMapping
2640 = (VBVAREPORTINPUTMAPPING RT_UNTRUSTED_VOLATILE_GUEST *)pvBuffer;
2641 inputMapping.x = pInputMapping->x;
2642 inputMapping.y = pInputMapping->y;
2643 inputMapping.cx = pInputMapping->cx;
2644 inputMapping.cy = pInputMapping->cy;
2645 }
2646 RT_UNTRUSTED_NONVOLATILE_COPY_FENCE();
2647
2648 LogRelFlowFunc(("VBVA: ChannelHandler: VBVA_REPORT_INPUT_MAPPING: x=%RI32, y=%RI32, cx=%RU32, cy=%RU32\n",
2649 inputMapping.x, inputMapping.y, inputMapping.cx, inputMapping.cy));
2650 pThisCC->pDrv->pfnVBVAInputMappingUpdate(pThisCC->pDrv,
2651 inputMapping.x, inputMapping.y,
2652 inputMapping.cx, inputMapping.cy);
2653 rc = VINF_SUCCESS;
2654 }
2655 else
2656 rc = VERR_INVALID_PARAMETER;
2657 break;
2658
2659 case VBVA_CURSOR_POSITION:
2660 if (cbBuffer >= sizeof(VBVACURSORPOSITION))
2661 {
2662 VBVACURSORPOSITION RT_UNTRUSTED_VOLATILE_GUEST *pReport = (VBVACURSORPOSITION RT_UNTRUSTED_VOLATILE_GUEST *)pvBuffer;
2663 VBVACURSORPOSITION Report;
2664 Report.fReportPosition = pReport->fReportPosition;
2665 Report.x = pReport->x;
2666 Report.y = pReport->y;
2667 RT_UNTRUSTED_NONVOLATILE_COPY_FENCE();
2668
2669 LogRelFlowFunc(("VBVA: ChannelHandler: VBVA_CURSOR_POSITION: fReportPosition=%RTbool, Id=%RU32, x=%RU32, y=%RU32\n",
2670 RT_BOOL(Report.fReportPosition), vbvaViewFromBufferPtr(pIns, pCtx, pvBuffer), Report.x, Report.y));
2671
2672 pThisCC->pDrv->pfnVBVAReportCursorPosition(pThisCC->pDrv, RT_BOOL(Report.fReportPosition), vbvaViewFromBufferPtr(pIns, pCtx, pvBuffer), Report.x, Report.y);
2673 /* This was only ever briefly used by the guest, and a value
2674 * of zero in both was taken to mean "ignore". */
2675 pReport->x = 0;
2676 pReport->y = 0;
2677 rc = VINF_SUCCESS;
2678 }
2679 else
2680 rc = VERR_INVALID_PARAMETER;
2681 break;
2682
2683 default:
2684 Log(("Unsupported VBVA guest command %d (%#x)!!!\n", u16ChannelInfo, u16ChannelInfo));
2685 break;
2686 }
2687
2688 return rc;
2689}
2690
2691/** When VBVA is paused, the VGA device is allowed to work but
2692 * no HGSMI etc state is changed.
2693 */
2694static void vbvaPause(PVGASTATECC pThisCC, bool fPause)
2695{
2696 if (!pThisCC || !pThisCC->pHGSMI)
2697 return;
2698
2699 VBVACONTEXT *pCtx = (VBVACONTEXT *)HGSMIContext(pThisCC->pHGSMI);
2700 if (pCtx)
2701 pCtx->fPaused = fPause;
2702}
2703
2704bool VBVAIsPaused(PVGASTATECC pThisCC)
2705{
2706 if (pThisCC && pThisCC->pHGSMI)
2707 {
2708 const VBVACONTEXT *pCtx = (VBVACONTEXT *)HGSMIContext(pThisCC->pHGSMI);
2709 if (pCtx && pCtx->cViews)
2710 {
2711 /* If VBVA is enabled at all. */
2712 const VBVAVIEW *pView = &pCtx->aViews[0];
2713 if (pView->vbva.guest.pVBVA)
2714 return pCtx->fPaused;
2715 }
2716 }
2717 /* VBVA is disabled. */
2718 return true;
2719}
2720
2721void VBVAOnVBEChanged(PVGASTATE pThis, PVGASTATECC pThisCC)
2722{
2723 /* The guest does not depend on host handling the VBE registers. */
2724 if (pThis->fGuestCaps & VBVACAPS_USE_VBVA_ONLY)
2725 return;
2726
2727 vbvaPause(pThisCC, (pThis->vbe_regs[VBE_DISPI_INDEX_ENABLE] & VBE_DISPI_ENABLED) == 0);
2728}
2729
2730void VBVAReset(PPDMDEVINS pDevIns, PVGASTATE pThis, PVGASTATECC pThisCC)
2731{
2732 if (!pThis || !pThisCC->pHGSMI)
2733 return;
2734
2735 VBVACONTEXT *pCtx = (VBVACONTEXT *)HGSMIContext(pThisCC->pHGSMI);
2736
2737#ifdef VBOX_WITH_VIDEOHWACCEL
2738 vbvaVHWAReset(pDevIns, pThis, pThisCC);
2739#endif
2740
2741 HGSMIReset(pThisCC->pHGSMI);
2742 /* Make sure the IRQ is reset. */
2743 PDMDevHlpPCISetIrq(pDevIns, 0, PDM_IRQ_LEVEL_LOW);
2744 pThis->fu32PendingGuestFlags = 0;
2745
2746 if (pCtx)
2747 {
2748 vbvaFlush(pThis, pThisCC, pCtx);
2749
2750 for (unsigned idScreen = 0; idScreen < pCtx->cViews; idScreen++)
2751 vbvaDisable(pThis, pThisCC, pCtx, idScreen);
2752
2753 pCtx->mouseShapeInfo.fSet = false;
2754 RTMemFreeZ(pCtx->mouseShapeInfo.pu8Shape, pCtx->mouseShapeInfo.cbAllocated);
2755 pCtx->mouseShapeInfo.pu8Shape = NULL;
2756 pCtx->mouseShapeInfo.cbAllocated = 0;
2757 pCtx->mouseShapeInfo.cbShape = 0;
2758 }
2759
2760}
2761
2762int VBVAUpdateDisplay(PVGASTATE pThis, PVGASTATECC pThisCC)
2763{
2764 int rc = VERR_NOT_SUPPORTED; /* Assuming that the VGA device will have to do updates. */
2765
2766 VBVACONTEXT *pCtx = (VBVACONTEXT *)HGSMIContext(pThisCC->pHGSMI);
2767 if (pCtx)
2768 {
2769 if (!pCtx->fPaused)
2770 {
2771 rc = vbvaFlush(pThis, pThisCC, pCtx);
2772 if (RT_SUCCESS(rc))
2773 {
2774 if (!pCtx->aViews[0].vbva.guest.pVBVA)
2775 {
2776 /* VBVA is not enabled for the first view, so VGA device must do updates. */
2777 rc = VERR_NOT_SUPPORTED;
2778 }
2779 }
2780 }
2781 }
2782
2783 return rc;
2784}
2785
2786static int vbvaSendModeHintWorker(PPDMDEVINS pDevIns, PVGASTATE pThis, PVGASTATECC pThisCC,
2787 uint32_t cx, uint32_t cy, uint32_t cBPP, uint32_t iDisplay, uint32_t dx,
2788 uint32_t dy, uint32_t fEnabled,
2789 uint32_t fNotifyGuest)
2790{
2791 VBVACONTEXT *pCtx = (VBVACONTEXT *)HGSMIContext(pThisCC->pHGSMI);
2792 /** @note See Display::setVideoModeHint: "It is up to the guest to decide
2793 * whether the hint is valid. Therefore don't do any VRAM sanity checks
2794 * here! */
2795 if (iDisplay >= RT_MIN(pThis->cMonitors, RT_ELEMENTS(pCtx->aModeHints)))
2796 return VERR_OUT_OF_RANGE;
2797 pCtx->aModeHints[iDisplay].magic = VBVAMODEHINT_MAGIC;
2798 pCtx->aModeHints[iDisplay].cx = cx;
2799 pCtx->aModeHints[iDisplay].cy = cy;
2800 pCtx->aModeHints[iDisplay].cBPP = cBPP;
2801 pCtx->aModeHints[iDisplay].dx = dx;
2802 pCtx->aModeHints[iDisplay].dy = dy;
2803 pCtx->aModeHints[iDisplay].fEnabled = fEnabled;
2804 if (fNotifyGuest && pThis->fGuestCaps & VBVACAPS_IRQ && pThis->fGuestCaps & VBVACAPS_VIDEO_MODE_HINTS)
2805 VBVARaiseIrq(pDevIns, pThis, pThisCC, HGSMIHOSTFLAGS_HOTPLUG);
2806 return VINF_SUCCESS;
2807}
2808
2809
2810/**
2811 * @interface_method_impl{PDMIDISPLAYPORT,pfnSendModeHint}
2812 */
2813DECLCALLBACK(int) vbvaR3PortSendModeHint(PPDMIDISPLAYPORT pInterface, uint32_t cx, uint32_t cy, uint32_t cBPP,
2814 uint32_t iDisplay, uint32_t dx, uint32_t dy, uint32_t fEnabled, uint32_t fNotifyGuest)
2815{
2816 PVGASTATECC pThisCC = RT_FROM_MEMBER(pInterface, VGASTATECC, IPort);
2817 PPDMDEVINS pDevIns = pThisCC->pDevIns;
2818 PVGASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVGASTATE);
2819 int rc = PDMDevHlpCritSectEnter(pDevIns, &pThis->CritSect, VERR_SEM_BUSY);
2820 AssertRCReturn(rc, rc);
2821
2822 rc = vbvaSendModeHintWorker(pDevIns, pThis, pThisCC, cx, cy, cBPP, iDisplay, dx, dy, fEnabled, fNotifyGuest);
2823
2824 PDMDevHlpCritSectLeave(pDevIns, &pThis->CritSect);
2825 return rc;
2826}
2827
2828int VBVAInit(PPDMDEVINS pDevIns, PVGASTATE pThis, PVGASTATECC pThisCC)
2829{
2830 int rc = HGSMICreate(&pThisCC->pHGSMI,
2831 pDevIns,
2832 "VBVA",
2833 0,
2834 pThisCC->pbVRam,
2835 pThis->vram_size,
2836 vbvaNotifyGuest,
2837 pDevIns,
2838 sizeof(VBVACONTEXT));
2839 if (RT_SUCCESS(rc))
2840 {
2841 rc = HGSMIHostChannelRegister(pThisCC->pHGSMI,
2842 HGSMI_CH_VBVA,
2843 vbvaChannelHandler,
2844 pDevIns);
2845 if (RT_SUCCESS(rc))
2846 {
2847 VBVACONTEXT *pCtx = (VBVACONTEXT *)HGSMIContext(pThisCC->pHGSMI);
2848 pCtx->cViews = pThis->cMonitors;
2849 pCtx->fPaused = true;
2850 memset(pCtx->aModeHints, ~0, sizeof(pCtx->aModeHints));
2851 }
2852 }
2853
2854 return rc;
2855
2856}
2857
2858void VBVADestroy(PVGASTATECC pThisCC)
2859{
2860 PHGSMIINSTANCE pHgsmi = pThisCC->pHGSMI;
2861 if (pHgsmi)
2862 {
2863 VBVACONTEXT *pCtx = (VBVACONTEXT *)HGSMIContext(pHgsmi);
2864 pCtx->mouseShapeInfo.fSet = false;
2865 RTMemFreeZ(pCtx->mouseShapeInfo.pu8Shape, pCtx->mouseShapeInfo.cbAllocated);
2866 pCtx->mouseShapeInfo.pu8Shape = NULL;
2867 pCtx->mouseShapeInfo.cbAllocated = 0;
2868 pCtx->mouseShapeInfo.cbShape = 0;
2869
2870 HGSMIDestroy(pHgsmi);
2871 pThisCC->pHGSMI = NULL;
2872 }
2873}
2874
Note: See TracBrowser for help on using the repository browser.

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