VirtualBox

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

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

Remove debugging change that shouldn't be in code others use

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

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