VirtualBox

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

Last change on this file since 71879 was 71656, checked in by vboxsync, 7 years ago

DevVGA: Code cleanup in progress. bugref:9094

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