VirtualBox

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

Last change on this file since 81980 was 81975, checked in by vboxsync, 5 years ago

fix burn due to RT logging macro not registering as a use of a variable

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

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