VirtualBox

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

Last change on this file since 98063 was 98063, checked in by vboxsync, 23 months ago

Devices/Virtio,Devices/DevVirtioNet_1_0: Additional fixes for the virtio-net device implementation, don't reset the shadow index counters in the virtq structure when it is being attached because the guest might have used the queues before setting the DRIVER_OK flag (FreeBSD's legacy virtio-net driver included up to version 12.3 and pfSense 2.6.0). Also add a device reset callback to properly reset the device state, avoiding the RX code to work on stale data overwriting random guest memory. Some style fixes, ticketref:21201

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 116.5 KB
Line 
1/* $Id: VirtioCore.cpp 98063 2023-01-12 15:01:04Z vboxsync $ */
2
3/** @file
4 * VirtioCore - Virtio Core (PCI, feature & config mgt, queue mgt & proxy, notification mgt)
5 */
6
7/*
8 * Copyright (C) 2009-2022 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
139const 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 */
338uint16_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
358void 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*/
394void 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 */
405void 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 */
444void 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 */
488void 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 */
580int virtioCoreIsLegacyMode(PVIRTIOCORE pVirtio)
581{
582 return pVirtio->fLegacyDriver;
583}
584
585#ifdef IN_RING3
586
587int 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
598int 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
610bool virtioCoreR3VirtqIsAttached(PVIRTIOCORE pVirtio, uint16_t uVirtqNbr)
611{
612 return pVirtio->aVirtqueues[uVirtqNbr].fAttached;
613}
614
615bool virtioCoreR3VirtqIsEnabled(PVIRTIOCORE pVirtio, uint16_t uVirtqNbr)
616{
617 PVIRTQUEUE pVirtq = &pVirtio->aVirtqueues[uVirtqNbr];
618 return (bool)pVirtq->uEnable && pVirtq->GCPhysVirtqDesc;
619}
620
621/** API Fuunction: See header file */
622void virtioCoreR3VirtqInfo(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs, int uVirtq)
623{
624 RT_NOREF(pszArgs);
625 PVIRTIOCORE pVirtio = PDMDEVINS_2_DATA(pDevIns, PVIRTIOCORE);
626 PVIRTQUEUE pVirtq = &pVirtio->aVirtqueues[uVirtq];
627
628 /** @todo add ability to dump physical contents described by any descriptor (using existing VirtIO core API function) */
629// bool fDump = pszArgs && (*pszArgs == 'd' || *pszArgs == 'D'); /* "dump" (avail phys descriptor)"
630
631 uint16_t uAvailIdx = virtioReadAvailRingIdx(pDevIns, pVirtio, pVirtq);
632 uint16_t uAvailIdxShadow = pVirtq->uAvailIdxShadow;
633
634 uint16_t uUsedIdx = virtioReadUsedRingIdx(pDevIns, pVirtio, pVirtq);
635 uint16_t uUsedIdxShadow = pVirtq->uUsedIdxShadow;
636
637#ifdef VIRTIO_VBUF_ON_STACK
638 VIRTQBUF_T VirtqBuf;
639 PVIRTQBUF pVirtqBuf = &VirtqBuf;
640#else /* !VIRTIO_VBUF_ON_STACK */
641 PVIRTQBUF pVirtqBuf = NULL;
642#endif /* !VIRTIO_VBUF_ON_STACK */
643
644 bool fEmpty = IS_VIRTQ_EMPTY(pDevIns, pVirtio, pVirtq);
645
646 LogFunc(("%s, empty = %s\n", pVirtq->szName, fEmpty ? "true" : "false"));
647
648 int cSendSegs = 0, cReturnSegs = 0;
649 if (!fEmpty)
650 {
651#ifdef VIRTIO_VBUF_ON_STACK
652 virtioCoreR3VirtqAvailBufPeek(pDevIns, pVirtio, uVirtq, pVirtqBuf);
653#else /* !VIRTIO_VBUF_ON_STACK */
654 virtioCoreR3VirtqAvailBufPeek(pDevIns, pVirtio, uVirtq, &pVirtqBuf);
655#endif /* !VIRTIO_VBUF_ON_STACK */
656 cSendSegs = pVirtqBuf->pSgPhysSend ? pVirtqBuf->pSgPhysSend->cSegs : 0;
657 cReturnSegs = pVirtqBuf->pSgPhysReturn ? pVirtqBuf->pSgPhysReturn->cSegs : 0;
658 }
659
660 bool fAvailNoInterrupt = virtioReadAvailRingFlags(pDevIns, pVirtio, pVirtq) & VIRTQ_AVAIL_F_NO_INTERRUPT;
661 bool fUsedNoNotify = virtioReadUsedRingFlags(pDevIns, pVirtio, pVirtq) & VIRTQ_USED_F_NO_NOTIFY;
662
663 pHlp->pfnPrintf(pHlp, " queue enabled: ........... %s\n", pVirtq->uEnable ? "true" : "false");
664 pHlp->pfnPrintf(pHlp, " size: .................... %d\n", pVirtq->uQueueSize);
665 pHlp->pfnPrintf(pHlp, " notify offset: ........... %d\n", pVirtq->uNotifyOffset);
666 if (pVirtio->fMsiSupport)
667 pHlp->pfnPrintf(pHlp, " MSIX vector: ....... %4.4x\n", pVirtq->uMsixVector);
668 pHlp->pfnPrintf(pHlp, "\n");
669 pHlp->pfnPrintf(pHlp, " avail ring (%d entries):\n", uAvailIdx - uAvailIdxShadow);
670 pHlp->pfnPrintf(pHlp, " index: ................ %d\n", uAvailIdx);
671 pHlp->pfnPrintf(pHlp, " shadow: ............... %d\n", uAvailIdxShadow);
672 pHlp->pfnPrintf(pHlp, " flags: ................ %s\n", fAvailNoInterrupt ? "NO_INTERRUPT" : "");
673 pHlp->pfnPrintf(pHlp, "\n");
674 pHlp->pfnPrintf(pHlp, " used ring (%d entries):\n", uUsedIdx - uUsedIdxShadow);
675 pHlp->pfnPrintf(pHlp, " index: ................ %d\n", uUsedIdx);
676 pHlp->pfnPrintf(pHlp, " shadow: ............... %d\n", uUsedIdxShadow);
677 pHlp->pfnPrintf(pHlp, " flags: ................ %s\n", fUsedNoNotify ? "NO_NOTIFY" : "");
678 pHlp->pfnPrintf(pHlp, "\n");
679 if (!fEmpty)
680 {
681 pHlp->pfnPrintf(pHlp, " desc chain:\n");
682 pHlp->pfnPrintf(pHlp, " head idx: ............. %d\n", uUsedIdx);
683 pHlp->pfnPrintf(pHlp, " segs: ................. %d\n", cSendSegs + cReturnSegs);
684 pHlp->pfnPrintf(pHlp, " refCnt ................ %d\n", pVirtqBuf->cRefs);
685 pHlp->pfnPrintf(pHlp, "\n");
686 pHlp->pfnPrintf(pHlp, " host-to-guest (%d bytes):\n", pVirtqBuf->cbPhysSend);
687 pHlp->pfnPrintf(pHlp, " segs: .............. %d\n", cSendSegs);
688 if (cSendSegs)
689 {
690 pHlp->pfnPrintf(pHlp, " index: ............. %d\n", pVirtqBuf->pSgPhysSend->idxSeg);
691 pHlp->pfnPrintf(pHlp, " unsent ............. %d\n", pVirtqBuf->pSgPhysSend->cbSegLeft);
692 }
693 pHlp->pfnPrintf(pHlp, "\n");
694 pHlp->pfnPrintf(pHlp, " guest-to-host (%d bytes)\n", pVirtqBuf->cbPhysReturn);
695 pHlp->pfnPrintf(pHlp, " segs: .............. %d\n", cReturnSegs);
696 if (cReturnSegs)
697 {
698 pHlp->pfnPrintf(pHlp, " index: ............. %d\n", pVirtqBuf->pSgPhysReturn->idxSeg);
699 pHlp->pfnPrintf(pHlp, " unsent ............. %d\n", pVirtqBuf->pSgPhysReturn->cbSegLeft);
700 }
701 } else
702 pHlp->pfnPrintf(pHlp, " No desc chains available\n");
703 pHlp->pfnPrintf(pHlp, "\n");
704}
705
706#ifdef VIRTIO_VBUF_ON_STACK
707/** API Function: See header file */
708PVIRTQBUF virtioCoreR3VirtqBufAlloc(void)
709{
710 PVIRTQBUF pVirtqBuf = (PVIRTQBUF)RTMemAllocZ(sizeof(VIRTQBUF_T));
711 AssertReturn(pVirtqBuf, NULL);
712 pVirtqBuf->u32Magic = VIRTQBUF_MAGIC;
713 pVirtqBuf->cRefs = 1;
714 return pVirtqBuf;
715}
716#endif /* VIRTIO_VBUF_ON_STACK */
717
718/** API Function: See header file */
719uint32_t virtioCoreR3VirtqBufRetain(PVIRTQBUF pVirtqBuf)
720{
721 AssertReturn(pVirtqBuf, UINT32_MAX);
722 AssertReturn(pVirtqBuf->u32Magic == VIRTQBUF_MAGIC, UINT32_MAX);
723 uint32_t cRefs = ASMAtomicIncU32(&pVirtqBuf->cRefs);
724 Assert(cRefs > 1);
725 Assert(cRefs < 16);
726 return cRefs;
727}
728
729/** API Function: See header file */
730uint32_t virtioCoreR3VirtqBufRelease(PVIRTIOCORE pVirtio, PVIRTQBUF pVirtqBuf)
731{
732 if (!pVirtqBuf)
733 return 0;
734 AssertReturn(pVirtqBuf, 0);
735 AssertReturn(pVirtqBuf->u32Magic == VIRTQBUF_MAGIC, 0);
736 uint32_t cRefs = ASMAtomicDecU32(&pVirtqBuf->cRefs);
737 Assert(cRefs < 16);
738 if (cRefs == 0)
739 {
740 pVirtqBuf->u32Magic = ~VIRTQBUF_MAGIC;
741 RTMemFree(pVirtqBuf);
742#ifdef VBOX_WITH_STATISTICS
743 STAM_REL_COUNTER_INC(&pVirtio->StatDescChainsFreed);
744#endif
745 }
746 RT_NOREF(pVirtio);
747 return cRefs;
748}
749
750/** API Function: See header file */
751void virtioCoreNotifyConfigChanged(PVIRTIOCORE pVirtio)
752{
753 virtioNudgeGuest(pVirtio->pDevInsR3, pVirtio, VIRTIO_ISR_DEVICE_CONFIG, pVirtio->uMsixConfig);
754}
755
756
757/** API Function: See header file */
758void virtioCoreVirtqEnableNotify(PVIRTIOCORE pVirtio, uint16_t uVirtq, bool fEnable)
759{
760 Assert(uVirtq < RT_ELEMENTS(pVirtio->aVirtqueues));
761 PVIRTQUEUE pVirtq = &pVirtio->aVirtqueues[uVirtq];
762
763 if (IS_DRIVER_OK(pVirtio))
764 {
765 uint16_t fFlags = virtioReadUsedRingFlags(pVirtio->pDevInsR3, pVirtio, pVirtq);
766
767 if (fEnable)
768 fFlags &= ~VIRTQ_USED_F_NO_NOTIFY;
769 else
770 fFlags |= VIRTQ_USED_F_NO_NOTIFY;
771
772 virtioWriteUsedRingFlags(pVirtio->pDevInsR3, pVirtio, pVirtq, fFlags);
773 }
774}
775
776/** API function: See Header file */
777void virtioCoreResetAll(PVIRTIOCORE pVirtio)
778{
779 LogFunc(("\n"));
780 pVirtio->fDeviceStatus |= VIRTIO_STATUS_DEVICE_NEEDS_RESET;
781 if (IS_DRIVER_OK(pVirtio))
782 {
783 if (!pVirtio->fLegacyDriver)
784 pVirtio->fGenUpdatePending = true;
785 virtioNudgeGuest(pVirtio->pDevInsR3, pVirtio, VIRTIO_ISR_DEVICE_CONFIG, pVirtio->uMsixConfig);
786 }
787}
788
789/** API function: See Header file */
790#ifdef VIRTIO_VBUF_ON_STACK
791int virtioCoreR3VirtqAvailBufPeek(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t uVirtq, PVIRTQBUF pVirtqBuf)
792{
793 return virtioCoreR3VirtqAvailBufGet(pDevIns, pVirtio, uVirtq, pVirtqBuf, false);
794}
795#else /* !VIRTIO_VBUF_ON_STACK */
796int virtioCoreR3VirtqAvailBufPeek(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t uVirtq,
797 PPVIRTQBUF ppVirtqBuf)
798{
799 return virtioCoreR3VirtqAvailBufGet(pDevIns, pVirtio, uVirtq, ppVirtqBuf, false);
800}
801#endif /* !VIRTIO_VBUF_ON_STACK */
802
803/** API function: See Header file */
804int virtioCoreR3VirtqAvailBufNext(PVIRTIOCORE pVirtio, uint16_t uVirtq)
805{
806 Assert(uVirtq < RT_ELEMENTS(pVirtio->aVirtqueues));
807 PVIRTQUEUE pVirtq = &pVirtio->aVirtqueues[uVirtq];
808
809 if (!pVirtio->fLegacyDriver)
810 AssertMsgReturn((pVirtio->fDeviceStatus & VIRTIO_STATUS_DRIVER_OK) && pVirtq->uEnable,
811 ("Guest driver not in ready state.\n"), VERR_INVALID_STATE);
812
813 if (IS_VIRTQ_EMPTY(pVirtio->pDevInsR3, pVirtio, pVirtq))
814 return VERR_NOT_AVAILABLE;
815
816 Log6Func(("%s avail shadow idx: %u\n", pVirtq->szName, pVirtq->uAvailIdxShadow));
817 pVirtq->uAvailIdxShadow++;
818
819 return VINF_SUCCESS;
820}
821
822/** API Function: See header file */
823#ifdef VIRTIO_VBUF_ON_STACK
824int virtioCoreR3VirtqAvailBufGet(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t uVirtq,
825 uint16_t uHeadIdx, PVIRTQBUF pVirtqBuf)
826#else /* !VIRTIO_VBUF_ON_STACK */
827int virtioCoreR3VirtqAvailBufGet(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t uVirtq,
828 uint16_t uHeadIdx, PPVIRTQBUF ppVirtqBuf)
829#endif /* !VIRTIO_VBUF_ON_STACK */
830{
831#ifndef VIRTIO_VBUF_ON_STACK
832 AssertReturn(ppVirtqBuf, VERR_INVALID_POINTER);
833 *ppVirtqBuf = NULL;
834#endif /* !VIRTIO_VBUF_ON_STACK */
835
836 AssertMsgReturn(uVirtq < RT_ELEMENTS(pVirtio->aVirtqueues),
837 ("uVirtq out of range"), VERR_INVALID_PARAMETER);
838
839 PVIRTQUEUE pVirtq = &pVirtio->aVirtqueues[uVirtq];
840
841 if (!pVirtio->fLegacyDriver)
842 AssertMsgReturn((pVirtio->fDeviceStatus & VIRTIO_STATUS_DRIVER_OK) && pVirtq->uEnable,
843 ("Guest driver not in ready state.\n"), VERR_INVALID_STATE);
844
845 uint16_t uDescIdx = uHeadIdx;
846
847 Log6Func(("%s DESC CHAIN: (head idx = %u)\n", pVirtio->aVirtqueues[uVirtq].szName, uHeadIdx));
848
849 /*
850 * Allocate and initialize the descriptor chain structure.
851 */
852#ifndef VIRTIO_VBUF_ON_STACK
853 PVIRTQBUF pVirtqBuf = (PVIRTQBUF)RTMemAllocZ(sizeof(VIRTQBUF_T));
854 AssertReturn(pVirtqBuf, VERR_NO_MEMORY);
855#endif /* !VIRTIO_VBUF_ON_STACK */
856 pVirtqBuf->u32Magic = VIRTQBUF_MAGIC;
857 pVirtqBuf->cRefs = 1;
858 pVirtqBuf->uHeadIdx = uHeadIdx;
859 pVirtqBuf->uVirtq = uVirtq;
860#ifndef VIRTIO_VBUF_ON_STACK
861 *ppVirtqBuf = pVirtqBuf;
862#endif /* !VIRTIO_VBUF_ON_STACK */
863
864 /*
865 * Gather segments.
866 */
867 VIRTQ_DESC_T desc;
868
869 uint32_t cbIn = 0;
870 uint32_t cbOut = 0;
871 uint32_t cSegsIn = 0;
872 uint32_t cSegsOut = 0;
873
874 PVIRTIOSGSEG paSegsIn = pVirtqBuf->aSegsIn;
875 PVIRTIOSGSEG paSegsOut = pVirtqBuf->aSegsOut;
876
877 do
878 {
879 PVIRTIOSGSEG pSeg;
880 /*
881 * Malicious guests may go beyond paSegsIn or paSegsOut boundaries by linking
882 * several descriptors into a loop. Since there is no legitimate way to get a sequences of
883 * linked descriptors exceeding the total number of descriptors in the ring (see @bugref{8620}),
884 * the following aborts I/O if breach and employs a simple log throttling algorithm to notify.
885 */
886 if (cSegsIn + cSegsOut >= pVirtq->uQueueSize)
887 {
888 static volatile uint32_t s_cMessages = 0;
889 static volatile uint32_t s_cThreshold = 1;
890 if (ASMAtomicIncU32(&s_cMessages) == ASMAtomicReadU32(&s_cThreshold))
891 {
892 LogRelMax(64, ("Too many linked descriptors; check if the guest arranges descriptors in a loop.\n"));
893 if (ASMAtomicReadU32(&s_cMessages) != 1)
894 LogRelMax(64, ("(the above error has occured %u times so far)\n", ASMAtomicReadU32(&s_cMessages)));
895 ASMAtomicWriteU32(&s_cThreshold, ASMAtomicReadU32(&s_cThreshold) * 10);
896 }
897 break;
898 }
899 RT_UNTRUSTED_VALIDATED_FENCE();
900
901 virtioReadDesc(pDevIns, pVirtio, pVirtq, uDescIdx, &desc);
902
903 if (desc.fFlags & VIRTQ_DESC_F_WRITE)
904 {
905 Log6Func(("%s IN idx=%-4u seg=%-3u addr=%RGp cb=%u\n", pVirtq->szName, uDescIdx, cSegsIn, desc.GCPhysBuf, desc.cb));
906 cbIn += desc.cb;
907 pSeg = &paSegsIn[cSegsIn++];
908 }
909 else
910 {
911 Log6Func(("%s OUT desc_idx=%-4u seg=%-3u addr=%RGp cb=%u\n", pVirtq->szName, uDescIdx, cSegsOut, desc.GCPhysBuf, desc.cb));
912 cbOut += desc.cb;
913 pSeg = &paSegsOut[cSegsOut++];
914#ifdef DEEP_DEBUG
915 if (LogIs11Enabled())
916 {
917 virtioCoreGCPhysHexDump(pDevIns, desc.GCPhysBuf, desc.cb, 0, NULL);
918 Log(("\n"));
919 }
920#endif
921 }
922 pSeg->GCPhys = desc.GCPhysBuf;
923 pSeg->cbSeg = desc.cb;
924 uDescIdx = desc.uDescIdxNext;
925 } while (desc.fFlags & VIRTQ_DESC_F_NEXT);
926
927 /*
928 * Add segments to the descriptor chain structure.
929 */
930 if (cSegsIn)
931 {
932 virtioCoreGCPhysChainInit(&pVirtqBuf->SgBufIn, paSegsIn, cSegsIn);
933 pVirtqBuf->pSgPhysReturn = &pVirtqBuf->SgBufIn;
934 pVirtqBuf->cbPhysReturn = cbIn;
935#ifdef VBOX_WITH_STATISTICS
936 STAM_REL_COUNTER_ADD(&pVirtio->StatDescChainsSegsIn, cSegsIn);
937#endif
938 }
939
940 if (cSegsOut)
941 {
942 virtioCoreGCPhysChainInit(&pVirtqBuf->SgBufOut, paSegsOut, cSegsOut);
943 pVirtqBuf->pSgPhysSend = &pVirtqBuf->SgBufOut;
944 pVirtqBuf->cbPhysSend = cbOut;
945#ifdef VBOX_WITH_STATISTICS
946 STAM_REL_COUNTER_ADD(&pVirtio->StatDescChainsSegsOut, cSegsOut);
947#endif
948 }
949
950#ifdef VBOX_WITH_STATISTICS
951 STAM_REL_COUNTER_INC(&pVirtio->StatDescChainsAllocated);
952#endif
953 Log6Func(("%s -- segs OUT: %u (%u bytes) IN: %u (%u bytes) --\n",
954 pVirtq->szName, cSegsOut, cbOut, cSegsIn, cbIn));
955
956 return VINF_SUCCESS;
957}
958
959/** API function: See Header file */
960#ifdef VIRTIO_VBUF_ON_STACK
961int virtioCoreR3VirtqAvailBufGet(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t uVirtq,
962 PVIRTQBUF pVirtqBuf, bool fRemove)
963#else /* !VIRTIO_VBUF_ON_STACK */
964int virtioCoreR3VirtqAvailBufGet(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t uVirtq,
965 PPVIRTQBUF ppVirtqBuf, bool fRemove)
966#endif /* !VIRTIO_VBUF_ON_STACK */
967{
968 Assert(uVirtq < RT_ELEMENTS(pVirtio->aVirtqueues));
969 PVIRTQUEUE pVirtq = &pVirtio->aVirtqueues[uVirtq];
970
971 if (IS_VIRTQ_EMPTY(pDevIns, pVirtio, pVirtq))
972 return VERR_NOT_AVAILABLE;
973
974 uint16_t uHeadIdx = virtioReadAvailDescIdx(pDevIns, pVirtio, pVirtq, pVirtq->uAvailIdxShadow);
975
976 if (pVirtio->uDriverFeatures & VIRTIO_F_EVENT_IDX)
977 virtioWriteUsedAvailEvent(pDevIns,pVirtio, pVirtq, pVirtq->uAvailIdxShadow + 1);
978
979 if (fRemove)
980 pVirtq->uAvailIdxShadow++;
981
982#ifdef VIRTIO_VBUF_ON_STACK
983 int rc = virtioCoreR3VirtqAvailBufGet(pDevIns, pVirtio, uVirtq, uHeadIdx, pVirtqBuf);
984#else /* !VIRTIO_VBUF_ON_STACK */
985 int rc = virtioCoreR3VirtqAvailBufGet(pDevIns, pVirtio, uVirtq, uHeadIdx, ppVirtqBuf);
986#endif /* !VIRTIO_VBUF_ON_STACK */
987 return rc;
988}
989
990/** API function: See Header file */
991int virtioCoreR3VirtqUsedBufPut(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t uVirtq, PRTSGBUF pSgVirtReturn,
992 PVIRTQBUF pVirtqBuf, bool fFence)
993{
994 Assert(uVirtq < RT_ELEMENTS(pVirtio->aVirtqueues));
995 PVIRTQUEUE pVirtq = &pVirtio->aVirtqueues[uVirtq];
996
997 PVIRTIOSGBUF pSgPhysReturn = pVirtqBuf->pSgPhysReturn;
998
999 Assert(pVirtqBuf->u32Magic == VIRTQBUF_MAGIC);
1000 Assert(pVirtqBuf->cRefs > 0);
1001
1002 /*
1003 * Workaround for a bug in FreeBSD's virtio-net driver up until 12.3 which supports only the legacy style devive.
1004 * When the device is re-initialized from the driver it violates the spec and posts commands to the control queue
1005 * before setting the DRIVER_OK flag, breaking the following check and rendering the device non-functional.
1006 * The queues are properly set up at this stage however so no real harm is done and we can safely continue here,
1007 * for the legacy device only of course after making sure the queue is properly set up.
1008 */
1009 AssertMsgReturn( IS_DRIVER_OK(pVirtio)
1010 || ( pVirtio->fLegacyDriver
1011 && pVirtq->GCPhysVirtqDesc),
1012 ("Guest driver not in ready state.\n"), VERR_INVALID_STATE);
1013
1014 Log6Func((" Copying device data to %s, [desc:%u -> used ring:%u]\n",
1015 VIRTQNAME(pVirtio, uVirtq), pVirtqBuf->uHeadIdx, pVirtq->uUsedIdxShadow));
1016
1017 /* Copy s/g buf (virtual memory) to guest phys mem (VirtIO "IN" direction). */
1018
1019 size_t cbCopy = 0, cbTotal = 0, cbRemain = 0;
1020
1021 if (pSgVirtReturn)
1022 {
1023 size_t cbTarget = virtioCoreGCPhysChainCalcBufSize(pSgPhysReturn);
1024 cbRemain = cbTotal = RTSgBufCalcTotalLength(pSgVirtReturn);
1025 AssertMsgReturn(cbTarget >= cbRemain, ("No space to write data to phys memory"), VERR_BUFFER_OVERFLOW);
1026 virtioCoreGCPhysChainReset(pSgPhysReturn);
1027 while (cbRemain)
1028 {
1029 cbCopy = RT_MIN(pSgVirtReturn->cbSegLeft, pSgPhysReturn->cbSegLeft);
1030 Assert(cbCopy > 0);
1031 virtioCoreGCPhysWrite(pVirtio, pDevIns, (RTGCPHYS)pSgPhysReturn->GCPhysCur, pSgVirtReturn->pvSegCur, cbCopy);
1032 RTSgBufAdvance(pSgVirtReturn, cbCopy);
1033 virtioCoreGCPhysChainAdvance(pSgPhysReturn, cbCopy);
1034 cbRemain -= cbCopy;
1035 }
1036
1037 if (fFence)
1038 RT_UNTRUSTED_NONVOLATILE_COPY_FENCE(); /* needed? */
1039
1040 Assert(!(cbCopy >> 32));
1041 }
1042
1043 /* Flag if write-ahead crosses threshold where guest driver indicated it wants event notification */
1044 if (pVirtio->uDriverFeatures & VIRTIO_F_EVENT_IDX)
1045 if (pVirtq->uUsedIdxShadow == virtioReadAvailUsedEvent(pDevIns, pVirtio, pVirtq))
1046 pVirtq->fUsedRingEvent = true;
1047
1048 /*
1049 * Place used buffer's descriptor in used ring but don't update used ring's slot index.
1050 * That will be done with a subsequent client call to virtioCoreVirtqUsedRingSync()
1051 */
1052 virtioWriteUsedElem(pDevIns, pVirtio, pVirtq, pVirtq->uUsedIdxShadow++, pVirtqBuf->uHeadIdx, (uint32_t)cbTotal);
1053
1054#ifdef LOG_ENABLED
1055 if (LogIs6Enabled() && pSgVirtReturn)
1056 {
1057
1058 LogFunc((" ... %d segs, %zu bytes, copied to %u byte buf@offset=%u. Residual: %zu bytes\n",
1059 pSgVirtReturn->cSegs, cbTotal - cbRemain, pVirtqBuf->cbPhysReturn,
1060 ((virtioCoreGCPhysChainCalcBufSize(pVirtqBuf->pSgPhysReturn) -
1061 virtioCoreGCPhysChainCalcLengthLeft(pVirtqBuf->pSgPhysReturn)) - (cbTotal - cbRemain)),
1062 virtioCoreGCPhysChainCalcLengthLeft(pVirtqBuf->pSgPhysReturn) ));
1063
1064 uint16_t uPending = virtioCoreR3CountPendingBufs(
1065 virtioReadUsedRingIdx(pDevIns, pVirtio, pVirtq),
1066 pVirtq->uUsedIdxShadow, pVirtq->uQueueSize);
1067
1068 LogFunc((" %u used buf%s not synced in %s\n", uPending, uPending == 1 ? "" : "s ",
1069 VIRTQNAME(pVirtio, uVirtq)));
1070 }
1071#endif
1072 return VINF_SUCCESS;
1073}
1074
1075/** API function: See Header file */
1076int virtioCoreR3VirtqUsedBufPut(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t uVirtq,
1077 size_t cb, void const *pv, PVIRTQBUF pVirtqBuf, size_t cbEnqueue, bool fFence)
1078{
1079 Assert(uVirtq < RT_ELEMENTS(pVirtio->aVirtqueues));
1080 Assert(pv);
1081
1082 PVIRTQUEUE pVirtq = &pVirtio->aVirtqueues[uVirtq];
1083 PVIRTIOSGBUF pSgPhysReturn = pVirtqBuf->pSgPhysReturn;
1084
1085 Assert(pVirtqBuf->u32Magic == VIRTQBUF_MAGIC);
1086 Assert(pVirtqBuf->cRefs > 0);
1087
1088 AssertMsgReturn(IS_DRIVER_OK(pVirtio), ("Guest driver not in ready state.\n"), VERR_INVALID_STATE);
1089
1090 Log6Func((" Copying device data to %s, [desc chain head idx:%u]\n",
1091 VIRTQNAME(pVirtio, uVirtq), pVirtqBuf->uHeadIdx));
1092 /*
1093 * Convert virtual memory simple buffer to guest physical memory (VirtIO descriptor chain)
1094 */
1095 uint8_t *pvBuf = (uint8_t *)pv;
1096 size_t cbRemain = cb, cbCopy = 0;
1097 while (cbRemain)
1098 {
1099 cbCopy = RT_MIN(pSgPhysReturn->cbSegLeft, cbRemain);
1100 Assert(cbCopy > 0);
1101 virtioCoreGCPhysWrite(pVirtio, pDevIns, (RTGCPHYS)pSgPhysReturn->GCPhysCur, pvBuf, cbCopy);
1102 virtioCoreGCPhysChainAdvance(pSgPhysReturn, cbCopy);
1103 pvBuf += cbCopy;
1104 cbRemain -= cbCopy;
1105 }
1106 LogFunc((" ...%zu bytes, copied to %u byte buf@offset=%u. Residual: %zu bytes\n",
1107 cb , pVirtqBuf->cbPhysReturn,
1108 ((virtioCoreGCPhysChainCalcBufSize(pVirtqBuf->pSgPhysReturn) -
1109 virtioCoreGCPhysChainCalcLengthLeft(pVirtqBuf->pSgPhysReturn)) - cb),
1110 virtioCoreGCPhysChainCalcLengthLeft(pVirtqBuf->pSgPhysReturn)));
1111
1112 if (cbEnqueue)
1113 {
1114 if (fFence)
1115 {
1116 RT_UNTRUSTED_NONVOLATILE_COPY_FENCE(); /* needed? */
1117 Assert(!(cbCopy >> 32));
1118 }
1119 /* Flag if write-ahead crosses threshold where guest driver indicated it wants event notification */
1120 if (pVirtio->uDriverFeatures & VIRTIO_F_EVENT_IDX)
1121 if (pVirtq->uUsedIdxShadow == virtioReadAvailUsedEvent(pDevIns, pVirtio, pVirtq))
1122 pVirtq->fUsedRingEvent = true;
1123 /*
1124 * Place used buffer's descriptor in used ring but don't update used ring's slot index.
1125 * That will be done with a subsequent client call to virtioCoreVirtqUsedRingSync()
1126 */
1127 Log6Func((" Enqueue desc chain head idx %u to %s used ring @ %u\n", pVirtqBuf->uHeadIdx,
1128 VIRTQNAME(pVirtio, uVirtq), pVirtq->uUsedIdxShadow));
1129
1130 virtioWriteUsedElem(pDevIns, pVirtio, pVirtq, pVirtq->uUsedIdxShadow++, pVirtqBuf->uHeadIdx, (uint32_t)cbEnqueue);
1131
1132#ifdef LOG_ENABLED
1133 if (LogIs6Enabled())
1134 {
1135 uint16_t uPending = virtioCoreR3CountPendingBufs(
1136 virtioReadUsedRingIdx(pDevIns, pVirtio, pVirtq),
1137 pVirtq->uUsedIdxShadow, pVirtq->uQueueSize);
1138
1139 LogFunc((" %u used buf%s not synced in %s\n",
1140 uPending, uPending == 1 ? "" : "s ", VIRTQNAME(pVirtio, uVirtq)));
1141 }
1142#endif
1143 } /* fEnqueue */
1144
1145 return VINF_SUCCESS;
1146}
1147
1148
1149#endif /* IN_RING3 */
1150
1151/** API function: See Header file */
1152int virtioCoreVirtqUsedRingSync(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t uVirtq)
1153{
1154 Assert(uVirtq < RT_ELEMENTS(pVirtio->aVirtqueues));
1155 PVIRTQUEUE pVirtq = &pVirtio->aVirtqueues[uVirtq];
1156
1157 if (!pVirtio->fLegacyDriver)
1158 AssertMsgReturn((pVirtio->fDeviceStatus & VIRTIO_STATUS_DRIVER_OK) && pVirtq->uEnable,
1159 ("Guest driver not in ready state.\n"), VERR_INVALID_STATE);
1160
1161 Log6Func((" Sync %s used ring (%u -> idx)\n",
1162 pVirtq->szName, pVirtq->uUsedIdxShadow));
1163
1164 virtioWriteUsedRingIdx(pDevIns, pVirtio, pVirtq, pVirtq->uUsedIdxShadow);
1165 virtioCoreNotifyGuestDriver(pDevIns, pVirtio, uVirtq);
1166
1167 return VINF_SUCCESS;
1168}
1169
1170/**
1171 * This is called from the MMIO callback code when the guest does an MMIO access to the
1172 * mapped queue notification capability area corresponding to a particular queue, to notify
1173 * the queue handler of available data in the avail ring of the queue (VirtIO 1.0, 4.1.4.4.1)
1174 *
1175 * @param pDevIns The device instance.
1176 * @param pVirtio Pointer to the shared virtio state.
1177 * @param uVirtq Virtq to check for guest interrupt handling preference
1178 * @param uNotifyIdx Notification index
1179 */
1180static void virtioCoreVirtqNotified(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t uVirtq, uint16_t uNotifyIdx)
1181{
1182 PVIRTIOCORECC pVirtioCC = PDMINS_2_DATA_CC(pDevIns, PVIRTIOCORECC);
1183
1184 /* VirtIO 1.0, section 4.1.5.2 implies uVirtq and uNotifyIdx should match. Disregarding any of
1185 * these notifications (if those indicies disagree) may break device/driver synchronization,
1186 * causing eternal throughput starvation, yet there's no specified way to disambiguate
1187 * which queue to wake-up in any awkward situation where the two parameters differ.
1188 */
1189 AssertMsg(uNotifyIdx == uVirtq,
1190 ("Guest kicked virtq %d's notify addr w/non-corresponding virtq idx %d\n",
1191 uVirtq, uNotifyIdx));
1192 RT_NOREF(uNotifyIdx);
1193
1194 AssertReturnVoid(uVirtq < RT_ELEMENTS(pVirtio->aVirtqueues));
1195 PVIRTQUEUE pVirtq = &pVirtio->aVirtqueues[uVirtq];
1196
1197 Log6Func(("%s: (desc chains: %u)\n", *pVirtq->szName ? pVirtq->szName : "?UNAMED QUEUE?",
1198 virtioCoreVirtqAvailCnt(pDevIns, pVirtio, pVirtq)));
1199
1200 /* Inform client */
1201 pVirtioCC->pfnVirtqNotified(pDevIns, pVirtio, uVirtq);
1202 RT_NOREF2(pVirtio, pVirtq);
1203}
1204
1205/**
1206 * Trigger MSI-X or INT# interrupt to notify guest of data added to used ring of
1207 * the specified virtq, depending on the interrupt configuration of the device
1208 * and depending on negotiated and realtime constraints flagged by the guest driver.
1209 *
1210 * See VirtIO 1.0 specification (section 2.4.7).
1211 *
1212 * @param pDevIns The device instance.
1213 * @param pVirtio Pointer to the shared virtio state.
1214 * @param uVirtq Virtq to check for guest interrupt handling preference
1215 */
1216static void virtioCoreNotifyGuestDriver(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t uVirtq)
1217{
1218 Assert(uVirtq < RT_ELEMENTS(pVirtio->aVirtqueues));
1219 PVIRTQUEUE pVirtq = &pVirtio->aVirtqueues[uVirtq];
1220
1221 if (!IS_DRIVER_OK(pVirtio))
1222 {
1223 LogFunc(("Guest driver not in ready state.\n"));
1224 return;
1225 }
1226
1227 if (pVirtio->uDriverFeatures & VIRTIO_F_EVENT_IDX)
1228 {
1229 if (pVirtq->fUsedRingEvent)
1230 {
1231#ifdef IN_RING3
1232 Log6Func(("...kicking guest %s, VIRTIO_F_EVENT_IDX set and threshold (%d) reached\n",
1233 pVirtq->szName, (uint16_t)virtioReadAvailUsedEvent(pDevIns, pVirtio, pVirtq)));
1234#endif
1235 virtioNudgeGuest(pDevIns, pVirtio, VIRTIO_ISR_VIRTQ_INTERRUPT, pVirtq->uMsixVector);
1236 pVirtq->fUsedRingEvent = false;
1237 return;
1238 }
1239#ifdef IN_RING3
1240 Log6Func(("...skip interrupt %s, VIRTIO_F_EVENT_IDX set but threshold (%d) not reached (%d)\n",
1241 pVirtq->szName,(uint16_t)virtioReadAvailUsedEvent(pDevIns, pVirtio, pVirtq), pVirtq->uUsedIdxShadow));
1242#endif
1243 }
1244 else
1245 {
1246 /** If guest driver hasn't suppressed interrupts, interrupt */
1247 if (!(virtioReadAvailRingFlags(pDevIns, pVirtio, pVirtq) & VIRTQ_AVAIL_F_NO_INTERRUPT))
1248 {
1249 virtioNudgeGuest(pDevIns, pVirtio, VIRTIO_ISR_VIRTQ_INTERRUPT, pVirtq->uMsixVector);
1250 return;
1251 }
1252 Log6Func(("...skipping interrupt for %s (guest set VIRTQ_AVAIL_F_NO_INTERRUPT)\n", pVirtq->szName));
1253 }
1254}
1255
1256/**
1257 * Raise interrupt or MSI-X
1258 *
1259 * @param pDevIns The device instance.
1260 * @param pVirtio Pointer to the shared virtio state.
1261 * @param uCause Interrupt cause bit mask to set in PCI ISR port.
1262 * @param uVec MSI-X vector, if enabled
1263 */
1264static int virtioNudgeGuest(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint8_t uCause, uint16_t uMsixVector)
1265{
1266 if (uCause == VIRTIO_ISR_VIRTQ_INTERRUPT)
1267 Log6Func(("Reason for interrupt - buffer added to 'used' ring.\n"));
1268 else
1269 if (uCause == VIRTIO_ISR_DEVICE_CONFIG)
1270 Log6Func(("Reason for interrupt - device config change\n"));
1271
1272 if (!pVirtio->fMsiSupport)
1273 {
1274 pVirtio->uISR |= uCause;
1275 PDMDevHlpPCISetIrq(pDevIns, 0, PDM_IRQ_LEVEL_HIGH);
1276 }
1277 else if (uMsixVector != VIRTIO_MSI_NO_VECTOR)
1278 PDMDevHlpPCISetIrq(pDevIns, uMsixVector, 1);
1279 return VINF_SUCCESS;
1280}
1281
1282/**
1283 * Lower interrupt (Called when guest reads ISR and when resetting)
1284 *
1285 * @param pDevIns The device instance.
1286 */
1287static void virtioLowerInterrupt(PPDMDEVINS pDevIns, uint16_t uMsixVector)
1288{
1289 PVIRTIOCORE pVirtio = PDMINS_2_DATA(pDevIns, PVIRTIOCORE);
1290 if (!pVirtio->fMsiSupport)
1291 PDMDevHlpPCISetIrq(pDevIns, 0, PDM_IRQ_LEVEL_LOW);
1292 else if (uMsixVector != VIRTIO_MSI_NO_VECTOR)
1293 PDMDevHlpPCISetIrq(pDevIns, pVirtio->uMsixConfig, PDM_IRQ_LEVEL_LOW);
1294}
1295
1296#ifdef IN_RING3
1297static void virtioResetVirtq(PVIRTIOCORE pVirtio, uint16_t uVirtq)
1298{
1299 Assert(uVirtq < RT_ELEMENTS(pVirtio->aVirtqueues));
1300 PVIRTQUEUE pVirtq = &pVirtio->aVirtqueues[uVirtq];
1301
1302 pVirtq->uQueueSize = VIRTQ_SIZE;
1303 pVirtq->uEnable = false;
1304 pVirtq->uNotifyOffset = uVirtq;
1305 pVirtq->fUsedRingEvent = false;
1306 pVirtq->uAvailIdxShadow = 0;
1307 pVirtq->uUsedIdxShadow = 0;
1308 pVirtq->uMsixVector = uVirtq + 2;
1309
1310 if (!pVirtio->fMsiSupport) /* VirtIO 1.0, 4.1.4.3 and 4.1.5.1.2 */
1311 pVirtq->uMsixVector = VIRTIO_MSI_NO_VECTOR;
1312
1313 virtioLowerInterrupt(pVirtio->pDevInsR3, pVirtq->uMsixVector);
1314}
1315
1316static void virtioResetDevice(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio)
1317{
1318 LogFunc(("Resetting device VirtIO state\n"));
1319 pVirtio->fLegacyDriver = pVirtio->fOfferLegacy; /* Cleared if VIRTIO_F_VERSION_1 feature ack'd */
1320 pVirtio->uDeviceFeaturesSelect = 0;
1321 pVirtio->uDriverFeaturesSelect = 0;
1322 pVirtio->uConfigGeneration = 0;
1323 pVirtio->fDeviceStatus = 0;
1324 pVirtio->uISR = 0;
1325
1326 if (!pVirtio->fMsiSupport)
1327 virtioLowerInterrupt(pDevIns, 0);
1328 else
1329 {
1330 virtioLowerInterrupt(pDevIns, pVirtio->uMsixConfig);
1331 for (int i = 0; i < VIRTQ_MAX_COUNT; i++)
1332 virtioLowerInterrupt(pDevIns, pVirtio->aVirtqueues[i].uMsixVector);
1333 }
1334
1335 if (!pVirtio->fMsiSupport) /* VirtIO 1.0, 4.1.4.3 and 4.1.5.1.2 */
1336 pVirtio->uMsixConfig = VIRTIO_MSI_NO_VECTOR;
1337
1338 for (uint16_t uVirtq = 0; uVirtq < VIRTQ_MAX_COUNT; uVirtq++)
1339 virtioResetVirtq(pVirtio, uVirtq);
1340}
1341
1342/**
1343 * Invoked by this implementation when guest driver resets the device.
1344 * The driver itself will not until the device has read the status change.
1345 */
1346static void virtioGuestR3WasReset(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, PVIRTIOCORECC pVirtioCC)
1347{
1348 Log(("%-23s: Guest reset the device\n", __FUNCTION__));
1349
1350 /* Let the client know */
1351 pVirtioCC->pfnStatusChanged(pVirtio, pVirtioCC, 0 /* fDriverOk */);
1352 virtioResetDevice(pDevIns, pVirtio);
1353}
1354
1355DECLHIDDEN(void) virtioCoreR3ResetDevice(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, PVIRTIOCORECC pVirtioCC)
1356{
1357 virtioGuestR3WasReset(pDevIns, pVirtio, pVirtioCC);
1358}
1359#endif /* IN_RING3 */
1360
1361/*
1362 * Determines whether guest virtio driver is modern or legacy and does callback
1363 * informing device-specific code that feature negotiation is complete.
1364 * Should be called only once (coordinated via the 'toggle' flag)
1365 */
1366#ifdef IN_RING3
1367DECLINLINE(void) virtioR3DoFeaturesCompleteOnceOnly(PVIRTIOCORE pVirtio, PVIRTIOCORECC pVirtioCC)
1368{
1369 if (pVirtio->uDriverFeatures & VIRTIO_F_VERSION_1)
1370 {
1371 LogFunc(("VIRTIO_F_VERSION_1 feature ack'd by guest\n"));
1372 pVirtio->fLegacyDriver = 0;
1373 }
1374 else
1375 {
1376 if (pVirtio->fOfferLegacy)
1377 {
1378 pVirtio->fLegacyDriver = 1;
1379 LogFunc(("VIRTIO_F_VERSION_1 feature was NOT set by guest\n"));
1380 }
1381 else
1382 AssertMsgFailed(("Guest didn't accept VIRTIO_F_VERSION_1, but fLegacyOffered flag not set.\n"));
1383 }
1384 if (pVirtioCC->pfnFeatureNegotiationComplete)
1385 pVirtioCC->pfnFeatureNegotiationComplete(pVirtio, pVirtio->uDriverFeatures, pVirtio->fLegacyDriver);
1386 pVirtio->fDriverFeaturesWritten |= DRIVER_FEATURES_COMPLETE_HANDLED;
1387}
1388#endif
1389
1390/**
1391 * Handle accesses to Common Configuration capability
1392 *
1393 * @returns VBox status code
1394 *
1395 * @param pDevIns The device instance.
1396 * @param pVirtio Pointer to the shared virtio state.
1397 * @param pVirtioCC Pointer to the current context virtio state.
1398 * @param fWrite Set if write access, clear if read access.
1399 * @param uOffsetOfAccess The common configuration capability offset.
1400 * @param cb Number of bytes to read or write
1401 * @param pv Pointer to location to write to or read from
1402 */
1403static int virtioCommonCfgAccessed(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, PVIRTIOCORECC pVirtioCC,
1404 int fWrite, uint32_t uOffsetOfAccess, unsigned cb, void *pv)
1405{
1406 uint16_t uVirtq = pVirtio->uVirtqSelect;
1407 int rc = VINF_SUCCESS;
1408 uint64_t val;
1409 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER(uDeviceFeatures, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess))
1410 {
1411 if (fWrite) /* Guest WRITE pCommonCfg>uDeviceFeatures */
1412 {
1413 /* VirtIO 1.0, 4.1.4.3 states device_feature is a (guest) driver readonly field,
1414 * yet the linux driver attempts to write/read it back twice */
1415 VIRTIO_DEV_CONFIG_LOG_ACCESS(uDeviceFeatures, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess);
1416 LogFunc(("... WARNING: Guest attempted to write readonly virtio_pci_common_cfg.device_feature (ignoring)\n"));
1417 return VINF_IOM_MMIO_UNUSED_00;
1418 }
1419 else /* Guest READ pCommonCfg->uDeviceFeatures */
1420 {
1421 switch (pVirtio->uDeviceFeaturesSelect)
1422 {
1423 case 0:
1424 val = pVirtio->uDeviceFeatures & UINT32_C(0xffffffff);
1425 memcpy(pv, &val, cb);
1426 VIRTIO_DEV_CONFIG_LOG_ACCESS(uDeviceFeatures, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess);
1427 break;
1428 case 1:
1429 val = pVirtio->uDeviceFeatures >> 32;
1430 memcpy(pv, &val, cb);
1431 VIRTIO_DEV_CONFIG_LOG_ACCESS(uDeviceFeatures, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess + sizeof(uint32_t));
1432 break;
1433 default:
1434 LogFunc(("Guest read uDeviceFeatures with out of range selector (%#x), returning 0\n",
1435 pVirtio->uDeviceFeaturesSelect));
1436 return VINF_IOM_MMIO_UNUSED_00;
1437 }
1438 }
1439 }
1440 else
1441 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER(uDriverFeatures, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess))
1442 {
1443 if (fWrite) /* Guest WRITE pCommonCfg->udriverFeatures */
1444 {
1445 switch (pVirtio->uDriverFeaturesSelect)
1446 {
1447 case 0:
1448 memcpy(&pVirtio->uDriverFeatures, pv, cb);
1449 pVirtio->fDriverFeaturesWritten |= DRIVER_FEATURES_0_WRITTEN;
1450 LogFunc(("Set DRIVER_FEATURES_0_WRITTEN. pVirtio->fDriverFeaturesWritten=%d\n", pVirtio->fDriverFeaturesWritten));
1451 if ( (pVirtio->fDriverFeaturesWritten & DRIVER_FEATURES_0_AND_1_WRITTEN) == DRIVER_FEATURES_0_AND_1_WRITTEN
1452 && !(pVirtio->fDriverFeaturesWritten & DRIVER_FEATURES_COMPLETE_HANDLED))
1453#ifdef IN_RING0
1454 return VINF_IOM_R3_MMIO_WRITE;
1455#endif
1456#ifdef IN_RING3
1457 virtioR3DoFeaturesCompleteOnceOnly(pVirtio, pVirtioCC);
1458#endif
1459 VIRTIO_DEV_CONFIG_LOG_ACCESS(uDriverFeatures, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess);
1460 break;
1461 case 1:
1462 memcpy((char *)&pVirtio->uDriverFeatures + sizeof(uint32_t), pv, cb);
1463 pVirtio->fDriverFeaturesWritten |= DRIVER_FEATURES_1_WRITTEN;
1464 LogFunc(("Set DRIVER_FEATURES_1_WRITTEN. pVirtio->fDriverFeaturesWritten=%d\n", pVirtio->fDriverFeaturesWritten));
1465 if ( (pVirtio->fDriverFeaturesWritten & DRIVER_FEATURES_0_AND_1_WRITTEN) == DRIVER_FEATURES_0_AND_1_WRITTEN
1466 && !(pVirtio->fDriverFeaturesWritten & DRIVER_FEATURES_COMPLETE_HANDLED))
1467#ifdef IN_RING0
1468 return VINF_IOM_R3_MMIO_WRITE;
1469#endif
1470#ifdef IN_RING3
1471 virtioR3DoFeaturesCompleteOnceOnly(pVirtio, pVirtioCC);
1472#endif
1473 VIRTIO_DEV_CONFIG_LOG_ACCESS(uDriverFeatures, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess + sizeof(uint32_t));
1474 break;
1475 default:
1476 LogFunc(("Guest wrote uDriverFeatures with out of range selector (%#x), returning 0\n",
1477 pVirtio->uDriverFeaturesSelect));
1478 return VINF_SUCCESS;
1479 }
1480 }
1481 else /* Guest READ pCommonCfg->udriverFeatures */
1482 {
1483 switch (pVirtio->uDriverFeaturesSelect)
1484 {
1485 case 0:
1486 val = pVirtio->uDriverFeatures & 0xffffffff;
1487 memcpy(pv, &val, cb);
1488 VIRTIO_DEV_CONFIG_LOG_ACCESS(uDriverFeatures, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess);
1489 break;
1490 case 1:
1491 val = (pVirtio->uDriverFeatures >> 32) & 0xffffffff;
1492 memcpy(pv, &val, cb);
1493 VIRTIO_DEV_CONFIG_LOG_ACCESS(uDriverFeatures, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess + 4);
1494 break;
1495 default:
1496 LogFunc(("Guest read uDriverFeatures with out of range selector (%#x), returning 0\n",
1497 pVirtio->uDriverFeaturesSelect));
1498 return VINF_IOM_MMIO_UNUSED_00;
1499 }
1500 }
1501 }
1502 else
1503 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER(uNumVirtqs, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess))
1504 {
1505 if (fWrite)
1506 {
1507 Log2Func(("Guest attempted to write readonly virtio_pci_common_cfg.num_queues\n"));
1508 return VINF_SUCCESS;
1509 }
1510 *(uint16_t *)pv = VIRTQ_MAX_COUNT;
1511 VIRTIO_DEV_CONFIG_LOG_ACCESS(uNumVirtqs, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess);
1512 }
1513 else
1514 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER(fDeviceStatus, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess))
1515 {
1516 if (fWrite) /* Guest WRITE pCommonCfg->fDeviceStatus */
1517 {
1518 pVirtio->fDeviceStatus = *(uint8_t *)pv;
1519 bool fDeviceReset = pVirtio->fDeviceStatus == 0;
1520#ifdef LOG_ENABLED
1521 if (LogIs7Enabled())
1522 {
1523 char szOut[80] = { 0 };
1524 virtioCoreFormatDeviceStatus(pVirtio->fDeviceStatus, szOut, sizeof(szOut));
1525 Log(("%-23s: Guest wrote fDeviceStatus ................ (%s)\n", __FUNCTION__, szOut));
1526 }
1527#endif
1528 bool const fStatusChanged = IS_DRIVER_OK(pVirtio) != WAS_DRIVER_OK(pVirtio);
1529
1530 if (fDeviceReset || fStatusChanged)
1531 {
1532#ifdef IN_RING0
1533 /* Since VirtIO status changes are cumbersome by nature, e.g. not a benchmark priority,
1534 * handle the rest in R3 to facilitate logging or whatever dev-specific client needs to do */
1535 Log6(("%-23s: RING0 => RING3 (demote)\n", __FUNCTION__));
1536 return VINF_IOM_R3_MMIO_WRITE;
1537#endif
1538 }
1539
1540#ifdef IN_RING3
1541 /*
1542 * Notify client only if status actually changed from last time and when we're reset.
1543 */
1544 if (fDeviceReset)
1545 virtioGuestR3WasReset(pDevIns, pVirtio, pVirtioCC);
1546
1547 if (fStatusChanged)
1548 pVirtioCC->pfnStatusChanged(pVirtio, pVirtioCC, IS_DRIVER_OK(pVirtio));
1549#endif
1550 /*
1551 * Save the current status for the next write so we can see what changed.
1552 */
1553 pVirtio->fPrevDeviceStatus = pVirtio->fDeviceStatus;
1554 }
1555 else /* Guest READ pCommonCfg->fDeviceStatus */
1556 {
1557 *(uint8_t *)pv = pVirtio->fDeviceStatus;
1558#ifdef LOG_ENABLED
1559 if (LogIs7Enabled())
1560 {
1561 char szOut[80] = { 0 };
1562 virtioCoreFormatDeviceStatus(pVirtio->fDeviceStatus, szOut, sizeof(szOut));
1563 LogFunc(("Guest read fDeviceStatus ................ (%s)\n", szOut));
1564 }
1565#endif
1566 }
1567 }
1568 else
1569 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uMsixConfig, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess))
1570 VIRTIO_DEV_CONFIG_ACCESS( uMsixConfig, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess, pVirtio);
1571 else
1572 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uDeviceFeaturesSelect, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess))
1573 VIRTIO_DEV_CONFIG_ACCESS( uDeviceFeaturesSelect, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess, pVirtio);
1574 else
1575 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uDriverFeaturesSelect, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess))
1576 VIRTIO_DEV_CONFIG_ACCESS( uDriverFeaturesSelect, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess, pVirtio);
1577 else
1578 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uConfigGeneration, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess))
1579 VIRTIO_DEV_CONFIG_ACCESS( uConfigGeneration, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess, pVirtio);
1580 else
1581 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uVirtqSelect, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess))
1582 {
1583 if (fWrite) {
1584 uint16_t uVirtqNew = *(uint16_t *)pv;
1585
1586 if (uVirtqNew < RT_ELEMENTS(pVirtio->aVirtqueues))
1587 VIRTIO_DEV_CONFIG_ACCESS( uVirtqSelect, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess, pVirtio);
1588 else
1589 LogFunc(("... WARNING: Guest attempted to write invalid virtq selector (ignoring)\n"));
1590 }
1591 else
1592 VIRTIO_DEV_CONFIG_ACCESS( uVirtqSelect, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess, pVirtio);
1593 }
1594 else
1595 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( GCPhysVirtqDesc, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess))
1596 VIRTIO_DEV_CONFIG_ACCESS_INDEXED( GCPhysVirtqDesc, uVirtq, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess, pVirtio->aVirtqueues);
1597 else
1598 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( GCPhysVirtqAvail, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess))
1599 VIRTIO_DEV_CONFIG_ACCESS_INDEXED( GCPhysVirtqAvail, uVirtq, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess, pVirtio->aVirtqueues);
1600 else
1601 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( GCPhysVirtqUsed, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess))
1602 VIRTIO_DEV_CONFIG_ACCESS_INDEXED( GCPhysVirtqUsed, uVirtq, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess, pVirtio->aVirtqueues);
1603 else
1604 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uQueueSize, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess))
1605 VIRTIO_DEV_CONFIG_ACCESS_INDEXED( uQueueSize, uVirtq, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess, pVirtio->aVirtqueues);
1606 else
1607 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uEnable, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess))
1608 VIRTIO_DEV_CONFIG_ACCESS_INDEXED( uEnable, uVirtq, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess, pVirtio->aVirtqueues);
1609 else
1610 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uNotifyOffset, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess))
1611 VIRTIO_DEV_CONFIG_ACCESS_INDEXED( uNotifyOffset, uVirtq, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess, pVirtio->aVirtqueues);
1612 else
1613 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uMsixVector, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess))
1614 VIRTIO_DEV_CONFIG_ACCESS_INDEXED( uMsixVector, uVirtq, VIRTIO_PCI_COMMON_CFG_T, uOffsetOfAccess, pVirtio->aVirtqueues);
1615 else
1616 {
1617 Log2Func(("Bad guest %s access to virtio_pci_common_cfg: uOffsetOfAccess=%#x (%d), cb=%d\n",
1618 fWrite ? "write" : "read ", uOffsetOfAccess, uOffsetOfAccess, cb));
1619 return fWrite ? VINF_SUCCESS : VINF_IOM_MMIO_UNUSED_00;
1620 }
1621
1622#ifndef IN_RING3
1623 RT_NOREF(pDevIns, pVirtioCC);
1624#endif
1625 return rc;
1626}
1627
1628/**
1629 * @callback_method_impl{FNIOMIOPORTNEWIN)
1630 *
1631 * This I/O handler exists only to handle access from legacy drivers.
1632 */
1633static DECLCALLBACK(VBOXSTRICTRC) virtioLegacyIOPortIn(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT offPort, uint32_t *pu32, unsigned cb)
1634{
1635 PVIRTIOCORE pVirtio = PDMINS_2_DATA(pDevIns, PVIRTIOCORE);
1636 STAM_PROFILE_ADV_START(&pVirtio->CTX_SUFF(StatRead), a);
1637
1638 RT_NOREF(pvUser);
1639 Log(("%-23s: Port read at offset=%RTiop, cb=%#x%s",
1640 __FUNCTION__, offPort, cb,
1641 VIRTIO_DEV_CONFIG_MATCH_MEMBER(fIsrStatus, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort) ? "" : "\n"));
1642
1643 void *pv = pu32; /* To use existing macros */
1644 int fWrite = 0; /* To use existing macros */
1645
1646 uint16_t uVirtq = pVirtio->uVirtqSelect;
1647
1648 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER(uDeviceFeatures, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort))
1649 {
1650 uint32_t val = pVirtio->uDeviceFeatures & UINT32_C(0xffffffff);
1651 memcpy(pu32, &val, cb);
1652 VIRTIO_DEV_CONFIG_LOG_ACCESS(uDeviceFeatures, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort);
1653 }
1654 else
1655 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER(uDriverFeatures, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort))
1656 {
1657 uint32_t val = pVirtio->uDriverFeatures & UINT32_C(0xffffffff);
1658 memcpy(pu32, &val, cb);
1659 VIRTIO_DEV_CONFIG_LOG_ACCESS(uDriverFeatures, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort);
1660 }
1661 else
1662 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER(fDeviceStatus, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort))
1663 {
1664 *(uint8_t *)pu32 = pVirtio->fDeviceStatus;
1665#ifdef LOG_ENABLED
1666 if (LogIs7Enabled())
1667 {
1668 char szOut[80] = { 0 };
1669 virtioCoreFormatDeviceStatus(pVirtio->fDeviceStatus, szOut, sizeof(szOut));
1670 Log(("%-23s: Guest read fDeviceStatus ................ (%s)\n", __FUNCTION__, szOut));
1671 }
1672#endif
1673 }
1674 else
1675 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER(fIsrStatus, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort))
1676 {
1677 ASSERT_GUEST_MSG(cb == 1, ("%d\n", cb));
1678 *(uint8_t *)pu32 = pVirtio->uISR;
1679 pVirtio->uISR = 0;
1680 virtioLowerInterrupt( pDevIns, 0);
1681 Log((" (ISR read and cleared)\n"));
1682 }
1683 else
1684 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uVirtqSelect, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort))
1685 VIRTIO_DEV_CONFIG_ACCESS( uVirtqSelect, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort, pVirtio);
1686 else
1687 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uVirtqPfn, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort))
1688 {
1689 PVIRTQUEUE pVirtQueue = &pVirtio->aVirtqueues[uVirtq];
1690 *pu32 = pVirtQueue->GCPhysVirtqDesc >> GUEST_PAGE_SHIFT;
1691 Log(("%-23s: Guest read uVirtqPfn .................... %#x\n", __FUNCTION__, *pu32));
1692 }
1693 else
1694 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uQueueSize, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort))
1695 VIRTIO_DEV_CONFIG_ACCESS_INDEXED( uQueueSize, uVirtq, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort, pVirtio->aVirtqueues);
1696 else
1697 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uQueueNotify, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort))
1698 VIRTIO_DEV_CONFIG_ACCESS( uQueueNotify, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort, pVirtio);
1699#ifdef LEGACY_MSIX_SUPPORTED
1700 else
1701 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uMsixConfig, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort))
1702 VIRTIO_DEV_CONFIG_ACCESS( uMsixConfig, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort, pVirtio);
1703 else
1704 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uMsixVector, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort))
1705 VIRTIO_DEV_CONFIG_ACCESS_INDEXED( uMsixVector, uVirtq, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort, pVirtio->aVirtqueues);
1706#endif
1707 else if (offPort >= sizeof(VIRTIO_LEGACY_PCI_COMMON_CFG_T))
1708 {
1709 STAM_PROFILE_ADV_STOP(&pVirtio->CTX_SUFF(StatRead), a);
1710#ifdef IN_RING3
1711 /* Access device-specific configuration */
1712 PVIRTIOCORECC pVirtioCC = PDMINS_2_DATA_CC(pDevIns, PVIRTIOCORECC);
1713 int rc = pVirtioCC->pfnDevCapRead(pDevIns, offPort - sizeof(VIRTIO_LEGACY_PCI_COMMON_CFG_T), pv, cb);
1714 return rc;
1715#else
1716 return VINF_IOM_R3_IOPORT_READ;
1717#endif
1718 }
1719 else
1720 {
1721 STAM_PROFILE_ADV_STOP(&pVirtio->CTX_SUFF(StatRead), a);
1722 Log2Func(("Bad guest read access to virtio_legacy_pci_common_cfg: offset=%#x, cb=%x\n",
1723 offPort, cb));
1724 int rc = PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS,
1725 "virtioLegacyIOPortIn: no valid port at offset offset=%RTiop cb=%#x\n", offPort, cb);
1726 return rc;
1727 }
1728 STAM_PROFILE_ADV_STOP(&pVirtio->CTX_SUFF(StatRead), a);
1729 return VINF_SUCCESS;
1730}
1731
1732/**
1733 * @callback_method_impl{ * @callback_method_impl{FNIOMIOPORTNEWOUT}
1734 *
1735 * This I/O Port interface exists only to handle access from legacy drivers.
1736 */
1737static DECLCALLBACK(VBOXSTRICTRC) virtioLegacyIOPortOut(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT offPort, uint32_t u32, unsigned cb)
1738{
1739 PVIRTIOCORE pVirtio = PDMINS_2_DATA(pDevIns, PVIRTIOCORE);
1740 STAM_PROFILE_ADV_START(&pVirtio->CTX_SUFF(StatWrite), a);
1741 RT_NOREF(pvUser);
1742
1743 uint16_t uVirtq = pVirtio->uVirtqSelect;
1744 uint32_t u32OnStack = u32; /* allows us to use this impl's MMIO parsing macros */
1745 void *pv = &u32OnStack; /* To use existing macros */
1746 int fWrite = 1; /* To use existing macros */
1747
1748 Log(("%-23s: Port written at offset=%RTiop, cb=%#x, u32=%#x\n", __FUNCTION__, offPort, cb, u32));
1749
1750 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uVirtqSelect, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort))
1751 {
1752 if (u32 < RT_ELEMENTS(pVirtio->aVirtqueues))
1753 VIRTIO_DEV_CONFIG_ACCESS( uVirtqSelect, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort, pVirtio);
1754 else
1755 LogFunc(("... WARNING: Guest attempted to write invalid virtq selector (ignoring)\n"));
1756 }
1757 else
1758#ifdef LEGACY_MSIX_SUPPORTED
1759 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uMsixConfig, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort))
1760 VIRTIO_DEV_CONFIG_ACCESS( uMsixConfig, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort, pVirtio);
1761 else
1762 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uMsixVector, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort))
1763 VIRTIO_DEV_CONFIG_ACCESS_INDEXED( uMsixVector, uVirtq, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort, pVirtio->aVirtqueues);
1764 else
1765#endif
1766 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER(uDeviceFeatures, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort))
1767 {
1768 /* Check to see if guest acknowledged unsupported features */
1769 VIRTIO_DEV_CONFIG_LOG_ACCESS(uDeviceFeatures, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort);
1770 LogFunc(("... WARNING: Guest attempted to write readonly virtio_pci_common_cfg.device_feature (ignoring)\n"));
1771 STAM_PROFILE_ADV_STOP(&pVirtio->CTX_SUFF(StatWrite), a);
1772 return VINF_SUCCESS;
1773 }
1774 else
1775 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER(uDriverFeatures, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort))
1776 {
1777 memcpy(&pVirtio->uDriverFeatures, pv, cb);
1778 if ((pVirtio->uDriverFeatures & ~VIRTIO_DEV_INDEPENDENT_LEGACY_FEATURES_OFFERED) == 0)
1779 {
1780 Log(("Guest asked for features host does not support! (host=%x guest=%x)\n",
1781 VIRTIO_DEV_INDEPENDENT_LEGACY_FEATURES_OFFERED, pVirtio->uDriverFeatures));
1782 pVirtio->uDriverFeatures &= VIRTIO_DEV_INDEPENDENT_LEGACY_FEATURES_OFFERED;
1783 }
1784 if (!(pVirtio->fDriverFeaturesWritten & DRIVER_FEATURES_COMPLETE_HANDLED))
1785 {
1786#ifdef IN_RING0
1787 Log6(("%-23s: RING0 => RING3 (demote)\n", __FUNCTION__));
1788 return VINF_IOM_R3_IOPORT_WRITE;
1789#endif
1790#ifdef IN_RING3
1791 PVIRTIOCORECC pVirtioCC = PDMINS_2_DATA_CC(pDevIns, PVIRTIOCORECC);
1792 virtioR3DoFeaturesCompleteOnceOnly(pVirtio, pVirtioCC);
1793#endif
1794 }
1795 VIRTIO_DEV_CONFIG_LOG_ACCESS(uDriverFeatures, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort);
1796 }
1797 else
1798 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER(uQueueSize, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort))
1799 {
1800 VIRTIO_DEV_CONFIG_LOG_ACCESS(uQueueSize, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort);
1801 LogFunc(("... WARNING: Guest attempted to write readonly device_feature (queue size) (ignoring)\n"));
1802 return VINF_SUCCESS;
1803 }
1804 else
1805 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER(fDeviceStatus, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort))
1806 {
1807 bool const fDriverInitiatedReset = (pVirtio->fDeviceStatus = (uint8_t)u32) == 0;
1808 bool const fDriverStateImproved = IS_DRIVER_OK(pVirtio) && !WAS_DRIVER_OK(pVirtio);
1809#ifdef LOG_ENABLED
1810 if (LogIs7Enabled())
1811 {
1812 char szOut[80] = { 0 };
1813 virtioCoreFormatDeviceStatus(pVirtio->fDeviceStatus, szOut, sizeof(szOut));
1814 Log(("%-23s: Guest wrote fDeviceStatus ................ (%s)\n", __FUNCTION__, szOut));
1815 }
1816#endif
1817 if (fDriverStateImproved || fDriverInitiatedReset)
1818 {
1819#ifdef IN_RING0
1820 Log6(("%-23s: RING0 => RING3 (demote)\n", __FUNCTION__));
1821 STAM_PROFILE_ADV_STOP(&pVirtio->CTX_SUFF(StatWrite), a);
1822 return VINF_IOM_R3_IOPORT_WRITE;
1823#endif
1824 }
1825
1826#ifdef IN_RING3
1827 PVIRTIOCORECC pVirtioCC = PDMINS_2_DATA_CC(pDevIns, PVIRTIOCORECC);
1828 if (fDriverInitiatedReset)
1829 virtioGuestR3WasReset(pDevIns, pVirtio, pVirtioCC);
1830
1831 else if (fDriverStateImproved)
1832 pVirtioCC->pfnStatusChanged(pVirtio, pVirtioCC, 1 /* fDriverOk */);
1833
1834#endif
1835 pVirtio->fPrevDeviceStatus = pVirtio->fDeviceStatus;
1836 }
1837 else
1838 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER(uVirtqPfn, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort))
1839 {
1840 PVIRTQUEUE pVirtq = &pVirtio->aVirtqueues[uVirtq];
1841 uint64_t uVirtqPfn = (uint64_t)u32;
1842
1843 if (uVirtqPfn)
1844 {
1845 /* Transitional devices calculate ring physical addresses using rigid spec-defined formulae,
1846 * instead of guest conveying respective address of each ring, as "modern" VirtIO drivers do,
1847 * thus there is no virtq PFN or single base queue address stored in instance data for
1848 * this transitional device, but rather it is derived, when read back, from GCPhysVirtqDesc */
1849
1850 pVirtq->GCPhysVirtqDesc = uVirtqPfn * VIRTIO_PAGE_SIZE;
1851 pVirtq->GCPhysVirtqAvail = pVirtq->GCPhysVirtqDesc + sizeof(VIRTQ_DESC_T) * pVirtq->uQueueSize;
1852 pVirtq->GCPhysVirtqUsed =
1853 RT_ALIGN(pVirtq->GCPhysVirtqAvail + RT_UOFFSETOF_DYN(VIRTQ_AVAIL_T, auRing[pVirtq->uQueueSize]), VIRTIO_PAGE_SIZE);
1854 }
1855 else
1856 {
1857 /* Don't set ring addresses for queue (to meaningless values), when guest resets the virtq's PFN */
1858 pVirtq->GCPhysVirtqDesc = 0;
1859 pVirtq->GCPhysVirtqAvail = 0;
1860 pVirtq->GCPhysVirtqUsed = 0;
1861 }
1862 Log(("%-23s: Guest wrote uVirtqPfn .................... %#x:\n"
1863 "%68s... %p -> GCPhysVirtqDesc\n%68s... %p -> GCPhysVirtqAvail\n%68s... %p -> GCPhysVirtqUsed\n",
1864 __FUNCTION__, u32, " ", pVirtq->GCPhysVirtqDesc, " ", pVirtq->GCPhysVirtqAvail, " ", pVirtq->GCPhysVirtqUsed));
1865 }
1866 else
1867 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER(uQueueNotify, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort))
1868 {
1869#ifdef IN_RING3
1870 ASSERT_GUEST_MSG(cb == 2, ("cb=%u\n", cb));
1871 pVirtio->uQueueNotify = u32 & 0xFFFF;
1872 if (uVirtq < VIRTQ_MAX_COUNT)
1873 {
1874 RT_UNTRUSTED_VALIDATED_FENCE();
1875
1876 /* Need to check that queue is configured. Legacy spec didn't have a queue enabled flag */
1877 if (pVirtio->aVirtqueues[pVirtio->uQueueNotify].GCPhysVirtqDesc)
1878 virtioCoreVirtqNotified(pDevIns, pVirtio, pVirtio->uQueueNotify, pVirtio->uQueueNotify /* uNotifyIdx */);
1879 else
1880 Log(("The queue (#%d) being notified has not been initialized.\n", pVirtio->uQueueNotify));
1881 }
1882 else
1883 Log(("Invalid queue number (%d)\n", pVirtio->uQueueNotify));
1884#else
1885 STAM_PROFILE_ADV_STOP(&pVirtio->CTX_SUFF(StatWrite), a);
1886 return VINF_IOM_R3_IOPORT_WRITE;
1887#endif
1888 }
1889 else
1890 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER(fIsrStatus, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort))
1891 {
1892 VIRTIO_DEV_CONFIG_LOG_ACCESS( fIsrStatus, VIRTIO_LEGACY_PCI_COMMON_CFG_T, offPort);
1893 LogFunc(("... WARNING: Guest attempted to write readonly device_feature (ISR status) (ignoring)\n"));
1894 STAM_PROFILE_ADV_STOP(&pVirtio->CTX_SUFF(StatWrite), a);
1895 return VINF_SUCCESS;
1896 }
1897 else if (offPort >= sizeof(VIRTIO_LEGACY_PCI_COMMON_CFG_T))
1898 {
1899#ifdef IN_RING3
1900
1901 /* Access device-specific configuration */
1902 PVIRTIOCORECC pVirtioCC = PDMINS_2_DATA_CC(pDevIns, PVIRTIOCORECC);
1903 return pVirtioCC->pfnDevCapWrite(pDevIns, offPort - sizeof(VIRTIO_LEGACY_PCI_COMMON_CFG_T), pv, cb);
1904#else
1905 STAM_PROFILE_ADV_STOP(&pVirtio->CTX_SUFF(StatWrite), a);
1906 return VINF_IOM_R3_IOPORT_WRITE;
1907#endif
1908 }
1909 else
1910 {
1911 Log2Func(("Bad guest write access to virtio_legacy_pci_common_cfg: offset=%#x, cb=0x%x\n",
1912 offPort, cb));
1913 STAM_PROFILE_ADV_STOP(&pVirtio->CTX_SUFF(StatWrite), a);
1914 int rc = PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS,
1915 "virtioLegacyIOPortOut: no valid port at offset offset=%RTiop cb=0x%#x\n", offPort, cb);
1916 return rc;
1917 }
1918
1919 RT_NOREF(uVirtq);
1920 STAM_PROFILE_ADV_STOP(&pVirtio->CTX_SUFF(StatWrite), a);
1921 return VINF_SUCCESS;
1922}
1923
1924
1925/**
1926 * @callback_method_impl{FNIOMMMIONEWREAD,
1927 * Memory mapped I/O Handler for PCI Capabilities read operations.}
1928 *
1929 * This MMIO handler specifically supports the VIRTIO_PCI_CAP_PCI_CFG capability defined
1930 * in the VirtIO 1.0 specification, section 4.1.4.7, and as such is restricted to reads
1931 * of 1, 2 or 4 bytes, only.
1932 *
1933 */
1934static DECLCALLBACK(VBOXSTRICTRC) virtioMmioRead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void *pv, unsigned cb)
1935{
1936 PVIRTIOCORE pVirtio = PDMINS_2_DATA(pDevIns, PVIRTIOCORE);
1937 PVIRTIOCORECC pVirtioCC = PDMINS_2_DATA_CC(pDevIns, PVIRTIOCORECC);
1938 AssertReturn(cb == 1 || cb == 2 || cb == 4, VERR_INVALID_PARAMETER);
1939 Assert(pVirtio == (PVIRTIOCORE)pvUser); RT_NOREF(pvUser);
1940 STAM_PROFILE_ADV_START(&pVirtio->CTX_SUFF(StatRead), a);
1941
1942
1943 uint32_t uOffset;
1944 if (MATCHES_VIRTIO_CAP_STRUCT(off, cb, uOffset, pVirtio->LocDeviceCap))
1945 {
1946#ifdef IN_RING3
1947 /*
1948 * Callback to client to manage device-specific configuration.
1949 */
1950 VBOXSTRICTRC rcStrict = pVirtioCC->pfnDevCapRead(pDevIns, uOffset, pv, cb);
1951
1952 /*
1953 * Anytime any part of the dev-specific dev config (which this virtio core implementation sees
1954 * as a blob, and virtio dev-specific code separates into fields) is READ, it must be compared
1955 * for deltas from previous read to maintain a config gen. seq. counter (VirtIO 1.0, section 4.1.4.3.1)
1956 */
1957 bool fDevSpecificFieldChanged = RT_BOOL(memcmp(pVirtioCC->pbDevSpecificCfg + uOffset,
1958 pVirtioCC->pbPrevDevSpecificCfg + uOffset,
1959 RT_MIN(cb, pVirtioCC->cbDevSpecificCfg - uOffset)));
1960
1961 memcpy(pVirtioCC->pbPrevDevSpecificCfg, pVirtioCC->pbDevSpecificCfg, pVirtioCC->cbDevSpecificCfg);
1962
1963 if (pVirtio->fGenUpdatePending || fDevSpecificFieldChanged)
1964 {
1965 ++pVirtio->uConfigGeneration;
1966 Log6Func(("Bumped cfg. generation to %d because %s%s\n", pVirtio->uConfigGeneration,
1967 fDevSpecificFieldChanged ? "<dev cfg changed> " : "",
1968 pVirtio->fGenUpdatePending ? "<update was pending>" : ""));
1969 pVirtio->fGenUpdatePending = false;
1970 }
1971
1972 virtioLowerInterrupt(pDevIns, 0);
1973 STAM_PROFILE_ADV_STOP(&pVirtio->CTX_SUFF(StatRead), a);
1974 return rcStrict;
1975#else
1976 STAM_PROFILE_ADV_STOP(&pVirtio->CTX_SUFF(StatRead), a);
1977 return VINF_IOM_R3_MMIO_READ;
1978#endif
1979 }
1980
1981 if (MATCHES_VIRTIO_CAP_STRUCT(off, cb, uOffset, pVirtio->LocCommonCfgCap))
1982 return virtioCommonCfgAccessed(pDevIns, pVirtio, pVirtioCC, false /* fWrite */, uOffset, cb, pv);
1983
1984 if (MATCHES_VIRTIO_CAP_STRUCT(off, cb, uOffset, pVirtio->LocIsrCap))
1985 {
1986 *(uint8_t *)pv = pVirtio->uISR;
1987 Log6Func(("Read and clear ISR\n"));
1988 pVirtio->uISR = 0; /* VirtIO spec requires reads of ISR to clear it */
1989 virtioLowerInterrupt(pDevIns, 0);
1990 STAM_PROFILE_ADV_STOP(&pVirtio->CTX_SUFF(StatRead), a);
1991 return VINF_SUCCESS;
1992 }
1993
1994 ASSERT_GUEST_MSG_FAILED(("Bad read access to mapped capabilities region: off=%RGp cb=%u\n", off, cb));
1995 STAM_PROFILE_ADV_STOP(&pVirtio->CTX_SUFF(StatRead), a);
1996 int rc = PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS,
1997 "virtioMmioRead: Bad MMIO access to capabilities, offset=%RTiop cb=%08x\n", off, cb);
1998 return rc;
1999}
2000
2001/**
2002 * @callback_method_impl{FNIOMMMIONEWREAD,
2003 * Memory mapped I/O Handler for PCI Capabilities write operations.}
2004 *
2005 * This MMIO handler specifically supports the VIRTIO_PCI_CAP_PCI_CFG capability defined
2006 * in the VirtIO 1.0 specification, section 4.1.4.7, and as such is restricted to writes
2007 * of 1, 2 or 4 bytes, only.
2008 */
2009static DECLCALLBACK(VBOXSTRICTRC) virtioMmioWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void const *pv, unsigned cb)
2010{
2011 PVIRTIOCORE pVirtio = PDMINS_2_DATA(pDevIns, PVIRTIOCORE);
2012 PVIRTIOCORECC pVirtioCC = PDMINS_2_DATA_CC(pDevIns, PVIRTIOCORECC);
2013 AssertReturn(cb == 1 || cb == 2 || cb == 4, VERR_INVALID_PARAMETER);
2014 Assert(pVirtio == (PVIRTIOCORE)pvUser); RT_NOREF(pvUser);
2015 STAM_PROFILE_ADV_START(&pVirtio->CTX_SUFF(StatWrite), a);
2016
2017 uint32_t uOffset;
2018 if (MATCHES_VIRTIO_CAP_STRUCT(off, cb, uOffset, pVirtio->LocDeviceCap))
2019 {
2020#ifdef IN_RING3
2021 /*
2022 * Foreward this MMIO write access for client to deal with.
2023 */
2024 STAM_PROFILE_ADV_STOP(&pVirtio->CTX_SUFF(StatWrite), a);
2025 return pVirtioCC->pfnDevCapWrite(pDevIns, uOffset, pv, cb);
2026#else
2027 STAM_PROFILE_ADV_STOP(&pVirtio->CTX_SUFF(StatWrite), a);
2028 Log6(("%-23s: RING0 => RING3 (demote)\n", __FUNCTION__));
2029 return VINF_IOM_R3_MMIO_WRITE;
2030#endif
2031 }
2032
2033 if (MATCHES_VIRTIO_CAP_STRUCT(off, cb, uOffset, pVirtio->LocCommonCfgCap))
2034 {
2035 STAM_PROFILE_ADV_STOP(&pVirtio->CTX_SUFF(StatWrite), a);
2036 return virtioCommonCfgAccessed(pDevIns, pVirtio, pVirtioCC, true /* fWrite */, uOffset, cb, (void *)pv);
2037 }
2038
2039 if (MATCHES_VIRTIO_CAP_STRUCT(off, cb, uOffset, pVirtio->LocIsrCap) && cb == sizeof(uint8_t))
2040 {
2041 pVirtio->uISR = *(uint8_t *)pv;
2042 Log6Func(("Setting uISR = 0x%02x (virtq interrupt: %d, dev confg interrupt: %d)\n",
2043 pVirtio->uISR & 0xff,
2044 pVirtio->uISR & VIRTIO_ISR_VIRTQ_INTERRUPT,
2045 RT_BOOL(pVirtio->uISR & VIRTIO_ISR_DEVICE_CONFIG)));
2046 STAM_PROFILE_ADV_STOP(&pVirtio->CTX_SUFF(StatWrite), a);
2047 return VINF_SUCCESS;
2048 }
2049
2050 /* This *should* be guest driver dropping index of a new descriptor in avail ring */
2051 if (MATCHES_VIRTIO_CAP_STRUCT(off, cb, uOffset, pVirtio->LocNotifyCap) && cb == sizeof(uint16_t))
2052 {
2053 virtioCoreVirtqNotified(pDevIns, pVirtio, uOffset / VIRTIO_NOTIFY_OFFSET_MULTIPLIER, *(uint16_t *)pv);
2054 STAM_PROFILE_ADV_STOP(&pVirtio->CTX_SUFF(StatWrite), a);
2055 return VINF_SUCCESS;
2056 }
2057
2058 ASSERT_GUEST_MSG_FAILED(("Bad write access to mapped capabilities region: off=%RGp pv=%#p{%.*Rhxs} cb=%u\n", off, pv, cb, pv, cb));
2059 STAM_PROFILE_ADV_STOP(&pVirtio->CTX_SUFF(StatWrite), a);
2060 int rc = PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS,
2061 "virtioMmioRead: Bad MMIO access to capabilities, offset=%RTiop cb=%08x\n", off, cb);
2062 return rc;
2063}
2064
2065#ifdef IN_RING3
2066
2067/**
2068 * @callback_method_impl{FNPCICONFIGREAD}
2069 */
2070static DECLCALLBACK(VBOXSTRICTRC) virtioR3PciConfigRead(PPDMDEVINS pDevIns, PPDMPCIDEV pPciDev,
2071 uint32_t uAddress, unsigned cb, uint32_t *pu32Value)
2072{
2073 PVIRTIOCORE pVirtio = PDMINS_2_DATA(pDevIns, PVIRTIOCORE);
2074 PVIRTIOCORECC pVirtioCC = PDMINS_2_DATA_CC(pDevIns, PVIRTIOCORECC);
2075 RT_NOREF(pPciDev);
2076
2077 if (uAddress == pVirtio->uPciCfgDataOff)
2078 {
2079 /* See comments in PCI Cfg capability initialization (in capabilities setup section of this code) */
2080 struct virtio_pci_cap *pPciCap = &pVirtioCC->pPciCfgCap->pciCap;
2081 uint32_t uLength = pPciCap->uLength;
2082
2083 Log7Func((" pDevIns=%p pPciDev=%p uAddress=%#x%s cb=%u uLength=%d, bar=%d\n",
2084 pDevIns, pPciDev, uAddress, uAddress < 0x10 ? " " : "", cb, uLength, pPciCap->uBar));
2085
2086 if ( (uLength != 1 && uLength != 2 && uLength != 4)
2087 || pPciCap->uBar != VIRTIO_REGION_PCI_CAP)
2088 {
2089 ASSERT_GUEST_MSG_FAILED(("Guest read virtio_pci_cfg_cap.pci_cfg_data using mismatching config. "
2090 "Ignoring\n"));
2091 *pu32Value = UINT32_MAX;
2092 return VINF_SUCCESS;
2093 }
2094
2095 VBOXSTRICTRC rcStrict = virtioMmioRead(pDevIns, pVirtio, pPciCap->uOffset, pu32Value, cb);
2096 Log7Func((" Guest read virtio_pci_cfg_cap.pci_cfg_data, bar=%d, offset=%d, length=%d, result=0x%x -> %Rrc\n",
2097 pPciCap->uBar, pPciCap->uOffset, uLength, *pu32Value, VBOXSTRICTRC_VAL(rcStrict)));
2098 return rcStrict;
2099 }
2100 Log7Func((" pDevIns=%p pPciDev=%p uAddress=%#x%s cb=%u pu32Value=%p\n",
2101 pDevIns, pPciDev, uAddress, uAddress < 0x10 ? " " : "", cb, pu32Value));
2102 return VINF_PDM_PCI_DO_DEFAULT;
2103}
2104
2105/**
2106 * @callback_method_impl{FNPCICONFIGWRITE}
2107 */
2108static DECLCALLBACK(VBOXSTRICTRC) virtioR3PciConfigWrite(PPDMDEVINS pDevIns, PPDMPCIDEV pPciDev,
2109 uint32_t uAddress, unsigned cb, uint32_t u32Value)
2110{
2111 PVIRTIOCORE pVirtio = PDMINS_2_DATA(pDevIns, PVIRTIOCORE);
2112 PVIRTIOCORECC pVirtioCC = PDMINS_2_DATA_CC(pDevIns, PVIRTIOCORECC);
2113 RT_NOREF(pPciDev);
2114
2115 Log7Func(("pDevIns=%p pPciDev=%p uAddress=%#x %scb=%u u32Value=%#x\n", pDevIns, pPciDev, uAddress, uAddress < 0xf ? " " : "", cb, u32Value));
2116 if (uAddress == pVirtio->uPciCfgDataOff)
2117 {
2118 /* See comments in PCI Cfg capability initialization (in capabilities setup section of this code) */
2119 struct virtio_pci_cap *pPciCap = &pVirtioCC->pPciCfgCap->pciCap;
2120 uint32_t uLength = pPciCap->uLength;
2121
2122 if ( (uLength != 1 && uLength != 2 && uLength != 4)
2123 || cb != uLength
2124 || pPciCap->uBar != VIRTIO_REGION_PCI_CAP)
2125 {
2126 ASSERT_GUEST_MSG_FAILED(("Guest write virtio_pci_cfg_cap.pci_cfg_data using mismatching config. Ignoring\n"));
2127 return VINF_SUCCESS;
2128 }
2129
2130 VBOXSTRICTRC rcStrict = virtioMmioWrite(pDevIns, pVirtio, pPciCap->uOffset, &u32Value, cb);
2131 Log2Func(("Guest wrote virtio_pci_cfg_cap.pci_cfg_data, bar=%d, offset=%x, length=%x, value=%d -> %Rrc\n",
2132 pPciCap->uBar, pPciCap->uOffset, uLength, u32Value, VBOXSTRICTRC_VAL(rcStrict)));
2133 return rcStrict;
2134 }
2135 return VINF_PDM_PCI_DO_DEFAULT;
2136}
2137
2138
2139/*********************************************************************************************************************************
2140* Saved state (SSM) *
2141*********************************************************************************************************************************/
2142
2143
2144/**
2145 * Loads a saved device state (called from device-specific code on SSM final pass)
2146 *
2147 * @param pVirtio Pointer to the shared virtio state.
2148 * @param pHlp The ring-3 device helpers.
2149 * @param pSSM The saved state handle.
2150 * @returns VBox status code.
2151 */
2152int virtioCoreR3LegacyDeviceLoadExec(PVIRTIOCORE pVirtio, PCPDMDEVHLPR3 pHlp,
2153 PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uVirtioLegacy_3_1_Beta)
2154{
2155 int rc;
2156 uint32_t uDriverFeaturesLegacy32bit;
2157
2158 rc = pHlp->pfnSSMGetU32( pSSM, &uDriverFeaturesLegacy32bit);
2159 AssertRCReturn(rc, rc);
2160 pVirtio->uDriverFeatures = (uint64_t)uDriverFeaturesLegacy32bit;
2161
2162 rc = pHlp->pfnSSMGetU16( pSSM, &pVirtio->uVirtqSelect);
2163 AssertRCReturn(rc, rc);
2164
2165 rc = pHlp->pfnSSMGetU8( pSSM, &pVirtio->fDeviceStatus);
2166 AssertRCReturn(rc, rc);
2167
2168#ifdef LOG_ENABLED
2169 char szOut[80] = { 0 };
2170 virtioCoreFormatDeviceStatus(pVirtio->fDeviceStatus, szOut, sizeof(szOut));
2171 Log(("Loaded legacy device status = (%s)\n", szOut));
2172#endif
2173
2174 rc = pHlp->pfnSSMGetU8( pSSM, &pVirtio->uISR);
2175 AssertRCReturn(rc, rc);
2176
2177 uint32_t cQueues = 3; /* This constant default value copied from earliest v0.9 code */
2178 if (uVersion > uVirtioLegacy_3_1_Beta)
2179 {
2180 rc = pHlp->pfnSSMGetU32(pSSM, &cQueues);
2181 AssertRCReturn(rc, rc);
2182 }
2183
2184 AssertLogRelMsgReturn(cQueues <= VIRTQ_MAX_COUNT, ("%#x\n", cQueues), VERR_SSM_LOAD_CONFIG_MISMATCH);
2185 AssertLogRelMsgReturn(pVirtio->uVirtqSelect < cQueues || (cQueues == 0 && pVirtio->uVirtqSelect),
2186 ("uVirtqSelect=%u cQueues=%u\n", pVirtio->uVirtqSelect, cQueues),
2187 VERR_SSM_LOAD_CONFIG_MISMATCH);
2188
2189 Log(("\nRestoring %d legacy-only virtio-net device queues from saved state:\n", cQueues));
2190 for (unsigned uVirtq = 0; uVirtq < cQueues; uVirtq++)
2191 {
2192 PVIRTQUEUE pVirtq = &pVirtio->aVirtqueues[uVirtq];
2193
2194 if (uVirtq == cQueues - 1)
2195 RTStrPrintf(pVirtq->szName, sizeof(pVirtq->szName), "legacy-ctrlq");
2196 else if (uVirtq % 2)
2197 RTStrPrintf(pVirtq->szName, sizeof(pVirtq->szName), "legacy-xmitq<%d>", uVirtq / 2);
2198 else
2199 RTStrPrintf(pVirtq->szName, sizeof(pVirtq->szName), "legacy-recvq<%d>", uVirtq / 2);
2200
2201 rc = pHlp->pfnSSMGetU16(pSSM, &pVirtq->uQueueSize);
2202 AssertRCReturn(rc, rc);
2203
2204 uint32_t uVirtqPfn;
2205 rc = pHlp->pfnSSMGetU32(pSSM, &uVirtqPfn);
2206 AssertRCReturn(rc, rc);
2207
2208 rc = pHlp->pfnSSMGetU16(pSSM, &pVirtq->uAvailIdxShadow);
2209 AssertRCReturn(rc, rc);
2210
2211 rc = pHlp->pfnSSMGetU16(pSSM, &pVirtq->uUsedIdxShadow);
2212 AssertRCReturn(rc, rc);
2213
2214 if (uVirtqPfn)
2215 {
2216 pVirtq->GCPhysVirtqDesc = (uint64_t)uVirtqPfn * VIRTIO_PAGE_SIZE;
2217 pVirtq->GCPhysVirtqAvail = pVirtq->GCPhysVirtqDesc + sizeof(VIRTQ_DESC_T) * pVirtq->uQueueSize;
2218 pVirtq->GCPhysVirtqUsed =
2219 RT_ALIGN(pVirtq->GCPhysVirtqAvail + RT_UOFFSETOF_DYN(VIRTQ_AVAIL_T, auRing[pVirtq->uQueueSize]), VIRTIO_PAGE_SIZE);
2220 pVirtq->uEnable = 1;
2221 }
2222 else
2223 {
2224 LogFunc(("WARNING: QUEUE \"%s\" PAGE NUMBER ZERO IN SAVED STATE\n", pVirtq->szName));
2225 pVirtq->uEnable = 0;
2226 }
2227 pVirtq->uNotifyOffset = 0; /* unused in legacy mode */
2228 pVirtq->uMsixVector = 0; /* unused in legacy mode */
2229 }
2230 pVirtio->fGenUpdatePending = 0; /* unused in legacy mode */
2231 pVirtio->uConfigGeneration = 0; /* unused in legacy mode */
2232 pVirtio->uPciCfgDataOff = 0; /* unused in legacy mode (port I/O used instead) */
2233
2234 return VINF_SUCCESS;
2235}
2236
2237/**
2238 * Loads a saved device state (called from device-specific code on SSM final pass)
2239 *
2240 * Note: This loads state saved by a Modern (VirtIO 1.0+) device, of which this transitional device is one,
2241 * and thus supports both legacy and modern guest virtio drivers.
2242 *
2243 * @param pVirtio Pointer to the shared virtio state.
2244 * @param pHlp The ring-3 device helpers.
2245 * @param pSSM The saved state handle.
2246 * @returns VBox status code.
2247 */
2248int virtioCoreR3ModernDeviceLoadExec(PVIRTIOCORE pVirtio, PCPDMDEVHLPR3 pHlp, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uTestVersion, uint32_t cQueues)
2249{
2250 RT_NOREF2(cQueues, uVersion);
2251 LogFunc(("\n"));
2252 /*
2253 * Check the marker and (embedded) version number.
2254 */
2255 uint64_t uMarker = 0;
2256 int rc;
2257
2258 rc = pHlp->pfnSSMGetU64(pSSM, &uMarker);
2259 AssertRCReturn(rc, rc);
2260 if (uMarker != VIRTIO_SAVEDSTATE_MARKER)
2261 return pHlp->pfnSSMSetLoadError(pSSM, VERR_SSM_DATA_UNIT_FORMAT_CHANGED, RT_SRC_POS,
2262 N_("Expected marker value %#RX64 found %#RX64 instead"),
2263 VIRTIO_SAVEDSTATE_MARKER, uMarker);
2264 uint32_t uVersionSaved = 0;
2265 rc = pHlp->pfnSSMGetU32(pSSM, &uVersionSaved);
2266 AssertRCReturn(rc, rc);
2267 if (uVersionSaved != uTestVersion)
2268 return pHlp->pfnSSMSetLoadError(pSSM, VERR_SSM_DATA_UNIT_FORMAT_CHANGED, RT_SRC_POS,
2269 N_("Unsupported virtio version: %u"), uVersionSaved);
2270 /*
2271 * Load the state.
2272 */
2273 rc = pHlp->pfnSSMGetU32( pSSM, &pVirtio->fLegacyDriver);
2274 AssertRCReturn(rc, rc);
2275 rc = pHlp->pfnSSMGetBool( pSSM, &pVirtio->fGenUpdatePending);
2276 AssertRCReturn(rc, rc);
2277 rc = pHlp->pfnSSMGetU8( pSSM, &pVirtio->fDeviceStatus);
2278 AssertRCReturn(rc, rc);
2279 rc = pHlp->pfnSSMGetU8( pSSM, &pVirtio->uConfigGeneration);
2280 AssertRCReturn(rc, rc);
2281 rc = pHlp->pfnSSMGetU8( pSSM, &pVirtio->uPciCfgDataOff);
2282 AssertRCReturn(rc, rc);
2283 rc = pHlp->pfnSSMGetU8( pSSM, &pVirtio->uISR);
2284 AssertRCReturn(rc, rc);
2285 rc = pHlp->pfnSSMGetU16( pSSM, &pVirtio->uVirtqSelect);
2286 AssertRCReturn(rc, rc);
2287 rc = pHlp->pfnSSMGetU32( pSSM, &pVirtio->uDeviceFeaturesSelect);
2288 AssertRCReturn(rc, rc);
2289 rc = pHlp->pfnSSMGetU32( pSSM, &pVirtio->uDriverFeaturesSelect);
2290 AssertRCReturn(rc, rc);
2291 rc = pHlp->pfnSSMGetU64( pSSM, &pVirtio->uDriverFeatures);
2292 AssertRCReturn(rc, rc);
2293
2294 /** @todo Adapt this loop use cQueues argument instead of static queue count (safely with SSM versioning) */
2295 for (uint32_t i = 0; i < VIRTQ_MAX_COUNT; i++)
2296 {
2297 PVIRTQUEUE pVirtq = &pVirtio->aVirtqueues[i];
2298 rc = pHlp->pfnSSMGetGCPhys64( pSSM, &pVirtq->GCPhysVirtqDesc);
2299 AssertRCReturn(rc, rc);
2300 rc = pHlp->pfnSSMGetGCPhys64( pSSM, &pVirtq->GCPhysVirtqAvail);
2301 AssertRCReturn(rc, rc);
2302 rc = pHlp->pfnSSMGetGCPhys64( pSSM, &pVirtq->GCPhysVirtqUsed);
2303 AssertRCReturn(rc, rc);
2304 rc = pHlp->pfnSSMGetU16( pSSM, &pVirtq->uNotifyOffset);
2305 AssertRCReturn(rc, rc);
2306 rc = pHlp->pfnSSMGetU16( pSSM, &pVirtq->uMsixVector);
2307 AssertRCReturn(rc, rc);
2308 rc = pHlp->pfnSSMGetU16( pSSM, &pVirtq->uEnable);
2309 AssertRCReturn(rc, rc);
2310 rc = pHlp->pfnSSMGetU16( pSSM, &pVirtq->uQueueSize);
2311 AssertRCReturn(rc, rc);
2312 rc = pHlp->pfnSSMGetU16( pSSM, &pVirtq->uAvailIdxShadow);
2313 AssertRCReturn(rc, rc);
2314 rc = pHlp->pfnSSMGetU16( pSSM, &pVirtq->uUsedIdxShadow);
2315 AssertRCReturn(rc, rc);
2316 rc = pHlp->pfnSSMGetMem( pSSM, pVirtq->szName, sizeof(pVirtq->szName));
2317 AssertRCReturn(rc, rc);
2318 }
2319 return VINF_SUCCESS;
2320}
2321
2322/**
2323 * Called from the FNSSMDEVSAVEEXEC function of the device.
2324 *
2325 * @param pVirtio Pointer to the shared virtio state.
2326 * @param pHlp The ring-3 device helpers.
2327 * @param pSSM The saved state handle.
2328 * @returns VBox status code.
2329 */
2330int virtioCoreR3SaveExec(PVIRTIOCORE pVirtio, PCPDMDEVHLPR3 pHlp, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t cQueues)
2331{
2332 RT_NOREF(cQueues);
2333 /** @todo figure out a way to save cQueues (with SSM versioning) */
2334
2335 LogFunc(("\n"));
2336 pHlp->pfnSSMPutU64(pSSM, VIRTIO_SAVEDSTATE_MARKER);
2337 pHlp->pfnSSMPutU32(pSSM, uVersion);
2338
2339 pHlp->pfnSSMPutU32( pSSM, pVirtio->fLegacyDriver);
2340 pHlp->pfnSSMPutBool(pSSM, pVirtio->fGenUpdatePending);
2341 pHlp->pfnSSMPutU8( pSSM, pVirtio->fDeviceStatus);
2342 pHlp->pfnSSMPutU8( pSSM, pVirtio->uConfigGeneration);
2343 pHlp->pfnSSMPutU8( pSSM, pVirtio->uPciCfgDataOff);
2344 pHlp->pfnSSMPutU8( pSSM, pVirtio->uISR);
2345 pHlp->pfnSSMPutU16( pSSM, pVirtio->uVirtqSelect);
2346 pHlp->pfnSSMPutU32( pSSM, pVirtio->uDeviceFeaturesSelect);
2347 pHlp->pfnSSMPutU32( pSSM, pVirtio->uDriverFeaturesSelect);
2348 pHlp->pfnSSMPutU64( pSSM, pVirtio->uDriverFeatures);
2349
2350 for (uint32_t i = 0; i < VIRTQ_MAX_COUNT; i++)
2351 {
2352 PVIRTQUEUE pVirtq = &pVirtio->aVirtqueues[i];
2353
2354 pHlp->pfnSSMPutGCPhys64( pSSM, pVirtq->GCPhysVirtqDesc);
2355 pHlp->pfnSSMPutGCPhys64( pSSM, pVirtq->GCPhysVirtqAvail);
2356 pHlp->pfnSSMPutGCPhys64( pSSM, pVirtq->GCPhysVirtqUsed);
2357 pHlp->pfnSSMPutU16( pSSM, pVirtq->uNotifyOffset);
2358 pHlp->pfnSSMPutU16( pSSM, pVirtq->uMsixVector);
2359 pHlp->pfnSSMPutU16( pSSM, pVirtq->uEnable);
2360 pHlp->pfnSSMPutU16( pSSM, pVirtq->uQueueSize);
2361 pHlp->pfnSSMPutU16( pSSM, pVirtq->uAvailIdxShadow);
2362 pHlp->pfnSSMPutU16( pSSM, pVirtq->uUsedIdxShadow);
2363 int rc = pHlp->pfnSSMPutMem(pSSM, pVirtq->szName, 32);
2364 AssertRCReturn(rc, rc);
2365 }
2366 return VINF_SUCCESS;
2367}
2368
2369
2370/*********************************************************************************************************************************
2371* Device Level *
2372*********************************************************************************************************************************/
2373
2374/**
2375 * This must be called by the client to handle VM state changes after the client takes care of its device-specific
2376 * tasks for the state change (i.e. reset, suspend, power-off, resume)
2377 *
2378 * @param pDevIns The device instance.
2379 * @param pVirtio Pointer to the shared virtio state.
2380 */
2381void virtioCoreR3VmStateChanged(PVIRTIOCORE pVirtio, VIRTIOVMSTATECHANGED enmState)
2382{
2383 LogFunc(("State changing to %s\n",
2384 virtioCoreGetStateChangeText(enmState)));
2385
2386 switch(enmState)
2387 {
2388 case kvirtIoVmStateChangedReset:
2389 virtioCoreResetAll(pVirtio);
2390 break;
2391 case kvirtIoVmStateChangedSuspend:
2392 break;
2393 case kvirtIoVmStateChangedPowerOff:
2394 break;
2395 case kvirtIoVmStateChangedResume:
2396 for (int uVirtq = 0; uVirtq < VIRTQ_MAX_COUNT; uVirtq++)
2397 {
2398 if ((!pVirtio->fLegacyDriver && pVirtio->aVirtqueues[uVirtq].uEnable)
2399 | pVirtio->aVirtqueues[uVirtq].GCPhysVirtqDesc)
2400 virtioCoreNotifyGuestDriver(pVirtio->pDevInsR3, pVirtio, uVirtq);
2401 }
2402 break;
2403 default:
2404 LogRelFunc(("Bad enum value"));
2405 return;
2406 }
2407}
2408
2409/**
2410 * This should be called from PDMDEVREGR3::pfnDestruct.
2411 *
2412 * @param pDevIns The device instance.
2413 * @param pVirtio Pointer to the shared virtio state.
2414 * @param pVirtioCC Pointer to the ring-3 virtio state.
2415 */
2416void virtioCoreR3Term(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, PVIRTIOCORECC pVirtioCC)
2417{
2418 if (pVirtioCC->pbPrevDevSpecificCfg)
2419 {
2420 RTMemFree(pVirtioCC->pbPrevDevSpecificCfg);
2421 pVirtioCC->pbPrevDevSpecificCfg = NULL;
2422 }
2423
2424 RT_NOREF(pDevIns, pVirtio);
2425}
2426
2427/** API Function: See header file */
2428int virtioCoreR3Init(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, PVIRTIOCORECC pVirtioCC, PVIRTIOPCIPARAMS pPciParams,
2429 const char *pcszInstance, uint64_t fDevSpecificFeatures, uint32_t fOfferLegacy,
2430 void *pvDevSpecificCfg, uint16_t cbDevSpecificCfg)
2431{
2432 /*
2433 * Virtio state must be the first member of shared device instance data,
2434 * otherwise can't get our bearings in PCI config callbacks.
2435 */
2436 AssertLogRelReturn(pVirtio == PDMINS_2_DATA(pDevIns, PVIRTIOCORE), VERR_STATE_CHANGED);
2437 AssertLogRelReturn(pVirtioCC == PDMINS_2_DATA_CC(pDevIns, PVIRTIOCORECC), VERR_STATE_CHANGED);
2438
2439 pVirtio->pDevInsR3 = pDevIns;
2440
2441 /*
2442 * Caller must initialize these.
2443 */
2444 AssertReturn(pVirtioCC->pfnStatusChanged, VERR_INVALID_POINTER);
2445 AssertReturn(pVirtioCC->pfnVirtqNotified, VERR_INVALID_POINTER);
2446 AssertReturn(VIRTQ_SIZE > 0 && VIRTQ_SIZE <= 32768, VERR_OUT_OF_RANGE); /* VirtIO specification-defined limit */
2447
2448#if 0 /* Until pdmR3DvHlp_PCISetIrq() impl is fixed and Assert that limits vec to 0 is removed
2449 * VBox legacy MSI support has not been implemented yet
2450 */
2451# ifdef VBOX_WITH_MSI_DEVICES
2452 pVirtio->fMsiSupport = true;
2453# endif
2454#endif
2455
2456 /*
2457 * Host features (presented as a smörgasbord for guest to select from)
2458 * include both dev-specific features & reserved dev-independent features (bitmask).
2459 */
2460 pVirtio->uDeviceFeatures = VIRTIO_F_VERSION_1
2461 | VIRTIO_DEV_INDEPENDENT_FEATURES_OFFERED
2462 | fDevSpecificFeatures;
2463
2464 pVirtio->fLegacyDriver = pVirtio->fOfferLegacy = fOfferLegacy;
2465
2466 RTStrCopy(pVirtio->szInstance, sizeof(pVirtio->szInstance), pcszInstance);
2467 pVirtioCC->cbDevSpecificCfg = cbDevSpecificCfg;
2468 pVirtioCC->pbDevSpecificCfg = (uint8_t *)pvDevSpecificCfg;
2469 pVirtioCC->pbPrevDevSpecificCfg = (uint8_t *)RTMemDup(pvDevSpecificCfg, cbDevSpecificCfg);
2470 AssertLogRelReturn(pVirtioCC->pbPrevDevSpecificCfg, VERR_NO_MEMORY);
2471
2472 /* Set PCI config registers (assume 32-bit mode) */
2473 PPDMPCIDEV pPciDev = pDevIns->apPciDevs[0];
2474 PDMPCIDEV_ASSERT_VALID(pDevIns, pPciDev);
2475
2476 PDMPciDevSetVendorId(pPciDev, DEVICE_PCI_VENDOR_ID_VIRTIO);
2477 PDMPciDevSetDeviceId(pPciDev, pPciParams->uDeviceId);
2478
2479 if (pPciParams->uDeviceId < DEVICE_PCI_DEVICE_ID_VIRTIO_BASE)
2480 /* Transitional devices MUST have a PCI Revision ID of 0. */
2481 PDMPciDevSetRevisionId(pPciDev, DEVICE_PCI_REVISION_ID_VIRTIO_TRANS);
2482 else
2483 /* Non-transitional devices SHOULD have a PCI Revision ID of 1 or higher. */
2484 PDMPciDevSetRevisionId(pPciDev, DEVICE_PCI_REVISION_ID_VIRTIO_V1);
2485
2486 PDMPciDevSetSubSystemId(pPciDev, pPciParams->uSubsystemId);
2487 PDMPciDevSetSubSystemVendorId(pPciDev, DEVICE_PCI_VENDOR_ID_VIRTIO);
2488 PDMPciDevSetClassBase(pPciDev, pPciParams->uClassBase);
2489 PDMPciDevSetClassSub(pPciDev, pPciParams->uClassSub);
2490 PDMPciDevSetClassProg(pPciDev, pPciParams->uClassProg);
2491 PDMPciDevSetInterruptLine(pPciDev, pPciParams->uInterruptLine);
2492 PDMPciDevSetInterruptPin(pPciDev, pPciParams->uInterruptPin);
2493
2494 /* Register PCI device */
2495 int rc = PDMDevHlpPCIRegister(pDevIns, pPciDev);
2496 if (RT_FAILURE(rc))
2497 return PDMDEV_SET_ERROR(pDevIns, rc, N_("virtio: cannot register PCI Device")); /* can we put params in this error? */
2498
2499 rc = PDMDevHlpPCIInterceptConfigAccesses(pDevIns, pPciDev, virtioR3PciConfigRead, virtioR3PciConfigWrite);
2500 AssertRCReturn(rc, rc);
2501
2502 /* Construct & map PCI vendor-specific capabilities for virtio host negotiation with guest driver */
2503
2504#define CFG_ADDR_2_IDX(addr) ((uint8_t)(((uintptr_t)(addr) - (uintptr_t)&pPciDev->abConfig[0])))
2505#define SET_PCI_CAP_LOC(a_pPciDev, a_pCfg, a_LocCap, a_uMmioLengthAlign) \
2506 do { \
2507 (a_LocCap).offMmio = (a_pCfg)->uOffset; \
2508 (a_LocCap).cbMmio = RT_ALIGN_T((a_pCfg)->uLength, a_uMmioLengthAlign, uint16_t); \
2509 (a_LocCap).offPci = (uint16_t)(uintptr_t)((uint8_t *)(a_pCfg) - &(a_pPciDev)->abConfig[0]); \
2510 (a_LocCap).cbPci = (a_pCfg)->uCapLen; \
2511 } while (0)
2512
2513 PVIRTIO_PCI_CAP_T pCfg;
2514 uint32_t cbRegion = 0;
2515
2516 /*
2517 * Common capability (VirtIO 1.0, section 4.1.4.3)
2518 */
2519 pCfg = (PVIRTIO_PCI_CAP_T)&pPciDev->abConfig[0x40];
2520 pCfg->uCfgType = VIRTIO_PCI_CAP_COMMON_CFG;
2521 pCfg->uCapVndr = VIRTIO_PCI_CAP_ID_VENDOR;
2522 pCfg->uCapLen = sizeof(VIRTIO_PCI_CAP_T);
2523 pCfg->uCapNext = CFG_ADDR_2_IDX(pCfg) + pCfg->uCapLen;
2524 pCfg->uBar = VIRTIO_REGION_PCI_CAP;
2525 pCfg->uOffset = RT_ALIGN_32(0, 4); /* Currently 0, but reminder to 32-bit align if changing this */
2526 pCfg->uLength = sizeof(VIRTIO_PCI_COMMON_CFG_T);
2527 cbRegion += pCfg->uLength;
2528 SET_PCI_CAP_LOC(pPciDev, pCfg, pVirtio->LocCommonCfgCap, 2);
2529 pVirtioCC->pCommonCfgCap = pCfg;
2530
2531 /*
2532 * Notify capability (VirtIO 1.0, section 4.1.4.4).
2533 *
2534 * The size of the spec-defined subregion described by this VirtIO capability is
2535 * based-on the choice of this implementation to make the notification area of each
2536 * queue equal to queue's ordinal position (e.g. queue selector value). The VirtIO
2537 * specification leaves it up to implementation to define queue notification area layout.
2538 */
2539 pCfg = (PVIRTIO_PCI_CAP_T)&pPciDev->abConfig[pCfg->uCapNext];
2540 pCfg->uCfgType = VIRTIO_PCI_CAP_NOTIFY_CFG;
2541 pCfg->uCapVndr = VIRTIO_PCI_CAP_ID_VENDOR;
2542 pCfg->uCapLen = sizeof(VIRTIO_PCI_NOTIFY_CAP_T);
2543 pCfg->uCapNext = CFG_ADDR_2_IDX(pCfg) + pCfg->uCapLen;
2544 pCfg->uBar = VIRTIO_REGION_PCI_CAP;
2545 pCfg->uOffset = pVirtioCC->pCommonCfgCap->uOffset + pVirtioCC->pCommonCfgCap->uLength;
2546 pCfg->uOffset = RT_ALIGN_32(pCfg->uOffset, 4);
2547 pCfg->uLength = VIRTQ_MAX_COUNT * VIRTIO_NOTIFY_OFFSET_MULTIPLIER + 2; /* will change in VirtIO 1.1 */
2548 cbRegion += pCfg->uLength;
2549 SET_PCI_CAP_LOC(pPciDev, pCfg, pVirtio->LocNotifyCap, 1);
2550 pVirtioCC->pNotifyCap = (PVIRTIO_PCI_NOTIFY_CAP_T)pCfg;
2551 pVirtioCC->pNotifyCap->uNotifyOffMultiplier = VIRTIO_NOTIFY_OFFSET_MULTIPLIER;
2552
2553 /* ISR capability (VirtIO 1.0, section 4.1.4.5)
2554 *
2555 * VirtIO 1.0 spec says 8-bit, unaligned in MMIO space. The specification example/diagram
2556 * illustrates this capability as 32-bit field with upper bits 'reserved'. Those depictions
2557 * differ. The spec's wording, not the diagram, is seen to work in practice.
2558 */
2559 pCfg = (PVIRTIO_PCI_CAP_T)&pPciDev->abConfig[pCfg->uCapNext];
2560 pCfg->uCfgType = VIRTIO_PCI_CAP_ISR_CFG;
2561 pCfg->uCapVndr = VIRTIO_PCI_CAP_ID_VENDOR;
2562 pCfg->uCapLen = sizeof(VIRTIO_PCI_CAP_T);
2563 pCfg->uCapNext = CFG_ADDR_2_IDX(pCfg) + pCfg->uCapLen;
2564 pCfg->uBar = VIRTIO_REGION_PCI_CAP;
2565 pCfg->uOffset = pVirtioCC->pNotifyCap->pciCap.uOffset + pVirtioCC->pNotifyCap->pciCap.uLength;
2566 pCfg->uOffset = RT_ALIGN_32(pCfg->uOffset, 4);
2567 pCfg->uLength = sizeof(uint8_t);
2568 cbRegion += pCfg->uLength;
2569 SET_PCI_CAP_LOC(pPciDev, pCfg, pVirtio->LocIsrCap, 4);
2570 pVirtioCC->pIsrCap = pCfg;
2571
2572 /* PCI Cfg capability (VirtIO 1.0, section 4.1.4.7)
2573 *
2574 * This capability facilitates early-boot access to this device (BIOS).
2575 * This region isn't page-MMIO mapped. PCI configuration accesses are intercepted,
2576 * wherein uBar, uOffset and uLength are modulated by consumers to locate and read/write
2577 * values in any part of any region. (NOTE: Linux driver doesn't utilize this feature.
2578 * This capability only appears in lspci output on Linux if uLength is non-zero, 4-byte aligned,
2579 * during initialization of linux virtio driver).
2580 */
2581 pVirtio->uPciCfgDataOff = pCfg->uCapNext + RT_OFFSETOF(VIRTIO_PCI_CFG_CAP_T, uPciCfgData);
2582 pCfg = (PVIRTIO_PCI_CAP_T)&pPciDev->abConfig[pCfg->uCapNext];
2583 pCfg->uCfgType = VIRTIO_PCI_CAP_PCI_CFG;
2584 pCfg->uCapVndr = VIRTIO_PCI_CAP_ID_VENDOR;
2585 pCfg->uCapLen = sizeof(VIRTIO_PCI_CFG_CAP_T);
2586 pCfg->uCapNext = (pVirtio->fMsiSupport || pVirtioCC->pbDevSpecificCfg) ? CFG_ADDR_2_IDX(pCfg) + pCfg->uCapLen : 0;
2587 pCfg->uBar = VIRTIO_REGION_PCI_CAP;
2588 pCfg->uOffset = 0;
2589 pCfg->uLength = 4;
2590 cbRegion += pCfg->uLength;
2591 SET_PCI_CAP_LOC(pPciDev, pCfg, pVirtio->LocPciCfgCap, 1);
2592 pVirtioCC->pPciCfgCap = (PVIRTIO_PCI_CFG_CAP_T)pCfg;
2593
2594 if (pVirtioCC->pbDevSpecificCfg)
2595 {
2596 /* Device-specific config capability (VirtIO 1.0, section 4.1.4.6).
2597 *
2598 * Client defines the device-specific config struct and passes size to virtioCoreR3Init()
2599 * to inform this.
2600 */
2601 pCfg = (PVIRTIO_PCI_CAP_T)&pPciDev->abConfig[pCfg->uCapNext];
2602 pCfg->uCfgType = VIRTIO_PCI_CAP_DEVICE_CFG;
2603 pCfg->uCapVndr = VIRTIO_PCI_CAP_ID_VENDOR;
2604 pCfg->uCapLen = sizeof(VIRTIO_PCI_CAP_T);
2605 pCfg->uCapNext = pVirtio->fMsiSupport ? CFG_ADDR_2_IDX(pCfg) + pCfg->uCapLen : 0;
2606 pCfg->uBar = VIRTIO_REGION_PCI_CAP;
2607 pCfg->uOffset = pVirtioCC->pIsrCap->uOffset + pVirtioCC->pIsrCap->uLength;
2608 pCfg->uOffset = RT_ALIGN_32(pCfg->uOffset, 4);
2609 pCfg->uLength = cbDevSpecificCfg;
2610 cbRegion += pCfg->uLength;
2611 SET_PCI_CAP_LOC(pPciDev, pCfg, pVirtio->LocDeviceCap, 4);
2612 pVirtioCC->pDeviceCap = pCfg;
2613 }
2614 else
2615 Assert(pVirtio->LocDeviceCap.cbMmio == 0 && pVirtio->LocDeviceCap.cbPci == 0);
2616
2617 if (pVirtio->fMsiSupport)
2618 {
2619 PDMMSIREG aMsiReg;
2620 RT_ZERO(aMsiReg);
2621 aMsiReg.iMsixCapOffset = pCfg->uCapNext;
2622 aMsiReg.iMsixNextOffset = 0;
2623 aMsiReg.iMsixBar = VIRTIO_REGION_MSIX_CAP;
2624 aMsiReg.cMsixVectors = VBOX_MSIX_MAX_ENTRIES;
2625 rc = PDMDevHlpPCIRegisterMsi(pDevIns, &aMsiReg); /* see MsixR3init() */
2626 if (RT_FAILURE(rc))
2627 {
2628 /* See PDMDevHlp.cpp:pdmR3DevHlp_PCIRegisterMsi */
2629 LogFunc(("Failed to configure MSI-X (%Rrc). Reverting to INTx\n", rc));
2630 pVirtio->fMsiSupport = false;
2631 }
2632 else
2633 Log2Func(("Using MSI-X for guest driver notification\n"));
2634 }
2635 else
2636 LogFunc(("MSI-X not available for VBox, using INTx notification\n"));
2637
2638 /* Set offset to first capability and enable PCI dev capabilities */
2639 PDMPciDevSetCapabilityList(pPciDev, 0x40);
2640 PDMPciDevSetStatus(pPciDev, VBOX_PCI_STATUS_CAP_LIST);
2641
2642 size_t cbSize = RTStrPrintf(pVirtioCC->szMmioName, sizeof(pVirtioCC->szMmioName), "%s (modern)", pcszInstance);
2643 if (cbSize <= 0)
2644 return PDMDEV_SET_ERROR(pDevIns, rc, N_("virtio: out of memory allocating string")); /* can we put params in this error? */
2645
2646 cbSize = RTStrPrintf(pVirtioCC->szPortIoName, sizeof(pVirtioCC->szPortIoName), "%s (legacy)", pcszInstance);
2647 if (cbSize <= 0)
2648 return PDMDEV_SET_ERROR(pDevIns, rc, N_("virtio: out of memory allocating string")); /* can we put params in this error? */
2649
2650 if (pVirtio->fOfferLegacy)
2651 {
2652 /* As a transitional device that supports legacy VirtIO drivers, this VirtIO device generic implementation presents
2653 * legacy driver interface in I/O space at BAR0. The following maps the common (e.g. device independent)
2654 * dev config area as well as device-specific dev config area (whose size is passed to init function of this VirtIO
2655 * generic device code) for access via Port I/O, since legacy drivers (e.g. pre VirtIO 1.0) don't use MMIO callbacks.
2656 * (See VirtIO 1.1, Section 4.1.4.8).
2657 */
2658 rc = PDMDevHlpPCIIORegionCreateIo(pDevIns, VIRTIO_REGION_LEGACY_IO, sizeof(VIRTIO_LEGACY_PCI_COMMON_CFG_T) + cbDevSpecificCfg,
2659 virtioLegacyIOPortOut, virtioLegacyIOPortIn, NULL /*pvUser*/, pVirtioCC->szPortIoName,
2660 NULL /*paExtDescs*/, &pVirtio->hLegacyIoPorts);
2661 AssertLogRelRCReturn(rc, PDMDEV_SET_ERROR(pDevIns, rc, N_("virtio: cannot register legacy config in I/O space at BAR0 */")));
2662 }
2663
2664 /* Note: The Linux driver at drivers/virtio/virtio_pci_modern.c tries to map at least a page for the
2665 * 'unknown' device-specific capability without querying the capability to determine size, so pad w/extra page.
2666 */
2667 rc = PDMDevHlpPCIIORegionCreateMmio(pDevIns, VIRTIO_REGION_PCI_CAP, RT_ALIGN_32(cbRegion + VIRTIO_PAGE_SIZE, VIRTIO_PAGE_SIZE),
2668 PCI_ADDRESS_SPACE_MEM, virtioMmioWrite, virtioMmioRead, pVirtio,
2669 IOMMMIO_FLAGS_READ_PASSTHRU | IOMMMIO_FLAGS_WRITE_PASSTHRU,
2670 pVirtioCC->szMmioName,
2671 &pVirtio->hMmioPciCap);
2672 AssertLogRelRCReturn(rc, PDMDEV_SET_ERROR(pDevIns, rc, N_("virtio: cannot register PCI Capabilities address space")));
2673 /*
2674 * Statistics.
2675 */
2676# ifdef VBOX_WITH_STATISTICS
2677 PDMDevHlpSTAMRegisterF(pDevIns, &pVirtio->StatDescChainsAllocated, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT,
2678 "Total number of allocated descriptor chains", "DescChainsAllocated");
2679 PDMDevHlpSTAMRegisterF(pDevIns, &pVirtio->StatDescChainsFreed, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT,
2680 "Total number of freed descriptor chains", "DescChainsFreed");
2681 PDMDevHlpSTAMRegisterF(pDevIns, &pVirtio->StatDescChainsSegsIn, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT,
2682 "Total number of inbound segments", "DescChainsSegsIn");
2683 PDMDevHlpSTAMRegisterF(pDevIns, &pVirtio->StatDescChainsSegsOut, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT,
2684 "Total number of outbound segments", "DescChainsSegsOut");
2685 PDMDevHlpSTAMRegister(pDevIns, &pVirtio->StatReadR3, STAMTYPE_PROFILE, "IO/ReadR3", STAMUNIT_TICKS_PER_CALL, "Profiling IO reads in R3");
2686 PDMDevHlpSTAMRegister(pDevIns, &pVirtio->StatReadR0, STAMTYPE_PROFILE, "IO/ReadR0", STAMUNIT_TICKS_PER_CALL, "Profiling IO reads in R0");
2687 PDMDevHlpSTAMRegister(pDevIns, &pVirtio->StatReadRC, STAMTYPE_PROFILE, "IO/ReadRC", STAMUNIT_TICKS_PER_CALL, "Profiling IO reads in RC");
2688 PDMDevHlpSTAMRegister(pDevIns, &pVirtio->StatWriteR3, STAMTYPE_PROFILE, "IO/WriteR3", STAMUNIT_TICKS_PER_CALL, "Profiling IO writes in R3");
2689 PDMDevHlpSTAMRegister(pDevIns, &pVirtio->StatWriteR0, STAMTYPE_PROFILE, "IO/WriteR0", STAMUNIT_TICKS_PER_CALL, "Profiling IO writes in R0");
2690 PDMDevHlpSTAMRegister(pDevIns, &pVirtio->StatWriteRC, STAMTYPE_PROFILE, "IO/WriteRC", STAMUNIT_TICKS_PER_CALL, "Profiling IO writes in RC");
2691# endif /* VBOX_WITH_STATISTICS */
2692
2693 return VINF_SUCCESS;
2694}
2695
2696#else /* !IN_RING3 */
2697
2698/**
2699 * Sets up the core ring-0/raw-mode virtio bits.
2700 *
2701 * @returns VBox status code.
2702 * @param pDevIns The device instance.
2703 * @param pVirtio Pointer to the shared virtio state. This must be the first
2704 * member in the shared device instance data!
2705 */
2706int virtioCoreRZInit(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio)
2707{
2708 AssertLogRelReturn(pVirtio == PDMINS_2_DATA(pDevIns, PVIRTIOCORE), VERR_STATE_CHANGED);
2709 int rc;
2710#ifdef FUTURE_OPTIMIZATION
2711 rc = PDMDevHlpSetDeviceCritSect(pDevIns, PDMDevHlpCritSectGetNop(pDevIns));
2712 AssertRCReturn(rc, rc);
2713#endif
2714 rc = PDMDevHlpMmioSetUpContext(pDevIns, pVirtio->hMmioPciCap, virtioMmioWrite, virtioMmioRead, pVirtio);
2715 AssertRCReturn(rc, rc);
2716
2717 if (pVirtio->fOfferLegacy)
2718 {
2719 rc = PDMDevHlpIoPortSetUpContext(pDevIns, pVirtio->hLegacyIoPorts, virtioLegacyIOPortOut, virtioLegacyIOPortIn, NULL /*pvUser*/);
2720 AssertRCReturn(rc, rc);
2721 }
2722 return rc;
2723}
2724
2725#endif /* !IN_RING3 */
2726
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