VirtualBox

source: vbox/trunk/src/VBox/Devices/Graphics/DevVGA-SVGA.cpp@ 86898

Last change on this file since 86898 was 86886, checked in by vboxsync, 4 years ago

Devices/Graphics: Map and GBO interfaces. bugref:9830

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 284.3 KB
Line 
1/* $Id: DevVGA-SVGA.cpp 86886 2020-11-14 02:30:53Z vboxsync $ */
2/** @file
3 * VMware SVGA device.
4 *
5 * Logging levels guidelines for this and related files:
6 * - Log() for normal bits.
7 * - LogFlow() for more info.
8 * - Log2 for hex dump of cursor data.
9 * - Log3 for hex dump of shader code.
10 * - Log4 for hex dumps of 3D data.
11 * - Log5 for info about GMR pages.
12 * - LogRel for the usual important stuff.
13 * - LogRel2 for cursor.
14 * - LogRel3 for 3D performance data.
15 * - LogRel4 for HW accelerated graphics output.
16 */
17
18/*
19 * Copyright (C) 2013-2020 Oracle Corporation
20 *
21 * This file is part of VirtualBox Open Source Edition (OSE), as
22 * available from http://www.virtualbox.org. This file is free software;
23 * you can redistribute it and/or modify it under the terms of the GNU
24 * General Public License (GPL) as published by the Free Software
25 * Foundation, in version 2 as it comes in the "COPYING" file of the
26 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
27 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
28 */
29
30
31/** @page pg_dev_vmsvga VMSVGA - VMware SVGA II Device Emulation
32 *
33 * This device emulation was contributed by trivirt AG. It offers an
34 * alternative to our Bochs based VGA graphics and 3d emulations. This is
35 * valuable for Xorg based guests, as there is driver support shipping with Xorg
36 * since it forked from XFree86.
37 *
38 *
39 * @section sec_dev_vmsvga_sdk The VMware SDK
40 *
41 * This is officially deprecated now, however it's still quite useful,
42 * especially for getting the old features working:
43 * http://vmware-svga.sourceforge.net/
44 *
45 * They currently point developers at the following resources.
46 * - http://cgit.freedesktop.org/xorg/driver/xf86-video-vmware/
47 * - http://cgit.freedesktop.org/mesa/mesa/tree/src/gallium/drivers/svga/
48 * - http://cgit.freedesktop.org/mesa/vmwgfx/
49 *
50 * @subsection subsec_dev_vmsvga_sdk_results Test results
51 *
52 * Test results:
53 * - 2dmark.img:
54 * + todo
55 * - backdoor-tclo.img:
56 * + todo
57 * - blit-cube.img:
58 * + todo
59 * - bunnies.img:
60 * + todo
61 * - cube.img:
62 * + todo
63 * - cubemark.img:
64 * + todo
65 * - dynamic-vertex-stress.img:
66 * + todo
67 * - dynamic-vertex.img:
68 * + todo
69 * - fence-stress.img:
70 * + todo
71 * - gmr-test.img:
72 * + todo
73 * - half-float-test.img:
74 * + todo
75 * - noscreen-cursor.img:
76 * - The CURSOR I/O and FIFO registers are not implemented, so the mouse
77 * cursor doesn't show. (Hacking the GUI a little, would make the cursor
78 * visible though.)
79 * - Cursor animation via the palette doesn't work.
80 * - During debugging, it turns out that the framebuffer content seems to
81 * be halfways ignore or something (memset(fb, 0xcc, lots)).
82 * - Trouble with way to small FIFO and the 256x256 cursor fails. Need to
83 * grow it 0x10 fold (128KB -> 2MB like in WS10).
84 * - null.img:
85 * + todo
86 * - pong.img:
87 * + todo
88 * - presentReadback.img:
89 * + todo
90 * - resolution-set.img:
91 * + todo
92 * - rt-gamma-test.img:
93 * + todo
94 * - screen-annotation.img:
95 * + todo
96 * - screen-cursor.img:
97 * + todo
98 * - screen-dma-coalesce.img:
99 * + todo
100 * - screen-gmr-discontig.img:
101 * + todo
102 * - screen-gmr-remap.img:
103 * + todo
104 * - screen-multimon.img:
105 * + todo
106 * - screen-present-clip.img:
107 * + todo
108 * - screen-render-test.img:
109 * + todo
110 * - screen-simple.img:
111 * + todo
112 * - screen-text.img:
113 * + todo
114 * - simple-shaders.img:
115 * + todo
116 * - simple_blit.img:
117 * + todo
118 * - tiny-2d-updates.img:
119 * + todo
120 * - video-formats.img:
121 * + todo
122 * - video-sync.img:
123 * + todo
124 *
125 */
126
127
128/*********************************************************************************************************************************
129* Header Files *
130*********************************************************************************************************************************/
131#define LOG_GROUP LOG_GROUP_DEV_VMSVGA
132#include <VBox/vmm/pdmdev.h>
133#include <VBox/version.h>
134#include <VBox/err.h>
135#include <VBox/log.h>
136#include <VBox/vmm/pgm.h>
137#include <VBox/sup.h>
138
139#include <iprt/assert.h>
140#include <iprt/semaphore.h>
141#include <iprt/uuid.h>
142#ifdef IN_RING3
143# include <iprt/ctype.h>
144# include <iprt/mem.h>
145# ifdef VBOX_STRICT
146# include <iprt/time.h>
147# endif
148#endif
149
150#include <VBox/AssertGuest.h>
151#include <VBox/VMMDev.h>
152#include <VBoxVideo.h>
153#include <VBox/bioslogo.h>
154
155/* should go BEFORE any other DevVGA include to make all DevVGA.h config defines be visible */
156#include "DevVGA.h"
157
158/* Should be included after DevVGA.h/DevVGA-SVGA.h to pick all defines. */
159#ifdef VBOX_WITH_VMSVGA3D
160# include "DevVGA-SVGA3d.h"
161# ifdef RT_OS_DARWIN
162# include "DevVGA-SVGA3d-cocoa.h"
163# endif
164# ifdef RT_OS_LINUX
165# ifdef IN_RING3
166# include "DevVGA-SVGA3d-glLdr.h"
167# endif
168# endif
169#endif
170#ifdef IN_RING3
171#include "DevVGA-SVGA-internal.h"
172#endif
173
174
175/*********************************************************************************************************************************
176* Defined Constants And Macros *
177*********************************************************************************************************************************/
178/**
179 * Macro for checking if a fixed FIFO register is valid according to the
180 * current FIFO configuration.
181 *
182 * @returns true / false.
183 * @param a_iIndex The fifo register index (like SVGA_FIFO_CAPABILITIES).
184 * @param a_offFifoMin A valid SVGA_FIFO_MIN value.
185 */
186#define VMSVGA_IS_VALID_FIFO_REG(a_iIndex, a_offFifoMin) ( ((a_iIndex) + 1) * sizeof(uint32_t) <= (a_offFifoMin) )
187
188
189/*********************************************************************************************************************************
190* Structures and Typedefs *
191*********************************************************************************************************************************/
192
193
194/*********************************************************************************************************************************
195* Internal Functions *
196*********************************************************************************************************************************/
197#ifdef IN_RING3
198# if defined(VMSVGA_USE_FIFO_ACCESS_HANDLER) || defined(DEBUG_FIFO_ACCESS)
199static FNPGMPHYSHANDLER vmsvgaR3FifoAccessHandler;
200# endif
201# ifdef DEBUG_GMR_ACCESS
202static FNPGMPHYSHANDLER vmsvgaR3GmrAccessHandler;
203# endif
204#endif
205
206
207/*********************************************************************************************************************************
208* Global Variables *
209*********************************************************************************************************************************/
210#ifdef IN_RING3
211
212/**
213 * SSM descriptor table for the VMSVGAGMRDESCRIPTOR structure.
214 */
215static SSMFIELD const g_aVMSVGAGMRDESCRIPTORFields[] =
216{
217 SSMFIELD_ENTRY_GCPHYS( VMSVGAGMRDESCRIPTOR, GCPhys),
218 SSMFIELD_ENTRY( VMSVGAGMRDESCRIPTOR, numPages),
219 SSMFIELD_ENTRY_TERM()
220};
221
222/**
223 * SSM descriptor table for the GMR structure.
224 */
225static SSMFIELD const g_aGMRFields[] =
226{
227 SSMFIELD_ENTRY( GMR, cMaxPages),
228 SSMFIELD_ENTRY( GMR, cbTotal),
229 SSMFIELD_ENTRY( GMR, numDescriptors),
230 SSMFIELD_ENTRY_IGN_HCPTR( GMR, paDesc),
231 SSMFIELD_ENTRY_TERM()
232};
233
234/**
235 * SSM descriptor table for the VMSVGASCREENOBJECT structure.
236 */
237static SSMFIELD const g_aVMSVGASCREENOBJECTFields[] =
238{
239 SSMFIELD_ENTRY( VMSVGASCREENOBJECT, fuScreen),
240 SSMFIELD_ENTRY( VMSVGASCREENOBJECT, idScreen),
241 SSMFIELD_ENTRY( VMSVGASCREENOBJECT, xOrigin),
242 SSMFIELD_ENTRY( VMSVGASCREENOBJECT, yOrigin),
243 SSMFIELD_ENTRY( VMSVGASCREENOBJECT, cWidth),
244 SSMFIELD_ENTRY( VMSVGASCREENOBJECT, cHeight),
245 SSMFIELD_ENTRY( VMSVGASCREENOBJECT, offVRAM),
246 SSMFIELD_ENTRY( VMSVGASCREENOBJECT, cbPitch),
247 SSMFIELD_ENTRY( VMSVGASCREENOBJECT, cBpp),
248 SSMFIELD_ENTRY( VMSVGASCREENOBJECT, fDefined),
249 SSMFIELD_ENTRY( VMSVGASCREENOBJECT, fModified),
250 SSMFIELD_ENTRY_VER( VMSVGASCREENOBJECT, cDpi, VGA_SAVEDSTATE_VERSION_VMSVGA_MIPLEVELS),
251 SSMFIELD_ENTRY_TERM()
252};
253
254/**
255 * SSM descriptor table for the VMSVGAR3STATE structure.
256 */
257static SSMFIELD const g_aVMSVGAR3STATEFields[] =
258{
259 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, paGMR),
260 SSMFIELD_ENTRY( VMSVGAR3STATE, GMRFB),
261 SSMFIELD_ENTRY( VMSVGAR3STATE, Cursor.fActive),
262 SSMFIELD_ENTRY( VMSVGAR3STATE, Cursor.xHotspot),
263 SSMFIELD_ENTRY( VMSVGAR3STATE, Cursor.yHotspot),
264 SSMFIELD_ENTRY( VMSVGAR3STATE, Cursor.width),
265 SSMFIELD_ENTRY( VMSVGAR3STATE, Cursor.height),
266 SSMFIELD_ENTRY( VMSVGAR3STATE, Cursor.cbData),
267 SSMFIELD_ENTRY_IGN_HCPTR( VMSVGAR3STATE, Cursor.pData),
268 SSMFIELD_ENTRY( VMSVGAR3STATE, colorAnnotation),
269 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, cBusyDelayedEmts),
270#ifdef VMSVGA_USE_EMT_HALT_CODE
271 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, BusyDelayedEmts),
272#else
273 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, hBusyDelayedEmts),
274#endif
275 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatBusyDelayEmts),
276 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3Cmd3dPresentProf),
277 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3Cmd3dDrawPrimitivesProf),
278 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3Cmd3dSurfaceDmaProf),
279 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3Cmd3dBlitSurfaceToScreenProf),
280 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3CmdDefineGmr2),
281 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3CmdDefineGmr2Free),
282 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3CmdDefineGmr2Modify),
283 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3CmdRemapGmr2),
284 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3CmdRemapGmr2Modify),
285 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3CmdInvalidCmd),
286 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3CmdFence),
287 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3CmdUpdate),
288 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3CmdUpdateVerbose),
289 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3CmdDefineCursor),
290 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3CmdDefineAlphaCursor),
291 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3CmdMoveCursor),
292 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3CmdDisplayCursor),
293 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3CmdRectFill),
294 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3CmdRectCopy),
295 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3CmdRectRopCopy),
296 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3CmdEscape),
297 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3CmdDefineScreen),
298 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3CmdDestroyScreen),
299 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3CmdDefineGmrFb),
300 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3CmdBlitGmrFbToScreen),
301 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3CmdBlitScreentoGmrFb),
302 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3CmdAnnotationFill),
303 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3CmdAnnotationCopy),
304 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3Cmd3dSurfaceDefine),
305 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3Cmd3dSurfaceDefineV2),
306 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3Cmd3dSurfaceDestroy),
307 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3Cmd3dSurfaceCopy),
308 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3Cmd3dSurfaceStretchBlt),
309 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3Cmd3dSurfaceDma),
310 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3Cmd3dSurfaceScreen),
311 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3Cmd3dContextDefine),
312 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3Cmd3dContextDestroy),
313 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3Cmd3dSetTransform),
314 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3Cmd3dSetZRange),
315 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3Cmd3dSetRenderState),
316 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3Cmd3dSetRenderTarget),
317 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3Cmd3dSetTextureState),
318 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3Cmd3dSetMaterial),
319 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3Cmd3dSetLightData),
320 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3Cmd3dSetLightEnable),
321 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3Cmd3dSetViewPort),
322 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3Cmd3dSetClipPlane),
323 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3Cmd3dClear),
324 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3Cmd3dPresent),
325 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3Cmd3dPresentReadBack),
326 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3Cmd3dShaderDefine),
327 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3Cmd3dShaderDestroy),
328 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3Cmd3dSetShader),
329 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3Cmd3dSetShaderConst),
330 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3Cmd3dDrawPrimitives),
331 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3Cmd3dSetScissorRect),
332 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3Cmd3dBeginQuery),
333 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3Cmd3dEndQuery),
334 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3Cmd3dWaitForQuery),
335 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3Cmd3dGenerateMipmaps),
336 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3Cmd3dActivateSurface),
337 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3Cmd3dDeactivateSurface),
338
339 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3RegConfigDoneWr),
340 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3RegGmrDescriptorWr),
341 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3RegGmrDescriptorWrErrors),
342 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatR3RegGmrDescriptorWrFree),
343
344 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatFifoCommands),
345 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatFifoErrors),
346 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatFifoUnkCmds),
347 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatFifoTodoTimeout),
348 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatFifoTodoWoken),
349 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatFifoStalls),
350 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatFifoExtendedSleep),
351# if defined(VMSVGA_USE_FIFO_ACCESS_HANDLER) || defined(DEBUG_FIFO_ACCESS)
352 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatFifoAccessHandler),
353# endif
354 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatFifoCursorFetchAgain),
355 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatFifoCursorNoChange),
356 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatFifoCursorPosition),
357 SSMFIELD_ENTRY_IGNORE( VMSVGAR3STATE, StatFifoCursorVisiblity),
358
359 SSMFIELD_ENTRY_TERM()
360};
361
362/**
363 * SSM descriptor table for the VGAState.svga structure.
364 */
365static SSMFIELD const g_aVGAStateSVGAFields[] =
366{
367 SSMFIELD_ENTRY_IGN_GCPHYS( VMSVGAState, GCPhysFIFO),
368 SSMFIELD_ENTRY_IGNORE( VMSVGAState, cbFIFO),
369 SSMFIELD_ENTRY_IGNORE( VMSVGAState, cbFIFOConfig),
370 SSMFIELD_ENTRY( VMSVGAState, u32SVGAId),
371 SSMFIELD_ENTRY( VMSVGAState, fEnabled),
372 SSMFIELD_ENTRY( VMSVGAState, fConfigured),
373 SSMFIELD_ENTRY( VMSVGAState, fBusy),
374 SSMFIELD_ENTRY( VMSVGAState, fTraces),
375 SSMFIELD_ENTRY( VMSVGAState, u32GuestId),
376 SSMFIELD_ENTRY( VMSVGAState, cScratchRegion),
377 SSMFIELD_ENTRY( VMSVGAState, au32ScratchRegion),
378 SSMFIELD_ENTRY( VMSVGAState, u32IrqStatus),
379 SSMFIELD_ENTRY( VMSVGAState, u32IrqMask),
380 SSMFIELD_ENTRY( VMSVGAState, u32PitchLock),
381 SSMFIELD_ENTRY( VMSVGAState, u32CurrentGMRId),
382 SSMFIELD_ENTRY( VMSVGAState, u32DeviceCaps),
383 SSMFIELD_ENTRY( VMSVGAState, u32IndexReg),
384 SSMFIELD_ENTRY_IGNORE( VMSVGAState, hFIFORequestSem),
385 SSMFIELD_ENTRY_IGNORE( VMSVGAState, uLastCursorUpdateCount),
386 SSMFIELD_ENTRY_IGNORE( VMSVGAState, fFIFOThreadSleeping),
387 SSMFIELD_ENTRY_VER( VMSVGAState, fGFBRegisters, VGA_SAVEDSTATE_VERSION_VMSVGA_SCREENS),
388 SSMFIELD_ENTRY( VMSVGAState, uWidth),
389 SSMFIELD_ENTRY( VMSVGAState, uHeight),
390 SSMFIELD_ENTRY( VMSVGAState, uBpp),
391 SSMFIELD_ENTRY( VMSVGAState, cbScanline),
392 SSMFIELD_ENTRY_VER( VMSVGAState, uScreenOffset, VGA_SAVEDSTATE_VERSION_VMSVGA),
393 SSMFIELD_ENTRY_VER( VMSVGAState, uCursorX, VGA_SAVEDSTATE_VERSION_VMSVGA_CURSOR),
394 SSMFIELD_ENTRY_VER( VMSVGAState, uCursorY, VGA_SAVEDSTATE_VERSION_VMSVGA_CURSOR),
395 SSMFIELD_ENTRY_VER( VMSVGAState, uCursorID, VGA_SAVEDSTATE_VERSION_VMSVGA_CURSOR),
396 SSMFIELD_ENTRY_VER( VMSVGAState, uCursorOn, VGA_SAVEDSTATE_VERSION_VMSVGA_CURSOR),
397 SSMFIELD_ENTRY( VMSVGAState, u32MaxWidth),
398 SSMFIELD_ENTRY( VMSVGAState, u32MaxHeight),
399 SSMFIELD_ENTRY( VMSVGAState, u32ActionFlags),
400 SSMFIELD_ENTRY( VMSVGAState, f3DEnabled),
401 SSMFIELD_ENTRY( VMSVGAState, fVRAMTracking),
402 SSMFIELD_ENTRY_IGNORE( VMSVGAState, u8FIFOExtCommand),
403 SSMFIELD_ENTRY_IGNORE( VMSVGAState, fFifoExtCommandWakeup),
404 SSMFIELD_ENTRY_IGNORE( VMSVGAState, cGMR),
405 SSMFIELD_ENTRY_TERM()
406};
407#endif /* IN_RING3 */
408
409
410/*********************************************************************************************************************************
411* Internal Functions *
412*********************************************************************************************************************************/
413#ifdef IN_RING3
414static void vmsvgaR3SetTraces(PPDMDEVINS pDevIns, PVGASTATE pThis, bool fTraces);
415static int vmsvgaR3LoadExecFifo(PCPDMDEVHLPR3 pHlp, PVGASTATE pThis, PVGASTATECC pThisCC, PSSMHANDLE pSSM,
416 uint32_t uVersion, uint32_t uPass);
417static int vmsvgaR3SaveExecFifo(PCPDMDEVHLPR3 pHlp, PVGASTATECC pThisCC, PSSMHANDLE pSSM);
418static void vmsvgaR3CmdBufSubmit(PPDMDEVINS pDevIns, PVGASTATE pThis, PVGASTATECC pThisCC, RTGCPHYS GCPhysCB, SVGACBContext CBCtx);
419#endif /* IN_RING3 */
420
421
422#define SVGA_CASE_ID2STR(idx) case idx: return #idx
423#if defined(LOG_ENABLED)
424/**
425 * Index register string name lookup
426 *
427 * @returns Index register string or "UNKNOWN"
428 * @param pThis The shared VGA/VMSVGA state.
429 * @param idxReg The index register.
430 */
431static const char *vmsvgaIndexToString(PVGASTATE pThis, uint32_t idxReg)
432{
433 switch (idxReg)
434 {
435 SVGA_CASE_ID2STR(SVGA_REG_ID);
436 SVGA_CASE_ID2STR(SVGA_REG_ENABLE);
437 SVGA_CASE_ID2STR(SVGA_REG_WIDTH);
438 SVGA_CASE_ID2STR(SVGA_REG_HEIGHT);
439 SVGA_CASE_ID2STR(SVGA_REG_MAX_WIDTH);
440 SVGA_CASE_ID2STR(SVGA_REG_MAX_HEIGHT);
441 SVGA_CASE_ID2STR(SVGA_REG_DEPTH);
442 SVGA_CASE_ID2STR(SVGA_REG_BITS_PER_PIXEL); /* Current bpp in the guest */
443 SVGA_CASE_ID2STR(SVGA_REG_PSEUDOCOLOR);
444 SVGA_CASE_ID2STR(SVGA_REG_RED_MASK);
445 SVGA_CASE_ID2STR(SVGA_REG_GREEN_MASK);
446 SVGA_CASE_ID2STR(SVGA_REG_BLUE_MASK);
447 SVGA_CASE_ID2STR(SVGA_REG_BYTES_PER_LINE);
448 SVGA_CASE_ID2STR(SVGA_REG_FB_START); /* (Deprecated) */
449 SVGA_CASE_ID2STR(SVGA_REG_FB_OFFSET);
450 SVGA_CASE_ID2STR(SVGA_REG_VRAM_SIZE);
451 SVGA_CASE_ID2STR(SVGA_REG_FB_SIZE);
452
453 /* ID 0 implementation only had the above registers, then the palette */
454 SVGA_CASE_ID2STR(SVGA_REG_CAPABILITIES);
455 SVGA_CASE_ID2STR(SVGA_REG_MEM_START); /* (Deprecated) */
456 SVGA_CASE_ID2STR(SVGA_REG_MEM_SIZE);
457 SVGA_CASE_ID2STR(SVGA_REG_CONFIG_DONE); /* Set when memory area configured */
458 SVGA_CASE_ID2STR(SVGA_REG_SYNC); /* See "FIFO Synchronization Registers" */
459 SVGA_CASE_ID2STR(SVGA_REG_BUSY); /* See "FIFO Synchronization Registers" */
460 SVGA_CASE_ID2STR(SVGA_REG_GUEST_ID); /* Set guest OS identifier */
461 SVGA_CASE_ID2STR(SVGA_REG_CURSOR_ID); /* (Deprecated) */
462 SVGA_CASE_ID2STR(SVGA_REG_CURSOR_X); /* (Deprecated) */
463 SVGA_CASE_ID2STR(SVGA_REG_CURSOR_Y); /* (Deprecated) */
464 SVGA_CASE_ID2STR(SVGA_REG_CURSOR_ON); /* (Deprecated) */
465 SVGA_CASE_ID2STR(SVGA_REG_HOST_BITS_PER_PIXEL); /* (Deprecated) */
466 SVGA_CASE_ID2STR(SVGA_REG_SCRATCH_SIZE); /* Number of scratch registers */
467 SVGA_CASE_ID2STR(SVGA_REG_MEM_REGS); /* Number of FIFO registers */
468 SVGA_CASE_ID2STR(SVGA_REG_NUM_DISPLAYS); /* (Deprecated) */
469 SVGA_CASE_ID2STR(SVGA_REG_PITCHLOCK); /* Fixed pitch for all modes */
470 SVGA_CASE_ID2STR(SVGA_REG_IRQMASK); /* Interrupt mask */
471
472 /* Legacy multi-monitor support */
473 SVGA_CASE_ID2STR(SVGA_REG_NUM_GUEST_DISPLAYS); /* Number of guest displays in X/Y direction */
474 SVGA_CASE_ID2STR(SVGA_REG_DISPLAY_ID); /* Display ID for the following display attributes */
475 SVGA_CASE_ID2STR(SVGA_REG_DISPLAY_IS_PRIMARY); /* Whether this is a primary display */
476 SVGA_CASE_ID2STR(SVGA_REG_DISPLAY_POSITION_X); /* The display position x */
477 SVGA_CASE_ID2STR(SVGA_REG_DISPLAY_POSITION_Y); /* The display position y */
478 SVGA_CASE_ID2STR(SVGA_REG_DISPLAY_WIDTH); /* The display's width */
479 SVGA_CASE_ID2STR(SVGA_REG_DISPLAY_HEIGHT); /* The display's height */
480
481 SVGA_CASE_ID2STR(SVGA_REG_GMR_ID);
482 SVGA_CASE_ID2STR(SVGA_REG_GMR_DESCRIPTOR);
483 SVGA_CASE_ID2STR(SVGA_REG_GMR_MAX_IDS);
484 SVGA_CASE_ID2STR(SVGA_REG_GMR_MAX_DESCRIPTOR_LENGTH);
485
486 SVGA_CASE_ID2STR(SVGA_REG_TRACES); /* Enable trace-based updates even when FIFO is on */
487 SVGA_CASE_ID2STR(SVGA_REG_GMRS_MAX_PAGES); /* Maximum number of 4KB pages for all GMRs */
488 SVGA_CASE_ID2STR(SVGA_REG_MEMORY_SIZE); /* Total dedicated device memory excluding FIFO */
489 SVGA_CASE_ID2STR(SVGA_REG_COMMAND_LOW); /* Lower 32 bits and submits commands */
490 SVGA_CASE_ID2STR(SVGA_REG_COMMAND_HIGH); /* Upper 32 bits of command buffer PA */
491 SVGA_CASE_ID2STR(SVGA_REG_MAX_PRIMARY_BOUNDING_BOX_MEM); /* Max primary memory */
492 SVGA_CASE_ID2STR(SVGA_REG_SUGGESTED_GBOBJECT_MEM_SIZE_KB); /* Suggested limit on mob mem */
493 SVGA_CASE_ID2STR(SVGA_REG_DEV_CAP); /* Write dev cap index, read value */
494 SVGA_CASE_ID2STR(SVGA_REG_CMD_PREPEND_LOW);
495 SVGA_CASE_ID2STR(SVGA_REG_iCMD_PREPEND_HIGH);
496 SVGA_CASE_ID2STR(SVGA_REG_SCREENTARGET_MAX_WIDTH);
497 SVGA_CASE_ID2STR(SVGA_REG_SCREENTARGET_MAX_HEIGHT);
498 SVGA_CASE_ID2STR(SVGA_REG_MOB_MAX_SIZE);
499 SVGA_CASE_ID2STR(SVGA_REG_TOP); /* Must be 1 more than the last register */
500
501 default:
502 if (idxReg - (uint32_t)SVGA_SCRATCH_BASE < pThis->svga.cScratchRegion)
503 return "SVGA_SCRATCH_BASE reg";
504 if (idxReg - (uint32_t)SVGA_PALETTE_BASE < (uint32_t)SVGA_NUM_PALETTE_REGS)
505 return "SVGA_PALETTE_BASE reg";
506 return "UNKNOWN";
507 }
508}
509#endif /* LOG_ENABLED */
510
511#if defined(LOG_ENABLED) || (defined(IN_RING3) && defined(VBOX_WITH_VMSVGA3D))
512static const char *vmsvgaDevCapIndexToString(SVGA3dDevCapIndex idxDevCap)
513{
514 switch (idxDevCap)
515 {
516 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_INVALID);
517 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_3D);
518 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_MAX_LIGHTS);
519 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_MAX_TEXTURES);
520 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_MAX_CLIP_PLANES);
521 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_VERTEX_SHADER_VERSION);
522 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_VERTEX_SHADER);
523 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_FRAGMENT_SHADER_VERSION);
524 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_FRAGMENT_SHADER);
525 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_MAX_RENDER_TARGETS);
526 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_S23E8_TEXTURES);
527 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_S10E5_TEXTURES);
528 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_MAX_FIXED_VERTEXBLEND);
529 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_D16_BUFFER_FORMAT);
530 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_D24S8_BUFFER_FORMAT);
531 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_D24X8_BUFFER_FORMAT);
532 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_QUERY_TYPES);
533 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_TEXTURE_GRADIENT_SAMPLING);
534 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_MAX_POINT_SIZE);
535 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_MAX_SHADER_TEXTURES);
536 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_MAX_TEXTURE_WIDTH);
537 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_MAX_TEXTURE_HEIGHT);
538 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_MAX_VOLUME_EXTENT);
539 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_MAX_TEXTURE_REPEAT);
540 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_MAX_TEXTURE_ASPECT_RATIO);
541 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_MAX_TEXTURE_ANISOTROPY);
542 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_MAX_PRIMITIVE_COUNT);
543 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_MAX_VERTEX_INDEX);
544 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_MAX_VERTEX_SHADER_INSTRUCTIONS);
545 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_MAX_FRAGMENT_SHADER_INSTRUCTIONS);
546 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_MAX_VERTEX_SHADER_TEMPS);
547 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_MAX_FRAGMENT_SHADER_TEMPS);
548 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_TEXTURE_OPS);
549 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_SURFACEFMT_X8R8G8B8);
550 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_SURFACEFMT_A8R8G8B8);
551 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_SURFACEFMT_A2R10G10B10);
552 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_SURFACEFMT_X1R5G5B5);
553 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_SURFACEFMT_A1R5G5B5);
554 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_SURFACEFMT_A4R4G4B4);
555 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_SURFACEFMT_R5G6B5);
556 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_SURFACEFMT_LUMINANCE16);
557 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_SURFACEFMT_LUMINANCE8_ALPHA8);
558 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_SURFACEFMT_ALPHA8);
559 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_SURFACEFMT_LUMINANCE8);
560 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_SURFACEFMT_Z_D16);
561 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_SURFACEFMT_Z_D24S8);
562 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_SURFACEFMT_Z_D24X8);
563 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_SURFACEFMT_DXT1);
564 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_SURFACEFMT_DXT2);
565 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_SURFACEFMT_DXT3);
566 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_SURFACEFMT_DXT4);
567 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_SURFACEFMT_DXT5);
568 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_SURFACEFMT_BUMPX8L8V8U8);
569 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_SURFACEFMT_A2W10V10U10);
570 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_SURFACEFMT_BUMPU8V8);
571 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_SURFACEFMT_Q8W8V8U8);
572 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_SURFACEFMT_CxV8U8);
573 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_SURFACEFMT_R_S10E5);
574 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_SURFACEFMT_R_S23E8);
575 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_SURFACEFMT_RG_S10E5);
576 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_SURFACEFMT_RG_S23E8);
577 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_SURFACEFMT_ARGB_S10E5);
578 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_SURFACEFMT_ARGB_S23E8);
579 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_MISSING62);
580 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_MAX_VERTEX_SHADER_TEXTURES);
581 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_MAX_SIMULTANEOUS_RENDER_TARGETS);
582 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_SURFACEFMT_V16U16);
583 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_SURFACEFMT_G16R16);
584 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_SURFACEFMT_A16B16G16R16);
585 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_SURFACEFMT_UYVY);
586 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_SURFACEFMT_YUY2);
587 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_MULTISAMPLE_NONMASKABLESAMPLES);
588 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_MULTISAMPLE_MASKABLESAMPLES);
589 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_ALPHATOCOVERAGE);
590 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_SUPERSAMPLE);
591 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_AUTOGENMIPMAPS);
592 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_SURFACEFMT_NV12);
593 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_SURFACEFMT_AYUV);
594 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_MAX_CONTEXT_IDS);
595 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_MAX_SURFACE_IDS);
596 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_SURFACEFMT_Z_DF16);
597 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_SURFACEFMT_Z_DF24);
598 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_SURFACEFMT_Z_D24S8_INT);
599 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_SURFACEFMT_ATI1);
600 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_SURFACEFMT_ATI2);
601 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DEAD1);
602 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_VIDEO_DECODE);
603 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_VIDEO_PROCESS);
604 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_LINE_AA);
605 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_LINE_STIPPLE);
606 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_MAX_LINE_WIDTH);
607 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_MAX_AA_LINE_WIDTH);
608 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_SURFACEFMT_YV12);
609 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_LOGICOPS);
610 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_TS_COLOR_KEY);
611 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DEAD2);
612 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DX);
613 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_MAX_TEXTURE_ARRAY_SIZE);
614 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DX_MAX_VERTEXBUFFERS);
615 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DX_MAX_CONSTANT_BUFFERS);
616 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DX_PROVOKING_VERTEX);
617 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_X8R8G8B8);
618 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_A8R8G8B8);
619 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R5G6B5);
620 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_X1R5G5B5);
621 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_A1R5G5B5);
622 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_A4R4G4B4);
623 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_Z_D32);
624 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_Z_D16);
625 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_Z_D24S8);
626 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_Z_D15S1);
627 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_LUMINANCE8);
628 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_LUMINANCE4_ALPHA4);
629 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_LUMINANCE16);
630 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_LUMINANCE8_ALPHA8);
631 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_DXT1);
632 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_DXT2);
633 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_DXT3);
634 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_DXT4);
635 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_DXT5);
636 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_BUMPU8V8);
637 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_BUMPL6V5U5);
638 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_BUMPX8L8V8U8);
639 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_FORMAT_DEAD1);
640 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_ARGB_S10E5);
641 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_ARGB_S23E8);
642 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_A2R10G10B10);
643 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_V8U8);
644 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_Q8W8V8U8);
645 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_CxV8U8);
646 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_X8L8V8U8);
647 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_A2W10V10U10);
648 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_ALPHA8);
649 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R_S10E5);
650 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R_S23E8);
651 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_RG_S10E5);
652 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_RG_S23E8);
653 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_BUFFER);
654 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_Z_D24X8);
655 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_V16U16);
656 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_G16R16);
657 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_A16B16G16R16);
658 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_UYVY);
659 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_YUY2);
660 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_NV12);
661 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_AYUV);
662 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R32G32B32A32_TYPELESS);
663 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R32G32B32A32_UINT);
664 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R32G32B32A32_SINT);
665 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R32G32B32_TYPELESS);
666 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R32G32B32_FLOAT);
667 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R32G32B32_UINT);
668 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R32G32B32_SINT);
669 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R16G16B16A16_TYPELESS);
670 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R16G16B16A16_UINT);
671 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R16G16B16A16_SNORM);
672 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R16G16B16A16_SINT);
673 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R32G32_TYPELESS);
674 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R32G32_UINT);
675 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R32G32_SINT);
676 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R32G8X24_TYPELESS);
677 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_D32_FLOAT_S8X24_UINT);
678 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R32_FLOAT_X8X24_TYPELESS);
679 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_X32_TYPELESS_G8X24_UINT);
680 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R10G10B10A2_TYPELESS);
681 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R10G10B10A2_UINT);
682 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R11G11B10_FLOAT);
683 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R8G8B8A8_TYPELESS);
684 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R8G8B8A8_UNORM);
685 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R8G8B8A8_UNORM_SRGB);
686 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R8G8B8A8_UINT);
687 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R8G8B8A8_SINT);
688 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R16G16_TYPELESS);
689 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R16G16_UINT);
690 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R16G16_SINT);
691 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R32_TYPELESS);
692 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_D32_FLOAT);
693 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R32_UINT);
694 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R32_SINT);
695 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R24G8_TYPELESS);
696 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_D24_UNORM_S8_UINT);
697 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R24_UNORM_X8_TYPELESS);
698 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_X24_TYPELESS_G8_UINT);
699 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R8G8_TYPELESS);
700 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R8G8_UNORM);
701 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R8G8_UINT);
702 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R8G8_SINT);
703 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R16_TYPELESS);
704 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R16_UNORM);
705 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R16_UINT);
706 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R16_SNORM);
707 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R16_SINT);
708 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R8_TYPELESS);
709 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R8_UNORM);
710 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R8_UINT);
711 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R8_SNORM);
712 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R8_SINT);
713 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_P8);
714 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R9G9B9E5_SHAREDEXP);
715 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R8G8_B8G8_UNORM);
716 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_G8R8_G8B8_UNORM);
717 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_BC1_TYPELESS);
718 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_BC1_UNORM_SRGB);
719 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_BC2_TYPELESS);
720 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_BC2_UNORM_SRGB);
721 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_BC3_TYPELESS);
722 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_BC3_UNORM_SRGB);
723 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_BC4_TYPELESS);
724 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_ATI1);
725 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_BC4_SNORM);
726 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_BC5_TYPELESS);
727 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_ATI2);
728 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_BC5_SNORM);
729 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R10G10B10_XR_BIAS_A2_UNORM);
730 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_B8G8R8A8_TYPELESS);
731 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_B8G8R8A8_UNORM_SRGB);
732 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_B8G8R8X8_TYPELESS);
733 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_B8G8R8X8_UNORM_SRGB);
734 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_Z_DF16);
735 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_Z_DF24);
736 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_Z_D24S8_INT);
737 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_YV12);
738 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R32G32B32A32_FLOAT);
739 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R16G16B16A16_FLOAT);
740 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R16G16B16A16_UNORM);
741 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R32G32_FLOAT);
742 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R10G10B10A2_UNORM);
743 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R8G8B8A8_SNORM);
744 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R16G16_FLOAT);
745 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R16G16_UNORM);
746 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R16G16_SNORM);
747 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R32_FLOAT);
748 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R8G8_SNORM);
749 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_R16_FLOAT);
750 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_D16_UNORM);
751 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_A8_UNORM);
752 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_BC1_UNORM);
753 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_BC2_UNORM);
754 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_BC3_UNORM);
755 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_B5G6R5_UNORM);
756 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_B5G5R5A1_UNORM);
757 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_B8G8R8A8_UNORM);
758 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_B8G8R8X8_UNORM);
759 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_BC4_UNORM);
760 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_DXFMT_BC5_UNORM);
761
762 SVGA_CASE_ID2STR(SVGA3D_DEVCAP_MAX);
763
764 default:
765 break;
766 }
767 return "UNKNOWN";
768}
769#endif /* defined(LOG_ENABLED) || (defined(IN_RING3) && defined(VBOX_WITH_VMSVGA3D)) */
770#undef SVGA_CASE_ID2STR
771
772
773#ifdef IN_RING3
774
775/**
776 * @interface_method_impl{PDMIDISPLAYPORT,pfnSetViewport}
777 */
778DECLCALLBACK(void) vmsvgaR3PortSetViewport(PPDMIDISPLAYPORT pInterface, uint32_t idScreen, uint32_t x, uint32_t y, uint32_t cx, uint32_t cy)
779{
780 PVGASTATECC pThisCC = RT_FROM_MEMBER(pInterface, VGASTATECC, IPort);
781 PVGASTATE pThis = PDMDEVINS_2_DATA(pThisCC->pDevIns, PVGASTATE);
782
783 Log(("vmsvgaPortSetViewPort: screen %d (%d,%d)(%d,%d)\n", idScreen, x, y, cx, cy));
784 VMSVGAVIEWPORT const OldViewport = pThis->svga.viewport;
785
786 /** @todo Test how it interacts with multiple screen objects. */
787 VMSVGASCREENOBJECT *pScreen = vmsvgaR3GetScreenObject(pThisCC, idScreen);
788 uint32_t const uWidth = pScreen ? pScreen->cWidth : 0;
789 uint32_t const uHeight = pScreen ? pScreen->cHeight : 0;
790
791 if (x < uWidth)
792 {
793 pThis->svga.viewport.x = x;
794 pThis->svga.viewport.cx = RT_MIN(cx, uWidth - x);
795 pThis->svga.viewport.xRight = x + pThis->svga.viewport.cx;
796 }
797 else
798 {
799 pThis->svga.viewport.x = uWidth;
800 pThis->svga.viewport.cx = 0;
801 pThis->svga.viewport.xRight = uWidth;
802 }
803 if (y < uHeight)
804 {
805 pThis->svga.viewport.y = y;
806 pThis->svga.viewport.cy = RT_MIN(cy, uHeight - y);
807 pThis->svga.viewport.yLowWC = uHeight - y - pThis->svga.viewport.cy;
808 pThis->svga.viewport.yHighWC = uHeight - y;
809 }
810 else
811 {
812 pThis->svga.viewport.y = uHeight;
813 pThis->svga.viewport.cy = 0;
814 pThis->svga.viewport.yLowWC = 0;
815 pThis->svga.viewport.yHighWC = 0;
816 }
817
818# ifdef VBOX_WITH_VMSVGA3D
819 /*
820 * Now inform the 3D backend.
821 */
822 if (pThis->svga.f3DEnabled)
823 vmsvga3dUpdateHostScreenViewport(pThisCC, idScreen, &OldViewport);
824# else
825 RT_NOREF(OldViewport);
826# endif
827}
828
829
830/**
831 * Updating screen information in API
832 *
833 * @param pThis The The shared VGA/VMSVGA instance data.
834 * @param pThisCC The VGA/VMSVGA state for ring-3.
835 */
836void vmsvgaR3VBVAResize(PVGASTATE pThis, PVGASTATECC pThisCC)
837{
838 int rc;
839
840 PVMSVGAR3STATE pSVGAState = pThisCC->svga.pSvgaR3State;
841
842 for (unsigned iScreen = 0; iScreen < RT_ELEMENTS(pSVGAState->aScreens); ++iScreen)
843 {
844 VMSVGASCREENOBJECT *pScreen = &pSVGAState->aScreens[iScreen];
845 if (!pScreen->fModified)
846 continue;
847
848 pScreen->fModified = false;
849
850 VBVAINFOVIEW view;
851 RT_ZERO(view);
852 view.u32ViewIndex = pScreen->idScreen;
853 // view.u32ViewOffset = 0;
854 view.u32ViewSize = pThis->vram_size;
855 view.u32MaxScreenSize = pThis->vram_size;
856
857 VBVAINFOSCREEN screen;
858 RT_ZERO(screen);
859 screen.u32ViewIndex = pScreen->idScreen;
860
861 if (pScreen->fDefined)
862 {
863 if ( pScreen->cWidth == VMSVGA_VAL_UNINITIALIZED
864 || pScreen->cHeight == VMSVGA_VAL_UNINITIALIZED
865 || pScreen->cBpp == VMSVGA_VAL_UNINITIALIZED)
866 {
867 Assert(pThis->svga.fGFBRegisters);
868 continue;
869 }
870
871 screen.i32OriginX = pScreen->xOrigin;
872 screen.i32OriginY = pScreen->yOrigin;
873 screen.u32StartOffset = pScreen->offVRAM;
874 screen.u32LineSize = pScreen->cbPitch;
875 screen.u32Width = pScreen->cWidth;
876 screen.u32Height = pScreen->cHeight;
877 screen.u16BitsPerPixel = pScreen->cBpp;
878 if (!(pScreen->fuScreen & SVGA_SCREEN_DEACTIVATE))
879 screen.u16Flags = VBVA_SCREEN_F_ACTIVE;
880 if (pScreen->fuScreen & SVGA_SCREEN_BLANKING)
881 screen.u16Flags |= VBVA_SCREEN_F_BLANK2;
882 }
883 else
884 {
885 /* Screen is destroyed. */
886 screen.u16Flags = VBVA_SCREEN_F_DISABLED;
887 }
888
889 void *pvVRAM = pScreen->pvScreenBitmap ? pScreen->pvScreenBitmap : pThisCC->pbVRam;
890 rc = pThisCC->pDrv->pfnVBVAResize(pThisCC->pDrv, &view, &screen, pvVRAM, /*fResetInputMapping=*/ true);
891 AssertRC(rc);
892 }
893}
894
895
896/**
897 * @interface_method_impl{PDMIDISPLAYPORT,pfnReportMonitorPositions}
898 *
899 * Used to update screen offsets (positions) since appearently vmwgfx fails to
900 * pass correct offsets thru FIFO.
901 */
902DECLCALLBACK(void) vmsvgaR3PortReportMonitorPositions(PPDMIDISPLAYPORT pInterface, uint32_t cPositions, PCRTPOINT paPositions)
903{
904 PVGASTATECC pThisCC = RT_FROM_MEMBER(pInterface, VGASTATECC, IPort);
905 PVGASTATE pThis = PDMDEVINS_2_DATA(pThisCC->pDevIns, PVGASTATE);
906 PVMSVGAR3STATE pSVGAState = pThisCC->svga.pSvgaR3State;
907
908 AssertReturnVoid(pSVGAState);
909
910 /* We assume cPositions is the # of outputs Xserver reports and paPositions is (-1, -1) for disabled monitors. */
911 cPositions = RT_MIN(cPositions, RT_ELEMENTS(pSVGAState->aScreens));
912 for (uint32_t i = 0; i < cPositions; ++i)
913 {
914 if ( pSVGAState->aScreens[i].xOrigin == paPositions[i].x
915 && pSVGAState->aScreens[i].yOrigin == paPositions[i].y)
916 continue;
917
918 if (pSVGAState->aScreens[i].xOrigin == -1)
919 continue;
920 if (pSVGAState->aScreens[i].yOrigin == -1)
921 continue;
922
923 pSVGAState->aScreens[i].xOrigin = paPositions[i].x;
924 pSVGAState->aScreens[i].yOrigin = paPositions[i].y;
925 pSVGAState->aScreens[i].fModified = true;
926 }
927
928 vmsvgaR3VBVAResize(pThis, pThisCC);
929}
930
931#endif /* IN_RING3 */
932
933/**
934 * Read port register
935 *
936 * @returns VBox status code.
937 * @param pDevIns The device instance.
938 * @param pThis The shared VGA/VMSVGA state.
939 * @param pu32 Where to store the read value
940 */
941static int vmsvgaReadPort(PPDMDEVINS pDevIns, PVGASTATE pThis, uint32_t *pu32)
942{
943#ifdef IN_RING3
944 PVGASTATER3 pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVGASTATECC);
945#endif
946 int rc = VINF_SUCCESS;
947 *pu32 = 0;
948
949 /* Rough index register validation. */
950 uint32_t idxReg = pThis->svga.u32IndexReg;
951#if !defined(IN_RING3) && defined(VBOX_STRICT)
952 ASSERT_GUEST_MSG_RETURN(idxReg < SVGA_SCRATCH_BASE + pThis->svga.cScratchRegion, ("idxReg=%#x\n", idxReg),
953 VINF_IOM_R3_IOPORT_READ);
954#else
955 ASSERT_GUEST_MSG_STMT_RETURN(idxReg < SVGA_SCRATCH_BASE + pThis->svga.cScratchRegion, ("idxReg=%#x\n", idxReg),
956 STAM_REL_COUNTER_INC(&pThis->svga.StatRegUnknownRd),
957 VINF_SUCCESS);
958#endif
959 RT_UNTRUSTED_VALIDATED_FENCE();
960
961 /* We must adjust the register number if we're in SVGA_ID_0 mode because the PALETTE range moved. */
962 if ( idxReg >= SVGA_REG_ID_0_TOP
963 && pThis->svga.u32SVGAId == SVGA_ID_0)
964 {
965 idxReg += SVGA_PALETTE_BASE - SVGA_REG_ID_0_TOP;
966 Log(("vmsvgaWritePort: SVGA_ID_0 reg adj %#x -> %#x\n", pThis->svga.u32IndexReg, idxReg));
967 }
968
969 switch (idxReg)
970 {
971 case SVGA_REG_ID:
972 STAM_REL_COUNTER_INC(&pThis->svga.StatRegIdRd);
973 *pu32 = pThis->svga.u32SVGAId;
974 break;
975
976 case SVGA_REG_ENABLE:
977 STAM_REL_COUNTER_INC(&pThis->svga.StatRegEnableRd);
978 *pu32 = pThis->svga.fEnabled;
979 break;
980
981 case SVGA_REG_WIDTH:
982 {
983 STAM_REL_COUNTER_INC(&pThis->svga.StatRegWidthRd);
984 if ( pThis->svga.fEnabled
985 && pThis->svga.uWidth != VMSVGA_VAL_UNINITIALIZED)
986 *pu32 = pThis->svga.uWidth;
987 else
988 {
989#ifndef IN_RING3
990 rc = VINF_IOM_R3_IOPORT_READ;
991#else
992 *pu32 = pThisCC->pDrv->cx;
993#endif
994 }
995 break;
996 }
997
998 case SVGA_REG_HEIGHT:
999 {
1000 STAM_REL_COUNTER_INC(&pThis->svga.StatRegHeightRd);
1001 if ( pThis->svga.fEnabled
1002 && pThis->svga.uHeight != VMSVGA_VAL_UNINITIALIZED)
1003 *pu32 = pThis->svga.uHeight;
1004 else
1005 {
1006#ifndef IN_RING3
1007 rc = VINF_IOM_R3_IOPORT_READ;
1008#else
1009 *pu32 = pThisCC->pDrv->cy;
1010#endif
1011 }
1012 break;
1013 }
1014
1015 case SVGA_REG_MAX_WIDTH:
1016 STAM_REL_COUNTER_INC(&pThis->svga.StatRegMaxWidthRd);
1017 *pu32 = pThis->svga.u32MaxWidth;
1018 break;
1019
1020 case SVGA_REG_MAX_HEIGHT:
1021 STAM_REL_COUNTER_INC(&pThis->svga.StatRegMaxHeightRd);
1022 *pu32 = pThis->svga.u32MaxHeight;
1023 break;
1024
1025 case SVGA_REG_DEPTH:
1026 /* This returns the color depth of the current mode. */
1027 STAM_REL_COUNTER_INC(&pThis->svga.StatRegDepthRd);
1028 switch (pThis->svga.uBpp)
1029 {
1030 case 15:
1031 case 16:
1032 case 24:
1033 *pu32 = pThis->svga.uBpp;
1034 break;
1035
1036 default:
1037 case 32:
1038 *pu32 = 24; /* The upper 8 bits are either alpha bits or not used. */
1039 break;
1040 }
1041 break;
1042
1043 case SVGA_REG_HOST_BITS_PER_PIXEL: /* (Deprecated) */
1044 STAM_REL_COUNTER_INC(&pThis->svga.StatRegHostBitsPerPixelRd);
1045 *pu32 = pThis->svga.uHostBpp;
1046 break;
1047
1048 case SVGA_REG_BITS_PER_PIXEL: /* Current bpp in the guest */
1049 STAM_REL_COUNTER_INC(&pThis->svga.StatRegBitsPerPixelRd);
1050 *pu32 = pThis->svga.uBpp;
1051 break;
1052
1053 case SVGA_REG_PSEUDOCOLOR:
1054 STAM_REL_COUNTER_INC(&pThis->svga.StatRegPsuedoColorRd);
1055 *pu32 = pThis->svga.uBpp == 8; /* See section 6 "Pseudocolor" in svga_interface.txt. */
1056 break;
1057
1058 case SVGA_REG_RED_MASK:
1059 case SVGA_REG_GREEN_MASK:
1060 case SVGA_REG_BLUE_MASK:
1061 {
1062 uint32_t uBpp;
1063
1064 if (pThis->svga.fEnabled)
1065 uBpp = pThis->svga.uBpp;
1066 else
1067 uBpp = pThis->svga.uHostBpp;
1068
1069 uint32_t u32RedMask, u32GreenMask, u32BlueMask;
1070 switch (uBpp)
1071 {
1072 case 8:
1073 u32RedMask = 0x07;
1074 u32GreenMask = 0x38;
1075 u32BlueMask = 0xc0;
1076 break;
1077
1078 case 15:
1079 u32RedMask = 0x0000001f;
1080 u32GreenMask = 0x000003e0;
1081 u32BlueMask = 0x00007c00;
1082 break;
1083
1084 case 16:
1085 u32RedMask = 0x0000001f;
1086 u32GreenMask = 0x000007e0;
1087 u32BlueMask = 0x0000f800;
1088 break;
1089
1090 case 24:
1091 case 32:
1092 default:
1093 u32RedMask = 0x00ff0000;
1094 u32GreenMask = 0x0000ff00;
1095 u32BlueMask = 0x000000ff;
1096 break;
1097 }
1098 switch (idxReg)
1099 {
1100 case SVGA_REG_RED_MASK:
1101 STAM_REL_COUNTER_INC(&pThis->svga.StatRegRedMaskRd);
1102 *pu32 = u32RedMask;
1103 break;
1104
1105 case SVGA_REG_GREEN_MASK:
1106 STAM_REL_COUNTER_INC(&pThis->svga.StatRegGreenMaskRd);
1107 *pu32 = u32GreenMask;
1108 break;
1109
1110 case SVGA_REG_BLUE_MASK:
1111 STAM_REL_COUNTER_INC(&pThis->svga.StatRegBlueMaskRd);
1112 *pu32 = u32BlueMask;
1113 break;
1114 }
1115 break;
1116 }
1117
1118 case SVGA_REG_BYTES_PER_LINE:
1119 {
1120 STAM_REL_COUNTER_INC(&pThis->svga.StatRegBytesPerLineRd);
1121 if ( pThis->svga.fEnabled
1122 && pThis->svga.cbScanline)
1123 *pu32 = pThis->svga.cbScanline;
1124 else
1125 {
1126#ifndef IN_RING3
1127 rc = VINF_IOM_R3_IOPORT_READ;
1128#else
1129 *pu32 = pThisCC->pDrv->cbScanline;
1130#endif
1131 }
1132 break;
1133 }
1134
1135 case SVGA_REG_VRAM_SIZE: /* VRAM size */
1136 STAM_REL_COUNTER_INC(&pThis->svga.StatRegVramSizeRd);
1137 *pu32 = pThis->vram_size;
1138 break;
1139
1140 case SVGA_REG_FB_START: /* Frame buffer physical address. */
1141 STAM_REL_COUNTER_INC(&pThis->svga.StatRegFbStartRd);
1142 Assert(pThis->GCPhysVRAM <= 0xffffffff);
1143 *pu32 = pThis->GCPhysVRAM;
1144 break;
1145
1146 case SVGA_REG_FB_OFFSET: /* Offset of the frame buffer in VRAM */
1147 STAM_REL_COUNTER_INC(&pThis->svga.StatRegFbOffsetRd);
1148 /* Always zero in our case. */
1149 *pu32 = 0;
1150 break;
1151
1152 case SVGA_REG_FB_SIZE: /* Frame buffer size */
1153 {
1154#ifndef IN_RING3
1155 rc = VINF_IOM_R3_IOPORT_READ;
1156#else
1157 STAM_REL_COUNTER_INC(&pThis->svga.StatRegFbSizeRd);
1158
1159 /* VMWare testcases want at least 4 MB in case the hardware is disabled. */
1160 if ( pThis->svga.fEnabled
1161 && pThis->svga.uHeight != VMSVGA_VAL_UNINITIALIZED)
1162 {
1163 /* Hardware enabled; return real framebuffer size .*/
1164 *pu32 = (uint32_t)pThis->svga.uHeight * pThis->svga.cbScanline;
1165 }
1166 else
1167 *pu32 = RT_MAX(0x100000, (uint32_t)pThisCC->pDrv->cy * pThisCC->pDrv->cbScanline);
1168
1169 *pu32 = RT_MIN(pThis->vram_size, *pu32);
1170 Log(("h=%d w=%d bpp=%d\n", pThisCC->pDrv->cy, pThisCC->pDrv->cx, pThisCC->pDrv->cBits));
1171#endif
1172 break;
1173 }
1174
1175 case SVGA_REG_CAPABILITIES:
1176 STAM_REL_COUNTER_INC(&pThis->svga.StatRegCapabilitesRd);
1177 *pu32 = pThis->svga.u32DeviceCaps;
1178 break;
1179
1180 case SVGA_REG_MEM_START: /* FIFO start */
1181 STAM_REL_COUNTER_INC(&pThis->svga.StatRegMemStartRd);
1182 Assert(pThis->svga.GCPhysFIFO <= 0xffffffff);
1183 *pu32 = pThis->svga.GCPhysFIFO;
1184 break;
1185
1186 case SVGA_REG_MEM_SIZE: /* FIFO size */
1187 STAM_REL_COUNTER_INC(&pThis->svga.StatRegMemSizeRd);
1188 *pu32 = pThis->svga.cbFIFO;
1189 break;
1190
1191 case SVGA_REG_CONFIG_DONE: /* Set when memory area configured */
1192 STAM_REL_COUNTER_INC(&pThis->svga.StatRegConfigDoneRd);
1193 *pu32 = pThis->svga.fConfigured;
1194 break;
1195
1196 case SVGA_REG_SYNC: /* See "FIFO Synchronization Registers" */
1197 STAM_REL_COUNTER_INC(&pThis->svga.StatRegSyncRd);
1198 *pu32 = 0;
1199 break;
1200
1201 case SVGA_REG_BUSY: /* See "FIFO Synchronization Registers" */
1202 STAM_REL_COUNTER_INC(&pThis->svga.StatRegBusyRd);
1203 if (pThis->svga.fBusy)
1204 {
1205#ifndef IN_RING3
1206 /* Go to ring-3 and halt the CPU. */
1207 rc = VINF_IOM_R3_IOPORT_READ;
1208 RT_NOREF(pDevIns);
1209 break;
1210#else
1211# if defined(VMSVGA_USE_EMT_HALT_CODE)
1212 /* The guest is basically doing a HLT via the device here, but with
1213 a special wake up condition on FIFO completion. */
1214 PVMSVGAR3STATE pSVGAState = pThisCC->svga.pSvgaR3State;
1215 STAM_REL_PROFILE_START(&pSVGAState->StatBusyDelayEmts, EmtDelay);
1216 PVM pVM = PDMDevHlpGetVM(pDevIns);
1217 VMCPUID idCpu = PDMDevHlpGetCurrentCpuId(pDevIns);
1218 VMCPUSET_ATOMIC_ADD(&pSVGAState->BusyDelayedEmts, idCpu);
1219 ASMAtomicIncU32(&pSVGAState->cBusyDelayedEmts);
1220 if (pThis->svga.fBusy)
1221 {
1222 PDMDevHlpCritSectLeave(pDevIns, &pThis->CritSect); /* hack around lock order issue. */
1223 rc = VMR3WaitForDeviceReady(pVM, idCpu);
1224 PDMDevHlpCritSectEnter(pDevIns, &pThis->CritSect, VERR_IGNORED);
1225 }
1226 ASMAtomicDecU32(&pSVGAState->cBusyDelayedEmts);
1227 VMCPUSET_ATOMIC_DEL(&pSVGAState->BusyDelayedEmts, idCpu);
1228# else
1229
1230 /* Delay the EMT a bit so the FIFO and others can get some work done.
1231 This used to be a crude 50 ms sleep. The current code tries to be
1232 more efficient, but the consept is still very crude. */
1233 PVMSVGAR3STATE pSVGAState = pThisCC->svga.pSvgaR3State;
1234 STAM_REL_PROFILE_START(&pSVGAState->StatBusyDelayEmts, EmtDelay);
1235 RTThreadYield();
1236 if (pThis->svga.fBusy)
1237 {
1238 uint32_t cRefs = ASMAtomicIncU32(&pSVGAState->cBusyDelayedEmts);
1239
1240 if (pThis->svga.fBusy && cRefs == 1)
1241 RTSemEventMultiReset(pSVGAState->hBusyDelayedEmts);
1242 if (pThis->svga.fBusy)
1243 {
1244 /** @todo If this code is going to stay, we need to call into the halt/wait
1245 * code in VMEmt.cpp here, otherwise all kind of EMT interaction will
1246 * suffer when the guest is polling on a busy FIFO. */
1247 uint64_t cNsMaxWait = TMVirtualSyncGetNsToDeadline(PDMDevHlpGetVM(pDevIns));
1248 if (cNsMaxWait >= RT_NS_100US)
1249 RTSemEventMultiWaitEx(pSVGAState->hBusyDelayedEmts,
1250 RTSEMWAIT_FLAGS_NANOSECS | RTSEMWAIT_FLAGS_RELATIVE | RTSEMWAIT_FLAGS_NORESUME,
1251 RT_MIN(cNsMaxWait, RT_NS_10MS));
1252 }
1253
1254 ASMAtomicDecU32(&pSVGAState->cBusyDelayedEmts);
1255 }
1256 STAM_REL_PROFILE_STOP(&pSVGAState->StatBusyDelayEmts, EmtDelay);
1257# endif
1258 *pu32 = pThis->svga.fBusy != 0;
1259#endif
1260 }
1261 else
1262 *pu32 = false;
1263 break;
1264
1265 case SVGA_REG_GUEST_ID: /* Set guest OS identifier */
1266 STAM_REL_COUNTER_INC(&pThis->svga.StatRegGuestIdRd);
1267 *pu32 = pThis->svga.u32GuestId;
1268 break;
1269
1270 case SVGA_REG_SCRATCH_SIZE: /* Number of scratch registers */
1271 STAM_REL_COUNTER_INC(&pThis->svga.StatRegScratchSizeRd);
1272 *pu32 = pThis->svga.cScratchRegion;
1273 break;
1274
1275 case SVGA_REG_MEM_REGS: /* Number of FIFO registers */
1276 STAM_REL_COUNTER_INC(&pThis->svga.StatRegMemRegsRd);
1277 *pu32 = SVGA_FIFO_NUM_REGS;
1278 break;
1279
1280 case SVGA_REG_PITCHLOCK: /* Fixed pitch for all modes */
1281 STAM_REL_COUNTER_INC(&pThis->svga.StatRegPitchLockRd);
1282 *pu32 = pThis->svga.u32PitchLock;
1283 break;
1284
1285 case SVGA_REG_IRQMASK: /* Interrupt mask */
1286 STAM_REL_COUNTER_INC(&pThis->svga.StatRegIrqMaskRd);
1287 *pu32 = pThis->svga.u32IrqMask;
1288 break;
1289
1290 /* See "Guest memory regions" below. */
1291 case SVGA_REG_GMR_ID:
1292 STAM_REL_COUNTER_INC(&pThis->svga.StatRegGmrIdRd);
1293 *pu32 = pThis->svga.u32CurrentGMRId;
1294 break;
1295
1296 case SVGA_REG_GMR_DESCRIPTOR:
1297 STAM_REL_COUNTER_INC(&pThis->svga.StatRegWriteOnlyRd);
1298 /* Write only */
1299 *pu32 = 0;
1300 break;
1301
1302 case SVGA_REG_GMR_MAX_IDS:
1303 STAM_REL_COUNTER_INC(&pThis->svga.StatRegGmrMaxIdsRd);
1304 *pu32 = pThis->svga.cGMR;
1305 break;
1306
1307 case SVGA_REG_GMR_MAX_DESCRIPTOR_LENGTH:
1308 STAM_REL_COUNTER_INC(&pThis->svga.StatRegGmrMaxDescriptorLengthRd);
1309 *pu32 = VMSVGA_MAX_GMR_PAGES;
1310 break;
1311
1312 case SVGA_REG_TRACES: /* Enable trace-based updates even when FIFO is on */
1313 STAM_REL_COUNTER_INC(&pThis->svga.StatRegTracesRd);
1314 *pu32 = pThis->svga.fTraces;
1315 break;
1316
1317 case SVGA_REG_GMRS_MAX_PAGES: /* Maximum number of 4KB pages for all GMRs */
1318 STAM_REL_COUNTER_INC(&pThis->svga.StatRegGmrsMaxPagesRd);
1319 *pu32 = VMSVGA_MAX_GMR_PAGES;
1320 break;
1321
1322 case SVGA_REG_MEMORY_SIZE: /* Total dedicated device memory excluding FIFO */
1323 STAM_REL_COUNTER_INC(&pThis->svga.StatRegMemorySizeRd);
1324 *pu32 = VMSVGA_SURFACE_SIZE;
1325 break;
1326
1327 case SVGA_REG_TOP: /* Must be 1 more than the last register */
1328 STAM_REL_COUNTER_INC(&pThis->svga.StatRegTopRd);
1329 break;
1330
1331 /* Mouse cursor support. */
1332 case SVGA_REG_CURSOR_ID:
1333 STAM_REL_COUNTER_INC(&pThis->svga.StatRegCursorIdRd);
1334 *pu32 = pThis->svga.uCursorID;
1335 break;
1336
1337 case SVGA_REG_CURSOR_X:
1338 STAM_REL_COUNTER_INC(&pThis->svga.StatRegCursorXRd);
1339 *pu32 = pThis->svga.uCursorX;
1340 break;
1341
1342 case SVGA_REG_CURSOR_Y:
1343 STAM_REL_COUNTER_INC(&pThis->svga.StatRegCursorYRd);
1344 *pu32 = pThis->svga.uCursorY;
1345 break;
1346
1347 case SVGA_REG_CURSOR_ON:
1348 STAM_REL_COUNTER_INC(&pThis->svga.StatRegCursorOnRd);
1349 *pu32 = pThis->svga.uCursorOn;
1350 break;
1351
1352 /* Legacy multi-monitor support */
1353 case SVGA_REG_NUM_GUEST_DISPLAYS:/* Number of guest displays in X/Y direction */
1354 STAM_REL_COUNTER_INC(&pThis->svga.StatRegNumGuestDisplaysRd);
1355 *pu32 = 1;
1356 break;
1357
1358 case SVGA_REG_DISPLAY_ID: /* Display ID for the following display attributes */
1359 STAM_REL_COUNTER_INC(&pThis->svga.StatRegDisplayIdRd);
1360 *pu32 = 0;
1361 break;
1362
1363 case SVGA_REG_DISPLAY_IS_PRIMARY:/* Whether this is a primary display */
1364 STAM_REL_COUNTER_INC(&pThis->svga.StatRegDisplayIsPrimaryRd);
1365 *pu32 = 0;
1366 break;
1367
1368 case SVGA_REG_DISPLAY_POSITION_X:/* The display position x */
1369 STAM_REL_COUNTER_INC(&pThis->svga.StatRegDisplayPositionXRd);
1370 *pu32 = 0;
1371 break;
1372
1373 case SVGA_REG_DISPLAY_POSITION_Y:/* The display position y */
1374 STAM_REL_COUNTER_INC(&pThis->svga.StatRegDisplayPositionYRd);
1375 *pu32 = 0;
1376 break;
1377
1378 case SVGA_REG_DISPLAY_WIDTH: /* The display's width */
1379 STAM_REL_COUNTER_INC(&pThis->svga.StatRegDisplayWidthRd);
1380 *pu32 = pThis->svga.uWidth;
1381 break;
1382
1383 case SVGA_REG_DISPLAY_HEIGHT: /* The display's height */
1384 STAM_REL_COUNTER_INC(&pThis->svga.StatRegDisplayHeightRd);
1385 *pu32 = pThis->svga.uHeight;
1386 break;
1387
1388 case SVGA_REG_NUM_DISPLAYS: /* (Deprecated) */
1389 STAM_REL_COUNTER_INC(&pThis->svga.StatRegNumDisplaysRd);
1390 /* We must return something sensible here otherwise the Linux driver
1391 will take a legacy code path without 3d support. This number also
1392 limits how many screens Linux guests will allow. */
1393 *pu32 = pThis->cMonitors;
1394 break;
1395
1396 /*
1397 * SVGA_CAP_GBOBJECTS+ registers.
1398 */
1399 case SVGA_REG_COMMAND_LOW:
1400 /* Lower 32 bits of command buffer physical address. */
1401 STAM_REL_COUNTER_INC(&pThis->svga.StatRegCommandLowRd);
1402 *pu32 = pThis->svga.u32RegCommandLow;
1403 break;
1404
1405 case SVGA_REG_COMMAND_HIGH:
1406 /* Upper 32 bits of command buffer PA. */
1407 STAM_REL_COUNTER_INC(&pThis->svga.StatRegCommandHighRd);
1408 *pu32 = pThis->svga.u32RegCommandHigh;
1409 break;
1410
1411 case SVGA_REG_MAX_PRIMARY_BOUNDING_BOX_MEM:
1412 /* Max primary (screen) memory. */
1413 STAM_REL_COUNTER_INC(&pThis->svga.StatRegMaxPrimBBMemRd);
1414 *pu32 = pThis->vram_size; /** @todo Maybe half VRAM? */
1415 break;
1416
1417 case SVGA_REG_SUGGESTED_GBOBJECT_MEM_SIZE_KB:
1418 /* Suggested limit on mob mem (i.e. size of the guest mapped VRAM in KB) */
1419 STAM_REL_COUNTER_INC(&pThis->svga.StatRegGBMemSizeRd);
1420 *pu32 = pThis->vram_size / 1024;
1421 break;
1422
1423 case SVGA_REG_DEV_CAP:
1424 /* Write dev cap index, read value */
1425 STAM_REL_COUNTER_INC(&pThis->svga.StatRegDevCapRd);
1426 if (pThis->svga.u32DevCapIndex < RT_ELEMENTS(pThis->svga.au32DevCaps))
1427 {
1428 RT_UNTRUSTED_VALIDATED_FENCE();
1429 *pu32 = pThis->svga.au32DevCaps[pThis->svga.u32DevCapIndex];
1430 }
1431 else
1432 *pu32 = 0;
1433 break;
1434
1435 case SVGA_REG_CMD_PREPEND_LOW:
1436 STAM_REL_COUNTER_INC(&pThis->svga.StatRegCmdPrependLowRd);
1437 *pu32 = 0; /* Not supported. */
1438 break;
1439
1440 case SVGA_REG_iCMD_PREPEND_HIGH:
1441 STAM_REL_COUNTER_INC(&pThis->svga.StatRegCmdPrependHighRd);
1442 *pu32 = 0; /* Not supported. */
1443 break;
1444
1445 case SVGA_REG_SCREENTARGET_MAX_WIDTH:
1446 STAM_REL_COUNTER_INC(&pThis->svga.StatRegScrnTgtMaxWidthRd);
1447 *pu32 = pThis->svga.u32MaxWidth;
1448 break;
1449
1450 case SVGA_REG_SCREENTARGET_MAX_HEIGHT:
1451 STAM_REL_COUNTER_INC(&pThis->svga.StatRegScrnTgtMaxHeightRd);
1452 *pu32 = pThis->svga.u32MaxHeight;
1453 break;
1454
1455 case SVGA_REG_MOB_MAX_SIZE:
1456 /* Essentially the max texture size */
1457 STAM_REL_COUNTER_INC(&pThis->svga.StatRegMobMaxSizeRd);
1458 *pu32 = _128M; /** @todo Some actual value. Probably the mapped VRAM size. */
1459 break;
1460
1461 default:
1462 {
1463 uint32_t offReg;
1464 if ((offReg = idxReg - SVGA_SCRATCH_BASE) < pThis->svga.cScratchRegion)
1465 {
1466 STAM_REL_COUNTER_INC(&pThis->svga.StatRegScratchRd);
1467 RT_UNTRUSTED_VALIDATED_FENCE();
1468 *pu32 = pThis->svga.au32ScratchRegion[offReg];
1469 }
1470 else if ((offReg = idxReg - SVGA_PALETTE_BASE) < (uint32_t)SVGA_NUM_PALETTE_REGS)
1471 {
1472 /* Note! Using last_palette rather than palette here to preserve the VGA one. */
1473 STAM_REL_COUNTER_INC(&pThis->svga.StatRegPaletteRd);
1474 RT_UNTRUSTED_VALIDATED_FENCE();
1475 uint32_t u32 = pThis->last_palette[offReg / 3];
1476 switch (offReg % 3)
1477 {
1478 case 0: *pu32 = (u32 >> 16) & 0xff; break; /* red */
1479 case 1: *pu32 = (u32 >> 8) & 0xff; break; /* green */
1480 case 2: *pu32 = u32 & 0xff; break; /* blue */
1481 }
1482 }
1483 else
1484 {
1485#if !defined(IN_RING3) && defined(VBOX_STRICT)
1486 rc = VINF_IOM_R3_IOPORT_READ;
1487#else
1488 STAM_REL_COUNTER_INC(&pThis->svga.StatRegUnknownRd);
1489
1490 /* Do not assert. The guest might be reading all registers. */
1491 LogFunc(("Unknown reg=%#x\n", idxReg));
1492#endif
1493 }
1494 break;
1495 }
1496 }
1497 Log(("vmsvgaReadPort index=%s (%d) val=%#x rc=%x\n", vmsvgaIndexToString(pThis, idxReg), idxReg, *pu32, rc));
1498 return rc;
1499}
1500
1501#ifdef IN_RING3
1502/**
1503 * Apply the current resolution settings to change the video mode.
1504 *
1505 * @returns VBox status code.
1506 * @param pThis The shared VGA state.
1507 * @param pThisCC The ring-3 VGA state.
1508 */
1509int vmsvgaR3ChangeMode(PVGASTATE pThis, PVGASTATECC pThisCC)
1510{
1511 /* Always do changemode on FIFO thread. */
1512 Assert(RTThreadSelf() == pThisCC->svga.pFIFOIOThread->Thread);
1513
1514 PVMSVGAR3STATE pSVGAState = pThisCC->svga.pSvgaR3State;
1515
1516 pThisCC->pDrv->pfnLFBModeChange(pThisCC->pDrv, true);
1517
1518 if (pThis->svga.fGFBRegisters)
1519 {
1520 /* "For backwards compatibility, when the GFB mode registers (WIDTH,
1521 * HEIGHT, PITCHLOCK, BITS_PER_PIXEL) are modified, the SVGA device
1522 * deletes all screens other than screen #0, and redefines screen
1523 * #0 according to the specified mode. Drivers that use
1524 * SVGA_CMD_DEFINE_SCREEN should destroy or redefine screen #0."
1525 */
1526
1527 VMSVGASCREENOBJECT *pScreen = &pSVGAState->aScreens[0];
1528 pScreen->fDefined = true;
1529 pScreen->fModified = true;
1530 pScreen->fuScreen = SVGA_SCREEN_MUST_BE_SET | SVGA_SCREEN_IS_PRIMARY;
1531 pScreen->idScreen = 0;
1532 pScreen->xOrigin = 0;
1533 pScreen->yOrigin = 0;
1534 pScreen->offVRAM = 0;
1535 pScreen->cbPitch = pThis->svga.cbScanline;
1536 pScreen->cWidth = pThis->svga.uWidth;
1537 pScreen->cHeight = pThis->svga.uHeight;
1538 pScreen->cBpp = pThis->svga.uBpp;
1539
1540 for (unsigned iScreen = 1; iScreen < RT_ELEMENTS(pSVGAState->aScreens); ++iScreen)
1541 {
1542 /* Delete screen. */
1543 pScreen = &pSVGAState->aScreens[iScreen];
1544 if (pScreen->fDefined)
1545 {
1546 pScreen->fModified = true;
1547 pScreen->fDefined = false;
1548 }
1549 }
1550 }
1551 else
1552 {
1553 /* "If Screen Objects are supported, they can be used to fully
1554 * replace the functionality provided by the framebuffer registers
1555 * (SVGA_REG_WIDTH, HEIGHT, etc.) and by SVGA_CAP_DISPLAY_TOPOLOGY."
1556 */
1557 pThis->svga.uWidth = VMSVGA_VAL_UNINITIALIZED;
1558 pThis->svga.uHeight = VMSVGA_VAL_UNINITIALIZED;
1559 pThis->svga.uBpp = pThis->svga.uHostBpp;
1560 }
1561
1562 vmsvgaR3VBVAResize(pThis, pThisCC);
1563
1564 /* Last stuff. For the VGA device screenshot. */
1565 pThis->last_bpp = pSVGAState->aScreens[0].cBpp;
1566 pThis->last_scr_width = pSVGAState->aScreens[0].cWidth;
1567 pThis->last_scr_height = pSVGAState->aScreens[0].cHeight;
1568 pThis->last_width = pSVGAState->aScreens[0].cWidth;
1569 pThis->last_height = pSVGAState->aScreens[0].cHeight;
1570
1571 /* vmsvgaPortSetViewPort not called after state load; set sensible defaults. */
1572 if ( pThis->svga.viewport.cx == 0
1573 && pThis->svga.viewport.cy == 0)
1574 {
1575 pThis->svga.viewport.cx = pSVGAState->aScreens[0].cWidth;
1576 pThis->svga.viewport.xRight = pSVGAState->aScreens[0].cWidth;
1577 pThis->svga.viewport.cy = pSVGAState->aScreens[0].cHeight;
1578 pThis->svga.viewport.yHighWC = pSVGAState->aScreens[0].cHeight;
1579 pThis->svga.viewport.yLowWC = 0;
1580 }
1581
1582 return VINF_SUCCESS;
1583}
1584
1585int vmsvgaR3UpdateScreen(PVGASTATECC pThisCC, VMSVGASCREENOBJECT *pScreen, int x, int y, int w, int h)
1586{
1587 VBVACMDHDR cmd;
1588 cmd.x = (int16_t)(pScreen->xOrigin + x);
1589 cmd.y = (int16_t)(pScreen->yOrigin + y);
1590 cmd.w = (uint16_t)w;
1591 cmd.h = (uint16_t)h;
1592
1593 pThisCC->pDrv->pfnVBVAUpdateBegin(pThisCC->pDrv, pScreen->idScreen);
1594 pThisCC->pDrv->pfnVBVAUpdateProcess(pThisCC->pDrv, pScreen->idScreen, &cmd, sizeof(cmd));
1595 pThisCC->pDrv->pfnVBVAUpdateEnd(pThisCC->pDrv, pScreen->idScreen,
1596 pScreen->xOrigin + x, pScreen->yOrigin + y, w, h);
1597
1598 return VINF_SUCCESS;
1599}
1600
1601#endif /* IN_RING3 */
1602#if defined(IN_RING0) || defined(IN_RING3)
1603
1604/**
1605 * Safely updates the SVGA_FIFO_BUSY register (in shared memory).
1606 *
1607 * @param pThis The shared VGA/VMSVGA instance data.
1608 * @param pThisCC The VGA/VMSVGA state for the current context.
1609 * @param fState The busy state.
1610 */
1611DECLINLINE(void) vmsvgaHCSafeFifoBusyRegUpdate(PVGASTATE pThis, PVGASTATECC pThisCC, bool fState)
1612{
1613 ASMAtomicWriteU32(&pThisCC->svga.pau32FIFO[SVGA_FIFO_BUSY], fState);
1614
1615 if (RT_UNLIKELY(fState != (pThis->svga.fBusy != 0)))
1616 {
1617 /* Race / unfortunately scheduling. Highly unlikly. */
1618 uint32_t cLoops = 64;
1619 do
1620 {
1621 ASMNopPause();
1622 fState = (pThis->svga.fBusy != 0);
1623 ASMAtomicWriteU32(&pThisCC->svga.pau32FIFO[SVGA_FIFO_BUSY], fState != 0);
1624 } while (cLoops-- > 0 && fState != (pThis->svga.fBusy != 0));
1625 }
1626}
1627
1628
1629/**
1630 * Update the scanline pitch in response to the guest changing mode
1631 * width/bpp.
1632 *
1633 * @param pThis The shared VGA/VMSVGA state.
1634 * @param pThisCC The VGA/VMSVGA state for the current context.
1635 */
1636DECLINLINE(void) vmsvgaHCUpdatePitch(PVGASTATE pThis, PVGASTATECC pThisCC)
1637{
1638 uint32_t RT_UNTRUSTED_VOLATILE_GUEST *pFIFO = pThisCC->svga.pau32FIFO;
1639 uint32_t uFifoPitchLock = pFIFO[SVGA_FIFO_PITCHLOCK];
1640 uint32_t uRegPitchLock = pThis->svga.u32PitchLock;
1641 uint32_t uFifoMin = pFIFO[SVGA_FIFO_MIN];
1642
1643 /* The SVGA_FIFO_PITCHLOCK register is only valid if SVGA_FIFO_MIN points past
1644 * it. If SVGA_FIFO_MIN is small, there may well be data at the SVGA_FIFO_PITCHLOCK
1645 * location but it has a different meaning.
1646 */
1647 if ((uFifoMin / sizeof(uint32_t)) <= SVGA_FIFO_PITCHLOCK)
1648 uFifoPitchLock = 0;
1649
1650 /* Sanitize values. */
1651 if ((uFifoPitchLock < 200) || (uFifoPitchLock > 32768))
1652 uFifoPitchLock = 0;
1653 if ((uRegPitchLock < 200) || (uRegPitchLock > 32768))
1654 uRegPitchLock = 0;
1655
1656 /* Prefer the register value to the FIFO value.*/
1657 if (uRegPitchLock)
1658 pThis->svga.cbScanline = uRegPitchLock;
1659 else if (uFifoPitchLock)
1660 pThis->svga.cbScanline = uFifoPitchLock;
1661 else
1662 pThis->svga.cbScanline = pThis->svga.uWidth * (RT_ALIGN(pThis->svga.uBpp, 8) / 8);
1663
1664 if ((uFifoMin / sizeof(uint32_t)) <= SVGA_FIFO_PITCHLOCK)
1665 pThis->svga.u32PitchLock = pThis->svga.cbScanline;
1666}
1667
1668#endif /* IN_RING0 || IN_RING3 */
1669
1670#ifdef IN_RING3
1671
1672/**
1673 * Sends cursor position and visibility information from legacy
1674 * SVGA registers to the front-end.
1675 */
1676static void vmsvgaR3RegUpdateCursor(PVGASTATECC pThisCC, PVGASTATE pThis, uint32_t uCursorOn)
1677{
1678 /*
1679 * Writing the X/Y/ID registers does not trigger changes; only writing the
1680 * SVGA_REG_CURSOR_ON register does. That minimizes the overhead.
1681 * We boldly assume that guests aren't stupid and aren't writing the CURSOR_ON
1682 * register if they don't have to.
1683 */
1684 uint32_t x, y, idScreen;
1685 uint32_t fFlags = VBVA_CURSOR_VALID_DATA;
1686
1687 x = pThis->svga.uCursorX;
1688 y = pThis->svga.uCursorY;
1689 idScreen = SVGA_ID_INVALID; /* The old register interface is single screen only. */
1690
1691 /* The original values for SVGA_REG_CURSOR_ON were off (0) and on (1); later, the values
1692 * were extended as follows:
1693 *
1694 * SVGA_CURSOR_ON_HIDE 0
1695 * SVGA_CURSOR_ON_SHOW 1
1696 * SVGA_CURSOR_ON_REMOVE_FROM_FB 2 - cursor on but not in the framebuffer
1697 * SVGA_CURSOR_ON_RESTORE_TO_FB 3 - cursor on, possibly in the framebuffer
1698 *
1699 * Since we never draw the cursor into the guest's framebuffer, we do not need to
1700 * distinguish between the non-zero values but still remember them.
1701 */
1702 if (RT_BOOL(pThis->svga.uCursorOn) != RT_BOOL(uCursorOn))
1703 {
1704 LogRel2(("vmsvgaR3RegUpdateCursor: uCursorOn %d prev CursorOn %d (%d,%d)\n", uCursorOn, pThis->svga.uCursorOn, x, y));
1705 pThisCC->pDrv->pfnVBVAMousePointerShape(pThisCC->pDrv, RT_BOOL(uCursorOn), false, 0, 0, 0, 0, NULL);
1706 }
1707 pThis->svga.uCursorOn = uCursorOn;
1708 pThisCC->pDrv->pfnVBVAReportCursorPosition(pThisCC->pDrv, fFlags, idScreen, x, y);
1709}
1710
1711#endif /* IN_RING3 */
1712
1713
1714/**
1715 * Write port register
1716 *
1717 * @returns Strict VBox status code.
1718 * @param pDevIns The device instance.
1719 * @param pThis The shared VGA/VMSVGA state.
1720 * @param pThisCC The VGA/VMSVGA state for the current context.
1721 * @param u32 Value to write
1722 */
1723static VBOXSTRICTRC vmsvgaWritePort(PPDMDEVINS pDevIns, PVGASTATE pThis, PVGASTATECC pThisCC, uint32_t u32)
1724{
1725#ifdef IN_RING3
1726 PVMSVGAR3STATE pSVGAState = pThisCC->svga.pSvgaR3State;
1727#endif
1728 VBOXSTRICTRC rc = VINF_SUCCESS;
1729 RT_NOREF(pThisCC);
1730
1731 /* Rough index register validation. */
1732 uint32_t idxReg = pThis->svga.u32IndexReg;
1733#if !defined(IN_RING3) && defined(VBOX_STRICT)
1734 ASSERT_GUEST_MSG_RETURN(idxReg < SVGA_SCRATCH_BASE + pThis->svga.cScratchRegion, ("idxReg=%#x\n", idxReg),
1735 VINF_IOM_R3_IOPORT_WRITE);
1736#else
1737 ASSERT_GUEST_MSG_STMT_RETURN(idxReg < SVGA_SCRATCH_BASE + pThis->svga.cScratchRegion, ("idxReg=%#x\n", idxReg),
1738 STAM_REL_COUNTER_INC(&pThis->svga.StatRegUnknownWr),
1739 VINF_SUCCESS);
1740#endif
1741 RT_UNTRUSTED_VALIDATED_FENCE();
1742
1743 /* We must adjust the register number if we're in SVGA_ID_0 mode because the PALETTE range moved. */
1744 if ( idxReg >= SVGA_REG_ID_0_TOP
1745 && pThis->svga.u32SVGAId == SVGA_ID_0)
1746 {
1747 idxReg += SVGA_PALETTE_BASE - SVGA_REG_ID_0_TOP;
1748 Log(("vmsvgaWritePort: SVGA_ID_0 reg adj %#x -> %#x\n", pThis->svga.u32IndexReg, idxReg));
1749 }
1750#ifdef LOG_ENABLED
1751 if (idxReg != SVGA_REG_DEV_CAP)
1752 Log(("vmsvgaWritePort index=%s (%d) val=%#x\n", vmsvgaIndexToString(pThis, idxReg), idxReg, u32));
1753 else
1754 Log(("vmsvgaWritePort index=%s (%d) val=%s (%d)\n", vmsvgaIndexToString(pThis, idxReg), idxReg, vmsvgaDevCapIndexToString((SVGA3dDevCapIndex)u32), u32));
1755#endif
1756 /* Check if the guest uses legacy registers. See vmsvgaR3ChangeMode */
1757 switch (idxReg)
1758 {
1759 case SVGA_REG_WIDTH:
1760 case SVGA_REG_HEIGHT:
1761 case SVGA_REG_PITCHLOCK:
1762 case SVGA_REG_BITS_PER_PIXEL:
1763 pThis->svga.fGFBRegisters = true;
1764 break;
1765 default:
1766 break;
1767 }
1768
1769 switch (idxReg)
1770 {
1771 case SVGA_REG_ID:
1772 STAM_REL_COUNTER_INC(&pThis->svga.StatRegIdWr);
1773 if ( u32 == SVGA_ID_0
1774 || u32 == SVGA_ID_1
1775 || u32 == SVGA_ID_2)
1776 pThis->svga.u32SVGAId = u32;
1777 else
1778 PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS, "Trying to set SVGA_REG_ID to %#x (%d)\n", u32, u32);
1779 break;
1780
1781 case SVGA_REG_ENABLE:
1782 STAM_REL_COUNTER_INC(&pThis->svga.StatRegEnableWr);
1783#ifdef IN_RING3
1784 if ( (u32 & SVGA_REG_ENABLE_ENABLE)
1785 && pThis->svga.fEnabled == false)
1786 {
1787 /* Make a backup copy of the first 512kb in order to save font data etc. */
1788 /** @todo should probably swap here, rather than copy + zero */
1789 memcpy(pThisCC->svga.pbVgaFrameBufferR3, pThisCC->pbVRam, VMSVGA_VGA_FB_BACKUP_SIZE);
1790 memset(pThisCC->pbVRam, 0, VMSVGA_VGA_FB_BACKUP_SIZE);
1791 }
1792
1793 pThis->svga.fEnabled = u32;
1794 if (pThis->svga.fEnabled)
1795 {
1796 if ( pThis->svga.uWidth == VMSVGA_VAL_UNINITIALIZED
1797 && pThis->svga.uHeight == VMSVGA_VAL_UNINITIALIZED)
1798 {
1799 /* Keep the current mode. */
1800 pThis->svga.uWidth = pThisCC->pDrv->cx;
1801 pThis->svga.uHeight = pThisCC->pDrv->cy;
1802 pThis->svga.uBpp = (pThisCC->pDrv->cBits + 7) & ~7;
1803 }
1804
1805 if ( pThis->svga.uWidth != VMSVGA_VAL_UNINITIALIZED
1806 && pThis->svga.uHeight != VMSVGA_VAL_UNINITIALIZED)
1807 ASMAtomicOrU32(&pThis->svga.u32ActionFlags, VMSVGA_ACTION_CHANGEMODE);
1808# ifdef LOG_ENABLED
1809 uint32_t *pFIFO = pThisCC->svga.pau32FIFO;
1810 Log(("configured=%d busy=%d\n", pThis->svga.fConfigured, pFIFO[SVGA_FIFO_BUSY]));
1811 Log(("next %x stop %x\n", pFIFO[SVGA_FIFO_NEXT_CMD], pFIFO[SVGA_FIFO_STOP]));
1812# endif
1813
1814 /* Disable or enable dirty page tracking according to the current fTraces value. */
1815 vmsvgaR3SetTraces(pDevIns, pThis, !!pThis->svga.fTraces);
1816
1817 /* bird: Whatever this is was added to make screenshot work, ask sunlover should explain... */
1818 for (uint32_t idScreen = 0; idScreen < pThis->cMonitors; ++idScreen)
1819 pThisCC->pDrv->pfnVBVAEnable(pThisCC->pDrv, idScreen, NULL /*pHostFlags*/);
1820
1821 /* Make the cursor visible again as needed. */
1822 if (pSVGAState->Cursor.fActive)
1823 pThisCC->pDrv->pfnVBVAMousePointerShape(pThisCC->pDrv, true /*fVisible*/, false, 0, 0, 0, 0, NULL);
1824 }
1825 else
1826 {
1827 /* Make sure the cursor is off. */
1828 if (pSVGAState->Cursor.fActive)
1829 pThisCC->pDrv->pfnVBVAMousePointerShape(pThisCC->pDrv, false /*fVisible*/, false, 0, 0, 0, 0, NULL);
1830
1831 /* Restore the text mode backup. */
1832 memcpy(pThisCC->pbVRam, pThisCC->svga.pbVgaFrameBufferR3, VMSVGA_VGA_FB_BACKUP_SIZE);
1833
1834 pThisCC->pDrv->pfnLFBModeChange(pThisCC->pDrv, false);
1835
1836 /* Enable dirty page tracking again when going into legacy mode. */
1837 vmsvgaR3SetTraces(pDevIns, pThis, true);
1838
1839 /* bird: Whatever this is was added to make screenshot work, ask sunlover should explain... */
1840 for (uint32_t idScreen = 0; idScreen < pThis->cMonitors; ++idScreen)
1841 pThisCC->pDrv->pfnVBVADisable(pThisCC->pDrv, idScreen);
1842
1843 /* Clear the pitch lock. */
1844 pThis->svga.u32PitchLock = 0;
1845 }
1846#else /* !IN_RING3 */
1847 rc = VINF_IOM_R3_IOPORT_WRITE;
1848#endif /* !IN_RING3 */
1849 break;
1850
1851 case SVGA_REG_WIDTH:
1852 STAM_REL_COUNTER_INC(&pThis->svga.StatRegWidthWr);
1853 if (pThis->svga.uWidth != u32)
1854 {
1855#if defined(IN_RING3) || defined(IN_RING0)
1856 pThis->svga.uWidth = u32;
1857 vmsvgaHCUpdatePitch(pThis, pThisCC);
1858 if (pThis->svga.fEnabled)
1859 ASMAtomicOrU32(&pThis->svga.u32ActionFlags, VMSVGA_ACTION_CHANGEMODE);
1860#else
1861 rc = VINF_IOM_R3_IOPORT_WRITE;
1862#endif
1863 }
1864 /* else: nop */
1865 break;
1866
1867 case SVGA_REG_HEIGHT:
1868 STAM_REL_COUNTER_INC(&pThis->svga.StatRegHeightWr);
1869 if (pThis->svga.uHeight != u32)
1870 {
1871 pThis->svga.uHeight = u32;
1872 if (pThis->svga.fEnabled)
1873 ASMAtomicOrU32(&pThis->svga.u32ActionFlags, VMSVGA_ACTION_CHANGEMODE);
1874 }
1875 /* else: nop */
1876 break;
1877
1878 case SVGA_REG_DEPTH:
1879 STAM_REL_COUNTER_INC(&pThis->svga.StatRegDepthWr);
1880 /** @todo read-only?? */
1881 break;
1882
1883 case SVGA_REG_BITS_PER_PIXEL: /* Current bpp in the guest */
1884 STAM_REL_COUNTER_INC(&pThis->svga.StatRegBitsPerPixelWr);
1885 if (pThis->svga.uBpp != u32)
1886 {
1887#if defined(IN_RING3) || defined(IN_RING0)
1888 pThis->svga.uBpp = u32;
1889 vmsvgaHCUpdatePitch(pThis, pThisCC);
1890 if (pThis->svga.fEnabled)
1891 ASMAtomicOrU32(&pThis->svga.u32ActionFlags, VMSVGA_ACTION_CHANGEMODE);
1892#else
1893 rc = VINF_IOM_R3_IOPORT_WRITE;
1894#endif
1895 }
1896 /* else: nop */
1897 break;
1898
1899 case SVGA_REG_PSEUDOCOLOR:
1900 STAM_REL_COUNTER_INC(&pThis->svga.StatRegPseudoColorWr);
1901 break;
1902
1903 case SVGA_REG_CONFIG_DONE: /* Set when memory area configured */
1904#ifdef IN_RING3
1905 STAM_REL_COUNTER_INC(&pSVGAState->StatR3RegConfigDoneWr);
1906 pThis->svga.fConfigured = u32;
1907 /* Disabling the FIFO enables tracing (dirty page detection) by default. */
1908 if (!pThis->svga.fConfigured)
1909 pThis->svga.fTraces = true;
1910 vmsvgaR3SetTraces(pDevIns, pThis, !!pThis->svga.fTraces);
1911#else
1912 rc = VINF_IOM_R3_IOPORT_WRITE;
1913#endif
1914 break;
1915
1916 case SVGA_REG_SYNC: /* See "FIFO Synchronization Registers" */
1917 STAM_REL_COUNTER_INC(&pThis->svga.StatRegSyncWr);
1918 if ( pThis->svga.fEnabled
1919 && pThis->svga.fConfigured)
1920 {
1921#if defined(IN_RING3) || defined(IN_RING0)
1922 Log(("SVGA_REG_SYNC: SVGA_FIFO_BUSY=%d\n", pThisCC->svga.pau32FIFO[SVGA_FIFO_BUSY]));
1923 /*
1924 * The VMSVGA_BUSY_F_EMT_FORCE flag makes sure we will check if the FIFO is empty
1925 * at least once; VMSVGA_BUSY_F_FIFO alone does not ensure that.
1926 */
1927 ASMAtomicWriteU32(&pThis->svga.fBusy, VMSVGA_BUSY_F_EMT_FORCE | VMSVGA_BUSY_F_FIFO);
1928 if (VMSVGA_IS_VALID_FIFO_REG(SVGA_FIFO_BUSY, pThisCC->svga.pau32FIFO[SVGA_FIFO_MIN]))
1929 vmsvgaHCSafeFifoBusyRegUpdate(pThis, pThisCC, true);
1930
1931 /* Kick the FIFO thread to start processing commands again. */
1932 PDMDevHlpSUPSemEventSignal(pDevIns, pThis->svga.hFIFORequestSem);
1933#else
1934 rc = VINF_IOM_R3_IOPORT_WRITE;
1935#endif
1936 }
1937 /* else nothing to do. */
1938 else
1939 Log(("Sync ignored enabled=%d configured=%d\n", pThis->svga.fEnabled, pThis->svga.fConfigured));
1940
1941 break;
1942
1943 case SVGA_REG_BUSY: /* See "FIFO Synchronization Registers" (read-only) */
1944 STAM_REL_COUNTER_INC(&pThis->svga.StatRegBusyWr);
1945 break;
1946
1947 case SVGA_REG_GUEST_ID: /* Set guest OS identifier */
1948 STAM_REL_COUNTER_INC(&pThis->svga.StatRegGuestIdWr);
1949 pThis->svga.u32GuestId = u32;
1950 break;
1951
1952 case SVGA_REG_PITCHLOCK: /* Fixed pitch for all modes */
1953 STAM_REL_COUNTER_INC(&pThis->svga.StatRegPitchLockWr);
1954 pThis->svga.u32PitchLock = u32;
1955 /* Should this also update the FIFO pitch lock? Unclear. */
1956 break;
1957
1958 case SVGA_REG_IRQMASK: /* Interrupt mask */
1959 STAM_REL_COUNTER_INC(&pThis->svga.StatRegIrqMaskWr);
1960 pThis->svga.u32IrqMask = u32;
1961
1962 /* Irq pending after the above change? */
1963 if (pThis->svga.u32IrqStatus & u32)
1964 {
1965 Log(("SVGA_REG_IRQMASK: Trigger interrupt with status %x\n", pThis->svga.u32IrqStatus));
1966 PDMDevHlpPCISetIrqNoWait(pDevIns, 0, 1);
1967 }
1968 else
1969 PDMDevHlpPCISetIrqNoWait(pDevIns, 0, 0);
1970 break;
1971
1972 /* Mouse cursor support */
1973 case SVGA_REG_CURSOR_ID:
1974 STAM_REL_COUNTER_INC(&pThis->svga.StatRegCursorIdWr);
1975 pThis->svga.uCursorID = u32;
1976 break;
1977
1978 case SVGA_REG_CURSOR_X:
1979 STAM_REL_COUNTER_INC(&pThis->svga.StatRegCursorXWr);
1980 pThis->svga.uCursorX = u32;
1981 break;
1982
1983 case SVGA_REG_CURSOR_Y:
1984 STAM_REL_COUNTER_INC(&pThis->svga.StatRegCursorYWr);
1985 pThis->svga.uCursorY = u32;
1986 break;
1987
1988 case SVGA_REG_CURSOR_ON:
1989#ifdef IN_RING3
1990 /* The cursor is only updated when SVGA_REG_CURSOR_ON is written. */
1991 STAM_REL_COUNTER_INC(&pThis->svga.StatRegCursorOnWr);
1992 vmsvgaR3RegUpdateCursor(pThisCC, pThis, u32);
1993#else
1994 rc = VINF_IOM_R3_IOPORT_WRITE;
1995#endif
1996 break;
1997
1998 /* Legacy multi-monitor support */
1999 case SVGA_REG_NUM_GUEST_DISPLAYS:/* Number of guest displays in X/Y direction */
2000 STAM_REL_COUNTER_INC(&pThis->svga.StatRegNumGuestDisplaysWr);
2001 break;
2002 case SVGA_REG_DISPLAY_ID: /* Display ID for the following display attributes */
2003 STAM_REL_COUNTER_INC(&pThis->svga.StatRegDisplayIdWr);
2004 break;
2005 case SVGA_REG_DISPLAY_IS_PRIMARY:/* Whether this is a primary display */
2006 STAM_REL_COUNTER_INC(&pThis->svga.StatRegDisplayIsPrimaryWr);
2007 break;
2008 case SVGA_REG_DISPLAY_POSITION_X:/* The display position x */
2009 STAM_REL_COUNTER_INC(&pThis->svga.StatRegDisplayPositionXWr);
2010 break;
2011 case SVGA_REG_DISPLAY_POSITION_Y:/* The display position y */
2012 STAM_REL_COUNTER_INC(&pThis->svga.StatRegDisplayPositionYWr);
2013 break;
2014 case SVGA_REG_DISPLAY_WIDTH: /* The display's width */
2015 STAM_REL_COUNTER_INC(&pThis->svga.StatRegDisplayWidthWr);
2016 break;
2017 case SVGA_REG_DISPLAY_HEIGHT: /* The display's height */
2018 STAM_REL_COUNTER_INC(&pThis->svga.StatRegDisplayHeightWr);
2019 break;
2020#ifdef VBOX_WITH_VMSVGA3D
2021 /* See "Guest memory regions" below. */
2022 case SVGA_REG_GMR_ID:
2023 STAM_REL_COUNTER_INC(&pThis->svga.StatRegGmrIdWr);
2024 pThis->svga.u32CurrentGMRId = u32;
2025 break;
2026
2027 case SVGA_REG_GMR_DESCRIPTOR:
2028# ifndef IN_RING3
2029 rc = VINF_IOM_R3_IOPORT_WRITE;
2030 break;
2031# else /* IN_RING3 */
2032 {
2033 STAM_REL_COUNTER_INC(&pSVGAState->StatR3RegGmrDescriptorWr);
2034
2035 /* Validate current GMR id. */
2036 uint32_t idGMR = pThis->svga.u32CurrentGMRId;
2037 AssertBreak(idGMR < pThis->svga.cGMR);
2038 RT_UNTRUSTED_VALIDATED_FENCE();
2039
2040 /* Free the old GMR if present. */
2041 vmsvgaR3GmrFree(pThisCC, idGMR);
2042
2043 /* Just undefine the GMR? */
2044 RTGCPHYS GCPhys = (RTGCPHYS)u32 << PAGE_SHIFT;
2045 if (GCPhys == 0)
2046 {
2047 STAM_REL_COUNTER_INC(&pSVGAState->StatR3RegGmrDescriptorWrFree);
2048 break;
2049 }
2050
2051
2052 /* Never cross a page boundary automatically. */
2053 const uint32_t cMaxPages = RT_MIN(VMSVGA_MAX_GMR_PAGES, UINT32_MAX / X86_PAGE_SIZE);
2054 uint32_t cPagesTotal = 0;
2055 uint32_t iDesc = 0;
2056 PVMSVGAGMRDESCRIPTOR paDescs = NULL;
2057 uint32_t cLoops = 0;
2058 RTGCPHYS GCPhysBase = GCPhys;
2059 while (PHYS_PAGE_ADDRESS(GCPhys) == PHYS_PAGE_ADDRESS(GCPhysBase))
2060 {
2061 /* Read descriptor. */
2062 SVGAGuestMemDescriptor desc;
2063 rc = PDMDevHlpPCIPhysRead(pDevIns, GCPhys, &desc, sizeof(desc));
2064 AssertRCBreak(VBOXSTRICTRC_VAL(rc));
2065
2066 if (desc.numPages != 0)
2067 {
2068 AssertBreakStmt(desc.numPages <= cMaxPages, rc = VERR_OUT_OF_RANGE);
2069 cPagesTotal += desc.numPages;
2070 AssertBreakStmt(cPagesTotal <= cMaxPages, rc = VERR_OUT_OF_RANGE);
2071
2072 if ((iDesc & 15) == 0)
2073 {
2074 void *pvNew = RTMemRealloc(paDescs, (iDesc + 16) * sizeof(VMSVGAGMRDESCRIPTOR));
2075 AssertBreakStmt(pvNew, rc = VERR_NO_MEMORY);
2076 paDescs = (PVMSVGAGMRDESCRIPTOR)pvNew;
2077 }
2078
2079 paDescs[iDesc].GCPhys = (RTGCPHYS)desc.ppn << PAGE_SHIFT;
2080 paDescs[iDesc++].numPages = desc.numPages;
2081
2082 /* Continue with the next descriptor. */
2083 GCPhys += sizeof(desc);
2084 }
2085 else if (desc.ppn == 0)
2086 break; /* terminator */
2087 else /* Pointer to the next physical page of descriptors. */
2088 GCPhys = GCPhysBase = (RTGCPHYS)desc.ppn << PAGE_SHIFT;
2089
2090 cLoops++;
2091 AssertBreakStmt(cLoops < VMSVGA_MAX_GMR_DESC_LOOP_COUNT, rc = VERR_OUT_OF_RANGE);
2092 }
2093
2094 AssertStmt(iDesc > 0 || RT_FAILURE_NP(rc), rc = VERR_OUT_OF_RANGE);
2095 if (RT_SUCCESS(rc))
2096 {
2097 /* Commit the GMR. */
2098 pSVGAState->paGMR[idGMR].paDesc = paDescs;
2099 pSVGAState->paGMR[idGMR].numDescriptors = iDesc;
2100 pSVGAState->paGMR[idGMR].cMaxPages = cPagesTotal;
2101 pSVGAState->paGMR[idGMR].cbTotal = cPagesTotal * PAGE_SIZE;
2102 Assert((pSVGAState->paGMR[idGMR].cbTotal >> PAGE_SHIFT) == cPagesTotal);
2103 Log(("Defined new gmr %x numDescriptors=%d cbTotal=%x (%#x pages)\n",
2104 idGMR, iDesc, pSVGAState->paGMR[idGMR].cbTotal, cPagesTotal));
2105 }
2106 else
2107 {
2108 RTMemFree(paDescs);
2109 STAM_REL_COUNTER_INC(&pSVGAState->StatR3RegGmrDescriptorWrErrors);
2110 }
2111 break;
2112 }
2113# endif /* IN_RING3 */
2114#endif // VBOX_WITH_VMSVGA3D
2115
2116 case SVGA_REG_TRACES: /* Enable trace-based updates even when FIFO is on */
2117 STAM_REL_COUNTER_INC(&pThis->svga.StatRegTracesWr);
2118 if (pThis->svga.fTraces == u32)
2119 break; /* nothing to do */
2120
2121#ifdef IN_RING3
2122 vmsvgaR3SetTraces(pDevIns, pThis, !!u32);
2123#else
2124 rc = VINF_IOM_R3_IOPORT_WRITE;
2125#endif
2126 break;
2127
2128 case SVGA_REG_TOP: /* Must be 1 more than the last register */
2129 STAM_REL_COUNTER_INC(&pThis->svga.StatRegTopWr);
2130 break;
2131
2132 case SVGA_REG_NUM_DISPLAYS: /* (Deprecated) */
2133 STAM_REL_COUNTER_INC(&pThis->svga.StatRegNumDisplaysWr);
2134 Log(("Write to deprecated register %x - val %x ignored\n", idxReg, u32));
2135 break;
2136
2137 /*
2138 * SVGA_CAP_GBOBJECTS+ registers.
2139 */
2140 case SVGA_REG_COMMAND_LOW:
2141 {
2142 /* Lower 32 bits of command buffer physical address and submit the command buffer. */
2143#ifdef IN_RING3
2144 STAM_REL_COUNTER_INC(&pThis->svga.StatRegCommandLowWr);
2145 pThis->svga.u32RegCommandLow = u32;
2146
2147 /* "lower 6 bits are used for the SVGACBContext" */
2148 RTGCPHYS GCPhysCB = pThis->svga.u32RegCommandHigh;
2149 GCPhysCB <<= 32;
2150 GCPhysCB |= pThis->svga.u32RegCommandLow & ~SVGA_CB_CONTEXT_MASK;
2151 SVGACBContext const CBCtx = (SVGACBContext)(pThis->svga.u32RegCommandLow & SVGA_CB_CONTEXT_MASK);
2152 vmsvgaR3CmdBufSubmit(pDevIns, pThis, pThisCC, GCPhysCB, CBCtx);
2153#else
2154 rc = VINF_IOM_R3_IOPORT_WRITE;
2155#endif
2156 break;
2157 }
2158
2159 case SVGA_REG_COMMAND_HIGH:
2160 /* Upper 32 bits of command buffer PA. */
2161 STAM_REL_COUNTER_INC(&pThis->svga.StatRegCommandHighWr);
2162 pThis->svga.u32RegCommandHigh = u32;
2163 break;
2164
2165 case SVGA_REG_DEV_CAP:
2166 /* Write dev cap index, read value */
2167 STAM_REL_COUNTER_INC(&pThis->svga.StatRegDevCapWr);
2168 pThis->svga.u32DevCapIndex = u32;
2169 break;
2170
2171 case SVGA_REG_CMD_PREPEND_LOW:
2172 STAM_REL_COUNTER_INC(&pThis->svga.StatRegCmdPrependLowWr);
2173 /* Not supported. */
2174 break;
2175
2176 case SVGA_REG_iCMD_PREPEND_HIGH:
2177 STAM_REL_COUNTER_INC(&pThis->svga.StatRegCmdPrependHighWr);
2178 /* Not supported. */
2179 break;
2180
2181 case SVGA_REG_FB_START:
2182 case SVGA_REG_MEM_START:
2183 case SVGA_REG_HOST_BITS_PER_PIXEL:
2184 case SVGA_REG_MAX_WIDTH:
2185 case SVGA_REG_MAX_HEIGHT:
2186 case SVGA_REG_VRAM_SIZE:
2187 case SVGA_REG_FB_SIZE:
2188 case SVGA_REG_CAPABILITIES:
2189 case SVGA_REG_MEM_SIZE:
2190 case SVGA_REG_SCRATCH_SIZE: /* Number of scratch registers */
2191 case SVGA_REG_MEM_REGS: /* Number of FIFO registers */
2192 case SVGA_REG_BYTES_PER_LINE:
2193 case SVGA_REG_FB_OFFSET:
2194 case SVGA_REG_RED_MASK:
2195 case SVGA_REG_GREEN_MASK:
2196 case SVGA_REG_BLUE_MASK:
2197 case SVGA_REG_GMRS_MAX_PAGES: /* Maximum number of 4KB pages for all GMRs */
2198 case SVGA_REG_MEMORY_SIZE: /* Total dedicated device memory excluding FIFO */
2199 case SVGA_REG_GMR_MAX_IDS:
2200 case SVGA_REG_GMR_MAX_DESCRIPTOR_LENGTH:
2201 case SVGA_REG_MAX_PRIMARY_BOUNDING_BOX_MEM:
2202 case SVGA_REG_SUGGESTED_GBOBJECT_MEM_SIZE_KB:
2203 case SVGA_REG_SCREENTARGET_MAX_WIDTH:
2204 case SVGA_REG_SCREENTARGET_MAX_HEIGHT:
2205 case SVGA_REG_MOB_MAX_SIZE:
2206 /* Read only - ignore. */
2207 Log(("Write to R/O register %x - val %x ignored\n", idxReg, u32));
2208 STAM_REL_COUNTER_INC(&pThis->svga.StatRegReadOnlyWr);
2209 break;
2210
2211 default:
2212 {
2213 uint32_t offReg;
2214 if ((offReg = idxReg - SVGA_SCRATCH_BASE) < pThis->svga.cScratchRegion)
2215 {
2216 RT_UNTRUSTED_VALIDATED_FENCE();
2217 pThis->svga.au32ScratchRegion[offReg] = u32;
2218 STAM_REL_COUNTER_INC(&pThis->svga.StatRegScratchWr);
2219 }
2220 else if ((offReg = idxReg - SVGA_PALETTE_BASE) < (uint32_t)SVGA_NUM_PALETTE_REGS)
2221 {
2222 /* Note! Using last_palette rather than palette here to preserve the VGA one.
2223 Btw, see rgb_to_pixel32. */
2224 STAM_REL_COUNTER_INC(&pThis->svga.StatRegPaletteWr);
2225 u32 &= 0xff;
2226 RT_UNTRUSTED_VALIDATED_FENCE();
2227 uint32_t uRgb = pThis->last_palette[offReg / 3];
2228 switch (offReg % 3)
2229 {
2230 case 0: uRgb = (uRgb & UINT32_C(0x0000ffff)) | (u32 << 16); break; /* red */
2231 case 1: uRgb = (uRgb & UINT32_C(0x00ff00ff)) | (u32 << 8); break; /* green */
2232 case 2: uRgb = (uRgb & UINT32_C(0x00ffff00)) | u32 ; break; /* blue */
2233 }
2234 pThis->last_palette[offReg / 3] = uRgb;
2235 }
2236 else
2237 {
2238#if !defined(IN_RING3) && defined(VBOX_STRICT)
2239 rc = VINF_IOM_R3_IOPORT_WRITE;
2240#else
2241 STAM_REL_COUNTER_INC(&pThis->svga.StatRegUnknownWr);
2242 AssertMsgFailed(("reg=%#x u32=%#x\n", idxReg, u32));
2243#endif
2244 }
2245 break;
2246 }
2247 }
2248 return rc;
2249}
2250
2251/**
2252 * @callback_method_impl{FNIOMIOPORTNEWIN}
2253 */
2254DECLCALLBACK(VBOXSTRICTRC) vmsvgaIORead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT offPort, uint32_t *pu32, unsigned cb)
2255{
2256 PVGASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVGASTATE);
2257 RT_NOREF_PV(pvUser);
2258
2259 /* Only dword accesses. */
2260 if (cb == 4)
2261 {
2262 switch (offPort)
2263 {
2264 case SVGA_INDEX_PORT:
2265 *pu32 = pThis->svga.u32IndexReg;
2266 break;
2267
2268 case SVGA_VALUE_PORT:
2269 return vmsvgaReadPort(pDevIns, pThis, pu32);
2270
2271 case SVGA_BIOS_PORT:
2272 Log(("Ignoring BIOS port read\n"));
2273 *pu32 = 0;
2274 break;
2275
2276 case SVGA_IRQSTATUS_PORT:
2277 LogFlow(("vmsvgaIORead: SVGA_IRQSTATUS_PORT %x\n", pThis->svga.u32IrqStatus));
2278 *pu32 = pThis->svga.u32IrqStatus;
2279 break;
2280
2281 default:
2282 ASSERT_GUEST_MSG_FAILED(("vmsvgaIORead: Unknown register %u was read from.\n", offPort));
2283 *pu32 = UINT32_MAX;
2284 break;
2285 }
2286 }
2287 else
2288 {
2289 Log(("Ignoring non-dword I/O port read at %x cb=%d\n", offPort, cb));
2290 *pu32 = UINT32_MAX;
2291 }
2292 return VINF_SUCCESS;
2293}
2294
2295/**
2296 * @callback_method_impl{FNIOMIOPORTNEWOUT}
2297 */
2298DECLCALLBACK(VBOXSTRICTRC) vmsvgaIOWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT offPort, uint32_t u32, unsigned cb)
2299{
2300 PVGASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVGASTATE);
2301 PVGASTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVGASTATECC);
2302 RT_NOREF_PV(pvUser);
2303
2304 /* Only dword accesses. */
2305 if (cb == 4)
2306 switch (offPort)
2307 {
2308 case SVGA_INDEX_PORT:
2309 pThis->svga.u32IndexReg = u32;
2310 break;
2311
2312 case SVGA_VALUE_PORT:
2313 return vmsvgaWritePort(pDevIns, pThis, pThisCC, u32);
2314
2315 case SVGA_BIOS_PORT:
2316 Log(("Ignoring BIOS port write (val=%x)\n", u32));
2317 break;
2318
2319 case SVGA_IRQSTATUS_PORT:
2320 Log(("vmsvgaIOWrite SVGA_IRQSTATUS_PORT %x: status %x -> %x\n", u32, pThis->svga.u32IrqStatus, pThis->svga.u32IrqStatus & ~u32));
2321 ASMAtomicAndU32(&pThis->svga.u32IrqStatus, ~u32);
2322 /* Clear the irq in case all events have been cleared. */
2323 if (!(pThis->svga.u32IrqStatus & pThis->svga.u32IrqMask))
2324 {
2325 Log(("vmsvgaIOWrite SVGA_IRQSTATUS_PORT: clearing IRQ\n"));
2326 PDMDevHlpPCISetIrqNoWait(pDevIns, 0, 0);
2327 }
2328 break;
2329
2330 default:
2331 ASSERT_GUEST_MSG_FAILED(("vmsvgaIOWrite: Unknown register %u was written to, value %#x LB %u.\n", offPort, u32, cb));
2332 break;
2333 }
2334 else
2335 Log(("Ignoring non-dword write at %x val=%x cb=%d\n", offPort, u32, cb));
2336
2337 return VINF_SUCCESS;
2338}
2339
2340#ifdef IN_RING3
2341
2342# ifdef DEBUG_FIFO_ACCESS
2343/**
2344 * Handle FIFO memory access.
2345 * @returns VBox status code.
2346 * @param pVM VM handle.
2347 * @param pThis The shared VGA/VMSVGA instance data.
2348 * @param GCPhys The access physical address.
2349 * @param fWriteAccess Read or write access
2350 */
2351static int vmsvgaR3DebugFifoAccess(PVM pVM, PVGASTATE pThis, RTGCPHYS GCPhys, bool fWriteAccess)
2352{
2353 RT_NOREF(pVM);
2354 RTGCPHYS GCPhysOffset = GCPhys - pThis->svga.GCPhysFIFO;
2355 uint32_t *pFIFO = pThisCC->svga.pau32FIFO;
2356
2357 switch (GCPhysOffset >> 2)
2358 {
2359 case SVGA_FIFO_MIN:
2360 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_MIN = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2361 break;
2362 case SVGA_FIFO_MAX:
2363 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_MAX = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2364 break;
2365 case SVGA_FIFO_NEXT_CMD:
2366 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_NEXT_CMD = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2367 break;
2368 case SVGA_FIFO_STOP:
2369 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_STOP = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2370 break;
2371 case SVGA_FIFO_CAPABILITIES:
2372 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_CAPABILITIES = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2373 break;
2374 case SVGA_FIFO_FLAGS:
2375 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_FLAGS = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2376 break;
2377 case SVGA_FIFO_FENCE:
2378 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_FENCE = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2379 break;
2380 case SVGA_FIFO_3D_HWVERSION:
2381 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_HWVERSION = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2382 break;
2383 case SVGA_FIFO_PITCHLOCK:
2384 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_PITCHLOCK = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2385 break;
2386 case SVGA_FIFO_CURSOR_ON:
2387 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_CURSOR_ON = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2388 break;
2389 case SVGA_FIFO_CURSOR_X:
2390 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_CURSOR_X = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2391 break;
2392 case SVGA_FIFO_CURSOR_Y:
2393 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_CURSOR_Y = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2394 break;
2395 case SVGA_FIFO_CURSOR_COUNT:
2396 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_CURSOR_COUNT = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2397 break;
2398 case SVGA_FIFO_CURSOR_LAST_UPDATED:
2399 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_CURSOR_LAST_UPDATED = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2400 break;
2401 case SVGA_FIFO_RESERVED:
2402 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_RESERVED = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2403 break;
2404 case SVGA_FIFO_CURSOR_SCREEN_ID:
2405 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_CURSOR_SCREEN_ID = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2406 break;
2407 case SVGA_FIFO_DEAD:
2408 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_DEAD = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2409 break;
2410 case SVGA_FIFO_3D_HWVERSION_REVISED:
2411 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_HWVERSION_REVISED = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2412 break;
2413 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_3D:
2414 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_3D = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2415 break;
2416 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_MAX_LIGHTS:
2417 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_MAX_LIGHTS = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2418 break;
2419 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_MAX_TEXTURES:
2420 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_MAX_TEXTURES = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2421 break;
2422 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_MAX_CLIP_PLANES:
2423 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_MAX_CLIP_PLANES = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2424 break;
2425 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_VERTEX_SHADER_VERSION:
2426 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_VERTEX_SHADER_VERSION = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2427 break;
2428 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_VERTEX_SHADER:
2429 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_VERTEX_SHADER = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2430 break;
2431 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_FRAGMENT_SHADER_VERSION:
2432 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_FRAGMENT_SHADER_VERSION = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2433 break;
2434 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_FRAGMENT_SHADER:
2435 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_FRAGMENT_SHADER = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2436 break;
2437 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_MAX_RENDER_TARGETS:
2438 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_MAX_RENDER_TARGETS = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2439 break;
2440 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_S23E8_TEXTURES:
2441 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_S23E8_TEXTURES = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2442 break;
2443 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_S10E5_TEXTURES:
2444 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_S10E5_TEXTURES = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2445 break;
2446 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_MAX_FIXED_VERTEXBLEND:
2447 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_MAX_FIXED_VERTEXBLEND = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2448 break;
2449 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_D16_BUFFER_FORMAT:
2450 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_D16_BUFFER_FORMAT = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2451 break;
2452 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_D24S8_BUFFER_FORMAT:
2453 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_D24S8_BUFFER_FORMAT = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2454 break;
2455 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_D24X8_BUFFER_FORMAT:
2456 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_D24X8_BUFFER_FORMAT = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2457 break;
2458 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_QUERY_TYPES:
2459 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_QUERY_TYPES = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2460 break;
2461 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_TEXTURE_GRADIENT_SAMPLING:
2462 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_TEXTURE_GRADIENT_SAMPLING = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2463 break;
2464 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_MAX_POINT_SIZE:
2465 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_MAX_POINT_SIZE = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2466 break;
2467 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_MAX_SHADER_TEXTURES:
2468 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_MAX_SHADER_TEXTURES = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2469 break;
2470 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_MAX_TEXTURE_WIDTH:
2471 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_MAX_TEXTURE_WIDTH = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2472 break;
2473 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_MAX_TEXTURE_HEIGHT:
2474 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_MAX_TEXTURE_HEIGHT = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2475 break;
2476 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_MAX_VOLUME_EXTENT:
2477 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_MAX_VOLUME_EXTENT = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2478 break;
2479 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_MAX_TEXTURE_REPEAT:
2480 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_MAX_TEXTURE_REPEAT = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2481 break;
2482 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_MAX_TEXTURE_ASPECT_RATIO:
2483 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_MAX_TEXTURE_ASPECT_RATIO = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2484 break;
2485 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_MAX_TEXTURE_ANISOTROPY:
2486 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_MAX_TEXTURE_ANISOTROPY = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2487 break;
2488 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_MAX_PRIMITIVE_COUNT:
2489 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_MAX_PRIMITIVE_COUNT = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2490 break;
2491 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_MAX_VERTEX_INDEX:
2492 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_MAX_VERTEX_INDEX = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2493 break;
2494 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_MAX_VERTEX_SHADER_INSTRUCTIONS:
2495 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_MAX_VERTEX_SHADER_INSTRUCTIONS = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2496 break;
2497 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_MAX_FRAGMENT_SHADER_INSTRUCTIONS:
2498 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_MAX_FRAGMENT_SHADER_INSTRUCTIONS = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2499 break;
2500 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_MAX_VERTEX_SHADER_TEMPS:
2501 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_MAX_VERTEX_SHADER_TEMPS = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2502 break;
2503 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_MAX_FRAGMENT_SHADER_TEMPS:
2504 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_MAX_FRAGMENT_SHADER_TEMPS = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2505 break;
2506 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_TEXTURE_OPS:
2507 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_TEXTURE_OPS = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2508 break;
2509 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_SURFACEFMT_X8R8G8B8:
2510 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_SURFACEFMT_X8R8G8B8 = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2511 break;
2512 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_SURFACEFMT_A8R8G8B8:
2513 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_SURFACEFMT_A8R8G8B8 = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2514 break;
2515 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_SURFACEFMT_A2R10G10B10:
2516 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_SURFACEFMT_A2R10G10B10 = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2517 break;
2518 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_SURFACEFMT_X1R5G5B5:
2519 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_SURFACEFMT_X1R5G5B5 = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2520 break;
2521 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_SURFACEFMT_A1R5G5B5:
2522 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_SURFACEFMT_A1R5G5B5 = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2523 break;
2524 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_SURFACEFMT_A4R4G4B4:
2525 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_SURFACEFMT_A4R4G4B4 = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2526 break;
2527 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_SURFACEFMT_R5G6B5:
2528 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_SURFACEFMT_R5G6B5 = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2529 break;
2530 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_SURFACEFMT_LUMINANCE16:
2531 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_SURFACEFMT_LUMINANCE16 = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2532 break;
2533 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_SURFACEFMT_LUMINANCE8_ALPHA8:
2534 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_SURFACEFMT_LUMINANCE8_ALPHA8 = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2535 break;
2536 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_SURFACEFMT_ALPHA8:
2537 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_SURFACEFMT_ALPHA8 = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2538 break;
2539 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_SURFACEFMT_LUMINANCE8:
2540 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_SURFACEFMT_LUMINANCE8 = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2541 break;
2542 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_SURFACEFMT_Z_D16:
2543 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_SURFACEFMT_Z_D16 = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2544 break;
2545 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_SURFACEFMT_Z_D24S8:
2546 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_SURFACEFMT_Z_D24S8 = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2547 break;
2548 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_SURFACEFMT_Z_D24X8:
2549 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_SURFACEFMT_Z_D24X8 = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2550 break;
2551 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_SURFACEFMT_DXT1:
2552 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_SURFACEFMT_DXT1 = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2553 break;
2554 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_SURFACEFMT_DXT2:
2555 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_SURFACEFMT_DXT2 = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2556 break;
2557 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_SURFACEFMT_DXT3:
2558 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_SURFACEFMT_DXT3 = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2559 break;
2560 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_SURFACEFMT_DXT4:
2561 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_SURFACEFMT_DXT4 = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2562 break;
2563 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_SURFACEFMT_DXT5:
2564 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_SURFACEFMT_DXT5 = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2565 break;
2566 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_SURFACEFMT_BUMPX8L8V8U8:
2567 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_SURFACEFMT_BUMPX8L8V8U8 = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2568 break;
2569 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_SURFACEFMT_A2W10V10U10:
2570 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_SURFACEFMT_A2W10V10U10 = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2571 break;
2572 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_SURFACEFMT_BUMPU8V8:
2573 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_SURFACEFMT_BUMPU8V8 = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2574 break;
2575 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_SURFACEFMT_Q8W8V8U8:
2576 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_SURFACEFMT_Q8W8V8U8 = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2577 break;
2578 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_SURFACEFMT_CxV8U8:
2579 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_SURFACEFMT_CxV8U8 = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2580 break;
2581 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_SURFACEFMT_R_S10E5:
2582 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_SURFACEFMT_R_S10E5 = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2583 break;
2584 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_SURFACEFMT_R_S23E8:
2585 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_SURFACEFMT_R_S23E8 = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2586 break;
2587 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_SURFACEFMT_RG_S10E5:
2588 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_SURFACEFMT_RG_S10E5 = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2589 break;
2590 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_SURFACEFMT_RG_S23E8:
2591 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_SURFACEFMT_RG_S23E8 = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2592 break;
2593 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_SURFACEFMT_ARGB_S10E5:
2594 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_SURFACEFMT_ARGB_S10E5 = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2595 break;
2596 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_SURFACEFMT_ARGB_S23E8:
2597 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_SURFACEFMT_ARGB_S23E8 = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2598 break;
2599 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_MAX_VERTEX_SHADER_TEXTURES:
2600 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_MAX_VERTEX_SHADER_TEXTURES = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2601 break;
2602 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_MAX_SIMULTANEOUS_RENDER_TARGETS:
2603 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_MAX_SIMULTANEOUS_RENDER_TARGETS = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2604 break;
2605 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_SURFACEFMT_V16U16:
2606 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_SURFACEFMT_V16U16 = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2607 break;
2608 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_SURFACEFMT_G16R16:
2609 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_SURFACEFMT_G16R16 = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2610 break;
2611 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_SURFACEFMT_A16B16G16R16:
2612 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_SURFACEFMT_A16B16G16R16 = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2613 break;
2614 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_SURFACEFMT_UYVY:
2615 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_SURFACEFMT_UYVY = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2616 break;
2617 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_SURFACEFMT_YUY2:
2618 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_SURFACEFMT_YUY2 = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2619 break;
2620 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_MULTISAMPLE_NONMASKABLESAMPLES:
2621 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_MULTISAMPLE_NONMASKABLESAMPLES = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2622 break;
2623 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_MULTISAMPLE_MASKABLESAMPLES:
2624 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_MULTISAMPLE_MASKABLESAMPLES = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2625 break;
2626 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_ALPHATOCOVERAGE:
2627 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_ALPHATOCOVERAGE = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2628 break;
2629 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_SUPERSAMPLE:
2630 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_SUPERSAMPLE = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2631 break;
2632 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_AUTOGENMIPMAPS:
2633 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_AUTOGENMIPMAPS = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2634 break;
2635 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_SURFACEFMT_NV12:
2636 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_SURFACEFMT_NV12 = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2637 break;
2638 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_SURFACEFMT_AYUV:
2639 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_SURFACEFMT_AYUV = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2640 break;
2641 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_MAX_CONTEXT_IDS:
2642 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_MAX_CONTEXT_IDS = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2643 break;
2644 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_MAX_SURFACE_IDS:
2645 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_MAX_SURFACE_IDS = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2646 break;
2647 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_SURFACEFMT_Z_DF16:
2648 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_SURFACEFMT_Z_DF16 = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2649 break;
2650 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_SURFACEFMT_Z_DF24:
2651 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_SURFACEFMT_Z_DF24 = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2652 break;
2653 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_SURFACEFMT_Z_D24S8_INT:
2654 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_SURFACEFMT_Z_D24S8_INT = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2655 break;
2656 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_SURFACEFMT_ATI1:
2657 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_SURFACEFMT_ATI1 = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2658 break;
2659 case SVGA_FIFO_3D_CAPS + SVGA3D_DEVCAP_SURFACEFMT_ATI2:
2660 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS SVGA3D_DEVCAP_SURFACEFMT_ATI2 = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2661 break;
2662 case SVGA_FIFO_3D_CAPS_LAST:
2663 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_3D_CAPS_LAST = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2664 break;
2665 case SVGA_FIFO_GUEST_3D_HWVERSION:
2666 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_GUEST_3D_HWVERSION = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2667 break;
2668 case SVGA_FIFO_FENCE_GOAL:
2669 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_FENCE_GOAL = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2670 break;
2671 case SVGA_FIFO_BUSY:
2672 Log(("vmsvgaFIFOAccess [0x%x]: %s SVGA_FIFO_BUSY = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", pFIFO[GCPhysOffset >> 2]));
2673 break;
2674 default:
2675 Log(("vmsvgaFIFOAccess [0x%x]: %s access at offset %x = %x\n", GCPhysOffset >> 2, (fWriteAccess) ? "WRITE" : "READ", GCPhysOffset, pFIFO[GCPhysOffset >> 2]));
2676 break;
2677 }
2678
2679 return VINF_EM_RAW_EMULATE_INSTR;
2680}
2681# endif /* DEBUG_FIFO_ACCESS */
2682
2683# if defined(VMSVGA_USE_FIFO_ACCESS_HANDLER) || defined(DEBUG_FIFO_ACCESS)
2684/**
2685 * HC access handler for the FIFO.
2686 *
2687 * @returns VINF_SUCCESS if the handler have carried out the operation.
2688 * @returns VINF_PGM_HANDLER_DO_DEFAULT if the caller should carry out the access operation.
2689 * @param pVM VM Handle.
2690 * @param pVCpu The cross context CPU structure for the calling EMT.
2691 * @param GCPhys The physical address the guest is writing to.
2692 * @param pvPhys The HC mapping of that address.
2693 * @param pvBuf What the guest is reading/writing.
2694 * @param cbBuf How much it's reading/writing.
2695 * @param enmAccessType The access type.
2696 * @param enmOrigin Who is making the access.
2697 * @param pvUser User argument.
2698 */
2699static DECLCALLBACK(VBOXSTRICTRC)
2700vmsvgaR3FifoAccessHandler(PVM pVM, PVMCPU pVCpu, RTGCPHYS GCPhys, void *pvPhys, void *pvBuf, size_t cbBuf,
2701 PGMACCESSTYPE enmAccessType, PGMACCESSORIGIN enmOrigin, void *pvUser)
2702{
2703 NOREF(pVCpu); NOREF(pvPhys); NOREF(pvBuf); NOREF(cbBuf); NOREF(enmOrigin); NOREF(enmAccessType); NOREF(GCPhys);
2704 PVGASTATE pThis = (PVGASTATE)pvUser;
2705 AssertPtr(pThis);
2706
2707# ifdef VMSVGA_USE_FIFO_ACCESS_HANDLER
2708 /*
2709 * Wake up the FIFO thread as it might have work to do now.
2710 */
2711 int rc = PDMDevHlpSUPSemEventSignal(pDevIns, pThis->svga.hFIFORequestSem);
2712 AssertLogRelRC(rc);
2713# endif
2714
2715# ifdef DEBUG_FIFO_ACCESS
2716 /*
2717 * When in debug-fifo-access mode, we do not disable the access handler,
2718 * but leave it on as we wish to catch all access.
2719 */
2720 Assert(GCPhys >= pThis->svga.GCPhysFIFO);
2721 rc = vmsvgaR3DebugFifoAccess(pVM, pThis, GCPhys, enmAccessType == PGMACCESSTYPE_WRITE);
2722# elif defined(VMSVGA_USE_FIFO_ACCESS_HANDLER)
2723 /*
2724 * Temporarily disable the access handler now that we've kicked the FIFO thread.
2725 */
2726 STAM_REL_COUNTER_INC(&pThisCC->svga.pSvgaR3State->StatFifoAccessHandler);
2727 rc = PGMHandlerPhysicalPageTempOff(pVM, pThis->svga.GCPhysFIFO, pThis->svga.GCPhysFIFO);
2728# endif
2729 if (RT_SUCCESS(rc))
2730 return VINF_PGM_HANDLER_DO_DEFAULT;
2731 AssertMsg(rc <= VINF_SUCCESS, ("rc=%Rrc\n", rc));
2732 return rc;
2733}
2734# endif /* VMSVGA_USE_FIFO_ACCESS_HANDLER || DEBUG_FIFO_ACCESS */
2735
2736#endif /* IN_RING3 */
2737
2738#ifdef DEBUG_GMR_ACCESS
2739# ifdef IN_RING3
2740
2741/**
2742 * HC access handler for GMRs.
2743 *
2744 * @returns VINF_SUCCESS if the handler have carried out the operation.
2745 * @returns VINF_PGM_HANDLER_DO_DEFAULT if the caller should carry out the access operation.
2746 * @param pVM VM Handle.
2747 * @param pVCpu The cross context CPU structure for the calling EMT.
2748 * @param GCPhys The physical address the guest is writing to.
2749 * @param pvPhys The HC mapping of that address.
2750 * @param pvBuf What the guest is reading/writing.
2751 * @param cbBuf How much it's reading/writing.
2752 * @param enmAccessType The access type.
2753 * @param enmOrigin Who is making the access.
2754 * @param pvUser User argument.
2755 */
2756static DECLCALLBACK(VBOXSTRICTRC)
2757vmsvgaR3GmrAccessHandler(PVM pVM, PVMCPU pVCpu, RTGCPHYS GCPhys, void *pvPhys, void *pvBuf, size_t cbBuf,
2758 PGMACCESSTYPE enmAccessType, PGMACCESSORIGIN enmOrigin, void *pvUser)
2759{
2760 PVGASTATE pThis = (PVGASTATE)pvUser;
2761 Assert(pThis);
2762 PVMSVGAR3STATE pSVGAState = pThisCC->svga.pSvgaR3State;
2763 NOREF(pVCpu); NOREF(pvPhys); NOREF(pvBuf); NOREF(cbBuf); NOREF(enmAccessType); NOREF(enmOrigin);
2764
2765 Log(("vmsvgaR3GmrAccessHandler: GMR access to page %RGp\n", GCPhys));
2766
2767 for (uint32_t i = 0; i < pThis->svga.cGMR; ++i)
2768 {
2769 PGMR pGMR = &pSVGAState->paGMR[i];
2770
2771 if (pGMR->numDescriptors)
2772 {
2773 for (uint32_t j = 0; j < pGMR->numDescriptors; j++)
2774 {
2775 if ( GCPhys >= pGMR->paDesc[j].GCPhys
2776 && GCPhys < pGMR->paDesc[j].GCPhys + pGMR->paDesc[j].numPages * PAGE_SIZE)
2777 {
2778 /*
2779 * Turn off the write handler for this particular page and make it R/W.
2780 * Then return telling the caller to restart the guest instruction.
2781 */
2782 int rc = PGMHandlerPhysicalPageTempOff(pVM, pGMR->paDesc[j].GCPhys, GCPhys);
2783 AssertRC(rc);
2784 return VINF_PGM_HANDLER_DO_DEFAULT;
2785 }
2786 }
2787 }
2788 }
2789
2790 return VINF_PGM_HANDLER_DO_DEFAULT;
2791}
2792
2793/** Callback handler for VMR3ReqCallWaitU */
2794static DECLCALLBACK(int) vmsvgaR3RegisterGmr(PPDMDEVINS pDevIns, uint32_t gmrId)
2795{
2796 PVGASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVGASTATE);
2797 PVMSVGAR3STATE pSVGAState = pThisCC->svga.pSvgaR3State;
2798 PGMR pGMR = &pSVGAState->paGMR[gmrId];
2799 int rc;
2800
2801 for (uint32_t i = 0; i < pGMR->numDescriptors; i++)
2802 {
2803 rc = PGMHandlerPhysicalRegister(PDMDevHlpGetVM(pDevIns),
2804 pGMR->paDesc[i].GCPhys, pGMR->paDesc[i].GCPhys + pGMR->paDesc[i].numPages * PAGE_SIZE - 1,
2805 pThis->svga.hGmrAccessHandlerType, pThis, NIL_RTR0PTR, NIL_RTRCPTR, "VMSVGA GMR");
2806 AssertRC(rc);
2807 }
2808 return VINF_SUCCESS;
2809}
2810
2811/** Callback handler for VMR3ReqCallWaitU */
2812static DECLCALLBACK(int) vmsvgaR3DeregisterGmr(PPDMDEVINS pDevIns, uint32_t gmrId)
2813{
2814 PVGASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVGASTATE);
2815 PVMSVGAR3STATE pSVGAState = pThisCC->svga.pSvgaR3State;
2816 PGMR pGMR = &pSVGAState->paGMR[gmrId];
2817
2818 for (uint32_t i = 0; i < pGMR->numDescriptors; i++)
2819 {
2820 int rc = PGMHandlerPhysicalDeregister(PDMDevHlpGetVM(pDevIns), pGMR->paDesc[i].GCPhys);
2821 AssertRC(rc);
2822 }
2823 return VINF_SUCCESS;
2824}
2825
2826/** Callback handler for VMR3ReqCallWaitU */
2827static DECLCALLBACK(int) vmsvgaR3ResetGmrHandlers(PVGASTATE pThis)
2828{
2829 PVMSVGAR3STATE pSVGAState = pThisCC->svga.pSvgaR3State;
2830
2831 for (uint32_t i = 0; i < pThis->svga.cGMR; ++i)
2832 {
2833 PGMR pGMR = &pSVGAState->paGMR[i];
2834
2835 if (pGMR->numDescriptors)
2836 {
2837 for (uint32_t j = 0; j < pGMR->numDescriptors; j++)
2838 {
2839 int rc = PGMHandlerPhysicalReset(PDMDevHlpGetVM(pDevIns), pGMR->paDesc[j].GCPhys);
2840 AssertRC(rc);
2841 }
2842 }
2843 }
2844 return VINF_SUCCESS;
2845}
2846
2847# endif /* IN_RING3 */
2848#endif /* DEBUG_GMR_ACCESS */
2849
2850/* -=-=-=-=-=- Ring 3 -=-=-=-=-=- */
2851
2852#ifdef IN_RING3
2853
2854
2855/*
2856 *
2857 * Command buffer submission.
2858 *
2859 * Guest submits a buffer by writing to SVGA_REG_COMMAND_LOW register.
2860 *
2861 * EMT thread appends a command buffer to the context queue (VMSVGACMDBUFCTX::listSubmitted)
2862 * and wakes up the FIFO thread.
2863 *
2864 * FIFO thread fetches the command buffer from the queue, processes the commands and writes
2865 * the buffer header back to the guest memory.
2866 *
2867 * If buffers are preempted, then the EMT thread removes all buffers from the context queue.
2868 *
2869 */
2870
2871
2872/** Update a command buffer header 'status' and 'errorOffset' fields in the guest memory.
2873 *
2874 * @param pDevIns The device instance.
2875 * @param GCPhysCB Guest physical address of the command buffer header.
2876 * @param status Command buffer status (SVGA_CB_STATUS_*).
2877 * @param errorOffset Offset to the first byte of the failing command for SVGA_CB_STATUS_COMMAND_ERROR.
2878 * errorOffset is ignored if the status is not SVGA_CB_STATUS_COMMAND_ERROR.
2879 * @thread FIFO or EMT.
2880 */
2881static void vmsvgaR3CmdBufWriteStatus(PPDMDEVINS pDevIns, RTGCPHYS GCPhysCB, SVGACBStatus status, uint32_t errorOffset)
2882{
2883 SVGACBHeader hdr;
2884 hdr.status = status;
2885 hdr.errorOffset = errorOffset;
2886 AssertCompile( RT_OFFSETOF(SVGACBHeader, status) == 0
2887 && RT_OFFSETOF(SVGACBHeader, errorOffset) == 4
2888 && RT_OFFSETOF(SVGACBHeader, id) == 8);
2889 size_t const cbWrite = status == SVGA_CB_STATUS_COMMAND_ERROR
2890 ? RT_UOFFSET_AFTER(SVGACBHeader, errorOffset) /* Both 'status' and 'errorOffset' fields. */
2891 : RT_UOFFSET_AFTER(SVGACBHeader, status); /* Only 'status' field. */
2892 PDMDevHlpPCIPhysWrite(pDevIns, GCPhysCB, &hdr, cbWrite);
2893}
2894
2895
2896/** Raise an IRQ.
2897 *
2898 * @param pDevIns The device instance.
2899 * @param pThis The shared VGA/VMSVGA state.
2900 * @param u32IrqStatus SVGA_IRQFLAG_* bits.
2901 * @thread FIFO or EMT.
2902 */
2903static void vmsvgaR3CmdBufRaiseIRQ(PPDMDEVINS pDevIns, PVGASTATE pThis, uint32_t u32IrqStatus)
2904{
2905 int rc = PDMDevHlpCritSectEnter(pDevIns, &pThis->CritSect, VERR_IGNORED);
2906 AssertRC(rc);
2907
2908 if (pThis->svga.u32IrqMask & u32IrqStatus)
2909 {
2910 LogFunc(("Trigger interrupt with status %#x\n", u32IrqStatus));
2911 ASMAtomicOrU32(&pThis->svga.u32IrqStatus, u32IrqStatus);
2912 PDMDevHlpPCISetIrq(pDevIns, 0, 1);
2913 }
2914
2915 PDMDevHlpCritSectLeave(pDevIns, &pThis->CritSect);
2916}
2917
2918
2919/** Allocate a command buffer structure.
2920 *
2921 * @param pCmdBufCtx The command buffer context which must allocate the buffer.
2922 * @return Pointer to the allocated command buffer structure.
2923 */
2924static PVMSVGACMDBUF vmsvgaR3CmdBufAlloc(PVMSVGACMDBUFCTX pCmdBufCtx)
2925{
2926 if (!pCmdBufCtx)
2927 return NULL;
2928
2929 PVMSVGACMDBUF pCmdBuf = (PVMSVGACMDBUF)RTMemAllocZ(sizeof(*pCmdBuf));
2930 if (pCmdBuf)
2931 {
2932 // RT_ZERO(pCmdBuf->nodeBuffer);
2933 pCmdBuf->pCmdBufCtx = pCmdBufCtx;
2934 // pCmdBuf->GCPhysCB = 0;
2935 // RT_ZERO(pCmdBuf->hdr);
2936 // pCmdBuf->pvCommands = NULL;
2937 }
2938
2939 return pCmdBuf;
2940}
2941
2942
2943/** Free a command buffer structure.
2944 *
2945 * @param pCmdBuf The command buffer pointer.
2946 */
2947static void vmsvgaR3CmdBufFree(PVMSVGACMDBUF pCmdBuf)
2948{
2949 if (pCmdBuf)
2950 RTMemFree(pCmdBuf->pvCommands);
2951 RTMemFree(pCmdBuf);
2952}
2953
2954
2955/** Initialize a command buffer context.
2956 *
2957 * @param pCmdBufCtx The command buffer context.
2958 */
2959static void vmsvgaR3CmdBufCtxInit(PVMSVGACMDBUFCTX pCmdBufCtx)
2960{
2961 RTListInit(&pCmdBufCtx->listSubmitted);
2962 pCmdBufCtx->cSubmitted = 0;
2963}
2964
2965
2966/** Destroy a command buffer context.
2967 *
2968 * @param pCmdBufCtx The command buffer context pointer.
2969 */
2970static void vmsvgaR3CmdBufCtxTerm(PVMSVGACMDBUFCTX pCmdBufCtx)
2971{
2972 if (!pCmdBufCtx)
2973 return;
2974
2975 if (pCmdBufCtx->listSubmitted.pNext)
2976 {
2977 /* If the list has been initialized. */
2978 PVMSVGACMDBUF pIter, pNext;
2979 RTListForEachSafe(&pCmdBufCtx->listSubmitted, pIter, pNext, VMSVGACMDBUF, nodeBuffer)
2980 {
2981 RTListNodeRemove(&pIter->nodeBuffer);
2982 --pCmdBufCtx->cSubmitted;
2983 vmsvgaR3CmdBufFree(pIter);
2984 }
2985 }
2986 Assert(pCmdBufCtx->cSubmitted == 0);
2987 pCmdBufCtx->cSubmitted = 0;
2988}
2989
2990
2991/** Handles SVGA_DC_CMD_START_STOP_CONTEXT command.
2992 *
2993 * @param pSvgaR3State VMSVGA R3 state.
2994 * @param pCmd The command data.
2995 * @return SVGACBStatus code.
2996 * @thread EMT
2997 */
2998static SVGACBStatus vmsvgaR3CmdBufDCStartStop(PVMSVGAR3STATE pSvgaR3State, SVGADCCmdStartStop const *pCmd)
2999{
3000 /* Create or destroy a regular command buffer context. */
3001 if (pCmd->context >= RT_ELEMENTS(pSvgaR3State->apCmdBufCtxs))
3002 return SVGA_CB_STATUS_COMMAND_ERROR;
3003 RT_UNTRUSTED_VALIDATED_FENCE();
3004
3005 SVGACBStatus CBStatus = SVGA_CB_STATUS_COMPLETED;
3006
3007 int rc = RTCritSectEnter(&pSvgaR3State->CritSectCmdBuf);
3008 AssertRC(rc);
3009 if (pCmd->enable)
3010 {
3011 pSvgaR3State->apCmdBufCtxs[pCmd->context] = (PVMSVGACMDBUFCTX)RTMemAlloc(sizeof(VMSVGACMDBUFCTX));
3012 if (pSvgaR3State->apCmdBufCtxs[pCmd->context])
3013 vmsvgaR3CmdBufCtxInit(pSvgaR3State->apCmdBufCtxs[pCmd->context]);
3014 else
3015 CBStatus = SVGA_CB_STATUS_QUEUE_FULL;
3016 }
3017 else
3018 {
3019 vmsvgaR3CmdBufCtxTerm(pSvgaR3State->apCmdBufCtxs[pCmd->context]);
3020 pSvgaR3State->apCmdBufCtxs[pCmd->context] = NULL;
3021 }
3022 RTCritSectLeave(&pSvgaR3State->CritSectCmdBuf);
3023
3024 return CBStatus;
3025}
3026
3027
3028/** Handles SVGA_DC_CMD_PREEMPT command.
3029 *
3030 * @param pDevIns The device instance.
3031 * @param pSvgaR3State VMSVGA R3 state.
3032 * @param pCmd The command data.
3033 * @return SVGACBStatus code.
3034 * @thread EMT
3035 */
3036static SVGACBStatus vmsvgaR3CmdBufDCPreempt(PPDMDEVINS pDevIns, PVMSVGAR3STATE pSvgaR3State, SVGADCCmdPreempt const *pCmd)
3037{
3038 /* Remove buffers from the processing queue of the specified context. */
3039 if (pCmd->context >= RT_ELEMENTS(pSvgaR3State->apCmdBufCtxs))
3040 return SVGA_CB_STATUS_COMMAND_ERROR;
3041 RT_UNTRUSTED_VALIDATED_FENCE();
3042
3043 PVMSVGACMDBUFCTX const pCmdBufCtx = pSvgaR3State->apCmdBufCtxs[pCmd->context];
3044 RTLISTANCHOR listPreempted;
3045
3046 int rc = RTCritSectEnter(&pSvgaR3State->CritSectCmdBuf);
3047 AssertRC(rc);
3048 if (pCmd->ignoreIDZero)
3049 {
3050 RTListInit(&listPreempted);
3051
3052 PVMSVGACMDBUF pIter, pNext;
3053 RTListForEachSafe(&pCmdBufCtx->listSubmitted, pIter, pNext, VMSVGACMDBUF, nodeBuffer)
3054 {
3055 if (pIter->hdr.id == 0)
3056 continue;
3057
3058 RTListNodeRemove(&pIter->nodeBuffer);
3059 --pCmdBufCtx->cSubmitted;
3060 RTListAppend(&listPreempted, &pIter->nodeBuffer);
3061 }
3062 }
3063 else
3064 {
3065 RTListMove(&listPreempted, &pCmdBufCtx->listSubmitted);
3066 }
3067 RTCritSectLeave(&pSvgaR3State->CritSectCmdBuf);
3068
3069 PVMSVGACMDBUF pIter, pNext;
3070 RTListForEachSafe(&listPreempted, pIter, pNext, VMSVGACMDBUF, nodeBuffer)
3071 {
3072 RTListNodeRemove(&pIter->nodeBuffer);
3073 vmsvgaR3CmdBufWriteStatus(pDevIns, pIter->GCPhysCB, SVGA_CB_STATUS_PREEMPTED, 0);
3074 vmsvgaR3CmdBufFree(pIter);
3075 }
3076
3077 return SVGA_CB_STATUS_COMPLETED;
3078}
3079
3080
3081/** @def VMSVGA_INC_CMD_SIZE_BREAK
3082 * Increments the size of the command cbCmd by a_cbMore.
3083 * Checks that the command buffer has at least cbCmd bytes. Will break out of the switch if it doesn't.
3084 * Used by vmsvgaR3CmdBufProcessDC and vmsvgaR3CmdBufProcessCommands.
3085 */
3086#define VMSVGA_INC_CMD_SIZE_BREAK(a_cbMore) \
3087 if (1) { \
3088 cbCmd += (a_cbMore); \
3089 ASSERT_GUEST_MSG_STMT_BREAK(cbRemain >= cbCmd, ("size=%#x remain=%#zx\n", cbCmd, (size_t)cbRemain), CBstatus = SVGA_CB_STATUS_COMMAND_ERROR); \
3090 RT_UNTRUSTED_VALIDATED_FENCE(); \
3091 } else do {} while (0)
3092
3093
3094/** Processes Device Context command buffer.
3095 *
3096 * @param pDevIns The device instance.
3097 * @param pSvgaR3State VMSVGA R3 state.
3098 * @param pvCommands Pointer to the command buffer.
3099 * @param cbCommands Size of the command buffer.
3100 * @param poffNextCmd Where to store the offset of the first unprocessed command.
3101 * @return SVGACBStatus code.
3102 * @thread EMT
3103 */
3104static SVGACBStatus vmsvgaR3CmdBufProcessDC(PPDMDEVINS pDevIns, PVMSVGAR3STATE pSvgaR3State, void const *pvCommands, uint32_t cbCommands, uint32_t *poffNextCmd)
3105{
3106 SVGACBStatus CBstatus = SVGA_CB_STATUS_COMPLETED;
3107
3108 uint8_t const *pu8Cmd = (uint8_t *)pvCommands;
3109 uint32_t cbRemain = cbCommands;
3110 while (cbRemain)
3111 {
3112 /* Command identifier is a 32 bit value. */
3113 if (cbRemain < sizeof(uint32_t))
3114 {
3115 CBstatus = SVGA_CB_STATUS_COMMAND_ERROR;
3116 break;
3117 }
3118
3119 /* Fetch the command id. */
3120 uint32_t const cmdId = *(uint32_t *)pu8Cmd;
3121 uint32_t cbCmd = sizeof(uint32_t);
3122 switch (cmdId)
3123 {
3124 case SVGA_DC_CMD_NOP:
3125 {
3126 /* NOP */
3127 break;
3128 }
3129
3130 case SVGA_DC_CMD_START_STOP_CONTEXT:
3131 {
3132 SVGADCCmdStartStop *pCmd = (SVGADCCmdStartStop *)&pu8Cmd[cbCmd];
3133 VMSVGA_INC_CMD_SIZE_BREAK(sizeof(*pCmd));
3134 CBstatus = vmsvgaR3CmdBufDCStartStop(pSvgaR3State, pCmd);
3135 break;
3136 }
3137
3138 case SVGA_DC_CMD_PREEMPT:
3139 {
3140 SVGADCCmdPreempt *pCmd = (SVGADCCmdPreempt *)&pu8Cmd[cbCmd];
3141 VMSVGA_INC_CMD_SIZE_BREAK(sizeof(*pCmd));
3142 CBstatus = vmsvgaR3CmdBufDCPreempt(pDevIns, pSvgaR3State, pCmd);
3143 break;
3144 }
3145
3146 default:
3147 {
3148 /* Unsupported command. */
3149 CBstatus = SVGA_CB_STATUS_COMMAND_ERROR;
3150 break;
3151 }
3152 }
3153
3154 if (CBstatus != SVGA_CB_STATUS_COMPLETED)
3155 break;
3156
3157 pu8Cmd += cbCmd;
3158 cbRemain -= cbCmd;
3159 }
3160
3161 Assert(cbRemain <= cbCommands);
3162 *poffNextCmd = cbCommands - cbRemain;
3163 return CBstatus;
3164}
3165
3166
3167/** Submits a device context command buffer for synchronous processing.
3168 *
3169 * @param pDevIns The device instance.
3170 * @param pThisCC The VGA/VMSVGA state for the current context.
3171 * @param ppCmdBuf Pointer to the command buffer pointer.
3172 * The function can set the command buffer pointer to NULL to prevent deallocation by the caller.
3173 * @param poffNextCmd Where to store the offset of the first unprocessed command.
3174 * @return SVGACBStatus code.
3175 * @thread EMT
3176 */
3177static SVGACBStatus vmsvgaR3CmdBufSubmitDC(PPDMDEVINS pDevIns, PVGASTATECC pThisCC, PVMSVGACMDBUF *ppCmdBuf, uint32_t *poffNextCmd)
3178{
3179 /* Synchronously process the device context commands. */
3180 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
3181 return vmsvgaR3CmdBufProcessDC(pDevIns, pSvgaR3State, (*ppCmdBuf)->pvCommands, (*ppCmdBuf)->hdr.length, poffNextCmd);
3182}
3183
3184/** Submits a command buffer for asynchronous processing by the FIFO thread.
3185 *
3186 * @param pDevIns The device instance.
3187 * @param pThis The shared VGA/VMSVGA state.
3188 * @param pThisCC The VGA/VMSVGA state for the current context.
3189 * @param ppCmdBuf Pointer to the command buffer pointer.
3190 * The function can set the command buffer pointer to NULL to prevent deallocation by the caller.
3191 * @return SVGACBStatus code.
3192 * @thread EMT
3193 */
3194static SVGACBStatus vmsvgaR3CmdBufSubmit(PPDMDEVINS pDevIns, PVGASTATE pThis, PVGASTATECC pThisCC, PVMSVGACMDBUF *ppCmdBuf)
3195{
3196 /* Command buffer submission. */
3197 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
3198
3199 SVGACBStatus CBstatus = SVGA_CB_STATUS_NONE;
3200
3201 PVMSVGACMDBUF const pCmdBuf = *ppCmdBuf;
3202 PVMSVGACMDBUFCTX const pCmdBufCtx = pCmdBuf->pCmdBufCtx;
3203
3204 int rc = RTCritSectEnter(&pSvgaR3State->CritSectCmdBuf);
3205 AssertRC(rc);
3206
3207 if (RT_LIKELY(pCmdBufCtx->cSubmitted < SVGA_CB_MAX_QUEUED_PER_CONTEXT))
3208 {
3209 RTListAppend(&pCmdBufCtx->listSubmitted, &pCmdBuf->nodeBuffer);
3210 ++pCmdBufCtx->cSubmitted;
3211 *ppCmdBuf = NULL; /* Consume the buffer. */
3212 ASMAtomicWriteU32(&pThisCC->svga.pSvgaR3State->fCmdBuf, 1);
3213 }
3214 else
3215 CBstatus = SVGA_CB_STATUS_QUEUE_FULL;
3216
3217 RTCritSectLeave(&pSvgaR3State->CritSectCmdBuf);
3218
3219 /* Inform the FIFO thread. */
3220 if (*ppCmdBuf == NULL)
3221 PDMDevHlpSUPSemEventSignal(pDevIns, pThis->svga.hFIFORequestSem);
3222
3223 return CBstatus;
3224}
3225
3226
3227/** SVGA_REG_COMMAND_LOW write handler.
3228 * Submits a command buffer to the FIFO thread or processes a device context command.
3229 *
3230 * @param pDevIns The device instance.
3231 * @param pThis The shared VGA/VMSVGA state.
3232 * @param pThisCC The VGA/VMSVGA state for the current context.
3233 * @param GCPhysCB Guest physical address of the command buffer header.
3234 * @param CBCtx Context the command buffer is submitted to.
3235 * @thread EMT
3236 */
3237static void vmsvgaR3CmdBufSubmit(PPDMDEVINS pDevIns, PVGASTATE pThis, PVGASTATECC pThisCC, RTGCPHYS GCPhysCB, SVGACBContext CBCtx)
3238{
3239 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
3240
3241 SVGACBStatus CBstatus = SVGA_CB_STATUS_NONE;
3242 uint32_t offNextCmd = 0;
3243 uint32_t fIRQ = 0;
3244
3245 /* Get the context if the device has the capability. */
3246 PVMSVGACMDBUFCTX pCmdBufCtx = NULL;
3247 if (pThis->svga.u32DeviceCaps & SVGA_CAP_COMMAND_BUFFERS)
3248 {
3249 if (RT_LIKELY(CBCtx < RT_ELEMENTS(pSvgaR3State->apCmdBufCtxs)))
3250 pCmdBufCtx = pSvgaR3State->apCmdBufCtxs[CBCtx];
3251 else if (CBCtx == SVGA_CB_CONTEXT_DEVICE)
3252 pCmdBufCtx = &pSvgaR3State->CmdBufCtxDC;
3253 RT_UNTRUSTED_VALIDATED_FENCE();
3254 }
3255
3256 /* Allocate a new command buffer. */
3257 PVMSVGACMDBUF pCmdBuf = vmsvgaR3CmdBufAlloc(pCmdBufCtx);
3258 if (RT_LIKELY(pCmdBuf))
3259 {
3260 pCmdBuf->GCPhysCB = GCPhysCB;
3261
3262 int rc = PDMDevHlpPCIPhysRead(pDevIns, GCPhysCB, &pCmdBuf->hdr, sizeof(pCmdBuf->hdr));
3263 if (RT_SUCCESS(rc))
3264 {
3265 /* Verify the command buffer header. */
3266 if (RT_LIKELY( pCmdBuf->hdr.status == SVGA_CB_STATUS_NONE
3267 && (pCmdBuf->hdr.flags & ~(SVGA_CB_FLAG_NO_IRQ)) == 0 /* No unexpected flags. */
3268 && pCmdBuf->hdr.length <= SVGA_CB_MAX_SIZE))
3269 {
3270 RT_UNTRUSTED_VALIDATED_FENCE();
3271
3272 /* Read the command buffer content. */
3273 pCmdBuf->pvCommands = RTMemAlloc(pCmdBuf->hdr.length);
3274 if (pCmdBuf->pvCommands)
3275 {
3276 RTGCPHYS const GCPhysCmd = (RTGCPHYS)pCmdBuf->hdr.ptr.pa;
3277 rc = PDMDevHlpPCIPhysRead(pDevIns, GCPhysCmd, pCmdBuf->pvCommands, pCmdBuf->hdr.length);
3278 if (RT_SUCCESS(rc))
3279 {
3280 /* Submit the buffer. Device context buffers will be processed synchronously. */
3281 if (RT_LIKELY(CBCtx < RT_ELEMENTS(pSvgaR3State->apCmdBufCtxs)))
3282 /* This usually processes the CB async and sets pCmbBuf to NULL. */
3283 CBstatus = vmsvgaR3CmdBufSubmit(pDevIns, pThis, pThisCC, &pCmdBuf);
3284 else
3285 CBstatus = vmsvgaR3CmdBufSubmitDC(pDevIns, pThisCC, &pCmdBuf, &offNextCmd);
3286 }
3287 else
3288 {
3289 ASSERT_GUEST_MSG_FAILED(("Failed to read commands at %RGp\n", GCPhysCmd));
3290 CBstatus = SVGA_CB_STATUS_CB_HEADER_ERROR;
3291 fIRQ = SVGA_IRQFLAG_ERROR | SVGA_IRQFLAG_COMMAND_BUFFER;
3292 }
3293 }
3294 else
3295 {
3296 /* No memory for commands. */
3297 CBstatus = SVGA_CB_STATUS_QUEUE_FULL;
3298 }
3299 }
3300 else
3301 {
3302 ASSERT_GUEST_MSG_FAILED(("Invalid buffer header\n"));
3303 CBstatus = SVGA_CB_STATUS_CB_HEADER_ERROR;
3304 fIRQ = SVGA_IRQFLAG_ERROR | SVGA_IRQFLAG_COMMAND_BUFFER;
3305 }
3306 }
3307 else
3308 {
3309 LogFunc(("Failed to read buffer header at %RGp\n", GCPhysCB));
3310 ASSERT_GUEST_FAILED();
3311 /* Do not attempt to write the status. */
3312 }
3313
3314 /* Free the buffer if pfnCmdBufSubmit did not consume it. */
3315 vmsvgaR3CmdBufFree(pCmdBuf);
3316 }
3317 else
3318 {
3319 LogFunc(("Can't allocate buffer for context id %#x\n", CBCtx));
3320 ASSERT_GUEST_FAILED();
3321 CBstatus = SVGA_CB_STATUS_QUEUE_FULL;
3322 }
3323
3324 if (CBstatus != SVGA_CB_STATUS_NONE)
3325 {
3326 LogFunc(("Write status %#x, offNextCmd %#x (of %#x), fIRQ %#x\n", CBstatus, offNextCmd, pCmdBuf->hdr.length, fIRQ));
3327 vmsvgaR3CmdBufWriteStatus(pDevIns, GCPhysCB, CBstatus, offNextCmd);
3328 if (fIRQ)
3329 vmsvgaR3CmdBufRaiseIRQ(pDevIns, pThis, fIRQ);
3330 }
3331}
3332
3333
3334/** Checks if there are some buffers to be processed.
3335 *
3336 * @param pThisCC The VGA/VMSVGA state for the current context.
3337 * @return true if buffers must be processed.
3338 * @thread FIFO
3339 */
3340static bool vmsvgaR3CmdBufHasWork(PVGASTATECC pThisCC)
3341{
3342 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
3343 return RT_BOOL(ASMAtomicReadU32(&pSvgaR3State->fCmdBuf));
3344}
3345
3346
3347/** Processes a command buffer.
3348 *
3349 * @param pDevIns The device instance.
3350 * @param pThis The shared VGA/VMSVGA state.
3351 * @param pThisCC The VGA/VMSVGA state for the current context.
3352 * @param pvCommands Pointer to the command buffer.
3353 * @param cbCommands Size of the command buffer.
3354 * @param poffNextCmd Where to store the offset of the first unprocessed command.
3355 * @param pu32IrqStatus Where to store SVGA_IRQFLAG_ if the IRQ is generated by the last command in the buffer.
3356 * @return SVGACBStatus code.
3357 * @thread FIFO
3358 */
3359static SVGACBStatus vmsvgaR3CmdBufProcessCommands(PPDMDEVINS pDevIns, PVGASTATE pThis, PVGASTATECC pThisCC, void const *pvCommands, uint32_t cbCommands, uint32_t *poffNextCmd, uint32_t *pu32IrqStatus)
3360{
3361 SVGACBStatus CBstatus = SVGA_CB_STATUS_COMPLETED;
3362 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
3363
3364 uint32_t RT_UNTRUSTED_VOLATILE_GUEST * const pFIFO = pThisCC->svga.pau32FIFO;
3365
3366 uint8_t const *pu8Cmd = (uint8_t *)pvCommands;
3367 uint32_t cbRemain = cbCommands;
3368 while (cbRemain)
3369 {
3370 /* Command identifier is a 32 bit value. */
3371 if (cbRemain < sizeof(uint32_t))
3372 {
3373 CBstatus = SVGA_CB_STATUS_COMMAND_ERROR;
3374 break;
3375 }
3376
3377 /* Fetch the command id.
3378 * 'cmdId' is actually a SVGAFifoCmdId. It is treated as uint32_t in order to avoid a compiler
3379 * warning. Because we support some obsolete and deprecated commands, which are not included in
3380 * the SVGAFifoCmdId enum in the VMSVGA headers anymore.
3381 */
3382 uint32_t const cmdId = *(uint32_t *)pu8Cmd;
3383 uint32_t cbCmd = sizeof(uint32_t);
3384
3385 LogFlowFunc(("%s %d\n", vmsvgaR3FifoCmdToString(cmdId), cmdId));
3386
3387 /* At the end of the switch cbCmd is equal to the total length of the command including the cmdId.
3388 * I.e. pu8Cmd + cbCmd must point to the next command.
3389 * However if CBstatus is set to anything but SVGA_CB_STATUS_COMPLETED in the switch, then
3390 * the cbCmd value is ignored (and pu8Cmd still points to the failed command).
3391 */
3392 /** @todo This code is very similar to the FIFO loop command processing. Think about merging. */
3393 switch (cmdId)
3394 {
3395 case SVGA_CMD_INVALID_CMD:
3396 {
3397 /* Nothing to do. */
3398 STAM_REL_COUNTER_INC(&pSvgaR3State->StatR3CmdInvalidCmd);
3399 break;
3400 }
3401
3402 case SVGA_CMD_FENCE:
3403 {
3404 SVGAFifoCmdFence *pCmd = (SVGAFifoCmdFence *)&pu8Cmd[cbCmd];
3405 VMSVGA_INC_CMD_SIZE_BREAK(sizeof(*pCmd));
3406 STAM_REL_COUNTER_INC(&pSvgaR3State->StatR3CmdFence);
3407 Log(("SVGA_CMD_FENCE %#x\n", pCmd->fence));
3408
3409 uint32_t const offFifoMin = pFIFO[SVGA_FIFO_MIN];
3410 if (VMSVGA_IS_VALID_FIFO_REG(SVGA_FIFO_FENCE, offFifoMin))
3411 {
3412 pFIFO[SVGA_FIFO_FENCE] = pCmd->fence;
3413
3414 if (pThis->svga.u32IrqMask & SVGA_IRQFLAG_ANY_FENCE)
3415 {
3416 Log(("any fence irq\n"));
3417 *pu32IrqStatus |= SVGA_IRQFLAG_ANY_FENCE;
3418 }
3419 else if ( VMSVGA_IS_VALID_FIFO_REG(SVGA_FIFO_FENCE_GOAL, offFifoMin)
3420 && (pThis->svga.u32IrqMask & SVGA_IRQFLAG_FENCE_GOAL)
3421 && pFIFO[SVGA_FIFO_FENCE_GOAL] == pCmd->fence)
3422 {
3423 Log(("fence goal reached irq (fence=%#x)\n", pCmd->fence));
3424 *pu32IrqStatus |= SVGA_IRQFLAG_FENCE_GOAL;
3425 }
3426 }
3427 else
3428 Log(("SVGA_CMD_FENCE is bogus when offFifoMin is %#x!\n", offFifoMin));
3429 break;
3430 }
3431
3432 case SVGA_CMD_UPDATE:
3433 {
3434 SVGAFifoCmdUpdate *pCmd = (SVGAFifoCmdUpdate *)&pu8Cmd[cbCmd];
3435 VMSVGA_INC_CMD_SIZE_BREAK(sizeof(*pCmd));
3436 vmsvgaR3CmdUpdate(pThis, pThisCC, pCmd);
3437 break;
3438 }
3439
3440 case SVGA_CMD_UPDATE_VERBOSE:
3441 {
3442 SVGAFifoCmdUpdateVerbose *pCmd = (SVGAFifoCmdUpdateVerbose *)&pu8Cmd[cbCmd];
3443 VMSVGA_INC_CMD_SIZE_BREAK(sizeof(*pCmd));
3444 vmsvgaR3CmdUpdateVerbose(pThis, pThisCC, pCmd);
3445 break;
3446 }
3447
3448 case SVGA_CMD_DEFINE_CURSOR:
3449 {
3450 /* Followed by bitmap data. */
3451 SVGAFifoCmdDefineCursor *pCmd = (SVGAFifoCmdDefineCursor *)&pu8Cmd[cbCmd];
3452 VMSVGA_INC_CMD_SIZE_BREAK(sizeof(*pCmd));
3453
3454 /* Figure out the size of the bitmap data. */
3455 ASSERT_GUEST_STMT_BREAK(pCmd->height < 2048 && pCmd->width < 2048, CBstatus = SVGA_CB_STATUS_COMMAND_ERROR);
3456 ASSERT_GUEST_STMT_BREAK(pCmd->andMaskDepth <= 32, CBstatus = SVGA_CB_STATUS_COMMAND_ERROR);
3457 ASSERT_GUEST_STMT_BREAK(pCmd->xorMaskDepth <= 32, CBstatus = SVGA_CB_STATUS_COMMAND_ERROR);
3458 RT_UNTRUSTED_VALIDATED_FENCE();
3459
3460 uint32_t const cbAndLine = RT_ALIGN_32(pCmd->width * (pCmd->andMaskDepth + (pCmd->andMaskDepth == 15)), 32) / 8;
3461 uint32_t const cbAndMask = cbAndLine * pCmd->height;
3462 uint32_t const cbXorLine = RT_ALIGN_32(pCmd->width * (pCmd->xorMaskDepth + (pCmd->xorMaskDepth == 15)), 32) / 8;
3463 uint32_t const cbXorMask = cbXorLine * pCmd->height;
3464
3465 VMSVGA_INC_CMD_SIZE_BREAK(cbAndMask + cbXorMask);
3466 vmsvgaR3CmdDefineCursor(pThis, pThisCC, pCmd);
3467 break;
3468 }
3469
3470 case SVGA_CMD_DEFINE_ALPHA_CURSOR:
3471 {
3472 /* Followed by bitmap data. */
3473 SVGAFifoCmdDefineAlphaCursor *pCmd = (SVGAFifoCmdDefineAlphaCursor *)&pu8Cmd[cbCmd];
3474 VMSVGA_INC_CMD_SIZE_BREAK(sizeof(*pCmd));
3475
3476 /* Figure out the size of the bitmap data. */
3477 ASSERT_GUEST_STMT_BREAK(pCmd->height < 2048 && pCmd->width < 2048, CBstatus = SVGA_CB_STATUS_COMMAND_ERROR);
3478
3479 VMSVGA_INC_CMD_SIZE_BREAK(pCmd->width * pCmd->height * sizeof(uint32_t)); /* 32-bit BRGA format */
3480 vmsvgaR3CmdDefineAlphaCursor(pThis, pThisCC, pCmd);
3481 break;
3482 }
3483
3484 case SVGA_CMD_MOVE_CURSOR:
3485 {
3486 /* Deprecated; there should be no driver which *requires* this command. However, if
3487 * we do ecncounter this command, it might be useful to not get the FIFO completely out of
3488 * alignment.
3489 * May be issued by guest if SVGA_CAP_CURSOR_BYPASS is missing.
3490 */
3491 SVGAFifoCmdMoveCursor *pCmd = (SVGAFifoCmdMoveCursor *)&pu8Cmd[cbCmd];
3492 VMSVGA_INC_CMD_SIZE_BREAK(sizeof(*pCmd));
3493 vmsvgaR3CmdMoveCursor(pThis, pThisCC, pCmd);
3494 break;
3495 }
3496
3497 case SVGA_CMD_DISPLAY_CURSOR:
3498 {
3499 /* Deprecated; there should be no driver which *requires* this command. However, if
3500 * we do ecncounter this command, it might be useful to not get the FIFO completely out of
3501 * alignment.
3502 * May be issued by guest if SVGA_CAP_CURSOR_BYPASS is missing.
3503 */
3504 SVGAFifoCmdDisplayCursor *pCmd = (SVGAFifoCmdDisplayCursor *)&pu8Cmd[cbCmd];
3505 VMSVGA_INC_CMD_SIZE_BREAK(sizeof(*pCmd));
3506 vmsvgaR3CmdDisplayCursor(pThis, pThisCC, pCmd);
3507 break;
3508 }
3509
3510 case SVGA_CMD_RECT_FILL:
3511 {
3512 SVGAFifoCmdRectFill *pCmd = (SVGAFifoCmdRectFill *)&pu8Cmd[cbCmd];
3513 VMSVGA_INC_CMD_SIZE_BREAK(sizeof(*pCmd));
3514 vmsvgaR3CmdRectFill(pThis, pThisCC, pCmd);
3515 break;
3516 }
3517
3518 case SVGA_CMD_RECT_COPY:
3519 {
3520 SVGAFifoCmdRectCopy *pCmd = (SVGAFifoCmdRectCopy *)&pu8Cmd[cbCmd];
3521 VMSVGA_INC_CMD_SIZE_BREAK(sizeof(*pCmd));
3522 vmsvgaR3CmdRectCopy(pThis, pThisCC, pCmd);
3523 break;
3524 }
3525
3526 case SVGA_CMD_RECT_ROP_COPY:
3527 {
3528 SVGAFifoCmdRectRopCopy *pCmd = (SVGAFifoCmdRectRopCopy *)&pu8Cmd[cbCmd];
3529 VMSVGA_INC_CMD_SIZE_BREAK(sizeof(*pCmd));
3530 vmsvgaR3CmdRectRopCopy(pThis, pThisCC, pCmd);
3531 break;
3532 }
3533
3534 case SVGA_CMD_ESCAPE:
3535 {
3536 /* Followed by 'size' bytes of data. */
3537 SVGAFifoCmdEscape *pCmd = (SVGAFifoCmdEscape *)&pu8Cmd[cbCmd];
3538 VMSVGA_INC_CMD_SIZE_BREAK(sizeof(*pCmd));
3539
3540 ASSERT_GUEST_STMT_BREAK(pCmd->size < pThis->svga.cbFIFO - sizeof(SVGAFifoCmdEscape), CBstatus = SVGA_CB_STATUS_COMMAND_ERROR);
3541 RT_UNTRUSTED_VALIDATED_FENCE();
3542
3543 VMSVGA_INC_CMD_SIZE_BREAK(pCmd->size);
3544 vmsvgaR3CmdEscape(pThis, pThisCC, pCmd);
3545 break;
3546 }
3547# ifdef VBOX_WITH_VMSVGA3D
3548 case SVGA_CMD_DEFINE_GMR2:
3549 {
3550 SVGAFifoCmdDefineGMR2 *pCmd = (SVGAFifoCmdDefineGMR2 *)&pu8Cmd[cbCmd];
3551 VMSVGA_INC_CMD_SIZE_BREAK(sizeof(*pCmd));
3552 vmsvgaR3CmdDefineGMR2(pThis, pThisCC, pCmd);
3553 break;
3554 }
3555
3556 case SVGA_CMD_REMAP_GMR2:
3557 {
3558 /* Followed by page descriptors or guest ptr. */
3559 SVGAFifoCmdRemapGMR2 *pCmd = (SVGAFifoCmdRemapGMR2 *)&pu8Cmd[cbCmd];
3560 VMSVGA_INC_CMD_SIZE_BREAK(sizeof(*pCmd));
3561
3562 /* Calculate the size of what comes after next and fetch it. */
3563 uint32_t cbMore = 0;
3564 if (pCmd->flags & SVGA_REMAP_GMR2_VIA_GMR)
3565 cbMore = sizeof(SVGAGuestPtr);
3566 else
3567 {
3568 uint32_t const cbPageDesc = (pCmd->flags & SVGA_REMAP_GMR2_PPN64) ? sizeof(uint64_t) : sizeof(uint32_t);
3569 if (pCmd->flags & SVGA_REMAP_GMR2_SINGLE_PPN)
3570 {
3571 cbMore = cbPageDesc;
3572 pCmd->numPages = 1;
3573 }
3574 else
3575 {
3576 ASSERT_GUEST_STMT_BREAK(pCmd->numPages <= pThis->svga.cbFIFO / cbPageDesc, CBstatus = SVGA_CB_STATUS_COMMAND_ERROR);
3577 cbMore = cbPageDesc * pCmd->numPages;
3578 }
3579 }
3580 VMSVGA_INC_CMD_SIZE_BREAK(cbMore);
3581 vmsvgaR3CmdRemapGMR2(pThis, pThisCC, pCmd);
3582# ifdef DEBUG_GMR_ACCESS
3583 VMR3ReqCallWaitU(PDMDevHlpGetUVM(pDevIns), VMCPUID_ANY, (PFNRT)vmsvgaR3RegisterGmr, 2, pDevIns, pCmd->gmrId);
3584# endif
3585 break;
3586 }
3587# endif /* VBOX_WITH_VMSVGA3D */
3588 case SVGA_CMD_DEFINE_SCREEN:
3589 {
3590 /* The size of this command is specified by the guest and depends on capabilities. */
3591 SVGAFifoCmdDefineScreen *pCmd = (SVGAFifoCmdDefineScreen *)&pu8Cmd[cbCmd];
3592 VMSVGA_INC_CMD_SIZE_BREAK(sizeof(pCmd->screen.structSize));
3593 ASSERT_GUEST_STMT_BREAK(pCmd->screen.structSize < pThis->svga.cbFIFO, CBstatus = SVGA_CB_STATUS_COMMAND_ERROR);
3594 RT_UNTRUSTED_VALIDATED_FENCE();
3595
3596 VMSVGA_INC_CMD_SIZE_BREAK(RT_MAX(sizeof(pCmd->screen.structSize), pCmd->screen.structSize) - sizeof(pCmd->screen.structSize));
3597 vmsvgaR3CmdDefineScreen(pThis, pThisCC, pCmd);
3598 break;
3599 }
3600
3601 case SVGA_CMD_DESTROY_SCREEN:
3602 {
3603 SVGAFifoCmdDestroyScreen *pCmd = (SVGAFifoCmdDestroyScreen *)&pu8Cmd[cbCmd];
3604 VMSVGA_INC_CMD_SIZE_BREAK(sizeof(*pCmd));
3605 vmsvgaR3CmdDestroyScreen(pThis, pThisCC, pCmd);
3606 break;
3607 }
3608
3609 case SVGA_CMD_DEFINE_GMRFB:
3610 {
3611 SVGAFifoCmdDefineGMRFB *pCmd = (SVGAFifoCmdDefineGMRFB *)&pu8Cmd[cbCmd];
3612 VMSVGA_INC_CMD_SIZE_BREAK(sizeof(*pCmd));
3613 vmsvgaR3CmdDefineGMRFB(pThis, pThisCC, pCmd);
3614 break;
3615 }
3616
3617 case SVGA_CMD_BLIT_GMRFB_TO_SCREEN:
3618 {
3619 SVGAFifoCmdBlitGMRFBToScreen *pCmd = (SVGAFifoCmdBlitGMRFBToScreen *)&pu8Cmd[cbCmd];
3620 VMSVGA_INC_CMD_SIZE_BREAK(sizeof(*pCmd));
3621 vmsvgaR3CmdBlitGMRFBToScreen(pThis, pThisCC, pCmd);
3622 break;
3623 }
3624
3625 case SVGA_CMD_BLIT_SCREEN_TO_GMRFB:
3626 {
3627 SVGAFifoCmdBlitScreenToGMRFB *pCmd = (SVGAFifoCmdBlitScreenToGMRFB *)&pu8Cmd[cbCmd];
3628 VMSVGA_INC_CMD_SIZE_BREAK(sizeof(*pCmd));
3629 vmsvgaR3CmdBlitScreenToGMRFB(pThis, pThisCC, pCmd);
3630 break;
3631 }
3632
3633 case SVGA_CMD_ANNOTATION_FILL:
3634 {
3635 SVGAFifoCmdAnnotationFill *pCmd = (SVGAFifoCmdAnnotationFill *)&pu8Cmd[cbCmd];
3636 VMSVGA_INC_CMD_SIZE_BREAK(sizeof(*pCmd));
3637 vmsvgaR3CmdAnnotationFill(pThis, pThisCC, pCmd);
3638 break;
3639 }
3640
3641 case SVGA_CMD_ANNOTATION_COPY:
3642 {
3643 SVGAFifoCmdAnnotationCopy *pCmd = (SVGAFifoCmdAnnotationCopy *)&pu8Cmd[cbCmd];
3644 VMSVGA_INC_CMD_SIZE_BREAK(sizeof(*pCmd));
3645 vmsvgaR3CmdAnnotationCopy(pThis, pThisCC, pCmd);
3646 break;
3647 }
3648
3649 default:
3650 {
3651# ifdef VBOX_WITH_VMSVGA3D
3652 if ( cmdId >= SVGA_3D_CMD_BASE
3653 && cmdId < SVGA_3D_CMD_MAX)
3654 {
3655 RT_UNTRUSTED_VALIDATED_FENCE();
3656
3657 /* All 3d commands start with a common header, which defines the identifier and the size
3658 * of the command. The identifier has been already read. Fetch the size.
3659 */
3660 uint32_t const *pcbMore = (uint32_t const *)&pu8Cmd[cbCmd];
3661 VMSVGA_INC_CMD_SIZE_BREAK(sizeof(*pcbMore));
3662 VMSVGA_INC_CMD_SIZE_BREAK(*pcbMore);
3663 if (RT_LIKELY(pThis->svga.f3DEnabled))
3664 { /* likely */ }
3665 else
3666 {
3667 LogRelMax(8, ("VMSVGA: 3D disabled, command %d skipped\n", cmdId));
3668 break;
3669 }
3670
3671 /* Command data begins after the 32 bit command length. */
3672 int rc = vmsvgaR3Process3dCmd(pThis, pThisCC, (SVGAFifo3dCmdId)cmdId, *pcbMore, pcbMore + 1);
3673 if (RT_SUCCESS(rc))
3674 { /* likely */ }
3675 else
3676 {
3677 CBstatus = SVGA_CB_STATUS_COMMAND_ERROR;
3678 break;
3679 }
3680 }
3681 else
3682# endif /* VBOX_WITH_VMSVGA3D */
3683 {
3684 /* Unsupported command. */
3685 STAM_REL_COUNTER_INC(&pSvgaR3State->StatFifoUnkCmds);
3686 ASSERT_GUEST_MSG_FAILED(("cmdId=%d\n", cmdId));
3687 LogRelMax(16, ("VMSVGA: unsupported command %d\n", cmdId));
3688 CBstatus = SVGA_CB_STATUS_COMMAND_ERROR;
3689 break;
3690 }
3691 }
3692 }
3693
3694 if (CBstatus != SVGA_CB_STATUS_COMPLETED)
3695 break;
3696
3697 pu8Cmd += cbCmd;
3698 cbRemain -= cbCmd;
3699
3700 /* If this is not the last command in the buffer, then generate IRQ, if required.
3701 * This avoids a double call to vmsvgaR3CmdBufRaiseIRQ if FENCE is the last command
3702 * in the buffer (usually the case).
3703 */
3704 if (RT_LIKELY(!(cbRemain && *pu32IrqStatus)))
3705 { /* likely */ }
3706 else
3707 {
3708 vmsvgaR3CmdBufRaiseIRQ(pDevIns, pThis, *pu32IrqStatus);
3709 *pu32IrqStatus = 0;
3710 }
3711 }
3712
3713 Assert(cbRemain <= cbCommands);
3714 *poffNextCmd = cbCommands - cbRemain;
3715 return CBstatus;
3716}
3717
3718
3719/** Process command buffers.
3720 *
3721 * @param pDevIns The device instance.
3722 * @param pThis The shared VGA/VMSVGA state.
3723 * @param pThisCC The VGA/VMSVGA state for the current context.
3724 * @param pThread Handle of the FIFO thread.
3725 * @thread FIFO
3726 */
3727static void vmsvgaR3CmdBufProcessBuffers(PPDMDEVINS pDevIns, PVGASTATE pThis, PVGASTATECC pThisCC, PPDMTHREAD pThread)
3728{
3729 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
3730
3731 for (;;)
3732 {
3733 if (pThread->enmState != PDMTHREADSTATE_RUNNING)
3734 break;
3735
3736 /* See if there is a submitted buffer. */
3737 PVMSVGACMDBUF pCmdBuf = NULL;
3738
3739 int rc = RTCritSectEnter(&pSvgaR3State->CritSectCmdBuf);
3740 AssertRC(rc);
3741
3742 /* It seems that a higher queue index has a higher priority.
3743 * See SVGACBContext in svga_reg.h from latest vmwgfx Linux driver.
3744 */
3745 for (unsigned i = RT_ELEMENTS(pSvgaR3State->apCmdBufCtxs); i > 0; --i)
3746 {
3747 PVMSVGACMDBUFCTX pCmdBufCtx = pSvgaR3State->apCmdBufCtxs[i - 1];
3748 if (pCmdBufCtx)
3749 {
3750 pCmdBuf = RTListRemoveFirst(&pCmdBufCtx->listSubmitted, VMSVGACMDBUF, nodeBuffer);
3751 if (pCmdBuf)
3752 {
3753 Assert(pCmdBufCtx->cSubmitted > 0);
3754 --pCmdBufCtx->cSubmitted;
3755 break;
3756 }
3757 }
3758 }
3759
3760 if (!pCmdBuf)
3761 {
3762 ASMAtomicWriteU32(&pSvgaR3State->fCmdBuf, 0);
3763 RTCritSectLeave(&pSvgaR3State->CritSectCmdBuf);
3764 break;
3765 }
3766
3767 RTCritSectLeave(&pSvgaR3State->CritSectCmdBuf);
3768
3769 SVGACBStatus CBstatus = SVGA_CB_STATUS_NONE;
3770 uint32_t offNextCmd = 0;
3771 uint32_t u32IrqStatus = 0;
3772
3773 /* Process one buffer. */
3774 CBstatus = vmsvgaR3CmdBufProcessCommands(pDevIns, pThis, pThisCC, pCmdBuf->pvCommands, pCmdBuf->hdr.length, &offNextCmd, &u32IrqStatus);
3775
3776 if (!RT_BOOL(pCmdBuf->hdr.flags & SVGA_CB_FLAG_NO_IRQ))
3777 u32IrqStatus |= SVGA_IRQFLAG_COMMAND_BUFFER;
3778 if (CBstatus == SVGA_CB_STATUS_COMMAND_ERROR)
3779 u32IrqStatus |= SVGA_IRQFLAG_ERROR;
3780
3781 vmsvgaR3CmdBufWriteStatus(pDevIns, pCmdBuf->GCPhysCB, CBstatus, offNextCmd);
3782 if (u32IrqStatus)
3783 vmsvgaR3CmdBufRaiseIRQ(pDevIns, pThis, u32IrqStatus);
3784
3785 vmsvgaR3CmdBufFree(pCmdBuf);
3786 }
3787}
3788
3789
3790/**
3791 * Worker for vmsvgaR3FifoThread that handles an external command.
3792 *
3793 * @param pDevIns The device instance.
3794 * @param pThis The shared VGA/VMSVGA instance data.
3795 * @param pThisCC The VGA/VMSVGA state for ring-3.
3796 */
3797static void vmsvgaR3FifoHandleExtCmd(PPDMDEVINS pDevIns, PVGASTATE pThis, PVGASTATECC pThisCC)
3798{
3799 uint8_t uExtCmd = pThis->svga.u8FIFOExtCommand;
3800 switch (pThis->svga.u8FIFOExtCommand)
3801 {
3802 case VMSVGA_FIFO_EXTCMD_RESET:
3803 Log(("vmsvgaR3FifoLoop: reset the fifo thread.\n"));
3804 Assert(pThisCC->svga.pvFIFOExtCmdParam == NULL);
3805
3806 vmsvgaR3ResetScreens(pThis, pThisCC);
3807# ifdef VBOX_WITH_VMSVGA3D
3808 if (pThis->svga.f3DEnabled)
3809 {
3810 /* The 3d subsystem must be reset from the fifo thread. */
3811 vmsvga3dReset(pThisCC);
3812 }
3813# endif
3814 break;
3815
3816 case VMSVGA_FIFO_EXTCMD_POWEROFF:
3817 Log(("vmsvgaR3FifoLoop: power off.\n"));
3818 Assert(pThisCC->svga.pvFIFOExtCmdParam == NULL);
3819
3820 /* The screens must be reset on the FIFO thread, because they may use 3D resources. */
3821 vmsvgaR3ResetScreens(pThis, pThisCC);
3822 break;
3823
3824 case VMSVGA_FIFO_EXTCMD_TERMINATE:
3825 Log(("vmsvgaR3FifoLoop: terminate the fifo thread.\n"));
3826 Assert(pThisCC->svga.pvFIFOExtCmdParam == NULL);
3827# ifdef VBOX_WITH_VMSVGA3D
3828 if (pThis->svga.f3DEnabled)
3829 {
3830 /* The 3d subsystem must be shut down from the fifo thread. */
3831 vmsvga3dTerminate(pThisCC);
3832 }
3833# endif
3834 break;
3835
3836 case VMSVGA_FIFO_EXTCMD_SAVESTATE:
3837 {
3838 Log(("vmsvgaR3FifoLoop: VMSVGA_FIFO_EXTCMD_SAVESTATE.\n"));
3839 PSSMHANDLE pSSM = (PSSMHANDLE)pThisCC->svga.pvFIFOExtCmdParam;
3840 AssertLogRelMsgBreak(RT_VALID_PTR(pSSM), ("pSSM=%p\n", pSSM));
3841 vmsvgaR3SaveExecFifo(pDevIns->pHlpR3, pThisCC, pSSM);
3842# ifdef VBOX_WITH_VMSVGA3D
3843 if (pThis->svga.f3DEnabled)
3844 vmsvga3dSaveExec(pDevIns, pThisCC, pSSM);
3845# endif
3846 break;
3847 }
3848
3849 case VMSVGA_FIFO_EXTCMD_LOADSTATE:
3850 {
3851 Log(("vmsvgaR3FifoLoop: VMSVGA_FIFO_EXTCMD_LOADSTATE.\n"));
3852 PVMSVGA_STATE_LOAD pLoadState = (PVMSVGA_STATE_LOAD)pThisCC->svga.pvFIFOExtCmdParam;
3853 AssertLogRelMsgBreak(RT_VALID_PTR(pLoadState), ("pLoadState=%p\n", pLoadState));
3854 vmsvgaR3LoadExecFifo(pDevIns->pHlpR3, pThis, pThisCC, pLoadState->pSSM, pLoadState->uVersion, pLoadState->uPass);
3855# ifdef VBOX_WITH_VMSVGA3D
3856 if (pThis->svga.f3DEnabled)
3857 vmsvga3dLoadExec(pDevIns, pThis, pThisCC, pLoadState->pSSM, pLoadState->uVersion, pLoadState->uPass);
3858# endif
3859 break;
3860 }
3861
3862 case VMSVGA_FIFO_EXTCMD_UPDATE_SURFACE_HEAP_BUFFERS:
3863 {
3864# ifdef VBOX_WITH_VMSVGA3D
3865 uint32_t sid = (uint32_t)(uintptr_t)pThisCC->svga.pvFIFOExtCmdParam;
3866 Log(("vmsvgaR3FifoLoop: VMSVGA_FIFO_EXTCMD_UPDATE_SURFACE_HEAP_BUFFERS sid=%#x\n", sid));
3867 vmsvga3dUpdateHeapBuffersForSurfaces(pThisCC, sid);
3868# endif
3869 break;
3870 }
3871
3872
3873 default:
3874 AssertLogRelMsgFailed(("uExtCmd=%#x pvFIFOExtCmdParam=%p\n", uExtCmd, pThisCC->svga.pvFIFOExtCmdParam));
3875 break;
3876 }
3877
3878 /*
3879 * Signal the end of the external command.
3880 */
3881 pThisCC->svga.pvFIFOExtCmdParam = NULL;
3882 pThis->svga.u8FIFOExtCommand = VMSVGA_FIFO_EXTCMD_NONE;
3883 ASMMemoryFence(); /* paranoia^2 */
3884 int rc = RTSemEventSignal(pThisCC->svga.hFIFOExtCmdSem);
3885 AssertLogRelRC(rc);
3886}
3887
3888/**
3889 * Worker for vmsvgaR3Destruct, vmsvgaR3Reset, vmsvgaR3Save and vmsvgaR3Load for
3890 * doing a job on the FIFO thread (even when it's officially suspended).
3891 *
3892 * @returns VBox status code (fully asserted).
3893 * @param pDevIns The device instance.
3894 * @param pThis The shared VGA/VMSVGA instance data.
3895 * @param pThisCC The VGA/VMSVGA state for ring-3.
3896 * @param uExtCmd The command to execute on the FIFO thread.
3897 * @param pvParam Pointer to command parameters.
3898 * @param cMsWait The time to wait for the command, given in
3899 * milliseconds.
3900 */
3901static int vmsvgaR3RunExtCmdOnFifoThread(PPDMDEVINS pDevIns, PVGASTATE pThis, PVGASTATECC pThisCC,
3902 uint8_t uExtCmd, void *pvParam, RTMSINTERVAL cMsWait)
3903{
3904 Assert(cMsWait >= RT_MS_1SEC * 5);
3905 AssertLogRelMsg(pThis->svga.u8FIFOExtCommand == VMSVGA_FIFO_EXTCMD_NONE,
3906 ("old=%d new=%d\n", pThis->svga.u8FIFOExtCommand, uExtCmd));
3907
3908 int rc;
3909 PPDMTHREAD pThread = pThisCC->svga.pFIFOIOThread;
3910 PDMTHREADSTATE enmState = pThread->enmState;
3911 if (enmState == PDMTHREADSTATE_SUSPENDED)
3912 {
3913 /*
3914 * The thread is suspended, we have to temporarily wake it up so it can
3915 * perform the task.
3916 * (We ASSUME not racing code here, both wrt thread state and ext commands.)
3917 */
3918 Log(("vmsvgaR3RunExtCmdOnFifoThread: uExtCmd=%d enmState=SUSPENDED\n", uExtCmd));
3919 /* Post the request. */
3920 pThis->svga.fFifoExtCommandWakeup = true;
3921 pThisCC->svga.pvFIFOExtCmdParam = pvParam;
3922 pThis->svga.u8FIFOExtCommand = uExtCmd;
3923 ASMMemoryFence(); /* paranoia^3 */
3924
3925 /* Resume the thread. */
3926 rc = PDMDevHlpThreadResume(pDevIns, pThread);
3927 AssertLogRelRC(rc);
3928 if (RT_SUCCESS(rc))
3929 {
3930 /* Wait. Take care in case the semaphore was already posted (same as below). */
3931 rc = RTSemEventWait(pThisCC->svga.hFIFOExtCmdSem, cMsWait);
3932 if ( rc == VINF_SUCCESS
3933 && pThis->svga.u8FIFOExtCommand == uExtCmd)
3934 rc = RTSemEventWait(pThisCC->svga.hFIFOExtCmdSem, cMsWait);
3935 AssertLogRelMsg(pThis->svga.u8FIFOExtCommand != uExtCmd || RT_FAILURE_NP(rc),
3936 ("%#x %Rrc\n", pThis->svga.u8FIFOExtCommand, rc));
3937
3938 /* suspend the thread */
3939 pThis->svga.fFifoExtCommandWakeup = false;
3940 int rc2 = PDMDevHlpThreadSuspend(pDevIns, pThread);
3941 AssertLogRelRC(rc2);
3942 if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
3943 rc = rc2;
3944 }
3945 pThis->svga.fFifoExtCommandWakeup = false;
3946 pThisCC->svga.pvFIFOExtCmdParam = NULL;
3947 }
3948 else if (enmState == PDMTHREADSTATE_RUNNING)
3949 {
3950 /*
3951 * The thread is running, should only happen during reset and vmsvga3dsfc.
3952 * We ASSUME not racing code here, both wrt thread state and ext commands.
3953 */
3954 Log(("vmsvgaR3RunExtCmdOnFifoThread: uExtCmd=%d enmState=RUNNING\n", uExtCmd));
3955 Assert(uExtCmd == VMSVGA_FIFO_EXTCMD_RESET || uExtCmd == VMSVGA_FIFO_EXTCMD_UPDATE_SURFACE_HEAP_BUFFERS || uExtCmd == VMSVGA_FIFO_EXTCMD_POWEROFF);
3956
3957 /* Post the request. */
3958 pThisCC->svga.pvFIFOExtCmdParam = pvParam;
3959 pThis->svga.u8FIFOExtCommand = uExtCmd;
3960 ASMMemoryFence(); /* paranoia^2 */
3961 rc = PDMDevHlpSUPSemEventSignal(pDevIns, pThis->svga.hFIFORequestSem);
3962 AssertLogRelRC(rc);
3963
3964 /* Wait. Take care in case the semaphore was already posted (same as above). */
3965 rc = RTSemEventWait(pThisCC->svga.hFIFOExtCmdSem, cMsWait);
3966 if ( rc == VINF_SUCCESS
3967 && pThis->svga.u8FIFOExtCommand == uExtCmd)
3968 rc = RTSemEventWait(pThisCC->svga.hFIFOExtCmdSem, cMsWait); /* it was already posted, retry the wait. */
3969 AssertLogRelMsg(pThis->svga.u8FIFOExtCommand != uExtCmd || RT_FAILURE_NP(rc),
3970 ("%#x %Rrc\n", pThis->svga.u8FIFOExtCommand, rc));
3971
3972 pThisCC->svga.pvFIFOExtCmdParam = NULL;
3973 }
3974 else
3975 {
3976 /*
3977 * Something is wrong with the thread!
3978 */
3979 AssertLogRelMsgFailed(("uExtCmd=%d enmState=%d\n", uExtCmd, enmState));
3980 rc = VERR_INVALID_STATE;
3981 }
3982 return rc;
3983}
3984
3985
3986/**
3987 * Marks the FIFO non-busy, notifying any waiting EMTs.
3988 *
3989 * @param pDevIns The device instance.
3990 * @param pThis The shared VGA/VMSVGA instance data.
3991 * @param pThisCC The VGA/VMSVGA state for ring-3.
3992 * @param pSVGAState Pointer to the ring-3 only SVGA state data.
3993 * @param offFifoMin The start byte offset of the command FIFO.
3994 */
3995static void vmsvgaR3FifoSetNotBusy(PPDMDEVINS pDevIns, PVGASTATE pThis, PVGASTATECC pThisCC, PVMSVGAR3STATE pSVGAState, uint32_t offFifoMin)
3996{
3997 ASMAtomicAndU32(&pThis->svga.fBusy, ~(VMSVGA_BUSY_F_FIFO | VMSVGA_BUSY_F_EMT_FORCE));
3998 if (VMSVGA_IS_VALID_FIFO_REG(SVGA_FIFO_BUSY, offFifoMin))
3999 vmsvgaHCSafeFifoBusyRegUpdate(pThis, pThisCC, pThis->svga.fBusy != 0);
4000
4001 /* Wake up any waiting EMTs. */
4002 if (pSVGAState->cBusyDelayedEmts > 0)
4003 {
4004# ifdef VMSVGA_USE_EMT_HALT_CODE
4005 PVM pVM = PDMDevHlpGetVM(pDevIns);
4006 VMCPUID idCpu = VMCpuSetFindLastPresentInternal(&pSVGAState->BusyDelayedEmts);
4007 if (idCpu != NIL_VMCPUID)
4008 {
4009 VMR3NotifyCpuDeviceReady(pVM, idCpu);
4010 while (idCpu-- > 0)
4011 if (VMCPUSET_IS_PRESENT(&pSVGAState->BusyDelayedEmts, idCpu))
4012 VMR3NotifyCpuDeviceReady(pVM, idCpu);
4013 }
4014# else
4015 int rc2 = RTSemEventMultiSignal(pSVGAState->hBusyDelayedEmts);
4016 AssertRC(rc2);
4017# endif
4018 }
4019}
4020
4021/**
4022 * Reads (more) payload into the command buffer.
4023 *
4024 * @returns pbBounceBuf on success
4025 * @retval (void *)1 if the thread was requested to stop.
4026 * @retval NULL on FIFO error.
4027 *
4028 * @param cbPayloadReq The number of bytes of payload requested.
4029 * @param pFIFO The FIFO.
4030 * @param offCurrentCmd The FIFO byte offset of the current command.
4031 * @param offFifoMin The start byte offset of the command FIFO.
4032 * @param offFifoMax The end byte offset of the command FIFO.
4033 * @param pbBounceBuf The bounch buffer. Same size as the entire FIFO, so
4034 * always sufficient size.
4035 * @param pcbAlreadyRead How much payload we've already read into the bounce
4036 * buffer. (We will NEVER re-read anything.)
4037 * @param pThread The calling PDM thread handle.
4038 * @param pThis The shared VGA/VMSVGA instance data.
4039 * @param pSVGAState Pointer to the ring-3 only SVGA state data. For
4040 * statistics collection.
4041 * @param pDevIns The device instance.
4042 */
4043static void *vmsvgaR3FifoGetCmdPayload(uint32_t cbPayloadReq, uint32_t RT_UNTRUSTED_VOLATILE_GUEST *pFIFO,
4044 uint32_t offCurrentCmd, uint32_t offFifoMin, uint32_t offFifoMax,
4045 uint8_t *pbBounceBuf, uint32_t *pcbAlreadyRead,
4046 PPDMTHREAD pThread, PVGASTATE pThis, PVMSVGAR3STATE pSVGAState, PPDMDEVINS pDevIns)
4047{
4048 Assert(pbBounceBuf);
4049 Assert(pcbAlreadyRead);
4050 Assert(offFifoMin < offFifoMax);
4051 Assert(offCurrentCmd >= offFifoMin && offCurrentCmd < offFifoMax);
4052 Assert(offFifoMax <= pThis->svga.cbFIFO);
4053
4054 /*
4055 * Check if the requested payload size has already been satisfied .
4056 * .
4057 * When called to read more, the caller is responsible for making sure the .
4058 * new command size (cbRequsted) never is smaller than what has already .
4059 * been read.
4060 */
4061 uint32_t cbAlreadyRead = *pcbAlreadyRead;
4062 if (cbPayloadReq <= cbAlreadyRead)
4063 {
4064 AssertLogRelReturn(cbPayloadReq == cbAlreadyRead, NULL);
4065 return pbBounceBuf;
4066 }
4067
4068 /*
4069 * Commands bigger than the fifo buffer are invalid.
4070 */
4071 uint32_t const cbFifoCmd = offFifoMax - offFifoMin;
4072 AssertMsgReturnStmt(cbPayloadReq <= cbFifoCmd, ("cbPayloadReq=%#x cbFifoCmd=%#x\n", cbPayloadReq, cbFifoCmd),
4073 STAM_REL_COUNTER_INC(&pSVGAState->StatFifoErrors),
4074 NULL);
4075
4076 /*
4077 * Move offCurrentCmd past the command dword.
4078 */
4079 offCurrentCmd += sizeof(uint32_t);
4080 if (offCurrentCmd >= offFifoMax)
4081 offCurrentCmd = offFifoMin;
4082
4083 /*
4084 * Do we have sufficient payload data available already?
4085 * The host should not read beyond [SVGA_FIFO_NEXT_CMD], therefore '>=' in the condition below.
4086 */
4087 uint32_t cbAfter, cbBefore;
4088 uint32_t offNextCmd = pFIFO[SVGA_FIFO_NEXT_CMD];
4089 RT_UNTRUSTED_NONVOLATILE_COPY_FENCE();
4090 if (offNextCmd >= offCurrentCmd)
4091 {
4092 if (RT_LIKELY(offNextCmd < offFifoMax))
4093 cbAfter = offNextCmd - offCurrentCmd;
4094 else
4095 {
4096 STAM_REL_COUNTER_INC(&pSVGAState->StatFifoErrors);
4097 LogRelMax(16, ("vmsvgaR3FifoGetCmdPayload: Invalid offNextCmd=%#x (offFifoMin=%#x offFifoMax=%#x)\n",
4098 offNextCmd, offFifoMin, offFifoMax));
4099 cbAfter = offFifoMax - offCurrentCmd;
4100 }
4101 cbBefore = 0;
4102 }
4103 else
4104 {
4105 cbAfter = offFifoMax - offCurrentCmd;
4106 if (offNextCmd >= offFifoMin)
4107 cbBefore = offNextCmd - offFifoMin;
4108 else
4109 {
4110 STAM_REL_COUNTER_INC(&pSVGAState->StatFifoErrors);
4111 LogRelMax(16, ("vmsvgaR3FifoGetCmdPayload: Invalid offNextCmd=%#x (offFifoMin=%#x offFifoMax=%#x)\n",
4112 offNextCmd, offFifoMin, offFifoMax));
4113 cbBefore = 0;
4114 }
4115 }
4116 if (cbAfter + cbBefore < cbPayloadReq)
4117 {
4118 /*
4119 * Insufficient, must wait for it to arrive.
4120 */
4121/** @todo Should clear the busy flag here to maybe encourage the guest to wake us up. */
4122 STAM_REL_PROFILE_START(&pSVGAState->StatFifoStalls, Stall);
4123 for (uint32_t i = 0;; i++)
4124 {
4125 if (pThread->enmState != PDMTHREADSTATE_RUNNING)
4126 {
4127 STAM_REL_PROFILE_STOP(&pSVGAState->StatFifoStalls, Stall);
4128 return (void *)(uintptr_t)1;
4129 }
4130 Log(("Guest still copying (%x vs %x) current %x next %x stop %x loop %u; sleep a bit\n",
4131 cbPayloadReq, cbAfter + cbBefore, offCurrentCmd, offNextCmd, pFIFO[SVGA_FIFO_STOP], i));
4132
4133 PDMDevHlpSUPSemEventWaitNoResume(pDevIns, pThis->svga.hFIFORequestSem, i < 16 ? 1 : 2);
4134
4135 offNextCmd = pFIFO[SVGA_FIFO_NEXT_CMD];
4136 RT_UNTRUSTED_NONVOLATILE_COPY_FENCE();
4137 if (offNextCmd >= offCurrentCmd)
4138 {
4139 cbAfter = RT_MIN(offNextCmd, offFifoMax) - offCurrentCmd;
4140 cbBefore = 0;
4141 }
4142 else
4143 {
4144 cbAfter = offFifoMax - offCurrentCmd;
4145 cbBefore = RT_MAX(offNextCmd, offFifoMin) - offFifoMin;
4146 }
4147
4148 if (cbAfter + cbBefore >= cbPayloadReq)
4149 break;
4150 }
4151 STAM_REL_PROFILE_STOP(&pSVGAState->StatFifoStalls, Stall);
4152 }
4153
4154 /*
4155 * Copy out the memory and update what pcbAlreadyRead points to.
4156 */
4157 if (cbAfter >= cbPayloadReq)
4158 memcpy(pbBounceBuf + cbAlreadyRead,
4159 (uint8_t *)pFIFO + offCurrentCmd + cbAlreadyRead,
4160 cbPayloadReq - cbAlreadyRead);
4161 else
4162 {
4163 LogFlow(("Split data buffer at %x (%u-%u)\n", offCurrentCmd, cbAfter, cbBefore));
4164 if (cbAlreadyRead < cbAfter)
4165 {
4166 memcpy(pbBounceBuf + cbAlreadyRead,
4167 (uint8_t *)pFIFO + offCurrentCmd + cbAlreadyRead,
4168 cbAfter - cbAlreadyRead);
4169 cbAlreadyRead = cbAfter;
4170 }
4171 memcpy(pbBounceBuf + cbAlreadyRead,
4172 (uint8_t *)pFIFO + offFifoMin + cbAlreadyRead - cbAfter,
4173 cbPayloadReq - cbAlreadyRead);
4174 }
4175 *pcbAlreadyRead = cbPayloadReq;
4176 RT_UNTRUSTED_NONVOLATILE_COPY_FENCE();
4177 return pbBounceBuf;
4178}
4179
4180
4181/**
4182 * Sends cursor position and visibility information from the FIFO to the front-end.
4183 * @returns SVGA_FIFO_CURSOR_COUNT value used.
4184 */
4185static uint32_t
4186vmsvgaR3FifoUpdateCursor(PVGASTATECC pThisCC, PVMSVGAR3STATE pSVGAState, uint32_t RT_UNTRUSTED_VOLATILE_GUEST *pFIFO,
4187 uint32_t offFifoMin, uint32_t uCursorUpdateCount,
4188 uint32_t *pxLast, uint32_t *pyLast, uint32_t *pfLastVisible)
4189{
4190 /*
4191 * Check if the cursor update counter has changed and try get a stable
4192 * set of values if it has. This is race-prone, especially consindering
4193 * the screen ID, but little we can do about that.
4194 */
4195 uint32_t x, y, fVisible, idScreen;
4196 for (uint32_t i = 0; ; i++)
4197 {
4198 x = pFIFO[SVGA_FIFO_CURSOR_X];
4199 y = pFIFO[SVGA_FIFO_CURSOR_Y];
4200 fVisible = pFIFO[SVGA_FIFO_CURSOR_ON];
4201 idScreen = VMSVGA_IS_VALID_FIFO_REG(SVGA_FIFO_CURSOR_SCREEN_ID, offFifoMin)
4202 ? pFIFO[SVGA_FIFO_CURSOR_SCREEN_ID] : SVGA_ID_INVALID;
4203 if ( uCursorUpdateCount == pFIFO[SVGA_FIFO_CURSOR_COUNT]
4204 || i > 3)
4205 break;
4206 if (i == 0)
4207 STAM_REL_COUNTER_INC(&pSVGAState->StatFifoCursorFetchAgain);
4208 ASMNopPause();
4209 uCursorUpdateCount = pFIFO[SVGA_FIFO_CURSOR_COUNT];
4210 }
4211
4212 /*
4213 * Check if anything has changed, as calling into pDrv is not light-weight.
4214 */
4215 if ( *pxLast == x
4216 && *pyLast == y
4217 && (idScreen != SVGA_ID_INVALID || *pfLastVisible == fVisible))
4218 STAM_REL_COUNTER_INC(&pSVGAState->StatFifoCursorNoChange);
4219 else
4220 {
4221 /*
4222 * Detected changes.
4223 *
4224 * We handle global, not per-screen visibility information by sending
4225 * pfnVBVAMousePointerShape without shape data.
4226 */
4227 *pxLast = x;
4228 *pyLast = y;
4229 uint32_t fFlags = VBVA_CURSOR_VALID_DATA;
4230 if (idScreen != SVGA_ID_INVALID)
4231 fFlags |= VBVA_CURSOR_SCREEN_RELATIVE;
4232 else if (*pfLastVisible != fVisible)
4233 {
4234 LogRel2(("vmsvgaR3FifoUpdateCursor: fVisible %d fLastVisible %d (%d,%d)\n", fVisible, *pfLastVisible, x, y));
4235 *pfLastVisible = fVisible;
4236 pThisCC->pDrv->pfnVBVAMousePointerShape(pThisCC->pDrv, RT_BOOL(fVisible), false, 0, 0, 0, 0, NULL);
4237 STAM_REL_COUNTER_INC(&pSVGAState->StatFifoCursorVisiblity);
4238 }
4239 pThisCC->pDrv->pfnVBVAReportCursorPosition(pThisCC->pDrv, fFlags, idScreen, x, y);
4240 STAM_REL_COUNTER_INC(&pSVGAState->StatFifoCursorPosition);
4241 }
4242
4243 /*
4244 * Update done. Signal this to the guest.
4245 */
4246 pFIFO[SVGA_FIFO_CURSOR_LAST_UPDATED] = uCursorUpdateCount;
4247
4248 return uCursorUpdateCount;
4249}
4250
4251
4252/**
4253 * Checks if there is work to be done, either cursor updating or FIFO commands.
4254 *
4255 * @returns true if pending work, false if not.
4256 * @param pThisCC The VGA/VMSVGA state for ring-3.
4257 * @param uLastCursorCount The last cursor update counter value.
4258 */
4259DECLINLINE(bool) vmsvgaR3FifoHasWork(PVGASTATECC pThisCC, uint32_t uLastCursorCount)
4260{
4261 /* If FIFO does not exist than there is nothing to do. Command buffers also require the enabled FIFO. */
4262 uint32_t RT_UNTRUSTED_VOLATILE_GUEST * const pFIFO = pThisCC->svga.pau32FIFO;
4263 AssertReturn(pFIFO, false);
4264
4265 if (vmsvgaR3CmdBufHasWork(pThisCC))
4266 return true;
4267
4268 if (pFIFO[SVGA_FIFO_NEXT_CMD] != pFIFO[SVGA_FIFO_STOP])
4269 return true;
4270
4271 if ( uLastCursorCount != pFIFO[SVGA_FIFO_CURSOR_COUNT]
4272 && VMSVGA_IS_VALID_FIFO_REG(SVGA_FIFO_CURSOR_LAST_UPDATED, pFIFO[SVGA_FIFO_MIN]))
4273 return true;
4274
4275 return false;
4276}
4277
4278
4279/**
4280 * Called by the VGA refresh timer to wake up the FIFO thread when needed.
4281 *
4282 * @param pDevIns The device instance.
4283 * @param pThis The shared VGA/VMSVGA instance data.
4284 * @param pThisCC The VGA/VMSVGA state for ring-3.
4285 */
4286void vmsvgaR3FifoWatchdogTimer(PPDMDEVINS pDevIns, PVGASTATE pThis, PVGASTATECC pThisCC)
4287{
4288 /* Caller already checked pThis->svga.fFIFOThreadSleeping, so we only have
4289 to recheck it before doing the signalling. */
4290 if ( vmsvgaR3FifoHasWork(pThisCC, ASMAtomicReadU32(&pThis->svga.uLastCursorUpdateCount))
4291 && pThis->svga.fFIFOThreadSleeping
4292 && !ASMAtomicReadBool(&pThis->svga.fBadGuest))
4293 {
4294 int rc = PDMDevHlpSUPSemEventSignal(pDevIns, pThis->svga.hFIFORequestSem);
4295 AssertRC(rc);
4296 STAM_REL_COUNTER_INC(&pThisCC->svga.pSvgaR3State->StatFifoWatchdogWakeUps);
4297 }
4298}
4299
4300
4301/**
4302 * Called by the FIFO thread to process pending actions.
4303 *
4304 * @param pDevIns The device instance.
4305 * @param pThis The shared VGA/VMSVGA instance data.
4306 * @param pThisCC The VGA/VMSVGA state for ring-3.
4307 */
4308void vmsvgaR3FifoPendingActions(PPDMDEVINS pDevIns, PVGASTATE pThis, PVGASTATECC pThisCC)
4309{
4310 RT_NOREF(pDevIns);
4311
4312 /* Currently just mode changes. */
4313 if (ASMBitTestAndClear(&pThis->svga.u32ActionFlags, VMSVGA_ACTION_CHANGEMODE_BIT))
4314 {
4315 vmsvgaR3ChangeMode(pThis, pThisCC);
4316# ifdef VBOX_WITH_VMSVGA3D
4317 if (pThisCC->svga.p3dState != NULL)
4318 vmsvga3dChangeMode(pThisCC);
4319# endif
4320 }
4321}
4322
4323
4324/*
4325 * These two macros are put outside vmsvgaR3FifoLoop because doxygen gets confused,
4326 * even the latest version, and thinks we're documenting vmsvgaR3FifoLoop. Sigh.
4327 */
4328/** @def VMSVGAFIFO_GET_CMD_BUFFER_BREAK
4329 * Macro for shortening calls to vmsvgaR3FifoGetCmdPayload.
4330 *
4331 * Will break out of the switch on failure.
4332 * Will restart and quit the loop if the thread was requested to stop.
4333 *
4334 * @param a_PtrVar Request variable pointer.
4335 * @param a_Type Request typedef (not pointer) for casting.
4336 * @param a_cbPayloadReq How much payload to fetch.
4337 * @remarks Accesses a bunch of variables in the current scope!
4338 */
4339# define VMSVGAFIFO_GET_CMD_BUFFER_BREAK(a_PtrVar, a_Type, a_cbPayloadReq) \
4340 if (1) { \
4341 (a_PtrVar) = (a_Type *)vmsvgaR3FifoGetCmdPayload((a_cbPayloadReq), pFIFO, offCurrentCmd, offFifoMin, offFifoMax, \
4342 pbBounceBuf, &cbPayload, pThread, pThis, pSVGAState, pDevIns); \
4343 if (RT_UNLIKELY((uintptr_t)(a_PtrVar) < 2)) { if ((uintptr_t)(a_PtrVar) == 1) continue; break; } \
4344 RT_UNTRUSTED_NONVOLATILE_COPY_FENCE(); \
4345 } else do {} while (0)
4346/* @def VMSVGAFIFO_GET_MORE_CMD_BUFFER_BREAK
4347 * Macro for shortening calls to vmsvgaR3FifoGetCmdPayload for refetching the
4348 * buffer after figuring out the actual command size.
4349 *
4350 * Will break out of the switch on failure.
4351 *
4352 * @param a_PtrVar Request variable pointer.
4353 * @param a_Type Request typedef (not pointer) for casting.
4354 * @param a_cbPayloadReq How much payload to fetch.
4355 * @remarks Accesses a bunch of variables in the current scope!
4356 */
4357# define VMSVGAFIFO_GET_MORE_CMD_BUFFER_BREAK(a_PtrVar, a_Type, a_cbPayloadReq) \
4358 if (1) { \
4359 VMSVGAFIFO_GET_CMD_BUFFER_BREAK(a_PtrVar, a_Type, a_cbPayloadReq); \
4360 } else do {} while (0)
4361
4362/**
4363 * @callback_method_impl{PFNPDMTHREADDEV, The async FIFO handling thread.}
4364 */
4365static DECLCALLBACK(int) vmsvgaR3FifoLoop(PPDMDEVINS pDevIns, PPDMTHREAD pThread)
4366{
4367 PVGASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVGASTATE);
4368 PVGASTATER3 pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVGASTATECC);
4369 PVMSVGAR3STATE pSVGAState = pThisCC->svga.pSvgaR3State;
4370 int rc;
4371
4372# if defined(VBOX_WITH_VMSVGA3D) && defined(RT_OS_LINUX)
4373 if (pThis->svga.f3DEnabled)
4374 {
4375 /* The FIFO thread may use X API for accelerated screen output. */
4376 XInitThreads();
4377 }
4378# endif
4379
4380 if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
4381 return VINF_SUCCESS;
4382
4383 /*
4384 * Special mode where we only execute an external command and the go back
4385 * to being suspended. Currently, all ext cmds ends up here, with the reset
4386 * one also being eligble for runtime execution further down as well.
4387 */
4388 if (pThis->svga.fFifoExtCommandWakeup)
4389 {
4390 vmsvgaR3FifoHandleExtCmd(pDevIns, pThis, pThisCC);
4391 while (pThread->enmState == PDMTHREADSTATE_RUNNING)
4392 if (pThis->svga.u8FIFOExtCommand == VMSVGA_FIFO_EXTCMD_NONE)
4393 PDMDevHlpSUPSemEventWaitNoResume(pDevIns, pThis->svga.hFIFORequestSem, RT_MS_1MIN);
4394 else
4395 vmsvgaR3FifoHandleExtCmd(pDevIns, pThis, pThisCC);
4396 return VINF_SUCCESS;
4397 }
4398
4399
4400 /*
4401 * Signal the semaphore to make sure we don't wait for 250ms after a
4402 * suspend & resume scenario (see vmsvgaR3FifoGetCmdPayload).
4403 */
4404 PDMDevHlpSUPSemEventSignal(pDevIns, pThis->svga.hFIFORequestSem);
4405
4406 /*
4407 * Allocate a bounce buffer for command we get from the FIFO.
4408 * (All code must return via the end of the function to free this buffer.)
4409 */
4410 uint8_t *pbBounceBuf = (uint8_t *)RTMemAllocZ(pThis->svga.cbFIFO);
4411 AssertReturn(pbBounceBuf, VERR_NO_MEMORY);
4412
4413 /*
4414 * Polling/sleep interval config.
4415 *
4416 * We wait for an a short interval if the guest has recently given us work
4417 * to do, but the interval increases the longer we're kept idle. Once we've
4418 * reached the refresh timer interval, we'll switch to extended waits,
4419 * depending on it or the guest to kick us into action when needed.
4420 *
4421 * Should the refresh time go fishing, we'll just continue increasing the
4422 * sleep length till we reaches the 250 ms max after about 16 seconds.
4423 */
4424 RTMSINTERVAL const cMsMinSleep = 16;
4425 RTMSINTERVAL const cMsIncSleep = 2;
4426 RTMSINTERVAL const cMsMaxSleep = 250;
4427 RTMSINTERVAL const cMsExtendedSleep = 15 * RT_MS_1SEC; /* Regular paranoia dictates that this cannot be indefinite. */
4428 RTMSINTERVAL cMsSleep = cMsMaxSleep;
4429
4430 /*
4431 * Cursor update state (SVGA_FIFO_CAP_CURSOR_BYPASS_3).
4432 *
4433 * Initialize with values that will detect an update from the guest.
4434 * Make sure that if the guest never updates the cursor position, then the device does not report it.
4435 * The guest has to change the value of uLastCursorUpdateCount, when the cursor position is actually updated.
4436 * xLastCursor, yLastCursor and fLastCursorVisible are set to report the first update.
4437 */
4438 uint32_t RT_UNTRUSTED_VOLATILE_GUEST * const pFIFO = pThisCC->svga.pau32FIFO;
4439 pThis->svga.uLastCursorUpdateCount = pFIFO[SVGA_FIFO_CURSOR_COUNT];
4440 uint32_t xLastCursor = ~pFIFO[SVGA_FIFO_CURSOR_X];
4441 uint32_t yLastCursor = ~pFIFO[SVGA_FIFO_CURSOR_Y];
4442 uint32_t fLastCursorVisible = ~pFIFO[SVGA_FIFO_CURSOR_ON];
4443
4444 /*
4445 * The FIFO loop.
4446 */
4447 LogFlow(("vmsvgaR3FifoLoop: started loop\n"));
4448 bool fBadOrDisabledFifo = ASMAtomicReadBool(&pThis->svga.fBadGuest);
4449 while (pThread->enmState == PDMTHREADSTATE_RUNNING)
4450 {
4451# if defined(RT_OS_DARWIN) && defined(VBOX_WITH_VMSVGA3D)
4452 /*
4453 * Should service the run loop every so often.
4454 */
4455 if (pThis->svga.f3DEnabled)
4456 vmsvga3dCocoaServiceRunLoop();
4457# endif
4458
4459 /* First check any pending actions. */
4460 vmsvgaR3FifoPendingActions(pDevIns, pThis, pThisCC);
4461
4462 /*
4463 * Unless there's already work pending, go to sleep for a short while.
4464 * (See polling/sleep interval config above.)
4465 */
4466 if ( fBadOrDisabledFifo
4467 || !vmsvgaR3FifoHasWork(pThisCC, pThis->svga.uLastCursorUpdateCount))
4468 {
4469 ASMAtomicWriteBool(&pThis->svga.fFIFOThreadSleeping, true);
4470 Assert(pThis->cMilliesRefreshInterval > 0);
4471 if (cMsSleep < pThis->cMilliesRefreshInterval)
4472 rc = PDMDevHlpSUPSemEventWaitNoResume(pDevIns, pThis->svga.hFIFORequestSem, cMsSleep);
4473 else
4474 {
4475# ifdef VMSVGA_USE_FIFO_ACCESS_HANDLER
4476 int rc2 = PGMHandlerPhysicalReset(PDMDevHlpGetVM(pDevIns), pThis->svga.GCPhysFIFO);
4477 AssertRC(rc2); /* No break. Racing EMTs unmapping and remapping the region. */
4478# endif
4479 if ( !fBadOrDisabledFifo
4480 && vmsvgaR3FifoHasWork(pThisCC, pThis->svga.uLastCursorUpdateCount))
4481 rc = VINF_SUCCESS;
4482 else
4483 {
4484 STAM_REL_PROFILE_START(&pSVGAState->StatFifoExtendedSleep, Acc);
4485 rc = PDMDevHlpSUPSemEventWaitNoResume(pDevIns, pThis->svga.hFIFORequestSem, cMsExtendedSleep);
4486 STAM_REL_PROFILE_STOP(&pSVGAState->StatFifoExtendedSleep, Acc);
4487 }
4488 }
4489 ASMAtomicWriteBool(&pThis->svga.fFIFOThreadSleeping, false);
4490 AssertBreak(RT_SUCCESS(rc) || rc == VERR_TIMEOUT || rc == VERR_INTERRUPTED);
4491 if (pThread->enmState != PDMTHREADSTATE_RUNNING)
4492 {
4493 LogFlow(("vmsvgaR3FifoLoop: thread state %x\n", pThread->enmState));
4494 break;
4495 }
4496 }
4497 else
4498 rc = VINF_SUCCESS;
4499 fBadOrDisabledFifo = ASMAtomicReadBool(&pThis->svga.fBadGuest);
4500 if (rc == VERR_TIMEOUT)
4501 {
4502 if (!vmsvgaR3FifoHasWork(pThisCC, pThis->svga.uLastCursorUpdateCount))
4503 {
4504 cMsSleep = RT_MIN(cMsSleep + cMsIncSleep, cMsMaxSleep);
4505 continue;
4506 }
4507 STAM_REL_COUNTER_INC(&pSVGAState->StatFifoTodoTimeout);
4508
4509 Log(("vmsvgaR3FifoLoop: timeout\n"));
4510 }
4511 else if (vmsvgaR3FifoHasWork(pThisCC, pThis->svga.uLastCursorUpdateCount))
4512 STAM_REL_COUNTER_INC(&pSVGAState->StatFifoTodoWoken);
4513 cMsSleep = cMsMinSleep;
4514
4515 Log(("vmsvgaR3FifoLoop: enabled=%d configured=%d busy=%d\n", pThis->svga.fEnabled, pThis->svga.fConfigured, pFIFO[SVGA_FIFO_BUSY]));
4516 Log(("vmsvgaR3FifoLoop: min %x max %x\n", pFIFO[SVGA_FIFO_MIN], pFIFO[SVGA_FIFO_MAX]));
4517 Log(("vmsvgaR3FifoLoop: next %x stop %x\n", pFIFO[SVGA_FIFO_NEXT_CMD], pFIFO[SVGA_FIFO_STOP]));
4518
4519 /*
4520 * Handle external commands (currently only reset).
4521 */
4522 if (pThis->svga.u8FIFOExtCommand != VMSVGA_FIFO_EXTCMD_NONE)
4523 {
4524 vmsvgaR3FifoHandleExtCmd(pDevIns, pThis, pThisCC);
4525 continue;
4526 }
4527
4528 /*
4529 * If guest misbehaves, then do nothing.
4530 */
4531 if (ASMAtomicReadBool(&pThis->svga.fBadGuest))
4532 {
4533 vmsvgaR3FifoSetNotBusy(pDevIns, pThis, pThisCC, pSVGAState, pFIFO[SVGA_FIFO_MIN]);
4534 cMsSleep = cMsExtendedSleep;
4535 LogRelMax(1, ("VMSVGA: FIFO processing stopped because of the guest misbehavior\n"));
4536 continue;
4537 }
4538
4539 /*
4540 * The device must be enabled and configured.
4541 */
4542 if ( !pThis->svga.fEnabled
4543 || !pThis->svga.fConfigured)
4544 {
4545 vmsvgaR3FifoSetNotBusy(pDevIns, pThis, pThisCC, pSVGAState, pFIFO[SVGA_FIFO_MIN]);
4546 fBadOrDisabledFifo = true;
4547 cMsSleep = cMsMaxSleep; /* cheat */
4548 continue;
4549 }
4550
4551 /*
4552 * Get and check the min/max values. We ASSUME that they will remain
4553 * unchanged while we process requests. A further ASSUMPTION is that
4554 * the guest won't mess with SVGA_FIFO_NEXT_CMD while we're busy, so
4555 * we don't read it back while in the loop.
4556 */
4557 uint32_t const offFifoMin = pFIFO[SVGA_FIFO_MIN];
4558 uint32_t const offFifoMax = pFIFO[SVGA_FIFO_MAX];
4559 uint32_t offCurrentCmd = pFIFO[SVGA_FIFO_STOP];
4560 RT_UNTRUSTED_NONVOLATILE_COPY_FENCE();
4561 if (RT_UNLIKELY( !VMSVGA_IS_VALID_FIFO_REG(SVGA_FIFO_STOP, offFifoMin)
4562 || offFifoMax <= offFifoMin
4563 || offFifoMax > pThis->svga.cbFIFO
4564 || (offFifoMax & 3) != 0
4565 || (offFifoMin & 3) != 0
4566 || offCurrentCmd < offFifoMin
4567 || offCurrentCmd > offFifoMax))
4568 {
4569 STAM_REL_COUNTER_INC(&pSVGAState->StatFifoErrors);
4570 LogRelMax(8, ("vmsvgaR3FifoLoop: Bad fifo: min=%#x stop=%#x max=%#x\n", offFifoMin, offCurrentCmd, offFifoMax));
4571 vmsvgaR3FifoSetNotBusy(pDevIns, pThis, pThisCC, pSVGAState, offFifoMin);
4572 fBadOrDisabledFifo = true;
4573 continue;
4574 }
4575 RT_UNTRUSTED_VALIDATED_FENCE();
4576 if (RT_UNLIKELY(offCurrentCmd & 3))
4577 {
4578 STAM_REL_COUNTER_INC(&pSVGAState->StatFifoErrors);
4579 LogRelMax(8, ("vmsvgaR3FifoLoop: Misaligned offCurrentCmd=%#x?\n", offCurrentCmd));
4580 offCurrentCmd &= ~UINT32_C(3);
4581 }
4582
4583 /*
4584 * Update the cursor position before we start on the FIFO commands.
4585 */
4586 /** @todo do we need to check whether the guest disabled the SVGA_FIFO_CAP_CURSOR_BYPASS_3 capability here? */
4587 if (VMSVGA_IS_VALID_FIFO_REG(SVGA_FIFO_CURSOR_LAST_UPDATED, offFifoMin))
4588 {
4589 uint32_t const uCursorUpdateCount = pFIFO[SVGA_FIFO_CURSOR_COUNT];
4590 if (uCursorUpdateCount == pThis->svga.uLastCursorUpdateCount)
4591 { /* halfways likely */ }
4592 else
4593 {
4594 uint32_t const uNewCount = vmsvgaR3FifoUpdateCursor(pThisCC, pSVGAState, pFIFO, offFifoMin, uCursorUpdateCount,
4595 &xLastCursor, &yLastCursor, &fLastCursorVisible);
4596 ASMAtomicWriteU32(&pThis->svga.uLastCursorUpdateCount, uNewCount);
4597 }
4598 }
4599
4600 /*
4601 * Mark the FIFO as busy.
4602 */
4603 ASMAtomicWriteU32(&pThis->svga.fBusy, VMSVGA_BUSY_F_FIFO); // Clears VMSVGA_BUSY_F_EMT_FORCE!
4604 if (VMSVGA_IS_VALID_FIFO_REG(SVGA_FIFO_BUSY, offFifoMin))
4605 ASMAtomicWriteU32(&pFIFO[SVGA_FIFO_BUSY], true);
4606
4607 /*
4608 * Process all submitted command buffers.
4609 */
4610 vmsvgaR3CmdBufProcessBuffers(pDevIns, pThis, pThisCC, pThread);
4611
4612 /*
4613 * Execute all queued FIFO commands.
4614 * Quit if pending external command or changes in the thread state.
4615 */
4616 bool fDone = false;
4617 while ( !(fDone = (pFIFO[SVGA_FIFO_NEXT_CMD] == offCurrentCmd))
4618 && pThread->enmState == PDMTHREADSTATE_RUNNING)
4619 {
4620 uint32_t cbPayload = 0;
4621 uint32_t u32IrqStatus = 0;
4622
4623 Assert(offCurrentCmd < offFifoMax && offCurrentCmd >= offFifoMin);
4624
4625 /* First check any pending actions. */
4626 vmsvgaR3FifoPendingActions(pDevIns, pThis, pThisCC);
4627
4628 /* Check for pending external commands (reset). */
4629 if (pThis->svga.u8FIFOExtCommand != VMSVGA_FIFO_EXTCMD_NONE)
4630 break;
4631
4632 /*
4633 * Process the command.
4634 */
4635 /* 'enmCmdId' is actually a SVGAFifoCmdId. It is treated as uint32_t in order to avoid a compiler
4636 * warning. Because we implement some obsolete and deprecated commands, which are not included in
4637 * the SVGAFifoCmdId enum in the VMSVGA headers anymore.
4638 */
4639 uint32_t const enmCmdId = pFIFO[offCurrentCmd / sizeof(uint32_t)];
4640 RT_UNTRUSTED_NONVOLATILE_COPY_FENCE();
4641 LogFlow(("vmsvgaR3FifoLoop: FIFO command (iCmd=0x%x) %s %d\n",
4642 offCurrentCmd / sizeof(uint32_t), vmsvgaR3FifoCmdToString(enmCmdId), enmCmdId));
4643 switch (enmCmdId)
4644 {
4645 case SVGA_CMD_INVALID_CMD:
4646 /* Nothing to do. */
4647 STAM_REL_COUNTER_INC(&pSVGAState->StatR3CmdInvalidCmd);
4648 break;
4649
4650 case SVGA_CMD_FENCE:
4651 {
4652 SVGAFifoCmdFence *pCmdFence;
4653 VMSVGAFIFO_GET_CMD_BUFFER_BREAK(pCmdFence, SVGAFifoCmdFence, sizeof(*pCmdFence));
4654 STAM_REL_COUNTER_INC(&pSVGAState->StatR3CmdFence);
4655 if (VMSVGA_IS_VALID_FIFO_REG(SVGA_FIFO_FENCE, offFifoMin))
4656 {
4657 Log(("vmsvgaR3FifoLoop: SVGA_CMD_FENCE %#x\n", pCmdFence->fence));
4658 pFIFO[SVGA_FIFO_FENCE] = pCmdFence->fence;
4659
4660 if (pThis->svga.u32IrqMask & SVGA_IRQFLAG_ANY_FENCE)
4661 {
4662 Log(("vmsvgaR3FifoLoop: any fence irq\n"));
4663 u32IrqStatus |= SVGA_IRQFLAG_ANY_FENCE;
4664 }
4665 else
4666 if ( VMSVGA_IS_VALID_FIFO_REG(SVGA_FIFO_FENCE_GOAL, offFifoMin)
4667 && (pThis->svga.u32IrqMask & SVGA_IRQFLAG_FENCE_GOAL)
4668 && pFIFO[SVGA_FIFO_FENCE_GOAL] == pCmdFence->fence)
4669 {
4670 Log(("vmsvgaR3FifoLoop: fence goal reached irq (fence=%#x)\n", pCmdFence->fence));
4671 u32IrqStatus |= SVGA_IRQFLAG_FENCE_GOAL;
4672 }
4673 }
4674 else
4675 Log(("SVGA_CMD_FENCE is bogus when offFifoMin is %#x!\n", offFifoMin));
4676 break;
4677 }
4678
4679 case SVGA_CMD_UPDATE:
4680 {
4681 SVGAFifoCmdUpdate *pCmd;
4682 VMSVGAFIFO_GET_CMD_BUFFER_BREAK(pCmd, SVGAFifoCmdUpdate, sizeof(*pCmd));
4683 vmsvgaR3CmdUpdate(pThis, pThisCC, pCmd);
4684 break;
4685 }
4686
4687 case SVGA_CMD_UPDATE_VERBOSE:
4688 {
4689 SVGAFifoCmdUpdateVerbose *pCmd;
4690 VMSVGAFIFO_GET_CMD_BUFFER_BREAK(pCmd, SVGAFifoCmdUpdateVerbose, sizeof(*pCmd));
4691 vmsvgaR3CmdUpdateVerbose(pThis, pThisCC, pCmd);
4692 break;
4693 }
4694
4695 case SVGA_CMD_DEFINE_CURSOR:
4696 {
4697 /* Followed by bitmap data. */
4698 SVGAFifoCmdDefineCursor *pCmd;
4699 VMSVGAFIFO_GET_CMD_BUFFER_BREAK(pCmd, SVGAFifoCmdDefineCursor, sizeof(*pCmd));
4700
4701 /* Figure out the size of the bitmap data. */
4702 ASSERT_GUEST_BREAK(pCmd->height < 2048 && pCmd->width < 2048);
4703 ASSERT_GUEST_BREAK(pCmd->andMaskDepth <= 32);
4704 ASSERT_GUEST_BREAK(pCmd->xorMaskDepth <= 32);
4705 RT_UNTRUSTED_VALIDATED_FENCE();
4706
4707 uint32_t const cbAndLine = RT_ALIGN_32(pCmd->width * (pCmd->andMaskDepth + (pCmd->andMaskDepth == 15)), 32) / 8;
4708 uint32_t const cbAndMask = cbAndLine * pCmd->height;
4709 uint32_t const cbXorLine = RT_ALIGN_32(pCmd->width * (pCmd->xorMaskDepth + (pCmd->xorMaskDepth == 15)), 32) / 8;
4710 uint32_t const cbXorMask = cbXorLine * pCmd->height;
4711
4712 uint32_t const cbCmd = sizeof(SVGAFifoCmdDefineCursor) + cbAndMask + cbXorMask;
4713 VMSVGAFIFO_GET_MORE_CMD_BUFFER_BREAK(pCmd, SVGAFifoCmdDefineCursor, cbCmd);
4714 vmsvgaR3CmdDefineCursor(pThis, pThisCC, pCmd);
4715 break;
4716 }
4717
4718 case SVGA_CMD_DEFINE_ALPHA_CURSOR:
4719 {
4720 /* Followed by bitmap data. */
4721 SVGAFifoCmdDefineAlphaCursor *pCmd;
4722 VMSVGAFIFO_GET_CMD_BUFFER_BREAK(pCmd, SVGAFifoCmdDefineAlphaCursor, sizeof(*pCmd));
4723
4724 /* Figure out the size of the bitmap data. */
4725 ASSERT_GUEST_BREAK(pCmd->height < 2048 && pCmd->width < 2048);
4726
4727 uint32_t const cbCmd = sizeof(SVGAFifoCmdDefineAlphaCursor) + pCmd->width * pCmd->height * sizeof(uint32_t) /* 32-bit BRGA format */;
4728 VMSVGAFIFO_GET_MORE_CMD_BUFFER_BREAK(pCmd, SVGAFifoCmdDefineAlphaCursor, cbCmd);
4729 vmsvgaR3CmdDefineAlphaCursor(pThis, pThisCC, pCmd);
4730 break;
4731 }
4732
4733 case SVGA_CMD_MOVE_CURSOR:
4734 {
4735 /* Deprecated; there should be no driver which *requires* this command. However, if
4736 * we do ecncounter this command, it might be useful to not get the FIFO completely out of
4737 * alignment.
4738 * May be issued by guest if SVGA_CAP_CURSOR_BYPASS is missing.
4739 */
4740 SVGAFifoCmdMoveCursor *pCmd;
4741 VMSVGAFIFO_GET_CMD_BUFFER_BREAK(pCmd, SVGAFifoCmdMoveCursor, sizeof(*pCmd));
4742 vmsvgaR3CmdMoveCursor(pThis, pThisCC, pCmd);
4743 break;
4744 }
4745
4746 case SVGA_CMD_DISPLAY_CURSOR:
4747 {
4748 /* Deprecated; there should be no driver which *requires* this command. However, if
4749 * we do ecncounter this command, it might be useful to not get the FIFO completely out of
4750 * alignment.
4751 * May be issued by guest if SVGA_CAP_CURSOR_BYPASS is missing.
4752 */
4753 SVGAFifoCmdDisplayCursor *pCmd;
4754 VMSVGAFIFO_GET_CMD_BUFFER_BREAK(pCmd, SVGAFifoCmdDisplayCursor, sizeof(*pCmd));
4755 vmsvgaR3CmdDisplayCursor(pThis, pThisCC, pCmd);
4756 break;
4757 }
4758
4759 case SVGA_CMD_RECT_FILL:
4760 {
4761 SVGAFifoCmdRectFill *pCmd;
4762 VMSVGAFIFO_GET_CMD_BUFFER_BREAK(pCmd, SVGAFifoCmdRectFill, sizeof(*pCmd));
4763 vmsvgaR3CmdRectFill(pThis, pThisCC, pCmd);
4764 break;
4765 }
4766
4767 case SVGA_CMD_RECT_COPY:
4768 {
4769 SVGAFifoCmdRectCopy *pCmd;
4770 VMSVGAFIFO_GET_CMD_BUFFER_BREAK(pCmd, SVGAFifoCmdRectCopy, sizeof(*pCmd));
4771 vmsvgaR3CmdRectCopy(pThis, pThisCC, pCmd);
4772 break;
4773 }
4774
4775 case SVGA_CMD_RECT_ROP_COPY:
4776 {
4777 SVGAFifoCmdRectRopCopy *pCmd;
4778 VMSVGAFIFO_GET_CMD_BUFFER_BREAK(pCmd, SVGAFifoCmdRectRopCopy, sizeof(*pCmd));
4779 vmsvgaR3CmdRectRopCopy(pThis, pThisCC, pCmd);
4780 break;
4781 }
4782
4783 case SVGA_CMD_ESCAPE:
4784 {
4785 /* Followed by 'size' bytes of data. */
4786 SVGAFifoCmdEscape *pCmd;
4787 VMSVGAFIFO_GET_CMD_BUFFER_BREAK(pCmd, SVGAFifoCmdEscape, sizeof(*pCmd));
4788
4789 ASSERT_GUEST_BREAK(pCmd->size < pThis->svga.cbFIFO - sizeof(SVGAFifoCmdEscape));
4790 RT_UNTRUSTED_VALIDATED_FENCE();
4791
4792 uint32_t const cbCmd = sizeof(SVGAFifoCmdEscape) + pCmd->size;
4793 VMSVGAFIFO_GET_MORE_CMD_BUFFER_BREAK(pCmd, SVGAFifoCmdEscape, cbCmd);
4794 vmsvgaR3CmdEscape(pThis, pThisCC, pCmd);
4795 break;
4796 }
4797# ifdef VBOX_WITH_VMSVGA3D
4798 case SVGA_CMD_DEFINE_GMR2:
4799 {
4800 SVGAFifoCmdDefineGMR2 *pCmd;
4801 VMSVGAFIFO_GET_CMD_BUFFER_BREAK(pCmd, SVGAFifoCmdDefineGMR2, sizeof(*pCmd));
4802 vmsvgaR3CmdDefineGMR2(pThis, pThisCC, pCmd);
4803 break;
4804 }
4805
4806 case SVGA_CMD_REMAP_GMR2:
4807 {
4808 /* Followed by page descriptors or guest ptr. */
4809 SVGAFifoCmdRemapGMR2 *pCmd;
4810 VMSVGAFIFO_GET_CMD_BUFFER_BREAK(pCmd, SVGAFifoCmdRemapGMR2, sizeof(*pCmd));
4811
4812 /* Calculate the size of what comes after next and fetch it. */
4813 uint32_t cbCmd = sizeof(SVGAFifoCmdRemapGMR2);
4814 if (pCmd->flags & SVGA_REMAP_GMR2_VIA_GMR)
4815 cbCmd += sizeof(SVGAGuestPtr);
4816 else
4817 {
4818 uint32_t const cbPageDesc = (pCmd->flags & SVGA_REMAP_GMR2_PPN64) ? sizeof(uint64_t) : sizeof(uint32_t);
4819 if (pCmd->flags & SVGA_REMAP_GMR2_SINGLE_PPN)
4820 {
4821 cbCmd += cbPageDesc;
4822 pCmd->numPages = 1;
4823 }
4824 else
4825 {
4826 ASSERT_GUEST_BREAK(pCmd->numPages <= pThis->svga.cbFIFO / cbPageDesc);
4827 cbCmd += cbPageDesc * pCmd->numPages;
4828 }
4829 }
4830 VMSVGAFIFO_GET_MORE_CMD_BUFFER_BREAK(pCmd, SVGAFifoCmdRemapGMR2, cbCmd);
4831 vmsvgaR3CmdRemapGMR2(pThis, pThisCC, pCmd);
4832# ifdef DEBUG_GMR_ACCESS
4833 VMR3ReqCallWaitU(PDMDevHlpGetUVM(pDevIns), VMCPUID_ANY, (PFNRT)vmsvgaR3RegisterGmr, 2, pDevIns, pCmd->gmrId);
4834# endif
4835 break;
4836 }
4837# endif // VBOX_WITH_VMSVGA3D
4838 case SVGA_CMD_DEFINE_SCREEN:
4839 {
4840 /* The size of this command is specified by the guest and depends on capabilities. */
4841 Assert(pFIFO[SVGA_FIFO_CAPABILITIES] & SVGA_FIFO_CAP_SCREEN_OBJECT_2);
4842
4843 SVGAFifoCmdDefineScreen *pCmd;
4844 VMSVGAFIFO_GET_CMD_BUFFER_BREAK(pCmd, SVGAFifoCmdDefineScreen, sizeof(pCmd->screen.structSize));
4845 AssertBreak(pCmd->screen.structSize < pThis->svga.cbFIFO);
4846 RT_UNTRUSTED_VALIDATED_FENCE();
4847
4848 RT_BZERO(&pCmd->screen.id, sizeof(*pCmd) - RT_OFFSETOF(SVGAFifoCmdDefineScreen, screen.id));
4849 VMSVGAFIFO_GET_MORE_CMD_BUFFER_BREAK(pCmd, SVGAFifoCmdDefineScreen, RT_MAX(sizeof(pCmd->screen.structSize), pCmd->screen.structSize));
4850 vmsvgaR3CmdDefineScreen(pThis, pThisCC, pCmd);
4851 break;
4852 }
4853
4854 case SVGA_CMD_DESTROY_SCREEN:
4855 {
4856 SVGAFifoCmdDestroyScreen *pCmd;
4857 VMSVGAFIFO_GET_CMD_BUFFER_BREAK(pCmd, SVGAFifoCmdDestroyScreen, sizeof(*pCmd));
4858 vmsvgaR3CmdDestroyScreen(pThis, pThisCC, pCmd);
4859 break;
4860 }
4861
4862 case SVGA_CMD_DEFINE_GMRFB:
4863 {
4864 SVGAFifoCmdDefineGMRFB *pCmd;
4865 VMSVGAFIFO_GET_CMD_BUFFER_BREAK(pCmd, SVGAFifoCmdDefineGMRFB, sizeof(*pCmd));
4866 vmsvgaR3CmdDefineGMRFB(pThis, pThisCC, pCmd);
4867 break;
4868 }
4869
4870 case SVGA_CMD_BLIT_GMRFB_TO_SCREEN:
4871 {
4872 SVGAFifoCmdBlitGMRFBToScreen *pCmd;
4873 VMSVGAFIFO_GET_CMD_BUFFER_BREAK(pCmd, SVGAFifoCmdBlitGMRFBToScreen, sizeof(*pCmd));
4874 vmsvgaR3CmdBlitGMRFBToScreen(pThis, pThisCC, pCmd);
4875 break;
4876 }
4877
4878 case SVGA_CMD_BLIT_SCREEN_TO_GMRFB:
4879 {
4880 SVGAFifoCmdBlitScreenToGMRFB *pCmd;
4881 VMSVGAFIFO_GET_CMD_BUFFER_BREAK(pCmd, SVGAFifoCmdBlitScreenToGMRFB, sizeof(*pCmd));
4882 vmsvgaR3CmdBlitScreenToGMRFB(pThis, pThisCC, pCmd);
4883 break;
4884 }
4885
4886 case SVGA_CMD_ANNOTATION_FILL:
4887 {
4888 SVGAFifoCmdAnnotationFill *pCmd;
4889 VMSVGAFIFO_GET_CMD_BUFFER_BREAK(pCmd, SVGAFifoCmdAnnotationFill, sizeof(*pCmd));
4890 vmsvgaR3CmdAnnotationFill(pThis, pThisCC, pCmd);
4891 break;
4892 }
4893
4894 case SVGA_CMD_ANNOTATION_COPY:
4895 {
4896 SVGAFifoCmdAnnotationCopy *pCmd;
4897 VMSVGAFIFO_GET_CMD_BUFFER_BREAK(pCmd, SVGAFifoCmdAnnotationCopy, sizeof(*pCmd));
4898 vmsvgaR3CmdAnnotationCopy(pThis, pThisCC, pCmd);
4899 break;
4900 }
4901
4902 default:
4903# ifdef VBOX_WITH_VMSVGA3D
4904 if ( (int)enmCmdId >= SVGA_3D_CMD_BASE
4905 && (int)enmCmdId < SVGA_3D_CMD_MAX)
4906 {
4907 RT_UNTRUSTED_VALIDATED_FENCE();
4908
4909 /* All 3d commands start with a common header, which defines the identifier and the size
4910 * of the command. The identifier has been already read from FIFO. Fetch the size.
4911 */
4912 uint32_t *pcbCmd;
4913 VMSVGAFIFO_GET_CMD_BUFFER_BREAK(pcbCmd, uint32_t, sizeof(*pcbCmd));
4914 uint32_t const cbCmd = *pcbCmd;
4915 AssertBreak(cbCmd < pThis->svga.cbFIFO);
4916 uint32_t *pu32Cmd;
4917 VMSVGAFIFO_GET_MORE_CMD_BUFFER_BREAK(pu32Cmd, uint32_t, sizeof(*pcbCmd) + cbCmd);
4918 pu32Cmd++; /* Skip the command size. */
4919
4920 if (RT_LIKELY(pThis->svga.f3DEnabled))
4921 { /* likely */ }
4922 else
4923 {
4924 LogRelMax(8, ("VMSVGA: 3D disabled, command %d skipped\n", enmCmdId));
4925 break;
4926 }
4927
4928 vmsvgaR3Process3dCmd(pThis, pThisCC, (SVGAFifo3dCmdId)enmCmdId, cbCmd, pu32Cmd);
4929 }
4930 else
4931# endif // VBOX_WITH_VMSVGA3D
4932 {
4933 STAM_REL_COUNTER_INC(&pSVGAState->StatFifoUnkCmds);
4934 AssertMsgFailed(("enmCmdId=%d\n", enmCmdId));
4935 LogRelMax(16, ("VMSVGA: unsupported command %d\n", enmCmdId));
4936 }
4937 }
4938
4939 /* Go to the next slot */
4940 Assert(cbPayload + sizeof(uint32_t) <= offFifoMax - offFifoMin);
4941 offCurrentCmd += RT_ALIGN_32(cbPayload + sizeof(uint32_t), sizeof(uint32_t));
4942 if (offCurrentCmd >= offFifoMax)
4943 {
4944 offCurrentCmd -= offFifoMax - offFifoMin;
4945 Assert(offCurrentCmd >= offFifoMin);
4946 Assert(offCurrentCmd < offFifoMax);
4947 }
4948 ASMAtomicWriteU32(&pFIFO[SVGA_FIFO_STOP], offCurrentCmd);
4949 STAM_REL_COUNTER_INC(&pSVGAState->StatFifoCommands);
4950
4951 /*
4952 * Raise IRQ if required. Must enter the critical section here
4953 * before making final decisions here, otherwise cubebench and
4954 * others may end up waiting forever.
4955 */
4956 if ( u32IrqStatus
4957 || (pThis->svga.u32IrqMask & SVGA_IRQFLAG_FIFO_PROGRESS))
4958 {
4959 int rc2 = PDMDevHlpCritSectEnter(pDevIns, &pThis->CritSect, VERR_IGNORED);
4960 AssertRC(rc2);
4961
4962 /* FIFO progress might trigger an interrupt. */
4963 if (pThis->svga.u32IrqMask & SVGA_IRQFLAG_FIFO_PROGRESS)
4964 {
4965 Log(("vmsvgaR3FifoLoop: fifo progress irq\n"));
4966 u32IrqStatus |= SVGA_IRQFLAG_FIFO_PROGRESS;
4967 }
4968
4969 /* Unmasked IRQ pending? */
4970 if (pThis->svga.u32IrqMask & u32IrqStatus)
4971 {
4972 Log(("vmsvgaR3FifoLoop: Trigger interrupt with status %x\n", u32IrqStatus));
4973 ASMAtomicOrU32(&pThis->svga.u32IrqStatus, u32IrqStatus);
4974 PDMDevHlpPCISetIrq(pDevIns, 0, 1);
4975 }
4976
4977 PDMDevHlpCritSectLeave(pDevIns, &pThis->CritSect);
4978 }
4979 }
4980
4981 /* If really done, clear the busy flag. */
4982 if (fDone)
4983 {
4984 Log(("vmsvgaR3FifoLoop: emptied the FIFO next=%x stop=%x\n", pFIFO[SVGA_FIFO_NEXT_CMD], offCurrentCmd));
4985 vmsvgaR3FifoSetNotBusy(pDevIns, pThis, pThisCC, pSVGAState, offFifoMin);
4986 }
4987 }
4988
4989 /*
4990 * Free the bounce buffer. (There are no returns above!)
4991 */
4992 RTMemFree(pbBounceBuf);
4993
4994 return VINF_SUCCESS;
4995}
4996
4997#undef VMSVGAFIFO_GET_MORE_CMD_BUFFER_BREAK
4998#undef VMSVGAFIFO_GET_CMD_BUFFER_BREAK
4999
5000/**
5001 * @callback_method_impl{PFNPDMTHREADWAKEUPDEV,
5002 * Unblock the FIFO I/O thread so it can respond to a state change.}
5003 */
5004static DECLCALLBACK(int) vmsvgaR3FifoLoopWakeUp(PPDMDEVINS pDevIns, PPDMTHREAD pThread)
5005{
5006 RT_NOREF(pDevIns);
5007 PVGASTATE pThis = (PVGASTATE)pThread->pvUser;
5008 Log(("vmsvgaR3FifoLoopWakeUp\n"));
5009 return PDMDevHlpSUPSemEventSignal(pDevIns, pThis->svga.hFIFORequestSem);
5010}
5011
5012/**
5013 * Enables or disables dirty page tracking for the framebuffer
5014 *
5015 * @param pDevIns The device instance.
5016 * @param pThis The shared VGA/VMSVGA instance data.
5017 * @param fTraces Enable/disable traces
5018 */
5019static void vmsvgaR3SetTraces(PPDMDEVINS pDevIns, PVGASTATE pThis, bool fTraces)
5020{
5021 if ( (!pThis->svga.fConfigured || !pThis->svga.fEnabled)
5022 && !fTraces)
5023 {
5024 //Assert(pThis->svga.fTraces);
5025 Log(("vmsvgaR3SetTraces: *not* allowed to disable dirty page tracking when the device is in legacy mode.\n"));
5026 return;
5027 }
5028
5029 pThis->svga.fTraces = fTraces;
5030 if (pThis->svga.fTraces)
5031 {
5032 unsigned cbFrameBuffer = pThis->vram_size;
5033
5034 Log(("vmsvgaR3SetTraces: enable dirty page handling for the frame buffer only (%x bytes)\n", 0));
5035 /** @todo How does this work with screens? */
5036 if (pThis->svga.uHeight != VMSVGA_VAL_UNINITIALIZED)
5037 {
5038# ifndef DEBUG_bird /* BB-10.3.1 triggers this as it initializes everything to zero. Better just ignore it. */
5039 Assert(pThis->svga.cbScanline);
5040# endif
5041 /* Hardware enabled; return real framebuffer size .*/
5042 cbFrameBuffer = (uint32_t)pThis->svga.uHeight * pThis->svga.cbScanline;
5043 cbFrameBuffer = RT_ALIGN(cbFrameBuffer, PAGE_SIZE);
5044 }
5045
5046 if (!pThis->svga.fVRAMTracking)
5047 {
5048 Log(("vmsvgaR3SetTraces: enable frame buffer dirty page tracking. (%x bytes; vram %x)\n", cbFrameBuffer, pThis->vram_size));
5049 vgaR3RegisterVRAMHandler(pDevIns, pThis, cbFrameBuffer);
5050 pThis->svga.fVRAMTracking = true;
5051 }
5052 }
5053 else
5054 {
5055 if (pThis->svga.fVRAMTracking)
5056 {
5057 Log(("vmsvgaR3SetTraces: disable frame buffer dirty page tracking\n"));
5058 vgaR3UnregisterVRAMHandler(pDevIns, pThis);
5059 pThis->svga.fVRAMTracking = false;
5060 }
5061 }
5062}
5063
5064/**
5065 * @callback_method_impl{FNPCIIOREGIONMAP}
5066 */
5067DECLCALLBACK(int) vmsvgaR3PciIORegionFifoMapUnmap(PPDMDEVINS pDevIns, PPDMPCIDEV pPciDev, uint32_t iRegion,
5068 RTGCPHYS GCPhysAddress, RTGCPHYS cb, PCIADDRESSSPACE enmType)
5069{
5070 PVGASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVGASTATE);
5071 int rc;
5072 RT_NOREF(pPciDev);
5073 Assert(pPciDev == pDevIns->apPciDevs[0]);
5074
5075 Log(("vmsvgaR3PciIORegionFifoMapUnmap: iRegion=%d GCPhysAddress=%RGp cb=%RGp enmType=%d\n", iRegion, GCPhysAddress, cb, enmType));
5076 AssertReturn( iRegion == pThis->pciRegions.iFIFO
5077 && ( enmType == PCI_ADDRESS_SPACE_MEM
5078 || (enmType == PCI_ADDRESS_SPACE_MEM_PREFETCH /* got wrong in 6.1.0RC1 */ && pThis->fStateLoaded))
5079 , VERR_INTERNAL_ERROR);
5080 if (GCPhysAddress != NIL_RTGCPHYS)
5081 {
5082 /*
5083 * Mapping the FIFO RAM.
5084 */
5085 AssertLogRelMsg(cb == pThis->svga.cbFIFO, ("cb=%#RGp cbFIFO=%#x\n", cb, pThis->svga.cbFIFO));
5086 rc = PDMDevHlpMmio2Map(pDevIns, pThis->hMmio2VmSvgaFifo, GCPhysAddress);
5087 AssertRC(rc);
5088
5089# if defined(VMSVGA_USE_FIFO_ACCESS_HANDLER) || defined(DEBUG_FIFO_ACCESS)
5090 if (RT_SUCCESS(rc))
5091 {
5092 rc = PGMHandlerPhysicalRegister(PDMDevHlpGetVM(pDevIns), GCPhysAddress,
5093# ifdef DEBUG_FIFO_ACCESS
5094 GCPhysAddress + (pThis->svga.cbFIFO - 1),
5095# else
5096 GCPhysAddress + PAGE_SIZE - 1,
5097# endif
5098 pThis->svga.hFifoAccessHandlerType, pThis, NIL_RTR0PTR, NIL_RTRCPTR,
5099 "VMSVGA FIFO");
5100 AssertRC(rc);
5101 }
5102# endif
5103 if (RT_SUCCESS(rc))
5104 {
5105 pThis->svga.GCPhysFIFO = GCPhysAddress;
5106 Log(("vmsvgaR3IORegionMap: GCPhysFIFO=%RGp cbFIFO=%#x\n", GCPhysAddress, pThis->svga.cbFIFO));
5107 }
5108 rc = VINF_PCI_MAPPING_DONE; /* caller only cares about this status, so it is okay that we overwrite errors here. */
5109 }
5110 else
5111 {
5112 Assert(pThis->svga.GCPhysFIFO);
5113# if defined(VMSVGA_USE_FIFO_ACCESS_HANDLER) || defined(DEBUG_FIFO_ACCESS)
5114 rc = PGMHandlerPhysicalDeregister(PDMDevHlpGetVM(pDevIns), pThis->svga.GCPhysFIFO);
5115 AssertRC(rc);
5116# else
5117 rc = VINF_SUCCESS;
5118# endif
5119 pThis->svga.GCPhysFIFO = 0;
5120 }
5121 return rc;
5122}
5123
5124# ifdef VBOX_WITH_VMSVGA3D
5125
5126/**
5127 * Used by vmsvga3dInfoSurfaceWorker to make the FIFO thread to save one or all
5128 * surfaces to VMSVGA3DMIPMAPLEVEL::pSurfaceData heap buffers.
5129 *
5130 * @param pDevIns The device instance.
5131 * @param pThis The The shared VGA/VMSVGA instance data.
5132 * @param pThisCC The VGA/VMSVGA state for ring-3.
5133 * @param sid Either UINT32_MAX or the ID of a specific surface. If
5134 * UINT32_MAX is used, all surfaces are processed.
5135 */
5136void vmsvgaR33dSurfaceUpdateHeapBuffersOnFifoThread(PPDMDEVINS pDevIns, PVGASTATE pThis, PVGASTATECC pThisCC, uint32_t sid)
5137{
5138 vmsvgaR3RunExtCmdOnFifoThread(pDevIns, pThis, pThisCC, VMSVGA_FIFO_EXTCMD_UPDATE_SURFACE_HEAP_BUFFERS, (void *)(uintptr_t)sid,
5139 sid == UINT32_MAX ? 10 * RT_MS_1SEC : RT_MS_1MIN);
5140}
5141
5142
5143/**
5144 * @callback_method_impl{FNDBGFHANDLERDEV, "vmsvga3dsfc"}
5145 */
5146DECLCALLBACK(void) vmsvgaR3Info3dSurface(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
5147{
5148 /* There might be a specific surface ID at the start of the
5149 arguments, if not show all surfaces. */
5150 uint32_t sid = UINT32_MAX;
5151 if (pszArgs)
5152 pszArgs = RTStrStripL(pszArgs);
5153 if (pszArgs && RT_C_IS_DIGIT(*pszArgs))
5154 sid = RTStrToUInt32(pszArgs);
5155
5156 /* Verbose or terse display, we default to verbose. */
5157 bool fVerbose = true;
5158 if (RTStrIStr(pszArgs, "terse"))
5159 fVerbose = false;
5160
5161 /* The size of the ascii art (x direction, y is 3/4 of x). */
5162 uint32_t cxAscii = 80;
5163 if (RTStrIStr(pszArgs, "gigantic"))
5164 cxAscii = 300;
5165 else if (RTStrIStr(pszArgs, "huge"))
5166 cxAscii = 180;
5167 else if (RTStrIStr(pszArgs, "big"))
5168 cxAscii = 132;
5169 else if (RTStrIStr(pszArgs, "normal"))
5170 cxAscii = 80;
5171 else if (RTStrIStr(pszArgs, "medium"))
5172 cxAscii = 64;
5173 else if (RTStrIStr(pszArgs, "small"))
5174 cxAscii = 48;
5175 else if (RTStrIStr(pszArgs, "tiny"))
5176 cxAscii = 24;
5177
5178 /* Y invert the image when producing the ASCII art. */
5179 bool fInvY = false;
5180 if (RTStrIStr(pszArgs, "invy"))
5181 fInvY = true;
5182
5183 vmsvga3dInfoSurfaceWorker(pDevIns, PDMDEVINS_2_DATA(pDevIns, PVGASTATE), PDMDEVINS_2_DATA_CC(pDevIns, PVGASTATECC),
5184 pHlp, sid, fVerbose, cxAscii, fInvY, NULL);
5185}
5186
5187
5188/**
5189 * @callback_method_impl{FNDBGFHANDLERDEV, "vmsvga3dsurf"}
5190 */
5191DECLCALLBACK(void) vmsvgaR3Info3dSurfaceBmp(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
5192{
5193 /* pszArg = "sid[>dir]"
5194 * Writes %dir%/info-S-sidI.bmp, where S - sequential bitmap number, I - decimal surface id.
5195 */
5196 char *pszBitmapPath = NULL;
5197 uint32_t sid = UINT32_MAX;
5198 if (pszArgs)
5199 pszArgs = RTStrStripL(pszArgs);
5200 if (pszArgs && RT_C_IS_DIGIT(*pszArgs))
5201 RTStrToUInt32Ex(pszArgs, &pszBitmapPath, 0, &sid);
5202 if ( pszBitmapPath
5203 && *pszBitmapPath == '>')
5204 ++pszBitmapPath;
5205
5206 const bool fVerbose = true;
5207 const uint32_t cxAscii = 0; /* No ASCII */
5208 const bool fInvY = false; /* Do not invert. */
5209 vmsvga3dInfoSurfaceWorker(pDevIns, PDMDEVINS_2_DATA(pDevIns, PVGASTATE), PDMDEVINS_2_DATA_CC(pDevIns, PVGASTATECC),
5210 pHlp, sid, fVerbose, cxAscii, fInvY, pszBitmapPath);
5211}
5212
5213/**
5214 * @callback_method_impl{FNDBGFHANDLERDEV, "vmsvga3dctx"}
5215 */
5216DECLCALLBACK(void) vmsvgaR3Info3dContext(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
5217{
5218 /* There might be a specific surface ID at the start of the
5219 arguments, if not show all contexts. */
5220 uint32_t sid = UINT32_MAX;
5221 if (pszArgs)
5222 pszArgs = RTStrStripL(pszArgs);
5223 if (pszArgs && RT_C_IS_DIGIT(*pszArgs))
5224 sid = RTStrToUInt32(pszArgs);
5225
5226 /* Verbose or terse display, we default to verbose. */
5227 bool fVerbose = true;
5228 if (RTStrIStr(pszArgs, "terse"))
5229 fVerbose = false;
5230
5231 vmsvga3dInfoContextWorker(PDMDEVINS_2_DATA_CC(pDevIns, PVGASTATECC), pHlp, sid, fVerbose);
5232}
5233# endif /* VBOX_WITH_VMSVGA3D */
5234
5235/**
5236 * @callback_method_impl{FNDBGFHANDLERDEV, "vmsvga"}
5237 */
5238static DECLCALLBACK(void) vmsvgaR3Info(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
5239{
5240 PVGASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVGASTATE);
5241 PVGASTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVGASTATECC);
5242 PVMSVGAR3STATE pSVGAState = pThisCC->svga.pSvgaR3State;
5243 uint32_t RT_UNTRUSTED_VOLATILE_GUEST *pFIFO = pThisCC->svga.pau32FIFO;
5244 RT_NOREF(pszArgs);
5245
5246 pHlp->pfnPrintf(pHlp, "Extension enabled: %RTbool\n", pThis->svga.fEnabled);
5247 pHlp->pfnPrintf(pHlp, "Configured: %RTbool\n", pThis->svga.fConfigured);
5248 pHlp->pfnPrintf(pHlp, "Base I/O port: %#x\n",
5249 pThis->hIoPortVmSvga != NIL_IOMIOPORTHANDLE
5250 ? PDMDevHlpIoPortGetMappingAddress(pDevIns, pThis->hIoPortVmSvga) : UINT32_MAX);
5251 pHlp->pfnPrintf(pHlp, "FIFO address: %RGp\n", pThis->svga.GCPhysFIFO);
5252 pHlp->pfnPrintf(pHlp, "FIFO size: %u (%#x)\n", pThis->svga.cbFIFO, pThis->svga.cbFIFO);
5253 pHlp->pfnPrintf(pHlp, "FIFO external cmd: %#x\n", pThis->svga.u8FIFOExtCommand);
5254 pHlp->pfnPrintf(pHlp, "FIFO extcmd wakeup: %u\n", pThis->svga.fFifoExtCommandWakeup);
5255 pHlp->pfnPrintf(pHlp, "FIFO min/max: %u/%u\n", pFIFO[SVGA_FIFO_MIN], pFIFO[SVGA_FIFO_MAX]);
5256 pHlp->pfnPrintf(pHlp, "Busy: %#x\n", pThis->svga.fBusy);
5257 pHlp->pfnPrintf(pHlp, "Traces: %RTbool (effective: %RTbool)\n", pThis->svga.fTraces, pThis->svga.fVRAMTracking);
5258 pHlp->pfnPrintf(pHlp, "Guest ID: %#x (%d)\n", pThis->svga.u32GuestId, pThis->svga.u32GuestId);
5259 pHlp->pfnPrintf(pHlp, "IRQ status: %#x\n", pThis->svga.u32IrqStatus);
5260 pHlp->pfnPrintf(pHlp, "IRQ mask: %#x\n", pThis->svga.u32IrqMask);
5261 pHlp->pfnPrintf(pHlp, "Pitch lock: %#x (FIFO:%#x)\n", pThis->svga.u32PitchLock, pFIFO[SVGA_FIFO_PITCHLOCK]);
5262 pHlp->pfnPrintf(pHlp, "Current GMR ID: %#x\n", pThis->svga.u32CurrentGMRId);
5263 pHlp->pfnPrintf(pHlp, "Device Capabilites: %#x\n", pThis->svga.u32DeviceCaps);
5264 pHlp->pfnPrintf(pHlp, "Index reg: %#x\n", pThis->svga.u32IndexReg);
5265 pHlp->pfnPrintf(pHlp, "Action flags: %#x\n", pThis->svga.u32ActionFlags);
5266 pHlp->pfnPrintf(pHlp, "Max display size: %ux%u\n", pThis->svga.u32MaxWidth, pThis->svga.u32MaxHeight);
5267 pHlp->pfnPrintf(pHlp, "Display size: %ux%u %ubpp\n", pThis->svga.uWidth, pThis->svga.uHeight, pThis->svga.uBpp);
5268 pHlp->pfnPrintf(pHlp, "Scanline: %u (%#x)\n", pThis->svga.cbScanline, pThis->svga.cbScanline);
5269 pHlp->pfnPrintf(pHlp, "Viewport position: %ux%u\n", pThis->svga.viewport.x, pThis->svga.viewport.y);
5270 pHlp->pfnPrintf(pHlp, "Viewport size: %ux%u\n", pThis->svga.viewport.cx, pThis->svga.viewport.cy);
5271
5272 pHlp->pfnPrintf(pHlp, "Cursor active: %RTbool\n", pSVGAState->Cursor.fActive);
5273 pHlp->pfnPrintf(pHlp, "Cursor hotspot: %ux%u\n", pSVGAState->Cursor.xHotspot, pSVGAState->Cursor.yHotspot);
5274 pHlp->pfnPrintf(pHlp, "Cursor size: %ux%u\n", pSVGAState->Cursor.width, pSVGAState->Cursor.height);
5275 pHlp->pfnPrintf(pHlp, "Cursor byte size: %u (%#x)\n", pSVGAState->Cursor.cbData, pSVGAState->Cursor.cbData);
5276
5277 pHlp->pfnPrintf(pHlp, "FIFO cursor: state %u, screen %d\n", pFIFO[SVGA_FIFO_CURSOR_ON], pFIFO[SVGA_FIFO_CURSOR_SCREEN_ID]);
5278 pHlp->pfnPrintf(pHlp, "FIFO cursor at: %u,%u\n", pFIFO[SVGA_FIFO_CURSOR_X], pFIFO[SVGA_FIFO_CURSOR_Y]);
5279
5280 pHlp->pfnPrintf(pHlp, "Legacy cursor: ID %u, state %u\n", pThis->svga.uCursorID, pThis->svga.uCursorOn);
5281 pHlp->pfnPrintf(pHlp, "Legacy cursor at: %u,%u\n", pThis->svga.uCursorX, pThis->svga.uCursorY);
5282
5283# ifdef VBOX_WITH_VMSVGA3D
5284 pHlp->pfnPrintf(pHlp, "3D enabled: %RTbool\n", pThis->svga.f3DEnabled);
5285# endif
5286 if (pThisCC->pDrv)
5287 {
5288 pHlp->pfnPrintf(pHlp, "Driver mode: %ux%u %ubpp\n", pThisCC->pDrv->cx, pThisCC->pDrv->cy, pThisCC->pDrv->cBits);
5289 pHlp->pfnPrintf(pHlp, "Driver pitch: %u (%#x)\n", pThisCC->pDrv->cbScanline, pThisCC->pDrv->cbScanline);
5290 }
5291
5292 /* Dump screen information. */
5293 for (unsigned iScreen = 0; iScreen < RT_ELEMENTS(pSVGAState->aScreens); ++iScreen)
5294 {
5295 VMSVGASCREENOBJECT *pScreen = vmsvgaR3GetScreenObject(pThisCC, iScreen);
5296 if (pScreen)
5297 {
5298 pHlp->pfnPrintf(pHlp, "Screen %u defined (ID %u):\n", iScreen, pScreen->idScreen);
5299 pHlp->pfnPrintf(pHlp, " %u x %u x %ubpp @ %u, %u\n", pScreen->cWidth, pScreen->cHeight,
5300 pScreen->cBpp, pScreen->xOrigin, pScreen->yOrigin);
5301 pHlp->pfnPrintf(pHlp, " Pitch %u bytes, VRAM offset %X\n", pScreen->cbPitch, pScreen->offVRAM);
5302 pHlp->pfnPrintf(pHlp, " Flags %X", pScreen->fuScreen);
5303 if (pScreen->fuScreen != SVGA_SCREEN_MUST_BE_SET)
5304 {
5305 pHlp->pfnPrintf(pHlp, " (");
5306 if (pScreen->fuScreen & SVGA_SCREEN_IS_PRIMARY)
5307 pHlp->pfnPrintf(pHlp, " IS_PRIMARY");
5308 if (pScreen->fuScreen & SVGA_SCREEN_FULLSCREEN_HINT)
5309 pHlp->pfnPrintf(pHlp, " FULLSCREEN_HINT");
5310 if (pScreen->fuScreen & SVGA_SCREEN_DEACTIVATE)
5311 pHlp->pfnPrintf(pHlp, " DEACTIVATE");
5312 if (pScreen->fuScreen & SVGA_SCREEN_BLANKING)
5313 pHlp->pfnPrintf(pHlp, " BLANKING");
5314 pHlp->pfnPrintf(pHlp, " )");
5315 }
5316 pHlp->pfnPrintf(pHlp, ", %smodified\n", pScreen->fModified ? "" : "not ");
5317 }
5318 }
5319
5320}
5321
5322/**
5323 * Portion of VMSVGA state which must be loaded oin the FIFO thread.
5324 */
5325static int vmsvgaR3LoadExecFifo(PCPDMDEVHLPR3 pHlp, PVGASTATE pThis, PVGASTATECC pThisCC,
5326 PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
5327{
5328 RT_NOREF(uPass);
5329
5330 PVMSVGAR3STATE pSVGAState = pThisCC->svga.pSvgaR3State;
5331 int rc;
5332
5333 if (uVersion >= VGA_SAVEDSTATE_VERSION_VMSVGA_SCREENS)
5334 {
5335 uint32_t cScreens = 0;
5336 rc = pHlp->pfnSSMGetU32(pSSM, &cScreens);
5337 AssertRCReturn(rc, rc);
5338 AssertLogRelMsgReturn(cScreens <= _64K, /* big enough */
5339 ("cScreens=%#x\n", cScreens),
5340 VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
5341
5342 for (uint32_t i = 0; i < cScreens; ++i)
5343 {
5344 VMSVGASCREENOBJECT screen;
5345 RT_ZERO(screen);
5346
5347 rc = pHlp->pfnSSMGetStructEx(pSSM, &screen, sizeof(screen), 0, g_aVMSVGASCREENOBJECTFields, NULL);
5348 AssertLogRelRCReturn(rc, rc);
5349
5350 if (screen.idScreen < RT_ELEMENTS(pSVGAState->aScreens))
5351 {
5352 VMSVGASCREENOBJECT *pScreen = &pSVGAState->aScreens[screen.idScreen];
5353 *pScreen = screen;
5354 pScreen->fModified = true;
5355 }
5356 else
5357 {
5358 LogRel(("VGA: ignored screen object %d\n", screen.idScreen));
5359 }
5360 }
5361 }
5362 else
5363 {
5364 /* Try to setup at least the first screen. */
5365 VMSVGASCREENOBJECT *pScreen = &pSVGAState->aScreens[0];
5366 pScreen->fDefined = true;
5367 pScreen->fModified = true;
5368 pScreen->fuScreen = SVGA_SCREEN_MUST_BE_SET | SVGA_SCREEN_IS_PRIMARY;
5369 pScreen->idScreen = 0;
5370 pScreen->xOrigin = 0;
5371 pScreen->yOrigin = 0;
5372 pScreen->offVRAM = pThis->svga.uScreenOffset;
5373 pScreen->cbPitch = pThis->svga.cbScanline;
5374 pScreen->cWidth = pThis->svga.uWidth;
5375 pScreen->cHeight = pThis->svga.uHeight;
5376 pScreen->cBpp = pThis->svga.uBpp;
5377 }
5378
5379 return VINF_SUCCESS;
5380}
5381
5382/**
5383 * @copydoc FNSSMDEVLOADEXEC
5384 */
5385int vmsvgaR3LoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
5386{
5387 RT_NOREF(uPass);
5388 PVGASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVGASTATE);
5389 PVGASTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVGASTATECC);
5390 PVMSVGAR3STATE pSVGAState = pThisCC->svga.pSvgaR3State;
5391 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
5392 int rc;
5393
5394 /* Load our part of the VGAState */
5395 rc = pHlp->pfnSSMGetStructEx(pSSM, &pThis->svga, sizeof(pThis->svga), 0, g_aVGAStateSVGAFields, NULL);
5396 AssertRCReturn(rc, rc);
5397
5398 /* Load the VGA framebuffer. */
5399 AssertCompile(VMSVGA_VGA_FB_BACKUP_SIZE >= _32K);
5400 uint32_t cbVgaFramebuffer = _32K;
5401 if (uVersion >= VGA_SAVEDSTATE_VERSION_VMSVGA_VGA_FB_FIX)
5402 {
5403 rc = pHlp->pfnSSMGetU32(pSSM, &cbVgaFramebuffer);
5404 AssertRCReturn(rc, rc);
5405 AssertLogRelMsgReturn(cbVgaFramebuffer <= _4M && cbVgaFramebuffer >= _32K && RT_IS_POWER_OF_TWO(cbVgaFramebuffer),
5406 ("cbVgaFramebuffer=%#x - expected 32KB..4MB, power of two\n", cbVgaFramebuffer),
5407 VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
5408 AssertCompile(VMSVGA_VGA_FB_BACKUP_SIZE <= _4M);
5409 AssertCompile(RT_IS_POWER_OF_TWO(VMSVGA_VGA_FB_BACKUP_SIZE));
5410 }
5411 rc = pHlp->pfnSSMGetMem(pSSM, pThisCC->svga.pbVgaFrameBufferR3, RT_MIN(cbVgaFramebuffer, VMSVGA_VGA_FB_BACKUP_SIZE));
5412 AssertRCReturn(rc, rc);
5413 if (cbVgaFramebuffer > VMSVGA_VGA_FB_BACKUP_SIZE)
5414 pHlp->pfnSSMSkip(pSSM, cbVgaFramebuffer - VMSVGA_VGA_FB_BACKUP_SIZE);
5415 else if (cbVgaFramebuffer < VMSVGA_VGA_FB_BACKUP_SIZE)
5416 RT_BZERO(&pThisCC->svga.pbVgaFrameBufferR3[cbVgaFramebuffer], VMSVGA_VGA_FB_BACKUP_SIZE - cbVgaFramebuffer);
5417
5418 /* Load the VMSVGA state. */
5419 rc = pHlp->pfnSSMGetStructEx(pSSM, pSVGAState, sizeof(*pSVGAState), 0, g_aVMSVGAR3STATEFields, NULL);
5420 AssertRCReturn(rc, rc);
5421
5422 /* Load the active cursor bitmaps. */
5423 if (pSVGAState->Cursor.fActive)
5424 {
5425 pSVGAState->Cursor.pData = RTMemAlloc(pSVGAState->Cursor.cbData);
5426 AssertReturn(pSVGAState->Cursor.pData, VERR_NO_MEMORY);
5427
5428 rc = pHlp->pfnSSMGetMem(pSSM, pSVGAState->Cursor.pData, pSVGAState->Cursor.cbData);
5429 AssertRCReturn(rc, rc);
5430 }
5431
5432 /* Load the GMR state. */
5433 uint32_t cGMR = 256; /* Hardcoded in previous saved state versions. */
5434 if (uVersion >= VGA_SAVEDSTATE_VERSION_VMSVGA_GMR_COUNT)
5435 {
5436 rc = pHlp->pfnSSMGetU32(pSSM, &cGMR);
5437 AssertRCReturn(rc, rc);
5438 /* Numbers of GMRs was never less than 256. 1MB is a large arbitrary limit. */
5439 AssertLogRelMsgReturn(cGMR <= _1M && cGMR >= 256,
5440 ("cGMR=%#x - expected 256B..1MB\n", cGMR),
5441 VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
5442 }
5443
5444 if (pThis->svga.cGMR != cGMR)
5445 {
5446 /* Reallocate GMR array. */
5447 Assert(pSVGAState->paGMR != NULL);
5448 RTMemFree(pSVGAState->paGMR);
5449 pSVGAState->paGMR = (PGMR)RTMemAllocZ(cGMR * sizeof(GMR));
5450 AssertReturn(pSVGAState->paGMR, VERR_NO_MEMORY);
5451 pThis->svga.cGMR = cGMR;
5452 }
5453
5454 for (uint32_t i = 0; i < cGMR; ++i)
5455 {
5456 PGMR pGMR = &pSVGAState->paGMR[i];
5457
5458 rc = pHlp->pfnSSMGetStructEx(pSSM, pGMR, sizeof(*pGMR), 0, g_aGMRFields, NULL);
5459 AssertRCReturn(rc, rc);
5460
5461 if (pGMR->numDescriptors)
5462 {
5463 Assert(pGMR->cMaxPages || pGMR->cbTotal);
5464 pGMR->paDesc = (PVMSVGAGMRDESCRIPTOR)RTMemAllocZ(pGMR->numDescriptors * sizeof(VMSVGAGMRDESCRIPTOR));
5465 AssertReturn(pGMR->paDesc, VERR_NO_MEMORY);
5466
5467 for (uint32_t j = 0; j < pGMR->numDescriptors; ++j)
5468 {
5469 rc = pHlp->pfnSSMGetStructEx(pSSM, &pGMR->paDesc[j], sizeof(pGMR->paDesc[j]), 0, g_aVMSVGAGMRDESCRIPTORFields, NULL);
5470 AssertRCReturn(rc, rc);
5471 }
5472 }
5473 }
5474
5475# ifdef RT_OS_DARWIN /** @todo r=bird: this is normally done on the EMT, so for DARWIN we do that when loading saved state too now. See DevVGA-SVGA3d-shared.h. */
5476 vmsvga3dPowerOn(pDevIns, pThis, PDMDEVINS_2_DATA_CC(pDevIns, PVGASTATECC));
5477# endif
5478
5479 VMSVGA_STATE_LOAD LoadState;
5480 LoadState.pSSM = pSSM;
5481 LoadState.uVersion = uVersion;
5482 LoadState.uPass = uPass;
5483 rc = vmsvgaR3RunExtCmdOnFifoThread(pDevIns, pThis, pThisCC, VMSVGA_FIFO_EXTCMD_LOADSTATE, &LoadState, RT_INDEFINITE_WAIT);
5484 AssertLogRelRCReturn(rc, rc);
5485
5486 return VINF_SUCCESS;
5487}
5488
5489/**
5490 * Reinit the video mode after the state has been loaded.
5491 */
5492int vmsvgaR3LoadDone(PPDMDEVINS pDevIns)
5493{
5494 PVGASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVGASTATE);
5495 PVGASTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVGASTATECC);
5496 PVMSVGAR3STATE pSVGAState = pThisCC->svga.pSvgaR3State;
5497
5498 /* Set the active cursor. */
5499 if (pSVGAState->Cursor.fActive)
5500 {
5501 /* We don't store the alpha flag, but we can take a guess that if
5502 * the old register interface was used, the cursor was B&W.
5503 */
5504 bool fAlpha = pThis->svga.uCursorOn ? false : true;
5505
5506 int rc = pThisCC->pDrv->pfnVBVAMousePointerShape(pThisCC->pDrv,
5507 true /*fVisible*/,
5508 fAlpha,
5509 pSVGAState->Cursor.xHotspot,
5510 pSVGAState->Cursor.yHotspot,
5511 pSVGAState->Cursor.width,
5512 pSVGAState->Cursor.height,
5513 pSVGAState->Cursor.pData);
5514 AssertRC(rc);
5515
5516 if (pThis->svga.uCursorOn)
5517 pThisCC->pDrv->pfnVBVAReportCursorPosition(pThisCC->pDrv, VBVA_CURSOR_VALID_DATA, SVGA_ID_INVALID, pThis->svga.uCursorX, pThis->svga.uCursorY);
5518 }
5519
5520 /* If the VRAM handler should not be registered, we have to explicitly
5521 * unregister it here!
5522 */
5523 if (!pThis->svga.fVRAMTracking)
5524 {
5525 vgaR3UnregisterVRAMHandler(pDevIns, pThis);
5526 }
5527
5528 /* Let the FIFO thread deal with changing the mode. */
5529 ASMAtomicOrU32(&pThis->svga.u32ActionFlags, VMSVGA_ACTION_CHANGEMODE);
5530
5531 return VINF_SUCCESS;
5532}
5533
5534/**
5535 * Portion of SVGA state which must be saved in the FIFO thread.
5536 */
5537static int vmsvgaR3SaveExecFifo(PCPDMDEVHLPR3 pHlp, PVGASTATECC pThisCC, PSSMHANDLE pSSM)
5538{
5539 PVMSVGAR3STATE pSVGAState = pThisCC->svga.pSvgaR3State;
5540 int rc;
5541
5542 /* Save the screen objects. */
5543 /* Count defined screen object. */
5544 uint32_t cScreens = 0;
5545 for (uint32_t i = 0; i < RT_ELEMENTS(pSVGAState->aScreens); ++i)
5546 {
5547 if (pSVGAState->aScreens[i].fDefined)
5548 ++cScreens;
5549 }
5550
5551 rc = pHlp->pfnSSMPutU32(pSSM, cScreens);
5552 AssertLogRelRCReturn(rc, rc);
5553
5554 for (uint32_t i = 0; i < cScreens; ++i)
5555 {
5556 VMSVGASCREENOBJECT *pScreen = &pSVGAState->aScreens[i];
5557
5558 rc = pHlp->pfnSSMPutStructEx(pSSM, pScreen, sizeof(*pScreen), 0, g_aVMSVGASCREENOBJECTFields, NULL);
5559 AssertLogRelRCReturn(rc, rc);
5560 }
5561 return VINF_SUCCESS;
5562}
5563
5564/**
5565 * @copydoc FNSSMDEVSAVEEXEC
5566 */
5567int vmsvgaR3SaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
5568{
5569 PVGASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVGASTATE);
5570 PVGASTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVGASTATECC);
5571 PVMSVGAR3STATE pSVGAState = pThisCC->svga.pSvgaR3State;
5572 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
5573 int rc;
5574
5575 /* Save our part of the VGAState */
5576 rc = pHlp->pfnSSMPutStructEx(pSSM, &pThis->svga, sizeof(pThis->svga), 0, g_aVGAStateSVGAFields, NULL);
5577 AssertLogRelRCReturn(rc, rc);
5578
5579 /* Save the framebuffer backup. */
5580 rc = pHlp->pfnSSMPutU32(pSSM, VMSVGA_VGA_FB_BACKUP_SIZE);
5581 rc = pHlp->pfnSSMPutMem(pSSM, pThisCC->svga.pbVgaFrameBufferR3, VMSVGA_VGA_FB_BACKUP_SIZE);
5582 AssertLogRelRCReturn(rc, rc);
5583
5584 /* Save the VMSVGA state. */
5585 rc = pHlp->pfnSSMPutStructEx(pSSM, pSVGAState, sizeof(*pSVGAState), 0, g_aVMSVGAR3STATEFields, NULL);
5586 AssertLogRelRCReturn(rc, rc);
5587
5588 /* Save the active cursor bitmaps. */
5589 if (pSVGAState->Cursor.fActive)
5590 {
5591 rc = pHlp->pfnSSMPutMem(pSSM, pSVGAState->Cursor.pData, pSVGAState->Cursor.cbData);
5592 AssertLogRelRCReturn(rc, rc);
5593 }
5594
5595 /* Save the GMR state */
5596 rc = pHlp->pfnSSMPutU32(pSSM, pThis->svga.cGMR);
5597 AssertLogRelRCReturn(rc, rc);
5598 for (uint32_t i = 0; i < pThis->svga.cGMR; ++i)
5599 {
5600 PGMR pGMR = &pSVGAState->paGMR[i];
5601
5602 rc = pHlp->pfnSSMPutStructEx(pSSM, pGMR, sizeof(*pGMR), 0, g_aGMRFields, NULL);
5603 AssertLogRelRCReturn(rc, rc);
5604
5605 for (uint32_t j = 0; j < pGMR->numDescriptors; ++j)
5606 {
5607 rc = pHlp->pfnSSMPutStructEx(pSSM, &pGMR->paDesc[j], sizeof(pGMR->paDesc[j]), 0, g_aVMSVGAGMRDESCRIPTORFields, NULL);
5608 AssertLogRelRCReturn(rc, rc);
5609 }
5610 }
5611
5612 /*
5613 * Must save some state (3D in particular) in the FIFO thread.
5614 */
5615 rc = vmsvgaR3RunExtCmdOnFifoThread(pDevIns, pThis, pThisCC, VMSVGA_FIFO_EXTCMD_SAVESTATE, pSSM, RT_INDEFINITE_WAIT);
5616 AssertLogRelRCReturn(rc, rc);
5617
5618 return VINF_SUCCESS;
5619}
5620
5621/**
5622 * Destructor for PVMSVGAR3STATE structure.
5623 *
5624 * @param pThis The shared VGA/VMSVGA instance data.
5625 * @param pSVGAState Pointer to the structure. It is not deallocated.
5626 */
5627static void vmsvgaR3StateTerm(PVGASTATE pThis, PVMSVGAR3STATE pSVGAState)
5628{
5629# ifndef VMSVGA_USE_EMT_HALT_CODE
5630 if (pSVGAState->hBusyDelayedEmts != NIL_RTSEMEVENTMULTI)
5631 {
5632 RTSemEventMultiDestroy(pSVGAState->hBusyDelayedEmts);
5633 pSVGAState->hBusyDelayedEmts = NIL_RTSEMEVENT;
5634 }
5635# endif
5636
5637 if (pSVGAState->Cursor.fActive)
5638 {
5639 RTMemFreeZ(pSVGAState->Cursor.pData, pSVGAState->Cursor.cbData);
5640 pSVGAState->Cursor.pData = NULL;
5641 pSVGAState->Cursor.fActive = false;
5642 }
5643
5644 if (pSVGAState->paGMR)
5645 {
5646 for (unsigned i = 0; i < pThis->svga.cGMR; ++i)
5647 if (pSVGAState->paGMR[i].paDesc)
5648 RTMemFree(pSVGAState->paGMR[i].paDesc);
5649
5650 RTMemFree(pSVGAState->paGMR);
5651 pSVGAState->paGMR = NULL;
5652 }
5653
5654 if (RTCritSectIsInitialized(&pSVGAState->CritSectCmdBuf))
5655 {
5656 RTCritSectEnter(&pSVGAState->CritSectCmdBuf);
5657 for (unsigned i = 0; i < RT_ELEMENTS(pSVGAState->apCmdBufCtxs); ++i)
5658 {
5659 vmsvgaR3CmdBufCtxTerm(pSVGAState->apCmdBufCtxs[i]);
5660 pSVGAState->apCmdBufCtxs[i] = NULL;
5661 }
5662 vmsvgaR3CmdBufCtxTerm(&pSVGAState->CmdBufCtxDC);
5663 RTCritSectLeave(&pSVGAState->CritSectCmdBuf);
5664 RTCritSectDelete(&pSVGAState->CritSectCmdBuf);
5665 }
5666
5667# ifdef VBOX_WITH_VMSVGA3D
5668 RTMemFree(pSVGAState->pFuncsMap);
5669 pSVGAState->pFuncsMap = NULL;
5670 RTMemFree(pSVGAState->pFuncsGBO);
5671 pSVGAState->pFuncsGBO = NULL;
5672 RTMemFree(pSVGAState->pFuncsDX);
5673 pSVGAState->pFuncsDX = NULL;
5674# endif
5675}
5676
5677/**
5678 * Constructor for PVMSVGAR3STATE structure.
5679 *
5680 * @returns VBox status code.
5681 * @param pDevIns The PDM device instance.
5682 * @param pThis The shared VGA/VMSVGA instance data.
5683 * @param pSVGAState Pointer to the structure. It is already allocated.
5684 */
5685static int vmsvgaR3StateInit(PPDMDEVINS pDevIns, PVGASTATE pThis, PVMSVGAR3STATE pSVGAState)
5686{
5687 int rc = VINF_SUCCESS;
5688
5689 pSVGAState->pDevIns = pDevIns;
5690
5691 pSVGAState->paGMR = (PGMR)RTMemAllocZ(pThis->svga.cGMR * sizeof(GMR));
5692 AssertReturn(pSVGAState->paGMR, VERR_NO_MEMORY);
5693
5694# ifndef VMSVGA_USE_EMT_HALT_CODE
5695 /* Create semaphore for delaying EMTs wait for the FIFO to stop being busy. */
5696 rc = RTSemEventMultiCreate(&pSVGAState->hBusyDelayedEmts);
5697 AssertRCReturn(rc, rc);
5698# endif
5699
5700 rc = RTCritSectInit(&pSVGAState->CritSectCmdBuf);
5701 AssertRCReturn(rc, rc);
5702
5703 vmsvgaR3CmdBufCtxInit(&pSVGAState->CmdBufCtxDC);
5704
5705 RTListInit(&pSVGAState->MOBLRUList);
5706 return rc;
5707}
5708
5709# ifdef VBOX_WITH_VMSVGA3D
5710/**
5711 * Initializes the optional host 3D backend interfaces.
5712 *
5713 * @returns VBox status code.
5714 * @param pThisCC The VGA/VMSVGA state for ring-3.
5715 */
5716static int vmsvgaR3Init3dInterfaces(PVGASTATECC pThisCC)
5717{
5718 PVMSVGAR3STATE pSVGAState = pThisCC->svga.pSvgaR3State;
5719
5720 int rc = vmsvga3dQueryInterface(pThisCC, VMSVGA3D_BACKEND_INTERFACE_NAME_DX, NULL, sizeof(VMSVGA3DBACKENDFUNCSDX));
5721 if (RT_SUCCESS(rc))
5722 {
5723 pSVGAState->pFuncsDX = (VMSVGA3DBACKENDFUNCSDX *)RTMemAllocZ(sizeof(VMSVGA3DBACKENDFUNCSDX));
5724 AssertReturn(pSVGAState->pFuncsDX, VERR_NO_MEMORY);
5725
5726 vmsvga3dQueryInterface(pThisCC, VMSVGA3D_BACKEND_INTERFACE_NAME_DX, pSVGAState->pFuncsDX, sizeof(VMSVGA3DBACKENDFUNCSDX));
5727 }
5728
5729 rc = vmsvga3dQueryInterface(pThisCC, VMSVGA3D_BACKEND_INTERFACE_NAME_MAP, NULL, sizeof(VMSVGA3DBACKENDFUNCSMAP));
5730 if (RT_SUCCESS(rc))
5731 {
5732 pSVGAState->pFuncsMap = (VMSVGA3DBACKENDFUNCSMAP *)RTMemAllocZ(sizeof(VMSVGA3DBACKENDFUNCSMAP));
5733 AssertReturn(pSVGAState->pFuncsMap, VERR_NO_MEMORY);
5734
5735 vmsvga3dQueryInterface(pThisCC, VMSVGA3D_BACKEND_INTERFACE_NAME_MAP, pSVGAState->pFuncsMap, sizeof(VMSVGA3DBACKENDFUNCSMAP));
5736 }
5737
5738 rc = vmsvga3dQueryInterface(pThisCC, VMSVGA3D_BACKEND_INTERFACE_NAME_GBO, NULL, sizeof(VMSVGA3DBACKENDFUNCSGBO));
5739 if (RT_SUCCESS(rc))
5740 {
5741 pSVGAState->pFuncsGBO = (VMSVGA3DBACKENDFUNCSGBO *)RTMemAllocZ(sizeof(VMSVGA3DBACKENDFUNCSGBO));
5742 AssertReturn(pSVGAState->pFuncsGBO, VERR_NO_MEMORY);
5743
5744 vmsvga3dQueryInterface(pThisCC, VMSVGA3D_BACKEND_INTERFACE_NAME_GBO, pSVGAState->pFuncsGBO, sizeof(VMSVGA3DBACKENDFUNCSGBO));
5745 }
5746
5747 return VINF_SUCCESS;
5748}
5749# endif
5750
5751/**
5752 * Initializes the host capabilities: device and FIFO.
5753 *
5754 * @returns VBox status code.
5755 * @param pThis The shared VGA/VMSVGA instance data.
5756 * @param pThisCC The VGA/VMSVGA state for ring-3.
5757 */
5758static void vmsvgaR3InitCaps(PVGASTATE pThis, PVGASTATECC pThisCC)
5759{
5760 /* Device caps. */
5761 pThis->svga.u32DeviceCaps = SVGA_CAP_GMR
5762 | SVGA_CAP_GMR2
5763 | SVGA_CAP_CURSOR
5764 | SVGA_CAP_CURSOR_BYPASS
5765 | SVGA_CAP_CURSOR_BYPASS_2
5766 | SVGA_CAP_EXTENDED_FIFO
5767 | SVGA_CAP_IRQMASK
5768 | SVGA_CAP_PITCHLOCK
5769 | SVGA_CAP_RECT_COPY
5770 | SVGA_CAP_TRACES
5771 | SVGA_CAP_SCREEN_OBJECT_2
5772 | SVGA_CAP_ALPHA_CURSOR;
5773
5774 /* VGPU10 capabilities. */
5775 if (pThis->fVMSVGA10)
5776 {
5777 pThis->svga.u32DeviceCaps |= SVGA_CAP_COMMAND_BUFFERS /* Enable register based command buffer submission. */
5778// | SVGA_CAP_CMD_BUFFERS_2 /* Support for SVGA_REG_CMD_PREPEND_LOW/HIGH */
5779 ;
5780
5781# ifdef VBOX_WITH_VMSVGA3D
5782 PVMSVGAR3STATE pSVGAState = pThisCC->svga.pSvgaR3State;
5783 if (pSVGAState->pFuncsGBO)
5784 pThis->svga.u32DeviceCaps |= SVGA_CAP_GBOBJECTS; /* Enable guest-backed objects and surfaces. */
5785 if (pSVGAState->pFuncsDX)
5786 pThis->svga.u32DeviceCaps |= SVGA_CAP_CMD_BUFFERS_3; /* AKA SVGA_CAP_DX. Enable support for DX commands, and command buffers in a mob. */
5787# endif
5788 }
5789
5790# ifdef VBOX_WITH_VMSVGA3D
5791 pThis->svga.u32DeviceCaps |= SVGA_CAP_3D;
5792# endif
5793
5794 /* Clear the FIFO. */
5795 RT_BZERO(pThisCC->svga.pau32FIFO, pThis->svga.cbFIFO);
5796
5797 /* Setup FIFO capabilities. */
5798 pThisCC->svga.pau32FIFO[SVGA_FIFO_CAPABILITIES] = SVGA_FIFO_CAP_FENCE
5799 | SVGA_FIFO_CAP_PITCHLOCK
5800 | SVGA_FIFO_CAP_CURSOR_BYPASS_3
5801 | SVGA_FIFO_CAP_RESERVE
5802 | SVGA_FIFO_CAP_GMR2
5803 | SVGA_FIFO_CAP_3D_HWVERSION_REVISED
5804 | SVGA_FIFO_CAP_SCREEN_OBJECT_2;
5805
5806 /* Valid with SVGA_FIFO_CAP_SCREEN_OBJECT_2 */
5807 pThisCC->svga.pau32FIFO[SVGA_FIFO_CURSOR_SCREEN_ID] = SVGA_ID_INVALID;
5808}
5809
5810# ifdef VBOX_WITH_VMSVGA3D
5811/**
5812 * Initializes the host 3D capabilities and writes them to FIFO memory.
5813 *
5814 * @returns VBox status code.
5815 * @param pThis The shared VGA/VMSVGA instance data.
5816 * @param pThisCC The VGA/VMSVGA state for ring-3.
5817 */
5818static void vmsvgaR3InitFifo3DCaps(PVGASTATE pThis, PVGASTATECC pThisCC)
5819{
5820 /* Query the capabilities and store them in the pThis->svga.au32DevCaps array. */
5821 bool const fSavedBuffering = RTLogRelSetBuffering(true);
5822
5823 for (unsigned i = 0; i < RT_ELEMENTS(pThis->svga.au32DevCaps); ++i)
5824 {
5825 uint32_t val = 0;
5826 int rc = vmsvga3dQueryCaps(pThisCC, (SVGA3dDevCapIndex)i, &val);
5827 if (RT_SUCCESS(rc))
5828 pThis->svga.au32DevCaps[i] = val;
5829 else
5830 pThis->svga.au32DevCaps[i] = 0;
5831
5832 /* LogRel the capability value. */
5833 if (i < SVGA3D_DEVCAP_MAX)
5834 {
5835 char const *pszDevCapName = &vmsvgaDevCapIndexToString((SVGA3dDevCapIndex)i)[sizeof("SVGA3D_DEVCAP")];
5836 if (RT_SUCCESS(rc))
5837 {
5838 if ( i == SVGA3D_DEVCAP_MAX_POINT_SIZE
5839 || i == SVGA3D_DEVCAP_MAX_LINE_WIDTH
5840 || i == SVGA3D_DEVCAP_MAX_AA_LINE_WIDTH)
5841 {
5842 float const fval = *(float *)&val;
5843 LogRel(("VMSVGA3d: cap[%u]=" FLOAT_FMT_STR " {%s}\n", i, FLOAT_FMT_ARGS(fval), pszDevCapName));
5844 }
5845 else
5846 LogRel(("VMSVGA3d: cap[%u]=%#010x {%s}\n", i, val, pszDevCapName));
5847 }
5848 else
5849 LogRel(("VMSVGA3d: cap[%u]=failed rc=%Rrc {%s}\n", i, rc, pszDevCapName));
5850 }
5851 else
5852 LogRel(("VMSVGA3d: new cap[%u]=%#010x rc=%Rrc\n", i, val, rc));
5853 }
5854
5855 RTLogRelSetBuffering(fSavedBuffering);
5856
5857 /* 3d hardware version; latest and greatest */
5858 pThisCC->svga.pau32FIFO[SVGA_FIFO_3D_HWVERSION_REVISED] = SVGA3D_HWVERSION_CURRENT;
5859 pThisCC->svga.pau32FIFO[SVGA_FIFO_3D_HWVERSION] = SVGA3D_HWVERSION_CURRENT;
5860
5861 /* Fill out 3d capabilities up to SVGA3D_DEVCAP_SURFACEFMT_ATI2 in the FIFO memory.
5862 * SVGA3D_DEVCAP_SURFACEFMT_ATI2 is the last capabiltiy for pre-SVGA_CAP_GBOBJECTS hardware.
5863 * If the VMSVGA device supports SVGA_CAP_GBOBJECTS capability, then the guest has to use SVGA_REG_DEV_CAP
5864 * register to query the devcaps. Older guests will still try to read the devcaps from FIFO.
5865 */
5866 SVGA3dCapsRecord *pCaps;
5867 SVGA3dCapPair *pData;
5868
5869 pCaps = (SVGA3dCapsRecord *)&pThisCC->svga.pau32FIFO[SVGA_FIFO_3D_CAPS];
5870 pCaps->header.type = SVGA3DCAPS_RECORD_DEVCAPS;
5871 pData = (SVGA3dCapPair *)&pCaps->data;
5872
5873 AssertCompile(SVGA3D_DEVCAP_DEAD1 == SVGA3D_DEVCAP_SURFACEFMT_ATI2 + 1);
5874 for (unsigned i = 0; i < SVGA3D_DEVCAP_DEAD1; ++i)
5875 {
5876 pData[i][0] = i;
5877 pData[i][1] = pThis->svga.au32DevCaps[i];
5878 }
5879 pCaps->header.length = (sizeof(pCaps->header) + SVGA3D_DEVCAP_DEAD1 * sizeof(SVGA3dCapPair)) / sizeof(uint32_t);
5880 pCaps = (SVGA3dCapsRecord *)((uint32_t *)pCaps + pCaps->header.length);
5881
5882 /* Mark end of record array (a zero word). */
5883 pCaps->header.length = 0;
5884}
5885
5886# endif
5887
5888/**
5889 * Resets the SVGA hardware state
5890 *
5891 * @returns VBox status code.
5892 * @param pDevIns The device instance.
5893 */
5894int vmsvgaR3Reset(PPDMDEVINS pDevIns)
5895{
5896 PVGASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVGASTATE);
5897 PVGASTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVGASTATECC);
5898 PVMSVGAR3STATE pSVGAState = pThisCC->svga.pSvgaR3State;
5899
5900 /* Reset before init? */
5901 if (!pSVGAState)
5902 return VINF_SUCCESS;
5903
5904 Log(("vmsvgaR3Reset\n"));
5905
5906 /* Reset the FIFO processing as well as the 3d state (if we have one). */
5907 pThisCC->svga.pau32FIFO[SVGA_FIFO_NEXT_CMD] = pThisCC->svga.pau32FIFO[SVGA_FIFO_STOP] = 0; /** @todo should probably let the FIFO thread do this ... */
5908 int rc = vmsvgaR3RunExtCmdOnFifoThread(pDevIns, pThis, pThisCC, VMSVGA_FIFO_EXTCMD_RESET, NULL /*pvParam*/, 10000 /*ms*/);
5909
5910 /* Reset other stuff. */
5911 pThis->svga.cScratchRegion = VMSVGA_SCRATCH_SIZE;
5912 RT_ZERO(pThis->svga.au32ScratchRegion);
5913
5914 ASMAtomicWriteBool(&pThis->svga.fBadGuest, false);
5915
5916 vmsvgaR3StateTerm(pThis, pThisCC->svga.pSvgaR3State);
5917 vmsvgaR3StateInit(pDevIns, pThis, pThisCC->svga.pSvgaR3State);
5918
5919 RT_BZERO(pThisCC->svga.pbVgaFrameBufferR3, VMSVGA_VGA_FB_BACKUP_SIZE);
5920
5921# ifdef VBOX_WITH_VMSVGA3D
5922 /* Device capabilities depend on this. */
5923 if (pThis->svga.f3DEnabled)
5924 vmsvgaR3Init3dInterfaces(pThisCC);
5925# endif
5926
5927 /* Initialize FIFO and register capabilities. */
5928 vmsvgaR3InitCaps(pThis, pThisCC);
5929
5930# ifdef VBOX_WITH_VMSVGA3D
5931 if (pThis->svga.f3DEnabled)
5932 vmsvgaR3InitFifo3DCaps(pThis, pThisCC);
5933# endif
5934
5935 /* VRAM tracking is enabled by default during bootup. */
5936 pThis->svga.fVRAMTracking = true;
5937 pThis->svga.fEnabled = false;
5938
5939 /* Invalidate current settings. */
5940 pThis->svga.uWidth = VMSVGA_VAL_UNINITIALIZED;
5941 pThis->svga.uHeight = VMSVGA_VAL_UNINITIALIZED;
5942 pThis->svga.uBpp = pThis->svga.uHostBpp;
5943 pThis->svga.cbScanline = 0;
5944 pThis->svga.u32PitchLock = 0;
5945
5946 return rc;
5947}
5948
5949/**
5950 * Cleans up the SVGA hardware state
5951 *
5952 * @returns VBox status code.
5953 * @param pDevIns The device instance.
5954 */
5955int vmsvgaR3Destruct(PPDMDEVINS pDevIns)
5956{
5957 PVGASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVGASTATE);
5958 PVGASTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVGASTATECC);
5959
5960 /*
5961 * Ask the FIFO thread to terminate the 3d state and then terminate it.
5962 */
5963 if (pThisCC->svga.pFIFOIOThread)
5964 {
5965 int rc = vmsvgaR3RunExtCmdOnFifoThread(pDevIns, pThis, pThisCC, VMSVGA_FIFO_EXTCMD_TERMINATE,
5966 NULL /*pvParam*/, 30000 /*ms*/);
5967 AssertLogRelRC(rc);
5968
5969 rc = PDMDevHlpThreadDestroy(pDevIns, pThisCC->svga.pFIFOIOThread, NULL);
5970 AssertLogRelRC(rc);
5971 pThisCC->svga.pFIFOIOThread = NULL;
5972 }
5973
5974 /*
5975 * Destroy the special SVGA state.
5976 */
5977 if (pThisCC->svga.pSvgaR3State)
5978 {
5979 vmsvgaR3StateTerm(pThis, pThisCC->svga.pSvgaR3State);
5980
5981 RTMemFree(pThisCC->svga.pSvgaR3State);
5982 pThisCC->svga.pSvgaR3State = NULL;
5983 }
5984
5985 /*
5986 * Free our resources residing in the VGA state.
5987 */
5988 if (pThisCC->svga.pbVgaFrameBufferR3)
5989 {
5990 RTMemFree(pThisCC->svga.pbVgaFrameBufferR3);
5991 pThisCC->svga.pbVgaFrameBufferR3 = NULL;
5992 }
5993 if (pThisCC->svga.hFIFOExtCmdSem != NIL_RTSEMEVENT)
5994 {
5995 RTSemEventDestroy(pThisCC->svga.hFIFOExtCmdSem);
5996 pThisCC->svga.hFIFOExtCmdSem = NIL_RTSEMEVENT;
5997 }
5998 if (pThis->svga.hFIFORequestSem != NIL_SUPSEMEVENT)
5999 {
6000 PDMDevHlpSUPSemEventClose(pDevIns, pThis->svga.hFIFORequestSem);
6001 pThis->svga.hFIFORequestSem = NIL_SUPSEMEVENT;
6002 }
6003
6004 return VINF_SUCCESS;
6005}
6006
6007/**
6008 * Initialize the SVGA hardware state
6009 *
6010 * @returns VBox status code.
6011 * @param pDevIns The device instance.
6012 */
6013int vmsvgaR3Init(PPDMDEVINS pDevIns)
6014{
6015 PVGASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVGASTATE);
6016 PVGASTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVGASTATECC);
6017 PVMSVGAR3STATE pSVGAState;
6018 int rc;
6019
6020 pThis->svga.cScratchRegion = VMSVGA_SCRATCH_SIZE;
6021 memset(pThis->svga.au32ScratchRegion, 0, sizeof(pThis->svga.au32ScratchRegion));
6022
6023 pThis->svga.cGMR = VMSVGA_MAX_GMR_IDS;
6024
6025 /* Necessary for creating a backup of the text mode frame buffer when switching into svga mode. */
6026 pThisCC->svga.pbVgaFrameBufferR3 = (uint8_t *)RTMemAllocZ(VMSVGA_VGA_FB_BACKUP_SIZE);
6027 AssertReturn(pThisCC->svga.pbVgaFrameBufferR3, VERR_NO_MEMORY);
6028
6029 /* Create event semaphore. */
6030 rc = PDMDevHlpSUPSemEventCreate(pDevIns, &pThis->svga.hFIFORequestSem);
6031 AssertRCReturn(rc, rc);
6032
6033 /* Create event semaphore. */
6034 rc = RTSemEventCreate(&pThisCC->svga.hFIFOExtCmdSem);
6035 AssertRCReturn(rc, rc);
6036
6037 pThisCC->svga.pSvgaR3State = (PVMSVGAR3STATE)RTMemAllocZ(sizeof(VMSVGAR3STATE));
6038 AssertReturn(pThisCC->svga.pSvgaR3State, VERR_NO_MEMORY);
6039
6040 rc = vmsvgaR3StateInit(pDevIns, pThis, pThisCC->svga.pSvgaR3State);
6041 AssertMsgRCReturn(rc, ("Failed to create pSvgaR3State.\n"), rc);
6042
6043 pSVGAState = pThisCC->svga.pSvgaR3State;
6044
6045 /* Register the write-protected GBO access handler type. */
6046 rc = PGMR3HandlerPhysicalTypeRegister(PDMDevHlpGetVM(pDevIns), PGMPHYSHANDLERKIND_WRITE,
6047 vmsvgaR3GboAccessHandler,
6048 NULL, NULL, NULL,
6049 NULL, NULL, NULL,
6050 "VMSVGA GBO", &pSVGAState->hGboAccessHandlerType);
6051 AssertRCReturn(rc, rc);
6052
6053# ifdef VBOX_WITH_VMSVGA3D
6054 if (pThis->svga.f3DEnabled)
6055 {
6056 rc = vmsvga3dInit(pDevIns, pThis, pThisCC);
6057 if (RT_SUCCESS(rc))
6058 {
6059 /* Device capabilities depend on this. */
6060 vmsvgaR3Init3dInterfaces(pThisCC);
6061 }
6062 else
6063 {
6064 LogRel(("VMSVGA3d: 3D support disabled! (vmsvga3dInit -> %Rrc)\n", rc));
6065 pThis->svga.f3DEnabled = false;
6066 }
6067 }
6068# endif
6069
6070 /* Initialize FIFO and register capabilities. */
6071 vmsvgaR3InitCaps(pThis, pThisCC);
6072
6073 /* VRAM tracking is enabled by default during bootup. */
6074 pThis->svga.fVRAMTracking = true;
6075
6076 /* Set up the host bpp. This value is as a default for the programmable
6077 * bpp value. On old implementations, SVGA_REG_HOST_BITS_PER_PIXEL did not
6078 * exist and SVGA_REG_BITS_PER_PIXEL was read-only, returning what was later
6079 * separated as SVGA_REG_HOST_BITS_PER_PIXEL.
6080 *
6081 * NB: The driver cBits value is currently constant for the lifetime of the
6082 * VM. If that changes, the host bpp logic might need revisiting.
6083 */
6084 pThis->svga.uHostBpp = (pThisCC->pDrv->cBits + 7) & ~7;
6085
6086 /* Invalidate current settings. */
6087 pThis->svga.uWidth = VMSVGA_VAL_UNINITIALIZED;
6088 pThis->svga.uHeight = VMSVGA_VAL_UNINITIALIZED;
6089 pThis->svga.uBpp = pThis->svga.uHostBpp;
6090 pThis->svga.cbScanline = 0;
6091
6092 pThis->svga.u32MaxWidth = VBE_DISPI_MAX_YRES;
6093 pThis->svga.u32MaxHeight = VBE_DISPI_MAX_XRES;
6094 while (pThis->svga.u32MaxWidth * pThis->svga.u32MaxHeight * 4 /* 32 bpp */ > pThis->vram_size)
6095 {
6096 pThis->svga.u32MaxWidth -= 256;
6097 pThis->svga.u32MaxHeight -= 256;
6098 }
6099 Log(("VMSVGA: Maximum size (%d,%d)\n", pThis->svga.u32MaxWidth, pThis->svga.u32MaxHeight));
6100
6101# ifdef DEBUG_GMR_ACCESS
6102 /* Register the GMR access handler type. */
6103 rc = PGMR3HandlerPhysicalTypeRegister(PDMDevHlpGetVM(pDevIns), PGMPHYSHANDLERKIND_WRITE,
6104 vmsvgaR3GmrAccessHandler,
6105 NULL, NULL, NULL,
6106 NULL, NULL, NULL,
6107 "VMSVGA GMR", &pThis->svga.hGmrAccessHandlerType);
6108 AssertRCReturn(rc, rc);
6109# endif
6110
6111# if defined(VMSVGA_USE_FIFO_ACCESS_HANDLER) || defined(DEBUG_FIFO_ACCESS)
6112 /* Register the FIFO access handler type. In addition to
6113 debugging FIFO access, this is also used to facilitate
6114 extended fifo thread sleeps. */
6115 rc = PGMR3HandlerPhysicalTypeRegister(PDMDevHlpGetVM(pDevIns),
6116# ifdef DEBUG_FIFO_ACCESS
6117 PGMPHYSHANDLERKIND_ALL,
6118# else
6119 PGMPHYSHANDLERKIND_WRITE,
6120# endif
6121 vmsvgaR3FifoAccessHandler,
6122 NULL, NULL, NULL,
6123 NULL, NULL, NULL,
6124 "VMSVGA FIFO", &pThis->svga.hFifoAccessHandlerType);
6125 AssertRCReturn(rc, rc);
6126# endif
6127
6128 /* Create the async IO thread. */
6129 rc = PDMDevHlpThreadCreate(pDevIns, &pThisCC->svga.pFIFOIOThread, pThis, vmsvgaR3FifoLoop, vmsvgaR3FifoLoopWakeUp, 0,
6130 RTTHREADTYPE_IO, "VMSVGA FIFO");
6131 if (RT_FAILURE(rc))
6132 {
6133 AssertMsgFailed(("%s: Async IO Thread creation for FIFO handling failed rc=%d\n", __FUNCTION__, rc));
6134 return rc;
6135 }
6136
6137 /*
6138 * Statistics.
6139 */
6140# define REG_CNT(a_pvSample, a_pszName, a_pszDesc) \
6141 PDMDevHlpSTAMRegister(pDevIns, (a_pvSample), STAMTYPE_COUNTER, a_pszName, STAMUNIT_OCCURENCES, a_pszDesc)
6142# define REG_PRF(a_pvSample, a_pszName, a_pszDesc) \
6143 PDMDevHlpSTAMRegister(pDevIns, (a_pvSample), STAMTYPE_PROFILE, a_pszName, STAMUNIT_TICKS_PER_CALL, a_pszDesc)
6144# ifdef VBOX_WITH_STATISTICS
6145 REG_PRF(&pSVGAState->StatR3Cmd3dDrawPrimitivesProf, "VMSVGA/Cmd/3dDrawPrimitivesProf", "Profiling of SVGA_3D_CMD_DRAW_PRIMITIVES.");
6146 REG_PRF(&pSVGAState->StatR3Cmd3dPresentProf, "VMSVGA/Cmd/3dPresentProfBoth", "Profiling of SVGA_3D_CMD_PRESENT and SVGA_3D_CMD_PRESENT_READBACK.");
6147 REG_PRF(&pSVGAState->StatR3Cmd3dSurfaceDmaProf, "VMSVGA/Cmd/3dSurfaceDmaProf", "Profiling of SVGA_3D_CMD_SURFACE_DMA.");
6148# endif
6149 REG_PRF(&pSVGAState->StatR3Cmd3dBlitSurfaceToScreenProf, "VMSVGA/Cmd/3dBlitSurfaceToScreenProf", "Profiling of SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN.");
6150 REG_CNT(&pSVGAState->StatR3Cmd3dActivateSurface, "VMSVGA/Cmd/3dActivateSurface", "SVGA_3D_CMD_ACTIVATE_SURFACE");
6151 REG_CNT(&pSVGAState->StatR3Cmd3dBeginQuery, "VMSVGA/Cmd/3dBeginQuery", "SVGA_3D_CMD_BEGIN_QUERY");
6152 REG_CNT(&pSVGAState->StatR3Cmd3dClear, "VMSVGA/Cmd/3dClear", "SVGA_3D_CMD_CLEAR");
6153 REG_CNT(&pSVGAState->StatR3Cmd3dContextDefine, "VMSVGA/Cmd/3dContextDefine", "SVGA_3D_CMD_CONTEXT_DEFINE");
6154 REG_CNT(&pSVGAState->StatR3Cmd3dContextDestroy, "VMSVGA/Cmd/3dContextDestroy", "SVGA_3D_CMD_CONTEXT_DESTROY");
6155 REG_CNT(&pSVGAState->StatR3Cmd3dDeactivateSurface, "VMSVGA/Cmd/3dDeactivateSurface", "SVGA_3D_CMD_DEACTIVATE_SURFACE");
6156 REG_CNT(&pSVGAState->StatR3Cmd3dDrawPrimitives, "VMSVGA/Cmd/3dDrawPrimitives", "SVGA_3D_CMD_DRAW_PRIMITIVES");
6157 REG_CNT(&pSVGAState->StatR3Cmd3dEndQuery, "VMSVGA/Cmd/3dEndQuery", "SVGA_3D_CMD_END_QUERY");
6158 REG_CNT(&pSVGAState->StatR3Cmd3dGenerateMipmaps, "VMSVGA/Cmd/3dGenerateMipmaps", "SVGA_3D_CMD_GENERATE_MIPMAPS");
6159 REG_CNT(&pSVGAState->StatR3Cmd3dPresent, "VMSVGA/Cmd/3dPresent", "SVGA_3D_CMD_PRESENT");
6160 REG_CNT(&pSVGAState->StatR3Cmd3dPresentReadBack, "VMSVGA/Cmd/3dPresentReadBack", "SVGA_3D_CMD_PRESENT_READBACK");
6161 REG_CNT(&pSVGAState->StatR3Cmd3dSetClipPlane, "VMSVGA/Cmd/3dSetClipPlane", "SVGA_3D_CMD_SETCLIPPLANE");
6162 REG_CNT(&pSVGAState->StatR3Cmd3dSetLightData, "VMSVGA/Cmd/3dSetLightData", "SVGA_3D_CMD_SETLIGHTDATA");
6163 REG_CNT(&pSVGAState->StatR3Cmd3dSetLightEnable, "VMSVGA/Cmd/3dSetLightEnable", "SVGA_3D_CMD_SETLIGHTENABLE");
6164 REG_CNT(&pSVGAState->StatR3Cmd3dSetMaterial, "VMSVGA/Cmd/3dSetMaterial", "SVGA_3D_CMD_SETMATERIAL");
6165 REG_CNT(&pSVGAState->StatR3Cmd3dSetRenderState, "VMSVGA/Cmd/3dSetRenderState", "SVGA_3D_CMD_SETRENDERSTATE");
6166 REG_CNT(&pSVGAState->StatR3Cmd3dSetRenderTarget, "VMSVGA/Cmd/3dSetRenderTarget", "SVGA_3D_CMD_SETRENDERTARGET");
6167 REG_CNT(&pSVGAState->StatR3Cmd3dSetScissorRect, "VMSVGA/Cmd/3dSetScissorRect", "SVGA_3D_CMD_SETSCISSORRECT");
6168 REG_CNT(&pSVGAState->StatR3Cmd3dSetShader, "VMSVGA/Cmd/3dSetShader", "SVGA_3D_CMD_SET_SHADER");
6169 REG_CNT(&pSVGAState->StatR3Cmd3dSetShaderConst, "VMSVGA/Cmd/3dSetShaderConst", "SVGA_3D_CMD_SET_SHADER_CONST");
6170 REG_CNT(&pSVGAState->StatR3Cmd3dSetTextureState, "VMSVGA/Cmd/3dSetTextureState", "SVGA_3D_CMD_SETTEXTURESTATE");
6171 REG_CNT(&pSVGAState->StatR3Cmd3dSetTransform, "VMSVGA/Cmd/3dSetTransform", "SVGA_3D_CMD_SETTRANSFORM");
6172 REG_CNT(&pSVGAState->StatR3Cmd3dSetViewPort, "VMSVGA/Cmd/3dSetViewPort", "SVGA_3D_CMD_SETVIEWPORT");
6173 REG_CNT(&pSVGAState->StatR3Cmd3dSetZRange, "VMSVGA/Cmd/3dSetZRange", "SVGA_3D_CMD_SETZRANGE");
6174 REG_CNT(&pSVGAState->StatR3Cmd3dShaderDefine, "VMSVGA/Cmd/3dShaderDefine", "SVGA_3D_CMD_SHADER_DEFINE");
6175 REG_CNT(&pSVGAState->StatR3Cmd3dShaderDestroy, "VMSVGA/Cmd/3dShaderDestroy", "SVGA_3D_CMD_SHADER_DESTROY");
6176 REG_CNT(&pSVGAState->StatR3Cmd3dSurfaceCopy, "VMSVGA/Cmd/3dSurfaceCopy", "SVGA_3D_CMD_SURFACE_COPY");
6177 REG_CNT(&pSVGAState->StatR3Cmd3dSurfaceDefine, "VMSVGA/Cmd/3dSurfaceDefine", "SVGA_3D_CMD_SURFACE_DEFINE");
6178 REG_CNT(&pSVGAState->StatR3Cmd3dSurfaceDefineV2, "VMSVGA/Cmd/3dSurfaceDefineV2", "SVGA_3D_CMD_SURFACE_DEFINE_V2");
6179 REG_CNT(&pSVGAState->StatR3Cmd3dSurfaceDestroy, "VMSVGA/Cmd/3dSurfaceDestroy", "SVGA_3D_CMD_SURFACE_DESTROY");
6180 REG_CNT(&pSVGAState->StatR3Cmd3dSurfaceDma, "VMSVGA/Cmd/3dSurfaceDma", "SVGA_3D_CMD_SURFACE_DMA");
6181 REG_CNT(&pSVGAState->StatR3Cmd3dSurfaceScreen, "VMSVGA/Cmd/3dSurfaceScreen", "SVGA_3D_CMD_SURFACE_SCREEN");
6182 REG_CNT(&pSVGAState->StatR3Cmd3dSurfaceStretchBlt, "VMSVGA/Cmd/3dSurfaceStretchBlt", "SVGA_3D_CMD_SURFACE_STRETCHBLT");
6183 REG_CNT(&pSVGAState->StatR3Cmd3dWaitForQuery, "VMSVGA/Cmd/3dWaitForQuery", "SVGA_3D_CMD_WAIT_FOR_QUERY");
6184 REG_CNT(&pSVGAState->StatR3CmdAnnotationCopy, "VMSVGA/Cmd/AnnotationCopy", "SVGA_CMD_ANNOTATION_COPY");
6185 REG_CNT(&pSVGAState->StatR3CmdAnnotationFill, "VMSVGA/Cmd/AnnotationFill", "SVGA_CMD_ANNOTATION_FILL");
6186 REG_CNT(&pSVGAState->StatR3CmdBlitGmrFbToScreen, "VMSVGA/Cmd/BlitGmrFbToScreen", "SVGA_CMD_BLIT_GMRFB_TO_SCREEN");
6187 REG_CNT(&pSVGAState->StatR3CmdBlitScreentoGmrFb, "VMSVGA/Cmd/BlitScreentoGmrFb", "SVGA_CMD_BLIT_SCREEN_TO_GMRFB");
6188 REG_CNT(&pSVGAState->StatR3CmdDefineAlphaCursor, "VMSVGA/Cmd/DefineAlphaCursor", "SVGA_CMD_DEFINE_ALPHA_CURSOR");
6189 REG_CNT(&pSVGAState->StatR3CmdDefineCursor, "VMSVGA/Cmd/DefineCursor", "SVGA_CMD_DEFINE_CURSOR");
6190 REG_CNT(&pSVGAState->StatR3CmdMoveCursor, "VMSVGA/Cmd/MoveCursor", "SVGA_CMD_MOVE_CURSOR");
6191 REG_CNT(&pSVGAState->StatR3CmdDisplayCursor, "VMSVGA/Cmd/DisplayCursor", "SVGA_CMD_DISPLAY_CURSOR");
6192 REG_CNT(&pSVGAState->StatR3CmdRectFill, "VMSVGA/Cmd/RectFill", "SVGA_CMD_RECT_FILL");
6193 REG_CNT(&pSVGAState->StatR3CmdRectCopy, "VMSVGA/Cmd/RectCopy", "SVGA_CMD_RECT_COPY");
6194 REG_CNT(&pSVGAState->StatR3CmdRectRopCopy, "VMSVGA/Cmd/RectRopCopy", "SVGA_CMD_RECT_ROP_COPY");
6195 REG_CNT(&pSVGAState->StatR3CmdDefineGmr2, "VMSVGA/Cmd/DefineGmr2", "SVGA_CMD_DEFINE_GMR2");
6196 REG_CNT(&pSVGAState->StatR3CmdDefineGmr2Free, "VMSVGA/Cmd/DefineGmr2/Free", "Number of SVGA_CMD_DEFINE_GMR2 commands that only frees.");
6197 REG_CNT(&pSVGAState->StatR3CmdDefineGmr2Modify, "VMSVGA/Cmd/DefineGmr2/Modify", "Number of SVGA_CMD_DEFINE_GMR2 commands that redefines a non-free GMR.");
6198 REG_CNT(&pSVGAState->StatR3CmdDefineGmrFb, "VMSVGA/Cmd/DefineGmrFb", "SVGA_CMD_DEFINE_GMRFB");
6199 REG_CNT(&pSVGAState->StatR3CmdDefineScreen, "VMSVGA/Cmd/DefineScreen", "SVGA_CMD_DEFINE_SCREEN");
6200 REG_CNT(&pSVGAState->StatR3CmdDestroyScreen, "VMSVGA/Cmd/DestroyScreen", "SVGA_CMD_DESTROY_SCREEN");
6201 REG_CNT(&pSVGAState->StatR3CmdEscape, "VMSVGA/Cmd/Escape", "SVGA_CMD_ESCAPE");
6202 REG_CNT(&pSVGAState->StatR3CmdFence, "VMSVGA/Cmd/Fence", "SVGA_CMD_FENCE");
6203 REG_CNT(&pSVGAState->StatR3CmdInvalidCmd, "VMSVGA/Cmd/InvalidCmd", "SVGA_CMD_INVALID_CMD");
6204 REG_CNT(&pSVGAState->StatR3CmdRemapGmr2, "VMSVGA/Cmd/RemapGmr2", "SVGA_CMD_REMAP_GMR2");
6205 REG_CNT(&pSVGAState->StatR3CmdRemapGmr2Modify, "VMSVGA/Cmd/RemapGmr2/Modify", "Number of SVGA_CMD_REMAP_GMR2 commands that modifies rather than complete the definition of a GMR.");
6206 REG_CNT(&pSVGAState->StatR3CmdUpdate, "VMSVGA/Cmd/Update", "SVGA_CMD_UPDATE");
6207 REG_CNT(&pSVGAState->StatR3CmdUpdateVerbose, "VMSVGA/Cmd/UpdateVerbose", "SVGA_CMD_UPDATE_VERBOSE");
6208
6209 REG_CNT(&pSVGAState->StatR3RegConfigDoneWr, "VMSVGA/Reg/ConfigDoneWrite", "SVGA_REG_CONFIG_DONE writes");
6210 REG_CNT(&pSVGAState->StatR3RegGmrDescriptorWr, "VMSVGA/Reg/GmrDescriptorWrite", "SVGA_REG_GMR_DESCRIPTOR writes");
6211 REG_CNT(&pSVGAState->StatR3RegGmrDescriptorWrErrors, "VMSVGA/Reg/GmrDescriptorWrite/Errors", "Number of erroneous SVGA_REG_GMR_DESCRIPTOR commands.");
6212 REG_CNT(&pSVGAState->StatR3RegGmrDescriptorWrFree, "VMSVGA/Reg/GmrDescriptorWrite/Free", "Number of SVGA_REG_GMR_DESCRIPTOR commands only freeing the GMR.");
6213 REG_CNT(&pThis->svga.StatRegBitsPerPixelWr, "VMSVGA/Reg/BitsPerPixelWrite", "SVGA_REG_BITS_PER_PIXEL writes.");
6214 REG_CNT(&pThis->svga.StatRegBusyWr, "VMSVGA/Reg/BusyWrite", "SVGA_REG_BUSY writes.");
6215 REG_CNT(&pThis->svga.StatRegCursorXWr, "VMSVGA/Reg/CursorXWrite", "SVGA_REG_CURSOR_X writes.");
6216 REG_CNT(&pThis->svga.StatRegCursorYWr, "VMSVGA/Reg/CursorYWrite", "SVGA_REG_CURSOR_Y writes.");
6217 REG_CNT(&pThis->svga.StatRegCursorIdWr, "VMSVGA/Reg/CursorIdWrite", "SVGA_REG_CURSOR_ID writes.");
6218 REG_CNT(&pThis->svga.StatRegCursorOnWr, "VMSVGA/Reg/CursorOnWrite", "SVGA_REG_CURSOR_ON writes.");
6219 REG_CNT(&pThis->svga.StatRegDepthWr, "VMSVGA/Reg/DepthWrite", "SVGA_REG_DEPTH writes.");
6220 REG_CNT(&pThis->svga.StatRegDisplayHeightWr, "VMSVGA/Reg/DisplayHeightWrite", "SVGA_REG_DISPLAY_HEIGHT writes.");
6221 REG_CNT(&pThis->svga.StatRegDisplayIdWr, "VMSVGA/Reg/DisplayIdWrite", "SVGA_REG_DISPLAY_ID writes.");
6222 REG_CNT(&pThis->svga.StatRegDisplayIsPrimaryWr, "VMSVGA/Reg/DisplayIsPrimaryWrite", "SVGA_REG_DISPLAY_IS_PRIMARY writes.");
6223 REG_CNT(&pThis->svga.StatRegDisplayPositionXWr, "VMSVGA/Reg/DisplayPositionXWrite", "SVGA_REG_DISPLAY_POSITION_X writes.");
6224 REG_CNT(&pThis->svga.StatRegDisplayPositionYWr, "VMSVGA/Reg/DisplayPositionYWrite", "SVGA_REG_DISPLAY_POSITION_Y writes.");
6225 REG_CNT(&pThis->svga.StatRegDisplayWidthWr, "VMSVGA/Reg/DisplayWidthWrite", "SVGA_REG_DISPLAY_WIDTH writes.");
6226 REG_CNT(&pThis->svga.StatRegEnableWr, "VMSVGA/Reg/EnableWrite", "SVGA_REG_ENABLE writes.");
6227 REG_CNT(&pThis->svga.StatRegGmrIdWr, "VMSVGA/Reg/GmrIdWrite", "SVGA_REG_GMR_ID writes.");
6228 REG_CNT(&pThis->svga.StatRegGuestIdWr, "VMSVGA/Reg/GuestIdWrite", "SVGA_REG_GUEST_ID writes.");
6229 REG_CNT(&pThis->svga.StatRegHeightWr, "VMSVGA/Reg/HeightWrite", "SVGA_REG_HEIGHT writes.");
6230 REG_CNT(&pThis->svga.StatRegIdWr, "VMSVGA/Reg/IdWrite", "SVGA_REG_ID writes.");
6231 REG_CNT(&pThis->svga.StatRegIrqMaskWr, "VMSVGA/Reg/IrqMaskWrite", "SVGA_REG_IRQMASK writes.");
6232 REG_CNT(&pThis->svga.StatRegNumDisplaysWr, "VMSVGA/Reg/NumDisplaysWrite", "SVGA_REG_NUM_DISPLAYS writes.");
6233 REG_CNT(&pThis->svga.StatRegNumGuestDisplaysWr, "VMSVGA/Reg/NumGuestDisplaysWrite", "SVGA_REG_NUM_GUEST_DISPLAYS writes.");
6234 REG_CNT(&pThis->svga.StatRegPaletteWr, "VMSVGA/Reg/PaletteWrite", "SVGA_PALETTE_XXXX writes.");
6235 REG_CNT(&pThis->svga.StatRegPitchLockWr, "VMSVGA/Reg/PitchLockWrite", "SVGA_REG_PITCHLOCK writes.");
6236 REG_CNT(&pThis->svga.StatRegPseudoColorWr, "VMSVGA/Reg/PseudoColorWrite", "SVGA_REG_PSEUDOCOLOR writes.");
6237 REG_CNT(&pThis->svga.StatRegReadOnlyWr, "VMSVGA/Reg/ReadOnlyWrite", "Read-only SVGA_REG_XXXX writes.");
6238 REG_CNT(&pThis->svga.StatRegScratchWr, "VMSVGA/Reg/ScratchWrite", "SVGA_REG_SCRATCH_XXXX writes.");
6239 REG_CNT(&pThis->svga.StatRegSyncWr, "VMSVGA/Reg/SyncWrite", "SVGA_REG_SYNC writes.");
6240 REG_CNT(&pThis->svga.StatRegTopWr, "VMSVGA/Reg/TopWrite", "SVGA_REG_TOP writes.");
6241 REG_CNT(&pThis->svga.StatRegTracesWr, "VMSVGA/Reg/TracesWrite", "SVGA_REG_TRACES writes.");
6242 REG_CNT(&pThis->svga.StatRegUnknownWr, "VMSVGA/Reg/UnknownWrite", "Writes to unknown register.");
6243 REG_CNT(&pThis->svga.StatRegWidthWr, "VMSVGA/Reg/WidthWrite", "SVGA_REG_WIDTH writes.");
6244 REG_CNT(&pThis->svga.StatRegCommandLowWr, "VMSVGA/Reg/CommandLowWrite", "SVGA_REG_COMMAND_LOW writes.");
6245 REG_CNT(&pThis->svga.StatRegCommandHighWr, "VMSVGA/Reg/CommandHighWrite", "SVGA_REG_COMMAND_HIGH writes.");
6246 REG_CNT(&pThis->svga.StatRegDevCapWr, "VMSVGA/Reg/DevCapWrite", "SVGA_REG_DEV_CAP writes.");
6247 REG_CNT(&pThis->svga.StatRegCmdPrependLowWr, "VMSVGA/Reg/CmdPrependLowWrite", "SVGA_REG_CMD_PREPEND_LOW writes.");
6248 REG_CNT(&pThis->svga.StatRegCmdPrependHighWr, "VMSVGA/Reg/CmdPrependHighWrite", "SVGA_REG_iCMD_PREPEND_HIGH writes.");
6249
6250 REG_CNT(&pThis->svga.StatRegBitsPerPixelRd, "VMSVGA/Reg/BitsPerPixelRead", "SVGA_REG_BITS_PER_PIXEL reads.");
6251 REG_CNT(&pThis->svga.StatRegBlueMaskRd, "VMSVGA/Reg/BlueMaskRead", "SVGA_REG_BLUE_MASK reads.");
6252 REG_CNT(&pThis->svga.StatRegBusyRd, "VMSVGA/Reg/BusyRead", "SVGA_REG_BUSY reads.");
6253 REG_CNT(&pThis->svga.StatRegBytesPerLineRd, "VMSVGA/Reg/BytesPerLineRead", "SVGA_REG_BYTES_PER_LINE reads.");
6254 REG_CNT(&pThis->svga.StatRegCapabilitesRd, "VMSVGA/Reg/CapabilitesRead", "SVGA_REG_CAPABILITIES reads.");
6255 REG_CNT(&pThis->svga.StatRegConfigDoneRd, "VMSVGA/Reg/ConfigDoneRead", "SVGA_REG_CONFIG_DONE reads.");
6256 REG_CNT(&pThis->svga.StatRegCursorXRd, "VMSVGA/Reg/CursorXRead", "SVGA_REG_CURSOR_X reads.");
6257 REG_CNT(&pThis->svga.StatRegCursorYRd, "VMSVGA/Reg/CursorYRead", "SVGA_REG_CURSOR_Y reads.");
6258 REG_CNT(&pThis->svga.StatRegCursorIdRd, "VMSVGA/Reg/CursorIdRead", "SVGA_REG_CURSOR_ID reads.");
6259 REG_CNT(&pThis->svga.StatRegCursorOnRd, "VMSVGA/Reg/CursorOnRead", "SVGA_REG_CURSOR_ON reads.");
6260 REG_CNT(&pThis->svga.StatRegDepthRd, "VMSVGA/Reg/DepthRead", "SVGA_REG_DEPTH reads.");
6261 REG_CNT(&pThis->svga.StatRegDisplayHeightRd, "VMSVGA/Reg/DisplayHeightRead", "SVGA_REG_DISPLAY_HEIGHT reads.");
6262 REG_CNT(&pThis->svga.StatRegDisplayIdRd, "VMSVGA/Reg/DisplayIdRead", "SVGA_REG_DISPLAY_ID reads.");
6263 REG_CNT(&pThis->svga.StatRegDisplayIsPrimaryRd, "VMSVGA/Reg/DisplayIsPrimaryRead", "SVGA_REG_DISPLAY_IS_PRIMARY reads.");
6264 REG_CNT(&pThis->svga.StatRegDisplayPositionXRd, "VMSVGA/Reg/DisplayPositionXRead", "SVGA_REG_DISPLAY_POSITION_X reads.");
6265 REG_CNT(&pThis->svga.StatRegDisplayPositionYRd, "VMSVGA/Reg/DisplayPositionYRead", "SVGA_REG_DISPLAY_POSITION_Y reads.");
6266 REG_CNT(&pThis->svga.StatRegDisplayWidthRd, "VMSVGA/Reg/DisplayWidthRead", "SVGA_REG_DISPLAY_WIDTH reads.");
6267 REG_CNT(&pThis->svga.StatRegEnableRd, "VMSVGA/Reg/EnableRead", "SVGA_REG_ENABLE reads.");
6268 REG_CNT(&pThis->svga.StatRegFbOffsetRd, "VMSVGA/Reg/FbOffsetRead", "SVGA_REG_FB_OFFSET reads.");
6269 REG_CNT(&pThis->svga.StatRegFbSizeRd, "VMSVGA/Reg/FbSizeRead", "SVGA_REG_FB_SIZE reads.");
6270 REG_CNT(&pThis->svga.StatRegFbStartRd, "VMSVGA/Reg/FbStartRead", "SVGA_REG_FB_START reads.");
6271 REG_CNT(&pThis->svga.StatRegGmrIdRd, "VMSVGA/Reg/GmrIdRead", "SVGA_REG_GMR_ID reads.");
6272 REG_CNT(&pThis->svga.StatRegGmrMaxDescriptorLengthRd, "VMSVGA/Reg/GmrMaxDescriptorLengthRead", "SVGA_REG_GMR_MAX_DESCRIPTOR_LENGTH reads.");
6273 REG_CNT(&pThis->svga.StatRegGmrMaxIdsRd, "VMSVGA/Reg/GmrMaxIdsRead", "SVGA_REG_GMR_MAX_IDS reads.");
6274 REG_CNT(&pThis->svga.StatRegGmrsMaxPagesRd, "VMSVGA/Reg/GmrsMaxPagesRead", "SVGA_REG_GMRS_MAX_PAGES reads.");
6275 REG_CNT(&pThis->svga.StatRegGreenMaskRd, "VMSVGA/Reg/GreenMaskRead", "SVGA_REG_GREEN_MASK reads.");
6276 REG_CNT(&pThis->svga.StatRegGuestIdRd, "VMSVGA/Reg/GuestIdRead", "SVGA_REG_GUEST_ID reads.");
6277 REG_CNT(&pThis->svga.StatRegHeightRd, "VMSVGA/Reg/HeightRead", "SVGA_REG_HEIGHT reads.");
6278 REG_CNT(&pThis->svga.StatRegHostBitsPerPixelRd, "VMSVGA/Reg/HostBitsPerPixelRead", "SVGA_REG_HOST_BITS_PER_PIXEL reads.");
6279 REG_CNT(&pThis->svga.StatRegIdRd, "VMSVGA/Reg/IdRead", "SVGA_REG_ID reads.");
6280 REG_CNT(&pThis->svga.StatRegIrqMaskRd, "VMSVGA/Reg/IrqMaskRead", "SVGA_REG_IRQ_MASK reads.");
6281 REG_CNT(&pThis->svga.StatRegMaxHeightRd, "VMSVGA/Reg/MaxHeightRead", "SVGA_REG_MAX_HEIGHT reads.");
6282 REG_CNT(&pThis->svga.StatRegMaxWidthRd, "VMSVGA/Reg/MaxWidthRead", "SVGA_REG_MAX_WIDTH reads.");
6283 REG_CNT(&pThis->svga.StatRegMemorySizeRd, "VMSVGA/Reg/MemorySizeRead", "SVGA_REG_MEMORY_SIZE reads.");
6284 REG_CNT(&pThis->svga.StatRegMemRegsRd, "VMSVGA/Reg/MemRegsRead", "SVGA_REG_MEM_REGS reads.");
6285 REG_CNT(&pThis->svga.StatRegMemSizeRd, "VMSVGA/Reg/MemSizeRead", "SVGA_REG_MEM_SIZE reads.");
6286 REG_CNT(&pThis->svga.StatRegMemStartRd, "VMSVGA/Reg/MemStartRead", "SVGA_REG_MEM_START reads.");
6287 REG_CNT(&pThis->svga.StatRegNumDisplaysRd, "VMSVGA/Reg/NumDisplaysRead", "SVGA_REG_NUM_DISPLAYS reads.");
6288 REG_CNT(&pThis->svga.StatRegNumGuestDisplaysRd, "VMSVGA/Reg/NumGuestDisplaysRead", "SVGA_REG_NUM_GUEST_DISPLAYS reads.");
6289 REG_CNT(&pThis->svga.StatRegPaletteRd, "VMSVGA/Reg/PaletteRead", "SVGA_REG_PLAETTE_XXXX reads.");
6290 REG_CNT(&pThis->svga.StatRegPitchLockRd, "VMSVGA/Reg/PitchLockRead", "SVGA_REG_PITCHLOCK reads.");
6291 REG_CNT(&pThis->svga.StatRegPsuedoColorRd, "VMSVGA/Reg/PsuedoColorRead", "SVGA_REG_PSEUDOCOLOR reads.");
6292 REG_CNT(&pThis->svga.StatRegRedMaskRd, "VMSVGA/Reg/RedMaskRead", "SVGA_REG_RED_MASK reads.");
6293 REG_CNT(&pThis->svga.StatRegScratchRd, "VMSVGA/Reg/ScratchRead", "SVGA_REG_SCRATCH reads.");
6294 REG_CNT(&pThis->svga.StatRegScratchSizeRd, "VMSVGA/Reg/ScratchSizeRead", "SVGA_REG_SCRATCH_SIZE reads.");
6295 REG_CNT(&pThis->svga.StatRegSyncRd, "VMSVGA/Reg/SyncRead", "SVGA_REG_SYNC reads.");
6296 REG_CNT(&pThis->svga.StatRegTopRd, "VMSVGA/Reg/TopRead", "SVGA_REG_TOP reads.");
6297 REG_CNT(&pThis->svga.StatRegTracesRd, "VMSVGA/Reg/TracesRead", "SVGA_REG_TRACES reads.");
6298 REG_CNT(&pThis->svga.StatRegUnknownRd, "VMSVGA/Reg/UnknownRead", "SVGA_REG_UNKNOWN reads.");
6299 REG_CNT(&pThis->svga.StatRegVramSizeRd, "VMSVGA/Reg/VramSizeRead", "SVGA_REG_VRAM_SIZE reads.");
6300 REG_CNT(&pThis->svga.StatRegWidthRd, "VMSVGA/Reg/WidthRead", "SVGA_REG_WIDTH reads.");
6301 REG_CNT(&pThis->svga.StatRegWriteOnlyRd, "VMSVGA/Reg/WriteOnlyRead", "Write-only SVGA_REG_XXXX reads.");
6302 REG_CNT(&pThis->svga.StatRegCommandLowRd, "VMSVGA/Reg/CommandLowRead", "SVGA_REG_COMMAND_LOW reads.");
6303 REG_CNT(&pThis->svga.StatRegCommandHighRd, "VMSVGA/Reg/CommandHighRead", "SVGA_REG_COMMAND_HIGH reads.");
6304 REG_CNT(&pThis->svga.StatRegMaxPrimBBMemRd, "VMSVGA/Reg/MaxPrimBBMemRead", "SVGA_REG_MAX_PRIMARY_BOUNDING_BOX_MEM reads.");
6305 REG_CNT(&pThis->svga.StatRegGBMemSizeRd, "VMSVGA/Reg/GBMemSizeRead", "SVGA_REG_SUGGESTED_GBOBJECT_MEM_SIZE_KB reads.");
6306 REG_CNT(&pThis->svga.StatRegDevCapRd, "VMSVGA/Reg/DevCapRead", "SVGA_REG_DEV_CAP reads.");
6307 REG_CNT(&pThis->svga.StatRegCmdPrependLowRd, "VMSVGA/Reg/CmdPrependLowRead", "SVGA_REG_CMD_PREPEND_LOW reads.");
6308 REG_CNT(&pThis->svga.StatRegCmdPrependHighRd, "VMSVGA/Reg/CmdPrependHighRead", "SVGA_REG_iCMD_PREPEND_HIGH reads.");
6309 REG_CNT(&pThis->svga.StatRegScrnTgtMaxWidthRd, "VMSVGA/Reg/ScrnTgtMaxWidthRead", "SVGA_REG_SCREENTARGET_MAX_WIDTH reads.");
6310 REG_CNT(&pThis->svga.StatRegScrnTgtMaxHeightRd, "VMSVGA/Reg/ScrnTgtMaxHeightRead", "SVGA_REG_SCREENTARGET_MAX_HEIGHT reads.");
6311 REG_CNT(&pThis->svga.StatRegMobMaxSizeRd, "VMSVGA/Reg/MobMaxSizeRead", "SVGA_REG_MOB_MAX_SIZE reads.");
6312
6313 REG_PRF(&pSVGAState->StatBusyDelayEmts, "VMSVGA/EmtDelayOnBusyFifo", "Time we've delayed EMTs because of busy FIFO thread.");
6314 REG_CNT(&pSVGAState->StatFifoCommands, "VMSVGA/FifoCommands", "FIFO command counter.");
6315 REG_CNT(&pSVGAState->StatFifoErrors, "VMSVGA/FifoErrors", "FIFO error counter.");
6316 REG_CNT(&pSVGAState->StatFifoUnkCmds, "VMSVGA/FifoUnknownCommands", "FIFO unknown command counter.");
6317 REG_CNT(&pSVGAState->StatFifoTodoTimeout, "VMSVGA/FifoTodoTimeout", "Number of times we discovered pending work after a wait timeout.");
6318 REG_CNT(&pSVGAState->StatFifoTodoWoken, "VMSVGA/FifoTodoWoken", "Number of times we discovered pending work after being woken up.");
6319 REG_PRF(&pSVGAState->StatFifoStalls, "VMSVGA/FifoStalls", "Profiling of FIFO stalls (waiting for guest to finish copying data).");
6320 REG_PRF(&pSVGAState->StatFifoExtendedSleep, "VMSVGA/FifoExtendedSleep", "Profiling FIFO sleeps relying on the refresh timer and/or access handler.");
6321# if defined(VMSVGA_USE_FIFO_ACCESS_HANDLER) || defined(DEBUG_FIFO_ACCESS)
6322 REG_CNT(&pSVGAState->StatFifoAccessHandler, "VMSVGA/FifoAccessHandler", "Number of times the FIFO access handler triggered.");
6323# endif
6324 REG_CNT(&pSVGAState->StatFifoCursorFetchAgain, "VMSVGA/FifoCursorFetchAgain", "Times the cursor update counter changed while reading.");
6325 REG_CNT(&pSVGAState->StatFifoCursorNoChange, "VMSVGA/FifoCursorNoChange", "No cursor position change event though the update counter was modified.");
6326 REG_CNT(&pSVGAState->StatFifoCursorPosition, "VMSVGA/FifoCursorPosition", "Cursor position and visibility changes.");
6327 REG_CNT(&pSVGAState->StatFifoCursorVisiblity, "VMSVGA/FifoCursorVisiblity", "Cursor visibility changes.");
6328 REG_CNT(&pSVGAState->StatFifoWatchdogWakeUps, "VMSVGA/FifoWatchdogWakeUps", "Number of times the FIFO refresh poller/watchdog woke up the FIFO thread.");
6329
6330# undef REG_CNT
6331# undef REG_PRF
6332
6333 /*
6334 * Info handlers.
6335 */
6336 PDMDevHlpDBGFInfoRegister(pDevIns, "vmsvga", "Basic VMSVGA device state details", vmsvgaR3Info);
6337# ifdef VBOX_WITH_VMSVGA3D
6338 PDMDevHlpDBGFInfoRegister(pDevIns, "vmsvga3dctx", "VMSVGA 3d context details. Accepts 'terse'.", vmsvgaR3Info3dContext);
6339 PDMDevHlpDBGFInfoRegister(pDevIns, "vmsvga3dsfc",
6340 "VMSVGA 3d surface details. "
6341 "Accepts 'terse', 'invy', and one of 'tiny', 'medium', 'normal', 'big', 'huge', or 'gigantic'.",
6342 vmsvgaR3Info3dSurface);
6343 PDMDevHlpDBGFInfoRegister(pDevIns, "vmsvga3dsurf",
6344 "VMSVGA 3d surface details and bitmap: "
6345 "sid[>dir]",
6346 vmsvgaR3Info3dSurfaceBmp);
6347# endif
6348
6349 return VINF_SUCCESS;
6350}
6351
6352/**
6353 * Power On notification.
6354 *
6355 * @returns VBox status code.
6356 * @param pDevIns The device instance data.
6357 *
6358 * @remarks Caller enters the device critical section.
6359 */
6360DECLCALLBACK(void) vmsvgaR3PowerOn(PPDMDEVINS pDevIns)
6361{
6362# ifdef VBOX_WITH_VMSVGA3D
6363 PVGASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVGASTATE);
6364 PVGASTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVGASTATECC);
6365 if (pThis->svga.f3DEnabled)
6366 {
6367 int rc = vmsvga3dPowerOn(pDevIns, pThis, pThisCC);
6368 if (RT_SUCCESS(rc))
6369 {
6370 /* Initialize FIFO 3D capabilities. */
6371 vmsvgaR3InitFifo3DCaps(pThis, pThisCC);
6372 }
6373 }
6374# else /* !VBOX_WITH_VMSVGA3D */
6375 RT_NOREF(pDevIns);
6376# endif /* !VBOX_WITH_VMSVGA3D */
6377}
6378
6379/**
6380 * Power Off notification.
6381 *
6382 * @param pDevIns The device instance data.
6383 *
6384 * @remarks Caller enters the device critical section.
6385 */
6386DECLCALLBACK(void) vmsvgaR3PowerOff(PPDMDEVINS pDevIns)
6387{
6388 PVGASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVGASTATE);
6389 PVGASTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVGASTATECC);
6390
6391 /*
6392 * Notify the FIFO thread.
6393 */
6394 if (pThisCC->svga.pFIFOIOThread)
6395 {
6396 int rc = vmsvgaR3RunExtCmdOnFifoThread(pDevIns, pThis, pThisCC, VMSVGA_FIFO_EXTCMD_POWEROFF,
6397 NULL /*pvParam*/, 30000 /*ms*/);
6398 AssertLogRelRC(rc);
6399 }
6400}
6401
6402#endif /* IN_RING3 */
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