VirtualBox

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

Last change on this file since 93732 was 93115, checked in by vboxsync, 3 years ago

scm --update-copyright-year

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

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