VirtualBox

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

Last change on this file since 97046 was 97030, checked in by vboxsync, 3 years ago

virtio: Do not select the wrong virtq (see bugref:10301).

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

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