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