VirtualBox

source: vbox/trunk/src/VBox/Devices/VirtIO/VirtioCore.cpp@ 106877

Last change on this file since 106877 was 106294, checked in by vboxsync, 7 weeks ago

VirtioNet/Core: debugger info fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 137.9 KB
Line 
1/* $Id: VirtioCore.cpp 106294 2024-10-10 17:42:05Z vboxsync $ */
2
3/** @file
4 * VirtioCore - Virtio Core (PCI, feature & config mgt, queue mgt & proxy, notification mgt)
5 */
6
7/*
8 * Copyright (C) 2009-2024 Oracle and/or its affiliates.
9 *
10 * This file is part of VirtualBox base platform packages, as
11 * available from https://www.virtualbox.org.
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation, in version 3 of the
16 * License.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, see <https://www.gnu.org/licenses>.
25 *
26 * SPDX-License-Identifier: GPL-3.0-only
27 */
28
29
30/*********************************************************************************************************************************
31* Header Files *
32*********************************************************************************************************************************/
33#define LOG_GROUP LOG_GROUP_DEV_VIRTIO
34
35#include <iprt/assert.h>
36#include <iprt/uuid.h>
37#include <iprt/mem.h>
38#include <iprt/sg.h>
39#include <iprt/assert.h>
40#include <iprt/string.h>
41#include <iprt/param.h>
42#include <iprt/types.h>
43#include <VBox/log.h>
44#include <VBox/msi.h>
45#include <iprt/types.h>
46#include <VBox/AssertGuest.h>
47#include <VBox/vmm/pdmdev.h>
48#include "VirtioCore.h"
49
50
51/*********************************************************************************************************************************
52* Defined Constants And Macros *
53*********************************************************************************************************************************/
54
55#define INSTANCE(a_pVirtio) ((a_pVirtio)->szInstance)
56#define VIRTQNAME(a_pVirtio, a_uVirtq) ((a_pVirtio)->aVirtqueues[(a_uVirtq)].szName)
57
58#define IS_VIRTQ_EMPTY(pDevIns, pVirtio, pVirtq) \
59 (virtioCoreVirtqAvailCnt(pDevIns, pVirtio, pVirtq) == 0)
60
61#define IS_DRIVER_OK(a_pVirtio) ((a_pVirtio)->fDeviceStatus & VIRTIO_STATUS_DRIVER_OK)
62#define WAS_DRIVER_OK(a_pVirtio) ((a_pVirtio)->fPrevDeviceStatus & VIRTIO_STATUS_DRIVER_OK)
63
64/**
65 * These defines are used to track guest virtio-net driver writing driver features accepted flags
66 * in two 32-bit operations (in arbitrary order), and one bit dedicated to ensured 'features complete'
67 * is handled once.
68 */
69#define DRIVER_FEATURES_0_WRITTEN 1 /**< fDriverFeatures[0] written by guest virtio-net */
70#define DRIVER_FEATURES_1_WRITTEN 2 /**< fDriverFeatures[1] written by guest virtio-net */
71#define DRIVER_FEATURES_0_AND_1_WRITTEN 3 /**< Both 32-bit parts of fDriverFeatures[] written */
72#define DRIVER_FEATURES_COMPLETE_HANDLED 4 /**< Features negotiation complete handler called */
73
74/**
75 * This macro returns true if the @a a_offAccess and access length (@a
76 * a_cbAccess) are within the range of the mapped capability struct described by
77 * @a a_LocCapData.
78 *
79 * @param[in] a_offAccess Input: The offset into the MMIO bar of the access.
80 * @param[in] a_cbAccess Input: The access size.
81 * @param[out] a_offsetIntoCap Output: uint32_t variable to return the intra-capability offset into.
82 * @param[in] a_LocCapData Input: The capability location info.
83 */
84#define MATCHES_VIRTIO_CAP_STRUCT(a_offAccess, a_cbAccess, a_offsetIntoCap, a_LocCapData) \
85 ( ((a_offsetIntoCap) = (uint32_t)((a_offAccess) - (a_LocCapData).offMmio)) < (uint32_t)(a_LocCapData).cbMmio \
86 && (a_offsetIntoCap) + (uint32_t)(a_cbAccess) <= (uint32_t)(a_LocCapData).cbMmio )
87
88
89/*********************************************************************************************************************************
90* Structures and Typedefs *
91*********************************************************************************************************************************/
92
93/** @name virtq related flags
94 * @{ */
95#define VIRTQ_DESC_F_NEXT 1 /**< Indicates this descriptor chains to next */
96#define VIRTQ_DESC_F_WRITE 2 /**< Marks buffer as write-only (default ro) */
97#define VIRTQ_DESC_F_INDIRECT 4 /**< Buffer is list of buffer descriptors */
98
99#define VIRTQ_USED_F_NO_NOTIFY 1 /**< Dev to Drv: Don't notify when buf added */
100#define VIRTQ_AVAIL_F_NO_INTERRUPT 1 /**< Drv to Dev: Don't notify when buf eaten */
101/** @} */
102
103/**
104 * virtq-related structs
105 * (struct names follow VirtIO 1.0 spec, field names use VBox styled naming, w/respective spec'd name in comments)
106 */
107typedef struct virtq_desc
108{
109 uint64_t GCPhysBuf; /**< addr GC Phys. address of buffer */
110 uint32_t cb; /**< len Buffer length */
111 uint16_t fFlags; /**< flags Buffer specific flags */
112 uint16_t uDescIdxNext; /**< next Idx set if VIRTIO_DESC_F_NEXT */
113} VIRTQ_DESC_T, *PVIRTQ_DESC_T;
114
115typedef struct virtq_avail
116{
117 uint16_t fFlags; /**< flags avail ring guest-to-host flags */
118 uint16_t uIdx; /**< idx Index of next free ring slot */
119 RT_FLEXIBLE_ARRAY_EXTENSION
120 uint16_t auRing[RT_FLEXIBLE_ARRAY]; /**< ring Ring: avail drv to dev bufs */
121 //uint16_t uUsedEventIdx; /**< used_event (if VIRTQ_USED_F_EVENT_IDX) */
122} VIRTQ_AVAIL_T, *PVIRTQ_AVAIL_T;
123
124typedef struct virtq_used_elem
125{
126 uint32_t uDescIdx; /**< idx Start of used desc chain */
127 uint32_t cbElem; /**< len Total len of used desc chain */
128} VIRTQ_USED_ELEM_T;
129
130typedef struct virt_used
131{
132 uint16_t fFlags; /**< flags used ring host-to-guest flags */
133 uint16_t uIdx; /**< idx Index of next ring slot */
134 RT_FLEXIBLE_ARRAY_EXTENSION
135 VIRTQ_USED_ELEM_T aRing[RT_FLEXIBLE_ARRAY]; /**< ring Ring: used dev to drv bufs */
136 //uint16_t uAvailEventIdx; /**< avail_event if (VIRTQ_USED_F_EVENT_IDX) */
137} VIRTQ_USED_T, *PVIRTQ_USED_T;
138
139DECLHIDDEN(const char *) virtioCoreGetStateChangeText(VIRTIOVMSTATECHANGED enmState)
140{
141 switch (enmState)
142 {
143 case kvirtIoVmStateChangedReset: return "VM RESET";
144 case kvirtIoVmStateChangedSuspend: return "VM SUSPEND";
145 case kvirtIoVmStateChangedPowerOff: return "VM POWER OFF";
146 case kvirtIoVmStateChangedResume: return "VM RESUME";
147 default: return "<BAD ENUM>";
148 }
149}
150
151/* Internal Functions */
152
153static void virtioCoreNotifyGuestDriver(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t uVirtq);
154static int virtioNudgeGuest(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint8_t uCause, uint16_t uVec);
155
156#ifdef IN_RING3
157# ifdef LOG_ENABLED
158DECLINLINE(uint16_t) virtioCoreR3CountPendingBufs(uint16_t uRingIdx, uint16_t uShadowIdx, uint16_t uQueueSize)
159{
160 if (uShadowIdx == uRingIdx)
161 return 0;
162 else
163 if (uShadowIdx > uRingIdx)
164 return uShadowIdx - uRingIdx;
165 return uQueueSize - (uRingIdx - uShadowIdx);
166}
167# endif
168#endif
169/** @name Internal queue operations
170 * @{ */
171
172/**
173 * Accessor for virtq descriptor
174 */
175#ifdef IN_RING3
176DECLINLINE(void) virtioReadDesc(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, PVIRTQUEUE pVirtq,
177 uint32_t idxDesc, PVIRTQ_DESC_T pDesc)
178{
179 /*
180 * Shut up assertion for legacy virtio-net driver in FreeBSD up to 12.3 (see virtioCoreR3VirtqUsedBufPut()
181 * for more information).
182 */
183 AssertMsg( IS_DRIVER_OK(pVirtio)
184 || ( pVirtio->fLegacyDriver
185 && pVirtq->GCPhysVirtqDesc),
186 ("Called with guest driver not ready\n"));
187 uint16_t const cVirtqItems = RT_MAX(pVirtq->uQueueSize, 1); /* Make sure to avoid div-by-zero. */
188
189 virtioCoreGCPhysRead(pVirtio, pDevIns,
190 pVirtq->GCPhysVirtqDesc + sizeof(VIRTQ_DESC_T) * (idxDesc % cVirtqItems),
191 pDesc, sizeof(VIRTQ_DESC_T));
192}
193#endif
194
195/**
196 * Accessors for virtq avail ring
197 */
198#ifdef IN_RING3
199DECLINLINE(uint16_t) virtioReadAvailDescIdx(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, PVIRTQUEUE pVirtq, uint32_t availIdx)
200{
201 uint16_t uDescIdx;
202
203 AssertMsg(pVirtio->fLegacyDriver || IS_DRIVER_OK(pVirtio), ("Called with guest driver not ready\n"));
204 uint16_t const cVirtqItems = RT_MAX(pVirtq->uQueueSize, 1); /* Make sure to avoid div-by-zero. */
205 virtioCoreGCPhysRead(pVirtio, pDevIns,
206 pVirtq->GCPhysVirtqAvail + RT_UOFFSETOF_DYN(VIRTQ_AVAIL_T, auRing[availIdx % cVirtqItems]),
207 &uDescIdx, sizeof(uDescIdx));
208 return uDescIdx;
209}
210
211DECLINLINE(uint16_t) virtioReadAvailUsedEvent(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, PVIRTQUEUE pVirtq)
212{
213 uint16_t uUsedEventIdx;
214 /* VirtIO 1.0 uUsedEventIdx (used_event) immediately follows ring */
215 AssertMsg(pVirtio->fLegacyDriver || IS_DRIVER_OK(pVirtio), ("Called with guest driver not ready\n"));
216 virtioCoreGCPhysRead(pVirtio, pDevIns,
217 pVirtq->GCPhysVirtqAvail + RT_UOFFSETOF_DYN(VIRTQ_AVAIL_T, auRing[pVirtq->uQueueSize]),
218 &uUsedEventIdx, sizeof(uUsedEventIdx));
219 return uUsedEventIdx;
220}
221#endif
222
223DECLINLINE(uint16_t) virtioReadAvailRingIdx(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, PVIRTQUEUE pVirtq)
224{
225 uint16_t uIdx = 0;
226 AssertMsg(pVirtio->fLegacyDriver || IS_DRIVER_OK(pVirtio), ("Called with guest driver not ready\n"));
227 virtioCoreGCPhysRead(pVirtio, pDevIns,
228 pVirtq->GCPhysVirtqAvail + RT_UOFFSETOF(VIRTQ_AVAIL_T, uIdx),
229 &uIdx, sizeof(uIdx));
230 return uIdx;
231}
232
233DECLINLINE(uint16_t) virtioReadAvailRingFlags(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, PVIRTQUEUE pVirtq)
234{
235 uint16_t fFlags = 0;
236 AssertMsg(pVirtio->fLegacyDriver || IS_DRIVER_OK(pVirtio), ("Called with guest driver not ready\n"));
237 virtioCoreGCPhysRead(pVirtio, pDevIns,
238 pVirtq->GCPhysVirtqAvail + RT_UOFFSETOF(VIRTQ_AVAIL_T, fFlags),
239 &fFlags, sizeof(fFlags));
240 return fFlags;
241}
242
243/** @} */
244
245/** @name Accessors for virtq used ring
246 * @{
247 */
248
249#ifdef IN_RING3
250DECLINLINE(void) virtioWriteUsedElem(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, PVIRTQUEUE pVirtq,
251 uint32_t usedIdx, uint32_t uDescIdx, uint32_t uLen)
252{
253 VIRTQ_USED_ELEM_T elem = { uDescIdx, uLen };
254 AssertMsg(pVirtio->fLegacyDriver || IS_DRIVER_OK(pVirtio), ("Called with guest driver not ready\n"));
255 uint16_t const cVirtqItems = RT_MAX(pVirtq->uQueueSize, 1); /* Make sure to avoid div-by-zero. */
256 virtioCoreGCPhysWrite(pVirtio, pDevIns,
257 pVirtq->GCPhysVirtqUsed
258 + RT_UOFFSETOF_DYN(VIRTQ_USED_T, aRing[usedIdx % cVirtqItems]),
259 &elem, sizeof(elem));
260}
261
262DECLINLINE(void) virtioWriteUsedRingFlags(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, PVIRTQUEUE pVirtq, uint16_t fFlags)
263{
264 AssertMsg(pVirtio->fLegacyDriver || IS_DRIVER_OK(pVirtio), ("Called with guest driver not ready\n"));
265 RT_UNTRUSTED_VALIDATED_FENCE(); /* VirtIO 1.0, Section 3.2.1.4.1 */
266 virtioCoreGCPhysWrite(pVirtio, pDevIns,
267 pVirtq->GCPhysVirtqUsed + RT_UOFFSETOF(VIRTQ_USED_T, fFlags),
268 &fFlags, sizeof(fFlags));
269}
270#endif
271
272DECLINLINE(void) virtioWriteUsedRingIdx(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, PVIRTQUEUE pVirtq, uint16_t uIdx)
273{
274 AssertMsg(pVirtio->fLegacyDriver || IS_DRIVER_OK(pVirtio), ("Called with guest driver not ready\n"));
275 RT_UNTRUSTED_VALIDATED_FENCE(); /* VirtIO 1.0, Section 3.2.1.4.1 */
276 virtioCoreGCPhysWrite(pVirtio, pDevIns,
277 pVirtq->GCPhysVirtqUsed + RT_UOFFSETOF(VIRTQ_USED_T, uIdx),
278 &uIdx, sizeof(uIdx));
279}
280
281#ifdef IN_RING3
282DECLINLINE(uint16_t) virtioReadUsedRingIdx(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, PVIRTQUEUE pVirtq)
283{
284 uint16_t uIdx = 0;
285 AssertMsg(pVirtio->fLegacyDriver || IS_DRIVER_OK(pVirtio), ("Called with guest driver not ready\n"));
286 virtioCoreGCPhysRead(pVirtio, pDevIns,
287 pVirtq->GCPhysVirtqUsed + RT_UOFFSETOF(VIRTQ_USED_T, uIdx),
288 &uIdx, sizeof(uIdx));
289 return uIdx;
290}
291
292DECLINLINE(uint16_t) virtioReadUsedRingFlags(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, PVIRTQUEUE pVirtq)
293{
294 uint16_t fFlags = 0;
295 AssertMsg(pVirtio->fLegacyDriver || IS_DRIVER_OK(pVirtio), ("Called with guest driver not ready\n"));
296 virtioCoreGCPhysRead(pVirtio, pDevIns,
297 pVirtq->GCPhysVirtqUsed + RT_UOFFSETOF(VIRTQ_USED_T, fFlags),
298 &fFlags, sizeof(fFlags));
299 return fFlags;
300}
301
302DECLINLINE(void) virtioWriteUsedAvailEvent(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, PVIRTQUEUE pVirtq, uint32_t uAvailEventIdx)
303{
304 /** VirtIO 1.0 uAvailEventIdx (avail_event) immediately follows ring */
305 AssertMsg(pVirtio->fLegacyDriver || IS_DRIVER_OK(pVirtio), ("Called with guest driver not ready\n"));
306 virtioCoreGCPhysWrite(pVirtio, pDevIns,
307 pVirtq->GCPhysVirtqUsed
308 + RT_UOFFSETOF_DYN(VIRTQ_USED_T, aRing[pVirtq->uQueueSize]),
309 &uAvailEventIdx, sizeof(uAvailEventIdx));
310}
311#endif
312/** @} */
313
314
315DECLINLINE(uint16_t) virtioCoreVirtqAvailCnt(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, PVIRTQUEUE pVirtq)
316{
317 uint16_t uIdxActual = virtioReadAvailRingIdx(pDevIns, pVirtio, pVirtq);
318 uint16_t uIdxShadow = pVirtq->uAvailIdxShadow;
319 uint16_t uIdxDelta;
320
321 if (uIdxActual < uIdxShadow)
322 uIdxDelta = (uIdxActual + pVirtq->uQueueSize) - uIdxShadow;
323 else
324 uIdxDelta = uIdxActual - uIdxShadow;
325
326 return uIdxDelta;
327}
328/**
329 * Get count of new (e.g. pending) elements in available ring.
330 *
331 * @param pDevIns The device instance.
332 * @param pVirtio Pointer to the shared virtio state.
333 * @param uVirtq Virtq number
334 *
335 * @returns how many entries have been added to ring as a delta of the consumer's
336 * avail index and the queue's guest-side current avail index.
337 */
338DECLHIDDEN(uint16_t) virtioCoreVirtqAvailBufCount(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t uVirtq)
339{
340 AssertMsgReturn(uVirtq < RT_ELEMENTS(pVirtio->aVirtqueues), ("uVirtq out of range"), 0);
341 PVIRTQUEUE pVirtq = &pVirtio->aVirtqueues[uVirtq];
342
343 if (!IS_DRIVER_OK(pVirtio))
344 {
345 LogRelFunc(("Driver not ready\n"));
346 return 0;
347 }
348 if (!pVirtio->fLegacyDriver && !pVirtq->uEnable)
349 {
350 LogRelFunc(("virtq: %s not enabled\n", VIRTQNAME(pVirtio, uVirtq)));
351 return 0;
352 }
353 return virtioCoreVirtqAvailCnt(pDevIns, pVirtio, pVirtq);
354}
355
356#ifdef IN_RING3
357
358static void virtioCoreR3FeatureDump(VIRTIOCORE *pVirtio, PCDBGFINFOHLP pHlp, const VIRTIO_FEATURES_LIST *s_aFeatures, int cFeatures, int fBanner)
359{
360#define MAXLINE 80
361 /* Display as a single buf to prevent interceding log messages */
362 uint16_t cbBuf = cFeatures * 132;
363 char *pszBuf = (char *)RTMemAllocZ(cbBuf);
364 Assert(pszBuf);
365 char *cp = pszBuf;
366 for (int i = 0; i < cFeatures; ++i)
367 {
368 bool isOffered = RT_BOOL(pVirtio->uDeviceFeatures & s_aFeatures[i].fFeatureBit);
369 bool isNegotiated = RT_BOOL(pVirtio->uDriverFeatures & s_aFeatures[i].fFeatureBit);
370 cp += RTStrPrintf(cp, cbBuf - (cp - pszBuf), " %s %s %s",
371 isOffered ? "+" : "-", isNegotiated ? "x" : " ", s_aFeatures[i].pcszDesc);
372 }
373 if (pHlp) {
374 if (fBanner)
375 pHlp->pfnPrintf(pHlp, "VirtIO Features Configuration\n\n"
376 " Offered Accepted Feature Description\n"
377 " ------- -------- ------- -----------\n");
378 pHlp->pfnPrintf(pHlp, "%s\n", pszBuf);
379 }
380#ifdef LOG_ENABLED
381 else
382 {
383 if (fBanner)
384 Log(("VirtIO Features Configuration\n\n"
385 " Offered Accepted Feature Description\n"
386 " ------- -------- ------- -----------\n"));
387 Log(("%s\n", pszBuf));
388 }
389#endif
390 RTMemFree(pszBuf);
391}
392
393/** API Function: See header file*/
394DECLHIDDEN(void) virtioCorePrintDeviceFeatures(VIRTIOCORE *pVirtio, PCDBGFINFOHLP pHlp,
395 const VIRTIO_FEATURES_LIST *s_aDevSpecificFeatures, int cFeatures) {
396 virtioCoreR3FeatureDump(pVirtio, pHlp, s_aCoreFeatures, RT_ELEMENTS(s_aCoreFeatures), 1 /*fBanner */);
397 virtioCoreR3FeatureDump(pVirtio, pHlp, s_aDevSpecificFeatures, cFeatures, 0 /*fBanner */);
398}
399
400#endif
401
402#ifdef LOG_ENABLED
403
404/** API Function: See header file */
405DECLHIDDEN(void) virtioCoreHexDump(uint8_t *pv, uint32_t cb, uint32_t uBase, const char *pszTitle)
406{
407#define ADJCURSOR(cb) pszOut += cb; cbRemain -= cb;
408 size_t cbPrint = 0, cbRemain = ((cb / 16) + 1) * 80;
409 char *pszBuf = (char *)RTMemAllocZ(cbRemain), *pszOut = pszBuf;
410 AssertMsgReturnVoid(pszBuf, ("Out of Memory"));
411 if (pszTitle)
412 {
413 cbPrint = RTStrPrintf(pszOut, cbRemain, "%s [%d bytes]:\n", pszTitle, cb);
414 ADJCURSOR(cbPrint);
415 }
416 for (uint32_t row = 0; row < RT_MAX(1, (cb / 16) + 1) && row * 16 < cb; row++)
417 {
418 cbPrint = RTStrPrintf(pszOut, cbRemain, "%04x: ", row * 16 + uBase); /* line address */
419 ADJCURSOR(cbPrint);
420 for (uint8_t col = 0; col < 16; col++)
421 {
422 uint32_t idx = row * 16 + col;
423 if (idx >= cb)
424 cbPrint = RTStrPrintf(pszOut, cbRemain, "-- %s", (col + 1) % 8 ? "" : " ");
425 else
426 cbPrint = RTStrPrintf(pszOut, cbRemain, "%02x %s", pv[idx], (col + 1) % 8 ? "" : " ");
427 ADJCURSOR(cbPrint);
428 }
429 for (uint32_t idx = row * 16; idx < row * 16 + 16; idx++)
430 {
431 cbPrint = RTStrPrintf(pszOut, cbRemain, "%c", (idx >= cb) ? ' ' : (pv[idx] >= 0x20 && pv[idx] <= 0x7e ? pv[idx] : '.'));
432 ADJCURSOR(cbPrint);
433 }
434 *pszOut++ = '\n';
435 --cbRemain;
436 }
437 Log(("%s\n", pszBuf));
438 RTMemFree(pszBuf);
439 RT_NOREF2(uBase, pv);
440#undef ADJCURSOR
441}
442
443/* API FUnction: See header file */
444DECLHIDDEN(void) virtioCoreGCPhysHexDump(PPDMDEVINS pDevIns, RTGCPHYS GCPhys, uint16_t cb, uint32_t uBase, const char *pszTitle)
445{
446 PVIRTIOCORE pVirtio = PDMDEVINS_2_DATA(pDevIns, PVIRTIOCORE);
447#define ADJCURSOR(cb) pszOut += cb; cbRemain -= cb;
448 size_t cbPrint = 0, cbRemain = ((cb / 16) + 1) * 80;
449 char *pszBuf = (char *)RTMemAllocZ(cbRemain), *pszOut = pszBuf;
450 AssertMsgReturnVoid(pszBuf, ("Out of Memory"));
451 if (pszTitle)
452 {
453 cbPrint = RTStrPrintf(pszOut, cbRemain, "%s [%d bytes]:\n", pszTitle, cb);
454 ADJCURSOR(cbPrint);
455 }
456 for (uint16_t row = 0; row < (uint16_t)RT_MAX(1, (cb / 16) + 1) && row * 16 < cb; row++)
457 {
458 uint8_t c;
459 cbPrint = RTStrPrintf(pszOut, cbRemain, "%04x: ", row * 16 + uBase); /* line address */
460 ADJCURSOR(cbPrint);
461 for (uint8_t col = 0; col < 16; col++)
462 {
463 uint32_t idx = row * 16 + col;
464 virtioCoreGCPhysRead(pVirtio, pDevIns, GCPhys + idx, &c, 1);
465 if (idx >= cb)
466 cbPrint = RTStrPrintf(pszOut, cbRemain, "-- %s", (col + 1) % 8 ? "" : " ");
467 else
468 cbPrint = RTStrPrintf(pszOut, cbRemain, "%02x %s", c, (col + 1) % 8 ? "" : " ");
469 ADJCURSOR(cbPrint);
470 }
471 for (uint16_t idx = row * 16; idx < row * 16 + 16; idx++)
472 {
473 virtioCoreGCPhysRead(pVirtio, pDevIns, GCPhys + idx, &c, 1);
474 cbPrint = RTStrPrintf(pszOut, cbRemain, "%c", (idx >= cb) ? ' ' : (c >= 0x20 && c <= 0x7e ? c : '.'));
475 ADJCURSOR(cbPrint);
476 }
477 *pszOut++ = '\n';
478 --cbRemain;
479 }
480 Log(("%s\n", pszBuf));
481 RTMemFree(pszBuf);
482 RT_NOREF(uBase);
483#undef ADJCURSOR
484}
485
486
487/** API function: See header file */
488DECLHIDDEN(void) virtioCoreLogMappedIoValue(const char *pszFunc, const char *pszMember, uint32_t uMemberSize,
489 const void *pv, uint32_t cb, uint32_t uOffset, int fWrite,
490 int fHasIndex, uint32_t idx)
491{
492 if (LogIs6Enabled())
493 {
494 char szIdx[16];
495 if (fHasIndex)
496 RTStrPrintf(szIdx, sizeof(szIdx), "[%d]", idx);
497 else
498 szIdx[0] = '\0';
499
500 if (cb == 1 || cb == 2 || cb == 4 || cb == 8)
501 {
502 char szDepiction[64];
503 size_t cchDepiction;
504 if (uOffset != 0 || cb != uMemberSize) /* display bounds if partial member access */
505 cchDepiction = RTStrPrintf(szDepiction, sizeof(szDepiction), "%s%s[%d:%d]",
506 pszMember, szIdx, uOffset, uOffset + cb - 1);
507 else
508 cchDepiction = RTStrPrintf(szDepiction, sizeof(szDepiction), "%s%s", pszMember, szIdx);
509
510 /* padding */
511 if (cchDepiction < 30)
512 szDepiction[cchDepiction++] = ' ';
513 while (cchDepiction < 30)
514 szDepiction[cchDepiction++] = '.';
515 szDepiction[cchDepiction] = '\0';
516
517 RTUINT64U uValue;
518 uValue.u = 0;
519 memcpy(uValue.au8, pv, cb);
520 Log6(("%-23s: Guest %s %s %#0*RX64\n",
521 pszFunc, fWrite ? "wrote" : "read ", szDepiction, 2 + cb * 2, uValue.u));
522 }
523 else /* odd number or oversized access, ... log inline hex-dump style */
524 {
525 Log6(("%-23s: Guest %s %s%s[%d:%d]: %.*Rhxs\n",
526 pszFunc, fWrite ? "wrote" : "read ", pszMember,
527 szIdx, uOffset, uOffset + cb, cb, pv));
528 }
529 }
530 RT_NOREF2(fWrite, pszFunc);
531}
532
533/**
534 * Log MMIO-mapped Virtio fDeviceStatus register bitmask, naming the bits
535 */
536DECLINLINE(void) virtioCoreFormatDeviceStatus(uint8_t bStatus, char *pszBuf, size_t uSize)
537{
538# define ADJCURSOR(len) { cp += len; uSize -= len; sep = (char *)" | "; }
539 memset(pszBuf, 0, uSize);
540 char *cp = pszBuf, *sep = (char *)"";
541 size_t len;
542 if (bStatus == 0)
543 RTStrPrintf(cp, uSize, "RESET");
544 else
545 {
546 if (bStatus & VIRTIO_STATUS_ACKNOWLEDGE)
547 {
548 len = RTStrPrintf(cp, uSize, "ACKNOWLEDGE");
549 ADJCURSOR(len);
550 }
551 if (bStatus & VIRTIO_STATUS_DRIVER)
552 {
553 len = RTStrPrintf(cp, uSize, "%sDRIVER", sep);
554 ADJCURSOR(len);
555 }
556 if (bStatus & VIRTIO_STATUS_FEATURES_OK)
557 {
558 len = RTStrPrintf(cp, uSize, "%sFEATURES_OK", sep);
559 ADJCURSOR(len);
560 }
561 if (bStatus & VIRTIO_STATUS_DRIVER_OK)
562 {
563 len = RTStrPrintf(cp, uSize, "%sDRIVER_OK", sep);
564 ADJCURSOR(len);
565 }
566 if (bStatus & VIRTIO_STATUS_FAILED)
567 {
568 len = RTStrPrintf(cp, uSize, "%sFAILED", sep);
569 ADJCURSOR(len);
570 }
571 if (bStatus & VIRTIO_STATUS_DEVICE_NEEDS_RESET)
572 RTStrPrintf(cp, uSize, "%sNEEDS_RESET", sep);
573 }
574# undef ADJCURSOR
575}
576
577#endif /* LOG_ENABLED */
578
579/** API function: See header file */
580DECLHIDDEN(int) virtioCoreIsLegacyMode(PVIRTIOCORE pVirtio)
581{
582 return pVirtio->fLegacyDriver;
583}
584
585#ifdef IN_RING3
586
587DECLHIDDEN(int) virtioCoreR3VirtqAttach(PVIRTIOCORE pVirtio, uint16_t uVirtq, const char *pcszName)
588{
589 LogFunc(("Attaching %s to VirtIO core\n", pcszName));
590 PVIRTQUEUE pVirtq = &pVirtio->aVirtqueues[uVirtq];
591 pVirtq->uVirtq = uVirtq;
592 pVirtq->fUsedRingEvent = false;
593 pVirtq->fAttached = true;
594 RTStrCopy(pVirtq->szName, sizeof(pVirtq->szName), pcszName);
595 return VINF_SUCCESS;
596}
597
598DECLHIDDEN(int) virtioCoreR3VirtqDetach(PVIRTIOCORE pVirtio, uint16_t uVirtqNbr)
599{
600 PVIRTQUEUE pVirtq = &pVirtio->aVirtqueues[uVirtqNbr];
601 pVirtq->uVirtq = 0;
602 pVirtq->uAvailIdxShadow = 0;
603 pVirtq->uUsedIdxShadow = 0;
604 pVirtq->fUsedRingEvent = false;
605 pVirtq->fAttached = false;
606 memset(pVirtq->szName, 0, sizeof(pVirtq->szName));
607 return VINF_SUCCESS;
608}
609
610DECLHIDDEN(bool) virtioCoreR3VirtqIsAttached(PVIRTIOCORE pVirtio, uint16_t uVirtqNbr)
611{
612 return pVirtio->aVirtqueues[uVirtqNbr].fAttached;
613}
614
615DECLHIDDEN(bool) virtioCoreR3VirtqIsEnabled(PVIRTIOCORE pVirtio, uint16_t uVirtqNbr)
616{
617 PVIRTQUEUE pVirtq = &pVirtio->aVirtqueues[uVirtqNbr];
618 return (bool)pVirtq->uEnable && pVirtq->GCPhysVirtqDesc;
619}
620
621DECLINLINE(void) virtioCoreR3DescInfo(PCDBGFINFOHLP pHlp, PVIRTQ_DESC_T pDesc, uint16_t iDesc, const char *cszTail)
622{
623 if (pDesc->fFlags & VIRTQ_DESC_F_NEXT)
624 pHlp->pfnPrintf(pHlp, " [%4d]%c%c %5d bytes @ %p [%4d] %s\n",
625 iDesc, pDesc->fFlags & VIRTQ_DESC_F_INDIRECT ? 'I' : ' ',
626 pDesc->fFlags & VIRTQ_DESC_F_WRITE ? 'W' : 'R',
627 pDesc->cb, pDesc->GCPhysBuf, pDesc->uDescIdxNext, cszTail);
628 else
629 pHlp->pfnPrintf(pHlp, " [%4d]%c%c %5d bytes @ %p %s\n",
630 iDesc, pDesc->fFlags & VIRTQ_DESC_F_INDIRECT ? 'I' : ' ',
631 pDesc->fFlags & VIRTQ_DESC_F_WRITE ? 'W' : 'R',
632 pDesc->cb, pDesc->GCPhysBuf, cszTail);
633}
634
635/** API Fuunction: See header file */
636DECLHIDDEN(void) virtioCoreR3VirtqInfo(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, PVIRTIOCORE pVirtio, const char *pszArgs, int uVirtq)
637{
638 RT_NOREF(pszArgs);
639 PVIRTQUEUE pVirtq = &pVirtio->aVirtqueues[uVirtq];
640
641 /** @todo add ability to dump physical contents described by any descriptor (using existing VirtIO core API function) */
642// bool fDump = pszArgs && (*pszArgs == 'd' || *pszArgs == 'D'); /* "dump" (avail phys descriptor)"
643
644 uint16_t uAvailIdx = virtioReadAvailRingIdx(pDevIns, pVirtio, pVirtq);
645 uint16_t uAvailIdxShadow = pVirtq->uAvailIdxShadow;
646
647 uint16_t uUsedIdx = virtioReadUsedRingIdx(pDevIns, pVirtio, pVirtq);
648 uint16_t uUsedIdxShadow = pVirtq->uUsedIdxShadow;
649
650 uint16_t uAvailEventIdx = 0;
651 uint16_t uUsedEventIdx = 0;
652 bool fNotify = !!(pVirtio->uDriverFeatures & VIRTIO_F_EVENT_IDX);
653 if (fNotify)
654 {
655 uUsedEventIdx = virtioReadAvailUsedEvent(pDevIns, pVirtio, pVirtq);
656 /* There is no helper for reading AvailEvent since the device is not supposed to read it. */
657 virtioCoreGCPhysRead(pVirtio, pDevIns,
658 pVirtq->GCPhysVirtqUsed
659 + RT_UOFFSETOF_DYN(VIRTQ_USED_T, aRing[pVirtq->uQueueSize]),
660 &uAvailEventIdx, sizeof(uAvailEventIdx));
661 }
662
663 VIRTQBUF_T VirtqBuf;
664 PVIRTQBUF pVirtqBuf = &VirtqBuf;
665 RT_ZERO(VirtqBuf); /* Make sure pSgPhysSend and pSgPhysReturn are initialized. */
666 bool fEmpty = IS_VIRTQ_EMPTY(pDevIns, pVirtio, pVirtq);
667
668 LogFunc(("%s, empty = %s\n", pVirtq->szName, fEmpty ? "true" : "false"));
669
670 int cSendSegs = 0, cReturnSegs = 0;
671 if (!fEmpty)
672 {
673 virtioCoreR3VirtqAvailBufPeek(pDevIns, pVirtio, uVirtq, pVirtqBuf);
674 cSendSegs = pVirtqBuf->pSgPhysSend ? pVirtqBuf->pSgPhysSend->cSegs : 0;
675 cReturnSegs = pVirtqBuf->pSgPhysReturn ? pVirtqBuf->pSgPhysReturn->cSegs : 0;
676 }
677
678 bool fAvailNoInterrupt = virtioReadAvailRingFlags(pDevIns, pVirtio, pVirtq) & VIRTQ_AVAIL_F_NO_INTERRUPT;
679 bool fUsedNoNotify = virtioReadUsedRingFlags(pDevIns, pVirtio, pVirtq) & VIRTQ_USED_F_NO_NOTIFY;
680
681 pHlp->pfnPrintf(pHlp, " queue enabled: ........... %s\n", pVirtq->uEnable ? "true" : "false");
682 pHlp->pfnPrintf(pHlp, " size: .................... %d\n", pVirtq->uQueueSize);
683 pHlp->pfnPrintf(pHlp, " notify offset: ........... %d\n", pVirtq->uNotifyOffset);
684 if (pVirtio->fMsiSupport)
685 pHlp->pfnPrintf(pHlp, " MSIX vector: ....... %4.4x\n", pVirtq->uMsixVector);
686 pHlp->pfnPrintf(pHlp, "\n");
687 pHlp->pfnPrintf(pHlp, " avail ring (%d entries):\n", uAvailIdx - uAvailIdxShadow);
688 pHlp->pfnPrintf(pHlp, " index: ................ %d (%d)\n", pVirtq->uQueueSize ? uAvailIdx % pVirtq->uQueueSize : uAvailIdx, uAvailIdx);
689 pHlp->pfnPrintf(pHlp, " shadow: ............... %d (%d)\n", pVirtq->uQueueSize ? uAvailIdxShadow % pVirtq->uQueueSize : uAvailIdxShadow, uAvailIdxShadow);
690 pHlp->pfnPrintf(pHlp, " flags: ................ %s\n", fAvailNoInterrupt ? "NO_INTERRUPT" : "");
691 pHlp->pfnPrintf(pHlp, "\n");
692 pHlp->pfnPrintf(pHlp, " used ring (%d entries):\n", uUsedIdxShadow - uUsedIdx);
693 pHlp->pfnPrintf(pHlp, " index: ................ %d (%d)\n", pVirtq->uQueueSize ? uUsedIdx % pVirtq->uQueueSize : uUsedIdx, uUsedIdx);
694 pHlp->pfnPrintf(pHlp, " shadow: ............... %d (%d)\n", pVirtq->uQueueSize ? uUsedIdxShadow % pVirtq->uQueueSize : uUsedIdxShadow, uUsedIdxShadow);
695 pHlp->pfnPrintf(pHlp, " flags: ................ %s\n", fUsedNoNotify ? "NO_NOTIFY" : "");
696 pHlp->pfnPrintf(pHlp, "\n");
697 if (!fEmpty)
698 {
699 pHlp->pfnPrintf(pHlp, " desc chain:\n");
700 pHlp->pfnPrintf(pHlp, " head idx: ............. %d (%d)\n", pVirtq->uQueueSize ? uUsedIdx % pVirtq->uQueueSize : uUsedIdx, uUsedIdx);
701 pHlp->pfnPrintf(pHlp, " segs: ................. %d\n", cSendSegs + cReturnSegs);
702 pHlp->pfnPrintf(pHlp, " refCnt ................ %d\n", pVirtqBuf->cRefs);
703 pHlp->pfnPrintf(pHlp, "\n");
704 pHlp->pfnPrintf(pHlp, " host-to-guest (%d bytes):\n", pVirtqBuf->cbPhysSend);
705 pHlp->pfnPrintf(pHlp, " segs: .............. %d\n", cSendSegs);
706 if (cSendSegs)
707 {
708 pHlp->pfnPrintf(pHlp, " index: ............. %d\n", pVirtqBuf->pSgPhysSend->idxSeg);
709 pHlp->pfnPrintf(pHlp, " unsent ............. %d\n", pVirtqBuf->pSgPhysSend->cbSegLeft);
710 }
711 pHlp->pfnPrintf(pHlp, "\n");
712 pHlp->pfnPrintf(pHlp, " guest-to-host (%d bytes):\n", pVirtqBuf->cbPhysReturn);
713 pHlp->pfnPrintf(pHlp, " segs: .............. %d\n", cReturnSegs);
714 if (cReturnSegs)
715 {
716 pHlp->pfnPrintf(pHlp, " index: ............. %d\n", pVirtqBuf->pSgPhysReturn->idxSeg);
717 pHlp->pfnPrintf(pHlp, " unsent ............. %d\n", pVirtqBuf->pSgPhysReturn->cbSegLeft);
718 }
719 } else
720 pHlp->pfnPrintf(pHlp, " no desc chains available\n");
721 pHlp->pfnPrintf(pHlp, "\n");
722
723 /* Avoid handling zero-sized queues, there is nothing to show anyway. */
724 if (pVirtq->uQueueSize == 0)
725 return;
726
727 pHlp->pfnPrintf(pHlp, " desc table:\n");
728 /*
729 * Each line in the descriptor table output consists of two parts: a fixed part and a variable "tail".
730 * The fixed part shows the descriptor index, its writability, size, physical address, and optionally
731 * which descriptor is next the chain. The tail shows which elements of avail/used rings point to
732 * this descriptor.
733 */
734 VIRTQ_DESC_T descTable[VIRTQ_SIZE];
735 char aszTails[VIRTQ_SIZE][32];
736 virtioCoreGCPhysRead(pVirtio, pDevIns, pVirtq->GCPhysVirtqDesc,
737 &descTable, sizeof(VIRTQ_DESC_T) * pVirtq->uQueueSize);
738 RT_BZERO(aszTails, sizeof(aszTails)); /* No tails by default */
739
740 /* Fill avail tail fields. */
741
742 /* The first available descriptor gets outer reverse angle brackets. */
743 char chOuterLeft = '>', chOuterRight = '<';
744 char chLeft = '[', chRight = ']';
745 /* Use 'not-equal' instead of 'less' because of uint16_t wrapping! */
746 for (uint16_t i = uAvailIdxShadow; i != uAvailIdx; i++)
747 {
748 /* The last descriptor gets inner curly braces, inner square brackets for the rest. */
749 if (i + 1 == uAvailIdx) { chLeft = '{'; chRight = '}'; }
750 uint16_t uDescIdx = virtioReadAvailDescIdx(pDevIns, pVirtio, pVirtq, i);
751 /* Print an exclamation sign instead of outer right bracket if this descriptor triggers notification. */
752 RTStrPrintf(aszTails[uDescIdx], sizeof(aszTails[0]), "%c%c%4d%c%c ",
753 chOuterLeft, chLeft, i % pVirtq->uQueueSize, chRight,
754 fNotify ? ((i % pVirtq->uQueueSize) == (uAvailEventIdx % pVirtq->uQueueSize) ? '!' : chOuterRight) : chOuterRight);
755 chOuterLeft = chOuterRight = ' ';
756 }
757
758 /* Fill used tail fields, see comments in the similar loop above. */
759
760 chOuterLeft = '>'; chOuterRight = '<';
761 chLeft = '['; chRight = ']';
762 for (uint16_t i = uUsedIdx; i != uUsedIdxShadow; i++)
763 {
764 VIRTQ_USED_ELEM_T elem;
765 virtioCoreGCPhysRead(pVirtio, pDevIns,
766 pVirtq->GCPhysVirtqUsed
767 + RT_UOFFSETOF_DYN(VIRTQ_USED_T, aRing[i % pVirtq->uQueueSize]),
768 &elem, sizeof(elem));
769 if (i + 1 == uUsedIdxShadow) { chLeft = '{'; chRight = '}'; }
770 char *szTail = aszTails[elem.uDescIdx % pVirtq->uQueueSize];
771 /* Add empty avail field if none is present, 9 spaces + terminating zero. */
772 if (*szTail == '\0')
773 RTStrCopy(szTail, 10, " ");
774 RTStrPrintf(szTail + 9, sizeof(aszTails[0]) - 9, " %c%c%4d%c%c %d bytes",
775 chOuterLeft, chLeft, i % pVirtq->uQueueSize, chRight,
776 fNotify ? ((i % pVirtq->uQueueSize) == (uUsedEventIdx % pVirtq->uQueueSize) ? '!' : chOuterRight) : chOuterRight,
777 elem.cbElem);
778 chOuterLeft = chOuterRight = ' ';
779 }
780
781 pHlp->pfnPrintf(pHlp, " index w/r size phys addr next @avail @used\n");
782 pHlp->pfnPrintf(pHlp, " ------ - ----------- ---------------- ------- -------- ------------------\n");
783 for (uint16_t i = 0; i < pVirtq->uQueueSize; i++)
784 virtioCoreR3DescInfo(pHlp, &descTable[i], i, aszTails[i]);
785}
786
787
788/** API Function: See header file */
789DECLHIDDEN(PVIRTQBUF) virtioCoreR3VirtqBufAlloc(void)
790{
791 PVIRTQBUF pVirtqBuf = (PVIRTQBUF)RTMemAllocZ(sizeof(VIRTQBUF_T));
792 AssertReturn(pVirtqBuf, NULL);
793 pVirtqBuf->u32Magic = VIRTQBUF_MAGIC;
794 pVirtqBuf->cRefs = 1;
795 return pVirtqBuf;
796}
797
798
799/** API Function: See header file */
800DECLHIDDEN(uint32_t) virtioCoreR3VirtqBufRetain(PVIRTQBUF pVirtqBuf)
801{
802 AssertReturn(pVirtqBuf, UINT32_MAX);
803 AssertReturn(pVirtqBuf->u32Magic == VIRTQBUF_MAGIC, UINT32_MAX);
804 uint32_t cRefs = ASMAtomicIncU32(&pVirtqBuf->cRefs);
805 Assert(cRefs > 1);
806 Assert(cRefs < 16);
807 return cRefs;
808}
809
810/** API Function: See header file */
811DECLHIDDEN(uint32_t) virtioCoreR3VirtqBufRelease(PVIRTIOCORE pVirtio, PVIRTQBUF pVirtqBuf)
812{
813 if (!pVirtqBuf)
814 return 0;
815 AssertReturn(pVirtqBuf, 0);
816 AssertReturn(pVirtqBuf->u32Magic == VIRTQBUF_MAGIC, 0);
817 uint32_t cRefs = ASMAtomicDecU32(&pVirtqBuf->cRefs);
818 Assert(cRefs < 16);
819 if (cRefs == 0)
820 {
821 pVirtqBuf->u32Magic = ~VIRTQBUF_MAGIC;
822 RTMemFree(pVirtqBuf);
823#ifdef VBOX_WITH_STATISTICS
824 STAM_REL_COUNTER_INC(&pVirtio->StatDescChainsFreed);
825#endif
826 }
827 RT_NOREF(pVirtio);
828 return cRefs;
829}
830
831/** API Function: See header file */
832DECLHIDDEN(void) virtioCoreNotifyConfigChanged(PVIRTIOCORE pVirtio)
833{
834 virtioNudgeGuest(pVirtio->pDevInsR3, pVirtio, VIRTIO_ISR_DEVICE_CONFIG, pVirtio->uMsixConfig);
835}
836
837
838/** API Function: See header file */
839DECLHIDDEN(void) virtioCoreVirtqEnableNotify(PVIRTIOCORE pVirtio, uint16_t uVirtq, bool fEnable)
840{
841 Assert(uVirtq < RT_ELEMENTS(pVirtio->aVirtqueues));
842 PVIRTQUEUE pVirtq = &pVirtio->aVirtqueues[uVirtq];
843
844 if (IS_DRIVER_OK(pVirtio))
845 {
846 uint16_t fFlags = virtioReadUsedRingFlags(pVirtio->pDevInsR3, pVirtio, pVirtq);
847
848 if (fEnable)
849 fFlags &= ~VIRTQ_USED_F_NO_NOTIFY;
850 else
851 fFlags |= VIRTQ_USED_F_NO_NOTIFY;
852
853 virtioWriteUsedRingFlags(pVirtio->pDevInsR3, pVirtio, pVirtq, fFlags);
854 }
855}
856
857/** API function: See Header file */
858DECLHIDDEN(void) virtioCoreResetAll(PVIRTIOCORE pVirtio)
859{
860 LogFunc(("\n"));
861 pVirtio->fDeviceStatus |= VIRTIO_STATUS_DEVICE_NEEDS_RESET;
862 if (IS_DRIVER_OK(pVirtio))
863 {
864 if (!pVirtio->fLegacyDriver)
865 pVirtio->fGenUpdatePending = true;
866 virtioNudgeGuest(pVirtio->pDevInsR3, pVirtio, VIRTIO_ISR_DEVICE_CONFIG, pVirtio->uMsixConfig);
867 }
868}
869
870/** API function: See Header file */
871DECLHIDDEN(int) virtioCoreR3VirtqAvailBufPeek(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t uVirtq, PVIRTQBUF pVirtqBuf)
872{
873 return virtioCoreR3VirtqAvailBufGet(pDevIns, pVirtio, uVirtq, pVirtqBuf, false);
874}
875
876
877/** API function: See Header file */
878DECLHIDDEN(int) virtioCoreR3VirtqAvailBufNext(PVIRTIOCORE pVirtio, uint16_t uVirtq)
879{
880 Assert(uVirtq < RT_ELEMENTS(pVirtio->aVirtqueues));
881 PVIRTQUEUE pVirtq = &pVirtio->aVirtqueues[uVirtq];
882
883 if (!pVirtio->fLegacyDriver)
884 AssertMsgReturn((pVirtio->fDeviceStatus & VIRTIO_STATUS_DRIVER_OK) && pVirtq->uEnable,
885 ("Guest driver not in ready state.\n"), VERR_INVALID_STATE);
886
887 if (IS_VIRTQ_EMPTY(pVirtio->pDevInsR3, pVirtio, pVirtq))
888 return VERR_NOT_AVAILABLE;
889
890 Log6Func(("%s avail shadow idx: %u\n", pVirtq->szName, pVirtq->uAvailIdxShadow));
891 pVirtq->uAvailIdxShadow++;
892
893 return VINF_SUCCESS;
894}
895
896/** API Function: See header file */
897DECLHIDDEN(int) virtioCoreR3VirtqAvailBufGet(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t uVirtq,
898 uint16_t uHeadIdx, PVIRTQBUF pVirtqBuf)
899{
900 AssertMsgReturn(uVirtq < RT_ELEMENTS(pVirtio->aVirtqueues),
901 ("uVirtq out of range"), VERR_INVALID_PARAMETER);
902
903 PVIRTQUEUE pVirtq = &pVirtio->aVirtqueues[uVirtq];
904
905 if (!pVirtio->fLegacyDriver)
906 AssertMsgReturn((pVirtio->fDeviceStatus & VIRTIO_STATUS_DRIVER_OK) && pVirtq->uEnable,
907 ("Guest driver not in ready state.\n"), VERR_INVALID_STATE);
908
909 uint16_t uDescIdx = uHeadIdx;
910
911 Log6Func(("%s DESC CHAIN: (head idx = %u)\n", pVirtio->aVirtqueues[uVirtq].szName, uHeadIdx));
912
913 /*
914 * Allocate and initialize the descriptor chain structure.
915 */
916 pVirtqBuf->u32Magic = VIRTQBUF_MAGIC;
917 pVirtqBuf->cRefs = 1;
918 pVirtqBuf->uHeadIdx = uHeadIdx;
919 pVirtqBuf->uVirtq = uVirtq;
920
921 /*
922 * Gather segments.
923 */
924 VIRTQ_DESC_T desc;
925
926 uint32_t cbIn = 0;
927 uint32_t cbOut = 0;
928 uint32_t cSegsIn = 0;
929 uint32_t cSegsOut = 0;
930
931 PVIRTIOSGSEG paSegsIn = pVirtqBuf->aSegsIn;
932 PVIRTIOSGSEG paSegsOut = pVirtqBuf->aSegsOut;
933
934 do
935 {
936 PVIRTIOSGSEG pSeg;
937 /*
938 * Malicious guests may go beyond paSegsIn or paSegsOut boundaries by linking
939 * several descriptors into a loop. Since there is no legitimate way to get a sequences of
940 * linked descriptors exceeding the total number of descriptors in the ring (see @bugref{8620}),
941 * the following aborts I/O if breach and employs a simple log throttling algorithm to notify.
942 */
943 if (cSegsIn + cSegsOut >= pVirtq->uQueueSize)
944 {
945 static volatile uint32_t s_cMessages = 0;
946 static volatile uint32_t s_cThreshold = 1;
947 if (ASMAtomicIncU32(&s_cMessages) == ASMAtomicReadU32(&s_cThreshold))
948 {
949 LogRelMax(64, ("Too many linked descriptors; check if the guest arranges descriptors in a loop (cSegsIn=%u cSegsOut=%u uQueueSize=%u uDescIdx=%u queue=%s).\n",
950 cSegsIn, cSegsOut, pVirtq->uQueueSize, uDescIdx, pVirtq->szName));
951 if (ASMAtomicReadU32(&s_cMessages) != 1)
952 LogRelMax(64, ("(the above error has occured %u times so far)\n", ASMAtomicReadU32(&s_cMessages)));
953 ASMAtomicWriteU32(&s_cThreshold, ASMAtomicReadU32(&s_cThreshold) * 10);
954 }
955 break;
956 }
957 /* Check if the limit has been reached for input chain (see section 2.4.4.1 of virtio 1.0 spec). */
958 if (cSegsIn >= RT_ELEMENTS(pVirtqBuf->aSegsIn))
959 {
960 LogRelMax(64, ("Too many input descriptors (cSegsIn=%u).\n", cSegsIn));
961 break;
962 }
963 /* Check if the limit has been reached for output chain (see section 2.4.4.1 of virtio 1.0 spec). */
964 if (cSegsOut >= RT_ELEMENTS(pVirtqBuf->aSegsOut))
965 {
966 LogRelMax(64, ("Too many output descriptors (cSegsOut=%u).\n", cSegsOut));
967 break;
968 }
969 RT_UNTRUSTED_VALIDATED_FENCE();
970
971 virtioReadDesc(pDevIns, pVirtio, pVirtq, uDescIdx, &desc);
972
973 if (desc.fFlags & VIRTQ_DESC_F_WRITE)
974 {
975 Log6Func(("%s IN idx=%-4u seg=%-3u addr=%RGp cb=%u\n", pVirtq->szName, uDescIdx, cSegsIn, desc.GCPhysBuf, desc.cb));
976 cbIn += desc.cb;
977 pSeg = &paSegsIn[cSegsIn++];
978 }
979 else
980 {
981 Log6Func(("%s OUT desc_idx=%-4u seg=%-3u addr=%RGp cb=%u\n", pVirtq->szName, uDescIdx, cSegsOut, desc.GCPhysBuf, desc.cb));
982 cbOut += desc.cb;
983 pSeg = &paSegsOut[cSegsOut++];
984#ifdef DEEP_DEBUG
985 if (LogIs11Enabled())
986 {
987 virtioCoreGCPhysHexDump(pDevIns, desc.GCPhysBuf, desc.cb, 0, NULL);
988 Log(("\n"));
989 }
990#endif
991 }
992 pSeg->GCPhys = desc.GCPhysBuf;
993 pSeg->cbSeg = desc.cb;
994 uDescIdx = desc.uDescIdxNext;
995 } while (desc.fFlags & VIRTQ_DESC_F_NEXT);
996
997 /*
998 * Add segments to the descriptor chain structure.
999 */
1000 if (cSegsIn)
1001 {
1002 virtioCoreGCPhysChainInit(&pVirtqBuf->SgBufIn, paSegsIn, cSegsIn);
1003 pVirtqBuf->pSgPhysReturn = &pVirtqBuf->SgBufIn;
1004 pVirtqBuf->cbPhysReturn = cbIn;
1005#ifdef VBOX_WITH_STATISTICS
1006 STAM_REL_COUNTER_ADD(&pVirtio->StatDescChainsSegsIn, cSegsIn);
1007#endif
1008 }
1009
1010 if (cSegsOut)
1011 {
1012 virtioCoreGCPhysChainInit(&pVirtqBuf->SgBufOut, paSegsOut, cSegsOut);
1013 pVirtqBuf->pSgPhysSend = &pVirtqBuf->SgBufOut;
1014 pVirtqBuf->cbPhysSend = cbOut;
1015#ifdef VBOX_WITH_STATISTICS
1016 STAM_REL_COUNTER_ADD(&pVirtio->StatDescChainsSegsOut, cSegsOut);
1017#endif
1018 }
1019
1020#ifdef VBOX_WITH_STATISTICS
1021 STAM_REL_COUNTER_INC(&pVirtio->StatDescChainsAllocated);
1022#endif
1023 Log6Func(("%s -- segs OUT: %u (%u bytes) IN: %u (%u bytes) --\n",
1024 pVirtq->szName, cSegsOut, cbOut, cSegsIn, cbIn));
1025
1026 return VINF_SUCCESS;
1027}
1028
1029/** API function: See Header file */
1030DECLHIDDEN(int) virtioCoreR3VirtqAvailBufGet(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t uVirtq,
1031 PVIRTQBUF pVirtqBuf, bool fRemove)
1032{
1033 Assert(uVirtq < RT_ELEMENTS(pVirtio->aVirtqueues));
1034 PVIRTQUEUE pVirtq = &pVirtio->aVirtqueues[uVirtq];
1035
1036 if (IS_VIRTQ_EMPTY(pDevIns, pVirtio, pVirtq))
1037 return VERR_NOT_AVAILABLE;
1038
1039 uint16_t uHeadIdx = virtioReadAvailDescIdx(pDevIns, pVirtio, pVirtq, pVirtq->uAvailIdxShadow);
1040
1041 if (pVirtio->uDriverFeatures & VIRTIO_F_EVENT_IDX)
1042 virtioWriteUsedAvailEvent(pDevIns,pVirtio, pVirtq, pVirtq->uAvailIdxShadow + 1);
1043
1044 if (fRemove)
1045 pVirtq->uAvailIdxShadow++;
1046
1047 return virtioCoreR3VirtqAvailBufGet(pDevIns, pVirtio, uVirtq, uHeadIdx, pVirtqBuf);
1048}
1049
1050/** API function: See Header file */
1051DECLHIDDEN(int) virtioCoreR3VirtqUsedBufPut(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t uVirtq, PRTSGBUF pSgVirtReturn,
1052 PVIRTQBUF pVirtqBuf, bool fFence)
1053{
1054 Assert(uVirtq < RT_ELEMENTS(pVirtio->aVirtqueues));
1055 PVIRTQUEUE pVirtq = &pVirtio->aVirtqueues[uVirtq];
1056
1057 PVIRTIOSGBUF pSgPhysReturn = pVirtqBuf->pSgPhysReturn;
1058
1059 Assert(pVirtqBuf->u32Magic == VIRTQBUF_MAGIC);
1060 Assert(pVirtqBuf->cRefs > 0);
1061
1062 /*
1063 * Workaround for a bug in FreeBSD's virtio-net driver up until 12.3 which supports only the legacy style devive.
1064 * When the device is re-initialized from the driver it violates the spec and posts commands to the control queue
1065 * before setting the DRIVER_OK flag, breaking the following check and rendering the device non-functional.
1066 * The queues are properly set up at this stage however so no real harm is done and we can safely continue here,
1067 * for the legacy device only of course after making sure the queue is properly set up.
1068 */
1069 AssertMsgReturn( IS_DRIVER_OK(pVirtio)
1070 || ( pVirtio->fLegacyDriver
1071 && pVirtq->GCPhysVirtqDesc),
1072 ("Guest driver not in ready state.\n"), VERR_INVALID_STATE);
1073
1074 Log6Func((" Copying device data to %s, [desc:%u -> used ring:%u]\n",
1075 VIRTQNAME(pVirtio, uVirtq), pVirtqBuf->uHeadIdx, pVirtq->uUsedIdxShadow));
1076
1077 /* Copy s/g buf (virtual memory) to guest phys mem (VirtIO "IN" direction). */
1078
1079 size_t cbCopy = 0, cbTotal = 0, cbRemain = 0;
1080
1081 /** @todo r=aeichner Check whether VirtIO should return an error if the device wants to return data but
1082 * the guest didn't set up an IN buffer. */
1083 if ( pSgVirtReturn
1084 && pSgPhysReturn)
1085 {
1086 size_t cbTarget = virtioCoreGCPhysChainCalcBufSize(pSgPhysReturn);
1087 cbRemain = cbTotal = RTSgBufCalcTotalLength(pSgVirtReturn);
1088 AssertMsgReturn(cbTarget >= cbRemain, ("No space to write data to phys memory"), VERR_BUFFER_OVERFLOW);
1089 virtioCoreGCPhysChainReset(pSgPhysReturn);
1090 while (cbRemain)
1091 {
1092 cbCopy = RT_MIN(pSgVirtReturn->cbSegLeft, pSgPhysReturn->cbSegLeft);
1093 AssertReturn(cbCopy > 0, VERR_INVALID_PARAMETER);
1094 virtioCoreGCPhysWrite(pVirtio, pDevIns, (RTGCPHYS)pSgPhysReturn->GCPhysCur, pSgVirtReturn->pvSegCur, cbCopy);
1095 RTSgBufAdvance(pSgVirtReturn, cbCopy);
1096 virtioCoreGCPhysChainAdvance(pSgPhysReturn, cbCopy);
1097 cbRemain -= cbCopy;
1098 }
1099
1100 if (fFence)
1101 RT_UNTRUSTED_NONVOLATILE_COPY_FENCE(); /* needed? */
1102
1103 Assert(!(cbCopy >> 32));
1104 }
1105
1106 /* Flag if write-ahead crosses threshold where guest driver indicated it wants event notification */
1107 if (pVirtio->uDriverFeatures & VIRTIO_F_EVENT_IDX)
1108 if (pVirtq->uUsedIdxShadow == virtioReadAvailUsedEvent(pDevIns, pVirtio, pVirtq))
1109 pVirtq->fUsedRingEvent = true;
1110
1111 /*
1112 * Place used buffer's descriptor in used ring but don't update used ring's slot index.
1113 * That will be done with a subsequent client call to virtioCoreVirtqUsedRingSync()
1114 *
1115 * @todo r=aeichner: The increment of the shadow index is not atomic but this code can be called
1116 * concurrently!!
1117 */
1118 virtioWriteUsedElem(pDevIns, pVirtio, pVirtq, pVirtq->uUsedIdxShadow++, pVirtqBuf->uHeadIdx, (uint32_t)cbTotal);
1119
1120#ifdef LOG_ENABLED
1121 if ( LogIs6Enabled()
1122 && pSgVirtReturn
1123 && pSgPhysReturn)
1124 {
1125
1126 LogFunc((" ... %d segs, %zu bytes, copied to %u byte buf@offset=%u. Residual: %zu bytes\n",
1127 pSgVirtReturn->cSegs, cbTotal - cbRemain, pVirtqBuf->cbPhysReturn,
1128 ((virtioCoreGCPhysChainCalcBufSize(pVirtqBuf->pSgPhysReturn) -
1129 virtioCoreGCPhysChainCalcLengthLeft(pVirtqBuf->pSgPhysReturn)) - (cbTotal - cbRemain)),
1130 virtioCoreGCPhysChainCalcLengthLeft(pVirtqBuf->pSgPhysReturn) ));
1131
1132 uint16_t uPending = virtioCoreR3CountPendingBufs(
1133 virtioReadUsedRingIdx(pDevIns, pVirtio, pVirtq),
1134 pVirtq->uUsedIdxShadow, pVirtq->uQueueSize);
1135
1136 LogFunc((" %u used buf%s not synced in %s\n", uPending, uPending == 1 ? "" : "s ",
1137 VIRTQNAME(pVirtio, uVirtq)));
1138 }
1139#endif
1140 return VINF_SUCCESS;
1141}
1142
1143/** API function: See Header file */
1144DECLHIDDEN(int) virtioCoreR3VirtqUsedBufPut(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t uVirtq,
1145 size_t cb, void const *pv, PVIRTQBUF pVirtqBuf, size_t cbEnqueue, bool fFence)
1146{
1147 Assert(uVirtq < RT_ELEMENTS(pVirtio->aVirtqueues));
1148 Assert(pv);
1149
1150 PVIRTQUEUE pVirtq = &pVirtio->aVirtqueues[uVirtq];
1151 PVIRTIOSGBUF pSgPhysReturn = pVirtqBuf->pSgPhysReturn;
1152
1153 Assert(pVirtqBuf->u32Magic == VIRTQBUF_MAGIC);
1154 Assert(pVirtqBuf->cRefs > 0);
1155
1156 AssertMsgReturn(IS_DRIVER_OK(pVirtio), ("Guest driver not in ready state.\n"), VERR_INVALID_STATE);
1157
1158 Log6Func((" Copying device data to %s, [desc chain head idx:%u]\n",
1159 VIRTQNAME(pVirtio, uVirtq), pVirtqBuf->uHeadIdx));
1160 /*
1161 * Convert virtual memory simple buffer to guest physical memory (VirtIO descriptor chain)
1162 */
1163 uint8_t *pvBuf = (uint8_t *)pv;
1164 size_t cbRemain = cb, cbCopy = 0;
1165 while (cbRemain)
1166 {
1167 cbCopy = RT_MIN(pSgPhysReturn->cbSegLeft, cbRemain);
1168 Assert(cbCopy > 0);
1169 virtioCoreGCPhysWrite(pVirtio, pDevIns, (RTGCPHYS)pSgPhysReturn->GCPhysCur, pvBuf, cbCopy);
1170 virtioCoreGCPhysChainAdvance(pSgPhysReturn, cbCopy);
1171 pvBuf += cbCopy;
1172 cbRemain -= cbCopy;
1173 }
1174 LogFunc((" ...%zu bytes, copied to %u byte buf@offset=%u. Residual: %zu bytes\n",
1175 cb , pVirtqBuf->cbPhysReturn,
1176 ((virtioCoreGCPhysChainCalcBufSize(pVirtqBuf->pSgPhysReturn) -
1177 virtioCoreGCPhysChainCalcLengthLeft(pVirtqBuf->pSgPhysReturn)) - cb),
1178 virtioCoreGCPhysChainCalcLengthLeft(pVirtqBuf->pSgPhysReturn)));
1179
1180 if (cbEnqueue)
1181 {
1182 if (fFence)
1183 {
1184 RT_UNTRUSTED_NONVOLATILE_COPY_FENCE(); /* needed? */
1185 Assert(!(cbCopy >> 32));
1186 }
1187 /* Flag if write-ahead crosses threshold where guest driver indicated it wants event notification */
1188 if (pVirtio->uDriverFeatures & VIRTIO_F_EVENT_IDX)
1189 if (pVirtq->uUsedIdxShadow == virtioReadAvailUsedEvent(pDevIns, pVirtio, pVirtq))
1190 pVirtq->fUsedRingEvent = true;
1191 /*
1192 * Place used buffer's descriptor in used ring but don't update used ring's slot index.
1193 * That will be done with a subsequent client call to virtioCoreVirtqUsedRingSync()
1194 */
1195 Log6Func((" Enqueue desc chain head idx %u to %s used ring @ %u\n", pVirtqBuf->uHeadIdx,
1196 VIRTQNAME(pVirtio, uVirtq), pVirtq->uUsedIdxShadow));
1197
1198 virtioWriteUsedElem(pDevIns, pVirtio, pVirtq, pVirtq->uUsedIdxShadow++, pVirtqBuf->uHeadIdx, (uint32_t)cbEnqueue);
1199
1200#ifdef LOG_ENABLED
1201 if (LogIs6Enabled())
1202 {
1203 uint16_t uPending = virtioCoreR3CountPendingBufs(
1204 virtioReadUsedRingIdx(pDevIns, pVirtio, pVirtq),
1205 pVirtq->uUsedIdxShadow, pVirtq->uQueueSize);
1206
1207 LogFunc((" %u used buf%s not synced in %s\n",
1208 uPending, uPending == 1 ? "" : "s ", VIRTQNAME(pVirtio, uVirtq)));
1209 }
1210#endif
1211 } /* fEnqueue */
1212
1213 return VINF_SUCCESS;
1214}
1215
1216
1217#endif /* IN_RING3 */
1218
1219/** API function: See Header file */
1220DECLHIDDEN(int) virtioCoreVirtqUsedRingSync(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t uVirtq)
1221{
1222 Assert(uVirtq < RT_ELEMENTS(pVirtio->aVirtqueues));
1223 PVIRTQUEUE pVirtq = &pVirtio->aVirtqueues[uVirtq];
1224
1225 if (!pVirtio->fLegacyDriver)
1226 AssertMsgReturn((pVirtio->fDeviceStatus & VIRTIO_STATUS_DRIVER_OK) && pVirtq->uEnable,
1227 ("Guest driver not in ready state.\n"), VERR_INVALID_STATE);
1228
1229 Log6Func((" Sync %s used ring (%u -> idx)\n",
1230 pVirtq->szName, pVirtq->uUsedIdxShadow));
1231
1232 virtioWriteUsedRingIdx(pDevIns, pVirtio, pVirtq, pVirtq->uUsedIdxShadow);
1233 virtioCoreNotifyGuestDriver(pDevIns, pVirtio, uVirtq);
1234
1235 return VINF_SUCCESS;
1236}
1237
1238/**
1239 * This is called from the MMIO callback code when the guest does an MMIO access to the
1240 * mapped queue notification capability area corresponding to a particular queue, to notify
1241 * the queue handler of available data in the avail ring of the queue (VirtIO 1.0, 4.1.4.4.1)
1242 *
1243 * @param pDevIns The device instance.
1244 * @param pVirtio Pointer to the shared virtio state.
1245 * @param uVirtq Virtq to check for guest interrupt handling preference
1246 * @param uNotifyIdx Notification index
1247 */
1248static void virtioCoreVirtqNotified(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t uVirtq, uint16_t uNotifyIdx)
1249{
1250 PVIRTIOCORECC pVirtioCC = PDMINS_2_DATA_CC(pDevIns, PVIRTIOCORECC);
1251
1252 /* VirtIO 1.0, section 4.1.5.2 implies uVirtq and uNotifyIdx should match. Disregarding any of
1253 * these notifications (if those indicies disagree) may break device/driver synchronization,
1254 * causing eternal throughput starvation, yet there's no specified way to disambiguate
1255 * which queue to wake-up in any awkward situation where the two parameters differ.
1256 */
1257 AssertMsg(uNotifyIdx == uVirtq,
1258 ("Guest kicked virtq %d's notify addr w/non-corresponding virtq idx %d\n",
1259 uVirtq, uNotifyIdx));
1260 RT_NOREF(uNotifyIdx);
1261
1262 AssertReturnVoid(uVirtq < RT_ELEMENTS(pVirtio->aVirtqueues));
1263 PVIRTQUEUE pVirtq = &pVirtio->aVirtqueues[uVirtq];
1264
1265 Log6Func(("%s: (desc chains: %u)\n", *pVirtq->szName ? pVirtq->szName : "?UNAMED QUEUE?",
1266 virtioCoreVirtqAvailCnt(pDevIns, pVirtio, pVirtq)));
1267
1268 /* Inform client */
1269 pVirtioCC->pfnVirtqNotified(pDevIns, pVirtio, uVirtq);
1270 RT_NOREF2(pVirtio, pVirtq);
1271}
1272
1273/**
1274 * Trigger MSI-X or INT# interrupt to notify guest of data added to used ring of
1275 * the specified virtq, depending on the interrupt configuration of the device
1276 * and depending on negotiated and realtime constraints flagged by the guest driver.
1277 *
1278 * See VirtIO 1.0 specification (section 2.4.7).
1279 *
1280 * @param pDevIns The device instance.
1281 * @param pVirtio Pointer to the shared virtio state.
1282 * @param uVirtq Virtq to check for guest interrupt handling preference
1283 */
1284static void virtioCoreNotifyGuestDriver(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t uVirtq)
1285{
1286 Assert(uVirtq < RT_ELEMENTS(pVirtio->aVirtqueues));
1287 PVIRTQUEUE pVirtq = &pVirtio->aVirtqueues[uVirtq];
1288
1289 if (!IS_DRIVER_OK(pVirtio))
1290 {
1291 LogFunc(("Guest driver not in ready state.\n"));
1292 return;
1293 }
1294
1295 if (pVirtio->uDriverFeatures & VIRTIO_F_EVENT_IDX)
1296 {
1297 if (pVirtq->fUsedRingEvent)
1298 {
1299#ifdef IN_RING3
1300 Log6Func(("...kicking guest %s, VIRTIO_F_EVENT_IDX set and threshold (%d) reached\n",
1301 pVirtq->szName, (uint16_t)virtioReadAvailUsedEvent(pDevIns, pVirtio, pVirtq)));
1302#endif
1303 virtioNudgeGuest(pDevIns, pVirtio, VIRTIO_ISR_VIRTQ_INTERRUPT, pVirtq->uMsixVector);
1304 pVirtq->fUsedRingEvent = false;
1305 return;
1306 }
1307#ifdef IN_RING3
1308 Log6Func(("...skip interrupt %s, VIRTIO_F_EVENT_IDX set but threshold (%d) not reached (%d)\n",
1309 pVirtq->szName,(uint16_t)virtioReadAvailUsedEvent(pDevIns, pVirtio, pVirtq), pVirtq->uUsedIdxShadow));
1310#endif
1311 }
1312 else
1313 {
1314 /** If guest driver hasn't suppressed interrupts, interrupt */
1315 if (!(virtioReadAvailRingFlags(pDevIns, pVirtio, pVirtq) & VIRTQ_AVAIL_F_NO_INTERRUPT))
1316 {
1317 virtioNudgeGuest(pDevIns, pVirtio, VIRTIO_ISR_VIRTQ_INTERRUPT, pVirtq->uMsixVector);
1318 return;
1319 }
1320 Log6Func(("...skipping interrupt for %s (guest set VIRTQ_AVAIL_F_NO_INTERRUPT)\n", pVirtq->szName));
1321 }
1322}
1323
1324/**
1325 * Raise interrupt or MSI-X
1326 *
1327 * @param pDevIns The device instance.
1328 * @param pVirtio Pointer to the shared virtio state.
1329 * @param uCause Interrupt cause bit mask to set in PCI ISR port.
1330 * @param uVec MSI-X vector, if enabled
1331 */
1332static int virtioNudgeGuest(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint8_t uCause, uint16_t uMsixVector)
1333{
1334 if (uCause == VIRTIO_ISR_VIRTQ_INTERRUPT)
1335 Log6Func(("Reason for interrupt - buffer added to 'used' ring.\n"));
1336 else
1337 if (uCause == VIRTIO_ISR_DEVICE_CONFIG)
1338 Log6Func(("Reason for interrupt - device config change\n"));
1339
1340 if (pVirtio->uIrqMmio)
1341 {
1342 pVirtio->uISR |= uCause;
1343 PDMDevHlpISASetIrq(pDevIns, pVirtio->uIrqMmio, PDM_IRQ_LEVEL_HIGH);
1344 }
1345 else if (!pVirtio->fMsiSupport)
1346 {
1347 pVirtio->uISR |= uCause;
1348 PDMDevHlpPCISetIrq(pDevIns, 0, PDM_IRQ_LEVEL_HIGH);
1349 }
1350 else if (uMsixVector != VIRTIO_MSI_NO_VECTOR)
1351 PDMDevHlpPCISetIrq(pDevIns, uMsixVector, 1);
1352 return VINF_SUCCESS;
1353}
1354
1355/**
1356 * Lower interrupt (Called when guest reads ISR and when resetting)
1357 *
1358 * @param pDevIns The device instance.
1359 */
1360static void virtioLowerInterrupt(PPDMDEVINS pDevIns, uint16_t uMsixVector)
1361{
1362 PVIRTIOCORE pVirtio = PDMINS_2_DATA(pDevIns, PVIRTIOCORE);
1363 if (pVirtio->uIrqMmio)
1364 PDMDevHlpISASetIrq(pDevIns, pVirtio->uIrqMmio, PDM_IRQ_LEVEL_LOW);
1365 else if (!pVirtio->fMsiSupport)
1366 PDMDevHlpPCISetIrq(pDevIns, 0, PDM_IRQ_LEVEL_LOW);
1367 else if (uMsixVector != VIRTIO_MSI_NO_VECTOR)
1368 PDMDevHlpPCISetIrq(pDevIns, pVirtio->uMsixConfig, PDM_IRQ_LEVEL_LOW);
1369}
1370
1371#ifdef IN_RING3
1372static void virtioResetVirtq(PVIRTIOCORE pVirtio, uint16_t uVirtq)
1373{
1374 Assert(uVirtq < RT_ELEMENTS(pVirtio->aVirtqueues));
1375 PVIRTQUEUE pVirtq = &pVirtio->aVirtqueues[uVirtq];
1376
1377 pVirtq->uQueueSize = VIRTQ_SIZE;
1378 pVirtq->uEnable = false;
1379 pVirtq->uNotifyOffset = uVirtq;
1380 pVirtq->fUsedRingEvent = false;
1381 pVirtq->uAvailIdxShadow = 0;
1382 pVirtq->uUsedIdxShadow = 0;
1383 pVirtq->uMsixVector = uVirtq + 2;
1384
1385 if (!pVirtio->fMsiSupport) /* VirtIO 1.0, 4.1.4.3 and 4.1.5.1.2 */
1386 pVirtq->uMsixVector = VIRTIO_MSI_NO_VECTOR;
1387
1388 virtioLowerInterrupt(pVirtio->pDevInsR3, pVirtq->uMsixVector);
1389}
1390
1391static void virtioResetDevice(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio)
1392{
1393 LogFunc(("Resetting device VirtIO state\n"));
1394 pVirtio->fLegacyDriver = pVirtio->fOfferLegacy; /* Cleared if VIRTIO_F_VERSION_1 feature ack'd */
1395 pVirtio->uDeviceFeaturesSelect = 0;
1396 pVirtio->uDriverFeaturesSelect = 0;
1397 pVirtio->uConfigGeneration = 0;
1398 pVirtio->fDeviceStatus = 0;
1399 pVirtio->uISR = 0;
1400
1401 if (!pVirtio->fMsiSupport)
1402 virtioLowerInterrupt(pDevIns, 0);
1403 else
1404 {
1405 virtioLowerInterrupt(pDevIns, pVirtio->uMsixConfig);
1406 for (int i = 0; i < VIRTQ_MAX_COUNT; i++)
1407 virtioLowerInterrupt(pDevIns, pVirtio->aVirtqueues[i].uMsixVector);
1408 }
1409
1410 if (!pVirtio->fMsiSupport) /* VirtIO 1.0, 4.1.4.3 and 4.1.5.1.2 */
1411 pVirtio->uMsixConfig = VIRTIO_MSI_NO_VECTOR;
1412
1413 for (uint16_t uVirtq = 0; uVirtq < VIRTQ_MAX_COUNT; uVirtq++)
1414 virtioResetVirtq(pVirtio, uVirtq);
1415}
1416
1417/**
1418 * Invoked by this implementation when guest driver resets the device.
1419 * The driver itself will not until the device has read the status change.
1420 */
1421static void virtioGuestR3WasReset(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, PVIRTIOCORECC pVirtioCC)
1422{
1423 Log(("%-23s: Guest reset the device\n", __FUNCTION__));
1424
1425 /* Let the client know */
1426 pVirtioCC->pfnStatusChanged(pVirtio, pVirtioCC, 0 /* fDriverOk */);
1427 virtioResetDevice(pDevIns, pVirtio);
1428}
1429
1430DECLHIDDEN(void) virtioCoreR3ResetDevice(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, PVIRTIOCORECC pVirtioCC)
1431{
1432 virtioGuestR3WasReset(pDevIns, pVirtio, pVirtioCC);
1433}
1434#endif /* IN_RING3 */
1435
1436/*
1437 * Determines whether guest virtio driver is modern or legacy and does callback
1438 * informing device-specific code that feature negotiation is complete.
1439 * Should be called only once (coordinated via the 'toggle' flag)
1440 */
1441#ifdef IN_RING3
1442DECLINLINE(void) virtioR3DoFeaturesCompleteOnceOnly(PVIRTIOCORE pVirtio, PVIRTIOCORECC pVirtioCC)
1443{
1444 if (pVirtio->uDriverFeatures & VIRTIO_F_VERSION_1)
1445 {
1446 LogFunc(("VIRTIO_F_VERSION_1 feature ack'd by guest\n"));
1447 pVirtio->fLegacyDriver = 0;
1448 }
1449 else
1450 {
1451 if (pVirtio->fOfferLegacy)
1452 {
1453 pVirtio->fLegacyDriver = 1;
1454 LogFunc(("VIRTIO_F_VERSION_1 feature was NOT set by guest\n"));
1455 }
1456 else
1457 AssertMsgFailed(("Guest didn't accept VIRTIO_F_VERSION_1, but fLegacyOffered flag not set.\n"));
1458 }
1459 if (pVirtioCC->pfnFeatureNegotiationComplete)
1460 pVirtioCC->pfnFeatureNegotiationComplete(pVirtio, pVirtio->uDriverFeatures, pVirtio->fLegacyDriver);
1461 pVirtio->fDriverFeaturesWritten |= DRIVER_FEATURES_COMPLETE_HANDLED;
1462}
1463#endif
1464
1465
1466/**
1467 * Handles a write to the device status register from the driver.
1468 *
1469 * @returns VBox status code
1470 *
1471 * @param pDevIns The device instance.
1472 * @param pVirtio Pointer to the shared virtio state.
1473 * @param pVirtioCC Pointer to the current context virtio state.
1474 * @param fDeviceStatus The device status to be written.
1475 */
1476DECLINLINE(int) virtioDeviceStatusWrite(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, PVIRTIOCORECC pVirtioCC,
1477 uint8_t fDeviceStatus)
1478{
1479 pVirtio->fDeviceStatus = fDeviceStatus;
1480 bool fDeviceReset = pVirtio->fDeviceStatus == 0;
1481#ifdef LOG_ENABLED
1482 if (LogIs7Enabled())
1483 {
1484 char szOut[80] = { 0 };
1485 virtioCoreFormatDeviceStatus(pVirtio->fDeviceStatus, szOut, sizeof(szOut));
1486 Log(("%-23s: Guest wrote fDeviceStatus ................ (%s)\n", __FUNCTION__, szOut));
1487 }
1488#endif
1489 bool const fStatusChanged = IS_DRIVER_OK(pVirtio) != WAS_DRIVER_OK(pVirtio);
1490
1491 if (fDeviceReset || fStatusChanged)
1492 {
1493#ifdef IN_RING0
1494 /* Since VirtIO status changes are cumbersome by nature, e.g. not a benchmark priority,
1495 * handle the rest in R3 to facilitate logging or whatever dev-specific client needs to do */
1496 Log6(("%-23s: RING0 => RING3 (demote)\n", __FUNCTION__));
1497 return VINF_IOM_R3_MMIO_WRITE;
1498#endif
1499 }
1500
1501#ifdef IN_RING3
1502 /*
1503 * Notify client only if status actually changed from last time and when we're reset.
1504 */
1505 if (fDeviceReset)
1506 virtioGuestR3WasReset(pDevIns, pVirtio, pVirtioCC);
1507
1508 if (fStatusChanged)
1509 pVirtioCC->pfnStatusChanged(pVirtio, pVirtioCC, IS_DRIVER_OK(pVirtio));
1510#else
1511 RT_NOREF(pDevIns, pVirtioCC);
1512#endif
1513 /*
1514 * Save the current status for the next write so we can see what changed.
1515 */
1516 pVirtio->fPrevDeviceStatus = pVirtio->fDeviceStatus;
1517 return VINF_SUCCESS;
1518}
1519
1520
1521/**
1522 * Handles a read from the device status register from the driver.
1523 *
1524 * @returns The device status register value.
1525 *
1526 * @param pVirtio Pointer to the shared virtio state.
1527 */
1528DECLINLINE(uint8_t) virtioDeviceStatusRead(PVIRTIOCORE pVirtio)
1529{
1530#ifdef LOG_ENABLED
1531 if (LogIs7Enabled())
1532 {
1533 char szOut[80] = { 0 };
1534 virtioCoreFormatDeviceStatus(pVirtio->fDeviceStatus, szOut, sizeof(szOut));
1535 LogFunc(("Guest read fDeviceStatus ................ (%s)\n", szOut));
1536 }
1537#endif
1538 return pVirtio->fDeviceStatus;
1539}
1540
1541
1542/**
1543 * Handle accesses to Common Configuration capability
1544 *
1545 * @returns VBox status code
1546 *
1547 * @param pDevIns The device instance.
1548 * @param pVirtio Pointer to the shared virtio state.
1549 * @param pVirtioCC Pointer to the current context virtio state.
1550 * @param fWrite Set if write access, clear if read access.
1551 * @param uOffsetOfAccess The common configuration capability offset.
1552 * @param cb Number of bytes to read or write
1553 * @param pv Pointer to location to write to or read from
1554 */
1555static int virtioCommonCfgAccessed(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, PVIRTIOCORECC pVirtioCC,
1556 int fWrite, uint32_t uOffsetOfAccess, unsigned cb, void *pv)
1557{
1558 uint16_t uVirtq = pVirtio->uVirtqSelect;
1559 int rc = VINF_SUCCESS;
1560 uint64_t val;
1561 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER(uDeviceFeatures, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess))
1562 {
1563 if (fWrite) /* Guest WRITE pCommonCfg>uDeviceFeatures */
1564 {
1565 /* VirtIO 1.0, 4.1.4.3 states device_feature is a (guest) driver readonly field,
1566 * yet the linux driver attempts to write/read it back twice */
1567 VIRTIO_DEV_CONFIG_LOG_ACCESS(uDeviceFeatures, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess);
1568 LogFunc(("... WARNING: Guest attempted to write readonly virtio_pci_common_cfg.device_feature (ignoring)\n"));
1569 return VINF_IOM_MMIO_UNUSED_00;
1570 }
1571 else /* Guest READ pCommonCfg->uDeviceFeatures */
1572 {
1573 switch (pVirtio->uDeviceFeaturesSelect)
1574 {
1575 case 0:
1576 val = pVirtio->uDeviceFeatures & UINT32_C(0xffffffff);
1577 memcpy(pv, &val, cb);
1578 VIRTIO_DEV_CONFIG_LOG_ACCESS(uDeviceFeatures, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess);
1579 break;
1580 case 1:
1581 val = pVirtio->uDeviceFeatures >> 32;
1582 memcpy(pv, &val, cb);
1583 VIRTIO_DEV_CONFIG_LOG_ACCESS(uDeviceFeatures, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess + sizeof(uint32_t));
1584 break;
1585 default:
1586 LogFunc(("Guest read uDeviceFeatures with out of range selector (%#x), returning 0\n",
1587 pVirtio->uDeviceFeaturesSelect));
1588 return VINF_IOM_MMIO_UNUSED_00;
1589 }
1590 }
1591 }
1592 else
1593 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER(uDriverFeatures, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess))
1594 {
1595 if (fWrite) /* Guest WRITE pCommonCfg->udriverFeatures */
1596 {
1597 switch (pVirtio->uDriverFeaturesSelect)
1598 {
1599 case 0:
1600 memcpy(&pVirtio->uDriverFeatures, pv, cb);
1601 pVirtio->fDriverFeaturesWritten |= DRIVER_FEATURES_0_WRITTEN;
1602 LogFunc(("Set DRIVER_FEATURES_0_WRITTEN. pVirtio->fDriverFeaturesWritten=%d\n", pVirtio->fDriverFeaturesWritten));
1603 if ( (pVirtio->fDriverFeaturesWritten & DRIVER_FEATURES_0_AND_1_WRITTEN) == DRIVER_FEATURES_0_AND_1_WRITTEN
1604 && !(pVirtio->fDriverFeaturesWritten & DRIVER_FEATURES_COMPLETE_HANDLED))
1605#ifdef IN_RING0
1606 return VINF_IOM_R3_MMIO_WRITE;
1607#endif
1608#ifdef IN_RING3
1609 virtioR3DoFeaturesCompleteOnceOnly(pVirtio, pVirtioCC);
1610#endif
1611 VIRTIO_DEV_CONFIG_LOG_ACCESS(uDriverFeatures, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess);
1612 break;
1613 case 1:
1614 memcpy((char *)&pVirtio->uDriverFeatures + sizeof(uint32_t), pv, cb);
1615 pVirtio->fDriverFeaturesWritten |= DRIVER_FEATURES_1_WRITTEN;
1616 LogFunc(("Set DRIVER_FEATURES_1_WRITTEN. pVirtio->fDriverFeaturesWritten=%d\n", pVirtio->fDriverFeaturesWritten));
1617 if ( (pVirtio->fDriverFeaturesWritten & DRIVER_FEATURES_0_AND_1_WRITTEN) == DRIVER_FEATURES_0_AND_1_WRITTEN
1618 && !(pVirtio->fDriverFeaturesWritten & DRIVER_FEATURES_COMPLETE_HANDLED))
1619#ifdef IN_RING0
1620 return VINF_IOM_R3_MMIO_WRITE;
1621#endif
1622#ifdef IN_RING3
1623 virtioR3DoFeaturesCompleteOnceOnly(pVirtio, pVirtioCC);
1624#endif
1625 VIRTIO_DEV_CONFIG_LOG_ACCESS(uDriverFeatures, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess + sizeof(uint32_t));
1626 break;
1627 default:
1628 LogFunc(("Guest wrote uDriverFeatures with out of range selector (%#x), returning 0\n",
1629 pVirtio->uDriverFeaturesSelect));
1630 return VINF_SUCCESS;
1631 }
1632 }
1633 else /* Guest READ pCommonCfg->udriverFeatures */
1634 {
1635 switch (pVirtio->uDriverFeaturesSelect)
1636 {
1637 case 0:
1638 val = pVirtio->uDriverFeatures & 0xffffffff;
1639 memcpy(pv, &val, cb);
1640 VIRTIO_DEV_CONFIG_LOG_ACCESS(uDriverFeatures, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess);
1641 break;
1642 case 1:
1643 val = (pVirtio->uDriverFeatures >> 32) & 0xffffffff;
1644 memcpy(pv, &val, cb);
1645 VIRTIO_DEV_CONFIG_LOG_ACCESS(uDriverFeatures, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess + 4);
1646 break;
1647 default:
1648 LogFunc(("Guest read uDriverFeatures with out of range selector (%#x), returning 0\n",
1649 pVirtio->uDriverFeaturesSelect));
1650 return VINF_IOM_MMIO_UNUSED_00;
1651 }
1652 }
1653 }
1654 else
1655 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER(uNumVirtqs, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess))
1656 {
1657 if (fWrite)
1658 {
1659 Log2Func(("Guest attempted to write readonly virtio_pci_common_cfg.num_queues\n"));
1660 return VINF_SUCCESS;
1661 }
1662 *(uint16_t *)pv = VIRTQ_MAX_COUNT;
1663 VIRTIO_DEV_CONFIG_LOG_ACCESS(uNumVirtqs, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess);
1664 }
1665 else
1666 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER(fDeviceStatus, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess))
1667 {
1668 if (fWrite) /* Guest WRITE pCommonCfg->fDeviceStatus */
1669 rc = virtioDeviceStatusWrite(pDevIns, pVirtio, pVirtioCC, *(uint8_t *)pv);
1670 else /* Guest READ pCommonCfg->fDeviceStatus */
1671 *(uint8_t *)pv = virtioDeviceStatusRead(pVirtio);
1672 }
1673 else
1674 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uMsixConfig, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess))
1675 VIRTIO_DEV_CONFIG_ACCESS( uMsixConfig, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess, pVirtio);
1676 else
1677 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uDeviceFeaturesSelect, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess))
1678 VIRTIO_DEV_CONFIG_ACCESS( uDeviceFeaturesSelect, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess, pVirtio);
1679 else
1680 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uDriverFeaturesSelect, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess))
1681 VIRTIO_DEV_CONFIG_ACCESS( uDriverFeaturesSelect, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess, pVirtio);
1682 else
1683 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uConfigGeneration, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess))
1684 VIRTIO_DEV_CONFIG_ACCESS( uConfigGeneration, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess, pVirtio);
1685 else
1686 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uVirtqSelect, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess))
1687 {
1688 if (fWrite) {
1689 uint16_t uVirtqNew = *(uint16_t *)pv;
1690
1691 if (uVirtqNew < RT_ELEMENTS(pVirtio->aVirtqueues))
1692 VIRTIO_DEV_CONFIG_ACCESS( uVirtqSelect, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess, pVirtio);
1693 else
1694 LogFunc(("... WARNING: Guest attempted to write invalid virtq selector (ignoring)\n"));
1695 }
1696 else
1697 VIRTIO_DEV_CONFIG_ACCESS( uVirtqSelect, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess, pVirtio);
1698 }
1699 else
1700 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( GCPhysVirtqDesc, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess))
1701 VIRTIO_DEV_CONFIG_ACCESS_INDEXED( GCPhysVirtqDesc, uVirtq, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess, pVirtio->aVirtqueues);
1702 else
1703 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( GCPhysVirtqAvail, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess))
1704 VIRTIO_DEV_CONFIG_ACCESS_INDEXED( GCPhysVirtqAvail, uVirtq, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess, pVirtio->aVirtqueues);
1705 else
1706 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( GCPhysVirtqUsed, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess))
1707 VIRTIO_DEV_CONFIG_ACCESS_INDEXED( GCPhysVirtqUsed, uVirtq, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess, pVirtio->aVirtqueues);
1708 else
1709 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uQueueSize, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess))
1710 VIRTIO_DEV_CONFIG_ACCESS_INDEXED( uQueueSize, uVirtq, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess, pVirtio->aVirtqueues);
1711 else
1712 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uEnable, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess))
1713 VIRTIO_DEV_CONFIG_ACCESS_INDEXED( uEnable, uVirtq, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess, pVirtio->aVirtqueues);
1714 else
1715 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uNotifyOffset, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess))
1716 VIRTIO_DEV_CONFIG_ACCESS_INDEXED( uNotifyOffset, uVirtq, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess, pVirtio->aVirtqueues);
1717 else
1718 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uMsixVector, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess))
1719 VIRTIO_DEV_CONFIG_ACCESS_INDEXED( uMsixVector, uVirtq, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess, pVirtio->aVirtqueues);
1720 else
1721 {
1722 Log2Func(("Bad guest %s access to virtio_pci_common_cfg: uOffsetOfAccess=%#x (%d), cb=%d\n",
1723 fWrite ? "write" : "read ", uOffsetOfAccess, uOffsetOfAccess, cb));
1724 return fWrite ? VINF_SUCCESS : VINF_IOM_MMIO_UNUSED_00;
1725 }
1726
1727#ifndef IN_RING3
1728 RT_NOREF(pDevIns, pVirtioCC);
1729#endif
1730 return rc;
1731}
1732
1733/**
1734 * @callback_method_impl{FNIOMIOPORTNEWIN)
1735 *
1736 * This I/O handler exists only to handle access from legacy drivers.
1737 */
1738static DECLCALLBACK(VBOXSTRICTRC) virtioLegacyIOPortIn(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT offPort, uint32_t *pu32, unsigned cb)
1739{
1740 PVIRTIOCORE pVirtio = PDMINS_2_DATA(pDevIns, PVIRTIOCORE);
1741 STAM_PROFILE_ADV_START(&pVirtio->CTX_SUFF(StatRead), a);
1742
1743 RT_NOREF(pvUser);
1744 Log(("%-23s: Port read at offset=%RTiop, cb=%#x%s",
1745 __FUNCTION__, offPort, cb,
1746 VIRTIO_DEV_CONFIG_MATCH_MEMBER(fIsrStatus, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort) ? "" : "\n"));
1747
1748 void *pv = pu32; /* To use existing macros */
1749 int fWrite = 0; /* To use existing macros */
1750
1751 uint16_t uVirtq = pVirtio->uVirtqSelect;
1752
1753 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER(uDeviceFeatures, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort))
1754 {
1755 uint32_t val = pVirtio->uDeviceFeatures & UINT32_C(0xffffffff);
1756 memcpy(pu32, &val, cb);
1757 VIRTIO_DEV_CONFIG_LOG_ACCESS(uDeviceFeatures, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort);
1758 }
1759 else
1760 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER(uDriverFeatures, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort))
1761 {
1762 uint32_t val = pVirtio->uDriverFeatures & UINT32_C(0xffffffff);
1763 memcpy(pu32, &val, cb);
1764 VIRTIO_DEV_CONFIG_LOG_ACCESS(uDriverFeatures, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort);
1765 }
1766 else
1767 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER(fDeviceStatus, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort))
1768 {
1769 *(uint8_t *)pu32 = pVirtio->fDeviceStatus;
1770#ifdef LOG_ENABLED
1771 if (LogIs7Enabled())
1772 {
1773 char szOut[80] = { 0 };
1774 virtioCoreFormatDeviceStatus(pVirtio->fDeviceStatus, szOut, sizeof(szOut));
1775 Log(("%-23s: Guest read fDeviceStatus ................ (%s)\n", __FUNCTION__, szOut));
1776 }
1777#endif
1778 }
1779 else
1780 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER(fIsrStatus, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort))
1781 {
1782 ASSERT_GUEST_MSG(cb == 1, ("%d\n", cb));
1783 *(uint8_t *)pu32 = pVirtio->uISR;
1784 pVirtio->uISR = 0;
1785 virtioLowerInterrupt( pDevIns, 0);
1786 Log((" (ISR read and cleared)\n"));
1787 }
1788 else
1789 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uVirtqSelect, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort))
1790 VIRTIO_DEV_CONFIG_ACCESS( uVirtqSelect, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort, pVirtio);
1791 else
1792 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uVirtqPfn, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort))
1793 {
1794 PVIRTQUEUE pVirtQueue = &pVirtio->aVirtqueues[uVirtq];
1795 *pu32 = pVirtQueue->GCPhysVirtqDesc >> GUEST_PAGE_SHIFT;
1796 Log(("%-23s: Guest read uVirtqPfn .................... %#x\n", __FUNCTION__, *pu32));
1797 }
1798 else
1799 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uQueueSize, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort))
1800 VIRTIO_DEV_CONFIG_ACCESS_INDEXED( uQueueSize, uVirtq, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort, pVirtio->aVirtqueues);
1801 else
1802 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uQueueNotify, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort))
1803 VIRTIO_DEV_CONFIG_ACCESS( uQueueNotify, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort, pVirtio);
1804#ifdef LEGACY_MSIX_SUPPORTED
1805 else
1806 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uMsixConfig, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort))
1807 VIRTIO_DEV_CONFIG_ACCESS( uMsixConfig, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort, pVirtio);
1808 else
1809 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uMsixVector, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort))
1810 VIRTIO_DEV_CONFIG_ACCESS_INDEXED( uMsixVector, uVirtq, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort, pVirtio->aVirtqueues);
1811#endif
1812 else if (offPort >= sizeof(VIRTIO_LEGACY_PCI_COMMON_CFG_T))
1813 {
1814 STAM_PROFILE_ADV_STOP(&pVirtio->CTX_SUFF(StatRead), a);
1815#ifdef IN_RING3
1816 /* Access device-specific configuration */
1817 PVIRTIOCORECC pVirtioCC = PDMINS_2_DATA_CC(pDevIns, PVIRTIOCORECC);
1818 int rc = pVirtioCC->pfnDevCapRead(pDevIns, offPort - sizeof(VIRTIO_LEGACY_PCI_COMMON_CFG_T), pv, cb);
1819 return rc;
1820#else
1821 return VINF_IOM_R3_IOPORT_READ;
1822#endif
1823 }
1824 else
1825 {
1826 STAM_PROFILE_ADV_STOP(&pVirtio->CTX_SUFF(StatRead), a);
1827 Log2Func(("Bad guest read access to virtio_legacy_pci_common_cfg: offset=%#x, cb=%x\n",
1828 offPort, cb));
1829 int rc = PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS,
1830 "virtioLegacyIOPortIn: no valid port at offset offset=%RTiop cb=%#x\n", offPort, cb);
1831 return rc;
1832 }
1833 STAM_PROFILE_ADV_STOP(&pVirtio->CTX_SUFF(StatRead), a);
1834 return VINF_SUCCESS;
1835}
1836
1837/**
1838 * @callback_method_impl{ * @callback_method_impl{FNIOMIOPORTNEWOUT}
1839 *
1840 * This I/O Port interface exists only to handle access from legacy drivers.
1841 */
1842static DECLCALLBACK(VBOXSTRICTRC) virtioLegacyIOPortOut(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT offPort, uint32_t u32, unsigned cb)
1843{
1844 PVIRTIOCORE pVirtio = PDMINS_2_DATA(pDevIns, PVIRTIOCORE);
1845 STAM_PROFILE_ADV_START(&pVirtio->CTX_SUFF(StatWrite), a);
1846 RT_NOREF(pvUser);
1847
1848 uint16_t uVirtq = pVirtio->uVirtqSelect;
1849 uint32_t u32OnStack = u32; /* allows us to use this impl's MMIO parsing macros */
1850 void *pv = &u32OnStack; /* To use existing macros */
1851 int fWrite = 1; /* To use existing macros */
1852
1853 Log(("%-23s: Port written at offset=%RTiop, cb=%#x, u32=%#x\n", __FUNCTION__, offPort, cb, u32));
1854
1855 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uVirtqSelect, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort))
1856 {
1857 if (u32 < RT_ELEMENTS(pVirtio->aVirtqueues))
1858 VIRTIO_DEV_CONFIG_ACCESS( uVirtqSelect, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort, pVirtio);
1859 else
1860 LogFunc(("... WARNING: Guest attempted to write invalid virtq selector (ignoring)\n"));
1861 }
1862 else
1863#ifdef LEGACY_MSIX_SUPPORTED
1864 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uMsixConfig, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort))
1865 VIRTIO_DEV_CONFIG_ACCESS( uMsixConfig, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort, pVirtio);
1866 else
1867 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uMsixVector, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort))
1868 VIRTIO_DEV_CONFIG_ACCESS_INDEXED( uMsixVector, uVirtq, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort, pVirtio->aVirtqueues);
1869 else
1870#endif
1871 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER(uDeviceFeatures, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort))
1872 {
1873 /* Check to see if guest acknowledged unsupported features */
1874 VIRTIO_DEV_CONFIG_LOG_ACCESS(uDeviceFeatures, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort);
1875 LogFunc(("... WARNING: Guest attempted to write readonly virtio_pci_common_cfg.device_feature (ignoring)\n"));
1876 STAM_PROFILE_ADV_STOP(&pVirtio->CTX_SUFF(StatWrite), a);
1877 return VINF_SUCCESS;
1878 }
1879 else
1880 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER(uDriverFeatures, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort))
1881 {
1882 memcpy(&pVirtio->uDriverFeatures, pv, cb);
1883 if ((pVirtio->uDriverFeatures & ~VIRTIO_DEV_INDEPENDENT_LEGACY_FEATURES_OFFERED) == 0)
1884 {
1885 Log(("Guest asked for features host does not support! (host=%x guest=%x)\n",
1886 VIRTIO_DEV_INDEPENDENT_LEGACY_FEATURES_OFFERED, pVirtio->uDriverFeatures));
1887 pVirtio->uDriverFeatures &= VIRTIO_DEV_INDEPENDENT_LEGACY_FEATURES_OFFERED;
1888 }
1889 if (!(pVirtio->fDriverFeaturesWritten & DRIVER_FEATURES_COMPLETE_HANDLED))
1890 {
1891#ifdef IN_RING0
1892 Log6(("%-23s: RING0 => RING3 (demote)\n", __FUNCTION__));
1893 return VINF_IOM_R3_IOPORT_WRITE;
1894#endif
1895#ifdef IN_RING3
1896 PVIRTIOCORECC pVirtioCC = PDMINS_2_DATA_CC(pDevIns, PVIRTIOCORECC);
1897 virtioR3DoFeaturesCompleteOnceOnly(pVirtio, pVirtioCC);
1898#endif
1899 }
1900 VIRTIO_DEV_CONFIG_LOG_ACCESS(uDriverFeatures, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort);
1901 }
1902 else
1903 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER(uQueueSize, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort))
1904 {
1905 VIRTIO_DEV_CONFIG_LOG_ACCESS(uQueueSize, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort);
1906 LogFunc(("... WARNING: Guest attempted to write readonly device_feature (queue size) (ignoring)\n"));
1907 return VINF_SUCCESS;
1908 }
1909 else
1910 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER(fDeviceStatus, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort))
1911 {
1912 bool const fDriverInitiatedReset = (pVirtio->fDeviceStatus = (uint8_t)u32) == 0;
1913 bool const fDriverStateImproved = IS_DRIVER_OK(pVirtio) && !WAS_DRIVER_OK(pVirtio);
1914#ifdef LOG_ENABLED
1915 if (LogIs7Enabled())
1916 {
1917 char szOut[80] = { 0 };
1918 virtioCoreFormatDeviceStatus(pVirtio->fDeviceStatus, szOut, sizeof(szOut));
1919 Log(("%-23s: Guest wrote fDeviceStatus ................ (%s)\n", __FUNCTION__, szOut));
1920 }
1921#endif
1922 if (fDriverStateImproved || fDriverInitiatedReset)
1923 {
1924#ifdef IN_RING0
1925 Log6(("%-23s: RING0 => RING3 (demote)\n", __FUNCTION__));
1926 STAM_PROFILE_ADV_STOP(&pVirtio->CTX_SUFF(StatWrite), a);
1927 return VINF_IOM_R3_IOPORT_WRITE;
1928#endif
1929 }
1930
1931#ifdef IN_RING3
1932 PVIRTIOCORECC pVirtioCC = PDMINS_2_DATA_CC(pDevIns, PVIRTIOCORECC);
1933 if (fDriverInitiatedReset)
1934 virtioGuestR3WasReset(pDevIns, pVirtio, pVirtioCC);
1935
1936 else if (fDriverStateImproved)
1937 pVirtioCC->pfnStatusChanged(pVirtio, pVirtioCC, 1 /* fDriverOk */);
1938
1939#endif
1940 pVirtio->fPrevDeviceStatus = pVirtio->fDeviceStatus;
1941 }
1942 else
1943 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER(uVirtqPfn, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort))
1944 {
1945 PVIRTQUEUE pVirtq = &pVirtio->aVirtqueues[uVirtq];
1946 uint64_t uVirtqPfn = (uint64_t)u32;
1947
1948 if (uVirtqPfn)
1949 {
1950 /* Transitional devices calculate ring physical addresses using rigid spec-defined formulae,
1951 * instead of guest conveying respective address of each ring, as "modern" VirtIO drivers do,
1952 * thus there is no virtq PFN or single base queue address stored in instance data for
1953 * this transitional device, but rather it is derived, when read back, from GCPhysVirtqDesc */
1954
1955 pVirtq->GCPhysVirtqDesc = uVirtqPfn * VIRTIO_PAGE_SIZE;
1956 pVirtq->GCPhysVirtqAvail = pVirtq->GCPhysVirtqDesc + sizeof(VIRTQ_DESC_T) * pVirtq->uQueueSize;
1957 pVirtq->GCPhysVirtqUsed =
1958 RT_ALIGN(pVirtq->GCPhysVirtqAvail + RT_UOFFSETOF_DYN(VIRTQ_AVAIL_T, auRing[pVirtq->uQueueSize]), VIRTIO_PAGE_SIZE);
1959 }
1960 else
1961 {
1962 /* Don't set ring addresses for queue (to meaningless values), when guest resets the virtq's PFN */
1963 pVirtq->GCPhysVirtqDesc = 0;
1964 pVirtq->GCPhysVirtqAvail = 0;
1965 pVirtq->GCPhysVirtqUsed = 0;
1966 }
1967 Log(("%-23s: Guest wrote uVirtqPfn .................... %#x:\n"
1968 "%68s... %p -> GCPhysVirtqDesc\n%68s... %p -> GCPhysVirtqAvail\n%68s... %p -> GCPhysVirtqUsed\n",
1969 __FUNCTION__, u32, " ", pVirtq->GCPhysVirtqDesc, " ", pVirtq->GCPhysVirtqAvail, " ", pVirtq->GCPhysVirtqUsed));
1970 }
1971 else
1972 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER(uQueueNotify, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort))
1973 {
1974#ifdef IN_RING3
1975 ASSERT_GUEST_MSG(cb == 2, ("cb=%u\n", cb));
1976 pVirtio->uQueueNotify = u32 & 0xFFFF;
1977 if (uVirtq < VIRTQ_MAX_COUNT)
1978 {
1979 RT_UNTRUSTED_VALIDATED_FENCE();
1980
1981 /* Need to check that queue is configured. Legacy spec didn't have a queue enabled flag */
1982 if (pVirtio->aVirtqueues[pVirtio->uQueueNotify].GCPhysVirtqDesc)
1983 virtioCoreVirtqNotified(pDevIns, pVirtio, pVirtio->uQueueNotify, pVirtio->uQueueNotify /* uNotifyIdx */);
1984 else
1985 Log(("The queue (#%d) being notified has not been initialized.\n", pVirtio->uQueueNotify));
1986 }
1987 else
1988 Log(("Invalid queue number (%d)\n", pVirtio->uQueueNotify));
1989#else
1990 STAM_PROFILE_ADV_STOP(&pVirtio->CTX_SUFF(StatWrite), a);
1991 return VINF_IOM_R3_IOPORT_WRITE;
1992#endif
1993 }
1994 else
1995 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER(fIsrStatus, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort))
1996 {
1997 VIRTIO_DEV_CONFIG_LOG_ACCESS( fIsrStatus, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort);
1998 LogFunc(("... WARNING: Guest attempted to write readonly device_feature (ISR status) (ignoring)\n"));
1999 STAM_PROFILE_ADV_STOP(&pVirtio->CTX_SUFF(StatWrite), a);
2000 return VINF_SUCCESS;
2001 }
2002 else if (offPort >= sizeof(VIRTIO_LEGACY_PCI_COMMON_CFG_T))
2003 {
2004#ifdef IN_RING3
2005
2006 /* Access device-specific configuration */
2007 PVIRTIOCORECC pVirtioCC = PDMINS_2_DATA_CC(pDevIns, PVIRTIOCORECC);
2008 return pVirtioCC->pfnDevCapWrite(pDevIns, offPort - sizeof(VIRTIO_LEGACY_PCI_COMMON_CFG_T), pv, cb);
2009#else
2010 STAM_PROFILE_ADV_STOP(&pVirtio->CTX_SUFF(StatWrite), a);
2011 return VINF_IOM_R3_IOPORT_WRITE;
2012#endif
2013 }
2014 else
2015 {
2016 Log2Func(("Bad guest write access to virtio_legacy_pci_common_cfg: offset=%#x, cb=0x%x\n",
2017 offPort, cb));
2018 STAM_PROFILE_ADV_STOP(&pVirtio->CTX_SUFF(StatWrite), a);
2019 int rc = PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS,
2020 "virtioLegacyIOPortOut: no valid port at offset offset=%RTiop cb=0x%#x\n", offPort, cb);
2021 return rc;
2022 }
2023
2024 RT_NOREF(uVirtq);
2025 STAM_PROFILE_ADV_STOP(&pVirtio->CTX_SUFF(StatWrite), a);
2026 return VINF_SUCCESS;
2027}
2028
2029
2030/**
2031 * Read from the device specific configuration at the given offset.
2032 *
2033 * @returns VBox status code.
2034 * @param pDevIns
2035 */
2036DECLINLINE(VBOXSTRICTRC) virtioDeviceCfgRead(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, PVIRTIOCORECC pVirtioCC,
2037 uint32_t offDevCfg, void *pv, unsigned cb)
2038{
2039#ifdef IN_RING3
2040 /*
2041 * Callback to client to manage device-specific configuration.
2042 */
2043 VBOXSTRICTRC rcStrict = pVirtioCC->pfnDevCapRead(pDevIns, offDevCfg, pv, cb);
2044
2045 /*
2046 * Anytime any part of the dev-specific dev config (which this virtio core implementation sees
2047 * as a blob, and virtio dev-specific code separates into fields) is READ, it must be compared
2048 * for deltas from previous read to maintain a config gen. seq. counter (VirtIO 1.0, section 4.1.4.3.1)
2049 */
2050 bool fDevSpecificFieldChanged = RT_BOOL(memcmp(pVirtioCC->pbDevSpecificCfg + offDevCfg,
2051 pVirtioCC->pbPrevDevSpecificCfg + offDevCfg,
2052 RT_MIN(cb, pVirtioCC->cbDevSpecificCfg - offDevCfg)));
2053
2054 memcpy(pVirtioCC->pbPrevDevSpecificCfg, pVirtioCC->pbDevSpecificCfg, pVirtioCC->cbDevSpecificCfg);
2055
2056 if (pVirtio->fGenUpdatePending || fDevSpecificFieldChanged)
2057 {
2058 ++pVirtio->uConfigGeneration;
2059 Log6Func(("Bumped cfg. generation to %d because %s%s\n", pVirtio->uConfigGeneration,
2060 fDevSpecificFieldChanged ? "<dev cfg changed> " : "",
2061 pVirtio->fGenUpdatePending ? "<update was pending>" : ""));
2062 pVirtio->fGenUpdatePending = false;
2063 }
2064
2065 virtioLowerInterrupt(pDevIns, 0);
2066 return rcStrict;
2067#else
2068 RT_NOREF(pDevIns, pVirtio, pVirtioCC, offDevCfg, pv, cb);
2069 return VINF_IOM_R3_MMIO_READ;
2070#endif
2071}
2072
2073
2074/**
2075 * @callback_method_impl{FNIOMMMIONEWREAD,
2076 * Memory mapped I/O Handler for PCI Capabilities read operations.}
2077 *
2078 * This MMIO handler specifically supports the VIRTIO_PCI_CAP_PCI_CFG capability defined
2079 * in the VirtIO 1.0 specification, section 4.1.4.7, and as such is restricted to reads
2080 * of 1, 2 or 4 bytes, only.
2081 *
2082 */
2083static DECLCALLBACK(VBOXSTRICTRC) virtioMmioRead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void *pv, unsigned cb)
2084{
2085 PVIRTIOCORE pVirtio = PDMINS_2_DATA(pDevIns, PVIRTIOCORE);
2086 PVIRTIOCORECC pVirtioCC = PDMINS_2_DATA_CC(pDevIns, PVIRTIOCORECC);
2087 AssertReturn(cb == 1 || cb == 2 || cb == 4, VINF_IOM_MMIO_UNUSED_FF);
2088 Assert(pVirtio == (PVIRTIOCORE)pvUser); RT_NOREF(pvUser);
2089
2090 STAM_PROFILE_ADV_START(&pVirtio->CTX_SUFF(StatRead), a);
2091
2092 VBOXSTRICTRC rcStrict;
2093 uint32_t uOffset;
2094 if (MATCHES_VIRTIO_CAP_STRUCT(off, cb, uOffset, pVirtio->LocDeviceCap))
2095 rcStrict = virtioDeviceCfgRead(pDevIns, pVirtio, pVirtioCC, uOffset, pv, cb);
2096 else if (MATCHES_VIRTIO_CAP_STRUCT(off, cb, uOffset, pVirtio->LocCommonCfgCap))
2097 rcStrict = virtioCommonCfgAccessed(pDevIns, pVirtio, pVirtioCC, false /* fWrite */, uOffset, cb, pv);
2098 else if (MATCHES_VIRTIO_CAP_STRUCT(off, cb, uOffset, pVirtio->LocIsrCap))
2099 {
2100 *(uint8_t *)pv = pVirtio->uISR;
2101 Log6Func(("Read and clear ISR\n"));
2102 pVirtio->uISR = 0; /* VirtIO spec requires reads of ISR to clear it */
2103 virtioLowerInterrupt(pDevIns, 0);
2104 rcStrict = VINF_SUCCESS;
2105 }
2106 else
2107 {
2108 ASSERT_GUEST_MSG_FAILED(("Bad read access to mapped capabilities region: off=%RGp cb=%u\n", off, cb));
2109 memset(pv, 0xFF, cb);
2110 rcStrict = PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS,
2111 "virtioMmioRead: Bad MMIO access to capabilities, offset=%RTiop cb=%08x\n", off, cb);
2112 }
2113
2114 STAM_PROFILE_ADV_STOP(&pVirtio->CTX_SUFF(StatRead), a);
2115 return rcStrict;
2116}
2117
2118/**
2119 * @callback_method_impl{FNIOMMMIONEWREAD,
2120 * Memory mapped I/O Handler for PCI Capabilities write operations.}
2121 *
2122 * This MMIO handler specifically supports the VIRTIO_PCI_CAP_PCI_CFG capability defined
2123 * in the VirtIO 1.0 specification, section 4.1.4.7, and as such is restricted to writes
2124 * of 1, 2 or 4 bytes, only.
2125 */
2126static DECLCALLBACK(VBOXSTRICTRC) virtioMmioWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void const *pv, unsigned cb)
2127{
2128 PVIRTIOCORE pVirtio = PDMINS_2_DATA(pDevIns, PVIRTIOCORE);
2129 PVIRTIOCORECC pVirtioCC = PDMINS_2_DATA_CC(pDevIns, PVIRTIOCORECC);
2130 AssertReturn(cb == 1 || cb == 2 || cb == 4, VERR_INVALID_PARAMETER);
2131 Assert(pVirtio == (PVIRTIOCORE)pvUser); RT_NOREF(pvUser);
2132 STAM_PROFILE_ADV_START(&pVirtio->CTX_SUFF(StatWrite), a);
2133
2134 VBOXSTRICTRC rcStrict;
2135 uint32_t uOffset;
2136 if (MATCHES_VIRTIO_CAP_STRUCT(off, cb, uOffset, pVirtio->LocDeviceCap))
2137 {
2138#ifdef IN_RING3
2139 /*
2140 * Foreward this MMIO write access for client to deal with.
2141 */
2142 rcStrict = pVirtioCC->pfnDevCapWrite(pDevIns, uOffset, pv, cb);
2143#else
2144 Log6(("%-23s: RING0 => RING3 (demote)\n", __FUNCTION__));
2145 rcStrict = VINF_IOM_R3_MMIO_WRITE;
2146#endif
2147 }
2148 else if (MATCHES_VIRTIO_CAP_STRUCT(off, cb, uOffset, pVirtio->LocCommonCfgCap))
2149 rcStrict = virtioCommonCfgAccessed(pDevIns, pVirtio, pVirtioCC, true /* fWrite */, uOffset, cb, (void *)pv);
2150 else if (MATCHES_VIRTIO_CAP_STRUCT(off, cb, uOffset, pVirtio->LocIsrCap) && cb == sizeof(uint8_t))
2151 {
2152 pVirtio->uISR = *(uint8_t *)pv;
2153 Log6Func(("Setting uISR = 0x%02x (virtq interrupt: %d, dev confg interrupt: %d)\n",
2154 pVirtio->uISR & 0xff,
2155 pVirtio->uISR & VIRTIO_ISR_VIRTQ_INTERRUPT,
2156 RT_BOOL(pVirtio->uISR & VIRTIO_ISR_DEVICE_CONFIG)));
2157 rcStrict = VINF_SUCCESS;
2158 }
2159 else if (MATCHES_VIRTIO_CAP_STRUCT(off, cb, uOffset, pVirtio->LocNotifyCap) && cb == sizeof(uint16_t))
2160 {
2161 /* This *should* be guest driver dropping index of a new descriptor in avail ring */
2162 virtioCoreVirtqNotified(pDevIns, pVirtio, uOffset / VIRTIO_NOTIFY_OFFSET_MULTIPLIER, *(uint16_t *)pv);
2163 rcStrict = VINF_SUCCESS;
2164 }
2165 else
2166 {
2167 ASSERT_GUEST_MSG_FAILED(("Bad write access to mapped capabilities region: off=%RGp pv=%#p{%.*Rhxs} cb=%u\n", off, pv, cb, pv, cb));
2168 rcStrict = PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS,
2169 "virtioMmioRead: Bad MMIO access to capabilities, offset=%RTiop cb=%08x\n", off, cb);
2170 }
2171
2172 STAM_PROFILE_ADV_STOP(&pVirtio->CTX_SUFF(StatWrite), a);
2173 return rcStrict;
2174}
2175
2176
2177/**
2178 * @callback_method_impl{FNIOMMMIONEWREAD,
2179 * Memory mapped I/O Handler for Virtio over MMIO read operations.}
2180 *
2181 */
2182static DECLCALLBACK(VBOXSTRICTRC) virtioMmioTransportRead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void *pv, unsigned cb)
2183{
2184 PVIRTIOCORE pVirtio = PDMINS_2_DATA(pDevIns, PVIRTIOCORE);
2185 PVIRTIOCORECC pVirtioCC = PDMINS_2_DATA_CC(pDevIns, PVIRTIOCORECC);
2186 RT_NOREF(pvUser);
2187 STAM_PROFILE_ADV_START(&pVirtio->CTX_SUFF(StatRead), a);
2188
2189 if (off >= VIRTIO_MMIO_SIZE)
2190 {
2191 VBOXSTRICTRC rcStrict = virtioDeviceCfgRead(pDevIns, pVirtio, pVirtioCC, (uint32_t)off - VIRTIO_MMIO_SIZE, pv, cb);
2192 STAM_PROFILE_ADV_STOP(&pVirtio->CTX_SUFF(StatRead), a);
2193 return rcStrict;
2194 }
2195
2196 /* All accesses below need to be aligned on a 32-bit boundary and must be 32-bit in size. */
2197 ASSERT_GUEST_MSG_RETURN(!(off & 0x3) && cb == sizeof(uint32_t),
2198 ("Bad read access: off=%RGp pv=%#p{%.*Rhxs} cb=%u\n", off, pv, cb, pv, cb),
2199 VINF_IOM_MMIO_UNUSED_FF);
2200
2201 int rc = VINF_SUCCESS;
2202 uint32_t *pu32 = (uint32_t *)pv;
2203 switch (off)
2204 {
2205 case VIRTIO_MMIO_REG_MAGIC_OFF:
2206 *pu32 = RT_H2LE_U32(VIRTIO_MMIO_REG_MAGIC_VALUE);
2207 break;
2208 case VIRTIO_MMIO_REG_VERSION_OFF:
2209 *pu32 = RT_H2LE_U32(VIRTIO_MMIO_REG_VERSION_VALUE);
2210 break;
2211 case VIRTIO_MMIO_REG_DEVICEID_OFF:
2212 *pu32 = pVirtio->uDeviceType;
2213 break;
2214 case VIRTIO_MMIO_REG_VENDORID_OFF:
2215 *pu32 = RT_H2LE_U32(DEVICE_PCI_VENDOR_ID_VIRTIO);
2216 break;
2217 case VIRTIO_MMIO_REG_DEVICEFEAT_OFF:
2218 {
2219 switch (pVirtio->uDeviceFeaturesSelect)
2220 {
2221 case 0:
2222 *pu32 = pVirtio->uDeviceFeatures & UINT32_C(0xffffffff);
2223 break;
2224 case 1:
2225 *pu32 = pVirtio->uDeviceFeatures >> 32;
2226 break;
2227 default:
2228 LogFunc(("Guest read uDeviceFeatures with out of range selector (%#x), returning 0\n",
2229 pVirtio->uDeviceFeaturesSelect));
2230 rc = VINF_IOM_MMIO_UNUSED_00;
2231 }
2232 break;
2233 }
2234 case VIRTIO_MMIO_REG_QUEUENUMMAX_OFF:
2235 *pu32 = VIRTQ_SIZE; /** @todo */
2236 break;
2237 case VIRTIO_MMIO_REG_QUEUERDY_OFF:
2238 {
2239 Assert(pVirtio->uVirtqSelect < RT_ELEMENTS(pVirtio->aVirtqueues));
2240 PVIRTQUEUE pVirtQueue = &pVirtio->aVirtqueues[pVirtio->uVirtqSelect];
2241 *pu32 = pVirtQueue->uEnable;
2242 break;
2243 }
2244 case VIRTIO_MMIO_REG_INTRSTATUS_OFF:
2245 *pu32 = pVirtio->uISR;
2246 break;
2247 case VIRTIO_MMIO_REG_DEVSTATUS_OFF:
2248 *pu32 = virtioDeviceStatusRead(pVirtio);
2249 break;
2250 case VIRTIO_MMIO_REG_CFGGEN_OFF:
2251 *pu32 = pVirtio->uConfigGeneration;
2252 break;
2253 default:
2254 ASSERT_GUEST_MSG_FAILED(("Bad read access to mapped capabilities region: off=%RGp cb=%u\n", off, cb));
2255 rc = PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS,
2256 "virtioMmioTransportRead: Bad MMIO access to capabilities, offset=%RTiop cb=%08x\n", off, cb);
2257 }
2258
2259 STAM_PROFILE_ADV_STOP(&pVirtio->CTX_SUFF(StatRead), a);
2260 return rc;
2261}
2262
2263/**
2264 * @callback_method_impl{FNIOMMMIONEWREAD,
2265 * Memory mapped I/O Handler for Virtio over MMIO write operations.}
2266 */
2267static DECLCALLBACK(VBOXSTRICTRC) virtioMmioTransportWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void const *pv, unsigned cb)
2268{
2269 PVIRTIOCORE pVirtio = PDMINS_2_DATA(pDevIns, PVIRTIOCORE);
2270 PVIRTIOCORECC pVirtioCC = PDMINS_2_DATA_CC(pDevIns, PVIRTIOCORECC);
2271 RT_NOREF(pvUser);
2272 STAM_PROFILE_ADV_START(&pVirtio->CTX_SUFF(StatWrite), a);
2273
2274 if (off >= VIRTIO_MMIO_SIZE)
2275 {
2276#ifdef IN_RING3
2277 /*
2278 * Forward this MMIO write access for client to deal with.
2279 */
2280 STAM_PROFILE_ADV_STOP(&pVirtio->CTX_SUFF(StatWrite), a);
2281 return pVirtioCC->pfnDevCapWrite(pDevIns, (uint32_t)off - VIRTIO_MMIO_SIZE, pv, cb);
2282#else
2283 STAM_PROFILE_ADV_STOP(&pVirtio->CTX_SUFF(StatWrite), a);
2284 Log6(("%-23s: RING0 => RING3 (demote)\n", __FUNCTION__));
2285 return VINF_IOM_R3_MMIO_WRITE;
2286#endif
2287 }
2288
2289 /* All accesses below need to be aligned on a 32-bit boundary and must be 32-bit in size. */
2290 ASSERT_GUEST_MSG_RETURN(!(off & 0x3) && cb == sizeof(uint32_t),
2291 ("Bad write access: off=%RGp pv=%#p{%.*Rhxs} cb=%u\n", off, pv, cb, pv, cb),
2292 VINF_SUCCESS);
2293
2294 int rc = VINF_SUCCESS;
2295 uint32_t const u32Val = *(const uint32_t *)pv;
2296 switch (off)
2297 {
2298 case VIRTIO_MMIO_REG_DEVICEFEATSEL_OFF:
2299 {
2300 pVirtio->uDeviceFeaturesSelect = u32Val;
2301 break;
2302 }
2303 case VIRTIO_MMIO_REG_DRIVERFEAT_OFF:
2304 {
2305 switch (pVirtio->uDriverFeaturesSelect)
2306 {
2307 case 0:
2308 pVirtio->uDriverFeatures = (pVirtio->uDriverFeatures & UINT64_C(0xffffffff00000000)) | u32Val;
2309 pVirtio->fDriverFeaturesWritten |= DRIVER_FEATURES_0_WRITTEN;
2310 LogFunc(("Set DRIVER_FEATURES_0_WRITTEN. pVirtio->fDriverFeaturesWritten=%d\n", pVirtio->fDriverFeaturesWritten));
2311 if ( (pVirtio->fDriverFeaturesWritten & DRIVER_FEATURES_0_AND_1_WRITTEN) == DRIVER_FEATURES_0_AND_1_WRITTEN
2312 && !(pVirtio->fDriverFeaturesWritten & DRIVER_FEATURES_COMPLETE_HANDLED))
2313#ifdef IN_RING0
2314 return VINF_IOM_R3_MMIO_WRITE;
2315#endif
2316#ifdef IN_RING3
2317 virtioR3DoFeaturesCompleteOnceOnly(pVirtio, pVirtioCC);
2318#endif
2319 break;
2320 case 1:
2321 pVirtio->uDriverFeatures = (pVirtio->uDriverFeatures & UINT64_C(0x00000000ffffffff)) | ((uint64_t)u32Val << 32);
2322 pVirtio->fDriverFeaturesWritten |= DRIVER_FEATURES_1_WRITTEN;
2323 LogFunc(("Set DRIVER_FEATURES_1_WRITTEN. pVirtio->fDriverFeaturesWritten=%d\n", pVirtio->fDriverFeaturesWritten));
2324 if ( (pVirtio->fDriverFeaturesWritten & DRIVER_FEATURES_0_AND_1_WRITTEN) == DRIVER_FEATURES_0_AND_1_WRITTEN
2325 && !(pVirtio->fDriverFeaturesWritten & DRIVER_FEATURES_COMPLETE_HANDLED))
2326#ifdef IN_RING0
2327 return VINF_IOM_R3_MMIO_WRITE;
2328#endif
2329#ifdef IN_RING3
2330 virtioR3DoFeaturesCompleteOnceOnly(pVirtio, pVirtioCC);
2331#endif
2332 break;
2333 default:
2334 LogFunc(("Guest wrote uDriverFeatures with out of range selector (%#x), returning 0\n",
2335 pVirtio->uDriverFeaturesSelect));
2336 return VINF_SUCCESS;
2337 }
2338 break;
2339 }
2340 case VIRTIO_MMIO_REG_DRIVERFEATSEL_OFF:
2341 {
2342 pVirtio->uDriverFeaturesSelect = u32Val;
2343 break;
2344 }
2345 case VIRTIO_MMIO_REG_QUEUESEL_OFF:
2346 {
2347 if (u32Val < RT_ELEMENTS(pVirtio->aVirtqueues))
2348 pVirtio->uVirtqSelect = (uint16_t)u32Val;
2349 else
2350 LogFunc(("... WARNING: Guest attempted to write invalid virtq selector (ignoring)\n"));
2351 break;
2352 }
2353 case VIRTIO_MMIO_REG_QUEUENUM_OFF:
2354 {
2355 Assert(pVirtio->uVirtqSelect < RT_ELEMENTS(pVirtio->aVirtqueues));
2356 PVIRTQUEUE pVirtQueue = &pVirtio->aVirtqueues[pVirtio->uVirtqSelect];
2357 pVirtQueue->uQueueSize = (uint16_t)u32Val;
2358 break;
2359 }
2360 case VIRTIO_MMIO_REG_QUEUERDY_OFF:
2361 {
2362 Assert(pVirtio->uVirtqSelect < RT_ELEMENTS(pVirtio->aVirtqueues));
2363 PVIRTQUEUE pVirtQueue = &pVirtio->aVirtqueues[pVirtio->uVirtqSelect];
2364 pVirtQueue->uEnable = (uint16_t)u32Val;
2365 break;
2366 }
2367 case VIRTIO_MMIO_REG_QUEUENOTIFY_OFF:
2368 {
2369 virtioCoreVirtqNotified(pDevIns, pVirtio, u32Val, (uint16_t)u32Val);
2370 break;
2371 }
2372 case VIRTIO_MMIO_REG_INTRACK_OFF:
2373 {
2374 pVirtio->uISR &= ~u32Val;
2375 if (!pVirtio->uISR)
2376 virtioLowerInterrupt(pDevIns, 0);
2377 break;
2378 }
2379 case VIRTIO_MMIO_REG_DEVSTATUS_OFF:
2380 {
2381 rc = virtioDeviceStatusWrite(pDevIns, pVirtio, pVirtioCC, (uint8_t)u32Val);
2382 break;
2383 }
2384 case VIRTIO_MMIO_REG_QUEUEALIGN_LEGACY_OFF:
2385 {
2386 /* Written by edk2 even though we don't offer legacy mode, ignore. */
2387 break;
2388 }
2389 case VIRTIO_MMIO_REG_QUEUEDESCLOW_OFF:
2390 {
2391 Assert(pVirtio->uVirtqSelect < RT_ELEMENTS(pVirtio->aVirtqueues));
2392 PVIRTQUEUE pVirtQueue = &pVirtio->aVirtqueues[pVirtio->uVirtqSelect];
2393 pVirtQueue->GCPhysVirtqDesc = (pVirtQueue->GCPhysVirtqDesc & UINT64_C(0xffffffff00000000)) | u32Val;
2394 break;
2395 }
2396 case VIRTIO_MMIO_REG_QUEUEDESCHIGH_OFF:
2397 {
2398 Assert(pVirtio->uVirtqSelect < RT_ELEMENTS(pVirtio->aVirtqueues));
2399 PVIRTQUEUE pVirtQueue = &pVirtio->aVirtqueues[pVirtio->uVirtqSelect];
2400 pVirtQueue->GCPhysVirtqDesc = (pVirtQueue->GCPhysVirtqDesc & UINT64_C(0x00000000ffffffff)) | ((uint64_t)u32Val << 32);
2401 break;
2402 }
2403 case VIRTIO_MMIO_REG_QUEUEDRVLOW_OFF:
2404 {
2405 Assert(pVirtio->uVirtqSelect < RT_ELEMENTS(pVirtio->aVirtqueues));
2406 PVIRTQUEUE pVirtQueue = &pVirtio->aVirtqueues[pVirtio->uVirtqSelect];
2407 pVirtQueue->GCPhysVirtqAvail = (pVirtQueue->GCPhysVirtqAvail & UINT64_C(0xffffffff00000000)) | u32Val;
2408 break;
2409 }
2410 case VIRTIO_MMIO_REG_QUEUEDRVHIGH_OFF:
2411 {
2412 Assert(pVirtio->uVirtqSelect < RT_ELEMENTS(pVirtio->aVirtqueues));
2413 PVIRTQUEUE pVirtQueue = &pVirtio->aVirtqueues[pVirtio->uVirtqSelect];
2414 pVirtQueue->GCPhysVirtqAvail = (pVirtQueue->GCPhysVirtqAvail & UINT64_C(0x00000000ffffffff)) | ((uint64_t)u32Val << 32);
2415 break;
2416 }
2417 case VIRTIO_MMIO_REG_QUEUEDEVLOW_OFF:
2418 {
2419 Assert(pVirtio->uVirtqSelect < RT_ELEMENTS(pVirtio->aVirtqueues));
2420 PVIRTQUEUE pVirtQueue = &pVirtio->aVirtqueues[pVirtio->uVirtqSelect];
2421 pVirtQueue->GCPhysVirtqUsed = (pVirtQueue->GCPhysVirtqUsed & UINT64_C(0xffffffff00000000)) | u32Val;
2422 break;
2423 }
2424 case VIRTIO_MMIO_REG_QUEUEDEVHIGH_OFF:
2425 {
2426 Assert(pVirtio->uVirtqSelect < RT_ELEMENTS(pVirtio->aVirtqueues));
2427 PVIRTQUEUE pVirtQueue = &pVirtio->aVirtqueues[pVirtio->uVirtqSelect];
2428 pVirtQueue->GCPhysVirtqUsed = (pVirtQueue->GCPhysVirtqUsed & UINT64_C(0x00000000ffffffff)) | ((uint64_t)u32Val << 32);
2429 break;
2430 }
2431 default:
2432 ASSERT_GUEST_MSG_FAILED(("Bad write access to mapped capabilities region: off=%RGp pv=%#p{%.*Rhxs} cb=%u\n", off, pv, cb, pv, cb));
2433 rc = PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS,
2434 "virtioMmioTransportWrite: Bad MMIO access to capabilities, offset=%RTiop cb=%08x\n", off, cb);
2435 }
2436
2437 STAM_PROFILE_ADV_STOP(&pVirtio->CTX_SUFF(StatWrite), a);
2438 return rc;
2439}
2440
2441
2442#ifdef IN_RING3
2443
2444/**
2445 * @callback_method_impl{FNPCICONFIGREAD}
2446 */
2447static DECLCALLBACK(VBOXSTRICTRC) virtioR3PciConfigRead(PPDMDEVINS pDevIns, PPDMPCIDEV pPciDev,
2448 uint32_t uAddress, unsigned cb, uint32_t *pu32Value)
2449{
2450 PVIRTIOCORE pVirtio = PDMINS_2_DATA(pDevIns, PVIRTIOCORE);
2451 PVIRTIOCORECC pVirtioCC = PDMINS_2_DATA_CC(pDevIns, PVIRTIOCORECC);
2452 RT_NOREF(pPciDev);
2453
2454 if (uAddress == pVirtio->uPciCfgDataOff)
2455 {
2456 /* See comments in PCI Cfg capability initialization (in capabilities setup section of this code) */
2457 struct virtio_pci_cap *pPciCap = &pVirtioCC->pPciCfgCap->pciCap;
2458 uint32_t uLength = pPciCap->uLength;
2459
2460 Log7Func((" pDevIns=%p pPciDev=%p uAddress=%#x%s cb=%u uLength=%d, bar=%d\n",
2461 pDevIns, pPciDev, uAddress, uAddress < 0x10 ? " " : "", cb, uLength, pPciCap->uBar));
2462
2463 if ( (uLength != 1 && uLength != 2 && uLength != 4)
2464 || pPciCap->uBar != VIRTIO_REGION_PCI_CAP)
2465 {
2466 ASSERT_GUEST_MSG_FAILED(("Guest read virtio_pci_cfg_cap.pci_cfg_data using mismatching config. "
2467 "Ignoring\n"));
2468 *pu32Value = UINT32_MAX;
2469 return VINF_SUCCESS;
2470 }
2471
2472 VBOXSTRICTRC rcStrict = virtioMmioRead(pDevIns, pVirtio, pPciCap->uOffset, pu32Value, cb);
2473 Log7Func((" Guest read virtio_pci_cfg_cap.pci_cfg_data, bar=%d, offset=%d, length=%d, result=0x%x -> %Rrc\n",
2474 pPciCap->uBar, pPciCap->uOffset, uLength, *pu32Value, VBOXSTRICTRC_VAL(rcStrict)));
2475 return rcStrict;
2476 }
2477 Log7Func((" pDevIns=%p pPciDev=%p uAddress=%#x%s cb=%u pu32Value=%p\n",
2478 pDevIns, pPciDev, uAddress, uAddress < 0x10 ? " " : "", cb, pu32Value));
2479 return VINF_PDM_PCI_DO_DEFAULT;
2480}
2481
2482/**
2483 * @callback_method_impl{FNPCICONFIGWRITE}
2484 */
2485static DECLCALLBACK(VBOXSTRICTRC) virtioR3PciConfigWrite(PPDMDEVINS pDevIns, PPDMPCIDEV pPciDev,
2486 uint32_t uAddress, unsigned cb, uint32_t u32Value)
2487{
2488 PVIRTIOCORE pVirtio = PDMINS_2_DATA(pDevIns, PVIRTIOCORE);
2489 PVIRTIOCORECC pVirtioCC = PDMINS_2_DATA_CC(pDevIns, PVIRTIOCORECC);
2490 RT_NOREF(pPciDev);
2491
2492 Log7Func(("pDevIns=%p pPciDev=%p uAddress=%#x %scb=%u u32Value=%#x\n", pDevIns, pPciDev, uAddress, uAddress < 0xf ? " " : "", cb, u32Value));
2493 if (uAddress == pVirtio->uPciCfgDataOff)
2494 {
2495 /* See comments in PCI Cfg capability initialization (in capabilities setup section of this code) */
2496 struct virtio_pci_cap *pPciCap = &pVirtioCC->pPciCfgCap->pciCap;
2497 uint32_t uLength = pPciCap->uLength;
2498
2499 if ( (uLength != 1 && uLength != 2 && uLength != 4)
2500 || cb != uLength
2501 || pPciCap->uBar != VIRTIO_REGION_PCI_CAP)
2502 {
2503 ASSERT_GUEST_MSG_FAILED(("Guest write virtio_pci_cfg_cap.pci_cfg_data using mismatching config. Ignoring\n"));
2504 return VINF_SUCCESS;
2505 }
2506
2507 VBOXSTRICTRC rcStrict = virtioMmioWrite(pDevIns, pVirtio, pPciCap->uOffset, &u32Value, cb);
2508 Log2Func(("Guest wrote virtio_pci_cfg_cap.pci_cfg_data, bar=%d, offset=%x, length=%x, value=%d -> %Rrc\n",
2509 pPciCap->uBar, pPciCap->uOffset, uLength, u32Value, VBOXSTRICTRC_VAL(rcStrict)));
2510 return rcStrict;
2511 }
2512 return VINF_PDM_PCI_DO_DEFAULT;
2513}
2514
2515
2516/*********************************************************************************************************************************
2517* Saved state (SSM) *
2518*********************************************************************************************************************************/
2519
2520
2521/**
2522 * Loads a saved device state (called from device-specific code on SSM final pass)
2523 *
2524 * @param pVirtio Pointer to the shared virtio state.
2525 * @param pHlp The ring-3 device helpers.
2526 * @param pSSM The saved state handle.
2527 * @returns VBox status code.
2528 */
2529DECLHIDDEN(int) virtioCoreR3LegacyDeviceLoadExec(PVIRTIOCORE pVirtio, PCPDMDEVHLPR3 pHlp, PSSMHANDLE pSSM,
2530 uint32_t uVersion, uint32_t uVirtioLegacy_3_1_Beta)
2531{
2532 int rc;
2533 uint32_t uDriverFeaturesLegacy32bit;
2534
2535 rc = pHlp->pfnSSMGetU32( pSSM, &uDriverFeaturesLegacy32bit);
2536 AssertRCReturn(rc, rc);
2537 pVirtio->uDriverFeatures = (uint64_t)uDriverFeaturesLegacy32bit;
2538
2539 rc = pHlp->pfnSSMGetU16( pSSM, &pVirtio->uVirtqSelect);
2540 AssertRCReturn(rc, rc);
2541
2542 rc = pHlp->pfnSSMGetU8( pSSM, &pVirtio->fDeviceStatus);
2543 AssertRCReturn(rc, rc);
2544
2545#ifdef LOG_ENABLED
2546 char szOut[80] = { 0 };
2547 virtioCoreFormatDeviceStatus(pVirtio->fDeviceStatus, szOut, sizeof(szOut));
2548 Log(("Loaded legacy device status = (%s)\n", szOut));
2549#endif
2550
2551 rc = pHlp->pfnSSMGetU8( pSSM, &pVirtio->uISR);
2552 AssertRCReturn(rc, rc);
2553
2554 uint32_t cQueues = 3; /* This constant default value copied from earliest v0.9 code */
2555 if (uVersion > uVirtioLegacy_3_1_Beta)
2556 {
2557 rc = pHlp->pfnSSMGetU32(pSSM, &cQueues);
2558 AssertRCReturn(rc, rc);
2559 }
2560
2561 AssertLogRelMsgReturn(cQueues <= VIRTQ_MAX_COUNT, ("%#x\n", cQueues), VERR_SSM_LOAD_CONFIG_MISMATCH);
2562 AssertLogRelMsgReturn(pVirtio->uVirtqSelect < cQueues || (cQueues == 0 && pVirtio->uVirtqSelect),
2563 ("uVirtqSelect=%u cQueues=%u\n", pVirtio->uVirtqSelect, cQueues),
2564 VERR_SSM_LOAD_CONFIG_MISMATCH);
2565
2566 Log(("\nRestoring %d legacy-only virtio-net device queues from saved state:\n", cQueues));
2567 for (unsigned uVirtq = 0; uVirtq < cQueues; uVirtq++)
2568 {
2569 PVIRTQUEUE pVirtq = &pVirtio->aVirtqueues[uVirtq];
2570
2571 if (uVirtq == cQueues - 1)
2572 RTStrPrintf(pVirtq->szName, sizeof(pVirtq->szName), "legacy-ctrlq");
2573 else if (uVirtq % 2)
2574 RTStrPrintf(pVirtq->szName, sizeof(pVirtq->szName), "legacy-xmitq<%d>", uVirtq / 2);
2575 else
2576 RTStrPrintf(pVirtq->szName, sizeof(pVirtq->szName), "legacy-recvq<%d>", uVirtq / 2);
2577
2578 rc = pHlp->pfnSSMGetU16(pSSM, &pVirtq->uQueueSize);
2579 AssertRCReturn(rc, rc);
2580
2581 uint32_t uVirtqPfn;
2582 rc = pHlp->pfnSSMGetU32(pSSM, &uVirtqPfn);
2583 AssertRCReturn(rc, rc);
2584
2585 rc = pHlp->pfnSSMGetU16(pSSM, &pVirtq->uAvailIdxShadow);
2586 AssertRCReturn(rc, rc);
2587
2588 rc = pHlp->pfnSSMGetU16(pSSM, &pVirtq->uUsedIdxShadow);
2589 AssertRCReturn(rc, rc);
2590
2591 if (uVirtqPfn)
2592 {
2593 pVirtq->GCPhysVirtqDesc = (uint64_t)uVirtqPfn * VIRTIO_PAGE_SIZE;
2594 pVirtq->GCPhysVirtqAvail = pVirtq->GCPhysVirtqDesc + sizeof(VIRTQ_DESC_T) * pVirtq->uQueueSize;
2595 pVirtq->GCPhysVirtqUsed =
2596 RT_ALIGN(pVirtq->GCPhysVirtqAvail + RT_UOFFSETOF_DYN(VIRTQ_AVAIL_T, auRing[pVirtq->uQueueSize]), VIRTIO_PAGE_SIZE);
2597 pVirtq->uEnable = 1;
2598 }
2599 else
2600 {
2601 LogFunc(("WARNING: QUEUE \"%s\" PAGE NUMBER ZERO IN SAVED STATE\n", pVirtq->szName));
2602 pVirtq->uEnable = 0;
2603 }
2604 pVirtq->uNotifyOffset = 0; /* unused in legacy mode */
2605 pVirtq->uMsixVector = 0; /* unused in legacy mode */
2606 }
2607 pVirtio->fGenUpdatePending = 0; /* unused in legacy mode */
2608 pVirtio->uConfigGeneration = 0; /* unused in legacy mode */
2609 pVirtio->uPciCfgDataOff = 0; /* unused in legacy mode (port I/O used instead) */
2610
2611 return VINF_SUCCESS;
2612}
2613
2614/**
2615 * Loads a saved device state (called from device-specific code on SSM final pass)
2616 *
2617 * Note: This loads state saved by a Modern (VirtIO 1.0+) device, of which this transitional device is one,
2618 * and thus supports both legacy and modern guest virtio drivers.
2619 *
2620 * @param pVirtio Pointer to the shared virtio state.
2621 * @param pHlp The ring-3 device helpers.
2622 * @param pSSM The saved state handle.
2623 * @returns VBox status code.
2624 */
2625DECLHIDDEN(int) virtioCoreR3ModernDeviceLoadExec(PVIRTIOCORE pVirtio, PCPDMDEVHLPR3 pHlp, PSSMHANDLE pSSM,
2626 uint32_t uVersion, uint32_t uTestVersion, uint32_t cQueues)
2627{
2628 RT_NOREF2(cQueues, uVersion);
2629 LogFunc(("\n"));
2630 /*
2631 * Check the marker and (embedded) version number.
2632 */
2633 uint64_t uMarker = 0;
2634 int rc;
2635
2636 rc = pHlp->pfnSSMGetU64(pSSM, &uMarker);
2637 AssertRCReturn(rc, rc);
2638 if (uMarker != VIRTIO_SAVEDSTATE_MARKER)
2639 return pHlp->pfnSSMSetLoadError(pSSM, VERR_SSM_DATA_UNIT_FORMAT_CHANGED, RT_SRC_POS,
2640 N_("Expected marker value %#RX64 found %#RX64 instead"),
2641 VIRTIO_SAVEDSTATE_MARKER, uMarker);
2642 uint32_t uVersionSaved = 0;
2643 rc = pHlp->pfnSSMGetU32(pSSM, &uVersionSaved);
2644 AssertRCReturn(rc, rc);
2645 if (uVersionSaved != uTestVersion)
2646 return pHlp->pfnSSMSetLoadError(pSSM, VERR_SSM_DATA_UNIT_FORMAT_CHANGED, RT_SRC_POS,
2647 N_("Unsupported virtio version: %u"), uVersionSaved);
2648 /*
2649 * Load the state.
2650 */
2651 rc = pHlp->pfnSSMGetU32( pSSM, &pVirtio->fLegacyDriver);
2652 AssertRCReturn(rc, rc);
2653 rc = pHlp->pfnSSMGetBool( pSSM, &pVirtio->fGenUpdatePending);
2654 AssertRCReturn(rc, rc);
2655 rc = pHlp->pfnSSMGetU8( pSSM, &pVirtio->fDeviceStatus);
2656 AssertRCReturn(rc, rc);
2657 rc = pHlp->pfnSSMGetU8( pSSM, &pVirtio->uConfigGeneration);
2658 AssertRCReturn(rc, rc);
2659 rc = pHlp->pfnSSMGetU8( pSSM, &pVirtio->uPciCfgDataOff);
2660 AssertRCReturn(rc, rc);
2661 rc = pHlp->pfnSSMGetU8( pSSM, &pVirtio->uISR);
2662 AssertRCReturn(rc, rc);
2663 rc = pHlp->pfnSSMGetU16( pSSM, &pVirtio->uVirtqSelect);
2664 AssertRCReturn(rc, rc);
2665 rc = pHlp->pfnSSMGetU32( pSSM, &pVirtio->uDeviceFeaturesSelect);
2666 AssertRCReturn(rc, rc);
2667 rc = pHlp->pfnSSMGetU32( pSSM, &pVirtio->uDriverFeaturesSelect);
2668 AssertRCReturn(rc, rc);
2669 rc = pHlp->pfnSSMGetU64( pSSM, &pVirtio->uDriverFeatures);
2670 AssertRCReturn(rc, rc);
2671
2672 /** @todo Adapt this loop use cQueues argument instead of static queue count (safely with SSM versioning) */
2673 for (uint32_t i = 0; i < VIRTQ_MAX_COUNT; i++)
2674 {
2675 PVIRTQUEUE pVirtq = &pVirtio->aVirtqueues[i];
2676 rc = pHlp->pfnSSMGetGCPhys64( pSSM, &pVirtq->GCPhysVirtqDesc);
2677 AssertRCReturn(rc, rc);
2678 rc = pHlp->pfnSSMGetGCPhys64( pSSM, &pVirtq->GCPhysVirtqAvail);
2679 AssertRCReturn(rc, rc);
2680 rc = pHlp->pfnSSMGetGCPhys64( pSSM, &pVirtq->GCPhysVirtqUsed);
2681 AssertRCReturn(rc, rc);
2682 rc = pHlp->pfnSSMGetU16( pSSM, &pVirtq->uNotifyOffset);
2683 AssertRCReturn(rc, rc);
2684 rc = pHlp->pfnSSMGetU16( pSSM, &pVirtq->uMsixVector);
2685 AssertRCReturn(rc, rc);
2686 rc = pHlp->pfnSSMGetU16( pSSM, &pVirtq->uEnable);
2687 AssertRCReturn(rc, rc);
2688 rc = pHlp->pfnSSMGetU16( pSSM, &pVirtq->uQueueSize);
2689 AssertRCReturn(rc, rc);
2690 rc = pHlp->pfnSSMGetU16( pSSM, &pVirtq->uAvailIdxShadow);
2691 AssertRCReturn(rc, rc);
2692 rc = pHlp->pfnSSMGetU16( pSSM, &pVirtq->uUsedIdxShadow);
2693 AssertRCReturn(rc, rc);
2694 rc = pHlp->pfnSSMGetMem( pSSM, pVirtq->szName, sizeof(pVirtq->szName));
2695 AssertRCReturn(rc, rc);
2696 }
2697 return VINF_SUCCESS;
2698}
2699
2700/**
2701 * Called from the FNSSMDEVSAVEEXEC function of the device.
2702 *
2703 * @param pVirtio Pointer to the shared virtio state.
2704 * @param pHlp The ring-3 device helpers.
2705 * @param pSSM The saved state handle.
2706 * @returns VBox status code.
2707 */
2708DECLHIDDEN(int) virtioCoreR3SaveExec(PVIRTIOCORE pVirtio, PCPDMDEVHLPR3 pHlp, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t cQueues)
2709{
2710 RT_NOREF(cQueues);
2711 /** @todo figure out a way to save cQueues (with SSM versioning) */
2712
2713 LogFunc(("\n"));
2714 pHlp->pfnSSMPutU64(pSSM, VIRTIO_SAVEDSTATE_MARKER);
2715 pHlp->pfnSSMPutU32(pSSM, uVersion);
2716
2717 pHlp->pfnSSMPutU32( pSSM, pVirtio->fLegacyDriver);
2718 pHlp->pfnSSMPutBool(pSSM, pVirtio->fGenUpdatePending);
2719 pHlp->pfnSSMPutU8( pSSM, pVirtio->fDeviceStatus);
2720 pHlp->pfnSSMPutU8( pSSM, pVirtio->uConfigGeneration);
2721 pHlp->pfnSSMPutU8( pSSM, pVirtio->uPciCfgDataOff);
2722 pHlp->pfnSSMPutU8( pSSM, pVirtio->uISR);
2723 pHlp->pfnSSMPutU16( pSSM, pVirtio->uVirtqSelect);
2724 pHlp->pfnSSMPutU32( pSSM, pVirtio->uDeviceFeaturesSelect);
2725 pHlp->pfnSSMPutU32( pSSM, pVirtio->uDriverFeaturesSelect);
2726 pHlp->pfnSSMPutU64( pSSM, pVirtio->uDriverFeatures);
2727
2728 for (uint32_t i = 0; i < VIRTQ_MAX_COUNT; i++)
2729 {
2730 PVIRTQUEUE pVirtq = &pVirtio->aVirtqueues[i];
2731
2732 pHlp->pfnSSMPutGCPhys64( pSSM, pVirtq->GCPhysVirtqDesc);
2733 pHlp->pfnSSMPutGCPhys64( pSSM, pVirtq->GCPhysVirtqAvail);
2734 pHlp->pfnSSMPutGCPhys64( pSSM, pVirtq->GCPhysVirtqUsed);
2735 pHlp->pfnSSMPutU16( pSSM, pVirtq->uNotifyOffset);
2736 pHlp->pfnSSMPutU16( pSSM, pVirtq->uMsixVector);
2737 pHlp->pfnSSMPutU16( pSSM, pVirtq->uEnable);
2738 pHlp->pfnSSMPutU16( pSSM, pVirtq->uQueueSize);
2739 pHlp->pfnSSMPutU16( pSSM, pVirtq->uAvailIdxShadow);
2740 pHlp->pfnSSMPutU16( pSSM, pVirtq->uUsedIdxShadow);
2741 int rc = pHlp->pfnSSMPutMem(pSSM, pVirtq->szName, 32);
2742 AssertRCReturn(rc, rc);
2743 }
2744 return VINF_SUCCESS;
2745}
2746
2747
2748/*********************************************************************************************************************************
2749* Device Level *
2750*********************************************************************************************************************************/
2751
2752/**
2753 * This must be called by the client to handle VM state changes after the client takes care of its device-specific
2754 * tasks for the state change (i.e. reset, suspend, power-off, resume)
2755 *
2756 * @param pDevIns The device instance.
2757 * @param pVirtio Pointer to the shared virtio state.
2758 */
2759DECLHIDDEN(void) virtioCoreR3VmStateChanged(PVIRTIOCORE pVirtio, VIRTIOVMSTATECHANGED enmState)
2760{
2761 LogFunc(("State changing to %s\n",
2762 virtioCoreGetStateChangeText(enmState)));
2763
2764 switch(enmState)
2765 {
2766 case kvirtIoVmStateChangedReset:
2767 virtioCoreResetAll(pVirtio);
2768 break;
2769 case kvirtIoVmStateChangedSuspend:
2770 break;
2771 case kvirtIoVmStateChangedPowerOff:
2772 break;
2773 case kvirtIoVmStateChangedResume:
2774 for (int uVirtq = 0; uVirtq < VIRTQ_MAX_COUNT; uVirtq++)
2775 {
2776 if ((!pVirtio->fLegacyDriver && pVirtio->aVirtqueues[uVirtq].uEnable)
2777 | pVirtio->aVirtqueues[uVirtq].GCPhysVirtqDesc)
2778 virtioCoreNotifyGuestDriver(pVirtio->pDevInsR3, pVirtio, uVirtq);
2779 }
2780 break;
2781 default:
2782 LogRelFunc(("Bad enum value"));
2783 return;
2784 }
2785}
2786
2787/**
2788 * This should be called from PDMDEVREGR3::pfnDestruct.
2789 *
2790 * @param pDevIns The device instance.
2791 * @param pVirtio Pointer to the shared virtio state.
2792 * @param pVirtioCC Pointer to the ring-3 virtio state.
2793 */
2794DECLHIDDEN(void) virtioCoreR3Term(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, PVIRTIOCORECC pVirtioCC)
2795{
2796 if (pVirtioCC->pbPrevDevSpecificCfg)
2797 {
2798 RTMemFree(pVirtioCC->pbPrevDevSpecificCfg);
2799 pVirtioCC->pbPrevDevSpecificCfg = NULL;
2800 }
2801
2802 RT_NOREF(pDevIns, pVirtio);
2803}
2804
2805
2806/**
2807 * Setup the Virtio device as a PCI device.
2808 *
2809 * @returns VBox status code.
2810 * @param pDevIns Device instance.
2811 * @param pVirtio Pointer to the shared virtio state. This
2812 * must be the first member in the shared
2813 * device instance data!
2814 * @param pVirtioCC Pointer to the ring-3 virtio state. This
2815 * must be the first member in the ring-3
2816 * device instance data!
2817 * @param pPciParams Values to populate industry standard PCI Configuration Space data structure
2818 * @param pcszInstance Device instance name (format-specifier)
2819 * @param cbDevSpecificCfg Size of virtio_pci_device_cap device-specific struct
2820 */
2821static int virtioR3PciTransportInit(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, PVIRTIOCORECC pVirtioCC, PVIRTIOPCIPARAMS pPciParams,
2822 const char *pcszInstance, uint16_t cbDevSpecificCfg)
2823{
2824 /* Set PCI config registers (assume 32-bit mode) */
2825 PPDMPCIDEV pPciDev = pDevIns->apPciDevs[0];
2826 PDMPCIDEV_ASSERT_VALID(pDevIns, pPciDev);
2827
2828 PDMPciDevSetVendorId(pPciDev, DEVICE_PCI_VENDOR_ID_VIRTIO);
2829 PDMPciDevSetDeviceId(pPciDev, pPciParams->uDeviceId);
2830
2831 if (pPciParams->uDeviceId < DEVICE_PCI_DEVICE_ID_VIRTIO_BASE)
2832 /* Transitional devices MUST have a PCI Revision ID of 0. */
2833 PDMPciDevSetRevisionId(pPciDev, DEVICE_PCI_REVISION_ID_VIRTIO_TRANS);
2834 else
2835 /* Non-transitional devices SHOULD have a PCI Revision ID of 1 or higher. */
2836 PDMPciDevSetRevisionId(pPciDev, DEVICE_PCI_REVISION_ID_VIRTIO_V1);
2837
2838 PDMPciDevSetSubSystemId(pPciDev, pPciParams->uSubsystemId);
2839 PDMPciDevSetSubSystemVendorId(pPciDev, DEVICE_PCI_VENDOR_ID_VIRTIO);
2840 PDMPciDevSetClassBase(pPciDev, pPciParams->uClassBase);
2841 PDMPciDevSetClassSub(pPciDev, pPciParams->uClassSub);
2842 PDMPciDevSetClassProg(pPciDev, pPciParams->uClassProg);
2843 PDMPciDevSetInterruptLine(pPciDev, pPciParams->uInterruptLine);
2844 PDMPciDevSetInterruptPin(pPciDev, pPciParams->uInterruptPin);
2845
2846 /* Register PCI device */
2847 int rc = PDMDevHlpPCIRegister(pDevIns, pPciDev);
2848 if (RT_FAILURE(rc))
2849 return PDMDEV_SET_ERROR(pDevIns, rc, N_("virtio: cannot register PCI Device")); /* can we put params in this error? */
2850
2851 rc = PDMDevHlpPCIInterceptConfigAccesses(pDevIns, pPciDev, virtioR3PciConfigRead, virtioR3PciConfigWrite);
2852 AssertRCReturn(rc, rc);
2853
2854 /* Construct & map PCI vendor-specific capabilities for virtio host negotiation with guest driver */
2855
2856#define CFG_ADDR_2_IDX(addr) ((uint8_t)(((uintptr_t)(addr) - (uintptr_t)&pPciDev->abConfig[0])))
2857#define SET_PCI_CAP_LOC(a_pPciDev, a_pCfg, a_LocCap, a_uMmioLengthAlign) \
2858 do { \
2859 (a_LocCap).offMmio = (a_pCfg)->uOffset; \
2860 (a_LocCap).cbMmio = RT_ALIGN_T((a_pCfg)->uLength, a_uMmioLengthAlign, uint16_t); \
2861 (a_LocCap).offPci = (uint16_t)(uintptr_t)((uint8_t *)(a_pCfg) - &(a_pPciDev)->abConfig[0]); \
2862 (a_LocCap).cbPci = (a_pCfg)->uCapLen; \
2863 } while (0)
2864
2865 PVIRTIO_PCI_CAP_T pCfg;
2866 uint32_t cbRegion = 0;
2867
2868 /*
2869 * Common capability (VirtIO 1.0, section 4.1.4.3)
2870 */
2871 pCfg = (PVIRTIO_PCI_CAP_T)&pPciDev->abConfig[0x40];
2872 pCfg->uCfgType = VIRTIO_PCI_CAP_COMMON_CFG;
2873 pCfg->uCapVndr = VIRTIO_PCI_CAP_ID_VENDOR;
2874 pCfg->uCapLen = sizeof(VIRTIO_PCI_CAP_T);
2875 pCfg->uCapNext = CFG_ADDR_2_IDX(pCfg) + pCfg->uCapLen;
2876 pCfg->uBar = VIRTIO_REGION_PCI_CAP;
2877 pCfg->uOffset = RT_ALIGN_32(0, 4); /* Currently 0, but reminder to 32-bit align if changing this */
2878 pCfg->uLength = sizeof(VIRTIO_PCI_COMMON_CFG_T);
2879 cbRegion += pCfg->uLength;
2880 SET_PCI_CAP_LOC(pPciDev, pCfg, pVirtio->LocCommonCfgCap, 2);
2881 pVirtioCC->pCommonCfgCap = pCfg;
2882
2883 /*
2884 * Notify capability (VirtIO 1.0, section 4.1.4.4).
2885 *
2886 * The size of the spec-defined subregion described by this VirtIO capability is
2887 * based-on the choice of this implementation to make the notification area of each
2888 * queue equal to queue's ordinal position (e.g. queue selector value). The VirtIO
2889 * specification leaves it up to implementation to define queue notification area layout.
2890 */
2891 pCfg = (PVIRTIO_PCI_CAP_T)&pPciDev->abConfig[pCfg->uCapNext];
2892 pCfg->uCfgType = VIRTIO_PCI_CAP_NOTIFY_CFG;
2893 pCfg->uCapVndr = VIRTIO_PCI_CAP_ID_VENDOR;
2894 pCfg->uCapLen = sizeof(VIRTIO_PCI_NOTIFY_CAP_T);
2895 pCfg->uCapNext = CFG_ADDR_2_IDX(pCfg) + pCfg->uCapLen;
2896 pCfg->uBar = VIRTIO_REGION_PCI_CAP;
2897 pCfg->uOffset = pVirtioCC->pCommonCfgCap->uOffset + pVirtioCC->pCommonCfgCap->uLength;
2898 pCfg->uOffset = RT_ALIGN_32(pCfg->uOffset, 4);
2899 pCfg->uLength = VIRTQ_MAX_COUNT * VIRTIO_NOTIFY_OFFSET_MULTIPLIER + 2; /* will change in VirtIO 1.1 */
2900 cbRegion += pCfg->uLength;
2901 SET_PCI_CAP_LOC(pPciDev, pCfg, pVirtio->LocNotifyCap, 1);
2902 pVirtioCC->pNotifyCap = (PVIRTIO_PCI_NOTIFY_CAP_T)pCfg;
2903 pVirtioCC->pNotifyCap->uNotifyOffMultiplier = VIRTIO_NOTIFY_OFFSET_MULTIPLIER;
2904
2905 /* ISR capability (VirtIO 1.0, section 4.1.4.5)
2906 *
2907 * VirtIO 1.0 spec says 8-bit, unaligned in MMIO space. The specification example/diagram
2908 * illustrates this capability as 32-bit field with upper bits 'reserved'. Those depictions
2909 * differ. The spec's wording, not the diagram, is seen to work in practice.
2910 */
2911 pCfg = (PVIRTIO_PCI_CAP_T)&pPciDev->abConfig[pCfg->uCapNext];
2912 pCfg->uCfgType = VIRTIO_PCI_CAP_ISR_CFG;
2913 pCfg->uCapVndr = VIRTIO_PCI_CAP_ID_VENDOR;
2914 pCfg->uCapLen = sizeof(VIRTIO_PCI_CAP_T);
2915 pCfg->uCapNext = CFG_ADDR_2_IDX(pCfg) + pCfg->uCapLen;
2916 pCfg->uBar = VIRTIO_REGION_PCI_CAP;
2917 pCfg->uOffset = pVirtioCC->pNotifyCap->pciCap.uOffset + pVirtioCC->pNotifyCap->pciCap.uLength;
2918 pCfg->uOffset = RT_ALIGN_32(pCfg->uOffset, 4);
2919 pCfg->uLength = sizeof(uint8_t);
2920 cbRegion += pCfg->uLength;
2921 SET_PCI_CAP_LOC(pPciDev, pCfg, pVirtio->LocIsrCap, 4);
2922 pVirtioCC->pIsrCap = pCfg;
2923
2924 /* PCI Cfg capability (VirtIO 1.0, section 4.1.4.7)
2925 *
2926 * This capability facilitates early-boot access to this device (BIOS).
2927 * This region isn't page-MMIO mapped. PCI configuration accesses are intercepted,
2928 * wherein uBar, uOffset and uLength are modulated by consumers to locate and read/write
2929 * values in any part of any region. (NOTE: Linux driver doesn't utilize this feature.
2930 * This capability only appears in lspci output on Linux if uLength is non-zero, 4-byte aligned,
2931 * during initialization of linux virtio driver).
2932 */
2933 pVirtio->uPciCfgDataOff = pCfg->uCapNext + RT_OFFSETOF(VIRTIO_PCI_CFG_CAP_T, uPciCfgData);
2934 pCfg = (PVIRTIO_PCI_CAP_T)&pPciDev->abConfig[pCfg->uCapNext];
2935 pCfg->uCfgType = VIRTIO_PCI_CAP_PCI_CFG;
2936 pCfg->uCapVndr = VIRTIO_PCI_CAP_ID_VENDOR;
2937 pCfg->uCapLen = sizeof(VIRTIO_PCI_CFG_CAP_T);
2938 pCfg->uCapNext = (pVirtio->fMsiSupport || pVirtioCC->pbDevSpecificCfg) ? CFG_ADDR_2_IDX(pCfg) + pCfg->uCapLen : 0;
2939 pCfg->uBar = VIRTIO_REGION_PCI_CAP;
2940 pCfg->uOffset = 0;
2941 pCfg->uLength = 4;
2942 cbRegion += pCfg->uLength;
2943 SET_PCI_CAP_LOC(pPciDev, pCfg, pVirtio->LocPciCfgCap, 1);
2944 pVirtioCC->pPciCfgCap = (PVIRTIO_PCI_CFG_CAP_T)pCfg;
2945
2946 if (pVirtioCC->pbDevSpecificCfg)
2947 {
2948 /* Device-specific config capability (VirtIO 1.0, section 4.1.4.6).
2949 *
2950 * Client defines the device-specific config struct and passes size to virtioCoreR3Init()
2951 * to inform this.
2952 */
2953 pCfg = (PVIRTIO_PCI_CAP_T)&pPciDev->abConfig[pCfg->uCapNext];
2954 pCfg->uCfgType = VIRTIO_PCI_CAP_DEVICE_CFG;
2955 pCfg->uCapVndr = VIRTIO_PCI_CAP_ID_VENDOR;
2956 pCfg->uCapLen = sizeof(VIRTIO_PCI_CAP_T);
2957 pCfg->uCapNext = pVirtio->fMsiSupport ? CFG_ADDR_2_IDX(pCfg) + pCfg->uCapLen : 0;
2958 pCfg->uBar = VIRTIO_REGION_PCI_CAP;
2959 pCfg->uOffset = pVirtioCC->pIsrCap->uOffset + pVirtioCC->pIsrCap->uLength;
2960 pCfg->uOffset = RT_ALIGN_32(pCfg->uOffset, 4);
2961 pCfg->uLength = cbDevSpecificCfg;
2962 cbRegion += pCfg->uLength;
2963 SET_PCI_CAP_LOC(pPciDev, pCfg, pVirtio->LocDeviceCap, 4);
2964 pVirtioCC->pDeviceCap = pCfg;
2965 }
2966 else
2967 Assert(pVirtio->LocDeviceCap.cbMmio == 0 && pVirtio->LocDeviceCap.cbPci == 0);
2968
2969 if (pVirtio->fMsiSupport)
2970 {
2971 PDMMSIREG aMsiReg;
2972 RT_ZERO(aMsiReg);
2973 aMsiReg.iMsixCapOffset = pCfg->uCapNext;
2974 aMsiReg.iMsixNextOffset = 0;
2975 aMsiReg.iMsixBar = VIRTIO_REGION_MSIX_CAP;
2976 aMsiReg.cMsixVectors = VBOX_MSIX_MAX_ENTRIES;
2977 rc = PDMDevHlpPCIRegisterMsi(pDevIns, &aMsiReg); /* see MsixR3init() */
2978 if (RT_FAILURE(rc))
2979 {
2980 /* See PDMDevHlp.cpp:pdmR3DevHlp_PCIRegisterMsi */
2981 LogFunc(("Failed to configure MSI-X (%Rrc). Reverting to INTx\n", rc));
2982 pVirtio->fMsiSupport = false;
2983 }
2984 else
2985 Log2Func(("Using MSI-X for guest driver notification\n"));
2986 }
2987 else
2988 LogFunc(("MSI-X not available for VBox, using INTx notification\n"));
2989
2990 /* Set offset to first capability and enable PCI dev capabilities */
2991 PDMPciDevSetCapabilityList(pPciDev, 0x40);
2992 PDMPciDevSetStatus(pPciDev, VBOX_PCI_STATUS_CAP_LIST);
2993
2994 size_t cbSize = RTStrPrintf(pVirtioCC->szMmioName, sizeof(pVirtioCC->szMmioName), "%s (modern)", pcszInstance);
2995 if (cbSize <= 0)
2996 return PDMDEV_SET_ERROR(pDevIns, VERR_BUFFER_OVERFLOW, N_("virtio: out of memory allocating string")); /* can we put params in this error? */
2997
2998 cbSize = RTStrPrintf(pVirtioCC->szPortIoName, sizeof(pVirtioCC->szPortIoName), "%s (legacy)", pcszInstance);
2999 if (cbSize <= 0)
3000 return PDMDEV_SET_ERROR(pDevIns, VERR_BUFFER_OVERFLOW, N_("virtio: out of memory allocating string")); /* can we put params in this error? */
3001
3002 if (pVirtio->fOfferLegacy)
3003 {
3004 /* As a transitional device that supports legacy VirtIO drivers, this VirtIO device generic implementation presents
3005 * legacy driver interface in I/O space at BAR0. The following maps the common (e.g. device independent)
3006 * dev config area as well as device-specific dev config area (whose size is passed to init function of this VirtIO
3007 * generic device code) for access via Port I/O, since legacy drivers (e.g. pre VirtIO 1.0) don't use MMIO callbacks.
3008 * (See VirtIO 1.1, Section 4.1.4.8).
3009 */
3010 rc = PDMDevHlpPCIIORegionCreateIo(pDevIns, VIRTIO_REGION_LEGACY_IO, sizeof(VIRTIO_LEGACY_PCI_COMMON_CFG_T) + cbDevSpecificCfg,
3011 virtioLegacyIOPortOut, virtioLegacyIOPortIn, NULL /*pvUser*/, pVirtioCC->szPortIoName,
3012 NULL /*paExtDescs*/, &pVirtio->hLegacyIoPorts);
3013 AssertLogRelRCReturn(rc, PDMDEV_SET_ERROR(pDevIns, rc, N_("virtio: cannot register legacy config in I/O space at BAR0 */")));
3014 }
3015
3016 /* Note: The Linux driver at drivers/virtio/virtio_pci_modern.c tries to map at least a page for the
3017 * 'unknown' device-specific capability without querying the capability to determine size, so pad w/extra page.
3018 */
3019 rc = PDMDevHlpPCIIORegionCreateMmio(pDevIns, VIRTIO_REGION_PCI_CAP, RT_ALIGN_32(cbRegion + VIRTIO_PAGE_SIZE, VIRTIO_PAGE_SIZE),
3020 PCI_ADDRESS_SPACE_MEM, virtioMmioWrite, virtioMmioRead, pVirtio,
3021 IOMMMIO_FLAGS_READ_PASSTHRU | IOMMMIO_FLAGS_WRITE_PASSTHRU,
3022 pVirtioCC->szMmioName,
3023 &pVirtio->hMmioPciCap);
3024 AssertLogRelRCReturn(rc, PDMDEV_SET_ERROR(pDevIns, rc, N_("virtio: cannot register PCI Capabilities address space")));
3025 return VINF_SUCCESS;
3026}
3027
3028
3029/**
3030 * Initializes the VirtIO device using the VirtIO over MMIO transport mode.
3031 *
3032 * @returns VBox status code.
3033 * @param pDevIns Device instance.
3034 * @param pVirtio Pointer to the shared virtio state. This
3035 * must be the first member in the shared
3036 * device instance data!
3037 * @param pVirtioCC Pointer to the ring-3 virtio state. This
3038 * must be the first member in the ring-3
3039 * device instance data!
3040 * @param pcszInstance Device instance name (format-specifier)
3041 * @param cbDevSpecificCfg Size of virtio_pci_device_cap device-specific struct
3042 * @param GCPhysMmioBase The physical guest address of the start of the MMIO area.
3043 * @param u16Irq The interrupt number to use for the virtio device.
3044 */
3045static int virtioR3MmioTransportInit(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, PVIRTIOCORECC pVirtioCC, const char *pcszInstance,
3046 uint16_t cbDevSpecificCfg, RTGCPHYS GCPhysMmioBase, uint16_t u16Irq)
3047{
3048 pVirtio->uIrqMmio = u16Irq;
3049
3050 size_t cbSize = RTStrPrintf(pVirtioCC->szMmioName, sizeof(pVirtioCC->szMmioName), "%s (modern)", pcszInstance);
3051 if (cbSize <= 0)
3052 return PDMDEV_SET_ERROR(pDevIns, VERR_BUFFER_OVERFLOW, N_("virtio: out of memory allocating string")); /* can we put params in this error? */
3053
3054 /*
3055 * Register and map the MMIO region.
3056 */
3057 int rc = PDMDevHlpMmioCreateAndMap(pDevIns, GCPhysMmioBase, RT_ALIGN_32(cbDevSpecificCfg + VIRTIO_MMIO_SIZE, 512),
3058 virtioMmioTransportWrite, virtioMmioTransportRead,
3059 IOMMMIO_FLAGS_READ_PASSTHRU | IOMMMIO_FLAGS_WRITE_PASSTHRU,
3060 pVirtioCC->szMmioName, &pVirtio->hMmioPciCap);
3061 AssertLogRelRCReturn(rc, PDMDEV_SET_ERROR(pDevIns, rc, N_("virtio: cannot register PCI Capabilities address space")));
3062 return VINF_SUCCESS;
3063}
3064
3065
3066/** API Function: See header file */
3067DECLHIDDEN(int) virtioCoreR3Init(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, PVIRTIOCORECC pVirtioCC, PVIRTIOPCIPARAMS pPciParams,
3068 const char *pcszInstance, uint64_t fDevSpecificFeatures, uint32_t fOfferLegacy,
3069 void *pvDevSpecificCfg, uint16_t cbDevSpecificCfg)
3070{
3071 /*
3072 * Virtio state must be the first member of shared device instance data,
3073 * otherwise can't get our bearings in PCI config callbacks.
3074 */
3075 AssertLogRelReturn(pVirtio == PDMINS_2_DATA(pDevIns, PVIRTIOCORE), VERR_STATE_CHANGED);
3076 AssertLogRelReturn(pVirtioCC == PDMINS_2_DATA_CC(pDevIns, PVIRTIOCORECC), VERR_STATE_CHANGED);
3077
3078 pVirtio->pDevInsR3 = pDevIns;
3079
3080 /*
3081 * Caller must initialize these.
3082 */
3083 AssertReturn(pVirtioCC->pfnStatusChanged, VERR_INVALID_POINTER);
3084 AssertReturn(pVirtioCC->pfnVirtqNotified, VERR_INVALID_POINTER);
3085 AssertReturn(VIRTQ_SIZE > 0 && VIRTQ_SIZE <= 32768, VERR_OUT_OF_RANGE); /* VirtIO specification-defined limit */
3086
3087 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
3088
3089 uint16_t u16Irq = 0;
3090 int rc = pHlp->pfnCFGMQueryU16Def(pDevIns->pCfg, "Irq", &u16Irq, 0);
3091 if (RT_FAILURE(rc))
3092 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Failed to get the \"Irq\" value"));
3093
3094 RTGCPHYS GCPhysMmioBase = 0;
3095 rc = pHlp->pfnCFGMQueryU64Def(pDevIns->pCfg, "MmioBase", &GCPhysMmioBase, NIL_RTGCPHYS);
3096 if (RT_FAILURE(rc))
3097 return PDMDEV_SET_ERROR(pDevIns, rc,
3098 N_("Configuration error: Failed to get the \"MmioBase\" value"));
3099
3100#if 0 /* Until pdmR3DvHlp_PCISetIrq() impl is fixed and Assert that limits vec to 0 is removed
3101 * VBox legacy MSI support has not been implemented yet
3102 */
3103# ifdef VBOX_WITH_MSI_DEVICES
3104 pVirtio->fMsiSupport = true;
3105# endif
3106#endif
3107
3108 /*
3109 * Host features (presented as a buffet for guest to select from)
3110 * include both dev-specific features & reserved dev-independent features (bitmask).
3111 */
3112 pVirtio->uDeviceType = pPciParams->uDeviceType;
3113 pVirtio->uDeviceFeatures = VIRTIO_F_VERSION_1
3114 | VIRTIO_DEV_INDEPENDENT_FEATURES_OFFERED
3115 | fDevSpecificFeatures;
3116
3117 pVirtio->fLegacyDriver = pVirtio->fOfferLegacy = fOfferLegacy;
3118
3119 RTStrCopy(pVirtio->szInstance, sizeof(pVirtio->szInstance), pcszInstance);
3120 pVirtioCC->cbDevSpecificCfg = cbDevSpecificCfg;
3121 pVirtioCC->pbDevSpecificCfg = (uint8_t *)pvDevSpecificCfg;
3122 pVirtioCC->pbPrevDevSpecificCfg = (uint8_t *)RTMemDup(pvDevSpecificCfg, cbDevSpecificCfg);
3123 AssertLogRelReturn(pVirtioCC->pbPrevDevSpecificCfg, VERR_NO_MEMORY);
3124
3125 if (GCPhysMmioBase != NIL_RTGCPHYS)
3126 rc = virtioR3MmioTransportInit(pDevIns, pVirtio, pVirtioCC, pcszInstance, cbDevSpecificCfg,
3127 GCPhysMmioBase, u16Irq);
3128 else
3129 rc = virtioR3PciTransportInit(pDevIns, pVirtio, pVirtioCC, pPciParams, pcszInstance, cbDevSpecificCfg);
3130 AssertLogRelRCReturn(rc, rc);
3131
3132 /*
3133 * Statistics.
3134 */
3135# ifdef VBOX_WITH_STATISTICS
3136 PDMDevHlpSTAMRegisterF(pDevIns, &pVirtio->StatDescChainsAllocated, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT,
3137 "Total number of allocated descriptor chains", "DescChainsAllocated");
3138 PDMDevHlpSTAMRegisterF(pDevIns, &pVirtio->StatDescChainsFreed, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT,
3139 "Total number of freed descriptor chains", "DescChainsFreed");
3140 PDMDevHlpSTAMRegisterF(pDevIns, &pVirtio->StatDescChainsSegsIn, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT,
3141 "Total number of inbound segments", "DescChainsSegsIn");
3142 PDMDevHlpSTAMRegisterF(pDevIns, &pVirtio->StatDescChainsSegsOut, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT,
3143 "Total number of outbound segments", "DescChainsSegsOut");
3144 PDMDevHlpSTAMRegister(pDevIns, &pVirtio->StatReadR3, STAMTYPE_PROFILE, "IO/ReadR3", STAMUNIT_TICKS_PER_CALL, "Profiling IO reads in R3");
3145 PDMDevHlpSTAMRegister(pDevIns, &pVirtio->StatReadR0, STAMTYPE_PROFILE, "IO/ReadR0", STAMUNIT_TICKS_PER_CALL, "Profiling IO reads in R0");
3146 PDMDevHlpSTAMRegister(pDevIns, &pVirtio->StatReadRC, STAMTYPE_PROFILE, "IO/ReadRC", STAMUNIT_TICKS_PER_CALL, "Profiling IO reads in RC");
3147 PDMDevHlpSTAMRegister(pDevIns, &pVirtio->StatWriteR3, STAMTYPE_PROFILE, "IO/WriteR3", STAMUNIT_TICKS_PER_CALL, "Profiling IO writes in R3");
3148 PDMDevHlpSTAMRegister(pDevIns, &pVirtio->StatWriteR0, STAMTYPE_PROFILE, "IO/WriteR0", STAMUNIT_TICKS_PER_CALL, "Profiling IO writes in R0");
3149 PDMDevHlpSTAMRegister(pDevIns, &pVirtio->StatWriteRC, STAMTYPE_PROFILE, "IO/WriteRC", STAMUNIT_TICKS_PER_CALL, "Profiling IO writes in RC");
3150# endif /* VBOX_WITH_STATISTICS */
3151
3152 return VINF_SUCCESS;
3153}
3154
3155#else /* !IN_RING3 */
3156
3157/**
3158 * Sets up the core ring-0/raw-mode virtio bits.
3159 *
3160 * @returns VBox status code.
3161 * @param pDevIns The device instance.
3162 * @param pVirtio Pointer to the shared virtio state. This must be the first
3163 * member in the shared device instance data!
3164 */
3165DECLHIDDEN(int) virtioCoreRZInit(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio)
3166{
3167 AssertLogRelReturn(pVirtio == PDMINS_2_DATA(pDevIns, PVIRTIOCORE), VERR_STATE_CHANGED);
3168 int rc;
3169#ifdef FUTURE_OPTIMIZATION
3170 rc = PDMDevHlpSetDeviceCritSect(pDevIns, PDMDevHlpCritSectGetNop(pDevIns));
3171 AssertRCReturn(rc, rc);
3172#endif
3173
3174 if (pVirtio->uIrqMmio != 0)
3175 {
3176 rc = PDMDevHlpMmioSetUpContext(pDevIns, pVirtio->hMmioPciCap, virtioMmioTransportWrite, virtioMmioTransportRead, pVirtio);
3177 AssertRCReturn(rc, rc);
3178 }
3179 else
3180 {
3181 rc = PDMDevHlpMmioSetUpContext(pDevIns, pVirtio->hMmioPciCap, virtioMmioWrite, virtioMmioRead, pVirtio);
3182 AssertRCReturn(rc, rc);
3183
3184 if (pVirtio->fOfferLegacy)
3185 {
3186 rc = PDMDevHlpIoPortSetUpContext(pDevIns, pVirtio->hLegacyIoPorts, virtioLegacyIOPortOut, virtioLegacyIOPortIn, NULL /*pvUser*/);
3187 AssertRCReturn(rc, rc);
3188 }
3189 }
3190 return rc;
3191}
3192
3193#endif /* !IN_RING3 */
3194
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