VirtualBox

source: vbox/trunk/src/VBox/Devices/VirtIO/Virtio_1_0.cpp@ 84509

最後變更 在這個檔案從84509是 84509,由 vboxsync 提交於 5 年 前

iprt/cdefs.h,*: Introducing RT_FLEXIBLE_ARRAY_EXTENSION as a g++ hack that allows us to use RT_FLEXIBLE_ARRAY without the compiler going all pendantic on us. Only tested with 10.1.0. bugref:9746

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 89.5 KB
 
1/* $Id: Virtio_1_0.cpp 84509 2020-05-25 15:09:24Z vboxsync $ */
2/** @file
3 * Virtio_1_0 - Virtio Common (PCI, feature & config mgt, queue mgt & proxy, notification mgt)
4 */
5
6/*
7 * Copyright (C) 2009-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.alldomusa.eu.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_DEV_VIRTIO
23
24#include <VBox/log.h>
25#include <VBox/msi.h>
26#include <VBox/AssertGuest.h>
27#include <iprt/param.h>
28#include <iprt/assert.h>
29#include <iprt/uuid.h>
30#include <iprt/mem.h>
31#include <iprt/assert.h>
32#include <iprt/sg.h>
33#include <iprt/string.h>
34#include <VBox/vmm/pdmdev.h>
35#include "Virtio_1_0.h"
36
37
38/*********************************************************************************************************************************
39* Defined Constants And Macros *
40*********************************************************************************************************************************/
41#define INSTANCE(a_pVirtio) ((a_pVirtio)->szInstance)
42#define VIRTQNAME(a_pVirtio, a_idxQueue) ((a_pVirtio)->virtqState[(a_idxQueue)].szVirtqName)
43#define IS_DRIVER_OK(a_pVirtio) ((a_pVirtio)->uDeviceStatus & VIRTIO_STATUS_DRIVER_OK)
44
45/**
46 * This macro returns true if the @a a_offAccess and access length (@a
47 * a_cbAccess) are within the range of the mapped capability struct described by
48 * @a a_LocCapData.
49 *
50 * @param[in] a_offAccess The offset into the MMIO bar of the access.
51 * @param[in] a_cbAccess The access size.
52 * @param[out] a_offIntraVar The variable to return the intra-capability
53 * offset into. ASSUMES this is uint32_t.
54 * @param[in] a_LocCapData The capability location info.
55 */
56#define MATCHES_VIRTIO_CAP_STRUCT(a_offAccess, a_cbAccess, a_offIntraVar, a_LocCapData) \
57 ( ((a_offIntraVar) = (uint32_t)((a_offAccess) - (a_LocCapData).offMmio)) < (uint32_t)(a_LocCapData).cbMmio \
58 && (a_offIntraVar) + (uint32_t)(a_cbAccess) <= (uint32_t)(a_LocCapData).cbMmio )
59
60
61/** Marks the start of the virtio saved state (just for sanity). */
62#define VIRTIO_SAVEDSTATE_MARKER UINT64_C(0x1133557799bbddff)
63/** The current saved state version for the virtio core. */
64#define VIRTIO_SAVEDSTATE_VERSION UINT32_C(1)
65
66
67/*********************************************************************************************************************************
68* Structures and Typedefs *
69*********************************************************************************************************************************/
70
71
72/** @name virtq related flags
73 * @{ */
74#define VIRTQ_DESC_F_NEXT 1 /**< Indicates this descriptor chains to next */
75#define VIRTQ_DESC_F_WRITE 2 /**< Marks buffer as write-only (default ro) */
76#define VIRTQ_DESC_F_INDIRECT 4 /**< Buffer is list of buffer descriptors */
77
78#define VIRTQ_USED_F_NO_NOTIFY 1 /**< Dev to Drv: Don't notify when buf added */
79#define VIRTQ_AVAIL_F_NO_INTERRUPT 1 /**< Drv to Dev: Don't notify when buf eaten */
80/** @} */
81
82/**
83 * virtq related structs
84 * (struct names follow VirtIO 1.0 spec, typedef use VBox style)
85 */
86typedef struct virtq_desc
87{
88 uint64_t GCPhysBuf; /**< addr GC Phys. address of buffer */
89 uint32_t cb; /**< len Buffer length */
90 uint16_t fFlags; /**< flags Buffer specific flags */
91 uint16_t uDescIdxNext; /**< next Idx set if VIRTIO_DESC_F_NEXT */
92} VIRTQ_DESC_T, *PVIRTQ_DESC_T;
93
94typedef struct virtq_avail
95{
96 uint16_t fFlags; /**< flags avail ring guest-to-host flags */
97 uint16_t uIdx; /**< idx Index of next free ring slot */
98 RT_FLEXIBLE_ARRAY_EXTENSION
99 uint16_t auRing[RT_FLEXIBLE_ARRAY]; /**< ring Ring: avail drv to dev bufs */
100 /* uint16_t uUsedEventIdx; - used_event (if VIRTQ_USED_F_EVENT_IDX) */
101} VIRTQ_AVAIL_T, *PVIRTQ_AVAIL_T;
102
103typedef struct virtq_used_elem
104{
105 uint32_t uDescIdx; /**< idx Start of used desc chain */
106 uint32_t cbElem; /**< len Total len of used desc chain */
107} VIRTQ_USED_ELEM_T;
108
109typedef struct virt_used
110{
111 uint16_t fFlags; /**< flags used ring host-to-guest flags */
112 uint16_t uIdx; /**< idx Index of next ring slot */
113 RT_FLEXIBLE_ARRAY_EXTENSION
114 VIRTQ_USED_ELEM_T aRing[RT_FLEXIBLE_ARRAY]; /**< ring Ring: used dev to drv bufs */
115 /* uint16_t uAvailEventIdx; - avail_event if (VIRTQ_USED_F_EVENT_IDX) */
116} VIRTQ_USED_T, *PVIRTQ_USED_T;
117
118
119const char *virtioCoreGetStateChangeText(VIRTIOVMSTATECHANGED enmState)
120{
121 switch (enmState)
122 {
123 case kvirtIoVmStateChangedReset: return "VM RESET";
124 case kvirtIoVmStateChangedSuspend: return "VM SUSPEND";
125 case kvirtIoVmStateChangedPowerOff: return "VM POWER OFF";
126 case kvirtIoVmStateChangedResume: return "VM RESUME";
127 default: return "<BAD ENUM>";
128 }
129}
130
131/* Internal Functions */
132
133static void virtioNotifyGuestDriver(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t idxQueue);
134static int virtioKick(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint8_t uCause, uint16_t uVec);
135
136/** @name Internal queue operations
137 * @{ */
138
139/**
140 * Accessor for virtq descriptor
141 */
142#ifdef IN_RING3
143DECLINLINE(void) virtioReadDesc(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t idxQueue,
144 uint32_t idxDesc, PVIRTQ_DESC_T pDesc)
145{
146 AssertMsg(pVirtio->uDeviceStatus & VIRTIO_STATUS_DRIVER_OK, ("Called with guest driver not ready\n"));
147 uint16_t const cQueueItems = RT_MAX(pVirtio->uQueueSize[idxQueue], 1); /* Make sure to avoid div-by-zero. */
148 PDMDevHlpPCIPhysRead(pDevIns,
149 pVirtio->aGCPhysQueueDesc[idxQueue] + sizeof(VIRTQ_DESC_T) * (idxDesc % cQueueItems),
150 pDesc, sizeof(VIRTQ_DESC_T));
151}
152#endif
153
154/**
155 * Accessors for virtq avail ring
156 */
157#ifdef IN_RING3
158DECLINLINE(uint16_t) virtioReadAvailDescIdx(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t idxQueue, uint32_t availIdx)
159{
160 uint16_t uDescIdx;
161 AssertMsg(pVirtio->uDeviceStatus & VIRTIO_STATUS_DRIVER_OK, ("Called with guest driver not ready\n"));
162 uint16_t const cQueueItems = RT_MAX(pVirtio->uQueueSize[idxQueue], 1); /* Make sure to avoid div-by-zero. */
163 PDMDevHlpPCIPhysRead(pDevIns,
164 pVirtio->aGCPhysQueueAvail[idxQueue]
165 + RT_UOFFSETOF_DYN(VIRTQ_AVAIL_T, auRing[availIdx % cQueueItems]),
166 &uDescIdx, sizeof(uDescIdx));
167 return uDescIdx;
168}
169
170DECLINLINE(uint16_t) virtioReadAvailUsedEvent(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t idxQueue)
171{
172 uint16_t uUsedEventIdx;
173 /* VirtIO 1.0 uUsedEventIdx (used_event) immediately follows ring */
174 AssertMsg(pVirtio->uDeviceStatus & VIRTIO_STATUS_DRIVER_OK, ("Called with guest driver not ready\n"));
175 PDMDevHlpPCIPhysRead(pDevIns,
176 pVirtio->aGCPhysQueueAvail[idxQueue] + RT_UOFFSETOF_DYN(VIRTQ_AVAIL_T, auRing[pVirtio->uQueueSize[idxQueue]]),
177 &uUsedEventIdx, sizeof(uUsedEventIdx));
178 return uUsedEventIdx;
179}
180#endif
181
182DECLINLINE(uint16_t) virtioReadAvailRingIdx(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t idxQueue)
183{
184 uint16_t uIdx = 0;
185 AssertMsg(pVirtio->uDeviceStatus & VIRTIO_STATUS_DRIVER_OK, ("Called with guest driver not ready\n"));
186 PDMDevHlpPCIPhysRead(pDevIns,
187 pVirtio->aGCPhysQueueAvail[idxQueue] + RT_UOFFSETOF(VIRTQ_AVAIL_T, uIdx),
188 &uIdx, sizeof(uIdx));
189 return uIdx;
190}
191
192DECLINLINE(bool) virtqIsEmpty(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t idxQueue)
193{
194 uint16_t uAvailGuestIdx = virtioReadAvailRingIdx(pDevIns, pVirtio, idxQueue);
195 bool fEmpty = uAvailGuestIdx == pVirtio->virtqState[idxQueue].uAvailIdx;
196
197 Log6Func(("%s idx=%u, shadow idx=%u (%s)\n",
198 VIRTQNAME(pVirtio, idxQueue), uAvailGuestIdx, pVirtio->virtqState[idxQueue].uAvailIdx,
199 fEmpty ? "Queue empty" : "Queue has available descriptors"));
200 return fEmpty;
201}
202
203DECLINLINE(uint16_t) virtioReadAvailRingFlags(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t idxQueue)
204{
205 uint16_t fFlags = 0;
206 AssertMsg(pVirtio->uDeviceStatus & VIRTIO_STATUS_DRIVER_OK, ("Called with guest driver not ready\n"));
207 PDMDevHlpPCIPhysRead(pDevIns,
208 pVirtio->aGCPhysQueueAvail[idxQueue] + RT_UOFFSETOF(VIRTQ_AVAIL_T, fFlags),
209 &fFlags, sizeof(fFlags));
210 return fFlags;
211}
212
213/** @} */
214
215/** @name Accessors for virtq used ring
216 * @{
217 */
218
219#ifdef IN_RING3
220DECLINLINE(void) virtioWriteUsedElem(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t idxQueue,
221 uint32_t usedIdx, uint32_t uDescIdx, uint32_t uLen)
222{
223 VIRTQ_USED_ELEM_T elem = { uDescIdx, uLen };
224 AssertMsg(pVirtio->uDeviceStatus & VIRTIO_STATUS_DRIVER_OK, ("Called with guest driver not ready\n"));
225 uint16_t const cQueueItems = RT_MAX(pVirtio->uQueueSize[idxQueue], 1); /* Make sure to avoid div-by-zero. */
226 PDMDevHlpPCIPhysWrite(pDevIns,
227 pVirtio->aGCPhysQueueUsed[idxQueue] + RT_UOFFSETOF_DYN(VIRTQ_USED_T, aRing[usedIdx % cQueueItems]),
228 &elem, sizeof(elem));
229}
230
231DECLINLINE(void) virtioWriteUsedRingFlags(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t idxQueue, uint16_t fFlags)
232{
233 AssertMsg(pVirtio->uDeviceStatus & VIRTIO_STATUS_DRIVER_OK, ("Called with guest driver not ready\n"));
234 RT_UNTRUSTED_VALIDATED_FENCE(); /* VirtIO 1.0, Section 3.2.1.4.1 */
235 PDMDevHlpPCIPhysWrite(pDevIns,
236 pVirtio->aGCPhysQueueUsed[idxQueue] + RT_UOFFSETOF(VIRTQ_USED_T, fFlags),
237 &fFlags, sizeof(fFlags));
238}
239#endif
240
241DECLINLINE(void) virtioWriteUsedRingIdx(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t idxQueue, uint16_t uIdx)
242{
243 AssertMsg(pVirtio->uDeviceStatus & VIRTIO_STATUS_DRIVER_OK, ("Called with guest driver not ready\n"));
244 PDMDevHlpPCIPhysWrite(pDevIns,
245 pVirtio->aGCPhysQueueUsed[idxQueue] + RT_UOFFSETOF(VIRTQ_USED_T, uIdx),
246 &uIdx, sizeof(uIdx));
247}
248
249#ifdef LOG_ENABLED
250DECLINLINE(uint16_t) virtioReadUsedRingIdx(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t idxQueue)
251{
252 uint16_t uIdx = 0;
253 AssertMsg(pVirtio->uDeviceStatus & VIRTIO_STATUS_DRIVER_OK, ("Called with guest driver not ready\n"));
254 PDMDevHlpPCIPhysRead(pDevIns,
255 pVirtio->aGCPhysQueueUsed[idxQueue] + RT_UOFFSETOF(VIRTQ_USED_T, uIdx),
256 &uIdx, sizeof(uIdx));
257 return uIdx;
258}
259#endif
260
261
262#ifdef IN_RING3
263DECLINLINE(uint16_t) virtioReadUsedRingFlags(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t idxQueue)
264{
265 uint16_t fFlags = 0;
266 AssertMsg(pVirtio->uDeviceStatus & VIRTIO_STATUS_DRIVER_OK, ("Called with guest driver not ready\n"));
267 PDMDevHlpPCIPhysRead(pDevIns,
268 pVirtio->aGCPhysQueueUsed[idxQueue] + RT_UOFFSETOF(VIRTQ_USED_T, fFlags),
269 &fFlags, sizeof(fFlags));
270 return fFlags;
271}
272
273DECLINLINE(void) virtioWriteUsedAvailEvent(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t idxQueue, uint32_t uAvailEventIdx)
274{
275 /** VirtIO 1.0 uAvailEventIdx (avail_event) immediately follows ring */
276 AssertMsg(pVirtio->uDeviceStatus & VIRTIO_STATUS_DRIVER_OK, ("Called with guest driver not ready\n"));
277 PDMDevHlpPCIPhysWrite(pDevIns,
278 pVirtio->aGCPhysQueueUsed[idxQueue] + RT_UOFFSETOF_DYN(VIRTQ_USED_T, aRing[pVirtio->uQueueSize[idxQueue]]),
279 &uAvailEventIdx, sizeof(uAvailEventIdx));
280}
281#endif
282
283/** @} */
284
285void virtioCoreSgBufInit(PVIRTIOSGBUF pGcSgBuf, PVIRTIOSGSEG paSegs, size_t cSegs)
286{
287 AssertPtr(pGcSgBuf);
288 Assert( (cSegs > 0 && VALID_PTR(paSegs)) || (!cSegs && !paSegs));
289 Assert(cSegs < (~(unsigned)0 >> 1));
290
291 pGcSgBuf->paSegs = paSegs;
292 pGcSgBuf->cSegs = (unsigned)cSegs;
293 pGcSgBuf->idxSeg = 0;
294 if (cSegs && paSegs)
295 {
296 pGcSgBuf->gcPhysCur = paSegs[0].gcPhys;
297 pGcSgBuf->cbSegLeft = paSegs[0].cbSeg;
298 }
299 else
300 {
301 pGcSgBuf->gcPhysCur = 0;
302 pGcSgBuf->cbSegLeft = 0;
303 }
304}
305
306static RTGCPHYS virtioCoreSgBufGet(PVIRTIOSGBUF pGcSgBuf, size_t *pcbData)
307{
308 size_t cbData;
309 RTGCPHYS pGcBuf;
310
311 /* Check that the S/G buffer has memory left. */
312 if (RT_LIKELY(pGcSgBuf->idxSeg < pGcSgBuf->cSegs && pGcSgBuf->cbSegLeft))
313 { /* likely */ }
314 else
315 {
316 *pcbData = 0;
317 return 0;
318 }
319
320 AssertMsg( pGcSgBuf->cbSegLeft <= 128 * _1M
321 && (RTGCPHYS)pGcSgBuf->gcPhysCur >= (RTGCPHYS)pGcSgBuf->paSegs[pGcSgBuf->idxSeg].gcPhys
322 && (RTGCPHYS)pGcSgBuf->gcPhysCur + pGcSgBuf->cbSegLeft <=
323 (RTGCPHYS)pGcSgBuf->paSegs[pGcSgBuf->idxSeg].gcPhys + pGcSgBuf->paSegs[pGcSgBuf->idxSeg].cbSeg,
324 ("pGcSgBuf->idxSeg=%d pGcSgBuf->cSegs=%d pGcSgBuf->gcPhysCur=%p pGcSgBuf->cbSegLeft=%zd "
325 "pGcSgBuf->paSegs[%d].gcPhys=%p pGcSgBuf->paSegs[%d].cbSeg=%zd\n",
326 pGcSgBuf->idxSeg, pGcSgBuf->cSegs, pGcSgBuf->gcPhysCur, pGcSgBuf->cbSegLeft,
327 pGcSgBuf->idxSeg, pGcSgBuf->paSegs[pGcSgBuf->idxSeg].gcPhys, pGcSgBuf->idxSeg,
328 pGcSgBuf->paSegs[pGcSgBuf->idxSeg].cbSeg));
329
330 cbData = RT_MIN(*pcbData, pGcSgBuf->cbSegLeft);
331 pGcBuf = pGcSgBuf->gcPhysCur;
332 pGcSgBuf->cbSegLeft -= cbData;
333 if (!pGcSgBuf->cbSegLeft)
334 {
335 pGcSgBuf->idxSeg++;
336
337 if (pGcSgBuf->idxSeg < pGcSgBuf->cSegs)
338 {
339 pGcSgBuf->gcPhysCur = pGcSgBuf->paSegs[pGcSgBuf->idxSeg].gcPhys;
340 pGcSgBuf->cbSegLeft = pGcSgBuf->paSegs[pGcSgBuf->idxSeg].cbSeg;
341 }
342 *pcbData = cbData;
343 }
344 else
345 pGcSgBuf->gcPhysCur = pGcSgBuf->gcPhysCur + cbData;
346
347 return pGcBuf;
348}
349
350void virtioCoreSgBufReset(PVIRTIOSGBUF pGcSgBuf)
351{
352 AssertPtrReturnVoid(pGcSgBuf);
353
354 pGcSgBuf->idxSeg = 0;
355 if (pGcSgBuf->cSegs)
356 {
357 pGcSgBuf->gcPhysCur = pGcSgBuf->paSegs[0].gcPhys;
358 pGcSgBuf->cbSegLeft = pGcSgBuf->paSegs[0].cbSeg;
359 }
360 else
361 {
362 pGcSgBuf->gcPhysCur = 0;
363 pGcSgBuf->cbSegLeft = 0;
364 }
365}
366
367RTGCPHYS virtioCoreSgBufAdvance(PVIRTIOSGBUF pGcSgBuf, size_t cbAdvance)
368{
369 AssertReturn(pGcSgBuf, 0);
370
371 size_t cbLeft = cbAdvance;
372 while (cbLeft)
373 {
374 size_t cbThisAdvance = cbLeft;
375 virtioCoreSgBufGet(pGcSgBuf, &cbThisAdvance);
376 if (!cbThisAdvance)
377 break;
378
379 cbLeft -= cbThisAdvance;
380 }
381 return cbAdvance - cbLeft;
382}
383
384RTGCPHYS virtioCoreSgBufGetNextSegment(PVIRTIOSGBUF pGcSgBuf, size_t *pcbSeg)
385{
386 AssertReturn(pGcSgBuf, 0);
387 AssertPtrReturn(pcbSeg, 0);
388
389 if (!*pcbSeg)
390 *pcbSeg = pGcSgBuf->cbSegLeft;
391
392 return virtioCoreSgBufGet(pGcSgBuf, pcbSeg);
393}
394
395size_t virtioCoreSgBufCalcTotalLength(PVIRTIOSGBUF pGcSgBuf)
396{
397 size_t cb = 0;
398 unsigned i = pGcSgBuf->cSegs;
399 while (i-- > 0)
400 cb += pGcSgBuf->paSegs[i].cbSeg;
401 return cb;
402 }
403
404#ifdef LOG_ENABLED
405
406void virtioPrintFeatures(VIRTIOCORE *pVirtio)
407{
408#ifdef LOG_ENABLED
409 static struct
410 {
411 uint64_t fFeatureBit;
412 const char *pcszDesc;
413 } const s_aFeatures[] =
414 {
415 { VIRTIO_F_RING_INDIRECT_DESC, " RING_INDIRECT_DESC Driver can use descriptors with VIRTQ_DESC_F_INDIRECT flag set\n" },
416 { VIRTIO_F_RING_EVENT_IDX, " RING_EVENT_IDX Enables use_event and avail_event fields described in 2.4.7, 2.4.8\n" },
417 { VIRTIO_F_VERSION_1, " VERSION Used to detect legacy drivers.\n" },
418 };
419
420#define MAXLINE 80
421 /* Display as a single buf to prevent interceding log messages */
422 uint16_t cbBuf = RT_ELEMENTS(s_aFeatures) * 132;
423 char *pszBuf = (char *)RTMemAllocZ(cbBuf);
424 Assert(pszBuf);
425 char *cp = pszBuf;
426 for (unsigned i = 0; i < RT_ELEMENTS(s_aFeatures); ++i)
427 {
428 bool isOffered = RT_BOOL(pVirtio->uDeviceFeatures & s_aFeatures[i].fFeatureBit);
429 bool isNegotiated = RT_BOOL(pVirtio->uDriverFeatures & s_aFeatures[i].fFeatureBit);
430 cp += RTStrPrintf(cp, cbBuf - (cp - pszBuf), " %s %s %s",
431 isOffered ? "+" : "-", isNegotiated ? "x" : " ", s_aFeatures[i].pcszDesc);
432 }
433 Log3(("VirtIO Features Configuration\n\n"
434 " Offered Accepted Feature Description\n"
435 " ------- -------- ------- -----------\n"
436 "%s\n", pszBuf));
437 RTMemFree(pszBuf);
438
439#else /* !LOG_ENABLED */
440 RT_NOREF3(pThis, fFeatures, pcszText);
441#endif /* !LOG_ENABLED */
442}
443
444
445/**
446 * Does a formatted hex dump using Log(()), recommend using VIRTIO_HEX_DUMP() macro to
447 * control enabling of logging efficiently.
448 *
449 * @param pv pointer to buffer to dump contents of
450 * @param cb count of characters to dump from buffer
451 * @param uBase base address of per-row address prefixing of hex output
452 * @param pszTitle Optional title. If present displays title that lists
453 * provided text with value of cb to indicate size next to it.
454 */
455void virtioCoreHexDump(uint8_t *pv, uint32_t cb, uint32_t uBase, const char *pszTitle)
456{
457 if (pszTitle)
458 Log(("%s [%d bytes]:\n", pszTitle, cb));
459 for (uint32_t row = 0; row < RT_MAX(1, (cb / 16) + 1) && row * 16 < cb; row++)
460 {
461 Log(("%04x: ", row * 16 + uBase)); /* line address */
462 for (uint8_t col = 0; col < 16; col++)
463 {
464 uint32_t idx = row * 16 + col;
465 if (idx >= cb)
466 Log(("-- %s", (col + 1) % 8 ? "" : " "));
467 else
468 Log(("%02x %s", pv[idx], (col + 1) % 8 ? "" : " "));
469 }
470 for (uint32_t idx = row * 16; idx < row * 16 + 16; idx++)
471 Log(("%c", (idx >= cb) ? ' ' : (pv[idx] >= 0x20 && pv[idx] <= 0x7e ? pv[idx] : '.')));
472 Log(("\n"));
473 }
474 Log(("\n"));
475 RT_NOREF2(uBase, pv);
476}
477
478/**
479 * Do a hex dump of memory in guest physical context
480 *
481 * @param gcPhys pointer to buffer to dump contents of
482 * @param cb count of characters to dump from buffer
483 * @param uBase base address of per-row address prefixing of hex output
484 * @param pszTitle Optional title. If present displays title that lists
485 * provided text with value of cb to indicate size next to it.
486 */
487void virtioCoreGcPhysHexDump(PPDMDEVINS pDevIns, RTGCPHYS gcPhys, uint32_t cb, uint32_t uBase, const char *pszTitle)
488{
489 if (pszTitle)
490 Log(("%s [%d bytes]:\n", pszTitle, cb));
491 for (uint32_t row = 0; row < RT_MAX(1, (cb / 16) + 1) && row * 16 < cb; row++)
492 {
493 uint8_t c;
494 Log(("%04x: ", row * 16 + uBase)); /* line address */
495 for (uint8_t col = 0; col < 16; col++)
496 {
497 uint32_t idx = row * 16 + col;
498 PDMDevHlpPCIPhysRead(pDevIns, gcPhys + idx, &c, 1);
499 if (idx >= cb)
500 Log(("-- %s", (col + 1) % 8 ? "" : " "));
501 else
502 Log(("%02x %s", c, (col + 1) % 8 ? "" : " "));
503 }
504 for (uint32_t idx = row * 16; idx < row * 16 + 16; idx++)
505 {
506 PDMDevHlpPCIPhysRead(pDevIns, gcPhys + idx, &c, 1);
507 Log(("%c", (idx >= cb) ? ' ' : (c >= 0x20 && c <= 0x7e ? c : '.')));
508 }
509 Log(("\n"));
510 }
511 Log(("\n"));
512 RT_NOREF(uBase);
513}
514#endif /* LOG_ENABLED */
515
516/**
517 * Log memory-mapped I/O input or output value.
518 *
519 * This is designed to be invoked by macros that can make contextual assumptions
520 * (e.g. implicitly derive MACRO parameters from the invoking function). It is exposed
521 * for the VirtIO client doing the device-specific implementation in order to log in a
522 * similar fashion accesses to the device-specific MMIO configuration structure. Macros
523 * that leverage this function are found in virtioCommonCfgAccessed() and can be
524 * used as an example of how to use this effectively for the device-specific
525 * code.
526 *
527 * @param pszFunc To avoid displaying this function's name via __FUNCTION__ or LogFunc()
528 * @param pszMember Name of struct member
529 * @param pv pointer to value
530 * @param cb size of value
531 * @param uOffset offset into member where value starts
532 * @param fWrite True if write I/O
533 * @param fHasIndex True if the member is indexed
534 * @param idx The index if fHasIndex
535 */
536void virtioCoreLogMappedIoValue(const char *pszFunc, const char *pszMember, uint32_t uMemberSize,
537 const void *pv, uint32_t cb, uint32_t uOffset, int fWrite,
538 int fHasIndex, uint32_t idx)
539{
540 if (!LogIs6Enabled())
541 return;
542
543 char szIdx[16];
544 if (fHasIndex)
545 RTStrPrintf(szIdx, sizeof(szIdx), "[%d]", idx);
546 else
547 szIdx[0] = '\0';
548
549 if (cb == 1 || cb == 2 || cb == 4 || cb == 8)
550 {
551 char szDepiction[64];
552 size_t cchDepiction;
553 if (uOffset != 0 || cb != uMemberSize) /* display bounds if partial member access */
554 cchDepiction = RTStrPrintf(szDepiction, sizeof(szDepiction), "%s%s[%d:%d]",
555 pszMember, szIdx, uOffset, uOffset + cb - 1);
556 else
557 cchDepiction = RTStrPrintf(szDepiction, sizeof(szDepiction), "%s%s", pszMember, szIdx);
558
559 /* padding */
560 if (cchDepiction < 30)
561 szDepiction[cchDepiction++] = ' ';
562 while (cchDepiction < 30)
563 szDepiction[cchDepiction++] = '.';
564 szDepiction[cchDepiction] = '\0';
565
566 RTUINT64U uValue;
567 uValue.u = 0;
568 memcpy(uValue.au8, pv, cb);
569 Log6(("%s: Guest %s %s %#0*RX64\n",
570 pszFunc, fWrite ? "wrote" : "read ", szDepiction, 2 + cb * 2, uValue.u));
571 }
572 else /* odd number or oversized access, ... log inline hex-dump style */
573 {
574 Log6(("%s: Guest %s %s%s[%d:%d]: %.*Rhxs\n",
575 pszFunc, fWrite ? "wrote" : "read ", pszMember,
576 szIdx, uOffset, uOffset + cb, cb, pv));
577 }
578 RT_NOREF2(fWrite, pszFunc);
579}
580
581
582/**
583 * Makes the MMIO-mapped Virtio uDeviceStatus registers non-cryptic
584 */
585DECLINLINE(void) virtioLogDeviceStatus(uint8_t bStatus)
586{
587 if (bStatus == 0)
588 Log6(("RESET"));
589 else
590 {
591 int primed = 0;
592 if (bStatus & VIRTIO_STATUS_ACKNOWLEDGE)
593 Log6(("%sACKNOWLEDGE", primed++ ? "" : ""));
594 if (bStatus & VIRTIO_STATUS_DRIVER)
595 Log6(("%sDRIVER", primed++ ? " | " : ""));
596 if (bStatus & VIRTIO_STATUS_FEATURES_OK)
597 Log6(("%sFEATURES_OK", primed++ ? " | " : ""));
598 if (bStatus & VIRTIO_STATUS_DRIVER_OK)
599 Log6(("%sDRIVER_OK", primed++ ? " | " : ""));
600 if (bStatus & VIRTIO_STATUS_FAILED)
601 Log6(("%sFAILED", primed++ ? " | " : ""));
602 if (bStatus & VIRTIO_STATUS_DEVICE_NEEDS_RESET)
603 Log6(("%sNEEDS_RESET", primed++ ? " | " : ""));
604 (void)primed;
605 }
606}
607
608#ifdef IN_RING3
609/**
610 * Allocate client context for client to work with VirtIO-provided with queue
611 *
612 * @param pVirtio Pointer to the shared virtio state.
613 * @param idxQueue Queue number
614 * @param pcszName Name to give queue
615 *
616 * @returns VBox status code.
617 */
618int virtioCoreR3QueueAttach(PVIRTIOCORE pVirtio, uint16_t idxQueue, const char *pcszName)
619{
620 LogFunc(("%s\n", pcszName));
621 PVIRTQSTATE pVirtq = &pVirtio->virtqState[idxQueue];
622 pVirtq->uAvailIdx = 0;
623 pVirtq->uUsedIdx = 0;
624 pVirtq->fEventThresholdReached = false;
625 RTStrCopy(pVirtq->szVirtqName, sizeof(pVirtq->szVirtqName), pcszName);
626 return VINF_SUCCESS;
627}
628#endif /* IN_RING3 */
629
630
631/**
632 * Check if the associated queue is empty
633 *
634 * @param pDevIns The device instance (for reading).
635 * @param pVirtio Pointer to the shared virtio state.
636 * @param idxQueue Queue number
637 *
638 * @retval true Queue is empty or unavailable.
639 * @retval false Queue is available and has entries
640 */
641bool virtioCoreQueueIsEmpty(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t idxQueue)
642{
643 if (pVirtio->uDeviceStatus & VIRTIO_STATUS_DRIVER_OK)
644 return virtqIsEmpty(pDevIns, pVirtio, idxQueue);
645 LogFunc(("VirtIO not ready: Returning 'true' for queue empty\n"));
646 return true;
647}
648
649#ifdef IN_RING3
650
651
652int virtioCoreR3DescChainGet(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t idxQueue,
653 uint16_t uHeadIdx, PPVIRTIO_DESC_CHAIN_T ppDescChain)
654{
655 AssertReturn(ppDescChain, VERR_INVALID_POINTER);
656 *ppDescChain = NULL;
657
658 Assert(idxQueue < RT_ELEMENTS(pVirtio->virtqState));
659
660 PVIRTQSTATE pVirtq = &pVirtio->virtqState[idxQueue];
661
662 AssertMsgReturn(IS_DRIVER_OK(pVirtio) && pVirtio->uQueueEnable[idxQueue],
663 ("Guest driver not in ready state.\n"), VERR_INVALID_STATE);
664
665 uint16_t uDescIdx = uHeadIdx;
666
667 Log6Func(("%s DESC CHAIN: (head) desc_idx=%u\n", pVirtq->szVirtqName, uHeadIdx));
668 RT_NOREF(pVirtq);
669
670 /*
671 * Allocate and initialize the descriptor chain structure.
672 */
673 PVIRTIO_DESC_CHAIN_T pDescChain = (PVIRTIO_DESC_CHAIN_T)RTMemAllocZ(sizeof(VIRTIO_DESC_CHAIN_T));
674 AssertReturn(pDescChain, VERR_NO_MEMORY);
675 pDescChain->u32Magic = VIRTIO_DESC_CHAIN_MAGIC;
676 pDescChain->cRefs = 1;
677 pDescChain->uHeadIdx = uHeadIdx;
678 *ppDescChain = pDescChain;
679
680 /*
681 * Gather segments.
682 */
683 VIRTQ_DESC_T desc;
684
685 uint32_t cbIn = 0;
686 uint32_t cbOut = 0;
687 uint32_t cSegsIn = 0;
688 uint32_t cSegsOut = 0;
689 PVIRTIOSGSEG paSegsIn = pDescChain->aSegsIn;
690 PVIRTIOSGSEG paSegsOut = pDescChain->aSegsOut;
691
692 do
693 {
694 PVIRTIOSGSEG pSeg;
695
696 /*
697 * Malicious guests may go beyond paSegsIn or paSegsOut boundaries by linking
698 * several descriptors into a loop. Since there is no legitimate way to get a sequences of
699 * linked descriptors exceeding the total number of descriptors in the ring (see @bugref{8620}),
700 * the following aborts I/O if breach and employs a simple log throttling algorithm to notify.
701 */
702 if (cSegsIn + cSegsOut >= VIRTQ_MAX_SIZE)
703 {
704 static volatile uint32_t s_cMessages = 0;
705 static volatile uint32_t s_cThreshold = 1;
706 if (ASMAtomicIncU32(&s_cMessages) == ASMAtomicReadU32(&s_cThreshold))
707 {
708 LogRelMax(64, ("Too many linked descriptors; check if the guest arranges descriptors in a loop.\n"));
709 if (ASMAtomicReadU32(&s_cMessages) != 1)
710 LogRelMax(64, ("(the above error has occured %u times so far)\n", ASMAtomicReadU32(&s_cMessages)));
711 ASMAtomicWriteU32(&s_cThreshold, ASMAtomicReadU32(&s_cThreshold) * 10);
712 }
713 break;
714 }
715 RT_UNTRUSTED_VALIDATED_FENCE();
716
717 virtioReadDesc(pDevIns, pVirtio, idxQueue, uDescIdx, &desc);
718
719 if (desc.fFlags & VIRTQ_DESC_F_WRITE)
720 {
721 Log6Func(("%s IN desc_idx=%u seg=%u addr=%RGp cb=%u\n", VIRTQNAME(pVirtio, idxQueue), uDescIdx, cSegsIn, desc.GCPhysBuf, desc.cb));
722 cbIn += desc.cb;
723 pSeg = &paSegsIn[cSegsIn++];
724 }
725 else
726 {
727 Log6Func(("%s OUT desc_idx=%u seg=%u addr=%RGp cb=%u\n", VIRTQNAME(pVirtio, idxQueue), uDescIdx, cSegsOut, desc.GCPhysBuf, desc.cb));
728 cbOut += desc.cb;
729 pSeg = &paSegsOut[cSegsOut++];
730 }
731
732 pSeg->gcPhys = desc.GCPhysBuf;
733 pSeg->cbSeg = desc.cb;
734
735 uDescIdx = desc.uDescIdxNext;
736 } while (desc.fFlags & VIRTQ_DESC_F_NEXT);
737
738 /*
739 * Add segments to the descriptor chain structure.
740 */
741 if (cSegsIn)
742 {
743 virtioCoreSgBufInit(&pDescChain->SgBufIn, paSegsIn, cSegsIn);
744 pDescChain->pSgPhysReturn = &pDescChain->SgBufIn;
745 pDescChain->cbPhysReturn = cbIn;
746 STAM_REL_COUNTER_ADD(&pVirtio->StatDescChainsSegsIn, cSegsIn);
747 }
748
749 if (cSegsOut)
750 {
751 virtioCoreSgBufInit(&pDescChain->SgBufOut, paSegsOut, cSegsOut);
752 pDescChain->pSgPhysSend = &pDescChain->SgBufOut;
753 pDescChain->cbPhysSend = cbOut;
754 STAM_REL_COUNTER_ADD(&pVirtio->StatDescChainsSegsOut, cSegsOut);
755 }
756
757 STAM_REL_COUNTER_INC(&pVirtio->StatDescChainsAllocated);
758 Log6Func(("%s -- segs OUT: %u (%u bytes) IN: %u (%u bytes) --\n", pVirtq->szVirtqName, cSegsOut, cbOut, cSegsIn, cbIn));
759
760 return VINF_SUCCESS;
761}
762
763
764/**
765 * Retains a reference to the given descriptor chain.
766 *
767 * @returns New reference count.
768 * @retval UINT32_MAX on invalid parameter.
769 * @param pDescChain The descriptor chain to reference.
770 */
771uint32_t virtioCoreR3DescChainRetain(PVIRTIO_DESC_CHAIN_T pDescChain)
772{
773 AssertReturn(pDescChain, UINT32_MAX);
774 AssertReturn(pDescChain->u32Magic == VIRTIO_DESC_CHAIN_MAGIC, UINT32_MAX);
775 uint32_t cRefs = ASMAtomicIncU32(&pDescChain->cRefs);
776 Assert(cRefs > 1);
777 Assert(cRefs < 16);
778 return cRefs;
779}
780
781
782/**
783 * Releases a reference to the given descriptor chain.
784 *
785 * @returns New reference count.
786 * @retval 0 if freed or invalid parameter.
787 * @param pVirtio Pointer to the shared virtio state.
788 * @param pDescChain The descriptor chain to reference. NULL is quietly
789 * ignored (returns 0).
790 */
791uint32_t virtioCoreR3DescChainRelease(PVIRTIOCORE pVirtio, PVIRTIO_DESC_CHAIN_T pDescChain)
792{
793 if (!pDescChain)
794 return 0;
795 AssertReturn(pDescChain, 0);
796 AssertReturn(pDescChain->u32Magic == VIRTIO_DESC_CHAIN_MAGIC, 0);
797 uint32_t cRefs = ASMAtomicDecU32(&pDescChain->cRefs);
798 Assert(cRefs < 16);
799 if (cRefs == 0)
800 {
801 pDescChain->u32Magic = ~VIRTIO_DESC_CHAIN_MAGIC;
802 RTMemFree(pDescChain);
803 STAM_REL_COUNTER_INC(&pVirtio->StatDescChainsFreed);
804 }
805 return cRefs;
806}
807
808
809/*
810 * Notifies guest (via ISR or MSI-X) of device configuration change
811 *
812 * @param pVirtio Pointer to the shared virtio state.
813 */
814void virtioCoreNotifyConfigChanged(PVIRTIOCORE pVirtio)
815{
816 virtioKick(pVirtio->pDevInsR3, pVirtio, VIRTIO_ISR_DEVICE_CONFIG, pVirtio->uMsixConfig);
817}
818
819/**
820 * Enable or Disable notification for the specified queue
821 *
822 * @param pVirtio Pointer to the shared virtio state.
823 * @param idxQueue Queue number
824 * @param fEnabled Selects notification mode (enabled or disabled)
825 */
826void virtioCoreQueueSetNotify(PVIRTIOCORE pVirtio, uint16_t idxQueue, bool fEnabled)
827{
828 if (pVirtio->uDeviceStatus & VIRTIO_STATUS_DRIVER_OK)
829 {
830 uint16_t fFlags = virtioReadUsedRingFlags(pVirtio->pDevInsR3, pVirtio, idxQueue);
831
832 if (fEnabled)
833 fFlags &= ~ VIRTQ_USED_F_NO_NOTIFY;
834 else
835 fFlags |= VIRTQ_USED_F_NO_NOTIFY;
836
837 virtioWriteUsedRingFlags(pVirtio->pDevInsR3, pVirtio, idxQueue, fFlags);
838 }
839}
840
841/**
842 * Initiate orderly reset procedure. This is an exposed API for clients that might need it.
843 * Invoked by client to reset the device and driver (see VirtIO 1.0 section 2.1.1/2.1.2)
844 *
845 * @param pVirtio Pointer to the virtio state.
846 */
847void virtioCoreResetAll(PVIRTIOCORE pVirtio)
848{
849 LogFunc(("\n"));
850 pVirtio->uDeviceStatus |= VIRTIO_STATUS_DEVICE_NEEDS_RESET;
851 if (pVirtio->uDeviceStatus & VIRTIO_STATUS_DRIVER_OK)
852 {
853 pVirtio->fGenUpdatePending = true;
854 virtioKick(pVirtio->pDevInsR3, pVirtio, VIRTIO_ISR_DEVICE_CONFIG, pVirtio->uMsixConfig);
855 }
856}
857/**
858 * Get count of new (e.g. pending) elements in available ring.
859 *
860 * @param pDevIns The device instance.
861 * @param pVirtio Pointer to the shared virtio state.
862 * @param idxQueue Queue number
863 *
864 * @returns how many entries have been added to ring as a delta of the consumer's
865 * avail index and the queue's guest-side current avail index.
866 */
867int virtioCoreR3QueuePendingCount(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t idxQueue)
868{
869 uint16_t uAvailRingIdx = virtioReadAvailRingIdx(pDevIns, pVirtio, idxQueue);
870 uint16_t uNextAvailIdx = pVirtio->virtqState[idxQueue].uAvailIdx;
871 uint16_t uDelta = uAvailRingIdx - uNextAvailIdx;
872 if (uAvailRingIdx > uNextAvailIdx)
873 return uDelta;
874 return VIRTQ_MAX_CNT + uDelta;
875}
876/**
877 * Fetches descriptor chain using avail ring of indicated queue and converts the descriptor
878 * chain into its OUT (to device) and IN to guest components, but does NOT remove it from
879 * the 'avail' queue. I.e. doesn't advance the index. This can be used with virtioQueueSkip(),
880 * which *does* advance the avail index. Together they facilitate a mechanism that allows
881 * work with a queue element (descriptor chain) to be aborted if necessary, by not advancing
882 * the pointer, or, upon success calling the skip function (above) to move to the next element.
883 *
884 * Additionally it converts the OUT desc chain data to a contiguous virtual
885 * memory buffer for easy consumption by the caller. The caller must return the
886 * descriptor chain pointer via virtioCoreR3QueuePut() and then call virtioCoreQueueSync()
887 * at some point to return the data to the guest and complete the transaction.
888 *
889 * @param pDevIns The device instance.
890 * @param pVirtio Pointer to the shared virtio state.
891 * @param idxQueue Queue number
892 * @param ppDescChain Address to store pointer to descriptor chain that contains the
893 * pre-processed transaction information pulled from the virtq.
894 *
895 * @returns VBox status code:
896 * @retval VINF_SUCCESS Success
897 * @retval VERR_INVALID_STATE VirtIO not in ready state (asserted).
898 * @retval VERR_NOT_AVAILABLE If the queue is empty.
899 */
900
901int virtioCoreR3QueuePeek(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t idxQueue,
902 PPVIRTIO_DESC_CHAIN_T ppDescChain)
903{
904 return virtioCoreR3QueueGet(pDevIns, pVirtio, idxQueue, ppDescChain, false);
905}
906
907/**
908 * Skip the next entry in the specified queue (typically used with virtioCoreR3QueuePeek())
909 *
910 * @param pVirtio Pointer to the virtio state.
911 * @param idxQueue Index of queue
912 */
913int virtioCoreR3QueueSkip(PVIRTIOCORE pVirtio, uint16_t idxQueue)
914{
915 Assert(idxQueue < RT_ELEMENTS(pVirtio->virtqState));
916 PVIRTQSTATE pVirtq = &pVirtio->virtqState[idxQueue];
917
918 AssertMsgReturn(IS_DRIVER_OK(pVirtio) && pVirtio->uQueueEnable[idxQueue],
919 ("Guest driver not in ready state.\n"), VERR_INVALID_STATE);
920
921 if (virtioCoreQueueIsEmpty(pVirtio->pDevInsR3, pVirtio, idxQueue))
922 return VERR_NOT_AVAILABLE;
923
924 Log2Func(("%s avail_idx=%u\n", pVirtq->szVirtqName, pVirtq->uAvailIdx));
925 pVirtq->uAvailIdx++;
926
927 return VINF_SUCCESS;
928}
929
930/**
931 * Fetches descriptor chain using avail ring of indicated queue and converts the descriptor
932 * chain into its OUT (to device) and IN to guest components.
933 *
934 * Additionally it converts the OUT desc chain data to a contiguous virtual
935 * memory buffer for easy consumption by the caller. The caller must return the
936 * descriptor chain pointer via virtioCoreR3QueuePut() and then call virtioCoreQueueSync()
937 * at some point to return the data to the guest and complete the transaction.
938 *
939 * @param pDevIns The device instance.
940 * @param pVirtio Pointer to the shared virtio state.
941 * @param idxQueue Queue number
942 * @param ppDescChain Address to store pointer to descriptor chain that contains the
943 * pre-processed transaction information pulled from the virtq.
944 * Returned reference must be released by calling
945 * virtioCoreR3DescChainRelease().
946 * @param fRemove flags whether to remove desc chain from queue (false = peek)
947 *
948 * @returns VBox status code:
949 * @retval VINF_SUCCESS Success
950 * @retval VERR_INVALID_STATE VirtIO not in ready state (asserted).
951 * @retval VERR_NOT_AVAILABLE If the queue is empty.
952 */
953int virtioCoreR3QueueGet(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t idxQueue,
954 PPVIRTIO_DESC_CHAIN_T ppDescChain, bool fRemove)
955{
956 PVIRTQSTATE pVirtq = &pVirtio->virtqState[idxQueue];
957
958 if (virtqIsEmpty(pDevIns, pVirtio, idxQueue))
959 return VERR_NOT_AVAILABLE;
960
961 uint16_t uHeadIdx = virtioReadAvailDescIdx(pDevIns, pVirtio, idxQueue, pVirtq->uAvailIdx);
962
963 if (pVirtio->uDriverFeatures & VIRTIO_F_EVENT_IDX)
964 virtioWriteUsedAvailEvent(pDevIns,pVirtio, idxQueue, pVirtq->uAvailIdx + 1);
965
966 if (fRemove)
967 pVirtq->uAvailIdx++;
968
969 int rc = virtioCoreR3DescChainGet(pDevIns, pVirtio, idxQueue, uHeadIdx, ppDescChain);
970 return rc;
971}
972
973/**
974 * Returns data to the guest to complete a transaction initiated by virtQueueGet().
975 *
976 * The caller passes in a pointer to a scatter-gather buffer of virtual memory segments
977 * and a pointer to the descriptor chain context originally derived from the pulled
978 * queue entry, and this function will write the virtual memory s/g buffer into the
979 * guest's physical memory free the descriptor chain. The caller handles the freeing
980 * (as needed) of the virtual memory buffer.
981 *
982 * @note This does a write-ahead to the used ring of the guest's queue. The data
983 * written won't be seen by the guest until the next call to virtioCoreQueueSync()
984 *
985 *
986 * @param pDevIns The device instance (for reading).
987 * @param pVirtio Pointer to the shared virtio state.
988 * @param idxQueue Queue number
989 *
990 * @param pSgVirtReturn Points to scatter-gather buffer of virtual memory
991 * segments the caller is returning to the guest.
992 *
993 * @param pDescChain This contains the context of the scatter-gather
994 * buffer originally pulled from the queue.
995 *
996 * @param fFence If true, put up copy fence (memory barrier) after
997 * copying to guest phys. mem.
998 *
999 * @returns VBox status code.
1000 * @retval VINF_SUCCESS Success
1001 * @retval VERR_INVALID_STATE VirtIO not in ready state
1002 * @retval VERR_NOT_AVAILABLE Queue is empty
1003 *
1004 * @note This function will not release any reference to pDescChain. The
1005 * caller must take care of that.
1006 */
1007int virtioCoreR3QueuePut(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t idxQueue, PRTSGBUF pSgVirtReturn,
1008 PVIRTIO_DESC_CHAIN_T pDescChain, bool fFence)
1009{
1010 Assert(idxQueue < RT_ELEMENTS(pVirtio->virtqState));
1011 PVIRTQSTATE pVirtq = &pVirtio->virtqState[idxQueue];
1012 PVIRTIOSGBUF pSgPhysReturn = pDescChain->pSgPhysReturn;
1013
1014 Assert(pDescChain->u32Magic == VIRTIO_DESC_CHAIN_MAGIC);
1015 Assert(pDescChain->cRefs > 0);
1016
1017 AssertMsgReturn(IS_DRIVER_OK(pVirtio), ("Guest driver not in ready state.\n"), VERR_INVALID_STATE);
1018
1019 Log6Func(("Copying client data to %s, desc chain (head desc_idx %d)\n",
1020 VIRTQNAME(pVirtio, idxQueue), virtioReadUsedRingIdx(pDevIns, pVirtio, idxQueue)));
1021
1022 /* Copy s/g buf (virtual memory) to guest phys mem (IN direction). */
1023
1024 size_t cbCopy = 0, cbTotal = 0, cbRemain = 0;
1025
1026 if (pSgVirtReturn)
1027 {
1028 size_t cbTarget = virtioCoreSgBufCalcTotalLength(pSgPhysReturn);
1029 cbRemain = cbTotal = RTSgBufCalcTotalLength(pSgVirtReturn);
1030 AssertMsgReturn(cbTarget >= cbRemain, ("No space to write data to phys memory"), VERR_BUFFER_OVERFLOW);
1031 virtioCoreSgBufReset(pSgPhysReturn); /* Reset ptr because req data may have already been written */
1032 while (cbRemain)
1033 {
1034 cbCopy = RT_MIN(pSgVirtReturn->cbSegLeft, pSgPhysReturn->cbSegLeft);
1035 Assert(cbCopy > 0);
1036 PDMDevHlpPhysWrite(pDevIns, (RTGCPHYS)pSgPhysReturn->gcPhysCur, pSgVirtReturn->pvSegCur, cbCopy);
1037 RTSgBufAdvance(pSgVirtReturn, cbCopy);
1038 virtioCoreSgBufAdvance(pSgPhysReturn, cbCopy);
1039 cbRemain -= cbCopy;
1040 }
1041
1042 if (fFence)
1043 RT_UNTRUSTED_NONVOLATILE_COPY_FENCE(); /* needed? */
1044
1045 Assert(!(cbCopy >> 32));
1046 }
1047
1048 /* If this write-ahead crosses threshold where the driver wants to get an event flag it */
1049 if (pVirtio->uDriverFeatures & VIRTIO_F_EVENT_IDX)
1050 if (pVirtq->uUsedIdx == virtioReadAvailUsedEvent(pDevIns, pVirtio, idxQueue))
1051 pVirtq->fEventThresholdReached = true;
1052
1053 /*
1054 * Place used buffer's descriptor in used ring but don't update used ring's slot index.
1055 * That will be done with a subsequent client call to virtioCoreQueueSync() */
1056 virtioWriteUsedElem(pDevIns, pVirtio, idxQueue, pVirtq->uUsedIdx++, pDescChain->uHeadIdx, (uint32_t)cbTotal);
1057
1058 if (pSgVirtReturn)
1059 Log6Func((".... Copied %zu bytes in %d segs to %u byte buffer, residual=%zu\n",
1060 cbTotal - cbRemain, pSgVirtReturn->cSegs, pDescChain->cbPhysReturn, pDescChain->cbPhysReturn - cbTotal));
1061
1062 Log6Func(("Write ahead used_idx=%u, %s used_idx=%u\n",
1063 pVirtq->uUsedIdx, VIRTQNAME(pVirtio, idxQueue), virtioReadUsedRingIdx(pDevIns, pVirtio, idxQueue)));
1064
1065 return VINF_SUCCESS;
1066}
1067
1068#endif /* IN_RING3 */
1069
1070/**
1071 * Updates the indicated virtq's "used ring" descriptor index to match the
1072 * current write-head index, thus exposing the data added to the used ring by all
1073 * virtioCoreR3QueuePut() calls since the last sync. This should be called after one or
1074 * more virtioCoreR3QueuePut() calls to inform the guest driver there is data in the queue.
1075 * Explicit notifications (e.g. interrupt or MSI-X) will be sent to the guest,
1076 * depending on VirtIO features negotiated and conditions, otherwise the guest
1077 * will detect the update by polling. (see VirtIO 1.0 specification, Section 2.4 "Virtqueues").
1078 *
1079 * @param pDevIns The device instance.
1080 * @param pVirtio Pointer to the shared virtio state.
1081 * @param idxQueue Queue number
1082 *
1083 * @returns VBox status code.
1084 * @retval VINF_SUCCESS Success
1085 * @retval VERR_INVALID_STATE VirtIO not in ready state
1086 */
1087int virtioCoreQueueSync(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t idxQueue)
1088{
1089 Assert(idxQueue < RT_ELEMENTS(pVirtio->virtqState));
1090 PVIRTQSTATE pVirtq = &pVirtio->virtqState[idxQueue];
1091
1092 AssertMsgReturn(IS_DRIVER_OK(pVirtio) && pVirtio->uQueueEnable[idxQueue],
1093 ("Guest driver not in ready state.\n"), VERR_INVALID_STATE);
1094
1095 Log6Func(("Updating %s used_idx from %u to %u\n",
1096 VIRTQNAME(pVirtio, idxQueue), virtioReadUsedRingIdx(pDevIns, pVirtio, idxQueue), pVirtq->uUsedIdx));
1097
1098 virtioWriteUsedRingIdx(pDevIns, pVirtio, idxQueue, pVirtq->uUsedIdx);
1099 virtioNotifyGuestDriver(pDevIns, pVirtio, idxQueue);
1100
1101 return VINF_SUCCESS;
1102}
1103
1104/**
1105 */
1106static void virtioQueueNotified(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t idxQueue, uint16_t uNotifyIdx)
1107{
1108
1109 PVIRTIOCORECC pVirtioCC = PDMDEVINS_2_DATA_CC(pDevIns, PVIRTIOCORECC);
1110
1111 /* See VirtIO 1.0, section 4.1.5.2 It implies that idxQueue and uNotifyIdx should match.
1112 * Disregarding this notification may cause throughput to stop, however there's no way to know
1113 * which was queue was intended for wake-up if the two parameters disagree. */
1114
1115 AssertMsg(uNotifyIdx == idxQueue,
1116 ("Guest kicked virtq %d's notify addr w/non-corresponding virtq idx %d\n",
1117 idxQueue, uNotifyIdx));
1118 RT_NOREF(uNotifyIdx);
1119
1120 AssertReturnVoid(idxQueue < RT_ELEMENTS(pVirtio->virtqState));
1121 Log6Func(("%s\n", pVirtio->virtqState[idxQueue].szVirtqName));
1122
1123 /* Inform client */
1124 pVirtioCC->pfnQueueNotified(pDevIns, pVirtio, idxQueue);
1125}
1126
1127/**
1128 * Trigger MSI-X or INT# interrupt to notify guest of data added to used ring of
1129 * the specified virtq, depending on the interrupt configuration of the device
1130 * and depending on negotiated and realtime constraints flagged by the guest driver.
1131 *
1132 * See VirtIO 1.0 specification (section 2.4.7).
1133 *
1134 * @param pDevIns The device instance.
1135 * @param pVirtio Pointer to the shared virtio state.
1136 * @param idxQueue Queue to check for guest interrupt handling preference
1137 */
1138static void virtioNotifyGuestDriver(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t idxQueue)
1139{
1140
1141 Assert(idxQueue < RT_ELEMENTS(pVirtio->virtqState));
1142 PVIRTQSTATE pVirtq = &pVirtio->virtqState[idxQueue];
1143
1144 if (!IS_DRIVER_OK(pVirtio))
1145 {
1146 LogFunc(("Guest driver not in ready state.\n"));
1147 return;
1148 }
1149
1150 if (pVirtio->uDriverFeatures & VIRTIO_F_EVENT_IDX)
1151 {
1152 if (pVirtq->fEventThresholdReached)
1153 {
1154#ifdef IN_RING3
1155 Log6Func(("...kicking guest %s, VIRTIO_F_EVENT_IDX set and threshold (%d) reached\n",
1156 VIRTQNAME(pVirtio, idxQueue), (uint16_t)virtioReadAvailUsedEvent(pDevIns, pVirtio, idxQueue)));
1157#endif
1158 virtioKick(pDevIns, pVirtio, VIRTIO_ISR_VIRTQ_INTERRUPT, pVirtio->uQueueMsixVector[idxQueue]);
1159 pVirtq->fEventThresholdReached = false;
1160 return;
1161 }
1162#ifdef IN_RING3
1163 Log6Func(("...skipping interrupt %s, VIRTIO_F_EVENT_IDX set but threshold (%d) not reached (%d)\n",
1164 VIRTQNAME(pVirtio, idxQueue),(uint16_t)virtioReadAvailUsedEvent(pDevIns, pVirtio, idxQueue), pVirtq->uUsedIdx));
1165#endif
1166 }
1167 else
1168 {
1169 /** If guest driver hasn't suppressed interrupts, interrupt */
1170 if (!(virtioReadAvailRingFlags(pDevIns, pVirtio, idxQueue) & VIRTQ_AVAIL_F_NO_INTERRUPT))
1171 {
1172 virtioKick(pDevIns, pVirtio, VIRTIO_ISR_VIRTQ_INTERRUPT, pVirtio->uQueueMsixVector[idxQueue]);
1173 return;
1174 }
1175 Log6Func(("...skipping interrupt, queue %s, Guest flagged VIRTQ_AVAIL_F_NO_INTERRUPT for queue\n",
1176 VIRTQNAME(pVirtio, idxQueue)));
1177 }
1178}
1179
1180/**
1181 * Raise interrupt or MSI-X
1182 *
1183 * @param pDevIns The device instance.
1184 * @param pVirtio Pointer to the shared virtio state.
1185 * @param uCause Interrupt cause bit mask to set in PCI ISR port.
1186 * @param uVec MSI-X vector, if enabled
1187 */
1188static int virtioKick(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint8_t uCause, uint16_t uMsixVector)
1189{
1190 if (uCause == VIRTIO_ISR_VIRTQ_INTERRUPT)
1191 Log6Func(("reason: buffer added to 'used' ring.\n"));
1192 else
1193 if (uCause == VIRTIO_ISR_DEVICE_CONFIG)
1194 Log6Func(("reason: device config change\n"));
1195
1196 if (!pVirtio->fMsiSupport)
1197 {
1198 pVirtio->uISR |= uCause;
1199 PDMDevHlpPCISetIrq(pDevIns, 0, PDM_IRQ_LEVEL_HIGH);
1200 }
1201 else if (uMsixVector != VIRTIO_MSI_NO_VECTOR)
1202 PDMDevHlpPCISetIrq(pDevIns, uMsixVector, 1);
1203 return VINF_SUCCESS;
1204}
1205
1206/**
1207 * Lower interrupt (Called when guest reads ISR and when resetting)
1208 *
1209 * @param pDevIns The device instance.
1210 */
1211static void virtioLowerInterrupt(PPDMDEVINS pDevIns, uint16_t uMsixVector)
1212{
1213 PVIRTIOCORE pVirtio = PDMINS_2_DATA(pDevIns, PVIRTIOCORE);
1214 if (!pVirtio->fMsiSupport)
1215 PDMDevHlpPCISetIrq(pDevIns, 0, PDM_IRQ_LEVEL_LOW);
1216 else if (uMsixVector != VIRTIO_MSI_NO_VECTOR)
1217 PDMDevHlpPCISetIrq(pDevIns, pVirtio->uMsixConfig, PDM_IRQ_LEVEL_LOW);
1218}
1219
1220#ifdef IN_RING3
1221static void virtioResetQueue(PVIRTIOCORE pVirtio, uint16_t idxQueue)
1222{
1223 Assert(idxQueue < RT_ELEMENTS(pVirtio->virtqState));
1224 PVIRTQSTATE pVirtq = &pVirtio->virtqState[idxQueue];
1225 pVirtq->uAvailIdx = 0;
1226 pVirtq->uUsedIdx = 0;
1227 pVirtq->fEventThresholdReached = false;
1228 pVirtio->uQueueEnable[idxQueue] = false;
1229 pVirtio->uQueueSize[idxQueue] = VIRTQ_MAX_SIZE;
1230 pVirtio->uQueueNotifyOff[idxQueue] = idxQueue;
1231 pVirtio->uQueueMsixVector[idxQueue] = idxQueue + 2;
1232 if (!pVirtio->fMsiSupport) /* VirtIO 1.0, 4.1.4.3 and 4.1.5.1.2 */
1233 pVirtio->uQueueMsixVector[idxQueue] = VIRTIO_MSI_NO_VECTOR;
1234
1235 virtioLowerInterrupt(pVirtio->pDevInsR3, pVirtio->uQueueMsixVector[idxQueue]);
1236}
1237
1238static void virtioResetDevice(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio)
1239{
1240 Log2Func(("\n"));
1241 pVirtio->uDeviceFeaturesSelect = 0;
1242 pVirtio->uDriverFeaturesSelect = 0;
1243 pVirtio->uConfigGeneration = 0;
1244 pVirtio->uDeviceStatus = 0;
1245 pVirtio->uISR = 0;
1246
1247 if (!pVirtio->fMsiSupport)
1248 virtioLowerInterrupt(pDevIns, 0);
1249 else
1250 {
1251 virtioLowerInterrupt(pDevIns, pVirtio->uMsixConfig);
1252 for (int i = 0; i < VIRTQ_MAX_CNT; i++)
1253 {
1254 virtioLowerInterrupt(pDevIns, pVirtio->uQueueMsixVector[i]);
1255 pVirtio->uQueueMsixVector[i];
1256 }
1257 }
1258
1259 if (!pVirtio->fMsiSupport) /* VirtIO 1.0, 4.1.4.3 and 4.1.5.1.2 */
1260 pVirtio->uMsixConfig = VIRTIO_MSI_NO_VECTOR;
1261
1262 for (uint16_t idxQueue = 0; idxQueue < VIRTQ_MAX_CNT; idxQueue++)
1263 virtioResetQueue(pVirtio, idxQueue);
1264}
1265
1266/**
1267 * Invoked by this implementation when guest driver resets the device.
1268 * The driver itself will not until the device has read the status change.
1269 */
1270static void virtioGuestR3WasReset(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, PVIRTIOCORECC pVirtioCC)
1271{
1272 LogFunc(("Guest reset the device\n"));
1273
1274 /* Let the client know */
1275 pVirtioCC->pfnStatusChanged(pVirtio, pVirtioCC, 0);
1276 virtioResetDevice(pDevIns, pVirtio);
1277}
1278#endif /* IN_RING3 */
1279
1280/**
1281 * Handle accesses to Common Configuration capability
1282 *
1283 * @returns VBox status code
1284 *
1285 * @param pDevIns The device instance.
1286 * @param pVirtio Pointer to the shared virtio state.
1287 * @param pVirtioCC Pointer to the current context virtio state.
1288 * @param fWrite Set if write access, clear if read access.
1289 * @param offCfg The common configuration capability offset.
1290 * @param cb Number of bytes to read or write
1291 * @param pv Pointer to location to write to or read from
1292 */
1293static int virtioCommonCfgAccessed(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, PVIRTIOCORECC pVirtioCC,
1294 int fWrite, uint32_t offCfg, unsigned cb, void *pv)
1295{
1296/**
1297 * This macro resolves to boolean true if the implied parameters, offCfg and cb,
1298 * match the field offset and size of a field in the Common Cfg struct, (or if
1299 * it is a 64-bit field, if it accesses either 32-bit part as a 32-bit access)
1300 * This is mandated by section 4.1.3.1 of the VirtIO 1.0 specification)
1301 *
1302 * @param member Member of VIRTIO_PCI_COMMON_CFG_T
1303 * @param offCfg Implied parameter: Offset into VIRTIO_PCI_COMMON_CFG_T
1304 * @param cb Implied parameter: Number of bytes to access
1305 * @result true or false
1306 */
1307#define MATCH_COMMON_CFG(member) \
1308 ( ( RT_SIZEOFMEMB(VIRTIO_PCI_COMMON_CFG_T, member) == 8 \
1309 && ( offCfg == RT_OFFSETOF(VIRTIO_PCI_COMMON_CFG_T, member) \
1310 || offCfg == RT_OFFSETOF(VIRTIO_PCI_COMMON_CFG_T, member) + sizeof(uint32_t)) \
1311 && cb == sizeof(uint32_t)) \
1312 || ( offCfg == RT_OFFSETOF(VIRTIO_PCI_COMMON_CFG_T, member) \
1313 && cb == RT_SIZEOFMEMB(VIRTIO_PCI_COMMON_CFG_T, member)) )
1314
1315#ifdef LOG_ENABLED
1316# define LOG_COMMON_CFG_ACCESS(member, a_offIntra) \
1317 if (LogIs7Enabled()) { \
1318 virtioCoreLogMappedIoValue(__FUNCTION__, #member, RT_SIZEOFMEMB(VIRTIO_PCI_COMMON_CFG_T, member), \
1319 pv, cb, a_offIntra, fWrite, false, 0); \
1320 }
1321# define LOG_COMMON_CFG_ACCESS_INDEXED(member, idx, a_offIntra) \
1322 if (LogIs7Enabled()) { \
1323 virtioCoreLogMappedIoValue(__FUNCTION__, #member, RT_SIZEOFMEMB(VIRTIO_PCI_COMMON_CFG_T, member), \
1324 pv, cb, a_offIntra, fWrite, true, idx); \
1325 }
1326#else
1327# define LOG_COMMON_CFG_ACCESS(member, a_offIntra) do { } while (0)
1328# define LOG_COMMON_CFG_ACCESS_INDEXED(member, idx, a_offIntra) do { } while (0)
1329#endif
1330
1331#define COMMON_CFG_ACCESSOR(member) \
1332 do \
1333 { \
1334 uint32_t offIntra = offCfg - RT_OFFSETOF(VIRTIO_PCI_COMMON_CFG_T, member); \
1335 if (fWrite) \
1336 memcpy((char *)&pVirtio->member + offIntra, (const char *)pv, cb); \
1337 else \
1338 memcpy(pv, (const char *)&pVirtio->member + offIntra, cb); \
1339 LOG_COMMON_CFG_ACCESS(member, offIntra); \
1340 } while(0)
1341
1342#define COMMON_CFG_ACCESSOR_INDEXED(member, idx) \
1343 do \
1344 { \
1345 uint32_t offIntra = offCfg - RT_OFFSETOF(VIRTIO_PCI_COMMON_CFG_T, member); \
1346 if (fWrite) \
1347 memcpy((char *)&pVirtio->member[idx] + offIntra, pv, cb); \
1348 else \
1349 memcpy(pv, (const char *)&pVirtio->member[idx] + offIntra, cb); \
1350 LOG_COMMON_CFG_ACCESS_INDEXED(member, idx, offIntra); \
1351 } while(0)
1352
1353#define COMMON_CFG_ACCESSOR_READONLY(member) \
1354 do \
1355 { \
1356 uint32_t offIntra = offCfg - RT_OFFSETOF(VIRTIO_PCI_COMMON_CFG_T, member); \
1357 if (fWrite) \
1358 LogFunc(("Guest attempted to write readonly virtio_pci_common_cfg.%s\n", #member)); \
1359 else \
1360 { \
1361 memcpy(pv, (const char *)&pVirtio->member + offIntra, cb); \
1362 LOG_COMMON_CFG_ACCESS(member, offIntra); \
1363 } \
1364 } while(0)
1365
1366#define COMMON_CFG_ACCESSOR_INDEXED_READONLY(member, idx) \
1367 do \
1368 { \
1369 uint32_t offIntra = offCfg - RT_OFFSETOF(VIRTIO_PCI_COMMON_CFG_T, member); \
1370 if (fWrite) \
1371 LogFunc(("Guest attempted to write readonly virtio_pci_common_cfg.%s[%d]\n", #member, idx)); \
1372 else \
1373 { \
1374 memcpy(pv, (char const *)&pVirtio->member[idx] + offIntra, cb); \
1375 LOG_COMMON_CFG_ACCESS_INDEXED(member, idx, offIntra); \
1376 } \
1377 } while(0)
1378
1379
1380 int rc = VINF_SUCCESS;
1381 uint64_t val;
1382 if (MATCH_COMMON_CFG(uDeviceFeatures))
1383 {
1384 if (fWrite) /* Guest WRITE pCommonCfg>uDeviceFeatures */
1385 {
1386 LogFunc(("Guest attempted to write readonly virtio_pci_common_cfg.device_feature\n"));
1387 return VINF_SUCCESS;
1388 }
1389 else /* Guest READ pCommonCfg->uDeviceFeatures */
1390 {
1391 switch (pVirtio->uDeviceFeaturesSelect)
1392 {
1393 case 0:
1394 val = pVirtio->uDeviceFeatures & UINT32_C(0xffffffff);
1395 memcpy(pv, &val, cb);
1396 LOG_COMMON_CFG_ACCESS(uDeviceFeatures, offCfg - RT_UOFFSETOF(VIRTIO_PCI_COMMON_CFG_T, uDeviceFeatures));
1397 break;
1398 case 1:
1399 val = pVirtio->uDeviceFeatures >> 32;
1400 memcpy(pv, &val, cb);
1401 LOG_COMMON_CFG_ACCESS(uDeviceFeatures, offCfg - RT_UOFFSETOF(VIRTIO_PCI_COMMON_CFG_T, uDeviceFeatures) + 4);
1402 break;
1403 default:
1404 LogFunc(("Guest read uDeviceFeatures with out of range selector (%#x), returning 0\n",
1405 pVirtio->uDeviceFeaturesSelect));
1406 return VINF_IOM_MMIO_UNUSED_00;
1407 }
1408 }
1409 }
1410 else if (MATCH_COMMON_CFG(uDriverFeatures))
1411 {
1412 if (fWrite) /* Guest WRITE pCommonCfg->udriverFeatures */
1413 {
1414 switch (pVirtio->uDriverFeaturesSelect)
1415 {
1416 case 0:
1417 memcpy(&pVirtio->uDriverFeatures, pv, cb);
1418 LOG_COMMON_CFG_ACCESS(uDriverFeatures, offCfg - RT_UOFFSETOF(VIRTIO_PCI_COMMON_CFG_T, uDriverFeatures));
1419 break;
1420 case 1:
1421 memcpy((char *)&pVirtio->uDriverFeatures + sizeof(uint32_t), pv, cb);
1422 LOG_COMMON_CFG_ACCESS(uDriverFeatures, offCfg - RT_UOFFSETOF(VIRTIO_PCI_COMMON_CFG_T, uDriverFeatures) + 4);
1423 break;
1424 default:
1425 LogFunc(("Guest wrote uDriverFeatures with out of range selector (%#x), returning 0\n",
1426 pVirtio->uDriverFeaturesSelect));
1427 return VINF_SUCCESS;
1428 }
1429 }
1430 else /* Guest READ pCommonCfg->udriverFeatures */
1431 {
1432 switch (pVirtio->uDriverFeaturesSelect)
1433 {
1434 case 0:
1435 val = pVirtio->uDriverFeatures & 0xffffffff;
1436 memcpy(pv, &val, cb);
1437 LOG_COMMON_CFG_ACCESS(uDriverFeatures, offCfg - RT_UOFFSETOF(VIRTIO_PCI_COMMON_CFG_T, uDriverFeatures));
1438 break;
1439 case 1:
1440 val = (pVirtio->uDriverFeatures >> 32) & 0xffffffff;
1441 memcpy(pv, &val, cb);
1442 LOG_COMMON_CFG_ACCESS(uDriverFeatures, offCfg - RT_UOFFSETOF(VIRTIO_PCI_COMMON_CFG_T, uDriverFeatures) + 4);
1443 break;
1444 default:
1445 LogFunc(("Guest read uDriverFeatures with out of range selector (%#x), returning 0\n",
1446 pVirtio->uDriverFeaturesSelect));
1447 return VINF_IOM_MMIO_UNUSED_00;
1448 }
1449 }
1450 }
1451 else if (MATCH_COMMON_CFG(uNumQueues))
1452 {
1453 if (fWrite)
1454 {
1455 Log2Func(("Guest attempted to write readonly virtio_pci_common_cfg.num_queues\n"));
1456 return VINF_SUCCESS;
1457 }
1458 else
1459 {
1460 *(uint16_t *)pv = VIRTQ_MAX_CNT;
1461 LOG_COMMON_CFG_ACCESS(uNumQueues, 0);
1462 }
1463 }
1464 else if (MATCH_COMMON_CFG(uDeviceStatus))
1465 {
1466 if (fWrite) /* Guest WRITE pCommonCfg->uDeviceStatus */
1467 {
1468 uint8_t const fNewStatus = *(uint8_t *)pv;
1469 Log7Func(("Guest wrote uDeviceStatus ................ ("));
1470 if (LogIs7Enabled())
1471 virtioLogDeviceStatus(fNewStatus ^ pVirtio->uDeviceStatus);
1472 Log7((")\n"));
1473
1474 /* If the status changed or we were reset, we need to go to ring-3 as
1475 it requires notifying the parent device. */
1476 bool const fStatusChanged = (fNewStatus & VIRTIO_STATUS_DRIVER_OK)
1477 != (pVirtio->uPrevDeviceStatus & VIRTIO_STATUS_DRIVER_OK);
1478#ifndef IN_RING3
1479 if (fStatusChanged || fNewStatus == 0)
1480 {
1481 Log6Func(("=>ring3\n"));
1482 return VINF_IOM_R3_MMIO_WRITE;
1483 }
1484#endif
1485 pVirtio->uDeviceStatus = fNewStatus;
1486
1487#ifdef IN_RING3
1488 /*
1489 * Notify client only if status actually changed from last time and when we're reset.
1490 */
1491 if (pVirtio->uDeviceStatus == 0)
1492 virtioGuestR3WasReset(pDevIns, pVirtio, pVirtioCC);
1493 if (fStatusChanged)
1494 pVirtioCC->pfnStatusChanged(pVirtio, pVirtioCC, fNewStatus & VIRTIO_STATUS_DRIVER_OK);
1495#endif
1496 /*
1497 * Save the current status for the next write so we can see what changed.
1498 */
1499 pVirtio->uPrevDeviceStatus = pVirtio->uDeviceStatus;
1500 }
1501 else /* Guest READ pCommonCfg->uDeviceStatus */
1502 {
1503 Log7Func(("Guest read uDeviceStatus ................ ("));
1504 *(uint8_t *)pv = pVirtio->uDeviceStatus;
1505 if (LogIs7Enabled())
1506 virtioLogDeviceStatus(pVirtio->uDeviceStatus);
1507 Log7((")\n"));
1508 }
1509 }
1510 else
1511 if (MATCH_COMMON_CFG(uMsixConfig))
1512 COMMON_CFG_ACCESSOR(uMsixConfig);
1513 else
1514 if (MATCH_COMMON_CFG(uDeviceFeaturesSelect))
1515 COMMON_CFG_ACCESSOR(uDeviceFeaturesSelect);
1516 else
1517 if (MATCH_COMMON_CFG(uDriverFeaturesSelect))
1518 COMMON_CFG_ACCESSOR(uDriverFeaturesSelect);
1519 else
1520 if (MATCH_COMMON_CFG(uConfigGeneration))
1521 COMMON_CFG_ACCESSOR_READONLY(uConfigGeneration);
1522 else
1523 if (MATCH_COMMON_CFG(uQueueSelect))
1524 COMMON_CFG_ACCESSOR(uQueueSelect);
1525 else
1526 if (MATCH_COMMON_CFG(uQueueSize))
1527 COMMON_CFG_ACCESSOR_INDEXED(uQueueSize, pVirtio->uQueueSelect);
1528 else
1529 if (MATCH_COMMON_CFG(uQueueMsixVector))
1530 COMMON_CFG_ACCESSOR_INDEXED(uQueueMsixVector, pVirtio->uQueueSelect);
1531 else
1532 if (MATCH_COMMON_CFG(uQueueEnable))
1533 COMMON_CFG_ACCESSOR_INDEXED(uQueueEnable, pVirtio->uQueueSelect);
1534 else
1535 if (MATCH_COMMON_CFG(uQueueNotifyOff))
1536 COMMON_CFG_ACCESSOR_INDEXED_READONLY(uQueueNotifyOff, pVirtio->uQueueSelect);
1537 else
1538 if (MATCH_COMMON_CFG(aGCPhysQueueDesc))
1539 COMMON_CFG_ACCESSOR_INDEXED(aGCPhysQueueDesc, pVirtio->uQueueSelect);
1540 else
1541 if (MATCH_COMMON_CFG(aGCPhysQueueAvail))
1542 COMMON_CFG_ACCESSOR_INDEXED(aGCPhysQueueAvail, pVirtio->uQueueSelect);
1543 else
1544 if (MATCH_COMMON_CFG(aGCPhysQueueUsed))
1545 COMMON_CFG_ACCESSOR_INDEXED(aGCPhysQueueUsed, pVirtio->uQueueSelect);
1546 else
1547 {
1548 Log2Func(("Bad guest %s access to virtio_pci_common_cfg: offCfg=%#x (%d), cb=%d\n",
1549 fWrite ? "write" : "read ", offCfg, offCfg, cb));
1550 return fWrite ? VINF_SUCCESS : VINF_IOM_MMIO_UNUSED_00;
1551 }
1552
1553#undef COMMON_CFG_ACCESSOR_READONLY
1554#undef COMMON_CFG_ACCESSOR_INDEXED_READONLY
1555#undef COMMON_CFG_ACCESSOR_INDEXED
1556#undef COMMON_CFG_ACCESSOR
1557#undef LOG_COMMON_CFG_ACCESS_INDEXED
1558#undef LOG_COMMON_CFG_ACCESS
1559#undef MATCH_COMMON_CFG
1560#ifndef IN_RING3
1561 RT_NOREF(pDevIns, pVirtioCC);
1562#endif
1563 return rc;
1564}
1565
1566/**
1567 * @callback_method_impl{FNIOMMMIONEWREAD,
1568 * Memory mapped I/O Handler for PCI Capabilities read operations.}
1569 *
1570 * This MMIO handler specifically supports the VIRTIO_PCI_CAP_PCI_CFG capability defined
1571 * in the VirtIO 1.0 specification, section 4.1.4.7, and as such is restricted to reads
1572 * of 1, 2 or 4 bytes, only.
1573 *
1574 */
1575static DECLCALLBACK(VBOXSTRICTRC) virtioMmioRead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void *pv, unsigned cb)
1576{
1577 PVIRTIOCORE pVirtio = PDMINS_2_DATA(pDevIns, PVIRTIOCORE);
1578 PVIRTIOCORECC pVirtioCC = PDMINS_2_DATA_CC(pDevIns, PVIRTIOCORECC);
1579 AssertReturn(cb == 1 || cb == 2 || cb == 4, VERR_INVALID_PARAMETER);
1580 Assert(pVirtio == (PVIRTIOCORE)pvUser); RT_NOREF(pvUser);
1581
1582
1583 uint32_t offIntra;
1584 if (MATCHES_VIRTIO_CAP_STRUCT(off, cb, offIntra, pVirtio->LocDeviceCap))
1585 {
1586#ifdef IN_RING3
1587 /*
1588 * Callback to client to manage device-specific configuration.
1589 */
1590 VBOXSTRICTRC rcStrict = pVirtioCC->pfnDevCapRead(pDevIns, offIntra, pv, cb);
1591
1592 /*
1593 * Additionally, anytime any part of the device-specific configuration (which our client maintains)
1594 * is READ it needs to be checked to see if it changed since the last time any part was read, in
1595 * order to maintain the config generation (see VirtIO 1.0 spec, section 4.1.4.3.1)
1596 */
1597 bool fDevSpecificFieldChanged = RT_BOOL(memcmp(pVirtioCC->pbDevSpecificCfg + offIntra,
1598 pVirtioCC->pbPrevDevSpecificCfg + offIntra,
1599 RT_MIN(cb, pVirtioCC->cbDevSpecificCfg - offIntra)));
1600
1601 memcpy(pVirtioCC->pbPrevDevSpecificCfg, pVirtioCC->pbDevSpecificCfg, pVirtioCC->cbDevSpecificCfg);
1602
1603 if (pVirtio->fGenUpdatePending || fDevSpecificFieldChanged)
1604 {
1605 ++pVirtio->uConfigGeneration;
1606 Log6Func(("Bumped cfg. generation to %d because %s%s\n",
1607 pVirtio->uConfigGeneration,
1608 fDevSpecificFieldChanged ? "<dev cfg changed> " : "",
1609 pVirtio->fGenUpdatePending ? "<update was pending>" : ""));
1610 pVirtio->fGenUpdatePending = false;
1611 }
1612
1613 virtioLowerInterrupt(pDevIns, 0);
1614 return rcStrict;
1615#else
1616 return VINF_IOM_R3_MMIO_READ;
1617#endif
1618 }
1619
1620 if (MATCHES_VIRTIO_CAP_STRUCT(off, cb, offIntra, pVirtio->LocCommonCfgCap))
1621 return virtioCommonCfgAccessed(pDevIns, pVirtio, pVirtioCC, false /* fWrite */, offIntra, cb, pv);
1622
1623 if (MATCHES_VIRTIO_CAP_STRUCT(off, cb, offIntra, pVirtio->LocIsrCap) && cb == sizeof(uint8_t))
1624 {
1625 *(uint8_t *)pv = pVirtio->uISR;
1626 Log6Func(("Read and clear ISR\n"));
1627 pVirtio->uISR = 0; /* VirtIO specification requires reads of ISR to clear it */
1628 virtioLowerInterrupt(pDevIns, 0);
1629 return VINF_SUCCESS;
1630 }
1631
1632 ASSERT_GUEST_MSG_FAILED(("Bad read access to mapped capabilities region: off=%RGp cb=%u\n", off, cb));
1633 return VINF_IOM_MMIO_UNUSED_00;
1634}
1635
1636/**
1637 * @callback_method_impl{FNIOMMMIONEWREAD,
1638 * Memory mapped I/O Handler for PCI Capabilities write operations.}
1639 *
1640 * This MMIO handler specifically supports the VIRTIO_PCI_CAP_PCI_CFG capability defined
1641 * in the VirtIO 1.0 specification, section 4.1.4.7, and as such is restricted to writes
1642 * of 1, 2 or 4 bytes, only.
1643 */
1644static DECLCALLBACK(VBOXSTRICTRC) virtioMmioWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void const *pv, unsigned cb)
1645{
1646 PVIRTIOCORE pVirtio = PDMINS_2_DATA(pDevIns, PVIRTIOCORE);
1647 PVIRTIOCORECC pVirtioCC = PDMINS_2_DATA_CC(pDevIns, PVIRTIOCORECC);
1648
1649 AssertReturn(cb == 1 || cb == 2 || cb == 4, VERR_INVALID_PARAMETER);
1650
1651 Assert(pVirtio == (PVIRTIOCORE)pvUser); RT_NOREF(pvUser);
1652 uint32_t offIntra;
1653 if (MATCHES_VIRTIO_CAP_STRUCT(off, cb, offIntra, pVirtio->LocDeviceCap))
1654 {
1655#ifdef IN_RING3
1656 /*
1657 * Pass this MMIO write access back to the client to handle
1658 */
1659 return pVirtioCC->pfnDevCapWrite(pDevIns, offIntra, pv, cb);
1660#else
1661 return VINF_IOM_R3_MMIO_WRITE;
1662#endif
1663 }
1664
1665 if (MATCHES_VIRTIO_CAP_STRUCT(off, cb, offIntra, pVirtio->LocCommonCfgCap))
1666 return virtioCommonCfgAccessed(pDevIns, pVirtio, pVirtioCC, true /* fWrite */, offIntra, cb, (void *)pv);
1667
1668 if (MATCHES_VIRTIO_CAP_STRUCT(off, cb, offIntra, pVirtio->LocIsrCap) && cb == sizeof(uint8_t))
1669 {
1670 pVirtio->uISR = *(uint8_t *)pv;
1671 Log6Func(("Setting uISR = 0x%02x (virtq interrupt: %d, dev confg interrupt: %d)\n",
1672 pVirtio->uISR & 0xff,
1673 pVirtio->uISR & VIRTIO_ISR_VIRTQ_INTERRUPT,
1674 RT_BOOL(pVirtio->uISR & VIRTIO_ISR_DEVICE_CONFIG)));
1675 return VINF_SUCCESS;
1676 }
1677
1678 /* This *should* be guest driver dropping index of a new descriptor in avail ring */
1679 if (MATCHES_VIRTIO_CAP_STRUCT(off, cb, offIntra, pVirtio->LocNotifyCap) && cb == sizeof(uint16_t))
1680 {
1681 virtioQueueNotified(pDevIns, pVirtio, offIntra / VIRTIO_NOTIFY_OFFSET_MULTIPLIER, *(uint16_t *)pv);
1682 return VINF_SUCCESS;
1683 }
1684
1685 ASSERT_GUEST_MSG_FAILED(("Bad write access to mapped capabilities region: off=%RGp pv=%#p{%.*Rhxs} cb=%u\n", off, pv, cb, pv, cb));
1686 return VINF_SUCCESS;
1687}
1688
1689#ifdef IN_RING3
1690
1691/**
1692 * @callback_method_impl{FNPCICONFIGREAD}
1693 */
1694static DECLCALLBACK(VBOXSTRICTRC) virtioR3PciConfigRead(PPDMDEVINS pDevIns, PPDMPCIDEV pPciDev,
1695 uint32_t uAddress, unsigned cb, uint32_t *pu32Value)
1696{
1697 PVIRTIOCORE pVirtio = PDMINS_2_DATA(pDevIns, PVIRTIOCORE);
1698 PVIRTIOCORECC pVirtioCC = PDMINS_2_DATA_CC(pDevIns, PVIRTIOCORECC);
1699 RT_NOREF(pPciDev);
1700
1701 Log7Func(("pDevIns=%p pPciDev=%p uAddress=%#x cb=%u pu32Value=%p\n",
1702 pDevIns, pPciDev, uAddress, cb, pu32Value));
1703 if (uAddress == pVirtio->uPciCfgDataOff)
1704 {
1705 /*
1706 * VirtIO 1.0 spec section 4.1.4.7 describes a required alternative access capability
1707 * whereby the guest driver can specify a bar, offset, and length via the PCI configuration space
1708 * (the virtio_pci_cfg_cap capability), and access data items.
1709 */
1710 struct virtio_pci_cap *pPciCap = &pVirtioCC->pPciCfgCap->pciCap;
1711 uint32_t uLength = pPciCap->uLength;
1712
1713 if ( (uLength != 1 && uLength != 2 && uLength != 4)
1714 || cb != uLength
1715 || pPciCap->uBar != VIRTIO_REGION_PCI_CAP)
1716 {
1717 ASSERT_GUEST_MSG_FAILED(("Guest read virtio_pci_cfg_cap.pci_cfg_data using mismatching config. Ignoring\n"));
1718 *pu32Value = UINT32_MAX;
1719 return VINF_SUCCESS;
1720 }
1721
1722 VBOXSTRICTRC rcStrict = virtioMmioRead(pDevIns, pVirtio, pPciCap->uOffset, pu32Value, cb);
1723 Log2Func(("virtio: Guest read virtio_pci_cfg_cap.pci_cfg_data, bar=%d, offset=%d, length=%d, result=%d -> %Rrc\n",
1724 pPciCap->uBar, pPciCap->uOffset, uLength, *pu32Value, VBOXSTRICTRC_VAL(rcStrict)));
1725 return rcStrict;
1726 }
1727 return VINF_PDM_PCI_DO_DEFAULT;
1728}
1729
1730/**
1731 * @callback_method_impl{FNPCICONFIGWRITE}
1732 */
1733static DECLCALLBACK(VBOXSTRICTRC) virtioR3PciConfigWrite(PPDMDEVINS pDevIns, PPDMPCIDEV pPciDev,
1734 uint32_t uAddress, unsigned cb, uint32_t u32Value)
1735{
1736 PVIRTIOCORE pVirtio = PDMINS_2_DATA(pDevIns, PVIRTIOCORE);
1737 PVIRTIOCORECC pVirtioCC = PDMINS_2_DATA_CC(pDevIns, PVIRTIOCORECC);
1738 RT_NOREF(pPciDev);
1739
1740 Log7Func(("pDevIns=%p pPciDev=%p uAddress=%#x cb=%u u32Value=%#x\n", pDevIns, pPciDev, uAddress, cb, u32Value));
1741 if (uAddress == pVirtio->uPciCfgDataOff)
1742 {
1743 /* VirtIO 1.0 spec section 4.1.4.7 describes a required alternative access capability
1744 * whereby the guest driver can specify a bar, offset, and length via the PCI configuration space
1745 * (the virtio_pci_cfg_cap capability), and access data items. */
1746
1747 struct virtio_pci_cap *pPciCap = &pVirtioCC->pPciCfgCap->pciCap;
1748 uint32_t uLength = pPciCap->uLength;
1749
1750 if ( (uLength != 1 && uLength != 2 && uLength != 4)
1751 || cb != uLength
1752 || pPciCap->uBar != VIRTIO_REGION_PCI_CAP)
1753 {
1754 ASSERT_GUEST_MSG_FAILED(("Guest write virtio_pci_cfg_cap.pci_cfg_data using mismatching config. Ignoring\n"));
1755 return VINF_SUCCESS;
1756 }
1757
1758 VBOXSTRICTRC rcStrict = virtioMmioWrite(pDevIns, pVirtio, pPciCap->uOffset, &u32Value, cb);
1759 Log2Func(("Guest wrote virtio_pci_cfg_cap.pci_cfg_data, bar=%d, offset=%x, length=%x, value=%d -> %Rrc\n",
1760 pPciCap->uBar, pPciCap->uOffset, uLength, u32Value, VBOXSTRICTRC_VAL(rcStrict)));
1761 return rcStrict;
1762 }
1763 return VINF_PDM_PCI_DO_DEFAULT;
1764}
1765
1766
1767/*********************************************************************************************************************************
1768* Saved state. *
1769*********************************************************************************************************************************/
1770
1771/**
1772 * Called from the FNSSMDEVSAVEEXEC function of the device.
1773 *
1774 * @param pVirtio Pointer to the shared virtio state.
1775 * @param pHlp The ring-3 device helpers.
1776 * @param pSSM The saved state handle.
1777 * @returns VBox status code.
1778 */
1779int virtioCoreR3SaveExec(PVIRTIOCORE pVirtio, PCPDMDEVHLPR3 pHlp, PSSMHANDLE pSSM)
1780{
1781 LogFunc(("\n"));
1782 pHlp->pfnSSMPutU64(pSSM, VIRTIO_SAVEDSTATE_MARKER);
1783 pHlp->pfnSSMPutU32(pSSM, VIRTIO_SAVEDSTATE_VERSION);
1784
1785 pHlp->pfnSSMPutBool(pSSM, pVirtio->fGenUpdatePending);
1786 pHlp->pfnSSMPutU8(pSSM, pVirtio->uDeviceStatus);
1787 pHlp->pfnSSMPutU8(pSSM, pVirtio->uConfigGeneration);
1788 pHlp->pfnSSMPutU8(pSSM, pVirtio->uPciCfgDataOff);
1789 pHlp->pfnSSMPutU8(pSSM, pVirtio->uISR);
1790 pHlp->pfnSSMPutU16(pSSM, pVirtio->uQueueSelect);
1791 pHlp->pfnSSMPutU32(pSSM, pVirtio->uDeviceFeaturesSelect);
1792 pHlp->pfnSSMPutU32(pSSM, pVirtio->uDriverFeaturesSelect);
1793 pHlp->pfnSSMPutU64(pSSM, pVirtio->uDriverFeatures);
1794
1795 for (uint32_t i = 0; i < VIRTQ_MAX_CNT; i++)
1796 {
1797 pHlp->pfnSSMPutGCPhys64(pSSM, pVirtio->aGCPhysQueueDesc[i]);
1798 pHlp->pfnSSMPutGCPhys64(pSSM, pVirtio->aGCPhysQueueAvail[i]);
1799 pHlp->pfnSSMPutGCPhys64(pSSM, pVirtio->aGCPhysQueueUsed[i]);
1800 pHlp->pfnSSMPutU16(pSSM, pVirtio->uQueueNotifyOff[i]);
1801 pHlp->pfnSSMPutU16(pSSM, pVirtio->uQueueMsixVector[i]);
1802 pHlp->pfnSSMPutU16(pSSM, pVirtio->uQueueEnable[i]);
1803 pHlp->pfnSSMPutU16(pSSM, pVirtio->uQueueSize[i]);
1804 pHlp->pfnSSMPutU16(pSSM, pVirtio->virtqState[i].uAvailIdx);
1805 pHlp->pfnSSMPutU16(pSSM, pVirtio->virtqState[i].uUsedIdx);
1806 int rc = pHlp->pfnSSMPutMem(pSSM, pVirtio->virtqState[i].szVirtqName, 32);
1807 AssertRCReturn(rc, rc);
1808 }
1809
1810 return VINF_SUCCESS;
1811}
1812
1813/**
1814 * Called from the FNSSMDEVLOADEXEC function of the device.
1815 *
1816 * @param pVirtio Pointer to the shared virtio state.
1817 * @param pHlp The ring-3 device helpers.
1818 * @param pSSM The saved state handle.
1819 * @returns VBox status code.
1820 */
1821int virtioCoreR3LoadExec(PVIRTIOCORE pVirtio, PCPDMDEVHLPR3 pHlp, PSSMHANDLE pSSM)
1822{
1823 LogFunc(("\n"));
1824 /*
1825 * Check the marker and (embedded) version number.
1826 */
1827 uint64_t uMarker = 0;
1828 int rc = pHlp->pfnSSMGetU64(pSSM, &uMarker);
1829 AssertRCReturn(rc, rc);
1830 if (uMarker != VIRTIO_SAVEDSTATE_MARKER)
1831 return pHlp->pfnSSMSetLoadError(pSSM, VERR_SSM_DATA_UNIT_FORMAT_CHANGED, RT_SRC_POS,
1832 N_("Expected marker value %#RX64 found %#RX64 instead"),
1833 VIRTIO_SAVEDSTATE_MARKER, uMarker);
1834 uint32_t uVersion = 0;
1835 rc = pHlp->pfnSSMGetU32(pSSM, &uVersion);
1836 AssertRCReturn(rc, rc);
1837 if (uVersion != VIRTIO_SAVEDSTATE_VERSION)
1838 return pHlp->pfnSSMSetLoadError(pSSM, VERR_SSM_DATA_UNIT_FORMAT_CHANGED, RT_SRC_POS,
1839 N_("Unsupported virtio version: %u"), uVersion);
1840 /*
1841 * Load the state.
1842 */
1843 pHlp->pfnSSMGetBool(pSSM, &pVirtio->fGenUpdatePending);
1844 pHlp->pfnSSMGetU8(pSSM, &pVirtio->uDeviceStatus);
1845 pHlp->pfnSSMGetU8(pSSM, &pVirtio->uConfigGeneration);
1846 pHlp->pfnSSMGetU8(pSSM, &pVirtio->uPciCfgDataOff);
1847 pHlp->pfnSSMGetU8(pSSM, &pVirtio->uISR);
1848 pHlp->pfnSSMGetU16(pSSM, &pVirtio->uQueueSelect);
1849 pHlp->pfnSSMGetU32(pSSM, &pVirtio->uDeviceFeaturesSelect);
1850 pHlp->pfnSSMGetU32(pSSM, &pVirtio->uDriverFeaturesSelect);
1851 pHlp->pfnSSMGetU64(pSSM, &pVirtio->uDriverFeatures);
1852
1853 for (uint32_t i = 0; i < VIRTQ_MAX_CNT; i++)
1854 {
1855 pHlp->pfnSSMGetGCPhys64(pSSM, &pVirtio->aGCPhysQueueDesc[i]);
1856 pHlp->pfnSSMGetGCPhys64(pSSM, &pVirtio->aGCPhysQueueAvail[i]);
1857 pHlp->pfnSSMGetGCPhys64(pSSM, &pVirtio->aGCPhysQueueUsed[i]);
1858 pHlp->pfnSSMGetU16(pSSM, &pVirtio->uQueueNotifyOff[i]);
1859 pHlp->pfnSSMGetU16(pSSM, &pVirtio->uQueueMsixVector[i]);
1860 pHlp->pfnSSMGetU16(pSSM, &pVirtio->uQueueEnable[i]);
1861 pHlp->pfnSSMGetU16(pSSM, &pVirtio->uQueueSize[i]);
1862 pHlp->pfnSSMGetU16(pSSM, &pVirtio->virtqState[i].uAvailIdx);
1863 pHlp->pfnSSMGetU16(pSSM, &pVirtio->virtqState[i].uUsedIdx);
1864 rc = pHlp->pfnSSMGetMem(pSSM, pVirtio->virtqState[i].szVirtqName,
1865 sizeof(pVirtio->virtqState[i].szVirtqName));
1866 AssertRCReturn(rc, rc);
1867 }
1868
1869 return VINF_SUCCESS;
1870}
1871
1872
1873/*********************************************************************************************************************************
1874* Device Level *
1875*********************************************************************************************************************************/
1876
1877/**
1878 * This must be called by the client to handle VM state changes
1879 * after the client takes care of its device-specific tasks for the state change.
1880 * (i.e. Reset, suspend, power-off, resume)
1881 *
1882 * @param pDevIns The device instance.
1883 * @param pVirtio Pointer to the shared virtio state.
1884 */
1885void virtioCoreR3VmStateChanged(PVIRTIOCORE pVirtio, VIRTIOVMSTATECHANGED enmState)
1886{
1887 LogFunc(("State changing to %s\n",
1888 virtioCoreGetStateChangeText(enmState)));
1889
1890 switch(enmState)
1891 {
1892 case kvirtIoVmStateChangedReset:
1893 virtioCoreResetAll(pVirtio);
1894 break;
1895 case kvirtIoVmStateChangedSuspend:
1896 break;
1897 case kvirtIoVmStateChangedPowerOff:
1898 break;
1899 case kvirtIoVmStateChangedResume:
1900 virtioNotifyGuestDriver(pVirtio->pDevInsR3, pVirtio, 0 /* idxQueue */);
1901 break;
1902 default:
1903 LogRelFunc(("Bad enum value"));
1904 return;
1905 }
1906}
1907
1908/**
1909 * This should be called from PDMDEVREGR3::pfnDestruct.
1910 *
1911 * @param pDevIns The device instance.
1912 * @param pVirtio Pointer to the shared virtio state.
1913 * @param pVirtioCC Pointer to the ring-3 virtio state.
1914 */
1915void virtioCoreR3Term(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, PVIRTIOCORECC pVirtioCC)
1916{
1917 if (pVirtioCC->pbPrevDevSpecificCfg)
1918 {
1919 RTMemFree(pVirtioCC->pbPrevDevSpecificCfg);
1920 pVirtioCC->pbPrevDevSpecificCfg = NULL;
1921 }
1922 RT_NOREF(pDevIns, pVirtio);
1923}
1924
1925
1926/**rr
1927 * Setup PCI device controller and Virtio state
1928 *
1929 * This should be called from PDMDEVREGR3::pfnConstruct.
1930 *
1931 * @param pDevIns The device instance.
1932 * @param pVirtio Pointer to the shared virtio state. This
1933 * must be the first member in the shared
1934 * device instance data!
1935 * @param pVirtioCC Pointer to the ring-3 virtio state. This
1936 * must be the first member in the ring-3
1937 * device instance data!
1938 * @param pPciParams Values to populate industry standard PCI Configuration Space data structure
1939 * @param pcszInstance Device instance name (format-specifier)
1940 * @param fDevSpecificFeatures VirtIO device-specific features offered by
1941 * client
1942 * @param cbDevSpecificCfg Size of virtio_pci_device_cap device-specific struct
1943 * @param pvDevSpecificCfg Address of client's dev-specific
1944 * configuration struct.
1945 */
1946int virtioCoreR3Init(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, PVIRTIOCORECC pVirtioCC, PVIRTIOPCIPARAMS pPciParams,
1947 const char *pcszInstance, uint64_t fDevSpecificFeatures, void *pvDevSpecificCfg, uint16_t cbDevSpecificCfg)
1948{
1949 /*
1950 * The pVirtio state must be the first member of the shared device instance
1951 * data, otherwise we cannot get our bearings in the PCI configuration callbacks.
1952 */
1953 AssertLogRelReturn(pVirtio == PDMINS_2_DATA(pDevIns, PVIRTIOCORE), VERR_STATE_CHANGED);
1954 AssertLogRelReturn(pVirtioCC == PDMINS_2_DATA_CC(pDevIns, PVIRTIOCORECC), VERR_STATE_CHANGED);
1955
1956 pVirtio->pDevInsR3 = pDevIns;
1957
1958 /*
1959 * Caller must initialize these.
1960 */
1961 AssertReturn(pVirtioCC->pfnStatusChanged, VERR_INVALID_POINTER);
1962 AssertReturn(pVirtioCC->pfnQueueNotified, VERR_INVALID_POINTER);
1963
1964#if 0 /* Until pdmR3DvHlp_PCISetIrq() impl is fixed and Assert that limits vec to 0 is removed */
1965# ifdef VBOX_WITH_MSI_DEVICES
1966 pVirtio->fMsiSupport = true;
1967# endif
1968#endif
1969
1970 /*
1971 * The host features offered include both device-specific features
1972 * and reserved feature bits (device independent)
1973 */
1974 pVirtio->uDeviceFeatures = VIRTIO_F_VERSION_1
1975 | VIRTIO_DEV_INDEPENDENT_FEATURES_OFFERED
1976 | fDevSpecificFeatures;
1977
1978 RTStrCopy(pVirtio->szInstance, sizeof(pVirtio->szInstance), pcszInstance);
1979
1980 pVirtio->uDeviceStatus = 0;
1981 pVirtioCC->cbDevSpecificCfg = cbDevSpecificCfg;
1982 pVirtioCC->pbDevSpecificCfg = (uint8_t *)pvDevSpecificCfg;
1983 pVirtioCC->pbPrevDevSpecificCfg = (uint8_t *)RTMemDup(pvDevSpecificCfg, cbDevSpecificCfg);
1984 AssertLogRelReturn(pVirtioCC->pbPrevDevSpecificCfg, VERR_NO_MEMORY);
1985
1986 /* Set PCI config registers (assume 32-bit mode) */
1987 PPDMPCIDEV pPciDev = pDevIns->apPciDevs[0];
1988 PDMPCIDEV_ASSERT_VALID(pDevIns, pPciDev);
1989
1990 PDMPciDevSetRevisionId(pPciDev, DEVICE_PCI_REVISION_ID_VIRTIO);
1991 PDMPciDevSetVendorId(pPciDev, DEVICE_PCI_VENDOR_ID_VIRTIO);
1992 PDMPciDevSetSubSystemVendorId(pPciDev, DEVICE_PCI_VENDOR_ID_VIRTIO);
1993 PDMPciDevSetDeviceId(pPciDev, pPciParams->uDeviceId);
1994 PDMPciDevSetClassBase(pPciDev, pPciParams->uClassBase);
1995 PDMPciDevSetClassSub(pPciDev, pPciParams->uClassSub);
1996 PDMPciDevSetClassProg(pPciDev, pPciParams->uClassProg);
1997 PDMPciDevSetSubSystemId(pPciDev, pPciParams->uSubsystemId);
1998 PDMPciDevSetInterruptLine(pPciDev, pPciParams->uInterruptLine);
1999 PDMPciDevSetInterruptPin(pPciDev, pPciParams->uInterruptPin);
2000
2001 /* Register PCI device */
2002 int rc = PDMDevHlpPCIRegister(pDevIns, pPciDev);
2003 if (RT_FAILURE(rc))
2004 return PDMDEV_SET_ERROR(pDevIns, rc, N_("virtio: cannot register PCI Device")); /* can we put params in this error? */
2005
2006 rc = PDMDevHlpPCIInterceptConfigAccesses(pDevIns, pPciDev, virtioR3PciConfigRead, virtioR3PciConfigWrite);
2007 AssertRCReturn(rc, rc);
2008
2009
2010 /* Construct & map PCI vendor-specific capabilities for virtio host negotiation with guest driver */
2011
2012 /* The following capability mapped via VirtIO 1.0: struct virtio_pci_cfg_cap (VIRTIO_PCI_CFG_CAP_T)
2013 * as a mandatory but suboptimal alternative interface to host device capabilities, facilitating
2014 * access the memory of any BAR. If the guest uses it (the VirtIO driver on Linux doesn't),
2015 * Unlike Common, Notify, ISR and Device capabilities, it is accessed directly via PCI Config region.
2016 * therefore does not contribute to the capabilities region (BAR) the other capabilities use.
2017 */
2018#define CFG_ADDR_2_IDX(addr) ((uint8_t)(((uintptr_t)(addr) - (uintptr_t)&pPciDev->abConfig[0])))
2019#define SET_PCI_CAP_LOC(a_pPciDev, a_pCfg, a_LocCap, a_uMmioLengthAlign) \
2020 do { \
2021 (a_LocCap).offMmio = (a_pCfg)->uOffset; \
2022 (a_LocCap).cbMmio = RT_ALIGN_T((a_pCfg)->uLength, a_uMmioLengthAlign, uint16_t); \
2023 (a_LocCap).offPci = (uint16_t)(uintptr_t)((uint8_t *)(a_pCfg) - &(a_pPciDev)->abConfig[0]); \
2024 (a_LocCap).cbPci = (a_pCfg)->uCapLen; \
2025 } while (0)
2026
2027 PVIRTIO_PCI_CAP_T pCfg;
2028 uint32_t cbRegion = 0;
2029
2030 /* Common capability (VirtIO 1.0 spec, section 4.1.4.3) */
2031 pCfg = (PVIRTIO_PCI_CAP_T)&pPciDev->abConfig[0x40];
2032 pCfg->uCfgType = VIRTIO_PCI_CAP_COMMON_CFG;
2033 pCfg->uCapVndr = VIRTIO_PCI_CAP_ID_VENDOR;
2034 pCfg->uCapLen = sizeof(VIRTIO_PCI_CAP_T);
2035 pCfg->uCapNext = CFG_ADDR_2_IDX(pCfg) + pCfg->uCapLen;
2036 pCfg->uBar = VIRTIO_REGION_PCI_CAP;
2037 pCfg->uOffset = RT_ALIGN_32(0, 4); /* reminder, in case someone changes offset */
2038 pCfg->uLength = sizeof(VIRTIO_PCI_COMMON_CFG_T);
2039 cbRegion += pCfg->uLength;
2040 SET_PCI_CAP_LOC(pPciDev, pCfg, pVirtio->LocCommonCfgCap, 2);
2041 pVirtioCC->pCommonCfgCap = pCfg;
2042
2043 /*
2044 * Notify capability (VirtIO 1.0 spec, section 4.1.4.4). Note: uLength is based on the choice
2045 * of this implementation to make each queue's uQueueNotifyOff equal to (QueueSelect) ordinal
2046 * value of the queue (different strategies are possible according to spec).
2047 */
2048 pCfg = (PVIRTIO_PCI_CAP_T)&pPciDev->abConfig[pCfg->uCapNext];
2049 pCfg->uCfgType = VIRTIO_PCI_CAP_NOTIFY_CFG;
2050 pCfg->uCapVndr = VIRTIO_PCI_CAP_ID_VENDOR;
2051 pCfg->uCapLen = sizeof(VIRTIO_PCI_NOTIFY_CAP_T);
2052 pCfg->uCapNext = CFG_ADDR_2_IDX(pCfg) + pCfg->uCapLen;
2053 pCfg->uBar = VIRTIO_REGION_PCI_CAP;
2054 pCfg->uOffset = pVirtioCC->pCommonCfgCap->uOffset + pVirtioCC->pCommonCfgCap->uLength;
2055 pCfg->uOffset = RT_ALIGN_32(pCfg->uOffset, 4);
2056
2057
2058 pCfg->uLength = VIRTQ_MAX_CNT * VIRTIO_NOTIFY_OFFSET_MULTIPLIER + 2; /* will change in VirtIO 1.1 */
2059 cbRegion += pCfg->uLength;
2060 SET_PCI_CAP_LOC(pPciDev, pCfg, pVirtio->LocNotifyCap, 1);
2061 pVirtioCC->pNotifyCap = (PVIRTIO_PCI_NOTIFY_CAP_T)pCfg;
2062 pVirtioCC->pNotifyCap->uNotifyOffMultiplier = VIRTIO_NOTIFY_OFFSET_MULTIPLIER;
2063
2064 /* ISR capability (VirtIO 1.0 spec, section 4.1.4.5)
2065 *
2066 * VirtIO 1.0 spec says 8-bit, unaligned in MMIO space. Example/diagram
2067 * of spec shows it as a 32-bit field with upper bits 'reserved'
2068 * Will take spec's words more literally than the diagram for now.
2069 */
2070 pCfg = (PVIRTIO_PCI_CAP_T)&pPciDev->abConfig[pCfg->uCapNext];
2071 pCfg->uCfgType = VIRTIO_PCI_CAP_ISR_CFG;
2072 pCfg->uCapVndr = VIRTIO_PCI_CAP_ID_VENDOR;
2073 pCfg->uCapLen = sizeof(VIRTIO_PCI_CAP_T);
2074 pCfg->uCapNext = CFG_ADDR_2_IDX(pCfg) + pCfg->uCapLen;
2075 pCfg->uBar = VIRTIO_REGION_PCI_CAP;
2076 pCfg->uOffset = pVirtioCC->pNotifyCap->pciCap.uOffset + pVirtioCC->pNotifyCap->pciCap.uLength;
2077 pCfg->uOffset = RT_ALIGN_32(pCfg->uOffset, 4);
2078 pCfg->uLength = sizeof(uint8_t);
2079 cbRegion += pCfg->uLength;
2080 SET_PCI_CAP_LOC(pPciDev, pCfg, pVirtio->LocIsrCap, 4);
2081 pVirtioCC->pIsrCap = pCfg;
2082
2083 /* PCI Cfg capability (VirtIO 1.0 spec, section 4.1.4.7)
2084 * This capability doesn't get page-MMIO mapped. Instead uBar, uOffset and uLength are intercepted
2085 * by trapping PCI configuration I/O and get modulated by consumers to locate fetch and read/write
2086 * values from any region. NOTE: The linux driver not only doesn't use this feature, it will not
2087 * even list it as present if uLength isn't non-zero and also 4-byte-aligned as the linux driver is
2088 * initializing.
2089 */
2090 pVirtio->uPciCfgDataOff = pCfg->uCapNext + RT_OFFSETOF(VIRTIO_PCI_CFG_CAP_T, uPciCfgData);
2091 pCfg = (PVIRTIO_PCI_CAP_T)&pPciDev->abConfig[pCfg->uCapNext];
2092 pCfg->uCfgType = VIRTIO_PCI_CAP_PCI_CFG;
2093 pCfg->uCapVndr = VIRTIO_PCI_CAP_ID_VENDOR;
2094 pCfg->uCapLen = sizeof(VIRTIO_PCI_CFG_CAP_T);
2095 pCfg->uCapNext = (pVirtio->fMsiSupport || pVirtioCC->pbDevSpecificCfg) ? CFG_ADDR_2_IDX(pCfg) + pCfg->uCapLen : 0;
2096 pCfg->uBar = 0;
2097 pCfg->uOffset = 0;
2098 pCfg->uLength = 0;
2099 cbRegion += pCfg->uLength;
2100 SET_PCI_CAP_LOC(pPciDev, pCfg, pVirtio->LocPciCfgCap, 1);
2101 pVirtioCC->pPciCfgCap = (PVIRTIO_PCI_CFG_CAP_T)pCfg;
2102
2103 if (pVirtioCC->pbDevSpecificCfg)
2104 {
2105 /* Following capability (via VirtIO 1.0, section 4.1.4.6). Client defines the
2106 * device-specific config fields struct and passes size to this constructor */
2107 pCfg = (PVIRTIO_PCI_CAP_T)&pPciDev->abConfig[pCfg->uCapNext];
2108 pCfg->uCfgType = VIRTIO_PCI_CAP_DEVICE_CFG;
2109 pCfg->uCapVndr = VIRTIO_PCI_CAP_ID_VENDOR;
2110 pCfg->uCapLen = sizeof(VIRTIO_PCI_CAP_T);
2111 pCfg->uCapNext = pVirtio->fMsiSupport ? CFG_ADDR_2_IDX(pCfg) + pCfg->uCapLen : 0;
2112 pCfg->uBar = VIRTIO_REGION_PCI_CAP;
2113 pCfg->uOffset = pVirtioCC->pIsrCap->uOffset + pVirtioCC->pIsrCap->uLength;
2114 pCfg->uOffset = RT_ALIGN_32(pCfg->uOffset, 4);
2115 pCfg->uLength = cbDevSpecificCfg;
2116 cbRegion += pCfg->uLength;
2117 SET_PCI_CAP_LOC(pPciDev, pCfg, pVirtio->LocDeviceCap, 4);
2118 pVirtioCC->pDeviceCap = pCfg;
2119 }
2120 else
2121 Assert(pVirtio->LocDeviceCap.cbMmio == 0 && pVirtio->LocDeviceCap.cbPci == 0);
2122
2123 if (pVirtio->fMsiSupport)
2124 {
2125 PDMMSIREG aMsiReg;
2126 RT_ZERO(aMsiReg);
2127 aMsiReg.iMsixCapOffset = pCfg->uCapNext;
2128 aMsiReg.iMsixNextOffset = 0;
2129 aMsiReg.iMsixBar = VIRTIO_REGION_MSIX_CAP;
2130 aMsiReg.cMsixVectors = VBOX_MSIX_MAX_ENTRIES;
2131 rc = PDMDevHlpPCIRegisterMsi(pDevIns, &aMsiReg); /* see MsixR3init() */
2132 if (RT_FAILURE(rc))
2133 {
2134 /* See PDMDevHlp.cpp:pdmR3DevHlp_PCIRegisterMsi */
2135 LogFunc(("Failed to configure MSI-X (%Rrc). Reverting to INTx\n", rc));
2136 pVirtio->fMsiSupport = false;
2137 }
2138 else
2139 Log2Func(("Using MSI-X for guest driver notification\n"));
2140 }
2141 else
2142 LogFunc(("MSI-X not available for VBox, using INTx notification\n"));
2143
2144 /* Set offset to first capability and enable PCI dev capabilities */
2145 PDMPciDevSetCapabilityList(pPciDev, 0x40);
2146 PDMPciDevSetStatus(pPciDev, VBOX_PCI_STATUS_CAP_LIST);
2147
2148 /* Linux drivers/virtio/virtio_pci_modern.c tries to map at least a page for the
2149 * 'unknown' device-specific capability without querying the capability to figure
2150 * out size, so pad with an extra page
2151 */
2152 size_t cbSize = RTStrPrintf(pVirtioCC->pcszMmioName, sizeof(pVirtioCC->pcszMmioName), "%s MMIO", pcszInstance);
2153 if (cbSize <= 0)
2154 return PDMDEV_SET_ERROR(pDevIns, rc, N_("virtio: out of memory allocating string")); /* can we put params in this error? */
2155
2156 rc = PDMDevHlpPCIIORegionCreateMmio(pDevIns, VIRTIO_REGION_PCI_CAP, RT_ALIGN_32(cbRegion + PAGE_SIZE, PAGE_SIZE),
2157 PCI_ADDRESS_SPACE_MEM, virtioMmioWrite, virtioMmioRead, pVirtio,
2158 IOMMMIO_FLAGS_READ_PASSTHRU | IOMMMIO_FLAGS_WRITE_PASSTHRU,
2159 pVirtioCC->pcszMmioName,
2160 &pVirtio->hMmioPciCap);
2161 AssertLogRelRCReturn(rc, PDMDEV_SET_ERROR(pDevIns, rc, N_("virtio: cannot register PCI Capabilities address space")));
2162 /*
2163 * Statistics.
2164 */
2165 PDMDevHlpSTAMRegisterF(pDevIns, &pVirtio->StatDescChainsAllocated, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT,
2166 "Total number of allocated descriptor chains", "DescChainsAllocated");
2167 PDMDevHlpSTAMRegisterF(pDevIns, &pVirtio->StatDescChainsFreed, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT,
2168 "Total number of freed descriptor chains", "DescChainsFreed");
2169 PDMDevHlpSTAMRegisterF(pDevIns, &pVirtio->StatDescChainsSegsIn, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT,
2170 "Total number of inbound segments", "DescChainsSegsIn");
2171 PDMDevHlpSTAMRegisterF(pDevIns, &pVirtio->StatDescChainsSegsOut, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT,
2172 "Total number of outbound segments", "DescChainsSegsOut");
2173
2174 return VINF_SUCCESS;
2175}
2176
2177#else /* !IN_RING3 */
2178
2179/**
2180 * Sets up the core ring-0/raw-mode virtio bits.
2181 *
2182 * @returns VBox status code.
2183 * @param pDevIns The device instance.
2184 * @param pVirtio Pointer to the shared virtio state. This must be the first
2185 * member in the shared device instance data!
2186 */
2187int virtioCoreRZInit(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio)
2188{
2189 AssertLogRelReturn(pVirtio == PDMINS_2_DATA(pDevIns, PVIRTIOCORE), VERR_STATE_CHANGED);
2190
2191#ifdef FUTURE_OPTIMIZATION
2192 int rc = PDMDevHlpSetDeviceCritSect(pDevIns, PDMDevHlpCritSectGetNop(pDevIns));
2193 AssertRCReturn(rc, rc);
2194#endif
2195 int rc = PDMDevHlpMmioSetUpContext(pDevIns, pVirtio->hMmioPciCap, virtioMmioWrite, virtioMmioRead, pVirtio);
2196 AssertRCReturn(rc, rc);
2197 return rc;
2198}
2199
2200#endif /* !IN_RING3 */
2201
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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