VirtualBox

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

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

Move function unused in R0 into R3 guards to fix burn

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

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