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