VirtualBox

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

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

Storage/DevVirtioSCSI.cpp: Enabled R0, increased req queue count to 4 for potential performance increased, cleaned up some comments and logging

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