VirtualBox

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

Last change on this file since 84124 was 83915, checked in by vboxsync, 5 years ago

Fix burns

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

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