VirtualBox

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

Last change on this file since 96622 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

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