VirtualBox

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

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

Copyright year updates by scm.

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

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