1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
/* $Id: DevVGA_VDMA.cpp $ */
/** @file
* Video DMA (VDMA) support.
*/
/*
* Copyright (C) 2006-2023 Oracle and/or its affiliates.
*
* This file is part of VirtualBox base platform packages, as
* available from https://www.virtualbox.org.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, in version 3 of the
* License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses>.
*
* SPDX-License-Identifier: GPL-3.0-only
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#define LOG_GROUP LOG_GROUP_DEV_VGA
#include <VBox/VMMDev.h>
#include <VBox/vmm/pdmdev.h>
#include <VBox/vmm/pgm.h>
#include <VBoxVideo.h>
#include <VBox/AssertGuest.h>
#include <iprt/semaphore.h>
#include <iprt/thread.h>
#include <iprt/mem.h>
#include <iprt/asm.h>
#include <iprt/list.h>
#include <iprt/param.h>
#include "DevVGA.h"
#include "HGSMI/SHGSMIHost.h"
#ifdef DEBUG_misha
# define VBOXVDBG_MEMCACHE_DISABLE
#endif
#ifndef VBOXVDBG_MEMCACHE_DISABLE
# include <iprt/memcache.h>
#endif
/*********************************************************************************************************************************
* Defined Constants And Macros *
*********************************************************************************************************************************/
#ifdef DEBUG_misha
# define WARN_BP() do { AssertFailed(); } while (0)
#else
# define WARN_BP() do { } while (0)
#endif
#define WARN(_msg) do { \
LogRel(_msg); \
WARN_BP(); \
} while (0)
#define VBOXVDMATHREAD_STATE_TERMINATED 0
#define VBOXVDMATHREAD_STATE_CREATING 1
#define VBOXVDMATHREAD_STATE_CREATED 3
#define VBOXVDMATHREAD_STATE_TERMINATING 4
/*********************************************************************************************************************************
* Structures and Typedefs *
*********************************************************************************************************************************/
struct VBOXVDMATHREAD;
typedef DECLCALLBACKPTR(void, PFNVBOXVDMATHREAD_CHANGED,(struct VBOXVDMATHREAD *pThread, int rc,
void *pvThreadContext, void *pvChangeContext));
typedef struct VBOXVDMATHREAD
{
RTTHREAD hWorkerThread;
RTSEMEVENT hEvent;
volatile uint32_t u32State;
PFNVBOXVDMATHREAD_CHANGED pfnChanged;
void *pvChanged;
} VBOXVDMATHREAD, *PVBOXVDMATHREAD;
/* state transformations:
*
* submitter | processor
*
* LISTENING ---> PROCESSING
*
* */
#define VBVAEXHOSTCONTEXT_STATE_LISTENING 0
#define VBVAEXHOSTCONTEXT_STATE_PROCESSING 1
#define VBVAEXHOSTCONTEXT_ESTATE_DISABLED -1
#define VBVAEXHOSTCONTEXT_ESTATE_PAUSED 0
#define VBVAEXHOSTCONTEXT_ESTATE_ENABLED 1
typedef struct VBVAEXHOSTCONTEXT
{
VBVABUFFER RT_UNTRUSTED_VOLATILE_GUEST *pVBVA;
/** Maximum number of data bytes addressible relative to pVBVA. */
uint32_t cbMaxData;
volatile int32_t i32State;
volatile int32_t i32EnableState;
volatile uint32_t u32cCtls;
/* critical section for accessing ctl lists */
RTCRITSECT CltCritSect;
RTLISTANCHOR GuestCtlList;
RTLISTANCHOR HostCtlList;
#ifndef VBOXVDBG_MEMCACHE_DISABLE
RTMEMCACHE CtlCache;
#endif
} VBVAEXHOSTCONTEXT;
typedef enum
{
VBVAEXHOSTCTL_TYPE_UNDEFINED = 0,
VBVAEXHOSTCTL_TYPE_HH_INTERNAL_PAUSE,
VBVAEXHOSTCTL_TYPE_HH_INTERNAL_RESUME,
VBVAEXHOSTCTL_TYPE_HH_SAVESTATE,
VBVAEXHOSTCTL_TYPE_HH_LOADSTATE,
VBVAEXHOSTCTL_TYPE_HH_LOADSTATE_DONE,
VBVAEXHOSTCTL_TYPE_HH_BE_OPAQUE,
VBVAEXHOSTCTL_TYPE_HH_ON_HGCM_UNLOAD,
VBVAEXHOSTCTL_TYPE_GHH_BE_OPAQUE,
VBVAEXHOSTCTL_TYPE_GHH_ENABLE,
VBVAEXHOSTCTL_TYPE_GHH_ENABLE_PAUSED,
VBVAEXHOSTCTL_TYPE_GHH_DISABLE,
VBVAEXHOSTCTL_TYPE_GHH_RESIZE
} VBVAEXHOSTCTL_TYPE;
struct VBVAEXHOSTCTL;
typedef DECLCALLBACKTYPE(void, FNVBVAEXHOSTCTL_COMPLETE,(VBVAEXHOSTCONTEXT *pVbva, struct VBVAEXHOSTCTL *pCtl, int rc, void *pvComplete));
typedef FNVBVAEXHOSTCTL_COMPLETE *PFNVBVAEXHOSTCTL_COMPLETE;
typedef struct VBVAEXHOSTCTL
{
RTLISTNODE Node;
VBVAEXHOSTCTL_TYPE enmType;
union
{
struct
{
void RT_UNTRUSTED_VOLATILE_GUEST *pvCmd;
uint32_t cbCmd;
} cmd;
struct
{
PSSMHANDLE pSSM;
uint32_t u32Version;
} state;
} u;
PFNVBVAEXHOSTCTL_COMPLETE pfnComplete;
void *pvComplete;
} VBVAEXHOSTCTL;
/* VBoxVBVAExHP**, i.e. processor functions, can NOT be called concurrently with each other,
* but can be called with other VBoxVBVAExS** (submitter) functions except Init/Start/Term aparently.
* Can only be called be the processor, i.e. the entity that acquired the processor state by direct or indirect call to the VBoxVBVAExHSCheckCommands
* see mor edetailed comments in headers for function definitions */
typedef enum
{
VBVAEXHOST_DATA_TYPE_NO_DATA = 0,
VBVAEXHOST_DATA_TYPE_CMD,
VBVAEXHOST_DATA_TYPE_HOSTCTL,
VBVAEXHOST_DATA_TYPE_GUESTCTL
} VBVAEXHOST_DATA_TYPE;
typedef struct VBOXVDMAHOST
{
PHGSMIINSTANCE pHgsmi; /**< Same as VGASTATE::pHgsmi. */
PVGASTATE pThis;
} VBOXVDMAHOST, *PVBOXVDMAHOST;
/**
* List selector for VBoxVBVAExHCtlSubmit(), vdmaVBVACtlSubmit().
*/
typedef enum
{
VBVAEXHOSTCTL_SOURCE_GUEST = 0,
VBVAEXHOSTCTL_SOURCE_HOST
} VBVAEXHOSTCTL_SOURCE;
/**
* Called by vgaR3Construct() to initialize the state.
*
* @returns VBox status code.
*/
int vboxVDMAConstruct(PVGASTATE pThis, PVGASTATECC pThisCC, uint32_t cPipeElements)
{
RT_NOREF(cPipeElements);
PVBOXVDMAHOST pVdma = (PVBOXVDMAHOST)RTMemAllocZ(sizeof(*pVdma));
Assert(pVdma);
if (pVdma)
{
pVdma->pHgsmi = pThisCC->pHGSMI;
pVdma->pThis = pThis;
pThisCC->pVdma = pVdma;
return VINF_SUCCESS;
}
return VERR_NO_MEMORY;
}
/**
* Called by vgaR3Reset() to do reset.
*/
void vboxVDMAReset(struct VBOXVDMAHOST *pVdma)
{
RT_NOREF(pVdma);
}
/**
* Called by vgaR3Destruct() to do cleanup.
*/
void vboxVDMADestruct(struct VBOXVDMAHOST *pVdma)
{
if (!pVdma)
return;
RTMemFree(pVdma);
}
/**
* Handle VBVA_VDMA_CTL, see vbvaChannelHandler
*
* @param pVdma The VDMA channel.
* @param pCmd The control command to handle. Considered volatile.
* @param cbCmd The size of the command. At least sizeof(VBOXVDMA_CTL).
*/
void vboxVDMAControl(struct VBOXVDMAHOST *pVdma, VBOXVDMA_CTL RT_UNTRUSTED_VOLATILE_GUEST *pCmd, uint32_t cbCmd)
{
RT_NOREF(cbCmd);
PHGSMIINSTANCE pIns = pVdma->pHgsmi;
VBOXVDMA_CTL_TYPE enmCtl = pCmd->enmCtl;
RT_UNTRUSTED_NONVOLATILE_COPY_FENCE();
int rc;
if (enmCtl < VBOXVDMA_CTL_TYPE_END)
{
RT_UNTRUSTED_VALIDATED_FENCE();
switch (enmCtl)
{
case VBOXVDMA_CTL_TYPE_ENABLE:
rc = VINF_SUCCESS;
break;
case VBOXVDMA_CTL_TYPE_DISABLE:
rc = VINF_SUCCESS;
break;
case VBOXVDMA_CTL_TYPE_FLUSH:
rc = VINF_SUCCESS;
break;
case VBOXVDMA_CTL_TYPE_WATCHDOG:
rc = VERR_NOT_SUPPORTED;
break;
default:
AssertFailedBreakStmt(rc = VERR_IPE_NOT_REACHED_DEFAULT_CASE);
}
}
else
{
RT_UNTRUSTED_VALIDATED_FENCE();
ASSERT_GUEST_FAILED();
rc = VERR_NOT_SUPPORTED;
}
pCmd->i32Result = rc;
rc = VBoxSHGSMICommandComplete(pIns, pCmd);
AssertRC(rc);
}
/**
* Handle VBVA_VDMA_CMD, see vbvaChannelHandler().
*
* @param pVdma The VDMA channel.
* @param pCmd The command to handle. Considered volatile.
* @param cbCmd The size of the command. At least sizeof(VBOXVDMACBUF_DR).
* @thread EMT
*/
void vboxVDMACommand(struct VBOXVDMAHOST *pVdma, VBOXVDMACBUF_DR RT_UNTRUSTED_VOLATILE_GUEST *pCmd, uint32_t cbCmd)
{
/*
* Process the command.
*/
bool fAsyncCmd = false;
RT_NOREF(cbCmd);
int rc = VERR_NOT_IMPLEMENTED;
/*
* Complete the command unless it's asynchronous (e.g. chromium).
*/
if (!fAsyncCmd)
{
pCmd->rc = rc;
int rc2 = VBoxSHGSMICommandComplete(pVdma->pHgsmi, pCmd);
AssertRC(rc2);
}
}
/*
*
*
* Saved state.
* Saved state.
* Saved state.
*
*
*/
int vboxVDMASaveStateExecPrep(struct VBOXVDMAHOST *pVdma)
{
RT_NOREF(pVdma);
return VINF_SUCCESS;
}
int vboxVDMASaveStateExecDone(struct VBOXVDMAHOST *pVdma)
{
RT_NOREF(pVdma);
return VINF_SUCCESS;
}
int vboxVDMASaveStateExecPerform(PCPDMDEVHLPR3 pHlp, struct VBOXVDMAHOST *pVdma, PSSMHANDLE pSSM)
{
int rc;
RT_NOREF(pVdma);
rc = pHlp->pfnSSMPutU32(pSSM, UINT32_MAX);
AssertRCReturn(rc, rc);
return VINF_SUCCESS;
}
int vboxVDMASaveLoadExecPerform(PCPDMDEVHLPR3 pHlp, struct VBOXVDMAHOST *pVdma, PSSMHANDLE pSSM, uint32_t u32Version)
{
uint32_t u32;
int rc = pHlp->pfnSSMGetU32(pSSM, &u32);
AssertLogRelRCReturn(rc, rc);
if (u32 != UINT32_MAX)
{
RT_NOREF(pVdma, u32Version);
WARN(("Unsupported VBVACtl info!\n"));
return VERR_VERSION_MISMATCH;
}
return VINF_SUCCESS;
}
int vboxVDMASaveLoadDone(struct VBOXVDMAHOST *pVdma)
{
RT_NOREF(pVdma);
return VINF_SUCCESS;
}
|