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