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