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