1 | /* $Id: Virtio_1_0.cpp 84782 2020-06-11 07:13:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Virtio_1_0 - Virtio Common (PCI, feature & config mgt, queue mgt & proxy, notification mgt)
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2020 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #define LOG_GROUP LOG_GROUP_DEV_VIRTIO
|
---|
23 |
|
---|
24 | #include <iprt/assert.h>
|
---|
25 | #include <iprt/uuid.h>
|
---|
26 | #include <iprt/mem.h>
|
---|
27 | #include <iprt/sg.h>
|
---|
28 | #include <iprt/assert.h>
|
---|
29 | #include <iprt/string.h>
|
---|
30 | #include <iprt/param.h>
|
---|
31 | #include <iprt/types.h>
|
---|
32 | #include <VBox/log.h>
|
---|
33 | #include <VBox/msi.h>
|
---|
34 | #include <iprt/types.h>
|
---|
35 | #include <VBox/AssertGuest.h>
|
---|
36 | #include <VBox/vmm/pdmdev.h>
|
---|
37 | #include "Virtio_1_0.h"
|
---|
38 |
|
---|
39 | /*********************************************************************************************************************************
|
---|
40 | * Defined Constants And Macros *
|
---|
41 | *********************************************************************************************************************************/
|
---|
42 | #define INSTANCE(a_pVirtio) ((a_pVirtio)->szInstance)
|
---|
43 | #define VIRTQNAME(a_pVirtio, a_idxQueue) ((a_pVirtio)->virtqState[(a_idxQueue)].szVirtqName)
|
---|
44 | #define IS_DRIVER_OK(a_pVirtio) ((a_pVirtio)->uDeviceStatus & VIRTIO_STATUS_DRIVER_OK)
|
---|
45 | #define IS_VIRTQ_EMPTY(pDevIns, pVirtio, pVirtq) \
|
---|
46 | (virtioCoreQueueAvailCount(pDevIns, pVirtio, pVirtq) == 0)
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * This macro returns true if the @a a_offAccess and access length (@a
|
---|
50 | * a_cbAccess) are within the range of the mapped capability struct described by
|
---|
51 | * @a a_LocCapData.
|
---|
52 | *
|
---|
53 | * @param[in] a_offAccess The offset into the MMIO bar of the access.
|
---|
54 | * @param[in] a_cbAccess The access size.
|
---|
55 | * @param[out] a_offsetInMbr The variable to return the intra-capability
|
---|
56 | * offset into. ASSUMES this is uint32_t.
|
---|
57 | * @param[in] a_LocCapData The capability location info.
|
---|
58 | */
|
---|
59 | #define MATCHES_VIRTIO_CAP_STRUCT(a_offAccess, a_cbAccess, a_offsetInMbr, a_LocCapData) \
|
---|
60 | ( ((a_offsetInMbr) = (uint32_t)((a_offAccess) - (a_LocCapData).offMmio)) < (uint32_t)(a_LocCapData).cbMmio \
|
---|
61 | && (a_offsetInMbr) + (uint32_t)(a_cbAccess) <= (uint32_t)(a_LocCapData).cbMmio )
|
---|
62 |
|
---|
63 |
|
---|
64 | /** Marks the start of the virtio saved state (just for sanity). */
|
---|
65 | #define VIRTIO_SAVEDSTATE_MARKER UINT64_C(0x1133557799bbddff)
|
---|
66 | /** The current saved state version for the virtio core. */
|
---|
67 | #define VIRTIO_SAVEDSTATE_VERSION UINT32_C(1)
|
---|
68 |
|
---|
69 |
|
---|
70 | /*********************************************************************************************************************************
|
---|
71 | * Structures and Typedefs *
|
---|
72 | *********************************************************************************************************************************/
|
---|
73 |
|
---|
74 |
|
---|
75 | /** @name virtq related flags
|
---|
76 | * @{ */
|
---|
77 | #define VIRTQ_DESC_F_NEXT 1 /**< Indicates this descriptor chains to next */
|
---|
78 | #define VIRTQ_DESC_F_WRITE 2 /**< Marks buffer as write-only (default ro) */
|
---|
79 | #define VIRTQ_DESC_F_INDIRECT 4 /**< Buffer is list of buffer descriptors */
|
---|
80 |
|
---|
81 | #define VIRTQ_USED_F_NO_NOTIFY 1 /**< Dev to Drv: Don't notify when buf added */
|
---|
82 | #define VIRTQ_AVAIL_F_NO_INTERRUPT 1 /**< Drv to Dev: Don't notify when buf eaten */
|
---|
83 | /** @} */
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * virtq related structs
|
---|
87 | * (struct names follow VirtIO 1.0 spec, typedef use VBox style)
|
---|
88 | */
|
---|
89 | typedef struct virtq_desc
|
---|
90 | {
|
---|
91 | uint64_t GCPhysBuf; /**< addr GC Phys. address of buffer */
|
---|
92 | uint32_t cb; /**< len Buffer length */
|
---|
93 | uint16_t fFlags; /**< flags Buffer specific flags */
|
---|
94 | uint16_t uDescIdxNext; /**< next Idx set if VIRTIO_DESC_F_NEXT */
|
---|
95 | } VIRTQ_DESC_T, *PVIRTQ_DESC_T;
|
---|
96 |
|
---|
97 | typedef struct virtq_avail
|
---|
98 | {
|
---|
99 | uint16_t fFlags; /**< flags avail ring guest-to-host flags */
|
---|
100 | uint16_t uIdx; /**< idx Index of next free ring slot */
|
---|
101 | RT_FLEXIBLE_ARRAY_EXTENSION
|
---|
102 | uint16_t auRing[RT_FLEXIBLE_ARRAY]; /**< ring Ring: avail drv to dev bufs */
|
---|
103 | /* uint16_t uUsedEventIdx; - used_event (if VIRTQ_USED_F_EVENT_IDX) */
|
---|
104 | } VIRTQ_AVAIL_T, *PVIRTQ_AVAIL_T;
|
---|
105 |
|
---|
106 | typedef struct virtq_used_elem
|
---|
107 | {
|
---|
108 | uint32_t uDescIdx; /**< idx Start of used desc chain */
|
---|
109 | uint32_t cbElem; /**< len Total len of used desc chain */
|
---|
110 | } VIRTQ_USED_ELEM_T;
|
---|
111 |
|
---|
112 | typedef struct virt_used
|
---|
113 | {
|
---|
114 | uint16_t fFlags; /**< flags used ring host-to-guest flags */
|
---|
115 | uint16_t uIdx; /**< idx Index of next ring slot */
|
---|
116 | RT_FLEXIBLE_ARRAY_EXTENSION
|
---|
117 | VIRTQ_USED_ELEM_T aRing[RT_FLEXIBLE_ARRAY]; /**< ring Ring: used dev to drv bufs */
|
---|
118 | /* uint16_t uAvailEventIdx; - avail_event if (VIRTQ_USED_F_EVENT_IDX) */
|
---|
119 | } VIRTQ_USED_T, *PVIRTQ_USED_T;
|
---|
120 |
|
---|
121 |
|
---|
122 | const char *virtioCoreGetStateChangeText(VIRTIOVMSTATECHANGED enmState)
|
---|
123 | {
|
---|
124 | switch (enmState)
|
---|
125 | {
|
---|
126 | case kvirtIoVmStateChangedReset: return "VM RESET";
|
---|
127 | case kvirtIoVmStateChangedSuspend: return "VM SUSPEND";
|
---|
128 | case kvirtIoVmStateChangedPowerOff: return "VM POWER OFF";
|
---|
129 | case kvirtIoVmStateChangedResume: return "VM RESUME";
|
---|
130 | default: return "<BAD ENUM>";
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | /* Internal Functions */
|
---|
135 |
|
---|
136 | static void virtioCoreNotifyGuestDriver(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t idxQueue);
|
---|
137 | static int virtioKick(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint8_t uCause, uint16_t uVec);
|
---|
138 |
|
---|
139 | /** @name Internal queue operations
|
---|
140 | * @{ */
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Accessor for virtq descriptor
|
---|
144 | */
|
---|
145 | #ifdef IN_RING3
|
---|
146 | DECLINLINE(void) virtioReadDesc(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t idxQueue,
|
---|
147 | uint32_t idxDesc, PVIRTQ_DESC_T pDesc)
|
---|
148 | {
|
---|
149 | AssertMsg(pVirtio->uDeviceStatus & VIRTIO_STATUS_DRIVER_OK, ("Called with guest driver not ready\n"));
|
---|
150 | uint16_t const cQueueItems = RT_MAX(pVirtio->uQueueSize[idxQueue], 1); /* Make sure to avoid div-by-zero. */
|
---|
151 | PDMDevHlpPCIPhysRead(pDevIns,
|
---|
152 | pVirtio->aGCPhysQueueDesc[idxQueue] + sizeof(VIRTQ_DESC_T) * (idxDesc % cQueueItems),
|
---|
153 | pDesc, sizeof(VIRTQ_DESC_T));
|
---|
154 | }
|
---|
155 | #endif
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * Accessors for virtq avail ring
|
---|
159 | */
|
---|
160 | #ifdef IN_RING3
|
---|
161 | DECLINLINE(uint16_t) virtioReadAvailDescIdx(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t idxQueue, uint32_t availIdx)
|
---|
162 | {
|
---|
163 | uint16_t uDescIdx;
|
---|
164 | AssertMsg(pVirtio->uDeviceStatus & VIRTIO_STATUS_DRIVER_OK, ("Called with guest driver not ready\n"));
|
---|
165 | uint16_t const cQueueItems = RT_MAX(pVirtio->uQueueSize[idxQueue], 1); /* Make sure to avoid div-by-zero. */
|
---|
166 | PDMDevHlpPCIPhysRead(pDevIns,
|
---|
167 | pVirtio->aGCPhysQueueAvail[idxQueue]
|
---|
168 | + RT_UOFFSETOF_DYN(VIRTQ_AVAIL_T, auRing[availIdx % cQueueItems]),
|
---|
169 | &uDescIdx, sizeof(uDescIdx));
|
---|
170 | return uDescIdx;
|
---|
171 | }
|
---|
172 |
|
---|
173 | DECLINLINE(uint16_t) virtioReadAvailUsedEvent(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t idxQueue)
|
---|
174 | {
|
---|
175 | uint16_t uUsedEventIdx;
|
---|
176 | /* VirtIO 1.0 uUsedEventIdx (used_event) immediately follows ring */
|
---|
177 | AssertMsg(pVirtio->uDeviceStatus & VIRTIO_STATUS_DRIVER_OK, ("Called with guest driver not ready\n"));
|
---|
178 | PDMDevHlpPCIPhysRead(pDevIns,
|
---|
179 | pVirtio->aGCPhysQueueAvail[idxQueue] + RT_UOFFSETOF_DYN(VIRTQ_AVAIL_T, auRing[pVirtio->uQueueSize[idxQueue]]),
|
---|
180 | &uUsedEventIdx, sizeof(uUsedEventIdx));
|
---|
181 | return uUsedEventIdx;
|
---|
182 | }
|
---|
183 | #endif
|
---|
184 |
|
---|
185 | DECLINLINE(uint16_t) virtioReadAvailRingIdx(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t idxQueue)
|
---|
186 | {
|
---|
187 | uint16_t uIdx = 0;
|
---|
188 | AssertMsg(pVirtio->uDeviceStatus & VIRTIO_STATUS_DRIVER_OK, ("Called with guest driver not ready\n"));
|
---|
189 | PDMDevHlpPCIPhysRead(pDevIns,
|
---|
190 | pVirtio->aGCPhysQueueAvail[idxQueue] + RT_UOFFSETOF(VIRTQ_AVAIL_T, uIdx),
|
---|
191 | &uIdx, sizeof(uIdx));
|
---|
192 | return uIdx;
|
---|
193 | }
|
---|
194 |
|
---|
195 | DECLINLINE(uint16_t) virtioReadAvailRingFlags(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t idxQueue)
|
---|
196 | {
|
---|
197 | uint16_t fFlags = 0;
|
---|
198 | AssertMsg(pVirtio->uDeviceStatus & VIRTIO_STATUS_DRIVER_OK, ("Called with guest driver not ready\n"));
|
---|
199 | PDMDevHlpPCIPhysRead(pDevIns,
|
---|
200 | pVirtio->aGCPhysQueueAvail[idxQueue] + RT_UOFFSETOF(VIRTQ_AVAIL_T, fFlags),
|
---|
201 | &fFlags, sizeof(fFlags));
|
---|
202 | return fFlags;
|
---|
203 | }
|
---|
204 |
|
---|
205 | /** @} */
|
---|
206 |
|
---|
207 | /** @name Accessors for virtq used ring
|
---|
208 | * @{
|
---|
209 | */
|
---|
210 |
|
---|
211 | #ifdef IN_RING3
|
---|
212 | DECLINLINE(void) virtioWriteUsedElem(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t idxQueue,
|
---|
213 | uint32_t usedIdx, uint32_t uDescIdx, uint32_t uLen)
|
---|
214 | {
|
---|
215 | VIRTQ_USED_ELEM_T elem = { uDescIdx, uLen };
|
---|
216 | AssertMsg(pVirtio->uDeviceStatus & VIRTIO_STATUS_DRIVER_OK, ("Called with guest driver not ready\n"));
|
---|
217 | uint16_t const cQueueItems = RT_MAX(pVirtio->uQueueSize[idxQueue], 1); /* Make sure to avoid div-by-zero. */
|
---|
218 | PDMDevHlpPCIPhysWrite(pDevIns,
|
---|
219 | pVirtio->aGCPhysQueueUsed[idxQueue] + RT_UOFFSETOF_DYN(VIRTQ_USED_T, aRing[usedIdx % cQueueItems]),
|
---|
220 | &elem, sizeof(elem));
|
---|
221 | }
|
---|
222 |
|
---|
223 | DECLINLINE(void) virtioWriteUsedRingFlags(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t idxQueue, uint16_t fFlags)
|
---|
224 | {
|
---|
225 | AssertMsg(pVirtio->uDeviceStatus & VIRTIO_STATUS_DRIVER_OK, ("Called with guest driver not ready\n"));
|
---|
226 | RT_UNTRUSTED_VALIDATED_FENCE(); /* VirtIO 1.0, Section 3.2.1.4.1 */
|
---|
227 | PDMDevHlpPCIPhysWrite(pDevIns,
|
---|
228 | pVirtio->aGCPhysQueueUsed[idxQueue] + RT_UOFFSETOF(VIRTQ_USED_T, fFlags),
|
---|
229 | &fFlags, sizeof(fFlags));
|
---|
230 | }
|
---|
231 | #endif
|
---|
232 |
|
---|
233 | DECLINLINE(void) virtioWriteUsedRingIdx(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t idxQueue, uint16_t uIdx)
|
---|
234 | {
|
---|
235 | AssertMsg(pVirtio->uDeviceStatus & VIRTIO_STATUS_DRIVER_OK, ("Called with guest driver not ready\n"));
|
---|
236 | PDMDevHlpPCIPhysWrite(pDevIns,
|
---|
237 | pVirtio->aGCPhysQueueUsed[idxQueue] + RT_UOFFSETOF(VIRTQ_USED_T, uIdx),
|
---|
238 | &uIdx, sizeof(uIdx));
|
---|
239 | }
|
---|
240 |
|
---|
241 |
|
---|
242 | #ifdef IN_RING3
|
---|
243 |
|
---|
244 | DECLINLINE(uint16_t) virtioReadUsedRingIdx(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t idxQueue)
|
---|
245 | {
|
---|
246 | uint16_t uIdx = 0;
|
---|
247 | AssertMsg(pVirtio->uDeviceStatus & VIRTIO_STATUS_DRIVER_OK, ("Called with guest driver not ready\n"));
|
---|
248 | PDMDevHlpPCIPhysRead(pDevIns,
|
---|
249 | pVirtio->aGCPhysQueueUsed[idxQueue] + RT_UOFFSETOF(VIRTQ_USED_T, uIdx),
|
---|
250 | &uIdx, sizeof(uIdx));
|
---|
251 | return uIdx;
|
---|
252 | }
|
---|
253 |
|
---|
254 | DECLINLINE(uint16_t) virtioReadUsedRingFlags(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t idxQueue)
|
---|
255 | {
|
---|
256 | uint16_t fFlags = 0;
|
---|
257 | AssertMsg(pVirtio->uDeviceStatus & VIRTIO_STATUS_DRIVER_OK, ("Called with guest driver not ready\n"));
|
---|
258 | PDMDevHlpPCIPhysRead(pDevIns,
|
---|
259 | pVirtio->aGCPhysQueueUsed[idxQueue] + RT_UOFFSETOF(VIRTQ_USED_T, fFlags),
|
---|
260 | &fFlags, sizeof(fFlags));
|
---|
261 | return fFlags;
|
---|
262 | }
|
---|
263 |
|
---|
264 | DECLINLINE(void) virtioWriteUsedAvailEvent(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t idxQueue, uint32_t uAvailEventIdx)
|
---|
265 | {
|
---|
266 | /** VirtIO 1.0 uAvailEventIdx (avail_event) immediately follows ring */
|
---|
267 | AssertMsg(pVirtio->uDeviceStatus & VIRTIO_STATUS_DRIVER_OK, ("Called with guest driver not ready\n"));
|
---|
268 | PDMDevHlpPCIPhysWrite(pDevIns,
|
---|
269 | pVirtio->aGCPhysQueueUsed[idxQueue] + RT_UOFFSETOF_DYN(VIRTQ_USED_T, aRing[pVirtio->uQueueSize[idxQueue]]),
|
---|
270 | &uAvailEventIdx, sizeof(uAvailEventIdx));
|
---|
271 | }
|
---|
272 |
|
---|
273 |
|
---|
274 | #endif
|
---|
275 |
|
---|
276 | DECLINLINE(uint16_t) virtioCoreQueueAvailCount(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, PVIRTQSTATE pVirtq)
|
---|
277 | {
|
---|
278 | uint16_t uIdx = virtioReadAvailRingIdx(pDevIns, pVirtio, pVirtq->idxQueue);
|
---|
279 | uint16_t uShadow = pVirtq->uAvailIdxShadow;
|
---|
280 |
|
---|
281 | uint16_t uDelta;
|
---|
282 | if (uIdx < uShadow)
|
---|
283 | uDelta = (uIdx + VIRTQ_MAX_ENTRIES) - uShadow;
|
---|
284 | else
|
---|
285 | uDelta = uIdx - uShadow;
|
---|
286 |
|
---|
287 | LogFunc(("%s has %u %s (idx=%u shadow=%u)\n",
|
---|
288 | VIRTQNAME(pVirtio, pVirtq->idxQueue), uDelta, uDelta == 1 ? "entry" : "entries",
|
---|
289 | uIdx, uShadow));
|
---|
290 |
|
---|
291 | return uDelta;
|
---|
292 | }
|
---|
293 | /**
|
---|
294 | * Get count of new (e.g. pending) elements in available ring.
|
---|
295 | *
|
---|
296 | * @param pDevIns The device instance.
|
---|
297 | * @param pVirtio Pointer to the shared virtio state.
|
---|
298 | * @param idxQueue Queue number
|
---|
299 | *
|
---|
300 | * @returns how many entries have been added to ring as a delta of the consumer's
|
---|
301 | * avail index and the queue's guest-side current avail index.
|
---|
302 | */
|
---|
303 | uint16_t virtioCoreQueueAvailCount(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t idxQueue)
|
---|
304 | {
|
---|
305 | if (!IS_DRIVER_OK(pVirtio) || !pVirtio->uQueueEnable[idxQueue])
|
---|
306 | {
|
---|
307 | LogRelFunc(("Driver not ready or queue not enabled\n"));
|
---|
308 | return 0;
|
---|
309 | }
|
---|
310 | return virtioCoreQueueAvailCount(pDevIns, pVirtio, &pVirtio->virtqState[idxQueue]);
|
---|
311 | }
|
---|
312 |
|
---|
313 |
|
---|
314 | /** @} */
|
---|
315 |
|
---|
316 | void virtioCoreSgBufInit(PVIRTIOSGBUF pGcSgBuf, PVIRTIOSGSEG paSegs, size_t cSegs)
|
---|
317 | {
|
---|
318 | AssertPtr(pGcSgBuf);
|
---|
319 | Assert( (cSegs > 0 && VALID_PTR(paSegs)) || (!cSegs && !paSegs));
|
---|
320 | Assert(cSegs < (~(unsigned)0 >> 1));
|
---|
321 |
|
---|
322 | pGcSgBuf->paSegs = paSegs;
|
---|
323 | pGcSgBuf->cSegs = (unsigned)cSegs;
|
---|
324 | pGcSgBuf->idxSeg = 0;
|
---|
325 | if (cSegs && paSegs)
|
---|
326 | {
|
---|
327 | pGcSgBuf->GCPhysCur = paSegs[0].GCPhys;
|
---|
328 | pGcSgBuf->cbSegLeft = paSegs[0].cbSeg;
|
---|
329 | }
|
---|
330 | else
|
---|
331 | {
|
---|
332 | pGcSgBuf->GCPhysCur = 0;
|
---|
333 | pGcSgBuf->cbSegLeft = 0;
|
---|
334 | }
|
---|
335 | }
|
---|
336 |
|
---|
337 | static RTGCPHYS virtioCoreSgBufGet(PVIRTIOSGBUF pGcSgBuf, size_t *pcbData)
|
---|
338 | {
|
---|
339 | size_t cbData;
|
---|
340 | RTGCPHYS pGcBuf;
|
---|
341 |
|
---|
342 | /* Check that the S/G buffer has memory left. */
|
---|
343 | if (RT_LIKELY(pGcSgBuf->idxSeg < pGcSgBuf->cSegs && pGcSgBuf->cbSegLeft))
|
---|
344 | { /* likely */ }
|
---|
345 | else
|
---|
346 | {
|
---|
347 | *pcbData = 0;
|
---|
348 | return 0;
|
---|
349 | }
|
---|
350 |
|
---|
351 | AssertMsg( pGcSgBuf->cbSegLeft <= 128 * _1M
|
---|
352 | && (RTGCPHYS)pGcSgBuf->GCPhysCur >= (RTGCPHYS)pGcSgBuf->paSegs[pGcSgBuf->idxSeg].GCPhys
|
---|
353 | && (RTGCPHYS)pGcSgBuf->GCPhysCur + pGcSgBuf->cbSegLeft <=
|
---|
354 | (RTGCPHYS)pGcSgBuf->paSegs[pGcSgBuf->idxSeg].GCPhys + pGcSgBuf->paSegs[pGcSgBuf->idxSeg].cbSeg,
|
---|
355 | ("pGcSgBuf->idxSeg=%d pGcSgBuf->cSegs=%d pGcSgBuf->GCPhysCur=%p pGcSgBuf->cbSegLeft=%zd "
|
---|
356 | "pGcSgBuf->paSegs[%d].GCPhys=%p pGcSgBuf->paSegs[%d].cbSeg=%zd\n",
|
---|
357 | pGcSgBuf->idxSeg, pGcSgBuf->cSegs, pGcSgBuf->GCPhysCur, pGcSgBuf->cbSegLeft,
|
---|
358 | pGcSgBuf->idxSeg, pGcSgBuf->paSegs[pGcSgBuf->idxSeg].GCPhys, pGcSgBuf->idxSeg,
|
---|
359 | pGcSgBuf->paSegs[pGcSgBuf->idxSeg].cbSeg));
|
---|
360 |
|
---|
361 | cbData = RT_MIN(*pcbData, pGcSgBuf->cbSegLeft);
|
---|
362 | pGcBuf = pGcSgBuf->GCPhysCur;
|
---|
363 | pGcSgBuf->cbSegLeft -= cbData;
|
---|
364 | if (!pGcSgBuf->cbSegLeft)
|
---|
365 | {
|
---|
366 | pGcSgBuf->idxSeg++;
|
---|
367 |
|
---|
368 | if (pGcSgBuf->idxSeg < pGcSgBuf->cSegs)
|
---|
369 | {
|
---|
370 | pGcSgBuf->GCPhysCur = pGcSgBuf->paSegs[pGcSgBuf->idxSeg].GCPhys;
|
---|
371 | pGcSgBuf->cbSegLeft = pGcSgBuf->paSegs[pGcSgBuf->idxSeg].cbSeg;
|
---|
372 | }
|
---|
373 | *pcbData = cbData;
|
---|
374 | }
|
---|
375 | else
|
---|
376 | pGcSgBuf->GCPhysCur = pGcSgBuf->GCPhysCur + cbData;
|
---|
377 |
|
---|
378 | return pGcBuf;
|
---|
379 | }
|
---|
380 |
|
---|
381 | void virtioCoreSgBufReset(PVIRTIOSGBUF pGcSgBuf)
|
---|
382 | {
|
---|
383 | AssertPtrReturnVoid(pGcSgBuf);
|
---|
384 |
|
---|
385 | pGcSgBuf->idxSeg = 0;
|
---|
386 | if (pGcSgBuf->cSegs)
|
---|
387 | {
|
---|
388 | pGcSgBuf->GCPhysCur = pGcSgBuf->paSegs[0].GCPhys;
|
---|
389 | pGcSgBuf->cbSegLeft = pGcSgBuf->paSegs[0].cbSeg;
|
---|
390 | }
|
---|
391 | else
|
---|
392 | {
|
---|
393 | pGcSgBuf->GCPhysCur = 0;
|
---|
394 | pGcSgBuf->cbSegLeft = 0;
|
---|
395 | }
|
---|
396 | }
|
---|
397 |
|
---|
398 | RTGCPHYS virtioCoreSgBufAdvance(PVIRTIOSGBUF pGcSgBuf, size_t cbAdvance)
|
---|
399 | {
|
---|
400 | AssertReturn(pGcSgBuf, 0);
|
---|
401 |
|
---|
402 | size_t cbLeft = cbAdvance;
|
---|
403 | while (cbLeft)
|
---|
404 | {
|
---|
405 | size_t cbThisAdvance = cbLeft;
|
---|
406 | virtioCoreSgBufGet(pGcSgBuf, &cbThisAdvance);
|
---|
407 | if (!cbThisAdvance)
|
---|
408 | break;
|
---|
409 |
|
---|
410 | cbLeft -= cbThisAdvance;
|
---|
411 | }
|
---|
412 | return cbAdvance - cbLeft;
|
---|
413 | }
|
---|
414 |
|
---|
415 | RTGCPHYS virtioCoreSgBufGetNextSegment(PVIRTIOSGBUF pGcSgBuf, size_t *pcbSeg)
|
---|
416 | {
|
---|
417 | AssertReturn(pGcSgBuf, 0);
|
---|
418 | AssertPtrReturn(pcbSeg, 0);
|
---|
419 |
|
---|
420 | if (!*pcbSeg)
|
---|
421 | *pcbSeg = pGcSgBuf->cbSegLeft;
|
---|
422 |
|
---|
423 | return virtioCoreSgBufGet(pGcSgBuf, pcbSeg);
|
---|
424 | }
|
---|
425 |
|
---|
426 | size_t virtioCoreSgBufCalcTotalLength(PVIRTIOSGBUF pGcSgBuf)
|
---|
427 | {
|
---|
428 | size_t cb = 0;
|
---|
429 | unsigned i = pGcSgBuf->cSegs;
|
---|
430 | while (i-- > 0)
|
---|
431 | cb += pGcSgBuf->paSegs[i].cbSeg;
|
---|
432 | return cb;
|
---|
433 | }
|
---|
434 |
|
---|
435 | #ifdef IN_RING3
|
---|
436 | void virtioCorePrintFeatures(VIRTIOCORE *pVirtio, PCDBGFINFOHLP pHlp)
|
---|
437 | {
|
---|
438 | static struct
|
---|
439 | {
|
---|
440 | uint64_t fFeatureBit;
|
---|
441 | const char *pcszDesc;
|
---|
442 | } const s_aFeatures[] =
|
---|
443 | {
|
---|
444 | { VIRTIO_F_RING_INDIRECT_DESC, " RING_INDIRECT_DESC Driver can use descriptors with VIRTQ_DESC_F_INDIRECT flag set\n" },
|
---|
445 | { VIRTIO_F_RING_EVENT_IDX, " RING_EVENT_IDX Enables use_event and avail_event fields described in 2.4.7, 2.4.8\n" },
|
---|
446 | { VIRTIO_F_VERSION_1, " VERSION Used to detect legacy drivers.\n" },
|
---|
447 | };
|
---|
448 |
|
---|
449 | #define MAXLINE 80
|
---|
450 | /* Display as a single buf to prevent interceding log messages */
|
---|
451 | uint16_t cbBuf = RT_ELEMENTS(s_aFeatures) * 132;
|
---|
452 | char *pszBuf = (char *)RTMemAllocZ(cbBuf);
|
---|
453 | Assert(pszBuf);
|
---|
454 | char *cp = pszBuf;
|
---|
455 | for (unsigned i = 0; i < RT_ELEMENTS(s_aFeatures); ++i)
|
---|
456 | {
|
---|
457 | bool isOffered = RT_BOOL(pVirtio->uDeviceFeatures & s_aFeatures[i].fFeatureBit);
|
---|
458 | bool isNegotiated = RT_BOOL(pVirtio->uDriverFeatures & s_aFeatures[i].fFeatureBit);
|
---|
459 | cp += RTStrPrintf(cp, cbBuf - (cp - pszBuf), " %s %s %s",
|
---|
460 | isOffered ? "+" : "-", isNegotiated ? "x" : " ", s_aFeatures[i].pcszDesc);
|
---|
461 | }
|
---|
462 | if (pHlp)
|
---|
463 | pHlp->pfnPrintf(pHlp, "VirtIO Core Features Configuration\n\n"
|
---|
464 | " Offered Accepted Feature Description\n"
|
---|
465 | " ------- -------- ------- -----------\n"
|
---|
466 | "%s\n", pszBuf);
|
---|
467 | #ifdef LOG_ENABLED
|
---|
468 | else
|
---|
469 | Log3(("VirtIO Core Features Configuration\n\n"
|
---|
470 | " Offered Accepted Feature Description\n"
|
---|
471 | " ------- -------- ------- -----------\n"
|
---|
472 | "%s\n", pszBuf));
|
---|
473 | #endif
|
---|
474 | RTMemFree(pszBuf);
|
---|
475 | }
|
---|
476 | #endif
|
---|
477 |
|
---|
478 | #ifdef LOG_ENABLED
|
---|
479 | /**
|
---|
480 | * Debug assist for consumer device code.
|
---|
481 | * Does a formatted hex dump using Log(()), recommend using VIRTIO_HEX_DUMP() macro to
|
---|
482 | * control enabling of logging efficiently.
|
---|
483 | *
|
---|
484 | * @param pv pointer to buffer to dump contents of
|
---|
485 | * @param cb count of characters to dump from buffer
|
---|
486 | * @param uBase base address of per-row address prefixing of hex output
|
---|
487 | * @param pszTitle Optional title. If present displays title that lists
|
---|
488 | * provided text with value of cb to indicate size next to it.
|
---|
489 | */
|
---|
490 | void virtioCoreHexDump(uint8_t *pv, uint32_t cb, uint32_t uBase, const char *pszTitle)
|
---|
491 | {
|
---|
492 | #define ADJCURSOR(cb) pszOut += cb; cbRemain -= cb;
|
---|
493 | size_t cbPrint = 0, cbRemain = ((cb / 16) + 1) * 80;
|
---|
494 | char *pszBuf = (char *)RTMemAllocZ(cbRemain), *pszOut = pszBuf;
|
---|
495 | AssertMsgReturnVoid(pszBuf, ("Out of Memory"));
|
---|
496 | if (pszTitle)
|
---|
497 | {
|
---|
498 | cbPrint = RTStrPrintf(pszOut, cbRemain, "%s [%d bytes]:\n", pszTitle, cb);
|
---|
499 | ADJCURSOR(cbPrint);
|
---|
500 | }
|
---|
501 | for (uint32_t row = 0; row < RT_MAX(1, (cb / 16) + 1) && row * 16 < cb; row++)
|
---|
502 | {
|
---|
503 | cbPrint = RTStrPrintf(pszOut, cbRemain, "%04x: ", row * 16 + uBase); /* line address */
|
---|
504 | ADJCURSOR(cbPrint);
|
---|
505 | for (uint8_t col = 0; col < 16; col++)
|
---|
506 | {
|
---|
507 | uint32_t idx = row * 16 + col;
|
---|
508 | if (idx >= cb)
|
---|
509 | cbPrint = RTStrPrintf(pszOut, cbRemain, "-- %s", (col + 1) % 8 ? "" : " ");
|
---|
510 | else
|
---|
511 | cbPrint = RTStrPrintf(pszOut, cbRemain, ("%02x %s", pv[idx], (col + 1) % 8 ? "" : " "));
|
---|
512 | ADJCURSOR(cbPrint);
|
---|
513 | }
|
---|
514 | for (uint32_t idx = row * 16; idx < row * 16 + 16; idx++)
|
---|
515 | {
|
---|
516 | cbPrint = RTStrPrintf(pszOut, cbRemain, "%c", (idx >= cb) ? ' ' : (pv[idx] >= 0x20 && pv[idx] <= 0x7e ? pv[idx] : '.'));
|
---|
517 | ADJCURSOR(cbPrint);
|
---|
518 | }
|
---|
519 | *pszOut++ = '\n';
|
---|
520 | --cbRemain;
|
---|
521 | }
|
---|
522 | Log(("%s\n", pszBuf));
|
---|
523 | RTMemFree(pszBuf);
|
---|
524 | RT_NOREF2(uBase, pv);
|
---|
525 | #undef ADJCURSOR
|
---|
526 | }
|
---|
527 |
|
---|
528 | /**
|
---|
529 | * Debug assist for consumer device code.
|
---|
530 | * Do a hex dump of memory in guest physical context
|
---|
531 | *
|
---|
532 | * @param GCPhys pointer to buffer to dump contents of
|
---|
533 | * @param cb count of characters to dump from buffer
|
---|
534 | * @param uBase base address of per-row address prefixing of hex output
|
---|
535 | * @param pszTitle Optional title. If present displays title that lists
|
---|
536 | * provided text with value of cb to indicate size next to it.
|
---|
537 | */
|
---|
538 | void virtioCoreGCPhysHexDump(PPDMDEVINS pDevIns, RTGCPHYS GCPhys, uint16_t cb, uint32_t uBase, const char *pszTitle)
|
---|
539 | {
|
---|
540 | #define ADJCURSOR(cb) pszOut += cb; cbRemain -= cb;
|
---|
541 | size_t cbPrint = 0, cbRemain = ((cb / 16) + 1) * 80;
|
---|
542 | char *pszBuf = (char *)RTMemAllocZ(cbRemain), *pszOut = pszBuf;
|
---|
543 | AssertMsgReturnVoid(pszBuf, ("Out of Memory"));
|
---|
544 | if (pszTitle)
|
---|
545 | {
|
---|
546 | cbPrint = RTStrPrintf(pszOut, cbRemain, "%s [%d bytes]:\n", pszTitle, cb);
|
---|
547 | ADJCURSOR(cbPrint);
|
---|
548 | }
|
---|
549 | for (uint16_t row = 0; row < (uint16_t)RT_MAX(1, (cb / 16) + 1) && row * 16 < cb; row++)
|
---|
550 | {
|
---|
551 | uint8_t c;
|
---|
552 | cbPrint = RTStrPrintf(pszOut, cbRemain, "%04x: ", row * 16 + uBase); /* line address */
|
---|
553 | ADJCURSOR(cbPrint);
|
---|
554 | for (uint8_t col = 0; col < 16; col++)
|
---|
555 | {
|
---|
556 | uint32_t idx = row * 16 + col;
|
---|
557 | PDMDevHlpPCIPhysRead(pDevIns, GCPhys + idx, &c, 1);
|
---|
558 | if (idx >= cb)
|
---|
559 | cbPrint = RTStrPrintf(pszOut, cbRemain, "-- %s", (col + 1) % 8 ? "" : " ");
|
---|
560 | else
|
---|
561 | cbPrint = RTStrPrintf(pszOut, cbRemain, "%02x %s", c, (col + 1) % 8 ? "" : " ");
|
---|
562 | ADJCURSOR(cbPrint);
|
---|
563 | }
|
---|
564 | for (uint16_t idx = row * 16; idx < row * 16 + 16; idx++)
|
---|
565 | {
|
---|
566 | PDMDevHlpPCIPhysRead(pDevIns, GCPhys + idx, &c, 1);
|
---|
567 | cbPrint = RTStrPrintf(pszOut, cbRemain, "%c", (idx >= cb) ? ' ' : (c >= 0x20 && c <= 0x7e ? c : '.'));
|
---|
568 | ADJCURSOR(cbPrint);
|
---|
569 | }
|
---|
570 | *pszOut++ = '\n';
|
---|
571 | --cbRemain;
|
---|
572 | }
|
---|
573 | Log(("%s\n", pszBuf));
|
---|
574 | RTMemFree(pszBuf);
|
---|
575 | RT_NOREF(uBase);
|
---|
576 | #undef ADJCURSOR
|
---|
577 | }
|
---|
578 | #endif /* LOG_ENABLED */
|
---|
579 |
|
---|
580 | /**
|
---|
581 | * Log memory-mapped I/O input or output value.
|
---|
582 | *
|
---|
583 | * This is designed to be invoked by macros that can make contextual assumptions
|
---|
584 | * (e.g. implicitly derive MACRO parameters from the invoking function). It is exposed
|
---|
585 | * for the VirtIO client doing the device-specific implementation in order to log in a
|
---|
586 | * similar fashion accesses to the device-specific MMIO configuration structure. Macros
|
---|
587 | * that leverage this function are found in virtioCommonCfgAccessed() and can be
|
---|
588 | * used as an example of how to use this effectively for the device-specific
|
---|
589 | * code.
|
---|
590 | *
|
---|
591 | * @param pszFunc To avoid displaying this function's name via __FUNCTION__ or LogFunc()
|
---|
592 | * @param pszMember Name of struct member
|
---|
593 | * @param pv pointer to value
|
---|
594 | * @param cb size of value
|
---|
595 | * @param uOffset offset into member where value starts
|
---|
596 | * @param fWrite True if write I/O
|
---|
597 | * @param fHasIndex True if the member is indexed
|
---|
598 | * @param idx The index if fHasIndex
|
---|
599 | */
|
---|
600 | void virtioCoreLogMappedIoValue(const char *pszFunc, const char *pszMember, uint32_t uMemberSize,
|
---|
601 | const void *pv, uint32_t cb, uint32_t uOffset, int fWrite,
|
---|
602 | int fHasIndex, uint32_t idx)
|
---|
603 | {
|
---|
604 | if (!LogIs6Enabled())
|
---|
605 | return;
|
---|
606 |
|
---|
607 | char szIdx[16];
|
---|
608 | if (fHasIndex)
|
---|
609 | RTStrPrintf(szIdx, sizeof(szIdx), "[%d]", idx);
|
---|
610 | else
|
---|
611 | szIdx[0] = '\0';
|
---|
612 |
|
---|
613 | if (cb == 1 || cb == 2 || cb == 4 || cb == 8)
|
---|
614 | {
|
---|
615 | char szDepiction[64];
|
---|
616 | size_t cchDepiction;
|
---|
617 | if (uOffset != 0 || cb != uMemberSize) /* display bounds if partial member access */
|
---|
618 | cchDepiction = RTStrPrintf(szDepiction, sizeof(szDepiction), "%s%s[%d:%d]",
|
---|
619 | pszMember, szIdx, uOffset, uOffset + cb - 1);
|
---|
620 | else
|
---|
621 | cchDepiction = RTStrPrintf(szDepiction, sizeof(szDepiction), "%s%s", pszMember, szIdx);
|
---|
622 |
|
---|
623 | /* padding */
|
---|
624 | if (cchDepiction < 30)
|
---|
625 | szDepiction[cchDepiction++] = ' ';
|
---|
626 | while (cchDepiction < 30)
|
---|
627 | szDepiction[cchDepiction++] = '.';
|
---|
628 | szDepiction[cchDepiction] = '\0';
|
---|
629 |
|
---|
630 | RTUINT64U uValue;
|
---|
631 | uValue.u = 0;
|
---|
632 | memcpy(uValue.au8, pv, cb);
|
---|
633 | Log6(("%s: Guest %s %s %#0*RX64\n",
|
---|
634 | pszFunc, fWrite ? "wrote" : "read ", szDepiction, 2 + cb * 2, uValue.u));
|
---|
635 | }
|
---|
636 | else /* odd number or oversized access, ... log inline hex-dump style */
|
---|
637 | {
|
---|
638 | Log6(("%s: Guest %s %s%s[%d:%d]: %.*Rhxs\n",
|
---|
639 | pszFunc, fWrite ? "wrote" : "read ", pszMember,
|
---|
640 | szIdx, uOffset, uOffset + cb, cb, pv));
|
---|
641 | }
|
---|
642 | RT_NOREF2(fWrite, pszFunc);
|
---|
643 | }
|
---|
644 |
|
---|
645 |
|
---|
646 | /**
|
---|
647 | * Makes the MMIO-mapped Virtio uDeviceStatus registers non-cryptic
|
---|
648 | */
|
---|
649 | DECLINLINE(void) virtioLogDeviceStatus(uint8_t bStatus)
|
---|
650 | {
|
---|
651 | if (bStatus == 0)
|
---|
652 | Log6(("RESET"));
|
---|
653 | else
|
---|
654 | {
|
---|
655 | int primed = 0;
|
---|
656 | if (bStatus & VIRTIO_STATUS_ACKNOWLEDGE)
|
---|
657 | Log6(("%sACKNOWLEDGE", primed++ ? "" : ""));
|
---|
658 | if (bStatus & VIRTIO_STATUS_DRIVER)
|
---|
659 | Log6(("%sDRIVER", primed++ ? " | " : ""));
|
---|
660 | if (bStatus & VIRTIO_STATUS_FEATURES_OK)
|
---|
661 | Log6(("%sFEATURES_OK", primed++ ? " | " : ""));
|
---|
662 | if (bStatus & VIRTIO_STATUS_DRIVER_OK)
|
---|
663 | Log6(("%sDRIVER_OK", primed++ ? " | " : ""));
|
---|
664 | if (bStatus & VIRTIO_STATUS_FAILED)
|
---|
665 | Log6(("%sFAILED", primed++ ? " | " : ""));
|
---|
666 | if (bStatus & VIRTIO_STATUS_DEVICE_NEEDS_RESET)
|
---|
667 | Log6(("%sNEEDS_RESET", primed++ ? " | " : ""));
|
---|
668 | (void)primed;
|
---|
669 | }
|
---|
670 | }
|
---|
671 |
|
---|
672 | #ifdef IN_RING3
|
---|
673 |
|
---|
674 | void virtioCoreR3Info(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
|
---|
675 | {
|
---|
676 | RT_NOREF(pDevIns);
|
---|
677 | bool fNone = pszArgs && *pszArgs == '\0';
|
---|
678 | bool fAll = pszArgs && (*pszArgs == 'a' || *pszArgs == 'A'); /* "all" */
|
---|
679 | bool fBasic = pszArgs && (*pszArgs == 'b' || *pszArgs == 'B'); /* "basic" */
|
---|
680 | bool fState = pszArgs && (*pszArgs == 's' || *pszArgs == 'S'); /* "state" */
|
---|
681 | bool fPointers = pszArgs && (*pszArgs == 'p' || *pszArgs == 'P'); /* "pointers" */
|
---|
682 | bool fQueues = pszArgs && (*pszArgs == 'q' || *pszArgs == 'Q'); /* "queues */
|
---|
683 | RT_NOREF6(fNone, fAll, fBasic, fState, fPointers, fQueues);
|
---|
684 | pHlp->pfnPrintf(pHlp, "");
|
---|
685 |
|
---|
686 | }
|
---|
687 |
|
---|
688 | void virtioCoreR3QueueInfo(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs, int idxQueue)
|
---|
689 | {
|
---|
690 | RT_NOREF(pszArgs);
|
---|
691 | PVIRTIOCORE pVirtio = PDMDEVINS_2_DATA(pDevIns, PVIRTIOCORE);
|
---|
692 | PVIRTQSTATE pVirtq = &pVirtio->virtqState[idxQueue];
|
---|
693 |
|
---|
694 | // bool fDump = pszArgs && (*pszArgs == 'd' || *pszArgs == 'D'); /* "dump" (avail phys descriptor)" */
|
---|
695 |
|
---|
696 | uint16_t uAvailIdx = virtioReadAvailRingIdx(pDevIns, pVirtio, idxQueue);
|
---|
697 | uint16_t uAvailIdxShadow = pVirtq->uAvailIdxShadow;
|
---|
698 |
|
---|
699 | uint16_t uUsedIdx = virtioReadUsedRingIdx(pDevIns, pVirtio, idxQueue);
|
---|
700 | uint16_t uUsedIdxShadow = pVirtq->uUsedIdxShadow;
|
---|
701 |
|
---|
702 | PVIRTIO_DESC_CHAIN_T pDescChain = NULL;
|
---|
703 |
|
---|
704 | bool fEmpty = IS_VIRTQ_EMPTY(pDevIns, pVirtio, pVirtq);
|
---|
705 |
|
---|
706 | LogFunc(("%s, empty = %s\n", VIRTQNAME(pVirtio, idxQueue), fEmpty ? "true" : "false"));
|
---|
707 |
|
---|
708 | int cSendSegs = 0, cReturnSegs = 0;
|
---|
709 | if (!fEmpty)
|
---|
710 | {
|
---|
711 | virtioCoreR3QueuePeek(pDevIns, pVirtio, idxQueue, &pDescChain);
|
---|
712 | cSendSegs = pDescChain->pSgPhysSend ? pDescChain->pSgPhysSend->cSegs : 0;
|
---|
713 | cReturnSegs = pDescChain->pSgPhysReturn ? pDescChain->pSgPhysReturn->cSegs : 0;
|
---|
714 | }
|
---|
715 |
|
---|
716 | bool fAvailNoInterrupt = virtioReadAvailRingFlags(pDevIns, pVirtio, idxQueue) & VIRTQ_AVAIL_F_NO_INTERRUPT;
|
---|
717 | bool fUsedNoNotify = virtioReadUsedRingFlags(pDevIns, pVirtio, idxQueue) & VIRTQ_USED_F_NO_NOTIFY;
|
---|
718 |
|
---|
719 |
|
---|
720 | pHlp->pfnPrintf(pHlp, " queue enabled: ........... %s\n", pVirtio->uQueueEnable[idxQueue] ? "true" : "false");
|
---|
721 | pHlp->pfnPrintf(pHlp, " size: .................... %d\n", pVirtio->uQueueSize[idxQueue]);
|
---|
722 | pHlp->pfnPrintf(pHlp, " notify offset: ........... %d\n", pVirtio->uQueueNotifyOff[idxQueue]);
|
---|
723 | if (pVirtio->fMsiSupport)
|
---|
724 | pHlp->pfnPrintf(pHlp, " MSIX vector: ....... %4.4x\n", pVirtio->uQueueMsixVector[idxQueue]);
|
---|
725 | pHlp->pfnPrintf(pHlp, "\n");
|
---|
726 | pHlp->pfnPrintf(pHlp, " avail ring (%d entries):\n", uAvailIdx - uAvailIdxShadow);
|
---|
727 | pHlp->pfnPrintf(pHlp, " index: ................ %d\n", uAvailIdx);
|
---|
728 | pHlp->pfnPrintf(pHlp, " shadow: ............... %d\n", uAvailIdxShadow);
|
---|
729 | pHlp->pfnPrintf(pHlp, " flags: ................ %s\n", fAvailNoInterrupt ? "NO_INTERRUPT" : "");
|
---|
730 | pHlp->pfnPrintf(pHlp, "\n");
|
---|
731 | pHlp->pfnPrintf(pHlp, " used ring (%d entries):\n", uUsedIdx - uUsedIdxShadow);
|
---|
732 | pHlp->pfnPrintf(pHlp, " index: ................ %d\n", uUsedIdx);
|
---|
733 | pHlp->pfnPrintf(pHlp, " shadow: ............... %d\n", uUsedIdxShadow);
|
---|
734 | pHlp->pfnPrintf(pHlp, " flags: ................ %s\n", fUsedNoNotify ? "NO_NOTIFY" : "");
|
---|
735 | pHlp->pfnPrintf(pHlp, "\n");
|
---|
736 | if (!fEmpty)
|
---|
737 | {
|
---|
738 | pHlp->pfnPrintf(pHlp, " desc chain:\n");
|
---|
739 | pHlp->pfnPrintf(pHlp, " head idx: ............. %d\n", uUsedIdx);
|
---|
740 | pHlp->pfnPrintf(pHlp, " segs: ................. %d\n", cSendSegs + cReturnSegs);
|
---|
741 | pHlp->pfnPrintf(pHlp, " refCnt ................ %d\n", pDescChain->cRefs);
|
---|
742 | pHlp->pfnPrintf(pHlp, "\n");
|
---|
743 | pHlp->pfnPrintf(pHlp, " host-to-guest (%d bytes):\n", pDescChain->cbPhysSend);
|
---|
744 | pHlp->pfnPrintf(pHlp, " segs: .............. %d\n", cSendSegs);
|
---|
745 | if (cSendSegs)
|
---|
746 | {
|
---|
747 | pHlp->pfnPrintf(pHlp, " index: ............. %d\n", pDescChain->pSgPhysSend->idxSeg);
|
---|
748 | pHlp->pfnPrintf(pHlp, " unsent ............. %d\n", pDescChain->pSgPhysSend->cbSegLeft);
|
---|
749 | }
|
---|
750 | pHlp->pfnPrintf(pHlp, "\n");
|
---|
751 | pHlp->pfnPrintf(pHlp, " guest-to-host (%d bytes)\n", pDescChain->cbPhysReturn);
|
---|
752 | pHlp->pfnPrintf(pHlp, " segs: .............. %d\n", cReturnSegs);
|
---|
753 | if (cReturnSegs)
|
---|
754 | {
|
---|
755 | pHlp->pfnPrintf(pHlp, " index: ............. %d\n", pDescChain->pSgPhysReturn->idxSeg);
|
---|
756 | pHlp->pfnPrintf(pHlp, " unsent ............. %d\n", pDescChain->pSgPhysReturn->cbSegLeft);
|
---|
757 | }
|
---|
758 | } else
|
---|
759 | pHlp->pfnPrintf(pHlp, " No desc chains available\n");
|
---|
760 | pHlp->pfnPrintf(pHlp, "\n");
|
---|
761 |
|
---|
762 | }
|
---|
763 |
|
---|
764 | /**
|
---|
765 | * Allocate client context for client to work with VirtIO-#provided with queue
|
---|
766 | *
|
---|
767 | * @param pVirtio Pointer to the shared virtio state.
|
---|
768 | * @param idxQueue Queue number
|
---|
769 | * @param pcszName Name to give queue
|
---|
770 | *
|
---|
771 | * @returns VBox status code.
|
---|
772 | */
|
---|
773 | int virtioCoreR3QueueAttach(PVIRTIOCORE pVirtio, uint16_t idxQueue, const char *pcszName)
|
---|
774 | {
|
---|
775 | LogFunc(("%s\n", pcszName));
|
---|
776 | PVIRTQSTATE pVirtq = &pVirtio->virtqState[idxQueue];
|
---|
777 | pVirtq->idxQueue = idxQueue;
|
---|
778 | pVirtq->uAvailIdxShadow = 0;
|
---|
779 | pVirtq->uUsedIdxShadow = 0;
|
---|
780 | pVirtq->fVirtqRingEventThreshold = false;
|
---|
781 | RTStrCopy(pVirtq->szVirtqName, sizeof(pVirtq->szVirtqName), pcszName);
|
---|
782 | return VINF_SUCCESS;
|
---|
783 | }
|
---|
784 | #endif /* IN_RING3 */
|
---|
785 |
|
---|
786 |
|
---|
787 | #ifdef IN_RING3
|
---|
788 |
|
---|
789 | int virtioCoreR3DescChainGet(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t idxQueue,
|
---|
790 | uint16_t uHeadIdx, PPVIRTIO_DESC_CHAIN_T ppDescChain)
|
---|
791 | {
|
---|
792 | AssertReturn(ppDescChain, VERR_INVALID_POINTER);
|
---|
793 | *ppDescChain = NULL;
|
---|
794 |
|
---|
795 | Assert(idxQueue < RT_ELEMENTS(pVirtio->virtqState));
|
---|
796 |
|
---|
797 | PVIRTQSTATE pVirtq = &pVirtio->virtqState[idxQueue];
|
---|
798 |
|
---|
799 | AssertMsgReturn(IS_DRIVER_OK(pVirtio) && pVirtio->uQueueEnable[idxQueue],
|
---|
800 | ("Guest driver not in ready state.\n"), VERR_INVALID_STATE);
|
---|
801 |
|
---|
802 | uint16_t uDescIdx = uHeadIdx;
|
---|
803 |
|
---|
804 | Log6Func(("%s DESC CHAIN: (head) desc_idx=%u\n", pVirtq->szVirtqName, uHeadIdx));
|
---|
805 | RT_NOREF(pVirtq);
|
---|
806 |
|
---|
807 | /*
|
---|
808 | * Allocate and initialize the descriptor chain structure.
|
---|
809 | */
|
---|
810 | PVIRTIO_DESC_CHAIN_T pDescChain = (PVIRTIO_DESC_CHAIN_T)RTMemAllocZ(sizeof(VIRTIO_DESC_CHAIN_T));
|
---|
811 | AssertReturn(pDescChain, VERR_NO_MEMORY);
|
---|
812 | pDescChain->u32Magic = VIRTIO_DESC_CHAIN_MAGIC;
|
---|
813 | pDescChain->cRefs = 1;
|
---|
814 | pDescChain->uHeadIdx = uHeadIdx;
|
---|
815 | *ppDescChain = pDescChain;
|
---|
816 |
|
---|
817 | /*
|
---|
818 | * Gather segments.
|
---|
819 | */
|
---|
820 | VIRTQ_DESC_T desc;
|
---|
821 |
|
---|
822 | uint32_t cbIn = 0;
|
---|
823 | uint32_t cbOut = 0;
|
---|
824 | uint32_t cSegsIn = 0;
|
---|
825 | uint32_t cSegsOut = 0;
|
---|
826 | PVIRTIOSGSEG paSegsIn = pDescChain->aSegsIn;
|
---|
827 | PVIRTIOSGSEG paSegsOut = pDescChain->aSegsOut;
|
---|
828 |
|
---|
829 | do
|
---|
830 | {
|
---|
831 | PVIRTIOSGSEG pSeg;
|
---|
832 |
|
---|
833 | /*
|
---|
834 | * Malicious guests may go beyond paSegsIn or paSegsOut boundaries by linking
|
---|
835 | * several descriptors into a loop. Since there is no legitimate way to get a sequences of
|
---|
836 | * linked descriptors exceeding the total number of descriptors in the ring (see @bugref{8620}),
|
---|
837 | * the following aborts I/O if breach and employs a simple log throttling algorithm to notify.
|
---|
838 | */
|
---|
839 | if (cSegsIn + cSegsOut >= VIRTQ_MAX_ENTRIES)
|
---|
840 | {
|
---|
841 | static volatile uint32_t s_cMessages = 0;
|
---|
842 | static volatile uint32_t s_cThreshold = 1;
|
---|
843 | if (ASMAtomicIncU32(&s_cMessages) == ASMAtomicReadU32(&s_cThreshold))
|
---|
844 | {
|
---|
845 | LogRelMax(64, ("Too many linked descriptors; check if the guest arranges descriptors in a loop.\n"));
|
---|
846 | if (ASMAtomicReadU32(&s_cMessages) != 1)
|
---|
847 | LogRelMax(64, ("(the above error has occured %u times so far)\n", ASMAtomicReadU32(&s_cMessages)));
|
---|
848 | ASMAtomicWriteU32(&s_cThreshold, ASMAtomicReadU32(&s_cThreshold) * 10);
|
---|
849 | }
|
---|
850 | break;
|
---|
851 | }
|
---|
852 | RT_UNTRUSTED_VALIDATED_FENCE();
|
---|
853 |
|
---|
854 | virtioReadDesc(pDevIns, pVirtio, idxQueue, uDescIdx, &desc);
|
---|
855 |
|
---|
856 | if (desc.fFlags & VIRTQ_DESC_F_WRITE)
|
---|
857 | {
|
---|
858 | Log6Func(("%s IN desc_idx=%u seg=%u addr=%RGp cb=%u\n", VIRTQNAME(pVirtio, idxQueue), uDescIdx, cSegsIn, desc.GCPhysBuf, desc.cb));
|
---|
859 | cbIn += desc.cb;
|
---|
860 | pSeg = &paSegsIn[cSegsIn++];
|
---|
861 | }
|
---|
862 | else
|
---|
863 | {
|
---|
864 | Log6Func(("%s OUT desc_idx=%u seg=%u addr=%RGp cb=%u\n", VIRTQNAME(pVirtio, idxQueue), uDescIdx, cSegsOut, desc.GCPhysBuf, desc.cb));
|
---|
865 | cbOut += desc.cb;
|
---|
866 | pSeg = &paSegsOut[cSegsOut++];
|
---|
867 | if (LogIs11Enabled())
|
---|
868 | {
|
---|
869 | virtioCoreGCPhysHexDump(pDevIns, desc.GCPhysBuf, desc.cb, 0, NULL);
|
---|
870 | Log(("\n"));
|
---|
871 | }
|
---|
872 | }
|
---|
873 |
|
---|
874 | pSeg->GCPhys = desc.GCPhysBuf;
|
---|
875 | pSeg->cbSeg = desc.cb;
|
---|
876 |
|
---|
877 | uDescIdx = desc.uDescIdxNext;
|
---|
878 | } while (desc.fFlags & VIRTQ_DESC_F_NEXT);
|
---|
879 |
|
---|
880 | /*
|
---|
881 | * Add segments to the descriptor chain structure.
|
---|
882 | */
|
---|
883 | if (cSegsIn)
|
---|
884 | {
|
---|
885 | virtioCoreSgBufInit(&pDescChain->SgBufIn, paSegsIn, cSegsIn);
|
---|
886 | pDescChain->pSgPhysReturn = &pDescChain->SgBufIn;
|
---|
887 | pDescChain->cbPhysReturn = cbIn;
|
---|
888 | STAM_REL_COUNTER_ADD(&pVirtio->StatDescChainsSegsIn, cSegsIn);
|
---|
889 | }
|
---|
890 |
|
---|
891 | if (cSegsOut)
|
---|
892 | {
|
---|
893 | virtioCoreSgBufInit(&pDescChain->SgBufOut, paSegsOut, cSegsOut);
|
---|
894 | pDescChain->pSgPhysSend = &pDescChain->SgBufOut;
|
---|
895 | pDescChain->cbPhysSend = cbOut;
|
---|
896 | STAM_REL_COUNTER_ADD(&pVirtio->StatDescChainsSegsOut, cSegsOut);
|
---|
897 | }
|
---|
898 |
|
---|
899 | STAM_REL_COUNTER_INC(&pVirtio->StatDescChainsAllocated);
|
---|
900 | Log6Func(("%s -- segs OUT: %u (%u bytes) IN: %u (%u bytes) --\n", pVirtq->szVirtqName, cSegsOut, cbOut, cSegsIn, cbIn));
|
---|
901 |
|
---|
902 | return VINF_SUCCESS;
|
---|
903 | }
|
---|
904 |
|
---|
905 |
|
---|
906 | /**
|
---|
907 | * Retains a reference to the given descriptor chain.
|
---|
908 | *
|
---|
909 | * @returns New reference count.
|
---|
910 | * @retval UINT32_MAX on invalid parameter.
|
---|
911 | * @param pDescChain The descriptor chain to reference.
|
---|
912 | */
|
---|
913 | uint32_t virtioCoreR3DescChainRetain(PVIRTIO_DESC_CHAIN_T pDescChain)
|
---|
914 | {
|
---|
915 | AssertReturn(pDescChain, UINT32_MAX);
|
---|
916 | AssertReturn(pDescChain->u32Magic == VIRTIO_DESC_CHAIN_MAGIC, UINT32_MAX);
|
---|
917 | uint32_t cRefs = ASMAtomicIncU32(&pDescChain->cRefs);
|
---|
918 | Assert(cRefs > 1);
|
---|
919 | Assert(cRefs < 16);
|
---|
920 | return cRefs;
|
---|
921 | }
|
---|
922 |
|
---|
923 |
|
---|
924 | /**
|
---|
925 | * Releases a reference to the given descriptor chain.
|
---|
926 | *
|
---|
927 | * @returns New reference count.
|
---|
928 | * @retval 0 if freed or invalid parameter.
|
---|
929 | * @param pVirtio Pointer to the shared virtio state.
|
---|
930 | * @param pDescChain The descriptor chain to reference. NULL is quietly
|
---|
931 | * ignored (returns 0).
|
---|
932 | */
|
---|
933 | uint32_t virtioCoreR3DescChainRelease(PVIRTIOCORE pVirtio, PVIRTIO_DESC_CHAIN_T pDescChain)
|
---|
934 | {
|
---|
935 | if (!pDescChain)
|
---|
936 | return 0;
|
---|
937 | AssertReturn(pDescChain, 0);
|
---|
938 | AssertReturn(pDescChain->u32Magic == VIRTIO_DESC_CHAIN_MAGIC, 0);
|
---|
939 | uint32_t cRefs = ASMAtomicDecU32(&pDescChain->cRefs);
|
---|
940 | Assert(cRefs < 16);
|
---|
941 | if (cRefs == 0)
|
---|
942 | {
|
---|
943 | pDescChain->u32Magic = ~VIRTIO_DESC_CHAIN_MAGIC;
|
---|
944 | RTMemFree(pDescChain);
|
---|
945 | STAM_REL_COUNTER_INC(&pVirtio->StatDescChainsFreed);
|
---|
946 | }
|
---|
947 | return cRefs;
|
---|
948 | }
|
---|
949 |
|
---|
950 |
|
---|
951 | /*
|
---|
952 | * Notifies guest (via ISR or MSI-X) of device configuration change
|
---|
953 | *
|
---|
954 | * @param pVirtio Pointer to the shared virtio state.
|
---|
955 | */
|
---|
956 | void virtioCoreNotifyConfigChanged(PVIRTIOCORE pVirtio)
|
---|
957 | {
|
---|
958 | virtioKick(pVirtio->pDevInsR3, pVirtio, VIRTIO_ISR_DEVICE_CONFIG, pVirtio->uMsixConfig);
|
---|
959 | }
|
---|
960 |
|
---|
961 | /**
|
---|
962 | * Enable or Disable notification for the specified queue
|
---|
963 | *
|
---|
964 | * @param pVirtio Pointer to the shared virtio state.
|
---|
965 | * @param idxQueue Queue number
|
---|
966 | * @param fEnable Selects notification mode (enabled or disabled)
|
---|
967 | */
|
---|
968 | void virtioCoreQueueNotifyEnable(PVIRTIOCORE pVirtio, uint16_t idxQueue, bool fEnable)
|
---|
969 | {
|
---|
970 | if (pVirtio->uDeviceStatus & VIRTIO_STATUS_DRIVER_OK)
|
---|
971 | {
|
---|
972 | uint16_t fFlags = virtioReadUsedRingFlags(pVirtio->pDevInsR3, pVirtio, idxQueue);
|
---|
973 |
|
---|
974 | if (fEnable)
|
---|
975 | fFlags &= ~ VIRTQ_USED_F_NO_NOTIFY;
|
---|
976 | else
|
---|
977 | fFlags |= VIRTQ_USED_F_NO_NOTIFY;
|
---|
978 |
|
---|
979 | virtioWriteUsedRingFlags(pVirtio->pDevInsR3, pVirtio, idxQueue, fFlags);
|
---|
980 | }
|
---|
981 | }
|
---|
982 |
|
---|
983 | /**
|
---|
984 | * Initiate orderly reset procedure. This is an exposed API for clients that might need it.
|
---|
985 | * Invoked by client to reset the device and driver (see VirtIO 1.0 section 2.1.1/2.1.2)
|
---|
986 | *
|
---|
987 | * @param pVirtio Pointer to the virtio state.
|
---|
988 | */
|
---|
989 | void virtioCoreResetAll(PVIRTIOCORE pVirtio)
|
---|
990 | {
|
---|
991 | LogFunc(("\n"));
|
---|
992 | pVirtio->uDeviceStatus |= VIRTIO_STATUS_DEVICE_NEEDS_RESET;
|
---|
993 | if (pVirtio->uDeviceStatus & VIRTIO_STATUS_DRIVER_OK)
|
---|
994 | {
|
---|
995 | pVirtio->fGenUpdatePending = true;
|
---|
996 | virtioKick(pVirtio->pDevInsR3, pVirtio, VIRTIO_ISR_DEVICE_CONFIG, pVirtio->uMsixConfig);
|
---|
997 | }
|
---|
998 | }
|
---|
999 |
|
---|
1000 |
|
---|
1001 |
|
---|
1002 | /**
|
---|
1003 | * Fetches descriptor chain using avail ring of indicated queue and converts the descriptor
|
---|
1004 | * chain into its OUT (to device) and IN to guest components, but does NOT remove it from
|
---|
1005 | * the 'avail' queue. I.e. doesn't advance the index. This can be used with virtioQueueSkip(),
|
---|
1006 | * which *does* advance the avail index. Together they facilitate a mechanism that allows
|
---|
1007 | * work with a queue element (descriptor chain) to be aborted if necessary, by not advancing
|
---|
1008 | * the pointer, or, upon success calling the skip function (above) to move to the next element.
|
---|
1009 | *
|
---|
1010 | * Additionally it converts the OUT desc chain data to a contiguous virtual
|
---|
1011 | * memory buffer for easy consumption by the caller. The caller must return the
|
---|
1012 | * descriptor chain pointer via virtioCoreR3QueuePut() and then call virtioCoreQueueSync()
|
---|
1013 | * at some point to return the data to the guest and complete the transaction.
|
---|
1014 | *
|
---|
1015 | * @param pDevIns The device instance.
|
---|
1016 | * @param pVirtio Pointer to the shared virtio state.
|
---|
1017 | * @param idxQueue Queue number
|
---|
1018 | * @param ppDescChain Address to store pointer to descriptor chain that contains the
|
---|
1019 | * pre-processed transaction information pulled from the virtq.
|
---|
1020 | *
|
---|
1021 | * @returns VBox status code:
|
---|
1022 | * @retval VINF_SUCCESS Success
|
---|
1023 | * @retval VERR_INVALID_STATE VirtIO not in ready state (asserted).
|
---|
1024 | * @retval VERR_NOT_AVAILABLE If the queue is empty.
|
---|
1025 | */
|
---|
1026 |
|
---|
1027 | int virtioCoreR3QueuePeek(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t idxQueue,
|
---|
1028 | PPVIRTIO_DESC_CHAIN_T ppDescChain)
|
---|
1029 | {
|
---|
1030 | return virtioCoreR3QueueGet(pDevIns, pVirtio, idxQueue, ppDescChain, false);
|
---|
1031 | }
|
---|
1032 |
|
---|
1033 | /**
|
---|
1034 | * Skip the next entry in the specified queue (typically used with virtioCoreR3QueuePeek())
|
---|
1035 | *
|
---|
1036 | * @param pVirtio Pointer to the virtio state.
|
---|
1037 | * @param idxQueue Index of queue
|
---|
1038 | */
|
---|
1039 | int virtioCoreR3QueueSkip(PVIRTIOCORE pVirtio, uint16_t idxQueue)
|
---|
1040 | {
|
---|
1041 | Assert(idxQueue < RT_ELEMENTS(pVirtio->virtqState));
|
---|
1042 | PVIRTQSTATE pVirtq = &pVirtio->virtqState[idxQueue];
|
---|
1043 |
|
---|
1044 | AssertMsgReturn(IS_DRIVER_OK(pVirtio) && pVirtio->uQueueEnable[idxQueue],
|
---|
1045 | ("Guest driver not in ready state.\n"), VERR_INVALID_STATE);
|
---|
1046 |
|
---|
1047 | if (IS_VIRTQ_EMPTY(pVirtio->pDevInsR3, pVirtio, pVirtq))
|
---|
1048 | return VERR_NOT_AVAILABLE;
|
---|
1049 |
|
---|
1050 | Log6Func(("%s avail shadow idx: %u\n", pVirtq->szVirtqName, pVirtq->uAvailIdxShadow));
|
---|
1051 | pVirtq->uAvailIdxShadow++;
|
---|
1052 |
|
---|
1053 | return VINF_SUCCESS;
|
---|
1054 | }
|
---|
1055 |
|
---|
1056 | /**
|
---|
1057 | * Fetches descriptor chain using avail ring of indicated queue and converts the descriptor
|
---|
1058 | * chain into its OUT (to device) and IN to guest components.
|
---|
1059 | *
|
---|
1060 | * Additionally it converts the OUT desc chain data to a contiguous virtual
|
---|
1061 | * memory buffer for easy consumption by the caller. The caller must return the
|
---|
1062 | * descriptor chain pointer via virtioCoreR3QueuePut() and then call virtioCoreQueueSync()
|
---|
1063 | * at some point to return the data to the guest and complete the transaction.
|
---|
1064 | *
|
---|
1065 | * @param pDevIns The device instance.
|
---|
1066 | * @param pVirtio Pointer to the shared virtio state.
|
---|
1067 | * @param idxQueue Queue number
|
---|
1068 | * @param ppDescChain Address to store pointer to descriptor chain that contains the
|
---|
1069 | * pre-processed transaction information pulled from the virtq.
|
---|
1070 | * Returned reference must be released by calling
|
---|
1071 | * virtioCoreR3DescChainRelease().
|
---|
1072 | * @param fRemove flags whether to remove desc chain from queue (false = peek)
|
---|
1073 | *
|
---|
1074 | * @returns VBox status code:
|
---|
1075 | * @retval VINF_SUCCESS Success
|
---|
1076 | * @retval VERR_INVALID_STATE VirtIO not in ready state (asserted).
|
---|
1077 | * @retval VERR_NOT_AVAILABLE If the queue is empty.
|
---|
1078 | */
|
---|
1079 | int virtioCoreR3QueueGet(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t idxQueue,
|
---|
1080 | PPVIRTIO_DESC_CHAIN_T ppDescChain, bool fRemove)
|
---|
1081 | {
|
---|
1082 | PVIRTQSTATE pVirtq = &pVirtio->virtqState[idxQueue];
|
---|
1083 |
|
---|
1084 | if (IS_VIRTQ_EMPTY(pDevIns, pVirtio, pVirtq))
|
---|
1085 | return VERR_NOT_AVAILABLE;
|
---|
1086 |
|
---|
1087 | uint16_t uHeadIdx = virtioReadAvailDescIdx(pDevIns, pVirtio, idxQueue, pVirtq->uAvailIdxShadow);
|
---|
1088 |
|
---|
1089 | if (pVirtio->uDriverFeatures & VIRTIO_F_EVENT_IDX)
|
---|
1090 | virtioWriteUsedAvailEvent(pDevIns,pVirtio, idxQueue, pVirtq->uAvailIdxShadow + 1);
|
---|
1091 |
|
---|
1092 | if (fRemove)
|
---|
1093 | pVirtq->uAvailIdxShadow++;
|
---|
1094 |
|
---|
1095 | int rc = virtioCoreR3DescChainGet(pDevIns, pVirtio, idxQueue, uHeadIdx, ppDescChain);
|
---|
1096 | return rc;
|
---|
1097 | }
|
---|
1098 |
|
---|
1099 | /**
|
---|
1100 | * Returns data to the guest to complete a transaction initiated by virtQueueGet().
|
---|
1101 | *
|
---|
1102 | * The caller passes in a pointer to a scatter-gather buffer of virtual memory segments
|
---|
1103 | * and a pointer to the descriptor chain context originally derived from the pulled
|
---|
1104 | * queue entry, and this function will write the virtual memory s/g buffer into the
|
---|
1105 | * guest's physical memory free the descriptor chain. The caller handles the freeing
|
---|
1106 | * (as needed) of the virtual memory buffer.
|
---|
1107 | *
|
---|
1108 | * @note This does a write-ahead to the used ring of the guest's queue. The data
|
---|
1109 | * written won't be seen by the guest until the next call to virtioCoreQueueSync()
|
---|
1110 | *
|
---|
1111 | *
|
---|
1112 | * @param pDevIns The device instance (for reading).
|
---|
1113 | * @param pVirtio Pointer to the shared virtio state.
|
---|
1114 | * @param idxQueue Queue number
|
---|
1115 | *
|
---|
1116 | * @param pSgVirtReturn Points to scatter-gather buffer of virtual memory
|
---|
1117 | * segments the caller is returning to the guest.
|
---|
1118 | *
|
---|
1119 | * @param pDescChain This contains the context of the scatter-gather
|
---|
1120 | * buffer originally pulled from the queue.
|
---|
1121 | *
|
---|
1122 | * @param fFence If true, put up copy fence (memory barrier) after
|
---|
1123 | * copying to guest phys. mem.
|
---|
1124 | *
|
---|
1125 | * @returns VBox status code.
|
---|
1126 | * @retval VINF_SUCCESS Success
|
---|
1127 | * @retval VERR_INVALID_STATE VirtIO not in ready state
|
---|
1128 | * @retval VERR_NOT_AVAILABLE Queue is empty
|
---|
1129 | *
|
---|
1130 | * @note This function will not release any reference to pDescChain. The
|
---|
1131 | * caller must take care of that.
|
---|
1132 | */
|
---|
1133 | int virtioCoreR3QueuePut(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t idxQueue, PRTSGBUF pSgVirtReturn,
|
---|
1134 | PVIRTIO_DESC_CHAIN_T pDescChain, bool fFence)
|
---|
1135 | {
|
---|
1136 | Assert(idxQueue < RT_ELEMENTS(pVirtio->virtqState));
|
---|
1137 | PVIRTQSTATE pVirtq = &pVirtio->virtqState[idxQueue];
|
---|
1138 | PVIRTIOSGBUF pSgPhysReturn = pDescChain->pSgPhysReturn;
|
---|
1139 |
|
---|
1140 | Assert(pDescChain->u32Magic == VIRTIO_DESC_CHAIN_MAGIC);
|
---|
1141 | Assert(pDescChain->cRefs > 0);
|
---|
1142 |
|
---|
1143 | AssertMsgReturn(IS_DRIVER_OK(pVirtio), ("Guest driver not in ready state.\n"), VERR_INVALID_STATE);
|
---|
1144 |
|
---|
1145 | Log6Func(("Copying client data to %s, desc chain (head desc_idx %d)\n",
|
---|
1146 | VIRTQNAME(pVirtio, idxQueue), virtioReadUsedRingIdx(pDevIns, pVirtio, idxQueue)));
|
---|
1147 |
|
---|
1148 | /* Copy s/g buf (virtual memory) to guest phys mem (IN direction). */
|
---|
1149 |
|
---|
1150 | size_t cbCopy = 0, cbTotal = 0, cbRemain = 0;
|
---|
1151 |
|
---|
1152 | if (pSgVirtReturn)
|
---|
1153 | {
|
---|
1154 | size_t cbTarget = virtioCoreSgBufCalcTotalLength(pSgPhysReturn);
|
---|
1155 | cbRemain = cbTotal = RTSgBufCalcTotalLength(pSgVirtReturn);
|
---|
1156 | AssertMsgReturn(cbTarget >= cbRemain, ("No space to write data to phys memory"), VERR_BUFFER_OVERFLOW);
|
---|
1157 | virtioCoreSgBufReset(pSgPhysReturn); /* Reset ptr because req data may have already been written */
|
---|
1158 | while (cbRemain)
|
---|
1159 | {
|
---|
1160 | cbCopy = RT_MIN(pSgVirtReturn->cbSegLeft, pSgPhysReturn->cbSegLeft);
|
---|
1161 | Assert(cbCopy > 0);
|
---|
1162 | PDMDevHlpPhysWrite(pDevIns, (RTGCPHYS)pSgPhysReturn->GCPhysCur, pSgVirtReturn->pvSegCur, cbCopy);
|
---|
1163 | RTSgBufAdvance(pSgVirtReturn, cbCopy);
|
---|
1164 | virtioCoreSgBufAdvance(pSgPhysReturn, cbCopy);
|
---|
1165 | cbRemain -= cbCopy;
|
---|
1166 | }
|
---|
1167 |
|
---|
1168 | if (fFence)
|
---|
1169 | RT_UNTRUSTED_NONVOLATILE_COPY_FENCE(); /* needed? */
|
---|
1170 |
|
---|
1171 | Assert(!(cbCopy >> 32));
|
---|
1172 | }
|
---|
1173 |
|
---|
1174 | /* If this write-ahead crosses threshold where the driver wants to get an event flag it */
|
---|
1175 | if (pVirtio->uDriverFeatures & VIRTIO_F_EVENT_IDX)
|
---|
1176 | if (pVirtq->uUsedIdxShadow == virtioReadAvailUsedEvent(pDevIns, pVirtio, idxQueue))
|
---|
1177 | pVirtq->fVirtqRingEventThreshold = true;
|
---|
1178 |
|
---|
1179 | /*
|
---|
1180 | * Place used buffer's descriptor in used ring but don't update used ring's slot index.
|
---|
1181 | * That will be done with a subsequent client call to virtioCoreQueueSync() */
|
---|
1182 | virtioWriteUsedElem(pDevIns, pVirtio, idxQueue, pVirtq->uUsedIdxShadow++, pDescChain->uHeadIdx, (uint32_t)cbTotal);
|
---|
1183 |
|
---|
1184 | if (pSgVirtReturn)
|
---|
1185 | Log6Func((".... Copied %zu bytes in %d segs to %u byte buffer, residual=%zu\n",
|
---|
1186 | cbTotal - cbRemain, pSgVirtReturn->cSegs, pDescChain->cbPhysReturn, pDescChain->cbPhysReturn - cbTotal));
|
---|
1187 |
|
---|
1188 | Log6Func(("Write ahead used_idx=%u, %s used_idx=%u\n",
|
---|
1189 | pVirtq->uUsedIdxShadow, VIRTQNAME(pVirtio, idxQueue), virtioReadUsedRingIdx(pDevIns, pVirtio, idxQueue)));
|
---|
1190 |
|
---|
1191 | return VINF_SUCCESS;
|
---|
1192 | }
|
---|
1193 |
|
---|
1194 | #endif /* IN_RING3 */
|
---|
1195 |
|
---|
1196 | /**
|
---|
1197 | * Updates the indicated virtq's "used ring" descriptor index to match the
|
---|
1198 | * current write-head index, thus exposing the data added to the used ring by all
|
---|
1199 | * virtioCoreR3QueuePut() calls since the last sync. This should be called after one or
|
---|
1200 | * more virtioCoreR3QueuePut() calls to inform the guest driver there is data in the queue.
|
---|
1201 | * Explicit notifications (e.g. interrupt or MSI-X) will be sent to the guest,
|
---|
1202 | * depending on VirtIO features negotiated and conditions, otherwise the guest
|
---|
1203 | * will detect the update by polling. (see VirtIO 1.0 specification, Section 2.4 "Virtqueues").
|
---|
1204 | *
|
---|
1205 | * @param pDevIns The device instance.
|
---|
1206 | * @param pVirtio Pointer to the shared virtio state.
|
---|
1207 | * @param idxQueue Queue number
|
---|
1208 | *
|
---|
1209 | * @returns VBox status code.
|
---|
1210 | * @retval VINF_SUCCESS Success
|
---|
1211 | * @retval VERR_INVALID_STATE VirtIO not in ready state
|
---|
1212 | */
|
---|
1213 | int virtioCoreQueueSync(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t idxQueue)
|
---|
1214 | {
|
---|
1215 | Assert(idxQueue < RT_ELEMENTS(pVirtio->virtqState));
|
---|
1216 | PVIRTQSTATE pVirtq = &pVirtio->virtqState[idxQueue];
|
---|
1217 |
|
---|
1218 | AssertMsgReturn(IS_DRIVER_OK(pVirtio) && pVirtio->uQueueEnable[idxQueue],
|
---|
1219 | ("Guest driver not in ready state.\n"), VERR_INVALID_STATE);
|
---|
1220 |
|
---|
1221 | Log6Func(("Updating %s used_idx to %u\n",
|
---|
1222 | VIRTQNAME(pVirtio, idxQueue), pVirtq->uUsedIdxShadow));
|
---|
1223 |
|
---|
1224 | virtioWriteUsedRingIdx(pDevIns, pVirtio, idxQueue, pVirtq->uUsedIdxShadow);
|
---|
1225 | virtioCoreNotifyGuestDriver(pDevIns, pVirtio, idxQueue);
|
---|
1226 |
|
---|
1227 | return VINF_SUCCESS;
|
---|
1228 | }
|
---|
1229 |
|
---|
1230 |
|
---|
1231 | /**
|
---|
1232 | */
|
---|
1233 | static void virtioCoreQueueNotified(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t idxQueue, uint16_t uNotifyIdx)
|
---|
1234 | {
|
---|
1235 |
|
---|
1236 | PVIRTIOCORECC pVirtioCC = PDMDEVINS_2_DATA_CC(pDevIns, PVIRTIOCORECC);
|
---|
1237 |
|
---|
1238 | /* See VirtIO 1.0, section 4.1.5.2 It implies that idxQueue and uNotifyIdx should match.
|
---|
1239 | * Disregarding this notification may cause throughput to stop, however there's no way to know
|
---|
1240 | * which was queue was intended for wake-up if the two parameters disagree. */
|
---|
1241 |
|
---|
1242 | AssertMsg(uNotifyIdx == idxQueue,
|
---|
1243 | ("Guest kicked virtq %d's notify addr w/non-corresponding virtq idx %d\n",
|
---|
1244 | idxQueue, uNotifyIdx));
|
---|
1245 | RT_NOREF(uNotifyIdx);
|
---|
1246 |
|
---|
1247 | AssertReturnVoid(idxQueue < RT_ELEMENTS(pVirtio->virtqState));
|
---|
1248 | Log6Func(("%s (desc chains: %u)\n",
|
---|
1249 | pVirtio->virtqState[idxQueue].szVirtqName,
|
---|
1250 | virtioCoreQueueAvailCount(pDevIns, pVirtio, idxQueue)));
|
---|
1251 |
|
---|
1252 | /* Inform client */
|
---|
1253 | pVirtioCC->pfnQueueNotified(pDevIns, pVirtio, idxQueue);
|
---|
1254 | }
|
---|
1255 |
|
---|
1256 | /**
|
---|
1257 | * Trigger MSI-X or INT# interrupt to notify guest of data added to used ring of
|
---|
1258 | * the specified virtq, depending on the interrupt configuration of the device
|
---|
1259 | * and depending on negotiated and realtime constraints flagged by the guest driver.
|
---|
1260 | *
|
---|
1261 | * See VirtIO 1.0 specification (section 2.4.7).
|
---|
1262 | *
|
---|
1263 | * @param pDevIns The device instance.
|
---|
1264 | * @param pVirtio Pointer to the shared virtio state.
|
---|
1265 | * @param idxQueue Queue to check for guest interrupt handling preference
|
---|
1266 | */
|
---|
1267 | static void virtioCoreNotifyGuestDriver(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t idxQueue)
|
---|
1268 | {
|
---|
1269 |
|
---|
1270 | Assert(idxQueue < RT_ELEMENTS(pVirtio->virtqState));
|
---|
1271 | PVIRTQSTATE pVirtq = &pVirtio->virtqState[idxQueue];
|
---|
1272 |
|
---|
1273 | if (!IS_DRIVER_OK(pVirtio))
|
---|
1274 | {
|
---|
1275 | LogFunc(("Guest driver not in ready state.\n"));
|
---|
1276 | return;
|
---|
1277 | }
|
---|
1278 |
|
---|
1279 | if (pVirtio->uDriverFeatures & VIRTIO_F_EVENT_IDX)
|
---|
1280 | {
|
---|
1281 | if (pVirtq->fVirtqRingEventThreshold)
|
---|
1282 | {
|
---|
1283 | #ifdef IN_RING3
|
---|
1284 | Log6Func(("...kicking guest %s, VIRTIO_F_EVENT_IDX set and threshold (%d) reached\n",
|
---|
1285 | VIRTQNAME(pVirtio, idxQueue), (uint16_t)virtioReadAvailUsedEvent(pDevIns, pVirtio, idxQueue)));
|
---|
1286 | #endif
|
---|
1287 | virtioKick(pDevIns, pVirtio, VIRTIO_ISR_VIRTQ_INTERRUPT, pVirtio->uQueueMsixVector[idxQueue]);
|
---|
1288 | pVirtq->fVirtqRingEventThreshold = false;
|
---|
1289 | return;
|
---|
1290 | }
|
---|
1291 | #ifdef IN_RING3
|
---|
1292 | Log6Func(("...skip interrupt %s, VIRTIO_F_EVENT_IDX set but threshold (%d) not reached (%d)\n",
|
---|
1293 | VIRTQNAME(pVirtio, idxQueue),(uint16_t)virtioReadAvailUsedEvent(pDevIns, pVirtio, idxQueue), pVirtq->uUsedIdxShadow));
|
---|
1294 | #endif
|
---|
1295 | }
|
---|
1296 | else
|
---|
1297 | {
|
---|
1298 | /** If guest driver hasn't suppressed interrupts, interrupt */
|
---|
1299 | if (!(virtioReadAvailRingFlags(pDevIns, pVirtio, idxQueue) & VIRTQ_AVAIL_F_NO_INTERRUPT))
|
---|
1300 | {
|
---|
1301 | virtioKick(pDevIns, pVirtio, VIRTIO_ISR_VIRTQ_INTERRUPT, pVirtio->uQueueMsixVector[idxQueue]);
|
---|
1302 | return;
|
---|
1303 | }
|
---|
1304 | Log6Func(("...skipping interrupt for %s (guest set VIRTQ_AVAIL_F_NO_INTERRUPT)\n",
|
---|
1305 | VIRTQNAME(pVirtio, idxQueue)));
|
---|
1306 | }
|
---|
1307 | }
|
---|
1308 |
|
---|
1309 | /**
|
---|
1310 | * Raise interrupt or MSI-X
|
---|
1311 | *
|
---|
1312 | * @param pDevIns The device instance.
|
---|
1313 | * @param pVirtio Pointer to the shared virtio state.
|
---|
1314 | * @param uCause Interrupt cause bit mask to set in PCI ISR port.
|
---|
1315 | * @param uVec MSI-X vector, if enabled
|
---|
1316 | */
|
---|
1317 | static int virtioKick(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint8_t uCause, uint16_t uMsixVector)
|
---|
1318 | {
|
---|
1319 | if (uCause == VIRTIO_ISR_VIRTQ_INTERRUPT)
|
---|
1320 | Log6Func(("reason: buffer added to 'used' ring.\n"));
|
---|
1321 | else
|
---|
1322 | if (uCause == VIRTIO_ISR_DEVICE_CONFIG)
|
---|
1323 | Log6Func(("reason: device config change\n"));
|
---|
1324 |
|
---|
1325 | if (!pVirtio->fMsiSupport)
|
---|
1326 | {
|
---|
1327 | pVirtio->uISR |= uCause;
|
---|
1328 | PDMDevHlpPCISetIrq(pDevIns, 0, PDM_IRQ_LEVEL_HIGH);
|
---|
1329 | }
|
---|
1330 | else if (uMsixVector != VIRTIO_MSI_NO_VECTOR)
|
---|
1331 | PDMDevHlpPCISetIrq(pDevIns, uMsixVector, 1);
|
---|
1332 | return VINF_SUCCESS;
|
---|
1333 | }
|
---|
1334 |
|
---|
1335 | /**
|
---|
1336 | * Lower interrupt (Called when guest reads ISR and when resetting)
|
---|
1337 | *
|
---|
1338 | * @param pDevIns The device instance.
|
---|
1339 | */
|
---|
1340 | static void virtioLowerInterrupt(PPDMDEVINS pDevIns, uint16_t uMsixVector)
|
---|
1341 | {
|
---|
1342 | PVIRTIOCORE pVirtio = PDMINS_2_DATA(pDevIns, PVIRTIOCORE);
|
---|
1343 | if (!pVirtio->fMsiSupport)
|
---|
1344 | PDMDevHlpPCISetIrq(pDevIns, 0, PDM_IRQ_LEVEL_LOW);
|
---|
1345 | else if (uMsixVector != VIRTIO_MSI_NO_VECTOR)
|
---|
1346 | PDMDevHlpPCISetIrq(pDevIns, pVirtio->uMsixConfig, PDM_IRQ_LEVEL_LOW);
|
---|
1347 | }
|
---|
1348 |
|
---|
1349 | #ifdef IN_RING3
|
---|
1350 | static void virtioResetQueue(PVIRTIOCORE pVirtio, uint16_t idxQueue)
|
---|
1351 | {
|
---|
1352 | Assert(idxQueue < RT_ELEMENTS(pVirtio->virtqState));
|
---|
1353 | PVIRTQSTATE pVirtq = &pVirtio->virtqState[idxQueue];
|
---|
1354 | pVirtq->uAvailIdxShadow = 0;
|
---|
1355 | pVirtq->uUsedIdxShadow = 0;
|
---|
1356 | pVirtq->fVirtqRingEventThreshold = false;
|
---|
1357 | pVirtio->uQueueEnable[idxQueue] = false;
|
---|
1358 | pVirtio->uQueueSize[idxQueue] = VIRTQ_MAX_ENTRIES;
|
---|
1359 | pVirtio->uQueueNotifyOff[idxQueue] = idxQueue;
|
---|
1360 | pVirtio->uQueueMsixVector[idxQueue] = idxQueue + 2;
|
---|
1361 | if (!pVirtio->fMsiSupport) /* VirtIO 1.0, 4.1.4.3 and 4.1.5.1.2 */
|
---|
1362 | pVirtio->uQueueMsixVector[idxQueue] = VIRTIO_MSI_NO_VECTOR;
|
---|
1363 |
|
---|
1364 | virtioLowerInterrupt(pVirtio->pDevInsR3, pVirtio->uQueueMsixVector[idxQueue]);
|
---|
1365 | }
|
---|
1366 |
|
---|
1367 | static void virtioResetDevice(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio)
|
---|
1368 | {
|
---|
1369 | Log2Func(("\n"));
|
---|
1370 | pVirtio->uDeviceFeaturesSelect = 0;
|
---|
1371 | pVirtio->uDriverFeaturesSelect = 0;
|
---|
1372 | pVirtio->uConfigGeneration = 0;
|
---|
1373 | pVirtio->uDeviceStatus = 0;
|
---|
1374 | pVirtio->uISR = 0;
|
---|
1375 |
|
---|
1376 | if (!pVirtio->fMsiSupport)
|
---|
1377 | virtioLowerInterrupt(pDevIns, 0);
|
---|
1378 | else
|
---|
1379 | {
|
---|
1380 | virtioLowerInterrupt(pDevIns, pVirtio->uMsixConfig);
|
---|
1381 | for (int i = 0; i < VIRTQ_MAX_CNT; i++)
|
---|
1382 | {
|
---|
1383 | virtioLowerInterrupt(pDevIns, pVirtio->uQueueMsixVector[i]);
|
---|
1384 | pVirtio->uQueueMsixVector[i];
|
---|
1385 | }
|
---|
1386 | }
|
---|
1387 |
|
---|
1388 | if (!pVirtio->fMsiSupport) /* VirtIO 1.0, 4.1.4.3 and 4.1.5.1.2 */
|
---|
1389 | pVirtio->uMsixConfig = VIRTIO_MSI_NO_VECTOR;
|
---|
1390 |
|
---|
1391 | for (uint16_t idxQueue = 0; idxQueue < VIRTQ_MAX_CNT; idxQueue++)
|
---|
1392 | virtioResetQueue(pVirtio, idxQueue);
|
---|
1393 | }
|
---|
1394 |
|
---|
1395 | /**
|
---|
1396 | * Invoked by this implementation when guest driver resets the device.
|
---|
1397 | * The driver itself will not until the device has read the status change.
|
---|
1398 | */
|
---|
1399 | static void virtioGuestR3WasReset(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, PVIRTIOCORECC pVirtioCC)
|
---|
1400 | {
|
---|
1401 | LogFunc(("Guest reset the device\n"));
|
---|
1402 |
|
---|
1403 | /* Let the client know */
|
---|
1404 | pVirtioCC->pfnStatusChanged(pVirtio, pVirtioCC, 0);
|
---|
1405 | virtioResetDevice(pDevIns, pVirtio);
|
---|
1406 | }
|
---|
1407 | #endif /* IN_RING3 */
|
---|
1408 |
|
---|
1409 | /**
|
---|
1410 | * Handle accesses to Common Configuration capability
|
---|
1411 | *
|
---|
1412 | * @returns VBox status code
|
---|
1413 | *
|
---|
1414 | * @param pDevIns The device instance.
|
---|
1415 | * @param pVirtio Pointer to the shared virtio state.
|
---|
1416 | * @param pVirtioCC Pointer to the current context virtio state.
|
---|
1417 | * @param fWrite Set if write access, clear if read access.
|
---|
1418 | * @param offCfg The common configuration capability offset.
|
---|
1419 | * @param cb Number of bytes to read or write
|
---|
1420 | * @param pv Pointer to location to write to or read from
|
---|
1421 | */
|
---|
1422 | static int virtioCommonCfgAccessed(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, PVIRTIOCORECC pVirtioCC,
|
---|
1423 | int fWrite, uint32_t offCfg, unsigned cb, void *pv)
|
---|
1424 | {
|
---|
1425 | /**
|
---|
1426 | * This macro resolves to boolean true if the implied parameters, offCfg and cb,
|
---|
1427 | * match the field offset and size of a field in the Common Cfg struct, (or if
|
---|
1428 | * it is a 64-bit field, if it accesses either 32-bit part as a 32-bit access)
|
---|
1429 | * This is mandated by section 4.1.3.1 of the VirtIO 1.0 specification)
|
---|
1430 | *
|
---|
1431 | * @param member Member of VIRTIO_PCI_COMMON_CFG_T
|
---|
1432 | * @param offCfg Implied parameter: Offset into VIRTIO_PCI_COMMON_CFG_T
|
---|
1433 | * @param cb Implied parameter: Number of bytes to access
|
---|
1434 | * @result true or false
|
---|
1435 | */
|
---|
1436 | #define MATCH_COMMON_CFG(member) \
|
---|
1437 | ( ( RT_SIZEOFMEMB(VIRTIO_PCI_COMMON_CFG_T, member) == 8 \
|
---|
1438 | && ( offCfg == RT_OFFSETOF(VIRTIO_PCI_COMMON_CFG_T, member) \
|
---|
1439 | || offCfg == RT_OFFSETOF(VIRTIO_PCI_COMMON_CFG_T, member) + sizeof(uint32_t)) \
|
---|
1440 | && cb == sizeof(uint32_t)) \
|
---|
1441 | || ( offCfg == RT_OFFSETOF(VIRTIO_PCI_COMMON_CFG_T, member) \
|
---|
1442 | && cb == RT_SIZEOFMEMB(VIRTIO_PCI_COMMON_CFG_T, member)) )
|
---|
1443 |
|
---|
1444 | #ifdef LOG_ENABLED
|
---|
1445 | # define LOG_COMMON_CFG_ACCESS(member, uOffset) \
|
---|
1446 | if (LogIs7Enabled()) { \
|
---|
1447 | virtioCoreLogMappedIoValue(__FUNCTION__, #member, RT_SIZEOFMEMB(VIRTIO_PCI_COMMON_CFG_T, member), \
|
---|
1448 | pv, cb, uOffset, fWrite, false, 0); \
|
---|
1449 | }
|
---|
1450 | # define LOG_COMMON_CFG_ACCESS_INDEXED(member, idx, uOffset) \
|
---|
1451 | if (LogIs7Enabled()) { \
|
---|
1452 | virtioCoreLogMappedIoValue(__FUNCTION__, #member, RT_SIZEOFMEMB(VIRTIO_PCI_COMMON_CFG_T, member), \
|
---|
1453 | pv, cb, uOffset, fWrite, true, idx); \
|
---|
1454 | }
|
---|
1455 | #else
|
---|
1456 | # define LOG_COMMON_CFG_ACCESS(member, uOffset) do { } while (0)
|
---|
1457 | # define LOG_COMMON_CFG_ACCESS_INDEXED(member, idx, uOffset) do { } while (0)
|
---|
1458 | #endif
|
---|
1459 |
|
---|
1460 | #define COMMON_CFG_ACCESSOR(member) \
|
---|
1461 | do \
|
---|
1462 | { \
|
---|
1463 | uint32_t uOffset = offCfg - RT_OFFSETOF(VIRTIO_PCI_COMMON_CFG_T, member); \
|
---|
1464 | if (fWrite) \
|
---|
1465 | memcpy((char *)&pVirtio->member + uOffset, (const char *)pv, cb); \
|
---|
1466 | else \
|
---|
1467 | memcpy(pv, (const char *)&pVirtio->member + uOffset, cb); \
|
---|
1468 | LOG_COMMON_CFG_ACCESS(member, uOffset); \
|
---|
1469 | } while(0)
|
---|
1470 |
|
---|
1471 | #define COMMON_CFG_ACCESSOR_INDEXED(member, idx) \
|
---|
1472 | do \
|
---|
1473 | { \
|
---|
1474 | uint32_t uOffset = offCfg - RT_OFFSETOF(VIRTIO_PCI_COMMON_CFG_T, member); \
|
---|
1475 | if (fWrite) \
|
---|
1476 | memcpy((char *)&pVirtio->member[idx] + uOffset, pv, cb); \
|
---|
1477 | else \
|
---|
1478 | memcpy(pv, (const char *)&pVirtio->member[idx] + uOffset, cb); \
|
---|
1479 | LOG_COMMON_CFG_ACCESS_INDEXED(member, idx, uOffset); \
|
---|
1480 | } while(0)
|
---|
1481 |
|
---|
1482 | #define COMMON_CFG_ACCESSOR_READONLY(member) \
|
---|
1483 | do \
|
---|
1484 | { \
|
---|
1485 | uint32_t uOffset = offCfg - RT_OFFSETOF(VIRTIO_PCI_COMMON_CFG_T, member); \
|
---|
1486 | if (fWrite) \
|
---|
1487 | LogFunc(("Guest attempted to write readonly virtio_pci_common_cfg.%s\n", #member)); \
|
---|
1488 | else \
|
---|
1489 | { \
|
---|
1490 | memcpy(pv, (const char *)&pVirtio->member + uOffset, cb); \
|
---|
1491 | LOG_COMMON_CFG_ACCESS(member, uOffset); \
|
---|
1492 | } \
|
---|
1493 | } while(0)
|
---|
1494 |
|
---|
1495 | #define COMMON_CFG_ACCESSOR_INDEXED_READONLY(member, idx) \
|
---|
1496 | do \
|
---|
1497 | { \
|
---|
1498 | uint32_t uOffset = offCfg - RT_OFFSETOF(VIRTIO_PCI_COMMON_CFG_T, member); \
|
---|
1499 | if (fWrite) \
|
---|
1500 | LogFunc(("Guest attempted to write readonly virtio_pci_common_cfg.%s[%d]\n", #member, idx)); \
|
---|
1501 | else \
|
---|
1502 | { \
|
---|
1503 | memcpy(pv, (char const *)&pVirtio->member[idx] + uOffset, cb); \
|
---|
1504 | LOG_COMMON_CFG_ACCESS_INDEXED(member, idx, uOffset); \
|
---|
1505 | } \
|
---|
1506 | } while(0)
|
---|
1507 |
|
---|
1508 |
|
---|
1509 | int rc = VINF_SUCCESS;
|
---|
1510 | uint64_t val;
|
---|
1511 | if (MATCH_COMMON_CFG(uDeviceFeatures))
|
---|
1512 | {
|
---|
1513 | if (fWrite) /* Guest WRITE pCommonCfg>uDeviceFeatures */
|
---|
1514 | {
|
---|
1515 | LogFunc(("Guest attempted to write readonly virtio_pci_common_cfg.device_feature\n"));
|
---|
1516 | return VINF_SUCCESS;
|
---|
1517 | }
|
---|
1518 | else /* Guest READ pCommonCfg->uDeviceFeatures */
|
---|
1519 | {
|
---|
1520 | switch (pVirtio->uDeviceFeaturesSelect)
|
---|
1521 | {
|
---|
1522 | case 0:
|
---|
1523 | val = pVirtio->uDeviceFeatures & UINT32_C(0xffffffff);
|
---|
1524 | memcpy(pv, &val, cb);
|
---|
1525 | LOG_COMMON_CFG_ACCESS(uDeviceFeatures, offCfg - RT_UOFFSETOF(VIRTIO_PCI_COMMON_CFG_T, uDeviceFeatures));
|
---|
1526 | break;
|
---|
1527 | case 1:
|
---|
1528 | val = pVirtio->uDeviceFeatures >> 32;
|
---|
1529 | memcpy(pv, &val, cb);
|
---|
1530 | LOG_COMMON_CFG_ACCESS(uDeviceFeatures, offCfg - RT_UOFFSETOF(VIRTIO_PCI_COMMON_CFG_T, uDeviceFeatures) + 4);
|
---|
1531 | break;
|
---|
1532 | default:
|
---|
1533 | LogFunc(("Guest read uDeviceFeatures with out of range selector (%#x), returning 0\n",
|
---|
1534 | pVirtio->uDeviceFeaturesSelect));
|
---|
1535 | return VINF_IOM_MMIO_UNUSED_00;
|
---|
1536 | }
|
---|
1537 | }
|
---|
1538 | }
|
---|
1539 | else if (MATCH_COMMON_CFG(uDriverFeatures))
|
---|
1540 | {
|
---|
1541 | if (fWrite) /* Guest WRITE pCommonCfg->udriverFeatures */
|
---|
1542 | {
|
---|
1543 | switch (pVirtio->uDriverFeaturesSelect)
|
---|
1544 | {
|
---|
1545 | case 0:
|
---|
1546 | memcpy(&pVirtio->uDriverFeatures, pv, cb);
|
---|
1547 | LOG_COMMON_CFG_ACCESS(uDriverFeatures, offCfg - RT_UOFFSETOF(VIRTIO_PCI_COMMON_CFG_T, uDriverFeatures));
|
---|
1548 | break;
|
---|
1549 | case 1:
|
---|
1550 | memcpy((char *)&pVirtio->uDriverFeatures + sizeof(uint32_t), pv, cb);
|
---|
1551 | LOG_COMMON_CFG_ACCESS(uDriverFeatures, offCfg - RT_UOFFSETOF(VIRTIO_PCI_COMMON_CFG_T, uDriverFeatures) + 4);
|
---|
1552 | break;
|
---|
1553 | default:
|
---|
1554 | LogFunc(("Guest wrote uDriverFeatures with out of range selector (%#x), returning 0\n",
|
---|
1555 | pVirtio->uDriverFeaturesSelect));
|
---|
1556 | return VINF_SUCCESS;
|
---|
1557 | }
|
---|
1558 | }
|
---|
1559 | else /* Guest READ pCommonCfg->udriverFeatures */
|
---|
1560 | {
|
---|
1561 | switch (pVirtio->uDriverFeaturesSelect)
|
---|
1562 | {
|
---|
1563 | case 0:
|
---|
1564 | val = pVirtio->uDriverFeatures & 0xffffffff;
|
---|
1565 | memcpy(pv, &val, cb);
|
---|
1566 | LOG_COMMON_CFG_ACCESS(uDriverFeatures, offCfg - RT_UOFFSETOF(VIRTIO_PCI_COMMON_CFG_T, uDriverFeatures));
|
---|
1567 | break;
|
---|
1568 | case 1:
|
---|
1569 | val = (pVirtio->uDriverFeatures >> 32) & 0xffffffff;
|
---|
1570 | memcpy(pv, &val, cb);
|
---|
1571 | LOG_COMMON_CFG_ACCESS(uDriverFeatures, offCfg - RT_UOFFSETOF(VIRTIO_PCI_COMMON_CFG_T, uDriverFeatures) + 4);
|
---|
1572 | break;
|
---|
1573 | default:
|
---|
1574 | LogFunc(("Guest read uDriverFeatures with out of range selector (%#x), returning 0\n",
|
---|
1575 | pVirtio->uDriverFeaturesSelect));
|
---|
1576 | return VINF_IOM_MMIO_UNUSED_00;
|
---|
1577 | }
|
---|
1578 | }
|
---|
1579 | }
|
---|
1580 | else if (MATCH_COMMON_CFG(uNumQueues))
|
---|
1581 | {
|
---|
1582 | if (fWrite)
|
---|
1583 | {
|
---|
1584 | Log2Func(("Guest attempted to write readonly virtio_pci_common_cfg.num_queues\n"));
|
---|
1585 | return VINF_SUCCESS;
|
---|
1586 | }
|
---|
1587 | else
|
---|
1588 | {
|
---|
1589 | *(uint16_t *)pv = VIRTQ_MAX_CNT;
|
---|
1590 | LOG_COMMON_CFG_ACCESS(uNumQueues, 0);
|
---|
1591 | }
|
---|
1592 | }
|
---|
1593 | else if (MATCH_COMMON_CFG(uDeviceStatus))
|
---|
1594 | {
|
---|
1595 | if (fWrite) /* Guest WRITE pCommonCfg->uDeviceStatus */
|
---|
1596 | {
|
---|
1597 | uint8_t const fNewStatus = *(uint8_t *)pv;
|
---|
1598 | Log7Func(("Guest wrote uDeviceStatus ................ ("));
|
---|
1599 | if (LogIs7Enabled())
|
---|
1600 | virtioLogDeviceStatus(fNewStatus ^ pVirtio->uDeviceStatus);
|
---|
1601 | Log7((")\n"));
|
---|
1602 |
|
---|
1603 | /* If the status changed or we were reset, we need to go to ring-3 as
|
---|
1604 | it requires notifying the parent device. */
|
---|
1605 | bool const fStatusChanged = (fNewStatus & VIRTIO_STATUS_DRIVER_OK)
|
---|
1606 | != (pVirtio->uPrevDeviceStatus & VIRTIO_STATUS_DRIVER_OK);
|
---|
1607 | #ifndef IN_RING3
|
---|
1608 | if (fStatusChanged || fNewStatus == 0)
|
---|
1609 | {
|
---|
1610 | Log6Func(("=>ring3\n"));
|
---|
1611 | return VINF_IOM_R3_MMIO_WRITE;
|
---|
1612 | }
|
---|
1613 | #endif
|
---|
1614 | pVirtio->uDeviceStatus = fNewStatus;
|
---|
1615 |
|
---|
1616 | #ifdef IN_RING3
|
---|
1617 | /*
|
---|
1618 | * Notify client only if status actually changed from last time and when we're reset.
|
---|
1619 | */
|
---|
1620 | if (pVirtio->uDeviceStatus == 0)
|
---|
1621 | virtioGuestR3WasReset(pDevIns, pVirtio, pVirtioCC);
|
---|
1622 | if (fStatusChanged)
|
---|
1623 | pVirtioCC->pfnStatusChanged(pVirtio, pVirtioCC, fNewStatus & VIRTIO_STATUS_DRIVER_OK);
|
---|
1624 | #endif
|
---|
1625 | /*
|
---|
1626 | * Save the current status for the next write so we can see what changed.
|
---|
1627 | */
|
---|
1628 | pVirtio->uPrevDeviceStatus = pVirtio->uDeviceStatus;
|
---|
1629 | }
|
---|
1630 | else /* Guest READ pCommonCfg->uDeviceStatus */
|
---|
1631 | {
|
---|
1632 | Log7Func(("Guest read uDeviceStatus ................ ("));
|
---|
1633 | *(uint8_t *)pv = pVirtio->uDeviceStatus;
|
---|
1634 | if (LogIs7Enabled())
|
---|
1635 | virtioLogDeviceStatus(pVirtio->uDeviceStatus);
|
---|
1636 | Log7((")\n"));
|
---|
1637 | }
|
---|
1638 | }
|
---|
1639 | else
|
---|
1640 | if (MATCH_COMMON_CFG(uMsixConfig))
|
---|
1641 | COMMON_CFG_ACCESSOR(uMsixConfig);
|
---|
1642 | else
|
---|
1643 | if (MATCH_COMMON_CFG(uDeviceFeaturesSelect))
|
---|
1644 | COMMON_CFG_ACCESSOR(uDeviceFeaturesSelect);
|
---|
1645 | else
|
---|
1646 | if (MATCH_COMMON_CFG(uDriverFeaturesSelect))
|
---|
1647 | COMMON_CFG_ACCESSOR(uDriverFeaturesSelect);
|
---|
1648 | else
|
---|
1649 | if (MATCH_COMMON_CFG(uConfigGeneration))
|
---|
1650 | COMMON_CFG_ACCESSOR_READONLY(uConfigGeneration);
|
---|
1651 | else
|
---|
1652 | if (MATCH_COMMON_CFG(uQueueSelect))
|
---|
1653 | COMMON_CFG_ACCESSOR(uQueueSelect);
|
---|
1654 | else
|
---|
1655 | if (MATCH_COMMON_CFG(uQueueSize))
|
---|
1656 | COMMON_CFG_ACCESSOR_INDEXED(uQueueSize, pVirtio->uQueueSelect);
|
---|
1657 | else
|
---|
1658 | if (MATCH_COMMON_CFG(uQueueMsixVector))
|
---|
1659 | COMMON_CFG_ACCESSOR_INDEXED(uQueueMsixVector, pVirtio->uQueueSelect);
|
---|
1660 | else
|
---|
1661 | if (MATCH_COMMON_CFG(uQueueEnable))
|
---|
1662 | COMMON_CFG_ACCESSOR_INDEXED(uQueueEnable, pVirtio->uQueueSelect);
|
---|
1663 | else
|
---|
1664 | if (MATCH_COMMON_CFG(uQueueNotifyOff))
|
---|
1665 | COMMON_CFG_ACCESSOR_INDEXED_READONLY(uQueueNotifyOff, pVirtio->uQueueSelect);
|
---|
1666 | else
|
---|
1667 | if (MATCH_COMMON_CFG(aGCPhysQueueDesc))
|
---|
1668 | COMMON_CFG_ACCESSOR_INDEXED(aGCPhysQueueDesc, pVirtio->uQueueSelect);
|
---|
1669 | else
|
---|
1670 | if (MATCH_COMMON_CFG(aGCPhysQueueAvail))
|
---|
1671 | COMMON_CFG_ACCESSOR_INDEXED(aGCPhysQueueAvail, pVirtio->uQueueSelect);
|
---|
1672 | else
|
---|
1673 | if (MATCH_COMMON_CFG(aGCPhysQueueUsed))
|
---|
1674 | COMMON_CFG_ACCESSOR_INDEXED(aGCPhysQueueUsed, pVirtio->uQueueSelect);
|
---|
1675 | else
|
---|
1676 | {
|
---|
1677 | Log2Func(("Bad guest %s access to virtio_pci_common_cfg: offCfg=%#x (%d), cb=%d\n",
|
---|
1678 | fWrite ? "write" : "read ", offCfg, offCfg, cb));
|
---|
1679 | return fWrite ? VINF_SUCCESS : VINF_IOM_MMIO_UNUSED_00;
|
---|
1680 | }
|
---|
1681 |
|
---|
1682 | #undef COMMON_CFG_ACCESSOR_READONLY
|
---|
1683 | #undef COMMON_CFG_ACCESSOR_INDEXED_READONLY
|
---|
1684 | #undef COMMON_CFG_ACCESSOR_INDEXED
|
---|
1685 | #undef COMMON_CFG_ACCESSOR
|
---|
1686 | #undef LOG_COMMON_CFG_ACCESS_INDEXED
|
---|
1687 | #undef LOG_COMMON_CFG_ACCESS
|
---|
1688 | #undef MATCH_COMMON_CFG
|
---|
1689 | #ifndef IN_RING3
|
---|
1690 | RT_NOREF(pDevIns, pVirtioCC);
|
---|
1691 | #endif
|
---|
1692 | return rc;
|
---|
1693 | }
|
---|
1694 |
|
---|
1695 | /**
|
---|
1696 | * @callback_method_impl{FNIOMMMIONEWREAD,
|
---|
1697 | * Memory mapped I/O Handler for PCI Capabilities read operations.}
|
---|
1698 | *
|
---|
1699 | * This MMIO handler specifically supports the VIRTIO_PCI_CAP_PCI_CFG capability defined
|
---|
1700 | * in the VirtIO 1.0 specification, section 4.1.4.7, and as such is restricted to reads
|
---|
1701 | * of 1, 2 or 4 bytes, only.
|
---|
1702 | *
|
---|
1703 | */
|
---|
1704 | static DECLCALLBACK(VBOXSTRICTRC) virtioMmioRead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void *pv, unsigned cb)
|
---|
1705 | {
|
---|
1706 | PVIRTIOCORE pVirtio = PDMINS_2_DATA(pDevIns, PVIRTIOCORE);
|
---|
1707 | PVIRTIOCORECC pVirtioCC = PDMINS_2_DATA_CC(pDevIns, PVIRTIOCORECC);
|
---|
1708 | AssertReturn(cb == 1 || cb == 2 || cb == 4, VERR_INVALID_PARAMETER);
|
---|
1709 | Assert(pVirtio == (PVIRTIOCORE)pvUser); RT_NOREF(pvUser);
|
---|
1710 |
|
---|
1711 |
|
---|
1712 | uint32_t uOffset;
|
---|
1713 | if (MATCHES_VIRTIO_CAP_STRUCT(off, cb, uOffset, pVirtio->LocDeviceCap))
|
---|
1714 | {
|
---|
1715 | #ifdef IN_RING3
|
---|
1716 | /*
|
---|
1717 | * Callback to client to manage device-specific configuration.
|
---|
1718 | */
|
---|
1719 | VBOXSTRICTRC rcStrict = pVirtioCC->pfnDevCapRead(pDevIns, uOffset, pv, cb);
|
---|
1720 |
|
---|
1721 | /*
|
---|
1722 | * Additionally, anytime any part of the device-specific configuration (which our client maintains)
|
---|
1723 | * is READ it needs to be checked to see if it changed since the last time any part was read, in
|
---|
1724 | * order to maintain the config generation (see VirtIO 1.0 spec, section 4.1.4.3.1)
|
---|
1725 | */
|
---|
1726 | bool fDevSpecificFieldChanged = RT_BOOL(memcmp(pVirtioCC->pbDevSpecificCfg + uOffset,
|
---|
1727 | pVirtioCC->pbPrevDevSpecificCfg + uOffset,
|
---|
1728 | RT_MIN(cb, pVirtioCC->cbDevSpecificCfg - uOffset)));
|
---|
1729 |
|
---|
1730 | memcpy(pVirtioCC->pbPrevDevSpecificCfg, pVirtioCC->pbDevSpecificCfg, pVirtioCC->cbDevSpecificCfg);
|
---|
1731 |
|
---|
1732 | if (pVirtio->fGenUpdatePending || fDevSpecificFieldChanged)
|
---|
1733 | {
|
---|
1734 | ++pVirtio->uConfigGeneration;
|
---|
1735 | Log6Func(("Bumped cfg. generation to %d because %s%s\n",
|
---|
1736 | pVirtio->uConfigGeneration,
|
---|
1737 | fDevSpecificFieldChanged ? "<dev cfg changed> " : "",
|
---|
1738 | pVirtio->fGenUpdatePending ? "<update was pending>" : ""));
|
---|
1739 | pVirtio->fGenUpdatePending = false;
|
---|
1740 | }
|
---|
1741 |
|
---|
1742 | virtioLowerInterrupt(pDevIns, 0);
|
---|
1743 | return rcStrict;
|
---|
1744 | #else
|
---|
1745 | return VINF_IOM_R3_MMIO_READ;
|
---|
1746 | #endif
|
---|
1747 | }
|
---|
1748 |
|
---|
1749 | if (MATCHES_VIRTIO_CAP_STRUCT(off, cb, uOffset, pVirtio->LocCommonCfgCap))
|
---|
1750 | return virtioCommonCfgAccessed(pDevIns, pVirtio, pVirtioCC, false /* fWrite */, uOffset, cb, pv);
|
---|
1751 |
|
---|
1752 | if (MATCHES_VIRTIO_CAP_STRUCT(off, cb, uOffset, pVirtio->LocIsrCap) && cb == sizeof(uint8_t))
|
---|
1753 | {
|
---|
1754 | *(uint8_t *)pv = pVirtio->uISR;
|
---|
1755 | Log6Func(("Read and clear ISR\n"));
|
---|
1756 | pVirtio->uISR = 0; /* VirtIO specification requires reads of ISR to clear it */
|
---|
1757 | virtioLowerInterrupt(pDevIns, 0);
|
---|
1758 | return VINF_SUCCESS;
|
---|
1759 | }
|
---|
1760 |
|
---|
1761 | ASSERT_GUEST_MSG_FAILED(("Bad read access to mapped capabilities region: off=%RGp cb=%u\n", off, cb));
|
---|
1762 | return VINF_IOM_MMIO_UNUSED_00;
|
---|
1763 | }
|
---|
1764 |
|
---|
1765 | /**
|
---|
1766 | * @callback_method_impl{FNIOMMMIONEWREAD,
|
---|
1767 | * Memory mapped I/O Handler for PCI Capabilities write operations.}
|
---|
1768 | *
|
---|
1769 | * This MMIO handler specifically supports the VIRTIO_PCI_CAP_PCI_CFG capability defined
|
---|
1770 | * in the VirtIO 1.0 specification, section 4.1.4.7, and as such is restricted to writes
|
---|
1771 | * of 1, 2 or 4 bytes, only.
|
---|
1772 | */
|
---|
1773 | static DECLCALLBACK(VBOXSTRICTRC) virtioMmioWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void const *pv, unsigned cb)
|
---|
1774 | {
|
---|
1775 | PVIRTIOCORE pVirtio = PDMINS_2_DATA(pDevIns, PVIRTIOCORE);
|
---|
1776 | PVIRTIOCORECC pVirtioCC = PDMINS_2_DATA_CC(pDevIns, PVIRTIOCORECC);
|
---|
1777 |
|
---|
1778 | AssertReturn(cb == 1 || cb == 2 || cb == 4, VERR_INVALID_PARAMETER);
|
---|
1779 |
|
---|
1780 | Assert(pVirtio == (PVIRTIOCORE)pvUser); RT_NOREF(pvUser);
|
---|
1781 | uint32_t uOffset;
|
---|
1782 | if (MATCHES_VIRTIO_CAP_STRUCT(off, cb, uOffset, pVirtio->LocDeviceCap))
|
---|
1783 | {
|
---|
1784 | #ifdef IN_RING3
|
---|
1785 | /*
|
---|
1786 | * Pass this MMIO write access back to the client to handle
|
---|
1787 | */
|
---|
1788 | return pVirtioCC->pfnDevCapWrite(pDevIns, uOffset, pv, cb);
|
---|
1789 | #else
|
---|
1790 | return VINF_IOM_R3_MMIO_WRITE;
|
---|
1791 | #endif
|
---|
1792 | }
|
---|
1793 |
|
---|
1794 | if (MATCHES_VIRTIO_CAP_STRUCT(off, cb, uOffset, pVirtio->LocCommonCfgCap))
|
---|
1795 | return virtioCommonCfgAccessed(pDevIns, pVirtio, pVirtioCC, true /* fWrite */, uOffset, cb, (void *)pv);
|
---|
1796 |
|
---|
1797 | if (MATCHES_VIRTIO_CAP_STRUCT(off, cb, uOffset, pVirtio->LocIsrCap) && cb == sizeof(uint8_t))
|
---|
1798 | {
|
---|
1799 | pVirtio->uISR = *(uint8_t *)pv;
|
---|
1800 | Log6Func(("Setting uISR = 0x%02x (virtq interrupt: %d, dev confg interrupt: %d)\n",
|
---|
1801 | pVirtio->uISR & 0xff,
|
---|
1802 | pVirtio->uISR & VIRTIO_ISR_VIRTQ_INTERRUPT,
|
---|
1803 | RT_BOOL(pVirtio->uISR & VIRTIO_ISR_DEVICE_CONFIG)));
|
---|
1804 | return VINF_SUCCESS;
|
---|
1805 | }
|
---|
1806 |
|
---|
1807 | /* This *should* be guest driver dropping index of a new descriptor in avail ring */
|
---|
1808 | if (MATCHES_VIRTIO_CAP_STRUCT(off, cb, uOffset, pVirtio->LocNotifyCap) && cb == sizeof(uint16_t))
|
---|
1809 | {
|
---|
1810 | virtioCoreQueueNotified(pDevIns, pVirtio, uOffset / VIRTIO_NOTIFY_OFFSET_MULTIPLIER, *(uint16_t *)pv);
|
---|
1811 | return VINF_SUCCESS;
|
---|
1812 | }
|
---|
1813 |
|
---|
1814 | ASSERT_GUEST_MSG_FAILED(("Bad write access to mapped capabilities region: off=%RGp pv=%#p{%.*Rhxs} cb=%u\n", off, pv, cb, pv, cb));
|
---|
1815 | return VINF_SUCCESS;
|
---|
1816 | }
|
---|
1817 |
|
---|
1818 | #ifdef IN_RING3
|
---|
1819 |
|
---|
1820 | /**
|
---|
1821 | * @callback_method_impl{FNPCICONFIGREAD}
|
---|
1822 | */
|
---|
1823 | static DECLCALLBACK(VBOXSTRICTRC) virtioR3PciConfigRead(PPDMDEVINS pDevIns, PPDMPCIDEV pPciDev,
|
---|
1824 | uint32_t uAddress, unsigned cb, uint32_t *pu32Value)
|
---|
1825 | {
|
---|
1826 | PVIRTIOCORE pVirtio = PDMINS_2_DATA(pDevIns, PVIRTIOCORE);
|
---|
1827 | PVIRTIOCORECC pVirtioCC = PDMINS_2_DATA_CC(pDevIns, PVIRTIOCORECC);
|
---|
1828 | RT_NOREF(pPciDev);
|
---|
1829 |
|
---|
1830 | Log7Func(("pDevIns=%p pPciDev=%p uAddress=%#x cb=%u pu32Value=%p\n",
|
---|
1831 | pDevIns, pPciDev, uAddress, cb, pu32Value));
|
---|
1832 | if (uAddress == pVirtio->uPciCfgDataOff)
|
---|
1833 | {
|
---|
1834 | /*
|
---|
1835 | * VirtIO 1.0 spec section 4.1.4.7 describes a required alternative access capability
|
---|
1836 | * whereby the guest driver can specify a bar, offset, and length via the PCI configuration space
|
---|
1837 | * (the virtio_pci_cfg_cap capability), and access data items.
|
---|
1838 | */
|
---|
1839 | struct virtio_pci_cap *pPciCap = &pVirtioCC->pPciCfgCap->pciCap;
|
---|
1840 | uint32_t uLength = pPciCap->uLength;
|
---|
1841 |
|
---|
1842 | if ( (uLength != 1 && uLength != 2 && uLength != 4)
|
---|
1843 | || cb != uLength
|
---|
1844 | || pPciCap->uBar != VIRTIO_REGION_PCI_CAP)
|
---|
1845 | {
|
---|
1846 | ASSERT_GUEST_MSG_FAILED(("Guest read virtio_pci_cfg_cap.pci_cfg_data using mismatching config. Ignoring\n"));
|
---|
1847 | *pu32Value = UINT32_MAX;
|
---|
1848 | return VINF_SUCCESS;
|
---|
1849 | }
|
---|
1850 |
|
---|
1851 | VBOXSTRICTRC rcStrict = virtioMmioRead(pDevIns, pVirtio, pPciCap->uOffset, pu32Value, cb);
|
---|
1852 | Log2Func(("virtio: Guest read virtio_pci_cfg_cap.pci_cfg_data, bar=%d, offset=%d, length=%d, result=%d -> %Rrc\n",
|
---|
1853 | pPciCap->uBar, pPciCap->uOffset, uLength, *pu32Value, VBOXSTRICTRC_VAL(rcStrict)));
|
---|
1854 | return rcStrict;
|
---|
1855 | }
|
---|
1856 | return VINF_PDM_PCI_DO_DEFAULT;
|
---|
1857 | }
|
---|
1858 |
|
---|
1859 | /**
|
---|
1860 | * @callback_method_impl{FNPCICONFIGWRITE}
|
---|
1861 | */
|
---|
1862 | static DECLCALLBACK(VBOXSTRICTRC) virtioR3PciConfigWrite(PPDMDEVINS pDevIns, PPDMPCIDEV pPciDev,
|
---|
1863 | uint32_t uAddress, unsigned cb, uint32_t u32Value)
|
---|
1864 | {
|
---|
1865 | PVIRTIOCORE pVirtio = PDMINS_2_DATA(pDevIns, PVIRTIOCORE);
|
---|
1866 | PVIRTIOCORECC pVirtioCC = PDMINS_2_DATA_CC(pDevIns, PVIRTIOCORECC);
|
---|
1867 | RT_NOREF(pPciDev);
|
---|
1868 |
|
---|
1869 | Log7Func(("pDevIns=%p pPciDev=%p uAddress=%#x cb=%u u32Value=%#x\n", pDevIns, pPciDev, uAddress, cb, u32Value));
|
---|
1870 | if (uAddress == pVirtio->uPciCfgDataOff)
|
---|
1871 | {
|
---|
1872 | /* VirtIO 1.0 spec section 4.1.4.7 describes a required alternative access capability
|
---|
1873 | * whereby the guest driver can specify a bar, offset, and length via the PCI configuration space
|
---|
1874 | * (the virtio_pci_cfg_cap capability), and access data items. */
|
---|
1875 |
|
---|
1876 | struct virtio_pci_cap *pPciCap = &pVirtioCC->pPciCfgCap->pciCap;
|
---|
1877 | uint32_t uLength = pPciCap->uLength;
|
---|
1878 |
|
---|
1879 | if ( (uLength != 1 && uLength != 2 && uLength != 4)
|
---|
1880 | || cb != uLength
|
---|
1881 | || pPciCap->uBar != VIRTIO_REGION_PCI_CAP)
|
---|
1882 | {
|
---|
1883 | ASSERT_GUEST_MSG_FAILED(("Guest write virtio_pci_cfg_cap.pci_cfg_data using mismatching config. Ignoring\n"));
|
---|
1884 | return VINF_SUCCESS;
|
---|
1885 | }
|
---|
1886 |
|
---|
1887 | VBOXSTRICTRC rcStrict = virtioMmioWrite(pDevIns, pVirtio, pPciCap->uOffset, &u32Value, cb);
|
---|
1888 | Log2Func(("Guest wrote virtio_pci_cfg_cap.pci_cfg_data, bar=%d, offset=%x, length=%x, value=%d -> %Rrc\n",
|
---|
1889 | pPciCap->uBar, pPciCap->uOffset, uLength, u32Value, VBOXSTRICTRC_VAL(rcStrict)));
|
---|
1890 | return rcStrict;
|
---|
1891 | }
|
---|
1892 | return VINF_PDM_PCI_DO_DEFAULT;
|
---|
1893 | }
|
---|
1894 |
|
---|
1895 |
|
---|
1896 | /*********************************************************************************************************************************
|
---|
1897 | * Saved state. *
|
---|
1898 | *********************************************************************************************************************************/
|
---|
1899 |
|
---|
1900 | /**
|
---|
1901 | * Called from the FNSSMDEVSAVEEXEC function of the device.
|
---|
1902 | *
|
---|
1903 | * @param pVirtio Pointer to the shared virtio state.
|
---|
1904 | * @param pHlp The ring-3 device helpers.
|
---|
1905 | * @param pSSM The saved state handle.
|
---|
1906 | * @returns VBox status code.
|
---|
1907 | */
|
---|
1908 | int virtioCoreR3SaveExec(PVIRTIOCORE pVirtio, PCPDMDEVHLPR3 pHlp, PSSMHANDLE pSSM)
|
---|
1909 | {
|
---|
1910 | LogFunc(("\n"));
|
---|
1911 | pHlp->pfnSSMPutU64(pSSM, VIRTIO_SAVEDSTATE_MARKER);
|
---|
1912 | pHlp->pfnSSMPutU32(pSSM, VIRTIO_SAVEDSTATE_VERSION);
|
---|
1913 |
|
---|
1914 | pHlp->pfnSSMPutBool(pSSM, pVirtio->fGenUpdatePending);
|
---|
1915 | pHlp->pfnSSMPutU8(pSSM, pVirtio->uDeviceStatus);
|
---|
1916 | pHlp->pfnSSMPutU8(pSSM, pVirtio->uConfigGeneration);
|
---|
1917 | pHlp->pfnSSMPutU8(pSSM, pVirtio->uPciCfgDataOff);
|
---|
1918 | pHlp->pfnSSMPutU8(pSSM, pVirtio->uISR);
|
---|
1919 | pHlp->pfnSSMPutU16(pSSM, pVirtio->uQueueSelect);
|
---|
1920 | pHlp->pfnSSMPutU32(pSSM, pVirtio->uDeviceFeaturesSelect);
|
---|
1921 | pHlp->pfnSSMPutU32(pSSM, pVirtio->uDriverFeaturesSelect);
|
---|
1922 | pHlp->pfnSSMPutU64(pSSM, pVirtio->uDriverFeatures);
|
---|
1923 |
|
---|
1924 | for (uint32_t i = 0; i < VIRTQ_MAX_CNT; i++)
|
---|
1925 | {
|
---|
1926 | pHlp->pfnSSMPutGCPhys64(pSSM, pVirtio->aGCPhysQueueDesc[i]);
|
---|
1927 | pHlp->pfnSSMPutGCPhys64(pSSM, pVirtio->aGCPhysQueueAvail[i]);
|
---|
1928 | pHlp->pfnSSMPutGCPhys64(pSSM, pVirtio->aGCPhysQueueUsed[i]);
|
---|
1929 | pHlp->pfnSSMPutU16(pSSM, pVirtio->uQueueNotifyOff[i]);
|
---|
1930 | pHlp->pfnSSMPutU16(pSSM, pVirtio->uQueueMsixVector[i]);
|
---|
1931 | pHlp->pfnSSMPutU16(pSSM, pVirtio->uQueueEnable[i]);
|
---|
1932 | pHlp->pfnSSMPutU16(pSSM, pVirtio->uQueueSize[i]);
|
---|
1933 | pHlp->pfnSSMPutU16(pSSM, pVirtio->virtqState[i].uAvailIdxShadow);
|
---|
1934 | pHlp->pfnSSMPutU16(pSSM, pVirtio->virtqState[i].uUsedIdxShadow);
|
---|
1935 | int rc = pHlp->pfnSSMPutMem(pSSM, pVirtio->virtqState[i].szVirtqName, 32);
|
---|
1936 | AssertRCReturn(rc, rc);
|
---|
1937 | }
|
---|
1938 |
|
---|
1939 | return VINF_SUCCESS;
|
---|
1940 | }
|
---|
1941 |
|
---|
1942 | /**
|
---|
1943 | * Called from the FNSSMDEVLOADEXEC function of the device.
|
---|
1944 | *
|
---|
1945 | * @param pVirtio Pointer to the shared virtio state.
|
---|
1946 | * @param pHlp The ring-3 device helpers.
|
---|
1947 | * @param pSSM The saved state handle.
|
---|
1948 | * @returns VBox status code.
|
---|
1949 | */
|
---|
1950 | int virtioCoreR3LoadExec(PVIRTIOCORE pVirtio, PCPDMDEVHLPR3 pHlp, PSSMHANDLE pSSM)
|
---|
1951 | {
|
---|
1952 | LogFunc(("\n"));
|
---|
1953 | /*
|
---|
1954 | * Check the marker and (embedded) version number.
|
---|
1955 | */
|
---|
1956 | uint64_t uMarker = 0;
|
---|
1957 | int rc = pHlp->pfnSSMGetU64(pSSM, &uMarker);
|
---|
1958 | AssertRCReturn(rc, rc);
|
---|
1959 | if (uMarker != VIRTIO_SAVEDSTATE_MARKER)
|
---|
1960 | return pHlp->pfnSSMSetLoadError(pSSM, VERR_SSM_DATA_UNIT_FORMAT_CHANGED, RT_SRC_POS,
|
---|
1961 | N_("Expected marker value %#RX64 found %#RX64 instead"),
|
---|
1962 | VIRTIO_SAVEDSTATE_MARKER, uMarker);
|
---|
1963 | uint32_t uVersion = 0;
|
---|
1964 | rc = pHlp->pfnSSMGetU32(pSSM, &uVersion);
|
---|
1965 | AssertRCReturn(rc, rc);
|
---|
1966 | if (uVersion != VIRTIO_SAVEDSTATE_VERSION)
|
---|
1967 | return pHlp->pfnSSMSetLoadError(pSSM, VERR_SSM_DATA_UNIT_FORMAT_CHANGED, RT_SRC_POS,
|
---|
1968 | N_("Unsupported virtio version: %u"), uVersion);
|
---|
1969 | /*
|
---|
1970 | * Load the state.
|
---|
1971 | */
|
---|
1972 | pHlp->pfnSSMGetBool(pSSM, &pVirtio->fGenUpdatePending);
|
---|
1973 | pHlp->pfnSSMGetU8(pSSM, &pVirtio->uDeviceStatus);
|
---|
1974 | pHlp->pfnSSMGetU8(pSSM, &pVirtio->uConfigGeneration);
|
---|
1975 | pHlp->pfnSSMGetU8(pSSM, &pVirtio->uPciCfgDataOff);
|
---|
1976 | pHlp->pfnSSMGetU8(pSSM, &pVirtio->uISR);
|
---|
1977 | pHlp->pfnSSMGetU16(pSSM, &pVirtio->uQueueSelect);
|
---|
1978 | pHlp->pfnSSMGetU32(pSSM, &pVirtio->uDeviceFeaturesSelect);
|
---|
1979 | pHlp->pfnSSMGetU32(pSSM, &pVirtio->uDriverFeaturesSelect);
|
---|
1980 | pHlp->pfnSSMGetU64(pSSM, &pVirtio->uDriverFeatures);
|
---|
1981 |
|
---|
1982 | for (uint32_t i = 0; i < VIRTQ_MAX_CNT; i++)
|
---|
1983 | {
|
---|
1984 | pHlp->pfnSSMGetGCPhys64(pSSM, &pVirtio->aGCPhysQueueDesc[i]);
|
---|
1985 | pHlp->pfnSSMGetGCPhys64(pSSM, &pVirtio->aGCPhysQueueAvail[i]);
|
---|
1986 | pHlp->pfnSSMGetGCPhys64(pSSM, &pVirtio->aGCPhysQueueUsed[i]);
|
---|
1987 | pHlp->pfnSSMGetU16(pSSM, &pVirtio->uQueueNotifyOff[i]);
|
---|
1988 | pHlp->pfnSSMGetU16(pSSM, &pVirtio->uQueueMsixVector[i]);
|
---|
1989 | pHlp->pfnSSMGetU16(pSSM, &pVirtio->uQueueEnable[i]);
|
---|
1990 | pHlp->pfnSSMGetU16(pSSM, &pVirtio->uQueueSize[i]);
|
---|
1991 | pHlp->pfnSSMGetU16(pSSM, &pVirtio->virtqState[i].uAvailIdxShadow);
|
---|
1992 | pHlp->pfnSSMGetU16(pSSM, &pVirtio->virtqState[i].uUsedIdxShadow);
|
---|
1993 | rc = pHlp->pfnSSMGetMem(pSSM, pVirtio->virtqState[i].szVirtqName,
|
---|
1994 | sizeof(pVirtio->virtqState[i].szVirtqName));
|
---|
1995 | AssertRCReturn(rc, rc);
|
---|
1996 | }
|
---|
1997 |
|
---|
1998 | return VINF_SUCCESS;
|
---|
1999 | }
|
---|
2000 |
|
---|
2001 |
|
---|
2002 | /*********************************************************************************************************************************
|
---|
2003 | * Device Level *
|
---|
2004 | *********************************************************************************************************************************/
|
---|
2005 |
|
---|
2006 | /**
|
---|
2007 | * This must be called by the client to handle VM state changes
|
---|
2008 | * after the client takes care of its device-specific tasks for the state change.
|
---|
2009 | * (i.e. Reset, suspend, power-off, resume)
|
---|
2010 | *
|
---|
2011 | * @param pDevIns The device instance.
|
---|
2012 | * @param pVirtio Pointer to the shared virtio state.
|
---|
2013 | */
|
---|
2014 | void virtioCoreR3VmStateChanged(PVIRTIOCORE pVirtio, VIRTIOVMSTATECHANGED enmState)
|
---|
2015 | {
|
---|
2016 | LogFunc(("State changing to %s\n",
|
---|
2017 | virtioCoreGetStateChangeText(enmState)));
|
---|
2018 |
|
---|
2019 | switch(enmState)
|
---|
2020 | {
|
---|
2021 | case kvirtIoVmStateChangedReset:
|
---|
2022 | virtioCoreResetAll(pVirtio);
|
---|
2023 | break;
|
---|
2024 | case kvirtIoVmStateChangedSuspend:
|
---|
2025 | break;
|
---|
2026 | case kvirtIoVmStateChangedPowerOff:
|
---|
2027 | break;
|
---|
2028 | case kvirtIoVmStateChangedResume:
|
---|
2029 | virtioCoreNotifyGuestDriver(pVirtio->pDevInsR3, pVirtio, 0 /* idxQueue */);
|
---|
2030 | break;
|
---|
2031 | default:
|
---|
2032 | LogRelFunc(("Bad enum value"));
|
---|
2033 | return;
|
---|
2034 | }
|
---|
2035 | }
|
---|
2036 |
|
---|
2037 | /**
|
---|
2038 | * This should be called from PDMDEVREGR3::pfnDestruct.
|
---|
2039 | *
|
---|
2040 | * @param pDevIns The device instance.
|
---|
2041 | * @param pVirtio Pointer to the shared virtio state.
|
---|
2042 | * @param pVirtioCC Pointer to the ring-3 virtio state.
|
---|
2043 | */
|
---|
2044 | void virtioCoreR3Term(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, PVIRTIOCORECC pVirtioCC)
|
---|
2045 | {
|
---|
2046 | if (pVirtioCC->pbPrevDevSpecificCfg)
|
---|
2047 | {
|
---|
2048 | RTMemFree(pVirtioCC->pbPrevDevSpecificCfg);
|
---|
2049 | pVirtioCC->pbPrevDevSpecificCfg = NULL;
|
---|
2050 | }
|
---|
2051 | RT_NOREF(pDevIns, pVirtio);
|
---|
2052 | }
|
---|
2053 |
|
---|
2054 |
|
---|
2055 | /**rr
|
---|
2056 | * Setup PCI device controller and Virtio state
|
---|
2057 | *
|
---|
2058 | * This should be called from PDMDEVREGR3::pfnConstruct.
|
---|
2059 | *
|
---|
2060 | * @param pDevIns The device instance.
|
---|
2061 | * @param pVirtio Pointer to the shared virtio state. This
|
---|
2062 | * must be the first member in the shared
|
---|
2063 | * device instance data!
|
---|
2064 | * @param pVirtioCC Pointer to the ring-3 virtio state. This
|
---|
2065 | * must be the first member in the ring-3
|
---|
2066 | * device instance data!
|
---|
2067 | * @param pPciParams Values to populate industry standard PCI Configuration Space data structure
|
---|
2068 | * @param pcszInstance Device instance name (format-specifier)
|
---|
2069 | * @param fDevSpecificFeatures VirtIO device-specific features offered by
|
---|
2070 | * client
|
---|
2071 | * @param cbDevSpecificCfg Size of virtio_pci_device_cap device-specific struct
|
---|
2072 | * @param pvDevSpecificCfg Address of client's dev-specific
|
---|
2073 | * configuration struct.
|
---|
2074 | */
|
---|
2075 | int virtioCoreR3Init(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, PVIRTIOCORECC pVirtioCC, PVIRTIOPCIPARAMS pPciParams,
|
---|
2076 | const char *pcszInstance, uint64_t fDevSpecificFeatures, void *pvDevSpecificCfg, uint16_t cbDevSpecificCfg)
|
---|
2077 | {
|
---|
2078 | /*
|
---|
2079 | * The pVirtio state must be the first member of the shared device instance
|
---|
2080 | * data, otherwise we cannot get our bearings in the PCI configuration callbacks.
|
---|
2081 | */
|
---|
2082 | AssertLogRelReturn(pVirtio == PDMINS_2_DATA(pDevIns, PVIRTIOCORE), VERR_STATE_CHANGED);
|
---|
2083 | AssertLogRelReturn(pVirtioCC == PDMINS_2_DATA_CC(pDevIns, PVIRTIOCORECC), VERR_STATE_CHANGED);
|
---|
2084 |
|
---|
2085 | pVirtio->pDevInsR3 = pDevIns;
|
---|
2086 |
|
---|
2087 | /*
|
---|
2088 | * Caller must initialize these.
|
---|
2089 | */
|
---|
2090 | AssertReturn(pVirtioCC->pfnStatusChanged, VERR_INVALID_POINTER);
|
---|
2091 | AssertReturn(pVirtioCC->pfnQueueNotified, VERR_INVALID_POINTER);
|
---|
2092 |
|
---|
2093 | #if 0 /* Until pdmR3DvHlp_PCISetIrq() impl is fixed and Assert that limits vec to 0 is removed */
|
---|
2094 | # ifdef VBOX_WITH_MSI_DEVICES
|
---|
2095 | pVirtio->fMsiSupport = true;
|
---|
2096 | # endif
|
---|
2097 | #endif
|
---|
2098 |
|
---|
2099 | /*
|
---|
2100 | * The host features offered include both device-specific features
|
---|
2101 | * and reserved feature bits (device independent)
|
---|
2102 | */
|
---|
2103 | pVirtio->uDeviceFeatures = VIRTIO_F_VERSION_1
|
---|
2104 | | VIRTIO_DEV_INDEPENDENT_FEATURES_OFFERED
|
---|
2105 | | fDevSpecificFeatures;
|
---|
2106 |
|
---|
2107 | RTStrCopy(pVirtio->szInstance, sizeof(pVirtio->szInstance), pcszInstance);
|
---|
2108 |
|
---|
2109 | pVirtio->uDeviceStatus = 0;
|
---|
2110 | pVirtioCC->cbDevSpecificCfg = cbDevSpecificCfg;
|
---|
2111 | pVirtioCC->pbDevSpecificCfg = (uint8_t *)pvDevSpecificCfg;
|
---|
2112 | pVirtioCC->pbPrevDevSpecificCfg = (uint8_t *)RTMemDup(pvDevSpecificCfg, cbDevSpecificCfg);
|
---|
2113 | AssertLogRelReturn(pVirtioCC->pbPrevDevSpecificCfg, VERR_NO_MEMORY);
|
---|
2114 |
|
---|
2115 | /* Set PCI config registers (assume 32-bit mode) */
|
---|
2116 | PPDMPCIDEV pPciDev = pDevIns->apPciDevs[0];
|
---|
2117 | PDMPCIDEV_ASSERT_VALID(pDevIns, pPciDev);
|
---|
2118 |
|
---|
2119 | PDMPciDevSetRevisionId(pPciDev, DEVICE_PCI_REVISION_ID_VIRTIO);
|
---|
2120 | PDMPciDevSetVendorId(pPciDev, DEVICE_PCI_VENDOR_ID_VIRTIO);
|
---|
2121 | PDMPciDevSetSubSystemVendorId(pPciDev, DEVICE_PCI_VENDOR_ID_VIRTIO);
|
---|
2122 | PDMPciDevSetDeviceId(pPciDev, pPciParams->uDeviceId);
|
---|
2123 | PDMPciDevSetClassBase(pPciDev, pPciParams->uClassBase);
|
---|
2124 | PDMPciDevSetClassSub(pPciDev, pPciParams->uClassSub);
|
---|
2125 | PDMPciDevSetClassProg(pPciDev, pPciParams->uClassProg);
|
---|
2126 | PDMPciDevSetSubSystemId(pPciDev, pPciParams->uSubsystemId);
|
---|
2127 | PDMPciDevSetInterruptLine(pPciDev, pPciParams->uInterruptLine);
|
---|
2128 | PDMPciDevSetInterruptPin(pPciDev, pPciParams->uInterruptPin);
|
---|
2129 |
|
---|
2130 | /* Register PCI device */
|
---|
2131 | int rc = PDMDevHlpPCIRegister(pDevIns, pPciDev);
|
---|
2132 | if (RT_FAILURE(rc))
|
---|
2133 | return PDMDEV_SET_ERROR(pDevIns, rc, N_("virtio: cannot register PCI Device")); /* can we put params in this error? */
|
---|
2134 |
|
---|
2135 | rc = PDMDevHlpPCIInterceptConfigAccesses(pDevIns, pPciDev, virtioR3PciConfigRead, virtioR3PciConfigWrite);
|
---|
2136 | AssertRCReturn(rc, rc);
|
---|
2137 |
|
---|
2138 |
|
---|
2139 | /* Construct & map PCI vendor-specific capabilities for virtio host negotiation with guest driver */
|
---|
2140 |
|
---|
2141 | /* The following capability mapped via VirtIO 1.0: struct virtio_pci_cfg_cap (VIRTIO_PCI_CFG_CAP_T)
|
---|
2142 | * as a mandatory but suboptimal alternative interface to host device capabilities, facilitating
|
---|
2143 | * access the memory of any BAR. If the guest uses it (the VirtIO driver on Linux doesn't),
|
---|
2144 | * Unlike Common, Notify, ISR and Device capabilities, it is accessed directly via PCI Config region.
|
---|
2145 | * therefore does not contribute to the capabilities region (BAR) the other capabilities use.
|
---|
2146 | */
|
---|
2147 | #define CFG_ADDR_2_IDX(addr) ((uint8_t)(((uintptr_t)(addr) - (uintptr_t)&pPciDev->abConfig[0])))
|
---|
2148 | #define SET_PCI_CAP_LOC(a_pPciDev, a_pCfg, a_LocCap, a_uMmioLengthAlign) \
|
---|
2149 | do { \
|
---|
2150 | (a_LocCap).offMmio = (a_pCfg)->uOffset; \
|
---|
2151 | (a_LocCap).cbMmio = RT_ALIGN_T((a_pCfg)->uLength, a_uMmioLengthAlign, uint16_t); \
|
---|
2152 | (a_LocCap).offPci = (uint16_t)(uintptr_t)((uint8_t *)(a_pCfg) - &(a_pPciDev)->abConfig[0]); \
|
---|
2153 | (a_LocCap).cbPci = (a_pCfg)->uCapLen; \
|
---|
2154 | } while (0)
|
---|
2155 |
|
---|
2156 | PVIRTIO_PCI_CAP_T pCfg;
|
---|
2157 | uint32_t cbRegion = 0;
|
---|
2158 |
|
---|
2159 | /* Common capability (VirtIO 1.0 spec, section 4.1.4.3) */
|
---|
2160 | pCfg = (PVIRTIO_PCI_CAP_T)&pPciDev->abConfig[0x40];
|
---|
2161 | pCfg->uCfgType = VIRTIO_PCI_CAP_COMMON_CFG;
|
---|
2162 | pCfg->uCapVndr = VIRTIO_PCI_CAP_ID_VENDOR;
|
---|
2163 | pCfg->uCapLen = sizeof(VIRTIO_PCI_CAP_T);
|
---|
2164 | pCfg->uCapNext = CFG_ADDR_2_IDX(pCfg) + pCfg->uCapLen;
|
---|
2165 | pCfg->uBar = VIRTIO_REGION_PCI_CAP;
|
---|
2166 | pCfg->uOffset = RT_ALIGN_32(0, 4); /* reminder, in case someone changes offset */
|
---|
2167 | pCfg->uLength = sizeof(VIRTIO_PCI_COMMON_CFG_T);
|
---|
2168 | cbRegion += pCfg->uLength;
|
---|
2169 | SET_PCI_CAP_LOC(pPciDev, pCfg, pVirtio->LocCommonCfgCap, 2);
|
---|
2170 | pVirtioCC->pCommonCfgCap = pCfg;
|
---|
2171 |
|
---|
2172 | /*
|
---|
2173 | * Notify capability (VirtIO 1.0 spec, section 4.1.4.4). Note: uLength is based on the choice
|
---|
2174 | * of this implementation to make each queue's uQueueNotifyOff equal to (QueueSelect) ordinal
|
---|
2175 | * value of the queue (different strategies are possible according to spec).
|
---|
2176 | */
|
---|
2177 | pCfg = (PVIRTIO_PCI_CAP_T)&pPciDev->abConfig[pCfg->uCapNext];
|
---|
2178 | pCfg->uCfgType = VIRTIO_PCI_CAP_NOTIFY_CFG;
|
---|
2179 | pCfg->uCapVndr = VIRTIO_PCI_CAP_ID_VENDOR;
|
---|
2180 | pCfg->uCapLen = sizeof(VIRTIO_PCI_NOTIFY_CAP_T);
|
---|
2181 | pCfg->uCapNext = CFG_ADDR_2_IDX(pCfg) + pCfg->uCapLen;
|
---|
2182 | pCfg->uBar = VIRTIO_REGION_PCI_CAP;
|
---|
2183 | pCfg->uOffset = pVirtioCC->pCommonCfgCap->uOffset + pVirtioCC->pCommonCfgCap->uLength;
|
---|
2184 | pCfg->uOffset = RT_ALIGN_32(pCfg->uOffset, 4);
|
---|
2185 |
|
---|
2186 |
|
---|
2187 | pCfg->uLength = VIRTQ_MAX_CNT * VIRTIO_NOTIFY_OFFSET_MULTIPLIER + 2; /* will change in VirtIO 1.1 */
|
---|
2188 | cbRegion += pCfg->uLength;
|
---|
2189 | SET_PCI_CAP_LOC(pPciDev, pCfg, pVirtio->LocNotifyCap, 1);
|
---|
2190 | pVirtioCC->pNotifyCap = (PVIRTIO_PCI_NOTIFY_CAP_T)pCfg;
|
---|
2191 | pVirtioCC->pNotifyCap->uNotifyOffMultiplier = VIRTIO_NOTIFY_OFFSET_MULTIPLIER;
|
---|
2192 |
|
---|
2193 | /* ISR capability (VirtIO 1.0 spec, section 4.1.4.5)
|
---|
2194 | *
|
---|
2195 | * VirtIO 1.0 spec says 8-bit, unaligned in MMIO space. Example/diagram
|
---|
2196 | * of spec shows it as a 32-bit field with upper bits 'reserved'
|
---|
2197 | * Will take spec's words more literally than the diagram for now.
|
---|
2198 | */
|
---|
2199 | pCfg = (PVIRTIO_PCI_CAP_T)&pPciDev->abConfig[pCfg->uCapNext];
|
---|
2200 | pCfg->uCfgType = VIRTIO_PCI_CAP_ISR_CFG;
|
---|
2201 | pCfg->uCapVndr = VIRTIO_PCI_CAP_ID_VENDOR;
|
---|
2202 | pCfg->uCapLen = sizeof(VIRTIO_PCI_CAP_T);
|
---|
2203 | pCfg->uCapNext = CFG_ADDR_2_IDX(pCfg) + pCfg->uCapLen;
|
---|
2204 | pCfg->uBar = VIRTIO_REGION_PCI_CAP;
|
---|
2205 | pCfg->uOffset = pVirtioCC->pNotifyCap->pciCap.uOffset + pVirtioCC->pNotifyCap->pciCap.uLength;
|
---|
2206 | pCfg->uOffset = RT_ALIGN_32(pCfg->uOffset, 4);
|
---|
2207 | pCfg->uLength = sizeof(uint8_t);
|
---|
2208 | cbRegion += pCfg->uLength;
|
---|
2209 | SET_PCI_CAP_LOC(pPciDev, pCfg, pVirtio->LocIsrCap, 4);
|
---|
2210 | pVirtioCC->pIsrCap = pCfg;
|
---|
2211 |
|
---|
2212 | /* PCI Cfg capability (VirtIO 1.0 spec, section 4.1.4.7)
|
---|
2213 | * This capability doesn't get page-MMIO mapped. Instead uBar, uOffset and uLength are intercepted
|
---|
2214 | * by trapping PCI configuration I/O and get modulated by consumers to locate fetch and read/write
|
---|
2215 | * values from any region. NOTE: The linux driver not only doesn't use this feature, it will not
|
---|
2216 | * even list it as present if uLength isn't non-zero and also 4-byte-aligned as the linux driver is
|
---|
2217 | * initializing.
|
---|
2218 | */
|
---|
2219 | pVirtio->uPciCfgDataOff = pCfg->uCapNext + RT_OFFSETOF(VIRTIO_PCI_CFG_CAP_T, uPciCfgData);
|
---|
2220 | pCfg = (PVIRTIO_PCI_CAP_T)&pPciDev->abConfig[pCfg->uCapNext];
|
---|
2221 | pCfg->uCfgType = VIRTIO_PCI_CAP_PCI_CFG;
|
---|
2222 | pCfg->uCapVndr = VIRTIO_PCI_CAP_ID_VENDOR;
|
---|
2223 | pCfg->uCapLen = sizeof(VIRTIO_PCI_CFG_CAP_T);
|
---|
2224 | pCfg->uCapNext = (pVirtio->fMsiSupport || pVirtioCC->pbDevSpecificCfg) ? CFG_ADDR_2_IDX(pCfg) + pCfg->uCapLen : 0;
|
---|
2225 | pCfg->uBar = 0;
|
---|
2226 | pCfg->uOffset = 0;
|
---|
2227 | pCfg->uLength = 0;
|
---|
2228 | cbRegion += pCfg->uLength;
|
---|
2229 | SET_PCI_CAP_LOC(pPciDev, pCfg, pVirtio->LocPciCfgCap, 1);
|
---|
2230 | pVirtioCC->pPciCfgCap = (PVIRTIO_PCI_CFG_CAP_T)pCfg;
|
---|
2231 |
|
---|
2232 | if (pVirtioCC->pbDevSpecificCfg)
|
---|
2233 | {
|
---|
2234 | /* Following capability (via VirtIO 1.0, section 4.1.4.6). Client defines the
|
---|
2235 | * device-specific config fields struct and passes size to this constructor */
|
---|
2236 | pCfg = (PVIRTIO_PCI_CAP_T)&pPciDev->abConfig[pCfg->uCapNext];
|
---|
2237 | pCfg->uCfgType = VIRTIO_PCI_CAP_DEVICE_CFG;
|
---|
2238 | pCfg->uCapVndr = VIRTIO_PCI_CAP_ID_VENDOR;
|
---|
2239 | pCfg->uCapLen = sizeof(VIRTIO_PCI_CAP_T);
|
---|
2240 | pCfg->uCapNext = pVirtio->fMsiSupport ? CFG_ADDR_2_IDX(pCfg) + pCfg->uCapLen : 0;
|
---|
2241 | pCfg->uBar = VIRTIO_REGION_PCI_CAP;
|
---|
2242 | pCfg->uOffset = pVirtioCC->pIsrCap->uOffset + pVirtioCC->pIsrCap->uLength;
|
---|
2243 | pCfg->uOffset = RT_ALIGN_32(pCfg->uOffset, 4);
|
---|
2244 | pCfg->uLength = cbDevSpecificCfg;
|
---|
2245 | cbRegion += pCfg->uLength;
|
---|
2246 | SET_PCI_CAP_LOC(pPciDev, pCfg, pVirtio->LocDeviceCap, 4);
|
---|
2247 | pVirtioCC->pDeviceCap = pCfg;
|
---|
2248 | }
|
---|
2249 | else
|
---|
2250 | Assert(pVirtio->LocDeviceCap.cbMmio == 0 && pVirtio->LocDeviceCap.cbPci == 0);
|
---|
2251 |
|
---|
2252 | if (pVirtio->fMsiSupport)
|
---|
2253 | {
|
---|
2254 | PDMMSIREG aMsiReg;
|
---|
2255 | RT_ZERO(aMsiReg);
|
---|
2256 | aMsiReg.iMsixCapOffset = pCfg->uCapNext;
|
---|
2257 | aMsiReg.iMsixNextOffset = 0;
|
---|
2258 | aMsiReg.iMsixBar = VIRTIO_REGION_MSIX_CAP;
|
---|
2259 | aMsiReg.cMsixVectors = VBOX_MSIX_MAX_ENTRIES;
|
---|
2260 | rc = PDMDevHlpPCIRegisterMsi(pDevIns, &aMsiReg); /* see MsixR3init() */
|
---|
2261 | if (RT_FAILURE(rc))
|
---|
2262 | {
|
---|
2263 | /* See PDMDevHlp.cpp:pdmR3DevHlp_PCIRegisterMsi */
|
---|
2264 | LogFunc(("Failed to configure MSI-X (%Rrc). Reverting to INTx\n", rc));
|
---|
2265 | pVirtio->fMsiSupport = false;
|
---|
2266 | }
|
---|
2267 | else
|
---|
2268 | Log2Func(("Using MSI-X for guest driver notification\n"));
|
---|
2269 | }
|
---|
2270 | else
|
---|
2271 | LogFunc(("MSI-X not available for VBox, using INTx notification\n"));
|
---|
2272 |
|
---|
2273 | /* Set offset to first capability and enable PCI dev capabilities */
|
---|
2274 | PDMPciDevSetCapabilityList(pPciDev, 0x40);
|
---|
2275 | PDMPciDevSetStatus(pPciDev, VBOX_PCI_STATUS_CAP_LIST);
|
---|
2276 |
|
---|
2277 | /* Linux drivers/virtio/virtio_pci_modern.c tries to map at least a page for the
|
---|
2278 | * 'unknown' device-specific capability without querying the capability to figure
|
---|
2279 | * out size, so pad with an extra page
|
---|
2280 | */
|
---|
2281 | size_t cbSize = RTStrPrintf(pVirtioCC->pcszMmioName, sizeof(pVirtioCC->pcszMmioName), "%s MMIO", pcszInstance);
|
---|
2282 | if (cbSize <= 0)
|
---|
2283 | return PDMDEV_SET_ERROR(pDevIns, rc, N_("virtio: out of memory allocating string")); /* can we put params in this error? */
|
---|
2284 |
|
---|
2285 | rc = PDMDevHlpPCIIORegionCreateMmio(pDevIns, VIRTIO_REGION_PCI_CAP, RT_ALIGN_32(cbRegion + PAGE_SIZE, PAGE_SIZE),
|
---|
2286 | PCI_ADDRESS_SPACE_MEM, virtioMmioWrite, virtioMmioRead, pVirtio,
|
---|
2287 | IOMMMIO_FLAGS_READ_PASSTHRU | IOMMMIO_FLAGS_WRITE_PASSTHRU,
|
---|
2288 | pVirtioCC->pcszMmioName,
|
---|
2289 | &pVirtio->hMmioPciCap);
|
---|
2290 | AssertLogRelRCReturn(rc, PDMDEV_SET_ERROR(pDevIns, rc, N_("virtio: cannot register PCI Capabilities address space")));
|
---|
2291 | /*
|
---|
2292 | * Statistics.
|
---|
2293 | */
|
---|
2294 | PDMDevHlpSTAMRegisterF(pDevIns, &pVirtio->StatDescChainsAllocated, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT,
|
---|
2295 | "Total number of allocated descriptor chains", "DescChainsAllocated");
|
---|
2296 | PDMDevHlpSTAMRegisterF(pDevIns, &pVirtio->StatDescChainsFreed, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT,
|
---|
2297 | "Total number of freed descriptor chains", "DescChainsFreed");
|
---|
2298 | PDMDevHlpSTAMRegisterF(pDevIns, &pVirtio->StatDescChainsSegsIn, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT,
|
---|
2299 | "Total number of inbound segments", "DescChainsSegsIn");
|
---|
2300 | PDMDevHlpSTAMRegisterF(pDevIns, &pVirtio->StatDescChainsSegsOut, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT,
|
---|
2301 | "Total number of outbound segments", "DescChainsSegsOut");
|
---|
2302 |
|
---|
2303 | return VINF_SUCCESS;
|
---|
2304 | }
|
---|
2305 |
|
---|
2306 | #else /* !IN_RING3 */
|
---|
2307 |
|
---|
2308 | /**
|
---|
2309 | * Sets up the core ring-0/raw-mode virtio bits.
|
---|
2310 | *
|
---|
2311 | * @returns VBox status code.
|
---|
2312 | * @param pDevIns The device instance.
|
---|
2313 | * @param pVirtio Pointer to the shared virtio state. This must be the first
|
---|
2314 | * member in the shared device instance data!
|
---|
2315 | */
|
---|
2316 | int virtioCoreRZInit(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio)
|
---|
2317 | {
|
---|
2318 | AssertLogRelReturn(pVirtio == PDMINS_2_DATA(pDevIns, PVIRTIOCORE), VERR_STATE_CHANGED);
|
---|
2319 |
|
---|
2320 | #ifdef FUTURE_OPTIMIZATION
|
---|
2321 | int rc = PDMDevHlpSetDeviceCritSect(pDevIns, PDMDevHlpCritSectGetNop(pDevIns));
|
---|
2322 | AssertRCReturn(rc, rc);
|
---|
2323 | #endif
|
---|
2324 | int rc = PDMDevHlpMmioSetUpContext(pDevIns, pVirtio->hMmioPciCap, virtioMmioWrite, virtioMmioRead, pVirtio);
|
---|
2325 | AssertRCReturn(rc, rc);
|
---|
2326 | return rc;
|
---|
2327 | }
|
---|
2328 |
|
---|
2329 | #endif /* !IN_RING3 */
|
---|
2330 |
|
---|